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 | is_instantiation_of_constexpr (tree fun) |
61 | { |
62 | return ((DECL_TEMPLOID_INSTANTIATION (fun) |
63 | && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun))) |
64 | || (DECL_DEFAULTED_FN (fun) |
65 | && DECL_DECLARED_CONSTEXPR_P (fun))); |
66 | } |
67 | |
68 | /* Return true if T is a literal type. */ |
69 | |
70 | bool |
71 | literal_type_p (tree t) |
72 | { |
73 | if (SCALAR_TYPE_P (t) |
74 | || VECTOR_TYPE_P (t) |
75 | || TYPE_REF_P (t) |
76 | || (VOID_TYPE_P (t) && cxx_dialect >= cxx14)) |
77 | return true; |
78 | if (CLASS_TYPE_P (t)) |
79 | { |
80 | t = complete_type (t); |
81 | gcc_assert (COMPLETE_TYPE_P (t) || errorcount); |
82 | return CLASSTYPE_LITERAL_P (t); |
83 | } |
84 | if (TREE_CODE (t) == ARRAY_TYPE) |
85 | return literal_type_p (t: strip_array_types (type: 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 | ensure_literal_type_for_constexpr_object (tree decl) |
95 | { |
96 | tree type = TREE_TYPE (decl); |
97 | if (VAR_P (decl) |
98 | && (DECL_DECLARED_CONSTEXPR_P (decl) |
99 | || var_in_constexpr_fn (decl)) |
100 | && !processing_template_decl) |
101 | { |
102 | tree stype = strip_array_types (type); |
103 | 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 | else if (!literal_type_p (t: type)) |
107 | { |
108 | if (DECL_DECLARED_CONSTEXPR_P (decl)) |
109 | { |
110 | auto_diagnostic_group d; |
111 | error_at (DECL_SOURCE_LOCATION (decl), |
112 | "the type %qT of %<constexpr%> variable %qD " |
113 | "is not literal" , type, decl); |
114 | explain_non_literal_class (type); |
115 | decl = error_mark_node; |
116 | } |
117 | else if (cxx_dialect < cxx23) |
118 | { |
119 | if (!is_instantiation_of_constexpr (fun: current_function_decl)) |
120 | { |
121 | auto_diagnostic_group d; |
122 | 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 | explain_non_literal_class (type); |
127 | decl = error_mark_node; |
128 | } |
129 | cp_function_chain->invalid_constexpr = true; |
130 | } |
131 | } |
132 | else if (DECL_DECLARED_CONSTEXPR_P (decl) |
133 | && variably_modified_type_p (type, NULL_TREE)) |
134 | { |
135 | error_at (DECL_SOURCE_LOCATION (decl), |
136 | "%<constexpr%> variable %qD has variably-modified " |
137 | "type %qT" , decl, type); |
138 | decl = error_mark_node; |
139 | } |
140 | } |
141 | 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 | constexpr_error (location_t location, bool constexpr_fundef_p, |
154 | const char *gmsgid, ...) |
155 | { |
156 | diagnostic_info diagnostic; |
157 | va_list ap; |
158 | rich_location richloc (line_table, location); |
159 | va_start (ap, gmsgid); |
160 | bool ret; |
161 | if (!constexpr_fundef_p) |
162 | { |
163 | /* Report an error that cannot be suppressed. */ |
164 | diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ERROR); |
165 | ret = diagnostic_report_diagnostic (context: global_dc, diagnostic: &diagnostic); |
166 | } |
167 | else if (warn_invalid_constexpr) |
168 | { |
169 | diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, |
170 | cxx_dialect < cxx23 ? DK_PEDWARN : DK_WARNING); |
171 | diagnostic.option_index = OPT_Winvalid_constexpr; |
172 | ret = diagnostic_report_diagnostic (context: global_dc, diagnostic: &diagnostic); |
173 | } |
174 | else |
175 | ret = false; |
176 | va_end (ap); |
177 | return ret; |
178 | } |
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 | constexpr_fundef_hasher::equal (const constexpr_fundef *lhs, |
197 | const constexpr_fundef *rhs) |
198 | { |
199 | 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 | constexpr_fundef_hasher::hash (const constexpr_fundef *fundef) |
207 | { |
208 | return DECL_UID (fundef->decl); |
209 | } |
210 | |
211 | /* Return a previously saved definition of function FUN. */ |
212 | |
213 | constexpr_fundef * |
214 | retrieve_constexpr_fundef (tree fun) |
215 | { |
216 | if (constexpr_fundef_table == NULL) |
217 | return NULL; |
218 | |
219 | constexpr_fundef fundef = { .decl: fun, NULL_TREE, NULL_TREE, NULL_TREE }; |
220 | return constexpr_fundef_table->find (value: &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 | is_valid_constexpr_fn (tree fun, bool complain) |
228 | { |
229 | bool ret = true; |
230 | |
231 | if (DECL_INHERITED_CTOR (fun) |
232 | && TREE_CODE (fun) == TEMPLATE_DECL) |
233 | { |
234 | ret = false; |
235 | if (complain) |
236 | error ("inherited constructor %qD is not %<constexpr%>" , |
237 | DECL_INHERITED_CTOR (fun)); |
238 | } |
239 | else |
240 | { |
241 | for (tree parm = FUNCTION_FIRST_USER_PARM (fun); |
242 | parm != NULL_TREE; parm = TREE_CHAIN (parm)) |
243 | if (!literal_type_p (TREE_TYPE (parm))) |
244 | { |
245 | ret = false; |
246 | if (complain) |
247 | { |
248 | auto_diagnostic_group d; |
249 | if (constexpr_error (location: input_location, /*constexpr_fundef_p*/true, |
250 | gmsgid: "invalid type for parameter %d of " |
251 | "%<constexpr%> function %q+#D" , |
252 | DECL_PARM_INDEX (parm), fun)) |
253 | explain_non_literal_class (TREE_TYPE (parm)); |
254 | } |
255 | } |
256 | } |
257 | |
258 | if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17) |
259 | { |
260 | ret = false; |
261 | if (complain) |
262 | inform (DECL_SOURCE_LOCATION (fun), |
263 | "lambdas are implicitly %<constexpr%> only in C++17 and later" ); |
264 | } |
265 | else if (DECL_DESTRUCTOR_P (fun) && cxx_dialect < cxx20) |
266 | { |
267 | ret = false; |
268 | if (complain) |
269 | error_at (DECL_SOURCE_LOCATION (fun), |
270 | "%<constexpr%> destructors only available with " |
271 | "%<-std=c++20%> or %<-std=gnu++20%>" ); |
272 | } |
273 | else if (!DECL_CONSTRUCTOR_P (fun) && !DECL_DESTRUCTOR_P (fun)) |
274 | { |
275 | tree rettype = TREE_TYPE (TREE_TYPE (fun)); |
276 | if (!literal_type_p (t: rettype)) |
277 | { |
278 | ret = false; |
279 | if (complain) |
280 | { |
281 | auto_diagnostic_group d; |
282 | if (constexpr_error (location: input_location, /*constexpr_fundef_p*/true, |
283 | gmsgid: "invalid return type %qT of %<constexpr%> " |
284 | "function %q+D" , rettype, fun)) |
285 | explain_non_literal_class (rettype); |
286 | } |
287 | } |
288 | |
289 | /* C++14 DR 1684 removed this restriction. */ |
290 | if (cxx_dialect < cxx14 |
291 | && DECL_IOBJ_MEMBER_FUNCTION_P (fun) |
292 | && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun))) |
293 | { |
294 | ret = false; |
295 | if (complain) |
296 | { |
297 | auto_diagnostic_group d; |
298 | 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 | explain_non_literal_class (DECL_CONTEXT (fun)); |
303 | } |
304 | } |
305 | } |
306 | else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun))) |
307 | { |
308 | ret = false; |
309 | if (complain) |
310 | error ("%q#T has virtual base classes" , DECL_CONTEXT (fun)); |
311 | } |
312 | |
313 | 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 | 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 | auto_vec<tree, 2> fields; |
331 | do |
332 | { |
333 | fields.safe_push (TREE_OPERAND (member, 1)); |
334 | member = TREE_OPERAND (member, 0); |
335 | } |
336 | while (ANON_AGGR_TYPE_P (TREE_TYPE (member)) |
337 | && 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 | while (field = fields.pop(), |
344 | ANON_AGGR_TYPE_P (TREE_TYPE (field))) |
345 | { |
346 | tree ctor; |
347 | /* If there is already an outer constructor entry for the anonymous |
348 | aggregate FIELD, use it; otherwise, insert one. */ |
349 | if (vec_safe_is_empty (v: *vec) |
350 | || (*vec)->last().index != field) |
351 | { |
352 | ctor = build_constructor (TREE_TYPE (field), NULL); |
353 | CONSTRUCTOR_APPEND_ELT (*vec, field, ctor); |
354 | } |
355 | else |
356 | ctor = (*vec)->last().value; |
357 | 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 | gcc_assert (fields.is_empty()); |
363 | CONSTRUCTOR_APPEND_ELT (*vec, field, init); |
364 | |
365 | return true; |
366 | } |
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 | build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec) |
376 | { |
377 | tree member, init; |
378 | if (TREE_CODE (t) == CLEANUP_POINT_EXPR) |
379 | t = TREE_OPERAND (t, 0); |
380 | if (TREE_CODE (t) == EXPR_STMT) |
381 | t = TREE_OPERAND (t, 0); |
382 | if (t == error_mark_node) |
383 | return false; |
384 | if (TREE_CODE (t) == STATEMENT_LIST) |
385 | { |
386 | for (tree stmt : tsi_range (t)) |
387 | if (! build_data_member_initialization (t: stmt, vec)) |
388 | return false; |
389 | return true; |
390 | } |
391 | 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 | return build_data_member_initialization (CLEANUP_BODY (t), vec); |
399 | } |
400 | if (TREE_CODE (t) == CONVERT_EXPR) |
401 | t = TREE_OPERAND (t, 0); |
402 | 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 | || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR)) |
407 | { |
408 | member = TREE_OPERAND (t, 0); |
409 | init = break_out_target_exprs (TREE_OPERAND (t, 1)); |
410 | } |
411 | else if (TREE_CODE (t) == CALL_EXPR) |
412 | { |
413 | tree fn = get_callee_fndecl (t); |
414 | if (!fn || !DECL_CONSTRUCTOR_P (fn)) |
415 | /* We're only interested in calls to subobject constructors. */ |
416 | return true; |
417 | 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 | init = break_out_target_exprs (t); |
422 | } |
423 | else if (TREE_CODE (t) == BIND_EXPR) |
424 | 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 | if (INDIRECT_REF_P (member)) |
429 | member = TREE_OPERAND (member, 0); |
430 | if (TREE_CODE (member) == NOP_EXPR) |
431 | { |
432 | tree op = member; |
433 | STRIP_NOPS (op); |
434 | if (TREE_CODE (op) == ADDR_EXPR) |
435 | { |
436 | 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 | else if (op == current_class_ptr |
444 | && (same_type_ignoring_top_level_qualifiers_p |
445 | (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 | gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member)))); |
454 | } |
455 | } |
456 | if (TREE_CODE (member) == ADDR_EXPR) |
457 | member = TREE_OPERAND (member, 0); |
458 | if (TREE_CODE (member) == COMPONENT_REF) |
459 | { |
460 | tree aggr = TREE_OPERAND (member, 0); |
461 | if (TREE_CODE (aggr) == VAR_DECL) |
462 | /* Initializing a local variable, don't add anything. */ |
463 | return true; |
464 | if (TREE_CODE (aggr) != COMPONENT_REF) |
465 | /* Normal member initialization. */ |
466 | member = TREE_OPERAND (member, 1); |
467 | else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr))) |
468 | /* Initializing a member of an anonymous union. */ |
469 | return build_anon_member_initialization (member, init, vec_outer: 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 | 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 | if (!vec_safe_is_empty (v: *vec) && (*vec)->last().index == member) |
479 | (*vec)->last().value = init; |
480 | else |
481 | 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 | check_constexpr_bind_expr_vars (tree t) |
491 | { |
492 | gcc_assert (TREE_CODE (t) == BIND_EXPR); |
493 | |
494 | for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var)) |
495 | if (TREE_CODE (var) == TYPE_DECL |
496 | && DECL_IMPLICIT_TYPEDEF_P (var) |
497 | && !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 | check_constexpr_ctor_body_1 (tree last, tree list) |
506 | { |
507 | switch (TREE_CODE (list)) |
508 | { |
509 | case DECL_EXPR: |
510 | if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL |
511 | || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL) |
512 | return true; |
513 | return false; |
514 | |
515 | case CLEANUP_POINT_EXPR: |
516 | return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0), |
517 | /*complain=*/false); |
518 | |
519 | case BIND_EXPR: |
520 | if (!check_constexpr_bind_expr_vars (t: list) |
521 | || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list), |
522 | /*complain=*/false)) |
523 | 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 | 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 | if (cxx_dialect >= cxx14) |
544 | return true; |
545 | |
546 | bool ok = true; |
547 | if (TREE_CODE (list) == STATEMENT_LIST) |
548 | { |
549 | tree_stmt_iterator i = tsi_last (t: list); |
550 | for (; !tsi_end_p (i); tsi_prev (i: &i)) |
551 | { |
552 | tree t = tsi_stmt (i); |
553 | if (t == last) |
554 | break; |
555 | if (!check_constexpr_ctor_body_1 (last, list: t)) |
556 | { |
557 | ok = false; |
558 | break; |
559 | } |
560 | } |
561 | } |
562 | else if (list != last |
563 | && !check_constexpr_ctor_body_1 (last, list)) |
564 | ok = false; |
565 | if (!ok) |
566 | { |
567 | if (complain) |
568 | error ("%<constexpr%> constructor does not have empty body" ); |
569 | 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 | sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v) |
582 | { |
583 | tree pri = CLASSTYPE_PRIMARY_BINFO (type); |
584 | tree field_type; |
585 | unsigned i; |
586 | constructor_elt *ce; |
587 | |
588 | if (pri) |
589 | field_type = BINFO_TYPE (pri); |
590 | else if (TYPE_CONTAINS_VPTR_P (type)) |
591 | 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 | for (i = 0; vec_safe_iterate (v, ix: i, ptr: &ce); ++i) |
598 | if (TREE_TYPE (ce->index) == field_type) |
599 | break; |
600 | |
601 | if (i > 0 && i < vec_safe_length (v)) |
602 | { |
603 | vec<constructor_elt, va_gc> &vref = *v; |
604 | constructor_elt elt = vref[i]; |
605 | for (; i > 0; --i) |
606 | vref[i] = vref[i-1]; |
607 | 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 | build_constexpr_constructor_member_initializers (tree type, tree body) |
618 | { |
619 | vec<constructor_elt, va_gc> *vec = NULL; |
620 | bool ok = true; |
621 | while (true) |
622 | switch (TREE_CODE (body)) |
623 | { |
624 | case MUST_NOT_THROW_EXPR: |
625 | case EH_SPEC_BLOCK: |
626 | body = TREE_OPERAND (body, 0); |
627 | break; |
628 | |
629 | case STATEMENT_LIST: |
630 | for (tree stmt : tsi_range (body)) |
631 | { |
632 | body = stmt; |
633 | if (TREE_CODE (body) == BIND_EXPR) |
634 | break; |
635 | } |
636 | break; |
637 | |
638 | case BIND_EXPR: |
639 | body = BIND_EXPR_BODY (body); |
640 | goto found; |
641 | |
642 | default: |
643 | gcc_unreachable (); |
644 | } |
645 | found: |
646 | if (TREE_CODE (body) == TRY_BLOCK) |
647 | { |
648 | body = TREE_OPERAND (body, 0); |
649 | if (TREE_CODE (body) == BIND_EXPR) |
650 | body = BIND_EXPR_BODY (body); |
651 | } |
652 | if (TREE_CODE (body) == CLEANUP_POINT_EXPR) |
653 | { |
654 | body = TREE_OPERAND (body, 0); |
655 | if (TREE_CODE (body) == EXPR_STMT) |
656 | body = TREE_OPERAND (body, 0); |
657 | if (TREE_CODE (body) == INIT_EXPR |
658 | && (same_type_ignoring_top_level_qualifiers_p |
659 | (TREE_TYPE (TREE_OPERAND (body, 0)), |
660 | current_class_type))) |
661 | { |
662 | /* Trivial copy. */ |
663 | return TREE_OPERAND (body, 1); |
664 | } |
665 | ok = build_data_member_initialization (t: body, vec: &vec); |
666 | } |
667 | else if (TREE_CODE (body) == STATEMENT_LIST) |
668 | { |
669 | for (tree stmt : tsi_range (body)) |
670 | { |
671 | ok = build_data_member_initialization (t: stmt, vec: &vec); |
672 | if (!ok) |
673 | break; |
674 | } |
675 | } |
676 | else if (EXPR_P (body)) |
677 | ok = build_data_member_initialization (t: body, vec: &vec); |
678 | else |
679 | gcc_assert (errorcount > 0); |
680 | if (ok) |
681 | { |
682 | if (vec_safe_length (v: vec) > 0) |
683 | { |
684 | /* In a delegating constructor, return the target. */ |
685 | constructor_elt *ce = &(*vec)[0]; |
686 | if (ce->index == current_class_ptr) |
687 | { |
688 | body = ce->value; |
689 | vec_free (v&: vec); |
690 | return body; |
691 | } |
692 | } |
693 | vec = sort_constexpr_mem_initializers (type, v: vec); |
694 | return build_constructor (type, vec); |
695 | } |
696 | else |
697 | 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 | get_function_named_in_call (tree t) |
706 | { |
707 | tree callee = cp_get_callee (t); |
708 | tree fun = cp_get_fndecl_from_callee (callee, /*fold*/false); |
709 | 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 | constexpr_fn_retval (tree body) |
719 | { |
720 | switch (TREE_CODE (body)) |
721 | { |
722 | case STATEMENT_LIST: |
723 | { |
724 | tree expr = NULL_TREE; |
725 | for (tree stmt : tsi_range (body)) |
726 | { |
727 | tree s = constexpr_fn_retval (body: stmt); |
728 | if (s == error_mark_node) |
729 | return error_mark_node; |
730 | else if (s == NULL_TREE) |
731 | /* Keep iterating. */; |
732 | else if (expr) |
733 | /* Multiple return statements. */ |
734 | return error_mark_node; |
735 | else |
736 | expr = s; |
737 | } |
738 | return expr; |
739 | } |
740 | |
741 | case RETURN_EXPR: |
742 | return break_out_target_exprs (TREE_OPERAND (body, 0)); |
743 | |
744 | case DECL_EXPR: |
745 | { |
746 | tree decl = DECL_EXPR_DECL (body); |
747 | if (TREE_CODE (decl) == USING_DECL |
748 | /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */ |
749 | || DECL_ARTIFICIAL (decl)) |
750 | return NULL_TREE; |
751 | return error_mark_node; |
752 | } |
753 | |
754 | case CLEANUP_POINT_EXPR: |
755 | return constexpr_fn_retval (TREE_OPERAND (body, 0)); |
756 | |
757 | case BIND_EXPR: |
758 | if (!check_constexpr_bind_expr_vars (t: body)) |
759 | return error_mark_node; |
760 | return constexpr_fn_retval (BIND_EXPR_BODY (body)); |
761 | |
762 | case USING_STMT: |
763 | case DEBUG_BEGIN_STMT: |
764 | return NULL_TREE; |
765 | |
766 | case CALL_EXPR: |
767 | { |
768 | tree fun = get_function_named_in_call (t: body); |
769 | if (fun != NULL_TREE |
770 | && fndecl_built_in_p (node: fun, name1: BUILT_IN_UNREACHABLE)) |
771 | return NULL_TREE; |
772 | } |
773 | /* Fallthru. */ |
774 | |
775 | default: |
776 | 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 | massage_constexpr_body (tree fun, tree body) |
786 | { |
787 | if (DECL_CONSTRUCTOR_P (fun)) |
788 | body = build_constexpr_constructor_member_initializers |
789 | (DECL_CONTEXT (fun), body); |
790 | else if (cxx_dialect < cxx14) |
791 | { |
792 | if (TREE_CODE (body) == EH_SPEC_BLOCK) |
793 | body = EH_SPEC_STMTS (body); |
794 | if (TREE_CODE (body) == MUST_NOT_THROW_EXPR) |
795 | body = TREE_OPERAND (body, 0); |
796 | body = constexpr_fn_retval (body); |
797 | } |
798 | 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 | cx_check_missing_mem_inits (tree ctype, tree body, bool complain) |
806 | { |
807 | /* We allow uninitialized bases/fields in C++20. */ |
808 | if (cxx_dialect >= cxx20) |
809 | return false; |
810 | |
811 | unsigned nelts = 0; |
812 | |
813 | if (body) |
814 | { |
815 | if (TREE_CODE (body) != CONSTRUCTOR) |
816 | return false; |
817 | nelts = CONSTRUCTOR_NELTS (body); |
818 | } |
819 | tree field = TYPE_FIELDS (ctype); |
820 | |
821 | if (TREE_CODE (ctype) == UNION_TYPE) |
822 | { |
823 | if (nelts == 0 && next_aggregate_field (field)) |
824 | { |
825 | if (complain) |
826 | error ("%<constexpr%> constructor for union %qT must " |
827 | "initialize exactly one non-static data member" , ctype); |
828 | return true; |
829 | } |
830 | 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 | for (unsigned i = 0; i <= nelts; ++i) |
837 | { |
838 | tree index = NULL_TREE; |
839 | if (i < nelts) |
840 | { |
841 | index = CONSTRUCTOR_ELT (body, i)->index; |
842 | /* Skip base and vtable inits. */ |
843 | if (TREE_CODE (index) != FIELD_DECL |
844 | || DECL_ARTIFICIAL (index)) |
845 | continue; |
846 | } |
847 | |
848 | for (; field != index; field = DECL_CHAIN (field)) |
849 | { |
850 | tree ftype; |
851 | if (TREE_CODE (field) != FIELD_DECL) |
852 | continue; |
853 | if (DECL_UNNAMED_BIT_FIELD (field)) |
854 | continue; |
855 | if (DECL_ARTIFICIAL (field)) |
856 | continue; |
857 | if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) |
858 | { |
859 | /* Recurse to check the anonymous aggregate member. */ |
860 | bad |= cx_check_missing_mem_inits |
861 | (TREE_TYPE (field), NULL_TREE, complain); |
862 | if (bad && !complain) |
863 | return true; |
864 | continue; |
865 | } |
866 | ftype = TREE_TYPE (field); |
867 | 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 | continue; |
871 | if (is_empty_field (field)) |
872 | /* An empty field doesn't need an initializer. */ |
873 | continue; |
874 | ftype = strip_array_types (type: ftype); |
875 | 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 | gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype) |
881 | || errorcount != 0); |
882 | continue; |
883 | } |
884 | if (!complain) |
885 | return true; |
886 | auto_diagnostic_group d; |
887 | error ("member %qD must be initialized by mem-initializer " |
888 | "in %<constexpr%> constructor" , field); |
889 | inform (DECL_SOURCE_LOCATION (field), "declared here" ); |
890 | bad = true; |
891 | } |
892 | if (field == NULL_TREE) |
893 | break; |
894 | |
895 | if (ANON_AGGR_TYPE_P (TREE_TYPE (index))) |
896 | { |
897 | /* Check the anonymous aggregate initializer is valid. */ |
898 | bad |= cx_check_missing_mem_inits |
899 | (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain); |
900 | if (bad && !complain) |
901 | return true; |
902 | } |
903 | 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 | maybe_save_constexpr_fundef (tree fun) |
915 | { |
916 | if (processing_template_decl |
917 | || cp_function_chain->invalid_constexpr |
918 | || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))) |
919 | 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 | bool implicit = false; |
924 | if (flag_implicit_constexpr) |
925 | { |
926 | if (DECL_DELETING_DESTRUCTOR_P (fun) |
927 | && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun))) |
928 | /* Don't inherit implicit constexpr from the non-deleting |
929 | destructor. */ |
930 | DECL_DECLARED_CONSTEXPR_P (fun) = false; |
931 | |
932 | if (!DECL_DECLARED_CONSTEXPR_P (fun) |
933 | && DECL_DECLARED_INLINE_P (fun) |
934 | && !lookup_attribute (attr_name: "noinline" , DECL_ATTRIBUTES (fun))) |
935 | implicit = true; |
936 | } |
937 | |
938 | if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit) |
939 | return; |
940 | |
941 | bool complain = !DECL_GENERATED_P (fun) && !implicit; |
942 | |
943 | if (!is_valid_constexpr_fn (fun, complain)) |
944 | return; |
945 | |
946 | tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun)); |
947 | if (massaged == NULL_TREE || massaged == error_mark_node) |
948 | { |
949 | if (!DECL_CONSTRUCTOR_P (fun) && complain) |
950 | error ("body of %<constexpr%> function %qD not a return-statement" , |
951 | fun); |
952 | return; |
953 | } |
954 | |
955 | bool potential = potential_rvalue_constant_expression (massaged); |
956 | if (!potential && complain) |
957 | require_potential_rvalue_constant_expression_fncheck (massaged); |
958 | |
959 | if (DECL_CONSTRUCTOR_P (fun) && potential |
960 | && !DECL_DEFAULTED_FN (fun)) |
961 | { |
962 | if (cx_check_missing_mem_inits (DECL_CONTEXT (fun), |
963 | body: massaged, complain)) |
964 | potential = false; |
965 | 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 | massaged = DECL_SAVED_TREE (fun); |
970 | potential = potential_rvalue_constant_expression (massaged); |
971 | if (!potential && complain) |
972 | require_potential_rvalue_constant_expression_fncheck (massaged); |
973 | } |
974 | } |
975 | |
976 | 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 | && warn_invalid_constexpr != 0) |
982 | return; |
983 | |
984 | if (implicit) |
985 | { |
986 | if (potential) |
987 | { |
988 | DECL_DECLARED_CONSTEXPR_P (fun) = true; |
989 | DECL_LANG_SPECIFIC (fun)->u.fn.implicit_constexpr = true; |
990 | if (DECL_CONSTRUCTOR_P (fun)) |
991 | 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 | constexpr_fundef entry = {.decl: fun, NULL_TREE, NULL_TREE, NULL_TREE}; |
1000 | bool clear_ctx = false; |
1001 | if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE) |
1002 | { |
1003 | clear_ctx = true; |
1004 | DECL_CONTEXT (DECL_RESULT (fun)) = fun; |
1005 | } |
1006 | tree saved_fn = current_function_decl; |
1007 | current_function_decl = fun; |
1008 | entry.body = copy_fn (entry.decl, entry.parms, entry.result); |
1009 | current_function_decl = saved_fn; |
1010 | if (clear_ctx) |
1011 | DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE; |
1012 | 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 | entry.result = error_mark_node; |
1017 | |
1018 | 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 | register_constexpr_fundef (const constexpr_fundef &value) |
1026 | { |
1027 | /* Create the constexpr function table if necessary. */ |
1028 | if (constexpr_fundef_table == NULL) |
1029 | constexpr_fundef_table |
1030 | = hash_table<constexpr_fundef_hasher>::create_ggc (n: 101); |
1031 | |
1032 | constexpr_fundef **slot = constexpr_fundef_table->find_slot |
1033 | (value: const_cast<constexpr_fundef *> (&value), insert: INSERT); |
1034 | |
1035 | gcc_assert (*slot == NULL); |
1036 | *slot = ggc_alloc<constexpr_fundef> (); |
1037 | **slot = value; |
1038 | } |
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 | explain_invalid_constexpr_fn (tree fun) |
1048 | { |
1049 | static hash_set<tree> *diagnosed; |
1050 | 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 | if (warn_invalid_constexpr == 0 && DECL_DECLARED_CONSTEXPR_P (fun)) |
1056 | /* Go on. */; |
1057 | /* Only diagnose defaulted functions, lambdas, or instantiations. */ |
1058 | else if (!DECL_DEFAULTED_FN (fun) |
1059 | && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) |
1060 | && !is_instantiation_of_constexpr (fun)) |
1061 | { |
1062 | inform (DECL_SOURCE_LOCATION (fun), "%qD declared here" , fun); |
1063 | return; |
1064 | } |
1065 | if (diagnosed == NULL) |
1066 | diagnosed = new hash_set<tree>; |
1067 | if (diagnosed->add (k: fun)) |
1068 | /* Already explained. */ |
1069 | return; |
1070 | |
1071 | iloc_sentinel ils = input_location; |
1072 | 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 | input_location = DECL_SOURCE_LOCATION (fun); |
1077 | inform (input_location, |
1078 | "%qD is not usable as a %<constexpr%> function because:" , fun); |
1079 | } |
1080 | /* First check the declaration. */ |
1081 | if (is_valid_constexpr_fn (fun, complain: true)) |
1082 | { |
1083 | /* Then if it's OK, the body. */ |
1084 | if (!DECL_DECLARED_CONSTEXPR_P (fun) |
1085 | && DECL_DEFAULTED_FN (fun)) |
1086 | explain_implicit_non_constexpr (fun); |
1087 | else |
1088 | { |
1089 | if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun)) |
1090 | body = fd->body; |
1091 | else |
1092 | body = DECL_SAVED_TREE (fun); |
1093 | body = massage_constexpr_body (fun, body); |
1094 | require_potential_rvalue_constant_expression (body); |
1095 | if (DECL_CONSTRUCTOR_P (fun)) |
1096 | { |
1097 | cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, complain: true); |
1098 | if (cxx_dialect > cxx11) |
1099 | { |
1100 | /* Also check the body, not just the ctor-initializer. */ |
1101 | body = DECL_SAVED_TREE (fun); |
1102 | require_potential_rvalue_constant_expression (body); |
1103 | } |
1104 | } |
1105 | } |
1106 | } |
1107 | } |
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 | 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 | constexpr_global_ctx () |
1174 | : constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr), |
1175 | heap_dealloc_count (0) {} |
1176 | |
1177 | bool is_outside_lifetime (tree t) |
1178 | { |
1179 | if (tree *p = values.get (k: t)) |
1180 | if (*p == void_node) |
1181 | return true; |
1182 | return false; |
1183 | } |
1184 | tree get_value (tree t) |
1185 | { |
1186 | if (tree *p = values.get (k: t)) |
1187 | if (*p != void_node) |
1188 | return *p; |
1189 | return NULL_TREE; |
1190 | } |
1191 | tree *get_value_ptr (tree t, bool initializing) |
1192 | { |
1193 | if (modifiable && !modifiable->contains (k: t)) |
1194 | return nullptr; |
1195 | if (tree *p = values.get (k: t)) |
1196 | { |
1197 | if (*p != void_node) |
1198 | return p; |
1199 | else if (initializing) |
1200 | { |
1201 | *p = NULL_TREE; |
1202 | return p; |
1203 | } |
1204 | } |
1205 | return nullptr; |
1206 | } |
1207 | void put_value (tree t, tree v) |
1208 | { |
1209 | bool already_in_map = values.put (k: t, v); |
1210 | if (!already_in_map && modifiable) |
1211 | modifiable->add (k: t); |
1212 | } |
1213 | void destroy_value (tree t) |
1214 | { |
1215 | if (TREE_CODE (t) == VAR_DECL |
1216 | || TREE_CODE (t) == PARM_DECL |
1217 | || TREE_CODE (t) == RESULT_DECL) |
1218 | values.put (k: t, void_node); |
1219 | else |
1220 | values.remove (k: t); |
1221 | } |
1222 | void clear_value (tree t) |
1223 | { |
1224 | values.remove (k: 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 | modifiable_tracker (constexpr_global_ctx *g): global(g) |
1243 | { |
1244 | global->modifiable = &set; |
1245 | } |
1246 | ~modifiable_tracker () |
1247 | { |
1248 | for (tree t: set) |
1249 | global->clear_value (t); |
1250 | global->modifiable = nullptr; |
1251 | } |
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 | destroy_value_checked (const constexpr_ctx* ctx, tree t, bool *non_constant_p) |
1294 | { |
1295 | 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 | if (!*non_constant_p |
1300 | && DECL_P (t) |
1301 | /* Non-trivial destructors have their lifetimes ended explicitly |
1302 | with a clobber, so don't worry about it here. */ |
1303 | && (!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 | || TREE_CODE (t) == PARM_DECL) |
1310 | && ctx->global->is_outside_lifetime (t)) |
1311 | { |
1312 | if (!ctx->quiet) |
1313 | { |
1314 | auto_diagnostic_group d; |
1315 | error ("destroying %qE outside its lifetime" , t); |
1316 | inform (DECL_SOURCE_LOCATION (t), "declared here" ); |
1317 | } |
1318 | *non_constant_p = true; |
1319 | } |
1320 | 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 | uid_sensitive_constexpr_evaluation_p () |
1339 | { |
1340 | if (uid_sensitive_constexpr_evaluation_value) |
1341 | { |
1342 | ++uid_sensitive_constexpr_evaluation_true_counter; |
1343 | 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 | uid_sensitive_constexpr_evaluation_sentinel |
1355 | ::uid_sensitive_constexpr_evaluation_sentinel () |
1356 | : ovr (uid_sensitive_constexpr_evaluation_value, true) |
1357 | { |
1358 | } |
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 | uid_sensitive_constexpr_evaluation_checker |
1365 | ::uid_sensitive_constexpr_evaluation_checker () |
1366 | : saved_counter (uid_sensitive_constexpr_evaluation_true_counter) |
1367 | { |
1368 | } |
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 | uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const |
1376 | { |
1377 | return (uid_sensitive_constexpr_evaluation_value |
1378 | && 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 | constexpr_call_hasher::hash (constexpr_call *info) |
1391 | { |
1392 | 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 | constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs) |
1401 | { |
1402 | if (lhs == rhs) |
1403 | return true; |
1404 | if (lhs->hash != rhs->hash) |
1405 | return false; |
1406 | if (lhs->manifestly_const_eval != rhs->manifestly_const_eval) |
1407 | return false; |
1408 | if (!constexpr_fundef_hasher::equal (lhs: lhs->fundef, rhs: rhs->fundef)) |
1409 | return false; |
1410 | return cp_tree_equal (lhs->bindings, rhs->bindings); |
1411 | } |
1412 | |
1413 | /* Initialize the constexpr call table, if needed. */ |
1414 | |
1415 | static void |
1416 | maybe_initialize_constexpr_call_table (void) |
1417 | { |
1418 | if (constexpr_call_table == NULL) |
1419 | constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (n: 101); |
1420 | } |
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 | get_fundef_copy (constexpr_fundef *fundef) |
1442 | { |
1443 | tree copy; |
1444 | bool existed; |
1445 | tree *slot = &(hash_map_safe_get_or_insert<hm_ggc> |
1446 | (h&: fundef_copies_table, k: fundef->decl, e: &existed, size: 127)); |
1447 | |
1448 | 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 | copy = build_tree_list (fundef->body, fundef->parms); |
1454 | TREE_TYPE (copy) = fundef->result; |
1455 | } |
1456 | else if (*slot == NULL_TREE) |
1457 | { |
1458 | if (uid_sensitive_constexpr_evaluation_p ()) |
1459 | return NULL_TREE; |
1460 | |
1461 | /* We've already used the function itself, so make a copy. */ |
1462 | copy = build_tree_list (NULL, NULL); |
1463 | tree saved_body = DECL_SAVED_TREE (fundef->decl); |
1464 | tree saved_parms = DECL_ARGUMENTS (fundef->decl); |
1465 | tree saved_result = DECL_RESULT (fundef->decl); |
1466 | tree saved_fn = current_function_decl; |
1467 | DECL_SAVED_TREE (fundef->decl) = fundef->body; |
1468 | DECL_ARGUMENTS (fundef->decl) = fundef->parms; |
1469 | DECL_RESULT (fundef->decl) = fundef->result; |
1470 | current_function_decl = fundef->decl; |
1471 | TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy), |
1472 | TREE_TYPE (copy)); |
1473 | current_function_decl = saved_fn; |
1474 | DECL_RESULT (fundef->decl) = saved_result; |
1475 | DECL_ARGUMENTS (fundef->decl) = saved_parms; |
1476 | DECL_SAVED_TREE (fundef->decl) = saved_body; |
1477 | } |
1478 | else |
1479 | { |
1480 | /* We have a cached function available. */ |
1481 | copy = *slot; |
1482 | *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 | save_fundef_copy (tree fun, tree copy) |
1494 | { |
1495 | tree *slot = fundef_copies_table->get (k: fun); |
1496 | TREE_CHAIN (copy) = *slot; |
1497 | *slot = copy; |
1498 | } |
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 | 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 | const int nargs = call_expr_nargs (t); |
1527 | tree *args = (tree *) alloca (nargs * sizeof (tree)); |
1528 | tree new_call; |
1529 | int i; |
1530 | |
1531 | /* Don't fold __builtin_constant_p within a constexpr function. */ |
1532 | 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 | if (bi_const_p |
1537 | && ctx->manifestly_const_eval != mce_true |
1538 | && current_function_decl |
1539 | && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) |
1540 | { |
1541 | *non_constant_p = true; |
1542 | 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 | if (fndecl_built_in_p (node: fun, name: CP_BUILT_IN_IS_CONSTANT_EVALUATED, |
1551 | klass: BUILT_IN_FRONTEND)) |
1552 | { |
1553 | if (ctx->manifestly_const_eval == mce_unknown) |
1554 | { |
1555 | *non_constant_p = true; |
1556 | return t; |
1557 | } |
1558 | return constant_boolean_node (ctx->manifestly_const_eval == mce_true, |
1559 | boolean_type_node); |
1560 | } |
1561 | |
1562 | if (fndecl_built_in_p (node: fun, name: CP_BUILT_IN_SOURCE_LOCATION, klass: BUILT_IN_FRONTEND)) |
1563 | { |
1564 | temp_override<tree> ovr (current_function_decl); |
1565 | if (ctx->call && ctx->call->fundef) |
1566 | current_function_decl = ctx->call->fundef->decl; |
1567 | return fold_builtin_source_location (t); |
1568 | } |
1569 | |
1570 | int strops = 0; |
1571 | int strret = 0; |
1572 | if (fndecl_built_in_p (node: fun, klass: BUILT_IN_NORMAL)) |
1573 | switch (DECL_FUNCTION_CODE (decl: fun)) |
1574 | { |
1575 | case BUILT_IN_STRLEN: |
1576 | case BUILT_IN_STRNLEN: |
1577 | strops = 1; |
1578 | break; |
1579 | case BUILT_IN_MEMCHR: |
1580 | case BUILT_IN_STRCHR: |
1581 | case BUILT_IN_STRRCHR: |
1582 | strops = 1; |
1583 | strret = 1; |
1584 | break; |
1585 | case BUILT_IN_MEMCMP: |
1586 | case BUILT_IN_STRCMP: |
1587 | strops = 2; |
1588 | break; |
1589 | case BUILT_IN_STRSTR: |
1590 | strops = 2; |
1591 | strret = 1; |
1592 | break; |
1593 | case BUILT_IN_ASAN_POINTER_COMPARE: |
1594 | case BUILT_IN_ASAN_POINTER_SUBTRACT: |
1595 | /* These builtins shall be ignored during constant expression |
1596 | evaluation. */ |
1597 | return void_node; |
1598 | case BUILT_IN_UNREACHABLE: |
1599 | case BUILT_IN_TRAP: |
1600 | 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 | if (EXPR_LOCATION (t) == BUILTINS_LOCATION) |
1606 | error ("%<constexpr%> call flows off the end of the function" ); |
1607 | else |
1608 | error ("%q+E is not a constant expression" , t); |
1609 | } |
1610 | *non_constant_p = true; |
1611 | 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 | constexpr_ctx new_ctx = *ctx; |
1619 | new_ctx.quiet = true; |
1620 | for (i = 0; i < nargs; ++i) |
1621 | { |
1622 | tree arg = CALL_EXPR_ARG (t, i); |
1623 | 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 | bool strop = i < strops; |
1628 | if (strop) |
1629 | { |
1630 | STRIP_NOPS (arg); |
1631 | if (TREE_CODE (arg) == ADDR_EXPR) |
1632 | 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 | if (!builtin_valid_in_constant_expr_p (fun) |
1641 | || potential_constant_expression (arg)) |
1642 | { |
1643 | bool dummy1 = false, dummy2 = false; |
1644 | arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue, |
1645 | &dummy1, &dummy2); |
1646 | } |
1647 | |
1648 | 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 | arg = cp_fold_rvalue (arg); |
1652 | else if (strop) |
1653 | { |
1654 | if (TREE_CODE (arg) == CONSTRUCTOR) |
1655 | arg = braced_lists_to_strings (TREE_TYPE (arg), arg); |
1656 | if (TREE_CODE (arg) == STRING_CST) |
1657 | arg = build_address (arg); |
1658 | else |
1659 | arg = oarg; |
1660 | } |
1661 | |
1662 | args[i] = arg; |
1663 | } |
1664 | |
1665 | bool save_ffbcp = force_folding_builtin_constant_p; |
1666 | force_folding_builtin_constant_p |= ctx->manifestly_const_eval == mce_true; |
1667 | tree save_cur_fn = current_function_decl; |
1668 | /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */ |
1669 | if (fndecl_built_in_p (node: fun, name1: BUILT_IN_FUNCTION) |
1670 | && ctx->call |
1671 | && ctx->call->fundef) |
1672 | current_function_decl = ctx->call->fundef->decl; |
1673 | if (fndecl_built_in_p (node: fun, |
1674 | name: CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS, |
1675 | klass: BUILT_IN_FRONTEND)) |
1676 | { |
1677 | location_t loc = EXPR_LOCATION (t); |
1678 | if (nargs >= 1) |
1679 | VERIFY_CONSTANT (args[0]); |
1680 | new_call |
1681 | = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs, |
1682 | args); |
1683 | } |
1684 | else if (fndecl_built_in_p (node: fun, |
1685 | name: CP_BUILT_IN_IS_CORRESPONDING_MEMBER, |
1686 | klass: BUILT_IN_FRONTEND)) |
1687 | { |
1688 | location_t loc = EXPR_LOCATION (t); |
1689 | if (nargs >= 2) |
1690 | { |
1691 | VERIFY_CONSTANT (args[0]); |
1692 | VERIFY_CONSTANT (args[1]); |
1693 | } |
1694 | new_call = fold_builtin_is_corresponding_member (loc, nargs, args); |
1695 | } |
1696 | else |
1697 | new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t), |
1698 | CALL_EXPR_FN (t), nargs, args); |
1699 | current_function_decl = save_cur_fn; |
1700 | force_folding_builtin_constant_p = save_ffbcp; |
1701 | if (new_call == NULL) |
1702 | { |
1703 | if (!*non_constant_p && !ctx->quiet) |
1704 | { |
1705 | new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t), |
1706 | CALL_EXPR_FN (t), nargs, args); |
1707 | error ("%q+E is not a constant expression" , new_call); |
1708 | } |
1709 | *non_constant_p = true; |
1710 | return t; |
1711 | } |
1712 | |
1713 | if (!potential_constant_expression (new_call)) |
1714 | { |
1715 | if (!*non_constant_p && !ctx->quiet) |
1716 | error ("%q+E is not a constant expression" , new_call); |
1717 | *non_constant_p = true; |
1718 | return t; |
1719 | } |
1720 | |
1721 | 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 | tree op = CALL_EXPR_ARG (t, strret-1); |
1726 | STRIP_NOPS (new_call); |
1727 | if (TREE_CODE (new_call) == POINTER_PLUS_EXPR) |
1728 | TREE_OPERAND (new_call, 0) = op; |
1729 | else if (TREE_CODE (new_call) == ADDR_EXPR) |
1730 | new_call = op; |
1731 | } |
1732 | |
1733 | return cxx_eval_constant_expression (&new_ctx, new_call, lval, |
1734 | 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 | adjust_temp_type (tree type, tree temp) |
1742 | { |
1743 | if (same_type_p (TREE_TYPE (temp), type)) |
1744 | return temp; |
1745 | /* Avoid wrapping an aggregate value in a NOP_EXPR. */ |
1746 | if (TREE_CODE (temp) == CONSTRUCTOR) |
1747 | { |
1748 | /* build_constructor wouldn't retain various CONSTRUCTOR flags. */ |
1749 | tree t = copy_node (temp); |
1750 | TREE_TYPE (t) = type; |
1751 | return t; |
1752 | } |
1753 | if (TREE_CODE (temp) == EMPTY_CLASS_EXPR) |
1754 | return build0 (EMPTY_CLASS_EXPR, type); |
1755 | 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 | 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 | unshare_constructor (tree t MEM_STAT_DECL) |
1770 | { |
1771 | if (!t || TREE_CODE (t) != CONSTRUCTOR) |
1772 | return t; |
1773 | auto_vec <tree*, 4> ptrs; |
1774 | ptrs.safe_push (obj: &t); |
1775 | while (!ptrs.is_empty ()) |
1776 | { |
1777 | tree *p = ptrs.pop (); |
1778 | tree n = copy_node (*p PASS_MEM_STAT); |
1779 | CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT); |
1780 | *p = n; |
1781 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n); |
1782 | constructor_elt *ce; |
1783 | for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, ix: i, ptr: &ce); ++i) |
1784 | if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR) |
1785 | ptrs.safe_push (obj: &ce->value); |
1786 | } |
1787 | return t; |
1788 | } |
1789 | |
1790 | /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */ |
1791 | |
1792 | static void |
1793 | free_constructor (tree t) |
1794 | { |
1795 | if (!t || TREE_CODE (t) != CONSTRUCTOR) |
1796 | return; |
1797 | releasing_vec ctors; |
1798 | vec_safe_push (r&: ctors, t); |
1799 | while (!ctors->is_empty ()) |
1800 | { |
1801 | tree c = ctors->pop (); |
1802 | if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c)) |
1803 | { |
1804 | constructor_elt *ce; |
1805 | for (HOST_WIDE_INT i = 0; vec_safe_iterate (v: elts, ix: i, ptr: &ce); ++i) |
1806 | if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR) |
1807 | vec_safe_push (r&: ctors, t: ce->value); |
1808 | ggc_free (elts); |
1809 | } |
1810 | ggc_free (c); |
1811 | } |
1812 | } |
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 | addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data) |
1820 | { |
1821 | if (TREE_CODE (*tp) == ADDR_EXPR) |
1822 | if (tree var = get_base_address (TREE_OPERAND (*tp, 0))) |
1823 | if (VAR_P (var) && TREE_STATIC (var)) |
1824 | { |
1825 | if (DECL_NAME (var) == heap_uninit_identifier |
1826 | || DECL_NAME (var) == heap_identifier |
1827 | || DECL_NAME (var) == heap_vec_uninit_identifier |
1828 | || DECL_NAME (var) == heap_vec_identifier) |
1829 | return var; |
1830 | |
1831 | constexpr_global_ctx *global = (constexpr_global_ctx *) data; |
1832 | if (global->get_value (t: var)) |
1833 | return var; |
1834 | } |
1835 | if (TYPE_P (*tp)) |
1836 | *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 | 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 | const int nargs = call_expr_nargs (t); |
1852 | tree parms = DECL_ARGUMENTS (fun); |
1853 | int i; |
1854 | /* We don't record ellipsis args below. */ |
1855 | int nparms = list_length (parms); |
1856 | int nbinds = nargs < nparms ? nargs : nparms; |
1857 | tree binds = make_tree_vec (nbinds); |
1858 | for (i = 0; i < nargs; ++i) |
1859 | { |
1860 | tree x, arg; |
1861 | tree type = parms ? TREE_TYPE (parms) : void_type_node; |
1862 | if (parms && DECL_BY_REFERENCE (parms)) |
1863 | type = TREE_TYPE (type); |
1864 | x = get_nth_callarg (t, n: 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 | if (i == 0 && DECL_CONSTRUCTOR_P (fun) |
1869 | && is_dummy_object (x)) |
1870 | { |
1871 | x = ctx->object; |
1872 | x = build_address (x); |
1873 | } |
1874 | if (TREE_ADDRESSABLE (type)) |
1875 | /* Undo convert_for_arg_passing work here. */ |
1876 | 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 | 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 | if (i == 0 && DECL_OBJECT_MEMBER_FUNCTION_P (fun) |
1885 | && 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 | && !(TREE_CODE (t) == CALL_EXPR && CALL_FROM_THUNK_P (t))) |
1890 | { |
1891 | if (!ctx->quiet) |
1892 | error_at (cp_expr_loc_or_input_loc (t: x), |
1893 | "dereferencing a null pointer" ); |
1894 | *non_constant_p = true; |
1895 | } |
1896 | /* Don't VERIFY_CONSTANT here. */ |
1897 | if (*non_constant_p && ctx->quiet) |
1898 | break; |
1899 | /* Just discard ellipsis args after checking their constantitude. */ |
1900 | if (!parms) |
1901 | continue; |
1902 | |
1903 | if (!*non_constant_p) |
1904 | { |
1905 | /* Make sure the binding has the same type as the parm. But |
1906 | only for constant args. */ |
1907 | if (!TYPE_REF_P (type)) |
1908 | arg = adjust_temp_type (type, temp: arg); |
1909 | if (!TREE_CONSTANT (arg)) |
1910 | *non_constant_args = true; |
1911 | else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
1912 | /* The destructor needs to see any modifications the callee makes |
1913 | to the argument. */ |
1914 | *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 | else if (!*non_constant_args |
1921 | && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global, |
1922 | NULL)) |
1923 | *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 | if (i == 0 && DECL_VIRTUAL_P (fun)) |
1929 | { |
1930 | tree addr = arg; |
1931 | STRIP_NOPS (addr); |
1932 | if (TREE_CODE (addr) == ADDR_EXPR) |
1933 | { |
1934 | tree obj = TREE_OPERAND (addr, 0); |
1935 | while (TREE_CODE (obj) == COMPONENT_REF |
1936 | && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1)) |
1937 | && !same_type_ignoring_top_level_qualifiers_p |
1938 | (TREE_TYPE (obj), DECL_CONTEXT (fun))) |
1939 | obj = TREE_OPERAND (obj, 0); |
1940 | if (obj != TREE_OPERAND (addr, 0)) |
1941 | arg = build_fold_addr_expr_with_type (obj, |
1942 | TREE_TYPE (arg)); |
1943 | } |
1944 | } |
1945 | TREE_VEC_ELT (binds, i) = arg; |
1946 | } |
1947 | parms = TREE_CHAIN (parms); |
1948 | } |
1949 | |
1950 | 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 | push_cx_call_context (tree call) |
1963 | { |
1964 | ++call_stack_tick; |
1965 | if (!EXPR_HAS_LOCATION (call)) |
1966 | SET_EXPR_LOCATION (call, input_location); |
1967 | call_stack.safe_push (obj: call); |
1968 | int len = call_stack.length (); |
1969 | if (len > max_constexpr_depth) |
1970 | return false; |
1971 | return len; |
1972 | } |
1973 | |
1974 | static void |
1975 | pop_cx_call_context (void) |
1976 | { |
1977 | ++call_stack_tick; |
1978 | call_stack.pop (); |
1979 | } |
1980 | |
1981 | vec<tree> |
1982 | cx_error_context (void) |
1983 | { |
1984 | vec<tree> r = vNULL; |
1985 | if (call_stack_tick != last_cx_error_tick |
1986 | && !call_stack.is_empty ()) |
1987 | r = call_stack; |
1988 | last_cx_error_tick = call_stack_tick; |
1989 | 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 | fold_operand (tree e, const constexpr_ctx *ctx) |
1997 | { |
1998 | if (ctx) |
1999 | { |
2000 | bool new_non_constant_p = false, new_overflow_p = false; |
2001 | e = cxx_eval_constant_expression (ctx, e, vc_prvalue, |
2002 | &new_non_constant_p, |
2003 | &new_overflow_p); |
2004 | } |
2005 | else |
2006 | e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true); |
2007 | 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 | find_failing_clause_r (const constexpr_ctx *ctx, tree expr) |
2019 | { |
2020 | if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR) |
2021 | { |
2022 | /* First check the left side... */ |
2023 | tree e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 0)); |
2024 | if (e == NULL_TREE) |
2025 | /* ...if we didn't find a false clause, check the right side. */ |
2026 | e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 1)); |
2027 | return e; |
2028 | } |
2029 | tree e = contextual_conv_bool (expr, tf_none); |
2030 | e = fold_operand (e, ctx); |
2031 | if (integer_zerop (e)) |
2032 | /* This is the failing clause. */ |
2033 | return expr; |
2034 | return NULL_TREE; |
2035 | } |
2036 | |
2037 | /* Wrapper for find_failing_clause_r. */ |
2038 | |
2039 | tree |
2040 | find_failing_clause (const constexpr_ctx *ctx, tree expr) |
2041 | { |
2042 | if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR) |
2043 | if (tree e = find_failing_clause_r (ctx, expr)) |
2044 | expr = e; |
2045 | 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 | 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 | bad = tree_strip_nop_conversions (bad); |
2059 | if (TREE_CODE (bad) == CLEANUP_POINT_EXPR) |
2060 | bad = TREE_OPERAND (bad, 0); |
2061 | |
2062 | /* Actually explain the failure if this is a concept check or a |
2063 | requires-expression. */ |
2064 | if (concept_check_p (t: bad) || TREE_CODE (bad) == REQUIRES_EXPR) |
2065 | diagnose_constraints (cloc, bad, NULL_TREE); |
2066 | else if (COMPARISON_CLASS_P (bad) |
2067 | && ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0)))) |
2068 | { |
2069 | tree op0 = fold_operand (TREE_OPERAND (bad, 0), ctx); |
2070 | tree op1 = fold_operand (TREE_OPERAND (bad, 1), ctx); |
2071 | tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1); |
2072 | inform (cloc, "the comparison reduces to %qE" , cond); |
2073 | } |
2074 | else if (show_expr_p) |
2075 | inform (cloc, "%qE evaluates to false" , bad); |
2076 | } |
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 | 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 | if (*non_constant_p) |
2088 | return true; |
2089 | |
2090 | tree eval; |
2091 | if (!evaluated) |
2092 | { |
2093 | if (!potential_rvalue_constant_expression (arg)) |
2094 | return true; |
2095 | |
2096 | constexpr_ctx new_ctx = *ctx; |
2097 | new_ctx.quiet = true; |
2098 | bool new_non_constant_p = false, new_overflow_p = false; |
2099 | /* Avoid modification of existing values. */ |
2100 | modifiable_tracker ms (new_ctx.global); |
2101 | eval = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue, |
2102 | &new_non_constant_p, |
2103 | &new_overflow_p); |
2104 | } |
2105 | else |
2106 | eval = cxx_eval_constant_expression (ctx, arg, vc_prvalue, |
2107 | non_constant_p, |
2108 | overflow_p); |
2109 | if (!*non_constant_p && integer_zerop (eval)) |
2110 | { |
2111 | if (!ctx->quiet) |
2112 | { |
2113 | /* See if we can find which clause was failing |
2114 | (for logical AND). */ |
2115 | tree bad = find_failing_clause (ctx, expr: arg); |
2116 | /* If not, or its location is unusable, fall back to the |
2117 | previous location. */ |
2118 | location_t cloc = cp_expr_loc_or_loc (t: bad, or_loc: loc); |
2119 | |
2120 | /* Report the error. */ |
2121 | auto_diagnostic_group d; |
2122 | error_at (cloc, msg); |
2123 | diagnose_failing_condition (bad, cloc, show_expr_p: true, ctx); |
2124 | return bad; |
2125 | } |
2126 | *non_constant_p = true; |
2127 | 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 | cxx_eval_internal_function (const constexpr_ctx *ctx, tree t, |
2139 | value_cat lval, |
2140 | bool *non_constant_p, bool *overflow_p) |
2141 | { |
2142 | enum tree_code opcode = ERROR_MARK; |
2143 | |
2144 | switch (CALL_EXPR_IFN (t)) |
2145 | { |
2146 | case IFN_UBSAN_NULL: |
2147 | case IFN_UBSAN_BOUNDS: |
2148 | case IFN_UBSAN_VPTR: |
2149 | case IFN_FALLTHROUGH: |
2150 | return void_node; |
2151 | |
2152 | case IFN_ASSUME: |
2153 | if (!cxx_eval_assert (ctx, CALL_EXPR_ARG (t, 0), |
2154 | G_("failed %<assume%> attribute assumption" ), |
2155 | EXPR_LOCATION (t), /*eval*/evaluated: false, |
2156 | non_constant_p, overflow_p)) |
2157 | return t; |
2158 | return void_node; |
2159 | |
2160 | case IFN_ADD_OVERFLOW: |
2161 | opcode = PLUS_EXPR; |
2162 | break; |
2163 | case IFN_SUB_OVERFLOW: |
2164 | opcode = MINUS_EXPR; |
2165 | break; |
2166 | case IFN_MUL_OVERFLOW: |
2167 | opcode = MULT_EXPR; |
2168 | break; |
2169 | |
2170 | case IFN_LAUNDER: |
2171 | return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), |
2172 | vc_prvalue, non_constant_p, |
2173 | overflow_p); |
2174 | |
2175 | case IFN_VEC_CONVERT: |
2176 | { |
2177 | tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), |
2178 | vc_prvalue, non_constant_p, |
2179 | overflow_p); |
2180 | if (TREE_CODE (arg) == VECTOR_CST) |
2181 | if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg)) |
2182 | return r; |
2183 | } |
2184 | /* FALLTHRU */ |
2185 | |
2186 | default: |
2187 | if (!ctx->quiet) |
2188 | error_at (cp_expr_loc_or_input_loc (t), |
2189 | "call to internal function %qE" , t); |
2190 | *non_constant_p = true; |
2191 | return t; |
2192 | } |
2193 | |
2194 | /* Evaluate constant arguments using OPCODE and return a complex |
2195 | number containing the result and the overflow bit. */ |
2196 | tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval, |
2197 | non_constant_p, overflow_p); |
2198 | tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval, |
2199 | non_constant_p, overflow_p); |
2200 | |
2201 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) |
2202 | { |
2203 | location_t loc = cp_expr_loc_or_input_loc (t); |
2204 | tree type = TREE_TYPE (TREE_TYPE (t)); |
2205 | tree result = fold_binary_loc (loc, opcode, type, |
2206 | fold_convert_loc (loc, type, arg0), |
2207 | fold_convert_loc (loc, type, arg1)); |
2208 | tree ovf |
2209 | = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1)); |
2210 | /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */ |
2211 | if (TREE_OVERFLOW (result)) |
2212 | TREE_OVERFLOW (result) = 0; |
2213 | |
2214 | return build_complex (TREE_TYPE (t), result, ovf); |
2215 | } |
2216 | |
2217 | *non_constant_p = true; |
2218 | return t; |
2219 | } |
2220 | |
2221 | /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */ |
2222 | |
2223 | static void |
2224 | clear_no_implicit_zero (tree ctor) |
2225 | { |
2226 | if (CONSTRUCTOR_NO_CLEARING (ctor)) |
2227 | { |
2228 | CONSTRUCTOR_NO_CLEARING (ctor) = false; |
2229 | for (auto &e: CONSTRUCTOR_ELTS (ctor)) |
2230 | if (TREE_CODE (e.value) == CONSTRUCTOR) |
2231 | clear_no_implicit_zero (ctor: e.value); |
2232 | } |
2233 | } |
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 | modifying_const_object_error (tree expr, tree obj) |
2240 | { |
2241 | location_t loc = cp_expr_loc_or_input_loc (t: expr); |
2242 | auto_diagnostic_group d; |
2243 | error_at (loc, "modifying a const object %qE is not allowed in " |
2244 | "a constant expression" , TREE_OPERAND (expr, 0)); |
2245 | |
2246 | /* Find the underlying object that was declared as const. */ |
2247 | location_t decl_loc = UNKNOWN_LOCATION; |
2248 | for (tree probe = obj; decl_loc == UNKNOWN_LOCATION; ) |
2249 | switch (TREE_CODE (probe)) |
2250 | { |
2251 | case BIT_FIELD_REF: |
2252 | case COMPONENT_REF: |
2253 | { |
2254 | tree elt = TREE_OPERAND (probe, 1); |
2255 | if (CP_TYPE_CONST_P (TREE_TYPE (elt))) |
2256 | decl_loc = DECL_SOURCE_LOCATION (elt); |
2257 | probe = TREE_OPERAND (probe, 0); |
2258 | } |
2259 | break; |
2260 | |
2261 | case ARRAY_REF: |
2262 | case REALPART_EXPR: |
2263 | case IMAGPART_EXPR: |
2264 | probe = TREE_OPERAND (probe, 0); |
2265 | break; |
2266 | |
2267 | default: |
2268 | decl_loc = location_of (probe); |
2269 | break; |
2270 | } |
2271 | inform (decl_loc, "originally declared %<const%> here" ); |
2272 | } |
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 | cxx_replaceable_global_alloc_fn (tree fndecl) |
2279 | { |
2280 | return (cxx_dialect >= cxx20 |
2281 | && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl)) |
2282 | && CP_DECL_CONTEXT (fndecl) == global_namespace |
2283 | && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl) |
2284 | || 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 | cxx_placement_new_fn (tree fndecl) |
2292 | { |
2293 | if (cxx_dialect >= cxx20 |
2294 | && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl)) |
2295 | && CP_DECL_CONTEXT (fndecl) == global_namespace |
2296 | && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl) |
2297 | && TREE_CODE (TREE_TYPE (fndecl)) == FUNCTION_TYPE) |
2298 | { |
2299 | tree first_arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl))); |
2300 | if (TREE_VALUE (first_arg) == ptr_type_node |
2301 | && TREE_CHAIN (first_arg) == void_list_node) |
2302 | return true; |
2303 | } |
2304 | return false; |
2305 | } |
2306 | |
2307 | /* Return true if FNDECL is std::construct_at. */ |
2308 | |
2309 | static inline bool |
2310 | is_std_construct_at (tree fndecl) |
2311 | { |
2312 | if (!decl_in_std_namespace_p (fndecl)) |
2313 | return false; |
2314 | |
2315 | tree name = DECL_NAME (fndecl); |
2316 | return name && id_equal (id: name, str: "construct_at" ); |
2317 | } |
2318 | |
2319 | /* Overload for the above taking constexpr_call*. */ |
2320 | |
2321 | static inline bool |
2322 | is_std_construct_at (const constexpr_call *call) |
2323 | { |
2324 | return (call |
2325 | && call->fundef |
2326 | && is_std_construct_at (fndecl: call->fundef->decl)); |
2327 | } |
2328 | |
2329 | /* True if CTX is an instance of std::allocator. */ |
2330 | |
2331 | bool |
2332 | is_std_allocator (tree ctx) |
2333 | { |
2334 | if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx)) |
2335 | return false; |
2336 | |
2337 | tree decl = TYPE_MAIN_DECL (ctx); |
2338 | tree name = DECL_NAME (decl); |
2339 | if (name == NULL_TREE || !id_equal (id: name, str: "allocator" )) |
2340 | return false; |
2341 | |
2342 | 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 | is_std_allocator_allocate (tree fndecl) |
2349 | { |
2350 | tree name = DECL_NAME (fndecl); |
2351 | if (name == NULL_TREE |
2352 | || !(id_equal (id: name, str: "allocate" ) || id_equal (id: name, str: "deallocate" ))) |
2353 | return false; |
2354 | |
2355 | return is_std_allocator (DECL_CONTEXT (fndecl)); |
2356 | } |
2357 | |
2358 | /* Overload for the above taking constexpr_call*. */ |
2359 | |
2360 | static inline bool |
2361 | is_std_allocator_allocate (const constexpr_call *call) |
2362 | { |
2363 | return (call |
2364 | && call->fundef |
2365 | && is_std_allocator_allocate (fndecl: call->fundef->decl)); |
2366 | } |
2367 | |
2368 | /* Return true if FNDECL is std::source_location::current. */ |
2369 | |
2370 | static inline bool |
2371 | is_std_source_location_current (tree fndecl) |
2372 | { |
2373 | if (!decl_in_std_namespace_p (fndecl)) |
2374 | return false; |
2375 | |
2376 | tree name = DECL_NAME (fndecl); |
2377 | if (name == NULL_TREE || !id_equal (id: name, str: "current" )) |
2378 | return false; |
2379 | |
2380 | tree ctx = DECL_CONTEXT (fndecl); |
2381 | if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx)) |
2382 | return false; |
2383 | |
2384 | name = DECL_NAME (TYPE_MAIN_DECL (ctx)); |
2385 | return name && id_equal (id: name, str: "source_location" ); |
2386 | } |
2387 | |
2388 | /* Overload for the above taking constexpr_call*. */ |
2389 | |
2390 | static inline bool |
2391 | is_std_source_location_current (const constexpr_call *call) |
2392 | { |
2393 | return (call |
2394 | && call->fundef |
2395 | && is_std_source_location_current (fndecl: call->fundef->decl)); |
2396 | } |
2397 | |
2398 | /* Return true if FNDECL is __dynamic_cast. */ |
2399 | |
2400 | static inline bool |
2401 | cxx_dynamic_cast_fn_p (tree fndecl) |
2402 | { |
2403 | return (cxx_dialect >= cxx20 |
2404 | && id_equal (DECL_NAME (fndecl), str: "__dynamic_cast" ) |
2405 | && 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 | (tree expr) |
2413 | { |
2414 | if (TREE_CODE (expr) == POINTER_PLUS_EXPR) |
2415 | expr = TREE_OPERAND (expr, 0); |
2416 | STRIP_NOPS (expr); |
2417 | if (TREE_CODE (expr) == ADDR_EXPR) |
2418 | expr = TREE_OPERAND (expr, 0); |
2419 | 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 | get_component_with_type (tree path, tree type, tree stop) |
2432 | { |
2433 | while (true) |
2434 | { |
2435 | if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type)) |
2436 | /* Found it. */ |
2437 | return path; |
2438 | else if (stop |
2439 | && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), |
2440 | stop))) |
2441 | return NULL_TREE; |
2442 | else if (TREE_CODE (path) == COMPONENT_REF |
2443 | && 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 | if (TREE_PRIVATE (TREE_OPERAND (path, 1)) |
2448 | || TREE_PROTECTED (TREE_OPERAND (path, 1))) |
2449 | return error_mark_node; |
2450 | 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 | 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 | gcc_assert (call_expr_nargs (call) == 4); |
2485 | tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; |
2486 | tree obj = CALL_EXPR_ARG (call, 0); |
2487 | tree type = CALL_EXPR_ARG (call, 2); |
2488 | HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3)); |
2489 | location_t loc = cp_expr_loc_or_input_loc (t: call); |
2490 | |
2491 | /* Get the target type of the dynamic_cast. */ |
2492 | gcc_assert (TREE_CODE (type) == ADDR_EXPR); |
2493 | type = TREE_OPERAND (type, 0); |
2494 | 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 | bool reference_p = false; |
2500 | while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR) |
2501 | { |
2502 | reference_p |= TYPE_REF_P (TREE_TYPE (obj)); |
2503 | obj = TREE_OPERAND (obj, 0); |
2504 | } |
2505 | |
2506 | /* Evaluate the object so that we know its dynamic type. */ |
2507 | obj = cxx_eval_constant_expression (ctx, obj, vc_prvalue, non_constant_p, |
2508 | overflow_p); |
2509 | 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 | obj = extract_obj_from_addr_offset (expr: obj); |
2516 | const tree objtype = TREE_TYPE (obj); |
2517 | /* If OBJ doesn't refer to a base field, we're done. */ |
2518 | if (tree t = (TREE_CODE (obj) == COMPONENT_REF |
2519 | ? TREE_OPERAND (obj, 1) : obj)) |
2520 | if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t)) |
2521 | { |
2522 | if (reference_p) |
2523 | { |
2524 | if (!ctx->quiet) |
2525 | { |
2526 | auto_diagnostic_group d; |
2527 | error_at (loc, "reference %<dynamic_cast%> failed" ); |
2528 | inform (loc, "dynamic type %qT of its operand does " |
2529 | "not have a base class of type %qT" , |
2530 | objtype, type); |
2531 | } |
2532 | *non_constant_p = true; |
2533 | } |
2534 | 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 | tree vtable = build_vfield_ref (obj, objtype); |
2543 | vtable = cxx_eval_constant_expression (ctx, vtable, vc_prvalue, |
2544 | non_constant_p, overflow_p); |
2545 | 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 | if (integer_zerop (vtable)) |
2550 | { |
2551 | if (!ctx->quiet) |
2552 | error_at (loc, "virtual table pointer is used uninitialized" ); |
2553 | *non_constant_p = true; |
2554 | return integer_zero_node; |
2555 | } |
2556 | /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */ |
2557 | vtable = extract_obj_from_addr_offset (expr: vtable); |
2558 | 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 | if (hint >= 0 || hint == -3) |
2572 | { |
2573 | /* Look for a component with type TYPE. */ |
2574 | tree t = get_component_with_type (path: obj, type, stop: mdtype); |
2575 | /* If not accessible, give an error. */ |
2576 | if (t == error_mark_node) |
2577 | { |
2578 | if (reference_p) |
2579 | { |
2580 | if (!ctx->quiet) |
2581 | { |
2582 | auto_diagnostic_group d; |
2583 | error_at (loc, "reference %<dynamic_cast%> failed" ); |
2584 | inform (loc, "static type %qT of its operand is a " |
2585 | "non-public base class of dynamic type %qT" , |
2586 | objtype, type); |
2587 | |
2588 | } |
2589 | *non_constant_p = true; |
2590 | } |
2591 | return integer_zero_node; |
2592 | } |
2593 | else if (t) |
2594 | /* The result points to the TYPE object. */ |
2595 | 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 | obj = get_component_with_type (path: obj, type: mdtype, NULL_TREE); |
2609 | if (obj == error_mark_node) |
2610 | { |
2611 | if (reference_p) |
2612 | { |
2613 | if (!ctx->quiet) |
2614 | { |
2615 | auto_diagnostic_group d; |
2616 | error_at (loc, "reference %<dynamic_cast%> failed" ); |
2617 | inform (loc, "static type %qT of its operand is a non-public" |
2618 | " base class of dynamic type %qT" , objtype, mdtype); |
2619 | } |
2620 | *non_constant_p = true; |
2621 | } |
2622 | return integer_zero_node; |
2623 | } |
2624 | else |
2625 | 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 | base_kind b_kind; |
2630 | tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none); |
2631 | if (!binfo || binfo == error_mark_node) |
2632 | { |
2633 | if (reference_p) |
2634 | { |
2635 | if (!ctx->quiet) |
2636 | { |
2637 | auto_diagnostic_group d; |
2638 | error_at (loc, "reference %<dynamic_cast%> failed" ); |
2639 | if (b_kind == bk_ambig) |
2640 | inform (loc, "%qT is an ambiguous base class of dynamic " |
2641 | "type %qT of its operand" , type, mdtype); |
2642 | else |
2643 | inform (loc, "dynamic type %qT of its operand does not " |
2644 | "have an unambiguous public base class %qT" , |
2645 | mdtype, type); |
2646 | } |
2647 | *non_constant_p = true; |
2648 | } |
2649 | return integer_zero_node; |
2650 | } |
2651 | /* If so, return the TYPE subobject of the most derived object. */ |
2652 | obj = convert_to_base_statically (obj, binfo); |
2653 | 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 | replace_decl_r (tree *tp, int *walk_subtrees, void *data) |
2674 | { |
2675 | replace_decl_data *d = (replace_decl_data *) data; |
2676 | |
2677 | if (*tp == d->decl) |
2678 | { |
2679 | *tp = unshare_expr (d->replacement); |
2680 | d->changed = true; |
2681 | *walk_subtrees = 0; |
2682 | } |
2683 | else if (TYPE_P (*tp) |
2684 | || d->pset->add (k: *tp)) |
2685 | *walk_subtrees = 0; |
2686 | |
2687 | 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 | replace_decl (tree *tp, tree decl, tree replacement) |
2696 | { |
2697 | gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p |
2698 | (TREE_TYPE (decl), TREE_TYPE (replacement))); |
2699 | hash_set<tree> pset; |
2700 | replace_decl_data data = { .decl: decl, .replacement: replacement, .pset: &pset, .changed: false }; |
2701 | cp_walk_tree (tp, replace_decl_r, &data, NULL); |
2702 | return data.changed; |
2703 | } |
2704 | |
2705 | /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */ |
2706 | |
2707 | static tree |
2708 | 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 | tree function = THUNK_TARGET (thunk_fndecl); |
2713 | |
2714 | if (THUNK_VIRTUAL_OFFSET (thunk_fndecl)) |
2715 | { |
2716 | if (!ctx->quiet) |
2717 | { |
2718 | if (!DECL_DECLARED_CONSTEXPR_P (function)) |
2719 | { |
2720 | error ("call to non-%<constexpr%> function %qD" , function); |
2721 | explain_invalid_constexpr_fn (fun: 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 | error ("calling constexpr member function %qD through virtual " |
2727 | "base subobject" , function); |
2728 | } |
2729 | *non_constant_p = true; |
2730 | return t; |
2731 | } |
2732 | |
2733 | tree new_call = copy_node (t); |
2734 | CALL_EXPR_FN (new_call) = function; |
2735 | TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function)); |
2736 | |
2737 | tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl)); |
2738 | |
2739 | if (DECL_THIS_THUNK_P (thunk_fndecl)) |
2740 | { |
2741 | /* 'this'-adjusting thunk. */ |
2742 | tree this_arg = CALL_EXPR_ARG (t, 0); |
2743 | this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg), |
2744 | this_arg, offset); |
2745 | CALL_EXPR_ARG (new_call, 0) = this_arg; |
2746 | } |
2747 | else |
2748 | /* Return-adjusting thunk. */ |
2749 | new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call), |
2750 | new_call, offset); |
2751 | |
2752 | return cxx_eval_constant_expression (ctx, new_call, lval, |
2753 | 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 | cxx_set_object_constness (const constexpr_ctx *ctx, tree object, |
2762 | bool readonly_p, bool *non_constant_p, |
2763 | bool *overflow_p) |
2764 | { |
2765 | if (CLASS_TYPE_P (TREE_TYPE (object)) |
2766 | && 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 | tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue, |
2771 | non_constant_p, overflow_p); |
2772 | if (TREE_CODE (e) == CONSTRUCTOR && !*non_constant_p) |
2773 | TREE_READONLY (e) = readonly_p; |
2774 | } |
2775 | } |
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 | 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 | if (concept_check_p (t)) |
2788 | return evaluate_concept_check (t); |
2789 | |
2790 | location_t loc = cp_expr_loc_or_input_loc (t); |
2791 | tree fun = get_function_named_in_call (t); |
2792 | constexpr_call new_call |
2793 | = { NULL, NULL, NULL, .hash: 0, .manifestly_const_eval: ctx->manifestly_const_eval }; |
2794 | int depth_ok; |
2795 | |
2796 | if (fun == NULL_TREE) |
2797 | return cxx_eval_internal_function (ctx, t, lval, |
2798 | non_constant_p, overflow_p); |
2799 | |
2800 | if (TREE_CODE (fun) != FUNCTION_DECL) |
2801 | { |
2802 | /* Might be a constexpr function pointer. */ |
2803 | fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue, |
2804 | non_constant_p, overflow_p); |
2805 | STRIP_NOPS (fun); |
2806 | if (TREE_CODE (fun) == ADDR_EXPR) |
2807 | 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), dindex: 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 | if (TREE_CODE (fun) != FUNCTION_DECL) |
2840 | { |
2841 | if (!ctx->quiet && !*non_constant_p) |
2842 | error_at (loc, "expression %qE does not designate a %<constexpr%> " |
2843 | "function" , fun); |
2844 | *non_constant_p = true; |
2845 | return t; |
2846 | } |
2847 | if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)) |
2848 | fun = DECL_CLONED_FUNCTION (fun); |
2849 | |
2850 | if (is_ubsan_builtin_p (fun)) |
2851 | return void_node; |
2852 | |
2853 | if (fndecl_built_in_p (node: fun)) |
2854 | return cxx_eval_builtin_function_call (ctx, t, fun, |
2855 | lval, non_constant_p, overflow_p); |
2856 | if (DECL_THUNK_P (fun)) |
2857 | return cxx_eval_thunk_call (ctx, t, thunk_fndecl: fun, lval, non_constant_p, overflow_p); |
2858 | if (!maybe_constexpr_fn (fun)) |
2859 | { |
2860 | if (TREE_CODE (t) == CALL_EXPR |
2861 | && cxx_replaceable_global_alloc_fn (fndecl: fun) |
2862 | && (CALL_FROM_NEW_OR_DELETE_P (t) |
2863 | || is_std_allocator_allocate (call: ctx->call))) |
2864 | { |
2865 | const bool new_op_p = IDENTIFIER_NEW_OP_P (DECL_NAME (fun)); |
2866 | const int nargs = call_expr_nargs (t); |
2867 | tree arg0 = NULL_TREE; |
2868 | for (int i = 0; i < nargs; ++i) |
2869 | { |
2870 | tree arg = CALL_EXPR_ARG (t, i); |
2871 | 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 | if (new_op_p || i != 0) |
2876 | VERIFY_CONSTANT (arg); |
2877 | if (i == 0) |
2878 | arg0 = arg; |
2879 | } |
2880 | gcc_assert (arg0); |
2881 | if (new_op_p) |
2882 | { |
2883 | tree type = build_array_type_nelts (char_type_node, |
2884 | tree_to_uhwi (arg0)); |
2885 | tree var = build_decl (loc, VAR_DECL, |
2886 | (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) |
2887 | & OVL_OP_FLAG_VEC) |
2888 | ? heap_vec_uninit_identifier |
2889 | : heap_uninit_identifier, |
2890 | type); |
2891 | DECL_ARTIFICIAL (var) = 1; |
2892 | 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 | varpool_node::finalize_decl (decl: var); |
2900 | ctx->global->heap_vars.safe_push (obj: var); |
2901 | ctx->global->put_value (t: var, NULL_TREE); |
2902 | return fold_convert (ptr_type_node, build_address (var)); |
2903 | } |
2904 | else |
2905 | { |
2906 | STRIP_NOPS (arg0); |
2907 | if (TREE_CODE (arg0) == ADDR_EXPR |
2908 | && VAR_P (TREE_OPERAND (arg0, 0))) |
2909 | { |
2910 | tree var = TREE_OPERAND (arg0, 0); |
2911 | if (DECL_NAME (var) == heap_uninit_identifier |
2912 | || DECL_NAME (var) == heap_identifier) |
2913 | { |
2914 | if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) |
2915 | & OVL_OP_FLAG_VEC) |
2916 | { |
2917 | if (!ctx->quiet) |
2918 | { |
2919 | auto_diagnostic_group d; |
2920 | error_at (loc, "array deallocation of object " |
2921 | "allocated with non-array " |
2922 | "allocation" ); |
2923 | inform (DECL_SOURCE_LOCATION (var), |
2924 | "allocation performed here" ); |
2925 | } |
2926 | *non_constant_p = true; |
2927 | return t; |
2928 | } |
2929 | DECL_NAME (var) = heap_deleted_identifier; |
2930 | ctx->global->destroy_value (t: var); |
2931 | ctx->global->heap_dealloc_count++; |
2932 | return void_node; |
2933 | } |
2934 | else if (DECL_NAME (var) == heap_vec_uninit_identifier |
2935 | || DECL_NAME (var) == heap_vec_identifier) |
2936 | { |
2937 | if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) |
2938 | & OVL_OP_FLAG_VEC) == 0) |
2939 | { |
2940 | if (!ctx->quiet) |
2941 | { |
2942 | auto_diagnostic_group d; |
2943 | error_at (loc, "non-array deallocation of " |
2944 | "object allocated with array " |
2945 | "allocation" ); |
2946 | inform (DECL_SOURCE_LOCATION (var), |
2947 | "allocation performed here" ); |
2948 | } |
2949 | *non_constant_p = true; |
2950 | return t; |
2951 | } |
2952 | DECL_NAME (var) = heap_deleted_identifier; |
2953 | ctx->global->destroy_value (t: var); |
2954 | ctx->global->heap_dealloc_count++; |
2955 | return void_node; |
2956 | } |
2957 | else if (DECL_NAME (var) == heap_deleted_identifier) |
2958 | { |
2959 | if (!ctx->quiet) |
2960 | error_at (loc, "deallocation of already deallocated " |
2961 | "storage" ); |
2962 | *non_constant_p = true; |
2963 | return t; |
2964 | } |
2965 | } |
2966 | if (!ctx->quiet) |
2967 | error_at (loc, "deallocation of storage that was " |
2968 | "not previously allocated" ); |
2969 | *non_constant_p = true; |
2970 | return t; |
2971 | } |
2972 | } |
2973 | /* Allow placement new in std::construct_at, just return the second |
2974 | argument. */ |
2975 | if (TREE_CODE (t) == CALL_EXPR |
2976 | && cxx_placement_new_fn (fndecl: fun) |
2977 | && is_std_construct_at (call: ctx->call)) |
2978 | { |
2979 | const int nargs = call_expr_nargs (t); |
2980 | tree arg1 = NULL_TREE; |
2981 | for (int i = 0; i < nargs; ++i) |
2982 | { |
2983 | tree arg = CALL_EXPR_ARG (t, i); |
2984 | arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue, |
2985 | non_constant_p, overflow_p); |
2986 | if (i == 1) |
2987 | arg1 = arg; |
2988 | else |
2989 | VERIFY_CONSTANT (arg); |
2990 | } |
2991 | gcc_assert (arg1); |
2992 | return arg1; |
2993 | } |
2994 | else if (cxx_dynamic_cast_fn_p (fndecl: fun)) |
2995 | return cxx_eval_dynamic_cast_fn (ctx, call: t, non_constant_p, overflow_p); |
2996 | |
2997 | if (!ctx->quiet) |
2998 | { |
2999 | if (!lambda_static_thunk_p (fun)) |
3000 | error_at (loc, "call to non-%<constexpr%> function %qD" , fun); |
3001 | explain_invalid_constexpr_fn (fun); |
3002 | } |
3003 | *non_constant_p = true; |
3004 | return t; |
3005 | } |
3006 | |
3007 | constexpr_ctx new_ctx = *ctx; |
3008 | if (DECL_CONSTRUCTOR_P (fun) && !ctx->object |
3009 | && 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 | new_ctx.object = AGGR_INIT_EXPR_SLOT (t); |
3014 | tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL); |
3015 | CONSTRUCTOR_NO_CLEARING (ctor) = true; |
3016 | ctx->global->put_value (t: new_ctx.object, v: ctor); |
3017 | 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 | 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 | bool non_constant_args = false; |
3029 | new_call.bindings |
3030 | = cxx_bind_parameters_in_call (ctx, t, fun, non_constant_p, |
3031 | overflow_p, non_constant_args: &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 | class free_bindings |
3037 | { |
3038 | tree *bindings; |
3039 | public: |
3040 | free_bindings (tree &b): bindings (&b) { } |
3041 | ~free_bindings () { if (bindings) ggc_free (*bindings); } |
3042 | void preserve () { bindings = NULL; } |
3043 | } fb (new_call.bindings); |
3044 | |
3045 | if (*non_constant_p) |
3046 | return t; |
3047 | |
3048 | /* We can't defer instantiating the function any longer. */ |
3049 | if (!DECL_INITIAL (fun) |
3050 | && (DECL_TEMPLOID_INSTANTIATION (fun) || DECL_DEFAULTED_FN (fun)) |
3051 | && !uid_sensitive_constexpr_evaluation_p ()) |
3052 | { |
3053 | location_t save_loc = input_location; |
3054 | input_location = loc; |
3055 | ++function_depth; |
3056 | if (ctx->manifestly_const_eval == mce_true) |
3057 | FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true; |
3058 | if (DECL_TEMPLOID_INSTANTIATION (fun)) |
3059 | instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false); |
3060 | else |
3061 | synthesize_method (fun); |
3062 | --function_depth; |
3063 | input_location = save_loc; |
3064 | } |
3065 | |
3066 | /* If in direct recursive call, optimize definition search. */ |
3067 | if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun) |
3068 | new_call.fundef = ctx->call->fundef; |
3069 | else |
3070 | { |
3071 | new_call.fundef = retrieve_constexpr_fundef (fun); |
3072 | if (new_call.fundef == NULL || new_call.fundef->body == NULL |
3073 | || new_call.fundef->result == error_mark_node |
3074 | || fun == current_function_decl) |
3075 | { |
3076 | 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 | if (DECL_INITIAL (fun) == error_mark_node |
3083 | || fun == current_function_decl) |
3084 | error_at (loc, "%qD called in a constant expression before its " |
3085 | "definition is complete" , fun); |
3086 | 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 | if (!lambda_static_thunk_p (fun)) |
3091 | error_at (loc, "%qD called in a constant expression" , fun); |
3092 | explain_invalid_constexpr_fn (fun); |
3093 | } |
3094 | else |
3095 | error_at (loc, "%qD used before its definition" , fun); |
3096 | } |
3097 | *non_constant_p = true; |
3098 | return t; |
3099 | } |
3100 | } |
3101 | |
3102 | depth_ok = push_cx_call_context (call: t); |
3103 | |
3104 | /* Remember the object we are constructing or destructing. */ |
3105 | tree new_obj = NULL_TREE; |
3106 | 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 | new_obj = TREE_VEC_ELT (new_call.bindings, 0); |
3112 | new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj); |
3113 | |
3114 | if (ctx->call && ctx->call->fundef |
3115 | && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl)) |
3116 | { |
3117 | tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0); |
3118 | STRIP_NOPS (cur_obj); |
3119 | if (TREE_CODE (cur_obj) == ADDR_EXPR) |
3120 | cur_obj = TREE_OPERAND (cur_obj, 0); |
3121 | 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 | new_obj = NULL_TREE; |
3127 | } |
3128 | } |
3129 | |
3130 | tree result = NULL_TREE; |
3131 | |
3132 | constexpr_call *entry = NULL; |
3133 | if (depth_ok && !non_constant_args && ctx->strict) |
3134 | { |
3135 | new_call.hash = constexpr_fundef_hasher::hash (fundef: new_call.fundef); |
3136 | new_call.hash |
3137 | = iterative_hash_template_arg (arg: new_call.bindings, val: new_call.hash); |
3138 | new_call.hash |
3139 | = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash); |
3140 | |
3141 | /* If we have seen this call before, we are done. */ |
3142 | maybe_initialize_constexpr_call_table (); |
3143 | bool insert = depth_ok < constexpr_cache_depth; |
3144 | constexpr_call **slot |
3145 | = constexpr_call_table->find_slot (value: &new_call, |
3146 | insert: insert ? INSERT : NO_INSERT); |
3147 | entry = slot ? *slot : NULL; |
3148 | if (entry == NULL) |
3149 | { |
3150 | /* Only cache up to constexpr_cache_depth to limit memory use. */ |
3151 | 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 | *slot = entry = ggc_alloc<constexpr_call> (); |
3156 | *entry = new_call; |
3157 | 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 | else if (entry->result == NULL) |
3165 | { |
3166 | if (!ctx->quiet) |
3167 | error ("call has circular dependency" ); |
3168 | *non_constant_p = true; |
3169 | entry->result = result = error_mark_node; |
3170 | } |
3171 | else |
3172 | result = entry->result; |
3173 | } |
3174 | |
3175 | if (!depth_ok) |
3176 | { |
3177 | if (!ctx->quiet) |
3178 | error ("%<constexpr%> evaluation depth exceeds maximum of %d (use " |
3179 | "%<-fconstexpr-depth=%> to increase the maximum)" , |
3180 | max_constexpr_depth); |
3181 | *non_constant_p = true; |
3182 | result = error_mark_node; |
3183 | } |
3184 | else |
3185 | { |
3186 | bool cacheable = !!entry; |
3187 | if (result && result != error_mark_node) |
3188 | /* OK */; |
3189 | 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 | gcc_assert (at_eof >= 3 && ctx->quiet); |
3196 | *non_constant_p = true; |
3197 | } |
3198 | else if (tree copy = get_fundef_copy (fundef: new_call.fundef)) |
3199 | { |
3200 | tree body, parms, res; |
3201 | releasing_vec ctors; |
3202 | |
3203 | /* Reuse or create a new unshared copy of this function's body. */ |
3204 | body = TREE_PURPOSE (copy); |
3205 | parms = TREE_VALUE (copy); |
3206 | res = TREE_TYPE (copy); |
3207 | |
3208 | /* Associate the bindings with the remapped parms. */ |
3209 | tree bound = new_call.bindings; |
3210 | tree remapped = parms; |
3211 | for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i) |
3212 | { |
3213 | tree arg = TREE_VEC_ELT (bound, i); |
3214 | 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 | arg = unshare_expr_without_location (arg); |
3220 | 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 | arg = unshare_constructor (t: arg); |
3226 | if (TREE_CODE (arg) == CONSTRUCTOR) |
3227 | vec_safe_push (r&: ctors, t: arg); |
3228 | } |
3229 | ctx->global->put_value (t: remapped, v: arg); |
3230 | remapped = DECL_CHAIN (remapped); |
3231 | } |
3232 | for (; remapped; remapped = TREE_CHAIN (remapped)) |
3233 | 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 | gcc_assert (!CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun))); |
3238 | ctx->global->put_value (t: remapped, integer_zero_node); |
3239 | } |
3240 | else |
3241 | { |
3242 | gcc_assert (seen_error ()); |
3243 | *non_constant_p = true; |
3244 | } |
3245 | /* Add the RESULT_DECL to the values map, too. */ |
3246 | gcc_assert (!DECL_BY_REFERENCE (res)); |
3247 | ctx->global->put_value (t: res, NULL_TREE); |
3248 | |
3249 | /* Remember the current call we're evaluating. */ |
3250 | constexpr_ctx call_ctx = *ctx; |
3251 | call_ctx.call = &new_call; |
3252 | unsigned save_heap_alloc_count = ctx->global->heap_vars.length (); |
3253 | 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 | if (DECL_IMMEDIATE_FUNCTION_P (fun)) |
3258 | 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 | if (new_obj && DECL_DESTRUCTOR_P (fun)) |
3263 | cxx_set_object_constness (ctx, object: 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 | if (new_obj |
3269 | && DECL_CONSTRUCTOR_P (fun) |
3270 | && TREE_CODE (new_obj) == COMPONENT_REF |
3271 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (new_obj, 0))) == UNION_TYPE) |
3272 | { |
3273 | tree activate = build2 (INIT_EXPR, TREE_TYPE (new_obj), |
3274 | new_obj, |
3275 | build_constructor (TREE_TYPE (new_obj), |
3276 | NULL)); |
3277 | cxx_eval_constant_expression (ctx, activate, |
3278 | lval, non_constant_p, overflow_p); |
3279 | ggc_free (activate); |
3280 | } |
3281 | |
3282 | tree jump_target = NULL_TREE; |
3283 | cxx_eval_constant_expression (&call_ctx, body, |
3284 | vc_discard, non_constant_p, overflow_p, |
3285 | &jump_target); |
3286 | |
3287 | 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 | result = lval ? ctx->object : ctx->ctor; |
3294 | else if (VOID_TYPE_P (TREE_TYPE (res))) |
3295 | result = void_node; |
3296 | else |
3297 | { |
3298 | result = ctx->global->get_value (t: res); |
3299 | if (result == NULL_TREE && !*non_constant_p |
3300 | && !DECL_DESTRUCTOR_P (fun)) |
3301 | { |
3302 | if (!ctx->quiet) |
3303 | error ("%<constexpr%> call flows off the end " |
3304 | "of the function" ); |
3305 | *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 | if (new_obj && DECL_CONSTRUCTOR_P (fun)) |
3314 | cxx_set_object_constness (ctx, object: new_obj, /*readonly_p=*/true, |
3315 | non_constant_p, overflow_p); |
3316 | |
3317 | /* Remove the parms/result from the values map. */ |
3318 | destroy_value_checked (ctx, t: res, non_constant_p); |
3319 | for (tree parm = parms; parm; parm = TREE_CHAIN (parm)) |
3320 | destroy_value_checked (ctx, t: parm, non_constant_p); |
3321 | |
3322 | /* Free any parameter CONSTRUCTORs we aren't returning directly. */ |
3323 | while (!ctors->is_empty ()) |
3324 | { |
3325 | tree c = ctors->pop (); |
3326 | if (c != result) |
3327 | free_constructor (t: c); |
3328 | } |
3329 | |
3330 | /* Make the unshared function copy we used available for re-use. */ |
3331 | 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 | if (cacheable |
3340 | && (save_heap_alloc_count != ctx->global->heap_vars.length () |
3341 | || (save_heap_dealloc_count |
3342 | != ctx->global->heap_dealloc_count))) |
3343 | { |
3344 | tree heap_var; |
3345 | unsigned int i; |
3346 | if ((ctx->global->heap_vars.length () |
3347 | - ctx->global->heap_dealloc_count) |
3348 | != save_heap_alloc_count - save_heap_dealloc_count) |
3349 | cacheable = false; |
3350 | else |
3351 | FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var, |
3352 | save_heap_alloc_count) |
3353 | 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 | if (!*non_constant_p && ctx->object |
3363 | && CLASS_TYPE_P (TREE_TYPE (res)) |
3364 | && !is_empty_class (TREE_TYPE (res))) |
3365 | if (replace_decl (tp: &result, decl: res, replacement: ctx->object)) |
3366 | cacheable = false; |
3367 | |
3368 | /* Only cache a permitted result of a constant expression. */ |
3369 | if (cacheable && !reduced_constant_expression_p (result)) |
3370 | cacheable = false; |
3371 | } |
3372 | else |
3373 | /* Couldn't get a function copy to evaluate. */ |
3374 | *non_constant_p = true; |
3375 | |
3376 | if (result == error_mark_node) |
3377 | *non_constant_p = true; |
3378 | if (*non_constant_p || *overflow_p) |
3379 | result = error_mark_node; |
3380 | else if (!result) |
3381 | result = void_node; |
3382 | if (entry) |
3383 | 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 | if (TREE_CODE (result) == CONSTRUCTOR |
3394 | && (cxx_dialect < cxx20 |
3395 | || !DECL_CONSTRUCTOR_P (fun))) |
3396 | clear_no_implicit_zero (ctor: result); |
3397 | |
3398 | pop_cx_call_context (); |
3399 | return result; |
3400 | } |
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 | reduced_constant_expression_p (tree t) |
3409 | { |
3410 | if (t == NULL_TREE) |
3411 | return false; |
3412 | |
3413 | 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 | case CONSTRUCTOR: |
3420 | /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */ |
3421 | tree field; |
3422 | 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 | if (CONSTRUCTOR_NO_CLEARING (t)) |
3427 | { |
3428 | if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) |
3429 | { |
3430 | /* There must be a valid constant initializer at every array |
3431 | index. */ |
3432 | tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t))); |
3433 | tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t))); |
3434 | tree cursor = min; |
3435 | for (auto &e: CONSTRUCTOR_ELTS (t)) |
3436 | { |
3437 | if (!reduced_constant_expression_p (t: e.value)) |
3438 | return false; |
3439 | if (array_index_cmp (key: cursor, index: e.index) != 0) |
3440 | return false; |
3441 | if (TREE_CODE (e.index) == RANGE_EXPR) |
3442 | cursor = TREE_OPERAND (e.index, 1); |
3443 | cursor = int_const_binop (PLUS_EXPR, cursor, size_one_node); |
3444 | } |
3445 | if (find_array_ctor_elt (ary: t, dindex: max) == -1) |
3446 | return false; |
3447 | goto ok; |
3448 | } |
3449 | else if (cxx_dialect >= cxx20 |
3450 | && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE) |
3451 | { |
3452 | 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 | field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t))); |
3460 | } |
3461 | else |
3462 | field = NULL_TREE; |
3463 | for (auto &e: CONSTRUCTOR_ELTS (t)) |
3464 | { |
3465 | /* If VAL is null, we're in the middle of initializing this |
3466 | element. */ |
3467 | if (!reduced_constant_expression_p (t: e.value)) |
3468 | return false; |
3469 | /* We want to remove initializers for empty fields in a struct to |
3470 | avoid confusing output_constructor. */ |
3471 | if (is_empty_field (e.index) |
3472 | && 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 | for (; field && e.index != field; |
3477 | field = next_subobject_field (DECL_CHAIN (field))) |
3478 | if (!is_really_empty_class (TREE_TYPE (field), |
3479 | /*ignore_vptr*/false)) |
3480 | return false; |
3481 | if (field) |
3482 | field = next_subobject_field (DECL_CHAIN (field)); |
3483 | } |
3484 | /* There could be a non-empty field at the end. */ |
3485 | for (; field; field = next_subobject_field (DECL_CHAIN (field))) |
3486 | if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false)) |
3487 | return false; |
3488 | ok: |
3489 | if (CONSTRUCTOR_NO_CLEARING (t)) |
3490 | /* All the fields are initialized. */ |
3491 | CONSTRUCTOR_NO_CLEARING (t) = false; |
3492 | return true; |
3493 | |
3494 | default: |
3495 | /* FIXME are we calling this too much? */ |
3496 | 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 | verify_constant_explain_r (tree *tp, int *walk_subtrees, void *) |
3505 | { |
3506 | bool ref_p = false; |
3507 | |
3508 | /* No need to look into types or unevaluated operands. */ |
3509 | if (TYPE_P (*tp) || unevaluated_p (TREE_CODE (*tp))) |
3510 | { |
3511 | *walk_subtrees = false; |
3512 | return NULL_TREE; |
3513 | } |
3514 | |
3515 | switch (TREE_CODE (*tp)) |
3516 | { |
3517 | CASE_CONVERT: |
3518 | if (TREE_CODE (TREE_OPERAND (*tp, 0)) != ADDR_EXPR) |
3519 | break; |
3520 | ref_p = TYPE_REF_P (TREE_TYPE (*tp)); |
3521 | *tp = TREE_OPERAND (*tp, 0); |
3522 | gcc_fallthrough (); |
3523 | case ADDR_EXPR: |
3524 | { |
3525 | tree op = TREE_OPERAND (*tp, 0); |
3526 | if (VAR_P (op) |
3527 | && DECL_DECLARED_CONSTEXPR_P (op) |
3528 | && !TREE_STATIC (op) |
3529 | /* ??? We should also say something about temporaries. */ |
3530 | && !DECL_ARTIFICIAL (op)) |
3531 | { |
3532 | if (ref_p) |
3533 | inform (location_of (*tp), "reference to %qD is not a constant " |
3534 | "expression" , op); |
3535 | else |
3536 | inform (location_of (*tp), "pointer to %qD is not a constant " |
3537 | "expression" , op); |
3538 | const location_t op_loc = DECL_SOURCE_LOCATION (op); |
3539 | rich_location richloc (line_table, op_loc); |
3540 | richloc.add_fixit_insert_before (where: op_loc, new_content: "static " ); |
3541 | 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 | } |
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 | verify_constant (tree t, bool allow_non_constant, bool *non_constant_p, |
3566 | bool *overflow_p) |
3567 | { |
3568 | if (!*non_constant_p && !reduced_constant_expression_p (t) |
3569 | && t != void_node) |
3570 | { |
3571 | if (!allow_non_constant) |
3572 | { |
3573 | auto_diagnostic_group d; |
3574 | error_at (cp_expr_loc_or_input_loc (t), |
3575 | "%q+E is not a constant expression" , t); |
3576 | cp_walk_tree_without_duplicates (&t, verify_constant_explain_r, |
3577 | nullptr); |
3578 | } |
3579 | *non_constant_p = true; |
3580 | } |
3581 | if (TREE_OVERFLOW_P (t)) |
3582 | { |
3583 | if (!allow_non_constant) |
3584 | { |
3585 | permerror (input_location, "overflow in constant expression" ); |
3586 | /* If we're being permissive (and are in an enforcing |
3587 | context), ignore the overflow. */ |
3588 | if (flag_permissive) |
3589 | return *non_constant_p; |
3590 | } |
3591 | *overflow_p = true; |
3592 | } |
3593 | 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 | 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 | if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR) |
3605 | || TREE_CODE (lhs) != INTEGER_CST |
3606 | || TREE_CODE (rhs) != INTEGER_CST) |
3607 | return false; |
3608 | |
3609 | tree lhstype = TREE_TYPE (lhs); |
3610 | 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 | if (tree_int_cst_sgn (rhs) == -1) |
3616 | { |
3617 | if (!ctx->quiet) |
3618 | permerror (loc, "right operand of shift expression %q+E is negative" , |
3619 | build2_loc (loc, code, type, arg0: lhs, arg1: rhs)); |
3620 | return (!flag_permissive || ctx->quiet); |
3621 | } |
3622 | if (compare_tree_int (rhs, uprec) >= 0) |
3623 | { |
3624 | if (!ctx->quiet) |
3625 | 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, arg0: lhs, arg1: rhs), uprec); |
3628 | 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 | if (code == LSHIFT_EXPR |
3640 | && !TYPE_OVERFLOW_WRAPS (lhstype) |
3641 | && cxx_dialect >= cxx11 |
3642 | && cxx_dialect < cxx20) |
3643 | { |
3644 | if (tree_int_cst_sgn (lhs) == -1) |
3645 | { |
3646 | if (!ctx->quiet) |
3647 | permerror (loc, |
3648 | "left operand of shift expression %q+E is negative" , |
3649 | build2_loc (loc, code, type, arg0: lhs, arg1: rhs)); |
3650 | 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 | tree t = build_int_cst (unsigned_type_node, uprec - 1); |
3661 | t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs); |
3662 | tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs); |
3663 | t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t); |
3664 | if (tree_int_cst_lt (integer_one_node, t2: t)) |
3665 | { |
3666 | if (!ctx->quiet) |
3667 | permerror (loc, "shift expression %q+E overflows" , |
3668 | build2_loc (loc, code, type, arg0: lhs, arg1: rhs)); |
3669 | 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 | cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t, |
3682 | bool /*lval*/, |
3683 | bool *non_constant_p, bool *overflow_p) |
3684 | { |
3685 | tree r; |
3686 | tree orig_arg = TREE_OPERAND (t, 0); |
3687 | tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue, |
3688 | non_constant_p, overflow_p); |
3689 | VERIFY_CONSTANT (arg); |
3690 | location_t loc = EXPR_LOCATION (t); |
3691 | enum tree_code code = TREE_CODE (t); |
3692 | tree type = TREE_TYPE (t); |
3693 | r = fold_unary_loc (loc, code, type, arg); |
3694 | if (r == NULL_TREE) |
3695 | { |
3696 | if (arg == orig_arg) |
3697 | r = t; |
3698 | else |
3699 | r = build1_loc (loc, code, type, arg1: arg); |
3700 | } |
3701 | 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 | 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 | STRIP_NOPS (lhs); |
3715 | if (TREE_CODE (lhs) != ADDR_EXPR) |
3716 | return NULL_TREE; |
3717 | |
3718 | lhs = TREE_OPERAND (lhs, 0); |
3719 | |
3720 | /* &A[i] p+ j => &A[i + j] */ |
3721 | if (TREE_CODE (lhs) == ARRAY_REF |
3722 | && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST |
3723 | && TREE_CODE (rhs) == INTEGER_CST |
3724 | && TYPE_SIZE_UNIT (TREE_TYPE (lhs)) |
3725 | && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST) |
3726 | { |
3727 | tree orig_type = TREE_TYPE (t); |
3728 | location_t loc = EXPR_LOCATION (t); |
3729 | tree type = TREE_TYPE (lhs); |
3730 | |
3731 | t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1)); |
3732 | tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0))); |
3733 | nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue, |
3734 | non_constant_p, overflow_p); |
3735 | if (*non_constant_p) |
3736 | return NULL_TREE; |
3737 | /* Don't fold an out-of-bound access. */ |
3738 | if (!tree_int_cst_le (t1: t, t2: nelts)) |
3739 | return NULL_TREE; |
3740 | 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 | if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype, |
3744 | 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 | rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs, |
3749 | TYPE_SIZE_UNIT (type)); |
3750 | t = size_binop_loc (loc, PLUS_EXPR, rhs, t); |
3751 | t = build4_loc (loc, code: ARRAY_REF, type, TREE_OPERAND (lhs, 0), |
3752 | arg1: t, NULL_TREE, NULL_TREE); |
3753 | t = cp_build_addr_expr (t, tf_warning_or_error); |
3754 | t = cp_fold_convert (orig_type, t); |
3755 | return cxx_eval_constant_expression (ctx, t, vc_prvalue, |
3756 | 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 | cxx_maybe_fold_addr_pointer_plus (tree t) |
3774 | { |
3775 | while (CONVERT_EXPR_P (t) |
3776 | && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0)))) |
3777 | t = TREE_OPERAND (t, 0); |
3778 | if (TREE_CODE (t) != POINTER_PLUS_EXPR) |
3779 | return NULL_TREE; |
3780 | tree op0 = TREE_OPERAND (t, 0); |
3781 | tree op1 = TREE_OPERAND (t, 1); |
3782 | if (TREE_CODE (op1) != INTEGER_CST) |
3783 | return NULL_TREE; |
3784 | while (CONVERT_EXPR_P (op0) |
3785 | && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))) |
3786 | op0 = TREE_OPERAND (op0, 0); |
3787 | if (TREE_CODE (op0) != ADDR_EXPR) |
3788 | return NULL_TREE; |
3789 | op1 = fold_convert (ptr_type_node, op1); |
3790 | tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1); |
3791 | return build1_loc (EXPR_LOCATION (t), code: ADDR_EXPR, TREE_TYPE (op0), arg1: 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 | cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t, |
3799 | value_cat lval, |
3800 | bool *non_constant_p, bool *overflow_p) |
3801 | { |
3802 | tree r = NULL_TREE; |
3803 | tree orig_lhs = TREE_OPERAND (t, 0); |
3804 | tree orig_rhs = TREE_OPERAND (t, 1); |
3805 | tree lhs, rhs; |
3806 | 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 | if (*non_constant_p) |
3811 | return t; |
3812 | rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue, |
3813 | non_constant_p, overflow_p); |
3814 | if (*non_constant_p) |
3815 | return t; |
3816 | |
3817 | location_t loc = EXPR_LOCATION (t); |
3818 | enum tree_code code = TREE_CODE (t); |
3819 | tree type = TREE_TYPE (t); |
3820 | |
3821 | if (code == EQ_EXPR || code == NE_EXPR) |
3822 | { |
3823 | bool is_code_eq = (code == EQ_EXPR); |
3824 | |
3825 | if (TREE_CODE (lhs) == PTRMEM_CST |
3826 | && TREE_CODE (rhs) == PTRMEM_CST) |
3827 | { |
3828 | tree lmem = PTRMEM_CST_MEMBER (lhs); |
3829 | tree rmem = PTRMEM_CST_MEMBER (rhs); |
3830 | bool eq; |
3831 | if (TREE_CODE (lmem) == TREE_CODE (rmem) |
3832 | && TREE_CODE (lmem) == FIELD_DECL |
3833 | && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE |
3834 | && 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 | eq = cp_tree_equal (lhs, rhs); |
3841 | r = constant_boolean_node (eq == is_code_eq, type); |
3842 | } |
3843 | else if ((TREE_CODE (lhs) == PTRMEM_CST |
3844 | || TREE_CODE (rhs) == PTRMEM_CST) |
3845 | && (null_member_pointer_value_p (lhs) |
3846 | || null_member_pointer_value_p (rhs))) |
3847 | r = constant_boolean_node (!is_code_eq, type); |
3848 | else if (TREE_CODE (lhs) == PTRMEM_CST) |
3849 | lhs = cplus_expand_constant (lhs); |
3850 | else if (TREE_CODE (rhs) == PTRMEM_CST) |
3851 | rhs = cplus_expand_constant (rhs); |
3852 | } |
3853 | if (r == NULL_TREE |
3854 | && TREE_CODE_CLASS (code) == tcc_comparison |
3855 | && POINTER_TYPE_P (TREE_TYPE (lhs))) |
3856 | { |
3857 | if (tree lhso = cxx_maybe_fold_addr_pointer_plus (t: lhs)) |
3858 | lhs = fold_convert (TREE_TYPE (lhs), lhso); |
3859 | if (tree rhso = cxx_maybe_fold_addr_pointer_plus (t: rhs)) |
3860 | rhs = fold_convert (TREE_TYPE (rhs), rhso); |
3861 | } |
3862 | if (code == POINTER_PLUS_EXPR && !*non_constant_p |
3863 | && integer_zerop (lhs) && !integer_zerop (rhs)) |
3864 | { |
3865 | if (!ctx->quiet) |
3866 | error ("arithmetic involving a null pointer in %qE" , lhs); |
3867 | *non_constant_p = true; |
3868 | return t; |
3869 | } |
3870 | else if (code == POINTER_PLUS_EXPR) |
3871 | r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p, |
3872 | overflow_p); |
3873 | else if (code == SPACESHIP_EXPR) |
3874 | { |
3875 | r = genericize_spaceship (loc, type, lhs, rhs); |
3876 | return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, |
3877 | overflow_p); |
3878 | } |
3879 | |
3880 | if (r == NULL_TREE) |
3881 | { |
3882 | if (ctx->manifestly_const_eval == mce_true |
3883 | && (flag_constexpr_fp_except |
3884 | || TREE_CODE (type) != REAL_TYPE)) |
3885 | { |
3886 | auto ofcc = make_temp_override (var&: folding_cxx_constexpr, overrider: true); |
3887 | r = fold_binary_initializer_loc (loc, code, type, lhs, rhs); |
3888 | } |
3889 | else |
3890 | r = fold_binary_loc (loc, code, type, lhs, rhs); |
3891 | } |
3892 | |
3893 | if (r == NULL_TREE |
3894 | && (code == LSHIFT_EXPR || code == RSHIFT_EXPR) |
3895 | && TREE_CODE (lhs) == INTEGER_CST |
3896 | && TREE_CODE (rhs) == INTEGER_CST |
3897 | && wi::neg_p (x: wi::to_wide (t: rhs))) |
3898 | { |
3899 | /* For diagnostics and -fpermissive emulate previous behavior of |
3900 | handling shifts by negative amount. */ |
3901 | tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs); |
3902 | if (nrhs) |
3903 | r = fold_binary_loc (loc, |
3904 | code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR, |
3905 | type, lhs, nrhs); |
3906 | } |
3907 | |
3908 | if (r == NULL_TREE) |
3909 | { |
3910 | if (lhs == orig_lhs && rhs == orig_rhs) |
3911 | r = t; |
3912 | else |
3913 | r = build2_loc (loc, code, type, arg0: lhs, arg1: rhs); |
3914 | } |
3915 | else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs)) |
3916 | *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 | bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs)); |
3920 | if (!ptr) |
3921 | 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 | 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 | tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
3935 | vc_prvalue, |
3936 | non_constant_p, overflow_p); |
3937 | VERIFY_CONSTANT (val); |
3938 | 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 | if (ctx->manifestly_const_eval == mce_unknown) |
3947 | { |
3948 | *non_constant_p = true; |
3949 | return t; |
3950 | } |
3951 | val = constant_boolean_node (ctx->manifestly_const_eval == mce_true, |
3952 | boolean_type_node); |
3953 | } |
3954 | /* Don't VERIFY_CONSTANT the other operands. */ |
3955 | const bool zero_p = integer_zerop (val); |
3956 | if (zero_p) |
3957 | val = TREE_OPERAND (t, 2); |
3958 | else |
3959 | val = TREE_OPERAND (t, 1); |
3960 | if (TREE_CODE (t) == IF_STMT && !val) |
3961 | val = void_node; |
3962 | |
3963 | /* P2564: a subexpression of a manifestly constant-evaluated expression |
3964 | or conversion is an immediate function context. */ |
3965 | if (ctx->manifestly_const_eval != mce_true |
3966 | && !in_immediate_context () |
3967 | && cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2), |
3968 | ctx->manifestly_const_eval)) |
3969 | { |
3970 | *non_constant_p = true; |
3971 | 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 | if (TREE_CODE (val) == TARGET_EXPR) |
3980 | val = TARGET_EXPR_INITIAL (val); |
3981 | return cxx_eval_constant_expression (ctx, val, lval, non_constant_p, |
3982 | 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 | cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t, |
3994 | bool *non_constant_p, bool *overflow_p) |
3995 | { |
3996 | tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
3997 | vc_prvalue, |
3998 | non_constant_p, overflow_p); |
3999 | VERIFY_CONSTANT (arg1); |
4000 | tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
4001 | vc_prvalue, |
4002 | non_constant_p, overflow_p); |
4003 | VERIFY_CONSTANT (arg2); |
4004 | tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2), |
4005 | vc_prvalue, |
4006 | non_constant_p, overflow_p); |
4007 | VERIFY_CONSTANT (arg3); |
4008 | location_t loc = EXPR_LOCATION (t); |
4009 | tree type = TREE_TYPE (t); |
4010 | tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); |
4011 | if (r == NULL_TREE) |
4012 | { |
4013 | if (arg1 == TREE_OPERAND (t, 0) |
4014 | && arg2 == TREE_OPERAND (t, 1) |
4015 | && arg3 == TREE_OPERAND (t, 2)) |
4016 | r = t; |
4017 | else |
4018 | r = build3_loc (loc, code: VEC_COND_EXPR, type, arg0: arg1, arg1: arg2, arg2: arg3); |
4019 | } |
4020 | 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 | array_index_cmp (tree key, tree index) |
4029 | { |
4030 | gcc_assert (TREE_CODE (key) == INTEGER_CST); |
4031 | |
4032 | switch (TREE_CODE (index)) |
4033 | { |
4034 | case INTEGER_CST: |
4035 | return tree_int_cst_compare (t1: key, t2: index); |
4036 | case RANGE_EXPR: |
4037 | { |
4038 | tree lo = TREE_OPERAND (index, 0); |
4039 | tree hi = TREE_OPERAND (index, 1); |
4040 | if (tree_int_cst_lt (t1: key, t2: lo)) |
4041 | return -1; |
4042 | else if (tree_int_cst_lt (t1: hi, t2: key)) |
4043 | return 1; |
4044 | else |
4045 | return 0; |
4046 | } |
4047 | default: |
4048 | 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 | find_array_ctor_elt (tree ary, tree dindex, bool insert) |
4057 | { |
4058 | if (tree_int_cst_sgn (dindex) < 0) |
4059 | return -1; |
4060 | |
4061 | unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex); |
4062 | vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary); |
4063 | unsigned HOST_WIDE_INT len = vec_safe_length (v: elts); |
4064 | |
4065 | unsigned HOST_WIDE_INT end = len; |
4066 | 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 | if (end > 0) |
4071 | { |
4072 | tree cindex = (*elts)[end - 1].index; |
4073 | if (cindex == NULL_TREE) |
4074 | { |
4075 | /* Verify that if the last index is missing, all indexes |
4076 | are missing. */ |
4077 | if (flag_checking) |
4078 | for (unsigned int j = 0; j < len - 1; ++j) |
4079 | gcc_assert ((*elts)[j].index == NULL_TREE); |
4080 | if (i < end) |
4081 | return i; |
4082 | else |
4083 | { |
4084 | begin = end; |
4085 | 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 | dindex = NULL_TREE; |
4089 | 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 | for (unsigned int j = 0; j < len; ++j) |
4094 | (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j); |
4095 | } |
4096 | } |
4097 | else if (TREE_CODE (cindex) == INTEGER_CST |
4098 | && compare_tree_int (cindex, end - 1) == 0) |
4099 | { |
4100 | if (i < end) |
4101 | return i; |
4102 | else |
4103 | begin = end; |
4104 | } |
4105 | } |
4106 | |
4107 | /* Otherwise, find a matching index by means of a binary search. */ |
4108 | while (begin != end) |
4109 | { |
4110 | unsigned HOST_WIDE_INT middle = (begin + end) / 2; |
4111 | constructor_elt &elt = (*elts)[middle]; |
4112 | tree idx = elt.index; |
4113 | |
4114 | int cmp = array_index_cmp (key: dindex, index: idx); |
4115 | if (cmp < 0) |
4116 | end = middle; |
4117 | else if (cmp > 0) |
4118 | begin = middle + 1; |
4119 | else |
4120 | { |
4121 | if (insert && TREE_CODE (idx) == RANGE_EXPR) |
4122 | { |
4123 | /* We need to split the range. */ |
4124 | constructor_elt e; |
4125 | tree lo = TREE_OPERAND (idx, 0); |
4126 | tree hi = TREE_OPERAND (idx, 1); |
4127 | tree value = elt.value; |
4128 | dindex = fold_convert (sizetype, dindex); |
4129 | if (tree_int_cst_lt (t1: lo, t2: dindex)) |
4130 | { |
4131 | /* There are still some lower elts; shorten the range. */ |
4132 | tree new_hi = int_const_binop (MINUS_EXPR, dindex, |
4133 | size_one_node); |
4134 | if (tree_int_cst_equal (lo, new_hi)) |
4135 | /* Only one element left, no longer a range. */ |
4136 | elt.index = lo; |
4137 | else |
4138 | TREE_OPERAND (idx, 1) = new_hi; |
4139 | /* Append the element we want to insert. */ |
4140 | ++middle; |
4141 | e.index = dindex; |
4142 | e.value = unshare_constructor (t: value); |
4143 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), ix: middle, obj: e); |
4144 | } |
4145 | else |
4146 | /* No lower elts, the range elt is now ours. */ |
4147 | elt.index = dindex; |
4148 | |
4149 | if (tree_int_cst_lt (t1: dindex, t2: hi)) |
4150 | { |
4151 | /* There are still some higher elts; append a range. */ |
4152 | tree new_lo = int_const_binop (PLUS_EXPR, dindex, |
4153 | size_one_node); |
4154 | if (tree_int_cst_equal (new_lo, hi)) |
4155 | e.index = hi; |
4156 | else |
4157 | e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi); |
4158 | e.value = unshare_constructor (t: value); |
4159 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), ix: middle + 1, obj: e); |
4160 | } |
4161 | } |
4162 | return middle; |
4163 | } |
4164 | } |
4165 | |
4166 | if (insert) |
4167 | { |
4168 | constructor_elt e = { .index: dindex, NULL_TREE }; |
4169 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), ix: end, obj: e); |
4170 | 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 | get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1) |
4184 | { |
4185 | /* Check the hint first. */ |
4186 | if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor) |
4187 | && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index) |
4188 | return CONSTRUCTOR_ELT (ctor, pos_hint); |
4189 | |
4190 | tree type = TREE_TYPE (ctor); |
4191 | if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE) |
4192 | { |
4193 | CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE); |
4194 | return &CONSTRUCTOR_ELTS (ctor)->last(); |
4195 | } |
4196 | else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE) |
4197 | { |
4198 | 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 | vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor); |
4205 | if (vec_safe_length (v: elts)) |
4206 | { |
4207 | tree lo = TREE_OPERAND (index, 0); |
4208 | gcc_assert (array_index_cmp (elts->last().index, lo) < 0); |
4209 | } |
4210 | CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE); |
4211 | return &elts->last(); |
4212 | } |
4213 | |
4214 | HOST_WIDE_INT i = find_array_ctor_elt (ary: ctor, dindex: index, /*insert*/true); |
4215 | gcc_assert (i >= 0); |
4216 | constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i); |
4217 | gcc_assert (cep->index == NULL_TREE |
4218 | || TREE_CODE (cep->index) != RANGE_EXPR); |
4219 | return cep; |
4220 | } |
4221 | else |
4222 | { |
4223 | 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 | tree fields = TYPE_FIELDS (DECL_CONTEXT (index)); |
4232 | unsigned HOST_WIDE_INT idx = 0; |
4233 | constructor_elt *cep = NULL; |
4234 | |
4235 | /* Check if we're changing the active member of a union. */ |
4236 | if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor) |
4237 | && CONSTRUCTOR_ELT (ctor, 0)->index != index) |
4238 | vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), size: 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 | else if (CONSTRUCTOR_NELTS (ctor) |
4243 | && tree_int_cst_compare (t1: bit_position (index), |
4244 | t2: bit_position (CONSTRUCTOR_ELTS (ctor) |
4245 | ->last().index)) > 0) |
4246 | { |
4247 | idx = CONSTRUCTOR_NELTS (ctor); |
4248 | goto insert; |
4249 | } |
4250 | |
4251 | /* Otherwise, we need to iterate over CTOR to find or insert INDEX |
4252 | appropriately. */ |
4253 | |
4254 | for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), ix: idx, ptr: &cep); |
4255 | idx++, fields = DECL_CHAIN (fields)) |
4256 | { |
4257 | if (index == cep->index) |
4258 | 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 | for (; fields != cep->index; fields = DECL_CHAIN (fields)) |
4264 | if (index == fields) |
4265 | goto insert; |
4266 | } |
4267 | /* We fell off the end of the CONSTRUCTOR, so insert a new |
4268 | entry at the end. */ |
4269 | |
4270 | insert: |
4271 | { |
4272 | constructor_elt ce = { .index: index, NULL_TREE }; |
4273 | |
4274 | vec_safe_insert (CONSTRUCTOR_ELTS (ctor), ix: idx, obj: ce); |
4275 | cep = CONSTRUCTOR_ELT (ctor, idx); |
4276 | } |
4277 | found:; |
4278 | |
4279 | 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 | diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index) |
4288 | { |
4289 | if (!ctx->quiet) |
4290 | { |
4291 | 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 | tree sidx = fold_convert (ssizetype, index); |
4296 | STRIP_ANY_LOCATION_WRAPPER (array); |
4297 | if (DECL_P (array)) |
4298 | { |
4299 | auto_diagnostic_group d; |
4300 | if (TYPE_DOMAIN (arraytype)) |
4301 | error_at (loc, "array subscript value %qE is outside the bounds " |
4302 | "of array %qD of type %qT" , sidx, array, arraytype); |
4303 | else |
4304 | error_at (loc, "nonzero array subscript %qE is used with array %qD of " |
4305 | "type %qT with unknown bounds" , sidx, array, arraytype); |
4306 | inform (DECL_SOURCE_LOCATION (array), "declared here" ); |
4307 | } |
4308 | else if (TYPE_DOMAIN (arraytype)) |
4309 | error_at (loc, "array subscript value %qE is outside the bounds " |
4310 | "of array type %qT" , sidx, arraytype); |
4311 | else |
4312 | error_at (loc, "nonzero array subscript %qE is used with array of type %qT " |
4313 | "with unknown bounds" , sidx, arraytype); |
4314 | } |
4315 | } |
4316 | |
4317 | /* Return the number of elements for TYPE (which is an ARRAY_TYPE or |
4318 | a VECTOR_TYPE). */ |
4319 | |
4320 | static tree |
4321 | get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type, |
4322 | bool *non_constant_p, bool *overflow_p) |
4323 | { |
4324 | tree nelts; |
4325 | if (TREE_CODE (type) == ARRAY_TYPE) |
4326 | { |
4327 | if (TYPE_DOMAIN (type)) |
4328 | nelts = array_type_nelts_top (type); |
4329 | else |
4330 | nelts = size_zero_node; |
4331 | } |
4332 | else if (VECTOR_TYPE_P (type)) |
4333 | nelts = size_int (TYPE_VECTOR_SUBPARTS (type)); |
4334 | else |
4335 | gcc_unreachable (); |
4336 | |
4337 | /* For VLAs, the number of elements won't be an integer constant. */ |
4338 | nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue, |
4339 | non_constant_p, overflow_p); |
4340 | return nelts; |
4341 | } |
4342 | |
4343 | /* Extract element INDEX consisting of CHARS_PER_ELT chars from |
4344 | STRING_CST STRING. */ |
4345 | |
4346 | static tree |
4347 | (tree string, unsigned chars_per_elt, unsigned index) |
4348 | { |
4349 | tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string))); |
4350 | tree r; |
4351 | |
4352 | if (chars_per_elt == 1) |
4353 | r = build_int_cst (type, TREE_STRING_POINTER (string)[index]); |
4354 | else |
4355 | { |
4356 | const unsigned char *ptr |
4357 | = ((const unsigned char *)TREE_STRING_POINTER (string) |
4358 | + index * chars_per_elt); |
4359 | r = native_interpret_expr (type, ptr, chars_per_elt); |
4360 | } |
4361 | 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 | 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 | location_t loc = cp_expr_loc_or_input_loc (t); |
4373 | tree ary = TREE_OPERAND (t, 0); |
4374 | t = TREE_OPERAND (t, 1); |
4375 | tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue, |
4376 | non_constant_p, overflow_p); |
4377 | VERIFY_CONSTANT (index); |
4378 | |
4379 | if (!tree_fits_shwi_p (index) |
4380 | || tree_int_cst_sgn (index) < 0) |
4381 | { |
4382 | diag_array_subscript (loc, ctx, array: ary, index); |
4383 | *non_constant_p = true; |
4384 | return t; |
4385 | } |
4386 | |
4387 | tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p, |
4388 | overflow_p); |
4389 | VERIFY_CONSTANT (nelts); |
4390 | if (allow_one_past |
4391 | ? !tree_int_cst_le (t1: index, t2: nelts) |
4392 | : !tree_int_cst_lt (t1: index, t2: nelts)) |
4393 | { |
4394 | diag_array_subscript (loc, ctx, array: ary, index); |
4395 | *non_constant_p = true; |
4396 | 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 | cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, |
4407 | value_cat lval, |
4408 | bool *non_constant_p, bool *overflow_p) |
4409 | { |
4410 | tree oldary = TREE_OPERAND (t, 0); |
4411 | tree ary = cxx_eval_constant_expression (ctx, oldary, |
4412 | lval, |
4413 | non_constant_p, overflow_p); |
4414 | if (*non_constant_p) |
4415 | return t; |
4416 | if (!lval |
4417 | && TREE_CODE (ary) == VIEW_CONVERT_EXPR |
4418 | && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0))) |
4419 | && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0)))) |
4420 | ary = TREE_OPERAND (ary, 0); |
4421 | |
4422 | tree oldidx = TREE_OPERAND (t, 1); |
4423 | tree index = eval_and_check_array_index (ctx, t, allow_one_past: lval, |
4424 | non_constant_p, overflow_p); |
4425 | if (*non_constant_p) |
4426 | return t; |
4427 | |
4428 | if (lval && ary == oldary && index == oldidx) |
4429 | return t; |
4430 | else if (lval == vc_discard) |
4431 | return t; |
4432 | else if (lval) |
4433 | return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL); |
4434 | |
4435 | unsigned len = 0, elem_nchars = 1; |
4436 | tree elem_type = TREE_TYPE (TREE_TYPE (ary)); |
4437 | if (TREE_CODE (ary) == CONSTRUCTOR) |
4438 | len = CONSTRUCTOR_NELTS (ary); |
4439 | else if (TREE_CODE (ary) == STRING_CST) |
4440 | { |
4441 | elem_nchars = (TYPE_PRECISION (elem_type) |
4442 | / TYPE_PRECISION (char_type_node)); |
4443 | len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars; |
4444 | } |
4445 | else if (TREE_CODE (ary) == VECTOR_CST) |
4446 | /* We don't create variable-length VECTOR_CSTs. */ |
4447 | 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 | VERIFY_CONSTANT (ary); |
4453 | gcc_unreachable (); |
4454 | } |
4455 | |
4456 | bool found; |
4457 | HOST_WIDE_INT i = 0; |
4458 | if (TREE_CODE (ary) == CONSTRUCTOR) |
4459 | { |
4460 | HOST_WIDE_INT ix = find_array_ctor_elt (ary, dindex: index); |
4461 | found = (ix >= 0); |
4462 | if (found) |
4463 | i = ix; |
4464 | } |
4465 | else |
4466 | { |
4467 | i = tree_to_shwi (index); |
4468 | found = (i < len); |
4469 | } |
4470 | |
4471 | if (found) |
4472 | { |
4473 | tree r; |
4474 | if (TREE_CODE (ary) == CONSTRUCTOR) |
4475 | r = (*CONSTRUCTOR_ELTS (ary))[i].value; |
4476 | else if (TREE_CODE (ary) == VECTOR_CST) |
4477 | r = VECTOR_CST_ELT (ary, i); |
4478 | else |
4479 | r = extract_string_elt (string: ary, chars_per_elt: elem_nchars, index: i); |
4480 | |
4481 | 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 | if (is_really_empty_class (elem_type, /*ignore_vptr*/false)) |
4491 | return build_constructor (elem_type, NULL); |
4492 | |
4493 | if (TREE_CODE (ary) == CONSTRUCTOR |
4494 | && 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 | if (!ctx->quiet) |
4500 | error ("accessing uninitialized array element" ); |
4501 | *non_constant_p = true; |
4502 | 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 | tree val; |
4509 | constexpr_ctx new_ctx; |
4510 | if (CP_AGGREGATE_TYPE_P (elem_type)) |
4511 | { |
4512 | tree empty_ctor = build_constructor (init_list_type_node, NULL); |
4513 | val = digest_init (elem_type, empty_ctor, tf_warning_or_error); |
4514 | } |
4515 | else |
4516 | 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 | const bool new_ctor = (!SCALAR_TYPE_P (elem_type) |
4520 | && (!ctx->ctor |
4521 | || !same_type_ignoring_top_level_qualifiers_p |
4522 | (elem_type, TREE_TYPE (ctx->ctor)))); |
4523 | if (new_ctor) |
4524 | { |
4525 | 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 | new_ctx.object = NULL_TREE; |
4530 | new_ctx.ctor = build_constructor (elem_type, NULL); |
4531 | ctx = &new_ctx; |
4532 | } |
4533 | t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p, |
4534 | overflow_p); |
4535 | if (new_ctor && t != ctx->ctor) |
4536 | free_constructor (t: 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 | cxx_eval_component_reference (const constexpr_ctx *ctx, tree t, |
4545 | value_cat lval, |
4546 | bool *non_constant_p, bool *overflow_p) |
4547 | { |
4548 | unsigned HOST_WIDE_INT i; |
4549 | tree field; |
4550 | tree value; |
4551 | tree part = TREE_OPERAND (t, 1); |
4552 | tree orig_whole = TREE_OPERAND (t, 0); |
4553 | tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
4554 | lval, |
4555 | non_constant_p, overflow_p); |
4556 | if (*non_constant_p) |
4557 | return t; |
4558 | if (INDIRECT_REF_P (whole) |
4559 | && integer_zerop (TREE_OPERAND (whole, 0))) |
4560 | { |
4561 | if (!ctx->quiet) |
4562 | error ("dereferencing a null pointer in %qE" , orig_whole); |
4563 | *non_constant_p = true; |
4564 | return t; |
4565 | } |
4566 | |
4567 | if (TREE_CODE (whole) == PTRMEM_CST) |
4568 | whole = cplus_expand_constant (whole); |
4569 | if (whole == orig_whole) |
4570 | return t; |
4571 | if (lval == vc_discard) |
4572 | return t; |
4573 | if (lval) |
4574 | 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 | if (TREE_CODE (whole) != CONSTRUCTOR) |
4579 | { |
4580 | if (!ctx->quiet) |
4581 | error ("%qE is not a constant expression" , orig_whole); |
4582 | *non_constant_p = true; |
4583 | return t; |
4584 | } |
4585 | if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole)) |
4586 | && DECL_MUTABLE_P (part)) |
4587 | { |
4588 | if (!ctx->quiet) |
4589 | error ("mutable %qD is not usable in a constant expression" , part); |
4590 | *non_constant_p = true; |
4591 | return t; |
4592 | } |
4593 | |
4594 | bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole)); |
4595 | 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 | if (pmf ? DECL_NAME (field) == DECL_NAME (part) |
4600 | : field == part) |
4601 | { |
4602 | if (value) |
4603 | { |
4604 | STRIP_ANY_LOCATION_WRAPPER (value); |
4605 | return value; |
4606 | } |
4607 | else |
4608 | /* We're in the middle of initializing it. */ |
4609 | break; |
4610 | } |
4611 | } |
4612 | if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE) |
4613 | { |
4614 | if (CONSTRUCTOR_NELTS (whole) > 0) |
4615 | { |
4616 | /* DR 1188 says we don't have to deal with this. */ |
4617 | if (!ctx->quiet) |
4618 | { |
4619 | constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0); |
4620 | if (cep->value == NULL_TREE) |
4621 | error ("accessing uninitialized member %qD" , part); |
4622 | else |
4623 | error ("accessing %qD member instead of initialized %qD member " |
4624 | "in constant expression" , part, cep->index); |
4625 | } |
4626 | *non_constant_p = true; |
4627 | return t; |
4628 | } |
4629 | else if (!CONSTRUCTOR_NO_CLEARING (whole)) |
4630 | { |
4631 | /* Value-initialized union, check if looking at the first member. */ |
4632 | tree first = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (whole))); |
4633 | if (first != part) |
4634 | { |
4635 | if (!ctx->quiet) |
4636 | error ("accessing %qD member instead of initialized %qD " |
4637 | "member in constant expression" , part, first); |
4638 | *non_constant_p = true; |
4639 | 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 | if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
4647 | return build_constructor (TREE_TYPE (t), NULL); |
4648 | |
4649 | gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole))); |
4650 | |
4651 | 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 | if (!ctx->quiet) |
4657 | error ("accessing uninitialized member %qD" , part); |
4658 | *non_constant_p = true; |
4659 | return t; |
4660 | } |
4661 | |
4662 | /* If there's no explicit init for this field, it's value-initialized. */ |
4663 | value = build_value_init (TREE_TYPE (t), tf_warning_or_error); |
4664 | return cxx_eval_constant_expression (ctx, value, |
4665 | lval, |
4666 | 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 | 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 | tree orig_whole = TREE_OPERAND (t, 0); |
4679 | tree retval, fldval, utype, mask; |
4680 | bool fld_seen = false; |
4681 | HOST_WIDE_INT istart, isize; |
4682 | tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
4683 | lval, |
4684 | non_constant_p, overflow_p); |
4685 | tree start, field, value; |
4686 | unsigned HOST_WIDE_INT i; |
4687 | |
4688 | 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 | if (!*non_constant_p |
4693 | && TREE_CODE (whole) != VECTOR_CST |
4694 | && TREE_CODE (whole) != CONSTRUCTOR) |
4695 | { |
4696 | if (!ctx->quiet) |
4697 | error ("%qE is not a constant expression" , orig_whole); |
4698 | *non_constant_p = true; |
4699 | } |
4700 | if (*non_constant_p) |
4701 | return t; |
4702 | |
4703 | if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t))) |
4704 | { |
4705 | 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 | if (!ctx->quiet) |
4709 | error ("%qE is not a constant expression" , orig_whole); |
4710 | *non_constant_p = true; |
4711 | return t; |
4712 | } |
4713 | |
4714 | start = TREE_OPERAND (t, 2); |
4715 | istart = tree_to_shwi (start); |
4716 | isize = tree_to_shwi (TREE_OPERAND (t, 1)); |
4717 | utype = TREE_TYPE (t); |
4718 | if (!TYPE_UNSIGNED (utype)) |
4719 | utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1); |
4720 | retval = build_int_cst (utype, 0); |
4721 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) |
4722 | { |
4723 | tree bitpos = bit_position (field); |
4724 | STRIP_ANY_LOCATION_WRAPPER (value); |
4725 | if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1)) |
4726 | return value; |
4727 | if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE |
4728 | && TREE_CODE (value) == INTEGER_CST |
4729 | && tree_fits_shwi_p (bitpos) |
4730 | && tree_fits_shwi_p (DECL_SIZE (field))) |
4731 | { |
4732 | HOST_WIDE_INT bit = tree_to_shwi (bitpos); |
4733 | HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field)); |
4734 | HOST_WIDE_INT shift; |
4735 | if (bit >= istart && bit + sz <= istart + isize) |
4736 | { |
4737 | fldval = fold_convert (utype, value); |
4738 | mask = build_int_cst_type (utype, -1); |
4739 | mask = fold_build2 (LSHIFT_EXPR, utype, mask, |
4740 | size_int (TYPE_PRECISION (utype) - sz)); |
4741 | mask = fold_build2 (RSHIFT_EXPR, utype, mask, |
4742 | size_int (TYPE_PRECISION (utype) - sz)); |
4743 | fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask); |
4744 | shift = bit - istart; |
4745 | if (BYTES_BIG_ENDIAN) |
4746 | shift = TYPE_PRECISION (utype) - shift - sz; |
4747 | fldval = fold_build2 (LSHIFT_EXPR, utype, fldval, |
4748 | size_int (shift)); |
4749 | retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval); |
4750 | fld_seen = true; |
4751 | } |
4752 | } |
4753 | } |
4754 | if (fld_seen) |
4755 | return fold_convert (TREE_TYPE (t), retval); |
4756 | 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 | check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type, |
4768 | tree orig_type) |
4769 | { |
4770 | if (TREE_CODE (type) == UNION_TYPE) |
4771 | { |
4772 | if (!ctx->quiet) |
4773 | { |
4774 | if (type == orig_type) |
4775 | error_at (loc, "%qs is not a constant expression because %qT is " |
4776 | "a union type" , "__builtin_bit_cast" , type); |
4777 | else |
4778 | error_at (loc, "%qs is not a constant expression because %qT " |
4779 | "contains a union type" , "__builtin_bit_cast" , |
4780 | orig_type); |
4781 | } |
4782 | return true; |
4783 | } |
4784 | if (TREE_CODE (type) == POINTER_TYPE) |
4785 | { |
4786 | if (!ctx->quiet) |
4787 | { |
4788 | if (type == orig_type) |
4789 | error_at (loc, "%qs is not a constant expression because %qT is " |
4790 | "a pointer type" , "__builtin_bit_cast" , type); |
4791 | else |
4792 | error_at (loc, "%qs is not a constant expression because %qT " |
4793 | "contains a pointer type" , "__builtin_bit_cast" , |
4794 | orig_type); |
4795 | } |
4796 | return true; |
4797 | } |
4798 | if (TREE_CODE (type) == REFERENCE_TYPE) |
4799 | { |
4800 | if (!ctx->quiet) |
4801 | { |
4802 | if (type == orig_type) |
4803 | error_at (loc, "%qs is not a constant expression because %qT is " |
4804 | "a reference type" , "__builtin_bit_cast" , type); |
4805 | else |
4806 | error_at (loc, "%qs is not a constant expression because %qT " |
4807 | "contains a reference type" , "__builtin_bit_cast" , |
4808 | orig_type); |
4809 | } |
4810 | return true; |
4811 | } |
4812 | if (TYPE_PTRMEM_P (type)) |
4813 | { |
4814 | if (!ctx->quiet) |
4815 | { |
4816 | if (type == orig_type) |
4817 | 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 | 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 | return true; |
4826 | } |
4827 | if (TYPE_VOLATILE (type)) |
4828 | { |
4829 | if (!ctx->quiet) |
4830 | { |
4831 | if (type == orig_type) |
4832 | error_at (loc, "%qs is not a constant expression because %qT is " |
4833 | "volatile" , "__builtin_bit_cast" , type); |
4834 | else |
4835 | error_at (loc, "%qs is not a constant expression because %qT " |
4836 | "contains a volatile subobject" , |
4837 | "__builtin_bit_cast" , orig_type); |
4838 | } |
4839 | return true; |
4840 | } |
4841 | if (TREE_CODE (type) == RECORD_TYPE) |
4842 | for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
4843 | if (TREE_CODE (field) == FIELD_DECL |
4844 | && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type)) |
4845 | return true; |
4846 | if (TREE_CODE (type) == ARRAY_TYPE) |
4847 | 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 | clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask) |
4859 | { |
4860 | if (TREE_CODE (t) != CONSTRUCTOR) |
4861 | return; |
4862 | |
4863 | unsigned i, j = 0; |
4864 | tree index, value; |
4865 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value) |
4866 | { |
4867 | tree type = TREE_TYPE (value); |
4868 | if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE |
4869 | && DECL_BIT_FIELD_TYPE (index) != NULL_TREE) |
4870 | { |
4871 | if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index))) |
4872 | { |
4873 | HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index)); |
4874 | gcc_assert (fldsz != 0); |
4875 | HOST_WIDE_INT pos = int_byte_position (index); |
4876 | HOST_WIDE_INT bpos |
4877 | = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index)); |
4878 | bpos %= BITS_PER_UNIT; |
4879 | HOST_WIDE_INT end |
4880 | = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT; |
4881 | gcc_assert (end == 1 || end == 2); |
4882 | unsigned char *p = mask + pos; |
4883 | unsigned char mask_save[2]; |
4884 | mask_save[0] = mask[pos]; |
4885 | mask_save[1] = end == 2 ? mask[pos + 1] : 0; |
4886 | if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) |
4887 | sorry_at (loc, "PDP11 bit-field handling unsupported" |
4888 | " in %qs" , "__builtin_bit_cast" ); |
4889 | 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 | if (bpos + fldsz <= BITS_PER_UNIT) |
4909 | *p &= ~(((1 << fldsz) - 1) << bpos); |
4910 | else |
4911 | { |
4912 | gcc_assert (bpos); |
4913 | *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos); |
4914 | p++; |
4915 | fldsz -= BITS_PER_UNIT - bpos; |
4916 | gcc_assert (fldsz && fldsz < BITS_PER_UNIT); |
4917 | *p &= ~((1 << fldsz) - 1); |
4918 | } |
4919 | } |
4920 | if (mask_save[0] != mask[pos] |
4921 | || (end == 2 && mask_save[1] != mask[pos + 1])) |
4922 | { |
4923 | CONSTRUCTOR_NO_CLEARING (t) = 1; |
4924 | continue; |
4925 | } |
4926 | } |
4927 | } |
4928 | else if (is_byte_access_type_not_plain_char (type)) |
4929 | { |
4930 | HOST_WIDE_INT pos; |
4931 | if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) |
4932 | pos = tree_to_shwi (index); |
4933 | else |
4934 | pos = int_byte_position (index); |
4935 | if (mask[pos]) |
4936 | { |
4937 | CONSTRUCTOR_NO_CLEARING (t) = 1; |
4938 | mask[pos] = 0; |
4939 | continue; |
4940 | } |
4941 | } |
4942 | if (TREE_CODE (value) == CONSTRUCTOR) |
4943 | { |
4944 | HOST_WIDE_INT pos; |
4945 | if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) |
4946 | pos = tree_to_shwi (index) |
4947 | * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)))); |
4948 | else |
4949 | pos = int_byte_position (index); |
4950 | clear_uchar_or_std_byte_in_mask (loc, t: value, mask: mask + pos); |
4951 | } |
4952 | if (i != j) |
4953 | { |
4954 | CONSTRUCTOR_ELT (t, j)->index = index; |
4955 | CONSTRUCTOR_ELT (t, j)->value = value; |
4956 | } |
4957 | ++j; |
4958 | } |
4959 | if (CONSTRUCTOR_NELTS (t) != j) |
4960 | vec_safe_truncate (CONSTRUCTOR_ELTS (t), size: j); |
4961 | } |
4962 | |
4963 | /* Subroutine of cxx_eval_constant_expression. |
4964 | Attempt to evaluate a BIT_CAST_EXPR. */ |
4965 | |
4966 | static tree |
4967 | cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p, |
4968 | bool *overflow_p) |
4969 | { |
4970 | if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t), |
4971 | TREE_TYPE (t)) |
4972 | || check_bit_cast_type (ctx, loc: cp_expr_loc_or_loc (TREE_OPERAND (t, 0), |
4973 | EXPR_LOCATION (t)), |
4974 | TREE_TYPE (TREE_OPERAND (t, 0)), |
4975 | TREE_TYPE (TREE_OPERAND (t, 0)))) |
4976 | { |
4977 | *non_constant_p = true; |
4978 | return t; |
4979 | } |
4980 | |
4981 | tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue, |
4982 | non_constant_p, overflow_p); |
4983 | if (*non_constant_p) |
4984 | return t; |
4985 | |
4986 | location_t loc = EXPR_LOCATION (t); |
4987 | 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 | if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t)))) |
4997 | { |
4998 | if (!ctx->quiet) |
4999 | sorry_at (loc, "%qs cannot be constant evaluated because the " |
5000 | "type is too large" , "__builtin_bit_cast" ); |
5001 | *non_constant_p = true; |
5002 | return t; |
5003 | } |
5004 | |
5005 | HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t))); |
5006 | if (len < 0 || (int) len != len) |
5007 | { |
5008 | if (!ctx->quiet) |
5009 | sorry_at (loc, "%qs cannot be constant evaluated because the " |
5010 | "type is too large" , "__builtin_bit_cast" ); |
5011 | *non_constant_p = true; |
5012 | return t; |
5013 | } |
5014 | |
5015 | unsigned char buf[64]; |
5016 | unsigned char *ptr, *mask; |
5017 | size_t alen = (size_t) len * 2; |
5018 | if (alen <= sizeof (buf)) |
5019 | ptr = buf; |
5020 | else |
5021 | ptr = XNEWVEC (unsigned char, alen); |
5022 | mask = ptr + (size_t) len; |
5023 | /* At the beginning consider everything indeterminate. */ |
5024 | memset (s: mask, c: ~0, n: (size_t) len); |
5025 | |
5026 | if (native_encode_initializer (op, ptr, len, off: 0, mask) != len) |
5027 | { |
5028 | if (!ctx->quiet) |
5029 | sorry_at (loc, "%qs cannot be constant evaluated because the " |
5030 | "argument cannot be encoded" , "__builtin_bit_cast" ); |
5031 | *non_constant_p = true; |
5032 | if (ptr != buf) |
5033 | XDELETE (ptr); |
5034 | return t; |
5035 | } |
5036 | |
5037 | tree r = NULL_TREE; |
5038 | if (can_native_interpret_type_p (TREE_TYPE (t))) |
5039 | { |
5040 | r = native_interpret_expr (TREE_TYPE (t), ptr, len); |
5041 | if (is_byte_access_type_not_plain_char (TREE_TYPE (t))) |
5042 | { |
5043 | gcc_assert (len == 1); |
5044 | if (mask[0]) |
5045 | { |
5046 | memset (s: mask, c: 0, n: len); |
5047 | r = build_constructor (TREE_TYPE (r), NULL); |
5048 | CONSTRUCTOR_NO_CLEARING (r) = 1; |
5049 | } |
5050 | } |
5051 | } |
5052 | else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE) |
5053 | { |
5054 | r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len); |
5055 | if (r != NULL_TREE) |
5056 | { |
5057 | clear_type_padding_in_mask (TREE_TYPE (t), mask); |
5058 | clear_uchar_or_std_byte_in_mask (loc, t: r, mask); |
5059 | if (CHECKING_P) |
5060 | { |
5061 | tree e = cxx_eval_bare_aggregate (ctx, r, vc_prvalue, |
5062 | non_constant_p, overflow_p); |
5063 | gcc_checking_assert (e == r); |
5064 | r = e; |
5065 | } |
5066 | } |
5067 | } |
5068 | |
5069 | if (r != NULL_TREE) |
5070 | { |
5071 | for (int i = 0; i < len; i++) |
5072 | if (mask[i]) |
5073 | { |
5074 | if (!ctx->quiet) |
5075 | error_at (loc, "%qs accessing uninitialized byte at offset %d" , |
5076 | "__builtin_bit_cast" , i); |
5077 | *non_constant_p = true; |
5078 | r = t; |
5079 | break; |
5080 | } |
5081 | if (ptr != buf) |
5082 | XDELETE (ptr); |
5083 | return r; |
5084 | } |
5085 | |
5086 | if (!ctx->quiet) |
5087 | sorry_at (loc, "%qs cannot be constant evaluated because the " |
5088 | "argument cannot be interpreted" , "__builtin_bit_cast" ); |
5089 | *non_constant_p = true; |
5090 | if (ptr != buf) |
5091 | 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 | 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 | tree r; |
5107 | tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
5108 | vc_prvalue, non_constant_p, |
5109 | overflow_p); |
5110 | VERIFY_CONSTANT (lhs); |
5111 | if (tree_int_cst_equal (lhs, bailout_value)) |
5112 | return lhs; |
5113 | gcc_assert (tree_int_cst_equal (lhs, continue_value)); |
5114 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
5115 | vc_prvalue, non_constant_p, |
5116 | overflow_p); |
5117 | 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 | base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref) |
5128 | { |
5129 | tree aggr = TREE_OPERAND (ref, 0); |
5130 | tree field = TREE_OPERAND (ref, 1); |
5131 | HOST_WIDE_INT i; |
5132 | constructor_elt *ce; |
5133 | |
5134 | gcc_assert (TREE_CODE (ref) == COMPONENT_REF); |
5135 | |
5136 | if (TREE_CODE (aggr) == COMPONENT_REF) |
5137 | { |
5138 | constructor_elt *base_ce |
5139 | = base_field_constructor_elt (v, ref: aggr); |
5140 | v = CONSTRUCTOR_ELTS (base_ce->value); |
5141 | } |
5142 | |
5143 | for (i = 0; vec_safe_iterate (v, ix: i, ptr: &ce); ++i) |
5144 | if (ce->index == field) |
5145 | return ce; |
5146 | |
5147 | 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 | initialized_type (tree t) |
5157 | { |
5158 | if (TYPE_P (t)) |
5159 | return t; |
5160 | tree type = TREE_TYPE (t); |
5161 | if (TREE_CODE (t) == CALL_EXPR) |
5162 | { |
5163 | /* A constructor call has void type, so we need to look deeper. */ |
5164 | tree fn = get_function_named_in_call (t); |
5165 | if (fn && TREE_CODE (fn) == FUNCTION_DECL |
5166 | && DECL_CXX_CONSTRUCTOR_P (fn)) |
5167 | type = DECL_CONTEXT (fn); |
5168 | } |
5169 | else if (TREE_CODE (t) == COMPOUND_EXPR) |
5170 | return initialized_type (TREE_OPERAND (t, 1)); |
5171 | else if (TREE_CODE (t) == AGGR_INIT_EXPR) |
5172 | type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t)); |
5173 | 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 | init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx, |
5183 | tree index, tree &value) |
5184 | { |
5185 | new_ctx = *ctx; |
5186 | |
5187 | if (index && TREE_CODE (index) != INTEGER_CST |
5188 | && TREE_CODE (index) != FIELD_DECL |
5189 | && TREE_CODE (index) != RANGE_EXPR) |
5190 | /* This won't have an element in the new CONSTRUCTOR. */ |
5191 | return; |
5192 | |
5193 | tree type = initialized_type (t: value); |
5194 | if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type)) |
5195 | /* A non-aggregate member doesn't get its own CONSTRUCTOR. */ |
5196 | return; |
5197 | if (VECTOR_TYPE_P (type) |
5198 | && VECTOR_TYPE_P (TREE_TYPE (ctx->ctor)) |
5199 | && 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 | if (ctx->object) |
5209 | { |
5210 | if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR) |
5211 | /* There's no well-defined subobject for this index. */ |
5212 | new_ctx.object = NULL_TREE; |
5213 | else |
5214 | new_ctx.object = build_ctor_subob_ref (index, type, ctx->object); |
5215 | } |
5216 | |
5217 | if (is_empty_class (type)) |
5218 | /* Leave ctor null for an empty subobject, they aren't represented in the |
5219 | result of evaluation. */ |
5220 | new_ctx.ctor = NULL_TREE; |
5221 | else |
5222 | { |
5223 | tree elt = build_constructor (type, NULL); |
5224 | CONSTRUCTOR_NO_CLEARING (elt) = true; |
5225 | new_ctx.ctor = elt; |
5226 | } |
5227 | |
5228 | if (TREE_CODE (value) == TARGET_EXPR) |
5229 | /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */ |
5230 | 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 | verify_ctor_sanity (const constexpr_ctx *ctx, tree type) |
5238 | { |
5239 | /* We don't bother building a ctor for an empty base subobject. */ |
5240 | 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 | gcc_assert (ctx->ctor); |
5247 | 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 | if (ctx->object) |
5252 | { |
5253 | tree otype = TREE_TYPE (ctx->object); |
5254 | 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 | 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 | cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t, |
5272 | value_cat lval, |
5273 | bool *non_constant_p, bool *overflow_p) |
5274 | { |
5275 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); |
5276 | bool changed = false; |
5277 | gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t)); |
5278 | tree type = TREE_TYPE (t); |
5279 | |
5280 | constexpr_ctx new_ctx; |
5281 | 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 | new_ctx = *ctx; |
5286 | new_ctx.ctor = build_constructor (type, NULL); |
5287 | new_ctx.object = NULL_TREE; |
5288 | ctx = &new_ctx; |
5289 | }; |
5290 | verify_ctor_sanity (ctx, type); |
5291 | vec<constructor_elt, va_gc> **p = nullptr; |
5292 | if (ctx->ctor) |
5293 | { |
5294 | p = &CONSTRUCTOR_ELTS (ctx->ctor); |
5295 | vec_alloc (v&: *p, nelems: vec_safe_length (v)); |
5296 | if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t)) |
5297 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1; |
5298 | } |
5299 | |
5300 | unsigned i; |
5301 | tree index, value; |
5302 | bool constant_p = true; |
5303 | bool side_effects_p = false; |
5304 | FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value) |
5305 | { |
5306 | tree orig_value = value; |
5307 | init_subob_ctx (ctx, new_ctx, index, value); |
5308 | /* Like in cxx_eval_store_expression, omit entries for empty fields. */ |
5309 | bool no_slot = new_ctx.ctor == NULL_TREE; |
5310 | int pos_hint = -1; |
5311 | 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 | constructor_elt *cep = get_or_insert_ctor_field (ctor: ctx->ctor, index); |
5316 | cep->value = new_ctx.ctor; |
5317 | pos_hint = cep - (*p)->begin(); |
5318 | } |
5319 | 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 | get_or_insert_ctor_field (ctor: ctx->ctor, index); |
5324 | tree elt = cxx_eval_constant_expression (&new_ctx, value, |
5325 | lval, |
5326 | non_constant_p, overflow_p); |
5327 | /* Don't VERIFY_CONSTANT here. */ |
5328 | if (ctx->quiet && *non_constant_p) |
5329 | break; |
5330 | if (elt != orig_value) |
5331 | changed = true; |
5332 | |
5333 | if (!TREE_CONSTANT (elt)) |
5334 | constant_p = false; |
5335 | if (TREE_SIDE_EFFECTS (elt)) |
5336 | side_effects_p = true; |
5337 | 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 | constructor_elt *inner = base_field_constructor_elt (v: *p, ref: index); |
5343 | inner->value = elt; |
5344 | changed = true; |
5345 | } |
5346 | 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 | else if (index |
5351 | && (TREE_CODE (index) == NOP_EXPR |
5352 | || TREE_CODE (index) == POINTER_PLUS_EXPR)) |
5353 | { |
5354 | /* Old representation of empty bases. FIXME remove. */ |
5355 | gcc_checking_assert (false); |
5356 | gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index)))); |
5357 | changed = true; |
5358 | } |
5359 | else |
5360 | { |
5361 | if (TREE_CODE (type) == UNION_TYPE |
5362 | && (*p)->last().index != index) |
5363 | /* The initializer erroneously changed the active union member that |
5364 | we're initializing. */ |
5365 | 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 | constructor_elt *cep |
5371 | = get_or_insert_ctor_field (ctor: ctx->ctor, index, pos_hint); |
5372 | cep->value = elt; |
5373 | } |
5374 | |
5375 | /* Adding or replacing an element might change the ctor's flags. */ |
5376 | TREE_CONSTANT (ctx->ctor) = constant_p; |
5377 | TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p; |
5378 | } |
5379 | } |
5380 | if (*non_constant_p) |
5381 | return t; |
5382 | if (!changed) |
5383 | { |
5384 | if (VECTOR_TYPE_P (type)) |
5385 | t = fold (t); |
5386 | return t; |
5387 | } |
5388 | t = ctx->ctor; |
5389 | if (!t) |
5390 | 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 | CONSTRUCTOR_NO_CLEARING (t) = false; |
5394 | TREE_CONSTANT (t) = constant_p; |
5395 | TREE_SIDE_EFFECTS (t) = side_effects_p; |
5396 | if (VECTOR_TYPE_P (type)) |
5397 | 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 | 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 | tree elttype = TREE_TYPE (atype); |
5419 | verify_ctor_sanity (ctx, type: atype); |
5420 | vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); |
5421 | bool pre_init = false; |
5422 | unsigned HOST_WIDE_INT i; |
5423 | tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; |
5424 | |
5425 | if (init && TREE_CODE (init) == CONSTRUCTOR) |
5426 | return cxx_eval_bare_aggregate (ctx, t: init, lval, |
5427 | 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 | if (TREE_CODE (elttype) == ARRAY_TYPE) |
5435 | /* We only do this at the lowest level. */; |
5436 | else if (value_init) |
5437 | { |
5438 | init = build_value_init (elttype, complain); |
5439 | pre_init = true; |
5440 | } |
5441 | else if (!init) |
5442 | { |
5443 | releasing_vec argvec; |
5444 | init = build_special_member_call (NULL_TREE, complete_ctor_identifier, |
5445 | &argvec, elttype, LOOKUP_NORMAL, |
5446 | complain); |
5447 | init = build_aggr_init_expr (elttype, init); |
5448 | pre_init = true; |
5449 | } |
5450 | |
5451 | bool zeroed_out = false; |
5452 | 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 | gcc_checking_assert (initializer_zerop (ctx->ctor)); |
5459 | zeroed_out = true; |
5460 | vec_safe_truncate (v: *p, size: 0); |
5461 | } |
5462 | |
5463 | tree nelts = get_array_or_vector_nelts (ctx, type: atype, non_constant_p, |
5464 | overflow_p); |
5465 | unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts); |
5466 | for (i = 0; i < max; ++i) |
5467 | { |
5468 | tree idx = build_int_cst (size_type_node, i); |
5469 | tree eltinit; |
5470 | bool reuse = false; |
5471 | constexpr_ctx new_ctx; |
5472 | init_subob_ctx (ctx, new_ctx, index: idx, value&: pre_init ? init : elttype); |
5473 | bool no_slot = new_ctx.ctor == NULL_TREE; |
5474 | if (new_ctx.ctor != ctx->ctor && !no_slot) |
5475 | { |
5476 | if (zeroed_out) |
5477 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false; |
5478 | CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor); |
5479 | } |
5480 | if (TREE_CODE (elttype) == ARRAY_TYPE) |
5481 | { |
5482 | /* A multidimensional array; recurse. */ |
5483 | if (value_init || init == NULL_TREE) |
5484 | { |
5485 | eltinit = NULL_TREE; |
5486 | reuse = i == 0; |
5487 | } |
5488 | else |
5489 | eltinit = cp_build_array_ref (input_location, init, idx, complain); |
5490 | eltinit = cxx_eval_vec_init_1 (ctx: &new_ctx, atype: elttype, init: eltinit, value_init, |
5491 | lval, |
5492 | non_constant_p, overflow_p); |
5493 | } |
5494 | else if (pre_init) |
5495 | { |
5496 | /* Initializing an element using value or default initialization |
5497 | we just pre-built above. */ |
5498 | if (init == void_node) |
5499 | /* Trivial default-init, don't do anything to the CONSTRUCTOR. */ |
5500 | return ctx->ctor; |
5501 | eltinit = cxx_eval_constant_expression (&new_ctx, init, lval, |
5502 | non_constant_p, overflow_p); |
5503 | reuse = i == 0; |
5504 | } |
5505 | else |
5506 | { |
5507 | /* Copying an element. */ |
5508 | eltinit = cp_build_array_ref (input_location, init, idx, complain); |
5509 | if (!lvalue_p (init)) |
5510 | eltinit = move (eltinit); |
5511 | eltinit = (perform_implicit_conversion_flags |
5512 | (elttype, eltinit, complain, |
5513 | LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING)); |
5514 | eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval, |
5515 | non_constant_p, overflow_p); |
5516 | } |
5517 | if (*non_constant_p) |
5518 | break; |
5519 | 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 | gcc_checking_assert (i == 0); |
5524 | break; |
5525 | } |
5526 | else if (new_ctx.ctor != ctx->ctor) |
5527 | { |
5528 | /* We appended this element above; update the value. */ |
5529 | gcc_assert ((*p)->last().index == idx); |
5530 | (*p)->last().value = eltinit; |
5531 | } |
5532 | else |
5533 | 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 | if (reuse |
5538 | && max > 1 |
5539 | && (eltinit == NULL_TREE |
5540 | || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit)) |
5541 | == null_pointer_node))) |
5542 | { |
5543 | if (new_ctx.ctor != ctx->ctor) |
5544 | eltinit = new_ctx.ctor; |
5545 | tree range = build2 (RANGE_EXPR, size_type_node, |
5546 | build_int_cst (size_type_node, 1), |
5547 | build_int_cst (size_type_node, max - 1)); |
5548 | CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit)); |
5549 | break; |
5550 | } |
5551 | else if (i == 0) |
5552 | vec_safe_reserve (v&: *p, nelems: max); |
5553 | } |
5554 | |
5555 | if (!*non_constant_p) |
5556 | { |
5557 | init = ctx->ctor; |
5558 | CONSTRUCTOR_NO_CLEARING (init) = false; |
5559 | } |
5560 | return init; |
5561 | } |
5562 | |
5563 | static tree |
5564 | cxx_eval_vec_init (const constexpr_ctx *ctx, tree t, |
5565 | value_cat lval, |
5566 | bool *non_constant_p, bool *overflow_p) |
5567 | { |
5568 | tree atype = TREE_TYPE (t); |
5569 | tree init = VEC_INIT_EXPR_INIT (t); |
5570 | bool value_init = VEC_INIT_EXPR_VALUE_INIT (t); |
5571 | if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init)) |
5572 | ; |
5573 | else if (CONSTRUCTOR_NELTS (init) == 0 |
5574 | && !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 | tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; |
5585 | constexpr_ctx new_ctx = *ctx; |
5586 | 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 | new_ctx.object = VEC_INIT_EXPR_SLOT (t); |
5591 | tree ctor = new_ctx.ctor = build_constructor (atype, NULL); |
5592 | CONSTRUCTOR_NO_CLEARING (ctor) = true; |
5593 | ctx->global->put_value (t: new_ctx.object, v: ctor); |
5594 | ctx = &new_ctx; |
5595 | } |
5596 | init = expand_vec_init_expr (ctx->object, t, complain); |
5597 | return cxx_eval_constant_expression (ctx, init, lval, non_constant_p, |
5598 | overflow_p); |
5599 | } |
5600 | tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init, |
5601 | lval, non_constant_p, overflow_p); |
5602 | if (*non_constant_p) |
5603 | return t; |
5604 | else |
5605 | 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 | same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2) |
5614 | { |
5615 | while (TREE_CODE (type1) == ARRAY_TYPE |
5616 | && TREE_CODE (type2) == ARRAY_TYPE |
5617 | && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2))) |
5618 | { |
5619 | type1 = TREE_TYPE (type1); |
5620 | type2 = TREE_TYPE (type2); |
5621 | } |
5622 | 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 | cxx_union_active_member (const constexpr_ctx *ctx, tree t) |
5631 | { |
5632 | constexpr_ctx new_ctx = *ctx; |
5633 | new_ctx.quiet = true; |
5634 | bool non_constant_p = false, overflow_p = false; |
5635 | tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue, |
5636 | &non_constant_p, |
5637 | &overflow_p); |
5638 | if (TREE_CODE (ctor) == CONSTRUCTOR |
5639 | && CONSTRUCTOR_NELTS (ctor) == 1 |
5640 | && CONSTRUCTOR_ELT (ctor, 0)->index |
5641 | && 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 | 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 | tree optype = TREE_TYPE (op); |
5653 | unsigned HOST_WIDE_INT const_nunits; |
5654 | if (off == 0 && similar_type_p (optype, type)) |
5655 | return op; |
5656 | else if (TREE_CODE (optype) == COMPLEX_TYPE |
5657 | && similar_type_p (type, TREE_TYPE (optype))) |
5658 | { |
5659 | /* *(foo *)&complexfoo => __real__ complexfoo */ |
5660 | if (off == 0) |
5661 | return build1_loc (loc, code: REALPART_EXPR, type, arg1: op); |
5662 | /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */ |
5663 | else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off) |
5664 | return build1_loc (loc, code: IMAGPART_EXPR, type, arg1: op); |
5665 | } |
5666 | /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */ |
5667 | else if (VECTOR_TYPE_P (optype) |
5668 | && similar_type_p (type, TREE_TYPE (optype)) |
5669 | && TYPE_VECTOR_SUBPARTS (node: optype).is_constant (const_value: &const_nunits)) |
5670 | { |
5671 | unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type)); |
5672 | unsigned HOST_WIDE_INT max_offset = part_width * const_nunits; |
5673 | if (off < max_offset && off % part_width == 0) |
5674 | { |
5675 | tree index = bitsize_int (off * BITS_PER_UNIT); |
5676 | return build3_loc (loc, code: BIT_FIELD_REF, type, arg0: op, |
5677 | TYPE_SIZE (type), arg2: index); |
5678 | } |
5679 | } |
5680 | /* ((foo *)&fooarray)[x] => fooarray[x] */ |
5681 | else if (TREE_CODE (optype) == ARRAY_TYPE |
5682 | && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype))) |
5683 | && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype)))) |
5684 | { |
5685 | tree type_domain = TYPE_DOMAIN (optype); |
5686 | tree min_val = size_zero_node; |
5687 | if (type_domain && TYPE_MIN_VALUE (type_domain)) |
5688 | min_val = TYPE_MIN_VALUE (type_domain); |
5689 | unsigned HOST_WIDE_INT el_sz |
5690 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype))); |
5691 | unsigned HOST_WIDE_INT idx = off / el_sz; |
5692 | unsigned HOST_WIDE_INT rem = off % el_sz; |
5693 | if (tree_fits_uhwi_p (min_val)) |
5694 | { |
5695 | tree index = size_int (idx + tree_to_uhwi (min_val)); |
5696 | op = build4_loc (loc, code: ARRAY_REF, TREE_TYPE (optype), arg0: op, arg1: index, |
5697 | NULL_TREE, NULL_TREE); |
5698 | return cxx_fold_indirect_ref_1 (ctx, loc, type, op, off: rem, |
5699 | empty_base); |
5700 | } |
5701 | } |
5702 | /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */ |
5703 | else if (TREE_CODE (optype) == RECORD_TYPE |
5704 | || TREE_CODE (optype) == UNION_TYPE) |
5705 | { |
5706 | if (TREE_CODE (optype) == UNION_TYPE) |
5707 | /* For unions prefer the currently active member. */ |
5708 | if (tree field = cxx_union_active_member (ctx, t: op)) |
5709 | { |
5710 | unsigned HOST_WIDE_INT el_sz |
5711 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field))); |
5712 | if (off < el_sz) |
5713 | { |
5714 | tree cop = build3 (COMPONENT_REF, TREE_TYPE (field), |
5715 | op, field, NULL_TREE); |
5716 | if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, op: cop, |
5717 | off, empty_base)) |
5718 | return ret; |
5719 | } |
5720 | } |
5721 | |
5722 | /* Handle conversion to "as base" type. */ |
5723 | if (CLASS_TYPE_P (optype) |
5724 | && 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 | if (is_empty_class (type) |
5731 | && CLASS_TYPE_P (optype) |
5732 | && lookup_base (optype, type, ba_any, NULL, tf_none, off)) |
5733 | { |
5734 | if (empty_base) |
5735 | *empty_base = true; |
5736 | return op; |
5737 | } |
5738 | |
5739 | for (tree field = TYPE_FIELDS (optype); |
5740 | field; field = DECL_CHAIN (field)) |
5741 | if (TREE_CODE (field) == FIELD_DECL |
5742 | && TREE_TYPE (field) != error_mark_node |
5743 | && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field)))) |
5744 | { |
5745 | tree pos = byte_position (field); |
5746 | if (!tree_fits_uhwi_p (pos)) |
5747 | continue; |
5748 | unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos); |
5749 | unsigned HOST_WIDE_INT el_sz |
5750 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field))); |
5751 | if (upos <= off && off < upos + el_sz) |
5752 | { |
5753 | tree cop = build3 (COMPONENT_REF, TREE_TYPE (field), |
5754 | op, field, NULL_TREE); |
5755 | if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, op: cop, |
5756 | off: off - upos, |
5757 | empty_base)) |
5758 | 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 | cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type, |
5777 | tree op0, bool *empty_base /* = NULL*/) |
5778 | { |
5779 | tree sub = op0; |
5780 | tree subtype; |
5781 | |
5782 | /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */ |
5783 | while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR |
5784 | || TREE_CODE (sub) == VIEW_CONVERT_EXPR) |
5785 | { |
5786 | if (TREE_CODE (sub) == NOP_EXPR |
5787 | && REINTERPRET_CAST_P (sub)) |
5788 | return NULL_TREE; |
5789 | sub = TREE_OPERAND (sub, 0); |
5790 | } |
5791 | |
5792 | subtype = TREE_TYPE (sub); |
5793 | 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 | auto canonicalize_obj_off = [] (tree& obj, tree& off) { |
5801 | while (TREE_CODE (obj) == COMPONENT_REF |
5802 | && (tree_int_cst_sign_bit (off) || integer_zerop (off))) |
5803 | { |
5804 | tree field = TREE_OPERAND (obj, 1); |
5805 | tree pos = byte_position (field); |
5806 | 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 | off = int_const_binop (PLUS_EXPR, off, pos); |
5811 | obj = TREE_OPERAND (obj, 0); |
5812 | } |
5813 | }; |
5814 | |
5815 | if (TREE_CODE (sub) == ADDR_EXPR) |
5816 | { |
5817 | tree op = TREE_OPERAND (sub, 0); |
5818 | tree optype = TREE_TYPE (op); |
5819 | |
5820 | /* *&CONST_DECL -> to the value of the const decl. */ |
5821 | if (TREE_CODE (op) == CONST_DECL) |
5822 | return DECL_INITIAL (op); |
5823 | /* *&p => p; make sure to handle *&"str"[cst] here. */ |
5824 | if (similar_type_p (optype, type)) |
5825 | { |
5826 | tree fop = fold_read_from_constant_string (op); |
5827 | if (fop) |
5828 | return fop; |
5829 | else |
5830 | return op; |
5831 | } |
5832 | else |
5833 | { |
5834 | tree off = integer_zero_node; |
5835 | canonicalize_obj_off (op, off); |
5836 | gcc_assert (integer_zerop (off)); |
5837 | return cxx_fold_indirect_ref_1 (ctx, loc, type, op, off: 0, empty_base); |
5838 | } |
5839 | } |
5840 | else if (TREE_CODE (sub) == POINTER_PLUS_EXPR |
5841 | && tree_fits_uhwi_p (TREE_OPERAND (sub, 1))) |
5842 | { |
5843 | tree op00 = TREE_OPERAND (sub, 0); |
5844 | tree off = TREE_OPERAND (sub, 1); |
5845 | |
5846 | STRIP_NOPS (op00); |
5847 | if (TREE_CODE (op00) == ADDR_EXPR) |
5848 | { |
5849 | tree obj = TREE_OPERAND (op00, 0); |
5850 | canonicalize_obj_off (obj, off); |
5851 | return cxx_fold_indirect_ref_1 (ctx, loc, type, op: obj, |
5852 | off: tree_to_uhwi (off), empty_base); |
5853 | } |
5854 | } |
5855 | /* *(foo *)fooarrptr => (*fooarrptr)[0] */ |
5856 | else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE |
5857 | && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype)))) |
5858 | { |
5859 | tree type_domain; |
5860 | tree min_val = size_zero_node; |
5861 | tree newsub |
5862 | = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), op0: sub, NULL); |
5863 | if (newsub) |
5864 | sub = newsub; |
5865 | else |
5866 | sub = build1_loc (loc, code: INDIRECT_REF, TREE_TYPE (subtype), arg1: sub); |
5867 | type_domain = TYPE_DOMAIN (TREE_TYPE (sub)); |
5868 | if (type_domain && TYPE_MIN_VALUE (type_domain)) |
5869 | min_val = TYPE_MIN_VALUE (type_domain); |
5870 | return build4_loc (loc, code: ARRAY_REF, type, arg0: sub, arg1: min_val, NULL_TREE, |
5871 | NULL_TREE); |
5872 | } |
5873 | |
5874 | return NULL_TREE; |
5875 | } |
5876 | |
5877 | static tree |
5878 | cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t, |
5879 | value_cat lval, |
5880 | bool *non_constant_p, bool *overflow_p) |
5881 | { |
5882 | tree orig_op0 = TREE_OPERAND (t, 0); |
5883 | 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 | if (TREE_CODE (t) == MEM_REF |
5889 | && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1)))) |
5890 | { |
5891 | gcc_assert (ctx->quiet); |
5892 | *non_constant_p = true; |
5893 | return t; |
5894 | } |
5895 | |
5896 | /* First try to simplify it directly. */ |
5897 | tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), |
5898 | op0: orig_op0, empty_base: &empty_base); |
5899 | if (!r) |
5900 | { |
5901 | /* If that didn't work, evaluate the operand first. */ |
5902 | 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 | if (*non_constant_p) |
5907 | return t; |
5908 | |
5909 | if (!lval && integer_zerop (op0)) |
5910 | { |
5911 | if (!ctx->quiet) |
5912 | error ("dereferencing a null pointer" ); |
5913 | *non_constant_p = true; |
5914 | return t; |
5915 | } |
5916 | |
5917 | r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0, |
5918 | empty_base: &empty_base); |
5919 | 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 | tree sub = op0; |
5924 | STRIP_NOPS (sub); |
5925 | if (TREE_CODE (sub) == ADDR_EXPR) |
5926 | { |
5927 | 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 | if (!ctx->quiet) |
5931 | 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 | TREE_TYPE (t)); |
5935 | *non_constant_p = true; |
5936 | return t; |
5937 | } |
5938 | |
5939 | if (lval == vc_glvalue && op0 != orig_op0) |
5940 | return build1 (INDIRECT_REF, TREE_TYPE (t), op0); |
5941 | if (!lval) |
5942 | VERIFY_CONSTANT (t); |
5943 | return t; |
5944 | } |
5945 | } |
5946 | |
5947 | r = cxx_eval_constant_expression (ctx, r, |
5948 | lval, non_constant_p, overflow_p); |
5949 | 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 | if (empty_base && !lval) |
5955 | { |
5956 | r = build_constructor (TREE_TYPE (t), NULL); |
5957 | 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 | outside_lifetime_error (location_t loc, tree r) |
5967 | { |
5968 | auto_diagnostic_group d; |
5969 | if (DECL_NAME (r) == heap_deleted_identifier) |
5970 | { |
5971 | /* Provide a more accurate message for deleted variables. */ |
5972 | error_at (loc, "use of allocated storage after deallocation " |
5973 | "in a constant expression" ); |
5974 | inform (DECL_SOURCE_LOCATION (r), "allocated here" ); |
5975 | } |
5976 | else |
5977 | { |
5978 | error_at (loc, "accessing %qE outside its lifetime" , r); |
5979 | inform (DECL_SOURCE_LOCATION (r), "declared here" ); |
5980 | } |
5981 | } |
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 | non_const_var_error (location_t loc, tree r, bool fundef_p) |
5990 | { |
5991 | auto_diagnostic_group d; |
5992 | tree type = TREE_TYPE (r); |
5993 | if (DECL_NAME (r) == heap_uninit_identifier |
5994 | || DECL_NAME (r) == heap_identifier |
5995 | || DECL_NAME (r) == heap_vec_uninit_identifier |
5996 | || DECL_NAME (r) == heap_vec_identifier) |
5997 | { |
5998 | if (constexpr_error (location: loc, constexpr_fundef_p: fundef_p, gmsgid: "the content of uninitialized " |
5999 | "storage is not usable in a constant expression" )) |
6000 | inform (DECL_SOURCE_LOCATION (r), "allocated here" ); |
6001 | return; |
6002 | } |
6003 | if (DECL_NAME (r) == heap_deleted_identifier) |
6004 | { |
6005 | if (constexpr_error (location: loc, constexpr_fundef_p: fundef_p, gmsgid: "use of allocated storage after " |
6006 | "deallocation in a constant expression" )) |
6007 | inform (DECL_SOURCE_LOCATION (r), "allocated here" ); |
6008 | return; |
6009 | } |
6010 | if (!constexpr_error (location: loc, constexpr_fundef_p: fundef_p, gmsgid: "the value of %qD is not usable in " |
6011 | "a constant expression" , r)) |
6012 | return; |
6013 | /* Avoid error cascade. */ |
6014 | if (DECL_INITIAL (r) == error_mark_node) |
6015 | return; |
6016 | if (DECL_DECLARED_CONSTEXPR_P (r)) |
6017 | inform (DECL_SOURCE_LOCATION (r), |
6018 | "%qD used in its own initializer" , r); |
6019 | else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) |
6020 | { |
6021 | if (!CP_TYPE_CONST_P (type)) |
6022 | inform (DECL_SOURCE_LOCATION (r), |
6023 | "%q#D is not const" , r); |
6024 | else if (CP_TYPE_VOLATILE_P (type)) |
6025 | inform (DECL_SOURCE_LOCATION (r), |
6026 | "%q#D is volatile" , r); |
6027 | else if (!DECL_INITIAL (r) |
6028 | || !TREE_CONSTANT (DECL_INITIAL (r)) |
6029 | || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)) |
6030 | inform (DECL_SOURCE_LOCATION (r), |
6031 | "%qD was not initialized with a constant " |
6032 | "expression" , r); |
6033 | else |
6034 | gcc_unreachable (); |
6035 | } |
6036 | else if (TYPE_REF_P (type)) |
6037 | inform (DECL_SOURCE_LOCATION (r), |
6038 | "%qD was not initialized with a constant " |
6039 | "expression" , r); |
6040 | else |
6041 | { |
6042 | if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r)) |
6043 | inform (DECL_SOURCE_LOCATION (r), |
6044 | "%qD was not declared %<constexpr%>" , r); |
6045 | else |
6046 | inform (DECL_SOURCE_LOCATION (r), |
6047 | "%qD does not have integral or enumeration type" , |
6048 | r); |
6049 | } |
6050 | } |
6051 | |
6052 | /* Subroutine of cxx_eval_constant_expression. |
6053 | Like cxx_eval_unary_expression, except for trinary expressions. */ |
6054 | |
6055 | static tree |
6056 | cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t, |
6057 | value_cat lval, |
6058 | bool *non_constant_p, bool *overflow_p) |
6059 | { |
6060 | int i; |
6061 | tree args[3]; |
6062 | tree val; |
6063 | |
6064 | for (i = 0; i < 3; i++) |
6065 | { |
6066 | args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i), |
6067 | lval, |
6068 | non_constant_p, overflow_p); |
6069 | VERIFY_CONSTANT (args[i]); |
6070 | } |
6071 | |
6072 | val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t), |
6073 | args[0], args[1], args[2]); |
6074 | if (val == NULL_TREE) |
6075 | return t; |
6076 | 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 | var_in_constexpr_fn (tree t) |
6085 | { |
6086 | tree ctx = DECL_CONTEXT (t); |
6087 | return (ctx && TREE_CODE (ctx) == FUNCTION_DECL |
6088 | && 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 | maybe_constexpr_fn (tree t) |
6096 | { |
6097 | return (DECL_DECLARED_CONSTEXPR_P (t) |
6098 | || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t)) |
6099 | || (flag_implicit_constexpr |
6100 | && 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 | var_in_maybe_constexpr_fn (tree t) |
6108 | { |
6109 | return (DECL_FUNCTION_SCOPE_P (t) |
6110 | && 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 | maybe_simplify_trivial_copy (tree &target, tree &init) |
6121 | { |
6122 | if (TREE_CODE (target) == MEM_REF |
6123 | && TREE_CODE (init) == MEM_REF |
6124 | && TREE_TYPE (target) == TREE_TYPE (init) |
6125 | && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE |
6126 | && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node) |
6127 | { |
6128 | target = build_fold_indirect_ref (TREE_OPERAND (target, 0)); |
6129 | init = build_fold_indirect_ref (TREE_OPERAND (init, 0)); |
6130 | } |
6131 | } |
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 | cref_has_const_field (tree ref) |
6139 | { |
6140 | while (TREE_CODE (ref) == COMPONENT_REF) |
6141 | { |
6142 | if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1)))) |
6143 | return true; |
6144 | 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 | modifying_const_object_p (tree_code code, tree obj, bool mutable_p) |
6156 | { |
6157 | /* If this is initialization, there's no problem. */ |
6158 | 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 | if (mutable_p) |
6164 | return false; |
6165 | |
6166 | if (TREE_READONLY (obj)) |
6167 | return true; |
6168 | |
6169 | 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 | if (TREE_CODE (obj) == COMPONENT_REF) |
6177 | return cref_has_const_field (ref: 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 | cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, |
6189 | value_cat lval, |
6190 | bool *non_constant_p, bool *overflow_p) |
6191 | { |
6192 | constexpr_ctx new_ctx = *ctx; |
6193 | |
6194 | tree init = TREE_OPERAND (t, 1); |
6195 | |
6196 | if (TREE_CLOBBER_P (init) |
6197 | && CLOBBER_KIND (init) < CLOBBER_OBJECT_END) |
6198 | /* Only handle clobbers ending the lifetime of objects. */ |
6199 | return void_node; |
6200 | |
6201 | /* First we figure out where we're storing to. */ |
6202 | tree target = TREE_OPERAND (t, 0); |
6203 | |
6204 | maybe_simplify_trivial_copy (target, init); |
6205 | |
6206 | tree type = TREE_TYPE (target); |
6207 | bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR; |
6208 | 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 | if (!SCALAR_TYPE_P (type)) |
6213 | new_ctx.ctor = new_ctx.object = NULL_TREE; |
6214 | init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue, |
6215 | non_constant_p, overflow_p); |
6216 | if (*non_constant_p) |
6217 | return t; |
6218 | } |
6219 | |
6220 | bool evaluated = false; |
6221 | 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 | target = cxx_eval_constant_expression (ctx, target, lval, |
6227 | non_constant_p, overflow_p); |
6228 | evaluated = true; |
6229 | if (*non_constant_p) |
6230 | return t; |
6231 | } |
6232 | |
6233 | /* Find the underlying variable. */ |
6234 | releasing_vec refs; |
6235 | tree object = NULL_TREE; |
6236 | /* If we're modifying a const object, save it. */ |
6237 | tree const_object_being_modified = NULL_TREE; |
6238 | bool mutable_p = false; |
6239 | for (tree probe = target; object == NULL_TREE; ) |
6240 | { |
6241 | switch (TREE_CODE (probe)) |
6242 | { |
6243 | case BIT_FIELD_REF: |
6244 | case COMPONENT_REF: |
6245 | case ARRAY_REF: |
6246 | { |
6247 | tree ob = TREE_OPERAND (probe, 0); |
6248 | tree elt = TREE_OPERAND (probe, 1); |
6249 | if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt)) |
6250 | mutable_p = true; |
6251 | if (TREE_CODE (probe) == ARRAY_REF) |
6252 | { |
6253 | elt = eval_and_check_array_index (ctx, t: probe, allow_one_past: false, |
6254 | non_constant_p, overflow_p); |
6255 | if (*non_constant_p) |
6256 | 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 | else if (evaluated |
6263 | && modifying_const_object_p (TREE_CODE (t), obj: probe, |
6264 | mutable_p) |
6265 | && 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 | if (!evaluated && TREE_CODE (probe) == COMPONENT_REF) |
6271 | vec_safe_push (r&: refs, t: probe); |
6272 | else |
6273 | vec_safe_push (r&: refs, NULL_TREE); |
6274 | |
6275 | vec_safe_push (r&: refs, t: elt); |
6276 | vec_safe_push (r&: refs, TREE_TYPE (probe)); |
6277 | probe = ob; |
6278 | } |
6279 | break; |
6280 | |
6281 | case REALPART_EXPR: |
6282 | gcc_assert (probe == target); |
6283 | vec_safe_push (r&: refs, NULL_TREE); |
6284 | vec_safe_push (r&: refs, t: probe); |
6285 | vec_safe_push (r&: refs, TREE_TYPE (probe)); |
6286 | probe = TREE_OPERAND (probe, 0); |
6287 | break; |
6288 | |
6289 | case IMAGPART_EXPR: |
6290 | gcc_assert (probe == target); |
6291 | vec_safe_push (r&: refs, NULL_TREE); |
6292 | vec_safe_push (r&: refs, t: probe); |
6293 | vec_safe_push (r&: refs, TREE_TYPE (probe)); |
6294 | probe = TREE_OPERAND (probe, 0); |
6295 | break; |
6296 | |
6297 | default: |
6298 | if (evaluated) |
6299 | object = probe; |
6300 | else |
6301 | { |
6302 | probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue, |
6303 | non_constant_p, overflow_p); |
6304 | evaluated = true; |
6305 | if (*non_constant_p) |
6306 | return t; |
6307 | } |
6308 | break; |
6309 | } |
6310 | } |
6311 | |
6312 | if (modifying_const_object_p (TREE_CODE (t), obj: object, mutable_p) |
6313 | && const_object_being_modified == NULL_TREE) |
6314 | const_object_being_modified = object; |
6315 | |
6316 | if (DECL_P (object) |
6317 | && TREE_CLOBBER_P (init) |
6318 | && 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 | return void_node; |
6322 | |
6323 | /* And then find/build up our initializer for the path to the subobject |
6324 | we're initializing. */ |
6325 | tree *valp; |
6326 | if (DECL_P (object)) |
6327 | valp = ctx->global->get_value_ptr (t: object, TREE_CODE (t) == INIT_EXPR); |
6328 | else |
6329 | valp = NULL; |
6330 | if (!valp) |
6331 | { |
6332 | /* A constant-expression cannot modify objects from outside the |
6333 | constant-expression. */ |
6334 | if (!ctx->quiet) |
6335 | { |
6336 | auto_diagnostic_group d; |
6337 | if (DECL_P (object) && DECL_NAME (object) == heap_deleted_identifier) |
6338 | { |
6339 | error ("modification of allocated storage after deallocation " |
6340 | "is not a constant expression" ); |
6341 | inform (DECL_SOURCE_LOCATION (object), "allocated here" ); |
6342 | } |
6343 | else if (DECL_P (object) && ctx->global->is_outside_lifetime (t: object)) |
6344 | { |
6345 | if (TREE_CLOBBER_P (init)) |
6346 | error ("destroying %qE outside its lifetime" , object); |
6347 | else |
6348 | error ("modification of %qE outside its lifetime " |
6349 | "is not a constant expression" , object); |
6350 | inform (DECL_SOURCE_LOCATION (object), "declared here" ); |
6351 | } |
6352 | else |
6353 | { |
6354 | if (TREE_CLOBBER_P (init)) |
6355 | error ("destroying %qE from outside current evaluation " |
6356 | "is not a constant expression" , object); |
6357 | else |
6358 | error ("modification of %qE from outside current evaluation " |
6359 | "is not a constant expression" , object); |
6360 | } |
6361 | } |
6362 | *non_constant_p = true; |
6363 | return t; |
6364 | } |
6365 | |
6366 | /* Handle explicit end-of-lifetime. */ |
6367 | if (TREE_CLOBBER_P (init)) |
6368 | { |
6369 | if (refs->is_empty ()) |
6370 | ctx->global->destroy_value (t: object); |
6371 | return void_node; |
6372 | } |
6373 | |
6374 | type = TREE_TYPE (object); |
6375 | bool no_zero_init = true; |
6376 | |
6377 | auto_vec<tree *> ctors; |
6378 | releasing_vec indexes; |
6379 | auto_vec<int> index_pos_hints; |
6380 | bool activated_union_member_p = false; |
6381 | bool empty_base = false; |
6382 | while (!refs->is_empty ()) |
6383 | { |
6384 | if (*valp == NULL_TREE) |
6385 | { |
6386 | *valp = build_constructor (type, NULL); |
6387 | CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; |
6388 | } |
6389 | else if (STRIP_ANY_LOCATION_WRAPPER (*valp), |
6390 | 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 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); |
6397 | |
6398 | tree string = *valp; |
6399 | tree elt_type = TREE_TYPE (type); |
6400 | unsigned chars_per_elt = (TYPE_PRECISION (elt_type) |
6401 | / TYPE_PRECISION (char_type_node)); |
6402 | unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt; |
6403 | tree ary_ctor = build_constructor (type, NULL); |
6404 | |
6405 | vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), nelems: num_elts); |
6406 | for (unsigned ix = 0; ix != num_elts; ix++) |
6407 | { |
6408 | constructor_elt elt = |
6409 | { |
6410 | .index: build_int_cst (size_type_node, ix), |
6411 | .value: extract_string_elt (string, chars_per_elt, index: ix) |
6412 | }; |
6413 | CONSTRUCTOR_ELTS (ary_ctor)->quick_push (obj: elt); |
6414 | } |
6415 | |
6416 | *valp = ary_ctor; |
6417 | } |
6418 | |
6419 | enum tree_code code = TREE_CODE (type); |
6420 | tree reftype = refs->pop(); |
6421 | tree index = refs->pop(); |
6422 | bool is_access_expr = refs->pop() != NULL_TREE; |
6423 | |
6424 | if (code == COMPLEX_TYPE) |
6425 | { |
6426 | if (TREE_CODE (*valp) == COMPLEX_CST) |
6427 | *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp), |
6428 | TREE_IMAGPART (*valp)); |
6429 | else if (TREE_CODE (*valp) == CONSTRUCTOR |
6430 | && CONSTRUCTOR_NELTS (*valp) == 0 |
6431 | && CONSTRUCTOR_NO_CLEARING (*valp)) |
6432 | { |
6433 | tree r = build_constructor (reftype, NULL); |
6434 | CONSTRUCTOR_NO_CLEARING (r) = 1; |
6435 | *valp = build2 (COMPLEX_EXPR, type, r, r); |
6436 | } |
6437 | gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR); |
6438 | ctors.safe_push (obj: valp); |
6439 | vec_safe_push (r&: indexes, t: index); |
6440 | valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR); |
6441 | gcc_checking_assert (refs->is_empty ()); |
6442 | type = reftype; |
6443 | 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 | no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp); |
6449 | |
6450 | 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 | if (code == UNION_TYPE |
6464 | && !no_zero_init |
6465 | && CONSTRUCTOR_NELTS (*valp) == 0) |
6466 | if (tree first = next_aggregate_field (TYPE_FIELDS (type))) |
6467 | CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (*valp), first, NULL_TREE); |
6468 | |
6469 | /* Check for implicit change of active member for a union. */ |
6470 | if (code == UNION_TYPE |
6471 | && (CONSTRUCTOR_NELTS (*valp) == 0 |
6472 | || 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 | && !(TREE_CODE (t) == INIT_EXPR && refs->is_empty ())) |
6477 | { |
6478 | bool has_active_member = CONSTRUCTOR_NELTS (*valp) != 0; |
6479 | tree inner = strip_array_types (type: reftype); |
6480 | |
6481 | if (has_active_member && cxx_dialect < cxx20) |
6482 | { |
6483 | if (!ctx->quiet) |
6484 | 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 | CONSTRUCTOR_ELT (*valp, 0)->index, |
6489 | index); |
6490 | *non_constant_p = true; |
6491 | } |
6492 | else if (!is_access_expr |
6493 | || (TREE_CODE (t) == MODIFY_EXPR |
6494 | && CLASS_TYPE_P (inner) |
6495 | && !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 | if (!ctx->quiet) |
6501 | { |
6502 | auto_diagnostic_group d; |
6503 | if (has_active_member) |
6504 | error_at (cp_expr_loc_or_input_loc (t), |
6505 | "accessing %qD member instead of initialized " |
6506 | "%qD member in constant expression" , |
6507 | index, CONSTRUCTOR_ELT (*valp, 0)->index); |
6508 | else |
6509 | error_at (cp_expr_loc_or_input_loc (t), |
6510 | "accessing uninitialized member %qD" , |
6511 | index); |
6512 | if (is_access_expr) |
6513 | 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 | inform (DECL_SOURCE_LOCATION (index), |
6521 | "initializing %qD requires a member access " |
6522 | "expression as the left operand of the assignment" , |
6523 | index); |
6524 | } |
6525 | *non_constant_p = true; |
6526 | } |
6527 | 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 | if (!ctx->quiet) |
6532 | 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 | CONSTRUCTOR_ELT (*valp, 0)->index, |
6536 | index); |
6537 | *non_constant_p = true; |
6538 | } |
6539 | no_zero_init = true; |
6540 | } |
6541 | |
6542 | ctors.safe_push (obj: valp); |
6543 | vec_safe_push (r&: indexes, t: index); |
6544 | |
6545 | constructor_elt *cep |
6546 | = get_or_insert_ctor_field (ctor: *valp, index); |
6547 | index_pos_hints.safe_push (obj: cep - CONSTRUCTOR_ELTS (*valp)->begin()); |
6548 | |
6549 | if (code == UNION_TYPE) |
6550 | activated_union_member_p = true; |
6551 | |
6552 | valp = &cep->value; |
6553 | 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 | if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p |
6560 | (initialized_type (t: init), type))) |
6561 | { |
6562 | 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 | if (const_object_being_modified) |
6592 | { |
6593 | bool fail = false; |
6594 | tree const_objtype |
6595 | = strip_array_types (TREE_TYPE (const_object_being_modified)); |
6596 | 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 | for (tree *elt : ctors) |
6606 | if (same_type_ignoring_top_level_qualifiers_p |
6607 | (TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt))) |
6608 | { |
6609 | fail = TREE_READONLY (*elt); |
6610 | break; |
6611 | } |
6612 | } |
6613 | if (fail) |
6614 | { |
6615 | if (!ctx->quiet) |
6616 | modifying_const_object_error (expr: t, obj: const_object_being_modified); |
6617 | *non_constant_p = true; |
6618 | return t; |
6619 | } |
6620 | } |
6621 | |
6622 | 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 | if (*valp == NULL_TREE) |
6630 | { |
6631 | *valp = build_constructor (type, NULL); |
6632 | CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; |
6633 | } |
6634 | new_ctx.ctor = empty_base ? NULL_TREE : *valp; |
6635 | 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 | if (TREE_CODE (init) == TARGET_EXPR) |
6640 | if (tree tinit = TARGET_EXPR_INITIAL (init)) |
6641 | init = tinit; |
6642 | 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 | valp = ctx->global->get_value_ptr (t: object, TREE_CODE (t) == INIT_EXPR); |
6648 | for (unsigned i = 0; i < vec_safe_length (r&: indexes); i++) |
6649 | { |
6650 | ctors[i] = valp; |
6651 | constructor_elt *cep |
6652 | = get_or_insert_ctor_field (ctor: *valp, index: indexes[i], pos_hint: index_pos_hints[i]); |
6653 | valp = &cep->value; |
6654 | } |
6655 | } |
6656 | |
6657 | if (*non_constant_p) |
6658 | return t; |
6659 | |
6660 | /* Don't share a CONSTRUCTOR that might be changed later. */ |
6661 | init = unshare_constructor (t: init); |
6662 | |
6663 | gcc_checking_assert (!*valp || (same_type_ignoring_top_level_qualifiers_p |
6664 | (TREE_TYPE (*valp), type))); |
6665 | 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 | if (!*valp) |
6670 | { |
6671 | /* But do make sure we have something in *valp. */ |
6672 | *valp = build_constructor (type, nullptr); |
6673 | CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; |
6674 | } |
6675 | } |
6676 | else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR |
6677 | && TREE_CODE (init) == CONSTRUCTOR) |
6678 | { |
6679 | /* An outer ctx->ctor might be pointing to *valp, so replace |
6680 | its contents. */ |
6681 | CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init); |
6682 | TREE_CONSTANT (*valp) = TREE_CONSTANT (init); |
6683 | TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init); |
6684 | CONSTRUCTOR_NO_CLEARING (*valp) |
6685 | = CONSTRUCTOR_NO_CLEARING (init); |
6686 | } |
6687 | else |
6688 | *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 | if (TREE_CODE (t) == INIT_EXPR |
6694 | && !empty_base |
6695 | && TREE_CODE (*valp) == CONSTRUCTOR |
6696 | && TYPE_READONLY (type)) |
6697 | { |
6698 | if (INDIRECT_REF_P (target) |
6699 | && (is_this_parameter |
6700 | (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 | gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p |
6705 | (TREE_TYPE (target), type) || empty_base); |
6706 | else |
6707 | TREE_READONLY (*valp) = true; |
6708 | } |
6709 | |
6710 | /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing |
6711 | CONSTRUCTORs, if any. */ |
6712 | bool c = TREE_CONSTANT (init); |
6713 | bool s = TREE_SIDE_EFFECTS (init); |
6714 | if (!indexes->is_empty ()) |
6715 | { |
6716 | tree last = indexes->last (); |
6717 | if (TREE_CODE (last) == REALPART_EXPR |
6718 | || TREE_CODE (last) == IMAGPART_EXPR) |
6719 | { |
6720 | /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if |
6721 | possible. */ |
6722 | tree *cexpr = ctors.last (); |
6723 | if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr), |
6724 | TREE_OPERAND (*cexpr, 0), |
6725 | TREE_OPERAND (*cexpr, 1))) |
6726 | *cexpr = c; |
6727 | else |
6728 | { |
6729 | TREE_CONSTANT (*cexpr) |
6730 | = (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0)) |
6731 | & TREE_CONSTANT (TREE_OPERAND (*cexpr, 1))); |
6732 | TREE_SIDE_EFFECTS (*cexpr) |
6733 | = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0)) |
6734 | | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1))); |
6735 | } |
6736 | c = TREE_CONSTANT (*cexpr); |
6737 | s = TREE_SIDE_EFFECTS (*cexpr); |
6738 | } |
6739 | } |
6740 | if (!c || s || activated_union_member_p) |
6741 | for (tree *elt : ctors) |
6742 | { |
6743 | if (TREE_CODE (*elt) != CONSTRUCTOR) |
6744 | continue; |
6745 | if (!c) |
6746 | TREE_CONSTANT (*elt) = false; |
6747 | if (s) |
6748 | TREE_SIDE_EFFECTS (*elt) = true; |
6749 | /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of |
6750 | this union. */ |
6751 | if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE) |
6752 | CONSTRUCTOR_NO_CLEARING (*elt) = false; |
6753 | } |
6754 | |
6755 | if (lval) |
6756 | return target; |
6757 | else |
6758 | return init; |
6759 | } |
6760 | |
6761 | /* Evaluate a ++ or -- expression. */ |
6762 | |
6763 | static tree |
6764 | cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t, |
6765 | value_cat lval, |
6766 | bool *non_constant_p, bool *overflow_p) |
6767 | { |
6768 | enum tree_code code = TREE_CODE (t); |
6769 | tree type = TREE_TYPE (t); |
6770 | tree op = TREE_OPERAND (t, 0); |
6771 | tree offset = TREE_OPERAND (t, 1); |
6772 | 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 | offset = fold_simple (offset); |
6777 | |
6778 | /* The operand as an lvalue. */ |
6779 | op = cxx_eval_constant_expression (ctx, op, vc_glvalue, |
6780 | non_constant_p, overflow_p); |
6781 | |
6782 | /* The operand as an rvalue. */ |
6783 | tree val |
6784 | = 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 | bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val)); |
6789 | if (!ptr) |
6790 | VERIFY_CONSTANT (val); |
6791 | |
6792 | /* The modified value. */ |
6793 | bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR); |
6794 | tree mod; |
6795 | if (INDIRECT_TYPE_P (type)) |
6796 | { |
6797 | /* The middle end requires pointers to use POINTER_PLUS_EXPR. */ |
6798 | offset = convert_to_ptrofftype (offset); |
6799 | if (!inc) |
6800 | offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset); |
6801 | mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset); |
6802 | } |
6803 | else if (c_promoting_integer_type_p (type) |
6804 | && !TYPE_UNSIGNED (type) |
6805 | && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) |
6806 | { |
6807 | offset = fold_convert (integer_type_node, offset); |
6808 | mod = fold_convert (integer_type_node, val); |
6809 | tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node, |
6810 | mod, offset); |
6811 | mod = fold_convert (type, t); |
6812 | if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t)) |
6813 | TREE_OVERFLOW (mod) = false; |
6814 | } |
6815 | else |
6816 | mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset); |
6817 | if (!ptr) |
6818 | VERIFY_CONSTANT (mod); |
6819 | |
6820 | /* Storing the modified value. */ |
6821 | tree store = build2_loc (loc: cp_expr_loc_or_loc (t, or_loc: input_location), |
6822 | code: MODIFY_EXPR, type, arg0: op, arg1: mod); |
6823 | mod = cxx_eval_constant_expression (ctx, store, lval, |
6824 | non_constant_p, overflow_p); |
6825 | ggc_free (store); |
6826 | if (*non_constant_p) |
6827 | return t; |
6828 | |
6829 | /* And the value of the expression. */ |
6830 | 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 | return val; |
6837 | } |
6838 | |
6839 | /* Predicates for the meaning of *jump_target. */ |
6840 | |
6841 | static bool |
6842 | returns (tree *jump_target) |
6843 | { |
6844 | return *jump_target |
6845 | && TREE_CODE (*jump_target) == RETURN_EXPR; |
6846 | } |
6847 | |
6848 | static bool |
6849 | breaks (tree *jump_target) |
6850 | { |
6851 | return *jump_target |
6852 | && ((TREE_CODE (*jump_target) == LABEL_DECL |
6853 | && LABEL_DECL_BREAK (*jump_target)) |
6854 | || TREE_CODE (*jump_target) == BREAK_STMT |
6855 | || TREE_CODE (*jump_target) == EXIT_EXPR); |
6856 | } |
6857 | |
6858 | static bool |
6859 | continues (tree *jump_target) |
6860 | { |
6861 | return *jump_target |
6862 | && ((TREE_CODE (*jump_target) == LABEL_DECL |
6863 | && LABEL_DECL_CONTINUE (*jump_target)) |
6864 | || TREE_CODE (*jump_target) == CONTINUE_STMT); |
6865 | |
6866 | } |
6867 | |
6868 | static bool |
6869 | switches (tree *jump_target) |
6870 | { |
6871 | return *jump_target |
6872 | && 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 | label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt) |
6881 | { |
6882 | switch (TREE_CODE (*jump_target)) |
6883 | { |
6884 | case LABEL_DECL: |
6885 | if (TREE_CODE (stmt) == LABEL_EXPR |
6886 | && LABEL_EXPR_LABEL (stmt) == *jump_target) |
6887 | return true; |
6888 | break; |
6889 | |
6890 | case INTEGER_CST: |
6891 | if (TREE_CODE (stmt) == CASE_LABEL_EXPR) |
6892 | { |
6893 | gcc_assert (ctx->css_state != NULL); |
6894 | if (!CASE_LOW (stmt)) |
6895 | { |
6896 | /* default: should appear just once in a SWITCH_EXPR |
6897 | body (excluding nested SWITCH_EXPR). */ |
6898 | 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 | if (*ctx->css_state == css_default_processing) |
6902 | return true; |
6903 | *ctx->css_state = css_default_seen; |
6904 | } |
6905 | else if (CASE_HIGH (stmt)) |
6906 | { |
6907 | if (tree_int_cst_le (CASE_LOW (stmt), t2: *jump_target) |
6908 | && tree_int_cst_le (t1: *jump_target, CASE_HIGH (stmt))) |
6909 | return true; |
6910 | } |
6911 | 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 | default: |
6923 | 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 | cxx_eval_statement_list (const constexpr_ctx *ctx, tree t, |
6933 | bool *non_constant_p, bool *overflow_p, |
6934 | tree *jump_target) |
6935 | { |
6936 | tree local_target; |
6937 | /* In a statement-expression we want to return the last value. |
6938 | For empty statement expression return void_node. */ |
6939 | tree r = void_node; |
6940 | if (!jump_target) |
6941 | { |
6942 | local_target = NULL_TREE; |
6943 | jump_target = &local_target; |
6944 | } |
6945 | for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i) |
6946 | { |
6947 | tree stmt = *i; |
6948 | |
6949 | /* We've found a continue, so skip everything until we reach |
6950 | the label its jumping to. */ |
6951 | if (continues (jump_target)) |
6952 | { |
6953 | if (label_matches (ctx, jump_target, stmt)) |
6954 | /* Found it. */ |
6955 | *jump_target = NULL_TREE; |
6956 | else |
6957 | continue; |
6958 | } |
6959 | if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT) |
6960 | continue; |
6961 | |
6962 | value_cat lval = vc_discard; |
6963 | /* The result of a statement-expression is not wrapped in EXPR_STMT. */ |
6964 | if (tsi_one_before_end_p (i) && TREE_CODE (stmt) != EXPR_STMT) |
6965 | lval = vc_prvalue; |
6966 | |
6967 | r = cxx_eval_constant_expression (ctx, stmt, lval, |
6968 | non_constant_p, overflow_p, |
6969 | jump_target); |
6970 | if (*non_constant_p) |
6971 | break; |
6972 | if (returns (jump_target) || breaks (jump_target)) |
6973 | break; |
6974 | } |
6975 | 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 | if (!ctx->quiet) |
6980 | error_at (cp_expr_loc_or_input_loc (t: r), |
6981 | "statement is not a constant expression" ); |
6982 | *non_constant_p = true; |
6983 | } |
6984 | 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 | cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t, |
6992 | bool *non_constant_p, bool *overflow_p, |
6993 | tree *jump_target) |
6994 | { |
6995 | tree local_target; |
6996 | if (!jump_target) |
6997 | { |
6998 | local_target = NULL_TREE; |
6999 | jump_target = &local_target; |
7000 | } |
7001 | |
7002 | tree body, cond = NULL_TREE, expr = NULL_TREE; |
7003 | int count = 0; |
7004 | switch (TREE_CODE (t)) |
7005 | { |
7006 | case LOOP_EXPR: |
7007 | body = LOOP_EXPR_BODY (t); |
7008 | break; |
7009 | case DO_STMT: |
7010 | body = DO_BODY (t); |
7011 | cond = DO_COND (t); |
7012 | break; |
7013 | case WHILE_STMT: |
7014 | body = WHILE_BODY (t); |
7015 | cond = WHILE_COND (t); |
7016 | count = -1; |
7017 | break; |
7018 | case FOR_STMT: |
7019 | if (FOR_INIT_STMT (t)) |
7020 | cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard, |
7021 | non_constant_p, overflow_p, jump_target); |
7022 | if (*non_constant_p) |
7023 | return NULL_TREE; |
7024 | body = FOR_BODY (t); |
7025 | cond = FOR_COND (t); |
7026 | expr = FOR_EXPR (t); |
7027 | count = -1; |
7028 | break; |
7029 | default: |
7030 | gcc_unreachable (); |
7031 | } |
7032 | do |
7033 | { |
7034 | if (count != -1) |
7035 | { |
7036 | if (body) |
7037 | cxx_eval_constant_expression (ctx, body, vc_discard, |
7038 | non_constant_p, overflow_p, |
7039 | jump_target); |
7040 | if (breaks (jump_target)) |
7041 | { |
7042 | *jump_target = NULL_TREE; |
7043 | break; |
7044 | } |
7045 | |
7046 | if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target)) |
7047 | *jump_target = NULL_TREE; |
7048 | |
7049 | if (expr) |
7050 | cxx_eval_constant_expression (ctx, expr, vc_prvalue, |
7051 | non_constant_p, overflow_p, |
7052 | jump_target); |
7053 | } |
7054 | |
7055 | if (cond) |
7056 | { |
7057 | tree res |
7058 | = cxx_eval_constant_expression (ctx, cond, vc_prvalue, |
7059 | non_constant_p, overflow_p, |
7060 | jump_target); |
7061 | if (res) |
7062 | { |
7063 | if (verify_constant (t: res, allow_non_constant: ctx->quiet, non_constant_p, |
7064 | overflow_p)) |
7065 | break; |
7066 | if (integer_zerop (res)) |
7067 | break; |
7068 | } |
7069 | else |
7070 | gcc_assert (*jump_target); |
7071 | } |
7072 | |
7073 | if (++count >= constexpr_loop_limit) |
7074 | { |
7075 | if (!ctx->quiet) |
7076 | 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 | *non_constant_p = true; |
7081 | break; |
7082 | } |
7083 | } |
7084 | while (!returns (jump_target) |
7085 | && !breaks (jump_target) |
7086 | && !continues (jump_target) |
7087 | && (!switches (jump_target) || count == 0) |
7088 | && !*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 | cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t, |
7098 | bool *non_constant_p, bool *overflow_p, |
7099 | tree *jump_target) |
7100 | { |
7101 | tree cond |
7102 | = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t); |
7103 | cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue, |
7104 | non_constant_p, overflow_p); |
7105 | VERIFY_CONSTANT (cond); |
7106 | 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 | gcc_checking_assert (ctx->quiet); |
7112 | *non_constant_p = true; |
7113 | return t; |
7114 | } |
7115 | |
7116 | *jump_target = cond; |
7117 | |
7118 | tree body |
7119 | = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t); |
7120 | constexpr_ctx new_ctx = *ctx; |
7121 | constexpr_switch_state css = css_default_not_seen; |
7122 | new_ctx.css_state = &css; |
7123 | cxx_eval_constant_expression (&new_ctx, body, vc_discard, |
7124 | non_constant_p, overflow_p, jump_target); |
7125 | 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 | css = css_default_processing; |
7131 | cxx_eval_constant_expression (&new_ctx, body, vc_discard, |
7132 | non_constant_p, overflow_p, jump_target); |
7133 | } |
7134 | if (breaks (jump_target) || switches (jump_target)) |
7135 | *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 | lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type) |
7143 | { |
7144 | if (!ctx) |
7145 | return NULL_TREE; |
7146 | |
7147 | /* Prefer the outermost matching object, but don't cross |
7148 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */ |
7149 | if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor)) |
7150 | if (tree outer_ob = lookup_placeholder (ctx: 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 | if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type)) |
7156 | return ctx->ctor; |
7157 | |
7158 | 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 | while (ob) |
7165 | { |
7166 | if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type)) |
7167 | break; |
7168 | if (handled_component_p (t: ob)) |
7169 | 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 | inline_asm_in_constexpr_error (location_t loc, bool fundef_p) |
7182 | { |
7183 | auto_diagnostic_group d; |
7184 | if (constexpr_error (location: loc, constexpr_fundef_p: fundef_p, gmsgid: "inline assembly is not a " |
7185 | "constant expression" )) |
7186 | inform (loc, "only unevaluated inline assembly is allowed in a " |
7187 | "%<constexpr%> function in C++20" ); |
7188 | } |
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 | maybe_warn_about_constant_value (location_t loc, tree decl) |
7195 | { |
7196 | static bool explained = false; |
7197 | if (cxx_dialect >= cxx17 |
7198 | && warn_interference_size |
7199 | && !OPTION_SET_P (param_destruct_interfere_size) |
7200 | && DECL_CONTEXT (decl) == std_node |
7201 | && id_equal (DECL_NAME (decl), str: "hardware_destructive_interference_size" ) |
7202 | && (LOCATION_FILE (input_location) != main_input_filename |
7203 | || module_exporting_p ()) |
7204 | && warning_at (loc, OPT_Winterference_size, "use of %qD" , decl) |
7205 | && !explained) |
7206 | { |
7207 | explained = true; |
7208 | inform (loc, "its value can vary between compiler versions or " |
7209 | "with different %<-mtune%> or %<-mcpu%> flags" ); |
7210 | inform (loc, "if this use is part of a public ABI, change it to " |
7211 | "instead use a constant variable you define" ); |
7212 | inform (loc, "the default value for the current CPU tuning " |
7213 | "is %d bytes" , param_destruct_interfere_size); |
7214 | 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 | } |
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 | 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 | gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size)); |
7239 | gcc_assert (tree_fits_uhwi_p (full_size)); |
7240 | unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0; |
7241 | if (arg_size) |
7242 | { |
7243 | STRIP_NOPS (arg_size); |
7244 | if (cookie_size) |
7245 | { |
7246 | if (TREE_CODE (arg_size) != PLUS_EXPR) |
7247 | arg_size = NULL_TREE; |
7248 | else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST |
7249 | && tree_int_cst_equal (cookie_size, |
7250 | TREE_OPERAND (arg_size, 0))) |
7251 | { |
7252 | arg_size = TREE_OPERAND (arg_size, 1); |
7253 | STRIP_NOPS (arg_size); |
7254 | } |
7255 | else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST |
7256 | && tree_int_cst_equal (cookie_size, |
7257 | TREE_OPERAND (arg_size, 1))) |
7258 | { |
7259 | arg_size = TREE_OPERAND (arg_size, 0); |
7260 | STRIP_NOPS (arg_size); |
7261 | } |
7262 | else |
7263 | arg_size = NULL_TREE; |
7264 | } |
7265 | if (arg_size && TREE_CODE (arg_size) == MULT_EXPR) |
7266 | { |
7267 | tree op0 = TREE_OPERAND (arg_size, 0); |
7268 | tree op1 = TREE_OPERAND (arg_size, 1); |
7269 | if (integer_zerop (op0)) |
7270 | arg_size |
7271 | = cxx_eval_constant_expression (ctx, op1, vc_prvalue, |
7272 | non_constant_p, overflow_p); |
7273 | else if (integer_zerop (op1)) |
7274 | arg_size |
7275 | = 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 | unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size); |
7285 | if (!arg_size) |
7286 | { |
7287 | unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type); |
7288 | gcc_assert (fsz >= csz); |
7289 | fsz -= csz; |
7290 | if (esz) |
7291 | fsz /= esz; |
7292 | } |
7293 | tree itype2 = build_index_type (size_int (fsz - 1)); |
7294 | if (!cookie_size) |
7295 | return build_cplus_array_type (elt_type, itype2); |
7296 | 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 | 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 | 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 | 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 | case LABEL_EXPR: |
7326 | case CASE_LABEL_EXPR: |
7327 | if (label_matches (ctx, jump_target, stmt: t)) |
7328 | /* Found it. */ |
7329 | *jump_target = NULL_TREE; |
7330 | return NULL_TREE; |
7331 | default: |
7332 | return NULL_TREE; |
7333 | } |
7334 | } |
7335 | if (error_operand_p (t)) |
7336 | { |
7337 | *non_constant_p = true; |
7338 | 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 | location_t loc = cp_expr_loc_or_input_loc (t); |
7344 | iloc_sentinel sentinel (loc); |
7345 | |
7346 | STRIP_ANY_LOCATION_WRAPPER (t); |
7347 | |
7348 | if (CONSTANT_CLASS_P (t)) |
7349 | { |
7350 | if (TREE_OVERFLOW (t)) |
7351 | { |
7352 | if (!ctx->quiet) |
7353 | permerror (input_location, "overflow in constant expression" ); |
7354 | if (!flag_permissive || ctx->quiet) |
7355 | *overflow_p = true; |
7356 | } |
7357 | |
7358 | if (TREE_CODE (t) == INTEGER_CST |
7359 | && 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 | && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE |
7364 | && !integer_zerop (t)) |
7365 | { |
7366 | if (!ctx->quiet) |
7367 | error ("value %qE of type %qT is not a constant expression" , |
7368 | t, TREE_TYPE (t)); |
7369 | *non_constant_p = true; |
7370 | } |
7371 | |
7372 | return t; |
7373 | } |
7374 | |
7375 | /* Avoid excessively long constexpr evaluations. */ |
7376 | if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit) |
7377 | { |
7378 | if (!ctx->quiet) |
7379 | 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 | ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT); |
7384 | *non_constant_p = true; |
7385 | return t; |
7386 | } |
7387 | |
7388 | constexpr_ctx new_ctx; |
7389 | tree r = t; |
7390 | |
7391 | tree_code tcode = TREE_CODE (t); |
7392 | switch (tcode) |
7393 | { |
7394 | case RESULT_DECL: |
7395 | 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 | if (tree v = ctx->global->get_value (t)) |
7401 | return v; |
7402 | else |
7403 | { |
7404 | if (!ctx->quiet) |
7405 | error ("%qE is not a constant expression" , t); |
7406 | *non_constant_p = true; |
7407 | } |
7408 | break; |
7409 | |
7410 | case VAR_DECL: |
7411 | if (DECL_HAS_VALUE_EXPR_P (t)) |
7412 | { |
7413 | if (is_normal_capture_proxy (t) |
7414 | && 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 | r = DECL_CAPTURED_VARIABLE (t); |
7420 | tree type = TREE_TYPE (t); |
7421 | if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r))) |
7422 | { |
7423 | /* Adjust r to match the reference-ness of t. */ |
7424 | if (TYPE_REF_P (type)) |
7425 | r = build_address (r); |
7426 | else |
7427 | r = convert_from_reference (r); |
7428 | } |
7429 | } |
7430 | else |
7431 | r = DECL_VALUE_EXPR (t); |
7432 | return cxx_eval_constant_expression (ctx, t: r, lval, non_constant_p, |
7433 | overflow_p); |
7434 | } |
7435 | /* fall through */ |
7436 | case CONST_DECL: |
7437 | /* We used to not check lval for CONST_DECL, but darwin.cc uses |
7438 | CONST_DECL for aggregate constants. */ |
7439 | if (lval) |
7440 | return t; |
7441 | else if (t == ctx->object) |
7442 | return ctx->ctor; |
7443 | if (VAR_P (t)) |
7444 | { |
7445 | if (tree v = ctx->global->get_value (t)) |
7446 | { |
7447 | r = v; |
7448 | break; |
7449 | } |
7450 | if (ctx->global->is_outside_lifetime (t)) |
7451 | { |
7452 | if (!ctx->quiet) |
7453 | outside_lifetime_error (loc, r: t); |
7454 | *non_constant_p = true; |
7455 | break; |
7456 | } |
7457 | } |
7458 | if (ctx->manifestly_const_eval == mce_true) |
7459 | maybe_warn_about_constant_value (loc, decl: t); |
7460 | if (COMPLETE_TYPE_P (TREE_TYPE (t)) |
7461 | && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
7462 | { |
7463 | /* If the class is empty, we aren't actually loading anything. */ |
7464 | r = build_constructor (TREE_TYPE (t), NULL); |
7465 | TREE_CONSTANT (r) = true; |
7466 | } |
7467 | else if (ctx->strict) |
7468 | r = decl_really_constant_value (t, /*unshare_p=*/false); |
7469 | else |
7470 | r = decl_constant_value (t, /*unshare_p=*/false); |
7471 | if (TREE_CODE (r) == TARGET_EXPR |
7472 | && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR) |
7473 | r = TARGET_EXPR_INITIAL (r); |
7474 | if (DECL_P (r) |
7475 | /* P2280 allows references to unknown. */ |
7476 | && !(VAR_P (t) && TYPE_REF_P (TREE_TYPE (t)))) |
7477 | { |
7478 | if (!ctx->quiet) |
7479 | non_const_var_error (loc, r, /*fundef_p*/false); |
7480 | *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 | case PARM_DECL: |
7498 | if (lval && !TYPE_REF_P (TREE_TYPE (t))) |
7499 | /* glvalue use. */; |
7500 | else if (tree v = ctx->global->get_value (t)) |
7501 | r = v; |
7502 | else if (lval) |
7503 | /* Defer in case this is only used for its type. */; |
7504 | else if (ctx->global->is_outside_lifetime (t)) |
7505 | { |
7506 | if (!ctx->quiet) |
7507 | outside_lifetime_error (loc, r: t); |
7508 | *non_constant_p = true; |
7509 | break; |
7510 | } |
7511 | else if (COMPLETE_TYPE_P (TREE_TYPE (t)) |
7512 | && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
7513 | { |
7514 | /* If the class is empty, we aren't actually loading anything. */ |
7515 | r = build_constructor (TREE_TYPE (t), NULL); |
7516 | TREE_CONSTANT (r) = true; |
7517 | } |
7518 | else if (TYPE_REF_P (TREE_TYPE (t))) |
7519 | /* P2280 allows references to unknown... */; |
7520 | else if (is_this_parameter (t)) |
7521 | /* ...as well as the this pointer. */; |
7522 | else |
7523 | { |
7524 | if (!ctx->quiet) |
7525 | error ("%qE is not a constant expression" , t); |
7526 | *non_constant_p = true; |
7527 | } |
7528 | break; |
7529 | |
7530 | case CALL_EXPR: |
7531 | case AGGR_INIT_EXPR: |
7532 | r = cxx_eval_call_expression (ctx, t, lval, |
7533 | non_constant_p, overflow_p); |
7534 | break; |
7535 | |
7536 | case DECL_EXPR: |
7537 | { |
7538 | r = DECL_EXPR_DECL (t); |
7539 | if (TREE_CODE (r) == USING_DECL) |
7540 | { |
7541 | r = void_node; |
7542 | break; |
7543 | } |
7544 | |
7545 | if (VAR_P (r) |
7546 | && (TREE_STATIC (r) |
7547 | || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r))) |
7548 | /* Allow __FUNCTION__ etc. */ |
7549 | && !DECL_ARTIFICIAL (r) |
7550 | && !decl_constant_var_p (r)) |
7551 | { |
7552 | if (!ctx->quiet) |
7553 | { |
7554 | if (CP_DECL_THREAD_LOCAL_P (r)) |
7555 | error_at (loc, "control passes through definition of %qD " |
7556 | "with thread storage duration" , r); |
7557 | else |
7558 | error_at (loc, "control passes through definition of %qD " |
7559 | "with static storage duration" , r); |
7560 | } |
7561 | *non_constant_p = true; |
7562 | 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 | if (VAR_P (r) |
7572 | && TREE_STATIC (r) |
7573 | && !DECL_REALLY_EXTERN (r) |
7574 | && DECL_FUNCTION_SCOPE_P (r) |
7575 | && !var_in_maybe_constexpr_fn (t: r) |
7576 | && decl_constant_var_p (r)) |
7577 | { |
7578 | varpool_node *node = varpool_node::get (decl: r); |
7579 | if (node == NULL || !node->definition) |
7580 | rest_of_decl_compilation (r, 0, at_eof); |
7581 | } |
7582 | |
7583 | if (AGGREGATE_TYPE_P (TREE_TYPE (r)) |
7584 | || VECTOR_TYPE_P (TREE_TYPE (r))) |
7585 | { |
7586 | new_ctx = *ctx; |
7587 | new_ctx.object = r; |
7588 | new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL); |
7589 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; |
7590 | ctx->global->put_value (t: r, v: new_ctx.ctor); |
7591 | ctx = &new_ctx; |
7592 | } |
7593 | |
7594 | if (tree init = DECL_INITIAL (r)) |
7595 | { |
7596 | init = cxx_eval_constant_expression (ctx, t: init, lval: vc_prvalue, |
7597 | non_constant_p, overflow_p); |
7598 | /* Don't share a CONSTRUCTOR that might be changed. */ |
7599 | init = unshare_constructor (t: init); |
7600 | /* Remember that a constant object's constructor has already |
7601 | run. */ |
7602 | if (CLASS_TYPE_P (TREE_TYPE (r)) |
7603 | && CP_TYPE_CONST_P (TREE_TYPE (r))) |
7604 | TREE_READONLY (init) = true; |
7605 | ctx->global->put_value (t: r, v: init); |
7606 | } |
7607 | else if (ctx == &new_ctx) |
7608 | /* We gave it a CONSTRUCTOR above. */; |
7609 | else |
7610 | ctx->global->put_value (t: r, NULL_TREE); |
7611 | } |
7612 | break; |
7613 | |
7614 | case TARGET_EXPR: |
7615 | { |
7616 | tree type = TREE_TYPE (t); |
7617 | |
7618 | if (!literal_type_p (t: type)) |
7619 | { |
7620 | if (!ctx->quiet) |
7621 | { |
7622 | auto_diagnostic_group d; |
7623 | error ("temporary of non-literal type %qT in a " |
7624 | "constant expression" , type); |
7625 | explain_non_literal_class (type); |
7626 | } |
7627 | *non_constant_p = true; |
7628 | break; |
7629 | } |
7630 | gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t)); |
7631 | /* Avoid evaluating a TARGET_EXPR more than once. */ |
7632 | tree slot = TARGET_EXPR_SLOT (t); |
7633 | if (tree v = ctx->global->get_value (t: slot)) |
7634 | { |
7635 | if (lval) |
7636 | return slot; |
7637 | r = v; |
7638 | break; |
7639 | } |
7640 | 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 | 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 | new_ctx.parent = ctx; |
7650 | new_ctx.ctor = build_constructor (type, NULL); |
7651 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; |
7652 | new_ctx.object = slot; |
7653 | ctx->global->put_value (t: new_ctx.object, v: new_ctx.ctor); |
7654 | ctx = &new_ctx; |
7655 | } |
7656 | /* Pass vc_prvalue because this indicates |
7657 | initialization of a temporary. */ |
7658 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), lval: vc_prvalue, |
7659 | non_constant_p, overflow_p); |
7660 | if (*non_constant_p) |
7661 | break; |
7662 | /* If the initializer is complex, evaluate it to initialize slot. */ |
7663 | bool is_complex = target_expr_needs_replace (t); |
7664 | if (!is_complex) |
7665 | { |
7666 | r = unshare_constructor (t: r); |
7667 | /* Adjust the type of the result to the type of the temporary. */ |
7668 | r = adjust_temp_type (type, temp: r); |
7669 | ctx->global->put_value (t: slot, v: r); |
7670 | } |
7671 | if (TARGET_EXPR_CLEANUP (t) && !CLEANUP_EH_ONLY (t)) |
7672 | ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t)); |
7673 | if (ctx->save_exprs) |
7674 | ctx->save_exprs->safe_push (obj: slot); |
7675 | if (lval) |
7676 | return slot; |
7677 | if (is_complex) |
7678 | r = ctx->global->get_value (t: slot); |
7679 | } |
7680 | break; |
7681 | |
7682 | case INIT_EXPR: |
7683 | case MODIFY_EXPR: |
7684 | gcc_assert (jump_target == NULL || *jump_target == NULL_TREE); |
7685 | r = cxx_eval_store_expression (ctx, t, lval, |
7686 | non_constant_p, overflow_p); |
7687 | break; |
7688 | |
7689 | case SCOPE_REF: |
7690 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
7691 | lval, |
7692 | non_constant_p, overflow_p); |
7693 | break; |
7694 | |
7695 | case RETURN_EXPR: |
7696 | if (TREE_OPERAND (t, 0) != NULL_TREE) |
7697 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
7698 | lval, |
7699 | non_constant_p, overflow_p); |
7700 | /* FALLTHRU */ |
7701 | case BREAK_STMT: |
7702 | case CONTINUE_STMT: |
7703 | if (jump_target) |
7704 | *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 | gcc_assert (ctx->quiet); |
7711 | *non_constant_p = true; |
7712 | } |
7713 | break; |
7714 | |
7715 | case SAVE_EXPR: |
7716 | /* Avoid evaluating a SAVE_EXPR more than once. */ |
7717 | if (tree v = ctx->global->get_value (t)) |
7718 | r = v; |
7719 | else |
7720 | { |
7721 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval: vc_prvalue, |
7722 | non_constant_p, overflow_p); |
7723 | if (*non_constant_p) |
7724 | break; |
7725 | ctx->global->put_value (t, v: r); |
7726 | if (ctx->save_exprs) |
7727 | ctx->save_exprs->safe_push (obj: t); |
7728 | } |
7729 | break; |
7730 | |
7731 | case TRY_CATCH_EXPR: |
7732 | if (TREE_OPERAND (t, 0) == NULL_TREE) |
7733 | { |
7734 | r = void_node; |
7735 | break; |
7736 | } |
7737 | /* FALLTHRU */ |
7738 | case NON_LVALUE_EXPR: |
7739 | case TRY_BLOCK: |
7740 | case MUST_NOT_THROW_EXPR: |
7741 | case EXPR_STMT: |
7742 | case EH_SPEC_BLOCK: |
7743 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
7744 | lval, |
7745 | non_constant_p, overflow_p, |
7746 | jump_target); |
7747 | break; |
7748 | |
7749 | case CLEANUP_POINT_EXPR: |
7750 | { |
7751 | auto_vec<tree, 2> cleanups; |
7752 | vec<tree> *prev_cleanups = ctx->global->cleanups; |
7753 | ctx->global->cleanups = &cleanups; |
7754 | |
7755 | auto_vec<tree, 10> save_exprs; |
7756 | constexpr_ctx new_ctx = *ctx; |
7757 | new_ctx.save_exprs = &save_exprs; |
7758 | |
7759 | r = cxx_eval_constant_expression (ctx: &new_ctx, TREE_OPERAND (t, 0), |
7760 | lval, |
7761 | non_constant_p, overflow_p, |
7762 | jump_target); |
7763 | |
7764 | ctx->global->cleanups = prev_cleanups; |
7765 | unsigned int i; |
7766 | tree cleanup; |
7767 | /* Evaluate the cleanups. */ |
7768 | FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup) |
7769 | cxx_eval_constant_expression (ctx: &new_ctx, t: cleanup, lval: vc_discard, |
7770 | non_constant_p, overflow_p); |
7771 | |
7772 | /* Forget SAVE_EXPRs and TARGET_EXPRs created by this |
7773 | full-expression. */ |
7774 | for (tree save_expr : save_exprs) |
7775 | destroy_value_checked (ctx, t: save_expr, non_constant_p); |
7776 | } |
7777 | break; |
7778 | |
7779 | case TRY_FINALLY_EXPR: |
7780 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, |
7781 | non_constant_p, overflow_p, |
7782 | jump_target); |
7783 | if (!*non_constant_p) |
7784 | /* Also evaluate the cleanup. */ |
7785 | cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), lval: vc_discard, |
7786 | non_constant_p, overflow_p); |
7787 | break; |
7788 | |
7789 | case CLEANUP_STMT: |
7790 | r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval, |
7791 | non_constant_p, overflow_p, |
7792 | jump_target); |
7793 | if (!CLEANUP_EH_ONLY (t) && !*non_constant_p) |
7794 | { |
7795 | iloc_sentinel ils (loc); |
7796 | /* Also evaluate the cleanup. */ |
7797 | cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), lval: vc_discard, |
7798 | non_constant_p, overflow_p); |
7799 | } |
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 | case MEM_REF: |
7806 | case INDIRECT_REF: |
7807 | r = cxx_eval_indirect_ref (ctx, t, lval, |
7808 | non_constant_p, overflow_p); |
7809 | break; |
7810 | |
7811 | case ADDR_EXPR: |
7812 | { |
7813 | tree oldop = TREE_OPERAND (t, 0); |
7814 | tree op = cxx_eval_constant_expression (ctx, t: oldop, lval: vc_glvalue, |
7815 | non_constant_p, overflow_p); |
7816 | /* Don't VERIFY_CONSTANT here. */ |
7817 | if (*non_constant_p) |
7818 | return t; |
7819 | gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR); |
7820 | /* This function does more aggressive folding than fold itself. */ |
7821 | r = build_fold_addr_expr_with_type (op, TREE_TYPE (t)); |
7822 | if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop) |
7823 | { |
7824 | ggc_free (r); |
7825 | return t; |
7826 | } |
7827 | break; |
7828 | } |
7829 | |
7830 | case REALPART_EXPR: |
7831 | case IMAGPART_EXPR: |
7832 | if (lval) |
7833 | { |
7834 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, |
7835 | non_constant_p, overflow_p); |
7836 | if (r == error_mark_node) |
7837 | ; |
7838 | else if (r == TREE_OPERAND (t, 0) || lval == vc_discard) |
7839 | r = t; |
7840 | else |
7841 | r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r); |
7842 | break; |
7843 | } |
7844 | /* FALLTHRU */ |
7845 | case CONJ_EXPR: |
7846 | case FIX_TRUNC_EXPR: |
7847 | case FLOAT_EXPR: |
7848 | case NEGATE_EXPR: |
7849 | case ABS_EXPR: |
7850 | case ABSU_EXPR: |
7851 | case BIT_NOT_EXPR: |
7852 | case TRUTH_NOT_EXPR: |
7853 | case FIXED_CONVERT_EXPR: |
7854 | r = cxx_eval_unary_expression (ctx, t, lval, |
7855 | non_constant_p, overflow_p); |
7856 | break; |
7857 | |
7858 | case SIZEOF_EXPR: |
7859 | 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 | if (TREE_CODE (r) != SIZEOF_EXPR) |
7863 | r = cxx_eval_constant_expression (ctx, t: r, lval, |
7864 | non_constant_p, overflow_p, |
7865 | jump_target); |
7866 | else |
7867 | { |
7868 | *non_constant_p = true; |
7869 | gcc_assert (ctx->quiet); |
7870 | } |
7871 | |
7872 | break; |
7873 | |
7874 | case COMPOUND_EXPR: |
7875 | { |
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 | tree op0 = TREE_OPERAND (t, 0); |
7880 | tree op1 = TREE_OPERAND (t, 1); |
7881 | STRIP_NOPS (op1); |
7882 | if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) |
7883 | || TREE_CODE (op1) == EMPTY_CLASS_EXPR) |
7884 | r = cxx_eval_constant_expression (ctx, t: 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 | cxx_eval_constant_expression (ctx, t: op0, lval: vc_discard, |
7891 | non_constant_p, overflow_p, |
7892 | jump_target); |
7893 | if (*non_constant_p) |
7894 | return t; |
7895 | op1 = TREE_OPERAND (t, 1); |
7896 | r = cxx_eval_constant_expression (ctx, t: op1, |
7897 | lval, non_constant_p, overflow_p, |
7898 | jump_target); |
7899 | } |
7900 | } |
7901 | break; |
7902 | |
7903 | case POINTER_PLUS_EXPR: |
7904 | case POINTER_DIFF_EXPR: |
7905 | case PLUS_EXPR: |
7906 | case MINUS_EXPR: |
7907 | case MULT_EXPR: |
7908 | case TRUNC_DIV_EXPR: |
7909 | case CEIL_DIV_EXPR: |
7910 | case FLOOR_DIV_EXPR: |
7911 | case ROUND_DIV_EXPR: |
7912 | case TRUNC_MOD_EXPR: |
7913 | case CEIL_MOD_EXPR: |
7914 | case ROUND_MOD_EXPR: |
7915 | case RDIV_EXPR: |
7916 | case EXACT_DIV_EXPR: |
7917 | case MIN_EXPR: |
7918 | case MAX_EXPR: |
7919 | case LSHIFT_EXPR: |
7920 | case RSHIFT_EXPR: |
7921 | case LROTATE_EXPR: |
7922 | case RROTATE_EXPR: |
7923 | case BIT_IOR_EXPR: |
7924 | case BIT_XOR_EXPR: |
7925 | case BIT_AND_EXPR: |
7926 | case TRUTH_XOR_EXPR: |
7927 | case LT_EXPR: |
7928 | case LE_EXPR: |
7929 | case GT_EXPR: |
7930 | case GE_EXPR: |
7931 | case EQ_EXPR: |
7932 | case NE_EXPR: |
7933 | case SPACESHIP_EXPR: |
7934 | case UNORDERED_EXPR: |
7935 | case ORDERED_EXPR: |
7936 | case UNLT_EXPR: |
7937 | case UNLE_EXPR: |
7938 | case UNGT_EXPR: |
7939 | case UNGE_EXPR: |
7940 | case UNEQ_EXPR: |
7941 | case LTGT_EXPR: |
7942 | case RANGE_EXPR: |
7943 | case COMPLEX_EXPR: |
7944 | r = cxx_eval_binary_expression (ctx, t, lval, |
7945 | non_constant_p, overflow_p); |
7946 | break; |
7947 | |
7948 | /* fold can introduce non-IF versions of these; still treat them as |
7949 | short-circuiting. */ |
7950 | case TRUTH_AND_EXPR: |
7951 | case TRUTH_ANDIF_EXPR: |
7952 | r = cxx_eval_logical_expression (ctx, t, boolean_false_node, |
7953 | boolean_true_node, |
7954 | non_constant_p, overflow_p); |
7955 | break; |
7956 | |
7957 | case TRUTH_OR_EXPR: |
7958 | case TRUTH_ORIF_EXPR: |
7959 | r = cxx_eval_logical_expression (ctx, t, boolean_true_node, |
7960 | boolean_false_node, |
7961 | non_constant_p, overflow_p); |
7962 | break; |
7963 | |
7964 | case ARRAY_REF: |
7965 | r = cxx_eval_array_reference (ctx, t, lval, |
7966 | non_constant_p, overflow_p); |
7967 | break; |
7968 | |
7969 | case COMPONENT_REF: |
7970 | 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 | gcc_checking_assert (ctx->quiet || errorcount); |
7977 | *non_constant_p = true; |
7978 | return t; |
7979 | } |
7980 | r = cxx_eval_component_reference (ctx, t, lval, |
7981 | non_constant_p, overflow_p); |
7982 | break; |
7983 | |
7984 | case BIT_FIELD_REF: |
7985 | r = cxx_eval_bit_field_ref (ctx, t, lval, |
7986 | non_constant_p, overflow_p); |
7987 | break; |
7988 | |
7989 | case COND_EXPR: |
7990 | case IF_STMT: |
7991 | if (jump_target && *jump_target) |
7992 | { |
7993 | tree orig_jump = *jump_target; |
7994 | tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1)) |
7995 | ? 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 | r = cxx_eval_constant_expression (ctx, t: 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 | if (*jump_target == orig_jump) |
8015 | { |
8016 | arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2)) |
8017 | ? TREE_OPERAND (t, 2) : void_node); |
8018 | r = cxx_eval_constant_expression (ctx, t: arg, lval, non_constant_p, |
8019 | overflow_p, jump_target); |
8020 | } |
8021 | break; |
8022 | } |
8023 | r = cxx_eval_conditional_expression (ctx, t, lval, |
8024 | non_constant_p, overflow_p, |
8025 | jump_target); |
8026 | break; |
8027 | case VEC_COND_EXPR: |
8028 | r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p, |
8029 | overflow_p); |
8030 | break; |
8031 | |
8032 | case CONSTRUCTOR: |
8033 | if (TREE_CONSTANT (t) && reduced_constant_expression_p (t)) |
8034 | { |
8035 | /* Don't re-process a constant CONSTRUCTOR. */ |
8036 | verify_constructor_flags (t); |
8037 | if (TREE_CONSTANT (t)) |
8038 | return t; |
8039 | } |
8040 | r = cxx_eval_bare_aggregate (ctx, t, lval, |
8041 | non_constant_p, overflow_p); |
8042 | break; |
8043 | |
8044 | 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 | r = cxx_eval_vec_init (ctx, t, lval, |
8051 | non_constant_p, overflow_p); |
8052 | break; |
8053 | |
8054 | case VEC_PERM_EXPR: |
8055 | r = cxx_eval_trinary_expression (ctx, t, lval, |
8056 | non_constant_p, overflow_p); |
8057 | break; |
8058 | |
8059 | case PAREN_EXPR: |
8060 | 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 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, |
8064 | non_constant_p, overflow_p); |
8065 | break; |
8066 | |
8067 | case NOP_EXPR: |
8068 | if (REINTERPRET_CAST_P (t)) |
8069 | { |
8070 | if (!ctx->quiet) |
8071 | error_at (loc, |
8072 | "%<reinterpret_cast%> is not a constant expression" ); |
8073 | *non_constant_p = true; |
8074 | return t; |
8075 | } |
8076 | /* FALLTHROUGH. */ |
8077 | case CONVERT_EXPR: |
8078 | case VIEW_CONVERT_EXPR: |
8079 | case UNARY_PLUS_EXPR: |
8080 | { |
8081 | tree oldop = TREE_OPERAND (t, 0); |
8082 | |
8083 | tree op = cxx_eval_constant_expression (ctx, t: oldop, |
8084 | lval, |
8085 | non_constant_p, overflow_p); |
8086 | if (*non_constant_p) |
8087 | return t; |
8088 | tree type = TREE_TYPE (t); |
8089 | |
8090 | if (VOID_TYPE_P (type)) |
8091 | return void_node; |
8092 | |
8093 | if (TREE_CODE (t) == CONVERT_EXPR |
8094 | && ARITHMETIC_TYPE_P (type) |
8095 | && INDIRECT_TYPE_P (TREE_TYPE (op)) |
8096 | && ctx->manifestly_const_eval == mce_true) |
8097 | { |
8098 | if (!ctx->quiet) |
8099 | error_at (loc, |
8100 | "conversion from pointer type %qT to arithmetic type " |
8101 | "%qT in a constant expression" , TREE_TYPE (op), type); |
8102 | *non_constant_p = true; |
8103 | 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 | if (TYPE_PTROB_P (type) |
8110 | && TYPE_PTR_P (TREE_TYPE (op)) |
8111 | && 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 | && !is_std_construct_at (call: ctx->call) |
8117 | && !is_std_allocator_allocate (call: ctx->call) |
8118 | && !is_std_source_location_current (call: ctx->call)) |
8119 | { |
8120 | /* Likewise, don't error when casting from void* when OP is |
8121 | &heap uninit and similar. */ |
8122 | tree sop = tree_strip_nop_conversions (op); |
8123 | tree decl = NULL_TREE; |
8124 | if (TREE_CODE (sop) == ADDR_EXPR) |
8125 | decl = TREE_OPERAND (sop, 0); |
8126 | if (decl |
8127 | && VAR_P (decl) |
8128 | && DECL_ARTIFICIAL (decl) |
8129 | && (DECL_NAME (decl) == heap_identifier |
8130 | || DECL_NAME (decl) == heap_uninit_identifier |
8131 | || DECL_NAME (decl) == heap_vec_identifier |
8132 | || 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 | else if (cxx_dialect > cxx23) |
8138 | { |
8139 | r = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), op0: sop); |
8140 | if (r) |
8141 | { |
8142 | r = build1 (ADDR_EXPR, type, r); |
8143 | break; |
8144 | } |
8145 | if (!ctx->quiet) |
8146 | { |
8147 | if (TREE_CODE (sop) == ADDR_EXPR) |
8148 | { |
8149 | auto_diagnostic_group d; |
8150 | 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 | TREE_TYPE (op), TREE_TYPE (TREE_TYPE (sop)), |
8154 | TREE_TYPE (type)); |
8155 | tree obj = build_fold_indirect_ref (sop); |
8156 | inform (DECL_SOURCE_LOCATION (obj), |
8157 | "pointed-to object declared here" ); |
8158 | } |
8159 | else |
8160 | { |
8161 | gcc_assert (integer_zerop (sop)); |
8162 | error_at (loc, "cast from %qT is not allowed in a " |
8163 | "constant expression because " |
8164 | "%qE does not point to an object" , |
8165 | TREE_TYPE (op), oldop); |
8166 | } |
8167 | } |
8168 | *non_constant_p = true; |
8169 | return t; |
8170 | } |
8171 | else |
8172 | { |
8173 | if (!ctx->quiet) |
8174 | error_at (loc, "cast from %qT is not allowed in a " |
8175 | "constant expression before C++26" , |
8176 | TREE_TYPE (op)); |
8177 | *non_constant_p = true; |
8178 | return t; |
8179 | } |
8180 | } |
8181 | |
8182 | if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type)) |
8183 | { |
8184 | op = cplus_expand_constant (op); |
8185 | if (TREE_CODE (op) == PTRMEM_CST) |
8186 | { |
8187 | if (!ctx->quiet) |
8188 | error_at (loc, "%qE is not a constant expression when the " |
8189 | "class %qT is still incomplete" , op, |
8190 | PTRMEM_CST_CLASS (op)); |
8191 | *non_constant_p = true; |
8192 | return t; |
8193 | } |
8194 | } |
8195 | |
8196 | if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR) |
8197 | { |
8198 | if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op)) |
8199 | && !can_convert_qual (type, op)) |
8200 | op = cplus_expand_constant (op); |
8201 | return cp_fold_convert (type, op); |
8202 | } |
8203 | |
8204 | if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST) |
8205 | { |
8206 | if (integer_zerop (op)) |
8207 | { |
8208 | if (TYPE_REF_P (type)) |
8209 | { |
8210 | if (!ctx->quiet) |
8211 | error_at (loc, "dereferencing a null pointer" ); |
8212 | *non_constant_p = true; |
8213 | return t; |
8214 | } |
8215 | } |
8216 | else |
8217 | { |
8218 | /* This detects for example: |
8219 | reinterpret_cast<void*>(sizeof 0) |
8220 | */ |
8221 | if (!ctx->quiet) |
8222 | error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not " |
8223 | "a constant expression" , |
8224 | type, op); |
8225 | *non_constant_p = true; |
8226 | return t; |
8227 | } |
8228 | } |
8229 | |
8230 | if (INDIRECT_TYPE_P (type) |
8231 | && TREE_CODE (op) == NOP_EXPR |
8232 | && TREE_TYPE (op) == ptr_type_node |
8233 | && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR |
8234 | && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0)) |
8235 | && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), |
8236 | 0)) == heap_uninit_identifier |
8237 | || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), |
8238 | 0)) == heap_vec_uninit_identifier)) |
8239 | { |
8240 | tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0); |
8241 | tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var)); |
8242 | tree elt_type = TREE_TYPE (type); |
8243 | tree cookie_size = NULL_TREE; |
8244 | tree arg_size = NULL_TREE; |
8245 | if (TREE_CODE (elt_type) == RECORD_TYPE |
8246 | && TYPE_NAME (elt_type) == heap_identifier) |
8247 | { |
8248 | tree fld1 = TYPE_FIELDS (elt_type); |
8249 | tree fld2 = DECL_CHAIN (fld1); |
8250 | elt_type = TREE_TYPE (TREE_TYPE (fld2)); |
8251 | cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1)); |
8252 | } |
8253 | DECL_NAME (var) |
8254 | = (DECL_NAME (var) == heap_uninit_identifier |
8255 | ? heap_identifier : heap_vec_identifier); |
8256 | /* For zero sized elt_type, try to recover how many outer_nelts |
8257 | it should have. */ |
8258 | if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size) |
8259 | : integer_zerop (var_size)) |
8260 | && !int_size_in_bytes (elt_type) |
8261 | && TREE_CODE (oldop) == CALL_EXPR |
8262 | && call_expr_nargs (oldop) >= 1) |
8263 | if (tree fun = get_function_named_in_call (t: oldop)) |
8264 | if (cxx_replaceable_global_alloc_fn (fndecl: fun) |
8265 | && IDENTIFIER_NEW_OP_P (DECL_NAME (fun))) |
8266 | arg_size = CALL_EXPR_ARG (oldop, 0); |
8267 | TREE_TYPE (var) |
8268 | = build_new_constexpr_heap_type (ctx, elt_type, cookie_size, |
8269 | full_size: var_size, arg_size, |
8270 | non_constant_p, overflow_p); |
8271 | TREE_TYPE (TREE_OPERAND (op, 0)) |
8272 | = build_pointer_type (TREE_TYPE (var)); |
8273 | } |
8274 | |
8275 | 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 | return fold (t); |
8279 | |
8280 | tree sop; |
8281 | |
8282 | /* Handle an array's bounds having been deduced after we built |
8283 | the wrapping expression. */ |
8284 | if (same_type_ignoring_tlq_and_bounds_p (type1: type, TREE_TYPE (op))) |
8285 | r = op; |
8286 | else if (sop = tree_strip_nop_conversions (op), |
8287 | sop != op && (same_type_ignoring_tlq_and_bounds_p |
8288 | (type1: type, TREE_TYPE (sop)))) |
8289 | r = sop; |
8290 | else if (tcode == UNARY_PLUS_EXPR) |
8291 | r = fold_convert (TREE_TYPE (t), op); |
8292 | else |
8293 | 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 | if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op)) |
8299 | TREE_OVERFLOW (r) = false; |
8300 | } |
8301 | break; |
8302 | |
8303 | case EXCESS_PRECISION_EXPR: |
8304 | { |
8305 | tree oldop = TREE_OPERAND (t, 0); |
8306 | |
8307 | tree op = cxx_eval_constant_expression (ctx, t: oldop, |
8308 | lval, |
8309 | non_constant_p, overflow_p); |
8310 | if (*non_constant_p) |
8311 | return t; |
8312 | r = fold_convert (TREE_TYPE (t), op); |
8313 | break; |
8314 | } |
8315 | |
8316 | case EMPTY_CLASS_EXPR: |
8317 | /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering |
8318 | it to an appropriate CONSTRUCTOR. */ |
8319 | return build_constructor (TREE_TYPE (t), NULL); |
8320 | |
8321 | case STATEMENT_LIST: |
8322 | new_ctx = *ctx; |
8323 | new_ctx.ctor = new_ctx.object = NULL_TREE; |
8324 | return cxx_eval_statement_list (ctx: &new_ctx, t, |
8325 | non_constant_p, overflow_p, jump_target); |
8326 | |
8327 | 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 | for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl)) |
8332 | ctx->global->clear_value (t: decl); |
8333 | r = cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t), |
8334 | lval, |
8335 | non_constant_p, overflow_p, |
8336 | jump_target); |
8337 | for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl)) |
8338 | destroy_value_checked (ctx, t: decl, non_constant_p); |
8339 | break; |
8340 | |
8341 | case PREINCREMENT_EXPR: |
8342 | case POSTINCREMENT_EXPR: |
8343 | case PREDECREMENT_EXPR: |
8344 | case POSTDECREMENT_EXPR: |
8345 | return cxx_eval_increment_expression (ctx, t, |
8346 | lval, non_constant_p, overflow_p); |
8347 | |
8348 | case LAMBDA_EXPR: |
8349 | case NEW_EXPR: |
8350 | case VEC_NEW_EXPR: |
8351 | case DELETE_EXPR: |
8352 | case VEC_DELETE_EXPR: |
8353 | case THROW_EXPR: |
8354 | case MODOP_EXPR: |
8355 | /* GCC internal stuff. */ |
8356 | case VA_ARG_EXPR: |
8357 | case BASELINK: |
8358 | case OFFSET_REF: |
8359 | if (!ctx->quiet) |
8360 | error_at (loc, "expression %qE is not a constant expression" , t); |
8361 | *non_constant_p = true; |
8362 | break; |
8363 | |
8364 | case OBJ_TYPE_REF: |
8365 | /* Virtual function lookup. We don't need to do anything fancy. */ |
8366 | return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t), |
8367 | lval, non_constant_p, overflow_p); |
8368 | |
8369 | case PLACEHOLDER_EXPR: |
8370 | /* Use of the value or address of the current object. */ |
8371 | if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t))) |
8372 | { |
8373 | if (TREE_CODE (ctor) == CONSTRUCTOR) |
8374 | return ctor; |
8375 | else |
8376 | return cxx_eval_constant_expression (ctx, t: ctor, lval, |
8377 | 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 | gcc_assert (ctx->quiet); |
8383 | *non_constant_p = true; |
8384 | break; |
8385 | |
8386 | case EXIT_EXPR: |
8387 | { |
8388 | tree cond = TREE_OPERAND (t, 0); |
8389 | cond = cxx_eval_constant_expression (ctx, t: cond, lval: vc_prvalue, |
8390 | non_constant_p, overflow_p); |
8391 | VERIFY_CONSTANT (cond); |
8392 | if (integer_nonzerop (cond)) |
8393 | *jump_target = t; |
8394 | } |
8395 | break; |
8396 | |
8397 | case GOTO_EXPR: |
8398 | if (breaks (jump_target: &TREE_OPERAND (t, 0)) |
8399 | || continues (jump_target: &TREE_OPERAND (t, 0))) |
8400 | *jump_target = TREE_OPERAND (t, 0); |
8401 | else |
8402 | { |
8403 | gcc_assert (cxx_dialect >= cxx23); |
8404 | if (!ctx->quiet) |
8405 | error_at (loc, "%<goto%> is not a constant expression" ); |
8406 | *non_constant_p = true; |
8407 | } |
8408 | break; |
8409 | |
8410 | case LOOP_EXPR: |
8411 | case DO_STMT: |
8412 | case WHILE_STMT: |
8413 | case FOR_STMT: |
8414 | cxx_eval_loop_expr (ctx, t, |
8415 | non_constant_p, overflow_p, jump_target); |
8416 | break; |
8417 | |
8418 | case SWITCH_EXPR: |
8419 | case SWITCH_STMT: |
8420 | cxx_eval_switch_expr (ctx, t, |
8421 | non_constant_p, overflow_p, jump_target); |
8422 | break; |
8423 | |
8424 | 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 | if (!processing_template_decl) |
8438 | return evaluate_requires_expr (t); |
8439 | else |
8440 | *non_constant_p = true; |
8441 | return t; |
8442 | |
8443 | case ANNOTATE_EXPR: |
8444 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
8445 | lval, |
8446 | non_constant_p, overflow_p, |
8447 | jump_target); |
8448 | break; |
8449 | |
8450 | case USING_STMT: |
8451 | r = void_node; |
8452 | break; |
8453 | |
8454 | case ASSERTION_STMT: |
8455 | case PRECONDITION_STMT: |
8456 | case POSTCONDITION_STMT: |
8457 | { |
8458 | contract_semantic semantic = get_contract_semantic (t); |
8459 | if (semantic == CCS_IGNORE) |
8460 | break; |
8461 | |
8462 | if (!cxx_eval_assert (ctx, CONTRACT_CONDITION (t), |
8463 | G_("contract predicate is false in " |
8464 | "constant expression" ), |
8465 | EXPR_LOCATION (t), evaluated: checked_contract_p (cs: semantic), |
8466 | non_constant_p, overflow_p)) |
8467 | *non_constant_p = true; |
8468 | r = void_node; |
8469 | } |
8470 | break; |
8471 | |
8472 | case TEMPLATE_ID_EXPR: |
8473 | { |
8474 | /* We can evaluate template-id that refers to a concept only if |
8475 | the template arguments are non-dependent. */ |
8476 | tree id = unpack_concept_check (t); |
8477 | tree tmpl = TREE_OPERAND (id, 0); |
8478 | if (!concept_definition_p (t: tmpl)) |
8479 | internal_error ("unexpected template-id %qE" , t); |
8480 | |
8481 | if (function_concept_p (t: tmpl)) |
8482 | { |
8483 | if (!ctx->quiet) |
8484 | error_at (cp_expr_loc_or_input_loc (t), |
8485 | "function concept must be called" ); |
8486 | r = error_mark_node; |
8487 | break; |
8488 | } |
8489 | |
8490 | if (!value_dependent_expression_p (t) |
8491 | && !uid_sensitive_constexpr_evaluation_p ()) |
8492 | r = evaluate_concept_check (t); |
8493 | else |
8494 | *non_constant_p = true; |
8495 | |
8496 | break; |
8497 | } |
8498 | |
8499 | case ASM_EXPR: |
8500 | if (!ctx->quiet) |
8501 | inline_asm_in_constexpr_error (loc, /*constexpr_fundef_p*/fundef_p: false); |
8502 | *non_constant_p = true; |
8503 | return t; |
8504 | |
8505 | case BIT_CAST_EXPR: |
8506 | if (lval) |
8507 | { |
8508 | if (!ctx->quiet) |
8509 | error_at (EXPR_LOCATION (t), |
8510 | "address of a call to %qs is not a constant expression" , |
8511 | "__builtin_bit_cast" ); |
8512 | *non_constant_p = true; |
8513 | return t; |
8514 | } |
8515 | r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p); |
8516 | break; |
8517 | |
8518 | case OMP_PARALLEL: |
8519 | case OMP_TASK: |
8520 | case OMP_FOR: |
8521 | case OMP_SIMD: |
8522 | case OMP_DISTRIBUTE: |
8523 | case OMP_TASKLOOP: |
8524 | case OMP_LOOP: |
8525 | case OMP_TEAMS: |
8526 | case OMP_TARGET_DATA: |
8527 | case OMP_TARGET: |
8528 | case OMP_SECTIONS: |
8529 | case OMP_ORDERED: |
8530 | case OMP_CRITICAL: |
8531 | case OMP_SINGLE: |
8532 | case OMP_SCAN: |
8533 | case OMP_SCOPE: |
8534 | case OMP_SECTION: |
8535 | case OMP_STRUCTURED_BLOCK: |
8536 | case OMP_MASTER: |
8537 | case OMP_MASKED: |
8538 | case OMP_TASKGROUP: |
8539 | case OMP_TARGET_UPDATE: |
8540 | case OMP_TARGET_ENTER_DATA: |
8541 | case OMP_TARGET_EXIT_DATA: |
8542 | case OMP_ATOMIC: |
8543 | case OMP_ATOMIC_READ: |
8544 | case OMP_ATOMIC_CAPTURE_OLD: |
8545 | case OMP_ATOMIC_CAPTURE_NEW: |
8546 | case OMP_DEPOBJ: |
8547 | case OACC_PARALLEL: |
8548 | case OACC_KERNELS: |
8549 | case OACC_SERIAL: |
8550 | case OACC_DATA: |
8551 | case OACC_HOST_DATA: |
8552 | case OACC_LOOP: |
8553 | case OACC_CACHE: |
8554 | case OACC_DECLARE: |
8555 | case OACC_ENTER_DATA: |
8556 | case OACC_EXIT_DATA: |
8557 | case OACC_UPDATE: |
8558 | if (!ctx->quiet) |
8559 | error_at (EXPR_LOCATION (t), |
8560 | "statement is not a constant expression" ); |
8561 | *non_constant_p = true; |
8562 | break; |
8563 | |
8564 | default: |
8565 | 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 | if (!ctx->quiet) |
8571 | error_at (EXPR_LOCATION (t), |
8572 | "statement is not a constant expression" ); |
8573 | } |
8574 | else |
8575 | internal_error ("unexpected expression %qE of kind %s" , t, |
8576 | get_tree_code_name (TREE_CODE (t))); |
8577 | *non_constant_p = true; |
8578 | break; |
8579 | } |
8580 | |
8581 | if (r == error_mark_node) |
8582 | *non_constant_p = true; |
8583 | |
8584 | if (*non_constant_p) |
8585 | 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 | instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/) |
8600 | { |
8601 | if (TREE_CODE (*tp) == FUNCTION_DECL |
8602 | && DECL_DECLARED_CONSTEXPR_P (*tp) |
8603 | && !DECL_INITIAL (*tp) |
8604 | && !trivial_fn_p (*tp) |
8605 | && (DECL_TEMPLOID_INSTANTIATION (*tp) || DECL_DEFAULTED_FN (*tp)) |
8606 | && !uid_sensitive_constexpr_evaluation_p ()) |
8607 | { |
8608 | ++function_depth; |
8609 | if (DECL_TEMPLOID_INSTANTIATION (*tp)) |
8610 | instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false); |
8611 | else |
8612 | synthesize_method (*tp); |
8613 | --function_depth; |
8614 | } |
8615 | else if (TREE_CODE (*tp) == CALL_EXPR |
8616 | || TREE_CODE (*tp) == AGGR_INIT_EXPR) |
8617 | { |
8618 | if (EXPR_HAS_LOCATION (*tp)) |
8619 | input_location = EXPR_LOCATION (*tp); |
8620 | } |
8621 | |
8622 | if (!EXPR_P (*tp)) |
8623 | *walk_subtrees = 0; |
8624 | |
8625 | return NULL_TREE; |
8626 | } |
8627 | |
8628 | static void |
8629 | instantiate_constexpr_fns (tree t) |
8630 | { |
8631 | location_t loc = input_location; |
8632 | cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL); |
8633 | input_location = loc; |
8634 | } |
8635 | |
8636 | /* Look for heap variables in the expression *TP. */ |
8637 | |
8638 | static tree |
8639 | find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/) |
8640 | { |
8641 | if (VAR_P (*tp) |
8642 | && (DECL_NAME (*tp) == heap_uninit_identifier |
8643 | || DECL_NAME (*tp) == heap_identifier |
8644 | || DECL_NAME (*tp) == heap_vec_uninit_identifier |
8645 | || DECL_NAME (*tp) == heap_vec_identifier |
8646 | || DECL_NAME (*tp) == heap_deleted_identifier)) |
8647 | return *tp; |
8648 | |
8649 | if (TYPE_P (*tp)) |
8650 | *walk_subtrees = 0; |
8651 | return NULL_TREE; |
8652 | } |
8653 | |
8654 | /* Find immediate function decls in *TP if any. */ |
8655 | |
8656 | static tree |
8657 | find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/) |
8658 | { |
8659 | if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp)) |
8660 | return *tp; |
8661 | if (TREE_CODE (*tp) == PTRMEM_CST |
8662 | && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL |
8663 | && 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 | mark_non_constant (tree t) |
8673 | { |
8674 | 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 | if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR) |
8682 | t = copy_node (t); |
8683 | else if (TREE_CODE (t) == CONSTRUCTOR) |
8684 | t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t); |
8685 | else |
8686 | t = build_nop (TREE_TYPE (t), t); |
8687 | TREE_CONSTANT (t) = false; |
8688 | 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 | 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 | auto_timevar time (TV_CONSTEXPR); |
8708 | |
8709 | bool non_constant_p = false; |
8710 | bool overflow_p = false; |
8711 | |
8712 | if (BRACE_ENCLOSED_INITIALIZER_P (t)) |
8713 | { |
8714 | gcc_checking_assert (allow_non_constant); |
8715 | return t; |
8716 | } |
8717 | |
8718 | constexpr_global_ctx global_ctx; |
8719 | constexpr_ctx ctx = { .global: &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL, |
8720 | .quiet: allow_non_constant, .strict: strict, |
8721 | .manifestly_const_eval: !allow_non_constant ? mce_true : manifestly_const_eval }; |
8722 | |
8723 | /* Turn off -frounding-math for manifestly constant evaluation. */ |
8724 | warning_sentinel rm (flag_rounding_math, |
8725 | ctx.manifestly_const_eval == mce_true); |
8726 | tree type = initialized_type (t); |
8727 | tree r = t; |
8728 | bool is_consteval = false; |
8729 | if (VOID_TYPE_P (type)) |
8730 | { |
8731 | if (constexpr_dtor) |
8732 | /* Used for destructors of array elements. */ |
8733 | type = TREE_TYPE (object); |
8734 | else |
8735 | { |
8736 | if (cxx_dialect < cxx20) |
8737 | return t; |
8738 | 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 | tree fndecl = cp_get_callee_fndecl_nofold (t); |
8743 | if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl)) |
8744 | return t; |
8745 | else |
8746 | is_consteval = true; |
8747 | } |
8748 | } |
8749 | else if (cxx_dialect >= cxx20 |
8750 | && (TREE_CODE (t) == CALL_EXPR |
8751 | || TREE_CODE (t) == AGGR_INIT_EXPR |
8752 | || TREE_CODE (t) == TARGET_EXPR)) |
8753 | { |
8754 | /* For non-concept checks, determine if it is consteval. */ |
8755 | if (!concept_check_p (t)) |
8756 | { |
8757 | tree x = t; |
8758 | if (TREE_CODE (x) == TARGET_EXPR) |
8759 | x = TARGET_EXPR_INITIAL (x); |
8760 | tree fndecl = cp_get_callee_fndecl_nofold (x); |
8761 | if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl)) |
8762 | is_consteval = true; |
8763 | } |
8764 | } |
8765 | 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 | if (constexpr_dtor) |
8775 | { |
8776 | gcc_assert (object && VAR_P (object)); |
8777 | gcc_assert (DECL_DECLARED_CONSTEXPR_P (object)); |
8778 | gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object)); |
8779 | if (error_operand_p (DECL_INITIAL (object))) |
8780 | return t; |
8781 | ctx.ctor = unshare_expr (DECL_INITIAL (object)); |
8782 | 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 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false; |
8786 | } |
8787 | else |
8788 | { |
8789 | ctx.ctor = build_constructor (type, NULL); |
8790 | CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true; |
8791 | } |
8792 | if (!object) |
8793 | { |
8794 | 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 | tree fn = cp_get_callee_fndecl_nofold (t); |
8801 | if (fn && DECL_CONSTRUCTOR_P (fn)) |
8802 | { |
8803 | object = CALL_EXPR_ARG (t, 0); |
8804 | object = build_fold_indirect_ref (object); |
8805 | r = build_aggr_init_expr (type, r); |
8806 | } |
8807 | } |
8808 | else if (TREE_CODE (t) == TARGET_EXPR) |
8809 | object = TARGET_EXPR_SLOT (t); |
8810 | else if (TREE_CODE (t) == AGGR_INIT_EXPR) |
8811 | object = AGGR_INIT_EXPR_SLOT (t); |
8812 | } |
8813 | ctx.object = object; |
8814 | if (object) |
8815 | gcc_assert (same_type_ignoring_top_level_qualifiers_p |
8816 | (type, TREE_TYPE (object))); |
8817 | if (object && DECL_P (object)) |
8818 | global_ctx.put_value (t: object, v: ctx.ctor); |
8819 | if (TREE_CODE (r) == TARGET_EXPR) |
8820 | /* Avoid creating another CONSTRUCTOR when we expand the |
8821 | TARGET_EXPR. */ |
8822 | r = TARGET_EXPR_INITIAL (r); |
8823 | } |
8824 | |
8825 | auto_vec<tree, 16> cleanups; |
8826 | global_ctx.cleanups = &cleanups; |
8827 | |
8828 | if (manifestly_const_eval == mce_true) |
8829 | instantiate_constexpr_fns (t: r); |
8830 | r = cxx_eval_constant_expression (ctx: &ctx, t: r, lval: vc_prvalue, |
8831 | non_constant_p: &non_constant_p, overflow_p: &overflow_p); |
8832 | |
8833 | if (!constexpr_dtor) |
8834 | verify_constant (t: r, allow_non_constant, non_constant_p: &non_constant_p, overflow_p: &overflow_p); |
8835 | else |
8836 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true; |
8837 | |
8838 | unsigned int i; |
8839 | tree cleanup; |
8840 | /* Evaluate the cleanups. */ |
8841 | FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup) |
8842 | cxx_eval_constant_expression (ctx: &ctx, t: cleanup, lval: vc_discard, |
8843 | non_constant_p: &non_constant_p, overflow_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 | if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_MUTABLE_POISON (r)) |
8849 | { |
8850 | if (!allow_non_constant) |
8851 | error ("%qE is not a constant expression because it refers to " |
8852 | "mutable subobjects of %qT" , t, type); |
8853 | non_constant_p = true; |
8854 | } |
8855 | |
8856 | if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r)) |
8857 | { |
8858 | if (!allow_non_constant) |
8859 | error ("%qE is not a constant expression because it refers to " |
8860 | "an incompletely initialized variable" , t); |
8861 | TREE_CONSTANT (r) = false; |
8862 | non_constant_p = true; |
8863 | } |
8864 | |
8865 | if (!non_constant_p && cxx_dialect >= cxx20 |
8866 | && !global_ctx.heap_vars.is_empty ()) |
8867 | { |
8868 | tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs, |
8869 | NULL); |
8870 | unsigned int i; |
8871 | if (heap_var) |
8872 | { |
8873 | if (!allow_non_constant && !non_constant_p) |
8874 | 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 | r = t; |
8878 | non_constant_p = true; |
8879 | } |
8880 | FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var) |
8881 | { |
8882 | if (DECL_NAME (heap_var) != heap_deleted_identifier) |
8883 | { |
8884 | if (!allow_non_constant && !non_constant_p) |
8885 | error_at (DECL_SOURCE_LOCATION (heap_var), |
8886 | "%qE is not a constant expression because allocated " |
8887 | "storage has not been deallocated" , t); |
8888 | r = t; |
8889 | non_constant_p = true; |
8890 | } |
8891 | varpool_node::get (decl: heap_var)->remove (); |
8892 | } |
8893 | } |
8894 | |
8895 | /* Check that immediate invocation does not return an expression referencing |
8896 | any immediate function decls. */ |
8897 | if (!non_constant_p && cxx_dialect >= cxx20) |
8898 | if (tree immediate_fndecl |
8899 | = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl, |
8900 | NULL)) |
8901 | { |
8902 | if (!allow_non_constant && !non_constant_p) |
8903 | { |
8904 | if (is_consteval) |
8905 | error_at (cp_expr_loc_or_input_loc (t), |
8906 | "immediate evaluation returns address of immediate " |
8907 | "function %qD" , immediate_fndecl); |
8908 | else |
8909 | error_at (cp_expr_loc_or_input_loc (t), |
8910 | "constant evaluation returns address of immediate " |
8911 | "function %qD" , immediate_fndecl); |
8912 | } |
8913 | r = t; |
8914 | non_constant_p = true; |
8915 | } |
8916 | |
8917 | 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 | r = t; |
8921 | |
8922 | if (!non_constant_p && overflow_p) |
8923 | non_constant_p = true; |
8924 | |
8925 | /* Unshare the result. */ |
8926 | bool should_unshare = true; |
8927 | if (r == t || (TREE_CODE (t) == TARGET_EXPR |
8928 | && TARGET_EXPR_INITIAL (t) == r)) |
8929 | should_unshare = false; |
8930 | |
8931 | if (non_constant_p && !allow_non_constant) |
8932 | return error_mark_node; |
8933 | else if (constexpr_dtor) |
8934 | return r; |
8935 | else if (non_constant_p && TREE_CONSTANT (r)) |
8936 | r = mark_non_constant (t: r); |
8937 | else if (non_constant_p) |
8938 | return t; |
8939 | |
8940 | if (should_unshare) |
8941 | r = unshare_expr (r); |
8942 | |
8943 | if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r))) |
8944 | { |
8945 | r = adjust_temp_type (type, temp: r); |
8946 | if (TREE_CODE (t) == TARGET_EXPR |
8947 | && TARGET_EXPR_INITIAL (t) == r) |
8948 | return t; |
8949 | 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 | else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t)) |
8952 | r = get_target_expr (r); |
8953 | else |
8954 | { |
8955 | r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup); |
8956 | TREE_CONSTANT (r) = true; |
8957 | } |
8958 | } |
8959 | |
8960 | if (TREE_CODE (t) == TARGET_EXPR |
8961 | && TREE_CODE (r) == TARGET_EXPR) |
8962 | { |
8963 | /* Preserve this flag for potential_constant_expression, and the others |
8964 | for good measure. */ |
8965 | TARGET_EXPR_ELIDING_P (r) = TARGET_EXPR_ELIDING_P (t); |
8966 | TARGET_EXPR_IMPLICIT_P (r) = TARGET_EXPR_IMPLICIT_P (t); |
8967 | TARGET_EXPR_LIST_INIT_P (r) = TARGET_EXPR_LIST_INIT_P (t); |
8968 | 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 | if (location_t loc = EXPR_LOCATION (t)) |
8973 | protected_set_expr_location (r, loc); |
8974 | |
8975 | return r; |
8976 | } |
8977 | |
8978 | /* If T represents a constant expression returns its reduced value. |
8979 | Otherwise return error_mark_node. */ |
8980 | |
8981 | tree |
8982 | cxx_constant_value (tree t, tree decl /* = NULL_TREE */, |
8983 | tsubst_flags_t complain /* = tf_error */) |
8984 | { |
8985 | bool sfinae = !(complain & tf_error); |
8986 | tree r = cxx_eval_outermost_constant_expr (t, allow_non_constant: sfinae, strict: true, manifestly_const_eval: mce_true, constexpr_dtor: false, object: decl); |
8987 | if (sfinae && !TREE_CONSTANT (r)) |
8988 | r = error_mark_node; |
8989 | 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 | cxx_constant_dtor (tree t, tree decl) |
8997 | { |
8998 | cxx_eval_outermost_constant_expr (t, allow_non_constant: false, strict: true, manifestly_const_eval: mce_true, constexpr_dtor: true, object: decl); |
8999 | } |
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 | fold_simple_1 (tree t) |
9009 | { |
9010 | tree op1; |
9011 | enum tree_code code = TREE_CODE (t); |
9012 | |
9013 | 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 | case SIZEOF_EXPR: |
9023 | return fold_sizeof_expr (t); |
9024 | |
9025 | case ABS_EXPR: |
9026 | case ABSU_EXPR: |
9027 | case CONJ_EXPR: |
9028 | case REALPART_EXPR: |
9029 | case IMAGPART_EXPR: |
9030 | case NEGATE_EXPR: |
9031 | case BIT_NOT_EXPR: |
9032 | case TRUTH_NOT_EXPR: |
9033 | case VIEW_CONVERT_EXPR: |
9034 | CASE_CONVERT: |
9035 | case FLOAT_EXPR: |
9036 | case FIX_TRUNC_EXPR: |
9037 | case FIXED_CONVERT_EXPR: |
9038 | case ADDR_SPACE_CONVERT_EXPR: |
9039 | |
9040 | op1 = TREE_OPERAND (t, 0); |
9041 | |
9042 | t = const_unop (code, TREE_TYPE (t), op1); |
9043 | if (!t) |
9044 | return NULL_TREE; |
9045 | |
9046 | if (CONVERT_EXPR_CODE_P (code) |
9047 | && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1)) |
9048 | 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 | fold_simple (tree t) |
9063 | { |
9064 | if (processing_template_decl) |
9065 | return t; |
9066 | |
9067 | tree r = fold_simple_1 (t); |
9068 | 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 | fold_to_constant (tree t) |
9079 | { |
9080 | tree r = fold (t); |
9081 | if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r)) |
9082 | return r; |
9083 | else |
9084 | 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 | maybe_constant_value (tree t, tree decl /* = NULL_TREE */, |
9097 | mce_value manifestly_const_eval /* = mce_unknown */) |
9098 | { |
9099 | tree r; |
9100 | |
9101 | if (!is_nondependent_constant_expression (t)) |
9102 | { |
9103 | if (TREE_OVERFLOW_P (t) |
9104 | || (!processing_template_decl && TREE_CONSTANT (t))) |
9105 | t = mark_non_constant (t); |
9106 | return t; |
9107 | } |
9108 | 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 | if (cp_unevaluated_operand && manifestly_const_eval != mce_true) |
9115 | return fold_to_constant (t); |
9116 | |
9117 | if (manifestly_const_eval != mce_unknown) |
9118 | return cxx_eval_outermost_constant_expr (t, allow_non_constant: true, strict: true, |
9119 | manifestly_const_eval, constexpr_dtor: false, object: decl); |
9120 | |
9121 | if (cv_cache == NULL) |
9122 | cv_cache = hash_map<tree, tree>::create_ggc (size: 101); |
9123 | if (tree *cached = cv_cache->get (k: t)) |
9124 | { |
9125 | r = *cached; |
9126 | 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 | processing_template_decl_sentinel ptds; |
9131 | |
9132 | r = break_out_target_exprs (r, /*clear_loc*/true); |
9133 | protected_set_expr_location (r, EXPR_LOCATION (t)); |
9134 | } |
9135 | return r; |
9136 | } |
9137 | |
9138 | uid_sensitive_constexpr_evaluation_checker c; |
9139 | r = cxx_eval_outermost_constant_expr (t, allow_non_constant: true, strict: true, |
9140 | manifestly_const_eval, constexpr_dtor: false, object: decl); |
9141 | 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 | if (!c.evaluation_restricted_p ()) |
9147 | cv_cache->put (k: t, v: r); |
9148 | return r; |
9149 | } |
9150 | |
9151 | /* Dispose of the whole CV_CACHE. */ |
9152 | |
9153 | static void |
9154 | clear_cv_cache (void) |
9155 | { |
9156 | if (cv_cache != NULL) |
9157 | cv_cache->empty (); |
9158 | } |
9159 | |
9160 | /* Dispose of the whole CV_CACHE and FOLD_CACHE. */ |
9161 | |
9162 | void |
9163 | clear_cv_and_fold_caches () |
9164 | { |
9165 | clear_cv_cache (); |
9166 | clear_fold_cache (); |
9167 | } |
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 | fold_non_dependent_expr_template (tree t, tsubst_flags_t complain, |
9182 | bool manifestly_const_eval, |
9183 | tree object) |
9184 | { |
9185 | gcc_assert (processing_template_decl); |
9186 | |
9187 | if (is_nondependent_constant_expression (t)) |
9188 | { |
9189 | processing_template_decl_sentinel s; |
9190 | t = instantiate_non_dependent_expr_internal (t, complain); |
9191 | |
9192 | if (type_unknown_p (expr: t) || BRACE_ENCLOSED_INITIALIZER_P (t)) |
9193 | { |
9194 | if (TREE_OVERFLOW_P (t)) |
9195 | { |
9196 | t = build_nop (TREE_TYPE (t), t); |
9197 | TREE_CONSTANT (t) = false; |
9198 | } |
9199 | return t; |
9200 | } |
9201 | 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 | if (cp_unevaluated_operand && !manifestly_const_eval) |
9208 | return fold_to_constant (t); |
9209 | |
9210 | tree r = cxx_eval_outermost_constant_expr (t, allow_non_constant: true, strict: true, |
9211 | manifestly_const_eval: mce_value (manifestly_const_eval), |
9212 | constexpr_dtor: false, object); |
9213 | /* cp_tree_equal looks through NOPs, so allow them. */ |
9214 | 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 | return r; |
9220 | } |
9221 | else if (TREE_OVERFLOW_P (t)) |
9222 | { |
9223 | t = build_nop (TREE_TYPE (t), t); |
9224 | 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 | 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 | if (t == NULL_TREE) |
9252 | return NULL_TREE; |
9253 | |
9254 | if (processing_template_decl) |
9255 | return fold_non_dependent_expr_template (t, complain, |
9256 | manifestly_const_eval, object); |
9257 | |
9258 | return maybe_constant_value (t, decl: object, manifestly_const_eval: 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 | maybe_fold_non_dependent_expr (tree expr, |
9266 | tsubst_flags_t complain/*=tf_warning_or_error*/) |
9267 | { |
9268 | tree t = fold_non_dependent_expr (t: expr, complain); |
9269 | if (t && TREE_CONSTANT (t)) |
9270 | return t; |
9271 | |
9272 | return expr; |
9273 | } |
9274 | |
9275 | /* Like maybe_constant_init but first fully instantiate the argument. */ |
9276 | |
9277 | tree |
9278 | 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 | if (t == NULL_TREE) |
9284 | return NULL_TREE; |
9285 | |
9286 | if (processing_template_decl) |
9287 | { |
9288 | 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 | if (TREE_CODE (t) == TARGET_EXPR) |
9292 | { |
9293 | tree init = TARGET_EXPR_INITIAL (t); |
9294 | if (TREE_CODE (init) == CONSTRUCTOR) |
9295 | t = init; |
9296 | } |
9297 | return t; |
9298 | } |
9299 | |
9300 | 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 | maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant, |
9311 | bool manifestly_const_eval) |
9312 | { |
9313 | if (!t) |
9314 | return t; |
9315 | if (TREE_CODE (t) == EXPR_STMT) |
9316 | t = TREE_OPERAND (t, 0); |
9317 | if (TREE_CODE (t) == CONVERT_EXPR |
9318 | && VOID_TYPE_P (TREE_TYPE (t))) |
9319 | t = TREE_OPERAND (t, 0); |
9320 | if (TREE_CODE (t) == INIT_EXPR) |
9321 | t = TREE_OPERAND (t, 1); |
9322 | if (TREE_CODE (t) == TARGET_EXPR) |
9323 | t = TARGET_EXPR_INITIAL (t); |
9324 | if (!is_nondependent_static_init_expression (t)) |
9325 | /* Don't try to evaluate it. */; |
9326 | 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 | bool is_static = (decl && DECL_P (decl) |
9334 | && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))); |
9335 | if (is_static) |
9336 | manifestly_const_eval = true; |
9337 | |
9338 | if (cp_unevaluated_operand && !manifestly_const_eval) |
9339 | return fold_to_constant (t); |
9340 | |
9341 | t = cxx_eval_outermost_constant_expr (t, allow_non_constant, strict: !is_static, |
9342 | manifestly_const_eval: mce_value (manifestly_const_eval), |
9343 | constexpr_dtor: false, object: decl); |
9344 | } |
9345 | if (TREE_CODE (t) == TARGET_EXPR) |
9346 | { |
9347 | tree init = TARGET_EXPR_INITIAL (t); |
9348 | if (TREE_CODE (init) == CONSTRUCTOR) |
9349 | t = init; |
9350 | } |
9351 | return t; |
9352 | } |
9353 | |
9354 | /* Wrapper for maybe_constant_init_1 which permits non constants. */ |
9355 | |
9356 | tree |
9357 | maybe_constant_init (tree t, tree decl, bool manifestly_const_eval) |
9358 | { |
9359 | return maybe_constant_init_1 (t, decl, allow_non_constant: true, manifestly_const_eval); |
9360 | } |
9361 | |
9362 | /* Wrapper for maybe_constant_init_1 which does not permit non constants. */ |
9363 | |
9364 | tree |
9365 | cxx_constant_init (tree t, tree decl) |
9366 | { |
9367 | return maybe_constant_init_1 (t, decl, allow_non_constant: false, manifestly_const_eval: 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 | check_for_return_continue (tree *tp, int *walk_subtrees, void *data) |
9409 | { |
9410 | tree t = *tp, s, b; |
9411 | check_for_return_continue_data *d = (check_for_return_continue_data *) data; |
9412 | switch (TREE_CODE (t)) |
9413 | { |
9414 | case RETURN_EXPR: |
9415 | return t; |
9416 | |
9417 | case CONTINUE_STMT: |
9418 | if (d->continue_stmt == NULL_TREE) |
9419 | d->continue_stmt = t; |
9420 | break; |
9421 | |
9422 | case BREAK_STMT: |
9423 | if (d->break_stmt == NULL_TREE) |
9424 | 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 | case DO_STMT: |
9435 | *walk_subtrees = 0; |
9436 | RECUR (DO_COND (t)); |
9437 | s = d->continue_stmt; |
9438 | b = d->break_stmt; |
9439 | RECUR (DO_BODY (t)); |
9440 | d->continue_stmt = s; |
9441 | d->break_stmt = b; |
9442 | break; |
9443 | |
9444 | case WHILE_STMT: |
9445 | *walk_subtrees = 0; |
9446 | RECUR (WHILE_COND (t)); |
9447 | s = d->continue_stmt; |
9448 | b = d->break_stmt; |
9449 | RECUR (WHILE_BODY (t)); |
9450 | d->continue_stmt = s; |
9451 | d->break_stmt = b; |
9452 | break; |
9453 | |
9454 | case FOR_STMT: |
9455 | *walk_subtrees = 0; |
9456 | RECUR (FOR_INIT_STMT (t)); |
9457 | RECUR (FOR_COND (t)); |
9458 | RECUR (FOR_EXPR (t)); |
9459 | s = d->continue_stmt; |
9460 | b = d->break_stmt; |
9461 | RECUR (FOR_BODY (t)); |
9462 | d->continue_stmt = s; |
9463 | d->break_stmt = b; |
9464 | break; |
9465 | |
9466 | case RANGE_FOR_STMT: |
9467 | *walk_subtrees = 0; |
9468 | RECUR (RANGE_FOR_EXPR (t)); |
9469 | s = d->continue_stmt; |
9470 | b = d->break_stmt; |
9471 | RECUR (RANGE_FOR_BODY (t)); |
9472 | d->continue_stmt = s; |
9473 | d->break_stmt = b; |
9474 | break; |
9475 | |
9476 | case SWITCH_STMT: |
9477 | *walk_subtrees = 0; |
9478 | RECUR (SWITCH_STMT_COND (t)); |
9479 | b = d->break_stmt; |
9480 | RECUR (SWITCH_STMT_BODY (t)); |
9481 | d->break_stmt = b; |
9482 | break; |
9483 | #undef RECUR |
9484 | |
9485 | case STATEMENT_LIST: |
9486 | case CONSTRUCTOR: |
9487 | break; |
9488 | |
9489 | default: |
9490 | if (!EXPR_P (t)) |
9491 | *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 | 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 | enum { any = false, rval = true }; |
9528 | int i; |
9529 | tree tmp; |
9530 | |
9531 | if (t == error_mark_node) |
9532 | return false; |
9533 | if (t == NULL_TREE) |
9534 | return true; |
9535 | location_t loc = cp_expr_loc_or_input_loc (t); |
9536 | |
9537 | 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 | if (TREE_THIS_VOLATILE (t) && want_rval |
9545 | && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t))) |
9546 | { |
9547 | if (flags & tf_error) |
9548 | constexpr_error (location: loc, constexpr_fundef_p: fundef_p, gmsgid: "lvalue-to-rvalue conversion of " |
9549 | "a volatile lvalue %qE with type %qT" , t, |
9550 | TREE_TYPE (t)); |
9551 | return false; |
9552 | } |
9553 | if (CONSTANT_CLASS_P (t)) |
9554 | return true; |
9555 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED) |
9556 | && TREE_TYPE (t) == error_mark_node) |
9557 | return false; |
9558 | |
9559 | 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 | case RETURN_EXPR: |
9590 | if (!RECUR (TREE_OPERAND (t, 0), any)) |
9591 | return false; |
9592 | /* FALLTHROUGH */ |
9593 | |
9594 | case BREAK_STMT: |
9595 | case CONTINUE_STMT: |
9596 | *jump_target = t; |
9597 | return true; |
9598 | |
9599 | case PARM_DECL: |
9600 | if (now && want_rval) |
9601 | { |
9602 | tree type = TREE_TYPE (t); |
9603 | if (dependent_type_p (type) |
9604 | || !COMPLETE_TYPE_P (processing_template_decl |
9605 | ? type : complete_type (type)) |
9606 | || is_really_empty_class (type, /*ignore_vptr*/false)) |
9607 | /* An empty class has no data to read. */ |
9608 | return true; |
9609 | if (flags & tf_error) |
9610 | constexpr_error (location: input_location, constexpr_fundef_p: fundef_p, |
9611 | gmsgid: "%qE is not a constant expression" , t); |
9612 | return false; |
9613 | } |
9614 | return true; |
9615 | |
9616 | case AGGR_INIT_EXPR: |
9617 | case CALL_EXPR: |
9618 | /* -- an invocation of a function other than a constexpr function |
9619 | or a constexpr constructor. */ |
9620 | { |
9621 | tree fun = get_function_named_in_call (t); |
9622 | const int nargs = call_expr_nargs (t); |
9623 | i = 0; |
9624 | |
9625 | 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 | bool bail = true; |
9630 | |
9631 | if (TREE_CODE (t) == CALL_EXPR |
9632 | && CALL_EXPR_FN (t) == NULL_TREE) |
9633 | 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 | if (flags & tf_error) |
9660 | constexpr_error (location: loc, constexpr_fundef_p: fundef_p, |
9661 | gmsgid: "call to internal function %qE" , t); |
9662 | return false; |
9663 | } |
9664 | } |
9665 | |
9666 | if (fun && is_overloaded_fn (fun)) |
9667 | { |
9668 | if (!RECUR (fun, true)) |
9669 | return false; |
9670 | fun = get_fns (fun); |
9671 | |
9672 | if (TREE_CODE (fun) == FUNCTION_DECL) |
9673 | { |
9674 | if (builtin_valid_in_constant_expr_p (fun)) |
9675 | return true; |
9676 | if (!maybe_constexpr_fn (t: fun) |
9677 | /* Allow any built-in function; if the expansion |
9678 | isn't constant, we'll deal with that then. */ |
9679 | && !fndecl_built_in_p (node: fun) |
9680 | /* In C++20, replaceable global allocation functions |
9681 | are constant expressions. */ |
9682 | && (!cxx_replaceable_global_alloc_fn (fndecl: fun) |
9683 | || TREE_CODE (t) != CALL_EXPR |
9684 | || (!CALL_FROM_NEW_OR_DELETE_P (t) |
9685 | && (current_function_decl == NULL_TREE |
9686 | || !is_std_allocator_allocate |
9687 | (fndecl: current_function_decl)))) |
9688 | /* Allow placement new in std::construct_at. */ |
9689 | && (!cxx_placement_new_fn (fndecl: fun) |
9690 | || TREE_CODE (t) != CALL_EXPR |
9691 | || current_function_decl == NULL_TREE |
9692 | || !is_std_construct_at (fndecl: current_function_decl)) |
9693 | && !cxx_dynamic_cast_fn_p (fndecl: fun)) |
9694 | { |
9695 | if ((flags & tf_error) |
9696 | && constexpr_error (location: loc, constexpr_fundef_p: fundef_p, |
9697 | gmsgid: "call to non-%<constexpr%> " |
9698 | "function %qD" , fun)) |
9699 | explain_invalid_constexpr_fn (fun); |
9700 | return false; |
9701 | } |
9702 | } |
9703 | |
9704 | fun = OVL_FIRST (fun); |
9705 | /* Skip initial arguments to base constructors. */ |
9706 | if (DECL_BASE_CONSTRUCTOR_P (fun)) |
9707 | i = num_artificial_parms_for (fun); |
9708 | } |
9709 | else if (fun) |
9710 | { |
9711 | if (TREE_TYPE (fun) |
9712 | && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun))) |
9713 | want_rval = rval; |
9714 | else |
9715 | want_rval = any; |
9716 | 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 | for (; i < nargs; ++i) |
9726 | { |
9727 | tree x = get_nth_callarg (t, n: 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 | 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 | bool sub_now = false; |
9735 | if (!potential_constant_expression_1 (t: x, want_rval: rv, strict, |
9736 | now: sub_now, fundef_p, flags, |
9737 | jump_target)) |
9738 | return false; |
9739 | } |
9740 | return true; |
9741 | } |
9742 | |
9743 | 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 | return RECUR (TREE_OPERAND (t, 0), rval); |
9753 | |
9754 | case EXCESS_PRECISION_EXPR: |
9755 | return RECUR (TREE_OPERAND (t, 0), rval); |
9756 | |
9757 | case VAR_DECL: |
9758 | if (DECL_HAS_VALUE_EXPR_P (t)) |
9759 | { |
9760 | 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 | 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 | return RECUR (DECL_CAPTURED_VARIABLE (t), rval); |
9771 | |
9772 | if (flags & tf_error) |
9773 | { |
9774 | tree cap = DECL_CAPTURED_VARIABLE (t); |
9775 | auto_diagnostic_group d; |
9776 | if (constexpr_error (location: input_location, constexpr_fundef_p: fundef_p, |
9777 | gmsgid: "lambda capture of %qE is not a " |
9778 | "constant expression" , cap) |
9779 | && decl_constant_var_p (cap)) |
9780 | inform (input_location, "because it is used as a glvalue" ); |
9781 | } |
9782 | return false; |
9783 | } |
9784 | /* Treat __PRETTY_FUNCTION__ inside a template function as |
9785 | potentially-constant. */ |
9786 | else if (DECL_PRETTY_FUNCTION_P (t) |
9787 | && DECL_VALUE_EXPR (t) == error_mark_node) |
9788 | return true; |
9789 | return RECUR (DECL_VALUE_EXPR (t), rval); |
9790 | } |
9791 | if (want_rval |
9792 | && (now || !var_in_maybe_constexpr_fn (t)) |
9793 | && !type_dependent_expression_p (t) |
9794 | && !decl_maybe_constant_var_p (t) |
9795 | && (strict |
9796 | || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t)) |
9797 | || (DECL_INITIAL (t) |
9798 | && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))) |
9799 | && COMPLETE_TYPE_P (TREE_TYPE (t)) |
9800 | && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
9801 | { |
9802 | if (flags & tf_error) |
9803 | non_const_var_error (loc, r: t, fundef_p); |
9804 | return false; |
9805 | } |
9806 | return true; |
9807 | |
9808 | case NOP_EXPR: |
9809 | if (REINTERPRET_CAST_P (t)) |
9810 | { |
9811 | if (flags & tf_error) |
9812 | constexpr_error (location: loc, constexpr_fundef_p: fundef_p, gmsgid: "%<reinterpret_cast%> is not a " |
9813 | "constant expression" ); |
9814 | return false; |
9815 | } |
9816 | /* FALLTHRU */ |
9817 | case CONVERT_EXPR: |
9818 | 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 | { |
9822 | tree from = TREE_OPERAND (t, 0); |
9823 | if (location_wrapper_p (exp: t)) |
9824 | { |
9825 | iloc_sentinel ils = loc; |
9826 | return (RECUR (from, want_rval)); |
9827 | } |
9828 | if (INDIRECT_TYPE_P (TREE_TYPE (t))) |
9829 | { |
9830 | STRIP_ANY_LOCATION_WRAPPER (from); |
9831 | if (TREE_CODE (from) == INTEGER_CST |
9832 | && !integer_zerop (from)) |
9833 | { |
9834 | if (flags & tf_error) |
9835 | constexpr_error (location: loc, constexpr_fundef_p: fundef_p, |
9836 | gmsgid: "%<reinterpret_cast%> from integer to " |
9837 | "pointer" ); |
9838 | return false; |
9839 | } |
9840 | } |
9841 | return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR)); |
9842 | } |
9843 | |
9844 | case ADDRESSOF_EXPR: |
9845 | /* This is like ADDR_EXPR, except it won't form pointer-to-member. */ |
9846 | t = TREE_OPERAND (t, 0); |
9847 | goto handle_addr_expr; |
9848 | |
9849 | 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 | t = TREE_OPERAND (t, 0); |
9854 | |
9855 | if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t)) |
9856 | /* A pointer-to-member constant. */ |
9857 | return true; |
9858 | |
9859 | 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 | return RECUR (t, any); |
9876 | |
9877 | case COMPONENT_REF: |
9878 | case ARROW_EXPR: |
9879 | 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 | if (type_unknown_p (expr: t)) |
9885 | return true; |
9886 | 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 | want_rval = false; |
9890 | gcc_fallthrough (); |
9891 | |
9892 | case REALPART_EXPR: |
9893 | case IMAGPART_EXPR: |
9894 | case BIT_FIELD_REF: |
9895 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
9896 | |
9897 | case EXPR_PACK_EXPANSION: |
9898 | return RECUR (PACK_EXPANSION_PATTERN (t), want_rval); |
9899 | |
9900 | case INDIRECT_REF: |
9901 | { |
9902 | tree x = TREE_OPERAND (t, 0); |
9903 | STRIP_NOPS (x); |
9904 | if (is_this_parameter (x) && !is_capture_proxy (x)) |
9905 | { |
9906 | if (now || !var_in_maybe_constexpr_fn (t: x)) |
9907 | { |
9908 | if (flags & tf_error) |
9909 | constexpr_error (location: loc, constexpr_fundef_p: fundef_p, gmsgid: "use of %<this%> in a " |
9910 | "constant expression" ); |
9911 | return false; |
9912 | } |
9913 | return true; |
9914 | } |
9915 | return RECUR (x, rval); |
9916 | } |
9917 | |
9918 | case STATEMENT_LIST: |
9919 | for (tree stmt : tsi_range (t)) |
9920 | if (!RECUR (stmt, any)) |
9921 | return false; |
9922 | return true; |
9923 | |
9924 | case MODIFY_EXPR: |
9925 | if (cxx_dialect < cxx14) |
9926 | goto fail; |
9927 | if (!RECUR (TREE_OPERAND (t, 0), any)) |
9928 | return false; |
9929 | /* Just ignore clobbers. */ |
9930 | if (TREE_CLOBBER_P (TREE_OPERAND (t, 1))) |
9931 | return true; |
9932 | if (!RECUR (TREE_OPERAND (t, 1), rval)) |
9933 | return false; |
9934 | return true; |
9935 | |
9936 | case MODOP_EXPR: |
9937 | if (cxx_dialect < cxx14) |
9938 | goto fail; |
9939 | if (!RECUR (TREE_OPERAND (t, 0), rval)) |
9940 | return false; |
9941 | if (!RECUR (TREE_OPERAND (t, 2), rval)) |
9942 | return false; |
9943 | return true; |
9944 | |
9945 | case DO_STMT: |
9946 | if (!RECUR (DO_COND (t), rval)) |
9947 | return false; |
9948 | if (!RECUR (DO_BODY (t), any)) |
9949 | return false; |
9950 | if (breaks (jump_target) || continues (jump_target)) |
9951 | *jump_target = NULL_TREE; |
9952 | return true; |
9953 | |
9954 | case FOR_STMT: |
9955 | if (!RECUR (FOR_INIT_STMT (t), any)) |
9956 | return false; |
9957 | tmp = FOR_COND (t); |
9958 | if (!RECUR (tmp, rval)) |
9959 | return false; |
9960 | if (tmp) |
9961 | { |
9962 | if (!processing_template_decl) |
9963 | tmp = cxx_eval_outermost_constant_expr (t: tmp, allow_non_constant: true); |
9964 | /* If we couldn't evaluate the condition, it might not ever be |
9965 | true. */ |
9966 | if (!integer_onep (tmp)) |
9967 | { |
9968 | /* Before returning true, check if the for body can contain |
9969 | a return. */ |
9970 | hash_set<tree> pset; |
9971 | check_for_return_continue_data data = { .pset: &pset, NULL_TREE, |
9972 | NULL_TREE }; |
9973 | if (tree ret_expr |
9974 | = cp_walk_tree (&FOR_BODY (t), check_for_return_continue, |
9975 | &data, &pset)) |
9976 | *jump_target = ret_expr; |
9977 | return true; |
9978 | } |
9979 | } |
9980 | if (!RECUR (FOR_EXPR (t), any)) |
9981 | return false; |
9982 | if (!RECUR (FOR_BODY (t), any)) |
9983 | return false; |
9984 | if (breaks (jump_target) || continues (jump_target)) |
9985 | *jump_target = NULL_TREE; |
9986 | return true; |
9987 | |
9988 | case RANGE_FOR_STMT: |
9989 | if (!RECUR (RANGE_FOR_INIT_STMT (t), any)) |
9990 | return false; |
9991 | if (!RECUR (RANGE_FOR_EXPR (t), any)) |
9992 | return false; |
9993 | if (!RECUR (RANGE_FOR_BODY (t), any)) |
9994 | return false; |
9995 | if (breaks (jump_target) || continues (jump_target)) |
9996 | *jump_target = NULL_TREE; |
9997 | return true; |
9998 | |
9999 | case WHILE_STMT: |
10000 | tmp = WHILE_COND (t); |
10001 | if (!RECUR (tmp, rval)) |
10002 | return false; |
10003 | if (!processing_template_decl) |
10004 | tmp = cxx_eval_outermost_constant_expr (t: tmp, allow_non_constant: true); |
10005 | /* If we couldn't evaluate the condition, it might not ever be true. */ |
10006 | if (!integer_onep (tmp)) |
10007 | { |
10008 | /* Before returning true, check if the while body can contain |
10009 | a return. */ |
10010 | hash_set<tree> pset; |
10011 | check_for_return_continue_data data = { .pset: &pset, NULL_TREE, |
10012 | NULL_TREE }; |
10013 | if (tree ret_expr |
10014 | = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue, |
10015 | &data, &pset)) |
10016 | *jump_target = ret_expr; |
10017 | return true; |
10018 | } |
10019 | if (!RECUR (WHILE_BODY (t), any)) |
10020 | return false; |
10021 | if (breaks (jump_target) || continues (jump_target)) |
10022 | *jump_target = NULL_TREE; |
10023 | return true; |
10024 | |
10025 | case SWITCH_STMT: |
10026 | 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 | hash_set<tree> pset; |
10037 | check_for_return_continue_data data = { .pset: &pset, NULL_TREE, |
10038 | NULL_TREE }; |
10039 | if (tree ret_expr |
10040 | = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue, |
10041 | &data, &pset)) |
10042 | /* The switch might return. */ |
10043 | *jump_target = ret_expr; |
10044 | else if (data.continue_stmt) |
10045 | /* The switch can't return, but might continue. */ |
10046 | *jump_target = data.continue_stmt; |
10047 | } |
10048 | return true; |
10049 | |
10050 | case STMT_EXPR: |
10051 | return RECUR (STMT_EXPR_STMT (t), rval); |
10052 | |
10053 | case LAMBDA_EXPR: |
10054 | if (cxx_dialect >= cxx17) |
10055 | /* In C++17 lambdas can be constexpr, don't give up yet. */ |
10056 | return true; |
10057 | else if (flags & tf_error) |
10058 | constexpr_error (location: loc, constexpr_fundef_p: fundef_p, gmsgid: "lambda-expression is not a " |
10059 | "constant expression before C++17" ); |
10060 | return false; |
10061 | |
10062 | case NEW_EXPR: |
10063 | case VEC_NEW_EXPR: |
10064 | case DELETE_EXPR: |
10065 | case VEC_DELETE_EXPR: |
10066 | if (cxx_dialect >= cxx20) |
10067 | /* In C++20, new-expressions are potentially constant. */ |
10068 | return true; |
10069 | else if (flags & tf_error) |
10070 | constexpr_error (location: loc, constexpr_fundef_p: fundef_p, gmsgid: "new-expression is not a " |
10071 | "constant expression before C++20" ); |
10072 | return false; |
10073 | |
10074 | case DYNAMIC_CAST_EXPR: |
10075 | case PSEUDO_DTOR_EXPR: |
10076 | case THROW_EXPR: |
10077 | case OMP_PARALLEL: |
10078 | case OMP_TASK: |
10079 | case OMP_FOR: |
10080 | case OMP_SIMD: |
10081 | case OMP_DISTRIBUTE: |
10082 | case OMP_TASKLOOP: |
10083 | case OMP_LOOP: |
10084 | case OMP_TEAMS: |
10085 | case OMP_TARGET_DATA: |
10086 | case OMP_TARGET: |
10087 | case OMP_SECTIONS: |
10088 | case OMP_ORDERED: |
10089 | case OMP_CRITICAL: |
10090 | case OMP_SINGLE: |
10091 | case OMP_SCAN: |
10092 | case OMP_SCOPE: |
10093 | case OMP_SECTION: |
10094 | case OMP_MASTER: |
10095 | case OMP_MASKED: |
10096 | case OMP_TASKGROUP: |
10097 | case OMP_TARGET_UPDATE: |
10098 | case OMP_TARGET_ENTER_DATA: |
10099 | case OMP_TARGET_EXIT_DATA: |
10100 | case OMP_ATOMIC: |
10101 | case OMP_ATOMIC_READ: |
10102 | case OMP_ATOMIC_CAPTURE_OLD: |
10103 | case OMP_ATOMIC_CAPTURE_NEW: |
10104 | case OMP_DEPOBJ: |
10105 | case OACC_PARALLEL: |
10106 | case OACC_KERNELS: |
10107 | case OACC_SERIAL: |
10108 | case OACC_DATA: |
10109 | case OACC_HOST_DATA: |
10110 | case OACC_LOOP: |
10111 | case OACC_CACHE: |
10112 | case OACC_DECLARE: |
10113 | case OACC_ENTER_DATA: |
10114 | case OACC_EXIT_DATA: |
10115 | case OACC_UPDATE: |
10116 | case OMP_ARRAY_SECTION: |
10117 | /* GCC internal stuff. */ |
10118 | case VA_ARG_EXPR: |
10119 | case TRANSACTION_EXPR: |
10120 | case AT_ENCODE_EXPR: |
10121 | fail: |
10122 | if (flags & tf_error) |
10123 | constexpr_error (location: loc, constexpr_fundef_p: fundef_p, gmsgid: "expression %qE is not a constant " |
10124 | "expression" , t); |
10125 | return false; |
10126 | |
10127 | case ASM_EXPR: |
10128 | if (flags & tf_error) |
10129 | inline_asm_in_constexpr_error (loc, fundef_p); |
10130 | return false; |
10131 | |
10132 | case OBJ_TYPE_REF: |
10133 | if (cxx_dialect >= cxx20) |
10134 | /* In C++20 virtual calls can be constexpr, don't give up yet. */ |
10135 | return true; |
10136 | else if (flags & tf_error) |
10137 | constexpr_error (location: loc, constexpr_fundef_p: fundef_p, gmsgid: "virtual functions cannot be " |
10138 | "%<constexpr%> before C++20" ); |
10139 | return false; |
10140 | |
10141 | case TYPEID_EXPR: |
10142 | /* In C++20, a typeid expression whose operand is of polymorphic |
10143 | class type can be constexpr. */ |
10144 | { |
10145 | tree e = TREE_OPERAND (t, 0); |
10146 | if (cxx_dialect < cxx20 |
10147 | && strict |
10148 | && !TYPE_P (e) |
10149 | && !type_dependent_expression_p (e) |
10150 | && TYPE_POLYMORPHIC_P (TREE_TYPE (e))) |
10151 | { |
10152 | if (flags & tf_error) |
10153 | constexpr_error (location: loc, constexpr_fundef_p: fundef_p, gmsgid: "%<typeid%> is not a " |
10154 | "constant expression because %qE is " |
10155 | "of polymorphic type" , e); |
10156 | return false; |
10157 | } |
10158 | return true; |
10159 | } |
10160 | |
10161 | case POINTER_DIFF_EXPR: |
10162 | case MINUS_EXPR: |
10163 | want_rval = true; |
10164 | goto binary; |
10165 | |
10166 | case LT_EXPR: |
10167 | case LE_EXPR: |
10168 | case GT_EXPR: |
10169 | case GE_EXPR: |
10170 | case EQ_EXPR: |
10171 | case NE_EXPR: |
10172 | case SPACESHIP_EXPR: |
10173 | want_rval = true; |
10174 | goto binary; |
10175 | |
10176 | case PREINCREMENT_EXPR: |
10177 | case POSTINCREMENT_EXPR: |
10178 | case PREDECREMENT_EXPR: |
10179 | case POSTDECREMENT_EXPR: |
10180 | if (cxx_dialect < cxx14) |
10181 | goto fail; |
10182 | goto unary; |
10183 | |
10184 | case BIT_NOT_EXPR: |
10185 | /* A destructor. */ |
10186 | if (TYPE_P (TREE_OPERAND (t, 0))) |
10187 | return true; |
10188 | /* fall through. */ |
10189 | |
10190 | case CONJ_EXPR: |
10191 | case SAVE_EXPR: |
10192 | case FIX_TRUNC_EXPR: |
10193 | case FLOAT_EXPR: |
10194 | case NEGATE_EXPR: |
10195 | case ABS_EXPR: |
10196 | case ABSU_EXPR: |
10197 | case TRUTH_NOT_EXPR: |
10198 | case FIXED_CONVERT_EXPR: |
10199 | case UNARY_PLUS_EXPR: |
10200 | case UNARY_LEFT_FOLD_EXPR: |
10201 | case UNARY_RIGHT_FOLD_EXPR: |
10202 | unary: |
10203 | return RECUR (TREE_OPERAND (t, 0), rval); |
10204 | |
10205 | case CAST_EXPR: |
10206 | case CONST_CAST_EXPR: |
10207 | case STATIC_CAST_EXPR: |
10208 | case REINTERPRET_CAST_EXPR: |
10209 | case IMPLICIT_CONV_EXPR: |
10210 | 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 | if (flags & tf_error) |
10215 | constexpr_error (location: loc, constexpr_fundef_p: fundef_p, |
10216 | gmsgid: "cast to non-integral type %qT in a constant " |
10217 | "expression" , TREE_TYPE (t)); |
10218 | 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 | else if (cxx_dialect >= cxx11 |
10224 | && (dependent_type_p (TREE_TYPE (t)) |
10225 | || !COMPLETE_TYPE_P (TREE_TYPE (t)) |
10226 | || literal_type_p (TREE_TYPE (t))) |
10227 | && TREE_OPERAND (t, 0)) |
10228 | { |
10229 | 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 | if (type == NULL_TREE || WILDCARD_TYPE_P (type)) |
10233 | return true; |
10234 | /* Or a non-dependent class which has conversions. */ |
10235 | else if (CLASS_TYPE_P (type) |
10236 | && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type))) |
10237 | return true; |
10238 | } |
10239 | |
10240 | return (RECUR (TREE_OPERAND (t, 0), |
10241 | !TYPE_REF_P (TREE_TYPE (t)))); |
10242 | |
10243 | case BIND_EXPR: |
10244 | return RECUR (BIND_EXPR_BODY (t), want_rval); |
10245 | |
10246 | case CLEANUP_POINT_EXPR: |
10247 | case MUST_NOT_THROW_EXPR: |
10248 | case TRY_CATCH_EXPR: |
10249 | case TRY_BLOCK: |
10250 | case EH_SPEC_BLOCK: |
10251 | case EXPR_STMT: |
10252 | case PAREN_EXPR: |
10253 | /* For convenience. */ |
10254 | case LOOP_EXPR: |
10255 | case EXIT_EXPR: |
10256 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
10257 | |
10258 | case DECL_EXPR: |
10259 | tmp = DECL_EXPR_DECL (t); |
10260 | if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp) |
10261 | && (processing_template_decl |
10262 | ? !decl_maybe_constant_var_p (tmp) |
10263 | : !decl_constant_var_p (tmp))) |
10264 | { |
10265 | if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp)) |
10266 | { |
10267 | if (flags & tf_error) |
10268 | constexpr_error (DECL_SOURCE_LOCATION (tmp), constexpr_fundef_p: fundef_p, |
10269 | gmsgid: "%qD defined %<thread_local%> in " |
10270 | "%<constexpr%> context" , tmp); |
10271 | return false; |
10272 | } |
10273 | else if (TREE_STATIC (tmp)) |
10274 | { |
10275 | if (flags & tf_error) |
10276 | constexpr_error (DECL_SOURCE_LOCATION (tmp), constexpr_fundef_p: fundef_p, |
10277 | gmsgid: "%qD defined %<static%> in %<constexpr%> " |
10278 | "context" , tmp); |
10279 | return false; |
10280 | } |
10281 | else if (!check_for_uninitialized_const_var |
10282 | (tmp, /*constexpr_context_p=*/true, flags)) |
10283 | return false; |
10284 | } |
10285 | if (VAR_P (tmp)) |
10286 | return RECUR (DECL_INITIAL (tmp), want_rval); |
10287 | return true; |
10288 | |
10289 | case TRY_FINALLY_EXPR: |
10290 | return (RECUR (TREE_OPERAND (t, 0), want_rval) |
10291 | && RECUR (TREE_OPERAND (t, 1), any)); |
10292 | |
10293 | case SCOPE_REF: |
10294 | return RECUR (TREE_OPERAND (t, 1), want_rval); |
10295 | |
10296 | case TARGET_EXPR: |
10297 | if (!TARGET_EXPR_DIRECT_INIT_P (t) |
10298 | && !TARGET_EXPR_ELIDING_P (t) |
10299 | && !literal_type_p (TREE_TYPE (t))) |
10300 | { |
10301 | if (flags & tf_error) |
10302 | { |
10303 | auto_diagnostic_group d; |
10304 | if (constexpr_error (location: loc, constexpr_fundef_p: fundef_p, |
10305 | gmsgid: "temporary of non-literal type %qT in a " |
10306 | "constant expression" , TREE_TYPE (t))) |
10307 | explain_non_literal_class (TREE_TYPE (t)); |
10308 | } |
10309 | return false; |
10310 | } |
10311 | /* FALLTHRU */ |
10312 | case INIT_EXPR: |
10313 | return RECUR (TREE_OPERAND (t, 1), rval); |
10314 | |
10315 | case CONSTRUCTOR: |
10316 | { |
10317 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); |
10318 | constructor_elt *ce; |
10319 | for (i = 0; vec_safe_iterate (v, ix: i, ptr: &ce); ++i) |
10320 | if (!RECUR (ce->value, want_rval)) |
10321 | return false; |
10322 | return true; |
10323 | } |
10324 | |
10325 | case TREE_LIST: |
10326 | { |
10327 | gcc_assert (TREE_PURPOSE (t) == NULL_TREE |
10328 | || DECL_P (TREE_PURPOSE (t))); |
10329 | if (!RECUR (TREE_VALUE (t), want_rval)) |
10330 | return false; |
10331 | if (TREE_CHAIN (t) == NULL_TREE) |
10332 | return true; |
10333 | return RECUR (TREE_CHAIN (t), want_rval); |
10334 | } |
10335 | |
10336 | case TRUNC_DIV_EXPR: |
10337 | case CEIL_DIV_EXPR: |
10338 | case FLOOR_DIV_EXPR: |
10339 | case ROUND_DIV_EXPR: |
10340 | case TRUNC_MOD_EXPR: |
10341 | case CEIL_MOD_EXPR: |
10342 | case ROUND_MOD_EXPR: |
10343 | { |
10344 | tree denom = TREE_OPERAND (t, 1); |
10345 | 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 | if (!processing_template_decl) |
10350 | denom = cxx_eval_outermost_constant_expr (t: denom, allow_non_constant: true); |
10351 | if (integer_zerop (denom)) |
10352 | { |
10353 | if (flags & tf_error) |
10354 | constexpr_error (location: input_location, constexpr_fundef_p: fundef_p, |
10355 | gmsgid: "division by zero is not a constant expression" ); |
10356 | return false; |
10357 | } |
10358 | else |
10359 | { |
10360 | want_rval = true; |
10361 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
10362 | } |
10363 | } |
10364 | |
10365 | case COMPOUND_EXPR: |
10366 | { |
10367 | /* check_return_expr sometimes wraps a TARGET_EXPR in a |
10368 | COMPOUND_EXPR; don't get confused. */ |
10369 | tree op0 = TREE_OPERAND (t, 0); |
10370 | tree op1 = TREE_OPERAND (t, 1); |
10371 | STRIP_NOPS (op1); |
10372 | if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) |
10373 | return RECUR (op0, want_rval); |
10374 | else |
10375 | 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 | case TRUTH_AND_EXPR: |
10382 | case TRUTH_ANDIF_EXPR: |
10383 | tmp = boolean_true_node; |
10384 | goto truth; |
10385 | case TRUTH_OR_EXPR: |
10386 | case TRUTH_ORIF_EXPR: |
10387 | tmp = boolean_false_node; |
10388 | truth: |
10389 | { |
10390 | tree op0 = TREE_OPERAND (t, 0); |
10391 | tree op1 = TREE_OPERAND (t, 1); |
10392 | if (!RECUR (op0, rval)) |
10393 | return false; |
10394 | 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 | if (!processing_template_decl) |
10399 | op0 = cxx_eval_outermost_constant_expr (t: op0, allow_non_constant: true); |
10400 | if (tree_int_cst_equal (op0, tmp)) |
10401 | 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 | want_rval = true; |
10432 | /* Fall through. */ |
10433 | case ARRAY_REF: |
10434 | case ARRAY_RANGE_REF: |
10435 | case MEMBER_REF: |
10436 | case DOTSTAR_EXPR: |
10437 | case MEM_REF: |
10438 | case BINARY_LEFT_FOLD_EXPR: |
10439 | case BINARY_RIGHT_FOLD_EXPR: |
10440 | binary: |
10441 | for (i = 0; i < 2; ++i) |
10442 | if (!RECUR (TREE_OPERAND (t, i), want_rval)) |
10443 | return false; |
10444 | return true; |
10445 | |
10446 | case VEC_PERM_EXPR: |
10447 | for (i = 0; i < 3; ++i) |
10448 | if (!RECUR (TREE_OPERAND (t, i), true)) |
10449 | return false; |
10450 | return true; |
10451 | |
10452 | case COND_EXPR: |
10453 | if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20) |
10454 | { |
10455 | if (flags & tf_error) |
10456 | constexpr_error (location: loc, constexpr_fundef_p: fundef_p, gmsgid: "%<delete[]%> is not a " |
10457 | "constant expression" ); |
10458 | return false; |
10459 | } |
10460 | /* Fall through. */ |
10461 | case IF_STMT: |
10462 | 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 | tmp = TREE_OPERAND (t, 0); |
10467 | if (!RECUR (tmp, rval)) |
10468 | return false; |
10469 | if (!processing_template_decl) |
10470 | tmp = cxx_eval_outermost_constant_expr (t: tmp, allow_non_constant: 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 | if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t)) |
10476 | { |
10477 | if (integer_zerop (tmp)) |
10478 | return RECUR (TREE_OPERAND (t, 2), want_rval); |
10479 | else if (TREE_CODE (tmp) == INTEGER_CST) |
10480 | return RECUR (TREE_OPERAND (t, 1), want_rval); |
10481 | } |
10482 | tmp = *jump_target; |
10483 | for (i = 1; i < 3; ++i) |
10484 | { |
10485 | tree this_jump_target = tmp; |
10486 | if (potential_constant_expression_1 (TREE_OPERAND (t, i), |
10487 | want_rval, strict, now, fundef_p, |
10488 | flags: tf_none, jump_target: &this_jump_target)) |
10489 | { |
10490 | if (returns (jump_target: &this_jump_target)) |
10491 | *jump_target = this_jump_target; |
10492 | else if (!returns (jump_target)) |
10493 | { |
10494 | if (breaks (jump_target: &this_jump_target) |
10495 | || continues (jump_target: &this_jump_target)) |
10496 | *jump_target = this_jump_target; |
10497 | 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 | hash_set<tree> pset; |
10503 | check_for_return_continue_data data = { .pset: &pset, NULL_TREE, |
10504 | NULL_TREE }; |
10505 | if (tree ret_expr |
10506 | = cp_walk_tree (&TREE_OPERAND (t, 2), |
10507 | check_for_return_continue, &data, |
10508 | &pset)) |
10509 | *jump_target = ret_expr; |
10510 | else if (*jump_target == NULL_TREE) |
10511 | { |
10512 | if (data.continue_stmt) |
10513 | *jump_target = data.continue_stmt; |
10514 | else if (data.break_stmt) |
10515 | *jump_target = data.break_stmt; |
10516 | } |
10517 | } |
10518 | } |
10519 | return true; |
10520 | } |
10521 | } |
10522 | if (flags & tf_error) |
10523 | { |
10524 | if (TREE_CODE (t) == IF_STMT) |
10525 | constexpr_error (location: loc, constexpr_fundef_p: fundef_p, gmsgid: "neither branch of %<if%> is a " |
10526 | "constant expression" ); |
10527 | else |
10528 | constexpr_error (location: loc, constexpr_fundef_p: fundef_p, gmsgid: "expression %qE is not a " |
10529 | "constant expression" , t); |
10530 | } |
10531 | return false; |
10532 | |
10533 | case VEC_INIT_EXPR: |
10534 | if (VEC_INIT_EXPR_IS_CONSTEXPR (t)) |
10535 | return true; |
10536 | if (flags & tf_error) |
10537 | { |
10538 | if (constexpr_error (location: loc, constexpr_fundef_p: fundef_p, gmsgid: "non-constant array " |
10539 | "initialization" )) |
10540 | 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 | case CLEANUP_STMT: |
10550 | if (!RECUR (CLEANUP_BODY (t), any)) |
10551 | return false; |
10552 | 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 | case GOTO_EXPR: |
10560 | { |
10561 | tree *target = &TREE_OPERAND (t, 0); |
10562 | /* Gotos representing break, continue and cdtor return are OK. */ |
10563 | if (breaks (jump_target: target) || continues (jump_target: target) || returns (jump_target: target)) |
10564 | { |
10565 | *jump_target = *target; |
10566 | return true; |
10567 | } |
10568 | if (flags & tf_error) |
10569 | constexpr_error (location: loc, constexpr_fundef_p: fundef_p, gmsgid: "%<goto%> is not a constant " |
10570 | "expression" ); |
10571 | return false; |
10572 | } |
10573 | |
10574 | case ASSERTION_STMT: |
10575 | case PRECONDITION_STMT: |
10576 | case POSTCONDITION_STMT: |
10577 | if (!checked_contract_p (cs: get_contract_semantic (t))) |
10578 | return true; |
10579 | return RECUR (CONTRACT_CONDITION (t), rval); |
10580 | |
10581 | case LABEL_EXPR: |
10582 | t = LABEL_EXPR_LABEL (t); |
10583 | if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23) |
10584 | return true; |
10585 | else if (flags & tf_error) |
10586 | constexpr_error (location: loc, constexpr_fundef_p: fundef_p, gmsgid: "label definition in %<constexpr%> " |
10587 | "function only available with %<-std=c++2b%> or " |
10588 | "%<-std=gnu++2b%>" ); |
10589 | return false; |
10590 | |
10591 | case ANNOTATE_EXPR: |
10592 | return RECUR (TREE_OPERAND (t, 0), rval); |
10593 | |
10594 | case BIT_CAST_EXPR: |
10595 | 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 | case NONTYPE_ARGUMENT_PACK: |
10604 | { |
10605 | tree args = ARGUMENT_PACK_ARGS (t); |
10606 | int len = TREE_VEC_LENGTH (args); |
10607 | for (int i = 0; i < len; ++i) |
10608 | if (!RECUR (TREE_VEC_ELT (args, i), any)) |
10609 | return false; |
10610 | return true; |
10611 | } |
10612 | |
10613 | default: |
10614 | if (objc_non_constant_expr_p (t)) |
10615 | return false; |
10616 | |
10617 | sorry ("unexpected AST of kind %s" , get_tree_code_name (TREE_CODE (t))); |
10618 | gcc_unreachable (); |
10619 | return false; |
10620 | } |
10621 | #undef RECUR |
10622 | } |
10623 | |
10624 | bool |
10625 | potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, |
10626 | bool fundef_p, tsubst_flags_t flags) |
10627 | { |
10628 | 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 | flags &= ~tf_error; |
10634 | if (potential_constant_expression_1 (t, want_rval, strict, now, fundef_p, |
10635 | flags)) |
10636 | return true; |
10637 | flags |= tf_error; |
10638 | } |
10639 | |
10640 | tree target = NULL_TREE; |
10641 | return potential_constant_expression_1 (t, want_rval, strict, now, fundef_p, |
10642 | flags, jump_target: &target); |
10643 | } |
10644 | |
10645 | /* The main entry point to the above. */ |
10646 | |
10647 | bool |
10648 | potential_constant_expression (tree t) |
10649 | { |
10650 | return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true, |
10651 | /*now*/false, /*fundef_p*/false, |
10652 | flags: tf_none); |
10653 | } |
10654 | |
10655 | /* As above, but require a constant rvalue. */ |
10656 | |
10657 | bool |
10658 | potential_rvalue_constant_expression (tree t) |
10659 | { |
10660 | return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true, |
10661 | /*now*/false, /*fundef_p*/false, |
10662 | flags: tf_none); |
10663 | } |
10664 | |
10665 | /* Like above, but complain about non-constant expressions. */ |
10666 | |
10667 | bool |
10668 | require_potential_constant_expression (tree t) |
10669 | { |
10670 | return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true, |
10671 | /*now*/false, /*fundef_p*/false, |
10672 | flags: tf_warning_or_error); |
10673 | } |
10674 | |
10675 | /* Cross product of the above. */ |
10676 | |
10677 | bool |
10678 | require_potential_rvalue_constant_expression (tree t) |
10679 | { |
10680 | return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true, |
10681 | /*now*/false, /*fundef_p*/false, |
10682 | flags: tf_warning_or_error); |
10683 | } |
10684 | |
10685 | /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */ |
10686 | |
10687 | bool |
10688 | require_potential_rvalue_constant_expression_fncheck (tree t) |
10689 | { |
10690 | return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true, |
10691 | /*now*/false, /*fundef_p*/true, |
10692 | flags: tf_warning_or_error); |
10693 | } |
10694 | |
10695 | /* Like above, but don't consider PARM_DECL a potential_constant_expression. */ |
10696 | |
10697 | bool |
10698 | require_rvalue_constant_expression (tree t) |
10699 | { |
10700 | return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true, |
10701 | /*now*/true, /*fundef_p*/false, |
10702 | flags: 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 | is_constant_expression (tree t) |
10714 | { |
10715 | return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true, |
10716 | /*now*/true, /*fundef_p*/false, |
10717 | flags: tf_none); |
10718 | } |
10719 | |
10720 | /* As above, but expect an rvalue. */ |
10721 | |
10722 | bool |
10723 | is_rvalue_constant_expression (tree t) |
10724 | { |
10725 | return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true, |
10726 | /*now*/true, /*fundef_p*/false, |
10727 | flags: tf_none); |
10728 | } |
10729 | |
10730 | /* Like above, but complain about non-constant expressions. */ |
10731 | |
10732 | bool |
10733 | require_constant_expression (tree t) |
10734 | { |
10735 | return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true, |
10736 | /*now*/true, /*fundef_p*/false, |
10737 | flags: 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 | is_static_init_expression (tree t) |
10745 | { |
10746 | return potential_constant_expression_1 (t, /*want_rval*/false, |
10747 | /*strict*/false, /*now*/true, |
10748 | /*fundef_p*/false, flags: 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 | is_nondependent_constant_expression (tree t) |
10757 | { |
10758 | return (!type_unknown_p (expr: t) |
10759 | && is_constant_expression (t) |
10760 | && !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 | is_nondependent_static_init_expression (tree t) |
10768 | { |
10769 | return (!type_unknown_p (expr: t) |
10770 | && is_static_init_expression (t) |
10771 | && !instantiation_dependent_expression_p (t)); |
10772 | } |
10773 | |
10774 | /* True iff FN is an implicitly constexpr function. */ |
10775 | |
10776 | bool |
10777 | decl_implicit_constexpr_p (tree fn) |
10778 | { |
10779 | if (!(flag_implicit_constexpr |
10780 | && TREE_CODE (fn) == FUNCTION_DECL |
10781 | && DECL_DECLARED_CONSTEXPR_P (fn))) |
10782 | return false; |
10783 | |
10784 | if (DECL_CLONED_FUNCTION_P (fn)) |
10785 | fn = DECL_CLONED_FUNCTION (fn); |
10786 | |
10787 | return (DECL_LANG_SPECIFIC (fn) |
10788 | && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr); |
10789 | } |
10790 | |
10791 | /* Finalize constexpr processing after parsing. */ |
10792 | |
10793 | void |
10794 | fini_constexpr (void) |
10795 | { |
10796 | /* The contexpr call and fundef copies tables are no longer needed. */ |
10797 | constexpr_call_table = NULL; |
10798 | fundef_copies_table = NULL; |
10799 | } |
10800 | |
10801 | #include "gt-cp-constexpr.h" |
10802 | |