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