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-2024 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 : 46781121 : is_instantiation_of_constexpr (tree fun)
61 : : {
62 : 62710792 : return ((DECL_TEMPLOID_INSTANTIATION (fun)
63 : 30930972 : && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
64 : 68477115 : || (DECL_DEFAULTED_FN (fun)
65 : 1145662 : && DECL_DECLARED_CONSTEXPR_P (fun)));
66 : : }
67 : :
68 : : /* Return true if T is a literal type. */
69 : :
70 : : bool
71 : 87819141 : literal_type_p (tree t)
72 : : {
73 : 85551910 : if (SCALAR_TYPE_P (t)
74 : 35810048 : || VECTOR_TYPE_P (t)
75 : 35798652 : || TYPE_REF_P (t)
76 : 117216376 : || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
77 : : return true;
78 : 27085402 : if (CLASS_TYPE_P (t))
79 : : {
80 : 25917395 : t = complete_type (t);
81 : 25917395 : gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
82 : 25917395 : return CLASSTYPE_LITERAL_P (t);
83 : : }
84 : 1168007 : if (TREE_CODE (t) == ARRAY_TYPE)
85 : 1167848 : 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 : 255042523 : ensure_literal_type_for_constexpr_object (tree decl)
95 : : {
96 : 255042523 : tree type = TREE_TYPE (decl);
97 : 255042523 : if (VAR_P (decl)
98 : 88676008 : && (DECL_DECLARED_CONSTEXPR_P (decl)
99 : 71333393 : || var_in_constexpr_fn (decl))
100 : 278103705 : && !processing_template_decl)
101 : : {
102 : 15087668 : tree stype = strip_array_types (type);
103 : 15087668 : 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 : 15087662 : else if (!literal_type_p (type))
107 : : {
108 : 5014 : if (DECL_DECLARED_CONSTEXPR_P (decl))
109 : : {
110 : 36 : auto_diagnostic_group d;
111 : 36 : error_at (DECL_SOURCE_LOCATION (decl),
112 : : "the type %qT of %<constexpr%> variable %qD "
113 : : "is not literal", type, decl);
114 : 36 : explain_non_literal_class (type);
115 : 36 : decl = error_mark_node;
116 : 36 : }
117 : 4978 : else if (cxx_dialect < cxx23)
118 : : {
119 : 2281 : 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++2b%> or %<-std=gnu++2b%>", decl, type);
126 : 8 : explain_non_literal_class (type);
127 : 8 : decl = error_mark_node;
128 : 8 : }
129 : 2281 : cp_function_chain->invalid_constexpr = true;
130 : : }
131 : : }
132 : 15082648 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
133 : 15082648 : && 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 : 255042523 : 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 : 639 : constexpr_error (location_t location, bool constexpr_fundef_p,
154 : : const char *gmsgid, ...)
155 : : {
156 : 639 : diagnostic_info diagnostic;
157 : 639 : va_list ap;
158 : 639 : rich_location richloc (line_table, location);
159 : 639 : va_start (ap, gmsgid);
160 : 639 : bool ret;
161 : 639 : if (!constexpr_fundef_p)
162 : : {
163 : : /* Report an error that cannot be suppressed. */
164 : 432 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ERROR);
165 : 432 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
166 : : }
167 : 207 : else if (warn_invalid_constexpr)
168 : : {
169 : 202 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
170 : 202 : cxx_dialect < cxx23 ? DK_PEDWARN : DK_WARNING);
171 : 202 : diagnostic.option_index = OPT_Winvalid_constexpr;
172 : 202 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
173 : : }
174 : : else
175 : : ret = false;
176 : 639 : va_end (ap);
177 : 1278 : return ret;
178 : 639 : }
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 : 434710607 : constexpr_fundef_hasher::equal (const constexpr_fundef *lhs,
197 : : const constexpr_fundef *rhs)
198 : : {
199 : 422110199 : 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 : 420078236 : constexpr_fundef_hasher::hash (const constexpr_fundef *fundef)
207 : : {
208 : 420078236 : return DECL_UID (fundef->decl);
209 : : }
210 : :
211 : : /* Return a previously saved definition of function FUN. */
212 : :
213 : : constexpr_fundef *
214 : 45608481 : retrieve_constexpr_fundef (tree fun)
215 : : {
216 : 45608481 : if (constexpr_fundef_table == NULL)
217 : : return NULL;
218 : :
219 : 45606233 : constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE };
220 : 45606233 : 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 : 15858706 : is_valid_constexpr_fn (tree fun, bool complain)
228 : : {
229 : 15858706 : bool ret = true;
230 : :
231 : 31717412 : if (DECL_INHERITED_CTOR (fun)
232 : 1732240 : && 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 : 15858706 : for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
242 : 31206678 : parm != NULL_TREE; parm = TREE_CHAIN (parm))
243 : 15347972 : if (!literal_type_p (TREE_TYPE (parm)))
244 : : {
245 : 8197 : ret = false;
246 : 8197 : if (complain)
247 : : {
248 : 20 : auto_diagnostic_group d;
249 : 20 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
250 : : "invalid type for parameter %d of "
251 : : "%<constexpr%> function %q+#D",
252 : 20 : DECL_PARM_INDEX (parm), fun))
253 : 20 : explain_non_literal_class (TREE_TYPE (parm));
254 : 20 : }
255 : : }
256 : : }
257 : :
258 : 25047796 : if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17)
259 : : {
260 : 1 : ret = false;
261 : 1 : if (complain)
262 : 1 : inform (DECL_SOURCE_LOCATION (fun),
263 : : "lambdas are implicitly %<constexpr%> only in C++17 and later");
264 : : }
265 : 31717410 : else if (DECL_DESTRUCTOR_P (fun) && cxx_dialect < cxx20)
266 : : {
267 : 72 : ret = false;
268 : 72 : 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 : 15858633 : else if (!DECL_CONSTRUCTOR_P (fun) && !DECL_DESTRUCTOR_P (fun))
274 : : {
275 : 13989889 : tree rettype = TREE_TYPE (TREE_TYPE (fun));
276 : 13989889 : if (!literal_type_p (rettype))
277 : : {
278 : 49382 : ret = false;
279 : 49382 : if (complain)
280 : : {
281 : 13 : auto_diagnostic_group d;
282 : 13 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
283 : : "invalid return type %qT of %<constexpr%> "
284 : : "function %q+D", rettype, fun))
285 : 13 : explain_non_literal_class (rettype);
286 : 13 : }
287 : : }
288 : :
289 : : /* C++14 DR 1684 removed this restriction. */
290 : 13989889 : if (cxx_dialect < cxx14
291 : 30638 : && DECL_IOBJ_MEMBER_FUNCTION_P (fun)
292 : 13990568 : && !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 : 1868744 : else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
307 : : {
308 : 127 : ret = false;
309 : 127 : if (complain)
310 : 6 : error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
311 : : }
312 : :
313 : 15858706 : 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 : 762 : 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 : 762 : auto_vec<tree, 2> fields;
331 : 1566 : do
332 : : {
333 : 1566 : fields.safe_push (TREE_OPERAND (member, 1));
334 : 1566 : member = TREE_OPERAND (member, 0);
335 : : }
336 : 1566 : while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
337 : 3135 : && 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 : 1566 : while (field = fields.pop(),
344 : 1566 : ANON_AGGR_TYPE_P (TREE_TYPE (field)))
345 : : {
346 : 804 : tree ctor;
347 : : /* If there is already an outer constructor entry for the anonymous
348 : : aggregate FIELD, use it; otherwise, insert one. */
349 : 804 : if (vec_safe_is_empty (*vec)
350 : 80 : || (*vec)->last().index != field)
351 : : {
352 : 762 : ctor = build_constructor (TREE_TYPE (field), NULL);
353 : 762 : CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
354 : : }
355 : : else
356 : 42 : ctor = (*vec)->last().value;
357 : 804 : 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 : 762 : gcc_assert (fields.is_empty());
363 : 762 : CONSTRUCTOR_APPEND_ELT (*vec, field, init);
364 : :
365 : 762 : return true;
366 : 762 : }
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 : 2454463 : build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
376 : : {
377 : 2663412 : tree member, init;
378 : 2663412 : if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
379 : 1063775 : t = TREE_OPERAND (t, 0);
380 : 2663412 : if (TREE_CODE (t) == EXPR_STMT)
381 : 1063855 : t = TREE_OPERAND (t, 0);
382 : 2663412 : if (t == error_mark_node)
383 : : return false;
384 : 2663406 : if (TREE_CODE (t) == STATEMENT_LIST)
385 : : {
386 : 2830410 : for (tree stmt : tsi_range (t))
387 : 167983 : if (! build_data_member_initialization (stmt, vec))
388 : 6 : return false;
389 : : return true;
390 : : }
391 : 2454674 : 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 : 199417 : return build_data_member_initialization (CLEANUP_BODY (t), vec);
399 : : }
400 : 2255257 : if (TREE_CODE (t) == CONVERT_EXPR)
401 : 1400349 : t = TREE_OPERAND (t, 0);
402 : 2255257 : 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 : 899408 : || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
407 : : {
408 : 1356208 : member = TREE_OPERAND (t, 0);
409 : 1356208 : init = break_out_target_exprs (TREE_OPERAND (t, 1));
410 : : }
411 : 899049 : else if (TREE_CODE (t) == CALL_EXPR)
412 : : {
413 : 807680 : tree fn = get_callee_fndecl (t);
414 : 1615360 : if (!fn || !DECL_CONSTRUCTOR_P (fn))
415 : : /* We're only interested in calls to subobject constructors. */
416 : : return true;
417 : 713949 : 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 : 713949 : init = break_out_target_exprs (t);
422 : : }
423 : 91369 : else if (TREE_CODE (t) == BIND_EXPR)
424 : 9532 : 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 : 2070157 : if (INDIRECT_REF_P (member))
429 : 27422 : member = TREE_OPERAND (member, 0);
430 : 2070157 : if (TREE_CODE (member) == NOP_EXPR)
431 : : {
432 : 194251 : tree op = member;
433 : 194251 : STRIP_NOPS (op);
434 : 194251 : if (TREE_CODE (op) == ADDR_EXPR)
435 : : {
436 : 235 : 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 : 194016 : else if (op == current_class_ptr
444 : 387932 : && (same_type_ignoring_top_level_qualifiers_p
445 : 193916 : (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 : 171100 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
454 : : }
455 : : }
456 : 2070157 : if (TREE_CODE (member) == ADDR_EXPR)
457 : 547355 : member = TREE_OPERAND (member, 0);
458 : 2070157 : if (TREE_CODE (member) == COMPONENT_REF)
459 : : {
460 : 1868105 : tree aggr = TREE_OPERAND (member, 0);
461 : 1868105 : if (TREE_CODE (aggr) == VAR_DECL)
462 : : /* Initializing a local variable, don't add anything. */
463 : : return true;
464 : 1868103 : if (TREE_CODE (aggr) != COMPONENT_REF)
465 : : /* Normal member initialization. */
466 : 1867124 : member = TREE_OPERAND (member, 1);
467 : 979 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
468 : : /* Initializing a member of an anonymous union. */
469 : 762 : 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 : 217 : 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 : 2493553 : if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
479 : 28 : (*vec)->last().value = init;
480 : : else
481 : 2069365 : 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 : 2537 : check_constexpr_bind_expr_vars (tree t)
491 : : {
492 : 2537 : gcc_assert (TREE_CODE (t) == BIND_EXPR);
493 : :
494 : 3408 : for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
495 : 883 : if (TREE_CODE (var) == TYPE_DECL
496 : 881 : && DECL_IMPLICIT_TYPEDEF_P (var)
497 : 901 : && !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 : 4420 : check_constexpr_ctor_body_1 (tree last, tree list)
506 : : {
507 : 4420 : 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 : 2 : case CLEANUP_POINT_EXPR:
516 : 2 : return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
517 : 2 : /*complain=*/false);
518 : :
519 : 2199 : case BIND_EXPR:
520 : 2199 : if (!check_constexpr_bind_expr_vars (list)
521 : 2199 : || !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 : 2900743 : 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 : 2900743 : if (cxx_dialect >= cxx14)
544 : : return true;
545 : :
546 : 10432 : bool ok = true;
547 : 10432 : if (TREE_CODE (list) == STATEMENT_LIST)
548 : : {
549 : 10421 : tree_stmt_iterator i = tsi_last (list);
550 : 14813 : for (; !tsi_end_p (i); tsi_prev (&i))
551 : : {
552 : 12565 : tree t = tsi_stmt (i);
553 : 12565 : if (t == last)
554 : : break;
555 : 4409 : if (!check_constexpr_ctor_body_1 (last, t))
556 : : {
557 : : ok = false;
558 : : break;
559 : : }
560 : : }
561 : : }
562 : 11 : else if (list != last
563 : 11 : && !check_constexpr_ctor_body_1 (last, list))
564 : : ok = false;
565 : 10421 : if (!ok)
566 : : {
567 : 20 : if (complain)
568 : 15 : error ("%<constexpr%> constructor does not have empty body");
569 : 20 : 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 : 1708553 : sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
582 : : {
583 : 1708553 : tree pri = CLASSTYPE_PRIMARY_BINFO (type);
584 : 1708553 : tree field_type;
585 : 1708553 : unsigned i;
586 : 1708553 : constructor_elt *ce;
587 : :
588 : 1708553 : if (pri)
589 : 6776 : field_type = BINFO_TYPE (pri);
590 : 1701777 : else if (TYPE_CONTAINS_VPTR_P (type))
591 : 20303 : 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 : 39188 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
598 : 19024 : if (TREE_TYPE (ce->index) == field_type)
599 : : break;
600 : :
601 : 35157 : 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 : 1731471 : build_constexpr_constructor_member_initializers (tree type, tree body)
618 : : {
619 : 1731471 : vec<constructor_elt, va_gc> *vec = NULL;
620 : 1731471 : bool ok = true;
621 : 3929078 : while (true)
622 : 3929078 : switch (TREE_CODE (body))
623 : : {
624 : 870731 : case MUST_NOT_THROW_EXPR:
625 : 870731 : case EH_SPEC_BLOCK:
626 : 870731 : body = TREE_OPERAND (body, 0);
627 : 870731 : break;
628 : :
629 : 1326876 : case STATEMENT_LIST:
630 : 5255976 : for (tree stmt : tsi_range (body))
631 : : {
632 : 2653768 : body = stmt;
633 : 2653768 : if (TREE_CODE (body) == BIND_EXPR)
634 : : break;
635 : : }
636 : : break;
637 : :
638 : 1731471 : case BIND_EXPR:
639 : 1731471 : body = BIND_EXPR_BODY (body);
640 : 1731471 : goto found;
641 : :
642 : 0 : default:
643 : 0 : gcc_unreachable ();
644 : : }
645 : 1731471 : found:
646 : 1731471 : 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 : 1731471 : if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
653 : : {
654 : 1132044 : body = TREE_OPERAND (body, 0);
655 : 1132044 : if (TREE_CODE (body) == EXPR_STMT)
656 : 1132044 : body = TREE_OPERAND (body, 0);
657 : 1132044 : if (TREE_CODE (body) == INIT_EXPR
658 : 1132044 : && (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 : 1132044 : ok = build_data_member_initialization (body, &vec);
666 : : }
667 : 599427 : else if (TREE_CODE (body) == STATEMENT_LIST)
668 : : {
669 : 1753635 : for (tree stmt : tsi_range (body))
670 : : {
671 : 1154322 : ok = build_data_member_initialization (stmt, &vec);
672 : 1154322 : if (!ok)
673 : : break;
674 : : }
675 : : }
676 : 114 : else if (EXPR_P (body))
677 : 114 : ok = build_data_member_initialization (body, &vec);
678 : : else
679 : 0 : gcc_assert (errorcount > 0);
680 : 1731471 : if (ok)
681 : : {
682 : 1731465 : if (vec_safe_length (vec) > 0)
683 : : {
684 : : /* In a delegating constructor, return the target. */
685 : 1645943 : constructor_elt *ce = &(*vec)[0];
686 : 1645943 : if (ce->index == current_class_ptr)
687 : : {
688 : 22912 : body = ce->value;
689 : 22912 : vec_free (vec);
690 : 22912 : return body;
691 : : }
692 : : }
693 : 1708553 : vec = sort_constexpr_mem_initializers (type, vec);
694 : 1708553 : return build_constructor (type, vec);
695 : : }
696 : : else
697 : 6 : 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 : 259349039 : get_function_named_in_call (tree t)
706 : : {
707 : 259349039 : tree callee = cp_get_callee (t);
708 : 259349039 : tree fun = cp_get_fndecl_from_callee (callee, /*fold*/false);
709 : 259349039 : 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 : 74174 : constexpr_fn_retval (tree body)
719 : : {
720 : 80404 : switch (TREE_CODE (body))
721 : : {
722 : 21746 : case STATEMENT_LIST:
723 : 21746 : {
724 : 21746 : tree expr = NULL_TREE;
725 : 65186 : for (tree stmt : tsi_range (body))
726 : : {
727 : 43460 : tree s = constexpr_fn_retval (stmt);
728 : 43460 : if (s == error_mark_node)
729 : 20 : return error_mark_node;
730 : 43440 : else if (s == NULL_TREE)
731 : : /* Keep iterating. */;
732 : 21718 : else if (expr)
733 : : /* Multiple return statements. */
734 : 0 : return error_mark_node;
735 : : else
736 : : expr = s;
737 : : }
738 : : return expr;
739 : : }
740 : :
741 : 30671 : case RETURN_EXPR:
742 : 30671 : return break_out_target_exprs (TREE_OPERAND (body, 0));
743 : :
744 : 4 : case DECL_EXPR:
745 : 4 : {
746 : 4 : tree decl = DECL_EXPR_DECL (body);
747 : 4 : if (TREE_CODE (decl) == USING_DECL
748 : : /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
749 : 4 : || DECL_ARTIFICIAL (decl))
750 : : return NULL_TREE;
751 : 2 : return error_mark_node;
752 : : }
753 : :
754 : 5898 : case CLEANUP_POINT_EXPR:
755 : 5898 : return constexpr_fn_retval (TREE_OPERAND (body, 0));
756 : :
757 : 338 : case BIND_EXPR:
758 : 338 : if (!check_constexpr_bind_expr_vars (body))
759 : 6 : return error_mark_node;
760 : 332 : return constexpr_fn_retval (BIND_EXPR_BODY (body));
761 : :
762 : : case USING_STMT:
763 : : case DEBUG_BEGIN_STMT:
764 : : return NULL_TREE;
765 : :
766 : 0 : case CALL_EXPR:
767 : 0 : {
768 : 0 : tree fun = get_function_named_in_call (body);
769 : 0 : if (fun != NULL_TREE
770 : 0 : && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE))
771 : : return NULL_TREE;
772 : : }
773 : : /* Fallthru. */
774 : :
775 : 27 : default:
776 : 27 : 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 : 15411818 : massage_constexpr_body (tree fun, tree body)
786 : : {
787 : 30823636 : if (DECL_CONSTRUCTOR_P (fun))
788 : 1731471 : body = build_constexpr_constructor_member_initializers
789 : 1731471 : (DECL_CONTEXT (fun), body);
790 : 13680347 : else if (cxx_dialect < cxx14)
791 : : {
792 : 30486 : if (TREE_CODE (body) == EH_SPEC_BLOCK)
793 : 0 : body = EH_SPEC_STMTS (body);
794 : 30486 : if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
795 : 20488 : body = TREE_OPERAND (body, 0);
796 : 30486 : body = constexpr_fn_retval (body);
797 : : }
798 : 15411818 : 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 : 1469593 : cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
806 : : {
807 : : /* We allow uninitialized bases/fields in C++20. */
808 : 1469593 : if (cxx_dialect >= cxx20)
809 : : return false;
810 : :
811 : 792234 : unsigned nelts = 0;
812 : :
813 : 792234 : if (body)
814 : : {
815 : 792230 : if (TREE_CODE (body) != CONSTRUCTOR)
816 : : return false;
817 : 792000 : nelts = CONSTRUCTOR_NELTS (body);
818 : : }
819 : 792004 : tree field = TYPE_FIELDS (ctype);
820 : :
821 : 792004 : if (TREE_CODE (ctype) == UNION_TYPE)
822 : : {
823 : 8193 : 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 : 8191 : 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 : 1694370 : for (unsigned i = 0; i <= nelts; ++i)
837 : : {
838 : 1694370 : tree index = NULL_TREE;
839 : 1694370 : if (i < nelts)
840 : : {
841 : 910559 : index = CONSTRUCTOR_ELT (body, i)->index;
842 : : /* Skip base and vtable inits. */
843 : 1178874 : if (TREE_CODE (index) != FIELD_DECL
844 : 910559 : || DECL_ARTIFICIAL (index))
845 : 268315 : continue;
846 : : }
847 : :
848 : 39563041 : for (; field != index; field = DECL_CHAIN (field))
849 : : {
850 : 38137054 : tree ftype;
851 : 38137054 : if (TREE_CODE (field) != FIELD_DECL)
852 : 75939162 : continue;
853 : 334860 : if (DECL_UNNAMED_BIT_FIELD (field))
854 : 12 : continue;
855 : 334848 : if (DECL_ARTIFICIAL (field))
856 : 334753 : continue;
857 : 95 : 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 : 68 : return true;
864 : 4 : continue;
865 : : }
866 : 91 : ftype = TREE_TYPE (field);
867 : 91 : 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 : 89 : if (is_empty_field (field))
872 : : /* An empty field doesn't need an initializer. */
873 : 1 : continue;
874 : 88 : ftype = strip_array_types (ftype);
875 : 88 : 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 : 2 : gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
881 : : || errorcount != 0);
882 : 2 : continue;
883 : : }
884 : 86 : if (!complain)
885 : : return true;
886 : 18 : auto_diagnostic_group d;
887 : 18 : error ("member %qD must be initialized by mem-initializer "
888 : : "in %<constexpr%> constructor", field);
889 : 18 : inform (DECL_SOURCE_LOCATION (field), "declared here");
890 : 18 : bad = true;
891 : 18 : }
892 : 1425987 : if (field == NULL_TREE)
893 : : break;
894 : :
895 : 642244 : 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 : 642244 : 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 : 131559134 : maybe_save_constexpr_fundef (tree fun)
915 : : {
916 : 131559134 : if (processing_template_decl
917 : 51754281 : || cp_function_chain->invalid_constexpr
918 : 183201552 : || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)))
919 : 116147559 : 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 : 38953900 : bool implicit = false;
924 : 38953900 : if (flag_implicit_constexpr)
925 : : {
926 : 8 : if (DECL_DELETING_DESTRUCTOR_P (fun)
927 : 8 : && 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 : 8 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
933 : 7 : && DECL_DECLARED_INLINE_P (fun)
934 : 14 : && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun)))
935 : : implicit = true;
936 : : }
937 : :
938 : 38953900 : if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit)
939 : : return;
940 : :
941 : 15464055 : bool complain = !DECL_GENERATED_P (fun) && !implicit;
942 : :
943 : 15464055 : if (!is_valid_constexpr_fn (fun, complain))
944 : : return;
945 : :
946 : 15411793 : tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
947 : 15411793 : if (massaged == NULL_TREE || massaged == error_mark_node)
948 : : {
949 : 48 : if (!DECL_CONSTRUCTOR_P (fun) && complain)
950 : 18 : error ("body of %<constexpr%> function %qD not a return-statement",
951 : : fun);
952 : 24 : return;
953 : : }
954 : :
955 : 15411769 : bool potential = potential_rvalue_constant_expression (massaged);
956 : 15411769 : if (!potential && complain)
957 : 164 : require_potential_rvalue_constant_expression_fncheck (massaged);
958 : :
959 : 17143228 : if (DECL_CONSTRUCTOR_P (fun) && potential
960 : 17141624 : && !DECL_DEFAULTED_FN (fun))
961 : : {
962 : 1469359 : if (cx_check_missing_mem_inits (DECL_CONTEXT (fun),
963 : : massaged, complain))
964 : : potential = false;
965 : 1469275 : 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 : 1464816 : massaged = DECL_SAVED_TREE (fun);
970 : 1464816 : potential = potential_rvalue_constant_expression (massaged);
971 : 1464816 : if (!potential && complain)
972 : 14 : require_potential_rvalue_constant_expression_fncheck (massaged);
973 : : }
974 : : }
975 : :
976 : 15411769 : 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 : 194 : && warn_invalid_constexpr != 0)
982 : : return;
983 : :
984 : 15411580 : if (implicit)
985 : : {
986 : 6 : 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 : 15411575 : constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE};
1000 : 15411575 : bool clear_ctx = false;
1001 : 15411575 : if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
1002 : : {
1003 : 15411575 : clear_ctx = true;
1004 : 15411575 : DECL_CONTEXT (DECL_RESULT (fun)) = fun;
1005 : : }
1006 : 15411575 : tree saved_fn = current_function_decl;
1007 : 15411575 : current_function_decl = fun;
1008 : 15411575 : entry.body = copy_fn (entry.decl, entry.parms, entry.result);
1009 : 15411575 : current_function_decl = saved_fn;
1010 : 15411575 : if (clear_ctx)
1011 : 15411575 : DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE;
1012 : 15411575 : 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 : 82247 : entry.result = error_mark_node;
1017 : :
1018 : 15411575 : 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 : 15446230 : register_constexpr_fundef (const constexpr_fundef &value)
1026 : : {
1027 : : /* Create the constexpr function table if necessary. */
1028 : 15446230 : if (constexpr_fundef_table == NULL)
1029 : 23621 : constexpr_fundef_table
1030 : 23621 : = hash_table<constexpr_fundef_hasher>::create_ggc (101);
1031 : :
1032 : 15446230 : constexpr_fundef **slot = constexpr_fundef_table->find_slot
1033 : 15446230 : (const_cast<constexpr_fundef *> (&value), INSERT);
1034 : :
1035 : 15446230 : gcc_assert (*slot == NULL);
1036 : 15446230 : *slot = ggc_alloc<constexpr_fundef> ();
1037 : 15446230 : **slot = value;
1038 : 15446230 : }
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 : 226 : explain_invalid_constexpr_fn (tree fun)
1048 : : {
1049 : 226 : static hash_set<tree> *diagnosed;
1050 : 226 : 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 : 226 : if (warn_invalid_constexpr == 0 && DECL_DECLARED_CONSTEXPR_P (fun))
1056 : : /* Go on. */;
1057 : : /* Only diagnose defaulted functions, lambdas, or instantiations. */
1058 : 221 : else if (!DECL_DEFAULTED_FN (fun)
1059 : 304 : && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
1060 : 431 : && !is_instantiation_of_constexpr (fun))
1061 : : {
1062 : 194 : inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun);
1063 : 194 : return;
1064 : : }
1065 : 32 : if (diagnosed == NULL)
1066 : 27 : diagnosed = new hash_set<tree>;
1067 : 32 : if (diagnosed->add (fun))
1068 : : /* Already explained. */
1069 : : return;
1070 : :
1071 : 31 : iloc_sentinel ils = input_location;
1072 : 31 : if (!lambda_static_thunk_p (fun))
1073 : : {
1074 : : /* Diagnostics should completely ignore the static thunk, so leave
1075 : : input_location set to our caller's location. */
1076 : 31 : input_location = DECL_SOURCE_LOCATION (fun);
1077 : 31 : inform (input_location,
1078 : : "%qD is not usable as a %<constexpr%> function because:", fun);
1079 : : }
1080 : : /* First check the declaration. */
1081 : 31 : if (is_valid_constexpr_fn (fun, true))
1082 : : {
1083 : : /* Then if it's OK, the body. */
1084 : 28 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
1085 : 28 : && DECL_DEFAULTED_FN (fun))
1086 : 3 : explain_implicit_non_constexpr (fun);
1087 : : else
1088 : : {
1089 : 25 : if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun))
1090 : 23 : body = fd->body;
1091 : : else
1092 : 2 : body = DECL_SAVED_TREE (fun);
1093 : 25 : body = massage_constexpr_body (fun, body);
1094 : 25 : require_potential_rvalue_constant_expression (body);
1095 : 50 : if (DECL_CONSTRUCTOR_P (fun))
1096 : : {
1097 : 6 : cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true);
1098 : 6 : if (cxx_dialect > cxx11)
1099 : : {
1100 : : /* Also check the body, not just the ctor-initializer. */
1101 : 6 : body = DECL_SAVED_TREE (fun);
1102 : 6 : require_potential_rvalue_constant_expression (body);
1103 : : }
1104 : : }
1105 : : }
1106 : : }
1107 : 31 : }
1108 : :
1109 : : /* Objects of this type represent calls to constexpr functions
1110 : : along with the bindings of parameters to their arguments, for
1111 : : the purpose of compile time evaluation. */
1112 : :
1113 : : struct GTY((for_user)) constexpr_call {
1114 : : /* Description of the constexpr function definition. */
1115 : : constexpr_fundef *fundef;
1116 : : /* Parameter bindings environment. A TREE_VEC of arguments. */
1117 : : tree bindings;
1118 : : /* Result of the call.
1119 : : NULL means the call is being evaluated.
1120 : : error_mark_node means that the evaluation was erroneous;
1121 : : otherwise, the actuall value of the call. */
1122 : : tree result;
1123 : : /* The hash of this call; we remember it here to avoid having to
1124 : : recalculate it when expanding the hash table. */
1125 : : hashval_t hash;
1126 : : /* The value of constexpr_ctx::manifestly_const_eval. */
1127 : : enum mce_value manifestly_const_eval;
1128 : : };
1129 : :
1130 : : struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
1131 : : {
1132 : : static hashval_t hash (constexpr_call *);
1133 : : static bool equal (constexpr_call *, constexpr_call *);
1134 : : };
1135 : :
1136 : : enum constexpr_switch_state {
1137 : : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1138 : : and default: label for that switch has not been seen yet. */
1139 : : css_default_not_seen,
1140 : : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1141 : : and default: label for that switch has been seen already. */
1142 : : css_default_seen,
1143 : : /* Used when processing a switch for the second time by
1144 : : cxx_eval_switch_expr, where default: label should match. */
1145 : : css_default_processing
1146 : : };
1147 : :
1148 : : /* The constexpr expansion context part which needs one instance per
1149 : : cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1150 : : variables initialized within the expression. */
1151 : :
1152 : 323027447 : class constexpr_global_ctx {
1153 : : /* Values for any temporaries or local variables within the
1154 : : constant-expression. Objects outside their lifetime have
1155 : : value 'void_node'. */
1156 : : hash_map<tree,tree> values;
1157 : : public:
1158 : : /* Number of cxx_eval_constant_expression calls (except skipped ones,
1159 : : on simple constants or location wrappers) encountered during current
1160 : : cxx_eval_outermost_constant_expr call. */
1161 : : HOST_WIDE_INT constexpr_ops_count;
1162 : : /* Heap VAR_DECLs created during the evaluation of the outermost constant
1163 : : expression. */
1164 : : auto_vec<tree, 16> heap_vars;
1165 : : /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1166 : : vec<tree> *cleanups;
1167 : : /* If non-null, only allow modification of existing values of the variables
1168 : : in this set. Set by modifiable_tracker, below. */
1169 : : hash_set<tree> *modifiable;
1170 : : /* Number of heap VAR_DECL deallocations. */
1171 : : unsigned heap_dealloc_count;
1172 : : /* Constructor. */
1173 : 323038231 : constexpr_global_ctx ()
1174 : 646076462 : : constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr),
1175 : 323038231 : heap_dealloc_count (0) {}
1176 : :
1177 : 177074479 : bool is_outside_lifetime (tree t)
1178 : : {
1179 : 177074479 : if (tree *p = values.get (t))
1180 : 38017799 : if (*p == void_node)
1181 : 162 : return true;
1182 : : return false;
1183 : : }
1184 : 215044450 : tree get_value (tree t)
1185 : : {
1186 : 215044450 : if (tree *p = values.get (t))
1187 : 67786616 : if (*p != void_node)
1188 : 67374298 : return *p;
1189 : : return NULL_TREE;
1190 : : }
1191 : 30985568 : tree *get_value_ptr (tree t, bool initializing)
1192 : : {
1193 : 30985568 : if (modifiable && !modifiable->contains (t))
1194 : : return nullptr;
1195 : 30985540 : if (tree *p = values.get (t))
1196 : : {
1197 : 30982914 : if (*p != void_node)
1198 : : return p;
1199 : 31 : else if (initializing)
1200 : : {
1201 : 4 : *p = NULL_TREE;
1202 : 4 : return p;
1203 : : }
1204 : : }
1205 : : return nullptr;
1206 : : }
1207 : 87459643 : void put_value (tree t, tree v)
1208 : : {
1209 : 87459643 : bool already_in_map = values.put (t, v);
1210 : 87459643 : if (!already_in_map && modifiable)
1211 : 28 : modifiable->add (t);
1212 : 87459643 : }
1213 : 72503111 : void destroy_value (tree t)
1214 : : {
1215 : 72503111 : if (TREE_CODE (t) == VAR_DECL
1216 : 72503111 : || TREE_CODE (t) == PARM_DECL
1217 : : || TREE_CODE (t) == RESULT_DECL)
1218 : 72497461 : values.put (t, void_node);
1219 : : else
1220 : 5650 : values.remove (t);
1221 : 72503111 : }
1222 : 2993615 : void clear_value (tree t)
1223 : : {
1224 : 5987230 : values.remove (t);
1225 : : }
1226 : : };
1227 : :
1228 : : /* Helper class for constexpr_global_ctx. In some cases we want to avoid
1229 : : side-effects from evaluation of a particular subexpression of a
1230 : : constant-expression. In such cases we use modifiable_tracker to prevent
1231 : : modification of variables created outside of that subexpression.
1232 : :
1233 : : ??? We could change the hash_set to a hash_map, allow and track external
1234 : : modifications, and roll them back in the destructor. It's not clear to me
1235 : : that this would be worthwhile. */
1236 : :
1237 : : class modifiable_tracker
1238 : : {
1239 : : hash_set<tree> set;
1240 : : constexpr_global_ctx *global;
1241 : : public:
1242 : 149674 : modifiable_tracker (constexpr_global_ctx *g): global(g)
1243 : : {
1244 : 149674 : global->modifiable = &set;
1245 : 149674 : }
1246 : 149674 : ~modifiable_tracker ()
1247 : : {
1248 : 149702 : for (tree t: set)
1249 : 28 : global->clear_value (t);
1250 : 149674 : global->modifiable = nullptr;
1251 : 149674 : }
1252 : : };
1253 : :
1254 : : /* The constexpr expansion context. CALL is the current function
1255 : : expansion, CTOR is the current aggregate initializer, OBJECT is the
1256 : : object being initialized by CTOR, either a VAR_DECL or a _REF. */
1257 : :
1258 : : struct constexpr_ctx {
1259 : : /* The part of the context that needs to be unique to the whole
1260 : : cxx_eval_outermost_constant_expr invocation. */
1261 : : constexpr_global_ctx *global;
1262 : : /* The innermost call we're evaluating. */
1263 : : constexpr_call *call;
1264 : : /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1265 : : within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1266 : : vec<tree> *save_exprs;
1267 : : /* The CONSTRUCTOR we're currently building up for an aggregate
1268 : : initializer. */
1269 : : tree ctor;
1270 : : /* The object we're building the CONSTRUCTOR for. */
1271 : : tree object;
1272 : : /* If inside SWITCH_EXPR. */
1273 : : constexpr_switch_state *css_state;
1274 : : /* The aggregate initialization context inside which this one is nested. This
1275 : : is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1276 : : const constexpr_ctx *parent;
1277 : :
1278 : : /* Whether we should error on a non-constant expression or fail quietly.
1279 : : This flag needs to be here, but some of the others could move to global
1280 : : if they get larger than a word. */
1281 : : bool quiet;
1282 : : /* Whether we are strictly conforming to constant expression rules or
1283 : : trying harder to get a constant value. */
1284 : : bool strict;
1285 : : /* Whether __builtin_is_constant_evaluated () should be true. */
1286 : : mce_value manifestly_const_eval;
1287 : : };
1288 : :
1289 : : /* Remove T from the global values map, checking for attempts to destroy
1290 : : a value that has already finished its lifetime. */
1291 : :
1292 : : static void
1293 : 72496185 : destroy_value_checked (const constexpr_ctx* ctx, tree t, bool *non_constant_p)
1294 : : {
1295 : 72496185 : if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
1296 : : return;
1297 : :
1298 : : /* Don't error again here if we've already reported a problem. */
1299 : 72496185 : if (!*non_constant_p
1300 : 38178210 : && DECL_P (t)
1301 : : /* Non-trivial destructors have their lifetimes ended explicitly
1302 : : with a clobber, so don't worry about it here. */
1303 : 38172794 : && (!TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t))
1304 : : /* ...except parameters are remapped in cxx_eval_call_expression,
1305 : : and the destructor call during cleanup won't be able to tell that
1306 : : this value has already been destroyed, so complain now. This is
1307 : : not quite unobservable, but is extremely unlikely to crop up in
1308 : : practice; see g++.dg/cpp2a/constexpr-lifetime2.C. */
1309 : 144490 : || TREE_CODE (t) == PARM_DECL)
1310 : 110524651 : && ctx->global->is_outside_lifetime (t))
1311 : : {
1312 : 51 : if (!ctx->quiet)
1313 : : {
1314 : 17 : auto_diagnostic_group d;
1315 : 17 : error ("destroying %qE outside its lifetime", t);
1316 : 17 : inform (DECL_SOURCE_LOCATION (t), "declared here");
1317 : 17 : }
1318 : 51 : *non_constant_p = true;
1319 : : }
1320 : 72496185 : ctx->global->destroy_value (t);
1321 : : }
1322 : :
1323 : : /* This internal flag controls whether we should avoid doing anything during
1324 : : constexpr evaluation that would cause extra DECL_UID generation, such as
1325 : : template instantiation and function body copying. */
1326 : :
1327 : : static bool uid_sensitive_constexpr_evaluation_value;
1328 : :
1329 : : /* An internal counter that keeps track of the number of times
1330 : : uid_sensitive_constexpr_evaluation_p returned true. */
1331 : :
1332 : : static unsigned uid_sensitive_constexpr_evaluation_true_counter;
1333 : :
1334 : : /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1335 : : increments the corresponding counter. */
1336 : :
1337 : : static bool
1338 : 6018747 : uid_sensitive_constexpr_evaluation_p ()
1339 : : {
1340 : 6014490 : if (uid_sensitive_constexpr_evaluation_value)
1341 : : {
1342 : 1607896 : ++uid_sensitive_constexpr_evaluation_true_counter;
1343 : 1607896 : return true;
1344 : : }
1345 : : else
1346 : : return false;
1347 : : }
1348 : :
1349 : : /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1350 : : enables the internal flag for uid_sensitive_constexpr_evaluation_p
1351 : : during the lifetime of the sentinel object. Upon its destruction, the
1352 : : previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1353 : :
1354 : 60427338 : uid_sensitive_constexpr_evaluation_sentinel
1355 : 60427338 : ::uid_sensitive_constexpr_evaluation_sentinel ()
1356 : 60427338 : : ovr (uid_sensitive_constexpr_evaluation_value, true)
1357 : : {
1358 : 60427338 : }
1359 : :
1360 : : /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1361 : : records the current number of times that uid_sensitive_constexpr_evaluation_p
1362 : : has been called and returned true. */
1363 : :
1364 : 1760534168 : uid_sensitive_constexpr_evaluation_checker
1365 : 1760534168 : ::uid_sensitive_constexpr_evaluation_checker ()
1366 : 1760534168 : : saved_counter (uid_sensitive_constexpr_evaluation_true_counter)
1367 : : {
1368 : 1760534168 : }
1369 : :
1370 : : /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1371 : : some constexpr evaluation was restricted due to u_s_c_e_p being called
1372 : : and returning true during the lifetime of this checker object. */
1373 : :
1374 : : bool
1375 : 1239466548 : uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1376 : : {
1377 : 1239466548 : return (uid_sensitive_constexpr_evaluation_value
1378 : 1239466548 : && saved_counter != uid_sensitive_constexpr_evaluation_true_counter);
1379 : : }
1380 : :
1381 : :
1382 : : /* A table of all constexpr calls that have been evaluated by the
1383 : : compiler in this translation unit. */
1384 : :
1385 : : static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
1386 : :
1387 : : /* Compute a hash value for a constexpr call representation. */
1388 : :
1389 : : inline hashval_t
1390 : 97461685 : constexpr_call_hasher::hash (constexpr_call *info)
1391 : : {
1392 : 97461685 : return info->hash;
1393 : : }
1394 : :
1395 : : /* Return true if the objects pointed to by P and Q represent calls
1396 : : to the same constexpr function with the same arguments.
1397 : : Otherwise, return false. */
1398 : :
1399 : : bool
1400 : 103640675 : constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1401 : : {
1402 : 103640675 : if (lhs == rhs)
1403 : : return true;
1404 : 103640675 : if (lhs->hash != rhs->hash)
1405 : : return false;
1406 : 12600408 : if (lhs->manifestly_const_eval != rhs->manifestly_const_eval)
1407 : : return false;
1408 : 12600408 : if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
1409 : : return false;
1410 : 12600408 : return cp_tree_equal (lhs->bindings, rhs->bindings);
1411 : : }
1412 : :
1413 : : /* Initialize the constexpr call table, if needed. */
1414 : :
1415 : : static void
1416 : 15744963 : maybe_initialize_constexpr_call_table (void)
1417 : : {
1418 : 15744963 : if (constexpr_call_table == NULL)
1419 : 15581 : constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1420 : 15744963 : }
1421 : :
1422 : : /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1423 : : a function happens to get called recursively, we unshare the callee
1424 : : function's body and evaluate this unshared copy instead of evaluating the
1425 : : original body.
1426 : :
1427 : : FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1428 : : copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1429 : : that's keyed off of the original FUNCTION_DECL and whose value is a
1430 : : TREE_LIST of this function's unused copies awaiting reuse.
1431 : :
1432 : : This is not GC-deletable to avoid GC affecting UID generation. */
1433 : :
1434 : : static GTY(()) decl_tree_map *fundef_copies_table;
1435 : :
1436 : : /* Reuse a copy or create a new unshared copy of the function FUN.
1437 : : Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1438 : : is parms, TYPE is result. */
1439 : :
1440 : : static tree
1441 : 31593056 : get_fundef_copy (constexpr_fundef *fundef)
1442 : : {
1443 : 31593056 : tree copy;
1444 : 31593056 : bool existed;
1445 : 31593056 : tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
1446 : 31593056 : (fundef_copies_table, fundef->decl, &existed, 127));
1447 : :
1448 : 31593056 : if (!existed)
1449 : : {
1450 : : /* There is no cached function available, or in use. We can use
1451 : : the function directly. That the slot is now created records
1452 : : that this function is now in use. */
1453 : 5046397 : copy = build_tree_list (fundef->body, fundef->parms);
1454 : 5046397 : TREE_TYPE (copy) = fundef->result;
1455 : : }
1456 : 26546659 : else if (*slot == NULL_TREE)
1457 : : {
1458 : 4257 : if (uid_sensitive_constexpr_evaluation_p ())
1459 : 0 : return NULL_TREE;
1460 : :
1461 : : /* We've already used the function itself, so make a copy. */
1462 : 4257 : copy = build_tree_list (NULL, NULL);
1463 : 4257 : tree saved_body = DECL_SAVED_TREE (fundef->decl);
1464 : 4257 : tree saved_parms = DECL_ARGUMENTS (fundef->decl);
1465 : 4257 : tree saved_result = DECL_RESULT (fundef->decl);
1466 : 4257 : tree saved_fn = current_function_decl;
1467 : 4257 : DECL_SAVED_TREE (fundef->decl) = fundef->body;
1468 : 4257 : DECL_ARGUMENTS (fundef->decl) = fundef->parms;
1469 : 4257 : DECL_RESULT (fundef->decl) = fundef->result;
1470 : 4257 : current_function_decl = fundef->decl;
1471 : 4257 : TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy),
1472 : 4257 : TREE_TYPE (copy));
1473 : 4257 : current_function_decl = saved_fn;
1474 : 4257 : DECL_RESULT (fundef->decl) = saved_result;
1475 : 4257 : DECL_ARGUMENTS (fundef->decl) = saved_parms;
1476 : 4257 : DECL_SAVED_TREE (fundef->decl) = saved_body;
1477 : : }
1478 : : else
1479 : : {
1480 : : /* We have a cached function available. */
1481 : 26542402 : copy = *slot;
1482 : 26542402 : *slot = TREE_CHAIN (copy);
1483 : : }
1484 : :
1485 : : return copy;
1486 : : }
1487 : :
1488 : : /* Save the copy COPY of function FUN for later reuse by
1489 : : get_fundef_copy(). By construction, there will always be an entry
1490 : : to find. */
1491 : :
1492 : : static void
1493 : 31593056 : save_fundef_copy (tree fun, tree copy)
1494 : : {
1495 : 31593056 : tree *slot = fundef_copies_table->get (fun);
1496 : 31593056 : TREE_CHAIN (copy) = *slot;
1497 : 31593056 : *slot = copy;
1498 : 31593056 : }
1499 : :
1500 : : /* Whether our evaluation wants a prvalue (e.g. CONSTRUCTOR or _CST),
1501 : : a glvalue (e.g. VAR_DECL or _REF), or nothing. */
1502 : :
1503 : : enum value_cat {
1504 : : vc_prvalue = 0,
1505 : : vc_glvalue = 1,
1506 : : vc_discard = 2
1507 : : };
1508 : :
1509 : : static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
1510 : : value_cat, bool *, bool *, tree * = NULL);
1511 : : static tree cxx_eval_bare_aggregate (const constexpr_ctx *, tree,
1512 : : value_cat, bool *, bool *);
1513 : : static tree cxx_fold_indirect_ref (const constexpr_ctx *, location_t, tree, tree,
1514 : : bool * = NULL);
1515 : : static tree find_heap_var_refs (tree *, int *, void *);
1516 : :
1517 : : /* Attempt to evaluate T which represents a call to a builtin function.
1518 : : We assume here that all builtin functions evaluate to scalar types
1519 : : represented by _CST nodes. */
1520 : :
1521 : : static tree
1522 : 10394873 : cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
1523 : : value_cat lval,
1524 : : bool *non_constant_p, bool *overflow_p)
1525 : : {
1526 : 10394873 : const int nargs = call_expr_nargs (t);
1527 : 10394873 : tree *args = (tree *) alloca (nargs * sizeof (tree));
1528 : 10394873 : tree new_call;
1529 : 10394873 : int i;
1530 : :
1531 : : /* Don't fold __builtin_constant_p within a constexpr function. */
1532 : 10394873 : bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun);
1533 : :
1534 : : /* If we aren't requiring a constant expression, defer __builtin_constant_p
1535 : : in a constexpr function until we have values for the parameters. */
1536 : 615763 : if (bi_const_p
1537 : 615763 : && ctx->manifestly_const_eval != mce_true
1538 : 606541 : && current_function_decl
1539 : 604396 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
1540 : : {
1541 : 309857 : *non_constant_p = true;
1542 : 309857 : return t;
1543 : : }
1544 : :
1545 : : /* For __builtin_is_constant_evaluated, defer it if not
1546 : : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
1547 : : without manifestly_const_eval even expressions or parts thereof which
1548 : : will later be manifestly const_eval evaluated), otherwise fold it to
1549 : : true. */
1550 : 10085016 : if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
1551 : : BUILT_IN_FRONTEND))
1552 : : {
1553 : 857921 : if (ctx->manifestly_const_eval == mce_unknown)
1554 : : {
1555 : 842230 : *non_constant_p = true;
1556 : 842230 : return t;
1557 : : }
1558 : 15691 : return constant_boolean_node (ctx->manifestly_const_eval == mce_true,
1559 : 15691 : boolean_type_node);
1560 : : }
1561 : :
1562 : 9227095 : if (fndecl_built_in_p (fun, CP_BUILT_IN_SOURCE_LOCATION, BUILT_IN_FRONTEND))
1563 : : {
1564 : 172 : temp_override<tree> ovr (current_function_decl);
1565 : 172 : if (ctx->call && ctx->call->fundef)
1566 : 53 : current_function_decl = ctx->call->fundef->decl;
1567 : 172 : return fold_builtin_source_location (t);
1568 : 172 : }
1569 : :
1570 : 9226923 : int strops = 0;
1571 : 9226923 : int strret = 0;
1572 : 9226923 : if (fndecl_built_in_p (fun, BUILT_IN_NORMAL))
1573 : 8621064 : switch (DECL_FUNCTION_CODE (fun))
1574 : : {
1575 : : case BUILT_IN_STRLEN:
1576 : : case BUILT_IN_STRNLEN:
1577 : 9226866 : strops = 1;
1578 : : break;
1579 : 102413 : case BUILT_IN_MEMCHR:
1580 : 102413 : case BUILT_IN_STRCHR:
1581 : 102413 : case BUILT_IN_STRRCHR:
1582 : 102413 : strops = 1;
1583 : 102413 : strret = 1;
1584 : 102413 : break;
1585 : 23803 : case BUILT_IN_MEMCMP:
1586 : 23803 : case BUILT_IN_STRCMP:
1587 : 23803 : strops = 2;
1588 : 23803 : break;
1589 : 29442 : case BUILT_IN_STRSTR:
1590 : 29442 : strops = 2;
1591 : 29442 : strret = 1;
1592 : 29442 : break;
1593 : 42 : case BUILT_IN_ASAN_POINTER_COMPARE:
1594 : 42 : case BUILT_IN_ASAN_POINTER_SUBTRACT:
1595 : : /* These builtins shall be ignored during constant expression
1596 : : evaluation. */
1597 : 42 : return void_node;
1598 : 15 : case BUILT_IN_UNREACHABLE:
1599 : 15 : case BUILT_IN_TRAP:
1600 : 15 : if (!*non_constant_p && !ctx->quiet)
1601 : : {
1602 : : /* Do not allow__builtin_unreachable in constexpr function.
1603 : : The __builtin_unreachable call with BUILTINS_LOCATION
1604 : : comes from cp_maybe_instrument_return. */
1605 : 1 : if (EXPR_LOCATION (t) == BUILTINS_LOCATION)
1606 : 0 : error ("%<constexpr%> call flows off the end of the function");
1607 : : else
1608 : 1 : error ("%q+E is not a constant expression", t);
1609 : : }
1610 : 15 : *non_constant_p = true;
1611 : 15 : return t;
1612 : : default:
1613 : : break;
1614 : : }
1615 : :
1616 : : /* Be permissive for arguments to built-ins; __builtin_constant_p should
1617 : : return constant false for a non-constant argument. */
1618 : 9226866 : constexpr_ctx new_ctx = *ctx;
1619 : 9226866 : new_ctx.quiet = true;
1620 : 25090396 : for (i = 0; i < nargs; ++i)
1621 : : {
1622 : 15863530 : tree arg = CALL_EXPR_ARG (t, i);
1623 : 15863530 : tree oarg = arg;
1624 : :
1625 : : /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
1626 : : expand_builtin doesn't know how to look in the values table. */
1627 : 15863530 : bool strop = i < strops;
1628 : 15863530 : if (strop)
1629 : : {
1630 : 285372 : STRIP_NOPS (arg);
1631 : 285372 : if (TREE_CODE (arg) == ADDR_EXPR)
1632 : 8047 : arg = TREE_OPERAND (arg, 0);
1633 : : else
1634 : : strop = false;
1635 : : }
1636 : :
1637 : : /* If builtin_valid_in_constant_expr_p is true,
1638 : : potential_constant_expression_1 has not recursed into the arguments
1639 : : of the builtin, verify it here. */
1640 : 15863530 : if (!builtin_valid_in_constant_expr_p (fun)
1641 : 15863530 : || potential_constant_expression (arg))
1642 : : {
1643 : 15863374 : bool dummy1 = false, dummy2 = false;
1644 : 15863374 : arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
1645 : : &dummy1, &dummy2);
1646 : : }
1647 : :
1648 : 15863530 : if (bi_const_p)
1649 : : /* For __builtin_constant_p, fold all expressions with constant values
1650 : : even if they aren't C++ constant-expressions. */
1651 : 305906 : arg = cp_fold_rvalue (arg);
1652 : 15557624 : else if (strop)
1653 : : {
1654 : 8047 : if (TREE_CODE (arg) == CONSTRUCTOR)
1655 : 113 : arg = braced_lists_to_strings (TREE_TYPE (arg), arg);
1656 : 8047 : if (TREE_CODE (arg) == STRING_CST)
1657 : 3104 : arg = build_address (arg);
1658 : : else
1659 : : arg = oarg;
1660 : : }
1661 : :
1662 : 15863530 : args[i] = arg;
1663 : : }
1664 : :
1665 : 9226866 : bool save_ffbcp = force_folding_builtin_constant_p;
1666 : 9226866 : force_folding_builtin_constant_p |= ctx->manifestly_const_eval == mce_true;
1667 : 9226866 : tree save_cur_fn = current_function_decl;
1668 : : /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
1669 : 9226866 : if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION)
1670 : 41 : && ctx->call
1671 : 9226869 : && ctx->call->fundef)
1672 : 3 : current_function_decl = ctx->call->fundef->decl;
1673 : 9226866 : if (fndecl_built_in_p (fun,
1674 : : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
1675 : : BUILT_IN_FRONTEND))
1676 : : {
1677 : 156 : location_t loc = EXPR_LOCATION (t);
1678 : 156 : if (nargs >= 1)
1679 : 155 : VERIFY_CONSTANT (args[0]);
1680 : 81 : new_call
1681 : 81 : = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs,
1682 : : args);
1683 : : }
1684 : 9226710 : else if (fndecl_built_in_p (fun,
1685 : : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
1686 : : BUILT_IN_FRONTEND))
1687 : : {
1688 : 211 : location_t loc = EXPR_LOCATION (t);
1689 : 211 : if (nargs >= 2)
1690 : : {
1691 : 209 : VERIFY_CONSTANT (args[0]);
1692 : 127 : VERIFY_CONSTANT (args[1]);
1693 : : }
1694 : 129 : new_call = fold_builtin_is_corresponding_member (loc, nargs, args);
1695 : : }
1696 : : else
1697 : 18452998 : new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
1698 : 9226499 : CALL_EXPR_FN (t), nargs, args);
1699 : 9226709 : current_function_decl = save_cur_fn;
1700 : 9226709 : force_folding_builtin_constant_p = save_ffbcp;
1701 : 9226709 : if (new_call == NULL)
1702 : : {
1703 : 6730627 : if (!*non_constant_p && !ctx->quiet)
1704 : : {
1705 : 0 : new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
1706 : 0 : CALL_EXPR_FN (t), nargs, args);
1707 : 0 : error ("%q+E is not a constant expression", new_call);
1708 : : }
1709 : 6730627 : *non_constant_p = true;
1710 : 6730627 : return t;
1711 : : }
1712 : :
1713 : 2496082 : if (!potential_constant_expression (new_call))
1714 : : {
1715 : 700 : if (!*non_constant_p && !ctx->quiet)
1716 : 3 : error ("%q+E is not a constant expression", new_call);
1717 : 700 : *non_constant_p = true;
1718 : 700 : return t;
1719 : : }
1720 : :
1721 : 2495382 : if (strret)
1722 : : {
1723 : : /* memchr returns a pointer into the first argument, but we replaced the
1724 : : argument above with a STRING_CST; put it back it now. */
1725 : 221 : tree op = CALL_EXPR_ARG (t, strret-1);
1726 : 221 : STRIP_NOPS (new_call);
1727 : 221 : if (TREE_CODE (new_call) == POINTER_PLUS_EXPR)
1728 : 109 : TREE_OPERAND (new_call, 0) = op;
1729 : 112 : else if (TREE_CODE (new_call) == ADDR_EXPR)
1730 : 2495382 : new_call = op;
1731 : : }
1732 : :
1733 : 2495382 : return cxx_eval_constant_expression (&new_ctx, new_call, lval,
1734 : 2495382 : non_constant_p, overflow_p);
1735 : : }
1736 : :
1737 : : /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1738 : : the type of the value to match. */
1739 : :
1740 : : static tree
1741 : 41071770 : adjust_temp_type (tree type, tree temp)
1742 : : {
1743 : 41071770 : if (same_type_p (TREE_TYPE (temp), type))
1744 : : return temp;
1745 : : /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1746 : 24946098 : if (TREE_CODE (temp) == CONSTRUCTOR)
1747 : : {
1748 : : /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
1749 : 775552 : tree t = copy_node (temp);
1750 : 775552 : TREE_TYPE (t) = type;
1751 : 775552 : return t;
1752 : : }
1753 : 24170546 : if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
1754 : 0 : return build0 (EMPTY_CLASS_EXPR, type);
1755 : 24170546 : gcc_assert (scalarish_type_p (type));
1756 : : /* Now we know we're dealing with a scalar, and a prvalue of non-class
1757 : : type is cv-unqualified. */
1758 : 24170546 : return cp_fold_convert (cv_unqualified (type), temp);
1759 : : }
1760 : :
1761 : : /* If T is a CONSTRUCTOR, return an unshared copy of T and any
1762 : : sub-CONSTRUCTORs. Otherwise return T.
1763 : :
1764 : : We use this whenever we initialize an object as a whole, whether it's a
1765 : : parameter, a local variable, or a subobject, so that subsequent
1766 : : modifications don't affect other places where it was used. */
1767 : :
1768 : : tree
1769 : 29452402 : unshare_constructor (tree t MEM_STAT_DECL)
1770 : : {
1771 : 29452402 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
1772 : : return t;
1773 : 6295727 : auto_vec <tree*, 4> ptrs;
1774 : 6295727 : ptrs.safe_push (&t);
1775 : 6295727 : while (!ptrs.is_empty ())
1776 : : {
1777 : 7017024 : tree *p = ptrs.pop ();
1778 : 7017024 : tree n = copy_node (*p PASS_MEM_STAT);
1779 : 9868759 : CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
1780 : 7017024 : *p = n;
1781 : 7017024 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
1782 : 7017024 : constructor_elt *ce;
1783 : 27714704 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
1784 : 7384929 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
1785 : 721297 : ptrs.safe_push (&ce->value);
1786 : : }
1787 : 6295727 : return t;
1788 : 6295727 : }
1789 : :
1790 : : /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1791 : :
1792 : : static void
1793 : 680974 : free_constructor (tree t)
1794 : : {
1795 : 680974 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
1796 : 0 : return;
1797 : 680974 : releasing_vec ctors;
1798 : 680974 : vec_safe_push (ctors, t);
1799 : 1370127 : while (!ctors->is_empty ())
1800 : : {
1801 : 689153 : tree c = ctors->pop ();
1802 : 689153 : if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
1803 : : {
1804 : : constructor_elt *ce;
1805 : 113165 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
1806 : 76227 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
1807 : 8179 : vec_safe_push (ctors, ce->value);
1808 : 36938 : ggc_free (elts);
1809 : : }
1810 : 689153 : ggc_free (c);
1811 : : }
1812 : 680974 : }
1813 : :
1814 : : /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
1815 : : if *TP is address of a static variable (or part of it) currently being
1816 : : constructed or of a heap artificial variable. */
1817 : :
1818 : : static tree
1819 : 7272659 : addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data)
1820 : : {
1821 : 7272659 : if (TREE_CODE (*tp) == ADDR_EXPR)
1822 : 730790 : if (tree var = get_base_address (TREE_OPERAND (*tp, 0)))
1823 : 730790 : if (VAR_P (var) && TREE_STATIC (var))
1824 : : {
1825 : 494520 : if (DECL_NAME (var) == heap_uninit_identifier
1826 : 494520 : || DECL_NAME (var) == heap_identifier
1827 : 418621 : || DECL_NAME (var) == heap_vec_uninit_identifier
1828 : 913141 : || DECL_NAME (var) == heap_vec_identifier)
1829 : : return var;
1830 : :
1831 : 418616 : constexpr_global_ctx *global = (constexpr_global_ctx *) data;
1832 : 418616 : if (global->get_value (var))
1833 : : return var;
1834 : : }
1835 : 7098470 : if (TYPE_P (*tp))
1836 : 851 : *walk_subtrees = false;
1837 : : return NULL_TREE;
1838 : : }
1839 : :
1840 : : /* Subroutine of cxx_eval_call_expression.
1841 : : We are processing a call expression (either CALL_EXPR or
1842 : : AGGR_INIT_EXPR) in the context of CTX. Evaluate
1843 : : all arguments and bind their values to correspondings
1844 : : parameters, making up the NEW_CALL context. */
1845 : :
1846 : : static tree
1847 : 58648385 : cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun,
1848 : : bool *non_constant_p, bool *overflow_p,
1849 : : bool *non_constant_args)
1850 : : {
1851 : 58648385 : const int nargs = call_expr_nargs (t);
1852 : 58648385 : tree parms = DECL_ARGUMENTS (fun);
1853 : 58648385 : int i;
1854 : : /* We don't record ellipsis args below. */
1855 : 58648385 : int nparms = list_length (parms);
1856 : 58648385 : int nbinds = nargs < nparms ? nargs : nparms;
1857 : 58648385 : tree binds = make_tree_vec (nbinds);
1858 : 103616928 : for (i = 0; i < nargs; ++i)
1859 : : {
1860 : 58571954 : tree x, arg;
1861 : 58571954 : tree type = parms ? TREE_TYPE (parms) : void_type_node;
1862 : 58571954 : if (parms && DECL_BY_REFERENCE (parms))
1863 : 5709 : type = TREE_TYPE (type);
1864 : 58571954 : x = get_nth_callarg (t, i);
1865 : : /* For member function, the first argument is a pointer to the implied
1866 : : object. For a constructor, it might still be a dummy object, in
1867 : : which case we get the real argument from ctx. */
1868 : 92708770 : if (i == 0 && DECL_CONSTRUCTOR_P (fun)
1869 : 65149149 : && is_dummy_object (x))
1870 : : {
1871 : 2250947 : x = ctx->object;
1872 : 2250947 : x = build_address (x);
1873 : : }
1874 : 58571954 : if (TREE_ADDRESSABLE (type))
1875 : : /* Undo convert_for_arg_passing work here. */
1876 : 7994 : x = convert_from_reference (x);
1877 : : /* Normally we would strip a TARGET_EXPR in an initialization context
1878 : : such as this, but here we do the elision differently: we keep the
1879 : : TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
1880 : 58571954 : arg = cxx_eval_constant_expression (ctx, x, vc_prvalue,
1881 : : non_constant_p, overflow_p);
1882 : : /* Check we aren't dereferencing a null pointer when calling a non-static
1883 : : member function, which is undefined behaviour. */
1884 : 46354385 : if (i == 0 && DECL_OBJECT_MEMBER_FUNCTION_P (fun)
1885 : 24136109 : && integer_zerop (arg)
1886 : : /* But ignore calls from within compiler-generated code, to handle
1887 : : cases like lambda function pointer conversion operator thunks
1888 : : which pass NULL as the 'this' pointer. */
1889 : 58590175 : && !(TREE_CODE (t) == CALL_EXPR && CALL_FROM_THUNK_P (t)))
1890 : : {
1891 : 95 : if (!ctx->quiet)
1892 : 6 : error_at (cp_expr_loc_or_input_loc (x),
1893 : : "dereferencing a null pointer");
1894 : 95 : *non_constant_p = true;
1895 : : }
1896 : : /* Don't VERIFY_CONSTANT here. */
1897 : 58571954 : if (*non_constant_p && ctx->quiet)
1898 : : break;
1899 : : /* Just discard ellipsis args after checking their constantitude. */
1900 : 44968543 : if (!parms)
1901 : 1804 : continue;
1902 : :
1903 : 44966739 : if (!*non_constant_p)
1904 : : {
1905 : : /* Make sure the binding has the same type as the parm. But
1906 : : only for constant args. */
1907 : 44966628 : if (!TYPE_REF_P (type))
1908 : 31644216 : arg = adjust_temp_type (type, arg);
1909 : 44966628 : if (!TREE_CONSTANT (arg))
1910 : 36816838 : *non_constant_args = true;
1911 : 8149790 : else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1912 : : /* The destructor needs to see any modifications the callee makes
1913 : : to the argument. */
1914 : 878 : *non_constant_args = true;
1915 : : /* If arg is or contains address of a heap artificial variable or
1916 : : of a static variable being constructed, avoid caching the
1917 : : function call, as those variables might be modified by the
1918 : : function, or might be modified by the callers in between
1919 : : the cached function and just read by the function. */
1920 : 8148912 : else if (!*non_constant_args
1921 : 8148912 : && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global,
1922 : : NULL))
1923 : 174189 : *non_constant_args = true;
1924 : :
1925 : : /* For virtual calls, adjust the this argument, so that it is
1926 : : the object on which the method is called, rather than
1927 : : one of its bases. */
1928 : 44966628 : if (i == 0 && DECL_VIRTUAL_P (fun))
1929 : : {
1930 : 5509 : tree addr = arg;
1931 : 5509 : STRIP_NOPS (addr);
1932 : 5509 : if (TREE_CODE (addr) == ADDR_EXPR)
1933 : : {
1934 : 5507 : tree obj = TREE_OPERAND (addr, 0);
1935 : 5507 : while (TREE_CODE (obj) == COMPONENT_REF
1936 : 2577 : && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))
1937 : 8074 : && !same_type_ignoring_top_level_qualifiers_p
1938 : 2558 : (TREE_TYPE (obj), DECL_CONTEXT (fun)))
1939 : 9 : obj = TREE_OPERAND (obj, 0);
1940 : 5507 : if (obj != TREE_OPERAND (addr, 0))
1941 : 9 : arg = build_fold_addr_expr_with_type (obj,
1942 : : TREE_TYPE (arg));
1943 : : }
1944 : : }
1945 : 44966628 : TREE_VEC_ELT (binds, i) = arg;
1946 : : }
1947 : 44966739 : parms = TREE_CHAIN (parms);
1948 : : }
1949 : :
1950 : 58648385 : return binds;
1951 : : }
1952 : :
1953 : : /* Variables and functions to manage constexpr call expansion context.
1954 : : These do not need to be marked for PCH or GC. */
1955 : :
1956 : : /* FIXME remember and print actual constant arguments. */
1957 : : static vec<tree> call_stack;
1958 : : static int call_stack_tick;
1959 : : static int last_cx_error_tick;
1960 : :
1961 : : static int
1962 : 42942087 : push_cx_call_context (tree call)
1963 : : {
1964 : 42942087 : ++call_stack_tick;
1965 : 42942087 : if (!EXPR_HAS_LOCATION (call))
1966 : 6295 : SET_EXPR_LOCATION (call, input_location);
1967 : 42942087 : call_stack.safe_push (call);
1968 : 42942087 : int len = call_stack.length ();
1969 : 42942087 : if (len > max_constexpr_depth)
1970 : 21 : return false;
1971 : : return len;
1972 : : }
1973 : :
1974 : : static void
1975 : 42942087 : pop_cx_call_context (void)
1976 : : {
1977 : 42942087 : ++call_stack_tick;
1978 : 42942087 : call_stack.pop ();
1979 : 42942087 : }
1980 : :
1981 : : vec<tree>
1982 : 210671 : cx_error_context (void)
1983 : : {
1984 : 210671 : vec<tree> r = vNULL;
1985 : 210671 : if (call_stack_tick != last_cx_error_tick
1986 : 210671 : && !call_stack.is_empty ())
1987 : : r = call_stack;
1988 : 210671 : last_cx_error_tick = call_stack_tick;
1989 : 210671 : return r;
1990 : : }
1991 : :
1992 : : /* E is an operand of a failed assertion, fold it either with or without
1993 : : constexpr context. */
1994 : :
1995 : : static tree
1996 : 520 : fold_operand (tree e, const constexpr_ctx *ctx)
1997 : : {
1998 : 520 : if (ctx)
1999 : : {
2000 : 81 : bool new_non_constant_p = false, new_overflow_p = false;
2001 : 81 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
2002 : : &new_non_constant_p,
2003 : : &new_overflow_p);
2004 : : }
2005 : : else
2006 : 439 : e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true);
2007 : 520 : return e;
2008 : : }
2009 : :
2010 : : /* If we have a condition in conjunctive normal form (CNF), find the first
2011 : : failing clause. In other words, given an expression like
2012 : :
2013 : : true && true && false && true && false
2014 : :
2015 : : return the first 'false'. EXPR is the expression. */
2016 : :
2017 : : static tree
2018 : 326 : find_failing_clause_r (const constexpr_ctx *ctx, tree expr)
2019 : : {
2020 : 413 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
2021 : : {
2022 : : /* First check the left side... */
2023 : 183 : tree e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 0));
2024 : 183 : if (e == NULL_TREE)
2025 : : /* ...if we didn't find a false clause, check the right side. */
2026 : 87 : e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 1));
2027 : 96 : return e;
2028 : : }
2029 : 230 : tree e = contextual_conv_bool (expr, tf_none);
2030 : 230 : e = fold_operand (e, ctx);
2031 : 230 : if (integer_zerop (e))
2032 : : /* This is the failing clause. */
2033 : 143 : return expr;
2034 : : return NULL_TREE;
2035 : : }
2036 : :
2037 : : /* Wrapper for find_failing_clause_r. */
2038 : :
2039 : : tree
2040 : 995 : find_failing_clause (const constexpr_ctx *ctx, tree expr)
2041 : : {
2042 : 995 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
2043 : 143 : if (tree e = find_failing_clause_r (ctx, expr))
2044 : 995 : expr = e;
2045 : 995 : return expr;
2046 : : }
2047 : :
2048 : : /* Emit additional diagnostics for failing condition BAD.
2049 : : Used by finish_static_assert and IFN_ASSUME constexpr diagnostics.
2050 : : If SHOW_EXPR_P is true, print the condition (because it was
2051 : : instantiation-dependent). */
2052 : :
2053 : : void
2054 : 995 : diagnose_failing_condition (tree bad, location_t cloc, bool show_expr_p,
2055 : : const constexpr_ctx *ctx /* = nullptr */)
2056 : : {
2057 : : /* Nobody wants to see the artificial (bool) cast. */
2058 : 995 : bad = tree_strip_nop_conversions (bad);
2059 : 995 : if (TREE_CODE (bad) == CLEANUP_POINT_EXPR)
2060 : 3 : bad = TREE_OPERAND (bad, 0);
2061 : :
2062 : : /* Actually explain the failure if this is a concept check or a
2063 : : requires-expression. */
2064 : 995 : if (concept_check_p (bad) || TREE_CODE (bad) == REQUIRES_EXPR)
2065 : 86 : diagnose_constraints (cloc, bad, NULL_TREE);
2066 : 909 : else if (COMPARISON_CLASS_P (bad)
2067 : 909 : && ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0))))
2068 : : {
2069 : 145 : tree op0 = fold_operand (TREE_OPERAND (bad, 0), ctx);
2070 : 145 : tree op1 = fold_operand (TREE_OPERAND (bad, 1), ctx);
2071 : 145 : tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1);
2072 : 145 : inform (cloc, "the comparison reduces to %qE", cond);
2073 : : }
2074 : 764 : else if (show_expr_p)
2075 : 621 : inform (cloc, "%qE evaluates to false", bad);
2076 : 995 : }
2077 : :
2078 : : /* Process an assert/assume of ORIG_ARG. If it's not supposed to be evaluated,
2079 : : do it without changing the current evaluation state. If it evaluates to
2080 : : false, complain and return false; otherwise, return true. */
2081 : :
2082 : : static bool
2083 : 149801 : cxx_eval_assert (const constexpr_ctx *ctx, tree arg, const char *msg,
2084 : : location_t loc, bool evaluated,
2085 : : bool *non_constant_p, bool *overflow_p)
2086 : : {
2087 : 149801 : if (*non_constant_p)
2088 : : return true;
2089 : :
2090 : 149801 : tree eval;
2091 : 149801 : if (!evaluated)
2092 : : {
2093 : 149730 : if (!potential_rvalue_constant_expression (arg))
2094 : 56 : return true;
2095 : :
2096 : 149674 : constexpr_ctx new_ctx = *ctx;
2097 : 149674 : new_ctx.quiet = true;
2098 : 149674 : bool new_non_constant_p = false, new_overflow_p = false;
2099 : : /* Avoid modification of existing values. */
2100 : 149674 : modifiable_tracker ms (new_ctx.global);
2101 : 149674 : eval = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
2102 : : &new_non_constant_p,
2103 : : &new_overflow_p);
2104 : 149674 : }
2105 : : else
2106 : 71 : eval = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2107 : : non_constant_p,
2108 : : overflow_p);
2109 : 149745 : if (!*non_constant_p && integer_zerop (eval))
2110 : : {
2111 : 110 : if (!ctx->quiet)
2112 : : {
2113 : : /* See if we can find which clause was failing
2114 : : (for logical AND). */
2115 : 36 : tree bad = find_failing_clause (ctx, arg);
2116 : : /* If not, or its location is unusable, fall back to the
2117 : : previous location. */
2118 : 36 : location_t cloc = cp_expr_loc_or_loc (bad, loc);
2119 : :
2120 : : /* Report the error. */
2121 : 36 : auto_diagnostic_group d;
2122 : 36 : error_at (cloc, msg);
2123 : 36 : diagnose_failing_condition (bad, cloc, true, ctx);
2124 : 36 : return bad;
2125 : 36 : }
2126 : 74 : *non_constant_p = true;
2127 : 74 : return false;
2128 : : }
2129 : :
2130 : : return true;
2131 : : }
2132 : :
2133 : : /* Evaluate a call T to a GCC internal function when possible and return
2134 : : the evaluated result or, under the control of CTX, give an error, set
2135 : : NON_CONSTANT_P, and return the unevaluated call T otherwise. */
2136 : :
2137 : : static tree
2138 : 188737 : cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
2139 : : value_cat lval,
2140 : : bool *non_constant_p, bool *overflow_p)
2141 : : {
2142 : 188737 : enum tree_code opcode = ERROR_MARK;
2143 : :
2144 : 188737 : switch (CALL_EXPR_IFN (t))
2145 : : {
2146 : 6 : case IFN_UBSAN_NULL:
2147 : 6 : case IFN_UBSAN_BOUNDS:
2148 : 6 : case IFN_UBSAN_VPTR:
2149 : 6 : case IFN_FALLTHROUGH:
2150 : 6 : return void_node;
2151 : :
2152 : 149715 : case IFN_ASSUME:
2153 : 149715 : if (!cxx_eval_assert (ctx, CALL_EXPR_ARG (t, 0),
2154 : : G_("failed %<assume%> attribute assumption"),
2155 : : EXPR_LOCATION (t), /*eval*/false,
2156 : : non_constant_p, overflow_p))
2157 : : return t;
2158 : 149679 : return void_node;
2159 : :
2160 : : case IFN_ADD_OVERFLOW:
2161 : : opcode = PLUS_EXPR;
2162 : : break;
2163 : 332 : case IFN_SUB_OVERFLOW:
2164 : 332 : opcode = MINUS_EXPR;
2165 : 332 : break;
2166 : 38062 : case IFN_MUL_OVERFLOW:
2167 : 38062 : opcode = MULT_EXPR;
2168 : 38062 : break;
2169 : :
2170 : 75 : case IFN_LAUNDER:
2171 : 75 : return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
2172 : : vc_prvalue, non_constant_p,
2173 : 75 : overflow_p);
2174 : :
2175 : 38 : case IFN_VEC_CONVERT:
2176 : 38 : {
2177 : 38 : tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
2178 : : vc_prvalue, non_constant_p,
2179 : : overflow_p);
2180 : 38 : if (TREE_CODE (arg) == VECTOR_CST)
2181 : 24 : if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg))
2182 : : return r;
2183 : : }
2184 : : /* FALLTHRU */
2185 : :
2186 : 21 : default:
2187 : 21 : if (!ctx->quiet)
2188 : 0 : error_at (cp_expr_loc_or_input_loc (t),
2189 : : "call to internal function %qE", t);
2190 : 21 : *non_constant_p = true;
2191 : 21 : return t;
2192 : : }
2193 : :
2194 : : /* Evaluate constant arguments using OPCODE and return a complex
2195 : : number containing the result and the overflow bit. */
2196 : 38903 : tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
2197 : : non_constant_p, overflow_p);
2198 : 38903 : tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
2199 : : non_constant_p, overflow_p);
2200 : :
2201 : 38903 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2202 : : {
2203 : 3 : location_t loc = cp_expr_loc_or_input_loc (t);
2204 : 3 : tree type = TREE_TYPE (TREE_TYPE (t));
2205 : 3 : tree result = fold_binary_loc (loc, opcode, type,
2206 : : fold_convert_loc (loc, type, arg0),
2207 : : fold_convert_loc (loc, type, arg1));
2208 : 3 : tree ovf
2209 : 3 : = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
2210 : : /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
2211 : 3 : if (TREE_OVERFLOW (result))
2212 : 0 : TREE_OVERFLOW (result) = 0;
2213 : :
2214 : 3 : return build_complex (TREE_TYPE (t), result, ovf);
2215 : : }
2216 : :
2217 : 38900 : *non_constant_p = true;
2218 : 38900 : return t;
2219 : : }
2220 : :
2221 : : /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
2222 : :
2223 : : static void
2224 : 3021830 : clear_no_implicit_zero (tree ctor)
2225 : : {
2226 : 3021830 : if (CONSTRUCTOR_NO_CLEARING (ctor))
2227 : : {
2228 : 561753 : CONSTRUCTOR_NO_CLEARING (ctor) = false;
2229 : 2613002 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
2230 : 957653 : if (TREE_CODE (e.value) == CONSTRUCTOR)
2231 : 185859 : clear_no_implicit_zero (e.value);
2232 : : }
2233 : 3021830 : }
2234 : :
2235 : : /* Complain about a const object OBJ being modified in a constant expression.
2236 : : EXPR is the MODIFY_EXPR expression performing the modification. */
2237 : :
2238 : : static void
2239 : 63 : modifying_const_object_error (tree expr, tree obj)
2240 : : {
2241 : 63 : location_t loc = cp_expr_loc_or_input_loc (expr);
2242 : 63 : auto_diagnostic_group d;
2243 : 126 : error_at (loc, "modifying a const object %qE is not allowed in "
2244 : 63 : "a constant expression", TREE_OPERAND (expr, 0));
2245 : :
2246 : : /* Find the underlying object that was declared as const. */
2247 : 63 : location_t decl_loc = UNKNOWN_LOCATION;
2248 : 138 : for (tree probe = obj; decl_loc == UNKNOWN_LOCATION; )
2249 : 75 : switch (TREE_CODE (probe))
2250 : : {
2251 : 39 : case BIT_FIELD_REF:
2252 : 39 : case COMPONENT_REF:
2253 : 39 : {
2254 : 39 : tree elt = TREE_OPERAND (probe, 1);
2255 : 39 : if (CP_TYPE_CONST_P (TREE_TYPE (elt)))
2256 : 27 : decl_loc = DECL_SOURCE_LOCATION (elt);
2257 : 39 : probe = TREE_OPERAND (probe, 0);
2258 : : }
2259 : 39 : break;
2260 : :
2261 : 0 : case ARRAY_REF:
2262 : 0 : case REALPART_EXPR:
2263 : 0 : case IMAGPART_EXPR:
2264 : 0 : probe = TREE_OPERAND (probe, 0);
2265 : 0 : break;
2266 : :
2267 : 36 : default:
2268 : 36 : decl_loc = location_of (probe);
2269 : 36 : break;
2270 : : }
2271 : 63 : inform (decl_loc, "originally declared %<const%> here");
2272 : 63 : }
2273 : :
2274 : : /* Return true if FNDECL is a replaceable global allocation function that
2275 : : should be useable during constant expression evaluation. */
2276 : :
2277 : : static inline bool
2278 : 24123979 : cxx_replaceable_global_alloc_fn (tree fndecl)
2279 : : {
2280 : 24123979 : return (cxx_dialect >= cxx20
2281 : 9963703 : && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl))
2282 : 180302 : && CP_DECL_CONTEXT (fndecl) == global_namespace
2283 : 24304143 : && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
2284 : 88956 : || DECL_IS_OPERATOR_DELETE_P (fndecl)));
2285 : : }
2286 : :
2287 : : /* Return true if FNDECL is a placement new function that should be
2288 : : useable during constant expression evaluation of std::construct_at. */
2289 : :
2290 : : static inline bool
2291 : 24063283 : cxx_placement_new_fn (tree fndecl)
2292 : : {
2293 : 24063283 : if (cxx_dialect >= cxx20
2294 : 9903007 : && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl))
2295 : 118556 : && CP_DECL_CONTEXT (fndecl) == global_namespace
2296 : 118418 : && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
2297 : 24129369 : && TREE_CODE (TREE_TYPE (fndecl)) == FUNCTION_TYPE)
2298 : : {
2299 : 66086 : tree first_arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
2300 : 66086 : if (TREE_VALUE (first_arg) == ptr_type_node
2301 : 66086 : && TREE_CHAIN (first_arg) == void_list_node)
2302 : 62805 : return true;
2303 : : }
2304 : : return false;
2305 : : }
2306 : :
2307 : : /* Return true if FNDECL is std::construct_at. */
2308 : :
2309 : : static inline bool
2310 : 83029 : is_std_construct_at (tree fndecl)
2311 : : {
2312 : 83029 : if (!decl_in_std_namespace_p (fndecl))
2313 : : return false;
2314 : :
2315 : 80157 : tree name = DECL_NAME (fndecl);
2316 : 80157 : return name && id_equal (name, "construct_at");
2317 : : }
2318 : :
2319 : : /* Overload for the above taking constexpr_call*. */
2320 : :
2321 : : static inline bool
2322 : 51367 : is_std_construct_at (const constexpr_call *call)
2323 : : {
2324 : 51367 : return (call
2325 : 50774 : && call->fundef
2326 : 102141 : && is_std_construct_at (call->fundef->decl));
2327 : : }
2328 : :
2329 : : /* True if CTX is an instance of std::allocator. */
2330 : :
2331 : : bool
2332 : 89231 : is_std_allocator (tree ctx)
2333 : : {
2334 : 89231 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
2335 : : return false;
2336 : :
2337 : 88751 : tree decl = TYPE_MAIN_DECL (ctx);
2338 : 88751 : tree name = DECL_NAME (decl);
2339 : 88751 : if (name == NULL_TREE || !id_equal (name, "allocator"))
2340 : : return false;
2341 : :
2342 : 56617 : return decl_in_std_namespace_p (decl);
2343 : : }
2344 : :
2345 : : /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
2346 : :
2347 : : static inline bool
2348 : 91382 : is_std_allocator_allocate (tree fndecl)
2349 : : {
2350 : 91382 : tree name = DECL_NAME (fndecl);
2351 : 91382 : if (name == NULL_TREE
2352 : 91382 : || !(id_equal (name, "allocate") || id_equal (name, "deallocate")))
2353 : : return false;
2354 : :
2355 : 85613 : return is_std_allocator (DECL_CONTEXT (fndecl));
2356 : : }
2357 : :
2358 : : /* Overload for the above taking constexpr_call*. */
2359 : :
2360 : : static inline bool
2361 : 18685 : is_std_allocator_allocate (const constexpr_call *call)
2362 : : {
2363 : 18685 : return (call
2364 : 2075 : && call->fundef
2365 : 20760 : && is_std_allocator_allocate (call->fundef->decl));
2366 : : }
2367 : :
2368 : : /* Return true if FNDECL is std::source_location::current. */
2369 : :
2370 : : static inline bool
2371 : 467 : is_std_source_location_current (tree fndecl)
2372 : : {
2373 : 467 : if (!decl_in_std_namespace_p (fndecl))
2374 : : return false;
2375 : :
2376 : 133 : tree name = DECL_NAME (fndecl);
2377 : 133 : if (name == NULL_TREE || !id_equal (name, "current"))
2378 : : return false;
2379 : :
2380 : 85 : tree ctx = DECL_CONTEXT (fndecl);
2381 : 85 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
2382 : : return false;
2383 : :
2384 : 85 : name = DECL_NAME (TYPE_MAIN_DECL (ctx));
2385 : 85 : return name && id_equal (name, "source_location");
2386 : : }
2387 : :
2388 : : /* Overload for the above taking constexpr_call*. */
2389 : :
2390 : : static inline bool
2391 : 1060 : is_std_source_location_current (const constexpr_call *call)
2392 : : {
2393 : 1060 : return (call
2394 : 467 : && call->fundef
2395 : 1527 : && is_std_source_location_current (call->fundef->decl));
2396 : : }
2397 : :
2398 : : /* Return true if FNDECL is __dynamic_cast. */
2399 : :
2400 : : static inline bool
2401 : 24003121 : cxx_dynamic_cast_fn_p (tree fndecl)
2402 : : {
2403 : 24003121 : return (cxx_dialect >= cxx20
2404 : 5091896 : && id_equal (DECL_NAME (fndecl), "__dynamic_cast")
2405 : 24004525 : && CP_DECL_CONTEXT (fndecl) == abi_node);
2406 : : }
2407 : :
2408 : : /* Often, we have an expression in the form of address + offset, e.g.
2409 : : "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
2410 : :
2411 : : static tree
2412 : 1163 : extract_obj_from_addr_offset (tree expr)
2413 : : {
2414 : 1163 : if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
2415 : 510 : expr = TREE_OPERAND (expr, 0);
2416 : 1163 : STRIP_NOPS (expr);
2417 : 1163 : if (TREE_CODE (expr) == ADDR_EXPR)
2418 : 1121 : expr = TREE_OPERAND (expr, 0);
2419 : 1163 : return expr;
2420 : : }
2421 : :
2422 : : /* Given a PATH like
2423 : :
2424 : : g.D.2181.D.2154.D.2102.D.2093
2425 : :
2426 : : find a component with type TYPE. Return NULL_TREE if not found, and
2427 : : error_mark_node if the component is not accessible. If STOP is non-null,
2428 : : this function will return NULL_TREE if STOP is found before TYPE. */
2429 : :
2430 : : static tree
2431 : 525 : get_component_with_type (tree path, tree type, tree stop)
2432 : : {
2433 : 1607 : while (true)
2434 : : {
2435 : 1066 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type))
2436 : : /* Found it. */
2437 : 231 : return path;
2438 : 835 : else if (stop
2439 : 835 : && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path),
2440 : : stop)))
2441 : : return NULL_TREE;
2442 : 820 : else if (TREE_CODE (path) == COMPONENT_REF
2443 : 820 : && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1)))
2444 : : {
2445 : : /* We need to check that the component we're accessing is in fact
2446 : : accessible. */
2447 : 820 : if (TREE_PRIVATE (TREE_OPERAND (path, 1))
2448 : 820 : || TREE_PROTECTED (TREE_OPERAND (path, 1)))
2449 : 279 : return error_mark_node;
2450 : 541 : path = TREE_OPERAND (path, 0);
2451 : : }
2452 : : else
2453 : : return NULL_TREE;
2454 : : }
2455 : : }
2456 : :
2457 : : /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
2458 : :
2459 : : The declaration of __dynamic_cast is:
2460 : :
2461 : : void* __dynamic_cast (const void* __src_ptr,
2462 : : const __class_type_info* __src_type,
2463 : : const __class_type_info* __dst_type,
2464 : : ptrdiff_t __src2dst);
2465 : :
2466 : : where src2dst has the following possible values
2467 : :
2468 : : >-1: src_type is a unique public non-virtual base of dst_type
2469 : : dst_ptr + src2dst == src_ptr
2470 : : -1: unspecified relationship
2471 : : -2: src_type is not a public base of dst_type
2472 : : -3: src_type is a multiple public non-virtual base of dst_type
2473 : :
2474 : : Since literal types can't have virtual bases, we only expect hint >=0,
2475 : : -2, or -3. */
2476 : :
2477 : : static tree
2478 : 660 : cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call,
2479 : : bool *non_constant_p, bool *overflow_p)
2480 : : {
2481 : : /* T will be something like
2482 : : __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
2483 : : dismantle it. */
2484 : 660 : gcc_assert (call_expr_nargs (call) == 4);
2485 : 660 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
2486 : 660 : tree obj = CALL_EXPR_ARG (call, 0);
2487 : 660 : tree type = CALL_EXPR_ARG (call, 2);
2488 : 660 : HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3));
2489 : 660 : location_t loc = cp_expr_loc_or_input_loc (call);
2490 : :
2491 : : /* Get the target type of the dynamic_cast. */
2492 : 660 : gcc_assert (TREE_CODE (type) == ADDR_EXPR);
2493 : 660 : type = TREE_OPERAND (type, 0);
2494 : 660 : type = TREE_TYPE (DECL_NAME (type));
2495 : :
2496 : : /* TYPE can only be either T* or T&. We can't know which of these it
2497 : : is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
2498 : : and something like "(T*)(T&)(T*) x" in the second case. */
2499 : 660 : bool reference_p = false;
2500 : 3460 : while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR)
2501 : : {
2502 : 2800 : reference_p |= TYPE_REF_P (TREE_TYPE (obj));
2503 : 2800 : obj = TREE_OPERAND (obj, 0);
2504 : : }
2505 : :
2506 : : /* Evaluate the object so that we know its dynamic type. */
2507 : 660 : obj = cxx_eval_constant_expression (ctx, obj, vc_prvalue, non_constant_p,
2508 : : overflow_p);
2509 : 660 : if (*non_constant_p)
2510 : : return call;
2511 : :
2512 : : /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
2513 : : but when HINT is > 0, it can also be something like
2514 : : &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
2515 : 653 : obj = extract_obj_from_addr_offset (obj);
2516 : 653 : const tree objtype = TREE_TYPE (obj);
2517 : : /* If OBJ doesn't refer to a base field, we're done. */
2518 : 1258 : if (tree t = (TREE_CODE (obj) == COMPONENT_REF
2519 : 653 : ? TREE_OPERAND (obj, 1) : obj))
2520 : 653 : if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t))
2521 : : {
2522 : 48 : if (reference_p)
2523 : : {
2524 : 46 : if (!ctx->quiet)
2525 : : {
2526 : 1 : auto_diagnostic_group d;
2527 : 1 : error_at (loc, "reference %<dynamic_cast%> failed");
2528 : 1 : inform (loc, "dynamic type %qT of its operand does "
2529 : : "not have a base class of type %qT",
2530 : : objtype, type);
2531 : 1 : }
2532 : 46 : *non_constant_p = true;
2533 : : }
2534 : 48 : return integer_zero_node;
2535 : : }
2536 : :
2537 : : /* [class.cdtor] When a dynamic_cast is used in a constructor ...
2538 : : or in a destructor ... if the operand of the dynamic_cast refers
2539 : : to the object under construction or destruction, this object is
2540 : : considered to be a most derived object that has the type of the
2541 : : constructor or destructor's class. */
2542 : 605 : tree vtable = build_vfield_ref (obj, objtype);
2543 : 605 : vtable = cxx_eval_constant_expression (ctx, vtable, vc_prvalue,
2544 : : non_constant_p, overflow_p);
2545 : 605 : if (*non_constant_p)
2546 : : return call;
2547 : : /* With -fsanitize=vptr, we initialize all vtable pointers to null,
2548 : : so it's possible that we got a null pointer now. */
2549 : 514 : if (integer_zerop (vtable))
2550 : : {
2551 : 4 : if (!ctx->quiet)
2552 : 1 : error_at (loc, "virtual table pointer is used uninitialized");
2553 : 4 : *non_constant_p = true;
2554 : 4 : return integer_zero_node;
2555 : : }
2556 : : /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
2557 : 510 : vtable = extract_obj_from_addr_offset (vtable);
2558 : 510 : const tree mdtype = DECL_CONTEXT (vtable);
2559 : :
2560 : : /* Given dynamic_cast<T>(v),
2561 : :
2562 : : [expr.dynamic.cast] If C is the class type to which T points or refers,
2563 : : the runtime check logically executes as follows:
2564 : :
2565 : : If, in the most derived object pointed (referred) to by v, v points
2566 : : (refers) to a public base class subobject of a C object, and if only
2567 : : one object of type C is derived from the subobject pointed (referred)
2568 : : to by v the result points (refers) to that C object.
2569 : :
2570 : : In this case, HINT >= 0 or -3. */
2571 : 510 : if (hint >= 0 || hint == -3)
2572 : : {
2573 : : /* Look for a component with type TYPE. */
2574 : 205 : tree t = get_component_with_type (obj, type, mdtype);
2575 : : /* If not accessible, give an error. */
2576 : 205 : if (t == error_mark_node)
2577 : : {
2578 : 66 : if (reference_p)
2579 : : {
2580 : 54 : if (!ctx->quiet)
2581 : : {
2582 : 6 : auto_diagnostic_group d;
2583 : 6 : error_at (loc, "reference %<dynamic_cast%> failed");
2584 : 6 : inform (loc, "static type %qT of its operand is a "
2585 : : "non-public base class of dynamic type %qT",
2586 : : objtype, type);
2587 : :
2588 : 6 : }
2589 : 54 : *non_constant_p = true;
2590 : : }
2591 : 66 : return integer_zero_node;
2592 : : }
2593 : 139 : else if (t)
2594 : : /* The result points to the TYPE object. */
2595 : 124 : return cp_build_addr_expr (t, complain);
2596 : : /* Else, TYPE was not found, because the HINT turned out to be wrong.
2597 : : Fall through to the normal processing. */
2598 : : }
2599 : :
2600 : : /* Otherwise, if v points (refers) to a public base class subobject of the
2601 : : most derived object, and the type of the most derived object has a base
2602 : : class, of type C, that is unambiguous and public, the result points
2603 : : (refers) to the C subobject of the most derived object.
2604 : :
2605 : : But it can also be an invalid case. */
2606 : :
2607 : : /* Get the most derived object. */
2608 : 320 : obj = get_component_with_type (obj, mdtype, NULL_TREE);
2609 : 320 : if (obj == error_mark_node)
2610 : : {
2611 : 213 : if (reference_p)
2612 : : {
2613 : 175 : if (!ctx->quiet)
2614 : : {
2615 : 19 : auto_diagnostic_group d;
2616 : 19 : error_at (loc, "reference %<dynamic_cast%> failed");
2617 : 19 : inform (loc, "static type %qT of its operand is a non-public"
2618 : : " base class of dynamic type %qT", objtype, mdtype);
2619 : 19 : }
2620 : 175 : *non_constant_p = true;
2621 : : }
2622 : 213 : return integer_zero_node;
2623 : : }
2624 : : else
2625 : 107 : gcc_assert (obj);
2626 : :
2627 : : /* Check that the type of the most derived object has a base class
2628 : : of type TYPE that is unambiguous and public. */
2629 : 107 : base_kind b_kind;
2630 : 107 : tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none);
2631 : 107 : if (!binfo || binfo == error_mark_node)
2632 : : {
2633 : 79 : if (reference_p)
2634 : : {
2635 : 67 : if (!ctx->quiet)
2636 : : {
2637 : 8 : auto_diagnostic_group d;
2638 : 8 : error_at (loc, "reference %<dynamic_cast%> failed");
2639 : 8 : if (b_kind == bk_ambig)
2640 : 3 : inform (loc, "%qT is an ambiguous base class of dynamic "
2641 : : "type %qT of its operand", type, mdtype);
2642 : : else
2643 : 5 : inform (loc, "dynamic type %qT of its operand does not "
2644 : : "have an unambiguous public base class %qT",
2645 : : mdtype, type);
2646 : 8 : }
2647 : 67 : *non_constant_p = true;
2648 : : }
2649 : 79 : return integer_zero_node;
2650 : : }
2651 : : /* If so, return the TYPE subobject of the most derived object. */
2652 : 28 : obj = convert_to_base_statically (obj, binfo);
2653 : 28 : return cp_build_addr_expr (obj, complain);
2654 : : }
2655 : :
2656 : : /* Data structure used by replace_decl and replace_decl_r. */
2657 : :
2658 : : struct replace_decl_data
2659 : : {
2660 : : /* The _DECL we want to replace. */
2661 : : tree decl;
2662 : : /* The replacement for DECL. */
2663 : : tree replacement;
2664 : : /* Trees we've visited. */
2665 : : hash_set<tree> *pset;
2666 : : /* Whether we've performed any replacements. */
2667 : : bool changed;
2668 : : };
2669 : :
2670 : : /* Helper function for replace_decl, called through cp_walk_tree. */
2671 : :
2672 : : static tree
2673 : 2063940 : replace_decl_r (tree *tp, int *walk_subtrees, void *data)
2674 : : {
2675 : 2063940 : replace_decl_data *d = (replace_decl_data *) data;
2676 : :
2677 : 2063940 : if (*tp == d->decl)
2678 : : {
2679 : 297 : *tp = unshare_expr (d->replacement);
2680 : 297 : d->changed = true;
2681 : 297 : *walk_subtrees = 0;
2682 : : }
2683 : 2063643 : else if (TYPE_P (*tp)
2684 : 2063643 : || d->pset->add (*tp))
2685 : 491468 : *walk_subtrees = 0;
2686 : :
2687 : 2063940 : return NULL_TREE;
2688 : : }
2689 : :
2690 : : /* Replace every occurrence of DECL with (an unshared copy of)
2691 : : REPLACEMENT within the expression *TP. Returns true iff a
2692 : : replacement was performed. */
2693 : :
2694 : : bool
2695 : 295304 : replace_decl (tree *tp, tree decl, tree replacement)
2696 : : {
2697 : 295304 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
2698 : : (TREE_TYPE (decl), TREE_TYPE (replacement)));
2699 : 295304 : hash_set<tree> pset;
2700 : 295304 : replace_decl_data data = { decl, replacement, &pset, false };
2701 : 295304 : cp_walk_tree (tp, replace_decl_r, &data, NULL);
2702 : 295304 : return data.changed;
2703 : 295304 : }
2704 : :
2705 : : /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
2706 : :
2707 : : static tree
2708 : 9 : cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl,
2709 : : value_cat lval,
2710 : : bool *non_constant_p, bool *overflow_p)
2711 : : {
2712 : 9 : tree function = THUNK_TARGET (thunk_fndecl);
2713 : :
2714 : 9 : if (THUNK_VIRTUAL_OFFSET (thunk_fndecl))
2715 : : {
2716 : 5 : if (!ctx->quiet)
2717 : : {
2718 : 1 : if (!DECL_DECLARED_CONSTEXPR_P (function))
2719 : : {
2720 : 0 : error ("call to non-%<constexpr%> function %qD", function);
2721 : 0 : explain_invalid_constexpr_fn (function);
2722 : : }
2723 : : else
2724 : : /* virtual_offset is only set for virtual bases, which make the
2725 : : class non-literal, so we don't need to handle it here. */
2726 : 1 : error ("calling constexpr member function %qD through virtual "
2727 : : "base subobject", function);
2728 : : }
2729 : 5 : *non_constant_p = true;
2730 : 5 : return t;
2731 : : }
2732 : :
2733 : 4 : tree new_call = copy_node (t);
2734 : 4 : CALL_EXPR_FN (new_call) = function;
2735 : 4 : TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function));
2736 : :
2737 : 4 : tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl));
2738 : :
2739 : 4 : if (DECL_THIS_THUNK_P (thunk_fndecl))
2740 : : {
2741 : : /* 'this'-adjusting thunk. */
2742 : 2 : tree this_arg = CALL_EXPR_ARG (t, 0);
2743 : 2 : this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg),
2744 : : this_arg, offset);
2745 : 2 : CALL_EXPR_ARG (new_call, 0) = this_arg;
2746 : : }
2747 : : else
2748 : : /* Return-adjusting thunk. */
2749 : 2 : new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call),
2750 : : new_call, offset);
2751 : :
2752 : 4 : return cxx_eval_constant_expression (ctx, new_call, lval,
2753 : 4 : non_constant_p, overflow_p);
2754 : : }
2755 : :
2756 : : /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
2757 : : its TREE_READONLY flag according to READONLY_P. Used for constexpr
2758 : : 'tors to detect modifying const objects in a constexpr context. */
2759 : :
2760 : : static void
2761 : 4901564 : cxx_set_object_constness (const constexpr_ctx *ctx, tree object,
2762 : : bool readonly_p, bool *non_constant_p,
2763 : : bool *overflow_p)
2764 : : {
2765 : 9803128 : if (CLASS_TYPE_P (TREE_TYPE (object))
2766 : 9803128 : && CP_TYPE_CONST_P (TREE_TYPE (object)))
2767 : : {
2768 : : /* Subobjects might not be stored in ctx->global->values but we
2769 : : can get its CONSTRUCTOR by evaluating *this. */
2770 : 640762 : tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue,
2771 : : non_constant_p, overflow_p);
2772 : 640762 : if (TREE_CODE (e) == CONSTRUCTOR && !*non_constant_p)
2773 : 638928 : TREE_READONLY (e) = readonly_p;
2774 : : }
2775 : 4901564 : }
2776 : :
2777 : : /* Subroutine of cxx_eval_constant_expression.
2778 : : Evaluate the call expression tree T in the context of OLD_CALL expression
2779 : : evaluation. */
2780 : :
2781 : : static tree
2782 : 69344800 : cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
2783 : : value_cat lval,
2784 : : bool *non_constant_p, bool *overflow_p)
2785 : : {
2786 : : /* Handle concept checks separately. */
2787 : 69344800 : if (concept_check_p (t))
2788 : 41 : return evaluate_concept_check (t);
2789 : :
2790 : 69344759 : location_t loc = cp_expr_loc_or_input_loc (t);
2791 : 69344759 : tree fun = get_function_named_in_call (t);
2792 : 69344759 : constexpr_call new_call
2793 : 69344759 : = { NULL, NULL, NULL, 0, ctx->manifestly_const_eval };
2794 : 69344759 : int depth_ok;
2795 : :
2796 : 69344759 : if (fun == NULL_TREE)
2797 : 188737 : return cxx_eval_internal_function (ctx, t, lval,
2798 : 188737 : non_constant_p, overflow_p);
2799 : :
2800 : 69156022 : if (TREE_CODE (fun) != FUNCTION_DECL)
2801 : : {
2802 : : /* Might be a constexpr function pointer. */
2803 : 64590 : fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue,
2804 : : non_constant_p, overflow_p);
2805 : 64590 : STRIP_NOPS (fun);
2806 : 64590 : if (TREE_CODE (fun) == ADDR_EXPR)
2807 : 7410 : fun = TREE_OPERAND (fun, 0);
2808 : : /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
2809 : : indirection, the called expression is a pointer into the
2810 : : virtual table which should contain FDESC_EXPR. Extract the
2811 : : FUNCTION_DECL from there. */
2812 : : else if (TARGET_VTABLE_USES_DESCRIPTORS
2813 : : && TREE_CODE (fun) == POINTER_PLUS_EXPR
2814 : : && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR
2815 : : && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST)
2816 : : {
2817 : : tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0);
2818 : : if (VAR_P (d)
2819 : : && DECL_VTABLE_OR_VTT_P (d)
2820 : : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE
2821 : : && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type
2822 : : && DECL_INITIAL (d)
2823 : : && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR)
2824 : : {
2825 : : tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1),
2826 : : TYPE_SIZE_UNIT (vtable_entry_type));
2827 : : HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i);
2828 : : if (idx >= 0)
2829 : : {
2830 : : tree fdesc
2831 : : = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value;
2832 : : if (TREE_CODE (fdesc) == FDESC_EXPR
2833 : : && integer_zerop (TREE_OPERAND (fdesc, 1)))
2834 : : fun = TREE_OPERAND (fdesc, 0);
2835 : : }
2836 : : }
2837 : : }
2838 : : }
2839 : 69156022 : if (TREE_CODE (fun) != FUNCTION_DECL)
2840 : : {
2841 : 57180 : if (!ctx->quiet && !*non_constant_p)
2842 : 0 : error_at (loc, "expression %qE does not designate a %<constexpr%> "
2843 : : "function", fun);
2844 : 57180 : *non_constant_p = true;
2845 : 57180 : return t;
2846 : : }
2847 : 69098842 : if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))
2848 : 6763617 : fun = DECL_CLONED_FUNCTION (fun);
2849 : :
2850 : 69098842 : if (is_ubsan_builtin_p (fun))
2851 : 71 : return void_node;
2852 : :
2853 : 69098771 : if (fndecl_built_in_p (fun))
2854 : 10394873 : return cxx_eval_builtin_function_call (ctx, t, fun,
2855 : 10394873 : lval, non_constant_p, overflow_p);
2856 : 58703898 : if (DECL_THUNK_P (fun))
2857 : 9 : return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p);
2858 : 58703889 : if (!maybe_constexpr_fn (fun))
2859 : : {
2860 : 55504 : if (TREE_CODE (t) == CALL_EXPR
2861 : 55458 : && cxx_replaceable_global_alloc_fn (fun)
2862 : 73297 : && (CALL_FROM_NEW_OR_DELETE_P (t)
2863 : 17088 : || is_std_allocator_allocate (ctx->call)))
2864 : : {
2865 : 1776 : const bool new_op_p = IDENTIFIER_NEW_OP_P (DECL_NAME (fun));
2866 : 1776 : const int nargs = call_expr_nargs (t);
2867 : 1776 : tree arg0 = NULL_TREE;
2868 : 3641 : for (int i = 0; i < nargs; ++i)
2869 : : {
2870 : 1889 : tree arg = CALL_EXPR_ARG (t, i);
2871 : 1889 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2872 : : non_constant_p, overflow_p);
2873 : : /* Deleting a non-constant pointer has a better error message
2874 : : below. */
2875 : 1889 : if (new_op_p || i != 0)
2876 : 1194 : VERIFY_CONSTANT (arg);
2877 : 1170 : if (i == 0)
2878 : : arg0 = arg;
2879 : : }
2880 : 1752 : gcc_assert (arg0);
2881 : 1752 : if (new_op_p)
2882 : : {
2883 : 2114 : tree type = build_array_type_nelts (char_type_node,
2884 : 1057 : tree_to_uhwi (arg0));
2885 : 1057 : tree var = build_decl (loc, VAR_DECL,
2886 : 1057 : (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
2887 : : & OVL_OP_FLAG_VEC)
2888 : : ? heap_vec_uninit_identifier
2889 : : : heap_uninit_identifier,
2890 : 1057 : type);
2891 : 1057 : DECL_ARTIFICIAL (var) = 1;
2892 : 1057 : TREE_STATIC (var) = 1;
2893 : : // Temporarily register the artificial var in varpool,
2894 : : // so that comparisons of its address against NULL are folded
2895 : : // through nonzero_address even with
2896 : : // -fno-delete-null-pointer-checks or that comparison of
2897 : : // addresses of different heap artificial vars is folded too.
2898 : : // See PR98988 and PR99031.
2899 : 1057 : varpool_node::finalize_decl (var);
2900 : 1057 : ctx->global->heap_vars.safe_push (var);
2901 : 1057 : ctx->global->put_value (var, NULL_TREE);
2902 : 1057 : return fold_convert (ptr_type_node, build_address (var));
2903 : : }
2904 : : else
2905 : : {
2906 : 695 : STRIP_NOPS (arg0);
2907 : 695 : if (TREE_CODE (arg0) == ADDR_EXPR
2908 : 695 : && VAR_P (TREE_OPERAND (arg0, 0)))
2909 : : {
2910 : 695 : tree var = TREE_OPERAND (arg0, 0);
2911 : 695 : if (DECL_NAME (var) == heap_uninit_identifier
2912 : 695 : || DECL_NAME (var) == heap_identifier)
2913 : : {
2914 : 631 : if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
2915 : : & OVL_OP_FLAG_VEC)
2916 : : {
2917 : 3 : if (!ctx->quiet)
2918 : : {
2919 : 1 : auto_diagnostic_group d;
2920 : 1 : error_at (loc, "array deallocation of object "
2921 : : "allocated with non-array "
2922 : : "allocation");
2923 : 1 : inform (DECL_SOURCE_LOCATION (var),
2924 : : "allocation performed here");
2925 : 1 : }
2926 : 3 : *non_constant_p = true;
2927 : 3 : return t;
2928 : : }
2929 : 628 : DECL_NAME (var) = heap_deleted_identifier;
2930 : 628 : ctx->global->destroy_value (var);
2931 : 628 : ctx->global->heap_dealloc_count++;
2932 : 628 : return void_node;
2933 : : }
2934 : 64 : else if (DECL_NAME (var) == heap_vec_uninit_identifier
2935 : 64 : || DECL_NAME (var) == heap_vec_identifier)
2936 : : {
2937 : 56 : if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
2938 : : & OVL_OP_FLAG_VEC) == 0)
2939 : : {
2940 : 3 : if (!ctx->quiet)
2941 : : {
2942 : 1 : auto_diagnostic_group d;
2943 : 1 : error_at (loc, "non-array deallocation of "
2944 : : "object allocated with array "
2945 : : "allocation");
2946 : 1 : inform (DECL_SOURCE_LOCATION (var),
2947 : : "allocation performed here");
2948 : 1 : }
2949 : 3 : *non_constant_p = true;
2950 : 3 : return t;
2951 : : }
2952 : 53 : DECL_NAME (var) = heap_deleted_identifier;
2953 : 53 : ctx->global->destroy_value (var);
2954 : 53 : ctx->global->heap_dealloc_count++;
2955 : 53 : return void_node;
2956 : : }
2957 : 8 : else if (DECL_NAME (var) == heap_deleted_identifier)
2958 : : {
2959 : 5 : if (!ctx->quiet)
2960 : 2 : error_at (loc, "deallocation of already deallocated "
2961 : : "storage");
2962 : 5 : *non_constant_p = true;
2963 : 5 : return t;
2964 : : }
2965 : : }
2966 : 3 : if (!ctx->quiet)
2967 : 1 : error_at (loc, "deallocation of storage that was "
2968 : : "not previously allocated");
2969 : 3 : *non_constant_p = true;
2970 : 3 : return t;
2971 : : }
2972 : : }
2973 : : /* Allow placement new in std::construct_at, just return the second
2974 : : argument. */
2975 : 53728 : if (TREE_CODE (t) == CALL_EXPR
2976 : 53682 : && cxx_placement_new_fn (fun)
2977 : 84272 : && is_std_construct_at (ctx->call))
2978 : : {
2979 : 30491 : const int nargs = call_expr_nargs (t);
2980 : 30491 : tree arg1 = NULL_TREE;
2981 : 91473 : for (int i = 0; i < nargs; ++i)
2982 : : {
2983 : 60982 : tree arg = CALL_EXPR_ARG (t, i);
2984 : 60982 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2985 : : non_constant_p, overflow_p);
2986 : 60982 : if (i == 1)
2987 : : arg1 = arg;
2988 : : else
2989 : 60982 : VERIFY_CONSTANT (arg);
2990 : : }
2991 : 30491 : gcc_assert (arg1);
2992 : : return arg1;
2993 : : }
2994 : 23237 : else if (cxx_dynamic_cast_fn_p (fun))
2995 : 660 : return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p);
2996 : :
2997 : 22577 : if (!ctx->quiet)
2998 : : {
2999 : 34 : if (!lambda_static_thunk_p (fun))
3000 : 34 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
3001 : 34 : explain_invalid_constexpr_fn (fun);
3002 : : }
3003 : 22577 : *non_constant_p = true;
3004 : 22577 : return t;
3005 : : }
3006 : :
3007 : 58648385 : constexpr_ctx new_ctx = *ctx;
3008 : 65225580 : if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
3009 : 60409762 : && TREE_CODE (t) == AGGR_INIT_EXPR)
3010 : : {
3011 : : /* We want to have an initialization target for an AGGR_INIT_EXPR.
3012 : : If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
3013 : 112 : new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
3014 : 112 : tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
3015 : 112 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
3016 : 112 : ctx->global->put_value (new_ctx.object, ctor);
3017 : 112 : ctx = &new_ctx;
3018 : : }
3019 : :
3020 : : /* We used to shortcut trivial constructor/op= here, but nowadays
3021 : : we can only get a trivial function here with -fno-elide-constructors. */
3022 : 58648385 : gcc_checking_assert (!trivial_fn_p (fun)
3023 : : || !flag_elide_constructors
3024 : : /* We don't elide constructors when processing
3025 : : a noexcept-expression. */
3026 : : || cp_noexcept_operand);
3027 : :
3028 : 58648385 : bool non_constant_args = false;
3029 : 58648385 : new_call.bindings
3030 : 58648385 : = cxx_bind_parameters_in_call (ctx, t, fun, non_constant_p,
3031 : : overflow_p, &non_constant_args);
3032 : :
3033 : : /* We build up the bindings list before we know whether we already have this
3034 : : call cached. If we don't end up saving these bindings, ggc_free them when
3035 : : this function exits. */
3036 : 58648385 : class free_bindings
3037 : : {
3038 : : tree *bindings;
3039 : : public:
3040 : 58648385 : free_bindings (tree &b): bindings (&b) { }
3041 : 58648385 : ~free_bindings () { if (bindings) ggc_free (*bindings); }
3042 : 3111629 : void preserve () { bindings = NULL; }
3043 : 58648385 : } fb (new_call.bindings);
3044 : :
3045 : 58648385 : if (*non_constant_p)
3046 : : return t;
3047 : :
3048 : : /* We can't defer instantiating the function any longer. */
3049 : 45044864 : if (!DECL_INITIAL (fun)
3050 : 3996613 : && (DECL_TEMPLOID_INSTANTIATION (fun) || DECL_DEFAULTED_FN (fun))
3051 : 45044864 : && !uid_sensitive_constexpr_evaluation_p ())
3052 : : {
3053 : 2382906 : location_t save_loc = input_location;
3054 : 2382906 : input_location = loc;
3055 : 2382906 : ++function_depth;
3056 : 2382906 : if (ctx->manifestly_const_eval == mce_true)
3057 : 48460 : FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true;
3058 : 2382906 : if (DECL_TEMPLOID_INSTANTIATION (fun))
3059 : 2382901 : instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
3060 : : else
3061 : 5 : synthesize_method (fun);
3062 : 2382906 : --function_depth;
3063 : 2382906 : input_location = save_loc;
3064 : : }
3065 : :
3066 : : /* If in direct recursive call, optimize definition search. */
3067 : 45044864 : if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
3068 : 3921 : new_call.fundef = ctx->call->fundef;
3069 : : else
3070 : : {
3071 : 45040943 : new_call.fundef = retrieve_constexpr_fundef (fun);
3072 : 45040943 : if (new_call.fundef == NULL || new_call.fundef->body == NULL
3073 : 43032094 : || new_call.fundef->result == error_mark_node
3074 : 42938169 : || fun == current_function_decl)
3075 : : {
3076 : 2102777 : if (!ctx->quiet)
3077 : : {
3078 : : /* We need to check for current_function_decl here in case we're
3079 : : being called during cp_fold_function, because at that point
3080 : : DECL_INITIAL is set properly and we have a fundef but we
3081 : : haven't lowered invisirefs yet (c++/70344). */
3082 : 98 : if (DECL_INITIAL (fun) == error_mark_node
3083 : 98 : || fun == current_function_decl)
3084 : 9 : error_at (loc, "%qD called in a constant expression before its "
3085 : : "definition is complete", fun);
3086 : 89 : else if (DECL_INITIAL (fun))
3087 : : {
3088 : : /* The definition of fun was somehow unsuitable. But pretend
3089 : : that lambda static thunks don't exist. */
3090 : 60 : if (!lambda_static_thunk_p (fun))
3091 : 60 : error_at (loc, "%qD called in a constant expression", fun);
3092 : 60 : explain_invalid_constexpr_fn (fun);
3093 : : }
3094 : : else
3095 : 29 : error_at (loc, "%qD used before its definition", fun);
3096 : : }
3097 : 2102777 : *non_constant_p = true;
3098 : 2102777 : return t;
3099 : : }
3100 : : }
3101 : :
3102 : 42942087 : depth_ok = push_cx_call_context (t);
3103 : :
3104 : : /* Remember the object we are constructing or destructing. */
3105 : 42942087 : tree new_obj = NULL_TREE;
3106 : 85884174 : if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun))
3107 : : {
3108 : : /* In a cdtor, it should be the first `this' argument.
3109 : : At this point it has already been evaluated in the call
3110 : : to cxx_bind_parameters_in_call. */
3111 : 5455579 : new_obj = TREE_VEC_ELT (new_call.bindings, 0);
3112 : 5455579 : new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj);
3113 : :
3114 : 2922620 : if (ctx->call && ctx->call->fundef
3115 : 11300819 : && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl))
3116 : : {
3117 : 2385095 : tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0);
3118 : 2385095 : STRIP_NOPS (cur_obj);
3119 : 2385095 : if (TREE_CODE (cur_obj) == ADDR_EXPR)
3120 : 2285071 : cur_obj = TREE_OPERAND (cur_obj, 0);
3121 : 2385095 : if (new_obj == cur_obj)
3122 : : /* We're calling the target constructor of a delegating
3123 : : constructor, or accessing a base subobject through a
3124 : : NOP_EXPR as part of a call to a base constructor, so
3125 : : there is no new (sub)object. */
3126 : 37694703 : new_obj = NULL_TREE;
3127 : : }
3128 : : }
3129 : :
3130 : 42942087 : tree result = NULL_TREE;
3131 : :
3132 : 42942087 : constexpr_call *entry = NULL;
3133 : 42942087 : if (depth_ok && !non_constant_args && ctx->strict)
3134 : : {
3135 : 15744963 : new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
3136 : 15744963 : new_call.hash
3137 : 15744963 : = iterative_hash_template_arg (new_call.bindings, new_call.hash);
3138 : 15744963 : new_call.hash
3139 : 15744963 : = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash);
3140 : :
3141 : : /* If we have seen this call before, we are done. */
3142 : 15744963 : maybe_initialize_constexpr_call_table ();
3143 : 15744963 : bool insert = depth_ok < constexpr_cache_depth;
3144 : 15744963 : constexpr_call **slot
3145 : 15802470 : = constexpr_call_table->find_slot (&new_call,
3146 : : insert ? INSERT : NO_INSERT);
3147 : 15744963 : entry = slot ? *slot : NULL;
3148 : 15712034 : if (entry == NULL)
3149 : : {
3150 : : /* Only cache up to constexpr_cache_depth to limit memory use. */
3151 : 3144558 : if (insert)
3152 : : {
3153 : : /* We need to keep a pointer to the entry, not just the slot, as
3154 : : the slot can move during evaluation of the body. */
3155 : 3111629 : *slot = entry = ggc_alloc<constexpr_call> ();
3156 : 3111629 : *entry = new_call;
3157 : 3111629 : fb.preserve ();
3158 : : }
3159 : : }
3160 : : /* Calls that are in progress have their result set to NULL, so that we
3161 : : can detect circular dependencies. Now that we only cache up to
3162 : : constexpr_cache_depth this won't catch circular dependencies that
3163 : : start deeper, but they'll hit the recursion or ops limit. */
3164 : 12600405 : else if (entry->result == NULL)
3165 : : {
3166 : 15 : if (!ctx->quiet)
3167 : 0 : error ("call has circular dependency");
3168 : 15 : *non_constant_p = true;
3169 : 15 : entry->result = result = error_mark_node;
3170 : : }
3171 : : else
3172 : 12600390 : result = entry->result;
3173 : : }
3174 : :
3175 : 42942087 : if (!depth_ok)
3176 : : {
3177 : 21 : if (!ctx->quiet)
3178 : 3 : error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
3179 : : "%<-fconstexpr-depth=%> to increase the maximum)",
3180 : : max_constexpr_depth);
3181 : 21 : *non_constant_p = true;
3182 : 21 : result = error_mark_node;
3183 : : }
3184 : : else
3185 : : {
3186 : 42942066 : bool cacheable = !!entry;
3187 : 42942066 : if (result && result != error_mark_node)
3188 : : /* OK */;
3189 : 31593056 : else if (!DECL_SAVED_TREE (fun))
3190 : : {
3191 : : /* When at_eof >= 3, cgraph has started throwing away
3192 : : DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
3193 : : late code generation for VEC_INIT_EXPR, which needs to be
3194 : : completely reconsidered. */
3195 : 0 : gcc_assert (at_eof >= 3 && ctx->quiet);
3196 : 0 : *non_constant_p = true;
3197 : : }
3198 : 31593056 : else if (tree copy = get_fundef_copy (new_call.fundef))
3199 : : {
3200 : 31593056 : tree body, parms, res;
3201 : 31593056 : releasing_vec ctors;
3202 : :
3203 : : /* Reuse or create a new unshared copy of this function's body. */
3204 : 31593056 : body = TREE_PURPOSE (copy);
3205 : 31593056 : parms = TREE_VALUE (copy);
3206 : 31593056 : res = TREE_TYPE (copy);
3207 : :
3208 : : /* Associate the bindings with the remapped parms. */
3209 : 31593056 : tree bound = new_call.bindings;
3210 : 31593056 : tree remapped = parms;
3211 : 68152027 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
3212 : : {
3213 : 36558971 : tree arg = TREE_VEC_ELT (bound, i);
3214 : 36558971 : if (entry)
3215 : : {
3216 : : /* Unshare args going into the hash table to separate them
3217 : : from the caller's context, for better GC and to avoid
3218 : : problems with verify_gimple. */
3219 : 1646185 : arg = unshare_expr_without_location (arg);
3220 : 1646185 : TREE_VEC_ELT (bound, i) = arg;
3221 : :
3222 : : /* And then unshare again so the callee doesn't change the
3223 : : argument values in the hash table. XXX Could we unshare
3224 : : lazily in cxx_eval_store_expression? */
3225 : 1646185 : arg = unshare_constructor (arg);
3226 : 1646185 : if (TREE_CODE (arg) == CONSTRUCTOR)
3227 : 680240 : vec_safe_push (ctors, arg);
3228 : : }
3229 : 36558971 : ctx->global->put_value (remapped, arg);
3230 : 36558971 : remapped = DECL_CHAIN (remapped);
3231 : : }
3232 : 31727847 : for (; remapped; remapped = TREE_CHAIN (remapped))
3233 : 134791 : if (DECL_NAME (remapped) == in_charge_identifier)
3234 : : {
3235 : : /* FIXME destructors unnecessarily have in-charge parameters
3236 : : even in classes without vbases, map it to 0 for now. */
3237 : 134785 : gcc_assert (!CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)));
3238 : 134785 : ctx->global->put_value (remapped, integer_zero_node);
3239 : : }
3240 : : else
3241 : : {
3242 : 6 : gcc_assert (seen_error ());
3243 : 6 : *non_constant_p = true;
3244 : : }
3245 : : /* Add the RESULT_DECL to the values map, too. */
3246 : 31593056 : gcc_assert (!DECL_BY_REFERENCE (res));
3247 : 31593056 : ctx->global->put_value (res, NULL_TREE);
3248 : :
3249 : : /* Remember the current call we're evaluating. */
3250 : 31593056 : constexpr_ctx call_ctx = *ctx;
3251 : 31593056 : call_ctx.call = &new_call;
3252 : 31593056 : unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
3253 : 31593056 : unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
3254 : :
3255 : : /* Make sure we fold std::is_constant_evaluated to true in an
3256 : : immediate function. */
3257 : 63186112 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
3258 : 209490 : call_ctx.manifestly_const_eval = mce_true;
3259 : :
3260 : : /* If this is a constexpr destructor, the object's const and volatile
3261 : : semantics are no longer in effect; see [class.dtor]p5. */
3262 : 36494620 : if (new_obj && DECL_DESTRUCTOR_P (fun))
3263 : 134786 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false,
3264 : : non_constant_p, overflow_p);
3265 : :
3266 : : /* If this is a constructor, we are beginning the lifetime of the
3267 : : object we are initializing. */
3268 : 31593056 : if (new_obj
3269 : 9803128 : && DECL_CONSTRUCTOR_P (fun)
3270 : 4766778 : && TREE_CODE (new_obj) == COMPONENT_REF
3271 : 34219569 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (new_obj, 0))) == UNION_TYPE)
3272 : : {
3273 : 5939 : tree activate = build2 (INIT_EXPR, TREE_TYPE (new_obj),
3274 : : new_obj,
3275 : 5939 : build_constructor (TREE_TYPE (new_obj),
3276 : : NULL));
3277 : 5939 : cxx_eval_constant_expression (ctx, activate,
3278 : : lval, non_constant_p, overflow_p);
3279 : 5939 : ggc_free (activate);
3280 : : }
3281 : :
3282 : 31593056 : tree jump_target = NULL_TREE;
3283 : 31593056 : cxx_eval_constant_expression (&call_ctx, body,
3284 : : vc_discard, non_constant_p, overflow_p,
3285 : : &jump_target);
3286 : :
3287 : 63186112 : if (DECL_CONSTRUCTOR_P (fun))
3288 : : /* This can be null for a subobject constructor call, in
3289 : : which case what we care about is the initialization
3290 : : side-effects rather than the value. We could get at the
3291 : : value by evaluating *this, but we don't bother; there's
3292 : : no need to put such a call in the hash table. */
3293 : 5320793 : result = lval ? ctx->object : ctx->ctor;
3294 : 26272263 : else if (VOID_TYPE_P (TREE_TYPE (res)))
3295 : 917683 : result = void_node;
3296 : : else
3297 : : {
3298 : 25354580 : result = ctx->global->get_value (res);
3299 : 10049505 : if (result == NULL_TREE && !*non_constant_p
3300 : 25354746 : && !DECL_DESTRUCTOR_P (fun))
3301 : : {
3302 : 83 : if (!ctx->quiet)
3303 : 12 : error ("%<constexpr%> call flows off the end "
3304 : : "of the function");
3305 : 83 : *non_constant_p = true;
3306 : : }
3307 : : }
3308 : :
3309 : : /* At this point, the object's constructor will have run, so
3310 : : the object is no longer under construction, and its possible
3311 : : 'const' semantics now apply. Make a note of this fact by
3312 : : marking the CONSTRUCTOR TREE_READONLY. */
3313 : 36494620 : if (new_obj && DECL_CONSTRUCTOR_P (fun))
3314 : 4766778 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true,
3315 : : non_constant_p, overflow_p);
3316 : :
3317 : : /* Remove the parms/result from the values map. */
3318 : 31593056 : destroy_value_checked (ctx, res, non_constant_p);
3319 : 68286818 : for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
3320 : 36693762 : destroy_value_checked (ctx, parm, non_constant_p);
3321 : :
3322 : : /* Free any parameter CONSTRUCTORs we aren't returning directly. */
3323 : 32273296 : while (!ctors->is_empty ())
3324 : : {
3325 : 680240 : tree c = ctors->pop ();
3326 : 680240 : if (c != result)
3327 : 680240 : free_constructor (c);
3328 : : }
3329 : :
3330 : : /* Make the unshared function copy we used available for re-use. */
3331 : 31593056 : save_fundef_copy (fun, copy);
3332 : :
3333 : : /* If the call allocated some heap object that hasn't been
3334 : : deallocated during the call, or if it deallocated some heap
3335 : : object it has not allocated, the call isn't really stateless
3336 : : for the constexpr evaluation and should not be cached.
3337 : : It is fine if the call allocates something and deallocates it
3338 : : too. */
3339 : 31593056 : if (cacheable
3340 : 35956080 : && (save_heap_alloc_count != ctx->global->heap_vars.length ()
3341 : 4362740 : || (save_heap_dealloc_count
3342 : 4362740 : != ctx->global->heap_dealloc_count)))
3343 : : {
3344 : 284 : tree heap_var;
3345 : 284 : unsigned int i;
3346 : 284 : if ((ctx->global->heap_vars.length ()
3347 : 284 : - ctx->global->heap_dealloc_count)
3348 : 284 : != save_heap_alloc_count - save_heap_dealloc_count)
3349 : : cacheable = false;
3350 : : else
3351 : 910 : FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var,
3352 : : save_heap_alloc_count)
3353 : 662 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
3354 : : {
3355 : : cacheable = false;
3356 : : break;
3357 : : }
3358 : : }
3359 : :
3360 : : /* Rewrite all occurrences of the function's RESULT_DECL with the
3361 : : current object under construction. */
3362 : 16872164 : if (!*non_constant_p && ctx->object
3363 : 5012680 : && CLASS_TYPE_P (TREE_TYPE (res))
3364 : 32482420 : && !is_empty_class (TREE_TYPE (res)))
3365 : 295222 : if (replace_decl (&result, res, ctx->object))
3366 : : cacheable = false;
3367 : :
3368 : : /* Only cache a permitted result of a constant expression. */
3369 : 31592871 : if (cacheable && !reduced_constant_expression_p (result))
3370 : : cacheable = false;
3371 : 31593056 : }
3372 : : else
3373 : : /* Couldn't get a function copy to evaluate. */
3374 : 0 : *non_constant_p = true;
3375 : :
3376 : 42942066 : if (result == error_mark_node)
3377 : 0 : *non_constant_p = true;
3378 : 42942066 : if (*non_constant_p || *overflow_p)
3379 : 14721048 : result = error_mark_node;
3380 : 28221018 : else if (!result)
3381 : 1220937 : result = void_node;
3382 : 42942066 : if (entry)
3383 : 15712034 : entry->result = cacheable ? result : error_mark_node;
3384 : : }
3385 : :
3386 : : /* The result of a constexpr function must be completely initialized.
3387 : :
3388 : : However, in C++20, a constexpr constructor doesn't necessarily have
3389 : : to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
3390 : : in order to detect reading an unitialized object in constexpr instead
3391 : : of value-initializing it. (reduced_constant_expression_p is expected to
3392 : : take care of clearing the flag.) */
3393 : 42942087 : if (TREE_CODE (result) == CONSTRUCTOR
3394 : 42942087 : && (cxx_dialect < cxx20
3395 : 4546674 : || !DECL_CONSTRUCTOR_P (fun)))
3396 : 2835971 : clear_no_implicit_zero (result);
3397 : :
3398 : 42942087 : pop_cx_call_context ();
3399 : 42942087 : return result;
3400 : 58648385 : }
3401 : :
3402 : : /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
3403 : : initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
3404 : : cleared.
3405 : : FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
3406 : :
3407 : : bool
3408 : 503868968 : reduced_constant_expression_p (tree t)
3409 : : {
3410 : 503868968 : if (t == NULL_TREE)
3411 : : return false;
3412 : :
3413 : 502596533 : switch (TREE_CODE (t))
3414 : : {
3415 : : case PTRMEM_CST:
3416 : : /* Even if we can't lower this yet, it's constant. */
3417 : : return true;
3418 : :
3419 : 28753791 : case CONSTRUCTOR:
3420 : : /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
3421 : 28753791 : tree field;
3422 : 28753791 : if (!AGGREGATE_TYPE_P (TREE_TYPE (t)))
3423 : : /* A constant vector would be folded to VECTOR_CST.
3424 : : A CONSTRUCTOR of scalar type means uninitialized. */
3425 : : return false;
3426 : 28736344 : if (CONSTRUCTOR_NO_CLEARING (t))
3427 : : {
3428 : 639946 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
3429 : : {
3430 : : /* There must be a valid constant initializer at every array
3431 : : index. */
3432 : 170 : tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
3433 : 170 : tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
3434 : 170 : tree cursor = min;
3435 : 15861 : for (auto &e: CONSTRUCTOR_ELTS (t))
3436 : : {
3437 : 15421 : if (!reduced_constant_expression_p (e.value))
3438 : : return false;
3439 : 15371 : if (array_index_cmp (cursor, e.index) != 0)
3440 : : return false;
3441 : 15351 : if (TREE_CODE (e.index) == RANGE_EXPR)
3442 : 0 : cursor = TREE_OPERAND (e.index, 1);
3443 : 15351 : cursor = int_const_binop (PLUS_EXPR, cursor, size_one_node);
3444 : : }
3445 : 100 : if (find_array_ctor_elt (t, max) == -1)
3446 : : return false;
3447 : 22 : goto ok;
3448 : : }
3449 : 639776 : else if (cxx_dialect >= cxx20
3450 : 639776 : && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
3451 : : {
3452 : 2150276 : if (CONSTRUCTOR_NELTS (t) == 0)
3453 : : /* An initialized union has a constructor element. */
3454 : : return false;
3455 : : /* And it only initializes one member. */
3456 : : field = NULL_TREE;
3457 : : }
3458 : : else
3459 : 639771 : field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t)));
3460 : : }
3461 : : else
3462 : : field = NULL_TREE;
3463 : 160970645 : for (auto &e: CONSTRUCTOR_ELTS (t))
3464 : : {
3465 : : /* If VAL is null, we're in the middle of initializing this
3466 : : element. */
3467 : 96887170 : if (!reduced_constant_expression_p (e.value))
3468 : : return false;
3469 : : /* We want to remove initializers for empty fields in a struct to
3470 : : avoid confusing output_constructor. */
3471 : 96543936 : if (is_empty_field (e.index)
3472 : 96543936 : && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
3473 : : return false;
3474 : : /* Check for non-empty fields between initialized fields when
3475 : : CONSTRUCTOR_NO_CLEARING. */
3476 : 96083891 : for (; field && e.index != field;
3477 : 55548 : field = next_subobject_field (DECL_CHAIN (field)))
3478 : 56613 : if (!is_really_empty_class (TREE_TYPE (field),
3479 : : /*ignore_vptr*/false))
3480 : : return false;
3481 : 96027278 : if (field)
3482 : 569522 : field = next_subobject_field (DECL_CHAIN (field));
3483 : : }
3484 : : /* There could be a non-empty field at the end. */
3485 : 27902461 : for (; field; field = next_subobject_field (DECL_CHAIN (field)))
3486 : 26533 : if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false))
3487 : : return false;
3488 : 27875928 : ok:
3489 : 27875950 : if (CONSTRUCTOR_NO_CLEARING (t))
3490 : : /* All the fields are initialized. */
3491 : 584379 : CONSTRUCTOR_NO_CLEARING (t) = false;
3492 : : return true;
3493 : :
3494 : 473838503 : default:
3495 : : /* FIXME are we calling this too much? */
3496 : 473838503 : return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
3497 : : }
3498 : : }
3499 : :
3500 : : /* *TP was not deemed constant by reduced_constant_expression_p. Explain
3501 : : why and suggest what could be done about it. */
3502 : :
3503 : : static tree
3504 : 814 : verify_constant_explain_r (tree *tp, int *walk_subtrees, void *)
3505 : : {
3506 : 814 : bool ref_p = false;
3507 : :
3508 : : /* No need to look into types or unevaluated operands. */
3509 : 814 : if (TYPE_P (*tp) || unevaluated_p (TREE_CODE (*tp)))
3510 : : {
3511 : 0 : *walk_subtrees = false;
3512 : 0 : return NULL_TREE;
3513 : : }
3514 : :
3515 : 814 : switch (TREE_CODE (*tp))
3516 : : {
3517 : 39 : CASE_CONVERT:
3518 : 39 : if (TREE_CODE (TREE_OPERAND (*tp, 0)) != ADDR_EXPR)
3519 : : break;
3520 : 28 : ref_p = TYPE_REF_P (TREE_TYPE (*tp));
3521 : 28 : *tp = TREE_OPERAND (*tp, 0);
3522 : 150 : gcc_fallthrough ();
3523 : 150 : case ADDR_EXPR:
3524 : 150 : {
3525 : 150 : tree op = TREE_OPERAND (*tp, 0);
3526 : 150 : if (VAR_P (op)
3527 : 81 : && DECL_DECLARED_CONSTEXPR_P (op)
3528 : 31 : && !TREE_STATIC (op)
3529 : : /* ??? We should also say something about temporaries. */
3530 : 180 : && !DECL_ARTIFICIAL (op))
3531 : : {
3532 : 27 : if (ref_p)
3533 : 6 : inform (location_of (*tp), "reference to %qD is not a constant "
3534 : : "expression", op);
3535 : : else
3536 : 21 : inform (location_of (*tp), "pointer to %qD is not a constant "
3537 : : "expression", op);
3538 : 27 : const location_t op_loc = DECL_SOURCE_LOCATION (op);
3539 : 27 : rich_location richloc (line_table, op_loc);
3540 : 27 : richloc.add_fixit_insert_before (op_loc, "static ");
3541 : 27 : inform (&richloc,
3542 : : "address of non-static constexpr variable %qD may differ on "
3543 : : "each invocation of the enclosing function; add %<static%> "
3544 : : "to give it a constant address", op);
3545 : 27 : }
3546 : : break;
3547 : : }
3548 : : default:
3549 : : break;
3550 : : }
3551 : :
3552 : : return NULL_TREE;
3553 : : }
3554 : :
3555 : : /* Some expressions may have constant operands but are not constant
3556 : : themselves, such as 1/0. Call this function to check for that
3557 : : condition.
3558 : :
3559 : : We only call this in places that require an arithmetic constant, not in
3560 : : places where we might have a non-constant expression that can be a
3561 : : component of a constant expression, such as the address of a constexpr
3562 : : variable that might be dereferenced later. */
3563 : :
3564 : : static bool
3565 : 430033827 : verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
3566 : : bool *overflow_p)
3567 : : {
3568 : 350865636 : if (!*non_constant_p && !reduced_constant_expression_p (t)
3569 : 440389471 : && t != void_node)
3570 : : {
3571 : 10355630 : if (!allow_non_constant)
3572 : : {
3573 : 200 : auto_diagnostic_group d;
3574 : 255 : error_at (cp_expr_loc_or_input_loc (t),
3575 : : "%q+E is not a constant expression", t);
3576 : 200 : cp_walk_tree_without_duplicates (&t, verify_constant_explain_r,
3577 : : nullptr);
3578 : 200 : }
3579 : 10355630 : *non_constant_p = true;
3580 : : }
3581 : 430033827 : if (TREE_OVERFLOW_P (t))
3582 : : {
3583 : 920 : if (!allow_non_constant)
3584 : : {
3585 : 195 : permerror (input_location, "overflow in constant expression");
3586 : : /* If we're being permissive (and are in an enforcing
3587 : : context), ignore the overflow. */
3588 : 195 : if (flag_permissive)
3589 : 100 : return *non_constant_p;
3590 : : }
3591 : 820 : *overflow_p = true;
3592 : : }
3593 : 430033727 : return *non_constant_p;
3594 : : }
3595 : :
3596 : : /* Check whether the shift operation with code CODE and type TYPE on LHS
3597 : : and RHS is undefined. If it is, give an error with an explanation,
3598 : : and return true; return false otherwise. */
3599 : :
3600 : : static bool
3601 : 31707846 : cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
3602 : : enum tree_code code, tree type, tree lhs, tree rhs)
3603 : : {
3604 : 31707846 : if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
3605 : 3134599 : || TREE_CODE (lhs) != INTEGER_CST
3606 : 3134599 : || TREE_CODE (rhs) != INTEGER_CST)
3607 : : return false;
3608 : :
3609 : 3134599 : tree lhstype = TREE_TYPE (lhs);
3610 : 3134599 : unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
3611 : :
3612 : : /* [expr.shift] The behavior is undefined if the right operand
3613 : : is negative, or greater than or equal to the length in bits
3614 : : of the promoted left operand. */
3615 : 3134599 : if (tree_int_cst_sgn (rhs) == -1)
3616 : : {
3617 : 138 : if (!ctx->quiet)
3618 : 40 : permerror (loc, "right operand of shift expression %q+E is negative",
3619 : : build2_loc (loc, code, type, lhs, rhs));
3620 : 138 : return (!flag_permissive || ctx->quiet);
3621 : : }
3622 : 3134461 : if (compare_tree_int (rhs, uprec) >= 0)
3623 : : {
3624 : 291 : if (!ctx->quiet)
3625 : 37 : permerror (loc, "right operand of shift expression %q+E is greater "
3626 : : "than or equal to the precision %wu of the left operand",
3627 : : build2_loc (loc, code, type, lhs, rhs), uprec);
3628 : 291 : return (!flag_permissive || ctx->quiet);
3629 : : }
3630 : :
3631 : : /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
3632 : : if E1 has a signed type and non-negative value, and E1x2^E2 is
3633 : : representable in the corresponding unsigned type of the result type,
3634 : : then that value, converted to the result type, is the resulting value;
3635 : : otherwise, the behavior is undefined.
3636 : : For C++20:
3637 : : The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
3638 : : 2^N, where N is the range exponent of the type of the result. */
3639 : 3134170 : if (code == LSHIFT_EXPR
3640 : 3006131 : && !TYPE_OVERFLOW_WRAPS (lhstype)
3641 : 2235688 : && cxx_dialect >= cxx11
3642 : 5349972 : && cxx_dialect < cxx20)
3643 : : {
3644 : 1747091 : if (tree_int_cst_sgn (lhs) == -1)
3645 : : {
3646 : 155 : if (!ctx->quiet)
3647 : 23 : permerror (loc,
3648 : : "left operand of shift expression %q+E is negative",
3649 : : build2_loc (loc, code, type, lhs, rhs));
3650 : 155 : return (!flag_permissive || ctx->quiet);
3651 : : }
3652 : : /* For signed x << y the following:
3653 : : (unsigned) x >> ((prec (lhs) - 1) - y)
3654 : : if > 1, is undefined. The right-hand side of this formula
3655 : : is the highest bit of the LHS that can be set (starting from 0),
3656 : : so that the shift doesn't overflow. We then right-shift the LHS
3657 : : to see whether any other bit is set making the original shift
3658 : : undefined -- the result is not representable in the corresponding
3659 : : unsigned type. */
3660 : 1746936 : tree t = build_int_cst (unsigned_type_node, uprec - 1);
3661 : 1746936 : t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
3662 : 1746936 : tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
3663 : 1746936 : t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
3664 : 1746936 : if (tree_int_cst_lt (integer_one_node, t))
3665 : : {
3666 : 108 : if (!ctx->quiet)
3667 : 8 : permerror (loc, "shift expression %q+E overflows",
3668 : : build2_loc (loc, code, type, lhs, rhs));
3669 : 108 : return (!flag_permissive || ctx->quiet);
3670 : : }
3671 : : }
3672 : : return false;
3673 : : }
3674 : :
3675 : : /* Subroutine of cxx_eval_constant_expression.
3676 : : Attempt to reduce the unary expression tree T to a compile time value.
3677 : : If successful, return the value. Otherwise issue a diagnostic
3678 : : and return error_mark_node. */
3679 : :
3680 : : static tree
3681 : 20752316 : cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
3682 : : bool /*lval*/,
3683 : : bool *non_constant_p, bool *overflow_p)
3684 : : {
3685 : 20752316 : tree r;
3686 : 20752316 : tree orig_arg = TREE_OPERAND (t, 0);
3687 : 20752316 : tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue,
3688 : : non_constant_p, overflow_p);
3689 : 20752316 : VERIFY_CONSTANT (arg);
3690 : 17525503 : location_t loc = EXPR_LOCATION (t);
3691 : 17525503 : enum tree_code code = TREE_CODE (t);
3692 : 17525503 : tree type = TREE_TYPE (t);
3693 : 17525503 : r = fold_unary_loc (loc, code, type, arg);
3694 : 17525503 : if (r == NULL_TREE)
3695 : : {
3696 : 3 : if (arg == orig_arg)
3697 : : r = t;
3698 : : else
3699 : 3 : r = build1_loc (loc, code, type, arg);
3700 : : }
3701 : 17525503 : VERIFY_CONSTANT (r);
3702 : : return r;
3703 : : }
3704 : :
3705 : : /* Helper function for cxx_eval_binary_expression. Try to optimize
3706 : : original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
3707 : : generic folding should be used. */
3708 : :
3709 : : static tree
3710 : 1857063 : cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
3711 : : tree lhs, tree rhs, bool *non_constant_p,
3712 : : bool *overflow_p)
3713 : : {
3714 : 1857063 : STRIP_NOPS (lhs);
3715 : 1857063 : if (TREE_CODE (lhs) != ADDR_EXPR)
3716 : : return NULL_TREE;
3717 : :
3718 : 1779910 : lhs = TREE_OPERAND (lhs, 0);
3719 : :
3720 : : /* &A[i] p+ j => &A[i + j] */
3721 : 1779910 : if (TREE_CODE (lhs) == ARRAY_REF
3722 : 3447 : && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
3723 : 3447 : && TREE_CODE (rhs) == INTEGER_CST
3724 : 3447 : && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
3725 : 1783357 : && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
3726 : : {
3727 : 3447 : tree orig_type = TREE_TYPE (t);
3728 : 3447 : location_t loc = EXPR_LOCATION (t);
3729 : 3447 : tree type = TREE_TYPE (lhs);
3730 : :
3731 : 3447 : t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
3732 : 3447 : tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
3733 : 3447 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
3734 : : non_constant_p, overflow_p);
3735 : 3447 : if (*non_constant_p)
3736 : : return NULL_TREE;
3737 : : /* Don't fold an out-of-bound access. */
3738 : 3429 : if (!tree_int_cst_le (t, nelts))
3739 : : return NULL_TREE;
3740 : 3429 : rhs = cp_fold_convert (ssizetype, rhs);
3741 : : /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
3742 : : constexpr int A[1]; ... (char *)&A[0] + 1 */
3743 : 3429 : if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
3744 : 3429 : rhs, TYPE_SIZE_UNIT (type))))
3745 : : return NULL_TREE;
3746 : : /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3747 : : as signed. */
3748 : 3392 : rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
3749 : 3392 : TYPE_SIZE_UNIT (type));
3750 : 3392 : t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
3751 : 3392 : t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
3752 : : t, NULL_TREE, NULL_TREE);
3753 : 3392 : t = cp_build_addr_expr (t, tf_warning_or_error);
3754 : 3392 : t = cp_fold_convert (orig_type, t);
3755 : 3392 : return cxx_eval_constant_expression (ctx, t, vc_prvalue,
3756 : 3392 : non_constant_p, overflow_p);
3757 : : }
3758 : :
3759 : : return NULL_TREE;
3760 : : }
3761 : :
3762 : : /* Try to fold expressions like
3763 : : (struct S *) (&a[0].D.2378 + 12)
3764 : : into
3765 : : &MEM <struct T> [(void *)&a + 12B]
3766 : : This is something normally done by gimple_fold_stmt_to_constant_1
3767 : : on GIMPLE, but is undesirable on GENERIC if we are e.g. going to
3768 : : dereference the address because some details are lost.
3769 : : For pointer comparisons we want such folding though so that
3770 : : match.pd address_compare optimization works. */
3771 : :
3772 : : static tree
3773 : 1429940 : cxx_maybe_fold_addr_pointer_plus (tree t)
3774 : : {
3775 : 2866431 : while (CONVERT_EXPR_P (t)
3776 : 1602850 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
3777 : 166359 : t = TREE_OPERAND (t, 0);
3778 : 1429940 : if (TREE_CODE (t) != POINTER_PLUS_EXPR)
3779 : : return NULL_TREE;
3780 : 1197158 : tree op0 = TREE_OPERAND (t, 0);
3781 : 1197158 : tree op1 = TREE_OPERAND (t, 1);
3782 : 1197158 : if (TREE_CODE (op1) != INTEGER_CST)
3783 : : return NULL_TREE;
3784 : 1197158 : while (CONVERT_EXPR_P (op0)
3785 : 2393472 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
3786 : 1196314 : op0 = TREE_OPERAND (op0, 0);
3787 : 1197158 : if (TREE_CODE (op0) != ADDR_EXPR)
3788 : : return NULL_TREE;
3789 : 1196496 : op1 = fold_convert (ptr_type_node, op1);
3790 : 1196496 : tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1);
3791 : 1196496 : return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r);
3792 : : }
3793 : :
3794 : : /* Subroutine of cxx_eval_constant_expression.
3795 : : Like cxx_eval_unary_expression, except for binary expressions. */
3796 : :
3797 : : static tree
3798 : 48160332 : cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
3799 : : value_cat lval,
3800 : : bool *non_constant_p, bool *overflow_p)
3801 : : {
3802 : 48160332 : tree r = NULL_TREE;
3803 : 48160332 : tree orig_lhs = TREE_OPERAND (t, 0);
3804 : 48160332 : tree orig_rhs = TREE_OPERAND (t, 1);
3805 : 48160332 : tree lhs, rhs;
3806 : 48160332 : lhs = cxx_eval_constant_expression (ctx, orig_lhs, vc_prvalue,
3807 : : non_constant_p, overflow_p);
3808 : : /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
3809 : : subtraction. */
3810 : 48160332 : if (*non_constant_p)
3811 : : return t;
3812 : 35363146 : rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue,
3813 : : non_constant_p, overflow_p);
3814 : 35363146 : if (*non_constant_p)
3815 : : return t;
3816 : :
3817 : 33469316 : location_t loc = EXPR_LOCATION (t);
3818 : 33469316 : enum tree_code code = TREE_CODE (t);
3819 : 33469316 : tree type = TREE_TYPE (t);
3820 : :
3821 : 33469316 : if (code == EQ_EXPR || code == NE_EXPR)
3822 : : {
3823 : 3242803 : bool is_code_eq = (code == EQ_EXPR);
3824 : :
3825 : 3242803 : if (TREE_CODE (lhs) == PTRMEM_CST
3826 : 137 : && TREE_CODE (rhs) == PTRMEM_CST)
3827 : : {
3828 : 54 : tree lmem = PTRMEM_CST_MEMBER (lhs);
3829 : 54 : tree rmem = PTRMEM_CST_MEMBER (rhs);
3830 : 54 : bool eq;
3831 : 54 : if (TREE_CODE (lmem) == TREE_CODE (rmem)
3832 : 54 : && TREE_CODE (lmem) == FIELD_DECL
3833 : 54 : && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
3834 : 81 : && same_type_p (DECL_CONTEXT (lmem),
3835 : : DECL_CONTEXT (rmem)))
3836 : : /* If both refer to (possibly different) members of the same union
3837 : : (12.3), they compare equal. */
3838 : : eq = true;
3839 : : else
3840 : 27 : eq = cp_tree_equal (lhs, rhs);
3841 : 54 : r = constant_boolean_node (eq == is_code_eq, type);
3842 : 54 : }
3843 : 3242749 : else if ((TREE_CODE (lhs) == PTRMEM_CST
3844 : 3242666 : || TREE_CODE (rhs) == PTRMEM_CST)
3845 : 3242749 : && (null_member_pointer_value_p (lhs)
3846 : 83 : || null_member_pointer_value_p (rhs)))
3847 : 83 : r = constant_boolean_node (!is_code_eq, type);
3848 : 3242666 : else if (TREE_CODE (lhs) == PTRMEM_CST)
3849 : 0 : lhs = cplus_expand_constant (lhs);
3850 : 3242666 : else if (TREE_CODE (rhs) == PTRMEM_CST)
3851 : 0 : rhs = cplus_expand_constant (rhs);
3852 : : }
3853 : 33469316 : if (r == NULL_TREE
3854 : 33469179 : && TREE_CODE_CLASS (code) == tcc_comparison
3855 : 44545627 : && POINTER_TYPE_P (TREE_TYPE (lhs)))
3856 : : {
3857 : 714970 : if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs))
3858 : 591649 : lhs = fold_convert (TREE_TYPE (lhs), lhso);
3859 : 714970 : if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs))
3860 : 604847 : rhs = fold_convert (TREE_TYPE (rhs), rhso);
3861 : : }
3862 : 1857220 : if (code == POINTER_PLUS_EXPR && !*non_constant_p
3863 : 35326536 : && integer_zerop (lhs) && !integer_zerop (rhs))
3864 : : {
3865 : 157 : if (!ctx->quiet)
3866 : 45 : error ("arithmetic involving a null pointer in %qE", lhs);
3867 : 157 : *non_constant_p = true;
3868 : 157 : return t;
3869 : : }
3870 : 33469159 : else if (code == POINTER_PLUS_EXPR)
3871 : 1857063 : r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
3872 : : overflow_p);
3873 : 31612096 : else if (code == SPACESHIP_EXPR)
3874 : : {
3875 : 15453 : r = genericize_spaceship (loc, type, lhs, rhs);
3876 : 15453 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
3877 : 15453 : overflow_p);
3878 : : }
3879 : :
3880 : 33453706 : if (r == NULL_TREE)
3881 : : {
3882 : 33450177 : if (ctx->manifestly_const_eval == mce_true
3883 : 16179696 : && (flag_constexpr_fp_except
3884 : 16179693 : || TREE_CODE (type) != REAL_TYPE))
3885 : : {
3886 : 16169029 : auto ofcc = make_temp_override (folding_cxx_constexpr, true);
3887 : 16169029 : r = fold_binary_initializer_loc (loc, code, type, lhs, rhs);
3888 : 16169029 : }
3889 : : else
3890 : 17281148 : r = fold_binary_loc (loc, code, type, lhs, rhs);
3891 : : }
3892 : :
3893 : 33450177 : if (r == NULL_TREE
3894 : 1746003 : && (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
3895 : 156 : && TREE_CODE (lhs) == INTEGER_CST
3896 : 143 : && TREE_CODE (rhs) == INTEGER_CST
3897 : 35199709 : && wi::neg_p (wi::to_wide (rhs)))
3898 : : {
3899 : : /* For diagnostics and -fpermissive emulate previous behavior of
3900 : : handling shifts by negative amount. */
3901 : 143 : tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs);
3902 : 143 : if (nrhs)
3903 : 201 : r = fold_binary_loc (loc,
3904 : : code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR,
3905 : : type, lhs, nrhs);
3906 : : }
3907 : :
3908 : 33453706 : if (r == NULL_TREE)
3909 : : {
3910 : 1745860 : if (lhs == orig_lhs && rhs == orig_rhs)
3911 : : r = t;
3912 : : else
3913 : 301039 : r = build2_loc (loc, code, type, lhs, rhs);
3914 : : }
3915 : 31707846 : else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
3916 : 680 : *non_constant_p = true;
3917 : : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3918 : : a local array in a constexpr function. */
3919 : 33453706 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
3920 : 30849473 : if (!ptr)
3921 : 30849473 : VERIFY_CONSTANT (r);
3922 : : return r;
3923 : : }
3924 : :
3925 : : /* Subroutine of cxx_eval_constant_expression.
3926 : : Attempt to evaluate condition expressions. */
3927 : :
3928 : : static tree
3929 : 5704182 : cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
3930 : : value_cat lval,
3931 : : bool *non_constant_p, bool *overflow_p,
3932 : : tree *jump_target)
3933 : : {
3934 : 5704182 : tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3935 : : vc_prvalue,
3936 : : non_constant_p, overflow_p);
3937 : 5704182 : VERIFY_CONSTANT (val);
3938 : 4486781 : if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t))
3939 : : {
3940 : : /* Evaluate the condition as if it was
3941 : : if (__builtin_is_constant_evaluated ()), i.e. defer it if not
3942 : : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
3943 : : without manifestly_const_eval even expressions or parts thereof which
3944 : : will later be manifestly const_eval evaluated), otherwise fold it to
3945 : : true. */
3946 : 436936 : if (ctx->manifestly_const_eval == mce_unknown)
3947 : : {
3948 : 432997 : *non_constant_p = true;
3949 : 432997 : return t;
3950 : : }
3951 : 3939 : val = constant_boolean_node (ctx->manifestly_const_eval == mce_true,
3952 : : boolean_type_node);
3953 : : }
3954 : : /* Don't VERIFY_CONSTANT the other operands. */
3955 : 4053784 : const bool zero_p = integer_zerop (val);
3956 : 4053784 : if (zero_p)
3957 : 2131230 : val = TREE_OPERAND (t, 2);
3958 : : else
3959 : 1922554 : val = TREE_OPERAND (t, 1);
3960 : 4053784 : if (TREE_CODE (t) == IF_STMT && !val)
3961 : 684720 : val = void_node;
3962 : :
3963 : : /* P2564: a subexpression of a manifestly constant-evaluated expression
3964 : : or conversion is an immediate function context. */
3965 : 4053784 : if (ctx->manifestly_const_eval != mce_true
3966 : 1867359 : && !in_immediate_context ()
3967 : 5921126 : && cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2),
3968 : 1867342 : ctx->manifestly_const_eval))
3969 : : {
3970 : 4 : *non_constant_p = true;
3971 : 4 : return t;
3972 : : }
3973 : :
3974 : : /* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still
3975 : : serve as the initializer for the same object as the outer TARGET_EXPR,
3976 : : as in
3977 : : A a = true ? A{} : A{};
3978 : : so strip the inner TARGET_EXPR so we don't materialize a temporary. */
3979 : 4053780 : if (TREE_CODE (val) == TARGET_EXPR)
3980 : 662 : val = TARGET_EXPR_INITIAL (val);
3981 : 4053780 : return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
3982 : 4053780 : overflow_p, jump_target);
3983 : : }
3984 : :
3985 : : /* Subroutine of cxx_eval_constant_expression.
3986 : : Attempt to evaluate vector condition expressions. Unlike
3987 : : cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
3988 : : ternary arithmetics operation, where all 3 arguments have to be
3989 : : evaluated as constants and then folding computes the result from
3990 : : them. */
3991 : :
3992 : : static tree
3993 : 655 : cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
3994 : : bool *non_constant_p, bool *overflow_p)
3995 : : {
3996 : 655 : tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3997 : : vc_prvalue,
3998 : : non_constant_p, overflow_p);
3999 : 655 : VERIFY_CONSTANT (arg1);
4000 : 653 : tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
4001 : : vc_prvalue,
4002 : : non_constant_p, overflow_p);
4003 : 653 : VERIFY_CONSTANT (arg2);
4004 : 653 : tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
4005 : : vc_prvalue,
4006 : : non_constant_p, overflow_p);
4007 : 653 : VERIFY_CONSTANT (arg3);
4008 : 653 : location_t loc = EXPR_LOCATION (t);
4009 : 653 : tree type = TREE_TYPE (t);
4010 : 653 : tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
4011 : 653 : if (r == NULL_TREE)
4012 : : {
4013 : 0 : if (arg1 == TREE_OPERAND (t, 0)
4014 : 0 : && arg2 == TREE_OPERAND (t, 1)
4015 : 0 : && arg3 == TREE_OPERAND (t, 2))
4016 : : r = t;
4017 : : else
4018 : 0 : r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
4019 : : }
4020 : 653 : VERIFY_CONSTANT (r);
4021 : : return r;
4022 : : }
4023 : :
4024 : : /* Returns less than, equal to, or greater than zero if KEY is found to be
4025 : : less than, to match, or to be greater than the constructor_elt's INDEX. */
4026 : :
4027 : : static int
4028 : 21341 : array_index_cmp (tree key, tree index)
4029 : : {
4030 : 21341 : gcc_assert (TREE_CODE (key) == INTEGER_CST);
4031 : :
4032 : 21341 : switch (TREE_CODE (index))
4033 : : {
4034 : 18167 : case INTEGER_CST:
4035 : 18167 : return tree_int_cst_compare (key, index);
4036 : 3174 : case RANGE_EXPR:
4037 : 3174 : {
4038 : 3174 : tree lo = TREE_OPERAND (index, 0);
4039 : 3174 : tree hi = TREE_OPERAND (index, 1);
4040 : 3174 : if (tree_int_cst_lt (key, lo))
4041 : : return -1;
4042 : 2879 : else if (tree_int_cst_lt (hi, key))
4043 : : return 1;
4044 : : else
4045 : 2879 : return 0;
4046 : : }
4047 : 0 : default:
4048 : 0 : gcc_unreachable ();
4049 : : }
4050 : : }
4051 : :
4052 : : /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
4053 : : if none. If INSERT is true, insert a matching element rather than fail. */
4054 : :
4055 : : static HOST_WIDE_INT
4056 : 1173395 : find_array_ctor_elt (tree ary, tree dindex, bool insert)
4057 : : {
4058 : 1173395 : if (tree_int_cst_sgn (dindex) < 0)
4059 : : return -1;
4060 : :
4061 : 1173395 : unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
4062 : 1173395 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
4063 : 1173395 : unsigned HOST_WIDE_INT len = vec_safe_length (elts);
4064 : :
4065 : 1817765 : unsigned HOST_WIDE_INT end = len;
4066 : 1093289 : unsigned HOST_WIDE_INT begin = 0;
4067 : :
4068 : : /* If the last element of the CONSTRUCTOR has its own index, we can assume
4069 : : that the same is true of the other elements and index directly. */
4070 : 1093289 : if (end > 0)
4071 : : {
4072 : 1072754 : tree cindex = (*elts)[end - 1].index;
4073 : 1072754 : if (cindex == NULL_TREE)
4074 : : {
4075 : : /* Verify that if the last index is missing, all indexes
4076 : : are missing. */
4077 : 0 : if (flag_checking)
4078 : 0 : for (unsigned int j = 0; j < len - 1; ++j)
4079 : 0 : gcc_assert ((*elts)[j].index == NULL_TREE);
4080 : 0 : if (i < end)
4081 : 0 : return i;
4082 : : else
4083 : : {
4084 : 0 : begin = end;
4085 : 0 : if (i == end)
4086 : : /* If the element is to be added right at the end,
4087 : : make sure it is added with cleared index too. */
4088 : 724476 : dindex = NULL_TREE;
4089 : 0 : else if (insert)
4090 : : /* Otherwise, in order not to break the assumption
4091 : : that CONSTRUCTOR either has all indexes or none,
4092 : : we need to add indexes to all elements. */
4093 : 0 : for (unsigned int j = 0; j < len; ++j)
4094 : 0 : (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j);
4095 : : }
4096 : : }
4097 : 1072754 : else if (TREE_CODE (cindex) == INTEGER_CST
4098 : 1072754 : && compare_tree_int (cindex, end - 1) == 0)
4099 : : {
4100 : 1068787 : if (i < end)
4101 : 448919 : return i;
4102 : : else
4103 : 724476 : begin = end;
4104 : : }
4105 : : }
4106 : :
4107 : : /* Otherwise, find a matching index by means of a binary search. */
4108 : 727125 : while (begin != end)
4109 : : {
4110 : 5967 : unsigned HOST_WIDE_INT middle = (begin + end) / 2;
4111 : 5967 : constructor_elt &elt = (*elts)[middle];
4112 : 5967 : tree idx = elt.index;
4113 : :
4114 : 5967 : int cmp = array_index_cmp (dindex, idx);
4115 : 5967 : if (cmp < 0)
4116 : : end = middle;
4117 : 4582 : else if (cmp > 0)
4118 : 1264 : begin = middle + 1;
4119 : : else
4120 : : {
4121 : 3318 : if (insert && TREE_CODE (idx) == RANGE_EXPR)
4122 : : {
4123 : : /* We need to split the range. */
4124 : 382 : constructor_elt e;
4125 : 382 : tree lo = TREE_OPERAND (idx, 0);
4126 : 382 : tree hi = TREE_OPERAND (idx, 1);
4127 : 382 : tree value = elt.value;
4128 : 382 : dindex = fold_convert (sizetype, dindex);
4129 : 382 : if (tree_int_cst_lt (lo, dindex))
4130 : : {
4131 : : /* There are still some lower elts; shorten the range. */
4132 : 210 : tree new_hi = int_const_binop (MINUS_EXPR, dindex,
4133 : 105 : size_one_node);
4134 : 105 : if (tree_int_cst_equal (lo, new_hi))
4135 : : /* Only one element left, no longer a range. */
4136 : 53 : elt.index = lo;
4137 : : else
4138 : 52 : TREE_OPERAND (idx, 1) = new_hi;
4139 : : /* Append the element we want to insert. */
4140 : 105 : ++middle;
4141 : 105 : e.index = dindex;
4142 : 105 : e.value = unshare_constructor (value);
4143 : 105 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
4144 : : }
4145 : : else
4146 : : /* No lower elts, the range elt is now ours. */
4147 : 277 : elt.index = dindex;
4148 : :
4149 : 382 : if (tree_int_cst_lt (dindex, hi))
4150 : : {
4151 : : /* There are still some higher elts; append a range. */
4152 : 512 : tree new_lo = int_const_binop (PLUS_EXPR, dindex,
4153 : 256 : size_one_node);
4154 : 256 : if (tree_int_cst_equal (new_lo, hi))
4155 : 127 : e.index = hi;
4156 : : else
4157 : 129 : e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
4158 : 256 : e.value = unshare_constructor (value);
4159 : 256 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
4160 : : }
4161 : : }
4162 : 3318 : return middle;
4163 : : }
4164 : : }
4165 : :
4166 : 721158 : if (insert)
4167 : : {
4168 : 718582 : constructor_elt e = { dindex, NULL_TREE };
4169 : 718582 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
4170 : 718582 : return end;
4171 : : }
4172 : :
4173 : : return -1;
4174 : : }
4175 : :
4176 : : /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
4177 : : matching constructor_elt exists, then add one to CTOR.
4178 : :
4179 : : As an optimization, if POS_HINT is non-negative then it is used as a guess
4180 : : for the (integer) index of the matching constructor_elt within CTOR. */
4181 : :
4182 : : static constructor_elt *
4183 : 6533493 : get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
4184 : : {
4185 : : /* Check the hint first. */
4186 : 457253 : if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor)
4187 : 6990746 : && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index)
4188 : : return CONSTRUCTOR_ELT (ctor, pos_hint);
4189 : :
4190 : 6076245 : tree type = TREE_TYPE (ctor);
4191 : 6076245 : if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE)
4192 : : {
4193 : 130 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE);
4194 : 130 : return &CONSTRUCTOR_ELTS (ctor)->last();
4195 : : }
4196 : 6076115 : else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
4197 : : {
4198 : 954040 : if (TREE_CODE (index) == RANGE_EXPR)
4199 : : {
4200 : : /* Support for RANGE_EXPR index lookups is currently limited to
4201 : : accessing an existing element via POS_HINT, or appending a new
4202 : : element to the end of CTOR. ??? Support for other access
4203 : : patterns may also be needed. */
4204 : 3 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
4205 : 3 : if (vec_safe_length (elts))
4206 : : {
4207 : 3 : tree lo = TREE_OPERAND (index, 0);
4208 : 3 : gcc_assert (array_index_cmp (elts->last().index, lo) < 0);
4209 : : }
4210 : 3 : CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE);
4211 : 3 : return &elts->last();
4212 : : }
4213 : :
4214 : 954037 : HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, /*insert*/true);
4215 : 954037 : gcc_assert (i >= 0);
4216 : 954037 : constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i);
4217 : 954037 : gcc_assert (cep->index == NULL_TREE
4218 : : || TREE_CODE (cep->index) != RANGE_EXPR);
4219 : : return cep;
4220 : : }
4221 : : else
4222 : : {
4223 : 5122075 : gcc_assert (TREE_CODE (index) == FIELD_DECL
4224 : : && (same_type_ignoring_top_level_qualifiers_p
4225 : : (DECL_CONTEXT (index), TREE_TYPE (ctor))));
4226 : :
4227 : : /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
4228 : : Usually we meet initializers in that order, but it is
4229 : : possible for base types to be placed not in program
4230 : : order. */
4231 : 5122075 : tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
4232 : 5122075 : unsigned HOST_WIDE_INT idx = 0;
4233 : 5122075 : constructor_elt *cep = NULL;
4234 : :
4235 : : /* Check if we're changing the active member of a union. */
4236 : 116866 : if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor)
4237 : 5131744 : && CONSTRUCTOR_ELT (ctor, 0)->index != index)
4238 : 1519 : vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0);
4239 : : /* If the bit offset of INDEX is larger than that of the last
4240 : : constructor_elt, then we can just immediately append a new
4241 : : constructor_elt to the end of CTOR. */
4242 : 5120556 : else if (CONSTRUCTOR_NELTS (ctor)
4243 : 5945720 : && tree_int_cst_compare (bit_position (index),
4244 : 5788540 : bit_position (CONSTRUCTOR_ELTS (ctor)
4245 : 2894270 : ->last().index)) > 0)
4246 : : {
4247 : 1317593 : idx = CONSTRUCTOR_NELTS (ctor);
4248 : 1317593 : goto insert;
4249 : : }
4250 : :
4251 : : /* Otherwise, we need to iterate over CTOR to find or insert INDEX
4252 : : appropriately. */
4253 : :
4254 : 5170805 : for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep);
4255 : 1366323 : idx++, fields = DECL_CHAIN (fields))
4256 : : {
4257 : 2943000 : if (index == cep->index)
4258 : 1574289 : goto found;
4259 : :
4260 : : /* The field we're initializing must be on the field
4261 : : list. Look to see if it is present before the
4262 : : field the current ELT initializes. */
4263 : 7416789 : for (; fields != cep->index; fields = DECL_CHAIN (fields))
4264 : 6050466 : if (index == fields)
4265 : 2388 : goto insert;
4266 : : }
4267 : : /* We fell off the end of the CONSTRUCTOR, so insert a new
4268 : : entry at the end. */
4269 : :
4270 : 3547786 : insert:
4271 : 3547786 : {
4272 : 3547786 : constructor_elt ce = { index, NULL_TREE };
4273 : :
4274 : 3547786 : vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce);
4275 : 3547786 : cep = CONSTRUCTOR_ELT (ctor, idx);
4276 : : }
4277 : 5122075 : found:;
4278 : :
4279 : 5122075 : return cep;
4280 : : }
4281 : : }
4282 : :
4283 : : /* Under the control of CTX, issue a detailed diagnostic for
4284 : : an out-of-bounds subscript INDEX into the expression ARRAY. */
4285 : :
4286 : : static void
4287 : 489 : diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
4288 : : {
4289 : 489 : if (!ctx->quiet)
4290 : : {
4291 : 105 : tree arraytype = TREE_TYPE (array);
4292 : :
4293 : : /* Convert the unsigned array subscript to a signed integer to avoid
4294 : : printing huge numbers for small negative values. */
4295 : 105 : tree sidx = fold_convert (ssizetype, index);
4296 : 105 : STRIP_ANY_LOCATION_WRAPPER (array);
4297 : 105 : if (DECL_P (array))
4298 : : {
4299 : 74 : auto_diagnostic_group d;
4300 : 74 : if (TYPE_DOMAIN (arraytype))
4301 : 68 : error_at (loc, "array subscript value %qE is outside the bounds "
4302 : : "of array %qD of type %qT", sidx, array, arraytype);
4303 : : else
4304 : 6 : error_at (loc, "nonzero array subscript %qE is used with array %qD of "
4305 : : "type %qT with unknown bounds", sidx, array, arraytype);
4306 : 74 : inform (DECL_SOURCE_LOCATION (array), "declared here");
4307 : 74 : }
4308 : 31 : else if (TYPE_DOMAIN (arraytype))
4309 : 28 : error_at (loc, "array subscript value %qE is outside the bounds "
4310 : : "of array type %qT", sidx, arraytype);
4311 : : else
4312 : 3 : error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
4313 : : "with unknown bounds", sidx, arraytype);
4314 : : }
4315 : 489 : }
4316 : :
4317 : : /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
4318 : : a VECTOR_TYPE). */
4319 : :
4320 : : static tree
4321 : 2296354 : get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
4322 : : bool *non_constant_p, bool *overflow_p)
4323 : : {
4324 : 2296354 : tree nelts;
4325 : 2296354 : if (TREE_CODE (type) == ARRAY_TYPE)
4326 : : {
4327 : 2296354 : if (TYPE_DOMAIN (type))
4328 : 2296176 : nelts = array_type_nelts_top (type);
4329 : : else
4330 : 178 : nelts = size_zero_node;
4331 : : }
4332 : 0 : else if (VECTOR_TYPE_P (type))
4333 : 0 : nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
4334 : : else
4335 : 0 : gcc_unreachable ();
4336 : :
4337 : : /* For VLAs, the number of elements won't be an integer constant. */
4338 : 2296354 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
4339 : : non_constant_p, overflow_p);
4340 : 2296354 : return nelts;
4341 : : }
4342 : :
4343 : : /* Extract element INDEX consisting of CHARS_PER_ELT chars from
4344 : : STRING_CST STRING. */
4345 : :
4346 : : static tree
4347 : 348580 : extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
4348 : : {
4349 : 348580 : tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
4350 : 348580 : tree r;
4351 : :
4352 : 348580 : if (chars_per_elt == 1)
4353 : 325527 : r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
4354 : : else
4355 : : {
4356 : 23053 : const unsigned char *ptr
4357 : 23053 : = ((const unsigned char *)TREE_STRING_POINTER (string)
4358 : 23053 : + index * chars_per_elt);
4359 : 23053 : r = native_interpret_expr (type, ptr, chars_per_elt);
4360 : : }
4361 : 348580 : return r;
4362 : : }
4363 : :
4364 : : /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
4365 : : subscript, diagnose any problems with it, and return the result. */
4366 : :
4367 : : static tree
4368 : 2299393 : eval_and_check_array_index (const constexpr_ctx *ctx,
4369 : : tree t, bool allow_one_past,
4370 : : bool *non_constant_p, bool *overflow_p)
4371 : : {
4372 : 2299393 : location_t loc = cp_expr_loc_or_input_loc (t);
4373 : 2299393 : tree ary = TREE_OPERAND (t, 0);
4374 : 2299393 : t = TREE_OPERAND (t, 1);
4375 : 2299393 : tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue,
4376 : : non_constant_p, overflow_p);
4377 : 2299393 : VERIFY_CONSTANT (index);
4378 : :
4379 : 2295979 : if (!tree_fits_shwi_p (index)
4380 : 2295979 : || tree_int_cst_sgn (index) < 0)
4381 : : {
4382 : 120 : diag_array_subscript (loc, ctx, ary, index);
4383 : 120 : *non_constant_p = true;
4384 : 120 : return t;
4385 : : }
4386 : :
4387 : 2295859 : tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
4388 : : overflow_p);
4389 : 2295859 : VERIFY_CONSTANT (nelts);
4390 : 2295786 : if (allow_one_past
4391 : 3739384 : ? !tree_int_cst_le (index, nelts)
4392 : 1443598 : : !tree_int_cst_lt (index, nelts))
4393 : : {
4394 : 369 : diag_array_subscript (loc, ctx, ary, index);
4395 : 369 : *non_constant_p = true;
4396 : 369 : return t;
4397 : : }
4398 : :
4399 : : return index;
4400 : : }
4401 : :
4402 : : /* Subroutine of cxx_eval_constant_expression.
4403 : : Attempt to reduce a reference to an array slot. */
4404 : :
4405 : : static tree
4406 : 1618209 : cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
4407 : : value_cat lval,
4408 : : bool *non_constant_p, bool *overflow_p)
4409 : : {
4410 : 1618209 : tree oldary = TREE_OPERAND (t, 0);
4411 : 1618209 : tree ary = cxx_eval_constant_expression (ctx, oldary,
4412 : : lval,
4413 : : non_constant_p, overflow_p);
4414 : 1618209 : if (*non_constant_p)
4415 : : return t;
4416 : 1435040 : if (!lval
4417 : 579535 : && TREE_CODE (ary) == VIEW_CONVERT_EXPR
4418 : 13075 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
4419 : 1448115 : && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))
4420 : 13075 : ary = TREE_OPERAND (ary, 0);
4421 : :
4422 : 1435040 : tree oldidx = TREE_OPERAND (t, 1);
4423 : 1435040 : tree index = eval_and_check_array_index (ctx, t, lval,
4424 : : non_constant_p, overflow_p);
4425 : 1435040 : if (*non_constant_p)
4426 : : return t;
4427 : :
4428 : 1431118 : if (lval && ary == oldary && index == oldidx)
4429 : : return t;
4430 : 678236 : else if (lval == vc_discard)
4431 : : return t;
4432 : 678023 : else if (lval)
4433 : 98897 : return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
4434 : :
4435 : 579126 : unsigned len = 0, elem_nchars = 1;
4436 : 579126 : tree elem_type = TREE_TYPE (TREE_TYPE (ary));
4437 : 579126 : if (TREE_CODE (ary) == CONSTRUCTOR)
4438 : 219258 : len = CONSTRUCTOR_NELTS (ary);
4439 : 359868 : else if (TREE_CODE (ary) == STRING_CST)
4440 : : {
4441 : 346805 : elem_nchars = (TYPE_PRECISION (elem_type)
4442 : 346805 : / TYPE_PRECISION (char_type_node));
4443 : 346805 : len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
4444 : : }
4445 : 13063 : else if (TREE_CODE (ary) == VECTOR_CST)
4446 : : /* We don't create variable-length VECTOR_CSTs. */
4447 : 13063 : len = VECTOR_CST_NELTS (ary).to_constant ();
4448 : : else
4449 : : {
4450 : : /* We can't do anything with other tree codes, so use
4451 : : VERIFY_CONSTANT to complain and fail. */
4452 : 0 : VERIFY_CONSTANT (ary);
4453 : 0 : gcc_unreachable ();
4454 : : }
4455 : :
4456 : 579126 : bool found;
4457 : 579126 : HOST_WIDE_INT i = 0;
4458 : 579126 : if (TREE_CODE (ary) == CONSTRUCTOR)
4459 : : {
4460 : 219258 : HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
4461 : 219258 : found = (ix >= 0);
4462 : 219258 : if (found)
4463 : 216760 : i = ix;
4464 : : }
4465 : : else
4466 : : {
4467 : 359868 : i = tree_to_shwi (index);
4468 : 359868 : found = (i < len);
4469 : : }
4470 : :
4471 : 579126 : if (found)
4472 : : {
4473 : 575927 : tree r;
4474 : 575927 : if (TREE_CODE (ary) == CONSTRUCTOR)
4475 : 216760 : r = (*CONSTRUCTOR_ELTS (ary))[i].value;
4476 : 359167 : else if (TREE_CODE (ary) == VECTOR_CST)
4477 : 13063 : r = VECTOR_CST_ELT (ary, i);
4478 : : else
4479 : 346104 : r = extract_string_elt (ary, elem_nchars, i);
4480 : :
4481 : 575927 : if (r)
4482 : : /* Don't VERIFY_CONSTANT here. */
4483 : : return r;
4484 : :
4485 : : /* Otherwise the element doesn't have a value yet. */
4486 : : }
4487 : :
4488 : : /* Not found. */
4489 : :
4490 : 3199 : if (is_really_empty_class (elem_type, /*ignore_vptr*/false))
4491 : 46 : return build_constructor (elem_type, NULL);
4492 : :
4493 : 3153 : if (TREE_CODE (ary) == CONSTRUCTOR
4494 : 3153 : && CONSTRUCTOR_NO_CLEARING (ary))
4495 : : {
4496 : : /* 'ary' is part of the aggregate initializer we're currently
4497 : : building; if there's no initializer for this element yet,
4498 : : that's an error. */
4499 : 52 : if (!ctx->quiet)
4500 : 16 : error ("accessing uninitialized array element");
4501 : 52 : *non_constant_p = true;
4502 : 52 : return t;
4503 : : }
4504 : :
4505 : : /* If it's within the array bounds but doesn't have an explicit
4506 : : initializer, it's initialized from {}. But use build_value_init
4507 : : directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
4508 : 3101 : tree val;
4509 : 3101 : constexpr_ctx new_ctx;
4510 : 3101 : if (CP_AGGREGATE_TYPE_P (elem_type))
4511 : : {
4512 : 752 : tree empty_ctor = build_constructor (init_list_type_node, NULL);
4513 : 752 : val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
4514 : : }
4515 : : else
4516 : 2349 : val = build_value_init (elem_type, tf_warning_or_error);
4517 : :
4518 : : /* Create a new constructor only if we don't already have a suitable one. */
4519 : 3101 : const bool new_ctor = (!SCALAR_TYPE_P (elem_type)
4520 : 3877 : && (!ctx->ctor
4521 : 15 : || !same_type_ignoring_top_level_qualifiers_p
4522 : 15 : (elem_type, TREE_TYPE (ctx->ctor))));
4523 : 767 : if (new_ctor)
4524 : : {
4525 : 767 : new_ctx = *ctx;
4526 : : /* We clear the object here. We used to replace it with T, but that
4527 : : caused problems (101371, 108158); and anyway, T is the initializer,
4528 : : not the target object. */
4529 : 767 : new_ctx.object = NULL_TREE;
4530 : 767 : new_ctx.ctor = build_constructor (elem_type, NULL);
4531 : 767 : ctx = &new_ctx;
4532 : : }
4533 : 3101 : t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
4534 : : overflow_p);
4535 : 3101 : if (new_ctor && t != ctx->ctor)
4536 : 734 : free_constructor (ctx->ctor);
4537 : : return t;
4538 : : }
4539 : :
4540 : : /* Subroutine of cxx_eval_constant_expression.
4541 : : Attempt to reduce a field access of a value of class type. */
4542 : :
4543 : : static tree
4544 : 30883273 : cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
4545 : : value_cat lval,
4546 : : bool *non_constant_p, bool *overflow_p)
4547 : : {
4548 : 30883273 : unsigned HOST_WIDE_INT i;
4549 : 30883273 : tree field;
4550 : 30883273 : tree value;
4551 : 30883273 : tree part = TREE_OPERAND (t, 1);
4552 : 30883273 : tree orig_whole = TREE_OPERAND (t, 0);
4553 : 30883273 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
4554 : : lval,
4555 : : non_constant_p, overflow_p);
4556 : 30883273 : if (*non_constant_p)
4557 : : return t;
4558 : 18605177 : if (INDIRECT_REF_P (whole)
4559 : 18605177 : && integer_zerop (TREE_OPERAND (whole, 0)))
4560 : : {
4561 : 191 : if (!ctx->quiet)
4562 : 39 : error ("dereferencing a null pointer in %qE", orig_whole);
4563 : 191 : *non_constant_p = true;
4564 : 191 : return t;
4565 : : }
4566 : :
4567 : 18604986 : if (TREE_CODE (whole) == PTRMEM_CST)
4568 : 1231 : whole = cplus_expand_constant (whole);
4569 : 18604986 : if (whole == orig_whole)
4570 : : return t;
4571 : 8051099 : if (lval == vc_discard)
4572 : : return t;
4573 : 8051075 : if (lval)
4574 : 5540491 : return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
4575 : : whole, part, NULL_TREE);
4576 : : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4577 : : CONSTRUCTOR. */
4578 : 2510584 : if (TREE_CODE (whole) != CONSTRUCTOR)
4579 : : {
4580 : 0 : if (!ctx->quiet)
4581 : 0 : error ("%qE is not a constant expression", orig_whole);
4582 : 0 : *non_constant_p = true;
4583 : 0 : return t;
4584 : : }
4585 : 2510328 : if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole))
4586 : 2511083 : && DECL_MUTABLE_P (part))
4587 : : {
4588 : 132 : if (!ctx->quiet)
4589 : 16 : error ("mutable %qD is not usable in a constant expression", part);
4590 : 132 : *non_constant_p = true;
4591 : 132 : return t;
4592 : : }
4593 : :
4594 : 2510452 : bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
4595 : 3609716 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
4596 : : {
4597 : : /* Use name match for PMF fields, as a variant will have a
4598 : : different FIELD_DECL with a different type. */
4599 : 3532419 : if (pmf ? DECL_NAME (field) == DECL_NAME (part)
4600 : : : field == part)
4601 : : {
4602 : 2433155 : if (value)
4603 : : {
4604 : 2433137 : STRIP_ANY_LOCATION_WRAPPER (value);
4605 : 2433137 : return value;
4606 : : }
4607 : : else
4608 : : /* We're in the middle of initializing it. */
4609 : : break;
4610 : : }
4611 : : }
4612 : 77315 : if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE)
4613 : : {
4614 : 86 : if (CONSTRUCTOR_NELTS (whole) > 0)
4615 : : {
4616 : : /* DR 1188 says we don't have to deal with this. */
4617 : 68 : if (!ctx->quiet)
4618 : : {
4619 : 13 : constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
4620 : 13 : if (cep->value == NULL_TREE)
4621 : 9 : error ("accessing uninitialized member %qD", part);
4622 : : else
4623 : 4 : error ("accessing %qD member instead of initialized %qD member "
4624 : : "in constant expression", part, cep->index);
4625 : : }
4626 : 68 : *non_constant_p = true;
4627 : 68 : return t;
4628 : : }
4629 : 18 : else if (!CONSTRUCTOR_NO_CLEARING (whole))
4630 : : {
4631 : : /* Value-initialized union, check if looking at the first member. */
4632 : 15 : tree first = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (whole)));
4633 : 15 : if (first != part)
4634 : : {
4635 : 9 : if (!ctx->quiet)
4636 : 3 : error ("accessing %qD member instead of initialized %qD "
4637 : : "member in constant expression", part, first);
4638 : 9 : *non_constant_p = true;
4639 : 9 : return t;
4640 : : }
4641 : : }
4642 : : }
4643 : :
4644 : : /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
4645 : : classes never get represented; throw together a value now. */
4646 : 77238 : if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
4647 : 1506 : return build_constructor (TREE_TYPE (t), NULL);
4648 : :
4649 : 75732 : gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
4650 : :
4651 : 75732 : if (CONSTRUCTOR_NO_CLEARING (whole))
4652 : : {
4653 : : /* 'whole' is part of the aggregate initializer we're currently
4654 : : building; if there's no initializer for this member yet, that's an
4655 : : error. */
4656 : 75431 : if (!ctx->quiet)
4657 : 17 : error ("accessing uninitialized member %qD", part);
4658 : 75431 : *non_constant_p = true;
4659 : 75431 : return t;
4660 : : }
4661 : :
4662 : : /* If there's no explicit init for this field, it's value-initialized. */
4663 : 301 : value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
4664 : 301 : return cxx_eval_constant_expression (ctx, value,
4665 : : lval,
4666 : 301 : non_constant_p, overflow_p);
4667 : : }
4668 : :
4669 : : /* Subroutine of cxx_eval_constant_expression.
4670 : : Attempt to reduce a field access of a value of class type that is
4671 : : expressed as a BIT_FIELD_REF. */
4672 : :
4673 : : static tree
4674 : 63 : cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
4675 : : value_cat lval,
4676 : : bool *non_constant_p, bool *overflow_p)
4677 : : {
4678 : 63 : tree orig_whole = TREE_OPERAND (t, 0);
4679 : 63 : tree retval, fldval, utype, mask;
4680 : 63 : bool fld_seen = false;
4681 : 63 : HOST_WIDE_INT istart, isize;
4682 : 63 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
4683 : : lval,
4684 : : non_constant_p, overflow_p);
4685 : 63 : tree start, field, value;
4686 : 63 : unsigned HOST_WIDE_INT i;
4687 : :
4688 : 63 : if (whole == orig_whole)
4689 : : return t;
4690 : : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4691 : : CONSTRUCTOR. */
4692 : 3 : if (!*non_constant_p
4693 : 3 : && TREE_CODE (whole) != VECTOR_CST
4694 : 0 : && TREE_CODE (whole) != CONSTRUCTOR)
4695 : : {
4696 : 0 : if (!ctx->quiet)
4697 : 0 : error ("%qE is not a constant expression", orig_whole);
4698 : 0 : *non_constant_p = true;
4699 : : }
4700 : 3 : if (*non_constant_p)
4701 : : return t;
4702 : :
4703 : 3 : if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
4704 : : {
4705 : 3 : if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
4706 : : TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
4707 : : return r;
4708 : 0 : if (!ctx->quiet)
4709 : 0 : error ("%qE is not a constant expression", orig_whole);
4710 : 0 : *non_constant_p = true;
4711 : 0 : return t;
4712 : : }
4713 : :
4714 : 0 : start = TREE_OPERAND (t, 2);
4715 : 0 : istart = tree_to_shwi (start);
4716 : 0 : isize = tree_to_shwi (TREE_OPERAND (t, 1));
4717 : 0 : utype = TREE_TYPE (t);
4718 : 0 : if (!TYPE_UNSIGNED (utype))
4719 : 0 : utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
4720 : 0 : retval = build_int_cst (utype, 0);
4721 : 0 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
4722 : : {
4723 : 0 : tree bitpos = bit_position (field);
4724 : 0 : STRIP_ANY_LOCATION_WRAPPER (value);
4725 : 0 : if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
4726 : 0 : return value;
4727 : 0 : if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
4728 : 0 : && TREE_CODE (value) == INTEGER_CST
4729 : 0 : && tree_fits_shwi_p (bitpos)
4730 : 0 : && tree_fits_shwi_p (DECL_SIZE (field)))
4731 : : {
4732 : 0 : HOST_WIDE_INT bit = tree_to_shwi (bitpos);
4733 : 0 : HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
4734 : 0 : HOST_WIDE_INT shift;
4735 : 0 : if (bit >= istart && bit + sz <= istart + isize)
4736 : : {
4737 : 0 : fldval = fold_convert (utype, value);
4738 : 0 : mask = build_int_cst_type (utype, -1);
4739 : 0 : mask = fold_build2 (LSHIFT_EXPR, utype, mask,
4740 : : size_int (TYPE_PRECISION (utype) - sz));
4741 : 0 : mask = fold_build2 (RSHIFT_EXPR, utype, mask,
4742 : : size_int (TYPE_PRECISION (utype) - sz));
4743 : 0 : fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
4744 : 0 : shift = bit - istart;
4745 : 0 : if (BYTES_BIG_ENDIAN)
4746 : : shift = TYPE_PRECISION (utype) - shift - sz;
4747 : 0 : fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
4748 : : size_int (shift));
4749 : 0 : retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
4750 : 0 : fld_seen = true;
4751 : : }
4752 : : }
4753 : : }
4754 : 0 : if (fld_seen)
4755 : 0 : return fold_convert (TREE_TYPE (t), retval);
4756 : 0 : gcc_unreachable ();
4757 : : return error_mark_node;
4758 : : }
4759 : :
4760 : : /* Helper for cxx_eval_bit_cast.
4761 : : Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
4762 : : types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
4763 : : is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
4764 : : data members of reference type. */
4765 : :
4766 : : static bool
4767 : 4786 : check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type,
4768 : : tree orig_type)
4769 : : {
4770 : 5193 : if (TREE_CODE (type) == UNION_TYPE)
4771 : : {
4772 : 39 : if (!ctx->quiet)
4773 : : {
4774 : 13 : if (type == orig_type)
4775 : 6 : error_at (loc, "%qs is not a constant expression because %qT is "
4776 : : "a union type", "__builtin_bit_cast", type);
4777 : : else
4778 : 7 : error_at (loc, "%qs is not a constant expression because %qT "
4779 : : "contains a union type", "__builtin_bit_cast",
4780 : : orig_type);
4781 : : }
4782 : 39 : return true;
4783 : : }
4784 : : if (TREE_CODE (type) == POINTER_TYPE)
4785 : : {
4786 : 54 : if (!ctx->quiet)
4787 : : {
4788 : 14 : if (type == orig_type)
4789 : 6 : error_at (loc, "%qs is not a constant expression because %qT is "
4790 : : "a pointer type", "__builtin_bit_cast", type);
4791 : : else
4792 : 8 : error_at (loc, "%qs is not a constant expression because %qT "
4793 : : "contains a pointer type", "__builtin_bit_cast",
4794 : : orig_type);
4795 : : }
4796 : 54 : return true;
4797 : : }
4798 : : if (TREE_CODE (type) == REFERENCE_TYPE)
4799 : : {
4800 : 0 : if (!ctx->quiet)
4801 : : {
4802 : 0 : if (type == orig_type)
4803 : 0 : error_at (loc, "%qs is not a constant expression because %qT is "
4804 : : "a reference type", "__builtin_bit_cast", type);
4805 : : else
4806 : 0 : error_at (loc, "%qs is not a constant expression because %qT "
4807 : : "contains a reference type", "__builtin_bit_cast",
4808 : : orig_type);
4809 : : }
4810 : 0 : return true;
4811 : : }
4812 : 1163 : if (TYPE_PTRMEM_P (type))
4813 : : {
4814 : 36 : if (!ctx->quiet)
4815 : : {
4816 : 12 : if (type == orig_type)
4817 : 12 : error_at (loc, "%qs is not a constant expression because %qT is "
4818 : : "a pointer to member type", "__builtin_bit_cast",
4819 : : type);
4820 : : else
4821 : 0 : error_at (loc, "%qs is not a constant expression because %qT "
4822 : : "contains a pointer to member type",
4823 : : "__builtin_bit_cast", orig_type);
4824 : : }
4825 : 36 : return true;
4826 : : }
4827 : 5064 : if (TYPE_VOLATILE (type))
4828 : : {
4829 : 0 : if (!ctx->quiet)
4830 : : {
4831 : 0 : if (type == orig_type)
4832 : 0 : error_at (loc, "%qs is not a constant expression because %qT is "
4833 : : "volatile", "__builtin_bit_cast", type);
4834 : : else
4835 : 0 : error_at (loc, "%qs is not a constant expression because %qT "
4836 : : "contains a volatile subobject",
4837 : : "__builtin_bit_cast", orig_type);
4838 : : }
4839 : 0 : return true;
4840 : : }
4841 : 5064 : if (TREE_CODE (type) == RECORD_TYPE)
4842 : 23492 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4843 : 22401 : if (TREE_CODE (field) == FIELD_DECL
4844 : 22401 : && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type))
4845 : : return true;
4846 : 5010 : if (TREE_CODE (type) == ARRAY_TYPE)
4847 : 407 : return check_bit_cast_type (ctx, loc, TREE_TYPE (type), orig_type);
4848 : : return false;
4849 : : }
4850 : :
4851 : : /* Helper function for cxx_eval_bit_cast. For unsigned char or
4852 : : std::byte members of CONSTRUCTOR (recursively) if they contain
4853 : : some indeterminate bits (as set in MASK), remove the ctor elts,
4854 : : mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
4855 : : bits in MASK. */
4856 : :
4857 : : static void
4858 : 617 : clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask)
4859 : : {
4860 : 617 : if (TREE_CODE (t) != CONSTRUCTOR)
4861 : : return;
4862 : :
4863 : : unsigned i, j = 0;
4864 : : tree index, value;
4865 : 2160 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
4866 : : {
4867 : 1543 : tree type = TREE_TYPE (value);
4868 : 1543 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
4869 : 2655 : && DECL_BIT_FIELD_TYPE (index) != NULL_TREE)
4870 : : {
4871 : 266 : if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index)))
4872 : : {
4873 : 135 : HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index));
4874 : 135 : gcc_assert (fldsz != 0);
4875 : 135 : HOST_WIDE_INT pos = int_byte_position (index);
4876 : 135 : HOST_WIDE_INT bpos
4877 : 135 : = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index));
4878 : 135 : bpos %= BITS_PER_UNIT;
4879 : 135 : HOST_WIDE_INT end
4880 : 135 : = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
4881 : 135 : gcc_assert (end == 1 || end == 2);
4882 : 135 : unsigned char *p = mask + pos;
4883 : 135 : unsigned char mask_save[2];
4884 : 135 : mask_save[0] = mask[pos];
4885 : 135 : mask_save[1] = end == 2 ? mask[pos + 1] : 0;
4886 : 135 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
4887 : : sorry_at (loc, "PDP11 bit-field handling unsupported"
4888 : : " in %qs", "__builtin_bit_cast");
4889 : 135 : else if (BYTES_BIG_ENDIAN)
4890 : : {
4891 : : /* Big endian. */
4892 : : if (bpos + fldsz <= BITS_PER_UNIT)
4893 : : *p &= ~(((1 << fldsz) - 1)
4894 : : << (BITS_PER_UNIT - bpos - fldsz));
4895 : : else
4896 : : {
4897 : : gcc_assert (bpos);
4898 : : *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
4899 : : p++;
4900 : : fldsz -= BITS_PER_UNIT - bpos;
4901 : : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
4902 : : *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
4903 : : }
4904 : : }
4905 : : else
4906 : : {
4907 : : /* Little endian. */
4908 : 135 : if (bpos + fldsz <= BITS_PER_UNIT)
4909 : 135 : *p &= ~(((1 << fldsz) - 1) << bpos);
4910 : : else
4911 : : {
4912 : 0 : gcc_assert (bpos);
4913 : 0 : *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
4914 : 0 : p++;
4915 : 0 : fldsz -= BITS_PER_UNIT - bpos;
4916 : 0 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
4917 : 0 : *p &= ~((1 << fldsz) - 1);
4918 : : }
4919 : : }
4920 : 135 : if (mask_save[0] != mask[pos]
4921 : 99 : || (end == 2 && mask_save[1] != mask[pos + 1]))
4922 : : {
4923 : 36 : CONSTRUCTOR_NO_CLEARING (t) = 1;
4924 : 36 : continue;
4925 : : }
4926 : : }
4927 : : }
4928 : 1277 : else if (is_byte_access_type_not_plain_char (type))
4929 : : {
4930 : 226 : HOST_WIDE_INT pos;
4931 : 226 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4932 : 132 : pos = tree_to_shwi (index);
4933 : : else
4934 : 94 : pos = int_byte_position (index);
4935 : 226 : if (mask[pos])
4936 : : {
4937 : 48 : CONSTRUCTOR_NO_CLEARING (t) = 1;
4938 : 48 : mask[pos] = 0;
4939 : 48 : continue;
4940 : : }
4941 : : }
4942 : 1459 : if (TREE_CODE (value) == CONSTRUCTOR)
4943 : : {
4944 : 260 : HOST_WIDE_INT pos;
4945 : 260 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4946 : 144 : pos = tree_to_shwi (index)
4947 : 72 : * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))));
4948 : : else
4949 : 188 : pos = int_byte_position (index);
4950 : 260 : clear_uchar_or_std_byte_in_mask (loc, value, mask + pos);
4951 : : }
4952 : 1459 : if (i != j)
4953 : : {
4954 : 180 : CONSTRUCTOR_ELT (t, j)->index = index;
4955 : 180 : CONSTRUCTOR_ELT (t, j)->value = value;
4956 : : }
4957 : 1459 : ++j;
4958 : : }
4959 : 1234 : if (CONSTRUCTOR_NELTS (t) != j)
4960 : 84 : vec_safe_truncate (CONSTRUCTOR_ELTS (t), j);
4961 : : }
4962 : :
4963 : : /* Subroutine of cxx_eval_constant_expression.
4964 : : Attempt to evaluate a BIT_CAST_EXPR. */
4965 : :
4966 : : static tree
4967 : 1154 : cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
4968 : : bool *overflow_p)
4969 : : {
4970 : 1154 : if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
4971 : 1154 : TREE_TYPE (t))
4972 : 3733 : || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0),
4973 : 1091 : EXPR_LOCATION (t)),
4974 : 1091 : TREE_TYPE (TREE_OPERAND (t, 0)),
4975 : 1091 : TREE_TYPE (TREE_OPERAND (t, 0))))
4976 : : {
4977 : 129 : *non_constant_p = true;
4978 : 129 : return t;
4979 : : }
4980 : :
4981 : 1025 : tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
4982 : : non_constant_p, overflow_p);
4983 : 1025 : if (*non_constant_p)
4984 : : return t;
4985 : :
4986 : 870 : location_t loc = EXPR_LOCATION (t);
4987 : 870 : if (BITS_PER_UNIT != 8 || CHAR_BIT != 8)
4988 : : {
4989 : : if (!ctx->quiet)
4990 : : sorry_at (loc, "%qs cannot be constant evaluated on the target",
4991 : : "__builtin_bit_cast");
4992 : : *non_constant_p = true;
4993 : : return t;
4994 : : }
4995 : :
4996 : 870 : if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
4997 : : {
4998 : 0 : if (!ctx->quiet)
4999 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
5000 : : "type is too large", "__builtin_bit_cast");
5001 : 0 : *non_constant_p = true;
5002 : 0 : return t;
5003 : : }
5004 : :
5005 : 870 : HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
5006 : 870 : if (len < 0 || (int) len != len)
5007 : : {
5008 : 0 : if (!ctx->quiet)
5009 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
5010 : : "type is too large", "__builtin_bit_cast");
5011 : 0 : *non_constant_p = true;
5012 : 0 : return t;
5013 : : }
5014 : :
5015 : 870 : unsigned char buf[64];
5016 : 870 : unsigned char *ptr, *mask;
5017 : 870 : size_t alen = (size_t) len * 2;
5018 : 870 : if (alen <= sizeof (buf))
5019 : : ptr = buf;
5020 : : else
5021 : 3 : ptr = XNEWVEC (unsigned char, alen);
5022 : 870 : mask = ptr + (size_t) len;
5023 : : /* At the beginning consider everything indeterminate. */
5024 : 870 : memset (mask, ~0, (size_t) len);
5025 : :
5026 : 870 : if (native_encode_initializer (op, ptr, len, 0, mask) != len)
5027 : : {
5028 : 0 : if (!ctx->quiet)
5029 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
5030 : : "argument cannot be encoded", "__builtin_bit_cast");
5031 : 0 : *non_constant_p = true;
5032 : 0 : if (ptr != buf)
5033 : 0 : XDELETE (ptr);
5034 : 0 : return t;
5035 : : }
5036 : :
5037 : 870 : tree r = NULL_TREE;
5038 : 870 : if (can_native_interpret_type_p (TREE_TYPE (t)))
5039 : : {
5040 : 513 : r = native_interpret_expr (TREE_TYPE (t), ptr, len);
5041 : 513 : if (is_byte_access_type_not_plain_char (TREE_TYPE (t)))
5042 : : {
5043 : 48 : gcc_assert (len == 1);
5044 : 48 : if (mask[0])
5045 : : {
5046 : 24 : memset (mask, 0, len);
5047 : 24 : r = build_constructor (TREE_TYPE (r), NULL);
5048 : 24 : CONSTRUCTOR_NO_CLEARING (r) = 1;
5049 : : }
5050 : : }
5051 : : }
5052 : 357 : else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
5053 : : {
5054 : 357 : r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len);
5055 : 357 : if (r != NULL_TREE)
5056 : : {
5057 : 357 : clear_type_padding_in_mask (TREE_TYPE (t), mask);
5058 : 357 : clear_uchar_or_std_byte_in_mask (loc, r, mask);
5059 : 357 : if (CHECKING_P)
5060 : : {
5061 : 357 : tree e = cxx_eval_bare_aggregate (ctx, r, vc_prvalue,
5062 : : non_constant_p, overflow_p);
5063 : 357 : gcc_checking_assert (e == r);
5064 : : r = e;
5065 : : }
5066 : : }
5067 : : }
5068 : :
5069 : 870 : if (r != NULL_TREE)
5070 : : {
5071 : 7517 : for (int i = 0; i < len; i++)
5072 : 6719 : if (mask[i])
5073 : : {
5074 : 72 : if (!ctx->quiet)
5075 : 24 : error_at (loc, "%qs accessing uninitialized byte at offset %d",
5076 : : "__builtin_bit_cast", i);
5077 : 72 : *non_constant_p = true;
5078 : 72 : r = t;
5079 : 72 : break;
5080 : : }
5081 : 870 : if (ptr != buf)
5082 : 3 : XDELETE (ptr);
5083 : 870 : return r;
5084 : : }
5085 : :
5086 : 0 : if (!ctx->quiet)
5087 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
5088 : : "argument cannot be interpreted", "__builtin_bit_cast");
5089 : 0 : *non_constant_p = true;
5090 : 0 : if (ptr != buf)
5091 : 0 : XDELETE (ptr);
5092 : : return t;
5093 : : }
5094 : :
5095 : : /* Subroutine of cxx_eval_constant_expression.
5096 : : Evaluate a short-circuited logical expression T in the context
5097 : : of a given constexpr CALL. BAILOUT_VALUE is the value for
5098 : : early return. CONTINUE_VALUE is used here purely for
5099 : : sanity check purposes. */
5100 : :
5101 : : static tree
5102 : 8699678 : cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
5103 : : tree bailout_value, tree continue_value,
5104 : : bool *non_constant_p, bool *overflow_p)
5105 : : {
5106 : 8699678 : tree r;
5107 : 8699678 : tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5108 : : vc_prvalue, non_constant_p,
5109 : : overflow_p);
5110 : 8699678 : VERIFY_CONSTANT (lhs);
5111 : 6224856 : if (tree_int_cst_equal (lhs, bailout_value))
5112 : : return lhs;
5113 : 5851799 : gcc_assert (tree_int_cst_equal (lhs, continue_value));
5114 : 5851799 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
5115 : : vc_prvalue, non_constant_p,
5116 : : overflow_p);
5117 : 5851799 : VERIFY_CONSTANT (r);
5118 : : return r;
5119 : : }
5120 : :
5121 : : /* REF is a COMPONENT_REF designating a particular field. V is a vector of
5122 : : CONSTRUCTOR elements to initialize (part of) an object containing that
5123 : : field. Return a pointer to the constructor_elt corresponding to the
5124 : : initialization of the field. */
5125 : :
5126 : : static constructor_elt *
5127 : 0 : base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
5128 : : {
5129 : 0 : tree aggr = TREE_OPERAND (ref, 0);
5130 : 0 : tree field = TREE_OPERAND (ref, 1);
5131 : 0 : HOST_WIDE_INT i;
5132 : 0 : constructor_elt *ce;
5133 : :
5134 : 0 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
5135 : :
5136 : 0 : if (TREE_CODE (aggr) == COMPONENT_REF)
5137 : : {
5138 : 0 : constructor_elt *base_ce
5139 : 0 : = base_field_constructor_elt (v, aggr);
5140 : 0 : v = CONSTRUCTOR_ELTS (base_ce->value);
5141 : : }
5142 : :
5143 : 0 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
5144 : 0 : if (ce->index == field)
5145 : 0 : return ce;
5146 : :
5147 : 0 : gcc_unreachable ();
5148 : : return NULL;
5149 : : }
5150 : :
5151 : : /* Some of the expressions fed to the constexpr mechanism are calls to
5152 : : constructors, which have type void. In that case, return the type being
5153 : : initialized by the constructor. */
5154 : :
5155 : : static tree
5156 : 347464564 : initialized_type (tree t)
5157 : : {
5158 : 348310540 : if (TYPE_P (t))
5159 : : return t;
5160 : 348310249 : tree type = TREE_TYPE (t);
5161 : 348310249 : if (TREE_CODE (t) == CALL_EXPR)
5162 : : {
5163 : : /* A constructor call has void type, so we need to look deeper. */
5164 : 37516939 : tree fn = get_function_named_in_call (t);
5165 : 37516911 : if (fn && TREE_CODE (fn) == FUNCTION_DECL
5166 : 74998293 : && DECL_CXX_CONSTRUCTOR_P (fn))
5167 : 2201424 : type = DECL_CONTEXT (fn);
5168 : : }
5169 : 310793310 : else if (TREE_CODE (t) == COMPOUND_EXPR)
5170 : 845976 : return initialized_type (TREE_OPERAND (t, 1));
5171 : 309947334 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
5172 : 399933 : type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
5173 : 347464273 : return cv_unqualified (type);
5174 : : }
5175 : :
5176 : : /* We're about to initialize element INDEX of an array or class from VALUE.
5177 : : Set up NEW_CTX appropriately by adjusting .object to refer to the
5178 : : subobject and creating a new CONSTRUCTOR if the element is itself
5179 : : a class or array. */
5180 : :
5181 : : static void
5182 : 1065370 : init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
5183 : : tree index, tree &value)
5184 : : {
5185 : 1065370 : new_ctx = *ctx;
5186 : :
5187 : 1065370 : if (index && TREE_CODE (index) != INTEGER_CST
5188 : 951587 : && TREE_CODE (index) != FIELD_DECL
5189 : 3 : && TREE_CODE (index) != RANGE_EXPR)
5190 : : /* This won't have an element in the new CONSTRUCTOR. */
5191 : : return;
5192 : :
5193 : 1065370 : tree type = initialized_type (value);
5194 : 1065370 : if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
5195 : : /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
5196 : : return;
5197 : 558356 : if (VECTOR_TYPE_P (type)
5198 : 9133 : && VECTOR_TYPE_P (TREE_TYPE (ctx->ctor))
5199 : 558410 : && index == NULL_TREE)
5200 : : /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
5201 : : vector is constructed from smaller vectors, doesn't get its own
5202 : : CONSTRUCTOR either. */
5203 : : return;
5204 : :
5205 : : /* The sub-aggregate initializer might contain a placeholder;
5206 : : update object to refer to the subobject and ctor to refer to
5207 : : the (newly created) sub-initializer. */
5208 : 558302 : if (ctx->object)
5209 : : {
5210 : 396315 : if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR)
5211 : : /* There's no well-defined subobject for this index. */
5212 : 3 : new_ctx.object = NULL_TREE;
5213 : : else
5214 : 396312 : new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
5215 : : }
5216 : :
5217 : 558302 : if (is_empty_class (type))
5218 : : /* Leave ctor null for an empty subobject, they aren't represented in the
5219 : : result of evaluation. */
5220 : 516395 : new_ctx.ctor = NULL_TREE;
5221 : : else
5222 : : {
5223 : 41907 : tree elt = build_constructor (type, NULL);
5224 : 41907 : CONSTRUCTOR_NO_CLEARING (elt) = true;
5225 : 41907 : new_ctx.ctor = elt;
5226 : : }
5227 : :
5228 : 558302 : if (TREE_CODE (value) == TARGET_EXPR)
5229 : : /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
5230 : 22611 : value = TARGET_EXPR_INITIAL (value);
5231 : : }
5232 : :
5233 : : /* We're about to process an initializer for a class or array TYPE. Make
5234 : : sure that CTX is set up appropriately. */
5235 : :
5236 : : static void
5237 : 727989 : verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
5238 : : {
5239 : : /* We don't bother building a ctor for an empty base subobject. */
5240 : 727989 : if (is_empty_class (type))
5241 : : return;
5242 : :
5243 : : /* We're in the middle of an initializer that might involve placeholders;
5244 : : our caller should have created a CONSTRUCTOR for us to put the
5245 : : initializer into. We will either return that constructor or T. */
5246 : 213069 : gcc_assert (ctx->ctor);
5247 : 213069 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
5248 : : (type, TREE_TYPE (ctx->ctor)));
5249 : : /* We used to check that ctx->ctor was empty, but that isn't the case when
5250 : : the object is zero-initialized before calling the constructor. */
5251 : 213069 : if (ctx->object)
5252 : : {
5253 : 106124 : tree otype = TREE_TYPE (ctx->object);
5254 : 106124 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
5255 : : /* Handle flexible array members. */
5256 : : || (TREE_CODE (otype) == ARRAY_TYPE
5257 : : && TYPE_DOMAIN (otype) == NULL_TREE
5258 : : && TREE_CODE (type) == ARRAY_TYPE
5259 : : && (same_type_ignoring_top_level_qualifiers_p
5260 : : (TREE_TYPE (type), TREE_TYPE (otype)))));
5261 : : }
5262 : 213069 : gcc_assert (!ctx->object || !DECL_P (ctx->object)
5263 : : || ctx->global->get_value (ctx->object) == ctx->ctor);
5264 : : }
5265 : :
5266 : : /* Subroutine of cxx_eval_constant_expression.
5267 : : The expression tree T denotes a C-style array or a C-style
5268 : : aggregate. Reduce it to a constant expression. */
5269 : :
5270 : : static tree
5271 : 727494 : cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
5272 : : value_cat lval,
5273 : : bool *non_constant_p, bool *overflow_p)
5274 : : {
5275 : 727494 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
5276 : 727494 : bool changed = false;
5277 : 727494 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
5278 : 727494 : tree type = TREE_TYPE (t);
5279 : :
5280 : 727494 : constexpr_ctx new_ctx;
5281 : 727494 : if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
5282 : : {
5283 : : /* We don't really need the ctx->ctor business for a PMF or
5284 : : vector, but it's simpler to use the same code. */
5285 : 25814 : new_ctx = *ctx;
5286 : 25814 : new_ctx.ctor = build_constructor (type, NULL);
5287 : 25814 : new_ctx.object = NULL_TREE;
5288 : 25814 : ctx = &new_ctx;
5289 : 727494 : };
5290 : 727494 : verify_ctor_sanity (ctx, type);
5291 : 727494 : vec<constructor_elt, va_gc> **p = nullptr;
5292 : 727494 : if (ctx->ctor)
5293 : : {
5294 : 727485 : p = &CONSTRUCTOR_ELTS (ctx->ctor);
5295 : 1454873 : vec_alloc (*p, vec_safe_length (v));
5296 : 727485 : if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
5297 : 15877 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
5298 : : }
5299 : :
5300 : 727494 : unsigned i;
5301 : 727494 : tree index, value;
5302 : 727494 : bool constant_p = true;
5303 : 727494 : bool side_effects_p = false;
5304 : 1744179 : FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
5305 : : {
5306 : 1059442 : tree orig_value = value;
5307 : 1059442 : init_subob_ctx (ctx, new_ctx, index, value);
5308 : : /* Like in cxx_eval_store_expression, omit entries for empty fields. */
5309 : 1059442 : bool no_slot = new_ctx.ctor == NULL_TREE;
5310 : 1059442 : int pos_hint = -1;
5311 : 1059442 : if (new_ctx.ctor != ctx->ctor && !no_slot)
5312 : : {
5313 : : /* If we built a new CONSTRUCTOR, attach it now so that other
5314 : : initializers can refer to it. */
5315 : 36121 : constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
5316 : 36121 : cep->value = new_ctx.ctor;
5317 : 36121 : pos_hint = cep - (*p)->begin();
5318 : 36121 : }
5319 : 1023321 : else if (TREE_CODE (type) == UNION_TYPE)
5320 : : /* Otherwise if we're constructing a non-aggregate union member, set
5321 : : the active union member now so that we can later detect and diagnose
5322 : : if its initializer attempts to activate another member. */
5323 : 971 : get_or_insert_ctor_field (ctx->ctor, index);
5324 : 1059442 : tree elt = cxx_eval_constant_expression (&new_ctx, value,
5325 : : lval,
5326 : : non_constant_p, overflow_p);
5327 : : /* Don't VERIFY_CONSTANT here. */
5328 : 1059442 : if (ctx->quiet && *non_constant_p)
5329 : : break;
5330 : 1016685 : if (elt != orig_value)
5331 : 104537 : changed = true;
5332 : :
5333 : 1016685 : if (!TREE_CONSTANT (elt))
5334 : 331696 : constant_p = false;
5335 : 1016685 : if (TREE_SIDE_EFFECTS (elt))
5336 : 24 : side_effects_p = true;
5337 : 1016685 : if (index && TREE_CODE (index) == COMPONENT_REF)
5338 : : {
5339 : : /* This is an initialization of a vfield inside a base
5340 : : subaggregate that we already initialized; push this
5341 : : initialization into the previous initialization. */
5342 : 0 : constructor_elt *inner = base_field_constructor_elt (*p, index);
5343 : 0 : inner->value = elt;
5344 : 0 : changed = true;
5345 : 0 : }
5346 : 1016685 : else if (no_slot)
5347 : : /* This is an initializer for an empty field; now that we've
5348 : : checked that it's constant, we can ignore it. */
5349 : : changed = true;
5350 : 500326 : else if (index
5351 : 500196 : && (TREE_CODE (index) == NOP_EXPR
5352 : 500196 : || TREE_CODE (index) == POINTER_PLUS_EXPR))
5353 : : {
5354 : : /* Old representation of empty bases. FIXME remove. */
5355 : 0 : gcc_checking_assert (false);
5356 : : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
5357 : : changed = true;
5358 : : }
5359 : : else
5360 : : {
5361 : 500326 : if (TREE_CODE (type) == UNION_TYPE
5362 : 500326 : && (*p)->last().index != index)
5363 : : /* The initializer erroneously changed the active union member that
5364 : : we're initializing. */
5365 : 8 : gcc_assert (*non_constant_p);
5366 : : else
5367 : : {
5368 : : /* The initializer might have mutated the underlying CONSTRUCTOR,
5369 : : so recompute the location of the target constructer_elt. */
5370 : 500318 : constructor_elt *cep
5371 : 500318 : = get_or_insert_ctor_field (ctx->ctor, index, pos_hint);
5372 : 500318 : cep->value = elt;
5373 : : }
5374 : :
5375 : : /* Adding or replacing an element might change the ctor's flags. */
5376 : 500326 : TREE_CONSTANT (ctx->ctor) = constant_p;
5377 : 500326 : TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
5378 : : }
5379 : : }
5380 : 727494 : if (*non_constant_p)
5381 : : return t;
5382 : 684704 : if (!changed)
5383 : : {
5384 : 113058 : if (VECTOR_TYPE_P (type))
5385 : 14169 : t = fold (t);
5386 : 113058 : return t;
5387 : : }
5388 : 571646 : t = ctx->ctor;
5389 : 571646 : if (!t)
5390 : 9 : t = build_constructor (type, NULL);
5391 : : /* We're done building this CONSTRUCTOR, so now we can interpret an
5392 : : element without an explicit initializer as value-initialized. */
5393 : 571646 : CONSTRUCTOR_NO_CLEARING (t) = false;
5394 : 571646 : TREE_CONSTANT (t) = constant_p;
5395 : 571646 : TREE_SIDE_EFFECTS (t) = side_effects_p;
5396 : 571646 : if (VECTOR_TYPE_P (type))
5397 : 1063 : t = fold (t);
5398 : : return t;
5399 : : }
5400 : :
5401 : : /* Subroutine of cxx_eval_constant_expression.
5402 : : The expression tree T is a VEC_INIT_EXPR which denotes the desired
5403 : : initialization of a non-static data member of array type. Reduce it to a
5404 : : CONSTRUCTOR.
5405 : :
5406 : : Note that apart from value-initialization (when VALUE_INIT is true),
5407 : : this is only intended to support value-initialization and the
5408 : : initializations done by defaulted constructors for classes with
5409 : : non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
5410 : : will either be NULL_TREE for the default constructor, or a COMPONENT_REF
5411 : : for the copy/move constructor. */
5412 : :
5413 : : static tree
5414 : 495 : cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
5415 : : bool value_init, value_cat lval,
5416 : : bool *non_constant_p, bool *overflow_p)
5417 : : {
5418 : 495 : tree elttype = TREE_TYPE (atype);
5419 : 495 : verify_ctor_sanity (ctx, atype);
5420 : 495 : vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
5421 : 495 : bool pre_init = false;
5422 : 495 : unsigned HOST_WIDE_INT i;
5423 : 495 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
5424 : :
5425 : 495 : if (init && TREE_CODE (init) == CONSTRUCTOR)
5426 : 0 : return cxx_eval_bare_aggregate (ctx, init, lval,
5427 : 0 : non_constant_p, overflow_p);
5428 : :
5429 : : /* For the default constructor, build up a call to the default
5430 : : constructor of the element type. We only need to handle class types
5431 : : here, as for a constructor to be constexpr, all members must be
5432 : : initialized, which for a defaulted default constructor means they must
5433 : : be of a class type with a constexpr default constructor. */
5434 : 495 : if (TREE_CODE (elttype) == ARRAY_TYPE)
5435 : : /* We only do this at the lowest level. */;
5436 : 456 : else if (value_init)
5437 : : {
5438 : 122 : init = build_value_init (elttype, complain);
5439 : 122 : pre_init = true;
5440 : : }
5441 : 334 : else if (!init)
5442 : : {
5443 : 285 : releasing_vec argvec;
5444 : 285 : init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
5445 : : &argvec, elttype, LOOKUP_NORMAL,
5446 : : complain);
5447 : 285 : init = build_aggr_init_expr (elttype, init);
5448 : 285 : pre_init = true;
5449 : 285 : }
5450 : :
5451 : 495 : bool zeroed_out = false;
5452 : 495 : if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor))
5453 : : {
5454 : : /* We're initializing an array object that had been zero-initialized
5455 : : earlier. Truncate ctx->ctor, and propagate its zeroed state by
5456 : : clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
5457 : : initializers we append to it. */
5458 : 148 : gcc_checking_assert (initializer_zerop (ctx->ctor));
5459 : 148 : zeroed_out = true;
5460 : 148 : vec_safe_truncate (*p, 0);
5461 : : }
5462 : :
5463 : 495 : tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
5464 : : overflow_p);
5465 : 495 : unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
5466 : 6119 : for (i = 0; i < max; ++i)
5467 : : {
5468 : 5928 : tree idx = build_int_cst (size_type_node, i);
5469 : 5928 : tree eltinit;
5470 : 5928 : bool reuse = false;
5471 : 5928 : constexpr_ctx new_ctx;
5472 : 6219 : init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
5473 : 5928 : bool no_slot = new_ctx.ctor == NULL_TREE;
5474 : 5928 : if (new_ctx.ctor != ctx->ctor && !no_slot)
5475 : : {
5476 : 5786 : if (zeroed_out)
5477 : 5437 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false;
5478 : 5786 : CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
5479 : : }
5480 : 5928 : if (TREE_CODE (elttype) == ARRAY_TYPE)
5481 : : {
5482 : : /* A multidimensional array; recurse. */
5483 : 171 : if (value_init || init == NULL_TREE)
5484 : : {
5485 : 163 : eltinit = NULL_TREE;
5486 : 163 : reuse = i == 0;
5487 : : }
5488 : : else
5489 : 8 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
5490 : 171 : eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
5491 : : lval,
5492 : : non_constant_p, overflow_p);
5493 : : }
5494 : 5757 : else if (pre_init)
5495 : : {
5496 : : /* Initializing an element using value or default initialization
5497 : : we just pre-built above. */
5498 : 5637 : if (init == void_node)
5499 : : /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
5500 : 3 : return ctx->ctor;
5501 : 5634 : eltinit = cxx_eval_constant_expression (&new_ctx, init, lval,
5502 : : non_constant_p, overflow_p);
5503 : 5634 : reuse = i == 0;
5504 : : }
5505 : : else
5506 : : {
5507 : : /* Copying an element. */
5508 : 120 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
5509 : 120 : if (!lvalue_p (init))
5510 : 95 : eltinit = move (eltinit);
5511 : 120 : eltinit = (perform_implicit_conversion_flags
5512 : 120 : (elttype, eltinit, complain,
5513 : : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING));
5514 : 120 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
5515 : : non_constant_p, overflow_p);
5516 : : }
5517 : 5925 : if (*non_constant_p)
5518 : : break;
5519 : 5862 : if (no_slot)
5520 : : {
5521 : : /* This is an initializer for an empty subobject; now that we've
5522 : : checked that it's constant, we can ignore it. */
5523 : 15 : gcc_checking_assert (i == 0);
5524 : : break;
5525 : : }
5526 : 5847 : else if (new_ctx.ctor != ctx->ctor)
5527 : : {
5528 : : /* We appended this element above; update the value. */
5529 : 5739 : gcc_assert ((*p)->last().index == idx);
5530 : 5739 : (*p)->last().value = eltinit;
5531 : : }
5532 : : else
5533 : 108 : CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
5534 : : /* Reuse the result of cxx_eval_constant_expression call
5535 : : from the first iteration to all others if it is a constant
5536 : : initializer that doesn't require relocations. */
5537 : 5847 : if (reuse
5538 : 5847 : && max > 1
5539 : 5847 : && (eltinit == NULL_TREE
5540 : 371 : || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
5541 : 371 : == null_pointer_node)))
5542 : : {
5543 : 223 : if (new_ctx.ctor != ctx->ctor)
5544 : 121 : eltinit = new_ctx.ctor;
5545 : 223 : tree range = build2 (RANGE_EXPR, size_type_node,
5546 : 223 : build_int_cst (size_type_node, 1),
5547 : 223 : build_int_cst (size_type_node, max - 1));
5548 : 223 : CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
5549 : 223 : break;
5550 : : }
5551 : 5624 : else if (i == 0)
5552 : 188 : vec_safe_reserve (*p, max);
5553 : : }
5554 : :
5555 : 492 : if (!*non_constant_p)
5556 : : {
5557 : 429 : init = ctx->ctor;
5558 : 429 : CONSTRUCTOR_NO_CLEARING (init) = false;
5559 : : }
5560 : 492 : return init;
5561 : : }
5562 : :
5563 : : static tree
5564 : 393 : cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
5565 : : value_cat lval,
5566 : : bool *non_constant_p, bool *overflow_p)
5567 : : {
5568 : 393 : tree atype = TREE_TYPE (t);
5569 : 393 : tree init = VEC_INIT_EXPR_INIT (t);
5570 : 393 : bool value_init = VEC_INIT_EXPR_VALUE_INIT (t);
5571 : 393 : if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init))
5572 : : ;
5573 : 76 : else if (CONSTRUCTOR_NELTS (init) == 0
5574 : 76 : && !CP_AGGREGATE_TYPE_P (strip_array_types (atype)))
5575 : : {
5576 : : /* Handle {} as value-init. */
5577 : : init = NULL_TREE;
5578 : : value_init = true;
5579 : : }
5580 : : else
5581 : : {
5582 : : /* This is a more complicated case, like needing to loop over trailing
5583 : : elements; call build_vec_init and evaluate the result. */
5584 : 69 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
5585 : 69 : constexpr_ctx new_ctx = *ctx;
5586 : 69 : if (!ctx->object)
5587 : : {
5588 : : /* We want to have an initialization target for an VEC_INIT_EXPR.
5589 : : If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */
5590 : 42 : new_ctx.object = VEC_INIT_EXPR_SLOT (t);
5591 : 42 : tree ctor = new_ctx.ctor = build_constructor (atype, NULL);
5592 : 42 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
5593 : 42 : ctx->global->put_value (new_ctx.object, ctor);
5594 : 42 : ctx = &new_ctx;
5595 : : }
5596 : 69 : init = expand_vec_init_expr (ctx->object, t, complain);
5597 : 69 : return cxx_eval_constant_expression (ctx, init, lval, non_constant_p,
5598 : : overflow_p);
5599 : : }
5600 : 324 : tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init,
5601 : : lval, non_constant_p, overflow_p);
5602 : 324 : if (*non_constant_p)
5603 : : return t;
5604 : : else
5605 : 280 : return r;
5606 : : }
5607 : :
5608 : : /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
5609 : : where the desired type is an array of unknown bounds because the variable
5610 : : has had its bounds deduced since the wrapping expression was created. */
5611 : :
5612 : : static bool
5613 : 136980194 : same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
5614 : : {
5615 : 136980194 : while (TREE_CODE (type1) == ARRAY_TYPE
5616 : 13332 : && TREE_CODE (type2) == ARRAY_TYPE
5617 : 136980200 : && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
5618 : : {
5619 : 3 : type1 = TREE_TYPE (type1);
5620 : 3 : type2 = TREE_TYPE (type2);
5621 : : }
5622 : 136980194 : return same_type_ignoring_top_level_qualifiers_p (type1, type2);
5623 : : }
5624 : :
5625 : : /* Try to determine the currently active union member for an expression
5626 : : with UNION_TYPE. If it can be determined, return the FIELD_DECL,
5627 : : otherwise return NULL_TREE. */
5628 : :
5629 : : static tree
5630 : 75756 : cxx_union_active_member (const constexpr_ctx *ctx, tree t)
5631 : : {
5632 : 75756 : constexpr_ctx new_ctx = *ctx;
5633 : 75756 : new_ctx.quiet = true;
5634 : 75756 : bool non_constant_p = false, overflow_p = false;
5635 : 75756 : tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue,
5636 : : &non_constant_p,
5637 : : &overflow_p);
5638 : 75756 : if (TREE_CODE (ctor) == CONSTRUCTOR
5639 : 638 : && CONSTRUCTOR_NELTS (ctor) == 1
5640 : 635 : && CONSTRUCTOR_ELT (ctor, 0)->index
5641 : 76391 : && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL)
5642 : : return CONSTRUCTOR_ELT (ctor, 0)->index;
5643 : : return NULL_TREE;
5644 : : }
5645 : :
5646 : : /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
5647 : :
5648 : : static tree
5649 : 3746621 : cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
5650 : : tree op, unsigned HOST_WIDE_INT off, bool *empty_base)
5651 : : {
5652 : 4867901 : tree optype = TREE_TYPE (op);
5653 : 4867901 : unsigned HOST_WIDE_INT const_nunits;
5654 : 4867901 : if (off == 0 && similar_type_p (optype, type))
5655 : : return op;
5656 : 3739424 : else if (TREE_CODE (optype) == COMPLEX_TYPE
5657 : 3739424 : && similar_type_p (type, TREE_TYPE (optype)))
5658 : : {
5659 : : /* *(foo *)&complexfoo => __real__ complexfoo */
5660 : 0 : if (off == 0)
5661 : 0 : return build1_loc (loc, REALPART_EXPR, type, op);
5662 : : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
5663 : 0 : else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
5664 : 0 : return build1_loc (loc, IMAGPART_EXPR, type, op);
5665 : : }
5666 : : /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
5667 : 3739424 : else if (VECTOR_TYPE_P (optype)
5668 : 0 : && similar_type_p (type, TREE_TYPE (optype))
5669 : 3739424 : && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
5670 : : {
5671 : 0 : unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
5672 : 0 : unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
5673 : 0 : if (off < max_offset && off % part_width == 0)
5674 : : {
5675 : 0 : tree index = bitsize_int (off * BITS_PER_UNIT);
5676 : 0 : return build3_loc (loc, BIT_FIELD_REF, type, op,
5677 : 0 : TYPE_SIZE (type), index);
5678 : : }
5679 : : }
5680 : : /* ((foo *)&fooarray)[x] => fooarray[x] */
5681 : 3739424 : else if (TREE_CODE (optype) == ARRAY_TYPE
5682 : 1121280 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
5683 : 4860704 : && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
5684 : : {
5685 : 1121280 : tree type_domain = TYPE_DOMAIN (optype);
5686 : 1121280 : tree min_val = size_zero_node;
5687 : 1121280 : if (type_domain && TYPE_MIN_VALUE (type_domain))
5688 : 1121270 : min_val = TYPE_MIN_VALUE (type_domain);
5689 : 1121280 : unsigned HOST_WIDE_INT el_sz
5690 : 1121280 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
5691 : 1121280 : unsigned HOST_WIDE_INT idx = off / el_sz;
5692 : 1121280 : unsigned HOST_WIDE_INT rem = off % el_sz;
5693 : 1121280 : if (tree_fits_uhwi_p (min_val))
5694 : : {
5695 : 1121280 : tree index = size_int (idx + tree_to_uhwi (min_val));
5696 : 1121280 : op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
5697 : : NULL_TREE, NULL_TREE);
5698 : 1121280 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem,
5699 : 1121280 : empty_base);
5700 : : }
5701 : : }
5702 : : /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
5703 : 2618144 : else if (TREE_CODE (optype) == RECORD_TYPE
5704 : 2618144 : || TREE_CODE (optype) == UNION_TYPE)
5705 : : {
5706 : 2615321 : if (TREE_CODE (optype) == UNION_TYPE)
5707 : : /* For unions prefer the currently active member. */
5708 : 75756 : if (tree field = cxx_union_active_member (ctx, op))
5709 : : {
5710 : 635 : unsigned HOST_WIDE_INT el_sz
5711 : 635 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
5712 : 635 : if (off < el_sz)
5713 : : {
5714 : 635 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
5715 : : op, field, NULL_TREE);
5716 : 635 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
5717 : : off, empty_base))
5718 : : return ret;
5719 : : }
5720 : : }
5721 : :
5722 : : /* Handle conversion to "as base" type. */
5723 : 2614688 : if (CLASS_TYPE_P (optype)
5724 : 5229346 : && CLASSTYPE_AS_BASE (optype) == type)
5725 : : return op;
5726 : :
5727 : : /* Handle conversion to an empty base class, which is represented with a
5728 : : NOP_EXPR. Do this before spelunking into the non-empty subobjects,
5729 : : which is likely to be a waste of time (109678). */
5730 : 2614173 : if (is_empty_class (type)
5731 : 2520400 : && CLASS_TYPE_P (optype)
5732 : 5134573 : && lookup_base (optype, type, ba_any, NULL, tf_none, off))
5733 : : {
5734 : 1423743 : if (empty_base)
5735 : 235280 : *empty_base = true;
5736 : 1423743 : return op;
5737 : : }
5738 : :
5739 : 1190430 : for (tree field = TYPE_FIELDS (optype);
5740 : 15299328 : field; field = DECL_CHAIN (field))
5741 : 15295885 : if (TREE_CODE (field) == FIELD_DECL
5742 : 1190781 : && TREE_TYPE (field) != error_mark_node
5743 : 16486666 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
5744 : : {
5745 : 1190781 : tree pos = byte_position (field);
5746 : 1190781 : if (!tree_fits_uhwi_p (pos))
5747 : 0 : continue;
5748 : 1190781 : unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
5749 : 1190781 : unsigned HOST_WIDE_INT el_sz
5750 : 1190781 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
5751 : 1190781 : if (upos <= off && off < upos + el_sz)
5752 : : {
5753 : 1190357 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
5754 : : op, field, NULL_TREE);
5755 : 1190357 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
5756 : : off - upos,
5757 : : empty_base))
5758 : 1186987 : return ret;
5759 : : }
5760 : : }
5761 : : }
5762 : :
5763 : : return NULL_TREE;
5764 : : }
5765 : :
5766 : : /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
5767 : : match. We want to be less strict for simple *& folding; if we have a
5768 : : non-const temporary that we access through a const pointer, that should
5769 : : work. We handle this here rather than change fold_indirect_ref_1
5770 : : because we're dealing with things like ADDR_EXPR of INTEGER_CST which
5771 : : don't really make sense outside of constant expression evaluation. Also
5772 : : we want to allow folding to COMPONENT_REF, which could cause trouble
5773 : : with TBAA in fold_indirect_ref_1. */
5774 : :
5775 : : static tree
5776 : 64899894 : cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
5777 : : tree op0, bool *empty_base /* = NULL*/)
5778 : : {
5779 : 64899894 : tree sub = op0;
5780 : 64899894 : tree subtype;
5781 : :
5782 : : /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
5783 : 133752137 : while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR
5784 : 179599373 : || TREE_CODE (sub) == VIEW_CONVERT_EXPR)
5785 : : {
5786 : 48450043 : if (TREE_CODE (sub) == NOP_EXPR
5787 : 48450043 : && REINTERPRET_CAST_P (sub))
5788 : : return NULL_TREE;
5789 : 48415868 : sub = TREE_OPERAND (sub, 0);
5790 : : }
5791 : :
5792 : 64865719 : subtype = TREE_TYPE (sub);
5793 : 64865719 : if (!INDIRECT_TYPE_P (subtype))
5794 : : return NULL_TREE;
5795 : :
5796 : : /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
5797 : : the innermost component into the offset until it would make the
5798 : : offset positive, so that cxx_fold_indirect_ref_1 can identify
5799 : : more folding opportunities. */
5800 : 67421213 : auto canonicalize_obj_off = [] (tree& obj, tree& off) {
5801 : 2555629 : while (TREE_CODE (obj) == COMPONENT_REF
5802 : 4234137 : && (tree_int_cst_sign_bit (off) || integer_zerop (off)))
5803 : : {
5804 : 1759274 : tree field = TREE_OPERAND (obj, 1);
5805 : 1759274 : tree pos = byte_position (field);
5806 : 1759274 : if (integer_zerop (off) && integer_nonzerop (pos))
5807 : : /* If the offset is already 0, keep going as long as the
5808 : : component is at position 0. */
5809 : : break;
5810 : 1678508 : off = int_const_binop (PLUS_EXPR, off, pos);
5811 : 1678508 : obj = TREE_OPERAND (obj, 0);
5812 : : }
5813 : 2555629 : };
5814 : :
5815 : 64865584 : if (TREE_CODE (sub) == ADDR_EXPR)
5816 : : {
5817 : 18560541 : tree op = TREE_OPERAND (sub, 0);
5818 : 18560541 : tree optype = TREE_TYPE (op);
5819 : :
5820 : : /* *&CONST_DECL -> to the value of the const decl. */
5821 : 18560541 : if (TREE_CODE (op) == CONST_DECL)
5822 : 0 : return DECL_INITIAL (op);
5823 : : /* *&p => p; make sure to handle *&"str"[cst] here. */
5824 : 18560541 : if (similar_type_p (optype, type))
5825 : : {
5826 : 16935756 : tree fop = fold_read_from_constant_string (op);
5827 : 16935756 : if (fop)
5828 : : return fop;
5829 : : else
5830 : 16931238 : return op;
5831 : : }
5832 : : else
5833 : : {
5834 : 1624785 : tree off = integer_zero_node;
5835 : 1624785 : canonicalize_obj_off (op, off);
5836 : 1624785 : gcc_assert (integer_zerop (off));
5837 : 1624785 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op, 0, empty_base);
5838 : : }
5839 : : }
5840 : 46305043 : else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
5841 : 46305043 : && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
5842 : : {
5843 : 1021436 : tree op00 = TREE_OPERAND (sub, 0);
5844 : 1021436 : tree off = TREE_OPERAND (sub, 1);
5845 : :
5846 : 1021436 : STRIP_NOPS (op00);
5847 : 1021436 : if (TREE_CODE (op00) == ADDR_EXPR)
5848 : : {
5849 : 930844 : tree obj = TREE_OPERAND (op00, 0);
5850 : 930844 : canonicalize_obj_off (obj, off);
5851 : 930844 : return cxx_fold_indirect_ref_1 (ctx, loc, type, obj,
5852 : : tree_to_uhwi (off), empty_base);
5853 : : }
5854 : : }
5855 : : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
5856 : 45283607 : else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
5857 : 45283607 : && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
5858 : : {
5859 : 349 : tree type_domain;
5860 : 349 : tree min_val = size_zero_node;
5861 : 349 : tree newsub
5862 : 349 : = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL);
5863 : 349 : if (newsub)
5864 : : sub = newsub;
5865 : : else
5866 : 349 : sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
5867 : 349 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
5868 : 349 : if (type_domain && TYPE_MIN_VALUE (type_domain))
5869 : 349 : min_val = TYPE_MIN_VALUE (type_domain);
5870 : 349 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
5871 : 349 : NULL_TREE);
5872 : : }
5873 : :
5874 : : return NULL_TREE;
5875 : : }
5876 : :
5877 : : static tree
5878 : 30710104 : cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
5879 : : value_cat lval,
5880 : : bool *non_constant_p, bool *overflow_p)
5881 : : {
5882 : 30710104 : tree orig_op0 = TREE_OPERAND (t, 0);
5883 : 30710104 : bool empty_base = false;
5884 : :
5885 : : /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
5886 : : operand is an integer-zero. Otherwise reject the MEM_REF for now. */
5887 : :
5888 : 30710104 : if (TREE_CODE (t) == MEM_REF
5889 : 30710104 : && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
5890 : : {
5891 : 10 : gcc_assert (ctx->quiet);
5892 : 10 : *non_constant_p = true;
5893 : 10 : return t;
5894 : : }
5895 : :
5896 : : /* First try to simplify it directly. */
5897 : 30710094 : tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
5898 : : orig_op0, &empty_base);
5899 : 30710094 : if (!r)
5900 : : {
5901 : : /* If that didn't work, evaluate the operand first. */
5902 : 30149846 : tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
5903 : : vc_prvalue, non_constant_p,
5904 : : overflow_p);
5905 : : /* Don't VERIFY_CONSTANT here. */
5906 : 30149846 : if (*non_constant_p)
5907 : : return t;
5908 : :
5909 : 28733930 : if (!lval && integer_zerop (op0))
5910 : : {
5911 : 70 : if (!ctx->quiet)
5912 : 12 : error ("dereferencing a null pointer");
5913 : 70 : *non_constant_p = true;
5914 : 70 : return t;
5915 : : }
5916 : :
5917 : 28733860 : r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0,
5918 : : &empty_base);
5919 : 28733860 : if (r == NULL_TREE)
5920 : : {
5921 : : /* We couldn't fold to a constant value. Make sure it's not
5922 : : something we should have been able to fold. */
5923 : 14915030 : tree sub = op0;
5924 : 14915030 : STRIP_NOPS (sub);
5925 : 14915030 : if (TREE_CODE (sub) == ADDR_EXPR)
5926 : : {
5927 : 1366 : gcc_assert (!similar_type_p
5928 : : (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
5929 : : /* DR 1188 says we don't have to deal with this. */
5930 : 1366 : if (!ctx->quiet)
5931 : 3 : error_at (cp_expr_loc_or_input_loc (t),
5932 : : "accessing value of %qE through a %qT glvalue in a "
5933 : : "constant expression", build_fold_indirect_ref (sub),
5934 : 3 : TREE_TYPE (t));
5935 : 1366 : *non_constant_p = true;
5936 : 1366 : return t;
5937 : : }
5938 : :
5939 : 14913664 : if (lval == vc_glvalue && op0 != orig_op0)
5940 : 1763452 : return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
5941 : 13150212 : if (!lval)
5942 : 7754288 : VERIFY_CONSTANT (t);
5943 : 13150212 : return t;
5944 : : }
5945 : : }
5946 : :
5947 : 14379078 : r = cxx_eval_constant_expression (ctx, r,
5948 : : lval, non_constant_p, overflow_p);
5949 : 14379078 : if (*non_constant_p)
5950 : : return t;
5951 : :
5952 : : /* If we're pulling out the value of an empty base, just return an empty
5953 : : CONSTRUCTOR. */
5954 : 10208926 : if (empty_base && !lval)
5955 : : {
5956 : 13839 : r = build_constructor (TREE_TYPE (t), NULL);
5957 : 13839 : TREE_CONSTANT (r) = true;
5958 : : }
5959 : :
5960 : : return r;
5961 : : }
5962 : :
5963 : : /* Complain about R, a DECL that is accessed outside its lifetime. */
5964 : :
5965 : : static void
5966 : 30 : outside_lifetime_error (location_t loc, tree r)
5967 : : {
5968 : 30 : auto_diagnostic_group d;
5969 : 30 : if (DECL_NAME (r) == heap_deleted_identifier)
5970 : : {
5971 : : /* Provide a more accurate message for deleted variables. */
5972 : 2 : error_at (loc, "use of allocated storage after deallocation "
5973 : : "in a constant expression");
5974 : 2 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
5975 : : }
5976 : : else
5977 : : {
5978 : 28 : error_at (loc, "accessing %qE outside its lifetime", r);
5979 : 28 : inform (DECL_SOURCE_LOCATION (r), "declared here");
5980 : : }
5981 : 30 : }
5982 : :
5983 : : /* Complain about R, a VAR_DECL, not being usable in a constant expression.
5984 : : FUNDEF_P is true if we're checking a constexpr function body.
5985 : : Shared between potential_constant_expression and
5986 : : cxx_eval_constant_expression. */
5987 : :
5988 : : static void
5989 : 224 : non_const_var_error (location_t loc, tree r, bool fundef_p)
5990 : : {
5991 : 224 : auto_diagnostic_group d;
5992 : 224 : tree type = TREE_TYPE (r);
5993 : 224 : if (DECL_NAME (r) == heap_uninit_identifier
5994 : 224 : || DECL_NAME (r) == heap_identifier
5995 : 223 : || DECL_NAME (r) == heap_vec_uninit_identifier
5996 : 447 : || DECL_NAME (r) == heap_vec_identifier)
5997 : : {
5998 : 1 : if (constexpr_error (loc, fundef_p, "the content of uninitialized "
5999 : : "storage is not usable in a constant expression"))
6000 : 1 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
6001 : 1 : return;
6002 : : }
6003 : 223 : if (DECL_NAME (r) == heap_deleted_identifier)
6004 : : {
6005 : 0 : if (constexpr_error (loc, fundef_p, "use of allocated storage after "
6006 : : "deallocation in a constant expression"))
6007 : 0 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
6008 : 0 : return;
6009 : : }
6010 : 223 : if (!constexpr_error (loc, fundef_p, "the value of %qD is not usable in "
6011 : : "a constant expression", r))
6012 : : return;
6013 : : /* Avoid error cascade. */
6014 : 223 : if (DECL_INITIAL (r) == error_mark_node)
6015 : : return;
6016 : 211 : if (DECL_DECLARED_CONSTEXPR_P (r))
6017 : 3 : inform (DECL_SOURCE_LOCATION (r),
6018 : : "%qD used in its own initializer", r);
6019 : 208 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6020 : : {
6021 : 160 : if (!CP_TYPE_CONST_P (type))
6022 : 127 : inform (DECL_SOURCE_LOCATION (r),
6023 : : "%q#D is not const", r);
6024 : 33 : else if (CP_TYPE_VOLATILE_P (type))
6025 : 0 : inform (DECL_SOURCE_LOCATION (r),
6026 : : "%q#D is volatile", r);
6027 : 33 : else if (!DECL_INITIAL (r)
6028 : 11 : || !TREE_CONSTANT (DECL_INITIAL (r))
6029 : 43 : || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
6030 : 33 : inform (DECL_SOURCE_LOCATION (r),
6031 : : "%qD was not initialized with a constant "
6032 : : "expression", r);
6033 : : else
6034 : 0 : gcc_unreachable ();
6035 : : }
6036 : 48 : else if (TYPE_REF_P (type))
6037 : 9 : inform (DECL_SOURCE_LOCATION (r),
6038 : : "%qD was not initialized with a constant "
6039 : : "expression", r);
6040 : : else
6041 : : {
6042 : 39 : if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
6043 : 39 : inform (DECL_SOURCE_LOCATION (r),
6044 : : "%qD was not declared %<constexpr%>", r);
6045 : : else
6046 : 0 : inform (DECL_SOURCE_LOCATION (r),
6047 : : "%qD does not have integral or enumeration type",
6048 : : r);
6049 : : }
6050 : 224 : }
6051 : :
6052 : : /* Subroutine of cxx_eval_constant_expression.
6053 : : Like cxx_eval_unary_expression, except for trinary expressions. */
6054 : :
6055 : : static tree
6056 : 18 : cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
6057 : : value_cat lval,
6058 : : bool *non_constant_p, bool *overflow_p)
6059 : : {
6060 : 18 : int i;
6061 : 18 : tree args[3];
6062 : 18 : tree val;
6063 : :
6064 : 45 : for (i = 0; i < 3; i++)
6065 : : {
6066 : 36 : args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
6067 : : lval,
6068 : : non_constant_p, overflow_p);
6069 : 36 : VERIFY_CONSTANT (args[i]);
6070 : : }
6071 : :
6072 : 9 : val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
6073 : : args[0], args[1], args[2]);
6074 : 9 : if (val == NULL_TREE)
6075 : : return t;
6076 : 9 : VERIFY_CONSTANT (val);
6077 : : return val;
6078 : : }
6079 : :
6080 : : /* True if T was declared in a function declared to be constexpr, and
6081 : : therefore potentially constant in C++14. */
6082 : :
6083 : : bool
6084 : 73864968 : var_in_constexpr_fn (tree t)
6085 : : {
6086 : 73864968 : tree ctx = DECL_CONTEXT (t);
6087 : 73864968 : return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
6088 : 140310783 : && DECL_DECLARED_CONSTEXPR_P (ctx));
6089 : : }
6090 : :
6091 : : /* True if a function might be constexpr: either a function that was
6092 : : declared constexpr, or a C++17 lambda op(). */
6093 : :
6094 : : bool
6095 : 338992996 : maybe_constexpr_fn (tree t)
6096 : : {
6097 : 338992996 : return (DECL_DECLARED_CONSTEXPR_P (t)
6098 : 142680396 : || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t))
6099 : 480312754 : || (flag_implicit_constexpr
6100 : 23 : && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t))));
6101 : : }
6102 : :
6103 : : /* True if T was declared in a function that might be constexpr: either a
6104 : : function that was declared constexpr, or a C++17 lambda op(). */
6105 : :
6106 : : bool
6107 : 57239019 : var_in_maybe_constexpr_fn (tree t)
6108 : : {
6109 : 114477339 : return (DECL_FUNCTION_SCOPE_P (t)
6110 : 108531700 : && maybe_constexpr_fn (DECL_CONTEXT (t)));
6111 : : }
6112 : :
6113 : : /* We're assigning INIT to TARGET. In do_build_copy_constructor and
6114 : : build_over_call we implement trivial copy of a class with tail padding using
6115 : : assignment of character arrays, which is valid in normal code, but not in
6116 : : constexpr evaluation. We don't need to worry about clobbering tail padding
6117 : : in constexpr evaluation, so strip the type punning. */
6118 : :
6119 : : static void
6120 : 33578175 : maybe_simplify_trivial_copy (tree &target, tree &init)
6121 : : {
6122 : 33578175 : if (TREE_CODE (target) == MEM_REF
6123 : 695 : && TREE_CODE (init) == MEM_REF
6124 : 695 : && TREE_TYPE (target) == TREE_TYPE (init)
6125 : 695 : && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
6126 : 33578870 : && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
6127 : : {
6128 : 695 : target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
6129 : 695 : init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
6130 : : }
6131 : 33578175 : }
6132 : :
6133 : : /* Returns true if REF, which is a COMPONENT_REF, has any fields
6134 : : of constant type. This does not check for 'mutable', so the
6135 : : caller is expected to be mindful of that. */
6136 : :
6137 : : static bool
6138 : 277 : cref_has_const_field (tree ref)
6139 : : {
6140 : 346 : while (TREE_CODE (ref) == COMPONENT_REF)
6141 : : {
6142 : 337 : if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
6143 : : return true;
6144 : 69 : ref = TREE_OPERAND (ref, 0);
6145 : : }
6146 : : return false;
6147 : : }
6148 : :
6149 : : /* Return true if we are modifying something that is const during constant
6150 : : expression evaluation. CODE is the code of the statement, OBJ is the
6151 : : object in question, MUTABLE_P is true if one of the subobjects were
6152 : : declared mutable. */
6153 : :
6154 : : static bool
6155 : 27280582 : modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
6156 : : {
6157 : : /* If this is initialization, there's no problem. */
6158 : 27280582 : if (code != MODIFY_EXPR)
6159 : : return false;
6160 : :
6161 : : /* [basic.type.qualifier] "A const object is an object of type
6162 : : const T or a non-mutable subobject of a const object." */
6163 : 6438334 : if (mutable_p)
6164 : : return false;
6165 : :
6166 : 6437244 : if (TREE_READONLY (obj))
6167 : : return true;
6168 : :
6169 : 6436019 : if (CP_TYPE_CONST_P (TREE_TYPE (obj)))
6170 : : {
6171 : : /* Although a COMPONENT_REF may have a const type, we should
6172 : : only consider it modifying a const object when any of the
6173 : : field components is const. This can happen when using
6174 : : constructs such as const_cast<const T &>(m), making something
6175 : : const even though it wasn't declared const. */
6176 : 2240 : if (TREE_CODE (obj) == COMPONENT_REF)
6177 : 277 : return cref_has_const_field (obj);
6178 : : else
6179 : : return true;
6180 : : }
6181 : :
6182 : : return false;
6183 : : }
6184 : :
6185 : : /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
6186 : :
6187 : : static tree
6188 : 36559425 : cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
6189 : : value_cat lval,
6190 : : bool *non_constant_p, bool *overflow_p)
6191 : : {
6192 : 36559425 : constexpr_ctx new_ctx = *ctx;
6193 : :
6194 : 36559425 : tree init = TREE_OPERAND (t, 1);
6195 : :
6196 : 36559425 : if (TREE_CLOBBER_P (init)
6197 : 36559425 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END)
6198 : : /* Only handle clobbers ending the lifetime of objects. */
6199 : 2981250 : return void_node;
6200 : :
6201 : : /* First we figure out where we're storing to. */
6202 : 33578175 : tree target = TREE_OPERAND (t, 0);
6203 : :
6204 : 33578175 : maybe_simplify_trivial_copy (target, init);
6205 : :
6206 : 33578175 : tree type = TREE_TYPE (target);
6207 : 33578175 : bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR;
6208 : 25316531 : if (preeval && !TREE_CLOBBER_P (init))
6209 : : {
6210 : : /* Evaluate the value to be stored without knowing what object it will be
6211 : : stored in, so that any side-effects happen first. */
6212 : 25305702 : if (!SCALAR_TYPE_P (type))
6213 : 10931 : new_ctx.ctor = new_ctx.object = NULL_TREE;
6214 : 25305702 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
6215 : : non_constant_p, overflow_p);
6216 : 25305702 : if (*non_constant_p)
6217 : : return t;
6218 : : }
6219 : :
6220 : 24538446 : bool evaluated = false;
6221 : 24538446 : if (lval == vc_glvalue)
6222 : : {
6223 : : /* If we want to return a reference to the target, we need to evaluate it
6224 : : as a whole; otherwise, only evaluate the innermost piece to avoid
6225 : : building up unnecessary *_REFs. */
6226 : 0 : target = cxx_eval_constant_expression (ctx, target, lval,
6227 : : non_constant_p, overflow_p);
6228 : 0 : evaluated = true;
6229 : 0 : if (*non_constant_p)
6230 : : return t;
6231 : : }
6232 : :
6233 : : /* Find the underlying variable. */
6234 : 24538446 : releasing_vec refs;
6235 : 24538446 : tree object = NULL_TREE;
6236 : : /* If we're modifying a const object, save it. */
6237 : 24538446 : tree const_object_being_modified = NULL_TREE;
6238 : 24538446 : bool mutable_p = false;
6239 : 81275502 : for (tree probe = target; object == NULL_TREE; )
6240 : : {
6241 : 56737116 : switch (TREE_CODE (probe))
6242 : : {
6243 : 7660310 : case BIT_FIELD_REF:
6244 : 7660310 : case COMPONENT_REF:
6245 : 7660310 : case ARRAY_REF:
6246 : 7660310 : {
6247 : 7660310 : tree ob = TREE_OPERAND (probe, 0);
6248 : 7660310 : tree elt = TREE_OPERAND (probe, 1);
6249 : 7660310 : if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
6250 : : mutable_p = true;
6251 : 7660310 : if (TREE_CODE (probe) == ARRAY_REF)
6252 : : {
6253 : 864353 : elt = eval_and_check_array_index (ctx, probe, false,
6254 : : non_constant_p, overflow_p);
6255 : 864353 : if (*non_constant_p)
6256 : 54 : return t;
6257 : : }
6258 : : /* We don't check modifying_const_object_p for ARRAY_REFs. Given
6259 : : "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
6260 : : the array isn't const. Instead, check "a" in the next iteration;
6261 : : that will detect modifying "const int a[10]". */
6262 : 6795957 : else if (evaluated
6263 : 2742196 : && modifying_const_object_p (TREE_CODE (t), probe,
6264 : : mutable_p)
6265 : 6796225 : && const_object_being_modified == NULL_TREE)
6266 : : const_object_being_modified = probe;
6267 : :
6268 : : /* Track named member accesses for unions to validate modifications
6269 : : that change active member. */
6270 : 7660256 : if (!evaluated && TREE_CODE (probe) == COMPONENT_REF)
6271 : 4053761 : vec_safe_push (refs, probe);
6272 : : else
6273 : 3606495 : vec_safe_push (refs, NULL_TREE);
6274 : :
6275 : 7660256 : vec_safe_push (refs, elt);
6276 : 7660256 : vec_safe_push (refs, TREE_TYPE (probe));
6277 : 7660256 : probe = ob;
6278 : : }
6279 : 7660256 : break;
6280 : :
6281 : 4 : case REALPART_EXPR:
6282 : 4 : gcc_assert (probe == target);
6283 : 4 : vec_safe_push (refs, NULL_TREE);
6284 : 4 : vec_safe_push (refs, probe);
6285 : 4 : vec_safe_push (refs, TREE_TYPE (probe));
6286 : 4 : probe = TREE_OPERAND (probe, 0);
6287 : 4 : break;
6288 : :
6289 : 6 : case IMAGPART_EXPR:
6290 : 6 : gcc_assert (probe == target);
6291 : 6 : vec_safe_push (refs, NULL_TREE);
6292 : 6 : vec_safe_push (refs, probe);
6293 : 6 : vec_safe_push (refs, TREE_TYPE (probe));
6294 : 6 : probe = TREE_OPERAND (probe, 0);
6295 : 6 : break;
6296 : :
6297 : 49076796 : default:
6298 : 49076796 : if (evaluated)
6299 : : object = probe;
6300 : : else
6301 : : {
6302 : 24538410 : probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue,
6303 : : non_constant_p, overflow_p);
6304 : 24538410 : evaluated = true;
6305 : 24538410 : if (*non_constant_p)
6306 : : return t;
6307 : : }
6308 : : break;
6309 : : }
6310 : : }
6311 : :
6312 : 24538386 : if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
6313 : 24538386 : && const_object_being_modified == NULL_TREE)
6314 : 24538386 : const_object_being_modified = object;
6315 : :
6316 : 24538386 : if (DECL_P (object)
6317 : 23378715 : && TREE_CLOBBER_P (init)
6318 : 24549215 : && DECL_NAME (object) == heap_deleted_identifier)
6319 : : /* Ignore clobbers of deleted allocations for now; we'll get a better error
6320 : : message later when operator delete is called. */
6321 : 5 : return void_node;
6322 : :
6323 : : /* And then find/build up our initializer for the path to the subobject
6324 : : we're initializing. */
6325 : 24538381 : tree *valp;
6326 : 24538381 : if (DECL_P (object))
6327 : 23378710 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
6328 : : else
6329 : 1159671 : valp = NULL;
6330 : 24538381 : if (!valp)
6331 : : {
6332 : : /* A constant-expression cannot modify objects from outside the
6333 : : constant-expression. */
6334 : 1162352 : if (!ctx->quiet)
6335 : : {
6336 : 19 : auto_diagnostic_group d;
6337 : 19 : if (DECL_P (object) && DECL_NAME (object) == heap_deleted_identifier)
6338 : : {
6339 : 0 : error ("modification of allocated storage after deallocation "
6340 : : "is not a constant expression");
6341 : 0 : inform (DECL_SOURCE_LOCATION (object), "allocated here");
6342 : : }
6343 : 19 : else if (DECL_P (object) && ctx->global->is_outside_lifetime (object))
6344 : : {
6345 : 9 : if (TREE_CLOBBER_P (init))
6346 : 2 : error ("destroying %qE outside its lifetime", object);
6347 : : else
6348 : 7 : error ("modification of %qE outside its lifetime "
6349 : : "is not a constant expression", object);
6350 : 9 : inform (DECL_SOURCE_LOCATION (object), "declared here");
6351 : : }
6352 : : else
6353 : : {
6354 : 10 : if (TREE_CLOBBER_P (init))
6355 : 4 : error ("destroying %qE from outside current evaluation "
6356 : : "is not a constant expression", object);
6357 : : else
6358 : 6 : error ("modification of %qE from outside current evaluation "
6359 : : "is not a constant expression", object);
6360 : : }
6361 : 19 : }
6362 : 1162352 : *non_constant_p = true;
6363 : 1162352 : return t;
6364 : : }
6365 : :
6366 : : /* Handle explicit end-of-lifetime. */
6367 : 23376029 : if (TREE_CLOBBER_P (init))
6368 : : {
6369 : 10805 : if (refs->is_empty ())
6370 : 6245 : ctx->global->destroy_value (object);
6371 : 10805 : return void_node;
6372 : : }
6373 : :
6374 : 23365224 : type = TREE_TYPE (object);
6375 : 23365224 : bool no_zero_init = true;
6376 : :
6377 : 46730448 : auto_vec<tree *> ctors;
6378 : 46730448 : releasing_vec indexes;
6379 : 46730448 : auto_vec<int> index_pos_hints;
6380 : 23365224 : bool activated_union_member_p = false;
6381 : 23365224 : bool empty_base = false;
6382 : 28934325 : while (!refs->is_empty ())
6383 : : {
6384 : 5573372 : if (*valp == NULL_TREE)
6385 : : {
6386 : 702568 : *valp = build_constructor (type, NULL);
6387 : 702568 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
6388 : : }
6389 : 4870804 : else if (STRIP_ANY_LOCATION_WRAPPER (*valp),
6390 : 4870804 : TREE_CODE (*valp) == STRING_CST)
6391 : : {
6392 : : /* An array was initialized with a string constant, and now
6393 : : we're writing into one of its elements. Explode the
6394 : : single initialization into a set of element
6395 : : initializations. */
6396 : 2321 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
6397 : :
6398 : 2321 : tree string = *valp;
6399 : 2321 : tree elt_type = TREE_TYPE (type);
6400 : 2321 : unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
6401 : 2321 : / TYPE_PRECISION (char_type_node));
6402 : 2321 : unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
6403 : 2321 : tree ary_ctor = build_constructor (type, NULL);
6404 : :
6405 : 2321 : vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
6406 : 4797 : for (unsigned ix = 0; ix != num_elts; ix++)
6407 : : {
6408 : 2476 : constructor_elt elt =
6409 : : {
6410 : 2476 : build_int_cst (size_type_node, ix),
6411 : 2476 : extract_string_elt (string, chars_per_elt, ix)
6412 : 2476 : };
6413 : 2476 : CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
6414 : : }
6415 : :
6416 : 2321 : *valp = ary_ctor;
6417 : : }
6418 : :
6419 : 5573372 : enum tree_code code = TREE_CODE (type);
6420 : 5573372 : tree reftype = refs->pop();
6421 : 5573372 : tree index = refs->pop();
6422 : 5573372 : bool is_access_expr = refs->pop() != NULL_TREE;
6423 : :
6424 : 5573372 : if (code == COMPLEX_TYPE)
6425 : : {
6426 : 10 : if (TREE_CODE (*valp) == COMPLEX_CST)
6427 : 8 : *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp),
6428 : 8 : TREE_IMAGPART (*valp));
6429 : 2 : else if (TREE_CODE (*valp) == CONSTRUCTOR
6430 : 1 : && CONSTRUCTOR_NELTS (*valp) == 0
6431 : 3 : && CONSTRUCTOR_NO_CLEARING (*valp))
6432 : : {
6433 : 1 : tree r = build_constructor (reftype, NULL);
6434 : 1 : CONSTRUCTOR_NO_CLEARING (r) = 1;
6435 : 1 : *valp = build2 (COMPLEX_EXPR, type, r, r);
6436 : : }
6437 : 10 : gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR);
6438 : 10 : ctors.safe_push (valp);
6439 : 10 : vec_safe_push (indexes, index);
6440 : 10 : valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR);
6441 : 10 : gcc_checking_assert (refs->is_empty ());
6442 : : type = reftype;
6443 : 4271 : break;
6444 : : }
6445 : :
6446 : : /* If the value of object is already zero-initialized, any new ctors for
6447 : : subobjects will also be zero-initialized. */
6448 : 5573362 : no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
6449 : :
6450 : 5573362 : if (code == RECORD_TYPE && is_empty_field (index))
6451 : : /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
6452 : : have no data and might have an offset lower than previously declared
6453 : : fields, which confuses the middle-end. The code below will notice
6454 : : that we don't have a CONSTRUCTOR for our inner target and just
6455 : : return init. */
6456 : : {
6457 : : empty_base = true;
6458 : : break;
6459 : : }
6460 : :
6461 : : /* If a union is zero-initialized, its first non-static named data member
6462 : : is zero-initialized (and therefore active). */
6463 : 5569101 : if (code == UNION_TYPE
6464 : 5569101 : && !no_zero_init
6465 : 5569101 : && CONSTRUCTOR_NELTS (*valp) == 0)
6466 : 526 : if (tree first = next_aggregate_field (TYPE_FIELDS (type)))
6467 : 526 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (*valp), first, NULL_TREE);
6468 : :
6469 : : /* Check for implicit change of active member for a union. */
6470 : 5569101 : if (code == UNION_TYPE
6471 : 114965 : && (CONSTRUCTOR_NELTS (*valp) == 0
6472 : 9648 : || CONSTRUCTOR_ELT (*valp, 0)->index != index)
6473 : : /* An INIT_EXPR of the last member in an access chain is always OK,
6474 : : but still check implicit change of members earlier on; see
6475 : : cpp2a/constexpr-union6.C. */
6476 : 5675934 : && !(TREE_CODE (t) == INIT_EXPR && refs->is_empty ()))
6477 : : {
6478 : 75683 : bool has_active_member = CONSTRUCTOR_NELTS (*valp) != 0;
6479 : 75683 : tree inner = strip_array_types (reftype);
6480 : :
6481 : 75683 : if (has_active_member && cxx_dialect < cxx20)
6482 : : {
6483 : 68 : if (!ctx->quiet)
6484 : 22 : error_at (cp_expr_loc_or_input_loc (t),
6485 : : "change of the active member of a union "
6486 : : "from %qD to %qD is not a constant expression "
6487 : : "before C++20",
6488 : 22 : CONSTRUCTOR_ELT (*valp, 0)->index,
6489 : : index);
6490 : 68 : *non_constant_p = true;
6491 : : }
6492 : 75615 : else if (!is_access_expr
6493 : 75615 : || (TREE_CODE (t) == MODIFY_EXPR
6494 : 472 : && CLASS_TYPE_P (inner)
6495 : 10 : && !type_has_non_deleted_trivial_default_ctor (inner)))
6496 : : {
6497 : : /* Diagnose changing active union member after initialization
6498 : : without a valid member access expression, as described in
6499 : : [class.union.general] p5. */
6500 : 75137 : if (!ctx->quiet)
6501 : : {
6502 : 12 : auto_diagnostic_group d;
6503 : 12 : if (has_active_member)
6504 : 8 : error_at (cp_expr_loc_or_input_loc (t),
6505 : : "accessing %qD member instead of initialized "
6506 : : "%qD member in constant expression",
6507 : 8 : index, CONSTRUCTOR_ELT (*valp, 0)->index);
6508 : : else
6509 : 4 : error_at (cp_expr_loc_or_input_loc (t),
6510 : : "accessing uninitialized member %qD",
6511 : : index);
6512 : 12 : if (is_access_expr)
6513 : 1 : inform (DECL_SOURCE_LOCATION (index),
6514 : : "%qD does not implicitly begin its lifetime "
6515 : : "because %qT does not have a non-deleted "
6516 : : "trivial default constructor, use "
6517 : : "%<std::construct_at%> instead",
6518 : : index, inner);
6519 : : else
6520 : 11 : inform (DECL_SOURCE_LOCATION (index),
6521 : : "initializing %qD requires a member access "
6522 : : "expression as the left operand of the assignment",
6523 : : index);
6524 : 12 : }
6525 : 75137 : *non_constant_p = true;
6526 : : }
6527 : 478 : else if (has_active_member && CONSTRUCTOR_NO_CLEARING (*valp))
6528 : : {
6529 : : /* Diagnose changing the active union member while the union
6530 : : is in the process of being initialized. */
6531 : 9 : if (!ctx->quiet)
6532 : 3 : error_at (cp_expr_loc_or_input_loc (t),
6533 : : "change of the active member of a union "
6534 : : "from %qD to %qD during initialization",
6535 : 3 : CONSTRUCTOR_ELT (*valp, 0)->index,
6536 : : index);
6537 : 9 : *non_constant_p = true;
6538 : : }
6539 : : no_zero_init = true;
6540 : : }
6541 : :
6542 : 5569101 : ctors.safe_push (valp);
6543 : 5569101 : vec_safe_push (indexes, index);
6544 : :
6545 : 5569101 : constructor_elt *cep
6546 : 5569101 : = get_or_insert_ctor_field (*valp, index);
6547 : 5569101 : index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin());
6548 : :
6549 : 5569101 : if (code == UNION_TYPE)
6550 : 114965 : activated_union_member_p = true;
6551 : :
6552 : 5569101 : valp = &cep->value;
6553 : 5569101 : type = reftype;
6554 : : }
6555 : :
6556 : : /* For initialization of an empty base, the original target will be
6557 : : *(base*)this, evaluation of which resolves to the object
6558 : : argument, which has the derived type rather than the base type. */
6559 : 46726187 : if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p
6560 : 23360963 : (initialized_type (init), type)))
6561 : : {
6562 : 1199 : gcc_assert (is_empty_class (TREE_TYPE (target)));
6563 : : empty_base = true;
6564 : : }
6565 : :
6566 : : /* Detect modifying a constant object in constexpr evaluation.
6567 : : We have found a const object that is being modified. Figure out
6568 : : if we need to issue an error. Consider
6569 : :
6570 : : struct A {
6571 : : int n;
6572 : : constexpr A() : n(1) { n = 2; } // #1
6573 : : };
6574 : : struct B {
6575 : : const A a;
6576 : : constexpr B() { a.n = 3; } // #2
6577 : : };
6578 : : constexpr B b{};
6579 : :
6580 : : #1 is OK, since we're modifying an object under construction, but
6581 : : #2 is wrong, since "a" is const and has been fully constructed.
6582 : : To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
6583 : : which means that the object is read-only. For the example above, the
6584 : : *ctors stack at the point of #2 will look like:
6585 : :
6586 : : ctors[0] = {.a={.n=2}} TREE_READONLY = 0
6587 : : ctors[1] = {.n=2} TREE_READONLY = 1
6588 : :
6589 : : and we're modifying "b.a", so we search the stack and see if the
6590 : : constructor for "b.a" has already run. */
6591 : 23365224 : if (const_object_being_modified)
6592 : : {
6593 : 2820 : bool fail = false;
6594 : 2820 : tree const_objtype
6595 : 2820 : = strip_array_types (TREE_TYPE (const_object_being_modified));
6596 : 2820 : if (!CLASS_TYPE_P (const_objtype))
6597 : : fail = true;
6598 : : else
6599 : : {
6600 : : /* [class.ctor]p5 "A constructor can be invoked for a const,
6601 : : volatile, or const volatile object. const and volatile
6602 : : semantics are not applied on an object under construction.
6603 : : They come into effect when the constructor for the most
6604 : : derived object ends." */
6605 : 8310 : for (tree *elt : ctors)
6606 : 2882 : if (same_type_ignoring_top_level_qualifiers_p
6607 : 2882 : (TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt)))
6608 : : {
6609 : 2713 : fail = TREE_READONLY (*elt);
6610 : 2713 : break;
6611 : : }
6612 : : }
6613 : 2715 : if (fail)
6614 : : {
6615 : 198 : if (!ctx->quiet)
6616 : 63 : modifying_const_object_error (t, const_object_being_modified);
6617 : 198 : *non_constant_p = true;
6618 : 198 : return t;
6619 : : }
6620 : : }
6621 : :
6622 : 23365026 : if (!preeval)
6623 : : {
6624 : : /* We're handling an INIT_EXPR of class type, so the value of the
6625 : : initializer can depend on the object it's initializing. */
6626 : :
6627 : : /* Create a new CONSTRUCTOR in case evaluation of the initializer
6628 : : wants to modify it. */
6629 : 7606858 : if (*valp == NULL_TREE)
6630 : : {
6631 : 7544522 : *valp = build_constructor (type, NULL);
6632 : 7544522 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
6633 : : }
6634 : 7606858 : new_ctx.ctor = empty_base ? NULL_TREE : *valp;
6635 : 7606858 : new_ctx.object = target;
6636 : : /* Avoid temporary materialization when initializing from a TARGET_EXPR.
6637 : : We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
6638 : : expansion of those trees uses ctx instead. */
6639 : 7606858 : if (TREE_CODE (init) == TARGET_EXPR)
6640 : 2267975 : if (tree tinit = TARGET_EXPR_INITIAL (init))
6641 : 2267975 : init = tinit;
6642 : 7606858 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
6643 : : non_constant_p, overflow_p);
6644 : : /* The hash table might have moved since the get earlier, and the
6645 : : initializer might have mutated the underlying CONSTRUCTORs, so we must
6646 : : recompute VALP. */
6647 : 7606858 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
6648 : 8033840 : for (unsigned i = 0; i < vec_safe_length (indexes); i++)
6649 : : {
6650 : 426982 : ctors[i] = valp;
6651 : 426982 : constructor_elt *cep
6652 : 426982 : = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
6653 : 426982 : valp = &cep->value;
6654 : : }
6655 : : }
6656 : :
6657 : 23365026 : if (*non_constant_p)
6658 : : return t;
6659 : :
6660 : : /* Don't share a CONSTRUCTOR that might be changed later. */
6661 : 21076723 : init = unshare_constructor (init);
6662 : :
6663 : 21076723 : gcc_checking_assert (!*valp || (same_type_ignoring_top_level_qualifiers_p
6664 : : (TREE_TYPE (*valp), type)));
6665 : 21076723 : if (empty_base)
6666 : : {
6667 : : /* Just evaluate the initializer and return, since there's no actual data
6668 : : to store, and we didn't build a CONSTRUCTOR. */
6669 : 5186 : if (!*valp)
6670 : : {
6671 : : /* But do make sure we have something in *valp. */
6672 : 0 : *valp = build_constructor (type, nullptr);
6673 : 0 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
6674 : : }
6675 : : }
6676 : 21071537 : else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
6677 : 5398825 : && TREE_CODE (init) == CONSTRUCTOR)
6678 : : {
6679 : : /* An outer ctx->ctor might be pointing to *valp, so replace
6680 : : its contents. */
6681 : 1284861 : CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
6682 : 1284861 : TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
6683 : 1284861 : TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
6684 : 3854583 : CONSTRUCTOR_NO_CLEARING (*valp)
6685 : 1284861 : = CONSTRUCTOR_NO_CLEARING (init);
6686 : : }
6687 : : else
6688 : 19786676 : *valp = init;
6689 : :
6690 : : /* After initialization, 'const' semantics apply to the value of the
6691 : : object. Make a note of this fact by marking the CONSTRUCTOR
6692 : : TREE_READONLY. */
6693 : 21076723 : if (TREE_CODE (t) == INIT_EXPR
6694 : 15682923 : && !empty_base
6695 : 15677737 : && TREE_CODE (*valp) == CONSTRUCTOR
6696 : 22351347 : && TYPE_READONLY (type))
6697 : : {
6698 : 17385 : if (INDIRECT_REF_P (target)
6699 : 32462 : && (is_this_parameter
6700 : 15077 : (tree_strip_nop_conversions (TREE_OPERAND (target, 0)))))
6701 : : /* We've just initialized '*this' (perhaps via the target
6702 : : constructor of a delegating constructor). Leave it up to the
6703 : : caller that set 'this' to set TREE_READONLY appropriately. */
6704 : 25 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
6705 : : (TREE_TYPE (target), type) || empty_base);
6706 : : else
6707 : 17360 : TREE_READONLY (*valp) = true;
6708 : : }
6709 : :
6710 : : /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
6711 : : CONSTRUCTORs, if any. */
6712 : 21076723 : bool c = TREE_CONSTANT (init);
6713 : 21076723 : bool s = TREE_SIDE_EFFECTS (init);
6714 : 21076723 : if (!indexes->is_empty ())
6715 : : {
6716 : 3419557 : tree last = indexes->last ();
6717 : 3419557 : if (TREE_CODE (last) == REALPART_EXPR
6718 : 3419557 : || TREE_CODE (last) == IMAGPART_EXPR)
6719 : : {
6720 : : /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if
6721 : : possible. */
6722 : 10 : tree *cexpr = ctors.last ();
6723 : 10 : if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr),
6724 : 10 : TREE_OPERAND (*cexpr, 0),
6725 : 10 : TREE_OPERAND (*cexpr, 1)))
6726 : 9 : *cexpr = c;
6727 : : else
6728 : : {
6729 : 2 : TREE_CONSTANT (*cexpr)
6730 : 1 : = (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0))
6731 : 1 : & TREE_CONSTANT (TREE_OPERAND (*cexpr, 1)));
6732 : 2 : TREE_SIDE_EFFECTS (*cexpr)
6733 : 2 : = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0))
6734 : 1 : | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1)));
6735 : : }
6736 : 10 : c = TREE_CONSTANT (*cexpr);
6737 : 10 : s = TREE_SIDE_EFFECTS (*cexpr);
6738 : : }
6739 : : }
6740 : 21076723 : if (!c || s || activated_union_member_p)
6741 : 8972285 : for (tree *elt : ctors)
6742 : : {
6743 : 676495 : if (TREE_CODE (*elt) != CONSTRUCTOR)
6744 : 0 : continue;
6745 : 676495 : if (!c)
6746 : 506034 : TREE_CONSTANT (*elt) = false;
6747 : 676495 : if (s)
6748 : 0 : TREE_SIDE_EFFECTS (*elt) = true;
6749 : : /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
6750 : : this union. */
6751 : 676495 : if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE)
6752 : 39711 : CONSTRUCTOR_NO_CLEARING (*elt) = false;
6753 : : }
6754 : :
6755 : 21076723 : if (lval)
6756 : : return target;
6757 : : else
6758 : 5196565 : return init;
6759 : 24538446 : }
6760 : :
6761 : : /* Evaluate a ++ or -- expression. */
6762 : :
6763 : : static tree
6764 : 1689275 : cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
6765 : : value_cat lval,
6766 : : bool *non_constant_p, bool *overflow_p)
6767 : : {
6768 : 1689275 : enum tree_code code = TREE_CODE (t);
6769 : 1689275 : tree type = TREE_TYPE (t);
6770 : 1689275 : tree op = TREE_OPERAND (t, 0);
6771 : 1689275 : tree offset = TREE_OPERAND (t, 1);
6772 : 1689275 : gcc_assert (TREE_CONSTANT (offset));
6773 : :
6774 : : /* OFFSET is constant, but perhaps not constant enough. We need to
6775 : : e.g. bash FLOAT_EXPRs to REAL_CSTs. */
6776 : 1689275 : offset = fold_simple (offset);
6777 : :
6778 : : /* The operand as an lvalue. */
6779 : 1689275 : op = cxx_eval_constant_expression (ctx, op, vc_glvalue,
6780 : : non_constant_p, overflow_p);
6781 : :
6782 : : /* The operand as an rvalue. */
6783 : 1689275 : tree val
6784 : 1689275 : = cxx_eval_constant_expression (ctx, op, vc_prvalue,
6785 : : non_constant_p, overflow_p);
6786 : : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
6787 : : a local array in a constexpr function. */
6788 : 1689275 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
6789 : 736877 : if (!ptr)
6790 : 736877 : VERIFY_CONSTANT (val);
6791 : :
6792 : : /* The modified value. */
6793 : 1671489 : bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
6794 : 1671489 : tree mod;
6795 : 1671489 : if (INDIRECT_TYPE_P (type))
6796 : : {
6797 : : /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
6798 : 952398 : offset = convert_to_ptrofftype (offset);
6799 : 952398 : if (!inc)
6800 : 76608 : offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
6801 : 952398 : mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
6802 : : }
6803 : 719091 : else if (c_promoting_integer_type_p (type)
6804 : 2217 : && !TYPE_UNSIGNED (type)
6805 : 719154 : && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6806 : : {
6807 : 63 : offset = fold_convert (integer_type_node, offset);
6808 : 63 : mod = fold_convert (integer_type_node, val);
6809 : 119 : tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node,
6810 : : mod, offset);
6811 : 63 : mod = fold_convert (type, t);
6812 : 63 : if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t))
6813 : 3 : TREE_OVERFLOW (mod) = false;
6814 : : }
6815 : : else
6816 : 1136833 : mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
6817 : 1671489 : if (!ptr)
6818 : 719091 : VERIFY_CONSTANT (mod);
6819 : :
6820 : : /* Storing the modified value. */
6821 : 2627771 : tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
6822 : : MODIFY_EXPR, type, op, mod);
6823 : 1671489 : mod = cxx_eval_constant_expression (ctx, store, lval,
6824 : : non_constant_p, overflow_p);
6825 : 1671489 : ggc_free (store);
6826 : 1671489 : if (*non_constant_p)
6827 : : return t;
6828 : :
6829 : : /* And the value of the expression. */
6830 : 1639060 : if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
6831 : : /* Prefix ops are lvalues, but the caller might want an rvalue;
6832 : : lval has already been taken into account in the store above. */
6833 : : return mod;
6834 : : else
6835 : : /* Postfix ops are rvalues. */
6836 : 450907 : return val;
6837 : : }
6838 : :
6839 : : /* Predicates for the meaning of *jump_target. */
6840 : :
6841 : : static bool
6842 : 26858520 : returns (tree *jump_target)
6843 : : {
6844 : 26858520 : return *jump_target
6845 : 3105894 : && TREE_CODE (*jump_target) == RETURN_EXPR;
6846 : : }
6847 : :
6848 : : static bool
6849 : 27932721 : breaks (tree *jump_target)
6850 : : {
6851 : 27932721 : return *jump_target
6852 : 27932721 : && ((TREE_CODE (*jump_target) == LABEL_DECL
6853 : 11 : && LABEL_DECL_BREAK (*jump_target))
6854 : 216827 : || TREE_CODE (*jump_target) == BREAK_STMT
6855 : 210950 : || TREE_CODE (*jump_target) == EXIT_EXPR);
6856 : : }
6857 : :
6858 : : static bool
6859 : 43586905 : continues (tree *jump_target)
6860 : : {
6861 : 43586905 : return *jump_target
6862 : 43586905 : && ((TREE_CODE (*jump_target) == LABEL_DECL
6863 : 11 : && LABEL_DECL_CONTINUE (*jump_target))
6864 : 214249 : || TREE_CODE (*jump_target) == CONTINUE_STMT);
6865 : :
6866 : : }
6867 : :
6868 : : static bool
6869 : 5954824 : switches (tree *jump_target)
6870 : : {
6871 : 5954824 : return *jump_target
6872 : 5954795 : && TREE_CODE (*jump_target) == INTEGER_CST;
6873 : : }
6874 : :
6875 : : /* Subroutine of cxx_eval_statement_list. Determine whether the statement
6876 : : STMT matches *jump_target. If we're looking for a case label and we see
6877 : : the default label, note it in ctx->css_state. */
6878 : :
6879 : : static bool
6880 : 71173 : label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
6881 : : {
6882 : 71173 : switch (TREE_CODE (*jump_target))
6883 : : {
6884 : 0 : case LABEL_DECL:
6885 : 0 : if (TREE_CODE (stmt) == LABEL_EXPR
6886 : 0 : && LABEL_EXPR_LABEL (stmt) == *jump_target)
6887 : : return true;
6888 : : break;
6889 : :
6890 : 68939 : case INTEGER_CST:
6891 : 68939 : if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
6892 : : {
6893 : 68939 : gcc_assert (ctx->css_state != NULL);
6894 : 68939 : if (!CASE_LOW (stmt))
6895 : : {
6896 : : /* default: should appear just once in a SWITCH_EXPR
6897 : : body (excluding nested SWITCH_EXPR). */
6898 : 8812 : gcc_assert (*ctx->css_state != css_default_seen);
6899 : : /* When evaluating SWITCH_EXPR body for the second time,
6900 : : return true for the default: label. */
6901 : 8812 : if (*ctx->css_state == css_default_processing)
6902 : : return true;
6903 : 4430 : *ctx->css_state = css_default_seen;
6904 : : }
6905 : 60127 : else if (CASE_HIGH (stmt))
6906 : : {
6907 : 40 : if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
6908 : 62 : && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
6909 : : return true;
6910 : : }
6911 : 60087 : else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
6912 : : return true;
6913 : : }
6914 : : break;
6915 : :
6916 : : case BREAK_STMT:
6917 : : case CONTINUE_STMT:
6918 : : /* These two are handled directly in cxx_eval_loop_expr by testing
6919 : : breaks (jump_target) or continues (jump_target). */
6920 : : break;
6921 : :
6922 : 0 : default:
6923 : 0 : gcc_unreachable ();
6924 : : }
6925 : : return false;
6926 : : }
6927 : :
6928 : : /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
6929 : : semantics, for switch, break, continue, and return. */
6930 : :
6931 : : static tree
6932 : 16953601 : cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
6933 : : bool *non_constant_p, bool *overflow_p,
6934 : : tree *jump_target)
6935 : : {
6936 : 16953601 : tree local_target;
6937 : : /* In a statement-expression we want to return the last value.
6938 : : For empty statement expression return void_node. */
6939 : 16953601 : tree r = void_node;
6940 : 16953601 : if (!jump_target)
6941 : : {
6942 : 335 : local_target = NULL_TREE;
6943 : 335 : jump_target = &local_target;
6944 : : }
6945 : 35799111 : for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i)
6946 : : {
6947 : 28618256 : tree stmt = *i;
6948 : :
6949 : : /* We've found a continue, so skip everything until we reach
6950 : : the label its jumping to. */
6951 : 28618256 : if (continues (jump_target))
6952 : : {
6953 : 2234 : if (label_matches (ctx, jump_target, stmt))
6954 : : /* Found it. */
6955 : 0 : *jump_target = NULL_TREE;
6956 : : else
6957 : 2234 : continue;
6958 : : }
6959 : 28616022 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
6960 : 5890454 : continue;
6961 : :
6962 : 22725568 : value_cat lval = vc_discard;
6963 : : /* The result of a statement-expression is not wrapped in EXPR_STMT. */
6964 : 22725568 : if (tsi_one_before_end_p (i) && TREE_CODE (stmt) != EXPR_STMT)
6965 : : lval = vc_prvalue;
6966 : :
6967 : 22725568 : r = cxx_eval_constant_expression (ctx, stmt, lval,
6968 : : non_constant_p, overflow_p,
6969 : : jump_target);
6970 : 22725568 : if (*non_constant_p)
6971 : : break;
6972 : 15236806 : if (returns (jump_target) || breaks (jump_target))
6973 : : break;
6974 : : }
6975 : 16953601 : if (*jump_target && jump_target == &local_target)
6976 : : {
6977 : : /* We aren't communicating the jump to our caller, so give up. We don't
6978 : : need to support evaluation of jumps out of statement-exprs. */
6979 : 30 : if (!ctx->quiet)
6980 : 3 : error_at (cp_expr_loc_or_input_loc (r),
6981 : : "statement is not a constant expression");
6982 : 30 : *non_constant_p = true;
6983 : : }
6984 : 16953601 : return r;
6985 : : }
6986 : :
6987 : : /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
6988 : : semantics; continue semantics are covered by cxx_eval_statement_list. */
6989 : :
6990 : : static tree
6991 : 386120 : cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
6992 : : bool *non_constant_p, bool *overflow_p,
6993 : : tree *jump_target)
6994 : : {
6995 : 386120 : tree local_target;
6996 : 386120 : if (!jump_target)
6997 : : {
6998 : 17 : local_target = NULL_TREE;
6999 : 17 : jump_target = &local_target;
7000 : : }
7001 : :
7002 : 386120 : tree body, cond = NULL_TREE, expr = NULL_TREE;
7003 : 386120 : int count = 0;
7004 : 386120 : switch (TREE_CODE (t))
7005 : : {
7006 : 17 : case LOOP_EXPR:
7007 : 17 : body = LOOP_EXPR_BODY (t);
7008 : 17 : break;
7009 : 316224 : case DO_STMT:
7010 : 316224 : body = DO_BODY (t);
7011 : 316224 : cond = DO_COND (t);
7012 : 316224 : break;
7013 : 33326 : case WHILE_STMT:
7014 : 33326 : body = WHILE_BODY (t);
7015 : 33326 : cond = WHILE_COND (t);
7016 : 33326 : count = -1;
7017 : 33326 : break;
7018 : 36553 : case FOR_STMT:
7019 : 36553 : if (FOR_INIT_STMT (t))
7020 : 0 : cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard,
7021 : : non_constant_p, overflow_p, jump_target);
7022 : 36553 : if (*non_constant_p)
7023 : : return NULL_TREE;
7024 : 36553 : body = FOR_BODY (t);
7025 : 36553 : cond = FOR_COND (t);
7026 : 36553 : expr = FOR_EXPR (t);
7027 : 36553 : count = -1;
7028 : 36553 : break;
7029 : 0 : default:
7030 : 0 : gcc_unreachable ();
7031 : : }
7032 : 6327926 : do
7033 : : {
7034 : 6327926 : if (count != -1)
7035 : : {
7036 : 6258047 : if (body)
7037 : 6258047 : cxx_eval_constant_expression (ctx, body, vc_discard,
7038 : : non_constant_p, overflow_p,
7039 : : jump_target);
7040 : 6258047 : if (breaks (jump_target))
7041 : : {
7042 : 554 : *jump_target = NULL_TREE;
7043 : 554 : break;
7044 : : }
7045 : :
7046 : 6257493 : if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
7047 : 1054 : *jump_target = NULL_TREE;
7048 : :
7049 : 6257493 : if (expr)
7050 : 848713 : cxx_eval_constant_expression (ctx, expr, vc_prvalue,
7051 : : non_constant_p, overflow_p,
7052 : : jump_target);
7053 : : }
7054 : :
7055 : 6327372 : if (cond)
7056 : : {
7057 : 6326374 : tree res
7058 : 6326374 : = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
7059 : : non_constant_p, overflow_p,
7060 : : jump_target);
7061 : 6326374 : if (res)
7062 : : {
7063 : 6311707 : if (verify_constant (res, ctx->quiet, non_constant_p,
7064 : : overflow_p))
7065 : : break;
7066 : 6247518 : if (integer_zerop (res))
7067 : : break;
7068 : : }
7069 : : else
7070 : 14667 : gcc_assert (*jump_target);
7071 : : }
7072 : :
7073 : 5956685 : if (++count >= constexpr_loop_limit)
7074 : : {
7075 : 28 : if (!ctx->quiet)
7076 : 9 : error_at (cp_expr_loc_or_input_loc (t),
7077 : : "%<constexpr%> loop iteration count exceeds limit of %d "
7078 : : "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
7079 : : constexpr_loop_limit);
7080 : 28 : *non_constant_p = true;
7081 : 28 : break;
7082 : : }
7083 : : }
7084 : 11898627 : while (!returns (jump_target)
7085 : 5941970 : && !breaks (jump_target)
7086 : 5941970 : && !continues (jump_target)
7087 : 186 : && (!switches (jump_target) || count == 0)
7088 : 11913418 : && !*non_constant_p);
7089 : :
7090 : : return NULL_TREE;
7091 : : }
7092 : :
7093 : : /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
7094 : : semantics. */
7095 : :
7096 : : static tree
7097 : 7818 : cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
7098 : : bool *non_constant_p, bool *overflow_p,
7099 : : tree *jump_target)
7100 : : {
7101 : 7818 : tree cond
7102 : 7818 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
7103 : 7818 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
7104 : : non_constant_p, overflow_p);
7105 : 7818 : VERIFY_CONSTANT (cond);
7106 : 7547 : if (TREE_CODE (cond) != INTEGER_CST)
7107 : : {
7108 : : /* If the condition doesn't reduce to an INTEGER_CST it isn't a usable
7109 : : switch condition even if it's constant enough for other things
7110 : : (c++/113545). */
7111 : 6 : gcc_checking_assert (ctx->quiet);
7112 : 6 : *non_constant_p = true;
7113 : 6 : return t;
7114 : : }
7115 : :
7116 : 7541 : *jump_target = cond;
7117 : :
7118 : 7541 : tree body
7119 : 7541 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
7120 : 7541 : constexpr_ctx new_ctx = *ctx;
7121 : 7541 : constexpr_switch_state css = css_default_not_seen;
7122 : 7541 : new_ctx.css_state = &css;
7123 : 7541 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
7124 : : non_constant_p, overflow_p, jump_target);
7125 : 11953 : if (switches (jump_target) && css == css_default_seen)
7126 : : {
7127 : : /* If the SWITCH_EXPR body has default: label, process it once again,
7128 : : this time instructing label_matches to return true for default:
7129 : : label on switches (jump_target). */
7130 : 4382 : css = css_default_processing;
7131 : 4382 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
7132 : : non_constant_p, overflow_p, jump_target);
7133 : : }
7134 : 7541 : if (breaks (jump_target) || switches (jump_target))
7135 : 2258 : *jump_target = NULL_TREE;
7136 : : return NULL_TREE;
7137 : : }
7138 : :
7139 : : /* Find the object of TYPE under initialization in CTX. */
7140 : :
7141 : : static tree
7142 : 16562 : lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type)
7143 : : {
7144 : 16562 : if (!ctx)
7145 : : return NULL_TREE;
7146 : :
7147 : : /* Prefer the outermost matching object, but don't cross
7148 : : CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
7149 : 16239 : if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor))
7150 : 499 : if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type))
7151 : : return outer_ob;
7152 : :
7153 : : /* We could use ctx->object unconditionally, but using ctx->ctor when we
7154 : : can is a minor optimization. */
7155 : 16064 : if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
7156 : 296 : return ctx->ctor;
7157 : :
7158 : 15768 : if (!ctx->object)
7159 : : return NULL_TREE;
7160 : :
7161 : : /* Since an object cannot have a field of its own type, we can search outward
7162 : : from ctx->object to find the unique containing object of TYPE. */
7163 : : tree ob = ctx->object;
7164 : 15798 : while (ob)
7165 : : {
7166 : 15798 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
7167 : : break;
7168 : 225 : if (handled_component_p (ob))
7169 : 223 : ob = TREE_OPERAND (ob, 0);
7170 : : else
7171 : : ob = NULL_TREE;
7172 : : }
7173 : :
7174 : : return ob;
7175 : : }
7176 : :
7177 : : /* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is
7178 : : true, we're checking a constexpr function body. */
7179 : :
7180 : : static void
7181 : 11 : inline_asm_in_constexpr_error (location_t loc, bool fundef_p)
7182 : : {
7183 : 11 : auto_diagnostic_group d;
7184 : 11 : if (constexpr_error (loc, fundef_p, "inline assembly is not a "
7185 : : "constant expression"))
7186 : 11 : inform (loc, "only unevaluated inline assembly is allowed in a "
7187 : : "%<constexpr%> function in C++20");
7188 : 11 : }
7189 : :
7190 : : /* We're getting the constant value of DECL in a manifestly constant-evaluated
7191 : : context; maybe complain about that. */
7192 : :
7193 : : static void
7194 : 50661784 : maybe_warn_about_constant_value (location_t loc, tree decl)
7195 : : {
7196 : 50661784 : static bool explained = false;
7197 : 50661784 : if (cxx_dialect >= cxx17
7198 : 50377288 : && warn_interference_size
7199 : 50377288 : && !OPTION_SET_P (param_destruct_interfere_size)
7200 : 50377288 : && DECL_CONTEXT (decl) == std_node
7201 : 4342906 : && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
7202 : 50661788 : && (LOCATION_FILE (input_location) != main_input_filename
7203 : 3 : || module_exporting_p ())
7204 : 5 : && warning_at (loc, OPT_Winterference_size, "use of %qD", decl)
7205 : 50661789 : && !explained)
7206 : : {
7207 : 3 : explained = true;
7208 : 3 : inform (loc, "its value can vary between compiler versions or "
7209 : : "with different %<-mtune%> or %<-mcpu%> flags");
7210 : 3 : inform (loc, "if this use is part of a public ABI, change it to "
7211 : : "instead use a constant variable you define");
7212 : 3 : inform (loc, "the default value for the current CPU tuning "
7213 : : "is %d bytes", param_destruct_interfere_size);
7214 : 3 : inform (loc, "you can stabilize this value with %<--param "
7215 : : "hardware_destructive_interference_size=%d%>, or disable "
7216 : : "this warning with %<-Wno-interference-size%>",
7217 : : param_destruct_interfere_size);
7218 : : }
7219 : 50661784 : }
7220 : :
7221 : : /* For element type ELT_TYPE, return the appropriate type of the heap object
7222 : : containing such element(s). COOKIE_SIZE is NULL or the size of cookie
7223 : : in bytes. If COOKIE_SIZE is NULL, return array type
7224 : : ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
7225 : : struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
7226 : : where N is computed such that the size of the struct fits into FULL_SIZE.
7227 : : If ARG_SIZE is non-NULL, it is the first argument to the new operator.
7228 : : It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE
7229 : : will be also 0 and so it is not possible to determine the actual array
7230 : : size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant
7231 : : expression evaluation of subexpressions of ARG_SIZE. */
7232 : :
7233 : : static tree
7234 : 1051 : build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type,
7235 : : tree cookie_size, tree full_size, tree arg_size,
7236 : : bool *non_constant_p, bool *overflow_p)
7237 : : {
7238 : 1051 : gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size));
7239 : 1051 : gcc_assert (tree_fits_uhwi_p (full_size));
7240 : 1051 : unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0;
7241 : 1051 : if (arg_size)
7242 : : {
7243 : 3 : STRIP_NOPS (arg_size);
7244 : 3 : if (cookie_size)
7245 : : {
7246 : 0 : if (TREE_CODE (arg_size) != PLUS_EXPR)
7247 : : arg_size = NULL_TREE;
7248 : 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST
7249 : 0 : && tree_int_cst_equal (cookie_size,
7250 : 0 : TREE_OPERAND (arg_size, 0)))
7251 : : {
7252 : 0 : arg_size = TREE_OPERAND (arg_size, 1);
7253 : 0 : STRIP_NOPS (arg_size);
7254 : : }
7255 : 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST
7256 : 0 : && tree_int_cst_equal (cookie_size,
7257 : 0 : TREE_OPERAND (arg_size, 1)))
7258 : : {
7259 : 0 : arg_size = TREE_OPERAND (arg_size, 0);
7260 : 0 : STRIP_NOPS (arg_size);
7261 : : }
7262 : : else
7263 : : arg_size = NULL_TREE;
7264 : : }
7265 : 3 : if (arg_size && TREE_CODE (arg_size) == MULT_EXPR)
7266 : : {
7267 : 3 : tree op0 = TREE_OPERAND (arg_size, 0);
7268 : 3 : tree op1 = TREE_OPERAND (arg_size, 1);
7269 : 3 : if (integer_zerop (op0))
7270 : 3 : arg_size
7271 : 3 : = cxx_eval_constant_expression (ctx, op1, vc_prvalue,
7272 : : non_constant_p, overflow_p);
7273 : 0 : else if (integer_zerop (op1))
7274 : 0 : arg_size
7275 : 0 : = cxx_eval_constant_expression (ctx, op0, vc_prvalue,
7276 : : non_constant_p, overflow_p);
7277 : : else
7278 : : arg_size = NULL_TREE;
7279 : : }
7280 : : else
7281 : : arg_size = NULL_TREE;
7282 : : }
7283 : :
7284 : 3 : unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size);
7285 : 1051 : if (!arg_size)
7286 : : {
7287 : 1048 : unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type);
7288 : 1048 : gcc_assert (fsz >= csz);
7289 : 1048 : fsz -= csz;
7290 : 1048 : if (esz)
7291 : 1048 : fsz /= esz;
7292 : : }
7293 : 1051 : tree itype2 = build_index_type (size_int (fsz - 1));
7294 : 1051 : if (!cookie_size)
7295 : 1050 : return build_cplus_array_type (elt_type, itype2);
7296 : 1 : return build_new_constexpr_heap_type (elt_type, cookie_size, itype2);
7297 : : }
7298 : :
7299 : : /* Attempt to reduce the expression T to a constant value.
7300 : : On failure, issue diagnostic and return error_mark_node. */
7301 : : /* FIXME unify with c_fully_fold */
7302 : : /* FIXME overflow_p is too global */
7303 : :
7304 : : static tree
7305 : 1073974508 : cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
7306 : : value_cat lval,
7307 : : bool *non_constant_p, bool *overflow_p,
7308 : : tree *jump_target /* = NULL */)
7309 : : {
7310 : 1073974508 : if (jump_target && *jump_target)
7311 : : {
7312 : : /* If we are jumping, ignore all statements/expressions except those
7313 : : that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
7314 : 190624 : switch (TREE_CODE (t))
7315 : : {
7316 : : case BIND_EXPR:
7317 : : case STATEMENT_LIST:
7318 : : case LOOP_EXPR:
7319 : : case COND_EXPR:
7320 : : case IF_STMT:
7321 : : case DO_STMT:
7322 : : case WHILE_STMT:
7323 : : case FOR_STMT:
7324 : : break;
7325 : 68939 : case LABEL_EXPR:
7326 : 68939 : case CASE_LABEL_EXPR:
7327 : 68939 : if (label_matches (ctx, jump_target, t))
7328 : : /* Found it. */
7329 : 7511 : *jump_target = NULL_TREE;
7330 : 68939 : return NULL_TREE;
7331 : : default:
7332 : : return NULL_TREE;
7333 : : }
7334 : : }
7335 : 1073800100 : if (error_operand_p (t))
7336 : : {
7337 : 134 : *non_constant_p = true;
7338 : 134 : return t;
7339 : : }
7340 : :
7341 : : /* Change the input location to the currently processed expression for
7342 : : better error messages when a subexpression has no location. */
7343 : 1073799966 : location_t loc = cp_expr_loc_or_input_loc (t);
7344 : 2147578364 : iloc_sentinel sentinel (loc);
7345 : :
7346 : 1073799966 : STRIP_ANY_LOCATION_WRAPPER (t);
7347 : :
7348 : 1073799966 : if (CONSTANT_CLASS_P (t))
7349 : : {
7350 : 228554526 : if (TREE_OVERFLOW (t))
7351 : : {
7352 : 62 : if (!ctx->quiet)
7353 : 17 : permerror (input_location, "overflow in constant expression");
7354 : 62 : if (!flag_permissive || ctx->quiet)
7355 : 58 : *overflow_p = true;
7356 : : }
7357 : :
7358 : 228554526 : if (TREE_CODE (t) == INTEGER_CST
7359 : 222599701 : && TYPE_PTR_P (TREE_TYPE (t))
7360 : : /* INTEGER_CST with pointer-to-method type is only used
7361 : : for a virtual method in a pointer to member function.
7362 : : Don't reject those. */
7363 : 711556 : && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE
7364 : 229265676 : && !integer_zerop (t))
7365 : : {
7366 : 11 : if (!ctx->quiet)
7367 : 0 : error ("value %qE of type %qT is not a constant expression",
7368 : 0 : t, TREE_TYPE (t));
7369 : 11 : *non_constant_p = true;
7370 : : }
7371 : :
7372 : 228554526 : return t;
7373 : : }
7374 : :
7375 : : /* Avoid excessively long constexpr evaluations. */
7376 : 845245440 : if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
7377 : : {
7378 : 9 : if (!ctx->quiet)
7379 : 3 : error_at (loc,
7380 : : "%<constexpr%> evaluation operation count exceeds limit of "
7381 : : "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
7382 : : constexpr_ops_limit);
7383 : 9 : ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
7384 : 9 : *non_constant_p = true;
7385 : 9 : return t;
7386 : : }
7387 : :
7388 : 845245431 : constexpr_ctx new_ctx;
7389 : 845245431 : tree r = t;
7390 : :
7391 : 845245431 : tree_code tcode = TREE_CODE (t);
7392 : 845245431 : switch (tcode)
7393 : : {
7394 : 15533795 : case RESULT_DECL:
7395 : 15533795 : if (lval)
7396 : : return t;
7397 : : /* We ask for an rvalue for the RESULT_DECL when indirecting
7398 : : through an invisible reference, or in named return value
7399 : : optimization. */
7400 : 8177 : if (tree v = ctx->global->get_value (t))
7401 : : return v;
7402 : : else
7403 : : {
7404 : 0 : if (!ctx->quiet)
7405 : 0 : error ("%qE is not a constant expression", t);
7406 : 0 : *non_constant_p = true;
7407 : : }
7408 : 0 : break;
7409 : :
7410 : 107791369 : case VAR_DECL:
7411 : 107791369 : if (DECL_HAS_VALUE_EXPR_P (t))
7412 : : {
7413 : 674547 : if (is_normal_capture_proxy (t)
7414 : 674547 : && current_function_decl == DECL_CONTEXT (t))
7415 : : {
7416 : : /* Function parms aren't constexpr within the function
7417 : : definition, so don't try to look at the closure. But if the
7418 : : captured variable is constant, try to evaluate it directly. */
7419 : 236678 : r = DECL_CAPTURED_VARIABLE (t);
7420 : 236678 : tree type = TREE_TYPE (t);
7421 : 236678 : if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
7422 : : {
7423 : : /* Adjust r to match the reference-ness of t. */
7424 : 154927 : if (TYPE_REF_P (type))
7425 : 154927 : r = build_address (r);
7426 : : else
7427 : 0 : r = convert_from_reference (r);
7428 : : }
7429 : : }
7430 : : else
7431 : 437869 : r = DECL_VALUE_EXPR (t);
7432 : 674547 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
7433 : 674547 : overflow_p);
7434 : : }
7435 : : /* fall through */
7436 : 115771750 : case CONST_DECL:
7437 : : /* We used to not check lval for CONST_DECL, but darwin.cc uses
7438 : : CONST_DECL for aggregate constants. */
7439 : 115771750 : if (lval)
7440 : : return t;
7441 : 96945127 : else if (t == ctx->object)
7442 : 639595 : return ctx->ctor;
7443 : 96305532 : if (VAR_P (t))
7444 : : {
7445 : 87650604 : if (tree v = ctx->global->get_value (t))
7446 : : {
7447 : : r = v;
7448 : : break;
7449 : : }
7450 : 80614416 : if (ctx->global->is_outside_lifetime (t))
7451 : : {
7452 : 84 : if (!ctx->quiet)
7453 : 24 : outside_lifetime_error (loc, t);
7454 : 84 : *non_constant_p = true;
7455 : 84 : break;
7456 : : }
7457 : : }
7458 : 89269260 : if (ctx->manifestly_const_eval == mce_true)
7459 : 50661784 : maybe_warn_about_constant_value (loc, t);
7460 : 89269260 : if (COMPLETE_TYPE_P (TREE_TYPE (t))
7461 : 89269260 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
7462 : : {
7463 : : /* If the class is empty, we aren't actually loading anything. */
7464 : 68549 : r = build_constructor (TREE_TYPE (t), NULL);
7465 : 68549 : TREE_CONSTANT (r) = true;
7466 : : }
7467 : 89200711 : else if (ctx->strict)
7468 : 88869717 : r = decl_really_constant_value (t, /*unshare_p=*/false);
7469 : : else
7470 : 330994 : r = decl_constant_value (t, /*unshare_p=*/false);
7471 : 89258476 : if (TREE_CODE (r) == TARGET_EXPR
7472 : 89258476 : && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
7473 : 0 : r = TARGET_EXPR_INITIAL (r);
7474 : 89258476 : if (DECL_P (r)
7475 : : /* P2280 allows references to unknown. */
7476 : 89258476 : && !(VAR_P (t) && TYPE_REF_P (TREE_TYPE (t))))
7477 : : {
7478 : 23361534 : if (!ctx->quiet)
7479 : 96 : non_const_var_error (loc, r, /*fundef_p*/false);
7480 : 23361534 : *non_constant_p = true;
7481 : : }
7482 : : break;
7483 : :
7484 : : case DEBUG_BEGIN_STMT:
7485 : : /* ??? It might be nice to retain this information somehow, so
7486 : : as to be able to step into a constexpr function call. */
7487 : : /* Fall through. */
7488 : :
7489 : : case FUNCTION_DECL:
7490 : : case TEMPLATE_DECL:
7491 : : case LABEL_DECL:
7492 : : case LABEL_EXPR:
7493 : : case CASE_LABEL_EXPR:
7494 : : case PREDICT_EXPR:
7495 : : return t;
7496 : :
7497 : 96615806 : case PARM_DECL:
7498 : 96615806 : if (lval && !TYPE_REF_P (TREE_TYPE (t)))
7499 : : /* glvalue use. */;
7500 : 93220037 : else if (tree v = ctx->global->get_value (t))
7501 : : r = v;
7502 : 58431581 : else if (lval)
7503 : : /* Defer in case this is only used for its type. */;
7504 : 58431581 : else if (ctx->global->is_outside_lifetime (t))
7505 : : {
7506 : 18 : if (!ctx->quiet)
7507 : 6 : outside_lifetime_error (loc, t);
7508 : 18 : *non_constant_p = true;
7509 : 18 : break;
7510 : : }
7511 : 58431563 : else if (COMPLETE_TYPE_P (TREE_TYPE (t))
7512 : 58431563 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
7513 : : {
7514 : : /* If the class is empty, we aren't actually loading anything. */
7515 : 3834 : r = build_constructor (TREE_TYPE (t), NULL);
7516 : 3834 : TREE_CONSTANT (r) = true;
7517 : : }
7518 : 58427729 : else if (TYPE_REF_P (TREE_TYPE (t)))
7519 : : /* P2280 allows references to unknown... */;
7520 : 49933383 : else if (is_this_parameter (t))
7521 : : /* ...as well as the this pointer. */;
7522 : : else
7523 : : {
7524 : 40550344 : if (!ctx->quiet)
7525 : 105 : error ("%qE is not a constant expression", t);
7526 : 40550344 : *non_constant_p = true;
7527 : : }
7528 : : break;
7529 : :
7530 : 69344800 : case CALL_EXPR:
7531 : 69344800 : case AGGR_INIT_EXPR:
7532 : 69344800 : r = cxx_eval_call_expression (ctx, t, lval,
7533 : : non_constant_p, overflow_p);
7534 : 69344800 : break;
7535 : :
7536 : 2732053 : case DECL_EXPR:
7537 : 2732053 : {
7538 : 2732053 : r = DECL_EXPR_DECL (t);
7539 : 2732053 : if (TREE_CODE (r) == USING_DECL)
7540 : : {
7541 : 47 : r = void_node;
7542 : 47 : break;
7543 : : }
7544 : :
7545 : 2732006 : if (VAR_P (r)
7546 : 2732006 : && (TREE_STATIC (r)
7547 : 2728695 : || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r)))
7548 : : /* Allow __FUNCTION__ etc. */
7549 : 3311 : && !DECL_ARTIFICIAL (r)
7550 : 2732027 : && !decl_constant_var_p (r))
7551 : : {
7552 : 8 : if (!ctx->quiet)
7553 : : {
7554 : 2 : if (CP_DECL_THREAD_LOCAL_P (r))
7555 : 1 : error_at (loc, "control passes through definition of %qD "
7556 : : "with thread storage duration", r);
7557 : : else
7558 : 1 : error_at (loc, "control passes through definition of %qD "
7559 : : "with static storage duration", r);
7560 : : }
7561 : 8 : *non_constant_p = true;
7562 : 8 : break;
7563 : : }
7564 : :
7565 : : /* make_rtl_for_nonlocal_decl could have deferred emission of
7566 : : a local static var, but if it appears in a statement expression
7567 : : which is constant expression evaluated to e.g. just the address
7568 : : of the variable, its DECL_EXPR will never be seen during
7569 : : gimple lowering's record_vars_into as the statement expression
7570 : : will not be in the IL at all. */
7571 : 2731998 : if (VAR_P (r)
7572 : 2731998 : && TREE_STATIC (r)
7573 : 3303 : && !DECL_REALLY_EXTERN (r)
7574 : 3303 : && DECL_FUNCTION_SCOPE_P (r)
7575 : 3303 : && !var_in_maybe_constexpr_fn (r)
7576 : 2732001 : && decl_constant_var_p (r))
7577 : : {
7578 : 3 : varpool_node *node = varpool_node::get (r);
7579 : 3 : if (node == NULL || !node->definition)
7580 : 3 : rest_of_decl_compilation (r, 0, at_eof);
7581 : : }
7582 : :
7583 : 5454678 : if (AGGREGATE_TYPE_P (TREE_TYPE (r))
7584 : 5152796 : || VECTOR_TYPE_P (TREE_TYPE (r)))
7585 : : {
7586 : 311396 : new_ctx = *ctx;
7587 : 311396 : new_ctx.object = r;
7588 : 311396 : new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
7589 : 311396 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
7590 : 311396 : ctx->global->put_value (r, new_ctx.ctor);
7591 : 311396 : ctx = &new_ctx;
7592 : : }
7593 : :
7594 : 2731998 : if (tree init = DECL_INITIAL (r))
7595 : : {
7596 : 1394811 : init = cxx_eval_constant_expression (ctx, init, vc_prvalue,
7597 : : non_constant_p, overflow_p);
7598 : : /* Don't share a CONSTRUCTOR that might be changed. */
7599 : 1394811 : init = unshare_constructor (init);
7600 : : /* Remember that a constant object's constructor has already
7601 : : run. */
7602 : 2789622 : if (CLASS_TYPE_P (TREE_TYPE (r))
7603 : 1636902 : && CP_TYPE_CONST_P (TREE_TYPE (r)))
7604 : 487 : TREE_READONLY (init) = true;
7605 : 1394811 : ctx->global->put_value (r, init);
7606 : : }
7607 : 1337187 : else if (ctx == &new_ctx)
7608 : : /* We gave it a CONSTRUCTOR above. */;
7609 : : else
7610 : 1273858 : ctx->global->put_value (r, NULL_TREE);
7611 : : }
7612 : : break;
7613 : :
7614 : 7894168 : case TARGET_EXPR:
7615 : 7894168 : {
7616 : 7894168 : tree type = TREE_TYPE (t);
7617 : :
7618 : 7894168 : if (!literal_type_p (type))
7619 : : {
7620 : 15 : if (!ctx->quiet)
7621 : : {
7622 : 0 : auto_diagnostic_group d;
7623 : 0 : error ("temporary of non-literal type %qT in a "
7624 : : "constant expression", type);
7625 : 0 : explain_non_literal_class (type);
7626 : 0 : }
7627 : 15 : *non_constant_p = true;
7628 : 3133493 : break;
7629 : : }
7630 : 7894153 : gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t));
7631 : : /* Avoid evaluating a TARGET_EXPR more than once. */
7632 : 7894153 : tree slot = TARGET_EXPR_SLOT (t);
7633 : 7894153 : if (tree v = ctx->global->get_value (slot))
7634 : : {
7635 : 391 : if (lval)
7636 : 4276060 : return slot;
7637 : : r = v;
7638 : : break;
7639 : : }
7640 : 7893762 : if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
7641 : : {
7642 : : /* We're being expanded without an explicit target, so start
7643 : : initializing a new object; expansion with an explicit target
7644 : : strips the TARGET_EXPR before we get here. */
7645 : 6338632 : new_ctx = *ctx;
7646 : : /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
7647 : : any PLACEHOLDER_EXPR within the initializer that refers to the
7648 : : former object under construction. */
7649 : 6338632 : new_ctx.parent = ctx;
7650 : 6338632 : new_ctx.ctor = build_constructor (type, NULL);
7651 : 6338632 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
7652 : 6338632 : new_ctx.object = slot;
7653 : 6338632 : ctx->global->put_value (new_ctx.object, new_ctx.ctor);
7654 : 6338632 : ctx = &new_ctx;
7655 : : }
7656 : : /* Pass vc_prvalue because this indicates
7657 : : initialization of a temporary. */
7658 : 7893762 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_prvalue,
7659 : : non_constant_p, overflow_p);
7660 : 7893762 : if (*non_constant_p)
7661 : : break;
7662 : : /* If the initializer is complex, evaluate it to initialize slot. */
7663 : 4760389 : bool is_complex = target_expr_needs_replace (t);
7664 : 4760389 : if (!is_complex)
7665 : : {
7666 : 4760389 : r = unshare_constructor (r);
7667 : : /* Adjust the type of the result to the type of the temporary. */
7668 : 4760389 : r = adjust_temp_type (type, r);
7669 : 4760389 : ctx->global->put_value (slot, r);
7670 : : }
7671 : 4760389 : if (TARGET_EXPR_CLEANUP (t) && !CLEANUP_EH_ONLY (t))
7672 : 177825 : ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
7673 : 4760389 : if (ctx->save_exprs)
7674 : 1210130 : ctx->save_exprs->safe_push (slot);
7675 : 4760389 : if (lval)
7676 : : return slot;
7677 : 484615 : if (is_complex)
7678 : 0 : r = ctx->global->get_value (slot);
7679 : : }
7680 : 484615 : break;
7681 : :
7682 : 36559425 : case INIT_EXPR:
7683 : 36559425 : case MODIFY_EXPR:
7684 : 36559425 : gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
7685 : 36559425 : r = cxx_eval_store_expression (ctx, t, lval,
7686 : : non_constant_p, overflow_p);
7687 : 36559425 : break;
7688 : :
7689 : 0 : case SCOPE_REF:
7690 : 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
7691 : : lval,
7692 : : non_constant_p, overflow_p);
7693 : 0 : break;
7694 : :
7695 : 23644075 : case RETURN_EXPR:
7696 : 23644075 : if (TREE_OPERAND (t, 0) != NULL_TREE)
7697 : 23635981 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
7698 : : lval,
7699 : : non_constant_p, overflow_p);
7700 : : /* FALLTHRU */
7701 : 23647930 : case BREAK_STMT:
7702 : 23647930 : case CONTINUE_STMT:
7703 : 23647930 : if (jump_target)
7704 : 23647930 : *jump_target = t;
7705 : : else
7706 : : {
7707 : : /* Can happen with ({ return true; }) && false; passed to
7708 : : maybe_constant_value. There is nothing to jump over in this
7709 : : case, and the bug will be diagnosed later. */
7710 : 0 : gcc_assert (ctx->quiet);
7711 : 0 : *non_constant_p = true;
7712 : : }
7713 : : break;
7714 : :
7715 : 416004 : case SAVE_EXPR:
7716 : : /* Avoid evaluating a SAVE_EXPR more than once. */
7717 : 416004 : if (tree v = ctx->global->get_value (t))
7718 : : r = v;
7719 : : else
7720 : : {
7721 : 410090 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
7722 : : non_constant_p, overflow_p);
7723 : 410090 : if (*non_constant_p)
7724 : : break;
7725 : 5930 : ctx->global->put_value (t, r);
7726 : 5930 : if (ctx->save_exprs)
7727 : 5650 : ctx->save_exprs->safe_push (t);
7728 : : }
7729 : : break;
7730 : :
7731 : 0 : case TRY_CATCH_EXPR:
7732 : 0 : if (TREE_OPERAND (t, 0) == NULL_TREE)
7733 : : {
7734 : 0 : r = void_node;
7735 : 0 : break;
7736 : : }
7737 : : /* FALLTHRU */
7738 : 39692370 : case NON_LVALUE_EXPR:
7739 : 39692370 : case TRY_BLOCK:
7740 : 39692370 : case MUST_NOT_THROW_EXPR:
7741 : 39692370 : case EXPR_STMT:
7742 : 39692370 : case EH_SPEC_BLOCK:
7743 : 39692370 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
7744 : : lval,
7745 : : non_constant_p, overflow_p,
7746 : : jump_target);
7747 : 39692370 : break;
7748 : :
7749 : 27913073 : case CLEANUP_POINT_EXPR:
7750 : 27913073 : {
7751 : 27913073 : auto_vec<tree, 2> cleanups;
7752 : 27913073 : vec<tree> *prev_cleanups = ctx->global->cleanups;
7753 : 27913073 : ctx->global->cleanups = &cleanups;
7754 : :
7755 : 27913073 : auto_vec<tree, 10> save_exprs;
7756 : 27913073 : constexpr_ctx new_ctx = *ctx;
7757 : 27913073 : new_ctx.save_exprs = &save_exprs;
7758 : :
7759 : 27913073 : r = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 0),
7760 : : lval,
7761 : : non_constant_p, overflow_p,
7762 : : jump_target);
7763 : :
7764 : 27913073 : ctx->global->cleanups = prev_cleanups;
7765 : 27913073 : unsigned int i;
7766 : 27913073 : tree cleanup;
7767 : : /* Evaluate the cleanups. */
7768 : 55953412 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
7769 : 127266 : cxx_eval_constant_expression (&new_ctx, cleanup, vc_discard,
7770 : : non_constant_p, overflow_p);
7771 : :
7772 : : /* Forget SAVE_EXPRs and TARGET_EXPRs created by this
7773 : : full-expression. */
7774 : 84954999 : for (tree save_expr : save_exprs)
7775 : 1215780 : destroy_value_checked (ctx, save_expr, non_constant_p);
7776 : 27913073 : }
7777 : 27913073 : break;
7778 : :
7779 : 113 : case TRY_FINALLY_EXPR:
7780 : 113 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
7781 : : non_constant_p, overflow_p,
7782 : : jump_target);
7783 : 113 : if (!*non_constant_p)
7784 : : /* Also evaluate the cleanup. */
7785 : 106 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
7786 : : non_constant_p, overflow_p);
7787 : : break;
7788 : :
7789 : 545589 : case CLEANUP_STMT:
7790 : 545589 : r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
7791 : : non_constant_p, overflow_p,
7792 : : jump_target);
7793 : 545589 : if (!CLEANUP_EH_ONLY (t) && !*non_constant_p)
7794 : : {
7795 : 16351 : iloc_sentinel ils (loc);
7796 : : /* Also evaluate the cleanup. */
7797 : 16351 : cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), vc_discard,
7798 : : non_constant_p, overflow_p);
7799 : 16351 : }
7800 : : break;
7801 : :
7802 : : /* These differ from cxx_eval_unary_expression in that this doesn't
7803 : : check for a constant operand or result; an address can be
7804 : : constant without its operand being, and vice versa. */
7805 : 30710104 : case MEM_REF:
7806 : 30710104 : case INDIRECT_REF:
7807 : 30710104 : r = cxx_eval_indirect_ref (ctx, t, lval,
7808 : : non_constant_p, overflow_p);
7809 : 30710104 : break;
7810 : :
7811 : 28990757 : case ADDR_EXPR:
7812 : 28990757 : {
7813 : 28990757 : tree oldop = TREE_OPERAND (t, 0);
7814 : 28990757 : tree op = cxx_eval_constant_expression (ctx, oldop, vc_glvalue,
7815 : : non_constant_p, overflow_p);
7816 : : /* Don't VERIFY_CONSTANT here. */
7817 : 28990757 : if (*non_constant_p)
7818 : : return t;
7819 : 26993663 : gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
7820 : : /* This function does more aggressive folding than fold itself. */
7821 : 26993663 : r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
7822 : 26993663 : if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
7823 : : {
7824 : 18227905 : ggc_free (r);
7825 : 18227905 : return t;
7826 : : }
7827 : : break;
7828 : : }
7829 : :
7830 : 428115 : case REALPART_EXPR:
7831 : 428115 : case IMAGPART_EXPR:
7832 : 428115 : if (lval)
7833 : : {
7834 : 30 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
7835 : : non_constant_p, overflow_p);
7836 : 30 : if (r == error_mark_node)
7837 : : ;
7838 : 30 : else if (r == TREE_OPERAND (t, 0) || lval == vc_discard)
7839 : : r = t;
7840 : : else
7841 : 30 : r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
7842 : : break;
7843 : : }
7844 : : /* FALLTHRU */
7845 : 20752316 : case CONJ_EXPR:
7846 : 20752316 : case FIX_TRUNC_EXPR:
7847 : 20752316 : case FLOAT_EXPR:
7848 : 20752316 : case NEGATE_EXPR:
7849 : 20752316 : case ABS_EXPR:
7850 : 20752316 : case ABSU_EXPR:
7851 : 20752316 : case BIT_NOT_EXPR:
7852 : 20752316 : case TRUTH_NOT_EXPR:
7853 : 20752316 : case FIXED_CONVERT_EXPR:
7854 : 20752316 : r = cxx_eval_unary_expression (ctx, t, lval,
7855 : : non_constant_p, overflow_p);
7856 : 20752316 : break;
7857 : :
7858 : 8347691 : case SIZEOF_EXPR:
7859 : 8347691 : r = fold_sizeof_expr (t);
7860 : : /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
7861 : : which could lead to an infinite recursion. */
7862 : 8347691 : if (TREE_CODE (r) != SIZEOF_EXPR)
7863 : 8347691 : r = cxx_eval_constant_expression (ctx, r, lval,
7864 : : non_constant_p, overflow_p,
7865 : : jump_target);
7866 : : else
7867 : : {
7868 : 0 : *non_constant_p = true;
7869 : 0 : gcc_assert (ctx->quiet);
7870 : : }
7871 : :
7872 : : break;
7873 : :
7874 : 3152073 : case COMPOUND_EXPR:
7875 : 3152073 : {
7876 : : /* check_return_expr sometimes wraps a TARGET_EXPR in a
7877 : : COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
7878 : : introduced by build_call_a. */
7879 : 3152073 : tree op0 = TREE_OPERAND (t, 0);
7880 : 3152073 : tree op1 = TREE_OPERAND (t, 1);
7881 : 3152073 : STRIP_NOPS (op1);
7882 : 864820 : if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
7883 : 3227827 : || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
7884 : 2841936 : r = cxx_eval_constant_expression (ctx, op0,
7885 : : lval, non_constant_p, overflow_p,
7886 : : jump_target);
7887 : : else
7888 : : {
7889 : : /* Check that the LHS is constant and then discard it. */
7890 : 310137 : cxx_eval_constant_expression (ctx, op0, vc_discard,
7891 : : non_constant_p, overflow_p,
7892 : : jump_target);
7893 : 310137 : if (*non_constant_p)
7894 : : return t;
7895 : 211635 : op1 = TREE_OPERAND (t, 1);
7896 : 211635 : r = cxx_eval_constant_expression (ctx, op1,
7897 : : lval, non_constant_p, overflow_p,
7898 : : jump_target);
7899 : : }
7900 : : }
7901 : : break;
7902 : :
7903 : 48160332 : case POINTER_PLUS_EXPR:
7904 : 48160332 : case POINTER_DIFF_EXPR:
7905 : 48160332 : case PLUS_EXPR:
7906 : 48160332 : case MINUS_EXPR:
7907 : 48160332 : case MULT_EXPR:
7908 : 48160332 : case TRUNC_DIV_EXPR:
7909 : 48160332 : case CEIL_DIV_EXPR:
7910 : 48160332 : case FLOOR_DIV_EXPR:
7911 : 48160332 : case ROUND_DIV_EXPR:
7912 : 48160332 : case TRUNC_MOD_EXPR:
7913 : 48160332 : case CEIL_MOD_EXPR:
7914 : 48160332 : case ROUND_MOD_EXPR:
7915 : 48160332 : case RDIV_EXPR:
7916 : 48160332 : case EXACT_DIV_EXPR:
7917 : 48160332 : case MIN_EXPR:
7918 : 48160332 : case MAX_EXPR:
7919 : 48160332 : case LSHIFT_EXPR:
7920 : 48160332 : case RSHIFT_EXPR:
7921 : 48160332 : case LROTATE_EXPR:
7922 : 48160332 : case RROTATE_EXPR:
7923 : 48160332 : case BIT_IOR_EXPR:
7924 : 48160332 : case BIT_XOR_EXPR:
7925 : 48160332 : case BIT_AND_EXPR:
7926 : 48160332 : case TRUTH_XOR_EXPR:
7927 : 48160332 : case LT_EXPR:
7928 : 48160332 : case LE_EXPR:
7929 : 48160332 : case GT_EXPR:
7930 : 48160332 : case GE_EXPR:
7931 : 48160332 : case EQ_EXPR:
7932 : 48160332 : case NE_EXPR:
7933 : 48160332 : case SPACESHIP_EXPR:
7934 : 48160332 : case UNORDERED_EXPR:
7935 : 48160332 : case ORDERED_EXPR:
7936 : 48160332 : case UNLT_EXPR:
7937 : 48160332 : case UNLE_EXPR:
7938 : 48160332 : case UNGT_EXPR:
7939 : 48160332 : case UNGE_EXPR:
7940 : 48160332 : case UNEQ_EXPR:
7941 : 48160332 : case LTGT_EXPR:
7942 : 48160332 : case RANGE_EXPR:
7943 : 48160332 : case COMPLEX_EXPR:
7944 : 48160332 : r = cxx_eval_binary_expression (ctx, t, lval,
7945 : : non_constant_p, overflow_p);
7946 : 48160332 : break;
7947 : :
7948 : : /* fold can introduce non-IF versions of these; still treat them as
7949 : : short-circuiting. */
7950 : 5732503 : case TRUTH_AND_EXPR:
7951 : 5732503 : case TRUTH_ANDIF_EXPR:
7952 : 5732503 : r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
7953 : : boolean_true_node,
7954 : : non_constant_p, overflow_p);
7955 : 5732503 : break;
7956 : :
7957 : 2967175 : case TRUTH_OR_EXPR:
7958 : 2967175 : case TRUTH_ORIF_EXPR:
7959 : 2967175 : r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
7960 : : boolean_false_node,
7961 : : non_constant_p, overflow_p);
7962 : 2967175 : break;
7963 : :
7964 : 1618209 : case ARRAY_REF:
7965 : 1618209 : r = cxx_eval_array_reference (ctx, t, lval,
7966 : : non_constant_p, overflow_p);
7967 : 1618209 : break;
7968 : :
7969 : 30883275 : case COMPONENT_REF:
7970 : 30883275 : if (is_overloaded_fn (t))
7971 : : {
7972 : : /* We can only get here in checking mode via
7973 : : build_non_dependent_expr, because any expression that
7974 : : calls or takes the address of the function will have
7975 : : pulled a FUNCTION_DECL out of the COMPONENT_REF. */
7976 : 2 : gcc_checking_assert (ctx->quiet || errorcount);
7977 : 2 : *non_constant_p = true;
7978 : 2 : return t;
7979 : : }
7980 : 30883273 : r = cxx_eval_component_reference (ctx, t, lval,
7981 : : non_constant_p, overflow_p);
7982 : 30883273 : break;
7983 : :
7984 : 63 : case BIT_FIELD_REF:
7985 : 63 : r = cxx_eval_bit_field_ref (ctx, t, lval,
7986 : : non_constant_p, overflow_p);
7987 : 63 : break;
7988 : :
7989 : 5706638 : case COND_EXPR:
7990 : 5706638 : case IF_STMT:
7991 : 5706638 : if (jump_target && *jump_target)
7992 : : {
7993 : 2456 : tree orig_jump = *jump_target;
7994 : 2456 : tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
7995 : 4912 : ? TREE_OPERAND (t, 1) : void_node);
7996 : : /* When jumping to a label, the label might be either in the
7997 : : then or else blocks, so process then block first in skipping
7998 : : mode first, and if we are still in the skipping mode at its end,
7999 : : process the else block too. */
8000 : 2456 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
8001 : : overflow_p, jump_target);
8002 : : /* It's possible that we found the label in the then block. But
8003 : : it could have been followed by another jumping statement, e.g.
8004 : : say we're looking for case 1:
8005 : : if (cond)
8006 : : {
8007 : : // skipped statements
8008 : : case 1:; // clears up *jump_target
8009 : : return 1; // and sets it to a RETURN_EXPR
8010 : : }
8011 : : else { ... }
8012 : : in which case we need not go looking to the else block.
8013 : : (goto is not allowed in a constexpr function.) */
8014 : 2456 : if (*jump_target == orig_jump)
8015 : : {
8016 : 2396 : arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
8017 : 4792 : ? TREE_OPERAND (t, 2) : void_node);
8018 : 2396 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
8019 : : overflow_p, jump_target);
8020 : : }
8021 : : break;
8022 : : }
8023 : 5704182 : r = cxx_eval_conditional_expression (ctx, t, lval,
8024 : : non_constant_p, overflow_p,
8025 : : jump_target);
8026 : 5704182 : break;
8027 : 655 : case VEC_COND_EXPR:
8028 : 655 : r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
8029 : : overflow_p);
8030 : 655 : break;
8031 : :
8032 : 8054672 : case CONSTRUCTOR:
8033 : 8054672 : if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
8034 : : {
8035 : : /* Don't re-process a constant CONSTRUCTOR. */
8036 : 7327535 : verify_constructor_flags (t);
8037 : 7327535 : if (TREE_CONSTANT (t))
8038 : : return t;
8039 : : }
8040 : 727137 : r = cxx_eval_bare_aggregate (ctx, t, lval,
8041 : : non_constant_p, overflow_p);
8042 : 727137 : break;
8043 : :
8044 : 393 : case VEC_INIT_EXPR:
8045 : : /* We can get this in a defaulted constructor for a class with a
8046 : : non-static data member of array type. Either the initializer will
8047 : : be NULL, meaning default-initialization, or it will be an lvalue
8048 : : or xvalue of the same type, meaning direct-initialization from the
8049 : : corresponding member. */
8050 : 393 : r = cxx_eval_vec_init (ctx, t, lval,
8051 : : non_constant_p, overflow_p);
8052 : 393 : break;
8053 : :
8054 : 18 : case VEC_PERM_EXPR:
8055 : 18 : r = cxx_eval_trinary_expression (ctx, t, lval,
8056 : : non_constant_p, overflow_p);
8057 : 18 : break;
8058 : :
8059 : 3 : case PAREN_EXPR:
8060 : 3 : gcc_assert (!REF_PARENTHESIZED_P (t));
8061 : : /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
8062 : : constant expressions since it's unaffected by -fassociative-math. */
8063 : 3 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
8064 : : non_constant_p, overflow_p);
8065 : 3 : break;
8066 : :
8067 : 152111131 : case NOP_EXPR:
8068 : 152111131 : if (REINTERPRET_CAST_P (t))
8069 : : {
8070 : 34185 : if (!ctx->quiet)
8071 : 8 : error_at (loc,
8072 : : "%<reinterpret_cast%> is not a constant expression");
8073 : 34185 : *non_constant_p = true;
8074 : 34185 : return t;
8075 : : }
8076 : : /* FALLTHROUGH. */
8077 : 182419178 : case CONVERT_EXPR:
8078 : 182419178 : case VIEW_CONVERT_EXPR:
8079 : 182419178 : case UNARY_PLUS_EXPR:
8080 : 182419178 : {
8081 : 182419178 : tree oldop = TREE_OPERAND (t, 0);
8082 : :
8083 : 182419178 : tree op = cxx_eval_constant_expression (ctx, oldop,
8084 : : lval,
8085 : : non_constant_p, overflow_p);
8086 : 182408394 : if (*non_constant_p)
8087 : : return t;
8088 : 152759010 : tree type = TREE_TYPE (t);
8089 : :
8090 : 152759010 : if (VOID_TYPE_P (type))
8091 : 7458658 : return void_node;
8092 : :
8093 : 145300352 : if (TREE_CODE (t) == CONVERT_EXPR
8094 : 15566459 : && ARITHMETIC_TYPE_P (type)
8095 : 2101731 : && INDIRECT_TYPE_P (TREE_TYPE (op))
8096 : 145344396 : && ctx->manifestly_const_eval == mce_true)
8097 : : {
8098 : 225 : if (!ctx->quiet)
8099 : 45 : error_at (loc,
8100 : : "conversion from pointer type %qT to arithmetic type "
8101 : 45 : "%qT in a constant expression", TREE_TYPE (op), type);
8102 : 225 : *non_constant_p = true;
8103 : 225 : return t;
8104 : : }
8105 : :
8106 : : /* [expr.const]: a conversion from type cv void* to a pointer-to-object
8107 : : type cannot be part of a core constant expression as a resolution to
8108 : : DR 1312. */
8109 : 53996303 : if (TYPE_PTROB_P (type)
8110 : 52892305 : && TYPE_PTR_P (TREE_TYPE (op))
8111 : 37294911 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
8112 : : /* Inside a call to std::construct_at,
8113 : : std::allocator<T>::{,de}allocate, or
8114 : : std::source_location::current, we permit casting from void*
8115 : : because that is compiler-generated code. */
8116 : 20823 : && !is_std_construct_at (ctx->call)
8117 : 1597 : && !is_std_allocator_allocate (ctx->call)
8118 : 145301187 : && !is_std_source_location_current (ctx->call))
8119 : : {
8120 : : /* Likewise, don't error when casting from void* when OP is
8121 : : &heap uninit and similar. */
8122 : 975 : tree sop = tree_strip_nop_conversions (op);
8123 : 975 : tree decl = NULL_TREE;
8124 : 975 : if (TREE_CODE (sop) == ADDR_EXPR)
8125 : 835 : decl = TREE_OPERAND (sop, 0);
8126 : 835 : if (decl
8127 : 835 : && VAR_P (decl)
8128 : 829 : && DECL_ARTIFICIAL (decl)
8129 : 1630 : && (DECL_NAME (decl) == heap_identifier
8130 : 620 : || DECL_NAME (decl) == heap_uninit_identifier
8131 : 340 : || DECL_NAME (decl) == heap_vec_identifier
8132 : 252 : || DECL_NAME (decl) == heap_vec_uninit_identifier))
8133 : : /* OK */;
8134 : : /* P2738 (C++26): a conversion from a prvalue P of type "pointer to
8135 : : cv void" to a pointer-to-object type T unless P points to an
8136 : : object whose type is similar to T. */
8137 : 198 : else if (cxx_dialect > cxx23)
8138 : : {
8139 : 12 : r = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), sop);
8140 : 12 : if (r)
8141 : : {
8142 : 3 : r = build1 (ADDR_EXPR, type, r);
8143 : 3 : break;
8144 : : }
8145 : 9 : if (!ctx->quiet)
8146 : : {
8147 : 3 : if (TREE_CODE (sop) == ADDR_EXPR)
8148 : : {
8149 : 0 : auto_diagnostic_group d;
8150 : 0 : error_at (loc, "cast from %qT is not allowed in a "
8151 : : "constant expression because "
8152 : : "pointed-to type %qT is not similar to %qT",
8153 : 0 : TREE_TYPE (op), TREE_TYPE (TREE_TYPE (sop)),
8154 : 0 : TREE_TYPE (type));
8155 : 0 : tree obj = build_fold_indirect_ref (sop);
8156 : 0 : inform (DECL_SOURCE_LOCATION (obj),
8157 : : "pointed-to object declared here");
8158 : 0 : }
8159 : : else
8160 : : {
8161 : 3 : gcc_assert (integer_zerop (sop));
8162 : 3 : error_at (loc, "cast from %qT is not allowed in a "
8163 : : "constant expression because "
8164 : : "%qE does not point to an object",
8165 : 3 : TREE_TYPE (op), oldop);
8166 : : }
8167 : : }
8168 : 9 : *non_constant_p = true;
8169 : 9 : return t;
8170 : : }
8171 : : else
8172 : : {
8173 : 186 : if (!ctx->quiet)
8174 : 30 : error_at (loc, "cast from %qT is not allowed in a "
8175 : : "constant expression before C++26",
8176 : 30 : TREE_TYPE (op));
8177 : 186 : *non_constant_p = true;
8178 : 186 : return t;
8179 : : }
8180 : : }
8181 : :
8182 : 145299929 : if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
8183 : : {
8184 : 1694 : op = cplus_expand_constant (op);
8185 : 1694 : if (TREE_CODE (op) == PTRMEM_CST)
8186 : : {
8187 : 21 : if (!ctx->quiet)
8188 : 3 : error_at (loc, "%qE is not a constant expression when the "
8189 : : "class %qT is still incomplete", op,
8190 : 3 : PTRMEM_CST_CLASS (op));
8191 : 21 : *non_constant_p = true;
8192 : 21 : return t;
8193 : : }
8194 : : }
8195 : :
8196 : 145299908 : if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
8197 : : {
8198 : 150 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
8199 : 150 : && !can_convert_qual (type, op))
8200 : 7 : op = cplus_expand_constant (op);
8201 : 150 : return cp_fold_convert (type, op);
8202 : : }
8203 : :
8204 : 145299758 : if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
8205 : : {
8206 : 222724 : if (integer_zerop (op))
8207 : : {
8208 : 166348 : if (TYPE_REF_P (type))
8209 : : {
8210 : 37 : if (!ctx->quiet)
8211 : 4 : error_at (loc, "dereferencing a null pointer");
8212 : 37 : *non_constant_p = true;
8213 : 37 : return t;
8214 : : }
8215 : : }
8216 : : else
8217 : : {
8218 : : /* This detects for example:
8219 : : reinterpret_cast<void*>(sizeof 0)
8220 : : */
8221 : 56376 : if (!ctx->quiet)
8222 : 15 : error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
8223 : : "a constant expression",
8224 : : type, op);
8225 : 56376 : *non_constant_p = true;
8226 : 56376 : return t;
8227 : : }
8228 : : }
8229 : :
8230 : 145243345 : if (INDIRECT_TYPE_P (type)
8231 : 78120031 : && TREE_CODE (op) == NOP_EXPR
8232 : 33286491 : && TREE_TYPE (op) == ptr_type_node
8233 : 26174 : && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
8234 : 23915 : && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
8235 : 145254751 : && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
8236 : 11406 : 0)) == heap_uninit_identifier
8237 : 10589 : || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
8238 : 10589 : 0)) == heap_vec_uninit_identifier))
8239 : : {
8240 : 1051 : tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
8241 : 1051 : tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
8242 : 1051 : tree elt_type = TREE_TYPE (type);
8243 : 1051 : tree cookie_size = NULL_TREE;
8244 : 1051 : tree arg_size = NULL_TREE;
8245 : 1051 : if (TREE_CODE (elt_type) == RECORD_TYPE
8246 : 1051 : && TYPE_NAME (elt_type) == heap_identifier)
8247 : : {
8248 : 1 : tree fld1 = TYPE_FIELDS (elt_type);
8249 : 1 : tree fld2 = DECL_CHAIN (fld1);
8250 : 1 : elt_type = TREE_TYPE (TREE_TYPE (fld2));
8251 : 1 : cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
8252 : : }
8253 : 1051 : DECL_NAME (var)
8254 : 1051 : = (DECL_NAME (var) == heap_uninit_identifier
8255 : 1051 : ? heap_identifier : heap_vec_identifier);
8256 : : /* For zero sized elt_type, try to recover how many outer_nelts
8257 : : it should have. */
8258 : 1051 : if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size)
8259 : 1050 : : integer_zerop (var_size))
8260 : 9 : && !int_size_in_bytes (elt_type)
8261 : 3 : && TREE_CODE (oldop) == CALL_EXPR
8262 : 1055 : && call_expr_nargs (oldop) >= 1)
8263 : 3 : if (tree fun = get_function_named_in_call (oldop))
8264 : 3 : if (cxx_replaceable_global_alloc_fn (fun)
8265 : 3 : && IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
8266 : 3 : arg_size = CALL_EXPR_ARG (oldop, 0);
8267 : 1051 : TREE_TYPE (var)
8268 : 1051 : = build_new_constexpr_heap_type (ctx, elt_type, cookie_size,
8269 : : var_size, arg_size,
8270 : : non_constant_p, overflow_p);
8271 : 1051 : TREE_TYPE (TREE_OPERAND (op, 0))
8272 : 2102 : = build_pointer_type (TREE_TYPE (var));
8273 : : }
8274 : :
8275 : 145243345 : if (op == oldop && tcode != UNARY_PLUS_EXPR)
8276 : : /* We didn't fold at the top so we could check for ptr-int
8277 : : conversion. */
8278 : 24269866 : return fold (t);
8279 : :
8280 : 120973479 : tree sop;
8281 : :
8282 : : /* Handle an array's bounds having been deduced after we built
8283 : : the wrapping expression. */
8284 : 120973479 : if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
8285 : : r = op;
8286 : 37577612 : else if (sop = tree_strip_nop_conversions (op),
8287 : 53584327 : sop != op && (same_type_ignoring_tlq_and_bounds_p
8288 : 16006715 : (type, TREE_TYPE (sop))))
8289 : : r = sop;
8290 : 30700815 : else if (tcode == UNARY_PLUS_EXPR)
8291 : 0 : r = fold_convert (TREE_TYPE (t), op);
8292 : : else
8293 : 30700815 : r = fold_build1 (tcode, type, op);
8294 : :
8295 : : /* Conversion of an out-of-range value has implementation-defined
8296 : : behavior; the language considers it different from arithmetic
8297 : : overflow, which is undefined. */
8298 : 120973479 : if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
8299 : 11262 : TREE_OVERFLOW (r) = false;
8300 : : }
8301 : : break;
8302 : :
8303 : 4728 : case EXCESS_PRECISION_EXPR:
8304 : 4728 : {
8305 : 4728 : tree oldop = TREE_OPERAND (t, 0);
8306 : :
8307 : 4728 : tree op = cxx_eval_constant_expression (ctx, oldop,
8308 : : lval,
8309 : : non_constant_p, overflow_p);
8310 : 4728 : if (*non_constant_p)
8311 : : return t;
8312 : 4728 : r = fold_convert (TREE_TYPE (t), op);
8313 : 4728 : break;
8314 : : }
8315 : :
8316 : 39214 : case EMPTY_CLASS_EXPR:
8317 : : /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
8318 : : it to an appropriate CONSTRUCTOR. */
8319 : 39214 : return build_constructor (TREE_TYPE (t), NULL);
8320 : :
8321 : 16953601 : case STATEMENT_LIST:
8322 : 16953601 : new_ctx = *ctx;
8323 : 16953601 : new_ctx.ctor = new_ctx.object = NULL_TREE;
8324 : 16953601 : return cxx_eval_statement_list (&new_ctx, t,
8325 : 16953601 : non_constant_p, overflow_p, jump_target);
8326 : :
8327 : 8322177 : case BIND_EXPR:
8328 : : /* Pre-emptively clear the vars declared by this BIND_EXPR from the value
8329 : : map, so that when checking whether they're already destroyed later we
8330 : : don't get confused by remnants of previous calls. */
8331 : 11315764 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
8332 : 2993587 : ctx->global->clear_value (decl);
8333 : 8322177 : r = cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
8334 : : lval,
8335 : : non_constant_p, overflow_p,
8336 : : jump_target);
8337 : 11315764 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
8338 : 2993587 : destroy_value_checked (ctx, decl, non_constant_p);
8339 : : break;
8340 : :
8341 : 1689275 : case PREINCREMENT_EXPR:
8342 : 1689275 : case POSTINCREMENT_EXPR:
8343 : 1689275 : case PREDECREMENT_EXPR:
8344 : 1689275 : case POSTDECREMENT_EXPR:
8345 : 1689275 : return cxx_eval_increment_expression (ctx, t,
8346 : 1689275 : lval, non_constant_p, overflow_p);
8347 : :
8348 : 219 : case LAMBDA_EXPR:
8349 : 219 : case NEW_EXPR:
8350 : 219 : case VEC_NEW_EXPR:
8351 : 219 : case DELETE_EXPR:
8352 : 219 : case VEC_DELETE_EXPR:
8353 : 219 : case THROW_EXPR:
8354 : 219 : case MODOP_EXPR:
8355 : : /* GCC internal stuff. */
8356 : 219 : case VA_ARG_EXPR:
8357 : 219 : case BASELINK:
8358 : 219 : case OFFSET_REF:
8359 : 219 : if (!ctx->quiet)
8360 : 50 : error_at (loc, "expression %qE is not a constant expression", t);
8361 : 219 : *non_constant_p = true;
8362 : 219 : break;
8363 : :
8364 : 61703 : case OBJ_TYPE_REF:
8365 : : /* Virtual function lookup. We don't need to do anything fancy. */
8366 : 61703 : return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t),
8367 : 61703 : lval, non_constant_p, overflow_p);
8368 : :
8369 : 16063 : case PLACEHOLDER_EXPR:
8370 : : /* Use of the value or address of the current object. */
8371 : 16063 : if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
8372 : : {
8373 : 15869 : if (TREE_CODE (ctor) == CONSTRUCTOR)
8374 : : return ctor;
8375 : : else
8376 : 15573 : return cxx_eval_constant_expression (ctx, ctor, lval,
8377 : 15573 : non_constant_p, overflow_p);
8378 : : }
8379 : : /* A placeholder without a referent. We can get here when
8380 : : checking whether NSDMIs are noexcept, or in massage_init_elt;
8381 : : just say it's non-constant for now. */
8382 : 194 : gcc_assert (ctx->quiet);
8383 : 194 : *non_constant_p = true;
8384 : 194 : break;
8385 : :
8386 : 60 : case EXIT_EXPR:
8387 : 60 : {
8388 : 60 : tree cond = TREE_OPERAND (t, 0);
8389 : 60 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
8390 : : non_constant_p, overflow_p);
8391 : 60 : VERIFY_CONSTANT (cond);
8392 : 60 : if (integer_nonzerop (cond))
8393 : 11 : *jump_target = t;
8394 : : }
8395 : : break;
8396 : :
8397 : 3 : case GOTO_EXPR:
8398 : 3 : if (breaks (&TREE_OPERAND (t, 0))
8399 : 3 : || continues (&TREE_OPERAND (t, 0)))
8400 : 0 : *jump_target = TREE_OPERAND (t, 0);
8401 : : else
8402 : : {
8403 : 3 : gcc_assert (cxx_dialect >= cxx23);
8404 : 3 : if (!ctx->quiet)
8405 : 1 : error_at (loc, "%<goto%> is not a constant expression");
8406 : 3 : *non_constant_p = true;
8407 : : }
8408 : : break;
8409 : :
8410 : 386120 : case LOOP_EXPR:
8411 : 386120 : case DO_STMT:
8412 : 386120 : case WHILE_STMT:
8413 : 386120 : case FOR_STMT:
8414 : 386120 : cxx_eval_loop_expr (ctx, t,
8415 : : non_constant_p, overflow_p, jump_target);
8416 : 386120 : break;
8417 : :
8418 : 7818 : case SWITCH_EXPR:
8419 : 7818 : case SWITCH_STMT:
8420 : 7818 : cxx_eval_switch_expr (ctx, t,
8421 : : non_constant_p, overflow_p, jump_target);
8422 : 7818 : break;
8423 : :
8424 : 64 : case REQUIRES_EXPR:
8425 : : /* It's possible to get a requires-expression in a constant
8426 : : expression. For example:
8427 : :
8428 : : template<typename T> concept bool C() {
8429 : : return requires (T t) { t; };
8430 : : }
8431 : :
8432 : : template<typename T> requires !C<T>() void f(T);
8433 : :
8434 : : Normalization leaves f with the associated constraint
8435 : : '!requires (T t) { ... }' which is not transformed into
8436 : : a constraint. */
8437 : 64 : if (!processing_template_decl)
8438 : 64 : return evaluate_requires_expr (t);
8439 : : else
8440 : 0 : *non_constant_p = true;
8441 : 0 : return t;
8442 : :
8443 : 57 : case ANNOTATE_EXPR:
8444 : 57 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
8445 : : lval,
8446 : : non_constant_p, overflow_p,
8447 : : jump_target);
8448 : 57 : break;
8449 : :
8450 : 3038 : case USING_STMT:
8451 : 3038 : r = void_node;
8452 : 3038 : break;
8453 : :
8454 : 102 : case ASSERTION_STMT:
8455 : 102 : case PRECONDITION_STMT:
8456 : 102 : case POSTCONDITION_STMT:
8457 : 102 : {
8458 : 102 : contract_semantic semantic = get_contract_semantic (t);
8459 : 102 : if (semantic == CCS_IGNORE)
8460 : : break;
8461 : :
8462 : 86 : if (!cxx_eval_assert (ctx, CONTRACT_CONDITION (t),
8463 : : G_("contract predicate is false in "
8464 : : "constant expression"),
8465 : 86 : EXPR_LOCATION (t), checked_contract_p (semantic),
8466 : : non_constant_p, overflow_p))
8467 : 38 : *non_constant_p = true;
8468 : 86 : r = void_node;
8469 : : }
8470 : 86 : break;
8471 : :
8472 : 528404 : case TEMPLATE_ID_EXPR:
8473 : 528404 : {
8474 : : /* We can evaluate template-id that refers to a concept only if
8475 : : the template arguments are non-dependent. */
8476 : 528404 : tree id = unpack_concept_check (t);
8477 : 528404 : tree tmpl = TREE_OPERAND (id, 0);
8478 : 528404 : if (!concept_definition_p (tmpl))
8479 : 0 : internal_error ("unexpected template-id %qE", t);
8480 : :
8481 : 528404 : if (function_concept_p (tmpl))
8482 : : {
8483 : 0 : if (!ctx->quiet)
8484 : 0 : error_at (cp_expr_loc_or_input_loc (t),
8485 : : "function concept must be called");
8486 : 0 : r = error_mark_node;
8487 : 0 : break;
8488 : : }
8489 : :
8490 : 528404 : if (!value_dependent_expression_p (t)
8491 : 528404 : && !uid_sensitive_constexpr_evaluation_p ())
8492 : 528358 : r = evaluate_concept_check (t);
8493 : : else
8494 : 46 : *non_constant_p = true;
8495 : :
8496 : : break;
8497 : : }
8498 : :
8499 : 18 : case ASM_EXPR:
8500 : 18 : if (!ctx->quiet)
8501 : 10 : inline_asm_in_constexpr_error (loc, /*constexpr_fundef_p*/false);
8502 : 18 : *non_constant_p = true;
8503 : 18 : return t;
8504 : :
8505 : 1154 : case BIT_CAST_EXPR:
8506 : 1154 : if (lval)
8507 : : {
8508 : 0 : if (!ctx->quiet)
8509 : 0 : error_at (EXPR_LOCATION (t),
8510 : : "address of a call to %qs is not a constant expression",
8511 : : "__builtin_bit_cast");
8512 : 0 : *non_constant_p = true;
8513 : 0 : return t;
8514 : : }
8515 : 1154 : r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p);
8516 : 1154 : break;
8517 : :
8518 : 27 : case OMP_PARALLEL:
8519 : 27 : case OMP_TASK:
8520 : 27 : case OMP_FOR:
8521 : 27 : case OMP_SIMD:
8522 : 27 : case OMP_DISTRIBUTE:
8523 : 27 : case OMP_TASKLOOP:
8524 : 27 : case OMP_LOOP:
8525 : 27 : case OMP_TEAMS:
8526 : 27 : case OMP_TARGET_DATA:
8527 : 27 : case OMP_TARGET:
8528 : 27 : case OMP_SECTIONS:
8529 : 27 : case OMP_ORDERED:
8530 : 27 : case OMP_CRITICAL:
8531 : 27 : case OMP_SINGLE:
8532 : 27 : case OMP_SCAN:
8533 : 27 : case OMP_SCOPE:
8534 : 27 : case OMP_SECTION:
8535 : 27 : case OMP_STRUCTURED_BLOCK:
8536 : 27 : case OMP_MASTER:
8537 : 27 : case OMP_MASKED:
8538 : 27 : case OMP_TASKGROUP:
8539 : 27 : case OMP_TARGET_UPDATE:
8540 : 27 : case OMP_TARGET_ENTER_DATA:
8541 : 27 : case OMP_TARGET_EXIT_DATA:
8542 : 27 : case OMP_ATOMIC:
8543 : 27 : case OMP_ATOMIC_READ:
8544 : 27 : case OMP_ATOMIC_CAPTURE_OLD:
8545 : 27 : case OMP_ATOMIC_CAPTURE_NEW:
8546 : 27 : case OMP_DEPOBJ:
8547 : 27 : case OACC_PARALLEL:
8548 : 27 : case OACC_KERNELS:
8549 : 27 : case OACC_SERIAL:
8550 : 27 : case OACC_DATA:
8551 : 27 : case OACC_HOST_DATA:
8552 : 27 : case OACC_LOOP:
8553 : 27 : case OACC_CACHE:
8554 : 27 : case OACC_DECLARE:
8555 : 27 : case OACC_ENTER_DATA:
8556 : 27 : case OACC_EXIT_DATA:
8557 : 27 : case OACC_UPDATE:
8558 : 27 : if (!ctx->quiet)
8559 : 9 : error_at (EXPR_LOCATION (t),
8560 : : "statement is not a constant expression");
8561 : 27 : *non_constant_p = true;
8562 : 27 : break;
8563 : :
8564 : 0 : default:
8565 : 0 : if (STATEMENT_CODE_P (TREE_CODE (t)))
8566 : : {
8567 : : /* This function doesn't know how to deal with pre-genericize
8568 : : statements; this can only happen with statement-expressions,
8569 : : so for now just fail. */
8570 : 0 : if (!ctx->quiet)
8571 : 0 : error_at (EXPR_LOCATION (t),
8572 : : "statement is not a constant expression");
8573 : : }
8574 : : else
8575 : 0 : internal_error ("unexpected expression %qE of kind %s", t,
8576 : : get_tree_code_name (TREE_CODE (t)));
8577 : 0 : *non_constant_p = true;
8578 : 0 : break;
8579 : : }
8580 : :
8581 : 695033471 : if (r == error_mark_node)
8582 : 14721577 : *non_constant_p = true;
8583 : :
8584 : 695033471 : if (*non_constant_p)
8585 : 207510162 : return t;
8586 : : else
8587 : : return r;
8588 : : }
8589 : :
8590 : : /* P0859: A function is needed for constant evaluation if it is a constexpr
8591 : : function that is named by an expression ([basic.def.odr]) that is
8592 : : potentially constant evaluated.
8593 : :
8594 : : So we need to instantiate any constexpr functions mentioned by the
8595 : : expression even if the definition isn't needed for evaluating the
8596 : : expression. */
8597 : :
8598 : : static tree
8599 : 347668075 : instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
8600 : : {
8601 : 347668075 : if (TREE_CODE (*tp) == FUNCTION_DECL
8602 : 9346248 : && DECL_DECLARED_CONSTEXPR_P (*tp)
8603 : 9245452 : && !DECL_INITIAL (*tp)
8604 : 1495387 : && !trivial_fn_p (*tp)
8605 : 1495384 : && (DECL_TEMPLOID_INSTANTIATION (*tp) || DECL_DEFAULTED_FN (*tp))
8606 : 347668075 : && !uid_sensitive_constexpr_evaluation_p ())
8607 : : {
8608 : 1495330 : ++function_depth;
8609 : 1495330 : if (DECL_TEMPLOID_INSTANTIATION (*tp))
8610 : 1495326 : instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
8611 : : else
8612 : 4 : synthesize_method (*tp);
8613 : 1495330 : --function_depth;
8614 : : }
8615 : 346172745 : else if (TREE_CODE (*tp) == CALL_EXPR
8616 : 336910055 : || TREE_CODE (*tp) == AGGR_INIT_EXPR)
8617 : : {
8618 : 9393553 : if (EXPR_HAS_LOCATION (*tp))
8619 : 9393482 : input_location = EXPR_LOCATION (*tp);
8620 : : }
8621 : :
8622 : 347668075 : if (!EXPR_P (*tp))
8623 : 207671680 : *walk_subtrees = 0;
8624 : :
8625 : 347668075 : return NULL_TREE;
8626 : : }
8627 : :
8628 : : static void
8629 : 178499576 : instantiate_constexpr_fns (tree t)
8630 : : {
8631 : 178499576 : location_t loc = input_location;
8632 : 178499576 : cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
8633 : 178499576 : input_location = loc;
8634 : 178499576 : }
8635 : :
8636 : : /* Look for heap variables in the expression *TP. */
8637 : :
8638 : : static tree
8639 : 2168 : find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/)
8640 : : {
8641 : 2168 : if (VAR_P (*tp)
8642 : 2168 : && (DECL_NAME (*tp) == heap_uninit_identifier
8643 : 328 : || DECL_NAME (*tp) == heap_identifier
8644 : 171 : || DECL_NAME (*tp) == heap_vec_uninit_identifier
8645 : 171 : || DECL_NAME (*tp) == heap_vec_identifier
8646 : 4 : || DECL_NAME (*tp) == heap_deleted_identifier))
8647 : : return *tp;
8648 : :
8649 : 1840 : if (TYPE_P (*tp))
8650 : 0 : *walk_subtrees = 0;
8651 : : return NULL_TREE;
8652 : : }
8653 : :
8654 : : /* Find immediate function decls in *TP if any. */
8655 : :
8656 : : static tree
8657 : 119380512 : find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/)
8658 : : {
8659 : 120410036 : if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
8660 : : return *tp;
8661 : 119380356 : if (TREE_CODE (*tp) == PTRMEM_CST
8662 : 397 : && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL
8663 : 119380567 : && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp)))
8664 : : return PTRMEM_CST_MEMBER (*tp);
8665 : : return NULL_TREE;
8666 : : }
8667 : :
8668 : : /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
8669 : : expression. Return a version of T that has TREE_CONSTANT cleared. */
8670 : :
8671 : : static tree
8672 : 104139 : mark_non_constant (tree t)
8673 : : {
8674 : 104139 : gcc_checking_assert (TREE_CONSTANT (t));
8675 : :
8676 : : /* This isn't actually constant, so unset TREE_CONSTANT.
8677 : : Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
8678 : : it to be set if it is invariant address, even when it is not
8679 : : a valid C++ constant expression. Wrap it with a NOP_EXPR
8680 : : instead. */
8681 : 104139 : if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR)
8682 : 103383 : t = copy_node (t);
8683 : 756 : else if (TREE_CODE (t) == CONSTRUCTOR)
8684 : 245 : t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t);
8685 : : else
8686 : 511 : t = build_nop (TREE_TYPE (t), t);
8687 : 104139 : TREE_CONSTANT (t) = false;
8688 : 104139 : return t;
8689 : : }
8690 : :
8691 : : /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
8692 : : STRICT has the same sense as for constant_value_1: true if we only allow
8693 : : conforming C++ constant expressions, or false if we want a constant value
8694 : : even if it doesn't conform.
8695 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
8696 : : per P0595 even when ALLOW_NON_CONSTANT is true.
8697 : : CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
8698 : : OBJECT must be non-NULL in that case. */
8699 : :
8700 : : static tree
8701 : 323038231 : cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
8702 : : bool strict = true,
8703 : : mce_value manifestly_const_eval = mce_unknown,
8704 : : bool constexpr_dtor = false,
8705 : : tree object = NULL_TREE)
8706 : : {
8707 : 323038231 : auto_timevar time (TV_CONSTEXPR);
8708 : :
8709 : 323038231 : bool non_constant_p = false;
8710 : 323038231 : bool overflow_p = false;
8711 : :
8712 : 323038231 : if (BRACE_ENCLOSED_INITIALIZER_P (t))
8713 : : {
8714 : 0 : gcc_checking_assert (allow_non_constant);
8715 : : return t;
8716 : : }
8717 : :
8718 : 323038231 : constexpr_global_ctx global_ctx;
8719 : 323038231 : constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
8720 : : allow_non_constant, strict,
8721 : 323038231 : !allow_non_constant ? mce_true : manifestly_const_eval };
8722 : :
8723 : : /* Turn off -frounding-math for manifestly constant evaluation. */
8724 : 323038231 : warning_sentinel rm (flag_rounding_math,
8725 : 323038231 : ctx.manifestly_const_eval == mce_true);
8726 : 323038231 : tree type = initialized_type (t);
8727 : 323038231 : tree r = t;
8728 : 323038231 : bool is_consteval = false;
8729 : 323038231 : if (VOID_TYPE_P (type))
8730 : : {
8731 : 2536512 : if (constexpr_dtor)
8732 : : /* Used for destructors of array elements. */
8733 : 60 : type = TREE_TYPE (object);
8734 : : else
8735 : : {
8736 : 2536452 : if (cxx_dialect < cxx20)
8737 : : return t;
8738 : 2208378 : if (TREE_CODE (t) != CALL_EXPR && TREE_CODE (t) != AGGR_INIT_EXPR)
8739 : : return t;
8740 : : /* Calls to immediate functions returning void need to be
8741 : : evaluated. */
8742 : 2208371 : tree fndecl = cp_get_callee_fndecl_nofold (t);
8743 : 4416742 : if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
8744 : : return t;
8745 : : else
8746 : : is_consteval = true;
8747 : : }
8748 : : }
8749 : 320501719 : else if (cxx_dialect >= cxx20
8750 : 112966932 : && (TREE_CODE (t) == CALL_EXPR
8751 : 96987391 : || TREE_CODE (t) == AGGR_INIT_EXPR
8752 : 96825378 : || TREE_CODE (t) == TARGET_EXPR))
8753 : : {
8754 : : /* For non-concept checks, determine if it is consteval. */
8755 : 17282608 : if (!concept_check_p (t))
8756 : : {
8757 : 17282599 : tree x = t;
8758 : 17282599 : if (TREE_CODE (x) == TARGET_EXPR)
8759 : 1141054 : x = TARGET_EXPR_INITIAL (x);
8760 : 17282599 : tree fndecl = cp_get_callee_fndecl_nofold (x);
8761 : 33950391 : if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
8762 : : is_consteval = true;
8763 : : }
8764 : : }
8765 : 320501792 : if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
8766 : : {
8767 : : /* In C++14 an NSDMI can participate in aggregate initialization,
8768 : : and can refer to the address of the object being initialized, so
8769 : : we need to pass in the relevant VAR_DECL if we want to do the
8770 : : evaluation in a single pass. The evaluation will dynamically
8771 : : update ctx.values for the VAR_DECL. We use the same strategy
8772 : : for C++11 constexpr constructors that refer to the object being
8773 : : initialized. */
8774 : 11482301 : if (constexpr_dtor)
8775 : : {
8776 : 60 : gcc_assert (object && VAR_P (object));
8777 : 60 : gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
8778 : 60 : gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
8779 : 60 : if (error_operand_p (DECL_INITIAL (object)))
8780 : : return t;
8781 : 58 : ctx.ctor = unshare_expr (DECL_INITIAL (object));
8782 : 58 : TREE_READONLY (ctx.ctor) = false;
8783 : : /* Temporarily force decl_really_constant_value to return false
8784 : : for it, we want to use ctx.ctor for the current value instead. */
8785 : 58 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
8786 : : }
8787 : : else
8788 : : {
8789 : 11482241 : ctx.ctor = build_constructor (type, NULL);
8790 : 11482241 : CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
8791 : : }
8792 : 11482299 : if (!object)
8793 : : {
8794 : 9319610 : if (TREE_CODE (t) == CALL_EXPR)
8795 : : {
8796 : : /* If T is calling a constructor to initialize an object, reframe
8797 : : it as an AGGR_INIT_EXPR to avoid trying to modify an object
8798 : : from outside the constant evaluation, which will fail even if
8799 : : the value is actually constant (is_constant_evaluated3.C). */
8800 : 4104894 : tree fn = cp_get_callee_fndecl_nofold (t);
8801 : 8209778 : if (fn && DECL_CONSTRUCTOR_P (fn))
8802 : : {
8803 : 1615302 : object = CALL_EXPR_ARG (t, 0);
8804 : 1615302 : object = build_fold_indirect_ref (object);
8805 : 1615302 : r = build_aggr_init_expr (type, r);
8806 : : }
8807 : : }
8808 : 5214716 : else if (TREE_CODE (t) == TARGET_EXPR)
8809 : 2619527 : object = TARGET_EXPR_SLOT (t);
8810 : 2595189 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
8811 : 387723 : object = AGGR_INIT_EXPR_SLOT (t);
8812 : : }
8813 : 11482299 : ctx.object = object;
8814 : 11482299 : if (object)
8815 : 6785241 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
8816 : : (type, TREE_TYPE (object)));
8817 : 6785241 : if (object && DECL_P (object))
8818 : 5086604 : global_ctx.put_value (object, ctx.ctor);
8819 : 11482299 : if (TREE_CODE (r) == TARGET_EXPR)
8820 : : /* Avoid creating another CONSTRUCTOR when we expand the
8821 : : TARGET_EXPR. */
8822 : 2661161 : r = TARGET_EXPR_INITIAL (r);
8823 : : }
8824 : :
8825 : 320501790 : auto_vec<tree, 16> cleanups;
8826 : 320501790 : global_ctx.cleanups = &cleanups;
8827 : :
8828 : 320501790 : if (manifestly_const_eval == mce_true)
8829 : 178499576 : instantiate_constexpr_fns (r);
8830 : 320501790 : r = cxx_eval_constant_expression (&ctx, r, vc_prvalue,
8831 : : &non_constant_p, &overflow_p);
8832 : :
8833 : 320491006 : if (!constexpr_dtor)
8834 : 320490948 : verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
8835 : : else
8836 : 58 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
8837 : :
8838 : 320491006 : unsigned int i;
8839 : 320491006 : tree cleanup;
8840 : : /* Evaluate the cleanups. */
8841 : 641032571 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
8842 : 50559 : cxx_eval_constant_expression (&ctx, cleanup, vc_discard,
8843 : : &non_constant_p, &overflow_p);
8844 : :
8845 : : /* Mutable logic is a bit tricky: we want to allow initialization of
8846 : : constexpr variables with mutable members, but we can't copy those
8847 : : members to another constexpr variable. */
8848 : 320491006 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_MUTABLE_POISON (r))
8849 : : {
8850 : 104 : if (!allow_non_constant)
8851 : 6 : error ("%qE is not a constant expression because it refers to "
8852 : : "mutable subobjects of %qT", t, type);
8853 : 104 : non_constant_p = true;
8854 : : }
8855 : :
8856 : 320491006 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
8857 : : {
8858 : 51782 : if (!allow_non_constant)
8859 : 12 : error ("%qE is not a constant expression because it refers to "
8860 : : "an incompletely initialized variable", t);
8861 : 51782 : TREE_CONSTANT (r) = false;
8862 : 51782 : non_constant_p = true;
8863 : : }
8864 : :
8865 : 245739738 : if (!non_constant_p && cxx_dialect >= cxx20
8866 : 566230744 : && !global_ctx.heap_vars.is_empty ())
8867 : : {
8868 : 590 : tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
8869 : : NULL);
8870 : 590 : unsigned int i;
8871 : 590 : if (heap_var)
8872 : : {
8873 : 328 : if (!allow_non_constant && !non_constant_p)
8874 : 2 : error_at (DECL_SOURCE_LOCATION (heap_var),
8875 : : "%qE is not a constant expression because it refers to "
8876 : : "a result of %<operator new%>", t);
8877 : 328 : r = t;
8878 : 328 : non_constant_p = true;
8879 : : }
8880 : 1606 : FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
8881 : : {
8882 : 1016 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
8883 : : {
8884 : 346 : if (!allow_non_constant && !non_constant_p)
8885 : 4 : error_at (DECL_SOURCE_LOCATION (heap_var),
8886 : : "%qE is not a constant expression because allocated "
8887 : : "storage has not been deallocated", t);
8888 : 346 : r = t;
8889 : 346 : non_constant_p = true;
8890 : : }
8891 : 1016 : varpool_node::get (heap_var)->remove ();
8892 : : }
8893 : : }
8894 : :
8895 : : /* Check that immediate invocation does not return an expression referencing
8896 : : any immediate function decls. */
8897 : 320491006 : if (!non_constant_p && cxx_dialect >= cxx20)
8898 : 167786216 : if (tree immediate_fndecl
8899 : 83893108 : = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
8900 : : NULL))
8901 : : {
8902 : 191 : if (!allow_non_constant && !non_constant_p)
8903 : : {
8904 : 26 : if (is_consteval)
8905 : 14 : error_at (cp_expr_loc_or_input_loc (t),
8906 : : "immediate evaluation returns address of immediate "
8907 : : "function %qD", immediate_fndecl);
8908 : : else
8909 : 18 : error_at (cp_expr_loc_or_input_loc (t),
8910 : : "constant evaluation returns address of immediate "
8911 : : "function %qD", immediate_fndecl);
8912 : : }
8913 : 191 : r = t;
8914 : 191 : non_constant_p = true;
8915 : : }
8916 : :
8917 : 320491006 : if (non_constant_p)
8918 : : /* If we saw something bad, go back to our argument. The wrapping below is
8919 : : only for the cases of TREE_CONSTANT argument or overflow. */
8920 : 74751809 : r = t;
8921 : :
8922 : 320491006 : if (!non_constant_p && overflow_p)
8923 : 299 : non_constant_p = true;
8924 : :
8925 : : /* Unshare the result. */
8926 : 320491006 : bool should_unshare = true;
8927 : 320491006 : if (r == t || (TREE_CODE (t) == TARGET_EXPR
8928 : 927719 : && TARGET_EXPR_INITIAL (t) == r))
8929 : : should_unshare = false;
8930 : :
8931 : 320491006 : if (non_constant_p && !allow_non_constant)
8932 : 1603 : return error_mark_node;
8933 : 320489403 : else if (constexpr_dtor)
8934 : : return r;
8935 : 320489351 : else if (non_constant_p && TREE_CONSTANT (r))
8936 : 99743 : r = mark_non_constant (r);
8937 : 320389608 : else if (non_constant_p)
8938 : : return t;
8939 : :
8940 : 245838589 : if (should_unshare)
8941 : 126397972 : r = unshare_expr (r);
8942 : :
8943 : 245838589 : if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
8944 : : {
8945 : 4667165 : r = adjust_temp_type (type, r);
8946 : 4667165 : if (TREE_CODE (t) == TARGET_EXPR
8947 : 4667165 : && TARGET_EXPR_INITIAL (t) == r)
8948 : : return t;
8949 : 3875512 : else if (TREE_CODE (t) == CONSTRUCTOR || TREE_CODE (t) == CALL_EXPR)
8950 : : /* Don't add a TARGET_EXPR if our argument didn't have one. */;
8951 : 519944 : else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t))
8952 : 2206 : r = get_target_expr (r);
8953 : : else
8954 : : {
8955 : 517738 : r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup);
8956 : 517738 : TREE_CONSTANT (r) = true;
8957 : : }
8958 : : }
8959 : :
8960 : 245046936 : if (TREE_CODE (t) == TARGET_EXPR
8961 : 136066 : && TREE_CODE (r) == TARGET_EXPR)
8962 : : {
8963 : : /* Preserve this flag for potential_constant_expression, and the others
8964 : : for good measure. */
8965 : 135986 : TARGET_EXPR_ELIDING_P (r) = TARGET_EXPR_ELIDING_P (t);
8966 : 135986 : TARGET_EXPR_IMPLICIT_P (r) = TARGET_EXPR_IMPLICIT_P (t);
8967 : 135986 : TARGET_EXPR_LIST_INIT_P (r) = TARGET_EXPR_LIST_INIT_P (t);
8968 : 135986 : TARGET_EXPR_DIRECT_INIT_P (r) = TARGET_EXPR_DIRECT_INIT_P (t);
8969 : : }
8970 : :
8971 : : /* Remember the original location if that wouldn't need a wrapper. */
8972 : 245046936 : if (location_t loc = EXPR_LOCATION (t))
8973 : 100034484 : protected_set_expr_location (r, loc);
8974 : :
8975 : 245046936 : return r;
8976 : 646054894 : }
8977 : :
8978 : : /* If T represents a constant expression returns its reduced value.
8979 : : Otherwise return error_mark_node. */
8980 : :
8981 : : tree
8982 : 111878686 : cxx_constant_value (tree t, tree decl /* = NULL_TREE */,
8983 : : tsubst_flags_t complain /* = tf_error */)
8984 : : {
8985 : 111878686 : bool sfinae = !(complain & tf_error);
8986 : 111878686 : tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, mce_true, false, decl);
8987 : 111878686 : if (sfinae && !TREE_CONSTANT (r))
8988 : 248 : r = error_mark_node;
8989 : 111878686 : return r;
8990 : : }
8991 : :
8992 : : /* Like cxx_constant_value, but used for evaluation of constexpr destructors
8993 : : of constexpr variables. The actual initializer of DECL is not modified. */
8994 : :
8995 : : void
8996 : 60 : cxx_constant_dtor (tree t, tree decl)
8997 : : {
8998 : 60 : cxx_eval_outermost_constant_expr (t, false, true, mce_true, true, decl);
8999 : 60 : }
9000 : :
9001 : : /* Helper routine for fold_simple function. Either return simplified
9002 : : expression T, otherwise NULL_TREE.
9003 : : In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
9004 : : even if we are within template-declaration. So be careful on call, as in
9005 : : such case types can be undefined. */
9006 : :
9007 : : static tree
9008 : 99117949 : fold_simple_1 (tree t)
9009 : : {
9010 : 99117949 : tree op1;
9011 : 99117949 : enum tree_code code = TREE_CODE (t);
9012 : :
9013 : 99117949 : switch (code)
9014 : : {
9015 : : case INTEGER_CST:
9016 : : case REAL_CST:
9017 : : case VECTOR_CST:
9018 : : case FIXED_CST:
9019 : : case COMPLEX_CST:
9020 : : return t;
9021 : :
9022 : 776760 : case SIZEOF_EXPR:
9023 : 776760 : return fold_sizeof_expr (t);
9024 : :
9025 : 27628011 : case ABS_EXPR:
9026 : 27628011 : case ABSU_EXPR:
9027 : 27628011 : case CONJ_EXPR:
9028 : 27628011 : case REALPART_EXPR:
9029 : 27628011 : case IMAGPART_EXPR:
9030 : 27628011 : case NEGATE_EXPR:
9031 : 27628011 : case BIT_NOT_EXPR:
9032 : 27628011 : case TRUTH_NOT_EXPR:
9033 : 27628011 : case VIEW_CONVERT_EXPR:
9034 : 27628011 : CASE_CONVERT:
9035 : 27628011 : case FLOAT_EXPR:
9036 : 27628011 : case FIX_TRUNC_EXPR:
9037 : 27628011 : case FIXED_CONVERT_EXPR:
9038 : 27628011 : case ADDR_SPACE_CONVERT_EXPR:
9039 : :
9040 : 27628011 : op1 = TREE_OPERAND (t, 0);
9041 : :
9042 : 27628011 : t = const_unop (code, TREE_TYPE (t), op1);
9043 : 27628011 : if (!t)
9044 : : return NULL_TREE;
9045 : :
9046 : 1690969 : if (CONVERT_EXPR_CODE_P (code)
9047 : 1690969 : && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
9048 : 0 : TREE_OVERFLOW (t) = false;
9049 : : return t;
9050 : :
9051 : : default:
9052 : : return NULL_TREE;
9053 : : }
9054 : : }
9055 : :
9056 : : /* If T is a simple constant expression, returns its simplified value.
9057 : : Otherwise returns T. In contrast to maybe_constant_value we
9058 : : simplify only few operations on constant-expressions, and we don't
9059 : : try to simplify constexpressions. */
9060 : :
9061 : : tree
9062 : 99603964 : fold_simple (tree t)
9063 : : {
9064 : 99603964 : if (processing_template_decl)
9065 : : return t;
9066 : :
9067 : 99117949 : tree r = fold_simple_1 (t);
9068 : 99117949 : if (r)
9069 : : return r;
9070 : :
9071 : : return t;
9072 : : }
9073 : :
9074 : : /* Try folding the expression T to a simple constant.
9075 : : Returns that constant, otherwise returns T. */
9076 : :
9077 : : tree
9078 : 495293 : fold_to_constant (tree t)
9079 : : {
9080 : 495293 : tree r = fold (t);
9081 : 495293 : if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r))
9082 : : return r;
9083 : : else
9084 : 36726 : return t;
9085 : : }
9086 : :
9087 : : /* If T is a constant expression, returns its reduced value.
9088 : : Otherwise, if T does not have TREE_CONSTANT set, returns T.
9089 : : Otherwise, returns a version of T without TREE_CONSTANT.
9090 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
9091 : : as per P0595. */
9092 : :
9093 : : static GTY((deletable)) hash_map<tree, tree> *cv_cache;
9094 : :
9095 : : tree
9096 : 474948901 : maybe_constant_value (tree t, tree decl /* = NULL_TREE */,
9097 : : mce_value manifestly_const_eval /* = mce_unknown */)
9098 : : {
9099 : 474948901 : tree r;
9100 : :
9101 : 474948901 : if (!is_nondependent_constant_expression (t))
9102 : : {
9103 : 0 : if (TREE_OVERFLOW_P (t)
9104 : 115157326 : || (!processing_template_decl && TREE_CONSTANT (t)))
9105 : 4396 : t = mark_non_constant (t);
9106 : 115157326 : return t;
9107 : : }
9108 : 359791575 : else if (CONSTANT_CLASS_P (t))
9109 : : /* No caching or evaluation needed. */
9110 : : return t;
9111 : :
9112 : : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
9113 : : but at least try folding it to a simple constant. */
9114 : 193888777 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
9115 : 440235 : return fold_to_constant (t);
9116 : :
9117 : 190673749 : if (manifestly_const_eval != mce_unknown)
9118 : 85812893 : return cxx_eval_outermost_constant_expr (t, true, true,
9119 : 85802109 : manifestly_const_eval, false, decl);
9120 : :
9121 : 107635649 : if (cv_cache == NULL)
9122 : 142164 : cv_cache = hash_map<tree, tree>::create_ggc (101);
9123 : 107635649 : if (tree *cached = cv_cache->get (t))
9124 : : {
9125 : 7850990 : r = *cached;
9126 : 7850990 : if (r != t)
9127 : : {
9128 : : /* Clear processing_template_decl for sake of break_out_target_exprs;
9129 : : entries in the cv_cache are non-templated. */
9130 : 3222885 : processing_template_decl_sentinel ptds;
9131 : :
9132 : 3222885 : r = break_out_target_exprs (r, /*clear_loc*/true);
9133 : 3222885 : protected_set_expr_location (r, EXPR_LOCATION (t));
9134 : 3222885 : }
9135 : 7850990 : return r;
9136 : : }
9137 : :
9138 : 99784659 : uid_sensitive_constexpr_evaluation_checker c;
9139 : 99784659 : r = cxx_eval_outermost_constant_expr (t, true, true,
9140 : : manifestly_const_eval, false, decl);
9141 : 99784659 : gcc_checking_assert (r == t
9142 : : || CONVERT_EXPR_P (t)
9143 : : || TREE_CODE (t) == VIEW_CONVERT_EXPR
9144 : : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
9145 : : || !cp_tree_equal (r, t));
9146 : 99784659 : if (!c.evaluation_restricted_p ())
9147 : 98176789 : cv_cache->put (t, r);
9148 : : return r;
9149 : : }
9150 : :
9151 : : /* Dispose of the whole CV_CACHE. */
9152 : :
9153 : : static void
9154 : 21814347 : clear_cv_cache (void)
9155 : : {
9156 : 21814347 : if (cv_cache != NULL)
9157 : 21578143 : cv_cache->empty ();
9158 : 21814347 : }
9159 : :
9160 : : /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
9161 : :
9162 : : void
9163 : 21814347 : clear_cv_and_fold_caches ()
9164 : : {
9165 : 21814347 : clear_cv_cache ();
9166 : 21814347 : clear_fold_cache ();
9167 : 21814347 : }
9168 : :
9169 : : /* Internal function handling expressions in templates for
9170 : : fold_non_dependent_expr and fold_non_dependent_init.
9171 : :
9172 : : If we're in a template, but T isn't value dependent, simplify
9173 : : it. We're supposed to treat:
9174 : :
9175 : : template <typename T> void f(T[1 + 1]);
9176 : : template <typename T> void f(T[2]);
9177 : :
9178 : : as two declarations of the same function, for example. */
9179 : :
9180 : : static tree
9181 : 22628896 : fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
9182 : : bool manifestly_const_eval,
9183 : : tree object)
9184 : : {
9185 : 22628896 : gcc_assert (processing_template_decl);
9186 : :
9187 : 22628896 : if (is_nondependent_constant_expression (t))
9188 : : {
9189 : 11737279 : processing_template_decl_sentinel s;
9190 : 11737279 : t = instantiate_non_dependent_expr_internal (t, complain);
9191 : :
9192 : 11737279 : if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
9193 : : {
9194 : 0 : if (TREE_OVERFLOW_P (t))
9195 : : {
9196 : 0 : t = build_nop (TREE_TYPE (t), t);
9197 : 0 : TREE_CONSTANT (t) = false;
9198 : : }
9199 : 0 : return t;
9200 : : }
9201 : 11737279 : else if (CONSTANT_CLASS_P (t))
9202 : : /* No evaluation needed. */
9203 : : return t;
9204 : :
9205 : : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
9206 : : but at least try folding it to a simple constant. */
9207 : 3483772 : if (cp_unevaluated_operand && !manifestly_const_eval)
9208 : 19037 : return fold_to_constant (t);
9209 : :
9210 : 3464735 : tree r = cxx_eval_outermost_constant_expr (t, true, true,
9211 : : mce_value (manifestly_const_eval),
9212 : : false, object);
9213 : : /* cp_tree_equal looks through NOPs, so allow them. */
9214 : 3464735 : gcc_checking_assert (r == t
9215 : : || CONVERT_EXPR_P (t)
9216 : : || TREE_CODE (t) == VIEW_CONVERT_EXPR
9217 : : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
9218 : : || !cp_tree_equal (r, t));
9219 : 3464735 : return r;
9220 : 11737279 : }
9221 : 10891617 : else if (TREE_OVERFLOW_P (t))
9222 : : {
9223 : 0 : t = build_nop (TREE_TYPE (t), t);
9224 : 0 : TREE_CONSTANT (t) = false;
9225 : : }
9226 : :
9227 : : return t;
9228 : : }
9229 : :
9230 : : /* Like maybe_constant_value but first fully instantiate the argument.
9231 : :
9232 : : Note: this is equivalent to instantiate_non_dependent_expr (t, complain)
9233 : : followed by maybe_constant_value but is more efficient,
9234 : : because it calls instantiation_dependent_expression_p and
9235 : : potential_constant_expression at most once.
9236 : : The manifestly_const_eval argument is passed to maybe_constant_value.
9237 : :
9238 : : Callers should generally pass their active complain, or if they are in a
9239 : : non-template, diagnosing context, they can use the default of
9240 : : tf_warning_or_error. Callers that might be within a template context, don't
9241 : : have a complain parameter, and aren't going to remember the result for long
9242 : : (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
9243 : : appropriately. */
9244 : :
9245 : : tree
9246 : 90000350 : fold_non_dependent_expr (tree t,
9247 : : tsubst_flags_t complain /* = tf_warning_or_error */,
9248 : : bool manifestly_const_eval /* = false */,
9249 : : tree object /* = NULL_TREE */)
9250 : : {
9251 : 90000350 : if (t == NULL_TREE)
9252 : : return NULL_TREE;
9253 : :
9254 : 89602511 : if (processing_template_decl)
9255 : 21690736 : return fold_non_dependent_expr_template (t, complain,
9256 : 21690736 : manifestly_const_eval, object);
9257 : :
9258 : 67911775 : return maybe_constant_value (t, object, mce_value (manifestly_const_eval));
9259 : : }
9260 : :
9261 : : /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
9262 : : return the original expression. */
9263 : :
9264 : : tree
9265 : 1623529 : maybe_fold_non_dependent_expr (tree expr,
9266 : : tsubst_flags_t complain/*=tf_warning_or_error*/)
9267 : : {
9268 : 1623529 : tree t = fold_non_dependent_expr (expr, complain);
9269 : 1623529 : if (t && TREE_CONSTANT (t))
9270 : 813224 : return t;
9271 : :
9272 : : return expr;
9273 : : }
9274 : :
9275 : : /* Like maybe_constant_init but first fully instantiate the argument. */
9276 : :
9277 : : tree
9278 : 16637675 : fold_non_dependent_init (tree t,
9279 : : tsubst_flags_t complain /*=tf_warning_or_error*/,
9280 : : bool manifestly_const_eval /*=false*/,
9281 : : tree object /* = NULL_TREE */)
9282 : : {
9283 : 16637675 : if (t == NULL_TREE)
9284 : : return NULL_TREE;
9285 : :
9286 : 16637675 : if (processing_template_decl)
9287 : : {
9288 : 938160 : t = fold_non_dependent_expr_template (t, complain,
9289 : : manifestly_const_eval, object);
9290 : : /* maybe_constant_init does this stripping, so do it here too. */
9291 : 938160 : if (TREE_CODE (t) == TARGET_EXPR)
9292 : : {
9293 : 47 : tree init = TARGET_EXPR_INITIAL (t);
9294 : 47 : if (TREE_CODE (init) == CONSTRUCTOR)
9295 : 938160 : t = init;
9296 : : }
9297 : 938160 : return t;
9298 : : }
9299 : :
9300 : 15699515 : return maybe_constant_init (t, object, manifestly_const_eval);
9301 : : }
9302 : :
9303 : : /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
9304 : : than wrapped in a TARGET_EXPR.
9305 : : ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
9306 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
9307 : : per P0595 even when ALLOW_NON_CONSTANT is true. */
9308 : :
9309 : : static tree
9310 : 32917767 : maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
9311 : : bool manifestly_const_eval)
9312 : : {
9313 : 32917767 : if (!t)
9314 : : return t;
9315 : 32917767 : if (TREE_CODE (t) == EXPR_STMT)
9316 : 20480 : t = TREE_OPERAND (t, 0);
9317 : 32917767 : if (TREE_CODE (t) == CONVERT_EXPR
9318 : 32917767 : && VOID_TYPE_P (TREE_TYPE (t)))
9319 : 20471 : t = TREE_OPERAND (t, 0);
9320 : 32917767 : if (TREE_CODE (t) == INIT_EXPR)
9321 : 23131 : t = TREE_OPERAND (t, 1);
9322 : 32917767 : if (TREE_CODE (t) == TARGET_EXPR)
9323 : 367909 : t = TARGET_EXPR_INITIAL (t);
9324 : 32917767 : if (!is_nondependent_static_init_expression (t))
9325 : : /* Don't try to evaluate it. */;
9326 : 30877865 : else if (CONSTANT_CLASS_P (t) && TREE_CODE (t) != PTRMEM_CST)
9327 : : /* No evaluation needed. PTRMEM_CST needs the immediate fn check. */;
9328 : : else
9329 : : {
9330 : : /* [basic.start.static] allows constant-initialization of variables with
9331 : : static or thread storage duration even if it isn't required, but we
9332 : : shouldn't bend the rules the same way for automatic variables. */
9333 : 2410378 : bool is_static = (decl && DECL_P (decl)
9334 : 14627381 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
9335 : : if (is_static)
9336 : : manifestly_const_eval = true;
9337 : :
9338 : 12431285 : if (cp_unevaluated_operand && !manifestly_const_eval)
9339 : 36021 : return fold_to_constant (t);
9340 : :
9341 : 12395264 : t = cxx_eval_outermost_constant_expr (t, allow_non_constant, !is_static,
9342 : : mce_value (manifestly_const_eval),
9343 : : false, decl);
9344 : : }
9345 : 32881746 : if (TREE_CODE (t) == TARGET_EXPR)
9346 : : {
9347 : 283128 : tree init = TARGET_EXPR_INITIAL (t);
9348 : 283128 : if (TREE_CODE (init) == CONSTRUCTOR)
9349 : 32917767 : t = init;
9350 : : }
9351 : : return t;
9352 : : }
9353 : :
9354 : : /* Wrapper for maybe_constant_init_1 which permits non constants. */
9355 : :
9356 : : tree
9357 : 32916666 : maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
9358 : : {
9359 : 32916666 : return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
9360 : : }
9361 : :
9362 : : /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
9363 : :
9364 : : tree
9365 : 1101 : cxx_constant_init (tree t, tree decl)
9366 : : {
9367 : 1101 : return maybe_constant_init_1 (t, decl, false, true);
9368 : : }
9369 : :
9370 : : #if 0
9371 : : /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
9372 : : /* Return true if the object referred to by REF has automatic or thread
9373 : : local storage. */
9374 : :
9375 : : enum { ck_ok, ck_bad, ck_unknown };
9376 : : static int
9377 : : check_automatic_or_tls (tree ref)
9378 : : {
9379 : : machine_mode mode;
9380 : : poly_int64 bitsize, bitpos;
9381 : : tree offset;
9382 : : int volatilep = 0, unsignedp = 0;
9383 : : tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
9384 : : &mode, &unsignedp, &volatilep, false);
9385 : : duration_kind dk;
9386 : :
9387 : : /* If there isn't a decl in the middle, we don't know the linkage here,
9388 : : and this isn't a constant expression anyway. */
9389 : : if (!DECL_P (decl))
9390 : : return ck_unknown;
9391 : : dk = decl_storage_duration (decl);
9392 : : return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
9393 : : }
9394 : : #endif
9395 : :
9396 : : /* Data structure for passing data from potential_constant_expression_1
9397 : : to check_for_return_continue via cp_walk_tree. */
9398 : : struct check_for_return_continue_data {
9399 : : hash_set<tree> *pset;
9400 : : tree continue_stmt;
9401 : : tree break_stmt;
9402 : : };
9403 : :
9404 : : /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
9405 : : called through cp_walk_tree. Return the first RETURN_EXPR found, or note
9406 : : the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */
9407 : : static tree
9408 : 22627411 : check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
9409 : : {
9410 : 22627411 : tree t = *tp, s, b;
9411 : 22627411 : check_for_return_continue_data *d = (check_for_return_continue_data *) data;
9412 : 22627411 : switch (TREE_CODE (t))
9413 : : {
9414 : : case RETURN_EXPR:
9415 : : return t;
9416 : :
9417 : 30 : case CONTINUE_STMT:
9418 : 30 : if (d->continue_stmt == NULL_TREE)
9419 : 30 : d->continue_stmt = t;
9420 : : break;
9421 : :
9422 : 2104 : case BREAK_STMT:
9423 : 2104 : if (d->break_stmt == NULL_TREE)
9424 : 2068 : d->break_stmt = t;
9425 : : break;
9426 : :
9427 : : #define RECUR(x) \
9428 : : if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
9429 : : d->pset)) \
9430 : : return r
9431 : :
9432 : : /* For loops, walk subtrees manually, so that continue stmts found
9433 : : inside of the bodies of the loops are ignored. */
9434 : 10213 : case DO_STMT:
9435 : 10213 : *walk_subtrees = 0;
9436 : 10213 : RECUR (DO_COND (t));
9437 : 10213 : s = d->continue_stmt;
9438 : 10213 : b = d->break_stmt;
9439 : 10213 : RECUR (DO_BODY (t));
9440 : 10213 : d->continue_stmt = s;
9441 : 10213 : d->break_stmt = b;
9442 : 10213 : break;
9443 : :
9444 : 86 : case WHILE_STMT:
9445 : 86 : *walk_subtrees = 0;
9446 : 86 : RECUR (WHILE_COND (t));
9447 : 86 : s = d->continue_stmt;
9448 : 86 : b = d->break_stmt;
9449 : 86 : RECUR (WHILE_BODY (t));
9450 : 82 : d->continue_stmt = s;
9451 : 82 : d->break_stmt = b;
9452 : 82 : break;
9453 : :
9454 : 98 : case FOR_STMT:
9455 : 98 : *walk_subtrees = 0;
9456 : 98 : RECUR (FOR_INIT_STMT (t));
9457 : 98 : RECUR (FOR_COND (t));
9458 : 98 : RECUR (FOR_EXPR (t));
9459 : 98 : s = d->continue_stmt;
9460 : 98 : b = d->break_stmt;
9461 : 98 : RECUR (FOR_BODY (t));
9462 : 60 : d->continue_stmt = s;
9463 : 60 : d->break_stmt = b;
9464 : 60 : break;
9465 : :
9466 : 0 : case RANGE_FOR_STMT:
9467 : 0 : *walk_subtrees = 0;
9468 : 0 : RECUR (RANGE_FOR_EXPR (t));
9469 : 0 : s = d->continue_stmt;
9470 : 0 : b = d->break_stmt;
9471 : 0 : RECUR (RANGE_FOR_BODY (t));
9472 : 0 : d->continue_stmt = s;
9473 : 0 : d->break_stmt = b;
9474 : 0 : break;
9475 : :
9476 : 6 : case SWITCH_STMT:
9477 : 6 : *walk_subtrees = 0;
9478 : 6 : RECUR (SWITCH_STMT_COND (t));
9479 : 6 : b = d->break_stmt;
9480 : 6 : RECUR (SWITCH_STMT_BODY (t));
9481 : 0 : d->break_stmt = b;
9482 : 0 : break;
9483 : : #undef RECUR
9484 : :
9485 : : case STATEMENT_LIST:
9486 : : case CONSTRUCTOR:
9487 : : break;
9488 : :
9489 : 21839905 : default:
9490 : 21839905 : if (!EXPR_P (t))
9491 : 6300318 : *walk_subtrees = 0;
9492 : : break;
9493 : : }
9494 : :
9495 : : return NULL_TREE;
9496 : : }
9497 : :
9498 : : /* Return true if T denotes a potentially constant expression. Issue
9499 : : diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
9500 : : an lvalue-rvalue conversion is implied. If NOW is true, we want to
9501 : : consider the expression in the current context, independent of constexpr
9502 : : substitution. If FUNDEF_P is true, we're checking a constexpr function body
9503 : : and hard errors should not be reported by constexpr_error.
9504 : :
9505 : : C++0x [expr.const] used to say
9506 : :
9507 : : 6 An expression is a potential constant expression if it is
9508 : : a constant expression where all occurrences of function
9509 : : parameters are replaced by arbitrary constant expressions
9510 : : of the appropriate type.
9511 : :
9512 : : 2 A conditional expression is a constant expression unless it
9513 : : involves one of the following as a potentially evaluated
9514 : : subexpression (3.2), but subexpressions of logical AND (5.14),
9515 : : logical OR (5.15), and conditional (5.16) operations that are
9516 : : not evaluated are not considered. */
9517 : :
9518 : : static bool
9519 : 2603116544 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
9520 : : bool fundef_p, tsubst_flags_t flags,
9521 : : tree *jump_target)
9522 : : {
9523 : : #define RECUR(T,RV) \
9524 : : potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \
9525 : : jump_target)
9526 : :
9527 : 2603116544 : enum { any = false, rval = true };
9528 : 2603116544 : int i;
9529 : 2603116544 : tree tmp;
9530 : :
9531 : 2603116544 : if (t == error_mark_node)
9532 : : return false;
9533 : 2603106328 : if (t == NULL_TREE)
9534 : : return true;
9535 : 2598787354 : location_t loc = cp_expr_loc_or_input_loc (t);
9536 : :
9537 : 2598787354 : if (*jump_target)
9538 : : /* If we are jumping, ignore everything. This is simpler than the
9539 : : cxx_eval_constant_expression handling because we only need to be
9540 : : conservatively correct, and we don't necessarily have a constant value
9541 : : available, so we don't bother with switch tracking. */
9542 : : return true;
9543 : :
9544 : 747176 : if (TREE_THIS_VOLATILE (t) && want_rval
9545 : 2596415372 : && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t)))
9546 : : {
9547 : 86399 : if (flags & tf_error)
9548 : 22 : constexpr_error (loc, fundef_p, "lvalue-to-rvalue conversion of "
9549 : : "a volatile lvalue %qE with type %qT", t,
9550 : 22 : TREE_TYPE (t));
9551 : 86399 : return false;
9552 : : }
9553 : 2596058783 : if (CONSTANT_CLASS_P (t))
9554 : : return true;
9555 : 1986078134 : if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
9556 : 1986078134 : && TREE_TYPE (t) == error_mark_node)
9557 : : return false;
9558 : :
9559 : 1986078099 : switch (TREE_CODE (t))
9560 : : {
9561 : : case FUNCTION_DECL:
9562 : : case BASELINK:
9563 : : case TEMPLATE_DECL:
9564 : : case OVERLOAD:
9565 : : case TEMPLATE_ID_EXPR:
9566 : : case LABEL_DECL:
9567 : : case CASE_LABEL_EXPR:
9568 : : case PREDICT_EXPR:
9569 : : case CONST_DECL:
9570 : : case SIZEOF_EXPR:
9571 : : case ALIGNOF_EXPR:
9572 : : case OFFSETOF_EXPR:
9573 : : case NOEXCEPT_EXPR:
9574 : : case TEMPLATE_PARM_INDEX:
9575 : : case TRAIT_EXPR:
9576 : : case IDENTIFIER_NODE:
9577 : : case USERDEF_LITERAL:
9578 : : /* We can see a FIELD_DECL in a pointer-to-member expression. */
9579 : : case FIELD_DECL:
9580 : : case RESULT_DECL:
9581 : : case USING_DECL:
9582 : : case USING_STMT:
9583 : : case PLACEHOLDER_EXPR:
9584 : : case REQUIRES_EXPR:
9585 : : case STATIC_ASSERT:
9586 : : case DEBUG_BEGIN_STMT:
9587 : : return true;
9588 : :
9589 : 12969001 : case RETURN_EXPR:
9590 : 12969001 : if (!RECUR (TREE_OPERAND (t, 0), any))
9591 : : return false;
9592 : : /* FALLTHROUGH */
9593 : :
9594 : 12854237 : case BREAK_STMT:
9595 : 12854237 : case CONTINUE_STMT:
9596 : 12854237 : *jump_target = t;
9597 : 12854237 : return true;
9598 : :
9599 : 181053425 : case PARM_DECL:
9600 : 181053425 : if (now && want_rval)
9601 : : {
9602 : 53856057 : tree type = TREE_TYPE (t);
9603 : 53856057 : if (dependent_type_p (type)
9604 : 35061596 : || !COMPLETE_TYPE_P (processing_template_decl
9605 : : ? type : complete_type (type))
9606 : 88917651 : || is_really_empty_class (type, /*ignore_vptr*/false))
9607 : : /* An empty class has no data to read. */
9608 : 18794697 : return true;
9609 : 35061360 : if (flags & tf_error)
9610 : 11 : constexpr_error (input_location, fundef_p,
9611 : : "%qE is not a constant expression", t);
9612 : 35061360 : return false;
9613 : : }
9614 : : return true;
9615 : :
9616 : 152487338 : case AGGR_INIT_EXPR:
9617 : 152487338 : case CALL_EXPR:
9618 : : /* -- an invocation of a function other than a constexpr function
9619 : : or a constexpr constructor. */
9620 : 152487338 : {
9621 : 152487338 : tree fun = get_function_named_in_call (t);
9622 : 152487338 : const int nargs = call_expr_nargs (t);
9623 : 152487338 : i = 0;
9624 : :
9625 : 152487338 : if (fun == NULL_TREE)
9626 : : {
9627 : : /* Reset to allow the function to continue past the end
9628 : : of the block below. Otherwise return early. */
9629 : 79755 : bool bail = true;
9630 : :
9631 : 79755 : if (TREE_CODE (t) == CALL_EXPR
9632 : 79755 : && CALL_EXPR_FN (t) == NULL_TREE)
9633 : 79755 : switch (CALL_EXPR_IFN (t))
9634 : : {
9635 : : /* These should be ignored, they are optimized away from
9636 : : constexpr functions. */
9637 : : case IFN_UBSAN_NULL:
9638 : : case IFN_UBSAN_BOUNDS:
9639 : : case IFN_UBSAN_VPTR:
9640 : : case IFN_FALLTHROUGH:
9641 : : case IFN_ASSUME:
9642 : : return true;
9643 : :
9644 : : case IFN_ADD_OVERFLOW:
9645 : : case IFN_SUB_OVERFLOW:
9646 : : case IFN_MUL_OVERFLOW:
9647 : : case IFN_LAUNDER:
9648 : : case IFN_VEC_CONVERT:
9649 : : bail = false;
9650 : : break;
9651 : :
9652 : : default:
9653 : : break;
9654 : : }
9655 : :
9656 : : if (bail)
9657 : : {
9658 : : /* fold_call_expr can't do anything with IFN calls. */
9659 : 5 : if (flags & tf_error)
9660 : 0 : constexpr_error (loc, fundef_p,
9661 : : "call to internal function %qE", t);
9662 : 5 : return false;
9663 : : }
9664 : : }
9665 : :
9666 : 152407583 : if (fun && is_overloaded_fn (fun))
9667 : : {
9668 : 137187593 : if (!RECUR (fun, true))
9669 : : return false;
9670 : 136568821 : fun = get_fns (fun);
9671 : :
9672 : 136568821 : if (TREE_CODE (fun) == FUNCTION_DECL)
9673 : : {
9674 : 116323338 : if (builtin_valid_in_constant_expr_p (fun))
9675 : : return true;
9676 : 115688951 : if (!maybe_constexpr_fn (fun)
9677 : : /* Allow any built-in function; if the expansion
9678 : : isn't constant, we'll deal with that then. */
9679 : 39215822 : && !fndecl_built_in_p (fun)
9680 : : /* In C++20, replaceable global allocation functions
9681 : : are constant expressions. */
9682 : 24068518 : && (!cxx_replaceable_global_alloc_fn (fun)
9683 : 96282 : || TREE_CODE (t) != CALL_EXPR
9684 : 96282 : || (!CALL_FROM_NEW_OR_DELETE_P (t)
9685 : 89307 : && (current_function_decl == NULL_TREE
9686 : 89307 : || !is_std_allocator_allocate
9687 : 89307 : (current_function_decl))))
9688 : : /* Allow placement new in std::construct_at. */
9689 : 24009601 : && (!cxx_placement_new_fn (fun)
9690 : 32261 : || TREE_CODE (t) != CALL_EXPR
9691 : 32261 : || current_function_decl == NULL_TREE
9692 : 32255 : || !is_std_construct_at (current_function_decl))
9693 : 139668835 : && !cxx_dynamic_cast_fn_p (fun))
9694 : : {
9695 : 23979140 : if ((flags & tf_error)
9696 : 23979140 : && constexpr_error (loc, fundef_p,
9697 : : "call to non-%<constexpr%> "
9698 : : "function %qD", fun))
9699 : 129 : explain_invalid_constexpr_fn (fun);
9700 : 23979140 : return false;
9701 : : }
9702 : : }
9703 : :
9704 : 111955294 : fun = OVL_FIRST (fun);
9705 : : /* Skip initial arguments to base constructors. */
9706 : 111955294 : if (DECL_BASE_CONSTRUCTOR_P (fun))
9707 : 1965122 : i = num_artificial_parms_for (fun);
9708 : : }
9709 : 15219990 : else if (fun)
9710 : : {
9711 : 15219990 : if (TREE_TYPE (fun)
9712 : 15219990 : && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun)))
9713 : : want_rval = rval;
9714 : : else
9715 : : want_rval = any;
9716 : 15219990 : if (RECUR (fun, want_rval))
9717 : : /* Might end up being a constant function pointer. But it
9718 : : could also be a function object with constexpr op(), so
9719 : : we pass 'any' so that the underlying VAR_DECL is deemed
9720 : : as potentially-constant even though it wasn't declared
9721 : : constexpr. */;
9722 : : else
9723 : : return false;
9724 : : }
9725 : 241050679 : for (; i < nargs; ++i)
9726 : : {
9727 : 132080715 : tree x = get_nth_callarg (t, i);
9728 : : /* In a template, reference arguments haven't been converted to
9729 : : REFERENCE_TYPE and we might not even know if the parameter
9730 : : is a reference, so accept lvalue constants too. */
9731 : 132080715 : bool rv = processing_template_decl ? any : rval;
9732 : : /* Don't require an immediately constant value, as constexpr
9733 : : substitution might not use the value of the argument. */
9734 : 132080715 : bool sub_now = false;
9735 : 132080715 : if (!potential_constant_expression_1 (x, rv, strict,
9736 : : sub_now, fundef_p, flags,
9737 : : jump_target))
9738 : : return false;
9739 : : }
9740 : : return true;
9741 : : }
9742 : :
9743 : 101474875 : case NON_LVALUE_EXPR:
9744 : : /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
9745 : : -- an lvalue of integral type that refers to a non-volatile
9746 : : const variable or static data member initialized with
9747 : : constant expressions, or
9748 : :
9749 : : -- an lvalue of literal type that refers to non-volatile
9750 : : object defined with constexpr, or that refers to a
9751 : : sub-object of such an object; */
9752 : 101474875 : return RECUR (TREE_OPERAND (t, 0), rval);
9753 : :
9754 : 21205 : case EXCESS_PRECISION_EXPR:
9755 : 21205 : return RECUR (TREE_OPERAND (t, 0), rval);
9756 : :
9757 : 210945472 : case VAR_DECL:
9758 : 210945472 : if (DECL_HAS_VALUE_EXPR_P (t))
9759 : : {
9760 : 2755363 : if (now && is_normal_capture_proxy (t))
9761 : : {
9762 : : /* -- in a lambda-expression, a reference to this or to a
9763 : : variable with automatic storage duration defined outside that
9764 : : lambda-expression, where the reference would be an
9765 : : odr-use. */
9766 : :
9767 : 297639 : if (want_rval)
9768 : : /* Since we're doing an lvalue-rvalue conversion, this might
9769 : : not be an odr-use, so evaluate the variable directly. */
9770 : 297157 : return RECUR (DECL_CAPTURED_VARIABLE (t), rval);
9771 : :
9772 : 482 : if (flags & tf_error)
9773 : : {
9774 : 2 : tree cap = DECL_CAPTURED_VARIABLE (t);
9775 : 2 : auto_diagnostic_group d;
9776 : 2 : if (constexpr_error (input_location, fundef_p,
9777 : : "lambda capture of %qE is not a "
9778 : : "constant expression", cap)
9779 : 2 : && decl_constant_var_p (cap))
9780 : 2 : inform (input_location, "because it is used as a glvalue");
9781 : 2 : }
9782 : 482 : return false;
9783 : : }
9784 : : /* Treat __PRETTY_FUNCTION__ inside a template function as
9785 : : potentially-constant. */
9786 : 4914259 : else if (DECL_PRETTY_FUNCTION_P (t)
9787 : 2529345 : && DECL_VALUE_EXPR (t) == error_mark_node)
9788 : : return true;
9789 : 2457719 : return RECUR (DECL_VALUE_EXPR (t), rval);
9790 : : }
9791 : 208190109 : if (want_rval
9792 : 145670174 : && (now || !var_in_maybe_constexpr_fn (t))
9793 : 138708775 : && !type_dependent_expression_p (t)
9794 : 108751153 : && !decl_maybe_constant_var_p (t)
9795 : 45140594 : && (strict
9796 : 409306 : || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
9797 : 339716 : || (DECL_INITIAL (t)
9798 : 339433 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
9799 : 45139891 : && COMPLETE_TYPE_P (TREE_TYPE (t))
9800 : 253315142 : && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
9801 : : {
9802 : 45121787 : if (flags & tf_error)
9803 : 128 : non_const_var_error (loc, t, fundef_p);
9804 : 45121787 : return false;
9805 : : }
9806 : : return true;
9807 : :
9808 : 175907191 : case NOP_EXPR:
9809 : 175907191 : if (REINTERPRET_CAST_P (t))
9810 : : {
9811 : 50017 : if (flags & tf_error)
9812 : 42 : constexpr_error (loc, fundef_p, "%<reinterpret_cast%> is not a "
9813 : : "constant expression");
9814 : 50017 : return false;
9815 : : }
9816 : : /* FALLTHRU */
9817 : 466398740 : case CONVERT_EXPR:
9818 : 466398740 : case VIEW_CONVERT_EXPR:
9819 : : /* -- a reinterpret_cast. FIXME not implemented, and this rule
9820 : : may change to something more specific to type-punning (DR 1312). */
9821 : 466398740 : {
9822 : 466398740 : tree from = TREE_OPERAND (t, 0);
9823 : 466398740 : if (location_wrapper_p (t))
9824 : : {
9825 : 260371754 : iloc_sentinel ils = loc;
9826 : 260371754 : return (RECUR (from, want_rval));
9827 : 260371754 : }
9828 : 206026986 : if (INDIRECT_TYPE_P (TREE_TYPE (t)))
9829 : : {
9830 : 81660160 : STRIP_ANY_LOCATION_WRAPPER (from);
9831 : 81660160 : if (TREE_CODE (from) == INTEGER_CST
9832 : 81660160 : && !integer_zerop (from))
9833 : : {
9834 : 1144 : if (flags & tf_error)
9835 : 21 : constexpr_error (loc, fundef_p,
9836 : : "%<reinterpret_cast%> from integer to "
9837 : : "pointer");
9838 : 1144 : return false;
9839 : : }
9840 : : }
9841 : 206025842 : return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
9842 : : }
9843 : :
9844 : 11514 : case ADDRESSOF_EXPR:
9845 : : /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
9846 : 11514 : t = TREE_OPERAND (t, 0);
9847 : 11514 : goto handle_addr_expr;
9848 : :
9849 : 37483985 : case ADDR_EXPR:
9850 : : /* -- a unary operator & that is applied to an lvalue that
9851 : : designates an object with thread or automatic storage
9852 : : duration; */
9853 : 37483985 : t = TREE_OPERAND (t, 0);
9854 : :
9855 : 37483985 : if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
9856 : : /* A pointer-to-member constant. */
9857 : : return true;
9858 : :
9859 : 37495168 : handle_addr_expr:
9860 : : #if 0
9861 : : /* FIXME adjust when issue 1197 is fully resolved. For now don't do
9862 : : any checking here, as we might dereference the pointer later. If
9863 : : we remove this code, also remove check_automatic_or_tls. */
9864 : : i = check_automatic_or_tls (t);
9865 : : if (i == ck_ok)
9866 : : return true;
9867 : : if (i == ck_bad)
9868 : : {
9869 : : if (flags & tf_error)
9870 : : error ("address-of an object %qE with thread local or "
9871 : : "automatic storage is not a constant expression", t);
9872 : : return false;
9873 : : }
9874 : : #endif
9875 : 37495168 : return RECUR (t, any);
9876 : :
9877 : 58058588 : case COMPONENT_REF:
9878 : 58058588 : case ARROW_EXPR:
9879 : 58058588 : case OFFSET_REF:
9880 : : /* -- a class member access unless its postfix-expression is
9881 : : of literal type or of pointer to literal type. */
9882 : : /* This test would be redundant, as it follows from the
9883 : : postfix-expression being a potential constant expression. */
9884 : 58058588 : if (type_unknown_p (t))
9885 : : return true;
9886 : 52310542 : if (is_overloaded_fn (t))
9887 : : /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
9888 : : which uses ob as an lvalue. */
9889 : 53609367 : want_rval = false;
9890 : 53609367 : gcc_fallthrough ();
9891 : :
9892 : 53609367 : case REALPART_EXPR:
9893 : 53609367 : case IMAGPART_EXPR:
9894 : 53609367 : case BIT_FIELD_REF:
9895 : 53609367 : return RECUR (TREE_OPERAND (t, 0), want_rval);
9896 : :
9897 : 150050 : case EXPR_PACK_EXPANSION:
9898 : 150050 : return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
9899 : :
9900 : 57367360 : case INDIRECT_REF:
9901 : 57367360 : {
9902 : 57367360 : tree x = TREE_OPERAND (t, 0);
9903 : 57367360 : STRIP_NOPS (x);
9904 : 57367360 : if (is_this_parameter (x) && !is_capture_proxy (x))
9905 : : {
9906 : 22586096 : if (now || !var_in_maybe_constexpr_fn (x))
9907 : : {
9908 : 15728646 : if (flags & tf_error)
9909 : 9 : constexpr_error (loc, fundef_p, "use of %<this%> in a "
9910 : : "constant expression");
9911 : 15728646 : return false;
9912 : : }
9913 : : return true;
9914 : : }
9915 : 34781264 : return RECUR (x, rval);
9916 : : }
9917 : :
9918 : 13302596 : case STATEMENT_LIST:
9919 : 45646346 : for (tree stmt : tsi_range (t))
9920 : 32753822 : if (!RECUR (stmt, any))
9921 : 245522749 : return false;
9922 : : return true;
9923 : :
9924 : 3092425 : case MODIFY_EXPR:
9925 : 3092425 : if (cxx_dialect < cxx14)
9926 : 1927 : goto fail;
9927 : 3090498 : if (!RECUR (TREE_OPERAND (t, 0), any))
9928 : : return false;
9929 : : /* Just ignore clobbers. */
9930 : 2701359 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
9931 : : return true;
9932 : 1301063 : if (!RECUR (TREE_OPERAND (t, 1), rval))
9933 : : return false;
9934 : : return true;
9935 : :
9936 : 149575 : case MODOP_EXPR:
9937 : 149575 : if (cxx_dialect < cxx14)
9938 : 96 : goto fail;
9939 : 149479 : if (!RECUR (TREE_OPERAND (t, 0), rval))
9940 : : return false;
9941 : 138671 : if (!RECUR (TREE_OPERAND (t, 2), rval))
9942 : : return false;
9943 : : return true;
9944 : :
9945 : 222691 : case DO_STMT:
9946 : 222691 : if (!RECUR (DO_COND (t), rval))
9947 : : return false;
9948 : 222691 : if (!RECUR (DO_BODY (t), any))
9949 : : return false;
9950 : 222642 : if (breaks (jump_target) || continues (jump_target))
9951 : 3 : *jump_target = NULL_TREE;
9952 : : return true;
9953 : :
9954 : 186345 : case FOR_STMT:
9955 : 186345 : if (!RECUR (FOR_INIT_STMT (t), any))
9956 : : return false;
9957 : 186345 : tmp = FOR_COND (t);
9958 : 186345 : if (!RECUR (tmp, rval))
9959 : : return false;
9960 : 186276 : if (tmp)
9961 : : {
9962 : 142801 : if (!processing_template_decl)
9963 : 141680 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
9964 : : /* If we couldn't evaluate the condition, it might not ever be
9965 : : true. */
9966 : 142801 : if (!integer_onep (tmp))
9967 : : {
9968 : : /* Before returning true, check if the for body can contain
9969 : : a return. */
9970 : 142801 : hash_set<tree> pset;
9971 : 142801 : check_for_return_continue_data data = { &pset, NULL_TREE,
9972 : 142801 : NULL_TREE };
9973 : 142801 : if (tree ret_expr
9974 : 142801 : = cp_walk_tree (&FOR_BODY (t), check_for_return_continue,
9975 : : &data, &pset))
9976 : 90571 : *jump_target = ret_expr;
9977 : 142801 : return true;
9978 : 142801 : }
9979 : : }
9980 : 43475 : if (!RECUR (FOR_EXPR (t), any))
9981 : : return false;
9982 : 43475 : if (!RECUR (FOR_BODY (t), any))
9983 : : return false;
9984 : 43475 : if (breaks (jump_target) || continues (jump_target))
9985 : 3 : *jump_target = NULL_TREE;
9986 : : return true;
9987 : :
9988 : 21 : case RANGE_FOR_STMT:
9989 : 21 : if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
9990 : : return false;
9991 : 21 : if (!RECUR (RANGE_FOR_EXPR (t), any))
9992 : : return false;
9993 : 21 : if (!RECUR (RANGE_FOR_BODY (t), any))
9994 : : return false;
9995 : 15 : if (breaks (jump_target) || continues (jump_target))
9996 : 0 : *jump_target = NULL_TREE;
9997 : : return true;
9998 : :
9999 : 80422 : case WHILE_STMT:
10000 : 80422 : tmp = WHILE_COND (t);
10001 : 80422 : if (!RECUR (tmp, rval))
10002 : : return false;
10003 : 80336 : if (!processing_template_decl)
10004 : 80326 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
10005 : : /* If we couldn't evaluate the condition, it might not ever be true. */
10006 : 80336 : if (!integer_onep (tmp))
10007 : : {
10008 : : /* Before returning true, check if the while body can contain
10009 : : a return. */
10010 : 78221 : hash_set<tree> pset;
10011 : 78221 : check_for_return_continue_data data = { &pset, NULL_TREE,
10012 : 78221 : NULL_TREE };
10013 : 78221 : if (tree ret_expr
10014 : 78221 : = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue,
10015 : : &data, &pset))
10016 : 80 : *jump_target = ret_expr;
10017 : 78221 : return true;
10018 : 78221 : }
10019 : 2115 : if (!RECUR (WHILE_BODY (t), any))
10020 : : return false;
10021 : 2111 : if (breaks (jump_target) || continues (jump_target))
10022 : 26 : *jump_target = NULL_TREE;
10023 : : return true;
10024 : :
10025 : 8217 : case SWITCH_STMT:
10026 : 8217 : if (!RECUR (SWITCH_STMT_COND (t), rval))
10027 : : return false;
10028 : : /* FIXME we don't check SWITCH_STMT_BODY currently, because even
10029 : : unreachable labels would be checked and it is enough if there is
10030 : : a single switch cond value for which it is a valid constant
10031 : : expression. We need to check if there are any RETURN_EXPRs
10032 : : or CONTINUE_STMTs inside of the body though, as in that case
10033 : : we need to set *jump_target. */
10034 : : else
10035 : : {
10036 : 8211 : hash_set<tree> pset;
10037 : 8211 : check_for_return_continue_data data = { &pset, NULL_TREE,
10038 : 8211 : NULL_TREE };
10039 : 8211 : if (tree ret_expr
10040 : 8211 : = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
10041 : : &data, &pset))
10042 : : /* The switch might return. */
10043 : 8172 : *jump_target = ret_expr;
10044 : 39 : else if (data.continue_stmt)
10045 : : /* The switch can't return, but might continue. */
10046 : 3 : *jump_target = data.continue_stmt;
10047 : 8211 : }
10048 : 8211 : return true;
10049 : :
10050 : 52 : case STMT_EXPR:
10051 : 52 : return RECUR (STMT_EXPR_STMT (t), rval);
10052 : :
10053 : 192637 : case LAMBDA_EXPR:
10054 : 192637 : if (cxx_dialect >= cxx17)
10055 : : /* In C++17 lambdas can be constexpr, don't give up yet. */
10056 : : return true;
10057 : 322 : else if (flags & tf_error)
10058 : 0 : constexpr_error (loc, fundef_p, "lambda-expression is not a "
10059 : : "constant expression before C++17");
10060 : : return false;
10061 : :
10062 : 114518 : case NEW_EXPR:
10063 : 114518 : case VEC_NEW_EXPR:
10064 : 114518 : case DELETE_EXPR:
10065 : 114518 : case VEC_DELETE_EXPR:
10066 : 114518 : if (cxx_dialect >= cxx20)
10067 : : /* In C++20, new-expressions are potentially constant. */
10068 : : return true;
10069 : 89633 : else if (flags & tf_error)
10070 : 0 : constexpr_error (loc, fundef_p, "new-expression is not a "
10071 : : "constant expression before C++20");
10072 : : return false;
10073 : :
10074 : 65172 : case DYNAMIC_CAST_EXPR:
10075 : 65172 : case PSEUDO_DTOR_EXPR:
10076 : 65172 : case THROW_EXPR:
10077 : 65172 : case OMP_PARALLEL:
10078 : 65172 : case OMP_TASK:
10079 : 65172 : case OMP_FOR:
10080 : 65172 : case OMP_SIMD:
10081 : 65172 : case OMP_DISTRIBUTE:
10082 : 65172 : case OMP_TASKLOOP:
10083 : 65172 : case OMP_LOOP:
10084 : 65172 : case OMP_TEAMS:
10085 : 65172 : case OMP_TARGET_DATA:
10086 : 65172 : case OMP_TARGET:
10087 : 65172 : case OMP_SECTIONS:
10088 : 65172 : case OMP_ORDERED:
10089 : 65172 : case OMP_CRITICAL:
10090 : 65172 : case OMP_SINGLE:
10091 : 65172 : case OMP_SCAN:
10092 : 65172 : case OMP_SCOPE:
10093 : 65172 : case OMP_SECTION:
10094 : 65172 : case OMP_MASTER:
10095 : 65172 : case OMP_MASKED:
10096 : 65172 : case OMP_TASKGROUP:
10097 : 65172 : case OMP_TARGET_UPDATE:
10098 : 65172 : case OMP_TARGET_ENTER_DATA:
10099 : 65172 : case OMP_TARGET_EXIT_DATA:
10100 : 65172 : case OMP_ATOMIC:
10101 : 65172 : case OMP_ATOMIC_READ:
10102 : 65172 : case OMP_ATOMIC_CAPTURE_OLD:
10103 : 65172 : case OMP_ATOMIC_CAPTURE_NEW:
10104 : 65172 : case OMP_DEPOBJ:
10105 : 65172 : case OACC_PARALLEL:
10106 : 65172 : case OACC_KERNELS:
10107 : 65172 : case OACC_SERIAL:
10108 : 65172 : case OACC_DATA:
10109 : 65172 : case OACC_HOST_DATA:
10110 : 65172 : case OACC_LOOP:
10111 : 65172 : case OACC_CACHE:
10112 : 65172 : case OACC_DECLARE:
10113 : 65172 : case OACC_ENTER_DATA:
10114 : 65172 : case OACC_EXIT_DATA:
10115 : 65172 : case OACC_UPDATE:
10116 : 65172 : case OMP_ARRAY_SECTION:
10117 : : /* GCC internal stuff. */
10118 : 65172 : case VA_ARG_EXPR:
10119 : 65172 : case TRANSACTION_EXPR:
10120 : 65172 : case AT_ENCODE_EXPR:
10121 : 65172 : fail:
10122 : 65172 : if (flags & tf_error)
10123 : 40 : constexpr_error (loc, fundef_p, "expression %qE is not a constant "
10124 : : "expression", t);
10125 : : return false;
10126 : :
10127 : 661 : case ASM_EXPR:
10128 : 661 : if (flags & tf_error)
10129 : 1 : inline_asm_in_constexpr_error (loc, fundef_p);
10130 : : return false;
10131 : :
10132 : 296417 : case OBJ_TYPE_REF:
10133 : 296417 : if (cxx_dialect >= cxx20)
10134 : : /* In C++20 virtual calls can be constexpr, don't give up yet. */
10135 : : return true;
10136 : 210457 : else if (flags & tf_error)
10137 : 0 : constexpr_error (loc, fundef_p, "virtual functions cannot be "
10138 : : "%<constexpr%> before C++20");
10139 : : return false;
10140 : :
10141 : 2464 : case TYPEID_EXPR:
10142 : : /* In C++20, a typeid expression whose operand is of polymorphic
10143 : : class type can be constexpr. */
10144 : 2464 : {
10145 : 2464 : tree e = TREE_OPERAND (t, 0);
10146 : 2464 : if (cxx_dialect < cxx20
10147 : 271 : && strict
10148 : 271 : && !TYPE_P (e)
10149 : 25 : && !type_dependent_expression_p (e)
10150 : 2470 : && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
10151 : : {
10152 : 4 : if (flags & tf_error)
10153 : 1 : constexpr_error (loc, fundef_p, "%<typeid%> is not a "
10154 : : "constant expression because %qE is "
10155 : : "of polymorphic type", e);
10156 : 4 : return false;
10157 : : }
10158 : : return true;
10159 : : }
10160 : :
10161 : 24230870 : case POINTER_DIFF_EXPR:
10162 : 24230870 : case MINUS_EXPR:
10163 : 24230870 : want_rval = true;
10164 : 24230870 : goto binary;
10165 : :
10166 : 61520635 : case LT_EXPR:
10167 : 61520635 : case LE_EXPR:
10168 : 61520635 : case GT_EXPR:
10169 : 61520635 : case GE_EXPR:
10170 : 61520635 : case EQ_EXPR:
10171 : 61520635 : case NE_EXPR:
10172 : 61520635 : case SPACESHIP_EXPR:
10173 : 61520635 : want_rval = true;
10174 : 61520635 : goto binary;
10175 : :
10176 : 1207119 : case PREINCREMENT_EXPR:
10177 : 1207119 : case POSTINCREMENT_EXPR:
10178 : 1207119 : case PREDECREMENT_EXPR:
10179 : 1207119 : case POSTDECREMENT_EXPR:
10180 : 1207119 : if (cxx_dialect < cxx14)
10181 : 9086 : goto fail;
10182 : 1198033 : goto unary;
10183 : :
10184 : 933346 : case BIT_NOT_EXPR:
10185 : : /* A destructor. */
10186 : 933346 : if (TYPE_P (TREE_OPERAND (t, 0)))
10187 : : return true;
10188 : : /* fall through. */
10189 : :
10190 : 43795321 : case CONJ_EXPR:
10191 : 43795321 : case SAVE_EXPR:
10192 : 43795321 : case FIX_TRUNC_EXPR:
10193 : 43795321 : case FLOAT_EXPR:
10194 : 43795321 : case NEGATE_EXPR:
10195 : 43795321 : case ABS_EXPR:
10196 : 43795321 : case ABSU_EXPR:
10197 : 43795321 : case TRUTH_NOT_EXPR:
10198 : 43795321 : case FIXED_CONVERT_EXPR:
10199 : 43795321 : case UNARY_PLUS_EXPR:
10200 : 43795321 : case UNARY_LEFT_FOLD_EXPR:
10201 : 43795321 : case UNARY_RIGHT_FOLD_EXPR:
10202 : 933340 : unary:
10203 : 43795321 : return RECUR (TREE_OPERAND (t, 0), rval);
10204 : :
10205 : 26124602 : case CAST_EXPR:
10206 : 26124602 : case CONST_CAST_EXPR:
10207 : 26124602 : case STATIC_CAST_EXPR:
10208 : 26124602 : case REINTERPRET_CAST_EXPR:
10209 : 26124602 : case IMPLICIT_CONV_EXPR:
10210 : 26124602 : if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t)))
10211 : : /* In C++98, a conversion to non-integral type can't be part of a
10212 : : constant expression. */
10213 : : {
10214 : 357 : if (flags & tf_error)
10215 : 0 : constexpr_error (loc, fundef_p,
10216 : : "cast to non-integral type %qT in a constant "
10217 : 0 : "expression", TREE_TYPE (t));
10218 : 357 : return false;
10219 : : }
10220 : : /* This might be a conversion from a class to a (potentially) literal
10221 : : type. Let's consider it potentially constant since the conversion
10222 : : might be a constexpr user-defined conversion. */
10223 : 26124245 : else if (cxx_dialect >= cxx11
10224 : 26103685 : && (dependent_type_p (TREE_TYPE (t))
10225 : 6648437 : || !COMPLETE_TYPE_P (TREE_TYPE (t))
10226 : 6648370 : || literal_type_p (TREE_TYPE (t)))
10227 : 52219656 : && TREE_OPERAND (t, 0))
10228 : : {
10229 : 25878402 : tree type = TREE_TYPE (TREE_OPERAND (t, 0));
10230 : : /* If this is a dependent type, it could end up being a class
10231 : : with conversions. */
10232 : 25878402 : if (type == NULL_TREE || WILDCARD_TYPE_P (type))
10233 : : return true;
10234 : : /* Or a non-dependent class which has conversions. */
10235 : 156970 : else if (CLASS_TYPE_P (type)
10236 : 156970 : && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
10237 : 10374 : return true;
10238 : : }
10239 : :
10240 : 3105782 : return (RECUR (TREE_OPERAND (t, 0),
10241 : 3105782 : !TYPE_REF_P (TREE_TYPE (t))));
10242 : :
10243 : 5934034 : case BIND_EXPR:
10244 : 5934034 : return RECUR (BIND_EXPR_BODY (t), want_rval);
10245 : :
10246 : 33093314 : case CLEANUP_POINT_EXPR:
10247 : 33093314 : case MUST_NOT_THROW_EXPR:
10248 : 33093314 : case TRY_CATCH_EXPR:
10249 : 33093314 : case TRY_BLOCK:
10250 : 33093314 : case EH_SPEC_BLOCK:
10251 : 33093314 : case EXPR_STMT:
10252 : 33093314 : case PAREN_EXPR:
10253 : : /* For convenience. */
10254 : 33093314 : case LOOP_EXPR:
10255 : 33093314 : case EXIT_EXPR:
10256 : 33093314 : return RECUR (TREE_OPERAND (t, 0), want_rval);
10257 : :
10258 : 3482721 : case DECL_EXPR:
10259 : 3482721 : tmp = DECL_EXPR_DECL (t);
10260 : 3433113 : if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)
10261 : 5701188 : && (processing_template_decl
10262 : 1948468 : ? !decl_maybe_constant_var_p (tmp)
10263 : 1678469 : : !decl_constant_var_p (tmp)))
10264 : : {
10265 : 1672572 : if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
10266 : : {
10267 : 5 : if (flags & tf_error)
10268 : 2 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
10269 : : "%qD defined %<thread_local%> in "
10270 : : "%<constexpr%> context", tmp);
10271 : 5 : return false;
10272 : : }
10273 : 1672567 : else if (TREE_STATIC (tmp))
10274 : : {
10275 : 64 : if (flags & tf_error)
10276 : 9 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
10277 : : "%qD defined %<static%> in %<constexpr%> "
10278 : : "context", tmp);
10279 : 64 : return false;
10280 : : }
10281 : 1672503 : else if (!check_for_uninitialized_const_var
10282 : 1672503 : (tmp, /*constexpr_context_p=*/true, flags))
10283 : : return false;
10284 : : }
10285 : 3478924 : if (VAR_P (tmp))
10286 : 3429316 : return RECUR (DECL_INITIAL (tmp), want_rval);
10287 : : return true;
10288 : :
10289 : 2269 : case TRY_FINALLY_EXPR:
10290 : 2269 : return (RECUR (TREE_OPERAND (t, 0), want_rval)
10291 : 2269 : && RECUR (TREE_OPERAND (t, 1), any));
10292 : :
10293 : 24094380 : case SCOPE_REF:
10294 : 24094380 : return RECUR (TREE_OPERAND (t, 1), want_rval);
10295 : :
10296 : 16440423 : case TARGET_EXPR:
10297 : 16440423 : if (!TARGET_EXPR_DIRECT_INIT_P (t)
10298 : 16408989 : && !TARGET_EXPR_ELIDING_P (t)
10299 : 30634882 : && !literal_type_p (TREE_TYPE (t)))
10300 : : {
10301 : 1276610 : if (flags & tf_error)
10302 : : {
10303 : 17 : auto_diagnostic_group d;
10304 : 17 : if (constexpr_error (loc, fundef_p,
10305 : : "temporary of non-literal type %qT in a "
10306 : 17 : "constant expression", TREE_TYPE (t)))
10307 : 17 : explain_non_literal_class (TREE_TYPE (t));
10308 : 17 : }
10309 : 1276610 : return false;
10310 : : }
10311 : : /* FALLTHRU */
10312 : 29709864 : case INIT_EXPR:
10313 : 29709864 : return RECUR (TREE_OPERAND (t, 1), rval);
10314 : :
10315 : 19159882 : case CONSTRUCTOR:
10316 : 19159882 : {
10317 : 19159882 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
10318 : 19159882 : constructor_elt *ce;
10319 : 64928296 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
10320 : 46353439 : if (!RECUR (ce->value, want_rval))
10321 : : return false;
10322 : : return true;
10323 : : }
10324 : :
10325 : 97694 : case TREE_LIST:
10326 : 97694 : {
10327 : 97694 : gcc_assert (TREE_PURPOSE (t) == NULL_TREE
10328 : : || DECL_P (TREE_PURPOSE (t)));
10329 : 97694 : if (!RECUR (TREE_VALUE (t), want_rval))
10330 : : return false;
10331 : 40219 : if (TREE_CHAIN (t) == NULL_TREE)
10332 : : return true;
10333 : 8312 : return RECUR (TREE_CHAIN (t), want_rval);
10334 : : }
10335 : :
10336 : 9516798 : case TRUNC_DIV_EXPR:
10337 : 9516798 : case CEIL_DIV_EXPR:
10338 : 9516798 : case FLOOR_DIV_EXPR:
10339 : 9516798 : case ROUND_DIV_EXPR:
10340 : 9516798 : case TRUNC_MOD_EXPR:
10341 : 9516798 : case CEIL_MOD_EXPR:
10342 : 9516798 : case ROUND_MOD_EXPR:
10343 : 9516798 : {
10344 : 9516798 : tree denom = TREE_OPERAND (t, 1);
10345 : 9516798 : if (!RECUR (denom, rval))
10346 : : return false;
10347 : : /* We can't call cxx_eval_outermost_constant_expr on an expression
10348 : : that hasn't been through instantiate_non_dependent_expr yet. */
10349 : 9203238 : if (!processing_template_decl)
10350 : 3727838 : denom = cxx_eval_outermost_constant_expr (denom, true);
10351 : 9203238 : if (integer_zerop (denom))
10352 : : {
10353 : 460 : if (flags & tf_error)
10354 : 50 : constexpr_error (input_location, fundef_p,
10355 : : "division by zero is not a constant expression");
10356 : 460 : return false;
10357 : : }
10358 : : else
10359 : : {
10360 : 9202778 : want_rval = true;
10361 : 9202778 : return RECUR (TREE_OPERAND (t, 0), want_rval);
10362 : : }
10363 : : }
10364 : :
10365 : 3895700 : case COMPOUND_EXPR:
10366 : 3895700 : {
10367 : : /* check_return_expr sometimes wraps a TARGET_EXPR in a
10368 : : COMPOUND_EXPR; don't get confused. */
10369 : 3895700 : tree op0 = TREE_OPERAND (t, 0);
10370 : 3895700 : tree op1 = TREE_OPERAND (t, 1);
10371 : 3895700 : STRIP_NOPS (op1);
10372 : 3895700 : if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
10373 : 480676 : return RECUR (op0, want_rval);
10374 : : else
10375 : 3415024 : goto binary;
10376 : : }
10377 : :
10378 : : /* If the first operand is the non-short-circuit constant, look at
10379 : : the second operand; otherwise we only care about the first one for
10380 : : potentiality. */
10381 : 15993743 : case TRUTH_AND_EXPR:
10382 : 15993743 : case TRUTH_ANDIF_EXPR:
10383 : 15993743 : tmp = boolean_true_node;
10384 : 15993743 : goto truth;
10385 : 7486705 : case TRUTH_OR_EXPR:
10386 : 7486705 : case TRUTH_ORIF_EXPR:
10387 : 7486705 : tmp = boolean_false_node;
10388 : 23480448 : truth:
10389 : 23480448 : {
10390 : 23480448 : tree op0 = TREE_OPERAND (t, 0);
10391 : 23480448 : tree op1 = TREE_OPERAND (t, 1);
10392 : 23480448 : if (!RECUR (op0, rval))
10393 : : return false;
10394 : 14858561 : if (!(flags & tf_error) && RECUR (op1, rval))
10395 : : /* When quiet, try to avoid expensive trial evaluation by first
10396 : : checking potentiality of the second operand. */
10397 : : return true;
10398 : 623005 : if (!processing_template_decl)
10399 : 383018 : op0 = cxx_eval_outermost_constant_expr (op0, true);
10400 : 623005 : if (tree_int_cst_equal (op0, tmp))
10401 : 6353 : return (flags & tf_error) ? RECUR (op1, rval) : false;
10402 : : else
10403 : : return true;
10404 : : }
10405 : :
10406 : : case PLUS_EXPR:
10407 : : case MULT_EXPR:
10408 : : case POINTER_PLUS_EXPR:
10409 : : case RDIV_EXPR:
10410 : : case EXACT_DIV_EXPR:
10411 : : case MIN_EXPR:
10412 : : case MAX_EXPR:
10413 : : case LSHIFT_EXPR:
10414 : : case RSHIFT_EXPR:
10415 : : case LROTATE_EXPR:
10416 : : case RROTATE_EXPR:
10417 : : case BIT_IOR_EXPR:
10418 : : case BIT_XOR_EXPR:
10419 : : case BIT_AND_EXPR:
10420 : : case TRUTH_XOR_EXPR:
10421 : : case UNORDERED_EXPR:
10422 : : case ORDERED_EXPR:
10423 : : case UNLT_EXPR:
10424 : : case UNLE_EXPR:
10425 : : case UNGT_EXPR:
10426 : : case UNGE_EXPR:
10427 : : case UNEQ_EXPR:
10428 : : case LTGT_EXPR:
10429 : : case RANGE_EXPR:
10430 : : case COMPLEX_EXPR:
10431 : 191002713 : want_rval = true;
10432 : : /* Fall through. */
10433 : 191002713 : case ARRAY_REF:
10434 : 191002713 : case ARRAY_RANGE_REF:
10435 : 191002713 : case MEMBER_REF:
10436 : 191002713 : case DOTSTAR_EXPR:
10437 : 191002713 : case MEM_REF:
10438 : 191002713 : case BINARY_LEFT_FOLD_EXPR:
10439 : 191002713 : case BINARY_RIGHT_FOLD_EXPR:
10440 : 191002713 : binary:
10441 : 397426049 : for (i = 0; i < 2; ++i)
10442 : 299436849 : if (!RECUR (TREE_OPERAND (t, i), want_rval))
10443 : : return false;
10444 : : return true;
10445 : :
10446 : : case VEC_PERM_EXPR:
10447 : 94383 : for (i = 0; i < 3; ++i)
10448 : 94356 : if (!RECUR (TREE_OPERAND (t, i), true))
10449 : : return false;
10450 : : return true;
10451 : :
10452 : 4205446 : case COND_EXPR:
10453 : 4205446 : if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20)
10454 : : {
10455 : 8 : if (flags & tf_error)
10456 : 2 : constexpr_error (loc, fundef_p, "%<delete[]%> is not a "
10457 : : "constant expression");
10458 : 8 : return false;
10459 : : }
10460 : : /* Fall through. */
10461 : 7618495 : case IF_STMT:
10462 : 7618495 : case VEC_COND_EXPR:
10463 : : /* If the condition is a known constant, we know which of the legs we
10464 : : care about; otherwise we only require that the condition and
10465 : : either of the legs be potentially constant. */
10466 : 7618495 : tmp = TREE_OPERAND (t, 0);
10467 : 7618495 : if (!RECUR (tmp, rval))
10468 : : return false;
10469 : 6412644 : if (!processing_template_decl)
10470 : 5369072 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
10471 : : /* potential_constant_expression* isn't told if it is called for
10472 : : manifestly_const_eval or not, so for consteval if always
10473 : : process both branches as if the condition is not a known
10474 : : constant. */
10475 : 6412644 : if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t))
10476 : : {
10477 : 6409937 : if (integer_zerop (tmp))
10478 : 1439209 : return RECUR (TREE_OPERAND (t, 2), want_rval);
10479 : 4970728 : else if (TREE_CODE (tmp) == INTEGER_CST)
10480 : 1594054 : return RECUR (TREE_OPERAND (t, 1), want_rval);
10481 : : }
10482 : 3379381 : tmp = *jump_target;
10483 : 3901856 : for (i = 1; i < 3; ++i)
10484 : : {
10485 : 3833541 : tree this_jump_target = tmp;
10486 : 3833541 : if (potential_constant_expression_1 (TREE_OPERAND (t, i),
10487 : : want_rval, strict, now, fundef_p,
10488 : : tf_none, &this_jump_target))
10489 : : {
10490 : 3311066 : if (returns (&this_jump_target))
10491 : 810006 : *jump_target = this_jump_target;
10492 : 2501060 : else if (!returns (jump_target))
10493 : : {
10494 : 2501060 : if (breaks (&this_jump_target)
10495 : 2501060 : || continues (&this_jump_target))
10496 : 50 : *jump_target = this_jump_target;
10497 : 2501060 : if (i == 1)
10498 : : {
10499 : : /* If the then branch is potentially constant, but
10500 : : does not return, check if the else branch
10501 : : couldn't return, break or continue. */
10502 : 2115373 : hash_set<tree> pset;
10503 : 2115373 : check_for_return_continue_data data = { &pset, NULL_TREE,
10504 : 2115373 : NULL_TREE };
10505 : 4230746 : if (tree ret_expr
10506 : 2115373 : = cp_walk_tree (&TREE_OPERAND (t, 2),
10507 : : check_for_return_continue, &data,
10508 : : &pset))
10509 : 13567 : *jump_target = ret_expr;
10510 : 2101806 : else if (*jump_target == NULL_TREE)
10511 : : {
10512 : 2101759 : if (data.continue_stmt)
10513 : 0 : *jump_target = data.continue_stmt;
10514 : 2101759 : else if (data.break_stmt)
10515 : 8 : *jump_target = data.break_stmt;
10516 : : }
10517 : 2115373 : }
10518 : : }
10519 : 3311066 : return true;
10520 : : }
10521 : : }
10522 : 68315 : if (flags & tf_error)
10523 : : {
10524 : 3 : if (TREE_CODE (t) == IF_STMT)
10525 : 3 : constexpr_error (loc, fundef_p, "neither branch of %<if%> is a "
10526 : : "constant expression");
10527 : : else
10528 : 0 : constexpr_error (loc, fundef_p, "expression %qE is not a "
10529 : : "constant expression", t);
10530 : : }
10531 : : return false;
10532 : :
10533 : 878 : case VEC_INIT_EXPR:
10534 : 878 : if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
10535 : : return true;
10536 : 388 : if (flags & tf_error)
10537 : : {
10538 : 3 : if (constexpr_error (loc, fundef_p, "non-constant array "
10539 : : "initialization"))
10540 : 3 : diagnose_non_constexpr_vec_init (t);
10541 : : }
10542 : : return false;
10543 : :
10544 : : case TYPE_DECL:
10545 : : case TAG_DEFN:
10546 : : /* We can see these in statement-expressions. */
10547 : : return true;
10548 : :
10549 : 382048 : case CLEANUP_STMT:
10550 : 382048 : if (!RECUR (CLEANUP_BODY (t), any))
10551 : : return false;
10552 : 381929 : if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
10553 : : return false;
10554 : : return true;
10555 : :
10556 : : case EMPTY_CLASS_EXPR:
10557 : : return true;
10558 : :
10559 : 8 : case GOTO_EXPR:
10560 : 8 : {
10561 : 8 : tree *target = &TREE_OPERAND (t, 0);
10562 : : /* Gotos representing break, continue and cdtor return are OK. */
10563 : 8 : if (breaks (target) || continues (target) || returns (target))
10564 : : {
10565 : 0 : *jump_target = *target;
10566 : 0 : return true;
10567 : : }
10568 : 8 : if (flags & tf_error)
10569 : 2 : constexpr_error (loc, fundef_p, "%<goto%> is not a constant "
10570 : : "expression");
10571 : : return false;
10572 : : }
10573 : :
10574 : 46 : case ASSERTION_STMT:
10575 : 46 : case PRECONDITION_STMT:
10576 : 46 : case POSTCONDITION_STMT:
10577 : 46 : if (!checked_contract_p (get_contract_semantic (t)))
10578 : : return true;
10579 : 33 : return RECUR (CONTRACT_CONDITION (t), rval);
10580 : :
10581 : 39 : case LABEL_EXPR:
10582 : 39 : t = LABEL_EXPR_LABEL (t);
10583 : 39 : if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23)
10584 : : return true;
10585 : 38 : else if (flags & tf_error)
10586 : 6 : constexpr_error (loc, fundef_p, "label definition in %<constexpr%> "
10587 : : "function only available with %<-std=c++2b%> or "
10588 : : "%<-std=gnu++2b%>");
10589 : : return false;
10590 : :
10591 : 22 : case ANNOTATE_EXPR:
10592 : 22 : return RECUR (TREE_OPERAND (t, 0), rval);
10593 : :
10594 : 12038 : case BIT_CAST_EXPR:
10595 : 12038 : return RECUR (TREE_OPERAND (t, 0), rval);
10596 : :
10597 : : /* Coroutine await, yield and return expressions are not. */
10598 : : case CO_AWAIT_EXPR:
10599 : : case CO_YIELD_EXPR:
10600 : : case CO_RETURN_EXPR:
10601 : : return false;
10602 : :
10603 : 30 : case NONTYPE_ARGUMENT_PACK:
10604 : 30 : {
10605 : 30 : tree args = ARGUMENT_PACK_ARGS (t);
10606 : 30 : int len = TREE_VEC_LENGTH (args);
10607 : 90 : for (int i = 0; i < len; ++i)
10608 : 60 : if (!RECUR (TREE_VEC_ELT (args, i), any))
10609 : : return false;
10610 : : return true;
10611 : : }
10612 : :
10613 : 0 : default:
10614 : 0 : if (objc_non_constant_expr_p (t))
10615 : : return false;
10616 : :
10617 : 0 : sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
10618 : 0 : gcc_unreachable ();
10619 : : return false;
10620 : : }
10621 : : #undef RECUR
10622 : : }
10623 : :
10624 : : bool
10625 : 1039890569 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
10626 : : bool fundef_p, tsubst_flags_t flags)
10627 : : {
10628 : 1039890569 : if (flags & tf_error)
10629 : : {
10630 : : /* Check potentiality quietly first, as that could be performed more
10631 : : efficiently in some cases (currently only for TRUTH_*_EXPR). If
10632 : : that fails, replay the check noisily to give errors. */
10633 : 31148502 : flags &= ~tf_error;
10634 : 31148502 : if (potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
10635 : : flags))
10636 : : return true;
10637 : 890 : flags |= tf_error;
10638 : : }
10639 : :
10640 : 1008742957 : tree target = NULL_TREE;
10641 : 1008742957 : return potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
10642 : 1008742957 : flags, &target);
10643 : : }
10644 : :
10645 : : /* The main entry point to the above. */
10646 : :
10647 : : bool
10648 : 324431334 : potential_constant_expression (tree t)
10649 : : {
10650 : 324431334 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
10651 : : /*now*/false, /*fundef_p*/false,
10652 : 324431334 : tf_none);
10653 : : }
10654 : :
10655 : : /* As above, but require a constant rvalue. */
10656 : :
10657 : : bool
10658 : 17652670 : potential_rvalue_constant_expression (tree t)
10659 : : {
10660 : 17652670 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
10661 : : /*now*/false, /*fundef_p*/false,
10662 : 17652670 : tf_none);
10663 : : }
10664 : :
10665 : : /* Like above, but complain about non-constant expressions. */
10666 : :
10667 : : bool
10668 : 27531997 : require_potential_constant_expression (tree t)
10669 : : {
10670 : 27531997 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
10671 : : /*now*/false, /*fundef_p*/false,
10672 : 27531997 : tf_warning_or_error);
10673 : : }
10674 : :
10675 : : /* Cross product of the above. */
10676 : :
10677 : : bool
10678 : 46395 : require_potential_rvalue_constant_expression (tree t)
10679 : : {
10680 : 46395 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
10681 : : /*now*/false, /*fundef_p*/false,
10682 : 46395 : tf_warning_or_error);
10683 : : }
10684 : :
10685 : : /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */
10686 : :
10687 : : bool
10688 : 182 : require_potential_rvalue_constant_expression_fncheck (tree t)
10689 : : {
10690 : 182 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
10691 : : /*now*/false, /*fundef_p*/true,
10692 : 182 : tf_warning_or_error);
10693 : : }
10694 : :
10695 : : /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
10696 : :
10697 : : bool
10698 : 424 : require_rvalue_constant_expression (tree t)
10699 : : {
10700 : 424 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
10701 : : /*now*/true, /*fundef_p*/false,
10702 : 424 : tf_warning_or_error);
10703 : : }
10704 : :
10705 : : /* Like potential_constant_expression, but don't consider possible constexpr
10706 : : substitution of the current function. That is, PARM_DECL qualifies under
10707 : : potential_constant_expression, but not here.
10708 : :
10709 : : This is basically what you can check when any actual constant values might
10710 : : be value-dependent. */
10711 : :
10712 : : bool
10713 : 511395964 : is_constant_expression (tree t)
10714 : : {
10715 : 511395964 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
10716 : : /*now*/true, /*fundef_p*/false,
10717 : 511395964 : tf_none);
10718 : : }
10719 : :
10720 : : /* As above, but expect an rvalue. */
10721 : :
10722 : : bool
10723 : 91195830 : is_rvalue_constant_expression (tree t)
10724 : : {
10725 : 91195830 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
10726 : : /*now*/true, /*fundef_p*/false,
10727 : 91195830 : tf_none);
10728 : : }
10729 : :
10730 : : /* Like above, but complain about non-constant expressions. */
10731 : :
10732 : : bool
10733 : 3569504 : require_constant_expression (tree t)
10734 : : {
10735 : 3569504 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
10736 : : /*now*/true, /*fundef_p*/false,
10737 : 3569504 : tf_warning_or_error);
10738 : : }
10739 : :
10740 : : /* Like is_constant_expression, but allow const variables that are not allowed
10741 : : under constexpr rules. */
10742 : :
10743 : : bool
10744 : 32917767 : is_static_init_expression (tree t)
10745 : : {
10746 : 32917767 : return potential_constant_expression_1 (t, /*want_rval*/false,
10747 : : /*strict*/false, /*now*/true,
10748 : 32917767 : /*fundef_p*/false, tf_none);
10749 : : }
10750 : :
10751 : : /* Returns true if T is a potential constant expression that is not
10752 : : instantiation-dependent, and therefore a candidate for constant folding even
10753 : : in a template. */
10754 : :
10755 : : bool
10756 : 497636542 : is_nondependent_constant_expression (tree t)
10757 : : {
10758 : 497636542 : return (!type_unknown_p (t)
10759 : 497636342 : && is_constant_expression (t)
10760 : 893683472 : && !instantiation_dependent_expression_p (t));
10761 : : }
10762 : :
10763 : : /* Returns true if T is a potential static initializer expression that is not
10764 : : instantiation-dependent. */
10765 : :
10766 : : bool
10767 : 32917767 : is_nondependent_static_init_expression (tree t)
10768 : : {
10769 : 32917767 : return (!type_unknown_p (t)
10770 : 32917767 : && is_static_init_expression (t)
10771 : 63805202 : && !instantiation_dependent_expression_p (t));
10772 : : }
10773 : :
10774 : : /* True iff FN is an implicitly constexpr function. */
10775 : :
10776 : : bool
10777 : 89076 : decl_implicit_constexpr_p (tree fn)
10778 : : {
10779 : 89076 : if (!(flag_implicit_constexpr
10780 : 0 : && TREE_CODE (fn) == FUNCTION_DECL
10781 : 0 : && DECL_DECLARED_CONSTEXPR_P (fn)))
10782 : : return false;
10783 : :
10784 : 0 : if (DECL_CLONED_FUNCTION_P (fn))
10785 : 0 : fn = DECL_CLONED_FUNCTION (fn);
10786 : :
10787 : 0 : return (DECL_LANG_SPECIFIC (fn)
10788 : 0 : && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr);
10789 : : }
10790 : :
10791 : : /* Finalize constexpr processing after parsing. */
10792 : :
10793 : : void
10794 : 99146 : fini_constexpr (void)
10795 : : {
10796 : : /* The contexpr call and fundef copies tables are no longer needed. */
10797 : 99146 : constexpr_call_table = NULL;
10798 : 99146 : fundef_copies_table = NULL;
10799 : 99146 : }
10800 : :
10801 : : #include "gt-cp-constexpr.h"
|