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