]>
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 | ||
2249 | /* virtual_offset is only set in the presence of virtual bases, which make | |
2250 | the class non-literal, so we don't need to handle it here. */ | |
2251 | if (THUNK_VIRTUAL_OFFSET (thunk_fndecl)) | |
2252 | { | |
2253 | gcc_assert (!DECL_DECLARED_CONSTEXPR_P (function)); | |
2254 | if (!ctx->quiet) | |
2255 | { | |
2256 | error ("call to non-%<constexpr%> function %qD", function); | |
2257 | explain_invalid_constexpr_fn (function); | |
2258 | } | |
2259 | *non_constant_p = true; | |
2260 | return t; | |
2261 | } | |
2262 | ||
2263 | tree new_call = copy_node (t); | |
2264 | CALL_EXPR_FN (new_call) = function; | |
2265 | TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function)); | |
2266 | ||
2267 | tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl)); | |
2268 | ||
2269 | if (DECL_THIS_THUNK_P (thunk_fndecl)) | |
2270 | { | |
2271 | /* 'this'-adjusting thunk. */ | |
2272 | tree this_arg = CALL_EXPR_ARG (t, 0); | |
2273 | this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg), | |
2274 | this_arg, offset); | |
2275 | CALL_EXPR_ARG (new_call, 0) = this_arg; | |
2276 | } | |
2277 | else | |
2278 | /* Return-adjusting thunk. */ | |
2279 | new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call), | |
2280 | new_call, offset); | |
2281 | ||
2282 | return cxx_eval_constant_expression (ctx, new_call, lval, | |
2283 | non_constant_p, overflow_p); | |
2284 | } | |
2285 | ||
caf17f3a MP |
2286 | /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set |
2287 | its TREE_READONLY flag according to READONLY_P. Used for constexpr | |
2288 | 'tors to detect modifying const objects in a constexpr context. */ | |
2289 | ||
2290 | static void | |
2291 | cxx_set_object_constness (const constexpr_ctx *ctx, tree object, | |
2292 | bool readonly_p, bool *non_constant_p, | |
2293 | bool *overflow_p) | |
2294 | { | |
2295 | if (CLASS_TYPE_P (TREE_TYPE (object)) | |
2296 | && CP_TYPE_CONST_P (TREE_TYPE (object))) | |
2297 | { | |
2298 | /* Subobjects might not be stored in ctx->global->values but we | |
2299 | can get its CONSTRUCTOR by evaluating *this. */ | |
2300 | tree e = cxx_eval_constant_expression (ctx, object, /*lval*/false, | |
2301 | non_constant_p, overflow_p); | |
2302 | if (TREE_CODE (e) == CONSTRUCTOR && !*non_constant_p) | |
2303 | TREE_READONLY (e) = readonly_p; | |
2304 | } | |
2305 | } | |
2306 | ||
2d76680f PC |
2307 | /* Subroutine of cxx_eval_constant_expression. |
2308 | Evaluate the call expression tree T in the context of OLD_CALL expression | |
2309 | evaluation. */ | |
2310 | ||
2311 | static tree | |
3e605b20 | 2312 | cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, |
92a596e8 | 2313 | bool lval, |
2d76680f PC |
2314 | bool *non_constant_p, bool *overflow_p) |
2315 | { | |
861d4af8 AS |
2316 | /* Handle concept checks separately. */ |
2317 | if (concept_check_p (t)) | |
662ef5b5 | 2318 | return evaluate_concept_check (t); |
861d4af8 | 2319 | |
f9d0ca40 | 2320 | location_t loc = cp_expr_loc_or_input_loc (t); |
2d76680f | 2321 | tree fun = get_function_named_in_call (t); |
cce3ae91 | 2322 | constexpr_call new_call |
13de99bc | 2323 | = { NULL, NULL, NULL, 0, ctx->manifestly_const_eval }; |
7ffc7de5 | 2324 | int depth_ok; |
2d76680f | 2325 | |
0b274c17 | 2326 | if (fun == NULL_TREE) |
44a845ca MS |
2327 | return cxx_eval_internal_function (ctx, t, lval, |
2328 | non_constant_p, overflow_p); | |
0b274c17 | 2329 | |
2d76680f PC |
2330 | if (TREE_CODE (fun) != FUNCTION_DECL) |
2331 | { | |
2332 | /* Might be a constexpr function pointer. */ | |
2b3ab879 | 2333 | fun = cxx_eval_constant_expression (ctx, fun, |
92a596e8 | 2334 | /*lval*/false, non_constant_p, |
5a804683 | 2335 | overflow_p); |
2d76680f PC |
2336 | STRIP_NOPS (fun); |
2337 | if (TREE_CODE (fun) == ADDR_EXPR) | |
2338 | fun = TREE_OPERAND (fun, 0); | |
582d2481 JJ |
2339 | /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no |
2340 | indirection, the called expression is a pointer into the | |
2341 | virtual table which should contain FDESC_EXPR. Extract the | |
2342 | FUNCTION_DECL from there. */ | |
2343 | else if (TARGET_VTABLE_USES_DESCRIPTORS | |
2344 | && TREE_CODE (fun) == POINTER_PLUS_EXPR | |
2345 | && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR | |
2346 | && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST) | |
2347 | { | |
2348 | tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0); | |
2349 | if (VAR_P (d) | |
2350 | && DECL_VTABLE_OR_VTT_P (d) | |
2351 | && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE | |
2352 | && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type | |
2353 | && DECL_INITIAL (d) | |
2354 | && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR) | |
2355 | { | |
2356 | tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1), | |
2357 | TYPE_SIZE_UNIT (vtable_entry_type)); | |
2358 | HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i); | |
2359 | if (idx >= 0) | |
2360 | { | |
2361 | tree fdesc | |
2362 | = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value; | |
2363 | if (TREE_CODE (fdesc) == FDESC_EXPR | |
2364 | && integer_zerop (TREE_OPERAND (fdesc, 1))) | |
2365 | fun = TREE_OPERAND (fdesc, 0); | |
2366 | } | |
2367 | } | |
2368 | } | |
2d76680f PC |
2369 | } |
2370 | if (TREE_CODE (fun) != FUNCTION_DECL) | |
2371 | { | |
2b3ab879 | 2372 | if (!ctx->quiet && !*non_constant_p) |
84fa214d | 2373 | error_at (loc, "expression %qE does not designate a %<constexpr%> " |
2d76680f PC |
2374 | "function", fun); |
2375 | *non_constant_p = true; | |
2376 | return t; | |
2377 | } | |
81f97181 | 2378 | if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)) |
2d76680f | 2379 | fun = DECL_CLONED_FUNCTION (fun); |
0b274c17 MP |
2380 | |
2381 | if (is_ubsan_builtin_p (fun)) | |
2382 | return void_node; | |
2383 | ||
3d78e008 | 2384 | if (fndecl_built_in_p (fun)) |
5756d0f9 | 2385 | return cxx_eval_builtin_function_call (ctx, t, fun, |
92a596e8 | 2386 | lval, non_constant_p, overflow_p); |
e6321c45 JM |
2387 | if (DECL_THUNK_P (fun)) |
2388 | return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p); | |
2d76680f PC |
2389 | if (!DECL_DECLARED_CONSTEXPR_P (fun)) |
2390 | { | |
8412b939 JJ |
2391 | if (TREE_CODE (t) == CALL_EXPR |
2392 | && cxx_replaceable_global_alloc_fn (fun) | |
2393 | && (CALL_FROM_NEW_OR_DELETE_P (t) | |
2ffc2645 | 2394 | || is_std_allocator_allocate (ctx->call))) |
8e007055 JJ |
2395 | { |
2396 | const int nargs = call_expr_nargs (t); | |
2397 | tree arg0 = NULL_TREE; | |
2398 | for (int i = 0; i < nargs; ++i) | |
2399 | { | |
2400 | tree arg = CALL_EXPR_ARG (t, i); | |
2401 | arg = cxx_eval_constant_expression (ctx, arg, false, | |
2402 | non_constant_p, overflow_p); | |
2403 | VERIFY_CONSTANT (arg); | |
2404 | if (i == 0) | |
2405 | arg0 = arg; | |
2406 | } | |
2407 | gcc_assert (arg0); | |
2408 | if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun))) | |
2409 | { | |
2410 | tree type = build_array_type_nelts (char_type_node, | |
2411 | tree_to_uhwi (arg0)); | |
815baade JJ |
2412 | tree var = build_decl (loc, VAR_DECL, |
2413 | (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) | |
2414 | & OVL_OP_FLAG_VEC) | |
2415 | ? heap_vec_uninit_identifier | |
2416 | : heap_uninit_identifier, | |
8e007055 JJ |
2417 | type); |
2418 | DECL_ARTIFICIAL (var) = 1; | |
2419 | TREE_STATIC (var) = 1; | |
a8db7887 JJ |
2420 | // Temporarily register the artificial var in varpool, |
2421 | // so that comparisons of its address against NULL are folded | |
2422 | // through nonzero_address even with | |
2423 | // -fno-delete-null-pointer-checks or that comparison of | |
2424 | // addresses of different heap artificial vars is folded too. | |
2425 | // See PR98988 and PR99031. | |
2426 | varpool_node::finalize_decl (var); | |
8e007055 JJ |
2427 | ctx->global->heap_vars.safe_push (var); |
2428 | ctx->global->values.put (var, NULL_TREE); | |
2429 | return fold_convert (ptr_type_node, build_address (var)); | |
2430 | } | |
2431 | else | |
2432 | { | |
2433 | STRIP_NOPS (arg0); | |
2434 | if (TREE_CODE (arg0) == ADDR_EXPR | |
2435 | && VAR_P (TREE_OPERAND (arg0, 0))) | |
2436 | { | |
2437 | tree var = TREE_OPERAND (arg0, 0); | |
2438 | if (DECL_NAME (var) == heap_uninit_identifier | |
2439 | || DECL_NAME (var) == heap_identifier) | |
2440 | { | |
815baade JJ |
2441 | if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) |
2442 | & OVL_OP_FLAG_VEC) | |
2443 | { | |
2444 | if (!ctx->quiet) | |
2445 | { | |
2446 | error_at (loc, "array deallocation of object " | |
2447 | "allocated with non-array " | |
2448 | "allocation"); | |
2449 | inform (DECL_SOURCE_LOCATION (var), | |
2450 | "allocation performed here"); | |
2451 | } | |
2452 | *non_constant_p = true; | |
2453 | return t; | |
2454 | } | |
2455 | DECL_NAME (var) = heap_deleted_identifier; | |
2456 | ctx->global->values.remove (var); | |
2457 | ctx->global->heap_dealloc_count++; | |
2458 | return void_node; | |
2459 | } | |
2460 | else if (DECL_NAME (var) == heap_vec_uninit_identifier | |
2461 | || DECL_NAME (var) == heap_vec_identifier) | |
2462 | { | |
2463 | if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) | |
2464 | & OVL_OP_FLAG_VEC) == 0) | |
2465 | { | |
2466 | if (!ctx->quiet) | |
2467 | { | |
2468 | error_at (loc, "non-array deallocation of " | |
2469 | "object allocated with array " | |
2470 | "allocation"); | |
2471 | inform (DECL_SOURCE_LOCATION (var), | |
2472 | "allocation performed here"); | |
2473 | } | |
2474 | *non_constant_p = true; | |
2475 | return t; | |
2476 | } | |
8e007055 JJ |
2477 | DECL_NAME (var) = heap_deleted_identifier; |
2478 | ctx->global->values.remove (var); | |
f74f6092 | 2479 | ctx->global->heap_dealloc_count++; |
8e007055 JJ |
2480 | return void_node; |
2481 | } | |
2482 | else if (DECL_NAME (var) == heap_deleted_identifier) | |
2483 | { | |
2484 | if (!ctx->quiet) | |
2485 | error_at (loc, "deallocation of already deallocated " | |
2486 | "storage"); | |
2487 | *non_constant_p = true; | |
2488 | return t; | |
2489 | } | |
2490 | } | |
2491 | if (!ctx->quiet) | |
2492 | error_at (loc, "deallocation of storage that was " | |
2493 | "not previously allocated"); | |
2494 | *non_constant_p = true; | |
2495 | return t; | |
2496 | } | |
2497 | } | |
cf650568 JJ |
2498 | /* Allow placement new in std::construct_at, just return the second |
2499 | argument. */ | |
8412b939 JJ |
2500 | if (TREE_CODE (t) == CALL_EXPR |
2501 | && cxx_placement_new_fn (fun) | |
2ffc2645 | 2502 | && is_std_construct_at (ctx->call)) |
cf650568 JJ |
2503 | { |
2504 | const int nargs = call_expr_nargs (t); | |
2505 | tree arg1 = NULL_TREE; | |
2506 | for (int i = 0; i < nargs; ++i) | |
2507 | { | |
2508 | tree arg = CALL_EXPR_ARG (t, i); | |
2509 | arg = cxx_eval_constant_expression (ctx, arg, false, | |
2510 | non_constant_p, overflow_p); | |
cf650568 JJ |
2511 | if (i == 1) |
2512 | arg1 = arg; | |
2805fcb3 JJ |
2513 | else |
2514 | VERIFY_CONSTANT (arg); | |
cf650568 JJ |
2515 | } |
2516 | gcc_assert (arg1); | |
2517 | return arg1; | |
2518 | } | |
22edf943 MP |
2519 | else if (cxx_dynamic_cast_fn_p (fun)) |
2520 | return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p); | |
2521 | ||
2b3ab879 | 2522 | if (!ctx->quiet) |
2d76680f | 2523 | { |
11399477 | 2524 | if (!lambda_static_thunk_p (fun)) |
84fa214d | 2525 | error_at (loc, "call to non-%<constexpr%> function %qD", fun); |
2d76680f PC |
2526 | explain_invalid_constexpr_fn (fun); |
2527 | } | |
2528 | *non_constant_p = true; | |
2529 | return t; | |
2530 | } | |
2531 | ||
86461cad JM |
2532 | constexpr_ctx new_ctx = *ctx; |
2533 | if (DECL_CONSTRUCTOR_P (fun) && !ctx->object | |
2534 | && TREE_CODE (t) == AGGR_INIT_EXPR) | |
2535 | { | |
2536 | /* We want to have an initialization target for an AGGR_INIT_EXPR. | |
2537 | If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */ | |
2538 | new_ctx.object = AGGR_INIT_EXPR_SLOT (t); | |
2539 | tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL); | |
e8c48716 | 2540 | CONSTRUCTOR_NO_CLEARING (ctor) = true; |
8e007055 | 2541 | ctx->global->values.put (new_ctx.object, ctor); |
86461cad JM |
2542 | ctx = &new_ctx; |
2543 | } | |
2544 | ||
2d76680f PC |
2545 | /* Shortcut trivial constructor/op=. */ |
2546 | if (trivial_fn_p (fun)) | |
2547 | { | |
86461cad | 2548 | tree init = NULL_TREE; |
2d76680f | 2549 | if (call_expr_nargs (t) == 2) |
86461cad | 2550 | init = convert_from_reference (get_nth_callarg (t, 1)); |
2d76680f PC |
2551 | else if (TREE_CODE (t) == AGGR_INIT_EXPR |
2552 | && AGGR_INIT_ZERO_FIRST (t)) | |
86461cad JM |
2553 | init = build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false); |
2554 | if (init) | |
2555 | { | |
2556 | tree op = get_nth_callarg (t, 0); | |
2557 | if (is_dummy_object (op)) | |
2558 | op = ctx->object; | |
2559 | else | |
2560 | op = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (op)), op); | |
2561 | tree set = build2 (MODIFY_EXPR, TREE_TYPE (op), op, init); | |
c8816908 JM |
2562 | new_ctx.call = &new_call; |
2563 | return cxx_eval_constant_expression (&new_ctx, set, lval, | |
86461cad JM |
2564 | non_constant_p, overflow_p); |
2565 | } | |
2d76680f PC |
2566 | } |
2567 | ||
1595fe44 JM |
2568 | bool non_constant_args = false; |
2569 | new_call.bindings | |
2570 | = cxx_bind_parameters_in_call (ctx, t, fun, non_constant_p, | |
2571 | overflow_p, &non_constant_args); | |
2572 | ||
2573 | /* We build up the bindings list before we know whether we already have this | |
2574 | call cached. If we don't end up saving these bindings, ggc_free them when | |
2575 | this function exits. */ | |
2576 | class free_bindings | |
2577 | { | |
2578 | tree *bindings; | |
2579 | public: | |
2580 | free_bindings (tree &b): bindings (&b) { } | |
2581 | ~free_bindings () { if (bindings) ggc_free (*bindings); } | |
2582 | void preserve () { bindings = NULL; } | |
2583 | } fb (new_call.bindings); | |
2584 | ||
2585 | if (*non_constant_p) | |
2586 | return t; | |
2587 | ||
81371eff JM |
2588 | /* We can't defer instantiating the function any longer. */ |
2589 | if (!DECL_INITIAL (fun) | |
f65a3299 PP |
2590 | && DECL_TEMPLOID_INSTANTIATION (fun) |
2591 | && !uid_sensitive_constexpr_evaluation_p ()) | |
81371eff | 2592 | { |
f065303f JM |
2593 | location_t save_loc = input_location; |
2594 | input_location = loc; | |
81371eff | 2595 | ++function_depth; |
9aeadd8c JM |
2596 | if (ctx->manifestly_const_eval) |
2597 | FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true; | |
81371eff JM |
2598 | instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false); |
2599 | --function_depth; | |
f065303f | 2600 | input_location = save_loc; |
81371eff JM |
2601 | } |
2602 | ||
2d76680f | 2603 | /* If in direct recursive call, optimize definition search. */ |
c8816908 | 2604 | if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun) |
3e605b20 | 2605 | new_call.fundef = ctx->call->fundef; |
2d76680f PC |
2606 | else |
2607 | { | |
2608 | new_call.fundef = retrieve_constexpr_fundef (fun); | |
4ddcdbf9 | 2609 | if (new_call.fundef == NULL || new_call.fundef->body == NULL |
4f05d85a | 2610 | || new_call.fundef->result == error_mark_node |
4ddcdbf9 | 2611 | || fun == current_function_decl) |
2d76680f | 2612 | { |
2b3ab879 | 2613 | if (!ctx->quiet) |
2d76680f | 2614 | { |
4ddcdbf9 JM |
2615 | /* We need to check for current_function_decl here in case we're |
2616 | being called during cp_fold_function, because at that point | |
2617 | DECL_INITIAL is set properly and we have a fundef but we | |
2618 | haven't lowered invisirefs yet (c++/70344). */ | |
2619 | if (DECL_INITIAL (fun) == error_mark_node | |
2620 | || fun == current_function_decl) | |
ddd6d421 JM |
2621 | error_at (loc, "%qD called in a constant expression before its " |
2622 | "definition is complete", fun); | |
2623 | else if (DECL_INITIAL (fun)) | |
2d76680f | 2624 | { |
98e5a19a JM |
2625 | /* The definition of fun was somehow unsuitable. But pretend |
2626 | that lambda static thunks don't exist. */ | |
2627 | if (!lambda_static_thunk_p (fun)) | |
2628 | error_at (loc, "%qD called in a constant expression", fun); | |
2d76680f PC |
2629 | explain_invalid_constexpr_fn (fun); |
2630 | } | |
2631 | else | |
2632 | error_at (loc, "%qD used before its definition", fun); | |
2633 | } | |
2634 | *non_constant_p = true; | |
2635 | return t; | |
2636 | } | |
2637 | } | |
60813a46 | 2638 | |
2d76680f PC |
2639 | depth_ok = push_cx_call_context (t); |
2640 | ||
caf17f3a | 2641 | /* Remember the object we are constructing or destructing. */ |
04e1749c | 2642 | tree new_obj = NULL_TREE; |
caf17f3a | 2643 | if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun)) |
04e1749c | 2644 | { |
caf17f3a | 2645 | /* In a cdtor, it should be the first `this' argument. |
04e1749c MP |
2646 | At this point it has already been evaluated in the call |
2647 | to cxx_bind_parameters_in_call. */ | |
2648 | new_obj = TREE_VEC_ELT (new_call.bindings, 0); | |
2649 | STRIP_NOPS (new_obj); | |
2650 | if (TREE_CODE (new_obj) == ADDR_EXPR) | |
2651 | new_obj = TREE_OPERAND (new_obj, 0); | |
64da1b76 PP |
2652 | |
2653 | if (ctx->call && ctx->call->fundef | |
2654 | && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl)) | |
2655 | { | |
2656 | tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0); | |
2657 | STRIP_NOPS (cur_obj); | |
2658 | if (TREE_CODE (cur_obj) == ADDR_EXPR) | |
2659 | cur_obj = TREE_OPERAND (cur_obj, 0); | |
2660 | if (new_obj == cur_obj) | |
2661 | /* We're calling the target constructor of a delegating | |
2662 | constructor, or accessing a base subobject through a | |
2663 | NOP_EXPR as part of a call to a base constructor, so | |
2664 | there is no new (sub)object. */ | |
2665 | new_obj = NULL_TREE; | |
2666 | } | |
04e1749c MP |
2667 | } |
2668 | ||
12d9ce19 | 2669 | tree result = NULL_TREE; |
2d76680f | 2670 | |
12d9ce19 | 2671 | constexpr_call *entry = NULL; |
4a58d2fe | 2672 | if (depth_ok && !non_constant_args && ctx->strict) |
2d76680f | 2673 | { |
cce3ae91 JJ |
2674 | new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef); |
2675 | new_call.hash | |
2676 | = iterative_hash_template_arg (new_call.bindings, new_call.hash); | |
2677 | new_call.hash | |
13de99bc | 2678 | = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash); |
12d9ce19 JM |
2679 | |
2680 | /* If we have seen this call before, we are done. */ | |
2681 | maybe_initialize_constexpr_call_table (); | |
2682 | constexpr_call **slot | |
2683 | = constexpr_call_table->find_slot (&new_call, INSERT); | |
2684 | entry = *slot; | |
2685 | if (entry == NULL) | |
2686 | { | |
7ffc7de5 JM |
2687 | /* Only cache up to constexpr_cache_depth to limit memory use. */ |
2688 | if (depth_ok < constexpr_cache_depth) | |
2689 | { | |
2690 | /* We need to keep a pointer to the entry, not just the slot, as | |
2691 | the slot can move during evaluation of the body. */ | |
2692 | *slot = entry = ggc_alloc<constexpr_call> (); | |
2693 | *entry = new_call; | |
2694 | fb.preserve (); | |
2695 | } | |
12d9ce19 | 2696 | } |
7ffc7de5 JM |
2697 | /* Calls that are in progress have their result set to NULL, so that we |
2698 | can detect circular dependencies. Now that we only cache up to | |
2699 | constexpr_cache_depth this won't catch circular dependencies that | |
2700 | start deeper, but they'll hit the recursion or ops limit. */ | |
12d9ce19 JM |
2701 | else if (entry->result == NULL) |
2702 | { | |
2703 | if (!ctx->quiet) | |
2704 | error ("call has circular dependency"); | |
2705 | *non_constant_p = true; | |
2706 | entry->result = result = error_mark_node; | |
2707 | } | |
2708 | else | |
2709 | result = entry->result; | |
2d76680f PC |
2710 | } |
2711 | ||
2712 | if (!depth_ok) | |
2713 | { | |
2b3ab879 | 2714 | if (!ctx->quiet) |
84fa214d | 2715 | error ("%<constexpr%> evaluation depth exceeds maximum of %d (use " |
7e7a6ed7 | 2716 | "%<-fconstexpr-depth=%> to increase the maximum)", |
2d76680f PC |
2717 | max_constexpr_depth); |
2718 | *non_constant_p = true; | |
12d9ce19 | 2719 | result = error_mark_node; |
2d76680f PC |
2720 | } |
2721 | else | |
2722 | { | |
f74f6092 | 2723 | bool cacheable = true; |
de54de93 JM |
2724 | if (result && result != error_mark_node) |
2725 | /* OK */; | |
2726 | else if (!DECL_SAVED_TREE (fun)) | |
2727 | { | |
2728 | /* When at_eof >= 2, cgraph has started throwing away | |
2729 | DECL_SAVED_TREE, so fail quietly. FIXME we get here because of | |
2730 | late code generation for VEC_INIT_EXPR, which needs to be | |
2731 | completely reconsidered. */ | |
2732 | gcc_assert (at_eof >= 2 && ctx->quiet); | |
2733 | *non_constant_p = true; | |
2734 | } | |
f65a3299 | 2735 | else if (tree copy = get_fundef_copy (new_call.fundef)) |
3e605b20 | 2736 | { |
c0daf32d | 2737 | tree body, parms, res; |
620adbec | 2738 | releasing_vec ctors; |
0567dcd2 | 2739 | |
c0daf32d | 2740 | /* Reuse or create a new unshared copy of this function's body. */ |
97f3003f JM |
2741 | body = TREE_PURPOSE (copy); |
2742 | parms = TREE_VALUE (copy); | |
2743 | res = TREE_TYPE (copy); | |
0567dcd2 MP |
2744 | |
2745 | /* Associate the bindings with the remapped parms. */ | |
2746 | tree bound = new_call.bindings; | |
2747 | tree remapped = parms; | |
3c961dc7 | 2748 | for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i) |
60813a46 | 2749 | { |
3c961dc7 | 2750 | tree arg = TREE_VEC_ELT (bound, i); |
4953b790 JM |
2751 | if (entry) |
2752 | { | |
2753 | /* Unshare args going into the hash table to separate them | |
2754 | from the caller's context, for better GC and to avoid | |
2755 | problems with verify_gimple. */ | |
db38a029 | 2756 | arg = unshare_expr_without_location (arg); |
4953b790 | 2757 | TREE_VEC_ELT (bound, i) = arg; |
5afd90c5 JJ |
2758 | |
2759 | /* And then unshare again so the callee doesn't change the | |
2760 | argument values in the hash table. XXX Could we unshare | |
2761 | lazily in cxx_eval_store_expression? */ | |
2762 | arg = unshare_constructor (arg); | |
2763 | if (TREE_CODE (arg) == CONSTRUCTOR) | |
2764 | vec_safe_push (ctors, arg); | |
4953b790 | 2765 | } |
8e007055 | 2766 | ctx->global->values.put (remapped, arg); |
0567dcd2 | 2767 | remapped = DECL_CHAIN (remapped); |
60813a46 | 2768 | } |
0567dcd2 | 2769 | /* Add the RESULT_DECL to the values map, too. */ |
cd3ca6cb JM |
2770 | gcc_assert (!DECL_BY_REFERENCE (res)); |
2771 | ctx->global->values.put (res, NULL_TREE); | |
60813a46 | 2772 | |
ee1de08d JJ |
2773 | /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that |
2774 | we can forget their values after the call. */ | |
c0daf32d | 2775 | constexpr_ctx ctx_with_save_exprs = *ctx; |
0ee28590 | 2776 | auto_vec<tree, 10> save_exprs; |
c0daf32d | 2777 | ctx_with_save_exprs.save_exprs = &save_exprs; |
1e163090 | 2778 | ctx_with_save_exprs.call = &new_call; |
f74f6092 JJ |
2779 | unsigned save_heap_alloc_count = ctx->global->heap_vars.length (); |
2780 | unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count; | |
c0daf32d | 2781 | |
caf17f3a MP |
2782 | /* If this is a constexpr destructor, the object's const and volatile |
2783 | semantics are no longer in effect; see [class.dtor]p5. */ | |
2784 | if (new_obj && DECL_DESTRUCTOR_P (fun)) | |
2785 | cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false, | |
2786 | non_constant_p, overflow_p); | |
2787 | ||
0567dcd2 | 2788 | tree jump_target = NULL_TREE; |
c0daf32d | 2789 | cxx_eval_constant_expression (&ctx_with_save_exprs, body, |
0567dcd2 MP |
2790 | lval, non_constant_p, overflow_p, |
2791 | &jump_target); | |
60813a46 | 2792 | |
0567dcd2 | 2793 | if (DECL_CONSTRUCTOR_P (fun)) |
de07cff9 PP |
2794 | { |
2795 | /* This can be null for a subobject constructor call, in | |
2796 | which case what we care about is the initialization | |
2797 | side-effects rather than the value. We could get at the | |
2798 | value by evaluating *this, but we don't bother; there's | |
2799 | no need to put such a call in the hash table. */ | |
2800 | result = lval ? ctx->object : ctx->ctor; | |
2801 | ||
2802 | /* If we've just evaluated a subobject constructor call for an | |
2803 | empty union member, it might not have produced a side effect | |
2804 | that actually activated the union member. So produce such a | |
2805 | side effect now to ensure the union appears initialized. */ | |
2806 | if (!result && new_obj | |
2807 | && TREE_CODE (new_obj) == COMPONENT_REF | |
2808 | && TREE_CODE (TREE_TYPE | |
2809 | (TREE_OPERAND (new_obj, 0))) == UNION_TYPE | |
2810 | && is_really_empty_class (TREE_TYPE (new_obj), | |
2811 | /*ignore_vptr*/false)) | |
2812 | { | |
2813 | tree activate = build2 (MODIFY_EXPR, TREE_TYPE (new_obj), | |
2814 | new_obj, | |
2815 | build_constructor (TREE_TYPE (new_obj), | |
2816 | NULL)); | |
2817 | cxx_eval_constant_expression (ctx, activate, lval, | |
2818 | non_constant_p, overflow_p); | |
2819 | ggc_free (activate); | |
2820 | } | |
2821 | } | |
0567dcd2 MP |
2822 | else if (VOID_TYPE_P (TREE_TYPE (res))) |
2823 | result = void_node; | |
2824 | else | |
2825 | { | |
cd3ca6cb | 2826 | result = *ctx->global->values.get (res); |
0567dcd2 | 2827 | if (result == NULL_TREE && !*non_constant_p) |
60813a46 | 2828 | { |
0567dcd2 | 2829 | if (!ctx->quiet) |
84fa214d | 2830 | error ("%<constexpr%> call flows off the end " |
0567dcd2 MP |
2831 | "of the function"); |
2832 | *non_constant_p = true; | |
60813a46 | 2833 | } |
60813a46 | 2834 | } |
0567dcd2 | 2835 | |
04e1749c MP |
2836 | /* At this point, the object's constructor will have run, so |
2837 | the object is no longer under construction, and its possible | |
2838 | 'const' semantics now apply. Make a note of this fact by | |
2839 | marking the CONSTRUCTOR TREE_READONLY. */ | |
caf17f3a MP |
2840 | if (new_obj && DECL_CONSTRUCTOR_P (fun)) |
2841 | cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true, | |
2842 | non_constant_p, overflow_p); | |
04e1749c | 2843 | |
ee1de08d JJ |
2844 | /* Forget the saved values of the callee's SAVE_EXPRs and |
2845 | TARGET_EXPRs. */ | |
3f207ab3 | 2846 | for (tree save_expr : save_exprs) |
8e007055 | 2847 | ctx->global->values.remove (save_expr); |
c0daf32d | 2848 | |
0567dcd2 MP |
2849 | /* Remove the parms/result from the values map. Is it worth |
2850 | bothering to do this when the map itself is only live for | |
2851 | one constexpr evaluation? If so, maybe also clear out | |
2852 | other vars from call, maybe in BIND_EXPR handling? */ | |
8e007055 | 2853 | ctx->global->values.remove (res); |
0567dcd2 | 2854 | for (tree parm = parms; parm; parm = TREE_CHAIN (parm)) |
8e007055 | 2855 | ctx->global->values.remove (parm); |
c0daf32d | 2856 | |
620adbec JM |
2857 | /* Free any parameter CONSTRUCTORs we aren't returning directly. */ |
2858 | while (!ctors->is_empty ()) | |
2859 | { | |
2860 | tree c = ctors->pop (); | |
2861 | if (c != result) | |
2862 | free_constructor (c); | |
2863 | } | |
2864 | ||
c0daf32d PP |
2865 | /* Make the unshared function copy we used available for re-use. */ |
2866 | save_fundef_copy (fun, copy); | |
f74f6092 JJ |
2867 | |
2868 | /* If the call allocated some heap object that hasn't been | |
2869 | deallocated during the call, or if it deallocated some heap | |
2870 | object it has not allocated, the call isn't really stateless | |
2871 | for the constexpr evaluation and should not be cached. | |
2872 | It is fine if the call allocates something and deallocates it | |
2873 | too. */ | |
2874 | if (entry | |
2875 | && (save_heap_alloc_count != ctx->global->heap_vars.length () | |
2876 | || (save_heap_dealloc_count | |
2877 | != ctx->global->heap_dealloc_count))) | |
2878 | { | |
2879 | tree heap_var; | |
2880 | unsigned int i; | |
2881 | if ((ctx->global->heap_vars.length () | |
2882 | - ctx->global->heap_dealloc_count) | |
2883 | != save_heap_alloc_count - save_heap_dealloc_count) | |
2884 | cacheable = false; | |
2885 | else | |
2886 | FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var, | |
2887 | save_heap_alloc_count) | |
2888 | if (DECL_NAME (heap_var) != heap_deleted_identifier) | |
2889 | { | |
2890 | cacheable = false; | |
2891 | break; | |
2892 | } | |
2893 | } | |
b2562229 PP |
2894 | |
2895 | /* Rewrite all occurrences of the function's RESULT_DECL with the | |
2896 | current object under construction. */ | |
2897 | if (!*non_constant_p && ctx->object | |
bb1f0b50 | 2898 | && CLASS_TYPE_P (TREE_TYPE (res)) |
b2562229 PP |
2899 | && !is_empty_class (TREE_TYPE (res))) |
2900 | if (replace_result_decl (&result, res, ctx->object)) | |
2901 | cacheable = false; | |
3e605b20 | 2902 | } |
aaa26bf4 JM |
2903 | else |
2904 | /* Couldn't get a function copy to evaluate. */ | |
2905 | *non_constant_p = true; | |
60813a46 | 2906 | |
2d76680f PC |
2907 | if (result == error_mark_node) |
2908 | *non_constant_p = true; | |
52228180 | 2909 | if (*non_constant_p || *overflow_p) |
12d9ce19 | 2910 | result = error_mark_node; |
0567dcd2 | 2911 | else if (!result) |
60813a46 | 2912 | result = void_node; |
12d9ce19 | 2913 | if (entry) |
f74f6092 | 2914 | entry->result = cacheable ? result : error_mark_node; |
2d76680f PC |
2915 | } |
2916 | ||
7906797e MP |
2917 | /* The result of a constexpr function must be completely initialized. |
2918 | ||
2919 | However, in C++20, a constexpr constructor doesn't necessarily have | |
2920 | to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING | |
2921 | in order to detect reading an unitialized object in constexpr instead | |
2922 | of value-initializing it. (reduced_constant_expression_p is expected to | |
2923 | take care of clearing the flag.) */ | |
2924 | if (TREE_CODE (result) == CONSTRUCTOR | |
b04445d4 | 2925 | && (cxx_dialect < cxx20 |
7906797e | 2926 | || !DECL_CONSTRUCTOR_P (fun))) |
ecc57615 | 2927 | clear_no_implicit_zero (result); |
f64e0c02 | 2928 | |
2d76680f | 2929 | pop_cx_call_context (); |
9b9eb42a | 2930 | return result; |
2d76680f PC |
2931 | } |
2932 | ||
7906797e MP |
2933 | /* Return true if T is a valid constant initializer. If a CONSTRUCTOR |
2934 | initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be | |
2935 | cleared. | |
2936 | FIXME speed this up, it's taking 16% of compile time on sieve testcase. */ | |
2d76680f PC |
2937 | |
2938 | bool | |
2939 | reduced_constant_expression_p (tree t) | |
2940 | { | |
4930c53e MP |
2941 | if (t == NULL_TREE) |
2942 | return false; | |
2943 | ||
2d76680f PC |
2944 | switch (TREE_CODE (t)) |
2945 | { | |
2946 | case PTRMEM_CST: | |
2947 | /* Even if we can't lower this yet, it's constant. */ | |
2948 | return true; | |
2949 | ||
2950 | case CONSTRUCTOR: | |
2951 | /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */ | |
fae00a0a | 2952 | tree field; |
e8c48716 | 2953 | if (CONSTRUCTOR_NO_CLEARING (t)) |
6f11ddd8 JM |
2954 | { |
2955 | if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE) | |
2956 | /* An initialized vector would have a VECTOR_CST. */ | |
2957 | return false; | |
b04445d4 | 2958 | else if (cxx_dialect >= cxx20 |
b599bf9d | 2959 | && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) |
baf05d54 PP |
2960 | { |
2961 | /* There must be a valid constant initializer at every array | |
2962 | index. */ | |
2963 | tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t))); | |
2964 | tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t))); | |
2965 | tree cursor = min; | |
fae00a0a | 2966 | for (auto &e: CONSTRUCTOR_ELTS (t)) |
baf05d54 | 2967 | { |
fae00a0a | 2968 | if (!reduced_constant_expression_p (e.value)) |
baf05d54 | 2969 | return false; |
fae00a0a | 2970 | if (array_index_cmp (cursor, e.index) != 0) |
baf05d54 | 2971 | return false; |
fae00a0a JM |
2972 | if (TREE_CODE (e.index) == RANGE_EXPR) |
2973 | cursor = TREE_OPERAND (e.index, 1); | |
baf05d54 PP |
2974 | cursor = int_const_binop (PLUS_EXPR, cursor, size_one_node); |
2975 | } | |
2976 | if (find_array_ctor_elt (t, max) == -1) | |
2977 | return false; | |
2978 | goto ok; | |
2979 | } | |
b04445d4 | 2980 | else if (cxx_dialect >= cxx20 |
b599bf9d PP |
2981 | && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE) |
2982 | { | |
2983 | if (CONSTRUCTOR_NELTS (t) == 0) | |
2984 | /* An initialized union has a constructor element. */ | |
2985 | return false; | |
2986 | /* And it only initializes one member. */ | |
2987 | field = NULL_TREE; | |
2988 | } | |
6f11ddd8 JM |
2989 | else |
2990 | field = next_initializable_field (TYPE_FIELDS (TREE_TYPE (t))); | |
2991 | } | |
7368cfa4 JM |
2992 | else |
2993 | field = NULL_TREE; | |
fae00a0a | 2994 | for (auto &e: CONSTRUCTOR_ELTS (t)) |
125db6a1 | 2995 | { |
4930c53e MP |
2996 | /* If VAL is null, we're in the middle of initializing this |
2997 | element. */ | |
fae00a0a | 2998 | if (!reduced_constant_expression_p (e.value)) |
125db6a1 | 2999 | return false; |
d6ff2207 | 3000 | /* Empty class field may or may not have an initializer. */ |
fae00a0a | 3001 | for (; field && e.index != field; |
d6ff2207 MP |
3002 | field = next_initializable_field (DECL_CHAIN (field))) |
3003 | if (!is_really_empty_class (TREE_TYPE (field), | |
3004 | /*ignore_vptr*/false)) | |
3005 | return false; | |
7368cfa4 | 3006 | if (field) |
d6ff2207 | 3007 | field = next_initializable_field (DECL_CHAIN (field)); |
125db6a1 | 3008 | } |
7906797e MP |
3009 | /* There could be a non-empty field at the end. */ |
3010 | for (; field; field = next_initializable_field (DECL_CHAIN (field))) | |
3011 | if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false)) | |
3012 | return false; | |
baf05d54 | 3013 | ok: |
7906797e | 3014 | if (CONSTRUCTOR_NO_CLEARING (t)) |
7368cfa4 | 3015 | /* All the fields are initialized. */ |
e8c48716 | 3016 | CONSTRUCTOR_NO_CLEARING (t) = false; |
2d76680f PC |
3017 | return true; |
3018 | ||
3019 | default: | |
3020 | /* FIXME are we calling this too much? */ | |
3021 | return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE; | |
3022 | } | |
3023 | } | |
3024 | ||
3025 | /* Some expressions may have constant operands but are not constant | |
7d75ea04 JJ |
3026 | themselves, such as 1/0. Call this function to check for that |
3027 | condition. | |
2d76680f PC |
3028 | |
3029 | We only call this in places that require an arithmetic constant, not in | |
3030 | places where we might have a non-constant expression that can be a | |
3031 | component of a constant expression, such as the address of a constexpr | |
3032 | variable that might be dereferenced later. */ | |
3033 | ||
3034 | static bool | |
3035 | verify_constant (tree t, bool allow_non_constant, bool *non_constant_p, | |
3036 | bool *overflow_p) | |
3037 | { | |
596334fa JM |
3038 | if (!*non_constant_p && !reduced_constant_expression_p (t) |
3039 | && t != void_node) | |
2d76680f PC |
3040 | { |
3041 | if (!allow_non_constant) | |
3042 | error ("%q+E is not a constant expression", t); | |
3043 | *non_constant_p = true; | |
3044 | } | |
3045 | if (TREE_OVERFLOW_P (t)) | |
3046 | { | |
3047 | if (!allow_non_constant) | |
3048 | { | |
3049 | permerror (input_location, "overflow in constant expression"); | |
3050 | /* If we're being permissive (and are in an enforcing | |
3051 | context), ignore the overflow. */ | |
3052 | if (flag_permissive) | |
3053 | return *non_constant_p; | |
3054 | } | |
3055 | *overflow_p = true; | |
3056 | } | |
3057 | return *non_constant_p; | |
3058 | } | |
3059 | ||
253a921b MP |
3060 | /* Check whether the shift operation with code CODE and type TYPE on LHS |
3061 | and RHS is undefined. If it is, give an error with an explanation, | |
3062 | and return true; return false otherwise. */ | |
3063 | ||
3064 | static bool | |
3065 | cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx, | |
3066 | enum tree_code code, tree type, tree lhs, tree rhs) | |
3067 | { | |
3068 | if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR) | |
3069 | || TREE_CODE (lhs) != INTEGER_CST | |
3070 | || TREE_CODE (rhs) != INTEGER_CST) | |
3071 | return false; | |
3072 | ||
3073 | tree lhstype = TREE_TYPE (lhs); | |
3074 | unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs)); | |
3075 | ||
3076 | /* [expr.shift] The behavior is undefined if the right operand | |
3077 | is negative, or greater than or equal to the length in bits | |
3078 | of the promoted left operand. */ | |
3079 | if (tree_int_cst_sgn (rhs) == -1) | |
3080 | { | |
3081 | if (!ctx->quiet) | |
5342156c MP |
3082 | permerror (loc, "right operand of shift expression %q+E is negative", |
3083 | build2_loc (loc, code, type, lhs, rhs)); | |
3084 | return (!flag_permissive || ctx->quiet); | |
253a921b MP |
3085 | } |
3086 | if (compare_tree_int (rhs, uprec) >= 0) | |
3087 | { | |
3088 | if (!ctx->quiet) | |
a9c697b8 MS |
3089 | permerror (loc, "right operand of shift expression %q+E is greater " |
3090 | "than or equal to the precision %wu of the left operand", | |
3091 | build2_loc (loc, code, type, lhs, rhs), uprec); | |
5342156c | 3092 | return (!flag_permissive || ctx->quiet); |
253a921b MP |
3093 | } |
3094 | ||
3095 | /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...] | |
3096 | if E1 has a signed type and non-negative value, and E1x2^E2 is | |
3097 | representable in the corresponding unsigned type of the result type, | |
3098 | then that value, converted to the result type, is the resulting value; | |
8ee09943 | 3099 | otherwise, the behavior is undefined. |
b04445d4 | 3100 | For C++20: |
8ee09943 JJ |
3101 | The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo |
3102 | 2^N, where N is the range exponent of the type of the result. */ | |
3103 | if (code == LSHIFT_EXPR | |
3104 | && !TYPE_UNSIGNED (lhstype) | |
3105 | && cxx_dialect >= cxx11 | |
b04445d4 | 3106 | && cxx_dialect < cxx20) |
253a921b MP |
3107 | { |
3108 | if (tree_int_cst_sgn (lhs) == -1) | |
3109 | { | |
3110 | if (!ctx->quiet) | |
5342156c MP |
3111 | permerror (loc, |
3112 | "left operand of shift expression %q+E is negative", | |
3113 | build2_loc (loc, code, type, lhs, rhs)); | |
3114 | return (!flag_permissive || ctx->quiet); | |
253a921b MP |
3115 | } |
3116 | /* For signed x << y the following: | |
3117 | (unsigned) x >> ((prec (lhs) - 1) - y) | |
3118 | if > 1, is undefined. The right-hand side of this formula | |
3119 | is the highest bit of the LHS that can be set (starting from 0), | |
3120 | so that the shift doesn't overflow. We then right-shift the LHS | |
3121 | to see whether any other bit is set making the original shift | |
3122 | undefined -- the result is not representable in the corresponding | |
3123 | unsigned type. */ | |
3124 | tree t = build_int_cst (unsigned_type_node, uprec - 1); | |
3125 | t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs); | |
3126 | tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs); | |
3127 | t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t); | |
3128 | if (tree_int_cst_lt (integer_one_node, t)) | |
3129 | { | |
3130 | if (!ctx->quiet) | |
5342156c MP |
3131 | permerror (loc, "shift expression %q+E overflows", |
3132 | build2_loc (loc, code, type, lhs, rhs)); | |
3133 | return (!flag_permissive || ctx->quiet); | |
253a921b MP |
3134 | } |
3135 | } | |
3136 | return false; | |
3137 | } | |
3138 | ||
2d76680f PC |
3139 | /* Subroutine of cxx_eval_constant_expression. |
3140 | Attempt to reduce the unary expression tree T to a compile time value. | |
3141 | If successful, return the value. Otherwise issue a diagnostic | |
3142 | and return error_mark_node. */ | |
3143 | ||
3144 | static tree | |
3e605b20 | 3145 | cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t, |
12d9ce19 | 3146 | bool /*lval*/, |
2d76680f PC |
3147 | bool *non_constant_p, bool *overflow_p) |
3148 | { | |
3149 | tree r; | |
3150 | tree orig_arg = TREE_OPERAND (t, 0); | |
12d9ce19 JM |
3151 | tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false, |
3152 | non_constant_p, overflow_p); | |
2d76680f | 3153 | VERIFY_CONSTANT (arg); |
f899317e JM |
3154 | location_t loc = EXPR_LOCATION (t); |
3155 | enum tree_code code = TREE_CODE (t); | |
3156 | tree type = TREE_TYPE (t); | |
3157 | r = fold_unary_loc (loc, code, type, arg); | |
3158 | if (r == NULL_TREE) | |
3159 | { | |
3160 | if (arg == orig_arg) | |
3161 | r = t; | |
3162 | else | |
3163 | r = build1_loc (loc, code, type, arg); | |
3164 | } | |
2d76680f PC |
3165 | VERIFY_CONSTANT (r); |
3166 | return r; | |
3167 | } | |
3168 | ||
ea8661cd JJ |
3169 | /* Helper function for cxx_eval_binary_expression. Try to optimize |
3170 | original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the | |
3171 | generic folding should be used. */ | |
3172 | ||
3173 | static tree | |
3174 | cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t, | |
3175 | tree lhs, tree rhs, bool *non_constant_p, | |
3176 | bool *overflow_p) | |
3177 | { | |
3178 | STRIP_NOPS (lhs); | |
3179 | if (TREE_CODE (lhs) != ADDR_EXPR) | |
3180 | return NULL_TREE; | |
3181 | ||
3182 | lhs = TREE_OPERAND (lhs, 0); | |
3183 | ||
3184 | /* &A[i] p+ j => &A[i + j] */ | |
3185 | if (TREE_CODE (lhs) == ARRAY_REF | |
3186 | && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST | |
3187 | && TREE_CODE (rhs) == INTEGER_CST | |
3188 | && TYPE_SIZE_UNIT (TREE_TYPE (lhs)) | |
3189 | && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST) | |
3190 | { | |
3191 | tree orig_type = TREE_TYPE (t); | |
3192 | location_t loc = EXPR_LOCATION (t); | |
3193 | tree type = TREE_TYPE (lhs); | |
3194 | ||
3195 | t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1)); | |
3196 | tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0))); | |
3197 | nelts = cxx_eval_constant_expression (ctx, nelts, false, non_constant_p, | |
3198 | overflow_p); | |
3199 | if (*non_constant_p) | |
3200 | return NULL_TREE; | |
3201 | /* Don't fold an out-of-bound access. */ | |
3202 | if (!tree_int_cst_le (t, nelts)) | |
3203 | return NULL_TREE; | |
3204 | rhs = cp_fold_convert (ssizetype, rhs); | |
3205 | /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT. | |
3206 | constexpr int A[1]; ... (char *)&A[0] + 1 */ | |
3207 | if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype, | |
3208 | rhs, TYPE_SIZE_UNIT (type)))) | |
3209 | return NULL_TREE; | |
3210 | /* Make sure to treat the second operand of POINTER_PLUS_EXPR | |
3211 | as signed. */ | |
3212 | rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs, | |
3213 | TYPE_SIZE_UNIT (type)); | |
3214 | t = size_binop_loc (loc, PLUS_EXPR, rhs, t); | |
3215 | t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0), | |
3216 | t, NULL_TREE, NULL_TREE); | |
3217 | t = cp_build_addr_expr (t, tf_warning_or_error); | |
3218 | t = cp_fold_convert (orig_type, t); | |
3219 | return cxx_eval_constant_expression (ctx, t, /*lval*/false, | |
3220 | non_constant_p, overflow_p); | |
3221 | } | |
3222 | ||
3223 | return NULL_TREE; | |
3224 | } | |
3225 | ||
2d76680f PC |
3226 | /* Subroutine of cxx_eval_constant_expression. |
3227 | Like cxx_eval_unary_expression, except for binary expressions. */ | |
3228 | ||
3229 | static tree | |
3e605b20 | 3230 | cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t, |
dfffecb8 | 3231 | bool lval, |
2d76680f PC |
3232 | bool *non_constant_p, bool *overflow_p) |
3233 | { | |
618d6c1c | 3234 | tree r = NULL_TREE; |
2d76680f PC |
3235 | tree orig_lhs = TREE_OPERAND (t, 0); |
3236 | tree orig_rhs = TREE_OPERAND (t, 1); | |
3237 | tree lhs, rhs; | |
12d9ce19 | 3238 | lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false, |
5a804683 | 3239 | non_constant_p, overflow_p); |
c8a66fc9 JM |
3240 | /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer |
3241 | subtraction. */ | |
3242 | if (*non_constant_p) | |
3243 | return t; | |
12d9ce19 | 3244 | rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false, |
5a804683 | 3245 | non_constant_p, overflow_p); |
c8a66fc9 JM |
3246 | if (*non_constant_p) |
3247 | return t; | |
f899317e JM |
3248 | |
3249 | location_t loc = EXPR_LOCATION (t); | |
3250 | enum tree_code code = TREE_CODE (t); | |
3251 | tree type = TREE_TYPE (t); | |
618d6c1c PP |
3252 | |
3253 | if (code == EQ_EXPR || code == NE_EXPR) | |
3254 | { | |
3255 | bool is_code_eq = (code == EQ_EXPR); | |
3256 | ||
3257 | if (TREE_CODE (lhs) == PTRMEM_CST | |
3258 | && TREE_CODE (rhs) == PTRMEM_CST) | |
9b0607de JM |
3259 | { |
3260 | tree lmem = PTRMEM_CST_MEMBER (lhs); | |
3261 | tree rmem = PTRMEM_CST_MEMBER (rhs); | |
3262 | bool eq; | |
3263 | if (TREE_CODE (lmem) == TREE_CODE (rmem) | |
3264 | && TREE_CODE (lmem) == FIELD_DECL | |
3265 | && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE | |
3266 | && same_type_p (DECL_CONTEXT (lmem), | |
3267 | DECL_CONTEXT (rmem))) | |
3268 | /* If both refer to (possibly different) members of the same union | |
3269 | (12.3), they compare equal. */ | |
3270 | eq = true; | |
3271 | else | |
3272 | eq = cp_tree_equal (lhs, rhs); | |
3273 | r = constant_boolean_node (eq == is_code_eq, type); | |
3274 | } | |
618d6c1c PP |
3275 | else if ((TREE_CODE (lhs) == PTRMEM_CST |
3276 | || TREE_CODE (rhs) == PTRMEM_CST) | |
3277 | && (null_member_pointer_value_p (lhs) | |
3278 | || null_member_pointer_value_p (rhs))) | |
3279 | r = constant_boolean_node (!is_code_eq, type); | |
dd5dda56 JM |
3280 | else if (TREE_CODE (lhs) == PTRMEM_CST) |
3281 | lhs = cplus_expand_constant (lhs); | |
3282 | else if (TREE_CODE (rhs) == PTRMEM_CST) | |
3283 | rhs = cplus_expand_constant (rhs); | |
618d6c1c | 3284 | } |
8bada5cd MS |
3285 | if (code == POINTER_PLUS_EXPR && !*non_constant_p |
3286 | && integer_zerop (lhs) && !integer_zerop (rhs)) | |
3287 | { | |
3288 | if (!ctx->quiet) | |
3289 | error ("arithmetic involving a null pointer in %qE", lhs); | |
bf533db8 | 3290 | *non_constant_p = true; |
8bada5cd MS |
3291 | return t; |
3292 | } | |
ea8661cd JJ |
3293 | else if (code == POINTER_PLUS_EXPR) |
3294 | r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p, | |
3295 | overflow_p); | |
b7689b96 JM |
3296 | else if (code == SPACESHIP_EXPR) |
3297 | { | |
4ed1dc12 | 3298 | r = genericize_spaceship (loc, type, lhs, rhs); |
5c64df80 JJ |
3299 | return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, |
3300 | overflow_p); | |
b7689b96 | 3301 | } |
618d6c1c PP |
3302 | |
3303 | if (r == NULL_TREE) | |
3304 | r = fold_binary_loc (loc, code, type, lhs, rhs); | |
3305 | ||
4866b2f5 JJ |
3306 | if (r == NULL_TREE |
3307 | && (code == LSHIFT_EXPR || code == RSHIFT_EXPR) | |
3308 | && TREE_CODE (lhs) == INTEGER_CST | |
3309 | && TREE_CODE (rhs) == INTEGER_CST | |
3310 | && wi::neg_p (wi::to_wide (rhs))) | |
3311 | { | |
3312 | /* For diagnostics and -fpermissive emulate previous behavior of | |
3313 | handling shifts by negative amount. */ | |
3314 | tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs); | |
3315 | if (nrhs) | |
3316 | r = fold_binary_loc (loc, | |
3317 | code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR, | |
3318 | type, lhs, nrhs); | |
3319 | } | |
3320 | ||
f899317e JM |
3321 | if (r == NULL_TREE) |
3322 | { | |
3323 | if (lhs == orig_lhs && rhs == orig_rhs) | |
3324 | r = t; | |
3325 | else | |
3326 | r = build2_loc (loc, code, type, lhs, rhs); | |
3327 | } | |
253a921b MP |
3328 | else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs)) |
3329 | *non_constant_p = true; | |
c8a66fc9 JM |
3330 | /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to |
3331 | a local array in a constexpr function. */ | |
71a93b08 | 3332 | bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs)); |
caee690e | 3333 | if (!ptr) |
134efa82 | 3334 | VERIFY_CONSTANT (r); |
2d76680f PC |
3335 | return r; |
3336 | } | |
3337 | ||
3338 | /* Subroutine of cxx_eval_constant_expression. | |
3339 | Attempt to evaluate condition expressions. Dead branches are not | |
3340 | looked into. */ | |
3341 | ||
3342 | static tree | |
3e605b20 | 3343 | cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t, |
92a596e8 | 3344 | bool lval, |
56632b27 JM |
3345 | bool *non_constant_p, bool *overflow_p, |
3346 | tree *jump_target) | |
2d76680f | 3347 | { |
3e605b20 | 3348 | tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
12d9ce19 | 3349 | /*lval*/false, |
5a804683 | 3350 | non_constant_p, overflow_p); |
2d76680f | 3351 | VERIFY_CONSTANT (val); |
117c6426 JJ |
3352 | if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t)) |
3353 | { | |
3354 | /* Evaluate the condition as if it was | |
3355 | if (__builtin_is_constant_evaluated ()), i.e. defer it if not | |
3356 | ctx->manifestly_const_eval (as sometimes we try to constant evaluate | |
3357 | without manifestly_const_eval even expressions or parts thereof which | |
3358 | will later be manifestly const_eval evaluated), otherwise fold it to | |
3359 | true. */ | |
3360 | if (ctx->manifestly_const_eval) | |
3361 | val = boolean_true_node; | |
3362 | else | |
3363 | { | |
3364 | *non_constant_p = true; | |
3365 | return t; | |
3366 | } | |
3367 | } | |
2d76680f PC |
3368 | /* Don't VERIFY_CONSTANT the other operands. */ |
3369 | if (integer_zerop (val)) | |
43574e4f JJ |
3370 | val = TREE_OPERAND (t, 2); |
3371 | else | |
3372 | val = TREE_OPERAND (t, 1); | |
3373 | if (TREE_CODE (t) == IF_STMT && !val) | |
3374 | val = void_node; | |
3375 | return cxx_eval_constant_expression (ctx, val, lval, non_constant_p, | |
3376 | overflow_p, jump_target); | |
2d76680f PC |
3377 | } |
3378 | ||
f370e36d JJ |
3379 | /* Subroutine of cxx_eval_constant_expression. |
3380 | Attempt to evaluate vector condition expressions. Unlike | |
3381 | cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal | |
3382 | ternary arithmetics operation, where all 3 arguments have to be | |
3383 | evaluated as constants and then folding computes the result from | |
3384 | them. */ | |
3385 | ||
3386 | static tree | |
3387 | cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t, | |
3388 | bool *non_constant_p, bool *overflow_p) | |
3389 | { | |
3390 | tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), | |
3391 | /*lval*/false, | |
3392 | non_constant_p, overflow_p); | |
3393 | VERIFY_CONSTANT (arg1); | |
3394 | tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), | |
3395 | /*lval*/false, | |
3396 | non_constant_p, overflow_p); | |
3397 | VERIFY_CONSTANT (arg2); | |
3398 | tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2), | |
3399 | /*lval*/false, | |
3400 | non_constant_p, overflow_p); | |
3401 | VERIFY_CONSTANT (arg3); | |
3402 | location_t loc = EXPR_LOCATION (t); | |
3403 | tree type = TREE_TYPE (t); | |
3404 | tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); | |
3405 | if (r == NULL_TREE) | |
3406 | { | |
3407 | if (arg1 == TREE_OPERAND (t, 0) | |
3408 | && arg2 == TREE_OPERAND (t, 1) | |
3409 | && arg3 == TREE_OPERAND (t, 2)) | |
3410 | r = t; | |
3411 | else | |
3412 | r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); | |
3413 | } | |
3414 | VERIFY_CONSTANT (r); | |
3415 | return r; | |
3416 | } | |
3417 | ||
ceaaf873 JM |
3418 | /* Returns less than, equal to, or greater than zero if KEY is found to be |
3419 | less than, to match, or to be greater than the constructor_elt's INDEX. */ | |
3420 | ||
3421 | static int | |
3422 | array_index_cmp (tree key, tree index) | |
3423 | { | |
3424 | gcc_assert (TREE_CODE (key) == INTEGER_CST); | |
3425 | ||
3426 | switch (TREE_CODE (index)) | |
3427 | { | |
3428 | case INTEGER_CST: | |
3429 | return tree_int_cst_compare (key, index); | |
3430 | case RANGE_EXPR: | |
3431 | { | |
3432 | tree lo = TREE_OPERAND (index, 0); | |
3433 | tree hi = TREE_OPERAND (index, 1); | |
3434 | if (tree_int_cst_lt (key, lo)) | |
3435 | return -1; | |
3436 | else if (tree_int_cst_lt (hi, key)) | |
3437 | return 1; | |
3438 | else | |
3439 | return 0; | |
3440 | } | |
3441 | default: | |
3442 | gcc_unreachable (); | |
3443 | } | |
3444 | } | |
3445 | ||
3446 | /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1 | |
3447 | if none. If INSERT is true, insert a matching element rather than fail. */ | |
3448 | ||
3449 | static HOST_WIDE_INT | |
582d2481 | 3450 | find_array_ctor_elt (tree ary, tree dindex, bool insert) |
ceaaf873 JM |
3451 | { |
3452 | if (tree_int_cst_sgn (dindex) < 0) | |
3453 | return -1; | |
3454 | ||
3455 | unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex); | |
3456 | vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary); | |
3457 | unsigned HOST_WIDE_INT len = vec_safe_length (elts); | |
3458 | ||
3459 | unsigned HOST_WIDE_INT end = len; | |
3460 | unsigned HOST_WIDE_INT begin = 0; | |
3461 | ||
3462 | /* If the last element of the CONSTRUCTOR has its own index, we can assume | |
3463 | that the same is true of the other elements and index directly. */ | |
3464 | if (end > 0) | |
3465 | { | |
fe217ba0 | 3466 | tree cindex = (*elts)[end - 1].index; |
c7c09af8 JJ |
3467 | if (cindex == NULL_TREE) |
3468 | { | |
3469 | /* Verify that if the last index is missing, all indexes | |
3470 | are missing. */ | |
3471 | if (flag_checking) | |
3472 | for (unsigned int j = 0; j < len - 1; ++j) | |
3473 | gcc_assert ((*elts)[j].index == NULL_TREE); | |
3474 | if (i < end) | |
3475 | return i; | |
3476 | else | |
3477 | { | |
3478 | begin = end; | |
3479 | if (i == end) | |
3480 | /* If the element is to be added right at the end, | |
3481 | make sure it is added with cleared index too. */ | |
3482 | dindex = NULL_TREE; | |
3483 | else if (insert) | |
3484 | /* Otherwise, in order not to break the assumption | |
3485 | that CONSTRUCTOR either has all indexes or none, | |
3486 | we need to add indexes to all elements. */ | |
3487 | for (unsigned int j = 0; j < len; ++j) | |
3488 | (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j); | |
3489 | } | |
3490 | } | |
3491 | else if (TREE_CODE (cindex) == INTEGER_CST | |
3492 | && compare_tree_int (cindex, end - 1) == 0) | |
ceaaf873 JM |
3493 | { |
3494 | if (i < end) | |
3495 | return i; | |
3496 | else | |
3497 | begin = end; | |
3498 | } | |
3499 | } | |
3500 | ||
3501 | /* Otherwise, find a matching index by means of a binary search. */ | |
3502 | while (begin != end) | |
3503 | { | |
3504 | unsigned HOST_WIDE_INT middle = (begin + end) / 2; | |
a7ccb9e7 JM |
3505 | constructor_elt &elt = (*elts)[middle]; |
3506 | tree idx = elt.index; | |
ceaaf873 | 3507 | |
a7ccb9e7 | 3508 | int cmp = array_index_cmp (dindex, idx); |
ceaaf873 JM |
3509 | if (cmp < 0) |
3510 | end = middle; | |
3511 | else if (cmp > 0) | |
3512 | begin = middle + 1; | |
3513 | else | |
a7ccb9e7 JM |
3514 | { |
3515 | if (insert && TREE_CODE (idx) == RANGE_EXPR) | |
3516 | { | |
3517 | /* We need to split the range. */ | |
3518 | constructor_elt e; | |
3519 | tree lo = TREE_OPERAND (idx, 0); | |
3520 | tree hi = TREE_OPERAND (idx, 1); | |
fe217ba0 JJ |
3521 | tree value = elt.value; |
3522 | dindex = fold_convert (sizetype, dindex); | |
a7ccb9e7 JM |
3523 | if (tree_int_cst_lt (lo, dindex)) |
3524 | { | |
3525 | /* There are still some lower elts; shorten the range. */ | |
3526 | tree new_hi = int_const_binop (MINUS_EXPR, dindex, | |
3527 | size_one_node); | |
3528 | if (tree_int_cst_equal (lo, new_hi)) | |
3529 | /* Only one element left, no longer a range. */ | |
3530 | elt.index = lo; | |
3531 | else | |
3532 | TREE_OPERAND (idx, 1) = new_hi; | |
3533 | /* Append the element we want to insert. */ | |
3534 | ++middle; | |
3535 | e.index = dindex; | |
fe217ba0 | 3536 | e.value = unshare_constructor (value); |
a7ccb9e7 JM |
3537 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e); |
3538 | } | |
3539 | else | |
3540 | /* No lower elts, the range elt is now ours. */ | |
3541 | elt.index = dindex; | |
3542 | ||
3543 | if (tree_int_cst_lt (dindex, hi)) | |
3544 | { | |
3545 | /* There are still some higher elts; append a range. */ | |
3546 | tree new_lo = int_const_binop (PLUS_EXPR, dindex, | |
3547 | size_one_node); | |
3548 | if (tree_int_cst_equal (new_lo, hi)) | |
3549 | e.index = hi; | |
3550 | else | |
3551 | e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi); | |
fe217ba0 JJ |
3552 | e.value = unshare_constructor (value); |
3553 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e); | |
a7ccb9e7 JM |
3554 | } |
3555 | } | |
3556 | return middle; | |
3557 | } | |
ceaaf873 JM |
3558 | } |
3559 | ||
3560 | if (insert) | |
3561 | { | |
3562 | constructor_elt e = { dindex, NULL_TREE }; | |
3563 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e); | |
3564 | return end; | |
3565 | } | |
3566 | ||
3567 | return -1; | |
3568 | } | |
3569 | ||
37244b21 PP |
3570 | /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no |
3571 | matching constructor_elt exists, then add one to CTOR. | |
3572 | ||
3573 | As an optimization, if POS_HINT is non-negative then it is used as a guess | |
3574 | for the (integer) index of the matching constructor_elt within CTOR. */ | |
3575 | ||
3576 | static constructor_elt * | |
077dd9b3 | 3577 | get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1) |
37244b21 PP |
3578 | { |
3579 | /* Check the hint first. */ | |
3580 | if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor) | |
3581 | && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index) | |
3582 | return CONSTRUCTOR_ELT (ctor, pos_hint); | |
3583 | ||
3584 | tree type = TREE_TYPE (ctor); | |
3585 | if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE) | |
3586 | { | |
3587 | CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE); | |
3588 | return &CONSTRUCTOR_ELTS (ctor)->last(); | |
3589 | } | |
3590 | else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE) | |
3591 | { | |
e069285c PP |
3592 | if (TREE_CODE (index) == RANGE_EXPR) |
3593 | { | |
3594 | /* Support for RANGE_EXPR index lookups is currently limited to | |
3595 | accessing an existing element via POS_HINT, or appending a new | |
3596 | element to the end of CTOR. ??? Support for other access | |
3597 | patterns may also be needed. */ | |
3598 | vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor); | |
3599 | if (vec_safe_length (elts)) | |
3600 | { | |
3601 | tree lo = TREE_OPERAND (index, 0); | |
3602 | gcc_assert (array_index_cmp (elts->last().index, lo) < 0); | |
3603 | } | |
3604 | CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE); | |
3605 | return &elts->last(); | |
3606 | } | |
3607 | ||
37244b21 PP |
3608 | HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, /*insert*/true); |
3609 | gcc_assert (i >= 0); | |
3610 | constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i); | |
3611 | gcc_assert (cep->index == NULL_TREE | |
3612 | || TREE_CODE (cep->index) != RANGE_EXPR); | |
3613 | return cep; | |
3614 | } | |
3615 | else | |
3616 | { | |
94ff4c9d JM |
3617 | gcc_assert (TREE_CODE (index) == FIELD_DECL |
3618 | && (same_type_ignoring_top_level_qualifiers_p | |
3619 | (DECL_CONTEXT (index), TREE_TYPE (ctor)))); | |
37244b21 PP |
3620 | |
3621 | /* We must keep the CONSTRUCTOR's ELTS in FIELD order. | |
3622 | Usually we meet initializers in that order, but it is | |
3623 | possible for base types to be placed not in program | |
3624 | order. */ | |
3625 | tree fields = TYPE_FIELDS (DECL_CONTEXT (index)); | |
3626 | unsigned HOST_WIDE_INT idx = 0; | |
3627 | constructor_elt *cep = NULL; | |
3628 | ||
3629 | /* Check if we're changing the active member of a union. */ | |
3630 | if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor) | |
3631 | && CONSTRUCTOR_ELT (ctor, 0)->index != index) | |
3632 | vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0); | |
3633 | /* If the bit offset of INDEX is larger than that of the last | |
3634 | constructor_elt, then we can just immediately append a new | |
3635 | constructor_elt to the end of CTOR. */ | |
3636 | else if (CONSTRUCTOR_NELTS (ctor) | |
3637 | && tree_int_cst_compare (bit_position (index), | |
3638 | bit_position (CONSTRUCTOR_ELTS (ctor) | |
3639 | ->last().index)) > 0) | |
3640 | { | |
3641 | idx = CONSTRUCTOR_NELTS (ctor); | |
3642 | goto insert; | |
3643 | } | |
3644 | ||
3645 | /* Otherwise, we need to iterate over CTOR to find or insert INDEX | |
3646 | appropriately. */ | |
3647 | ||
3648 | for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep); | |
3649 | idx++, fields = DECL_CHAIN (fields)) | |
3650 | { | |
3651 | if (index == cep->index) | |
3652 | goto found; | |
3653 | ||
3654 | /* The field we're initializing must be on the field | |
3655 | list. Look to see if it is present before the | |
3656 | field the current ELT initializes. */ | |
3657 | for (; fields != cep->index; fields = DECL_CHAIN (fields)) | |
3658 | if (index == fields) | |
3659 | goto insert; | |
3660 | } | |
3661 | /* We fell off the end of the CONSTRUCTOR, so insert a new | |
3662 | entry at the end. */ | |
3663 | ||
3664 | insert: | |
3665 | { | |
3666 | constructor_elt ce = { index, NULL_TREE }; | |
3667 | ||
3668 | vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce); | |
3669 | cep = CONSTRUCTOR_ELT (ctor, idx); | |
3670 | } | |
3671 | found:; | |
3672 | ||
3673 | return cep; | |
3674 | } | |
3675 | } | |
3676 | ||
abdc16c8 MS |
3677 | /* Under the control of CTX, issue a detailed diagnostic for |
3678 | an out-of-bounds subscript INDEX into the expression ARRAY. */ | |
3679 | ||
3680 | static void | |
043666e0 | 3681 | diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index) |
abdc16c8 MS |
3682 | { |
3683 | if (!ctx->quiet) | |
3684 | { | |
3685 | tree arraytype = TREE_TYPE (array); | |
3686 | ||
3687 | /* Convert the unsigned array subscript to a signed integer to avoid | |
3688 | printing huge numbers for small negative values. */ | |
3689 | tree sidx = fold_convert (ssizetype, index); | |
043666e0 | 3690 | STRIP_ANY_LOCATION_WRAPPER (array); |
abdc16c8 MS |
3691 | if (DECL_P (array)) |
3692 | { | |
08b3748c | 3693 | if (TYPE_DOMAIN (arraytype)) |
043666e0 JM |
3694 | error_at (loc, "array subscript value %qE is outside the bounds " |
3695 | "of array %qD of type %qT", sidx, array, arraytype); | |
08b3748c | 3696 | else |
043666e0 JM |
3697 | error_at (loc, "nonzero array subscript %qE is used with array %qD of " |
3698 | "type %qT with unknown bounds", sidx, array, arraytype); | |
abdc16c8 MS |
3699 | inform (DECL_SOURCE_LOCATION (array), "declared here"); |
3700 | } | |
08b3748c | 3701 | else if (TYPE_DOMAIN (arraytype)) |
043666e0 JM |
3702 | error_at (loc, "array subscript value %qE is outside the bounds " |
3703 | "of array type %qT", sidx, arraytype); | |
08b3748c | 3704 | else |
043666e0 JM |
3705 | error_at (loc, "nonzero array subscript %qE is used with array of type %qT " |
3706 | "with unknown bounds", sidx, arraytype); | |
abdc16c8 MS |
3707 | } |
3708 | } | |
ceaaf873 | 3709 | |
74f8705e MP |
3710 | /* Return the number of elements for TYPE (which is an ARRAY_TYPE or |
3711 | a VECTOR_TYPE). */ | |
3712 | ||
3713 | static tree | |
3714 | get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type, | |
3715 | bool *non_constant_p, bool *overflow_p) | |
3716 | { | |
3717 | tree nelts; | |
3718 | if (TREE_CODE (type) == ARRAY_TYPE) | |
3719 | { | |
3720 | if (TYPE_DOMAIN (type)) | |
3721 | nelts = array_type_nelts_top (type); | |
3722 | else | |
3723 | nelts = size_zero_node; | |
3724 | } | |
3725 | else if (VECTOR_TYPE_P (type)) | |
3726 | nelts = size_int (TYPE_VECTOR_SUBPARTS (type)); | |
3727 | else | |
3728 | gcc_unreachable (); | |
3729 | ||
3730 | /* For VLAs, the number of elements won't be an integer constant. */ | |
3731 | nelts = cxx_eval_constant_expression (ctx, nelts, false, | |
3732 | non_constant_p, overflow_p); | |
3733 | return nelts; | |
3734 | } | |
3735 | ||
d6b46fca NS |
3736 | /* Extract element INDEX consisting of CHARS_PER_ELT chars from |
3737 | STRING_CST STRING. */ | |
3738 | ||
3739 | static tree | |
3740 | extract_string_elt (tree string, unsigned chars_per_elt, unsigned index) | |
3741 | { | |
3742 | tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string))); | |
3743 | tree r; | |
3744 | ||
3745 | if (chars_per_elt == 1) | |
3746 | r = build_int_cst (type, TREE_STRING_POINTER (string)[index]); | |
3747 | else | |
3748 | { | |
3749 | const unsigned char *ptr | |
3750 | = ((const unsigned char *)TREE_STRING_POINTER (string) | |
3751 | + index * chars_per_elt); | |
3752 | r = native_interpret_expr (type, ptr, chars_per_elt); | |
3753 | } | |
3754 | return r; | |
3755 | } | |
3756 | ||
043666e0 JM |
3757 | /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the |
3758 | subscript, diagnose any problems with it, and return the result. */ | |
3759 | ||
3760 | static tree | |
3761 | eval_and_check_array_index (const constexpr_ctx *ctx, | |
3762 | tree t, bool allow_one_past, | |
3763 | bool *non_constant_p, bool *overflow_p) | |
3764 | { | |
f9d0ca40 | 3765 | location_t loc = cp_expr_loc_or_input_loc (t); |
043666e0 JM |
3766 | tree ary = TREE_OPERAND (t, 0); |
3767 | t = TREE_OPERAND (t, 1); | |
3768 | tree index = cxx_eval_constant_expression (ctx, t, false, | |
3769 | non_constant_p, overflow_p); | |
3770 | VERIFY_CONSTANT (index); | |
3771 | ||
3772 | if (!tree_fits_shwi_p (index) | |
3773 | || tree_int_cst_sgn (index) < 0) | |
3774 | { | |
3775 | diag_array_subscript (loc, ctx, ary, index); | |
3776 | *non_constant_p = true; | |
3777 | return t; | |
3778 | } | |
3779 | ||
3780 | tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p, | |
3781 | overflow_p); | |
3782 | VERIFY_CONSTANT (nelts); | |
3783 | if (allow_one_past | |
3784 | ? !tree_int_cst_le (index, nelts) | |
3785 | : !tree_int_cst_lt (index, nelts)) | |
3786 | { | |
3787 | diag_array_subscript (loc, ctx, ary, index); | |
3788 | *non_constant_p = true; | |
3789 | return t; | |
3790 | } | |
3791 | ||
3792 | return index; | |
3793 | } | |
3794 | ||
2d76680f PC |
3795 | /* Subroutine of cxx_eval_constant_expression. |
3796 | Attempt to reduce a reference to an array slot. */ | |
3797 | ||
3798 | static tree | |
3e605b20 | 3799 | cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, |
92a596e8 | 3800 | bool lval, |
2d76680f PC |
3801 | bool *non_constant_p, bool *overflow_p) |
3802 | { | |
3803 | tree oldary = TREE_OPERAND (t, 0); | |
3e605b20 | 3804 | tree ary = cxx_eval_constant_expression (ctx, oldary, |
92a596e8 | 3805 | lval, |
5a804683 | 3806 | non_constant_p, overflow_p); |
2d76680f PC |
3807 | if (*non_constant_p) |
3808 | return t; | |
bc2687dd JJ |
3809 | if (!lval |
3810 | && TREE_CODE (ary) == VIEW_CONVERT_EXPR | |
043666e0 JM |
3811 | && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0))) |
3812 | && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0)))) | |
3813 | ary = TREE_OPERAND (ary, 0); | |
3b9997bb | 3814 | |
043666e0 JM |
3815 | tree oldidx = TREE_OPERAND (t, 1); |
3816 | tree index = eval_and_check_array_index (ctx, t, lval, | |
3817 | non_constant_p, overflow_p); | |
3818 | if (*non_constant_p) | |
3819 | return t; | |
5453bfed | 3820 | |
bc2a38df JJ |
3821 | if (lval && ary == oldary && index == oldidx) |
3822 | return t; | |
3823 | else if (lval) | |
3824 | return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL); | |
3825 | ||
043666e0 JM |
3826 | unsigned len = 0, elem_nchars = 1; |
3827 | tree elem_type = TREE_TYPE (TREE_TYPE (ary)); | |
3828 | if (TREE_CODE (ary) == CONSTRUCTOR) | |
3829 | len = CONSTRUCTOR_NELTS (ary); | |
3830 | else if (TREE_CODE (ary) == STRING_CST) | |
3831 | { | |
3832 | elem_nchars = (TYPE_PRECISION (elem_type) | |
3833 | / TYPE_PRECISION (char_type_node)); | |
3834 | len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars; | |
3835 | } | |
3836 | else if (TREE_CODE (ary) == VECTOR_CST) | |
3837 | /* We don't create variable-length VECTOR_CSTs. */ | |
3838 | len = VECTOR_CST_NELTS (ary).to_constant (); | |
3839 | else | |
3840 | { | |
3841 | /* We can't do anything with other tree codes, so use | |
3842 | VERIFY_CONSTANT to complain and fail. */ | |
3843 | VERIFY_CONSTANT (ary); | |
3844 | gcc_unreachable (); | |
3845 | } | |
3846 | ||
ceaaf873 | 3847 | bool found; |
043666e0 | 3848 | HOST_WIDE_INT i = 0; |
ceaaf873 JM |
3849 | if (TREE_CODE (ary) == CONSTRUCTOR) |
3850 | { | |
3851 | HOST_WIDE_INT ix = find_array_ctor_elt (ary, index); | |
3852 | found = (ix >= 0); | |
3853 | if (found) | |
3854 | i = ix; | |
3b9997bb | 3855 | } |
ceaaf873 | 3856 | else |
043666e0 JM |
3857 | { |
3858 | i = tree_to_shwi (index); | |
3859 | found = (i < len); | |
3860 | } | |
3b9997bb | 3861 | |
fd2bfee5 JM |
3862 | if (found) |
3863 | { | |
3864 | tree r; | |
3865 | if (TREE_CODE (ary) == CONSTRUCTOR) | |
3866 | r = (*CONSTRUCTOR_ELTS (ary))[i].value; | |
3867 | else if (TREE_CODE (ary) == VECTOR_CST) | |
3868 | r = VECTOR_CST_ELT (ary, i); | |
fd2bfee5 | 3869 | else |
d6b46fca NS |
3870 | r = extract_string_elt (ary, elem_nchars, i); |
3871 | ||
fd2bfee5 JM |
3872 | if (r) |
3873 | /* Don't VERIFY_CONSTANT here. */ | |
3874 | return r; | |
2d76680f | 3875 | |
fd2bfee5 | 3876 | /* Otherwise the element doesn't have a value yet. */ |
2d76680f | 3877 | } |
3b9997bb | 3878 | |
fd2bfee5 JM |
3879 | /* Not found. */ |
3880 | ||
3881 | if (TREE_CODE (ary) == CONSTRUCTOR | |
e8c48716 | 3882 | && CONSTRUCTOR_NO_CLEARING (ary)) |
2d76680f | 3883 | { |
fd2bfee5 JM |
3884 | /* 'ary' is part of the aggregate initializer we're currently |
3885 | building; if there's no initializer for this element yet, | |
3886 | that's an error. */ | |
3887 | if (!ctx->quiet) | |
3888 | error ("accessing uninitialized array element"); | |
3889 | *non_constant_p = true; | |
3890 | return t; | |
2d76680f | 3891 | } |
fd2bfee5 JM |
3892 | |
3893 | /* If it's within the array bounds but doesn't have an explicit | |
0712ea63 JM |
3894 | initializer, it's initialized from {}. But use build_value_init |
3895 | directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */ | |
3896 | tree val; | |
0df73bee | 3897 | constexpr_ctx new_ctx; |
a688c284 PP |
3898 | if (is_really_empty_class (elem_type, /*ignore_vptr*/false)) |
3899 | return build_constructor (elem_type, NULL); | |
3900 | else if (CP_AGGREGATE_TYPE_P (elem_type)) | |
0712ea63 JM |
3901 | { |
3902 | tree empty_ctor = build_constructor (init_list_type_node, NULL); | |
3903 | val = digest_init (elem_type, empty_ctor, tf_warning_or_error); | |
a42f8120 MP |
3904 | } |
3905 | else | |
3906 | val = build_value_init (elem_type, tf_warning_or_error); | |
3907 | ||
3908 | if (!SCALAR_TYPE_P (elem_type)) | |
3909 | { | |
0df73bee | 3910 | new_ctx = *ctx; |
a42f8120 MP |
3911 | if (ctx->object) |
3912 | /* If there was no object, don't add one: it could confuse us | |
3913 | into thinking we're modifying a const object. */ | |
3914 | new_ctx.object = t; | |
0df73bee MP |
3915 | new_ctx.ctor = build_constructor (elem_type, NULL); |
3916 | ctx = &new_ctx; | |
0712ea63 | 3917 | } |
0df73bee MP |
3918 | t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p, |
3919 | overflow_p); | |
a42f8120 | 3920 | if (!SCALAR_TYPE_P (elem_type) && t != ctx->ctor) |
0df73bee MP |
3921 | free_constructor (ctx->ctor); |
3922 | return t; | |
2d76680f PC |
3923 | } |
3924 | ||
3925 | /* Subroutine of cxx_eval_constant_expression. | |
3926 | Attempt to reduce a field access of a value of class type. */ | |
3927 | ||
3928 | static tree | |
3e605b20 | 3929 | cxx_eval_component_reference (const constexpr_ctx *ctx, tree t, |
92a596e8 | 3930 | bool lval, |
2d76680f PC |
3931 | bool *non_constant_p, bool *overflow_p) |
3932 | { | |
3933 | unsigned HOST_WIDE_INT i; | |
3934 | tree field; | |
3935 | tree value; | |
3936 | tree part = TREE_OPERAND (t, 1); | |
3937 | tree orig_whole = TREE_OPERAND (t, 0); | |
3e605b20 | 3938 | tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
92a596e8 | 3939 | lval, |
5a804683 | 3940 | non_constant_p, overflow_p); |
a7f8415c | 3941 | if (INDIRECT_REF_P (whole) |
bf533db8 JJ |
3942 | && integer_zerop (TREE_OPERAND (whole, 0))) |
3943 | { | |
3944 | if (!ctx->quiet) | |
3945 | error ("dereferencing a null pointer in %qE", orig_whole); | |
3946 | *non_constant_p = true; | |
3947 | return t; | |
3948 | } | |
8bada5cd | 3949 | |
dcdbc004 JM |
3950 | if (TREE_CODE (whole) == PTRMEM_CST) |
3951 | whole = cplus_expand_constant (whole); | |
2d76680f PC |
3952 | if (whole == orig_whole) |
3953 | return t; | |
92a596e8 | 3954 | if (lval) |
2d76680f PC |
3955 | return fold_build3 (COMPONENT_REF, TREE_TYPE (t), |
3956 | whole, part, NULL_TREE); | |
3957 | /* Don't VERIFY_CONSTANT here; we only want to check that we got a | |
3958 | CONSTRUCTOR. */ | |
3959 | if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR) | |
3960 | { | |
2b3ab879 | 3961 | if (!ctx->quiet) |
2d76680f PC |
3962 | error ("%qE is not a constant expression", orig_whole); |
3963 | *non_constant_p = true; | |
3964 | } | |
3965 | if (DECL_MUTABLE_P (part)) | |
3966 | { | |
2b3ab879 | 3967 | if (!ctx->quiet) |
2d76680f PC |
3968 | error ("mutable %qD is not usable in a constant expression", part); |
3969 | *non_constant_p = true; | |
3970 | } | |
3971 | if (*non_constant_p) | |
3972 | return t; | |
2db613e5 | 3973 | bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole)); |
2d76680f PC |
3974 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) |
3975 | { | |
2db613e5 JM |
3976 | /* Use name match for PMF fields, as a variant will have a |
3977 | different FIELD_DECL with a different type. */ | |
3978 | if (pmf ? DECL_NAME (field) == DECL_NAME (part) | |
3979 | : field == part) | |
60813a46 JM |
3980 | { |
3981 | if (value) | |
5b884e94 JJ |
3982 | { |
3983 | STRIP_ANY_LOCATION_WRAPPER (value); | |
3984 | return value; | |
3985 | } | |
60813a46 JM |
3986 | else |
3987 | /* We're in the middle of initializing it. */ | |
3988 | break; | |
3989 | } | |
2d76680f PC |
3990 | } |
3991 | if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE | |
3992 | && CONSTRUCTOR_NELTS (whole) > 0) | |
3993 | { | |
3994 | /* DR 1188 says we don't have to deal with this. */ | |
2b3ab879 | 3995 | if (!ctx->quiet) |
b599bf9d PP |
3996 | { |
3997 | constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0); | |
3998 | if (cep->value == NULL_TREE) | |
3999 | error ("accessing uninitialized member %qD", part); | |
4000 | else | |
4001 | error ("accessing %qD member instead of initialized %qD member in " | |
4002 | "constant expression", part, cep->index); | |
4003 | } | |
2d76680f PC |
4004 | *non_constant_p = true; |
4005 | return t; | |
4006 | } | |
4007 | ||
188e53bd JM |
4008 | /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty |
4009 | classes never get represented; throw together a value now. */ | |
dbcd32f8 | 4010 | if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
188e53bd JM |
4011 | return build_constructor (TREE_TYPE (t), NULL); |
4012 | ||
2db613e5 JM |
4013 | gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole))); |
4014 | ||
e8c48716 | 4015 | if (CONSTRUCTOR_NO_CLEARING (whole)) |
3e605b20 JM |
4016 | { |
4017 | /* 'whole' is part of the aggregate initializer we're currently | |
4018 | building; if there's no initializer for this member yet, that's an | |
188e53bd | 4019 | error. */ |
2b3ab879 | 4020 | if (!ctx->quiet) |
3e605b20 JM |
4021 | error ("accessing uninitialized member %qD", part); |
4022 | *non_constant_p = true; | |
4023 | return t; | |
4024 | } | |
4025 | ||
2d76680f PC |
4026 | /* If there's no explicit init for this field, it's value-initialized. */ |
4027 | value = build_value_init (TREE_TYPE (t), tf_warning_or_error); | |
3e605b20 | 4028 | return cxx_eval_constant_expression (ctx, value, |
92a596e8 | 4029 | lval, |
5a804683 | 4030 | non_constant_p, overflow_p); |
2d76680f PC |
4031 | } |
4032 | ||
4033 | /* Subroutine of cxx_eval_constant_expression. | |
4034 | Attempt to reduce a field access of a value of class type that is | |
4035 | expressed as a BIT_FIELD_REF. */ | |
4036 | ||
4037 | static tree | |
3e605b20 | 4038 | cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t, |
92a596e8 | 4039 | bool lval, |
2d76680f PC |
4040 | bool *non_constant_p, bool *overflow_p) |
4041 | { | |
4042 | tree orig_whole = TREE_OPERAND (t, 0); | |
4043 | tree retval, fldval, utype, mask; | |
4044 | bool fld_seen = false; | |
4045 | HOST_WIDE_INT istart, isize; | |
3e605b20 | 4046 | tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
92a596e8 | 4047 | lval, |
5a804683 | 4048 | non_constant_p, overflow_p); |
2d76680f PC |
4049 | tree start, field, value; |
4050 | unsigned HOST_WIDE_INT i; | |
4051 | ||
4052 | if (whole == orig_whole) | |
4053 | return t; | |
4054 | /* Don't VERIFY_CONSTANT here; we only want to check that we got a | |
4055 | CONSTRUCTOR. */ | |
4056 | if (!*non_constant_p | |
4057 | && TREE_CODE (whole) != VECTOR_CST | |
4058 | && TREE_CODE (whole) != CONSTRUCTOR) | |
4059 | { | |
2b3ab879 | 4060 | if (!ctx->quiet) |
2d76680f PC |
4061 | error ("%qE is not a constant expression", orig_whole); |
4062 | *non_constant_p = true; | |
4063 | } | |
4064 | if (*non_constant_p) | |
4065 | return t; | |
4066 | ||
4067 | if (TREE_CODE (whole) == VECTOR_CST) | |
4068 | return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole, | |
4069 | TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)); | |
4070 | ||
4071 | start = TREE_OPERAND (t, 2); | |
4072 | istart = tree_to_shwi (start); | |
4073 | isize = tree_to_shwi (TREE_OPERAND (t, 1)); | |
4074 | utype = TREE_TYPE (t); | |
4075 | if (!TYPE_UNSIGNED (utype)) | |
4076 | utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1); | |
4077 | retval = build_int_cst (utype, 0); | |
4078 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) | |
4079 | { | |
4080 | tree bitpos = bit_position (field); | |
5b884e94 | 4081 | STRIP_ANY_LOCATION_WRAPPER (value); |
2d76680f PC |
4082 | if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1)) |
4083 | return value; | |
4084 | if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE | |
4085 | && TREE_CODE (value) == INTEGER_CST | |
4086 | && tree_fits_shwi_p (bitpos) | |
4087 | && tree_fits_shwi_p (DECL_SIZE (field))) | |
4088 | { | |
4089 | HOST_WIDE_INT bit = tree_to_shwi (bitpos); | |
4090 | HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field)); | |
4091 | HOST_WIDE_INT shift; | |
4092 | if (bit >= istart && bit + sz <= istart + isize) | |
4093 | { | |
4094 | fldval = fold_convert (utype, value); | |
4095 | mask = build_int_cst_type (utype, -1); | |
4096 | mask = fold_build2 (LSHIFT_EXPR, utype, mask, | |
4097 | size_int (TYPE_PRECISION (utype) - sz)); | |
4098 | mask = fold_build2 (RSHIFT_EXPR, utype, mask, | |
4099 | size_int (TYPE_PRECISION (utype) - sz)); | |
4100 | fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask); | |
4101 | shift = bit - istart; | |
4102 | if (BYTES_BIG_ENDIAN) | |
4103 | shift = TYPE_PRECISION (utype) - shift - sz; | |
4104 | fldval = fold_build2 (LSHIFT_EXPR, utype, fldval, | |
4105 | size_int (shift)); | |
4106 | retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval); | |
4107 | fld_seen = true; | |
4108 | } | |
4109 | } | |
4110 | } | |
4111 | if (fld_seen) | |
4112 | return fold_convert (TREE_TYPE (t), retval); | |
4113 | gcc_unreachable (); | |
4114 | return error_mark_node; | |
4115 | } | |
4116 | ||
896048cf JJ |
4117 | /* Helper for cxx_eval_bit_cast. |
4118 | Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From | |
4119 | types and types of all subobjects have is_union_v<T>, is_pointer_v<T>, | |
4120 | is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static | |
4121 | data members of reference type. */ | |
4122 | ||
4123 | static bool | |
4124 | check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type, | |
4125 | tree orig_type) | |
4126 | { | |
4127 | if (TREE_CODE (type) == UNION_TYPE) | |
4128 | { | |
4129 | if (!ctx->quiet) | |
4130 | { | |
4131 | if (type == orig_type) | |
4132 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4133 | "a union type", "__builtin_bit_cast", type); | |
4134 | else | |
4135 | error_at (loc, "%qs is not a constant expression because %qT " | |
4136 | "contains a union type", "__builtin_bit_cast", | |
4137 | orig_type); | |
4138 | } | |
4139 | return true; | |
4140 | } | |
4141 | if (TREE_CODE (type) == POINTER_TYPE) | |
4142 | { | |
4143 | if (!ctx->quiet) | |
4144 | { | |
4145 | if (type == orig_type) | |
4146 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4147 | "a pointer type", "__builtin_bit_cast", type); | |
4148 | else | |
4149 | error_at (loc, "%qs is not a constant expression because %qT " | |
4150 | "contains a pointer type", "__builtin_bit_cast", | |
4151 | orig_type); | |
4152 | } | |
4153 | return true; | |
4154 | } | |
4155 | if (TREE_CODE (type) == REFERENCE_TYPE) | |
4156 | { | |
4157 | if (!ctx->quiet) | |
4158 | { | |
4159 | if (type == orig_type) | |
4160 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4161 | "a reference type", "__builtin_bit_cast", type); | |
4162 | else | |
4163 | error_at (loc, "%qs is not a constant expression because %qT " | |
4164 | "contains a reference type", "__builtin_bit_cast", | |
4165 | orig_type); | |
4166 | } | |
4167 | return true; | |
4168 | } | |
4169 | if (TYPE_PTRMEM_P (type)) | |
4170 | { | |
4171 | if (!ctx->quiet) | |
4172 | { | |
4173 | if (type == orig_type) | |
4174 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4175 | "a pointer to member type", "__builtin_bit_cast", | |
4176 | type); | |
4177 | else | |
4178 | error_at (loc, "%qs is not a constant expression because %qT " | |
4179 | "contains a pointer to member type", | |
4180 | "__builtin_bit_cast", orig_type); | |
4181 | } | |
4182 | return true; | |
4183 | } | |
4184 | if (TYPE_VOLATILE (type)) | |
4185 | { | |
4186 | if (!ctx->quiet) | |
4187 | { | |
4188 | if (type == orig_type) | |
4189 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4190 | "volatile", "__builtin_bit_cast", type); | |
4191 | else | |
4192 | error_at (loc, "%qs is not a constant expression because %qT " | |
4193 | "contains a volatile subobject", | |
4194 | "__builtin_bit_cast", orig_type); | |
4195 | } | |
4196 | return true; | |
4197 | } | |
4198 | if (TREE_CODE (type) == RECORD_TYPE) | |
4199 | for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) | |
4200 | if (TREE_CODE (field) == FIELD_DECL | |
4201 | && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type)) | |
4202 | return true; | |
4203 | return false; | |
4204 | } | |
4205 | ||
4206 | /* Subroutine of cxx_eval_constant_expression. | |
4207 | Attempt to evaluate a BIT_CAST_EXPR. */ | |
4208 | ||
4209 | static tree | |
4210 | cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p, | |
4211 | bool *overflow_p) | |
4212 | { | |
4213 | if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t), | |
4214 | TREE_TYPE (t)) | |
4215 | || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0), | |
4216 | EXPR_LOCATION (t)), | |
4217 | TREE_TYPE (TREE_OPERAND (t, 0)), | |
4218 | TREE_TYPE (TREE_OPERAND (t, 0)))) | |
4219 | { | |
4220 | *non_constant_p = true; | |
4221 | return t; | |
4222 | } | |
4223 | ||
4224 | tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false, | |
4225 | non_constant_p, overflow_p); | |
4226 | if (*non_constant_p) | |
4227 | return t; | |
4228 | ||
4229 | location_t loc = EXPR_LOCATION (t); | |
4230 | if (BITS_PER_UNIT != 8 || CHAR_BIT != 8) | |
4231 | { | |
4232 | if (!ctx->quiet) | |
4233 | sorry_at (loc, "%qs cannot be constant evaluated on the target", | |
4234 | "__builtin_bit_cast"); | |
4235 | *non_constant_p = true; | |
4236 | return t; | |
4237 | } | |
4238 | ||
4239 | if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t)))) | |
4240 | { | |
4241 | if (!ctx->quiet) | |
4242 | sorry_at (loc, "%qs cannot be constant evaluated because the " | |
4243 | "type is too large", "__builtin_bit_cast"); | |
4244 | *non_constant_p = true; | |
4245 | return t; | |
4246 | } | |
4247 | ||
4248 | HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t))); | |
4249 | if (len < 0 || (int) len != len) | |
4250 | { | |
4251 | if (!ctx->quiet) | |
4252 | sorry_at (loc, "%qs cannot be constant evaluated because the " | |
4253 | "type is too large", "__builtin_bit_cast"); | |
4254 | *non_constant_p = true; | |
4255 | return t; | |
4256 | } | |
4257 | ||
4258 | unsigned char buf[64]; | |
4259 | unsigned char *ptr, *mask; | |
4260 | size_t alen = (size_t) len * 2; | |
4261 | if (alen <= sizeof (buf)) | |
4262 | ptr = buf; | |
4263 | else | |
4264 | ptr = XNEWVEC (unsigned char, alen); | |
4265 | mask = ptr + (size_t) len; | |
4266 | /* At the beginning consider everything indeterminate. */ | |
4267 | memset (mask, ~0, (size_t) len); | |
4268 | ||
4269 | if (native_encode_initializer (op, ptr, len, 0, mask) != len) | |
4270 | { | |
4271 | if (!ctx->quiet) | |
4272 | sorry_at (loc, "%qs cannot be constant evaluated because the " | |
4273 | "argument cannot be encoded", "__builtin_bit_cast"); | |
4274 | *non_constant_p = true; | |
4275 | if (ptr != buf) | |
4276 | XDELETE (ptr); | |
4277 | return t; | |
4278 | } | |
4279 | ||
4280 | tree r = NULL_TREE; | |
4281 | if (can_native_interpret_type_p (TREE_TYPE (t))) | |
4282 | r = native_interpret_expr (TREE_TYPE (t), ptr, len); | |
4283 | else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE) | |
4284 | { | |
4285 | r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len); | |
4286 | if (r != NULL_TREE) | |
4287 | clear_type_padding_in_mask (TREE_TYPE (t), mask); | |
4288 | } | |
4289 | ||
4290 | if (r != NULL_TREE) | |
4291 | { | |
4292 | for (int i = 0; i < len; i++) | |
4293 | if (mask[i]) | |
4294 | { | |
4295 | if (!ctx->quiet) | |
4296 | error_at (loc, "%qs accessing uninitialized byte at offset %d", | |
4297 | "__builtin_bit_cast", i); | |
4298 | *non_constant_p = true; | |
4299 | r = t; | |
4300 | break; | |
4301 | } | |
4302 | if (ptr != buf) | |
4303 | XDELETE (ptr); | |
4304 | return r; | |
4305 | } | |
4306 | ||
4307 | if (!ctx->quiet) | |
4308 | sorry_at (loc, "%qs cannot be constant evaluated because the " | |
4309 | "argument cannot be interpreted", "__builtin_bit_cast"); | |
4310 | *non_constant_p = true; | |
4311 | if (ptr != buf) | |
4312 | XDELETE (ptr); | |
4313 | return t; | |
4314 | } | |
4315 | ||
2d76680f PC |
4316 | /* Subroutine of cxx_eval_constant_expression. |
4317 | Evaluate a short-circuited logical expression T in the context | |
4318 | of a given constexpr CALL. BAILOUT_VALUE is the value for | |
4319 | early return. CONTINUE_VALUE is used here purely for | |
4320 | sanity check purposes. */ | |
4321 | ||
4322 | static tree | |
3e605b20 | 4323 | cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t, |
2d76680f | 4324 | tree bailout_value, tree continue_value, |
92a596e8 | 4325 | bool lval, |
2d76680f PC |
4326 | bool *non_constant_p, bool *overflow_p) |
4327 | { | |
4328 | tree r; | |
3e605b20 | 4329 | tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
92a596e8 | 4330 | lval, |
5a804683 | 4331 | non_constant_p, overflow_p); |
2d76680f PC |
4332 | VERIFY_CONSTANT (lhs); |
4333 | if (tree_int_cst_equal (lhs, bailout_value)) | |
4334 | return lhs; | |
4335 | gcc_assert (tree_int_cst_equal (lhs, continue_value)); | |
3e605b20 | 4336 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
92a596e8 | 4337 | lval, non_constant_p, |
5a804683 | 4338 | overflow_p); |
2d76680f PC |
4339 | VERIFY_CONSTANT (r); |
4340 | return r; | |
4341 | } | |
4342 | ||
4343 | /* REF is a COMPONENT_REF designating a particular field. V is a vector of | |
4344 | CONSTRUCTOR elements to initialize (part of) an object containing that | |
4345 | field. Return a pointer to the constructor_elt corresponding to the | |
4346 | initialization of the field. */ | |
4347 | ||
4348 | static constructor_elt * | |
4349 | base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref) | |
4350 | { | |
4351 | tree aggr = TREE_OPERAND (ref, 0); | |
4352 | tree field = TREE_OPERAND (ref, 1); | |
4353 | HOST_WIDE_INT i; | |
4354 | constructor_elt *ce; | |
4355 | ||
4356 | gcc_assert (TREE_CODE (ref) == COMPONENT_REF); | |
4357 | ||
4358 | if (TREE_CODE (aggr) == COMPONENT_REF) | |
4359 | { | |
4360 | constructor_elt *base_ce | |
4361 | = base_field_constructor_elt (v, aggr); | |
4362 | v = CONSTRUCTOR_ELTS (base_ce->value); | |
4363 | } | |
4364 | ||
4365 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
4366 | if (ce->index == field) | |
4367 | return ce; | |
4368 | ||
4369 | gcc_unreachable (); | |
4370 | return NULL; | |
4371 | } | |
4372 | ||
3e605b20 JM |
4373 | /* Some of the expressions fed to the constexpr mechanism are calls to |
4374 | constructors, which have type void. In that case, return the type being | |
4375 | initialized by the constructor. */ | |
4376 | ||
4377 | static tree | |
4378 | initialized_type (tree t) | |
4379 | { | |
4380 | if (TYPE_P (t)) | |
4381 | return t; | |
5dab8b11 | 4382 | tree type = TREE_TYPE (t); |
4d0c18c6 | 4383 | if (TREE_CODE (t) == CALL_EXPR) |
3e605b20 JM |
4384 | { |
4385 | /* A constructor call has void type, so we need to look deeper. */ | |
4386 | tree fn = get_function_named_in_call (t); | |
4387 | if (fn && TREE_CODE (fn) == FUNCTION_DECL | |
4388 | && DECL_CXX_CONSTRUCTOR_P (fn)) | |
4389 | type = DECL_CONTEXT (fn); | |
4390 | } | |
4d0c18c6 JM |
4391 | else if (TREE_CODE (t) == COMPOUND_EXPR) |
4392 | return initialized_type (TREE_OPERAND (t, 1)); | |
5dab8b11 JM |
4393 | else if (TREE_CODE (t) == AGGR_INIT_EXPR) |
4394 | type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t)); | |
4395 | return cv_unqualified (type); | |
3e605b20 JM |
4396 | } |
4397 | ||
4398 | /* We're about to initialize element INDEX of an array or class from VALUE. | |
4399 | Set up NEW_CTX appropriately by adjusting .object to refer to the | |
4400 | subobject and creating a new CONSTRUCTOR if the element is itself | |
4401 | a class or array. */ | |
4402 | ||
4403 | static void | |
4404 | init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx, | |
4405 | tree index, tree &value) | |
4406 | { | |
4407 | new_ctx = *ctx; | |
4408 | ||
4409 | if (index && TREE_CODE (index) != INTEGER_CST | |
3d423c6f PP |
4410 | && TREE_CODE (index) != FIELD_DECL |
4411 | && TREE_CODE (index) != RANGE_EXPR) | |
3e605b20 JM |
4412 | /* This won't have an element in the new CONSTRUCTOR. */ |
4413 | return; | |
4414 | ||
4415 | tree type = initialized_type (value); | |
4416 | if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type)) | |
4417 | /* A non-aggregate member doesn't get its own CONSTRUCTOR. */ | |
4418 | return; | |
4419 | ||
4420 | /* The sub-aggregate initializer might contain a placeholder; | |
4421 | update object to refer to the subobject and ctor to refer to | |
4422 | the (newly created) sub-initializer. */ | |
4423 | if (ctx->object) | |
3d423c6f PP |
4424 | { |
4425 | if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR) | |
4426 | /* There's no well-defined subobject for this index. */ | |
4427 | new_ctx.object = NULL_TREE; | |
4428 | else | |
4429 | new_ctx.object = build_ctor_subob_ref (index, type, ctx->object); | |
4430 | } | |
3e605b20 | 4431 | tree elt = build_constructor (type, NULL); |
e8c48716 | 4432 | CONSTRUCTOR_NO_CLEARING (elt) = true; |
3e605b20 JM |
4433 | new_ctx.ctor = elt; |
4434 | ||
4435 | if (TREE_CODE (value) == TARGET_EXPR) | |
4436 | /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */ | |
4437 | value = TARGET_EXPR_INITIAL (value); | |
4438 | } | |
4439 | ||
4440 | /* We're about to process an initializer for a class or array TYPE. Make | |
4441 | sure that CTX is set up appropriately. */ | |
4442 | ||
4443 | static void | |
4444 | verify_ctor_sanity (const constexpr_ctx *ctx, tree type) | |
4445 | { | |
4446 | /* We don't bother building a ctor for an empty base subobject. */ | |
4447 | if (is_empty_class (type)) | |
4448 | return; | |
4449 | ||
4450 | /* We're in the middle of an initializer that might involve placeholders; | |
4451 | our caller should have created a CONSTRUCTOR for us to put the | |
4452 | initializer into. We will either return that constructor or T. */ | |
4453 | gcc_assert (ctx->ctor); | |
4454 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
4455 | (type, TREE_TYPE (ctx->ctor))); | |
fe69277d JM |
4456 | /* We used to check that ctx->ctor was empty, but that isn't the case when |
4457 | the object is zero-initialized before calling the constructor. */ | |
3e605b20 | 4458 | if (ctx->object) |
176e79b5 JM |
4459 | { |
4460 | tree otype = TREE_TYPE (ctx->object); | |
4461 | gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype) | |
4462 | /* Handle flexible array members. */ | |
4463 | || (TREE_CODE (otype) == ARRAY_TYPE | |
4464 | && TYPE_DOMAIN (otype) == NULL_TREE | |
4465 | && TREE_CODE (type) == ARRAY_TYPE | |
4466 | && (same_type_ignoring_top_level_qualifiers_p | |
4467 | (TREE_TYPE (type), TREE_TYPE (otype))))); | |
4468 | } | |
3e605b20 | 4469 | gcc_assert (!ctx->object || !DECL_P (ctx->object) |
8e007055 | 4470 | || *(ctx->global->values.get (ctx->object)) == ctx->ctor); |
3e605b20 JM |
4471 | } |
4472 | ||
2d76680f PC |
4473 | /* Subroutine of cxx_eval_constant_expression. |
4474 | The expression tree T denotes a C-style array or a C-style | |
4475 | aggregate. Reduce it to a constant expression. */ | |
4476 | ||
4477 | static tree | |
3e605b20 | 4478 | cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t, |
92a596e8 | 4479 | bool lval, |
2d76680f PC |
4480 | bool *non_constant_p, bool *overflow_p) |
4481 | { | |
4482 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); | |
2d76680f PC |
4483 | bool changed = false; |
4484 | gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t)); | |
8a29084d | 4485 | tree type = TREE_TYPE (t); |
3e605b20 | 4486 | |
8a29084d | 4487 | constexpr_ctx new_ctx; |
d4619dc1 | 4488 | if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type)) |
8a29084d | 4489 | { |
d4619dc1 NS |
4490 | /* We don't really need the ctx->ctor business for a PMF or |
4491 | vector, but it's simpler to use the same code. */ | |
8a29084d JM |
4492 | new_ctx = *ctx; |
4493 | new_ctx.ctor = build_constructor (type, NULL); | |
4494 | new_ctx.object = NULL_TREE; | |
4495 | ctx = &new_ctx; | |
4496 | }; | |
4497 | verify_ctor_sanity (ctx, type); | |
3e605b20 JM |
4498 | vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); |
4499 | vec_alloc (*p, vec_safe_length (v)); | |
4500 | ||
49a86fce PP |
4501 | if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t)) |
4502 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1; | |
4503 | ||
2d63bc39 JM |
4504 | unsigned i; |
4505 | tree index, value; | |
4506 | bool constant_p = true; | |
4507 | bool side_effects_p = false; | |
3e605b20 | 4508 | FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value) |
2d76680f | 4509 | { |
bcb5f3c9 | 4510 | tree orig_value = value; |
2168bfb8 JM |
4511 | /* Like in cxx_eval_store_expression, omit entries for empty fields. */ |
4512 | bool no_slot = TREE_CODE (type) == RECORD_TYPE && is_empty_field (index); | |
4513 | if (no_slot) | |
4514 | new_ctx = *ctx; | |
4515 | else | |
4516 | init_subob_ctx (ctx, new_ctx, index, value); | |
37244b21 | 4517 | int pos_hint = -1; |
3e605b20 | 4518 | if (new_ctx.ctor != ctx->ctor) |
37244b21 PP |
4519 | { |
4520 | /* If we built a new CONSTRUCTOR, attach it now so that other | |
4521 | initializers can refer to it. */ | |
077dd9b3 PP |
4522 | constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index); |
4523 | cep->value = new_ctx.ctor; | |
4524 | pos_hint = cep - (*p)->begin(); | |
37244b21 | 4525 | } |
b599bf9d | 4526 | else if (TREE_CODE (type) == UNION_TYPE) |
37244b21 PP |
4527 | /* Otherwise if we're constructing a non-aggregate union member, set |
4528 | the active union member now so that we can later detect and diagnose | |
4529 | if its initializer attempts to activate another member. */ | |
077dd9b3 | 4530 | get_or_insert_ctor_field (ctx->ctor, index); |
3e605b20 | 4531 | tree elt = cxx_eval_constant_expression (&new_ctx, value, |
92a596e8 | 4532 | lval, |
5a804683 | 4533 | non_constant_p, overflow_p); |
2d76680f | 4534 | /* Don't VERIFY_CONSTANT here. */ |
2b3ab879 | 4535 | if (ctx->quiet && *non_constant_p) |
3e605b20 | 4536 | break; |
bcb5f3c9 | 4537 | if (elt != orig_value) |
2d76680f | 4538 | changed = true; |
2d63bc39 JM |
4539 | |
4540 | if (!TREE_CONSTANT (elt)) | |
4541 | constant_p = false; | |
4542 | if (TREE_SIDE_EFFECTS (elt)) | |
4543 | side_effects_p = true; | |
3e605b20 | 4544 | if (index && TREE_CODE (index) == COMPONENT_REF) |
2d76680f PC |
4545 | { |
4546 | /* This is an initialization of a vfield inside a base | |
4547 | subaggregate that we already initialized; push this | |
4548 | initialization into the previous initialization. */ | |
3e605b20 | 4549 | constructor_elt *inner = base_field_constructor_elt (*p, index); |
2d76680f | 4550 | inner->value = elt; |
3e605b20 | 4551 | changed = true; |
2d76680f | 4552 | } |
3e605b20 JM |
4553 | else if (index |
4554 | && (TREE_CODE (index) == NOP_EXPR | |
4555 | || TREE_CODE (index) == POINTER_PLUS_EXPR)) | |
2d76680f PC |
4556 | { |
4557 | /* This is an initializer for an empty base; now that we've | |
4558 | checked that it's constant, we can ignore it. */ | |
3e605b20 JM |
4559 | gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index)))); |
4560 | changed = true; | |
4561 | } | |
2168bfb8 JM |
4562 | else if (no_slot) |
4563 | changed = true; | |
2d76680f | 4564 | else |
ac9ec198 | 4565 | { |
b599bf9d PP |
4566 | if (TREE_CODE (type) == UNION_TYPE |
4567 | && (*p)->last().index != index) | |
37244b21 PP |
4568 | /* The initializer erroneously changed the active union member that |
4569 | we're initializing. */ | |
b599bf9d | 4570 | gcc_assert (*non_constant_p); |
37244b21 | 4571 | else |
1bdbef09 | 4572 | { |
37244b21 PP |
4573 | /* The initializer might have mutated the underlying CONSTRUCTOR, |
4574 | so recompute the location of the target constructer_elt. */ | |
4575 | constructor_elt *cep | |
4576 | = get_or_insert_ctor_field (ctx->ctor, index, pos_hint); | |
4577 | cep->value = elt; | |
1bdbef09 | 4578 | } |
37244b21 | 4579 | |
1bdbef09 | 4580 | /* Adding or replacing an element might change the ctor's flags. */ |
ac9ec198 MP |
4581 | TREE_CONSTANT (ctx->ctor) = constant_p; |
4582 | TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p; | |
4583 | } | |
2d76680f PC |
4584 | } |
4585 | if (*non_constant_p || !changed) | |
3e605b20 JM |
4586 | return t; |
4587 | t = ctx->ctor; | |
4588 | /* We're done building this CONSTRUCTOR, so now we can interpret an | |
4589 | element without an explicit initializer as value-initialized. */ | |
e8c48716 | 4590 | CONSTRUCTOR_NO_CLEARING (t) = false; |
2d63bc39 JM |
4591 | TREE_CONSTANT (t) = constant_p; |
4592 | TREE_SIDE_EFFECTS (t) = side_effects_p; | |
8a29084d | 4593 | if (VECTOR_TYPE_P (type)) |
2d76680f PC |
4594 | t = fold (t); |
4595 | return t; | |
4596 | } | |
4597 | ||
4598 | /* Subroutine of cxx_eval_constant_expression. | |
4599 | The expression tree T is a VEC_INIT_EXPR which denotes the desired | |
4600 | initialization of a non-static data member of array type. Reduce it to a | |
4601 | CONSTRUCTOR. | |
4602 | ||
4603 | Note that apart from value-initialization (when VALUE_INIT is true), | |
4604 | this is only intended to support value-initialization and the | |
4605 | initializations done by defaulted constructors for classes with | |
4606 | non-static data members of array type. In this case, VEC_INIT_EXPR_INIT | |
4607 | will either be NULL_TREE for the default constructor, or a COMPONENT_REF | |
4608 | for the copy/move constructor. */ | |
4609 | ||
4610 | static tree | |
3e605b20 | 4611 | cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init, |
92a596e8 | 4612 | bool value_init, bool lval, |
2d76680f PC |
4613 | bool *non_constant_p, bool *overflow_p) |
4614 | { | |
4615 | tree elttype = TREE_TYPE (atype); | |
3e605b20 JM |
4616 | verify_ctor_sanity (ctx, atype); |
4617 | vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); | |
2d76680f | 4618 | bool pre_init = false; |
4784470a | 4619 | unsigned HOST_WIDE_INT i; |
dc5ca6c8 | 4620 | tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; |
2d76680f | 4621 | |
a1c9c9ff JM |
4622 | if (init && TREE_CODE (init) == CONSTRUCTOR) |
4623 | return cxx_eval_bare_aggregate (ctx, init, lval, | |
4624 | non_constant_p, overflow_p); | |
4625 | ||
2d76680f PC |
4626 | /* For the default constructor, build up a call to the default |
4627 | constructor of the element type. We only need to handle class types | |
4628 | here, as for a constructor to be constexpr, all members must be | |
4629 | initialized, which for a defaulted default constructor means they must | |
4630 | be of a class type with a constexpr default constructor. */ | |
4631 | if (TREE_CODE (elttype) == ARRAY_TYPE) | |
4632 | /* We only do this at the lowest level. */; | |
4633 | else if (value_init) | |
4634 | { | |
dc5ca6c8 | 4635 | init = build_value_init (elttype, complain); |
2d76680f PC |
4636 | pre_init = true; |
4637 | } | |
4638 | else if (!init) | |
4639 | { | |
cd9cf97b | 4640 | releasing_vec argvec; |
2d76680f PC |
4641 | init = build_special_member_call (NULL_TREE, complete_ctor_identifier, |
4642 | &argvec, elttype, LOOKUP_NORMAL, | |
dc5ca6c8 | 4643 | complain); |
5dab8b11 | 4644 | init = build_aggr_init_expr (elttype, init); |
2d76680f PC |
4645 | pre_init = true; |
4646 | } | |
4647 | ||
d21252de PP |
4648 | bool zeroed_out = false; |
4649 | if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor)) | |
4650 | { | |
4651 | /* We're initializing an array object that had been zero-initialized | |
4652 | earlier. Truncate ctx->ctor, and propagate its zeroed state by | |
4653 | clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element | |
4654 | initializers we append to it. */ | |
4655 | gcc_checking_assert (initializer_zerop (ctx->ctor)); | |
4656 | zeroed_out = true; | |
4657 | vec_safe_truncate (*p, 0); | |
4658 | } | |
4659 | ||
74f8705e MP |
4660 | tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p, |
4661 | overflow_p); | |
4662 | unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts); | |
4784470a | 4663 | for (i = 0; i < max; ++i) |
2d76680f PC |
4664 | { |
4665 | tree idx = build_int_cst (size_type_node, i); | |
4666 | tree eltinit; | |
928af3bf | 4667 | bool reuse = false; |
3e605b20 JM |
4668 | constexpr_ctx new_ctx; |
4669 | init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype); | |
4670 | if (new_ctx.ctor != ctx->ctor) | |
d21252de PP |
4671 | { |
4672 | if (zeroed_out) | |
4673 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false; | |
4674 | CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor); | |
4675 | } | |
2d76680f PC |
4676 | if (TREE_CODE (elttype) == ARRAY_TYPE) |
4677 | { | |
4678 | /* A multidimensional array; recurse. */ | |
4679 | if (value_init || init == NULL_TREE) | |
928af3bf JJ |
4680 | { |
4681 | eltinit = NULL_TREE; | |
4682 | reuse = i == 0; | |
4683 | } | |
2d76680f | 4684 | else |
dc5ca6c8 | 4685 | eltinit = cp_build_array_ref (input_location, init, idx, complain); |
3e605b20 | 4686 | eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init, |
92a596e8 | 4687 | lval, |
2d76680f PC |
4688 | non_constant_p, overflow_p); |
4689 | } | |
4690 | else if (pre_init) | |
4691 | { | |
4692 | /* Initializing an element using value or default initialization | |
4693 | we just pre-built above. */ | |
3ee378fb JM |
4694 | if (init == void_node) |
4695 | /* Trivial default-init, don't do anything to the CONSTRUCTOR. */ | |
4696 | return ctx->ctor; | |
928af3bf JJ |
4697 | eltinit = cxx_eval_constant_expression (&new_ctx, init, lval, |
4698 | non_constant_p, overflow_p); | |
4699 | reuse = i == 0; | |
2d76680f PC |
4700 | } |
4701 | else | |
4702 | { | |
4703 | /* Copying an element. */ | |
4704 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
4705 | (atype, TREE_TYPE (init))); | |
dc5ca6c8 | 4706 | eltinit = cp_build_array_ref (input_location, init, idx, complain); |
72b3e203 | 4707 | if (!lvalue_p (init)) |
2d76680f | 4708 | eltinit = move (eltinit); |
dc5ca6c8 | 4709 | eltinit = force_rvalue (eltinit, complain); |
c2236b9b JJ |
4710 | eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval, |
4711 | non_constant_p, overflow_p); | |
2d76680f | 4712 | } |
6b7d53a2 | 4713 | if (*non_constant_p) |
3e605b20 JM |
4714 | break; |
4715 | if (new_ctx.ctor != ctx->ctor) | |
4716 | { | |
4717 | /* We appended this element above; update the value. */ | |
4718 | gcc_assert ((*p)->last().index == idx); | |
4719 | (*p)->last().value = eltinit; | |
4720 | } | |
4721 | else | |
4722 | CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit); | |
928af3bf | 4723 | /* Reuse the result of cxx_eval_constant_expression call |
c2236b9b JJ |
4724 | from the first iteration to all others if it is a constant |
4725 | initializer that doesn't require relocations. */ | |
928af3bf JJ |
4726 | if (reuse |
4727 | && max > 1 | |
c2236b9b JJ |
4728 | && (eltinit == NULL_TREE |
4729 | || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit)) | |
4730 | == null_pointer_node))) | |
928af3bf JJ |
4731 | { |
4732 | if (new_ctx.ctor != ctx->ctor) | |
4733 | eltinit = new_ctx.ctor; | |
1e027956 RB |
4734 | tree range = build2 (RANGE_EXPR, size_type_node, |
4735 | build_int_cst (size_type_node, 1), | |
4736 | build_int_cst (size_type_node, max - 1)); | |
4737 | CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit)); | |
928af3bf JJ |
4738 | break; |
4739 | } | |
1e027956 RB |
4740 | else if (i == 0) |
4741 | vec_safe_reserve (*p, max); | |
2d76680f PC |
4742 | } |
4743 | ||
4744 | if (!*non_constant_p) | |
4745 | { | |
3e605b20 | 4746 | init = ctx->ctor; |
e8c48716 | 4747 | CONSTRUCTOR_NO_CLEARING (init) = false; |
2d76680f | 4748 | } |
2d76680f PC |
4749 | return init; |
4750 | } | |
4751 | ||
4752 | static tree | |
3e605b20 | 4753 | cxx_eval_vec_init (const constexpr_ctx *ctx, tree t, |
92a596e8 | 4754 | bool lval, |
2d76680f PC |
4755 | bool *non_constant_p, bool *overflow_p) |
4756 | { | |
4757 | tree atype = TREE_TYPE (t); | |
4758 | tree init = VEC_INIT_EXPR_INIT (t); | |
3e605b20 | 4759 | tree r = cxx_eval_vec_init_1 (ctx, atype, init, |
2d76680f | 4760 | VEC_INIT_EXPR_VALUE_INIT (t), |
92a596e8 | 4761 | lval, non_constant_p, overflow_p); |
2d76680f PC |
4762 | if (*non_constant_p) |
4763 | return t; | |
4764 | else | |
4765 | return r; | |
4766 | } | |
4767 | ||
e4511ca2 JM |
4768 | /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case |
4769 | where the desired type is an array of unknown bounds because the variable | |
4770 | has had its bounds deduced since the wrapping expression was created. */ | |
4771 | ||
4772 | static bool | |
4773 | same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2) | |
4774 | { | |
4775 | while (TREE_CODE (type1) == ARRAY_TYPE | |
4776 | && TREE_CODE (type2) == ARRAY_TYPE | |
4777 | && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2))) | |
4778 | { | |
4779 | type1 = TREE_TYPE (type1); | |
4780 | type2 = TREE_TYPE (type2); | |
4781 | } | |
4782 | return same_type_ignoring_top_level_qualifiers_p (type1, type2); | |
4783 | } | |
4784 | ||
43e84ce7 JJ |
4785 | /* Try to determine the currently active union member for an expression |
4786 | with UNION_TYPE. If it can be determined, return the FIELD_DECL, | |
4787 | otherwise return NULL_TREE. */ | |
4788 | ||
4789 | static tree | |
4790 | cxx_union_active_member (const constexpr_ctx *ctx, tree t) | |
4791 | { | |
4792 | constexpr_ctx new_ctx = *ctx; | |
4793 | new_ctx.quiet = true; | |
4794 | bool non_constant_p = false, overflow_p = false; | |
4795 | tree ctor = cxx_eval_constant_expression (&new_ctx, t, false, | |
4796 | &non_constant_p, | |
4797 | &overflow_p); | |
4798 | if (TREE_CODE (ctor) == CONSTRUCTOR | |
4799 | && CONSTRUCTOR_NELTS (ctor) == 1 | |
4800 | && CONSTRUCTOR_ELT (ctor, 0)->index | |
4801 | && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL) | |
4802 | return CONSTRUCTOR_ELT (ctor, 0)->index; | |
4803 | return NULL_TREE; | |
4804 | } | |
4805 | ||
0fe2ae29 JJ |
4806 | /* Helper function for cxx_fold_indirect_ref_1, called recursively. */ |
4807 | ||
4808 | static tree | |
43e84ce7 JJ |
4809 | cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type, |
4810 | tree op, unsigned HOST_WIDE_INT off, bool *empty_base) | |
0fe2ae29 JJ |
4811 | { |
4812 | tree optype = TREE_TYPE (op); | |
4813 | unsigned HOST_WIDE_INT const_nunits; | |
bc99c54d JM |
4814 | if (off == 0 && similar_type_p (optype, type)) |
4815 | return op; | |
4816 | else if (TREE_CODE (optype) == COMPLEX_TYPE | |
4817 | && similar_type_p (type, TREE_TYPE (optype))) | |
0fe2ae29 | 4818 | { |
0fe2ae29 | 4819 | /* *(foo *)&complexfoo => __real__ complexfoo */ |
bc99c54d | 4820 | if (off == 0) |
0fe2ae29 | 4821 | return build1_loc (loc, REALPART_EXPR, type, op); |
bc99c54d JM |
4822 | /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */ |
4823 | else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off) | |
4824 | return build1_loc (loc, IMAGPART_EXPR, type, op); | |
0fe2ae29 JJ |
4825 | } |
4826 | /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */ | |
4827 | else if (VECTOR_TYPE_P (optype) | |
4828 | && similar_type_p (type, TREE_TYPE (optype)) | |
4829 | && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits)) | |
4830 | { | |
4831 | unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type)); | |
4832 | unsigned HOST_WIDE_INT max_offset = part_width * const_nunits; | |
4833 | if (off < max_offset && off % part_width == 0) | |
4834 | { | |
4835 | tree index = bitsize_int (off * BITS_PER_UNIT); | |
4836 | return build3_loc (loc, BIT_FIELD_REF, type, op, | |
4837 | TYPE_SIZE (type), index); | |
4838 | } | |
4839 | } | |
4840 | /* ((foo *)&fooarray)[x] => fooarray[x] */ | |
4841 | else if (TREE_CODE (optype) == ARRAY_TYPE | |
4842 | && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype))) | |
4843 | && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype)))) | |
4844 | { | |
4845 | tree type_domain = TYPE_DOMAIN (optype); | |
4846 | tree min_val = size_zero_node; | |
4847 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
4848 | min_val = TYPE_MIN_VALUE (type_domain); | |
4849 | unsigned HOST_WIDE_INT el_sz | |
4850 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype))); | |
4851 | unsigned HOST_WIDE_INT idx = off / el_sz; | |
4852 | unsigned HOST_WIDE_INT rem = off % el_sz; | |
4853 | if (tree_fits_uhwi_p (min_val)) | |
4854 | { | |
4855 | tree index = size_int (idx + tree_to_uhwi (min_val)); | |
4856 | op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index, | |
4857 | NULL_TREE, NULL_TREE); | |
43e84ce7 | 4858 | return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem, |
0fe2ae29 JJ |
4859 | empty_base); |
4860 | } | |
4861 | } | |
4862 | /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */ | |
43e84ce7 JJ |
4863 | else if (TREE_CODE (optype) == RECORD_TYPE |
4864 | || TREE_CODE (optype) == UNION_TYPE) | |
0fe2ae29 | 4865 | { |
43e84ce7 JJ |
4866 | if (TREE_CODE (optype) == UNION_TYPE) |
4867 | /* For unions prefer the currently active member. */ | |
4868 | if (tree field = cxx_union_active_member (ctx, op)) | |
4869 | { | |
4870 | unsigned HOST_WIDE_INT el_sz | |
4871 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field))); | |
4872 | if (off < el_sz) | |
4873 | { | |
4874 | tree cop = build3 (COMPONENT_REF, TREE_TYPE (field), | |
4875 | op, field, NULL_TREE); | |
4876 | if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop, | |
4877 | off, empty_base)) | |
4878 | return ret; | |
4879 | } | |
4880 | } | |
0fe2ae29 JJ |
4881 | for (tree field = TYPE_FIELDS (optype); |
4882 | field; field = DECL_CHAIN (field)) | |
4883 | if (TREE_CODE (field) == FIELD_DECL | |
4884 | && TREE_TYPE (field) != error_mark_node | |
4885 | && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field)))) | |
4886 | { | |
4887 | tree pos = byte_position (field); | |
4888 | if (!tree_fits_uhwi_p (pos)) | |
4889 | continue; | |
4890 | unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos); | |
43e84ce7 | 4891 | unsigned HOST_WIDE_INT el_sz |
0fe2ae29 JJ |
4892 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field))); |
4893 | if (upos <= off && off < upos + el_sz) | |
4894 | { | |
4895 | tree cop = build3 (COMPONENT_REF, TREE_TYPE (field), | |
4896 | op, field, NULL_TREE); | |
43e84ce7 | 4897 | if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop, |
0fe2ae29 JJ |
4898 | off - upos, |
4899 | empty_base)) | |
4900 | return ret; | |
4901 | } | |
4902 | } | |
bc99c54d JM |
4903 | /* Also handle conversion to an empty base class, which |
4904 | is represented with a NOP_EXPR. */ | |
4905 | if (is_empty_class (type) | |
4906 | && CLASS_TYPE_P (optype) | |
4907 | && DERIVED_FROM_P (type, optype)) | |
4908 | { | |
4909 | *empty_base = true; | |
4910 | return op; | |
4911 | } | |
0fe2ae29 JJ |
4912 | } |
4913 | ||
4914 | return NULL_TREE; | |
4915 | } | |
4916 | ||
2d76680f PC |
4917 | /* A less strict version of fold_indirect_ref_1, which requires cv-quals to |
4918 | match. We want to be less strict for simple *& folding; if we have a | |
4919 | non-const temporary that we access through a const pointer, that should | |
4920 | work. We handle this here rather than change fold_indirect_ref_1 | |
4921 | because we're dealing with things like ADDR_EXPR of INTEGER_CST which | |
4922 | don't really make sense outside of constant expression evaluation. Also | |
4923 | we want to allow folding to COMPONENT_REF, which could cause trouble | |
0fe2ae29 | 4924 | with TBAA in fold_indirect_ref_1. */ |
2d76680f PC |
4925 | |
4926 | static tree | |
43e84ce7 JJ |
4927 | cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type, |
4928 | tree op0, bool *empty_base) | |
2d76680f | 4929 | { |
ebe4bf41 MP |
4930 | tree sub = op0; |
4931 | tree subtype; | |
4932 | poly_uint64 const_op01; | |
2d76680f | 4933 | |
80de0002 JM |
4934 | /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */ |
4935 | while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR | |
4936 | || TREE_CODE (sub) == VIEW_CONVERT_EXPR) | |
4937 | { | |
4938 | if (TREE_CODE (sub) == NOP_EXPR | |
4939 | && REINTERPRET_CAST_P (sub)) | |
4940 | return NULL_TREE; | |
4941 | sub = TREE_OPERAND (sub, 0); | |
4942 | } | |
4943 | ||
2d76680f | 4944 | subtype = TREE_TYPE (sub); |
71a93b08 | 4945 | if (!INDIRECT_TYPE_P (subtype)) |
2d76680f PC |
4946 | return NULL_TREE; |
4947 | ||
4948 | if (TREE_CODE (sub) == ADDR_EXPR) | |
4949 | { | |
4950 | tree op = TREE_OPERAND (sub, 0); | |
4951 | tree optype = TREE_TYPE (op); | |
4952 | ||
4953 | /* *&CONST_DECL -> to the value of the const decl. */ | |
4954 | if (TREE_CODE (op) == CONST_DECL) | |
4955 | return DECL_INITIAL (op); | |
4956 | /* *&p => p; make sure to handle *&"str"[cst] here. */ | |
1a120ec1 | 4957 | if (similar_type_p (optype, type)) |
2d76680f PC |
4958 | { |
4959 | tree fop = fold_read_from_constant_string (op); | |
4960 | if (fop) | |
4961 | return fop; | |
4962 | else | |
4963 | return op; | |
4964 | } | |
0fe2ae29 | 4965 | else |
43e84ce7 | 4966 | return cxx_fold_indirect_ref_1 (ctx, loc, type, op, 0, empty_base); |
2d76680f PC |
4967 | } |
4968 | else if (TREE_CODE (sub) == POINTER_PLUS_EXPR | |
0fe2ae29 | 4969 | && tree_fits_uhwi_p (TREE_OPERAND (sub, 1))) |
2d76680f PC |
4970 | { |
4971 | tree op00 = TREE_OPERAND (sub, 0); | |
0120cd93 | 4972 | tree off = TREE_OPERAND (sub, 1); |
2d76680f PC |
4973 | |
4974 | STRIP_NOPS (op00); | |
4975 | if (TREE_CODE (op00) == ADDR_EXPR) | |
0120cd93 PP |
4976 | { |
4977 | tree obj = TREE_OPERAND (op00, 0); | |
4978 | while (TREE_CODE (obj) == COMPONENT_REF | |
4979 | && tree_int_cst_sign_bit (off)) | |
4980 | { | |
4981 | /* Canonicalize this object/offset pair by iteratively absorbing | |
4982 | the innermost component into the offset until the offset is | |
4983 | nonnegative, so that cxx_fold_indirect_ref_1 can identify | |
4984 | more folding opportunities. */ | |
4985 | tree field = TREE_OPERAND (obj, 1); | |
4986 | off = int_const_binop (PLUS_EXPR, off, byte_position (field)); | |
4987 | obj = TREE_OPERAND (obj, 0); | |
4988 | } | |
4989 | return cxx_fold_indirect_ref_1 (ctx, loc, type, obj, | |
4990 | tree_to_uhwi (off), empty_base); | |
4991 | } | |
2d76680f PC |
4992 | } |
4993 | /* *(foo *)fooarrptr => (*fooarrptr)[0] */ | |
4994 | else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE | |
1a120ec1 | 4995 | && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype)))) |
2d76680f PC |
4996 | { |
4997 | tree type_domain; | |
4998 | tree min_val = size_zero_node; | |
ebe4bf41 | 4999 | tree newsub |
43e84ce7 | 5000 | = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL); |
2d76680f PC |
5001 | if (newsub) |
5002 | sub = newsub; | |
5003 | else | |
5004 | sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub); | |
5005 | type_domain = TYPE_DOMAIN (TREE_TYPE (sub)); | |
5006 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
5007 | min_val = TYPE_MIN_VALUE (type_domain); | |
5008 | return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE, | |
5009 | NULL_TREE); | |
5010 | } | |
5011 | ||
5012 | return NULL_TREE; | |
5013 | } | |
5014 | ||
5015 | static tree | |
3e605b20 | 5016 | cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t, |
92a596e8 | 5017 | bool lval, |
2d76680f PC |
5018 | bool *non_constant_p, bool *overflow_p) |
5019 | { | |
5020 | tree orig_op0 = TREE_OPERAND (t, 0); | |
2d76680f | 5021 | bool empty_base = false; |
2d76680f | 5022 | |
cda0a029 JM |
5023 | /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second |
5024 | operand is an integer-zero. Otherwise reject the MEM_REF for now. */ | |
5025 | ||
5026 | if (TREE_CODE (t) == MEM_REF | |
5027 | && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1)))) | |
5028 | { | |
5029 | gcc_assert (ctx->quiet); | |
5030 | *non_constant_p = true; | |
5031 | return t; | |
5032 | } | |
5033 | ||
255a48d6 | 5034 | /* First try to simplify it directly. */ |
43e84ce7 JJ |
5035 | tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), |
5036 | orig_op0, &empty_base); | |
255a48d6 | 5037 | if (!r) |
2d76680f | 5038 | { |
255a48d6 JM |
5039 | /* If that didn't work, evaluate the operand first. */ |
5040 | tree op0 = cxx_eval_constant_expression (ctx, orig_op0, | |
5041 | /*lval*/false, non_constant_p, | |
5042 | overflow_p); | |
5043 | /* Don't VERIFY_CONSTANT here. */ | |
5044 | if (*non_constant_p) | |
5045 | return t; | |
5046 | ||
8bada5cd MS |
5047 | if (!lval && integer_zerop (op0)) |
5048 | { | |
5049 | if (!ctx->quiet) | |
5050 | error ("dereferencing a null pointer"); | |
5051 | *non_constant_p = true; | |
5052 | return t; | |
5053 | } | |
5054 | ||
43e84ce7 | 5055 | r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0, |
255a48d6 JM |
5056 | &empty_base); |
5057 | if (r == NULL_TREE) | |
2d76680f PC |
5058 | { |
5059 | /* We couldn't fold to a constant value. Make sure it's not | |
5060 | something we should have been able to fold. */ | |
255a48d6 JM |
5061 | tree sub = op0; |
5062 | STRIP_NOPS (sub); | |
5063 | if (TREE_CODE (sub) == ADDR_EXPR) | |
5064 | { | |
1a120ec1 | 5065 | gcc_assert (!similar_type_p |
255a48d6 JM |
5066 | (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t))); |
5067 | /* DR 1188 says we don't have to deal with this. */ | |
5068 | if (!ctx->quiet) | |
1a120ec1 JM |
5069 | error_at (cp_expr_loc_or_input_loc (t), |
5070 | "accessing value of %qE through a %qT glvalue in a " | |
5071 | "constant expression", build_fold_indirect_ref (sub), | |
5072 | TREE_TYPE (t)); | |
255a48d6 JM |
5073 | *non_constant_p = true; |
5074 | return t; | |
5075 | } | |
5076 | ||
5077 | if (lval && op0 != orig_op0) | |
5078 | return build1 (INDIRECT_REF, TREE_TYPE (t), op0); | |
5079 | if (!lval) | |
5080 | VERIFY_CONSTANT (t); | |
2d76680f PC |
5081 | return t; |
5082 | } | |
5083 | } | |
5084 | ||
255a48d6 JM |
5085 | r = cxx_eval_constant_expression (ctx, r, |
5086 | lval, non_constant_p, overflow_p); | |
5087 | if (*non_constant_p) | |
5088 | return t; | |
5089 | ||
125db6a1 | 5090 | /* If we're pulling out the value of an empty base, just return an empty |
2d76680f | 5091 | CONSTRUCTOR. */ |
92a596e8 | 5092 | if (empty_base && !lval) |
2d76680f | 5093 | { |
2d76680f PC |
5094 | r = build_constructor (TREE_TYPE (t), NULL); |
5095 | TREE_CONSTANT (r) = true; | |
5096 | } | |
5097 | ||
2d76680f PC |
5098 | return r; |
5099 | } | |
5100 | ||
5101 | /* Complain about R, a VAR_DECL, not being usable in a constant expression. | |
5102 | Shared between potential_constant_expression and | |
5103 | cxx_eval_constant_expression. */ | |
5104 | ||
5105 | static void | |
6821245b | 5106 | non_const_var_error (location_t loc, tree r) |
2d76680f | 5107 | { |
8e007055 | 5108 | auto_diagnostic_group d; |
2d76680f | 5109 | tree type = TREE_TYPE (r); |
8e007055 | 5110 | if (DECL_NAME (r) == heap_uninit_identifier |
815baade JJ |
5111 | || DECL_NAME (r) == heap_identifier |
5112 | || DECL_NAME (r) == heap_vec_uninit_identifier | |
5113 | || DECL_NAME (r) == heap_vec_identifier) | |
8e007055 | 5114 | { |
6821245b JM |
5115 | error_at (loc, "the content of uninitialized storage is not usable " |
5116 | "in a constant expression"); | |
8e007055 JJ |
5117 | inform (DECL_SOURCE_LOCATION (r), "allocated here"); |
5118 | return; | |
5119 | } | |
5120 | if (DECL_NAME (r) == heap_deleted_identifier) | |
5121 | { | |
6821245b JM |
5122 | error_at (loc, "use of allocated storage after deallocation in a " |
5123 | "constant expression"); | |
8e007055 JJ |
5124 | inform (DECL_SOURCE_LOCATION (r), "allocated here"); |
5125 | return; | |
5126 | } | |
6821245b JM |
5127 | error_at (loc, "the value of %qD is not usable in a constant " |
5128 | "expression", r); | |
2d76680f PC |
5129 | /* Avoid error cascade. */ |
5130 | if (DECL_INITIAL (r) == error_mark_node) | |
5131 | return; | |
5132 | if (DECL_DECLARED_CONSTEXPR_P (r)) | |
5133 | inform (DECL_SOURCE_LOCATION (r), | |
5134 | "%qD used in its own initializer", r); | |
5135 | else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) | |
5136 | { | |
5137 | if (!CP_TYPE_CONST_P (type)) | |
5138 | inform (DECL_SOURCE_LOCATION (r), | |
5139 | "%q#D is not const", r); | |
5140 | else if (CP_TYPE_VOLATILE_P (type)) | |
5141 | inform (DECL_SOURCE_LOCATION (r), | |
5142 | "%q#D is volatile", r); | |
5143 | else if (!DECL_INITIAL (r) | |
a3e2b438 PP |
5144 | || !TREE_CONSTANT (DECL_INITIAL (r)) |
5145 | || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)) | |
2d76680f PC |
5146 | inform (DECL_SOURCE_LOCATION (r), |
5147 | "%qD was not initialized with a constant " | |
5148 | "expression", r); | |
5149 | else | |
5150 | gcc_unreachable (); | |
5151 | } | |
9f613f06 | 5152 | else if (TYPE_REF_P (type)) |
fd338b13 JM |
5153 | inform (DECL_SOURCE_LOCATION (r), |
5154 | "%qD was not initialized with a constant " | |
5155 | "expression", r); | |
2d76680f PC |
5156 | else |
5157 | { | |
5158 | if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r)) | |
5159 | inform (DECL_SOURCE_LOCATION (r), | |
5160 | "%qD was not declared %<constexpr%>", r); | |
5161 | else | |
5162 | inform (DECL_SOURCE_LOCATION (r), | |
5163 | "%qD does not have integral or enumeration type", | |
5164 | r); | |
5165 | } | |
5166 | } | |
5167 | ||
5168 | /* Subroutine of cxx_eval_constant_expression. | |
5169 | Like cxx_eval_unary_expression, except for trinary expressions. */ | |
5170 | ||
5171 | static tree | |
3e605b20 | 5172 | cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t, |
92a596e8 | 5173 | bool lval, |
2d76680f PC |
5174 | bool *non_constant_p, bool *overflow_p) |
5175 | { | |
5176 | int i; | |
5177 | tree args[3]; | |
5178 | tree val; | |
5179 | ||
5180 | for (i = 0; i < 3; i++) | |
5181 | { | |
3e605b20 | 5182 | args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i), |
92a596e8 | 5183 | lval, |
5a804683 | 5184 | non_constant_p, overflow_p); |
2d76680f PC |
5185 | VERIFY_CONSTANT (args[i]); |
5186 | } | |
5187 | ||
5188 | val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t), | |
5189 | args[0], args[1], args[2]); | |
5190 | if (val == NULL_TREE) | |
5191 | return t; | |
5192 | VERIFY_CONSTANT (val); | |
5193 | return val; | |
5194 | } | |
5195 | ||
98e5a19a JM |
5196 | /* True if T was declared in a function declared to be constexpr, and |
5197 | therefore potentially constant in C++14. */ | |
5198 | ||
2d76680f PC |
5199 | bool |
5200 | var_in_constexpr_fn (tree t) | |
5201 | { | |
5202 | tree ctx = DECL_CONTEXT (t); | |
98e5a19a | 5203 | return (ctx && TREE_CODE (ctx) == FUNCTION_DECL |
2d76680f PC |
5204 | && DECL_DECLARED_CONSTEXPR_P (ctx)); |
5205 | } | |
5206 | ||
26dbe85a MP |
5207 | /* True if a function might be constexpr: either a function that was |
5208 | declared constexpr, or a C++17 lambda op(). */ | |
5209 | ||
5210 | bool | |
5211 | maybe_constexpr_fn (tree t) | |
5212 | { | |
5213 | return (DECL_DECLARED_CONSTEXPR_P (t) | |
5214 | || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t))); | |
5215 | } | |
5216 | ||
98e5a19a JM |
5217 | /* True if T was declared in a function that might be constexpr: either a |
5218 | function that was declared constexpr, or a C++17 lambda op(). */ | |
5219 | ||
5220 | bool | |
5221 | var_in_maybe_constexpr_fn (tree t) | |
5222 | { | |
7b936140 | 5223 | if (cxx_dialect >= cxx17 |
98e5a19a JM |
5224 | && DECL_FUNCTION_SCOPE_P (t) |
5225 | && LAMBDA_FUNCTION_P (DECL_CONTEXT (t))) | |
5226 | return true; | |
5227 | return var_in_constexpr_fn (t); | |
5228 | } | |
5229 | ||
68d01920 JM |
5230 | /* We're assigning INIT to TARGET. In do_build_copy_constructor and |
5231 | build_over_call we implement trivial copy of a class with tail padding using | |
5232 | assignment of character arrays, which is valid in normal code, but not in | |
5233 | constexpr evaluation. We don't need to worry about clobbering tail padding | |
5234 | in constexpr evaluation, so strip the type punning. */ | |
5235 | ||
5236 | static void | |
5237 | maybe_simplify_trivial_copy (tree &target, tree &init) | |
5238 | { | |
5239 | if (TREE_CODE (target) == MEM_REF | |
5240 | && TREE_CODE (init) == MEM_REF | |
5241 | && TREE_TYPE (target) == TREE_TYPE (init) | |
5242 | && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE | |
5243 | && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node) | |
5244 | { | |
5245 | target = build_fold_indirect_ref (TREE_OPERAND (target, 0)); | |
5246 | init = build_fold_indirect_ref (TREE_OPERAND (init, 0)); | |
5247 | } | |
5248 | } | |
5249 | ||
7eb5be6a MP |
5250 | /* Returns true if REF, which is a COMPONENT_REF, has any fields |
5251 | of constant type. This does not check for 'mutable', so the | |
5252 | caller is expected to be mindful of that. */ | |
5253 | ||
5254 | static bool | |
5255 | cref_has_const_field (tree ref) | |
5256 | { | |
5257 | while (TREE_CODE (ref) == COMPONENT_REF) | |
5258 | { | |
5259 | if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1)))) | |
5260 | return true; | |
5261 | ref = TREE_OPERAND (ref, 0); | |
5262 | } | |
5263 | return false; | |
5264 | } | |
5265 | ||
04e1749c MP |
5266 | /* Return true if we are modifying something that is const during constant |
5267 | expression evaluation. CODE is the code of the statement, OBJ is the | |
5268 | object in question, MUTABLE_P is true if one of the subobjects were | |
5269 | declared mutable. */ | |
5270 | ||
5271 | static bool | |
5272 | modifying_const_object_p (tree_code code, tree obj, bool mutable_p) | |
5273 | { | |
5274 | /* If this is initialization, there's no problem. */ | |
5275 | if (code != MODIFY_EXPR) | |
5276 | return false; | |
5277 | ||
5278 | /* [basic.type.qualifier] "A const object is an object of type | |
5279 | const T or a non-mutable subobject of a const object." */ | |
5280 | if (mutable_p) | |
5281 | return false; | |
5282 | ||
7eb5be6a MP |
5283 | if (TREE_READONLY (obj)) |
5284 | return true; | |
5285 | ||
5286 | if (CP_TYPE_CONST_P (TREE_TYPE (obj))) | |
5287 | { | |
5288 | /* Although a COMPONENT_REF may have a const type, we should | |
5289 | only consider it modifying a const object when any of the | |
5290 | field components is const. This can happen when using | |
5291 | constructs such as const_cast<const T &>(m), making something | |
5292 | const even though it wasn't declared const. */ | |
5293 | if (TREE_CODE (obj) == COMPONENT_REF) | |
5294 | return cref_has_const_field (obj); | |
5295 | else | |
5296 | return true; | |
5297 | } | |
5298 | ||
5299 | return false; | |
04e1749c MP |
5300 | } |
5301 | ||
60813a46 JM |
5302 | /* Evaluate an INIT_EXPR or MODIFY_EXPR. */ |
5303 | ||
5304 | static tree | |
5305 | cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, | |
92a596e8 | 5306 | bool lval, |
60813a46 JM |
5307 | bool *non_constant_p, bool *overflow_p) |
5308 | { | |
5309 | constexpr_ctx new_ctx = *ctx; | |
5310 | ||
58cc255c JM |
5311 | tree init = TREE_OPERAND (t, 1); |
5312 | if (TREE_CLOBBER_P (init)) | |
5313 | /* Just ignore clobbers. */ | |
5314 | return void_node; | |
5315 | ||
60813a46 JM |
5316 | /* First we figure out where we're storing to. */ |
5317 | tree target = TREE_OPERAND (t, 0); | |
68d01920 JM |
5318 | |
5319 | maybe_simplify_trivial_copy (target, init); | |
5320 | ||
3f8e2835 | 5321 | tree type = TREE_TYPE (target); |
e8b3c1bc JM |
5322 | bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR; |
5323 | if (preeval) | |
5324 | { | |
5325 | /* Evaluate the value to be stored without knowing what object it will be | |
5326 | stored in, so that any side-effects happen first. */ | |
5327 | if (!SCALAR_TYPE_P (type)) | |
5328 | new_ctx.ctor = new_ctx.object = NULL_TREE; | |
5329 | init = cxx_eval_constant_expression (&new_ctx, init, false, | |
5330 | non_constant_p, overflow_p); | |
5331 | if (*non_constant_p) | |
5332 | return t; | |
5333 | } | |
60813a46 | 5334 | |
d0aa42d2 JM |
5335 | bool evaluated = false; |
5336 | if (lval) | |
3f8e2835 | 5337 | { |
d0aa42d2 JM |
5338 | /* If we want to return a reference to the target, we need to evaluate it |
5339 | as a whole; otherwise, only evaluate the innermost piece to avoid | |
5340 | building up unnecessary *_REFs. */ | |
5341 | target = cxx_eval_constant_expression (ctx, target, true, | |
5342 | non_constant_p, overflow_p); | |
5343 | evaluated = true; | |
5344 | if (*non_constant_p) | |
5345 | return t; | |
3f8e2835 JM |
5346 | } |
5347 | ||
043666e0 | 5348 | /* Find the underlying variable. */ |
cd9cf97b | 5349 | releasing_vec refs; |
60813a46 | 5350 | tree object = NULL_TREE; |
04e1749c MP |
5351 | /* If we're modifying a const object, save it. */ |
5352 | tree const_object_being_modified = NULL_TREE; | |
5353 | bool mutable_p = false; | |
60813a46 JM |
5354 | for (tree probe = target; object == NULL_TREE; ) |
5355 | { | |
5356 | switch (TREE_CODE (probe)) | |
5357 | { | |
5358 | case BIT_FIELD_REF: | |
5359 | case COMPONENT_REF: | |
5360 | case ARRAY_REF: | |
043666e0 JM |
5361 | { |
5362 | tree ob = TREE_OPERAND (probe, 0); | |
5363 | tree elt = TREE_OPERAND (probe, 1); | |
7d349dd8 | 5364 | if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt)) |
04e1749c | 5365 | mutable_p = true; |
043666e0 JM |
5366 | if (TREE_CODE (probe) == ARRAY_REF) |
5367 | { | |
5368 | elt = eval_and_check_array_index (ctx, probe, false, | |
5369 | non_constant_p, overflow_p); | |
5370 | if (*non_constant_p) | |
5371 | return t; | |
5372 | } | |
0f184800 MP |
5373 | /* We don't check modifying_const_object_p for ARRAY_REFs. Given |
5374 | "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though | |
5375 | the array isn't const. Instead, check "a" in the next iteration; | |
5376 | that will detect modifying "const int a[10]". */ | |
5377 | else if (evaluated | |
5378 | && modifying_const_object_p (TREE_CODE (t), probe, | |
5379 | mutable_p) | |
5380 | && const_object_being_modified == NULL_TREE) | |
5381 | const_object_being_modified = probe; | |
043666e0 JM |
5382 | vec_safe_push (refs, elt); |
5383 | vec_safe_push (refs, TREE_TYPE (probe)); | |
5384 | probe = ob; | |
5385 | } | |
60813a46 JM |
5386 | break; |
5387 | ||
5388 | default: | |
d0aa42d2 JM |
5389 | if (evaluated) |
5390 | object = probe; | |
5391 | else | |
5392 | { | |
5393 | probe = cxx_eval_constant_expression (ctx, probe, true, | |
5394 | non_constant_p, overflow_p); | |
5395 | evaluated = true; | |
5396 | if (*non_constant_p) | |
5397 | return t; | |
5398 | } | |
5399 | break; | |
60813a46 JM |
5400 | } |
5401 | } | |
5402 | ||
04e1749c MP |
5403 | if (modifying_const_object_p (TREE_CODE (t), object, mutable_p) |
5404 | && const_object_being_modified == NULL_TREE) | |
5405 | const_object_being_modified = object; | |
5406 | ||
60813a46 JM |
5407 | /* And then find/build up our initializer for the path to the subobject |
5408 | we're initializing. */ | |
a1408b7e | 5409 | tree *valp; |
077dd9b3 | 5410 | if (DECL_P (object)) |
8e007055 | 5411 | valp = ctx->global->values.get (object); |
a1408b7e JM |
5412 | else |
5413 | valp = NULL; | |
60813a46 JM |
5414 | if (!valp) |
5415 | { | |
5416 | /* A constant-expression cannot modify objects from outside the | |
5417 | constant-expression. */ | |
2b3ab879 | 5418 | if (!ctx->quiet) |
64d6d399 | 5419 | error ("modification of %qE is not a constant expression", object); |
60813a46 JM |
5420 | *non_constant_p = true; |
5421 | return t; | |
5422 | } | |
3f8e2835 | 5423 | type = TREE_TYPE (object); |
c75ce530 | 5424 | bool no_zero_init = true; |
2d63bc39 | 5425 | |
37244b21 PP |
5426 | releasing_vec ctors, indexes; |
5427 | auto_vec<int> index_pos_hints; | |
5428 | bool activated_union_member_p = false; | |
276a52d5 | 5429 | while (!refs->is_empty ()) |
60813a46 JM |
5430 | { |
5431 | if (*valp == NULL_TREE) | |
5432 | { | |
5433 | *valp = build_constructor (type, NULL); | |
e8c48716 | 5434 | CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; |
60813a46 | 5435 | } |
d6b46fca NS |
5436 | else if (TREE_CODE (*valp) == STRING_CST) |
5437 | { | |
5438 | /* An array was initialized with a string constant, and now | |
5439 | we're writing into one of its elements. Explode the | |
5440 | single initialization into a set of element | |
5441 | initializations. */ | |
5442 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); | |
5443 | ||
5444 | tree string = *valp; | |
5445 | tree elt_type = TREE_TYPE (type); | |
5446 | unsigned chars_per_elt = (TYPE_PRECISION (elt_type) | |
5447 | / TYPE_PRECISION (char_type_node)); | |
5448 | unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt; | |
5449 | tree ary_ctor = build_constructor (type, NULL); | |
5450 | ||
5451 | vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts); | |
5452 | for (unsigned ix = 0; ix != num_elts; ix++) | |
5453 | { | |
5454 | constructor_elt elt = | |
5455 | { | |
5456 | build_int_cst (size_type_node, ix), | |
5457 | extract_string_elt (string, chars_per_elt, ix) | |
5458 | }; | |
5459 | CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt); | |
5460 | } | |
94ff4c9d | 5461 | |
d6b46fca NS |
5462 | *valp = ary_ctor; |
5463 | } | |
5464 | ||
c75ce530 JM |
5465 | /* If the value of object is already zero-initialized, any new ctors for |
5466 | subobjects will also be zero-initialized. */ | |
e8c48716 | 5467 | no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp); |
60813a46 | 5468 | |
ceaaf873 | 5469 | enum tree_code code = TREE_CODE (type); |
60813a46 | 5470 | type = refs->pop(); |
ceaaf873 | 5471 | tree index = refs->pop(); |
60813a46 | 5472 | |
a8dd2b3e | 5473 | if (code == RECORD_TYPE && is_empty_field (index)) |
a4dfd0f0 JM |
5474 | /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they |
5475 | have no data and might have an offset lower than previously declared | |
5476 | fields, which confuses the middle-end. The code below will notice | |
5477 | that we don't have a CONSTRUCTOR for our inner target and just | |
5478 | return init. */ | |
5479 | break; | |
94ff4c9d | 5480 | |
37244b21 PP |
5481 | if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp) |
5482 | && CONSTRUCTOR_ELT (*valp, 0)->index != index) | |
ceaaf873 | 5483 | { |
b04445d4 | 5484 | if (cxx_dialect < cxx20) |
ed4ec9ce | 5485 | { |
37244b21 PP |
5486 | if (!ctx->quiet) |
5487 | error_at (cp_expr_loc_or_input_loc (t), | |
5488 | "change of the active member of a union " | |
5489 | "from %qD to %qD", | |
5490 | CONSTRUCTOR_ELT (*valp, 0)->index, | |
5491 | index); | |
5492 | *non_constant_p = true; | |
ed4ec9ce | 5493 | } |
37244b21 PP |
5494 | else if (TREE_CODE (t) == MODIFY_EXPR |
5495 | && CONSTRUCTOR_NO_CLEARING (*valp)) | |
ceaaf873 | 5496 | { |
37244b21 PP |
5497 | /* Diagnose changing the active union member while the union |
5498 | is in the process of being initialized. */ | |
5499 | if (!ctx->quiet) | |
5500 | error_at (cp_expr_loc_or_input_loc (t), | |
5501 | "change of the active member of a union " | |
5502 | "from %qD to %qD during initialization", | |
5503 | CONSTRUCTOR_ELT (*valp, 0)->index, | |
5504 | index); | |
5505 | *non_constant_p = true; | |
ceaaf873 | 5506 | } |
37244b21 PP |
5507 | no_zero_init = true; |
5508 | } | |
88504f34 | 5509 | |
37244b21 PP |
5510 | vec_safe_push (ctors, *valp); |
5511 | vec_safe_push (indexes, index); | |
88504f34 | 5512 | |
37244b21 | 5513 | constructor_elt *cep |
077dd9b3 | 5514 | = get_or_insert_ctor_field (*valp, index); |
37244b21 PP |
5515 | index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin()); |
5516 | ||
5517 | if (code == UNION_TYPE) | |
5518 | activated_union_member_p = true; | |
b599bf9d | 5519 | |
60813a46 JM |
5520 | valp = &cep->value; |
5521 | } | |
60813a46 | 5522 | |
04e1749c MP |
5523 | /* Detect modifying a constant object in constexpr evaluation. |
5524 | We have found a const object that is being modified. Figure out | |
5525 | if we need to issue an error. Consider | |
5526 | ||
5527 | struct A { | |
5528 | int n; | |
5529 | constexpr A() : n(1) { n = 2; } // #1 | |
5530 | }; | |
5531 | struct B { | |
5532 | const A a; | |
5533 | constexpr B() { a.n = 3; } // #2 | |
5534 | }; | |
5535 | constexpr B b{}; | |
5536 | ||
5537 | #1 is OK, since we're modifying an object under construction, but | |
5538 | #2 is wrong, since "a" is const and has been fully constructed. | |
5539 | To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR | |
5540 | which means that the object is read-only. For the example above, the | |
5541 | *ctors stack at the point of #2 will look like: | |
5542 | ||
5543 | ctors[0] = {.a={.n=2}} TREE_READONLY = 0 | |
5544 | ctors[1] = {.n=2} TREE_READONLY = 1 | |
5545 | ||
5546 | and we're modifying "b.a", so we search the stack and see if the | |
5547 | constructor for "b.a" has already run. */ | |
5548 | if (const_object_being_modified) | |
5549 | { | |
5550 | bool fail = false; | |
276a52d5 JJ |
5551 | tree const_objtype |
5552 | = strip_array_types (TREE_TYPE (const_object_being_modified)); | |
5553 | if (!CLASS_TYPE_P (const_objtype)) | |
04e1749c MP |
5554 | fail = true; |
5555 | else | |
5556 | { | |
5557 | /* [class.ctor]p5 "A constructor can be invoked for a const, | |
5558 | volatile, or const volatile object. const and volatile | |
5559 | semantics are not applied on an object under construction. | |
5560 | They come into effect when the constructor for the most | |
5561 | derived object ends." */ | |
3f207ab3 | 5562 | for (tree elt : *ctors) |
04e1749c MP |
5563 | if (same_type_ignoring_top_level_qualifiers_p |
5564 | (TREE_TYPE (const_object_being_modified), TREE_TYPE (elt))) | |
5565 | { | |
5566 | fail = TREE_READONLY (elt); | |
5567 | break; | |
5568 | } | |
5569 | } | |
5570 | if (fail) | |
5571 | { | |
5572 | if (!ctx->quiet) | |
5573 | modifying_const_object_error (t, const_object_being_modified); | |
5574 | *non_constant_p = true; | |
5575 | return t; | |
5576 | } | |
5577 | } | |
5578 | ||
e8b3c1bc | 5579 | if (!preeval) |
60813a46 | 5580 | { |
6c59b8a9 JM |
5581 | /* We're handling an INIT_EXPR of class type, so the value of the |
5582 | initializer can depend on the object it's initializing. */ | |
5583 | ||
60813a46 JM |
5584 | /* Create a new CONSTRUCTOR in case evaluation of the initializer |
5585 | wants to modify it. */ | |
acb2970c | 5586 | if (*valp == NULL_TREE) |
73a84269 | 5587 | { |
664beaf2 | 5588 | *valp = build_constructor (type, NULL); |
e8c48716 | 5589 | CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; |
73a84269 | 5590 | } |
664beaf2 | 5591 | new_ctx.ctor = *valp; |
60813a46 | 5592 | new_ctx.object = target; |
10d2f801 JM |
5593 | /* Avoid temporary materialization when initializing from a TARGET_EXPR. |
5594 | We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because | |
5595 | expansion of those trees uses ctx instead. */ | |
5596 | if (TREE_CODE (init) == TARGET_EXPR) | |
5597 | if (tree tinit = TARGET_EXPR_INITIAL (init)) | |
5598 | init = tinit; | |
e8b3c1bc JM |
5599 | init = cxx_eval_constant_expression (&new_ctx, init, false, |
5600 | non_constant_p, overflow_p); | |
37244b21 PP |
5601 | /* The hash table might have moved since the get earlier, and the |
5602 | initializer might have mutated the underlying CONSTRUCTORs, so we must | |
5603 | recompute VALP. */ | |
5604 | valp = ctx->global->values.get (object); | |
5605 | for (unsigned i = 0; i < vec_safe_length (indexes); i++) | |
5606 | { | |
5607 | constructor_elt *cep | |
5608 | = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]); | |
5609 | valp = &cep->value; | |
5610 | } | |
60813a46 JM |
5611 | } |
5612 | ||
7574c916 | 5613 | /* Don't share a CONSTRUCTOR that might be changed later. */ |
0146e25f | 5614 | init = unshare_constructor (init); |
d96e8407 | 5615 | |
e8b3c1bc JM |
5616 | if (*valp && TREE_CODE (*valp) == CONSTRUCTOR |
5617 | && TREE_CODE (init) == CONSTRUCTOR) | |
acb2970c | 5618 | { |
d96e8407 JM |
5619 | /* An outer ctx->ctor might be pointing to *valp, so replace |
5620 | its contents. */ | |
d0aa42d2 JM |
5621 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), |
5622 | TREE_TYPE (*valp))) | |
5623 | { | |
5624 | /* For initialization of an empty base, the original target will be | |
5625 | *(base*)this, evaluation of which resolves to the object | |
5626 | argument, which has the derived type rather than the base type. In | |
5627 | this situation, just evaluate the initializer and return, since | |
5628 | there's no actual data to store. */ | |
21fd62e5 PP |
5629 | gcc_assert (is_empty_class (TREE_TYPE (init))); |
5630 | return lval ? target : init; | |
d0aa42d2 | 5631 | } |
d96e8407 JM |
5632 | CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init); |
5633 | TREE_CONSTANT (*valp) = TREE_CONSTANT (init); | |
5634 | TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init); | |
e8c48716 JM |
5635 | CONSTRUCTOR_NO_CLEARING (*valp) |
5636 | = CONSTRUCTOR_NO_CLEARING (init); | |
acb2970c | 5637 | } |
35e8b38a JJ |
5638 | else if (TREE_CODE (init) == CONSTRUCTOR |
5639 | && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), | |
5640 | type)) | |
5641 | { | |
5642 | /* See above on initialization of empty bases. */ | |
5643 | gcc_assert (is_empty_class (TREE_TYPE (init)) && !lval); | |
5644 | return init; | |
5645 | } | |
60813a46 | 5646 | else |
d96e8407 | 5647 | *valp = init; |
2d63bc39 | 5648 | |
7eb5be6a MP |
5649 | /* After initialization, 'const' semantics apply to the value of the |
5650 | object. Make a note of this fact by marking the CONSTRUCTOR | |
5651 | TREE_READONLY. */ | |
5652 | if (TREE_CODE (t) == INIT_EXPR | |
5653 | && TREE_CODE (*valp) == CONSTRUCTOR | |
5654 | && TYPE_READONLY (type)) | |
64da1b76 PP |
5655 | { |
5656 | if (INDIRECT_REF_P (target) | |
5657 | && (is_this_parameter | |
5658 | (tree_strip_nop_conversions (TREE_OPERAND (target, 0))))) | |
5659 | /* We've just initialized '*this' (perhaps via the target | |
5660 | constructor of a delegating constructor). Leave it up to the | |
5661 | caller that set 'this' to set TREE_READONLY appropriately. */ | |
5662 | gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p | |
5663 | (TREE_TYPE (target), type)); | |
5664 | else | |
5665 | TREE_READONLY (*valp) = true; | |
5666 | } | |
7eb5be6a | 5667 | |
d96e8407 JM |
5668 | /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing |
5669 | CONSTRUCTORs, if any. */ | |
d96e8407 JM |
5670 | bool c = TREE_CONSTANT (init); |
5671 | bool s = TREE_SIDE_EFFECTS (init); | |
37244b21 | 5672 | if (!c || s || activated_union_member_p) |
3f207ab3 | 5673 | for (tree elt : *ctors) |
d96e8407 JM |
5674 | { |
5675 | if (!c) | |
5676 | TREE_CONSTANT (elt) = false; | |
5677 | if (s) | |
5678 | TREE_SIDE_EFFECTS (elt) = true; | |
b599bf9d PP |
5679 | /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of |
5680 | this union. */ | |
5681 | if (TREE_CODE (TREE_TYPE (elt)) == UNION_TYPE) | |
5682 | CONSTRUCTOR_NO_CLEARING (elt) = false; | |
d96e8407 | 5683 | } |
60813a46 JM |
5684 | |
5685 | if (*non_constant_p) | |
5686 | return t; | |
92a596e8 | 5687 | else if (lval) |
60813a46 JM |
5688 | return target; |
5689 | else | |
3bdf0a9b | 5690 | return init; |
60813a46 JM |
5691 | } |
5692 | ||
5693 | /* Evaluate a ++ or -- expression. */ | |
5694 | ||
5695 | static tree | |
5696 | cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t, | |
92a596e8 | 5697 | bool lval, |
60813a46 JM |
5698 | bool *non_constant_p, bool *overflow_p) |
5699 | { | |
5700 | enum tree_code code = TREE_CODE (t); | |
5701 | tree type = TREE_TYPE (t); | |
5702 | tree op = TREE_OPERAND (t, 0); | |
5703 | tree offset = TREE_OPERAND (t, 1); | |
5704 | gcc_assert (TREE_CONSTANT (offset)); | |
5705 | ||
d85569f6 MP |
5706 | /* OFFSET is constant, but perhaps not constant enough. We need to |
5707 | e.g. bash FLOAT_EXPRs to REAL_CSTs. */ | |
5708 | offset = fold_simple (offset); | |
5709 | ||
60813a46 | 5710 | /* The operand as an lvalue. */ |
2b3ab879 | 5711 | op = cxx_eval_constant_expression (ctx, op, true, |
5a804683 | 5712 | non_constant_p, overflow_p); |
60813a46 JM |
5713 | |
5714 | /* The operand as an rvalue. */ | |
84dd815f JM |
5715 | tree val |
5716 | = cxx_eval_constant_expression (ctx, op, false, | |
5717 | non_constant_p, overflow_p); | |
caee690e JM |
5718 | /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to |
5719 | a local array in a constexpr function. */ | |
71a93b08 | 5720 | bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val)); |
caee690e JM |
5721 | if (!ptr) |
5722 | VERIFY_CONSTANT (val); | |
60813a46 JM |
5723 | |
5724 | /* The modified value. */ | |
5725 | bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR); | |
ac2f8d26 | 5726 | tree mod; |
71a93b08 | 5727 | if (INDIRECT_TYPE_P (type)) |
ac2f8d26 JM |
5728 | { |
5729 | /* The middle end requires pointers to use POINTER_PLUS_EXPR. */ | |
5730 | offset = convert_to_ptrofftype (offset); | |
5731 | if (!inc) | |
5732 | offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset); | |
5733 | mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset); | |
5734 | } | |
5735 | else | |
5736 | mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset); | |
caee690e JM |
5737 | if (!ptr) |
5738 | VERIFY_CONSTANT (mod); | |
60813a46 JM |
5739 | |
5740 | /* Storing the modified value. */ | |
04e1749c MP |
5741 | tree store = build2_loc (cp_expr_loc_or_loc (t, input_location), |
5742 | MODIFY_EXPR, type, op, mod); | |
d1bba463 PP |
5743 | mod = cxx_eval_constant_expression (ctx, store, lval, |
5744 | non_constant_p, overflow_p); | |
ecdcd560 | 5745 | ggc_free (store); |
d1bba463 PP |
5746 | if (*non_constant_p) |
5747 | return t; | |
60813a46 JM |
5748 | |
5749 | /* And the value of the expression. */ | |
5750 | if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR) | |
d1bba463 PP |
5751 | /* Prefix ops are lvalues, but the caller might want an rvalue; |
5752 | lval has already been taken into account in the store above. */ | |
5753 | return mod; | |
60813a46 JM |
5754 | else |
5755 | /* Postfix ops are rvalues. */ | |
5756 | return val; | |
5757 | } | |
5758 | ||
56632b27 JM |
5759 | /* Predicates for the meaning of *jump_target. */ |
5760 | ||
5761 | static bool | |
5762 | returns (tree *jump_target) | |
5763 | { | |
5764 | return *jump_target | |
d49318d9 PC |
5765 | && (TREE_CODE (*jump_target) == RETURN_EXPR |
5766 | || (TREE_CODE (*jump_target) == LABEL_DECL | |
5767 | && LABEL_DECL_CDTOR (*jump_target))); | |
56632b27 JM |
5768 | } |
5769 | ||
5770 | static bool | |
5771 | breaks (tree *jump_target) | |
5772 | { | |
5773 | return *jump_target | |
06ec22b7 JM |
5774 | && ((TREE_CODE (*jump_target) == LABEL_DECL |
5775 | && LABEL_DECL_BREAK (*jump_target)) | |
3075affd | 5776 | || TREE_CODE (*jump_target) == BREAK_STMT |
06ec22b7 | 5777 | || TREE_CODE (*jump_target) == EXIT_EXPR); |
56632b27 JM |
5778 | } |
5779 | ||
5780 | static bool | |
5781 | continues (tree *jump_target) | |
5782 | { | |
5783 | return *jump_target | |
3075affd JM |
5784 | && ((TREE_CODE (*jump_target) == LABEL_DECL |
5785 | && LABEL_DECL_CONTINUE (*jump_target)) | |
5786 | || TREE_CODE (*jump_target) == CONTINUE_STMT); | |
5787 | ||
56632b27 JM |
5788 | } |
5789 | ||
5790 | static bool | |
5791 | switches (tree *jump_target) | |
5792 | { | |
5793 | return *jump_target | |
5794 | && TREE_CODE (*jump_target) == INTEGER_CST; | |
5795 | } | |
5796 | ||
5797 | /* Subroutine of cxx_eval_statement_list. Determine whether the statement | |
4b390698 JJ |
5798 | STMT matches *jump_target. If we're looking for a case label and we see |
5799 | the default label, note it in ctx->css_state. */ | |
56632b27 JM |
5800 | |
5801 | static bool | |
4b390698 | 5802 | label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt) |
56632b27 | 5803 | { |
56632b27 JM |
5804 | switch (TREE_CODE (*jump_target)) |
5805 | { | |
5806 | case LABEL_DECL: | |
5807 | if (TREE_CODE (stmt) == LABEL_EXPR | |
5808 | && LABEL_EXPR_LABEL (stmt) == *jump_target) | |
5809 | return true; | |
5810 | break; | |
5811 | ||
5812 | case INTEGER_CST: | |
5813 | if (TREE_CODE (stmt) == CASE_LABEL_EXPR) | |
5814 | { | |
4b390698 | 5815 | gcc_assert (ctx->css_state != NULL); |
56632b27 | 5816 | if (!CASE_LOW (stmt)) |
4b390698 JJ |
5817 | { |
5818 | /* default: should appear just once in a SWITCH_EXPR | |
5819 | body (excluding nested SWITCH_EXPR). */ | |
5820 | gcc_assert (*ctx->css_state != css_default_seen); | |
5821 | /* When evaluating SWITCH_EXPR body for the second time, | |
5822 | return true for the default: label. */ | |
5823 | if (*ctx->css_state == css_default_processing) | |
5824 | return true; | |
5825 | *ctx->css_state = css_default_seen; | |
5826 | } | |
385ed708 JJ |
5827 | else if (CASE_HIGH (stmt)) |
5828 | { | |
5829 | if (tree_int_cst_le (CASE_LOW (stmt), *jump_target) | |
5830 | && tree_int_cst_le (*jump_target, CASE_HIGH (stmt))) | |
5831 | return true; | |
5832 | } | |
56632b27 JM |
5833 | else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt))) |
5834 | return true; | |
5835 | } | |
5836 | break; | |
5837 | ||
43574e4f JJ |
5838 | case BREAK_STMT: |
5839 | case CONTINUE_STMT: | |
5840 | /* These two are handled directly in cxx_eval_loop_expr by testing | |
5841 | breaks (jump_target) or continues (jump_target). */ | |
5842 | break; | |
5843 | ||
56632b27 JM |
5844 | default: |
5845 | gcc_unreachable (); | |
5846 | } | |
5847 | return false; | |
5848 | } | |
5849 | ||
5850 | /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump | |
5851 | semantics, for switch, break, continue, and return. */ | |
5852 | ||
5853 | static tree | |
5854 | cxx_eval_statement_list (const constexpr_ctx *ctx, tree t, | |
56632b27 JM |
5855 | bool *non_constant_p, bool *overflow_p, |
5856 | tree *jump_target) | |
5857 | { | |
58611fb6 | 5858 | tree local_target; |
345edb37 JJ |
5859 | /* In a statement-expression we want to return the last value. |
5860 | For empty statement expression return void_node. */ | |
5861 | tree r = void_node; | |
58611fb6 JM |
5862 | if (!jump_target) |
5863 | { | |
5864 | local_target = NULL_TREE; | |
5865 | jump_target = &local_target; | |
5866 | } | |
0f54cc9c | 5867 | for (tree stmt : tsi_range (t)) |
56632b27 | 5868 | { |
0250ba58 MP |
5869 | /* We've found a continue, so skip everything until we reach |
5870 | the label its jumping to. */ | |
5871 | if (continues (jump_target)) | |
5872 | { | |
5873 | if (label_matches (ctx, jump_target, stmt)) | |
5874 | /* Found it. */ | |
5875 | *jump_target = NULL_TREE; | |
5876 | else | |
5877 | continue; | |
5878 | } | |
6ef72c36 JJ |
5879 | if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT) |
5880 | continue; | |
58611fb6 JM |
5881 | r = cxx_eval_constant_expression (ctx, stmt, false, |
5882 | non_constant_p, overflow_p, | |
5883 | jump_target); | |
56632b27 JM |
5884 | if (*non_constant_p) |
5885 | break; | |
5886 | if (returns (jump_target) || breaks (jump_target)) | |
5887 | break; | |
5888 | } | |
ce36ba09 JM |
5889 | if (*jump_target && jump_target == &local_target) |
5890 | { | |
5891 | /* We aren't communicating the jump to our caller, so give up. We don't | |
5892 | need to support evaluation of jumps out of statement-exprs. */ | |
5893 | if (!ctx->quiet) | |
f9d0ca40 | 5894 | error_at (cp_expr_loc_or_input_loc (r), |
ce36ba09 JM |
5895 | "statement is not a constant expression"); |
5896 | *non_constant_p = true; | |
5897 | } | |
58611fb6 | 5898 | return r; |
56632b27 JM |
5899 | } |
5900 | ||
5901 | /* Evaluate a LOOP_EXPR for side-effects. Handles break and return | |
5902 | semantics; continue semantics are covered by cxx_eval_statement_list. */ | |
5903 | ||
5904 | static tree | |
5905 | cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t, | |
56632b27 JM |
5906 | bool *non_constant_p, bool *overflow_p, |
5907 | tree *jump_target) | |
5908 | { | |
39dce2b7 | 5909 | constexpr_ctx new_ctx = *ctx; |
8e007055 JJ |
5910 | tree local_target; |
5911 | if (!jump_target) | |
5912 | { | |
5913 | local_target = NULL_TREE; | |
5914 | jump_target = &local_target; | |
5915 | } | |
39dce2b7 | 5916 | |
43574e4f | 5917 | tree body, cond = NULL_TREE, expr = NULL_TREE; |
5ec2cd9f | 5918 | int count = 0; |
43574e4f JJ |
5919 | switch (TREE_CODE (t)) |
5920 | { | |
5921 | case LOOP_EXPR: | |
5922 | body = LOOP_EXPR_BODY (t); | |
5923 | break; | |
5924 | case DO_STMT: | |
5925 | body = DO_BODY (t); | |
5926 | cond = DO_COND (t); | |
5927 | break; | |
5928 | case WHILE_STMT: | |
5929 | body = WHILE_BODY (t); | |
5930 | cond = WHILE_COND (t); | |
5931 | count = -1; | |
5932 | break; | |
5933 | case FOR_STMT: | |
5934 | if (FOR_INIT_STMT (t)) | |
5935 | cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), /*lval*/false, | |
5936 | non_constant_p, overflow_p, jump_target); | |
5937 | if (*non_constant_p) | |
5938 | return NULL_TREE; | |
5939 | body = FOR_BODY (t); | |
5940 | cond = FOR_COND (t); | |
5941 | expr = FOR_EXPR (t); | |
5942 | count = -1; | |
5943 | break; | |
5944 | default: | |
5945 | gcc_unreachable (); | |
5946 | } | |
0ee28590 | 5947 | auto_vec<tree, 10> save_exprs; |
43574e4f | 5948 | new_ctx.save_exprs = &save_exprs; |
d259b234 | 5949 | do |
56632b27 | 5950 | { |
43574e4f JJ |
5951 | if (count != -1) |
5952 | { | |
5953 | if (body) | |
5954 | cxx_eval_constant_expression (&new_ctx, body, /*lval*/false, | |
5955 | non_constant_p, overflow_p, | |
5956 | jump_target); | |
5957 | if (breaks (jump_target)) | |
5958 | { | |
5959 | *jump_target = NULL_TREE; | |
5960 | break; | |
5961 | } | |
39dce2b7 | 5962 | |
43574e4f JJ |
5963 | if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target)) |
5964 | *jump_target = NULL_TREE; | |
5965 | ||
5966 | if (expr) | |
5967 | cxx_eval_constant_expression (&new_ctx, expr, /*lval*/false, | |
5968 | non_constant_p, overflow_p, | |
5969 | jump_target); | |
5970 | } | |
5971 | ||
5972 | if (cond) | |
5973 | { | |
5974 | tree res | |
5975 | = cxx_eval_constant_expression (&new_ctx, cond, /*lval*/false, | |
5976 | non_constant_p, overflow_p, | |
5977 | jump_target); | |
5978 | if (res) | |
5979 | { | |
5980 | if (verify_constant (res, ctx->quiet, non_constant_p, | |
5981 | overflow_p)) | |
5982 | break; | |
5983 | if (integer_zerop (res)) | |
5984 | break; | |
5985 | } | |
5986 | else | |
5987 | gcc_assert (*jump_target); | |
5988 | } | |
39dce2b7 | 5989 | |
ee1de08d | 5990 | /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */ |
3f207ab3 | 5991 | for (tree save_expr : save_exprs) |
8e007055 | 5992 | ctx->global->values.remove (save_expr); |
0ee28590 | 5993 | save_exprs.truncate (0); |
43574e4f | 5994 | |
5ec2cd9f JM |
5995 | if (++count >= constexpr_loop_limit) |
5996 | { | |
5997 | if (!ctx->quiet) | |
f9d0ca40 | 5998 | error_at (cp_expr_loc_or_input_loc (t), |
84fa214d | 5999 | "%<constexpr%> loop iteration count exceeds limit of %d " |
a3f9f006 | 6000 | "(use %<-fconstexpr-loop-limit=%> to increase the limit)", |
5ec2cd9f JM |
6001 | constexpr_loop_limit); |
6002 | *non_constant_p = true; | |
6003 | break; | |
6004 | } | |
56632b27 | 6005 | } |
4b390698 JJ |
6006 | while (!returns (jump_target) |
6007 | && !breaks (jump_target) | |
43574e4f JJ |
6008 | && !continues (jump_target) |
6009 | && (!switches (jump_target) || count == 0) | |
4b390698 | 6010 | && !*non_constant_p); |
d259b234 | 6011 | |
ee1de08d | 6012 | /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */ |
3f207ab3 | 6013 | for (tree save_expr : save_exprs) |
8e007055 | 6014 | ctx->global->values.remove (save_expr); |
39dce2b7 | 6015 | |
56632b27 JM |
6016 | return NULL_TREE; |
6017 | } | |
6018 | ||
6019 | /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump | |
6020 | semantics. */ | |
6021 | ||
6022 | static tree | |
6023 | cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t, | |
56632b27 JM |
6024 | bool *non_constant_p, bool *overflow_p, |
6025 | tree *jump_target) | |
6026 | { | |
43574e4f JJ |
6027 | tree cond |
6028 | = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t); | |
2b3ab879 | 6029 | cond = cxx_eval_constant_expression (ctx, cond, false, |
5a804683 | 6030 | non_constant_p, overflow_p); |
56632b27 JM |
6031 | VERIFY_CONSTANT (cond); |
6032 | *jump_target = cond; | |
6033 | ||
43574e4f JJ |
6034 | tree body |
6035 | = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t); | |
4b390698 JJ |
6036 | constexpr_ctx new_ctx = *ctx; |
6037 | constexpr_switch_state css = css_default_not_seen; | |
6038 | new_ctx.css_state = &css; | |
6039 | cxx_eval_constant_expression (&new_ctx, body, false, | |
6040 | non_constant_p, overflow_p, jump_target); | |
6041 | if (switches (jump_target) && css == css_default_seen) | |
6042 | { | |
6043 | /* If the SWITCH_EXPR body has default: label, process it once again, | |
6044 | this time instructing label_matches to return true for default: | |
6045 | label on switches (jump_target). */ | |
6046 | css = css_default_processing; | |
6047 | cxx_eval_constant_expression (&new_ctx, body, false, | |
6048 | non_constant_p, overflow_p, jump_target); | |
6049 | } | |
56632b27 JM |
6050 | if (breaks (jump_target) || switches (jump_target)) |
6051 | *jump_target = NULL_TREE; | |
6052 | return NULL_TREE; | |
6053 | } | |
6054 | ||
89262ec6 JM |
6055 | /* Find the object of TYPE under initialization in CTX. */ |
6056 | ||
6057 | static tree | |
6058 | lookup_placeholder (const constexpr_ctx *ctx, bool lval, tree type) | |
6059 | { | |
fbd603c4 | 6060 | if (!ctx) |
89262ec6 JM |
6061 | return NULL_TREE; |
6062 | ||
49a86fce PP |
6063 | /* Prefer the outermost matching object, but don't cross |
6064 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */ | |
6065 | if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor)) | |
6066 | if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type)) | |
6067 | return outer_ob; | |
6068 | ||
89262ec6 JM |
6069 | /* We could use ctx->object unconditionally, but using ctx->ctor when we |
6070 | can is a minor optimization. */ | |
fbd603c4 | 6071 | if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type)) |
89262ec6 JM |
6072 | return ctx->ctor; |
6073 | ||
fbd603c4 JM |
6074 | if (!ctx->object) |
6075 | return NULL_TREE; | |
6076 | ||
89262ec6 JM |
6077 | /* Since an object cannot have a field of its own type, we can search outward |
6078 | from ctx->object to find the unique containing object of TYPE. */ | |
6079 | tree ob = ctx->object; | |
6080 | while (ob) | |
6081 | { | |
6082 | if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type)) | |
6083 | break; | |
6084 | if (handled_component_p (ob)) | |
6085 | ob = TREE_OPERAND (ob, 0); | |
6086 | else | |
6087 | ob = NULL_TREE; | |
6088 | } | |
6089 | ||
6090 | return ob; | |
6091 | } | |
6092 | ||
7c814975 MP |
6093 | /* Complain about an attempt to evaluate inline assembly. */ |
6094 | ||
6095 | static void | |
6096 | inline_asm_in_constexpr_error (location_t loc) | |
6097 | { | |
6098 | auto_diagnostic_group d; | |
6099 | error_at (loc, "inline assembly is not a constant expression"); | |
6100 | inform (loc, "only unevaluated inline assembly is allowed in a " | |
b04445d4 | 6101 | "%<constexpr%> function in C++20"); |
7c814975 MP |
6102 | } |
6103 | ||
76b75018 JM |
6104 | /* We're getting the constant value of DECL in a manifestly constant-evaluated |
6105 | context; maybe complain about that. */ | |
6106 | ||
6107 | static void | |
6108 | maybe_warn_about_constant_value (location_t loc, tree decl) | |
6109 | { | |
6110 | static bool explained = false; | |
6111 | if (cxx_dialect >= cxx17 | |
6112 | && warn_interference_size | |
00f34291 | 6113 | && !OPTION_SET_P (param_destruct_interfere_size) |
76b75018 JM |
6114 | && DECL_CONTEXT (decl) == std_node |
6115 | && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size") | |
6116 | && (LOCATION_FILE (input_location) != main_input_filename | |
6117 | || module_exporting_p ()) | |
6118 | && warning_at (loc, OPT_Winterference_size, "use of %qD", decl) | |
6119 | && !explained) | |
6120 | { | |
6121 | explained = true; | |
6122 | inform (loc, "its value can vary between compiler versions or " | |
6123 | "with different %<-mtune%> or %<-mcpu%> flags"); | |
6124 | inform (loc, "if this use is part of a public ABI, change it to " | |
6125 | "instead use a constant variable you define"); | |
6126 | inform (loc, "the default value for the current CPU tuning " | |
6127 | "is %d bytes", param_destruct_interfere_size); | |
6128 | inform (loc, "you can stabilize this value with %<--param " | |
6129 | "hardware_destructive_interference_size=%d%>, or disable " | |
6130 | "this warning with %<-Wno-interference-size%>", | |
6131 | param_destruct_interfere_size); | |
6132 | } | |
6133 | } | |
6134 | ||
2d76680f PC |
6135 | /* Attempt to reduce the expression T to a constant value. |
6136 | On failure, issue diagnostic and return error_mark_node. */ | |
6137 | /* FIXME unify with c_fully_fold */ | |
60813a46 | 6138 | /* FIXME overflow_p is too global */ |
2d76680f PC |
6139 | |
6140 | static tree | |
3e605b20 | 6141 | cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, |
92a596e8 | 6142 | bool lval, |
56632b27 | 6143 | bool *non_constant_p, bool *overflow_p, |
3075affd | 6144 | tree *jump_target /* = NULL */) |
2d76680f | 6145 | { |
4b390698 JJ |
6146 | if (jump_target && *jump_target) |
6147 | { | |
6148 | /* If we are jumping, ignore all statements/expressions except those | |
6149 | that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */ | |
6150 | switch (TREE_CODE (t)) | |
6151 | { | |
6152 | case BIND_EXPR: | |
6153 | case STATEMENT_LIST: | |
6154 | case LOOP_EXPR: | |
6155 | case COND_EXPR: | |
43574e4f JJ |
6156 | case IF_STMT: |
6157 | case DO_STMT: | |
6158 | case WHILE_STMT: | |
6159 | case FOR_STMT: | |
4b390698 JJ |
6160 | break; |
6161 | case LABEL_EXPR: | |
6162 | case CASE_LABEL_EXPR: | |
6163 | if (label_matches (ctx, jump_target, t)) | |
6164 | /* Found it. */ | |
6165 | *jump_target = NULL_TREE; | |
6166 | return NULL_TREE; | |
6167 | default: | |
6168 | return NULL_TREE; | |
6169 | } | |
6170 | } | |
3075affd | 6171 | if (error_operand_p (t)) |
2d76680f PC |
6172 | { |
6173 | *non_constant_p = true; | |
6174 | return t; | |
6175 | } | |
7a649ef5 | 6176 | |
6821245b JM |
6177 | location_t loc = cp_expr_loc_or_input_loc (t); |
6178 | ||
7a649ef5 JM |
6179 | STRIP_ANY_LOCATION_WRAPPER (t); |
6180 | ||
2d76680f PC |
6181 | if (CONSTANT_CLASS_P (t)) |
6182 | { | |
61637db3 JJ |
6183 | if (TREE_OVERFLOW (t)) |
6184 | { | |
6185 | if (!ctx->quiet) | |
6186 | permerror (input_location, "overflow in constant expression"); | |
6187 | if (!flag_permissive || ctx->quiet) | |
6188 | *overflow_p = true; | |
6189 | } | |
8bada5cd MS |
6190 | |
6191 | if (TREE_CODE (t) == INTEGER_CST | |
9f613f06 | 6192 | && TYPE_PTR_P (TREE_TYPE (t)) |
f45610a4 JJ |
6193 | /* INTEGER_CST with pointer-to-method type is only used |
6194 | for a virtual method in a pointer to member function. | |
6195 | Don't reject those. */ | |
6196 | && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE | |
8bada5cd MS |
6197 | && !integer_zerop (t)) |
6198 | { | |
6199 | if (!ctx->quiet) | |
6200 | error ("value %qE of type %qT is not a constant expression", | |
6201 | t, TREE_TYPE (t)); | |
6202 | *non_constant_p = true; | |
6203 | } | |
6204 | ||
2d76680f PC |
6205 | return t; |
6206 | } | |
2d76680f | 6207 | |
a15ffa22 | 6208 | /* Avoid excessively long constexpr evaluations. */ |
8e007055 | 6209 | if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit) |
a15ffa22 JJ |
6210 | { |
6211 | if (!ctx->quiet) | |
6821245b | 6212 | error_at (loc, |
a15ffa22 | 6213 | "%<constexpr%> evaluation operation count exceeds limit of " |
a9c697b8 | 6214 | "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)", |
a15ffa22 | 6215 | constexpr_ops_limit); |
8e007055 | 6216 | ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT); |
a15ffa22 JJ |
6217 | *non_constant_p = true; |
6218 | return t; | |
6219 | } | |
6220 | ||
7a649ef5 JM |
6221 | constexpr_ctx new_ctx; |
6222 | tree r = t; | |
6223 | ||
8bada5cd MS |
6224 | tree_code tcode = TREE_CODE (t); |
6225 | switch (tcode) | |
2d76680f | 6226 | { |
60813a46 | 6227 | case RESULT_DECL: |
92a596e8 | 6228 | if (lval) |
60813a46 JM |
6229 | return t; |
6230 | /* We ask for an rvalue for the RESULT_DECL when indirecting | |
bf66b9b4 AH |
6231 | through an invisible reference, or in named return value |
6232 | optimization. */ | |
8e007055 | 6233 | if (tree *p = ctx->global->values.get (t)) |
1efb1dc2 MP |
6234 | return *p; |
6235 | else | |
6236 | { | |
6237 | if (!ctx->quiet) | |
6238 | error ("%qE is not a constant expression", t); | |
6239 | *non_constant_p = true; | |
6240 | } | |
6241 | break; | |
60813a46 | 6242 | |
2d76680f | 6243 | case VAR_DECL: |
70f40fea | 6244 | if (DECL_HAS_VALUE_EXPR_P (t)) |
c59fa7ea JM |
6245 | { |
6246 | if (is_normal_capture_proxy (t) | |
6247 | && current_function_decl == DECL_CONTEXT (t)) | |
6248 | { | |
6249 | /* Function parms aren't constexpr within the function | |
6250 | definition, so don't try to look at the closure. But if the | |
6251 | captured variable is constant, try to evaluate it directly. */ | |
6252 | r = DECL_CAPTURED_VARIABLE (t); | |
6253 | tree type = TREE_TYPE (t); | |
6254 | if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r))) | |
6255 | { | |
6256 | /* Adjust r to match the reference-ness of t. */ | |
6257 | if (TYPE_REF_P (type)) | |
6258 | r = build_address (r); | |
6259 | else | |
6260 | r = convert_from_reference (r); | |
6261 | } | |
6262 | } | |
6263 | else | |
6264 | r = DECL_VALUE_EXPR (t); | |
6265 | return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, | |
6266 | overflow_p); | |
6267 | } | |
191816a3 | 6268 | /* fall through */ |
7c83622c JM |
6269 | case CONST_DECL: |
6270 | /* We used to not check lval for CONST_DECL, but darwin.c uses | |
6271 | CONST_DECL for aggregate constants. */ | |
92a596e8 | 6272 | if (lval) |
2d76680f | 6273 | return t; |
ef529765 JM |
6274 | else if (t == ctx->object) |
6275 | return ctx->ctor; | |
6276 | if (VAR_P (t)) | |
6277 | if (tree *p = ctx->global->values.get (t)) | |
6278 | if (*p != NULL_TREE) | |
6279 | { | |
6280 | r = *p; | |
6281 | break; | |
6282 | } | |
76b75018 JM |
6283 | if (ctx->manifestly_const_eval) |
6284 | maybe_warn_about_constant_value (loc, t); | |
f4fce183 | 6285 | if (COMPLETE_TYPE_P (TREE_TYPE (t)) |
dbcd32f8 | 6286 | && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
7dc2b4a2 JM |
6287 | { |
6288 | /* If the class is empty, we aren't actually loading anything. */ | |
6289 | r = build_constructor (TREE_TYPE (t), NULL); | |
6290 | TREE_CONSTANT (r) = true; | |
6291 | } | |
6292 | else if (ctx->strict) | |
8c00059c | 6293 | r = decl_really_constant_value (t, /*unshare_p=*/false); |
69eb4fde | 6294 | else |
8c00059c | 6295 | r = decl_constant_value (t, /*unshare_p=*/false); |
2d76680f PC |
6296 | if (TREE_CODE (r) == TARGET_EXPR |
6297 | && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR) | |
6298 | r = TARGET_EXPR_INITIAL (r); | |
2d76680f PC |
6299 | if (DECL_P (r)) |
6300 | { | |
2b3ab879 | 6301 | if (!ctx->quiet) |
6821245b | 6302 | non_const_var_error (loc, r); |
2d76680f PC |
6303 | *non_constant_p = true; |
6304 | } | |
6305 | break; | |
6306 | ||
96a95ac1 AO |
6307 | case DEBUG_BEGIN_STMT: |
6308 | /* ??? It might be nice to retain this information somehow, so | |
6309 | as to be able to step into a constexpr function call. */ | |
6310 | /* Fall through. */ | |
6311 | ||
2d76680f PC |
6312 | case FUNCTION_DECL: |
6313 | case TEMPLATE_DECL: | |
6314 | case LABEL_DECL: | |
56632b27 JM |
6315 | case LABEL_EXPR: |
6316 | case CASE_LABEL_EXPR: | |
4b390698 | 6317 | case PREDICT_EXPR: |
2d76680f PC |
6318 | return t; |
6319 | ||
6320 | case PARM_DECL: | |
9f613f06 | 6321 | if (lval && !TYPE_REF_P (TREE_TYPE (t))) |
60813a46 | 6322 | /* glvalue use. */; |
8e007055 | 6323 | else if (tree *p = ctx->global->values.get (r)) |
60813a46 | 6324 | r = *p; |
92a596e8 | 6325 | else if (lval) |
2d76680f | 6326 | /* Defer in case this is only used for its type. */; |
1c2f613f | 6327 | else if (COMPLETE_TYPE_P (TREE_TYPE (t)) |
dbcd32f8 | 6328 | && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
052beba4 JM |
6329 | { |
6330 | /* If the class is empty, we aren't actually loading anything. */ | |
6331 | r = build_constructor (TREE_TYPE (t), NULL); | |
6332 | TREE_CONSTANT (r) = true; | |
6333 | } | |
2d76680f PC |
6334 | else |
6335 | { | |
2b3ab879 | 6336 | if (!ctx->quiet) |
2d76680f PC |
6337 | error ("%qE is not a constant expression", t); |
6338 | *non_constant_p = true; | |
6339 | } | |
6340 | break; | |
6341 | ||
6342 | case CALL_EXPR: | |
6343 | case AGGR_INIT_EXPR: | |
92a596e8 | 6344 | r = cxx_eval_call_expression (ctx, t, lval, |
2d76680f PC |
6345 | non_constant_p, overflow_p); |
6346 | break; | |
6347 | ||
60813a46 JM |
6348 | case DECL_EXPR: |
6349 | { | |
6350 | r = DECL_EXPR_DECL (t); | |
43574e4f JJ |
6351 | if (TREE_CODE (r) == USING_DECL) |
6352 | { | |
6353 | r = void_node; | |
6354 | break; | |
6355 | } | |
8892d532 JJ |
6356 | |
6357 | if (VAR_P (r) | |
6358 | && (TREE_STATIC (r) || CP_DECL_THREAD_LOCAL_P (r)) | |
6359 | /* Allow __FUNCTION__ etc. */ | |
6360 | && !DECL_ARTIFICIAL (r)) | |
6361 | { | |
6362 | gcc_assert (cxx_dialect >= cxx23); | |
6363 | if (!ctx->quiet) | |
6364 | { | |
6365 | if (CP_DECL_THREAD_LOCAL_P (r)) | |
6366 | error_at (loc, "control passes through declaration of %qD " | |
6367 | "with thread storage duration", r); | |
6368 | else | |
6369 | error_at (loc, "control passes through declaration of %qD " | |
6370 | "with static storage duration", r); | |
6371 | } | |
6372 | *non_constant_p = true; | |
6373 | break; | |
6374 | } | |
6375 | ||
60813a46 JM |
6376 | if (AGGREGATE_TYPE_P (TREE_TYPE (r)) |
6377 | || VECTOR_TYPE_P (TREE_TYPE (r))) | |
6378 | { | |
6379 | new_ctx = *ctx; | |
6380 | new_ctx.object = r; | |
6381 | new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL); | |
e8c48716 | 6382 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; |
8e007055 | 6383 | ctx->global->values.put (r, new_ctx.ctor); |
60813a46 JM |
6384 | ctx = &new_ctx; |
6385 | } | |
6386 | ||
6387 | if (tree init = DECL_INITIAL (r)) | |
6388 | { | |
6389 | init = cxx_eval_constant_expression (ctx, init, | |
2b3ab879 | 6390 | false, |
5a804683 | 6391 | non_constant_p, overflow_p); |
7f0e23e9 | 6392 | /* Don't share a CONSTRUCTOR that might be changed. */ |
0146e25f | 6393 | init = unshare_constructor (init); |
04e1749c MP |
6394 | /* Remember that a constant object's constructor has already |
6395 | run. */ | |
6396 | if (CLASS_TYPE_P (TREE_TYPE (r)) | |
6397 | && CP_TYPE_CONST_P (TREE_TYPE (r))) | |
6398 | TREE_READONLY (init) = true; | |
8e007055 | 6399 | ctx->global->values.put (r, init); |
60813a46 JM |
6400 | } |
6401 | else if (ctx == &new_ctx) | |
6402 | /* We gave it a CONSTRUCTOR above. */; | |
6403 | else | |
8e007055 | 6404 | ctx->global->values.put (r, NULL_TREE); |
60813a46 JM |
6405 | } |
6406 | break; | |
6407 | ||
2d76680f | 6408 | case TARGET_EXPR: |
595f1b12 JM |
6409 | { |
6410 | tree type = TREE_TYPE (t); | |
6411 | ||
6412 | if (!literal_type_p (type)) | |
6413 | { | |
6414 | if (!ctx->quiet) | |
6415 | { | |
6416 | auto_diagnostic_group d; | |
6417 | error ("temporary of non-literal type %qT in a " | |
6418 | "constant expression", type); | |
6419 | explain_non_literal_class (type); | |
6420 | } | |
6421 | *non_constant_p = true; | |
6422 | break; | |
6423 | } | |
6424 | gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t)); | |
6425 | /* Avoid evaluating a TARGET_EXPR more than once. */ | |
6426 | tree slot = TARGET_EXPR_SLOT (t); | |
6427 | if (tree *p = ctx->global->values.get (slot)) | |
6428 | { | |
6429 | if (lval) | |
6430 | return slot; | |
6431 | r = *p; | |
6432 | break; | |
6433 | } | |
595f1b12 JM |
6434 | if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))) |
6435 | { | |
6436 | /* We're being expanded without an explicit target, so start | |
6437 | initializing a new object; expansion with an explicit target | |
6438 | strips the TARGET_EXPR before we get here. */ | |
6439 | new_ctx = *ctx; | |
49a86fce PP |
6440 | /* Link CTX to NEW_CTX so that lookup_placeholder can resolve |
6441 | any PLACEHOLDER_EXPR within the initializer that refers to the | |
6442 | former object under construction. */ | |
6443 | new_ctx.parent = ctx; | |
595f1b12 JM |
6444 | new_ctx.ctor = build_constructor (type, NULL); |
6445 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; | |
6446 | new_ctx.object = slot; | |
6447 | ctx->global->values.put (new_ctx.object, new_ctx.ctor); | |
6448 | ctx = &new_ctx; | |
6449 | } | |
6450 | /* Pass false for 'lval' because this indicates | |
6451 | initialization of a temporary. */ | |
6452 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), | |
6453 | false, | |
6454 | non_constant_p, overflow_p); | |
6455 | if (*non_constant_p) | |
ee1de08d | 6456 | break; |
595f1b12 JM |
6457 | /* Adjust the type of the result to the type of the temporary. */ |
6458 | r = adjust_temp_type (type, r); | |
6459 | if (TARGET_EXPR_CLEANUP (t) && !CLEANUP_EH_ONLY (t)) | |
6460 | ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t)); | |
6461 | r = unshare_constructor (r); | |
6462 | ctx->global->values.put (slot, r); | |
6463 | if (ctx->save_exprs) | |
6464 | ctx->save_exprs->safe_push (slot); | |
6465 | if (lval) | |
6466 | return slot; | |
6467 | } | |
2d76680f PC |
6468 | break; |
6469 | ||
60813a46 | 6470 | case INIT_EXPR: |
60813a46 | 6471 | case MODIFY_EXPR: |
4b390698 | 6472 | gcc_assert (jump_target == NULL || *jump_target == NULL_TREE); |
92a596e8 | 6473 | r = cxx_eval_store_expression (ctx, t, lval, |
60813a46 JM |
6474 | non_constant_p, overflow_p); |
6475 | break; | |
6476 | ||
2d76680f | 6477 | case SCOPE_REF: |
3e605b20 | 6478 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
92a596e8 | 6479 | lval, |
5a804683 | 6480 | non_constant_p, overflow_p); |
2d76680f PC |
6481 | break; |
6482 | ||
6483 | case RETURN_EXPR: | |
75e0295b MP |
6484 | if (TREE_OPERAND (t, 0) != NULL_TREE) |
6485 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), | |
6486 | lval, | |
6487 | non_constant_p, overflow_p); | |
43574e4f JJ |
6488 | /* FALLTHRU */ |
6489 | case BREAK_STMT: | |
6490 | case CONTINUE_STMT: | |
019e0ae8 MP |
6491 | if (jump_target) |
6492 | *jump_target = t; | |
6493 | else | |
6494 | { | |
6495 | /* Can happen with ({ return true; }) && false; passed to | |
6496 | maybe_constant_value. There is nothing to jump over in this | |
6497 | case, and the bug will be diagnosed later. */ | |
6498 | gcc_assert (ctx->quiet); | |
6499 | *non_constant_p = true; | |
6500 | } | |
56632b27 JM |
6501 | break; |
6502 | ||
cabaf50a JM |
6503 | case SAVE_EXPR: |
6504 | /* Avoid evaluating a SAVE_EXPR more than once. */ | |
8e007055 | 6505 | if (tree *p = ctx->global->values.get (t)) |
cabaf50a JM |
6506 | r = *p; |
6507 | else | |
6508 | { | |
12d9ce19 | 6509 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false, |
cabaf50a | 6510 | non_constant_p, overflow_p); |
0e0ffbfc JJ |
6511 | if (*non_constant_p) |
6512 | break; | |
8e007055 | 6513 | ctx->global->values.put (t, r); |
39dce2b7 | 6514 | if (ctx->save_exprs) |
0ee28590 | 6515 | ctx->save_exprs->safe_push (t); |
cabaf50a JM |
6516 | } |
6517 | break; | |
6518 | ||
2d76680f | 6519 | case TRY_CATCH_EXPR: |
ee1de08d JJ |
6520 | if (TREE_OPERAND (t, 0) == NULL_TREE) |
6521 | { | |
6522 | r = void_node; | |
6523 | break; | |
6524 | } | |
6525 | /* FALLTHRU */ | |
6526 | case NON_LVALUE_EXPR: | |
769430b2 | 6527 | case TRY_BLOCK: |
2d76680f | 6528 | case MUST_NOT_THROW_EXPR: |
60813a46 JM |
6529 | case EXPR_STMT: |
6530 | case EH_SPEC_BLOCK: | |
3e605b20 | 6531 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
92a596e8 | 6532 | lval, |
56632b27 JM |
6533 | non_constant_p, overflow_p, |
6534 | jump_target); | |
2d76680f PC |
6535 | break; |
6536 | ||
ee1de08d JJ |
6537 | case CLEANUP_POINT_EXPR: |
6538 | { | |
6539 | auto_vec<tree, 2> cleanups; | |
6540 | vec<tree> *prev_cleanups = ctx->global->cleanups; | |
6541 | ctx->global->cleanups = &cleanups; | |
6542 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), | |
6543 | lval, | |
6544 | non_constant_p, overflow_p, | |
6545 | jump_target); | |
6546 | ctx->global->cleanups = prev_cleanups; | |
6547 | unsigned int i; | |
6548 | tree cleanup; | |
6549 | /* Evaluate the cleanups. */ | |
6550 | FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup) | |
6551 | cxx_eval_constant_expression (ctx, cleanup, false, | |
fc531c2e | 6552 | non_constant_p, overflow_p); |
ee1de08d JJ |
6553 | } |
6554 | break; | |
6555 | ||
769430b2 JM |
6556 | case TRY_FINALLY_EXPR: |
6557 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, | |
6558 | non_constant_p, overflow_p, | |
6559 | jump_target); | |
6560 | if (!*non_constant_p) | |
6561 | /* Also evaluate the cleanup. */ | |
6562 | cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true, | |
fc531c2e | 6563 | non_constant_p, overflow_p); |
769430b2 JM |
6564 | break; |
6565 | ||
43574e4f | 6566 | case CLEANUP_STMT: |
fc531c2e JJ |
6567 | r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval, |
6568 | non_constant_p, overflow_p, | |
6569 | jump_target); | |
6570 | if (!CLEANUP_EH_ONLY (t) && !*non_constant_p) | |
6571 | { | |
6572 | iloc_sentinel ils (loc); | |
6573 | /* Also evaluate the cleanup. */ | |
6574 | cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), true, | |
6575 | non_constant_p, overflow_p); | |
6576 | } | |
43574e4f JJ |
6577 | break; |
6578 | ||
2d76680f PC |
6579 | /* These differ from cxx_eval_unary_expression in that this doesn't |
6580 | check for a constant operand or result; an address can be | |
6581 | constant without its operand being, and vice versa. */ | |
cda0a029 | 6582 | case MEM_REF: |
2d76680f | 6583 | case INDIRECT_REF: |
92a596e8 | 6584 | r = cxx_eval_indirect_ref (ctx, t, lval, |
2d76680f PC |
6585 | non_constant_p, overflow_p); |
6586 | break; | |
6587 | ||
6588 | case ADDR_EXPR: | |
6589 | { | |
6590 | tree oldop = TREE_OPERAND (t, 0); | |
3e605b20 | 6591 | tree op = cxx_eval_constant_expression (ctx, oldop, |
92a596e8 | 6592 | /*lval*/true, |
5a804683 | 6593 | non_constant_p, overflow_p); |
2d76680f PC |
6594 | /* Don't VERIFY_CONSTANT here. */ |
6595 | if (*non_constant_p) | |
6596 | return t; | |
f2acb8ad | 6597 | gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR); |
2d76680f PC |
6598 | /* This function does more aggressive folding than fold itself. */ |
6599 | r = build_fold_addr_expr_with_type (op, TREE_TYPE (t)); | |
6600 | if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop) | |
7a649ef5 JM |
6601 | { |
6602 | ggc_free (r); | |
6603 | return t; | |
6604 | } | |
2d76680f PC |
6605 | break; |
6606 | } | |
6607 | ||
6608 | case REALPART_EXPR: | |
6609 | case IMAGPART_EXPR: | |
87713c6a JJ |
6610 | if (lval) |
6611 | { | |
6612 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, | |
6613 | non_constant_p, overflow_p); | |
6614 | if (r == error_mark_node) | |
6615 | ; | |
6616 | else if (r == TREE_OPERAND (t, 0)) | |
6617 | r = t; | |
6618 | else | |
6619 | r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r); | |
6620 | break; | |
6621 | } | |
6622 | /* FALLTHRU */ | |
2d76680f PC |
6623 | case CONJ_EXPR: |
6624 | case FIX_TRUNC_EXPR: | |
6625 | case FLOAT_EXPR: | |
6626 | case NEGATE_EXPR: | |
6627 | case ABS_EXPR: | |
e28fadbc | 6628 | case ABSU_EXPR: |
2d76680f PC |
6629 | case BIT_NOT_EXPR: |
6630 | case TRUTH_NOT_EXPR: | |
6631 | case FIXED_CONVERT_EXPR: | |
92a596e8 | 6632 | r = cxx_eval_unary_expression (ctx, t, lval, |
2d76680f PC |
6633 | non_constant_p, overflow_p); |
6634 | break; | |
6635 | ||
6636 | case SIZEOF_EXPR: | |
99e764a2 MP |
6637 | r = fold_sizeof_expr (t); |
6638 | /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR, | |
6639 | which could lead to an infinite recursion. */ | |
6640 | if (TREE_CODE (r) != SIZEOF_EXPR) | |
6641 | r = cxx_eval_constant_expression (ctx, r, lval, | |
6642 | non_constant_p, overflow_p, | |
6643 | jump_target); | |
6644 | else | |
6645 | { | |
6646 | *non_constant_p = true; | |
6647 | gcc_assert (ctx->quiet); | |
6648 | } | |
6649 | ||
2d76680f PC |
6650 | break; |
6651 | ||
6652 | case COMPOUND_EXPR: | |
6653 | { | |
6654 | /* check_return_expr sometimes wraps a TARGET_EXPR in a | |
6655 | COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR | |
6656 | introduced by build_call_a. */ | |
6657 | tree op0 = TREE_OPERAND (t, 0); | |
6658 | tree op1 = TREE_OPERAND (t, 1); | |
6659 | STRIP_NOPS (op1); | |
6660 | if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) | |
6661 | || TREE_CODE (op1) == EMPTY_CLASS_EXPR) | |
2b3ab879 | 6662 | r = cxx_eval_constant_expression (ctx, op0, |
92a596e8 | 6663 | lval, non_constant_p, overflow_p, |
56632b27 | 6664 | jump_target); |
2d76680f PC |
6665 | else |
6666 | { | |
6667 | /* Check that the LHS is constant and then discard it. */ | |
2b3ab879 | 6668 | cxx_eval_constant_expression (ctx, op0, |
12d9ce19 | 6669 | true, non_constant_p, overflow_p, |
56632b27 | 6670 | jump_target); |
06ec22b7 JM |
6671 | if (*non_constant_p) |
6672 | return t; | |
2d76680f | 6673 | op1 = TREE_OPERAND (t, 1); |
2b3ab879 | 6674 | r = cxx_eval_constant_expression (ctx, op1, |
92a596e8 | 6675 | lval, non_constant_p, overflow_p, |
56632b27 | 6676 | jump_target); |
2d76680f PC |
6677 | } |
6678 | } | |
6679 | break; | |
6680 | ||
6681 | case POINTER_PLUS_EXPR: | |
1af4ebf5 | 6682 | case POINTER_DIFF_EXPR: |
2d76680f PC |
6683 | case PLUS_EXPR: |
6684 | case MINUS_EXPR: | |
6685 | case MULT_EXPR: | |
6686 | case TRUNC_DIV_EXPR: | |
6687 | case CEIL_DIV_EXPR: | |
6688 | case FLOOR_DIV_EXPR: | |
6689 | case ROUND_DIV_EXPR: | |
6690 | case TRUNC_MOD_EXPR: | |
6691 | case CEIL_MOD_EXPR: | |
6692 | case ROUND_MOD_EXPR: | |
6693 | case RDIV_EXPR: | |
6694 | case EXACT_DIV_EXPR: | |
6695 | case MIN_EXPR: | |
6696 | case MAX_EXPR: | |
6697 | case LSHIFT_EXPR: | |
6698 | case RSHIFT_EXPR: | |
81fa6d73 JJ |
6699 | case LROTATE_EXPR: |
6700 | case RROTATE_EXPR: | |
2d76680f PC |
6701 | case BIT_IOR_EXPR: |
6702 | case BIT_XOR_EXPR: | |
6703 | case BIT_AND_EXPR: | |
6704 | case TRUTH_XOR_EXPR: | |
6705 | case LT_EXPR: | |
6706 | case LE_EXPR: | |
6707 | case GT_EXPR: | |
6708 | case GE_EXPR: | |
6709 | case EQ_EXPR: | |
6710 | case NE_EXPR: | |
b7689b96 | 6711 | case SPACESHIP_EXPR: |
2d76680f PC |
6712 | case UNORDERED_EXPR: |
6713 | case ORDERED_EXPR: | |
6714 | case UNLT_EXPR: | |
6715 | case UNLE_EXPR: | |
6716 | case UNGT_EXPR: | |
6717 | case UNGE_EXPR: | |
6718 | case UNEQ_EXPR: | |
6719 | case LTGT_EXPR: | |
6720 | case RANGE_EXPR: | |
6721 | case COMPLEX_EXPR: | |
92a596e8 | 6722 | r = cxx_eval_binary_expression (ctx, t, lval, |
2d76680f PC |
6723 | non_constant_p, overflow_p); |
6724 | break; | |
6725 | ||
6726 | /* fold can introduce non-IF versions of these; still treat them as | |
6727 | short-circuiting. */ | |
6728 | case TRUTH_AND_EXPR: | |
6729 | case TRUTH_ANDIF_EXPR: | |
3e605b20 | 6730 | r = cxx_eval_logical_expression (ctx, t, boolean_false_node, |
2d76680f | 6731 | boolean_true_node, |
92a596e8 | 6732 | lval, |
2d76680f PC |
6733 | non_constant_p, overflow_p); |
6734 | break; | |
6735 | ||
6736 | case TRUTH_OR_EXPR: | |
6737 | case TRUTH_ORIF_EXPR: | |
3e605b20 | 6738 | r = cxx_eval_logical_expression (ctx, t, boolean_true_node, |
2d76680f | 6739 | boolean_false_node, |
92a596e8 | 6740 | lval, |
2d76680f PC |
6741 | non_constant_p, overflow_p); |
6742 | break; | |
6743 | ||
6744 | case ARRAY_REF: | |
92a596e8 | 6745 | r = cxx_eval_array_reference (ctx, t, lval, |
2d76680f PC |
6746 | non_constant_p, overflow_p); |
6747 | break; | |
6748 | ||
6749 | case COMPONENT_REF: | |
6750 | if (is_overloaded_fn (t)) | |
6751 | { | |
6752 | /* We can only get here in checking mode via | |
6753 | build_non_dependent_expr, because any expression that | |
6754 | calls or takes the address of the function will have | |
6755 | pulled a FUNCTION_DECL out of the COMPONENT_REF. */ | |
2b3ab879 | 6756 | gcc_checking_assert (ctx->quiet || errorcount); |
2d76680f PC |
6757 | *non_constant_p = true; |
6758 | return t; | |
6759 | } | |
92a596e8 | 6760 | r = cxx_eval_component_reference (ctx, t, lval, |
2d76680f PC |
6761 | non_constant_p, overflow_p); |
6762 | break; | |
6763 | ||
6764 | case BIT_FIELD_REF: | |
92a596e8 | 6765 | r = cxx_eval_bit_field_ref (ctx, t, lval, |
2d76680f PC |
6766 | non_constant_p, overflow_p); |
6767 | break; | |
6768 | ||
6769 | case COND_EXPR: | |
43574e4f | 6770 | case IF_STMT: |
4b390698 JJ |
6771 | if (jump_target && *jump_target) |
6772 | { | |
e9fa2f6d | 6773 | tree orig_jump = *jump_target; |
43574e4f JJ |
6774 | tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1)) |
6775 | ? TREE_OPERAND (t, 1) : void_node); | |
4b390698 JJ |
6776 | /* When jumping to a label, the label might be either in the |
6777 | then or else blocks, so process then block first in skipping | |
6778 | mode first, and if we are still in the skipping mode at its end, | |
6779 | process the else block too. */ | |
43574e4f JJ |
6780 | r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, |
6781 | overflow_p, jump_target); | |
e9fa2f6d MP |
6782 | /* It's possible that we found the label in the then block. But |
6783 | it could have been followed by another jumping statement, e.g. | |
6784 | say we're looking for case 1: | |
6785 | if (cond) | |
6786 | { | |
6787 | // skipped statements | |
6788 | case 1:; // clears up *jump_target | |
6789 | return 1; // and sets it to a RETURN_EXPR | |
6790 | } | |
6791 | else { ... } | |
6792 | in which case we need not go looking to the else block. | |
6793 | (goto is not allowed in a constexpr function.) */ | |
6794 | if (*jump_target == orig_jump) | |
43574e4f JJ |
6795 | { |
6796 | arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2)) | |
6797 | ? TREE_OPERAND (t, 2) : void_node); | |
6798 | r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, | |
6799 | overflow_p, jump_target); | |
6800 | } | |
4b390698 JJ |
6801 | break; |
6802 | } | |
92a596e8 | 6803 | r = cxx_eval_conditional_expression (ctx, t, lval, |
56632b27 JM |
6804 | non_constant_p, overflow_p, |
6805 | jump_target); | |
2d76680f | 6806 | break; |
f370e36d JJ |
6807 | case VEC_COND_EXPR: |
6808 | r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p, | |
6809 | overflow_p); | |
6810 | break; | |
2d76680f PC |
6811 | |
6812 | case CONSTRUCTOR: | |
35d87f01 | 6813 | if (TREE_CONSTANT (t) && reduced_constant_expression_p (t)) |
2d63bc39 JM |
6814 | { |
6815 | /* Don't re-process a constant CONSTRUCTOR, but do fold it to | |
6816 | VECTOR_CST if applicable. */ | |
ac9ec198 | 6817 | verify_constructor_flags (t); |
2d63bc39 JM |
6818 | if (TREE_CONSTANT (t)) |
6819 | return fold (t); | |
6820 | } | |
92a596e8 | 6821 | r = cxx_eval_bare_aggregate (ctx, t, lval, |
2d76680f PC |
6822 | non_constant_p, overflow_p); |
6823 | break; | |
6824 | ||
6825 | case VEC_INIT_EXPR: | |
6826 | /* We can get this in a defaulted constructor for a class with a | |
6827 | non-static data member of array type. Either the initializer will | |
6828 | be NULL, meaning default-initialization, or it will be an lvalue | |
6829 | or xvalue of the same type, meaning direct-initialization from the | |
6830 | corresponding member. */ | |
92a596e8 | 6831 | r = cxx_eval_vec_init (ctx, t, lval, |
2d76680f PC |
6832 | non_constant_p, overflow_p); |
6833 | break; | |
6834 | ||
2d76680f | 6835 | case VEC_PERM_EXPR: |
92a596e8 | 6836 | r = cxx_eval_trinary_expression (ctx, t, lval, |
2d76680f PC |
6837 | non_constant_p, overflow_p); |
6838 | break; | |
6839 | ||
7d75ea04 JJ |
6840 | case NOP_EXPR: |
6841 | if (REINTERPRET_CAST_P (t)) | |
6842 | { | |
6843 | if (!ctx->quiet) | |
6821245b | 6844 | error_at (loc, |
a9c697b8 | 6845 | "%<reinterpret_cast%> is not a constant expression"); |
7d75ea04 JJ |
6846 | *non_constant_p = true; |
6847 | return t; | |
6848 | } | |
6849 | /* FALLTHROUGH. */ | |
2d76680f PC |
6850 | case CONVERT_EXPR: |
6851 | case VIEW_CONVERT_EXPR: | |
cda0a029 | 6852 | case UNARY_PLUS_EXPR: |
2d76680f PC |
6853 | { |
6854 | tree oldop = TREE_OPERAND (t, 0); | |
cda0a029 | 6855 | |
3e605b20 | 6856 | tree op = cxx_eval_constant_expression (ctx, oldop, |
92a596e8 | 6857 | lval, |
5a804683 | 6858 | non_constant_p, overflow_p); |
2d76680f PC |
6859 | if (*non_constant_p) |
6860 | return t; | |
dcdbc004 | 6861 | tree type = TREE_TYPE (t); |
02a8575c JM |
6862 | |
6863 | if (VOID_TYPE_P (type)) | |
6864 | return void_node; | |
6865 | ||
eeb54a14 JJ |
6866 | if (TREE_CODE (t) == CONVERT_EXPR |
6867 | && ARITHMETIC_TYPE_P (type) | |
82bb6673 JJ |
6868 | && INDIRECT_TYPE_P (TREE_TYPE (op)) |
6869 | && ctx->manifestly_const_eval) | |
eeb54a14 JJ |
6870 | { |
6871 | if (!ctx->quiet) | |
6872 | error_at (loc, | |
6873 | "conversion from pointer type %qT to arithmetic type " | |
6874 | "%qT in a constant expression", TREE_TYPE (op), type); | |
6875 | *non_constant_p = true; | |
6876 | return t; | |
6877 | } | |
6878 | ||
2ffc2645 MP |
6879 | /* [expr.const]: a conversion from type cv void* to a pointer-to-object |
6880 | type cannot be part of a core constant expression as a resolution to | |
6881 | DR 1312. */ | |
6252e35c | 6882 | if (TYPE_PTROB_P (type) |
2ffc2645 MP |
6883 | && TYPE_PTR_P (TREE_TYPE (op)) |
6884 | && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op))) | |
6885 | /* Inside a call to std::construct_at or to | |
6886 | std::allocator<T>::{,de}allocate, we permit casting from void* | |
6887 | because that is compiler-generated code. */ | |
6888 | && !is_std_construct_at (ctx->call) | |
6889 | && !is_std_allocator_allocate (ctx->call)) | |
6890 | { | |
6891 | /* Likewise, don't error when casting from void* when OP is | |
6892 | &heap uninit and similar. */ | |
6893 | tree sop = tree_strip_nop_conversions (op); | |
6894 | if (TREE_CODE (sop) == ADDR_EXPR | |
6895 | && VAR_P (TREE_OPERAND (sop, 0)) | |
6896 | && DECL_ARTIFICIAL (TREE_OPERAND (sop, 0))) | |
6897 | /* OK */; | |
6898 | else | |
6899 | { | |
6900 | if (!ctx->quiet) | |
6901 | error_at (loc, "cast from %qT is not allowed", | |
6902 | TREE_TYPE (op)); | |
6903 | *non_constant_p = true; | |
6904 | return t; | |
6905 | } | |
6906 | } | |
6907 | ||
8e007055 | 6908 | if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type)) |
dcdbc004 | 6909 | op = cplus_expand_constant (op); |
7d75ea04 | 6910 | |
05bf54c3 MP |
6911 | if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR) |
6912 | { | |
7d75ea04 JJ |
6913 | if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op)) |
6914 | && !can_convert_qual (type, op)) | |
6915 | op = cplus_expand_constant (op); | |
6916 | return cp_fold_convert (type, op); | |
05bf54c3 | 6917 | } |
8bada5cd | 6918 | |
71a93b08 | 6919 | if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST) |
2d76680f | 6920 | { |
8bada5cd MS |
6921 | if (integer_zerop (op)) |
6922 | { | |
9f613f06 | 6923 | if (TYPE_REF_P (type)) |
8bada5cd MS |
6924 | { |
6925 | if (!ctx->quiet) | |
2ffc2645 | 6926 | error_at (loc, "dereferencing a null pointer"); |
8bada5cd MS |
6927 | *non_constant_p = true; |
6928 | return t; | |
6929 | } | |
8bada5cd MS |
6930 | } |
6931 | else | |
6932 | { | |
6933 | /* This detects for example: | |
6934 | reinterpret_cast<void*>(sizeof 0) | |
6935 | */ | |
6936 | if (!ctx->quiet) | |
6821245b | 6937 | error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not " |
64d6d399 | 6938 | "a constant expression", |
8bada5cd MS |
6939 | type, op); |
6940 | *non_constant_p = true; | |
6941 | return t; | |
6942 | } | |
2d76680f | 6943 | } |
7d75ea04 | 6944 | |
8e007055 JJ |
6945 | if (INDIRECT_TYPE_P (type) |
6946 | && TREE_CODE (op) == NOP_EXPR | |
6947 | && TREE_TYPE (op) == ptr_type_node | |
6948 | && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR | |
6949 | && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0)) | |
815baade JJ |
6950 | && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), |
6951 | 0)) == heap_uninit_identifier | |
6952 | || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), | |
6953 | 0)) == heap_vec_uninit_identifier)) | |
8e007055 JJ |
6954 | { |
6955 | tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0); | |
6956 | tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var)); | |
6957 | tree elt_type = TREE_TYPE (type); | |
6958 | tree cookie_size = NULL_TREE; | |
6959 | if (TREE_CODE (elt_type) == RECORD_TYPE | |
6960 | && TYPE_NAME (elt_type) == heap_identifier) | |
6961 | { | |
6962 | tree fld1 = TYPE_FIELDS (elt_type); | |
6963 | tree fld2 = DECL_CHAIN (fld1); | |
6964 | elt_type = TREE_TYPE (TREE_TYPE (fld2)); | |
6965 | cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1)); | |
6966 | } | |
815baade JJ |
6967 | DECL_NAME (var) |
6968 | = (DECL_NAME (var) == heap_uninit_identifier | |
6969 | ? heap_identifier : heap_vec_identifier); | |
8e007055 JJ |
6970 | TREE_TYPE (var) |
6971 | = build_new_constexpr_heap_type (elt_type, cookie_size, | |
6972 | var_size); | |
6973 | TREE_TYPE (TREE_OPERAND (op, 0)) | |
6974 | = build_pointer_type (TREE_TYPE (var)); | |
6975 | } | |
6976 | ||
cda0a029 | 6977 | if (op == oldop && tcode != UNARY_PLUS_EXPR) |
2d76680f PC |
6978 | /* We didn't fold at the top so we could check for ptr-int |
6979 | conversion. */ | |
6980 | return fold (t); | |
7d75ea04 | 6981 | |
02a8575c JM |
6982 | tree sop; |
6983 | ||
e4511ca2 JM |
6984 | /* Handle an array's bounds having been deduced after we built |
6985 | the wrapping expression. */ | |
6986 | if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op))) | |
6987 | r = op; | |
02a8575c JM |
6988 | else if (sop = tree_strip_nop_conversions (op), |
6989 | sop != op && (same_type_ignoring_tlq_and_bounds_p | |
6990 | (type, TREE_TYPE (sop)))) | |
6991 | r = sop; | |
e4511ca2 | 6992 | else if (tcode == UNARY_PLUS_EXPR) |
cda0a029 JM |
6993 | r = fold_convert (TREE_TYPE (t), op); |
6994 | else | |
6995 | r = fold_build1 (tcode, type, op); | |
7d75ea04 | 6996 | |
2d76680f PC |
6997 | /* Conversion of an out-of-range value has implementation-defined |
6998 | behavior; the language considers it different from arithmetic | |
6999 | overflow, which is undefined. */ | |
7000 | if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op)) | |
7001 | TREE_OVERFLOW (r) = false; | |
7002 | } | |
7003 | break; | |
7004 | ||
7005 | case EMPTY_CLASS_EXPR: | |
46fdced6 PP |
7006 | /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering |
7007 | it to an appropriate CONSTRUCTOR. */ | |
7008 | return build_constructor (TREE_TYPE (t), NULL); | |
2d76680f | 7009 | |
60813a46 | 7010 | case STATEMENT_LIST: |
56632b27 JM |
7011 | new_ctx = *ctx; |
7012 | new_ctx.ctor = new_ctx.object = NULL_TREE; | |
2b3ab879 | 7013 | return cxx_eval_statement_list (&new_ctx, t, |
56632b27 | 7014 | non_constant_p, overflow_p, jump_target); |
60813a46 JM |
7015 | |
7016 | case BIND_EXPR: | |
7017 | return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t), | |
92a596e8 | 7018 | lval, |
56632b27 JM |
7019 | non_constant_p, overflow_p, |
7020 | jump_target); | |
60813a46 | 7021 | |
2d76680f PC |
7022 | case PREINCREMENT_EXPR: |
7023 | case POSTINCREMENT_EXPR: | |
7024 | case PREDECREMENT_EXPR: | |
7025 | case POSTDECREMENT_EXPR: | |
2b3ab879 | 7026 | return cxx_eval_increment_expression (ctx, t, |
92a596e8 | 7027 | lval, non_constant_p, overflow_p); |
60813a46 JM |
7028 | |
7029 | case LAMBDA_EXPR: | |
2d76680f PC |
7030 | case NEW_EXPR: |
7031 | case VEC_NEW_EXPR: | |
7032 | case DELETE_EXPR: | |
7033 | case VEC_DELETE_EXPR: | |
7034 | case THROW_EXPR: | |
2d76680f PC |
7035 | case MODOP_EXPR: |
7036 | /* GCC internal stuff. */ | |
7037 | case VA_ARG_EXPR: | |
2d76680f PC |
7038 | case NON_DEPENDENT_EXPR: |
7039 | case BASELINK: | |
2d76680f | 7040 | case OFFSET_REF: |
2b3ab879 | 7041 | if (!ctx->quiet) |
6821245b | 7042 | error_at (loc, "expression %qE is not a constant expression", t); |
2d76680f PC |
7043 | *non_constant_p = true; |
7044 | break; | |
7045 | ||
bf8d8309 | 7046 | case OBJ_TYPE_REF: |
7ece3bd8 JM |
7047 | /* Virtual function lookup. We don't need to do anything fancy. */ |
7048 | return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t), | |
7049 | lval, non_constant_p, overflow_p); | |
bf8d8309 | 7050 | |
3e605b20 | 7051 | case PLACEHOLDER_EXPR: |
89262ec6 JM |
7052 | /* Use of the value or address of the current object. */ |
7053 | if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t))) | |
b599bf9d PP |
7054 | { |
7055 | if (TREE_CODE (ctor) == CONSTRUCTOR) | |
7056 | return ctor; | |
7057 | else | |
7058 | return cxx_eval_constant_expression (ctx, ctor, lval, | |
7059 | non_constant_p, overflow_p); | |
7060 | } | |
89262ec6 JM |
7061 | /* A placeholder without a referent. We can get here when |
7062 | checking whether NSDMIs are noexcept, or in massage_init_elt; | |
7063 | just say it's non-constant for now. */ | |
7064 | gcc_assert (ctx->quiet); | |
7065 | *non_constant_p = true; | |
3e605b20 JM |
7066 | break; |
7067 | ||
06ec22b7 JM |
7068 | case EXIT_EXPR: |
7069 | { | |
7070 | tree cond = TREE_OPERAND (t, 0); | |
7071 | cond = cxx_eval_constant_expression (ctx, cond, /*lval*/false, | |
7072 | non_constant_p, overflow_p); | |
7073 | VERIFY_CONSTANT (cond); | |
7074 | if (integer_nonzerop (cond)) | |
7075 | *jump_target = t; | |
7076 | } | |
7077 | break; | |
7078 | ||
60813a46 | 7079 | case GOTO_EXPR: |
8892d532 JJ |
7080 | if (breaks (&TREE_OPERAND (t, 0)) |
7081 | || continues (&TREE_OPERAND (t, 0)) | |
7082 | /* Allow for jumping to a cdtor_label. */ | |
7083 | || returns (&TREE_OPERAND (t, 0))) | |
7084 | *jump_target = TREE_OPERAND (t, 0); | |
7085 | else | |
7086 | { | |
7087 | gcc_assert (cxx_dialect >= cxx23); | |
7088 | if (!ctx->quiet) | |
7089 | error_at (loc, "%<goto%> is not a constant expression"); | |
7090 | *non_constant_p = true; | |
7091 | } | |
56632b27 JM |
7092 | break; |
7093 | ||
60813a46 | 7094 | case LOOP_EXPR: |
43574e4f JJ |
7095 | case DO_STMT: |
7096 | case WHILE_STMT: | |
7097 | case FOR_STMT: | |
2b3ab879 | 7098 | cxx_eval_loop_expr (ctx, t, |
56632b27 JM |
7099 | non_constant_p, overflow_p, jump_target); |
7100 | break; | |
7101 | ||
60813a46 | 7102 | case SWITCH_EXPR: |
43574e4f | 7103 | case SWITCH_STMT: |
2b3ab879 | 7104 | cxx_eval_switch_expr (ctx, t, |
56632b27 | 7105 | non_constant_p, overflow_p, jump_target); |
60813a46 JM |
7106 | break; |
7107 | ||
971e17ff AS |
7108 | case REQUIRES_EXPR: |
7109 | /* It's possible to get a requires-expression in a constant | |
7110 | expression. For example: | |
7111 | ||
7112 | template<typename T> concept bool C() { | |
7113 | return requires (T t) { t; }; | |
7114 | } | |
7115 | ||
7116 | template<typename T> requires !C<T>() void f(T); | |
7117 | ||
7118 | Normalization leaves f with the associated constraint | |
7119 | '!requires (T t) { ... }' which is not transformed into | |
7120 | a constraint. */ | |
7121 | if (!processing_template_decl) | |
662ef5b5 | 7122 | return evaluate_requires_expr (t); |
971e17ff AS |
7123 | else |
7124 | *non_constant_p = true; | |
7125 | return t; | |
7126 | ||
98e09245 | 7127 | case ANNOTATE_EXPR: |
98e09245 JJ |
7128 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
7129 | lval, | |
7130 | non_constant_p, overflow_p, | |
7131 | jump_target); | |
7132 | break; | |
7133 | ||
3664e317 JM |
7134 | case USING_STMT: |
7135 | r = void_node; | |
7136 | break; | |
7137 | ||
cb57504a JM |
7138 | case TEMPLATE_ID_EXPR: |
7139 | { | |
7140 | /* We can evaluate template-id that refers to a concept only if | |
7141 | the template arguments are non-dependent. */ | |
861d4af8 AS |
7142 | tree id = unpack_concept_check (t); |
7143 | tree tmpl = TREE_OPERAND (id, 0); | |
7144 | if (!concept_definition_p (tmpl)) | |
cb57504a JM |
7145 | internal_error ("unexpected template-id %qE", t); |
7146 | ||
861d4af8 AS |
7147 | if (function_concept_p (tmpl)) |
7148 | { | |
7149 | if (!ctx->quiet) | |
7150 | error_at (cp_expr_loc_or_input_loc (t), | |
7151 | "function concept must be called"); | |
7152 | r = error_mark_node; | |
7153 | break; | |
7154 | } | |
7155 | ||
9329344a | 7156 | if (!value_dependent_expression_p (t) |
6d1556ec | 7157 | && !uid_sensitive_constexpr_evaluation_p ()) |
662ef5b5 | 7158 | r = evaluate_concept_check (t); |
cb57504a JM |
7159 | else |
7160 | *non_constant_p = true; | |
861d4af8 AS |
7161 | |
7162 | break; | |
cb57504a JM |
7163 | } |
7164 | ||
529bc410 MP |
7165 | case ASM_EXPR: |
7166 | if (!ctx->quiet) | |
6821245b | 7167 | inline_asm_in_constexpr_error (loc); |
529bc410 MP |
7168 | *non_constant_p = true; |
7169 | return t; | |
7170 | ||
896048cf | 7171 | case BIT_CAST_EXPR: |
606f2af1 JJ |
7172 | if (lval) |
7173 | { | |
7174 | if (!ctx->quiet) | |
7175 | error_at (EXPR_LOCATION (t), | |
7176 | "address of a call to %qs is not a constant expression", | |
7177 | "__builtin_bit_cast"); | |
7178 | *non_constant_p = true; | |
7179 | return t; | |
7180 | } | |
896048cf JJ |
7181 | r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p); |
7182 | break; | |
7183 | ||
2d76680f | 7184 | default: |
c6e7c499 JM |
7185 | if (STATEMENT_CODE_P (TREE_CODE (t))) |
7186 | { | |
7187 | /* This function doesn't know how to deal with pre-genericize | |
7188 | statements; this can only happen with statement-expressions, | |
7189 | so for now just fail. */ | |
7190 | if (!ctx->quiet) | |
7191 | error_at (EXPR_LOCATION (t), | |
64d6d399 | 7192 | "statement is not a constant expression"); |
c6e7c499 JM |
7193 | } |
7194 | else | |
7195 | internal_error ("unexpected expression %qE of kind %s", t, | |
7196 | get_tree_code_name (TREE_CODE (t))); | |
2d76680f PC |
7197 | *non_constant_p = true; |
7198 | break; | |
7199 | } | |
7200 | ||
7201 | if (r == error_mark_node) | |
7202 | *non_constant_p = true; | |
7203 | ||
7204 | if (*non_constant_p) | |
7205 | return t; | |
7206 | else | |
7207 | return r; | |
7208 | } | |
7209 | ||
e079dced JM |
7210 | /* P0859: A function is needed for constant evaluation if it is a constexpr |
7211 | function that is named by an expression ([basic.def.odr]) that is | |
7212 | potentially constant evaluated. | |
7213 | ||
7214 | So we need to instantiate any constexpr functions mentioned by the | |
7215 | expression even if the definition isn't needed for evaluating the | |
7216 | expression. */ | |
7217 | ||
7218 | static tree | |
7219 | instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/) | |
7220 | { | |
7221 | if (TREE_CODE (*tp) == FUNCTION_DECL | |
7222 | && DECL_DECLARED_CONSTEXPR_P (*tp) | |
7223 | && !DECL_INITIAL (*tp) | |
4fdda3ce | 7224 | && !trivial_fn_p (*tp) |
f65a3299 PP |
7225 | && DECL_TEMPLOID_INSTANTIATION (*tp) |
7226 | && !uid_sensitive_constexpr_evaluation_p ()) | |
e079dced JM |
7227 | { |
7228 | ++function_depth; | |
7229 | instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false); | |
7230 | --function_depth; | |
7231 | } | |
7232 | else if (TREE_CODE (*tp) == CALL_EXPR | |
7233 | || TREE_CODE (*tp) == AGGR_INIT_EXPR) | |
7234 | { | |
7235 | if (EXPR_HAS_LOCATION (*tp)) | |
7236 | input_location = EXPR_LOCATION (*tp); | |
7237 | } | |
7238 | ||
7239 | if (!EXPR_P (*tp)) | |
7240 | *walk_subtrees = 0; | |
7241 | ||
7242 | return NULL_TREE; | |
7243 | } | |
8e007055 | 7244 | |
e079dced JM |
7245 | static void |
7246 | instantiate_constexpr_fns (tree t) | |
7247 | { | |
7248 | location_t loc = input_location; | |
7249 | cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL); | |
7250 | input_location = loc; | |
7251 | } | |
7252 | ||
8e007055 JJ |
7253 | /* Look for heap variables in the expression *TP. */ |
7254 | ||
7255 | static tree | |
7256 | find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/) | |
7257 | { | |
7258 | if (VAR_P (*tp) | |
7259 | && (DECL_NAME (*tp) == heap_uninit_identifier | |
7260 | || DECL_NAME (*tp) == heap_identifier | |
815baade JJ |
7261 | || DECL_NAME (*tp) == heap_vec_uninit_identifier |
7262 | || DECL_NAME (*tp) == heap_vec_identifier | |
8e007055 JJ |
7263 | || DECL_NAME (*tp) == heap_deleted_identifier)) |
7264 | return *tp; | |
7265 | ||
7266 | if (TYPE_P (*tp)) | |
7267 | *walk_subtrees = 0; | |
7268 | return NULL_TREE; | |
7269 | } | |
7270 | ||
f968ef9b JJ |
7271 | /* Find immediate function decls in *TP if any. */ |
7272 | ||
7273 | static tree | |
7274 | find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/) | |
7275 | { | |
7276 | if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp)) | |
7277 | return *tp; | |
4b2fda8b JJ |
7278 | if (TREE_CODE (*tp) == PTRMEM_CST |
7279 | && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL | |
7280 | && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp))) | |
7281 | return PTRMEM_CST_MEMBER (*tp); | |
f968ef9b JJ |
7282 | return NULL_TREE; |
7283 | } | |
7284 | ||
e4082611 | 7285 | /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression. |
d4accef3 JM |
7286 | STRICT has the same sense as for constant_value_1: true if we only allow |
7287 | conforming C++ constant expressions, or false if we want a constant value | |
7288 | even if it doesn't conform. | |
13de99bc | 7289 | MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as |
8e007055 JJ |
7290 | per P0595 even when ALLOW_NON_CONSTANT is true. |
7291 | CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable. | |
7292 | OBJECT must be non-NULL in that case. */ | |
e4082611 | 7293 | |
2d76680f | 7294 | static tree |
3e605b20 | 7295 | cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant, |
e4082611 | 7296 | bool strict = true, |
13de99bc | 7297 | bool manifestly_const_eval = false, |
8e007055 | 7298 | bool constexpr_dtor = false, |
f65a3299 | 7299 | tree object = NULL_TREE) |
2d76680f | 7300 | { |
8108ea30 JM |
7301 | auto_timevar time (TV_CONSTEXPR); |
7302 | ||
2d76680f PC |
7303 | bool non_constant_p = false; |
7304 | bool overflow_p = false; | |
39dce2b7 | 7305 | |
d419e176 MP |
7306 | if (BRACE_ENCLOSED_INITIALIZER_P (t)) |
7307 | { | |
7308 | gcc_checking_assert (allow_non_constant); | |
7309 | return t; | |
7310 | } | |
7311 | ||
8e007055 | 7312 | constexpr_global_ctx global_ctx; |
49a86fce | 7313 | constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL, |
8e007055 | 7314 | allow_non_constant, strict, |
f65a3299 | 7315 | manifestly_const_eval || !allow_non_constant }; |
39dce2b7 | 7316 | |
6641d6d3 JJ |
7317 | /* Turn off -frounding-math for manifestly constant evaluation. */ |
7318 | warning_sentinel rm (flag_rounding_math, ctx.manifestly_const_eval); | |
3e605b20 | 7319 | tree type = initialized_type (t); |
3e605b20 | 7320 | tree r = t; |
f968ef9b | 7321 | bool is_consteval = false; |
4d0c18c6 | 7322 | if (VOID_TYPE_P (type)) |
8e007055 JJ |
7323 | { |
7324 | if (constexpr_dtor) | |
7325 | /* Used for destructors of array elements. */ | |
7326 | type = TREE_TYPE (object); | |
7327 | else | |
f968ef9b | 7328 | { |
b04445d4 | 7329 | if (cxx_dialect < cxx20) |
f968ef9b JJ |
7330 | return t; |
7331 | if (TREE_CODE (t) != CALL_EXPR && TREE_CODE (t) != AGGR_INIT_EXPR) | |
7332 | return t; | |
7333 | /* Calls to immediate functions returning void need to be | |
7334 | evaluated. */ | |
7335 | tree fndecl = cp_get_callee_fndecl_nofold (t); | |
7336 | if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl)) | |
7337 | return t; | |
7338 | else | |
7339 | is_consteval = true; | |
7340 | } | |
7341 | } | |
b04445d4 | 7342 | else if (cxx_dialect >= cxx20 |
f968ef9b JJ |
7343 | && (TREE_CODE (t) == CALL_EXPR |
7344 | || TREE_CODE (t) == AGGR_INIT_EXPR | |
7345 | || TREE_CODE (t) == TARGET_EXPR)) | |
7346 | { | |
861d4af8 AS |
7347 | /* For non-concept checks, determine if it is consteval. */ |
7348 | if (!concept_check_p (t)) | |
7349 | { | |
7350 | tree x = t; | |
7351 | if (TREE_CODE (x) == TARGET_EXPR) | |
7352 | x = TARGET_EXPR_INITIAL (x); | |
7353 | tree fndecl = cp_get_callee_fndecl_nofold (x); | |
7354 | if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl)) | |
7355 | is_consteval = true; | |
7356 | } | |
8e007055 | 7357 | } |
3e605b20 JM |
7358 | if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)) |
7359 | { | |
7360 | /* In C++14 an NSDMI can participate in aggregate initialization, | |
7361 | and can refer to the address of the object being initialized, so | |
7362 | we need to pass in the relevant VAR_DECL if we want to do the | |
7363 | evaluation in a single pass. The evaluation will dynamically | |
7364 | update ctx.values for the VAR_DECL. We use the same strategy | |
7365 | for C++11 constexpr constructors that refer to the object being | |
7366 | initialized. */ | |
8e007055 JJ |
7367 | if (constexpr_dtor) |
7368 | { | |
7369 | gcc_assert (object && VAR_P (object)); | |
7370 | gcc_assert (DECL_DECLARED_CONSTEXPR_P (object)); | |
7371 | gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object)); | |
fce6467b JJ |
7372 | if (error_operand_p (DECL_INITIAL (object))) |
7373 | return t; | |
8e007055 JJ |
7374 | ctx.ctor = unshare_expr (DECL_INITIAL (object)); |
7375 | TREE_READONLY (ctx.ctor) = false; | |
7376 | /* Temporarily force decl_really_constant_value to return false | |
7377 | for it, we want to use ctx.ctor for the current value instead. */ | |
7378 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false; | |
7379 | } | |
7380 | else | |
7381 | { | |
7382 | ctx.ctor = build_constructor (type, NULL); | |
7383 | CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true; | |
7384 | } | |
60813a46 JM |
7385 | if (!object) |
7386 | { | |
7387 | if (TREE_CODE (t) == TARGET_EXPR) | |
7388 | object = TARGET_EXPR_SLOT (t); | |
7389 | else if (TREE_CODE (t) == AGGR_INIT_EXPR) | |
7390 | object = AGGR_INIT_EXPR_SLOT (t); | |
7391 | } | |
3e605b20 JM |
7392 | ctx.object = object; |
7393 | if (object) | |
7394 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
7395 | (type, TREE_TYPE (object))); | |
7396 | if (object && DECL_P (object)) | |
8e007055 | 7397 | global_ctx.values.put (object, ctx.ctor); |
3e605b20 JM |
7398 | if (TREE_CODE (r) == TARGET_EXPR) |
7399 | /* Avoid creating another CONSTRUCTOR when we expand the | |
7400 | TARGET_EXPR. */ | |
7401 | r = TARGET_EXPR_INITIAL (r); | |
7402 | } | |
7403 | ||
ee1de08d JJ |
7404 | auto_vec<tree, 16> cleanups; |
7405 | global_ctx.cleanups = &cleanups; | |
7406 | ||
1595fe44 JM |
7407 | if (manifestly_const_eval) |
7408 | instantiate_constexpr_fns (r); | |
2b3ab879 | 7409 | r = cxx_eval_constant_expression (&ctx, r, |
5a804683 | 7410 | false, &non_constant_p, &overflow_p); |
2d76680f | 7411 | |
8e007055 JJ |
7412 | if (!constexpr_dtor) |
7413 | verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p); | |
7414 | else | |
7415 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true; | |
2d76680f | 7416 | |
ee1de08d JJ |
7417 | unsigned int i; |
7418 | tree cleanup; | |
7419 | /* Evaluate the cleanups. */ | |
7420 | FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup) | |
7421 | cxx_eval_constant_expression (&ctx, cleanup, false, | |
7422 | &non_constant_p, &overflow_p); | |
7423 | ||
023d89c7 JM |
7424 | /* Mutable logic is a bit tricky: we want to allow initialization of |
7425 | constexpr variables with mutable members, but we can't copy those | |
7426 | members to another constexpr variable. */ | |
8e007055 | 7427 | if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_MUTABLE_POISON (r)) |
2d76680f | 7428 | { |
2d76680f | 7429 | if (!allow_non_constant) |
023d89c7 JM |
7430 | error ("%qE is not a constant expression because it refers to " |
7431 | "mutable subobjects of %qT", t, type); | |
2d76680f PC |
7432 | non_constant_p = true; |
7433 | } | |
7434 | ||
8e007055 | 7435 | if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r)) |
f64e0c02 JM |
7436 | { |
7437 | if (!allow_non_constant) | |
7438 | error ("%qE is not a constant expression because it refers to " | |
7439 | "an incompletely initialized variable", t); | |
7440 | TREE_CONSTANT (r) = false; | |
7441 | non_constant_p = true; | |
7442 | } | |
7443 | ||
8e007055 JJ |
7444 | if (!global_ctx.heap_vars.is_empty ()) |
7445 | { | |
7446 | tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs, | |
7447 | NULL); | |
7448 | unsigned int i; | |
7449 | if (heap_var) | |
7450 | { | |
7451 | if (!allow_non_constant && !non_constant_p) | |
7452 | error_at (DECL_SOURCE_LOCATION (heap_var), | |
7453 | "%qE is not a constant expression because it refers to " | |
7454 | "a result of %<operator new%>", t); | |
7455 | r = t; | |
7456 | non_constant_p = true; | |
7457 | } | |
7458 | FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var) | |
a8db7887 JJ |
7459 | { |
7460 | if (DECL_NAME (heap_var) != heap_deleted_identifier) | |
7461 | { | |
7462 | if (!allow_non_constant && !non_constant_p) | |
7463 | error_at (DECL_SOURCE_LOCATION (heap_var), | |
7464 | "%qE is not a constant expression because allocated " | |
7465 | "storage has not been deallocated", t); | |
7466 | r = t; | |
7467 | non_constant_p = true; | |
7468 | } | |
7469 | varpool_node::get (heap_var)->remove (); | |
7470 | } | |
8e007055 JJ |
7471 | } |
7472 | ||
f968ef9b | 7473 | /* Check that immediate invocation does not return an expression referencing |
7473b8a9 JJ |
7474 | any immediate function decls. */ |
7475 | if (is_consteval || in_immediate_context ()) | |
f968ef9b JJ |
7476 | if (tree immediate_fndecl |
7477 | = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl, | |
7478 | NULL)) | |
7479 | { | |
7480 | if (!allow_non_constant && !non_constant_p) | |
7481 | error_at (cp_expr_loc_or_input_loc (t), | |
7482 | "immediate evaluation returns address of immediate " | |
7483 | "function %qD", immediate_fndecl); | |
7484 | r = t; | |
7485 | non_constant_p = true; | |
7486 | } | |
7487 | ||
8895443a JM |
7488 | if (non_constant_p) |
7489 | /* If we saw something bad, go back to our argument. The wrapping below is | |
7490 | only for the cases of TREE_CONSTANT argument or overflow. */ | |
7491 | r = t; | |
2d76680f PC |
7492 | |
7493 | if (!non_constant_p && overflow_p) | |
7494 | non_constant_p = true; | |
7495 | ||
e0804c9b | 7496 | /* Unshare the result. */ |
56cfb596 | 7497 | bool should_unshare = true; |
e0804c9b JM |
7498 | if (r == t || (TREE_CODE (t) == TARGET_EXPR |
7499 | && TARGET_EXPR_INITIAL (t) == r)) | |
56cfb596 PP |
7500 | should_unshare = false; |
7501 | ||
2d76680f PC |
7502 | if (non_constant_p && !allow_non_constant) |
7503 | return error_mark_node; | |
8e007055 JJ |
7504 | else if (constexpr_dtor) |
7505 | return r; | |
2d76680f PC |
7506 | else if (non_constant_p && TREE_CONSTANT (r)) |
7507 | { | |
eddd715c JM |
7508 | /* This isn't actually constant, so unset TREE_CONSTANT. |
7509 | Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires | |
7510 | it to be set if it is invariant address, even when it is not | |
7511 | a valid C++ constant expression. Wrap it with a NOP_EXPR | |
7512 | instead. */ | |
7513 | if (EXPR_P (r) && TREE_CODE (r) != ADDR_EXPR) | |
2d76680f PC |
7514 | r = copy_node (r); |
7515 | else if (TREE_CODE (r) == CONSTRUCTOR) | |
7516 | r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r); | |
7517 | else | |
7518 | r = build_nop (TREE_TYPE (r), r); | |
7519 | TREE_CONSTANT (r) = false; | |
7520 | } | |
5dab8b11 | 7521 | else if (non_constant_p) |
2d76680f PC |
7522 | return t; |
7523 | ||
56cfb596 PP |
7524 | if (should_unshare) |
7525 | r = unshare_expr (r); | |
7526 | ||
2d76680f PC |
7527 | if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r))) |
7528 | { | |
5dab8b11 | 7529 | r = adjust_temp_type (type, r); |
2d76680f PC |
7530 | if (TREE_CODE (t) == TARGET_EXPR |
7531 | && TARGET_EXPR_INITIAL (t) == r) | |
7532 | return t; | |
5dab8b11 | 7533 | else if (TREE_CODE (t) != CONSTRUCTOR) |
2d76680f | 7534 | { |
8e007055 | 7535 | r = get_target_expr_sfinae (r, tf_warning_or_error | tf_no_cleanup); |
2d76680f | 7536 | TREE_CONSTANT (r) = true; |
2d76680f PC |
7537 | } |
7538 | } | |
5dab8b11 | 7539 | |
729f6881 JM |
7540 | /* Remember the original location if that wouldn't need a wrapper. */ |
7541 | if (location_t loc = EXPR_LOCATION (t)) | |
8d970817 | 7542 | protected_set_expr_location (r, loc); |
729f6881 | 7543 | |
5dab8b11 | 7544 | return r; |
2d76680f PC |
7545 | } |
7546 | ||
2d76680f PC |
7547 | /* If T represents a constant expression returns its reduced value. |
7548 | Otherwise return error_mark_node. If T is dependent, then | |
7549 | return NULL. */ | |
7550 | ||
7551 | tree | |
3e605b20 | 7552 | cxx_constant_value (tree t, tree decl) |
2d76680f | 7553 | { |
8e007055 JJ |
7554 | return cxx_eval_outermost_constant_expr (t, false, true, true, false, decl); |
7555 | } | |
7556 | ||
a8de8324 JM |
7557 | /* As above, but respect SFINAE. */ |
7558 | ||
7559 | tree | |
7560 | cxx_constant_value_sfinae (tree t, tsubst_flags_t complain) | |
7561 | { | |
7562 | bool sfinae = !(complain & tf_error); | |
7563 | tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, true); | |
7564 | if (sfinae && !TREE_CONSTANT (r)) | |
7565 | r = error_mark_node; | |
7566 | return r; | |
7567 | } | |
7568 | ||
8e007055 JJ |
7569 | /* Like cxx_constant_value, but used for evaluation of constexpr destructors |
7570 | of constexpr variables. The actual initializer of DECL is not modified. */ | |
7571 | ||
7572 | void | |
7573 | cxx_constant_dtor (tree t, tree decl) | |
7574 | { | |
7575 | cxx_eval_outermost_constant_expr (t, false, true, true, true, decl); | |
2d76680f PC |
7576 | } |
7577 | ||
cda0a029 JM |
7578 | /* Helper routine for fold_simple function. Either return simplified |
7579 | expression T, otherwise NULL_TREE. | |
7580 | In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold | |
7581 | even if we are within template-declaration. So be careful on call, as in | |
7582 | such case types can be undefined. */ | |
7583 | ||
7584 | static tree | |
7585 | fold_simple_1 (tree t) | |
7586 | { | |
7587 | tree op1; | |
7588 | enum tree_code code = TREE_CODE (t); | |
7589 | ||
7590 | switch (code) | |
7591 | { | |
7592 | case INTEGER_CST: | |
7593 | case REAL_CST: | |
7594 | case VECTOR_CST: | |
7595 | case FIXED_CST: | |
7596 | case COMPLEX_CST: | |
7597 | return t; | |
7598 | ||
7599 | case SIZEOF_EXPR: | |
7600 | return fold_sizeof_expr (t); | |
7601 | ||
7602 | case ABS_EXPR: | |
e28fadbc | 7603 | case ABSU_EXPR: |
cda0a029 JM |
7604 | case CONJ_EXPR: |
7605 | case REALPART_EXPR: | |
7606 | case IMAGPART_EXPR: | |
7607 | case NEGATE_EXPR: | |
7608 | case BIT_NOT_EXPR: | |
7609 | case TRUTH_NOT_EXPR: | |
7610 | case NOP_EXPR: | |
7611 | case VIEW_CONVERT_EXPR: | |
7612 | case CONVERT_EXPR: | |
7613 | case FLOAT_EXPR: | |
7614 | case FIX_TRUNC_EXPR: | |
7615 | case FIXED_CONVERT_EXPR: | |
7616 | case ADDR_SPACE_CONVERT_EXPR: | |
7617 | ||
7618 | op1 = TREE_OPERAND (t, 0); | |
7619 | ||
7620 | t = const_unop (code, TREE_TYPE (t), op1); | |
7621 | if (!t) | |
7622 | return NULL_TREE; | |
7623 | ||
7624 | if (CONVERT_EXPR_CODE_P (code) | |
7625 | && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1)) | |
7626 | TREE_OVERFLOW (t) = false; | |
7627 | return t; | |
7628 | ||
7629 | default: | |
7630 | return NULL_TREE; | |
7631 | } | |
7632 | } | |
7633 | ||
7634 | /* If T is a simple constant expression, returns its simplified value. | |
dba99244 | 7635 | Otherwise returns T. In contrast to maybe_constant_value we |
cda0a029 JM |
7636 | simplify only few operations on constant-expressions, and we don't |
7637 | try to simplify constexpressions. */ | |
7638 | ||
7639 | tree | |
7640 | fold_simple (tree t) | |
7641 | { | |
cda0a029 JM |
7642 | if (processing_template_decl) |
7643 | return t; | |
7644 | ||
dba99244 MP |
7645 | tree r = fold_simple_1 (t); |
7646 | if (r) | |
7647 | return r; | |
cda0a029 | 7648 | |
dba99244 | 7649 | return t; |
cda0a029 JM |
7650 | } |
7651 | ||
2d76680f PC |
7652 | /* If T is a constant expression, returns its reduced value. |
7653 | Otherwise, if T does not have TREE_CONSTANT set, returns T. | |
13de99bc JJ |
7654 | Otherwise, returns a version of T without TREE_CONSTANT. |
7655 | MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated | |
f65a3299 | 7656 | as per P0595. */ |
2d76680f | 7657 | |
8a87daca JM |
7658 | static GTY((deletable)) hash_map<tree, tree> *cv_cache; |
7659 | ||
7660 | tree | |
f65a3299 | 7661 | maybe_constant_value (tree t, tree decl, bool manifestly_const_eval) |
2d76680f PC |
7662 | { |
7663 | tree r; | |
7664 | ||
a0ab7ccd | 7665 | if (!is_nondependent_constant_expression (t)) |
2d76680f PC |
7666 | { |
7667 | if (TREE_OVERFLOW_P (t)) | |
7668 | { | |
7669 | t = build_nop (TREE_TYPE (t), t); | |
7670 | TREE_CONSTANT (t) = false; | |
7671 | } | |
7672 | return t; | |
7673 | } | |
8a87daca JM |
7674 | else if (CONSTANT_CLASS_P (t)) |
7675 | /* No caching or evaluation needed. */ | |
7676 | return t; | |
7677 | ||
13de99bc | 7678 | if (manifestly_const_eval) |
f65a3299 | 7679 | return cxx_eval_outermost_constant_expr (t, true, true, true, false, decl); |
13de99bc | 7680 | |
8a87daca JM |
7681 | if (cv_cache == NULL) |
7682 | cv_cache = hash_map<tree, tree>::create_ggc (101); | |
7683 | if (tree *cached = cv_cache->get (t)) | |
173c8def JM |
7684 | { |
7685 | r = *cached; | |
7686 | if (r != t) | |
7687 | { | |
9f143008 | 7688 | r = break_out_target_exprs (r, /*clear_loc*/true); |
173c8def JM |
7689 | protected_set_expr_location (r, EXPR_LOCATION (t)); |
7690 | } | |
dfffecb8 | 7691 | return r; |
173c8def | 7692 | } |
2d76680f | 7693 | |
f65a3299 PP |
7694 | uid_sensitive_constexpr_evaluation_checker c; |
7695 | r = cxx_eval_outermost_constant_expr (t, true, true, false, false, decl); | |
595278be MM |
7696 | gcc_checking_assert (r == t |
7697 | || CONVERT_EXPR_P (t) | |
7698 | || TREE_CODE (t) == VIEW_CONVERT_EXPR | |
7699 | || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) | |
7700 | || !cp_tree_equal (r, t)); | |
f65a3299 PP |
7701 | if (!c.evaluation_restricted_p ()) |
7702 | cv_cache->put (t, r); | |
2d76680f PC |
7703 | return r; |
7704 | } | |
7705 | ||
7a7ac32a PP |
7706 | /* Dispose of the whole CV_CACHE. */ |
7707 | ||
7708 | static void | |
7709 | clear_cv_cache (void) | |
7710 | { | |
7711 | if (cv_cache != NULL) | |
7712 | cv_cache->empty (); | |
7713 | } | |
7714 | ||
bebabf70 | 7715 | /* Dispose of the whole CV_CACHE and FOLD_CACHE. */ |
1e297006 MP |
7716 | |
7717 | void | |
bebabf70 | 7718 | clear_cv_and_fold_caches () |
1e297006 | 7719 | { |
7a7ac32a | 7720 | clear_cv_cache (); |
eba9e839 | 7721 | clear_fold_cache (); |
1e297006 MP |
7722 | } |
7723 | ||
a81c8e8c MP |
7724 | /* Internal function handling expressions in templates for |
7725 | fold_non_dependent_expr and fold_non_dependent_init. | |
7726 | ||
7727 | If we're in a template, but T isn't value dependent, simplify | |
7728 | it. We're supposed to treat: | |
7729 | ||
7730 | template <typename T> void f(T[1 + 1]); | |
7731 | template <typename T> void f(T[2]); | |
7732 | ||
7733 | as two declarations of the same function, for example. */ | |
7734 | ||
7735 | static tree | |
7736 | fold_non_dependent_expr_template (tree t, tsubst_flags_t complain, | |
f968ef9b JJ |
7737 | bool manifestly_const_eval, |
7738 | tree object) | |
a81c8e8c MP |
7739 | { |
7740 | gcc_assert (processing_template_decl); | |
7741 | ||
7742 | if (is_nondependent_constant_expression (t)) | |
7743 | { | |
7744 | processing_template_decl_sentinel s; | |
7745 | t = instantiate_non_dependent_expr_internal (t, complain); | |
7746 | ||
7747 | if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t)) | |
7748 | { | |
7749 | if (TREE_OVERFLOW_P (t)) | |
7750 | { | |
7751 | t = build_nop (TREE_TYPE (t), t); | |
7752 | TREE_CONSTANT (t) = false; | |
7753 | } | |
7754 | return t; | |
7755 | } | |
7756 | ||
7757 | tree r = cxx_eval_outermost_constant_expr (t, true, true, | |
7758 | manifestly_const_eval, | |
f968ef9b | 7759 | false, object); |
a81c8e8c MP |
7760 | /* cp_tree_equal looks through NOPs, so allow them. */ |
7761 | gcc_checking_assert (r == t | |
7762 | || CONVERT_EXPR_P (t) | |
7763 | || TREE_CODE (t) == VIEW_CONVERT_EXPR | |
7764 | || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) | |
7765 | || !cp_tree_equal (r, t)); | |
7766 | return r; | |
7767 | } | |
7768 | else if (TREE_OVERFLOW_P (t)) | |
7769 | { | |
7770 | t = build_nop (TREE_TYPE (t), t); | |
7771 | TREE_CONSTANT (t) = false; | |
7772 | } | |
7773 | ||
7774 | return t; | |
7775 | } | |
7776 | ||
234bef96 PC |
7777 | /* Like maybe_constant_value but first fully instantiate the argument. |
7778 | ||
7779 | Note: this is equivalent to instantiate_non_dependent_expr_sfinae | |
e56f6629 JM |
7780 | (t, complain) followed by maybe_constant_value but is more efficient, |
7781 | because it calls instantiation_dependent_expression_p and | |
7782 | potential_constant_expression at most once. | |
4cd3e7df | 7783 | The manifestly_const_eval argument is passed to maybe_constant_value. |
e56f6629 JM |
7784 | |
7785 | Callers should generally pass their active complain, or if they are in a | |
7786 | non-template, diagnosing context, they can use the default of | |
7787 | tf_warning_or_error. Callers that might be within a template context, don't | |
7788 | have a complain parameter, and aren't going to remember the result for long | |
7789 | (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node | |
7790 | appropriately. */ | |
234bef96 PC |
7791 | |
7792 | tree | |
e56f6629 | 7793 | fold_non_dependent_expr (tree t, |
4cd3e7df | 7794 | tsubst_flags_t complain /* = tf_warning_or_error */, |
f968ef9b JJ |
7795 | bool manifestly_const_eval /* = false */, |
7796 | tree object /* = NULL_TREE */) | |
234bef96 PC |
7797 | { |
7798 | if (t == NULL_TREE) | |
7799 | return NULL_TREE; | |
7800 | ||
a81c8e8c MP |
7801 | if (processing_template_decl) |
7802 | return fold_non_dependent_expr_template (t, complain, | |
f968ef9b | 7803 | manifestly_const_eval, object); |
234bef96 | 7804 | |
f968ef9b | 7805 | return maybe_constant_value (t, object, manifestly_const_eval); |
a81c8e8c | 7806 | } |
234bef96 | 7807 | |
ae2ebf01 MP |
7808 | /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant, |
7809 | return the original expression. */ | |
7810 | ||
7811 | tree | |
7812 | maybe_fold_non_dependent_expr (tree expr, | |
7813 | tsubst_flags_t complain/*=tf_warning_or_error*/) | |
7814 | { | |
7815 | tree t = fold_non_dependent_expr (expr, complain); | |
7816 | if (t && TREE_CONSTANT (t)) | |
7817 | return t; | |
7818 | ||
7819 | return expr; | |
7820 | } | |
234bef96 | 7821 | |
a81c8e8c | 7822 | /* Like maybe_constant_init but first fully instantiate the argument. */ |
234bef96 | 7823 | |
a81c8e8c MP |
7824 | tree |
7825 | fold_non_dependent_init (tree t, | |
7826 | tsubst_flags_t complain /*=tf_warning_or_error*/, | |
69bf1c7d MP |
7827 | bool manifestly_const_eval /*=false*/, |
7828 | tree object /* = NULL_TREE */) | |
a81c8e8c MP |
7829 | { |
7830 | if (t == NULL_TREE) | |
7831 | return NULL_TREE; | |
7832 | ||
7833 | if (processing_template_decl) | |
7834 | { | |
7835 | t = fold_non_dependent_expr_template (t, complain, | |
69bf1c7d | 7836 | manifestly_const_eval, object); |
a81c8e8c MP |
7837 | /* maybe_constant_init does this stripping, so do it here too. */ |
7838 | if (TREE_CODE (t) == TARGET_EXPR) | |
234bef96 | 7839 | { |
a81c8e8c MP |
7840 | tree init = TARGET_EXPR_INITIAL (t); |
7841 | if (TREE_CODE (init) == CONSTRUCTOR) | |
7842 | t = init; | |
234bef96 PC |
7843 | } |
7844 | return t; | |
7845 | } | |
7846 | ||
69bf1c7d | 7847 | return maybe_constant_init (t, object, manifestly_const_eval); |
234bef96 PC |
7848 | } |
7849 | ||
2d76680f | 7850 | /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather |
e4082611 JJ |
7851 | than wrapped in a TARGET_EXPR. |
7852 | ALLOW_NON_CONSTANT is false if T is required to be a constant expression. | |
13de99bc | 7853 | MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as |
e4082611 | 7854 | per P0595 even when ALLOW_NON_CONSTANT is true. */ |
2d76680f | 7855 | |
118cd6ba | 7856 | static tree |
e4082611 | 7857 | maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant, |
13de99bc | 7858 | bool manifestly_const_eval) |
2d76680f | 7859 | { |
cda0a029 JM |
7860 | if (!t) |
7861 | return t; | |
2d76680f PC |
7862 | if (TREE_CODE (t) == EXPR_STMT) |
7863 | t = TREE_OPERAND (t, 0); | |
7864 | if (TREE_CODE (t) == CONVERT_EXPR | |
7865 | && VOID_TYPE_P (TREE_TYPE (t))) | |
7866 | t = TREE_OPERAND (t, 0); | |
60813a46 JM |
7867 | if (TREE_CODE (t) == INIT_EXPR) |
7868 | t = TREE_OPERAND (t, 1); | |
f64e0c02 JM |
7869 | if (TREE_CODE (t) == TARGET_EXPR) |
7870 | t = TARGET_EXPR_INITIAL (t); | |
a0ab7ccd | 7871 | if (!is_nondependent_static_init_expression (t)) |
69eb4fde | 7872 | /* Don't try to evaluate it. */; |
118cd6ba | 7873 | else if (CONSTANT_CLASS_P (t) && allow_non_constant) |
8a87daca | 7874 | /* No evaluation needed. */; |
69eb4fde | 7875 | else |
e4082611 | 7876 | t = cxx_eval_outermost_constant_expr (t, allow_non_constant, |
38295573 | 7877 | /*strict*/false, |
8e007055 | 7878 | manifestly_const_eval, false, decl); |
2d76680f PC |
7879 | if (TREE_CODE (t) == TARGET_EXPR) |
7880 | { | |
7881 | tree init = TARGET_EXPR_INITIAL (t); | |
7882 | if (TREE_CODE (init) == CONSTRUCTOR) | |
7883 | t = init; | |
7884 | } | |
7885 | return t; | |
7886 | } | |
7887 | ||
118cd6ba MP |
7888 | /* Wrapper for maybe_constant_init_1 which permits non constants. */ |
7889 | ||
7890 | tree | |
13de99bc | 7891 | maybe_constant_init (tree t, tree decl, bool manifestly_const_eval) |
118cd6ba | 7892 | { |
13de99bc | 7893 | return maybe_constant_init_1 (t, decl, true, manifestly_const_eval); |
118cd6ba MP |
7894 | } |
7895 | ||
7896 | /* Wrapper for maybe_constant_init_1 which does not permit non constants. */ | |
7897 | ||
7898 | tree | |
7899 | cxx_constant_init (tree t, tree decl) | |
7900 | { | |
e4082611 | 7901 | return maybe_constant_init_1 (t, decl, false, true); |
118cd6ba MP |
7902 | } |
7903 | ||
2d76680f PC |
7904 | #if 0 |
7905 | /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */ | |
7906 | /* Return true if the object referred to by REF has automatic or thread | |
7907 | local storage. */ | |
7908 | ||
7909 | enum { ck_ok, ck_bad, ck_unknown }; | |
7910 | static int | |
7911 | check_automatic_or_tls (tree ref) | |
7912 | { | |
ef4bddc2 | 7913 | machine_mode mode; |
f37fac2b | 7914 | poly_int64 bitsize, bitpos; |
2d76680f PC |
7915 | tree offset; |
7916 | int volatilep = 0, unsignedp = 0; | |
7917 | tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset, | |
7918 | &mode, &unsignedp, &volatilep, false); | |
7919 | duration_kind dk; | |
7920 | ||
7921 | /* If there isn't a decl in the middle, we don't know the linkage here, | |
7922 | and this isn't a constant expression anyway. */ | |
7923 | if (!DECL_P (decl)) | |
7924 | return ck_unknown; | |
7925 | dk = decl_storage_duration (decl); | |
7926 | return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok; | |
7927 | } | |
7928 | #endif | |
7929 | ||
c7a53bdb JJ |
7930 | /* Data structure for passing data from potential_constant_expression_1 |
7931 | to check_for_return_continue via cp_walk_tree. */ | |
7932 | struct check_for_return_continue_data { | |
7933 | hash_set<tree> *pset; | |
7934 | tree continue_stmt; | |
0fb7aa20 | 7935 | tree break_stmt; |
c7a53bdb JJ |
7936 | }; |
7937 | ||
7938 | /* Helper function for potential_constant_expression_1 SWITCH_STMT handling, | |
7939 | called through cp_walk_tree. Return the first RETURN_EXPR found, or note | |
0fb7aa20 | 7940 | the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */ |
c7a53bdb JJ |
7941 | static tree |
7942 | check_for_return_continue (tree *tp, int *walk_subtrees, void *data) | |
7943 | { | |
0fb7aa20 | 7944 | tree t = *tp, s, b; |
c7a53bdb JJ |
7945 | check_for_return_continue_data *d = (check_for_return_continue_data *) data; |
7946 | switch (TREE_CODE (t)) | |
7947 | { | |
7948 | case RETURN_EXPR: | |
7949 | return t; | |
7950 | ||
7951 | case CONTINUE_STMT: | |
7952 | if (d->continue_stmt == NULL_TREE) | |
7953 | d->continue_stmt = t; | |
7954 | break; | |
7955 | ||
0fb7aa20 JJ |
7956 | case BREAK_STMT: |
7957 | if (d->break_stmt == NULL_TREE) | |
7958 | d->break_stmt = t; | |
7959 | break; | |
7960 | ||
c7a53bdb JJ |
7961 | #define RECUR(x) \ |
7962 | if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \ | |
7963 | d->pset)) \ | |
7964 | return r | |
7965 | ||
7966 | /* For loops, walk subtrees manually, so that continue stmts found | |
7967 | inside of the bodies of the loops are ignored. */ | |
7968 | case DO_STMT: | |
7969 | *walk_subtrees = 0; | |
7970 | RECUR (DO_COND (t)); | |
7971 | s = d->continue_stmt; | |
0fb7aa20 | 7972 | b = d->break_stmt; |
c7a53bdb JJ |
7973 | RECUR (DO_BODY (t)); |
7974 | d->continue_stmt = s; | |
0fb7aa20 | 7975 | d->break_stmt = b; |
c7a53bdb JJ |
7976 | break; |
7977 | ||
7978 | case WHILE_STMT: | |
7979 | *walk_subtrees = 0; | |
7980 | RECUR (WHILE_COND (t)); | |
7981 | s = d->continue_stmt; | |
0fb7aa20 | 7982 | b = d->break_stmt; |
c7a53bdb JJ |
7983 | RECUR (WHILE_BODY (t)); |
7984 | d->continue_stmt = s; | |
0fb7aa20 | 7985 | d->break_stmt = b; |
c7a53bdb JJ |
7986 | break; |
7987 | ||
7988 | case FOR_STMT: | |
7989 | *walk_subtrees = 0; | |
7990 | RECUR (FOR_INIT_STMT (t)); | |
7991 | RECUR (FOR_COND (t)); | |
7992 | RECUR (FOR_EXPR (t)); | |
7993 | s = d->continue_stmt; | |
0fb7aa20 | 7994 | b = d->break_stmt; |
c7a53bdb JJ |
7995 | RECUR (FOR_BODY (t)); |
7996 | d->continue_stmt = s; | |
0fb7aa20 | 7997 | d->break_stmt = b; |
c7a53bdb JJ |
7998 | break; |
7999 | ||
8000 | case RANGE_FOR_STMT: | |
8001 | *walk_subtrees = 0; | |
8002 | RECUR (RANGE_FOR_EXPR (t)); | |
8003 | s = d->continue_stmt; | |
0fb7aa20 | 8004 | b = d->break_stmt; |
c7a53bdb JJ |
8005 | RECUR (RANGE_FOR_BODY (t)); |
8006 | d->continue_stmt = s; | |
0fb7aa20 JJ |
8007 | d->break_stmt = b; |
8008 | break; | |
8009 | ||
8010 | case SWITCH_STMT: | |
8011 | *walk_subtrees = 0; | |
8012 | RECUR (SWITCH_STMT_COND (t)); | |
8013 | b = d->break_stmt; | |
8014 | RECUR (SWITCH_STMT_BODY (t)); | |
8015 | d->break_stmt = b; | |
c7a53bdb JJ |
8016 | break; |
8017 | #undef RECUR | |
8018 | ||
8019 | case STATEMENT_LIST: | |
8020 | case CONSTRUCTOR: | |
8021 | break; | |
8022 | ||
8023 | default: | |
8024 | if (!EXPR_P (t)) | |
8025 | *walk_subtrees = 0; | |
8026 | break; | |
8027 | } | |
8028 | ||
8029 | return NULL_TREE; | |
8030 | } | |
8031 | ||
2d76680f PC |
8032 | /* Return true if T denotes a potentially constant expression. Issue |
8033 | diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true, | |
a0ab7ccd JM |
8034 | an lvalue-rvalue conversion is implied. If NOW is true, we want to |
8035 | consider the expression in the current context, independent of constexpr | |
8036 | substitution. | |
2d76680f PC |
8037 | |
8038 | C++0x [expr.const] used to say | |
8039 | ||
8040 | 6 An expression is a potential constant expression if it is | |
8041 | a constant expression where all occurrences of function | |
8042 | parameters are replaced by arbitrary constant expressions | |
8043 | of the appropriate type. | |
8044 | ||
8045 | 2 A conditional expression is a constant expression unless it | |
8046 | involves one of the following as a potentially evaluated | |
8047 | subexpression (3.2), but subexpressions of logical AND (5.14), | |
8048 | logical OR (5.15), and conditional (5.16) operations that are | |
8049 | not evaluated are not considered. */ | |
8050 | ||
8051 | static bool | |
a0ab7ccd | 8052 | potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, |
3075affd | 8053 | tsubst_flags_t flags, tree *jump_target) |
2d76680f | 8054 | { |
a0ab7ccd | 8055 | #define RECUR(T,RV) \ |
3075affd | 8056 | potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target) |
a0ab7ccd | 8057 | |
2d76680f PC |
8058 | enum { any = false, rval = true }; |
8059 | int i; | |
8060 | tree tmp; | |
8061 | ||
8062 | if (t == error_mark_node) | |
8063 | return false; | |
8064 | if (t == NULL_TREE) | |
8065 | return true; | |
f9d0ca40 | 8066 | location_t loc = cp_expr_loc_or_input_loc (t); |
3075affd JM |
8067 | |
8068 | if (*jump_target) | |
8069 | /* If we are jumping, ignore everything. This is simpler than the | |
8070 | cxx_eval_constant_expression handling because we only need to be | |
8071 | conservatively correct, and we don't necessarily have a constant value | |
8072 | available, so we don't bother with switch tracking. */ | |
8073 | return true; | |
8074 | ||
3c393a2c | 8075 | if (TREE_THIS_VOLATILE (t) && want_rval) |
2d76680f PC |
8076 | { |
8077 | if (flags & tf_error) | |
3c393a2c MP |
8078 | error_at (loc, "lvalue-to-rvalue conversion of a volatile lvalue " |
8079 | "%qE with type %qT", t, TREE_TYPE (t)); | |
2d76680f PC |
8080 | return false; |
8081 | } | |
8082 | if (CONSTANT_CLASS_P (t)) | |
8083 | return true; | |
7dc2b4a2 JM |
8084 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED) |
8085 | && TREE_TYPE (t) == error_mark_node) | |
8086 | return false; | |
2d76680f PC |
8087 | |
8088 | switch (TREE_CODE (t)) | |
8089 | { | |
8090 | case FUNCTION_DECL: | |
8091 | case BASELINK: | |
8092 | case TEMPLATE_DECL: | |
8093 | case OVERLOAD: | |
8094 | case TEMPLATE_ID_EXPR: | |
8095 | case LABEL_DECL: | |
60813a46 | 8096 | case CASE_LABEL_EXPR: |
81012684 | 8097 | case PREDICT_EXPR: |
2d76680f PC |
8098 | case CONST_DECL: |
8099 | case SIZEOF_EXPR: | |
8100 | case ALIGNOF_EXPR: | |
8101 | case OFFSETOF_EXPR: | |
8102 | case NOEXCEPT_EXPR: | |
8103 | case TEMPLATE_PARM_INDEX: | |
8104 | case TRAIT_EXPR: | |
8105 | case IDENTIFIER_NODE: | |
8106 | case USERDEF_LITERAL: | |
8107 | /* We can see a FIELD_DECL in a pointer-to-member expression. */ | |
8108 | case FIELD_DECL: | |
cda0a029 | 8109 | case RESULT_DECL: |
2d76680f | 8110 | case USING_DECL: |
60813a46 | 8111 | case USING_STMT: |
3e605b20 | 8112 | case PLACEHOLDER_EXPR: |
971e17ff | 8113 | case REQUIRES_EXPR: |
98e5a19a | 8114 | case STATIC_ASSERT: |
96a95ac1 | 8115 | case DEBUG_BEGIN_STMT: |
2d76680f PC |
8116 | return true; |
8117 | ||
3075affd JM |
8118 | case RETURN_EXPR: |
8119 | if (!RECUR (TREE_OPERAND (t, 0), any)) | |
8120 | return false; | |
8121 | /* FALLTHROUGH */ | |
8122 | ||
8123 | case BREAK_STMT: | |
8124 | case CONTINUE_STMT: | |
8125 | *jump_target = t; | |
8126 | return true; | |
8127 | ||
a0ab7ccd | 8128 | case PARM_DECL: |
8fda2c27 | 8129 | if (now && want_rval) |
a0ab7ccd | 8130 | { |
8fda2c27 | 8131 | tree type = TREE_TYPE (t); |
9eb7d0d7 | 8132 | if ((processing_template_decl && !COMPLETE_TYPE_P (type)) |
61466206 | 8133 | || dependent_type_p (type) |
8fda2c27 JM |
8134 | || is_really_empty_class (type, /*ignore_vptr*/false)) |
8135 | /* An empty class has no data to read. */ | |
8136 | return true; | |
a0ab7ccd JM |
8137 | if (flags & tf_error) |
8138 | error ("%qE is not a constant expression", t); | |
8139 | return false; | |
8140 | } | |
8141 | return true; | |
8142 | ||
2d76680f PC |
8143 | case AGGR_INIT_EXPR: |
8144 | case CALL_EXPR: | |
8145 | /* -- an invocation of a function other than a constexpr function | |
8146 | or a constexpr constructor. */ | |
8147 | { | |
8148 | tree fun = get_function_named_in_call (t); | |
8149 | const int nargs = call_expr_nargs (t); | |
8150 | i = 0; | |
8151 | ||
60813a46 JM |
8152 | if (fun == NULL_TREE) |
8153 | { | |
44a845ca MS |
8154 | /* Reset to allow the function to continue past the end |
8155 | of the block below. Otherwise return early. */ | |
8156 | bool bail = true; | |
8157 | ||
35228ac7 JJ |
8158 | if (TREE_CODE (t) == CALL_EXPR |
8159 | && CALL_EXPR_FN (t) == NULL_TREE) | |
8160 | switch (CALL_EXPR_IFN (t)) | |
8161 | { | |
8162 | /* These should be ignored, they are optimized away from | |
8163 | constexpr functions. */ | |
8164 | case IFN_UBSAN_NULL: | |
8165 | case IFN_UBSAN_BOUNDS: | |
8166 | case IFN_UBSAN_VPTR: | |
81fea426 | 8167 | case IFN_FALLTHROUGH: |
35228ac7 | 8168 | return true; |
44a845ca MS |
8169 | |
8170 | case IFN_ADD_OVERFLOW: | |
8171 | case IFN_SUB_OVERFLOW: | |
8172 | case IFN_MUL_OVERFLOW: | |
e16f1cc7 | 8173 | case IFN_LAUNDER: |
d8fcab68 | 8174 | case IFN_VEC_CONVERT: |
44a845ca | 8175 | bail = false; |
d8fcab68 | 8176 | break; |
44a845ca | 8177 | |
35228ac7 JJ |
8178 | default: |
8179 | break; | |
8180 | } | |
44a845ca MS |
8181 | |
8182 | if (bail) | |
8183 | { | |
8184 | /* fold_call_expr can't do anything with IFN calls. */ | |
8185 | if (flags & tf_error) | |
e40b6fc7 | 8186 | error_at (loc, "call to internal function %qE", t); |
44a845ca MS |
8187 | return false; |
8188 | } | |
60813a46 | 8189 | } |
44a845ca MS |
8190 | |
8191 | if (fun && is_overloaded_fn (fun)) | |
2d76680f PC |
8192 | { |
8193 | if (TREE_CODE (fun) == FUNCTION_DECL) | |
8194 | { | |
8195 | if (builtin_valid_in_constant_expr_p (fun)) | |
8196 | return true; | |
8197 | if (!DECL_DECLARED_CONSTEXPR_P (fun) | |
8198 | /* Allow any built-in function; if the expansion | |
8199 | isn't constant, we'll deal with that then. */ | |
8e007055 | 8200 | && !fndecl_built_in_p (fun) |
b04445d4 | 8201 | /* In C++20, replaceable global allocation functions |
8e007055 | 8202 | are constant expressions. */ |
8412b939 JJ |
8203 | && (!cxx_replaceable_global_alloc_fn (fun) |
8204 | || TREE_CODE (t) != CALL_EXPR | |
8205 | || (!CALL_FROM_NEW_OR_DELETE_P (t) | |
8206 | && (current_function_decl == NULL_TREE | |
8207 | || !is_std_allocator_allocate | |
8208 | (current_function_decl)))) | |
cf650568 JJ |
8209 | /* Allow placement new in std::construct_at. */ |
8210 | && (!cxx_placement_new_fn (fun) | |
8412b939 | 8211 | || TREE_CODE (t) != CALL_EXPR |
cf650568 | 8212 | || current_function_decl == NULL_TREE |
22edf943 MP |
8213 | || !is_std_construct_at (current_function_decl)) |
8214 | && !cxx_dynamic_cast_fn_p (fun)) | |
2d76680f PC |
8215 | { |
8216 | if (flags & tf_error) | |
8217 | { | |
84fa214d | 8218 | error_at (loc, "call to non-%<constexpr%> function %qD", |
e40b6fc7 | 8219 | fun); |
2d76680f PC |
8220 | explain_invalid_constexpr_fn (fun); |
8221 | } | |
8222 | return false; | |
8223 | } | |
8224 | /* A call to a non-static member function takes the address | |
8225 | of the object as the first argument. But in a constant | |
8226 | expression the address will be folded away, so look | |
8227 | through it now. */ | |
8228 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) | |
8229 | && !DECL_CONSTRUCTOR_P (fun)) | |
8230 | { | |
8231 | tree x = get_nth_callarg (t, 0); | |
8232 | if (is_this_parameter (x)) | |
3e605b20 | 8233 | return true; |
a0ab7ccd JM |
8234 | /* Don't require an immediately constant value, as |
8235 | constexpr substitution might not use the value. */ | |
8236 | bool sub_now = false; | |
8237 | if (!potential_constant_expression_1 (x, rval, strict, | |
3075affd JM |
8238 | sub_now, flags, |
8239 | jump_target)) | |
2d76680f PC |
8240 | return false; |
8241 | i = 1; | |
8242 | } | |
8243 | } | |
8244 | else | |
8245 | { | |
69eb4fde | 8246 | if (!RECUR (fun, true)) |
2d76680f PC |
8247 | return false; |
8248 | fun = get_first_fn (fun); | |
8249 | } | |
8250 | /* Skip initial arguments to base constructors. */ | |
8251 | if (DECL_BASE_CONSTRUCTOR_P (fun)) | |
8252 | i = num_artificial_parms_for (fun); | |
8253 | fun = DECL_ORIGIN (fun); | |
8254 | } | |
44a845ca | 8255 | else if (fun) |
2d76680f | 8256 | { |
69eb4fde | 8257 | if (RECUR (fun, rval)) |
2d76680f PC |
8258 | /* Might end up being a constant function pointer. */; |
8259 | else | |
8260 | return false; | |
8261 | } | |
8262 | for (; i < nargs; ++i) | |
8263 | { | |
8264 | tree x = get_nth_callarg (t, i); | |
c3c29ba5 JM |
8265 | /* In a template, reference arguments haven't been converted to |
8266 | REFERENCE_TYPE and we might not even know if the parameter | |
8267 | is a reference, so accept lvalue constants too. */ | |
8268 | bool rv = processing_template_decl ? any : rval; | |
a0ab7ccd JM |
8269 | /* Don't require an immediately constant value, as constexpr |
8270 | substitution might not use the value of the argument. */ | |
8271 | bool sub_now = false; | |
8272 | if (!potential_constant_expression_1 (x, rv, strict, | |
3075affd | 8273 | sub_now, flags, jump_target)) |
2d76680f PC |
8274 | return false; |
8275 | } | |
8276 | return true; | |
8277 | } | |
8278 | ||
8279 | case NON_LVALUE_EXPR: | |
8280 | /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to | |
8281 | -- an lvalue of integral type that refers to a non-volatile | |
8282 | const variable or static data member initialized with | |
8283 | constant expressions, or | |
8284 | ||
8285 | -- an lvalue of literal type that refers to non-volatile | |
8286 | object defined with constexpr, or that refers to a | |
8287 | sub-object of such an object; */ | |
69eb4fde | 8288 | return RECUR (TREE_OPERAND (t, 0), rval); |
2d76680f PC |
8289 | |
8290 | case VAR_DECL: | |
70f40fea | 8291 | if (DECL_HAS_VALUE_EXPR_P (t)) |
c1051bf7 | 8292 | { |
68ad1bf7 | 8293 | if (now && is_normal_capture_proxy (t)) |
c1051bf7 JM |
8294 | { |
8295 | /* -- in a lambda-expression, a reference to this or to a | |
8296 | variable with automatic storage duration defined outside that | |
8297 | lambda-expression, where the reference would be an | |
8298 | odr-use. */ | |
04b89192 JM |
8299 | |
8300 | if (want_rval) | |
8301 | /* Since we're doing an lvalue-rvalue conversion, this might | |
8302 | not be an odr-use, so evaluate the variable directly. */ | |
8303 | return RECUR (DECL_CAPTURED_VARIABLE (t), rval); | |
8304 | ||
c1051bf7 JM |
8305 | if (flags & tf_error) |
8306 | { | |
8307 | tree cap = DECL_CAPTURED_VARIABLE (t); | |
8308 | error ("lambda capture of %qE is not a constant expression", | |
8309 | cap); | |
04b89192 | 8310 | if (decl_constant_var_p (cap)) |
c1051bf7 JM |
8311 | inform (input_location, "because it is used as a glvalue"); |
8312 | } | |
8313 | return false; | |
8314 | } | |
c2856cee MP |
8315 | /* Treat __PRETTY_FUNCTION__ inside a template function as |
8316 | potentially-constant. */ | |
8317 | else if (DECL_PRETTY_FUNCTION_P (t) | |
8318 | && DECL_VALUE_EXPR (t) == error_mark_node) | |
8319 | return true; | |
c1051bf7 JM |
8320 | return RECUR (DECL_VALUE_EXPR (t), rval); |
8321 | } | |
69eb4fde | 8322 | if (want_rval |
98e5a19a | 8323 | && !var_in_maybe_constexpr_fn (t) |
7dc2b4a2 | 8324 | && !type_dependent_expression_p (t) |
4b691b13 | 8325 | && !decl_maybe_constant_var_p (t) |
69eb4fde JM |
8326 | && (strict |
8327 | || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t)) | |
4b691b13 JM |
8328 | || (DECL_INITIAL (t) |
8329 | && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))) | |
7dc2b4a2 | 8330 | && COMPLETE_TYPE_P (TREE_TYPE (t)) |
dbcd32f8 | 8331 | && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
2d76680f PC |
8332 | { |
8333 | if (flags & tf_error) | |
6821245b | 8334 | non_const_var_error (loc, t); |
2d76680f PC |
8335 | return false; |
8336 | } | |
8337 | return true; | |
8338 | ||
8339 | case NOP_EXPR: | |
ed3ea9f2 JJ |
8340 | if (REINTERPRET_CAST_P (t)) |
8341 | { | |
8342 | if (flags & tf_error) | |
a9c697b8 | 8343 | error_at (loc, "%<reinterpret_cast%> is not a constant expression"); |
ed3ea9f2 JJ |
8344 | return false; |
8345 | } | |
8346 | /* FALLTHRU */ | |
2d76680f PC |
8347 | case CONVERT_EXPR: |
8348 | case VIEW_CONVERT_EXPR: | |
8349 | /* -- a reinterpret_cast. FIXME not implemented, and this rule | |
8350 | may change to something more specific to type-punning (DR 1312). */ | |
8351 | { | |
8352 | tree from = TREE_OPERAND (t, 0); | |
dfd7fdca DM |
8353 | if (location_wrapper_p (t)) |
8354 | return (RECUR (from, want_rval)); | |
8355 | if (INDIRECT_TYPE_P (TREE_TYPE (t))) | |
2d76680f | 8356 | { |
dfd7fdca DM |
8357 | STRIP_ANY_LOCATION_WRAPPER (from); |
8358 | if (TREE_CODE (from) == INTEGER_CST | |
8359 | && !integer_zerop (from)) | |
8360 | { | |
8361 | if (flags & tf_error) | |
a9c697b8 MS |
8362 | error_at (loc, |
8363 | "%<reinterpret_cast%> from integer to pointer"); | |
dfd7fdca DM |
8364 | return false; |
8365 | } | |
2d76680f | 8366 | } |
69eb4fde | 8367 | return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR)); |
2d76680f PC |
8368 | } |
8369 | ||
be845b04 JJ |
8370 | case ADDRESSOF_EXPR: |
8371 | /* This is like ADDR_EXPR, except it won't form pointer-to-member. */ | |
8372 | t = TREE_OPERAND (t, 0); | |
8373 | goto handle_addr_expr; | |
8374 | ||
2d76680f PC |
8375 | case ADDR_EXPR: |
8376 | /* -- a unary operator & that is applied to an lvalue that | |
8377 | designates an object with thread or automatic storage | |
8378 | duration; */ | |
8379 | t = TREE_OPERAND (t, 0); | |
8380 | ||
8381 | if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t)) | |
8382 | /* A pointer-to-member constant. */ | |
8383 | return true; | |
8384 | ||
be845b04 | 8385 | handle_addr_expr: |
2d76680f PC |
8386 | #if 0 |
8387 | /* FIXME adjust when issue 1197 is fully resolved. For now don't do | |
8388 | any checking here, as we might dereference the pointer later. If | |
8389 | we remove this code, also remove check_automatic_or_tls. */ | |
8390 | i = check_automatic_or_tls (t); | |
8391 | if (i == ck_ok) | |
8392 | return true; | |
8393 | if (i == ck_bad) | |
8394 | { | |
8395 | if (flags & tf_error) | |
8396 | error ("address-of an object %qE with thread local or " | |
8397 | "automatic storage is not a constant expression", t); | |
8398 | return false; | |
8399 | } | |
8400 | #endif | |
69eb4fde | 8401 | return RECUR (t, any); |
2d76680f PC |
8402 | |
8403 | case COMPONENT_REF: | |
2d76680f PC |
8404 | case ARROW_EXPR: |
8405 | case OFFSET_REF: | |
8406 | /* -- a class member access unless its postfix-expression is | |
8407 | of literal type or of pointer to literal type. */ | |
8408 | /* This test would be redundant, as it follows from the | |
8409 | postfix-expression being a potential constant expression. */ | |
19dedccf JM |
8410 | if (type_unknown_p (t)) |
8411 | return true; | |
8fda2c27 JM |
8412 | if (is_overloaded_fn (t)) |
8413 | /* In a template, a COMPONENT_REF of a function expresses ob.fn(), | |
8414 | which uses ob as an lvalue. */ | |
8415 | want_rval = false; | |
8416 | gcc_fallthrough (); | |
8417 | ||
8418 | case REALPART_EXPR: | |
8419 | case IMAGPART_EXPR: | |
8420 | case BIT_FIELD_REF: | |
69eb4fde | 8421 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
2d76680f PC |
8422 | |
8423 | case EXPR_PACK_EXPANSION: | |
69eb4fde | 8424 | return RECUR (PACK_EXPANSION_PATTERN (t), want_rval); |
2d76680f PC |
8425 | |
8426 | case INDIRECT_REF: | |
8427 | { | |
8428 | tree x = TREE_OPERAND (t, 0); | |
8429 | STRIP_NOPS (x); | |
948d0d91 | 8430 | if (is_this_parameter (x) && !is_capture_proxy (x)) |
2d76680f | 8431 | { |
5d4e573b | 8432 | if (!var_in_maybe_constexpr_fn (x)) |
2d76680f PC |
8433 | { |
8434 | if (flags & tf_error) | |
e40b6fc7 | 8435 | error_at (loc, "use of %<this%> in a constant expression"); |
2d76680f PC |
8436 | return false; |
8437 | } | |
2d76680f PC |
8438 | return true; |
8439 | } | |
69eb4fde | 8440 | return RECUR (x, rval); |
2d76680f PC |
8441 | } |
8442 | ||
60813a46 | 8443 | case STATEMENT_LIST: |
0f54cc9c JM |
8444 | for (tree stmt : tsi_range (t)) |
8445 | if (!RECUR (stmt, any)) | |
8446 | return false; | |
8447 | return true; | |
60813a46 JM |
8448 | |
8449 | case MODIFY_EXPR: | |
8450 | if (cxx_dialect < cxx14) | |
8451 | goto fail; | |
69eb4fde | 8452 | if (!RECUR (TREE_OPERAND (t, 0), any)) |
60813a46 | 8453 | return false; |
8e007055 JJ |
8454 | /* Just ignore clobbers. */ |
8455 | if (TREE_CLOBBER_P (TREE_OPERAND (t, 1))) | |
8456 | return true; | |
69eb4fde | 8457 | if (!RECUR (TREE_OPERAND (t, 1), rval)) |
60813a46 JM |
8458 | return false; |
8459 | return true; | |
8460 | ||
8461 | case MODOP_EXPR: | |
8462 | if (cxx_dialect < cxx14) | |
8463 | goto fail; | |
69eb4fde | 8464 | if (!RECUR (TREE_OPERAND (t, 0), rval)) |
60813a46 | 8465 | return false; |
69eb4fde | 8466 | if (!RECUR (TREE_OPERAND (t, 2), rval)) |
60813a46 JM |
8467 | return false; |
8468 | return true; | |
8469 | ||
60813a46 | 8470 | case DO_STMT: |
69eb4fde | 8471 | if (!RECUR (DO_COND (t), rval)) |
60813a46 | 8472 | return false; |
69eb4fde | 8473 | if (!RECUR (DO_BODY (t), any)) |
60813a46 | 8474 | return false; |
3075affd JM |
8475 | if (breaks (jump_target) || continues (jump_target)) |
8476 | *jump_target = NULL_TREE; | |
60813a46 JM |
8477 | return true; |
8478 | ||
8479 | case FOR_STMT: | |
69eb4fde | 8480 | if (!RECUR (FOR_INIT_STMT (t), any)) |
60813a46 | 8481 | return false; |
8e9558f0 MP |
8482 | tmp = FOR_COND (t); |
8483 | if (!RECUR (tmp, rval)) | |
60813a46 | 8484 | return false; |
8e9558f0 MP |
8485 | if (tmp) |
8486 | { | |
8487 | if (!processing_template_decl) | |
8488 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
a385474c MP |
8489 | /* If we couldn't evaluate the condition, it might not ever be |
8490 | true. */ | |
8491 | if (!integer_onep (tmp)) | |
0fb7aa20 JJ |
8492 | { |
8493 | /* Before returning true, check if the for body can contain | |
8494 | a return. */ | |
8495 | hash_set<tree> pset; | |
8496 | check_for_return_continue_data data = { &pset, NULL_TREE, | |
8497 | NULL_TREE }; | |
8498 | if (tree ret_expr | |
8499 | = cp_walk_tree (&FOR_BODY (t), check_for_return_continue, | |
8500 | &data, &pset)) | |
8501 | *jump_target = ret_expr; | |
8502 | return true; | |
8503 | } | |
8e9558f0 | 8504 | } |
69eb4fde | 8505 | if (!RECUR (FOR_EXPR (t), any)) |
60813a46 | 8506 | return false; |
69eb4fde | 8507 | if (!RECUR (FOR_BODY (t), any)) |
60813a46 | 8508 | return false; |
3075affd JM |
8509 | if (breaks (jump_target) || continues (jump_target)) |
8510 | *jump_target = NULL_TREE; | |
60813a46 JM |
8511 | return true; |
8512 | ||
98e5a19a | 8513 | case RANGE_FOR_STMT: |
8112667c MP |
8514 | if (!RECUR (RANGE_FOR_INIT_STMT (t), any)) |
8515 | return false; | |
98e5a19a JM |
8516 | if (!RECUR (RANGE_FOR_EXPR (t), any)) |
8517 | return false; | |
8518 | if (!RECUR (RANGE_FOR_BODY (t), any)) | |
8519 | return false; | |
3075affd JM |
8520 | if (breaks (jump_target) || continues (jump_target)) |
8521 | *jump_target = NULL_TREE; | |
98e5a19a JM |
8522 | return true; |
8523 | ||
60813a46 | 8524 | case WHILE_STMT: |
8e9558f0 MP |
8525 | tmp = WHILE_COND (t); |
8526 | if (!RECUR (tmp, rval)) | |
60813a46 | 8527 | return false; |
8e9558f0 MP |
8528 | if (!processing_template_decl) |
8529 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
a385474c MP |
8530 | /* If we couldn't evaluate the condition, it might not ever be true. */ |
8531 | if (!integer_onep (tmp)) | |
0fb7aa20 JJ |
8532 | { |
8533 | /* Before returning true, check if the while body can contain | |
8534 | a return. */ | |
8535 | hash_set<tree> pset; | |
8536 | check_for_return_continue_data data = { &pset, NULL_TREE, | |
8537 | NULL_TREE }; | |
8538 | if (tree ret_expr | |
8539 | = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue, | |
8540 | &data, &pset)) | |
8541 | *jump_target = ret_expr; | |
8542 | return true; | |
8543 | } | |
69eb4fde | 8544 | if (!RECUR (WHILE_BODY (t), any)) |
60813a46 | 8545 | return false; |
3075affd JM |
8546 | if (breaks (jump_target) || continues (jump_target)) |
8547 | *jump_target = NULL_TREE; | |
60813a46 JM |
8548 | return true; |
8549 | ||
8550 | case SWITCH_STMT: | |
69eb4fde | 8551 | if (!RECUR (SWITCH_STMT_COND (t), rval)) |
60813a46 | 8552 | return false; |
ce965730 | 8553 | /* FIXME we don't check SWITCH_STMT_BODY currently, because even |
c7a53bdb JJ |
8554 | unreachable labels would be checked and it is enough if there is |
8555 | a single switch cond value for which it is a valid constant | |
8556 | expression. We need to check if there are any RETURN_EXPRs | |
8557 | or CONTINUE_STMTs inside of the body though, as in that case | |
8558 | we need to set *jump_target. */ | |
8559 | else | |
8560 | { | |
8561 | hash_set<tree> pset; | |
0fb7aa20 JJ |
8562 | check_for_return_continue_data data = { &pset, NULL_TREE, |
8563 | NULL_TREE }; | |
c7a53bdb JJ |
8564 | if (tree ret_expr |
8565 | = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue, | |
8566 | &data, &pset)) | |
8567 | /* The switch might return. */ | |
8568 | *jump_target = ret_expr; | |
8569 | else if (data.continue_stmt) | |
8570 | /* The switch can't return, but might continue. */ | |
8571 | *jump_target = data.continue_stmt; | |
8572 | } | |
60813a46 JM |
8573 | return true; |
8574 | ||
58611fb6 | 8575 | case STMT_EXPR: |
69eb4fde | 8576 | return RECUR (STMT_EXPR_STMT (t), rval); |
58611fb6 | 8577 | |
2d76680f | 8578 | case LAMBDA_EXPR: |
899ac3b8 JM |
8579 | if (cxx_dialect >= cxx17) |
8580 | /* In C++17 lambdas can be constexpr, don't give up yet. */ | |
8581 | return true; | |
8582 | else if (flags & tf_error) | |
8583 | error_at (loc, "lambda-expression is not a constant expression " | |
8584 | "before C++17"); | |
8585 | return false; | |
8586 | ||
2d76680f PC |
8587 | case DYNAMIC_CAST_EXPR: |
8588 | case PSEUDO_DTOR_EXPR: | |
2d76680f PC |
8589 | case NEW_EXPR: |
8590 | case VEC_NEW_EXPR: | |
8591 | case DELETE_EXPR: | |
8592 | case VEC_DELETE_EXPR: | |
8593 | case THROW_EXPR: | |
e40b6fc7 JJ |
8594 | case OMP_PARALLEL: |
8595 | case OMP_TASK: | |
8596 | case OMP_FOR: | |
897064e2 | 8597 | case OMP_SIMD: |
e40b6fc7 JJ |
8598 | case OMP_DISTRIBUTE: |
8599 | case OMP_TASKLOOP: | |
d81ab49d | 8600 | case OMP_LOOP: |
e40b6fc7 JJ |
8601 | case OMP_TEAMS: |
8602 | case OMP_TARGET_DATA: | |
8603 | case OMP_TARGET: | |
8604 | case OMP_SECTIONS: | |
8605 | case OMP_ORDERED: | |
8606 | case OMP_CRITICAL: | |
8607 | case OMP_SINGLE: | |
8608 | case OMP_SECTION: | |
8609 | case OMP_MASTER: | |
8610 | case OMP_TASKGROUP: | |
8611 | case OMP_TARGET_UPDATE: | |
8612 | case OMP_TARGET_ENTER_DATA: | |
8613 | case OMP_TARGET_EXIT_DATA: | |
2d76680f PC |
8614 | case OMP_ATOMIC: |
8615 | case OMP_ATOMIC_READ: | |
8616 | case OMP_ATOMIC_CAPTURE_OLD: | |
8617 | case OMP_ATOMIC_CAPTURE_NEW: | |
28567c40 | 8618 | case OMP_DEPOBJ: |
e40b6fc7 JJ |
8619 | case OACC_PARALLEL: |
8620 | case OACC_KERNELS: | |
62aee289 | 8621 | case OACC_SERIAL: |
e40b6fc7 JJ |
8622 | case OACC_DATA: |
8623 | case OACC_HOST_DATA: | |
8624 | case OACC_LOOP: | |
8625 | case OACC_CACHE: | |
8626 | case OACC_DECLARE: | |
8627 | case OACC_ENTER_DATA: | |
8628 | case OACC_EXIT_DATA: | |
8629 | case OACC_UPDATE: | |
2d76680f PC |
8630 | /* GCC internal stuff. */ |
8631 | case VA_ARG_EXPR: | |
2d76680f | 8632 | case TRANSACTION_EXPR: |
81b6a6c5 | 8633 | case AT_ENCODE_EXPR: |
60813a46 | 8634 | fail: |
2d76680f | 8635 | if (flags & tf_error) |
e40b6fc7 | 8636 | error_at (loc, "expression %qE is not a constant expression", t); |
2d76680f PC |
8637 | return false; |
8638 | ||
529bc410 | 8639 | case ASM_EXPR: |
7c814975 MP |
8640 | if (flags & tf_error) |
8641 | inline_asm_in_constexpr_error (loc); | |
8642 | return false; | |
529bc410 | 8643 | |
bf8d8309 | 8644 | case OBJ_TYPE_REF: |
b04445d4 JM |
8645 | if (cxx_dialect >= cxx20) |
8646 | /* In C++20 virtual calls can be constexpr, don't give up yet. */ | |
bf8d8309 MP |
8647 | return true; |
8648 | else if (flags & tf_error) | |
a9c697b8 | 8649 | error_at (loc, |
b04445d4 | 8650 | "virtual functions cannot be %<constexpr%> before C++20"); |
bf8d8309 MP |
8651 | return false; |
8652 | ||
2d76680f | 8653 | case TYPEID_EXPR: |
66acfb80 MP |
8654 | /* In C++20, a typeid expression whose operand is of polymorphic |
8655 | class type can be constexpr. */ | |
2d76680f PC |
8656 | { |
8657 | tree e = TREE_OPERAND (t, 0); | |
b04445d4 | 8658 | if (cxx_dialect < cxx20 |
66acfb80 MP |
8659 | && strict |
8660 | && !TYPE_P (e) | |
8661 | && !type_dependent_expression_p (e) | |
2d76680f PC |
8662 | && TYPE_POLYMORPHIC_P (TREE_TYPE (e))) |
8663 | { | |
8664 | if (flags & tf_error) | |
a9c697b8 | 8665 | error_at (loc, "%<typeid%> is not a constant expression " |
e40b6fc7 | 8666 | "because %qE is of polymorphic type", e); |
2d76680f PC |
8667 | return false; |
8668 | } | |
8669 | return true; | |
8670 | } | |
8671 | ||
1af4ebf5 | 8672 | case POINTER_DIFF_EXPR: |
2d76680f | 8673 | case MINUS_EXPR: |
2d76680f PC |
8674 | want_rval = true; |
8675 | goto binary; | |
8676 | ||
8677 | case LT_EXPR: | |
8678 | case LE_EXPR: | |
8679 | case GT_EXPR: | |
8680 | case GE_EXPR: | |
8681 | case EQ_EXPR: | |
8682 | case NE_EXPR: | |
b7689b96 | 8683 | case SPACESHIP_EXPR: |
2d76680f PC |
8684 | want_rval = true; |
8685 | goto binary; | |
8686 | ||
60813a46 JM |
8687 | case PREINCREMENT_EXPR: |
8688 | case POSTINCREMENT_EXPR: | |
8689 | case PREDECREMENT_EXPR: | |
8690 | case POSTDECREMENT_EXPR: | |
8691 | if (cxx_dialect < cxx14) | |
8692 | goto fail; | |
8693 | goto unary; | |
8694 | ||
2d76680f PC |
8695 | case BIT_NOT_EXPR: |
8696 | /* A destructor. */ | |
8697 | if (TYPE_P (TREE_OPERAND (t, 0))) | |
8698 | return true; | |
191816a3 | 8699 | /* fall through. */ |
2d76680f | 8700 | |
2d76680f PC |
8701 | case CONJ_EXPR: |
8702 | case SAVE_EXPR: | |
8703 | case FIX_TRUNC_EXPR: | |
8704 | case FLOAT_EXPR: | |
8705 | case NEGATE_EXPR: | |
8706 | case ABS_EXPR: | |
e197e64e | 8707 | case ABSU_EXPR: |
2d76680f PC |
8708 | case TRUTH_NOT_EXPR: |
8709 | case FIXED_CONVERT_EXPR: | |
8710 | case UNARY_PLUS_EXPR: | |
4856a1f0 PC |
8711 | case UNARY_LEFT_FOLD_EXPR: |
8712 | case UNARY_RIGHT_FOLD_EXPR: | |
60813a46 | 8713 | unary: |
69eb4fde | 8714 | return RECUR (TREE_OPERAND (t, 0), rval); |
2d76680f PC |
8715 | |
8716 | case CAST_EXPR: | |
8717 | case CONST_CAST_EXPR: | |
8718 | case STATIC_CAST_EXPR: | |
8719 | case REINTERPRET_CAST_EXPR: | |
8720 | case IMPLICIT_CONV_EXPR: | |
8721 | if (cxx_dialect < cxx11 | |
8722 | && !dependent_type_p (TREE_TYPE (t)) | |
8723 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))) | |
8724 | /* In C++98, a conversion to non-integral type can't be part of a | |
8725 | constant expression. */ | |
8726 | { | |
8727 | if (flags & tf_error) | |
e40b6fc7 JJ |
8728 | error_at (loc, |
8729 | "cast to non-integral type %qT in a constant expression", | |
8730 | TREE_TYPE (t)); | |
2d76680f PC |
8731 | return false; |
8732 | } | |
fe0604d3 MP |
8733 | /* This might be a conversion from a class to a (potentially) literal |
8734 | type. Let's consider it potentially constant since the conversion | |
8735 | might be a constexpr user-defined conversion. */ | |
8736 | else if (cxx_dialect >= cxx11 | |
8737 | && (dependent_type_p (TREE_TYPE (t)) | |
8738 | || !COMPLETE_TYPE_P (TREE_TYPE (t)) | |
8739 | || literal_type_p (TREE_TYPE (t))) | |
8740 | && TREE_OPERAND (t, 0)) | |
8741 | { | |
8742 | tree type = TREE_TYPE (TREE_OPERAND (t, 0)); | |
8743 | /* If this is a dependent type, it could end up being a class | |
8744 | with conversions. */ | |
8745 | if (type == NULL_TREE || WILDCARD_TYPE_P (type)) | |
8746 | return true; | |
8747 | /* Or a non-dependent class which has conversions. */ | |
8748 | else if (CLASS_TYPE_P (type) | |
8749 | && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type))) | |
8750 | return true; | |
8751 | } | |
2d76680f | 8752 | |
69eb4fde | 8753 | return (RECUR (TREE_OPERAND (t, 0), |
9f613f06 | 8754 | !TYPE_REF_P (TREE_TYPE (t)))); |
2d76680f | 8755 | |
60813a46 | 8756 | case BIND_EXPR: |
69eb4fde | 8757 | return RECUR (BIND_EXPR_BODY (t), want_rval); |
60813a46 | 8758 | |
60813a46 JM |
8759 | case CLEANUP_POINT_EXPR: |
8760 | case MUST_NOT_THROW_EXPR: | |
8761 | case TRY_CATCH_EXPR: | |
769430b2 | 8762 | case TRY_BLOCK: |
60813a46 JM |
8763 | case EH_SPEC_BLOCK: |
8764 | case EXPR_STMT: | |
2d76680f PC |
8765 | case PAREN_EXPR: |
8766 | case NON_DEPENDENT_EXPR: | |
8767 | /* For convenience. */ | |
f937929e JM |
8768 | case LOOP_EXPR: |
8769 | case EXIT_EXPR: | |
69eb4fde | 8770 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
2d76680f | 8771 | |
98e5a19a JM |
8772 | case DECL_EXPR: |
8773 | tmp = DECL_EXPR_DECL (t); | |
8774 | if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)) | |
8775 | { | |
8892d532 | 8776 | if (CP_DECL_THREAD_LOCAL_P (tmp)) |
98e5a19a JM |
8777 | { |
8778 | if (flags & tf_error) | |
8779 | error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared " | |
8892d532 | 8780 | "%<thread_local%> in %<constexpr%> context", tmp); |
98e5a19a JM |
8781 | return false; |
8782 | } | |
8892d532 | 8783 | else if (TREE_STATIC (tmp)) |
98e5a19a JM |
8784 | { |
8785 | if (flags & tf_error) | |
8786 | error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared " | |
8892d532 | 8787 | "%<static%> in %<constexpr%> context", tmp); |
98e5a19a JM |
8788 | return false; |
8789 | } | |
3885527d PC |
8790 | else if (!check_for_uninitialized_const_var |
8791 | (tmp, /*constexpr_context_p=*/true, flags)) | |
8792 | return false; | |
98e5a19a JM |
8793 | } |
8794 | return RECUR (tmp, want_rval); | |
8795 | ||
769430b2 JM |
8796 | case TRY_FINALLY_EXPR: |
8797 | return (RECUR (TREE_OPERAND (t, 0), want_rval) | |
8798 | && RECUR (TREE_OPERAND (t, 1), any)); | |
8799 | ||
2d76680f | 8800 | case SCOPE_REF: |
69eb4fde | 8801 | return RECUR (TREE_OPERAND (t, 1), want_rval); |
2d76680f PC |
8802 | |
8803 | case TARGET_EXPR: | |
d3a9902e JM |
8804 | if (!TARGET_EXPR_DIRECT_INIT_P (t) |
8805 | && !literal_type_p (TREE_TYPE (t))) | |
2d76680f PC |
8806 | { |
8807 | if (flags & tf_error) | |
8808 | { | |
097f82ec | 8809 | auto_diagnostic_group d; |
e40b6fc7 JJ |
8810 | error_at (loc, "temporary of non-literal type %qT in a " |
8811 | "constant expression", TREE_TYPE (t)); | |
2d76680f PC |
8812 | explain_non_literal_class (TREE_TYPE (t)); |
8813 | } | |
8814 | return false; | |
8815 | } | |
191816a3 | 8816 | /* FALLTHRU */ |
2d76680f | 8817 | case INIT_EXPR: |
69eb4fde | 8818 | return RECUR (TREE_OPERAND (t, 1), rval); |
2d76680f PC |
8819 | |
8820 | case CONSTRUCTOR: | |
8821 | { | |
8822 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); | |
8823 | constructor_elt *ce; | |
8824 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
69eb4fde | 8825 | if (!RECUR (ce->value, want_rval)) |
2d76680f PC |
8826 | return false; |
8827 | return true; | |
8828 | } | |
8829 | ||
8830 | case TREE_LIST: | |
8831 | { | |
8832 | gcc_assert (TREE_PURPOSE (t) == NULL_TREE | |
8833 | || DECL_P (TREE_PURPOSE (t))); | |
69eb4fde | 8834 | if (!RECUR (TREE_VALUE (t), want_rval)) |
2d76680f PC |
8835 | return false; |
8836 | if (TREE_CHAIN (t) == NULL_TREE) | |
8837 | return true; | |
69eb4fde | 8838 | return RECUR (TREE_CHAIN (t), want_rval); |
2d76680f PC |
8839 | } |
8840 | ||
8841 | case TRUNC_DIV_EXPR: | |
8842 | case CEIL_DIV_EXPR: | |
8843 | case FLOOR_DIV_EXPR: | |
8844 | case ROUND_DIV_EXPR: | |
8845 | case TRUNC_MOD_EXPR: | |
8846 | case CEIL_MOD_EXPR: | |
8847 | case ROUND_MOD_EXPR: | |
8848 | { | |
8849 | tree denom = TREE_OPERAND (t, 1); | |
69eb4fde | 8850 | if (!RECUR (denom, rval)) |
2d76680f PC |
8851 | return false; |
8852 | /* We can't call cxx_eval_outermost_constant_expr on an expression | |
234bef96 | 8853 | that hasn't been through instantiate_non_dependent_expr yet. */ |
2d76680f PC |
8854 | if (!processing_template_decl) |
8855 | denom = cxx_eval_outermost_constant_expr (denom, true); | |
8856 | if (integer_zerop (denom)) | |
8857 | { | |
8858 | if (flags & tf_error) | |
64d6d399 | 8859 | error ("division by zero is not a constant expression"); |
2d76680f PC |
8860 | return false; |
8861 | } | |
8862 | else | |
8863 | { | |
8864 | want_rval = true; | |
69eb4fde | 8865 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
2d76680f PC |
8866 | } |
8867 | } | |
8868 | ||
8869 | case COMPOUND_EXPR: | |
8870 | { | |
8871 | /* check_return_expr sometimes wraps a TARGET_EXPR in a | |
46fdced6 | 8872 | COMPOUND_EXPR; don't get confused. */ |
2d76680f PC |
8873 | tree op0 = TREE_OPERAND (t, 0); |
8874 | tree op1 = TREE_OPERAND (t, 1); | |
8875 | STRIP_NOPS (op1); | |
46fdced6 | 8876 | if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) |
69eb4fde | 8877 | return RECUR (op0, want_rval); |
2d76680f PC |
8878 | else |
8879 | goto binary; | |
8880 | } | |
8881 | ||
8882 | /* If the first operand is the non-short-circuit constant, look at | |
8883 | the second operand; otherwise we only care about the first one for | |
8884 | potentiality. */ | |
8885 | case TRUTH_AND_EXPR: | |
8886 | case TRUTH_ANDIF_EXPR: | |
8887 | tmp = boolean_true_node; | |
8888 | goto truth; | |
8889 | case TRUTH_OR_EXPR: | |
8890 | case TRUTH_ORIF_EXPR: | |
8891 | tmp = boolean_false_node; | |
8892 | truth: | |
8893 | { | |
9927ecbb PP |
8894 | tree op0 = TREE_OPERAND (t, 0); |
8895 | tree op1 = TREE_OPERAND (t, 1); | |
8896 | if (!RECUR (op0, rval)) | |
2d76680f | 8897 | return false; |
9927ecbb PP |
8898 | if (!(flags & tf_error) && RECUR (op1, rval)) |
8899 | /* When quiet, try to avoid expensive trial evaluation by first | |
8900 | checking potentiality of the second operand. */ | |
8901 | return true; | |
2d76680f | 8902 | if (!processing_template_decl) |
9927ecbb PP |
8903 | op0 = cxx_eval_outermost_constant_expr (op0, true); |
8904 | if (tree_int_cst_equal (op0, tmp)) | |
8905 | return (flags & tf_error) ? RECUR (op1, rval) : false; | |
2d76680f PC |
8906 | else |
8907 | return true; | |
8908 | } | |
8909 | ||
8910 | case PLUS_EXPR: | |
8911 | case MULT_EXPR: | |
8912 | case POINTER_PLUS_EXPR: | |
8913 | case RDIV_EXPR: | |
8914 | case EXACT_DIV_EXPR: | |
8915 | case MIN_EXPR: | |
8916 | case MAX_EXPR: | |
8917 | case LSHIFT_EXPR: | |
8918 | case RSHIFT_EXPR: | |
81fa6d73 JJ |
8919 | case LROTATE_EXPR: |
8920 | case RROTATE_EXPR: | |
2d76680f PC |
8921 | case BIT_IOR_EXPR: |
8922 | case BIT_XOR_EXPR: | |
8923 | case BIT_AND_EXPR: | |
8924 | case TRUTH_XOR_EXPR: | |
8925 | case UNORDERED_EXPR: | |
8926 | case ORDERED_EXPR: | |
8927 | case UNLT_EXPR: | |
8928 | case UNLE_EXPR: | |
8929 | case UNGT_EXPR: | |
8930 | case UNGE_EXPR: | |
8931 | case UNEQ_EXPR: | |
8932 | case LTGT_EXPR: | |
8933 | case RANGE_EXPR: | |
8934 | case COMPLEX_EXPR: | |
8935 | want_rval = true; | |
8936 | /* Fall through. */ | |
8937 | case ARRAY_REF: | |
8938 | case ARRAY_RANGE_REF: | |
8939 | case MEMBER_REF: | |
8940 | case DOTSTAR_EXPR: | |
41b38772 | 8941 | case MEM_REF: |
4856a1f0 PC |
8942 | case BINARY_LEFT_FOLD_EXPR: |
8943 | case BINARY_RIGHT_FOLD_EXPR: | |
2d76680f PC |
8944 | binary: |
8945 | for (i = 0; i < 2; ++i) | |
69eb4fde | 8946 | if (!RECUR (TREE_OPERAND (t, i), want_rval)) |
2d76680f PC |
8947 | return false; |
8948 | return true; | |
8949 | ||
2d76680f PC |
8950 | case VEC_PERM_EXPR: |
8951 | for (i = 0; i < 3; ++i) | |
69eb4fde | 8952 | if (!RECUR (TREE_OPERAND (t, i), true)) |
2d76680f PC |
8953 | return false; |
8954 | return true; | |
8955 | ||
8956 | case COND_EXPR: | |
b04445d4 | 8957 | if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20) |
d5bcd6d4 JM |
8958 | { |
8959 | if (flags & tf_error) | |
e40b6fc7 | 8960 | error_at (loc, "%<delete[]%> is not a constant expression"); |
d5bcd6d4 JM |
8961 | return false; |
8962 | } | |
8963 | /* Fall through. */ | |
8964 | case IF_STMT: | |
2d76680f PC |
8965 | case VEC_COND_EXPR: |
8966 | /* If the condition is a known constant, we know which of the legs we | |
8967 | care about; otherwise we only require that the condition and | |
8968 | either of the legs be potentially constant. */ | |
8969 | tmp = TREE_OPERAND (t, 0); | |
69eb4fde | 8970 | if (!RECUR (tmp, rval)) |
2d76680f PC |
8971 | return false; |
8972 | if (!processing_template_decl) | |
8973 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
117c6426 JJ |
8974 | /* potential_constant_expression* isn't told if it is called for |
8975 | manifestly_const_eval or not, so for consteval if always | |
8976 | process both branches as if the condition is not a known | |
8977 | constant. */ | |
8978 | if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t)) | |
8979 | { | |
8980 | if (integer_zerop (tmp)) | |
8981 | return RECUR (TREE_OPERAND (t, 2), want_rval); | |
8982 | else if (TREE_CODE (tmp) == INTEGER_CST) | |
8983 | return RECUR (TREE_OPERAND (t, 1), want_rval); | |
8984 | } | |
0fb7aa20 | 8985 | tmp = *jump_target; |
2d76680f | 8986 | for (i = 1; i < 3; ++i) |
0fb7aa20 JJ |
8987 | { |
8988 | tree this_jump_target = tmp; | |
8989 | if (potential_constant_expression_1 (TREE_OPERAND (t, i), | |
8990 | want_rval, strict, now, | |
8991 | tf_none, &this_jump_target)) | |
8992 | { | |
8993 | if (returns (&this_jump_target)) | |
8994 | *jump_target = this_jump_target; | |
8995 | else if (!returns (jump_target)) | |
8996 | { | |
8997 | if (breaks (&this_jump_target) | |
8998 | || continues (&this_jump_target)) | |
8999 | *jump_target = this_jump_target; | |
9000 | if (i == 1) | |
9001 | { | |
9002 | /* If the then branch is potentially constant, but | |
9003 | does not return, check if the else branch | |
9004 | couldn't return, break or continue. */ | |
9005 | hash_set<tree> pset; | |
9006 | check_for_return_continue_data data = { &pset, NULL_TREE, | |
9007 | NULL_TREE }; | |
9008 | if (tree ret_expr | |
9009 | = cp_walk_tree (&TREE_OPERAND (t, 2), | |
9010 | check_for_return_continue, &data, | |
9011 | &pset)) | |
9012 | *jump_target = ret_expr; | |
9013 | else if (*jump_target == NULL_TREE) | |
9014 | { | |
9015 | if (data.continue_stmt) | |
9016 | *jump_target = data.continue_stmt; | |
9017 | else if (data.break_stmt) | |
9018 | *jump_target = data.break_stmt; | |
9019 | } | |
9020 | } | |
9021 | } | |
9022 | return true; | |
9023 | } | |
9024 | } | |
2d76680f | 9025 | if (flags & tf_error) |
e40b6fc7 | 9026 | error_at (loc, "expression %qE is not a constant expression", t); |
2d76680f PC |
9027 | return false; |
9028 | ||
9029 | case VEC_INIT_EXPR: | |
9030 | if (VEC_INIT_EXPR_IS_CONSTEXPR (t)) | |
9031 | return true; | |
9032 | if (flags & tf_error) | |
9033 | { | |
e40b6fc7 | 9034 | error_at (loc, "non-constant array initialization"); |
2d76680f PC |
9035 | diagnose_non_constexpr_vec_init (t); |
9036 | } | |
9037 | return false; | |
9038 | ||
133bc698 JM |
9039 | case TYPE_DECL: |
9040 | case TAG_DEFN: | |
9041 | /* We can see these in statement-expressions. */ | |
9042 | return true; | |
9043 | ||
baf9ebc8 | 9044 | case CLEANUP_STMT: |
1006c9d4 JJ |
9045 | if (!RECUR (CLEANUP_BODY (t), any)) |
9046 | return false; | |
9047 | if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any)) | |
9048 | return false; | |
9049 | return true; | |
9050 | ||
cda0a029 | 9051 | case EMPTY_CLASS_EXPR: |
46fdced6 | 9052 | return true; |
cda0a029 | 9053 | |
f937929e JM |
9054 | case GOTO_EXPR: |
9055 | { | |
9056 | tree *target = &TREE_OPERAND (t, 0); | |
ab3af181 JJ |
9057 | /* Gotos representing break and continue are OK. */ |
9058 | if (breaks (target) || continues (target)) | |
3075affd JM |
9059 | { |
9060 | *jump_target = *target; | |
9061 | return true; | |
9062 | } | |
ab3af181 | 9063 | if (flags & tf_error) |
e40b6fc7 | 9064 | error_at (loc, "%<goto%> is not a constant expression"); |
ab3af181 | 9065 | return false; |
f937929e JM |
9066 | } |
9067 | ||
6f20c42c MP |
9068 | case LABEL_EXPR: |
9069 | t = LABEL_EXPR_LABEL (t); | |
8892d532 | 9070 | if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23) |
6f20c42c MP |
9071 | return true; |
9072 | else if (flags & tf_error) | |
8892d532 JJ |
9073 | error_at (loc, "label definition in %<constexpr%> function only " |
9074 | "available with %<-std=c++2b%> or %<-std=gnu++2b%>"); | |
6f20c42c MP |
9075 | return false; |
9076 | ||
98e09245 | 9077 | case ANNOTATE_EXPR: |
98e09245 JJ |
9078 | return RECUR (TREE_OPERAND (t, 0), rval); |
9079 | ||
896048cf JJ |
9080 | case BIT_CAST_EXPR: |
9081 | return RECUR (TREE_OPERAND (t, 0), rval); | |
9082 | ||
49789fd0 IS |
9083 | /* Coroutine await, yield and return expressions are not. */ |
9084 | case CO_AWAIT_EXPR: | |
9085 | case CO_YIELD_EXPR: | |
9086 | case CO_RETURN_EXPR: | |
9087 | return false; | |
9088 | ||
d4c470c3 PP |
9089 | case NONTYPE_ARGUMENT_PACK: |
9090 | { | |
9091 | tree args = ARGUMENT_PACK_ARGS (t); | |
9092 | int len = TREE_VEC_LENGTH (args); | |
9093 | for (int i = 0; i < len; ++i) | |
9094 | if (!RECUR (TREE_VEC_ELT (args, i), any)) | |
9095 | return false; | |
9096 | return true; | |
9097 | } | |
9098 | ||
2d76680f | 9099 | default: |
878cffbd | 9100 | if (objc_non_constant_expr_p (t)) |
2d76680f PC |
9101 | return false; |
9102 | ||
9103 | sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t))); | |
ab3af181 | 9104 | gcc_unreachable (); |
2d76680f PC |
9105 | return false; |
9106 | } | |
69eb4fde | 9107 | #undef RECUR |
2d76680f PC |
9108 | } |
9109 | ||
3075affd JM |
9110 | bool |
9111 | potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, | |
9112 | tsubst_flags_t flags) | |
9113 | { | |
9927ecbb PP |
9114 | if (flags & tf_error) |
9115 | { | |
9116 | /* Check potentiality quietly first, as that could be performed more | |
9117 | efficiently in some cases (currently only for TRUTH_*_EXPR). If | |
9118 | that fails, replay the check noisily to give errors. */ | |
9119 | flags &= ~tf_error; | |
9120 | if (potential_constant_expression_1 (t, want_rval, strict, now, flags)) | |
9121 | return true; | |
9122 | flags |= tf_error; | |
9123 | } | |
9124 | ||
3075affd JM |
9125 | tree target = NULL_TREE; |
9126 | return potential_constant_expression_1 (t, want_rval, strict, now, | |
9127 | flags, &target); | |
9128 | } | |
9129 | ||
2d76680f PC |
9130 | /* The main entry point to the above. */ |
9131 | ||
9132 | bool | |
9133 | potential_constant_expression (tree t) | |
9134 | { | |
a0ab7ccd | 9135 | return potential_constant_expression_1 (t, false, true, false, tf_none); |
2d76680f PC |
9136 | } |
9137 | ||
9138 | /* As above, but require a constant rvalue. */ | |
9139 | ||
9140 | bool | |
9141 | potential_rvalue_constant_expression (tree t) | |
9142 | { | |
a0ab7ccd | 9143 | return potential_constant_expression_1 (t, true, true, false, tf_none); |
2d76680f PC |
9144 | } |
9145 | ||
9146 | /* Like above, but complain about non-constant expressions. */ | |
9147 | ||
9148 | bool | |
9149 | require_potential_constant_expression (tree t) | |
9150 | { | |
d8cff23f MP |
9151 | return potential_constant_expression_1 (t, false, true, false, |
9152 | tf_warning_or_error); | |
2d76680f PC |
9153 | } |
9154 | ||
9155 | /* Cross product of the above. */ | |
9156 | ||
9157 | bool | |
9158 | require_potential_rvalue_constant_expression (tree t) | |
9159 | { | |
d8cff23f MP |
9160 | return potential_constant_expression_1 (t, true, true, false, |
9161 | tf_warning_or_error); | |
9162 | } | |
9163 | ||
9164 | /* Like above, but don't consider PARM_DECL a potential_constant_expression. */ | |
9165 | ||
9166 | bool | |
9167 | require_rvalue_constant_expression (tree t) | |
9168 | { | |
9169 | return potential_constant_expression_1 (t, true, true, true, | |
9170 | tf_warning_or_error); | |
a0ab7ccd JM |
9171 | } |
9172 | ||
9173 | /* Like potential_constant_expression, but don't consider possible constexpr | |
9174 | substitution of the current function. That is, PARM_DECL qualifies under | |
9175 | potential_constant_expression, but not here. | |
9176 | ||
9177 | This is basically what you can check when any actual constant values might | |
9178 | be value-dependent. */ | |
9179 | ||
9180 | bool | |
9181 | is_constant_expression (tree t) | |
9182 | { | |
9183 | return potential_constant_expression_1 (t, false, true, true, tf_none); | |
9184 | } | |
9185 | ||
beb019d3 JM |
9186 | /* As above, but expect an rvalue. */ |
9187 | ||
9188 | bool | |
9189 | is_rvalue_constant_expression (tree t) | |
9190 | { | |
9191 | return potential_constant_expression_1 (t, true, true, true, tf_none); | |
9192 | } | |
9193 | ||
a0ab7ccd JM |
9194 | /* Like above, but complain about non-constant expressions. */ |
9195 | ||
9196 | bool | |
9197 | require_constant_expression (tree t) | |
9198 | { | |
9199 | return potential_constant_expression_1 (t, false, true, true, | |
9200 | tf_warning_or_error); | |
9201 | } | |
9202 | ||
9203 | /* Like is_constant_expression, but allow const variables that are not allowed | |
9204 | under constexpr rules. */ | |
9205 | ||
9206 | bool | |
9207 | is_static_init_expression (tree t) | |
9208 | { | |
9209 | return potential_constant_expression_1 (t, false, false, true, tf_none); | |
2d76680f PC |
9210 | } |
9211 | ||
eb07f187 JM |
9212 | /* Returns true if T is a potential constant expression that is not |
9213 | instantiation-dependent, and therefore a candidate for constant folding even | |
9214 | in a template. */ | |
9215 | ||
9216 | bool | |
a0ab7ccd | 9217 | is_nondependent_constant_expression (tree t) |
eb07f187 JM |
9218 | { |
9219 | return (!type_unknown_p (t) | |
a0ab7ccd | 9220 | && is_constant_expression (t) |
eb07f187 JM |
9221 | && !instantiation_dependent_expression_p (t)); |
9222 | } | |
9223 | ||
9224 | /* Returns true if T is a potential static initializer expression that is not | |
9225 | instantiation-dependent. */ | |
9226 | ||
9227 | bool | |
a0ab7ccd | 9228 | is_nondependent_static_init_expression (tree t) |
eb07f187 JM |
9229 | { |
9230 | return (!type_unknown_p (t) | |
a0ab7ccd | 9231 | && is_static_init_expression (t) |
eb07f187 JM |
9232 | && !instantiation_dependent_expression_p (t)); |
9233 | } | |
9234 | ||
97f3003f JM |
9235 | /* Finalize constexpr processing after parsing. */ |
9236 | ||
9237 | void | |
9238 | fini_constexpr (void) | |
9239 | { | |
9240 | /* The contexpr call and fundef copies tables are no longer needed. */ | |
9241 | constexpr_call_table = NULL; | |
9242 | fundef_copies_table = NULL; | |
9243 | } | |
9244 | ||
2d76680f | 9245 | #include "gt-cp-constexpr.h" |