]>
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 | ||
99dee823 | 5 | Copyright (C) 1998-2021 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" |
44a845ca | 34 | #include "gimple-fold.h" |
8108ea30 | 35 | #include "timevar.h" |
d8fcab68 | 36 | #include "fold-const-call.h" |
8e007055 | 37 | #include "stor-layout.h" |
a8db7887 | 38 | #include "cgraph.h" |
00f34291 | 39 | #include "opts.h" |
2d76680f PC |
40 | |
41 | static bool verify_constant (tree, bool, bool *, bool *); | |
42 | #define VERIFY_CONSTANT(X) \ | |
43 | do { \ | |
2b3ab879 | 44 | if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \ |
2d76680f PC |
45 | return t; \ |
46 | } while (0) | |
47 | ||
582d2481 JJ |
48 | static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex, |
49 | bool insert = false); | |
baf05d54 | 50 | static int array_index_cmp (tree key, tree index); |
582d2481 | 51 | |
2d76680f PC |
52 | /* Returns true iff FUN is an instantiation of a constexpr function |
53 | template or a defaulted constexpr function. */ | |
54 | ||
55 | bool | |
56 | is_instantiation_of_constexpr (tree fun) | |
57 | { | |
58 | return ((DECL_TEMPLOID_INSTANTIATION (fun) | |
59 | && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun))) | |
60 | || (DECL_DEFAULTED_FN (fun) | |
61 | && DECL_DECLARED_CONSTEXPR_P (fun))); | |
62 | } | |
63 | ||
64 | /* Return true if T is a literal type. */ | |
65 | ||
66 | bool | |
67 | literal_type_p (tree t) | |
68 | { | |
69 | if (SCALAR_TYPE_P (t) | |
b55b02ea | 70 | || VECTOR_TYPE_P (t) |
9f613f06 | 71 | || TYPE_REF_P (t) |
e42c407c | 72 | || (VOID_TYPE_P (t) && cxx_dialect >= cxx14)) |
2d76680f PC |
73 | return true; |
74 | if (CLASS_TYPE_P (t)) | |
75 | { | |
76 | t = complete_type (t); | |
77 | gcc_assert (COMPLETE_TYPE_P (t) || errorcount); | |
78 | return CLASSTYPE_LITERAL_P (t); | |
79 | } | |
80 | if (TREE_CODE (t) == ARRAY_TYPE) | |
81 | return literal_type_p (strip_array_types (t)); | |
82 | return false; | |
83 | } | |
84 | ||
85 | /* If DECL is a variable declared `constexpr', require its type | |
9638f320 PC |
86 | be literal. Return error_mark_node if we give an error, the |
87 | DECL otherwise. */ | |
2d76680f PC |
88 | |
89 | tree | |
90 | ensure_literal_type_for_constexpr_object (tree decl) | |
91 | { | |
92 | tree type = TREE_TYPE (decl); | |
93 | if (VAR_P (decl) | |
94 | && (DECL_DECLARED_CONSTEXPR_P (decl) | |
95 | || var_in_constexpr_fn (decl)) | |
96 | && !processing_template_decl) | |
97 | { | |
98 | tree stype = strip_array_types (type); | |
99 | if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype))) | |
100 | /* Don't complain here, we'll complain about incompleteness | |
101 | when we try to initialize the variable. */; | |
102 | else if (!literal_type_p (type)) | |
103 | { | |
104 | if (DECL_DECLARED_CONSTEXPR_P (decl)) | |
f1eac182 | 105 | { |
097f82ec | 106 | auto_diagnostic_group d; |
f2935576 PC |
107 | error_at (DECL_SOURCE_LOCATION (decl), |
108 | "the type %qT of %<constexpr%> variable %qD " | |
109 | "is not literal", type, decl); | |
f1eac182 | 110 | explain_non_literal_class (type); |
9638f320 | 111 | decl = error_mark_node; |
f1eac182 | 112 | } |
8892d532 | 113 | else if (cxx_dialect < cxx23) |
60813a46 | 114 | { |
5d4e573b | 115 | if (!is_instantiation_of_constexpr (current_function_decl)) |
f1eac182 | 116 | { |
097f82ec | 117 | auto_diagnostic_group d; |
f2935576 PC |
118 | error_at (DECL_SOURCE_LOCATION (decl), |
119 | "variable %qD of non-literal type %qT in " | |
8892d532 JJ |
120 | "%<constexpr%> function only available with " |
121 | "%<-std=c++2b%> or %<-std=gnu++2b%>", decl, type); | |
f1eac182 | 122 | explain_non_literal_class (type); |
9638f320 | 123 | decl = error_mark_node; |
f1eac182 | 124 | } |
60813a46 JM |
125 | cp_function_chain->invalid_constexpr = true; |
126 | } | |
2d76680f | 127 | } |
8e9589bd JM |
128 | else if (DECL_DECLARED_CONSTEXPR_P (decl) |
129 | && variably_modified_type_p (type, NULL_TREE)) | |
130 | { | |
f2935576 PC |
131 | error_at (DECL_SOURCE_LOCATION (decl), |
132 | "%<constexpr%> variable %qD has variably-modified " | |
133 | "type %qT", decl, type); | |
8e9589bd JM |
134 | decl = error_mark_node; |
135 | } | |
2d76680f PC |
136 | } |
137 | return decl; | |
138 | } | |
139 | ||
ca752f39 | 140 | struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef> |
2d76680f | 141 | { |
bfc139e2 NS |
142 | static hashval_t hash (const constexpr_fundef *); |
143 | static bool equal (const constexpr_fundef *, const constexpr_fundef *); | |
2d76680f PC |
144 | }; |
145 | ||
146 | /* This table holds all constexpr function definitions seen in | |
147 | the current translation unit. */ | |
148 | ||
149 | static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table; | |
150 | ||
151 | /* Utility function used for managing the constexpr function table. | |
152 | Return true if the entries pointed to by P and Q are for the | |
153 | same constexpr function. */ | |
154 | ||
155 | inline bool | |
bfc139e2 NS |
156 | constexpr_fundef_hasher::equal (const constexpr_fundef *lhs, |
157 | const constexpr_fundef *rhs) | |
2d76680f PC |
158 | { |
159 | return lhs->decl == rhs->decl; | |
160 | } | |
161 | ||
162 | /* Utility function used for managing the constexpr function table. | |
163 | Return a hash value for the entry pointed to by Q. */ | |
164 | ||
165 | inline hashval_t | |
bfc139e2 | 166 | constexpr_fundef_hasher::hash (const constexpr_fundef *fundef) |
2d76680f PC |
167 | { |
168 | return DECL_UID (fundef->decl); | |
169 | } | |
170 | ||
171 | /* Return a previously saved definition of function FUN. */ | |
172 | ||
bfc139e2 | 173 | constexpr_fundef * |
2d76680f PC |
174 | retrieve_constexpr_fundef (tree fun) |
175 | { | |
2d76680f PC |
176 | if (constexpr_fundef_table == NULL) |
177 | return NULL; | |
178 | ||
bfc139e2 | 179 | constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE }; |
2d76680f PC |
180 | return constexpr_fundef_table->find (&fundef); |
181 | } | |
182 | ||
183 | /* Check whether the parameter and return types of FUN are valid for a | |
184 | constexpr function, and complain if COMPLAIN. */ | |
185 | ||
98e5a19a | 186 | bool |
2d76680f PC |
187 | is_valid_constexpr_fn (tree fun, bool complain) |
188 | { | |
189 | bool ret = true; | |
190 | ||
31f7f784 | 191 | if (DECL_INHERITED_CTOR (fun) |
2d76680f PC |
192 | && TREE_CODE (fun) == TEMPLATE_DECL) |
193 | { | |
194 | ret = false; | |
195 | if (complain) | |
84fa214d | 196 | error ("inherited constructor %qD is not %<constexpr%>", |
31f7f784 | 197 | DECL_INHERITED_CTOR (fun)); |
2d76680f PC |
198 | } |
199 | else | |
200 | { | |
201 | for (tree parm = FUNCTION_FIRST_USER_PARM (fun); | |
202 | parm != NULL_TREE; parm = TREE_CHAIN (parm)) | |
203 | if (!literal_type_p (TREE_TYPE (parm))) | |
204 | { | |
205 | ret = false; | |
206 | if (complain) | |
207 | { | |
097f82ec | 208 | auto_diagnostic_group d; |
84fa214d | 209 | error ("invalid type for parameter %d of %<constexpr%> " |
2d76680f PC |
210 | "function %q+#D", DECL_PARM_INDEX (parm), fun); |
211 | explain_non_literal_class (TREE_TYPE (parm)); | |
212 | } | |
213 | } | |
214 | } | |
215 | ||
e1bea341 JM |
216 | if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17) |
217 | { | |
218 | ret = false; | |
219 | if (complain) | |
220 | inform (DECL_SOURCE_LOCATION (fun), | |
84fa214d | 221 | "lambdas are implicitly %<constexpr%> only in C++17 and later"); |
e1bea341 JM |
222 | } |
223 | else if (!DECL_CONSTRUCTOR_P (fun)) | |
2d76680f PC |
224 | { |
225 | tree rettype = TREE_TYPE (TREE_TYPE (fun)); | |
226 | if (!literal_type_p (rettype)) | |
227 | { | |
228 | ret = false; | |
229 | if (complain) | |
230 | { | |
097f82ec | 231 | auto_diagnostic_group d; |
84fa214d | 232 | error ("invalid return type %qT of %<constexpr%> function %q+D", |
2d76680f PC |
233 | rettype, fun); |
234 | explain_non_literal_class (rettype); | |
235 | } | |
236 | } | |
237 | ||
54069e59 JM |
238 | /* C++14 DR 1684 removed this restriction. */ |
239 | if (cxx_dialect < cxx14 | |
240 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) | |
2d76680f PC |
241 | && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun))) |
242 | { | |
243 | ret = false; | |
097f82ec DM |
244 | if (complain) |
245 | { | |
246 | auto_diagnostic_group d; | |
247 | if (pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic, | |
248 | "enclosing class of %<constexpr%> non-static" | |
249 | " member function %q+#D is not a literal type", | |
250 | fun)) | |
251 | explain_non_literal_class (DECL_CONTEXT (fun)); | |
252 | } | |
2d76680f PC |
253 | } |
254 | } | |
255 | else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun))) | |
256 | { | |
257 | ret = false; | |
258 | if (complain) | |
259 | error ("%q#T has virtual base classes", DECL_CONTEXT (fun)); | |
260 | } | |
261 | ||
262 | return ret; | |
263 | } | |
264 | ||
265 | /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF | |
266 | for a member of an anonymous aggregate, INIT is the initializer for that | |
267 | member, and VEC_OUTER is the vector of constructor elements for the class | |
268 | whose constructor we are processing. Add the initializer to the vector | |
269 | and return true to indicate success. */ | |
270 | ||
271 | static bool | |
272 | build_anon_member_initialization (tree member, tree init, | |
273 | vec<constructor_elt, va_gc> **vec_outer) | |
274 | { | |
275 | /* MEMBER presents the relevant fields from the inside out, but we need | |
276 | to build up the initializer from the outside in so that we can reuse | |
277 | previously built CONSTRUCTORs if this is, say, the second field in an | |
278 | anonymous struct. So we use a vec as a stack. */ | |
279 | auto_vec<tree, 2> fields; | |
280 | do | |
281 | { | |
282 | fields.safe_push (TREE_OPERAND (member, 1)); | |
283 | member = TREE_OPERAND (member, 0); | |
284 | } | |
285 | while (ANON_AGGR_TYPE_P (TREE_TYPE (member)) | |
286 | && TREE_CODE (member) == COMPONENT_REF); | |
287 | ||
288 | /* VEC has the constructor elements vector for the context of FIELD. | |
289 | If FIELD is an anonymous aggregate, we will push inside it. */ | |
290 | vec<constructor_elt, va_gc> **vec = vec_outer; | |
291 | tree field; | |
292 | while (field = fields.pop(), | |
293 | ANON_AGGR_TYPE_P (TREE_TYPE (field))) | |
294 | { | |
295 | tree ctor; | |
296 | /* If there is already an outer constructor entry for the anonymous | |
297 | aggregate FIELD, use it; otherwise, insert one. */ | |
298 | if (vec_safe_is_empty (*vec) | |
299 | || (*vec)->last().index != field) | |
300 | { | |
301 | ctor = build_constructor (TREE_TYPE (field), NULL); | |
302 | CONSTRUCTOR_APPEND_ELT (*vec, field, ctor); | |
303 | } | |
304 | else | |
305 | ctor = (*vec)->last().value; | |
306 | vec = &CONSTRUCTOR_ELTS (ctor); | |
307 | } | |
308 | ||
309 | /* Now we're at the innermost field, the one that isn't an anonymous | |
310 | aggregate. Add its initializer to the CONSTRUCTOR and we're done. */ | |
311 | gcc_assert (fields.is_empty()); | |
312 | CONSTRUCTOR_APPEND_ELT (*vec, field, init); | |
313 | ||
314 | return true; | |
315 | } | |
316 | ||
317 | /* Subroutine of build_constexpr_constructor_member_initializers. | |
318 | The expression tree T represents a data member initialization | |
319 | in a (constexpr) constructor definition. Build a pairing of | |
320 | the data member with its initializer, and prepend that pair | |
321 | to the existing initialization pair INITS. */ | |
322 | ||
323 | static bool | |
324 | build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec) | |
325 | { | |
326 | tree member, init; | |
327 | if (TREE_CODE (t) == CLEANUP_POINT_EXPR) | |
328 | t = TREE_OPERAND (t, 0); | |
329 | if (TREE_CODE (t) == EXPR_STMT) | |
330 | t = TREE_OPERAND (t, 0); | |
331 | if (t == error_mark_node) | |
332 | return false; | |
333 | if (TREE_CODE (t) == STATEMENT_LIST) | |
334 | { | |
0f54cc9c JM |
335 | for (tree stmt : tsi_range (t)) |
336 | if (! build_data_member_initialization (stmt, vec)) | |
337 | return false; | |
2d76680f PC |
338 | return true; |
339 | } | |
340 | if (TREE_CODE (t) == CLEANUP_STMT) | |
341 | { | |
342 | /* We can't see a CLEANUP_STMT in a constructor for a literal class, | |
343 | but we can in a constexpr constructor for a non-literal class. Just | |
344 | ignore it; either all the initialization will be constant, in which | |
345 | case the cleanup can't run, or it can't be constexpr. | |
346 | Still recurse into CLEANUP_BODY. */ | |
347 | return build_data_member_initialization (CLEANUP_BODY (t), vec); | |
348 | } | |
349 | if (TREE_CODE (t) == CONVERT_EXPR) | |
350 | t = TREE_OPERAND (t, 0); | |
351 | if (TREE_CODE (t) == INIT_EXPR | |
60813a46 JM |
352 | /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only |
353 | use what this function builds for cx_check_missing_mem_inits, and | |
354 | assignment in the ctor body doesn't count. */ | |
355 | || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR)) | |
2d76680f PC |
356 | { |
357 | member = TREE_OPERAND (t, 0); | |
358 | init = break_out_target_exprs (TREE_OPERAND (t, 1)); | |
359 | } | |
360 | else if (TREE_CODE (t) == CALL_EXPR) | |
361 | { | |
60813a46 JM |
362 | tree fn = get_callee_fndecl (t); |
363 | if (!fn || !DECL_CONSTRUCTOR_P (fn)) | |
364 | /* We're only interested in calls to subobject constructors. */ | |
365 | return true; | |
2d76680f PC |
366 | member = CALL_EXPR_ARG (t, 0); |
367 | /* We don't use build_cplus_new here because it complains about | |
368 | abstract bases. Leaving the call unwrapped means that it has the | |
369 | wrong type, but cxx_eval_constant_expression doesn't care. */ | |
370 | init = break_out_target_exprs (t); | |
371 | } | |
372 | else if (TREE_CODE (t) == BIND_EXPR) | |
373 | return build_data_member_initialization (BIND_EXPR_BODY (t), vec); | |
2d76680f | 374 | else |
60813a46 JM |
375 | /* Don't add anything else to the CONSTRUCTOR. */ |
376 | return true; | |
2d76680f PC |
377 | if (INDIRECT_REF_P (member)) |
378 | member = TREE_OPERAND (member, 0); | |
379 | if (TREE_CODE (member) == NOP_EXPR) | |
380 | { | |
381 | tree op = member; | |
382 | STRIP_NOPS (op); | |
383 | if (TREE_CODE (op) == ADDR_EXPR) | |
384 | { | |
385 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
386 | (TREE_TYPE (TREE_TYPE (op)), | |
387 | TREE_TYPE (TREE_TYPE (member)))); | |
388 | /* Initializing a cv-qualified member; we need to look through | |
389 | the const_cast. */ | |
390 | member = op; | |
391 | } | |
392 | else if (op == current_class_ptr | |
393 | && (same_type_ignoring_top_level_qualifiers_p | |
394 | (TREE_TYPE (TREE_TYPE (member)), | |
395 | current_class_type))) | |
396 | /* Delegating constructor. */ | |
397 | member = op; | |
398 | else | |
399 | { | |
400 | /* This is an initializer for an empty base; keep it for now so | |
401 | we can check it in cxx_eval_bare_aggregate. */ | |
402 | gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member)))); | |
403 | } | |
404 | } | |
405 | if (TREE_CODE (member) == ADDR_EXPR) | |
406 | member = TREE_OPERAND (member, 0); | |
407 | if (TREE_CODE (member) == COMPONENT_REF) | |
408 | { | |
409 | tree aggr = TREE_OPERAND (member, 0); | |
8cb7aaa1 JM |
410 | if (TREE_CODE (aggr) == VAR_DECL) |
411 | /* Initializing a local variable, don't add anything. */ | |
412 | return true; | |
2d76680f PC |
413 | if (TREE_CODE (aggr) != COMPONENT_REF) |
414 | /* Normal member initialization. */ | |
415 | member = TREE_OPERAND (member, 1); | |
416 | else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr))) | |
417 | /* Initializing a member of an anonymous union. */ | |
418 | return build_anon_member_initialization (member, init, vec); | |
419 | else | |
420 | /* We're initializing a vtable pointer in a base. Leave it as | |
421 | COMPONENT_REF so we remember the path to get to the vfield. */ | |
422 | gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node); | |
423 | } | |
424 | ||
49b5925f JM |
425 | /* Value-initialization can produce multiple initializers for the |
426 | same field; use the last one. */ | |
427 | if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member) | |
428 | (*vec)->last().value = init; | |
429 | else | |
430 | CONSTRUCTOR_APPEND_ELT (*vec, member, init); | |
2d76680f PC |
431 | return true; |
432 | } | |
433 | ||
434 | /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval. | |
435 | In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a | |
436 | BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */ | |
437 | ||
438 | static bool | |
439 | check_constexpr_bind_expr_vars (tree t) | |
440 | { | |
441 | gcc_assert (TREE_CODE (t) == BIND_EXPR); | |
442 | ||
2d76680f PC |
443 | for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var)) |
444 | if (TREE_CODE (var) == TYPE_DECL | |
4414e22e PC |
445 | && DECL_IMPLICIT_TYPEDEF_P (var) |
446 | && !LAMBDA_TYPE_P (TREE_TYPE (var))) | |
2d76680f PC |
447 | return false; |
448 | return true; | |
449 | } | |
450 | ||
451 | /* Subroutine of check_constexpr_ctor_body. */ | |
452 | ||
453 | static bool | |
454 | check_constexpr_ctor_body_1 (tree last, tree list) | |
455 | { | |
456 | switch (TREE_CODE (list)) | |
457 | { | |
458 | case DECL_EXPR: | |
a0008434 MP |
459 | if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL |
460 | || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL) | |
2d76680f | 461 | return true; |
2d76680f PC |
462 | return false; |
463 | ||
464 | case CLEANUP_POINT_EXPR: | |
465 | return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0), | |
466 | /*complain=*/false); | |
467 | ||
468 | case BIND_EXPR: | |
469 | if (!check_constexpr_bind_expr_vars (list) | |
470 | || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list), | |
471 | /*complain=*/false)) | |
472 | return false; | |
473 | return true; | |
474 | ||
475 | case USING_STMT: | |
476 | case STATIC_ASSERT: | |
96a95ac1 | 477 | case DEBUG_BEGIN_STMT: |
2d76680f PC |
478 | return true; |
479 | ||
480 | default: | |
481 | return false; | |
482 | } | |
483 | } | |
484 | ||
485 | /* Make sure that there are no statements after LAST in the constructor | |
486 | body represented by LIST. */ | |
487 | ||
488 | bool | |
489 | check_constexpr_ctor_body (tree last, tree list, bool complain) | |
490 | { | |
60813a46 JM |
491 | /* C++14 doesn't require a constexpr ctor to have an empty body. */ |
492 | if (cxx_dialect >= cxx14) | |
493 | return true; | |
494 | ||
2d76680f PC |
495 | bool ok = true; |
496 | if (TREE_CODE (list) == STATEMENT_LIST) | |
497 | { | |
498 | tree_stmt_iterator i = tsi_last (list); | |
499 | for (; !tsi_end_p (i); tsi_prev (&i)) | |
500 | { | |
501 | tree t = tsi_stmt (i); | |
502 | if (t == last) | |
503 | break; | |
504 | if (!check_constexpr_ctor_body_1 (last, t)) | |
505 | { | |
506 | ok = false; | |
507 | break; | |
508 | } | |
509 | } | |
510 | } | |
511 | else if (list != last | |
512 | && !check_constexpr_ctor_body_1 (last, list)) | |
513 | ok = false; | |
514 | if (!ok) | |
515 | { | |
516 | if (complain) | |
84fa214d | 517 | error ("%<constexpr%> constructor does not have empty body"); |
2d76680f PC |
518 | DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false; |
519 | } | |
520 | return ok; | |
521 | } | |
522 | ||
523 | /* V is a vector of constructor elements built up for the base and member | |
524 | initializers of a constructor for TYPE. They need to be in increasing | |
525 | offset order, which they might not be yet if TYPE has a primary base | |
526 | which is not first in the base-clause or a vptr and at least one base | |
527 | all of which are non-primary. */ | |
528 | ||
529 | static vec<constructor_elt, va_gc> * | |
530 | sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v) | |
531 | { | |
532 | tree pri = CLASSTYPE_PRIMARY_BINFO (type); | |
533 | tree field_type; | |
534 | unsigned i; | |
535 | constructor_elt *ce; | |
536 | ||
537 | if (pri) | |
538 | field_type = BINFO_TYPE (pri); | |
539 | else if (TYPE_CONTAINS_VPTR_P (type)) | |
540 | field_type = vtbl_ptr_type_node; | |
541 | else | |
542 | return v; | |
543 | ||
544 | /* Find the element for the primary base or vptr and move it to the | |
545 | beginning of the vec. */ | |
546 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
547 | if (TREE_TYPE (ce->index) == field_type) | |
548 | break; | |
549 | ||
550 | if (i > 0 && i < vec_safe_length (v)) | |
551 | { | |
552 | vec<constructor_elt, va_gc> &vref = *v; | |
553 | constructor_elt elt = vref[i]; | |
554 | for (; i > 0; --i) | |
555 | vref[i] = vref[i-1]; | |
556 | vref[0] = elt; | |
557 | } | |
558 | ||
559 | return v; | |
560 | } | |
561 | ||
562 | /* Build compile-time evalable representations of member-initializer list | |
563 | for a constexpr constructor. */ | |
564 | ||
565 | static tree | |
566 | build_constexpr_constructor_member_initializers (tree type, tree body) | |
567 | { | |
568 | vec<constructor_elt, va_gc> *vec = NULL; | |
569 | bool ok = true; | |
fa837fb6 JM |
570 | while (true) |
571 | switch (TREE_CODE (body)) | |
572 | { | |
573 | case MUST_NOT_THROW_EXPR: | |
574 | case EH_SPEC_BLOCK: | |
575 | body = TREE_OPERAND (body, 0); | |
576 | break; | |
577 | ||
578 | case STATEMENT_LIST: | |
0f54cc9c | 579 | for (tree stmt : tsi_range (body)) |
fa837fb6 | 580 | { |
0f54cc9c | 581 | body = stmt; |
fa837fb6 JM |
582 | if (TREE_CODE (body) == BIND_EXPR) |
583 | break; | |
584 | } | |
585 | break; | |
586 | ||
587 | case BIND_EXPR: | |
588 | body = BIND_EXPR_BODY (body); | |
589 | goto found; | |
590 | ||
591 | default: | |
592 | gcc_unreachable (); | |
58cc255c | 593 | } |
fa837fb6 | 594 | found: |
1259cb6d JJ |
595 | if (TREE_CODE (body) == TRY_BLOCK) |
596 | { | |
597 | body = TREE_OPERAND (body, 0); | |
598 | if (TREE_CODE (body) == BIND_EXPR) | |
599 | body = BIND_EXPR_BODY (body); | |
600 | } | |
2d76680f PC |
601 | if (TREE_CODE (body) == CLEANUP_POINT_EXPR) |
602 | { | |
603 | body = TREE_OPERAND (body, 0); | |
604 | if (TREE_CODE (body) == EXPR_STMT) | |
605 | body = TREE_OPERAND (body, 0); | |
606 | if (TREE_CODE (body) == INIT_EXPR | |
607 | && (same_type_ignoring_top_level_qualifiers_p | |
608 | (TREE_TYPE (TREE_OPERAND (body, 0)), | |
609 | current_class_type))) | |
610 | { | |
611 | /* Trivial copy. */ | |
612 | return TREE_OPERAND (body, 1); | |
613 | } | |
614 | ok = build_data_member_initialization (body, &vec); | |
615 | } | |
616 | else if (TREE_CODE (body) == STATEMENT_LIST) | |
617 | { | |
0f54cc9c | 618 | for (tree stmt : tsi_range (body)) |
2d76680f | 619 | { |
0f54cc9c | 620 | ok = build_data_member_initialization (stmt, &vec); |
2d76680f PC |
621 | if (!ok) |
622 | break; | |
623 | } | |
624 | } | |
2d76680f PC |
625 | else if (EXPR_P (body)) |
626 | ok = build_data_member_initialization (body, &vec); | |
627 | else | |
628 | gcc_assert (errorcount > 0); | |
629 | if (ok) | |
630 | { | |
631 | if (vec_safe_length (vec) > 0) | |
632 | { | |
633 | /* In a delegating constructor, return the target. */ | |
634 | constructor_elt *ce = &(*vec)[0]; | |
635 | if (ce->index == current_class_ptr) | |
636 | { | |
637 | body = ce->value; | |
638 | vec_free (vec); | |
639 | return body; | |
640 | } | |
641 | } | |
642 | vec = sort_constexpr_mem_initializers (type, vec); | |
643 | return build_constructor (type, vec); | |
644 | } | |
645 | else | |
646 | return error_mark_node; | |
647 | } | |
648 | ||
1b6fa695 ML |
649 | /* We have an expression tree T that represents a call, either CALL_EXPR |
650 | or AGGR_INIT_EXPR. If the call is lexically to a named function, | |
651 | retrun the _DECL for that function. */ | |
652 | ||
653 | static tree | |
654 | get_function_named_in_call (tree t) | |
655 | { | |
656 | tree fun = cp_get_callee (t); | |
657 | if (fun && TREE_CODE (fun) == ADDR_EXPR | |
658 | && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL) | |
659 | fun = TREE_OPERAND (fun, 0); | |
660 | return fun; | |
661 | } | |
662 | ||
bfc139e2 | 663 | /* Subroutine of check_constexpr_fundef. BODY is the body of a function |
2d76680f PC |
664 | declared to be constexpr, or a sub-statement thereof. Returns the |
665 | return value if suitable, error_mark_node for a statement not allowed in | |
666 | a constexpr function, or NULL_TREE if no return value was found. */ | |
667 | ||
5afef8b1 | 668 | tree |
2d76680f PC |
669 | constexpr_fn_retval (tree body) |
670 | { | |
671 | switch (TREE_CODE (body)) | |
672 | { | |
673 | case STATEMENT_LIST: | |
674 | { | |
2d76680f | 675 | tree expr = NULL_TREE; |
0f54cc9c | 676 | for (tree stmt : tsi_range (body)) |
2d76680f | 677 | { |
0f54cc9c | 678 | tree s = constexpr_fn_retval (stmt); |
2d76680f PC |
679 | if (s == error_mark_node) |
680 | return error_mark_node; | |
681 | else if (s == NULL_TREE) | |
682 | /* Keep iterating. */; | |
683 | else if (expr) | |
684 | /* Multiple return statements. */ | |
685 | return error_mark_node; | |
686 | else | |
687 | expr = s; | |
688 | } | |
689 | return expr; | |
690 | } | |
691 | ||
692 | case RETURN_EXPR: | |
693 | return break_out_target_exprs (TREE_OPERAND (body, 0)); | |
694 | ||
695 | case DECL_EXPR: | |
0162cb3b PC |
696 | { |
697 | tree decl = DECL_EXPR_DECL (body); | |
698 | if (TREE_CODE (decl) == USING_DECL | |
699 | /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */ | |
700 | || DECL_ARTIFICIAL (decl)) | |
701 | return NULL_TREE; | |
702 | return error_mark_node; | |
703 | } | |
2d76680f PC |
704 | |
705 | case CLEANUP_POINT_EXPR: | |
706 | return constexpr_fn_retval (TREE_OPERAND (body, 0)); | |
707 | ||
708 | case BIND_EXPR: | |
709 | if (!check_constexpr_bind_expr_vars (body)) | |
710 | return error_mark_node; | |
711 | return constexpr_fn_retval (BIND_EXPR_BODY (body)); | |
712 | ||
713 | case USING_STMT: | |
96a95ac1 | 714 | case DEBUG_BEGIN_STMT: |
2d76680f PC |
715 | return NULL_TREE; |
716 | ||
1b6fa695 ML |
717 | case CALL_EXPR: |
718 | { | |
719 | tree fun = get_function_named_in_call (body); | |
720 | if (fun != NULL_TREE | |
3d78e008 | 721 | && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE)) |
1b6fa695 ML |
722 | return NULL_TREE; |
723 | } | |
724 | /* Fallthru. */ | |
725 | ||
2d76680f PC |
726 | default: |
727 | return error_mark_node; | |
728 | } | |
729 | } | |
730 | ||
bfc139e2 | 731 | /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of |
2d76680f PC |
732 | FUN; do the necessary transformations to turn it into a single expression |
733 | that we can store in the hash table. */ | |
734 | ||
735 | static tree | |
736 | massage_constexpr_body (tree fun, tree body) | |
737 | { | |
738 | if (DECL_CONSTRUCTOR_P (fun)) | |
739 | body = build_constexpr_constructor_member_initializers | |
740 | (DECL_CONTEXT (fun), body); | |
60813a46 | 741 | else if (cxx_dialect < cxx14) |
2d76680f PC |
742 | { |
743 | if (TREE_CODE (body) == EH_SPEC_BLOCK) | |
744 | body = EH_SPEC_STMTS (body); | |
745 | if (TREE_CODE (body) == MUST_NOT_THROW_EXPR) | |
746 | body = TREE_OPERAND (body, 0); | |
747 | body = constexpr_fn_retval (body); | |
748 | } | |
749 | return body; | |
750 | } | |
751 | ||
3e4b91f2 NS |
752 | /* CTYPE is a type constructed from BODY. Return true if some |
753 | bases/fields are uninitialized, and complain if COMPLAIN. */ | |
2d76680f PC |
754 | |
755 | static bool | |
3e4b91f2 | 756 | cx_check_missing_mem_inits (tree ctype, tree body, bool complain) |
2d76680f | 757 | { |
7906797e | 758 | /* We allow uninitialized bases/fields in C++20. */ |
b04445d4 | 759 | if (cxx_dialect >= cxx20) |
7906797e MP |
760 | return false; |
761 | ||
3e4b91f2 NS |
762 | unsigned nelts = 0; |
763 | ||
764 | if (body) | |
765 | { | |
766 | if (TREE_CODE (body) != CONSTRUCTOR) | |
767 | return false; | |
768 | nelts = CONSTRUCTOR_NELTS (body); | |
769 | } | |
770 | tree field = TYPE_FIELDS (ctype); | |
2d76680f PC |
771 | |
772 | if (TREE_CODE (ctype) == UNION_TYPE) | |
773 | { | |
774 | if (nelts == 0 && next_initializable_field (field)) | |
775 | { | |
776 | if (complain) | |
777 | error ("%<constexpr%> constructor for union %qT must " | |
778 | "initialize exactly one non-static data member", ctype); | |
779 | return true; | |
780 | } | |
781 | return false; | |
782 | } | |
783 | ||
3e4b91f2 NS |
784 | /* Iterate over the CONSTRUCTOR, checking any missing fields don't |
785 | need an explicit initialization. */ | |
786 | bool bad = false; | |
787 | for (unsigned i = 0; i <= nelts; ++i) | |
2d76680f | 788 | { |
3e4b91f2 NS |
789 | tree index = NULL_TREE; |
790 | if (i < nelts) | |
2d76680f PC |
791 | { |
792 | index = CONSTRUCTOR_ELT (body, i)->index; | |
793 | /* Skip base and vtable inits. */ | |
794 | if (TREE_CODE (index) != FIELD_DECL | |
795 | || DECL_ARTIFICIAL (index)) | |
796 | continue; | |
797 | } | |
3e4b91f2 | 798 | |
2d76680f PC |
799 | for (; field != index; field = DECL_CHAIN (field)) |
800 | { | |
801 | tree ftype; | |
3e4b91f2 NS |
802 | if (TREE_CODE (field) != FIELD_DECL) |
803 | continue; | |
7c30b12a | 804 | if (DECL_UNNAMED_BIT_FIELD (field)) |
2d76680f | 805 | continue; |
3e4b91f2 NS |
806 | if (DECL_ARTIFICIAL (field)) |
807 | continue; | |
808 | if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) | |
809 | { | |
7906797e | 810 | /* Recurse to check the anonymous aggregate member. */ |
3e4b91f2 NS |
811 | bad |= cx_check_missing_mem_inits |
812 | (TREE_TYPE (field), NULL_TREE, complain); | |
813 | if (bad && !complain) | |
814 | return true; | |
815 | continue; | |
816 | } | |
a1c9c9ff JM |
817 | ftype = TREE_TYPE (field); |
818 | if (!ftype || !TYPE_P (ftype) || !COMPLETE_TYPE_P (ftype)) | |
819 | /* A flexible array can't be intialized here, so don't complain | |
820 | that it isn't. */ | |
821 | continue; | |
a4dfd0f0 | 822 | if (is_empty_field (field)) |
6876b269 JM |
823 | /* An empty field doesn't need an initializer. */ |
824 | continue; | |
a1c9c9ff | 825 | ftype = strip_array_types (ftype); |
2d76680f PC |
826 | if (type_has_constexpr_default_constructor (ftype)) |
827 | { | |
828 | /* It's OK to skip a member with a trivial constexpr ctor. | |
829 | A constexpr ctor that isn't trivial should have been | |
830 | added in by now. */ | |
831 | gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype) | |
832 | || errorcount != 0); | |
833 | continue; | |
834 | } | |
835 | if (!complain) | |
836 | return true; | |
097f82ec | 837 | auto_diagnostic_group d; |
b8cd3996 JM |
838 | error ("member %qD must be initialized by mem-initializer " |
839 | "in %<constexpr%> constructor", field); | |
840 | inform (DECL_SOURCE_LOCATION (field), "declared here"); | |
2d76680f PC |
841 | bad = true; |
842 | } | |
843 | if (field == NULL_TREE) | |
844 | break; | |
3e4b91f2 NS |
845 | |
846 | if (ANON_AGGR_TYPE_P (TREE_TYPE (index))) | |
847 | { | |
848 | /* Check the anonymous aggregate initializer is valid. */ | |
849 | bad |= cx_check_missing_mem_inits | |
850 | (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain); | |
851 | if (bad && !complain) | |
852 | return true; | |
853 | } | |
2d76680f PC |
854 | field = DECL_CHAIN (field); |
855 | } | |
856 | ||
857 | return bad; | |
858 | } | |
859 | ||
860 | /* We are processing the definition of the constexpr function FUN. | |
bfc139e2 NS |
861 | Check that its body fulfills the apropriate requirements and |
862 | enter it in the constexpr function definition table. */ | |
2d76680f | 863 | |
bfc139e2 NS |
864 | void |
865 | maybe_save_constexpr_fundef (tree fun) | |
2d76680f | 866 | { |
bfc139e2 NS |
867 | if (processing_template_decl |
868 | || !DECL_DECLARED_CONSTEXPR_P (fun) | |
869 | || cp_function_chain->invalid_constexpr | |
81f97181 | 870 | || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))) |
bfc139e2 | 871 | return; |
2d76680f PC |
872 | |
873 | if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun))) | |
bfc139e2 | 874 | return; |
2d76680f | 875 | |
bfc139e2 | 876 | tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun)); |
ca30abcd | 877 | if (massaged == NULL_TREE || massaged == error_mark_node) |
2d76680f PC |
878 | { |
879 | if (!DECL_CONSTRUCTOR_P (fun)) | |
84fa214d ML |
880 | error ("body of %<constexpr%> function %qD not a return-statement", |
881 | fun); | |
bfc139e2 | 882 | return; |
2d76680f PC |
883 | } |
884 | ||
4f05d85a JM |
885 | bool potential = potential_rvalue_constant_expression (massaged); |
886 | if (!potential && !DECL_GENERATED_P (fun)) | |
887 | require_potential_rvalue_constant_expression (massaged); | |
2d76680f PC |
888 | |
889 | if (DECL_CONSTRUCTOR_P (fun) | |
3e4b91f2 NS |
890 | && cx_check_missing_mem_inits (DECL_CONTEXT (fun), |
891 | massaged, !DECL_GENERATED_P (fun))) | |
4f05d85a JM |
892 | potential = false; |
893 | ||
894 | if (!potential && !DECL_GENERATED_P (fun)) | |
bfc139e2 | 895 | return; |
2d76680f | 896 | |
bfc139e2 | 897 | constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE}; |
43574e4f | 898 | bool clear_ctx = false; |
43574e4f JJ |
899 | if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE) |
900 | { | |
901 | clear_ctx = true; | |
902 | DECL_CONTEXT (DECL_RESULT (fun)) = fun; | |
903 | } | |
bfc139e2 NS |
904 | tree saved_fn = current_function_decl; |
905 | current_function_decl = fun; | |
906 | entry.body = copy_fn (entry.decl, entry.parms, entry.result); | |
43574e4f | 907 | current_function_decl = saved_fn; |
43574e4f | 908 | if (clear_ctx) |
bfc139e2 | 909 | DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE; |
4f05d85a JM |
910 | if (!potential) |
911 | /* For a template instantiation, we want to remember the pre-generic body | |
912 | for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression | |
913 | that it doesn't need to bother trying to expand the function. */ | |
914 | entry.result = error_mark_node; | |
915 | ||
bfc139e2 NS |
916 | register_constexpr_fundef (entry); |
917 | } | |
918 | ||
919 | /* BODY is a validated and massaged definition of a constexpr | |
920 | function. Register it in the hash table. */ | |
921 | ||
922 | void | |
923 | register_constexpr_fundef (const constexpr_fundef &value) | |
924 | { | |
925 | /* Create the constexpr function table if necessary. */ | |
926 | if (constexpr_fundef_table == NULL) | |
927 | constexpr_fundef_table | |
928 | = hash_table<constexpr_fundef_hasher>::create_ggc (101); | |
929 | ||
930 | constexpr_fundef **slot = constexpr_fundef_table->find_slot | |
931 | (const_cast<constexpr_fundef *> (&value), INSERT); | |
932 | ||
2d76680f PC |
933 | gcc_assert (*slot == NULL); |
934 | *slot = ggc_alloc<constexpr_fundef> (); | |
bfc139e2 | 935 | **slot = value; |
2d76680f PC |
936 | } |
937 | ||
938 | /* FUN is a non-constexpr function called in a context that requires a | |
939 | constant expression. If it comes from a constexpr template, explain why | |
940 | the instantiation isn't constexpr. */ | |
941 | ||
942 | void | |
943 | explain_invalid_constexpr_fn (tree fun) | |
944 | { | |
945 | static hash_set<tree> *diagnosed; | |
946 | tree body; | |
98e5a19a | 947 | /* Only diagnose defaulted functions, lambdas, or instantiations. */ |
2d76680f | 948 | if (!DECL_DEFAULTED_FN (fun) |
98e5a19a | 949 | && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) |
2d76680f | 950 | && !is_instantiation_of_constexpr (fun)) |
f22f817c JM |
951 | { |
952 | inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun); | |
953 | return; | |
954 | } | |
2d76680f PC |
955 | if (diagnosed == NULL) |
956 | diagnosed = new hash_set<tree>; | |
957 | if (diagnosed->add (fun)) | |
958 | /* Already explained. */ | |
959 | return; | |
960 | ||
17dc903e | 961 | iloc_sentinel ils = input_location; |
98e5a19a JM |
962 | if (!lambda_static_thunk_p (fun)) |
963 | { | |
964 | /* Diagnostics should completely ignore the static thunk, so leave | |
965 | input_location set to our caller's location. */ | |
966 | input_location = DECL_SOURCE_LOCATION (fun); | |
967 | inform (input_location, | |
84fa214d | 968 | "%qD is not usable as a %<constexpr%> function because:", fun); |
98e5a19a | 969 | } |
2d76680f PC |
970 | /* First check the declaration. */ |
971 | if (is_valid_constexpr_fn (fun, true)) | |
972 | { | |
973 | /* Then if it's OK, the body. */ | |
98e5a19a | 974 | if (!DECL_DECLARED_CONSTEXPR_P (fun) |
4f05d85a | 975 | && DECL_DEFAULTED_FN (fun)) |
2d76680f PC |
976 | explain_implicit_non_constexpr (fun); |
977 | else | |
978 | { | |
4f05d85a JM |
979 | if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun)) |
980 | body = fd->body; | |
981 | else | |
982 | body = DECL_SAVED_TREE (fun); | |
983 | body = massage_constexpr_body (fun, body); | |
2d76680f PC |
984 | require_potential_rvalue_constant_expression (body); |
985 | if (DECL_CONSTRUCTOR_P (fun)) | |
3e4b91f2 | 986 | cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true); |
2d76680f PC |
987 | } |
988 | } | |
2d76680f PC |
989 | } |
990 | ||
991 | /* Objects of this type represent calls to constexpr functions | |
992 | along with the bindings of parameters to their arguments, for | |
993 | the purpose of compile time evaluation. */ | |
994 | ||
995 | struct GTY((for_user)) constexpr_call { | |
996 | /* Description of the constexpr function definition. */ | |
997 | constexpr_fundef *fundef; | |
3c961dc7 | 998 | /* Parameter bindings environment. A TREE_VEC of arguments. */ |
2d76680f PC |
999 | tree bindings; |
1000 | /* Result of the call. | |
1001 | NULL means the call is being evaluated. | |
1002 | error_mark_node means that the evaluation was erroneous; | |
1003 | otherwise, the actuall value of the call. */ | |
1004 | tree result; | |
1005 | /* The hash of this call; we remember it here to avoid having to | |
1006 | recalculate it when expanding the hash table. */ | |
1007 | hashval_t hash; | |
cce3ae91 | 1008 | /* Whether __builtin_is_constant_evaluated() should evaluate to true. */ |
13de99bc | 1009 | bool manifestly_const_eval; |
2d76680f PC |
1010 | }; |
1011 | ||
ca752f39 | 1012 | struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call> |
2d76680f PC |
1013 | { |
1014 | static hashval_t hash (constexpr_call *); | |
1015 | static bool equal (constexpr_call *, constexpr_call *); | |
3e605b20 JM |
1016 | }; |
1017 | ||
4b390698 JJ |
1018 | enum constexpr_switch_state { |
1019 | /* Used when processing a switch for the first time by cxx_eval_switch_expr | |
1020 | and default: label for that switch has not been seen yet. */ | |
1021 | css_default_not_seen, | |
1022 | /* Used when processing a switch for the first time by cxx_eval_switch_expr | |
1023 | and default: label for that switch has been seen already. */ | |
1024 | css_default_seen, | |
1025 | /* Used when processing a switch for the second time by | |
1026 | cxx_eval_switch_expr, where default: label should match. */ | |
1027 | css_default_processing | |
1028 | }; | |
1029 | ||
8e007055 JJ |
1030 | /* The constexpr expansion context part which needs one instance per |
1031 | cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of | |
1032 | variables initialized within the expression. */ | |
1033 | ||
1034 | struct constexpr_global_ctx { | |
1035 | /* Values for any temporaries or local variables within the | |
1036 | constant-expression. */ | |
1037 | hash_map<tree,tree> values; | |
1038 | /* Number of cxx_eval_constant_expression calls (except skipped ones, | |
1039 | on simple constants or location wrappers) encountered during current | |
1040 | cxx_eval_outermost_constant_expr call. */ | |
1041 | HOST_WIDE_INT constexpr_ops_count; | |
1042 | /* Heap VAR_DECLs created during the evaluation of the outermost constant | |
1043 | expression. */ | |
1044 | auto_vec<tree, 16> heap_vars; | |
ee1de08d JJ |
1045 | /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */ |
1046 | vec<tree> *cleanups; | |
f74f6092 JJ |
1047 | /* Number of heap VAR_DECL deallocations. */ |
1048 | unsigned heap_dealloc_count; | |
8e007055 | 1049 | /* Constructor. */ |
f74f6092 JJ |
1050 | constexpr_global_ctx () |
1051 | : constexpr_ops_count (0), cleanups (NULL), heap_dealloc_count (0) {} | |
8e007055 JJ |
1052 | }; |
1053 | ||
3e605b20 JM |
1054 | /* The constexpr expansion context. CALL is the current function |
1055 | expansion, CTOR is the current aggregate initializer, OBJECT is the | |
8e007055 | 1056 | object being initialized by CTOR, either a VAR_DECL or a _REF. */ |
3e605b20 JM |
1057 | |
1058 | struct constexpr_ctx { | |
8e007055 JJ |
1059 | /* The part of the context that needs to be unique to the whole |
1060 | cxx_eval_outermost_constant_expr invocation. */ | |
1061 | constexpr_global_ctx *global; | |
13f649f6 | 1062 | /* The innermost call we're evaluating. */ |
3e605b20 | 1063 | constexpr_call *call; |
ee1de08d JJ |
1064 | /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen |
1065 | within the current LOOP_EXPR. NULL if we aren't inside a loop. */ | |
0ee28590 | 1066 | vec<tree> *save_exprs; |
13f649f6 JM |
1067 | /* The CONSTRUCTOR we're currently building up for an aggregate |
1068 | initializer. */ | |
3e605b20 | 1069 | tree ctor; |
13f649f6 | 1070 | /* The object we're building the CONSTRUCTOR for. */ |
3e605b20 | 1071 | tree object; |
4b390698 JJ |
1072 | /* If inside SWITCH_EXPR. */ |
1073 | constexpr_switch_state *css_state; | |
49a86fce PP |
1074 | /* The aggregate initialization context inside which this one is nested. This |
1075 | is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */ | |
1076 | const constexpr_ctx *parent; | |
a15ffa22 | 1077 | |
aaa26bf4 JM |
1078 | /* Whether we should error on a non-constant expression or fail quietly. |
1079 | This flag needs to be here, but some of the others could move to global | |
1080 | if they get larger than a word. */ | |
2b3ab879 | 1081 | bool quiet; |
13f649f6 JM |
1082 | /* Whether we are strictly conforming to constant expression rules or |
1083 | trying harder to get a constant value. */ | |
69eb4fde | 1084 | bool strict; |
e4082611 | 1085 | /* Whether __builtin_is_constant_evaluated () should be true. */ |
13de99bc | 1086 | bool manifestly_const_eval; |
3e605b20 | 1087 | }; |
2d76680f | 1088 | |
f65a3299 PP |
1089 | /* This internal flag controls whether we should avoid doing anything during |
1090 | constexpr evaluation that would cause extra DECL_UID generation, such as | |
1091 | template instantiation and function body copying. */ | |
1092 | ||
1093 | static bool uid_sensitive_constexpr_evaluation_value; | |
1094 | ||
1095 | /* An internal counter that keeps track of the number of times | |
1096 | uid_sensitive_constexpr_evaluation_p returned true. */ | |
1097 | ||
1098 | static unsigned uid_sensitive_constexpr_evaluation_true_counter; | |
1099 | ||
1100 | /* The accessor for uid_sensitive_constexpr_evaluation_value which also | |
1101 | increments the corresponding counter. */ | |
1102 | ||
1103 | static bool | |
1104 | uid_sensitive_constexpr_evaluation_p () | |
1105 | { | |
1106 | if (uid_sensitive_constexpr_evaluation_value) | |
1107 | { | |
1108 | ++uid_sensitive_constexpr_evaluation_true_counter; | |
1109 | return true; | |
1110 | } | |
1111 | else | |
1112 | return false; | |
1113 | } | |
1114 | ||
1115 | /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel | |
1116 | enables the internal flag for uid_sensitive_constexpr_evaluation_p | |
1117 | during the lifetime of the sentinel object. Upon its destruction, the | |
1118 | previous value of uid_sensitive_constexpr_evaluation_p is restored. */ | |
1119 | ||
1120 | uid_sensitive_constexpr_evaluation_sentinel | |
1121 | ::uid_sensitive_constexpr_evaluation_sentinel () | |
1122 | : ovr (uid_sensitive_constexpr_evaluation_value, true) | |
1123 | { | |
1124 | } | |
1125 | ||
1126 | /* The default constructor for uid_sensitive_constexpr_evaluation_checker | |
1127 | records the current number of times that uid_sensitive_constexpr_evaluation_p | |
1128 | has been called and returned true. */ | |
1129 | ||
1130 | uid_sensitive_constexpr_evaluation_checker | |
1131 | ::uid_sensitive_constexpr_evaluation_checker () | |
1132 | : saved_counter (uid_sensitive_constexpr_evaluation_true_counter) | |
1133 | { | |
1134 | } | |
1135 | ||
1136 | /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and | |
1137 | some constexpr evaluation was restricted due to u_s_c_e_p being called | |
1138 | and returning true during the lifetime of this checker object. */ | |
1139 | ||
1140 | bool | |
1141 | uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const | |
1142 | { | |
1143 | return (uid_sensitive_constexpr_evaluation_value | |
1144 | && saved_counter != uid_sensitive_constexpr_evaluation_true_counter); | |
1145 | } | |
1146 | ||
1147 | ||
2d76680f PC |
1148 | /* A table of all constexpr calls that have been evaluated by the |
1149 | compiler in this translation unit. */ | |
1150 | ||
97f3003f | 1151 | static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table; |
2d76680f | 1152 | |
3e605b20 | 1153 | static tree cxx_eval_constant_expression (const constexpr_ctx *, tree, |
5a804683 | 1154 | bool, bool *, bool *, tree * = NULL); |
2d76680f PC |
1155 | |
1156 | /* Compute a hash value for a constexpr call representation. */ | |
1157 | ||
1158 | inline hashval_t | |
1159 | constexpr_call_hasher::hash (constexpr_call *info) | |
1160 | { | |
1161 | return info->hash; | |
1162 | } | |
1163 | ||
1164 | /* Return true if the objects pointed to by P and Q represent calls | |
1165 | to the same constexpr function with the same arguments. | |
1166 | Otherwise, return false. */ | |
1167 | ||
1168 | bool | |
1169 | constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs) | |
1170 | { | |
2d76680f | 1171 | if (lhs == rhs) |
46cf7fa1 JJ |
1172 | return true; |
1173 | if (lhs->hash != rhs->hash) | |
1174 | return false; | |
13de99bc | 1175 | if (lhs->manifestly_const_eval != rhs->manifestly_const_eval) |
cce3ae91 | 1176 | return false; |
2d76680f | 1177 | if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef)) |
46cf7fa1 | 1178 | return false; |
3c961dc7 | 1179 | return cp_tree_equal (lhs->bindings, rhs->bindings); |
2d76680f PC |
1180 | } |
1181 | ||
1182 | /* Initialize the constexpr call table, if needed. */ | |
1183 | ||
1184 | static void | |
1185 | maybe_initialize_constexpr_call_table (void) | |
1186 | { | |
1187 | if (constexpr_call_table == NULL) | |
1188 | constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101); | |
1189 | } | |
1190 | ||
c0daf32d PP |
1191 | /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when |
1192 | a function happens to get called recursively, we unshare the callee | |
1193 | function's body and evaluate this unshared copy instead of evaluating the | |
1194 | original body. | |
1195 | ||
1196 | FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function | |
1197 | copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map | |
97f3003f JM |
1198 | that's keyed off of the original FUNCTION_DECL and whose value is a |
1199 | TREE_LIST of this function's unused copies awaiting reuse. | |
c0daf32d | 1200 | |
97f3003f | 1201 | This is not GC-deletable to avoid GC affecting UID generation. */ |
c0daf32d | 1202 | |
ba6730bd | 1203 | static GTY(()) decl_tree_map *fundef_copies_table; |
c0daf32d | 1204 | |
c0daf32d | 1205 | /* Reuse a copy or create a new unshared copy of the function FUN. |
3c98ff9b NS |
1206 | Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE |
1207 | is parms, TYPE is result. */ | |
c0daf32d | 1208 | |
97f3003f | 1209 | static tree |
f65a3299 | 1210 | get_fundef_copy (constexpr_fundef *fundef) |
c0daf32d | 1211 | { |
97f3003f | 1212 | tree copy; |
3c98ff9b | 1213 | bool existed; |
c89844e5 JM |
1214 | tree *slot = &(hash_map_safe_get_or_insert<hm_ggc> |
1215 | (fundef_copies_table, fundef->decl, &existed, 127)); | |
3c98ff9b NS |
1216 | |
1217 | if (!existed) | |
c0daf32d | 1218 | { |
3c98ff9b NS |
1219 | /* There is no cached function available, or in use. We can use |
1220 | the function directly. That the slot is now created records | |
1221 | that this function is now in use. */ | |
43574e4f JJ |
1222 | copy = build_tree_list (fundef->body, fundef->parms); |
1223 | TREE_TYPE (copy) = fundef->result; | |
3c98ff9b NS |
1224 | } |
1225 | else if (*slot == NULL_TREE) | |
1226 | { | |
f65a3299 | 1227 | if (uid_sensitive_constexpr_evaluation_p ()) |
aaa26bf4 JM |
1228 | return NULL_TREE; |
1229 | ||
3c98ff9b | 1230 | /* We've already used the function itself, so make a copy. */ |
97f3003f | 1231 | copy = build_tree_list (NULL, NULL); |
43574e4f JJ |
1232 | tree saved_body = DECL_SAVED_TREE (fundef->decl); |
1233 | tree saved_parms = DECL_ARGUMENTS (fundef->decl); | |
1234 | tree saved_result = DECL_RESULT (fundef->decl); | |
1235 | tree saved_fn = current_function_decl; | |
1236 | DECL_SAVED_TREE (fundef->decl) = fundef->body; | |
1237 | DECL_ARGUMENTS (fundef->decl) = fundef->parms; | |
1238 | DECL_RESULT (fundef->decl) = fundef->result; | |
1239 | current_function_decl = fundef->decl; | |
1240 | TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy), | |
1241 | TREE_TYPE (copy)); | |
1242 | current_function_decl = saved_fn; | |
1243 | DECL_RESULT (fundef->decl) = saved_result; | |
1244 | DECL_ARGUMENTS (fundef->decl) = saved_parms; | |
1245 | DECL_SAVED_TREE (fundef->decl) = saved_body; | |
c0daf32d PP |
1246 | } |
1247 | else | |
1248 | { | |
3c98ff9b | 1249 | /* We have a cached function available. */ |
c0daf32d | 1250 | copy = *slot; |
97f3003f | 1251 | *slot = TREE_CHAIN (copy); |
c0daf32d PP |
1252 | } |
1253 | ||
1254 | return copy; | |
1255 | } | |
1256 | ||
3c98ff9b NS |
1257 | /* Save the copy COPY of function FUN for later reuse by |
1258 | get_fundef_copy(). By construction, there will always be an entry | |
1259 | to find. */ | |
c0daf32d PP |
1260 | |
1261 | static void | |
97f3003f | 1262 | save_fundef_copy (tree fun, tree copy) |
c0daf32d | 1263 | { |
3c98ff9b | 1264 | tree *slot = fundef_copies_table->get (fun); |
97f3003f | 1265 | TREE_CHAIN (copy) = *slot; |
c0daf32d PP |
1266 | *slot = copy; |
1267 | } | |
1268 | ||
2d76680f PC |
1269 | /* We have an expression tree T that represents a call, either CALL_EXPR |
1270 | or AGGR_INIT_EXPR. Return the Nth argument. */ | |
1271 | ||
1272 | static inline tree | |
1273 | get_nth_callarg (tree t, int n) | |
1274 | { | |
1275 | switch (TREE_CODE (t)) | |
1276 | { | |
1277 | case CALL_EXPR: | |
1278 | return CALL_EXPR_ARG (t, n); | |
1279 | ||
1280 | case AGGR_INIT_EXPR: | |
1281 | return AGGR_INIT_EXPR_ARG (t, n); | |
1282 | ||
1283 | default: | |
1284 | gcc_unreachable (); | |
1285 | return NULL; | |
1286 | } | |
1287 | } | |
1288 | ||
2d76680f PC |
1289 | /* Attempt to evaluate T which represents a call to a builtin function. |
1290 | We assume here that all builtin functions evaluate to scalar types | |
1291 | represented by _CST nodes. */ | |
1292 | ||
1293 | static tree | |
5756d0f9 | 1294 | cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun, |
92a596e8 | 1295 | bool lval, |
2d76680f PC |
1296 | bool *non_constant_p, bool *overflow_p) |
1297 | { | |
1298 | const int nargs = call_expr_nargs (t); | |
1299 | tree *args = (tree *) alloca (nargs * sizeof (tree)); | |
1300 | tree new_call; | |
1301 | int i; | |
5756d0f9 JM |
1302 | |
1303 | /* Don't fold __builtin_constant_p within a constexpr function. */ | |
e4082611 | 1304 | bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun); |
b925d25d | 1305 | |
fe736ffd JM |
1306 | /* If we aren't requiring a constant expression, defer __builtin_constant_p |
1307 | in a constexpr function until we have values for the parameters. */ | |
b925d25d | 1308 | if (bi_const_p |
4cd3e7df | 1309 | && !ctx->manifestly_const_eval |
5756d0f9 JM |
1310 | && current_function_decl |
1311 | && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) | |
2d76680f | 1312 | { |
5756d0f9 JM |
1313 | *non_constant_p = true; |
1314 | return t; | |
2d76680f | 1315 | } |
5756d0f9 | 1316 | |
e4082611 | 1317 | /* For __builtin_is_constant_evaluated, defer it if not |
117c6426 JJ |
1318 | ctx->manifestly_const_eval (as sometimes we try to constant evaluate |
1319 | without manifestly_const_eval even expressions or parts thereof which | |
1320 | will later be manifestly const_eval evaluated), otherwise fold it to | |
1321 | true. */ | |
3d78e008 | 1322 | if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED, |
13de99bc | 1323 | BUILT_IN_FRONTEND)) |
e4082611 | 1324 | { |
13de99bc | 1325 | if (!ctx->manifestly_const_eval) |
e4082611 JJ |
1326 | { |
1327 | *non_constant_p = true; | |
1328 | return t; | |
1329 | } | |
1330 | return boolean_true_node; | |
1331 | } | |
1332 | ||
ff603745 | 1333 | if (fndecl_built_in_p (fun, CP_BUILT_IN_SOURCE_LOCATION, BUILT_IN_FRONTEND)) |
ba3d8dff JJ |
1334 | { |
1335 | temp_override<tree> ovr (current_function_decl); | |
1336 | if (ctx->call && ctx->call->fundef) | |
1337 | current_function_decl = ctx->call->fundef->decl; | |
1338 | return fold_builtin_source_location (EXPR_LOCATION (t)); | |
1339 | } | |
ff603745 | 1340 | |
69dc042f JM |
1341 | int strops = 0; |
1342 | int strret = 0; | |
1343 | if (fndecl_built_in_p (fun, BUILT_IN_NORMAL)) | |
1344 | switch (DECL_FUNCTION_CODE (fun)) | |
1345 | { | |
1346 | case BUILT_IN_STRLEN: | |
1347 | case BUILT_IN_STRNLEN: | |
1348 | strops = 1; | |
1349 | break; | |
1350 | case BUILT_IN_MEMCHR: | |
1351 | case BUILT_IN_STRCHR: | |
1352 | case BUILT_IN_STRRCHR: | |
1353 | strops = 1; | |
1354 | strret = 1; | |
1355 | break; | |
1356 | case BUILT_IN_MEMCMP: | |
1357 | case BUILT_IN_STRCMP: | |
1358 | strops = 2; | |
1359 | break; | |
1360 | case BUILT_IN_STRSTR: | |
1361 | strops = 2; | |
1362 | strret = 1; | |
bc13106e JJ |
1363 | break; |
1364 | case BUILT_IN_ASAN_POINTER_COMPARE: | |
1365 | case BUILT_IN_ASAN_POINTER_SUBTRACT: | |
1366 | /* These builtins shall be ignored during constant expression | |
1367 | evaluation. */ | |
1368 | return void_node; | |
69dc042f JM |
1369 | default: |
1370 | break; | |
1371 | } | |
1372 | ||
5756d0f9 JM |
1373 | /* Be permissive for arguments to built-ins; __builtin_constant_p should |
1374 | return constant false for a non-constant argument. */ | |
1375 | constexpr_ctx new_ctx = *ctx; | |
1376 | new_ctx.quiet = true; | |
5756d0f9 | 1377 | for (i = 0; i < nargs; ++i) |
b925d25d | 1378 | { |
69dc042f | 1379 | tree arg = CALL_EXPR_ARG (t, i); |
6f346913 | 1380 | tree oarg = arg; |
69dc042f JM |
1381 | |
1382 | /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since | |
1383 | expand_builtin doesn't know how to look in the values table. */ | |
1384 | bool strop = i < strops; | |
1385 | if (strop) | |
1386 | { | |
1387 | STRIP_NOPS (arg); | |
1388 | if (TREE_CODE (arg) == ADDR_EXPR) | |
1389 | arg = TREE_OPERAND (arg, 0); | |
1390 | else | |
1391 | strop = false; | |
1392 | } | |
1393 | ||
2ff7172a JJ |
1394 | /* If builtin_valid_in_constant_expr_p is true, |
1395 | potential_constant_expression_1 has not recursed into the arguments | |
1396 | of the builtin, verify it here. */ | |
1397 | if (!builtin_valid_in_constant_expr_p (fun) | |
69dc042f | 1398 | || potential_constant_expression (arg)) |
4cd3e7df JJ |
1399 | { |
1400 | bool dummy1 = false, dummy2 = false; | |
69dc042f JM |
1401 | arg = cxx_eval_constant_expression (&new_ctx, arg, false, |
1402 | &dummy1, &dummy2); | |
4cd3e7df JJ |
1403 | } |
1404 | ||
b925d25d | 1405 | if (bi_const_p) |
4cd3e7df | 1406 | /* For __builtin_constant_p, fold all expressions with constant values |
b925d25d | 1407 | even if they aren't C++ constant-expressions. */ |
69dc042f JM |
1408 | arg = cp_fold_rvalue (arg); |
1409 | else if (strop) | |
1410 | { | |
1411 | if (TREE_CODE (arg) == CONSTRUCTOR) | |
1412 | arg = braced_lists_to_strings (TREE_TYPE (arg), arg); | |
1413 | if (TREE_CODE (arg) == STRING_CST) | |
1414 | arg = build_address (arg); | |
6f346913 JM |
1415 | else |
1416 | arg = oarg; | |
69dc042f JM |
1417 | } |
1418 | ||
1419 | args[i] = arg; | |
b925d25d | 1420 | } |
5756d0f9 JM |
1421 | |
1422 | bool save_ffbcp = force_folding_builtin_constant_p; | |
b09a67ea | 1423 | force_folding_builtin_constant_p |= ctx->manifestly_const_eval; |
43574e4f JJ |
1424 | tree save_cur_fn = current_function_decl; |
1425 | /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */ | |
1426 | if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION) | |
1427 | && ctx->call | |
1428 | && ctx->call->fundef) | |
1429 | current_function_decl = ctx->call->fundef->decl; | |
6cd005a2 JJ |
1430 | if (fndecl_built_in_p (fun, |
1431 | CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS, | |
1432 | BUILT_IN_FRONTEND)) | |
1433 | { | |
1434 | location_t loc = EXPR_LOCATION (t); | |
1435 | if (nargs >= 1) | |
1436 | VERIFY_CONSTANT (args[0]); | |
1437 | new_call | |
1438 | = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs, | |
1439 | args); | |
1440 | } | |
32c3a753 JJ |
1441 | else if (fndecl_built_in_p (fun, |
1442 | CP_BUILT_IN_IS_CORRESPONDING_MEMBER, | |
1443 | BUILT_IN_FRONTEND)) | |
1444 | { | |
1445 | location_t loc = EXPR_LOCATION (t); | |
1446 | if (nargs >= 2) | |
1447 | { | |
1448 | VERIFY_CONSTANT (args[0]); | |
1449 | VERIFY_CONSTANT (args[1]); | |
1450 | } | |
1451 | new_call = fold_builtin_is_corresponding_member (loc, nargs, args); | |
1452 | } | |
6cd005a2 JJ |
1453 | else |
1454 | new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t), | |
1455 | CALL_EXPR_FN (t), nargs, args); | |
43574e4f | 1456 | current_function_decl = save_cur_fn; |
5756d0f9 | 1457 | force_folding_builtin_constant_p = save_ffbcp; |
109d2197 JJ |
1458 | if (new_call == NULL) |
1459 | { | |
1460 | if (!*non_constant_p && !ctx->quiet) | |
1461 | { | |
1b6fa695 ML |
1462 | /* Do not allow__builtin_unreachable in constexpr function. |
1463 | The __builtin_unreachable call with BUILTINS_LOCATION | |
1464 | comes from cp_maybe_instrument_return. */ | |
3d78e008 | 1465 | if (fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE) |
1b6fa695 | 1466 | && EXPR_LOCATION (t) == BUILTINS_LOCATION) |
84fa214d | 1467 | error ("%<constexpr%> call flows off the end of the function"); |
1b6fa695 ML |
1468 | else |
1469 | { | |
1470 | new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t), | |
1471 | CALL_EXPR_FN (t), nargs, args); | |
1472 | error ("%q+E is not a constant expression", new_call); | |
1473 | } | |
109d2197 JJ |
1474 | } |
1475 | *non_constant_p = true; | |
1476 | return t; | |
1477 | } | |
1478 | ||
43574e4f | 1479 | if (!potential_constant_expression (new_call)) |
109d2197 JJ |
1480 | { |
1481 | if (!*non_constant_p && !ctx->quiet) | |
1482 | error ("%q+E is not a constant expression", new_call); | |
1483 | *non_constant_p = true; | |
1484 | return t; | |
1485 | } | |
1486 | ||
69dc042f JM |
1487 | if (strret) |
1488 | { | |
1489 | /* memchr returns a pointer into the first argument, but we replaced the | |
1490 | argument above with a STRING_CST; put it back it now. */ | |
1491 | tree op = CALL_EXPR_ARG (t, strret-1); | |
1492 | STRIP_NOPS (new_call); | |
1493 | if (TREE_CODE (new_call) == POINTER_PLUS_EXPR) | |
1494 | TREE_OPERAND (new_call, 0) = op; | |
1495 | else if (TREE_CODE (new_call) == ADDR_EXPR) | |
1496 | new_call = op; | |
1497 | } | |
1498 | ||
109d2197 JJ |
1499 | return cxx_eval_constant_expression (&new_ctx, new_call, lval, |
1500 | non_constant_p, overflow_p); | |
2d76680f PC |
1501 | } |
1502 | ||
1503 | /* TEMP is the constant value of a temporary object of type TYPE. Adjust | |
1504 | the type of the value to match. */ | |
1505 | ||
1506 | static tree | |
1507 | adjust_temp_type (tree type, tree temp) | |
1508 | { | |
47be9509 | 1509 | if (same_type_p (TREE_TYPE (temp), type)) |
2d76680f PC |
1510 | return temp; |
1511 | /* Avoid wrapping an aggregate value in a NOP_EXPR. */ | |
1512 | if (TREE_CODE (temp) == CONSTRUCTOR) | |
b27f74e7 MP |
1513 | { |
1514 | /* build_constructor wouldn't retain various CONSTRUCTOR flags. */ | |
1515 | tree t = copy_node (temp); | |
1516 | TREE_TYPE (t) = type; | |
1517 | return t; | |
1518 | } | |
ff8ba86f JJ |
1519 | if (TREE_CODE (temp) == EMPTY_CLASS_EXPR) |
1520 | return build0 (EMPTY_CLASS_EXPR, type); | |
2d76680f | 1521 | gcc_assert (scalarish_type_p (type)); |
72b091f7 MP |
1522 | /* Now we know we're dealing with a scalar, and a prvalue of non-class |
1523 | type is cv-unqualified. */ | |
1524 | return cp_fold_convert (cv_unqualified (type), temp); | |
2d76680f PC |
1525 | } |
1526 | ||
9b9eb42a JM |
1527 | /* If T is a CONSTRUCTOR, return an unshared copy of T and any |
1528 | sub-CONSTRUCTORs. Otherwise return T. | |
0146e25f | 1529 | |
9b9eb42a JM |
1530 | We use this whenever we initialize an object as a whole, whether it's a |
1531 | parameter, a local variable, or a subobject, so that subsequent | |
1532 | modifications don't affect other places where it was used. */ | |
0146e25f | 1533 | |
1f6857ba | 1534 | tree |
3da7d774 | 1535 | unshare_constructor (tree t MEM_STAT_DECL) |
0146e25f | 1536 | { |
9b9eb42a JM |
1537 | if (!t || TREE_CODE (t) != CONSTRUCTOR) |
1538 | return t; | |
1539 | auto_vec <tree*, 4> ptrs; | |
1540 | ptrs.safe_push (&t); | |
1541 | while (!ptrs.is_empty ()) | |
1542 | { | |
1543 | tree *p = ptrs.pop (); | |
3da7d774 JM |
1544 | tree n = copy_node (*p PASS_MEM_STAT); |
1545 | CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT); | |
9b9eb42a JM |
1546 | *p = n; |
1547 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n); | |
1548 | constructor_elt *ce; | |
1549 | for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
6b7d53a2 | 1550 | if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR) |
9b9eb42a JM |
1551 | ptrs.safe_push (&ce->value); |
1552 | } | |
0146e25f PP |
1553 | return t; |
1554 | } | |
1555 | ||
620adbec JM |
1556 | /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */ |
1557 | ||
1558 | static void | |
1559 | free_constructor (tree t) | |
1560 | { | |
1561 | if (!t || TREE_CODE (t) != CONSTRUCTOR) | |
1562 | return; | |
1563 | releasing_vec ctors; | |
1564 | vec_safe_push (ctors, t); | |
1565 | while (!ctors->is_empty ()) | |
1566 | { | |
1567 | tree c = ctors->pop (); | |
1568 | if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c)) | |
1569 | { | |
1570 | constructor_elt *ce; | |
1571 | for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i) | |
1572 | if (TREE_CODE (ce->value) == CONSTRUCTOR) | |
1573 | vec_safe_push (ctors, ce->value); | |
1574 | ggc_free (elts); | |
1575 | } | |
1576 | ggc_free (c); | |
1577 | } | |
1578 | } | |
1579 | ||
559d2f1e JJ |
1580 | /* Helper function of cxx_bind_parameters_in_call. Return non-NULL |
1581 | if *TP is address of a static variable (or part of it) currently being | |
1582 | constructed or of a heap artificial variable. */ | |
1583 | ||
1584 | static tree | |
1585 | addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data) | |
1586 | { | |
1587 | if (TREE_CODE (*tp) == ADDR_EXPR) | |
1588 | if (tree var = get_base_address (TREE_OPERAND (*tp, 0))) | |
1589 | if (VAR_P (var) && TREE_STATIC (var)) | |
1590 | { | |
1591 | if (DECL_NAME (var) == heap_uninit_identifier | |
1592 | || DECL_NAME (var) == heap_identifier | |
1593 | || DECL_NAME (var) == heap_vec_uninit_identifier | |
1594 | || DECL_NAME (var) == heap_vec_identifier) | |
1595 | return var; | |
1596 | ||
1597 | constexpr_global_ctx *global = (constexpr_global_ctx *) data; | |
1598 | if (global->values.get (var)) | |
1599 | return var; | |
1600 | } | |
1601 | if (TYPE_P (*tp)) | |
1602 | *walk_subtrees = false; | |
1603 | return NULL_TREE; | |
1604 | } | |
1605 | ||
2d76680f PC |
1606 | /* Subroutine of cxx_eval_call_expression. |
1607 | We are processing a call expression (either CALL_EXPR or | |
3e605b20 | 1608 | AGGR_INIT_EXPR) in the context of CTX. Evaluate |
2d76680f PC |
1609 | all arguments and bind their values to correspondings |
1610 | parameters, making up the NEW_CALL context. */ | |
1611 | ||
1595fe44 JM |
1612 | static tree |
1613 | cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun, | |
12d9ce19 JM |
1614 | bool *non_constant_p, bool *overflow_p, |
1615 | bool *non_constant_args) | |
2d76680f PC |
1616 | { |
1617 | const int nargs = call_expr_nargs (t); | |
1595fe44 | 1618 | tree parms = DECL_ARGUMENTS (fun); |
2d76680f | 1619 | int i; |
3c961dc7 JM |
1620 | /* We don't record ellipsis args below. */ |
1621 | int nparms = list_length (parms); | |
1622 | int nbinds = nargs < nparms ? nargs : nparms; | |
1595fe44 | 1623 | tree binds = make_tree_vec (nbinds); |
2d76680f PC |
1624 | for (i = 0; i < nargs; ++i) |
1625 | { | |
1626 | tree x, arg; | |
1627 | tree type = parms ? TREE_TYPE (parms) : void_type_node; | |
1595fe44 JM |
1628 | if (parms && DECL_BY_REFERENCE (parms)) |
1629 | type = TREE_TYPE (type); | |
2d76680f | 1630 | x = get_nth_callarg (t, i); |
3e605b20 JM |
1631 | /* For member function, the first argument is a pointer to the implied |
1632 | object. For a constructor, it might still be a dummy object, in | |
60813a46 | 1633 | which case we get the real argument from ctx. */ |
3e605b20 JM |
1634 | if (i == 0 && DECL_CONSTRUCTOR_P (fun) |
1635 | && is_dummy_object (x)) | |
1636 | { | |
1637 | x = ctx->object; | |
84dd815f | 1638 | x = build_address (x); |
3e605b20 | 1639 | } |
43574e4f JJ |
1640 | if (TREE_ADDRESSABLE (type)) |
1641 | /* Undo convert_for_arg_passing work here. */ | |
1642 | x = convert_from_reference (x); | |
6c59b8a9 JM |
1643 | /* Normally we would strip a TARGET_EXPR in an initialization context |
1644 | such as this, but here we do the elision differently: we keep the | |
1645 | TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */ | |
0e038601 | 1646 | arg = cxx_eval_constant_expression (ctx, x, /*lval=*/false, |
5a804683 | 1647 | non_constant_p, overflow_p); |
2d76680f | 1648 | /* Don't VERIFY_CONSTANT here. */ |
2b3ab879 | 1649 | if (*non_constant_p && ctx->quiet) |
1595fe44 | 1650 | break; |
2d76680f PC |
1651 | /* Just discard ellipsis args after checking their constantitude. */ |
1652 | if (!parms) | |
1653 | continue; | |
4727d4c6 NS |
1654 | |
1655 | if (!*non_constant_p) | |
1656 | { | |
1657 | /* Make sure the binding has the same type as the parm. But | |
1658 | only for constant args. */ | |
9f613f06 | 1659 | if (!TYPE_REF_P (type)) |
4727d4c6 NS |
1660 | arg = adjust_temp_type (type, arg); |
1661 | if (!TREE_CONSTANT (arg)) | |
1662 | *non_constant_args = true; | |
5afd90c5 JJ |
1663 | else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
1664 | /* The destructor needs to see any modifications the callee makes | |
1665 | to the argument. */ | |
1666 | *non_constant_args = true; | |
559d2f1e JJ |
1667 | /* If arg is or contains address of a heap artificial variable or |
1668 | of a static variable being constructed, avoid caching the | |
1669 | function call, as those variables might be modified by the | |
1670 | function, or might be modified by the callers in between | |
1671 | the cached function and just read by the function. */ | |
1672 | else if (!*non_constant_args | |
1673 | && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global, | |
1674 | NULL)) | |
1675 | *non_constant_args = true; | |
5afd90c5 | 1676 | |
5558a0da JJ |
1677 | /* For virtual calls, adjust the this argument, so that it is |
1678 | the object on which the method is called, rather than | |
1679 | one of its bases. */ | |
1680 | if (i == 0 && DECL_VIRTUAL_P (fun)) | |
1681 | { | |
1682 | tree addr = arg; | |
1683 | STRIP_NOPS (addr); | |
1684 | if (TREE_CODE (addr) == ADDR_EXPR) | |
1685 | { | |
1686 | tree obj = TREE_OPERAND (addr, 0); | |
1687 | while (TREE_CODE (obj) == COMPONENT_REF | |
1688 | && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1)) | |
1689 | && !same_type_ignoring_top_level_qualifiers_p | |
1690 | (TREE_TYPE (obj), DECL_CONTEXT (fun))) | |
1691 | obj = TREE_OPERAND (obj, 0); | |
1692 | if (obj != TREE_OPERAND (addr, 0)) | |
1693 | arg = build_fold_addr_expr_with_type (obj, | |
1694 | TREE_TYPE (arg)); | |
1695 | } | |
1696 | } | |
3c961dc7 | 1697 | TREE_VEC_ELT (binds, i) = arg; |
4727d4c6 | 1698 | } |
2d76680f PC |
1699 | parms = TREE_CHAIN (parms); |
1700 | } | |
1595fe44 JM |
1701 | |
1702 | return binds; | |
2d76680f PC |
1703 | } |
1704 | ||
1705 | /* Variables and functions to manage constexpr call expansion context. | |
1706 | These do not need to be marked for PCH or GC. */ | |
1707 | ||
1708 | /* FIXME remember and print actual constant arguments. */ | |
7de76362 | 1709 | static vec<tree> call_stack; |
2d76680f PC |
1710 | static int call_stack_tick; |
1711 | static int last_cx_error_tick; | |
1712 | ||
7ffc7de5 | 1713 | static int |
2d76680f PC |
1714 | push_cx_call_context (tree call) |
1715 | { | |
1716 | ++call_stack_tick; | |
1717 | if (!EXPR_HAS_LOCATION (call)) | |
1718 | SET_EXPR_LOCATION (call, input_location); | |
1719 | call_stack.safe_push (call); | |
7ffc7de5 JM |
1720 | int len = call_stack.length (); |
1721 | if (len > max_constexpr_depth) | |
2d76680f | 1722 | return false; |
7ffc7de5 | 1723 | return len; |
2d76680f PC |
1724 | } |
1725 | ||
1726 | static void | |
1727 | pop_cx_call_context (void) | |
1728 | { | |
1729 | ++call_stack_tick; | |
1730 | call_stack.pop (); | |
1731 | } | |
1732 | ||
1733 | vec<tree> | |
1734 | cx_error_context (void) | |
1735 | { | |
1736 | vec<tree> r = vNULL; | |
1737 | if (call_stack_tick != last_cx_error_tick | |
1738 | && !call_stack.is_empty ()) | |
1739 | r = call_stack; | |
1740 | last_cx_error_tick = call_stack_tick; | |
1741 | return r; | |
1742 | } | |
1743 | ||
44a845ca MS |
1744 | /* Evaluate a call T to a GCC internal function when possible and return |
1745 | the evaluated result or, under the control of CTX, give an error, set | |
1746 | NON_CONSTANT_P, and return the unevaluated call T otherwise. */ | |
1747 | ||
1748 | static tree | |
1749 | cxx_eval_internal_function (const constexpr_ctx *ctx, tree t, | |
1750 | bool lval, | |
1751 | bool *non_constant_p, bool *overflow_p) | |
1752 | { | |
1753 | enum tree_code opcode = ERROR_MARK; | |
1754 | ||
1755 | switch (CALL_EXPR_IFN (t)) | |
1756 | { | |
1757 | case IFN_UBSAN_NULL: | |
1758 | case IFN_UBSAN_BOUNDS: | |
1759 | case IFN_UBSAN_VPTR: | |
81fea426 | 1760 | case IFN_FALLTHROUGH: |
44a845ca MS |
1761 | return void_node; |
1762 | ||
1763 | case IFN_ADD_OVERFLOW: | |
1764 | opcode = PLUS_EXPR; | |
1765 | break; | |
1766 | case IFN_SUB_OVERFLOW: | |
1767 | opcode = MINUS_EXPR; | |
1768 | break; | |
1769 | case IFN_MUL_OVERFLOW: | |
1770 | opcode = MULT_EXPR; | |
1771 | break; | |
1772 | ||
e16f1cc7 JJ |
1773 | case IFN_LAUNDER: |
1774 | return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), | |
1775 | false, non_constant_p, overflow_p); | |
1776 | ||
d8fcab68 JJ |
1777 | case IFN_VEC_CONVERT: |
1778 | { | |
1779 | tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), | |
1780 | false, non_constant_p, | |
1781 | overflow_p); | |
1782 | if (TREE_CODE (arg) == VECTOR_CST) | |
1783 | return fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg); | |
1784 | else | |
1785 | { | |
1786 | *non_constant_p = true; | |
1787 | return t; | |
1788 | } | |
1789 | } | |
1790 | ||
44a845ca MS |
1791 | default: |
1792 | if (!ctx->quiet) | |
f9d0ca40 | 1793 | error_at (cp_expr_loc_or_input_loc (t), |
44a845ca MS |
1794 | "call to internal function %qE", t); |
1795 | *non_constant_p = true; | |
1796 | return t; | |
1797 | } | |
1798 | ||
1799 | /* Evaluate constant arguments using OPCODE and return a complex | |
1800 | number containing the result and the overflow bit. */ | |
1801 | tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval, | |
1802 | non_constant_p, overflow_p); | |
1803 | tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval, | |
1804 | non_constant_p, overflow_p); | |
1805 | ||
1806 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) | |
1807 | { | |
f9d0ca40 | 1808 | location_t loc = cp_expr_loc_or_input_loc (t); |
44a845ca MS |
1809 | tree type = TREE_TYPE (TREE_TYPE (t)); |
1810 | tree result = fold_binary_loc (loc, opcode, type, | |
1811 | fold_convert_loc (loc, type, arg0), | |
1812 | fold_convert_loc (loc, type, arg1)); | |
1813 | tree ovf | |
1814 | = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1)); | |
1815 | /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */ | |
1816 | if (TREE_OVERFLOW (result)) | |
1817 | TREE_OVERFLOW (result) = 0; | |
1818 | ||
1819 | return build_complex (TREE_TYPE (t), result, ovf); | |
1820 | } | |
1821 | ||
1822 | *non_constant_p = true; | |
1823 | return t; | |
1824 | } | |
1825 | ||
e8c48716 | 1826 | /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */ |
ecc57615 JM |
1827 | |
1828 | static void | |
1829 | clear_no_implicit_zero (tree ctor) | |
1830 | { | |
e8c48716 | 1831 | if (CONSTRUCTOR_NO_CLEARING (ctor)) |
ecc57615 | 1832 | { |
e8c48716 | 1833 | CONSTRUCTOR_NO_CLEARING (ctor) = false; |
fae00a0a JM |
1834 | for (auto &e: CONSTRUCTOR_ELTS (ctor)) |
1835 | if (TREE_CODE (e.value) == CONSTRUCTOR) | |
1836 | clear_no_implicit_zero (e.value); | |
ecc57615 JM |
1837 | } |
1838 | } | |
1839 | ||
04e1749c MP |
1840 | /* Complain about a const object OBJ being modified in a constant expression. |
1841 | EXPR is the MODIFY_EXPR expression performing the modification. */ | |
1842 | ||
1843 | static void | |
1844 | modifying_const_object_error (tree expr, tree obj) | |
1845 | { | |
1846 | location_t loc = cp_expr_loc_or_input_loc (expr); | |
1847 | auto_diagnostic_group d; | |
1848 | error_at (loc, "modifying a const object %qE is not allowed in " | |
1849 | "a constant expression", TREE_OPERAND (expr, 0)); | |
1850 | inform (location_of (obj), "originally declared %<const%> here"); | |
1851 | } | |
1852 | ||
8e007055 JJ |
1853 | /* Return true if FNDECL is a replaceable global allocation function that |
1854 | should be useable during constant expression evaluation. */ | |
1855 | ||
1856 | static inline bool | |
1857 | cxx_replaceable_global_alloc_fn (tree fndecl) | |
1858 | { | |
b04445d4 | 1859 | return (cxx_dialect >= cxx20 |
8e007055 | 1860 | && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl)) |
cf650568 JJ |
1861 | && CP_DECL_CONTEXT (fndecl) == global_namespace |
1862 | && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl) | |
1863 | || DECL_IS_OPERATOR_DELETE_P (fndecl))); | |
1864 | } | |
1865 | ||
1866 | /* Return true if FNDECL is a placement new function that should be | |
1867 | useable during constant expression evaluation of std::construct_at. */ | |
1868 | ||
1869 | static inline bool | |
1870 | cxx_placement_new_fn (tree fndecl) | |
1871 | { | |
b04445d4 | 1872 | if (cxx_dialect >= cxx20 |
cf650568 JJ |
1873 | && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl)) |
1874 | && CP_DECL_CONTEXT (fndecl) == global_namespace | |
1875 | && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl) | |
1876 | && TREE_CODE (TREE_TYPE (fndecl)) == FUNCTION_TYPE) | |
1877 | { | |
1878 | tree first_arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl))); | |
1879 | if (TREE_VALUE (first_arg) == ptr_type_node | |
1880 | && TREE_CHAIN (first_arg) == void_list_node) | |
1881 | return true; | |
1882 | } | |
1883 | return false; | |
1884 | } | |
1885 | ||
1886 | /* Return true if FNDECL is std::construct_at. */ | |
1887 | ||
1888 | static inline bool | |
1889 | is_std_construct_at (tree fndecl) | |
1890 | { | |
1891 | if (!decl_in_std_namespace_p (fndecl)) | |
1892 | return false; | |
1893 | ||
1894 | tree name = DECL_NAME (fndecl); | |
1895 | return name && id_equal (name, "construct_at"); | |
8e007055 JJ |
1896 | } |
1897 | ||
2ffc2645 MP |
1898 | /* Overload for the above taking constexpr_call*. */ |
1899 | ||
1900 | static inline bool | |
1901 | is_std_construct_at (const constexpr_call *call) | |
1902 | { | |
1903 | return (call | |
1904 | && call->fundef | |
1905 | && is_std_construct_at (call->fundef->decl)); | |
1906 | } | |
1907 | ||
8412b939 JJ |
1908 | /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */ |
1909 | ||
1910 | static inline bool | |
1911 | is_std_allocator_allocate (tree fndecl) | |
1912 | { | |
1913 | tree name = DECL_NAME (fndecl); | |
1914 | if (name == NULL_TREE | |
1915 | || !(id_equal (name, "allocate") || id_equal (name, "deallocate"))) | |
1916 | return false; | |
1917 | ||
1918 | tree ctx = DECL_CONTEXT (fndecl); | |
1919 | if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx)) | |
1920 | return false; | |
1921 | ||
1922 | tree decl = TYPE_MAIN_DECL (ctx); | |
1923 | name = DECL_NAME (decl); | |
1924 | if (name == NULL_TREE || !id_equal (name, "allocator")) | |
1925 | return false; | |
1926 | ||
1927 | return decl_in_std_namespace_p (decl); | |
1928 | } | |
1929 | ||
2ffc2645 MP |
1930 | /* Overload for the above taking constexpr_call*. */ |
1931 | ||
1932 | static inline bool | |
1933 | is_std_allocator_allocate (const constexpr_call *call) | |
1934 | { | |
1935 | return (call | |
1936 | && call->fundef | |
1937 | && is_std_allocator_allocate (call->fundef->decl)); | |
1938 | } | |
1939 | ||
22edf943 MP |
1940 | /* Return true if FNDECL is __dynamic_cast. */ |
1941 | ||
1942 | static inline bool | |
1943 | cxx_dynamic_cast_fn_p (tree fndecl) | |
1944 | { | |
b04445d4 | 1945 | return (cxx_dialect >= cxx20 |
22edf943 MP |
1946 | && id_equal (DECL_NAME (fndecl), "__dynamic_cast") |
1947 | && CP_DECL_CONTEXT (fndecl) == global_namespace); | |
1948 | } | |
1949 | ||
1950 | /* Often, we have an expression in the form of address + offset, e.g. | |
1951 | "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */ | |
1952 | ||
1953 | static tree | |
1954 | extract_obj_from_addr_offset (tree expr) | |
1955 | { | |
1956 | if (TREE_CODE (expr) == POINTER_PLUS_EXPR) | |
1957 | expr = TREE_OPERAND (expr, 0); | |
1958 | STRIP_NOPS (expr); | |
1959 | if (TREE_CODE (expr) == ADDR_EXPR) | |
1960 | expr = TREE_OPERAND (expr, 0); | |
1961 | return expr; | |
1962 | } | |
1963 | ||
1964 | /* Given a PATH like | |
1965 | ||
1966 | g.D.2181.D.2154.D.2102.D.2093 | |
1967 | ||
1968 | find a component with type TYPE. Return NULL_TREE if not found, and | |
1969 | error_mark_node if the component is not accessible. If STOP is non-null, | |
1970 | this function will return NULL_TREE if STOP is found before TYPE. */ | |
1971 | ||
1972 | static tree | |
1973 | get_component_with_type (tree path, tree type, tree stop) | |
1974 | { | |
1975 | while (true) | |
1976 | { | |
1977 | if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type)) | |
1978 | /* Found it. */ | |
1979 | return path; | |
1980 | else if (stop | |
1981 | && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), | |
1982 | stop))) | |
1983 | return NULL_TREE; | |
1984 | else if (TREE_CODE (path) == COMPONENT_REF | |
1985 | && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1))) | |
1986 | { | |
1987 | /* We need to check that the component we're accessing is in fact | |
1988 | accessible. */ | |
1989 | if (TREE_PRIVATE (TREE_OPERAND (path, 1)) | |
1990 | || TREE_PROTECTED (TREE_OPERAND (path, 1))) | |
1991 | return error_mark_node; | |
1992 | path = TREE_OPERAND (path, 0); | |
1993 | } | |
1994 | else | |
1995 | return NULL_TREE; | |
1996 | } | |
1997 | } | |
1998 | ||
1999 | /* Evaluate a call to __dynamic_cast (permitted by P1327R1). | |
2000 | ||
2001 | The declaration of __dynamic_cast is: | |
2002 | ||
2003 | void* __dynamic_cast (const void* __src_ptr, | |
2004 | const __class_type_info* __src_type, | |
2005 | const __class_type_info* __dst_type, | |
2006 | ptrdiff_t __src2dst); | |
2007 | ||
2008 | where src2dst has the following possible values | |
2009 | ||
2010 | >-1: src_type is a unique public non-virtual base of dst_type | |
2011 | dst_ptr + src2dst == src_ptr | |
2012 | -1: unspecified relationship | |
2013 | -2: src_type is not a public base of dst_type | |
2014 | -3: src_type is a multiple public non-virtual base of dst_type | |
2015 | ||
2016 | Since literal types can't have virtual bases, we only expect hint >=0, | |
2017 | -2, or -3. */ | |
2018 | ||
2019 | static tree | |
2020 | cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call, | |
2021 | bool *non_constant_p, bool *overflow_p) | |
2022 | { | |
2023 | /* T will be something like | |
2024 | __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8) | |
2025 | dismantle it. */ | |
2026 | gcc_assert (call_expr_nargs (call) == 4); | |
2027 | tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; | |
2028 | tree obj = CALL_EXPR_ARG (call, 0); | |
2029 | tree type = CALL_EXPR_ARG (call, 2); | |
2030 | HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3)); | |
2031 | location_t loc = cp_expr_loc_or_input_loc (call); | |
2032 | ||
2033 | /* Get the target type of the dynamic_cast. */ | |
2034 | gcc_assert (TREE_CODE (type) == ADDR_EXPR); | |
2035 | type = TREE_OPERAND (type, 0); | |
2036 | type = TREE_TYPE (DECL_NAME (type)); | |
2037 | ||
2038 | /* TYPE can only be either T* or T&. We can't know which of these it | |
2039 | is by looking at TYPE, but OBJ will be "(T*) x" in the first case, | |
2040 | and something like "(T*)(T&)(T*) x" in the second case. */ | |
2041 | bool reference_p = false; | |
2042 | while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR) | |
2043 | { | |
2044 | reference_p |= TYPE_REF_P (TREE_TYPE (obj)); | |
2045 | obj = TREE_OPERAND (obj, 0); | |
2046 | } | |
2047 | ||
2048 | /* Evaluate the object so that we know its dynamic type. */ | |
2049 | obj = cxx_eval_constant_expression (ctx, obj, /*lval*/false, non_constant_p, | |
2050 | overflow_p); | |
2051 | if (*non_constant_p) | |
2052 | return call; | |
2053 | ||
2054 | /* We expect OBJ to be in form of &d.D.2102 when HINT == 0, | |
2055 | but when HINT is > 0, it can also be something like | |
2056 | &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */ | |
2057 | obj = extract_obj_from_addr_offset (obj); | |
2058 | const tree objtype = TREE_TYPE (obj); | |
2059 | /* If OBJ doesn't refer to a base field, we're done. */ | |
2060 | if (tree t = (TREE_CODE (obj) == COMPONENT_REF | |
2061 | ? TREE_OPERAND (obj, 1) : obj)) | |
2062 | if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t)) | |
de0684bf MP |
2063 | { |
2064 | if (reference_p) | |
2065 | { | |
2066 | if (!ctx->quiet) | |
2067 | { | |
2068 | error_at (loc, "reference %<dynamic_cast%> failed"); | |
2069 | inform (loc, "dynamic type %qT of its operand does " | |
2070 | "not have a base class of type %qT", | |
2071 | objtype, type); | |
2072 | } | |
2073 | *non_constant_p = true; | |
2074 | } | |
2075 | return integer_zero_node; | |
2076 | } | |
22edf943 MP |
2077 | |
2078 | /* [class.cdtor] When a dynamic_cast is used in a constructor ... | |
2079 | or in a destructor ... if the operand of the dynamic_cast refers | |
2080 | to the object under construction or destruction, this object is | |
2081 | considered to be a most derived object that has the type of the | |
2082 | constructor or destructor's class. */ | |
0221c656 | 2083 | tree vtable = build_vfield_ref (obj, objtype); |
22edf943 MP |
2084 | vtable = cxx_eval_constant_expression (ctx, vtable, /*lval*/false, |
2085 | non_constant_p, overflow_p); | |
2086 | if (*non_constant_p) | |
2087 | return call; | |
0221c656 MP |
2088 | /* With -fsanitize=vptr, we initialize all vtable pointers to null, |
2089 | so it's possible that we got a null pointer now. */ | |
2090 | if (integer_zerop (vtable)) | |
2091 | { | |
2092 | if (!ctx->quiet) | |
2093 | error_at (loc, "virtual table pointer is used uninitialized"); | |
2094 | *non_constant_p = true; | |
2095 | return integer_zero_node; | |
2096 | } | |
22edf943 MP |
2097 | /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */ |
2098 | vtable = extract_obj_from_addr_offset (vtable); | |
2099 | const tree mdtype = DECL_CONTEXT (vtable); | |
2100 | ||
2101 | /* Given dynamic_cast<T>(v), | |
2102 | ||
2103 | [expr.dynamic.cast] If C is the class type to which T points or refers, | |
2104 | the runtime check logically executes as follows: | |
2105 | ||
2106 | If, in the most derived object pointed (referred) to by v, v points | |
2107 | (refers) to a public base class subobject of a C object, and if only | |
2108 | one object of type C is derived from the subobject pointed (referred) | |
2109 | to by v the result points (refers) to that C object. | |
2110 | ||
2111 | In this case, HINT >= 0 or -3. */ | |
2112 | if (hint >= 0 || hint == -3) | |
2113 | { | |
2114 | /* Look for a component with type TYPE. */ | |
2115 | tree t = get_component_with_type (obj, type, mdtype); | |
2116 | /* If not accessible, give an error. */ | |
2117 | if (t == error_mark_node) | |
2118 | { | |
2119 | if (reference_p) | |
2120 | { | |
2121 | if (!ctx->quiet) | |
2122 | { | |
2123 | error_at (loc, "reference %<dynamic_cast%> failed"); | |
2124 | inform (loc, "static type %qT of its operand is a " | |
2125 | "non-public base class of dynamic type %qT", | |
2126 | objtype, type); | |
2127 | ||
2128 | } | |
2129 | *non_constant_p = true; | |
2130 | } | |
2131 | return integer_zero_node; | |
2132 | } | |
2133 | else if (t) | |
2134 | /* The result points to the TYPE object. */ | |
2135 | return cp_build_addr_expr (t, complain); | |
2136 | /* Else, TYPE was not found, because the HINT turned out to be wrong. | |
2137 | Fall through to the normal processing. */ | |
2138 | } | |
2139 | ||
2140 | /* Otherwise, if v points (refers) to a public base class subobject of the | |
2141 | most derived object, and the type of the most derived object has a base | |
2142 | class, of type C, that is unambiguous and public, the result points | |
2143 | (refers) to the C subobject of the most derived object. | |
2144 | ||
2145 | But it can also be an invalid case. */ | |
2146 | ||
2147 | /* Get the most derived object. */ | |
2148 | obj = get_component_with_type (obj, mdtype, NULL_TREE); | |
2149 | if (obj == error_mark_node) | |
2150 | { | |
2151 | if (reference_p) | |
2152 | { | |
2153 | if (!ctx->quiet) | |
2154 | { | |
2155 | error_at (loc, "reference %<dynamic_cast%> failed"); | |
2156 | inform (loc, "static type %qT of its operand is a non-public" | |
2157 | " base class of dynamic type %qT", objtype, mdtype); | |
2158 | } | |
2159 | *non_constant_p = true; | |
2160 | } | |
2161 | return integer_zero_node; | |
2162 | } | |
2163 | else | |
2164 | gcc_assert (obj); | |
2165 | ||
2166 | /* Check that the type of the most derived object has a base class | |
2167 | of type TYPE that is unambiguous and public. */ | |
2168 | base_kind b_kind; | |
2169 | tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none); | |
2170 | if (!binfo || binfo == error_mark_node) | |
2171 | { | |
2172 | if (reference_p) | |
2173 | { | |
2174 | if (!ctx->quiet) | |
2175 | { | |
2176 | error_at (loc, "reference %<dynamic_cast%> failed"); | |
2177 | if (b_kind == bk_ambig) | |
2178 | inform (loc, "%qT is an ambiguous base class of dynamic " | |
2179 | "type %qT of its operand", type, mdtype); | |
2180 | else | |
2181 | inform (loc, "dynamic type %qT of its operand does not " | |
2182 | "have an unambiguous public base class %qT", | |
2183 | mdtype, type); | |
2184 | } | |
2185 | *non_constant_p = true; | |
2186 | } | |
2187 | return integer_zero_node; | |
2188 | } | |
2189 | /* If so, return the TYPE subobject of the most derived object. */ | |
2190 | obj = convert_to_base_statically (obj, binfo); | |
2191 | return cp_build_addr_expr (obj, complain); | |
2192 | } | |
2193 | ||
b2562229 PP |
2194 | /* Data structure used by replace_result_decl and replace_result_decl_r. */ |
2195 | ||
2196 | struct replace_result_decl_data | |
2197 | { | |
2198 | /* The RESULT_DECL we want to replace. */ | |
2199 | tree decl; | |
2200 | /* The replacement for DECL. */ | |
2201 | tree replacement; | |
2202 | /* Whether we've performed any replacements. */ | |
2203 | bool changed; | |
2204 | }; | |
2205 | ||
2206 | /* Helper function for replace_result_decl, called through cp_walk_tree. */ | |
2207 | ||
2208 | static tree | |
2209 | replace_result_decl_r (tree *tp, int *walk_subtrees, void *data) | |
2210 | { | |
2211 | replace_result_decl_data *d = (replace_result_decl_data *) data; | |
2212 | ||
2213 | if (*tp == d->decl) | |
2214 | { | |
2215 | *tp = unshare_expr (d->replacement); | |
2216 | d->changed = true; | |
2217 | *walk_subtrees = 0; | |
2218 | } | |
2219 | else if (TYPE_P (*tp)) | |
2220 | *walk_subtrees = 0; | |
2221 | ||
2222 | return NULL_TREE; | |
2223 | } | |
2224 | ||
2225 | /* Replace every occurrence of DECL, a RESULT_DECL, with (an unshared copy of) | |
2226 | REPLACEMENT within the reduced constant expression *TP. Returns true iff a | |
2227 | replacement was performed. */ | |
2228 | ||
2229 | static bool | |
2230 | replace_result_decl (tree *tp, tree decl, tree replacement) | |
2231 | { | |
2232 | gcc_checking_assert (TREE_CODE (decl) == RESULT_DECL | |
2233 | && (same_type_ignoring_top_level_qualifiers_p | |
2234 | (TREE_TYPE (decl), TREE_TYPE (replacement)))); | |
2235 | replace_result_decl_data data = { decl, replacement, false }; | |
2236 | cp_walk_tree_without_duplicates (tp, replace_result_decl_r, &data); | |
2237 | return data.changed; | |
2238 | } | |
2239 | ||
e6321c45 JM |
2240 | /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */ |
2241 | ||
2242 | static tree | |
2243 | cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl, | |
2244 | bool lval, | |
2245 | bool *non_constant_p, bool *overflow_p) | |
2246 | { | |
2247 | tree function = THUNK_TARGET (thunk_fndecl); | |
2248 | ||
e6321c45 JM |
2249 | if (THUNK_VIRTUAL_OFFSET (thunk_fndecl)) |
2250 | { | |
e6321c45 JM |
2251 | if (!ctx->quiet) |
2252 | { | |
267318a2 JM |
2253 | if (!DECL_DECLARED_CONSTEXPR_P (function)) |
2254 | { | |
2255 | error ("call to non-%<constexpr%> function %qD", function); | |
2256 | explain_invalid_constexpr_fn (function); | |
2257 | } | |
2258 | else | |
2259 | /* virtual_offset is only set for virtual bases, which make the | |
2260 | class non-literal, so we don't need to handle it here. */ | |
2261 | error ("calling constexpr member function %qD through virtual " | |
2262 | "base subobject", function); | |
e6321c45 JM |
2263 | } |
2264 | *non_constant_p = true; | |
2265 | return t; | |
2266 | } | |
2267 | ||
2268 | tree new_call = copy_node (t); | |
2269 | CALL_EXPR_FN (new_call) = function; | |
2270 | TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function)); | |
2271 | ||
2272 | tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl)); | |
2273 | ||
2274 | if (DECL_THIS_THUNK_P (thunk_fndecl)) | |
2275 | { | |
2276 | /* 'this'-adjusting thunk. */ | |
2277 | tree this_arg = CALL_EXPR_ARG (t, 0); | |
2278 | this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg), | |
2279 | this_arg, offset); | |
2280 | CALL_EXPR_ARG (new_call, 0) = this_arg; | |
2281 | } | |
2282 | else | |
2283 | /* Return-adjusting thunk. */ | |
2284 | new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call), | |
2285 | new_call, offset); | |
2286 | ||
2287 | return cxx_eval_constant_expression (ctx, new_call, lval, | |
2288 | non_constant_p, overflow_p); | |
2289 | } | |
2290 | ||
caf17f3a MP |
2291 | /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set |
2292 | its TREE_READONLY flag according to READONLY_P. Used for constexpr | |
2293 | 'tors to detect modifying const objects in a constexpr context. */ | |
2294 | ||
2295 | static void | |
2296 | cxx_set_object_constness (const constexpr_ctx *ctx, tree object, | |
2297 | bool readonly_p, bool *non_constant_p, | |
2298 | bool *overflow_p) | |
2299 | { | |
2300 | if (CLASS_TYPE_P (TREE_TYPE (object)) | |
2301 | && CP_TYPE_CONST_P (TREE_TYPE (object))) | |
2302 | { | |
2303 | /* Subobjects might not be stored in ctx->global->values but we | |
2304 | can get its CONSTRUCTOR by evaluating *this. */ | |
2305 | tree e = cxx_eval_constant_expression (ctx, object, /*lval*/false, | |
2306 | non_constant_p, overflow_p); | |
2307 | if (TREE_CODE (e) == CONSTRUCTOR && !*non_constant_p) | |
2308 | TREE_READONLY (e) = readonly_p; | |
2309 | } | |
2310 | } | |
2311 | ||
2d76680f PC |
2312 | /* Subroutine of cxx_eval_constant_expression. |
2313 | Evaluate the call expression tree T in the context of OLD_CALL expression | |
2314 | evaluation. */ | |
2315 | ||
2316 | static tree | |
3e605b20 | 2317 | cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, |
92a596e8 | 2318 | bool lval, |
2d76680f PC |
2319 | bool *non_constant_p, bool *overflow_p) |
2320 | { | |
861d4af8 AS |
2321 | /* Handle concept checks separately. */ |
2322 | if (concept_check_p (t)) | |
662ef5b5 | 2323 | return evaluate_concept_check (t); |
861d4af8 | 2324 | |
f9d0ca40 | 2325 | location_t loc = cp_expr_loc_or_input_loc (t); |
2d76680f | 2326 | tree fun = get_function_named_in_call (t); |
cce3ae91 | 2327 | constexpr_call new_call |
13de99bc | 2328 | = { NULL, NULL, NULL, 0, ctx->manifestly_const_eval }; |
7ffc7de5 | 2329 | int depth_ok; |
2d76680f | 2330 | |
0b274c17 | 2331 | if (fun == NULL_TREE) |
44a845ca MS |
2332 | return cxx_eval_internal_function (ctx, t, lval, |
2333 | non_constant_p, overflow_p); | |
0b274c17 | 2334 | |
2d76680f PC |
2335 | if (TREE_CODE (fun) != FUNCTION_DECL) |
2336 | { | |
2337 | /* Might be a constexpr function pointer. */ | |
2b3ab879 | 2338 | fun = cxx_eval_constant_expression (ctx, fun, |
92a596e8 | 2339 | /*lval*/false, non_constant_p, |
5a804683 | 2340 | overflow_p); |
2d76680f PC |
2341 | STRIP_NOPS (fun); |
2342 | if (TREE_CODE (fun) == ADDR_EXPR) | |
2343 | fun = TREE_OPERAND (fun, 0); | |
582d2481 JJ |
2344 | /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no |
2345 | indirection, the called expression is a pointer into the | |
2346 | virtual table which should contain FDESC_EXPR. Extract the | |
2347 | FUNCTION_DECL from there. */ | |
2348 | else if (TARGET_VTABLE_USES_DESCRIPTORS | |
2349 | && TREE_CODE (fun) == POINTER_PLUS_EXPR | |
2350 | && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR | |
2351 | && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST) | |
2352 | { | |
2353 | tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0); | |
2354 | if (VAR_P (d) | |
2355 | && DECL_VTABLE_OR_VTT_P (d) | |
2356 | && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE | |
2357 | && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type | |
2358 | && DECL_INITIAL (d) | |
2359 | && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR) | |
2360 | { | |
2361 | tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1), | |
2362 | TYPE_SIZE_UNIT (vtable_entry_type)); | |
2363 | HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i); | |
2364 | if (idx >= 0) | |
2365 | { | |
2366 | tree fdesc | |
2367 | = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value; | |
2368 | if (TREE_CODE (fdesc) == FDESC_EXPR | |
2369 | && integer_zerop (TREE_OPERAND (fdesc, 1))) | |
2370 | fun = TREE_OPERAND (fdesc, 0); | |
2371 | } | |
2372 | } | |
2373 | } | |
2d76680f PC |
2374 | } |
2375 | if (TREE_CODE (fun) != FUNCTION_DECL) | |
2376 | { | |
2b3ab879 | 2377 | if (!ctx->quiet && !*non_constant_p) |
84fa214d | 2378 | error_at (loc, "expression %qE does not designate a %<constexpr%> " |
2d76680f PC |
2379 | "function", fun); |
2380 | *non_constant_p = true; | |
2381 | return t; | |
2382 | } | |
81f97181 | 2383 | if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)) |
2d76680f | 2384 | fun = DECL_CLONED_FUNCTION (fun); |
0b274c17 MP |
2385 | |
2386 | if (is_ubsan_builtin_p (fun)) | |
2387 | return void_node; | |
2388 | ||
3d78e008 | 2389 | if (fndecl_built_in_p (fun)) |
5756d0f9 | 2390 | return cxx_eval_builtin_function_call (ctx, t, fun, |
92a596e8 | 2391 | lval, non_constant_p, overflow_p); |
e6321c45 JM |
2392 | if (DECL_THUNK_P (fun)) |
2393 | return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p); | |
2d76680f PC |
2394 | if (!DECL_DECLARED_CONSTEXPR_P (fun)) |
2395 | { | |
8412b939 JJ |
2396 | if (TREE_CODE (t) == CALL_EXPR |
2397 | && cxx_replaceable_global_alloc_fn (fun) | |
2398 | && (CALL_FROM_NEW_OR_DELETE_P (t) | |
2ffc2645 | 2399 | || is_std_allocator_allocate (ctx->call))) |
8e007055 JJ |
2400 | { |
2401 | const int nargs = call_expr_nargs (t); | |
2402 | tree arg0 = NULL_TREE; | |
2403 | for (int i = 0; i < nargs; ++i) | |
2404 | { | |
2405 | tree arg = CALL_EXPR_ARG (t, i); | |
2406 | arg = cxx_eval_constant_expression (ctx, arg, false, | |
2407 | non_constant_p, overflow_p); | |
2408 | VERIFY_CONSTANT (arg); | |
2409 | if (i == 0) | |
2410 | arg0 = arg; | |
2411 | } | |
2412 | gcc_assert (arg0); | |
2413 | if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun))) | |
2414 | { | |
2415 | tree type = build_array_type_nelts (char_type_node, | |
2416 | tree_to_uhwi (arg0)); | |
815baade JJ |
2417 | tree var = build_decl (loc, VAR_DECL, |
2418 | (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) | |
2419 | & OVL_OP_FLAG_VEC) | |
2420 | ? heap_vec_uninit_identifier | |
2421 | : heap_uninit_identifier, | |
8e007055 JJ |
2422 | type); |
2423 | DECL_ARTIFICIAL (var) = 1; | |
2424 | TREE_STATIC (var) = 1; | |
a8db7887 JJ |
2425 | // Temporarily register the artificial var in varpool, |
2426 | // so that comparisons of its address against NULL are folded | |
2427 | // through nonzero_address even with | |
2428 | // -fno-delete-null-pointer-checks or that comparison of | |
2429 | // addresses of different heap artificial vars is folded too. | |
2430 | // See PR98988 and PR99031. | |
2431 | varpool_node::finalize_decl (var); | |
8e007055 JJ |
2432 | ctx->global->heap_vars.safe_push (var); |
2433 | ctx->global->values.put (var, NULL_TREE); | |
2434 | return fold_convert (ptr_type_node, build_address (var)); | |
2435 | } | |
2436 | else | |
2437 | { | |
2438 | STRIP_NOPS (arg0); | |
2439 | if (TREE_CODE (arg0) == ADDR_EXPR | |
2440 | && VAR_P (TREE_OPERAND (arg0, 0))) | |
2441 | { | |
2442 | tree var = TREE_OPERAND (arg0, 0); | |
2443 | if (DECL_NAME (var) == heap_uninit_identifier | |
2444 | || DECL_NAME (var) == heap_identifier) | |
2445 | { | |
815baade JJ |
2446 | if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) |
2447 | & OVL_OP_FLAG_VEC) | |
2448 | { | |
2449 | if (!ctx->quiet) | |
2450 | { | |
2451 | error_at (loc, "array deallocation of object " | |
2452 | "allocated with non-array " | |
2453 | "allocation"); | |
2454 | inform (DECL_SOURCE_LOCATION (var), | |
2455 | "allocation performed here"); | |
2456 | } | |
2457 | *non_constant_p = true; | |
2458 | return t; | |
2459 | } | |
2460 | DECL_NAME (var) = heap_deleted_identifier; | |
2461 | ctx->global->values.remove (var); | |
2462 | ctx->global->heap_dealloc_count++; | |
2463 | return void_node; | |
2464 | } | |
2465 | else if (DECL_NAME (var) == heap_vec_uninit_identifier | |
2466 | || DECL_NAME (var) == heap_vec_identifier) | |
2467 | { | |
2468 | if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) | |
2469 | & OVL_OP_FLAG_VEC) == 0) | |
2470 | { | |
2471 | if (!ctx->quiet) | |
2472 | { | |
2473 | error_at (loc, "non-array deallocation of " | |
2474 | "object allocated with array " | |
2475 | "allocation"); | |
2476 | inform (DECL_SOURCE_LOCATION (var), | |
2477 | "allocation performed here"); | |
2478 | } | |
2479 | *non_constant_p = true; | |
2480 | return t; | |
2481 | } | |
8e007055 JJ |
2482 | DECL_NAME (var) = heap_deleted_identifier; |
2483 | ctx->global->values.remove (var); | |
f74f6092 | 2484 | ctx->global->heap_dealloc_count++; |
8e007055 JJ |
2485 | return void_node; |
2486 | } | |
2487 | else if (DECL_NAME (var) == heap_deleted_identifier) | |
2488 | { | |
2489 | if (!ctx->quiet) | |
2490 | error_at (loc, "deallocation of already deallocated " | |
2491 | "storage"); | |
2492 | *non_constant_p = true; | |
2493 | return t; | |
2494 | } | |
2495 | } | |
2496 | if (!ctx->quiet) | |
2497 | error_at (loc, "deallocation of storage that was " | |
2498 | "not previously allocated"); | |
2499 | *non_constant_p = true; | |
2500 | return t; | |
2501 | } | |
2502 | } | |
cf650568 JJ |
2503 | /* Allow placement new in std::construct_at, just return the second |
2504 | argument. */ | |
8412b939 JJ |
2505 | if (TREE_CODE (t) == CALL_EXPR |
2506 | && cxx_placement_new_fn (fun) | |
2ffc2645 | 2507 | && is_std_construct_at (ctx->call)) |
cf650568 JJ |
2508 | { |
2509 | const int nargs = call_expr_nargs (t); | |
2510 | tree arg1 = NULL_TREE; | |
2511 | for (int i = 0; i < nargs; ++i) | |
2512 | { | |
2513 | tree arg = CALL_EXPR_ARG (t, i); | |
2514 | arg = cxx_eval_constant_expression (ctx, arg, false, | |
2515 | non_constant_p, overflow_p); | |
cf650568 JJ |
2516 | if (i == 1) |
2517 | arg1 = arg; | |
2805fcb3 JJ |
2518 | else |
2519 | VERIFY_CONSTANT (arg); | |
cf650568 JJ |
2520 | } |
2521 | gcc_assert (arg1); | |
2522 | return arg1; | |
2523 | } | |
22edf943 MP |
2524 | else if (cxx_dynamic_cast_fn_p (fun)) |
2525 | return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p); | |
2526 | ||
2b3ab879 | 2527 | if (!ctx->quiet) |
2d76680f | 2528 | { |
11399477 | 2529 | if (!lambda_static_thunk_p (fun)) |
84fa214d | 2530 | error_at (loc, "call to non-%<constexpr%> function %qD", fun); |
2d76680f PC |
2531 | explain_invalid_constexpr_fn (fun); |
2532 | } | |
2533 | *non_constant_p = true; | |
2534 | return t; | |
2535 | } | |
2536 | ||
86461cad JM |
2537 | constexpr_ctx new_ctx = *ctx; |
2538 | if (DECL_CONSTRUCTOR_P (fun) && !ctx->object | |
2539 | && TREE_CODE (t) == AGGR_INIT_EXPR) | |
2540 | { | |
2541 | /* We want to have an initialization target for an AGGR_INIT_EXPR. | |
2542 | If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */ | |
2543 | new_ctx.object = AGGR_INIT_EXPR_SLOT (t); | |
2544 | tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL); | |
e8c48716 | 2545 | CONSTRUCTOR_NO_CLEARING (ctor) = true; |
8e007055 | 2546 | ctx->global->values.put (new_ctx.object, ctor); |
86461cad JM |
2547 | ctx = &new_ctx; |
2548 | } | |
2549 | ||
2d76680f PC |
2550 | /* Shortcut trivial constructor/op=. */ |
2551 | if (trivial_fn_p (fun)) | |
2552 | { | |
86461cad | 2553 | tree init = NULL_TREE; |
2d76680f | 2554 | if (call_expr_nargs (t) == 2) |
86461cad | 2555 | init = convert_from_reference (get_nth_callarg (t, 1)); |
2d76680f PC |
2556 | else if (TREE_CODE (t) == AGGR_INIT_EXPR |
2557 | && AGGR_INIT_ZERO_FIRST (t)) | |
86461cad JM |
2558 | init = build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false); |
2559 | if (init) | |
2560 | { | |
2561 | tree op = get_nth_callarg (t, 0); | |
2562 | if (is_dummy_object (op)) | |
2563 | op = ctx->object; | |
2564 | else | |
2565 | op = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (op)), op); | |
2566 | tree set = build2 (MODIFY_EXPR, TREE_TYPE (op), op, init); | |
c8816908 JM |
2567 | new_ctx.call = &new_call; |
2568 | return cxx_eval_constant_expression (&new_ctx, set, lval, | |
86461cad JM |
2569 | non_constant_p, overflow_p); |
2570 | } | |
2d76680f PC |
2571 | } |
2572 | ||
1595fe44 JM |
2573 | bool non_constant_args = false; |
2574 | new_call.bindings | |
2575 | = cxx_bind_parameters_in_call (ctx, t, fun, non_constant_p, | |
2576 | overflow_p, &non_constant_args); | |
2577 | ||
2578 | /* We build up the bindings list before we know whether we already have this | |
2579 | call cached. If we don't end up saving these bindings, ggc_free them when | |
2580 | this function exits. */ | |
2581 | class free_bindings | |
2582 | { | |
2583 | tree *bindings; | |
2584 | public: | |
2585 | free_bindings (tree &b): bindings (&b) { } | |
2586 | ~free_bindings () { if (bindings) ggc_free (*bindings); } | |
2587 | void preserve () { bindings = NULL; } | |
2588 | } fb (new_call.bindings); | |
2589 | ||
2590 | if (*non_constant_p) | |
2591 | return t; | |
2592 | ||
81371eff JM |
2593 | /* We can't defer instantiating the function any longer. */ |
2594 | if (!DECL_INITIAL (fun) | |
f65a3299 PP |
2595 | && DECL_TEMPLOID_INSTANTIATION (fun) |
2596 | && !uid_sensitive_constexpr_evaluation_p ()) | |
81371eff | 2597 | { |
f065303f JM |
2598 | location_t save_loc = input_location; |
2599 | input_location = loc; | |
81371eff | 2600 | ++function_depth; |
9aeadd8c JM |
2601 | if (ctx->manifestly_const_eval) |
2602 | FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true; | |
81371eff JM |
2603 | instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false); |
2604 | --function_depth; | |
f065303f | 2605 | input_location = save_loc; |
81371eff JM |
2606 | } |
2607 | ||
2d76680f | 2608 | /* If in direct recursive call, optimize definition search. */ |
c8816908 | 2609 | if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun) |
3e605b20 | 2610 | new_call.fundef = ctx->call->fundef; |
2d76680f PC |
2611 | else |
2612 | { | |
2613 | new_call.fundef = retrieve_constexpr_fundef (fun); | |
4ddcdbf9 | 2614 | if (new_call.fundef == NULL || new_call.fundef->body == NULL |
4f05d85a | 2615 | || new_call.fundef->result == error_mark_node |
4ddcdbf9 | 2616 | || fun == current_function_decl) |
2d76680f | 2617 | { |
2b3ab879 | 2618 | if (!ctx->quiet) |
2d76680f | 2619 | { |
4ddcdbf9 JM |
2620 | /* We need to check for current_function_decl here in case we're |
2621 | being called during cp_fold_function, because at that point | |
2622 | DECL_INITIAL is set properly and we have a fundef but we | |
2623 | haven't lowered invisirefs yet (c++/70344). */ | |
2624 | if (DECL_INITIAL (fun) == error_mark_node | |
2625 | || fun == current_function_decl) | |
ddd6d421 JM |
2626 | error_at (loc, "%qD called in a constant expression before its " |
2627 | "definition is complete", fun); | |
2628 | else if (DECL_INITIAL (fun)) | |
2d76680f | 2629 | { |
98e5a19a JM |
2630 | /* The definition of fun was somehow unsuitable. But pretend |
2631 | that lambda static thunks don't exist. */ | |
2632 | if (!lambda_static_thunk_p (fun)) | |
2633 | error_at (loc, "%qD called in a constant expression", fun); | |
2d76680f PC |
2634 | explain_invalid_constexpr_fn (fun); |
2635 | } | |
2636 | else | |
2637 | error_at (loc, "%qD used before its definition", fun); | |
2638 | } | |
2639 | *non_constant_p = true; | |
2640 | return t; | |
2641 | } | |
2642 | } | |
60813a46 | 2643 | |
2d76680f PC |
2644 | depth_ok = push_cx_call_context (t); |
2645 | ||
caf17f3a | 2646 | /* Remember the object we are constructing or destructing. */ |
04e1749c | 2647 | tree new_obj = NULL_TREE; |
caf17f3a | 2648 | if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun)) |
04e1749c | 2649 | { |
caf17f3a | 2650 | /* In a cdtor, it should be the first `this' argument. |
04e1749c MP |
2651 | At this point it has already been evaluated in the call |
2652 | to cxx_bind_parameters_in_call. */ | |
2653 | new_obj = TREE_VEC_ELT (new_call.bindings, 0); | |
2654 | STRIP_NOPS (new_obj); | |
2655 | if (TREE_CODE (new_obj) == ADDR_EXPR) | |
2656 | new_obj = TREE_OPERAND (new_obj, 0); | |
64da1b76 PP |
2657 | |
2658 | if (ctx->call && ctx->call->fundef | |
2659 | && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl)) | |
2660 | { | |
2661 | tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0); | |
2662 | STRIP_NOPS (cur_obj); | |
2663 | if (TREE_CODE (cur_obj) == ADDR_EXPR) | |
2664 | cur_obj = TREE_OPERAND (cur_obj, 0); | |
2665 | if (new_obj == cur_obj) | |
2666 | /* We're calling the target constructor of a delegating | |
2667 | constructor, or accessing a base subobject through a | |
2668 | NOP_EXPR as part of a call to a base constructor, so | |
2669 | there is no new (sub)object. */ | |
2670 | new_obj = NULL_TREE; | |
2671 | } | |
04e1749c MP |
2672 | } |
2673 | ||
12d9ce19 | 2674 | tree result = NULL_TREE; |
2d76680f | 2675 | |
12d9ce19 | 2676 | constexpr_call *entry = NULL; |
4a58d2fe | 2677 | if (depth_ok && !non_constant_args && ctx->strict) |
2d76680f | 2678 | { |
cce3ae91 JJ |
2679 | new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef); |
2680 | new_call.hash | |
2681 | = iterative_hash_template_arg (new_call.bindings, new_call.hash); | |
2682 | new_call.hash | |
13de99bc | 2683 | = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash); |
12d9ce19 JM |
2684 | |
2685 | /* If we have seen this call before, we are done. */ | |
2686 | maybe_initialize_constexpr_call_table (); | |
2687 | constexpr_call **slot | |
2688 | = constexpr_call_table->find_slot (&new_call, INSERT); | |
2689 | entry = *slot; | |
2690 | if (entry == NULL) | |
2691 | { | |
7ffc7de5 JM |
2692 | /* Only cache up to constexpr_cache_depth to limit memory use. */ |
2693 | if (depth_ok < constexpr_cache_depth) | |
2694 | { | |
2695 | /* We need to keep a pointer to the entry, not just the slot, as | |
2696 | the slot can move during evaluation of the body. */ | |
2697 | *slot = entry = ggc_alloc<constexpr_call> (); | |
2698 | *entry = new_call; | |
2699 | fb.preserve (); | |
2700 | } | |
12d9ce19 | 2701 | } |
7ffc7de5 JM |
2702 | /* Calls that are in progress have their result set to NULL, so that we |
2703 | can detect circular dependencies. Now that we only cache up to | |
2704 | constexpr_cache_depth this won't catch circular dependencies that | |
2705 | start deeper, but they'll hit the recursion or ops limit. */ | |
12d9ce19 JM |
2706 | else if (entry->result == NULL) |
2707 | { | |
2708 | if (!ctx->quiet) | |
2709 | error ("call has circular dependency"); | |
2710 | *non_constant_p = true; | |
2711 | entry->result = result = error_mark_node; | |
2712 | } | |
2713 | else | |
2714 | result = entry->result; | |
2d76680f PC |
2715 | } |
2716 | ||
2717 | if (!depth_ok) | |
2718 | { | |
2b3ab879 | 2719 | if (!ctx->quiet) |
84fa214d | 2720 | error ("%<constexpr%> evaluation depth exceeds maximum of %d (use " |
7e7a6ed7 | 2721 | "%<-fconstexpr-depth=%> to increase the maximum)", |
2d76680f PC |
2722 | max_constexpr_depth); |
2723 | *non_constant_p = true; | |
12d9ce19 | 2724 | result = error_mark_node; |
2d76680f PC |
2725 | } |
2726 | else | |
2727 | { | |
f74f6092 | 2728 | bool cacheable = true; |
de54de93 JM |
2729 | if (result && result != error_mark_node) |
2730 | /* OK */; | |
2731 | else if (!DECL_SAVED_TREE (fun)) | |
2732 | { | |
2733 | /* When at_eof >= 2, cgraph has started throwing away | |
2734 | DECL_SAVED_TREE, so fail quietly. FIXME we get here because of | |
2735 | late code generation for VEC_INIT_EXPR, which needs to be | |
2736 | completely reconsidered. */ | |
2737 | gcc_assert (at_eof >= 2 && ctx->quiet); | |
2738 | *non_constant_p = true; | |
2739 | } | |
f65a3299 | 2740 | else if (tree copy = get_fundef_copy (new_call.fundef)) |
3e605b20 | 2741 | { |
c0daf32d | 2742 | tree body, parms, res; |
620adbec | 2743 | releasing_vec ctors; |
0567dcd2 | 2744 | |
c0daf32d | 2745 | /* Reuse or create a new unshared copy of this function's body. */ |
97f3003f JM |
2746 | body = TREE_PURPOSE (copy); |
2747 | parms = TREE_VALUE (copy); | |
2748 | res = TREE_TYPE (copy); | |
0567dcd2 MP |
2749 | |
2750 | /* Associate the bindings with the remapped parms. */ | |
2751 | tree bound = new_call.bindings; | |
2752 | tree remapped = parms; | |
3c961dc7 | 2753 | for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i) |
60813a46 | 2754 | { |
3c961dc7 | 2755 | tree arg = TREE_VEC_ELT (bound, i); |
4953b790 JM |
2756 | if (entry) |
2757 | { | |
2758 | /* Unshare args going into the hash table to separate them | |
2759 | from the caller's context, for better GC and to avoid | |
2760 | problems with verify_gimple. */ | |
db38a029 | 2761 | arg = unshare_expr_without_location (arg); |
4953b790 | 2762 | TREE_VEC_ELT (bound, i) = arg; |
5afd90c5 JJ |
2763 | |
2764 | /* And then unshare again so the callee doesn't change the | |
2765 | argument values in the hash table. XXX Could we unshare | |
2766 | lazily in cxx_eval_store_expression? */ | |
2767 | arg = unshare_constructor (arg); | |
2768 | if (TREE_CODE (arg) == CONSTRUCTOR) | |
2769 | vec_safe_push (ctors, arg); | |
4953b790 | 2770 | } |
8e007055 | 2771 | ctx->global->values.put (remapped, arg); |
0567dcd2 | 2772 | remapped = DECL_CHAIN (remapped); |
60813a46 | 2773 | } |
0567dcd2 | 2774 | /* Add the RESULT_DECL to the values map, too. */ |
cd3ca6cb JM |
2775 | gcc_assert (!DECL_BY_REFERENCE (res)); |
2776 | ctx->global->values.put (res, NULL_TREE); | |
60813a46 | 2777 | |
ee1de08d JJ |
2778 | /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that |
2779 | we can forget their values after the call. */ | |
c0daf32d | 2780 | constexpr_ctx ctx_with_save_exprs = *ctx; |
0ee28590 | 2781 | auto_vec<tree, 10> save_exprs; |
c0daf32d | 2782 | ctx_with_save_exprs.save_exprs = &save_exprs; |
1e163090 | 2783 | ctx_with_save_exprs.call = &new_call; |
f74f6092 JJ |
2784 | unsigned save_heap_alloc_count = ctx->global->heap_vars.length (); |
2785 | unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count; | |
c0daf32d | 2786 | |
caf17f3a MP |
2787 | /* If this is a constexpr destructor, the object's const and volatile |
2788 | semantics are no longer in effect; see [class.dtor]p5. */ | |
2789 | if (new_obj && DECL_DESTRUCTOR_P (fun)) | |
2790 | cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false, | |
2791 | non_constant_p, overflow_p); | |
2792 | ||
0567dcd2 | 2793 | tree jump_target = NULL_TREE; |
c0daf32d | 2794 | cxx_eval_constant_expression (&ctx_with_save_exprs, body, |
0567dcd2 MP |
2795 | lval, non_constant_p, overflow_p, |
2796 | &jump_target); | |
60813a46 | 2797 | |
0567dcd2 | 2798 | if (DECL_CONSTRUCTOR_P (fun)) |
de07cff9 PP |
2799 | { |
2800 | /* This can be null for a subobject constructor call, in | |
2801 | which case what we care about is the initialization | |
2802 | side-effects rather than the value. We could get at the | |
2803 | value by evaluating *this, but we don't bother; there's | |
2804 | no need to put such a call in the hash table. */ | |
2805 | result = lval ? ctx->object : ctx->ctor; | |
2806 | ||
2807 | /* If we've just evaluated a subobject constructor call for an | |
2808 | empty union member, it might not have produced a side effect | |
2809 | that actually activated the union member. So produce such a | |
2810 | side effect now to ensure the union appears initialized. */ | |
2811 | if (!result && new_obj | |
2812 | && TREE_CODE (new_obj) == COMPONENT_REF | |
2813 | && TREE_CODE (TREE_TYPE | |
2814 | (TREE_OPERAND (new_obj, 0))) == UNION_TYPE | |
2815 | && is_really_empty_class (TREE_TYPE (new_obj), | |
2816 | /*ignore_vptr*/false)) | |
2817 | { | |
2818 | tree activate = build2 (MODIFY_EXPR, TREE_TYPE (new_obj), | |
2819 | new_obj, | |
2820 | build_constructor (TREE_TYPE (new_obj), | |
2821 | NULL)); | |
2822 | cxx_eval_constant_expression (ctx, activate, lval, | |
2823 | non_constant_p, overflow_p); | |
2824 | ggc_free (activate); | |
2825 | } | |
2826 | } | |
0567dcd2 MP |
2827 | else if (VOID_TYPE_P (TREE_TYPE (res))) |
2828 | result = void_node; | |
2829 | else | |
2830 | { | |
cd3ca6cb | 2831 | result = *ctx->global->values.get (res); |
0567dcd2 | 2832 | if (result == NULL_TREE && !*non_constant_p) |
60813a46 | 2833 | { |
0567dcd2 | 2834 | if (!ctx->quiet) |
84fa214d | 2835 | error ("%<constexpr%> call flows off the end " |
0567dcd2 MP |
2836 | "of the function"); |
2837 | *non_constant_p = true; | |
60813a46 | 2838 | } |
60813a46 | 2839 | } |
0567dcd2 | 2840 | |
04e1749c MP |
2841 | /* At this point, the object's constructor will have run, so |
2842 | the object is no longer under construction, and its possible | |
2843 | 'const' semantics now apply. Make a note of this fact by | |
2844 | marking the CONSTRUCTOR TREE_READONLY. */ | |
caf17f3a MP |
2845 | if (new_obj && DECL_CONSTRUCTOR_P (fun)) |
2846 | cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true, | |
2847 | non_constant_p, overflow_p); | |
04e1749c | 2848 | |
ee1de08d JJ |
2849 | /* Forget the saved values of the callee's SAVE_EXPRs and |
2850 | TARGET_EXPRs. */ | |
3f207ab3 | 2851 | for (tree save_expr : save_exprs) |
8e007055 | 2852 | ctx->global->values.remove (save_expr); |
c0daf32d | 2853 | |
0567dcd2 MP |
2854 | /* Remove the parms/result from the values map. Is it worth |
2855 | bothering to do this when the map itself is only live for | |
2856 | one constexpr evaluation? If so, maybe also clear out | |
2857 | other vars from call, maybe in BIND_EXPR handling? */ | |
8e007055 | 2858 | ctx->global->values.remove (res); |
0567dcd2 | 2859 | for (tree parm = parms; parm; parm = TREE_CHAIN (parm)) |
8e007055 | 2860 | ctx->global->values.remove (parm); |
c0daf32d | 2861 | |
620adbec JM |
2862 | /* Free any parameter CONSTRUCTORs we aren't returning directly. */ |
2863 | while (!ctors->is_empty ()) | |
2864 | { | |
2865 | tree c = ctors->pop (); | |
2866 | if (c != result) | |
2867 | free_constructor (c); | |
2868 | } | |
2869 | ||
c0daf32d PP |
2870 | /* Make the unshared function copy we used available for re-use. */ |
2871 | save_fundef_copy (fun, copy); | |
f74f6092 JJ |
2872 | |
2873 | /* If the call allocated some heap object that hasn't been | |
2874 | deallocated during the call, or if it deallocated some heap | |
2875 | object it has not allocated, the call isn't really stateless | |
2876 | for the constexpr evaluation and should not be cached. | |
2877 | It is fine if the call allocates something and deallocates it | |
2878 | too. */ | |
2879 | if (entry | |
2880 | && (save_heap_alloc_count != ctx->global->heap_vars.length () | |
2881 | || (save_heap_dealloc_count | |
2882 | != ctx->global->heap_dealloc_count))) | |
2883 | { | |
2884 | tree heap_var; | |
2885 | unsigned int i; | |
2886 | if ((ctx->global->heap_vars.length () | |
2887 | - ctx->global->heap_dealloc_count) | |
2888 | != save_heap_alloc_count - save_heap_dealloc_count) | |
2889 | cacheable = false; | |
2890 | else | |
2891 | FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var, | |
2892 | save_heap_alloc_count) | |
2893 | if (DECL_NAME (heap_var) != heap_deleted_identifier) | |
2894 | { | |
2895 | cacheable = false; | |
2896 | break; | |
2897 | } | |
2898 | } | |
b2562229 PP |
2899 | |
2900 | /* Rewrite all occurrences of the function's RESULT_DECL with the | |
2901 | current object under construction. */ | |
2902 | if (!*non_constant_p && ctx->object | |
bb1f0b50 | 2903 | && CLASS_TYPE_P (TREE_TYPE (res)) |
b2562229 PP |
2904 | && !is_empty_class (TREE_TYPE (res))) |
2905 | if (replace_result_decl (&result, res, ctx->object)) | |
2906 | cacheable = false; | |
3e605b20 | 2907 | } |
aaa26bf4 JM |
2908 | else |
2909 | /* Couldn't get a function copy to evaluate. */ | |
2910 | *non_constant_p = true; | |
60813a46 | 2911 | |
2d76680f PC |
2912 | if (result == error_mark_node) |
2913 | *non_constant_p = true; | |
52228180 | 2914 | if (*non_constant_p || *overflow_p) |
12d9ce19 | 2915 | result = error_mark_node; |
0567dcd2 | 2916 | else if (!result) |
60813a46 | 2917 | result = void_node; |
12d9ce19 | 2918 | if (entry) |
f74f6092 | 2919 | entry->result = cacheable ? result : error_mark_node; |
2d76680f PC |
2920 | } |
2921 | ||
7906797e MP |
2922 | /* The result of a constexpr function must be completely initialized. |
2923 | ||
2924 | However, in C++20, a constexpr constructor doesn't necessarily have | |
2925 | to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING | |
2926 | in order to detect reading an unitialized object in constexpr instead | |
2927 | of value-initializing it. (reduced_constant_expression_p is expected to | |
2928 | take care of clearing the flag.) */ | |
2929 | if (TREE_CODE (result) == CONSTRUCTOR | |
b04445d4 | 2930 | && (cxx_dialect < cxx20 |
7906797e | 2931 | || !DECL_CONSTRUCTOR_P (fun))) |
ecc57615 | 2932 | clear_no_implicit_zero (result); |
f64e0c02 | 2933 | |
2d76680f | 2934 | pop_cx_call_context (); |
9b9eb42a | 2935 | return result; |
2d76680f PC |
2936 | } |
2937 | ||
7906797e MP |
2938 | /* Return true if T is a valid constant initializer. If a CONSTRUCTOR |
2939 | initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be | |
2940 | cleared. | |
2941 | FIXME speed this up, it's taking 16% of compile time on sieve testcase. */ | |
2d76680f PC |
2942 | |
2943 | bool | |
2944 | reduced_constant_expression_p (tree t) | |
2945 | { | |
4930c53e MP |
2946 | if (t == NULL_TREE) |
2947 | return false; | |
2948 | ||
2d76680f PC |
2949 | switch (TREE_CODE (t)) |
2950 | { | |
2951 | case PTRMEM_CST: | |
2952 | /* Even if we can't lower this yet, it's constant. */ | |
2953 | return true; | |
2954 | ||
2955 | case CONSTRUCTOR: | |
2956 | /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */ | |
fae00a0a | 2957 | tree field; |
e8c48716 | 2958 | if (CONSTRUCTOR_NO_CLEARING (t)) |
6f11ddd8 JM |
2959 | { |
2960 | if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE) | |
2961 | /* An initialized vector would have a VECTOR_CST. */ | |
2962 | return false; | |
b04445d4 | 2963 | else if (cxx_dialect >= cxx20 |
b599bf9d | 2964 | && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) |
baf05d54 PP |
2965 | { |
2966 | /* There must be a valid constant initializer at every array | |
2967 | index. */ | |
2968 | tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t))); | |
2969 | tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t))); | |
2970 | tree cursor = min; | |
fae00a0a | 2971 | for (auto &e: CONSTRUCTOR_ELTS (t)) |
baf05d54 | 2972 | { |
fae00a0a | 2973 | if (!reduced_constant_expression_p (e.value)) |
baf05d54 | 2974 | return false; |
fae00a0a | 2975 | if (array_index_cmp (cursor, e.index) != 0) |
baf05d54 | 2976 | return false; |
fae00a0a JM |
2977 | if (TREE_CODE (e.index) == RANGE_EXPR) |
2978 | cursor = TREE_OPERAND (e.index, 1); | |
baf05d54 PP |
2979 | cursor = int_const_binop (PLUS_EXPR, cursor, size_one_node); |
2980 | } | |
2981 | if (find_array_ctor_elt (t, max) == -1) | |
2982 | return false; | |
2983 | goto ok; | |
2984 | } | |
b04445d4 | 2985 | else if (cxx_dialect >= cxx20 |
b599bf9d PP |
2986 | && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE) |
2987 | { | |
2988 | if (CONSTRUCTOR_NELTS (t) == 0) | |
2989 | /* An initialized union has a constructor element. */ | |
2990 | return false; | |
2991 | /* And it only initializes one member. */ | |
2992 | field = NULL_TREE; | |
2993 | } | |
6f11ddd8 JM |
2994 | else |
2995 | field = next_initializable_field (TYPE_FIELDS (TREE_TYPE (t))); | |
2996 | } | |
7368cfa4 JM |
2997 | else |
2998 | field = NULL_TREE; | |
fae00a0a | 2999 | for (auto &e: CONSTRUCTOR_ELTS (t)) |
125db6a1 | 3000 | { |
4930c53e MP |
3001 | /* If VAL is null, we're in the middle of initializing this |
3002 | element. */ | |
fae00a0a | 3003 | if (!reduced_constant_expression_p (e.value)) |
125db6a1 | 3004 | return false; |
d6ff2207 | 3005 | /* Empty class field may or may not have an initializer. */ |
fae00a0a | 3006 | for (; field && e.index != field; |
d6ff2207 MP |
3007 | field = next_initializable_field (DECL_CHAIN (field))) |
3008 | if (!is_really_empty_class (TREE_TYPE (field), | |
3009 | /*ignore_vptr*/false)) | |
3010 | return false; | |
7368cfa4 | 3011 | if (field) |
d6ff2207 | 3012 | field = next_initializable_field (DECL_CHAIN (field)); |
125db6a1 | 3013 | } |
7906797e MP |
3014 | /* There could be a non-empty field at the end. */ |
3015 | for (; field; field = next_initializable_field (DECL_CHAIN (field))) | |
3016 | if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false)) | |
3017 | return false; | |
baf05d54 | 3018 | ok: |
7906797e | 3019 | if (CONSTRUCTOR_NO_CLEARING (t)) |
7368cfa4 | 3020 | /* All the fields are initialized. */ |
e8c48716 | 3021 | CONSTRUCTOR_NO_CLEARING (t) = false; |
2d76680f PC |
3022 | return true; |
3023 | ||
3024 | default: | |
3025 | /* FIXME are we calling this too much? */ | |
3026 | return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE; | |
3027 | } | |
3028 | } | |
3029 | ||
3030 | /* Some expressions may have constant operands but are not constant | |
7d75ea04 JJ |
3031 | themselves, such as 1/0. Call this function to check for that |
3032 | condition. | |
2d76680f PC |
3033 | |
3034 | We only call this in places that require an arithmetic constant, not in | |
3035 | places where we might have a non-constant expression that can be a | |
3036 | component of a constant expression, such as the address of a constexpr | |
3037 | variable that might be dereferenced later. */ | |
3038 | ||
3039 | static bool | |
3040 | verify_constant (tree t, bool allow_non_constant, bool *non_constant_p, | |
3041 | bool *overflow_p) | |
3042 | { | |
596334fa JM |
3043 | if (!*non_constant_p && !reduced_constant_expression_p (t) |
3044 | && t != void_node) | |
2d76680f PC |
3045 | { |
3046 | if (!allow_non_constant) | |
3047 | error ("%q+E is not a constant expression", t); | |
3048 | *non_constant_p = true; | |
3049 | } | |
3050 | if (TREE_OVERFLOW_P (t)) | |
3051 | { | |
3052 | if (!allow_non_constant) | |
3053 | { | |
3054 | permerror (input_location, "overflow in constant expression"); | |
3055 | /* If we're being permissive (and are in an enforcing | |
3056 | context), ignore the overflow. */ | |
3057 | if (flag_permissive) | |
3058 | return *non_constant_p; | |
3059 | } | |
3060 | *overflow_p = true; | |
3061 | } | |
3062 | return *non_constant_p; | |
3063 | } | |
3064 | ||
253a921b MP |
3065 | /* Check whether the shift operation with code CODE and type TYPE on LHS |
3066 | and RHS is undefined. If it is, give an error with an explanation, | |
3067 | and return true; return false otherwise. */ | |
3068 | ||
3069 | static bool | |
3070 | cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx, | |
3071 | enum tree_code code, tree type, tree lhs, tree rhs) | |
3072 | { | |
3073 | if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR) | |
3074 | || TREE_CODE (lhs) != INTEGER_CST | |
3075 | || TREE_CODE (rhs) != INTEGER_CST) | |
3076 | return false; | |
3077 | ||
3078 | tree lhstype = TREE_TYPE (lhs); | |
3079 | unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs)); | |
3080 | ||
3081 | /* [expr.shift] The behavior is undefined if the right operand | |
3082 | is negative, or greater than or equal to the length in bits | |
3083 | of the promoted left operand. */ | |
3084 | if (tree_int_cst_sgn (rhs) == -1) | |
3085 | { | |
3086 | if (!ctx->quiet) | |
5342156c MP |
3087 | permerror (loc, "right operand of shift expression %q+E is negative", |
3088 | build2_loc (loc, code, type, lhs, rhs)); | |
3089 | return (!flag_permissive || ctx->quiet); | |
253a921b MP |
3090 | } |
3091 | if (compare_tree_int (rhs, uprec) >= 0) | |
3092 | { | |
3093 | if (!ctx->quiet) | |
a9c697b8 MS |
3094 | permerror (loc, "right operand of shift expression %q+E is greater " |
3095 | "than or equal to the precision %wu of the left operand", | |
3096 | build2_loc (loc, code, type, lhs, rhs), uprec); | |
5342156c | 3097 | return (!flag_permissive || ctx->quiet); |
253a921b MP |
3098 | } |
3099 | ||
3100 | /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...] | |
3101 | if E1 has a signed type and non-negative value, and E1x2^E2 is | |
3102 | representable in the corresponding unsigned type of the result type, | |
3103 | then that value, converted to the result type, is the resulting value; | |
8ee09943 | 3104 | otherwise, the behavior is undefined. |
b04445d4 | 3105 | For C++20: |
8ee09943 JJ |
3106 | The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo |
3107 | 2^N, where N is the range exponent of the type of the result. */ | |
3108 | if (code == LSHIFT_EXPR | |
3109 | && !TYPE_UNSIGNED (lhstype) | |
3110 | && cxx_dialect >= cxx11 | |
b04445d4 | 3111 | && cxx_dialect < cxx20) |
253a921b MP |
3112 | { |
3113 | if (tree_int_cst_sgn (lhs) == -1) | |
3114 | { | |
3115 | if (!ctx->quiet) | |
5342156c MP |
3116 | permerror (loc, |
3117 | "left operand of shift expression %q+E is negative", | |
3118 | build2_loc (loc, code, type, lhs, rhs)); | |
3119 | return (!flag_permissive || ctx->quiet); | |
253a921b MP |
3120 | } |
3121 | /* For signed x << y the following: | |
3122 | (unsigned) x >> ((prec (lhs) - 1) - y) | |
3123 | if > 1, is undefined. The right-hand side of this formula | |
3124 | is the highest bit of the LHS that can be set (starting from 0), | |
3125 | so that the shift doesn't overflow. We then right-shift the LHS | |
3126 | to see whether any other bit is set making the original shift | |
3127 | undefined -- the result is not representable in the corresponding | |
3128 | unsigned type. */ | |
3129 | tree t = build_int_cst (unsigned_type_node, uprec - 1); | |
3130 | t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs); | |
3131 | tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs); | |
3132 | t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t); | |
3133 | if (tree_int_cst_lt (integer_one_node, t)) | |
3134 | { | |
3135 | if (!ctx->quiet) | |
5342156c MP |
3136 | permerror (loc, "shift expression %q+E overflows", |
3137 | build2_loc (loc, code, type, lhs, rhs)); | |
3138 | return (!flag_permissive || ctx->quiet); | |
253a921b MP |
3139 | } |
3140 | } | |
3141 | return false; | |
3142 | } | |
3143 | ||
2d76680f PC |
3144 | /* Subroutine of cxx_eval_constant_expression. |
3145 | Attempt to reduce the unary expression tree T to a compile time value. | |
3146 | If successful, return the value. Otherwise issue a diagnostic | |
3147 | and return error_mark_node. */ | |
3148 | ||
3149 | static tree | |
3e605b20 | 3150 | cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t, |
12d9ce19 | 3151 | bool /*lval*/, |
2d76680f PC |
3152 | bool *non_constant_p, bool *overflow_p) |
3153 | { | |
3154 | tree r; | |
3155 | tree orig_arg = TREE_OPERAND (t, 0); | |
12d9ce19 JM |
3156 | tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false, |
3157 | non_constant_p, overflow_p); | |
2d76680f | 3158 | VERIFY_CONSTANT (arg); |
f899317e JM |
3159 | location_t loc = EXPR_LOCATION (t); |
3160 | enum tree_code code = TREE_CODE (t); | |
3161 | tree type = TREE_TYPE (t); | |
3162 | r = fold_unary_loc (loc, code, type, arg); | |
3163 | if (r == NULL_TREE) | |
3164 | { | |
3165 | if (arg == orig_arg) | |
3166 | r = t; | |
3167 | else | |
3168 | r = build1_loc (loc, code, type, arg); | |
3169 | } | |
2d76680f PC |
3170 | VERIFY_CONSTANT (r); |
3171 | return r; | |
3172 | } | |
3173 | ||
ea8661cd JJ |
3174 | /* Helper function for cxx_eval_binary_expression. Try to optimize |
3175 | original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the | |
3176 | generic folding should be used. */ | |
3177 | ||
3178 | static tree | |
3179 | cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t, | |
3180 | tree lhs, tree rhs, bool *non_constant_p, | |
3181 | bool *overflow_p) | |
3182 | { | |
3183 | STRIP_NOPS (lhs); | |
3184 | if (TREE_CODE (lhs) != ADDR_EXPR) | |
3185 | return NULL_TREE; | |
3186 | ||
3187 | lhs = TREE_OPERAND (lhs, 0); | |
3188 | ||
3189 | /* &A[i] p+ j => &A[i + j] */ | |
3190 | if (TREE_CODE (lhs) == ARRAY_REF | |
3191 | && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST | |
3192 | && TREE_CODE (rhs) == INTEGER_CST | |
3193 | && TYPE_SIZE_UNIT (TREE_TYPE (lhs)) | |
3194 | && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST) | |
3195 | { | |
3196 | tree orig_type = TREE_TYPE (t); | |
3197 | location_t loc = EXPR_LOCATION (t); | |
3198 | tree type = TREE_TYPE (lhs); | |
3199 | ||
3200 | t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1)); | |
3201 | tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0))); | |
3202 | nelts = cxx_eval_constant_expression (ctx, nelts, false, non_constant_p, | |
3203 | overflow_p); | |
3204 | if (*non_constant_p) | |
3205 | return NULL_TREE; | |
3206 | /* Don't fold an out-of-bound access. */ | |
3207 | if (!tree_int_cst_le (t, nelts)) | |
3208 | return NULL_TREE; | |
3209 | rhs = cp_fold_convert (ssizetype, rhs); | |
3210 | /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT. | |
3211 | constexpr int A[1]; ... (char *)&A[0] + 1 */ | |
3212 | if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype, | |
3213 | rhs, TYPE_SIZE_UNIT (type)))) | |
3214 | return NULL_TREE; | |
3215 | /* Make sure to treat the second operand of POINTER_PLUS_EXPR | |
3216 | as signed. */ | |
3217 | rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs, | |
3218 | TYPE_SIZE_UNIT (type)); | |
3219 | t = size_binop_loc (loc, PLUS_EXPR, rhs, t); | |
3220 | t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0), | |
3221 | t, NULL_TREE, NULL_TREE); | |
3222 | t = cp_build_addr_expr (t, tf_warning_or_error); | |
3223 | t = cp_fold_convert (orig_type, t); | |
3224 | return cxx_eval_constant_expression (ctx, t, /*lval*/false, | |
3225 | non_constant_p, overflow_p); | |
3226 | } | |
3227 | ||
3228 | return NULL_TREE; | |
3229 | } | |
3230 | ||
2d76680f PC |
3231 | /* Subroutine of cxx_eval_constant_expression. |
3232 | Like cxx_eval_unary_expression, except for binary expressions. */ | |
3233 | ||
3234 | static tree | |
3e605b20 | 3235 | cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t, |
dfffecb8 | 3236 | bool lval, |
2d76680f PC |
3237 | bool *non_constant_p, bool *overflow_p) |
3238 | { | |
618d6c1c | 3239 | tree r = NULL_TREE; |
2d76680f PC |
3240 | tree orig_lhs = TREE_OPERAND (t, 0); |
3241 | tree orig_rhs = TREE_OPERAND (t, 1); | |
3242 | tree lhs, rhs; | |
12d9ce19 | 3243 | lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false, |
5a804683 | 3244 | non_constant_p, overflow_p); |
c8a66fc9 JM |
3245 | /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer |
3246 | subtraction. */ | |
3247 | if (*non_constant_p) | |
3248 | return t; | |
12d9ce19 | 3249 | rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false, |
5a804683 | 3250 | non_constant_p, overflow_p); |
c8a66fc9 JM |
3251 | if (*non_constant_p) |
3252 | return t; | |
f899317e JM |
3253 | |
3254 | location_t loc = EXPR_LOCATION (t); | |
3255 | enum tree_code code = TREE_CODE (t); | |
3256 | tree type = TREE_TYPE (t); | |
618d6c1c PP |
3257 | |
3258 | if (code == EQ_EXPR || code == NE_EXPR) | |
3259 | { | |
3260 | bool is_code_eq = (code == EQ_EXPR); | |
3261 | ||
3262 | if (TREE_CODE (lhs) == PTRMEM_CST | |
3263 | && TREE_CODE (rhs) == PTRMEM_CST) | |
9b0607de JM |
3264 | { |
3265 | tree lmem = PTRMEM_CST_MEMBER (lhs); | |
3266 | tree rmem = PTRMEM_CST_MEMBER (rhs); | |
3267 | bool eq; | |
3268 | if (TREE_CODE (lmem) == TREE_CODE (rmem) | |
3269 | && TREE_CODE (lmem) == FIELD_DECL | |
3270 | && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE | |
3271 | && same_type_p (DECL_CONTEXT (lmem), | |
3272 | DECL_CONTEXT (rmem))) | |
3273 | /* If both refer to (possibly different) members of the same union | |
3274 | (12.3), they compare equal. */ | |
3275 | eq = true; | |
3276 | else | |
3277 | eq = cp_tree_equal (lhs, rhs); | |
3278 | r = constant_boolean_node (eq == is_code_eq, type); | |
3279 | } | |
618d6c1c PP |
3280 | else if ((TREE_CODE (lhs) == PTRMEM_CST |
3281 | || TREE_CODE (rhs) == PTRMEM_CST) | |
3282 | && (null_member_pointer_value_p (lhs) | |
3283 | || null_member_pointer_value_p (rhs))) | |
3284 | r = constant_boolean_node (!is_code_eq, type); | |
dd5dda56 JM |
3285 | else if (TREE_CODE (lhs) == PTRMEM_CST) |
3286 | lhs = cplus_expand_constant (lhs); | |
3287 | else if (TREE_CODE (rhs) == PTRMEM_CST) | |
3288 | rhs = cplus_expand_constant (rhs); | |
618d6c1c | 3289 | } |
8bada5cd MS |
3290 | if (code == POINTER_PLUS_EXPR && !*non_constant_p |
3291 | && integer_zerop (lhs) && !integer_zerop (rhs)) | |
3292 | { | |
3293 | if (!ctx->quiet) | |
3294 | error ("arithmetic involving a null pointer in %qE", lhs); | |
bf533db8 | 3295 | *non_constant_p = true; |
8bada5cd MS |
3296 | return t; |
3297 | } | |
ea8661cd JJ |
3298 | else if (code == POINTER_PLUS_EXPR) |
3299 | r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p, | |
3300 | overflow_p); | |
b7689b96 JM |
3301 | else if (code == SPACESHIP_EXPR) |
3302 | { | |
4ed1dc12 | 3303 | r = genericize_spaceship (loc, type, lhs, rhs); |
5c64df80 JJ |
3304 | return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, |
3305 | overflow_p); | |
b7689b96 | 3306 | } |
618d6c1c PP |
3307 | |
3308 | if (r == NULL_TREE) | |
3309 | r = fold_binary_loc (loc, code, type, lhs, rhs); | |
3310 | ||
4866b2f5 JJ |
3311 | if (r == NULL_TREE |
3312 | && (code == LSHIFT_EXPR || code == RSHIFT_EXPR) | |
3313 | && TREE_CODE (lhs) == INTEGER_CST | |
3314 | && TREE_CODE (rhs) == INTEGER_CST | |
3315 | && wi::neg_p (wi::to_wide (rhs))) | |
3316 | { | |
3317 | /* For diagnostics and -fpermissive emulate previous behavior of | |
3318 | handling shifts by negative amount. */ | |
3319 | tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs); | |
3320 | if (nrhs) | |
3321 | r = fold_binary_loc (loc, | |
3322 | code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR, | |
3323 | type, lhs, nrhs); | |
3324 | } | |
3325 | ||
f899317e JM |
3326 | if (r == NULL_TREE) |
3327 | { | |
3328 | if (lhs == orig_lhs && rhs == orig_rhs) | |
3329 | r = t; | |
3330 | else | |
3331 | r = build2_loc (loc, code, type, lhs, rhs); | |
3332 | } | |
253a921b MP |
3333 | else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs)) |
3334 | *non_constant_p = true; | |
c8a66fc9 JM |
3335 | /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to |
3336 | a local array in a constexpr function. */ | |
71a93b08 | 3337 | bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs)); |
caee690e | 3338 | if (!ptr) |
134efa82 | 3339 | VERIFY_CONSTANT (r); |
2d76680f PC |
3340 | return r; |
3341 | } | |
3342 | ||
3343 | /* Subroutine of cxx_eval_constant_expression. | |
3344 | Attempt to evaluate condition expressions. Dead branches are not | |
3345 | looked into. */ | |
3346 | ||
3347 | static tree | |
3e605b20 | 3348 | cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t, |
92a596e8 | 3349 | bool lval, |
56632b27 JM |
3350 | bool *non_constant_p, bool *overflow_p, |
3351 | tree *jump_target) | |
2d76680f | 3352 | { |
3e605b20 | 3353 | tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
12d9ce19 | 3354 | /*lval*/false, |
5a804683 | 3355 | non_constant_p, overflow_p); |
2d76680f | 3356 | VERIFY_CONSTANT (val); |
117c6426 JJ |
3357 | if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t)) |
3358 | { | |
3359 | /* Evaluate the condition as if it was | |
3360 | if (__builtin_is_constant_evaluated ()), i.e. defer it if not | |
3361 | ctx->manifestly_const_eval (as sometimes we try to constant evaluate | |
3362 | without manifestly_const_eval even expressions or parts thereof which | |
3363 | will later be manifestly const_eval evaluated), otherwise fold it to | |
3364 | true. */ | |
3365 | if (ctx->manifestly_const_eval) | |
3366 | val = boolean_true_node; | |
3367 | else | |
3368 | { | |
3369 | *non_constant_p = true; | |
3370 | return t; | |
3371 | } | |
3372 | } | |
2d76680f PC |
3373 | /* Don't VERIFY_CONSTANT the other operands. */ |
3374 | if (integer_zerop (val)) | |
43574e4f JJ |
3375 | val = TREE_OPERAND (t, 2); |
3376 | else | |
3377 | val = TREE_OPERAND (t, 1); | |
3378 | if (TREE_CODE (t) == IF_STMT && !val) | |
3379 | val = void_node; | |
3380 | return cxx_eval_constant_expression (ctx, val, lval, non_constant_p, | |
3381 | overflow_p, jump_target); | |
2d76680f PC |
3382 | } |
3383 | ||
f370e36d JJ |
3384 | /* Subroutine of cxx_eval_constant_expression. |
3385 | Attempt to evaluate vector condition expressions. Unlike | |
3386 | cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal | |
3387 | ternary arithmetics operation, where all 3 arguments have to be | |
3388 | evaluated as constants and then folding computes the result from | |
3389 | them. */ | |
3390 | ||
3391 | static tree | |
3392 | cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t, | |
3393 | bool *non_constant_p, bool *overflow_p) | |
3394 | { | |
3395 | tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), | |
3396 | /*lval*/false, | |
3397 | non_constant_p, overflow_p); | |
3398 | VERIFY_CONSTANT (arg1); | |
3399 | tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), | |
3400 | /*lval*/false, | |
3401 | non_constant_p, overflow_p); | |
3402 | VERIFY_CONSTANT (arg2); | |
3403 | tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2), | |
3404 | /*lval*/false, | |
3405 | non_constant_p, overflow_p); | |
3406 | VERIFY_CONSTANT (arg3); | |
3407 | location_t loc = EXPR_LOCATION (t); | |
3408 | tree type = TREE_TYPE (t); | |
3409 | tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); | |
3410 | if (r == NULL_TREE) | |
3411 | { | |
3412 | if (arg1 == TREE_OPERAND (t, 0) | |
3413 | && arg2 == TREE_OPERAND (t, 1) | |
3414 | && arg3 == TREE_OPERAND (t, 2)) | |
3415 | r = t; | |
3416 | else | |
3417 | r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); | |
3418 | } | |
3419 | VERIFY_CONSTANT (r); | |
3420 | return r; | |
3421 | } | |
3422 | ||
ceaaf873 JM |
3423 | /* Returns less than, equal to, or greater than zero if KEY is found to be |
3424 | less than, to match, or to be greater than the constructor_elt's INDEX. */ | |
3425 | ||
3426 | static int | |
3427 | array_index_cmp (tree key, tree index) | |
3428 | { | |
3429 | gcc_assert (TREE_CODE (key) == INTEGER_CST); | |
3430 | ||
3431 | switch (TREE_CODE (index)) | |
3432 | { | |
3433 | case INTEGER_CST: | |
3434 | return tree_int_cst_compare (key, index); | |
3435 | case RANGE_EXPR: | |
3436 | { | |
3437 | tree lo = TREE_OPERAND (index, 0); | |
3438 | tree hi = TREE_OPERAND (index, 1); | |
3439 | if (tree_int_cst_lt (key, lo)) | |
3440 | return -1; | |
3441 | else if (tree_int_cst_lt (hi, key)) | |
3442 | return 1; | |
3443 | else | |
3444 | return 0; | |
3445 | } | |
3446 | default: | |
3447 | gcc_unreachable (); | |
3448 | } | |
3449 | } | |
3450 | ||
3451 | /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1 | |
3452 | if none. If INSERT is true, insert a matching element rather than fail. */ | |
3453 | ||
3454 | static HOST_WIDE_INT | |
582d2481 | 3455 | find_array_ctor_elt (tree ary, tree dindex, bool insert) |
ceaaf873 JM |
3456 | { |
3457 | if (tree_int_cst_sgn (dindex) < 0) | |
3458 | return -1; | |
3459 | ||
3460 | unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex); | |
3461 | vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary); | |
3462 | unsigned HOST_WIDE_INT len = vec_safe_length (elts); | |
3463 | ||
3464 | unsigned HOST_WIDE_INT end = len; | |
3465 | unsigned HOST_WIDE_INT begin = 0; | |
3466 | ||
3467 | /* If the last element of the CONSTRUCTOR has its own index, we can assume | |
3468 | that the same is true of the other elements and index directly. */ | |
3469 | if (end > 0) | |
3470 | { | |
fe217ba0 | 3471 | tree cindex = (*elts)[end - 1].index; |
c7c09af8 JJ |
3472 | if (cindex == NULL_TREE) |
3473 | { | |
3474 | /* Verify that if the last index is missing, all indexes | |
3475 | are missing. */ | |
3476 | if (flag_checking) | |
3477 | for (unsigned int j = 0; j < len - 1; ++j) | |
3478 | gcc_assert ((*elts)[j].index == NULL_TREE); | |
3479 | if (i < end) | |
3480 | return i; | |
3481 | else | |
3482 | { | |
3483 | begin = end; | |
3484 | if (i == end) | |
3485 | /* If the element is to be added right at the end, | |
3486 | make sure it is added with cleared index too. */ | |
3487 | dindex = NULL_TREE; | |
3488 | else if (insert) | |
3489 | /* Otherwise, in order not to break the assumption | |
3490 | that CONSTRUCTOR either has all indexes or none, | |
3491 | we need to add indexes to all elements. */ | |
3492 | for (unsigned int j = 0; j < len; ++j) | |
3493 | (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j); | |
3494 | } | |
3495 | } | |
3496 | else if (TREE_CODE (cindex) == INTEGER_CST | |
3497 | && compare_tree_int (cindex, end - 1) == 0) | |
ceaaf873 JM |
3498 | { |
3499 | if (i < end) | |
3500 | return i; | |
3501 | else | |
3502 | begin = end; | |
3503 | } | |
3504 | } | |
3505 | ||
3506 | /* Otherwise, find a matching index by means of a binary search. */ | |
3507 | while (begin != end) | |
3508 | { | |
3509 | unsigned HOST_WIDE_INT middle = (begin + end) / 2; | |
a7ccb9e7 JM |
3510 | constructor_elt &elt = (*elts)[middle]; |
3511 | tree idx = elt.index; | |
ceaaf873 | 3512 | |
a7ccb9e7 | 3513 | int cmp = array_index_cmp (dindex, idx); |
ceaaf873 JM |
3514 | if (cmp < 0) |
3515 | end = middle; | |
3516 | else if (cmp > 0) | |
3517 | begin = middle + 1; | |
3518 | else | |
a7ccb9e7 JM |
3519 | { |
3520 | if (insert && TREE_CODE (idx) == RANGE_EXPR) | |
3521 | { | |
3522 | /* We need to split the range. */ | |
3523 | constructor_elt e; | |
3524 | tree lo = TREE_OPERAND (idx, 0); | |
3525 | tree hi = TREE_OPERAND (idx, 1); | |
fe217ba0 JJ |
3526 | tree value = elt.value; |
3527 | dindex = fold_convert (sizetype, dindex); | |
a7ccb9e7 JM |
3528 | if (tree_int_cst_lt (lo, dindex)) |
3529 | { | |
3530 | /* There are still some lower elts; shorten the range. */ | |
3531 | tree new_hi = int_const_binop (MINUS_EXPR, dindex, | |
3532 | size_one_node); | |
3533 | if (tree_int_cst_equal (lo, new_hi)) | |
3534 | /* Only one element left, no longer a range. */ | |
3535 | elt.index = lo; | |
3536 | else | |
3537 | TREE_OPERAND (idx, 1) = new_hi; | |
3538 | /* Append the element we want to insert. */ | |
3539 | ++middle; | |
3540 | e.index = dindex; | |
fe217ba0 | 3541 | e.value = unshare_constructor (value); |
a7ccb9e7 JM |
3542 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e); |
3543 | } | |
3544 | else | |
3545 | /* No lower elts, the range elt is now ours. */ | |
3546 | elt.index = dindex; | |
3547 | ||
3548 | if (tree_int_cst_lt (dindex, hi)) | |
3549 | { | |
3550 | /* There are still some higher elts; append a range. */ | |
3551 | tree new_lo = int_const_binop (PLUS_EXPR, dindex, | |
3552 | size_one_node); | |
3553 | if (tree_int_cst_equal (new_lo, hi)) | |
3554 | e.index = hi; | |
3555 | else | |
3556 | e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi); | |
fe217ba0 JJ |
3557 | e.value = unshare_constructor (value); |
3558 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e); | |
a7ccb9e7 JM |
3559 | } |
3560 | } | |
3561 | return middle; | |
3562 | } | |
ceaaf873 JM |
3563 | } |
3564 | ||
3565 | if (insert) | |
3566 | { | |
3567 | constructor_elt e = { dindex, NULL_TREE }; | |
3568 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e); | |
3569 | return end; | |
3570 | } | |
3571 | ||
3572 | return -1; | |
3573 | } | |
3574 | ||
37244b21 PP |
3575 | /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no |
3576 | matching constructor_elt exists, then add one to CTOR. | |
3577 | ||
3578 | As an optimization, if POS_HINT is non-negative then it is used as a guess | |
3579 | for the (integer) index of the matching constructor_elt within CTOR. */ | |
3580 | ||
3581 | static constructor_elt * | |
077dd9b3 | 3582 | get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1) |
37244b21 PP |
3583 | { |
3584 | /* Check the hint first. */ | |
3585 | if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor) | |
3586 | && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index) | |
3587 | return CONSTRUCTOR_ELT (ctor, pos_hint); | |
3588 | ||
3589 | tree type = TREE_TYPE (ctor); | |
3590 | if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE) | |
3591 | { | |
3592 | CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE); | |
3593 | return &CONSTRUCTOR_ELTS (ctor)->last(); | |
3594 | } | |
3595 | else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE) | |
3596 | { | |
e069285c PP |
3597 | if (TREE_CODE (index) == RANGE_EXPR) |
3598 | { | |
3599 | /* Support for RANGE_EXPR index lookups is currently limited to | |
3600 | accessing an existing element via POS_HINT, or appending a new | |
3601 | element to the end of CTOR. ??? Support for other access | |
3602 | patterns may also be needed. */ | |
3603 | vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor); | |
3604 | if (vec_safe_length (elts)) | |
3605 | { | |
3606 | tree lo = TREE_OPERAND (index, 0); | |
3607 | gcc_assert (array_index_cmp (elts->last().index, lo) < 0); | |
3608 | } | |
3609 | CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE); | |
3610 | return &elts->last(); | |
3611 | } | |
3612 | ||
37244b21 PP |
3613 | HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, /*insert*/true); |
3614 | gcc_assert (i >= 0); | |
3615 | constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i); | |
3616 | gcc_assert (cep->index == NULL_TREE | |
3617 | || TREE_CODE (cep->index) != RANGE_EXPR); | |
3618 | return cep; | |
3619 | } | |
3620 | else | |
3621 | { | |
94ff4c9d JM |
3622 | gcc_assert (TREE_CODE (index) == FIELD_DECL |
3623 | && (same_type_ignoring_top_level_qualifiers_p | |
3624 | (DECL_CONTEXT (index), TREE_TYPE (ctor)))); | |
37244b21 PP |
3625 | |
3626 | /* We must keep the CONSTRUCTOR's ELTS in FIELD order. | |
3627 | Usually we meet initializers in that order, but it is | |
3628 | possible for base types to be placed not in program | |
3629 | order. */ | |
3630 | tree fields = TYPE_FIELDS (DECL_CONTEXT (index)); | |
3631 | unsigned HOST_WIDE_INT idx = 0; | |
3632 | constructor_elt *cep = NULL; | |
3633 | ||
3634 | /* Check if we're changing the active member of a union. */ | |
3635 | if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor) | |
3636 | && CONSTRUCTOR_ELT (ctor, 0)->index != index) | |
3637 | vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0); | |
3638 | /* If the bit offset of INDEX is larger than that of the last | |
3639 | constructor_elt, then we can just immediately append a new | |
3640 | constructor_elt to the end of CTOR. */ | |
3641 | else if (CONSTRUCTOR_NELTS (ctor) | |
3642 | && tree_int_cst_compare (bit_position (index), | |
3643 | bit_position (CONSTRUCTOR_ELTS (ctor) | |
3644 | ->last().index)) > 0) | |
3645 | { | |
3646 | idx = CONSTRUCTOR_NELTS (ctor); | |
3647 | goto insert; | |
3648 | } | |
3649 | ||
3650 | /* Otherwise, we need to iterate over CTOR to find or insert INDEX | |
3651 | appropriately. */ | |
3652 | ||
3653 | for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep); | |
3654 | idx++, fields = DECL_CHAIN (fields)) | |
3655 | { | |
3656 | if (index == cep->index) | |
3657 | goto found; | |
3658 | ||
3659 | /* The field we're initializing must be on the field | |
3660 | list. Look to see if it is present before the | |
3661 | field the current ELT initializes. */ | |
3662 | for (; fields != cep->index; fields = DECL_CHAIN (fields)) | |
3663 | if (index == fields) | |
3664 | goto insert; | |
3665 | } | |
3666 | /* We fell off the end of the CONSTRUCTOR, so insert a new | |
3667 | entry at the end. */ | |
3668 | ||
3669 | insert: | |
3670 | { | |
3671 | constructor_elt ce = { index, NULL_TREE }; | |
3672 | ||
3673 | vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce); | |
3674 | cep = CONSTRUCTOR_ELT (ctor, idx); | |
3675 | } | |
3676 | found:; | |
3677 | ||
3678 | return cep; | |
3679 | } | |
3680 | } | |
3681 | ||
abdc16c8 MS |
3682 | /* Under the control of CTX, issue a detailed diagnostic for |
3683 | an out-of-bounds subscript INDEX into the expression ARRAY. */ | |
3684 | ||
3685 | static void | |
043666e0 | 3686 | diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index) |
abdc16c8 MS |
3687 | { |
3688 | if (!ctx->quiet) | |
3689 | { | |
3690 | tree arraytype = TREE_TYPE (array); | |
3691 | ||
3692 | /* Convert the unsigned array subscript to a signed integer to avoid | |
3693 | printing huge numbers for small negative values. */ | |
3694 | tree sidx = fold_convert (ssizetype, index); | |
043666e0 | 3695 | STRIP_ANY_LOCATION_WRAPPER (array); |
abdc16c8 MS |
3696 | if (DECL_P (array)) |
3697 | { | |
08b3748c | 3698 | if (TYPE_DOMAIN (arraytype)) |
043666e0 JM |
3699 | error_at (loc, "array subscript value %qE is outside the bounds " |
3700 | "of array %qD of type %qT", sidx, array, arraytype); | |
08b3748c | 3701 | else |
043666e0 JM |
3702 | error_at (loc, "nonzero array subscript %qE is used with array %qD of " |
3703 | "type %qT with unknown bounds", sidx, array, arraytype); | |
abdc16c8 MS |
3704 | inform (DECL_SOURCE_LOCATION (array), "declared here"); |
3705 | } | |
08b3748c | 3706 | else if (TYPE_DOMAIN (arraytype)) |
043666e0 JM |
3707 | error_at (loc, "array subscript value %qE is outside the bounds " |
3708 | "of array type %qT", sidx, arraytype); | |
08b3748c | 3709 | else |
043666e0 JM |
3710 | error_at (loc, "nonzero array subscript %qE is used with array of type %qT " |
3711 | "with unknown bounds", sidx, arraytype); | |
abdc16c8 MS |
3712 | } |
3713 | } | |
ceaaf873 | 3714 | |
74f8705e MP |
3715 | /* Return the number of elements for TYPE (which is an ARRAY_TYPE or |
3716 | a VECTOR_TYPE). */ | |
3717 | ||
3718 | static tree | |
3719 | get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type, | |
3720 | bool *non_constant_p, bool *overflow_p) | |
3721 | { | |
3722 | tree nelts; | |
3723 | if (TREE_CODE (type) == ARRAY_TYPE) | |
3724 | { | |
3725 | if (TYPE_DOMAIN (type)) | |
3726 | nelts = array_type_nelts_top (type); | |
3727 | else | |
3728 | nelts = size_zero_node; | |
3729 | } | |
3730 | else if (VECTOR_TYPE_P (type)) | |
3731 | nelts = size_int (TYPE_VECTOR_SUBPARTS (type)); | |
3732 | else | |
3733 | gcc_unreachable (); | |
3734 | ||
3735 | /* For VLAs, the number of elements won't be an integer constant. */ | |
3736 | nelts = cxx_eval_constant_expression (ctx, nelts, false, | |
3737 | non_constant_p, overflow_p); | |
3738 | return nelts; | |
3739 | } | |
3740 | ||
d6b46fca NS |
3741 | /* Extract element INDEX consisting of CHARS_PER_ELT chars from |
3742 | STRING_CST STRING. */ | |
3743 | ||
3744 | static tree | |
3745 | extract_string_elt (tree string, unsigned chars_per_elt, unsigned index) | |
3746 | { | |
3747 | tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string))); | |
3748 | tree r; | |
3749 | ||
3750 | if (chars_per_elt == 1) | |
3751 | r = build_int_cst (type, TREE_STRING_POINTER (string)[index]); | |
3752 | else | |
3753 | { | |
3754 | const unsigned char *ptr | |
3755 | = ((const unsigned char *)TREE_STRING_POINTER (string) | |
3756 | + index * chars_per_elt); | |
3757 | r = native_interpret_expr (type, ptr, chars_per_elt); | |
3758 | } | |
3759 | return r; | |
3760 | } | |
3761 | ||
043666e0 JM |
3762 | /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the |
3763 | subscript, diagnose any problems with it, and return the result. */ | |
3764 | ||
3765 | static tree | |
3766 | eval_and_check_array_index (const constexpr_ctx *ctx, | |
3767 | tree t, bool allow_one_past, | |
3768 | bool *non_constant_p, bool *overflow_p) | |
3769 | { | |
f9d0ca40 | 3770 | location_t loc = cp_expr_loc_or_input_loc (t); |
043666e0 JM |
3771 | tree ary = TREE_OPERAND (t, 0); |
3772 | t = TREE_OPERAND (t, 1); | |
3773 | tree index = cxx_eval_constant_expression (ctx, t, false, | |
3774 | non_constant_p, overflow_p); | |
3775 | VERIFY_CONSTANT (index); | |
3776 | ||
3777 | if (!tree_fits_shwi_p (index) | |
3778 | || tree_int_cst_sgn (index) < 0) | |
3779 | { | |
3780 | diag_array_subscript (loc, ctx, ary, index); | |
3781 | *non_constant_p = true; | |
3782 | return t; | |
3783 | } | |
3784 | ||
3785 | tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p, | |
3786 | overflow_p); | |
3787 | VERIFY_CONSTANT (nelts); | |
3788 | if (allow_one_past | |
3789 | ? !tree_int_cst_le (index, nelts) | |
3790 | : !tree_int_cst_lt (index, nelts)) | |
3791 | { | |
3792 | diag_array_subscript (loc, ctx, ary, index); | |
3793 | *non_constant_p = true; | |
3794 | return t; | |
3795 | } | |
3796 | ||
3797 | return index; | |
3798 | } | |
3799 | ||
2d76680f PC |
3800 | /* Subroutine of cxx_eval_constant_expression. |
3801 | Attempt to reduce a reference to an array slot. */ | |
3802 | ||
3803 | static tree | |
3e605b20 | 3804 | cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, |
92a596e8 | 3805 | bool lval, |
2d76680f PC |
3806 | bool *non_constant_p, bool *overflow_p) |
3807 | { | |
3808 | tree oldary = TREE_OPERAND (t, 0); | |
3e605b20 | 3809 | tree ary = cxx_eval_constant_expression (ctx, oldary, |
92a596e8 | 3810 | lval, |
5a804683 | 3811 | non_constant_p, overflow_p); |
2d76680f PC |
3812 | if (*non_constant_p) |
3813 | return t; | |
bc2687dd JJ |
3814 | if (!lval |
3815 | && TREE_CODE (ary) == VIEW_CONVERT_EXPR | |
043666e0 JM |
3816 | && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0))) |
3817 | && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0)))) | |
3818 | ary = TREE_OPERAND (ary, 0); | |
3b9997bb | 3819 | |
043666e0 JM |
3820 | tree oldidx = TREE_OPERAND (t, 1); |
3821 | tree index = eval_and_check_array_index (ctx, t, lval, | |
3822 | non_constant_p, overflow_p); | |
3823 | if (*non_constant_p) | |
3824 | return t; | |
5453bfed | 3825 | |
bc2a38df JJ |
3826 | if (lval && ary == oldary && index == oldidx) |
3827 | return t; | |
3828 | else if (lval) | |
3829 | return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL); | |
3830 | ||
043666e0 JM |
3831 | unsigned len = 0, elem_nchars = 1; |
3832 | tree elem_type = TREE_TYPE (TREE_TYPE (ary)); | |
3833 | if (TREE_CODE (ary) == CONSTRUCTOR) | |
3834 | len = CONSTRUCTOR_NELTS (ary); | |
3835 | else if (TREE_CODE (ary) == STRING_CST) | |
3836 | { | |
3837 | elem_nchars = (TYPE_PRECISION (elem_type) | |
3838 | / TYPE_PRECISION (char_type_node)); | |
3839 | len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars; | |
3840 | } | |
3841 | else if (TREE_CODE (ary) == VECTOR_CST) | |
3842 | /* We don't create variable-length VECTOR_CSTs. */ | |
3843 | len = VECTOR_CST_NELTS (ary).to_constant (); | |
3844 | else | |
3845 | { | |
3846 | /* We can't do anything with other tree codes, so use | |
3847 | VERIFY_CONSTANT to complain and fail. */ | |
3848 | VERIFY_CONSTANT (ary); | |
3849 | gcc_unreachable (); | |
3850 | } | |
3851 | ||
ceaaf873 | 3852 | bool found; |
043666e0 | 3853 | HOST_WIDE_INT i = 0; |
ceaaf873 JM |
3854 | if (TREE_CODE (ary) == CONSTRUCTOR) |
3855 | { | |
3856 | HOST_WIDE_INT ix = find_array_ctor_elt (ary, index); | |
3857 | found = (ix >= 0); | |
3858 | if (found) | |
3859 | i = ix; | |
3b9997bb | 3860 | } |
ceaaf873 | 3861 | else |
043666e0 JM |
3862 | { |
3863 | i = tree_to_shwi (index); | |
3864 | found = (i < len); | |
3865 | } | |
3b9997bb | 3866 | |
fd2bfee5 JM |
3867 | if (found) |
3868 | { | |
3869 | tree r; | |
3870 | if (TREE_CODE (ary) == CONSTRUCTOR) | |
3871 | r = (*CONSTRUCTOR_ELTS (ary))[i].value; | |
3872 | else if (TREE_CODE (ary) == VECTOR_CST) | |
3873 | r = VECTOR_CST_ELT (ary, i); | |
fd2bfee5 | 3874 | else |
d6b46fca NS |
3875 | r = extract_string_elt (ary, elem_nchars, i); |
3876 | ||
fd2bfee5 JM |
3877 | if (r) |
3878 | /* Don't VERIFY_CONSTANT here. */ | |
3879 | return r; | |
2d76680f | 3880 | |
fd2bfee5 | 3881 | /* Otherwise the element doesn't have a value yet. */ |
2d76680f | 3882 | } |
3b9997bb | 3883 | |
fd2bfee5 JM |
3884 | /* Not found. */ |
3885 | ||
3886 | if (TREE_CODE (ary) == CONSTRUCTOR | |
e8c48716 | 3887 | && CONSTRUCTOR_NO_CLEARING (ary)) |
2d76680f | 3888 | { |
fd2bfee5 JM |
3889 | /* 'ary' is part of the aggregate initializer we're currently |
3890 | building; if there's no initializer for this element yet, | |
3891 | that's an error. */ | |
3892 | if (!ctx->quiet) | |
3893 | error ("accessing uninitialized array element"); | |
3894 | *non_constant_p = true; | |
3895 | return t; | |
2d76680f | 3896 | } |
fd2bfee5 JM |
3897 | |
3898 | /* If it's within the array bounds but doesn't have an explicit | |
0712ea63 JM |
3899 | initializer, it's initialized from {}. But use build_value_init |
3900 | directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */ | |
3901 | tree val; | |
0df73bee | 3902 | constexpr_ctx new_ctx; |
a688c284 PP |
3903 | if (is_really_empty_class (elem_type, /*ignore_vptr*/false)) |
3904 | return build_constructor (elem_type, NULL); | |
3905 | else if (CP_AGGREGATE_TYPE_P (elem_type)) | |
0712ea63 JM |
3906 | { |
3907 | tree empty_ctor = build_constructor (init_list_type_node, NULL); | |
3908 | val = digest_init (elem_type, empty_ctor, tf_warning_or_error); | |
a42f8120 MP |
3909 | } |
3910 | else | |
3911 | val = build_value_init (elem_type, tf_warning_or_error); | |
3912 | ||
3913 | if (!SCALAR_TYPE_P (elem_type)) | |
3914 | { | |
0df73bee | 3915 | new_ctx = *ctx; |
a42f8120 MP |
3916 | if (ctx->object) |
3917 | /* If there was no object, don't add one: it could confuse us | |
3918 | into thinking we're modifying a const object. */ | |
3919 | new_ctx.object = t; | |
0df73bee MP |
3920 | new_ctx.ctor = build_constructor (elem_type, NULL); |
3921 | ctx = &new_ctx; | |
0712ea63 | 3922 | } |
0df73bee MP |
3923 | t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p, |
3924 | overflow_p); | |
a42f8120 | 3925 | if (!SCALAR_TYPE_P (elem_type) && t != ctx->ctor) |
0df73bee MP |
3926 | free_constructor (ctx->ctor); |
3927 | return t; | |
2d76680f PC |
3928 | } |
3929 | ||
3930 | /* Subroutine of cxx_eval_constant_expression. | |
3931 | Attempt to reduce a field access of a value of class type. */ | |
3932 | ||
3933 | static tree | |
3e605b20 | 3934 | cxx_eval_component_reference (const constexpr_ctx *ctx, tree t, |
92a596e8 | 3935 | bool lval, |
2d76680f PC |
3936 | bool *non_constant_p, bool *overflow_p) |
3937 | { | |
3938 | unsigned HOST_WIDE_INT i; | |
3939 | tree field; | |
3940 | tree value; | |
3941 | tree part = TREE_OPERAND (t, 1); | |
3942 | tree orig_whole = TREE_OPERAND (t, 0); | |
3e605b20 | 3943 | tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
92a596e8 | 3944 | lval, |
5a804683 | 3945 | non_constant_p, overflow_p); |
a7f8415c | 3946 | if (INDIRECT_REF_P (whole) |
bf533db8 JJ |
3947 | && integer_zerop (TREE_OPERAND (whole, 0))) |
3948 | { | |
3949 | if (!ctx->quiet) | |
3950 | error ("dereferencing a null pointer in %qE", orig_whole); | |
3951 | *non_constant_p = true; | |
3952 | return t; | |
3953 | } | |
8bada5cd | 3954 | |
dcdbc004 JM |
3955 | if (TREE_CODE (whole) == PTRMEM_CST) |
3956 | whole = cplus_expand_constant (whole); | |
2d76680f PC |
3957 | if (whole == orig_whole) |
3958 | return t; | |
92a596e8 | 3959 | if (lval) |
2d76680f PC |
3960 | return fold_build3 (COMPONENT_REF, TREE_TYPE (t), |
3961 | whole, part, NULL_TREE); | |
3962 | /* Don't VERIFY_CONSTANT here; we only want to check that we got a | |
3963 | CONSTRUCTOR. */ | |
3964 | if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR) | |
3965 | { | |
2b3ab879 | 3966 | if (!ctx->quiet) |
2d76680f PC |
3967 | error ("%qE is not a constant expression", orig_whole); |
3968 | *non_constant_p = true; | |
3969 | } | |
3970 | if (DECL_MUTABLE_P (part)) | |
3971 | { | |
2b3ab879 | 3972 | if (!ctx->quiet) |
2d76680f PC |
3973 | error ("mutable %qD is not usable in a constant expression", part); |
3974 | *non_constant_p = true; | |
3975 | } | |
3976 | if (*non_constant_p) | |
3977 | return t; | |
2db613e5 | 3978 | bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole)); |
2d76680f PC |
3979 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) |
3980 | { | |
2db613e5 JM |
3981 | /* Use name match for PMF fields, as a variant will have a |
3982 | different FIELD_DECL with a different type. */ | |
3983 | if (pmf ? DECL_NAME (field) == DECL_NAME (part) | |
3984 | : field == part) | |
60813a46 JM |
3985 | { |
3986 | if (value) | |
5b884e94 JJ |
3987 | { |
3988 | STRIP_ANY_LOCATION_WRAPPER (value); | |
3989 | return value; | |
3990 | } | |
60813a46 JM |
3991 | else |
3992 | /* We're in the middle of initializing it. */ | |
3993 | break; | |
3994 | } | |
2d76680f PC |
3995 | } |
3996 | if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE | |
3997 | && CONSTRUCTOR_NELTS (whole) > 0) | |
3998 | { | |
3999 | /* DR 1188 says we don't have to deal with this. */ | |
2b3ab879 | 4000 | if (!ctx->quiet) |
b599bf9d PP |
4001 | { |
4002 | constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0); | |
4003 | if (cep->value == NULL_TREE) | |
4004 | error ("accessing uninitialized member %qD", part); | |
4005 | else | |
4006 | error ("accessing %qD member instead of initialized %qD member in " | |
4007 | "constant expression", part, cep->index); | |
4008 | } | |
2d76680f PC |
4009 | *non_constant_p = true; |
4010 | return t; | |
4011 | } | |
4012 | ||
188e53bd JM |
4013 | /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty |
4014 | classes never get represented; throw together a value now. */ | |
dbcd32f8 | 4015 | if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
188e53bd JM |
4016 | return build_constructor (TREE_TYPE (t), NULL); |
4017 | ||
2db613e5 JM |
4018 | gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole))); |
4019 | ||
e8c48716 | 4020 | if (CONSTRUCTOR_NO_CLEARING (whole)) |
3e605b20 JM |
4021 | { |
4022 | /* 'whole' is part of the aggregate initializer we're currently | |
4023 | building; if there's no initializer for this member yet, that's an | |
188e53bd | 4024 | error. */ |
2b3ab879 | 4025 | if (!ctx->quiet) |
3e605b20 JM |
4026 | error ("accessing uninitialized member %qD", part); |
4027 | *non_constant_p = true; | |
4028 | return t; | |
4029 | } | |
4030 | ||
2d76680f PC |
4031 | /* If there's no explicit init for this field, it's value-initialized. */ |
4032 | value = build_value_init (TREE_TYPE (t), tf_warning_or_error); | |
3e605b20 | 4033 | return cxx_eval_constant_expression (ctx, value, |
92a596e8 | 4034 | lval, |
5a804683 | 4035 | non_constant_p, overflow_p); |
2d76680f PC |
4036 | } |
4037 | ||
4038 | /* Subroutine of cxx_eval_constant_expression. | |
4039 | Attempt to reduce a field access of a value of class type that is | |
4040 | expressed as a BIT_FIELD_REF. */ | |
4041 | ||
4042 | static tree | |
3e605b20 | 4043 | cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t, |
92a596e8 | 4044 | bool lval, |
2d76680f PC |
4045 | bool *non_constant_p, bool *overflow_p) |
4046 | { | |
4047 | tree orig_whole = TREE_OPERAND (t, 0); | |
4048 | tree retval, fldval, utype, mask; | |
4049 | bool fld_seen = false; | |
4050 | HOST_WIDE_INT istart, isize; | |
3e605b20 | 4051 | tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
92a596e8 | 4052 | lval, |
5a804683 | 4053 | non_constant_p, overflow_p); |
2d76680f PC |
4054 | tree start, field, value; |
4055 | unsigned HOST_WIDE_INT i; | |
4056 | ||
4057 | if (whole == orig_whole) | |
4058 | return t; | |
4059 | /* Don't VERIFY_CONSTANT here; we only want to check that we got a | |
4060 | CONSTRUCTOR. */ | |
4061 | if (!*non_constant_p | |
4062 | && TREE_CODE (whole) != VECTOR_CST | |
4063 | && TREE_CODE (whole) != CONSTRUCTOR) | |
4064 | { | |
2b3ab879 | 4065 | if (!ctx->quiet) |
2d76680f PC |
4066 | error ("%qE is not a constant expression", orig_whole); |
4067 | *non_constant_p = true; | |
4068 | } | |
4069 | if (*non_constant_p) | |
4070 | return t; | |
4071 | ||
4072 | if (TREE_CODE (whole) == VECTOR_CST) | |
4073 | return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole, | |
4074 | TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)); | |
4075 | ||
4076 | start = TREE_OPERAND (t, 2); | |
4077 | istart = tree_to_shwi (start); | |
4078 | isize = tree_to_shwi (TREE_OPERAND (t, 1)); | |
4079 | utype = TREE_TYPE (t); | |
4080 | if (!TYPE_UNSIGNED (utype)) | |
4081 | utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1); | |
4082 | retval = build_int_cst (utype, 0); | |
4083 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) | |
4084 | { | |
4085 | tree bitpos = bit_position (field); | |
5b884e94 | 4086 | STRIP_ANY_LOCATION_WRAPPER (value); |
2d76680f PC |
4087 | if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1)) |
4088 | return value; | |
4089 | if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE | |
4090 | && TREE_CODE (value) == INTEGER_CST | |
4091 | && tree_fits_shwi_p (bitpos) | |
4092 | && tree_fits_shwi_p (DECL_SIZE (field))) | |
4093 | { | |
4094 | HOST_WIDE_INT bit = tree_to_shwi (bitpos); | |
4095 | HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field)); | |
4096 | HOST_WIDE_INT shift; | |
4097 | if (bit >= istart && bit + sz <= istart + isize) | |
4098 | { | |
4099 | fldval = fold_convert (utype, value); | |
4100 | mask = build_int_cst_type (utype, -1); | |
4101 | mask = fold_build2 (LSHIFT_EXPR, utype, mask, | |
4102 | size_int (TYPE_PRECISION (utype) - sz)); | |
4103 | mask = fold_build2 (RSHIFT_EXPR, utype, mask, | |
4104 | size_int (TYPE_PRECISION (utype) - sz)); | |
4105 | fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask); | |
4106 | shift = bit - istart; | |
4107 | if (BYTES_BIG_ENDIAN) | |
4108 | shift = TYPE_PRECISION (utype) - shift - sz; | |
4109 | fldval = fold_build2 (LSHIFT_EXPR, utype, fldval, | |
4110 | size_int (shift)); | |
4111 | retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval); | |
4112 | fld_seen = true; | |
4113 | } | |
4114 | } | |
4115 | } | |
4116 | if (fld_seen) | |
4117 | return fold_convert (TREE_TYPE (t), retval); | |
4118 | gcc_unreachable (); | |
4119 | return error_mark_node; | |
4120 | } | |
4121 | ||
896048cf JJ |
4122 | /* Helper for cxx_eval_bit_cast. |
4123 | Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From | |
4124 | types and types of all subobjects have is_union_v<T>, is_pointer_v<T>, | |
4125 | is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static | |
4126 | data members of reference type. */ | |
4127 | ||
4128 | static bool | |
4129 | check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type, | |
4130 | tree orig_type) | |
4131 | { | |
4132 | if (TREE_CODE (type) == UNION_TYPE) | |
4133 | { | |
4134 | if (!ctx->quiet) | |
4135 | { | |
4136 | if (type == orig_type) | |
4137 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4138 | "a union type", "__builtin_bit_cast", type); | |
4139 | else | |
4140 | error_at (loc, "%qs is not a constant expression because %qT " | |
4141 | "contains a union type", "__builtin_bit_cast", | |
4142 | orig_type); | |
4143 | } | |
4144 | return true; | |
4145 | } | |
4146 | if (TREE_CODE (type) == POINTER_TYPE) | |
4147 | { | |
4148 | if (!ctx->quiet) | |
4149 | { | |
4150 | if (type == orig_type) | |
4151 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4152 | "a pointer type", "__builtin_bit_cast", type); | |
4153 | else | |
4154 | error_at (loc, "%qs is not a constant expression because %qT " | |
4155 | "contains a pointer type", "__builtin_bit_cast", | |
4156 | orig_type); | |
4157 | } | |
4158 | return true; | |
4159 | } | |
4160 | if (TREE_CODE (type) == REFERENCE_TYPE) | |
4161 | { | |
4162 | if (!ctx->quiet) | |
4163 | { | |
4164 | if (type == orig_type) | |
4165 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4166 | "a reference type", "__builtin_bit_cast", type); | |
4167 | else | |
4168 | error_at (loc, "%qs is not a constant expression because %qT " | |
4169 | "contains a reference type", "__builtin_bit_cast", | |
4170 | orig_type); | |
4171 | } | |
4172 | return true; | |
4173 | } | |
4174 | if (TYPE_PTRMEM_P (type)) | |
4175 | { | |
4176 | if (!ctx->quiet) | |
4177 | { | |
4178 | if (type == orig_type) | |
4179 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4180 | "a pointer to member type", "__builtin_bit_cast", | |
4181 | type); | |
4182 | else | |
4183 | error_at (loc, "%qs is not a constant expression because %qT " | |
4184 | "contains a pointer to member type", | |
4185 | "__builtin_bit_cast", orig_type); | |
4186 | } | |
4187 | return true; | |
4188 | } | |
4189 | if (TYPE_VOLATILE (type)) | |
4190 | { | |
4191 | if (!ctx->quiet) | |
4192 | { | |
4193 | if (type == orig_type) | |
4194 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4195 | "volatile", "__builtin_bit_cast", type); | |
4196 | else | |
4197 | error_at (loc, "%qs is not a constant expression because %qT " | |
4198 | "contains a volatile subobject", | |
4199 | "__builtin_bit_cast", orig_type); | |
4200 | } | |
4201 | return true; | |
4202 | } | |
4203 | if (TREE_CODE (type) == RECORD_TYPE) | |
4204 | for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) | |
4205 | if (TREE_CODE (field) == FIELD_DECL | |
4206 | && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type)) | |
4207 | return true; | |
4208 | return false; | |
4209 | } | |
4210 | ||
4211 | /* Subroutine of cxx_eval_constant_expression. | |
4212 | Attempt to evaluate a BIT_CAST_EXPR. */ | |
4213 | ||
4214 | static tree | |
4215 | cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p, | |
4216 | bool *overflow_p) | |
4217 | { | |
4218 | if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t), | |
4219 | TREE_TYPE (t)) | |
4220 | || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0), | |
4221 | EXPR_LOCATION (t)), | |
4222 | TREE_TYPE (TREE_OPERAND (t, 0)), | |
4223 | TREE_TYPE (TREE_OPERAND (t, 0)))) | |
4224 | { | |
4225 | *non_constant_p = true; | |
4226 | return t; | |
4227 | } | |
4228 | ||
4229 | tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false, | |
4230 | non_constant_p, overflow_p); | |
4231 | if (*non_constant_p) | |
4232 | return t; | |
4233 | ||
4234 | location_t loc = EXPR_LOCATION (t); | |
4235 | if (BITS_PER_UNIT != 8 || CHAR_BIT != 8) | |
4236 | { | |
4237 | if (!ctx->quiet) | |
4238 | sorry_at (loc, "%qs cannot be constant evaluated on the target", | |
4239 | "__builtin_bit_cast"); | |
4240 | *non_constant_p = true; | |
4241 | return t; | |
4242 | } | |
4243 | ||
4244 | if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t)))) | |
4245 | { | |
4246 | if (!ctx->quiet) | |
4247 | sorry_at (loc, "%qs cannot be constant evaluated because the " | |
4248 | "type is too large", "__builtin_bit_cast"); | |
4249 | *non_constant_p = true; | |
4250 | return t; | |
4251 | } | |
4252 | ||
4253 | HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t))); | |
4254 | if (len < 0 || (int) len != len) | |
4255 | { | |
4256 | if (!ctx->quiet) | |
4257 | sorry_at (loc, "%qs cannot be constant evaluated because the " | |
4258 | "type is too large", "__builtin_bit_cast"); | |
4259 | *non_constant_p = true; | |
4260 | return t; | |
4261 | } | |
4262 | ||
4263 | unsigned char buf[64]; | |
4264 | unsigned char *ptr, *mask; | |
4265 | size_t alen = (size_t) len * 2; | |
4266 | if (alen <= sizeof (buf)) | |
4267 | ptr = buf; | |
4268 | else | |
4269 | ptr = XNEWVEC (unsigned char, alen); | |
4270 | mask = ptr + (size_t) len; | |
4271 | /* At the beginning consider everything indeterminate. */ | |
4272 | memset (mask, ~0, (size_t) len); | |
4273 | ||
4274 | if (native_encode_initializer (op, ptr, len, 0, mask) != len) | |
4275 | { | |
4276 | if (!ctx->quiet) | |
4277 | sorry_at (loc, "%qs cannot be constant evaluated because the " | |
4278 | "argument cannot be encoded", "__builtin_bit_cast"); | |
4279 | *non_constant_p = true; | |
4280 | if (ptr != buf) | |
4281 | XDELETE (ptr); | |
4282 | return t; | |
4283 | } | |
4284 | ||
4285 | tree r = NULL_TREE; | |
4286 | if (can_native_interpret_type_p (TREE_TYPE (t))) | |
4287 | r = native_interpret_expr (TREE_TYPE (t), ptr, len); | |
4288 | else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE) | |
4289 | { | |
4290 | r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len); | |
4291 | if (r != NULL_TREE) | |
4292 | clear_type_padding_in_mask (TREE_TYPE (t), mask); | |
4293 | } | |
4294 | ||
4295 | if (r != NULL_TREE) | |
4296 | { | |
4297 | for (int i = 0; i < len; i++) | |
4298 | if (mask[i]) | |
4299 | { | |
4300 | if (!ctx->quiet) | |
4301 | error_at (loc, "%qs accessing uninitialized byte at offset %d", | |
4302 | "__builtin_bit_cast", i); | |
4303 | *non_constant_p = true; | |
4304 | r = t; | |
4305 | break; | |
4306 | } | |
4307 | if (ptr != buf) | |
4308 | XDELETE (ptr); | |
4309 | return r; | |
4310 | } | |
4311 | ||
4312 | if (!ctx->quiet) | |
4313 | sorry_at (loc, "%qs cannot be constant evaluated because the " | |
4314 | "argument cannot be interpreted", "__builtin_bit_cast"); | |
4315 | *non_constant_p = true; | |
4316 | if (ptr != buf) | |
4317 | XDELETE (ptr); | |
4318 | return t; | |
4319 | } | |
4320 | ||
2d76680f PC |
4321 | /* Subroutine of cxx_eval_constant_expression. |
4322 | Evaluate a short-circuited logical expression T in the context | |
4323 | of a given constexpr CALL. BAILOUT_VALUE is the value for | |
4324 | early return. CONTINUE_VALUE is used here purely for | |
4325 | sanity check purposes. */ | |
4326 | ||
4327 | static tree | |
3e605b20 | 4328 | cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t, |
2d76680f | 4329 | tree bailout_value, tree continue_value, |
92a596e8 | 4330 | bool lval, |
2d76680f PC |
4331 | bool *non_constant_p, bool *overflow_p) |
4332 | { | |
4333 | tree r; | |
3e605b20 | 4334 | tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
92a596e8 | 4335 | lval, |
5a804683 | 4336 | non_constant_p, overflow_p); |
2d76680f PC |
4337 | VERIFY_CONSTANT (lhs); |
4338 | if (tree_int_cst_equal (lhs, bailout_value)) | |
4339 | return lhs; | |
4340 | gcc_assert (tree_int_cst_equal (lhs, continue_value)); | |
3e605b20 | 4341 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
92a596e8 | 4342 | lval, non_constant_p, |
5a804683 | 4343 | overflow_p); |
2d76680f PC |
4344 | VERIFY_CONSTANT (r); |
4345 | return r; | |
4346 | } | |
4347 | ||
4348 | /* REF is a COMPONENT_REF designating a particular field. V is a vector of | |
4349 | CONSTRUCTOR elements to initialize (part of) an object containing that | |
4350 | field. Return a pointer to the constructor_elt corresponding to the | |
4351 | initialization of the field. */ | |
4352 | ||
4353 | static constructor_elt * | |
4354 | base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref) | |
4355 | { | |
4356 | tree aggr = TREE_OPERAND (ref, 0); | |
4357 | tree field = TREE_OPERAND (ref, 1); | |
4358 | HOST_WIDE_INT i; | |
4359 | constructor_elt *ce; | |
4360 | ||
4361 | gcc_assert (TREE_CODE (ref) == COMPONENT_REF); | |
4362 | ||
4363 | if (TREE_CODE (aggr) == COMPONENT_REF) | |
4364 | { | |
4365 | constructor_elt *base_ce | |
4366 | = base_field_constructor_elt (v, aggr); | |
4367 | v = CONSTRUCTOR_ELTS (base_ce->value); | |
4368 | } | |
4369 | ||
4370 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
4371 | if (ce->index == field) | |
4372 | return ce; | |
4373 | ||
4374 | gcc_unreachable (); | |
4375 | return NULL; | |
4376 | } | |
4377 | ||
3e605b20 JM |
4378 | /* Some of the expressions fed to the constexpr mechanism are calls to |
4379 | constructors, which have type void. In that case, return the type being | |
4380 | initialized by the constructor. */ | |
4381 | ||
4382 | static tree | |
4383 | initialized_type (tree t) | |
4384 | { | |
4385 | if (TYPE_P (t)) | |
4386 | return t; | |
5dab8b11 | 4387 | tree type = TREE_TYPE (t); |
4d0c18c6 | 4388 | if (TREE_CODE (t) == CALL_EXPR) |
3e605b20 JM |
4389 | { |
4390 | /* A constructor call has void type, so we need to look deeper. */ | |
4391 | tree fn = get_function_named_in_call (t); | |
4392 | if (fn && TREE_CODE (fn) == FUNCTION_DECL | |
4393 | && DECL_CXX_CONSTRUCTOR_P (fn)) | |
4394 | type = DECL_CONTEXT (fn); | |
4395 | } | |
4d0c18c6 JM |
4396 | else if (TREE_CODE (t) == COMPOUND_EXPR) |
4397 | return initialized_type (TREE_OPERAND (t, 1)); | |
5dab8b11 JM |
4398 | else if (TREE_CODE (t) == AGGR_INIT_EXPR) |
4399 | type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t)); | |
4400 | return cv_unqualified (type); | |
3e605b20 JM |
4401 | } |
4402 | ||
4403 | /* We're about to initialize element INDEX of an array or class from VALUE. | |
4404 | Set up NEW_CTX appropriately by adjusting .object to refer to the | |
4405 | subobject and creating a new CONSTRUCTOR if the element is itself | |
4406 | a class or array. */ | |
4407 | ||
4408 | static void | |
4409 | init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx, | |
4410 | tree index, tree &value) | |
4411 | { | |
4412 | new_ctx = *ctx; | |
4413 | ||
4414 | if (index && TREE_CODE (index) != INTEGER_CST | |
3d423c6f PP |
4415 | && TREE_CODE (index) != FIELD_DECL |
4416 | && TREE_CODE (index) != RANGE_EXPR) | |
3e605b20 JM |
4417 | /* This won't have an element in the new CONSTRUCTOR. */ |
4418 | return; | |
4419 | ||
4420 | tree type = initialized_type (value); | |
4421 | if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type)) | |
4422 | /* A non-aggregate member doesn't get its own CONSTRUCTOR. */ | |
4423 | return; | |
4424 | ||
4425 | /* The sub-aggregate initializer might contain a placeholder; | |
4426 | update object to refer to the subobject and ctor to refer to | |
4427 | the (newly created) sub-initializer. */ | |
4428 | if (ctx->object) | |
3d423c6f PP |
4429 | { |
4430 | if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR) | |
4431 | /* There's no well-defined subobject for this index. */ | |
4432 | new_ctx.object = NULL_TREE; | |
4433 | else | |
4434 | new_ctx.object = build_ctor_subob_ref (index, type, ctx->object); | |
4435 | } | |
3e605b20 | 4436 | tree elt = build_constructor (type, NULL); |
e8c48716 | 4437 | CONSTRUCTOR_NO_CLEARING (elt) = true; |
3e605b20 JM |
4438 | new_ctx.ctor = elt; |
4439 | ||
4440 | if (TREE_CODE (value) == TARGET_EXPR) | |
4441 | /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */ | |
4442 | value = TARGET_EXPR_INITIAL (value); | |
4443 | } | |
4444 | ||
4445 | /* We're about to process an initializer for a class or array TYPE. Make | |
4446 | sure that CTX is set up appropriately. */ | |
4447 | ||
4448 | static void | |
4449 | verify_ctor_sanity (const constexpr_ctx *ctx, tree type) | |
4450 | { | |
4451 | /* We don't bother building a ctor for an empty base subobject. */ | |
4452 | if (is_empty_class (type)) | |
4453 | return; | |
4454 | ||
4455 | /* We're in the middle of an initializer that might involve placeholders; | |
4456 | our caller should have created a CONSTRUCTOR for us to put the | |
4457 | initializer into. We will either return that constructor or T. */ | |
4458 | gcc_assert (ctx->ctor); | |
4459 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
4460 | (type, TREE_TYPE (ctx->ctor))); | |
fe69277d JM |
4461 | /* We used to check that ctx->ctor was empty, but that isn't the case when |
4462 | the object is zero-initialized before calling the constructor. */ | |
3e605b20 | 4463 | if (ctx->object) |
176e79b5 JM |
4464 | { |
4465 | tree otype = TREE_TYPE (ctx->object); | |
4466 | gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype) | |
4467 | /* Handle flexible array members. */ | |
4468 | || (TREE_CODE (otype) == ARRAY_TYPE | |
4469 | && TYPE_DOMAIN (otype) == NULL_TREE | |
4470 | && TREE_CODE (type) == ARRAY_TYPE | |
4471 | && (same_type_ignoring_top_level_qualifiers_p | |
4472 | (TREE_TYPE (type), TREE_TYPE (otype))))); | |
4473 | } | |
3e605b20 | 4474 | gcc_assert (!ctx->object || !DECL_P (ctx->object) |
8e007055 | 4475 | || *(ctx->global->values.get (ctx->object)) == ctx->ctor); |
3e605b20 JM |
4476 | } |
4477 | ||
2d76680f PC |
4478 | /* Subroutine of cxx_eval_constant_expression. |
4479 | The expression tree T denotes a C-style array or a C-style | |
4480 | aggregate. Reduce it to a constant expression. */ | |
4481 | ||
4482 | static tree | |
3e605b20 | 4483 | cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t, |
92a596e8 | 4484 | bool lval, |
2d76680f PC |
4485 | bool *non_constant_p, bool *overflow_p) |
4486 | { | |
4487 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); | |
2d76680f PC |
4488 | bool changed = false; |
4489 | gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t)); | |
8a29084d | 4490 | tree type = TREE_TYPE (t); |
3e605b20 | 4491 | |
8a29084d | 4492 | constexpr_ctx new_ctx; |
d4619dc1 | 4493 | if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type)) |
8a29084d | 4494 | { |
d4619dc1 NS |
4495 | /* We don't really need the ctx->ctor business for a PMF or |
4496 | vector, but it's simpler to use the same code. */ | |
8a29084d JM |
4497 | new_ctx = *ctx; |
4498 | new_ctx.ctor = build_constructor (type, NULL); | |
4499 | new_ctx.object = NULL_TREE; | |
4500 | ctx = &new_ctx; | |
4501 | }; | |
4502 | verify_ctor_sanity (ctx, type); | |
3e605b20 JM |
4503 | vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); |
4504 | vec_alloc (*p, vec_safe_length (v)); | |
4505 | ||
49a86fce PP |
4506 | if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t)) |
4507 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1; | |
4508 | ||
2d63bc39 JM |
4509 | unsigned i; |
4510 | tree index, value; | |
4511 | bool constant_p = true; | |
4512 | bool side_effects_p = false; | |
3e605b20 | 4513 | FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value) |
2d76680f | 4514 | { |
bcb5f3c9 | 4515 | tree orig_value = value; |
2168bfb8 JM |
4516 | /* Like in cxx_eval_store_expression, omit entries for empty fields. */ |
4517 | bool no_slot = TREE_CODE (type) == RECORD_TYPE && is_empty_field (index); | |
4518 | if (no_slot) | |
4519 | new_ctx = *ctx; | |
4520 | else | |
4521 | init_subob_ctx (ctx, new_ctx, index, value); | |
37244b21 | 4522 | int pos_hint = -1; |
3e605b20 | 4523 | if (new_ctx.ctor != ctx->ctor) |
37244b21 PP |
4524 | { |
4525 | /* If we built a new CONSTRUCTOR, attach it now so that other | |
4526 | initializers can refer to it. */ | |
077dd9b3 PP |
4527 | constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index); |
4528 | cep->value = new_ctx.ctor; | |
4529 | pos_hint = cep - (*p)->begin(); | |
37244b21 | 4530 | } |
b599bf9d | 4531 | else if (TREE_CODE (type) == UNION_TYPE) |
37244b21 PP |
4532 | /* Otherwise if we're constructing a non-aggregate union member, set |
4533 | the active union member now so that we can later detect and diagnose | |
4534 | if its initializer attempts to activate another member. */ | |
077dd9b3 | 4535 | get_or_insert_ctor_field (ctx->ctor, index); |
3e605b20 | 4536 | tree elt = cxx_eval_constant_expression (&new_ctx, value, |
92a596e8 | 4537 | lval, |
5a804683 | 4538 | non_constant_p, overflow_p); |
2d76680f | 4539 | /* Don't VERIFY_CONSTANT here. */ |
2b3ab879 | 4540 | if (ctx->quiet && *non_constant_p) |
3e605b20 | 4541 | break; |
bcb5f3c9 | 4542 | if (elt != orig_value) |
2d76680f | 4543 | changed = true; |
2d63bc39 JM |
4544 | |
4545 | if (!TREE_CONSTANT (elt)) | |
4546 | constant_p = false; | |
4547 | if (TREE_SIDE_EFFECTS (elt)) | |
4548 | side_effects_p = true; | |
3e605b20 | 4549 | if (index && TREE_CODE (index) == COMPONENT_REF) |
2d76680f PC |
4550 | { |
4551 | /* This is an initialization of a vfield inside a base | |
4552 | subaggregate that we already initialized; push this | |
4553 | initialization into the previous initialization. */ | |
3e605b20 | 4554 | constructor_elt *inner = base_field_constructor_elt (*p, index); |
2d76680f | 4555 | inner->value = elt; |
3e605b20 | 4556 | changed = true; |
2d76680f | 4557 | } |
3e605b20 JM |
4558 | else if (index |
4559 | && (TREE_CODE (index) == NOP_EXPR | |
4560 | || TREE_CODE (index) == POINTER_PLUS_EXPR)) | |
2d76680f PC |
4561 | { |
4562 | /* This is an initializer for an empty base; now that we've | |
4563 | checked that it's constant, we can ignore it. */ | |
3e605b20 JM |
4564 | gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index)))); |
4565 | changed = true; | |
4566 | } | |
2168bfb8 JM |
4567 | else if (no_slot) |
4568 | changed = true; | |
2d76680f | 4569 | else |
ac9ec198 | 4570 | { |
b599bf9d PP |
4571 | if (TREE_CODE (type) == UNION_TYPE |
4572 | && (*p)->last().index != index) | |
37244b21 PP |
4573 | /* The initializer erroneously changed the active union member that |
4574 | we're initializing. */ | |
b599bf9d | 4575 | gcc_assert (*non_constant_p); |
37244b21 | 4576 | else |
1bdbef09 | 4577 | { |
37244b21 PP |
4578 | /* The initializer might have mutated the underlying CONSTRUCTOR, |
4579 | so recompute the location of the target constructer_elt. */ | |
4580 | constructor_elt *cep | |
4581 | = get_or_insert_ctor_field (ctx->ctor, index, pos_hint); | |
4582 | cep->value = elt; | |
1bdbef09 | 4583 | } |
37244b21 | 4584 | |
1bdbef09 | 4585 | /* Adding or replacing an element might change the ctor's flags. */ |
ac9ec198 MP |
4586 | TREE_CONSTANT (ctx->ctor) = constant_p; |
4587 | TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p; | |
4588 | } | |
2d76680f PC |
4589 | } |
4590 | if (*non_constant_p || !changed) | |
3e605b20 JM |
4591 | return t; |
4592 | t = ctx->ctor; | |
4593 | /* We're done building this CONSTRUCTOR, so now we can interpret an | |
4594 | element without an explicit initializer as value-initialized. */ | |
e8c48716 | 4595 | CONSTRUCTOR_NO_CLEARING (t) = false; |
2d63bc39 JM |
4596 | TREE_CONSTANT (t) = constant_p; |
4597 | TREE_SIDE_EFFECTS (t) = side_effects_p; | |
8a29084d | 4598 | if (VECTOR_TYPE_P (type)) |
2d76680f PC |
4599 | t = fold (t); |
4600 | return t; | |
4601 | } | |
4602 | ||
4603 | /* Subroutine of cxx_eval_constant_expression. | |
4604 | The expression tree T is a VEC_INIT_EXPR which denotes the desired | |
4605 | initialization of a non-static data member of array type. Reduce it to a | |
4606 | CONSTRUCTOR. | |
4607 | ||
4608 | Note that apart from value-initialization (when VALUE_INIT is true), | |
4609 | this is only intended to support value-initialization and the | |
4610 | initializations done by defaulted constructors for classes with | |
4611 | non-static data members of array type. In this case, VEC_INIT_EXPR_INIT | |
4612 | will either be NULL_TREE for the default constructor, or a COMPONENT_REF | |
4613 | for the copy/move constructor. */ | |
4614 | ||
4615 | static tree | |
3e605b20 | 4616 | cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init, |
92a596e8 | 4617 | bool value_init, bool lval, |
2d76680f PC |
4618 | bool *non_constant_p, bool *overflow_p) |
4619 | { | |
4620 | tree elttype = TREE_TYPE (atype); | |
3e605b20 JM |
4621 | verify_ctor_sanity (ctx, atype); |
4622 | vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); | |
2d76680f | 4623 | bool pre_init = false; |
4784470a | 4624 | unsigned HOST_WIDE_INT i; |
dc5ca6c8 | 4625 | tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; |
2d76680f | 4626 | |
a1c9c9ff JM |
4627 | if (init && TREE_CODE (init) == CONSTRUCTOR) |
4628 | return cxx_eval_bare_aggregate (ctx, init, lval, | |
4629 | non_constant_p, overflow_p); | |
4630 | ||
2d76680f PC |
4631 | /* For the default constructor, build up a call to the default |
4632 | constructor of the element type. We only need to handle class types | |
4633 | here, as for a constructor to be constexpr, all members must be | |
4634 | initialized, which for a defaulted default constructor means they must | |
4635 | be of a class type with a constexpr default constructor. */ | |
4636 | if (TREE_CODE (elttype) == ARRAY_TYPE) | |
4637 | /* We only do this at the lowest level. */; | |
4638 | else if (value_init) | |
4639 | { | |
dc5ca6c8 | 4640 | init = build_value_init (elttype, complain); |
2d76680f PC |
4641 | pre_init = true; |
4642 | } | |
4643 | else if (!init) | |
4644 | { | |
cd9cf97b | 4645 | releasing_vec argvec; |
2d76680f PC |
4646 | init = build_special_member_call (NULL_TREE, complete_ctor_identifier, |
4647 | &argvec, elttype, LOOKUP_NORMAL, | |
dc5ca6c8 | 4648 | complain); |
5dab8b11 | 4649 | init = build_aggr_init_expr (elttype, init); |
2d76680f PC |
4650 | pre_init = true; |
4651 | } | |
4652 | ||
d21252de PP |
4653 | bool zeroed_out = false; |
4654 | if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor)) | |
4655 | { | |
4656 | /* We're initializing an array object that had been zero-initialized | |
4657 | earlier. Truncate ctx->ctor, and propagate its zeroed state by | |
4658 | clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element | |
4659 | initializers we append to it. */ | |
4660 | gcc_checking_assert (initializer_zerop (ctx->ctor)); | |
4661 | zeroed_out = true; | |
4662 | vec_safe_truncate (*p, 0); | |
4663 | } | |
4664 | ||
74f8705e MP |
4665 | tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p, |
4666 | overflow_p); | |
4667 | unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts); | |
4784470a | 4668 | for (i = 0; i < max; ++i) |
2d76680f PC |
4669 | { |
4670 | tree idx = build_int_cst (size_type_node, i); | |
4671 | tree eltinit; | |
928af3bf | 4672 | bool reuse = false; |
3e605b20 JM |
4673 | constexpr_ctx new_ctx; |
4674 | init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype); | |
4675 | if (new_ctx.ctor != ctx->ctor) | |
d21252de PP |
4676 | { |
4677 | if (zeroed_out) | |
4678 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false; | |
4679 | CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor); | |
4680 | } | |
2d76680f PC |
4681 | if (TREE_CODE (elttype) == ARRAY_TYPE) |
4682 | { | |
4683 | /* A multidimensional array; recurse. */ | |
4684 | if (value_init || init == NULL_TREE) | |
928af3bf JJ |
4685 | { |
4686 | eltinit = NULL_TREE; | |
4687 | reuse = i == 0; | |
4688 | } | |
2d76680f | 4689 | else |
dc5ca6c8 | 4690 | eltinit = cp_build_array_ref (input_location, init, idx, complain); |
3e605b20 | 4691 | eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init, |
92a596e8 | 4692 | lval, |
2d76680f PC |
4693 | non_constant_p, overflow_p); |
4694 | } | |
4695 | else if (pre_init) | |
4696 | { | |
4697 | /* Initializing an element using value or default initialization | |
4698 | we just pre-built above. */ | |
3ee378fb JM |
4699 | if (init == void_node) |
4700 | /* Trivial default-init, don't do anything to the CONSTRUCTOR. */ | |
4701 | return ctx->ctor; | |
928af3bf JJ |
4702 | eltinit = cxx_eval_constant_expression (&new_ctx, init, lval, |
4703 | non_constant_p, overflow_p); | |
4704 | reuse = i == 0; | |
2d76680f PC |
4705 | } |
4706 | else | |
4707 | { | |
4708 | /* Copying an element. */ | |
4709 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
4710 | (atype, TREE_TYPE (init))); | |
dc5ca6c8 | 4711 | eltinit = cp_build_array_ref (input_location, init, idx, complain); |
72b3e203 | 4712 | if (!lvalue_p (init)) |
2d76680f | 4713 | eltinit = move (eltinit); |
dc5ca6c8 | 4714 | eltinit = force_rvalue (eltinit, complain); |
c2236b9b JJ |
4715 | eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval, |
4716 | non_constant_p, overflow_p); | |
2d76680f | 4717 | } |
6b7d53a2 | 4718 | if (*non_constant_p) |
3e605b20 JM |
4719 | break; |
4720 | if (new_ctx.ctor != ctx->ctor) | |
4721 | { | |
4722 | /* We appended this element above; update the value. */ | |
4723 | gcc_assert ((*p)->last().index == idx); | |
4724 | (*p)->last().value = eltinit; | |
4725 | } | |
4726 | else | |
4727 | CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit); | |
928af3bf | 4728 | /* Reuse the result of cxx_eval_constant_expression call |
c2236b9b JJ |
4729 | from the first iteration to all others if it is a constant |
4730 | initializer that doesn't require relocations. */ | |
928af3bf JJ |
4731 | if (reuse |
4732 | && max > 1 | |
c2236b9b JJ |
4733 | && (eltinit == NULL_TREE |
4734 | || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit)) | |
4735 | == null_pointer_node))) | |
928af3bf JJ |
4736 | { |
4737 | if (new_ctx.ctor != ctx->ctor) | |
4738 | eltinit = new_ctx.ctor; | |
1e027956 RB |
4739 | tree range = build2 (RANGE_EXPR, size_type_node, |
4740 | build_int_cst (size_type_node, 1), | |
4741 | build_int_cst (size_type_node, max - 1)); | |
4742 | CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit)); | |
928af3bf JJ |
4743 | break; |
4744 | } | |
1e027956 RB |
4745 | else if (i == 0) |
4746 | vec_safe_reserve (*p, max); | |
2d76680f PC |
4747 | } |
4748 | ||
4749 | if (!*non_constant_p) | |
4750 | { | |
3e605b20 | 4751 | init = ctx->ctor; |
e8c48716 | 4752 | CONSTRUCTOR_NO_CLEARING (init) = false; |
2d76680f | 4753 | } |
2d76680f PC |
4754 | return init; |
4755 | } | |
4756 | ||
4757 | static tree | |
3e605b20 | 4758 | cxx_eval_vec_init (const constexpr_ctx *ctx, tree t, |
92a596e8 | 4759 | bool lval, |
2d76680f PC |
4760 | bool *non_constant_p, bool *overflow_p) |
4761 | { | |
4762 | tree atype = TREE_TYPE (t); | |
4763 | tree init = VEC_INIT_EXPR_INIT (t); | |
3e605b20 | 4764 | tree r = cxx_eval_vec_init_1 (ctx, atype, init, |
2d76680f | 4765 | VEC_INIT_EXPR_VALUE_INIT (t), |
92a596e8 | 4766 | lval, non_constant_p, overflow_p); |
2d76680f PC |
4767 | if (*non_constant_p) |
4768 | return t; | |
4769 | else | |
4770 | return r; | |
4771 | } | |
4772 | ||
e4511ca2 JM |
4773 | /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case |
4774 | where the desired type is an array of unknown bounds because the variable | |
4775 | has had its bounds deduced since the wrapping expression was created. */ | |
4776 | ||
4777 | static bool | |
4778 | same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2) | |
4779 | { | |
4780 | while (TREE_CODE (type1) == ARRAY_TYPE | |
4781 | && TREE_CODE (type2) == ARRAY_TYPE | |
4782 | && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2))) | |
4783 | { | |
4784 | type1 = TREE_TYPE (type1); | |
4785 | type2 = TREE_TYPE (type2); | |
4786 | } | |
4787 | return same_type_ignoring_top_level_qualifiers_p (type1, type2); | |
4788 | } | |
4789 | ||
43e84ce7 JJ |
4790 | /* Try to determine the currently active union member for an expression |
4791 | with UNION_TYPE. If it can be determined, return the FIELD_DECL, | |
4792 | otherwise return NULL_TREE. */ | |
4793 | ||
4794 | static tree | |
4795 | cxx_union_active_member (const constexpr_ctx *ctx, tree t) | |
4796 | { | |
4797 | constexpr_ctx new_ctx = *ctx; | |
4798 | new_ctx.quiet = true; | |
4799 | bool non_constant_p = false, overflow_p = false; | |
4800 | tree ctor = cxx_eval_constant_expression (&new_ctx, t, false, | |
4801 | &non_constant_p, | |
4802 | &overflow_p); | |
4803 | if (TREE_CODE (ctor) == CONSTRUCTOR | |
4804 | && CONSTRUCTOR_NELTS (ctor) == 1 | |
4805 | && CONSTRUCTOR_ELT (ctor, 0)->index | |
4806 | && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL) | |
4807 | return CONSTRUCTOR_ELT (ctor, 0)->index; | |
4808 | return NULL_TREE; | |
4809 | } | |
4810 | ||
0fe2ae29 JJ |
4811 | /* Helper function for cxx_fold_indirect_ref_1, called recursively. */ |
4812 | ||
4813 | static tree | |
43e84ce7 JJ |
4814 | cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type, |
4815 | tree op, unsigned HOST_WIDE_INT off, bool *empty_base) | |
0fe2ae29 JJ |
4816 | { |
4817 | tree optype = TREE_TYPE (op); | |
4818 | unsigned HOST_WIDE_INT const_nunits; | |
bc99c54d JM |
4819 | if (off == 0 && similar_type_p (optype, type)) |
4820 | return op; | |
4821 | else if (TREE_CODE (optype) == COMPLEX_TYPE | |
4822 | && similar_type_p (type, TREE_TYPE (optype))) | |
0fe2ae29 | 4823 | { |
0fe2ae29 | 4824 | /* *(foo *)&complexfoo => __real__ complexfoo */ |
bc99c54d | 4825 | if (off == 0) |
0fe2ae29 | 4826 | return build1_loc (loc, REALPART_EXPR, type, op); |
bc99c54d JM |
4827 | /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */ |
4828 | else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off) | |
4829 | return build1_loc (loc, IMAGPART_EXPR, type, op); | |
0fe2ae29 JJ |
4830 | } |
4831 | /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */ | |
4832 | else if (VECTOR_TYPE_P (optype) | |
4833 | && similar_type_p (type, TREE_TYPE (optype)) | |
4834 | && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits)) | |
4835 | { | |
4836 | unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type)); | |
4837 | unsigned HOST_WIDE_INT max_offset = part_width * const_nunits; | |
4838 | if (off < max_offset && off % part_width == 0) | |
4839 | { | |
4840 | tree index = bitsize_int (off * BITS_PER_UNIT); | |
4841 | return build3_loc (loc, BIT_FIELD_REF, type, op, | |
4842 | TYPE_SIZE (type), index); | |
4843 | } | |
4844 | } | |
4845 | /* ((foo *)&fooarray)[x] => fooarray[x] */ | |
4846 | else if (TREE_CODE (optype) == ARRAY_TYPE | |
4847 | && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype))) | |
4848 | && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype)))) | |
4849 | { | |
4850 | tree type_domain = TYPE_DOMAIN (optype); | |
4851 | tree min_val = size_zero_node; | |
4852 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
4853 | min_val = TYPE_MIN_VALUE (type_domain); | |
4854 | unsigned HOST_WIDE_INT el_sz | |
4855 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype))); | |
4856 | unsigned HOST_WIDE_INT idx = off / el_sz; | |
4857 | unsigned HOST_WIDE_INT rem = off % el_sz; | |
4858 | if (tree_fits_uhwi_p (min_val)) | |
4859 | { | |
4860 | tree index = size_int (idx + tree_to_uhwi (min_val)); | |
4861 | op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index, | |
4862 | NULL_TREE, NULL_TREE); | |
43e84ce7 | 4863 | return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem, |
0fe2ae29 JJ |
4864 | empty_base); |
4865 | } | |
4866 | } | |
4867 | /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */ | |
43e84ce7 JJ |
4868 | else if (TREE_CODE (optype) == RECORD_TYPE |
4869 | || TREE_CODE (optype) == UNION_TYPE) | |
0fe2ae29 | 4870 | { |
43e84ce7 JJ |
4871 | if (TREE_CODE (optype) == UNION_TYPE) |
4872 | /* For unions prefer the currently active member. */ | |
4873 | if (tree field = cxx_union_active_member (ctx, op)) | |
4874 | { | |
4875 | unsigned HOST_WIDE_INT el_sz | |
4876 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field))); | |
4877 | if (off < el_sz) | |
4878 | { | |
4879 | tree cop = build3 (COMPONENT_REF, TREE_TYPE (field), | |
4880 | op, field, NULL_TREE); | |
4881 | if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop, | |
4882 | off, empty_base)) | |
4883 | return ret; | |
4884 | } | |
4885 | } | |
0fe2ae29 JJ |
4886 | for (tree field = TYPE_FIELDS (optype); |
4887 | field; field = DECL_CHAIN (field)) | |
4888 | if (TREE_CODE (field) == FIELD_DECL | |
4889 | && TREE_TYPE (field) != error_mark_node | |
4890 | && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field)))) | |
4891 | { | |
4892 | tree pos = byte_position (field); | |
4893 | if (!tree_fits_uhwi_p (pos)) | |
4894 | continue; | |
4895 | unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos); | |
43e84ce7 | 4896 | unsigned HOST_WIDE_INT el_sz |
0fe2ae29 JJ |
4897 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field))); |
4898 | if (upos <= off && off < upos + el_sz) | |
4899 | { | |
4900 | tree cop = build3 (COMPONENT_REF, TREE_TYPE (field), | |
4901 | op, field, NULL_TREE); | |
43e84ce7 | 4902 | if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop, |
0fe2ae29 JJ |
4903 | off - upos, |
4904 | empty_base)) | |
4905 | return ret; | |
4906 | } | |
4907 | } | |
bc99c54d JM |
4908 | /* Also handle conversion to an empty base class, which |
4909 | is represented with a NOP_EXPR. */ | |
4910 | if (is_empty_class (type) | |
4911 | && CLASS_TYPE_P (optype) | |
4912 | && DERIVED_FROM_P (type, optype)) | |
4913 | { | |
4914 | *empty_base = true; | |
4915 | return op; | |
4916 | } | |
0fe2ae29 JJ |
4917 | } |
4918 | ||
4919 | return NULL_TREE; | |
4920 | } | |
4921 | ||
2d76680f PC |
4922 | /* A less strict version of fold_indirect_ref_1, which requires cv-quals to |
4923 | match. We want to be less strict for simple *& folding; if we have a | |
4924 | non-const temporary that we access through a const pointer, that should | |
4925 | work. We handle this here rather than change fold_indirect_ref_1 | |
4926 | because we're dealing with things like ADDR_EXPR of INTEGER_CST which | |
4927 | don't really make sense outside of constant expression evaluation. Also | |
4928 | we want to allow folding to COMPONENT_REF, which could cause trouble | |
0fe2ae29 | 4929 | with TBAA in fold_indirect_ref_1. */ |
2d76680f PC |
4930 | |
4931 | static tree | |
43e84ce7 JJ |
4932 | cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type, |
4933 | tree op0, bool *empty_base) | |
2d76680f | 4934 | { |
ebe4bf41 MP |
4935 | tree sub = op0; |
4936 | tree subtype; | |
4937 | poly_uint64 const_op01; | |
2d76680f | 4938 | |
80de0002 JM |
4939 | /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */ |
4940 | while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR | |
4941 | || TREE_CODE (sub) == VIEW_CONVERT_EXPR) | |
4942 | { | |
4943 | if (TREE_CODE (sub) == NOP_EXPR | |
4944 | && REINTERPRET_CAST_P (sub)) | |
4945 | return NULL_TREE; | |
4946 | sub = TREE_OPERAND (sub, 0); | |
4947 | } | |
4948 | ||
2d76680f | 4949 | subtype = TREE_TYPE (sub); |
71a93b08 | 4950 | if (!INDIRECT_TYPE_P (subtype)) |
2d76680f PC |
4951 | return NULL_TREE; |
4952 | ||
4953 | if (TREE_CODE (sub) == ADDR_EXPR) | |
4954 | { | |
4955 | tree op = TREE_OPERAND (sub, 0); | |
4956 | tree optype = TREE_TYPE (op); | |
4957 | ||
4958 | /* *&CONST_DECL -> to the value of the const decl. */ | |
4959 | if (TREE_CODE (op) == CONST_DECL) | |
4960 | return DECL_INITIAL (op); | |
4961 | /* *&p => p; make sure to handle *&"str"[cst] here. */ | |
1a120ec1 | 4962 | if (similar_type_p (optype, type)) |
2d76680f PC |
4963 | { |
4964 | tree fop = fold_read_from_constant_string (op); | |
4965 | if (fop) | |
4966 | return fop; | |
4967 | else | |
4968 | return op; | |
4969 | } | |
0fe2ae29 | 4970 | else |
43e84ce7 | 4971 | return cxx_fold_indirect_ref_1 (ctx, loc, type, op, 0, empty_base); |
2d76680f PC |
4972 | } |
4973 | else if (TREE_CODE (sub) == POINTER_PLUS_EXPR | |
0fe2ae29 | 4974 | && tree_fits_uhwi_p (TREE_OPERAND (sub, 1))) |
2d76680f PC |
4975 | { |
4976 | tree op00 = TREE_OPERAND (sub, 0); | |
0120cd93 | 4977 | tree off = TREE_OPERAND (sub, 1); |
2d76680f PC |
4978 | |
4979 | STRIP_NOPS (op00); | |
4980 | if (TREE_CODE (op00) == ADDR_EXPR) | |
0120cd93 PP |
4981 | { |
4982 | tree obj = TREE_OPERAND (op00, 0); | |
4983 | while (TREE_CODE (obj) == COMPONENT_REF | |
4984 | && tree_int_cst_sign_bit (off)) | |
4985 | { | |
4986 | /* Canonicalize this object/offset pair by iteratively absorbing | |
4987 | the innermost component into the offset until the offset is | |
4988 | nonnegative, so that cxx_fold_indirect_ref_1 can identify | |
4989 | more folding opportunities. */ | |
4990 | tree field = TREE_OPERAND (obj, 1); | |
4991 | off = int_const_binop (PLUS_EXPR, off, byte_position (field)); | |
4992 | obj = TREE_OPERAND (obj, 0); | |
4993 | } | |
4994 | return cxx_fold_indirect_ref_1 (ctx, loc, type, obj, | |
4995 | tree_to_uhwi (off), empty_base); | |
4996 | } | |
2d76680f PC |
4997 | } |
4998 | /* *(foo *)fooarrptr => (*fooarrptr)[0] */ | |
4999 | else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE | |
1a120ec1 | 5000 | && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype)))) |
2d76680f PC |
5001 | { |
5002 | tree type_domain; | |
5003 | tree min_val = size_zero_node; | |
ebe4bf41 | 5004 | tree newsub |
43e84ce7 | 5005 | = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL); |
2d76680f PC |
5006 | if (newsub) |
5007 | sub = newsub; | |
5008 | else | |
5009 | sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub); | |
5010 | type_domain = TYPE_DOMAIN (TREE_TYPE (sub)); | |
5011 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
5012 | min_val = TYPE_MIN_VALUE (type_domain); | |
5013 | return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE, | |
5014 | NULL_TREE); | |
5015 | } | |
5016 | ||
5017 | return NULL_TREE; | |
5018 | } | |
5019 | ||
5020 | static tree | |
3e605b20 | 5021 | cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t, |
92a596e8 | 5022 | bool lval, |
2d76680f PC |
5023 | bool *non_constant_p, bool *overflow_p) |
5024 | { | |
5025 | tree orig_op0 = TREE_OPERAND (t, 0); | |
2d76680f | 5026 | bool empty_base = false; |
2d76680f | 5027 | |
cda0a029 JM |
5028 | /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second |
5029 | operand is an integer-zero. Otherwise reject the MEM_REF for now. */ | |
5030 | ||
5031 | if (TREE_CODE (t) == MEM_REF | |
5032 | && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1)))) | |
5033 | { | |
5034 | gcc_assert (ctx->quiet); | |
5035 | *non_constant_p = true; | |
5036 | return t; | |
5037 | } | |
5038 | ||
255a48d6 | 5039 | /* First try to simplify it directly. */ |
43e84ce7 JJ |
5040 | tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), |
5041 | orig_op0, &empty_base); | |
255a48d6 | 5042 | if (!r) |
2d76680f | 5043 | { |
255a48d6 JM |
5044 | /* If that didn't work, evaluate the operand first. */ |
5045 | tree op0 = cxx_eval_constant_expression (ctx, orig_op0, | |
5046 | /*lval*/false, non_constant_p, | |
5047 | overflow_p); | |
5048 | /* Don't VERIFY_CONSTANT here. */ | |
5049 | if (*non_constant_p) | |
5050 | return t; | |
5051 | ||
8bada5cd MS |
5052 | if (!lval && integer_zerop (op0)) |
5053 | { | |
5054 | if (!ctx->quiet) | |
5055 | error ("dereferencing a null pointer"); | |
5056 | *non_constant_p = true; | |
5057 | return t; | |
5058 | } | |
5059 | ||
43e84ce7 | 5060 | r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0, |
255a48d6 JM |
5061 | &empty_base); |
5062 | if (r == NULL_TREE) | |
2d76680f PC |
5063 | { |
5064 | /* We couldn't fold to a constant value. Make sure it's not | |
5065 | something we should have been able to fold. */ | |
255a48d6 JM |
5066 | tree sub = op0; |
5067 | STRIP_NOPS (sub); | |
5068 | if (TREE_CODE (sub) == ADDR_EXPR) | |
5069 | { | |
1a120ec1 | 5070 | gcc_assert (!similar_type_p |
255a48d6 JM |
5071 | (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t))); |
5072 | /* DR 1188 says we don't have to deal with this. */ | |
5073 | if (!ctx->quiet) | |
1a120ec1 JM |
5074 | error_at (cp_expr_loc_or_input_loc (t), |
5075 | "accessing value of %qE through a %qT glvalue in a " | |
5076 | "constant expression", build_fold_indirect_ref (sub), | |
5077 | TREE_TYPE (t)); | |
255a48d6 JM |
5078 | *non_constant_p = true; |
5079 | return t; | |
5080 | } | |
5081 | ||
5082 | if (lval && op0 != orig_op0) | |
5083 | return build1 (INDIRECT_REF, TREE_TYPE (t), op0); | |
5084 | if (!lval) | |
5085 | VERIFY_CONSTANT (t); | |
2d76680f PC |
5086 | return t; |
5087 | } | |
5088 | } | |
5089 | ||
255a48d6 JM |
5090 | r = cxx_eval_constant_expression (ctx, r, |
5091 | lval, non_constant_p, overflow_p); | |
5092 | if (*non_constant_p) | |
5093 | return t; | |
5094 | ||
125db6a1 | 5095 | /* If we're pulling out the value of an empty base, just return an empty |
2d76680f | 5096 | CONSTRUCTOR. */ |
92a596e8 | 5097 | if (empty_base && !lval) |
2d76680f | 5098 | { |
2d76680f PC |
5099 | r = build_constructor (TREE_TYPE (t), NULL); |
5100 | TREE_CONSTANT (r) = true; | |
5101 | } | |
5102 | ||
2d76680f PC |
5103 | return r; |
5104 | } | |
5105 | ||
5106 | /* Complain about R, a VAR_DECL, not being usable in a constant expression. | |
5107 | Shared between potential_constant_expression and | |
5108 | cxx_eval_constant_expression. */ | |
5109 | ||
5110 | static void | |
6821245b | 5111 | non_const_var_error (location_t loc, tree r) |
2d76680f | 5112 | { |
8e007055 | 5113 | auto_diagnostic_group d; |
2d76680f | 5114 | tree type = TREE_TYPE (r); |
8e007055 | 5115 | if (DECL_NAME (r) == heap_uninit_identifier |
815baade JJ |
5116 | || DECL_NAME (r) == heap_identifier |
5117 | || DECL_NAME (r) == heap_vec_uninit_identifier | |
5118 | || DECL_NAME (r) == heap_vec_identifier) | |
8e007055 | 5119 | { |
6821245b JM |
5120 | error_at (loc, "the content of uninitialized storage is not usable " |
5121 | "in a constant expression"); | |
8e007055 JJ |
5122 | inform (DECL_SOURCE_LOCATION (r), "allocated here"); |
5123 | return; | |
5124 | } | |
5125 | if (DECL_NAME (r) == heap_deleted_identifier) | |
5126 | { | |
6821245b JM |
5127 | error_at (loc, "use of allocated storage after deallocation in a " |
5128 | "constant expression"); | |
8e007055 JJ |
5129 | inform (DECL_SOURCE_LOCATION (r), "allocated here"); |
5130 | return; | |
5131 | } | |
6821245b JM |
5132 | error_at (loc, "the value of %qD is not usable in a constant " |
5133 | "expression", r); | |
2d76680f PC |
5134 | /* Avoid error cascade. */ |
5135 | if (DECL_INITIAL (r) == error_mark_node) | |
5136 | return; | |
5137 | if (DECL_DECLARED_CONSTEXPR_P (r)) | |
5138 | inform (DECL_SOURCE_LOCATION (r), | |
5139 | "%qD used in its own initializer", r); | |
5140 | else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) | |
5141 | { | |
5142 | if (!CP_TYPE_CONST_P (type)) | |
5143 | inform (DECL_SOURCE_LOCATION (r), | |
5144 | "%q#D is not const", r); | |
5145 | else if (CP_TYPE_VOLATILE_P (type)) | |
5146 | inform (DECL_SOURCE_LOCATION (r), | |
5147 | "%q#D is volatile", r); | |
5148 | else if (!DECL_INITIAL (r) | |
a3e2b438 PP |
5149 | || !TREE_CONSTANT (DECL_INITIAL (r)) |
5150 | || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)) | |
2d76680f PC |
5151 | inform (DECL_SOURCE_LOCATION (r), |
5152 | "%qD was not initialized with a constant " | |
5153 | "expression", r); | |
5154 | else | |
5155 | gcc_unreachable (); | |
5156 | } | |
9f613f06 | 5157 | else if (TYPE_REF_P (type)) |
fd338b13 JM |
5158 | inform (DECL_SOURCE_LOCATION (r), |
5159 | "%qD was not initialized with a constant " | |
5160 | "expression", r); | |
2d76680f PC |
5161 | else |
5162 | { | |
5163 | if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r)) | |
5164 | inform (DECL_SOURCE_LOCATION (r), | |
5165 | "%qD was not declared %<constexpr%>", r); | |
5166 | else | |
5167 | inform (DECL_SOURCE_LOCATION (r), | |
5168 | "%qD does not have integral or enumeration type", | |
5169 | r); | |
5170 | } | |
5171 | } | |
5172 | ||
5173 | /* Subroutine of cxx_eval_constant_expression. | |
5174 | Like cxx_eval_unary_expression, except for trinary expressions. */ | |
5175 | ||
5176 | static tree | |
3e605b20 | 5177 | cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t, |
92a596e8 | 5178 | bool lval, |
2d76680f PC |
5179 | bool *non_constant_p, bool *overflow_p) |
5180 | { | |
5181 | int i; | |
5182 | tree args[3]; | |
5183 | tree val; | |
5184 | ||
5185 | for (i = 0; i < 3; i++) | |
5186 | { | |
3e605b20 | 5187 | args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i), |
92a596e8 | 5188 | lval, |
5a804683 | 5189 | non_constant_p, overflow_p); |
2d76680f PC |
5190 | VERIFY_CONSTANT (args[i]); |
5191 | } | |
5192 | ||
5193 | val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t), | |
5194 | args[0], args[1], args[2]); | |
5195 | if (val == NULL_TREE) | |
5196 | return t; | |
5197 | VERIFY_CONSTANT (val); | |
5198 | return val; | |
5199 | } | |
5200 | ||
98e5a19a JM |
5201 | /* True if T was declared in a function declared to be constexpr, and |
5202 | therefore potentially constant in C++14. */ | |
5203 | ||
2d76680f PC |
5204 | bool |
5205 | var_in_constexpr_fn (tree t) | |
5206 | { | |
5207 | tree ctx = DECL_CONTEXT (t); | |
98e5a19a | 5208 | return (ctx && TREE_CODE (ctx) == FUNCTION_DECL |
2d76680f PC |
5209 | && DECL_DECLARED_CONSTEXPR_P (ctx)); |
5210 | } | |
5211 | ||
26dbe85a MP |
5212 | /* True if a function might be constexpr: either a function that was |
5213 | declared constexpr, or a C++17 lambda op(). */ | |
5214 | ||
5215 | bool | |
5216 | maybe_constexpr_fn (tree t) | |
5217 | { | |
5218 | return (DECL_DECLARED_CONSTEXPR_P (t) | |
5219 | || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t))); | |
5220 | } | |
5221 | ||
98e5a19a JM |
5222 | /* True if T was declared in a function that might be constexpr: either a |
5223 | function that was declared constexpr, or a C++17 lambda op(). */ | |
5224 | ||
5225 | bool | |
5226 | var_in_maybe_constexpr_fn (tree t) | |
5227 | { | |
7b936140 | 5228 | if (cxx_dialect >= cxx17 |
98e5a19a JM |
5229 | && DECL_FUNCTION_SCOPE_P (t) |
5230 | && LAMBDA_FUNCTION_P (DECL_CONTEXT (t))) | |
5231 | return true; | |
5232 | return var_in_constexpr_fn (t); | |
5233 | } | |
5234 | ||
68d01920 JM |
5235 | /* We're assigning INIT to TARGET. In do_build_copy_constructor and |
5236 | build_over_call we implement trivial copy of a class with tail padding using | |
5237 | assignment of character arrays, which is valid in normal code, but not in | |
5238 | constexpr evaluation. We don't need to worry about clobbering tail padding | |
5239 | in constexpr evaluation, so strip the type punning. */ | |
5240 | ||
5241 | static void | |
5242 | maybe_simplify_trivial_copy (tree &target, tree &init) | |
5243 | { | |
5244 | if (TREE_CODE (target) == MEM_REF | |
5245 | && TREE_CODE (init) == MEM_REF | |
5246 | && TREE_TYPE (target) == TREE_TYPE (init) | |
5247 | && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE | |
5248 | && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node) | |
5249 | { | |
5250 | target = build_fold_indirect_ref (TREE_OPERAND (target, 0)); | |
5251 | init = build_fold_indirect_ref (TREE_OPERAND (init, 0)); | |
5252 | } | |
5253 | } | |
5254 | ||
7eb5be6a MP |
5255 | /* Returns true if REF, which is a COMPONENT_REF, has any fields |
5256 | of constant type. This does not check for 'mutable', so the | |
5257 | caller is expected to be mindful of that. */ | |
5258 | ||
5259 | static bool | |
5260 | cref_has_const_field (tree ref) | |
5261 | { | |
5262 | while (TREE_CODE (ref) == COMPONENT_REF) | |
5263 | { | |
5264 | if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1)))) | |
5265 | return true; | |
5266 | ref = TREE_OPERAND (ref, 0); | |
5267 | } | |
5268 | return false; | |
5269 | } | |
5270 | ||
04e1749c MP |
5271 | /* Return true if we are modifying something that is const during constant |
5272 | expression evaluation. CODE is the code of the statement, OBJ is the | |
5273 | object in question, MUTABLE_P is true if one of the subobjects were | |
5274 | declared mutable. */ | |
5275 | ||
5276 | static bool | |
5277 | modifying_const_object_p (tree_code code, tree obj, bool mutable_p) | |
5278 | { | |
5279 | /* If this is initialization, there's no problem. */ | |
5280 | if (code != MODIFY_EXPR) | |
5281 | return false; | |
5282 | ||
5283 | /* [basic.type.qualifier] "A const object is an object of type | |
5284 | const T or a non-mutable subobject of a const object." */ | |
5285 | if (mutable_p) | |
5286 | return false; | |
5287 | ||
7eb5be6a MP |
5288 | if (TREE_READONLY (obj)) |
5289 | return true; | |
5290 | ||
5291 | if (CP_TYPE_CONST_P (TREE_TYPE (obj))) | |
5292 | { | |
5293 | /* Although a COMPONENT_REF may have a const type, we should | |
5294 | only consider it modifying a const object when any of the | |
5295 | field components is const. This can happen when using | |
5296 | constructs such as const_cast<const T &>(m), making something | |
5297 | const even though it wasn't declared const. */ | |
5298 | if (TREE_CODE (obj) == COMPONENT_REF) | |
5299 | return cref_has_const_field (obj); | |
5300 | else | |
5301 | return true; | |
5302 | } | |
5303 | ||
5304 | return false; | |
04e1749c MP |
5305 | } |
5306 | ||
60813a46 JM |
5307 | /* Evaluate an INIT_EXPR or MODIFY_EXPR. */ |
5308 | ||
5309 | static tree | |
5310 | cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, | |
92a596e8 | 5311 | bool lval, |
60813a46 JM |
5312 | bool *non_constant_p, bool *overflow_p) |
5313 | { | |
5314 | constexpr_ctx new_ctx = *ctx; | |
5315 | ||
58cc255c JM |
5316 | tree init = TREE_OPERAND (t, 1); |
5317 | if (TREE_CLOBBER_P (init)) | |
5318 | /* Just ignore clobbers. */ | |
5319 | return void_node; | |
5320 | ||
60813a46 JM |
5321 | /* First we figure out where we're storing to. */ |
5322 | tree target = TREE_OPERAND (t, 0); | |
68d01920 JM |
5323 | |
5324 | maybe_simplify_trivial_copy (target, init); | |
5325 | ||
3f8e2835 | 5326 | tree type = TREE_TYPE (target); |
e8b3c1bc JM |
5327 | bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR; |
5328 | if (preeval) | |
5329 | { | |
5330 | /* Evaluate the value to be stored without knowing what object it will be | |
5331 | stored in, so that any side-effects happen first. */ | |
5332 | if (!SCALAR_TYPE_P (type)) | |
5333 | new_ctx.ctor = new_ctx.object = NULL_TREE; | |
5334 | init = cxx_eval_constant_expression (&new_ctx, init, false, | |
5335 | non_constant_p, overflow_p); | |
5336 | if (*non_constant_p) | |
5337 | return t; | |
5338 | } | |
60813a46 | 5339 | |
d0aa42d2 JM |
5340 | bool evaluated = false; |
5341 | if (lval) | |
3f8e2835 | 5342 | { |
d0aa42d2 JM |
5343 | /* If we want to return a reference to the target, we need to evaluate it |
5344 | as a whole; otherwise, only evaluate the innermost piece to avoid | |
5345 | building up unnecessary *_REFs. */ | |
5346 | target = cxx_eval_constant_expression (ctx, target, true, | |
5347 | non_constant_p, overflow_p); | |
5348 | evaluated = true; | |
5349 | if (*non_constant_p) | |
5350 | return t; | |
3f8e2835 JM |
5351 | } |
5352 | ||
043666e0 | 5353 | /* Find the underlying variable. */ |
cd9cf97b | 5354 | releasing_vec refs; |
60813a46 | 5355 | tree object = NULL_TREE; |
04e1749c MP |
5356 | /* If we're modifying a const object, save it. */ |
5357 | tree const_object_being_modified = NULL_TREE; | |
5358 | bool mutable_p = false; | |
60813a46 JM |
5359 | for (tree probe = target; object == NULL_TREE; ) |
5360 | { | |
5361 | switch (TREE_CODE (probe)) | |
5362 | { | |
5363 | case BIT_FIELD_REF: | |
5364 | case COMPONENT_REF: | |
5365 | case ARRAY_REF: | |
043666e0 JM |
5366 | { |
5367 | tree ob = TREE_OPERAND (probe, 0); | |
5368 | tree elt = TREE_OPERAND (probe, 1); | |
7d349dd8 | 5369 | if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt)) |
04e1749c | 5370 | mutable_p = true; |
043666e0 JM |
5371 | if (TREE_CODE (probe) == ARRAY_REF) |
5372 | { | |
5373 | elt = eval_and_check_array_index (ctx, probe, false, | |
5374 | non_constant_p, overflow_p); | |
5375 | if (*non_constant_p) | |
5376 | return t; | |
5377 | } | |
0f184800 MP |
5378 | /* We don't check modifying_const_object_p for ARRAY_REFs. Given |
5379 | "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though | |
5380 | the array isn't const. Instead, check "a" in the next iteration; | |
5381 | that will detect modifying "const int a[10]". */ | |
5382 | else if (evaluated | |
5383 | && modifying_const_object_p (TREE_CODE (t), probe, | |
5384 | mutable_p) | |
5385 | && const_object_being_modified == NULL_TREE) | |
5386 | const_object_being_modified = probe; | |
043666e0 JM |
5387 | vec_safe_push (refs, elt); |
5388 | vec_safe_push (refs, TREE_TYPE (probe)); | |
5389 | probe = ob; | |
5390 | } | |
60813a46 JM |
5391 | break; |
5392 | ||
5393 | default: | |
d0aa42d2 JM |
5394 | if (evaluated) |
5395 | object = probe; | |
5396 | else | |
5397 | { | |
5398 | probe = cxx_eval_constant_expression (ctx, probe, true, | |
5399 | non_constant_p, overflow_p); | |
5400 | evaluated = true; | |
5401 | if (*non_constant_p) | |
5402 | return t; | |
5403 | } | |
5404 | break; | |
60813a46 JM |
5405 | } |
5406 | } | |
5407 | ||
04e1749c MP |
5408 | if (modifying_const_object_p (TREE_CODE (t), object, mutable_p) |
5409 | && const_object_being_modified == NULL_TREE) | |
5410 | const_object_being_modified = object; | |
5411 | ||
60813a46 JM |
5412 | /* And then find/build up our initializer for the path to the subobject |
5413 | we're initializing. */ | |
a1408b7e | 5414 | tree *valp; |
077dd9b3 | 5415 | if (DECL_P (object)) |
8e007055 | 5416 | valp = ctx->global->values.get (object); |
a1408b7e JM |
5417 | else |
5418 | valp = NULL; | |
60813a46 JM |
5419 | if (!valp) |
5420 | { | |
5421 | /* A constant-expression cannot modify objects from outside the | |
5422 | constant-expression. */ | |
2b3ab879 | 5423 | if (!ctx->quiet) |
64d6d399 | 5424 | error ("modification of %qE is not a constant expression", object); |
60813a46 JM |
5425 | *non_constant_p = true; |
5426 | return t; | |
5427 | } | |
3f8e2835 | 5428 | type = TREE_TYPE (object); |
c75ce530 | 5429 | bool no_zero_init = true; |
2d63bc39 | 5430 | |
37244b21 PP |
5431 | releasing_vec ctors, indexes; |
5432 | auto_vec<int> index_pos_hints; | |
5433 | bool activated_union_member_p = false; | |
276a52d5 | 5434 | while (!refs->is_empty ()) |
60813a46 JM |
5435 | { |
5436 | if (*valp == NULL_TREE) | |
5437 | { | |
5438 | *valp = build_constructor (type, NULL); | |
e8c48716 | 5439 | CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; |
60813a46 | 5440 | } |
d6b46fca NS |
5441 | else if (TREE_CODE (*valp) == STRING_CST) |
5442 | { | |
5443 | /* An array was initialized with a string constant, and now | |
5444 | we're writing into one of its elements. Explode the | |
5445 | single initialization into a set of element | |
5446 | initializations. */ | |
5447 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); | |
5448 | ||
5449 | tree string = *valp; | |
5450 | tree elt_type = TREE_TYPE (type); | |
5451 | unsigned chars_per_elt = (TYPE_PRECISION (elt_type) | |
5452 | / TYPE_PRECISION (char_type_node)); | |
5453 | unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt; | |
5454 | tree ary_ctor = build_constructor (type, NULL); | |
5455 | ||
5456 | vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts); | |
5457 | for (unsigned ix = 0; ix != num_elts; ix++) | |
5458 | { | |
5459 | constructor_elt elt = | |
5460 | { | |
5461 | build_int_cst (size_type_node, ix), | |
5462 | extract_string_elt (string, chars_per_elt, ix) | |
5463 | }; | |
5464 | CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt); | |
5465 | } | |
94ff4c9d | 5466 | |
d6b46fca NS |
5467 | *valp = ary_ctor; |
5468 | } | |
5469 | ||
c75ce530 JM |
5470 | /* If the value of object is already zero-initialized, any new ctors for |
5471 | subobjects will also be zero-initialized. */ | |
e8c48716 | 5472 | no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp); |
60813a46 | 5473 | |
ceaaf873 | 5474 | enum tree_code code = TREE_CODE (type); |
60813a46 | 5475 | type = refs->pop(); |
ceaaf873 | 5476 | tree index = refs->pop(); |
60813a46 | 5477 | |
a8dd2b3e | 5478 | if (code == RECORD_TYPE && is_empty_field (index)) |
a4dfd0f0 JM |
5479 | /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they |
5480 | have no data and might have an offset lower than previously declared | |
5481 | fields, which confuses the middle-end. The code below will notice | |
5482 | that we don't have a CONSTRUCTOR for our inner target and just | |
5483 | return init. */ | |
5484 | break; | |
94ff4c9d | 5485 | |
37244b21 PP |
5486 | if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp) |
5487 | && CONSTRUCTOR_ELT (*valp, 0)->index != index) | |
ceaaf873 | 5488 | { |
b04445d4 | 5489 | if (cxx_dialect < cxx20) |
ed4ec9ce | 5490 | { |
37244b21 PP |
5491 | if (!ctx->quiet) |
5492 | error_at (cp_expr_loc_or_input_loc (t), | |
5493 | "change of the active member of a union " | |
5494 | "from %qD to %qD", | |
5495 | CONSTRUCTOR_ELT (*valp, 0)->index, | |
5496 | index); | |
5497 | *non_constant_p = true; | |
ed4ec9ce | 5498 | } |
37244b21 PP |
5499 | else if (TREE_CODE (t) == MODIFY_EXPR |
5500 | && CONSTRUCTOR_NO_CLEARING (*valp)) | |
ceaaf873 | 5501 | { |
37244b21 PP |
5502 | /* Diagnose changing the active union member while the union |
5503 | is in the process of being initialized. */ | |
5504 | if (!ctx->quiet) | |
5505 | error_at (cp_expr_loc_or_input_loc (t), | |
5506 | "change of the active member of a union " | |
5507 | "from %qD to %qD during initialization", | |
5508 | CONSTRUCTOR_ELT (*valp, 0)->index, | |
5509 | index); | |
5510 | *non_constant_p = true; | |
ceaaf873 | 5511 | } |
37244b21 PP |
5512 | no_zero_init = true; |
5513 | } | |
88504f34 | 5514 | |
37244b21 PP |
5515 | vec_safe_push (ctors, *valp); |
5516 | vec_safe_push (indexes, index); | |
88504f34 | 5517 | |
37244b21 | 5518 | constructor_elt *cep |
077dd9b3 | 5519 | = get_or_insert_ctor_field (*valp, index); |
37244b21 PP |
5520 | index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin()); |
5521 | ||
5522 | if (code == UNION_TYPE) | |
5523 | activated_union_member_p = true; | |
b599bf9d | 5524 | |
60813a46 JM |
5525 | valp = &cep->value; |
5526 | } | |
60813a46 | 5527 | |
04e1749c MP |
5528 | /* Detect modifying a constant object in constexpr evaluation. |
5529 | We have found a const object that is being modified. Figure out | |
5530 | if we need to issue an error. Consider | |
5531 | ||
5532 | struct A { | |
5533 | int n; | |
5534 | constexpr A() : n(1) { n = 2; } // #1 | |
5535 | }; | |
5536 | struct B { | |
5537 | const A a; | |
5538 | constexpr B() { a.n = 3; } // #2 | |
5539 | }; | |
5540 | constexpr B b{}; | |
5541 | ||
5542 | #1 is OK, since we're modifying an object under construction, but | |
5543 | #2 is wrong, since "a" is const and has been fully constructed. | |
5544 | To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR | |
5545 | which means that the object is read-only. For the example above, the | |
5546 | *ctors stack at the point of #2 will look like: | |
5547 | ||
5548 | ctors[0] = {.a={.n=2}} TREE_READONLY = 0 | |
5549 | ctors[1] = {.n=2} TREE_READONLY = 1 | |
5550 | ||
5551 | and we're modifying "b.a", so we search the stack and see if the | |
5552 | constructor for "b.a" has already run. */ | |
5553 | if (const_object_being_modified) | |
5554 | { | |
5555 | bool fail = false; | |
276a52d5 JJ |
5556 | tree const_objtype |
5557 | = strip_array_types (TREE_TYPE (const_object_being_modified)); | |
5558 | if (!CLASS_TYPE_P (const_objtype)) | |
04e1749c MP |
5559 | fail = true; |
5560 | else | |
5561 | { | |
5562 | /* [class.ctor]p5 "A constructor can be invoked for a const, | |
5563 | volatile, or const volatile object. const and volatile | |
5564 | semantics are not applied on an object under construction. | |
5565 | They come into effect when the constructor for the most | |
5566 | derived object ends." */ | |
3f207ab3 | 5567 | for (tree elt : *ctors) |
04e1749c MP |
5568 | if (same_type_ignoring_top_level_qualifiers_p |
5569 | (TREE_TYPE (const_object_being_modified), TREE_TYPE (elt))) | |
5570 | { | |
5571 | fail = TREE_READONLY (elt); | |
5572 | break; | |
5573 | } | |
5574 | } | |
5575 | if (fail) | |
5576 | { | |
5577 | if (!ctx->quiet) | |
5578 | modifying_const_object_error (t, const_object_being_modified); | |
5579 | *non_constant_p = true; | |
5580 | return t; | |
5581 | } | |
5582 | } | |
5583 | ||
e8b3c1bc | 5584 | if (!preeval) |
60813a46 | 5585 | { |
6c59b8a9 JM |
5586 | /* We're handling an INIT_EXPR of class type, so the value of the |
5587 | initializer can depend on the object it's initializing. */ | |
5588 | ||
60813a46 JM |
5589 | /* Create a new CONSTRUCTOR in case evaluation of the initializer |
5590 | wants to modify it. */ | |
acb2970c | 5591 | if (*valp == NULL_TREE) |
73a84269 | 5592 | { |
664beaf2 | 5593 | *valp = build_constructor (type, NULL); |
e8c48716 | 5594 | CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; |
73a84269 | 5595 | } |
664beaf2 | 5596 | new_ctx.ctor = *valp; |
60813a46 | 5597 | new_ctx.object = target; |
10d2f801 JM |
5598 | /* Avoid temporary materialization when initializing from a TARGET_EXPR. |
5599 | We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because | |
5600 | expansion of those trees uses ctx instead. */ | |
5601 | if (TREE_CODE (init) == TARGET_EXPR) | |
5602 | if (tree tinit = TARGET_EXPR_INITIAL (init)) | |
5603 | init = tinit; | |
e8b3c1bc JM |
5604 | init = cxx_eval_constant_expression (&new_ctx, init, false, |
5605 | non_constant_p, overflow_p); | |
37244b21 PP |
5606 | /* The hash table might have moved since the get earlier, and the |
5607 | initializer might have mutated the underlying CONSTRUCTORs, so we must | |
5608 | recompute VALP. */ | |
5609 | valp = ctx->global->values.get (object); | |
5610 | for (unsigned i = 0; i < vec_safe_length (indexes); i++) | |
5611 | { | |
5612 | constructor_elt *cep | |
5613 | = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]); | |
5614 | valp = &cep->value; | |
5615 | } | |
60813a46 JM |
5616 | } |
5617 | ||
7574c916 | 5618 | /* Don't share a CONSTRUCTOR that might be changed later. */ |
0146e25f | 5619 | init = unshare_constructor (init); |
d96e8407 | 5620 | |
e8b3c1bc JM |
5621 | if (*valp && TREE_CODE (*valp) == CONSTRUCTOR |
5622 | && TREE_CODE (init) == CONSTRUCTOR) | |
acb2970c | 5623 | { |
d96e8407 JM |
5624 | /* An outer ctx->ctor might be pointing to *valp, so replace |
5625 | its contents. */ | |
d0aa42d2 JM |
5626 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), |
5627 | TREE_TYPE (*valp))) | |
5628 | { | |
5629 | /* For initialization of an empty base, the original target will be | |
5630 | *(base*)this, evaluation of which resolves to the object | |
5631 | argument, which has the derived type rather than the base type. In | |
5632 | this situation, just evaluate the initializer and return, since | |
5633 | there's no actual data to store. */ | |
21fd62e5 PP |
5634 | gcc_assert (is_empty_class (TREE_TYPE (init))); |
5635 | return lval ? target : init; | |
d0aa42d2 | 5636 | } |
d96e8407 JM |
5637 | CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init); |
5638 | TREE_CONSTANT (*valp) = TREE_CONSTANT (init); | |
5639 | TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init); | |
e8c48716 JM |
5640 | CONSTRUCTOR_NO_CLEARING (*valp) |
5641 | = CONSTRUCTOR_NO_CLEARING (init); | |
acb2970c | 5642 | } |
35e8b38a JJ |
5643 | else if (TREE_CODE (init) == CONSTRUCTOR |
5644 | && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), | |
5645 | type)) | |
5646 | { | |
5647 | /* See above on initialization of empty bases. */ | |
5648 | gcc_assert (is_empty_class (TREE_TYPE (init)) && !lval); | |
5649 | return init; | |
5650 | } | |
60813a46 | 5651 | else |
d96e8407 | 5652 | *valp = init; |
2d63bc39 | 5653 | |
7eb5be6a MP |
5654 | /* After initialization, 'const' semantics apply to the value of the |
5655 | object. Make a note of this fact by marking the CONSTRUCTOR | |
5656 | TREE_READONLY. */ | |
5657 | if (TREE_CODE (t) == INIT_EXPR | |
5658 | && TREE_CODE (*valp) == CONSTRUCTOR | |
5659 | && TYPE_READONLY (type)) | |
64da1b76 PP |
5660 | { |
5661 | if (INDIRECT_REF_P (target) | |
5662 | && (is_this_parameter | |
5663 | (tree_strip_nop_conversions (TREE_OPERAND (target, 0))))) | |
5664 | /* We've just initialized '*this' (perhaps via the target | |
5665 | constructor of a delegating constructor). Leave it up to the | |
5666 | caller that set 'this' to set TREE_READONLY appropriately. */ | |
5667 | gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p | |
5668 | (TREE_TYPE (target), type)); | |
5669 | else | |
5670 | TREE_READONLY (*valp) = true; | |
5671 | } | |
7eb5be6a | 5672 | |
d96e8407 JM |
5673 | /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing |
5674 | CONSTRUCTORs, if any. */ | |
d96e8407 JM |
5675 | bool c = TREE_CONSTANT (init); |
5676 | bool s = TREE_SIDE_EFFECTS (init); | |
37244b21 | 5677 | if (!c || s || activated_union_member_p) |
3f207ab3 | 5678 | for (tree elt : *ctors) |
d96e8407 JM |
5679 | { |
5680 | if (!c) | |
5681 | TREE_CONSTANT (elt) = false; | |
5682 | if (s) | |
5683 | TREE_SIDE_EFFECTS (elt) = true; | |
b599bf9d PP |
5684 | /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of |
5685 | this union. */ | |
5686 | if (TREE_CODE (TREE_TYPE (elt)) == UNION_TYPE) | |
5687 | CONSTRUCTOR_NO_CLEARING (elt) = false; | |
d96e8407 | 5688 | } |
60813a46 JM |
5689 | |
5690 | if (*non_constant_p) | |
5691 | return t; | |
92a596e8 | 5692 | else if (lval) |
60813a46 JM |
5693 | return target; |
5694 | else | |
3bdf0a9b | 5695 | return init; |
60813a46 JM |
5696 | } |
5697 | ||
5698 | /* Evaluate a ++ or -- expression. */ | |
5699 | ||
5700 | static tree | |
5701 | cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t, | |
92a596e8 | 5702 | bool lval, |
60813a46 JM |
5703 | bool *non_constant_p, bool *overflow_p) |
5704 | { | |
5705 | enum tree_code code = TREE_CODE (t); | |
5706 | tree type = TREE_TYPE (t); | |
5707 | tree op = TREE_OPERAND (t, 0); | |
5708 | tree offset = TREE_OPERAND (t, 1); | |
5709 | gcc_assert (TREE_CONSTANT (offset)); | |
5710 | ||
d85569f6 MP |
5711 | /* OFFSET is constant, but perhaps not constant enough. We need to |
5712 | e.g. bash FLOAT_EXPRs to REAL_CSTs. */ | |
5713 | offset = fold_simple (offset); | |
5714 | ||
60813a46 | 5715 | /* The operand as an lvalue. */ |
2b3ab879 | 5716 | op = cxx_eval_constant_expression (ctx, op, true, |
5a804683 | 5717 | non_constant_p, overflow_p); |
60813a46 JM |
5718 | |
5719 | /* The operand as an rvalue. */ | |
84dd815f JM |
5720 | tree val |
5721 | = cxx_eval_constant_expression (ctx, op, false, | |
5722 | non_constant_p, overflow_p); | |
caee690e JM |
5723 | /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to |
5724 | a local array in a constexpr function. */ | |
71a93b08 | 5725 | bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val)); |
caee690e JM |
5726 | if (!ptr) |
5727 | VERIFY_CONSTANT (val); | |
60813a46 JM |
5728 | |
5729 | /* The modified value. */ | |
5730 | bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR); | |
ac2f8d26 | 5731 | tree mod; |
71a93b08 | 5732 | if (INDIRECT_TYPE_P (type)) |
ac2f8d26 JM |
5733 | { |
5734 | /* The middle end requires pointers to use POINTER_PLUS_EXPR. */ | |
5735 | offset = convert_to_ptrofftype (offset); | |
5736 | if (!inc) | |
5737 | offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset); | |
5738 | mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset); | |
5739 | } | |
5740 | else | |
5741 | mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset); | |
caee690e JM |
5742 | if (!ptr) |
5743 | VERIFY_CONSTANT (mod); | |
60813a46 JM |
5744 | |
5745 | /* Storing the modified value. */ | |
04e1749c MP |
5746 | tree store = build2_loc (cp_expr_loc_or_loc (t, input_location), |
5747 | MODIFY_EXPR, type, op, mod); | |
d1bba463 PP |
5748 | mod = cxx_eval_constant_expression (ctx, store, lval, |
5749 | non_constant_p, overflow_p); | |
ecdcd560 | 5750 | ggc_free (store); |
d1bba463 PP |
5751 | if (*non_constant_p) |
5752 | return t; | |
60813a46 JM |
5753 | |
5754 | /* And the value of the expression. */ | |
5755 | if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR) | |
d1bba463 PP |
5756 | /* Prefix ops are lvalues, but the caller might want an rvalue; |
5757 | lval has already been taken into account in the store above. */ | |
5758 | return mod; | |
60813a46 JM |
5759 | else |
5760 | /* Postfix ops are rvalues. */ | |
5761 | return val; | |
5762 | } | |
5763 | ||
56632b27 JM |
5764 | /* Predicates for the meaning of *jump_target. */ |
5765 | ||
5766 | static bool | |
5767 | returns (tree *jump_target) | |
5768 | { | |
5769 | return *jump_target | |
d49318d9 PC |
5770 | && (TREE_CODE (*jump_target) == RETURN_EXPR |
5771 | || (TREE_CODE (*jump_target) == LABEL_DECL | |
5772 | && LABEL_DECL_CDTOR (*jump_target))); | |
56632b27 JM |
5773 | } |
5774 | ||
5775 | static bool | |
5776 | breaks (tree *jump_target) | |
5777 | { | |
5778 | return *jump_target | |
06ec22b7 JM |
5779 | && ((TREE_CODE (*jump_target) == LABEL_DECL |
5780 | && LABEL_DECL_BREAK (*jump_target)) | |
3075affd | 5781 | || TREE_CODE (*jump_target) == BREAK_STMT |
06ec22b7 | 5782 | || TREE_CODE (*jump_target) == EXIT_EXPR); |
56632b27 JM |
5783 | } |
5784 | ||
5785 | static bool | |
5786 | continues (tree *jump_target) | |
5787 | { | |
5788 | return *jump_target | |
3075affd JM |
5789 | && ((TREE_CODE (*jump_target) == LABEL_DECL |
5790 | && LABEL_DECL_CONTINUE (*jump_target)) | |
5791 | || TREE_CODE (*jump_target) == CONTINUE_STMT); | |
5792 | ||
56632b27 JM |
5793 | } |
5794 | ||
5795 | static bool | |
5796 | switches (tree *jump_target) | |
5797 | { | |
5798 | return *jump_target | |
5799 | && TREE_CODE (*jump_target) == INTEGER_CST; | |
5800 | } | |
5801 | ||
5802 | /* Subroutine of cxx_eval_statement_list. Determine whether the statement | |
4b390698 JJ |
5803 | STMT matches *jump_target. If we're looking for a case label and we see |
5804 | the default label, note it in ctx->css_state. */ | |
56632b27 JM |
5805 | |
5806 | static bool | |
4b390698 | 5807 | label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt) |
56632b27 | 5808 | { |
56632b27 JM |
5809 | switch (TREE_CODE (*jump_target)) |
5810 | { | |
5811 | case LABEL_DECL: | |
5812 | if (TREE_CODE (stmt) == LABEL_EXPR | |
5813 | && LABEL_EXPR_LABEL (stmt) == *jump_target) | |
5814 | return true; | |
5815 | break; | |
5816 | ||
5817 | case INTEGER_CST: | |
5818 | if (TREE_CODE (stmt) == CASE_LABEL_EXPR) | |
5819 | { | |
4b390698 | 5820 | gcc_assert (ctx->css_state != NULL); |
56632b27 | 5821 | if (!CASE_LOW (stmt)) |
4b390698 JJ |
5822 | { |
5823 | /* default: should appear just once in a SWITCH_EXPR | |
5824 | body (excluding nested SWITCH_EXPR). */ | |
5825 | gcc_assert (*ctx->css_state != css_default_seen); | |
5826 | /* When evaluating SWITCH_EXPR body for the second time, | |
5827 | return true for the default: label. */ | |
5828 | if (*ctx->css_state == css_default_processing) | |
5829 | return true; | |
5830 | *ctx->css_state = css_default_seen; | |
5831 | } | |
385ed708 JJ |
5832 | else if (CASE_HIGH (stmt)) |
5833 | { | |
5834 | if (tree_int_cst_le (CASE_LOW (stmt), *jump_target) | |
5835 | && tree_int_cst_le (*jump_target, CASE_HIGH (stmt))) | |
5836 | return true; | |
5837 | } | |
56632b27 JM |
5838 | else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt))) |
5839 | return true; | |
5840 | } | |
5841 | break; | |
5842 | ||
43574e4f JJ |
5843 | case BREAK_STMT: |
5844 | case CONTINUE_STMT: | |
5845 | /* These two are handled directly in cxx_eval_loop_expr by testing | |
5846 | breaks (jump_target) or continues (jump_target). */ | |
5847 | break; | |
5848 | ||
56632b27 JM |
5849 | default: |
5850 | gcc_unreachable (); | |
5851 | } | |
5852 | return false; | |
5853 | } | |
5854 | ||
5855 | /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump | |
5856 | semantics, for switch, break, continue, and return. */ | |
5857 | ||
5858 | static tree | |
5859 | cxx_eval_statement_list (const constexpr_ctx *ctx, tree t, | |
56632b27 JM |
5860 | bool *non_constant_p, bool *overflow_p, |
5861 | tree *jump_target) | |
5862 | { | |
58611fb6 | 5863 | tree local_target; |
345edb37 JJ |
5864 | /* In a statement-expression we want to return the last value. |
5865 | For empty statement expression return void_node. */ | |
5866 | tree r = void_node; | |
58611fb6 JM |
5867 | if (!jump_target) |
5868 | { | |
5869 | local_target = NULL_TREE; | |
5870 | jump_target = &local_target; | |
5871 | } | |
0f54cc9c | 5872 | for (tree stmt : tsi_range (t)) |
56632b27 | 5873 | { |
0250ba58 MP |
5874 | /* We've found a continue, so skip everything until we reach |
5875 | the label its jumping to. */ | |
5876 | if (continues (jump_target)) | |
5877 | { | |
5878 | if (label_matches (ctx, jump_target, stmt)) | |
5879 | /* Found it. */ | |
5880 | *jump_target = NULL_TREE; | |
5881 | else | |
5882 | continue; | |
5883 | } | |
6ef72c36 JJ |
5884 | if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT) |
5885 | continue; | |
58611fb6 JM |
5886 | r = cxx_eval_constant_expression (ctx, stmt, false, |
5887 | non_constant_p, overflow_p, | |
5888 | jump_target); | |
56632b27 JM |
5889 | if (*non_constant_p) |
5890 | break; | |
5891 | if (returns (jump_target) || breaks (jump_target)) | |
5892 | break; | |
5893 | } | |
ce36ba09 JM |
5894 | if (*jump_target && jump_target == &local_target) |
5895 | { | |
5896 | /* We aren't communicating the jump to our caller, so give up. We don't | |
5897 | need to support evaluation of jumps out of statement-exprs. */ | |
5898 | if (!ctx->quiet) | |
f9d0ca40 | 5899 | error_at (cp_expr_loc_or_input_loc (r), |
ce36ba09 JM |
5900 | "statement is not a constant expression"); |
5901 | *non_constant_p = true; | |
5902 | } | |
58611fb6 | 5903 | return r; |
56632b27 JM |
5904 | } |
5905 | ||
5906 | /* Evaluate a LOOP_EXPR for side-effects. Handles break and return | |
5907 | semantics; continue semantics are covered by cxx_eval_statement_list. */ | |
5908 | ||
5909 | static tree | |
5910 | cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t, | |
56632b27 JM |
5911 | bool *non_constant_p, bool *overflow_p, |
5912 | tree *jump_target) | |
5913 | { | |
39dce2b7 | 5914 | constexpr_ctx new_ctx = *ctx; |
8e007055 JJ |
5915 | tree local_target; |
5916 | if (!jump_target) | |
5917 | { | |
5918 | local_target = NULL_TREE; | |
5919 | jump_target = &local_target; | |
5920 | } | |
39dce2b7 | 5921 | |
43574e4f | 5922 | tree body, cond = NULL_TREE, expr = NULL_TREE; |
5ec2cd9f | 5923 | int count = 0; |
43574e4f JJ |
5924 | switch (TREE_CODE (t)) |
5925 | { | |
5926 | case LOOP_EXPR: | |
5927 | body = LOOP_EXPR_BODY (t); | |
5928 | break; | |
5929 | case DO_STMT: | |
5930 | body = DO_BODY (t); | |
5931 | cond = DO_COND (t); | |
5932 | break; | |
5933 | case WHILE_STMT: | |
5934 | body = WHILE_BODY (t); | |
5935 | cond = WHILE_COND (t); | |
5936 | count = -1; | |
5937 | break; | |
5938 | case FOR_STMT: | |
5939 | if (FOR_INIT_STMT (t)) | |
5940 | cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), /*lval*/false, | |
5941 | non_constant_p, overflow_p, jump_target); | |
5942 | if (*non_constant_p) | |
5943 | return NULL_TREE; | |
5944 | body = FOR_BODY (t); | |
5945 | cond = FOR_COND (t); | |
5946 | expr = FOR_EXPR (t); | |
5947 | count = -1; | |
5948 | break; | |
5949 | default: | |
5950 | gcc_unreachable (); | |
5951 | } | |
0ee28590 | 5952 | auto_vec<tree, 10> save_exprs; |
43574e4f | 5953 | new_ctx.save_exprs = &save_exprs; |
d259b234 | 5954 | do |
56632b27 | 5955 | { |
43574e4f JJ |
5956 | if (count != -1) |
5957 | { | |
5958 | if (body) | |
5959 | cxx_eval_constant_expression (&new_ctx, body, /*lval*/false, | |
5960 | non_constant_p, overflow_p, | |
5961 | jump_target); | |
5962 | if (breaks (jump_target)) | |
5963 | { | |
5964 | *jump_target = NULL_TREE; | |
5965 | break; | |
5966 | } | |
39dce2b7 | 5967 | |
43574e4f JJ |
5968 | if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target)) |
5969 | *jump_target = NULL_TREE; | |
5970 | ||
5971 | if (expr) | |
5972 | cxx_eval_constant_expression (&new_ctx, expr, /*lval*/false, | |
5973 | non_constant_p, overflow_p, | |
5974 | jump_target); | |
5975 | } | |
5976 | ||
5977 | if (cond) | |
5978 | { | |
5979 | tree res | |
5980 | = cxx_eval_constant_expression (&new_ctx, cond, /*lval*/false, | |
5981 | non_constant_p, overflow_p, | |
5982 | jump_target); | |
5983 | if (res) | |
5984 | { | |
5985 | if (verify_constant (res, ctx->quiet, non_constant_p, | |
5986 | overflow_p)) | |
5987 | break; | |
5988 | if (integer_zerop (res)) | |
5989 | break; | |
5990 | } | |
5991 | else | |
5992 | gcc_assert (*jump_target); | |
5993 | } | |
39dce2b7 | 5994 | |
ee1de08d | 5995 | /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */ |
3f207ab3 | 5996 | for (tree save_expr : save_exprs) |
8e007055 | 5997 | ctx->global->values.remove (save_expr); |
0ee28590 | 5998 | save_exprs.truncate (0); |
43574e4f | 5999 | |
5ec2cd9f JM |
6000 | if (++count >= constexpr_loop_limit) |
6001 | { | |
6002 | if (!ctx->quiet) | |
f9d0ca40 | 6003 | error_at (cp_expr_loc_or_input_loc (t), |
84fa214d | 6004 | "%<constexpr%> loop iteration count exceeds limit of %d " |
a3f9f006 | 6005 | "(use %<-fconstexpr-loop-limit=%> to increase the limit)", |
5ec2cd9f JM |
6006 | constexpr_loop_limit); |
6007 | *non_constant_p = true; | |
6008 | break; | |
6009 | } | |
56632b27 | 6010 | } |
4b390698 JJ |
6011 | while (!returns (jump_target) |
6012 | && !breaks (jump_target) | |
43574e4f JJ |
6013 | && !continues (jump_target) |
6014 | && (!switches (jump_target) || count == 0) | |
4b390698 | 6015 | && !*non_constant_p); |
d259b234 | 6016 | |
ee1de08d | 6017 | /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */ |
3f207ab3 | 6018 | for (tree save_expr : save_exprs) |
8e007055 | 6019 | ctx->global->values.remove (save_expr); |
39dce2b7 | 6020 | |
56632b27 JM |
6021 | return NULL_TREE; |
6022 | } | |
6023 | ||
6024 | /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump | |
6025 | semantics. */ | |
6026 | ||
6027 | static tree | |
6028 | cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t, | |
56632b27 JM |
6029 | bool *non_constant_p, bool *overflow_p, |
6030 | tree *jump_target) | |
6031 | { | |
43574e4f JJ |
6032 | tree cond |
6033 | = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t); | |
2b3ab879 | 6034 | cond = cxx_eval_constant_expression (ctx, cond, false, |
5a804683 | 6035 | non_constant_p, overflow_p); |
56632b27 JM |
6036 | VERIFY_CONSTANT (cond); |
6037 | *jump_target = cond; | |
6038 | ||
43574e4f JJ |
6039 | tree body |
6040 | = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t); | |
4b390698 JJ |
6041 | constexpr_ctx new_ctx = *ctx; |
6042 | constexpr_switch_state css = css_default_not_seen; | |
6043 | new_ctx.css_state = &css; | |
6044 | cxx_eval_constant_expression (&new_ctx, body, false, | |
6045 | non_constant_p, overflow_p, jump_target); | |
6046 | if (switches (jump_target) && css == css_default_seen) | |
6047 | { | |
6048 | /* If the SWITCH_EXPR body has default: label, process it once again, | |
6049 | this time instructing label_matches to return true for default: | |
6050 | label on switches (jump_target). */ | |
6051 | css = css_default_processing; | |
6052 | cxx_eval_constant_expression (&new_ctx, body, false, | |
6053 | non_constant_p, overflow_p, jump_target); | |
6054 | } | |
56632b27 JM |
6055 | if (breaks (jump_target) || switches (jump_target)) |
6056 | *jump_target = NULL_TREE; | |
6057 | return NULL_TREE; | |
6058 | } | |
6059 | ||
89262ec6 JM |
6060 | /* Find the object of TYPE under initialization in CTX. */ |
6061 | ||
6062 | static tree | |
6063 | lookup_placeholder (const constexpr_ctx *ctx, bool lval, tree type) | |
6064 | { | |
fbd603c4 | 6065 | if (!ctx) |
89262ec6 JM |
6066 | return NULL_TREE; |
6067 | ||
49a86fce PP |
6068 | /* Prefer the outermost matching object, but don't cross |
6069 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */ | |
6070 | if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor)) | |
6071 | if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type)) | |
6072 | return outer_ob; | |
6073 | ||
89262ec6 JM |
6074 | /* We could use ctx->object unconditionally, but using ctx->ctor when we |
6075 | can is a minor optimization. */ | |
fbd603c4 | 6076 | if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type)) |
89262ec6 JM |
6077 | return ctx->ctor; |
6078 | ||
fbd603c4 JM |
6079 | if (!ctx->object) |
6080 | return NULL_TREE; | |
6081 | ||
89262ec6 JM |
6082 | /* Since an object cannot have a field of its own type, we can search outward |
6083 | from ctx->object to find the unique containing object of TYPE. */ | |
6084 | tree ob = ctx->object; | |
6085 | while (ob) | |
6086 | { | |
6087 | if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type)) | |
6088 | break; | |
6089 | if (handled_component_p (ob)) | |
6090 | ob = TREE_OPERAND (ob, 0); | |
6091 | else | |
6092 | ob = NULL_TREE; | |
6093 | } | |
6094 | ||
6095 | return ob; | |
6096 | } | |
6097 | ||
7c814975 MP |
6098 | /* Complain about an attempt to evaluate inline assembly. */ |
6099 | ||
6100 | static void | |
6101 | inline_asm_in_constexpr_error (location_t loc) | |
6102 | { | |
6103 | auto_diagnostic_group d; | |
6104 | error_at (loc, "inline assembly is not a constant expression"); | |
6105 | inform (loc, "only unevaluated inline assembly is allowed in a " | |
b04445d4 | 6106 | "%<constexpr%> function in C++20"); |
7c814975 MP |
6107 | } |
6108 | ||
76b75018 JM |
6109 | /* We're getting the constant value of DECL in a manifestly constant-evaluated |
6110 | context; maybe complain about that. */ | |
6111 | ||
6112 | static void | |
6113 | maybe_warn_about_constant_value (location_t loc, tree decl) | |
6114 | { | |
6115 | static bool explained = false; | |
6116 | if (cxx_dialect >= cxx17 | |
6117 | && warn_interference_size | |
00f34291 | 6118 | && !OPTION_SET_P (param_destruct_interfere_size) |
76b75018 JM |
6119 | && DECL_CONTEXT (decl) == std_node |
6120 | && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size") | |
6121 | && (LOCATION_FILE (input_location) != main_input_filename | |
6122 | || module_exporting_p ()) | |
6123 | && warning_at (loc, OPT_Winterference_size, "use of %qD", decl) | |
6124 | && !explained) | |
6125 | { | |
6126 | explained = true; | |
6127 | inform (loc, "its value can vary between compiler versions or " | |
6128 | "with different %<-mtune%> or %<-mcpu%> flags"); | |
6129 | inform (loc, "if this use is part of a public ABI, change it to " | |
6130 | "instead use a constant variable you define"); | |
6131 | inform (loc, "the default value for the current CPU tuning " | |
6132 | "is %d bytes", param_destruct_interfere_size); | |
6133 | inform (loc, "you can stabilize this value with %<--param " | |
6134 | "hardware_destructive_interference_size=%d%>, or disable " | |
6135 | "this warning with %<-Wno-interference-size%>", | |
6136 | param_destruct_interfere_size); | |
6137 | } | |
6138 | } | |
6139 | ||
2d76680f PC |
6140 | /* Attempt to reduce the expression T to a constant value. |
6141 | On failure, issue diagnostic and return error_mark_node. */ | |
6142 | /* FIXME unify with c_fully_fold */ | |
60813a46 | 6143 | /* FIXME overflow_p is too global */ |
2d76680f PC |
6144 | |
6145 | static tree | |
3e605b20 | 6146 | cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, |
92a596e8 | 6147 | bool lval, |
56632b27 | 6148 | bool *non_constant_p, bool *overflow_p, |
3075affd | 6149 | tree *jump_target /* = NULL */) |
2d76680f | 6150 | { |
4b390698 JJ |
6151 | if (jump_target && *jump_target) |
6152 | { | |
6153 | /* If we are jumping, ignore all statements/expressions except those | |
6154 | that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */ | |
6155 | switch (TREE_CODE (t)) | |
6156 | { | |
6157 | case BIND_EXPR: | |
6158 | case STATEMENT_LIST: | |
6159 | case LOOP_EXPR: | |
6160 | case COND_EXPR: | |
43574e4f JJ |
6161 | case IF_STMT: |
6162 | case DO_STMT: | |
6163 | case WHILE_STMT: | |
6164 | case FOR_STMT: | |
4b390698 JJ |
6165 | break; |
6166 | case LABEL_EXPR: | |
6167 | case CASE_LABEL_EXPR: | |
6168 | if (label_matches (ctx, jump_target, t)) | |
6169 | /* Found it. */ | |
6170 | *jump_target = NULL_TREE; | |
6171 | return NULL_TREE; | |
6172 | default: | |
6173 | return NULL_TREE; | |
6174 | } | |
6175 | } | |
3075affd | 6176 | if (error_operand_p (t)) |
2d76680f PC |
6177 | { |
6178 | *non_constant_p = true; | |
6179 | return t; | |
6180 | } | |
7a649ef5 | 6181 | |
6821245b JM |
6182 | location_t loc = cp_expr_loc_or_input_loc (t); |
6183 | ||
7a649ef5 JM |
6184 | STRIP_ANY_LOCATION_WRAPPER (t); |
6185 | ||
2d76680f PC |
6186 | if (CONSTANT_CLASS_P (t)) |
6187 | { | |
61637db3 JJ |
6188 | if (TREE_OVERFLOW (t)) |
6189 | { | |
6190 | if (!ctx->quiet) | |
6191 | permerror (input_location, "overflow in constant expression"); | |
6192 | if (!flag_permissive || ctx->quiet) | |
6193 | *overflow_p = true; | |
6194 | } | |
8bada5cd MS |
6195 | |
6196 | if (TREE_CODE (t) == INTEGER_CST | |
9f613f06 | 6197 | && TYPE_PTR_P (TREE_TYPE (t)) |
f45610a4 JJ |
6198 | /* INTEGER_CST with pointer-to-method type is only used |
6199 | for a virtual method in a pointer to member function. | |
6200 | Don't reject those. */ | |
6201 | && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE | |
8bada5cd MS |
6202 | && !integer_zerop (t)) |
6203 | { | |
6204 | if (!ctx->quiet) | |
6205 | error ("value %qE of type %qT is not a constant expression", | |
6206 | t, TREE_TYPE (t)); | |
6207 | *non_constant_p = true; | |
6208 | } | |
6209 | ||
2d76680f PC |
6210 | return t; |
6211 | } | |
2d76680f | 6212 | |
a15ffa22 | 6213 | /* Avoid excessively long constexpr evaluations. */ |
8e007055 | 6214 | if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit) |
a15ffa22 JJ |
6215 | { |
6216 | if (!ctx->quiet) | |
6821245b | 6217 | error_at (loc, |
a15ffa22 | 6218 | "%<constexpr%> evaluation operation count exceeds limit of " |
a9c697b8 | 6219 | "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)", |
a15ffa22 | 6220 | constexpr_ops_limit); |
8e007055 | 6221 | ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT); |
a15ffa22 JJ |
6222 | *non_constant_p = true; |
6223 | return t; | |
6224 | } | |
6225 | ||
7a649ef5 JM |
6226 | constexpr_ctx new_ctx; |
6227 | tree r = t; | |
6228 | ||
8bada5cd MS |
6229 | tree_code tcode = TREE_CODE (t); |
6230 | switch (tcode) | |
2d76680f | 6231 | { |
60813a46 | 6232 | case RESULT_DECL: |
92a596e8 | 6233 | if (lval) |
60813a46 JM |
6234 | return t; |
6235 | /* We ask for an rvalue for the RESULT_DECL when indirecting | |
bf66b9b4 AH |
6236 | through an invisible reference, or in named return value |
6237 | optimization. */ | |
8e007055 | 6238 | if (tree *p = ctx->global->values.get (t)) |
1efb1dc2 MP |
6239 | return *p; |
6240 | else | |
6241 | { | |
6242 | if (!ctx->quiet) | |
6243 | error ("%qE is not a constant expression", t); | |
6244 | *non_constant_p = true; | |
6245 | } | |
6246 | break; | |
60813a46 | 6247 | |
2d76680f | 6248 | case VAR_DECL: |
70f40fea | 6249 | if (DECL_HAS_VALUE_EXPR_P (t)) |
c59fa7ea JM |
6250 | { |
6251 | if (is_normal_capture_proxy (t) | |
6252 | && current_function_decl == DECL_CONTEXT (t)) | |
6253 | { | |
6254 | /* Function parms aren't constexpr within the function | |
6255 | definition, so don't try to look at the closure. But if the | |
6256 | captured variable is constant, try to evaluate it directly. */ | |
6257 | r = DECL_CAPTURED_VARIABLE (t); | |
6258 | tree type = TREE_TYPE (t); | |
6259 | if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r))) | |
6260 | { | |
6261 | /* Adjust r to match the reference-ness of t. */ | |
6262 | if (TYPE_REF_P (type)) | |
6263 | r = build_address (r); | |
6264 | else | |
6265 | r = convert_from_reference (r); | |
6266 | } | |
6267 | } | |
6268 | else | |
6269 | r = DECL_VALUE_EXPR (t); | |
6270 | return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, | |
6271 | overflow_p); | |
6272 | } | |
191816a3 | 6273 | /* fall through */ |
7c83622c JM |
6274 | case CONST_DECL: |
6275 | /* We used to not check lval for CONST_DECL, but darwin.c uses | |
6276 | CONST_DECL for aggregate constants. */ | |
92a596e8 | 6277 | if (lval) |
2d76680f | 6278 | return t; |
ef529765 JM |
6279 | else if (t == ctx->object) |
6280 | return ctx->ctor; | |
6281 | if (VAR_P (t)) | |
6282 | if (tree *p = ctx->global->values.get (t)) | |
6283 | if (*p != NULL_TREE) | |
6284 | { | |
6285 | r = *p; | |
6286 | break; | |
6287 | } | |
76b75018 JM |
6288 | if (ctx->manifestly_const_eval) |
6289 | maybe_warn_about_constant_value (loc, t); | |
f4fce183 | 6290 | if (COMPLETE_TYPE_P (TREE_TYPE (t)) |
dbcd32f8 | 6291 | && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
7dc2b4a2 JM |
6292 | { |
6293 | /* If the class is empty, we aren't actually loading anything. */ | |
6294 | r = build_constructor (TREE_TYPE (t), NULL); | |
6295 | TREE_CONSTANT (r) = true; | |
6296 | } | |
6297 | else if (ctx->strict) | |
8c00059c | 6298 | r = decl_really_constant_value (t, /*unshare_p=*/false); |
69eb4fde | 6299 | else |
8c00059c | 6300 | r = decl_constant_value (t, /*unshare_p=*/false); |
2d76680f PC |
6301 | if (TREE_CODE (r) == TARGET_EXPR |
6302 | && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR) | |
6303 | r = TARGET_EXPR_INITIAL (r); | |
2d76680f PC |
6304 | if (DECL_P (r)) |
6305 | { | |
2b3ab879 | 6306 | if (!ctx->quiet) |
6821245b | 6307 | non_const_var_error (loc, r); |
2d76680f PC |
6308 | *non_constant_p = true; |
6309 | } | |
6310 | break; | |
6311 | ||
96a95ac1 AO |
6312 | case DEBUG_BEGIN_STMT: |
6313 | /* ??? It might be nice to retain this information somehow, so | |
6314 | as to be able to step into a constexpr function call. */ | |
6315 | /* Fall through. */ | |
6316 | ||
2d76680f PC |
6317 | case FUNCTION_DECL: |
6318 | case TEMPLATE_DECL: | |
6319 | case LABEL_DECL: | |
56632b27 JM |
6320 | case LABEL_EXPR: |
6321 | case CASE_LABEL_EXPR: | |
4b390698 | 6322 | case PREDICT_EXPR: |
2d76680f PC |
6323 | return t; |
6324 | ||
6325 | case PARM_DECL: | |
9f613f06 | 6326 | if (lval && !TYPE_REF_P (TREE_TYPE (t))) |
60813a46 | 6327 | /* glvalue use. */; |
8e007055 | 6328 | else if (tree *p = ctx->global->values.get (r)) |
60813a46 | 6329 | r = *p; |
92a596e8 | 6330 | else if (lval) |
2d76680f | 6331 | /* Defer in case this is only used for its type. */; |
1c2f613f | 6332 | else if (COMPLETE_TYPE_P (TREE_TYPE (t)) |
dbcd32f8 | 6333 | && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
052beba4 JM |
6334 | { |
6335 | /* If the class is empty, we aren't actually loading anything. */ | |
6336 | r = build_constructor (TREE_TYPE (t), NULL); | |
6337 | TREE_CONSTANT (r) = true; | |
6338 | } | |
2d76680f PC |
6339 | else |
6340 | { | |
2b3ab879 | 6341 | if (!ctx->quiet) |
2d76680f PC |
6342 | error ("%qE is not a constant expression", t); |
6343 | *non_constant_p = true; | |
6344 | } | |
6345 | break; | |
6346 | ||
6347 | case CALL_EXPR: | |
6348 | case AGGR_INIT_EXPR: | |
92a596e8 | 6349 | r = cxx_eval_call_expression (ctx, t, lval, |
2d76680f PC |
6350 | non_constant_p, overflow_p); |
6351 | break; | |
6352 | ||
60813a46 JM |
6353 | case DECL_EXPR: |
6354 | { | |
6355 | r = DECL_EXPR_DECL (t); | |
43574e4f JJ |
6356 | if (TREE_CODE (r) == USING_DECL) |
6357 | { | |
6358 | r = void_node; | |
6359 | break; | |
6360 | } | |
8892d532 JJ |
6361 | |
6362 | if (VAR_P (r) | |
6363 | && (TREE_STATIC (r) || CP_DECL_THREAD_LOCAL_P (r)) | |
6364 | /* Allow __FUNCTION__ etc. */ | |
6365 | && !DECL_ARTIFICIAL (r)) | |
6366 | { | |
6367 | gcc_assert (cxx_dialect >= cxx23); | |
6368 | if (!ctx->quiet) | |
6369 | { | |
6370 | if (CP_DECL_THREAD_LOCAL_P (r)) | |
6371 | error_at (loc, "control passes through declaration of %qD " | |
6372 | "with thread storage duration", r); | |
6373 | else | |
6374 | error_at (loc, "control passes through declaration of %qD " | |
6375 | "with static storage duration", r); | |
6376 | } | |
6377 | *non_constant_p = true; | |
6378 | break; | |
6379 | } | |
6380 | ||
60813a46 JM |
6381 | if (AGGREGATE_TYPE_P (TREE_TYPE (r)) |
6382 | || VECTOR_TYPE_P (TREE_TYPE (r))) | |
6383 | { | |
6384 | new_ctx = *ctx; | |
6385 | new_ctx.object = r; | |
6386 | new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL); | |
e8c48716 | 6387 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; |
8e007055 | 6388 | ctx->global->values.put (r, new_ctx.ctor); |
60813a46 JM |
6389 | ctx = &new_ctx; |
6390 | } | |
6391 | ||
6392 | if (tree init = DECL_INITIAL (r)) | |
6393 | { | |
6394 | init = cxx_eval_constant_expression (ctx, init, | |
2b3ab879 | 6395 | false, |
5a804683 | 6396 | non_constant_p, overflow_p); |
7f0e23e9 | 6397 | /* Don't share a CONSTRUCTOR that might be changed. */ |
0146e25f | 6398 | init = unshare_constructor (init); |
04e1749c MP |
6399 | /* Remember that a constant object's constructor has already |
6400 | run. */ | |
6401 | if (CLASS_TYPE_P (TREE_TYPE (r)) | |
6402 | && CP_TYPE_CONST_P (TREE_TYPE (r))) | |
6403 | TREE_READONLY (init) = true; | |
8e007055 | 6404 | ctx->global->values.put (r, init); |
60813a46 JM |
6405 | } |
6406 | else if (ctx == &new_ctx) | |
6407 | /* We gave it a CONSTRUCTOR above. */; | |
6408 | else | |
8e007055 | 6409 | ctx->global->values.put (r, NULL_TREE); |
60813a46 JM |
6410 | } |
6411 | break; | |
6412 | ||
2d76680f | 6413 | case TARGET_EXPR: |
595f1b12 JM |
6414 | { |
6415 | tree type = TREE_TYPE (t); | |
6416 | ||
6417 | if (!literal_type_p (type)) | |
6418 | { | |
6419 | if (!ctx->quiet) | |
6420 | { | |
6421 | auto_diagnostic_group d; | |
6422 | error ("temporary of non-literal type %qT in a " | |
6423 | "constant expression", type); | |
6424 | explain_non_literal_class (type); | |
6425 | } | |
6426 | *non_constant_p = true; | |
6427 | break; | |
6428 | } | |
6429 | gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t)); | |
6430 | /* Avoid evaluating a TARGET_EXPR more than once. */ | |
6431 | tree slot = TARGET_EXPR_SLOT (t); | |
6432 | if (tree *p = ctx->global->values.get (slot)) | |
6433 | { | |
6434 | if (lval) | |
6435 | return slot; | |
6436 | r = *p; | |
6437 | break; | |
6438 | } | |
595f1b12 JM |
6439 | if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))) |
6440 | { | |
6441 | /* We're being expanded without an explicit target, so start | |
6442 | initializing a new object; expansion with an explicit target | |
6443 | strips the TARGET_EXPR before we get here. */ | |
6444 | new_ctx = *ctx; | |
49a86fce PP |
6445 | /* Link CTX to NEW_CTX so that lookup_placeholder can resolve |
6446 | any PLACEHOLDER_EXPR within the initializer that refers to the | |
6447 | former object under construction. */ | |
6448 | new_ctx.parent = ctx; | |
595f1b12 JM |
6449 | new_ctx.ctor = build_constructor (type, NULL); |
6450 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; | |
6451 | new_ctx.object = slot; | |
6452 | ctx->global->values.put (new_ctx.object, new_ctx.ctor); | |
6453 | ctx = &new_ctx; | |
6454 | } | |
6455 | /* Pass false for 'lval' because this indicates | |
6456 | initialization of a temporary. */ | |
6457 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), | |
6458 | false, | |
6459 | non_constant_p, overflow_p); | |
6460 | if (*non_constant_p) | |
ee1de08d | 6461 | break; |
595f1b12 JM |
6462 | /* Adjust the type of the result to the type of the temporary. */ |
6463 | r = adjust_temp_type (type, r); | |
6464 | if (TARGET_EXPR_CLEANUP (t) && !CLEANUP_EH_ONLY (t)) | |
6465 | ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t)); | |
6466 | r = unshare_constructor (r); | |
6467 | ctx->global->values.put (slot, r); | |
6468 | if (ctx->save_exprs) | |
6469 | ctx->save_exprs->safe_push (slot); | |
6470 | if (lval) | |
6471 | return slot; | |
6472 | } | |
2d76680f PC |
6473 | break; |
6474 | ||
60813a46 | 6475 | case INIT_EXPR: |
60813a46 | 6476 | case MODIFY_EXPR: |
4b390698 | 6477 | gcc_assert (jump_target == NULL || *jump_target == NULL_TREE); |
92a596e8 | 6478 | r = cxx_eval_store_expression (ctx, t, lval, |
60813a46 JM |
6479 | non_constant_p, overflow_p); |
6480 | break; | |
6481 | ||
2d76680f | 6482 | case SCOPE_REF: |
3e605b20 | 6483 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
92a596e8 | 6484 | lval, |
5a804683 | 6485 | non_constant_p, overflow_p); |
2d76680f PC |
6486 | break; |
6487 | ||
6488 | case RETURN_EXPR: | |
75e0295b MP |
6489 | if (TREE_OPERAND (t, 0) != NULL_TREE) |
6490 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), | |
6491 | lval, | |
6492 | non_constant_p, overflow_p); | |
43574e4f JJ |
6493 | /* FALLTHRU */ |
6494 | case BREAK_STMT: | |
6495 | case CONTINUE_STMT: | |
019e0ae8 MP |
6496 | if (jump_target) |
6497 | *jump_target = t; | |
6498 | else | |
6499 | { | |
6500 | /* Can happen with ({ return true; }) && false; passed to | |
6501 | maybe_constant_value. There is nothing to jump over in this | |
6502 | case, and the bug will be diagnosed later. */ | |
6503 | gcc_assert (ctx->quiet); | |
6504 | *non_constant_p = true; | |
6505 | } | |
56632b27 JM |
6506 | break; |
6507 | ||
cabaf50a JM |
6508 | case SAVE_EXPR: |
6509 | /* Avoid evaluating a SAVE_EXPR more than once. */ | |
8e007055 | 6510 | if (tree *p = ctx->global->values.get (t)) |
cabaf50a JM |
6511 | r = *p; |
6512 | else | |
6513 | { | |
12d9ce19 | 6514 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false, |
cabaf50a | 6515 | non_constant_p, overflow_p); |
0e0ffbfc JJ |
6516 | if (*non_constant_p) |
6517 | break; | |
8e007055 | 6518 | ctx->global->values.put (t, r); |
39dce2b7 | 6519 | if (ctx->save_exprs) |
0ee28590 | 6520 | ctx->save_exprs->safe_push (t); |
cabaf50a JM |
6521 | } |
6522 | break; | |
6523 | ||
2d76680f | 6524 | case TRY_CATCH_EXPR: |
ee1de08d JJ |
6525 | if (TREE_OPERAND (t, 0) == NULL_TREE) |
6526 | { | |
6527 | r = void_node; | |
6528 | break; | |
6529 | } | |
6530 | /* FALLTHRU */ | |
6531 | case NON_LVALUE_EXPR: | |
769430b2 | 6532 | case TRY_BLOCK: |
2d76680f | 6533 | case MUST_NOT_THROW_EXPR: |
60813a46 JM |
6534 | case EXPR_STMT: |
6535 | case EH_SPEC_BLOCK: | |
3e605b20 | 6536 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
92a596e8 | 6537 | lval, |
56632b27 JM |
6538 | non_constant_p, overflow_p, |
6539 | jump_target); | |
2d76680f PC |
6540 | break; |
6541 | ||
ee1de08d JJ |
6542 | case CLEANUP_POINT_EXPR: |
6543 | { | |
6544 | auto_vec<tree, 2> cleanups; | |
6545 | vec<tree> *prev_cleanups = ctx->global->cleanups; | |
6546 | ctx->global->cleanups = &cleanups; | |
6547 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), | |
6548 | lval, | |
6549 | non_constant_p, overflow_p, | |
6550 | jump_target); | |
6551 | ctx->global->cleanups = prev_cleanups; | |
6552 | unsigned int i; | |
6553 | tree cleanup; | |
6554 | /* Evaluate the cleanups. */ | |
6555 | FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup) | |
6556 | cxx_eval_constant_expression (ctx, cleanup, false, | |
fc531c2e | 6557 | non_constant_p, overflow_p); |
ee1de08d JJ |
6558 | } |
6559 | break; | |
6560 | ||
769430b2 JM |
6561 | case TRY_FINALLY_EXPR: |
6562 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, | |
6563 | non_constant_p, overflow_p, | |
6564 | jump_target); | |
6565 | if (!*non_constant_p) | |
6566 | /* Also evaluate the cleanup. */ | |
6567 | cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true, | |
fc531c2e | 6568 | non_constant_p, overflow_p); |
769430b2 JM |
6569 | break; |
6570 | ||
43574e4f | 6571 | case CLEANUP_STMT: |
fc531c2e JJ |
6572 | r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval, |
6573 | non_constant_p, overflow_p, | |
6574 | jump_target); | |
6575 | if (!CLEANUP_EH_ONLY (t) && !*non_constant_p) | |
6576 | { | |
6577 | iloc_sentinel ils (loc); | |
6578 | /* Also evaluate the cleanup. */ | |
6579 | cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), true, | |
6580 | non_constant_p, overflow_p); | |
6581 | } | |
43574e4f JJ |
6582 | break; |
6583 | ||
2d76680f PC |
6584 | /* These differ from cxx_eval_unary_expression in that this doesn't |
6585 | check for a constant operand or result; an address can be | |
6586 | constant without its operand being, and vice versa. */ | |
cda0a029 | 6587 | case MEM_REF: |
2d76680f | 6588 | case INDIRECT_REF: |
92a596e8 | 6589 | r = cxx_eval_indirect_ref (ctx, t, lval, |
2d76680f PC |
6590 | non_constant_p, overflow_p); |
6591 | break; | |
6592 | ||
6593 | case ADDR_EXPR: | |
6594 | { | |
6595 | tree oldop = TREE_OPERAND (t, 0); | |
3e605b20 | 6596 | tree op = cxx_eval_constant_expression (ctx, oldop, |
92a596e8 | 6597 | /*lval*/true, |
5a804683 | 6598 | non_constant_p, overflow_p); |
2d76680f PC |
6599 | /* Don't VERIFY_CONSTANT here. */ |
6600 | if (*non_constant_p) | |
6601 | return t; | |
f2acb8ad | 6602 | gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR); |
2d76680f PC |
6603 | /* This function does more aggressive folding than fold itself. */ |
6604 | r = build_fold_addr_expr_with_type (op, TREE_TYPE (t)); | |
6605 | if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop) | |
7a649ef5 JM |
6606 | { |
6607 | ggc_free (r); | |
6608 | return t; | |
6609 | } | |
2d76680f PC |
6610 | break; |
6611 | } | |
6612 | ||
6613 | case REALPART_EXPR: | |
6614 | case IMAGPART_EXPR: | |
87713c6a JJ |
6615 | if (lval) |
6616 | { | |
6617 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, | |
6618 | non_constant_p, overflow_p); | |
6619 | if (r == error_mark_node) | |
6620 | ; | |
6621 | else if (r == TREE_OPERAND (t, 0)) | |
6622 | r = t; | |
6623 | else | |
6624 | r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r); | |
6625 | break; | |
6626 | } | |
6627 | /* FALLTHRU */ | |
2d76680f PC |
6628 | case CONJ_EXPR: |
6629 | case FIX_TRUNC_EXPR: | |
6630 | case FLOAT_EXPR: | |
6631 | case NEGATE_EXPR: | |
6632 | case ABS_EXPR: | |
e28fadbc | 6633 | case ABSU_EXPR: |
2d76680f PC |
6634 | case BIT_NOT_EXPR: |
6635 | case TRUTH_NOT_EXPR: | |
6636 | case FIXED_CONVERT_EXPR: | |
92a596e8 | 6637 | r = cxx_eval_unary_expression (ctx, t, lval, |
2d76680f PC |
6638 | non_constant_p, overflow_p); |
6639 | break; | |
6640 | ||
6641 | case SIZEOF_EXPR: | |
99e764a2 MP |
6642 | r = fold_sizeof_expr (t); |
6643 | /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR, | |
6644 | which could lead to an infinite recursion. */ | |
6645 | if (TREE_CODE (r) != SIZEOF_EXPR) | |
6646 | r = cxx_eval_constant_expression (ctx, r, lval, | |
6647 | non_constant_p, overflow_p, | |
6648 | jump_target); | |
6649 | else | |
6650 | { | |
6651 | *non_constant_p = true; | |
6652 | gcc_assert (ctx->quiet); | |
6653 | } | |
6654 | ||
2d76680f PC |
6655 | break; |
6656 | ||
6657 | case COMPOUND_EXPR: | |
6658 | { | |
6659 | /* check_return_expr sometimes wraps a TARGET_EXPR in a | |
6660 | COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR | |
6661 | introduced by build_call_a. */ | |
6662 | tree op0 = TREE_OPERAND (t, 0); | |
6663 | tree op1 = TREE_OPERAND (t, 1); | |
6664 | STRIP_NOPS (op1); | |
6665 | if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) | |
6666 | || TREE_CODE (op1) == EMPTY_CLASS_EXPR) | |
2b3ab879 | 6667 | r = cxx_eval_constant_expression (ctx, op0, |
92a596e8 | 6668 | lval, non_constant_p, overflow_p, |
56632b27 | 6669 | jump_target); |
2d76680f PC |
6670 | else |
6671 | { | |
6672 | /* Check that the LHS is constant and then discard it. */ | |
2b3ab879 | 6673 | cxx_eval_constant_expression (ctx, op0, |
12d9ce19 | 6674 | true, non_constant_p, overflow_p, |
56632b27 | 6675 | jump_target); |
06ec22b7 JM |
6676 | if (*non_constant_p) |
6677 | return t; | |
2d76680f | 6678 | op1 = TREE_OPERAND (t, 1); |
2b3ab879 | 6679 | r = cxx_eval_constant_expression (ctx, op1, |
92a596e8 | 6680 | lval, non_constant_p, overflow_p, |
56632b27 | 6681 | jump_target); |
2d76680f PC |
6682 | } |
6683 | } | |
6684 | break; | |
6685 | ||
6686 | case POINTER_PLUS_EXPR: | |
1af4ebf5 | 6687 | case POINTER_DIFF_EXPR: |
2d76680f PC |
6688 | case PLUS_EXPR: |
6689 | case MINUS_EXPR: | |
6690 | case MULT_EXPR: | |
6691 | case TRUNC_DIV_EXPR: | |
6692 | case CEIL_DIV_EXPR: | |
6693 | case FLOOR_DIV_EXPR: | |
6694 | case ROUND_DIV_EXPR: | |
6695 | case TRUNC_MOD_EXPR: | |
6696 | case CEIL_MOD_EXPR: | |
6697 | case ROUND_MOD_EXPR: | |
6698 | case RDIV_EXPR: | |
6699 | case EXACT_DIV_EXPR: | |
6700 | case MIN_EXPR: | |
6701 | case MAX_EXPR: | |
6702 | case LSHIFT_EXPR: | |
6703 | case RSHIFT_EXPR: | |
81fa6d73 JJ |
6704 | case LROTATE_EXPR: |
6705 | case RROTATE_EXPR: | |
2d76680f PC |
6706 | case BIT_IOR_EXPR: |
6707 | case BIT_XOR_EXPR: | |
6708 | case BIT_AND_EXPR: | |
6709 | case TRUTH_XOR_EXPR: | |
6710 | case LT_EXPR: | |
6711 | case LE_EXPR: | |
6712 | case GT_EXPR: | |
6713 | case GE_EXPR: | |
6714 | case EQ_EXPR: | |
6715 | case NE_EXPR: | |
b7689b96 | 6716 | case SPACESHIP_EXPR: |
2d76680f PC |
6717 | case UNORDERED_EXPR: |
6718 | case ORDERED_EXPR: | |
6719 | case UNLT_EXPR: | |
6720 | case UNLE_EXPR: | |
6721 | case UNGT_EXPR: | |
6722 | case UNGE_EXPR: | |
6723 | case UNEQ_EXPR: | |
6724 | case LTGT_EXPR: | |
6725 | case RANGE_EXPR: | |
6726 | case COMPLEX_EXPR: | |
92a596e8 | 6727 | r = cxx_eval_binary_expression (ctx, t, lval, |
2d76680f PC |
6728 | non_constant_p, overflow_p); |
6729 | break; | |
6730 | ||
6731 | /* fold can introduce non-IF versions of these; still treat them as | |
6732 | short-circuiting. */ | |
6733 | case TRUTH_AND_EXPR: | |
6734 | case TRUTH_ANDIF_EXPR: | |
3e605b20 | 6735 | r = cxx_eval_logical_expression (ctx, t, boolean_false_node, |
2d76680f | 6736 | boolean_true_node, |
92a596e8 | 6737 | lval, |
2d76680f PC |
6738 | non_constant_p, overflow_p); |
6739 | break; | |
6740 | ||
6741 | case TRUTH_OR_EXPR: | |
6742 | case TRUTH_ORIF_EXPR: | |
3e605b20 | 6743 | r = cxx_eval_logical_expression (ctx, t, boolean_true_node, |
2d76680f | 6744 | boolean_false_node, |
92a596e8 | 6745 | lval, |
2d76680f PC |
6746 | non_constant_p, overflow_p); |
6747 | break; | |
6748 | ||
6749 | case ARRAY_REF: | |
92a596e8 | 6750 | r = cxx_eval_array_reference (ctx, t, lval, |
2d76680f PC |
6751 | non_constant_p, overflow_p); |
6752 | break; | |
6753 | ||
6754 | case COMPONENT_REF: | |
6755 | if (is_overloaded_fn (t)) | |
6756 | { | |
6757 | /* We can only get here in checking mode via | |
6758 | build_non_dependent_expr, because any expression that | |
6759 | calls or takes the address of the function will have | |
6760 | pulled a FUNCTION_DECL out of the COMPONENT_REF. */ | |
2b3ab879 | 6761 | gcc_checking_assert (ctx->quiet || errorcount); |
2d76680f PC |
6762 | *non_constant_p = true; |
6763 | return t; | |
6764 | } | |
92a596e8 | 6765 | r = cxx_eval_component_reference (ctx, t, lval, |
2d76680f PC |
6766 | non_constant_p, overflow_p); |
6767 | break; | |
6768 | ||
6769 | case BIT_FIELD_REF: | |
92a596e8 | 6770 | r = cxx_eval_bit_field_ref (ctx, t, lval, |
2d76680f PC |
6771 | non_constant_p, overflow_p); |
6772 | break; | |
6773 | ||
6774 | case COND_EXPR: | |
43574e4f | 6775 | case IF_STMT: |
4b390698 JJ |
6776 | if (jump_target && *jump_target) |
6777 | { | |
e9fa2f6d | 6778 | tree orig_jump = *jump_target; |
43574e4f JJ |
6779 | tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1)) |
6780 | ? TREE_OPERAND (t, 1) : void_node); | |
4b390698 JJ |
6781 | /* When jumping to a label, the label might be either in the |
6782 | then or else blocks, so process then block first in skipping | |
6783 | mode first, and if we are still in the skipping mode at its end, | |
6784 | process the else block too. */ | |
43574e4f JJ |
6785 | r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, |
6786 | overflow_p, jump_target); | |
e9fa2f6d MP |
6787 | /* It's possible that we found the label in the then block. But |
6788 | it could have been followed by another jumping statement, e.g. | |
6789 | say we're looking for case 1: | |
6790 | if (cond) | |
6791 | { | |
6792 | // skipped statements | |
6793 | case 1:; // clears up *jump_target | |
6794 | return 1; // and sets it to a RETURN_EXPR | |
6795 | } | |
6796 | else { ... } | |
6797 | in which case we need not go looking to the else block. | |
6798 | (goto is not allowed in a constexpr function.) */ | |
6799 | if (*jump_target == orig_jump) | |
43574e4f JJ |
6800 | { |
6801 | arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2)) | |
6802 | ? TREE_OPERAND (t, 2) : void_node); | |
6803 | r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, | |
6804 | overflow_p, jump_target); | |
6805 | } | |
4b390698 JJ |
6806 | break; |
6807 | } | |
92a596e8 | 6808 | r = cxx_eval_conditional_expression (ctx, t, lval, |
56632b27 JM |
6809 | non_constant_p, overflow_p, |
6810 | jump_target); | |
2d76680f | 6811 | break; |
f370e36d JJ |
6812 | case VEC_COND_EXPR: |
6813 | r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p, | |
6814 | overflow_p); | |
6815 | break; | |
2d76680f PC |
6816 | |
6817 | case CONSTRUCTOR: | |
35d87f01 | 6818 | if (TREE_CONSTANT (t) && reduced_constant_expression_p (t)) |
2d63bc39 JM |
6819 | { |
6820 | /* Don't re-process a constant CONSTRUCTOR, but do fold it to | |
6821 | VECTOR_CST if applicable. */ | |
ac9ec198 | 6822 | verify_constructor_flags (t); |
2d63bc39 JM |
6823 | if (TREE_CONSTANT (t)) |
6824 | return fold (t); | |
6825 | } | |
92a596e8 | 6826 | r = cxx_eval_bare_aggregate (ctx, t, lval, |
2d76680f PC |
6827 | non_constant_p, overflow_p); |
6828 | break; | |
6829 | ||
6830 | case VEC_INIT_EXPR: | |
6831 | /* We can get this in a defaulted constructor for a class with a | |
6832 | non-static data member of array type. Either the initializer will | |
6833 | be NULL, meaning default-initialization, or it will be an lvalue | |
6834 | or xvalue of the same type, meaning direct-initialization from the | |
6835 | corresponding member. */ | |
92a596e8 | 6836 | r = cxx_eval_vec_init (ctx, t, lval, |
2d76680f PC |
6837 | non_constant_p, overflow_p); |
6838 | break; | |
6839 | ||
2d76680f | 6840 | case VEC_PERM_EXPR: |
92a596e8 | 6841 | r = cxx_eval_trinary_expression (ctx, t, lval, |
2d76680f PC |
6842 | non_constant_p, overflow_p); |
6843 | break; | |
6844 | ||
7d75ea04 JJ |
6845 | case NOP_EXPR: |
6846 | if (REINTERPRET_CAST_P (t)) | |
6847 | { | |
6848 | if (!ctx->quiet) | |
6821245b | 6849 | error_at (loc, |
a9c697b8 | 6850 | "%<reinterpret_cast%> is not a constant expression"); |
7d75ea04 JJ |
6851 | *non_constant_p = true; |
6852 | return t; | |
6853 | } | |
6854 | /* FALLTHROUGH. */ | |
2d76680f PC |
6855 | case CONVERT_EXPR: |
6856 | case VIEW_CONVERT_EXPR: | |
cda0a029 | 6857 | case UNARY_PLUS_EXPR: |
2d76680f PC |
6858 | { |
6859 | tree oldop = TREE_OPERAND (t, 0); | |
cda0a029 | 6860 | |
3e605b20 | 6861 | tree op = cxx_eval_constant_expression (ctx, oldop, |
92a596e8 | 6862 | lval, |
5a804683 | 6863 | non_constant_p, overflow_p); |
2d76680f PC |
6864 | if (*non_constant_p) |
6865 | return t; | |
dcdbc004 | 6866 | tree type = TREE_TYPE (t); |
02a8575c JM |
6867 | |
6868 | if (VOID_TYPE_P (type)) | |
6869 | return void_node; | |
6870 | ||
eeb54a14 JJ |
6871 | if (TREE_CODE (t) == CONVERT_EXPR |
6872 | && ARITHMETIC_TYPE_P (type) | |
82bb6673 JJ |
6873 | && INDIRECT_TYPE_P (TREE_TYPE (op)) |
6874 | && ctx->manifestly_const_eval) | |
eeb54a14 JJ |
6875 | { |
6876 | if (!ctx->quiet) | |
6877 | error_at (loc, | |
6878 | "conversion from pointer type %qT to arithmetic type " | |
6879 | "%qT in a constant expression", TREE_TYPE (op), type); | |
6880 | *non_constant_p = true; | |
6881 | return t; | |
6882 | } | |
6883 | ||
2ffc2645 MP |
6884 | /* [expr.const]: a conversion from type cv void* to a pointer-to-object |
6885 | type cannot be part of a core constant expression as a resolution to | |
6886 | DR 1312. */ | |
6252e35c | 6887 | if (TYPE_PTROB_P (type) |
2ffc2645 MP |
6888 | && TYPE_PTR_P (TREE_TYPE (op)) |
6889 | && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op))) | |
6890 | /* Inside a call to std::construct_at or to | |
6891 | std::allocator<T>::{,de}allocate, we permit casting from void* | |
6892 | because that is compiler-generated code. */ | |
6893 | && !is_std_construct_at (ctx->call) | |
6894 | && !is_std_allocator_allocate (ctx->call)) | |
6895 | { | |
6896 | /* Likewise, don't error when casting from void* when OP is | |
6897 | &heap uninit and similar. */ | |
6898 | tree sop = tree_strip_nop_conversions (op); | |
6899 | if (TREE_CODE (sop) == ADDR_EXPR | |
6900 | && VAR_P (TREE_OPERAND (sop, 0)) | |
6901 | && DECL_ARTIFICIAL (TREE_OPERAND (sop, 0))) | |
6902 | /* OK */; | |
6903 | else | |
6904 | { | |
6905 | if (!ctx->quiet) | |
6906 | error_at (loc, "cast from %qT is not allowed", | |
6907 | TREE_TYPE (op)); | |
6908 | *non_constant_p = true; | |
6909 | return t; | |
6910 | } | |
6911 | } | |
6912 | ||
8e007055 | 6913 | if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type)) |
dcdbc004 | 6914 | op = cplus_expand_constant (op); |
7d75ea04 | 6915 | |
05bf54c3 MP |
6916 | if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR) |
6917 | { | |
7d75ea04 JJ |
6918 | if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op)) |
6919 | && !can_convert_qual (type, op)) | |
6920 | op = cplus_expand_constant (op); | |
6921 | return cp_fold_convert (type, op); | |
05bf54c3 | 6922 | } |
8bada5cd | 6923 | |
71a93b08 | 6924 | if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST) |
2d76680f | 6925 | { |
8bada5cd MS |
6926 | if (integer_zerop (op)) |
6927 | { | |
9f613f06 | 6928 | if (TYPE_REF_P (type)) |
8bada5cd MS |
6929 | { |
6930 | if (!ctx->quiet) | |
2ffc2645 | 6931 | error_at (loc, "dereferencing a null pointer"); |
8bada5cd MS |
6932 | *non_constant_p = true; |
6933 | return t; | |
6934 | } | |
8bada5cd MS |
6935 | } |
6936 | else | |
6937 | { | |
6938 | /* This detects for example: | |
6939 | reinterpret_cast<void*>(sizeof 0) | |
6940 | */ | |
6941 | if (!ctx->quiet) | |
6821245b | 6942 | error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not " |
64d6d399 | 6943 | "a constant expression", |
8bada5cd MS |
6944 | type, op); |
6945 | *non_constant_p = true; | |
6946 | return t; | |
6947 | } | |
2d76680f | 6948 | } |
7d75ea04 | 6949 | |
8e007055 JJ |
6950 | if (INDIRECT_TYPE_P (type) |
6951 | && TREE_CODE (op) == NOP_EXPR | |
6952 | && TREE_TYPE (op) == ptr_type_node | |
6953 | && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR | |
6954 | && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0)) | |
815baade JJ |
6955 | && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), |
6956 | 0)) == heap_uninit_identifier | |
6957 | || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), | |
6958 | 0)) == heap_vec_uninit_identifier)) | |
8e007055 JJ |
6959 | { |
6960 | tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0); | |
6961 | tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var)); | |
6962 | tree elt_type = TREE_TYPE (type); | |
6963 | tree cookie_size = NULL_TREE; | |
6964 | if (TREE_CODE (elt_type) == RECORD_TYPE | |
6965 | && TYPE_NAME (elt_type) == heap_identifier) | |
6966 | { | |
6967 | tree fld1 = TYPE_FIELDS (elt_type); | |
6968 | tree fld2 = DECL_CHAIN (fld1); | |
6969 | elt_type = TREE_TYPE (TREE_TYPE (fld2)); | |
6970 | cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1)); | |
6971 | } | |
815baade JJ |
6972 | DECL_NAME (var) |
6973 | = (DECL_NAME (var) == heap_uninit_identifier | |
6974 | ? heap_identifier : heap_vec_identifier); | |
8e007055 JJ |
6975 | TREE_TYPE (var) |
6976 | = build_new_constexpr_heap_type (elt_type, cookie_size, | |
6977 | var_size); | |
6978 | TREE_TYPE (TREE_OPERAND (op, 0)) | |
6979 | = build_pointer_type (TREE_TYPE (var)); | |
6980 | } | |
6981 | ||
cda0a029 | 6982 | if (op == oldop && tcode != UNARY_PLUS_EXPR) |
2d76680f PC |
6983 | /* We didn't fold at the top so we could check for ptr-int |
6984 | conversion. */ | |
6985 | return fold (t); | |
7d75ea04 | 6986 | |
02a8575c JM |
6987 | tree sop; |
6988 | ||
e4511ca2 JM |
6989 | /* Handle an array's bounds having been deduced after we built |
6990 | the wrapping expression. */ | |
6991 | if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op))) | |
6992 | r = op; | |
02a8575c JM |
6993 | else if (sop = tree_strip_nop_conversions (op), |
6994 | sop != op && (same_type_ignoring_tlq_and_bounds_p | |
6995 | (type, TREE_TYPE (sop)))) | |
6996 | r = sop; | |
e4511ca2 | 6997 | else if (tcode == UNARY_PLUS_EXPR) |
cda0a029 JM |
6998 | r = fold_convert (TREE_TYPE (t), op); |
6999 | else | |
7000 | r = fold_build1 (tcode, type, op); | |
7d75ea04 | 7001 | |
2d76680f PC |
7002 | /* Conversion of an out-of-range value has implementation-defined |
7003 | behavior; the language considers it different from arithmetic | |
7004 | overflow, which is undefined. */ | |
7005 | if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op)) | |
7006 | TREE_OVERFLOW (r) = false; | |
7007 | } | |
7008 | break; | |
7009 | ||
7010 | case EMPTY_CLASS_EXPR: | |
46fdced6 PP |
7011 | /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering |
7012 | it to an appropriate CONSTRUCTOR. */ | |
7013 | return build_constructor (TREE_TYPE (t), NULL); | |
2d76680f | 7014 | |
60813a46 | 7015 | case STATEMENT_LIST: |
56632b27 JM |
7016 | new_ctx = *ctx; |
7017 | new_ctx.ctor = new_ctx.object = NULL_TREE; | |
2b3ab879 | 7018 | return cxx_eval_statement_list (&new_ctx, t, |
56632b27 | 7019 | non_constant_p, overflow_p, jump_target); |
60813a46 JM |
7020 | |
7021 | case BIND_EXPR: | |
7022 | return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t), | |
92a596e8 | 7023 | lval, |
56632b27 JM |
7024 | non_constant_p, overflow_p, |
7025 | jump_target); | |
60813a46 | 7026 | |
2d76680f PC |
7027 | case PREINCREMENT_EXPR: |
7028 | case POSTINCREMENT_EXPR: | |
7029 | case PREDECREMENT_EXPR: | |
7030 | case POSTDECREMENT_EXPR: | |
2b3ab879 | 7031 | return cxx_eval_increment_expression (ctx, t, |
92a596e8 | 7032 | lval, non_constant_p, overflow_p); |
60813a46 JM |
7033 | |
7034 | case LAMBDA_EXPR: | |
2d76680f PC |
7035 | case NEW_EXPR: |
7036 | case VEC_NEW_EXPR: | |
7037 | case DELETE_EXPR: | |
7038 | case VEC_DELETE_EXPR: | |
7039 | case THROW_EXPR: | |
2d76680f PC |
7040 | case MODOP_EXPR: |
7041 | /* GCC internal stuff. */ | |
7042 | case VA_ARG_EXPR: | |
2d76680f PC |
7043 | case NON_DEPENDENT_EXPR: |
7044 | case BASELINK: | |
2d76680f | 7045 | case OFFSET_REF: |
2b3ab879 | 7046 | if (!ctx->quiet) |
6821245b | 7047 | error_at (loc, "expression %qE is not a constant expression", t); |
2d76680f PC |
7048 | *non_constant_p = true; |
7049 | break; | |
7050 | ||
bf8d8309 | 7051 | case OBJ_TYPE_REF: |
7ece3bd8 JM |
7052 | /* Virtual function lookup. We don't need to do anything fancy. */ |
7053 | return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t), | |
7054 | lval, non_constant_p, overflow_p); | |
bf8d8309 | 7055 | |
3e605b20 | 7056 | case PLACEHOLDER_EXPR: |
89262ec6 JM |
7057 | /* Use of the value or address of the current object. */ |
7058 | if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t))) | |
b599bf9d PP |
7059 | { |
7060 | if (TREE_CODE (ctor) == CONSTRUCTOR) | |
7061 | return ctor; | |
7062 | else | |
7063 | return cxx_eval_constant_expression (ctx, ctor, lval, | |
7064 | non_constant_p, overflow_p); | |
7065 | } | |
89262ec6 JM |
7066 | /* A placeholder without a referent. We can get here when |
7067 | checking whether NSDMIs are noexcept, or in massage_init_elt; | |
7068 | just say it's non-constant for now. */ | |
7069 | gcc_assert (ctx->quiet); | |
7070 | *non_constant_p = true; | |
3e605b20 JM |
7071 | break; |
7072 | ||
06ec22b7 JM |
7073 | case EXIT_EXPR: |
7074 | { | |
7075 | tree cond = TREE_OPERAND (t, 0); | |
7076 | cond = cxx_eval_constant_expression (ctx, cond, /*lval*/false, | |
7077 | non_constant_p, overflow_p); | |
7078 | VERIFY_CONSTANT (cond); | |
7079 | if (integer_nonzerop (cond)) | |
7080 | *jump_target = t; | |
7081 | } | |
7082 | break; | |
7083 | ||
60813a46 | 7084 | case GOTO_EXPR: |
8892d532 JJ |
7085 | if (breaks (&TREE_OPERAND (t, 0)) |
7086 | || continues (&TREE_OPERAND (t, 0)) | |
7087 | /* Allow for jumping to a cdtor_label. */ | |
7088 | || returns (&TREE_OPERAND (t, 0))) | |
7089 | *jump_target = TREE_OPERAND (t, 0); | |
7090 | else | |
7091 | { | |
7092 | gcc_assert (cxx_dialect >= cxx23); | |
7093 | if (!ctx->quiet) | |
7094 | error_at (loc, "%<goto%> is not a constant expression"); | |
7095 | *non_constant_p = true; | |
7096 | } | |
56632b27 JM |
7097 | break; |
7098 | ||
60813a46 | 7099 | case LOOP_EXPR: |
43574e4f JJ |
7100 | case DO_STMT: |
7101 | case WHILE_STMT: | |
7102 | case FOR_STMT: | |
2b3ab879 | 7103 | cxx_eval_loop_expr (ctx, t, |
56632b27 JM |
7104 | non_constant_p, overflow_p, jump_target); |
7105 | break; | |
7106 | ||
60813a46 | 7107 | case SWITCH_EXPR: |
43574e4f | 7108 | case SWITCH_STMT: |
2b3ab879 | 7109 | cxx_eval_switch_expr (ctx, t, |
56632b27 | 7110 | non_constant_p, overflow_p, jump_target); |
60813a46 JM |
7111 | break; |
7112 | ||
971e17ff AS |
7113 | case REQUIRES_EXPR: |
7114 | /* It's possible to get a requires-expression in a constant | |
7115 | expression. For example: | |
7116 | ||
7117 | template<typename T> concept bool C() { | |
7118 | return requires (T t) { t; }; | |
7119 | } | |
7120 | ||
7121 | template<typename T> requires !C<T>() void f(T); | |
7122 | ||
7123 | Normalization leaves f with the associated constraint | |
7124 | '!requires (T t) { ... }' which is not transformed into | |
7125 | a constraint. */ | |
7126 | if (!processing_template_decl) | |
662ef5b5 | 7127 | return evaluate_requires_expr (t); |
971e17ff AS |
7128 | else |
7129 | *non_constant_p = true; | |
7130 | return t; | |
7131 | ||
98e09245 | 7132 | case ANNOTATE_EXPR: |
98e09245 JJ |
7133 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
7134 | lval, | |
7135 | non_constant_p, overflow_p, | |
7136 | jump_target); | |
7137 | break; | |
7138 | ||
3664e317 JM |
7139 | case USING_STMT: |
7140 | r = void_node; | |
7141 | break; | |
7142 | ||
cb57504a JM |
7143 | case TEMPLATE_ID_EXPR: |
7144 | { | |
7145 | /* We can evaluate template-id that refers to a concept only if | |
7146 | the template arguments are non-dependent. */ | |
861d4af8 AS |
7147 | tree id = unpack_concept_check (t); |
7148 | tree tmpl = TREE_OPERAND (id, 0); | |
7149 | if (!concept_definition_p (tmpl)) | |
cb57504a JM |
7150 | internal_error ("unexpected template-id %qE", t); |
7151 | ||
861d4af8 AS |
7152 | if (function_concept_p (tmpl)) |
7153 | { | |
7154 | if (!ctx->quiet) | |
7155 | error_at (cp_expr_loc_or_input_loc (t), | |
7156 | "function concept must be called"); | |
7157 | r = error_mark_node; | |
7158 | break; | |
7159 | } | |
7160 | ||
9329344a | 7161 | if (!value_dependent_expression_p (t) |
6d1556ec | 7162 | && !uid_sensitive_constexpr_evaluation_p ()) |
662ef5b5 | 7163 | r = evaluate_concept_check (t); |
cb57504a JM |
7164 | else |
7165 | *non_constant_p = true; | |
861d4af8 AS |
7166 | |
7167 | break; | |
cb57504a JM |
7168 | } |
7169 | ||
529bc410 MP |
7170 | case ASM_EXPR: |
7171 | if (!ctx->quiet) | |
6821245b | 7172 | inline_asm_in_constexpr_error (loc); |
529bc410 MP |
7173 | *non_constant_p = true; |
7174 | return t; | |
7175 | ||
896048cf | 7176 | case BIT_CAST_EXPR: |
606f2af1 JJ |
7177 | if (lval) |
7178 | { | |
7179 | if (!ctx->quiet) | |
7180 | error_at (EXPR_LOCATION (t), | |
7181 | "address of a call to %qs is not a constant expression", | |
7182 | "__builtin_bit_cast"); | |
7183 | *non_constant_p = true; | |
7184 | return t; | |
7185 | } | |
896048cf JJ |
7186 | r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p); |
7187 | break; | |
7188 | ||
2d76680f | 7189 | default: |
c6e7c499 JM |
7190 | if (STATEMENT_CODE_P (TREE_CODE (t))) |
7191 | { | |
7192 | /* This function doesn't know how to deal with pre-genericize | |
7193 | statements; this can only happen with statement-expressions, | |
7194 | so for now just fail. */ | |
7195 | if (!ctx->quiet) | |
7196 | error_at (EXPR_LOCATION (t), | |
64d6d399 | 7197 | "statement is not a constant expression"); |
c6e7c499 JM |
7198 | } |
7199 | else | |
7200 | internal_error ("unexpected expression %qE of kind %s", t, | |
7201 | get_tree_code_name (TREE_CODE (t))); | |
2d76680f PC |
7202 | *non_constant_p = true; |
7203 | break; | |
7204 | } | |
7205 | ||
7206 | if (r == error_mark_node) | |
7207 | *non_constant_p = true; | |
7208 | ||
7209 | if (*non_constant_p) | |
7210 | return t; | |
7211 | else | |
7212 | return r; | |
7213 | } | |
7214 | ||
e079dced JM |
7215 | /* P0859: A function is needed for constant evaluation if it is a constexpr |
7216 | function that is named by an expression ([basic.def.odr]) that is | |
7217 | potentially constant evaluated. | |
7218 | ||
7219 | So we need to instantiate any constexpr functions mentioned by the | |
7220 | expression even if the definition isn't needed for evaluating the | |
7221 | expression. */ | |
7222 | ||
7223 | static tree | |
7224 | instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/) | |
7225 | { | |
7226 | if (TREE_CODE (*tp) == FUNCTION_DECL | |
7227 | && DECL_DECLARED_CONSTEXPR_P (*tp) | |
7228 | && !DECL_INITIAL (*tp) | |
4fdda3ce | 7229 | && !trivial_fn_p (*tp) |
f65a3299 PP |
7230 | && DECL_TEMPLOID_INSTANTIATION (*tp) |
7231 | && !uid_sensitive_constexpr_evaluation_p ()) | |
e079dced JM |
7232 | { |
7233 | ++function_depth; | |
7234 | instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false); | |
7235 | --function_depth; | |
7236 | } | |
7237 | else if (TREE_CODE (*tp) == CALL_EXPR | |
7238 | || TREE_CODE (*tp) == AGGR_INIT_EXPR) | |
7239 | { | |
7240 | if (EXPR_HAS_LOCATION (*tp)) | |
7241 | input_location = EXPR_LOCATION (*tp); | |
7242 | } | |
7243 | ||
7244 | if (!EXPR_P (*tp)) | |
7245 | *walk_subtrees = 0; | |
7246 | ||
7247 | return NULL_TREE; | |
7248 | } | |
8e007055 | 7249 | |
e079dced JM |
7250 | static void |
7251 | instantiate_constexpr_fns (tree t) | |
7252 | { | |
7253 | location_t loc = input_location; | |
7254 | cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL); | |
7255 | input_location = loc; | |
7256 | } | |
7257 | ||
8e007055 JJ |
7258 | /* Look for heap variables in the expression *TP. */ |
7259 | ||
7260 | static tree | |
7261 | find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/) | |
7262 | { | |
7263 | if (VAR_P (*tp) | |
7264 | && (DECL_NAME (*tp) == heap_uninit_identifier | |
7265 | || DECL_NAME (*tp) == heap_identifier | |
815baade JJ |
7266 | || DECL_NAME (*tp) == heap_vec_uninit_identifier |
7267 | || DECL_NAME (*tp) == heap_vec_identifier | |
8e007055 JJ |
7268 | || DECL_NAME (*tp) == heap_deleted_identifier)) |
7269 | return *tp; | |
7270 | ||
7271 | if (TYPE_P (*tp)) | |
7272 | *walk_subtrees = 0; | |
7273 | return NULL_TREE; | |
7274 | } | |
7275 | ||
f968ef9b JJ |
7276 | /* Find immediate function decls in *TP if any. */ |
7277 | ||
7278 | static tree | |
7279 | find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/) | |
7280 | { | |
7281 | if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp)) | |
7282 | return *tp; | |
4b2fda8b JJ |
7283 | if (TREE_CODE (*tp) == PTRMEM_CST |
7284 | && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL | |
7285 | && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp))) | |
7286 | return PTRMEM_CST_MEMBER (*tp); | |
f968ef9b JJ |
7287 | return NULL_TREE; |
7288 | } | |
7289 | ||
e4082611 | 7290 | /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression. |
d4accef3 JM |
7291 | STRICT has the same sense as for constant_value_1: true if we only allow |
7292 | conforming C++ constant expressions, or false if we want a constant value | |
7293 | even if it doesn't conform. | |
13de99bc | 7294 | MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as |
8e007055 JJ |
7295 | per P0595 even when ALLOW_NON_CONSTANT is true. |
7296 | CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable. | |
7297 | OBJECT must be non-NULL in that case. */ | |
e4082611 | 7298 | |
2d76680f | 7299 | static tree |
3e605b20 | 7300 | cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant, |
e4082611 | 7301 | bool strict = true, |
13de99bc | 7302 | bool manifestly_const_eval = false, |
8e007055 | 7303 | bool constexpr_dtor = false, |
f65a3299 | 7304 | tree object = NULL_TREE) |
2d76680f | 7305 | { |
8108ea30 JM |
7306 | auto_timevar time (TV_CONSTEXPR); |
7307 | ||
2d76680f PC |
7308 | bool non_constant_p = false; |
7309 | bool overflow_p = false; | |
39dce2b7 | 7310 | |
d419e176 MP |
7311 | if (BRACE_ENCLOSED_INITIALIZER_P (t)) |
7312 | { | |
7313 | gcc_checking_assert (allow_non_constant); | |
7314 | return t; | |
7315 | } | |
7316 | ||
8e007055 | 7317 | constexpr_global_ctx global_ctx; |
49a86fce | 7318 | constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL, |
8e007055 | 7319 | allow_non_constant, strict, |
f65a3299 | 7320 | manifestly_const_eval || !allow_non_constant }; |
39dce2b7 | 7321 | |
6641d6d3 JJ |
7322 | /* Turn off -frounding-math for manifestly constant evaluation. */ |
7323 | warning_sentinel rm (flag_rounding_math, ctx.manifestly_const_eval); | |
3e605b20 | 7324 | tree type = initialized_type (t); |
3e605b20 | 7325 | tree r = t; |
f968ef9b | 7326 | bool is_consteval = false; |
4d0c18c6 | 7327 | if (VOID_TYPE_P (type)) |
8e007055 JJ |
7328 | { |
7329 | if (constexpr_dtor) | |
7330 | /* Used for destructors of array elements. */ | |
7331 | type = TREE_TYPE (object); | |
7332 | else | |
f968ef9b | 7333 | { |
b04445d4 | 7334 | if (cxx_dialect < cxx20) |
f968ef9b JJ |
7335 | return t; |
7336 | if (TREE_CODE (t) != CALL_EXPR && TREE_CODE (t) != AGGR_INIT_EXPR) | |
7337 | return t; | |
7338 | /* Calls to immediate functions returning void need to be | |
7339 | evaluated. */ | |
7340 | tree fndecl = cp_get_callee_fndecl_nofold (t); | |
7341 | if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl)) | |
7342 | return t; | |
7343 | else | |
7344 | is_consteval = true; | |
7345 | } | |
7346 | } | |
b04445d4 | 7347 | else if (cxx_dialect >= cxx20 |
f968ef9b JJ |
7348 | && (TREE_CODE (t) == CALL_EXPR |
7349 | || TREE_CODE (t) == AGGR_INIT_EXPR | |
7350 | || TREE_CODE (t) == TARGET_EXPR)) | |
7351 | { | |
861d4af8 AS |
7352 | /* For non-concept checks, determine if it is consteval. */ |
7353 | if (!concept_check_p (t)) | |
7354 | { | |
7355 | tree x = t; | |
7356 | if (TREE_CODE (x) == TARGET_EXPR) | |
7357 | x = TARGET_EXPR_INITIAL (x); | |
7358 | tree fndecl = cp_get_callee_fndecl_nofold (x); | |
7359 | if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl)) | |
7360 | is_consteval = true; | |
7361 | } | |
8e007055 | 7362 | } |
3e605b20 JM |
7363 | if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)) |
7364 | { | |
7365 | /* In C++14 an NSDMI can participate in aggregate initialization, | |
7366 | and can refer to the address of the object being initialized, so | |
7367 | we need to pass in the relevant VAR_DECL if we want to do the | |
7368 | evaluation in a single pass. The evaluation will dynamically | |
7369 | update ctx.values for the VAR_DECL. We use the same strategy | |
7370 | for C++11 constexpr constructors that refer to the object being | |
7371 | initialized. */ | |
8e007055 JJ |
7372 | if (constexpr_dtor) |
7373 | { | |
7374 | gcc_assert (object && VAR_P (object)); | |
7375 | gcc_assert (DECL_DECLARED_CONSTEXPR_P (object)); | |
7376 | gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object)); | |
fce6467b JJ |
7377 | if (error_operand_p (DECL_INITIAL (object))) |
7378 | return t; | |
8e007055 JJ |
7379 | ctx.ctor = unshare_expr (DECL_INITIAL (object)); |
7380 | TREE_READONLY (ctx.ctor) = false; | |
7381 | /* Temporarily force decl_really_constant_value to return false | |
7382 | for it, we want to use ctx.ctor for the current value instead. */ | |
7383 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false; | |
7384 | } | |
7385 | else | |
7386 | { | |
7387 | ctx.ctor = build_constructor (type, NULL); | |
7388 | CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true; | |
7389 | } | |
60813a46 JM |
7390 | if (!object) |
7391 | { | |
7392 | if (TREE_CODE (t) == TARGET_EXPR) | |
7393 | object = TARGET_EXPR_SLOT (t); | |
7394 | else if (TREE_CODE (t) == AGGR_INIT_EXPR) | |
7395 | object = AGGR_INIT_EXPR_SLOT (t); | |
7396 | } | |
3e605b20 JM |
7397 | ctx.object = object; |
7398 | if (object) | |
7399 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
7400 | (type, TREE_TYPE (object))); | |
7401 | if (object && DECL_P (object)) | |
8e007055 | 7402 | global_ctx.values.put (object, ctx.ctor); |
3e605b20 JM |
7403 | if (TREE_CODE (r) == TARGET_EXPR) |
7404 | /* Avoid creating another CONSTRUCTOR when we expand the | |
7405 | TARGET_EXPR. */ | |
7406 | r = TARGET_EXPR_INITIAL (r); | |
7407 | } | |
7408 | ||
ee1de08d JJ |
7409 | auto_vec<tree, 16> cleanups; |
7410 | global_ctx.cleanups = &cleanups; | |
7411 | ||
1595fe44 JM |
7412 | if (manifestly_const_eval) |
7413 | instantiate_constexpr_fns (r); | |
2b3ab879 | 7414 | r = cxx_eval_constant_expression (&ctx, r, |
5a804683 | 7415 | false, &non_constant_p, &overflow_p); |
2d76680f | 7416 | |
8e007055 JJ |
7417 | if (!constexpr_dtor) |
7418 | verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p); | |
7419 | else | |
7420 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true; | |
2d76680f | 7421 | |
ee1de08d JJ |
7422 | unsigned int i; |
7423 | tree cleanup; | |
7424 | /* Evaluate the cleanups. */ | |
7425 | FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup) | |
7426 | cxx_eval_constant_expression (&ctx, cleanup, false, | |
7427 | &non_constant_p, &overflow_p); | |
7428 | ||
023d89c7 JM |
7429 | /* Mutable logic is a bit tricky: we want to allow initialization of |
7430 | constexpr variables with mutable members, but we can't copy those | |
7431 | members to another constexpr variable. */ | |
8e007055 | 7432 | if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_MUTABLE_POISON (r)) |
2d76680f | 7433 | { |
2d76680f | 7434 | if (!allow_non_constant) |
023d89c7 JM |
7435 | error ("%qE is not a constant expression because it refers to " |
7436 | "mutable subobjects of %qT", t, type); | |
2d76680f PC |
7437 | non_constant_p = true; |
7438 | } | |
7439 | ||
8e007055 | 7440 | if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r)) |
f64e0c02 JM |
7441 | { |
7442 | if (!allow_non_constant) | |
7443 | error ("%qE is not a constant expression because it refers to " | |
7444 | "an incompletely initialized variable", t); | |
7445 | TREE_CONSTANT (r) = false; | |
7446 | non_constant_p = true; | |
7447 | } | |
7448 | ||
8e007055 JJ |
7449 | if (!global_ctx.heap_vars.is_empty ()) |
7450 | { | |
7451 | tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs, | |
7452 | NULL); | |
7453 | unsigned int i; | |
7454 | if (heap_var) | |
7455 | { | |
7456 | if (!allow_non_constant && !non_constant_p) | |
7457 | error_at (DECL_SOURCE_LOCATION (heap_var), | |
7458 | "%qE is not a constant expression because it refers to " | |
7459 | "a result of %<operator new%>", t); | |
7460 | r = t; | |
7461 | non_constant_p = true; | |
7462 | } | |
7463 | FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var) | |
a8db7887 JJ |
7464 | { |
7465 | if (DECL_NAME (heap_var) != heap_deleted_identifier) | |
7466 | { | |
7467 | if (!allow_non_constant && !non_constant_p) | |
7468 | error_at (DECL_SOURCE_LOCATION (heap_var), | |
7469 | "%qE is not a constant expression because allocated " | |
7470 | "storage has not been deallocated", t); | |
7471 | r = t; | |
7472 | non_constant_p = true; | |
7473 | } | |
7474 | varpool_node::get (heap_var)->remove (); | |
7475 | } | |
8e007055 JJ |
7476 | } |
7477 | ||
f968ef9b | 7478 | /* Check that immediate invocation does not return an expression referencing |
7473b8a9 JJ |
7479 | any immediate function decls. */ |
7480 | if (is_consteval || in_immediate_context ()) | |
f968ef9b JJ |
7481 | if (tree immediate_fndecl |
7482 | = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl, | |
7483 | NULL)) | |
7484 | { | |
7485 | if (!allow_non_constant && !non_constant_p) | |
7486 | error_at (cp_expr_loc_or_input_loc (t), | |
7487 | "immediate evaluation returns address of immediate " | |
7488 | "function %qD", immediate_fndecl); | |
7489 | r = t; | |
7490 | non_constant_p = true; | |
7491 | } | |
7492 | ||
8895443a JM |
7493 | if (non_constant_p) |
7494 | /* If we saw something bad, go back to our argument. The wrapping below is | |
7495 | only for the cases of TREE_CONSTANT argument or overflow. */ | |
7496 | r = t; | |
2d76680f PC |
7497 | |
7498 | if (!non_constant_p && overflow_p) | |
7499 | non_constant_p = true; | |
7500 | ||
e0804c9b | 7501 | /* Unshare the result. */ |
56cfb596 | 7502 | bool should_unshare = true; |
e0804c9b JM |
7503 | if (r == t || (TREE_CODE (t) == TARGET_EXPR |
7504 | && TARGET_EXPR_INITIAL (t) == r)) | |
56cfb596 PP |
7505 | should_unshare = false; |
7506 | ||
2d76680f PC |
7507 | if (non_constant_p && !allow_non_constant) |
7508 | return error_mark_node; | |
8e007055 JJ |
7509 | else if (constexpr_dtor) |
7510 | return r; | |
2d76680f PC |
7511 | else if (non_constant_p && TREE_CONSTANT (r)) |
7512 | { | |
eddd715c JM |
7513 | /* This isn't actually constant, so unset TREE_CONSTANT. |
7514 | Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires | |
7515 | it to be set if it is invariant address, even when it is not | |
7516 | a valid C++ constant expression. Wrap it with a NOP_EXPR | |
7517 | instead. */ | |
7518 | if (EXPR_P (r) && TREE_CODE (r) != ADDR_EXPR) | |
2d76680f PC |
7519 | r = copy_node (r); |
7520 | else if (TREE_CODE (r) == CONSTRUCTOR) | |
7521 | r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r); | |
7522 | else | |
7523 | r = build_nop (TREE_TYPE (r), r); | |
7524 | TREE_CONSTANT (r) = false; | |
7525 | } | |
5dab8b11 | 7526 | else if (non_constant_p) |
2d76680f PC |
7527 | return t; |
7528 | ||
56cfb596 PP |
7529 | if (should_unshare) |
7530 | r = unshare_expr (r); | |
7531 | ||
2d76680f PC |
7532 | if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r))) |
7533 | { | |
5dab8b11 | 7534 | r = adjust_temp_type (type, r); |
2d76680f PC |
7535 | if (TREE_CODE (t) == TARGET_EXPR |
7536 | && TARGET_EXPR_INITIAL (t) == r) | |
7537 | return t; | |
5dab8b11 | 7538 | else if (TREE_CODE (t) != CONSTRUCTOR) |
2d76680f | 7539 | { |
8e007055 | 7540 | r = get_target_expr_sfinae (r, tf_warning_or_error | tf_no_cleanup); |
2d76680f | 7541 | TREE_CONSTANT (r) = true; |
2d76680f PC |
7542 | } |
7543 | } | |
5dab8b11 | 7544 | |
729f6881 JM |
7545 | /* Remember the original location if that wouldn't need a wrapper. */ |
7546 | if (location_t loc = EXPR_LOCATION (t)) | |
8d970817 | 7547 | protected_set_expr_location (r, loc); |
729f6881 | 7548 | |
5dab8b11 | 7549 | return r; |
2d76680f PC |
7550 | } |
7551 | ||
2d76680f PC |
7552 | /* If T represents a constant expression returns its reduced value. |
7553 | Otherwise return error_mark_node. If T is dependent, then | |
7554 | return NULL. */ | |
7555 | ||
7556 | tree | |
3e605b20 | 7557 | cxx_constant_value (tree t, tree decl) |
2d76680f | 7558 | { |
8e007055 JJ |
7559 | return cxx_eval_outermost_constant_expr (t, false, true, true, false, decl); |
7560 | } | |
7561 | ||
a8de8324 JM |
7562 | /* As above, but respect SFINAE. */ |
7563 | ||
7564 | tree | |
7565 | cxx_constant_value_sfinae (tree t, tsubst_flags_t complain) | |
7566 | { | |
7567 | bool sfinae = !(complain & tf_error); | |
7568 | tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, true); | |
7569 | if (sfinae && !TREE_CONSTANT (r)) | |
7570 | r = error_mark_node; | |
7571 | return r; | |
7572 | } | |
7573 | ||
8e007055 JJ |
7574 | /* Like cxx_constant_value, but used for evaluation of constexpr destructors |
7575 | of constexpr variables. The actual initializer of DECL is not modified. */ | |
7576 | ||
7577 | void | |
7578 | cxx_constant_dtor (tree t, tree decl) | |
7579 | { | |
7580 | cxx_eval_outermost_constant_expr (t, false, true, true, true, decl); | |
2d76680f PC |
7581 | } |
7582 | ||
cda0a029 JM |
7583 | /* Helper routine for fold_simple function. Either return simplified |
7584 | expression T, otherwise NULL_TREE. | |
7585 | In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold | |
7586 | even if we are within template-declaration. So be careful on call, as in | |
7587 | such case types can be undefined. */ | |
7588 | ||
7589 | static tree | |
7590 | fold_simple_1 (tree t) | |
7591 | { | |
7592 | tree op1; | |
7593 | enum tree_code code = TREE_CODE (t); | |
7594 | ||
7595 | switch (code) | |
7596 | { | |
7597 | case INTEGER_CST: | |
7598 | case REAL_CST: | |
7599 | case VECTOR_CST: | |
7600 | case FIXED_CST: | |
7601 | case COMPLEX_CST: | |
7602 | return t; | |
7603 | ||
7604 | case SIZEOF_EXPR: | |
7605 | return fold_sizeof_expr (t); | |
7606 | ||
7607 | case ABS_EXPR: | |
e28fadbc | 7608 | case ABSU_EXPR: |
cda0a029 JM |
7609 | case CONJ_EXPR: |
7610 | case REALPART_EXPR: | |
7611 | case IMAGPART_EXPR: | |
7612 | case NEGATE_EXPR: | |
7613 | case BIT_NOT_EXPR: | |
7614 | case TRUTH_NOT_EXPR: | |
7615 | case NOP_EXPR: | |
7616 | case VIEW_CONVERT_EXPR: | |
7617 | case CONVERT_EXPR: | |
7618 | case FLOAT_EXPR: | |
7619 | case FIX_TRUNC_EXPR: | |
7620 | case FIXED_CONVERT_EXPR: | |
7621 | case ADDR_SPACE_CONVERT_EXPR: | |
7622 | ||
7623 | op1 = TREE_OPERAND (t, 0); | |
7624 | ||
7625 | t = const_unop (code, TREE_TYPE (t), op1); | |
7626 | if (!t) | |
7627 | return NULL_TREE; | |
7628 | ||
7629 | if (CONVERT_EXPR_CODE_P (code) | |
7630 | && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1)) | |
7631 | TREE_OVERFLOW (t) = false; | |
7632 | return t; | |
7633 | ||
7634 | default: | |
7635 | return NULL_TREE; | |
7636 | } | |
7637 | } | |
7638 | ||
7639 | /* If T is a simple constant expression, returns its simplified value. | |
dba99244 | 7640 | Otherwise returns T. In contrast to maybe_constant_value we |
cda0a029 JM |
7641 | simplify only few operations on constant-expressions, and we don't |
7642 | try to simplify constexpressions. */ | |
7643 | ||
7644 | tree | |
7645 | fold_simple (tree t) | |
7646 | { | |
cda0a029 JM |
7647 | if (processing_template_decl) |
7648 | return t; | |
7649 | ||
dba99244 MP |
7650 | tree r = fold_simple_1 (t); |
7651 | if (r) | |
7652 | return r; | |
cda0a029 | 7653 | |
dba99244 | 7654 | return t; |
cda0a029 JM |
7655 | } |
7656 | ||
2d76680f PC |
7657 | /* If T is a constant expression, returns its reduced value. |
7658 | Otherwise, if T does not have TREE_CONSTANT set, returns T. | |
13de99bc JJ |
7659 | Otherwise, returns a version of T without TREE_CONSTANT. |
7660 | MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated | |
f65a3299 | 7661 | as per P0595. */ |
2d76680f | 7662 | |
8a87daca JM |
7663 | static GTY((deletable)) hash_map<tree, tree> *cv_cache; |
7664 | ||
7665 | tree | |
f65a3299 | 7666 | maybe_constant_value (tree t, tree decl, bool manifestly_const_eval) |
2d76680f PC |
7667 | { |
7668 | tree r; | |
7669 | ||
a0ab7ccd | 7670 | if (!is_nondependent_constant_expression (t)) |
2d76680f PC |
7671 | { |
7672 | if (TREE_OVERFLOW_P (t)) | |
7673 | { | |
7674 | t = build_nop (TREE_TYPE (t), t); | |
7675 | TREE_CONSTANT (t) = false; | |
7676 | } | |
7677 | return t; | |
7678 | } | |
8a87daca JM |
7679 | else if (CONSTANT_CLASS_P (t)) |
7680 | /* No caching or evaluation needed. */ | |
7681 | return t; | |
7682 | ||
13de99bc | 7683 | if (manifestly_const_eval) |
f65a3299 | 7684 | return cxx_eval_outermost_constant_expr (t, true, true, true, false, decl); |
13de99bc | 7685 | |
8a87daca JM |
7686 | if (cv_cache == NULL) |
7687 | cv_cache = hash_map<tree, tree>::create_ggc (101); | |
7688 | if (tree *cached = cv_cache->get (t)) | |
173c8def JM |
7689 | { |
7690 | r = *cached; | |
7691 | if (r != t) | |
7692 | { | |
9f143008 | 7693 | r = break_out_target_exprs (r, /*clear_loc*/true); |
173c8def JM |
7694 | protected_set_expr_location (r, EXPR_LOCATION (t)); |
7695 | } | |
dfffecb8 | 7696 | return r; |
173c8def | 7697 | } |
2d76680f | 7698 | |
4df7f8c7 JM |
7699 | /* Don't evaluate an unevaluated operand. */ |
7700 | if (cp_unevaluated_operand) | |
7701 | return t; | |
7702 | ||
f65a3299 PP |
7703 | uid_sensitive_constexpr_evaluation_checker c; |
7704 | r = cxx_eval_outermost_constant_expr (t, true, true, false, false, decl); | |
595278be MM |
7705 | gcc_checking_assert (r == t |
7706 | || CONVERT_EXPR_P (t) | |
7707 | || TREE_CODE (t) == VIEW_CONVERT_EXPR | |
7708 | || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) | |
7709 | || !cp_tree_equal (r, t)); | |
f65a3299 PP |
7710 | if (!c.evaluation_restricted_p ()) |
7711 | cv_cache->put (t, r); | |
2d76680f PC |
7712 | return r; |
7713 | } | |
7714 | ||
7a7ac32a PP |
7715 | /* Dispose of the whole CV_CACHE. */ |
7716 | ||
7717 | static void | |
7718 | clear_cv_cache (void) | |
7719 | { | |
7720 | if (cv_cache != NULL) | |
7721 | cv_cache->empty (); | |
7722 | } | |
7723 | ||
bebabf70 | 7724 | /* Dispose of the whole CV_CACHE and FOLD_CACHE. */ |
1e297006 MP |
7725 | |
7726 | void | |
bebabf70 | 7727 | clear_cv_and_fold_caches () |
1e297006 | 7728 | { |
7a7ac32a | 7729 | clear_cv_cache (); |
eba9e839 | 7730 | clear_fold_cache (); |
1e297006 MP |
7731 | } |
7732 | ||
a81c8e8c MP |
7733 | /* Internal function handling expressions in templates for |
7734 | fold_non_dependent_expr and fold_non_dependent_init. | |
7735 | ||
7736 | If we're in a template, but T isn't value dependent, simplify | |
7737 | it. We're supposed to treat: | |
7738 | ||
7739 | template <typename T> void f(T[1 + 1]); | |
7740 | template <typename T> void f(T[2]); | |
7741 | ||
7742 | as two declarations of the same function, for example. */ | |
7743 | ||
7744 | static tree | |
7745 | fold_non_dependent_expr_template (tree t, tsubst_flags_t complain, | |
f968ef9b JJ |
7746 | bool manifestly_const_eval, |
7747 | tree object) | |
a81c8e8c MP |
7748 | { |
7749 | gcc_assert (processing_template_decl); | |
7750 | ||
7751 | if (is_nondependent_constant_expression (t)) | |
7752 | { | |
7753 | processing_template_decl_sentinel s; | |
7754 | t = instantiate_non_dependent_expr_internal (t, complain); | |
7755 | ||
7756 | if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t)) | |
7757 | { | |
7758 | if (TREE_OVERFLOW_P (t)) | |
7759 | { | |
7760 | t = build_nop (TREE_TYPE (t), t); | |
7761 | TREE_CONSTANT (t) = false; | |
7762 | } | |
7763 | return t; | |
7764 | } | |
7765 | ||
4df7f8c7 JM |
7766 | if (cp_unevaluated_operand && !manifestly_const_eval) |
7767 | return t; | |
7768 | ||
a81c8e8c MP |
7769 | tree r = cxx_eval_outermost_constant_expr (t, true, true, |
7770 | manifestly_const_eval, | |
f968ef9b | 7771 | false, object); |
a81c8e8c MP |
7772 | /* cp_tree_equal looks through NOPs, so allow them. */ |
7773 | gcc_checking_assert (r == t | |
7774 | || CONVERT_EXPR_P (t) | |
7775 | || TREE_CODE (t) == VIEW_CONVERT_EXPR | |
7776 | || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) | |
7777 | || !cp_tree_equal (r, t)); | |
7778 | return r; | |
7779 | } | |
7780 | else if (TREE_OVERFLOW_P (t)) | |
7781 | { | |
7782 | t = build_nop (TREE_TYPE (t), t); | |
7783 | TREE_CONSTANT (t) = false; | |
7784 | } | |
7785 | ||
7786 | return t; | |
7787 | } | |
7788 | ||
234bef96 PC |
7789 | /* Like maybe_constant_value but first fully instantiate the argument. |
7790 | ||
7791 | Note: this is equivalent to instantiate_non_dependent_expr_sfinae | |
e56f6629 JM |
7792 | (t, complain) followed by maybe_constant_value but is more efficient, |
7793 | because it calls instantiation_dependent_expression_p and | |
7794 | potential_constant_expression at most once. | |
4cd3e7df | 7795 | The manifestly_const_eval argument is passed to maybe_constant_value. |
e56f6629 JM |
7796 | |
7797 | Callers should generally pass their active complain, or if they are in a | |
7798 | non-template, diagnosing context, they can use the default of | |
7799 | tf_warning_or_error. Callers that might be within a template context, don't | |
7800 | have a complain parameter, and aren't going to remember the result for long | |
7801 | (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node | |
7802 | appropriately. */ | |
234bef96 PC |
7803 | |
7804 | tree | |
e56f6629 | 7805 | fold_non_dependent_expr (tree t, |
4cd3e7df | 7806 | tsubst_flags_t complain /* = tf_warning_or_error */, |
f968ef9b JJ |
7807 | bool manifestly_const_eval /* = false */, |
7808 | tree object /* = NULL_TREE */) | |
234bef96 PC |
7809 | { |
7810 | if (t == NULL_TREE) | |
7811 | return NULL_TREE; | |
7812 | ||
a81c8e8c MP |
7813 | if (processing_template_decl) |
7814 | return fold_non_dependent_expr_template (t, complain, | |
f968ef9b | 7815 | manifestly_const_eval, object); |
234bef96 | 7816 | |
f968ef9b | 7817 | return maybe_constant_value (t, object, manifestly_const_eval); |
a81c8e8c | 7818 | } |
234bef96 | 7819 | |
ae2ebf01 MP |
7820 | /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant, |
7821 | return the original expression. */ | |
7822 | ||
7823 | tree | |
7824 | maybe_fold_non_dependent_expr (tree expr, | |
7825 | tsubst_flags_t complain/*=tf_warning_or_error*/) | |
7826 | { | |
7827 | tree t = fold_non_dependent_expr (expr, complain); | |
7828 | if (t && TREE_CONSTANT (t)) | |
7829 | return t; | |
7830 | ||
7831 | return expr; | |
7832 | } | |
234bef96 | 7833 | |
a81c8e8c | 7834 | /* Like maybe_constant_init but first fully instantiate the argument. */ |
234bef96 | 7835 | |
a81c8e8c MP |
7836 | tree |
7837 | fold_non_dependent_init (tree t, | |
7838 | tsubst_flags_t complain /*=tf_warning_or_error*/, | |
69bf1c7d MP |
7839 | bool manifestly_const_eval /*=false*/, |
7840 | tree object /* = NULL_TREE */) | |
a81c8e8c MP |
7841 | { |
7842 | if (t == NULL_TREE) | |
7843 | return NULL_TREE; | |
7844 | ||
7845 | if (processing_template_decl) | |
7846 | { | |
7847 | t = fold_non_dependent_expr_template (t, complain, | |
69bf1c7d | 7848 | manifestly_const_eval, object); |
a81c8e8c MP |
7849 | /* maybe_constant_init does this stripping, so do it here too. */ |
7850 | if (TREE_CODE (t) == TARGET_EXPR) | |
234bef96 | 7851 | { |
a81c8e8c MP |
7852 | tree init = TARGET_EXPR_INITIAL (t); |
7853 | if (TREE_CODE (init) == CONSTRUCTOR) | |
7854 | t = init; | |
234bef96 PC |
7855 | } |
7856 | return t; | |
7857 | } | |
7858 | ||
69bf1c7d | 7859 | return maybe_constant_init (t, object, manifestly_const_eval); |
234bef96 PC |
7860 | } |
7861 | ||
2d76680f | 7862 | /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather |
e4082611 JJ |
7863 | than wrapped in a TARGET_EXPR. |
7864 | ALLOW_NON_CONSTANT is false if T is required to be a constant expression. | |
13de99bc | 7865 | MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as |
e4082611 | 7866 | per P0595 even when ALLOW_NON_CONSTANT is true. */ |
2d76680f | 7867 | |
118cd6ba | 7868 | static tree |
e4082611 | 7869 | maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant, |
13de99bc | 7870 | bool manifestly_const_eval) |
2d76680f | 7871 | { |
cda0a029 JM |
7872 | if (!t) |
7873 | return t; | |
2d76680f PC |
7874 | if (TREE_CODE (t) == EXPR_STMT) |
7875 | t = TREE_OPERAND (t, 0); | |
7876 | if (TREE_CODE (t) == CONVERT_EXPR | |
7877 | && VOID_TYPE_P (TREE_TYPE (t))) | |
7878 | t = TREE_OPERAND (t, 0); | |
60813a46 JM |
7879 | if (TREE_CODE (t) == INIT_EXPR) |
7880 | t = TREE_OPERAND (t, 1); | |
f64e0c02 JM |
7881 | if (TREE_CODE (t) == TARGET_EXPR) |
7882 | t = TARGET_EXPR_INITIAL (t); | |
a0ab7ccd | 7883 | if (!is_nondependent_static_init_expression (t)) |
69eb4fde | 7884 | /* Don't try to evaluate it. */; |
118cd6ba | 7885 | else if (CONSTANT_CLASS_P (t) && allow_non_constant) |
8a87daca | 7886 | /* No evaluation needed. */; |
69eb4fde | 7887 | else |
e4082611 | 7888 | t = cxx_eval_outermost_constant_expr (t, allow_non_constant, |
38295573 | 7889 | /*strict*/false, |
8e007055 | 7890 | manifestly_const_eval, false, decl); |
2d76680f PC |
7891 | if (TREE_CODE (t) == TARGET_EXPR) |
7892 | { | |
7893 | tree init = TARGET_EXPR_INITIAL (t); | |
7894 | if (TREE_CODE (init) == CONSTRUCTOR) | |
7895 | t = init; | |
7896 | } | |
7897 | return t; | |
7898 | } | |
7899 | ||
118cd6ba MP |
7900 | /* Wrapper for maybe_constant_init_1 which permits non constants. */ |
7901 | ||
7902 | tree | |
13de99bc | 7903 | maybe_constant_init (tree t, tree decl, bool manifestly_const_eval) |
118cd6ba | 7904 | { |
13de99bc | 7905 | return maybe_constant_init_1 (t, decl, true, manifestly_const_eval); |
118cd6ba MP |
7906 | } |
7907 | ||
7908 | /* Wrapper for maybe_constant_init_1 which does not permit non constants. */ | |
7909 | ||
7910 | tree | |
7911 | cxx_constant_init (tree t, tree decl) | |
7912 | { | |
e4082611 | 7913 | return maybe_constant_init_1 (t, decl, false, true); |
118cd6ba MP |
7914 | } |
7915 | ||
2d76680f PC |
7916 | #if 0 |
7917 | /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */ | |
7918 | /* Return true if the object referred to by REF has automatic or thread | |
7919 | local storage. */ | |
7920 | ||
7921 | enum { ck_ok, ck_bad, ck_unknown }; | |
7922 | static int | |
7923 | check_automatic_or_tls (tree ref) | |
7924 | { | |
ef4bddc2 | 7925 | machine_mode mode; |
f37fac2b | 7926 | poly_int64 bitsize, bitpos; |
2d76680f PC |
7927 | tree offset; |
7928 | int volatilep = 0, unsignedp = 0; | |
7929 | tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset, | |
7930 | &mode, &unsignedp, &volatilep, false); | |
7931 | duration_kind dk; | |
7932 | ||
7933 | /* If there isn't a decl in the middle, we don't know the linkage here, | |
7934 | and this isn't a constant expression anyway. */ | |
7935 | if (!DECL_P (decl)) | |
7936 | return ck_unknown; | |
7937 | dk = decl_storage_duration (decl); | |
7938 | return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok; | |
7939 | } | |
7940 | #endif | |
7941 | ||
c7a53bdb JJ |
7942 | /* Data structure for passing data from potential_constant_expression_1 |
7943 | to check_for_return_continue via cp_walk_tree. */ | |
7944 | struct check_for_return_continue_data { | |
7945 | hash_set<tree> *pset; | |
7946 | tree continue_stmt; | |
0fb7aa20 | 7947 | tree break_stmt; |
c7a53bdb JJ |
7948 | }; |
7949 | ||
7950 | /* Helper function for potential_constant_expression_1 SWITCH_STMT handling, | |
7951 | called through cp_walk_tree. Return the first RETURN_EXPR found, or note | |
0fb7aa20 | 7952 | the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */ |
c7a53bdb JJ |
7953 | static tree |
7954 | check_for_return_continue (tree *tp, int *walk_subtrees, void *data) | |
7955 | { | |
0fb7aa20 | 7956 | tree t = *tp, s, b; |
c7a53bdb JJ |
7957 | check_for_return_continue_data *d = (check_for_return_continue_data *) data; |
7958 | switch (TREE_CODE (t)) | |
7959 | { | |
7960 | case RETURN_EXPR: | |
7961 | return t; | |
7962 | ||
7963 | case CONTINUE_STMT: | |
7964 | if (d->continue_stmt == NULL_TREE) | |
7965 | d->continue_stmt = t; | |
7966 | break; | |
7967 | ||
0fb7aa20 JJ |
7968 | case BREAK_STMT: |
7969 | if (d->break_stmt == NULL_TREE) | |
7970 | d->break_stmt = t; | |
7971 | break; | |
7972 | ||
c7a53bdb JJ |
7973 | #define RECUR(x) \ |
7974 | if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \ | |
7975 | d->pset)) \ | |
7976 | return r | |
7977 | ||
7978 | /* For loops, walk subtrees manually, so that continue stmts found | |
7979 | inside of the bodies of the loops are ignored. */ | |
7980 | case DO_STMT: | |
7981 | *walk_subtrees = 0; | |
7982 | RECUR (DO_COND (t)); | |
7983 | s = d->continue_stmt; | |
0fb7aa20 | 7984 | b = d->break_stmt; |
c7a53bdb JJ |
7985 | RECUR (DO_BODY (t)); |
7986 | d->continue_stmt = s; | |
0fb7aa20 | 7987 | d->break_stmt = b; |
c7a53bdb JJ |
7988 | break; |
7989 | ||
7990 | case WHILE_STMT: | |
7991 | *walk_subtrees = 0; | |
7992 | RECUR (WHILE_COND (t)); | |
7993 | s = d->continue_stmt; | |
0fb7aa20 | 7994 | b = d->break_stmt; |
c7a53bdb JJ |
7995 | RECUR (WHILE_BODY (t)); |
7996 | d->continue_stmt = s; | |
0fb7aa20 | 7997 | d->break_stmt = b; |
c7a53bdb JJ |
7998 | break; |
7999 | ||
8000 | case FOR_STMT: | |
8001 | *walk_subtrees = 0; | |
8002 | RECUR (FOR_INIT_STMT (t)); | |
8003 | RECUR (FOR_COND (t)); | |
8004 | RECUR (FOR_EXPR (t)); | |
8005 | s = d->continue_stmt; | |
0fb7aa20 | 8006 | b = d->break_stmt; |
c7a53bdb JJ |
8007 | RECUR (FOR_BODY (t)); |
8008 | d->continue_stmt = s; | |
0fb7aa20 | 8009 | d->break_stmt = b; |
c7a53bdb JJ |
8010 | break; |
8011 | ||
8012 | case RANGE_FOR_STMT: | |
8013 | *walk_subtrees = 0; | |
8014 | RECUR (RANGE_FOR_EXPR (t)); | |
8015 | s = d->continue_stmt; | |
0fb7aa20 | 8016 | b = d->break_stmt; |
c7a53bdb JJ |
8017 | RECUR (RANGE_FOR_BODY (t)); |
8018 | d->continue_stmt = s; | |
0fb7aa20 JJ |
8019 | d->break_stmt = b; |
8020 | break; | |
8021 | ||
8022 | case SWITCH_STMT: | |
8023 | *walk_subtrees = 0; | |
8024 | RECUR (SWITCH_STMT_COND (t)); | |
8025 | b = d->break_stmt; | |
8026 | RECUR (SWITCH_STMT_BODY (t)); | |
8027 | d->break_stmt = b; | |
c7a53bdb JJ |
8028 | break; |
8029 | #undef RECUR | |
8030 | ||
8031 | case STATEMENT_LIST: | |
8032 | case CONSTRUCTOR: | |
8033 | break; | |
8034 | ||
8035 | default: | |
8036 | if (!EXPR_P (t)) | |
8037 | *walk_subtrees = 0; | |
8038 | break; | |
8039 | } | |
8040 | ||
8041 | return NULL_TREE; | |
8042 | } | |
8043 | ||
2d76680f PC |
8044 | /* Return true if T denotes a potentially constant expression. Issue |
8045 | diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true, | |
a0ab7ccd JM |
8046 | an lvalue-rvalue conversion is implied. If NOW is true, we want to |
8047 | consider the expression in the current context, independent of constexpr | |
8048 | substitution. | |
2d76680f PC |
8049 | |
8050 | C++0x [expr.const] used to say | |
8051 | ||
8052 | 6 An expression is a potential constant expression if it is | |
8053 | a constant expression where all occurrences of function | |
8054 | parameters are replaced by arbitrary constant expressions | |
8055 | of the appropriate type. | |
8056 | ||
8057 | 2 A conditional expression is a constant expression unless it | |
8058 | involves one of the following as a potentially evaluated | |
8059 | subexpression (3.2), but subexpressions of logical AND (5.14), | |
8060 | logical OR (5.15), and conditional (5.16) operations that are | |
8061 | not evaluated are not considered. */ | |
8062 | ||
8063 | static bool | |
a0ab7ccd | 8064 | potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, |
3075affd | 8065 | tsubst_flags_t flags, tree *jump_target) |
2d76680f | 8066 | { |
a0ab7ccd | 8067 | #define RECUR(T,RV) \ |
3075affd | 8068 | potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target) |
a0ab7ccd | 8069 | |
2d76680f PC |
8070 | enum { any = false, rval = true }; |
8071 | int i; | |
8072 | tree tmp; | |
8073 | ||
8074 | if (t == error_mark_node) | |
8075 | return false; | |
8076 | if (t == NULL_TREE) | |
8077 | return true; | |
f9d0ca40 | 8078 | location_t loc = cp_expr_loc_or_input_loc (t); |
3075affd JM |
8079 | |
8080 | if (*jump_target) | |
8081 | /* If we are jumping, ignore everything. This is simpler than the | |
8082 | cxx_eval_constant_expression handling because we only need to be | |
8083 | conservatively correct, and we don't necessarily have a constant value | |
8084 | available, so we don't bother with switch tracking. */ | |
8085 | return true; | |
8086 | ||
3c393a2c | 8087 | if (TREE_THIS_VOLATILE (t) && want_rval) |
2d76680f PC |
8088 | { |
8089 | if (flags & tf_error) | |
3c393a2c MP |
8090 | error_at (loc, "lvalue-to-rvalue conversion of a volatile lvalue " |
8091 | "%qE with type %qT", t, TREE_TYPE (t)); | |
2d76680f PC |
8092 | return false; |
8093 | } | |
8094 | if (CONSTANT_CLASS_P (t)) | |
8095 | return true; | |
7dc2b4a2 JM |
8096 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED) |
8097 | && TREE_TYPE (t) == error_mark_node) | |
8098 | return false; | |
2d76680f PC |
8099 | |
8100 | switch (TREE_CODE (t)) | |
8101 | { | |
8102 | case FUNCTION_DECL: | |
8103 | case BASELINK: | |
8104 | case TEMPLATE_DECL: | |
8105 | case OVERLOAD: | |
8106 | case TEMPLATE_ID_EXPR: | |
8107 | case LABEL_DECL: | |
60813a46 | 8108 | case CASE_LABEL_EXPR: |
81012684 | 8109 | case PREDICT_EXPR: |
2d76680f PC |
8110 | case CONST_DECL: |
8111 | case SIZEOF_EXPR: | |
8112 | case ALIGNOF_EXPR: | |
8113 | case OFFSETOF_EXPR: | |
8114 | case NOEXCEPT_EXPR: | |
8115 | case TEMPLATE_PARM_INDEX: | |
8116 | case TRAIT_EXPR: | |
8117 | case IDENTIFIER_NODE: | |
8118 | case USERDEF_LITERAL: | |
8119 | /* We can see a FIELD_DECL in a pointer-to-member expression. */ | |
8120 | case FIELD_DECL: | |
cda0a029 | 8121 | case RESULT_DECL: |
2d76680f | 8122 | case USING_DECL: |
60813a46 | 8123 | case USING_STMT: |
3e605b20 | 8124 | case PLACEHOLDER_EXPR: |
971e17ff | 8125 | case REQUIRES_EXPR: |
98e5a19a | 8126 | case STATIC_ASSERT: |
96a95ac1 | 8127 | case DEBUG_BEGIN_STMT: |
2d76680f PC |
8128 | return true; |
8129 | ||
3075affd JM |
8130 | case RETURN_EXPR: |
8131 | if (!RECUR (TREE_OPERAND (t, 0), any)) | |
8132 | return false; | |
8133 | /* FALLTHROUGH */ | |
8134 | ||
8135 | case BREAK_STMT: | |
8136 | case CONTINUE_STMT: | |
8137 | *jump_target = t; | |
8138 | return true; | |
8139 | ||
a0ab7ccd | 8140 | case PARM_DECL: |
8fda2c27 | 8141 | if (now && want_rval) |
a0ab7ccd | 8142 | { |
8fda2c27 | 8143 | tree type = TREE_TYPE (t); |
9eb7d0d7 | 8144 | if ((processing_template_decl && !COMPLETE_TYPE_P (type)) |
61466206 | 8145 | || dependent_type_p (type) |
8fda2c27 JM |
8146 | || is_really_empty_class (type, /*ignore_vptr*/false)) |
8147 | /* An empty class has no data to read. */ | |
8148 | return true; | |
a0ab7ccd JM |
8149 | if (flags & tf_error) |
8150 | error ("%qE is not a constant expression", t); | |
8151 | return false; | |
8152 | } | |
8153 | return true; | |
8154 | ||
2d76680f PC |
8155 | case AGGR_INIT_EXPR: |
8156 | case CALL_EXPR: | |
8157 | /* -- an invocation of a function other than a constexpr function | |
8158 | or a constexpr constructor. */ | |
8159 | { | |
8160 | tree fun = get_function_named_in_call (t); | |
8161 | const int nargs = call_expr_nargs (t); | |
8162 | i = 0; | |
8163 | ||
60813a46 JM |
8164 | if (fun == NULL_TREE) |
8165 | { | |
44a845ca MS |
8166 | /* Reset to allow the function to continue past the end |
8167 | of the block below. Otherwise return early. */ | |
8168 | bool bail = true; | |
8169 | ||
35228ac7 JJ |
8170 | if (TREE_CODE (t) == CALL_EXPR |
8171 | && CALL_EXPR_FN (t) == NULL_TREE) | |
8172 | switch (CALL_EXPR_IFN (t)) | |
8173 | { | |
8174 | /* These should be ignored, they are optimized away from | |
8175 | constexpr functions. */ | |
8176 | case IFN_UBSAN_NULL: | |
8177 | case IFN_UBSAN_BOUNDS: | |
8178 | case IFN_UBSAN_VPTR: | |
81fea426 | 8179 | case IFN_FALLTHROUGH: |
35228ac7 | 8180 | return true; |
44a845ca MS |
8181 | |
8182 | case IFN_ADD_OVERFLOW: | |
8183 | case IFN_SUB_OVERFLOW: | |
8184 | case IFN_MUL_OVERFLOW: | |
e16f1cc7 | 8185 | case IFN_LAUNDER: |
d8fcab68 | 8186 | case IFN_VEC_CONVERT: |
44a845ca | 8187 | bail = false; |
d8fcab68 | 8188 | break; |
44a845ca | 8189 | |
35228ac7 JJ |
8190 | default: |
8191 | break; | |
8192 | } | |
44a845ca MS |
8193 | |
8194 | if (bail) | |
8195 | { | |
8196 | /* fold_call_expr can't do anything with IFN calls. */ | |
8197 | if (flags & tf_error) | |
e40b6fc7 | 8198 | error_at (loc, "call to internal function %qE", t); |
44a845ca MS |
8199 | return false; |
8200 | } | |
60813a46 | 8201 | } |
44a845ca MS |
8202 | |
8203 | if (fun && is_overloaded_fn (fun)) | |
2d76680f PC |
8204 | { |
8205 | if (TREE_CODE (fun) == FUNCTION_DECL) | |
8206 | { | |
8207 | if (builtin_valid_in_constant_expr_p (fun)) | |
8208 | return true; | |
8209 | if (!DECL_DECLARED_CONSTEXPR_P (fun) | |
8210 | /* Allow any built-in function; if the expansion | |
8211 | isn't constant, we'll deal with that then. */ | |
8e007055 | 8212 | && !fndecl_built_in_p (fun) |
b04445d4 | 8213 | /* In C++20, replaceable global allocation functions |
8e007055 | 8214 | are constant expressions. */ |
8412b939 JJ |
8215 | && (!cxx_replaceable_global_alloc_fn (fun) |
8216 | || TREE_CODE (t) != CALL_EXPR | |
8217 | || (!CALL_FROM_NEW_OR_DELETE_P (t) | |
8218 | && (current_function_decl == NULL_TREE | |
8219 | || !is_std_allocator_allocate | |
8220 | (current_function_decl)))) | |
cf650568 JJ |
8221 | /* Allow placement new in std::construct_at. */ |
8222 | && (!cxx_placement_new_fn (fun) | |
8412b939 | 8223 | || TREE_CODE (t) != CALL_EXPR |
cf650568 | 8224 | || current_function_decl == NULL_TREE |
22edf943 MP |
8225 | || !is_std_construct_at (current_function_decl)) |
8226 | && !cxx_dynamic_cast_fn_p (fun)) | |
2d76680f PC |
8227 | { |
8228 | if (flags & tf_error) | |
8229 | { | |
84fa214d | 8230 | error_at (loc, "call to non-%<constexpr%> function %qD", |
e40b6fc7 | 8231 | fun); |
2d76680f PC |
8232 | explain_invalid_constexpr_fn (fun); |
8233 | } | |
8234 | return false; | |
8235 | } | |
8236 | /* A call to a non-static member function takes the address | |
8237 | of the object as the first argument. But in a constant | |
8238 | expression the address will be folded away, so look | |
8239 | through it now. */ | |
8240 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) | |
8241 | && !DECL_CONSTRUCTOR_P (fun)) | |
8242 | { | |
8243 | tree x = get_nth_callarg (t, 0); | |
8244 | if (is_this_parameter (x)) | |
3e605b20 | 8245 | return true; |
a0ab7ccd JM |
8246 | /* Don't require an immediately constant value, as |
8247 | constexpr substitution might not use the value. */ | |
8248 | bool sub_now = false; | |
8249 | if (!potential_constant_expression_1 (x, rval, strict, | |
3075affd JM |
8250 | sub_now, flags, |
8251 | jump_target)) | |
2d76680f PC |
8252 | return false; |
8253 | i = 1; | |
8254 | } | |
8255 | } | |
8256 | else | |
8257 | { | |
69eb4fde | 8258 | if (!RECUR (fun, true)) |
2d76680f PC |
8259 | return false; |
8260 | fun = get_first_fn (fun); | |
8261 | } | |
8262 | /* Skip initial arguments to base constructors. */ | |
8263 | if (DECL_BASE_CONSTRUCTOR_P (fun)) | |
8264 | i = num_artificial_parms_for (fun); | |
8265 | fun = DECL_ORIGIN (fun); | |
8266 | } | |
44a845ca | 8267 | else if (fun) |
2d76680f | 8268 | { |
69eb4fde | 8269 | if (RECUR (fun, rval)) |
2d76680f PC |
8270 | /* Might end up being a constant function pointer. */; |
8271 | else | |
8272 | return false; | |
8273 | } | |
8274 | for (; i < nargs; ++i) | |
8275 | { | |
8276 | tree x = get_nth_callarg (t, i); | |
c3c29ba5 JM |
8277 | /* In a template, reference arguments haven't been converted to |
8278 | REFERENCE_TYPE and we might not even know if the parameter | |
8279 | is a reference, so accept lvalue constants too. */ | |
8280 | bool rv = processing_template_decl ? any : rval; | |
a0ab7ccd JM |
8281 | /* Don't require an immediately constant value, as constexpr |
8282 | substitution might not use the value of the argument. */ | |
8283 | bool sub_now = false; | |
8284 | if (!potential_constant_expression_1 (x, rv, strict, | |
3075affd | 8285 | sub_now, flags, jump_target)) |
2d76680f PC |
8286 | return false; |
8287 | } | |
8288 | return true; | |
8289 | } | |
8290 | ||
8291 | case NON_LVALUE_EXPR: | |
8292 | /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to | |
8293 | -- an lvalue of integral type that refers to a non-volatile | |
8294 | const variable or static data member initialized with | |
8295 | constant expressions, or | |
8296 | ||
8297 | -- an lvalue of literal type that refers to non-volatile | |
8298 | object defined with constexpr, or that refers to a | |
8299 | sub-object of such an object; */ | |
69eb4fde | 8300 | return RECUR (TREE_OPERAND (t, 0), rval); |
2d76680f PC |
8301 | |
8302 | case VAR_DECL: | |
70f40fea | 8303 | if (DECL_HAS_VALUE_EXPR_P (t)) |
c1051bf7 | 8304 | { |
68ad1bf7 | 8305 | if (now && is_normal_capture_proxy (t)) |
c1051bf7 JM |
8306 | { |
8307 | /* -- in a lambda-expression, a reference to this or to a | |
8308 | variable with automatic storage duration defined outside that | |
8309 | lambda-expression, where the reference would be an | |
8310 | odr-use. */ | |
04b89192 JM |
8311 | |
8312 | if (want_rval) | |
8313 | /* Since we're doing an lvalue-rvalue conversion, this might | |
8314 | not be an odr-use, so evaluate the variable directly. */ | |
8315 | return RECUR (DECL_CAPTURED_VARIABLE (t), rval); | |
8316 | ||
c1051bf7 JM |
8317 | if (flags & tf_error) |
8318 | { | |
8319 | tree cap = DECL_CAPTURED_VARIABLE (t); | |
8320 | error ("lambda capture of %qE is not a constant expression", | |
8321 | cap); | |
04b89192 | 8322 | if (decl_constant_var_p (cap)) |
c1051bf7 JM |
8323 | inform (input_location, "because it is used as a glvalue"); |
8324 | } | |
8325 | return false; | |
8326 | } | |
c2856cee MP |
8327 | /* Treat __PRETTY_FUNCTION__ inside a template function as |
8328 | potentially-constant. */ | |
8329 | else if (DECL_PRETTY_FUNCTION_P (t) | |
8330 | && DECL_VALUE_EXPR (t) == error_mark_node) | |
8331 | return true; | |
c1051bf7 JM |
8332 | return RECUR (DECL_VALUE_EXPR (t), rval); |
8333 | } | |
69eb4fde | 8334 | if (want_rval |
98e5a19a | 8335 | && !var_in_maybe_constexpr_fn (t) |
7dc2b4a2 | 8336 | && !type_dependent_expression_p (t) |
4b691b13 | 8337 | && !decl_maybe_constant_var_p (t) |
69eb4fde JM |
8338 | && (strict |
8339 | || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t)) | |
4b691b13 JM |
8340 | || (DECL_INITIAL (t) |
8341 | && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))) | |
7dc2b4a2 | 8342 | && COMPLETE_TYPE_P (TREE_TYPE (t)) |
dbcd32f8 | 8343 | && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
2d76680f PC |
8344 | { |
8345 | if (flags & tf_error) | |
6821245b | 8346 | non_const_var_error (loc, t); |
2d76680f PC |
8347 | return false; |
8348 | } | |
8349 | return true; | |
8350 | ||
8351 | case NOP_EXPR: | |
ed3ea9f2 JJ |
8352 | if (REINTERPRET_CAST_P (t)) |
8353 | { | |
8354 | if (flags & tf_error) | |
a9c697b8 | 8355 | error_at (loc, "%<reinterpret_cast%> is not a constant expression"); |
ed3ea9f2 JJ |
8356 | return false; |
8357 | } | |
8358 | /* FALLTHRU */ | |
2d76680f PC |
8359 | case CONVERT_EXPR: |
8360 | case VIEW_CONVERT_EXPR: | |
8361 | /* -- a reinterpret_cast. FIXME not implemented, and this rule | |
8362 | may change to something more specific to type-punning (DR 1312). */ | |
8363 | { | |
8364 | tree from = TREE_OPERAND (t, 0); | |
dfd7fdca DM |
8365 | if (location_wrapper_p (t)) |
8366 | return (RECUR (from, want_rval)); | |
8367 | if (INDIRECT_TYPE_P (TREE_TYPE (t))) | |
2d76680f | 8368 | { |
dfd7fdca DM |
8369 | STRIP_ANY_LOCATION_WRAPPER (from); |
8370 | if (TREE_CODE (from) == INTEGER_CST | |
8371 | && !integer_zerop (from)) | |
8372 | { | |
8373 | if (flags & tf_error) | |
a9c697b8 MS |
8374 | error_at (loc, |
8375 | "%<reinterpret_cast%> from integer to pointer"); | |
dfd7fdca DM |
8376 | return false; |
8377 | } | |
2d76680f | 8378 | } |
69eb4fde | 8379 | return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR)); |
2d76680f PC |
8380 | } |
8381 | ||
be845b04 JJ |
8382 | case ADDRESSOF_EXPR: |
8383 | /* This is like ADDR_EXPR, except it won't form pointer-to-member. */ | |
8384 | t = TREE_OPERAND (t, 0); | |
8385 | goto handle_addr_expr; | |
8386 | ||
2d76680f PC |
8387 | case ADDR_EXPR: |
8388 | /* -- a unary operator & that is applied to an lvalue that | |
8389 | designates an object with thread or automatic storage | |
8390 | duration; */ | |
8391 | t = TREE_OPERAND (t, 0); | |
8392 | ||
8393 | if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t)) | |
8394 | /* A pointer-to-member constant. */ | |
8395 | return true; | |
8396 | ||
be845b04 | 8397 | handle_addr_expr: |
2d76680f PC |
8398 | #if 0 |
8399 | /* FIXME adjust when issue 1197 is fully resolved. For now don't do | |
8400 | any checking here, as we might dereference the pointer later. If | |
8401 | we remove this code, also remove check_automatic_or_tls. */ | |
8402 | i = check_automatic_or_tls (t); | |
8403 | if (i == ck_ok) | |
8404 | return true; | |
8405 | if (i == ck_bad) | |
8406 | { | |
8407 | if (flags & tf_error) | |
8408 | error ("address-of an object %qE with thread local or " | |
8409 | "automatic storage is not a constant expression", t); | |
8410 | return false; | |
8411 | } | |
8412 | #endif | |
69eb4fde | 8413 | return RECUR (t, any); |
2d76680f PC |
8414 | |
8415 | case COMPONENT_REF: | |
2d76680f PC |
8416 | case ARROW_EXPR: |
8417 | case OFFSET_REF: | |
8418 | /* -- a class member access unless its postfix-expression is | |
8419 | of literal type or of pointer to literal type. */ | |
8420 | /* This test would be redundant, as it follows from the | |
8421 | postfix-expression being a potential constant expression. */ | |
19dedccf JM |
8422 | if (type_unknown_p (t)) |
8423 | return true; | |
8fda2c27 JM |
8424 | if (is_overloaded_fn (t)) |
8425 | /* In a template, a COMPONENT_REF of a function expresses ob.fn(), | |
8426 | which uses ob as an lvalue. */ | |
8427 | want_rval = false; | |
8428 | gcc_fallthrough (); | |
8429 | ||
8430 | case REALPART_EXPR: | |
8431 | case IMAGPART_EXPR: | |
8432 | case BIT_FIELD_REF: | |
69eb4fde | 8433 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
2d76680f PC |
8434 | |
8435 | case EXPR_PACK_EXPANSION: | |
69eb4fde | 8436 | return RECUR (PACK_EXPANSION_PATTERN (t), want_rval); |
2d76680f PC |
8437 | |
8438 | case INDIRECT_REF: | |
8439 | { | |
8440 | tree x = TREE_OPERAND (t, 0); | |
8441 | STRIP_NOPS (x); | |
948d0d91 | 8442 | if (is_this_parameter (x) && !is_capture_proxy (x)) |
2d76680f | 8443 | { |
5d4e573b | 8444 | if (!var_in_maybe_constexpr_fn (x)) |
2d76680f PC |
8445 | { |
8446 | if (flags & tf_error) | |
e40b6fc7 | 8447 | error_at (loc, "use of %<this%> in a constant expression"); |
2d76680f PC |
8448 | return false; |
8449 | } | |
2d76680f PC |
8450 | return true; |
8451 | } | |
69eb4fde | 8452 | return RECUR (x, rval); |
2d76680f PC |
8453 | } |
8454 | ||
60813a46 | 8455 | case STATEMENT_LIST: |
0f54cc9c JM |
8456 | for (tree stmt : tsi_range (t)) |
8457 | if (!RECUR (stmt, any)) | |
8458 | return false; | |
8459 | return true; | |
60813a46 JM |
8460 | |
8461 | case MODIFY_EXPR: | |
8462 | if (cxx_dialect < cxx14) | |
8463 | goto fail; | |
69eb4fde | 8464 | if (!RECUR (TREE_OPERAND (t, 0), any)) |
60813a46 | 8465 | return false; |
8e007055 JJ |
8466 | /* Just ignore clobbers. */ |
8467 | if (TREE_CLOBBER_P (TREE_OPERAND (t, 1))) | |
8468 | return true; | |
69eb4fde | 8469 | if (!RECUR (TREE_OPERAND (t, 1), rval)) |
60813a46 JM |
8470 | return false; |
8471 | return true; | |
8472 | ||
8473 | case MODOP_EXPR: | |
8474 | if (cxx_dialect < cxx14) | |
8475 | goto fail; | |
69eb4fde | 8476 | if (!RECUR (TREE_OPERAND (t, 0), rval)) |
60813a46 | 8477 | return false; |
69eb4fde | 8478 | if (!RECUR (TREE_OPERAND (t, 2), rval)) |
60813a46 JM |
8479 | return false; |
8480 | return true; | |
8481 | ||
60813a46 | 8482 | case DO_STMT: |
69eb4fde | 8483 | if (!RECUR (DO_COND (t), rval)) |
60813a46 | 8484 | return false; |
69eb4fde | 8485 | if (!RECUR (DO_BODY (t), any)) |
60813a46 | 8486 | return false; |
3075affd JM |
8487 | if (breaks (jump_target) || continues (jump_target)) |
8488 | *jump_target = NULL_TREE; | |
60813a46 JM |
8489 | return true; |
8490 | ||
8491 | case FOR_STMT: | |
69eb4fde | 8492 | if (!RECUR (FOR_INIT_STMT (t), any)) |
60813a46 | 8493 | return false; |
8e9558f0 MP |
8494 | tmp = FOR_COND (t); |
8495 | if (!RECUR (tmp, rval)) | |
60813a46 | 8496 | return false; |
8e9558f0 MP |
8497 | if (tmp) |
8498 | { | |
8499 | if (!processing_template_decl) | |
8500 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
a385474c MP |
8501 | /* If we couldn't evaluate the condition, it might not ever be |
8502 | true. */ | |
8503 | if (!integer_onep (tmp)) | |
0fb7aa20 JJ |
8504 | { |
8505 | /* Before returning true, check if the for body can contain | |
8506 | a return. */ | |
8507 | hash_set<tree> pset; | |
8508 | check_for_return_continue_data data = { &pset, NULL_TREE, | |
8509 | NULL_TREE }; | |
8510 | if (tree ret_expr | |
8511 | = cp_walk_tree (&FOR_BODY (t), check_for_return_continue, | |
8512 | &data, &pset)) | |
8513 | *jump_target = ret_expr; | |
8514 | return true; | |
8515 | } | |
8e9558f0 | 8516 | } |
69eb4fde | 8517 | if (!RECUR (FOR_EXPR (t), any)) |
60813a46 | 8518 | return false; |
69eb4fde | 8519 | if (!RECUR (FOR_BODY (t), any)) |
60813a46 | 8520 | return false; |
3075affd JM |
8521 | if (breaks (jump_target) || continues (jump_target)) |
8522 | *jump_target = NULL_TREE; | |
60813a46 JM |
8523 | return true; |
8524 | ||
98e5a19a | 8525 | case RANGE_FOR_STMT: |
8112667c MP |
8526 | if (!RECUR (RANGE_FOR_INIT_STMT (t), any)) |
8527 | return false; | |
98e5a19a JM |
8528 | if (!RECUR (RANGE_FOR_EXPR (t), any)) |
8529 | return false; | |
8530 | if (!RECUR (RANGE_FOR_BODY (t), any)) | |
8531 | return false; | |
3075affd JM |
8532 | if (breaks (jump_target) || continues (jump_target)) |
8533 | *jump_target = NULL_TREE; | |
98e5a19a JM |
8534 | return true; |
8535 | ||
60813a46 | 8536 | case WHILE_STMT: |
8e9558f0 MP |
8537 | tmp = WHILE_COND (t); |
8538 | if (!RECUR (tmp, rval)) | |
60813a46 | 8539 | return false; |
8e9558f0 MP |
8540 | if (!processing_template_decl) |
8541 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
a385474c MP |
8542 | /* If we couldn't evaluate the condition, it might not ever be true. */ |
8543 | if (!integer_onep (tmp)) | |
0fb7aa20 JJ |
8544 | { |
8545 | /* Before returning true, check if the while body can contain | |
8546 | a return. */ | |
8547 | hash_set<tree> pset; | |
8548 | check_for_return_continue_data data = { &pset, NULL_TREE, | |
8549 | NULL_TREE }; | |
8550 | if (tree ret_expr | |
8551 | = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue, | |
8552 | &data, &pset)) | |
8553 | *jump_target = ret_expr; | |
8554 | return true; | |
8555 | } | |
69eb4fde | 8556 | if (!RECUR (WHILE_BODY (t), any)) |
60813a46 | 8557 | return false; |
3075affd JM |
8558 | if (breaks (jump_target) || continues (jump_target)) |
8559 | *jump_target = NULL_TREE; | |
60813a46 JM |
8560 | return true; |
8561 | ||
8562 | case SWITCH_STMT: | |
69eb4fde | 8563 | if (!RECUR (SWITCH_STMT_COND (t), rval)) |
60813a46 | 8564 | return false; |
ce965730 | 8565 | /* FIXME we don't check SWITCH_STMT_BODY currently, because even |
c7a53bdb JJ |
8566 | unreachable labels would be checked and it is enough if there is |
8567 | a single switch cond value for which it is a valid constant | |
8568 | expression. We need to check if there are any RETURN_EXPRs | |
8569 | or CONTINUE_STMTs inside of the body though, as in that case | |
8570 | we need to set *jump_target. */ | |
8571 | else | |
8572 | { | |
8573 | hash_set<tree> pset; | |
0fb7aa20 JJ |
8574 | check_for_return_continue_data data = { &pset, NULL_TREE, |
8575 | NULL_TREE }; | |
c7a53bdb JJ |
8576 | if (tree ret_expr |
8577 | = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue, | |
8578 | &data, &pset)) | |
8579 | /* The switch might return. */ | |
8580 | *jump_target = ret_expr; | |
8581 | else if (data.continue_stmt) | |
8582 | /* The switch can't return, but might continue. */ | |
8583 | *jump_target = data.continue_stmt; | |
8584 | } | |
60813a46 JM |
8585 | return true; |
8586 | ||
58611fb6 | 8587 | case STMT_EXPR: |
69eb4fde | 8588 | return RECUR (STMT_EXPR_STMT (t), rval); |
58611fb6 | 8589 | |
2d76680f | 8590 | case LAMBDA_EXPR: |
899ac3b8 JM |
8591 | if (cxx_dialect >= cxx17) |
8592 | /* In C++17 lambdas can be constexpr, don't give up yet. */ | |
8593 | return true; | |
8594 | else if (flags & tf_error) | |
8595 | error_at (loc, "lambda-expression is not a constant expression " | |
8596 | "before C++17"); | |
8597 | return false; | |
8598 | ||
2d76680f PC |
8599 | case DYNAMIC_CAST_EXPR: |
8600 | case PSEUDO_DTOR_EXPR: | |
2d76680f PC |
8601 | case NEW_EXPR: |
8602 | case VEC_NEW_EXPR: | |
8603 | case DELETE_EXPR: | |
8604 | case VEC_DELETE_EXPR: | |
8605 | case THROW_EXPR: | |
e40b6fc7 JJ |
8606 | case OMP_PARALLEL: |
8607 | case OMP_TASK: | |
8608 | case OMP_FOR: | |
897064e2 | 8609 | case OMP_SIMD: |
e40b6fc7 JJ |
8610 | case OMP_DISTRIBUTE: |
8611 | case OMP_TASKLOOP: | |
d81ab49d | 8612 | case OMP_LOOP: |
e40b6fc7 JJ |
8613 | case OMP_TEAMS: |
8614 | case OMP_TARGET_DATA: | |
8615 | case OMP_TARGET: | |
8616 | case OMP_SECTIONS: | |
8617 | case OMP_ORDERED: | |
8618 | case OMP_CRITICAL: | |
8619 | case OMP_SINGLE: | |
8620 | case OMP_SECTION: | |
8621 | case OMP_MASTER: | |
8622 | case OMP_TASKGROUP: | |
8623 | case OMP_TARGET_UPDATE: | |
8624 | case OMP_TARGET_ENTER_DATA: | |
8625 | case OMP_TARGET_EXIT_DATA: | |
2d76680f PC |
8626 | case OMP_ATOMIC: |
8627 | case OMP_ATOMIC_READ: | |
8628 | case OMP_ATOMIC_CAPTURE_OLD: | |
8629 | case OMP_ATOMIC_CAPTURE_NEW: | |
28567c40 | 8630 | case OMP_DEPOBJ: |
e40b6fc7 JJ |
8631 | case OACC_PARALLEL: |
8632 | case OACC_KERNELS: | |
62aee289 | 8633 | case OACC_SERIAL: |
e40b6fc7 JJ |
8634 | case OACC_DATA: |
8635 | case OACC_HOST_DATA: | |
8636 | case OACC_LOOP: | |
8637 | case OACC_CACHE: | |
8638 | case OACC_DECLARE: | |
8639 | case OACC_ENTER_DATA: | |
8640 | case OACC_EXIT_DATA: | |
8641 | case OACC_UPDATE: | |
2d76680f PC |
8642 | /* GCC internal stuff. */ |
8643 | case VA_ARG_EXPR: | |
2d76680f | 8644 | case TRANSACTION_EXPR: |
81b6a6c5 | 8645 | case AT_ENCODE_EXPR: |
60813a46 | 8646 | fail: |
2d76680f | 8647 | if (flags & tf_error) |
e40b6fc7 | 8648 | error_at (loc, "expression %qE is not a constant expression", t); |
2d76680f PC |
8649 | return false; |
8650 | ||
529bc410 | 8651 | case ASM_EXPR: |
7c814975 MP |
8652 | if (flags & tf_error) |
8653 | inline_asm_in_constexpr_error (loc); | |
8654 | return false; | |
529bc410 | 8655 | |
bf8d8309 | 8656 | case OBJ_TYPE_REF: |
b04445d4 JM |
8657 | if (cxx_dialect >= cxx20) |
8658 | /* In C++20 virtual calls can be constexpr, don't give up yet. */ | |
bf8d8309 MP |
8659 | return true; |
8660 | else if (flags & tf_error) | |
a9c697b8 | 8661 | error_at (loc, |
b04445d4 | 8662 | "virtual functions cannot be %<constexpr%> before C++20"); |
bf8d8309 MP |
8663 | return false; |
8664 | ||
2d76680f | 8665 | case TYPEID_EXPR: |
66acfb80 MP |
8666 | /* In C++20, a typeid expression whose operand is of polymorphic |
8667 | class type can be constexpr. */ | |
2d76680f PC |
8668 | { |
8669 | tree e = TREE_OPERAND (t, 0); | |
b04445d4 | 8670 | if (cxx_dialect < cxx20 |
66acfb80 MP |
8671 | && strict |
8672 | && !TYPE_P (e) | |
8673 | && !type_dependent_expression_p (e) | |
2d76680f PC |
8674 | && TYPE_POLYMORPHIC_P (TREE_TYPE (e))) |
8675 | { | |
8676 | if (flags & tf_error) | |
a9c697b8 | 8677 | error_at (loc, "%<typeid%> is not a constant expression " |
e40b6fc7 | 8678 | "because %qE is of polymorphic type", e); |
2d76680f PC |
8679 | return false; |
8680 | } | |
8681 | return true; | |
8682 | } | |
8683 | ||
1af4ebf5 | 8684 | case POINTER_DIFF_EXPR: |
2d76680f | 8685 | case MINUS_EXPR: |
2d76680f PC |
8686 | want_rval = true; |
8687 | goto binary; | |
8688 | ||
8689 | case LT_EXPR: | |
8690 | case LE_EXPR: | |
8691 | case GT_EXPR: | |
8692 | case GE_EXPR: | |
8693 | case EQ_EXPR: | |
8694 | case NE_EXPR: | |
b7689b96 | 8695 | case SPACESHIP_EXPR: |
2d76680f PC |
8696 | want_rval = true; |
8697 | goto binary; | |
8698 | ||
60813a46 JM |
8699 | case PREINCREMENT_EXPR: |
8700 | case POSTINCREMENT_EXPR: | |
8701 | case PREDECREMENT_EXPR: | |
8702 | case POSTDECREMENT_EXPR: | |
8703 | if (cxx_dialect < cxx14) | |
8704 | goto fail; | |
8705 | goto unary; | |
8706 | ||
2d76680f PC |
8707 | case BIT_NOT_EXPR: |
8708 | /* A destructor. */ | |
8709 | if (TYPE_P (TREE_OPERAND (t, 0))) | |
8710 | return true; | |
191816a3 | 8711 | /* fall through. */ |
2d76680f | 8712 | |
2d76680f PC |
8713 | case CONJ_EXPR: |
8714 | case SAVE_EXPR: | |
8715 | case FIX_TRUNC_EXPR: | |
8716 | case FLOAT_EXPR: | |
8717 | case NEGATE_EXPR: | |
8718 | case ABS_EXPR: | |
e197e64e | 8719 | case ABSU_EXPR: |
2d76680f PC |
8720 | case TRUTH_NOT_EXPR: |
8721 | case FIXED_CONVERT_EXPR: | |
8722 | case UNARY_PLUS_EXPR: | |
4856a1f0 PC |
8723 | case UNARY_LEFT_FOLD_EXPR: |
8724 | case UNARY_RIGHT_FOLD_EXPR: | |
60813a46 | 8725 | unary: |
69eb4fde | 8726 | return RECUR (TREE_OPERAND (t, 0), rval); |
2d76680f PC |
8727 | |
8728 | case CAST_EXPR: | |
8729 | case CONST_CAST_EXPR: | |
8730 | case STATIC_CAST_EXPR: | |
8731 | case REINTERPRET_CAST_EXPR: | |
8732 | case IMPLICIT_CONV_EXPR: | |
8733 | if (cxx_dialect < cxx11 | |
8734 | && !dependent_type_p (TREE_TYPE (t)) | |
8735 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))) | |
8736 | /* In C++98, a conversion to non-integral type can't be part of a | |
8737 | constant expression. */ | |
8738 | { | |
8739 | if (flags & tf_error) | |
e40b6fc7 JJ |
8740 | error_at (loc, |
8741 | "cast to non-integral type %qT in a constant expression", | |
8742 | TREE_TYPE (t)); | |
2d76680f PC |
8743 | return false; |
8744 | } | |
fe0604d3 MP |
8745 | /* This might be a conversion from a class to a (potentially) literal |
8746 | type. Let's consider it potentially constant since the conversion | |
8747 | might be a constexpr user-defined conversion. */ | |
8748 | else if (cxx_dialect >= cxx11 | |
8749 | && (dependent_type_p (TREE_TYPE (t)) | |
8750 | || !COMPLETE_TYPE_P (TREE_TYPE (t)) | |
8751 | || literal_type_p (TREE_TYPE (t))) | |
8752 | && TREE_OPERAND (t, 0)) | |
8753 | { | |
8754 | tree type = TREE_TYPE (TREE_OPERAND (t, 0)); | |
8755 | /* If this is a dependent type, it could end up being a class | |
8756 | with conversions. */ | |
8757 | if (type == NULL_TREE || WILDCARD_TYPE_P (type)) | |
8758 | return true; | |
8759 | /* Or a non-dependent class which has conversions. */ | |
8760 | else if (CLASS_TYPE_P (type) | |
8761 | && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type))) | |
8762 | return true; | |
8763 | } | |
2d76680f | 8764 | |
69eb4fde | 8765 | return (RECUR (TREE_OPERAND (t, 0), |
9f613f06 | 8766 | !TYPE_REF_P (TREE_TYPE (t)))); |
2d76680f | 8767 | |
60813a46 | 8768 | case BIND_EXPR: |
69eb4fde | 8769 | return RECUR (BIND_EXPR_BODY (t), want_rval); |
60813a46 | 8770 | |
60813a46 JM |
8771 | case CLEANUP_POINT_EXPR: |
8772 | case MUST_NOT_THROW_EXPR: | |
8773 | case TRY_CATCH_EXPR: | |
769430b2 | 8774 | case TRY_BLOCK: |
60813a46 JM |
8775 | case EH_SPEC_BLOCK: |
8776 | case EXPR_STMT: | |
2d76680f PC |
8777 | case PAREN_EXPR: |
8778 | case NON_DEPENDENT_EXPR: | |
8779 | /* For convenience. */ | |
f937929e JM |
8780 | case LOOP_EXPR: |
8781 | case EXIT_EXPR: | |
69eb4fde | 8782 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
2d76680f | 8783 | |
98e5a19a JM |
8784 | case DECL_EXPR: |
8785 | tmp = DECL_EXPR_DECL (t); | |
8786 | if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)) | |
8787 | { | |
8892d532 | 8788 | if (CP_DECL_THREAD_LOCAL_P (tmp)) |
98e5a19a JM |
8789 | { |
8790 | if (flags & tf_error) | |
8791 | error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared " | |
8892d532 | 8792 | "%<thread_local%> in %<constexpr%> context", tmp); |
98e5a19a JM |
8793 | return false; |
8794 | } | |
8892d532 | 8795 | else if (TREE_STATIC (tmp)) |
98e5a19a JM |
8796 | { |
8797 | if (flags & tf_error) | |
8798 | error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared " | |
8892d532 | 8799 | "%<static%> in %<constexpr%> context", tmp); |
98e5a19a JM |
8800 | return false; |
8801 | } | |
3885527d PC |
8802 | else if (!check_for_uninitialized_const_var |
8803 | (tmp, /*constexpr_context_p=*/true, flags)) | |
8804 | return false; | |
98e5a19a JM |
8805 | } |
8806 | return RECUR (tmp, want_rval); | |
8807 | ||
769430b2 JM |
8808 | case TRY_FINALLY_EXPR: |
8809 | return (RECUR (TREE_OPERAND (t, 0), want_rval) | |
8810 | && RECUR (TREE_OPERAND (t, 1), any)); | |
8811 | ||
2d76680f | 8812 | case SCOPE_REF: |
69eb4fde | 8813 | return RECUR (TREE_OPERAND (t, 1), want_rval); |
2d76680f PC |
8814 | |
8815 | case TARGET_EXPR: | |
d3a9902e JM |
8816 | if (!TARGET_EXPR_DIRECT_INIT_P (t) |
8817 | && !literal_type_p (TREE_TYPE (t))) | |
2d76680f PC |
8818 | { |
8819 | if (flags & tf_error) | |
8820 | { | |
097f82ec | 8821 | auto_diagnostic_group d; |
e40b6fc7 JJ |
8822 | error_at (loc, "temporary of non-literal type %qT in a " |
8823 | "constant expression", TREE_TYPE (t)); | |
2d76680f PC |
8824 | explain_non_literal_class (TREE_TYPE (t)); |
8825 | } | |
8826 | return false; | |
8827 | } | |
191816a3 | 8828 | /* FALLTHRU */ |
2d76680f | 8829 | case INIT_EXPR: |
69eb4fde | 8830 | return RECUR (TREE_OPERAND (t, 1), rval); |
2d76680f PC |
8831 | |
8832 | case CONSTRUCTOR: | |
8833 | { | |
8834 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); | |
8835 | constructor_elt *ce; | |
8836 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
69eb4fde | 8837 | if (!RECUR (ce->value, want_rval)) |
2d76680f PC |
8838 | return false; |
8839 | return true; | |
8840 | } | |
8841 | ||
8842 | case TREE_LIST: | |
8843 | { | |
8844 | gcc_assert (TREE_PURPOSE (t) == NULL_TREE | |
8845 | || DECL_P (TREE_PURPOSE (t))); | |
69eb4fde | 8846 | if (!RECUR (TREE_VALUE (t), want_rval)) |
2d76680f PC |
8847 | return false; |
8848 | if (TREE_CHAIN (t) == NULL_TREE) | |
8849 | return true; | |
69eb4fde | 8850 | return RECUR (TREE_CHAIN (t), want_rval); |
2d76680f PC |
8851 | } |
8852 | ||
8853 | case TRUNC_DIV_EXPR: | |
8854 | case CEIL_DIV_EXPR: | |
8855 | case FLOOR_DIV_EXPR: | |
8856 | case ROUND_DIV_EXPR: | |
8857 | case TRUNC_MOD_EXPR: | |
8858 | case CEIL_MOD_EXPR: | |
8859 | case ROUND_MOD_EXPR: | |
8860 | { | |
8861 | tree denom = TREE_OPERAND (t, 1); | |
69eb4fde | 8862 | if (!RECUR (denom, rval)) |
2d76680f PC |
8863 | return false; |
8864 | /* We can't call cxx_eval_outermost_constant_expr on an expression | |
234bef96 | 8865 | that hasn't been through instantiate_non_dependent_expr yet. */ |
2d76680f PC |
8866 | if (!processing_template_decl) |
8867 | denom = cxx_eval_outermost_constant_expr (denom, true); | |
8868 | if (integer_zerop (denom)) | |
8869 | { | |
8870 | if (flags & tf_error) | |
64d6d399 | 8871 | error ("division by zero is not a constant expression"); |
2d76680f PC |
8872 | return false; |
8873 | } | |
8874 | else | |
8875 | { | |
8876 | want_rval = true; | |
69eb4fde | 8877 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
2d76680f PC |
8878 | } |
8879 | } | |
8880 | ||
8881 | case COMPOUND_EXPR: | |
8882 | { | |
8883 | /* check_return_expr sometimes wraps a TARGET_EXPR in a | |
46fdced6 | 8884 | COMPOUND_EXPR; don't get confused. */ |
2d76680f PC |
8885 | tree op0 = TREE_OPERAND (t, 0); |
8886 | tree op1 = TREE_OPERAND (t, 1); | |
8887 | STRIP_NOPS (op1); | |
46fdced6 | 8888 | if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) |
69eb4fde | 8889 | return RECUR (op0, want_rval); |
2d76680f PC |
8890 | else |
8891 | goto binary; | |
8892 | } | |
8893 | ||
8894 | /* If the first operand is the non-short-circuit constant, look at | |
8895 | the second operand; otherwise we only care about the first one for | |
8896 | potentiality. */ | |
8897 | case TRUTH_AND_EXPR: | |
8898 | case TRUTH_ANDIF_EXPR: | |
8899 | tmp = boolean_true_node; | |
8900 | goto truth; | |
8901 | case TRUTH_OR_EXPR: | |
8902 | case TRUTH_ORIF_EXPR: | |
8903 | tmp = boolean_false_node; | |
8904 | truth: | |
8905 | { | |
9927ecbb PP |
8906 | tree op0 = TREE_OPERAND (t, 0); |
8907 | tree op1 = TREE_OPERAND (t, 1); | |
8908 | if (!RECUR (op0, rval)) | |
2d76680f | 8909 | return false; |
9927ecbb PP |
8910 | if (!(flags & tf_error) && RECUR (op1, rval)) |
8911 | /* When quiet, try to avoid expensive trial evaluation by first | |
8912 | checking potentiality of the second operand. */ | |
8913 | return true; | |
2d76680f | 8914 | if (!processing_template_decl) |
9927ecbb PP |
8915 | op0 = cxx_eval_outermost_constant_expr (op0, true); |
8916 | if (tree_int_cst_equal (op0, tmp)) | |
8917 | return (flags & tf_error) ? RECUR (op1, rval) : false; | |
2d76680f PC |
8918 | else |
8919 | return true; | |
8920 | } | |
8921 | ||
8922 | case PLUS_EXPR: | |
8923 | case MULT_EXPR: | |
8924 | case POINTER_PLUS_EXPR: | |
8925 | case RDIV_EXPR: | |
8926 | case EXACT_DIV_EXPR: | |
8927 | case MIN_EXPR: | |
8928 | case MAX_EXPR: | |
8929 | case LSHIFT_EXPR: | |
8930 | case RSHIFT_EXPR: | |
81fa6d73 JJ |
8931 | case LROTATE_EXPR: |
8932 | case RROTATE_EXPR: | |
2d76680f PC |
8933 | case BIT_IOR_EXPR: |
8934 | case BIT_XOR_EXPR: | |
8935 | case BIT_AND_EXPR: | |
8936 | case TRUTH_XOR_EXPR: | |
8937 | case UNORDERED_EXPR: | |
8938 | case ORDERED_EXPR: | |
8939 | case UNLT_EXPR: | |
8940 | case UNLE_EXPR: | |
8941 | case UNGT_EXPR: | |
8942 | case UNGE_EXPR: | |
8943 | case UNEQ_EXPR: | |
8944 | case LTGT_EXPR: | |
8945 | case RANGE_EXPR: | |
8946 | case COMPLEX_EXPR: | |
8947 | want_rval = true; | |
8948 | /* Fall through. */ | |
8949 | case ARRAY_REF: | |
8950 | case ARRAY_RANGE_REF: | |
8951 | case MEMBER_REF: | |
8952 | case DOTSTAR_EXPR: | |
41b38772 | 8953 | case MEM_REF: |
4856a1f0 PC |
8954 | case BINARY_LEFT_FOLD_EXPR: |
8955 | case BINARY_RIGHT_FOLD_EXPR: | |
2d76680f PC |
8956 | binary: |
8957 | for (i = 0; i < 2; ++i) | |
69eb4fde | 8958 | if (!RECUR (TREE_OPERAND (t, i), want_rval)) |
2d76680f PC |
8959 | return false; |
8960 | return true; | |
8961 | ||
2d76680f PC |
8962 | case VEC_PERM_EXPR: |
8963 | for (i = 0; i < 3; ++i) | |
69eb4fde | 8964 | if (!RECUR (TREE_OPERAND (t, i), true)) |
2d76680f PC |
8965 | return false; |
8966 | return true; | |
8967 | ||
8968 | case COND_EXPR: | |
b04445d4 | 8969 | if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20) |
d5bcd6d4 JM |
8970 | { |
8971 | if (flags & tf_error) | |
e40b6fc7 | 8972 | error_at (loc, "%<delete[]%> is not a constant expression"); |
d5bcd6d4 JM |
8973 | return false; |
8974 | } | |
8975 | /* Fall through. */ | |
8976 | case IF_STMT: | |
2d76680f PC |
8977 | case VEC_COND_EXPR: |
8978 | /* If the condition is a known constant, we know which of the legs we | |
8979 | care about; otherwise we only require that the condition and | |
8980 | either of the legs be potentially constant. */ | |
8981 | tmp = TREE_OPERAND (t, 0); | |
69eb4fde | 8982 | if (!RECUR (tmp, rval)) |
2d76680f PC |
8983 | return false; |
8984 | if (!processing_template_decl) | |
8985 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
117c6426 JJ |
8986 | /* potential_constant_expression* isn't told if it is called for |
8987 | manifestly_const_eval or not, so for consteval if always | |
8988 | process both branches as if the condition is not a known | |
8989 | constant. */ | |
8990 | if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t)) | |
8991 | { | |
8992 | if (integer_zerop (tmp)) | |
8993 | return RECUR (TREE_OPERAND (t, 2), want_rval); | |
8994 | else if (TREE_CODE (tmp) == INTEGER_CST) | |
8995 | return RECUR (TREE_OPERAND (t, 1), want_rval); | |
8996 | } | |
0fb7aa20 | 8997 | tmp = *jump_target; |
2d76680f | 8998 | for (i = 1; i < 3; ++i) |
0fb7aa20 JJ |
8999 | { |
9000 | tree this_jump_target = tmp; | |
9001 | if (potential_constant_expression_1 (TREE_OPERAND (t, i), | |
9002 | want_rval, strict, now, | |
9003 | tf_none, &this_jump_target)) | |
9004 | { | |
9005 | if (returns (&this_jump_target)) | |
9006 | *jump_target = this_jump_target; | |
9007 | else if (!returns (jump_target)) | |
9008 | { | |
9009 | if (breaks (&this_jump_target) | |
9010 | || continues (&this_jump_target)) | |
9011 | *jump_target = this_jump_target; | |
9012 | if (i == 1) | |
9013 | { | |
9014 | /* If the then branch is potentially constant, but | |
9015 | does not return, check if the else branch | |
9016 | couldn't return, break or continue. */ | |
9017 | hash_set<tree> pset; | |
9018 | check_for_return_continue_data data = { &pset, NULL_TREE, | |
9019 | NULL_TREE }; | |
9020 | if (tree ret_expr | |
9021 | = cp_walk_tree (&TREE_OPERAND (t, 2), | |
9022 | check_for_return_continue, &data, | |
9023 | &pset)) | |
9024 | *jump_target = ret_expr; | |
9025 | else if (*jump_target == NULL_TREE) | |
9026 | { | |
9027 | if (data.continue_stmt) | |
9028 | *jump_target = data.continue_stmt; | |
9029 | else if (data.break_stmt) | |
9030 | *jump_target = data.break_stmt; | |
9031 | } | |
9032 | } | |
9033 | } | |
9034 | return true; | |
9035 | } | |
9036 | } | |
2d76680f | 9037 | if (flags & tf_error) |
e40b6fc7 | 9038 | error_at (loc, "expression %qE is not a constant expression", t); |
2d76680f PC |
9039 | return false; |
9040 | ||
9041 | case VEC_INIT_EXPR: | |
9042 | if (VEC_INIT_EXPR_IS_CONSTEXPR (t)) | |
9043 | return true; | |
9044 | if (flags & tf_error) | |
9045 | { | |
e40b6fc7 | 9046 | error_at (loc, "non-constant array initialization"); |
2d76680f PC |
9047 | diagnose_non_constexpr_vec_init (t); |
9048 | } | |
9049 | return false; | |
9050 | ||
133bc698 JM |
9051 | case TYPE_DECL: |
9052 | case TAG_DEFN: | |
9053 | /* We can see these in statement-expressions. */ | |
9054 | return true; | |
9055 | ||
baf9ebc8 | 9056 | case CLEANUP_STMT: |
1006c9d4 JJ |
9057 | if (!RECUR (CLEANUP_BODY (t), any)) |
9058 | return false; | |
9059 | if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any)) | |
9060 | return false; | |
9061 | return true; | |
9062 | ||
cda0a029 | 9063 | case EMPTY_CLASS_EXPR: |
46fdced6 | 9064 | return true; |
cda0a029 | 9065 | |
f937929e JM |
9066 | case GOTO_EXPR: |
9067 | { | |
9068 | tree *target = &TREE_OPERAND (t, 0); | |
ab3af181 JJ |
9069 | /* Gotos representing break and continue are OK. */ |
9070 | if (breaks (target) || continues (target)) | |
3075affd JM |
9071 | { |
9072 | *jump_target = *target; | |
9073 | return true; | |
9074 | } | |
ab3af181 | 9075 | if (flags & tf_error) |
e40b6fc7 | 9076 | error_at (loc, "%<goto%> is not a constant expression"); |
ab3af181 | 9077 | return false; |
f937929e JM |
9078 | } |
9079 | ||
6f20c42c MP |
9080 | case LABEL_EXPR: |
9081 | t = LABEL_EXPR_LABEL (t); | |
8892d532 | 9082 | if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23) |
6f20c42c MP |
9083 | return true; |
9084 | else if (flags & tf_error) | |
8892d532 JJ |
9085 | error_at (loc, "label definition in %<constexpr%> function only " |
9086 | "available with %<-std=c++2b%> or %<-std=gnu++2b%>"); | |
6f20c42c MP |
9087 | return false; |
9088 | ||
98e09245 | 9089 | case ANNOTATE_EXPR: |
98e09245 JJ |
9090 | return RECUR (TREE_OPERAND (t, 0), rval); |
9091 | ||
896048cf JJ |
9092 | case BIT_CAST_EXPR: |
9093 | return RECUR (TREE_OPERAND (t, 0), rval); | |
9094 | ||
49789fd0 IS |
9095 | /* Coroutine await, yield and return expressions are not. */ |
9096 | case CO_AWAIT_EXPR: | |
9097 | case CO_YIELD_EXPR: | |
9098 | case CO_RETURN_EXPR: | |
9099 | return false; | |
9100 | ||
d4c470c3 PP |
9101 | case NONTYPE_ARGUMENT_PACK: |
9102 | { | |
9103 | tree args = ARGUMENT_PACK_ARGS (t); | |
9104 | int len = TREE_VEC_LENGTH (args); | |
9105 | for (int i = 0; i < len; ++i) | |
9106 | if (!RECUR (TREE_VEC_ELT (args, i), any)) | |
9107 | return false; | |
9108 | return true; | |
9109 | } | |
9110 | ||
2d76680f | 9111 | default: |
878cffbd | 9112 | if (objc_non_constant_expr_p (t)) |
2d76680f PC |
9113 | return false; |
9114 | ||
9115 | sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t))); | |
ab3af181 | 9116 | gcc_unreachable (); |
2d76680f PC |
9117 | return false; |
9118 | } | |
69eb4fde | 9119 | #undef RECUR |
2d76680f PC |
9120 | } |
9121 | ||
3075affd JM |
9122 | bool |
9123 | potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, | |
9124 | tsubst_flags_t flags) | |
9125 | { | |
9927ecbb PP |
9126 | if (flags & tf_error) |
9127 | { | |
9128 | /* Check potentiality quietly first, as that could be performed more | |
9129 | efficiently in some cases (currently only for TRUTH_*_EXPR). If | |
9130 | that fails, replay the check noisily to give errors. */ | |
9131 | flags &= ~tf_error; | |
9132 | if (potential_constant_expression_1 (t, want_rval, strict, now, flags)) | |
9133 | return true; | |
9134 | flags |= tf_error; | |
9135 | } | |
9136 | ||
3075affd JM |
9137 | tree target = NULL_TREE; |
9138 | return potential_constant_expression_1 (t, want_rval, strict, now, | |
9139 | flags, &target); | |
9140 | } | |
9141 | ||
2d76680f PC |
9142 | /* The main entry point to the above. */ |
9143 | ||
9144 | bool | |
9145 | potential_constant_expression (tree t) | |
9146 | { | |
a0ab7ccd | 9147 | return potential_constant_expression_1 (t, false, true, false, tf_none); |
2d76680f PC |
9148 | } |
9149 | ||
9150 | /* As above, but require a constant rvalue. */ | |
9151 | ||
9152 | bool | |
9153 | potential_rvalue_constant_expression (tree t) | |
9154 | { | |
a0ab7ccd | 9155 | return potential_constant_expression_1 (t, true, true, false, tf_none); |
2d76680f PC |
9156 | } |
9157 | ||
9158 | /* Like above, but complain about non-constant expressions. */ | |
9159 | ||
9160 | bool | |
9161 | require_potential_constant_expression (tree t) | |
9162 | { | |
d8cff23f MP |
9163 | return potential_constant_expression_1 (t, false, true, false, |
9164 | tf_warning_or_error); | |
2d76680f PC |
9165 | } |
9166 | ||
9167 | /* Cross product of the above. */ | |
9168 | ||
9169 | bool | |
9170 | require_potential_rvalue_constant_expression (tree t) | |
9171 | { | |
d8cff23f MP |
9172 | return potential_constant_expression_1 (t, true, true, false, |
9173 | tf_warning_or_error); | |
9174 | } | |
9175 | ||
9176 | /* Like above, but don't consider PARM_DECL a potential_constant_expression. */ | |
9177 | ||
9178 | bool | |
9179 | require_rvalue_constant_expression (tree t) | |
9180 | { | |
9181 | return potential_constant_expression_1 (t, true, true, true, | |
9182 | tf_warning_or_error); | |
a0ab7ccd JM |
9183 | } |
9184 | ||
9185 | /* Like potential_constant_expression, but don't consider possible constexpr | |
9186 | substitution of the current function. That is, PARM_DECL qualifies under | |
9187 | potential_constant_expression, but not here. | |
9188 | ||
9189 | This is basically what you can check when any actual constant values might | |
9190 | be value-dependent. */ | |
9191 | ||
9192 | bool | |
9193 | is_constant_expression (tree t) | |
9194 | { | |
9195 | return potential_constant_expression_1 (t, false, true, true, tf_none); | |
9196 | } | |
9197 | ||
beb019d3 JM |
9198 | /* As above, but expect an rvalue. */ |
9199 | ||
9200 | bool | |
9201 | is_rvalue_constant_expression (tree t) | |
9202 | { | |
9203 | return potential_constant_expression_1 (t, true, true, true, tf_none); | |
9204 | } | |
9205 | ||
a0ab7ccd JM |
9206 | /* Like above, but complain about non-constant expressions. */ |
9207 | ||
9208 | bool | |
9209 | require_constant_expression (tree t) | |
9210 | { | |
9211 | return potential_constant_expression_1 (t, false, true, true, | |
9212 | tf_warning_or_error); | |
9213 | } | |
9214 | ||
9215 | /* Like is_constant_expression, but allow const variables that are not allowed | |
9216 | under constexpr rules. */ | |
9217 | ||
9218 | bool | |
9219 | is_static_init_expression (tree t) | |
9220 | { | |
9221 | return potential_constant_expression_1 (t, false, false, true, tf_none); | |
2d76680f PC |
9222 | } |
9223 | ||
eb07f187 JM |
9224 | /* Returns true if T is a potential constant expression that is not |
9225 | instantiation-dependent, and therefore a candidate for constant folding even | |
9226 | in a template. */ | |
9227 | ||
9228 | bool | |
a0ab7ccd | 9229 | is_nondependent_constant_expression (tree t) |
eb07f187 JM |
9230 | { |
9231 | return (!type_unknown_p (t) | |
a0ab7ccd | 9232 | && is_constant_expression (t) |
eb07f187 JM |
9233 | && !instantiation_dependent_expression_p (t)); |
9234 | } | |
9235 | ||
9236 | /* Returns true if T is a potential static initializer expression that is not | |
9237 | instantiation-dependent. */ | |
9238 | ||
9239 | bool | |
a0ab7ccd | 9240 | is_nondependent_static_init_expression (tree t) |
eb07f187 JM |
9241 | { |
9242 | return (!type_unknown_p (t) | |
a0ab7ccd | 9243 | && is_static_init_expression (t) |
eb07f187 JM |
9244 | && !instantiation_dependent_expression_p (t)); |
9245 | } | |
9246 | ||
97f3003f JM |
9247 | /* Finalize constexpr processing after parsing. */ |
9248 | ||
9249 | void | |
9250 | fini_constexpr (void) | |
9251 | { | |
9252 | /* The contexpr call and fundef copies tables are no longer needed. */ | |
9253 | constexpr_call_table = NULL; | |
9254 | fundef_copies_table = NULL; | |
9255 | } | |
9256 | ||
2d76680f | 9257 | #include "gt-cp-constexpr.h" |