]>
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 | ||
83ffe9cd | 5 | Copyright (C) 1998-2023 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 | } |
87c2080b JM |
265 | else if (DECL_DESTRUCTOR_P (fun)) |
266 | { | |
267 | if (cxx_dialect < cxx20) | |
268 | { | |
269 | ret = false; | |
270 | if (complain) | |
271 | error_at (DECL_SOURCE_LOCATION (fun), | |
272 | "%<constexpr%> destructors only available" | |
273 | " with %<-std=c++20%> or %<-std=gnu++20%>"); | |
274 | } | |
275 | } | |
e1bea341 | 276 | else if (!DECL_CONSTRUCTOR_P (fun)) |
2d76680f PC |
277 | { |
278 | tree rettype = TREE_TYPE (TREE_TYPE (fun)); | |
279 | if (!literal_type_p (rettype)) | |
280 | { | |
281 | ret = false; | |
282 | if (complain) | |
283 | { | |
097f82ec | 284 | auto_diagnostic_group d; |
c85f8dbb MP |
285 | if (constexpr_error (input_location, /*constexpr_fundef_p*/true, |
286 | "invalid return type %qT of %<constexpr%> " | |
287 | "function %q+D", rettype, fun)) | |
288 | explain_non_literal_class (rettype); | |
2d76680f PC |
289 | } |
290 | } | |
291 | ||
54069e59 JM |
292 | /* C++14 DR 1684 removed this restriction. */ |
293 | if (cxx_dialect < cxx14 | |
294 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) | |
2d76680f PC |
295 | && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun))) |
296 | { | |
297 | ret = false; | |
097f82ec DM |
298 | if (complain) |
299 | { | |
300 | auto_diagnostic_group d; | |
301 | if (pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic, | |
302 | "enclosing class of %<constexpr%> non-static" | |
303 | " member function %q+#D is not a literal type", | |
304 | fun)) | |
305 | explain_non_literal_class (DECL_CONTEXT (fun)); | |
306 | } | |
2d76680f PC |
307 | } |
308 | } | |
309 | else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun))) | |
310 | { | |
311 | ret = false; | |
312 | if (complain) | |
313 | error ("%q#T has virtual base classes", DECL_CONTEXT (fun)); | |
314 | } | |
315 | ||
316 | return ret; | |
317 | } | |
318 | ||
319 | /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF | |
320 | for a member of an anonymous aggregate, INIT is the initializer for that | |
321 | member, and VEC_OUTER is the vector of constructor elements for the class | |
322 | whose constructor we are processing. Add the initializer to the vector | |
323 | and return true to indicate success. */ | |
324 | ||
325 | static bool | |
326 | build_anon_member_initialization (tree member, tree init, | |
327 | vec<constructor_elt, va_gc> **vec_outer) | |
328 | { | |
329 | /* MEMBER presents the relevant fields from the inside out, but we need | |
330 | to build up the initializer from the outside in so that we can reuse | |
331 | previously built CONSTRUCTORs if this is, say, the second field in an | |
332 | anonymous struct. So we use a vec as a stack. */ | |
333 | auto_vec<tree, 2> fields; | |
334 | do | |
335 | { | |
336 | fields.safe_push (TREE_OPERAND (member, 1)); | |
337 | member = TREE_OPERAND (member, 0); | |
338 | } | |
339 | while (ANON_AGGR_TYPE_P (TREE_TYPE (member)) | |
340 | && TREE_CODE (member) == COMPONENT_REF); | |
341 | ||
342 | /* VEC has the constructor elements vector for the context of FIELD. | |
343 | If FIELD is an anonymous aggregate, we will push inside it. */ | |
344 | vec<constructor_elt, va_gc> **vec = vec_outer; | |
345 | tree field; | |
346 | while (field = fields.pop(), | |
347 | ANON_AGGR_TYPE_P (TREE_TYPE (field))) | |
348 | { | |
349 | tree ctor; | |
350 | /* If there is already an outer constructor entry for the anonymous | |
351 | aggregate FIELD, use it; otherwise, insert one. */ | |
352 | if (vec_safe_is_empty (*vec) | |
353 | || (*vec)->last().index != field) | |
354 | { | |
355 | ctor = build_constructor (TREE_TYPE (field), NULL); | |
356 | CONSTRUCTOR_APPEND_ELT (*vec, field, ctor); | |
357 | } | |
358 | else | |
359 | ctor = (*vec)->last().value; | |
360 | vec = &CONSTRUCTOR_ELTS (ctor); | |
361 | } | |
362 | ||
363 | /* Now we're at the innermost field, the one that isn't an anonymous | |
364 | aggregate. Add its initializer to the CONSTRUCTOR and we're done. */ | |
365 | gcc_assert (fields.is_empty()); | |
366 | CONSTRUCTOR_APPEND_ELT (*vec, field, init); | |
367 | ||
368 | return true; | |
369 | } | |
370 | ||
371 | /* Subroutine of build_constexpr_constructor_member_initializers. | |
372 | The expression tree T represents a data member initialization | |
373 | in a (constexpr) constructor definition. Build a pairing of | |
374 | the data member with its initializer, and prepend that pair | |
375 | to the existing initialization pair INITS. */ | |
376 | ||
377 | static bool | |
378 | build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec) | |
379 | { | |
380 | tree member, init; | |
381 | if (TREE_CODE (t) == CLEANUP_POINT_EXPR) | |
382 | t = TREE_OPERAND (t, 0); | |
383 | if (TREE_CODE (t) == EXPR_STMT) | |
384 | t = TREE_OPERAND (t, 0); | |
385 | if (t == error_mark_node) | |
386 | return false; | |
387 | if (TREE_CODE (t) == STATEMENT_LIST) | |
388 | { | |
0f54cc9c JM |
389 | for (tree stmt : tsi_range (t)) |
390 | if (! build_data_member_initialization (stmt, vec)) | |
391 | return false; | |
2d76680f PC |
392 | return true; |
393 | } | |
394 | if (TREE_CODE (t) == CLEANUP_STMT) | |
395 | { | |
396 | /* We can't see a CLEANUP_STMT in a constructor for a literal class, | |
397 | but we can in a constexpr constructor for a non-literal class. Just | |
398 | ignore it; either all the initialization will be constant, in which | |
399 | case the cleanup can't run, or it can't be constexpr. | |
400 | Still recurse into CLEANUP_BODY. */ | |
401 | return build_data_member_initialization (CLEANUP_BODY (t), vec); | |
402 | } | |
403 | if (TREE_CODE (t) == CONVERT_EXPR) | |
404 | t = TREE_OPERAND (t, 0); | |
405 | if (TREE_CODE (t) == INIT_EXPR | |
60813a46 JM |
406 | /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only |
407 | use what this function builds for cx_check_missing_mem_inits, and | |
408 | assignment in the ctor body doesn't count. */ | |
409 | || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR)) | |
2d76680f PC |
410 | { |
411 | member = TREE_OPERAND (t, 0); | |
412 | init = break_out_target_exprs (TREE_OPERAND (t, 1)); | |
413 | } | |
414 | else if (TREE_CODE (t) == CALL_EXPR) | |
415 | { | |
60813a46 JM |
416 | tree fn = get_callee_fndecl (t); |
417 | if (!fn || !DECL_CONSTRUCTOR_P (fn)) | |
418 | /* We're only interested in calls to subobject constructors. */ | |
419 | return true; | |
2d76680f PC |
420 | member = CALL_EXPR_ARG (t, 0); |
421 | /* We don't use build_cplus_new here because it complains about | |
422 | abstract bases. Leaving the call unwrapped means that it has the | |
423 | wrong type, but cxx_eval_constant_expression doesn't care. */ | |
424 | init = break_out_target_exprs (t); | |
425 | } | |
426 | else if (TREE_CODE (t) == BIND_EXPR) | |
427 | return build_data_member_initialization (BIND_EXPR_BODY (t), vec); | |
2d76680f | 428 | else |
60813a46 JM |
429 | /* Don't add anything else to the CONSTRUCTOR. */ |
430 | return true; | |
2d76680f PC |
431 | if (INDIRECT_REF_P (member)) |
432 | member = TREE_OPERAND (member, 0); | |
433 | if (TREE_CODE (member) == NOP_EXPR) | |
434 | { | |
435 | tree op = member; | |
436 | STRIP_NOPS (op); | |
437 | if (TREE_CODE (op) == ADDR_EXPR) | |
438 | { | |
439 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
440 | (TREE_TYPE (TREE_TYPE (op)), | |
441 | TREE_TYPE (TREE_TYPE (member)))); | |
442 | /* Initializing a cv-qualified member; we need to look through | |
443 | the const_cast. */ | |
444 | member = op; | |
445 | } | |
446 | else if (op == current_class_ptr | |
447 | && (same_type_ignoring_top_level_qualifiers_p | |
448 | (TREE_TYPE (TREE_TYPE (member)), | |
449 | current_class_type))) | |
450 | /* Delegating constructor. */ | |
451 | member = op; | |
452 | else | |
453 | { | |
454 | /* This is an initializer for an empty base; keep it for now so | |
455 | we can check it in cxx_eval_bare_aggregate. */ | |
456 | gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member)))); | |
457 | } | |
458 | } | |
459 | if (TREE_CODE (member) == ADDR_EXPR) | |
460 | member = TREE_OPERAND (member, 0); | |
461 | if (TREE_CODE (member) == COMPONENT_REF) | |
462 | { | |
463 | tree aggr = TREE_OPERAND (member, 0); | |
8cb7aaa1 JM |
464 | if (TREE_CODE (aggr) == VAR_DECL) |
465 | /* Initializing a local variable, don't add anything. */ | |
466 | return true; | |
2d76680f PC |
467 | if (TREE_CODE (aggr) != COMPONENT_REF) |
468 | /* Normal member initialization. */ | |
469 | member = TREE_OPERAND (member, 1); | |
470 | else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr))) | |
471 | /* Initializing a member of an anonymous union. */ | |
472 | return build_anon_member_initialization (member, init, vec); | |
473 | else | |
474 | /* We're initializing a vtable pointer in a base. Leave it as | |
475 | COMPONENT_REF so we remember the path to get to the vfield. */ | |
476 | gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node); | |
477 | } | |
478 | ||
49b5925f JM |
479 | /* Value-initialization can produce multiple initializers for the |
480 | same field; use the last one. */ | |
481 | if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member) | |
482 | (*vec)->last().value = init; | |
483 | else | |
484 | CONSTRUCTOR_APPEND_ELT (*vec, member, init); | |
2d76680f PC |
485 | return true; |
486 | } | |
487 | ||
488 | /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval. | |
489 | In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a | |
490 | BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */ | |
491 | ||
492 | static bool | |
493 | check_constexpr_bind_expr_vars (tree t) | |
494 | { | |
495 | gcc_assert (TREE_CODE (t) == BIND_EXPR); | |
496 | ||
2d76680f PC |
497 | for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var)) |
498 | if (TREE_CODE (var) == TYPE_DECL | |
4414e22e PC |
499 | && DECL_IMPLICIT_TYPEDEF_P (var) |
500 | && !LAMBDA_TYPE_P (TREE_TYPE (var))) | |
2d76680f PC |
501 | return false; |
502 | return true; | |
503 | } | |
504 | ||
505 | /* Subroutine of check_constexpr_ctor_body. */ | |
506 | ||
507 | static bool | |
508 | check_constexpr_ctor_body_1 (tree last, tree list) | |
509 | { | |
510 | switch (TREE_CODE (list)) | |
511 | { | |
512 | case DECL_EXPR: | |
a0008434 MP |
513 | if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL |
514 | || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL) | |
2d76680f | 515 | return true; |
2d76680f PC |
516 | return false; |
517 | ||
518 | case CLEANUP_POINT_EXPR: | |
519 | return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0), | |
520 | /*complain=*/false); | |
521 | ||
522 | case BIND_EXPR: | |
523 | if (!check_constexpr_bind_expr_vars (list) | |
524 | || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list), | |
525 | /*complain=*/false)) | |
526 | return false; | |
527 | return true; | |
528 | ||
529 | case USING_STMT: | |
530 | case STATIC_ASSERT: | |
96a95ac1 | 531 | case DEBUG_BEGIN_STMT: |
2d76680f PC |
532 | return true; |
533 | ||
534 | default: | |
535 | return false; | |
536 | } | |
537 | } | |
538 | ||
539 | /* Make sure that there are no statements after LAST in the constructor | |
540 | body represented by LIST. */ | |
541 | ||
542 | bool | |
543 | check_constexpr_ctor_body (tree last, tree list, bool complain) | |
544 | { | |
60813a46 JM |
545 | /* C++14 doesn't require a constexpr ctor to have an empty body. */ |
546 | if (cxx_dialect >= cxx14) | |
547 | return true; | |
548 | ||
2d76680f PC |
549 | bool ok = true; |
550 | if (TREE_CODE (list) == STATEMENT_LIST) | |
551 | { | |
552 | tree_stmt_iterator i = tsi_last (list); | |
553 | for (; !tsi_end_p (i); tsi_prev (&i)) | |
554 | { | |
555 | tree t = tsi_stmt (i); | |
556 | if (t == last) | |
557 | break; | |
558 | if (!check_constexpr_ctor_body_1 (last, t)) | |
559 | { | |
560 | ok = false; | |
561 | break; | |
562 | } | |
563 | } | |
564 | } | |
565 | else if (list != last | |
566 | && !check_constexpr_ctor_body_1 (last, list)) | |
567 | ok = false; | |
568 | if (!ok) | |
569 | { | |
570 | if (complain) | |
84fa214d | 571 | error ("%<constexpr%> constructor does not have empty body"); |
2d76680f PC |
572 | DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false; |
573 | } | |
574 | return ok; | |
575 | } | |
576 | ||
577 | /* V is a vector of constructor elements built up for the base and member | |
578 | initializers of a constructor for TYPE. They need to be in increasing | |
579 | offset order, which they might not be yet if TYPE has a primary base | |
580 | which is not first in the base-clause or a vptr and at least one base | |
581 | all of which are non-primary. */ | |
582 | ||
583 | static vec<constructor_elt, va_gc> * | |
584 | sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v) | |
585 | { | |
586 | tree pri = CLASSTYPE_PRIMARY_BINFO (type); | |
587 | tree field_type; | |
588 | unsigned i; | |
589 | constructor_elt *ce; | |
590 | ||
591 | if (pri) | |
592 | field_type = BINFO_TYPE (pri); | |
593 | else if (TYPE_CONTAINS_VPTR_P (type)) | |
594 | field_type = vtbl_ptr_type_node; | |
595 | else | |
596 | return v; | |
597 | ||
598 | /* Find the element for the primary base or vptr and move it to the | |
599 | beginning of the vec. */ | |
600 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
601 | if (TREE_TYPE (ce->index) == field_type) | |
602 | break; | |
603 | ||
604 | if (i > 0 && i < vec_safe_length (v)) | |
605 | { | |
606 | vec<constructor_elt, va_gc> &vref = *v; | |
607 | constructor_elt elt = vref[i]; | |
608 | for (; i > 0; --i) | |
609 | vref[i] = vref[i-1]; | |
610 | vref[0] = elt; | |
611 | } | |
612 | ||
613 | return v; | |
614 | } | |
615 | ||
616 | /* Build compile-time evalable representations of member-initializer list | |
617 | for a constexpr constructor. */ | |
618 | ||
619 | static tree | |
620 | build_constexpr_constructor_member_initializers (tree type, tree body) | |
621 | { | |
622 | vec<constructor_elt, va_gc> *vec = NULL; | |
623 | bool ok = true; | |
fa837fb6 JM |
624 | while (true) |
625 | switch (TREE_CODE (body)) | |
626 | { | |
627 | case MUST_NOT_THROW_EXPR: | |
628 | case EH_SPEC_BLOCK: | |
629 | body = TREE_OPERAND (body, 0); | |
630 | break; | |
631 | ||
632 | case STATEMENT_LIST: | |
0f54cc9c | 633 | for (tree stmt : tsi_range (body)) |
fa837fb6 | 634 | { |
0f54cc9c | 635 | body = stmt; |
fa837fb6 JM |
636 | if (TREE_CODE (body) == BIND_EXPR) |
637 | break; | |
638 | } | |
639 | break; | |
640 | ||
641 | case BIND_EXPR: | |
642 | body = BIND_EXPR_BODY (body); | |
643 | goto found; | |
644 | ||
645 | default: | |
646 | gcc_unreachable (); | |
58cc255c | 647 | } |
fa837fb6 | 648 | found: |
1259cb6d JJ |
649 | if (TREE_CODE (body) == TRY_BLOCK) |
650 | { | |
651 | body = TREE_OPERAND (body, 0); | |
652 | if (TREE_CODE (body) == BIND_EXPR) | |
653 | body = BIND_EXPR_BODY (body); | |
654 | } | |
2d76680f PC |
655 | if (TREE_CODE (body) == CLEANUP_POINT_EXPR) |
656 | { | |
657 | body = TREE_OPERAND (body, 0); | |
658 | if (TREE_CODE (body) == EXPR_STMT) | |
659 | body = TREE_OPERAND (body, 0); | |
660 | if (TREE_CODE (body) == INIT_EXPR | |
661 | && (same_type_ignoring_top_level_qualifiers_p | |
662 | (TREE_TYPE (TREE_OPERAND (body, 0)), | |
663 | current_class_type))) | |
664 | { | |
665 | /* Trivial copy. */ | |
666 | return TREE_OPERAND (body, 1); | |
667 | } | |
668 | ok = build_data_member_initialization (body, &vec); | |
669 | } | |
670 | else if (TREE_CODE (body) == STATEMENT_LIST) | |
671 | { | |
0f54cc9c | 672 | for (tree stmt : tsi_range (body)) |
2d76680f | 673 | { |
0f54cc9c | 674 | ok = build_data_member_initialization (stmt, &vec); |
2d76680f PC |
675 | if (!ok) |
676 | break; | |
677 | } | |
678 | } | |
2d76680f PC |
679 | else if (EXPR_P (body)) |
680 | ok = build_data_member_initialization (body, &vec); | |
681 | else | |
682 | gcc_assert (errorcount > 0); | |
683 | if (ok) | |
684 | { | |
685 | if (vec_safe_length (vec) > 0) | |
686 | { | |
687 | /* In a delegating constructor, return the target. */ | |
688 | constructor_elt *ce = &(*vec)[0]; | |
689 | if (ce->index == current_class_ptr) | |
690 | { | |
691 | body = ce->value; | |
692 | vec_free (vec); | |
693 | return body; | |
694 | } | |
695 | } | |
696 | vec = sort_constexpr_mem_initializers (type, vec); | |
697 | return build_constructor (type, vec); | |
698 | } | |
699 | else | |
700 | return error_mark_node; | |
701 | } | |
702 | ||
1b6fa695 ML |
703 | /* We have an expression tree T that represents a call, either CALL_EXPR |
704 | or AGGR_INIT_EXPR. If the call is lexically to a named function, | |
705 | retrun the _DECL for that function. */ | |
706 | ||
707 | static tree | |
708 | get_function_named_in_call (tree t) | |
709 | { | |
710 | tree fun = cp_get_callee (t); | |
711 | if (fun && TREE_CODE (fun) == ADDR_EXPR | |
712 | && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL) | |
713 | fun = TREE_OPERAND (fun, 0); | |
714 | return fun; | |
715 | } | |
716 | ||
bfc139e2 | 717 | /* Subroutine of check_constexpr_fundef. BODY is the body of a function |
2d76680f PC |
718 | declared to be constexpr, or a sub-statement thereof. Returns the |
719 | return value if suitable, error_mark_node for a statement not allowed in | |
720 | a constexpr function, or NULL_TREE if no return value was found. */ | |
721 | ||
5afef8b1 | 722 | tree |
2d76680f PC |
723 | constexpr_fn_retval (tree body) |
724 | { | |
725 | switch (TREE_CODE (body)) | |
726 | { | |
727 | case STATEMENT_LIST: | |
728 | { | |
2d76680f | 729 | tree expr = NULL_TREE; |
0f54cc9c | 730 | for (tree stmt : tsi_range (body)) |
2d76680f | 731 | { |
0f54cc9c | 732 | tree s = constexpr_fn_retval (stmt); |
2d76680f PC |
733 | if (s == error_mark_node) |
734 | return error_mark_node; | |
735 | else if (s == NULL_TREE) | |
736 | /* Keep iterating. */; | |
737 | else if (expr) | |
738 | /* Multiple return statements. */ | |
739 | return error_mark_node; | |
740 | else | |
741 | expr = s; | |
742 | } | |
743 | return expr; | |
744 | } | |
745 | ||
746 | case RETURN_EXPR: | |
747 | return break_out_target_exprs (TREE_OPERAND (body, 0)); | |
748 | ||
749 | case DECL_EXPR: | |
0162cb3b PC |
750 | { |
751 | tree decl = DECL_EXPR_DECL (body); | |
752 | if (TREE_CODE (decl) == USING_DECL | |
753 | /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */ | |
754 | || DECL_ARTIFICIAL (decl)) | |
755 | return NULL_TREE; | |
756 | return error_mark_node; | |
757 | } | |
2d76680f PC |
758 | |
759 | case CLEANUP_POINT_EXPR: | |
760 | return constexpr_fn_retval (TREE_OPERAND (body, 0)); | |
761 | ||
762 | case BIND_EXPR: | |
763 | if (!check_constexpr_bind_expr_vars (body)) | |
764 | return error_mark_node; | |
765 | return constexpr_fn_retval (BIND_EXPR_BODY (body)); | |
766 | ||
767 | case USING_STMT: | |
96a95ac1 | 768 | case DEBUG_BEGIN_STMT: |
2d76680f PC |
769 | return NULL_TREE; |
770 | ||
1b6fa695 ML |
771 | case CALL_EXPR: |
772 | { | |
773 | tree fun = get_function_named_in_call (body); | |
774 | if (fun != NULL_TREE | |
3d78e008 | 775 | && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE)) |
1b6fa695 ML |
776 | return NULL_TREE; |
777 | } | |
778 | /* Fallthru. */ | |
779 | ||
2d76680f PC |
780 | default: |
781 | return error_mark_node; | |
782 | } | |
783 | } | |
784 | ||
bfc139e2 | 785 | /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of |
2d76680f PC |
786 | FUN; do the necessary transformations to turn it into a single expression |
787 | that we can store in the hash table. */ | |
788 | ||
789 | static tree | |
790 | massage_constexpr_body (tree fun, tree body) | |
791 | { | |
792 | if (DECL_CONSTRUCTOR_P (fun)) | |
793 | body = build_constexpr_constructor_member_initializers | |
794 | (DECL_CONTEXT (fun), body); | |
60813a46 | 795 | else if (cxx_dialect < cxx14) |
2d76680f PC |
796 | { |
797 | if (TREE_CODE (body) == EH_SPEC_BLOCK) | |
798 | body = EH_SPEC_STMTS (body); | |
799 | if (TREE_CODE (body) == MUST_NOT_THROW_EXPR) | |
800 | body = TREE_OPERAND (body, 0); | |
801 | body = constexpr_fn_retval (body); | |
802 | } | |
803 | return body; | |
804 | } | |
805 | ||
3e4b91f2 NS |
806 | /* CTYPE is a type constructed from BODY. Return true if some |
807 | bases/fields are uninitialized, and complain if COMPLAIN. */ | |
2d76680f PC |
808 | |
809 | static bool | |
3e4b91f2 | 810 | cx_check_missing_mem_inits (tree ctype, tree body, bool complain) |
2d76680f | 811 | { |
7906797e | 812 | /* We allow uninitialized bases/fields in C++20. */ |
b04445d4 | 813 | if (cxx_dialect >= cxx20) |
7906797e MP |
814 | return false; |
815 | ||
3e4b91f2 NS |
816 | unsigned nelts = 0; |
817 | ||
818 | if (body) | |
819 | { | |
820 | if (TREE_CODE (body) != CONSTRUCTOR) | |
821 | return false; | |
822 | nelts = CONSTRUCTOR_NELTS (body); | |
823 | } | |
824 | tree field = TYPE_FIELDS (ctype); | |
2d76680f PC |
825 | |
826 | if (TREE_CODE (ctype) == UNION_TYPE) | |
827 | { | |
0c7bce0a | 828 | if (nelts == 0 && next_aggregate_field (field)) |
2d76680f PC |
829 | { |
830 | if (complain) | |
831 | error ("%<constexpr%> constructor for union %qT must " | |
832 | "initialize exactly one non-static data member", ctype); | |
833 | return true; | |
834 | } | |
835 | return false; | |
836 | } | |
837 | ||
3e4b91f2 NS |
838 | /* Iterate over the CONSTRUCTOR, checking any missing fields don't |
839 | need an explicit initialization. */ | |
840 | bool bad = false; | |
841 | for (unsigned i = 0; i <= nelts; ++i) | |
2d76680f | 842 | { |
3e4b91f2 NS |
843 | tree index = NULL_TREE; |
844 | if (i < nelts) | |
2d76680f PC |
845 | { |
846 | index = CONSTRUCTOR_ELT (body, i)->index; | |
847 | /* Skip base and vtable inits. */ | |
848 | if (TREE_CODE (index) != FIELD_DECL | |
849 | || DECL_ARTIFICIAL (index)) | |
850 | continue; | |
851 | } | |
3e4b91f2 | 852 | |
2d76680f PC |
853 | for (; field != index; field = DECL_CHAIN (field)) |
854 | { | |
855 | tree ftype; | |
3e4b91f2 NS |
856 | if (TREE_CODE (field) != FIELD_DECL) |
857 | continue; | |
7c30b12a | 858 | if (DECL_UNNAMED_BIT_FIELD (field)) |
2d76680f | 859 | continue; |
3e4b91f2 NS |
860 | if (DECL_ARTIFICIAL (field)) |
861 | continue; | |
862 | if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) | |
863 | { | |
7906797e | 864 | /* Recurse to check the anonymous aggregate member. */ |
3e4b91f2 NS |
865 | bad |= cx_check_missing_mem_inits |
866 | (TREE_TYPE (field), NULL_TREE, complain); | |
867 | if (bad && !complain) | |
868 | return true; | |
869 | continue; | |
870 | } | |
a1c9c9ff JM |
871 | ftype = TREE_TYPE (field); |
872 | if (!ftype || !TYPE_P (ftype) || !COMPLETE_TYPE_P (ftype)) | |
873 | /* A flexible array can't be intialized here, so don't complain | |
874 | that it isn't. */ | |
875 | continue; | |
a4dfd0f0 | 876 | if (is_empty_field (field)) |
6876b269 JM |
877 | /* An empty field doesn't need an initializer. */ |
878 | continue; | |
a1c9c9ff | 879 | ftype = strip_array_types (ftype); |
2d76680f PC |
880 | if (type_has_constexpr_default_constructor (ftype)) |
881 | { | |
882 | /* It's OK to skip a member with a trivial constexpr ctor. | |
883 | A constexpr ctor that isn't trivial should have been | |
884 | added in by now. */ | |
885 | gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype) | |
886 | || errorcount != 0); | |
887 | continue; | |
888 | } | |
889 | if (!complain) | |
890 | return true; | |
097f82ec | 891 | auto_diagnostic_group d; |
b8cd3996 JM |
892 | error ("member %qD must be initialized by mem-initializer " |
893 | "in %<constexpr%> constructor", field); | |
894 | inform (DECL_SOURCE_LOCATION (field), "declared here"); | |
2d76680f PC |
895 | bad = true; |
896 | } | |
897 | if (field == NULL_TREE) | |
898 | break; | |
3e4b91f2 NS |
899 | |
900 | if (ANON_AGGR_TYPE_P (TREE_TYPE (index))) | |
901 | { | |
902 | /* Check the anonymous aggregate initializer is valid. */ | |
903 | bad |= cx_check_missing_mem_inits | |
904 | (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain); | |
905 | if (bad && !complain) | |
906 | return true; | |
907 | } | |
2d76680f PC |
908 | field = DECL_CHAIN (field); |
909 | } | |
910 | ||
911 | return bad; | |
912 | } | |
913 | ||
914 | /* We are processing the definition of the constexpr function FUN. | |
bfc139e2 NS |
915 | Check that its body fulfills the apropriate requirements and |
916 | enter it in the constexpr function definition table. */ | |
2d76680f | 917 | |
bfc139e2 NS |
918 | void |
919 | maybe_save_constexpr_fundef (tree fun) | |
2d76680f | 920 | { |
bfc139e2 | 921 | if (processing_template_decl |
bfc139e2 | 922 | || cp_function_chain->invalid_constexpr |
81f97181 | 923 | || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))) |
bfc139e2 | 924 | return; |
2d76680f | 925 | |
87c2080b JM |
926 | /* With -fimplicit-constexpr, try to make inlines constexpr. We'll |
927 | actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass. */ | |
928 | bool implicit = false; | |
929 | if (flag_implicit_constexpr) | |
930 | { | |
931 | if (DECL_DELETING_DESTRUCTOR_P (fun) | |
932 | && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun))) | |
933 | /* Don't inherit implicit constexpr from the non-deleting | |
934 | destructor. */ | |
935 | DECL_DECLARED_CONSTEXPR_P (fun) = false; | |
936 | ||
937 | if (!DECL_DECLARED_CONSTEXPR_P (fun) | |
938 | && DECL_DECLARED_INLINE_P (fun) | |
939 | && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun))) | |
940 | implicit = true; | |
941 | } | |
942 | ||
943 | if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit) | |
944 | return; | |
945 | ||
946 | bool complain = !DECL_GENERATED_P (fun) && !implicit; | |
37326651 JM |
947 | |
948 | if (!is_valid_constexpr_fn (fun, complain)) | |
bfc139e2 | 949 | return; |
2d76680f | 950 | |
bfc139e2 | 951 | tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun)); |
ca30abcd | 952 | if (massaged == NULL_TREE || massaged == error_mark_node) |
2d76680f | 953 | { |
87c2080b | 954 | if (!DECL_CONSTRUCTOR_P (fun) && complain) |
84fa214d ML |
955 | error ("body of %<constexpr%> function %qD not a return-statement", |
956 | fun); | |
bfc139e2 | 957 | return; |
2d76680f PC |
958 | } |
959 | ||
4f05d85a | 960 | bool potential = potential_rvalue_constant_expression (massaged); |
37326651 | 961 | if (!potential && complain) |
c85f8dbb | 962 | require_potential_rvalue_constant_expression_fncheck (massaged); |
2d76680f | 963 | |
c5de3444 JM |
964 | if (DECL_CONSTRUCTOR_P (fun) && potential |
965 | && !DECL_DEFAULTED_FN (fun)) | |
37326651 JM |
966 | { |
967 | if (cx_check_missing_mem_inits (DECL_CONTEXT (fun), | |
968 | massaged, complain)) | |
969 | potential = false; | |
970 | else if (cxx_dialect > cxx11) | |
971 | { | |
972 | /* What we got from massage_constexpr_body is pretty much just the | |
973 | ctor-initializer, also check the body. */ | |
974 | massaged = DECL_SAVED_TREE (fun); | |
975 | potential = potential_rvalue_constant_expression (massaged); | |
976 | if (!potential && complain) | |
c85f8dbb | 977 | require_potential_rvalue_constant_expression_fncheck (massaged); |
37326651 JM |
978 | } |
979 | } | |
4f05d85a | 980 | |
c85f8dbb MP |
981 | if (!potential && complain |
982 | /* If -Wno-invalid-constexpr was specified, we haven't complained | |
983 | about non-constant expressions yet. Register the function and | |
984 | complain in explain_invalid_constexpr_fn if the function is | |
985 | called. */ | |
986 | && warn_invalid_constexpr != 0) | |
bfc139e2 | 987 | return; |
2d76680f | 988 | |
87c2080b JM |
989 | if (implicit) |
990 | { | |
991 | if (potential) | |
992 | { | |
993 | DECL_DECLARED_CONSTEXPR_P (fun) = true; | |
994 | DECL_LANG_SPECIFIC (fun)->u.fn.implicit_constexpr = true; | |
995 | if (DECL_CONSTRUCTOR_P (fun)) | |
996 | TYPE_HAS_CONSTEXPR_CTOR (DECL_CONTEXT (fun)) = true; | |
997 | } | |
998 | else | |
999 | /* Don't bother keeping the pre-generic body of unsuitable functions | |
1000 | not explicitly declared constexpr. */ | |
1001 | return; | |
1002 | } | |
1003 | ||
bfc139e2 | 1004 | constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE}; |
43574e4f | 1005 | bool clear_ctx = false; |
43574e4f JJ |
1006 | if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE) |
1007 | { | |
1008 | clear_ctx = true; | |
1009 | DECL_CONTEXT (DECL_RESULT (fun)) = fun; | |
1010 | } | |
bfc139e2 NS |
1011 | tree saved_fn = current_function_decl; |
1012 | current_function_decl = fun; | |
1013 | entry.body = copy_fn (entry.decl, entry.parms, entry.result); | |
43574e4f | 1014 | current_function_decl = saved_fn; |
43574e4f | 1015 | if (clear_ctx) |
bfc139e2 | 1016 | DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE; |
4f05d85a JM |
1017 | if (!potential) |
1018 | /* For a template instantiation, we want to remember the pre-generic body | |
1019 | for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression | |
1020 | that it doesn't need to bother trying to expand the function. */ | |
1021 | entry.result = error_mark_node; | |
1022 | ||
bfc139e2 NS |
1023 | register_constexpr_fundef (entry); |
1024 | } | |
1025 | ||
1026 | /* BODY is a validated and massaged definition of a constexpr | |
1027 | function. Register it in the hash table. */ | |
1028 | ||
1029 | void | |
1030 | register_constexpr_fundef (const constexpr_fundef &value) | |
1031 | { | |
1032 | /* Create the constexpr function table if necessary. */ | |
1033 | if (constexpr_fundef_table == NULL) | |
1034 | constexpr_fundef_table | |
1035 | = hash_table<constexpr_fundef_hasher>::create_ggc (101); | |
1036 | ||
1037 | constexpr_fundef **slot = constexpr_fundef_table->find_slot | |
1038 | (const_cast<constexpr_fundef *> (&value), INSERT); | |
1039 | ||
2d76680f PC |
1040 | gcc_assert (*slot == NULL); |
1041 | *slot = ggc_alloc<constexpr_fundef> (); | |
bfc139e2 | 1042 | **slot = value; |
2d76680f PC |
1043 | } |
1044 | ||
c85f8dbb MP |
1045 | /* FUN is a non-constexpr (or, with -Wno-invalid-constexpr, a constexpr |
1046 | function called in a context that requires a constant expression). | |
1047 | If it comes from a constexpr template, explain why the instantiation | |
1048 | isn't constexpr. Otherwise, explain why the function cannot be used | |
1049 | in a constexpr context. */ | |
2d76680f PC |
1050 | |
1051 | void | |
1052 | explain_invalid_constexpr_fn (tree fun) | |
1053 | { | |
1054 | static hash_set<tree> *diagnosed; | |
1055 | tree body; | |
c85f8dbb MP |
1056 | /* In C++23, a function marked 'constexpr' may not actually be a constant |
1057 | expression. We haven't diagnosed the problem yet: -Winvalid-constexpr | |
1058 | wasn't enabled. The function was called, so diagnose why it cannot be | |
1059 | used in a constant expression. */ | |
1060 | if (warn_invalid_constexpr == 0 && DECL_DECLARED_CONSTEXPR_P (fun)) | |
1061 | /* Go on. */; | |
98e5a19a | 1062 | /* Only diagnose defaulted functions, lambdas, or instantiations. */ |
c85f8dbb MP |
1063 | else if (!DECL_DEFAULTED_FN (fun) |
1064 | && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) | |
1065 | && !is_instantiation_of_constexpr (fun)) | |
f22f817c JM |
1066 | { |
1067 | inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun); | |
1068 | return; | |
1069 | } | |
2d76680f PC |
1070 | if (diagnosed == NULL) |
1071 | diagnosed = new hash_set<tree>; | |
1072 | if (diagnosed->add (fun)) | |
1073 | /* Already explained. */ | |
1074 | return; | |
1075 | ||
17dc903e | 1076 | iloc_sentinel ils = input_location; |
98e5a19a JM |
1077 | if (!lambda_static_thunk_p (fun)) |
1078 | { | |
1079 | /* Diagnostics should completely ignore the static thunk, so leave | |
1080 | input_location set to our caller's location. */ | |
1081 | input_location = DECL_SOURCE_LOCATION (fun); | |
1082 | inform (input_location, | |
84fa214d | 1083 | "%qD is not usable as a %<constexpr%> function because:", fun); |
98e5a19a | 1084 | } |
2d76680f PC |
1085 | /* First check the declaration. */ |
1086 | if (is_valid_constexpr_fn (fun, true)) | |
1087 | { | |
1088 | /* Then if it's OK, the body. */ | |
98e5a19a | 1089 | if (!DECL_DECLARED_CONSTEXPR_P (fun) |
4f05d85a | 1090 | && DECL_DEFAULTED_FN (fun)) |
2d76680f PC |
1091 | explain_implicit_non_constexpr (fun); |
1092 | else | |
1093 | { | |
4f05d85a JM |
1094 | if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun)) |
1095 | body = fd->body; | |
1096 | else | |
1097 | body = DECL_SAVED_TREE (fun); | |
1098 | body = massage_constexpr_body (fun, body); | |
2d76680f PC |
1099 | require_potential_rvalue_constant_expression (body); |
1100 | if (DECL_CONSTRUCTOR_P (fun)) | |
a22eeaca MP |
1101 | { |
1102 | cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true); | |
1103 | if (cxx_dialect > cxx11) | |
1104 | { | |
1105 | /* Also check the body, not just the ctor-initializer. */ | |
1106 | body = DECL_SAVED_TREE (fun); | |
1107 | require_potential_rvalue_constant_expression (body); | |
1108 | } | |
1109 | } | |
2d76680f PC |
1110 | } |
1111 | } | |
2d76680f PC |
1112 | } |
1113 | ||
1114 | /* Objects of this type represent calls to constexpr functions | |
1115 | along with the bindings of parameters to their arguments, for | |
1116 | the purpose of compile time evaluation. */ | |
1117 | ||
1118 | struct GTY((for_user)) constexpr_call { | |
1119 | /* Description of the constexpr function definition. */ | |
1120 | constexpr_fundef *fundef; | |
3c961dc7 | 1121 | /* Parameter bindings environment. A TREE_VEC of arguments. */ |
2d76680f PC |
1122 | tree bindings; |
1123 | /* Result of the call. | |
1124 | NULL means the call is being evaluated. | |
1125 | error_mark_node means that the evaluation was erroneous; | |
1126 | otherwise, the actuall value of the call. */ | |
1127 | tree result; | |
1128 | /* The hash of this call; we remember it here to avoid having to | |
1129 | recalculate it when expanding the hash table. */ | |
1130 | hashval_t hash; | |
3a0bc47c PP |
1131 | /* The value of constexpr_ctx::manifestly_const_eval. */ |
1132 | enum mce_value manifestly_const_eval; | |
2d76680f PC |
1133 | }; |
1134 | ||
ca752f39 | 1135 | struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call> |
2d76680f PC |
1136 | { |
1137 | static hashval_t hash (constexpr_call *); | |
1138 | static bool equal (constexpr_call *, constexpr_call *); | |
3e605b20 JM |
1139 | }; |
1140 | ||
4b390698 JJ |
1141 | enum constexpr_switch_state { |
1142 | /* Used when processing a switch for the first time by cxx_eval_switch_expr | |
1143 | and default: label for that switch has not been seen yet. */ | |
1144 | css_default_not_seen, | |
1145 | /* Used when processing a switch for the first time by cxx_eval_switch_expr | |
1146 | and default: label for that switch has been seen already. */ | |
1147 | css_default_seen, | |
1148 | /* Used when processing a switch for the second time by | |
1149 | cxx_eval_switch_expr, where default: label should match. */ | |
1150 | css_default_processing | |
1151 | }; | |
1152 | ||
8e007055 JJ |
1153 | /* The constexpr expansion context part which needs one instance per |
1154 | cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of | |
1155 | variables initialized within the expression. */ | |
1156 | ||
e6a29aab | 1157 | class constexpr_global_ctx { |
8e007055 | 1158 | /* Values for any temporaries or local variables within the |
9fdbd7d6 NS |
1159 | constant-expression. Objects outside their lifetime have |
1160 | value 'void_node'. */ | |
8e007055 | 1161 | hash_map<tree,tree> values; |
e6a29aab | 1162 | public: |
8e007055 JJ |
1163 | /* Number of cxx_eval_constant_expression calls (except skipped ones, |
1164 | on simple constants or location wrappers) encountered during current | |
1165 | cxx_eval_outermost_constant_expr call. */ | |
1166 | HOST_WIDE_INT constexpr_ops_count; | |
1167 | /* Heap VAR_DECLs created during the evaluation of the outermost constant | |
1168 | expression. */ | |
1169 | auto_vec<tree, 16> heap_vars; | |
ee1de08d JJ |
1170 | /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */ |
1171 | vec<tree> *cleanups; | |
e6a29aab JM |
1172 | /* If non-null, only allow modification of existing values of the variables |
1173 | in this set. Set by modifiable_tracker, below. */ | |
1174 | hash_set<tree> *modifiable; | |
f74f6092 JJ |
1175 | /* Number of heap VAR_DECL deallocations. */ |
1176 | unsigned heap_dealloc_count; | |
8e007055 | 1177 | /* Constructor. */ |
f74f6092 | 1178 | constexpr_global_ctx () |
e6a29aab JM |
1179 | : constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr), |
1180 | heap_dealloc_count (0) {} | |
1181 | ||
9fdbd7d6 NS |
1182 | bool is_outside_lifetime (tree t) |
1183 | { | |
1184 | if (tree *p = values.get (t)) | |
1185 | if (*p == void_node) | |
1186 | return true; | |
1187 | return false; | |
1188 | } | |
e6a29aab JM |
1189 | tree get_value (tree t) |
1190 | { | |
1191 | if (tree *p = values.get (t)) | |
9fdbd7d6 NS |
1192 | if (*p != void_node) |
1193 | return *p; | |
e6a29aab JM |
1194 | return NULL_TREE; |
1195 | } | |
90bc2d09 | 1196 | tree *get_value_ptr (tree t, bool initializing) |
e6a29aab JM |
1197 | { |
1198 | if (modifiable && !modifiable->contains (t)) | |
1199 | return nullptr; | |
9fdbd7d6 | 1200 | if (tree *p = values.get (t)) |
90bc2d09 NS |
1201 | { |
1202 | if (*p != void_node) | |
1203 | return p; | |
1204 | else if (initializing) | |
1205 | { | |
1206 | *p = NULL_TREE; | |
1207 | return p; | |
1208 | } | |
1209 | } | |
9fdbd7d6 | 1210 | return nullptr; |
e6a29aab JM |
1211 | } |
1212 | void put_value (tree t, tree v) | |
1213 | { | |
1214 | bool already_in_map = values.put (t, v); | |
1215 | if (!already_in_map && modifiable) | |
1216 | modifiable->add (t); | |
1217 | } | |
90bc2d09 | 1218 | void destroy_value (tree t) |
9fdbd7d6 | 1219 | { |
90bc2d09 NS |
1220 | if (TREE_CODE (t) == VAR_DECL |
1221 | || TREE_CODE (t) == PARM_DECL | |
1222 | || TREE_CODE (t) == RESULT_DECL) | |
9fdbd7d6 NS |
1223 | values.put (t, void_node); |
1224 | else | |
1225 | values.remove (t); | |
1226 | } | |
90bc2d09 NS |
1227 | void clear_value (tree t) |
1228 | { | |
1229 | values.remove (t); | |
1230 | } | |
e6a29aab JM |
1231 | }; |
1232 | ||
1233 | /* Helper class for constexpr_global_ctx. In some cases we want to avoid | |
1234 | side-effects from evaluation of a particular subexpression of a | |
1235 | constant-expression. In such cases we use modifiable_tracker to prevent | |
1236 | modification of variables created outside of that subexpression. | |
1237 | ||
1238 | ??? We could change the hash_set to a hash_map, allow and track external | |
1239 | modifications, and roll them back in the destructor. It's not clear to me | |
1240 | that this would be worthwhile. */ | |
1241 | ||
1242 | class modifiable_tracker | |
1243 | { | |
1244 | hash_set<tree> set; | |
1245 | constexpr_global_ctx *global; | |
1246 | public: | |
1247 | modifiable_tracker (constexpr_global_ctx *g): global(g) | |
1248 | { | |
1249 | global->modifiable = &set; | |
1250 | } | |
1251 | ~modifiable_tracker () | |
1252 | { | |
1253 | for (tree t: set) | |
90bc2d09 | 1254 | global->clear_value (t); |
e6a29aab JM |
1255 | global->modifiable = nullptr; |
1256 | } | |
8e007055 JJ |
1257 | }; |
1258 | ||
3e605b20 JM |
1259 | /* The constexpr expansion context. CALL is the current function |
1260 | expansion, CTOR is the current aggregate initializer, OBJECT is the | |
8e007055 | 1261 | object being initialized by CTOR, either a VAR_DECL or a _REF. */ |
3e605b20 JM |
1262 | |
1263 | struct constexpr_ctx { | |
8e007055 JJ |
1264 | /* The part of the context that needs to be unique to the whole |
1265 | cxx_eval_outermost_constant_expr invocation. */ | |
1266 | constexpr_global_ctx *global; | |
13f649f6 | 1267 | /* The innermost call we're evaluating. */ |
3e605b20 | 1268 | constexpr_call *call; |
ee1de08d JJ |
1269 | /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen |
1270 | within the current LOOP_EXPR. NULL if we aren't inside a loop. */ | |
0ee28590 | 1271 | vec<tree> *save_exprs; |
13f649f6 JM |
1272 | /* The CONSTRUCTOR we're currently building up for an aggregate |
1273 | initializer. */ | |
3e605b20 | 1274 | tree ctor; |
13f649f6 | 1275 | /* The object we're building the CONSTRUCTOR for. */ |
3e605b20 | 1276 | tree object; |
4b390698 JJ |
1277 | /* If inside SWITCH_EXPR. */ |
1278 | constexpr_switch_state *css_state; | |
49a86fce PP |
1279 | /* The aggregate initialization context inside which this one is nested. This |
1280 | is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */ | |
1281 | const constexpr_ctx *parent; | |
a15ffa22 | 1282 | |
aaa26bf4 JM |
1283 | /* Whether we should error on a non-constant expression or fail quietly. |
1284 | This flag needs to be here, but some of the others could move to global | |
1285 | if they get larger than a word. */ | |
2b3ab879 | 1286 | bool quiet; |
13f649f6 JM |
1287 | /* Whether we are strictly conforming to constant expression rules or |
1288 | trying harder to get a constant value. */ | |
69eb4fde | 1289 | bool strict; |
e4082611 | 1290 | /* Whether __builtin_is_constant_evaluated () should be true. */ |
3a0bc47c | 1291 | mce_value manifestly_const_eval; |
3e605b20 | 1292 | }; |
2d76680f | 1293 | |
90bc2d09 NS |
1294 | /* Remove T from the global values map, checking for attempts to destroy |
1295 | a value that has already finished its lifetime. */ | |
1296 | ||
1297 | static void | |
1298 | destroy_value_checked (const constexpr_ctx* ctx, tree t, bool *non_constant_p) | |
1299 | { | |
1300 | if (t == error_mark_node || TREE_TYPE (t) == error_mark_node) | |
1301 | return; | |
1302 | ||
1303 | /* Don't error again here if we've already reported a problem. */ | |
1304 | if (!*non_constant_p | |
1305 | && DECL_P (t) | |
1306 | /* Non-trivial destructors have their lifetimes ended explicitly | |
1307 | with a clobber, so don't worry about it here. */ | |
1308 | && (!TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t)) | |
1309 | /* ...except parameters are remapped in cxx_eval_call_expression, | |
1310 | and the destructor call during cleanup won't be able to tell that | |
1311 | this value has already been destroyed, so complain now. This is | |
1312 | not quite unobservable, but is extremely unlikely to crop up in | |
1313 | practice; see g++.dg/cpp2a/constexpr-lifetime2.C. */ | |
1314 | || TREE_CODE (t) == PARM_DECL) | |
1315 | && ctx->global->is_outside_lifetime (t)) | |
1316 | { | |
1317 | if (!ctx->quiet) | |
1318 | { | |
1319 | auto_diagnostic_group d; | |
1320 | error ("destroying %qE outside its lifetime", t); | |
1321 | inform (DECL_SOURCE_LOCATION (t), "declared here"); | |
1322 | } | |
1323 | *non_constant_p = true; | |
1324 | } | |
1325 | ctx->global->destroy_value (t); | |
1326 | } | |
1327 | ||
f65a3299 PP |
1328 | /* This internal flag controls whether we should avoid doing anything during |
1329 | constexpr evaluation that would cause extra DECL_UID generation, such as | |
1330 | template instantiation and function body copying. */ | |
1331 | ||
1332 | static bool uid_sensitive_constexpr_evaluation_value; | |
1333 | ||
1334 | /* An internal counter that keeps track of the number of times | |
1335 | uid_sensitive_constexpr_evaluation_p returned true. */ | |
1336 | ||
1337 | static unsigned uid_sensitive_constexpr_evaluation_true_counter; | |
1338 | ||
1339 | /* The accessor for uid_sensitive_constexpr_evaluation_value which also | |
1340 | increments the corresponding counter. */ | |
1341 | ||
1342 | static bool | |
1343 | uid_sensitive_constexpr_evaluation_p () | |
1344 | { | |
1345 | if (uid_sensitive_constexpr_evaluation_value) | |
1346 | { | |
1347 | ++uid_sensitive_constexpr_evaluation_true_counter; | |
1348 | return true; | |
1349 | } | |
1350 | else | |
1351 | return false; | |
1352 | } | |
1353 | ||
1354 | /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel | |
1355 | enables the internal flag for uid_sensitive_constexpr_evaluation_p | |
1356 | during the lifetime of the sentinel object. Upon its destruction, the | |
1357 | previous value of uid_sensitive_constexpr_evaluation_p is restored. */ | |
1358 | ||
1359 | uid_sensitive_constexpr_evaluation_sentinel | |
1360 | ::uid_sensitive_constexpr_evaluation_sentinel () | |
1361 | : ovr (uid_sensitive_constexpr_evaluation_value, true) | |
1362 | { | |
1363 | } | |
1364 | ||
1365 | /* The default constructor for uid_sensitive_constexpr_evaluation_checker | |
1366 | records the current number of times that uid_sensitive_constexpr_evaluation_p | |
1367 | has been called and returned true. */ | |
1368 | ||
1369 | uid_sensitive_constexpr_evaluation_checker | |
1370 | ::uid_sensitive_constexpr_evaluation_checker () | |
1371 | : saved_counter (uid_sensitive_constexpr_evaluation_true_counter) | |
1372 | { | |
1373 | } | |
1374 | ||
1375 | /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and | |
1376 | some constexpr evaluation was restricted due to u_s_c_e_p being called | |
1377 | and returning true during the lifetime of this checker object. */ | |
1378 | ||
1379 | bool | |
1380 | uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const | |
1381 | { | |
1382 | return (uid_sensitive_constexpr_evaluation_value | |
1383 | && saved_counter != uid_sensitive_constexpr_evaluation_true_counter); | |
1384 | } | |
1385 | ||
1386 | ||
2d76680f PC |
1387 | /* A table of all constexpr calls that have been evaluated by the |
1388 | compiler in this translation unit. */ | |
1389 | ||
97f3003f | 1390 | static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table; |
2d76680f | 1391 | |
2d76680f PC |
1392 | /* Compute a hash value for a constexpr call representation. */ |
1393 | ||
1394 | inline hashval_t | |
1395 | constexpr_call_hasher::hash (constexpr_call *info) | |
1396 | { | |
1397 | return info->hash; | |
1398 | } | |
1399 | ||
1400 | /* Return true if the objects pointed to by P and Q represent calls | |
1401 | to the same constexpr function with the same arguments. | |
1402 | Otherwise, return false. */ | |
1403 | ||
1404 | bool | |
1405 | constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs) | |
1406 | { | |
2d76680f | 1407 | if (lhs == rhs) |
46cf7fa1 JJ |
1408 | return true; |
1409 | if (lhs->hash != rhs->hash) | |
1410 | return false; | |
13de99bc | 1411 | if (lhs->manifestly_const_eval != rhs->manifestly_const_eval) |
cce3ae91 | 1412 | return false; |
2d76680f | 1413 | if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef)) |
46cf7fa1 | 1414 | return false; |
3c961dc7 | 1415 | return cp_tree_equal (lhs->bindings, rhs->bindings); |
2d76680f PC |
1416 | } |
1417 | ||
1418 | /* Initialize the constexpr call table, if needed. */ | |
1419 | ||
1420 | static void | |
1421 | maybe_initialize_constexpr_call_table (void) | |
1422 | { | |
1423 | if (constexpr_call_table == NULL) | |
1424 | constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101); | |
1425 | } | |
1426 | ||
c0daf32d PP |
1427 | /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when |
1428 | a function happens to get called recursively, we unshare the callee | |
1429 | function's body and evaluate this unshared copy instead of evaluating the | |
1430 | original body. | |
1431 | ||
1432 | FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function | |
1433 | copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map | |
97f3003f JM |
1434 | that's keyed off of the original FUNCTION_DECL and whose value is a |
1435 | TREE_LIST of this function's unused copies awaiting reuse. | |
c0daf32d | 1436 | |
97f3003f | 1437 | This is not GC-deletable to avoid GC affecting UID generation. */ |
c0daf32d | 1438 | |
ba6730bd | 1439 | static GTY(()) decl_tree_map *fundef_copies_table; |
c0daf32d | 1440 | |
c0daf32d | 1441 | /* Reuse a copy or create a new unshared copy of the function FUN. |
3c98ff9b NS |
1442 | Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE |
1443 | is parms, TYPE is result. */ | |
c0daf32d | 1444 | |
97f3003f | 1445 | static tree |
f65a3299 | 1446 | get_fundef_copy (constexpr_fundef *fundef) |
c0daf32d | 1447 | { |
97f3003f | 1448 | tree copy; |
3c98ff9b | 1449 | bool existed; |
c89844e5 JM |
1450 | tree *slot = &(hash_map_safe_get_or_insert<hm_ggc> |
1451 | (fundef_copies_table, fundef->decl, &existed, 127)); | |
3c98ff9b NS |
1452 | |
1453 | if (!existed) | |
c0daf32d | 1454 | { |
3c98ff9b NS |
1455 | /* There is no cached function available, or in use. We can use |
1456 | the function directly. That the slot is now created records | |
1457 | that this function is now in use. */ | |
43574e4f JJ |
1458 | copy = build_tree_list (fundef->body, fundef->parms); |
1459 | TREE_TYPE (copy) = fundef->result; | |
3c98ff9b NS |
1460 | } |
1461 | else if (*slot == NULL_TREE) | |
1462 | { | |
f65a3299 | 1463 | if (uid_sensitive_constexpr_evaluation_p ()) |
aaa26bf4 JM |
1464 | return NULL_TREE; |
1465 | ||
3c98ff9b | 1466 | /* We've already used the function itself, so make a copy. */ |
97f3003f | 1467 | copy = build_tree_list (NULL, NULL); |
43574e4f JJ |
1468 | tree saved_body = DECL_SAVED_TREE (fundef->decl); |
1469 | tree saved_parms = DECL_ARGUMENTS (fundef->decl); | |
1470 | tree saved_result = DECL_RESULT (fundef->decl); | |
1471 | tree saved_fn = current_function_decl; | |
1472 | DECL_SAVED_TREE (fundef->decl) = fundef->body; | |
1473 | DECL_ARGUMENTS (fundef->decl) = fundef->parms; | |
1474 | DECL_RESULT (fundef->decl) = fundef->result; | |
1475 | current_function_decl = fundef->decl; | |
1476 | TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy), | |
1477 | TREE_TYPE (copy)); | |
1478 | current_function_decl = saved_fn; | |
1479 | DECL_RESULT (fundef->decl) = saved_result; | |
1480 | DECL_ARGUMENTS (fundef->decl) = saved_parms; | |
1481 | DECL_SAVED_TREE (fundef->decl) = saved_body; | |
c0daf32d PP |
1482 | } |
1483 | else | |
1484 | { | |
3c98ff9b | 1485 | /* We have a cached function available. */ |
c0daf32d | 1486 | copy = *slot; |
97f3003f | 1487 | *slot = TREE_CHAIN (copy); |
c0daf32d PP |
1488 | } |
1489 | ||
1490 | return copy; | |
1491 | } | |
1492 | ||
3c98ff9b NS |
1493 | /* Save the copy COPY of function FUN for later reuse by |
1494 | get_fundef_copy(). By construction, there will always be an entry | |
1495 | to find. */ | |
c0daf32d PP |
1496 | |
1497 | static void | |
97f3003f | 1498 | save_fundef_copy (tree fun, tree copy) |
c0daf32d | 1499 | { |
3c98ff9b | 1500 | tree *slot = fundef_copies_table->get (fun); |
97f3003f | 1501 | TREE_CHAIN (copy) = *slot; |
c0daf32d PP |
1502 | *slot = copy; |
1503 | } | |
1504 | ||
72f76540 JM |
1505 | /* Whether our evaluation wants a prvalue (e.g. CONSTRUCTOR or _CST), |
1506 | a glvalue (e.g. VAR_DECL or _REF), or nothing. */ | |
1507 | ||
1508 | enum value_cat { | |
1509 | vc_prvalue = 0, | |
1510 | vc_glvalue = 1, | |
1511 | vc_discard = 2 | |
1512 | }; | |
1513 | ||
1514 | static tree cxx_eval_constant_expression (const constexpr_ctx *, tree, | |
1515 | value_cat, bool *, bool *, tree * = NULL); | |
b41a927b JM |
1516 | static tree cxx_eval_bare_aggregate (const constexpr_ctx *, tree, |
1517 | value_cat, bool *, bool *); | |
1189c038 JM |
1518 | static tree cxx_fold_indirect_ref (const constexpr_ctx *, location_t, tree, tree, |
1519 | bool * = NULL); | |
8c9c92f8 | 1520 | static tree find_heap_var_refs (tree *, int *, void *); |
72f76540 | 1521 | |
2d76680f PC |
1522 | /* Attempt to evaluate T which represents a call to a builtin function. |
1523 | We assume here that all builtin functions evaluate to scalar types | |
1524 | represented by _CST nodes. */ | |
1525 | ||
1526 | static tree | |
5756d0f9 | 1527 | cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun, |
72f76540 | 1528 | value_cat lval, |
2d76680f PC |
1529 | bool *non_constant_p, bool *overflow_p) |
1530 | { | |
1531 | const int nargs = call_expr_nargs (t); | |
1532 | tree *args = (tree *) alloca (nargs * sizeof (tree)); | |
1533 | tree new_call; | |
1534 | int i; | |
5756d0f9 JM |
1535 | |
1536 | /* Don't fold __builtin_constant_p within a constexpr function. */ | |
e4082611 | 1537 | bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun); |
b925d25d | 1538 | |
fe736ffd JM |
1539 | /* If we aren't requiring a constant expression, defer __builtin_constant_p |
1540 | in a constexpr function until we have values for the parameters. */ | |
b925d25d | 1541 | if (bi_const_p |
3a0bc47c | 1542 | && ctx->manifestly_const_eval != mce_true |
5756d0f9 JM |
1543 | && current_function_decl |
1544 | && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) | |
2d76680f | 1545 | { |
5756d0f9 JM |
1546 | *non_constant_p = true; |
1547 | return t; | |
2d76680f | 1548 | } |
5756d0f9 | 1549 | |
e4082611 | 1550 | /* For __builtin_is_constant_evaluated, defer it if not |
117c6426 JJ |
1551 | ctx->manifestly_const_eval (as sometimes we try to constant evaluate |
1552 | without manifestly_const_eval even expressions or parts thereof which | |
1553 | will later be manifestly const_eval evaluated), otherwise fold it to | |
1554 | true. */ | |
3d78e008 | 1555 | if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED, |
13de99bc | 1556 | BUILT_IN_FRONTEND)) |
e4082611 | 1557 | { |
3a0bc47c | 1558 | if (ctx->manifestly_const_eval == mce_unknown) |
e4082611 JJ |
1559 | { |
1560 | *non_constant_p = true; | |
1561 | return t; | |
1562 | } | |
3a0bc47c PP |
1563 | return constant_boolean_node (ctx->manifestly_const_eval == mce_true, |
1564 | boolean_type_node); | |
e4082611 JJ |
1565 | } |
1566 | ||
ff603745 | 1567 | if (fndecl_built_in_p (fun, CP_BUILT_IN_SOURCE_LOCATION, BUILT_IN_FRONTEND)) |
ba3d8dff JJ |
1568 | { |
1569 | temp_override<tree> ovr (current_function_decl); | |
1570 | if (ctx->call && ctx->call->fundef) | |
1571 | current_function_decl = ctx->call->fundef->decl; | |
64f7a3b3 | 1572 | return fold_builtin_source_location (t); |
ba3d8dff | 1573 | } |
ff603745 | 1574 | |
69dc042f JM |
1575 | int strops = 0; |
1576 | int strret = 0; | |
1577 | if (fndecl_built_in_p (fun, BUILT_IN_NORMAL)) | |
1578 | switch (DECL_FUNCTION_CODE (fun)) | |
1579 | { | |
1580 | case BUILT_IN_STRLEN: | |
1581 | case BUILT_IN_STRNLEN: | |
1582 | strops = 1; | |
1583 | break; | |
1584 | case BUILT_IN_MEMCHR: | |
1585 | case BUILT_IN_STRCHR: | |
1586 | case BUILT_IN_STRRCHR: | |
1587 | strops = 1; | |
1588 | strret = 1; | |
1589 | break; | |
1590 | case BUILT_IN_MEMCMP: | |
1591 | case BUILT_IN_STRCMP: | |
1592 | strops = 2; | |
1593 | break; | |
1594 | case BUILT_IN_STRSTR: | |
1595 | strops = 2; | |
1596 | strret = 1; | |
bc13106e JJ |
1597 | break; |
1598 | case BUILT_IN_ASAN_POINTER_COMPARE: | |
1599 | case BUILT_IN_ASAN_POINTER_SUBTRACT: | |
1600 | /* These builtins shall be ignored during constant expression | |
1601 | evaluation. */ | |
1602 | return void_node; | |
d68d3664 JM |
1603 | case BUILT_IN_UNREACHABLE: |
1604 | case BUILT_IN_TRAP: | |
1605 | if (!*non_constant_p && !ctx->quiet) | |
1606 | { | |
1607 | /* Do not allow__builtin_unreachable in constexpr function. | |
1608 | The __builtin_unreachable call with BUILTINS_LOCATION | |
1609 | comes from cp_maybe_instrument_return. */ | |
1610 | if (EXPR_LOCATION (t) == BUILTINS_LOCATION) | |
1611 | error ("%<constexpr%> call flows off the end of the function"); | |
1612 | else | |
1613 | error ("%q+E is not a constant expression", t); | |
1614 | } | |
1615 | *non_constant_p = true; | |
1616 | return t; | |
69dc042f JM |
1617 | default: |
1618 | break; | |
1619 | } | |
1620 | ||
5756d0f9 JM |
1621 | /* Be permissive for arguments to built-ins; __builtin_constant_p should |
1622 | return constant false for a non-constant argument. */ | |
1623 | constexpr_ctx new_ctx = *ctx; | |
1624 | new_ctx.quiet = true; | |
5756d0f9 | 1625 | for (i = 0; i < nargs; ++i) |
b925d25d | 1626 | { |
69dc042f | 1627 | tree arg = CALL_EXPR_ARG (t, i); |
6f346913 | 1628 | tree oarg = arg; |
69dc042f JM |
1629 | |
1630 | /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since | |
1631 | expand_builtin doesn't know how to look in the values table. */ | |
1632 | bool strop = i < strops; | |
1633 | if (strop) | |
1634 | { | |
1635 | STRIP_NOPS (arg); | |
1636 | if (TREE_CODE (arg) == ADDR_EXPR) | |
1637 | arg = TREE_OPERAND (arg, 0); | |
1638 | else | |
1639 | strop = false; | |
1640 | } | |
1641 | ||
2ff7172a JJ |
1642 | /* If builtin_valid_in_constant_expr_p is true, |
1643 | potential_constant_expression_1 has not recursed into the arguments | |
1644 | of the builtin, verify it here. */ | |
1645 | if (!builtin_valid_in_constant_expr_p (fun) | |
69dc042f | 1646 | || potential_constant_expression (arg)) |
4cd3e7df JJ |
1647 | { |
1648 | bool dummy1 = false, dummy2 = false; | |
72f76540 | 1649 | arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue, |
69dc042f | 1650 | &dummy1, &dummy2); |
4cd3e7df JJ |
1651 | } |
1652 | ||
b925d25d | 1653 | if (bi_const_p) |
4cd3e7df | 1654 | /* For __builtin_constant_p, fold all expressions with constant values |
b925d25d | 1655 | even if they aren't C++ constant-expressions. */ |
69dc042f JM |
1656 | arg = cp_fold_rvalue (arg); |
1657 | else if (strop) | |
1658 | { | |
1659 | if (TREE_CODE (arg) == CONSTRUCTOR) | |
1660 | arg = braced_lists_to_strings (TREE_TYPE (arg), arg); | |
1661 | if (TREE_CODE (arg) == STRING_CST) | |
1662 | arg = build_address (arg); | |
6f346913 JM |
1663 | else |
1664 | arg = oarg; | |
69dc042f JM |
1665 | } |
1666 | ||
1667 | args[i] = arg; | |
b925d25d | 1668 | } |
5756d0f9 JM |
1669 | |
1670 | bool save_ffbcp = force_folding_builtin_constant_p; | |
3a0bc47c | 1671 | force_folding_builtin_constant_p |= ctx->manifestly_const_eval == mce_true; |
43574e4f JJ |
1672 | tree save_cur_fn = current_function_decl; |
1673 | /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */ | |
1674 | if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION) | |
1675 | && ctx->call | |
1676 | && ctx->call->fundef) | |
1677 | current_function_decl = ctx->call->fundef->decl; | |
6cd005a2 JJ |
1678 | if (fndecl_built_in_p (fun, |
1679 | CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS, | |
1680 | BUILT_IN_FRONTEND)) | |
1681 | { | |
1682 | location_t loc = EXPR_LOCATION (t); | |
1683 | if (nargs >= 1) | |
1684 | VERIFY_CONSTANT (args[0]); | |
1685 | new_call | |
1686 | = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs, | |
1687 | args); | |
1688 | } | |
32c3a753 JJ |
1689 | else if (fndecl_built_in_p (fun, |
1690 | CP_BUILT_IN_IS_CORRESPONDING_MEMBER, | |
1691 | BUILT_IN_FRONTEND)) | |
1692 | { | |
1693 | location_t loc = EXPR_LOCATION (t); | |
1694 | if (nargs >= 2) | |
1695 | { | |
1696 | VERIFY_CONSTANT (args[0]); | |
1697 | VERIFY_CONSTANT (args[1]); | |
1698 | } | |
1699 | new_call = fold_builtin_is_corresponding_member (loc, nargs, args); | |
1700 | } | |
6cd005a2 JJ |
1701 | else |
1702 | new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t), | |
1703 | CALL_EXPR_FN (t), nargs, args); | |
43574e4f | 1704 | current_function_decl = save_cur_fn; |
5756d0f9 | 1705 | force_folding_builtin_constant_p = save_ffbcp; |
109d2197 JJ |
1706 | if (new_call == NULL) |
1707 | { | |
1708 | if (!*non_constant_p && !ctx->quiet) | |
1709 | { | |
d68d3664 JM |
1710 | new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t), |
1711 | CALL_EXPR_FN (t), nargs, args); | |
1712 | error ("%q+E is not a constant expression", new_call); | |
109d2197 JJ |
1713 | } |
1714 | *non_constant_p = true; | |
1715 | return t; | |
1716 | } | |
1717 | ||
43574e4f | 1718 | if (!potential_constant_expression (new_call)) |
109d2197 JJ |
1719 | { |
1720 | if (!*non_constant_p && !ctx->quiet) | |
1721 | error ("%q+E is not a constant expression", new_call); | |
1722 | *non_constant_p = true; | |
1723 | return t; | |
1724 | } | |
1725 | ||
69dc042f JM |
1726 | if (strret) |
1727 | { | |
1728 | /* memchr returns a pointer into the first argument, but we replaced the | |
1729 | argument above with a STRING_CST; put it back it now. */ | |
1730 | tree op = CALL_EXPR_ARG (t, strret-1); | |
1731 | STRIP_NOPS (new_call); | |
1732 | if (TREE_CODE (new_call) == POINTER_PLUS_EXPR) | |
1733 | TREE_OPERAND (new_call, 0) = op; | |
1734 | else if (TREE_CODE (new_call) == ADDR_EXPR) | |
1735 | new_call = op; | |
1736 | } | |
1737 | ||
109d2197 JJ |
1738 | return cxx_eval_constant_expression (&new_ctx, new_call, lval, |
1739 | non_constant_p, overflow_p); | |
2d76680f PC |
1740 | } |
1741 | ||
1742 | /* TEMP is the constant value of a temporary object of type TYPE. Adjust | |
1743 | the type of the value to match. */ | |
1744 | ||
1745 | static tree | |
1746 | adjust_temp_type (tree type, tree temp) | |
1747 | { | |
47be9509 | 1748 | if (same_type_p (TREE_TYPE (temp), type)) |
2d76680f PC |
1749 | return temp; |
1750 | /* Avoid wrapping an aggregate value in a NOP_EXPR. */ | |
1751 | if (TREE_CODE (temp) == CONSTRUCTOR) | |
b27f74e7 MP |
1752 | { |
1753 | /* build_constructor wouldn't retain various CONSTRUCTOR flags. */ | |
1754 | tree t = copy_node (temp); | |
1755 | TREE_TYPE (t) = type; | |
1756 | return t; | |
1757 | } | |
ff8ba86f JJ |
1758 | if (TREE_CODE (temp) == EMPTY_CLASS_EXPR) |
1759 | return build0 (EMPTY_CLASS_EXPR, type); | |
2d76680f | 1760 | gcc_assert (scalarish_type_p (type)); |
72b091f7 MP |
1761 | /* Now we know we're dealing with a scalar, and a prvalue of non-class |
1762 | type is cv-unqualified. */ | |
1763 | return cp_fold_convert (cv_unqualified (type), temp); | |
2d76680f PC |
1764 | } |
1765 | ||
9b9eb42a JM |
1766 | /* If T is a CONSTRUCTOR, return an unshared copy of T and any |
1767 | sub-CONSTRUCTORs. Otherwise return T. | |
0146e25f | 1768 | |
9b9eb42a JM |
1769 | We use this whenever we initialize an object as a whole, whether it's a |
1770 | parameter, a local variable, or a subobject, so that subsequent | |
1771 | modifications don't affect other places where it was used. */ | |
0146e25f | 1772 | |
1f6857ba | 1773 | tree |
3da7d774 | 1774 | unshare_constructor (tree t MEM_STAT_DECL) |
0146e25f | 1775 | { |
9b9eb42a JM |
1776 | if (!t || TREE_CODE (t) != CONSTRUCTOR) |
1777 | return t; | |
1778 | auto_vec <tree*, 4> ptrs; | |
1779 | ptrs.safe_push (&t); | |
1780 | while (!ptrs.is_empty ()) | |
1781 | { | |
1782 | tree *p = ptrs.pop (); | |
3da7d774 JM |
1783 | tree n = copy_node (*p PASS_MEM_STAT); |
1784 | CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT); | |
9b9eb42a JM |
1785 | *p = n; |
1786 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n); | |
1787 | constructor_elt *ce; | |
1788 | for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
6b7d53a2 | 1789 | if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR) |
9b9eb42a JM |
1790 | ptrs.safe_push (&ce->value); |
1791 | } | |
0146e25f PP |
1792 | return t; |
1793 | } | |
1794 | ||
620adbec JM |
1795 | /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */ |
1796 | ||
1797 | static void | |
1798 | free_constructor (tree t) | |
1799 | { | |
1800 | if (!t || TREE_CODE (t) != CONSTRUCTOR) | |
1801 | return; | |
1802 | releasing_vec ctors; | |
1803 | vec_safe_push (ctors, t); | |
1804 | while (!ctors->is_empty ()) | |
1805 | { | |
1806 | tree c = ctors->pop (); | |
1807 | if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c)) | |
1808 | { | |
1809 | constructor_elt *ce; | |
1810 | for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i) | |
22cda0ca | 1811 | if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR) |
620adbec JM |
1812 | vec_safe_push (ctors, ce->value); |
1813 | ggc_free (elts); | |
1814 | } | |
1815 | ggc_free (c); | |
1816 | } | |
1817 | } | |
1818 | ||
559d2f1e JJ |
1819 | /* Helper function of cxx_bind_parameters_in_call. Return non-NULL |
1820 | if *TP is address of a static variable (or part of it) currently being | |
1821 | constructed or of a heap artificial variable. */ | |
1822 | ||
1823 | static tree | |
1824 | addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data) | |
1825 | { | |
1826 | if (TREE_CODE (*tp) == ADDR_EXPR) | |
1827 | if (tree var = get_base_address (TREE_OPERAND (*tp, 0))) | |
1828 | if (VAR_P (var) && TREE_STATIC (var)) | |
1829 | { | |
1830 | if (DECL_NAME (var) == heap_uninit_identifier | |
1831 | || DECL_NAME (var) == heap_identifier | |
1832 | || DECL_NAME (var) == heap_vec_uninit_identifier | |
1833 | || DECL_NAME (var) == heap_vec_identifier) | |
1834 | return var; | |
1835 | ||
1836 | constexpr_global_ctx *global = (constexpr_global_ctx *) data; | |
e6a29aab | 1837 | if (global->get_value (var)) |
559d2f1e JJ |
1838 | return var; |
1839 | } | |
1840 | if (TYPE_P (*tp)) | |
1841 | *walk_subtrees = false; | |
1842 | return NULL_TREE; | |
1843 | } | |
1844 | ||
2d76680f PC |
1845 | /* Subroutine of cxx_eval_call_expression. |
1846 | We are processing a call expression (either CALL_EXPR or | |
3e605b20 | 1847 | AGGR_INIT_EXPR) in the context of CTX. Evaluate |
2d76680f PC |
1848 | all arguments and bind their values to correspondings |
1849 | parameters, making up the NEW_CALL context. */ | |
1850 | ||
1595fe44 JM |
1851 | static tree |
1852 | cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun, | |
12d9ce19 JM |
1853 | bool *non_constant_p, bool *overflow_p, |
1854 | bool *non_constant_args) | |
2d76680f PC |
1855 | { |
1856 | const int nargs = call_expr_nargs (t); | |
1595fe44 | 1857 | tree parms = DECL_ARGUMENTS (fun); |
2d76680f | 1858 | int i; |
3c961dc7 JM |
1859 | /* We don't record ellipsis args below. */ |
1860 | int nparms = list_length (parms); | |
1861 | int nbinds = nargs < nparms ? nargs : nparms; | |
1595fe44 | 1862 | tree binds = make_tree_vec (nbinds); |
2d76680f PC |
1863 | for (i = 0; i < nargs; ++i) |
1864 | { | |
1865 | tree x, arg; | |
1866 | tree type = parms ? TREE_TYPE (parms) : void_type_node; | |
1595fe44 JM |
1867 | if (parms && DECL_BY_REFERENCE (parms)) |
1868 | type = TREE_TYPE (type); | |
2d76680f | 1869 | x = get_nth_callarg (t, i); |
3e605b20 JM |
1870 | /* For member function, the first argument is a pointer to the implied |
1871 | object. For a constructor, it might still be a dummy object, in | |
60813a46 | 1872 | which case we get the real argument from ctx. */ |
3e605b20 JM |
1873 | if (i == 0 && DECL_CONSTRUCTOR_P (fun) |
1874 | && is_dummy_object (x)) | |
1875 | { | |
1876 | x = ctx->object; | |
84dd815f | 1877 | x = build_address (x); |
3e605b20 | 1878 | } |
43574e4f JJ |
1879 | if (TREE_ADDRESSABLE (type)) |
1880 | /* Undo convert_for_arg_passing work here. */ | |
1881 | x = convert_from_reference (x); | |
6c59b8a9 JM |
1882 | /* Normally we would strip a TARGET_EXPR in an initialization context |
1883 | such as this, but here we do the elision differently: we keep the | |
1884 | TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */ | |
72f76540 | 1885 | arg = cxx_eval_constant_expression (ctx, x, vc_prvalue, |
5a804683 | 1886 | non_constant_p, overflow_p); |
2d76680f | 1887 | /* Don't VERIFY_CONSTANT here. */ |
2b3ab879 | 1888 | if (*non_constant_p && ctx->quiet) |
1595fe44 | 1889 | break; |
2d76680f PC |
1890 | /* Just discard ellipsis args after checking their constantitude. */ |
1891 | if (!parms) | |
1892 | continue; | |
4727d4c6 NS |
1893 | |
1894 | if (!*non_constant_p) | |
1895 | { | |
1896 | /* Make sure the binding has the same type as the parm. But | |
1897 | only for constant args. */ | |
9f613f06 | 1898 | if (!TYPE_REF_P (type)) |
4727d4c6 NS |
1899 | arg = adjust_temp_type (type, arg); |
1900 | if (!TREE_CONSTANT (arg)) | |
1901 | *non_constant_args = true; | |
5afd90c5 JJ |
1902 | else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
1903 | /* The destructor needs to see any modifications the callee makes | |
1904 | to the argument. */ | |
1905 | *non_constant_args = true; | |
559d2f1e JJ |
1906 | /* If arg is or contains address of a heap artificial variable or |
1907 | of a static variable being constructed, avoid caching the | |
1908 | function call, as those variables might be modified by the | |
1909 | function, or might be modified by the callers in between | |
1910 | the cached function and just read by the function. */ | |
1911 | else if (!*non_constant_args | |
1912 | && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global, | |
1913 | NULL)) | |
1914 | *non_constant_args = true; | |
5afd90c5 | 1915 | |
5558a0da JJ |
1916 | /* For virtual calls, adjust the this argument, so that it is |
1917 | the object on which the method is called, rather than | |
1918 | one of its bases. */ | |
1919 | if (i == 0 && DECL_VIRTUAL_P (fun)) | |
1920 | { | |
1921 | tree addr = arg; | |
1922 | STRIP_NOPS (addr); | |
1923 | if (TREE_CODE (addr) == ADDR_EXPR) | |
1924 | { | |
1925 | tree obj = TREE_OPERAND (addr, 0); | |
1926 | while (TREE_CODE (obj) == COMPONENT_REF | |
1927 | && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1)) | |
1928 | && !same_type_ignoring_top_level_qualifiers_p | |
1929 | (TREE_TYPE (obj), DECL_CONTEXT (fun))) | |
1930 | obj = TREE_OPERAND (obj, 0); | |
1931 | if (obj != TREE_OPERAND (addr, 0)) | |
1932 | arg = build_fold_addr_expr_with_type (obj, | |
1933 | TREE_TYPE (arg)); | |
1934 | } | |
1935 | } | |
3c961dc7 | 1936 | TREE_VEC_ELT (binds, i) = arg; |
4727d4c6 | 1937 | } |
2d76680f PC |
1938 | parms = TREE_CHAIN (parms); |
1939 | } | |
1595fe44 JM |
1940 | |
1941 | return binds; | |
2d76680f PC |
1942 | } |
1943 | ||
1944 | /* Variables and functions to manage constexpr call expansion context. | |
1945 | These do not need to be marked for PCH or GC. */ | |
1946 | ||
1947 | /* FIXME remember and print actual constant arguments. */ | |
7de76362 | 1948 | static vec<tree> call_stack; |
2d76680f PC |
1949 | static int call_stack_tick; |
1950 | static int last_cx_error_tick; | |
1951 | ||
7ffc7de5 | 1952 | static int |
2d76680f PC |
1953 | push_cx_call_context (tree call) |
1954 | { | |
1955 | ++call_stack_tick; | |
1956 | if (!EXPR_HAS_LOCATION (call)) | |
1957 | SET_EXPR_LOCATION (call, input_location); | |
1958 | call_stack.safe_push (call); | |
7ffc7de5 JM |
1959 | int len = call_stack.length (); |
1960 | if (len > max_constexpr_depth) | |
2d76680f | 1961 | return false; |
7ffc7de5 | 1962 | return len; |
2d76680f PC |
1963 | } |
1964 | ||
1965 | static void | |
1966 | pop_cx_call_context (void) | |
1967 | { | |
1968 | ++call_stack_tick; | |
1969 | call_stack.pop (); | |
1970 | } | |
1971 | ||
1972 | vec<tree> | |
1973 | cx_error_context (void) | |
1974 | { | |
1975 | vec<tree> r = vNULL; | |
1976 | if (call_stack_tick != last_cx_error_tick | |
1977 | && !call_stack.is_empty ()) | |
1978 | r = call_stack; | |
1979 | last_cx_error_tick = call_stack_tick; | |
1980 | return r; | |
1981 | } | |
1982 | ||
ac8f92c1 JM |
1983 | /* E is an operand of a failed assertion, fold it either with or without |
1984 | constexpr context. */ | |
1985 | ||
1986 | static tree | |
1987 | fold_operand (tree e, const constexpr_ctx *ctx) | |
1988 | { | |
1989 | if (ctx) | |
1990 | { | |
1991 | bool new_non_constant_p = false, new_overflow_p = false; | |
1992 | e = cxx_eval_constant_expression (ctx, e, vc_prvalue, | |
1993 | &new_non_constant_p, | |
1994 | &new_overflow_p); | |
1995 | } | |
1996 | else | |
1997 | e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true); | |
1998 | return e; | |
1999 | } | |
2000 | ||
08b51bad JJ |
2001 | /* If we have a condition in conjunctive normal form (CNF), find the first |
2002 | failing clause. In other words, given an expression like | |
2003 | ||
2004 | true && true && false && true && false | |
2005 | ||
2006 | return the first 'false'. EXPR is the expression. */ | |
2007 | ||
2008 | static tree | |
ac8f92c1 | 2009 | find_failing_clause_r (const constexpr_ctx *ctx, tree expr) |
08b51bad JJ |
2010 | { |
2011 | if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR) | |
2012 | { | |
2013 | /* First check the left side... */ | |
2014 | tree e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 0)); | |
2015 | if (e == NULL_TREE) | |
2016 | /* ...if we didn't find a false clause, check the right side. */ | |
2017 | e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 1)); | |
2018 | return e; | |
2019 | } | |
4a54873d JM |
2020 | tree e = contextual_conv_bool (expr, tf_none); |
2021 | e = fold_operand (e, ctx); | |
08b51bad JJ |
2022 | if (integer_zerop (e)) |
2023 | /* This is the failing clause. */ | |
2024 | return expr; | |
2025 | return NULL_TREE; | |
2026 | } | |
2027 | ||
2028 | /* Wrapper for find_failing_clause_r. */ | |
2029 | ||
2030 | tree | |
ac8f92c1 | 2031 | find_failing_clause (const constexpr_ctx *ctx, tree expr) |
08b51bad JJ |
2032 | { |
2033 | if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR) | |
2034 | if (tree e = find_failing_clause_r (ctx, expr)) | |
2035 | expr = e; | |
2036 | return expr; | |
2037 | } | |
2038 | ||
ac8f92c1 JM |
2039 | /* Emit additional diagnostics for failing condition BAD. |
2040 | Used by finish_static_assert and IFN_ASSUME constexpr diagnostics. | |
2041 | If SHOW_EXPR_P is true, print the condition (because it was | |
2042 | instantiation-dependent). */ | |
2043 | ||
2044 | void | |
2045 | diagnose_failing_condition (tree bad, location_t cloc, bool show_expr_p, | |
2046 | const constexpr_ctx *ctx /* = nullptr */) | |
2047 | { | |
2048 | /* Nobody wants to see the artificial (bool) cast. */ | |
2049 | bad = tree_strip_nop_conversions (bad); | |
e6a29aab JM |
2050 | if (TREE_CODE (bad) == CLEANUP_POINT_EXPR) |
2051 | bad = TREE_OPERAND (bad, 0); | |
ac8f92c1 JM |
2052 | |
2053 | /* Actually explain the failure if this is a concept check or a | |
2054 | requires-expression. */ | |
2055 | if (concept_check_p (bad) || TREE_CODE (bad) == REQUIRES_EXPR) | |
2056 | diagnose_constraints (cloc, bad, NULL_TREE); | |
2057 | else if (COMPARISON_CLASS_P (bad) | |
2058 | && ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0)))) | |
2059 | { | |
2060 | tree op0 = fold_operand (TREE_OPERAND (bad, 0), ctx); | |
2061 | tree op1 = fold_operand (TREE_OPERAND (bad, 1), ctx); | |
2062 | tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1); | |
2063 | inform (cloc, "the comparison reduces to %qE", cond); | |
2064 | } | |
2065 | else if (show_expr_p) | |
2066 | inform (cloc, "%qE evaluates to false", bad); | |
2067 | } | |
2068 | ||
2efb237f JCI |
2069 | /* Process an assert/assume of ORIG_ARG. If it's not supposed to be evaluated, |
2070 | do it without changing the current evaluation state. If it evaluates to | |
2071 | false, complain and return false; otherwise, return true. */ | |
2072 | ||
2073 | static bool | |
2074 | cxx_eval_assert (const constexpr_ctx *ctx, tree arg, const char *msg, | |
2075 | location_t loc, bool evaluated, | |
2076 | bool *non_constant_p, bool *overflow_p) | |
2077 | { | |
2078 | if (*non_constant_p) | |
2079 | return true; | |
2080 | ||
2081 | tree eval; | |
2082 | if (!evaluated) | |
2083 | { | |
2084 | if (!potential_rvalue_constant_expression (arg)) | |
2085 | return true; | |
2086 | ||
2087 | constexpr_ctx new_ctx = *ctx; | |
2088 | new_ctx.quiet = true; | |
2089 | bool new_non_constant_p = false, new_overflow_p = false; | |
2090 | /* Avoid modification of existing values. */ | |
2091 | modifiable_tracker ms (new_ctx.global); | |
2092 | eval = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue, | |
2093 | &new_non_constant_p, | |
2094 | &new_overflow_p); | |
2095 | } | |
2096 | else | |
2097 | eval = cxx_eval_constant_expression (ctx, arg, vc_prvalue, | |
2098 | non_constant_p, | |
2099 | overflow_p); | |
2100 | if (!*non_constant_p && integer_zerop (eval)) | |
2101 | { | |
2102 | if (!ctx->quiet) | |
2103 | { | |
2104 | /* See if we can find which clause was failing | |
2105 | (for logical AND). */ | |
2106 | tree bad = find_failing_clause (ctx, arg); | |
2107 | /* If not, or its location is unusable, fall back to the | |
2108 | previous location. */ | |
2109 | location_t cloc = cp_expr_loc_or_loc (bad, loc); | |
2110 | ||
2111 | /* Report the error. */ | |
2112 | auto_diagnostic_group d; | |
2113 | error_at (cloc, msg); | |
2114 | diagnose_failing_condition (bad, cloc, true, ctx); | |
2115 | return bad; | |
2116 | } | |
2117 | *non_constant_p = true; | |
2118 | return false; | |
2119 | } | |
2120 | ||
2121 | return true; | |
2122 | } | |
2123 | ||
44a845ca MS |
2124 | /* Evaluate a call T to a GCC internal function when possible and return |
2125 | the evaluated result or, under the control of CTX, give an error, set | |
2126 | NON_CONSTANT_P, and return the unevaluated call T otherwise. */ | |
2127 | ||
2128 | static tree | |
2129 | cxx_eval_internal_function (const constexpr_ctx *ctx, tree t, | |
72f76540 | 2130 | value_cat lval, |
44a845ca MS |
2131 | bool *non_constant_p, bool *overflow_p) |
2132 | { | |
2133 | enum tree_code opcode = ERROR_MARK; | |
2134 | ||
2135 | switch (CALL_EXPR_IFN (t)) | |
2136 | { | |
2137 | case IFN_UBSAN_NULL: | |
2138 | case IFN_UBSAN_BOUNDS: | |
2139 | case IFN_UBSAN_VPTR: | |
81fea426 | 2140 | case IFN_FALLTHROUGH: |
44a845ca MS |
2141 | return void_node; |
2142 | ||
08b51bad | 2143 | case IFN_ASSUME: |
2efb237f JCI |
2144 | if (!cxx_eval_assert (ctx, CALL_EXPR_ARG (t, 0), |
2145 | G_("failed %<assume%> attribute assumption"), | |
2146 | EXPR_LOCATION (t), /*eval*/false, | |
2147 | non_constant_p, overflow_p)) | |
2148 | return t; | |
08b51bad JJ |
2149 | return void_node; |
2150 | ||
44a845ca MS |
2151 | case IFN_ADD_OVERFLOW: |
2152 | opcode = PLUS_EXPR; | |
2153 | break; | |
2154 | case IFN_SUB_OVERFLOW: | |
2155 | opcode = MINUS_EXPR; | |
2156 | break; | |
2157 | case IFN_MUL_OVERFLOW: | |
2158 | opcode = MULT_EXPR; | |
2159 | break; | |
2160 | ||
e16f1cc7 JJ |
2161 | case IFN_LAUNDER: |
2162 | return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), | |
72f76540 JM |
2163 | vc_prvalue, non_constant_p, |
2164 | overflow_p); | |
e16f1cc7 | 2165 | |
d8fcab68 JJ |
2166 | case IFN_VEC_CONVERT: |
2167 | { | |
2168 | tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), | |
72f76540 | 2169 | vc_prvalue, non_constant_p, |
d8fcab68 JJ |
2170 | overflow_p); |
2171 | if (TREE_CODE (arg) == VECTOR_CST) | |
84993d94 JJ |
2172 | if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg)) |
2173 | return r; | |
d8fcab68 | 2174 | } |
84993d94 | 2175 | /* FALLTHRU */ |
d8fcab68 | 2176 | |
44a845ca MS |
2177 | default: |
2178 | if (!ctx->quiet) | |
f9d0ca40 | 2179 | error_at (cp_expr_loc_or_input_loc (t), |
44a845ca MS |
2180 | "call to internal function %qE", t); |
2181 | *non_constant_p = true; | |
2182 | return t; | |
2183 | } | |
2184 | ||
2185 | /* Evaluate constant arguments using OPCODE and return a complex | |
2186 | number containing the result and the overflow bit. */ | |
2187 | tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval, | |
2188 | non_constant_p, overflow_p); | |
2189 | tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval, | |
2190 | non_constant_p, overflow_p); | |
2191 | ||
2192 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) | |
2193 | { | |
f9d0ca40 | 2194 | location_t loc = cp_expr_loc_or_input_loc (t); |
44a845ca MS |
2195 | tree type = TREE_TYPE (TREE_TYPE (t)); |
2196 | tree result = fold_binary_loc (loc, opcode, type, | |
2197 | fold_convert_loc (loc, type, arg0), | |
2198 | fold_convert_loc (loc, type, arg1)); | |
2199 | tree ovf | |
2200 | = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1)); | |
2201 | /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */ | |
2202 | if (TREE_OVERFLOW (result)) | |
2203 | TREE_OVERFLOW (result) = 0; | |
2204 | ||
2205 | return build_complex (TREE_TYPE (t), result, ovf); | |
2206 | } | |
2207 | ||
2208 | *non_constant_p = true; | |
2209 | return t; | |
2210 | } | |
2211 | ||
e8c48716 | 2212 | /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */ |
ecc57615 JM |
2213 | |
2214 | static void | |
2215 | clear_no_implicit_zero (tree ctor) | |
2216 | { | |
e8c48716 | 2217 | if (CONSTRUCTOR_NO_CLEARING (ctor)) |
ecc57615 | 2218 | { |
e8c48716 | 2219 | CONSTRUCTOR_NO_CLEARING (ctor) = false; |
fae00a0a JM |
2220 | for (auto &e: CONSTRUCTOR_ELTS (ctor)) |
2221 | if (TREE_CODE (e.value) == CONSTRUCTOR) | |
2222 | clear_no_implicit_zero (e.value); | |
ecc57615 JM |
2223 | } |
2224 | } | |
2225 | ||
04e1749c MP |
2226 | /* Complain about a const object OBJ being modified in a constant expression. |
2227 | EXPR is the MODIFY_EXPR expression performing the modification. */ | |
2228 | ||
2229 | static void | |
2230 | modifying_const_object_error (tree expr, tree obj) | |
2231 | { | |
2232 | location_t loc = cp_expr_loc_or_input_loc (expr); | |
2233 | auto_diagnostic_group d; | |
2234 | error_at (loc, "modifying a const object %qE is not allowed in " | |
2235 | "a constant expression", TREE_OPERAND (expr, 0)); | |
5ebe5bcf NS |
2236 | |
2237 | /* Find the underlying object that was declared as const. */ | |
2238 | location_t decl_loc = UNKNOWN_LOCATION; | |
2239 | for (tree probe = obj; decl_loc == UNKNOWN_LOCATION; ) | |
2240 | switch (TREE_CODE (probe)) | |
2241 | { | |
2242 | case BIT_FIELD_REF: | |
2243 | case COMPONENT_REF: | |
2244 | { | |
2245 | tree elt = TREE_OPERAND (probe, 1); | |
2246 | if (CP_TYPE_CONST_P (TREE_TYPE (elt))) | |
2247 | decl_loc = DECL_SOURCE_LOCATION (elt); | |
2248 | probe = TREE_OPERAND (probe, 0); | |
2249 | } | |
2250 | break; | |
2251 | ||
2252 | case ARRAY_REF: | |
2253 | case REALPART_EXPR: | |
2254 | case IMAGPART_EXPR: | |
2255 | probe = TREE_OPERAND (probe, 0); | |
2256 | break; | |
2257 | ||
2258 | default: | |
2259 | decl_loc = location_of (probe); | |
2260 | break; | |
2261 | } | |
2262 | inform (decl_loc, "originally declared %<const%> here"); | |
04e1749c MP |
2263 | } |
2264 | ||
8e007055 JJ |
2265 | /* Return true if FNDECL is a replaceable global allocation function that |
2266 | should be useable during constant expression evaluation. */ | |
2267 | ||
2268 | static inline bool | |
2269 | cxx_replaceable_global_alloc_fn (tree fndecl) | |
2270 | { | |
b04445d4 | 2271 | return (cxx_dialect >= cxx20 |
8e007055 | 2272 | && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl)) |
cf650568 JJ |
2273 | && CP_DECL_CONTEXT (fndecl) == global_namespace |
2274 | && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl) | |
2275 | || DECL_IS_OPERATOR_DELETE_P (fndecl))); | |
2276 | } | |
2277 | ||
2278 | /* Return true if FNDECL is a placement new function that should be | |
2279 | useable during constant expression evaluation of std::construct_at. */ | |
2280 | ||
2281 | static inline bool | |
2282 | cxx_placement_new_fn (tree fndecl) | |
2283 | { | |
b04445d4 | 2284 | if (cxx_dialect >= cxx20 |
cf650568 JJ |
2285 | && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl)) |
2286 | && CP_DECL_CONTEXT (fndecl) == global_namespace | |
2287 | && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl) | |
2288 | && TREE_CODE (TREE_TYPE (fndecl)) == FUNCTION_TYPE) | |
2289 | { | |
2290 | tree first_arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl))); | |
2291 | if (TREE_VALUE (first_arg) == ptr_type_node | |
2292 | && TREE_CHAIN (first_arg) == void_list_node) | |
2293 | return true; | |
2294 | } | |
2295 | return false; | |
2296 | } | |
2297 | ||
2298 | /* Return true if FNDECL is std::construct_at. */ | |
2299 | ||
2300 | static inline bool | |
2301 | is_std_construct_at (tree fndecl) | |
2302 | { | |
2303 | if (!decl_in_std_namespace_p (fndecl)) | |
2304 | return false; | |
2305 | ||
2306 | tree name = DECL_NAME (fndecl); | |
2307 | return name && id_equal (name, "construct_at"); | |
8e007055 JJ |
2308 | } |
2309 | ||
2ffc2645 MP |
2310 | /* Overload for the above taking constexpr_call*. */ |
2311 | ||
2312 | static inline bool | |
2313 | is_std_construct_at (const constexpr_call *call) | |
2314 | { | |
2315 | return (call | |
2316 | && call->fundef | |
2317 | && is_std_construct_at (call->fundef->decl)); | |
2318 | } | |
2319 | ||
1e184761 | 2320 | /* True if CTX is an instance of std::allocator. */ |
8412b939 | 2321 | |
1e184761 JM |
2322 | bool |
2323 | is_std_allocator (tree ctx) | |
8412b939 | 2324 | { |
8412b939 JJ |
2325 | if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx)) |
2326 | return false; | |
2327 | ||
2328 | tree decl = TYPE_MAIN_DECL (ctx); | |
1e184761 | 2329 | tree name = DECL_NAME (decl); |
8412b939 JJ |
2330 | if (name == NULL_TREE || !id_equal (name, "allocator")) |
2331 | return false; | |
2332 | ||
2333 | return decl_in_std_namespace_p (decl); | |
2334 | } | |
2335 | ||
1e184761 JM |
2336 | /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */ |
2337 | ||
2338 | static inline bool | |
2339 | is_std_allocator_allocate (tree fndecl) | |
2340 | { | |
2341 | tree name = DECL_NAME (fndecl); | |
2342 | if (name == NULL_TREE | |
2343 | || !(id_equal (name, "allocate") || id_equal (name, "deallocate"))) | |
2344 | return false; | |
2345 | ||
2346 | return is_std_allocator (DECL_CONTEXT (fndecl)); | |
2347 | } | |
2348 | ||
2ffc2645 MP |
2349 | /* Overload for the above taking constexpr_call*. */ |
2350 | ||
2351 | static inline bool | |
2352 | is_std_allocator_allocate (const constexpr_call *call) | |
2353 | { | |
2354 | return (call | |
2355 | && call->fundef | |
2356 | && is_std_allocator_allocate (call->fundef->decl)); | |
2357 | } | |
2358 | ||
b69ee500 NS |
2359 | /* Return true if FNDECL is std::source_location::current. */ |
2360 | ||
2361 | static inline bool | |
2362 | is_std_source_location_current (tree fndecl) | |
2363 | { | |
2364 | if (!decl_in_std_namespace_p (fndecl)) | |
2365 | return false; | |
2366 | ||
2367 | tree name = DECL_NAME (fndecl); | |
2368 | if (name == NULL_TREE || !id_equal (name, "current")) | |
2369 | return false; | |
2370 | ||
2371 | tree ctx = DECL_CONTEXT (fndecl); | |
2372 | if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx)) | |
2373 | return false; | |
2374 | ||
2375 | name = DECL_NAME (TYPE_MAIN_DECL (ctx)); | |
2376 | return name && id_equal (name, "source_location"); | |
2377 | } | |
2378 | ||
2379 | /* Overload for the above taking constexpr_call*. */ | |
2380 | ||
2381 | static inline bool | |
2382 | is_std_source_location_current (const constexpr_call *call) | |
2383 | { | |
2384 | return (call | |
2385 | && call->fundef | |
2386 | && is_std_source_location_current (call->fundef->decl)); | |
2387 | } | |
2388 | ||
22edf943 MP |
2389 | /* Return true if FNDECL is __dynamic_cast. */ |
2390 | ||
2391 | static inline bool | |
2392 | cxx_dynamic_cast_fn_p (tree fndecl) | |
2393 | { | |
b04445d4 | 2394 | return (cxx_dialect >= cxx20 |
22edf943 | 2395 | && id_equal (DECL_NAME (fndecl), "__dynamic_cast") |
d6a488f2 | 2396 | && CP_DECL_CONTEXT (fndecl) == abi_node); |
22edf943 MP |
2397 | } |
2398 | ||
2399 | /* Often, we have an expression in the form of address + offset, e.g. | |
2400 | "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */ | |
2401 | ||
2402 | static tree | |
2403 | extract_obj_from_addr_offset (tree expr) | |
2404 | { | |
2405 | if (TREE_CODE (expr) == POINTER_PLUS_EXPR) | |
2406 | expr = TREE_OPERAND (expr, 0); | |
2407 | STRIP_NOPS (expr); | |
2408 | if (TREE_CODE (expr) == ADDR_EXPR) | |
2409 | expr = TREE_OPERAND (expr, 0); | |
2410 | return expr; | |
2411 | } | |
2412 | ||
2413 | /* Given a PATH like | |
2414 | ||
2415 | g.D.2181.D.2154.D.2102.D.2093 | |
2416 | ||
2417 | find a component with type TYPE. Return NULL_TREE if not found, and | |
2418 | error_mark_node if the component is not accessible. If STOP is non-null, | |
2419 | this function will return NULL_TREE if STOP is found before TYPE. */ | |
2420 | ||
2421 | static tree | |
2422 | get_component_with_type (tree path, tree type, tree stop) | |
2423 | { | |
2424 | while (true) | |
2425 | { | |
2426 | if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type)) | |
2427 | /* Found it. */ | |
2428 | return path; | |
2429 | else if (stop | |
2430 | && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), | |
2431 | stop))) | |
2432 | return NULL_TREE; | |
2433 | else if (TREE_CODE (path) == COMPONENT_REF | |
2434 | && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1))) | |
2435 | { | |
2436 | /* We need to check that the component we're accessing is in fact | |
2437 | accessible. */ | |
2438 | if (TREE_PRIVATE (TREE_OPERAND (path, 1)) | |
2439 | || TREE_PROTECTED (TREE_OPERAND (path, 1))) | |
2440 | return error_mark_node; | |
2441 | path = TREE_OPERAND (path, 0); | |
2442 | } | |
2443 | else | |
2444 | return NULL_TREE; | |
2445 | } | |
2446 | } | |
2447 | ||
2448 | /* Evaluate a call to __dynamic_cast (permitted by P1327R1). | |
2449 | ||
2450 | The declaration of __dynamic_cast is: | |
2451 | ||
2452 | void* __dynamic_cast (const void* __src_ptr, | |
2453 | const __class_type_info* __src_type, | |
2454 | const __class_type_info* __dst_type, | |
2455 | ptrdiff_t __src2dst); | |
2456 | ||
2457 | where src2dst has the following possible values | |
2458 | ||
2459 | >-1: src_type is a unique public non-virtual base of dst_type | |
2460 | dst_ptr + src2dst == src_ptr | |
2461 | -1: unspecified relationship | |
2462 | -2: src_type is not a public base of dst_type | |
2463 | -3: src_type is a multiple public non-virtual base of dst_type | |
2464 | ||
2465 | Since literal types can't have virtual bases, we only expect hint >=0, | |
2466 | -2, or -3. */ | |
2467 | ||
2468 | static tree | |
2469 | cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call, | |
2470 | bool *non_constant_p, bool *overflow_p) | |
2471 | { | |
2472 | /* T will be something like | |
2473 | __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8) | |
2474 | dismantle it. */ | |
2475 | gcc_assert (call_expr_nargs (call) == 4); | |
2476 | tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; | |
2477 | tree obj = CALL_EXPR_ARG (call, 0); | |
2478 | tree type = CALL_EXPR_ARG (call, 2); | |
2479 | HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3)); | |
2480 | location_t loc = cp_expr_loc_or_input_loc (call); | |
2481 | ||
2482 | /* Get the target type of the dynamic_cast. */ | |
2483 | gcc_assert (TREE_CODE (type) == ADDR_EXPR); | |
2484 | type = TREE_OPERAND (type, 0); | |
2485 | type = TREE_TYPE (DECL_NAME (type)); | |
2486 | ||
2487 | /* TYPE can only be either T* or T&. We can't know which of these it | |
2488 | is by looking at TYPE, but OBJ will be "(T*) x" in the first case, | |
2489 | and something like "(T*)(T&)(T*) x" in the second case. */ | |
2490 | bool reference_p = false; | |
2491 | while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR) | |
2492 | { | |
2493 | reference_p |= TYPE_REF_P (TREE_TYPE (obj)); | |
2494 | obj = TREE_OPERAND (obj, 0); | |
2495 | } | |
2496 | ||
2497 | /* Evaluate the object so that we know its dynamic type. */ | |
72f76540 | 2498 | obj = cxx_eval_constant_expression (ctx, obj, vc_prvalue, non_constant_p, |
22edf943 MP |
2499 | overflow_p); |
2500 | if (*non_constant_p) | |
2501 | return call; | |
2502 | ||
2503 | /* We expect OBJ to be in form of &d.D.2102 when HINT == 0, | |
2504 | but when HINT is > 0, it can also be something like | |
2505 | &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */ | |
2506 | obj = extract_obj_from_addr_offset (obj); | |
2507 | const tree objtype = TREE_TYPE (obj); | |
2508 | /* If OBJ doesn't refer to a base field, we're done. */ | |
2509 | if (tree t = (TREE_CODE (obj) == COMPONENT_REF | |
2510 | ? TREE_OPERAND (obj, 1) : obj)) | |
2511 | if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t)) | |
de0684bf MP |
2512 | { |
2513 | if (reference_p) | |
2514 | { | |
2515 | if (!ctx->quiet) | |
2516 | { | |
4f870007 | 2517 | auto_diagnostic_group d; |
de0684bf MP |
2518 | error_at (loc, "reference %<dynamic_cast%> failed"); |
2519 | inform (loc, "dynamic type %qT of its operand does " | |
2520 | "not have a base class of type %qT", | |
2521 | objtype, type); | |
2522 | } | |
2523 | *non_constant_p = true; | |
2524 | } | |
2525 | return integer_zero_node; | |
2526 | } | |
22edf943 MP |
2527 | |
2528 | /* [class.cdtor] When a dynamic_cast is used in a constructor ... | |
2529 | or in a destructor ... if the operand of the dynamic_cast refers | |
2530 | to the object under construction or destruction, this object is | |
2531 | considered to be a most derived object that has the type of the | |
2532 | constructor or destructor's class. */ | |
0221c656 | 2533 | tree vtable = build_vfield_ref (obj, objtype); |
72f76540 | 2534 | vtable = cxx_eval_constant_expression (ctx, vtable, vc_prvalue, |
22edf943 MP |
2535 | non_constant_p, overflow_p); |
2536 | if (*non_constant_p) | |
2537 | return call; | |
0221c656 MP |
2538 | /* With -fsanitize=vptr, we initialize all vtable pointers to null, |
2539 | so it's possible that we got a null pointer now. */ | |
2540 | if (integer_zerop (vtable)) | |
2541 | { | |
2542 | if (!ctx->quiet) | |
2543 | error_at (loc, "virtual table pointer is used uninitialized"); | |
2544 | *non_constant_p = true; | |
2545 | return integer_zero_node; | |
2546 | } | |
22edf943 MP |
2547 | /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */ |
2548 | vtable = extract_obj_from_addr_offset (vtable); | |
2549 | const tree mdtype = DECL_CONTEXT (vtable); | |
2550 | ||
2551 | /* Given dynamic_cast<T>(v), | |
2552 | ||
2553 | [expr.dynamic.cast] If C is the class type to which T points or refers, | |
2554 | the runtime check logically executes as follows: | |
2555 | ||
2556 | If, in the most derived object pointed (referred) to by v, v points | |
2557 | (refers) to a public base class subobject of a C object, and if only | |
2558 | one object of type C is derived from the subobject pointed (referred) | |
2559 | to by v the result points (refers) to that C object. | |
2560 | ||
2561 | In this case, HINT >= 0 or -3. */ | |
2562 | if (hint >= 0 || hint == -3) | |
2563 | { | |
2564 | /* Look for a component with type TYPE. */ | |
2565 | tree t = get_component_with_type (obj, type, mdtype); | |
2566 | /* If not accessible, give an error. */ | |
2567 | if (t == error_mark_node) | |
2568 | { | |
2569 | if (reference_p) | |
2570 | { | |
2571 | if (!ctx->quiet) | |
2572 | { | |
4f870007 | 2573 | auto_diagnostic_group d; |
22edf943 MP |
2574 | error_at (loc, "reference %<dynamic_cast%> failed"); |
2575 | inform (loc, "static type %qT of its operand is a " | |
2576 | "non-public base class of dynamic type %qT", | |
2577 | objtype, type); | |
2578 | ||
2579 | } | |
2580 | *non_constant_p = true; | |
2581 | } | |
2582 | return integer_zero_node; | |
2583 | } | |
2584 | else if (t) | |
2585 | /* The result points to the TYPE object. */ | |
2586 | return cp_build_addr_expr (t, complain); | |
2587 | /* Else, TYPE was not found, because the HINT turned out to be wrong. | |
2588 | Fall through to the normal processing. */ | |
2589 | } | |
2590 | ||
2591 | /* Otherwise, if v points (refers) to a public base class subobject of the | |
2592 | most derived object, and the type of the most derived object has a base | |
2593 | class, of type C, that is unambiguous and public, the result points | |
2594 | (refers) to the C subobject of the most derived object. | |
2595 | ||
2596 | But it can also be an invalid case. */ | |
2597 | ||
2598 | /* Get the most derived object. */ | |
2599 | obj = get_component_with_type (obj, mdtype, NULL_TREE); | |
2600 | if (obj == error_mark_node) | |
2601 | { | |
2602 | if (reference_p) | |
2603 | { | |
2604 | if (!ctx->quiet) | |
2605 | { | |
4f870007 | 2606 | auto_diagnostic_group d; |
22edf943 MP |
2607 | error_at (loc, "reference %<dynamic_cast%> failed"); |
2608 | inform (loc, "static type %qT of its operand is a non-public" | |
2609 | " base class of dynamic type %qT", objtype, mdtype); | |
2610 | } | |
2611 | *non_constant_p = true; | |
2612 | } | |
2613 | return integer_zero_node; | |
2614 | } | |
2615 | else | |
2616 | gcc_assert (obj); | |
2617 | ||
2618 | /* Check that the type of the most derived object has a base class | |
2619 | of type TYPE that is unambiguous and public. */ | |
2620 | base_kind b_kind; | |
2621 | tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none); | |
2622 | if (!binfo || binfo == error_mark_node) | |
2623 | { | |
2624 | if (reference_p) | |
2625 | { | |
2626 | if (!ctx->quiet) | |
2627 | { | |
4f870007 | 2628 | auto_diagnostic_group d; |
22edf943 MP |
2629 | error_at (loc, "reference %<dynamic_cast%> failed"); |
2630 | if (b_kind == bk_ambig) | |
2631 | inform (loc, "%qT is an ambiguous base class of dynamic " | |
2632 | "type %qT of its operand", type, mdtype); | |
2633 | else | |
2634 | inform (loc, "dynamic type %qT of its operand does not " | |
2635 | "have an unambiguous public base class %qT", | |
2636 | mdtype, type); | |
2637 | } | |
2638 | *non_constant_p = true; | |
2639 | } | |
2640 | return integer_zero_node; | |
2641 | } | |
2642 | /* If so, return the TYPE subobject of the most derived object. */ | |
2643 | obj = convert_to_base_statically (obj, binfo); | |
2644 | return cp_build_addr_expr (obj, complain); | |
2645 | } | |
2646 | ||
4f6bc28f | 2647 | /* Data structure used by replace_decl and replace_decl_r. */ |
b2562229 | 2648 | |
4f6bc28f | 2649 | struct replace_decl_data |
b2562229 | 2650 | { |
4f6bc28f | 2651 | /* The _DECL we want to replace. */ |
b2562229 PP |
2652 | tree decl; |
2653 | /* The replacement for DECL. */ | |
2654 | tree replacement; | |
4f6bc28f JM |
2655 | /* Trees we've visited. */ |
2656 | hash_set<tree> *pset; | |
b2562229 PP |
2657 | /* Whether we've performed any replacements. */ |
2658 | bool changed; | |
2659 | }; | |
2660 | ||
4f6bc28f | 2661 | /* Helper function for replace_decl, called through cp_walk_tree. */ |
b2562229 PP |
2662 | |
2663 | static tree | |
4f6bc28f | 2664 | replace_decl_r (tree *tp, int *walk_subtrees, void *data) |
b2562229 | 2665 | { |
4f6bc28f | 2666 | replace_decl_data *d = (replace_decl_data *) data; |
b2562229 PP |
2667 | |
2668 | if (*tp == d->decl) | |
2669 | { | |
2670 | *tp = unshare_expr (d->replacement); | |
2671 | d->changed = true; | |
2672 | *walk_subtrees = 0; | |
2673 | } | |
4f6bc28f JM |
2674 | else if (TYPE_P (*tp) |
2675 | || d->pset->add (*tp)) | |
b2562229 PP |
2676 | *walk_subtrees = 0; |
2677 | ||
2678 | return NULL_TREE; | |
2679 | } | |
2680 | ||
4f6bc28f JM |
2681 | /* Replace every occurrence of DECL with (an unshared copy of) |
2682 | REPLACEMENT within the expression *TP. Returns true iff a | |
b2562229 PP |
2683 | replacement was performed. */ |
2684 | ||
4f6bc28f JM |
2685 | bool |
2686 | replace_decl (tree *tp, tree decl, tree replacement) | |
b2562229 | 2687 | { |
4f6bc28f JM |
2688 | gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p |
2689 | (TREE_TYPE (decl), TREE_TYPE (replacement))); | |
2690 | hash_set<tree> pset; | |
2691 | replace_decl_data data = { decl, replacement, &pset, false }; | |
2692 | cp_walk_tree (tp, replace_decl_r, &data, NULL); | |
b2562229 PP |
2693 | return data.changed; |
2694 | } | |
2695 | ||
e6321c45 JM |
2696 | /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */ |
2697 | ||
2698 | static tree | |
2699 | cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl, | |
72f76540 | 2700 | value_cat lval, |
e6321c45 JM |
2701 | bool *non_constant_p, bool *overflow_p) |
2702 | { | |
2703 | tree function = THUNK_TARGET (thunk_fndecl); | |
2704 | ||
e6321c45 JM |
2705 | if (THUNK_VIRTUAL_OFFSET (thunk_fndecl)) |
2706 | { | |
e6321c45 JM |
2707 | if (!ctx->quiet) |
2708 | { | |
267318a2 JM |
2709 | if (!DECL_DECLARED_CONSTEXPR_P (function)) |
2710 | { | |
2711 | error ("call to non-%<constexpr%> function %qD", function); | |
2712 | explain_invalid_constexpr_fn (function); | |
2713 | } | |
2714 | else | |
2715 | /* virtual_offset is only set for virtual bases, which make the | |
2716 | class non-literal, so we don't need to handle it here. */ | |
2717 | error ("calling constexpr member function %qD through virtual " | |
2718 | "base subobject", function); | |
e6321c45 JM |
2719 | } |
2720 | *non_constant_p = true; | |
2721 | return t; | |
2722 | } | |
2723 | ||
2724 | tree new_call = copy_node (t); | |
2725 | CALL_EXPR_FN (new_call) = function; | |
2726 | TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function)); | |
2727 | ||
2728 | tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl)); | |
2729 | ||
2730 | if (DECL_THIS_THUNK_P (thunk_fndecl)) | |
2731 | { | |
2732 | /* 'this'-adjusting thunk. */ | |
2733 | tree this_arg = CALL_EXPR_ARG (t, 0); | |
2734 | this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg), | |
2735 | this_arg, offset); | |
2736 | CALL_EXPR_ARG (new_call, 0) = this_arg; | |
2737 | } | |
2738 | else | |
2739 | /* Return-adjusting thunk. */ | |
2740 | new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call), | |
2741 | new_call, offset); | |
2742 | ||
2743 | return cxx_eval_constant_expression (ctx, new_call, lval, | |
2744 | non_constant_p, overflow_p); | |
2745 | } | |
2746 | ||
caf17f3a MP |
2747 | /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set |
2748 | its TREE_READONLY flag according to READONLY_P. Used for constexpr | |
2749 | 'tors to detect modifying const objects in a constexpr context. */ | |
2750 | ||
2751 | static void | |
2752 | cxx_set_object_constness (const constexpr_ctx *ctx, tree object, | |
2753 | bool readonly_p, bool *non_constant_p, | |
2754 | bool *overflow_p) | |
2755 | { | |
2756 | if (CLASS_TYPE_P (TREE_TYPE (object)) | |
2757 | && CP_TYPE_CONST_P (TREE_TYPE (object))) | |
2758 | { | |
2759 | /* Subobjects might not be stored in ctx->global->values but we | |
2760 | can get its CONSTRUCTOR by evaluating *this. */ | |
72f76540 | 2761 | tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue, |
caf17f3a MP |
2762 | non_constant_p, overflow_p); |
2763 | if (TREE_CODE (e) == CONSTRUCTOR && !*non_constant_p) | |
2764 | TREE_READONLY (e) = readonly_p; | |
2765 | } | |
2766 | } | |
2767 | ||
2d76680f PC |
2768 | /* Subroutine of cxx_eval_constant_expression. |
2769 | Evaluate the call expression tree T in the context of OLD_CALL expression | |
2770 | evaluation. */ | |
2771 | ||
2772 | static tree | |
3e605b20 | 2773 | cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, |
72f76540 | 2774 | value_cat lval, |
2d76680f PC |
2775 | bool *non_constant_p, bool *overflow_p) |
2776 | { | |
861d4af8 AS |
2777 | /* Handle concept checks separately. */ |
2778 | if (concept_check_p (t)) | |
662ef5b5 | 2779 | return evaluate_concept_check (t); |
861d4af8 | 2780 | |
f9d0ca40 | 2781 | location_t loc = cp_expr_loc_or_input_loc (t); |
2d76680f | 2782 | tree fun = get_function_named_in_call (t); |
cce3ae91 | 2783 | constexpr_call new_call |
13de99bc | 2784 | = { NULL, NULL, NULL, 0, ctx->manifestly_const_eval }; |
7ffc7de5 | 2785 | int depth_ok; |
2d76680f | 2786 | |
0b274c17 | 2787 | if (fun == NULL_TREE) |
44a845ca MS |
2788 | return cxx_eval_internal_function (ctx, t, lval, |
2789 | non_constant_p, overflow_p); | |
0b274c17 | 2790 | |
2d76680f PC |
2791 | if (TREE_CODE (fun) != FUNCTION_DECL) |
2792 | { | |
2793 | /* Might be a constexpr function pointer. */ | |
72f76540 JM |
2794 | fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue, |
2795 | non_constant_p, overflow_p); | |
2d76680f PC |
2796 | STRIP_NOPS (fun); |
2797 | if (TREE_CODE (fun) == ADDR_EXPR) | |
2798 | fun = TREE_OPERAND (fun, 0); | |
582d2481 JJ |
2799 | /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no |
2800 | indirection, the called expression is a pointer into the | |
2801 | virtual table which should contain FDESC_EXPR. Extract the | |
2802 | FUNCTION_DECL from there. */ | |
2803 | else if (TARGET_VTABLE_USES_DESCRIPTORS | |
2804 | && TREE_CODE (fun) == POINTER_PLUS_EXPR | |
2805 | && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR | |
2806 | && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST) | |
2807 | { | |
2808 | tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0); | |
2809 | if (VAR_P (d) | |
2810 | && DECL_VTABLE_OR_VTT_P (d) | |
2811 | && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE | |
2812 | && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type | |
2813 | && DECL_INITIAL (d) | |
2814 | && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR) | |
2815 | { | |
2816 | tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1), | |
2817 | TYPE_SIZE_UNIT (vtable_entry_type)); | |
2818 | HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i); | |
2819 | if (idx >= 0) | |
2820 | { | |
2821 | tree fdesc | |
2822 | = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value; | |
2823 | if (TREE_CODE (fdesc) == FDESC_EXPR | |
2824 | && integer_zerop (TREE_OPERAND (fdesc, 1))) | |
2825 | fun = TREE_OPERAND (fdesc, 0); | |
2826 | } | |
2827 | } | |
2828 | } | |
2d76680f PC |
2829 | } |
2830 | if (TREE_CODE (fun) != FUNCTION_DECL) | |
2831 | { | |
2b3ab879 | 2832 | if (!ctx->quiet && !*non_constant_p) |
84fa214d | 2833 | error_at (loc, "expression %qE does not designate a %<constexpr%> " |
2d76680f PC |
2834 | "function", fun); |
2835 | *non_constant_p = true; | |
2836 | return t; | |
2837 | } | |
81f97181 | 2838 | if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)) |
2d76680f | 2839 | fun = DECL_CLONED_FUNCTION (fun); |
0b274c17 MP |
2840 | |
2841 | if (is_ubsan_builtin_p (fun)) | |
2842 | return void_node; | |
2843 | ||
3d78e008 | 2844 | if (fndecl_built_in_p (fun)) |
5756d0f9 | 2845 | return cxx_eval_builtin_function_call (ctx, t, fun, |
92a596e8 | 2846 | lval, non_constant_p, overflow_p); |
e6321c45 JM |
2847 | if (DECL_THUNK_P (fun)) |
2848 | return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p); | |
87c2080b | 2849 | if (!maybe_constexpr_fn (fun)) |
2d76680f | 2850 | { |
8412b939 JJ |
2851 | if (TREE_CODE (t) == CALL_EXPR |
2852 | && cxx_replaceable_global_alloc_fn (fun) | |
2853 | && (CALL_FROM_NEW_OR_DELETE_P (t) | |
2ffc2645 | 2854 | || is_std_allocator_allocate (ctx->call))) |
8e007055 | 2855 | { |
90bc2d09 | 2856 | const bool new_op_p = IDENTIFIER_NEW_OP_P (DECL_NAME (fun)); |
8e007055 JJ |
2857 | const int nargs = call_expr_nargs (t); |
2858 | tree arg0 = NULL_TREE; | |
2859 | for (int i = 0; i < nargs; ++i) | |
2860 | { | |
2861 | tree arg = CALL_EXPR_ARG (t, i); | |
72f76540 | 2862 | arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue, |
8e007055 | 2863 | non_constant_p, overflow_p); |
90bc2d09 NS |
2864 | /* Deleting a non-constant pointer has a better error message |
2865 | below. */ | |
2866 | if (new_op_p || i != 0) | |
2867 | VERIFY_CONSTANT (arg); | |
8e007055 JJ |
2868 | if (i == 0) |
2869 | arg0 = arg; | |
2870 | } | |
2871 | gcc_assert (arg0); | |
90bc2d09 | 2872 | if (new_op_p) |
8e007055 JJ |
2873 | { |
2874 | tree type = build_array_type_nelts (char_type_node, | |
2875 | tree_to_uhwi (arg0)); | |
815baade JJ |
2876 | tree var = build_decl (loc, VAR_DECL, |
2877 | (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) | |
2878 | & OVL_OP_FLAG_VEC) | |
2879 | ? heap_vec_uninit_identifier | |
2880 | : heap_uninit_identifier, | |
8e007055 JJ |
2881 | type); |
2882 | DECL_ARTIFICIAL (var) = 1; | |
2883 | TREE_STATIC (var) = 1; | |
a8db7887 JJ |
2884 | // Temporarily register the artificial var in varpool, |
2885 | // so that comparisons of its address against NULL are folded | |
2886 | // through nonzero_address even with | |
2887 | // -fno-delete-null-pointer-checks or that comparison of | |
2888 | // addresses of different heap artificial vars is folded too. | |
2889 | // See PR98988 and PR99031. | |
2890 | varpool_node::finalize_decl (var); | |
8e007055 | 2891 | ctx->global->heap_vars.safe_push (var); |
e6a29aab | 2892 | ctx->global->put_value (var, NULL_TREE); |
8e007055 JJ |
2893 | return fold_convert (ptr_type_node, build_address (var)); |
2894 | } | |
2895 | else | |
2896 | { | |
2897 | STRIP_NOPS (arg0); | |
2898 | if (TREE_CODE (arg0) == ADDR_EXPR | |
2899 | && VAR_P (TREE_OPERAND (arg0, 0))) | |
2900 | { | |
2901 | tree var = TREE_OPERAND (arg0, 0); | |
2902 | if (DECL_NAME (var) == heap_uninit_identifier | |
2903 | || DECL_NAME (var) == heap_identifier) | |
2904 | { | |
815baade JJ |
2905 | if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) |
2906 | & OVL_OP_FLAG_VEC) | |
2907 | { | |
2908 | if (!ctx->quiet) | |
2909 | { | |
4f870007 | 2910 | auto_diagnostic_group d; |
815baade JJ |
2911 | error_at (loc, "array deallocation of object " |
2912 | "allocated with non-array " | |
2913 | "allocation"); | |
2914 | inform (DECL_SOURCE_LOCATION (var), | |
2915 | "allocation performed here"); | |
2916 | } | |
2917 | *non_constant_p = true; | |
2918 | return t; | |
2919 | } | |
2920 | DECL_NAME (var) = heap_deleted_identifier; | |
90bc2d09 | 2921 | ctx->global->destroy_value (var); |
815baade JJ |
2922 | ctx->global->heap_dealloc_count++; |
2923 | return void_node; | |
2924 | } | |
2925 | else if (DECL_NAME (var) == heap_vec_uninit_identifier | |
2926 | || DECL_NAME (var) == heap_vec_identifier) | |
2927 | { | |
2928 | if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) | |
2929 | & OVL_OP_FLAG_VEC) == 0) | |
2930 | { | |
2931 | if (!ctx->quiet) | |
2932 | { | |
4f870007 | 2933 | auto_diagnostic_group d; |
815baade JJ |
2934 | error_at (loc, "non-array deallocation of " |
2935 | "object allocated with array " | |
2936 | "allocation"); | |
2937 | inform (DECL_SOURCE_LOCATION (var), | |
2938 | "allocation performed here"); | |
2939 | } | |
2940 | *non_constant_p = true; | |
2941 | return t; | |
2942 | } | |
8e007055 | 2943 | DECL_NAME (var) = heap_deleted_identifier; |
90bc2d09 | 2944 | ctx->global->destroy_value (var); |
f74f6092 | 2945 | ctx->global->heap_dealloc_count++; |
8e007055 JJ |
2946 | return void_node; |
2947 | } | |
2948 | else if (DECL_NAME (var) == heap_deleted_identifier) | |
2949 | { | |
2950 | if (!ctx->quiet) | |
2951 | error_at (loc, "deallocation of already deallocated " | |
2952 | "storage"); | |
2953 | *non_constant_p = true; | |
2954 | return t; | |
2955 | } | |
2956 | } | |
2957 | if (!ctx->quiet) | |
2958 | error_at (loc, "deallocation of storage that was " | |
2959 | "not previously allocated"); | |
2960 | *non_constant_p = true; | |
2961 | return t; | |
2962 | } | |
2963 | } | |
cf650568 JJ |
2964 | /* Allow placement new in std::construct_at, just return the second |
2965 | argument. */ | |
8412b939 JJ |
2966 | if (TREE_CODE (t) == CALL_EXPR |
2967 | && cxx_placement_new_fn (fun) | |
2ffc2645 | 2968 | && is_std_construct_at (ctx->call)) |
cf650568 JJ |
2969 | { |
2970 | const int nargs = call_expr_nargs (t); | |
2971 | tree arg1 = NULL_TREE; | |
2972 | for (int i = 0; i < nargs; ++i) | |
2973 | { | |
2974 | tree arg = CALL_EXPR_ARG (t, i); | |
72f76540 | 2975 | arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue, |
cf650568 | 2976 | non_constant_p, overflow_p); |
cf650568 JJ |
2977 | if (i == 1) |
2978 | arg1 = arg; | |
2805fcb3 JJ |
2979 | else |
2980 | VERIFY_CONSTANT (arg); | |
cf650568 JJ |
2981 | } |
2982 | gcc_assert (arg1); | |
2983 | return arg1; | |
2984 | } | |
22edf943 MP |
2985 | else if (cxx_dynamic_cast_fn_p (fun)) |
2986 | return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p); | |
2987 | ||
2b3ab879 | 2988 | if (!ctx->quiet) |
2d76680f | 2989 | { |
11399477 | 2990 | if (!lambda_static_thunk_p (fun)) |
84fa214d | 2991 | error_at (loc, "call to non-%<constexpr%> function %qD", fun); |
2d76680f PC |
2992 | explain_invalid_constexpr_fn (fun); |
2993 | } | |
2994 | *non_constant_p = true; | |
2995 | return t; | |
2996 | } | |
2997 | ||
86461cad JM |
2998 | constexpr_ctx new_ctx = *ctx; |
2999 | if (DECL_CONSTRUCTOR_P (fun) && !ctx->object | |
3000 | && TREE_CODE (t) == AGGR_INIT_EXPR) | |
3001 | { | |
3002 | /* We want to have an initialization target for an AGGR_INIT_EXPR. | |
3003 | If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */ | |
3004 | new_ctx.object = AGGR_INIT_EXPR_SLOT (t); | |
3005 | tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL); | |
e8c48716 | 3006 | CONSTRUCTOR_NO_CLEARING (ctor) = true; |
e6a29aab | 3007 | ctx->global->put_value (new_ctx.object, ctor); |
86461cad JM |
3008 | ctx = &new_ctx; |
3009 | } | |
3010 | ||
b2287a4d MP |
3011 | /* We used to shortcut trivial constructor/op= here, but nowadays |
3012 | we can only get a trivial function here with -fno-elide-constructors. */ | |
e4692319 MP |
3013 | gcc_checking_assert (!trivial_fn_p (fun) |
3014 | || !flag_elide_constructors | |
3015 | /* We don't elide constructors when processing | |
3016 | a noexcept-expression. */ | |
3017 | || cp_noexcept_operand); | |
2d76680f | 3018 | |
1595fe44 JM |
3019 | bool non_constant_args = false; |
3020 | new_call.bindings | |
3021 | = cxx_bind_parameters_in_call (ctx, t, fun, non_constant_p, | |
3022 | overflow_p, &non_constant_args); | |
3023 | ||
3024 | /* We build up the bindings list before we know whether we already have this | |
3025 | call cached. If we don't end up saving these bindings, ggc_free them when | |
3026 | this function exits. */ | |
3027 | class free_bindings | |
3028 | { | |
3029 | tree *bindings; | |
3030 | public: | |
3031 | free_bindings (tree &b): bindings (&b) { } | |
3032 | ~free_bindings () { if (bindings) ggc_free (*bindings); } | |
3033 | void preserve () { bindings = NULL; } | |
3034 | } fb (new_call.bindings); | |
3035 | ||
3036 | if (*non_constant_p) | |
3037 | return t; | |
3038 | ||
81371eff JM |
3039 | /* We can't defer instantiating the function any longer. */ |
3040 | if (!DECL_INITIAL (fun) | |
59946a4c | 3041 | && (DECL_TEMPLOID_INSTANTIATION (fun) || DECL_DEFAULTED_FN (fun)) |
f65a3299 | 3042 | && !uid_sensitive_constexpr_evaluation_p ()) |
81371eff | 3043 | { |
f065303f JM |
3044 | location_t save_loc = input_location; |
3045 | input_location = loc; | |
81371eff | 3046 | ++function_depth; |
3a0bc47c | 3047 | if (ctx->manifestly_const_eval == mce_true) |
9aeadd8c | 3048 | FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true; |
59946a4c PP |
3049 | if (DECL_TEMPLOID_INSTANTIATION (fun)) |
3050 | instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false); | |
3051 | else | |
3052 | synthesize_method (fun); | |
81371eff | 3053 | --function_depth; |
f065303f | 3054 | input_location = save_loc; |
81371eff JM |
3055 | } |
3056 | ||
2d76680f | 3057 | /* If in direct recursive call, optimize definition search. */ |
c8816908 | 3058 | if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun) |
3e605b20 | 3059 | new_call.fundef = ctx->call->fundef; |
2d76680f PC |
3060 | else |
3061 | { | |
3062 | new_call.fundef = retrieve_constexpr_fundef (fun); | |
4ddcdbf9 | 3063 | if (new_call.fundef == NULL || new_call.fundef->body == NULL |
4f05d85a | 3064 | || new_call.fundef->result == error_mark_node |
4ddcdbf9 | 3065 | || fun == current_function_decl) |
2d76680f | 3066 | { |
2b3ab879 | 3067 | if (!ctx->quiet) |
2d76680f | 3068 | { |
4ddcdbf9 JM |
3069 | /* We need to check for current_function_decl here in case we're |
3070 | being called during cp_fold_function, because at that point | |
3071 | DECL_INITIAL is set properly and we have a fundef but we | |
3072 | haven't lowered invisirefs yet (c++/70344). */ | |
3073 | if (DECL_INITIAL (fun) == error_mark_node | |
3074 | || fun == current_function_decl) | |
ddd6d421 JM |
3075 | error_at (loc, "%qD called in a constant expression before its " |
3076 | "definition is complete", fun); | |
3077 | else if (DECL_INITIAL (fun)) | |
2d76680f | 3078 | { |
98e5a19a JM |
3079 | /* The definition of fun was somehow unsuitable. But pretend |
3080 | that lambda static thunks don't exist. */ | |
3081 | if (!lambda_static_thunk_p (fun)) | |
3082 | error_at (loc, "%qD called in a constant expression", fun); | |
2d76680f PC |
3083 | explain_invalid_constexpr_fn (fun); |
3084 | } | |
3085 | else | |
3086 | error_at (loc, "%qD used before its definition", fun); | |
3087 | } | |
3088 | *non_constant_p = true; | |
3089 | return t; | |
3090 | } | |
3091 | } | |
60813a46 | 3092 | |
2d76680f PC |
3093 | depth_ok = push_cx_call_context (t); |
3094 | ||
caf17f3a | 3095 | /* Remember the object we are constructing or destructing. */ |
04e1749c | 3096 | tree new_obj = NULL_TREE; |
caf17f3a | 3097 | if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun)) |
04e1749c | 3098 | { |
caf17f3a | 3099 | /* In a cdtor, it should be the first `this' argument. |
04e1749c MP |
3100 | At this point it has already been evaluated in the call |
3101 | to cxx_bind_parameters_in_call. */ | |
3102 | new_obj = TREE_VEC_ELT (new_call.bindings, 0); | |
1189c038 | 3103 | new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj); |
64da1b76 PP |
3104 | |
3105 | if (ctx->call && ctx->call->fundef | |
3106 | && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl)) | |
3107 | { | |
3108 | tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0); | |
3109 | STRIP_NOPS (cur_obj); | |
3110 | if (TREE_CODE (cur_obj) == ADDR_EXPR) | |
3111 | cur_obj = TREE_OPERAND (cur_obj, 0); | |
3112 | if (new_obj == cur_obj) | |
3113 | /* We're calling the target constructor of a delegating | |
3114 | constructor, or accessing a base subobject through a | |
3115 | NOP_EXPR as part of a call to a base constructor, so | |
3116 | there is no new (sub)object. */ | |
3117 | new_obj = NULL_TREE; | |
3118 | } | |
04e1749c MP |
3119 | } |
3120 | ||
12d9ce19 | 3121 | tree result = NULL_TREE; |
2d76680f | 3122 | |
12d9ce19 | 3123 | constexpr_call *entry = NULL; |
4a58d2fe | 3124 | if (depth_ok && !non_constant_args && ctx->strict) |
2d76680f | 3125 | { |
cce3ae91 JJ |
3126 | new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef); |
3127 | new_call.hash | |
3128 | = iterative_hash_template_arg (new_call.bindings, new_call.hash); | |
3129 | new_call.hash | |
13de99bc | 3130 | = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash); |
12d9ce19 JM |
3131 | |
3132 | /* If we have seen this call before, we are done. */ | |
3133 | maybe_initialize_constexpr_call_table (); | |
6ec8079e | 3134 | bool insert = depth_ok < constexpr_cache_depth; |
12d9ce19 | 3135 | constexpr_call **slot |
6ec8079e AO |
3136 | = constexpr_call_table->find_slot (&new_call, |
3137 | insert ? INSERT : NO_INSERT); | |
3138 | entry = slot ? *slot : NULL; | |
12d9ce19 JM |
3139 | if (entry == NULL) |
3140 | { | |
7ffc7de5 | 3141 | /* Only cache up to constexpr_cache_depth to limit memory use. */ |
6ec8079e | 3142 | if (insert) |
7ffc7de5 JM |
3143 | { |
3144 | /* We need to keep a pointer to the entry, not just the slot, as | |
3145 | the slot can move during evaluation of the body. */ | |
3146 | *slot = entry = ggc_alloc<constexpr_call> (); | |
3147 | *entry = new_call; | |
3148 | fb.preserve (); | |
3149 | } | |
12d9ce19 | 3150 | } |
7ffc7de5 JM |
3151 | /* Calls that are in progress have their result set to NULL, so that we |
3152 | can detect circular dependencies. Now that we only cache up to | |
3153 | constexpr_cache_depth this won't catch circular dependencies that | |
3154 | start deeper, but they'll hit the recursion or ops limit. */ | |
12d9ce19 JM |
3155 | else if (entry->result == NULL) |
3156 | { | |
3157 | if (!ctx->quiet) | |
3158 | error ("call has circular dependency"); | |
3159 | *non_constant_p = true; | |
3160 | entry->result = result = error_mark_node; | |
3161 | } | |
3162 | else | |
3163 | result = entry->result; | |
2d76680f PC |
3164 | } |
3165 | ||
3166 | if (!depth_ok) | |
3167 | { | |
2b3ab879 | 3168 | if (!ctx->quiet) |
84fa214d | 3169 | error ("%<constexpr%> evaluation depth exceeds maximum of %d (use " |
7e7a6ed7 | 3170 | "%<-fconstexpr-depth=%> to increase the maximum)", |
2d76680f PC |
3171 | max_constexpr_depth); |
3172 | *non_constant_p = true; | |
12d9ce19 | 3173 | result = error_mark_node; |
2d76680f PC |
3174 | } |
3175 | else | |
3176 | { | |
c7ac1de5 | 3177 | bool cacheable = !!entry; |
de54de93 JM |
3178 | if (result && result != error_mark_node) |
3179 | /* OK */; | |
3180 | else if (!DECL_SAVED_TREE (fun)) | |
3181 | { | |
1f1c4322 | 3182 | /* When at_eof >= 3, cgraph has started throwing away |
de54de93 JM |
3183 | DECL_SAVED_TREE, so fail quietly. FIXME we get here because of |
3184 | late code generation for VEC_INIT_EXPR, which needs to be | |
3185 | completely reconsidered. */ | |
1f1c4322 | 3186 | gcc_assert (at_eof >= 3 && ctx->quiet); |
de54de93 JM |
3187 | *non_constant_p = true; |
3188 | } | |
f65a3299 | 3189 | else if (tree copy = get_fundef_copy (new_call.fundef)) |
3e605b20 | 3190 | { |
c0daf32d | 3191 | tree body, parms, res; |
620adbec | 3192 | releasing_vec ctors; |
0567dcd2 | 3193 | |
c0daf32d | 3194 | /* Reuse or create a new unshared copy of this function's body. */ |
97f3003f JM |
3195 | body = TREE_PURPOSE (copy); |
3196 | parms = TREE_VALUE (copy); | |
3197 | res = TREE_TYPE (copy); | |
0567dcd2 MP |
3198 | |
3199 | /* Associate the bindings with the remapped parms. */ | |
3200 | tree bound = new_call.bindings; | |
3201 | tree remapped = parms; | |
3c961dc7 | 3202 | for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i) |
60813a46 | 3203 | { |
3c961dc7 | 3204 | tree arg = TREE_VEC_ELT (bound, i); |
4953b790 JM |
3205 | if (entry) |
3206 | { | |
3207 | /* Unshare args going into the hash table to separate them | |
3208 | from the caller's context, for better GC and to avoid | |
3209 | problems with verify_gimple. */ | |
db38a029 | 3210 | arg = unshare_expr_without_location (arg); |
4953b790 | 3211 | TREE_VEC_ELT (bound, i) = arg; |
5afd90c5 JJ |
3212 | |
3213 | /* And then unshare again so the callee doesn't change the | |
3214 | argument values in the hash table. XXX Could we unshare | |
3215 | lazily in cxx_eval_store_expression? */ | |
3216 | arg = unshare_constructor (arg); | |
3217 | if (TREE_CODE (arg) == CONSTRUCTOR) | |
3218 | vec_safe_push (ctors, arg); | |
4953b790 | 3219 | } |
e6a29aab | 3220 | ctx->global->put_value (remapped, arg); |
0567dcd2 | 3221 | remapped = DECL_CHAIN (remapped); |
60813a46 | 3222 | } |
e0659b54 JM |
3223 | for (; remapped; remapped = TREE_CHAIN (remapped)) |
3224 | if (DECL_NAME (remapped) == in_charge_identifier) | |
3225 | { | |
3226 | /* FIXME destructors unnecessarily have in-charge parameters | |
3227 | even in classes without vbases, map it to 0 for now. */ | |
3228 | gcc_assert (!CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun))); | |
3229 | ctx->global->put_value (remapped, integer_zero_node); | |
3230 | } | |
3231 | else | |
3232 | { | |
3233 | gcc_assert (seen_error ()); | |
3234 | *non_constant_p = true; | |
3235 | } | |
0567dcd2 | 3236 | /* Add the RESULT_DECL to the values map, too. */ |
cd3ca6cb | 3237 | gcc_assert (!DECL_BY_REFERENCE (res)); |
e6a29aab | 3238 | ctx->global->put_value (res, NULL_TREE); |
60813a46 | 3239 | |
9fdbd7d6 NS |
3240 | /* Remember the current call we're evaluating. */ |
3241 | constexpr_ctx call_ctx = *ctx; | |
3242 | call_ctx.call = &new_call; | |
f74f6092 JJ |
3243 | unsigned save_heap_alloc_count = ctx->global->heap_vars.length (); |
3244 | unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count; | |
c0daf32d | 3245 | |
6851e342 MP |
3246 | /* Make sure we fold std::is_constant_evaluated to true in an |
3247 | immediate function. */ | |
3248 | if (DECL_IMMEDIATE_FUNCTION_P (fun)) | |
3249 | call_ctx.manifestly_const_eval = mce_true; | |
3250 | ||
caf17f3a MP |
3251 | /* If this is a constexpr destructor, the object's const and volatile |
3252 | semantics are no longer in effect; see [class.dtor]p5. */ | |
3253 | if (new_obj && DECL_DESTRUCTOR_P (fun)) | |
3254 | cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false, | |
3255 | non_constant_p, overflow_p); | |
3256 | ||
1d260ab0 NS |
3257 | /* If this is a constructor, we are beginning the lifetime of the |
3258 | object we are initializing. */ | |
3259 | if (new_obj | |
3260 | && DECL_CONSTRUCTOR_P (fun) | |
3261 | && TREE_CODE (new_obj) == COMPONENT_REF | |
3262 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (new_obj, 0))) == UNION_TYPE) | |
3263 | { | |
3264 | tree activate = build2 (INIT_EXPR, TREE_TYPE (new_obj), | |
3265 | new_obj, | |
3266 | build_constructor (TREE_TYPE (new_obj), | |
3267 | NULL)); | |
3268 | cxx_eval_constant_expression (ctx, activate, | |
3269 | lval, non_constant_p, overflow_p); | |
3270 | ggc_free (activate); | |
3271 | } | |
3272 | ||
0567dcd2 | 3273 | tree jump_target = NULL_TREE; |
9fdbd7d6 | 3274 | cxx_eval_constant_expression (&call_ctx, body, |
72f76540 | 3275 | vc_discard, non_constant_p, overflow_p, |
0567dcd2 | 3276 | &jump_target); |
60813a46 | 3277 | |
0567dcd2 | 3278 | if (DECL_CONSTRUCTOR_P (fun)) |
1d260ab0 NS |
3279 | /* This can be null for a subobject constructor call, in |
3280 | which case what we care about is the initialization | |
3281 | side-effects rather than the value. We could get at the | |
3282 | value by evaluating *this, but we don't bother; there's | |
3283 | no need to put such a call in the hash table. */ | |
3284 | result = lval ? ctx->object : ctx->ctor; | |
0567dcd2 MP |
3285 | else if (VOID_TYPE_P (TREE_TYPE (res))) |
3286 | result = void_node; | |
3287 | else | |
3288 | { | |
e6a29aab | 3289 | result = ctx->global->get_value (res); |
5ce08ecb AO |
3290 | if (result == NULL_TREE && !*non_constant_p |
3291 | && !DECL_DESTRUCTOR_P (fun)) | |
60813a46 | 3292 | { |
0567dcd2 | 3293 | if (!ctx->quiet) |
84fa214d | 3294 | error ("%<constexpr%> call flows off the end " |
0567dcd2 MP |
3295 | "of the function"); |
3296 | *non_constant_p = true; | |
60813a46 | 3297 | } |
60813a46 | 3298 | } |
0567dcd2 | 3299 | |
04e1749c MP |
3300 | /* At this point, the object's constructor will have run, so |
3301 | the object is no longer under construction, and its possible | |
3302 | 'const' semantics now apply. Make a note of this fact by | |
3303 | marking the CONSTRUCTOR TREE_READONLY. */ | |
caf17f3a MP |
3304 | if (new_obj && DECL_CONSTRUCTOR_P (fun)) |
3305 | cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true, | |
3306 | non_constant_p, overflow_p); | |
04e1749c | 3307 | |
9fdbd7d6 | 3308 | /* Remove the parms/result from the values map. */ |
90bc2d09 | 3309 | destroy_value_checked (ctx, res, non_constant_p); |
0567dcd2 | 3310 | for (tree parm = parms; parm; parm = TREE_CHAIN (parm)) |
90bc2d09 | 3311 | destroy_value_checked (ctx, parm, non_constant_p); |
c0daf32d | 3312 | |
620adbec JM |
3313 | /* Free any parameter CONSTRUCTORs we aren't returning directly. */ |
3314 | while (!ctors->is_empty ()) | |
3315 | { | |
3316 | tree c = ctors->pop (); | |
3317 | if (c != result) | |
3318 | free_constructor (c); | |
3319 | } | |
3320 | ||
c0daf32d PP |
3321 | /* Make the unshared function copy we used available for re-use. */ |
3322 | save_fundef_copy (fun, copy); | |
f74f6092 JJ |
3323 | |
3324 | /* If the call allocated some heap object that hasn't been | |
3325 | deallocated during the call, or if it deallocated some heap | |
3326 | object it has not allocated, the call isn't really stateless | |
3327 | for the constexpr evaluation and should not be cached. | |
3328 | It is fine if the call allocates something and deallocates it | |
3329 | too. */ | |
c7ac1de5 | 3330 | if (cacheable |
f74f6092 JJ |
3331 | && (save_heap_alloc_count != ctx->global->heap_vars.length () |
3332 | || (save_heap_dealloc_count | |
3333 | != ctx->global->heap_dealloc_count))) | |
3334 | { | |
3335 | tree heap_var; | |
3336 | unsigned int i; | |
3337 | if ((ctx->global->heap_vars.length () | |
3338 | - ctx->global->heap_dealloc_count) | |
3339 | != save_heap_alloc_count - save_heap_dealloc_count) | |
3340 | cacheable = false; | |
3341 | else | |
3342 | FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var, | |
3343 | save_heap_alloc_count) | |
3344 | if (DECL_NAME (heap_var) != heap_deleted_identifier) | |
3345 | { | |
3346 | cacheable = false; | |
3347 | break; | |
3348 | } | |
3349 | } | |
b2562229 PP |
3350 | |
3351 | /* Rewrite all occurrences of the function's RESULT_DECL with the | |
3352 | current object under construction. */ | |
3353 | if (!*non_constant_p && ctx->object | |
bb1f0b50 | 3354 | && CLASS_TYPE_P (TREE_TYPE (res)) |
b2562229 | 3355 | && !is_empty_class (TREE_TYPE (res))) |
4f6bc28f | 3356 | if (replace_decl (&result, res, ctx->object)) |
b2562229 | 3357 | cacheable = false; |
c7ac1de5 JM |
3358 | |
3359 | /* Only cache a permitted result of a constant expression. */ | |
3360 | if (cacheable && !reduced_constant_expression_p (result)) | |
3361 | cacheable = false; | |
3e605b20 | 3362 | } |
aaa26bf4 JM |
3363 | else |
3364 | /* Couldn't get a function copy to evaluate. */ | |
3365 | *non_constant_p = true; | |
60813a46 | 3366 | |
2d76680f PC |
3367 | if (result == error_mark_node) |
3368 | *non_constant_p = true; | |
52228180 | 3369 | if (*non_constant_p || *overflow_p) |
12d9ce19 | 3370 | result = error_mark_node; |
0567dcd2 | 3371 | else if (!result) |
60813a46 | 3372 | result = void_node; |
12d9ce19 | 3373 | if (entry) |
f74f6092 | 3374 | entry->result = cacheable ? result : error_mark_node; |
2d76680f PC |
3375 | } |
3376 | ||
7906797e MP |
3377 | /* The result of a constexpr function must be completely initialized. |
3378 | ||
3379 | However, in C++20, a constexpr constructor doesn't necessarily have | |
3380 | to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING | |
3381 | in order to detect reading an unitialized object in constexpr instead | |
3382 | of value-initializing it. (reduced_constant_expression_p is expected to | |
3383 | take care of clearing the flag.) */ | |
3384 | if (TREE_CODE (result) == CONSTRUCTOR | |
b04445d4 | 3385 | && (cxx_dialect < cxx20 |
7906797e | 3386 | || !DECL_CONSTRUCTOR_P (fun))) |
ecc57615 | 3387 | clear_no_implicit_zero (result); |
f64e0c02 | 3388 | |
2d76680f | 3389 | pop_cx_call_context (); |
9b9eb42a | 3390 | return result; |
2d76680f PC |
3391 | } |
3392 | ||
7906797e MP |
3393 | /* Return true if T is a valid constant initializer. If a CONSTRUCTOR |
3394 | initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be | |
3395 | cleared. | |
3396 | FIXME speed this up, it's taking 16% of compile time on sieve testcase. */ | |
2d76680f PC |
3397 | |
3398 | bool | |
3399 | reduced_constant_expression_p (tree t) | |
3400 | { | |
4930c53e MP |
3401 | if (t == NULL_TREE) |
3402 | return false; | |
3403 | ||
2d76680f PC |
3404 | switch (TREE_CODE (t)) |
3405 | { | |
3406 | case PTRMEM_CST: | |
3407 | /* Even if we can't lower this yet, it's constant. */ | |
3408 | return true; | |
3409 | ||
3410 | case CONSTRUCTOR: | |
3411 | /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */ | |
fae00a0a | 3412 | tree field; |
c7ac1de5 JM |
3413 | if (!AGGREGATE_TYPE_P (TREE_TYPE (t))) |
3414 | /* A constant vector would be folded to VECTOR_CST. | |
3415 | A CONSTRUCTOR of scalar type means uninitialized. */ | |
2cc41601 | 3416 | return false; |
e8c48716 | 3417 | if (CONSTRUCTOR_NO_CLEARING (t)) |
6f11ddd8 | 3418 | { |
2cc41601 | 3419 | if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) |
baf05d54 PP |
3420 | { |
3421 | /* There must be a valid constant initializer at every array | |
3422 | index. */ | |
3423 | tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t))); | |
3424 | tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t))); | |
3425 | tree cursor = min; | |
fae00a0a | 3426 | for (auto &e: CONSTRUCTOR_ELTS (t)) |
baf05d54 | 3427 | { |
fae00a0a | 3428 | if (!reduced_constant_expression_p (e.value)) |
baf05d54 | 3429 | return false; |
fae00a0a | 3430 | if (array_index_cmp (cursor, e.index) != 0) |
baf05d54 | 3431 | return false; |
fae00a0a JM |
3432 | if (TREE_CODE (e.index) == RANGE_EXPR) |
3433 | cursor = TREE_OPERAND (e.index, 1); | |
baf05d54 PP |
3434 | cursor = int_const_binop (PLUS_EXPR, cursor, size_one_node); |
3435 | } | |
3436 | if (find_array_ctor_elt (t, max) == -1) | |
3437 | return false; | |
3438 | goto ok; | |
3439 | } | |
b04445d4 | 3440 | else if (cxx_dialect >= cxx20 |
b599bf9d PP |
3441 | && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE) |
3442 | { | |
3443 | if (CONSTRUCTOR_NELTS (t) == 0) | |
3444 | /* An initialized union has a constructor element. */ | |
3445 | return false; | |
3446 | /* And it only initializes one member. */ | |
3447 | field = NULL_TREE; | |
3448 | } | |
6f11ddd8 | 3449 | else |
0c7bce0a | 3450 | field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t))); |
6f11ddd8 | 3451 | } |
7368cfa4 JM |
3452 | else |
3453 | field = NULL_TREE; | |
fae00a0a | 3454 | for (auto &e: CONSTRUCTOR_ELTS (t)) |
125db6a1 | 3455 | { |
4930c53e MP |
3456 | /* If VAL is null, we're in the middle of initializing this |
3457 | element. */ | |
fae00a0a | 3458 | if (!reduced_constant_expression_p (e.value)) |
125db6a1 | 3459 | return false; |
9efe4e15 JM |
3460 | /* We want to remove initializers for empty fields in a struct to |
3461 | avoid confusing output_constructor. */ | |
3462 | if (is_empty_field (e.index) | |
3463 | && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE) | |
3464 | return false; | |
3465 | /* Check for non-empty fields between initialized fields when | |
3466 | CONSTRUCTOR_NO_CLEARING. */ | |
fae00a0a | 3467 | for (; field && e.index != field; |
0c7bce0a | 3468 | field = next_subobject_field (DECL_CHAIN (field))) |
d6ff2207 MP |
3469 | if (!is_really_empty_class (TREE_TYPE (field), |
3470 | /*ignore_vptr*/false)) | |
3471 | return false; | |
7368cfa4 | 3472 | if (field) |
0c7bce0a | 3473 | field = next_subobject_field (DECL_CHAIN (field)); |
125db6a1 | 3474 | } |
7906797e | 3475 | /* There could be a non-empty field at the end. */ |
0c7bce0a | 3476 | for (; field; field = next_subobject_field (DECL_CHAIN (field))) |
7906797e MP |
3477 | if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false)) |
3478 | return false; | |
baf05d54 | 3479 | ok: |
7906797e | 3480 | if (CONSTRUCTOR_NO_CLEARING (t)) |
7368cfa4 | 3481 | /* All the fields are initialized. */ |
e8c48716 | 3482 | CONSTRUCTOR_NO_CLEARING (t) = false; |
2d76680f PC |
3483 | return true; |
3484 | ||
3485 | default: | |
3486 | /* FIXME are we calling this too much? */ | |
3487 | return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE; | |
3488 | } | |
3489 | } | |
3490 | ||
b78cedc6 MP |
3491 | /* *TP was not deemed constant by reduced_constant_expression_p. Explain |
3492 | why and suggest what could be done about it. */ | |
3493 | ||
3494 | static tree | |
3495 | verify_constant_explain_r (tree *tp, int *walk_subtrees, void *) | |
3496 | { | |
3497 | bool ref_p = false; | |
3498 | ||
3499 | /* No need to look into types or unevaluated operands. */ | |
3500 | if (TYPE_P (*tp) || unevaluated_p (TREE_CODE (*tp))) | |
3501 | { | |
3502 | *walk_subtrees = false; | |
3503 | return NULL_TREE; | |
3504 | } | |
3505 | ||
3506 | switch (TREE_CODE (*tp)) | |
3507 | { | |
3508 | CASE_CONVERT: | |
3509 | if (TREE_CODE (TREE_OPERAND (*tp, 0)) != ADDR_EXPR) | |
3510 | break; | |
3511 | ref_p = TYPE_REF_P (TREE_TYPE (*tp)); | |
3512 | *tp = TREE_OPERAND (*tp, 0); | |
3513 | gcc_fallthrough (); | |
3514 | case ADDR_EXPR: | |
3515 | { | |
3516 | tree op = TREE_OPERAND (*tp, 0); | |
3517 | if (VAR_P (op) | |
3518 | && DECL_DECLARED_CONSTEXPR_P (op) | |
3519 | && !TREE_STATIC (op) | |
3520 | /* ??? We should also say something about temporaries. */ | |
3521 | && !DECL_ARTIFICIAL (op)) | |
3522 | { | |
3523 | if (ref_p) | |
3524 | inform (location_of (*tp), "reference to %qD is not a constant " | |
3525 | "expression", op); | |
3526 | else | |
3527 | inform (location_of (*tp), "pointer to %qD is not a constant " | |
3528 | "expression", op); | |
3529 | const location_t op_loc = DECL_SOURCE_LOCATION (op); | |
3530 | rich_location richloc (line_table, op_loc); | |
3531 | richloc.add_fixit_insert_before (op_loc, "static "); | |
3532 | inform (&richloc, | |
3533 | "address of non-static constexpr variable %qD may differ on " | |
3534 | "each invocation of the enclosing function; add %<static%> " | |
3535 | "to give it a constant address", op); | |
3536 | } | |
3537 | break; | |
3538 | } | |
3539 | default: | |
3540 | break; | |
3541 | } | |
3542 | ||
3543 | return NULL_TREE; | |
3544 | } | |
3545 | ||
2d76680f | 3546 | /* Some expressions may have constant operands but are not constant |
7d75ea04 JJ |
3547 | themselves, such as 1/0. Call this function to check for that |
3548 | condition. | |
2d76680f PC |
3549 | |
3550 | We only call this in places that require an arithmetic constant, not in | |
3551 | places where we might have a non-constant expression that can be a | |
3552 | component of a constant expression, such as the address of a constexpr | |
3553 | variable that might be dereferenced later. */ | |
3554 | ||
3555 | static bool | |
3556 | verify_constant (tree t, bool allow_non_constant, bool *non_constant_p, | |
3557 | bool *overflow_p) | |
3558 | { | |
596334fa JM |
3559 | if (!*non_constant_p && !reduced_constant_expression_p (t) |
3560 | && t != void_node) | |
2d76680f PC |
3561 | { |
3562 | if (!allow_non_constant) | |
b78cedc6 MP |
3563 | { |
3564 | auto_diagnostic_group d; | |
3565 | error_at (cp_expr_loc_or_input_loc (t), | |
3566 | "%q+E is not a constant expression", t); | |
3567 | cp_walk_tree_without_duplicates (&t, verify_constant_explain_r, | |
3568 | nullptr); | |
3569 | } | |
2d76680f PC |
3570 | *non_constant_p = true; |
3571 | } | |
3572 | if (TREE_OVERFLOW_P (t)) | |
3573 | { | |
3574 | if (!allow_non_constant) | |
3575 | { | |
3576 | permerror (input_location, "overflow in constant expression"); | |
3577 | /* If we're being permissive (and are in an enforcing | |
3578 | context), ignore the overflow. */ | |
3579 | if (flag_permissive) | |
3580 | return *non_constant_p; | |
3581 | } | |
3582 | *overflow_p = true; | |
3583 | } | |
3584 | return *non_constant_p; | |
3585 | } | |
3586 | ||
253a921b MP |
3587 | /* Check whether the shift operation with code CODE and type TYPE on LHS |
3588 | and RHS is undefined. If it is, give an error with an explanation, | |
3589 | and return true; return false otherwise. */ | |
3590 | ||
3591 | static bool | |
3592 | cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx, | |
3593 | enum tree_code code, tree type, tree lhs, tree rhs) | |
3594 | { | |
3595 | if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR) | |
3596 | || TREE_CODE (lhs) != INTEGER_CST | |
3597 | || TREE_CODE (rhs) != INTEGER_CST) | |
3598 | return false; | |
3599 | ||
3600 | tree lhstype = TREE_TYPE (lhs); | |
3601 | unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs)); | |
3602 | ||
3603 | /* [expr.shift] The behavior is undefined if the right operand | |
3604 | is negative, or greater than or equal to the length in bits | |
3605 | of the promoted left operand. */ | |
3606 | if (tree_int_cst_sgn (rhs) == -1) | |
3607 | { | |
3608 | if (!ctx->quiet) | |
5342156c MP |
3609 | permerror (loc, "right operand of shift expression %q+E is negative", |
3610 | build2_loc (loc, code, type, lhs, rhs)); | |
3611 | return (!flag_permissive || ctx->quiet); | |
253a921b MP |
3612 | } |
3613 | if (compare_tree_int (rhs, uprec) >= 0) | |
3614 | { | |
3615 | if (!ctx->quiet) | |
a9c697b8 MS |
3616 | permerror (loc, "right operand of shift expression %q+E is greater " |
3617 | "than or equal to the precision %wu of the left operand", | |
3618 | build2_loc (loc, code, type, lhs, rhs), uprec); | |
5342156c | 3619 | return (!flag_permissive || ctx->quiet); |
253a921b MP |
3620 | } |
3621 | ||
3622 | /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...] | |
3623 | if E1 has a signed type and non-negative value, and E1x2^E2 is | |
3624 | representable in the corresponding unsigned type of the result type, | |
3625 | then that value, converted to the result type, is the resulting value; | |
8ee09943 | 3626 | otherwise, the behavior is undefined. |
b04445d4 | 3627 | For C++20: |
8ee09943 JJ |
3628 | The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo |
3629 | 2^N, where N is the range exponent of the type of the result. */ | |
3630 | if (code == LSHIFT_EXPR | |
d7651113 | 3631 | && !TYPE_OVERFLOW_WRAPS (lhstype) |
8ee09943 | 3632 | && cxx_dialect >= cxx11 |
b04445d4 | 3633 | && cxx_dialect < cxx20) |
253a921b MP |
3634 | { |
3635 | if (tree_int_cst_sgn (lhs) == -1) | |
3636 | { | |
3637 | if (!ctx->quiet) | |
5342156c MP |
3638 | permerror (loc, |
3639 | "left operand of shift expression %q+E is negative", | |
3640 | build2_loc (loc, code, type, lhs, rhs)); | |
3641 | return (!flag_permissive || ctx->quiet); | |
253a921b MP |
3642 | } |
3643 | /* For signed x << y the following: | |
3644 | (unsigned) x >> ((prec (lhs) - 1) - y) | |
3645 | if > 1, is undefined. The right-hand side of this formula | |
3646 | is the highest bit of the LHS that can be set (starting from 0), | |
3647 | so that the shift doesn't overflow. We then right-shift the LHS | |
3648 | to see whether any other bit is set making the original shift | |
3649 | undefined -- the result is not representable in the corresponding | |
3650 | unsigned type. */ | |
3651 | tree t = build_int_cst (unsigned_type_node, uprec - 1); | |
3652 | t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs); | |
3653 | tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs); | |
3654 | t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t); | |
3655 | if (tree_int_cst_lt (integer_one_node, t)) | |
3656 | { | |
3657 | if (!ctx->quiet) | |
5342156c MP |
3658 | permerror (loc, "shift expression %q+E overflows", |
3659 | build2_loc (loc, code, type, lhs, rhs)); | |
3660 | return (!flag_permissive || ctx->quiet); | |
253a921b MP |
3661 | } |
3662 | } | |
3663 | return false; | |
3664 | } | |
3665 | ||
2d76680f PC |
3666 | /* Subroutine of cxx_eval_constant_expression. |
3667 | Attempt to reduce the unary expression tree T to a compile time value. | |
3668 | If successful, return the value. Otherwise issue a diagnostic | |
3669 | and return error_mark_node. */ | |
3670 | ||
3671 | static tree | |
3e605b20 | 3672 | cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t, |
12d9ce19 | 3673 | bool /*lval*/, |
2d76680f PC |
3674 | bool *non_constant_p, bool *overflow_p) |
3675 | { | |
3676 | tree r; | |
3677 | tree orig_arg = TREE_OPERAND (t, 0); | |
72f76540 | 3678 | tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue, |
12d9ce19 | 3679 | non_constant_p, overflow_p); |
2d76680f | 3680 | VERIFY_CONSTANT (arg); |
f899317e JM |
3681 | location_t loc = EXPR_LOCATION (t); |
3682 | enum tree_code code = TREE_CODE (t); | |
3683 | tree type = TREE_TYPE (t); | |
3684 | r = fold_unary_loc (loc, code, type, arg); | |
3685 | if (r == NULL_TREE) | |
3686 | { | |
3687 | if (arg == orig_arg) | |
3688 | r = t; | |
3689 | else | |
3690 | r = build1_loc (loc, code, type, arg); | |
3691 | } | |
2d76680f PC |
3692 | VERIFY_CONSTANT (r); |
3693 | return r; | |
3694 | } | |
3695 | ||
ea8661cd JJ |
3696 | /* Helper function for cxx_eval_binary_expression. Try to optimize |
3697 | original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the | |
3698 | generic folding should be used. */ | |
3699 | ||
3700 | static tree | |
3701 | cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t, | |
3702 | tree lhs, tree rhs, bool *non_constant_p, | |
3703 | bool *overflow_p) | |
3704 | { | |
3705 | STRIP_NOPS (lhs); | |
3706 | if (TREE_CODE (lhs) != ADDR_EXPR) | |
3707 | return NULL_TREE; | |
3708 | ||
3709 | lhs = TREE_OPERAND (lhs, 0); | |
3710 | ||
3711 | /* &A[i] p+ j => &A[i + j] */ | |
3712 | if (TREE_CODE (lhs) == ARRAY_REF | |
3713 | && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST | |
3714 | && TREE_CODE (rhs) == INTEGER_CST | |
3715 | && TYPE_SIZE_UNIT (TREE_TYPE (lhs)) | |
3716 | && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST) | |
3717 | { | |
3718 | tree orig_type = TREE_TYPE (t); | |
3719 | location_t loc = EXPR_LOCATION (t); | |
3720 | tree type = TREE_TYPE (lhs); | |
3721 | ||
3722 | t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1)); | |
3723 | tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0))); | |
72f76540 JM |
3724 | nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue, |
3725 | non_constant_p, overflow_p); | |
ea8661cd JJ |
3726 | if (*non_constant_p) |
3727 | return NULL_TREE; | |
3728 | /* Don't fold an out-of-bound access. */ | |
3729 | if (!tree_int_cst_le (t, nelts)) | |
3730 | return NULL_TREE; | |
3731 | rhs = cp_fold_convert (ssizetype, rhs); | |
3732 | /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT. | |
3733 | constexpr int A[1]; ... (char *)&A[0] + 1 */ | |
3734 | if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype, | |
3735 | rhs, TYPE_SIZE_UNIT (type)))) | |
3736 | return NULL_TREE; | |
3737 | /* Make sure to treat the second operand of POINTER_PLUS_EXPR | |
3738 | as signed. */ | |
3739 | rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs, | |
3740 | TYPE_SIZE_UNIT (type)); | |
3741 | t = size_binop_loc (loc, PLUS_EXPR, rhs, t); | |
3742 | t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0), | |
3743 | t, NULL_TREE, NULL_TREE); | |
3744 | t = cp_build_addr_expr (t, tf_warning_or_error); | |
3745 | t = cp_fold_convert (orig_type, t); | |
72f76540 | 3746 | return cxx_eval_constant_expression (ctx, t, vc_prvalue, |
ea8661cd JJ |
3747 | non_constant_p, overflow_p); |
3748 | } | |
3749 | ||
3750 | return NULL_TREE; | |
3751 | } | |
3752 | ||
51d464b6 JJ |
3753 | /* Try to fold expressions like |
3754 | (struct S *) (&a[0].D.2378 + 12) | |
3755 | into | |
3756 | &MEM <struct T> [(void *)&a + 12B] | |
3757 | This is something normally done by gimple_fold_stmt_to_constant_1 | |
3758 | on GIMPLE, but is undesirable on GENERIC if we are e.g. going to | |
3759 | dereference the address because some details are lost. | |
3760 | For pointer comparisons we want such folding though so that | |
3761 | match.pd address_compare optimization works. */ | |
3762 | ||
3763 | static tree | |
3764 | cxx_maybe_fold_addr_pointer_plus (tree t) | |
3765 | { | |
3766 | while (CONVERT_EXPR_P (t) | |
3767 | && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0)))) | |
3768 | t = TREE_OPERAND (t, 0); | |
3769 | if (TREE_CODE (t) != POINTER_PLUS_EXPR) | |
3770 | return NULL_TREE; | |
3771 | tree op0 = TREE_OPERAND (t, 0); | |
3772 | tree op1 = TREE_OPERAND (t, 1); | |
3773 | if (TREE_CODE (op1) != INTEGER_CST) | |
3774 | return NULL_TREE; | |
3775 | while (CONVERT_EXPR_P (op0) | |
3776 | && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))) | |
3777 | op0 = TREE_OPERAND (op0, 0); | |
3778 | if (TREE_CODE (op0) != ADDR_EXPR) | |
3779 | return NULL_TREE; | |
3780 | op1 = fold_convert (ptr_type_node, op1); | |
3781 | tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1); | |
3782 | return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r); | |
3783 | } | |
3784 | ||
2d76680f PC |
3785 | /* Subroutine of cxx_eval_constant_expression. |
3786 | Like cxx_eval_unary_expression, except for binary expressions. */ | |
3787 | ||
3788 | static tree | |
3e605b20 | 3789 | cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t, |
72f76540 | 3790 | value_cat lval, |
2d76680f PC |
3791 | bool *non_constant_p, bool *overflow_p) |
3792 | { | |
618d6c1c | 3793 | tree r = NULL_TREE; |
2d76680f PC |
3794 | tree orig_lhs = TREE_OPERAND (t, 0); |
3795 | tree orig_rhs = TREE_OPERAND (t, 1); | |
3796 | tree lhs, rhs; | |
72f76540 | 3797 | lhs = cxx_eval_constant_expression (ctx, orig_lhs, vc_prvalue, |
5a804683 | 3798 | non_constant_p, overflow_p); |
c8a66fc9 JM |
3799 | /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer |
3800 | subtraction. */ | |
3801 | if (*non_constant_p) | |
3802 | return t; | |
72f76540 | 3803 | rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue, |
5a804683 | 3804 | non_constant_p, overflow_p); |
c8a66fc9 JM |
3805 | if (*non_constant_p) |
3806 | return t; | |
f899317e JM |
3807 | |
3808 | location_t loc = EXPR_LOCATION (t); | |
3809 | enum tree_code code = TREE_CODE (t); | |
3810 | tree type = TREE_TYPE (t); | |
618d6c1c PP |
3811 | |
3812 | if (code == EQ_EXPR || code == NE_EXPR) | |
3813 | { | |
3814 | bool is_code_eq = (code == EQ_EXPR); | |
3815 | ||
3816 | if (TREE_CODE (lhs) == PTRMEM_CST | |
3817 | && TREE_CODE (rhs) == PTRMEM_CST) | |
9b0607de JM |
3818 | { |
3819 | tree lmem = PTRMEM_CST_MEMBER (lhs); | |
3820 | tree rmem = PTRMEM_CST_MEMBER (rhs); | |
3821 | bool eq; | |
3822 | if (TREE_CODE (lmem) == TREE_CODE (rmem) | |
3823 | && TREE_CODE (lmem) == FIELD_DECL | |
3824 | && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE | |
3825 | && same_type_p (DECL_CONTEXT (lmem), | |
3826 | DECL_CONTEXT (rmem))) | |
3827 | /* If both refer to (possibly different) members of the same union | |
3828 | (12.3), they compare equal. */ | |
3829 | eq = true; | |
3830 | else | |
3831 | eq = cp_tree_equal (lhs, rhs); | |
3832 | r = constant_boolean_node (eq == is_code_eq, type); | |
3833 | } | |
618d6c1c PP |
3834 | else if ((TREE_CODE (lhs) == PTRMEM_CST |
3835 | || TREE_CODE (rhs) == PTRMEM_CST) | |
3836 | && (null_member_pointer_value_p (lhs) | |
3837 | || null_member_pointer_value_p (rhs))) | |
3838 | r = constant_boolean_node (!is_code_eq, type); | |
dd5dda56 JM |
3839 | else if (TREE_CODE (lhs) == PTRMEM_CST) |
3840 | lhs = cplus_expand_constant (lhs); | |
3841 | else if (TREE_CODE (rhs) == PTRMEM_CST) | |
3842 | rhs = cplus_expand_constant (rhs); | |
618d6c1c | 3843 | } |
51d464b6 JJ |
3844 | if (r == NULL_TREE |
3845 | && TREE_CODE_CLASS (code) == tcc_comparison | |
3846 | && POINTER_TYPE_P (TREE_TYPE (lhs))) | |
3847 | { | |
3848 | if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs)) | |
3849 | lhs = fold_convert (TREE_TYPE (lhs), lhso); | |
3850 | if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs)) | |
3851 | rhs = fold_convert (TREE_TYPE (rhs), rhso); | |
3852 | } | |
8bada5cd MS |
3853 | if (code == POINTER_PLUS_EXPR && !*non_constant_p |
3854 | && integer_zerop (lhs) && !integer_zerop (rhs)) | |
3855 | { | |
3856 | if (!ctx->quiet) | |
3857 | error ("arithmetic involving a null pointer in %qE", lhs); | |
bf533db8 | 3858 | *non_constant_p = true; |
8bada5cd MS |
3859 | return t; |
3860 | } | |
ea8661cd JJ |
3861 | else if (code == POINTER_PLUS_EXPR) |
3862 | r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p, | |
3863 | overflow_p); | |
b7689b96 JM |
3864 | else if (code == SPACESHIP_EXPR) |
3865 | { | |
4ed1dc12 | 3866 | r = genericize_spaceship (loc, type, lhs, rhs); |
5c64df80 JJ |
3867 | return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, |
3868 | overflow_p); | |
b7689b96 | 3869 | } |
618d6c1c PP |
3870 | |
3871 | if (r == NULL_TREE) | |
53caa472 | 3872 | { |
3a0bc47c | 3873 | if (ctx->manifestly_const_eval == mce_true |
53caa472 JM |
3874 | && (flag_constexpr_fp_except |
3875 | || TREE_CODE (type) != REAL_TYPE)) | |
f9e900ce JJ |
3876 | { |
3877 | auto ofcc = make_temp_override (folding_cxx_constexpr, true); | |
3878 | r = fold_binary_initializer_loc (loc, code, type, lhs, rhs); | |
3879 | } | |
53caa472 JM |
3880 | else |
3881 | r = fold_binary_loc (loc, code, type, lhs, rhs); | |
3882 | } | |
618d6c1c | 3883 | |
4866b2f5 JJ |
3884 | if (r == NULL_TREE |
3885 | && (code == LSHIFT_EXPR || code == RSHIFT_EXPR) | |
3886 | && TREE_CODE (lhs) == INTEGER_CST | |
3887 | && TREE_CODE (rhs) == INTEGER_CST | |
3888 | && wi::neg_p (wi::to_wide (rhs))) | |
3889 | { | |
3890 | /* For diagnostics and -fpermissive emulate previous behavior of | |
3891 | handling shifts by negative amount. */ | |
3892 | tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs); | |
3893 | if (nrhs) | |
3894 | r = fold_binary_loc (loc, | |
3895 | code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR, | |
3896 | type, lhs, nrhs); | |
3897 | } | |
3898 | ||
f899317e JM |
3899 | if (r == NULL_TREE) |
3900 | { | |
3901 | if (lhs == orig_lhs && rhs == orig_rhs) | |
3902 | r = t; | |
3903 | else | |
3904 | r = build2_loc (loc, code, type, lhs, rhs); | |
3905 | } | |
253a921b MP |
3906 | else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs)) |
3907 | *non_constant_p = true; | |
c8a66fc9 JM |
3908 | /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to |
3909 | a local array in a constexpr function. */ | |
71a93b08 | 3910 | bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs)); |
caee690e | 3911 | if (!ptr) |
134efa82 | 3912 | VERIFY_CONSTANT (r); |
2d76680f PC |
3913 | return r; |
3914 | } | |
3915 | ||
3916 | /* Subroutine of cxx_eval_constant_expression. | |
6851e342 | 3917 | Attempt to evaluate condition expressions. */ |
2d76680f PC |
3918 | |
3919 | static tree | |
3e605b20 | 3920 | cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t, |
72f76540 | 3921 | value_cat lval, |
56632b27 JM |
3922 | bool *non_constant_p, bool *overflow_p, |
3923 | tree *jump_target) | |
2d76680f | 3924 | { |
3e605b20 | 3925 | tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
72f76540 | 3926 | vc_prvalue, |
5a804683 | 3927 | non_constant_p, overflow_p); |
2d76680f | 3928 | VERIFY_CONSTANT (val); |
117c6426 JJ |
3929 | if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t)) |
3930 | { | |
3931 | /* Evaluate the condition as if it was | |
3932 | if (__builtin_is_constant_evaluated ()), i.e. defer it if not | |
3933 | ctx->manifestly_const_eval (as sometimes we try to constant evaluate | |
3934 | without manifestly_const_eval even expressions or parts thereof which | |
3935 | will later be manifestly const_eval evaluated), otherwise fold it to | |
3936 | true. */ | |
3a0bc47c | 3937 | if (ctx->manifestly_const_eval == mce_unknown) |
117c6426 JJ |
3938 | { |
3939 | *non_constant_p = true; | |
3940 | return t; | |
3941 | } | |
3a0bc47c PP |
3942 | val = constant_boolean_node (ctx->manifestly_const_eval == mce_true, |
3943 | boolean_type_node); | |
117c6426 | 3944 | } |
2d76680f | 3945 | /* Don't VERIFY_CONSTANT the other operands. */ |
6851e342 MP |
3946 | const bool zero_p = integer_zerop (val); |
3947 | if (zero_p) | |
43574e4f JJ |
3948 | val = TREE_OPERAND (t, 2); |
3949 | else | |
3950 | val = TREE_OPERAND (t, 1); | |
3951 | if (TREE_CODE (t) == IF_STMT && !val) | |
3952 | val = void_node; | |
6851e342 MP |
3953 | |
3954 | /* P2564: a subexpression of a manifestly constant-evaluated expression | |
3955 | or conversion is an immediate function context. */ | |
3956 | if (ctx->manifestly_const_eval != mce_true | |
3957 | && !in_immediate_context () | |
3958 | && cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2), | |
3959 | ctx->manifestly_const_eval)) | |
3960 | { | |
3961 | *non_constant_p = true; | |
3962 | return t; | |
3963 | } | |
3964 | ||
ecd11aca MP |
3965 | /* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still |
3966 | serve as the initializer for the same object as the outer TARGET_EXPR, | |
3967 | as in | |
3968 | A a = true ? A{} : A{}; | |
3969 | so strip the inner TARGET_EXPR so we don't materialize a temporary. */ | |
3970 | if (TREE_CODE (val) == TARGET_EXPR) | |
3971 | val = TARGET_EXPR_INITIAL (val); | |
43574e4f JJ |
3972 | return cxx_eval_constant_expression (ctx, val, lval, non_constant_p, |
3973 | overflow_p, jump_target); | |
2d76680f PC |
3974 | } |
3975 | ||
f370e36d JJ |
3976 | /* Subroutine of cxx_eval_constant_expression. |
3977 | Attempt to evaluate vector condition expressions. Unlike | |
3978 | cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal | |
3979 | ternary arithmetics operation, where all 3 arguments have to be | |
3980 | evaluated as constants and then folding computes the result from | |
3981 | them. */ | |
3982 | ||
3983 | static tree | |
3984 | cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t, | |
3985 | bool *non_constant_p, bool *overflow_p) | |
3986 | { | |
3987 | tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), | |
72f76540 | 3988 | vc_prvalue, |
f370e36d JJ |
3989 | non_constant_p, overflow_p); |
3990 | VERIFY_CONSTANT (arg1); | |
3991 | tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), | |
72f76540 | 3992 | vc_prvalue, |
f370e36d JJ |
3993 | non_constant_p, overflow_p); |
3994 | VERIFY_CONSTANT (arg2); | |
3995 | tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2), | |
72f76540 | 3996 | vc_prvalue, |
f370e36d JJ |
3997 | non_constant_p, overflow_p); |
3998 | VERIFY_CONSTANT (arg3); | |
3999 | location_t loc = EXPR_LOCATION (t); | |
4000 | tree type = TREE_TYPE (t); | |
4001 | tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); | |
4002 | if (r == NULL_TREE) | |
4003 | { | |
4004 | if (arg1 == TREE_OPERAND (t, 0) | |
4005 | && arg2 == TREE_OPERAND (t, 1) | |
4006 | && arg3 == TREE_OPERAND (t, 2)) | |
4007 | r = t; | |
4008 | else | |
4009 | r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); | |
4010 | } | |
4011 | VERIFY_CONSTANT (r); | |
4012 | return r; | |
4013 | } | |
4014 | ||
ceaaf873 JM |
4015 | /* Returns less than, equal to, or greater than zero if KEY is found to be |
4016 | less than, to match, or to be greater than the constructor_elt's INDEX. */ | |
4017 | ||
4018 | static int | |
4019 | array_index_cmp (tree key, tree index) | |
4020 | { | |
4021 | gcc_assert (TREE_CODE (key) == INTEGER_CST); | |
4022 | ||
4023 | switch (TREE_CODE (index)) | |
4024 | { | |
4025 | case INTEGER_CST: | |
4026 | return tree_int_cst_compare (key, index); | |
4027 | case RANGE_EXPR: | |
4028 | { | |
4029 | tree lo = TREE_OPERAND (index, 0); | |
4030 | tree hi = TREE_OPERAND (index, 1); | |
4031 | if (tree_int_cst_lt (key, lo)) | |
4032 | return -1; | |
4033 | else if (tree_int_cst_lt (hi, key)) | |
4034 | return 1; | |
4035 | else | |
4036 | return 0; | |
4037 | } | |
4038 | default: | |
4039 | gcc_unreachable (); | |
4040 | } | |
4041 | } | |
4042 | ||
4043 | /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1 | |
4044 | if none. If INSERT is true, insert a matching element rather than fail. */ | |
4045 | ||
4046 | static HOST_WIDE_INT | |
582d2481 | 4047 | find_array_ctor_elt (tree ary, tree dindex, bool insert) |
ceaaf873 JM |
4048 | { |
4049 | if (tree_int_cst_sgn (dindex) < 0) | |
4050 | return -1; | |
4051 | ||
4052 | unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex); | |
4053 | vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary); | |
4054 | unsigned HOST_WIDE_INT len = vec_safe_length (elts); | |
4055 | ||
4056 | unsigned HOST_WIDE_INT end = len; | |
4057 | unsigned HOST_WIDE_INT begin = 0; | |
4058 | ||
4059 | /* If the last element of the CONSTRUCTOR has its own index, we can assume | |
4060 | that the same is true of the other elements and index directly. */ | |
4061 | if (end > 0) | |
4062 | { | |
fe217ba0 | 4063 | tree cindex = (*elts)[end - 1].index; |
c7c09af8 JJ |
4064 | if (cindex == NULL_TREE) |
4065 | { | |
4066 | /* Verify that if the last index is missing, all indexes | |
4067 | are missing. */ | |
4068 | if (flag_checking) | |
4069 | for (unsigned int j = 0; j < len - 1; ++j) | |
4070 | gcc_assert ((*elts)[j].index == NULL_TREE); | |
4071 | if (i < end) | |
4072 | return i; | |
4073 | else | |
4074 | { | |
4075 | begin = end; | |
4076 | if (i == end) | |
4077 | /* If the element is to be added right at the end, | |
4078 | make sure it is added with cleared index too. */ | |
4079 | dindex = NULL_TREE; | |
4080 | else if (insert) | |
4081 | /* Otherwise, in order not to break the assumption | |
4082 | that CONSTRUCTOR either has all indexes or none, | |
4083 | we need to add indexes to all elements. */ | |
4084 | for (unsigned int j = 0; j < len; ++j) | |
4085 | (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j); | |
4086 | } | |
4087 | } | |
4088 | else if (TREE_CODE (cindex) == INTEGER_CST | |
4089 | && compare_tree_int (cindex, end - 1) == 0) | |
ceaaf873 JM |
4090 | { |
4091 | if (i < end) | |
4092 | return i; | |
4093 | else | |
4094 | begin = end; | |
4095 | } | |
4096 | } | |
4097 | ||
4098 | /* Otherwise, find a matching index by means of a binary search. */ | |
4099 | while (begin != end) | |
4100 | { | |
4101 | unsigned HOST_WIDE_INT middle = (begin + end) / 2; | |
a7ccb9e7 JM |
4102 | constructor_elt &elt = (*elts)[middle]; |
4103 | tree idx = elt.index; | |
ceaaf873 | 4104 | |
a7ccb9e7 | 4105 | int cmp = array_index_cmp (dindex, idx); |
ceaaf873 JM |
4106 | if (cmp < 0) |
4107 | end = middle; | |
4108 | else if (cmp > 0) | |
4109 | begin = middle + 1; | |
4110 | else | |
a7ccb9e7 JM |
4111 | { |
4112 | if (insert && TREE_CODE (idx) == RANGE_EXPR) | |
4113 | { | |
4114 | /* We need to split the range. */ | |
4115 | constructor_elt e; | |
4116 | tree lo = TREE_OPERAND (idx, 0); | |
4117 | tree hi = TREE_OPERAND (idx, 1); | |
fe217ba0 JJ |
4118 | tree value = elt.value; |
4119 | dindex = fold_convert (sizetype, dindex); | |
a7ccb9e7 JM |
4120 | if (tree_int_cst_lt (lo, dindex)) |
4121 | { | |
4122 | /* There are still some lower elts; shorten the range. */ | |
4123 | tree new_hi = int_const_binop (MINUS_EXPR, dindex, | |
4124 | size_one_node); | |
4125 | if (tree_int_cst_equal (lo, new_hi)) | |
4126 | /* Only one element left, no longer a range. */ | |
4127 | elt.index = lo; | |
4128 | else | |
4129 | TREE_OPERAND (idx, 1) = new_hi; | |
4130 | /* Append the element we want to insert. */ | |
4131 | ++middle; | |
4132 | e.index = dindex; | |
fe217ba0 | 4133 | e.value = unshare_constructor (value); |
a7ccb9e7 JM |
4134 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e); |
4135 | } | |
4136 | else | |
4137 | /* No lower elts, the range elt is now ours. */ | |
4138 | elt.index = dindex; | |
4139 | ||
4140 | if (tree_int_cst_lt (dindex, hi)) | |
4141 | { | |
4142 | /* There are still some higher elts; append a range. */ | |
4143 | tree new_lo = int_const_binop (PLUS_EXPR, dindex, | |
4144 | size_one_node); | |
4145 | if (tree_int_cst_equal (new_lo, hi)) | |
4146 | e.index = hi; | |
4147 | else | |
4148 | e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi); | |
fe217ba0 JJ |
4149 | e.value = unshare_constructor (value); |
4150 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e); | |
a7ccb9e7 JM |
4151 | } |
4152 | } | |
4153 | return middle; | |
4154 | } | |
ceaaf873 JM |
4155 | } |
4156 | ||
4157 | if (insert) | |
4158 | { | |
4159 | constructor_elt e = { dindex, NULL_TREE }; | |
4160 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e); | |
4161 | return end; | |
4162 | } | |
4163 | ||
4164 | return -1; | |
4165 | } | |
4166 | ||
37244b21 PP |
4167 | /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no |
4168 | matching constructor_elt exists, then add one to CTOR. | |
4169 | ||
4170 | As an optimization, if POS_HINT is non-negative then it is used as a guess | |
4171 | for the (integer) index of the matching constructor_elt within CTOR. */ | |
4172 | ||
4173 | static constructor_elt * | |
077dd9b3 | 4174 | get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1) |
37244b21 PP |
4175 | { |
4176 | /* Check the hint first. */ | |
4177 | if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor) | |
4178 | && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index) | |
4179 | return CONSTRUCTOR_ELT (ctor, pos_hint); | |
4180 | ||
4181 | tree type = TREE_TYPE (ctor); | |
4182 | if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE) | |
4183 | { | |
4184 | CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE); | |
4185 | return &CONSTRUCTOR_ELTS (ctor)->last(); | |
4186 | } | |
4187 | else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE) | |
4188 | { | |
e069285c PP |
4189 | if (TREE_CODE (index) == RANGE_EXPR) |
4190 | { | |
4191 | /* Support for RANGE_EXPR index lookups is currently limited to | |
4192 | accessing an existing element via POS_HINT, or appending a new | |
4193 | element to the end of CTOR. ??? Support for other access | |
4194 | patterns may also be needed. */ | |
4195 | vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor); | |
4196 | if (vec_safe_length (elts)) | |
4197 | { | |
4198 | tree lo = TREE_OPERAND (index, 0); | |
4199 | gcc_assert (array_index_cmp (elts->last().index, lo) < 0); | |
4200 | } | |
4201 | CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE); | |
4202 | return &elts->last(); | |
4203 | } | |
4204 | ||
37244b21 PP |
4205 | HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, /*insert*/true); |
4206 | gcc_assert (i >= 0); | |
4207 | constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i); | |
4208 | gcc_assert (cep->index == NULL_TREE | |
4209 | || TREE_CODE (cep->index) != RANGE_EXPR); | |
4210 | return cep; | |
4211 | } | |
4212 | else | |
4213 | { | |
94ff4c9d JM |
4214 | gcc_assert (TREE_CODE (index) == FIELD_DECL |
4215 | && (same_type_ignoring_top_level_qualifiers_p | |
4216 | (DECL_CONTEXT (index), TREE_TYPE (ctor)))); | |
37244b21 PP |
4217 | |
4218 | /* We must keep the CONSTRUCTOR's ELTS in FIELD order. | |
4219 | Usually we meet initializers in that order, but it is | |
4220 | possible for base types to be placed not in program | |
4221 | order. */ | |
4222 | tree fields = TYPE_FIELDS (DECL_CONTEXT (index)); | |
4223 | unsigned HOST_WIDE_INT idx = 0; | |
4224 | constructor_elt *cep = NULL; | |
4225 | ||
4226 | /* Check if we're changing the active member of a union. */ | |
4227 | if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor) | |
4228 | && CONSTRUCTOR_ELT (ctor, 0)->index != index) | |
4229 | vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0); | |
4230 | /* If the bit offset of INDEX is larger than that of the last | |
4231 | constructor_elt, then we can just immediately append a new | |
4232 | constructor_elt to the end of CTOR. */ | |
4233 | else if (CONSTRUCTOR_NELTS (ctor) | |
4234 | && tree_int_cst_compare (bit_position (index), | |
4235 | bit_position (CONSTRUCTOR_ELTS (ctor) | |
4236 | ->last().index)) > 0) | |
4237 | { | |
4238 | idx = CONSTRUCTOR_NELTS (ctor); | |
4239 | goto insert; | |
4240 | } | |
4241 | ||
4242 | /* Otherwise, we need to iterate over CTOR to find or insert INDEX | |
4243 | appropriately. */ | |
4244 | ||
4245 | for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep); | |
4246 | idx++, fields = DECL_CHAIN (fields)) | |
4247 | { | |
4248 | if (index == cep->index) | |
4249 | goto found; | |
4250 | ||
4251 | /* The field we're initializing must be on the field | |
4252 | list. Look to see if it is present before the | |
4253 | field the current ELT initializes. */ | |
4254 | for (; fields != cep->index; fields = DECL_CHAIN (fields)) | |
4255 | if (index == fields) | |
4256 | goto insert; | |
4257 | } | |
4258 | /* We fell off the end of the CONSTRUCTOR, so insert a new | |
4259 | entry at the end. */ | |
4260 | ||
4261 | insert: | |
4262 | { | |
4263 | constructor_elt ce = { index, NULL_TREE }; | |
4264 | ||
4265 | vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce); | |
4266 | cep = CONSTRUCTOR_ELT (ctor, idx); | |
4267 | } | |
4268 | found:; | |
4269 | ||
4270 | return cep; | |
4271 | } | |
4272 | } | |
4273 | ||
abdc16c8 MS |
4274 | /* Under the control of CTX, issue a detailed diagnostic for |
4275 | an out-of-bounds subscript INDEX into the expression ARRAY. */ | |
4276 | ||
4277 | static void | |
043666e0 | 4278 | diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index) |
abdc16c8 MS |
4279 | { |
4280 | if (!ctx->quiet) | |
4281 | { | |
4282 | tree arraytype = TREE_TYPE (array); | |
4283 | ||
4284 | /* Convert the unsigned array subscript to a signed integer to avoid | |
4285 | printing huge numbers for small negative values. */ | |
4286 | tree sidx = fold_convert (ssizetype, index); | |
043666e0 | 4287 | STRIP_ANY_LOCATION_WRAPPER (array); |
abdc16c8 MS |
4288 | if (DECL_P (array)) |
4289 | { | |
4f870007 | 4290 | auto_diagnostic_group d; |
08b3748c | 4291 | if (TYPE_DOMAIN (arraytype)) |
043666e0 JM |
4292 | error_at (loc, "array subscript value %qE is outside the bounds " |
4293 | "of array %qD of type %qT", sidx, array, arraytype); | |
08b3748c | 4294 | else |
043666e0 JM |
4295 | error_at (loc, "nonzero array subscript %qE is used with array %qD of " |
4296 | "type %qT with unknown bounds", sidx, array, arraytype); | |
abdc16c8 MS |
4297 | inform (DECL_SOURCE_LOCATION (array), "declared here"); |
4298 | } | |
08b3748c | 4299 | else if (TYPE_DOMAIN (arraytype)) |
043666e0 JM |
4300 | error_at (loc, "array subscript value %qE is outside the bounds " |
4301 | "of array type %qT", sidx, arraytype); | |
08b3748c | 4302 | else |
043666e0 JM |
4303 | error_at (loc, "nonzero array subscript %qE is used with array of type %qT " |
4304 | "with unknown bounds", sidx, arraytype); | |
abdc16c8 MS |
4305 | } |
4306 | } | |
ceaaf873 | 4307 | |
74f8705e MP |
4308 | /* Return the number of elements for TYPE (which is an ARRAY_TYPE or |
4309 | a VECTOR_TYPE). */ | |
4310 | ||
4311 | static tree | |
4312 | get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type, | |
4313 | bool *non_constant_p, bool *overflow_p) | |
4314 | { | |
4315 | tree nelts; | |
4316 | if (TREE_CODE (type) == ARRAY_TYPE) | |
4317 | { | |
4318 | if (TYPE_DOMAIN (type)) | |
4319 | nelts = array_type_nelts_top (type); | |
4320 | else | |
4321 | nelts = size_zero_node; | |
4322 | } | |
4323 | else if (VECTOR_TYPE_P (type)) | |
4324 | nelts = size_int (TYPE_VECTOR_SUBPARTS (type)); | |
4325 | else | |
4326 | gcc_unreachable (); | |
4327 | ||
4328 | /* For VLAs, the number of elements won't be an integer constant. */ | |
72f76540 | 4329 | nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue, |
74f8705e MP |
4330 | non_constant_p, overflow_p); |
4331 | return nelts; | |
4332 | } | |
4333 | ||
d6b46fca NS |
4334 | /* Extract element INDEX consisting of CHARS_PER_ELT chars from |
4335 | STRING_CST STRING. */ | |
4336 | ||
4337 | static tree | |
4338 | extract_string_elt (tree string, unsigned chars_per_elt, unsigned index) | |
4339 | { | |
4340 | tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string))); | |
4341 | tree r; | |
4342 | ||
4343 | if (chars_per_elt == 1) | |
4344 | r = build_int_cst (type, TREE_STRING_POINTER (string)[index]); | |
4345 | else | |
4346 | { | |
4347 | const unsigned char *ptr | |
4348 | = ((const unsigned char *)TREE_STRING_POINTER (string) | |
4349 | + index * chars_per_elt); | |
4350 | r = native_interpret_expr (type, ptr, chars_per_elt); | |
4351 | } | |
4352 | return r; | |
4353 | } | |
4354 | ||
043666e0 JM |
4355 | /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the |
4356 | subscript, diagnose any problems with it, and return the result. */ | |
4357 | ||
4358 | static tree | |
4359 | eval_and_check_array_index (const constexpr_ctx *ctx, | |
4360 | tree t, bool allow_one_past, | |
4361 | bool *non_constant_p, bool *overflow_p) | |
4362 | { | |
f9d0ca40 | 4363 | location_t loc = cp_expr_loc_or_input_loc (t); |
043666e0 JM |
4364 | tree ary = TREE_OPERAND (t, 0); |
4365 | t = TREE_OPERAND (t, 1); | |
72f76540 | 4366 | tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue, |
043666e0 JM |
4367 | non_constant_p, overflow_p); |
4368 | VERIFY_CONSTANT (index); | |
4369 | ||
4370 | if (!tree_fits_shwi_p (index) | |
4371 | || tree_int_cst_sgn (index) < 0) | |
4372 | { | |
4373 | diag_array_subscript (loc, ctx, ary, index); | |
4374 | *non_constant_p = true; | |
4375 | return t; | |
4376 | } | |
4377 | ||
4378 | tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p, | |
4379 | overflow_p); | |
4380 | VERIFY_CONSTANT (nelts); | |
4381 | if (allow_one_past | |
4382 | ? !tree_int_cst_le (index, nelts) | |
4383 | : !tree_int_cst_lt (index, nelts)) | |
4384 | { | |
4385 | diag_array_subscript (loc, ctx, ary, index); | |
4386 | *non_constant_p = true; | |
4387 | return t; | |
4388 | } | |
4389 | ||
4390 | return index; | |
4391 | } | |
4392 | ||
2d76680f PC |
4393 | /* Subroutine of cxx_eval_constant_expression. |
4394 | Attempt to reduce a reference to an array slot. */ | |
4395 | ||
4396 | static tree | |
3e605b20 | 4397 | cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, |
72f76540 | 4398 | value_cat lval, |
2d76680f PC |
4399 | bool *non_constant_p, bool *overflow_p) |
4400 | { | |
4401 | tree oldary = TREE_OPERAND (t, 0); | |
3e605b20 | 4402 | tree ary = cxx_eval_constant_expression (ctx, oldary, |
92a596e8 | 4403 | lval, |
5a804683 | 4404 | non_constant_p, overflow_p); |
2d76680f PC |
4405 | if (*non_constant_p) |
4406 | return t; | |
bc2687dd JJ |
4407 | if (!lval |
4408 | && TREE_CODE (ary) == VIEW_CONVERT_EXPR | |
043666e0 JM |
4409 | && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0))) |
4410 | && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0)))) | |
4411 | ary = TREE_OPERAND (ary, 0); | |
3b9997bb | 4412 | |
043666e0 JM |
4413 | tree oldidx = TREE_OPERAND (t, 1); |
4414 | tree index = eval_and_check_array_index (ctx, t, lval, | |
4415 | non_constant_p, overflow_p); | |
4416 | if (*non_constant_p) | |
4417 | return t; | |
5453bfed | 4418 | |
bc2a38df JJ |
4419 | if (lval && ary == oldary && index == oldidx) |
4420 | return t; | |
72f76540 JM |
4421 | else if (lval == vc_discard) |
4422 | return t; | |
bc2a38df JJ |
4423 | else if (lval) |
4424 | return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL); | |
4425 | ||
043666e0 JM |
4426 | unsigned len = 0, elem_nchars = 1; |
4427 | tree elem_type = TREE_TYPE (TREE_TYPE (ary)); | |
4428 | if (TREE_CODE (ary) == CONSTRUCTOR) | |
4429 | len = CONSTRUCTOR_NELTS (ary); | |
4430 | else if (TREE_CODE (ary) == STRING_CST) | |
4431 | { | |
4432 | elem_nchars = (TYPE_PRECISION (elem_type) | |
4433 | / TYPE_PRECISION (char_type_node)); | |
4434 | len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars; | |
4435 | } | |
4436 | else if (TREE_CODE (ary) == VECTOR_CST) | |
4437 | /* We don't create variable-length VECTOR_CSTs. */ | |
4438 | len = VECTOR_CST_NELTS (ary).to_constant (); | |
4439 | else | |
4440 | { | |
4441 | /* We can't do anything with other tree codes, so use | |
4442 | VERIFY_CONSTANT to complain and fail. */ | |
4443 | VERIFY_CONSTANT (ary); | |
4444 | gcc_unreachable (); | |
4445 | } | |
4446 | ||
ceaaf873 | 4447 | bool found; |
043666e0 | 4448 | HOST_WIDE_INT i = 0; |
ceaaf873 JM |
4449 | if (TREE_CODE (ary) == CONSTRUCTOR) |
4450 | { | |
4451 | HOST_WIDE_INT ix = find_array_ctor_elt (ary, index); | |
4452 | found = (ix >= 0); | |
4453 | if (found) | |
4454 | i = ix; | |
3b9997bb | 4455 | } |
ceaaf873 | 4456 | else |
043666e0 JM |
4457 | { |
4458 | i = tree_to_shwi (index); | |
4459 | found = (i < len); | |
4460 | } | |
3b9997bb | 4461 | |
fd2bfee5 JM |
4462 | if (found) |
4463 | { | |
4464 | tree r; | |
4465 | if (TREE_CODE (ary) == CONSTRUCTOR) | |
4466 | r = (*CONSTRUCTOR_ELTS (ary))[i].value; | |
4467 | else if (TREE_CODE (ary) == VECTOR_CST) | |
4468 | r = VECTOR_CST_ELT (ary, i); | |
fd2bfee5 | 4469 | else |
d6b46fca NS |
4470 | r = extract_string_elt (ary, elem_nchars, i); |
4471 | ||
fd2bfee5 JM |
4472 | if (r) |
4473 | /* Don't VERIFY_CONSTANT here. */ | |
4474 | return r; | |
2d76680f | 4475 | |
fd2bfee5 | 4476 | /* Otherwise the element doesn't have a value yet. */ |
2d76680f | 4477 | } |
3b9997bb | 4478 | |
fd2bfee5 JM |
4479 | /* Not found. */ |
4480 | ||
a426b91b PP |
4481 | if (is_really_empty_class (elem_type, /*ignore_vptr*/false)) |
4482 | return build_constructor (elem_type, NULL); | |
4483 | ||
fd2bfee5 | 4484 | if (TREE_CODE (ary) == CONSTRUCTOR |
e8c48716 | 4485 | && CONSTRUCTOR_NO_CLEARING (ary)) |
2d76680f | 4486 | { |
fd2bfee5 JM |
4487 | /* 'ary' is part of the aggregate initializer we're currently |
4488 | building; if there's no initializer for this element yet, | |
4489 | that's an error. */ | |
4490 | if (!ctx->quiet) | |
4491 | error ("accessing uninitialized array element"); | |
4492 | *non_constant_p = true; | |
4493 | return t; | |
2d76680f | 4494 | } |
fd2bfee5 JM |
4495 | |
4496 | /* If it's within the array bounds but doesn't have an explicit | |
0712ea63 JM |
4497 | initializer, it's initialized from {}. But use build_value_init |
4498 | directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */ | |
4499 | tree val; | |
0df73bee | 4500 | constexpr_ctx new_ctx; |
a426b91b | 4501 | if (CP_AGGREGATE_TYPE_P (elem_type)) |
0712ea63 JM |
4502 | { |
4503 | tree empty_ctor = build_constructor (init_list_type_node, NULL); | |
4504 | val = digest_init (elem_type, empty_ctor, tf_warning_or_error); | |
a42f8120 MP |
4505 | } |
4506 | else | |
4507 | val = build_value_init (elem_type, tf_warning_or_error); | |
4508 | ||
6e424feb MP |
4509 | /* Create a new constructor only if we don't already have a suitable one. */ |
4510 | const bool new_ctor = (!SCALAR_TYPE_P (elem_type) | |
4511 | && (!ctx->ctor | |
4512 | || !same_type_ignoring_top_level_qualifiers_p | |
4513 | (elem_type, TREE_TYPE (ctx->ctor)))); | |
4514 | if (new_ctor) | |
a42f8120 | 4515 | { |
0df73bee | 4516 | new_ctx = *ctx; |
6e424feb MP |
4517 | /* We clear the object here. We used to replace it with T, but that |
4518 | caused problems (101371, 108158); and anyway, T is the initializer, | |
4519 | not the target object. */ | |
4520 | new_ctx.object = NULL_TREE; | |
0df73bee MP |
4521 | new_ctx.ctor = build_constructor (elem_type, NULL); |
4522 | ctx = &new_ctx; | |
0712ea63 | 4523 | } |
0df73bee MP |
4524 | t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p, |
4525 | overflow_p); | |
6e424feb | 4526 | if (new_ctor && t != ctx->ctor) |
0df73bee MP |
4527 | free_constructor (ctx->ctor); |
4528 | return t; | |
2d76680f PC |
4529 | } |
4530 | ||
4531 | /* Subroutine of cxx_eval_constant_expression. | |
4532 | Attempt to reduce a field access of a value of class type. */ | |
4533 | ||
4534 | static tree | |
3e605b20 | 4535 | cxx_eval_component_reference (const constexpr_ctx *ctx, tree t, |
72f76540 | 4536 | value_cat lval, |
2d76680f PC |
4537 | bool *non_constant_p, bool *overflow_p) |
4538 | { | |
4539 | unsigned HOST_WIDE_INT i; | |
4540 | tree field; | |
4541 | tree value; | |
4542 | tree part = TREE_OPERAND (t, 1); | |
4543 | tree orig_whole = TREE_OPERAND (t, 0); | |
3e605b20 | 4544 | tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
92a596e8 | 4545 | lval, |
5a804683 | 4546 | non_constant_p, overflow_p); |
7107ea6f PP |
4547 | if (*non_constant_p) |
4548 | return t; | |
a7f8415c | 4549 | if (INDIRECT_REF_P (whole) |
bf533db8 JJ |
4550 | && integer_zerop (TREE_OPERAND (whole, 0))) |
4551 | { | |
4552 | if (!ctx->quiet) | |
4553 | error ("dereferencing a null pointer in %qE", orig_whole); | |
4554 | *non_constant_p = true; | |
4555 | return t; | |
4556 | } | |
8bada5cd | 4557 | |
dcdbc004 JM |
4558 | if (TREE_CODE (whole) == PTRMEM_CST) |
4559 | whole = cplus_expand_constant (whole); | |
2d76680f PC |
4560 | if (whole == orig_whole) |
4561 | return t; | |
72f76540 JM |
4562 | if (lval == vc_discard) |
4563 | return t; | |
92a596e8 | 4564 | if (lval) |
2d76680f PC |
4565 | return fold_build3 (COMPONENT_REF, TREE_TYPE (t), |
4566 | whole, part, NULL_TREE); | |
4567 | /* Don't VERIFY_CONSTANT here; we only want to check that we got a | |
4568 | CONSTRUCTOR. */ | |
7107ea6f | 4569 | if (TREE_CODE (whole) != CONSTRUCTOR) |
2d76680f | 4570 | { |
2b3ab879 | 4571 | if (!ctx->quiet) |
2d76680f PC |
4572 | error ("%qE is not a constant expression", orig_whole); |
4573 | *non_constant_p = true; | |
7107ea6f | 4574 | return t; |
2d76680f | 4575 | } |
7107ea6f PP |
4576 | if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole)) |
4577 | && DECL_MUTABLE_P (part)) | |
2d76680f | 4578 | { |
2b3ab879 | 4579 | if (!ctx->quiet) |
2d76680f PC |
4580 | error ("mutable %qD is not usable in a constant expression", part); |
4581 | *non_constant_p = true; | |
7107ea6f | 4582 | return t; |
2d76680f | 4583 | } |
1d260ab0 | 4584 | |
2db613e5 | 4585 | bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole)); |
2d76680f PC |
4586 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) |
4587 | { | |
2db613e5 JM |
4588 | /* Use name match for PMF fields, as a variant will have a |
4589 | different FIELD_DECL with a different type. */ | |
4590 | if (pmf ? DECL_NAME (field) == DECL_NAME (part) | |
4591 | : field == part) | |
60813a46 JM |
4592 | { |
4593 | if (value) | |
5b884e94 JJ |
4594 | { |
4595 | STRIP_ANY_LOCATION_WRAPPER (value); | |
4596 | return value; | |
4597 | } | |
60813a46 JM |
4598 | else |
4599 | /* We're in the middle of initializing it. */ | |
4600 | break; | |
4601 | } | |
2d76680f | 4602 | } |
1d260ab0 | 4603 | if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE) |
2d76680f | 4604 | { |
1d260ab0 | 4605 | if (CONSTRUCTOR_NELTS (whole) > 0) |
b599bf9d | 4606 | { |
1d260ab0 NS |
4607 | /* DR 1188 says we don't have to deal with this. */ |
4608 | if (!ctx->quiet) | |
4609 | { | |
4610 | constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0); | |
4611 | if (cep->value == NULL_TREE) | |
4612 | error ("accessing uninitialized member %qD", part); | |
4613 | else | |
4614 | error ("accessing %qD member instead of initialized %qD member " | |
4615 | "in constant expression", part, cep->index); | |
4616 | } | |
4617 | *non_constant_p = true; | |
4618 | return t; | |
4619 | } | |
4620 | else if (!CONSTRUCTOR_NO_CLEARING (whole)) | |
4621 | { | |
4622 | /* Value-initialized union, check if looking at the first member. */ | |
4623 | tree first = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (whole))); | |
4624 | if (first != part) | |
4625 | { | |
4626 | if (!ctx->quiet) | |
4627 | error ("accessing %qD member instead of initialized %qD " | |
4628 | "member in constant expression", part, first); | |
4629 | *non_constant_p = true; | |
4630 | return t; | |
4631 | } | |
b599bf9d | 4632 | } |
2d76680f PC |
4633 | } |
4634 | ||
188e53bd JM |
4635 | /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty |
4636 | classes never get represented; throw together a value now. */ | |
dbcd32f8 | 4637 | if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
188e53bd JM |
4638 | return build_constructor (TREE_TYPE (t), NULL); |
4639 | ||
2db613e5 JM |
4640 | gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole))); |
4641 | ||
e8c48716 | 4642 | if (CONSTRUCTOR_NO_CLEARING (whole)) |
3e605b20 JM |
4643 | { |
4644 | /* 'whole' is part of the aggregate initializer we're currently | |
4645 | building; if there's no initializer for this member yet, that's an | |
188e53bd | 4646 | error. */ |
2b3ab879 | 4647 | if (!ctx->quiet) |
3e605b20 JM |
4648 | error ("accessing uninitialized member %qD", part); |
4649 | *non_constant_p = true; | |
4650 | return t; | |
4651 | } | |
4652 | ||
2d76680f PC |
4653 | /* If there's no explicit init for this field, it's value-initialized. */ |
4654 | value = build_value_init (TREE_TYPE (t), tf_warning_or_error); | |
3e605b20 | 4655 | return cxx_eval_constant_expression (ctx, value, |
92a596e8 | 4656 | lval, |
5a804683 | 4657 | non_constant_p, overflow_p); |
2d76680f PC |
4658 | } |
4659 | ||
4660 | /* Subroutine of cxx_eval_constant_expression. | |
4661 | Attempt to reduce a field access of a value of class type that is | |
4662 | expressed as a BIT_FIELD_REF. */ | |
4663 | ||
4664 | static tree | |
3e605b20 | 4665 | cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t, |
72f76540 | 4666 | value_cat lval, |
2d76680f PC |
4667 | bool *non_constant_p, bool *overflow_p) |
4668 | { | |
4669 | tree orig_whole = TREE_OPERAND (t, 0); | |
4670 | tree retval, fldval, utype, mask; | |
4671 | bool fld_seen = false; | |
4672 | HOST_WIDE_INT istart, isize; | |
3e605b20 | 4673 | tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
92a596e8 | 4674 | lval, |
5a804683 | 4675 | non_constant_p, overflow_p); |
2d76680f PC |
4676 | tree start, field, value; |
4677 | unsigned HOST_WIDE_INT i; | |
4678 | ||
4679 | if (whole == orig_whole) | |
4680 | return t; | |
4681 | /* Don't VERIFY_CONSTANT here; we only want to check that we got a | |
4682 | CONSTRUCTOR. */ | |
4683 | if (!*non_constant_p | |
4684 | && TREE_CODE (whole) != VECTOR_CST | |
4685 | && TREE_CODE (whole) != CONSTRUCTOR) | |
4686 | { | |
2b3ab879 | 4687 | if (!ctx->quiet) |
2d76680f PC |
4688 | error ("%qE is not a constant expression", orig_whole); |
4689 | *non_constant_p = true; | |
4690 | } | |
4691 | if (*non_constant_p) | |
4692 | return t; | |
4693 | ||
4c334e0e JJ |
4694 | if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t))) |
4695 | { | |
4696 | if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole, | |
4697 | TREE_OPERAND (t, 1), TREE_OPERAND (t, 2))) | |
4698 | return r; | |
4699 | if (!ctx->quiet) | |
4700 | error ("%qE is not a constant expression", orig_whole); | |
4701 | *non_constant_p = true; | |
4702 | return t; | |
4703 | } | |
2d76680f PC |
4704 | |
4705 | start = TREE_OPERAND (t, 2); | |
4706 | istart = tree_to_shwi (start); | |
4707 | isize = tree_to_shwi (TREE_OPERAND (t, 1)); | |
4708 | utype = TREE_TYPE (t); | |
4709 | if (!TYPE_UNSIGNED (utype)) | |
4710 | utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1); | |
4711 | retval = build_int_cst (utype, 0); | |
4712 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) | |
4713 | { | |
4714 | tree bitpos = bit_position (field); | |
5b884e94 | 4715 | STRIP_ANY_LOCATION_WRAPPER (value); |
2d76680f PC |
4716 | if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1)) |
4717 | return value; | |
4718 | if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE | |
4719 | && TREE_CODE (value) == INTEGER_CST | |
4720 | && tree_fits_shwi_p (bitpos) | |
4721 | && tree_fits_shwi_p (DECL_SIZE (field))) | |
4722 | { | |
4723 | HOST_WIDE_INT bit = tree_to_shwi (bitpos); | |
4724 | HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field)); | |
4725 | HOST_WIDE_INT shift; | |
4726 | if (bit >= istart && bit + sz <= istart + isize) | |
4727 | { | |
4728 | fldval = fold_convert (utype, value); | |
4729 | mask = build_int_cst_type (utype, -1); | |
4730 | mask = fold_build2 (LSHIFT_EXPR, utype, mask, | |
4731 | size_int (TYPE_PRECISION (utype) - sz)); | |
4732 | mask = fold_build2 (RSHIFT_EXPR, utype, mask, | |
4733 | size_int (TYPE_PRECISION (utype) - sz)); | |
4734 | fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask); | |
4735 | shift = bit - istart; | |
4736 | if (BYTES_BIG_ENDIAN) | |
4737 | shift = TYPE_PRECISION (utype) - shift - sz; | |
4738 | fldval = fold_build2 (LSHIFT_EXPR, utype, fldval, | |
4739 | size_int (shift)); | |
4740 | retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval); | |
4741 | fld_seen = true; | |
4742 | } | |
4743 | } | |
4744 | } | |
4745 | if (fld_seen) | |
4746 | return fold_convert (TREE_TYPE (t), retval); | |
4747 | gcc_unreachable (); | |
4748 | return error_mark_node; | |
4749 | } | |
4750 | ||
896048cf JJ |
4751 | /* Helper for cxx_eval_bit_cast. |
4752 | Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From | |
4753 | types and types of all subobjects have is_union_v<T>, is_pointer_v<T>, | |
4754 | is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static | |
4755 | data members of reference type. */ | |
4756 | ||
4757 | static bool | |
4758 | check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type, | |
4759 | tree orig_type) | |
4760 | { | |
4761 | if (TREE_CODE (type) == UNION_TYPE) | |
4762 | { | |
4763 | if (!ctx->quiet) | |
4764 | { | |
4765 | if (type == orig_type) | |
4766 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4767 | "a union type", "__builtin_bit_cast", type); | |
4768 | else | |
4769 | error_at (loc, "%qs is not a constant expression because %qT " | |
4770 | "contains a union type", "__builtin_bit_cast", | |
4771 | orig_type); | |
4772 | } | |
4773 | return true; | |
4774 | } | |
4775 | if (TREE_CODE (type) == POINTER_TYPE) | |
4776 | { | |
4777 | if (!ctx->quiet) | |
4778 | { | |
4779 | if (type == orig_type) | |
4780 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4781 | "a pointer type", "__builtin_bit_cast", type); | |
4782 | else | |
4783 | error_at (loc, "%qs is not a constant expression because %qT " | |
4784 | "contains a pointer type", "__builtin_bit_cast", | |
4785 | orig_type); | |
4786 | } | |
4787 | return true; | |
4788 | } | |
4789 | if (TREE_CODE (type) == REFERENCE_TYPE) | |
4790 | { | |
4791 | if (!ctx->quiet) | |
4792 | { | |
4793 | if (type == orig_type) | |
4794 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4795 | "a reference type", "__builtin_bit_cast", type); | |
4796 | else | |
4797 | error_at (loc, "%qs is not a constant expression because %qT " | |
4798 | "contains a reference type", "__builtin_bit_cast", | |
4799 | orig_type); | |
4800 | } | |
4801 | return true; | |
4802 | } | |
4803 | if (TYPE_PTRMEM_P (type)) | |
4804 | { | |
4805 | if (!ctx->quiet) | |
4806 | { | |
4807 | if (type == orig_type) | |
4808 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4809 | "a pointer to member type", "__builtin_bit_cast", | |
4810 | type); | |
4811 | else | |
4812 | error_at (loc, "%qs is not a constant expression because %qT " | |
4813 | "contains a pointer to member type", | |
4814 | "__builtin_bit_cast", orig_type); | |
4815 | } | |
4816 | return true; | |
4817 | } | |
4818 | if (TYPE_VOLATILE (type)) | |
4819 | { | |
4820 | if (!ctx->quiet) | |
4821 | { | |
4822 | if (type == orig_type) | |
4823 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4824 | "volatile", "__builtin_bit_cast", type); | |
4825 | else | |
4826 | error_at (loc, "%qs is not a constant expression because %qT " | |
4827 | "contains a volatile subobject", | |
4828 | "__builtin_bit_cast", orig_type); | |
4829 | } | |
4830 | return true; | |
4831 | } | |
4832 | if (TREE_CODE (type) == RECORD_TYPE) | |
4833 | for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) | |
4834 | if (TREE_CODE (field) == FIELD_DECL | |
4835 | && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type)) | |
4836 | return true; | |
4837 | return false; | |
4838 | } | |
4839 | ||
c57c910c JJ |
4840 | /* Helper function for cxx_eval_bit_cast. For unsigned char or |
4841 | std::byte members of CONSTRUCTOR (recursively) if they contain | |
4842 | some indeterminate bits (as set in MASK), remove the ctor elts, | |
4843 | mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the | |
4844 | bits in MASK. */ | |
4845 | ||
4846 | static void | |
4847 | clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask) | |
4848 | { | |
4849 | if (TREE_CODE (t) != CONSTRUCTOR) | |
4850 | return; | |
4851 | ||
4852 | unsigned i, j = 0; | |
4853 | tree index, value; | |
4854 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value) | |
4855 | { | |
4856 | tree type = TREE_TYPE (value); | |
4857 | if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE | |
4858 | && DECL_BIT_FIELD_TYPE (index) != NULL_TREE) | |
4859 | { | |
4860 | if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index))) | |
4861 | { | |
4862 | HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index)); | |
4863 | gcc_assert (fldsz != 0); | |
4864 | HOST_WIDE_INT pos = int_byte_position (index); | |
4865 | HOST_WIDE_INT bpos | |
4866 | = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index)); | |
4867 | bpos %= BITS_PER_UNIT; | |
4868 | HOST_WIDE_INT end | |
4869 | = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT; | |
4870 | gcc_assert (end == 1 || end == 2); | |
4871 | unsigned char *p = mask + pos; | |
4872 | unsigned char mask_save[2]; | |
4873 | mask_save[0] = mask[pos]; | |
4874 | mask_save[1] = end == 2 ? mask[pos + 1] : 0; | |
4875 | if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) | |
4876 | sorry_at (loc, "PDP11 bit-field handling unsupported" | |
4877 | " in %qs", "__builtin_bit_cast"); | |
4878 | else if (BYTES_BIG_ENDIAN) | |
4879 | { | |
4880 | /* Big endian. */ | |
4881 | if (bpos + fldsz <= BITS_PER_UNIT) | |
4882 | *p &= ~(((1 << fldsz) - 1) | |
4883 | << (BITS_PER_UNIT - bpos - fldsz)); | |
4884 | else | |
4885 | { | |
4886 | gcc_assert (bpos); | |
4887 | *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos); | |
4888 | p++; | |
4889 | fldsz -= BITS_PER_UNIT - bpos; | |
4890 | gcc_assert (fldsz && fldsz < BITS_PER_UNIT); | |
4891 | *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz; | |
4892 | } | |
4893 | } | |
4894 | else | |
4895 | { | |
4896 | /* Little endian. */ | |
4897 | if (bpos + fldsz <= BITS_PER_UNIT) | |
4898 | *p &= ~(((1 << fldsz) - 1) << bpos); | |
4899 | else | |
4900 | { | |
4901 | gcc_assert (bpos); | |
4902 | *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos); | |
4903 | p++; | |
4904 | fldsz -= BITS_PER_UNIT - bpos; | |
4905 | gcc_assert (fldsz && fldsz < BITS_PER_UNIT); | |
4906 | *p &= ~((1 << fldsz) - 1); | |
4907 | } | |
4908 | } | |
4909 | if (mask_save[0] != mask[pos] | |
4910 | || (end == 2 && mask_save[1] != mask[pos + 1])) | |
4911 | { | |
4912 | CONSTRUCTOR_NO_CLEARING (t) = 1; | |
4913 | continue; | |
4914 | } | |
4915 | } | |
4916 | } | |
4917 | else if (is_byte_access_type_not_plain_char (type)) | |
4918 | { | |
4919 | HOST_WIDE_INT pos; | |
4920 | if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) | |
4921 | pos = tree_to_shwi (index); | |
4922 | else | |
4923 | pos = int_byte_position (index); | |
4924 | if (mask[pos]) | |
4925 | { | |
4926 | CONSTRUCTOR_NO_CLEARING (t) = 1; | |
4927 | mask[pos] = 0; | |
4928 | continue; | |
4929 | } | |
4930 | } | |
4931 | if (TREE_CODE (value) == CONSTRUCTOR) | |
4932 | { | |
4933 | HOST_WIDE_INT pos; | |
4934 | if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) | |
4935 | pos = tree_to_shwi (index) | |
4936 | * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)))); | |
4937 | else | |
4938 | pos = int_byte_position (index); | |
4939 | clear_uchar_or_std_byte_in_mask (loc, value, mask + pos); | |
4940 | } | |
4941 | if (i != j) | |
4942 | { | |
4943 | CONSTRUCTOR_ELT (t, j)->index = index; | |
4944 | CONSTRUCTOR_ELT (t, j)->value = value; | |
4945 | } | |
4946 | ++j; | |
4947 | } | |
4948 | if (CONSTRUCTOR_NELTS (t) != j) | |
4949 | vec_safe_truncate (CONSTRUCTOR_ELTS (t), j); | |
4950 | } | |
4951 | ||
896048cf JJ |
4952 | /* Subroutine of cxx_eval_constant_expression. |
4953 | Attempt to evaluate a BIT_CAST_EXPR. */ | |
4954 | ||
4955 | static tree | |
4956 | cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p, | |
4957 | bool *overflow_p) | |
4958 | { | |
4959 | if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t), | |
4960 | TREE_TYPE (t)) | |
4961 | || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0), | |
4962 | EXPR_LOCATION (t)), | |
4963 | TREE_TYPE (TREE_OPERAND (t, 0)), | |
4964 | TREE_TYPE (TREE_OPERAND (t, 0)))) | |
4965 | { | |
4966 | *non_constant_p = true; | |
4967 | return t; | |
4968 | } | |
4969 | ||
72f76540 | 4970 | tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue, |
896048cf JJ |
4971 | non_constant_p, overflow_p); |
4972 | if (*non_constant_p) | |
4973 | return t; | |
4974 | ||
4975 | location_t loc = EXPR_LOCATION (t); | |
4976 | if (BITS_PER_UNIT != 8 || CHAR_BIT != 8) | |
4977 | { | |
4978 | if (!ctx->quiet) | |
4979 | sorry_at (loc, "%qs cannot be constant evaluated on the target", | |
4980 | "__builtin_bit_cast"); | |
4981 | *non_constant_p = true; | |
4982 | return t; | |
4983 | } | |
4984 | ||
4985 | if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t)))) | |
4986 | { | |
4987 | if (!ctx->quiet) | |
4988 | sorry_at (loc, "%qs cannot be constant evaluated because the " | |
4989 | "type is too large", "__builtin_bit_cast"); | |
4990 | *non_constant_p = true; | |
4991 | return t; | |
4992 | } | |
4993 | ||
4994 | HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t))); | |
4995 | if (len < 0 || (int) len != len) | |
4996 | { | |
4997 | if (!ctx->quiet) | |
4998 | sorry_at (loc, "%qs cannot be constant evaluated because the " | |
4999 | "type is too large", "__builtin_bit_cast"); | |
5000 | *non_constant_p = true; | |
5001 | return t; | |
5002 | } | |
5003 | ||
5004 | unsigned char buf[64]; | |
5005 | unsigned char *ptr, *mask; | |
5006 | size_t alen = (size_t) len * 2; | |
5007 | if (alen <= sizeof (buf)) | |
5008 | ptr = buf; | |
5009 | else | |
5010 | ptr = XNEWVEC (unsigned char, alen); | |
5011 | mask = ptr + (size_t) len; | |
5012 | /* At the beginning consider everything indeterminate. */ | |
5013 | memset (mask, ~0, (size_t) len); | |
5014 | ||
5015 | if (native_encode_initializer (op, ptr, len, 0, mask) != len) | |
5016 | { | |
5017 | if (!ctx->quiet) | |
5018 | sorry_at (loc, "%qs cannot be constant evaluated because the " | |
5019 | "argument cannot be encoded", "__builtin_bit_cast"); | |
5020 | *non_constant_p = true; | |
5021 | if (ptr != buf) | |
5022 | XDELETE (ptr); | |
5023 | return t; | |
5024 | } | |
5025 | ||
5026 | tree r = NULL_TREE; | |
5027 | if (can_native_interpret_type_p (TREE_TYPE (t))) | |
c57c910c JJ |
5028 | { |
5029 | r = native_interpret_expr (TREE_TYPE (t), ptr, len); | |
5030 | if (is_byte_access_type_not_plain_char (TREE_TYPE (t))) | |
5031 | { | |
5032 | gcc_assert (len == 1); | |
5033 | if (mask[0]) | |
5034 | { | |
5035 | memset (mask, 0, len); | |
5036 | r = build_constructor (TREE_TYPE (r), NULL); | |
5037 | CONSTRUCTOR_NO_CLEARING (r) = 1; | |
5038 | } | |
5039 | } | |
5040 | } | |
896048cf JJ |
5041 | else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE) |
5042 | { | |
5043 | r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len); | |
5044 | if (r != NULL_TREE) | |
c57c910c JJ |
5045 | { |
5046 | clear_type_padding_in_mask (TREE_TYPE (t), mask); | |
5047 | clear_uchar_or_std_byte_in_mask (loc, r, mask); | |
b41a927b JM |
5048 | if (CHECKING_P) |
5049 | { | |
5050 | tree e = cxx_eval_bare_aggregate (ctx, r, vc_prvalue, | |
5051 | non_constant_p, overflow_p); | |
5052 | gcc_checking_assert (e == r); | |
5053 | r = e; | |
5054 | } | |
c57c910c | 5055 | } |
896048cf JJ |
5056 | } |
5057 | ||
5058 | if (r != NULL_TREE) | |
5059 | { | |
5060 | for (int i = 0; i < len; i++) | |
5061 | if (mask[i]) | |
5062 | { | |
5063 | if (!ctx->quiet) | |
5064 | error_at (loc, "%qs accessing uninitialized byte at offset %d", | |
5065 | "__builtin_bit_cast", i); | |
5066 | *non_constant_p = true; | |
5067 | r = t; | |
5068 | break; | |
5069 | } | |
5070 | if (ptr != buf) | |
5071 | XDELETE (ptr); | |
5072 | return r; | |
5073 | } | |
5074 | ||
5075 | if (!ctx->quiet) | |
5076 | sorry_at (loc, "%qs cannot be constant evaluated because the " | |
5077 | "argument cannot be interpreted", "__builtin_bit_cast"); | |
5078 | *non_constant_p = true; | |
5079 | if (ptr != buf) | |
5080 | XDELETE (ptr); | |
5081 | return t; | |
5082 | } | |
5083 | ||
2d76680f PC |
5084 | /* Subroutine of cxx_eval_constant_expression. |
5085 | Evaluate a short-circuited logical expression T in the context | |
5086 | of a given constexpr CALL. BAILOUT_VALUE is the value for | |
5087 | early return. CONTINUE_VALUE is used here purely for | |
5088 | sanity check purposes. */ | |
5089 | ||
5090 | static tree | |
3e605b20 | 5091 | cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t, |
2d76680f | 5092 | tree bailout_value, tree continue_value, |
605a80bb | 5093 | bool *non_constant_p, bool *overflow_p) |
2d76680f PC |
5094 | { |
5095 | tree r; | |
3e605b20 | 5096 | tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
72f76540 | 5097 | vc_prvalue, non_constant_p, |
93b65ed9 | 5098 | overflow_p); |
2d76680f PC |
5099 | VERIFY_CONSTANT (lhs); |
5100 | if (tree_int_cst_equal (lhs, bailout_value)) | |
5101 | return lhs; | |
5102 | gcc_assert (tree_int_cst_equal (lhs, continue_value)); | |
3e605b20 | 5103 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
72f76540 | 5104 | vc_prvalue, non_constant_p, |
5a804683 | 5105 | overflow_p); |
2d76680f PC |
5106 | VERIFY_CONSTANT (r); |
5107 | return r; | |
5108 | } | |
5109 | ||
5110 | /* REF is a COMPONENT_REF designating a particular field. V is a vector of | |
5111 | CONSTRUCTOR elements to initialize (part of) an object containing that | |
5112 | field. Return a pointer to the constructor_elt corresponding to the | |
5113 | initialization of the field. */ | |
5114 | ||
5115 | static constructor_elt * | |
5116 | base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref) | |
5117 | { | |
5118 | tree aggr = TREE_OPERAND (ref, 0); | |
5119 | tree field = TREE_OPERAND (ref, 1); | |
5120 | HOST_WIDE_INT i; | |
5121 | constructor_elt *ce; | |
5122 | ||
5123 | gcc_assert (TREE_CODE (ref) == COMPONENT_REF); | |
5124 | ||
5125 | if (TREE_CODE (aggr) == COMPONENT_REF) | |
5126 | { | |
5127 | constructor_elt *base_ce | |
5128 | = base_field_constructor_elt (v, aggr); | |
5129 | v = CONSTRUCTOR_ELTS (base_ce->value); | |
5130 | } | |
5131 | ||
5132 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
5133 | if (ce->index == field) | |
5134 | return ce; | |
5135 | ||
5136 | gcc_unreachable (); | |
5137 | return NULL; | |
5138 | } | |
5139 | ||
3e605b20 JM |
5140 | /* Some of the expressions fed to the constexpr mechanism are calls to |
5141 | constructors, which have type void. In that case, return the type being | |
5142 | initialized by the constructor. */ | |
5143 | ||
5144 | static tree | |
5145 | initialized_type (tree t) | |
5146 | { | |
5147 | if (TYPE_P (t)) | |
5148 | return t; | |
5dab8b11 | 5149 | tree type = TREE_TYPE (t); |
4d0c18c6 | 5150 | if (TREE_CODE (t) == CALL_EXPR) |
3e605b20 JM |
5151 | { |
5152 | /* A constructor call has void type, so we need to look deeper. */ | |
5153 | tree fn = get_function_named_in_call (t); | |
5154 | if (fn && TREE_CODE (fn) == FUNCTION_DECL | |
5155 | && DECL_CXX_CONSTRUCTOR_P (fn)) | |
5156 | type = DECL_CONTEXT (fn); | |
5157 | } | |
4d0c18c6 JM |
5158 | else if (TREE_CODE (t) == COMPOUND_EXPR) |
5159 | return initialized_type (TREE_OPERAND (t, 1)); | |
5dab8b11 JM |
5160 | else if (TREE_CODE (t) == AGGR_INIT_EXPR) |
5161 | type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t)); | |
5162 | return cv_unqualified (type); | |
3e605b20 JM |
5163 | } |
5164 | ||
5165 | /* We're about to initialize element INDEX of an array or class from VALUE. | |
5166 | Set up NEW_CTX appropriately by adjusting .object to refer to the | |
5167 | subobject and creating a new CONSTRUCTOR if the element is itself | |
5168 | a class or array. */ | |
5169 | ||
5170 | static void | |
5171 | init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx, | |
5172 | tree index, tree &value) | |
5173 | { | |
5174 | new_ctx = *ctx; | |
5175 | ||
5176 | if (index && TREE_CODE (index) != INTEGER_CST | |
3d423c6f PP |
5177 | && TREE_CODE (index) != FIELD_DECL |
5178 | && TREE_CODE (index) != RANGE_EXPR) | |
3e605b20 JM |
5179 | /* This won't have an element in the new CONSTRUCTOR. */ |
5180 | return; | |
5181 | ||
5182 | tree type = initialized_type (value); | |
5183 | if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type)) | |
5184 | /* A non-aggregate member doesn't get its own CONSTRUCTOR. */ | |
5185 | return; | |
abea1c9a JJ |
5186 | if (VECTOR_TYPE_P (type) |
5187 | && VECTOR_TYPE_P (TREE_TYPE (ctx->ctor)) | |
5188 | && index == NULL_TREE) | |
5189 | /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger | |
5190 | vector is constructed from smaller vectors, doesn't get its own | |
5191 | CONSTRUCTOR either. */ | |
5192 | return; | |
3e605b20 JM |
5193 | |
5194 | /* The sub-aggregate initializer might contain a placeholder; | |
5195 | update object to refer to the subobject and ctor to refer to | |
5196 | the (newly created) sub-initializer. */ | |
5197 | if (ctx->object) | |
3d423c6f PP |
5198 | { |
5199 | if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR) | |
5200 | /* There's no well-defined subobject for this index. */ | |
5201 | new_ctx.object = NULL_TREE; | |
5202 | else | |
5203 | new_ctx.object = build_ctor_subob_ref (index, type, ctx->object); | |
5204 | } | |
d19b4342 JM |
5205 | |
5206 | if (is_empty_class (type)) | |
5207 | /* Leave ctor null for an empty subobject, they aren't represented in the | |
5208 | result of evaluation. */ | |
5209 | new_ctx.ctor = NULL_TREE; | |
5210 | else | |
5211 | { | |
5212 | tree elt = build_constructor (type, NULL); | |
5213 | CONSTRUCTOR_NO_CLEARING (elt) = true; | |
5214 | new_ctx.ctor = elt; | |
5215 | } | |
3e605b20 JM |
5216 | |
5217 | if (TREE_CODE (value) == TARGET_EXPR) | |
5218 | /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */ | |
5219 | value = TARGET_EXPR_INITIAL (value); | |
5220 | } | |
5221 | ||
5222 | /* We're about to process an initializer for a class or array TYPE. Make | |
5223 | sure that CTX is set up appropriately. */ | |
5224 | ||
5225 | static void | |
5226 | verify_ctor_sanity (const constexpr_ctx *ctx, tree type) | |
5227 | { | |
5228 | /* We don't bother building a ctor for an empty base subobject. */ | |
5229 | if (is_empty_class (type)) | |
5230 | return; | |
5231 | ||
5232 | /* We're in the middle of an initializer that might involve placeholders; | |
5233 | our caller should have created a CONSTRUCTOR for us to put the | |
5234 | initializer into. We will either return that constructor or T. */ | |
5235 | gcc_assert (ctx->ctor); | |
5236 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
5237 | (type, TREE_TYPE (ctx->ctor))); | |
fe69277d JM |
5238 | /* We used to check that ctx->ctor was empty, but that isn't the case when |
5239 | the object is zero-initialized before calling the constructor. */ | |
3e605b20 | 5240 | if (ctx->object) |
176e79b5 JM |
5241 | { |
5242 | tree otype = TREE_TYPE (ctx->object); | |
5243 | gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype) | |
5244 | /* Handle flexible array members. */ | |
5245 | || (TREE_CODE (otype) == ARRAY_TYPE | |
5246 | && TYPE_DOMAIN (otype) == NULL_TREE | |
5247 | && TREE_CODE (type) == ARRAY_TYPE | |
5248 | && (same_type_ignoring_top_level_qualifiers_p | |
5249 | (TREE_TYPE (type), TREE_TYPE (otype))))); | |
5250 | } | |
3e605b20 | 5251 | gcc_assert (!ctx->object || !DECL_P (ctx->object) |
e6a29aab | 5252 | || ctx->global->get_value (ctx->object) == ctx->ctor); |
3e605b20 JM |
5253 | } |
5254 | ||
2d76680f PC |
5255 | /* Subroutine of cxx_eval_constant_expression. |
5256 | The expression tree T denotes a C-style array or a C-style | |
5257 | aggregate. Reduce it to a constant expression. */ | |
5258 | ||
5259 | static tree | |
3e605b20 | 5260 | cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t, |
72f76540 | 5261 | value_cat lval, |
2d76680f PC |
5262 | bool *non_constant_p, bool *overflow_p) |
5263 | { | |
5264 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); | |
2d76680f PC |
5265 | bool changed = false; |
5266 | gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t)); | |
8a29084d | 5267 | tree type = TREE_TYPE (t); |
3e605b20 | 5268 | |
8a29084d | 5269 | constexpr_ctx new_ctx; |
d4619dc1 | 5270 | if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type)) |
8a29084d | 5271 | { |
d4619dc1 NS |
5272 | /* We don't really need the ctx->ctor business for a PMF or |
5273 | vector, but it's simpler to use the same code. */ | |
8a29084d JM |
5274 | new_ctx = *ctx; |
5275 | new_ctx.ctor = build_constructor (type, NULL); | |
5276 | new_ctx.object = NULL_TREE; | |
5277 | ctx = &new_ctx; | |
5278 | }; | |
5279 | verify_ctor_sanity (ctx, type); | |
d19b4342 JM |
5280 | vec<constructor_elt, va_gc> **p = nullptr; |
5281 | if (ctx->ctor) | |
5282 | { | |
5283 | p = &CONSTRUCTOR_ELTS (ctx->ctor); | |
5284 | vec_alloc (*p, vec_safe_length (v)); | |
5285 | if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t)) | |
5286 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1; | |
5287 | } | |
49a86fce | 5288 | |
2d63bc39 JM |
5289 | unsigned i; |
5290 | tree index, value; | |
5291 | bool constant_p = true; | |
5292 | bool side_effects_p = false; | |
3e605b20 | 5293 | FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value) |
2d76680f | 5294 | { |
bcb5f3c9 | 5295 | tree orig_value = value; |
db4243bb | 5296 | init_subob_ctx (ctx, new_ctx, index, value); |
a426b91b PP |
5297 | /* Like in cxx_eval_store_expression, omit entries for empty fields. */ |
5298 | bool no_slot = new_ctx.ctor == NULL_TREE; | |
37244b21 | 5299 | int pos_hint = -1; |
db4243bb | 5300 | if (new_ctx.ctor != ctx->ctor && !no_slot) |
37244b21 PP |
5301 | { |
5302 | /* If we built a new CONSTRUCTOR, attach it now so that other | |
5303 | initializers can refer to it. */ | |
077dd9b3 PP |
5304 | constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index); |
5305 | cep->value = new_ctx.ctor; | |
5306 | pos_hint = cep - (*p)->begin(); | |
37244b21 | 5307 | } |
b599bf9d | 5308 | else if (TREE_CODE (type) == UNION_TYPE) |
37244b21 PP |
5309 | /* Otherwise if we're constructing a non-aggregate union member, set |
5310 | the active union member now so that we can later detect and diagnose | |
5311 | if its initializer attempts to activate another member. */ | |
077dd9b3 | 5312 | get_or_insert_ctor_field (ctx->ctor, index); |
3e605b20 | 5313 | tree elt = cxx_eval_constant_expression (&new_ctx, value, |
92a596e8 | 5314 | lval, |
5a804683 | 5315 | non_constant_p, overflow_p); |
2d76680f | 5316 | /* Don't VERIFY_CONSTANT here. */ |
2b3ab879 | 5317 | if (ctx->quiet && *non_constant_p) |
3e605b20 | 5318 | break; |
bcb5f3c9 | 5319 | if (elt != orig_value) |
2d76680f | 5320 | changed = true; |
2d63bc39 JM |
5321 | |
5322 | if (!TREE_CONSTANT (elt)) | |
5323 | constant_p = false; | |
5324 | if (TREE_SIDE_EFFECTS (elt)) | |
5325 | side_effects_p = true; | |
3e605b20 | 5326 | if (index && TREE_CODE (index) == COMPONENT_REF) |
2d76680f PC |
5327 | { |
5328 | /* This is an initialization of a vfield inside a base | |
5329 | subaggregate that we already initialized; push this | |
5330 | initialization into the previous initialization. */ | |
3e605b20 | 5331 | constructor_elt *inner = base_field_constructor_elt (*p, index); |
2d76680f | 5332 | inner->value = elt; |
3e605b20 | 5333 | changed = true; |
2d76680f | 5334 | } |
d19b4342 JM |
5335 | else if (no_slot) |
5336 | /* This is an initializer for an empty field; now that we've | |
5337 | checked that it's constant, we can ignore it. */ | |
5338 | changed = true; | |
3e605b20 JM |
5339 | else if (index |
5340 | && (TREE_CODE (index) == NOP_EXPR | |
5341 | || TREE_CODE (index) == POINTER_PLUS_EXPR)) | |
2d76680f | 5342 | { |
d19b4342 JM |
5343 | /* Old representation of empty bases. FIXME remove. */ |
5344 | gcc_checking_assert (false); | |
3e605b20 JM |
5345 | gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index)))); |
5346 | changed = true; | |
5347 | } | |
2d76680f | 5348 | else |
ac9ec198 | 5349 | { |
b599bf9d PP |
5350 | if (TREE_CODE (type) == UNION_TYPE |
5351 | && (*p)->last().index != index) | |
37244b21 PP |
5352 | /* The initializer erroneously changed the active union member that |
5353 | we're initializing. */ | |
b599bf9d | 5354 | gcc_assert (*non_constant_p); |
37244b21 | 5355 | else |
1bdbef09 | 5356 | { |
37244b21 PP |
5357 | /* The initializer might have mutated the underlying CONSTRUCTOR, |
5358 | so recompute the location of the target constructer_elt. */ | |
5359 | constructor_elt *cep | |
5360 | = get_or_insert_ctor_field (ctx->ctor, index, pos_hint); | |
5361 | cep->value = elt; | |
1bdbef09 | 5362 | } |
37244b21 | 5363 | |
1bdbef09 | 5364 | /* Adding or replacing an element might change the ctor's flags. */ |
ac9ec198 MP |
5365 | TREE_CONSTANT (ctx->ctor) = constant_p; |
5366 | TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p; | |
5367 | } | |
2d76680f | 5368 | } |
2cc41601 | 5369 | if (*non_constant_p) |
3e605b20 | 5370 | return t; |
2cc41601 JJ |
5371 | if (!changed) |
5372 | { | |
5373 | if (VECTOR_TYPE_P (type)) | |
5374 | t = fold (t); | |
5375 | return t; | |
5376 | } | |
3e605b20 | 5377 | t = ctx->ctor; |
d19b4342 JM |
5378 | if (!t) |
5379 | t = build_constructor (type, NULL); | |
3e605b20 JM |
5380 | /* We're done building this CONSTRUCTOR, so now we can interpret an |
5381 | element without an explicit initializer as value-initialized. */ | |
e8c48716 | 5382 | CONSTRUCTOR_NO_CLEARING (t) = false; |
2d63bc39 JM |
5383 | TREE_CONSTANT (t) = constant_p; |
5384 | TREE_SIDE_EFFECTS (t) = side_effects_p; | |
8a29084d | 5385 | if (VECTOR_TYPE_P (type)) |
2d76680f PC |
5386 | t = fold (t); |
5387 | return t; | |
5388 | } | |
5389 | ||
5390 | /* Subroutine of cxx_eval_constant_expression. | |
5391 | The expression tree T is a VEC_INIT_EXPR which denotes the desired | |
5392 | initialization of a non-static data member of array type. Reduce it to a | |
5393 | CONSTRUCTOR. | |
5394 | ||
5395 | Note that apart from value-initialization (when VALUE_INIT is true), | |
5396 | this is only intended to support value-initialization and the | |
5397 | initializations done by defaulted constructors for classes with | |
5398 | non-static data members of array type. In this case, VEC_INIT_EXPR_INIT | |
5399 | will either be NULL_TREE for the default constructor, or a COMPONENT_REF | |
5400 | for the copy/move constructor. */ | |
5401 | ||
5402 | static tree | |
3e605b20 | 5403 | cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init, |
72f76540 | 5404 | bool value_init, value_cat lval, |
2d76680f PC |
5405 | bool *non_constant_p, bool *overflow_p) |
5406 | { | |
5407 | tree elttype = TREE_TYPE (atype); | |
3e605b20 JM |
5408 | verify_ctor_sanity (ctx, atype); |
5409 | vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); | |
2d76680f | 5410 | bool pre_init = false; |
4784470a | 5411 | unsigned HOST_WIDE_INT i; |
dc5ca6c8 | 5412 | tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; |
2d76680f | 5413 | |
a1c9c9ff JM |
5414 | if (init && TREE_CODE (init) == CONSTRUCTOR) |
5415 | return cxx_eval_bare_aggregate (ctx, init, lval, | |
5416 | non_constant_p, overflow_p); | |
5417 | ||
2d76680f PC |
5418 | /* For the default constructor, build up a call to the default |
5419 | constructor of the element type. We only need to handle class types | |
5420 | here, as for a constructor to be constexpr, all members must be | |
5421 | initialized, which for a defaulted default constructor means they must | |
5422 | be of a class type with a constexpr default constructor. */ | |
5423 | if (TREE_CODE (elttype) == ARRAY_TYPE) | |
5424 | /* We only do this at the lowest level. */; | |
5425 | else if (value_init) | |
5426 | { | |
dc5ca6c8 | 5427 | init = build_value_init (elttype, complain); |
2d76680f PC |
5428 | pre_init = true; |
5429 | } | |
5430 | else if (!init) | |
5431 | { | |
cd9cf97b | 5432 | releasing_vec argvec; |
2d76680f PC |
5433 | init = build_special_member_call (NULL_TREE, complete_ctor_identifier, |
5434 | &argvec, elttype, LOOKUP_NORMAL, | |
dc5ca6c8 | 5435 | complain); |
5dab8b11 | 5436 | init = build_aggr_init_expr (elttype, init); |
2d76680f PC |
5437 | pre_init = true; |
5438 | } | |
5439 | ||
d21252de PP |
5440 | bool zeroed_out = false; |
5441 | if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor)) | |
5442 | { | |
5443 | /* We're initializing an array object that had been zero-initialized | |
5444 | earlier. Truncate ctx->ctor, and propagate its zeroed state by | |
5445 | clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element | |
5446 | initializers we append to it. */ | |
5447 | gcc_checking_assert (initializer_zerop (ctx->ctor)); | |
5448 | zeroed_out = true; | |
5449 | vec_safe_truncate (*p, 0); | |
5450 | } | |
5451 | ||
74f8705e MP |
5452 | tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p, |
5453 | overflow_p); | |
5454 | unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts); | |
4784470a | 5455 | for (i = 0; i < max; ++i) |
2d76680f PC |
5456 | { |
5457 | tree idx = build_int_cst (size_type_node, i); | |
5458 | tree eltinit; | |
928af3bf | 5459 | bool reuse = false; |
3e605b20 JM |
5460 | constexpr_ctx new_ctx; |
5461 | init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype); | |
a426b91b PP |
5462 | bool no_slot = new_ctx.ctor == NULL_TREE; |
5463 | if (new_ctx.ctor != ctx->ctor && !no_slot) | |
d21252de PP |
5464 | { |
5465 | if (zeroed_out) | |
5466 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false; | |
5467 | CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor); | |
5468 | } | |
2d76680f PC |
5469 | if (TREE_CODE (elttype) == ARRAY_TYPE) |
5470 | { | |
5471 | /* A multidimensional array; recurse. */ | |
5472 | if (value_init || init == NULL_TREE) | |
928af3bf JJ |
5473 | { |
5474 | eltinit = NULL_TREE; | |
5475 | reuse = i == 0; | |
5476 | } | |
2d76680f | 5477 | else |
dc5ca6c8 | 5478 | eltinit = cp_build_array_ref (input_location, init, idx, complain); |
3e605b20 | 5479 | eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init, |
92a596e8 | 5480 | lval, |
2d76680f PC |
5481 | non_constant_p, overflow_p); |
5482 | } | |
5483 | else if (pre_init) | |
5484 | { | |
5485 | /* Initializing an element using value or default initialization | |
5486 | we just pre-built above. */ | |
3ee378fb JM |
5487 | if (init == void_node) |
5488 | /* Trivial default-init, don't do anything to the CONSTRUCTOR. */ | |
5489 | return ctx->ctor; | |
928af3bf JJ |
5490 | eltinit = cxx_eval_constant_expression (&new_ctx, init, lval, |
5491 | non_constant_p, overflow_p); | |
5492 | reuse = i == 0; | |
2d76680f PC |
5493 | } |
5494 | else | |
5495 | { | |
5496 | /* Copying an element. */ | |
dc5ca6c8 | 5497 | eltinit = cp_build_array_ref (input_location, init, idx, complain); |
72b3e203 | 5498 | if (!lvalue_p (init)) |
2d76680f | 5499 | eltinit = move (eltinit); |
2764335b JM |
5500 | eltinit = (perform_implicit_conversion_flags |
5501 | (elttype, eltinit, complain, | |
5502 | LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING)); | |
c2236b9b JJ |
5503 | eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval, |
5504 | non_constant_p, overflow_p); | |
2d76680f | 5505 | } |
6b7d53a2 | 5506 | if (*non_constant_p) |
3e605b20 | 5507 | break; |
a426b91b PP |
5508 | if (no_slot) |
5509 | { | |
5510 | /* This is an initializer for an empty subobject; now that we've | |
5511 | checked that it's constant, we can ignore it. */ | |
5512 | gcc_checking_assert (i == 0); | |
5513 | break; | |
5514 | } | |
5515 | else if (new_ctx.ctor != ctx->ctor) | |
3e605b20 JM |
5516 | { |
5517 | /* We appended this element above; update the value. */ | |
5518 | gcc_assert ((*p)->last().index == idx); | |
5519 | (*p)->last().value = eltinit; | |
5520 | } | |
5521 | else | |
5522 | CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit); | |
928af3bf | 5523 | /* Reuse the result of cxx_eval_constant_expression call |
c2236b9b JJ |
5524 | from the first iteration to all others if it is a constant |
5525 | initializer that doesn't require relocations. */ | |
928af3bf JJ |
5526 | if (reuse |
5527 | && max > 1 | |
c2236b9b JJ |
5528 | && (eltinit == NULL_TREE |
5529 | || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit)) | |
5530 | == null_pointer_node))) | |
928af3bf JJ |
5531 | { |
5532 | if (new_ctx.ctor != ctx->ctor) | |
5533 | eltinit = new_ctx.ctor; | |
1e027956 RB |
5534 | tree range = build2 (RANGE_EXPR, size_type_node, |
5535 | build_int_cst (size_type_node, 1), | |
5536 | build_int_cst (size_type_node, max - 1)); | |
5537 | CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit)); | |
928af3bf JJ |
5538 | break; |
5539 | } | |
1e027956 RB |
5540 | else if (i == 0) |
5541 | vec_safe_reserve (*p, max); | |
2d76680f PC |
5542 | } |
5543 | ||
5544 | if (!*non_constant_p) | |
5545 | { | |
3e605b20 | 5546 | init = ctx->ctor; |
e8c48716 | 5547 | CONSTRUCTOR_NO_CLEARING (init) = false; |
2d76680f | 5548 | } |
2d76680f PC |
5549 | return init; |
5550 | } | |
5551 | ||
5552 | static tree | |
3e605b20 | 5553 | cxx_eval_vec_init (const constexpr_ctx *ctx, tree t, |
72f76540 | 5554 | value_cat lval, |
2d76680f PC |
5555 | bool *non_constant_p, bool *overflow_p) |
5556 | { | |
5557 | tree atype = TREE_TYPE (t); | |
5558 | tree init = VEC_INIT_EXPR_INIT (t); | |
e948436e JM |
5559 | bool value_init = VEC_INIT_EXPR_VALUE_INIT (t); |
5560 | if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init)) | |
5561 | ; | |
4822108e JM |
5562 | else if (CONSTRUCTOR_NELTS (init) == 0 |
5563 | && !CP_AGGREGATE_TYPE_P (strip_array_types (atype))) | |
e948436e JM |
5564 | { |
5565 | /* Handle {} as value-init. */ | |
5566 | init = NULL_TREE; | |
5567 | value_init = true; | |
5568 | } | |
5569 | else | |
5570 | { | |
5571 | /* This is a more complicated case, like needing to loop over trailing | |
5572 | elements; call build_vec_init and evaluate the result. */ | |
5573 | tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; | |
5574 | constexpr_ctx new_ctx = *ctx; | |
5575 | if (!ctx->object) | |
5576 | { | |
5577 | /* We want to have an initialization target for an VEC_INIT_EXPR. | |
5578 | If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */ | |
5579 | new_ctx.object = VEC_INIT_EXPR_SLOT (t); | |
5580 | tree ctor = new_ctx.ctor = build_constructor (atype, NULL); | |
5581 | CONSTRUCTOR_NO_CLEARING (ctor) = true; | |
e6a29aab | 5582 | ctx->global->put_value (new_ctx.object, ctor); |
e948436e JM |
5583 | ctx = &new_ctx; |
5584 | } | |
5585 | init = expand_vec_init_expr (ctx->object, t, complain); | |
5586 | return cxx_eval_constant_expression (ctx, init, lval, non_constant_p, | |
5587 | overflow_p); | |
5588 | } | |
5589 | tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init, | |
92a596e8 | 5590 | lval, non_constant_p, overflow_p); |
2d76680f PC |
5591 | if (*non_constant_p) |
5592 | return t; | |
5593 | else | |
5594 | return r; | |
5595 | } | |
5596 | ||
e4511ca2 JM |
5597 | /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case |
5598 | where the desired type is an array of unknown bounds because the variable | |
5599 | has had its bounds deduced since the wrapping expression was created. */ | |
5600 | ||
5601 | static bool | |
5602 | same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2) | |
5603 | { | |
5604 | while (TREE_CODE (type1) == ARRAY_TYPE | |
5605 | && TREE_CODE (type2) == ARRAY_TYPE | |
5606 | && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2))) | |
5607 | { | |
5608 | type1 = TREE_TYPE (type1); | |
5609 | type2 = TREE_TYPE (type2); | |
5610 | } | |
5611 | return same_type_ignoring_top_level_qualifiers_p (type1, type2); | |
5612 | } | |
5613 | ||
43e84ce7 JJ |
5614 | /* Try to determine the currently active union member for an expression |
5615 | with UNION_TYPE. If it can be determined, return the FIELD_DECL, | |
5616 | otherwise return NULL_TREE. */ | |
5617 | ||
5618 | static tree | |
5619 | cxx_union_active_member (const constexpr_ctx *ctx, tree t) | |
5620 | { | |
5621 | constexpr_ctx new_ctx = *ctx; | |
5622 | new_ctx.quiet = true; | |
5623 | bool non_constant_p = false, overflow_p = false; | |
72f76540 | 5624 | tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue, |
43e84ce7 JJ |
5625 | &non_constant_p, |
5626 | &overflow_p); | |
5627 | if (TREE_CODE (ctor) == CONSTRUCTOR | |
5628 | && CONSTRUCTOR_NELTS (ctor) == 1 | |
5629 | && CONSTRUCTOR_ELT (ctor, 0)->index | |
5630 | && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL) | |
5631 | return CONSTRUCTOR_ELT (ctor, 0)->index; | |
5632 | return NULL_TREE; | |
5633 | } | |
5634 | ||
0fe2ae29 JJ |
5635 | /* Helper function for cxx_fold_indirect_ref_1, called recursively. */ |
5636 | ||
5637 | static tree | |
43e84ce7 JJ |
5638 | cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type, |
5639 | tree op, unsigned HOST_WIDE_INT off, bool *empty_base) | |
0fe2ae29 JJ |
5640 | { |
5641 | tree optype = TREE_TYPE (op); | |
5642 | unsigned HOST_WIDE_INT const_nunits; | |
bc99c54d JM |
5643 | if (off == 0 && similar_type_p (optype, type)) |
5644 | return op; | |
5645 | else if (TREE_CODE (optype) == COMPLEX_TYPE | |
5646 | && similar_type_p (type, TREE_TYPE (optype))) | |
0fe2ae29 | 5647 | { |
0fe2ae29 | 5648 | /* *(foo *)&complexfoo => __real__ complexfoo */ |
bc99c54d | 5649 | if (off == 0) |
0fe2ae29 | 5650 | return build1_loc (loc, REALPART_EXPR, type, op); |
bc99c54d JM |
5651 | /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */ |
5652 | else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off) | |
5653 | return build1_loc (loc, IMAGPART_EXPR, type, op); | |
0fe2ae29 JJ |
5654 | } |
5655 | /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */ | |
5656 | else if (VECTOR_TYPE_P (optype) | |
5657 | && similar_type_p (type, TREE_TYPE (optype)) | |
5658 | && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits)) | |
5659 | { | |
5660 | unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type)); | |
5661 | unsigned HOST_WIDE_INT max_offset = part_width * const_nunits; | |
5662 | if (off < max_offset && off % part_width == 0) | |
5663 | { | |
5664 | tree index = bitsize_int (off * BITS_PER_UNIT); | |
5665 | return build3_loc (loc, BIT_FIELD_REF, type, op, | |
5666 | TYPE_SIZE (type), index); | |
5667 | } | |
5668 | } | |
5669 | /* ((foo *)&fooarray)[x] => fooarray[x] */ | |
5670 | else if (TREE_CODE (optype) == ARRAY_TYPE | |
5671 | && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype))) | |
5672 | && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype)))) | |
5673 | { | |
5674 | tree type_domain = TYPE_DOMAIN (optype); | |
5675 | tree min_val = size_zero_node; | |
5676 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
5677 | min_val = TYPE_MIN_VALUE (type_domain); | |
5678 | unsigned HOST_WIDE_INT el_sz | |
5679 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype))); | |
5680 | unsigned HOST_WIDE_INT idx = off / el_sz; | |
5681 | unsigned HOST_WIDE_INT rem = off % el_sz; | |
5682 | if (tree_fits_uhwi_p (min_val)) | |
5683 | { | |
5684 | tree index = size_int (idx + tree_to_uhwi (min_val)); | |
5685 | op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index, | |
5686 | NULL_TREE, NULL_TREE); | |
43e84ce7 | 5687 | return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem, |
0fe2ae29 JJ |
5688 | empty_base); |
5689 | } | |
5690 | } | |
5691 | /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */ | |
43e84ce7 JJ |
5692 | else if (TREE_CODE (optype) == RECORD_TYPE |
5693 | || TREE_CODE (optype) == UNION_TYPE) | |
0fe2ae29 | 5694 | { |
43e84ce7 JJ |
5695 | if (TREE_CODE (optype) == UNION_TYPE) |
5696 | /* For unions prefer the currently active member. */ | |
5697 | if (tree field = cxx_union_active_member (ctx, op)) | |
5698 | { | |
5699 | unsigned HOST_WIDE_INT el_sz | |
5700 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field))); | |
5701 | if (off < el_sz) | |
5702 | { | |
5703 | tree cop = build3 (COMPONENT_REF, TREE_TYPE (field), | |
5704 | op, field, NULL_TREE); | |
5705 | if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop, | |
5706 | off, empty_base)) | |
5707 | return ret; | |
5708 | } | |
5709 | } | |
4b8d0d4d | 5710 | |
90bc2d09 NS |
5711 | /* Handle conversion to "as base" type. */ |
5712 | if (CLASSTYPE_AS_BASE (optype) == type) | |
5713 | return op; | |
5714 | ||
4b8d0d4d JM |
5715 | /* Handle conversion to an empty base class, which is represented with a |
5716 | NOP_EXPR. Do this before spelunking into the non-empty subobjects, | |
5717 | which is likely to be a waste of time (109678). */ | |
5718 | if (is_empty_class (type) | |
5719 | && CLASS_TYPE_P (optype) | |
a43f3616 | 5720 | && lookup_base (optype, type, ba_any, NULL, tf_none, off)) |
4b8d0d4d JM |
5721 | { |
5722 | if (empty_base) | |
5723 | *empty_base = true; | |
5724 | return op; | |
5725 | } | |
5726 | ||
0fe2ae29 JJ |
5727 | for (tree field = TYPE_FIELDS (optype); |
5728 | field; field = DECL_CHAIN (field)) | |
5729 | if (TREE_CODE (field) == FIELD_DECL | |
5730 | && TREE_TYPE (field) != error_mark_node | |
5731 | && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field)))) | |
5732 | { | |
5733 | tree pos = byte_position (field); | |
5734 | if (!tree_fits_uhwi_p (pos)) | |
5735 | continue; | |
5736 | unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos); | |
43e84ce7 | 5737 | unsigned HOST_WIDE_INT el_sz |
0fe2ae29 JJ |
5738 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field))); |
5739 | if (upos <= off && off < upos + el_sz) | |
5740 | { | |
5741 | tree cop = build3 (COMPONENT_REF, TREE_TYPE (field), | |
5742 | op, field, NULL_TREE); | |
43e84ce7 | 5743 | if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop, |
0fe2ae29 JJ |
5744 | off - upos, |
5745 | empty_base)) | |
5746 | return ret; | |
5747 | } | |
5748 | } | |
5749 | } | |
5750 | ||
5751 | return NULL_TREE; | |
5752 | } | |
5753 | ||
2d76680f PC |
5754 | /* A less strict version of fold_indirect_ref_1, which requires cv-quals to |
5755 | match. We want to be less strict for simple *& folding; if we have a | |
5756 | non-const temporary that we access through a const pointer, that should | |
5757 | work. We handle this here rather than change fold_indirect_ref_1 | |
5758 | because we're dealing with things like ADDR_EXPR of INTEGER_CST which | |
5759 | don't really make sense outside of constant expression evaluation. Also | |
5760 | we want to allow folding to COMPONENT_REF, which could cause trouble | |
0fe2ae29 | 5761 | with TBAA in fold_indirect_ref_1. */ |
2d76680f PC |
5762 | |
5763 | static tree | |
43e84ce7 | 5764 | cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type, |
1189c038 | 5765 | tree op0, bool *empty_base /* = NULL*/) |
2d76680f | 5766 | { |
ebe4bf41 MP |
5767 | tree sub = op0; |
5768 | tree subtype; | |
2d76680f | 5769 | |
80de0002 JM |
5770 | /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */ |
5771 | while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR | |
5772 | || TREE_CODE (sub) == VIEW_CONVERT_EXPR) | |
5773 | { | |
5774 | if (TREE_CODE (sub) == NOP_EXPR | |
5775 | && REINTERPRET_CAST_P (sub)) | |
5776 | return NULL_TREE; | |
5777 | sub = TREE_OPERAND (sub, 0); | |
5778 | } | |
5779 | ||
2d76680f | 5780 | subtype = TREE_TYPE (sub); |
71a93b08 | 5781 | if (!INDIRECT_TYPE_P (subtype)) |
2d76680f PC |
5782 | return NULL_TREE; |
5783 | ||
ab36b554 PP |
5784 | /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing |
5785 | the innermost component into the offset until it would make the | |
5786 | offset positive, so that cxx_fold_indirect_ref_1 can identify | |
5787 | more folding opportunities. */ | |
5788 | auto canonicalize_obj_off = [] (tree& obj, tree& off) { | |
5789 | while (TREE_CODE (obj) == COMPONENT_REF | |
5790 | && (tree_int_cst_sign_bit (off) || integer_zerop (off))) | |
5791 | { | |
5792 | tree field = TREE_OPERAND (obj, 1); | |
5793 | tree pos = byte_position (field); | |
5794 | if (integer_zerop (off) && integer_nonzerop (pos)) | |
5795 | /* If the offset is already 0, keep going as long as the | |
5796 | component is at position 0. */ | |
5797 | break; | |
5798 | off = int_const_binop (PLUS_EXPR, off, pos); | |
5799 | obj = TREE_OPERAND (obj, 0); | |
5800 | } | |
5801 | }; | |
5802 | ||
2d76680f PC |
5803 | if (TREE_CODE (sub) == ADDR_EXPR) |
5804 | { | |
5805 | tree op = TREE_OPERAND (sub, 0); | |
5806 | tree optype = TREE_TYPE (op); | |
5807 | ||
5808 | /* *&CONST_DECL -> to the value of the const decl. */ | |
5809 | if (TREE_CODE (op) == CONST_DECL) | |
5810 | return DECL_INITIAL (op); | |
5811 | /* *&p => p; make sure to handle *&"str"[cst] here. */ | |
1a120ec1 | 5812 | if (similar_type_p (optype, type)) |
2d76680f PC |
5813 | { |
5814 | tree fop = fold_read_from_constant_string (op); | |
5815 | if (fop) | |
5816 | return fop; | |
5817 | else | |
5818 | return op; | |
5819 | } | |
0fe2ae29 | 5820 | else |
ab36b554 PP |
5821 | { |
5822 | tree off = integer_zero_node; | |
5823 | canonicalize_obj_off (op, off); | |
5824 | gcc_assert (integer_zerop (off)); | |
5825 | return cxx_fold_indirect_ref_1 (ctx, loc, type, op, 0, empty_base); | |
5826 | } | |
2d76680f PC |
5827 | } |
5828 | else if (TREE_CODE (sub) == POINTER_PLUS_EXPR | |
0fe2ae29 | 5829 | && tree_fits_uhwi_p (TREE_OPERAND (sub, 1))) |
2d76680f PC |
5830 | { |
5831 | tree op00 = TREE_OPERAND (sub, 0); | |
0120cd93 | 5832 | tree off = TREE_OPERAND (sub, 1); |
2d76680f PC |
5833 | |
5834 | STRIP_NOPS (op00); | |
5835 | if (TREE_CODE (op00) == ADDR_EXPR) | |
0120cd93 PP |
5836 | { |
5837 | tree obj = TREE_OPERAND (op00, 0); | |
ab36b554 | 5838 | canonicalize_obj_off (obj, off); |
0120cd93 PP |
5839 | return cxx_fold_indirect_ref_1 (ctx, loc, type, obj, |
5840 | tree_to_uhwi (off), empty_base); | |
5841 | } | |
2d76680f PC |
5842 | } |
5843 | /* *(foo *)fooarrptr => (*fooarrptr)[0] */ | |
5844 | else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE | |
1a120ec1 | 5845 | && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype)))) |
2d76680f PC |
5846 | { |
5847 | tree type_domain; | |
5848 | tree min_val = size_zero_node; | |
ebe4bf41 | 5849 | tree newsub |
43e84ce7 | 5850 | = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL); |
2d76680f PC |
5851 | if (newsub) |
5852 | sub = newsub; | |
5853 | else | |
5854 | sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub); | |
5855 | type_domain = TYPE_DOMAIN (TREE_TYPE (sub)); | |
5856 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
5857 | min_val = TYPE_MIN_VALUE (type_domain); | |
5858 | return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE, | |
5859 | NULL_TREE); | |
5860 | } | |
5861 | ||
5862 | return NULL_TREE; | |
5863 | } | |
5864 | ||
5865 | static tree | |
3e605b20 | 5866 | cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t, |
72f76540 | 5867 | value_cat lval, |
2d76680f PC |
5868 | bool *non_constant_p, bool *overflow_p) |
5869 | { | |
5870 | tree orig_op0 = TREE_OPERAND (t, 0); | |
2d76680f | 5871 | bool empty_base = false; |
2d76680f | 5872 | |
cda0a029 JM |
5873 | /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second |
5874 | operand is an integer-zero. Otherwise reject the MEM_REF for now. */ | |
5875 | ||
5876 | if (TREE_CODE (t) == MEM_REF | |
5877 | && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1)))) | |
5878 | { | |
5879 | gcc_assert (ctx->quiet); | |
5880 | *non_constant_p = true; | |
5881 | return t; | |
5882 | } | |
5883 | ||
255a48d6 | 5884 | /* First try to simplify it directly. */ |
43e84ce7 JJ |
5885 | tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), |
5886 | orig_op0, &empty_base); | |
255a48d6 | 5887 | if (!r) |
2d76680f | 5888 | { |
255a48d6 JM |
5889 | /* If that didn't work, evaluate the operand first. */ |
5890 | tree op0 = cxx_eval_constant_expression (ctx, orig_op0, | |
72f76540 | 5891 | vc_prvalue, non_constant_p, |
255a48d6 JM |
5892 | overflow_p); |
5893 | /* Don't VERIFY_CONSTANT here. */ | |
5894 | if (*non_constant_p) | |
5895 | return t; | |
5896 | ||
8bada5cd MS |
5897 | if (!lval && integer_zerop (op0)) |
5898 | { | |
5899 | if (!ctx->quiet) | |
5900 | error ("dereferencing a null pointer"); | |
5901 | *non_constant_p = true; | |
5902 | return t; | |
5903 | } | |
5904 | ||
43e84ce7 | 5905 | r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0, |
255a48d6 JM |
5906 | &empty_base); |
5907 | if (r == NULL_TREE) | |
2d76680f PC |
5908 | { |
5909 | /* We couldn't fold to a constant value. Make sure it's not | |
5910 | something we should have been able to fold. */ | |
255a48d6 JM |
5911 | tree sub = op0; |
5912 | STRIP_NOPS (sub); | |
5913 | if (TREE_CODE (sub) == ADDR_EXPR) | |
5914 | { | |
1a120ec1 | 5915 | gcc_assert (!similar_type_p |
255a48d6 JM |
5916 | (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t))); |
5917 | /* DR 1188 says we don't have to deal with this. */ | |
5918 | if (!ctx->quiet) | |
1a120ec1 JM |
5919 | error_at (cp_expr_loc_or_input_loc (t), |
5920 | "accessing value of %qE through a %qT glvalue in a " | |
5921 | "constant expression", build_fold_indirect_ref (sub), | |
5922 | TREE_TYPE (t)); | |
255a48d6 JM |
5923 | *non_constant_p = true; |
5924 | return t; | |
5925 | } | |
5926 | ||
72f76540 | 5927 | if (lval == vc_glvalue && op0 != orig_op0) |
255a48d6 JM |
5928 | return build1 (INDIRECT_REF, TREE_TYPE (t), op0); |
5929 | if (!lval) | |
5930 | VERIFY_CONSTANT (t); | |
2d76680f PC |
5931 | return t; |
5932 | } | |
5933 | } | |
5934 | ||
255a48d6 JM |
5935 | r = cxx_eval_constant_expression (ctx, r, |
5936 | lval, non_constant_p, overflow_p); | |
5937 | if (*non_constant_p) | |
5938 | return t; | |
5939 | ||
125db6a1 | 5940 | /* If we're pulling out the value of an empty base, just return an empty |
2d76680f | 5941 | CONSTRUCTOR. */ |
92a596e8 | 5942 | if (empty_base && !lval) |
2d76680f | 5943 | { |
2d76680f PC |
5944 | r = build_constructor (TREE_TYPE (t), NULL); |
5945 | TREE_CONSTANT (r) = true; | |
5946 | } | |
5947 | ||
2d76680f PC |
5948 | return r; |
5949 | } | |
5950 | ||
9fdbd7d6 NS |
5951 | /* Complain about R, a DECL that is accessed outside its lifetime. */ |
5952 | ||
5953 | static void | |
5954 | outside_lifetime_error (location_t loc, tree r) | |
5955 | { | |
4f870007 | 5956 | auto_diagnostic_group d; |
9fdbd7d6 NS |
5957 | if (DECL_NAME (r) == heap_deleted_identifier) |
5958 | { | |
5959 | /* Provide a more accurate message for deleted variables. */ | |
5960 | error_at (loc, "use of allocated storage after deallocation " | |
5961 | "in a constant expression"); | |
5962 | inform (DECL_SOURCE_LOCATION (r), "allocated here"); | |
5963 | } | |
5964 | else | |
5965 | { | |
90bc2d09 | 5966 | error_at (loc, "accessing %qE outside its lifetime", r); |
9fdbd7d6 NS |
5967 | inform (DECL_SOURCE_LOCATION (r), "declared here"); |
5968 | } | |
5969 | } | |
5970 | ||
2d76680f | 5971 | /* Complain about R, a VAR_DECL, not being usable in a constant expression. |
c85f8dbb | 5972 | FUNDEF_P is true if we're checking a constexpr function body. |
2d76680f PC |
5973 | Shared between potential_constant_expression and |
5974 | cxx_eval_constant_expression. */ | |
5975 | ||
5976 | static void | |
c85f8dbb | 5977 | non_const_var_error (location_t loc, tree r, bool fundef_p) |
2d76680f | 5978 | { |
8e007055 | 5979 | auto_diagnostic_group d; |
2d76680f | 5980 | tree type = TREE_TYPE (r); |
8e007055 | 5981 | if (DECL_NAME (r) == heap_uninit_identifier |
815baade JJ |
5982 | || DECL_NAME (r) == heap_identifier |
5983 | || DECL_NAME (r) == heap_vec_uninit_identifier | |
5984 | || DECL_NAME (r) == heap_vec_identifier) | |
8e007055 | 5985 | { |
c85f8dbb MP |
5986 | if (constexpr_error (loc, fundef_p, "the content of uninitialized " |
5987 | "storage is not usable in a constant expression")) | |
5988 | inform (DECL_SOURCE_LOCATION (r), "allocated here"); | |
8e007055 JJ |
5989 | return; |
5990 | } | |
5991 | if (DECL_NAME (r) == heap_deleted_identifier) | |
5992 | { | |
c85f8dbb MP |
5993 | if (constexpr_error (loc, fundef_p, "use of allocated storage after " |
5994 | "deallocation in a constant expression")) | |
5995 | inform (DECL_SOURCE_LOCATION (r), "allocated here"); | |
8e007055 JJ |
5996 | return; |
5997 | } | |
c85f8dbb MP |
5998 | if (!constexpr_error (loc, fundef_p, "the value of %qD is not usable in " |
5999 | "a constant expression", r)) | |
6000 | return; | |
2d76680f PC |
6001 | /* Avoid error cascade. */ |
6002 | if (DECL_INITIAL (r) == error_mark_node) | |
6003 | return; | |
6004 | if (DECL_DECLARED_CONSTEXPR_P (r)) | |
6005 | inform (DECL_SOURCE_LOCATION (r), | |
6006 | "%qD used in its own initializer", r); | |
6007 | else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) | |
6008 | { | |
6009 | if (!CP_TYPE_CONST_P (type)) | |
6010 | inform (DECL_SOURCE_LOCATION (r), | |
6011 | "%q#D is not const", r); | |
6012 | else if (CP_TYPE_VOLATILE_P (type)) | |
6013 | inform (DECL_SOURCE_LOCATION (r), | |
6014 | "%q#D is volatile", r); | |
6015 | else if (!DECL_INITIAL (r) | |
a3e2b438 PP |
6016 | || !TREE_CONSTANT (DECL_INITIAL (r)) |
6017 | || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)) | |
2d76680f PC |
6018 | inform (DECL_SOURCE_LOCATION (r), |
6019 | "%qD was not initialized with a constant " | |
6020 | "expression", r); | |
6021 | else | |
6022 | gcc_unreachable (); | |
6023 | } | |
9f613f06 | 6024 | else if (TYPE_REF_P (type)) |
fd338b13 JM |
6025 | inform (DECL_SOURCE_LOCATION (r), |
6026 | "%qD was not initialized with a constant " | |
6027 | "expression", r); | |
2d76680f PC |
6028 | else |
6029 | { | |
6030 | if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r)) | |
6031 | inform (DECL_SOURCE_LOCATION (r), | |
6032 | "%qD was not declared %<constexpr%>", r); | |
6033 | else | |
6034 | inform (DECL_SOURCE_LOCATION (r), | |
6035 | "%qD does not have integral or enumeration type", | |
6036 | r); | |
6037 | } | |
6038 | } | |
6039 | ||
6040 | /* Subroutine of cxx_eval_constant_expression. | |
6041 | Like cxx_eval_unary_expression, except for trinary expressions. */ | |
6042 | ||
6043 | static tree | |
3e605b20 | 6044 | cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t, |
72f76540 | 6045 | value_cat lval, |
2d76680f PC |
6046 | bool *non_constant_p, bool *overflow_p) |
6047 | { | |
6048 | int i; | |
6049 | tree args[3]; | |
6050 | tree val; | |
6051 | ||
6052 | for (i = 0; i < 3; i++) | |
6053 | { | |
3e605b20 | 6054 | args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i), |
92a596e8 | 6055 | lval, |
5a804683 | 6056 | non_constant_p, overflow_p); |
2d76680f PC |
6057 | VERIFY_CONSTANT (args[i]); |
6058 | } | |
6059 | ||
6060 | val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t), | |
6061 | args[0], args[1], args[2]); | |
6062 | if (val == NULL_TREE) | |
6063 | return t; | |
6064 | VERIFY_CONSTANT (val); | |
6065 | return val; | |
6066 | } | |
6067 | ||
98e5a19a JM |
6068 | /* True if T was declared in a function declared to be constexpr, and |
6069 | therefore potentially constant in C++14. */ | |
6070 | ||
2d76680f PC |
6071 | bool |
6072 | var_in_constexpr_fn (tree t) | |
6073 | { | |
6074 | tree ctx = DECL_CONTEXT (t); | |
98e5a19a | 6075 | return (ctx && TREE_CODE (ctx) == FUNCTION_DECL |
2d76680f PC |
6076 | && DECL_DECLARED_CONSTEXPR_P (ctx)); |
6077 | } | |
6078 | ||
26dbe85a MP |
6079 | /* True if a function might be constexpr: either a function that was |
6080 | declared constexpr, or a C++17 lambda op(). */ | |
6081 | ||
6082 | bool | |
6083 | maybe_constexpr_fn (tree t) | |
6084 | { | |
6085 | return (DECL_DECLARED_CONSTEXPR_P (t) | |
87c2080b JM |
6086 | || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t)) |
6087 | || (flag_implicit_constexpr | |
6088 | && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t)))); | |
26dbe85a MP |
6089 | } |
6090 | ||
98e5a19a JM |
6091 | /* True if T was declared in a function that might be constexpr: either a |
6092 | function that was declared constexpr, or a C++17 lambda op(). */ | |
6093 | ||
6094 | bool | |
6095 | var_in_maybe_constexpr_fn (tree t) | |
6096 | { | |
87c2080b JM |
6097 | return (DECL_FUNCTION_SCOPE_P (t) |
6098 | && maybe_constexpr_fn (DECL_CONTEXT (t))); | |
98e5a19a JM |
6099 | } |
6100 | ||
68d01920 JM |
6101 | /* We're assigning INIT to TARGET. In do_build_copy_constructor and |
6102 | build_over_call we implement trivial copy of a class with tail padding using | |
6103 | assignment of character arrays, which is valid in normal code, but not in | |
6104 | constexpr evaluation. We don't need to worry about clobbering tail padding | |
6105 | in constexpr evaluation, so strip the type punning. */ | |
6106 | ||
6107 | static void | |
6108 | maybe_simplify_trivial_copy (tree &target, tree &init) | |
6109 | { | |
6110 | if (TREE_CODE (target) == MEM_REF | |
6111 | && TREE_CODE (init) == MEM_REF | |
6112 | && TREE_TYPE (target) == TREE_TYPE (init) | |
6113 | && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE | |
6114 | && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node) | |
6115 | { | |
6116 | target = build_fold_indirect_ref (TREE_OPERAND (target, 0)); | |
6117 | init = build_fold_indirect_ref (TREE_OPERAND (init, 0)); | |
6118 | } | |
6119 | } | |
6120 | ||
7eb5be6a MP |
6121 | /* Returns true if REF, which is a COMPONENT_REF, has any fields |
6122 | of constant type. This does not check for 'mutable', so the | |
6123 | caller is expected to be mindful of that. */ | |
6124 | ||
6125 | static bool | |
6126 | cref_has_const_field (tree ref) | |
6127 | { | |
6128 | while (TREE_CODE (ref) == COMPONENT_REF) | |
6129 | { | |
6130 | if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1)))) | |
6131 | return true; | |
6132 | ref = TREE_OPERAND (ref, 0); | |
6133 | } | |
6134 | return false; | |
6135 | } | |
6136 | ||
04e1749c MP |
6137 | /* Return true if we are modifying something that is const during constant |
6138 | expression evaluation. CODE is the code of the statement, OBJ is the | |
6139 | object in question, MUTABLE_P is true if one of the subobjects were | |
6140 | declared mutable. */ | |
6141 | ||
6142 | static bool | |
6143 | modifying_const_object_p (tree_code code, tree obj, bool mutable_p) | |
6144 | { | |
6145 | /* If this is initialization, there's no problem. */ | |
6146 | if (code != MODIFY_EXPR) | |
6147 | return false; | |
6148 | ||
6149 | /* [basic.type.qualifier] "A const object is an object of type | |
6150 | const T or a non-mutable subobject of a const object." */ | |
6151 | if (mutable_p) | |
6152 | return false; | |
6153 | ||
7eb5be6a MP |
6154 | if (TREE_READONLY (obj)) |
6155 | return true; | |
6156 | ||
6157 | if (CP_TYPE_CONST_P (TREE_TYPE (obj))) | |
6158 | { | |
6159 | /* Although a COMPONENT_REF may have a const type, we should | |
6160 | only consider it modifying a const object when any of the | |
6161 | field components is const. This can happen when using | |
6162 | constructs such as const_cast<const T &>(m), making something | |
6163 | const even though it wasn't declared const. */ | |
6164 | if (TREE_CODE (obj) == COMPONENT_REF) | |
6165 | return cref_has_const_field (obj); | |
6166 | else | |
6167 | return true; | |
6168 | } | |
6169 | ||
6170 | return false; | |
04e1749c MP |
6171 | } |
6172 | ||
60813a46 JM |
6173 | /* Evaluate an INIT_EXPR or MODIFY_EXPR. */ |
6174 | ||
6175 | static tree | |
6176 | cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, | |
72f76540 | 6177 | value_cat lval, |
60813a46 JM |
6178 | bool *non_constant_p, bool *overflow_p) |
6179 | { | |
6180 | constexpr_ctx new_ctx = *ctx; | |
6181 | ||
58cc255c | 6182 | tree init = TREE_OPERAND (t, 1); |
90bc2d09 NS |
6183 | |
6184 | if (TREE_CLOBBER_P (init) | |
6185 | && CLOBBER_KIND (init) < CLOBBER_OBJECT_END) | |
6186 | /* Only handle clobbers ending the lifetime of objects. */ | |
58cc255c JM |
6187 | return void_node; |
6188 | ||
60813a46 JM |
6189 | /* First we figure out where we're storing to. */ |
6190 | tree target = TREE_OPERAND (t, 0); | |
68d01920 JM |
6191 | |
6192 | maybe_simplify_trivial_copy (target, init); | |
6193 | ||
3f8e2835 | 6194 | tree type = TREE_TYPE (target); |
e8b3c1bc | 6195 | bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR; |
90bc2d09 | 6196 | if (preeval && !TREE_CLOBBER_P (init)) |
e8b3c1bc JM |
6197 | { |
6198 | /* Evaluate the value to be stored without knowing what object it will be | |
6199 | stored in, so that any side-effects happen first. */ | |
6200 | if (!SCALAR_TYPE_P (type)) | |
6201 | new_ctx.ctor = new_ctx.object = NULL_TREE; | |
72f76540 | 6202 | init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue, |
e8b3c1bc JM |
6203 | non_constant_p, overflow_p); |
6204 | if (*non_constant_p) | |
6205 | return t; | |
6206 | } | |
60813a46 | 6207 | |
d0aa42d2 | 6208 | bool evaluated = false; |
72f76540 | 6209 | if (lval == vc_glvalue) |
3f8e2835 | 6210 | { |
d0aa42d2 JM |
6211 | /* If we want to return a reference to the target, we need to evaluate it |
6212 | as a whole; otherwise, only evaluate the innermost piece to avoid | |
6213 | building up unnecessary *_REFs. */ | |
72f76540 | 6214 | target = cxx_eval_constant_expression (ctx, target, lval, |
d0aa42d2 JM |
6215 | non_constant_p, overflow_p); |
6216 | evaluated = true; | |
6217 | if (*non_constant_p) | |
6218 | return t; | |
3f8e2835 JM |
6219 | } |
6220 | ||
043666e0 | 6221 | /* Find the underlying variable. */ |
cd9cf97b | 6222 | releasing_vec refs; |
60813a46 | 6223 | tree object = NULL_TREE; |
04e1749c MP |
6224 | /* If we're modifying a const object, save it. */ |
6225 | tree const_object_being_modified = NULL_TREE; | |
6226 | bool mutable_p = false; | |
60813a46 JM |
6227 | for (tree probe = target; object == NULL_TREE; ) |
6228 | { | |
6229 | switch (TREE_CODE (probe)) | |
6230 | { | |
6231 | case BIT_FIELD_REF: | |
6232 | case COMPONENT_REF: | |
6233 | case ARRAY_REF: | |
043666e0 JM |
6234 | { |
6235 | tree ob = TREE_OPERAND (probe, 0); | |
6236 | tree elt = TREE_OPERAND (probe, 1); | |
7d349dd8 | 6237 | if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt)) |
04e1749c | 6238 | mutable_p = true; |
043666e0 JM |
6239 | if (TREE_CODE (probe) == ARRAY_REF) |
6240 | { | |
6241 | elt = eval_and_check_array_index (ctx, probe, false, | |
6242 | non_constant_p, overflow_p); | |
6243 | if (*non_constant_p) | |
6244 | return t; | |
6245 | } | |
0f184800 MP |
6246 | /* We don't check modifying_const_object_p for ARRAY_REFs. Given |
6247 | "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though | |
6248 | the array isn't const. Instead, check "a" in the next iteration; | |
6249 | that will detect modifying "const int a[10]". */ | |
6250 | else if (evaluated | |
6251 | && modifying_const_object_p (TREE_CODE (t), probe, | |
6252 | mutable_p) | |
6253 | && const_object_being_modified == NULL_TREE) | |
6254 | const_object_being_modified = probe; | |
1d260ab0 NS |
6255 | |
6256 | /* Track named member accesses for unions to validate modifications | |
6257 | that change active member. */ | |
6258 | if (!evaluated && TREE_CODE (probe) == COMPONENT_REF) | |
6259 | vec_safe_push (refs, probe); | |
6260 | else | |
6261 | vec_safe_push (refs, NULL_TREE); | |
6262 | ||
043666e0 JM |
6263 | vec_safe_push (refs, elt); |
6264 | vec_safe_push (refs, TREE_TYPE (probe)); | |
6265 | probe = ob; | |
6266 | } | |
60813a46 JM |
6267 | break; |
6268 | ||
19077677 JJ |
6269 | case REALPART_EXPR: |
6270 | gcc_assert (probe == target); | |
1d260ab0 | 6271 | vec_safe_push (refs, NULL_TREE); |
19077677 JJ |
6272 | vec_safe_push (refs, probe); |
6273 | vec_safe_push (refs, TREE_TYPE (probe)); | |
6274 | probe = TREE_OPERAND (probe, 0); | |
6275 | break; | |
6276 | ||
6277 | case IMAGPART_EXPR: | |
6278 | gcc_assert (probe == target); | |
1d260ab0 | 6279 | vec_safe_push (refs, NULL_TREE); |
19077677 JJ |
6280 | vec_safe_push (refs, probe); |
6281 | vec_safe_push (refs, TREE_TYPE (probe)); | |
6282 | probe = TREE_OPERAND (probe, 0); | |
6283 | break; | |
6284 | ||
60813a46 | 6285 | default: |
d0aa42d2 JM |
6286 | if (evaluated) |
6287 | object = probe; | |
6288 | else | |
6289 | { | |
72f76540 | 6290 | probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue, |
d0aa42d2 JM |
6291 | non_constant_p, overflow_p); |
6292 | evaluated = true; | |
6293 | if (*non_constant_p) | |
6294 | return t; | |
6295 | } | |
6296 | break; | |
60813a46 JM |
6297 | } |
6298 | } | |
6299 | ||
04e1749c MP |
6300 | if (modifying_const_object_p (TREE_CODE (t), object, mutable_p) |
6301 | && const_object_being_modified == NULL_TREE) | |
6302 | const_object_being_modified = object; | |
6303 | ||
90bc2d09 NS |
6304 | if (DECL_P (object) |
6305 | && TREE_CLOBBER_P (init) | |
6306 | && DECL_NAME (object) == heap_deleted_identifier) | |
6307 | /* Ignore clobbers of deleted allocations for now; we'll get a better error | |
6308 | message later when operator delete is called. */ | |
6309 | return void_node; | |
6310 | ||
60813a46 JM |
6311 | /* And then find/build up our initializer for the path to the subobject |
6312 | we're initializing. */ | |
a1408b7e | 6313 | tree *valp; |
077dd9b3 | 6314 | if (DECL_P (object)) |
90bc2d09 | 6315 | valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR); |
a1408b7e JM |
6316 | else |
6317 | valp = NULL; | |
60813a46 JM |
6318 | if (!valp) |
6319 | { | |
6320 | /* A constant-expression cannot modify objects from outside the | |
6321 | constant-expression. */ | |
2b3ab879 | 6322 | if (!ctx->quiet) |
90bc2d09 NS |
6323 | { |
6324 | auto_diagnostic_group d; | |
6325 | if (DECL_P (object) && DECL_NAME (object) == heap_deleted_identifier) | |
6326 | { | |
6327 | error ("modification of allocated storage after deallocation " | |
6328 | "is not a constant expression"); | |
6329 | inform (DECL_SOURCE_LOCATION (object), "allocated here"); | |
6330 | } | |
6331 | else if (DECL_P (object) && ctx->global->is_outside_lifetime (object)) | |
6332 | { | |
6333 | if (TREE_CLOBBER_P (init)) | |
6334 | error ("destroying %qE outside its lifetime", object); | |
6335 | else | |
6336 | error ("modification of %qE outside its lifetime " | |
6337 | "is not a constant expression", object); | |
6338 | inform (DECL_SOURCE_LOCATION (object), "declared here"); | |
6339 | } | |
6340 | else | |
6341 | { | |
6342 | if (TREE_CLOBBER_P (init)) | |
6343 | error ("destroying %qE from outside current evaluation " | |
6344 | "is not a constant expression", object); | |
6345 | else | |
6346 | error ("modification of %qE from outside current evaluation " | |
6347 | "is not a constant expression", object); | |
6348 | } | |
6349 | } | |
60813a46 JM |
6350 | *non_constant_p = true; |
6351 | return t; | |
6352 | } | |
90bc2d09 NS |
6353 | |
6354 | /* Handle explicit end-of-lifetime. */ | |
6355 | if (TREE_CLOBBER_P (init)) | |
6356 | { | |
6357 | if (refs->is_empty ()) | |
6358 | ctx->global->destroy_value (object); | |
6359 | return void_node; | |
6360 | } | |
6361 | ||
3f8e2835 | 6362 | type = TREE_TYPE (object); |
c75ce530 | 6363 | bool no_zero_init = true; |
2d63bc39 | 6364 | |
19077677 JJ |
6365 | auto_vec<tree *> ctors; |
6366 | releasing_vec indexes; | |
37244b21 PP |
6367 | auto_vec<int> index_pos_hints; |
6368 | bool activated_union_member_p = false; | |
967cdbe6 | 6369 | bool empty_base = false; |
276a52d5 | 6370 | while (!refs->is_empty ()) |
60813a46 JM |
6371 | { |
6372 | if (*valp == NULL_TREE) | |
6373 | { | |
6374 | *valp = build_constructor (type, NULL); | |
e8c48716 | 6375 | CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; |
60813a46 | 6376 | } |
be20dcc3 MP |
6377 | else if (STRIP_ANY_LOCATION_WRAPPER (*valp), |
6378 | TREE_CODE (*valp) == STRING_CST) | |
d6b46fca NS |
6379 | { |
6380 | /* An array was initialized with a string constant, and now | |
6381 | we're writing into one of its elements. Explode the | |
6382 | single initialization into a set of element | |
6383 | initializations. */ | |
6384 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); | |
6385 | ||
6386 | tree string = *valp; | |
6387 | tree elt_type = TREE_TYPE (type); | |
6388 | unsigned chars_per_elt = (TYPE_PRECISION (elt_type) | |
6389 | / TYPE_PRECISION (char_type_node)); | |
6390 | unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt; | |
6391 | tree ary_ctor = build_constructor (type, NULL); | |
6392 | ||
6393 | vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts); | |
6394 | for (unsigned ix = 0; ix != num_elts; ix++) | |
6395 | { | |
6396 | constructor_elt elt = | |
6397 | { | |
6398 | build_int_cst (size_type_node, ix), | |
6399 | extract_string_elt (string, chars_per_elt, ix) | |
6400 | }; | |
6401 | CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt); | |
6402 | } | |
94ff4c9d | 6403 | |
d6b46fca NS |
6404 | *valp = ary_ctor; |
6405 | } | |
6406 | ||
ceaaf873 | 6407 | enum tree_code code = TREE_CODE (type); |
967cdbe6 | 6408 | tree reftype = refs->pop(); |
ceaaf873 | 6409 | tree index = refs->pop(); |
1d260ab0 | 6410 | bool is_access_expr = refs->pop() != NULL_TREE; |
60813a46 | 6411 | |
19077677 JJ |
6412 | if (code == COMPLEX_TYPE) |
6413 | { | |
6414 | if (TREE_CODE (*valp) == COMPLEX_CST) | |
6415 | *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp), | |
6416 | TREE_IMAGPART (*valp)); | |
6417 | else if (TREE_CODE (*valp) == CONSTRUCTOR | |
6418 | && CONSTRUCTOR_NELTS (*valp) == 0 | |
6419 | && CONSTRUCTOR_NO_CLEARING (*valp)) | |
6420 | { | |
6421 | tree r = build_constructor (reftype, NULL); | |
6422 | CONSTRUCTOR_NO_CLEARING (r) = 1; | |
6423 | *valp = build2 (COMPLEX_EXPR, type, r, r); | |
6424 | } | |
6425 | gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR); | |
6426 | ctors.safe_push (valp); | |
6427 | vec_safe_push (indexes, index); | |
6428 | valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR); | |
6429 | gcc_checking_assert (refs->is_empty ()); | |
6430 | type = reftype; | |
6431 | break; | |
6432 | } | |
6433 | ||
6434 | /* If the value of object is already zero-initialized, any new ctors for | |
6435 | subobjects will also be zero-initialized. */ | |
6436 | no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp); | |
6437 | ||
a8dd2b3e | 6438 | if (code == RECORD_TYPE && is_empty_field (index)) |
a4dfd0f0 JM |
6439 | /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they |
6440 | have no data and might have an offset lower than previously declared | |
6441 | fields, which confuses the middle-end. The code below will notice | |
6442 | that we don't have a CONSTRUCTOR for our inner target and just | |
6443 | return init. */ | |
967cdbe6 JM |
6444 | { |
6445 | empty_base = true; | |
6446 | break; | |
6447 | } | |
6448 | ||
1d260ab0 NS |
6449 | /* If a union is zero-initialized, its first non-static named data member |
6450 | is zero-initialized (and therefore active). */ | |
6451 | if (code == UNION_TYPE | |
6452 | && !no_zero_init | |
6453 | && CONSTRUCTOR_NELTS (*valp) == 0) | |
6454 | if (tree first = next_aggregate_field (TYPE_FIELDS (type))) | |
6455 | CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (*valp), first, NULL_TREE); | |
6456 | ||
6457 | /* Check for implicit change of active member for a union. */ | |
6458 | if (code == UNION_TYPE | |
6459 | && (CONSTRUCTOR_NELTS (*valp) == 0 | |
6460 | || CONSTRUCTOR_ELT (*valp, 0)->index != index) | |
6461 | /* An INIT_EXPR of the last member in an access chain is always OK, | |
6462 | but still check implicit change of members earlier on; see | |
6463 | cpp2a/constexpr-union6.C. */ | |
6464 | && !(TREE_CODE (t) == INIT_EXPR && refs->is_empty ())) | |
ceaaf873 | 6465 | { |
1d260ab0 NS |
6466 | bool has_active_member = CONSTRUCTOR_NELTS (*valp) != 0; |
6467 | tree inner = strip_array_types (reftype); | |
6468 | ||
6469 | if (has_active_member && cxx_dialect < cxx20) | |
ed4ec9ce | 6470 | { |
37244b21 PP |
6471 | if (!ctx->quiet) |
6472 | error_at (cp_expr_loc_or_input_loc (t), | |
6473 | "change of the active member of a union " | |
1d260ab0 NS |
6474 | "from %qD to %qD is not a constant expression " |
6475 | "before C++20", | |
37244b21 PP |
6476 | CONSTRUCTOR_ELT (*valp, 0)->index, |
6477 | index); | |
6478 | *non_constant_p = true; | |
ed4ec9ce | 6479 | } |
1d260ab0 NS |
6480 | else if (!is_access_expr |
6481 | || (TREE_CODE (t) == MODIFY_EXPR | |
6482 | && CLASS_TYPE_P (inner) | |
6483 | && !type_has_non_deleted_trivial_default_ctor (inner))) | |
6484 | { | |
6485 | /* Diagnose changing active union member after initialization | |
6486 | without a valid member access expression, as described in | |
6487 | [class.union.general] p5. */ | |
6488 | if (!ctx->quiet) | |
6489 | { | |
6490 | auto_diagnostic_group d; | |
6491 | if (has_active_member) | |
6492 | error_at (cp_expr_loc_or_input_loc (t), | |
6493 | "accessing %qD member instead of initialized " | |
6494 | "%qD member in constant expression", | |
6495 | index, CONSTRUCTOR_ELT (*valp, 0)->index); | |
6496 | else | |
6497 | error_at (cp_expr_loc_or_input_loc (t), | |
6498 | "accessing uninitialized member %qD", | |
6499 | index); | |
6500 | if (is_access_expr) | |
6501 | inform (DECL_SOURCE_LOCATION (index), | |
6502 | "%qD does not implicitly begin its lifetime " | |
6503 | "because %qT does not have a non-deleted " | |
6504 | "trivial default constructor, use " | |
6505 | "%<std::construct_at%> instead", | |
6506 | index, inner); | |
6507 | else | |
6508 | inform (DECL_SOURCE_LOCATION (index), | |
6509 | "initializing %qD requires a member access " | |
6510 | "expression as the left operand of the assignment", | |
6511 | index); | |
6512 | } | |
6513 | *non_constant_p = true; | |
6514 | } | |
6515 | else if (has_active_member && CONSTRUCTOR_NO_CLEARING (*valp)) | |
ceaaf873 | 6516 | { |
37244b21 PP |
6517 | /* Diagnose changing the active union member while the union |
6518 | is in the process of being initialized. */ | |
6519 | if (!ctx->quiet) | |
6520 | error_at (cp_expr_loc_or_input_loc (t), | |
6521 | "change of the active member of a union " | |
6522 | "from %qD to %qD during initialization", | |
6523 | CONSTRUCTOR_ELT (*valp, 0)->index, | |
6524 | index); | |
6525 | *non_constant_p = true; | |
ceaaf873 | 6526 | } |
37244b21 PP |
6527 | no_zero_init = true; |
6528 | } | |
88504f34 | 6529 | |
19077677 | 6530 | ctors.safe_push (valp); |
37244b21 | 6531 | vec_safe_push (indexes, index); |
88504f34 | 6532 | |
37244b21 | 6533 | constructor_elt *cep |
077dd9b3 | 6534 | = get_or_insert_ctor_field (*valp, index); |
37244b21 PP |
6535 | index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin()); |
6536 | ||
6537 | if (code == UNION_TYPE) | |
6538 | activated_union_member_p = true; | |
b599bf9d | 6539 | |
60813a46 | 6540 | valp = &cep->value; |
1d260ab0 | 6541 | type = reftype; |
60813a46 | 6542 | } |
60813a46 | 6543 | |
d19b4342 JM |
6544 | /* For initialization of an empty base, the original target will be |
6545 | *(base*)this, evaluation of which resolves to the object | |
6546 | argument, which has the derived type rather than the base type. */ | |
6547 | if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p | |
6548 | (initialized_type (init), type))) | |
6549 | { | |
6550 | gcc_assert (is_empty_class (TREE_TYPE (target))); | |
6551 | empty_base = true; | |
6552 | } | |
6553 | ||
04e1749c MP |
6554 | /* Detect modifying a constant object in constexpr evaluation. |
6555 | We have found a const object that is being modified. Figure out | |
6556 | if we need to issue an error. Consider | |
6557 | ||
6558 | struct A { | |
6559 | int n; | |
6560 | constexpr A() : n(1) { n = 2; } // #1 | |
6561 | }; | |
6562 | struct B { | |
6563 | const A a; | |
6564 | constexpr B() { a.n = 3; } // #2 | |
6565 | }; | |
6566 | constexpr B b{}; | |
6567 | ||
6568 | #1 is OK, since we're modifying an object under construction, but | |
6569 | #2 is wrong, since "a" is const and has been fully constructed. | |
6570 | To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR | |
6571 | which means that the object is read-only. For the example above, the | |
6572 | *ctors stack at the point of #2 will look like: | |
6573 | ||
6574 | ctors[0] = {.a={.n=2}} TREE_READONLY = 0 | |
6575 | ctors[1] = {.n=2} TREE_READONLY = 1 | |
6576 | ||
6577 | and we're modifying "b.a", so we search the stack and see if the | |
6578 | constructor for "b.a" has already run. */ | |
6579 | if (const_object_being_modified) | |
6580 | { | |
6581 | bool fail = false; | |
276a52d5 JJ |
6582 | tree const_objtype |
6583 | = strip_array_types (TREE_TYPE (const_object_being_modified)); | |
6584 | if (!CLASS_TYPE_P (const_objtype)) | |
04e1749c MP |
6585 | fail = true; |
6586 | else | |
6587 | { | |
6588 | /* [class.ctor]p5 "A constructor can be invoked for a const, | |
6589 | volatile, or const volatile object. const and volatile | |
6590 | semantics are not applied on an object under construction. | |
6591 | They come into effect when the constructor for the most | |
6592 | derived object ends." */ | |
19077677 | 6593 | for (tree *elt : ctors) |
04e1749c | 6594 | if (same_type_ignoring_top_level_qualifiers_p |
19077677 | 6595 | (TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt))) |
04e1749c | 6596 | { |
19077677 | 6597 | fail = TREE_READONLY (*elt); |
04e1749c MP |
6598 | break; |
6599 | } | |
6600 | } | |
6601 | if (fail) | |
6602 | { | |
6603 | if (!ctx->quiet) | |
6604 | modifying_const_object_error (t, const_object_being_modified); | |
6605 | *non_constant_p = true; | |
6606 | return t; | |
6607 | } | |
6608 | } | |
6609 | ||
e8b3c1bc | 6610 | if (!preeval) |
60813a46 | 6611 | { |
6c59b8a9 JM |
6612 | /* We're handling an INIT_EXPR of class type, so the value of the |
6613 | initializer can depend on the object it's initializing. */ | |
6614 | ||
60813a46 JM |
6615 | /* Create a new CONSTRUCTOR in case evaluation of the initializer |
6616 | wants to modify it. */ | |
acb2970c | 6617 | if (*valp == NULL_TREE) |
73a84269 | 6618 | { |
664beaf2 | 6619 | *valp = build_constructor (type, NULL); |
e8c48716 | 6620 | CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; |
73a84269 | 6621 | } |
d19b4342 | 6622 | new_ctx.ctor = empty_base ? NULL_TREE : *valp; |
60813a46 | 6623 | new_ctx.object = target; |
10d2f801 JM |
6624 | /* Avoid temporary materialization when initializing from a TARGET_EXPR. |
6625 | We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because | |
6626 | expansion of those trees uses ctx instead. */ | |
6627 | if (TREE_CODE (init) == TARGET_EXPR) | |
6628 | if (tree tinit = TARGET_EXPR_INITIAL (init)) | |
6629 | init = tinit; | |
72f76540 | 6630 | init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue, |
e8b3c1bc | 6631 | non_constant_p, overflow_p); |
37244b21 PP |
6632 | /* The hash table might have moved since the get earlier, and the |
6633 | initializer might have mutated the underlying CONSTRUCTORs, so we must | |
6634 | recompute VALP. */ | |
90bc2d09 | 6635 | valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR); |
37244b21 PP |
6636 | for (unsigned i = 0; i < vec_safe_length (indexes); i++) |
6637 | { | |
19077677 | 6638 | ctors[i] = valp; |
37244b21 PP |
6639 | constructor_elt *cep |
6640 | = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]); | |
6641 | valp = &cep->value; | |
6642 | } | |
60813a46 JM |
6643 | } |
6644 | ||
967cdbe6 JM |
6645 | if (*non_constant_p) |
6646 | return t; | |
6647 | ||
7574c916 | 6648 | /* Don't share a CONSTRUCTOR that might be changed later. */ |
0146e25f | 6649 | init = unshare_constructor (init); |
d96e8407 | 6650 | |
967cdbe6 JM |
6651 | gcc_checking_assert (!*valp || (same_type_ignoring_top_level_qualifiers_p |
6652 | (TREE_TYPE (*valp), type))); | |
d19b4342 JM |
6653 | if (empty_base) |
6654 | { | |
6655 | /* Just evaluate the initializer and return, since there's no actual data | |
6656 | to store, and we didn't build a CONSTRUCTOR. */ | |
967cdbe6 JM |
6657 | if (!*valp) |
6658 | { | |
6659 | /* But do make sure we have something in *valp. */ | |
6660 | *valp = build_constructor (type, nullptr); | |
6661 | CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; | |
6662 | } | |
6663 | } | |
6664 | else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR | |
6665 | && TREE_CODE (init) == CONSTRUCTOR) | |
acb2970c | 6666 | { |
d96e8407 JM |
6667 | /* An outer ctx->ctor might be pointing to *valp, so replace |
6668 | its contents. */ | |
6669 | CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init); | |
6670 | TREE_CONSTANT (*valp) = TREE_CONSTANT (init); | |
6671 | TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init); | |
e8c48716 JM |
6672 | CONSTRUCTOR_NO_CLEARING (*valp) |
6673 | = CONSTRUCTOR_NO_CLEARING (init); | |
acb2970c | 6674 | } |
60813a46 | 6675 | else |
d96e8407 | 6676 | *valp = init; |
2d63bc39 | 6677 | |
7eb5be6a MP |
6678 | /* After initialization, 'const' semantics apply to the value of the |
6679 | object. Make a note of this fact by marking the CONSTRUCTOR | |
6680 | TREE_READONLY. */ | |
6681 | if (TREE_CODE (t) == INIT_EXPR | |
6682 | && TREE_CODE (*valp) == CONSTRUCTOR | |
6683 | && TYPE_READONLY (type)) | |
64da1b76 PP |
6684 | { |
6685 | if (INDIRECT_REF_P (target) | |
6686 | && (is_this_parameter | |
6687 | (tree_strip_nop_conversions (TREE_OPERAND (target, 0))))) | |
6688 | /* We've just initialized '*this' (perhaps via the target | |
6689 | constructor of a delegating constructor). Leave it up to the | |
6690 | caller that set 'this' to set TREE_READONLY appropriately. */ | |
6691 | gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p | |
967cdbe6 | 6692 | (TREE_TYPE (target), type) || empty_base); |
64da1b76 PP |
6693 | else |
6694 | TREE_READONLY (*valp) = true; | |
6695 | } | |
7eb5be6a | 6696 | |
d96e8407 JM |
6697 | /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing |
6698 | CONSTRUCTORs, if any. */ | |
d96e8407 JM |
6699 | bool c = TREE_CONSTANT (init); |
6700 | bool s = TREE_SIDE_EFFECTS (init); | |
19077677 JJ |
6701 | if (!indexes->is_empty ()) |
6702 | { | |
6703 | tree last = indexes->last (); | |
6704 | if (TREE_CODE (last) == REALPART_EXPR | |
6705 | || TREE_CODE (last) == IMAGPART_EXPR) | |
6706 | { | |
6707 | /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if | |
6708 | possible. */ | |
6709 | tree *cexpr = ctors.last (); | |
6710 | if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr), | |
6711 | TREE_OPERAND (*cexpr, 0), | |
6712 | TREE_OPERAND (*cexpr, 1))) | |
6713 | *cexpr = c; | |
6714 | else | |
6715 | { | |
6716 | TREE_CONSTANT (*cexpr) | |
6717 | = (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0)) | |
6718 | & TREE_CONSTANT (TREE_OPERAND (*cexpr, 1))); | |
6719 | TREE_SIDE_EFFECTS (*cexpr) | |
6720 | = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0)) | |
6721 | | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1))); | |
6722 | } | |
6723 | c = TREE_CONSTANT (*cexpr); | |
6724 | s = TREE_SIDE_EFFECTS (*cexpr); | |
6725 | } | |
6726 | } | |
37244b21 | 6727 | if (!c || s || activated_union_member_p) |
19077677 | 6728 | for (tree *elt : ctors) |
d96e8407 | 6729 | { |
19077677 JJ |
6730 | if (TREE_CODE (*elt) != CONSTRUCTOR) |
6731 | continue; | |
d96e8407 | 6732 | if (!c) |
19077677 | 6733 | TREE_CONSTANT (*elt) = false; |
d96e8407 | 6734 | if (s) |
19077677 | 6735 | TREE_SIDE_EFFECTS (*elt) = true; |
b599bf9d PP |
6736 | /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of |
6737 | this union. */ | |
19077677 JJ |
6738 | if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE) |
6739 | CONSTRUCTOR_NO_CLEARING (*elt) = false; | |
d96e8407 | 6740 | } |
60813a46 | 6741 | |
967cdbe6 | 6742 | if (lval) |
60813a46 JM |
6743 | return target; |
6744 | else | |
3bdf0a9b | 6745 | return init; |
60813a46 JM |
6746 | } |
6747 | ||
6748 | /* Evaluate a ++ or -- expression. */ | |
6749 | ||
6750 | static tree | |
6751 | cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t, | |
72f76540 | 6752 | value_cat lval, |
60813a46 JM |
6753 | bool *non_constant_p, bool *overflow_p) |
6754 | { | |
6755 | enum tree_code code = TREE_CODE (t); | |
6756 | tree type = TREE_TYPE (t); | |
6757 | tree op = TREE_OPERAND (t, 0); | |
6758 | tree offset = TREE_OPERAND (t, 1); | |
6759 | gcc_assert (TREE_CONSTANT (offset)); | |
6760 | ||
d85569f6 MP |
6761 | /* OFFSET is constant, but perhaps not constant enough. We need to |
6762 | e.g. bash FLOAT_EXPRs to REAL_CSTs. */ | |
6763 | offset = fold_simple (offset); | |
6764 | ||
60813a46 | 6765 | /* The operand as an lvalue. */ |
72f76540 | 6766 | op = cxx_eval_constant_expression (ctx, op, vc_glvalue, |
5a804683 | 6767 | non_constant_p, overflow_p); |
60813a46 JM |
6768 | |
6769 | /* The operand as an rvalue. */ | |
84dd815f | 6770 | tree val |
72f76540 | 6771 | = cxx_eval_constant_expression (ctx, op, vc_prvalue, |
84dd815f | 6772 | non_constant_p, overflow_p); |
caee690e JM |
6773 | /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to |
6774 | a local array in a constexpr function. */ | |
71a93b08 | 6775 | bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val)); |
caee690e JM |
6776 | if (!ptr) |
6777 | VERIFY_CONSTANT (val); | |
60813a46 JM |
6778 | |
6779 | /* The modified value. */ | |
6780 | bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR); | |
ac2f8d26 | 6781 | tree mod; |
71a93b08 | 6782 | if (INDIRECT_TYPE_P (type)) |
ac2f8d26 JM |
6783 | { |
6784 | /* The middle end requires pointers to use POINTER_PLUS_EXPR. */ | |
6785 | offset = convert_to_ptrofftype (offset); | |
6786 | if (!inc) | |
6787 | offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset); | |
6788 | mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset); | |
6789 | } | |
da8c362c JJ |
6790 | else if (c_promoting_integer_type_p (type) |
6791 | && !TYPE_UNSIGNED (type) | |
6792 | && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) | |
6793 | { | |
6794 | offset = fold_convert (integer_type_node, offset); | |
6795 | mod = fold_convert (integer_type_node, val); | |
6796 | tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node, | |
6797 | mod, offset); | |
6798 | mod = fold_convert (type, t); | |
6799 | if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t)) | |
6800 | TREE_OVERFLOW (mod) = false; | |
6801 | } | |
ac2f8d26 JM |
6802 | else |
6803 | mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset); | |
caee690e JM |
6804 | if (!ptr) |
6805 | VERIFY_CONSTANT (mod); | |
60813a46 JM |
6806 | |
6807 | /* Storing the modified value. */ | |
04e1749c MP |
6808 | tree store = build2_loc (cp_expr_loc_or_loc (t, input_location), |
6809 | MODIFY_EXPR, type, op, mod); | |
d1bba463 PP |
6810 | mod = cxx_eval_constant_expression (ctx, store, lval, |
6811 | non_constant_p, overflow_p); | |
ecdcd560 | 6812 | ggc_free (store); |
d1bba463 PP |
6813 | if (*non_constant_p) |
6814 | return t; | |
60813a46 JM |
6815 | |
6816 | /* And the value of the expression. */ | |
6817 | if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR) | |
d1bba463 PP |
6818 | /* Prefix ops are lvalues, but the caller might want an rvalue; |
6819 | lval has already been taken into account in the store above. */ | |
6820 | return mod; | |
60813a46 JM |
6821 | else |
6822 | /* Postfix ops are rvalues. */ | |
6823 | return val; | |
6824 | } | |
6825 | ||
56632b27 JM |
6826 | /* Predicates for the meaning of *jump_target. */ |
6827 | ||
6828 | static bool | |
6829 | returns (tree *jump_target) | |
6830 | { | |
6831 | return *jump_target | |
9c6a4bee | 6832 | && TREE_CODE (*jump_target) == RETURN_EXPR; |
56632b27 JM |
6833 | } |
6834 | ||
6835 | static bool | |
6836 | breaks (tree *jump_target) | |
6837 | { | |
6838 | return *jump_target | |
06ec22b7 JM |
6839 | && ((TREE_CODE (*jump_target) == LABEL_DECL |
6840 | && LABEL_DECL_BREAK (*jump_target)) | |
3075affd | 6841 | || TREE_CODE (*jump_target) == BREAK_STMT |
06ec22b7 | 6842 | || TREE_CODE (*jump_target) == EXIT_EXPR); |
56632b27 JM |
6843 | } |
6844 | ||
6845 | static bool | |
6846 | continues (tree *jump_target) | |
6847 | { | |
6848 | return *jump_target | |
3075affd JM |
6849 | && ((TREE_CODE (*jump_target) == LABEL_DECL |
6850 | && LABEL_DECL_CONTINUE (*jump_target)) | |
6851 | || TREE_CODE (*jump_target) == CONTINUE_STMT); | |
6852 | ||
56632b27 JM |
6853 | } |
6854 | ||
6855 | static bool | |
6856 | switches (tree *jump_target) | |
6857 | { | |
6858 | return *jump_target | |
6859 | && TREE_CODE (*jump_target) == INTEGER_CST; | |
6860 | } | |
6861 | ||
6862 | /* Subroutine of cxx_eval_statement_list. Determine whether the statement | |
4b390698 JJ |
6863 | STMT matches *jump_target. If we're looking for a case label and we see |
6864 | the default label, note it in ctx->css_state. */ | |
56632b27 JM |
6865 | |
6866 | static bool | |
4b390698 | 6867 | label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt) |
56632b27 | 6868 | { |
56632b27 JM |
6869 | switch (TREE_CODE (*jump_target)) |
6870 | { | |
6871 | case LABEL_DECL: | |
6872 | if (TREE_CODE (stmt) == LABEL_EXPR | |
6873 | && LABEL_EXPR_LABEL (stmt) == *jump_target) | |
6874 | return true; | |
6875 | break; | |
6876 | ||
6877 | case INTEGER_CST: | |
6878 | if (TREE_CODE (stmt) == CASE_LABEL_EXPR) | |
6879 | { | |
4b390698 | 6880 | gcc_assert (ctx->css_state != NULL); |
56632b27 | 6881 | if (!CASE_LOW (stmt)) |
4b390698 JJ |
6882 | { |
6883 | /* default: should appear just once in a SWITCH_EXPR | |
6884 | body (excluding nested SWITCH_EXPR). */ | |
6885 | gcc_assert (*ctx->css_state != css_default_seen); | |
6886 | /* When evaluating SWITCH_EXPR body for the second time, | |
6887 | return true for the default: label. */ | |
6888 | if (*ctx->css_state == css_default_processing) | |
6889 | return true; | |
6890 | *ctx->css_state = css_default_seen; | |
6891 | } | |
385ed708 JJ |
6892 | else if (CASE_HIGH (stmt)) |
6893 | { | |
6894 | if (tree_int_cst_le (CASE_LOW (stmt), *jump_target) | |
6895 | && tree_int_cst_le (*jump_target, CASE_HIGH (stmt))) | |
6896 | return true; | |
6897 | } | |
56632b27 JM |
6898 | else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt))) |
6899 | return true; | |
6900 | } | |
6901 | break; | |
6902 | ||
43574e4f JJ |
6903 | case BREAK_STMT: |
6904 | case CONTINUE_STMT: | |
6905 | /* These two are handled directly in cxx_eval_loop_expr by testing | |
6906 | breaks (jump_target) or continues (jump_target). */ | |
6907 | break; | |
6908 | ||
56632b27 JM |
6909 | default: |
6910 | gcc_unreachable (); | |
6911 | } | |
6912 | return false; | |
6913 | } | |
6914 | ||
6915 | /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump | |
6916 | semantics, for switch, break, continue, and return. */ | |
6917 | ||
6918 | static tree | |
6919 | cxx_eval_statement_list (const constexpr_ctx *ctx, tree t, | |
56632b27 JM |
6920 | bool *non_constant_p, bool *overflow_p, |
6921 | tree *jump_target) | |
6922 | { | |
58611fb6 | 6923 | tree local_target; |
345edb37 JJ |
6924 | /* In a statement-expression we want to return the last value. |
6925 | For empty statement expression return void_node. */ | |
6926 | tree r = void_node; | |
58611fb6 JM |
6927 | if (!jump_target) |
6928 | { | |
6929 | local_target = NULL_TREE; | |
6930 | jump_target = &local_target; | |
6931 | } | |
72f76540 | 6932 | for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i) |
56632b27 | 6933 | { |
72f76540 JM |
6934 | tree stmt = *i; |
6935 | ||
0250ba58 MP |
6936 | /* We've found a continue, so skip everything until we reach |
6937 | the label its jumping to. */ | |
6938 | if (continues (jump_target)) | |
6939 | { | |
6940 | if (label_matches (ctx, jump_target, stmt)) | |
6941 | /* Found it. */ | |
6942 | *jump_target = NULL_TREE; | |
6943 | else | |
6944 | continue; | |
6945 | } | |
6ef72c36 JJ |
6946 | if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT) |
6947 | continue; | |
72f76540 JM |
6948 | |
6949 | value_cat lval = vc_discard; | |
6950 | /* The result of a statement-expression is not wrapped in EXPR_STMT. */ | |
6951 | if (tsi_one_before_end_p (i) && TREE_CODE (stmt) != EXPR_STMT) | |
6952 | lval = vc_prvalue; | |
6953 | ||
6954 | r = cxx_eval_constant_expression (ctx, stmt, lval, | |
58611fb6 JM |
6955 | non_constant_p, overflow_p, |
6956 | jump_target); | |
56632b27 JM |
6957 | if (*non_constant_p) |
6958 | break; | |
6959 | if (returns (jump_target) || breaks (jump_target)) | |
6960 | break; | |
6961 | } | |
ce36ba09 JM |
6962 | if (*jump_target && jump_target == &local_target) |
6963 | { | |
6964 | /* We aren't communicating the jump to our caller, so give up. We don't | |
6965 | need to support evaluation of jumps out of statement-exprs. */ | |
6966 | if (!ctx->quiet) | |
f9d0ca40 | 6967 | error_at (cp_expr_loc_or_input_loc (r), |
ce36ba09 JM |
6968 | "statement is not a constant expression"); |
6969 | *non_constant_p = true; | |
6970 | } | |
58611fb6 | 6971 | return r; |
56632b27 JM |
6972 | } |
6973 | ||
6974 | /* Evaluate a LOOP_EXPR for side-effects. Handles break and return | |
6975 | semantics; continue semantics are covered by cxx_eval_statement_list. */ | |
6976 | ||
6977 | static tree | |
6978 | cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t, | |
56632b27 JM |
6979 | bool *non_constant_p, bool *overflow_p, |
6980 | tree *jump_target) | |
6981 | { | |
8e007055 JJ |
6982 | tree local_target; |
6983 | if (!jump_target) | |
6984 | { | |
6985 | local_target = NULL_TREE; | |
6986 | jump_target = &local_target; | |
6987 | } | |
39dce2b7 | 6988 | |
43574e4f | 6989 | tree body, cond = NULL_TREE, expr = NULL_TREE; |
5ec2cd9f | 6990 | int count = 0; |
43574e4f JJ |
6991 | switch (TREE_CODE (t)) |
6992 | { | |
6993 | case LOOP_EXPR: | |
6994 | body = LOOP_EXPR_BODY (t); | |
6995 | break; | |
6996 | case DO_STMT: | |
6997 | body = DO_BODY (t); | |
6998 | cond = DO_COND (t); | |
6999 | break; | |
7000 | case WHILE_STMT: | |
7001 | body = WHILE_BODY (t); | |
7002 | cond = WHILE_COND (t); | |
7003 | count = -1; | |
7004 | break; | |
7005 | case FOR_STMT: | |
7006 | if (FOR_INIT_STMT (t)) | |
72f76540 | 7007 | cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard, |
43574e4f JJ |
7008 | non_constant_p, overflow_p, jump_target); |
7009 | if (*non_constant_p) | |
7010 | return NULL_TREE; | |
7011 | body = FOR_BODY (t); | |
7012 | cond = FOR_COND (t); | |
7013 | expr = FOR_EXPR (t); | |
7014 | count = -1; | |
7015 | break; | |
7016 | default: | |
7017 | gcc_unreachable (); | |
7018 | } | |
d259b234 | 7019 | do |
56632b27 | 7020 | { |
43574e4f JJ |
7021 | if (count != -1) |
7022 | { | |
7023 | if (body) | |
9fdbd7d6 | 7024 | cxx_eval_constant_expression (ctx, body, vc_discard, |
43574e4f JJ |
7025 | non_constant_p, overflow_p, |
7026 | jump_target); | |
7027 | if (breaks (jump_target)) | |
7028 | { | |
7029 | *jump_target = NULL_TREE; | |
7030 | break; | |
7031 | } | |
39dce2b7 | 7032 | |
43574e4f JJ |
7033 | if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target)) |
7034 | *jump_target = NULL_TREE; | |
7035 | ||
7036 | if (expr) | |
9fdbd7d6 | 7037 | cxx_eval_constant_expression (ctx, expr, vc_prvalue, |
43574e4f JJ |
7038 | non_constant_p, overflow_p, |
7039 | jump_target); | |
7040 | } | |
7041 | ||
7042 | if (cond) | |
7043 | { | |
7044 | tree res | |
9fdbd7d6 | 7045 | = cxx_eval_constant_expression (ctx, cond, vc_prvalue, |
43574e4f JJ |
7046 | non_constant_p, overflow_p, |
7047 | jump_target); | |
7048 | if (res) | |
7049 | { | |
7050 | if (verify_constant (res, ctx->quiet, non_constant_p, | |
7051 | overflow_p)) | |
7052 | break; | |
7053 | if (integer_zerop (res)) | |
7054 | break; | |
7055 | } | |
7056 | else | |
7057 | gcc_assert (*jump_target); | |
7058 | } | |
39dce2b7 | 7059 | |
5ec2cd9f JM |
7060 | if (++count >= constexpr_loop_limit) |
7061 | { | |
7062 | if (!ctx->quiet) | |
f9d0ca40 | 7063 | error_at (cp_expr_loc_or_input_loc (t), |
84fa214d | 7064 | "%<constexpr%> loop iteration count exceeds limit of %d " |
a3f9f006 | 7065 | "(use %<-fconstexpr-loop-limit=%> to increase the limit)", |
5ec2cd9f JM |
7066 | constexpr_loop_limit); |
7067 | *non_constant_p = true; | |
7068 | break; | |
7069 | } | |
56632b27 | 7070 | } |
4b390698 JJ |
7071 | while (!returns (jump_target) |
7072 | && !breaks (jump_target) | |
43574e4f JJ |
7073 | && !continues (jump_target) |
7074 | && (!switches (jump_target) || count == 0) | |
4b390698 | 7075 | && !*non_constant_p); |
d259b234 | 7076 | |
56632b27 JM |
7077 | return NULL_TREE; |
7078 | } | |
7079 | ||
7080 | /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump | |
7081 | semantics. */ | |
7082 | ||
7083 | static tree | |
7084 | cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t, | |
56632b27 JM |
7085 | bool *non_constant_p, bool *overflow_p, |
7086 | tree *jump_target) | |
7087 | { | |
43574e4f JJ |
7088 | tree cond |
7089 | = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t); | |
72f76540 | 7090 | cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue, |
5a804683 | 7091 | non_constant_p, overflow_p); |
56632b27 JM |
7092 | VERIFY_CONSTANT (cond); |
7093 | *jump_target = cond; | |
7094 | ||
43574e4f JJ |
7095 | tree body |
7096 | = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t); | |
4b390698 JJ |
7097 | constexpr_ctx new_ctx = *ctx; |
7098 | constexpr_switch_state css = css_default_not_seen; | |
7099 | new_ctx.css_state = &css; | |
72f76540 | 7100 | cxx_eval_constant_expression (&new_ctx, body, vc_discard, |
4b390698 JJ |
7101 | non_constant_p, overflow_p, jump_target); |
7102 | if (switches (jump_target) && css == css_default_seen) | |
7103 | { | |
7104 | /* If the SWITCH_EXPR body has default: label, process it once again, | |
7105 | this time instructing label_matches to return true for default: | |
7106 | label on switches (jump_target). */ | |
7107 | css = css_default_processing; | |
72f76540 | 7108 | cxx_eval_constant_expression (&new_ctx, body, vc_discard, |
4b390698 JJ |
7109 | non_constant_p, overflow_p, jump_target); |
7110 | } | |
56632b27 JM |
7111 | if (breaks (jump_target) || switches (jump_target)) |
7112 | *jump_target = NULL_TREE; | |
7113 | return NULL_TREE; | |
7114 | } | |
7115 | ||
89262ec6 JM |
7116 | /* Find the object of TYPE under initialization in CTX. */ |
7117 | ||
7118 | static tree | |
72f76540 | 7119 | lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type) |
89262ec6 | 7120 | { |
fbd603c4 | 7121 | if (!ctx) |
89262ec6 JM |
7122 | return NULL_TREE; |
7123 | ||
49a86fce PP |
7124 | /* Prefer the outermost matching object, but don't cross |
7125 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */ | |
7126 | if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor)) | |
7127 | if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type)) | |
7128 | return outer_ob; | |
7129 | ||
89262ec6 JM |
7130 | /* We could use ctx->object unconditionally, but using ctx->ctor when we |
7131 | can is a minor optimization. */ | |
fbd603c4 | 7132 | if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type)) |
89262ec6 JM |
7133 | return ctx->ctor; |
7134 | ||
fbd603c4 JM |
7135 | if (!ctx->object) |
7136 | return NULL_TREE; | |
7137 | ||
89262ec6 JM |
7138 | /* Since an object cannot have a field of its own type, we can search outward |
7139 | from ctx->object to find the unique containing object of TYPE. */ | |
7140 | tree ob = ctx->object; | |
7141 | while (ob) | |
7142 | { | |
7143 | if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type)) | |
7144 | break; | |
7145 | if (handled_component_p (ob)) | |
7146 | ob = TREE_OPERAND (ob, 0); | |
7147 | else | |
7148 | ob = NULL_TREE; | |
7149 | } | |
7150 | ||
7151 | return ob; | |
7152 | } | |
7153 | ||
c85f8dbb MP |
7154 | /* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is |
7155 | true, we're checking a constexpr function body. */ | |
7c814975 MP |
7156 | |
7157 | static void | |
c85f8dbb | 7158 | inline_asm_in_constexpr_error (location_t loc, bool fundef_p) |
7c814975 MP |
7159 | { |
7160 | auto_diagnostic_group d; | |
c85f8dbb MP |
7161 | if (constexpr_error (loc, fundef_p, "inline assembly is not a " |
7162 | "constant expression")) | |
7163 | inform (loc, "only unevaluated inline assembly is allowed in a " | |
7164 | "%<constexpr%> function in C++20"); | |
7c814975 MP |
7165 | } |
7166 | ||
76b75018 JM |
7167 | /* We're getting the constant value of DECL in a manifestly constant-evaluated |
7168 | context; maybe complain about that. */ | |
7169 | ||
7170 | static void | |
7171 | maybe_warn_about_constant_value (location_t loc, tree decl) | |
7172 | { | |
7173 | static bool explained = false; | |
7174 | if (cxx_dialect >= cxx17 | |
7175 | && warn_interference_size | |
00f34291 | 7176 | && !OPTION_SET_P (param_destruct_interfere_size) |
76b75018 JM |
7177 | && DECL_CONTEXT (decl) == std_node |
7178 | && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size") | |
7179 | && (LOCATION_FILE (input_location) != main_input_filename | |
7180 | || module_exporting_p ()) | |
7181 | && warning_at (loc, OPT_Winterference_size, "use of %qD", decl) | |
7182 | && !explained) | |
7183 | { | |
7184 | explained = true; | |
7185 | inform (loc, "its value can vary between compiler versions or " | |
7186 | "with different %<-mtune%> or %<-mcpu%> flags"); | |
7187 | inform (loc, "if this use is part of a public ABI, change it to " | |
7188 | "instead use a constant variable you define"); | |
7189 | inform (loc, "the default value for the current CPU tuning " | |
7190 | "is %d bytes", param_destruct_interfere_size); | |
7191 | inform (loc, "you can stabilize this value with %<--param " | |
7192 | "hardware_destructive_interference_size=%d%>, or disable " | |
7193 | "this warning with %<-Wno-interference-size%>", | |
7194 | param_destruct_interfere_size); | |
7195 | } | |
7196 | } | |
7197 | ||
0a0c2c3f JJ |
7198 | /* For element type ELT_TYPE, return the appropriate type of the heap object |
7199 | containing such element(s). COOKIE_SIZE is NULL or the size of cookie | |
7200 | in bytes. If COOKIE_SIZE is NULL, return array type | |
7201 | ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return | |
7202 | struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; } | |
7203 | where N is is computed such that the size of the struct fits into FULL_SIZE. | |
7204 | If ARG_SIZE is non-NULL, it is the first argument to the new operator. | |
7205 | It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE | |
7206 | will be also 0 and so it is not possible to determine the actual array | |
7207 | size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant | |
7208 | expression evaluation of subexpressions of ARG_SIZE. */ | |
7209 | ||
7210 | static tree | |
7211 | build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type, | |
7212 | tree cookie_size, tree full_size, tree arg_size, | |
7213 | bool *non_constant_p, bool *overflow_p) | |
7214 | { | |
7215 | gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size)); | |
7216 | gcc_assert (tree_fits_uhwi_p (full_size)); | |
7217 | unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0; | |
7218 | if (arg_size) | |
7219 | { | |
7220 | STRIP_NOPS (arg_size); | |
7221 | if (cookie_size) | |
7222 | { | |
7223 | if (TREE_CODE (arg_size) != PLUS_EXPR) | |
7224 | arg_size = NULL_TREE; | |
7225 | else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST | |
7226 | && tree_int_cst_equal (cookie_size, | |
7227 | TREE_OPERAND (arg_size, 0))) | |
7228 | { | |
7229 | arg_size = TREE_OPERAND (arg_size, 1); | |
7230 | STRIP_NOPS (arg_size); | |
7231 | } | |
7232 | else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST | |
7233 | && tree_int_cst_equal (cookie_size, | |
7234 | TREE_OPERAND (arg_size, 1))) | |
7235 | { | |
7236 | arg_size = TREE_OPERAND (arg_size, 0); | |
7237 | STRIP_NOPS (arg_size); | |
7238 | } | |
7239 | else | |
7240 | arg_size = NULL_TREE; | |
7241 | } | |
7242 | if (arg_size && TREE_CODE (arg_size) == MULT_EXPR) | |
7243 | { | |
7244 | tree op0 = TREE_OPERAND (arg_size, 0); | |
7245 | tree op1 = TREE_OPERAND (arg_size, 1); | |
7246 | if (integer_zerop (op0)) | |
7247 | arg_size | |
72f76540 JM |
7248 | = cxx_eval_constant_expression (ctx, op1, vc_prvalue, |
7249 | non_constant_p, overflow_p); | |
0a0c2c3f JJ |
7250 | else if (integer_zerop (op1)) |
7251 | arg_size | |
72f76540 JM |
7252 | = cxx_eval_constant_expression (ctx, op0, vc_prvalue, |
7253 | non_constant_p, overflow_p); | |
0a0c2c3f JJ |
7254 | else |
7255 | arg_size = NULL_TREE; | |
7256 | } | |
7257 | else | |
7258 | arg_size = NULL_TREE; | |
7259 | } | |
7260 | ||
7261 | unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size); | |
7262 | if (!arg_size) | |
7263 | { | |
7264 | unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type); | |
7265 | gcc_assert (fsz >= csz); | |
7266 | fsz -= csz; | |
7267 | if (esz) | |
7268 | fsz /= esz; | |
7269 | } | |
7270 | tree itype2 = build_index_type (size_int (fsz - 1)); | |
7271 | if (!cookie_size) | |
7272 | return build_cplus_array_type (elt_type, itype2); | |
7273 | return build_new_constexpr_heap_type (elt_type, cookie_size, itype2); | |
7274 | } | |
7275 | ||
2d76680f PC |
7276 | /* Attempt to reduce the expression T to a constant value. |
7277 | On failure, issue diagnostic and return error_mark_node. */ | |
7278 | /* FIXME unify with c_fully_fold */ | |
60813a46 | 7279 | /* FIXME overflow_p is too global */ |
2d76680f PC |
7280 | |
7281 | static tree | |
3e605b20 | 7282 | cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, |
72f76540 | 7283 | value_cat lval, |
56632b27 | 7284 | bool *non_constant_p, bool *overflow_p, |
3075affd | 7285 | tree *jump_target /* = NULL */) |
2d76680f | 7286 | { |
4b390698 JJ |
7287 | if (jump_target && *jump_target) |
7288 | { | |
7289 | /* If we are jumping, ignore all statements/expressions except those | |
7290 | that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */ | |
7291 | switch (TREE_CODE (t)) | |
7292 | { | |
7293 | case BIND_EXPR: | |
7294 | case STATEMENT_LIST: | |
7295 | case LOOP_EXPR: | |
7296 | case COND_EXPR: | |
43574e4f JJ |
7297 | case IF_STMT: |
7298 | case DO_STMT: | |
7299 | case WHILE_STMT: | |
7300 | case FOR_STMT: | |
4b390698 JJ |
7301 | break; |
7302 | case LABEL_EXPR: | |
7303 | case CASE_LABEL_EXPR: | |
7304 | if (label_matches (ctx, jump_target, t)) | |
7305 | /* Found it. */ | |
7306 | *jump_target = NULL_TREE; | |
7307 | return NULL_TREE; | |
7308 | default: | |
7309 | return NULL_TREE; | |
7310 | } | |
7311 | } | |
3075affd | 7312 | if (error_operand_p (t)) |
2d76680f PC |
7313 | { |
7314 | *non_constant_p = true; | |
7315 | return t; | |
7316 | } | |
7a649ef5 | 7317 | |
5ebe5bcf NS |
7318 | /* Change the input location to the currently processed expression for |
7319 | better error messages when a subexpression has no location. */ | |
6821245b | 7320 | location_t loc = cp_expr_loc_or_input_loc (t); |
5ebe5bcf | 7321 | iloc_sentinel sentinel (loc); |
6821245b | 7322 | |
7a649ef5 JM |
7323 | STRIP_ANY_LOCATION_WRAPPER (t); |
7324 | ||
2d76680f PC |
7325 | if (CONSTANT_CLASS_P (t)) |
7326 | { | |
61637db3 JJ |
7327 | if (TREE_OVERFLOW (t)) |
7328 | { | |
7329 | if (!ctx->quiet) | |
7330 | permerror (input_location, "overflow in constant expression"); | |
7331 | if (!flag_permissive || ctx->quiet) | |
7332 | *overflow_p = true; | |
7333 | } | |
8bada5cd MS |
7334 | |
7335 | if (TREE_CODE (t) == INTEGER_CST | |
9f613f06 | 7336 | && TYPE_PTR_P (TREE_TYPE (t)) |
f45610a4 JJ |
7337 | /* INTEGER_CST with pointer-to-method type is only used |
7338 | for a virtual method in a pointer to member function. | |
7339 | Don't reject those. */ | |
7340 | && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE | |
8bada5cd MS |
7341 | && !integer_zerop (t)) |
7342 | { | |
7343 | if (!ctx->quiet) | |
7344 | error ("value %qE of type %qT is not a constant expression", | |
7345 | t, TREE_TYPE (t)); | |
7346 | *non_constant_p = true; | |
7347 | } | |
7348 | ||
2d76680f PC |
7349 | return t; |
7350 | } | |
2d76680f | 7351 | |
a15ffa22 | 7352 | /* Avoid excessively long constexpr evaluations. */ |
8e007055 | 7353 | if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit) |
a15ffa22 JJ |
7354 | { |
7355 | if (!ctx->quiet) | |
6821245b | 7356 | error_at (loc, |
a15ffa22 | 7357 | "%<constexpr%> evaluation operation count exceeds limit of " |
a9c697b8 | 7358 | "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)", |
a15ffa22 | 7359 | constexpr_ops_limit); |
8e007055 | 7360 | ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT); |
a15ffa22 JJ |
7361 | *non_constant_p = true; |
7362 | return t; | |
7363 | } | |
7364 | ||
7a649ef5 JM |
7365 | constexpr_ctx new_ctx; |
7366 | tree r = t; | |
7367 | ||
8bada5cd MS |
7368 | tree_code tcode = TREE_CODE (t); |
7369 | switch (tcode) | |
2d76680f | 7370 | { |
60813a46 | 7371 | case RESULT_DECL: |
92a596e8 | 7372 | if (lval) |
60813a46 JM |
7373 | return t; |
7374 | /* We ask for an rvalue for the RESULT_DECL when indirecting | |
bf66b9b4 AH |
7375 | through an invisible reference, or in named return value |
7376 | optimization. */ | |
e6a29aab JM |
7377 | if (tree v = ctx->global->get_value (t)) |
7378 | return v; | |
1efb1dc2 MP |
7379 | else |
7380 | { | |
7381 | if (!ctx->quiet) | |
7382 | error ("%qE is not a constant expression", t); | |
7383 | *non_constant_p = true; | |
7384 | } | |
7385 | break; | |
60813a46 | 7386 | |
2d76680f | 7387 | case VAR_DECL: |
70f40fea | 7388 | if (DECL_HAS_VALUE_EXPR_P (t)) |
c59fa7ea JM |
7389 | { |
7390 | if (is_normal_capture_proxy (t) | |
7391 | && current_function_decl == DECL_CONTEXT (t)) | |
7392 | { | |
7393 | /* Function parms aren't constexpr within the function | |
7394 | definition, so don't try to look at the closure. But if the | |
7395 | captured variable is constant, try to evaluate it directly. */ | |
7396 | r = DECL_CAPTURED_VARIABLE (t); | |
7397 | tree type = TREE_TYPE (t); | |
7398 | if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r))) | |
7399 | { | |
7400 | /* Adjust r to match the reference-ness of t. */ | |
7401 | if (TYPE_REF_P (type)) | |
7402 | r = build_address (r); | |
7403 | else | |
7404 | r = convert_from_reference (r); | |
7405 | } | |
7406 | } | |
7407 | else | |
7408 | r = DECL_VALUE_EXPR (t); | |
7409 | return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, | |
7410 | overflow_p); | |
7411 | } | |
191816a3 | 7412 | /* fall through */ |
7c83622c | 7413 | case CONST_DECL: |
e53b6e56 | 7414 | /* We used to not check lval for CONST_DECL, but darwin.cc uses |
7c83622c | 7415 | CONST_DECL for aggregate constants. */ |
92a596e8 | 7416 | if (lval) |
2d76680f | 7417 | return t; |
ef529765 JM |
7418 | else if (t == ctx->object) |
7419 | return ctx->ctor; | |
7420 | if (VAR_P (t)) | |
9fdbd7d6 NS |
7421 | { |
7422 | if (tree v = ctx->global->get_value (t)) | |
ef529765 | 7423 | { |
e6a29aab | 7424 | r = v; |
ef529765 JM |
7425 | break; |
7426 | } | |
9fdbd7d6 NS |
7427 | if (ctx->global->is_outside_lifetime (t)) |
7428 | { | |
7429 | if (!ctx->quiet) | |
7430 | outside_lifetime_error (loc, t); | |
7431 | *non_constant_p = true; | |
7432 | break; | |
7433 | } | |
7434 | } | |
3a0bc47c | 7435 | if (ctx->manifestly_const_eval == mce_true) |
76b75018 | 7436 | maybe_warn_about_constant_value (loc, t); |
f4fce183 | 7437 | if (COMPLETE_TYPE_P (TREE_TYPE (t)) |
dbcd32f8 | 7438 | && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
7dc2b4a2 JM |
7439 | { |
7440 | /* If the class is empty, we aren't actually loading anything. */ | |
7441 | r = build_constructor (TREE_TYPE (t), NULL); | |
7442 | TREE_CONSTANT (r) = true; | |
7443 | } | |
7444 | else if (ctx->strict) | |
8c00059c | 7445 | r = decl_really_constant_value (t, /*unshare_p=*/false); |
69eb4fde | 7446 | else |
8c00059c | 7447 | r = decl_constant_value (t, /*unshare_p=*/false); |
2d76680f PC |
7448 | if (TREE_CODE (r) == TARGET_EXPR |
7449 | && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR) | |
7450 | r = TARGET_EXPR_INITIAL (r); | |
99d114c1 MP |
7451 | if (DECL_P (r) |
7452 | /* P2280 allows references to unknown. */ | |
7453 | && !(VAR_P (t) && TYPE_REF_P (TREE_TYPE (t)))) | |
2d76680f | 7454 | { |
2b3ab879 | 7455 | if (!ctx->quiet) |
c85f8dbb | 7456 | non_const_var_error (loc, r, /*fundef_p*/false); |
2d76680f PC |
7457 | *non_constant_p = true; |
7458 | } | |
7459 | break; | |
7460 | ||
96a95ac1 AO |
7461 | case DEBUG_BEGIN_STMT: |
7462 | /* ??? It might be nice to retain this information somehow, so | |
7463 | as to be able to step into a constexpr function call. */ | |
7464 | /* Fall through. */ | |
7465 | ||
2d76680f PC |
7466 | case FUNCTION_DECL: |
7467 | case TEMPLATE_DECL: | |
7468 | case LABEL_DECL: | |
56632b27 JM |
7469 | case LABEL_EXPR: |
7470 | case CASE_LABEL_EXPR: | |
4b390698 | 7471 | case PREDICT_EXPR: |
2d76680f PC |
7472 | return t; |
7473 | ||
7474 | case PARM_DECL: | |
9f613f06 | 7475 | if (lval && !TYPE_REF_P (TREE_TYPE (t))) |
60813a46 | 7476 | /* glvalue use. */; |
12a11620 | 7477 | else if (tree v = ctx->global->get_value (t)) |
e6a29aab | 7478 | r = v; |
92a596e8 | 7479 | else if (lval) |
2d76680f | 7480 | /* Defer in case this is only used for its type. */; |
9fdbd7d6 NS |
7481 | else if (ctx->global->is_outside_lifetime (t)) |
7482 | { | |
7483 | if (!ctx->quiet) | |
7484 | outside_lifetime_error (loc, t); | |
7485 | *non_constant_p = true; | |
7486 | break; | |
7487 | } | |
1c2f613f | 7488 | else if (COMPLETE_TYPE_P (TREE_TYPE (t)) |
dbcd32f8 | 7489 | && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
052beba4 JM |
7490 | { |
7491 | /* If the class is empty, we aren't actually loading anything. */ | |
7492 | r = build_constructor (TREE_TYPE (t), NULL); | |
7493 | TREE_CONSTANT (r) = true; | |
7494 | } | |
99d114c1 MP |
7495 | else if (TYPE_REF_P (TREE_TYPE (t))) |
7496 | /* P2280 allows references to unknown... */; | |
7497 | else if (is_this_parameter (t)) | |
7498 | /* ...as well as the this pointer. */; | |
2d76680f PC |
7499 | else |
7500 | { | |
2b3ab879 | 7501 | if (!ctx->quiet) |
2d76680f PC |
7502 | error ("%qE is not a constant expression", t); |
7503 | *non_constant_p = true; | |
7504 | } | |
7505 | break; | |
7506 | ||
7507 | case CALL_EXPR: | |
7508 | case AGGR_INIT_EXPR: | |
92a596e8 | 7509 | r = cxx_eval_call_expression (ctx, t, lval, |
2d76680f PC |
7510 | non_constant_p, overflow_p); |
7511 | break; | |
7512 | ||
60813a46 JM |
7513 | case DECL_EXPR: |
7514 | { | |
7515 | r = DECL_EXPR_DECL (t); | |
43574e4f JJ |
7516 | if (TREE_CODE (r) == USING_DECL) |
7517 | { | |
7518 | r = void_node; | |
7519 | break; | |
7520 | } | |
8892d532 JJ |
7521 | |
7522 | if (VAR_P (r) | |
72124f48 JJ |
7523 | && (TREE_STATIC (r) |
7524 | || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r))) | |
8892d532 | 7525 | /* Allow __FUNCTION__ etc. */ |
32d16fe9 JJ |
7526 | && !DECL_ARTIFICIAL (r) |
7527 | && !decl_constant_var_p (r)) | |
8892d532 | 7528 | { |
8892d532 JJ |
7529 | if (!ctx->quiet) |
7530 | { | |
7531 | if (CP_DECL_THREAD_LOCAL_P (r)) | |
72124f48 | 7532 | error_at (loc, "control passes through definition of %qD " |
8892d532 JJ |
7533 | "with thread storage duration", r); |
7534 | else | |
72124f48 | 7535 | error_at (loc, "control passes through definition of %qD " |
8892d532 JJ |
7536 | "with static storage duration", r); |
7537 | } | |
7538 | *non_constant_p = true; | |
7539 | break; | |
7540 | } | |
7541 | ||
9d5730de JJ |
7542 | /* make_rtl_for_nonlocal_decl could have deferred emission of |
7543 | a local static var, but if it appears in a statement expression | |
7544 | which is constant expression evaluated to e.g. just the address | |
7545 | of the variable, its DECL_EXPR will never be seen during | |
7546 | gimple lowering's record_vars_into as the statement expression | |
7547 | will not be in the IL at all. */ | |
7548 | if (VAR_P (r) | |
7549 | && TREE_STATIC (r) | |
7550 | && !DECL_REALLY_EXTERN (r) | |
7551 | && DECL_FUNCTION_SCOPE_P (r) | |
7552 | && !var_in_maybe_constexpr_fn (r) | |
7553 | && decl_constant_var_p (r)) | |
7554 | { | |
7555 | varpool_node *node = varpool_node::get (r); | |
7556 | if (node == NULL || !node->definition) | |
7557 | rest_of_decl_compilation (r, 0, at_eof); | |
7558 | } | |
7559 | ||
60813a46 JM |
7560 | if (AGGREGATE_TYPE_P (TREE_TYPE (r)) |
7561 | || VECTOR_TYPE_P (TREE_TYPE (r))) | |
7562 | { | |
7563 | new_ctx = *ctx; | |
7564 | new_ctx.object = r; | |
7565 | new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL); | |
e8c48716 | 7566 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; |
e6a29aab | 7567 | ctx->global->put_value (r, new_ctx.ctor); |
60813a46 JM |
7568 | ctx = &new_ctx; |
7569 | } | |
7570 | ||
7571 | if (tree init = DECL_INITIAL (r)) | |
7572 | { | |
72f76540 | 7573 | init = cxx_eval_constant_expression (ctx, init, vc_prvalue, |
5a804683 | 7574 | non_constant_p, overflow_p); |
7f0e23e9 | 7575 | /* Don't share a CONSTRUCTOR that might be changed. */ |
0146e25f | 7576 | init = unshare_constructor (init); |
04e1749c MP |
7577 | /* Remember that a constant object's constructor has already |
7578 | run. */ | |
7579 | if (CLASS_TYPE_P (TREE_TYPE (r)) | |
7580 | && CP_TYPE_CONST_P (TREE_TYPE (r))) | |
7581 | TREE_READONLY (init) = true; | |
e6a29aab | 7582 | ctx->global->put_value (r, init); |
60813a46 JM |
7583 | } |
7584 | else if (ctx == &new_ctx) | |
7585 | /* We gave it a CONSTRUCTOR above. */; | |
7586 | else | |
e6a29aab | 7587 | ctx->global->put_value (r, NULL_TREE); |
60813a46 JM |
7588 | } |
7589 | break; | |
7590 | ||
2d76680f | 7591 | case TARGET_EXPR: |
595f1b12 JM |
7592 | { |
7593 | tree type = TREE_TYPE (t); | |
7594 | ||
7595 | if (!literal_type_p (type)) | |
7596 | { | |
7597 | if (!ctx->quiet) | |
7598 | { | |
7599 | auto_diagnostic_group d; | |
7600 | error ("temporary of non-literal type %qT in a " | |
7601 | "constant expression", type); | |
7602 | explain_non_literal_class (type); | |
7603 | } | |
7604 | *non_constant_p = true; | |
7605 | break; | |
7606 | } | |
7607 | gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t)); | |
7608 | /* Avoid evaluating a TARGET_EXPR more than once. */ | |
7609 | tree slot = TARGET_EXPR_SLOT (t); | |
e6a29aab | 7610 | if (tree v = ctx->global->get_value (slot)) |
595f1b12 JM |
7611 | { |
7612 | if (lval) | |
7613 | return slot; | |
e6a29aab | 7614 | r = v; |
595f1b12 JM |
7615 | break; |
7616 | } | |
595f1b12 JM |
7617 | if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))) |
7618 | { | |
7619 | /* We're being expanded without an explicit target, so start | |
7620 | initializing a new object; expansion with an explicit target | |
7621 | strips the TARGET_EXPR before we get here. */ | |
7622 | new_ctx = *ctx; | |
49a86fce PP |
7623 | /* Link CTX to NEW_CTX so that lookup_placeholder can resolve |
7624 | any PLACEHOLDER_EXPR within the initializer that refers to the | |
7625 | former object under construction. */ | |
7626 | new_ctx.parent = ctx; | |
595f1b12 JM |
7627 | new_ctx.ctor = build_constructor (type, NULL); |
7628 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; | |
7629 | new_ctx.object = slot; | |
e6a29aab | 7630 | ctx->global->put_value (new_ctx.object, new_ctx.ctor); |
595f1b12 JM |
7631 | ctx = &new_ctx; |
7632 | } | |
72f76540 | 7633 | /* Pass vc_prvalue because this indicates |
595f1b12 | 7634 | initialization of a temporary. */ |
72f76540 | 7635 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_prvalue, |
595f1b12 JM |
7636 | non_constant_p, overflow_p); |
7637 | if (*non_constant_p) | |
ee1de08d | 7638 | break; |
13669741 JM |
7639 | /* If the initializer is complex, evaluate it to initialize slot. */ |
7640 | bool is_complex = target_expr_needs_replace (t); | |
7641 | if (!is_complex) | |
7642 | { | |
7643 | r = unshare_constructor (r); | |
7644 | /* Adjust the type of the result to the type of the temporary. */ | |
7645 | r = adjust_temp_type (type, r); | |
7646 | ctx->global->put_value (slot, r); | |
7647 | } | |
595f1b12 JM |
7648 | if (TARGET_EXPR_CLEANUP (t) && !CLEANUP_EH_ONLY (t)) |
7649 | ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t)); | |
595f1b12 JM |
7650 | if (ctx->save_exprs) |
7651 | ctx->save_exprs->safe_push (slot); | |
7652 | if (lval) | |
7653 | return slot; | |
13669741 JM |
7654 | if (is_complex) |
7655 | r = ctx->global->get_value (slot); | |
595f1b12 | 7656 | } |
2d76680f PC |
7657 | break; |
7658 | ||
60813a46 | 7659 | case INIT_EXPR: |
60813a46 | 7660 | case MODIFY_EXPR: |
4b390698 | 7661 | gcc_assert (jump_target == NULL || *jump_target == NULL_TREE); |
92a596e8 | 7662 | r = cxx_eval_store_expression (ctx, t, lval, |
60813a46 JM |
7663 | non_constant_p, overflow_p); |
7664 | break; | |
7665 | ||
2d76680f | 7666 | case SCOPE_REF: |
3e605b20 | 7667 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
92a596e8 | 7668 | lval, |
5a804683 | 7669 | non_constant_p, overflow_p); |
2d76680f PC |
7670 | break; |
7671 | ||
7672 | case RETURN_EXPR: | |
75e0295b MP |
7673 | if (TREE_OPERAND (t, 0) != NULL_TREE) |
7674 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), | |
7675 | lval, | |
7676 | non_constant_p, overflow_p); | |
43574e4f JJ |
7677 | /* FALLTHRU */ |
7678 | case BREAK_STMT: | |
7679 | case CONTINUE_STMT: | |
019e0ae8 MP |
7680 | if (jump_target) |
7681 | *jump_target = t; | |
7682 | else | |
7683 | { | |
7684 | /* Can happen with ({ return true; }) && false; passed to | |
7685 | maybe_constant_value. There is nothing to jump over in this | |
7686 | case, and the bug will be diagnosed later. */ | |
7687 | gcc_assert (ctx->quiet); | |
7688 | *non_constant_p = true; | |
7689 | } | |
56632b27 JM |
7690 | break; |
7691 | ||
cabaf50a JM |
7692 | case SAVE_EXPR: |
7693 | /* Avoid evaluating a SAVE_EXPR more than once. */ | |
e6a29aab JM |
7694 | if (tree v = ctx->global->get_value (t)) |
7695 | r = v; | |
cabaf50a JM |
7696 | else |
7697 | { | |
72f76540 | 7698 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue, |
cabaf50a | 7699 | non_constant_p, overflow_p); |
0e0ffbfc JJ |
7700 | if (*non_constant_p) |
7701 | break; | |
e6a29aab | 7702 | ctx->global->put_value (t, r); |
39dce2b7 | 7703 | if (ctx->save_exprs) |
0ee28590 | 7704 | ctx->save_exprs->safe_push (t); |
cabaf50a JM |
7705 | } |
7706 | break; | |
7707 | ||
2d76680f | 7708 | case TRY_CATCH_EXPR: |
ee1de08d JJ |
7709 | if (TREE_OPERAND (t, 0) == NULL_TREE) |
7710 | { | |
7711 | r = void_node; | |
7712 | break; | |
7713 | } | |
7714 | /* FALLTHRU */ | |
7715 | case NON_LVALUE_EXPR: | |
769430b2 | 7716 | case TRY_BLOCK: |
2d76680f | 7717 | case MUST_NOT_THROW_EXPR: |
60813a46 JM |
7718 | case EXPR_STMT: |
7719 | case EH_SPEC_BLOCK: | |
3e605b20 | 7720 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
92a596e8 | 7721 | lval, |
56632b27 JM |
7722 | non_constant_p, overflow_p, |
7723 | jump_target); | |
2d76680f PC |
7724 | break; |
7725 | ||
ee1de08d JJ |
7726 | case CLEANUP_POINT_EXPR: |
7727 | { | |
7728 | auto_vec<tree, 2> cleanups; | |
7729 | vec<tree> *prev_cleanups = ctx->global->cleanups; | |
7730 | ctx->global->cleanups = &cleanups; | |
9fdbd7d6 NS |
7731 | |
7732 | auto_vec<tree, 10> save_exprs; | |
7733 | constexpr_ctx new_ctx = *ctx; | |
7734 | new_ctx.save_exprs = &save_exprs; | |
7735 | ||
7736 | r = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 0), | |
ee1de08d JJ |
7737 | lval, |
7738 | non_constant_p, overflow_p, | |
7739 | jump_target); | |
9fdbd7d6 | 7740 | |
ee1de08d JJ |
7741 | ctx->global->cleanups = prev_cleanups; |
7742 | unsigned int i; | |
7743 | tree cleanup; | |
7744 | /* Evaluate the cleanups. */ | |
7745 | FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup) | |
9fdbd7d6 | 7746 | cxx_eval_constant_expression (&new_ctx, cleanup, vc_discard, |
fc531c2e | 7747 | non_constant_p, overflow_p); |
9fdbd7d6 NS |
7748 | |
7749 | /* Forget SAVE_EXPRs and TARGET_EXPRs created by this | |
7750 | full-expression. */ | |
7751 | for (tree save_expr : save_exprs) | |
90bc2d09 | 7752 | destroy_value_checked (ctx, save_expr, non_constant_p); |
ee1de08d JJ |
7753 | } |
7754 | break; | |
7755 | ||
769430b2 JM |
7756 | case TRY_FINALLY_EXPR: |
7757 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, | |
7758 | non_constant_p, overflow_p, | |
7759 | jump_target); | |
7760 | if (!*non_constant_p) | |
7761 | /* Also evaluate the cleanup. */ | |
72f76540 | 7762 | cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard, |
fc531c2e | 7763 | non_constant_p, overflow_p); |
769430b2 JM |
7764 | break; |
7765 | ||
43574e4f | 7766 | case CLEANUP_STMT: |
fc531c2e JJ |
7767 | r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval, |
7768 | non_constant_p, overflow_p, | |
7769 | jump_target); | |
7770 | if (!CLEANUP_EH_ONLY (t) && !*non_constant_p) | |
7771 | { | |
7772 | iloc_sentinel ils (loc); | |
7773 | /* Also evaluate the cleanup. */ | |
72f76540 | 7774 | cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), vc_discard, |
fc531c2e JJ |
7775 | non_constant_p, overflow_p); |
7776 | } | |
43574e4f JJ |
7777 | break; |
7778 | ||
2d76680f PC |
7779 | /* These differ from cxx_eval_unary_expression in that this doesn't |
7780 | check for a constant operand or result; an address can be | |
7781 | constant without its operand being, and vice versa. */ | |
cda0a029 | 7782 | case MEM_REF: |
2d76680f | 7783 | case INDIRECT_REF: |
92a596e8 | 7784 | r = cxx_eval_indirect_ref (ctx, t, lval, |
2d76680f PC |
7785 | non_constant_p, overflow_p); |
7786 | break; | |
7787 | ||
7788 | case ADDR_EXPR: | |
7789 | { | |
7790 | tree oldop = TREE_OPERAND (t, 0); | |
72f76540 | 7791 | tree op = cxx_eval_constant_expression (ctx, oldop, vc_glvalue, |
5a804683 | 7792 | non_constant_p, overflow_p); |
2d76680f PC |
7793 | /* Don't VERIFY_CONSTANT here. */ |
7794 | if (*non_constant_p) | |
7795 | return t; | |
f2acb8ad | 7796 | gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR); |
2d76680f PC |
7797 | /* This function does more aggressive folding than fold itself. */ |
7798 | r = build_fold_addr_expr_with_type (op, TREE_TYPE (t)); | |
7799 | if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop) | |
7a649ef5 JM |
7800 | { |
7801 | ggc_free (r); | |
7802 | return t; | |
7803 | } | |
2d76680f PC |
7804 | break; |
7805 | } | |
7806 | ||
7807 | case REALPART_EXPR: | |
7808 | case IMAGPART_EXPR: | |
87713c6a JJ |
7809 | if (lval) |
7810 | { | |
7811 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, | |
7812 | non_constant_p, overflow_p); | |
7813 | if (r == error_mark_node) | |
7814 | ; | |
72f76540 | 7815 | else if (r == TREE_OPERAND (t, 0) || lval == vc_discard) |
87713c6a JJ |
7816 | r = t; |
7817 | else | |
7818 | r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r); | |
7819 | break; | |
7820 | } | |
7821 | /* FALLTHRU */ | |
2d76680f PC |
7822 | case CONJ_EXPR: |
7823 | case FIX_TRUNC_EXPR: | |
7824 | case FLOAT_EXPR: | |
7825 | case NEGATE_EXPR: | |
7826 | case ABS_EXPR: | |
e28fadbc | 7827 | case ABSU_EXPR: |
2d76680f PC |
7828 | case BIT_NOT_EXPR: |
7829 | case TRUTH_NOT_EXPR: | |
7830 | case FIXED_CONVERT_EXPR: | |
92a596e8 | 7831 | r = cxx_eval_unary_expression (ctx, t, lval, |
2d76680f PC |
7832 | non_constant_p, overflow_p); |
7833 | break; | |
7834 | ||
7835 | case SIZEOF_EXPR: | |
99e764a2 MP |
7836 | r = fold_sizeof_expr (t); |
7837 | /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR, | |
7838 | which could lead to an infinite recursion. */ | |
7839 | if (TREE_CODE (r) != SIZEOF_EXPR) | |
7840 | r = cxx_eval_constant_expression (ctx, r, lval, | |
7841 | non_constant_p, overflow_p, | |
7842 | jump_target); | |
7843 | else | |
7844 | { | |
7845 | *non_constant_p = true; | |
7846 | gcc_assert (ctx->quiet); | |
7847 | } | |
7848 | ||
2d76680f PC |
7849 | break; |
7850 | ||
7851 | case COMPOUND_EXPR: | |
7852 | { | |
7853 | /* check_return_expr sometimes wraps a TARGET_EXPR in a | |
7854 | COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR | |
7855 | introduced by build_call_a. */ | |
7856 | tree op0 = TREE_OPERAND (t, 0); | |
7857 | tree op1 = TREE_OPERAND (t, 1); | |
7858 | STRIP_NOPS (op1); | |
7859 | if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) | |
7860 | || TREE_CODE (op1) == EMPTY_CLASS_EXPR) | |
2b3ab879 | 7861 | r = cxx_eval_constant_expression (ctx, op0, |
92a596e8 | 7862 | lval, non_constant_p, overflow_p, |
56632b27 | 7863 | jump_target); |
2d76680f PC |
7864 | else |
7865 | { | |
7866 | /* Check that the LHS is constant and then discard it. */ | |
72f76540 JM |
7867 | cxx_eval_constant_expression (ctx, op0, vc_discard, |
7868 | non_constant_p, overflow_p, | |
56632b27 | 7869 | jump_target); |
06ec22b7 JM |
7870 | if (*non_constant_p) |
7871 | return t; | |
2d76680f | 7872 | op1 = TREE_OPERAND (t, 1); |
2b3ab879 | 7873 | r = cxx_eval_constant_expression (ctx, op1, |
92a596e8 | 7874 | lval, non_constant_p, overflow_p, |
56632b27 | 7875 | jump_target); |
2d76680f PC |
7876 | } |
7877 | } | |
7878 | break; | |
7879 | ||
7880 | case POINTER_PLUS_EXPR: | |
1af4ebf5 | 7881 | case POINTER_DIFF_EXPR: |
2d76680f PC |
7882 | case PLUS_EXPR: |
7883 | case MINUS_EXPR: | |
7884 | case MULT_EXPR: | |
7885 | case TRUNC_DIV_EXPR: | |
7886 | case CEIL_DIV_EXPR: | |
7887 | case FLOOR_DIV_EXPR: | |
7888 | case ROUND_DIV_EXPR: | |
7889 | case TRUNC_MOD_EXPR: | |
7890 | case CEIL_MOD_EXPR: | |
7891 | case ROUND_MOD_EXPR: | |
7892 | case RDIV_EXPR: | |
7893 | case EXACT_DIV_EXPR: | |
7894 | case MIN_EXPR: | |
7895 | case MAX_EXPR: | |
7896 | case LSHIFT_EXPR: | |
7897 | case RSHIFT_EXPR: | |
81fa6d73 JJ |
7898 | case LROTATE_EXPR: |
7899 | case RROTATE_EXPR: | |
2d76680f PC |
7900 | case BIT_IOR_EXPR: |
7901 | case BIT_XOR_EXPR: | |
7902 | case BIT_AND_EXPR: | |
7903 | case TRUTH_XOR_EXPR: | |
7904 | case LT_EXPR: | |
7905 | case LE_EXPR: | |
7906 | case GT_EXPR: | |
7907 | case GE_EXPR: | |
7908 | case EQ_EXPR: | |
7909 | case NE_EXPR: | |
b7689b96 | 7910 | case SPACESHIP_EXPR: |
2d76680f PC |
7911 | case UNORDERED_EXPR: |
7912 | case ORDERED_EXPR: | |
7913 | case UNLT_EXPR: | |
7914 | case UNLE_EXPR: | |
7915 | case UNGT_EXPR: | |
7916 | case UNGE_EXPR: | |
7917 | case UNEQ_EXPR: | |
7918 | case LTGT_EXPR: | |
7919 | case RANGE_EXPR: | |
7920 | case COMPLEX_EXPR: | |
92a596e8 | 7921 | r = cxx_eval_binary_expression (ctx, t, lval, |
2d76680f PC |
7922 | non_constant_p, overflow_p); |
7923 | break; | |
7924 | ||
7925 | /* fold can introduce non-IF versions of these; still treat them as | |
7926 | short-circuiting. */ | |
7927 | case TRUTH_AND_EXPR: | |
7928 | case TRUTH_ANDIF_EXPR: | |
3e605b20 | 7929 | r = cxx_eval_logical_expression (ctx, t, boolean_false_node, |
2d76680f | 7930 | boolean_true_node, |
2d76680f PC |
7931 | non_constant_p, overflow_p); |
7932 | break; | |
7933 | ||
7934 | case TRUTH_OR_EXPR: | |
7935 | case TRUTH_ORIF_EXPR: | |
3e605b20 | 7936 | r = cxx_eval_logical_expression (ctx, t, boolean_true_node, |
2d76680f | 7937 | boolean_false_node, |
2d76680f PC |
7938 | non_constant_p, overflow_p); |
7939 | break; | |
7940 | ||
7941 | case ARRAY_REF: | |
92a596e8 | 7942 | r = cxx_eval_array_reference (ctx, t, lval, |
2d76680f PC |
7943 | non_constant_p, overflow_p); |
7944 | break; | |
7945 | ||
7946 | case COMPONENT_REF: | |
7947 | if (is_overloaded_fn (t)) | |
7948 | { | |
7949 | /* We can only get here in checking mode via | |
7950 | build_non_dependent_expr, because any expression that | |
7951 | calls or takes the address of the function will have | |
7952 | pulled a FUNCTION_DECL out of the COMPONENT_REF. */ | |
2b3ab879 | 7953 | gcc_checking_assert (ctx->quiet || errorcount); |
2d76680f PC |
7954 | *non_constant_p = true; |
7955 | return t; | |
7956 | } | |
92a596e8 | 7957 | r = cxx_eval_component_reference (ctx, t, lval, |
2d76680f PC |
7958 | non_constant_p, overflow_p); |
7959 | break; | |
7960 | ||
7961 | case BIT_FIELD_REF: | |
92a596e8 | 7962 | r = cxx_eval_bit_field_ref (ctx, t, lval, |
2d76680f PC |
7963 | non_constant_p, overflow_p); |
7964 | break; | |
7965 | ||
7966 | case COND_EXPR: | |
43574e4f | 7967 | case IF_STMT: |
4b390698 JJ |
7968 | if (jump_target && *jump_target) |
7969 | { | |
e9fa2f6d | 7970 | tree orig_jump = *jump_target; |
43574e4f JJ |
7971 | tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1)) |
7972 | ? TREE_OPERAND (t, 1) : void_node); | |
4b390698 JJ |
7973 | /* When jumping to a label, the label might be either in the |
7974 | then or else blocks, so process then block first in skipping | |
7975 | mode first, and if we are still in the skipping mode at its end, | |
7976 | process the else block too. */ | |
43574e4f JJ |
7977 | r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, |
7978 | overflow_p, jump_target); | |
e9fa2f6d MP |
7979 | /* It's possible that we found the label in the then block. But |
7980 | it could have been followed by another jumping statement, e.g. | |
7981 | say we're looking for case 1: | |
7982 | if (cond) | |
7983 | { | |
7984 | // skipped statements | |
7985 | case 1:; // clears up *jump_target | |
7986 | return 1; // and sets it to a RETURN_EXPR | |
7987 | } | |
7988 | else { ... } | |
7989 | in which case we need not go looking to the else block. | |
7990 | (goto is not allowed in a constexpr function.) */ | |
7991 | if (*jump_target == orig_jump) | |
43574e4f JJ |
7992 | { |
7993 | arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2)) | |
7994 | ? TREE_OPERAND (t, 2) : void_node); | |
7995 | r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, | |
7996 | overflow_p, jump_target); | |
7997 | } | |
4b390698 JJ |
7998 | break; |
7999 | } | |
92a596e8 | 8000 | r = cxx_eval_conditional_expression (ctx, t, lval, |
56632b27 JM |
8001 | non_constant_p, overflow_p, |
8002 | jump_target); | |
2d76680f | 8003 | break; |
f370e36d JJ |
8004 | case VEC_COND_EXPR: |
8005 | r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p, | |
8006 | overflow_p); | |
8007 | break; | |
2d76680f PC |
8008 | |
8009 | case CONSTRUCTOR: | |
35d87f01 | 8010 | if (TREE_CONSTANT (t) && reduced_constant_expression_p (t)) |
2d63bc39 | 8011 | { |
2cc41601 | 8012 | /* Don't re-process a constant CONSTRUCTOR. */ |
ac9ec198 | 8013 | verify_constructor_flags (t); |
2d63bc39 | 8014 | if (TREE_CONSTANT (t)) |
2cc41601 | 8015 | return t; |
2d63bc39 | 8016 | } |
92a596e8 | 8017 | r = cxx_eval_bare_aggregate (ctx, t, lval, |
2d76680f PC |
8018 | non_constant_p, overflow_p); |
8019 | break; | |
8020 | ||
8021 | case VEC_INIT_EXPR: | |
8022 | /* We can get this in a defaulted constructor for a class with a | |
8023 | non-static data member of array type. Either the initializer will | |
8024 | be NULL, meaning default-initialization, or it will be an lvalue | |
8025 | or xvalue of the same type, meaning direct-initialization from the | |
8026 | corresponding member. */ | |
92a596e8 | 8027 | r = cxx_eval_vec_init (ctx, t, lval, |
2d76680f PC |
8028 | non_constant_p, overflow_p); |
8029 | break; | |
8030 | ||
2d76680f | 8031 | case VEC_PERM_EXPR: |
92a596e8 | 8032 | r = cxx_eval_trinary_expression (ctx, t, lval, |
2d76680f PC |
8033 | non_constant_p, overflow_p); |
8034 | break; | |
efb7c510 MK |
8035 | |
8036 | case PAREN_EXPR: | |
8037 | gcc_assert (!REF_PARENTHESIZED_P (t)); | |
8038 | /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in | |
8039 | constant expressions since it's unaffected by -fassociative-math. */ | |
8040 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, | |
8041 | non_constant_p, overflow_p); | |
8042 | break; | |
2d76680f | 8043 | |
7d75ea04 JJ |
8044 | case NOP_EXPR: |
8045 | if (REINTERPRET_CAST_P (t)) | |
8046 | { | |
8047 | if (!ctx->quiet) | |
6821245b | 8048 | error_at (loc, |
a9c697b8 | 8049 | "%<reinterpret_cast%> is not a constant expression"); |
7d75ea04 JJ |
8050 | *non_constant_p = true; |
8051 | return t; | |
8052 | } | |
8053 | /* FALLTHROUGH. */ | |
2d76680f PC |
8054 | case CONVERT_EXPR: |
8055 | case VIEW_CONVERT_EXPR: | |
cda0a029 | 8056 | case UNARY_PLUS_EXPR: |
2d76680f PC |
8057 | { |
8058 | tree oldop = TREE_OPERAND (t, 0); | |
cda0a029 | 8059 | |
3e605b20 | 8060 | tree op = cxx_eval_constant_expression (ctx, oldop, |
92a596e8 | 8061 | lval, |
5a804683 | 8062 | non_constant_p, overflow_p); |
2d76680f PC |
8063 | if (*non_constant_p) |
8064 | return t; | |
dcdbc004 | 8065 | tree type = TREE_TYPE (t); |
02a8575c JM |
8066 | |
8067 | if (VOID_TYPE_P (type)) | |
8068 | return void_node; | |
8069 | ||
eeb54a14 JJ |
8070 | if (TREE_CODE (t) == CONVERT_EXPR |
8071 | && ARITHMETIC_TYPE_P (type) | |
82bb6673 | 8072 | && INDIRECT_TYPE_P (TREE_TYPE (op)) |
3a0bc47c | 8073 | && ctx->manifestly_const_eval == mce_true) |
eeb54a14 JJ |
8074 | { |
8075 | if (!ctx->quiet) | |
8076 | error_at (loc, | |
8077 | "conversion from pointer type %qT to arithmetic type " | |
8078 | "%qT in a constant expression", TREE_TYPE (op), type); | |
8079 | *non_constant_p = true; | |
8080 | return t; | |
8081 | } | |
8082 | ||
2ffc2645 MP |
8083 | /* [expr.const]: a conversion from type cv void* to a pointer-to-object |
8084 | type cannot be part of a core constant expression as a resolution to | |
8085 | DR 1312. */ | |
6252e35c | 8086 | if (TYPE_PTROB_P (type) |
2ffc2645 MP |
8087 | && TYPE_PTR_P (TREE_TYPE (op)) |
8088 | && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op))) | |
b69ee500 NS |
8089 | /* Inside a call to std::construct_at, |
8090 | std::allocator<T>::{,de}allocate, or | |
8091 | std::source_location::current, we permit casting from void* | |
2ffc2645 MP |
8092 | because that is compiler-generated code. */ |
8093 | && !is_std_construct_at (ctx->call) | |
b69ee500 NS |
8094 | && !is_std_allocator_allocate (ctx->call) |
8095 | && !is_std_source_location_current (ctx->call)) | |
2ffc2645 MP |
8096 | { |
8097 | /* Likewise, don't error when casting from void* when OP is | |
8098 | &heap uninit and similar. */ | |
8099 | tree sop = tree_strip_nop_conversions (op); | |
b69ee500 NS |
8100 | tree decl = NULL_TREE; |
8101 | if (TREE_CODE (sop) == ADDR_EXPR) | |
8102 | decl = TREE_OPERAND (sop, 0); | |
8103 | if (decl | |
8104 | && VAR_P (decl) | |
8105 | && DECL_ARTIFICIAL (decl) | |
8106 | && (DECL_NAME (decl) == heap_identifier | |
8107 | || DECL_NAME (decl) == heap_uninit_identifier | |
8108 | || DECL_NAME (decl) == heap_vec_identifier | |
8109 | || DECL_NAME (decl) == heap_vec_uninit_identifier)) | |
2ffc2645 | 8110 | /* OK */; |
8d344146 JM |
8111 | /* P2738 (C++26): a conversion from a prvalue P of type "pointer to |
8112 | cv void" to a pointer-to-object type T unless P points to an | |
8113 | object whose type is similar to T. */ | |
b69ee500 | 8114 | else if (cxx_dialect > cxx23) |
8d344146 | 8115 | { |
b69ee500 NS |
8116 | r = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), sop); |
8117 | if (r) | |
8118 | { | |
8119 | r = build1 (ADDR_EXPR, type, r); | |
8120 | break; | |
8121 | } | |
8122 | if (!ctx->quiet) | |
8123 | { | |
8124 | if (TREE_CODE (sop) == ADDR_EXPR) | |
8125 | { | |
8126 | auto_diagnostic_group d; | |
8127 | error_at (loc, "cast from %qT is not allowed in a " | |
8128 | "constant expression because " | |
8129 | "pointed-to type %qT is not similar to %qT", | |
8130 | TREE_TYPE (op), TREE_TYPE (TREE_TYPE (sop)), | |
8131 | TREE_TYPE (type)); | |
8132 | tree obj = build_fold_indirect_ref (sop); | |
8133 | inform (DECL_SOURCE_LOCATION (obj), | |
8134 | "pointed-to object declared here"); | |
8135 | } | |
8136 | else | |
8137 | { | |
8138 | gcc_assert (integer_zerop (sop)); | |
8139 | error_at (loc, "cast from %qT is not allowed in a " | |
8140 | "constant expression because " | |
8141 | "%qE does not point to an object", | |
8142 | TREE_TYPE (op), oldop); | |
8143 | } | |
8144 | } | |
8145 | *non_constant_p = true; | |
8146 | return t; | |
8d344146 | 8147 | } |
2ffc2645 MP |
8148 | else |
8149 | { | |
8150 | if (!ctx->quiet) | |
b69ee500 NS |
8151 | error_at (loc, "cast from %qT is not allowed in a " |
8152 | "constant expression before C++26", | |
2ffc2645 MP |
8153 | TREE_TYPE (op)); |
8154 | *non_constant_p = true; | |
8155 | return t; | |
8156 | } | |
8157 | } | |
8158 | ||
8e007055 | 8159 | if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type)) |
de81e062 MP |
8160 | { |
8161 | op = cplus_expand_constant (op); | |
8162 | if (TREE_CODE (op) == PTRMEM_CST) | |
8163 | { | |
8164 | if (!ctx->quiet) | |
8165 | error_at (loc, "%qE is not a constant expression when the " | |
8166 | "class %qT is still incomplete", op, | |
8167 | PTRMEM_CST_CLASS (op)); | |
8168 | *non_constant_p = true; | |
8169 | return t; | |
8170 | } | |
8171 | } | |
7d75ea04 | 8172 | |
05bf54c3 MP |
8173 | if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR) |
8174 | { | |
7d75ea04 JJ |
8175 | if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op)) |
8176 | && !can_convert_qual (type, op)) | |
8177 | op = cplus_expand_constant (op); | |
8178 | return cp_fold_convert (type, op); | |
05bf54c3 | 8179 | } |
8bada5cd | 8180 | |
71a93b08 | 8181 | if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST) |
2d76680f | 8182 | { |
8bada5cd MS |
8183 | if (integer_zerop (op)) |
8184 | { | |
9f613f06 | 8185 | if (TYPE_REF_P (type)) |
8bada5cd MS |
8186 | { |
8187 | if (!ctx->quiet) | |
2ffc2645 | 8188 | error_at (loc, "dereferencing a null pointer"); |
8bada5cd MS |
8189 | *non_constant_p = true; |
8190 | return t; | |
8191 | } | |
8bada5cd MS |
8192 | } |
8193 | else | |
8194 | { | |
8195 | /* This detects for example: | |
8196 | reinterpret_cast<void*>(sizeof 0) | |
8197 | */ | |
8198 | if (!ctx->quiet) | |
6821245b | 8199 | error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not " |
64d6d399 | 8200 | "a constant expression", |
8bada5cd MS |
8201 | type, op); |
8202 | *non_constant_p = true; | |
8203 | return t; | |
8204 | } | |
2d76680f | 8205 | } |
7d75ea04 | 8206 | |
8e007055 JJ |
8207 | if (INDIRECT_TYPE_P (type) |
8208 | && TREE_CODE (op) == NOP_EXPR | |
8209 | && TREE_TYPE (op) == ptr_type_node | |
8210 | && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR | |
8211 | && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0)) | |
815baade JJ |
8212 | && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), |
8213 | 0)) == heap_uninit_identifier | |
8214 | || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), | |
8215 | 0)) == heap_vec_uninit_identifier)) | |
8e007055 JJ |
8216 | { |
8217 | tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0); | |
8218 | tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var)); | |
8219 | tree elt_type = TREE_TYPE (type); | |
8220 | tree cookie_size = NULL_TREE; | |
0a0c2c3f | 8221 | tree arg_size = NULL_TREE; |
8e007055 JJ |
8222 | if (TREE_CODE (elt_type) == RECORD_TYPE |
8223 | && TYPE_NAME (elt_type) == heap_identifier) | |
8224 | { | |
8225 | tree fld1 = TYPE_FIELDS (elt_type); | |
8226 | tree fld2 = DECL_CHAIN (fld1); | |
8227 | elt_type = TREE_TYPE (TREE_TYPE (fld2)); | |
8228 | cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1)); | |
8229 | } | |
815baade JJ |
8230 | DECL_NAME (var) |
8231 | = (DECL_NAME (var) == heap_uninit_identifier | |
8232 | ? heap_identifier : heap_vec_identifier); | |
0a0c2c3f JJ |
8233 | /* For zero sized elt_type, try to recover how many outer_nelts |
8234 | it should have. */ | |
8235 | if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size) | |
8236 | : integer_zerop (var_size)) | |
8237 | && !int_size_in_bytes (elt_type) | |
8238 | && TREE_CODE (oldop) == CALL_EXPR | |
8239 | && call_expr_nargs (oldop) >= 1) | |
8240 | if (tree fun = get_function_named_in_call (oldop)) | |
8241 | if (cxx_replaceable_global_alloc_fn (fun) | |
8242 | && IDENTIFIER_NEW_OP_P (DECL_NAME (fun))) | |
8243 | arg_size = CALL_EXPR_ARG (oldop, 0); | |
8e007055 | 8244 | TREE_TYPE (var) |
0a0c2c3f JJ |
8245 | = build_new_constexpr_heap_type (ctx, elt_type, cookie_size, |
8246 | var_size, arg_size, | |
8247 | non_constant_p, overflow_p); | |
8e007055 JJ |
8248 | TREE_TYPE (TREE_OPERAND (op, 0)) |
8249 | = build_pointer_type (TREE_TYPE (var)); | |
8250 | } | |
8251 | ||
cda0a029 | 8252 | if (op == oldop && tcode != UNARY_PLUS_EXPR) |
2d76680f PC |
8253 | /* We didn't fold at the top so we could check for ptr-int |
8254 | conversion. */ | |
8255 | return fold (t); | |
7d75ea04 | 8256 | |
02a8575c JM |
8257 | tree sop; |
8258 | ||
e4511ca2 JM |
8259 | /* Handle an array's bounds having been deduced after we built |
8260 | the wrapping expression. */ | |
8261 | if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op))) | |
8262 | r = op; | |
02a8575c JM |
8263 | else if (sop = tree_strip_nop_conversions (op), |
8264 | sop != op && (same_type_ignoring_tlq_and_bounds_p | |
8265 | (type, TREE_TYPE (sop)))) | |
8266 | r = sop; | |
e4511ca2 | 8267 | else if (tcode == UNARY_PLUS_EXPR) |
cda0a029 JM |
8268 | r = fold_convert (TREE_TYPE (t), op); |
8269 | else | |
8270 | r = fold_build1 (tcode, type, op); | |
7d75ea04 | 8271 | |
2d76680f PC |
8272 | /* Conversion of an out-of-range value has implementation-defined |
8273 | behavior; the language considers it different from arithmetic | |
8274 | overflow, which is undefined. */ | |
8275 | if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op)) | |
8276 | TREE_OVERFLOW (r) = false; | |
8277 | } | |
8278 | break; | |
8279 | ||
98e34113 JJ |
8280 | case EXCESS_PRECISION_EXPR: |
8281 | { | |
8282 | tree oldop = TREE_OPERAND (t, 0); | |
8283 | ||
8284 | tree op = cxx_eval_constant_expression (ctx, oldop, | |
8285 | lval, | |
8286 | non_constant_p, overflow_p); | |
8287 | if (*non_constant_p) | |
8288 | return t; | |
8289 | r = fold_convert (TREE_TYPE (t), op); | |
8290 | break; | |
8291 | } | |
8292 | ||
2d76680f | 8293 | case EMPTY_CLASS_EXPR: |
46fdced6 PP |
8294 | /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering |
8295 | it to an appropriate CONSTRUCTOR. */ | |
8296 | return build_constructor (TREE_TYPE (t), NULL); | |
2d76680f | 8297 | |
60813a46 | 8298 | case STATEMENT_LIST: |
56632b27 JM |
8299 | new_ctx = *ctx; |
8300 | new_ctx.ctor = new_ctx.object = NULL_TREE; | |
2b3ab879 | 8301 | return cxx_eval_statement_list (&new_ctx, t, |
56632b27 | 8302 | non_constant_p, overflow_p, jump_target); |
60813a46 JM |
8303 | |
8304 | case BIND_EXPR: | |
90bc2d09 NS |
8305 | /* Pre-emptively clear the vars declared by this BIND_EXPR from the value |
8306 | map, so that when checking whether they're already destroyed later we | |
8307 | don't get confused by remnants of previous calls. */ | |
8308 | for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl)) | |
8309 | ctx->global->clear_value (decl); | |
9fdbd7d6 NS |
8310 | r = cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t), |
8311 | lval, | |
8312 | non_constant_p, overflow_p, | |
8313 | jump_target); | |
8314 | for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl)) | |
90bc2d09 NS |
8315 | destroy_value_checked (ctx, decl, non_constant_p); |
8316 | break; | |
60813a46 | 8317 | |
2d76680f PC |
8318 | case PREINCREMENT_EXPR: |
8319 | case POSTINCREMENT_EXPR: | |
8320 | case PREDECREMENT_EXPR: | |
8321 | case POSTDECREMENT_EXPR: | |
2b3ab879 | 8322 | return cxx_eval_increment_expression (ctx, t, |
92a596e8 | 8323 | lval, non_constant_p, overflow_p); |
60813a46 JM |
8324 | |
8325 | case LAMBDA_EXPR: | |
2d76680f PC |
8326 | case NEW_EXPR: |
8327 | case VEC_NEW_EXPR: | |
8328 | case DELETE_EXPR: | |
8329 | case VEC_DELETE_EXPR: | |
8330 | case THROW_EXPR: | |
2d76680f PC |
8331 | case MODOP_EXPR: |
8332 | /* GCC internal stuff. */ | |
8333 | case VA_ARG_EXPR: | |
2d76680f | 8334 | case BASELINK: |
2d76680f | 8335 | case OFFSET_REF: |
2b3ab879 | 8336 | if (!ctx->quiet) |
6821245b | 8337 | error_at (loc, "expression %qE is not a constant expression", t); |
2d76680f PC |
8338 | *non_constant_p = true; |
8339 | break; | |
8340 | ||
bf8d8309 | 8341 | case OBJ_TYPE_REF: |
7ece3bd8 JM |
8342 | /* Virtual function lookup. We don't need to do anything fancy. */ |
8343 | return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t), | |
8344 | lval, non_constant_p, overflow_p); | |
bf8d8309 | 8345 | |
3e605b20 | 8346 | case PLACEHOLDER_EXPR: |
89262ec6 JM |
8347 | /* Use of the value or address of the current object. */ |
8348 | if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t))) | |
b599bf9d PP |
8349 | { |
8350 | if (TREE_CODE (ctor) == CONSTRUCTOR) | |
8351 | return ctor; | |
8352 | else | |
8353 | return cxx_eval_constant_expression (ctx, ctor, lval, | |
8354 | non_constant_p, overflow_p); | |
8355 | } | |
89262ec6 JM |
8356 | /* A placeholder without a referent. We can get here when |
8357 | checking whether NSDMIs are noexcept, or in massage_init_elt; | |
8358 | just say it's non-constant for now. */ | |
8359 | gcc_assert (ctx->quiet); | |
8360 | *non_constant_p = true; | |
3e605b20 JM |
8361 | break; |
8362 | ||
06ec22b7 JM |
8363 | case EXIT_EXPR: |
8364 | { | |
8365 | tree cond = TREE_OPERAND (t, 0); | |
72f76540 | 8366 | cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue, |
06ec22b7 JM |
8367 | non_constant_p, overflow_p); |
8368 | VERIFY_CONSTANT (cond); | |
8369 | if (integer_nonzerop (cond)) | |
8370 | *jump_target = t; | |
8371 | } | |
8372 | break; | |
8373 | ||
60813a46 | 8374 | case GOTO_EXPR: |
8892d532 | 8375 | if (breaks (&TREE_OPERAND (t, 0)) |
9c6a4bee | 8376 | || continues (&TREE_OPERAND (t, 0))) |
8892d532 JJ |
8377 | *jump_target = TREE_OPERAND (t, 0); |
8378 | else | |
8379 | { | |
8380 | gcc_assert (cxx_dialect >= cxx23); | |
8381 | if (!ctx->quiet) | |
8382 | error_at (loc, "%<goto%> is not a constant expression"); | |
8383 | *non_constant_p = true; | |
8384 | } | |
56632b27 JM |
8385 | break; |
8386 | ||
60813a46 | 8387 | case LOOP_EXPR: |
43574e4f JJ |
8388 | case DO_STMT: |
8389 | case WHILE_STMT: | |
8390 | case FOR_STMT: | |
2b3ab879 | 8391 | cxx_eval_loop_expr (ctx, t, |
56632b27 JM |
8392 | non_constant_p, overflow_p, jump_target); |
8393 | break; | |
8394 | ||
60813a46 | 8395 | case SWITCH_EXPR: |
43574e4f | 8396 | case SWITCH_STMT: |
2b3ab879 | 8397 | cxx_eval_switch_expr (ctx, t, |
56632b27 | 8398 | non_constant_p, overflow_p, jump_target); |
60813a46 JM |
8399 | break; |
8400 | ||
971e17ff AS |
8401 | case REQUIRES_EXPR: |
8402 | /* It's possible to get a requires-expression in a constant | |
8403 | expression. For example: | |
8404 | ||
8405 | template<typename T> concept bool C() { | |
8406 | return requires (T t) { t; }; | |
8407 | } | |
8408 | ||
8409 | template<typename T> requires !C<T>() void f(T); | |
8410 | ||
8411 | Normalization leaves f with the associated constraint | |
8412 | '!requires (T t) { ... }' which is not transformed into | |
8413 | a constraint. */ | |
8414 | if (!processing_template_decl) | |
662ef5b5 | 8415 | return evaluate_requires_expr (t); |
971e17ff AS |
8416 | else |
8417 | *non_constant_p = true; | |
8418 | return t; | |
8419 | ||
98e09245 | 8420 | case ANNOTATE_EXPR: |
98e09245 JJ |
8421 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
8422 | lval, | |
8423 | non_constant_p, overflow_p, | |
8424 | jump_target); | |
8425 | break; | |
8426 | ||
3664e317 JM |
8427 | case USING_STMT: |
8428 | r = void_node; | |
8429 | break; | |
8430 | ||
2efb237f JCI |
8431 | case ASSERTION_STMT: |
8432 | case PRECONDITION_STMT: | |
8433 | case POSTCONDITION_STMT: | |
8434 | { | |
8435 | contract_semantic semantic = get_contract_semantic (t); | |
8436 | if (semantic == CCS_IGNORE) | |
8437 | break; | |
8438 | ||
8439 | if (!cxx_eval_assert (ctx, CONTRACT_CONDITION (t), | |
8440 | G_("contract predicate is false in " | |
8441 | "constant expression"), | |
8442 | EXPR_LOCATION (t), checked_contract_p (semantic), | |
8443 | non_constant_p, overflow_p)) | |
8444 | *non_constant_p = true; | |
8445 | r = void_node; | |
8446 | } | |
8447 | break; | |
8448 | ||
cb57504a JM |
8449 | case TEMPLATE_ID_EXPR: |
8450 | { | |
8451 | /* We can evaluate template-id that refers to a concept only if | |
8452 | the template arguments are non-dependent. */ | |
861d4af8 AS |
8453 | tree id = unpack_concept_check (t); |
8454 | tree tmpl = TREE_OPERAND (id, 0); | |
8455 | if (!concept_definition_p (tmpl)) | |
cb57504a JM |
8456 | internal_error ("unexpected template-id %qE", t); |
8457 | ||
861d4af8 AS |
8458 | if (function_concept_p (tmpl)) |
8459 | { | |
8460 | if (!ctx->quiet) | |
8461 | error_at (cp_expr_loc_or_input_loc (t), | |
8462 | "function concept must be called"); | |
8463 | r = error_mark_node; | |
8464 | break; | |
8465 | } | |
8466 | ||
9329344a | 8467 | if (!value_dependent_expression_p (t) |
6d1556ec | 8468 | && !uid_sensitive_constexpr_evaluation_p ()) |
662ef5b5 | 8469 | r = evaluate_concept_check (t); |
cb57504a JM |
8470 | else |
8471 | *non_constant_p = true; | |
861d4af8 AS |
8472 | |
8473 | break; | |
cb57504a JM |
8474 | } |
8475 | ||
529bc410 MP |
8476 | case ASM_EXPR: |
8477 | if (!ctx->quiet) | |
c85f8dbb | 8478 | inline_asm_in_constexpr_error (loc, /*constexpr_fundef_p*/false); |
529bc410 MP |
8479 | *non_constant_p = true; |
8480 | return t; | |
8481 | ||
896048cf | 8482 | case BIT_CAST_EXPR: |
606f2af1 JJ |
8483 | if (lval) |
8484 | { | |
8485 | if (!ctx->quiet) | |
8486 | error_at (EXPR_LOCATION (t), | |
8487 | "address of a call to %qs is not a constant expression", | |
8488 | "__builtin_bit_cast"); | |
8489 | *non_constant_p = true; | |
8490 | return t; | |
8491 | } | |
896048cf JJ |
8492 | r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p); |
8493 | break; | |
8494 | ||
bfc07059 JJ |
8495 | case OMP_PARALLEL: |
8496 | case OMP_TASK: | |
8497 | case OMP_FOR: | |
8498 | case OMP_SIMD: | |
8499 | case OMP_DISTRIBUTE: | |
8500 | case OMP_TASKLOOP: | |
8501 | case OMP_LOOP: | |
8502 | case OMP_TEAMS: | |
8503 | case OMP_TARGET_DATA: | |
8504 | case OMP_TARGET: | |
8505 | case OMP_SECTIONS: | |
8506 | case OMP_ORDERED: | |
8507 | case OMP_CRITICAL: | |
8508 | case OMP_SINGLE: | |
8509 | case OMP_SCAN: | |
8510 | case OMP_SCOPE: | |
8511 | case OMP_SECTION: | |
a62c8324 | 8512 | case OMP_STRUCTURED_BLOCK: |
bfc07059 JJ |
8513 | case OMP_MASTER: |
8514 | case OMP_MASKED: | |
8515 | case OMP_TASKGROUP: | |
8516 | case OMP_TARGET_UPDATE: | |
8517 | case OMP_TARGET_ENTER_DATA: | |
8518 | case OMP_TARGET_EXIT_DATA: | |
8519 | case OMP_ATOMIC: | |
8520 | case OMP_ATOMIC_READ: | |
8521 | case OMP_ATOMIC_CAPTURE_OLD: | |
8522 | case OMP_ATOMIC_CAPTURE_NEW: | |
8523 | case OMP_DEPOBJ: | |
8524 | case OACC_PARALLEL: | |
8525 | case OACC_KERNELS: | |
8526 | case OACC_SERIAL: | |
8527 | case OACC_DATA: | |
8528 | case OACC_HOST_DATA: | |
8529 | case OACC_LOOP: | |
8530 | case OACC_CACHE: | |
8531 | case OACC_DECLARE: | |
8532 | case OACC_ENTER_DATA: | |
8533 | case OACC_EXIT_DATA: | |
8534 | case OACC_UPDATE: | |
8535 | if (!ctx->quiet) | |
8536 | error_at (EXPR_LOCATION (t), | |
8537 | "statement is not a constant expression"); | |
8538 | *non_constant_p = true; | |
8539 | break; | |
8540 | ||
2d76680f | 8541 | default: |
c6e7c499 JM |
8542 | if (STATEMENT_CODE_P (TREE_CODE (t))) |
8543 | { | |
8544 | /* This function doesn't know how to deal with pre-genericize | |
8545 | statements; this can only happen with statement-expressions, | |
8546 | so for now just fail. */ | |
8547 | if (!ctx->quiet) | |
8548 | error_at (EXPR_LOCATION (t), | |
64d6d399 | 8549 | "statement is not a constant expression"); |
c6e7c499 JM |
8550 | } |
8551 | else | |
8552 | internal_error ("unexpected expression %qE of kind %s", t, | |
8553 | get_tree_code_name (TREE_CODE (t))); | |
2d76680f PC |
8554 | *non_constant_p = true; |
8555 | break; | |
8556 | } | |
8557 | ||
8558 | if (r == error_mark_node) | |
8559 | *non_constant_p = true; | |
8560 | ||
8561 | if (*non_constant_p) | |
8562 | return t; | |
8563 | else | |
8564 | return r; | |
8565 | } | |
8566 | ||
e079dced JM |
8567 | /* P0859: A function is needed for constant evaluation if it is a constexpr |
8568 | function that is named by an expression ([basic.def.odr]) that is | |
8569 | potentially constant evaluated. | |
8570 | ||
8571 | So we need to instantiate any constexpr functions mentioned by the | |
8572 | expression even if the definition isn't needed for evaluating the | |
8573 | expression. */ | |
8574 | ||
8575 | static tree | |
8576 | instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/) | |
8577 | { | |
8578 | if (TREE_CODE (*tp) == FUNCTION_DECL | |
8579 | && DECL_DECLARED_CONSTEXPR_P (*tp) | |
8580 | && !DECL_INITIAL (*tp) | |
4fdda3ce | 8581 | && !trivial_fn_p (*tp) |
59946a4c | 8582 | && (DECL_TEMPLOID_INSTANTIATION (*tp) || DECL_DEFAULTED_FN (*tp)) |
f65a3299 | 8583 | && !uid_sensitive_constexpr_evaluation_p ()) |
e079dced JM |
8584 | { |
8585 | ++function_depth; | |
59946a4c PP |
8586 | if (DECL_TEMPLOID_INSTANTIATION (*tp)) |
8587 | instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false); | |
8588 | else | |
8589 | synthesize_method (*tp); | |
e079dced JM |
8590 | --function_depth; |
8591 | } | |
8592 | else if (TREE_CODE (*tp) == CALL_EXPR | |
8593 | || TREE_CODE (*tp) == AGGR_INIT_EXPR) | |
8594 | { | |
8595 | if (EXPR_HAS_LOCATION (*tp)) | |
8596 | input_location = EXPR_LOCATION (*tp); | |
8597 | } | |
8598 | ||
8599 | if (!EXPR_P (*tp)) | |
8600 | *walk_subtrees = 0; | |
8601 | ||
8602 | return NULL_TREE; | |
8603 | } | |
8e007055 | 8604 | |
e079dced JM |
8605 | static void |
8606 | instantiate_constexpr_fns (tree t) | |
8607 | { | |
8608 | location_t loc = input_location; | |
8609 | cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL); | |
8610 | input_location = loc; | |
8611 | } | |
8612 | ||
8e007055 JJ |
8613 | /* Look for heap variables in the expression *TP. */ |
8614 | ||
8615 | static tree | |
8616 | find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/) | |
8617 | { | |
8618 | if (VAR_P (*tp) | |
8619 | && (DECL_NAME (*tp) == heap_uninit_identifier | |
8620 | || DECL_NAME (*tp) == heap_identifier | |
815baade JJ |
8621 | || DECL_NAME (*tp) == heap_vec_uninit_identifier |
8622 | || DECL_NAME (*tp) == heap_vec_identifier | |
8e007055 JJ |
8623 | || DECL_NAME (*tp) == heap_deleted_identifier)) |
8624 | return *tp; | |
8625 | ||
8626 | if (TYPE_P (*tp)) | |
8627 | *walk_subtrees = 0; | |
8628 | return NULL_TREE; | |
8629 | } | |
8630 | ||
f968ef9b JJ |
8631 | /* Find immediate function decls in *TP if any. */ |
8632 | ||
8633 | static tree | |
8634 | find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/) | |
8635 | { | |
8636 | if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp)) | |
8637 | return *tp; | |
4b2fda8b JJ |
8638 | if (TREE_CODE (*tp) == PTRMEM_CST |
8639 | && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL | |
8640 | && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp))) | |
8641 | return PTRMEM_CST_MEMBER (*tp); | |
f968ef9b JJ |
8642 | return NULL_TREE; |
8643 | } | |
8644 | ||
ab71d3fe PP |
8645 | /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant |
8646 | expression. Return a version of T that has TREE_CONSTANT cleared. */ | |
8647 | ||
8648 | static tree | |
8649 | mark_non_constant (tree t) | |
8650 | { | |
8651 | gcc_checking_assert (TREE_CONSTANT (t)); | |
8652 | ||
8653 | /* This isn't actually constant, so unset TREE_CONSTANT. | |
8654 | Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires | |
8655 | it to be set if it is invariant address, even when it is not | |
8656 | a valid C++ constant expression. Wrap it with a NOP_EXPR | |
8657 | instead. */ | |
8658 | if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR) | |
8659 | t = copy_node (t); | |
8660 | else if (TREE_CODE (t) == CONSTRUCTOR) | |
8661 | t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t); | |
8662 | else | |
8663 | t = build_nop (TREE_TYPE (t), t); | |
8664 | TREE_CONSTANT (t) = false; | |
8665 | return t; | |
8666 | } | |
8667 | ||
e4082611 | 8668 | /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression. |
d4accef3 JM |
8669 | STRICT has the same sense as for constant_value_1: true if we only allow |
8670 | conforming C++ constant expressions, or false if we want a constant value | |
8671 | even if it doesn't conform. | |
13de99bc | 8672 | MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as |
8e007055 JJ |
8673 | per P0595 even when ALLOW_NON_CONSTANT is true. |
8674 | CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable. | |
8675 | OBJECT must be non-NULL in that case. */ | |
e4082611 | 8676 | |
2d76680f | 8677 | static tree |
3e605b20 | 8678 | cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant, |
e4082611 | 8679 | bool strict = true, |
3a0bc47c | 8680 | mce_value manifestly_const_eval = mce_unknown, |
8e007055 | 8681 | bool constexpr_dtor = false, |
f65a3299 | 8682 | tree object = NULL_TREE) |
2d76680f | 8683 | { |
8108ea30 JM |
8684 | auto_timevar time (TV_CONSTEXPR); |
8685 | ||
2d76680f PC |
8686 | bool non_constant_p = false; |
8687 | bool overflow_p = false; | |
39dce2b7 | 8688 | |
d419e176 MP |
8689 | if (BRACE_ENCLOSED_INITIALIZER_P (t)) |
8690 | { | |
8691 | gcc_checking_assert (allow_non_constant); | |
8692 | return t; | |
8693 | } | |
8694 | ||
8e007055 | 8695 | constexpr_global_ctx global_ctx; |
49a86fce | 8696 | constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL, |
8e007055 | 8697 | allow_non_constant, strict, |
3a0bc47c | 8698 | !allow_non_constant ? mce_true : manifestly_const_eval }; |
39dce2b7 | 8699 | |
6641d6d3 | 8700 | /* Turn off -frounding-math for manifestly constant evaluation. */ |
3a0bc47c PP |
8701 | warning_sentinel rm (flag_rounding_math, |
8702 | ctx.manifestly_const_eval == mce_true); | |
3e605b20 | 8703 | tree type = initialized_type (t); |
3e605b20 | 8704 | tree r = t; |
f968ef9b | 8705 | bool is_consteval = false; |
4d0c18c6 | 8706 | if (VOID_TYPE_P (type)) |
8e007055 JJ |
8707 | { |
8708 | if (constexpr_dtor) | |
8709 | /* Used for destructors of array elements. */ | |
8710 | type = TREE_TYPE (object); | |
8711 | else | |
f968ef9b | 8712 | { |
b04445d4 | 8713 | if (cxx_dialect < cxx20) |
f968ef9b JJ |
8714 | return t; |
8715 | if (TREE_CODE (t) != CALL_EXPR && TREE_CODE (t) != AGGR_INIT_EXPR) | |
8716 | return t; | |
8717 | /* Calls to immediate functions returning void need to be | |
8718 | evaluated. */ | |
8719 | tree fndecl = cp_get_callee_fndecl_nofold (t); | |
8720 | if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl)) | |
8721 | return t; | |
8722 | else | |
8723 | is_consteval = true; | |
8724 | } | |
8725 | } | |
b04445d4 | 8726 | else if (cxx_dialect >= cxx20 |
f968ef9b JJ |
8727 | && (TREE_CODE (t) == CALL_EXPR |
8728 | || TREE_CODE (t) == AGGR_INIT_EXPR | |
8729 | || TREE_CODE (t) == TARGET_EXPR)) | |
8730 | { | |
861d4af8 AS |
8731 | /* For non-concept checks, determine if it is consteval. */ |
8732 | if (!concept_check_p (t)) | |
8733 | { | |
8734 | tree x = t; | |
8735 | if (TREE_CODE (x) == TARGET_EXPR) | |
8736 | x = TARGET_EXPR_INITIAL (x); | |
8737 | tree fndecl = cp_get_callee_fndecl_nofold (x); | |
8738 | if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl)) | |
8739 | is_consteval = true; | |
8740 | } | |
8e007055 | 8741 | } |
3e605b20 JM |
8742 | if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)) |
8743 | { | |
8744 | /* In C++14 an NSDMI can participate in aggregate initialization, | |
8745 | and can refer to the address of the object being initialized, so | |
8746 | we need to pass in the relevant VAR_DECL if we want to do the | |
8747 | evaluation in a single pass. The evaluation will dynamically | |
8748 | update ctx.values for the VAR_DECL. We use the same strategy | |
8749 | for C++11 constexpr constructors that refer to the object being | |
8750 | initialized. */ | |
8e007055 JJ |
8751 | if (constexpr_dtor) |
8752 | { | |
8753 | gcc_assert (object && VAR_P (object)); | |
8754 | gcc_assert (DECL_DECLARED_CONSTEXPR_P (object)); | |
8755 | gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object)); | |
fce6467b JJ |
8756 | if (error_operand_p (DECL_INITIAL (object))) |
8757 | return t; | |
8e007055 JJ |
8758 | ctx.ctor = unshare_expr (DECL_INITIAL (object)); |
8759 | TREE_READONLY (ctx.ctor) = false; | |
8760 | /* Temporarily force decl_really_constant_value to return false | |
8761 | for it, we want to use ctx.ctor for the current value instead. */ | |
8762 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false; | |
8763 | } | |
8764 | else | |
8765 | { | |
8766 | ctx.ctor = build_constructor (type, NULL); | |
8767 | CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true; | |
8768 | } | |
60813a46 JM |
8769 | if (!object) |
8770 | { | |
958940eb JM |
8771 | if (TREE_CODE (t) == CALL_EXPR) |
8772 | { | |
8773 | /* If T is calling a constructor to initialize an object, reframe | |
8774 | it as an AGGR_INIT_EXPR to avoid trying to modify an object | |
8775 | from outside the constant evaluation, which will fail even if | |
8776 | the value is actually constant (is_constant_evaluated3.C). */ | |
8777 | tree fn = cp_get_callee_fndecl_nofold (t); | |
8778 | if (fn && DECL_CONSTRUCTOR_P (fn)) | |
8779 | { | |
8780 | object = CALL_EXPR_ARG (t, 0); | |
8781 | object = build_fold_indirect_ref (object); | |
8782 | r = build_aggr_init_expr (type, r); | |
8783 | } | |
8784 | } | |
8785 | else if (TREE_CODE (t) == TARGET_EXPR) | |
60813a46 JM |
8786 | object = TARGET_EXPR_SLOT (t); |
8787 | else if (TREE_CODE (t) == AGGR_INIT_EXPR) | |
8788 | object = AGGR_INIT_EXPR_SLOT (t); | |
8789 | } | |
3e605b20 JM |
8790 | ctx.object = object; |
8791 | if (object) | |
8792 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
8793 | (type, TREE_TYPE (object))); | |
8794 | if (object && DECL_P (object)) | |
e6a29aab | 8795 | global_ctx.put_value (object, ctx.ctor); |
3e605b20 JM |
8796 | if (TREE_CODE (r) == TARGET_EXPR) |
8797 | /* Avoid creating another CONSTRUCTOR when we expand the | |
8798 | TARGET_EXPR. */ | |
8799 | r = TARGET_EXPR_INITIAL (r); | |
8800 | } | |
8801 | ||
ee1de08d JJ |
8802 | auto_vec<tree, 16> cleanups; |
8803 | global_ctx.cleanups = &cleanups; | |
8804 | ||
3a0bc47c | 8805 | if (manifestly_const_eval == mce_true) |
1595fe44 | 8806 | instantiate_constexpr_fns (r); |
72f76540 JM |
8807 | r = cxx_eval_constant_expression (&ctx, r, vc_prvalue, |
8808 | &non_constant_p, &overflow_p); | |
2d76680f | 8809 | |
8e007055 JJ |
8810 | if (!constexpr_dtor) |
8811 | verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p); | |
8812 | else | |
8813 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true; | |
2d76680f | 8814 | |
ee1de08d JJ |
8815 | unsigned int i; |
8816 | tree cleanup; | |
8817 | /* Evaluate the cleanups. */ | |
8818 | FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup) | |
72f76540 | 8819 | cxx_eval_constant_expression (&ctx, cleanup, vc_discard, |
ee1de08d JJ |
8820 | &non_constant_p, &overflow_p); |
8821 | ||
023d89c7 JM |
8822 | /* Mutable logic is a bit tricky: we want to allow initialization of |
8823 | constexpr variables with mutable members, but we can't copy those | |
8824 | members to another constexpr variable. */ | |
8e007055 | 8825 | if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_MUTABLE_POISON (r)) |
2d76680f | 8826 | { |
2d76680f | 8827 | if (!allow_non_constant) |
023d89c7 JM |
8828 | error ("%qE is not a constant expression because it refers to " |
8829 | "mutable subobjects of %qT", t, type); | |
2d76680f PC |
8830 | non_constant_p = true; |
8831 | } | |
8832 | ||
8e007055 | 8833 | if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r)) |
f64e0c02 JM |
8834 | { |
8835 | if (!allow_non_constant) | |
8836 | error ("%qE is not a constant expression because it refers to " | |
8837 | "an incompletely initialized variable", t); | |
8838 | TREE_CONSTANT (r) = false; | |
8839 | non_constant_p = true; | |
8840 | } | |
8841 | ||
688fdde2 JM |
8842 | if (!non_constant_p && cxx_dialect >= cxx20 |
8843 | && !global_ctx.heap_vars.is_empty ()) | |
8e007055 JJ |
8844 | { |
8845 | tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs, | |
8846 | NULL); | |
8847 | unsigned int i; | |
8848 | if (heap_var) | |
8849 | { | |
8850 | if (!allow_non_constant && !non_constant_p) | |
8851 | error_at (DECL_SOURCE_LOCATION (heap_var), | |
8852 | "%qE is not a constant expression because it refers to " | |
8853 | "a result of %<operator new%>", t); | |
8854 | r = t; | |
8855 | non_constant_p = true; | |
8856 | } | |
8857 | FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var) | |
a8db7887 JJ |
8858 | { |
8859 | if (DECL_NAME (heap_var) != heap_deleted_identifier) | |
8860 | { | |
8861 | if (!allow_non_constant && !non_constant_p) | |
8862 | error_at (DECL_SOURCE_LOCATION (heap_var), | |
8863 | "%qE is not a constant expression because allocated " | |
8864 | "storage has not been deallocated", t); | |
8865 | r = t; | |
8866 | non_constant_p = true; | |
8867 | } | |
8868 | varpool_node::get (heap_var)->remove (); | |
8869 | } | |
8e007055 JJ |
8870 | } |
8871 | ||
f968ef9b | 8872 | /* Check that immediate invocation does not return an expression referencing |
7473b8a9 | 8873 | any immediate function decls. */ |
688fdde2 | 8874 | if (!non_constant_p && cxx_dialect >= cxx20) |
f968ef9b JJ |
8875 | if (tree immediate_fndecl |
8876 | = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl, | |
8877 | NULL)) | |
8878 | { | |
8879 | if (!allow_non_constant && !non_constant_p) | |
688fdde2 JM |
8880 | { |
8881 | if (is_consteval) | |
8882 | error_at (cp_expr_loc_or_input_loc (t), | |
8883 | "immediate evaluation returns address of immediate " | |
8884 | "function %qD", immediate_fndecl); | |
8885 | else | |
8886 | error_at (cp_expr_loc_or_input_loc (t), | |
8887 | "constant evaluation returns address of immediate " | |
8888 | "function %qD", immediate_fndecl); | |
8889 | } | |
f968ef9b JJ |
8890 | r = t; |
8891 | non_constant_p = true; | |
8892 | } | |
8893 | ||
8895443a JM |
8894 | if (non_constant_p) |
8895 | /* If we saw something bad, go back to our argument. The wrapping below is | |
8896 | only for the cases of TREE_CONSTANT argument or overflow. */ | |
8897 | r = t; | |
2d76680f PC |
8898 | |
8899 | if (!non_constant_p && overflow_p) | |
8900 | non_constant_p = true; | |
8901 | ||
e0804c9b | 8902 | /* Unshare the result. */ |
56cfb596 | 8903 | bool should_unshare = true; |
e0804c9b JM |
8904 | if (r == t || (TREE_CODE (t) == TARGET_EXPR |
8905 | && TARGET_EXPR_INITIAL (t) == r)) | |
56cfb596 PP |
8906 | should_unshare = false; |
8907 | ||
2d76680f PC |
8908 | if (non_constant_p && !allow_non_constant) |
8909 | return error_mark_node; | |
8e007055 JJ |
8910 | else if (constexpr_dtor) |
8911 | return r; | |
2d76680f | 8912 | else if (non_constant_p && TREE_CONSTANT (r)) |
ab71d3fe | 8913 | r = mark_non_constant (r); |
5dab8b11 | 8914 | else if (non_constant_p) |
2d76680f PC |
8915 | return t; |
8916 | ||
56cfb596 PP |
8917 | if (should_unshare) |
8918 | r = unshare_expr (r); | |
8919 | ||
2d76680f PC |
8920 | if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r))) |
8921 | { | |
5dab8b11 | 8922 | r = adjust_temp_type (type, r); |
2d76680f PC |
8923 | if (TREE_CODE (t) == TARGET_EXPR |
8924 | && TARGET_EXPR_INITIAL (t) == r) | |
8925 | return t; | |
43faf3e5 JM |
8926 | else if (TREE_CODE (t) == CONSTRUCTOR || TREE_CODE (t) == CALL_EXPR) |
8927 | /* Don't add a TARGET_EXPR if our argument didn't have one. */; | |
1a5145f1 JJ |
8928 | else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t)) |
8929 | r = get_target_expr (r); | |
8930 | else | |
2d76680f | 8931 | { |
c17fa0f2 | 8932 | r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup); |
2d76680f | 8933 | TREE_CONSTANT (r) = true; |
2d76680f PC |
8934 | } |
8935 | } | |
5dab8b11 | 8936 | |
8d46516a JM |
8937 | if (TREE_CODE (t) == TARGET_EXPR |
8938 | && TREE_CODE (r) == TARGET_EXPR) | |
8939 | { | |
8940 | /* Preserve this flag for potential_constant_expression, and the others | |
8941 | for good measure. */ | |
8942 | TARGET_EXPR_ELIDING_P (r) = TARGET_EXPR_ELIDING_P (t); | |
8943 | TARGET_EXPR_IMPLICIT_P (r) = TARGET_EXPR_IMPLICIT_P (t); | |
8944 | TARGET_EXPR_LIST_INIT_P (r) = TARGET_EXPR_LIST_INIT_P (t); | |
8945 | TARGET_EXPR_DIRECT_INIT_P (r) = TARGET_EXPR_DIRECT_INIT_P (t); | |
8946 | } | |
8947 | ||
729f6881 JM |
8948 | /* Remember the original location if that wouldn't need a wrapper. */ |
8949 | if (location_t loc = EXPR_LOCATION (t)) | |
8d970817 | 8950 | protected_set_expr_location (r, loc); |
729f6881 | 8951 | |
5dab8b11 | 8952 | return r; |
2d76680f PC |
8953 | } |
8954 | ||
2d76680f | 8955 | /* If T represents a constant expression returns its reduced value. |
c17fa0f2 | 8956 | Otherwise return error_mark_node. */ |
2d76680f PC |
8957 | |
8958 | tree | |
c17fa0f2 PP |
8959 | cxx_constant_value (tree t, tree decl /* = NULL_TREE */, |
8960 | tsubst_flags_t complain /* = tf_error */) | |
a8de8324 JM |
8961 | { |
8962 | bool sfinae = !(complain & tf_error); | |
3a0bc47c | 8963 | tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, mce_true, false, decl); |
a8de8324 JM |
8964 | if (sfinae && !TREE_CONSTANT (r)) |
8965 | r = error_mark_node; | |
8966 | return r; | |
8967 | } | |
8968 | ||
8e007055 JJ |
8969 | /* Like cxx_constant_value, but used for evaluation of constexpr destructors |
8970 | of constexpr variables. The actual initializer of DECL is not modified. */ | |
8971 | ||
8972 | void | |
8973 | cxx_constant_dtor (tree t, tree decl) | |
8974 | { | |
3a0bc47c | 8975 | cxx_eval_outermost_constant_expr (t, false, true, mce_true, true, decl); |
2d76680f PC |
8976 | } |
8977 | ||
cda0a029 JM |
8978 | /* Helper routine for fold_simple function. Either return simplified |
8979 | expression T, otherwise NULL_TREE. | |
8980 | In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold | |
8981 | even if we are within template-declaration. So be careful on call, as in | |
8982 | such case types can be undefined. */ | |
8983 | ||
8984 | static tree | |
8985 | fold_simple_1 (tree t) | |
8986 | { | |
8987 | tree op1; | |
8988 | enum tree_code code = TREE_CODE (t); | |
8989 | ||
8990 | switch (code) | |
8991 | { | |
8992 | case INTEGER_CST: | |
8993 | case REAL_CST: | |
8994 | case VECTOR_CST: | |
8995 | case FIXED_CST: | |
8996 | case COMPLEX_CST: | |
8997 | return t; | |
8998 | ||
8999 | case SIZEOF_EXPR: | |
9000 | return fold_sizeof_expr (t); | |
9001 | ||
9002 | case ABS_EXPR: | |
e28fadbc | 9003 | case ABSU_EXPR: |
cda0a029 JM |
9004 | case CONJ_EXPR: |
9005 | case REALPART_EXPR: | |
9006 | case IMAGPART_EXPR: | |
9007 | case NEGATE_EXPR: | |
9008 | case BIT_NOT_EXPR: | |
9009 | case TRUTH_NOT_EXPR: | |
cda0a029 | 9010 | case VIEW_CONVERT_EXPR: |
82a344f2 | 9011 | CASE_CONVERT: |
cda0a029 JM |
9012 | case FLOAT_EXPR: |
9013 | case FIX_TRUNC_EXPR: | |
9014 | case FIXED_CONVERT_EXPR: | |
9015 | case ADDR_SPACE_CONVERT_EXPR: | |
9016 | ||
9017 | op1 = TREE_OPERAND (t, 0); | |
9018 | ||
9019 | t = const_unop (code, TREE_TYPE (t), op1); | |
9020 | if (!t) | |
9021 | return NULL_TREE; | |
9022 | ||
9023 | if (CONVERT_EXPR_CODE_P (code) | |
9024 | && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1)) | |
9025 | TREE_OVERFLOW (t) = false; | |
9026 | return t; | |
9027 | ||
9028 | default: | |
9029 | return NULL_TREE; | |
9030 | } | |
9031 | } | |
9032 | ||
9033 | /* If T is a simple constant expression, returns its simplified value. | |
dba99244 | 9034 | Otherwise returns T. In contrast to maybe_constant_value we |
cda0a029 JM |
9035 | simplify only few operations on constant-expressions, and we don't |
9036 | try to simplify constexpressions. */ | |
9037 | ||
9038 | tree | |
9039 | fold_simple (tree t) | |
9040 | { | |
cda0a029 JM |
9041 | if (processing_template_decl) |
9042 | return t; | |
9043 | ||
dba99244 MP |
9044 | tree r = fold_simple_1 (t); |
9045 | if (r) | |
9046 | return r; | |
cda0a029 | 9047 | |
dba99244 | 9048 | return t; |
cda0a029 JM |
9049 | } |
9050 | ||
096f034a PP |
9051 | /* Try folding the expression T to a simple constant. |
9052 | Returns that constant, otherwise returns T. */ | |
9053 | ||
9054 | tree | |
9055 | fold_to_constant (tree t) | |
9056 | { | |
9057 | tree r = fold (t); | |
9058 | if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r)) | |
9059 | return r; | |
9060 | else | |
9061 | return t; | |
9062 | } | |
9063 | ||
2d76680f PC |
9064 | /* If T is a constant expression, returns its reduced value. |
9065 | Otherwise, if T does not have TREE_CONSTANT set, returns T. | |
13de99bc JJ |
9066 | Otherwise, returns a version of T without TREE_CONSTANT. |
9067 | MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated | |
f65a3299 | 9068 | as per P0595. */ |
2d76680f | 9069 | |
8a87daca JM |
9070 | static GTY((deletable)) hash_map<tree, tree> *cv_cache; |
9071 | ||
9072 | tree | |
3da5ae7a | 9073 | maybe_constant_value (tree t, tree decl /* = NULL_TREE */, |
3a0bc47c | 9074 | mce_value manifestly_const_eval /* = mce_unknown */) |
2d76680f PC |
9075 | { |
9076 | tree r; | |
9077 | ||
a0ab7ccd | 9078 | if (!is_nondependent_constant_expression (t)) |
2d76680f | 9079 | { |
ab71d3fe PP |
9080 | if (TREE_OVERFLOW_P (t) |
9081 | || (!processing_template_decl && TREE_CONSTANT (t))) | |
9082 | t = mark_non_constant (t); | |
2d76680f PC |
9083 | return t; |
9084 | } | |
8a87daca JM |
9085 | else if (CONSTANT_CLASS_P (t)) |
9086 | /* No caching or evaluation needed. */ | |
9087 | return t; | |
9088 | ||
096f034a PP |
9089 | /* Don't constant evaluate an unevaluated non-manifestly-constant operand, |
9090 | but at least try folding it to a simple constant. */ | |
9091 | if (cp_unevaluated_operand && manifestly_const_eval != mce_true) | |
9092 | return fold_to_constant (t); | |
9093 | ||
3a0bc47c PP |
9094 | if (manifestly_const_eval != mce_unknown) |
9095 | return cxx_eval_outermost_constant_expr (t, true, true, | |
9096 | manifestly_const_eval, false, decl); | |
13de99bc | 9097 | |
8a87daca JM |
9098 | if (cv_cache == NULL) |
9099 | cv_cache = hash_map<tree, tree>::create_ggc (101); | |
9100 | if (tree *cached = cv_cache->get (t)) | |
173c8def JM |
9101 | { |
9102 | r = *cached; | |
9103 | if (r != t) | |
9104 | { | |
cf59c898 PP |
9105 | /* Clear processing_template_decl for sake of break_out_target_exprs; |
9106 | entries in the cv_cache are non-templated. */ | |
9107 | processing_template_decl_sentinel ptds; | |
9108 | ||
9f143008 | 9109 | r = break_out_target_exprs (r, /*clear_loc*/true); |
173c8def JM |
9110 | protected_set_expr_location (r, EXPR_LOCATION (t)); |
9111 | } | |
dfffecb8 | 9112 | return r; |
173c8def | 9113 | } |
2d76680f | 9114 | |
f65a3299 | 9115 | uid_sensitive_constexpr_evaluation_checker c; |
3a0bc47c PP |
9116 | r = cxx_eval_outermost_constant_expr (t, true, true, |
9117 | manifestly_const_eval, false, decl); | |
595278be MM |
9118 | gcc_checking_assert (r == t |
9119 | || CONVERT_EXPR_P (t) | |
9120 | || TREE_CODE (t) == VIEW_CONVERT_EXPR | |
9121 | || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) | |
9122 | || !cp_tree_equal (r, t)); | |
f65a3299 PP |
9123 | if (!c.evaluation_restricted_p ()) |
9124 | cv_cache->put (t, r); | |
2d76680f PC |
9125 | return r; |
9126 | } | |
9127 | ||
7a7ac32a PP |
9128 | /* Dispose of the whole CV_CACHE. */ |
9129 | ||
9130 | static void | |
9131 | clear_cv_cache (void) | |
9132 | { | |
9133 | if (cv_cache != NULL) | |
9134 | cv_cache->empty (); | |
9135 | } | |
9136 | ||
bebabf70 | 9137 | /* Dispose of the whole CV_CACHE and FOLD_CACHE. */ |
1e297006 MP |
9138 | |
9139 | void | |
bebabf70 | 9140 | clear_cv_and_fold_caches () |
1e297006 | 9141 | { |
7a7ac32a | 9142 | clear_cv_cache (); |
eba9e839 | 9143 | clear_fold_cache (); |
1e297006 MP |
9144 | } |
9145 | ||
a81c8e8c MP |
9146 | /* Internal function handling expressions in templates for |
9147 | fold_non_dependent_expr and fold_non_dependent_init. | |
9148 | ||
9149 | If we're in a template, but T isn't value dependent, simplify | |
9150 | it. We're supposed to treat: | |
9151 | ||
9152 | template <typename T> void f(T[1 + 1]); | |
9153 | template <typename T> void f(T[2]); | |
9154 | ||
9155 | as two declarations of the same function, for example. */ | |
9156 | ||
9157 | static tree | |
9158 | fold_non_dependent_expr_template (tree t, tsubst_flags_t complain, | |
f968ef9b JJ |
9159 | bool manifestly_const_eval, |
9160 | tree object) | |
a81c8e8c MP |
9161 | { |
9162 | gcc_assert (processing_template_decl); | |
9163 | ||
9164 | if (is_nondependent_constant_expression (t)) | |
9165 | { | |
9166 | processing_template_decl_sentinel s; | |
9167 | t = instantiate_non_dependent_expr_internal (t, complain); | |
9168 | ||
9169 | if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t)) | |
9170 | { | |
9171 | if (TREE_OVERFLOW_P (t)) | |
9172 | { | |
9173 | t = build_nop (TREE_TYPE (t), t); | |
9174 | TREE_CONSTANT (t) = false; | |
9175 | } | |
9176 | return t; | |
9177 | } | |
096f034a PP |
9178 | else if (CONSTANT_CLASS_P (t)) |
9179 | /* No evaluation needed. */ | |
9180 | return t; | |
a81c8e8c | 9181 | |
096f034a PP |
9182 | /* Don't constant evaluate an unevaluated non-manifestly-constant operand, |
9183 | but at least try folding it to a simple constant. */ | |
4df7f8c7 | 9184 | if (cp_unevaluated_operand && !manifestly_const_eval) |
096f034a | 9185 | return fold_to_constant (t); |
4df7f8c7 | 9186 | |
a81c8e8c | 9187 | tree r = cxx_eval_outermost_constant_expr (t, true, true, |
3a0bc47c | 9188 | mce_value (manifestly_const_eval), |
f968ef9b | 9189 | false, object); |
a81c8e8c MP |
9190 | /* cp_tree_equal looks through NOPs, so allow them. */ |
9191 | gcc_checking_assert (r == t | |
9192 | || CONVERT_EXPR_P (t) | |
9193 | || TREE_CODE (t) == VIEW_CONVERT_EXPR | |
9194 | || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) | |
9195 | || !cp_tree_equal (r, t)); | |
9196 | return r; | |
9197 | } | |
9198 | else if (TREE_OVERFLOW_P (t)) | |
9199 | { | |
9200 | t = build_nop (TREE_TYPE (t), t); | |
9201 | TREE_CONSTANT (t) = false; | |
9202 | } | |
9203 | ||
9204 | return t; | |
9205 | } | |
9206 | ||
234bef96 PC |
9207 | /* Like maybe_constant_value but first fully instantiate the argument. |
9208 | ||
c17fa0f2 PP |
9209 | Note: this is equivalent to instantiate_non_dependent_expr (t, complain) |
9210 | followed by maybe_constant_value but is more efficient, | |
e56f6629 JM |
9211 | because it calls instantiation_dependent_expression_p and |
9212 | potential_constant_expression at most once. | |
4cd3e7df | 9213 | The manifestly_const_eval argument is passed to maybe_constant_value. |
e56f6629 JM |
9214 | |
9215 | Callers should generally pass their active complain, or if they are in a | |
9216 | non-template, diagnosing context, they can use the default of | |
9217 | tf_warning_or_error. Callers that might be within a template context, don't | |
9218 | have a complain parameter, and aren't going to remember the result for long | |
9219 | (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node | |
9220 | appropriately. */ | |
234bef96 PC |
9221 | |
9222 | tree | |
e56f6629 | 9223 | fold_non_dependent_expr (tree t, |
4cd3e7df | 9224 | tsubst_flags_t complain /* = tf_warning_or_error */, |
f968ef9b JJ |
9225 | bool manifestly_const_eval /* = false */, |
9226 | tree object /* = NULL_TREE */) | |
234bef96 PC |
9227 | { |
9228 | if (t == NULL_TREE) | |
9229 | return NULL_TREE; | |
9230 | ||
a81c8e8c MP |
9231 | if (processing_template_decl) |
9232 | return fold_non_dependent_expr_template (t, complain, | |
f968ef9b | 9233 | manifestly_const_eval, object); |
234bef96 | 9234 | |
3a0bc47c | 9235 | return maybe_constant_value (t, object, mce_value (manifestly_const_eval)); |
a81c8e8c | 9236 | } |
234bef96 | 9237 | |
ae2ebf01 MP |
9238 | /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant, |
9239 | return the original expression. */ | |
9240 | ||
9241 | tree | |
9242 | maybe_fold_non_dependent_expr (tree expr, | |
9243 | tsubst_flags_t complain/*=tf_warning_or_error*/) | |
9244 | { | |
9245 | tree t = fold_non_dependent_expr (expr, complain); | |
9246 | if (t && TREE_CONSTANT (t)) | |
9247 | return t; | |
9248 | ||
9249 | return expr; | |
9250 | } | |
234bef96 | 9251 | |
a81c8e8c | 9252 | /* Like maybe_constant_init but first fully instantiate the argument. */ |
234bef96 | 9253 | |
a81c8e8c MP |
9254 | tree |
9255 | fold_non_dependent_init (tree t, | |
9256 | tsubst_flags_t complain /*=tf_warning_or_error*/, | |
69bf1c7d MP |
9257 | bool manifestly_const_eval /*=false*/, |
9258 | tree object /* = NULL_TREE */) | |
a81c8e8c MP |
9259 | { |
9260 | if (t == NULL_TREE) | |
9261 | return NULL_TREE; | |
9262 | ||
9263 | if (processing_template_decl) | |
9264 | { | |
9265 | t = fold_non_dependent_expr_template (t, complain, | |
69bf1c7d | 9266 | manifestly_const_eval, object); |
a81c8e8c MP |
9267 | /* maybe_constant_init does this stripping, so do it here too. */ |
9268 | if (TREE_CODE (t) == TARGET_EXPR) | |
234bef96 | 9269 | { |
a81c8e8c MP |
9270 | tree init = TARGET_EXPR_INITIAL (t); |
9271 | if (TREE_CODE (init) == CONSTRUCTOR) | |
9272 | t = init; | |
234bef96 PC |
9273 | } |
9274 | return t; | |
9275 | } | |
9276 | ||
69bf1c7d | 9277 | return maybe_constant_init (t, object, manifestly_const_eval); |
234bef96 PC |
9278 | } |
9279 | ||
2d76680f | 9280 | /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather |
e4082611 JJ |
9281 | than wrapped in a TARGET_EXPR. |
9282 | ALLOW_NON_CONSTANT is false if T is required to be a constant expression. | |
13de99bc | 9283 | MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as |
e4082611 | 9284 | per P0595 even when ALLOW_NON_CONSTANT is true. */ |
2d76680f | 9285 | |
118cd6ba | 9286 | static tree |
e4082611 | 9287 | maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant, |
13de99bc | 9288 | bool manifestly_const_eval) |
2d76680f | 9289 | { |
cda0a029 JM |
9290 | if (!t) |
9291 | return t; | |
2d76680f PC |
9292 | if (TREE_CODE (t) == EXPR_STMT) |
9293 | t = TREE_OPERAND (t, 0); | |
9294 | if (TREE_CODE (t) == CONVERT_EXPR | |
9295 | && VOID_TYPE_P (TREE_TYPE (t))) | |
9296 | t = TREE_OPERAND (t, 0); | |
60813a46 JM |
9297 | if (TREE_CODE (t) == INIT_EXPR) |
9298 | t = TREE_OPERAND (t, 1); | |
f64e0c02 JM |
9299 | if (TREE_CODE (t) == TARGET_EXPR) |
9300 | t = TARGET_EXPR_INITIAL (t); | |
a0ab7ccd | 9301 | if (!is_nondependent_static_init_expression (t)) |
69eb4fde | 9302 | /* Don't try to evaluate it. */; |
688fdde2 JM |
9303 | else if (CONSTANT_CLASS_P (t) && TREE_CODE (t) != PTRMEM_CST) |
9304 | /* No evaluation needed. PTRMEM_CST needs the immediate fn check. */; | |
69eb4fde | 9305 | else |
6209009d JM |
9306 | { |
9307 | /* [basic.start.static] allows constant-initialization of variables with | |
9308 | static or thread storage duration even if it isn't required, but we | |
9309 | shouldn't bend the rules the same way for automatic variables. */ | |
9310 | bool is_static = (decl && DECL_P (decl) | |
9311 | && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))); | |
cbaa1d9c PP |
9312 | if (is_static) |
9313 | manifestly_const_eval = true; | |
31cdfdef PP |
9314 | |
9315 | if (cp_unevaluated_operand && !manifestly_const_eval) | |
9316 | return fold_to_constant (t); | |
9317 | ||
6209009d | 9318 | t = cxx_eval_outermost_constant_expr (t, allow_non_constant, !is_static, |
3a0bc47c PP |
9319 | mce_value (manifestly_const_eval), |
9320 | false, decl); | |
6209009d | 9321 | } |
2d76680f PC |
9322 | if (TREE_CODE (t) == TARGET_EXPR) |
9323 | { | |
9324 | tree init = TARGET_EXPR_INITIAL (t); | |
9325 | if (TREE_CODE (init) == CONSTRUCTOR) | |
9326 | t = init; | |
9327 | } | |
9328 | return t; | |
9329 | } | |
9330 | ||
118cd6ba MP |
9331 | /* Wrapper for maybe_constant_init_1 which permits non constants. */ |
9332 | ||
9333 | tree | |
13de99bc | 9334 | maybe_constant_init (tree t, tree decl, bool manifestly_const_eval) |
118cd6ba | 9335 | { |
13de99bc | 9336 | return maybe_constant_init_1 (t, decl, true, manifestly_const_eval); |
118cd6ba MP |
9337 | } |
9338 | ||
9339 | /* Wrapper for maybe_constant_init_1 which does not permit non constants. */ | |
9340 | ||
9341 | tree | |
9342 | cxx_constant_init (tree t, tree decl) | |
9343 | { | |
e4082611 | 9344 | return maybe_constant_init_1 (t, decl, false, true); |
118cd6ba MP |
9345 | } |
9346 | ||
2d76680f PC |
9347 | #if 0 |
9348 | /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */ | |
9349 | /* Return true if the object referred to by REF has automatic or thread | |
9350 | local storage. */ | |
9351 | ||
9352 | enum { ck_ok, ck_bad, ck_unknown }; | |
9353 | static int | |
9354 | check_automatic_or_tls (tree ref) | |
9355 | { | |
ef4bddc2 | 9356 | machine_mode mode; |
f37fac2b | 9357 | poly_int64 bitsize, bitpos; |
2d76680f PC |
9358 | tree offset; |
9359 | int volatilep = 0, unsignedp = 0; | |
9360 | tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset, | |
9361 | &mode, &unsignedp, &volatilep, false); | |
9362 | duration_kind dk; | |
9363 | ||
9364 | /* If there isn't a decl in the middle, we don't know the linkage here, | |
9365 | and this isn't a constant expression anyway. */ | |
9366 | if (!DECL_P (decl)) | |
9367 | return ck_unknown; | |
9368 | dk = decl_storage_duration (decl); | |
9369 | return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok; | |
9370 | } | |
9371 | #endif | |
9372 | ||
c7a53bdb JJ |
9373 | /* Data structure for passing data from potential_constant_expression_1 |
9374 | to check_for_return_continue via cp_walk_tree. */ | |
9375 | struct check_for_return_continue_data { | |
9376 | hash_set<tree> *pset; | |
9377 | tree continue_stmt; | |
0fb7aa20 | 9378 | tree break_stmt; |
c7a53bdb JJ |
9379 | }; |
9380 | ||
9381 | /* Helper function for potential_constant_expression_1 SWITCH_STMT handling, | |
9382 | called through cp_walk_tree. Return the first RETURN_EXPR found, or note | |
0fb7aa20 | 9383 | the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */ |
c7a53bdb JJ |
9384 | static tree |
9385 | check_for_return_continue (tree *tp, int *walk_subtrees, void *data) | |
9386 | { | |
0fb7aa20 | 9387 | tree t = *tp, s, b; |
c7a53bdb JJ |
9388 | check_for_return_continue_data *d = (check_for_return_continue_data *) data; |
9389 | switch (TREE_CODE (t)) | |
9390 | { | |
9391 | case RETURN_EXPR: | |
9392 | return t; | |
9393 | ||
9394 | case CONTINUE_STMT: | |
9395 | if (d->continue_stmt == NULL_TREE) | |
9396 | d->continue_stmt = t; | |
9397 | break; | |
9398 | ||
0fb7aa20 JJ |
9399 | case BREAK_STMT: |
9400 | if (d->break_stmt == NULL_TREE) | |
9401 | d->break_stmt = t; | |
9402 | break; | |
9403 | ||
c7a53bdb JJ |
9404 | #define RECUR(x) \ |
9405 | if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \ | |
9406 | d->pset)) \ | |
9407 | return r | |
9408 | ||
9409 | /* For loops, walk subtrees manually, so that continue stmts found | |
9410 | inside of the bodies of the loops are ignored. */ | |
9411 | case DO_STMT: | |
9412 | *walk_subtrees = 0; | |
9413 | RECUR (DO_COND (t)); | |
9414 | s = d->continue_stmt; | |
0fb7aa20 | 9415 | b = d->break_stmt; |
c7a53bdb JJ |
9416 | RECUR (DO_BODY (t)); |
9417 | d->continue_stmt = s; | |
0fb7aa20 | 9418 | d->break_stmt = b; |
c7a53bdb JJ |
9419 | break; |
9420 | ||
9421 | case WHILE_STMT: | |
9422 | *walk_subtrees = 0; | |
9423 | RECUR (WHILE_COND (t)); | |
9424 | s = d->continue_stmt; | |
0fb7aa20 | 9425 | b = d->break_stmt; |
c7a53bdb JJ |
9426 | RECUR (WHILE_BODY (t)); |
9427 | d->continue_stmt = s; | |
0fb7aa20 | 9428 | d->break_stmt = b; |
c7a53bdb JJ |
9429 | break; |
9430 | ||
9431 | case FOR_STMT: | |
9432 | *walk_subtrees = 0; | |
9433 | RECUR (FOR_INIT_STMT (t)); | |
9434 | RECUR (FOR_COND (t)); | |
9435 | RECUR (FOR_EXPR (t)); | |
9436 | s = d->continue_stmt; | |
0fb7aa20 | 9437 | b = d->break_stmt; |
c7a53bdb JJ |
9438 | RECUR (FOR_BODY (t)); |
9439 | d->continue_stmt = s; | |
0fb7aa20 | 9440 | d->break_stmt = b; |
c7a53bdb JJ |
9441 | break; |
9442 | ||
9443 | case RANGE_FOR_STMT: | |
9444 | *walk_subtrees = 0; | |
9445 | RECUR (RANGE_FOR_EXPR (t)); | |
9446 | s = d->continue_stmt; | |
0fb7aa20 | 9447 | b = d->break_stmt; |
c7a53bdb JJ |
9448 | RECUR (RANGE_FOR_BODY (t)); |
9449 | d->continue_stmt = s; | |
0fb7aa20 JJ |
9450 | d->break_stmt = b; |
9451 | break; | |
9452 | ||
9453 | case SWITCH_STMT: | |
9454 | *walk_subtrees = 0; | |
9455 | RECUR (SWITCH_STMT_COND (t)); | |
9456 | b = d->break_stmt; | |
9457 | RECUR (SWITCH_STMT_BODY (t)); | |
9458 | d->break_stmt = b; | |
c7a53bdb JJ |
9459 | break; |
9460 | #undef RECUR | |
9461 | ||
9462 | case STATEMENT_LIST: | |
9463 | case CONSTRUCTOR: | |
9464 | break; | |
9465 | ||
9466 | default: | |
9467 | if (!EXPR_P (t)) | |
9468 | *walk_subtrees = 0; | |
9469 | break; | |
9470 | } | |
9471 | ||
9472 | return NULL_TREE; | |
9473 | } | |
9474 | ||
2d76680f PC |
9475 | /* Return true if T denotes a potentially constant expression. Issue |
9476 | diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true, | |
a0ab7ccd JM |
9477 | an lvalue-rvalue conversion is implied. If NOW is true, we want to |
9478 | consider the expression in the current context, independent of constexpr | |
c85f8dbb MP |
9479 | substitution. If FUNDEF_P is true, we're checking a constexpr function body |
9480 | and hard errors should not be reported by constexpr_error. | |
2d76680f PC |
9481 | |
9482 | C++0x [expr.const] used to say | |
9483 | ||
9484 | 6 An expression is a potential constant expression if it is | |
9485 | a constant expression where all occurrences of function | |
9486 | parameters are replaced by arbitrary constant expressions | |
9487 | of the appropriate type. | |
9488 | ||
9489 | 2 A conditional expression is a constant expression unless it | |
9490 | involves one of the following as a potentially evaluated | |
9491 | subexpression (3.2), but subexpressions of logical AND (5.14), | |
9492 | logical OR (5.15), and conditional (5.16) operations that are | |
9493 | not evaluated are not considered. */ | |
9494 | ||
9495 | static bool | |
a0ab7ccd | 9496 | potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, |
c85f8dbb MP |
9497 | bool fundef_p, tsubst_flags_t flags, |
9498 | tree *jump_target) | |
2d76680f | 9499 | { |
a0ab7ccd | 9500 | #define RECUR(T,RV) \ |
c85f8dbb MP |
9501 | potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \ |
9502 | jump_target) | |
a0ab7ccd | 9503 | |
2d76680f PC |
9504 | enum { any = false, rval = true }; |
9505 | int i; | |
9506 | tree tmp; | |
9507 | ||
9508 | if (t == error_mark_node) | |
9509 | return false; | |
9510 | if (t == NULL_TREE) | |
9511 | return true; | |
f9d0ca40 | 9512 | location_t loc = cp_expr_loc_or_input_loc (t); |
3075affd JM |
9513 | |
9514 | if (*jump_target) | |
9515 | /* If we are jumping, ignore everything. This is simpler than the | |
9516 | cxx_eval_constant_expression handling because we only need to be | |
9517 | conservatively correct, and we don't necessarily have a constant value | |
9518 | available, so we don't bother with switch tracking. */ | |
9519 | return true; | |
9520 | ||
4c718551 JM |
9521 | if (TREE_THIS_VOLATILE (t) && want_rval |
9522 | && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t))) | |
2d76680f PC |
9523 | { |
9524 | if (flags & tf_error) | |
c85f8dbb MP |
9525 | constexpr_error (loc, fundef_p, "lvalue-to-rvalue conversion of " |
9526 | "a volatile lvalue %qE with type %qT", t, | |
9527 | TREE_TYPE (t)); | |
2d76680f PC |
9528 | return false; |
9529 | } | |
9530 | if (CONSTANT_CLASS_P (t)) | |
9531 | return true; | |
7dc2b4a2 JM |
9532 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED) |
9533 | && TREE_TYPE (t) == error_mark_node) | |
9534 | return false; | |
2d76680f PC |
9535 | |
9536 | switch (TREE_CODE (t)) | |
9537 | { | |
9538 | case FUNCTION_DECL: | |
9539 | case BASELINK: | |
9540 | case TEMPLATE_DECL: | |
9541 | case OVERLOAD: | |
9542 | case TEMPLATE_ID_EXPR: | |
9543 | case LABEL_DECL: | |
60813a46 | 9544 | case CASE_LABEL_EXPR: |
81012684 | 9545 | case PREDICT_EXPR: |
2d76680f PC |
9546 | case CONST_DECL: |
9547 | case SIZEOF_EXPR: | |
9548 | case ALIGNOF_EXPR: | |
9549 | case OFFSETOF_EXPR: | |
9550 | case NOEXCEPT_EXPR: | |
9551 | case TEMPLATE_PARM_INDEX: | |
9552 | case TRAIT_EXPR: | |
9553 | case IDENTIFIER_NODE: | |
9554 | case USERDEF_LITERAL: | |
9555 | /* We can see a FIELD_DECL in a pointer-to-member expression. */ | |
9556 | case FIELD_DECL: | |
cda0a029 | 9557 | case RESULT_DECL: |
2d76680f | 9558 | case USING_DECL: |
60813a46 | 9559 | case USING_STMT: |
3e605b20 | 9560 | case PLACEHOLDER_EXPR: |
971e17ff | 9561 | case REQUIRES_EXPR: |
98e5a19a | 9562 | case STATIC_ASSERT: |
96a95ac1 | 9563 | case DEBUG_BEGIN_STMT: |
2d76680f PC |
9564 | return true; |
9565 | ||
3075affd JM |
9566 | case RETURN_EXPR: |
9567 | if (!RECUR (TREE_OPERAND (t, 0), any)) | |
9568 | return false; | |
9569 | /* FALLTHROUGH */ | |
9570 | ||
9571 | case BREAK_STMT: | |
9572 | case CONTINUE_STMT: | |
9573 | *jump_target = t; | |
9574 | return true; | |
9575 | ||
a0ab7ccd | 9576 | case PARM_DECL: |
8fda2c27 | 9577 | if (now && want_rval) |
a0ab7ccd | 9578 | { |
8fda2c27 | 9579 | tree type = TREE_TYPE (t); |
e36d1994 MP |
9580 | if (dependent_type_p (type) |
9581 | || !COMPLETE_TYPE_P (processing_template_decl | |
9582 | ? type : complete_type (type)) | |
8fda2c27 JM |
9583 | || is_really_empty_class (type, /*ignore_vptr*/false)) |
9584 | /* An empty class has no data to read. */ | |
9585 | return true; | |
a0ab7ccd | 9586 | if (flags & tf_error) |
c85f8dbb MP |
9587 | constexpr_error (input_location, fundef_p, |
9588 | "%qE is not a constant expression", t); | |
a0ab7ccd JM |
9589 | return false; |
9590 | } | |
9591 | return true; | |
9592 | ||
2d76680f PC |
9593 | case AGGR_INIT_EXPR: |
9594 | case CALL_EXPR: | |
9595 | /* -- an invocation of a function other than a constexpr function | |
9596 | or a constexpr constructor. */ | |
9597 | { | |
9598 | tree fun = get_function_named_in_call (t); | |
9599 | const int nargs = call_expr_nargs (t); | |
9600 | i = 0; | |
9601 | ||
60813a46 JM |
9602 | if (fun == NULL_TREE) |
9603 | { | |
44a845ca MS |
9604 | /* Reset to allow the function to continue past the end |
9605 | of the block below. Otherwise return early. */ | |
9606 | bool bail = true; | |
9607 | ||
35228ac7 JJ |
9608 | if (TREE_CODE (t) == CALL_EXPR |
9609 | && CALL_EXPR_FN (t) == NULL_TREE) | |
9610 | switch (CALL_EXPR_IFN (t)) | |
9611 | { | |
9612 | /* These should be ignored, they are optimized away from | |
9613 | constexpr functions. */ | |
9614 | case IFN_UBSAN_NULL: | |
9615 | case IFN_UBSAN_BOUNDS: | |
9616 | case IFN_UBSAN_VPTR: | |
81fea426 | 9617 | case IFN_FALLTHROUGH: |
08b51bad | 9618 | case IFN_ASSUME: |
35228ac7 | 9619 | return true; |
44a845ca MS |
9620 | |
9621 | case IFN_ADD_OVERFLOW: | |
9622 | case IFN_SUB_OVERFLOW: | |
9623 | case IFN_MUL_OVERFLOW: | |
e16f1cc7 | 9624 | case IFN_LAUNDER: |
d8fcab68 | 9625 | case IFN_VEC_CONVERT: |
44a845ca | 9626 | bail = false; |
d8fcab68 | 9627 | break; |
44a845ca | 9628 | |
35228ac7 JJ |
9629 | default: |
9630 | break; | |
9631 | } | |
44a845ca MS |
9632 | |
9633 | if (bail) | |
9634 | { | |
9635 | /* fold_call_expr can't do anything with IFN calls. */ | |
9636 | if (flags & tf_error) | |
c85f8dbb MP |
9637 | constexpr_error (loc, fundef_p, |
9638 | "call to internal function %qE", t); | |
44a845ca MS |
9639 | return false; |
9640 | } | |
60813a46 | 9641 | } |
44a845ca MS |
9642 | |
9643 | if (fun && is_overloaded_fn (fun)) | |
2d76680f | 9644 | { |
7f4840dd PP |
9645 | if (!RECUR (fun, true)) |
9646 | return false; | |
9647 | fun = get_fns (fun); | |
9648 | ||
2d76680f PC |
9649 | if (TREE_CODE (fun) == FUNCTION_DECL) |
9650 | { | |
9651 | if (builtin_valid_in_constant_expr_p (fun)) | |
9652 | return true; | |
87c2080b | 9653 | if (!maybe_constexpr_fn (fun) |
2d76680f PC |
9654 | /* Allow any built-in function; if the expansion |
9655 | isn't constant, we'll deal with that then. */ | |
8e007055 | 9656 | && !fndecl_built_in_p (fun) |
b04445d4 | 9657 | /* In C++20, replaceable global allocation functions |
8e007055 | 9658 | are constant expressions. */ |
8412b939 JJ |
9659 | && (!cxx_replaceable_global_alloc_fn (fun) |
9660 | || TREE_CODE (t) != CALL_EXPR | |
9661 | || (!CALL_FROM_NEW_OR_DELETE_P (t) | |
9662 | && (current_function_decl == NULL_TREE | |
9663 | || !is_std_allocator_allocate | |
9664 | (current_function_decl)))) | |
cf650568 JJ |
9665 | /* Allow placement new in std::construct_at. */ |
9666 | && (!cxx_placement_new_fn (fun) | |
8412b939 | 9667 | || TREE_CODE (t) != CALL_EXPR |
cf650568 | 9668 | || current_function_decl == NULL_TREE |
22edf943 MP |
9669 | || !is_std_construct_at (current_function_decl)) |
9670 | && !cxx_dynamic_cast_fn_p (fun)) | |
2d76680f | 9671 | { |
c85f8dbb MP |
9672 | if ((flags & tf_error) |
9673 | && constexpr_error (loc, fundef_p, | |
9674 | "call to non-%<constexpr%> " | |
9675 | "function %qD", fun)) | |
9676 | explain_invalid_constexpr_fn (fun); | |
2d76680f PC |
9677 | return false; |
9678 | } | |
2d76680f | 9679 | } |
7f4840dd PP |
9680 | |
9681 | fun = OVL_FIRST (fun); | |
2d76680f PC |
9682 | /* Skip initial arguments to base constructors. */ |
9683 | if (DECL_BASE_CONSTRUCTOR_P (fun)) | |
9684 | i = num_artificial_parms_for (fun); | |
2d76680f | 9685 | } |
44a845ca | 9686 | else if (fun) |
2d76680f | 9687 | { |
0077c0fb PP |
9688 | if (TREE_TYPE (fun) |
9689 | && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun))) | |
9690 | want_rval = rval; | |
9691 | else | |
9692 | want_rval = any; | |
9693 | if (RECUR (fun, want_rval)) | |
e09bc034 MP |
9694 | /* Might end up being a constant function pointer. But it |
9695 | could also be a function object with constexpr op(), so | |
9696 | we pass 'any' so that the underlying VAR_DECL is deemed | |
9697 | as potentially-constant even though it wasn't declared | |
9698 | constexpr. */; | |
2d76680f PC |
9699 | else |
9700 | return false; | |
9701 | } | |
9702 | for (; i < nargs; ++i) | |
9703 | { | |
9704 | tree x = get_nth_callarg (t, i); | |
c3c29ba5 JM |
9705 | /* In a template, reference arguments haven't been converted to |
9706 | REFERENCE_TYPE and we might not even know if the parameter | |
9707 | is a reference, so accept lvalue constants too. */ | |
9708 | bool rv = processing_template_decl ? any : rval; | |
a0ab7ccd JM |
9709 | /* Don't require an immediately constant value, as constexpr |
9710 | substitution might not use the value of the argument. */ | |
9711 | bool sub_now = false; | |
9712 | if (!potential_constant_expression_1 (x, rv, strict, | |
c85f8dbb MP |
9713 | sub_now, fundef_p, flags, |
9714 | jump_target)) | |
2d76680f PC |
9715 | return false; |
9716 | } | |
9717 | return true; | |
9718 | } | |
9719 | ||
9720 | case NON_LVALUE_EXPR: | |
9721 | /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to | |
9722 | -- an lvalue of integral type that refers to a non-volatile | |
9723 | const variable or static data member initialized with | |
9724 | constant expressions, or | |
9725 | ||
9726 | -- an lvalue of literal type that refers to non-volatile | |
9727 | object defined with constexpr, or that refers to a | |
9728 | sub-object of such an object; */ | |
69eb4fde | 9729 | return RECUR (TREE_OPERAND (t, 0), rval); |
2d76680f | 9730 | |
98e34113 JJ |
9731 | case EXCESS_PRECISION_EXPR: |
9732 | return RECUR (TREE_OPERAND (t, 0), rval); | |
9733 | ||
2d76680f | 9734 | case VAR_DECL: |
70f40fea | 9735 | if (DECL_HAS_VALUE_EXPR_P (t)) |
c1051bf7 | 9736 | { |
68ad1bf7 | 9737 | if (now && is_normal_capture_proxy (t)) |
c1051bf7 JM |
9738 | { |
9739 | /* -- in a lambda-expression, a reference to this or to a | |
9740 | variable with automatic storage duration defined outside that | |
9741 | lambda-expression, where the reference would be an | |
9742 | odr-use. */ | |
04b89192 JM |
9743 | |
9744 | if (want_rval) | |
9745 | /* Since we're doing an lvalue-rvalue conversion, this might | |
9746 | not be an odr-use, so evaluate the variable directly. */ | |
9747 | return RECUR (DECL_CAPTURED_VARIABLE (t), rval); | |
9748 | ||
c1051bf7 JM |
9749 | if (flags & tf_error) |
9750 | { | |
9751 | tree cap = DECL_CAPTURED_VARIABLE (t); | |
4f870007 | 9752 | auto_diagnostic_group d; |
c85f8dbb MP |
9753 | if (constexpr_error (input_location, fundef_p, |
9754 | "lambda capture of %qE is not a " | |
9755 | "constant expression", cap) | |
9756 | && decl_constant_var_p (cap)) | |
c1051bf7 JM |
9757 | inform (input_location, "because it is used as a glvalue"); |
9758 | } | |
9759 | return false; | |
9760 | } | |
c2856cee MP |
9761 | /* Treat __PRETTY_FUNCTION__ inside a template function as |
9762 | potentially-constant. */ | |
9763 | else if (DECL_PRETTY_FUNCTION_P (t) | |
9764 | && DECL_VALUE_EXPR (t) == error_mark_node) | |
9765 | return true; | |
c1051bf7 JM |
9766 | return RECUR (DECL_VALUE_EXPR (t), rval); |
9767 | } | |
69eb4fde | 9768 | if (want_rval |
6665a857 | 9769 | && (now || !var_in_maybe_constexpr_fn (t)) |
7dc2b4a2 | 9770 | && !type_dependent_expression_p (t) |
4b691b13 | 9771 | && !decl_maybe_constant_var_p (t) |
69eb4fde JM |
9772 | && (strict |
9773 | || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t)) | |
4b691b13 JM |
9774 | || (DECL_INITIAL (t) |
9775 | && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))) | |
7dc2b4a2 | 9776 | && COMPLETE_TYPE_P (TREE_TYPE (t)) |
dbcd32f8 | 9777 | && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
2d76680f | 9778 | { |
c85f8dbb MP |
9779 | if (flags & tf_error) |
9780 | non_const_var_error (loc, t, fundef_p); | |
2d76680f PC |
9781 | return false; |
9782 | } | |
9783 | return true; | |
9784 | ||
9785 | case NOP_EXPR: | |
ed3ea9f2 JJ |
9786 | if (REINTERPRET_CAST_P (t)) |
9787 | { | |
9788 | if (flags & tf_error) | |
c85f8dbb MP |
9789 | constexpr_error (loc, fundef_p, "%<reinterpret_cast%> is not a " |
9790 | "constant expression"); | |
ed3ea9f2 JJ |
9791 | return false; |
9792 | } | |
9793 | /* FALLTHRU */ | |
2d76680f PC |
9794 | case CONVERT_EXPR: |
9795 | case VIEW_CONVERT_EXPR: | |
9796 | /* -- a reinterpret_cast. FIXME not implemented, and this rule | |
9797 | may change to something more specific to type-punning (DR 1312). */ | |
9798 | { | |
9799 | tree from = TREE_OPERAND (t, 0); | |
dfd7fdca | 9800 | if (location_wrapper_p (t)) |
73d9b0e5 JM |
9801 | { |
9802 | iloc_sentinel ils = loc; | |
9803 | return (RECUR (from, want_rval)); | |
9804 | } | |
dfd7fdca | 9805 | if (INDIRECT_TYPE_P (TREE_TYPE (t))) |
2d76680f | 9806 | { |
dfd7fdca DM |
9807 | STRIP_ANY_LOCATION_WRAPPER (from); |
9808 | if (TREE_CODE (from) == INTEGER_CST | |
9809 | && !integer_zerop (from)) | |
9810 | { | |
9811 | if (flags & tf_error) | |
c85f8dbb MP |
9812 | constexpr_error (loc, fundef_p, |
9813 | "%<reinterpret_cast%> from integer to " | |
9814 | "pointer"); | |
dfd7fdca DM |
9815 | return false; |
9816 | } | |
2d76680f | 9817 | } |
69eb4fde | 9818 | return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR)); |
2d76680f PC |
9819 | } |
9820 | ||
be845b04 JJ |
9821 | case ADDRESSOF_EXPR: |
9822 | /* This is like ADDR_EXPR, except it won't form pointer-to-member. */ | |
9823 | t = TREE_OPERAND (t, 0); | |
9824 | goto handle_addr_expr; | |
9825 | ||
2d76680f PC |
9826 | case ADDR_EXPR: |
9827 | /* -- a unary operator & that is applied to an lvalue that | |
9828 | designates an object with thread or automatic storage | |
9829 | duration; */ | |
9830 | t = TREE_OPERAND (t, 0); | |
9831 | ||
9832 | if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t)) | |
9833 | /* A pointer-to-member constant. */ | |
9834 | return true; | |
9835 | ||
be845b04 | 9836 | handle_addr_expr: |
2d76680f PC |
9837 | #if 0 |
9838 | /* FIXME adjust when issue 1197 is fully resolved. For now don't do | |
9839 | any checking here, as we might dereference the pointer later. If | |
9840 | we remove this code, also remove check_automatic_or_tls. */ | |
9841 | i = check_automatic_or_tls (t); | |
9842 | if (i == ck_ok) | |
9843 | return true; | |
9844 | if (i == ck_bad) | |
9845 | { | |
9846 | if (flags & tf_error) | |
9847 | error ("address-of an object %qE with thread local or " | |
9848 | "automatic storage is not a constant expression", t); | |
9849 | return false; | |
9850 | } | |
9851 | #endif | |
69eb4fde | 9852 | return RECUR (t, any); |
2d76680f PC |
9853 | |
9854 | case COMPONENT_REF: | |
2d76680f PC |
9855 | case ARROW_EXPR: |
9856 | case OFFSET_REF: | |
9857 | /* -- a class member access unless its postfix-expression is | |
9858 | of literal type or of pointer to literal type. */ | |
9859 | /* This test would be redundant, as it follows from the | |
9860 | postfix-expression being a potential constant expression. */ | |
19dedccf JM |
9861 | if (type_unknown_p (t)) |
9862 | return true; | |
8fda2c27 JM |
9863 | if (is_overloaded_fn (t)) |
9864 | /* In a template, a COMPONENT_REF of a function expresses ob.fn(), | |
9865 | which uses ob as an lvalue. */ | |
9866 | want_rval = false; | |
9867 | gcc_fallthrough (); | |
9868 | ||
9869 | case REALPART_EXPR: | |
9870 | case IMAGPART_EXPR: | |
9871 | case BIT_FIELD_REF: | |
69eb4fde | 9872 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
2d76680f PC |
9873 | |
9874 | case EXPR_PACK_EXPANSION: | |
69eb4fde | 9875 | return RECUR (PACK_EXPANSION_PATTERN (t), want_rval); |
2d76680f PC |
9876 | |
9877 | case INDIRECT_REF: | |
9878 | { | |
9879 | tree x = TREE_OPERAND (t, 0); | |
9880 | STRIP_NOPS (x); | |
948d0d91 | 9881 | if (is_this_parameter (x) && !is_capture_proxy (x)) |
2d76680f | 9882 | { |
6665a857 | 9883 | if (now || !var_in_maybe_constexpr_fn (x)) |
2d76680f PC |
9884 | { |
9885 | if (flags & tf_error) | |
c85f8dbb MP |
9886 | constexpr_error (loc, fundef_p, "use of %<this%> in a " |
9887 | "constant expression"); | |
2d76680f PC |
9888 | return false; |
9889 | } | |
2d76680f PC |
9890 | return true; |
9891 | } | |
69eb4fde | 9892 | return RECUR (x, rval); |
2d76680f PC |
9893 | } |
9894 | ||
60813a46 | 9895 | case STATEMENT_LIST: |
0f54cc9c JM |
9896 | for (tree stmt : tsi_range (t)) |
9897 | if (!RECUR (stmt, any)) | |
9898 | return false; | |
9899 | return true; | |
60813a46 JM |
9900 | |
9901 | case MODIFY_EXPR: | |
9902 | if (cxx_dialect < cxx14) | |
9903 | goto fail; | |
69eb4fde | 9904 | if (!RECUR (TREE_OPERAND (t, 0), any)) |
60813a46 | 9905 | return false; |
8e007055 JJ |
9906 | /* Just ignore clobbers. */ |
9907 | if (TREE_CLOBBER_P (TREE_OPERAND (t, 1))) | |
9908 | return true; | |
69eb4fde | 9909 | if (!RECUR (TREE_OPERAND (t, 1), rval)) |
60813a46 JM |
9910 | return false; |
9911 | return true; | |
9912 | ||
9913 | case MODOP_EXPR: | |
9914 | if (cxx_dialect < cxx14) | |
9915 | goto fail; | |
69eb4fde | 9916 | if (!RECUR (TREE_OPERAND (t, 0), rval)) |
60813a46 | 9917 | return false; |
69eb4fde | 9918 | if (!RECUR (TREE_OPERAND (t, 2), rval)) |
60813a46 JM |
9919 | return false; |
9920 | return true; | |
9921 | ||
60813a46 | 9922 | case DO_STMT: |
69eb4fde | 9923 | if (!RECUR (DO_COND (t), rval)) |
60813a46 | 9924 | return false; |
69eb4fde | 9925 | if (!RECUR (DO_BODY (t), any)) |
60813a46 | 9926 | return false; |
3075affd JM |
9927 | if (breaks (jump_target) || continues (jump_target)) |
9928 | *jump_target = NULL_TREE; | |
60813a46 JM |
9929 | return true; |
9930 | ||
9931 | case FOR_STMT: | |
69eb4fde | 9932 | if (!RECUR (FOR_INIT_STMT (t), any)) |
60813a46 | 9933 | return false; |
8e9558f0 MP |
9934 | tmp = FOR_COND (t); |
9935 | if (!RECUR (tmp, rval)) | |
60813a46 | 9936 | return false; |
8e9558f0 MP |
9937 | if (tmp) |
9938 | { | |
9939 | if (!processing_template_decl) | |
9940 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
a385474c MP |
9941 | /* If we couldn't evaluate the condition, it might not ever be |
9942 | true. */ | |
9943 | if (!integer_onep (tmp)) | |
0fb7aa20 JJ |
9944 | { |
9945 | /* Before returning true, check if the for body can contain | |
9946 | a return. */ | |
9947 | hash_set<tree> pset; | |
9948 | check_for_return_continue_data data = { &pset, NULL_TREE, | |
9949 | NULL_TREE }; | |
9950 | if (tree ret_expr | |
9951 | = cp_walk_tree (&FOR_BODY (t), check_for_return_continue, | |
9952 | &data, &pset)) | |
9953 | *jump_target = ret_expr; | |
9954 | return true; | |
9955 | } | |
8e9558f0 | 9956 | } |
69eb4fde | 9957 | if (!RECUR (FOR_EXPR (t), any)) |
60813a46 | 9958 | return false; |
69eb4fde | 9959 | if (!RECUR (FOR_BODY (t), any)) |
60813a46 | 9960 | return false; |
3075affd JM |
9961 | if (breaks (jump_target) || continues (jump_target)) |
9962 | *jump_target = NULL_TREE; | |
60813a46 JM |
9963 | return true; |
9964 | ||
98e5a19a | 9965 | case RANGE_FOR_STMT: |
8112667c MP |
9966 | if (!RECUR (RANGE_FOR_INIT_STMT (t), any)) |
9967 | return false; | |
98e5a19a JM |
9968 | if (!RECUR (RANGE_FOR_EXPR (t), any)) |
9969 | return false; | |
9970 | if (!RECUR (RANGE_FOR_BODY (t), any)) | |
9971 | return false; | |
3075affd JM |
9972 | if (breaks (jump_target) || continues (jump_target)) |
9973 | *jump_target = NULL_TREE; | |
98e5a19a JM |
9974 | return true; |
9975 | ||
60813a46 | 9976 | case WHILE_STMT: |
8e9558f0 MP |
9977 | tmp = WHILE_COND (t); |
9978 | if (!RECUR (tmp, rval)) | |
60813a46 | 9979 | return false; |
8e9558f0 MP |
9980 | if (!processing_template_decl) |
9981 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
a385474c MP |
9982 | /* If we couldn't evaluate the condition, it might not ever be true. */ |
9983 | if (!integer_onep (tmp)) | |
0fb7aa20 JJ |
9984 | { |
9985 | /* Before returning true, check if the while body can contain | |
9986 | a return. */ | |
9987 | hash_set<tree> pset; | |
9988 | check_for_return_continue_data data = { &pset, NULL_TREE, | |
9989 | NULL_TREE }; | |
9990 | if (tree ret_expr | |
9991 | = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue, | |
9992 | &data, &pset)) | |
9993 | *jump_target = ret_expr; | |
9994 | return true; | |
9995 | } | |
69eb4fde | 9996 | if (!RECUR (WHILE_BODY (t), any)) |
60813a46 | 9997 | return false; |
3075affd JM |
9998 | if (breaks (jump_target) || continues (jump_target)) |
9999 | *jump_target = NULL_TREE; | |
60813a46 JM |
10000 | return true; |
10001 | ||
10002 | case SWITCH_STMT: | |
69eb4fde | 10003 | if (!RECUR (SWITCH_STMT_COND (t), rval)) |
60813a46 | 10004 | return false; |
ce965730 | 10005 | /* FIXME we don't check SWITCH_STMT_BODY currently, because even |
c7a53bdb JJ |
10006 | unreachable labels would be checked and it is enough if there is |
10007 | a single switch cond value for which it is a valid constant | |
10008 | expression. We need to check if there are any RETURN_EXPRs | |
10009 | or CONTINUE_STMTs inside of the body though, as in that case | |
10010 | we need to set *jump_target. */ | |
10011 | else | |
10012 | { | |
10013 | hash_set<tree> pset; | |
0fb7aa20 JJ |
10014 | check_for_return_continue_data data = { &pset, NULL_TREE, |
10015 | NULL_TREE }; | |
c7a53bdb JJ |
10016 | if (tree ret_expr |
10017 | = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue, | |
10018 | &data, &pset)) | |
10019 | /* The switch might return. */ | |
10020 | *jump_target = ret_expr; | |
10021 | else if (data.continue_stmt) | |
10022 | /* The switch can't return, but might continue. */ | |
10023 | *jump_target = data.continue_stmt; | |
10024 | } | |
60813a46 JM |
10025 | return true; |
10026 | ||
58611fb6 | 10027 | case STMT_EXPR: |
69eb4fde | 10028 | return RECUR (STMT_EXPR_STMT (t), rval); |
58611fb6 | 10029 | |
2d76680f | 10030 | case LAMBDA_EXPR: |
899ac3b8 JM |
10031 | if (cxx_dialect >= cxx17) |
10032 | /* In C++17 lambdas can be constexpr, don't give up yet. */ | |
10033 | return true; | |
10034 | else if (flags & tf_error) | |
c85f8dbb MP |
10035 | constexpr_error (loc, fundef_p, "lambda-expression is not a " |
10036 | "constant expression before C++17"); | |
899ac3b8 JM |
10037 | return false; |
10038 | ||
2d76680f PC |
10039 | case NEW_EXPR: |
10040 | case VEC_NEW_EXPR: | |
10041 | case DELETE_EXPR: | |
10042 | case VEC_DELETE_EXPR: | |
2423f654 MP |
10043 | if (cxx_dialect >= cxx20) |
10044 | /* In C++20, new-expressions are potentially constant. */ | |
10045 | return true; | |
10046 | else if (flags & tf_error) | |
c85f8dbb MP |
10047 | constexpr_error (loc, fundef_p, "new-expression is not a " |
10048 | "constant expression before C++20"); | |
2423f654 MP |
10049 | return false; |
10050 | ||
10051 | case DYNAMIC_CAST_EXPR: | |
10052 | case PSEUDO_DTOR_EXPR: | |
2d76680f | 10053 | case THROW_EXPR: |
e40b6fc7 JJ |
10054 | case OMP_PARALLEL: |
10055 | case OMP_TASK: | |
10056 | case OMP_FOR: | |
897064e2 | 10057 | case OMP_SIMD: |
e40b6fc7 JJ |
10058 | case OMP_DISTRIBUTE: |
10059 | case OMP_TASKLOOP: | |
d81ab49d | 10060 | case OMP_LOOP: |
e40b6fc7 JJ |
10061 | case OMP_TEAMS: |
10062 | case OMP_TARGET_DATA: | |
10063 | case OMP_TARGET: | |
10064 | case OMP_SECTIONS: | |
10065 | case OMP_ORDERED: | |
10066 | case OMP_CRITICAL: | |
10067 | case OMP_SINGLE: | |
bfc07059 JJ |
10068 | case OMP_SCAN: |
10069 | case OMP_SCOPE: | |
e40b6fc7 JJ |
10070 | case OMP_SECTION: |
10071 | case OMP_MASTER: | |
4b1e1434 | 10072 | case OMP_MASKED: |
e40b6fc7 JJ |
10073 | case OMP_TASKGROUP: |
10074 | case OMP_TARGET_UPDATE: | |
10075 | case OMP_TARGET_ENTER_DATA: | |
10076 | case OMP_TARGET_EXIT_DATA: | |
2d76680f PC |
10077 | case OMP_ATOMIC: |
10078 | case OMP_ATOMIC_READ: | |
10079 | case OMP_ATOMIC_CAPTURE_OLD: | |
10080 | case OMP_ATOMIC_CAPTURE_NEW: | |
28567c40 | 10081 | case OMP_DEPOBJ: |
e40b6fc7 JJ |
10082 | case OACC_PARALLEL: |
10083 | case OACC_KERNELS: | |
62aee289 | 10084 | case OACC_SERIAL: |
e40b6fc7 JJ |
10085 | case OACC_DATA: |
10086 | case OACC_HOST_DATA: | |
10087 | case OACC_LOOP: | |
10088 | case OACC_CACHE: | |
10089 | case OACC_DECLARE: | |
10090 | case OACC_ENTER_DATA: | |
10091 | case OACC_EXIT_DATA: | |
10092 | case OACC_UPDATE: | |
2d76680f PC |
10093 | /* GCC internal stuff. */ |
10094 | case VA_ARG_EXPR: | |
2d76680f | 10095 | case TRANSACTION_EXPR: |
81b6a6c5 | 10096 | case AT_ENCODE_EXPR: |
60813a46 | 10097 | fail: |
2d76680f | 10098 | if (flags & tf_error) |
c85f8dbb MP |
10099 | constexpr_error (loc, fundef_p, "expression %qE is not a constant " |
10100 | "expression", t); | |
2d76680f PC |
10101 | return false; |
10102 | ||
529bc410 | 10103 | case ASM_EXPR: |
7c814975 | 10104 | if (flags & tf_error) |
c85f8dbb | 10105 | inline_asm_in_constexpr_error (loc, fundef_p); |
7c814975 | 10106 | return false; |
529bc410 | 10107 | |
bf8d8309 | 10108 | case OBJ_TYPE_REF: |
b04445d4 JM |
10109 | if (cxx_dialect >= cxx20) |
10110 | /* In C++20 virtual calls can be constexpr, don't give up yet. */ | |
bf8d8309 MP |
10111 | return true; |
10112 | else if (flags & tf_error) | |
c85f8dbb MP |
10113 | constexpr_error (loc, fundef_p, "virtual functions cannot be " |
10114 | "%<constexpr%> before C++20"); | |
bf8d8309 MP |
10115 | return false; |
10116 | ||
2d76680f | 10117 | case TYPEID_EXPR: |
66acfb80 MP |
10118 | /* In C++20, a typeid expression whose operand is of polymorphic |
10119 | class type can be constexpr. */ | |
2d76680f PC |
10120 | { |
10121 | tree e = TREE_OPERAND (t, 0); | |
b04445d4 | 10122 | if (cxx_dialect < cxx20 |
66acfb80 MP |
10123 | && strict |
10124 | && !TYPE_P (e) | |
10125 | && !type_dependent_expression_p (e) | |
2d76680f PC |
10126 | && TYPE_POLYMORPHIC_P (TREE_TYPE (e))) |
10127 | { | |
10128 | if (flags & tf_error) | |
c85f8dbb MP |
10129 | constexpr_error (loc, fundef_p, "%<typeid%> is not a " |
10130 | "constant expression because %qE is " | |
10131 | "of polymorphic type", e); | |
2d76680f PC |
10132 | return false; |
10133 | } | |
10134 | return true; | |
10135 | } | |
10136 | ||
1af4ebf5 | 10137 | case POINTER_DIFF_EXPR: |
2d76680f | 10138 | case MINUS_EXPR: |
2d76680f PC |
10139 | want_rval = true; |
10140 | goto binary; | |
10141 | ||
10142 | case LT_EXPR: | |
10143 | case LE_EXPR: | |
10144 | case GT_EXPR: | |
10145 | case GE_EXPR: | |
10146 | case EQ_EXPR: | |
10147 | case NE_EXPR: | |
b7689b96 | 10148 | case SPACESHIP_EXPR: |
2d76680f PC |
10149 | want_rval = true; |
10150 | goto binary; | |
10151 | ||
60813a46 JM |
10152 | case PREINCREMENT_EXPR: |
10153 | case POSTINCREMENT_EXPR: | |
10154 | case PREDECREMENT_EXPR: | |
10155 | case POSTDECREMENT_EXPR: | |
10156 | if (cxx_dialect < cxx14) | |
10157 | goto fail; | |
10158 | goto unary; | |
10159 | ||
2d76680f PC |
10160 | case BIT_NOT_EXPR: |
10161 | /* A destructor. */ | |
10162 | if (TYPE_P (TREE_OPERAND (t, 0))) | |
10163 | return true; | |
191816a3 | 10164 | /* fall through. */ |
2d76680f | 10165 | |
2d76680f PC |
10166 | case CONJ_EXPR: |
10167 | case SAVE_EXPR: | |
10168 | case FIX_TRUNC_EXPR: | |
10169 | case FLOAT_EXPR: | |
10170 | case NEGATE_EXPR: | |
10171 | case ABS_EXPR: | |
e197e64e | 10172 | case ABSU_EXPR: |
2d76680f PC |
10173 | case TRUTH_NOT_EXPR: |
10174 | case FIXED_CONVERT_EXPR: | |
10175 | case UNARY_PLUS_EXPR: | |
4856a1f0 PC |
10176 | case UNARY_LEFT_FOLD_EXPR: |
10177 | case UNARY_RIGHT_FOLD_EXPR: | |
60813a46 | 10178 | unary: |
69eb4fde | 10179 | return RECUR (TREE_OPERAND (t, 0), rval); |
2d76680f PC |
10180 | |
10181 | case CAST_EXPR: | |
10182 | case CONST_CAST_EXPR: | |
10183 | case STATIC_CAST_EXPR: | |
10184 | case REINTERPRET_CAST_EXPR: | |
10185 | case IMPLICIT_CONV_EXPR: | |
5925f0ec | 10186 | if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t))) |
2d76680f PC |
10187 | /* In C++98, a conversion to non-integral type can't be part of a |
10188 | constant expression. */ | |
10189 | { | |
10190 | if (flags & tf_error) | |
c85f8dbb MP |
10191 | constexpr_error (loc, fundef_p, |
10192 | "cast to non-integral type %qT in a constant " | |
10193 | "expression", TREE_TYPE (t)); | |
2d76680f PC |
10194 | return false; |
10195 | } | |
fe0604d3 MP |
10196 | /* This might be a conversion from a class to a (potentially) literal |
10197 | type. Let's consider it potentially constant since the conversion | |
10198 | might be a constexpr user-defined conversion. */ | |
10199 | else if (cxx_dialect >= cxx11 | |
10200 | && (dependent_type_p (TREE_TYPE (t)) | |
10201 | || !COMPLETE_TYPE_P (TREE_TYPE (t)) | |
10202 | || literal_type_p (TREE_TYPE (t))) | |
10203 | && TREE_OPERAND (t, 0)) | |
10204 | { | |
10205 | tree type = TREE_TYPE (TREE_OPERAND (t, 0)); | |
10206 | /* If this is a dependent type, it could end up being a class | |
10207 | with conversions. */ | |
10208 | if (type == NULL_TREE || WILDCARD_TYPE_P (type)) | |
10209 | return true; | |
10210 | /* Or a non-dependent class which has conversions. */ | |
10211 | else if (CLASS_TYPE_P (type) | |
10212 | && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type))) | |
10213 | return true; | |
10214 | } | |
2d76680f | 10215 | |
69eb4fde | 10216 | return (RECUR (TREE_OPERAND (t, 0), |
9f613f06 | 10217 | !TYPE_REF_P (TREE_TYPE (t)))); |
2d76680f | 10218 | |
60813a46 | 10219 | case BIND_EXPR: |
69eb4fde | 10220 | return RECUR (BIND_EXPR_BODY (t), want_rval); |
60813a46 | 10221 | |
60813a46 JM |
10222 | case CLEANUP_POINT_EXPR: |
10223 | case MUST_NOT_THROW_EXPR: | |
10224 | case TRY_CATCH_EXPR: | |
769430b2 | 10225 | case TRY_BLOCK: |
60813a46 JM |
10226 | case EH_SPEC_BLOCK: |
10227 | case EXPR_STMT: | |
2d76680f | 10228 | case PAREN_EXPR: |
2d76680f | 10229 | /* For convenience. */ |
f937929e JM |
10230 | case LOOP_EXPR: |
10231 | case EXIT_EXPR: | |
69eb4fde | 10232 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
2d76680f | 10233 | |
98e5a19a JM |
10234 | case DECL_EXPR: |
10235 | tmp = DECL_EXPR_DECL (t); | |
32d16fe9 JJ |
10236 | if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp) |
10237 | && (processing_template_decl | |
10238 | ? !decl_maybe_constant_var_p (tmp) | |
10239 | : !decl_constant_var_p (tmp))) | |
98e5a19a | 10240 | { |
72124f48 | 10241 | if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp)) |
98e5a19a JM |
10242 | { |
10243 | if (flags & tf_error) | |
c85f8dbb MP |
10244 | constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p, |
10245 | "%qD defined %<thread_local%> in " | |
10246 | "%<constexpr%> context", tmp); | |
98e5a19a JM |
10247 | return false; |
10248 | } | |
8892d532 | 10249 | else if (TREE_STATIC (tmp)) |
98e5a19a JM |
10250 | { |
10251 | if (flags & tf_error) | |
c85f8dbb MP |
10252 | constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p, |
10253 | "%qD defined %<static%> in %<constexpr%> " | |
10254 | "context", tmp); | |
98e5a19a JM |
10255 | return false; |
10256 | } | |
3885527d PC |
10257 | else if (!check_for_uninitialized_const_var |
10258 | (tmp, /*constexpr_context_p=*/true, flags)) | |
10259 | return false; | |
98e5a19a | 10260 | } |
b222e725 JJ |
10261 | if (VAR_P (tmp)) |
10262 | return RECUR (DECL_INITIAL (tmp), want_rval); | |
10263 | return true; | |
98e5a19a | 10264 | |
769430b2 JM |
10265 | case TRY_FINALLY_EXPR: |
10266 | return (RECUR (TREE_OPERAND (t, 0), want_rval) | |
10267 | && RECUR (TREE_OPERAND (t, 1), any)); | |
10268 | ||
2d76680f | 10269 | case SCOPE_REF: |
69eb4fde | 10270 | return RECUR (TREE_OPERAND (t, 1), want_rval); |
2d76680f PC |
10271 | |
10272 | case TARGET_EXPR: | |
d3a9902e | 10273 | if (!TARGET_EXPR_DIRECT_INIT_P (t) |
8d46516a | 10274 | && !TARGET_EXPR_ELIDING_P (t) |
d3a9902e | 10275 | && !literal_type_p (TREE_TYPE (t))) |
2d76680f PC |
10276 | { |
10277 | if (flags & tf_error) | |
10278 | { | |
097f82ec | 10279 | auto_diagnostic_group d; |
c85f8dbb MP |
10280 | if (constexpr_error (loc, fundef_p, |
10281 | "temporary of non-literal type %qT in a " | |
10282 | "constant expression", TREE_TYPE (t))) | |
10283 | explain_non_literal_class (TREE_TYPE (t)); | |
2d76680f PC |
10284 | } |
10285 | return false; | |
10286 | } | |
191816a3 | 10287 | /* FALLTHRU */ |
2d76680f | 10288 | case INIT_EXPR: |
69eb4fde | 10289 | return RECUR (TREE_OPERAND (t, 1), rval); |
2d76680f PC |
10290 | |
10291 | case CONSTRUCTOR: | |
10292 | { | |
10293 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); | |
10294 | constructor_elt *ce; | |
10295 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
69eb4fde | 10296 | if (!RECUR (ce->value, want_rval)) |
2d76680f PC |
10297 | return false; |
10298 | return true; | |
10299 | } | |
10300 | ||
10301 | case TREE_LIST: | |
10302 | { | |
10303 | gcc_assert (TREE_PURPOSE (t) == NULL_TREE | |
10304 | || DECL_P (TREE_PURPOSE (t))); | |
69eb4fde | 10305 | if (!RECUR (TREE_VALUE (t), want_rval)) |
2d76680f PC |
10306 | return false; |
10307 | if (TREE_CHAIN (t) == NULL_TREE) | |
10308 | return true; | |
69eb4fde | 10309 | return RECUR (TREE_CHAIN (t), want_rval); |
2d76680f PC |
10310 | } |
10311 | ||
10312 | case TRUNC_DIV_EXPR: | |
10313 | case CEIL_DIV_EXPR: | |
10314 | case FLOOR_DIV_EXPR: | |
10315 | case ROUND_DIV_EXPR: | |
10316 | case TRUNC_MOD_EXPR: | |
10317 | case CEIL_MOD_EXPR: | |
10318 | case ROUND_MOD_EXPR: | |
10319 | { | |
10320 | tree denom = TREE_OPERAND (t, 1); | |
69eb4fde | 10321 | if (!RECUR (denom, rval)) |
2d76680f PC |
10322 | return false; |
10323 | /* We can't call cxx_eval_outermost_constant_expr on an expression | |
234bef96 | 10324 | that hasn't been through instantiate_non_dependent_expr yet. */ |
2d76680f PC |
10325 | if (!processing_template_decl) |
10326 | denom = cxx_eval_outermost_constant_expr (denom, true); | |
10327 | if (integer_zerop (denom)) | |
10328 | { | |
10329 | if (flags & tf_error) | |
c85f8dbb MP |
10330 | constexpr_error (input_location, fundef_p, |
10331 | "division by zero is not a constant expression"); | |
2d76680f PC |
10332 | return false; |
10333 | } | |
10334 | else | |
10335 | { | |
10336 | want_rval = true; | |
69eb4fde | 10337 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
2d76680f PC |
10338 | } |
10339 | } | |
10340 | ||
10341 | case COMPOUND_EXPR: | |
10342 | { | |
10343 | /* check_return_expr sometimes wraps a TARGET_EXPR in a | |
46fdced6 | 10344 | COMPOUND_EXPR; don't get confused. */ |
2d76680f PC |
10345 | tree op0 = TREE_OPERAND (t, 0); |
10346 | tree op1 = TREE_OPERAND (t, 1); | |
10347 | STRIP_NOPS (op1); | |
46fdced6 | 10348 | if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) |
69eb4fde | 10349 | return RECUR (op0, want_rval); |
2d76680f PC |
10350 | else |
10351 | goto binary; | |
10352 | } | |
10353 | ||
10354 | /* If the first operand is the non-short-circuit constant, look at | |
10355 | the second operand; otherwise we only care about the first one for | |
10356 | potentiality. */ | |
10357 | case TRUTH_AND_EXPR: | |
10358 | case TRUTH_ANDIF_EXPR: | |
10359 | tmp = boolean_true_node; | |
10360 | goto truth; | |
10361 | case TRUTH_OR_EXPR: | |
10362 | case TRUTH_ORIF_EXPR: | |
10363 | tmp = boolean_false_node; | |
10364 | truth: | |
10365 | { | |
9927ecbb PP |
10366 | tree op0 = TREE_OPERAND (t, 0); |
10367 | tree op1 = TREE_OPERAND (t, 1); | |
10368 | if (!RECUR (op0, rval)) | |
2d76680f | 10369 | return false; |
9927ecbb PP |
10370 | if (!(flags & tf_error) && RECUR (op1, rval)) |
10371 | /* When quiet, try to avoid expensive trial evaluation by first | |
10372 | checking potentiality of the second operand. */ | |
10373 | return true; | |
2d76680f | 10374 | if (!processing_template_decl) |
9927ecbb PP |
10375 | op0 = cxx_eval_outermost_constant_expr (op0, true); |
10376 | if (tree_int_cst_equal (op0, tmp)) | |
10377 | return (flags & tf_error) ? RECUR (op1, rval) : false; | |
2d76680f PC |
10378 | else |
10379 | return true; | |
10380 | } | |
10381 | ||
10382 | case PLUS_EXPR: | |
10383 | case MULT_EXPR: | |
10384 | case POINTER_PLUS_EXPR: | |
10385 | case RDIV_EXPR: | |
10386 | case EXACT_DIV_EXPR: | |
10387 | case MIN_EXPR: | |
10388 | case MAX_EXPR: | |
10389 | case LSHIFT_EXPR: | |
10390 | case RSHIFT_EXPR: | |
81fa6d73 JJ |
10391 | case LROTATE_EXPR: |
10392 | case RROTATE_EXPR: | |
2d76680f PC |
10393 | case BIT_IOR_EXPR: |
10394 | case BIT_XOR_EXPR: | |
10395 | case BIT_AND_EXPR: | |
10396 | case TRUTH_XOR_EXPR: | |
10397 | case UNORDERED_EXPR: | |
10398 | case ORDERED_EXPR: | |
10399 | case UNLT_EXPR: | |
10400 | case UNLE_EXPR: | |
10401 | case UNGT_EXPR: | |
10402 | case UNGE_EXPR: | |
10403 | case UNEQ_EXPR: | |
10404 | case LTGT_EXPR: | |
10405 | case RANGE_EXPR: | |
10406 | case COMPLEX_EXPR: | |
10407 | want_rval = true; | |
10408 | /* Fall through. */ | |
10409 | case ARRAY_REF: | |
10410 | case ARRAY_RANGE_REF: | |
10411 | case MEMBER_REF: | |
10412 | case DOTSTAR_EXPR: | |
41b38772 | 10413 | case MEM_REF: |
4856a1f0 PC |
10414 | case BINARY_LEFT_FOLD_EXPR: |
10415 | case BINARY_RIGHT_FOLD_EXPR: | |
2d76680f PC |
10416 | binary: |
10417 | for (i = 0; i < 2; ++i) | |
69eb4fde | 10418 | if (!RECUR (TREE_OPERAND (t, i), want_rval)) |
2d76680f PC |
10419 | return false; |
10420 | return true; | |
10421 | ||
2d76680f PC |
10422 | case VEC_PERM_EXPR: |
10423 | for (i = 0; i < 3; ++i) | |
69eb4fde | 10424 | if (!RECUR (TREE_OPERAND (t, i), true)) |
2d76680f PC |
10425 | return false; |
10426 | return true; | |
10427 | ||
10428 | case COND_EXPR: | |
b04445d4 | 10429 | if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20) |
d5bcd6d4 JM |
10430 | { |
10431 | if (flags & tf_error) | |
c85f8dbb MP |
10432 | constexpr_error (loc, fundef_p, "%<delete[]%> is not a " |
10433 | "constant expression"); | |
d5bcd6d4 JM |
10434 | return false; |
10435 | } | |
10436 | /* Fall through. */ | |
10437 | case IF_STMT: | |
2d76680f PC |
10438 | case VEC_COND_EXPR: |
10439 | /* If the condition is a known constant, we know which of the legs we | |
10440 | care about; otherwise we only require that the condition and | |
10441 | either of the legs be potentially constant. */ | |
10442 | tmp = TREE_OPERAND (t, 0); | |
69eb4fde | 10443 | if (!RECUR (tmp, rval)) |
2d76680f PC |
10444 | return false; |
10445 | if (!processing_template_decl) | |
10446 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
117c6426 JJ |
10447 | /* potential_constant_expression* isn't told if it is called for |
10448 | manifestly_const_eval or not, so for consteval if always | |
10449 | process both branches as if the condition is not a known | |
10450 | constant. */ | |
10451 | if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t)) | |
10452 | { | |
10453 | if (integer_zerop (tmp)) | |
10454 | return RECUR (TREE_OPERAND (t, 2), want_rval); | |
10455 | else if (TREE_CODE (tmp) == INTEGER_CST) | |
10456 | return RECUR (TREE_OPERAND (t, 1), want_rval); | |
10457 | } | |
0fb7aa20 | 10458 | tmp = *jump_target; |
2d76680f | 10459 | for (i = 1; i < 3; ++i) |
0fb7aa20 JJ |
10460 | { |
10461 | tree this_jump_target = tmp; | |
10462 | if (potential_constant_expression_1 (TREE_OPERAND (t, i), | |
c85f8dbb | 10463 | want_rval, strict, now, fundef_p, |
0fb7aa20 JJ |
10464 | tf_none, &this_jump_target)) |
10465 | { | |
10466 | if (returns (&this_jump_target)) | |
10467 | *jump_target = this_jump_target; | |
10468 | else if (!returns (jump_target)) | |
10469 | { | |
10470 | if (breaks (&this_jump_target) | |
10471 | || continues (&this_jump_target)) | |
10472 | *jump_target = this_jump_target; | |
10473 | if (i == 1) | |
10474 | { | |
10475 | /* If the then branch is potentially constant, but | |
10476 | does not return, check if the else branch | |
10477 | couldn't return, break or continue. */ | |
10478 | hash_set<tree> pset; | |
10479 | check_for_return_continue_data data = { &pset, NULL_TREE, | |
10480 | NULL_TREE }; | |
10481 | if (tree ret_expr | |
10482 | = cp_walk_tree (&TREE_OPERAND (t, 2), | |
10483 | check_for_return_continue, &data, | |
10484 | &pset)) | |
10485 | *jump_target = ret_expr; | |
10486 | else if (*jump_target == NULL_TREE) | |
10487 | { | |
10488 | if (data.continue_stmt) | |
10489 | *jump_target = data.continue_stmt; | |
10490 | else if (data.break_stmt) | |
10491 | *jump_target = data.break_stmt; | |
10492 | } | |
10493 | } | |
10494 | } | |
10495 | return true; | |
10496 | } | |
10497 | } | |
2d76680f | 10498 | if (flags & tf_error) |
ff465bd8 PP |
10499 | { |
10500 | if (TREE_CODE (t) == IF_STMT) | |
c85f8dbb MP |
10501 | constexpr_error (loc, fundef_p, "neither branch of %<if%> is a " |
10502 | "constant expression"); | |
ff465bd8 | 10503 | else |
c85f8dbb MP |
10504 | constexpr_error (loc, fundef_p, "expression %qE is not a " |
10505 | "constant expression", t); | |
ff465bd8 | 10506 | } |
2d76680f PC |
10507 | return false; |
10508 | ||
10509 | case VEC_INIT_EXPR: | |
10510 | if (VEC_INIT_EXPR_IS_CONSTEXPR (t)) | |
10511 | return true; | |
10512 | if (flags & tf_error) | |
10513 | { | |
c85f8dbb MP |
10514 | if (constexpr_error (loc, fundef_p, "non-constant array " |
10515 | "initialization")) | |
10516 | diagnose_non_constexpr_vec_init (t); | |
2d76680f PC |
10517 | } |
10518 | return false; | |
10519 | ||
133bc698 JM |
10520 | case TYPE_DECL: |
10521 | case TAG_DEFN: | |
10522 | /* We can see these in statement-expressions. */ | |
10523 | return true; | |
10524 | ||
baf9ebc8 | 10525 | case CLEANUP_STMT: |
1006c9d4 JJ |
10526 | if (!RECUR (CLEANUP_BODY (t), any)) |
10527 | return false; | |
10528 | if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any)) | |
10529 | return false; | |
10530 | return true; | |
10531 | ||
cda0a029 | 10532 | case EMPTY_CLASS_EXPR: |
46fdced6 | 10533 | return true; |
cda0a029 | 10534 | |
f937929e JM |
10535 | case GOTO_EXPR: |
10536 | { | |
10537 | tree *target = &TREE_OPERAND (t, 0); | |
02a981a8 JJ |
10538 | /* Gotos representing break, continue and cdtor return are OK. */ |
10539 | if (breaks (target) || continues (target) || returns (target)) | |
3075affd JM |
10540 | { |
10541 | *jump_target = *target; | |
10542 | return true; | |
10543 | } | |
ab3af181 | 10544 | if (flags & tf_error) |
c85f8dbb MP |
10545 | constexpr_error (loc, fundef_p, "%<goto%> is not a constant " |
10546 | "expression"); | |
ab3af181 | 10547 | return false; |
f937929e JM |
10548 | } |
10549 | ||
2efb237f JCI |
10550 | case ASSERTION_STMT: |
10551 | case PRECONDITION_STMT: | |
10552 | case POSTCONDITION_STMT: | |
10553 | if (!checked_contract_p (get_contract_semantic (t))) | |
10554 | return true; | |
10555 | return RECUR (CONTRACT_CONDITION (t), rval); | |
10556 | ||
6f20c42c MP |
10557 | case LABEL_EXPR: |
10558 | t = LABEL_EXPR_LABEL (t); | |
8892d532 | 10559 | if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23) |
6f20c42c MP |
10560 | return true; |
10561 | else if (flags & tf_error) | |
c85f8dbb MP |
10562 | constexpr_error (loc, fundef_p, "label definition in %<constexpr%> " |
10563 | "function only available with %<-std=c++2b%> or " | |
10564 | "%<-std=gnu++2b%>"); | |
6f20c42c MP |
10565 | return false; |
10566 | ||
98e09245 | 10567 | case ANNOTATE_EXPR: |
98e09245 JJ |
10568 | return RECUR (TREE_OPERAND (t, 0), rval); |
10569 | ||
896048cf JJ |
10570 | case BIT_CAST_EXPR: |
10571 | return RECUR (TREE_OPERAND (t, 0), rval); | |
10572 | ||
49789fd0 IS |
10573 | /* Coroutine await, yield and return expressions are not. */ |
10574 | case CO_AWAIT_EXPR: | |
10575 | case CO_YIELD_EXPR: | |
10576 | case CO_RETURN_EXPR: | |
10577 | return false; | |
10578 | ||
d4c470c3 PP |
10579 | case NONTYPE_ARGUMENT_PACK: |
10580 | { | |
10581 | tree args = ARGUMENT_PACK_ARGS (t); | |
10582 | int len = TREE_VEC_LENGTH (args); | |
10583 | for (int i = 0; i < len; ++i) | |
10584 | if (!RECUR (TREE_VEC_ELT (args, i), any)) | |
10585 | return false; | |
10586 | return true; | |
10587 | } | |
10588 | ||
2d76680f | 10589 | default: |
878cffbd | 10590 | if (objc_non_constant_expr_p (t)) |
2d76680f PC |
10591 | return false; |
10592 | ||
10593 | sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t))); | |
ab3af181 | 10594 | gcc_unreachable (); |
2d76680f PC |
10595 | return false; |
10596 | } | |
69eb4fde | 10597 | #undef RECUR |
2d76680f PC |
10598 | } |
10599 | ||
3075affd JM |
10600 | bool |
10601 | potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, | |
c85f8dbb | 10602 | bool fundef_p, tsubst_flags_t flags) |
3075affd | 10603 | { |
9927ecbb PP |
10604 | if (flags & tf_error) |
10605 | { | |
10606 | /* Check potentiality quietly first, as that could be performed more | |
10607 | efficiently in some cases (currently only for TRUTH_*_EXPR). If | |
10608 | that fails, replay the check noisily to give errors. */ | |
10609 | flags &= ~tf_error; | |
c85f8dbb MP |
10610 | if (potential_constant_expression_1 (t, want_rval, strict, now, fundef_p, |
10611 | flags)) | |
9927ecbb PP |
10612 | return true; |
10613 | flags |= tf_error; | |
10614 | } | |
10615 | ||
3075affd | 10616 | tree target = NULL_TREE; |
c85f8dbb | 10617 | return potential_constant_expression_1 (t, want_rval, strict, now, fundef_p, |
3075affd JM |
10618 | flags, &target); |
10619 | } | |
10620 | ||
2d76680f PC |
10621 | /* The main entry point to the above. */ |
10622 | ||
10623 | bool | |
10624 | potential_constant_expression (tree t) | |
10625 | { | |
c85f8dbb MP |
10626 | return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true, |
10627 | /*now*/false, /*fundef_p*/false, | |
10628 | tf_none); | |
2d76680f PC |
10629 | } |
10630 | ||
10631 | /* As above, but require a constant rvalue. */ | |
10632 | ||
10633 | bool | |
10634 | potential_rvalue_constant_expression (tree t) | |
10635 | { | |
c85f8dbb MP |
10636 | return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true, |
10637 | /*now*/false, /*fundef_p*/false, | |
10638 | tf_none); | |
2d76680f PC |
10639 | } |
10640 | ||
10641 | /* Like above, but complain about non-constant expressions. */ | |
10642 | ||
10643 | bool | |
10644 | require_potential_constant_expression (tree t) | |
10645 | { | |
c85f8dbb MP |
10646 | return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true, |
10647 | /*now*/false, /*fundef_p*/false, | |
d8cff23f | 10648 | tf_warning_or_error); |
2d76680f PC |
10649 | } |
10650 | ||
10651 | /* Cross product of the above. */ | |
10652 | ||
10653 | bool | |
10654 | require_potential_rvalue_constant_expression (tree t) | |
10655 | { | |
c85f8dbb MP |
10656 | return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true, |
10657 | /*now*/false, /*fundef_p*/false, | |
10658 | tf_warning_or_error); | |
10659 | } | |
10660 | ||
10661 | /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */ | |
10662 | ||
10663 | bool | |
10664 | require_potential_rvalue_constant_expression_fncheck (tree t) | |
10665 | { | |
10666 | return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true, | |
10667 | /*now*/false, /*fundef_p*/true, | |
d8cff23f MP |
10668 | tf_warning_or_error); |
10669 | } | |
10670 | ||
10671 | /* Like above, but don't consider PARM_DECL a potential_constant_expression. */ | |
10672 | ||
10673 | bool | |
10674 | require_rvalue_constant_expression (tree t) | |
10675 | { | |
c85f8dbb MP |
10676 | return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true, |
10677 | /*now*/true, /*fundef_p*/false, | |
d8cff23f | 10678 | tf_warning_or_error); |
a0ab7ccd JM |
10679 | } |
10680 | ||
10681 | /* Like potential_constant_expression, but don't consider possible constexpr | |
10682 | substitution of the current function. That is, PARM_DECL qualifies under | |
10683 | potential_constant_expression, but not here. | |
10684 | ||
10685 | This is basically what you can check when any actual constant values might | |
10686 | be value-dependent. */ | |
10687 | ||
10688 | bool | |
10689 | is_constant_expression (tree t) | |
10690 | { | |
c85f8dbb MP |
10691 | return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true, |
10692 | /*now*/true, /*fundef_p*/false, | |
10693 | tf_none); | |
a0ab7ccd JM |
10694 | } |
10695 | ||
beb019d3 JM |
10696 | /* As above, but expect an rvalue. */ |
10697 | ||
10698 | bool | |
10699 | is_rvalue_constant_expression (tree t) | |
10700 | { | |
c85f8dbb MP |
10701 | return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true, |
10702 | /*now*/true, /*fundef_p*/false, | |
10703 | tf_none); | |
beb019d3 JM |
10704 | } |
10705 | ||
a0ab7ccd JM |
10706 | /* Like above, but complain about non-constant expressions. */ |
10707 | ||
10708 | bool | |
10709 | require_constant_expression (tree t) | |
10710 | { | |
c85f8dbb MP |
10711 | return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true, |
10712 | /*now*/true, /*fundef_p*/false, | |
a0ab7ccd JM |
10713 | tf_warning_or_error); |
10714 | } | |
10715 | ||
10716 | /* Like is_constant_expression, but allow const variables that are not allowed | |
10717 | under constexpr rules. */ | |
10718 | ||
10719 | bool | |
10720 | is_static_init_expression (tree t) | |
10721 | { | |
c85f8dbb MP |
10722 | return potential_constant_expression_1 (t, /*want_rval*/false, |
10723 | /*strict*/false, /*now*/true, | |
10724 | /*fundef_p*/false, tf_none); | |
2d76680f PC |
10725 | } |
10726 | ||
eb07f187 JM |
10727 | /* Returns true if T is a potential constant expression that is not |
10728 | instantiation-dependent, and therefore a candidate for constant folding even | |
10729 | in a template. */ | |
10730 | ||
10731 | bool | |
a0ab7ccd | 10732 | is_nondependent_constant_expression (tree t) |
eb07f187 JM |
10733 | { |
10734 | return (!type_unknown_p (t) | |
a0ab7ccd | 10735 | && is_constant_expression (t) |
eb07f187 JM |
10736 | && !instantiation_dependent_expression_p (t)); |
10737 | } | |
10738 | ||
10739 | /* Returns true if T is a potential static initializer expression that is not | |
10740 | instantiation-dependent. */ | |
10741 | ||
10742 | bool | |
a0ab7ccd | 10743 | is_nondependent_static_init_expression (tree t) |
eb07f187 JM |
10744 | { |
10745 | return (!type_unknown_p (t) | |
a0ab7ccd | 10746 | && is_static_init_expression (t) |
eb07f187 JM |
10747 | && !instantiation_dependent_expression_p (t)); |
10748 | } | |
10749 | ||
87c2080b JM |
10750 | /* True iff FN is an implicitly constexpr function. */ |
10751 | ||
10752 | bool | |
10753 | decl_implicit_constexpr_p (tree fn) | |
10754 | { | |
10755 | if (!(flag_implicit_constexpr | |
10756 | && TREE_CODE (fn) == FUNCTION_DECL | |
10757 | && DECL_DECLARED_CONSTEXPR_P (fn))) | |
10758 | return false; | |
10759 | ||
10760 | if (DECL_CLONED_FUNCTION_P (fn)) | |
10761 | fn = DECL_CLONED_FUNCTION (fn); | |
10762 | ||
10763 | return (DECL_LANG_SPECIFIC (fn) | |
10764 | && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr); | |
10765 | } | |
10766 | ||
97f3003f JM |
10767 | /* Finalize constexpr processing after parsing. */ |
10768 | ||
10769 | void | |
10770 | fini_constexpr (void) | |
10771 | { | |
10772 | /* The contexpr call and fundef copies tables are no longer needed. */ | |
10773 | constexpr_call_table = NULL; | |
10774 | fundef_copies_table = NULL; | |
10775 | } | |
10776 | ||
2d76680f | 10777 | #include "gt-cp-constexpr.h" |