]>
Commit | Line | Data |
---|---|---|
13f649f6 JM |
1 | /* Perform -*- C++ -*- constant expression evaluation, including calls to |
2 | constexpr functions. These routines are used both during actual parsing | |
2d76680f PC |
3 | and during the instantiation of template functions. |
4 | ||
a945c346 | 5 | Copyright (C) 1998-2024 Free Software Foundation, Inc. |
2d76680f PC |
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" | |
2d76680f | 26 | #include "cp-tree.h" |
2adfab87 | 27 | #include "varasm.h" |
2d76680f PC |
28 | #include "c-family/c-objc.h" |
29 | #include "tree-iterator.h" | |
30 | #include "gimplify.h" | |
31 | #include "builtins.h" | |
60813a46 | 32 | #include "tree-inline.h" |
0b274c17 | 33 | #include "ubsan.h" |
8108ea30 | 34 | #include "timevar.h" |
d8fcab68 | 35 | #include "fold-const-call.h" |
8e007055 | 36 | #include "stor-layout.h" |
a8db7887 | 37 | #include "cgraph.h" |
00f34291 | 38 | #include "opts.h" |
87c2080b JM |
39 | #include "stringpool.h" |
40 | #include "attribs.h" | |
08b51bad | 41 | #include "fold-const.h" |
2efb237f | 42 | #include "intl.h" |
9d5730de | 43 | #include "toplev.h" |
2d76680f PC |
44 | |
45 | static bool verify_constant (tree, bool, bool *, bool *); | |
46 | #define VERIFY_CONSTANT(X) \ | |
47 | do { \ | |
2b3ab879 | 48 | if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \ |
2d76680f PC |
49 | return t; \ |
50 | } while (0) | |
51 | ||
582d2481 JJ |
52 | static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex, |
53 | bool insert = false); | |
baf05d54 | 54 | static int array_index_cmp (tree key, tree index); |
582d2481 | 55 | |
2d76680f PC |
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) | |
b55b02ea | 74 | || VECTOR_TYPE_P (t) |
9f613f06 | 75 | || TYPE_REF_P (t) |
e42c407c | 76 | || (VOID_TYPE_P (t) && cxx_dialect >= cxx14)) |
2d76680f PC |
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 (strip_array_types (t)); | |
86 | return false; | |
87 | } | |
88 | ||
89 | /* If DECL is a variable declared `constexpr', require its type | |
9638f320 PC |
90 | be literal. Return error_mark_node if we give an error, the |
91 | DECL otherwise. */ | |
2d76680f PC |
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 (type)) | |
107 | { | |
108 | if (DECL_DECLARED_CONSTEXPR_P (decl)) | |
f1eac182 | 109 | { |
097f82ec | 110 | auto_diagnostic_group d; |
f2935576 PC |
111 | error_at (DECL_SOURCE_LOCATION (decl), |
112 | "the type %qT of %<constexpr%> variable %qD " | |
113 | "is not literal", type, decl); | |
f1eac182 | 114 | explain_non_literal_class (type); |
9638f320 | 115 | decl = error_mark_node; |
f1eac182 | 116 | } |
8892d532 | 117 | else if (cxx_dialect < cxx23) |
60813a46 | 118 | { |
5d4e573b | 119 | if (!is_instantiation_of_constexpr (current_function_decl)) |
f1eac182 | 120 | { |
097f82ec | 121 | auto_diagnostic_group d; |
f2935576 PC |
122 | error_at (DECL_SOURCE_LOCATION (decl), |
123 | "variable %qD of non-literal type %qT in " | |
8892d532 JJ |
124 | "%<constexpr%> function only available with " |
125 | "%<-std=c++2b%> or %<-std=gnu++2b%>", decl, type); | |
f1eac182 | 126 | explain_non_literal_class (type); |
9638f320 | 127 | decl = error_mark_node; |
f1eac182 | 128 | } |
60813a46 JM |
129 | cp_function_chain->invalid_constexpr = true; |
130 | } | |
2d76680f | 131 | } |
8e9589bd JM |
132 | else if (DECL_DECLARED_CONSTEXPR_P (decl) |
133 | && variably_modified_type_p (type, NULL_TREE)) | |
134 | { | |
f2935576 PC |
135 | error_at (DECL_SOURCE_LOCATION (decl), |
136 | "%<constexpr%> variable %qD has variably-modified " | |
137 | "type %qT", decl, type); | |
8e9589bd JM |
138 | decl = error_mark_node; |
139 | } | |
2d76680f PC |
140 | } |
141 | return decl; | |
142 | } | |
143 | ||
c85f8dbb MP |
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 (global_dc, &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 (global_dc, &diagnostic); | |
173 | } | |
174 | else | |
175 | ret = false; | |
176 | va_end (ap); | |
177 | return ret; | |
178 | } | |
179 | ||
ca752f39 | 180 | struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef> |
2d76680f | 181 | { |
bfc139e2 NS |
182 | static hashval_t hash (const constexpr_fundef *); |
183 | static bool equal (const constexpr_fundef *, const constexpr_fundef *); | |
2d76680f PC |
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 | |
bfc139e2 NS |
196 | constexpr_fundef_hasher::equal (const constexpr_fundef *lhs, |
197 | const constexpr_fundef *rhs) | |
2d76680f PC |
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 | |
bfc139e2 | 206 | constexpr_fundef_hasher::hash (const constexpr_fundef *fundef) |
2d76680f PC |
207 | { |
208 | return DECL_UID (fundef->decl); | |
209 | } | |
210 | ||
211 | /* Return a previously saved definition of function FUN. */ | |
212 | ||
bfc139e2 | 213 | constexpr_fundef * |
2d76680f PC |
214 | retrieve_constexpr_fundef (tree fun) |
215 | { | |
2d76680f PC |
216 | if (constexpr_fundef_table == NULL) |
217 | return NULL; | |
218 | ||
bfc139e2 | 219 | constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE }; |
2d76680f PC |
220 | return constexpr_fundef_table->find (&fundef); |
221 | } | |
222 | ||
223 | /* Check whether the parameter and return types of FUN are valid for a | |
224 | constexpr function, and complain if COMPLAIN. */ | |
225 | ||
98e5a19a | 226 | bool |
2d76680f PC |
227 | is_valid_constexpr_fn (tree fun, bool complain) |
228 | { | |
229 | bool ret = true; | |
230 | ||
31f7f784 | 231 | if (DECL_INHERITED_CTOR (fun) |
2d76680f PC |
232 | && TREE_CODE (fun) == TEMPLATE_DECL) |
233 | { | |
234 | ret = false; | |
235 | if (complain) | |
84fa214d | 236 | error ("inherited constructor %qD is not %<constexpr%>", |
31f7f784 | 237 | DECL_INHERITED_CTOR (fun)); |
2d76680f PC |
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 | { | |
097f82ec | 248 | auto_diagnostic_group d; |
c85f8dbb MP |
249 | if (constexpr_error (input_location, /*constexpr_fundef_p*/true, |
250 | "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)); | |
2d76680f PC |
254 | } |
255 | } | |
256 | } | |
257 | ||
e1bea341 JM |
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), | |
84fa214d | 263 | "lambdas are implicitly %<constexpr%> only in C++17 and later"); |
e1bea341 | 264 | } |
0fd824d7 | 265 | else if (DECL_DESTRUCTOR_P (fun) && cxx_dialect < cxx20) |
87c2080b | 266 | { |
0fd824d7 JJ |
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%>"); | |
87c2080b | 272 | } |
0fd824d7 | 273 | else if (!DECL_CONSTRUCTOR_P (fun) && !DECL_DESTRUCTOR_P (fun)) |
2d76680f PC |
274 | { |
275 | tree rettype = TREE_TYPE (TREE_TYPE (fun)); | |
276 | if (!literal_type_p (rettype)) | |
277 | { | |
278 | ret = false; | |
279 | if (complain) | |
280 | { | |
097f82ec | 281 | auto_diagnostic_group d; |
c85f8dbb MP |
282 | if (constexpr_error (input_location, /*constexpr_fundef_p*/true, |
283 | "invalid return type %qT of %<constexpr%> " | |
284 | "function %q+D", rettype, fun)) | |
285 | explain_non_literal_class (rettype); | |
2d76680f PC |
286 | } |
287 | } | |
288 | ||
54069e59 JM |
289 | /* C++14 DR 1684 removed this restriction. */ |
290 | if (cxx_dialect < cxx14 | |
f9fbf93d | 291 | && DECL_IOBJ_MEMBER_FUNCTION_P (fun) |
2d76680f PC |
292 | && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun))) |
293 | { | |
294 | ret = false; | |
097f82ec DM |
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 | } | |
2d76680f PC |
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 (*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 | { | |
0f54cc9c JM |
386 | for (tree stmt : tsi_range (t)) |
387 | if (! build_data_member_initialization (stmt, vec)) | |
388 | return false; | |
2d76680f PC |
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 | |
60813a46 JM |
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)) | |
2d76680f PC |
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 | { | |
60813a46 JM |
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; | |
2d76680f PC |
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); | |
2d76680f | 425 | else |
60813a46 JM |
426 | /* Don't add anything else to the CONSTRUCTOR. */ |
427 | return true; | |
2d76680f PC |
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); | |
8cb7aaa1 JM |
461 | if (TREE_CODE (aggr) == VAR_DECL) |
462 | /* Initializing a local variable, don't add anything. */ | |
463 | return true; | |
2d76680f PC |
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); | |
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 | ||
49b5925f JM |
476 | /* Value-initialization can produce multiple initializers for the |
477 | same field; use the last one. */ | |
478 | if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member) | |
479 | (*vec)->last().value = init; | |
480 | else | |
481 | CONSTRUCTOR_APPEND_ELT (*vec, member, init); | |
2d76680f PC |
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 | ||
2d76680f PC |
494 | for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var)) |
495 | if (TREE_CODE (var) == TYPE_DECL | |
4414e22e PC |
496 | && DECL_IMPLICIT_TYPEDEF_P (var) |
497 | && !LAMBDA_TYPE_P (TREE_TYPE (var))) | |
2d76680f PC |
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: | |
a0008434 MP |
510 | if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL |
511 | || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL) | |
2d76680f | 512 | return true; |
2d76680f PC |
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 (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: | |
96a95ac1 | 528 | case DEBUG_BEGIN_STMT: |
2d76680f PC |
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 | { | |
60813a46 JM |
542 | /* C++14 doesn't require a constexpr ctor to have an empty body. */ |
543 | if (cxx_dialect >= cxx14) | |
544 | return true; | |
545 | ||
2d76680f PC |
546 | bool ok = true; |
547 | if (TREE_CODE (list) == STATEMENT_LIST) | |
548 | { | |
549 | tree_stmt_iterator i = tsi_last (list); | |
550 | for (; !tsi_end_p (i); tsi_prev (&i)) | |
551 | { | |
552 | tree t = tsi_stmt (i); | |
553 | if (t == last) | |
554 | break; | |
555 | if (!check_constexpr_ctor_body_1 (last, 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) | |
84fa214d | 568 | error ("%<constexpr%> constructor does not have empty body"); |
2d76680f PC |
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, i, &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; | |
fa837fb6 JM |
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: | |
0f54cc9c | 630 | for (tree stmt : tsi_range (body)) |
fa837fb6 | 631 | { |
0f54cc9c | 632 | body = stmt; |
fa837fb6 JM |
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 (); | |
58cc255c | 644 | } |
fa837fb6 | 645 | found: |
1259cb6d JJ |
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 | } | |
2d76680f PC |
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 (body, &vec); | |
666 | } | |
667 | else if (TREE_CODE (body) == STATEMENT_LIST) | |
668 | { | |
0f54cc9c | 669 | for (tree stmt : tsi_range (body)) |
2d76680f | 670 | { |
0f54cc9c | 671 | ok = build_data_member_initialization (stmt, &vec); |
2d76680f PC |
672 | if (!ok) |
673 | break; | |
674 | } | |
675 | } | |
2d76680f PC |
676 | else if (EXPR_P (body)) |
677 | ok = build_data_member_initialization (body, &vec); | |
678 | else | |
679 | gcc_assert (errorcount > 0); | |
680 | if (ok) | |
681 | { | |
682 | if (vec_safe_length (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 (vec); | |
690 | return body; | |
691 | } | |
692 | } | |
693 | vec = sort_constexpr_mem_initializers (type, vec); | |
694 | return build_constructor (type, vec); | |
695 | } | |
696 | else | |
697 | return error_mark_node; | |
698 | } | |
699 | ||
1b6fa695 ML |
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, | |
8c906382 | 702 | return the _DECL for that function. */ |
1b6fa695 ML |
703 | |
704 | static tree | |
705 | get_function_named_in_call (tree t) | |
706 | { | |
8c906382 MP |
707 | tree callee = cp_get_callee (t); |
708 | tree fun = cp_get_fndecl_from_callee (callee, /*fold*/false); | |
709 | return fun ? fun : callee; | |
1b6fa695 ML |
710 | } |
711 | ||
bfc139e2 | 712 | /* Subroutine of check_constexpr_fundef. BODY is the body of a function |
2d76680f PC |
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 | ||
5afef8b1 | 717 | tree |
2d76680f PC |
718 | constexpr_fn_retval (tree body) |
719 | { | |
720 | switch (TREE_CODE (body)) | |
721 | { | |
722 | case STATEMENT_LIST: | |
723 | { | |
2d76680f | 724 | tree expr = NULL_TREE; |
0f54cc9c | 725 | for (tree stmt : tsi_range (body)) |
2d76680f | 726 | { |
0f54cc9c | 727 | tree s = constexpr_fn_retval (stmt); |
2d76680f PC |
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: | |
0162cb3b PC |
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 | } | |
2d76680f PC |
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 (body)) | |
759 | return error_mark_node; | |
760 | return constexpr_fn_retval (BIND_EXPR_BODY (body)); | |
761 | ||
762 | case USING_STMT: | |
96a95ac1 | 763 | case DEBUG_BEGIN_STMT: |
2d76680f PC |
764 | return NULL_TREE; |
765 | ||
1b6fa695 ML |
766 | case CALL_EXPR: |
767 | { | |
768 | tree fun = get_function_named_in_call (body); | |
769 | if (fun != NULL_TREE | |
3d78e008 | 770 | && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE)) |
1b6fa695 ML |
771 | return NULL_TREE; |
772 | } | |
773 | /* Fallthru. */ | |
774 | ||
2d76680f PC |
775 | default: |
776 | return error_mark_node; | |
777 | } | |
778 | } | |
779 | ||
bfc139e2 | 780 | /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of |
2d76680f PC |
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); | |
60813a46 | 790 | else if (cxx_dialect < cxx14) |
2d76680f PC |
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 | ||
3e4b91f2 NS |
801 | /* CTYPE is a type constructed from BODY. Return true if some |
802 | bases/fields are uninitialized, and complain if COMPLAIN. */ | |
2d76680f PC |
803 | |
804 | static bool | |
3e4b91f2 | 805 | cx_check_missing_mem_inits (tree ctype, tree body, bool complain) |
2d76680f | 806 | { |
7906797e | 807 | /* We allow uninitialized bases/fields in C++20. */ |
b04445d4 | 808 | if (cxx_dialect >= cxx20) |
7906797e MP |
809 | return false; |
810 | ||
3e4b91f2 NS |
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); | |
2d76680f PC |
820 | |
821 | if (TREE_CODE (ctype) == UNION_TYPE) | |
822 | { | |
0c7bce0a | 823 | if (nelts == 0 && next_aggregate_field (field)) |
2d76680f PC |
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 | ||
3e4b91f2 NS |
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) | |
2d76680f | 837 | { |
3e4b91f2 NS |
838 | tree index = NULL_TREE; |
839 | if (i < nelts) | |
2d76680f PC |
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 | } | |
3e4b91f2 | 847 | |
2d76680f PC |
848 | for (; field != index; field = DECL_CHAIN (field)) |
849 | { | |
850 | tree ftype; | |
3e4b91f2 NS |
851 | if (TREE_CODE (field) != FIELD_DECL) |
852 | continue; | |
7c30b12a | 853 | if (DECL_UNNAMED_BIT_FIELD (field)) |
2d76680f | 854 | continue; |
3e4b91f2 NS |
855 | if (DECL_ARTIFICIAL (field)) |
856 | continue; | |
857 | if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) | |
858 | { | |
7906797e | 859 | /* Recurse to check the anonymous aggregate member. */ |
3e4b91f2 NS |
860 | bad |= cx_check_missing_mem_inits |
861 | (TREE_TYPE (field), NULL_TREE, complain); | |
862 | if (bad && !complain) | |
863 | return true; | |
864 | continue; | |
865 | } | |
a1c9c9ff JM |
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; | |
a4dfd0f0 | 871 | if (is_empty_field (field)) |
6876b269 JM |
872 | /* An empty field doesn't need an initializer. */ |
873 | continue; | |
a1c9c9ff | 874 | ftype = strip_array_types (ftype); |
2d76680f PC |
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; | |
097f82ec | 886 | auto_diagnostic_group d; |
b8cd3996 JM |
887 | error ("member %qD must be initialized by mem-initializer " |
888 | "in %<constexpr%> constructor", field); | |
889 | inform (DECL_SOURCE_LOCATION (field), "declared here"); | |
2d76680f PC |
890 | bad = true; |
891 | } | |
892 | if (field == NULL_TREE) | |
893 | break; | |
3e4b91f2 NS |
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 | } | |
2d76680f PC |
903 | field = DECL_CHAIN (field); |
904 | } | |
905 | ||
906 | return bad; | |
907 | } | |
908 | ||
909 | /* We are processing the definition of the constexpr function FUN. | |
bfc139e2 NS |
910 | Check that its body fulfills the apropriate requirements and |
911 | enter it in the constexpr function definition table. */ | |
2d76680f | 912 | |
bfc139e2 NS |
913 | void |
914 | maybe_save_constexpr_fundef (tree fun) | |
2d76680f | 915 | { |
bfc139e2 | 916 | if (processing_template_decl |
bfc139e2 | 917 | || cp_function_chain->invalid_constexpr |
81f97181 | 918 | || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))) |
bfc139e2 | 919 | return; |
2d76680f | 920 | |
87c2080b JM |
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 ("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; | |
37326651 JM |
942 | |
943 | if (!is_valid_constexpr_fn (fun, complain)) | |
bfc139e2 | 944 | return; |
2d76680f | 945 | |
bfc139e2 | 946 | tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun)); |
ca30abcd | 947 | if (massaged == NULL_TREE || massaged == error_mark_node) |
2d76680f | 948 | { |
87c2080b | 949 | if (!DECL_CONSTRUCTOR_P (fun) && complain) |
84fa214d ML |
950 | error ("body of %<constexpr%> function %qD not a return-statement", |
951 | fun); | |
bfc139e2 | 952 | return; |
2d76680f PC |
953 | } |
954 | ||
4f05d85a | 955 | bool potential = potential_rvalue_constant_expression (massaged); |
37326651 | 956 | if (!potential && complain) |
c85f8dbb | 957 | require_potential_rvalue_constant_expression_fncheck (massaged); |
2d76680f | 958 | |
c5de3444 JM |
959 | if (DECL_CONSTRUCTOR_P (fun) && potential |
960 | && !DECL_DEFAULTED_FN (fun)) | |
37326651 JM |
961 | { |
962 | if (cx_check_missing_mem_inits (DECL_CONTEXT (fun), | |
963 | 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) | |
c85f8dbb | 972 | require_potential_rvalue_constant_expression_fncheck (massaged); |
37326651 JM |
973 | } |
974 | } | |
4f05d85a | 975 | |
c85f8dbb MP |
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) | |
bfc139e2 | 982 | return; |
2d76680f | 983 | |
87c2080b JM |
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 | ||
bfc139e2 | 999 | constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE}; |
43574e4f | 1000 | bool clear_ctx = false; |
43574e4f JJ |
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 | } | |
bfc139e2 NS |
1006 | tree saved_fn = current_function_decl; |
1007 | current_function_decl = fun; | |
1008 | entry.body = copy_fn (entry.decl, entry.parms, entry.result); | |
43574e4f | 1009 | current_function_decl = saved_fn; |
43574e4f | 1010 | if (clear_ctx) |
bfc139e2 | 1011 | DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE; |
4f05d85a JM |
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 | ||
bfc139e2 NS |
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 (101); | |
1031 | ||
1032 | constexpr_fundef **slot = constexpr_fundef_table->find_slot | |
1033 | (const_cast<constexpr_fundef *> (&value), INSERT); | |
1034 | ||
2d76680f PC |
1035 | gcc_assert (*slot == NULL); |
1036 | *slot = ggc_alloc<constexpr_fundef> (); | |
bfc139e2 | 1037 | **slot = value; |
2d76680f PC |
1038 | } |
1039 | ||
c85f8dbb MP |
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. */ | |
2d76680f PC |
1045 | |
1046 | void | |
1047 | explain_invalid_constexpr_fn (tree fun) | |
1048 | { | |
1049 | static hash_set<tree> *diagnosed; | |
1050 | tree body; | |
c85f8dbb MP |
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. */; | |
98e5a19a | 1057 | /* Only diagnose defaulted functions, lambdas, or instantiations. */ |
c85f8dbb MP |
1058 | else if (!DECL_DEFAULTED_FN (fun) |
1059 | && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) | |
1060 | && !is_instantiation_of_constexpr (fun)) | |
f22f817c JM |
1061 | { |
1062 | inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun); | |
1063 | return; | |
1064 | } | |
2d76680f PC |
1065 | if (diagnosed == NULL) |
1066 | diagnosed = new hash_set<tree>; | |
1067 | if (diagnosed->add (fun)) | |
1068 | /* Already explained. */ | |
1069 | return; | |
1070 | ||
17dc903e | 1071 | iloc_sentinel ils = input_location; |
98e5a19a JM |
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, | |
84fa214d | 1078 | "%qD is not usable as a %<constexpr%> function because:", fun); |
98e5a19a | 1079 | } |
2d76680f PC |
1080 | /* First check the declaration. */ |
1081 | if (is_valid_constexpr_fn (fun, true)) | |
1082 | { | |
1083 | /* Then if it's OK, the body. */ | |
98e5a19a | 1084 | if (!DECL_DECLARED_CONSTEXPR_P (fun) |
4f05d85a | 1085 | && DECL_DEFAULTED_FN (fun)) |
2d76680f PC |
1086 | explain_implicit_non_constexpr (fun); |
1087 | else | |
1088 | { | |
4f05d85a JM |
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); | |
2d76680f PC |
1094 | require_potential_rvalue_constant_expression (body); |
1095 | if (DECL_CONSTRUCTOR_P (fun)) | |
a22eeaca MP |
1096 | { |
1097 | cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, 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 | } | |
2d76680f PC |
1105 | } |
1106 | } | |
2d76680f PC |
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; | |
3c961dc7 | 1116 | /* Parameter bindings environment. A TREE_VEC of arguments. */ |
2d76680f PC |
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; | |
3a0bc47c PP |
1126 | /* The value of constexpr_ctx::manifestly_const_eval. */ |
1127 | enum mce_value manifestly_const_eval; | |
2d76680f PC |
1128 | }; |
1129 | ||
ca752f39 | 1130 | struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call> |
2d76680f PC |
1131 | { |
1132 | static hashval_t hash (constexpr_call *); | |
1133 | static bool equal (constexpr_call *, constexpr_call *); | |
3e605b20 JM |
1134 | }; |
1135 | ||
4b390698 JJ |
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 | ||
8e007055 JJ |
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 | ||
e6a29aab | 1152 | class constexpr_global_ctx { |
8e007055 | 1153 | /* Values for any temporaries or local variables within the |
9fdbd7d6 NS |
1154 | constant-expression. Objects outside their lifetime have |
1155 | value 'void_node'. */ | |
8e007055 | 1156 | hash_map<tree,tree> values; |
e6a29aab | 1157 | public: |
8e007055 JJ |
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; | |
ee1de08d JJ |
1165 | /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */ |
1166 | vec<tree> *cleanups; | |
e6a29aab JM |
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; | |
f74f6092 JJ |
1170 | /* Number of heap VAR_DECL deallocations. */ |
1171 | unsigned heap_dealloc_count; | |
8e007055 | 1172 | /* Constructor. */ |
f74f6092 | 1173 | constexpr_global_ctx () |
e6a29aab JM |
1174 | : constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr), |
1175 | heap_dealloc_count (0) {} | |
1176 | ||
9fdbd7d6 NS |
1177 | bool is_outside_lifetime (tree t) |
1178 | { | |
1179 | if (tree *p = values.get (t)) | |
1180 | if (*p == void_node) | |
1181 | return true; | |
1182 | return false; | |
1183 | } | |
e6a29aab JM |
1184 | tree get_value (tree t) |
1185 | { | |
1186 | if (tree *p = values.get (t)) | |
9fdbd7d6 NS |
1187 | if (*p != void_node) |
1188 | return *p; | |
e6a29aab JM |
1189 | return NULL_TREE; |
1190 | } | |
90bc2d09 | 1191 | tree *get_value_ptr (tree t, bool initializing) |
e6a29aab JM |
1192 | { |
1193 | if (modifiable && !modifiable->contains (t)) | |
1194 | return nullptr; | |
9fdbd7d6 | 1195 | if (tree *p = values.get (t)) |
90bc2d09 NS |
1196 | { |
1197 | if (*p != void_node) | |
1198 | return p; | |
1199 | else if (initializing) | |
1200 | { | |
1201 | *p = NULL_TREE; | |
1202 | return p; | |
1203 | } | |
1204 | } | |
9fdbd7d6 | 1205 | return nullptr; |
e6a29aab JM |
1206 | } |
1207 | void put_value (tree t, tree v) | |
1208 | { | |
1209 | bool already_in_map = values.put (t, v); | |
1210 | if (!already_in_map && modifiable) | |
1211 | modifiable->add (t); | |
1212 | } | |
90bc2d09 | 1213 | void destroy_value (tree t) |
9fdbd7d6 | 1214 | { |
90bc2d09 NS |
1215 | if (TREE_CODE (t) == VAR_DECL |
1216 | || TREE_CODE (t) == PARM_DECL | |
1217 | || TREE_CODE (t) == RESULT_DECL) | |
9fdbd7d6 NS |
1218 | values.put (t, void_node); |
1219 | else | |
1220 | values.remove (t); | |
1221 | } | |
90bc2d09 NS |
1222 | void clear_value (tree t) |
1223 | { | |
1224 | values.remove (t); | |
1225 | } | |
e6a29aab JM |
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) | |
90bc2d09 | 1249 | global->clear_value (t); |
e6a29aab JM |
1250 | global->modifiable = nullptr; |
1251 | } | |
8e007055 JJ |
1252 | }; |
1253 | ||
3e605b20 JM |
1254 | /* The constexpr expansion context. CALL is the current function |
1255 | expansion, CTOR is the current aggregate initializer, OBJECT is the | |
8e007055 | 1256 | object being initialized by CTOR, either a VAR_DECL or a _REF. */ |
3e605b20 JM |
1257 | |
1258 | struct constexpr_ctx { | |
8e007055 JJ |
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; | |
13f649f6 | 1262 | /* The innermost call we're evaluating. */ |
3e605b20 | 1263 | constexpr_call *call; |
ee1de08d JJ |
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. */ | |
0ee28590 | 1266 | vec<tree> *save_exprs; |
13f649f6 JM |
1267 | /* The CONSTRUCTOR we're currently building up for an aggregate |
1268 | initializer. */ | |
3e605b20 | 1269 | tree ctor; |
13f649f6 | 1270 | /* The object we're building the CONSTRUCTOR for. */ |
3e605b20 | 1271 | tree object; |
4b390698 JJ |
1272 | /* If inside SWITCH_EXPR. */ |
1273 | constexpr_switch_state *css_state; | |
49a86fce PP |
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; | |
a15ffa22 | 1277 | |
aaa26bf4 JM |
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. */ | |
2b3ab879 | 1281 | bool quiet; |
13f649f6 JM |
1282 | /* Whether we are strictly conforming to constant expression rules or |
1283 | trying harder to get a constant value. */ | |
69eb4fde | 1284 | bool strict; |
e4082611 | 1285 | /* Whether __builtin_is_constant_evaluated () should be true. */ |
3a0bc47c | 1286 | mce_value manifestly_const_eval; |
3e605b20 | 1287 | }; |
2d76680f | 1288 | |
90bc2d09 NS |
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 | ||
f65a3299 PP |
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 | ||
2d76680f PC |
1382 | /* A table of all constexpr calls that have been evaluated by the |
1383 | compiler in this translation unit. */ | |
1384 | ||
97f3003f | 1385 | static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table; |
2d76680f | 1386 | |
2d76680f PC |
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 | { | |
2d76680f | 1402 | if (lhs == rhs) |
46cf7fa1 JJ |
1403 | return true; |
1404 | if (lhs->hash != rhs->hash) | |
1405 | return false; | |
13de99bc | 1406 | if (lhs->manifestly_const_eval != rhs->manifestly_const_eval) |
cce3ae91 | 1407 | return false; |
2d76680f | 1408 | if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef)) |
46cf7fa1 | 1409 | return false; |
3c961dc7 | 1410 | return cp_tree_equal (lhs->bindings, rhs->bindings); |
2d76680f PC |
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 (101); | |
1420 | } | |
1421 | ||
c0daf32d PP |
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 | |
97f3003f JM |
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. | |
c0daf32d | 1431 | |
97f3003f | 1432 | This is not GC-deletable to avoid GC affecting UID generation. */ |
c0daf32d | 1433 | |
ba6730bd | 1434 | static GTY(()) decl_tree_map *fundef_copies_table; |
c0daf32d | 1435 | |
c0daf32d | 1436 | /* Reuse a copy or create a new unshared copy of the function FUN. |
3c98ff9b NS |
1437 | Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE |
1438 | is parms, TYPE is result. */ | |
c0daf32d | 1439 | |
97f3003f | 1440 | static tree |
f65a3299 | 1441 | get_fundef_copy (constexpr_fundef *fundef) |
c0daf32d | 1442 | { |
97f3003f | 1443 | tree copy; |
3c98ff9b | 1444 | bool existed; |
c89844e5 JM |
1445 | tree *slot = &(hash_map_safe_get_or_insert<hm_ggc> |
1446 | (fundef_copies_table, fundef->decl, &existed, 127)); | |
3c98ff9b NS |
1447 | |
1448 | if (!existed) | |
c0daf32d | 1449 | { |
3c98ff9b NS |
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. */ | |
43574e4f JJ |
1453 | copy = build_tree_list (fundef->body, fundef->parms); |
1454 | TREE_TYPE (copy) = fundef->result; | |
3c98ff9b NS |
1455 | } |
1456 | else if (*slot == NULL_TREE) | |
1457 | { | |
f65a3299 | 1458 | if (uid_sensitive_constexpr_evaluation_p ()) |
aaa26bf4 JM |
1459 | return NULL_TREE; |
1460 | ||
3c98ff9b | 1461 | /* We've already used the function itself, so make a copy. */ |
97f3003f | 1462 | copy = build_tree_list (NULL, NULL); |
43574e4f JJ |
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; | |
c0daf32d PP |
1477 | } |
1478 | else | |
1479 | { | |
3c98ff9b | 1480 | /* We have a cached function available. */ |
c0daf32d | 1481 | copy = *slot; |
97f3003f | 1482 | *slot = TREE_CHAIN (copy); |
c0daf32d PP |
1483 | } |
1484 | ||
1485 | return copy; | |
1486 | } | |
1487 | ||
3c98ff9b NS |
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. */ | |
c0daf32d PP |
1491 | |
1492 | static void | |
97f3003f | 1493 | save_fundef_copy (tree fun, tree copy) |
c0daf32d | 1494 | { |
3c98ff9b | 1495 | tree *slot = fundef_copies_table->get (fun); |
97f3003f | 1496 | TREE_CHAIN (copy) = *slot; |
c0daf32d PP |
1497 | *slot = copy; |
1498 | } | |
1499 | ||
72f76540 JM |
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); | |
b41a927b JM |
1511 | static tree cxx_eval_bare_aggregate (const constexpr_ctx *, tree, |
1512 | value_cat, bool *, bool *); | |
1189c038 JM |
1513 | static tree cxx_fold_indirect_ref (const constexpr_ctx *, location_t, tree, tree, |
1514 | bool * = NULL); | |
8c9c92f8 | 1515 | static tree find_heap_var_refs (tree *, int *, void *); |
72f76540 | 1516 | |
2d76680f PC |
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 | |
5756d0f9 | 1522 | cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun, |
72f76540 | 1523 | value_cat lval, |
2d76680f PC |
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; | |
5756d0f9 JM |
1530 | |
1531 | /* Don't fold __builtin_constant_p within a constexpr function. */ | |
e4082611 | 1532 | bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun); |
b925d25d | 1533 | |
fe736ffd JM |
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. */ | |
b925d25d | 1536 | if (bi_const_p |
3a0bc47c | 1537 | && ctx->manifestly_const_eval != mce_true |
5756d0f9 JM |
1538 | && current_function_decl |
1539 | && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) | |
2d76680f | 1540 | { |
5756d0f9 JM |
1541 | *non_constant_p = true; |
1542 | return t; | |
2d76680f | 1543 | } |
5756d0f9 | 1544 | |
e4082611 | 1545 | /* For __builtin_is_constant_evaluated, defer it if not |
117c6426 JJ |
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. */ | |
3d78e008 | 1550 | if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED, |
13de99bc | 1551 | BUILT_IN_FRONTEND)) |
e4082611 | 1552 | { |
3a0bc47c | 1553 | if (ctx->manifestly_const_eval == mce_unknown) |
e4082611 JJ |
1554 | { |
1555 | *non_constant_p = true; | |
1556 | return t; | |
1557 | } | |
3a0bc47c PP |
1558 | return constant_boolean_node (ctx->manifestly_const_eval == mce_true, |
1559 | boolean_type_node); | |
e4082611 JJ |
1560 | } |
1561 | ||
ff603745 | 1562 | if (fndecl_built_in_p (fun, CP_BUILT_IN_SOURCE_LOCATION, BUILT_IN_FRONTEND)) |
ba3d8dff JJ |
1563 | { |
1564 | temp_override<tree> ovr (current_function_decl); | |
1565 | if (ctx->call && ctx->call->fundef) | |
1566 | current_function_decl = ctx->call->fundef->decl; | |
64f7a3b3 | 1567 | return fold_builtin_source_location (t); |
ba3d8dff | 1568 | } |
ff603745 | 1569 | |
69dc042f JM |
1570 | int strops = 0; |
1571 | int strret = 0; | |
1572 | if (fndecl_built_in_p (fun, BUILT_IN_NORMAL)) | |
1573 | switch (DECL_FUNCTION_CODE (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; | |
bc13106e JJ |
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; | |
d68d3664 JM |
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; | |
69dc042f JM |
1612 | default: |
1613 | break; | |
1614 | } | |
1615 | ||
5756d0f9 JM |
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; | |
5756d0f9 | 1620 | for (i = 0; i < nargs; ++i) |
b925d25d | 1621 | { |
69dc042f | 1622 | tree arg = CALL_EXPR_ARG (t, i); |
6f346913 | 1623 | tree oarg = arg; |
69dc042f JM |
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 | ||
2ff7172a JJ |
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) | |
69dc042f | 1641 | || potential_constant_expression (arg)) |
4cd3e7df JJ |
1642 | { |
1643 | bool dummy1 = false, dummy2 = false; | |
72f76540 | 1644 | arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue, |
69dc042f | 1645 | &dummy1, &dummy2); |
4cd3e7df JJ |
1646 | } |
1647 | ||
b925d25d | 1648 | if (bi_const_p) |
4cd3e7df | 1649 | /* For __builtin_constant_p, fold all expressions with constant values |
b925d25d | 1650 | even if they aren't C++ constant-expressions. */ |
69dc042f JM |
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); | |
6f346913 JM |
1658 | else |
1659 | arg = oarg; | |
69dc042f JM |
1660 | } |
1661 | ||
1662 | args[i] = arg; | |
b925d25d | 1663 | } |
5756d0f9 JM |
1664 | |
1665 | bool save_ffbcp = force_folding_builtin_constant_p; | |
3a0bc47c | 1666 | force_folding_builtin_constant_p |= ctx->manifestly_const_eval == mce_true; |
43574e4f JJ |
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 (fun, BUILT_IN_FUNCTION) | |
1670 | && ctx->call | |
1671 | && ctx->call->fundef) | |
1672 | current_function_decl = ctx->call->fundef->decl; | |
6cd005a2 JJ |
1673 | if (fndecl_built_in_p (fun, |
1674 | CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS, | |
1675 | 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 | } | |
32c3a753 JJ |
1684 | else if (fndecl_built_in_p (fun, |
1685 | CP_BUILT_IN_IS_CORRESPONDING_MEMBER, | |
1686 | 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 | } | |
6cd005a2 JJ |
1696 | else |
1697 | new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t), | |
1698 | CALL_EXPR_FN (t), nargs, args); | |
43574e4f | 1699 | current_function_decl = save_cur_fn; |
5756d0f9 | 1700 | force_folding_builtin_constant_p = save_ffbcp; |
109d2197 JJ |
1701 | if (new_call == NULL) |
1702 | { | |
1703 | if (!*non_constant_p && !ctx->quiet) | |
1704 | { | |
d68d3664 JM |
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); | |
109d2197 JJ |
1708 | } |
1709 | *non_constant_p = true; | |
1710 | return t; | |
1711 | } | |
1712 | ||
43574e4f | 1713 | if (!potential_constant_expression (new_call)) |
109d2197 JJ |
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 | ||
69dc042f JM |
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 | ||
109d2197 JJ |
1733 | return cxx_eval_constant_expression (&new_ctx, new_call, lval, |
1734 | non_constant_p, overflow_p); | |
2d76680f PC |
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 | { | |
47be9509 | 1743 | if (same_type_p (TREE_TYPE (temp), type)) |
2d76680f PC |
1744 | return temp; |
1745 | /* Avoid wrapping an aggregate value in a NOP_EXPR. */ | |
1746 | if (TREE_CODE (temp) == CONSTRUCTOR) | |
b27f74e7 MP |
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 | } | |
ff8ba86f JJ |
1753 | if (TREE_CODE (temp) == EMPTY_CLASS_EXPR) |
1754 | return build0 (EMPTY_CLASS_EXPR, type); | |
2d76680f | 1755 | gcc_assert (scalarish_type_p (type)); |
72b091f7 MP |
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); | |
2d76680f PC |
1759 | } |
1760 | ||
9b9eb42a JM |
1761 | /* If T is a CONSTRUCTOR, return an unshared copy of T and any |
1762 | sub-CONSTRUCTORs. Otherwise return T. | |
0146e25f | 1763 | |
9b9eb42a JM |
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. */ | |
0146e25f | 1767 | |
1f6857ba | 1768 | tree |
3da7d774 | 1769 | unshare_constructor (tree t MEM_STAT_DECL) |
0146e25f | 1770 | { |
9b9eb42a JM |
1771 | if (!t || TREE_CODE (t) != CONSTRUCTOR) |
1772 | return t; | |
1773 | auto_vec <tree*, 4> ptrs; | |
1774 | ptrs.safe_push (&t); | |
1775 | while (!ptrs.is_empty ()) | |
1776 | { | |
1777 | tree *p = ptrs.pop (); | |
3da7d774 JM |
1778 | tree n = copy_node (*p PASS_MEM_STAT); |
1779 | CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT); | |
9b9eb42a JM |
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, i, &ce); ++i) | |
6b7d53a2 | 1784 | if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR) |
9b9eb42a JM |
1785 | ptrs.safe_push (&ce->value); |
1786 | } | |
0146e25f PP |
1787 | return t; |
1788 | } | |
1789 | ||
620adbec JM |
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 (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 (elts, i, &ce); ++i) | |
22cda0ca | 1806 | if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR) |
620adbec JM |
1807 | vec_safe_push (ctors, ce->value); |
1808 | ggc_free (elts); | |
1809 | } | |
1810 | ggc_free (c); | |
1811 | } | |
1812 | } | |
1813 | ||
559d2f1e JJ |
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; | |
e6a29aab | 1832 | if (global->get_value (var)) |
559d2f1e JJ |
1833 | return var; |
1834 | } | |
1835 | if (TYPE_P (*tp)) | |
1836 | *walk_subtrees = false; | |
1837 | return NULL_TREE; | |
1838 | } | |
1839 | ||
2d76680f PC |
1840 | /* Subroutine of cxx_eval_call_expression. |
1841 | We are processing a call expression (either CALL_EXPR or | |
3e605b20 | 1842 | AGGR_INIT_EXPR) in the context of CTX. Evaluate |
2d76680f PC |
1843 | all arguments and bind their values to correspondings |
1844 | parameters, making up the NEW_CALL context. */ | |
1845 | ||
1595fe44 JM |
1846 | static tree |
1847 | cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun, | |
12d9ce19 JM |
1848 | bool *non_constant_p, bool *overflow_p, |
1849 | bool *non_constant_args) | |
2d76680f PC |
1850 | { |
1851 | const int nargs = call_expr_nargs (t); | |
1595fe44 | 1852 | tree parms = DECL_ARGUMENTS (fun); |
2d76680f | 1853 | int i; |
3c961dc7 JM |
1854 | /* We don't record ellipsis args below. */ |
1855 | int nparms = list_length (parms); | |
1856 | int nbinds = nargs < nparms ? nargs : nparms; | |
1595fe44 | 1857 | tree binds = make_tree_vec (nbinds); |
2d76680f PC |
1858 | for (i = 0; i < nargs; ++i) |
1859 | { | |
1860 | tree x, arg; | |
1861 | tree type = parms ? TREE_TYPE (parms) : void_type_node; | |
1595fe44 JM |
1862 | if (parms && DECL_BY_REFERENCE (parms)) |
1863 | type = TREE_TYPE (type); | |
2d76680f | 1864 | x = get_nth_callarg (t, i); |
3e605b20 JM |
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 | |
60813a46 | 1867 | which case we get the real argument from ctx. */ |
3e605b20 JM |
1868 | if (i == 0 && DECL_CONSTRUCTOR_P (fun) |
1869 | && is_dummy_object (x)) | |
1870 | { | |
1871 | x = ctx->object; | |
84dd815f | 1872 | x = build_address (x); |
3e605b20 | 1873 | } |
43574e4f JJ |
1874 | if (TREE_ADDRESSABLE (type)) |
1875 | /* Undo convert_for_arg_passing work here. */ | |
1876 | x = convert_from_reference (x); | |
6c59b8a9 JM |
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. */ | |
72f76540 | 1880 | arg = cxx_eval_constant_expression (ctx, x, vc_prvalue, |
5a804683 | 1881 | non_constant_p, overflow_p); |
5ba949c0 NS |
1882 | /* Check we aren't dereferencing a null pointer when calling a non-static |
1883 | member function, which is undefined behaviour. */ | |
f9fbf93d | 1884 | if (i == 0 && DECL_OBJECT_MEMBER_FUNCTION_P (fun) |
5ba949c0 NS |
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 (x), | |
1893 | "dereferencing a null pointer"); | |
1894 | *non_constant_p = true; | |
1895 | } | |
2d76680f | 1896 | /* Don't VERIFY_CONSTANT here. */ |
2b3ab879 | 1897 | if (*non_constant_p && ctx->quiet) |
1595fe44 | 1898 | break; |
2d76680f PC |
1899 | /* Just discard ellipsis args after checking their constantitude. */ |
1900 | if (!parms) | |
1901 | continue; | |
4727d4c6 NS |
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. */ | |
9f613f06 | 1907 | if (!TYPE_REF_P (type)) |
4727d4c6 NS |
1908 | arg = adjust_temp_type (type, arg); |
1909 | if (!TREE_CONSTANT (arg)) | |
1910 | *non_constant_args = true; | |
5afd90c5 JJ |
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; | |
559d2f1e JJ |
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; | |
5afd90c5 | 1924 | |
5558a0da JJ |
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 | } | |
3c961dc7 | 1945 | TREE_VEC_ELT (binds, i) = arg; |
4727d4c6 | 1946 | } |
2d76680f PC |
1947 | parms = TREE_CHAIN (parms); |
1948 | } | |
1595fe44 JM |
1949 | |
1950 | return binds; | |
2d76680f PC |
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. */ | |
7de76362 | 1957 | static vec<tree> call_stack; |
2d76680f PC |
1958 | static int call_stack_tick; |
1959 | static int last_cx_error_tick; | |
1960 | ||
7ffc7de5 | 1961 | static int |
2d76680f PC |
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 (call); | |
7ffc7de5 JM |
1968 | int len = call_stack.length (); |
1969 | if (len > max_constexpr_depth) | |
2d76680f | 1970 | return false; |
7ffc7de5 | 1971 | return len; |
2d76680f PC |
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 | ||
ac8f92c1 JM |
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 | ||
08b51bad JJ |
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 | |
ac8f92c1 | 2018 | find_failing_clause_r (const constexpr_ctx *ctx, tree expr) |
08b51bad JJ |
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 | } | |
4a54873d JM |
2029 | tree e = contextual_conv_bool (expr, tf_none); |
2030 | e = fold_operand (e, ctx); | |
08b51bad JJ |
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 | |
ac8f92c1 | 2040 | find_failing_clause (const constexpr_ctx *ctx, tree expr) |
08b51bad JJ |
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 | ||
ac8f92c1 JM |
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); | |
e6a29aab JM |
2059 | if (TREE_CODE (bad) == CLEANUP_POINT_EXPR) |
2060 | bad = TREE_OPERAND (bad, 0); | |
ac8f92c1 JM |
2061 | |
2062 | /* Actually explain the failure if this is a concept check or a | |
2063 | requires-expression. */ | |
2064 | if (concept_check_p (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 | ||
2efb237f JCI |
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, 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 (bad, loc); | |
2119 | ||
2120 | /* Report the error. */ | |
2121 | auto_diagnostic_group d; | |
2122 | error_at (cloc, msg); | |
2123 | diagnose_failing_condition (bad, cloc, true, ctx); | |
2124 | return bad; | |
2125 | } | |
2126 | *non_constant_p = true; | |
2127 | return false; | |
2128 | } | |
2129 | ||
2130 | return true; | |
2131 | } | |
2132 | ||
44a845ca MS |
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, | |
72f76540 | 2139 | value_cat lval, |
44a845ca MS |
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: | |
81fea426 | 2149 | case IFN_FALLTHROUGH: |
44a845ca MS |
2150 | return void_node; |
2151 | ||
08b51bad | 2152 | case IFN_ASSUME: |
2efb237f JCI |
2153 | if (!cxx_eval_assert (ctx, CALL_EXPR_ARG (t, 0), |
2154 | G_("failed %<assume%> attribute assumption"), | |
2155 | EXPR_LOCATION (t), /*eval*/false, | |
2156 | non_constant_p, overflow_p)) | |
2157 | return t; | |
08b51bad JJ |
2158 | return void_node; |
2159 | ||
44a845ca MS |
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 | ||
e16f1cc7 JJ |
2170 | case IFN_LAUNDER: |
2171 | return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), | |
72f76540 JM |
2172 | vc_prvalue, non_constant_p, |
2173 | overflow_p); | |
e16f1cc7 | 2174 | |
d8fcab68 JJ |
2175 | case IFN_VEC_CONVERT: |
2176 | { | |
2177 | tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), | |
72f76540 | 2178 | vc_prvalue, non_constant_p, |
d8fcab68 JJ |
2179 | overflow_p); |
2180 | if (TREE_CODE (arg) == VECTOR_CST) | |
84993d94 JJ |
2181 | if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg)) |
2182 | return r; | |
d8fcab68 | 2183 | } |
84993d94 | 2184 | /* FALLTHRU */ |
d8fcab68 | 2185 | |
44a845ca MS |
2186 | default: |
2187 | if (!ctx->quiet) | |
f9d0ca40 | 2188 | error_at (cp_expr_loc_or_input_loc (t), |
44a845ca MS |
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 | { | |
f9d0ca40 | 2203 | location_t loc = cp_expr_loc_or_input_loc (t); |
44a845ca MS |
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 | ||
e8c48716 | 2221 | /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */ |
ecc57615 JM |
2222 | |
2223 | static void | |
2224 | clear_no_implicit_zero (tree ctor) | |
2225 | { | |
e8c48716 | 2226 | if (CONSTRUCTOR_NO_CLEARING (ctor)) |
ecc57615 | 2227 | { |
e8c48716 | 2228 | CONSTRUCTOR_NO_CLEARING (ctor) = false; |
fae00a0a JM |
2229 | for (auto &e: CONSTRUCTOR_ELTS (ctor)) |
2230 | if (TREE_CODE (e.value) == CONSTRUCTOR) | |
2231 | clear_no_implicit_zero (e.value); | |
ecc57615 JM |
2232 | } |
2233 | } | |
2234 | ||
04e1749c MP |
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 (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)); | |
5ebe5bcf NS |
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"); | |
04e1749c MP |
2272 | } |
2273 | ||
8e007055 JJ |
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 | { | |
b04445d4 | 2280 | return (cxx_dialect >= cxx20 |
8e007055 | 2281 | && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl)) |
cf650568 JJ |
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 | { | |
b04445d4 | 2293 | if (cxx_dialect >= cxx20 |
cf650568 JJ |
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 (name, "construct_at"); | |
8e007055 JJ |
2317 | } |
2318 | ||
2ffc2645 MP |
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 (call->fundef->decl)); | |
2327 | } | |
2328 | ||
1e184761 | 2329 | /* True if CTX is an instance of std::allocator. */ |
8412b939 | 2330 | |
1e184761 JM |
2331 | bool |
2332 | is_std_allocator (tree ctx) | |
8412b939 | 2333 | { |
8412b939 JJ |
2334 | if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx)) |
2335 | return false; | |
2336 | ||
2337 | tree decl = TYPE_MAIN_DECL (ctx); | |
1e184761 | 2338 | tree name = DECL_NAME (decl); |
8412b939 JJ |
2339 | if (name == NULL_TREE || !id_equal (name, "allocator")) |
2340 | return false; | |
2341 | ||
2342 | return decl_in_std_namespace_p (decl); | |
2343 | } | |
2344 | ||
1e184761 JM |
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 (name, "allocate") || id_equal (name, "deallocate"))) | |
2353 | return false; | |
2354 | ||
2355 | return is_std_allocator (DECL_CONTEXT (fndecl)); | |
2356 | } | |
2357 | ||
2ffc2645 MP |
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 (call->fundef->decl)); | |
2366 | } | |
2367 | ||
b69ee500 NS |
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 (name, "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 (name, "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 (call->fundef->decl)); | |
2396 | } | |
2397 | ||
22edf943 MP |
2398 | /* Return true if FNDECL is __dynamic_cast. */ |
2399 | ||
2400 | static inline bool | |
2401 | cxx_dynamic_cast_fn_p (tree fndecl) | |
2402 | { | |
b04445d4 | 2403 | return (cxx_dialect >= cxx20 |
22edf943 | 2404 | && id_equal (DECL_NAME (fndecl), "__dynamic_cast") |
d6a488f2 | 2405 | && CP_DECL_CONTEXT (fndecl) == abi_node); |
22edf943 MP |
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 | extract_obj_from_addr_offset (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 (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. */ | |
72f76540 | 2507 | obj = cxx_eval_constant_expression (ctx, obj, vc_prvalue, non_constant_p, |
22edf943 MP |
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 (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)) | |
de0684bf MP |
2521 | { |
2522 | if (reference_p) | |
2523 | { | |
2524 | if (!ctx->quiet) | |
2525 | { | |
4f870007 | 2526 | auto_diagnostic_group d; |
de0684bf MP |
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 | } | |
22edf943 MP |
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. */ | |
0221c656 | 2542 | tree vtable = build_vfield_ref (obj, objtype); |
72f76540 | 2543 | vtable = cxx_eval_constant_expression (ctx, vtable, vc_prvalue, |
22edf943 MP |
2544 | non_constant_p, overflow_p); |
2545 | if (*non_constant_p) | |
2546 | return call; | |
0221c656 MP |
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 | } | |
22edf943 MP |
2556 | /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */ |
2557 | vtable = extract_obj_from_addr_offset (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 (obj, type, 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 | { | |
4f870007 | 2582 | auto_diagnostic_group d; |
22edf943 MP |
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 (obj, mdtype, NULL_TREE); | |
2609 | if (obj == error_mark_node) | |
2610 | { | |
2611 | if (reference_p) | |
2612 | { | |
2613 | if (!ctx->quiet) | |
2614 | { | |
4f870007 | 2615 | auto_diagnostic_group d; |
22edf943 MP |
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 | { | |
4f870007 | 2637 | auto_diagnostic_group d; |
22edf943 MP |
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 | ||
4f6bc28f | 2656 | /* Data structure used by replace_decl and replace_decl_r. */ |
b2562229 | 2657 | |
4f6bc28f | 2658 | struct replace_decl_data |
b2562229 | 2659 | { |
4f6bc28f | 2660 | /* The _DECL we want to replace. */ |
b2562229 PP |
2661 | tree decl; |
2662 | /* The replacement for DECL. */ | |
2663 | tree replacement; | |
4f6bc28f JM |
2664 | /* Trees we've visited. */ |
2665 | hash_set<tree> *pset; | |
b2562229 PP |
2666 | /* Whether we've performed any replacements. */ |
2667 | bool changed; | |
2668 | }; | |
2669 | ||
4f6bc28f | 2670 | /* Helper function for replace_decl, called through cp_walk_tree. */ |
b2562229 PP |
2671 | |
2672 | static tree | |
4f6bc28f | 2673 | replace_decl_r (tree *tp, int *walk_subtrees, void *data) |
b2562229 | 2674 | { |
4f6bc28f | 2675 | replace_decl_data *d = (replace_decl_data *) data; |
b2562229 PP |
2676 | |
2677 | if (*tp == d->decl) | |
2678 | { | |
2679 | *tp = unshare_expr (d->replacement); | |
2680 | d->changed = true; | |
2681 | *walk_subtrees = 0; | |
2682 | } | |
4f6bc28f JM |
2683 | else if (TYPE_P (*tp) |
2684 | || d->pset->add (*tp)) | |
b2562229 PP |
2685 | *walk_subtrees = 0; |
2686 | ||
2687 | return NULL_TREE; | |
2688 | } | |
2689 | ||
4f6bc28f JM |
2690 | /* Replace every occurrence of DECL with (an unshared copy of) |
2691 | REPLACEMENT within the expression *TP. Returns true iff a | |
b2562229 PP |
2692 | replacement was performed. */ |
2693 | ||
4f6bc28f JM |
2694 | bool |
2695 | replace_decl (tree *tp, tree decl, tree replacement) | |
b2562229 | 2696 | { |
4f6bc28f JM |
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, replacement, &pset, false }; | |
2701 | cp_walk_tree (tp, replace_decl_r, &data, NULL); | |
b2562229 PP |
2702 | return data.changed; |
2703 | } | |
2704 | ||
e6321c45 JM |
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, | |
72f76540 | 2709 | value_cat lval, |
e6321c45 JM |
2710 | bool *non_constant_p, bool *overflow_p) |
2711 | { | |
2712 | tree function = THUNK_TARGET (thunk_fndecl); | |
2713 | ||
e6321c45 JM |
2714 | if (THUNK_VIRTUAL_OFFSET (thunk_fndecl)) |
2715 | { | |
e6321c45 JM |
2716 | if (!ctx->quiet) |
2717 | { | |
267318a2 JM |
2718 | if (!DECL_DECLARED_CONSTEXPR_P (function)) |
2719 | { | |
2720 | error ("call to non-%<constexpr%> function %qD", function); | |
2721 | explain_invalid_constexpr_fn (function); | |
2722 | } | |
2723 | else | |
2724 | /* virtual_offset is only set for virtual bases, which make the | |
2725 | class non-literal, so we don't need to handle it here. */ | |
2726 | error ("calling constexpr member function %qD through virtual " | |
2727 | "base subobject", function); | |
e6321c45 JM |
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 | ||
caf17f3a MP |
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. */ | |
72f76540 | 2770 | tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue, |
caf17f3a MP |
2771 | non_constant_p, overflow_p); |
2772 | if (TREE_CODE (e) == CONSTRUCTOR && !*non_constant_p) | |
2773 | TREE_READONLY (e) = readonly_p; | |
2774 | } | |
2775 | } | |
2776 | ||
2d76680f PC |
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 | |
3e605b20 | 2782 | cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, |
72f76540 | 2783 | value_cat lval, |
2d76680f PC |
2784 | bool *non_constant_p, bool *overflow_p) |
2785 | { | |
861d4af8 AS |
2786 | /* Handle concept checks separately. */ |
2787 | if (concept_check_p (t)) | |
662ef5b5 | 2788 | return evaluate_concept_check (t); |
861d4af8 | 2789 | |
f9d0ca40 | 2790 | location_t loc = cp_expr_loc_or_input_loc (t); |
2d76680f | 2791 | tree fun = get_function_named_in_call (t); |
cce3ae91 | 2792 | constexpr_call new_call |
13de99bc | 2793 | = { NULL, NULL, NULL, 0, ctx->manifestly_const_eval }; |
7ffc7de5 | 2794 | int depth_ok; |
2d76680f | 2795 | |
0b274c17 | 2796 | if (fun == NULL_TREE) |
44a845ca MS |
2797 | return cxx_eval_internal_function (ctx, t, lval, |
2798 | non_constant_p, overflow_p); | |
0b274c17 | 2799 | |
2d76680f PC |
2800 | if (TREE_CODE (fun) != FUNCTION_DECL) |
2801 | { | |
2802 | /* Might be a constexpr function pointer. */ | |
72f76540 JM |
2803 | fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue, |
2804 | non_constant_p, overflow_p); | |
2d76680f PC |
2805 | STRIP_NOPS (fun); |
2806 | if (TREE_CODE (fun) == ADDR_EXPR) | |
2807 | fun = TREE_OPERAND (fun, 0); | |
582d2481 JJ |
2808 | /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no |
2809 | indirection, the called expression is a pointer into the | |
2810 | virtual table which should contain FDESC_EXPR. Extract the | |
2811 | FUNCTION_DECL from there. */ | |
2812 | else if (TARGET_VTABLE_USES_DESCRIPTORS | |
2813 | && TREE_CODE (fun) == POINTER_PLUS_EXPR | |
2814 | && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR | |
2815 | && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST) | |
2816 | { | |
2817 | tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0); | |
2818 | if (VAR_P (d) | |
2819 | && DECL_VTABLE_OR_VTT_P (d) | |
2820 | && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE | |
2821 | && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type | |
2822 | && DECL_INITIAL (d) | |
2823 | && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR) | |
2824 | { | |
2825 | tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1), | |
2826 | TYPE_SIZE_UNIT (vtable_entry_type)); | |
2827 | HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i); | |
2828 | if (idx >= 0) | |
2829 | { | |
2830 | tree fdesc | |
2831 | = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value; | |
2832 | if (TREE_CODE (fdesc) == FDESC_EXPR | |
2833 | && integer_zerop (TREE_OPERAND (fdesc, 1))) | |
2834 | fun = TREE_OPERAND (fdesc, 0); | |
2835 | } | |
2836 | } | |
2837 | } | |
2d76680f PC |
2838 | } |
2839 | if (TREE_CODE (fun) != FUNCTION_DECL) | |
2840 | { | |
2b3ab879 | 2841 | if (!ctx->quiet && !*non_constant_p) |
84fa214d | 2842 | error_at (loc, "expression %qE does not designate a %<constexpr%> " |
2d76680f PC |
2843 | "function", fun); |
2844 | *non_constant_p = true; | |
2845 | return t; | |
2846 | } | |
81f97181 | 2847 | if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)) |
2d76680f | 2848 | fun = DECL_CLONED_FUNCTION (fun); |
0b274c17 MP |
2849 | |
2850 | if (is_ubsan_builtin_p (fun)) | |
2851 | return void_node; | |
2852 | ||
3d78e008 | 2853 | if (fndecl_built_in_p (fun)) |
5756d0f9 | 2854 | return cxx_eval_builtin_function_call (ctx, t, fun, |
92a596e8 | 2855 | lval, non_constant_p, overflow_p); |
e6321c45 JM |
2856 | if (DECL_THUNK_P (fun)) |
2857 | return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p); | |
87c2080b | 2858 | if (!maybe_constexpr_fn (fun)) |
2d76680f | 2859 | { |
8412b939 JJ |
2860 | if (TREE_CODE (t) == CALL_EXPR |
2861 | && cxx_replaceable_global_alloc_fn (fun) | |
2862 | && (CALL_FROM_NEW_OR_DELETE_P (t) | |
2ffc2645 | 2863 | || is_std_allocator_allocate (ctx->call))) |
8e007055 | 2864 | { |
90bc2d09 | 2865 | const bool new_op_p = IDENTIFIER_NEW_OP_P (DECL_NAME (fun)); |
8e007055 JJ |
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); | |
72f76540 | 2871 | arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue, |
8e007055 | 2872 | non_constant_p, overflow_p); |
90bc2d09 NS |
2873 | /* Deleting a non-constant pointer has a better error message |
2874 | below. */ | |
2875 | if (new_op_p || i != 0) | |
2876 | VERIFY_CONSTANT (arg); | |
8e007055 JJ |
2877 | if (i == 0) |
2878 | arg0 = arg; | |
2879 | } | |
2880 | gcc_assert (arg0); | |
90bc2d09 | 2881 | if (new_op_p) |
8e007055 JJ |
2882 | { |
2883 | tree type = build_array_type_nelts (char_type_node, | |
2884 | tree_to_uhwi (arg0)); | |
815baade JJ |
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, | |
8e007055 JJ |
2890 | type); |
2891 | DECL_ARTIFICIAL (var) = 1; | |
2892 | TREE_STATIC (var) = 1; | |
a8db7887 JJ |
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 (var); | |
8e007055 | 2900 | ctx->global->heap_vars.safe_push (var); |
e6a29aab | 2901 | ctx->global->put_value (var, NULL_TREE); |
8e007055 JJ |
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 | { | |
815baade JJ |
2914 | if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) |
2915 | & OVL_OP_FLAG_VEC) | |
2916 | { | |
2917 | if (!ctx->quiet) | |
2918 | { | |
4f870007 | 2919 | auto_diagnostic_group d; |
815baade JJ |
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; | |
90bc2d09 | 2930 | ctx->global->destroy_value (var); |
815baade JJ |
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 | { | |
4f870007 | 2942 | auto_diagnostic_group d; |
815baade JJ |
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 | } | |
8e007055 | 2952 | DECL_NAME (var) = heap_deleted_identifier; |
90bc2d09 | 2953 | ctx->global->destroy_value (var); |
f74f6092 | 2954 | ctx->global->heap_dealloc_count++; |
8e007055 JJ |
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 | } | |
cf650568 JJ |
2973 | /* Allow placement new in std::construct_at, just return the second |
2974 | argument. */ | |
8412b939 JJ |
2975 | if (TREE_CODE (t) == CALL_EXPR |
2976 | && cxx_placement_new_fn (fun) | |
2ffc2645 | 2977 | && is_std_construct_at (ctx->call)) |
cf650568 JJ |
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); | |
72f76540 | 2984 | arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue, |
cf650568 | 2985 | non_constant_p, overflow_p); |
cf650568 JJ |
2986 | if (i == 1) |
2987 | arg1 = arg; | |
2805fcb3 JJ |
2988 | else |
2989 | VERIFY_CONSTANT (arg); | |
cf650568 JJ |
2990 | } |
2991 | gcc_assert (arg1); | |
2992 | return arg1; | |
2993 | } | |
22edf943 MP |
2994 | else if (cxx_dynamic_cast_fn_p (fun)) |
2995 | return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p); | |
2996 | ||
2b3ab879 | 2997 | if (!ctx->quiet) |
2d76680f | 2998 | { |
11399477 | 2999 | if (!lambda_static_thunk_p (fun)) |
84fa214d | 3000 | error_at (loc, "call to non-%<constexpr%> function %qD", fun); |
2d76680f PC |
3001 | explain_invalid_constexpr_fn (fun); |
3002 | } | |
3003 | *non_constant_p = true; | |
3004 | return t; | |
3005 | } | |
3006 | ||
86461cad JM |
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); | |
e8c48716 | 3015 | CONSTRUCTOR_NO_CLEARING (ctor) = true; |
e6a29aab | 3016 | ctx->global->put_value (new_ctx.object, ctor); |
86461cad JM |
3017 | ctx = &new_ctx; |
3018 | } | |
3019 | ||
b2287a4d MP |
3020 | /* We used to shortcut trivial constructor/op= here, but nowadays |
3021 | we can only get a trivial function here with -fno-elide-constructors. */ | |
e4692319 MP |
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); | |
2d76680f | 3027 | |
1595fe44 JM |
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); | |
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 | ||
81371eff JM |
3048 | /* We can't defer instantiating the function any longer. */ |
3049 | if (!DECL_INITIAL (fun) | |
59946a4c | 3050 | && (DECL_TEMPLOID_INSTANTIATION (fun) || DECL_DEFAULTED_FN (fun)) |
f65a3299 | 3051 | && !uid_sensitive_constexpr_evaluation_p ()) |
81371eff | 3052 | { |
f065303f JM |
3053 | location_t save_loc = input_location; |
3054 | input_location = loc; | |
81371eff | 3055 | ++function_depth; |
3a0bc47c | 3056 | if (ctx->manifestly_const_eval == mce_true) |
9aeadd8c | 3057 | FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true; |
59946a4c PP |
3058 | if (DECL_TEMPLOID_INSTANTIATION (fun)) |
3059 | instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false); | |
3060 | else | |
3061 | synthesize_method (fun); | |
81371eff | 3062 | --function_depth; |
f065303f | 3063 | input_location = save_loc; |
81371eff JM |
3064 | } |
3065 | ||
2d76680f | 3066 | /* If in direct recursive call, optimize definition search. */ |
c8816908 | 3067 | if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun) |
3e605b20 | 3068 | new_call.fundef = ctx->call->fundef; |
2d76680f PC |
3069 | else |
3070 | { | |
3071 | new_call.fundef = retrieve_constexpr_fundef (fun); | |
4ddcdbf9 | 3072 | if (new_call.fundef == NULL || new_call.fundef->body == NULL |
4f05d85a | 3073 | || new_call.fundef->result == error_mark_node |
4ddcdbf9 | 3074 | || fun == current_function_decl) |
2d76680f | 3075 | { |
2b3ab879 | 3076 | if (!ctx->quiet) |
2d76680f | 3077 | { |
4ddcdbf9 JM |
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) | |
ddd6d421 JM |
3084 | error_at (loc, "%qD called in a constant expression before its " |
3085 | "definition is complete", fun); | |
3086 | else if (DECL_INITIAL (fun)) | |
2d76680f | 3087 | { |
98e5a19a JM |
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); | |
2d76680f PC |
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 | } | |
60813a46 | 3101 | |
2d76680f PC |
3102 | depth_ok = push_cx_call_context (t); |
3103 | ||
caf17f3a | 3104 | /* Remember the object we are constructing or destructing. */ |
04e1749c | 3105 | tree new_obj = NULL_TREE; |
caf17f3a | 3106 | if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun)) |
04e1749c | 3107 | { |
caf17f3a | 3108 | /* In a cdtor, it should be the first `this' argument. |
04e1749c MP |
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); | |
1189c038 | 3112 | new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj); |
64da1b76 PP |
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 | } | |
04e1749c MP |
3128 | } |
3129 | ||
12d9ce19 | 3130 | tree result = NULL_TREE; |
2d76680f | 3131 | |
12d9ce19 | 3132 | constexpr_call *entry = NULL; |
4a58d2fe | 3133 | if (depth_ok && !non_constant_args && ctx->strict) |
2d76680f | 3134 | { |
cce3ae91 JJ |
3135 | new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef); |
3136 | new_call.hash | |
3137 | = iterative_hash_template_arg (new_call.bindings, new_call.hash); | |
3138 | new_call.hash | |
13de99bc | 3139 | = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash); |
12d9ce19 JM |
3140 | |
3141 | /* If we have seen this call before, we are done. */ | |
3142 | maybe_initialize_constexpr_call_table (); | |
6ec8079e | 3143 | bool insert = depth_ok < constexpr_cache_depth; |
12d9ce19 | 3144 | constexpr_call **slot |
6ec8079e AO |
3145 | = constexpr_call_table->find_slot (&new_call, |
3146 | insert ? INSERT : NO_INSERT); | |
3147 | entry = slot ? *slot : NULL; | |
12d9ce19 JM |
3148 | if (entry == NULL) |
3149 | { | |
7ffc7de5 | 3150 | /* Only cache up to constexpr_cache_depth to limit memory use. */ |
6ec8079e | 3151 | if (insert) |
7ffc7de5 JM |
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 | } | |
12d9ce19 | 3159 | } |
7ffc7de5 JM |
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. */ | |
12d9ce19 JM |
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; | |
2d76680f PC |
3173 | } |
3174 | ||
3175 | if (!depth_ok) | |
3176 | { | |
2b3ab879 | 3177 | if (!ctx->quiet) |
84fa214d | 3178 | error ("%<constexpr%> evaluation depth exceeds maximum of %d (use " |
7e7a6ed7 | 3179 | "%<-fconstexpr-depth=%> to increase the maximum)", |
2d76680f PC |
3180 | max_constexpr_depth); |
3181 | *non_constant_p = true; | |
12d9ce19 | 3182 | result = error_mark_node; |
2d76680f PC |
3183 | } |
3184 | else | |
3185 | { | |
c7ac1de5 | 3186 | bool cacheable = !!entry; |
de54de93 JM |
3187 | if (result && result != error_mark_node) |
3188 | /* OK */; | |
3189 | else if (!DECL_SAVED_TREE (fun)) | |
3190 | { | |
1f1c4322 | 3191 | /* When at_eof >= 3, cgraph has started throwing away |
de54de93 JM |
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. */ | |
1f1c4322 | 3195 | gcc_assert (at_eof >= 3 && ctx->quiet); |
de54de93 JM |
3196 | *non_constant_p = true; |
3197 | } | |
f65a3299 | 3198 | else if (tree copy = get_fundef_copy (new_call.fundef)) |
3e605b20 | 3199 | { |
c0daf32d | 3200 | tree body, parms, res; |
620adbec | 3201 | releasing_vec ctors; |
0567dcd2 | 3202 | |
c0daf32d | 3203 | /* Reuse or create a new unshared copy of this function's body. */ |
97f3003f JM |
3204 | body = TREE_PURPOSE (copy); |
3205 | parms = TREE_VALUE (copy); | |
3206 | res = TREE_TYPE (copy); | |
0567dcd2 MP |
3207 | |
3208 | /* Associate the bindings with the remapped parms. */ | |
3209 | tree bound = new_call.bindings; | |
3210 | tree remapped = parms; | |
3c961dc7 | 3211 | for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i) |
60813a46 | 3212 | { |
3c961dc7 | 3213 | tree arg = TREE_VEC_ELT (bound, i); |
4953b790 JM |
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. */ | |
db38a029 | 3219 | arg = unshare_expr_without_location (arg); |
4953b790 | 3220 | TREE_VEC_ELT (bound, i) = arg; |
5afd90c5 JJ |
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 (arg); | |
3226 | if (TREE_CODE (arg) == CONSTRUCTOR) | |
3227 | vec_safe_push (ctors, arg); | |
4953b790 | 3228 | } |
e6a29aab | 3229 | ctx->global->put_value (remapped, arg); |
0567dcd2 | 3230 | remapped = DECL_CHAIN (remapped); |
60813a46 | 3231 | } |
e0659b54 JM |
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 (remapped, integer_zero_node); | |
3239 | } | |
3240 | else | |
3241 | { | |
3242 | gcc_assert (seen_error ()); | |
3243 | *non_constant_p = true; | |
3244 | } | |
0567dcd2 | 3245 | /* Add the RESULT_DECL to the values map, too. */ |
cd3ca6cb | 3246 | gcc_assert (!DECL_BY_REFERENCE (res)); |
e6a29aab | 3247 | ctx->global->put_value (res, NULL_TREE); |
60813a46 | 3248 | |
9fdbd7d6 NS |
3249 | /* Remember the current call we're evaluating. */ |
3250 | constexpr_ctx call_ctx = *ctx; | |
3251 | call_ctx.call = &new_call; | |
f74f6092 JJ |
3252 | unsigned save_heap_alloc_count = ctx->global->heap_vars.length (); |
3253 | unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count; | |
c0daf32d | 3254 | |
6851e342 MP |
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 | ||
caf17f3a MP |
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, new_obj, /*readonly_p=*/false, | |
3264 | non_constant_p, overflow_p); | |
3265 | ||
1d260ab0 NS |
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 | ||
0567dcd2 | 3282 | tree jump_target = NULL_TREE; |
9fdbd7d6 | 3283 | cxx_eval_constant_expression (&call_ctx, body, |
72f76540 | 3284 | vc_discard, non_constant_p, overflow_p, |
0567dcd2 | 3285 | &jump_target); |
60813a46 | 3286 | |
0567dcd2 | 3287 | if (DECL_CONSTRUCTOR_P (fun)) |
1d260ab0 NS |
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; | |
0567dcd2 MP |
3294 | else if (VOID_TYPE_P (TREE_TYPE (res))) |
3295 | result = void_node; | |
3296 | else | |
3297 | { | |
e6a29aab | 3298 | result = ctx->global->get_value (res); |
5ce08ecb AO |
3299 | if (result == NULL_TREE && !*non_constant_p |
3300 | && !DECL_DESTRUCTOR_P (fun)) | |
60813a46 | 3301 | { |
0567dcd2 | 3302 | if (!ctx->quiet) |
84fa214d | 3303 | error ("%<constexpr%> call flows off the end " |
0567dcd2 MP |
3304 | "of the function"); |
3305 | *non_constant_p = true; | |
60813a46 | 3306 | } |
60813a46 | 3307 | } |
0567dcd2 | 3308 | |
04e1749c MP |
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. */ | |
caf17f3a MP |
3313 | if (new_obj && DECL_CONSTRUCTOR_P (fun)) |
3314 | cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true, | |
3315 | non_constant_p, overflow_p); | |
04e1749c | 3316 | |
9fdbd7d6 | 3317 | /* Remove the parms/result from the values map. */ |
90bc2d09 | 3318 | destroy_value_checked (ctx, res, non_constant_p); |
0567dcd2 | 3319 | for (tree parm = parms; parm; parm = TREE_CHAIN (parm)) |
90bc2d09 | 3320 | destroy_value_checked (ctx, parm, non_constant_p); |
c0daf32d | 3321 | |
620adbec JM |
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 (c); | |
3328 | } | |
3329 | ||
c0daf32d PP |
3330 | /* Make the unshared function copy we used available for re-use. */ |
3331 | save_fundef_copy (fun, copy); | |
f74f6092 JJ |
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. */ | |
c7ac1de5 | 3339 | if (cacheable |
f74f6092 JJ |
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 | } | |
b2562229 PP |
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 | |
bb1f0b50 | 3363 | && CLASS_TYPE_P (TREE_TYPE (res)) |
b2562229 | 3364 | && !is_empty_class (TREE_TYPE (res))) |
4f6bc28f | 3365 | if (replace_decl (&result, res, ctx->object)) |
b2562229 | 3366 | cacheable = false; |
c7ac1de5 JM |
3367 | |
3368 | /* Only cache a permitted result of a constant expression. */ | |
3369 | if (cacheable && !reduced_constant_expression_p (result)) | |
3370 | cacheable = false; | |
3e605b20 | 3371 | } |
aaa26bf4 JM |
3372 | else |
3373 | /* Couldn't get a function copy to evaluate. */ | |
3374 | *non_constant_p = true; | |
60813a46 | 3375 | |
2d76680f PC |
3376 | if (result == error_mark_node) |
3377 | *non_constant_p = true; | |
52228180 | 3378 | if (*non_constant_p || *overflow_p) |
12d9ce19 | 3379 | result = error_mark_node; |
0567dcd2 | 3380 | else if (!result) |
60813a46 | 3381 | result = void_node; |
12d9ce19 | 3382 | if (entry) |
f74f6092 | 3383 | entry->result = cacheable ? result : error_mark_node; |
2d76680f PC |
3384 | } |
3385 | ||
7906797e MP |
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 | |
b04445d4 | 3394 | && (cxx_dialect < cxx20 |
7906797e | 3395 | || !DECL_CONSTRUCTOR_P (fun))) |
ecc57615 | 3396 | clear_no_implicit_zero (result); |
f64e0c02 | 3397 | |
2d76680f | 3398 | pop_cx_call_context (); |
9b9eb42a | 3399 | return result; |
2d76680f PC |
3400 | } |
3401 | ||
7906797e MP |
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. */ | |
2d76680f PC |
3406 | |
3407 | bool | |
3408 | reduced_constant_expression_p (tree t) | |
3409 | { | |
4930c53e MP |
3410 | if (t == NULL_TREE) |
3411 | return false; | |
3412 | ||
2d76680f PC |
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. */ | |
fae00a0a | 3421 | tree field; |
c7ac1de5 JM |
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. */ | |
2cc41601 | 3425 | return false; |
e8c48716 | 3426 | if (CONSTRUCTOR_NO_CLEARING (t)) |
6f11ddd8 | 3427 | { |
2cc41601 | 3428 | if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) |
baf05d54 PP |
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; | |
fae00a0a | 3435 | for (auto &e: CONSTRUCTOR_ELTS (t)) |
baf05d54 | 3436 | { |
fae00a0a | 3437 | if (!reduced_constant_expression_p (e.value)) |
baf05d54 | 3438 | return false; |
fae00a0a | 3439 | if (array_index_cmp (cursor, e.index) != 0) |
baf05d54 | 3440 | return false; |
fae00a0a JM |
3441 | if (TREE_CODE (e.index) == RANGE_EXPR) |
3442 | cursor = TREE_OPERAND (e.index, 1); | |
baf05d54 PP |
3443 | cursor = int_const_binop (PLUS_EXPR, cursor, size_one_node); |
3444 | } | |
3445 | if (find_array_ctor_elt (t, max) == -1) | |
3446 | return false; | |
3447 | goto ok; | |
3448 | } | |
b04445d4 | 3449 | else if (cxx_dialect >= cxx20 |
b599bf9d PP |
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 | } | |
6f11ddd8 | 3458 | else |
0c7bce0a | 3459 | field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t))); |
6f11ddd8 | 3460 | } |
7368cfa4 JM |
3461 | else |
3462 | field = NULL_TREE; | |
fae00a0a | 3463 | for (auto &e: CONSTRUCTOR_ELTS (t)) |
125db6a1 | 3464 | { |
4930c53e MP |
3465 | /* If VAL is null, we're in the middle of initializing this |
3466 | element. */ | |
fae00a0a | 3467 | if (!reduced_constant_expression_p (e.value)) |
125db6a1 | 3468 | return false; |
9efe4e15 JM |
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. */ | |
fae00a0a | 3476 | for (; field && e.index != field; |
0c7bce0a | 3477 | field = next_subobject_field (DECL_CHAIN (field))) |
d6ff2207 MP |
3478 | if (!is_really_empty_class (TREE_TYPE (field), |
3479 | /*ignore_vptr*/false)) | |
3480 | return false; | |
7368cfa4 | 3481 | if (field) |
0c7bce0a | 3482 | field = next_subobject_field (DECL_CHAIN (field)); |
125db6a1 | 3483 | } |
7906797e | 3484 | /* There could be a non-empty field at the end. */ |
0c7bce0a | 3485 | for (; field; field = next_subobject_field (DECL_CHAIN (field))) |
7906797e MP |
3486 | if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false)) |
3487 | return false; | |
baf05d54 | 3488 | ok: |
7906797e | 3489 | if (CONSTRUCTOR_NO_CLEARING (t)) |
7368cfa4 | 3490 | /* All the fields are initialized. */ |
e8c48716 | 3491 | CONSTRUCTOR_NO_CLEARING (t) = false; |
2d76680f PC |
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 | ||
b78cedc6 MP |
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 (op_loc, "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 | ||
2d76680f | 3555 | /* Some expressions may have constant operands but are not constant |
7d75ea04 JJ |
3556 | themselves, such as 1/0. Call this function to check for that |
3557 | condition. | |
2d76680f PC |
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 | { | |
596334fa JM |
3568 | if (!*non_constant_p && !reduced_constant_expression_p (t) |
3569 | && t != void_node) | |
2d76680f PC |
3570 | { |
3571 | if (!allow_non_constant) | |
b78cedc6 MP |
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 | } | |
2d76680f PC |
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 | ||
253a921b MP |
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) | |
5342156c MP |
3618 | permerror (loc, "right operand of shift expression %q+E is negative", |
3619 | build2_loc (loc, code, type, lhs, rhs)); | |
3620 | return (!flag_permissive || ctx->quiet); | |
253a921b MP |
3621 | } |
3622 | if (compare_tree_int (rhs, uprec) >= 0) | |
3623 | { | |
3624 | if (!ctx->quiet) | |
a9c697b8 MS |
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, lhs, rhs), uprec); | |
5342156c | 3628 | return (!flag_permissive || ctx->quiet); |
253a921b MP |
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; | |
8ee09943 | 3635 | otherwise, the behavior is undefined. |
b04445d4 | 3636 | For C++20: |
8ee09943 JJ |
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 | |
d7651113 | 3640 | && !TYPE_OVERFLOW_WRAPS (lhstype) |
8ee09943 | 3641 | && cxx_dialect >= cxx11 |
b04445d4 | 3642 | && cxx_dialect < cxx20) |
253a921b MP |
3643 | { |
3644 | if (tree_int_cst_sgn (lhs) == -1) | |
3645 | { | |
3646 | if (!ctx->quiet) | |
5342156c MP |
3647 | permerror (loc, |
3648 | "left operand of shift expression %q+E is negative", | |
3649 | build2_loc (loc, code, type, lhs, rhs)); | |
3650 | return (!flag_permissive || ctx->quiet); | |
253a921b MP |
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, t)) | |
3665 | { | |
3666 | if (!ctx->quiet) | |
5342156c MP |
3667 | permerror (loc, "shift expression %q+E overflows", |
3668 | build2_loc (loc, code, type, lhs, rhs)); | |
3669 | return (!flag_permissive || ctx->quiet); | |
253a921b MP |
3670 | } |
3671 | } | |
3672 | return false; | |
3673 | } | |
3674 | ||
2d76680f PC |
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 | |
3e605b20 | 3681 | cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t, |
12d9ce19 | 3682 | bool /*lval*/, |
2d76680f PC |
3683 | bool *non_constant_p, bool *overflow_p) |
3684 | { | |
3685 | tree r; | |
3686 | tree orig_arg = TREE_OPERAND (t, 0); | |
72f76540 | 3687 | tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue, |
12d9ce19 | 3688 | non_constant_p, overflow_p); |
2d76680f | 3689 | VERIFY_CONSTANT (arg); |
f899317e JM |
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, arg); | |
3700 | } | |
2d76680f PC |
3701 | VERIFY_CONSTANT (r); |
3702 | return r; | |
3703 | } | |
3704 | ||
ea8661cd JJ |
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))); | |
72f76540 JM |
3733 | nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue, |
3734 | non_constant_p, overflow_p); | |
ea8661cd JJ |
3735 | if (*non_constant_p) |
3736 | return NULL_TREE; | |
3737 | /* Don't fold an out-of-bound access. */ | |
3738 | if (!tree_int_cst_le (t, 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, ARRAY_REF, type, TREE_OPERAND (lhs, 0), | |
3752 | t, NULL_TREE, NULL_TREE); | |
3753 | t = cp_build_addr_expr (t, tf_warning_or_error); | |
3754 | t = cp_fold_convert (orig_type, t); | |
72f76540 | 3755 | return cxx_eval_constant_expression (ctx, t, vc_prvalue, |
ea8661cd JJ |
3756 | non_constant_p, overflow_p); |
3757 | } | |
3758 | ||
3759 | return NULL_TREE; | |
3760 | } | |
3761 | ||
51d464b6 JJ |
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), ADDR_EXPR, TREE_TYPE (op0), r); | |
3792 | } | |
3793 | ||
2d76680f PC |
3794 | /* Subroutine of cxx_eval_constant_expression. |
3795 | Like cxx_eval_unary_expression, except for binary expressions. */ | |
3796 | ||
3797 | static tree | |
3e605b20 | 3798 | cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t, |
72f76540 | 3799 | value_cat lval, |
2d76680f PC |
3800 | bool *non_constant_p, bool *overflow_p) |
3801 | { | |
618d6c1c | 3802 | tree r = NULL_TREE; |
2d76680f PC |
3803 | tree orig_lhs = TREE_OPERAND (t, 0); |
3804 | tree orig_rhs = TREE_OPERAND (t, 1); | |
3805 | tree lhs, rhs; | |
72f76540 | 3806 | lhs = cxx_eval_constant_expression (ctx, orig_lhs, vc_prvalue, |
5a804683 | 3807 | non_constant_p, overflow_p); |
c8a66fc9 JM |
3808 | /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer |
3809 | subtraction. */ | |
3810 | if (*non_constant_p) | |
3811 | return t; | |
72f76540 | 3812 | rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue, |
5a804683 | 3813 | non_constant_p, overflow_p); |
c8a66fc9 JM |
3814 | if (*non_constant_p) |
3815 | return t; | |
f899317e JM |
3816 | |
3817 | location_t loc = EXPR_LOCATION (t); | |
3818 | enum tree_code code = TREE_CODE (t); | |
3819 | tree type = TREE_TYPE (t); | |
618d6c1c PP |
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) | |
9b0607de JM |
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 | } | |
618d6c1c PP |
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); | |
dd5dda56 JM |
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); | |
618d6c1c | 3852 | } |
51d464b6 JJ |
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 (lhs)) | |
3858 | lhs = fold_convert (TREE_TYPE (lhs), lhso); | |
3859 | if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs)) | |
3860 | rhs = fold_convert (TREE_TYPE (rhs), rhso); | |
3861 | } | |
8bada5cd MS |
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); | |
bf533db8 | 3867 | *non_constant_p = true; |
8bada5cd MS |
3868 | return t; |
3869 | } | |
ea8661cd JJ |
3870 | else if (code == POINTER_PLUS_EXPR) |
3871 | r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p, | |
3872 | overflow_p); | |
b7689b96 JM |
3873 | else if (code == SPACESHIP_EXPR) |
3874 | { | |
4ed1dc12 | 3875 | r = genericize_spaceship (loc, type, lhs, rhs); |
5c64df80 JJ |
3876 | return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, |
3877 | overflow_p); | |
b7689b96 | 3878 | } |
618d6c1c PP |
3879 | |
3880 | if (r == NULL_TREE) | |
53caa472 | 3881 | { |
3a0bc47c | 3882 | if (ctx->manifestly_const_eval == mce_true |
53caa472 JM |
3883 | && (flag_constexpr_fp_except |
3884 | || TREE_CODE (type) != REAL_TYPE)) | |
f9e900ce JJ |
3885 | { |
3886 | auto ofcc = make_temp_override (folding_cxx_constexpr, true); | |
3887 | r = fold_binary_initializer_loc (loc, code, type, lhs, rhs); | |
3888 | } | |
53caa472 JM |
3889 | else |
3890 | r = fold_binary_loc (loc, code, type, lhs, rhs); | |
3891 | } | |
618d6c1c | 3892 | |
4866b2f5 JJ |
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 (wi::to_wide (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 | ||
f899317e JM |
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, lhs, rhs); | |
3914 | } | |
253a921b MP |
3915 | else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs)) |
3916 | *non_constant_p = true; | |
c8a66fc9 JM |
3917 | /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to |
3918 | a local array in a constexpr function. */ | |
71a93b08 | 3919 | bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs)); |
caee690e | 3920 | if (!ptr) |
134efa82 | 3921 | VERIFY_CONSTANT (r); |
2d76680f PC |
3922 | return r; |
3923 | } | |
3924 | ||
3925 | /* Subroutine of cxx_eval_constant_expression. | |
6851e342 | 3926 | Attempt to evaluate condition expressions. */ |
2d76680f PC |
3927 | |
3928 | static tree | |
3e605b20 | 3929 | cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t, |
72f76540 | 3930 | value_cat lval, |
56632b27 JM |
3931 | bool *non_constant_p, bool *overflow_p, |
3932 | tree *jump_target) | |
2d76680f | 3933 | { |
3e605b20 | 3934 | tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
72f76540 | 3935 | vc_prvalue, |
5a804683 | 3936 | non_constant_p, overflow_p); |
2d76680f | 3937 | VERIFY_CONSTANT (val); |
117c6426 JJ |
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. */ | |
3a0bc47c | 3946 | if (ctx->manifestly_const_eval == mce_unknown) |
117c6426 JJ |
3947 | { |
3948 | *non_constant_p = true; | |
3949 | return t; | |
3950 | } | |
3a0bc47c PP |
3951 | val = constant_boolean_node (ctx->manifestly_const_eval == mce_true, |
3952 | boolean_type_node); | |
117c6426 | 3953 | } |
2d76680f | 3954 | /* Don't VERIFY_CONSTANT the other operands. */ |
6851e342 MP |
3955 | const bool zero_p = integer_zerop (val); |
3956 | if (zero_p) | |
43574e4f JJ |
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; | |
6851e342 MP |
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 | ||
ecd11aca MP |
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); | |
43574e4f JJ |
3981 | return cxx_eval_constant_expression (ctx, val, lval, non_constant_p, |
3982 | overflow_p, jump_target); | |
2d76680f PC |
3983 | } |
3984 | ||
f370e36d JJ |
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), | |
72f76540 | 3997 | vc_prvalue, |
f370e36d JJ |
3998 | non_constant_p, overflow_p); |
3999 | VERIFY_CONSTANT (arg1); | |
4000 | tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), | |
72f76540 | 4001 | vc_prvalue, |
f370e36d JJ |
4002 | non_constant_p, overflow_p); |
4003 | VERIFY_CONSTANT (arg2); | |
4004 | tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2), | |
72f76540 | 4005 | vc_prvalue, |
f370e36d JJ |
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, VEC_COND_EXPR, type, arg1, arg2, arg3); | |
4019 | } | |
4020 | VERIFY_CONSTANT (r); | |
4021 | return r; | |
4022 | } | |
4023 | ||
ceaaf873 JM |
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 (key, 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 (key, lo)) | |
4041 | return -1; | |
4042 | else if (tree_int_cst_lt (hi, 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 | |
582d2481 | 4056 | find_array_ctor_elt (tree ary, tree dindex, bool insert) |
ceaaf873 JM |
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 (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 | { | |
fe217ba0 | 4072 | tree cindex = (*elts)[end - 1].index; |
c7c09af8 JJ |
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) | |
ceaaf873 JM |
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; | |
a7ccb9e7 JM |
4111 | constructor_elt &elt = (*elts)[middle]; |
4112 | tree idx = elt.index; | |
ceaaf873 | 4113 | |
a7ccb9e7 | 4114 | int cmp = array_index_cmp (dindex, idx); |
ceaaf873 JM |
4115 | if (cmp < 0) |
4116 | end = middle; | |
4117 | else if (cmp > 0) | |
4118 | begin = middle + 1; | |
4119 | else | |
a7ccb9e7 JM |
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); | |
fe217ba0 JJ |
4127 | tree value = elt.value; |
4128 | dindex = fold_convert (sizetype, dindex); | |
a7ccb9e7 JM |
4129 | if (tree_int_cst_lt (lo, 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; | |
fe217ba0 | 4142 | e.value = unshare_constructor (value); |
a7ccb9e7 JM |
4143 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, 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 (dindex, 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); | |
fe217ba0 JJ |
4158 | e.value = unshare_constructor (value); |
4159 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e); | |
a7ccb9e7 JM |
4160 | } |
4161 | } | |
4162 | return middle; | |
4163 | } | |
ceaaf873 JM |
4164 | } |
4165 | ||
4166 | if (insert) | |
4167 | { | |
4168 | constructor_elt e = { dindex, NULL_TREE }; | |
4169 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e); | |
4170 | return end; | |
4171 | } | |
4172 | ||
4173 | return -1; | |
4174 | } | |
4175 | ||
37244b21 PP |
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 * | |
077dd9b3 | 4183 | get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1) |
37244b21 PP |
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 | { | |
e069285c PP |
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 (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 | ||
37244b21 PP |
4214 | HOST_WIDE_INT i = find_array_ctor_elt (ctor, 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 | { | |
94ff4c9d JM |
4223 | gcc_assert (TREE_CODE (index) == FIELD_DECL |
4224 | && (same_type_ignoring_top_level_qualifiers_p | |
4225 | (DECL_CONTEXT (index), TREE_TYPE (ctor)))); | |
37244b21 PP |
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), 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 (bit_position (index), | |
4244 | 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), idx, &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, NULL_TREE }; | |
4273 | ||
4274 | vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce); | |
4275 | cep = CONSTRUCTOR_ELT (ctor, idx); | |
4276 | } | |
4277 | found:; | |
4278 | ||
4279 | return cep; | |
4280 | } | |
4281 | } | |
4282 | ||
abdc16c8 MS |
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 | |
043666e0 | 4287 | diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index) |
abdc16c8 MS |
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); | |
043666e0 | 4296 | STRIP_ANY_LOCATION_WRAPPER (array); |
abdc16c8 MS |
4297 | if (DECL_P (array)) |
4298 | { | |
4f870007 | 4299 | auto_diagnostic_group d; |
08b3748c | 4300 | if (TYPE_DOMAIN (arraytype)) |
043666e0 JM |
4301 | error_at (loc, "array subscript value %qE is outside the bounds " |
4302 | "of array %qD of type %qT", sidx, array, arraytype); | |
08b3748c | 4303 | else |
043666e0 JM |
4304 | error_at (loc, "nonzero array subscript %qE is used with array %qD of " |
4305 | "type %qT with unknown bounds", sidx, array, arraytype); | |
abdc16c8 MS |
4306 | inform (DECL_SOURCE_LOCATION (array), "declared here"); |
4307 | } | |
08b3748c | 4308 | else if (TYPE_DOMAIN (arraytype)) |
043666e0 JM |
4309 | error_at (loc, "array subscript value %qE is outside the bounds " |
4310 | "of array type %qT", sidx, arraytype); | |
08b3748c | 4311 | else |
043666e0 JM |
4312 | error_at (loc, "nonzero array subscript %qE is used with array of type %qT " |
4313 | "with unknown bounds", sidx, arraytype); | |
abdc16c8 MS |
4314 | } |
4315 | } | |
ceaaf873 | 4316 | |
74f8705e MP |
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. */ | |
72f76540 | 4338 | nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue, |
74f8705e MP |
4339 | non_constant_p, overflow_p); |
4340 | return nelts; | |
4341 | } | |
4342 | ||
d6b46fca NS |
4343 | /* Extract element INDEX consisting of CHARS_PER_ELT chars from |
4344 | STRING_CST STRING. */ | |
4345 | ||
4346 | static tree | |
4347 | extract_string_elt (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 | ||
043666e0 JM |
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 | { | |
f9d0ca40 | 4372 | location_t loc = cp_expr_loc_or_input_loc (t); |
043666e0 JM |
4373 | tree ary = TREE_OPERAND (t, 0); |
4374 | t = TREE_OPERAND (t, 1); | |
72f76540 | 4375 | tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue, |
043666e0 JM |
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, 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 (index, nelts) | |
4392 | : !tree_int_cst_lt (index, nelts)) | |
4393 | { | |
4394 | diag_array_subscript (loc, ctx, ary, index); | |
4395 | *non_constant_p = true; | |
4396 | return t; | |
4397 | } | |
4398 | ||
4399 | return index; | |
4400 | } | |
4401 | ||
2d76680f PC |
4402 | /* Subroutine of cxx_eval_constant_expression. |
4403 | Attempt to reduce a reference to an array slot. */ | |
4404 | ||
4405 | static tree | |
3e605b20 | 4406 | cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, |
72f76540 | 4407 | value_cat lval, |
2d76680f PC |
4408 | bool *non_constant_p, bool *overflow_p) |
4409 | { | |
4410 | tree oldary = TREE_OPERAND (t, 0); | |
3e605b20 | 4411 | tree ary = cxx_eval_constant_expression (ctx, oldary, |
92a596e8 | 4412 | lval, |
5a804683 | 4413 | non_constant_p, overflow_p); |
2d76680f PC |
4414 | if (*non_constant_p) |
4415 | return t; | |
bc2687dd JJ |
4416 | if (!lval |
4417 | && TREE_CODE (ary) == VIEW_CONVERT_EXPR | |
043666e0 JM |
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); | |
3b9997bb | 4421 | |
043666e0 JM |
4422 | tree oldidx = TREE_OPERAND (t, 1); |
4423 | tree index = eval_and_check_array_index (ctx, t, lval, | |
4424 | non_constant_p, overflow_p); | |
4425 | if (*non_constant_p) | |
4426 | return t; | |
5453bfed | 4427 | |
bc2a38df JJ |
4428 | if (lval && ary == oldary && index == oldidx) |
4429 | return t; | |
72f76540 JM |
4430 | else if (lval == vc_discard) |
4431 | return t; | |
bc2a38df JJ |
4432 | else if (lval) |
4433 | return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL); | |
4434 | ||
043666e0 JM |
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 | ||
ceaaf873 | 4456 | bool found; |
043666e0 | 4457 | HOST_WIDE_INT i = 0; |
ceaaf873 JM |
4458 | if (TREE_CODE (ary) == CONSTRUCTOR) |
4459 | { | |
4460 | HOST_WIDE_INT ix = find_array_ctor_elt (ary, index); | |
4461 | found = (ix >= 0); | |
4462 | if (found) | |
4463 | i = ix; | |
3b9997bb | 4464 | } |
ceaaf873 | 4465 | else |
043666e0 JM |
4466 | { |
4467 | i = tree_to_shwi (index); | |
4468 | found = (i < len); | |
4469 | } | |
3b9997bb | 4470 | |
fd2bfee5 JM |
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); | |
fd2bfee5 | 4478 | else |
d6b46fca NS |
4479 | r = extract_string_elt (ary, elem_nchars, i); |
4480 | ||
fd2bfee5 JM |
4481 | if (r) |
4482 | /* Don't VERIFY_CONSTANT here. */ | |
4483 | return r; | |
2d76680f | 4484 | |
fd2bfee5 | 4485 | /* Otherwise the element doesn't have a value yet. */ |
2d76680f | 4486 | } |
3b9997bb | 4487 | |
fd2bfee5 JM |
4488 | /* Not found. */ |
4489 | ||
a426b91b PP |
4490 | if (is_really_empty_class (elem_type, /*ignore_vptr*/false)) |
4491 | return build_constructor (elem_type, NULL); | |
4492 | ||
fd2bfee5 | 4493 | if (TREE_CODE (ary) == CONSTRUCTOR |
e8c48716 | 4494 | && CONSTRUCTOR_NO_CLEARING (ary)) |
2d76680f | 4495 | { |
fd2bfee5 JM |
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; | |
2d76680f | 4503 | } |
fd2bfee5 JM |
4504 | |
4505 | /* If it's within the array bounds but doesn't have an explicit | |
0712ea63 JM |
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; | |
0df73bee | 4509 | constexpr_ctx new_ctx; |
a426b91b | 4510 | if (CP_AGGREGATE_TYPE_P (elem_type)) |
0712ea63 JM |
4511 | { |
4512 | tree empty_ctor = build_constructor (init_list_type_node, NULL); | |
4513 | val = digest_init (elem_type, empty_ctor, tf_warning_or_error); | |
a42f8120 MP |
4514 | } |
4515 | else | |
4516 | val = build_value_init (elem_type, tf_warning_or_error); | |
4517 | ||
6e424feb MP |
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) | |
a42f8120 | 4524 | { |
0df73bee | 4525 | new_ctx = *ctx; |
6e424feb MP |
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; | |
0df73bee MP |
4530 | new_ctx.ctor = build_constructor (elem_type, NULL); |
4531 | ctx = &new_ctx; | |
0712ea63 | 4532 | } |
0df73bee MP |
4533 | t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p, |
4534 | overflow_p); | |
6e424feb | 4535 | if (new_ctor && t != ctx->ctor) |
0df73bee MP |
4536 | free_constructor (ctx->ctor); |
4537 | return t; | |
2d76680f PC |
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 | |
3e605b20 | 4544 | cxx_eval_component_reference (const constexpr_ctx *ctx, tree t, |
72f76540 | 4545 | value_cat lval, |
2d76680f PC |
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); | |
3e605b20 | 4553 | tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
92a596e8 | 4554 | lval, |
5a804683 | 4555 | non_constant_p, overflow_p); |
7107ea6f PP |
4556 | if (*non_constant_p) |
4557 | return t; | |
a7f8415c | 4558 | if (INDIRECT_REF_P (whole) |
bf533db8 JJ |
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 | } | |
8bada5cd | 4566 | |
dcdbc004 JM |
4567 | if (TREE_CODE (whole) == PTRMEM_CST) |
4568 | whole = cplus_expand_constant (whole); | |
2d76680f PC |
4569 | if (whole == orig_whole) |
4570 | return t; | |
72f76540 JM |
4571 | if (lval == vc_discard) |
4572 | return t; | |
92a596e8 | 4573 | if (lval) |
2d76680f PC |
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. */ | |
7107ea6f | 4578 | if (TREE_CODE (whole) != CONSTRUCTOR) |
2d76680f | 4579 | { |
2b3ab879 | 4580 | if (!ctx->quiet) |
2d76680f PC |
4581 | error ("%qE is not a constant expression", orig_whole); |
4582 | *non_constant_p = true; | |
7107ea6f | 4583 | return t; |
2d76680f | 4584 | } |
7107ea6f PP |
4585 | if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole)) |
4586 | && DECL_MUTABLE_P (part)) | |
2d76680f | 4587 | { |
2b3ab879 | 4588 | if (!ctx->quiet) |
2d76680f PC |
4589 | error ("mutable %qD is not usable in a constant expression", part); |
4590 | *non_constant_p = true; | |
7107ea6f | 4591 | return t; |
2d76680f | 4592 | } |
1d260ab0 | 4593 | |
2db613e5 | 4594 | bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole)); |
2d76680f PC |
4595 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) |
4596 | { | |
2db613e5 JM |
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) | |
60813a46 JM |
4601 | { |
4602 | if (value) | |
5b884e94 JJ |
4603 | { |
4604 | STRIP_ANY_LOCATION_WRAPPER (value); | |
4605 | return value; | |
4606 | } | |
60813a46 JM |
4607 | else |
4608 | /* We're in the middle of initializing it. */ | |
4609 | break; | |
4610 | } | |
2d76680f | 4611 | } |
1d260ab0 | 4612 | if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE) |
2d76680f | 4613 | { |
1d260ab0 | 4614 | if (CONSTRUCTOR_NELTS (whole) > 0) |
b599bf9d | 4615 | { |
1d260ab0 NS |
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 | } | |
b599bf9d | 4641 | } |
2d76680f PC |
4642 | } |
4643 | ||
188e53bd JM |
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. */ | |
dbcd32f8 | 4646 | if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
188e53bd JM |
4647 | return build_constructor (TREE_TYPE (t), NULL); |
4648 | ||
2db613e5 JM |
4649 | gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole))); |
4650 | ||
e8c48716 | 4651 | if (CONSTRUCTOR_NO_CLEARING (whole)) |
3e605b20 JM |
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 | |
188e53bd | 4655 | error. */ |
2b3ab879 | 4656 | if (!ctx->quiet) |
3e605b20 JM |
4657 | error ("accessing uninitialized member %qD", part); |
4658 | *non_constant_p = true; | |
4659 | return t; | |
4660 | } | |
4661 | ||
2d76680f PC |
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); | |
3e605b20 | 4664 | return cxx_eval_constant_expression (ctx, value, |
92a596e8 | 4665 | lval, |
5a804683 | 4666 | non_constant_p, overflow_p); |
2d76680f PC |
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 | |
3e605b20 | 4674 | cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t, |
72f76540 | 4675 | value_cat lval, |
2d76680f PC |
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; | |
3e605b20 | 4682 | tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
92a596e8 | 4683 | lval, |
5a804683 | 4684 | non_constant_p, overflow_p); |
2d76680f PC |
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 | { | |
2b3ab879 | 4696 | if (!ctx->quiet) |
2d76680f PC |
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 | ||
4c334e0e JJ |
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 | } | |
2d76680f PC |
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); | |
5b884e94 | 4724 | STRIP_ANY_LOCATION_WRAPPER (value); |
2d76680f PC |
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 | ||
896048cf JJ |
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; | |
79ff5345 JJ |
4846 | if (TREE_CODE (type) == ARRAY_TYPE) |
4847 | return check_bit_cast_type (ctx, loc, TREE_TYPE (type), orig_type); | |
896048cf JJ |
4848 | return false; |
4849 | } | |
4850 | ||
c57c910c JJ |
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, value, 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), j); | |
4961 | } | |
4962 | ||
896048cf JJ |
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, 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 | ||
72f76540 | 4981 | tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue, |
896048cf JJ |
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 (mask, ~0, (size_t) len); | |
5025 | ||
5026 | if (native_encode_initializer (op, ptr, len, 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))) | |
c57c910c JJ |
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 (mask, 0, len); | |
5047 | r = build_constructor (TREE_TYPE (r), NULL); | |
5048 | CONSTRUCTOR_NO_CLEARING (r) = 1; | |
5049 | } | |
5050 | } | |
5051 | } | |
896048cf JJ |
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) | |
c57c910c JJ |
5056 | { |
5057 | clear_type_padding_in_mask (TREE_TYPE (t), mask); | |
5058 | clear_uchar_or_std_byte_in_mask (loc, r, mask); | |
b41a927b JM |
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 | } | |
c57c910c | 5066 | } |
896048cf JJ |
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 | ||
2d76680f PC |
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 | |
3e605b20 | 5102 | cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t, |
2d76680f | 5103 | tree bailout_value, tree continue_value, |
605a80bb | 5104 | bool *non_constant_p, bool *overflow_p) |
2d76680f PC |
5105 | { |
5106 | tree r; | |
3e605b20 | 5107 | tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
72f76540 | 5108 | vc_prvalue, non_constant_p, |
93b65ed9 | 5109 | overflow_p); |
2d76680f PC |
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)); | |
3e605b20 | 5114 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
72f76540 | 5115 | vc_prvalue, non_constant_p, |
5a804683 | 5116 | overflow_p); |
2d76680f PC |
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, aggr); | |
5140 | v = CONSTRUCTOR_ELTS (base_ce->value); | |
5141 | } | |
5142 | ||
5143 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
5144 | if (ce->index == field) | |
5145 | return ce; | |
5146 | ||
5147 | gcc_unreachable (); | |
5148 | return NULL; | |
5149 | } | |
5150 | ||
3e605b20 JM |
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; | |
5dab8b11 | 5160 | tree type = TREE_TYPE (t); |
4d0c18c6 | 5161 | if (TREE_CODE (t) == CALL_EXPR) |
3e605b20 JM |
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 | } | |
4d0c18c6 JM |
5169 | else if (TREE_CODE (t) == COMPOUND_EXPR) |
5170 | return initialized_type (TREE_OPERAND (t, 1)); | |
5dab8b11 JM |
5171 | else if (TREE_CODE (t) == AGGR_INIT_EXPR) |
5172 | type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t)); | |
5173 | return cv_unqualified (type); | |
3e605b20 JM |
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 | |
3d423c6f PP |
5188 | && TREE_CODE (index) != FIELD_DECL |
5189 | && TREE_CODE (index) != RANGE_EXPR) | |
3e605b20 JM |
5190 | /* This won't have an element in the new CONSTRUCTOR. */ |
5191 | return; | |
5192 | ||
5193 | tree type = initialized_type (value); | |
5194 | if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type)) | |
5195 | /* A non-aggregate member doesn't get its own CONSTRUCTOR. */ | |
5196 | return; | |
abea1c9a JJ |
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; | |
3e605b20 JM |
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) | |
3d423c6f PP |
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 | } | |
d19b4342 JM |
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 | } | |
3e605b20 JM |
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))); | |
fe69277d JM |
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. */ | |
3e605b20 | 5251 | if (ctx->object) |
176e79b5 JM |
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 | } | |
3e605b20 | 5262 | gcc_assert (!ctx->object || !DECL_P (ctx->object) |
e6a29aab | 5263 | || ctx->global->get_value (ctx->object) == ctx->ctor); |
3e605b20 JM |
5264 | } |
5265 | ||
2d76680f PC |
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 | |
3e605b20 | 5271 | cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t, |
72f76540 | 5272 | value_cat lval, |
2d76680f PC |
5273 | bool *non_constant_p, bool *overflow_p) |
5274 | { | |
5275 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); | |
2d76680f PC |
5276 | bool changed = false; |
5277 | gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t)); | |
8a29084d | 5278 | tree type = TREE_TYPE (t); |
3e605b20 | 5279 | |
8a29084d | 5280 | constexpr_ctx new_ctx; |
d4619dc1 | 5281 | if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type)) |
8a29084d | 5282 | { |
d4619dc1 NS |
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. */ | |
8a29084d JM |
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); | |
d19b4342 JM |
5291 | vec<constructor_elt, va_gc> **p = nullptr; |
5292 | if (ctx->ctor) | |
5293 | { | |
5294 | p = &CONSTRUCTOR_ELTS (ctx->ctor); | |
5295 | vec_alloc (*p, vec_safe_length (v)); | |
5296 | if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t)) | |
5297 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1; | |
5298 | } | |
49a86fce | 5299 | |
2d63bc39 JM |
5300 | unsigned i; |
5301 | tree index, value; | |
5302 | bool constant_p = true; | |
5303 | bool side_effects_p = false; | |
3e605b20 | 5304 | FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value) |
2d76680f | 5305 | { |
bcb5f3c9 | 5306 | tree orig_value = value; |
db4243bb | 5307 | init_subob_ctx (ctx, new_ctx, index, value); |
a426b91b PP |
5308 | /* Like in cxx_eval_store_expression, omit entries for empty fields. */ |
5309 | bool no_slot = new_ctx.ctor == NULL_TREE; | |
37244b21 | 5310 | int pos_hint = -1; |
db4243bb | 5311 | if (new_ctx.ctor != ctx->ctor && !no_slot) |
37244b21 PP |
5312 | { |
5313 | /* If we built a new CONSTRUCTOR, attach it now so that other | |
5314 | initializers can refer to it. */ | |
077dd9b3 PP |
5315 | constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index); |
5316 | cep->value = new_ctx.ctor; | |
5317 | pos_hint = cep - (*p)->begin(); | |
37244b21 | 5318 | } |
b599bf9d | 5319 | else if (TREE_CODE (type) == UNION_TYPE) |
37244b21 PP |
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. */ | |
077dd9b3 | 5323 | get_or_insert_ctor_field (ctx->ctor, index); |
3e605b20 | 5324 | tree elt = cxx_eval_constant_expression (&new_ctx, value, |
92a596e8 | 5325 | lval, |
5a804683 | 5326 | non_constant_p, overflow_p); |
2d76680f | 5327 | /* Don't VERIFY_CONSTANT here. */ |
2b3ab879 | 5328 | if (ctx->quiet && *non_constant_p) |
3e605b20 | 5329 | break; |
bcb5f3c9 | 5330 | if (elt != orig_value) |
2d76680f | 5331 | changed = true; |
2d63bc39 JM |
5332 | |
5333 | if (!TREE_CONSTANT (elt)) | |
5334 | constant_p = false; | |
5335 | if (TREE_SIDE_EFFECTS (elt)) | |
5336 | side_effects_p = true; | |
3e605b20 | 5337 | if (index && TREE_CODE (index) == COMPONENT_REF) |
2d76680f PC |
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. */ | |
3e605b20 | 5342 | constructor_elt *inner = base_field_constructor_elt (*p, index); |
2d76680f | 5343 | inner->value = elt; |
3e605b20 | 5344 | changed = true; |
2d76680f | 5345 | } |
d19b4342 JM |
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; | |
3e605b20 JM |
5350 | else if (index |
5351 | && (TREE_CODE (index) == NOP_EXPR | |
5352 | || TREE_CODE (index) == POINTER_PLUS_EXPR)) | |
2d76680f | 5353 | { |
d19b4342 JM |
5354 | /* Old representation of empty bases. FIXME remove. */ |
5355 | gcc_checking_assert (false); | |
3e605b20 JM |
5356 | gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index)))); |
5357 | changed = true; | |
5358 | } | |
2d76680f | 5359 | else |
ac9ec198 | 5360 | { |
b599bf9d PP |
5361 | if (TREE_CODE (type) == UNION_TYPE |
5362 | && (*p)->last().index != index) | |
37244b21 PP |
5363 | /* The initializer erroneously changed the active union member that |
5364 | we're initializing. */ | |
b599bf9d | 5365 | gcc_assert (*non_constant_p); |
37244b21 | 5366 | else |
1bdbef09 | 5367 | { |
37244b21 PP |
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 (ctx->ctor, index, pos_hint); | |
5372 | cep->value = elt; | |
1bdbef09 | 5373 | } |
37244b21 | 5374 | |
1bdbef09 | 5375 | /* Adding or replacing an element might change the ctor's flags. */ |
ac9ec198 MP |
5376 | TREE_CONSTANT (ctx->ctor) = constant_p; |
5377 | TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p; | |
5378 | } | |
2d76680f | 5379 | } |
2cc41601 | 5380 | if (*non_constant_p) |
3e605b20 | 5381 | return t; |
2cc41601 JJ |
5382 | if (!changed) |
5383 | { | |
5384 | if (VECTOR_TYPE_P (type)) | |
5385 | t = fold (t); | |
5386 | return t; | |
5387 | } | |
3e605b20 | 5388 | t = ctx->ctor; |
d19b4342 JM |
5389 | if (!t) |
5390 | t = build_constructor (type, NULL); | |
3e605b20 JM |
5391 | /* We're done building this CONSTRUCTOR, so now we can interpret an |
5392 | element without an explicit initializer as value-initialized. */ | |
e8c48716 | 5393 | CONSTRUCTOR_NO_CLEARING (t) = false; |
2d63bc39 JM |
5394 | TREE_CONSTANT (t) = constant_p; |
5395 | TREE_SIDE_EFFECTS (t) = side_effects_p; | |
8a29084d | 5396 | if (VECTOR_TYPE_P (type)) |
2d76680f PC |
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 | |
3e605b20 | 5414 | cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init, |
72f76540 | 5415 | bool value_init, value_cat lval, |
2d76680f PC |
5416 | bool *non_constant_p, bool *overflow_p) |
5417 | { | |
5418 | tree elttype = TREE_TYPE (atype); | |
3e605b20 JM |
5419 | verify_ctor_sanity (ctx, atype); |
5420 | vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); | |
2d76680f | 5421 | bool pre_init = false; |
4784470a | 5422 | unsigned HOST_WIDE_INT i; |
dc5ca6c8 | 5423 | tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; |
2d76680f | 5424 | |
a1c9c9ff JM |
5425 | if (init && TREE_CODE (init) == CONSTRUCTOR) |
5426 | return cxx_eval_bare_aggregate (ctx, init, lval, | |
5427 | non_constant_p, overflow_p); | |
5428 | ||
2d76680f PC |
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 | { | |
dc5ca6c8 | 5438 | init = build_value_init (elttype, complain); |
2d76680f PC |
5439 | pre_init = true; |
5440 | } | |
5441 | else if (!init) | |
5442 | { | |
cd9cf97b | 5443 | releasing_vec argvec; |
2d76680f PC |
5444 | init = build_special_member_call (NULL_TREE, complete_ctor_identifier, |
5445 | &argvec, elttype, LOOKUP_NORMAL, | |
dc5ca6c8 | 5446 | complain); |
5dab8b11 | 5447 | init = build_aggr_init_expr (elttype, init); |
2d76680f PC |
5448 | pre_init = true; |
5449 | } | |
5450 | ||
d21252de PP |
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 (*p, 0); | |
5461 | } | |
5462 | ||
74f8705e MP |
5463 | tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p, |
5464 | overflow_p); | |
5465 | unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts); | |
4784470a | 5466 | for (i = 0; i < max; ++i) |
2d76680f PC |
5467 | { |
5468 | tree idx = build_int_cst (size_type_node, i); | |
5469 | tree eltinit; | |
928af3bf | 5470 | bool reuse = false; |
3e605b20 JM |
5471 | constexpr_ctx new_ctx; |
5472 | init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype); | |
a426b91b PP |
5473 | bool no_slot = new_ctx.ctor == NULL_TREE; |
5474 | if (new_ctx.ctor != ctx->ctor && !no_slot) | |
d21252de PP |
5475 | { |
5476 | if (zeroed_out) | |
5477 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false; | |
5478 | CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor); | |
5479 | } | |
2d76680f PC |
5480 | if (TREE_CODE (elttype) == ARRAY_TYPE) |
5481 | { | |
5482 | /* A multidimensional array; recurse. */ | |
5483 | if (value_init || init == NULL_TREE) | |
928af3bf JJ |
5484 | { |
5485 | eltinit = NULL_TREE; | |
5486 | reuse = i == 0; | |
5487 | } | |
2d76680f | 5488 | else |
dc5ca6c8 | 5489 | eltinit = cp_build_array_ref (input_location, init, idx, complain); |
3e605b20 | 5490 | eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init, |
92a596e8 | 5491 | lval, |
2d76680f PC |
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. */ | |
3ee378fb JM |
5498 | if (init == void_node) |
5499 | /* Trivial default-init, don't do anything to the CONSTRUCTOR. */ | |
5500 | return ctx->ctor; | |
928af3bf JJ |
5501 | eltinit = cxx_eval_constant_expression (&new_ctx, init, lval, |
5502 | non_constant_p, overflow_p); | |
5503 | reuse = i == 0; | |
2d76680f PC |
5504 | } |
5505 | else | |
5506 | { | |
5507 | /* Copying an element. */ | |
dc5ca6c8 | 5508 | eltinit = cp_build_array_ref (input_location, init, idx, complain); |
72b3e203 | 5509 | if (!lvalue_p (init)) |
2d76680f | 5510 | eltinit = move (eltinit); |
2764335b JM |
5511 | eltinit = (perform_implicit_conversion_flags |
5512 | (elttype, eltinit, complain, | |
5513 | LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING)); | |
c2236b9b JJ |
5514 | eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval, |
5515 | non_constant_p, overflow_p); | |
2d76680f | 5516 | } |
6b7d53a2 | 5517 | if (*non_constant_p) |
3e605b20 | 5518 | break; |
a426b91b PP |
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) | |
3e605b20 JM |
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); | |
928af3bf | 5534 | /* Reuse the result of cxx_eval_constant_expression call |
c2236b9b JJ |
5535 | from the first iteration to all others if it is a constant |
5536 | initializer that doesn't require relocations. */ | |
928af3bf JJ |
5537 | if (reuse |
5538 | && max > 1 | |
c2236b9b JJ |
5539 | && (eltinit == NULL_TREE |
5540 | || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit)) | |
5541 | == null_pointer_node))) | |
928af3bf JJ |
5542 | { |
5543 | if (new_ctx.ctor != ctx->ctor) | |
5544 | eltinit = new_ctx.ctor; | |
1e027956 RB |
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)); | |
928af3bf JJ |
5549 | break; |
5550 | } | |
1e027956 RB |
5551 | else if (i == 0) |
5552 | vec_safe_reserve (*p, max); | |
2d76680f PC |
5553 | } |
5554 | ||
5555 | if (!*non_constant_p) | |
5556 | { | |
3e605b20 | 5557 | init = ctx->ctor; |
e8c48716 | 5558 | CONSTRUCTOR_NO_CLEARING (init) = false; |
2d76680f | 5559 | } |
2d76680f PC |
5560 | return init; |
5561 | } | |
5562 | ||
5563 | static tree | |
3e605b20 | 5564 | cxx_eval_vec_init (const constexpr_ctx *ctx, tree t, |
72f76540 | 5565 | value_cat lval, |
2d76680f PC |
5566 | bool *non_constant_p, bool *overflow_p) |
5567 | { | |
5568 | tree atype = TREE_TYPE (t); | |
5569 | tree init = VEC_INIT_EXPR_INIT (t); | |
e948436e JM |
5570 | bool value_init = VEC_INIT_EXPR_VALUE_INIT (t); |
5571 | if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init)) | |
5572 | ; | |
4822108e JM |
5573 | else if (CONSTRUCTOR_NELTS (init) == 0 |
5574 | && !CP_AGGREGATE_TYPE_P (strip_array_types (atype))) | |
e948436e JM |
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; | |
e6a29aab | 5593 | ctx->global->put_value (new_ctx.object, ctor); |
e948436e JM |
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, | |
92a596e8 | 5601 | lval, non_constant_p, overflow_p); |
2d76680f PC |
5602 | if (*non_constant_p) |
5603 | return t; | |
5604 | else | |
5605 | return r; | |
5606 | } | |
5607 | ||
e4511ca2 JM |
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 | ||
43e84ce7 JJ |
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; | |
72f76540 | 5635 | tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue, |
43e84ce7 JJ |
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 | ||
0fe2ae29 JJ |
5646 | /* Helper function for cxx_fold_indirect_ref_1, called recursively. */ |
5647 | ||
5648 | static tree | |
43e84ce7 JJ |
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) | |
0fe2ae29 JJ |
5651 | { |
5652 | tree optype = TREE_TYPE (op); | |
5653 | unsigned HOST_WIDE_INT const_nunits; | |
bc99c54d JM |
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))) | |
0fe2ae29 | 5658 | { |
0fe2ae29 | 5659 | /* *(foo *)&complexfoo => __real__ complexfoo */ |
bc99c54d | 5660 | if (off == 0) |
0fe2ae29 | 5661 | return build1_loc (loc, REALPART_EXPR, type, op); |
bc99c54d JM |
5662 | /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */ |
5663 | else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off) | |
5664 | return build1_loc (loc, IMAGPART_EXPR, type, op); | |
0fe2ae29 JJ |
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 (optype).is_constant (&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, BIT_FIELD_REF, type, op, | |
5677 | TYPE_SIZE (type), 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, ARRAY_REF, TREE_TYPE (optype), op, index, | |
5697 | NULL_TREE, NULL_TREE); | |
43e84ce7 | 5698 | return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem, |
0fe2ae29 JJ |
5699 | empty_base); |
5700 | } | |
5701 | } | |
5702 | /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */ | |
43e84ce7 JJ |
5703 | else if (TREE_CODE (optype) == RECORD_TYPE |
5704 | || TREE_CODE (optype) == UNION_TYPE) | |
0fe2ae29 | 5705 | { |
43e84ce7 JJ |
5706 | if (TREE_CODE (optype) == UNION_TYPE) |
5707 | /* For unions prefer the currently active member. */ | |
5708 | if (tree field = cxx_union_active_member (ctx, 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, cop, | |
5717 | off, empty_base)) | |
5718 | return ret; | |
5719 | } | |
5720 | } | |
4b8d0d4d | 5721 | |
90bc2d09 | 5722 | /* Handle conversion to "as base" type. */ |
39f9c426 NS |
5723 | if (CLASS_TYPE_P (optype) |
5724 | && CLASSTYPE_AS_BASE (optype) == type) | |
90bc2d09 NS |
5725 | return op; |
5726 | ||
4b8d0d4d JM |
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) | |
a43f3616 | 5732 | && lookup_base (optype, type, ba_any, NULL, tf_none, off)) |
4b8d0d4d JM |
5733 | { |
5734 | if (empty_base) | |
5735 | *empty_base = true; | |
5736 | return op; | |
5737 | } | |
5738 | ||
0fe2ae29 JJ |
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); | |
43e84ce7 | 5749 | unsigned HOST_WIDE_INT el_sz |
0fe2ae29 JJ |
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); | |
43e84ce7 | 5755 | if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop, |
0fe2ae29 JJ |
5756 | off - upos, |
5757 | empty_base)) | |
5758 | return ret; | |
5759 | } | |
5760 | } | |
5761 | } | |
5762 | ||
5763 | return NULL_TREE; | |
5764 | } | |
5765 | ||
2d76680f PC |
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 | |
0fe2ae29 | 5773 | with TBAA in fold_indirect_ref_1. */ |
2d76680f PC |
5774 | |
5775 | static tree | |
43e84ce7 | 5776 | cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type, |
1189c038 | 5777 | tree op0, bool *empty_base /* = NULL*/) |
2d76680f | 5778 | { |
ebe4bf41 MP |
5779 | tree sub = op0; |
5780 | tree subtype; | |
2d76680f | 5781 | |
80de0002 JM |
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 | ||
2d76680f | 5792 | subtype = TREE_TYPE (sub); |
71a93b08 | 5793 | if (!INDIRECT_TYPE_P (subtype)) |
2d76680f PC |
5794 | return NULL_TREE; |
5795 | ||
ab36b554 PP |
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 | |
0844170e PP |
5802 | /* We need to preserve union member accesses so that we can |
5803 | later properly diagnose accessing the wrong member. */ | |
5804 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (obj, 0))) == RECORD_TYPE | |
ab36b554 PP |
5805 | && (tree_int_cst_sign_bit (off) || integer_zerop (off))) |
5806 | { | |
5807 | tree field = TREE_OPERAND (obj, 1); | |
5808 | tree pos = byte_position (field); | |
5809 | if (integer_zerop (off) && integer_nonzerop (pos)) | |
5810 | /* If the offset is already 0, keep going as long as the | |
5811 | component is at position 0. */ | |
5812 | break; | |
5813 | off = int_const_binop (PLUS_EXPR, off, pos); | |
5814 | obj = TREE_OPERAND (obj, 0); | |
5815 | } | |
5816 | }; | |
5817 | ||
2d76680f PC |
5818 | if (TREE_CODE (sub) == ADDR_EXPR) |
5819 | { | |
5820 | tree op = TREE_OPERAND (sub, 0); | |
5821 | tree optype = TREE_TYPE (op); | |
5822 | ||
5823 | /* *&CONST_DECL -> to the value of the const decl. */ | |
5824 | if (TREE_CODE (op) == CONST_DECL) | |
5825 | return DECL_INITIAL (op); | |
5826 | /* *&p => p; make sure to handle *&"str"[cst] here. */ | |
1a120ec1 | 5827 | if (similar_type_p (optype, type)) |
2d76680f PC |
5828 | { |
5829 | tree fop = fold_read_from_constant_string (op); | |
5830 | if (fop) | |
5831 | return fop; | |
5832 | else | |
5833 | return op; | |
5834 | } | |
0fe2ae29 | 5835 | else |
ab36b554 PP |
5836 | { |
5837 | tree off = integer_zero_node; | |
5838 | canonicalize_obj_off (op, off); | |
5839 | gcc_assert (integer_zerop (off)); | |
5840 | return cxx_fold_indirect_ref_1 (ctx, loc, type, op, 0, empty_base); | |
5841 | } | |
2d76680f PC |
5842 | } |
5843 | else if (TREE_CODE (sub) == POINTER_PLUS_EXPR | |
0fe2ae29 | 5844 | && tree_fits_uhwi_p (TREE_OPERAND (sub, 1))) |
2d76680f PC |
5845 | { |
5846 | tree op00 = TREE_OPERAND (sub, 0); | |
0120cd93 | 5847 | tree off = TREE_OPERAND (sub, 1); |
2d76680f PC |
5848 | |
5849 | STRIP_NOPS (op00); | |
5850 | if (TREE_CODE (op00) == ADDR_EXPR) | |
0120cd93 PP |
5851 | { |
5852 | tree obj = TREE_OPERAND (op00, 0); | |
ab36b554 | 5853 | canonicalize_obj_off (obj, off); |
0120cd93 PP |
5854 | return cxx_fold_indirect_ref_1 (ctx, loc, type, obj, |
5855 | tree_to_uhwi (off), empty_base); | |
5856 | } | |
2d76680f PC |
5857 | } |
5858 | /* *(foo *)fooarrptr => (*fooarrptr)[0] */ | |
5859 | else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE | |
1a120ec1 | 5860 | && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype)))) |
2d76680f PC |
5861 | { |
5862 | tree type_domain; | |
5863 | tree min_val = size_zero_node; | |
ebe4bf41 | 5864 | tree newsub |
43e84ce7 | 5865 | = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL); |
2d76680f PC |
5866 | if (newsub) |
5867 | sub = newsub; | |
5868 | else | |
5869 | sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub); | |
5870 | type_domain = TYPE_DOMAIN (TREE_TYPE (sub)); | |
5871 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
5872 | min_val = TYPE_MIN_VALUE (type_domain); | |
5873 | return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE, | |
5874 | NULL_TREE); | |
5875 | } | |
5876 | ||
5877 | return NULL_TREE; | |
5878 | } | |
5879 | ||
5880 | static tree | |
3e605b20 | 5881 | cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t, |
72f76540 | 5882 | value_cat lval, |
2d76680f PC |
5883 | bool *non_constant_p, bool *overflow_p) |
5884 | { | |
5885 | tree orig_op0 = TREE_OPERAND (t, 0); | |
2d76680f | 5886 | bool empty_base = false; |
2d76680f | 5887 | |
cda0a029 JM |
5888 | /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second |
5889 | operand is an integer-zero. Otherwise reject the MEM_REF for now. */ | |
5890 | ||
5891 | if (TREE_CODE (t) == MEM_REF | |
5892 | && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1)))) | |
5893 | { | |
5894 | gcc_assert (ctx->quiet); | |
5895 | *non_constant_p = true; | |
5896 | return t; | |
5897 | } | |
5898 | ||
255a48d6 | 5899 | /* First try to simplify it directly. */ |
43e84ce7 JJ |
5900 | tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), |
5901 | orig_op0, &empty_base); | |
255a48d6 | 5902 | if (!r) |
2d76680f | 5903 | { |
255a48d6 JM |
5904 | /* If that didn't work, evaluate the operand first. */ |
5905 | tree op0 = cxx_eval_constant_expression (ctx, orig_op0, | |
72f76540 | 5906 | vc_prvalue, non_constant_p, |
255a48d6 JM |
5907 | overflow_p); |
5908 | /* Don't VERIFY_CONSTANT here. */ | |
5909 | if (*non_constant_p) | |
5910 | return t; | |
5911 | ||
8bada5cd MS |
5912 | if (!lval && integer_zerop (op0)) |
5913 | { | |
5914 | if (!ctx->quiet) | |
5915 | error ("dereferencing a null pointer"); | |
5916 | *non_constant_p = true; | |
5917 | return t; | |
5918 | } | |
5919 | ||
43e84ce7 | 5920 | r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0, |
255a48d6 JM |
5921 | &empty_base); |
5922 | if (r == NULL_TREE) | |
2d76680f PC |
5923 | { |
5924 | /* We couldn't fold to a constant value. Make sure it's not | |
5925 | something we should have been able to fold. */ | |
255a48d6 JM |
5926 | tree sub = op0; |
5927 | STRIP_NOPS (sub); | |
5928 | if (TREE_CODE (sub) == ADDR_EXPR) | |
5929 | { | |
1a120ec1 | 5930 | gcc_assert (!similar_type_p |
255a48d6 JM |
5931 | (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t))); |
5932 | /* DR 1188 says we don't have to deal with this. */ | |
5933 | if (!ctx->quiet) | |
1a120ec1 JM |
5934 | error_at (cp_expr_loc_or_input_loc (t), |
5935 | "accessing value of %qE through a %qT glvalue in a " | |
5936 | "constant expression", build_fold_indirect_ref (sub), | |
5937 | TREE_TYPE (t)); | |
255a48d6 JM |
5938 | *non_constant_p = true; |
5939 | return t; | |
5940 | } | |
5941 | ||
72f76540 | 5942 | if (lval == vc_glvalue && op0 != orig_op0) |
255a48d6 JM |
5943 | return build1 (INDIRECT_REF, TREE_TYPE (t), op0); |
5944 | if (!lval) | |
5945 | VERIFY_CONSTANT (t); | |
2d76680f PC |
5946 | return t; |
5947 | } | |
5948 | } | |
5949 | ||
255a48d6 JM |
5950 | r = cxx_eval_constant_expression (ctx, r, |
5951 | lval, non_constant_p, overflow_p); | |
5952 | if (*non_constant_p) | |
5953 | return t; | |
5954 | ||
125db6a1 | 5955 | /* If we're pulling out the value of an empty base, just return an empty |
2d76680f | 5956 | CONSTRUCTOR. */ |
92a596e8 | 5957 | if (empty_base && !lval) |
2d76680f | 5958 | { |
2d76680f PC |
5959 | r = build_constructor (TREE_TYPE (t), NULL); |
5960 | TREE_CONSTANT (r) = true; | |
5961 | } | |
5962 | ||
2d76680f PC |
5963 | return r; |
5964 | } | |
5965 | ||
9fdbd7d6 NS |
5966 | /* Complain about R, a DECL that is accessed outside its lifetime. */ |
5967 | ||
5968 | static void | |
5969 | outside_lifetime_error (location_t loc, tree r) | |
5970 | { | |
4f870007 | 5971 | auto_diagnostic_group d; |
9fdbd7d6 NS |
5972 | if (DECL_NAME (r) == heap_deleted_identifier) |
5973 | { | |
5974 | /* Provide a more accurate message for deleted variables. */ | |
5975 | error_at (loc, "use of allocated storage after deallocation " | |
5976 | "in a constant expression"); | |
5977 | inform (DECL_SOURCE_LOCATION (r), "allocated here"); | |
5978 | } | |
5979 | else | |
5980 | { | |
90bc2d09 | 5981 | error_at (loc, "accessing %qE outside its lifetime", r); |
9fdbd7d6 NS |
5982 | inform (DECL_SOURCE_LOCATION (r), "declared here"); |
5983 | } | |
5984 | } | |
5985 | ||
2d76680f | 5986 | /* Complain about R, a VAR_DECL, not being usable in a constant expression. |
c85f8dbb | 5987 | FUNDEF_P is true if we're checking a constexpr function body. |
2d76680f PC |
5988 | Shared between potential_constant_expression and |
5989 | cxx_eval_constant_expression. */ | |
5990 | ||
5991 | static void | |
c85f8dbb | 5992 | non_const_var_error (location_t loc, tree r, bool fundef_p) |
2d76680f | 5993 | { |
8e007055 | 5994 | auto_diagnostic_group d; |
2d76680f | 5995 | tree type = TREE_TYPE (r); |
8e007055 | 5996 | if (DECL_NAME (r) == heap_uninit_identifier |
815baade JJ |
5997 | || DECL_NAME (r) == heap_identifier |
5998 | || DECL_NAME (r) == heap_vec_uninit_identifier | |
5999 | || DECL_NAME (r) == heap_vec_identifier) | |
8e007055 | 6000 | { |
c85f8dbb MP |
6001 | if (constexpr_error (loc, fundef_p, "the content of uninitialized " |
6002 | "storage is not usable in a constant expression")) | |
6003 | inform (DECL_SOURCE_LOCATION (r), "allocated here"); | |
8e007055 JJ |
6004 | return; |
6005 | } | |
6006 | if (DECL_NAME (r) == heap_deleted_identifier) | |
6007 | { | |
c85f8dbb MP |
6008 | if (constexpr_error (loc, fundef_p, "use of allocated storage after " |
6009 | "deallocation in a constant expression")) | |
6010 | inform (DECL_SOURCE_LOCATION (r), "allocated here"); | |
8e007055 JJ |
6011 | return; |
6012 | } | |
c85f8dbb MP |
6013 | if (!constexpr_error (loc, fundef_p, "the value of %qD is not usable in " |
6014 | "a constant expression", r)) | |
6015 | return; | |
2d76680f PC |
6016 | /* Avoid error cascade. */ |
6017 | if (DECL_INITIAL (r) == error_mark_node) | |
6018 | return; | |
6019 | if (DECL_DECLARED_CONSTEXPR_P (r)) | |
6020 | inform (DECL_SOURCE_LOCATION (r), | |
6021 | "%qD used in its own initializer", r); | |
6022 | else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) | |
6023 | { | |
6024 | if (!CP_TYPE_CONST_P (type)) | |
6025 | inform (DECL_SOURCE_LOCATION (r), | |
6026 | "%q#D is not const", r); | |
6027 | else if (CP_TYPE_VOLATILE_P (type)) | |
6028 | inform (DECL_SOURCE_LOCATION (r), | |
6029 | "%q#D is volatile", r); | |
6030 | else if (!DECL_INITIAL (r) | |
a3e2b438 PP |
6031 | || !TREE_CONSTANT (DECL_INITIAL (r)) |
6032 | || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)) | |
2d76680f PC |
6033 | inform (DECL_SOURCE_LOCATION (r), |
6034 | "%qD was not initialized with a constant " | |
6035 | "expression", r); | |
6036 | else | |
6037 | gcc_unreachable (); | |
6038 | } | |
9f613f06 | 6039 | else if (TYPE_REF_P (type)) |
fd338b13 JM |
6040 | inform (DECL_SOURCE_LOCATION (r), |
6041 | "%qD was not initialized with a constant " | |
6042 | "expression", r); | |
2d76680f PC |
6043 | else |
6044 | { | |
6045 | if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r)) | |
6046 | inform (DECL_SOURCE_LOCATION (r), | |
6047 | "%qD was not declared %<constexpr%>", r); | |
6048 | else | |
6049 | inform (DECL_SOURCE_LOCATION (r), | |
6050 | "%qD does not have integral or enumeration type", | |
6051 | r); | |
6052 | } | |
6053 | } | |
6054 | ||
6055 | /* Subroutine of cxx_eval_constant_expression. | |
6056 | Like cxx_eval_unary_expression, except for trinary expressions. */ | |
6057 | ||
6058 | static tree | |
3e605b20 | 6059 | cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t, |
72f76540 | 6060 | value_cat lval, |
2d76680f PC |
6061 | bool *non_constant_p, bool *overflow_p) |
6062 | { | |
6063 | int i; | |
6064 | tree args[3]; | |
6065 | tree val; | |
6066 | ||
6067 | for (i = 0; i < 3; i++) | |
6068 | { | |
3e605b20 | 6069 | args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i), |
92a596e8 | 6070 | lval, |
5a804683 | 6071 | non_constant_p, overflow_p); |
2d76680f PC |
6072 | VERIFY_CONSTANT (args[i]); |
6073 | } | |
6074 | ||
6075 | val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t), | |
6076 | args[0], args[1], args[2]); | |
6077 | if (val == NULL_TREE) | |
6078 | return t; | |
6079 | VERIFY_CONSTANT (val); | |
6080 | return val; | |
6081 | } | |
6082 | ||
98e5a19a JM |
6083 | /* True if T was declared in a function declared to be constexpr, and |
6084 | therefore potentially constant in C++14. */ | |
6085 | ||
2d76680f PC |
6086 | bool |
6087 | var_in_constexpr_fn (tree t) | |
6088 | { | |
6089 | tree ctx = DECL_CONTEXT (t); | |
98e5a19a | 6090 | return (ctx && TREE_CODE (ctx) == FUNCTION_DECL |
2d76680f PC |
6091 | && DECL_DECLARED_CONSTEXPR_P (ctx)); |
6092 | } | |
6093 | ||
26dbe85a MP |
6094 | /* True if a function might be constexpr: either a function that was |
6095 | declared constexpr, or a C++17 lambda op(). */ | |
6096 | ||
6097 | bool | |
6098 | maybe_constexpr_fn (tree t) | |
6099 | { | |
6100 | return (DECL_DECLARED_CONSTEXPR_P (t) | |
87c2080b JM |
6101 | || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t)) |
6102 | || (flag_implicit_constexpr | |
6103 | && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t)))); | |
26dbe85a MP |
6104 | } |
6105 | ||
98e5a19a JM |
6106 | /* True if T was declared in a function that might be constexpr: either a |
6107 | function that was declared constexpr, or a C++17 lambda op(). */ | |
6108 | ||
6109 | bool | |
6110 | var_in_maybe_constexpr_fn (tree t) | |
6111 | { | |
87c2080b JM |
6112 | return (DECL_FUNCTION_SCOPE_P (t) |
6113 | && maybe_constexpr_fn (DECL_CONTEXT (t))); | |
98e5a19a JM |
6114 | } |
6115 | ||
68d01920 JM |
6116 | /* We're assigning INIT to TARGET. In do_build_copy_constructor and |
6117 | build_over_call we implement trivial copy of a class with tail padding using | |
6118 | assignment of character arrays, which is valid in normal code, but not in | |
6119 | constexpr evaluation. We don't need to worry about clobbering tail padding | |
6120 | in constexpr evaluation, so strip the type punning. */ | |
6121 | ||
6122 | static void | |
6123 | maybe_simplify_trivial_copy (tree &target, tree &init) | |
6124 | { | |
6125 | if (TREE_CODE (target) == MEM_REF | |
6126 | && TREE_CODE (init) == MEM_REF | |
6127 | && TREE_TYPE (target) == TREE_TYPE (init) | |
6128 | && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE | |
6129 | && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node) | |
6130 | { | |
6131 | target = build_fold_indirect_ref (TREE_OPERAND (target, 0)); | |
6132 | init = build_fold_indirect_ref (TREE_OPERAND (init, 0)); | |
6133 | } | |
6134 | } | |
6135 | ||
7eb5be6a MP |
6136 | /* Returns true if REF, which is a COMPONENT_REF, has any fields |
6137 | of constant type. This does not check for 'mutable', so the | |
6138 | caller is expected to be mindful of that. */ | |
6139 | ||
6140 | static bool | |
6141 | cref_has_const_field (tree ref) | |
6142 | { | |
6143 | while (TREE_CODE (ref) == COMPONENT_REF) | |
6144 | { | |
6145 | if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1)))) | |
6146 | return true; | |
6147 | ref = TREE_OPERAND (ref, 0); | |
6148 | } | |
6149 | return false; | |
6150 | } | |
6151 | ||
04e1749c MP |
6152 | /* Return true if we are modifying something that is const during constant |
6153 | expression evaluation. CODE is the code of the statement, OBJ is the | |
6154 | object in question, MUTABLE_P is true if one of the subobjects were | |
6155 | declared mutable. */ | |
6156 | ||
6157 | static bool | |
6158 | modifying_const_object_p (tree_code code, tree obj, bool mutable_p) | |
6159 | { | |
6160 | /* If this is initialization, there's no problem. */ | |
6161 | if (code != MODIFY_EXPR) | |
6162 | return false; | |
6163 | ||
6164 | /* [basic.type.qualifier] "A const object is an object of type | |
6165 | const T or a non-mutable subobject of a const object." */ | |
6166 | if (mutable_p) | |
6167 | return false; | |
6168 | ||
7eb5be6a MP |
6169 | if (TREE_READONLY (obj)) |
6170 | return true; | |
6171 | ||
6172 | if (CP_TYPE_CONST_P (TREE_TYPE (obj))) | |
6173 | { | |
6174 | /* Although a COMPONENT_REF may have a const type, we should | |
6175 | only consider it modifying a const object when any of the | |
6176 | field components is const. This can happen when using | |
6177 | constructs such as const_cast<const T &>(m), making something | |
6178 | const even though it wasn't declared const. */ | |
6179 | if (TREE_CODE (obj) == COMPONENT_REF) | |
6180 | return cref_has_const_field (obj); | |
6181 | else | |
6182 | return true; | |
6183 | } | |
6184 | ||
6185 | return false; | |
04e1749c MP |
6186 | } |
6187 | ||
60813a46 JM |
6188 | /* Evaluate an INIT_EXPR or MODIFY_EXPR. */ |
6189 | ||
6190 | static tree | |
6191 | cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, | |
72f76540 | 6192 | value_cat lval, |
60813a46 JM |
6193 | bool *non_constant_p, bool *overflow_p) |
6194 | { | |
6195 | constexpr_ctx new_ctx = *ctx; | |
6196 | ||
58cc255c | 6197 | tree init = TREE_OPERAND (t, 1); |
90bc2d09 NS |
6198 | |
6199 | if (TREE_CLOBBER_P (init) | |
6200 | && CLOBBER_KIND (init) < CLOBBER_OBJECT_END) | |
6201 | /* Only handle clobbers ending the lifetime of objects. */ | |
58cc255c JM |
6202 | return void_node; |
6203 | ||
60813a46 JM |
6204 | /* First we figure out where we're storing to. */ |
6205 | tree target = TREE_OPERAND (t, 0); | |
68d01920 JM |
6206 | |
6207 | maybe_simplify_trivial_copy (target, init); | |
6208 | ||
3f8e2835 | 6209 | tree type = TREE_TYPE (target); |
e8b3c1bc | 6210 | bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR; |
90bc2d09 | 6211 | if (preeval && !TREE_CLOBBER_P (init)) |
e8b3c1bc JM |
6212 | { |
6213 | /* Evaluate the value to be stored without knowing what object it will be | |
6214 | stored in, so that any side-effects happen first. */ | |
6215 | if (!SCALAR_TYPE_P (type)) | |
6216 | new_ctx.ctor = new_ctx.object = NULL_TREE; | |
72f76540 | 6217 | init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue, |
e8b3c1bc JM |
6218 | non_constant_p, overflow_p); |
6219 | if (*non_constant_p) | |
6220 | return t; | |
6221 | } | |
60813a46 | 6222 | |
d0aa42d2 | 6223 | bool evaluated = false; |
72f76540 | 6224 | if (lval == vc_glvalue) |
3f8e2835 | 6225 | { |
d0aa42d2 JM |
6226 | /* If we want to return a reference to the target, we need to evaluate it |
6227 | as a whole; otherwise, only evaluate the innermost piece to avoid | |
6228 | building up unnecessary *_REFs. */ | |
72f76540 | 6229 | target = cxx_eval_constant_expression (ctx, target, lval, |
d0aa42d2 JM |
6230 | non_constant_p, overflow_p); |
6231 | evaluated = true; | |
6232 | if (*non_constant_p) | |
6233 | return t; | |
3f8e2835 JM |
6234 | } |
6235 | ||
043666e0 | 6236 | /* Find the underlying variable. */ |
cd9cf97b | 6237 | releasing_vec refs; |
60813a46 | 6238 | tree object = NULL_TREE; |
04e1749c MP |
6239 | /* If we're modifying a const object, save it. */ |
6240 | tree const_object_being_modified = NULL_TREE; | |
6241 | bool mutable_p = false; | |
60813a46 JM |
6242 | for (tree probe = target; object == NULL_TREE; ) |
6243 | { | |
6244 | switch (TREE_CODE (probe)) | |
6245 | { | |
6246 | case BIT_FIELD_REF: | |
6247 | case COMPONENT_REF: | |
6248 | case ARRAY_REF: | |
043666e0 JM |
6249 | { |
6250 | tree ob = TREE_OPERAND (probe, 0); | |
6251 | tree elt = TREE_OPERAND (probe, 1); | |
7d349dd8 | 6252 | if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt)) |
04e1749c | 6253 | mutable_p = true; |
043666e0 JM |
6254 | if (TREE_CODE (probe) == ARRAY_REF) |
6255 | { | |
6256 | elt = eval_and_check_array_index (ctx, probe, false, | |
6257 | non_constant_p, overflow_p); | |
6258 | if (*non_constant_p) | |
6259 | return t; | |
6260 | } | |
0f184800 MP |
6261 | /* We don't check modifying_const_object_p for ARRAY_REFs. Given |
6262 | "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though | |
6263 | the array isn't const. Instead, check "a" in the next iteration; | |
6264 | that will detect modifying "const int a[10]". */ | |
6265 | else if (evaluated | |
6266 | && modifying_const_object_p (TREE_CODE (t), probe, | |
6267 | mutable_p) | |
6268 | && const_object_being_modified == NULL_TREE) | |
6269 | const_object_being_modified = probe; | |
1d260ab0 NS |
6270 | |
6271 | /* Track named member accesses for unions to validate modifications | |
6272 | that change active member. */ | |
6273 | if (!evaluated && TREE_CODE (probe) == COMPONENT_REF) | |
6274 | vec_safe_push (refs, probe); | |
6275 | else | |
6276 | vec_safe_push (refs, NULL_TREE); | |
6277 | ||
043666e0 JM |
6278 | vec_safe_push (refs, elt); |
6279 | vec_safe_push (refs, TREE_TYPE (probe)); | |
6280 | probe = ob; | |
6281 | } | |
60813a46 JM |
6282 | break; |
6283 | ||
19077677 JJ |
6284 | case REALPART_EXPR: |
6285 | gcc_assert (probe == target); | |
1d260ab0 | 6286 | vec_safe_push (refs, NULL_TREE); |
19077677 JJ |
6287 | vec_safe_push (refs, probe); |
6288 | vec_safe_push (refs, TREE_TYPE (probe)); | |
6289 | probe = TREE_OPERAND (probe, 0); | |
6290 | break; | |
6291 | ||
6292 | case IMAGPART_EXPR: | |
6293 | gcc_assert (probe == target); | |
1d260ab0 | 6294 | vec_safe_push (refs, NULL_TREE); |
19077677 JJ |
6295 | vec_safe_push (refs, probe); |
6296 | vec_safe_push (refs, TREE_TYPE (probe)); | |
6297 | probe = TREE_OPERAND (probe, 0); | |
6298 | break; | |
6299 | ||
60813a46 | 6300 | default: |
d0aa42d2 JM |
6301 | if (evaluated) |
6302 | object = probe; | |
6303 | else | |
6304 | { | |
72f76540 | 6305 | probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue, |
d0aa42d2 JM |
6306 | non_constant_p, overflow_p); |
6307 | evaluated = true; | |
6308 | if (*non_constant_p) | |
6309 | return t; | |
6310 | } | |
6311 | break; | |
60813a46 JM |
6312 | } |
6313 | } | |
6314 | ||
04e1749c MP |
6315 | if (modifying_const_object_p (TREE_CODE (t), object, mutable_p) |
6316 | && const_object_being_modified == NULL_TREE) | |
6317 | const_object_being_modified = object; | |
6318 | ||
90bc2d09 NS |
6319 | if (DECL_P (object) |
6320 | && TREE_CLOBBER_P (init) | |
6321 | && DECL_NAME (object) == heap_deleted_identifier) | |
6322 | /* Ignore clobbers of deleted allocations for now; we'll get a better error | |
6323 | message later when operator delete is called. */ | |
6324 | return void_node; | |
6325 | ||
60813a46 JM |
6326 | /* And then find/build up our initializer for the path to the subobject |
6327 | we're initializing. */ | |
a1408b7e | 6328 | tree *valp; |
077dd9b3 | 6329 | if (DECL_P (object)) |
90bc2d09 | 6330 | valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR); |
a1408b7e JM |
6331 | else |
6332 | valp = NULL; | |
60813a46 JM |
6333 | if (!valp) |
6334 | { | |
6335 | /* A constant-expression cannot modify objects from outside the | |
6336 | constant-expression. */ | |
2b3ab879 | 6337 | if (!ctx->quiet) |
90bc2d09 NS |
6338 | { |
6339 | auto_diagnostic_group d; | |
6340 | if (DECL_P (object) && DECL_NAME (object) == heap_deleted_identifier) | |
6341 | { | |
6342 | error ("modification of allocated storage after deallocation " | |
6343 | "is not a constant expression"); | |
6344 | inform (DECL_SOURCE_LOCATION (object), "allocated here"); | |
6345 | } | |
6346 | else if (DECL_P (object) && ctx->global->is_outside_lifetime (object)) | |
6347 | { | |
6348 | if (TREE_CLOBBER_P (init)) | |
6349 | error ("destroying %qE outside its lifetime", object); | |
6350 | else | |
6351 | error ("modification of %qE outside its lifetime " | |
6352 | "is not a constant expression", object); | |
6353 | inform (DECL_SOURCE_LOCATION (object), "declared here"); | |
6354 | } | |
6355 | else | |
6356 | { | |
6357 | if (TREE_CLOBBER_P (init)) | |
6358 | error ("destroying %qE from outside current evaluation " | |
6359 | "is not a constant expression", object); | |
6360 | else | |
6361 | error ("modification of %qE from outside current evaluation " | |
6362 | "is not a constant expression", object); | |
6363 | } | |
6364 | } | |
60813a46 JM |
6365 | *non_constant_p = true; |
6366 | return t; | |
6367 | } | |
90bc2d09 NS |
6368 | |
6369 | /* Handle explicit end-of-lifetime. */ | |
6370 | if (TREE_CLOBBER_P (init)) | |
6371 | { | |
6372 | if (refs->is_empty ()) | |
6373 | ctx->global->destroy_value (object); | |
6374 | return void_node; | |
6375 | } | |
6376 | ||
3f8e2835 | 6377 | type = TREE_TYPE (object); |
c75ce530 | 6378 | bool no_zero_init = true; |
2d63bc39 | 6379 | |
19077677 JJ |
6380 | auto_vec<tree *> ctors; |
6381 | releasing_vec indexes; | |
37244b21 PP |
6382 | auto_vec<int> index_pos_hints; |
6383 | bool activated_union_member_p = false; | |
967cdbe6 | 6384 | bool empty_base = false; |
276a52d5 | 6385 | while (!refs->is_empty ()) |
60813a46 JM |
6386 | { |
6387 | if (*valp == NULL_TREE) | |
6388 | { | |
6389 | *valp = build_constructor (type, NULL); | |
e8c48716 | 6390 | CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; |
60813a46 | 6391 | } |
be20dcc3 MP |
6392 | else if (STRIP_ANY_LOCATION_WRAPPER (*valp), |
6393 | TREE_CODE (*valp) == STRING_CST) | |
d6b46fca NS |
6394 | { |
6395 | /* An array was initialized with a string constant, and now | |
6396 | we're writing into one of its elements. Explode the | |
6397 | single initialization into a set of element | |
6398 | initializations. */ | |
6399 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); | |
6400 | ||
6401 | tree string = *valp; | |
6402 | tree elt_type = TREE_TYPE (type); | |
6403 | unsigned chars_per_elt = (TYPE_PRECISION (elt_type) | |
6404 | / TYPE_PRECISION (char_type_node)); | |
6405 | unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt; | |
6406 | tree ary_ctor = build_constructor (type, NULL); | |
6407 | ||
6408 | vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts); | |
6409 | for (unsigned ix = 0; ix != num_elts; ix++) | |
6410 | { | |
6411 | constructor_elt elt = | |
6412 | { | |
6413 | build_int_cst (size_type_node, ix), | |
6414 | extract_string_elt (string, chars_per_elt, ix) | |
6415 | }; | |
6416 | CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt); | |
6417 | } | |
94ff4c9d | 6418 | |
d6b46fca NS |
6419 | *valp = ary_ctor; |
6420 | } | |
6421 | ||
ceaaf873 | 6422 | enum tree_code code = TREE_CODE (type); |
967cdbe6 | 6423 | tree reftype = refs->pop(); |
ceaaf873 | 6424 | tree index = refs->pop(); |
1d260ab0 | 6425 | bool is_access_expr = refs->pop() != NULL_TREE; |
60813a46 | 6426 | |
19077677 JJ |
6427 | if (code == COMPLEX_TYPE) |
6428 | { | |
6429 | if (TREE_CODE (*valp) == COMPLEX_CST) | |
6430 | *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp), | |
6431 | TREE_IMAGPART (*valp)); | |
6432 | else if (TREE_CODE (*valp) == CONSTRUCTOR | |
6433 | && CONSTRUCTOR_NELTS (*valp) == 0 | |
6434 | && CONSTRUCTOR_NO_CLEARING (*valp)) | |
6435 | { | |
6436 | tree r = build_constructor (reftype, NULL); | |
6437 | CONSTRUCTOR_NO_CLEARING (r) = 1; | |
6438 | *valp = build2 (COMPLEX_EXPR, type, r, r); | |
6439 | } | |
6440 | gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR); | |
6441 | ctors.safe_push (valp); | |
6442 | vec_safe_push (indexes, index); | |
6443 | valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR); | |
6444 | gcc_checking_assert (refs->is_empty ()); | |
6445 | type = reftype; | |
6446 | break; | |
6447 | } | |
6448 | ||
6449 | /* If the value of object is already zero-initialized, any new ctors for | |
6450 | subobjects will also be zero-initialized. */ | |
6451 | no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp); | |
6452 | ||
a8dd2b3e | 6453 | if (code == RECORD_TYPE && is_empty_field (index)) |
a4dfd0f0 JM |
6454 | /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they |
6455 | have no data and might have an offset lower than previously declared | |
6456 | fields, which confuses the middle-end. The code below will notice | |
6457 | that we don't have a CONSTRUCTOR for our inner target and just | |
6458 | return init. */ | |
967cdbe6 JM |
6459 | { |
6460 | empty_base = true; | |
6461 | break; | |
6462 | } | |
6463 | ||
1d260ab0 NS |
6464 | /* If a union is zero-initialized, its first non-static named data member |
6465 | is zero-initialized (and therefore active). */ | |
6466 | if (code == UNION_TYPE | |
6467 | && !no_zero_init | |
6468 | && CONSTRUCTOR_NELTS (*valp) == 0) | |
6469 | if (tree first = next_aggregate_field (TYPE_FIELDS (type))) | |
6470 | CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (*valp), first, NULL_TREE); | |
6471 | ||
6472 | /* Check for implicit change of active member for a union. */ | |
6473 | if (code == UNION_TYPE | |
6474 | && (CONSTRUCTOR_NELTS (*valp) == 0 | |
6475 | || CONSTRUCTOR_ELT (*valp, 0)->index != index) | |
6476 | /* An INIT_EXPR of the last member in an access chain is always OK, | |
6477 | but still check implicit change of members earlier on; see | |
6478 | cpp2a/constexpr-union6.C. */ | |
6479 | && !(TREE_CODE (t) == INIT_EXPR && refs->is_empty ())) | |
ceaaf873 | 6480 | { |
1d260ab0 NS |
6481 | bool has_active_member = CONSTRUCTOR_NELTS (*valp) != 0; |
6482 | tree inner = strip_array_types (reftype); | |
6483 | ||
6484 | if (has_active_member && cxx_dialect < cxx20) | |
ed4ec9ce | 6485 | { |
37244b21 PP |
6486 | if (!ctx->quiet) |
6487 | error_at (cp_expr_loc_or_input_loc (t), | |
6488 | "change of the active member of a union " | |
1d260ab0 NS |
6489 | "from %qD to %qD is not a constant expression " |
6490 | "before C++20", | |
37244b21 PP |
6491 | CONSTRUCTOR_ELT (*valp, 0)->index, |
6492 | index); | |
6493 | *non_constant_p = true; | |
ed4ec9ce | 6494 | } |
1d260ab0 NS |
6495 | else if (!is_access_expr |
6496 | || (TREE_CODE (t) == MODIFY_EXPR | |
6497 | && CLASS_TYPE_P (inner) | |
6498 | && !type_has_non_deleted_trivial_default_ctor (inner))) | |
6499 | { | |
6500 | /* Diagnose changing active union member after initialization | |
6501 | without a valid member access expression, as described in | |
6502 | [class.union.general] p5. */ | |
6503 | if (!ctx->quiet) | |
6504 | { | |
6505 | auto_diagnostic_group d; | |
6506 | if (has_active_member) | |
6507 | error_at (cp_expr_loc_or_input_loc (t), | |
6508 | "accessing %qD member instead of initialized " | |
6509 | "%qD member in constant expression", | |
6510 | index, CONSTRUCTOR_ELT (*valp, 0)->index); | |
6511 | else | |
6512 | error_at (cp_expr_loc_or_input_loc (t), | |
6513 | "accessing uninitialized member %qD", | |
6514 | index); | |
6515 | if (is_access_expr) | |
6516 | inform (DECL_SOURCE_LOCATION (index), | |
6517 | "%qD does not implicitly begin its lifetime " | |
6518 | "because %qT does not have a non-deleted " | |
6519 | "trivial default constructor, use " | |
6520 | "%<std::construct_at%> instead", | |
6521 | index, inner); | |
6522 | else | |
6523 | inform (DECL_SOURCE_LOCATION (index), | |
6524 | "initializing %qD requires a member access " | |
6525 | "expression as the left operand of the assignment", | |
6526 | index); | |
6527 | } | |
6528 | *non_constant_p = true; | |
6529 | } | |
6530 | else if (has_active_member && CONSTRUCTOR_NO_CLEARING (*valp)) | |
ceaaf873 | 6531 | { |
37244b21 PP |
6532 | /* Diagnose changing the active union member while the union |
6533 | is in the process of being initialized. */ | |
6534 | if (!ctx->quiet) | |
6535 | error_at (cp_expr_loc_or_input_loc (t), | |
6536 | "change of the active member of a union " | |
6537 | "from %qD to %qD during initialization", | |
6538 | CONSTRUCTOR_ELT (*valp, 0)->index, | |
6539 | index); | |
6540 | *non_constant_p = true; | |
ceaaf873 | 6541 | } |
37244b21 PP |
6542 | no_zero_init = true; |
6543 | } | |
88504f34 | 6544 | |
19077677 | 6545 | ctors.safe_push (valp); |
37244b21 | 6546 | vec_safe_push (indexes, index); |
88504f34 | 6547 | |
37244b21 | 6548 | constructor_elt *cep |
077dd9b3 | 6549 | = get_or_insert_ctor_field (*valp, index); |
37244b21 PP |
6550 | index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin()); |
6551 | ||
6552 | if (code == UNION_TYPE) | |
6553 | activated_union_member_p = true; | |
b599bf9d | 6554 | |
60813a46 | 6555 | valp = &cep->value; |
1d260ab0 | 6556 | type = reftype; |
60813a46 | 6557 | } |
60813a46 | 6558 | |
d19b4342 JM |
6559 | /* For initialization of an empty base, the original target will be |
6560 | *(base*)this, evaluation of which resolves to the object | |
6561 | argument, which has the derived type rather than the base type. */ | |
6562 | if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p | |
6563 | (initialized_type (init), type))) | |
6564 | { | |
6565 | gcc_assert (is_empty_class (TREE_TYPE (target))); | |
6566 | empty_base = true; | |
6567 | } | |
6568 | ||
04e1749c MP |
6569 | /* Detect modifying a constant object in constexpr evaluation. |
6570 | We have found a const object that is being modified. Figure out | |
6571 | if we need to issue an error. Consider | |
6572 | ||
6573 | struct A { | |
6574 | int n; | |
6575 | constexpr A() : n(1) { n = 2; } // #1 | |
6576 | }; | |
6577 | struct B { | |
6578 | const A a; | |
6579 | constexpr B() { a.n = 3; } // #2 | |
6580 | }; | |
6581 | constexpr B b{}; | |
6582 | ||
6583 | #1 is OK, since we're modifying an object under construction, but | |
6584 | #2 is wrong, since "a" is const and has been fully constructed. | |
6585 | To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR | |
6586 | which means that the object is read-only. For the example above, the | |
6587 | *ctors stack at the point of #2 will look like: | |
6588 | ||
6589 | ctors[0] = {.a={.n=2}} TREE_READONLY = 0 | |
6590 | ctors[1] = {.n=2} TREE_READONLY = 1 | |
6591 | ||
6592 | and we're modifying "b.a", so we search the stack and see if the | |
6593 | constructor for "b.a" has already run. */ | |
6594 | if (const_object_being_modified) | |
6595 | { | |
6596 | bool fail = false; | |
276a52d5 JJ |
6597 | tree const_objtype |
6598 | = strip_array_types (TREE_TYPE (const_object_being_modified)); | |
6599 | if (!CLASS_TYPE_P (const_objtype)) | |
04e1749c MP |
6600 | fail = true; |
6601 | else | |
6602 | { | |
6603 | /* [class.ctor]p5 "A constructor can be invoked for a const, | |
6604 | volatile, or const volatile object. const and volatile | |
6605 | semantics are not applied on an object under construction. | |
6606 | They come into effect when the constructor for the most | |
6607 | derived object ends." */ | |
19077677 | 6608 | for (tree *elt : ctors) |
04e1749c | 6609 | if (same_type_ignoring_top_level_qualifiers_p |
19077677 | 6610 | (TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt))) |
04e1749c | 6611 | { |
19077677 | 6612 | fail = TREE_READONLY (*elt); |
04e1749c MP |
6613 | break; |
6614 | } | |
6615 | } | |
6616 | if (fail) | |
6617 | { | |
6618 | if (!ctx->quiet) | |
6619 | modifying_const_object_error (t, const_object_being_modified); | |
6620 | *non_constant_p = true; | |
6621 | return t; | |
6622 | } | |
6623 | } | |
6624 | ||
e8b3c1bc | 6625 | if (!preeval) |
60813a46 | 6626 | { |
6c59b8a9 JM |
6627 | /* We're handling an INIT_EXPR of class type, so the value of the |
6628 | initializer can depend on the object it's initializing. */ | |
6629 | ||
60813a46 JM |
6630 | /* Create a new CONSTRUCTOR in case evaluation of the initializer |
6631 | wants to modify it. */ | |
acb2970c | 6632 | if (*valp == NULL_TREE) |
73a84269 | 6633 | { |
664beaf2 | 6634 | *valp = build_constructor (type, NULL); |
e8c48716 | 6635 | CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; |
73a84269 | 6636 | } |
d19b4342 | 6637 | new_ctx.ctor = empty_base ? NULL_TREE : *valp; |
60813a46 | 6638 | new_ctx.object = target; |
10d2f801 JM |
6639 | /* Avoid temporary materialization when initializing from a TARGET_EXPR. |
6640 | We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because | |
6641 | expansion of those trees uses ctx instead. */ | |
6642 | if (TREE_CODE (init) == TARGET_EXPR) | |
6643 | if (tree tinit = TARGET_EXPR_INITIAL (init)) | |
6644 | init = tinit; | |
72f76540 | 6645 | init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue, |
e8b3c1bc | 6646 | non_constant_p, overflow_p); |
37244b21 PP |
6647 | /* The hash table might have moved since the get earlier, and the |
6648 | initializer might have mutated the underlying CONSTRUCTORs, so we must | |
6649 | recompute VALP. */ | |
90bc2d09 | 6650 | valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR); |
37244b21 PP |
6651 | for (unsigned i = 0; i < vec_safe_length (indexes); i++) |
6652 | { | |
19077677 | 6653 | ctors[i] = valp; |
37244b21 PP |
6654 | constructor_elt *cep |
6655 | = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]); | |
6656 | valp = &cep->value; | |
6657 | } | |
60813a46 JM |
6658 | } |
6659 | ||
967cdbe6 JM |
6660 | if (*non_constant_p) |
6661 | return t; | |
6662 | ||
7574c916 | 6663 | /* Don't share a CONSTRUCTOR that might be changed later. */ |
0146e25f | 6664 | init = unshare_constructor (init); |
d96e8407 | 6665 | |
967cdbe6 JM |
6666 | gcc_checking_assert (!*valp || (same_type_ignoring_top_level_qualifiers_p |
6667 | (TREE_TYPE (*valp), type))); | |
d19b4342 JM |
6668 | if (empty_base) |
6669 | { | |
6670 | /* Just evaluate the initializer and return, since there's no actual data | |
6671 | to store, and we didn't build a CONSTRUCTOR. */ | |
967cdbe6 JM |
6672 | if (!*valp) |
6673 | { | |
6674 | /* But do make sure we have something in *valp. */ | |
6675 | *valp = build_constructor (type, nullptr); | |
6676 | CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; | |
6677 | } | |
6678 | } | |
6679 | else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR | |
6680 | && TREE_CODE (init) == CONSTRUCTOR) | |
acb2970c | 6681 | { |
d96e8407 JM |
6682 | /* An outer ctx->ctor might be pointing to *valp, so replace |
6683 | its contents. */ | |
6684 | CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init); | |
6685 | TREE_CONSTANT (*valp) = TREE_CONSTANT (init); | |
6686 | TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init); | |
e8c48716 JM |
6687 | CONSTRUCTOR_NO_CLEARING (*valp) |
6688 | = CONSTRUCTOR_NO_CLEARING (init); | |
acb2970c | 6689 | } |
60813a46 | 6690 | else |
d96e8407 | 6691 | *valp = init; |
2d63bc39 | 6692 | |
7eb5be6a MP |
6693 | /* After initialization, 'const' semantics apply to the value of the |
6694 | object. Make a note of this fact by marking the CONSTRUCTOR | |
6695 | TREE_READONLY. */ | |
6696 | if (TREE_CODE (t) == INIT_EXPR | |
f4998609 | 6697 | && !empty_base |
7eb5be6a MP |
6698 | && TREE_CODE (*valp) == CONSTRUCTOR |
6699 | && TYPE_READONLY (type)) | |
64da1b76 PP |
6700 | { |
6701 | if (INDIRECT_REF_P (target) | |
6702 | && (is_this_parameter | |
6703 | (tree_strip_nop_conversions (TREE_OPERAND (target, 0))))) | |
6704 | /* We've just initialized '*this' (perhaps via the target | |
6705 | constructor of a delegating constructor). Leave it up to the | |
6706 | caller that set 'this' to set TREE_READONLY appropriately. */ | |
6707 | gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p | |
967cdbe6 | 6708 | (TREE_TYPE (target), type) || empty_base); |
64da1b76 PP |
6709 | else |
6710 | TREE_READONLY (*valp) = true; | |
6711 | } | |
7eb5be6a | 6712 | |
d96e8407 JM |
6713 | /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing |
6714 | CONSTRUCTORs, if any. */ | |
d96e8407 JM |
6715 | bool c = TREE_CONSTANT (init); |
6716 | bool s = TREE_SIDE_EFFECTS (init); | |
19077677 JJ |
6717 | if (!indexes->is_empty ()) |
6718 | { | |
6719 | tree last = indexes->last (); | |
6720 | if (TREE_CODE (last) == REALPART_EXPR | |
6721 | || TREE_CODE (last) == IMAGPART_EXPR) | |
6722 | { | |
6723 | /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if | |
6724 | possible. */ | |
6725 | tree *cexpr = ctors.last (); | |
6726 | if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr), | |
6727 | TREE_OPERAND (*cexpr, 0), | |
6728 | TREE_OPERAND (*cexpr, 1))) | |
6729 | *cexpr = c; | |
6730 | else | |
6731 | { | |
6732 | TREE_CONSTANT (*cexpr) | |
6733 | = (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0)) | |
6734 | & TREE_CONSTANT (TREE_OPERAND (*cexpr, 1))); | |
6735 | TREE_SIDE_EFFECTS (*cexpr) | |
6736 | = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0)) | |
6737 | | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1))); | |
6738 | } | |
6739 | c = TREE_CONSTANT (*cexpr); | |
6740 | s = TREE_SIDE_EFFECTS (*cexpr); | |
6741 | } | |
6742 | } | |
37244b21 | 6743 | if (!c || s || activated_union_member_p) |
19077677 | 6744 | for (tree *elt : ctors) |
d96e8407 | 6745 | { |
19077677 JJ |
6746 | if (TREE_CODE (*elt) != CONSTRUCTOR) |
6747 | continue; | |
d96e8407 | 6748 | if (!c) |
19077677 | 6749 | TREE_CONSTANT (*elt) = false; |
d96e8407 | 6750 | if (s) |
19077677 | 6751 | TREE_SIDE_EFFECTS (*elt) = true; |
b599bf9d PP |
6752 | /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of |
6753 | this union. */ | |
19077677 JJ |
6754 | if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE) |
6755 | CONSTRUCTOR_NO_CLEARING (*elt) = false; | |
d96e8407 | 6756 | } |
60813a46 | 6757 | |
967cdbe6 | 6758 | if (lval) |
60813a46 JM |
6759 | return target; |
6760 | else | |
3bdf0a9b | 6761 | return init; |
60813a46 JM |
6762 | } |
6763 | ||
6764 | /* Evaluate a ++ or -- expression. */ | |
6765 | ||
6766 | static tree | |
6767 | cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t, | |
72f76540 | 6768 | value_cat lval, |
60813a46 JM |
6769 | bool *non_constant_p, bool *overflow_p) |
6770 | { | |
6771 | enum tree_code code = TREE_CODE (t); | |
6772 | tree type = TREE_TYPE (t); | |
6773 | tree op = TREE_OPERAND (t, 0); | |
6774 | tree offset = TREE_OPERAND (t, 1); | |
6775 | gcc_assert (TREE_CONSTANT (offset)); | |
6776 | ||
d85569f6 MP |
6777 | /* OFFSET is constant, but perhaps not constant enough. We need to |
6778 | e.g. bash FLOAT_EXPRs to REAL_CSTs. */ | |
6779 | offset = fold_simple (offset); | |
6780 | ||
60813a46 | 6781 | /* The operand as an lvalue. */ |
72f76540 | 6782 | op = cxx_eval_constant_expression (ctx, op, vc_glvalue, |
5a804683 | 6783 | non_constant_p, overflow_p); |
60813a46 JM |
6784 | |
6785 | /* The operand as an rvalue. */ | |
84dd815f | 6786 | tree val |
72f76540 | 6787 | = cxx_eval_constant_expression (ctx, op, vc_prvalue, |
84dd815f | 6788 | non_constant_p, overflow_p); |
caee690e JM |
6789 | /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to |
6790 | a local array in a constexpr function. */ | |
71a93b08 | 6791 | bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val)); |
caee690e JM |
6792 | if (!ptr) |
6793 | VERIFY_CONSTANT (val); | |
60813a46 JM |
6794 | |
6795 | /* The modified value. */ | |
6796 | bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR); | |
ac2f8d26 | 6797 | tree mod; |
71a93b08 | 6798 | if (INDIRECT_TYPE_P (type)) |
ac2f8d26 JM |
6799 | { |
6800 | /* The middle end requires pointers to use POINTER_PLUS_EXPR. */ | |
6801 | offset = convert_to_ptrofftype (offset); | |
6802 | if (!inc) | |
6803 | offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset); | |
6804 | mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset); | |
6805 | } | |
da8c362c JJ |
6806 | else if (c_promoting_integer_type_p (type) |
6807 | && !TYPE_UNSIGNED (type) | |
6808 | && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) | |
6809 | { | |
6810 | offset = fold_convert (integer_type_node, offset); | |
6811 | mod = fold_convert (integer_type_node, val); | |
6812 | tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node, | |
6813 | mod, offset); | |
6814 | mod = fold_convert (type, t); | |
6815 | if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t)) | |
6816 | TREE_OVERFLOW (mod) = false; | |
6817 | } | |
ac2f8d26 JM |
6818 | else |
6819 | mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset); | |
caee690e JM |
6820 | if (!ptr) |
6821 | VERIFY_CONSTANT (mod); | |
60813a46 JM |
6822 | |
6823 | /* Storing the modified value. */ | |
04e1749c MP |
6824 | tree store = build2_loc (cp_expr_loc_or_loc (t, input_location), |
6825 | MODIFY_EXPR, type, op, mod); | |
d1bba463 PP |
6826 | mod = cxx_eval_constant_expression (ctx, store, lval, |
6827 | non_constant_p, overflow_p); | |
ecdcd560 | 6828 | ggc_free (store); |
d1bba463 PP |
6829 | if (*non_constant_p) |
6830 | return t; | |
60813a46 JM |
6831 | |
6832 | /* And the value of the expression. */ | |
6833 | if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR) | |
d1bba463 PP |
6834 | /* Prefix ops are lvalues, but the caller might want an rvalue; |
6835 | lval has already been taken into account in the store above. */ | |
6836 | return mod; | |
60813a46 JM |
6837 | else |
6838 | /* Postfix ops are rvalues. */ | |
6839 | return val; | |
6840 | } | |
6841 | ||
56632b27 JM |
6842 | /* Predicates for the meaning of *jump_target. */ |
6843 | ||
6844 | static bool | |
6845 | returns (tree *jump_target) | |
6846 | { | |
6847 | return *jump_target | |
9c6a4bee | 6848 | && TREE_CODE (*jump_target) == RETURN_EXPR; |
56632b27 JM |
6849 | } |
6850 | ||
6851 | static bool | |
6852 | breaks (tree *jump_target) | |
6853 | { | |
6854 | return *jump_target | |
06ec22b7 JM |
6855 | && ((TREE_CODE (*jump_target) == LABEL_DECL |
6856 | && LABEL_DECL_BREAK (*jump_target)) | |
3075affd | 6857 | || TREE_CODE (*jump_target) == BREAK_STMT |
06ec22b7 | 6858 | || TREE_CODE (*jump_target) == EXIT_EXPR); |
56632b27 JM |
6859 | } |
6860 | ||
6861 | static bool | |
6862 | continues (tree *jump_target) | |
6863 | { | |
6864 | return *jump_target | |
3075affd JM |
6865 | && ((TREE_CODE (*jump_target) == LABEL_DECL |
6866 | && LABEL_DECL_CONTINUE (*jump_target)) | |
6867 | || TREE_CODE (*jump_target) == CONTINUE_STMT); | |
6868 | ||
56632b27 JM |
6869 | } |
6870 | ||
6871 | static bool | |
6872 | switches (tree *jump_target) | |
6873 | { | |
6874 | return *jump_target | |
6875 | && TREE_CODE (*jump_target) == INTEGER_CST; | |
6876 | } | |
6877 | ||
6878 | /* Subroutine of cxx_eval_statement_list. Determine whether the statement | |
4b390698 JJ |
6879 | STMT matches *jump_target. If we're looking for a case label and we see |
6880 | the default label, note it in ctx->css_state. */ | |
56632b27 JM |
6881 | |
6882 | static bool | |
4b390698 | 6883 | label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt) |
56632b27 | 6884 | { |
56632b27 JM |
6885 | switch (TREE_CODE (*jump_target)) |
6886 | { | |
6887 | case LABEL_DECL: | |
6888 | if (TREE_CODE (stmt) == LABEL_EXPR | |
6889 | && LABEL_EXPR_LABEL (stmt) == *jump_target) | |
6890 | return true; | |
6891 | break; | |
6892 | ||
6893 | case INTEGER_CST: | |
6894 | if (TREE_CODE (stmt) == CASE_LABEL_EXPR) | |
6895 | { | |
4b390698 | 6896 | gcc_assert (ctx->css_state != NULL); |
56632b27 | 6897 | if (!CASE_LOW (stmt)) |
4b390698 JJ |
6898 | { |
6899 | /* default: should appear just once in a SWITCH_EXPR | |
6900 | body (excluding nested SWITCH_EXPR). */ | |
6901 | gcc_assert (*ctx->css_state != css_default_seen); | |
6902 | /* When evaluating SWITCH_EXPR body for the second time, | |
6903 | return true for the default: label. */ | |
6904 | if (*ctx->css_state == css_default_processing) | |
6905 | return true; | |
6906 | *ctx->css_state = css_default_seen; | |
6907 | } | |
385ed708 JJ |
6908 | else if (CASE_HIGH (stmt)) |
6909 | { | |
6910 | if (tree_int_cst_le (CASE_LOW (stmt), *jump_target) | |
6911 | && tree_int_cst_le (*jump_target, CASE_HIGH (stmt))) | |
6912 | return true; | |
6913 | } | |
56632b27 JM |
6914 | else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt))) |
6915 | return true; | |
6916 | } | |
6917 | break; | |
6918 | ||
43574e4f JJ |
6919 | case BREAK_STMT: |
6920 | case CONTINUE_STMT: | |
6921 | /* These two are handled directly in cxx_eval_loop_expr by testing | |
6922 | breaks (jump_target) or continues (jump_target). */ | |
6923 | break; | |
6924 | ||
56632b27 JM |
6925 | default: |
6926 | gcc_unreachable (); | |
6927 | } | |
6928 | return false; | |
6929 | } | |
6930 | ||
6931 | /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump | |
6932 | semantics, for switch, break, continue, and return. */ | |
6933 | ||
6934 | static tree | |
6935 | cxx_eval_statement_list (const constexpr_ctx *ctx, tree t, | |
56632b27 JM |
6936 | bool *non_constant_p, bool *overflow_p, |
6937 | tree *jump_target) | |
6938 | { | |
58611fb6 | 6939 | tree local_target; |
345edb37 JJ |
6940 | /* In a statement-expression we want to return the last value. |
6941 | For empty statement expression return void_node. */ | |
6942 | tree r = void_node; | |
58611fb6 JM |
6943 | if (!jump_target) |
6944 | { | |
6945 | local_target = NULL_TREE; | |
6946 | jump_target = &local_target; | |
6947 | } | |
72f76540 | 6948 | for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i) |
56632b27 | 6949 | { |
72f76540 JM |
6950 | tree stmt = *i; |
6951 | ||
0250ba58 MP |
6952 | /* We've found a continue, so skip everything until we reach |
6953 | the label its jumping to. */ | |
6954 | if (continues (jump_target)) | |
6955 | { | |
6956 | if (label_matches (ctx, jump_target, stmt)) | |
6957 | /* Found it. */ | |
6958 | *jump_target = NULL_TREE; | |
6959 | else | |
6960 | continue; | |
6961 | } | |
6ef72c36 JJ |
6962 | if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT) |
6963 | continue; | |
72f76540 JM |
6964 | |
6965 | value_cat lval = vc_discard; | |
6966 | /* The result of a statement-expression is not wrapped in EXPR_STMT. */ | |
6967 | if (tsi_one_before_end_p (i) && TREE_CODE (stmt) != EXPR_STMT) | |
6968 | lval = vc_prvalue; | |
6969 | ||
6970 | r = cxx_eval_constant_expression (ctx, stmt, lval, | |
58611fb6 JM |
6971 | non_constant_p, overflow_p, |
6972 | jump_target); | |
56632b27 JM |
6973 | if (*non_constant_p) |
6974 | break; | |
6975 | if (returns (jump_target) || breaks (jump_target)) | |
6976 | break; | |
6977 | } | |
ce36ba09 JM |
6978 | if (*jump_target && jump_target == &local_target) |
6979 | { | |
6980 | /* We aren't communicating the jump to our caller, so give up. We don't | |
6981 | need to support evaluation of jumps out of statement-exprs. */ | |
6982 | if (!ctx->quiet) | |
f9d0ca40 | 6983 | error_at (cp_expr_loc_or_input_loc (r), |
ce36ba09 JM |
6984 | "statement is not a constant expression"); |
6985 | *non_constant_p = true; | |
6986 | } | |
58611fb6 | 6987 | return r; |
56632b27 JM |
6988 | } |
6989 | ||
6990 | /* Evaluate a LOOP_EXPR for side-effects. Handles break and return | |
6991 | semantics; continue semantics are covered by cxx_eval_statement_list. */ | |
6992 | ||
6993 | static tree | |
6994 | cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t, | |
56632b27 JM |
6995 | bool *non_constant_p, bool *overflow_p, |
6996 | tree *jump_target) | |
6997 | { | |
8e007055 JJ |
6998 | tree local_target; |
6999 | if (!jump_target) | |
7000 | { | |
7001 | local_target = NULL_TREE; | |
7002 | jump_target = &local_target; | |
7003 | } | |
39dce2b7 | 7004 | |
43574e4f | 7005 | tree body, cond = NULL_TREE, expr = NULL_TREE; |
5ec2cd9f | 7006 | int count = 0; |
43574e4f JJ |
7007 | switch (TREE_CODE (t)) |
7008 | { | |
7009 | case LOOP_EXPR: | |
7010 | body = LOOP_EXPR_BODY (t); | |
7011 | break; | |
7012 | case DO_STMT: | |
7013 | body = DO_BODY (t); | |
7014 | cond = DO_COND (t); | |
7015 | break; | |
7016 | case WHILE_STMT: | |
7017 | body = WHILE_BODY (t); | |
7018 | cond = WHILE_COND (t); | |
7019 | count = -1; | |
7020 | break; | |
7021 | case FOR_STMT: | |
7022 | if (FOR_INIT_STMT (t)) | |
72f76540 | 7023 | cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard, |
43574e4f JJ |
7024 | non_constant_p, overflow_p, jump_target); |
7025 | if (*non_constant_p) | |
7026 | return NULL_TREE; | |
7027 | body = FOR_BODY (t); | |
7028 | cond = FOR_COND (t); | |
7029 | expr = FOR_EXPR (t); | |
7030 | count = -1; | |
7031 | break; | |
7032 | default: | |
7033 | gcc_unreachable (); | |
7034 | } | |
d259b234 | 7035 | do |
56632b27 | 7036 | { |
43574e4f JJ |
7037 | if (count != -1) |
7038 | { | |
7039 | if (body) | |
9fdbd7d6 | 7040 | cxx_eval_constant_expression (ctx, body, vc_discard, |
43574e4f JJ |
7041 | non_constant_p, overflow_p, |
7042 | jump_target); | |
7043 | if (breaks (jump_target)) | |
7044 | { | |
7045 | *jump_target = NULL_TREE; | |
7046 | break; | |
7047 | } | |
39dce2b7 | 7048 | |
43574e4f JJ |
7049 | if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target)) |
7050 | *jump_target = NULL_TREE; | |
7051 | ||
7052 | if (expr) | |
9fdbd7d6 | 7053 | cxx_eval_constant_expression (ctx, expr, vc_prvalue, |
43574e4f JJ |
7054 | non_constant_p, overflow_p, |
7055 | jump_target); | |
7056 | } | |
7057 | ||
7058 | if (cond) | |
7059 | { | |
7060 | tree res | |
9fdbd7d6 | 7061 | = cxx_eval_constant_expression (ctx, cond, vc_prvalue, |
43574e4f JJ |
7062 | non_constant_p, overflow_p, |
7063 | jump_target); | |
7064 | if (res) | |
7065 | { | |
7066 | if (verify_constant (res, ctx->quiet, non_constant_p, | |
7067 | overflow_p)) | |
7068 | break; | |
7069 | if (integer_zerop (res)) | |
7070 | break; | |
7071 | } | |
7072 | else | |
7073 | gcc_assert (*jump_target); | |
7074 | } | |
39dce2b7 | 7075 | |
5ec2cd9f JM |
7076 | if (++count >= constexpr_loop_limit) |
7077 | { | |
7078 | if (!ctx->quiet) | |
f9d0ca40 | 7079 | error_at (cp_expr_loc_or_input_loc (t), |
84fa214d | 7080 | "%<constexpr%> loop iteration count exceeds limit of %d " |
a3f9f006 | 7081 | "(use %<-fconstexpr-loop-limit=%> to increase the limit)", |
5ec2cd9f JM |
7082 | constexpr_loop_limit); |
7083 | *non_constant_p = true; | |
7084 | break; | |
7085 | } | |
56632b27 | 7086 | } |
4b390698 JJ |
7087 | while (!returns (jump_target) |
7088 | && !breaks (jump_target) | |
43574e4f JJ |
7089 | && !continues (jump_target) |
7090 | && (!switches (jump_target) || count == 0) | |
4b390698 | 7091 | && !*non_constant_p); |
d259b234 | 7092 | |
56632b27 JM |
7093 | return NULL_TREE; |
7094 | } | |
7095 | ||
7096 | /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump | |
7097 | semantics. */ | |
7098 | ||
7099 | static tree | |
7100 | cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t, | |
56632b27 JM |
7101 | bool *non_constant_p, bool *overflow_p, |
7102 | tree *jump_target) | |
7103 | { | |
43574e4f JJ |
7104 | tree cond |
7105 | = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t); | |
72f76540 | 7106 | cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue, |
5a804683 | 7107 | non_constant_p, overflow_p); |
56632b27 | 7108 | VERIFY_CONSTANT (cond); |
39d98902 MP |
7109 | if (TREE_CODE (cond) != INTEGER_CST) |
7110 | { | |
7111 | /* If the condition doesn't reduce to an INTEGER_CST it isn't a usable | |
7112 | switch condition even if it's constant enough for other things | |
7113 | (c++/113545). */ | |
7114 | gcc_checking_assert (ctx->quiet); | |
7115 | *non_constant_p = true; | |
7116 | return t; | |
7117 | } | |
7118 | ||
56632b27 JM |
7119 | *jump_target = cond; |
7120 | ||
43574e4f JJ |
7121 | tree body |
7122 | = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t); | |
4b390698 JJ |
7123 | constexpr_ctx new_ctx = *ctx; |
7124 | constexpr_switch_state css = css_default_not_seen; | |
7125 | new_ctx.css_state = &css; | |
72f76540 | 7126 | cxx_eval_constant_expression (&new_ctx, body, vc_discard, |
4b390698 JJ |
7127 | non_constant_p, overflow_p, jump_target); |
7128 | if (switches (jump_target) && css == css_default_seen) | |
7129 | { | |
7130 | /* If the SWITCH_EXPR body has default: label, process it once again, | |
7131 | this time instructing label_matches to return true for default: | |
7132 | label on switches (jump_target). */ | |
7133 | css = css_default_processing; | |
72f76540 | 7134 | cxx_eval_constant_expression (&new_ctx, body, vc_discard, |
4b390698 JJ |
7135 | non_constant_p, overflow_p, jump_target); |
7136 | } | |
56632b27 JM |
7137 | if (breaks (jump_target) || switches (jump_target)) |
7138 | *jump_target = NULL_TREE; | |
7139 | return NULL_TREE; | |
7140 | } | |
7141 | ||
89262ec6 JM |
7142 | /* Find the object of TYPE under initialization in CTX. */ |
7143 | ||
7144 | static tree | |
72f76540 | 7145 | lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type) |
89262ec6 | 7146 | { |
fbd603c4 | 7147 | if (!ctx) |
89262ec6 JM |
7148 | return NULL_TREE; |
7149 | ||
49a86fce PP |
7150 | /* Prefer the outermost matching object, but don't cross |
7151 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */ | |
7152 | if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor)) | |
7153 | if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type)) | |
7154 | return outer_ob; | |
7155 | ||
89262ec6 JM |
7156 | /* We could use ctx->object unconditionally, but using ctx->ctor when we |
7157 | can is a minor optimization. */ | |
fbd603c4 | 7158 | if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type)) |
89262ec6 JM |
7159 | return ctx->ctor; |
7160 | ||
fbd603c4 JM |
7161 | if (!ctx->object) |
7162 | return NULL_TREE; | |
7163 | ||
89262ec6 JM |
7164 | /* Since an object cannot have a field of its own type, we can search outward |
7165 | from ctx->object to find the unique containing object of TYPE. */ | |
7166 | tree ob = ctx->object; | |
7167 | while (ob) | |
7168 | { | |
7169 | if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type)) | |
7170 | break; | |
7171 | if (handled_component_p (ob)) | |
7172 | ob = TREE_OPERAND (ob, 0); | |
7173 | else | |
7174 | ob = NULL_TREE; | |
7175 | } | |
7176 | ||
7177 | return ob; | |
7178 | } | |
7179 | ||
c85f8dbb MP |
7180 | /* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is |
7181 | true, we're checking a constexpr function body. */ | |
7c814975 MP |
7182 | |
7183 | static void | |
c85f8dbb | 7184 | inline_asm_in_constexpr_error (location_t loc, bool fundef_p) |
7c814975 MP |
7185 | { |
7186 | auto_diagnostic_group d; | |
c85f8dbb MP |
7187 | if (constexpr_error (loc, fundef_p, "inline assembly is not a " |
7188 | "constant expression")) | |
7189 | inform (loc, "only unevaluated inline assembly is allowed in a " | |
7190 | "%<constexpr%> function in C++20"); | |
7c814975 MP |
7191 | } |
7192 | ||
76b75018 JM |
7193 | /* We're getting the constant value of DECL in a manifestly constant-evaluated |
7194 | context; maybe complain about that. */ | |
7195 | ||
7196 | static void | |
7197 | maybe_warn_about_constant_value (location_t loc, tree decl) | |
7198 | { | |
7199 | static bool explained = false; | |
7200 | if (cxx_dialect >= cxx17 | |
7201 | && warn_interference_size | |
00f34291 | 7202 | && !OPTION_SET_P (param_destruct_interfere_size) |
76b75018 JM |
7203 | && DECL_CONTEXT (decl) == std_node |
7204 | && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size") | |
7205 | && (LOCATION_FILE (input_location) != main_input_filename | |
7206 | || module_exporting_p ()) | |
7207 | && warning_at (loc, OPT_Winterference_size, "use of %qD", decl) | |
7208 | && !explained) | |
7209 | { | |
7210 | explained = true; | |
7211 | inform (loc, "its value can vary between compiler versions or " | |
7212 | "with different %<-mtune%> or %<-mcpu%> flags"); | |
7213 | inform (loc, "if this use is part of a public ABI, change it to " | |
7214 | "instead use a constant variable you define"); | |
7215 | inform (loc, "the default value for the current CPU tuning " | |
7216 | "is %d bytes", param_destruct_interfere_size); | |
7217 | inform (loc, "you can stabilize this value with %<--param " | |
7218 | "hardware_destructive_interference_size=%d%>, or disable " | |
7219 | "this warning with %<-Wno-interference-size%>", | |
7220 | param_destruct_interfere_size); | |
7221 | } | |
7222 | } | |
7223 | ||
0a0c2c3f JJ |
7224 | /* For element type ELT_TYPE, return the appropriate type of the heap object |
7225 | containing such element(s). COOKIE_SIZE is NULL or the size of cookie | |
7226 | in bytes. If COOKIE_SIZE is NULL, return array type | |
7227 | ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return | |
7228 | struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; } | |
64aa48ce | 7229 | where N is computed such that the size of the struct fits into FULL_SIZE. |
0a0c2c3f JJ |
7230 | If ARG_SIZE is non-NULL, it is the first argument to the new operator. |
7231 | It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE | |
7232 | will be also 0 and so it is not possible to determine the actual array | |
7233 | size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant | |
7234 | expression evaluation of subexpressions of ARG_SIZE. */ | |
7235 | ||
7236 | static tree | |
7237 | build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type, | |
7238 | tree cookie_size, tree full_size, tree arg_size, | |
7239 | bool *non_constant_p, bool *overflow_p) | |
7240 | { | |
7241 | gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size)); | |
7242 | gcc_assert (tree_fits_uhwi_p (full_size)); | |
7243 | unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0; | |
7244 | if (arg_size) | |
7245 | { | |
7246 | STRIP_NOPS (arg_size); | |
7247 | if (cookie_size) | |
7248 | { | |
7249 | if (TREE_CODE (arg_size) != PLUS_EXPR) | |
7250 | arg_size = NULL_TREE; | |
7251 | else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST | |
7252 | && tree_int_cst_equal (cookie_size, | |
7253 | TREE_OPERAND (arg_size, 0))) | |
7254 | { | |
7255 | arg_size = TREE_OPERAND (arg_size, 1); | |
7256 | STRIP_NOPS (arg_size); | |
7257 | } | |
7258 | else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST | |
7259 | && tree_int_cst_equal (cookie_size, | |
7260 | TREE_OPERAND (arg_size, 1))) | |
7261 | { | |
7262 | arg_size = TREE_OPERAND (arg_size, 0); | |
7263 | STRIP_NOPS (arg_size); | |
7264 | } | |
7265 | else | |
7266 | arg_size = NULL_TREE; | |
7267 | } | |
7268 | if (arg_size && TREE_CODE (arg_size) == MULT_EXPR) | |
7269 | { | |
7270 | tree op0 = TREE_OPERAND (arg_size, 0); | |
7271 | tree op1 = TREE_OPERAND (arg_size, 1); | |
7272 | if (integer_zerop (op0)) | |
7273 | arg_size | |
72f76540 JM |
7274 | = cxx_eval_constant_expression (ctx, op1, vc_prvalue, |
7275 | non_constant_p, overflow_p); | |
0a0c2c3f JJ |
7276 | else if (integer_zerop (op1)) |
7277 | arg_size | |
72f76540 JM |
7278 | = cxx_eval_constant_expression (ctx, op0, vc_prvalue, |
7279 | non_constant_p, overflow_p); | |
0a0c2c3f JJ |
7280 | else |
7281 | arg_size = NULL_TREE; | |
7282 | } | |
7283 | else | |
7284 | arg_size = NULL_TREE; | |
7285 | } | |
7286 | ||
7287 | unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size); | |
7288 | if (!arg_size) | |
7289 | { | |
7290 | unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type); | |
7291 | gcc_assert (fsz >= csz); | |
7292 | fsz -= csz; | |
7293 | if (esz) | |
7294 | fsz /= esz; | |
7295 | } | |
7296 | tree itype2 = build_index_type (size_int (fsz - 1)); | |
7297 | if (!cookie_size) | |
7298 | return build_cplus_array_type (elt_type, itype2); | |
7299 | return build_new_constexpr_heap_type (elt_type, cookie_size, itype2); | |
7300 | } | |
7301 | ||
2d76680f PC |
7302 | /* Attempt to reduce the expression T to a constant value. |
7303 | On failure, issue diagnostic and return error_mark_node. */ | |
7304 | /* FIXME unify with c_fully_fold */ | |
60813a46 | 7305 | /* FIXME overflow_p is too global */ |
2d76680f PC |
7306 | |
7307 | static tree | |
3e605b20 | 7308 | cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, |
72f76540 | 7309 | value_cat lval, |
56632b27 | 7310 | bool *non_constant_p, bool *overflow_p, |
3075affd | 7311 | tree *jump_target /* = NULL */) |
2d76680f | 7312 | { |
4b390698 JJ |
7313 | if (jump_target && *jump_target) |
7314 | { | |
7315 | /* If we are jumping, ignore all statements/expressions except those | |
7316 | that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */ | |
7317 | switch (TREE_CODE (t)) | |
7318 | { | |
7319 | case BIND_EXPR: | |
7320 | case STATEMENT_LIST: | |
7321 | case LOOP_EXPR: | |
7322 | case COND_EXPR: | |
43574e4f JJ |
7323 | case IF_STMT: |
7324 | case DO_STMT: | |
7325 | case WHILE_STMT: | |
7326 | case FOR_STMT: | |
4b390698 JJ |
7327 | break; |
7328 | case LABEL_EXPR: | |
7329 | case CASE_LABEL_EXPR: | |
7330 | if (label_matches (ctx, jump_target, t)) | |
7331 | /* Found it. */ | |
7332 | *jump_target = NULL_TREE; | |
7333 | return NULL_TREE; | |
7334 | default: | |
7335 | return NULL_TREE; | |
7336 | } | |
7337 | } | |
3075affd | 7338 | if (error_operand_p (t)) |
2d76680f PC |
7339 | { |
7340 | *non_constant_p = true; | |
7341 | return t; | |
7342 | } | |
7a649ef5 | 7343 | |
5ebe5bcf NS |
7344 | /* Change the input location to the currently processed expression for |
7345 | better error messages when a subexpression has no location. */ | |
6821245b | 7346 | location_t loc = cp_expr_loc_or_input_loc (t); |
5ebe5bcf | 7347 | iloc_sentinel sentinel (loc); |
6821245b | 7348 | |
7a649ef5 JM |
7349 | STRIP_ANY_LOCATION_WRAPPER (t); |
7350 | ||
2d76680f PC |
7351 | if (CONSTANT_CLASS_P (t)) |
7352 | { | |
61637db3 JJ |
7353 | if (TREE_OVERFLOW (t)) |
7354 | { | |
7355 | if (!ctx->quiet) | |
7356 | permerror (input_location, "overflow in constant expression"); | |
7357 | if (!flag_permissive || ctx->quiet) | |
7358 | *overflow_p = true; | |
7359 | } | |
8bada5cd MS |
7360 | |
7361 | if (TREE_CODE (t) == INTEGER_CST | |
9f613f06 | 7362 | && TYPE_PTR_P (TREE_TYPE (t)) |
f45610a4 JJ |
7363 | /* INTEGER_CST with pointer-to-method type is only used |
7364 | for a virtual method in a pointer to member function. | |
7365 | Don't reject those. */ | |
7366 | && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE | |
8bada5cd MS |
7367 | && !integer_zerop (t)) |
7368 | { | |
7369 | if (!ctx->quiet) | |
7370 | error ("value %qE of type %qT is not a constant expression", | |
7371 | t, TREE_TYPE (t)); | |
7372 | *non_constant_p = true; | |
7373 | } | |
7374 | ||
2d76680f PC |
7375 | return t; |
7376 | } | |
2d76680f | 7377 | |
a15ffa22 | 7378 | /* Avoid excessively long constexpr evaluations. */ |
8e007055 | 7379 | if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit) |
a15ffa22 JJ |
7380 | { |
7381 | if (!ctx->quiet) | |
6821245b | 7382 | error_at (loc, |
a15ffa22 | 7383 | "%<constexpr%> evaluation operation count exceeds limit of " |
a9c697b8 | 7384 | "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)", |
a15ffa22 | 7385 | constexpr_ops_limit); |
8e007055 | 7386 | ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT); |
a15ffa22 JJ |
7387 | *non_constant_p = true; |
7388 | return t; | |
7389 | } | |
7390 | ||
7a649ef5 JM |
7391 | constexpr_ctx new_ctx; |
7392 | tree r = t; | |
7393 | ||
8bada5cd MS |
7394 | tree_code tcode = TREE_CODE (t); |
7395 | switch (tcode) | |
2d76680f | 7396 | { |
60813a46 | 7397 | case RESULT_DECL: |
92a596e8 | 7398 | if (lval) |
60813a46 JM |
7399 | return t; |
7400 | /* We ask for an rvalue for the RESULT_DECL when indirecting | |
bf66b9b4 AH |
7401 | through an invisible reference, or in named return value |
7402 | optimization. */ | |
e6a29aab JM |
7403 | if (tree v = ctx->global->get_value (t)) |
7404 | return v; | |
1efb1dc2 MP |
7405 | else |
7406 | { | |
7407 | if (!ctx->quiet) | |
7408 | error ("%qE is not a constant expression", t); | |
7409 | *non_constant_p = true; | |
7410 | } | |
7411 | break; | |
60813a46 | 7412 | |
2d76680f | 7413 | case VAR_DECL: |
70f40fea | 7414 | if (DECL_HAS_VALUE_EXPR_P (t)) |
c59fa7ea JM |
7415 | { |
7416 | if (is_normal_capture_proxy (t) | |
7417 | && current_function_decl == DECL_CONTEXT (t)) | |
7418 | { | |
7419 | /* Function parms aren't constexpr within the function | |
7420 | definition, so don't try to look at the closure. But if the | |
7421 | captured variable is constant, try to evaluate it directly. */ | |
7422 | r = DECL_CAPTURED_VARIABLE (t); | |
7423 | tree type = TREE_TYPE (t); | |
7424 | if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r))) | |
7425 | { | |
7426 | /* Adjust r to match the reference-ness of t. */ | |
7427 | if (TYPE_REF_P (type)) | |
7428 | r = build_address (r); | |
7429 | else | |
7430 | r = convert_from_reference (r); | |
7431 | } | |
7432 | } | |
7433 | else | |
7434 | r = DECL_VALUE_EXPR (t); | |
7435 | return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, | |
7436 | overflow_p); | |
7437 | } | |
191816a3 | 7438 | /* fall through */ |
7c83622c | 7439 | case CONST_DECL: |
e53b6e56 | 7440 | /* We used to not check lval for CONST_DECL, but darwin.cc uses |
7c83622c | 7441 | CONST_DECL for aggregate constants. */ |
92a596e8 | 7442 | if (lval) |
2d76680f | 7443 | return t; |
ef529765 JM |
7444 | else if (t == ctx->object) |
7445 | return ctx->ctor; | |
7446 | if (VAR_P (t)) | |
9fdbd7d6 NS |
7447 | { |
7448 | if (tree v = ctx->global->get_value (t)) | |
ef529765 | 7449 | { |
e6a29aab | 7450 | r = v; |
ef529765 JM |
7451 | break; |
7452 | } | |
9fdbd7d6 NS |
7453 | if (ctx->global->is_outside_lifetime (t)) |
7454 | { | |
7455 | if (!ctx->quiet) | |
7456 | outside_lifetime_error (loc, t); | |
7457 | *non_constant_p = true; | |
7458 | break; | |
7459 | } | |
7460 | } | |
3a0bc47c | 7461 | if (ctx->manifestly_const_eval == mce_true) |
76b75018 | 7462 | maybe_warn_about_constant_value (loc, t); |
f4fce183 | 7463 | if (COMPLETE_TYPE_P (TREE_TYPE (t)) |
dbcd32f8 | 7464 | && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
7dc2b4a2 JM |
7465 | { |
7466 | /* If the class is empty, we aren't actually loading anything. */ | |
7467 | r = build_constructor (TREE_TYPE (t), NULL); | |
7468 | TREE_CONSTANT (r) = true; | |
7469 | } | |
7470 | else if (ctx->strict) | |
8c00059c | 7471 | r = decl_really_constant_value (t, /*unshare_p=*/false); |
69eb4fde | 7472 | else |
8c00059c | 7473 | r = decl_constant_value (t, /*unshare_p=*/false); |
2d76680f PC |
7474 | if (TREE_CODE (r) == TARGET_EXPR |
7475 | && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR) | |
7476 | r = TARGET_EXPR_INITIAL (r); | |
99d114c1 MP |
7477 | if (DECL_P (r) |
7478 | /* P2280 allows references to unknown. */ | |
7479 | && !(VAR_P (t) && TYPE_REF_P (TREE_TYPE (t)))) | |
2d76680f | 7480 | { |
2b3ab879 | 7481 | if (!ctx->quiet) |
c85f8dbb | 7482 | non_const_var_error (loc, r, /*fundef_p*/false); |
2d76680f PC |
7483 | *non_constant_p = true; |
7484 | } | |
7485 | break; | |
7486 | ||
96a95ac1 AO |
7487 | case DEBUG_BEGIN_STMT: |
7488 | /* ??? It might be nice to retain this information somehow, so | |
7489 | as to be able to step into a constexpr function call. */ | |
7490 | /* Fall through. */ | |
7491 | ||
2d76680f PC |
7492 | case FUNCTION_DECL: |
7493 | case TEMPLATE_DECL: | |
7494 | case LABEL_DECL: | |
56632b27 JM |
7495 | case LABEL_EXPR: |
7496 | case CASE_LABEL_EXPR: | |
4b390698 | 7497 | case PREDICT_EXPR: |
2d76680f PC |
7498 | return t; |
7499 | ||
7500 | case PARM_DECL: | |
9f613f06 | 7501 | if (lval && !TYPE_REF_P (TREE_TYPE (t))) |
60813a46 | 7502 | /* glvalue use. */; |
12a11620 | 7503 | else if (tree v = ctx->global->get_value (t)) |
e6a29aab | 7504 | r = v; |
92a596e8 | 7505 | else if (lval) |
2d76680f | 7506 | /* Defer in case this is only used for its type. */; |
9fdbd7d6 NS |
7507 | else if (ctx->global->is_outside_lifetime (t)) |
7508 | { | |
7509 | if (!ctx->quiet) | |
7510 | outside_lifetime_error (loc, t); | |
7511 | *non_constant_p = true; | |
7512 | break; | |
7513 | } | |
1c2f613f | 7514 | else if (COMPLETE_TYPE_P (TREE_TYPE (t)) |
dbcd32f8 | 7515 | && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
052beba4 JM |
7516 | { |
7517 | /* If the class is empty, we aren't actually loading anything. */ | |
7518 | r = build_constructor (TREE_TYPE (t), NULL); | |
7519 | TREE_CONSTANT (r) = true; | |
7520 | } | |
99d114c1 MP |
7521 | else if (TYPE_REF_P (TREE_TYPE (t))) |
7522 | /* P2280 allows references to unknown... */; | |
7523 | else if (is_this_parameter (t)) | |
7524 | /* ...as well as the this pointer. */; | |
2d76680f PC |
7525 | else |
7526 | { | |
2b3ab879 | 7527 | if (!ctx->quiet) |
2d76680f PC |
7528 | error ("%qE is not a constant expression", t); |
7529 | *non_constant_p = true; | |
7530 | } | |
7531 | break; | |
7532 | ||
7533 | case CALL_EXPR: | |
7534 | case AGGR_INIT_EXPR: | |
92a596e8 | 7535 | r = cxx_eval_call_expression (ctx, t, lval, |
2d76680f PC |
7536 | non_constant_p, overflow_p); |
7537 | break; | |
7538 | ||
60813a46 JM |
7539 | case DECL_EXPR: |
7540 | { | |
7541 | r = DECL_EXPR_DECL (t); | |
43574e4f JJ |
7542 | if (TREE_CODE (r) == USING_DECL) |
7543 | { | |
7544 | r = void_node; | |
7545 | break; | |
7546 | } | |
8892d532 JJ |
7547 | |
7548 | if (VAR_P (r) | |
72124f48 JJ |
7549 | && (TREE_STATIC (r) |
7550 | || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r))) | |
8892d532 | 7551 | /* Allow __FUNCTION__ etc. */ |
32d16fe9 JJ |
7552 | && !DECL_ARTIFICIAL (r) |
7553 | && !decl_constant_var_p (r)) | |
8892d532 | 7554 | { |
8892d532 JJ |
7555 | if (!ctx->quiet) |
7556 | { | |
7557 | if (CP_DECL_THREAD_LOCAL_P (r)) | |
72124f48 | 7558 | error_at (loc, "control passes through definition of %qD " |
8892d532 JJ |
7559 | "with thread storage duration", r); |
7560 | else | |
72124f48 | 7561 | error_at (loc, "control passes through definition of %qD " |
8892d532 JJ |
7562 | "with static storage duration", r); |
7563 | } | |
7564 | *non_constant_p = true; | |
7565 | break; | |
7566 | } | |
7567 | ||
9d5730de JJ |
7568 | /* make_rtl_for_nonlocal_decl could have deferred emission of |
7569 | a local static var, but if it appears in a statement expression | |
7570 | which is constant expression evaluated to e.g. just the address | |
7571 | of the variable, its DECL_EXPR will never be seen during | |
7572 | gimple lowering's record_vars_into as the statement expression | |
7573 | will not be in the IL at all. */ | |
7574 | if (VAR_P (r) | |
7575 | && TREE_STATIC (r) | |
7576 | && !DECL_REALLY_EXTERN (r) | |
7577 | && DECL_FUNCTION_SCOPE_P (r) | |
7578 | && !var_in_maybe_constexpr_fn (r) | |
7579 | && decl_constant_var_p (r)) | |
7580 | { | |
7581 | varpool_node *node = varpool_node::get (r); | |
7582 | if (node == NULL || !node->definition) | |
7583 | rest_of_decl_compilation (r, 0, at_eof); | |
7584 | } | |
7585 | ||
60813a46 JM |
7586 | if (AGGREGATE_TYPE_P (TREE_TYPE (r)) |
7587 | || VECTOR_TYPE_P (TREE_TYPE (r))) | |
7588 | { | |
7589 | new_ctx = *ctx; | |
7590 | new_ctx.object = r; | |
7591 | new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL); | |
e8c48716 | 7592 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; |
e6a29aab | 7593 | ctx->global->put_value (r, new_ctx.ctor); |
60813a46 JM |
7594 | ctx = &new_ctx; |
7595 | } | |
7596 | ||
7597 | if (tree init = DECL_INITIAL (r)) | |
7598 | { | |
72f76540 | 7599 | init = cxx_eval_constant_expression (ctx, init, vc_prvalue, |
5a804683 | 7600 | non_constant_p, overflow_p); |
7f0e23e9 | 7601 | /* Don't share a CONSTRUCTOR that might be changed. */ |
0146e25f | 7602 | init = unshare_constructor (init); |
04e1749c MP |
7603 | /* Remember that a constant object's constructor has already |
7604 | run. */ | |
7605 | if (CLASS_TYPE_P (TREE_TYPE (r)) | |
7606 | && CP_TYPE_CONST_P (TREE_TYPE (r))) | |
7607 | TREE_READONLY (init) = true; | |
e6a29aab | 7608 | ctx->global->put_value (r, init); |
60813a46 JM |
7609 | } |
7610 | else if (ctx == &new_ctx) | |
7611 | /* We gave it a CONSTRUCTOR above. */; | |
7612 | else | |
e6a29aab | 7613 | ctx->global->put_value (r, NULL_TREE); |
60813a46 JM |
7614 | } |
7615 | break; | |
7616 | ||
2d76680f | 7617 | case TARGET_EXPR: |
595f1b12 JM |
7618 | { |
7619 | tree type = TREE_TYPE (t); | |
7620 | ||
7621 | if (!literal_type_p (type)) | |
7622 | { | |
7623 | if (!ctx->quiet) | |
7624 | { | |
7625 | auto_diagnostic_group d; | |
7626 | error ("temporary of non-literal type %qT in a " | |
7627 | "constant expression", type); | |
7628 | explain_non_literal_class (type); | |
7629 | } | |
7630 | *non_constant_p = true; | |
7631 | break; | |
7632 | } | |
7633 | gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t)); | |
7634 | /* Avoid evaluating a TARGET_EXPR more than once. */ | |
7635 | tree slot = TARGET_EXPR_SLOT (t); | |
e6a29aab | 7636 | if (tree v = ctx->global->get_value (slot)) |
595f1b12 JM |
7637 | { |
7638 | if (lval) | |
7639 | return slot; | |
e6a29aab | 7640 | r = v; |
595f1b12 JM |
7641 | break; |
7642 | } | |
595f1b12 JM |
7643 | if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))) |
7644 | { | |
7645 | /* We're being expanded without an explicit target, so start | |
7646 | initializing a new object; expansion with an explicit target | |
7647 | strips the TARGET_EXPR before we get here. */ | |
7648 | new_ctx = *ctx; | |
49a86fce PP |
7649 | /* Link CTX to NEW_CTX so that lookup_placeholder can resolve |
7650 | any PLACEHOLDER_EXPR within the initializer that refers to the | |
7651 | former object under construction. */ | |
7652 | new_ctx.parent = ctx; | |
595f1b12 JM |
7653 | new_ctx.ctor = build_constructor (type, NULL); |
7654 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; | |
7655 | new_ctx.object = slot; | |
e6a29aab | 7656 | ctx->global->put_value (new_ctx.object, new_ctx.ctor); |
595f1b12 JM |
7657 | ctx = &new_ctx; |
7658 | } | |
72f76540 | 7659 | /* Pass vc_prvalue because this indicates |
595f1b12 | 7660 | initialization of a temporary. */ |
72f76540 | 7661 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_prvalue, |
595f1b12 JM |
7662 | non_constant_p, overflow_p); |
7663 | if (*non_constant_p) | |
ee1de08d | 7664 | break; |
13669741 JM |
7665 | /* If the initializer is complex, evaluate it to initialize slot. */ |
7666 | bool is_complex = target_expr_needs_replace (t); | |
7667 | if (!is_complex) | |
7668 | { | |
7669 | r = unshare_constructor (r); | |
7670 | /* Adjust the type of the result to the type of the temporary. */ | |
7671 | r = adjust_temp_type (type, r); | |
7672 | ctx->global->put_value (slot, r); | |
7673 | } | |
595f1b12 JM |
7674 | if (TARGET_EXPR_CLEANUP (t) && !CLEANUP_EH_ONLY (t)) |
7675 | ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t)); | |
595f1b12 JM |
7676 | if (ctx->save_exprs) |
7677 | ctx->save_exprs->safe_push (slot); | |
7678 | if (lval) | |
7679 | return slot; | |
13669741 JM |
7680 | if (is_complex) |
7681 | r = ctx->global->get_value (slot); | |
595f1b12 | 7682 | } |
2d76680f PC |
7683 | break; |
7684 | ||
60813a46 | 7685 | case INIT_EXPR: |
60813a46 | 7686 | case MODIFY_EXPR: |
4b390698 | 7687 | gcc_assert (jump_target == NULL || *jump_target == NULL_TREE); |
92a596e8 | 7688 | r = cxx_eval_store_expression (ctx, t, lval, |
60813a46 JM |
7689 | non_constant_p, overflow_p); |
7690 | break; | |
7691 | ||
2d76680f | 7692 | case SCOPE_REF: |
3e605b20 | 7693 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
92a596e8 | 7694 | lval, |
5a804683 | 7695 | non_constant_p, overflow_p); |
2d76680f PC |
7696 | break; |
7697 | ||
7698 | case RETURN_EXPR: | |
75e0295b MP |
7699 | if (TREE_OPERAND (t, 0) != NULL_TREE) |
7700 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), | |
7701 | lval, | |
7702 | non_constant_p, overflow_p); | |
43574e4f JJ |
7703 | /* FALLTHRU */ |
7704 | case BREAK_STMT: | |
7705 | case CONTINUE_STMT: | |
019e0ae8 MP |
7706 | if (jump_target) |
7707 | *jump_target = t; | |
7708 | else | |
7709 | { | |
7710 | /* Can happen with ({ return true; }) && false; passed to | |
7711 | maybe_constant_value. There is nothing to jump over in this | |
7712 | case, and the bug will be diagnosed later. */ | |
7713 | gcc_assert (ctx->quiet); | |
7714 | *non_constant_p = true; | |
7715 | } | |
56632b27 JM |
7716 | break; |
7717 | ||
cabaf50a JM |
7718 | case SAVE_EXPR: |
7719 | /* Avoid evaluating a SAVE_EXPR more than once. */ | |
e6a29aab JM |
7720 | if (tree v = ctx->global->get_value (t)) |
7721 | r = v; | |
cabaf50a JM |
7722 | else |
7723 | { | |
72f76540 | 7724 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue, |
cabaf50a | 7725 | non_constant_p, overflow_p); |
0e0ffbfc JJ |
7726 | if (*non_constant_p) |
7727 | break; | |
e6a29aab | 7728 | ctx->global->put_value (t, r); |
39dce2b7 | 7729 | if (ctx->save_exprs) |
0ee28590 | 7730 | ctx->save_exprs->safe_push (t); |
cabaf50a JM |
7731 | } |
7732 | break; | |
7733 | ||
2d76680f | 7734 | case TRY_CATCH_EXPR: |
ee1de08d JJ |
7735 | if (TREE_OPERAND (t, 0) == NULL_TREE) |
7736 | { | |
7737 | r = void_node; | |
7738 | break; | |
7739 | } | |
7740 | /* FALLTHRU */ | |
7741 | case NON_LVALUE_EXPR: | |
769430b2 | 7742 | case TRY_BLOCK: |
2d76680f | 7743 | case MUST_NOT_THROW_EXPR: |
60813a46 JM |
7744 | case EXPR_STMT: |
7745 | case EH_SPEC_BLOCK: | |
3e605b20 | 7746 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
92a596e8 | 7747 | lval, |
56632b27 JM |
7748 | non_constant_p, overflow_p, |
7749 | jump_target); | |
2d76680f PC |
7750 | break; |
7751 | ||
ee1de08d JJ |
7752 | case CLEANUP_POINT_EXPR: |
7753 | { | |
7754 | auto_vec<tree, 2> cleanups; | |
7755 | vec<tree> *prev_cleanups = ctx->global->cleanups; | |
7756 | ctx->global->cleanups = &cleanups; | |
9fdbd7d6 NS |
7757 | |
7758 | auto_vec<tree, 10> save_exprs; | |
7759 | constexpr_ctx new_ctx = *ctx; | |
7760 | new_ctx.save_exprs = &save_exprs; | |
7761 | ||
7762 | r = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 0), | |
ee1de08d JJ |
7763 | lval, |
7764 | non_constant_p, overflow_p, | |
7765 | jump_target); | |
9fdbd7d6 | 7766 | |
ee1de08d JJ |
7767 | ctx->global->cleanups = prev_cleanups; |
7768 | unsigned int i; | |
7769 | tree cleanup; | |
7770 | /* Evaluate the cleanups. */ | |
7771 | FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup) | |
9fdbd7d6 | 7772 | cxx_eval_constant_expression (&new_ctx, cleanup, vc_discard, |
fc531c2e | 7773 | non_constant_p, overflow_p); |
9fdbd7d6 NS |
7774 | |
7775 | /* Forget SAVE_EXPRs and TARGET_EXPRs created by this | |
7776 | full-expression. */ | |
7777 | for (tree save_expr : save_exprs) | |
90bc2d09 | 7778 | destroy_value_checked (ctx, save_expr, non_constant_p); |
ee1de08d JJ |
7779 | } |
7780 | break; | |
7781 | ||
769430b2 JM |
7782 | case TRY_FINALLY_EXPR: |
7783 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, | |
7784 | non_constant_p, overflow_p, | |
7785 | jump_target); | |
7786 | if (!*non_constant_p) | |
7787 | /* Also evaluate the cleanup. */ | |
72f76540 | 7788 | cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard, |
fc531c2e | 7789 | non_constant_p, overflow_p); |
769430b2 JM |
7790 | break; |
7791 | ||
43574e4f | 7792 | case CLEANUP_STMT: |
fc531c2e JJ |
7793 | r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval, |
7794 | non_constant_p, overflow_p, | |
7795 | jump_target); | |
7796 | if (!CLEANUP_EH_ONLY (t) && !*non_constant_p) | |
7797 | { | |
7798 | iloc_sentinel ils (loc); | |
7799 | /* Also evaluate the cleanup. */ | |
72f76540 | 7800 | cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), vc_discard, |
fc531c2e JJ |
7801 | non_constant_p, overflow_p); |
7802 | } | |
43574e4f JJ |
7803 | break; |
7804 | ||
2d76680f PC |
7805 | /* These differ from cxx_eval_unary_expression in that this doesn't |
7806 | check for a constant operand or result; an address can be | |
7807 | constant without its operand being, and vice versa. */ | |
cda0a029 | 7808 | case MEM_REF: |
2d76680f | 7809 | case INDIRECT_REF: |
92a596e8 | 7810 | r = cxx_eval_indirect_ref (ctx, t, lval, |
2d76680f PC |
7811 | non_constant_p, overflow_p); |
7812 | break; | |
7813 | ||
7814 | case ADDR_EXPR: | |
7815 | { | |
7816 | tree oldop = TREE_OPERAND (t, 0); | |
72f76540 | 7817 | tree op = cxx_eval_constant_expression (ctx, oldop, vc_glvalue, |
5a804683 | 7818 | non_constant_p, overflow_p); |
2d76680f PC |
7819 | /* Don't VERIFY_CONSTANT here. */ |
7820 | if (*non_constant_p) | |
7821 | return t; | |
f2acb8ad | 7822 | gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR); |
2d76680f PC |
7823 | /* This function does more aggressive folding than fold itself. */ |
7824 | r = build_fold_addr_expr_with_type (op, TREE_TYPE (t)); | |
7825 | if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop) | |
7a649ef5 JM |
7826 | { |
7827 | ggc_free (r); | |
7828 | return t; | |
7829 | } | |
2d76680f PC |
7830 | break; |
7831 | } | |
7832 | ||
7833 | case REALPART_EXPR: | |
7834 | case IMAGPART_EXPR: | |
87713c6a JJ |
7835 | if (lval) |
7836 | { | |
7837 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, | |
7838 | non_constant_p, overflow_p); | |
7839 | if (r == error_mark_node) | |
7840 | ; | |
72f76540 | 7841 | else if (r == TREE_OPERAND (t, 0) || lval == vc_discard) |
87713c6a JJ |
7842 | r = t; |
7843 | else | |
7844 | r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r); | |
7845 | break; | |
7846 | } | |
7847 | /* FALLTHRU */ | |
2d76680f PC |
7848 | case CONJ_EXPR: |
7849 | case FIX_TRUNC_EXPR: | |
7850 | case FLOAT_EXPR: | |
7851 | case NEGATE_EXPR: | |
7852 | case ABS_EXPR: | |
e28fadbc | 7853 | case ABSU_EXPR: |
2d76680f PC |
7854 | case BIT_NOT_EXPR: |
7855 | case TRUTH_NOT_EXPR: | |
7856 | case FIXED_CONVERT_EXPR: | |
92a596e8 | 7857 | r = cxx_eval_unary_expression (ctx, t, lval, |
2d76680f PC |
7858 | non_constant_p, overflow_p); |
7859 | break; | |
7860 | ||
7861 | case SIZEOF_EXPR: | |
99e764a2 MP |
7862 | r = fold_sizeof_expr (t); |
7863 | /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR, | |
7864 | which could lead to an infinite recursion. */ | |
7865 | if (TREE_CODE (r) != SIZEOF_EXPR) | |
7866 | r = cxx_eval_constant_expression (ctx, r, lval, | |
7867 | non_constant_p, overflow_p, | |
7868 | jump_target); | |
7869 | else | |
7870 | { | |
7871 | *non_constant_p = true; | |
7872 | gcc_assert (ctx->quiet); | |
7873 | } | |
7874 | ||
2d76680f PC |
7875 | break; |
7876 | ||
7877 | case COMPOUND_EXPR: | |
7878 | { | |
7879 | /* check_return_expr sometimes wraps a TARGET_EXPR in a | |
7880 | COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR | |
7881 | introduced by build_call_a. */ | |
7882 | tree op0 = TREE_OPERAND (t, 0); | |
7883 | tree op1 = TREE_OPERAND (t, 1); | |
7884 | STRIP_NOPS (op1); | |
7885 | if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) | |
7886 | || TREE_CODE (op1) == EMPTY_CLASS_EXPR) | |
2b3ab879 | 7887 | r = cxx_eval_constant_expression (ctx, op0, |
92a596e8 | 7888 | lval, non_constant_p, overflow_p, |
56632b27 | 7889 | jump_target); |
2d76680f PC |
7890 | else |
7891 | { | |
7892 | /* Check that the LHS is constant and then discard it. */ | |
72f76540 JM |
7893 | cxx_eval_constant_expression (ctx, op0, vc_discard, |
7894 | non_constant_p, overflow_p, | |
56632b27 | 7895 | jump_target); |
06ec22b7 JM |
7896 | if (*non_constant_p) |
7897 | return t; | |
2d76680f | 7898 | op1 = TREE_OPERAND (t, 1); |
2b3ab879 | 7899 | r = cxx_eval_constant_expression (ctx, op1, |
92a596e8 | 7900 | lval, non_constant_p, overflow_p, |
56632b27 | 7901 | jump_target); |
2d76680f PC |
7902 | } |
7903 | } | |
7904 | break; | |
7905 | ||
7906 | case POINTER_PLUS_EXPR: | |
1af4ebf5 | 7907 | case POINTER_DIFF_EXPR: |
2d76680f PC |
7908 | case PLUS_EXPR: |
7909 | case MINUS_EXPR: | |
7910 | case MULT_EXPR: | |
7911 | case TRUNC_DIV_EXPR: | |
7912 | case CEIL_DIV_EXPR: | |
7913 | case FLOOR_DIV_EXPR: | |
7914 | case ROUND_DIV_EXPR: | |
7915 | case TRUNC_MOD_EXPR: | |
7916 | case CEIL_MOD_EXPR: | |
7917 | case ROUND_MOD_EXPR: | |
7918 | case RDIV_EXPR: | |
7919 | case EXACT_DIV_EXPR: | |
7920 | case MIN_EXPR: | |
7921 | case MAX_EXPR: | |
7922 | case LSHIFT_EXPR: | |
7923 | case RSHIFT_EXPR: | |
81fa6d73 JJ |
7924 | case LROTATE_EXPR: |
7925 | case RROTATE_EXPR: | |
2d76680f PC |
7926 | case BIT_IOR_EXPR: |
7927 | case BIT_XOR_EXPR: | |
7928 | case BIT_AND_EXPR: | |
7929 | case TRUTH_XOR_EXPR: | |
7930 | case LT_EXPR: | |
7931 | case LE_EXPR: | |
7932 | case GT_EXPR: | |
7933 | case GE_EXPR: | |
7934 | case EQ_EXPR: | |
7935 | case NE_EXPR: | |
b7689b96 | 7936 | case SPACESHIP_EXPR: |
2d76680f PC |
7937 | case UNORDERED_EXPR: |
7938 | case ORDERED_EXPR: | |
7939 | case UNLT_EXPR: | |
7940 | case UNLE_EXPR: | |
7941 | case UNGT_EXPR: | |
7942 | case UNGE_EXPR: | |
7943 | case UNEQ_EXPR: | |
7944 | case LTGT_EXPR: | |
7945 | case RANGE_EXPR: | |
7946 | case COMPLEX_EXPR: | |
92a596e8 | 7947 | r = cxx_eval_binary_expression (ctx, t, lval, |
2d76680f PC |
7948 | non_constant_p, overflow_p); |
7949 | break; | |
7950 | ||
7951 | /* fold can introduce non-IF versions of these; still treat them as | |
7952 | short-circuiting. */ | |
7953 | case TRUTH_AND_EXPR: | |
7954 | case TRUTH_ANDIF_EXPR: | |
3e605b20 | 7955 | r = cxx_eval_logical_expression (ctx, t, boolean_false_node, |
2d76680f | 7956 | boolean_true_node, |
2d76680f PC |
7957 | non_constant_p, overflow_p); |
7958 | break; | |
7959 | ||
7960 | case TRUTH_OR_EXPR: | |
7961 | case TRUTH_ORIF_EXPR: | |
3e605b20 | 7962 | r = cxx_eval_logical_expression (ctx, t, boolean_true_node, |
2d76680f | 7963 | boolean_false_node, |
2d76680f PC |
7964 | non_constant_p, overflow_p); |
7965 | break; | |
7966 | ||
7967 | case ARRAY_REF: | |
92a596e8 | 7968 | r = cxx_eval_array_reference (ctx, t, lval, |
2d76680f PC |
7969 | non_constant_p, overflow_p); |
7970 | break; | |
7971 | ||
7972 | case COMPONENT_REF: | |
7973 | if (is_overloaded_fn (t)) | |
7974 | { | |
7975 | /* We can only get here in checking mode via | |
7976 | build_non_dependent_expr, because any expression that | |
7977 | calls or takes the address of the function will have | |
7978 | pulled a FUNCTION_DECL out of the COMPONENT_REF. */ | |
2b3ab879 | 7979 | gcc_checking_assert (ctx->quiet || errorcount); |
2d76680f PC |
7980 | *non_constant_p = true; |
7981 | return t; | |
7982 | } | |
92a596e8 | 7983 | r = cxx_eval_component_reference (ctx, t, lval, |
2d76680f PC |
7984 | non_constant_p, overflow_p); |
7985 | break; | |
7986 | ||
7987 | case BIT_FIELD_REF: | |
92a596e8 | 7988 | r = cxx_eval_bit_field_ref (ctx, t, lval, |
2d76680f PC |
7989 | non_constant_p, overflow_p); |
7990 | break; | |
7991 | ||
7992 | case COND_EXPR: | |
43574e4f | 7993 | case IF_STMT: |
4b390698 JJ |
7994 | if (jump_target && *jump_target) |
7995 | { | |
e9fa2f6d | 7996 | tree orig_jump = *jump_target; |
43574e4f JJ |
7997 | tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1)) |
7998 | ? TREE_OPERAND (t, 1) : void_node); | |
4b390698 JJ |
7999 | /* When jumping to a label, the label might be either in the |
8000 | then or else blocks, so process then block first in skipping | |
8001 | mode first, and if we are still in the skipping mode at its end, | |
8002 | process the else block too. */ | |
43574e4f JJ |
8003 | r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, |
8004 | overflow_p, jump_target); | |
e9fa2f6d MP |
8005 | /* It's possible that we found the label in the then block. But |
8006 | it could have been followed by another jumping statement, e.g. | |
8007 | say we're looking for case 1: | |
8008 | if (cond) | |
8009 | { | |
8010 | // skipped statements | |
8011 | case 1:; // clears up *jump_target | |
8012 | return 1; // and sets it to a RETURN_EXPR | |
8013 | } | |
8014 | else { ... } | |
8015 | in which case we need not go looking to the else block. | |
8016 | (goto is not allowed in a constexpr function.) */ | |
8017 | if (*jump_target == orig_jump) | |
43574e4f JJ |
8018 | { |
8019 | arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2)) | |
8020 | ? TREE_OPERAND (t, 2) : void_node); | |
8021 | r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, | |
8022 | overflow_p, jump_target); | |
8023 | } | |
4b390698 JJ |
8024 | break; |
8025 | } | |
92a596e8 | 8026 | r = cxx_eval_conditional_expression (ctx, t, lval, |
56632b27 JM |
8027 | non_constant_p, overflow_p, |
8028 | jump_target); | |
2d76680f | 8029 | break; |
f370e36d JJ |
8030 | case VEC_COND_EXPR: |
8031 | r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p, | |
8032 | overflow_p); | |
8033 | break; | |
2d76680f PC |
8034 | |
8035 | case CONSTRUCTOR: | |
35d87f01 | 8036 | if (TREE_CONSTANT (t) && reduced_constant_expression_p (t)) |
2d63bc39 | 8037 | { |
2cc41601 | 8038 | /* Don't re-process a constant CONSTRUCTOR. */ |
ac9ec198 | 8039 | verify_constructor_flags (t); |
2d63bc39 | 8040 | if (TREE_CONSTANT (t)) |
2cc41601 | 8041 | return t; |
2d63bc39 | 8042 | } |
92a596e8 | 8043 | r = cxx_eval_bare_aggregate (ctx, t, lval, |
2d76680f PC |
8044 | non_constant_p, overflow_p); |
8045 | break; | |
8046 | ||
8047 | case VEC_INIT_EXPR: | |
8048 | /* We can get this in a defaulted constructor for a class with a | |
8049 | non-static data member of array type. Either the initializer will | |
8050 | be NULL, meaning default-initialization, or it will be an lvalue | |
8051 | or xvalue of the same type, meaning direct-initialization from the | |
8052 | corresponding member. */ | |
92a596e8 | 8053 | r = cxx_eval_vec_init (ctx, t, lval, |
2d76680f PC |
8054 | non_constant_p, overflow_p); |
8055 | break; | |
8056 | ||
2d76680f | 8057 | case VEC_PERM_EXPR: |
92a596e8 | 8058 | r = cxx_eval_trinary_expression (ctx, t, lval, |
2d76680f PC |
8059 | non_constant_p, overflow_p); |
8060 | break; | |
efb7c510 MK |
8061 | |
8062 | case PAREN_EXPR: | |
8063 | gcc_assert (!REF_PARENTHESIZED_P (t)); | |
8064 | /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in | |
8065 | constant expressions since it's unaffected by -fassociative-math. */ | |
8066 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, | |
8067 | non_constant_p, overflow_p); | |
8068 | break; | |
2d76680f | 8069 | |
7d75ea04 JJ |
8070 | case NOP_EXPR: |
8071 | if (REINTERPRET_CAST_P (t)) | |
8072 | { | |
8073 | if (!ctx->quiet) | |
6821245b | 8074 | error_at (loc, |
a9c697b8 | 8075 | "%<reinterpret_cast%> is not a constant expression"); |
7d75ea04 JJ |
8076 | *non_constant_p = true; |
8077 | return t; | |
8078 | } | |
8079 | /* FALLTHROUGH. */ | |
2d76680f PC |
8080 | case CONVERT_EXPR: |
8081 | case VIEW_CONVERT_EXPR: | |
cda0a029 | 8082 | case UNARY_PLUS_EXPR: |
2d76680f PC |
8083 | { |
8084 | tree oldop = TREE_OPERAND (t, 0); | |
cda0a029 | 8085 | |
3e605b20 | 8086 | tree op = cxx_eval_constant_expression (ctx, oldop, |
92a596e8 | 8087 | lval, |
5a804683 | 8088 | non_constant_p, overflow_p); |
2d76680f PC |
8089 | if (*non_constant_p) |
8090 | return t; | |
dcdbc004 | 8091 | tree type = TREE_TYPE (t); |
02a8575c JM |
8092 | |
8093 | if (VOID_TYPE_P (type)) | |
8094 | return void_node; | |
8095 | ||
eeb54a14 JJ |
8096 | if (TREE_CODE (t) == CONVERT_EXPR |
8097 | && ARITHMETIC_TYPE_P (type) | |
82bb6673 | 8098 | && INDIRECT_TYPE_P (TREE_TYPE (op)) |
3a0bc47c | 8099 | && ctx->manifestly_const_eval == mce_true) |
eeb54a14 JJ |
8100 | { |
8101 | if (!ctx->quiet) | |
8102 | error_at (loc, | |
8103 | "conversion from pointer type %qT to arithmetic type " | |
8104 | "%qT in a constant expression", TREE_TYPE (op), type); | |
8105 | *non_constant_p = true; | |
8106 | return t; | |
8107 | } | |
8108 | ||
2ffc2645 MP |
8109 | /* [expr.const]: a conversion from type cv void* to a pointer-to-object |
8110 | type cannot be part of a core constant expression as a resolution to | |
8111 | DR 1312. */ | |
6252e35c | 8112 | if (TYPE_PTROB_P (type) |
2ffc2645 MP |
8113 | && TYPE_PTR_P (TREE_TYPE (op)) |
8114 | && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op))) | |
b69ee500 NS |
8115 | /* Inside a call to std::construct_at, |
8116 | std::allocator<T>::{,de}allocate, or | |
8117 | std::source_location::current, we permit casting from void* | |
2ffc2645 MP |
8118 | because that is compiler-generated code. */ |
8119 | && !is_std_construct_at (ctx->call) | |
b69ee500 NS |
8120 | && !is_std_allocator_allocate (ctx->call) |
8121 | && !is_std_source_location_current (ctx->call)) | |
2ffc2645 MP |
8122 | { |
8123 | /* Likewise, don't error when casting from void* when OP is | |
8124 | &heap uninit and similar. */ | |
8125 | tree sop = tree_strip_nop_conversions (op); | |
b69ee500 NS |
8126 | tree decl = NULL_TREE; |
8127 | if (TREE_CODE (sop) == ADDR_EXPR) | |
8128 | decl = TREE_OPERAND (sop, 0); | |
8129 | if (decl | |
8130 | && VAR_P (decl) | |
8131 | && DECL_ARTIFICIAL (decl) | |
8132 | && (DECL_NAME (decl) == heap_identifier | |
8133 | || DECL_NAME (decl) == heap_uninit_identifier | |
8134 | || DECL_NAME (decl) == heap_vec_identifier | |
8135 | || DECL_NAME (decl) == heap_vec_uninit_identifier)) | |
2ffc2645 | 8136 | /* OK */; |
8d344146 JM |
8137 | /* P2738 (C++26): a conversion from a prvalue P of type "pointer to |
8138 | cv void" to a pointer-to-object type T unless P points to an | |
8139 | object whose type is similar to T. */ | |
b69ee500 | 8140 | else if (cxx_dialect > cxx23) |
8d344146 | 8141 | { |
b69ee500 NS |
8142 | r = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), sop); |
8143 | if (r) | |
8144 | { | |
8145 | r = build1 (ADDR_EXPR, type, r); | |
8146 | break; | |
8147 | } | |
8148 | if (!ctx->quiet) | |
8149 | { | |
8150 | if (TREE_CODE (sop) == ADDR_EXPR) | |
8151 | { | |
8152 | auto_diagnostic_group d; | |
8153 | error_at (loc, "cast from %qT is not allowed in a " | |
8154 | "constant expression because " | |
8155 | "pointed-to type %qT is not similar to %qT", | |
8156 | TREE_TYPE (op), TREE_TYPE (TREE_TYPE (sop)), | |
8157 | TREE_TYPE (type)); | |
8158 | tree obj = build_fold_indirect_ref (sop); | |
8159 | inform (DECL_SOURCE_LOCATION (obj), | |
8160 | "pointed-to object declared here"); | |
8161 | } | |
8162 | else | |
8163 | { | |
8164 | gcc_assert (integer_zerop (sop)); | |
8165 | error_at (loc, "cast from %qT is not allowed in a " | |
8166 | "constant expression because " | |
8167 | "%qE does not point to an object", | |
8168 | TREE_TYPE (op), oldop); | |
8169 | } | |
8170 | } | |
8171 | *non_constant_p = true; | |
8172 | return t; | |
8d344146 | 8173 | } |
2ffc2645 MP |
8174 | else |
8175 | { | |
8176 | if (!ctx->quiet) | |
b69ee500 NS |
8177 | error_at (loc, "cast from %qT is not allowed in a " |
8178 | "constant expression before C++26", | |
2ffc2645 MP |
8179 | TREE_TYPE (op)); |
8180 | *non_constant_p = true; | |
8181 | return t; | |
8182 | } | |
8183 | } | |
8184 | ||
8e007055 | 8185 | if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type)) |
de81e062 MP |
8186 | { |
8187 | op = cplus_expand_constant (op); | |
8188 | if (TREE_CODE (op) == PTRMEM_CST) | |
8189 | { | |
8190 | if (!ctx->quiet) | |
8191 | error_at (loc, "%qE is not a constant expression when the " | |
8192 | "class %qT is still incomplete", op, | |
8193 | PTRMEM_CST_CLASS (op)); | |
8194 | *non_constant_p = true; | |
8195 | return t; | |
8196 | } | |
8197 | } | |
7d75ea04 | 8198 | |
05bf54c3 MP |
8199 | if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR) |
8200 | { | |
7d75ea04 JJ |
8201 | if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op)) |
8202 | && !can_convert_qual (type, op)) | |
8203 | op = cplus_expand_constant (op); | |
8204 | return cp_fold_convert (type, op); | |
05bf54c3 | 8205 | } |
8bada5cd | 8206 | |
71a93b08 | 8207 | if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST) |
2d76680f | 8208 | { |
8bada5cd MS |
8209 | if (integer_zerop (op)) |
8210 | { | |
9f613f06 | 8211 | if (TYPE_REF_P (type)) |
8bada5cd MS |
8212 | { |
8213 | if (!ctx->quiet) | |
2ffc2645 | 8214 | error_at (loc, "dereferencing a null pointer"); |
8bada5cd MS |
8215 | *non_constant_p = true; |
8216 | return t; | |
8217 | } | |
8bada5cd MS |
8218 | } |
8219 | else | |
8220 | { | |
8221 | /* This detects for example: | |
8222 | reinterpret_cast<void*>(sizeof 0) | |
8223 | */ | |
8224 | if (!ctx->quiet) | |
6821245b | 8225 | error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not " |
64d6d399 | 8226 | "a constant expression", |
8bada5cd MS |
8227 | type, op); |
8228 | *non_constant_p = true; | |
8229 | return t; | |
8230 | } | |
2d76680f | 8231 | } |
7d75ea04 | 8232 | |
8e007055 JJ |
8233 | if (INDIRECT_TYPE_P (type) |
8234 | && TREE_CODE (op) == NOP_EXPR | |
8235 | && TREE_TYPE (op) == ptr_type_node | |
8236 | && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR | |
8237 | && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0)) | |
815baade JJ |
8238 | && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), |
8239 | 0)) == heap_uninit_identifier | |
8240 | || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), | |
8241 | 0)) == heap_vec_uninit_identifier)) | |
8e007055 JJ |
8242 | { |
8243 | tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0); | |
8244 | tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var)); | |
8245 | tree elt_type = TREE_TYPE (type); | |
8246 | tree cookie_size = NULL_TREE; | |
0a0c2c3f | 8247 | tree arg_size = NULL_TREE; |
8e007055 JJ |
8248 | if (TREE_CODE (elt_type) == RECORD_TYPE |
8249 | && TYPE_NAME (elt_type) == heap_identifier) | |
8250 | { | |
8251 | tree fld1 = TYPE_FIELDS (elt_type); | |
8252 | tree fld2 = DECL_CHAIN (fld1); | |
8253 | elt_type = TREE_TYPE (TREE_TYPE (fld2)); | |
8254 | cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1)); | |
8255 | } | |
815baade JJ |
8256 | DECL_NAME (var) |
8257 | = (DECL_NAME (var) == heap_uninit_identifier | |
8258 | ? heap_identifier : heap_vec_identifier); | |
0a0c2c3f JJ |
8259 | /* For zero sized elt_type, try to recover how many outer_nelts |
8260 | it should have. */ | |
8261 | if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size) | |
8262 | : integer_zerop (var_size)) | |
8263 | && !int_size_in_bytes (elt_type) | |
8264 | && TREE_CODE (oldop) == CALL_EXPR | |
8265 | && call_expr_nargs (oldop) >= 1) | |
8266 | if (tree fun = get_function_named_in_call (oldop)) | |
8267 | if (cxx_replaceable_global_alloc_fn (fun) | |
8268 | && IDENTIFIER_NEW_OP_P (DECL_NAME (fun))) | |
8269 | arg_size = CALL_EXPR_ARG (oldop, 0); | |
8e007055 | 8270 | TREE_TYPE (var) |
0a0c2c3f JJ |
8271 | = build_new_constexpr_heap_type (ctx, elt_type, cookie_size, |
8272 | var_size, arg_size, | |
8273 | non_constant_p, overflow_p); | |
8e007055 JJ |
8274 | TREE_TYPE (TREE_OPERAND (op, 0)) |
8275 | = build_pointer_type (TREE_TYPE (var)); | |
8276 | } | |
8277 | ||
cda0a029 | 8278 | if (op == oldop && tcode != UNARY_PLUS_EXPR) |
2d76680f PC |
8279 | /* We didn't fold at the top so we could check for ptr-int |
8280 | conversion. */ | |
8281 | return fold (t); | |
7d75ea04 | 8282 | |
02a8575c JM |
8283 | tree sop; |
8284 | ||
e4511ca2 JM |
8285 | /* Handle an array's bounds having been deduced after we built |
8286 | the wrapping expression. */ | |
8287 | if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op))) | |
8288 | r = op; | |
02a8575c JM |
8289 | else if (sop = tree_strip_nop_conversions (op), |
8290 | sop != op && (same_type_ignoring_tlq_and_bounds_p | |
8291 | (type, TREE_TYPE (sop)))) | |
8292 | r = sop; | |
e4511ca2 | 8293 | else if (tcode == UNARY_PLUS_EXPR) |
cda0a029 JM |
8294 | r = fold_convert (TREE_TYPE (t), op); |
8295 | else | |
8296 | r = fold_build1 (tcode, type, op); | |
7d75ea04 | 8297 | |
2d76680f PC |
8298 | /* Conversion of an out-of-range value has implementation-defined |
8299 | behavior; the language considers it different from arithmetic | |
8300 | overflow, which is undefined. */ | |
8301 | if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op)) | |
8302 | TREE_OVERFLOW (r) = false; | |
8303 | } | |
8304 | break; | |
8305 | ||
98e34113 JJ |
8306 | case EXCESS_PRECISION_EXPR: |
8307 | { | |
8308 | tree oldop = TREE_OPERAND (t, 0); | |
8309 | ||
8310 | tree op = cxx_eval_constant_expression (ctx, oldop, | |
8311 | lval, | |
8312 | non_constant_p, overflow_p); | |
8313 | if (*non_constant_p) | |
8314 | return t; | |
8315 | r = fold_convert (TREE_TYPE (t), op); | |
8316 | break; | |
8317 | } | |
8318 | ||
2d76680f | 8319 | case EMPTY_CLASS_EXPR: |
46fdced6 PP |
8320 | /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering |
8321 | it to an appropriate CONSTRUCTOR. */ | |
8322 | return build_constructor (TREE_TYPE (t), NULL); | |
2d76680f | 8323 | |
60813a46 | 8324 | case STATEMENT_LIST: |
56632b27 JM |
8325 | new_ctx = *ctx; |
8326 | new_ctx.ctor = new_ctx.object = NULL_TREE; | |
2b3ab879 | 8327 | return cxx_eval_statement_list (&new_ctx, t, |
56632b27 | 8328 | non_constant_p, overflow_p, jump_target); |
60813a46 JM |
8329 | |
8330 | case BIND_EXPR: | |
90bc2d09 NS |
8331 | /* Pre-emptively clear the vars declared by this BIND_EXPR from the value |
8332 | map, so that when checking whether they're already destroyed later we | |
8333 | don't get confused by remnants of previous calls. */ | |
8334 | for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl)) | |
8335 | ctx->global->clear_value (decl); | |
9fdbd7d6 NS |
8336 | r = cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t), |
8337 | lval, | |
8338 | non_constant_p, overflow_p, | |
8339 | jump_target); | |
8340 | for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl)) | |
90bc2d09 NS |
8341 | destroy_value_checked (ctx, decl, non_constant_p); |
8342 | break; | |
60813a46 | 8343 | |
2d76680f PC |
8344 | case PREINCREMENT_EXPR: |
8345 | case POSTINCREMENT_EXPR: | |
8346 | case PREDECREMENT_EXPR: | |
8347 | case POSTDECREMENT_EXPR: | |
2b3ab879 | 8348 | return cxx_eval_increment_expression (ctx, t, |
92a596e8 | 8349 | lval, non_constant_p, overflow_p); |
60813a46 JM |
8350 | |
8351 | case LAMBDA_EXPR: | |
2d76680f PC |
8352 | case NEW_EXPR: |
8353 | case VEC_NEW_EXPR: | |
8354 | case DELETE_EXPR: | |
8355 | case VEC_DELETE_EXPR: | |
8356 | case THROW_EXPR: | |
2d76680f PC |
8357 | case MODOP_EXPR: |
8358 | /* GCC internal stuff. */ | |
8359 | case VA_ARG_EXPR: | |
2d76680f | 8360 | case BASELINK: |
2d76680f | 8361 | case OFFSET_REF: |
2b3ab879 | 8362 | if (!ctx->quiet) |
6821245b | 8363 | error_at (loc, "expression %qE is not a constant expression", t); |
2d76680f PC |
8364 | *non_constant_p = true; |
8365 | break; | |
8366 | ||
bf8d8309 | 8367 | case OBJ_TYPE_REF: |
7ece3bd8 JM |
8368 | /* Virtual function lookup. We don't need to do anything fancy. */ |
8369 | return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t), | |
8370 | lval, non_constant_p, overflow_p); | |
bf8d8309 | 8371 | |
3e605b20 | 8372 | case PLACEHOLDER_EXPR: |
89262ec6 JM |
8373 | /* Use of the value or address of the current object. */ |
8374 | if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t))) | |
b599bf9d PP |
8375 | { |
8376 | if (TREE_CODE (ctor) == CONSTRUCTOR) | |
8377 | return ctor; | |
8378 | else | |
8379 | return cxx_eval_constant_expression (ctx, ctor, lval, | |
8380 | non_constant_p, overflow_p); | |
8381 | } | |
89262ec6 JM |
8382 | /* A placeholder without a referent. We can get here when |
8383 | checking whether NSDMIs are noexcept, or in massage_init_elt; | |
8384 | just say it's non-constant for now. */ | |
8385 | gcc_assert (ctx->quiet); | |
8386 | *non_constant_p = true; | |
3e605b20 JM |
8387 | break; |
8388 | ||
06ec22b7 JM |
8389 | case EXIT_EXPR: |
8390 | { | |
8391 | tree cond = TREE_OPERAND (t, 0); | |
72f76540 | 8392 | cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue, |
06ec22b7 JM |
8393 | non_constant_p, overflow_p); |
8394 | VERIFY_CONSTANT (cond); | |
8395 | if (integer_nonzerop (cond)) | |
8396 | *jump_target = t; | |
8397 | } | |
8398 | break; | |
8399 | ||
60813a46 | 8400 | case GOTO_EXPR: |
8892d532 | 8401 | if (breaks (&TREE_OPERAND (t, 0)) |
9c6a4bee | 8402 | || continues (&TREE_OPERAND (t, 0))) |
8892d532 JJ |
8403 | *jump_target = TREE_OPERAND (t, 0); |
8404 | else | |
8405 | { | |
8406 | gcc_assert (cxx_dialect >= cxx23); | |
8407 | if (!ctx->quiet) | |
8408 | error_at (loc, "%<goto%> is not a constant expression"); | |
8409 | *non_constant_p = true; | |
8410 | } | |
56632b27 JM |
8411 | break; |
8412 | ||
60813a46 | 8413 | case LOOP_EXPR: |
43574e4f JJ |
8414 | case DO_STMT: |
8415 | case WHILE_STMT: | |
8416 | case FOR_STMT: | |
2b3ab879 | 8417 | cxx_eval_loop_expr (ctx, t, |
56632b27 JM |
8418 | non_constant_p, overflow_p, jump_target); |
8419 | break; | |
8420 | ||
60813a46 | 8421 | case SWITCH_EXPR: |
43574e4f | 8422 | case SWITCH_STMT: |
2b3ab879 | 8423 | cxx_eval_switch_expr (ctx, t, |
56632b27 | 8424 | non_constant_p, overflow_p, jump_target); |
60813a46 JM |
8425 | break; |
8426 | ||
971e17ff AS |
8427 | case REQUIRES_EXPR: |
8428 | /* It's possible to get a requires-expression in a constant | |
8429 | expression. For example: | |
8430 | ||
8431 | template<typename T> concept bool C() { | |
8432 | return requires (T t) { t; }; | |
8433 | } | |
8434 | ||
8435 | template<typename T> requires !C<T>() void f(T); | |
8436 | ||
8437 | Normalization leaves f with the associated constraint | |
8438 | '!requires (T t) { ... }' which is not transformed into | |
8439 | a constraint. */ | |
8440 | if (!processing_template_decl) | |
662ef5b5 | 8441 | return evaluate_requires_expr (t); |
971e17ff AS |
8442 | else |
8443 | *non_constant_p = true; | |
8444 | return t; | |
8445 | ||
98e09245 | 8446 | case ANNOTATE_EXPR: |
98e09245 JJ |
8447 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
8448 | lval, | |
8449 | non_constant_p, overflow_p, | |
8450 | jump_target); | |
8451 | break; | |
8452 | ||
3664e317 JM |
8453 | case USING_STMT: |
8454 | r = void_node; | |
8455 | break; | |
8456 | ||
2efb237f JCI |
8457 | case ASSERTION_STMT: |
8458 | case PRECONDITION_STMT: | |
8459 | case POSTCONDITION_STMT: | |
8460 | { | |
8461 | contract_semantic semantic = get_contract_semantic (t); | |
8462 | if (semantic == CCS_IGNORE) | |
8463 | break; | |
8464 | ||
8465 | if (!cxx_eval_assert (ctx, CONTRACT_CONDITION (t), | |
8466 | G_("contract predicate is false in " | |
8467 | "constant expression"), | |
8468 | EXPR_LOCATION (t), checked_contract_p (semantic), | |
8469 | non_constant_p, overflow_p)) | |
8470 | *non_constant_p = true; | |
8471 | r = void_node; | |
8472 | } | |
8473 | break; | |
8474 | ||
cb57504a JM |
8475 | case TEMPLATE_ID_EXPR: |
8476 | { | |
8477 | /* We can evaluate template-id that refers to a concept only if | |
8478 | the template arguments are non-dependent. */ | |
861d4af8 AS |
8479 | tree id = unpack_concept_check (t); |
8480 | tree tmpl = TREE_OPERAND (id, 0); | |
8481 | if (!concept_definition_p (tmpl)) | |
cb57504a JM |
8482 | internal_error ("unexpected template-id %qE", t); |
8483 | ||
861d4af8 AS |
8484 | if (function_concept_p (tmpl)) |
8485 | { | |
8486 | if (!ctx->quiet) | |
8487 | error_at (cp_expr_loc_or_input_loc (t), | |
8488 | "function concept must be called"); | |
8489 | r = error_mark_node; | |
8490 | break; | |
8491 | } | |
8492 | ||
9329344a | 8493 | if (!value_dependent_expression_p (t) |
6d1556ec | 8494 | && !uid_sensitive_constexpr_evaluation_p ()) |
662ef5b5 | 8495 | r = evaluate_concept_check (t); |
cb57504a JM |
8496 | else |
8497 | *non_constant_p = true; | |
861d4af8 AS |
8498 | |
8499 | break; | |
cb57504a JM |
8500 | } |
8501 | ||
529bc410 MP |
8502 | case ASM_EXPR: |
8503 | if (!ctx->quiet) | |
c85f8dbb | 8504 | inline_asm_in_constexpr_error (loc, /*constexpr_fundef_p*/false); |
529bc410 MP |
8505 | *non_constant_p = true; |
8506 | return t; | |
8507 | ||
896048cf | 8508 | case BIT_CAST_EXPR: |
606f2af1 JJ |
8509 | if (lval) |
8510 | { | |
8511 | if (!ctx->quiet) | |
8512 | error_at (EXPR_LOCATION (t), | |
8513 | "address of a call to %qs is not a constant expression", | |
8514 | "__builtin_bit_cast"); | |
8515 | *non_constant_p = true; | |
8516 | return t; | |
8517 | } | |
896048cf JJ |
8518 | r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p); |
8519 | break; | |
8520 | ||
bfc07059 JJ |
8521 | case OMP_PARALLEL: |
8522 | case OMP_TASK: | |
8523 | case OMP_FOR: | |
8524 | case OMP_SIMD: | |
8525 | case OMP_DISTRIBUTE: | |
8526 | case OMP_TASKLOOP: | |
8527 | case OMP_LOOP: | |
8528 | case OMP_TEAMS: | |
8529 | case OMP_TARGET_DATA: | |
8530 | case OMP_TARGET: | |
8531 | case OMP_SECTIONS: | |
8532 | case OMP_ORDERED: | |
8533 | case OMP_CRITICAL: | |
8534 | case OMP_SINGLE: | |
8535 | case OMP_SCAN: | |
8536 | case OMP_SCOPE: | |
8537 | case OMP_SECTION: | |
a62c8324 | 8538 | case OMP_STRUCTURED_BLOCK: |
bfc07059 JJ |
8539 | case OMP_MASTER: |
8540 | case OMP_MASKED: | |
8541 | case OMP_TASKGROUP: | |
8542 | case OMP_TARGET_UPDATE: | |
8543 | case OMP_TARGET_ENTER_DATA: | |
8544 | case OMP_TARGET_EXIT_DATA: | |
8545 | case OMP_ATOMIC: | |
8546 | case OMP_ATOMIC_READ: | |
8547 | case OMP_ATOMIC_CAPTURE_OLD: | |
8548 | case OMP_ATOMIC_CAPTURE_NEW: | |
8549 | case OMP_DEPOBJ: | |
8550 | case OACC_PARALLEL: | |
8551 | case OACC_KERNELS: | |
8552 | case OACC_SERIAL: | |
8553 | case OACC_DATA: | |
8554 | case OACC_HOST_DATA: | |
8555 | case OACC_LOOP: | |
8556 | case OACC_CACHE: | |
8557 | case OACC_DECLARE: | |
8558 | case OACC_ENTER_DATA: | |
8559 | case OACC_EXIT_DATA: | |
8560 | case OACC_UPDATE: | |
8561 | if (!ctx->quiet) | |
8562 | error_at (EXPR_LOCATION (t), | |
8563 | "statement is not a constant expression"); | |
8564 | *non_constant_p = true; | |
8565 | break; | |
8566 | ||
2d76680f | 8567 | default: |
c6e7c499 JM |
8568 | if (STATEMENT_CODE_P (TREE_CODE (t))) |
8569 | { | |
8570 | /* This function doesn't know how to deal with pre-genericize | |
8571 | statements; this can only happen with statement-expressions, | |
8572 | so for now just fail. */ | |
8573 | if (!ctx->quiet) | |
8574 | error_at (EXPR_LOCATION (t), | |
64d6d399 | 8575 | "statement is not a constant expression"); |
c6e7c499 JM |
8576 | } |
8577 | else | |
8578 | internal_error ("unexpected expression %qE of kind %s", t, | |
8579 | get_tree_code_name (TREE_CODE (t))); | |
2d76680f PC |
8580 | *non_constant_p = true; |
8581 | break; | |
8582 | } | |
8583 | ||
8584 | if (r == error_mark_node) | |
8585 | *non_constant_p = true; | |
8586 | ||
8587 | if (*non_constant_p) | |
8588 | return t; | |
8589 | else | |
8590 | return r; | |
8591 | } | |
8592 | ||
e079dced JM |
8593 | /* P0859: A function is needed for constant evaluation if it is a constexpr |
8594 | function that is named by an expression ([basic.def.odr]) that is | |
8595 | potentially constant evaluated. | |
8596 | ||
8597 | So we need to instantiate any constexpr functions mentioned by the | |
8598 | expression even if the definition isn't needed for evaluating the | |
8599 | expression. */ | |
8600 | ||
8601 | static tree | |
8602 | instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/) | |
8603 | { | |
8604 | if (TREE_CODE (*tp) == FUNCTION_DECL | |
8605 | && DECL_DECLARED_CONSTEXPR_P (*tp) | |
8606 | && !DECL_INITIAL (*tp) | |
4fdda3ce | 8607 | && !trivial_fn_p (*tp) |
59946a4c | 8608 | && (DECL_TEMPLOID_INSTANTIATION (*tp) || DECL_DEFAULTED_FN (*tp)) |
f65a3299 | 8609 | && !uid_sensitive_constexpr_evaluation_p ()) |
e079dced JM |
8610 | { |
8611 | ++function_depth; | |
59946a4c PP |
8612 | if (DECL_TEMPLOID_INSTANTIATION (*tp)) |
8613 | instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false); | |
8614 | else | |
8615 | synthesize_method (*tp); | |
e079dced JM |
8616 | --function_depth; |
8617 | } | |
8618 | else if (TREE_CODE (*tp) == CALL_EXPR | |
8619 | || TREE_CODE (*tp) == AGGR_INIT_EXPR) | |
8620 | { | |
8621 | if (EXPR_HAS_LOCATION (*tp)) | |
8622 | input_location = EXPR_LOCATION (*tp); | |
8623 | } | |
8624 | ||
8625 | if (!EXPR_P (*tp)) | |
8626 | *walk_subtrees = 0; | |
8627 | ||
8628 | return NULL_TREE; | |
8629 | } | |
8e007055 | 8630 | |
e079dced JM |
8631 | static void |
8632 | instantiate_constexpr_fns (tree t) | |
8633 | { | |
8634 | location_t loc = input_location; | |
8635 | cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL); | |
8636 | input_location = loc; | |
8637 | } | |
8638 | ||
8e007055 JJ |
8639 | /* Look for heap variables in the expression *TP. */ |
8640 | ||
8641 | static tree | |
8642 | find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/) | |
8643 | { | |
8644 | if (VAR_P (*tp) | |
8645 | && (DECL_NAME (*tp) == heap_uninit_identifier | |
8646 | || DECL_NAME (*tp) == heap_identifier | |
815baade JJ |
8647 | || DECL_NAME (*tp) == heap_vec_uninit_identifier |
8648 | || DECL_NAME (*tp) == heap_vec_identifier | |
8e007055 JJ |
8649 | || DECL_NAME (*tp) == heap_deleted_identifier)) |
8650 | return *tp; | |
8651 | ||
8652 | if (TYPE_P (*tp)) | |
8653 | *walk_subtrees = 0; | |
8654 | return NULL_TREE; | |
8655 | } | |
8656 | ||
f968ef9b JJ |
8657 | /* Find immediate function decls in *TP if any. */ |
8658 | ||
8659 | static tree | |
8660 | find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/) | |
8661 | { | |
8662 | if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp)) | |
8663 | return *tp; | |
4b2fda8b JJ |
8664 | if (TREE_CODE (*tp) == PTRMEM_CST |
8665 | && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL | |
8666 | && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp))) | |
8667 | return PTRMEM_CST_MEMBER (*tp); | |
f968ef9b JJ |
8668 | return NULL_TREE; |
8669 | } | |
8670 | ||
ab71d3fe PP |
8671 | /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant |
8672 | expression. Return a version of T that has TREE_CONSTANT cleared. */ | |
8673 | ||
8674 | static tree | |
8675 | mark_non_constant (tree t) | |
8676 | { | |
8677 | gcc_checking_assert (TREE_CONSTANT (t)); | |
8678 | ||
8679 | /* This isn't actually constant, so unset TREE_CONSTANT. | |
8680 | Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires | |
8681 | it to be set if it is invariant address, even when it is not | |
8682 | a valid C++ constant expression. Wrap it with a NOP_EXPR | |
8683 | instead. */ | |
8684 | if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR) | |
8685 | t = copy_node (t); | |
8686 | else if (TREE_CODE (t) == CONSTRUCTOR) | |
8687 | t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t); | |
8688 | else | |
8689 | t = build_nop (TREE_TYPE (t), t); | |
8690 | TREE_CONSTANT (t) = false; | |
8691 | return t; | |
8692 | } | |
8693 | ||
e4082611 | 8694 | /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression. |
d4accef3 JM |
8695 | STRICT has the same sense as for constant_value_1: true if we only allow |
8696 | conforming C++ constant expressions, or false if we want a constant value | |
8697 | even if it doesn't conform. | |
13de99bc | 8698 | MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as |
8e007055 JJ |
8699 | per P0595 even when ALLOW_NON_CONSTANT is true. |
8700 | CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable. | |
8701 | OBJECT must be non-NULL in that case. */ | |
e4082611 | 8702 | |
2d76680f | 8703 | static tree |
3e605b20 | 8704 | cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant, |
e4082611 | 8705 | bool strict = true, |
3a0bc47c | 8706 | mce_value manifestly_const_eval = mce_unknown, |
8e007055 | 8707 | bool constexpr_dtor = false, |
f65a3299 | 8708 | tree object = NULL_TREE) |
2d76680f | 8709 | { |
8108ea30 JM |
8710 | auto_timevar time (TV_CONSTEXPR); |
8711 | ||
2d76680f PC |
8712 | bool non_constant_p = false; |
8713 | bool overflow_p = false; | |
39dce2b7 | 8714 | |
d419e176 MP |
8715 | if (BRACE_ENCLOSED_INITIALIZER_P (t)) |
8716 | { | |
8717 | gcc_checking_assert (allow_non_constant); | |
8718 | return t; | |
8719 | } | |
8720 | ||
8e007055 | 8721 | constexpr_global_ctx global_ctx; |
49a86fce | 8722 | constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL, |
8e007055 | 8723 | allow_non_constant, strict, |
3a0bc47c | 8724 | !allow_non_constant ? mce_true : manifestly_const_eval }; |
39dce2b7 | 8725 | |
6641d6d3 | 8726 | /* Turn off -frounding-math for manifestly constant evaluation. */ |
3a0bc47c PP |
8727 | warning_sentinel rm (flag_rounding_math, |
8728 | ctx.manifestly_const_eval == mce_true); | |
3e605b20 | 8729 | tree type = initialized_type (t); |
3e605b20 | 8730 | tree r = t; |
f968ef9b | 8731 | bool is_consteval = false; |
4d0c18c6 | 8732 | if (VOID_TYPE_P (type)) |
8e007055 JJ |
8733 | { |
8734 | if (constexpr_dtor) | |
8735 | /* Used for destructors of array elements. */ | |
8736 | type = TREE_TYPE (object); | |
8737 | else | |
f968ef9b | 8738 | { |
b04445d4 | 8739 | if (cxx_dialect < cxx20) |
f968ef9b JJ |
8740 | return t; |
8741 | if (TREE_CODE (t) != CALL_EXPR && TREE_CODE (t) != AGGR_INIT_EXPR) | |
8742 | return t; | |
8743 | /* Calls to immediate functions returning void need to be | |
8744 | evaluated. */ | |
8745 | tree fndecl = cp_get_callee_fndecl_nofold (t); | |
8746 | if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl)) | |
8747 | return t; | |
8748 | else | |
8749 | is_consteval = true; | |
8750 | } | |
8751 | } | |
b04445d4 | 8752 | else if (cxx_dialect >= cxx20 |
f968ef9b JJ |
8753 | && (TREE_CODE (t) == CALL_EXPR |
8754 | || TREE_CODE (t) == AGGR_INIT_EXPR | |
8755 | || TREE_CODE (t) == TARGET_EXPR)) | |
8756 | { | |
861d4af8 AS |
8757 | /* For non-concept checks, determine if it is consteval. */ |
8758 | if (!concept_check_p (t)) | |
8759 | { | |
8760 | tree x = t; | |
8761 | if (TREE_CODE (x) == TARGET_EXPR) | |
8762 | x = TARGET_EXPR_INITIAL (x); | |
8763 | tree fndecl = cp_get_callee_fndecl_nofold (x); | |
8764 | if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl)) | |
8765 | is_consteval = true; | |
8766 | } | |
8e007055 | 8767 | } |
3e605b20 JM |
8768 | if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)) |
8769 | { | |
8770 | /* In C++14 an NSDMI can participate in aggregate initialization, | |
8771 | and can refer to the address of the object being initialized, so | |
8772 | we need to pass in the relevant VAR_DECL if we want to do the | |
8773 | evaluation in a single pass. The evaluation will dynamically | |
8774 | update ctx.values for the VAR_DECL. We use the same strategy | |
8775 | for C++11 constexpr constructors that refer to the object being | |
8776 | initialized. */ | |
8e007055 JJ |
8777 | if (constexpr_dtor) |
8778 | { | |
8779 | gcc_assert (object && VAR_P (object)); | |
8780 | gcc_assert (DECL_DECLARED_CONSTEXPR_P (object)); | |
8781 | gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object)); | |
fce6467b JJ |
8782 | if (error_operand_p (DECL_INITIAL (object))) |
8783 | return t; | |
8e007055 JJ |
8784 | ctx.ctor = unshare_expr (DECL_INITIAL (object)); |
8785 | TREE_READONLY (ctx.ctor) = false; | |
8786 | /* Temporarily force decl_really_constant_value to return false | |
8787 | for it, we want to use ctx.ctor for the current value instead. */ | |
8788 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false; | |
8789 | } | |
8790 | else | |
8791 | { | |
8792 | ctx.ctor = build_constructor (type, NULL); | |
8793 | CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true; | |
8794 | } | |
60813a46 JM |
8795 | if (!object) |
8796 | { | |
958940eb JM |
8797 | if (TREE_CODE (t) == CALL_EXPR) |
8798 | { | |
8799 | /* If T is calling a constructor to initialize an object, reframe | |
8800 | it as an AGGR_INIT_EXPR to avoid trying to modify an object | |
8801 | from outside the constant evaluation, which will fail even if | |
8802 | the value is actually constant (is_constant_evaluated3.C). */ | |
8803 | tree fn = cp_get_callee_fndecl_nofold (t); | |
8804 | if (fn && DECL_CONSTRUCTOR_P (fn)) | |
8805 | { | |
8806 | object = CALL_EXPR_ARG (t, 0); | |
8807 | object = build_fold_indirect_ref (object); | |
8808 | r = build_aggr_init_expr (type, r); | |
8809 | } | |
8810 | } | |
8811 | else if (TREE_CODE (t) == TARGET_EXPR) | |
60813a46 JM |
8812 | object = TARGET_EXPR_SLOT (t); |
8813 | else if (TREE_CODE (t) == AGGR_INIT_EXPR) | |
8814 | object = AGGR_INIT_EXPR_SLOT (t); | |
8815 | } | |
3e605b20 JM |
8816 | ctx.object = object; |
8817 | if (object) | |
8818 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
8819 | (type, TREE_TYPE (object))); | |
8820 | if (object && DECL_P (object)) | |
e6a29aab | 8821 | global_ctx.put_value (object, ctx.ctor); |
3e605b20 JM |
8822 | if (TREE_CODE (r) == TARGET_EXPR) |
8823 | /* Avoid creating another CONSTRUCTOR when we expand the | |
8824 | TARGET_EXPR. */ | |
8825 | r = TARGET_EXPR_INITIAL (r); | |
8826 | } | |
8827 | ||
ee1de08d JJ |
8828 | auto_vec<tree, 16> cleanups; |
8829 | global_ctx.cleanups = &cleanups; | |
8830 | ||
3a0bc47c | 8831 | if (manifestly_const_eval == mce_true) |
1595fe44 | 8832 | instantiate_constexpr_fns (r); |
72f76540 JM |
8833 | r = cxx_eval_constant_expression (&ctx, r, vc_prvalue, |
8834 | &non_constant_p, &overflow_p); | |
2d76680f | 8835 | |
8e007055 JJ |
8836 | if (!constexpr_dtor) |
8837 | verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p); | |
8838 | else | |
8839 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true; | |
2d76680f | 8840 | |
ee1de08d JJ |
8841 | unsigned int i; |
8842 | tree cleanup; | |
8843 | /* Evaluate the cleanups. */ | |
8844 | FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup) | |
72f76540 | 8845 | cxx_eval_constant_expression (&ctx, cleanup, vc_discard, |
ee1de08d JJ |
8846 | &non_constant_p, &overflow_p); |
8847 | ||
023d89c7 JM |
8848 | /* Mutable logic is a bit tricky: we want to allow initialization of |
8849 | constexpr variables with mutable members, but we can't copy those | |
8850 | members to another constexpr variable. */ | |
8e007055 | 8851 | if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_MUTABLE_POISON (r)) |
2d76680f | 8852 | { |
2d76680f | 8853 | if (!allow_non_constant) |
023d89c7 JM |
8854 | error ("%qE is not a constant expression because it refers to " |
8855 | "mutable subobjects of %qT", t, type); | |
2d76680f PC |
8856 | non_constant_p = true; |
8857 | } | |
8858 | ||
8e007055 | 8859 | if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r)) |
f64e0c02 JM |
8860 | { |
8861 | if (!allow_non_constant) | |
8862 | error ("%qE is not a constant expression because it refers to " | |
8863 | "an incompletely initialized variable", t); | |
8864 | TREE_CONSTANT (r) = false; | |
8865 | non_constant_p = true; | |
8866 | } | |
8867 | ||
688fdde2 JM |
8868 | if (!non_constant_p && cxx_dialect >= cxx20 |
8869 | && !global_ctx.heap_vars.is_empty ()) | |
8e007055 JJ |
8870 | { |
8871 | tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs, | |
8872 | NULL); | |
8873 | unsigned int i; | |
8874 | if (heap_var) | |
8875 | { | |
8876 | if (!allow_non_constant && !non_constant_p) | |
8877 | error_at (DECL_SOURCE_LOCATION (heap_var), | |
8878 | "%qE is not a constant expression because it refers to " | |
8879 | "a result of %<operator new%>", t); | |
8880 | r = t; | |
8881 | non_constant_p = true; | |
8882 | } | |
8883 | FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var) | |
a8db7887 JJ |
8884 | { |
8885 | if (DECL_NAME (heap_var) != heap_deleted_identifier) | |
8886 | { | |
8887 | if (!allow_non_constant && !non_constant_p) | |
8888 | error_at (DECL_SOURCE_LOCATION (heap_var), | |
8889 | "%qE is not a constant expression because allocated " | |
8890 | "storage has not been deallocated", t); | |
8891 | r = t; | |
8892 | non_constant_p = true; | |
8893 | } | |
8894 | varpool_node::get (heap_var)->remove (); | |
8895 | } | |
8e007055 JJ |
8896 | } |
8897 | ||
f968ef9b | 8898 | /* Check that immediate invocation does not return an expression referencing |
7473b8a9 | 8899 | any immediate function decls. */ |
688fdde2 | 8900 | if (!non_constant_p && cxx_dialect >= cxx20) |
f968ef9b JJ |
8901 | if (tree immediate_fndecl |
8902 | = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl, | |
8903 | NULL)) | |
8904 | { | |
8905 | if (!allow_non_constant && !non_constant_p) | |
688fdde2 JM |
8906 | { |
8907 | if (is_consteval) | |
8908 | error_at (cp_expr_loc_or_input_loc (t), | |
8909 | "immediate evaluation returns address of immediate " | |
8910 | "function %qD", immediate_fndecl); | |
8911 | else | |
8912 | error_at (cp_expr_loc_or_input_loc (t), | |
8913 | "constant evaluation returns address of immediate " | |
8914 | "function %qD", immediate_fndecl); | |
8915 | } | |
f968ef9b JJ |
8916 | r = t; |
8917 | non_constant_p = true; | |
8918 | } | |
8919 | ||
8895443a JM |
8920 | if (non_constant_p) |
8921 | /* If we saw something bad, go back to our argument. The wrapping below is | |
8922 | only for the cases of TREE_CONSTANT argument or overflow. */ | |
8923 | r = t; | |
2d76680f PC |
8924 | |
8925 | if (!non_constant_p && overflow_p) | |
8926 | non_constant_p = true; | |
8927 | ||
e0804c9b | 8928 | /* Unshare the result. */ |
56cfb596 | 8929 | bool should_unshare = true; |
e0804c9b JM |
8930 | if (r == t || (TREE_CODE (t) == TARGET_EXPR |
8931 | && TARGET_EXPR_INITIAL (t) == r)) | |
56cfb596 PP |
8932 | should_unshare = false; |
8933 | ||
2d76680f PC |
8934 | if (non_constant_p && !allow_non_constant) |
8935 | return error_mark_node; | |
8e007055 JJ |
8936 | else if (constexpr_dtor) |
8937 | return r; | |
2d76680f | 8938 | else if (non_constant_p && TREE_CONSTANT (r)) |
ab71d3fe | 8939 | r = mark_non_constant (r); |
5dab8b11 | 8940 | else if (non_constant_p) |
2d76680f PC |
8941 | return t; |
8942 | ||
56cfb596 PP |
8943 | if (should_unshare) |
8944 | r = unshare_expr (r); | |
8945 | ||
2d76680f PC |
8946 | if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r))) |
8947 | { | |
5dab8b11 | 8948 | r = adjust_temp_type (type, r); |
2d76680f PC |
8949 | if (TREE_CODE (t) == TARGET_EXPR |
8950 | && TARGET_EXPR_INITIAL (t) == r) | |
8951 | return t; | |
43faf3e5 JM |
8952 | else if (TREE_CODE (t) == CONSTRUCTOR || TREE_CODE (t) == CALL_EXPR) |
8953 | /* Don't add a TARGET_EXPR if our argument didn't have one. */; | |
1a5145f1 JJ |
8954 | else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t)) |
8955 | r = get_target_expr (r); | |
8956 | else | |
2d76680f | 8957 | { |
c17fa0f2 | 8958 | r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup); |
2d76680f | 8959 | TREE_CONSTANT (r) = true; |
2d76680f PC |
8960 | } |
8961 | } | |
5dab8b11 | 8962 | |
8d46516a JM |
8963 | if (TREE_CODE (t) == TARGET_EXPR |
8964 | && TREE_CODE (r) == TARGET_EXPR) | |
8965 | { | |
8966 | /* Preserve this flag for potential_constant_expression, and the others | |
8967 | for good measure. */ | |
8968 | TARGET_EXPR_ELIDING_P (r) = TARGET_EXPR_ELIDING_P (t); | |
8969 | TARGET_EXPR_IMPLICIT_P (r) = TARGET_EXPR_IMPLICIT_P (t); | |
8970 | TARGET_EXPR_LIST_INIT_P (r) = TARGET_EXPR_LIST_INIT_P (t); | |
8971 | TARGET_EXPR_DIRECT_INIT_P (r) = TARGET_EXPR_DIRECT_INIT_P (t); | |
8972 | } | |
8973 | ||
729f6881 JM |
8974 | /* Remember the original location if that wouldn't need a wrapper. */ |
8975 | if (location_t loc = EXPR_LOCATION (t)) | |
8d970817 | 8976 | protected_set_expr_location (r, loc); |
729f6881 | 8977 | |
5dab8b11 | 8978 | return r; |
2d76680f PC |
8979 | } |
8980 | ||
2d76680f | 8981 | /* If T represents a constant expression returns its reduced value. |
c17fa0f2 | 8982 | Otherwise return error_mark_node. */ |
2d76680f PC |
8983 | |
8984 | tree | |
c17fa0f2 PP |
8985 | cxx_constant_value (tree t, tree decl /* = NULL_TREE */, |
8986 | tsubst_flags_t complain /* = tf_error */) | |
a8de8324 JM |
8987 | { |
8988 | bool sfinae = !(complain & tf_error); | |
3a0bc47c | 8989 | tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, mce_true, false, decl); |
a8de8324 JM |
8990 | if (sfinae && !TREE_CONSTANT (r)) |
8991 | r = error_mark_node; | |
8992 | return r; | |
8993 | } | |
8994 | ||
8e007055 JJ |
8995 | /* Like cxx_constant_value, but used for evaluation of constexpr destructors |
8996 | of constexpr variables. The actual initializer of DECL is not modified. */ | |
8997 | ||
8998 | void | |
8999 | cxx_constant_dtor (tree t, tree decl) | |
9000 | { | |
3a0bc47c | 9001 | cxx_eval_outermost_constant_expr (t, false, true, mce_true, true, decl); |
2d76680f PC |
9002 | } |
9003 | ||
cda0a029 JM |
9004 | /* Helper routine for fold_simple function. Either return simplified |
9005 | expression T, otherwise NULL_TREE. | |
9006 | In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold | |
9007 | even if we are within template-declaration. So be careful on call, as in | |
9008 | such case types can be undefined. */ | |
9009 | ||
9010 | static tree | |
9011 | fold_simple_1 (tree t) | |
9012 | { | |
9013 | tree op1; | |
9014 | enum tree_code code = TREE_CODE (t); | |
9015 | ||
9016 | switch (code) | |
9017 | { | |
9018 | case INTEGER_CST: | |
9019 | case REAL_CST: | |
9020 | case VECTOR_CST: | |
9021 | case FIXED_CST: | |
9022 | case COMPLEX_CST: | |
9023 | return t; | |
9024 | ||
9025 | case SIZEOF_EXPR: | |
9026 | return fold_sizeof_expr (t); | |
9027 | ||
9028 | case ABS_EXPR: | |
e28fadbc | 9029 | case ABSU_EXPR: |
cda0a029 JM |
9030 | case CONJ_EXPR: |
9031 | case REALPART_EXPR: | |
9032 | case IMAGPART_EXPR: | |
9033 | case NEGATE_EXPR: | |
9034 | case BIT_NOT_EXPR: | |
9035 | case TRUTH_NOT_EXPR: | |
cda0a029 | 9036 | case VIEW_CONVERT_EXPR: |
82a344f2 | 9037 | CASE_CONVERT: |
cda0a029 JM |
9038 | case FLOAT_EXPR: |
9039 | case FIX_TRUNC_EXPR: | |
9040 | case FIXED_CONVERT_EXPR: | |
9041 | case ADDR_SPACE_CONVERT_EXPR: | |
9042 | ||
9043 | op1 = TREE_OPERAND (t, 0); | |
9044 | ||
9045 | t = const_unop (code, TREE_TYPE (t), op1); | |
9046 | if (!t) | |
9047 | return NULL_TREE; | |
9048 | ||
9049 | if (CONVERT_EXPR_CODE_P (code) | |
9050 | && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1)) | |
9051 | TREE_OVERFLOW (t) = false; | |
9052 | return t; | |
9053 | ||
9054 | default: | |
9055 | return NULL_TREE; | |
9056 | } | |
9057 | } | |
9058 | ||
9059 | /* If T is a simple constant expression, returns its simplified value. | |
dba99244 | 9060 | Otherwise returns T. In contrast to maybe_constant_value we |
cda0a029 JM |
9061 | simplify only few operations on constant-expressions, and we don't |
9062 | try to simplify constexpressions. */ | |
9063 | ||
9064 | tree | |
9065 | fold_simple (tree t) | |
9066 | { | |
cda0a029 JM |
9067 | if (processing_template_decl) |
9068 | return t; | |
9069 | ||
dba99244 MP |
9070 | tree r = fold_simple_1 (t); |
9071 | if (r) | |
9072 | return r; | |
cda0a029 | 9073 | |
dba99244 | 9074 | return t; |
cda0a029 JM |
9075 | } |
9076 | ||
096f034a PP |
9077 | /* Try folding the expression T to a simple constant. |
9078 | Returns that constant, otherwise returns T. */ | |
9079 | ||
9080 | tree | |
9081 | fold_to_constant (tree t) | |
9082 | { | |
9083 | tree r = fold (t); | |
9084 | if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r)) | |
9085 | return r; | |
9086 | else | |
9087 | return t; | |
9088 | } | |
9089 | ||
2d76680f PC |
9090 | /* If T is a constant expression, returns its reduced value. |
9091 | Otherwise, if T does not have TREE_CONSTANT set, returns T. | |
13de99bc JJ |
9092 | Otherwise, returns a version of T without TREE_CONSTANT. |
9093 | MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated | |
f65a3299 | 9094 | as per P0595. */ |
2d76680f | 9095 | |
8a87daca JM |
9096 | static GTY((deletable)) hash_map<tree, tree> *cv_cache; |
9097 | ||
9098 | tree | |
3da5ae7a | 9099 | maybe_constant_value (tree t, tree decl /* = NULL_TREE */, |
3a0bc47c | 9100 | mce_value manifestly_const_eval /* = mce_unknown */) |
2d76680f PC |
9101 | { |
9102 | tree r; | |
9103 | ||
a0ab7ccd | 9104 | if (!is_nondependent_constant_expression (t)) |
2d76680f | 9105 | { |
ab71d3fe PP |
9106 | if (TREE_OVERFLOW_P (t) |
9107 | || (!processing_template_decl && TREE_CONSTANT (t))) | |
9108 | t = mark_non_constant (t); | |
2d76680f PC |
9109 | return t; |
9110 | } | |
8a87daca JM |
9111 | else if (CONSTANT_CLASS_P (t)) |
9112 | /* No caching or evaluation needed. */ | |
9113 | return t; | |
9114 | ||
096f034a PP |
9115 | /* Don't constant evaluate an unevaluated non-manifestly-constant operand, |
9116 | but at least try folding it to a simple constant. */ | |
9117 | if (cp_unevaluated_operand && manifestly_const_eval != mce_true) | |
9118 | return fold_to_constant (t); | |
9119 | ||
3a0bc47c PP |
9120 | if (manifestly_const_eval != mce_unknown) |
9121 | return cxx_eval_outermost_constant_expr (t, true, true, | |
9122 | manifestly_const_eval, false, decl); | |
13de99bc | 9123 | |
8a87daca JM |
9124 | if (cv_cache == NULL) |
9125 | cv_cache = hash_map<tree, tree>::create_ggc (101); | |
9126 | if (tree *cached = cv_cache->get (t)) | |
173c8def JM |
9127 | { |
9128 | r = *cached; | |
9129 | if (r != t) | |
9130 | { | |
cf59c898 PP |
9131 | /* Clear processing_template_decl for sake of break_out_target_exprs; |
9132 | entries in the cv_cache are non-templated. */ | |
9133 | processing_template_decl_sentinel ptds; | |
9134 | ||
9f143008 | 9135 | r = break_out_target_exprs (r, /*clear_loc*/true); |
173c8def JM |
9136 | protected_set_expr_location (r, EXPR_LOCATION (t)); |
9137 | } | |
dfffecb8 | 9138 | return r; |
173c8def | 9139 | } |
2d76680f | 9140 | |
f65a3299 | 9141 | uid_sensitive_constexpr_evaluation_checker c; |
3a0bc47c PP |
9142 | r = cxx_eval_outermost_constant_expr (t, true, true, |
9143 | manifestly_const_eval, false, decl); | |
595278be MM |
9144 | gcc_checking_assert (r == t |
9145 | || CONVERT_EXPR_P (t) | |
9146 | || TREE_CODE (t) == VIEW_CONVERT_EXPR | |
9147 | || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) | |
9148 | || !cp_tree_equal (r, t)); | |
f65a3299 PP |
9149 | if (!c.evaluation_restricted_p ()) |
9150 | cv_cache->put (t, r); | |
2d76680f PC |
9151 | return r; |
9152 | } | |
9153 | ||
7a7ac32a PP |
9154 | /* Dispose of the whole CV_CACHE. */ |
9155 | ||
9156 | static void | |
9157 | clear_cv_cache (void) | |
9158 | { | |
9159 | if (cv_cache != NULL) | |
9160 | cv_cache->empty (); | |
9161 | } | |
9162 | ||
bebabf70 | 9163 | /* Dispose of the whole CV_CACHE and FOLD_CACHE. */ |
1e297006 MP |
9164 | |
9165 | void | |
bebabf70 | 9166 | clear_cv_and_fold_caches () |
1e297006 | 9167 | { |
7a7ac32a | 9168 | clear_cv_cache (); |
eba9e839 | 9169 | clear_fold_cache (); |
1e297006 MP |
9170 | } |
9171 | ||
a81c8e8c MP |
9172 | /* Internal function handling expressions in templates for |
9173 | fold_non_dependent_expr and fold_non_dependent_init. | |
9174 | ||
9175 | If we're in a template, but T isn't value dependent, simplify | |
9176 | it. We're supposed to treat: | |
9177 | ||
9178 | template <typename T> void f(T[1 + 1]); | |
9179 | template <typename T> void f(T[2]); | |
9180 | ||
9181 | as two declarations of the same function, for example. */ | |
9182 | ||
9183 | static tree | |
9184 | fold_non_dependent_expr_template (tree t, tsubst_flags_t complain, | |
f968ef9b JJ |
9185 | bool manifestly_const_eval, |
9186 | tree object) | |
a81c8e8c MP |
9187 | { |
9188 | gcc_assert (processing_template_decl); | |
9189 | ||
9190 | if (is_nondependent_constant_expression (t)) | |
9191 | { | |
9192 | processing_template_decl_sentinel s; | |
9193 | t = instantiate_non_dependent_expr_internal (t, complain); | |
9194 | ||
9195 | if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t)) | |
9196 | { | |
9197 | if (TREE_OVERFLOW_P (t)) | |
9198 | { | |
9199 | t = build_nop (TREE_TYPE (t), t); | |
9200 | TREE_CONSTANT (t) = false; | |
9201 | } | |
9202 | return t; | |
9203 | } | |
096f034a PP |
9204 | else if (CONSTANT_CLASS_P (t)) |
9205 | /* No evaluation needed. */ | |
9206 | return t; | |
a81c8e8c | 9207 | |
096f034a PP |
9208 | /* Don't constant evaluate an unevaluated non-manifestly-constant operand, |
9209 | but at least try folding it to a simple constant. */ | |
4df7f8c7 | 9210 | if (cp_unevaluated_operand && !manifestly_const_eval) |
096f034a | 9211 | return fold_to_constant (t); |
4df7f8c7 | 9212 | |
a81c8e8c | 9213 | tree r = cxx_eval_outermost_constant_expr (t, true, true, |
3a0bc47c | 9214 | mce_value (manifestly_const_eval), |
f968ef9b | 9215 | false, object); |
a81c8e8c MP |
9216 | /* cp_tree_equal looks through NOPs, so allow them. */ |
9217 | gcc_checking_assert (r == t | |
9218 | || CONVERT_EXPR_P (t) | |
9219 | || TREE_CODE (t) == VIEW_CONVERT_EXPR | |
9220 | || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) | |
9221 | || !cp_tree_equal (r, t)); | |
9222 | return r; | |
9223 | } | |
9224 | else if (TREE_OVERFLOW_P (t)) | |
9225 | { | |
9226 | t = build_nop (TREE_TYPE (t), t); | |
9227 | TREE_CONSTANT (t) = false; | |
9228 | } | |
9229 | ||
9230 | return t; | |
9231 | } | |
9232 | ||
234bef96 PC |
9233 | /* Like maybe_constant_value but first fully instantiate the argument. |
9234 | ||
c17fa0f2 PP |
9235 | Note: this is equivalent to instantiate_non_dependent_expr (t, complain) |
9236 | followed by maybe_constant_value but is more efficient, | |
e56f6629 JM |
9237 | because it calls instantiation_dependent_expression_p and |
9238 | potential_constant_expression at most once. | |
4cd3e7df | 9239 | The manifestly_const_eval argument is passed to maybe_constant_value. |
e56f6629 JM |
9240 | |
9241 | Callers should generally pass their active complain, or if they are in a | |
9242 | non-template, diagnosing context, they can use the default of | |
9243 | tf_warning_or_error. Callers that might be within a template context, don't | |
9244 | have a complain parameter, and aren't going to remember the result for long | |
9245 | (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node | |
9246 | appropriately. */ | |
234bef96 PC |
9247 | |
9248 | tree | |
e56f6629 | 9249 | fold_non_dependent_expr (tree t, |
4cd3e7df | 9250 | tsubst_flags_t complain /* = tf_warning_or_error */, |
f968ef9b JJ |
9251 | bool manifestly_const_eval /* = false */, |
9252 | tree object /* = NULL_TREE */) | |
234bef96 PC |
9253 | { |
9254 | if (t == NULL_TREE) | |
9255 | return NULL_TREE; | |
9256 | ||
a81c8e8c MP |
9257 | if (processing_template_decl) |
9258 | return fold_non_dependent_expr_template (t, complain, | |
f968ef9b | 9259 | manifestly_const_eval, object); |
234bef96 | 9260 | |
3a0bc47c | 9261 | return maybe_constant_value (t, object, mce_value (manifestly_const_eval)); |
a81c8e8c | 9262 | } |
234bef96 | 9263 | |
ae2ebf01 MP |
9264 | /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant, |
9265 | return the original expression. */ | |
9266 | ||
9267 | tree | |
9268 | maybe_fold_non_dependent_expr (tree expr, | |
9269 | tsubst_flags_t complain/*=tf_warning_or_error*/) | |
9270 | { | |
9271 | tree t = fold_non_dependent_expr (expr, complain); | |
9272 | if (t && TREE_CONSTANT (t)) | |
9273 | return t; | |
9274 | ||
9275 | return expr; | |
9276 | } | |
234bef96 | 9277 | |
a81c8e8c | 9278 | /* Like maybe_constant_init but first fully instantiate the argument. */ |
234bef96 | 9279 | |
a81c8e8c MP |
9280 | tree |
9281 | fold_non_dependent_init (tree t, | |
9282 | tsubst_flags_t complain /*=tf_warning_or_error*/, | |
69bf1c7d MP |
9283 | bool manifestly_const_eval /*=false*/, |
9284 | tree object /* = NULL_TREE */) | |
a81c8e8c MP |
9285 | { |
9286 | if (t == NULL_TREE) | |
9287 | return NULL_TREE; | |
9288 | ||
9289 | if (processing_template_decl) | |
9290 | { | |
9291 | t = fold_non_dependent_expr_template (t, complain, | |
69bf1c7d | 9292 | manifestly_const_eval, object); |
a81c8e8c MP |
9293 | /* maybe_constant_init does this stripping, so do it here too. */ |
9294 | if (TREE_CODE (t) == TARGET_EXPR) | |
234bef96 | 9295 | { |
a81c8e8c MP |
9296 | tree init = TARGET_EXPR_INITIAL (t); |
9297 | if (TREE_CODE (init) == CONSTRUCTOR) | |
9298 | t = init; | |
234bef96 PC |
9299 | } |
9300 | return t; | |
9301 | } | |
9302 | ||
69bf1c7d | 9303 | return maybe_constant_init (t, object, manifestly_const_eval); |
234bef96 PC |
9304 | } |
9305 | ||
2d76680f | 9306 | /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather |
e4082611 JJ |
9307 | than wrapped in a TARGET_EXPR. |
9308 | ALLOW_NON_CONSTANT is false if T is required to be a constant expression. | |
13de99bc | 9309 | MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as |
e4082611 | 9310 | per P0595 even when ALLOW_NON_CONSTANT is true. */ |
2d76680f | 9311 | |
118cd6ba | 9312 | static tree |
e4082611 | 9313 | maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant, |
13de99bc | 9314 | bool manifestly_const_eval) |
2d76680f | 9315 | { |
cda0a029 JM |
9316 | if (!t) |
9317 | return t; | |
2d76680f PC |
9318 | if (TREE_CODE (t) == EXPR_STMT) |
9319 | t = TREE_OPERAND (t, 0); | |
9320 | if (TREE_CODE (t) == CONVERT_EXPR | |
9321 | && VOID_TYPE_P (TREE_TYPE (t))) | |
9322 | t = TREE_OPERAND (t, 0); | |
60813a46 JM |
9323 | if (TREE_CODE (t) == INIT_EXPR) |
9324 | t = TREE_OPERAND (t, 1); | |
f64e0c02 JM |
9325 | if (TREE_CODE (t) == TARGET_EXPR) |
9326 | t = TARGET_EXPR_INITIAL (t); | |
a0ab7ccd | 9327 | if (!is_nondependent_static_init_expression (t)) |
69eb4fde | 9328 | /* Don't try to evaluate it. */; |
688fdde2 JM |
9329 | else if (CONSTANT_CLASS_P (t) && TREE_CODE (t) != PTRMEM_CST) |
9330 | /* No evaluation needed. PTRMEM_CST needs the immediate fn check. */; | |
69eb4fde | 9331 | else |
6209009d JM |
9332 | { |
9333 | /* [basic.start.static] allows constant-initialization of variables with | |
9334 | static or thread storage duration even if it isn't required, but we | |
9335 | shouldn't bend the rules the same way for automatic variables. */ | |
9336 | bool is_static = (decl && DECL_P (decl) | |
9337 | && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))); | |
cbaa1d9c PP |
9338 | if (is_static) |
9339 | manifestly_const_eval = true; | |
31cdfdef PP |
9340 | |
9341 | if (cp_unevaluated_operand && !manifestly_const_eval) | |
9342 | return fold_to_constant (t); | |
9343 | ||
6209009d | 9344 | t = cxx_eval_outermost_constant_expr (t, allow_non_constant, !is_static, |
3a0bc47c PP |
9345 | mce_value (manifestly_const_eval), |
9346 | false, decl); | |
6209009d | 9347 | } |
2d76680f PC |
9348 | if (TREE_CODE (t) == TARGET_EXPR) |
9349 | { | |
9350 | tree init = TARGET_EXPR_INITIAL (t); | |
9351 | if (TREE_CODE (init) == CONSTRUCTOR) | |
9352 | t = init; | |
9353 | } | |
9354 | return t; | |
9355 | } | |
9356 | ||
118cd6ba MP |
9357 | /* Wrapper for maybe_constant_init_1 which permits non constants. */ |
9358 | ||
9359 | tree | |
13de99bc | 9360 | maybe_constant_init (tree t, tree decl, bool manifestly_const_eval) |
118cd6ba | 9361 | { |
13de99bc | 9362 | return maybe_constant_init_1 (t, decl, true, manifestly_const_eval); |
118cd6ba MP |
9363 | } |
9364 | ||
9365 | /* Wrapper for maybe_constant_init_1 which does not permit non constants. */ | |
9366 | ||
9367 | tree | |
9368 | cxx_constant_init (tree t, tree decl) | |
9369 | { | |
e4082611 | 9370 | return maybe_constant_init_1 (t, decl, false, true); |
118cd6ba MP |
9371 | } |
9372 | ||
2d76680f PC |
9373 | #if 0 |
9374 | /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */ | |
9375 | /* Return true if the object referred to by REF has automatic or thread | |
9376 | local storage. */ | |
9377 | ||
9378 | enum { ck_ok, ck_bad, ck_unknown }; | |
9379 | static int | |
9380 | check_automatic_or_tls (tree ref) | |
9381 | { | |
ef4bddc2 | 9382 | machine_mode mode; |
f37fac2b | 9383 | poly_int64 bitsize, bitpos; |
2d76680f PC |
9384 | tree offset; |
9385 | int volatilep = 0, unsignedp = 0; | |
9386 | tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset, | |
9387 | &mode, &unsignedp, &volatilep, false); | |
9388 | duration_kind dk; | |
9389 | ||
9390 | /* If there isn't a decl in the middle, we don't know the linkage here, | |
9391 | and this isn't a constant expression anyway. */ | |
9392 | if (!DECL_P (decl)) | |
9393 | return ck_unknown; | |
9394 | dk = decl_storage_duration (decl); | |
9395 | return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok; | |
9396 | } | |
9397 | #endif | |
9398 | ||
c7a53bdb JJ |
9399 | /* Data structure for passing data from potential_constant_expression_1 |
9400 | to check_for_return_continue via cp_walk_tree. */ | |
9401 | struct check_for_return_continue_data { | |
9402 | hash_set<tree> *pset; | |
9403 | tree continue_stmt; | |
0fb7aa20 | 9404 | tree break_stmt; |
c7a53bdb JJ |
9405 | }; |
9406 | ||
9407 | /* Helper function for potential_constant_expression_1 SWITCH_STMT handling, | |
9408 | called through cp_walk_tree. Return the first RETURN_EXPR found, or note | |
0fb7aa20 | 9409 | the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */ |
c7a53bdb JJ |
9410 | static tree |
9411 | check_for_return_continue (tree *tp, int *walk_subtrees, void *data) | |
9412 | { | |
0fb7aa20 | 9413 | tree t = *tp, s, b; |
c7a53bdb JJ |
9414 | check_for_return_continue_data *d = (check_for_return_continue_data *) data; |
9415 | switch (TREE_CODE (t)) | |
9416 | { | |
9417 | case RETURN_EXPR: | |
9418 | return t; | |
9419 | ||
9420 | case CONTINUE_STMT: | |
9421 | if (d->continue_stmt == NULL_TREE) | |
9422 | d->continue_stmt = t; | |
9423 | break; | |
9424 | ||
0fb7aa20 JJ |
9425 | case BREAK_STMT: |
9426 | if (d->break_stmt == NULL_TREE) | |
9427 | d->break_stmt = t; | |
9428 | break; | |
9429 | ||
c7a53bdb JJ |
9430 | #define RECUR(x) \ |
9431 | if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \ | |
9432 | d->pset)) \ | |
9433 | return r | |
9434 | ||
9435 | /* For loops, walk subtrees manually, so that continue stmts found | |
9436 | inside of the bodies of the loops are ignored. */ | |
9437 | case DO_STMT: | |
9438 | *walk_subtrees = 0; | |
9439 | RECUR (DO_COND (t)); | |
9440 | s = d->continue_stmt; | |
0fb7aa20 | 9441 | b = d->break_stmt; |
c7a53bdb JJ |
9442 | RECUR (DO_BODY (t)); |
9443 | d->continue_stmt = s; | |
0fb7aa20 | 9444 | d->break_stmt = b; |
c7a53bdb JJ |
9445 | break; |
9446 | ||
9447 | case WHILE_STMT: | |
9448 | *walk_subtrees = 0; | |
9449 | RECUR (WHILE_COND (t)); | |
9450 | s = d->continue_stmt; | |
0fb7aa20 | 9451 | b = d->break_stmt; |
c7a53bdb JJ |
9452 | RECUR (WHILE_BODY (t)); |
9453 | d->continue_stmt = s; | |
0fb7aa20 | 9454 | d->break_stmt = b; |
c7a53bdb JJ |
9455 | break; |
9456 | ||
9457 | case FOR_STMT: | |
9458 | *walk_subtrees = 0; | |
9459 | RECUR (FOR_INIT_STMT (t)); | |
9460 | RECUR (FOR_COND (t)); | |
9461 | RECUR (FOR_EXPR (t)); | |
9462 | s = d->continue_stmt; | |
0fb7aa20 | 9463 | b = d->break_stmt; |
c7a53bdb JJ |
9464 | RECUR (FOR_BODY (t)); |
9465 | d->continue_stmt = s; | |
0fb7aa20 | 9466 | d->break_stmt = b; |
c7a53bdb JJ |
9467 | break; |
9468 | ||
9469 | case RANGE_FOR_STMT: | |
9470 | *walk_subtrees = 0; | |
9471 | RECUR (RANGE_FOR_EXPR (t)); | |
9472 | s = d->continue_stmt; | |
0fb7aa20 | 9473 | b = d->break_stmt; |
c7a53bdb JJ |
9474 | RECUR (RANGE_FOR_BODY (t)); |
9475 | d->continue_stmt = s; | |
0fb7aa20 JJ |
9476 | d->break_stmt = b; |
9477 | break; | |
9478 | ||
9479 | case SWITCH_STMT: | |
9480 | *walk_subtrees = 0; | |
9481 | RECUR (SWITCH_STMT_COND (t)); | |
9482 | b = d->break_stmt; | |
9483 | RECUR (SWITCH_STMT_BODY (t)); | |
9484 | d->break_stmt = b; | |
c7a53bdb JJ |
9485 | break; |
9486 | #undef RECUR | |
9487 | ||
9488 | case STATEMENT_LIST: | |
9489 | case CONSTRUCTOR: | |
9490 | break; | |
9491 | ||
9492 | default: | |
9493 | if (!EXPR_P (t)) | |
9494 | *walk_subtrees = 0; | |
9495 | break; | |
9496 | } | |
9497 | ||
9498 | return NULL_TREE; | |
9499 | } | |
9500 | ||
2d76680f PC |
9501 | /* Return true if T denotes a potentially constant expression. Issue |
9502 | diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true, | |
a0ab7ccd JM |
9503 | an lvalue-rvalue conversion is implied. If NOW is true, we want to |
9504 | consider the expression in the current context, independent of constexpr | |
c85f8dbb MP |
9505 | substitution. If FUNDEF_P is true, we're checking a constexpr function body |
9506 | and hard errors should not be reported by constexpr_error. | |
2d76680f PC |
9507 | |
9508 | C++0x [expr.const] used to say | |
9509 | ||
9510 | 6 An expression is a potential constant expression if it is | |
9511 | a constant expression where all occurrences of function | |
9512 | parameters are replaced by arbitrary constant expressions | |
9513 | of the appropriate type. | |
9514 | ||
9515 | 2 A conditional expression is a constant expression unless it | |
9516 | involves one of the following as a potentially evaluated | |
9517 | subexpression (3.2), but subexpressions of logical AND (5.14), | |
9518 | logical OR (5.15), and conditional (5.16) operations that are | |
9519 | not evaluated are not considered. */ | |
9520 | ||
9521 | static bool | |
a0ab7ccd | 9522 | potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, |
c85f8dbb MP |
9523 | bool fundef_p, tsubst_flags_t flags, |
9524 | tree *jump_target) | |
2d76680f | 9525 | { |
a0ab7ccd | 9526 | #define RECUR(T,RV) \ |
c85f8dbb MP |
9527 | potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \ |
9528 | jump_target) | |
a0ab7ccd | 9529 | |
2d76680f PC |
9530 | enum { any = false, rval = true }; |
9531 | int i; | |
9532 | tree tmp; | |
9533 | ||
9534 | if (t == error_mark_node) | |
9535 | return false; | |
9536 | if (t == NULL_TREE) | |
9537 | return true; | |
f9d0ca40 | 9538 | location_t loc = cp_expr_loc_or_input_loc (t); |
3075affd JM |
9539 | |
9540 | if (*jump_target) | |
9541 | /* If we are jumping, ignore everything. This is simpler than the | |
9542 | cxx_eval_constant_expression handling because we only need to be | |
9543 | conservatively correct, and we don't necessarily have a constant value | |
9544 | available, so we don't bother with switch tracking. */ | |
9545 | return true; | |
9546 | ||
4c718551 JM |
9547 | if (TREE_THIS_VOLATILE (t) && want_rval |
9548 | && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t))) | |
2d76680f PC |
9549 | { |
9550 | if (flags & tf_error) | |
c85f8dbb MP |
9551 | constexpr_error (loc, fundef_p, "lvalue-to-rvalue conversion of " |
9552 | "a volatile lvalue %qE with type %qT", t, | |
9553 | TREE_TYPE (t)); | |
2d76680f PC |
9554 | return false; |
9555 | } | |
9556 | if (CONSTANT_CLASS_P (t)) | |
9557 | return true; | |
7dc2b4a2 JM |
9558 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED) |
9559 | && TREE_TYPE (t) == error_mark_node) | |
9560 | return false; | |
2d76680f PC |
9561 | |
9562 | switch (TREE_CODE (t)) | |
9563 | { | |
9564 | case FUNCTION_DECL: | |
9565 | case BASELINK: | |
9566 | case TEMPLATE_DECL: | |
9567 | case OVERLOAD: | |
9568 | case TEMPLATE_ID_EXPR: | |
9569 | case LABEL_DECL: | |
60813a46 | 9570 | case CASE_LABEL_EXPR: |
81012684 | 9571 | case PREDICT_EXPR: |
2d76680f PC |
9572 | case CONST_DECL: |
9573 | case SIZEOF_EXPR: | |
9574 | case ALIGNOF_EXPR: | |
9575 | case OFFSETOF_EXPR: | |
9576 | case NOEXCEPT_EXPR: | |
9577 | case TEMPLATE_PARM_INDEX: | |
9578 | case TRAIT_EXPR: | |
9579 | case IDENTIFIER_NODE: | |
9580 | case USERDEF_LITERAL: | |
9581 | /* We can see a FIELD_DECL in a pointer-to-member expression. */ | |
9582 | case FIELD_DECL: | |
cda0a029 | 9583 | case RESULT_DECL: |
2d76680f | 9584 | case USING_DECL: |
60813a46 | 9585 | case USING_STMT: |
3e605b20 | 9586 | case PLACEHOLDER_EXPR: |
971e17ff | 9587 | case REQUIRES_EXPR: |
98e5a19a | 9588 | case STATIC_ASSERT: |
96a95ac1 | 9589 | case DEBUG_BEGIN_STMT: |
2d76680f PC |
9590 | return true; |
9591 | ||
3075affd JM |
9592 | case RETURN_EXPR: |
9593 | if (!RECUR (TREE_OPERAND (t, 0), any)) | |
9594 | return false; | |
9595 | /* FALLTHROUGH */ | |
9596 | ||
9597 | case BREAK_STMT: | |
9598 | case CONTINUE_STMT: | |
9599 | *jump_target = t; | |
9600 | return true; | |
9601 | ||
a0ab7ccd | 9602 | case PARM_DECL: |
8fda2c27 | 9603 | if (now && want_rval) |
a0ab7ccd | 9604 | { |
8fda2c27 | 9605 | tree type = TREE_TYPE (t); |
e36d1994 MP |
9606 | if (dependent_type_p (type) |
9607 | || !COMPLETE_TYPE_P (processing_template_decl | |
9608 | ? type : complete_type (type)) | |
8fda2c27 JM |
9609 | || is_really_empty_class (type, /*ignore_vptr*/false)) |
9610 | /* An empty class has no data to read. */ | |
9611 | return true; | |
a0ab7ccd | 9612 | if (flags & tf_error) |
c85f8dbb MP |
9613 | constexpr_error (input_location, fundef_p, |
9614 | "%qE is not a constant expression", t); | |
a0ab7ccd JM |
9615 | return false; |
9616 | } | |
9617 | return true; | |
9618 | ||
2d76680f PC |
9619 | case AGGR_INIT_EXPR: |
9620 | case CALL_EXPR: | |
9621 | /* -- an invocation of a function other than a constexpr function | |
9622 | or a constexpr constructor. */ | |
9623 | { | |
9624 | tree fun = get_function_named_in_call (t); | |
9625 | const int nargs = call_expr_nargs (t); | |
9626 | i = 0; | |
9627 | ||
60813a46 JM |
9628 | if (fun == NULL_TREE) |
9629 | { | |
44a845ca MS |
9630 | /* Reset to allow the function to continue past the end |
9631 | of the block below. Otherwise return early. */ | |
9632 | bool bail = true; | |
9633 | ||
35228ac7 JJ |
9634 | if (TREE_CODE (t) == CALL_EXPR |
9635 | && CALL_EXPR_FN (t) == NULL_TREE) | |
9636 | switch (CALL_EXPR_IFN (t)) | |
9637 | { | |
9638 | /* These should be ignored, they are optimized away from | |
9639 | constexpr functions. */ | |
9640 | case IFN_UBSAN_NULL: | |
9641 | case IFN_UBSAN_BOUNDS: | |
9642 | case IFN_UBSAN_VPTR: | |
81fea426 | 9643 | case IFN_FALLTHROUGH: |
08b51bad | 9644 | case IFN_ASSUME: |
35228ac7 | 9645 | return true; |
44a845ca MS |
9646 | |
9647 | case IFN_ADD_OVERFLOW: | |
9648 | case IFN_SUB_OVERFLOW: | |
9649 | case IFN_MUL_OVERFLOW: | |
e16f1cc7 | 9650 | case IFN_LAUNDER: |
d8fcab68 | 9651 | case IFN_VEC_CONVERT: |
44a845ca | 9652 | bail = false; |
d8fcab68 | 9653 | break; |
44a845ca | 9654 | |
35228ac7 JJ |
9655 | default: |
9656 | break; | |
9657 | } | |
44a845ca MS |
9658 | |
9659 | if (bail) | |
9660 | { | |
9661 | /* fold_call_expr can't do anything with IFN calls. */ | |
9662 | if (flags & tf_error) | |
c85f8dbb MP |
9663 | constexpr_error (loc, fundef_p, |
9664 | "call to internal function %qE", t); | |
44a845ca MS |
9665 | return false; |
9666 | } | |
60813a46 | 9667 | } |
44a845ca MS |
9668 | |
9669 | if (fun && is_overloaded_fn (fun)) | |
2d76680f | 9670 | { |
7f4840dd PP |
9671 | if (!RECUR (fun, true)) |
9672 | return false; | |
9673 | fun = get_fns (fun); | |
9674 | ||
2d76680f PC |
9675 | if (TREE_CODE (fun) == FUNCTION_DECL) |
9676 | { | |
9677 | if (builtin_valid_in_constant_expr_p (fun)) | |
9678 | return true; | |
87c2080b | 9679 | if (!maybe_constexpr_fn (fun) |
2d76680f PC |
9680 | /* Allow any built-in function; if the expansion |
9681 | isn't constant, we'll deal with that then. */ | |
8e007055 | 9682 | && !fndecl_built_in_p (fun) |
b04445d4 | 9683 | /* In C++20, replaceable global allocation functions |
8e007055 | 9684 | are constant expressions. */ |
8412b939 JJ |
9685 | && (!cxx_replaceable_global_alloc_fn (fun) |
9686 | || TREE_CODE (t) != CALL_EXPR | |
9687 | || (!CALL_FROM_NEW_OR_DELETE_P (t) | |
9688 | && (current_function_decl == NULL_TREE | |
9689 | || !is_std_allocator_allocate | |
9690 | (current_function_decl)))) | |
cf650568 JJ |
9691 | /* Allow placement new in std::construct_at. */ |
9692 | && (!cxx_placement_new_fn (fun) | |
8412b939 | 9693 | || TREE_CODE (t) != CALL_EXPR |
cf650568 | 9694 | || current_function_decl == NULL_TREE |
22edf943 MP |
9695 | || !is_std_construct_at (current_function_decl)) |
9696 | && !cxx_dynamic_cast_fn_p (fun)) | |
2d76680f | 9697 | { |
c85f8dbb MP |
9698 | if ((flags & tf_error) |
9699 | && constexpr_error (loc, fundef_p, | |
9700 | "call to non-%<constexpr%> " | |
9701 | "function %qD", fun)) | |
9702 | explain_invalid_constexpr_fn (fun); | |
2d76680f PC |
9703 | return false; |
9704 | } | |
2d76680f | 9705 | } |
7f4840dd PP |
9706 | |
9707 | fun = OVL_FIRST (fun); | |
2d76680f PC |
9708 | /* Skip initial arguments to base constructors. */ |
9709 | if (DECL_BASE_CONSTRUCTOR_P (fun)) | |
9710 | i = num_artificial_parms_for (fun); | |
2d76680f | 9711 | } |
44a845ca | 9712 | else if (fun) |
2d76680f | 9713 | { |
0077c0fb PP |
9714 | if (TREE_TYPE (fun) |
9715 | && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun))) | |
9716 | want_rval = rval; | |
9717 | else | |
9718 | want_rval = any; | |
9719 | if (RECUR (fun, want_rval)) | |
e09bc034 MP |
9720 | /* Might end up being a constant function pointer. But it |
9721 | could also be a function object with constexpr op(), so | |
9722 | we pass 'any' so that the underlying VAR_DECL is deemed | |
9723 | as potentially-constant even though it wasn't declared | |
9724 | constexpr. */; | |
2d76680f PC |
9725 | else |
9726 | return false; | |
9727 | } | |
9728 | for (; i < nargs; ++i) | |
9729 | { | |
9730 | tree x = get_nth_callarg (t, i); | |
c3c29ba5 JM |
9731 | /* In a template, reference arguments haven't been converted to |
9732 | REFERENCE_TYPE and we might not even know if the parameter | |
9733 | is a reference, so accept lvalue constants too. */ | |
9734 | bool rv = processing_template_decl ? any : rval; | |
a0ab7ccd JM |
9735 | /* Don't require an immediately constant value, as constexpr |
9736 | substitution might not use the value of the argument. */ | |
9737 | bool sub_now = false; | |
9738 | if (!potential_constant_expression_1 (x, rv, strict, | |
c85f8dbb MP |
9739 | sub_now, fundef_p, flags, |
9740 | jump_target)) | |
2d76680f PC |
9741 | return false; |
9742 | } | |
9743 | return true; | |
9744 | } | |
9745 | ||
9746 | case NON_LVALUE_EXPR: | |
9747 | /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to | |
9748 | -- an lvalue of integral type that refers to a non-volatile | |
9749 | const variable or static data member initialized with | |
9750 | constant expressions, or | |
9751 | ||
9752 | -- an lvalue of literal type that refers to non-volatile | |
9753 | object defined with constexpr, or that refers to a | |
9754 | sub-object of such an object; */ | |
69eb4fde | 9755 | return RECUR (TREE_OPERAND (t, 0), rval); |
2d76680f | 9756 | |
98e34113 JJ |
9757 | case EXCESS_PRECISION_EXPR: |
9758 | return RECUR (TREE_OPERAND (t, 0), rval); | |
9759 | ||
2d76680f | 9760 | case VAR_DECL: |
70f40fea | 9761 | if (DECL_HAS_VALUE_EXPR_P (t)) |
c1051bf7 | 9762 | { |
68ad1bf7 | 9763 | if (now && is_normal_capture_proxy (t)) |
c1051bf7 JM |
9764 | { |
9765 | /* -- in a lambda-expression, a reference to this or to a | |
9766 | variable with automatic storage duration defined outside that | |
9767 | lambda-expression, where the reference would be an | |
9768 | odr-use. */ | |
04b89192 JM |
9769 | |
9770 | if (want_rval) | |
9771 | /* Since we're doing an lvalue-rvalue conversion, this might | |
9772 | not be an odr-use, so evaluate the variable directly. */ | |
9773 | return RECUR (DECL_CAPTURED_VARIABLE (t), rval); | |
9774 | ||
c1051bf7 JM |
9775 | if (flags & tf_error) |
9776 | { | |
9777 | tree cap = DECL_CAPTURED_VARIABLE (t); | |
4f870007 | 9778 | auto_diagnostic_group d; |
c85f8dbb MP |
9779 | if (constexpr_error (input_location, fundef_p, |
9780 | "lambda capture of %qE is not a " | |
9781 | "constant expression", cap) | |
9782 | && decl_constant_var_p (cap)) | |
c1051bf7 JM |
9783 | inform (input_location, "because it is used as a glvalue"); |
9784 | } | |
9785 | return false; | |
9786 | } | |
c2856cee MP |
9787 | /* Treat __PRETTY_FUNCTION__ inside a template function as |
9788 | potentially-constant. */ | |
9789 | else if (DECL_PRETTY_FUNCTION_P (t) | |
9790 | && DECL_VALUE_EXPR (t) == error_mark_node) | |
9791 | return true; | |
c1051bf7 JM |
9792 | return RECUR (DECL_VALUE_EXPR (t), rval); |
9793 | } | |
69eb4fde | 9794 | if (want_rval |
6665a857 | 9795 | && (now || !var_in_maybe_constexpr_fn (t)) |
7dc2b4a2 | 9796 | && !type_dependent_expression_p (t) |
4b691b13 | 9797 | && !decl_maybe_constant_var_p (t) |
69eb4fde JM |
9798 | && (strict |
9799 | || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t)) | |
4b691b13 JM |
9800 | || (DECL_INITIAL (t) |
9801 | && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))) | |
7dc2b4a2 | 9802 | && COMPLETE_TYPE_P (TREE_TYPE (t)) |
dbcd32f8 | 9803 | && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
2d76680f | 9804 | { |
c85f8dbb MP |
9805 | if (flags & tf_error) |
9806 | non_const_var_error (loc, t, fundef_p); | |
2d76680f PC |
9807 | return false; |
9808 | } | |
9809 | return true; | |
9810 | ||
9811 | case NOP_EXPR: | |
ed3ea9f2 JJ |
9812 | if (REINTERPRET_CAST_P (t)) |
9813 | { | |
9814 | if (flags & tf_error) | |
c85f8dbb MP |
9815 | constexpr_error (loc, fundef_p, "%<reinterpret_cast%> is not a " |
9816 | "constant expression"); | |
ed3ea9f2 JJ |
9817 | return false; |
9818 | } | |
9819 | /* FALLTHRU */ | |
2d76680f PC |
9820 | case CONVERT_EXPR: |
9821 | case VIEW_CONVERT_EXPR: | |
9822 | /* -- a reinterpret_cast. FIXME not implemented, and this rule | |
9823 | may change to something more specific to type-punning (DR 1312). */ | |
9824 | { | |
9825 | tree from = TREE_OPERAND (t, 0); | |
dfd7fdca | 9826 | if (location_wrapper_p (t)) |
73d9b0e5 JM |
9827 | { |
9828 | iloc_sentinel ils = loc; | |
9829 | return (RECUR (from, want_rval)); | |
9830 | } | |
dfd7fdca | 9831 | if (INDIRECT_TYPE_P (TREE_TYPE (t))) |
2d76680f | 9832 | { |
dfd7fdca DM |
9833 | STRIP_ANY_LOCATION_WRAPPER (from); |
9834 | if (TREE_CODE (from) == INTEGER_CST | |
9835 | && !integer_zerop (from)) | |
9836 | { | |
9837 | if (flags & tf_error) | |
c85f8dbb MP |
9838 | constexpr_error (loc, fundef_p, |
9839 | "%<reinterpret_cast%> from integer to " | |
9840 | "pointer"); | |
dfd7fdca DM |
9841 | return false; |
9842 | } | |
2d76680f | 9843 | } |
69eb4fde | 9844 | return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR)); |
2d76680f PC |
9845 | } |
9846 | ||
be845b04 JJ |
9847 | case ADDRESSOF_EXPR: |
9848 | /* This is like ADDR_EXPR, except it won't form pointer-to-member. */ | |
9849 | t = TREE_OPERAND (t, 0); | |
9850 | goto handle_addr_expr; | |
9851 | ||
2d76680f PC |
9852 | case ADDR_EXPR: |
9853 | /* -- a unary operator & that is applied to an lvalue that | |
9854 | designates an object with thread or automatic storage | |
9855 | duration; */ | |
9856 | t = TREE_OPERAND (t, 0); | |
9857 | ||
9858 | if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t)) | |
9859 | /* A pointer-to-member constant. */ | |
9860 | return true; | |
9861 | ||
be845b04 | 9862 | handle_addr_expr: |
2d76680f PC |
9863 | #if 0 |
9864 | /* FIXME adjust when issue 1197 is fully resolved. For now don't do | |
9865 | any checking here, as we might dereference the pointer later. If | |
9866 | we remove this code, also remove check_automatic_or_tls. */ | |
9867 | i = check_automatic_or_tls (t); | |
9868 | if (i == ck_ok) | |
9869 | return true; | |
9870 | if (i == ck_bad) | |
9871 | { | |
9872 | if (flags & tf_error) | |
9873 | error ("address-of an object %qE with thread local or " | |
9874 | "automatic storage is not a constant expression", t); | |
9875 | return false; | |
9876 | } | |
9877 | #endif | |
69eb4fde | 9878 | return RECUR (t, any); |
2d76680f PC |
9879 | |
9880 | case COMPONENT_REF: | |
2d76680f PC |
9881 | case ARROW_EXPR: |
9882 | case OFFSET_REF: | |
9883 | /* -- a class member access unless its postfix-expression is | |
9884 | of literal type or of pointer to literal type. */ | |
9885 | /* This test would be redundant, as it follows from the | |
9886 | postfix-expression being a potential constant expression. */ | |
19dedccf JM |
9887 | if (type_unknown_p (t)) |
9888 | return true; | |
8fda2c27 JM |
9889 | if (is_overloaded_fn (t)) |
9890 | /* In a template, a COMPONENT_REF of a function expresses ob.fn(), | |
9891 | which uses ob as an lvalue. */ | |
9892 | want_rval = false; | |
9893 | gcc_fallthrough (); | |
9894 | ||
9895 | case REALPART_EXPR: | |
9896 | case IMAGPART_EXPR: | |
9897 | case BIT_FIELD_REF: | |
69eb4fde | 9898 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
2d76680f PC |
9899 | |
9900 | case EXPR_PACK_EXPANSION: | |
69eb4fde | 9901 | return RECUR (PACK_EXPANSION_PATTERN (t), want_rval); |
2d76680f PC |
9902 | |
9903 | case INDIRECT_REF: | |
9904 | { | |
9905 | tree x = TREE_OPERAND (t, 0); | |
9906 | STRIP_NOPS (x); | |
948d0d91 | 9907 | if (is_this_parameter (x) && !is_capture_proxy (x)) |
2d76680f | 9908 | { |
6665a857 | 9909 | if (now || !var_in_maybe_constexpr_fn (x)) |
2d76680f PC |
9910 | { |
9911 | if (flags & tf_error) | |
c85f8dbb MP |
9912 | constexpr_error (loc, fundef_p, "use of %<this%> in a " |
9913 | "constant expression"); | |
2d76680f PC |
9914 | return false; |
9915 | } | |
2d76680f PC |
9916 | return true; |
9917 | } | |
69eb4fde | 9918 | return RECUR (x, rval); |
2d76680f PC |
9919 | } |
9920 | ||
60813a46 | 9921 | case STATEMENT_LIST: |
0f54cc9c JM |
9922 | for (tree stmt : tsi_range (t)) |
9923 | if (!RECUR (stmt, any)) | |
9924 | return false; | |
9925 | return true; | |
60813a46 JM |
9926 | |
9927 | case MODIFY_EXPR: | |
9928 | if (cxx_dialect < cxx14) | |
9929 | goto fail; | |
69eb4fde | 9930 | if (!RECUR (TREE_OPERAND (t, 0), any)) |
60813a46 | 9931 | return false; |
8e007055 JJ |
9932 | /* Just ignore clobbers. */ |
9933 | if (TREE_CLOBBER_P (TREE_OPERAND (t, 1))) | |
9934 | return true; | |
69eb4fde | 9935 | if (!RECUR (TREE_OPERAND (t, 1), rval)) |
60813a46 JM |
9936 | return false; |
9937 | return true; | |
9938 | ||
9939 | case MODOP_EXPR: | |
9940 | if (cxx_dialect < cxx14) | |
9941 | goto fail; | |
69eb4fde | 9942 | if (!RECUR (TREE_OPERAND (t, 0), rval)) |
60813a46 | 9943 | return false; |
69eb4fde | 9944 | if (!RECUR (TREE_OPERAND (t, 2), rval)) |
60813a46 JM |
9945 | return false; |
9946 | return true; | |
9947 | ||
60813a46 | 9948 | case DO_STMT: |
69eb4fde | 9949 | if (!RECUR (DO_COND (t), rval)) |
60813a46 | 9950 | return false; |
69eb4fde | 9951 | if (!RECUR (DO_BODY (t), any)) |
60813a46 | 9952 | return false; |
3075affd JM |
9953 | if (breaks (jump_target) || continues (jump_target)) |
9954 | *jump_target = NULL_TREE; | |
60813a46 JM |
9955 | return true; |
9956 | ||
9957 | case FOR_STMT: | |
69eb4fde | 9958 | if (!RECUR (FOR_INIT_STMT (t), any)) |
60813a46 | 9959 | return false; |
8e9558f0 MP |
9960 | tmp = FOR_COND (t); |
9961 | if (!RECUR (tmp, rval)) | |
60813a46 | 9962 | return false; |
8e9558f0 MP |
9963 | if (tmp) |
9964 | { | |
9965 | if (!processing_template_decl) | |
9966 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
a385474c MP |
9967 | /* If we couldn't evaluate the condition, it might not ever be |
9968 | true. */ | |
9969 | if (!integer_onep (tmp)) | |
0fb7aa20 JJ |
9970 | { |
9971 | /* Before returning true, check if the for body can contain | |
9972 | a return. */ | |
9973 | hash_set<tree> pset; | |
9974 | check_for_return_continue_data data = { &pset, NULL_TREE, | |
9975 | NULL_TREE }; | |
9976 | if (tree ret_expr | |
9977 | = cp_walk_tree (&FOR_BODY (t), check_for_return_continue, | |
9978 | &data, &pset)) | |
9979 | *jump_target = ret_expr; | |
9980 | return true; | |
9981 | } | |
8e9558f0 | 9982 | } |
69eb4fde | 9983 | if (!RECUR (FOR_EXPR (t), any)) |
60813a46 | 9984 | return false; |
69eb4fde | 9985 | if (!RECUR (FOR_BODY (t), any)) |
60813a46 | 9986 | return false; |
3075affd JM |
9987 | if (breaks (jump_target) || continues (jump_target)) |
9988 | *jump_target = NULL_TREE; | |
60813a46 JM |
9989 | return true; |
9990 | ||
98e5a19a | 9991 | case RANGE_FOR_STMT: |
8112667c MP |
9992 | if (!RECUR (RANGE_FOR_INIT_STMT (t), any)) |
9993 | return false; | |
98e5a19a JM |
9994 | if (!RECUR (RANGE_FOR_EXPR (t), any)) |
9995 | return false; | |
9996 | if (!RECUR (RANGE_FOR_BODY (t), any)) | |
9997 | return false; | |
3075affd JM |
9998 | if (breaks (jump_target) || continues (jump_target)) |
9999 | *jump_target = NULL_TREE; | |
98e5a19a JM |
10000 | return true; |
10001 | ||
60813a46 | 10002 | case WHILE_STMT: |
8e9558f0 MP |
10003 | tmp = WHILE_COND (t); |
10004 | if (!RECUR (tmp, rval)) | |
60813a46 | 10005 | return false; |
8e9558f0 MP |
10006 | if (!processing_template_decl) |
10007 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
a385474c MP |
10008 | /* If we couldn't evaluate the condition, it might not ever be true. */ |
10009 | if (!integer_onep (tmp)) | |
0fb7aa20 JJ |
10010 | { |
10011 | /* Before returning true, check if the while body can contain | |
10012 | a return. */ | |
10013 | hash_set<tree> pset; | |
10014 | check_for_return_continue_data data = { &pset, NULL_TREE, | |
10015 | NULL_TREE }; | |
10016 | if (tree ret_expr | |
10017 | = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue, | |
10018 | &data, &pset)) | |
10019 | *jump_target = ret_expr; | |
10020 | return true; | |
10021 | } | |
69eb4fde | 10022 | if (!RECUR (WHILE_BODY (t), any)) |
60813a46 | 10023 | return false; |
3075affd JM |
10024 | if (breaks (jump_target) || continues (jump_target)) |
10025 | *jump_target = NULL_TREE; | |
60813a46 JM |
10026 | return true; |
10027 | ||
10028 | case SWITCH_STMT: | |
69eb4fde | 10029 | if (!RECUR (SWITCH_STMT_COND (t), rval)) |
60813a46 | 10030 | return false; |
ce965730 | 10031 | /* FIXME we don't check SWITCH_STMT_BODY currently, because even |
c7a53bdb JJ |
10032 | unreachable labels would be checked and it is enough if there is |
10033 | a single switch cond value for which it is a valid constant | |
10034 | expression. We need to check if there are any RETURN_EXPRs | |
10035 | or CONTINUE_STMTs inside of the body though, as in that case | |
10036 | we need to set *jump_target. */ | |
10037 | else | |
10038 | { | |
10039 | hash_set<tree> pset; | |
0fb7aa20 JJ |
10040 | check_for_return_continue_data data = { &pset, NULL_TREE, |
10041 | NULL_TREE }; | |
c7a53bdb JJ |
10042 | if (tree ret_expr |
10043 | = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue, | |
10044 | &data, &pset)) | |
10045 | /* The switch might return. */ | |
10046 | *jump_target = ret_expr; | |
10047 | else if (data.continue_stmt) | |
10048 | /* The switch can't return, but might continue. */ | |
10049 | *jump_target = data.continue_stmt; | |
10050 | } | |
60813a46 JM |
10051 | return true; |
10052 | ||
58611fb6 | 10053 | case STMT_EXPR: |
69eb4fde | 10054 | return RECUR (STMT_EXPR_STMT (t), rval); |
58611fb6 | 10055 | |
2d76680f | 10056 | case LAMBDA_EXPR: |
899ac3b8 JM |
10057 | if (cxx_dialect >= cxx17) |
10058 | /* In C++17 lambdas can be constexpr, don't give up yet. */ | |
10059 | return true; | |
10060 | else if (flags & tf_error) | |
c85f8dbb MP |
10061 | constexpr_error (loc, fundef_p, "lambda-expression is not a " |
10062 | "constant expression before C++17"); | |
899ac3b8 JM |
10063 | return false; |
10064 | ||
2d76680f PC |
10065 | case NEW_EXPR: |
10066 | case VEC_NEW_EXPR: | |
10067 | case DELETE_EXPR: | |
10068 | case VEC_DELETE_EXPR: | |
2423f654 MP |
10069 | if (cxx_dialect >= cxx20) |
10070 | /* In C++20, new-expressions are potentially constant. */ | |
10071 | return true; | |
10072 | else if (flags & tf_error) | |
c85f8dbb MP |
10073 | constexpr_error (loc, fundef_p, "new-expression is not a " |
10074 | "constant expression before C++20"); | |
2423f654 MP |
10075 | return false; |
10076 | ||
10077 | case DYNAMIC_CAST_EXPR: | |
10078 | case PSEUDO_DTOR_EXPR: | |
2d76680f | 10079 | case THROW_EXPR: |
e40b6fc7 JJ |
10080 | case OMP_PARALLEL: |
10081 | case OMP_TASK: | |
10082 | case OMP_FOR: | |
897064e2 | 10083 | case OMP_SIMD: |
e40b6fc7 JJ |
10084 | case OMP_DISTRIBUTE: |
10085 | case OMP_TASKLOOP: | |
d81ab49d | 10086 | case OMP_LOOP: |
e40b6fc7 JJ |
10087 | case OMP_TEAMS: |
10088 | case OMP_TARGET_DATA: | |
10089 | case OMP_TARGET: | |
10090 | case OMP_SECTIONS: | |
10091 | case OMP_ORDERED: | |
10092 | case OMP_CRITICAL: | |
10093 | case OMP_SINGLE: | |
bfc07059 JJ |
10094 | case OMP_SCAN: |
10095 | case OMP_SCOPE: | |
e40b6fc7 JJ |
10096 | case OMP_SECTION: |
10097 | case OMP_MASTER: | |
4b1e1434 | 10098 | case OMP_MASKED: |
e40b6fc7 JJ |
10099 | case OMP_TASKGROUP: |
10100 | case OMP_TARGET_UPDATE: | |
10101 | case OMP_TARGET_ENTER_DATA: | |
10102 | case OMP_TARGET_EXIT_DATA: | |
2d76680f PC |
10103 | case OMP_ATOMIC: |
10104 | case OMP_ATOMIC_READ: | |
10105 | case OMP_ATOMIC_CAPTURE_OLD: | |
10106 | case OMP_ATOMIC_CAPTURE_NEW: | |
28567c40 | 10107 | case OMP_DEPOBJ: |
e40b6fc7 JJ |
10108 | case OACC_PARALLEL: |
10109 | case OACC_KERNELS: | |
62aee289 | 10110 | case OACC_SERIAL: |
e40b6fc7 JJ |
10111 | case OACC_DATA: |
10112 | case OACC_HOST_DATA: | |
10113 | case OACC_LOOP: | |
10114 | case OACC_CACHE: | |
10115 | case OACC_DECLARE: | |
10116 | case OACC_ENTER_DATA: | |
10117 | case OACC_EXIT_DATA: | |
10118 | case OACC_UPDATE: | |
1413af02 | 10119 | case OMP_ARRAY_SECTION: |
2d76680f PC |
10120 | /* GCC internal stuff. */ |
10121 | case VA_ARG_EXPR: | |
2d76680f | 10122 | case TRANSACTION_EXPR: |
81b6a6c5 | 10123 | case AT_ENCODE_EXPR: |
60813a46 | 10124 | fail: |
2d76680f | 10125 | if (flags & tf_error) |
c85f8dbb MP |
10126 | constexpr_error (loc, fundef_p, "expression %qE is not a constant " |
10127 | "expression", t); | |
2d76680f PC |
10128 | return false; |
10129 | ||
529bc410 | 10130 | case ASM_EXPR: |
7c814975 | 10131 | if (flags & tf_error) |
c85f8dbb | 10132 | inline_asm_in_constexpr_error (loc, fundef_p); |
7c814975 | 10133 | return false; |
529bc410 | 10134 | |
bf8d8309 | 10135 | case OBJ_TYPE_REF: |
b04445d4 JM |
10136 | if (cxx_dialect >= cxx20) |
10137 | /* In C++20 virtual calls can be constexpr, don't give up yet. */ | |
bf8d8309 MP |
10138 | return true; |
10139 | else if (flags & tf_error) | |
c85f8dbb MP |
10140 | constexpr_error (loc, fundef_p, "virtual functions cannot be " |
10141 | "%<constexpr%> before C++20"); | |
bf8d8309 MP |
10142 | return false; |
10143 | ||
2d76680f | 10144 | case TYPEID_EXPR: |
66acfb80 MP |
10145 | /* In C++20, a typeid expression whose operand is of polymorphic |
10146 | class type can be constexpr. */ | |
2d76680f PC |
10147 | { |
10148 | tree e = TREE_OPERAND (t, 0); | |
b04445d4 | 10149 | if (cxx_dialect < cxx20 |
66acfb80 MP |
10150 | && strict |
10151 | && !TYPE_P (e) | |
10152 | && !type_dependent_expression_p (e) | |
2d76680f PC |
10153 | && TYPE_POLYMORPHIC_P (TREE_TYPE (e))) |
10154 | { | |
10155 | if (flags & tf_error) | |
c85f8dbb MP |
10156 | constexpr_error (loc, fundef_p, "%<typeid%> is not a " |
10157 | "constant expression because %qE is " | |
10158 | "of polymorphic type", e); | |
2d76680f PC |
10159 | return false; |
10160 | } | |
10161 | return true; | |
10162 | } | |
10163 | ||
1af4ebf5 | 10164 | case POINTER_DIFF_EXPR: |
2d76680f | 10165 | case MINUS_EXPR: |
2d76680f PC |
10166 | want_rval = true; |
10167 | goto binary; | |
10168 | ||
10169 | case LT_EXPR: | |
10170 | case LE_EXPR: | |
10171 | case GT_EXPR: | |
10172 | case GE_EXPR: | |
10173 | case EQ_EXPR: | |
10174 | case NE_EXPR: | |
b7689b96 | 10175 | case SPACESHIP_EXPR: |
2d76680f PC |
10176 | want_rval = true; |
10177 | goto binary; | |
10178 | ||
60813a46 JM |
10179 | case PREINCREMENT_EXPR: |
10180 | case POSTINCREMENT_EXPR: | |
10181 | case PREDECREMENT_EXPR: | |
10182 | case POSTDECREMENT_EXPR: | |
10183 | if (cxx_dialect < cxx14) | |
10184 | goto fail; | |
10185 | goto unary; | |
10186 | ||
2d76680f PC |
10187 | case BIT_NOT_EXPR: |
10188 | /* A destructor. */ | |
10189 | if (TYPE_P (TREE_OPERAND (t, 0))) | |
10190 | return true; | |
191816a3 | 10191 | /* fall through. */ |
2d76680f | 10192 | |
2d76680f PC |
10193 | case CONJ_EXPR: |
10194 | case SAVE_EXPR: | |
10195 | case FIX_TRUNC_EXPR: | |
10196 | case FLOAT_EXPR: | |
10197 | case NEGATE_EXPR: | |
10198 | case ABS_EXPR: | |
e197e64e | 10199 | case ABSU_EXPR: |
2d76680f PC |
10200 | case TRUTH_NOT_EXPR: |
10201 | case FIXED_CONVERT_EXPR: | |
10202 | case UNARY_PLUS_EXPR: | |
4856a1f0 PC |
10203 | case UNARY_LEFT_FOLD_EXPR: |
10204 | case UNARY_RIGHT_FOLD_EXPR: | |
60813a46 | 10205 | unary: |
69eb4fde | 10206 | return RECUR (TREE_OPERAND (t, 0), rval); |
2d76680f PC |
10207 | |
10208 | case CAST_EXPR: | |
10209 | case CONST_CAST_EXPR: | |
10210 | case STATIC_CAST_EXPR: | |
10211 | case REINTERPRET_CAST_EXPR: | |
10212 | case IMPLICIT_CONV_EXPR: | |
5925f0ec | 10213 | if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t))) |
2d76680f PC |
10214 | /* In C++98, a conversion to non-integral type can't be part of a |
10215 | constant expression. */ | |
10216 | { | |
10217 | if (flags & tf_error) | |
c85f8dbb MP |
10218 | constexpr_error (loc, fundef_p, |
10219 | "cast to non-integral type %qT in a constant " | |
10220 | "expression", TREE_TYPE (t)); | |
2d76680f PC |
10221 | return false; |
10222 | } | |
fe0604d3 MP |
10223 | /* This might be a conversion from a class to a (potentially) literal |
10224 | type. Let's consider it potentially constant since the conversion | |
10225 | might be a constexpr user-defined conversion. */ | |
10226 | else if (cxx_dialect >= cxx11 | |
10227 | && (dependent_type_p (TREE_TYPE (t)) | |
10228 | || !COMPLETE_TYPE_P (TREE_TYPE (t)) | |
10229 | || literal_type_p (TREE_TYPE (t))) | |
10230 | && TREE_OPERAND (t, 0)) | |
10231 | { | |
10232 | tree type = TREE_TYPE (TREE_OPERAND (t, 0)); | |
10233 | /* If this is a dependent type, it could end up being a class | |
10234 | with conversions. */ | |
10235 | if (type == NULL_TREE || WILDCARD_TYPE_P (type)) | |
10236 | return true; | |
10237 | /* Or a non-dependent class which has conversions. */ | |
10238 | else if (CLASS_TYPE_P (type) | |
10239 | && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type))) | |
10240 | return true; | |
10241 | } | |
2d76680f | 10242 | |
69eb4fde | 10243 | return (RECUR (TREE_OPERAND (t, 0), |
9f613f06 | 10244 | !TYPE_REF_P (TREE_TYPE (t)))); |
2d76680f | 10245 | |
60813a46 | 10246 | case BIND_EXPR: |
69eb4fde | 10247 | return RECUR (BIND_EXPR_BODY (t), want_rval); |
60813a46 | 10248 | |
60813a46 JM |
10249 | case CLEANUP_POINT_EXPR: |
10250 | case MUST_NOT_THROW_EXPR: | |
10251 | case TRY_CATCH_EXPR: | |
769430b2 | 10252 | case TRY_BLOCK: |
60813a46 JM |
10253 | case EH_SPEC_BLOCK: |
10254 | case EXPR_STMT: | |
2d76680f | 10255 | case PAREN_EXPR: |
2d76680f | 10256 | /* For convenience. */ |
f937929e JM |
10257 | case LOOP_EXPR: |
10258 | case EXIT_EXPR: | |
69eb4fde | 10259 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
2d76680f | 10260 | |
98e5a19a JM |
10261 | case DECL_EXPR: |
10262 | tmp = DECL_EXPR_DECL (t); | |
32d16fe9 JJ |
10263 | if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp) |
10264 | && (processing_template_decl | |
10265 | ? !decl_maybe_constant_var_p (tmp) | |
10266 | : !decl_constant_var_p (tmp))) | |
98e5a19a | 10267 | { |
72124f48 | 10268 | if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp)) |
98e5a19a JM |
10269 | { |
10270 | if (flags & tf_error) | |
c85f8dbb MP |
10271 | constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p, |
10272 | "%qD defined %<thread_local%> in " | |
10273 | "%<constexpr%> context", tmp); | |
98e5a19a JM |
10274 | return false; |
10275 | } | |
8892d532 | 10276 | else if (TREE_STATIC (tmp)) |
98e5a19a JM |
10277 | { |
10278 | if (flags & tf_error) | |
c85f8dbb MP |
10279 | constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p, |
10280 | "%qD defined %<static%> in %<constexpr%> " | |
10281 | "context", tmp); | |
98e5a19a JM |
10282 | return false; |
10283 | } | |
3885527d PC |
10284 | else if (!check_for_uninitialized_const_var |
10285 | (tmp, /*constexpr_context_p=*/true, flags)) | |
10286 | return false; | |
98e5a19a | 10287 | } |
b222e725 JJ |
10288 | if (VAR_P (tmp)) |
10289 | return RECUR (DECL_INITIAL (tmp), want_rval); | |
10290 | return true; | |
98e5a19a | 10291 | |
769430b2 JM |
10292 | case TRY_FINALLY_EXPR: |
10293 | return (RECUR (TREE_OPERAND (t, 0), want_rval) | |
10294 | && RECUR (TREE_OPERAND (t, 1), any)); | |
10295 | ||
2d76680f | 10296 | case SCOPE_REF: |
69eb4fde | 10297 | return RECUR (TREE_OPERAND (t, 1), want_rval); |
2d76680f PC |
10298 | |
10299 | case TARGET_EXPR: | |
d3a9902e | 10300 | if (!TARGET_EXPR_DIRECT_INIT_P (t) |
8d46516a | 10301 | && !TARGET_EXPR_ELIDING_P (t) |
d3a9902e | 10302 | && !literal_type_p (TREE_TYPE (t))) |
2d76680f PC |
10303 | { |
10304 | if (flags & tf_error) | |
10305 | { | |
097f82ec | 10306 | auto_diagnostic_group d; |
c85f8dbb MP |
10307 | if (constexpr_error (loc, fundef_p, |
10308 | "temporary of non-literal type %qT in a " | |
10309 | "constant expression", TREE_TYPE (t))) | |
10310 | explain_non_literal_class (TREE_TYPE (t)); | |
2d76680f PC |
10311 | } |
10312 | return false; | |
10313 | } | |
191816a3 | 10314 | /* FALLTHRU */ |
2d76680f | 10315 | case INIT_EXPR: |
69eb4fde | 10316 | return RECUR (TREE_OPERAND (t, 1), rval); |
2d76680f PC |
10317 | |
10318 | case CONSTRUCTOR: | |
10319 | { | |
10320 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); | |
10321 | constructor_elt *ce; | |
10322 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
69eb4fde | 10323 | if (!RECUR (ce->value, want_rval)) |
2d76680f PC |
10324 | return false; |
10325 | return true; | |
10326 | } | |
10327 | ||
10328 | case TREE_LIST: | |
10329 | { | |
10330 | gcc_assert (TREE_PURPOSE (t) == NULL_TREE | |
10331 | || DECL_P (TREE_PURPOSE (t))); | |
69eb4fde | 10332 | if (!RECUR (TREE_VALUE (t), want_rval)) |
2d76680f PC |
10333 | return false; |
10334 | if (TREE_CHAIN (t) == NULL_TREE) | |
10335 | return true; | |
69eb4fde | 10336 | return RECUR (TREE_CHAIN (t), want_rval); |
2d76680f PC |
10337 | } |
10338 | ||
10339 | case TRUNC_DIV_EXPR: | |
10340 | case CEIL_DIV_EXPR: | |
10341 | case FLOOR_DIV_EXPR: | |
10342 | case ROUND_DIV_EXPR: | |
10343 | case TRUNC_MOD_EXPR: | |
10344 | case CEIL_MOD_EXPR: | |
10345 | case ROUND_MOD_EXPR: | |
10346 | { | |
10347 | tree denom = TREE_OPERAND (t, 1); | |
69eb4fde | 10348 | if (!RECUR (denom, rval)) |
2d76680f PC |
10349 | return false; |
10350 | /* We can't call cxx_eval_outermost_constant_expr on an expression | |
234bef96 | 10351 | that hasn't been through instantiate_non_dependent_expr yet. */ |
2d76680f PC |
10352 | if (!processing_template_decl) |
10353 | denom = cxx_eval_outermost_constant_expr (denom, true); | |
10354 | if (integer_zerop (denom)) | |
10355 | { | |
10356 | if (flags & tf_error) | |
c85f8dbb MP |
10357 | constexpr_error (input_location, fundef_p, |
10358 | "division by zero is not a constant expression"); | |
2d76680f PC |
10359 | return false; |
10360 | } | |
10361 | else | |
10362 | { | |
10363 | want_rval = true; | |
69eb4fde | 10364 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
2d76680f PC |
10365 | } |
10366 | } | |
10367 | ||
10368 | case COMPOUND_EXPR: | |
10369 | { | |
10370 | /* check_return_expr sometimes wraps a TARGET_EXPR in a | |
46fdced6 | 10371 | COMPOUND_EXPR; don't get confused. */ |
2d76680f PC |
10372 | tree op0 = TREE_OPERAND (t, 0); |
10373 | tree op1 = TREE_OPERAND (t, 1); | |
10374 | STRIP_NOPS (op1); | |
46fdced6 | 10375 | if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) |
69eb4fde | 10376 | return RECUR (op0, want_rval); |
2d76680f PC |
10377 | else |
10378 | goto binary; | |
10379 | } | |
10380 | ||
10381 | /* If the first operand is the non-short-circuit constant, look at | |
10382 | the second operand; otherwise we only care about the first one for | |
10383 | potentiality. */ | |
10384 | case TRUTH_AND_EXPR: | |
10385 | case TRUTH_ANDIF_EXPR: | |
10386 | tmp = boolean_true_node; | |
10387 | goto truth; | |
10388 | case TRUTH_OR_EXPR: | |
10389 | case TRUTH_ORIF_EXPR: | |
10390 | tmp = boolean_false_node; | |
10391 | truth: | |
10392 | { | |
9927ecbb PP |
10393 | tree op0 = TREE_OPERAND (t, 0); |
10394 | tree op1 = TREE_OPERAND (t, 1); | |
10395 | if (!RECUR (op0, rval)) | |
2d76680f | 10396 | return false; |
9927ecbb PP |
10397 | if (!(flags & tf_error) && RECUR (op1, rval)) |
10398 | /* When quiet, try to avoid expensive trial evaluation by first | |
10399 | checking potentiality of the second operand. */ | |
10400 | return true; | |
2d76680f | 10401 | if (!processing_template_decl) |
9927ecbb PP |
10402 | op0 = cxx_eval_outermost_constant_expr (op0, true); |
10403 | if (tree_int_cst_equal (op0, tmp)) | |
10404 | return (flags & tf_error) ? RECUR (op1, rval) : false; | |
2d76680f PC |
10405 | else |
10406 | return true; | |
10407 | } | |
10408 | ||
10409 | case PLUS_EXPR: | |
10410 | case MULT_EXPR: | |
10411 | case POINTER_PLUS_EXPR: | |
10412 | case RDIV_EXPR: | |
10413 | case EXACT_DIV_EXPR: | |
10414 | case MIN_EXPR: | |
10415 | case MAX_EXPR: | |
10416 | case LSHIFT_EXPR: | |
10417 | case RSHIFT_EXPR: | |
81fa6d73 JJ |
10418 | case LROTATE_EXPR: |
10419 | case RROTATE_EXPR: | |
2d76680f PC |
10420 | case BIT_IOR_EXPR: |
10421 | case BIT_XOR_EXPR: | |
10422 | case BIT_AND_EXPR: | |
10423 | case TRUTH_XOR_EXPR: | |
10424 | case UNORDERED_EXPR: | |
10425 | case ORDERED_EXPR: | |
10426 | case UNLT_EXPR: | |
10427 | case UNLE_EXPR: | |
10428 | case UNGT_EXPR: | |
10429 | case UNGE_EXPR: | |
10430 | case UNEQ_EXPR: | |
10431 | case LTGT_EXPR: | |
10432 | case RANGE_EXPR: | |
10433 | case COMPLEX_EXPR: | |
10434 | want_rval = true; | |
10435 | /* Fall through. */ | |
10436 | case ARRAY_REF: | |
10437 | case ARRAY_RANGE_REF: | |
10438 | case MEMBER_REF: | |
10439 | case DOTSTAR_EXPR: | |
41b38772 | 10440 | case MEM_REF: |
4856a1f0 PC |
10441 | case BINARY_LEFT_FOLD_EXPR: |
10442 | case BINARY_RIGHT_FOLD_EXPR: | |
2d76680f PC |
10443 | binary: |
10444 | for (i = 0; i < 2; ++i) | |
69eb4fde | 10445 | if (!RECUR (TREE_OPERAND (t, i), want_rval)) |
2d76680f PC |
10446 | return false; |
10447 | return true; | |
10448 | ||
2d76680f PC |
10449 | case VEC_PERM_EXPR: |
10450 | for (i = 0; i < 3; ++i) | |
69eb4fde | 10451 | if (!RECUR (TREE_OPERAND (t, i), true)) |
2d76680f PC |
10452 | return false; |
10453 | return true; | |
10454 | ||
10455 | case COND_EXPR: | |
b04445d4 | 10456 | if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20) |
d5bcd6d4 JM |
10457 | { |
10458 | if (flags & tf_error) | |
c85f8dbb MP |
10459 | constexpr_error (loc, fundef_p, "%<delete[]%> is not a " |
10460 | "constant expression"); | |
d5bcd6d4 JM |
10461 | return false; |
10462 | } | |
10463 | /* Fall through. */ | |
10464 | case IF_STMT: | |
2d76680f PC |
10465 | case VEC_COND_EXPR: |
10466 | /* If the condition is a known constant, we know which of the legs we | |
10467 | care about; otherwise we only require that the condition and | |
10468 | either of the legs be potentially constant. */ | |
10469 | tmp = TREE_OPERAND (t, 0); | |
69eb4fde | 10470 | if (!RECUR (tmp, rval)) |
2d76680f PC |
10471 | return false; |
10472 | if (!processing_template_decl) | |
10473 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
117c6426 JJ |
10474 | /* potential_constant_expression* isn't told if it is called for |
10475 | manifestly_const_eval or not, so for consteval if always | |
10476 | process both branches as if the condition is not a known | |
10477 | constant. */ | |
10478 | if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t)) | |
10479 | { | |
10480 | if (integer_zerop (tmp)) | |
10481 | return RECUR (TREE_OPERAND (t, 2), want_rval); | |
10482 | else if (TREE_CODE (tmp) == INTEGER_CST) | |
10483 | return RECUR (TREE_OPERAND (t, 1), want_rval); | |
10484 | } | |
0fb7aa20 | 10485 | tmp = *jump_target; |
2d76680f | 10486 | for (i = 1; i < 3; ++i) |
0fb7aa20 JJ |
10487 | { |
10488 | tree this_jump_target = tmp; | |
10489 | if (potential_constant_expression_1 (TREE_OPERAND (t, i), | |
c85f8dbb | 10490 | want_rval, strict, now, fundef_p, |
0fb7aa20 JJ |
10491 | tf_none, &this_jump_target)) |
10492 | { | |
10493 | if (returns (&this_jump_target)) | |
10494 | *jump_target = this_jump_target; | |
10495 | else if (!returns (jump_target)) | |
10496 | { | |
10497 | if (breaks (&this_jump_target) | |
10498 | || continues (&this_jump_target)) | |
10499 | *jump_target = this_jump_target; | |
10500 | if (i == 1) | |
10501 | { | |
10502 | /* If the then branch is potentially constant, but | |
10503 | does not return, check if the else branch | |
10504 | couldn't return, break or continue. */ | |
10505 | hash_set<tree> pset; | |
10506 | check_for_return_continue_data data = { &pset, NULL_TREE, | |
10507 | NULL_TREE }; | |
10508 | if (tree ret_expr | |
10509 | = cp_walk_tree (&TREE_OPERAND (t, 2), | |
10510 | check_for_return_continue, &data, | |
10511 | &pset)) | |
10512 | *jump_target = ret_expr; | |
10513 | else if (*jump_target == NULL_TREE) | |
10514 | { | |
10515 | if (data.continue_stmt) | |
10516 | *jump_target = data.continue_stmt; | |
10517 | else if (data.break_stmt) | |
10518 | *jump_target = data.break_stmt; | |
10519 | } | |
10520 | } | |
10521 | } | |
10522 | return true; | |
10523 | } | |
10524 | } | |
2d76680f | 10525 | if (flags & tf_error) |
ff465bd8 PP |
10526 | { |
10527 | if (TREE_CODE (t) == IF_STMT) | |
c85f8dbb MP |
10528 | constexpr_error (loc, fundef_p, "neither branch of %<if%> is a " |
10529 | "constant expression"); | |
ff465bd8 | 10530 | else |
c85f8dbb MP |
10531 | constexpr_error (loc, fundef_p, "expression %qE is not a " |
10532 | "constant expression", t); | |
ff465bd8 | 10533 | } |
2d76680f PC |
10534 | return false; |
10535 | ||
10536 | case VEC_INIT_EXPR: | |
10537 | if (VEC_INIT_EXPR_IS_CONSTEXPR (t)) | |
10538 | return true; | |
10539 | if (flags & tf_error) | |
10540 | { | |
c85f8dbb MP |
10541 | if (constexpr_error (loc, fundef_p, "non-constant array " |
10542 | "initialization")) | |
10543 | diagnose_non_constexpr_vec_init (t); | |
2d76680f PC |
10544 | } |
10545 | return false; | |
10546 | ||
133bc698 JM |
10547 | case TYPE_DECL: |
10548 | case TAG_DEFN: | |
10549 | /* We can see these in statement-expressions. */ | |
10550 | return true; | |
10551 | ||
baf9ebc8 | 10552 | case CLEANUP_STMT: |
1006c9d4 JJ |
10553 | if (!RECUR (CLEANUP_BODY (t), any)) |
10554 | return false; | |
10555 | if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any)) | |
10556 | return false; | |
10557 | return true; | |
10558 | ||
cda0a029 | 10559 | case EMPTY_CLASS_EXPR: |
46fdced6 | 10560 | return true; |
cda0a029 | 10561 | |
f937929e JM |
10562 | case GOTO_EXPR: |
10563 | { | |
10564 | tree *target = &TREE_OPERAND (t, 0); | |
02a981a8 JJ |
10565 | /* Gotos representing break, continue and cdtor return are OK. */ |
10566 | if (breaks (target) || continues (target) || returns (target)) | |
3075affd JM |
10567 | { |
10568 | *jump_target = *target; | |
10569 | return true; | |
10570 | } | |
ab3af181 | 10571 | if (flags & tf_error) |
c85f8dbb MP |
10572 | constexpr_error (loc, fundef_p, "%<goto%> is not a constant " |
10573 | "expression"); | |
ab3af181 | 10574 | return false; |
f937929e JM |
10575 | } |
10576 | ||
2efb237f JCI |
10577 | case ASSERTION_STMT: |
10578 | case PRECONDITION_STMT: | |
10579 | case POSTCONDITION_STMT: | |
10580 | if (!checked_contract_p (get_contract_semantic (t))) | |
10581 | return true; | |
10582 | return RECUR (CONTRACT_CONDITION (t), rval); | |
10583 | ||
6f20c42c MP |
10584 | case LABEL_EXPR: |
10585 | t = LABEL_EXPR_LABEL (t); | |
8892d532 | 10586 | if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23) |
6f20c42c MP |
10587 | return true; |
10588 | else if (flags & tf_error) | |
c85f8dbb MP |
10589 | constexpr_error (loc, fundef_p, "label definition in %<constexpr%> " |
10590 | "function only available with %<-std=c++2b%> or " | |
10591 | "%<-std=gnu++2b%>"); | |
6f20c42c MP |
10592 | return false; |
10593 | ||
98e09245 | 10594 | case ANNOTATE_EXPR: |
98e09245 JJ |
10595 | return RECUR (TREE_OPERAND (t, 0), rval); |
10596 | ||
896048cf JJ |
10597 | case BIT_CAST_EXPR: |
10598 | return RECUR (TREE_OPERAND (t, 0), rval); | |
10599 | ||
49789fd0 IS |
10600 | /* Coroutine await, yield and return expressions are not. */ |
10601 | case CO_AWAIT_EXPR: | |
10602 | case CO_YIELD_EXPR: | |
10603 | case CO_RETURN_EXPR: | |
10604 | return false; | |
10605 | ||
d4c470c3 PP |
10606 | case NONTYPE_ARGUMENT_PACK: |
10607 | { | |
10608 | tree args = ARGUMENT_PACK_ARGS (t); | |
10609 | int len = TREE_VEC_LENGTH (args); | |
10610 | for (int i = 0; i < len; ++i) | |
10611 | if (!RECUR (TREE_VEC_ELT (args, i), any)) | |
10612 | return false; | |
10613 | return true; | |
10614 | } | |
10615 | ||
2d76680f | 10616 | default: |
878cffbd | 10617 | if (objc_non_constant_expr_p (t)) |
2d76680f PC |
10618 | return false; |
10619 | ||
10620 | sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t))); | |
ab3af181 | 10621 | gcc_unreachable (); |
2d76680f PC |
10622 | return false; |
10623 | } | |
69eb4fde | 10624 | #undef RECUR |
2d76680f PC |
10625 | } |
10626 | ||
3075affd JM |
10627 | bool |
10628 | potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, | |
c85f8dbb | 10629 | bool fundef_p, tsubst_flags_t flags) |
3075affd | 10630 | { |
9927ecbb PP |
10631 | if (flags & tf_error) |
10632 | { | |
10633 | /* Check potentiality quietly first, as that could be performed more | |
10634 | efficiently in some cases (currently only for TRUTH_*_EXPR). If | |
10635 | that fails, replay the check noisily to give errors. */ | |
10636 | flags &= ~tf_error; | |
c85f8dbb MP |
10637 | if (potential_constant_expression_1 (t, want_rval, strict, now, fundef_p, |
10638 | flags)) | |
9927ecbb PP |
10639 | return true; |
10640 | flags |= tf_error; | |
10641 | } | |
10642 | ||
3075affd | 10643 | tree target = NULL_TREE; |
c85f8dbb | 10644 | return potential_constant_expression_1 (t, want_rval, strict, now, fundef_p, |
3075affd JM |
10645 | flags, &target); |
10646 | } | |
10647 | ||
2d76680f PC |
10648 | /* The main entry point to the above. */ |
10649 | ||
10650 | bool | |
10651 | potential_constant_expression (tree t) | |
10652 | { | |
c85f8dbb MP |
10653 | return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true, |
10654 | /*now*/false, /*fundef_p*/false, | |
10655 | tf_none); | |
2d76680f PC |
10656 | } |
10657 | ||
10658 | /* As above, but require a constant rvalue. */ | |
10659 | ||
10660 | bool | |
10661 | potential_rvalue_constant_expression (tree t) | |
10662 | { | |
c85f8dbb MP |
10663 | return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true, |
10664 | /*now*/false, /*fundef_p*/false, | |
10665 | tf_none); | |
2d76680f PC |
10666 | } |
10667 | ||
10668 | /* Like above, but complain about non-constant expressions. */ | |
10669 | ||
10670 | bool | |
10671 | require_potential_constant_expression (tree t) | |
10672 | { | |
c85f8dbb MP |
10673 | return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true, |
10674 | /*now*/false, /*fundef_p*/false, | |
d8cff23f | 10675 | tf_warning_or_error); |
2d76680f PC |
10676 | } |
10677 | ||
10678 | /* Cross product of the above. */ | |
10679 | ||
10680 | bool | |
10681 | require_potential_rvalue_constant_expression (tree t) | |
10682 | { | |
c85f8dbb MP |
10683 | return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true, |
10684 | /*now*/false, /*fundef_p*/false, | |
10685 | tf_warning_or_error); | |
10686 | } | |
10687 | ||
10688 | /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */ | |
10689 | ||
10690 | bool | |
10691 | require_potential_rvalue_constant_expression_fncheck (tree t) | |
10692 | { | |
10693 | return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true, | |
10694 | /*now*/false, /*fundef_p*/true, | |
d8cff23f MP |
10695 | tf_warning_or_error); |
10696 | } | |
10697 | ||
10698 | /* Like above, but don't consider PARM_DECL a potential_constant_expression. */ | |
10699 | ||
10700 | bool | |
10701 | require_rvalue_constant_expression (tree t) | |
10702 | { | |
c85f8dbb MP |
10703 | return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true, |
10704 | /*now*/true, /*fundef_p*/false, | |
d8cff23f | 10705 | tf_warning_or_error); |
a0ab7ccd JM |
10706 | } |
10707 | ||
10708 | /* Like potential_constant_expression, but don't consider possible constexpr | |
10709 | substitution of the current function. That is, PARM_DECL qualifies under | |
10710 | potential_constant_expression, but not here. | |
10711 | ||
10712 | This is basically what you can check when any actual constant values might | |
10713 | be value-dependent. */ | |
10714 | ||
10715 | bool | |
10716 | is_constant_expression (tree t) | |
10717 | { | |
c85f8dbb MP |
10718 | return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true, |
10719 | /*now*/true, /*fundef_p*/false, | |
10720 | tf_none); | |
a0ab7ccd JM |
10721 | } |
10722 | ||
beb019d3 JM |
10723 | /* As above, but expect an rvalue. */ |
10724 | ||
10725 | bool | |
10726 | is_rvalue_constant_expression (tree t) | |
10727 | { | |
c85f8dbb MP |
10728 | return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true, |
10729 | /*now*/true, /*fundef_p*/false, | |
10730 | tf_none); | |
beb019d3 JM |
10731 | } |
10732 | ||
a0ab7ccd JM |
10733 | /* Like above, but complain about non-constant expressions. */ |
10734 | ||
10735 | bool | |
10736 | require_constant_expression (tree t) | |
10737 | { | |
c85f8dbb MP |
10738 | return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true, |
10739 | /*now*/true, /*fundef_p*/false, | |
a0ab7ccd JM |
10740 | tf_warning_or_error); |
10741 | } | |
10742 | ||
10743 | /* Like is_constant_expression, but allow const variables that are not allowed | |
10744 | under constexpr rules. */ | |
10745 | ||
10746 | bool | |
10747 | is_static_init_expression (tree t) | |
10748 | { | |
c85f8dbb MP |
10749 | return potential_constant_expression_1 (t, /*want_rval*/false, |
10750 | /*strict*/false, /*now*/true, | |
10751 | /*fundef_p*/false, tf_none); | |
2d76680f PC |
10752 | } |
10753 | ||
eb07f187 JM |
10754 | /* Returns true if T is a potential constant expression that is not |
10755 | instantiation-dependent, and therefore a candidate for constant folding even | |
10756 | in a template. */ | |
10757 | ||
10758 | bool | |
a0ab7ccd | 10759 | is_nondependent_constant_expression (tree t) |
eb07f187 JM |
10760 | { |
10761 | return (!type_unknown_p (t) | |
a0ab7ccd | 10762 | && is_constant_expression (t) |
eb07f187 JM |
10763 | && !instantiation_dependent_expression_p (t)); |
10764 | } | |
10765 | ||
10766 | /* Returns true if T is a potential static initializer expression that is not | |
10767 | instantiation-dependent. */ | |
10768 | ||
10769 | bool | |
a0ab7ccd | 10770 | is_nondependent_static_init_expression (tree t) |
eb07f187 JM |
10771 | { |
10772 | return (!type_unknown_p (t) | |
a0ab7ccd | 10773 | && is_static_init_expression (t) |
eb07f187 JM |
10774 | && !instantiation_dependent_expression_p (t)); |
10775 | } | |
10776 | ||
87c2080b JM |
10777 | /* True iff FN is an implicitly constexpr function. */ |
10778 | ||
10779 | bool | |
10780 | decl_implicit_constexpr_p (tree fn) | |
10781 | { | |
10782 | if (!(flag_implicit_constexpr | |
10783 | && TREE_CODE (fn) == FUNCTION_DECL | |
10784 | && DECL_DECLARED_CONSTEXPR_P (fn))) | |
10785 | return false; | |
10786 | ||
10787 | if (DECL_CLONED_FUNCTION_P (fn)) | |
10788 | fn = DECL_CLONED_FUNCTION (fn); | |
10789 | ||
10790 | return (DECL_LANG_SPECIFIC (fn) | |
10791 | && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr); | |
10792 | } | |
10793 | ||
97f3003f JM |
10794 | /* Finalize constexpr processing after parsing. */ |
10795 | ||
10796 | void | |
10797 | fini_constexpr (void) | |
10798 | { | |
10799 | /* The contexpr call and fundef copies tables are no longer needed. */ | |
10800 | constexpr_call_table = NULL; | |
10801 | fundef_copies_table = NULL; | |
10802 | } | |
10803 | ||
2d76680f | 10804 | #include "gt-cp-constexpr.h" |