]>
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 | ||
7adcbafe | 5 | Copyright (C) 1998-2022 Free Software Foundation, Inc. |
2d76680f PC |
6 | |
7 | This file is part of GCC. | |
8 | ||
9 | GCC is free software; you can redistribute it and/or modify it | |
10 | under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 3, or (at your option) | |
12 | any later version. | |
13 | ||
14 | GCC is distributed in the hope that it will be useful, but | |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with GCC; see the file COPYING3. If not see | |
21 | <http://www.gnu.org/licenses/>. */ | |
22 | ||
23 | #include "config.h" | |
24 | #include "system.h" | |
25 | #include "coretypes.h" | |
2d76680f | 26 | #include "cp-tree.h" |
2adfab87 | 27 | #include "varasm.h" |
2d76680f PC |
28 | #include "c-family/c-objc.h" |
29 | #include "tree-iterator.h" | |
30 | #include "gimplify.h" | |
31 | #include "builtins.h" | |
60813a46 | 32 | #include "tree-inline.h" |
0b274c17 | 33 | #include "ubsan.h" |
8108ea30 | 34 | #include "timevar.h" |
d8fcab68 | 35 | #include "fold-const-call.h" |
8e007055 | 36 | #include "stor-layout.h" |
a8db7887 | 37 | #include "cgraph.h" |
00f34291 | 38 | #include "opts.h" |
87c2080b JM |
39 | #include "stringpool.h" |
40 | #include "attribs.h" | |
2d76680f PC |
41 | |
42 | static bool verify_constant (tree, bool, bool *, bool *); | |
43 | #define VERIFY_CONSTANT(X) \ | |
44 | do { \ | |
2b3ab879 | 45 | if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \ |
2d76680f PC |
46 | return t; \ |
47 | } while (0) | |
48 | ||
582d2481 JJ |
49 | static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex, |
50 | bool insert = false); | |
baf05d54 | 51 | static int array_index_cmp (tree key, tree index); |
582d2481 | 52 | |
2d76680f PC |
53 | /* Returns true iff FUN is an instantiation of a constexpr function |
54 | template or a defaulted constexpr function. */ | |
55 | ||
56 | bool | |
57 | is_instantiation_of_constexpr (tree fun) | |
58 | { | |
59 | return ((DECL_TEMPLOID_INSTANTIATION (fun) | |
60 | && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun))) | |
61 | || (DECL_DEFAULTED_FN (fun) | |
62 | && DECL_DECLARED_CONSTEXPR_P (fun))); | |
63 | } | |
64 | ||
65 | /* Return true if T is a literal type. */ | |
66 | ||
67 | bool | |
68 | literal_type_p (tree t) | |
69 | { | |
70 | if (SCALAR_TYPE_P (t) | |
b55b02ea | 71 | || VECTOR_TYPE_P (t) |
9f613f06 | 72 | || TYPE_REF_P (t) |
e42c407c | 73 | || (VOID_TYPE_P (t) && cxx_dialect >= cxx14)) |
2d76680f PC |
74 | return true; |
75 | if (CLASS_TYPE_P (t)) | |
76 | { | |
77 | t = complete_type (t); | |
78 | gcc_assert (COMPLETE_TYPE_P (t) || errorcount); | |
79 | return CLASSTYPE_LITERAL_P (t); | |
80 | } | |
81 | if (TREE_CODE (t) == ARRAY_TYPE) | |
82 | return literal_type_p (strip_array_types (t)); | |
83 | return false; | |
84 | } | |
85 | ||
86 | /* If DECL is a variable declared `constexpr', require its type | |
9638f320 PC |
87 | be literal. Return error_mark_node if we give an error, the |
88 | DECL otherwise. */ | |
2d76680f PC |
89 | |
90 | tree | |
91 | ensure_literal_type_for_constexpr_object (tree decl) | |
92 | { | |
93 | tree type = TREE_TYPE (decl); | |
94 | if (VAR_P (decl) | |
95 | && (DECL_DECLARED_CONSTEXPR_P (decl) | |
96 | || var_in_constexpr_fn (decl)) | |
97 | && !processing_template_decl) | |
98 | { | |
99 | tree stype = strip_array_types (type); | |
100 | if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype))) | |
101 | /* Don't complain here, we'll complain about incompleteness | |
102 | when we try to initialize the variable. */; | |
103 | else if (!literal_type_p (type)) | |
104 | { | |
105 | if (DECL_DECLARED_CONSTEXPR_P (decl)) | |
f1eac182 | 106 | { |
097f82ec | 107 | auto_diagnostic_group d; |
f2935576 PC |
108 | error_at (DECL_SOURCE_LOCATION (decl), |
109 | "the type %qT of %<constexpr%> variable %qD " | |
110 | "is not literal", type, decl); | |
f1eac182 | 111 | explain_non_literal_class (type); |
9638f320 | 112 | decl = error_mark_node; |
f1eac182 | 113 | } |
8892d532 | 114 | else if (cxx_dialect < cxx23) |
60813a46 | 115 | { |
5d4e573b | 116 | if (!is_instantiation_of_constexpr (current_function_decl)) |
f1eac182 | 117 | { |
097f82ec | 118 | auto_diagnostic_group d; |
f2935576 PC |
119 | error_at (DECL_SOURCE_LOCATION (decl), |
120 | "variable %qD of non-literal type %qT in " | |
8892d532 JJ |
121 | "%<constexpr%> function only available with " |
122 | "%<-std=c++2b%> or %<-std=gnu++2b%>", decl, type); | |
f1eac182 | 123 | explain_non_literal_class (type); |
9638f320 | 124 | decl = error_mark_node; |
f1eac182 | 125 | } |
60813a46 JM |
126 | cp_function_chain->invalid_constexpr = true; |
127 | } | |
2d76680f | 128 | } |
8e9589bd JM |
129 | else if (DECL_DECLARED_CONSTEXPR_P (decl) |
130 | && variably_modified_type_p (type, NULL_TREE)) | |
131 | { | |
f2935576 PC |
132 | error_at (DECL_SOURCE_LOCATION (decl), |
133 | "%<constexpr%> variable %qD has variably-modified " | |
134 | "type %qT", decl, type); | |
8e9589bd JM |
135 | decl = error_mark_node; |
136 | } | |
2d76680f PC |
137 | } |
138 | return decl; | |
139 | } | |
140 | ||
ca752f39 | 141 | struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef> |
2d76680f | 142 | { |
bfc139e2 NS |
143 | static hashval_t hash (const constexpr_fundef *); |
144 | static bool equal (const constexpr_fundef *, const constexpr_fundef *); | |
2d76680f PC |
145 | }; |
146 | ||
147 | /* This table holds all constexpr function definitions seen in | |
148 | the current translation unit. */ | |
149 | ||
150 | static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table; | |
151 | ||
152 | /* Utility function used for managing the constexpr function table. | |
153 | Return true if the entries pointed to by P and Q are for the | |
154 | same constexpr function. */ | |
155 | ||
156 | inline bool | |
bfc139e2 NS |
157 | constexpr_fundef_hasher::equal (const constexpr_fundef *lhs, |
158 | const constexpr_fundef *rhs) | |
2d76680f PC |
159 | { |
160 | return lhs->decl == rhs->decl; | |
161 | } | |
162 | ||
163 | /* Utility function used for managing the constexpr function table. | |
164 | Return a hash value for the entry pointed to by Q. */ | |
165 | ||
166 | inline hashval_t | |
bfc139e2 | 167 | constexpr_fundef_hasher::hash (const constexpr_fundef *fundef) |
2d76680f PC |
168 | { |
169 | return DECL_UID (fundef->decl); | |
170 | } | |
171 | ||
172 | /* Return a previously saved definition of function FUN. */ | |
173 | ||
bfc139e2 | 174 | constexpr_fundef * |
2d76680f PC |
175 | retrieve_constexpr_fundef (tree fun) |
176 | { | |
2d76680f PC |
177 | if (constexpr_fundef_table == NULL) |
178 | return NULL; | |
179 | ||
bfc139e2 | 180 | constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE }; |
2d76680f PC |
181 | return constexpr_fundef_table->find (&fundef); |
182 | } | |
183 | ||
184 | /* Check whether the parameter and return types of FUN are valid for a | |
185 | constexpr function, and complain if COMPLAIN. */ | |
186 | ||
98e5a19a | 187 | bool |
2d76680f PC |
188 | is_valid_constexpr_fn (tree fun, bool complain) |
189 | { | |
190 | bool ret = true; | |
191 | ||
31f7f784 | 192 | if (DECL_INHERITED_CTOR (fun) |
2d76680f PC |
193 | && TREE_CODE (fun) == TEMPLATE_DECL) |
194 | { | |
195 | ret = false; | |
196 | if (complain) | |
84fa214d | 197 | error ("inherited constructor %qD is not %<constexpr%>", |
31f7f784 | 198 | DECL_INHERITED_CTOR (fun)); |
2d76680f PC |
199 | } |
200 | else | |
201 | { | |
202 | for (tree parm = FUNCTION_FIRST_USER_PARM (fun); | |
203 | parm != NULL_TREE; parm = TREE_CHAIN (parm)) | |
204 | if (!literal_type_p (TREE_TYPE (parm))) | |
205 | { | |
206 | ret = false; | |
207 | if (complain) | |
208 | { | |
097f82ec | 209 | auto_diagnostic_group d; |
84fa214d | 210 | error ("invalid type for parameter %d of %<constexpr%> " |
2d76680f PC |
211 | "function %q+#D", DECL_PARM_INDEX (parm), fun); |
212 | explain_non_literal_class (TREE_TYPE (parm)); | |
213 | } | |
214 | } | |
215 | } | |
216 | ||
e1bea341 JM |
217 | if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17) |
218 | { | |
219 | ret = false; | |
220 | if (complain) | |
221 | inform (DECL_SOURCE_LOCATION (fun), | |
84fa214d | 222 | "lambdas are implicitly %<constexpr%> only in C++17 and later"); |
e1bea341 | 223 | } |
87c2080b JM |
224 | else if (DECL_DESTRUCTOR_P (fun)) |
225 | { | |
226 | if (cxx_dialect < cxx20) | |
227 | { | |
228 | ret = false; | |
229 | if (complain) | |
230 | error_at (DECL_SOURCE_LOCATION (fun), | |
231 | "%<constexpr%> destructors only available" | |
232 | " with %<-std=c++20%> or %<-std=gnu++20%>"); | |
233 | } | |
234 | } | |
e1bea341 | 235 | else if (!DECL_CONSTRUCTOR_P (fun)) |
2d76680f PC |
236 | { |
237 | tree rettype = TREE_TYPE (TREE_TYPE (fun)); | |
238 | if (!literal_type_p (rettype)) | |
239 | { | |
240 | ret = false; | |
241 | if (complain) | |
242 | { | |
097f82ec | 243 | auto_diagnostic_group d; |
84fa214d | 244 | error ("invalid return type %qT of %<constexpr%> function %q+D", |
2d76680f PC |
245 | rettype, fun); |
246 | explain_non_literal_class (rettype); | |
247 | } | |
248 | } | |
249 | ||
54069e59 JM |
250 | /* C++14 DR 1684 removed this restriction. */ |
251 | if (cxx_dialect < cxx14 | |
252 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) | |
2d76680f PC |
253 | && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun))) |
254 | { | |
255 | ret = false; | |
097f82ec DM |
256 | if (complain) |
257 | { | |
258 | auto_diagnostic_group d; | |
259 | if (pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic, | |
260 | "enclosing class of %<constexpr%> non-static" | |
261 | " member function %q+#D is not a literal type", | |
262 | fun)) | |
263 | explain_non_literal_class (DECL_CONTEXT (fun)); | |
264 | } | |
2d76680f PC |
265 | } |
266 | } | |
267 | else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun))) | |
268 | { | |
269 | ret = false; | |
270 | if (complain) | |
271 | error ("%q#T has virtual base classes", DECL_CONTEXT (fun)); | |
272 | } | |
273 | ||
274 | return ret; | |
275 | } | |
276 | ||
277 | /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF | |
278 | for a member of an anonymous aggregate, INIT is the initializer for that | |
279 | member, and VEC_OUTER is the vector of constructor elements for the class | |
280 | whose constructor we are processing. Add the initializer to the vector | |
281 | and return true to indicate success. */ | |
282 | ||
283 | static bool | |
284 | build_anon_member_initialization (tree member, tree init, | |
285 | vec<constructor_elt, va_gc> **vec_outer) | |
286 | { | |
287 | /* MEMBER presents the relevant fields from the inside out, but we need | |
288 | to build up the initializer from the outside in so that we can reuse | |
289 | previously built CONSTRUCTORs if this is, say, the second field in an | |
290 | anonymous struct. So we use a vec as a stack. */ | |
291 | auto_vec<tree, 2> fields; | |
292 | do | |
293 | { | |
294 | fields.safe_push (TREE_OPERAND (member, 1)); | |
295 | member = TREE_OPERAND (member, 0); | |
296 | } | |
297 | while (ANON_AGGR_TYPE_P (TREE_TYPE (member)) | |
298 | && TREE_CODE (member) == COMPONENT_REF); | |
299 | ||
300 | /* VEC has the constructor elements vector for the context of FIELD. | |
301 | If FIELD is an anonymous aggregate, we will push inside it. */ | |
302 | vec<constructor_elt, va_gc> **vec = vec_outer; | |
303 | tree field; | |
304 | while (field = fields.pop(), | |
305 | ANON_AGGR_TYPE_P (TREE_TYPE (field))) | |
306 | { | |
307 | tree ctor; | |
308 | /* If there is already an outer constructor entry for the anonymous | |
309 | aggregate FIELD, use it; otherwise, insert one. */ | |
310 | if (vec_safe_is_empty (*vec) | |
311 | || (*vec)->last().index != field) | |
312 | { | |
313 | ctor = build_constructor (TREE_TYPE (field), NULL); | |
314 | CONSTRUCTOR_APPEND_ELT (*vec, field, ctor); | |
315 | } | |
316 | else | |
317 | ctor = (*vec)->last().value; | |
318 | vec = &CONSTRUCTOR_ELTS (ctor); | |
319 | } | |
320 | ||
321 | /* Now we're at the innermost field, the one that isn't an anonymous | |
322 | aggregate. Add its initializer to the CONSTRUCTOR and we're done. */ | |
323 | gcc_assert (fields.is_empty()); | |
324 | CONSTRUCTOR_APPEND_ELT (*vec, field, init); | |
325 | ||
326 | return true; | |
327 | } | |
328 | ||
329 | /* Subroutine of build_constexpr_constructor_member_initializers. | |
330 | The expression tree T represents a data member initialization | |
331 | in a (constexpr) constructor definition. Build a pairing of | |
332 | the data member with its initializer, and prepend that pair | |
333 | to the existing initialization pair INITS. */ | |
334 | ||
335 | static bool | |
336 | build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec) | |
337 | { | |
338 | tree member, init; | |
339 | if (TREE_CODE (t) == CLEANUP_POINT_EXPR) | |
340 | t = TREE_OPERAND (t, 0); | |
341 | if (TREE_CODE (t) == EXPR_STMT) | |
342 | t = TREE_OPERAND (t, 0); | |
343 | if (t == error_mark_node) | |
344 | return false; | |
345 | if (TREE_CODE (t) == STATEMENT_LIST) | |
346 | { | |
0f54cc9c JM |
347 | for (tree stmt : tsi_range (t)) |
348 | if (! build_data_member_initialization (stmt, vec)) | |
349 | return false; | |
2d76680f PC |
350 | return true; |
351 | } | |
352 | if (TREE_CODE (t) == CLEANUP_STMT) | |
353 | { | |
354 | /* We can't see a CLEANUP_STMT in a constructor for a literal class, | |
355 | but we can in a constexpr constructor for a non-literal class. Just | |
356 | ignore it; either all the initialization will be constant, in which | |
357 | case the cleanup can't run, or it can't be constexpr. | |
358 | Still recurse into CLEANUP_BODY. */ | |
359 | return build_data_member_initialization (CLEANUP_BODY (t), vec); | |
360 | } | |
361 | if (TREE_CODE (t) == CONVERT_EXPR) | |
362 | t = TREE_OPERAND (t, 0); | |
363 | if (TREE_CODE (t) == INIT_EXPR | |
60813a46 JM |
364 | /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only |
365 | use what this function builds for cx_check_missing_mem_inits, and | |
366 | assignment in the ctor body doesn't count. */ | |
367 | || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR)) | |
2d76680f PC |
368 | { |
369 | member = TREE_OPERAND (t, 0); | |
370 | init = break_out_target_exprs (TREE_OPERAND (t, 1)); | |
371 | } | |
372 | else if (TREE_CODE (t) == CALL_EXPR) | |
373 | { | |
60813a46 JM |
374 | tree fn = get_callee_fndecl (t); |
375 | if (!fn || !DECL_CONSTRUCTOR_P (fn)) | |
376 | /* We're only interested in calls to subobject constructors. */ | |
377 | return true; | |
2d76680f PC |
378 | member = CALL_EXPR_ARG (t, 0); |
379 | /* We don't use build_cplus_new here because it complains about | |
380 | abstract bases. Leaving the call unwrapped means that it has the | |
381 | wrong type, but cxx_eval_constant_expression doesn't care. */ | |
382 | init = break_out_target_exprs (t); | |
383 | } | |
384 | else if (TREE_CODE (t) == BIND_EXPR) | |
385 | return build_data_member_initialization (BIND_EXPR_BODY (t), vec); | |
2d76680f | 386 | else |
60813a46 JM |
387 | /* Don't add anything else to the CONSTRUCTOR. */ |
388 | return true; | |
2d76680f PC |
389 | if (INDIRECT_REF_P (member)) |
390 | member = TREE_OPERAND (member, 0); | |
391 | if (TREE_CODE (member) == NOP_EXPR) | |
392 | { | |
393 | tree op = member; | |
394 | STRIP_NOPS (op); | |
395 | if (TREE_CODE (op) == ADDR_EXPR) | |
396 | { | |
397 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
398 | (TREE_TYPE (TREE_TYPE (op)), | |
399 | TREE_TYPE (TREE_TYPE (member)))); | |
400 | /* Initializing a cv-qualified member; we need to look through | |
401 | the const_cast. */ | |
402 | member = op; | |
403 | } | |
404 | else if (op == current_class_ptr | |
405 | && (same_type_ignoring_top_level_qualifiers_p | |
406 | (TREE_TYPE (TREE_TYPE (member)), | |
407 | current_class_type))) | |
408 | /* Delegating constructor. */ | |
409 | member = op; | |
410 | else | |
411 | { | |
412 | /* This is an initializer for an empty base; keep it for now so | |
413 | we can check it in cxx_eval_bare_aggregate. */ | |
414 | gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member)))); | |
415 | } | |
416 | } | |
417 | if (TREE_CODE (member) == ADDR_EXPR) | |
418 | member = TREE_OPERAND (member, 0); | |
419 | if (TREE_CODE (member) == COMPONENT_REF) | |
420 | { | |
421 | tree aggr = TREE_OPERAND (member, 0); | |
8cb7aaa1 JM |
422 | if (TREE_CODE (aggr) == VAR_DECL) |
423 | /* Initializing a local variable, don't add anything. */ | |
424 | return true; | |
2d76680f PC |
425 | if (TREE_CODE (aggr) != COMPONENT_REF) |
426 | /* Normal member initialization. */ | |
427 | member = TREE_OPERAND (member, 1); | |
428 | else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr))) | |
429 | /* Initializing a member of an anonymous union. */ | |
430 | return build_anon_member_initialization (member, init, vec); | |
431 | else | |
432 | /* We're initializing a vtable pointer in a base. Leave it as | |
433 | COMPONENT_REF so we remember the path to get to the vfield. */ | |
434 | gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node); | |
435 | } | |
436 | ||
49b5925f JM |
437 | /* Value-initialization can produce multiple initializers for the |
438 | same field; use the last one. */ | |
439 | if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member) | |
440 | (*vec)->last().value = init; | |
441 | else | |
442 | CONSTRUCTOR_APPEND_ELT (*vec, member, init); | |
2d76680f PC |
443 | return true; |
444 | } | |
445 | ||
446 | /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval. | |
447 | In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a | |
448 | BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */ | |
449 | ||
450 | static bool | |
451 | check_constexpr_bind_expr_vars (tree t) | |
452 | { | |
453 | gcc_assert (TREE_CODE (t) == BIND_EXPR); | |
454 | ||
2d76680f PC |
455 | for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var)) |
456 | if (TREE_CODE (var) == TYPE_DECL | |
4414e22e PC |
457 | && DECL_IMPLICIT_TYPEDEF_P (var) |
458 | && !LAMBDA_TYPE_P (TREE_TYPE (var))) | |
2d76680f PC |
459 | return false; |
460 | return true; | |
461 | } | |
462 | ||
463 | /* Subroutine of check_constexpr_ctor_body. */ | |
464 | ||
465 | static bool | |
466 | check_constexpr_ctor_body_1 (tree last, tree list) | |
467 | { | |
468 | switch (TREE_CODE (list)) | |
469 | { | |
470 | case DECL_EXPR: | |
a0008434 MP |
471 | if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL |
472 | || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL) | |
2d76680f | 473 | return true; |
2d76680f PC |
474 | return false; |
475 | ||
476 | case CLEANUP_POINT_EXPR: | |
477 | return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0), | |
478 | /*complain=*/false); | |
479 | ||
480 | case BIND_EXPR: | |
481 | if (!check_constexpr_bind_expr_vars (list) | |
482 | || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list), | |
483 | /*complain=*/false)) | |
484 | return false; | |
485 | return true; | |
486 | ||
487 | case USING_STMT: | |
488 | case STATIC_ASSERT: | |
96a95ac1 | 489 | case DEBUG_BEGIN_STMT: |
2d76680f PC |
490 | return true; |
491 | ||
492 | default: | |
493 | return false; | |
494 | } | |
495 | } | |
496 | ||
497 | /* Make sure that there are no statements after LAST in the constructor | |
498 | body represented by LIST. */ | |
499 | ||
500 | bool | |
501 | check_constexpr_ctor_body (tree last, tree list, bool complain) | |
502 | { | |
60813a46 JM |
503 | /* C++14 doesn't require a constexpr ctor to have an empty body. */ |
504 | if (cxx_dialect >= cxx14) | |
505 | return true; | |
506 | ||
2d76680f PC |
507 | bool ok = true; |
508 | if (TREE_CODE (list) == STATEMENT_LIST) | |
509 | { | |
510 | tree_stmt_iterator i = tsi_last (list); | |
511 | for (; !tsi_end_p (i); tsi_prev (&i)) | |
512 | { | |
513 | tree t = tsi_stmt (i); | |
514 | if (t == last) | |
515 | break; | |
516 | if (!check_constexpr_ctor_body_1 (last, t)) | |
517 | { | |
518 | ok = false; | |
519 | break; | |
520 | } | |
521 | } | |
522 | } | |
523 | else if (list != last | |
524 | && !check_constexpr_ctor_body_1 (last, list)) | |
525 | ok = false; | |
526 | if (!ok) | |
527 | { | |
528 | if (complain) | |
84fa214d | 529 | error ("%<constexpr%> constructor does not have empty body"); |
2d76680f PC |
530 | DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false; |
531 | } | |
532 | return ok; | |
533 | } | |
534 | ||
535 | /* V is a vector of constructor elements built up for the base and member | |
536 | initializers of a constructor for TYPE. They need to be in increasing | |
537 | offset order, which they might not be yet if TYPE has a primary base | |
538 | which is not first in the base-clause or a vptr and at least one base | |
539 | all of which are non-primary. */ | |
540 | ||
541 | static vec<constructor_elt, va_gc> * | |
542 | sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v) | |
543 | { | |
544 | tree pri = CLASSTYPE_PRIMARY_BINFO (type); | |
545 | tree field_type; | |
546 | unsigned i; | |
547 | constructor_elt *ce; | |
548 | ||
549 | if (pri) | |
550 | field_type = BINFO_TYPE (pri); | |
551 | else if (TYPE_CONTAINS_VPTR_P (type)) | |
552 | field_type = vtbl_ptr_type_node; | |
553 | else | |
554 | return v; | |
555 | ||
556 | /* Find the element for the primary base or vptr and move it to the | |
557 | beginning of the vec. */ | |
558 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
559 | if (TREE_TYPE (ce->index) == field_type) | |
560 | break; | |
561 | ||
562 | if (i > 0 && i < vec_safe_length (v)) | |
563 | { | |
564 | vec<constructor_elt, va_gc> &vref = *v; | |
565 | constructor_elt elt = vref[i]; | |
566 | for (; i > 0; --i) | |
567 | vref[i] = vref[i-1]; | |
568 | vref[0] = elt; | |
569 | } | |
570 | ||
571 | return v; | |
572 | } | |
573 | ||
574 | /* Build compile-time evalable representations of member-initializer list | |
575 | for a constexpr constructor. */ | |
576 | ||
577 | static tree | |
578 | build_constexpr_constructor_member_initializers (tree type, tree body) | |
579 | { | |
580 | vec<constructor_elt, va_gc> *vec = NULL; | |
581 | bool ok = true; | |
fa837fb6 JM |
582 | while (true) |
583 | switch (TREE_CODE (body)) | |
584 | { | |
585 | case MUST_NOT_THROW_EXPR: | |
586 | case EH_SPEC_BLOCK: | |
587 | body = TREE_OPERAND (body, 0); | |
588 | break; | |
589 | ||
590 | case STATEMENT_LIST: | |
0f54cc9c | 591 | for (tree stmt : tsi_range (body)) |
fa837fb6 | 592 | { |
0f54cc9c | 593 | body = stmt; |
fa837fb6 JM |
594 | if (TREE_CODE (body) == BIND_EXPR) |
595 | break; | |
596 | } | |
597 | break; | |
598 | ||
599 | case BIND_EXPR: | |
600 | body = BIND_EXPR_BODY (body); | |
601 | goto found; | |
602 | ||
603 | default: | |
604 | gcc_unreachable (); | |
58cc255c | 605 | } |
fa837fb6 | 606 | found: |
1259cb6d JJ |
607 | if (TREE_CODE (body) == TRY_BLOCK) |
608 | { | |
609 | body = TREE_OPERAND (body, 0); | |
610 | if (TREE_CODE (body) == BIND_EXPR) | |
611 | body = BIND_EXPR_BODY (body); | |
612 | } | |
2d76680f PC |
613 | if (TREE_CODE (body) == CLEANUP_POINT_EXPR) |
614 | { | |
615 | body = TREE_OPERAND (body, 0); | |
616 | if (TREE_CODE (body) == EXPR_STMT) | |
617 | body = TREE_OPERAND (body, 0); | |
618 | if (TREE_CODE (body) == INIT_EXPR | |
619 | && (same_type_ignoring_top_level_qualifiers_p | |
620 | (TREE_TYPE (TREE_OPERAND (body, 0)), | |
621 | current_class_type))) | |
622 | { | |
623 | /* Trivial copy. */ | |
624 | return TREE_OPERAND (body, 1); | |
625 | } | |
626 | ok = build_data_member_initialization (body, &vec); | |
627 | } | |
628 | else if (TREE_CODE (body) == STATEMENT_LIST) | |
629 | { | |
0f54cc9c | 630 | for (tree stmt : tsi_range (body)) |
2d76680f | 631 | { |
0f54cc9c | 632 | ok = build_data_member_initialization (stmt, &vec); |
2d76680f PC |
633 | if (!ok) |
634 | break; | |
635 | } | |
636 | } | |
2d76680f PC |
637 | else if (EXPR_P (body)) |
638 | ok = build_data_member_initialization (body, &vec); | |
639 | else | |
640 | gcc_assert (errorcount > 0); | |
641 | if (ok) | |
642 | { | |
643 | if (vec_safe_length (vec) > 0) | |
644 | { | |
645 | /* In a delegating constructor, return the target. */ | |
646 | constructor_elt *ce = &(*vec)[0]; | |
647 | if (ce->index == current_class_ptr) | |
648 | { | |
649 | body = ce->value; | |
650 | vec_free (vec); | |
651 | return body; | |
652 | } | |
653 | } | |
654 | vec = sort_constexpr_mem_initializers (type, vec); | |
655 | return build_constructor (type, vec); | |
656 | } | |
657 | else | |
658 | return error_mark_node; | |
659 | } | |
660 | ||
1b6fa695 ML |
661 | /* We have an expression tree T that represents a call, either CALL_EXPR |
662 | or AGGR_INIT_EXPR. If the call is lexically to a named function, | |
663 | retrun the _DECL for that function. */ | |
664 | ||
665 | static tree | |
666 | get_function_named_in_call (tree t) | |
667 | { | |
668 | tree fun = cp_get_callee (t); | |
669 | if (fun && TREE_CODE (fun) == ADDR_EXPR | |
670 | && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL) | |
671 | fun = TREE_OPERAND (fun, 0); | |
672 | return fun; | |
673 | } | |
674 | ||
bfc139e2 | 675 | /* Subroutine of check_constexpr_fundef. BODY is the body of a function |
2d76680f PC |
676 | declared to be constexpr, or a sub-statement thereof. Returns the |
677 | return value if suitable, error_mark_node for a statement not allowed in | |
678 | a constexpr function, or NULL_TREE if no return value was found. */ | |
679 | ||
5afef8b1 | 680 | tree |
2d76680f PC |
681 | constexpr_fn_retval (tree body) |
682 | { | |
683 | switch (TREE_CODE (body)) | |
684 | { | |
685 | case STATEMENT_LIST: | |
686 | { | |
2d76680f | 687 | tree expr = NULL_TREE; |
0f54cc9c | 688 | for (tree stmt : tsi_range (body)) |
2d76680f | 689 | { |
0f54cc9c | 690 | tree s = constexpr_fn_retval (stmt); |
2d76680f PC |
691 | if (s == error_mark_node) |
692 | return error_mark_node; | |
693 | else if (s == NULL_TREE) | |
694 | /* Keep iterating. */; | |
695 | else if (expr) | |
696 | /* Multiple return statements. */ | |
697 | return error_mark_node; | |
698 | else | |
699 | expr = s; | |
700 | } | |
701 | return expr; | |
702 | } | |
703 | ||
704 | case RETURN_EXPR: | |
705 | return break_out_target_exprs (TREE_OPERAND (body, 0)); | |
706 | ||
707 | case DECL_EXPR: | |
0162cb3b PC |
708 | { |
709 | tree decl = DECL_EXPR_DECL (body); | |
710 | if (TREE_CODE (decl) == USING_DECL | |
711 | /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */ | |
712 | || DECL_ARTIFICIAL (decl)) | |
713 | return NULL_TREE; | |
714 | return error_mark_node; | |
715 | } | |
2d76680f PC |
716 | |
717 | case CLEANUP_POINT_EXPR: | |
718 | return constexpr_fn_retval (TREE_OPERAND (body, 0)); | |
719 | ||
720 | case BIND_EXPR: | |
721 | if (!check_constexpr_bind_expr_vars (body)) | |
722 | return error_mark_node; | |
723 | return constexpr_fn_retval (BIND_EXPR_BODY (body)); | |
724 | ||
725 | case USING_STMT: | |
96a95ac1 | 726 | case DEBUG_BEGIN_STMT: |
2d76680f PC |
727 | return NULL_TREE; |
728 | ||
1b6fa695 ML |
729 | case CALL_EXPR: |
730 | { | |
731 | tree fun = get_function_named_in_call (body); | |
732 | if (fun != NULL_TREE | |
3d78e008 | 733 | && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE)) |
1b6fa695 ML |
734 | return NULL_TREE; |
735 | } | |
736 | /* Fallthru. */ | |
737 | ||
2d76680f PC |
738 | default: |
739 | return error_mark_node; | |
740 | } | |
741 | } | |
742 | ||
bfc139e2 | 743 | /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of |
2d76680f PC |
744 | FUN; do the necessary transformations to turn it into a single expression |
745 | that we can store in the hash table. */ | |
746 | ||
747 | static tree | |
748 | massage_constexpr_body (tree fun, tree body) | |
749 | { | |
750 | if (DECL_CONSTRUCTOR_P (fun)) | |
751 | body = build_constexpr_constructor_member_initializers | |
752 | (DECL_CONTEXT (fun), body); | |
60813a46 | 753 | else if (cxx_dialect < cxx14) |
2d76680f PC |
754 | { |
755 | if (TREE_CODE (body) == EH_SPEC_BLOCK) | |
756 | body = EH_SPEC_STMTS (body); | |
757 | if (TREE_CODE (body) == MUST_NOT_THROW_EXPR) | |
758 | body = TREE_OPERAND (body, 0); | |
759 | body = constexpr_fn_retval (body); | |
760 | } | |
761 | return body; | |
762 | } | |
763 | ||
3e4b91f2 NS |
764 | /* CTYPE is a type constructed from BODY. Return true if some |
765 | bases/fields are uninitialized, and complain if COMPLAIN. */ | |
2d76680f PC |
766 | |
767 | static bool | |
3e4b91f2 | 768 | cx_check_missing_mem_inits (tree ctype, tree body, bool complain) |
2d76680f | 769 | { |
7906797e | 770 | /* We allow uninitialized bases/fields in C++20. */ |
b04445d4 | 771 | if (cxx_dialect >= cxx20) |
7906797e MP |
772 | return false; |
773 | ||
3e4b91f2 NS |
774 | unsigned nelts = 0; |
775 | ||
776 | if (body) | |
777 | { | |
778 | if (TREE_CODE (body) != CONSTRUCTOR) | |
779 | return false; | |
780 | nelts = CONSTRUCTOR_NELTS (body); | |
781 | } | |
782 | tree field = TYPE_FIELDS (ctype); | |
2d76680f PC |
783 | |
784 | if (TREE_CODE (ctype) == UNION_TYPE) | |
785 | { | |
0c7bce0a | 786 | if (nelts == 0 && next_aggregate_field (field)) |
2d76680f PC |
787 | { |
788 | if (complain) | |
789 | error ("%<constexpr%> constructor for union %qT must " | |
790 | "initialize exactly one non-static data member", ctype); | |
791 | return true; | |
792 | } | |
793 | return false; | |
794 | } | |
795 | ||
3e4b91f2 NS |
796 | /* Iterate over the CONSTRUCTOR, checking any missing fields don't |
797 | need an explicit initialization. */ | |
798 | bool bad = false; | |
799 | for (unsigned i = 0; i <= nelts; ++i) | |
2d76680f | 800 | { |
3e4b91f2 NS |
801 | tree index = NULL_TREE; |
802 | if (i < nelts) | |
2d76680f PC |
803 | { |
804 | index = CONSTRUCTOR_ELT (body, i)->index; | |
805 | /* Skip base and vtable inits. */ | |
806 | if (TREE_CODE (index) != FIELD_DECL | |
807 | || DECL_ARTIFICIAL (index)) | |
808 | continue; | |
809 | } | |
3e4b91f2 | 810 | |
2d76680f PC |
811 | for (; field != index; field = DECL_CHAIN (field)) |
812 | { | |
813 | tree ftype; | |
3e4b91f2 NS |
814 | if (TREE_CODE (field) != FIELD_DECL) |
815 | continue; | |
7c30b12a | 816 | if (DECL_UNNAMED_BIT_FIELD (field)) |
2d76680f | 817 | continue; |
3e4b91f2 NS |
818 | if (DECL_ARTIFICIAL (field)) |
819 | continue; | |
820 | if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) | |
821 | { | |
7906797e | 822 | /* Recurse to check the anonymous aggregate member. */ |
3e4b91f2 NS |
823 | bad |= cx_check_missing_mem_inits |
824 | (TREE_TYPE (field), NULL_TREE, complain); | |
825 | if (bad && !complain) | |
826 | return true; | |
827 | continue; | |
828 | } | |
a1c9c9ff JM |
829 | ftype = TREE_TYPE (field); |
830 | if (!ftype || !TYPE_P (ftype) || !COMPLETE_TYPE_P (ftype)) | |
831 | /* A flexible array can't be intialized here, so don't complain | |
832 | that it isn't. */ | |
833 | continue; | |
a4dfd0f0 | 834 | if (is_empty_field (field)) |
6876b269 JM |
835 | /* An empty field doesn't need an initializer. */ |
836 | continue; | |
a1c9c9ff | 837 | ftype = strip_array_types (ftype); |
2d76680f PC |
838 | if (type_has_constexpr_default_constructor (ftype)) |
839 | { | |
840 | /* It's OK to skip a member with a trivial constexpr ctor. | |
841 | A constexpr ctor that isn't trivial should have been | |
842 | added in by now. */ | |
843 | gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype) | |
844 | || errorcount != 0); | |
845 | continue; | |
846 | } | |
847 | if (!complain) | |
848 | return true; | |
097f82ec | 849 | auto_diagnostic_group d; |
b8cd3996 JM |
850 | error ("member %qD must be initialized by mem-initializer " |
851 | "in %<constexpr%> constructor", field); | |
852 | inform (DECL_SOURCE_LOCATION (field), "declared here"); | |
2d76680f PC |
853 | bad = true; |
854 | } | |
855 | if (field == NULL_TREE) | |
856 | break; | |
3e4b91f2 NS |
857 | |
858 | if (ANON_AGGR_TYPE_P (TREE_TYPE (index))) | |
859 | { | |
860 | /* Check the anonymous aggregate initializer is valid. */ | |
861 | bad |= cx_check_missing_mem_inits | |
862 | (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain); | |
863 | if (bad && !complain) | |
864 | return true; | |
865 | } | |
2d76680f PC |
866 | field = DECL_CHAIN (field); |
867 | } | |
868 | ||
869 | return bad; | |
870 | } | |
871 | ||
872 | /* We are processing the definition of the constexpr function FUN. | |
bfc139e2 NS |
873 | Check that its body fulfills the apropriate requirements and |
874 | enter it in the constexpr function definition table. */ | |
2d76680f | 875 | |
bfc139e2 NS |
876 | void |
877 | maybe_save_constexpr_fundef (tree fun) | |
2d76680f | 878 | { |
bfc139e2 | 879 | if (processing_template_decl |
bfc139e2 | 880 | || cp_function_chain->invalid_constexpr |
81f97181 | 881 | || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))) |
bfc139e2 | 882 | return; |
2d76680f | 883 | |
87c2080b JM |
884 | /* With -fimplicit-constexpr, try to make inlines constexpr. We'll |
885 | actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass. */ | |
886 | bool implicit = false; | |
887 | if (flag_implicit_constexpr) | |
888 | { | |
889 | if (DECL_DELETING_DESTRUCTOR_P (fun) | |
890 | && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun))) | |
891 | /* Don't inherit implicit constexpr from the non-deleting | |
892 | destructor. */ | |
893 | DECL_DECLARED_CONSTEXPR_P (fun) = false; | |
894 | ||
895 | if (!DECL_DECLARED_CONSTEXPR_P (fun) | |
896 | && DECL_DECLARED_INLINE_P (fun) | |
897 | && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun))) | |
898 | implicit = true; | |
899 | } | |
900 | ||
901 | if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit) | |
902 | return; | |
903 | ||
904 | bool complain = !DECL_GENERATED_P (fun) && !implicit; | |
37326651 JM |
905 | |
906 | if (!is_valid_constexpr_fn (fun, complain)) | |
bfc139e2 | 907 | return; |
2d76680f | 908 | |
bfc139e2 | 909 | tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun)); |
ca30abcd | 910 | if (massaged == NULL_TREE || massaged == error_mark_node) |
2d76680f | 911 | { |
87c2080b | 912 | if (!DECL_CONSTRUCTOR_P (fun) && complain) |
84fa214d ML |
913 | error ("body of %<constexpr%> function %qD not a return-statement", |
914 | fun); | |
bfc139e2 | 915 | return; |
2d76680f PC |
916 | } |
917 | ||
4f05d85a | 918 | bool potential = potential_rvalue_constant_expression (massaged); |
37326651 | 919 | if (!potential && complain) |
4f05d85a | 920 | require_potential_rvalue_constant_expression (massaged); |
2d76680f | 921 | |
c5de3444 JM |
922 | if (DECL_CONSTRUCTOR_P (fun) && potential |
923 | && !DECL_DEFAULTED_FN (fun)) | |
37326651 JM |
924 | { |
925 | if (cx_check_missing_mem_inits (DECL_CONTEXT (fun), | |
926 | massaged, complain)) | |
927 | potential = false; | |
928 | else if (cxx_dialect > cxx11) | |
929 | { | |
930 | /* What we got from massage_constexpr_body is pretty much just the | |
931 | ctor-initializer, also check the body. */ | |
932 | massaged = DECL_SAVED_TREE (fun); | |
933 | potential = potential_rvalue_constant_expression (massaged); | |
934 | if (!potential && complain) | |
935 | require_potential_rvalue_constant_expression (massaged); | |
936 | } | |
937 | } | |
4f05d85a | 938 | |
37326651 | 939 | if (!potential && complain) |
bfc139e2 | 940 | return; |
2d76680f | 941 | |
87c2080b JM |
942 | if (implicit) |
943 | { | |
944 | if (potential) | |
945 | { | |
946 | DECL_DECLARED_CONSTEXPR_P (fun) = true; | |
947 | DECL_LANG_SPECIFIC (fun)->u.fn.implicit_constexpr = true; | |
948 | if (DECL_CONSTRUCTOR_P (fun)) | |
949 | TYPE_HAS_CONSTEXPR_CTOR (DECL_CONTEXT (fun)) = true; | |
950 | } | |
951 | else | |
952 | /* Don't bother keeping the pre-generic body of unsuitable functions | |
953 | not explicitly declared constexpr. */ | |
954 | return; | |
955 | } | |
956 | ||
bfc139e2 | 957 | constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE}; |
43574e4f | 958 | bool clear_ctx = false; |
43574e4f JJ |
959 | if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE) |
960 | { | |
961 | clear_ctx = true; | |
962 | DECL_CONTEXT (DECL_RESULT (fun)) = fun; | |
963 | } | |
bfc139e2 NS |
964 | tree saved_fn = current_function_decl; |
965 | current_function_decl = fun; | |
966 | entry.body = copy_fn (entry.decl, entry.parms, entry.result); | |
43574e4f | 967 | current_function_decl = saved_fn; |
43574e4f | 968 | if (clear_ctx) |
bfc139e2 | 969 | DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE; |
4f05d85a JM |
970 | if (!potential) |
971 | /* For a template instantiation, we want to remember the pre-generic body | |
972 | for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression | |
973 | that it doesn't need to bother trying to expand the function. */ | |
974 | entry.result = error_mark_node; | |
975 | ||
bfc139e2 NS |
976 | register_constexpr_fundef (entry); |
977 | } | |
978 | ||
979 | /* BODY is a validated and massaged definition of a constexpr | |
980 | function. Register it in the hash table. */ | |
981 | ||
982 | void | |
983 | register_constexpr_fundef (const constexpr_fundef &value) | |
984 | { | |
985 | /* Create the constexpr function table if necessary. */ | |
986 | if (constexpr_fundef_table == NULL) | |
987 | constexpr_fundef_table | |
988 | = hash_table<constexpr_fundef_hasher>::create_ggc (101); | |
989 | ||
990 | constexpr_fundef **slot = constexpr_fundef_table->find_slot | |
991 | (const_cast<constexpr_fundef *> (&value), INSERT); | |
992 | ||
2d76680f PC |
993 | gcc_assert (*slot == NULL); |
994 | *slot = ggc_alloc<constexpr_fundef> (); | |
bfc139e2 | 995 | **slot = value; |
2d76680f PC |
996 | } |
997 | ||
998 | /* FUN is a non-constexpr function called in a context that requires a | |
999 | constant expression. If it comes from a constexpr template, explain why | |
1000 | the instantiation isn't constexpr. */ | |
1001 | ||
1002 | void | |
1003 | explain_invalid_constexpr_fn (tree fun) | |
1004 | { | |
1005 | static hash_set<tree> *diagnosed; | |
1006 | tree body; | |
98e5a19a | 1007 | /* Only diagnose defaulted functions, lambdas, or instantiations. */ |
2d76680f | 1008 | if (!DECL_DEFAULTED_FN (fun) |
98e5a19a | 1009 | && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) |
2d76680f | 1010 | && !is_instantiation_of_constexpr (fun)) |
f22f817c JM |
1011 | { |
1012 | inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun); | |
1013 | return; | |
1014 | } | |
2d76680f PC |
1015 | if (diagnosed == NULL) |
1016 | diagnosed = new hash_set<tree>; | |
1017 | if (diagnosed->add (fun)) | |
1018 | /* Already explained. */ | |
1019 | return; | |
1020 | ||
17dc903e | 1021 | iloc_sentinel ils = input_location; |
98e5a19a JM |
1022 | if (!lambda_static_thunk_p (fun)) |
1023 | { | |
1024 | /* Diagnostics should completely ignore the static thunk, so leave | |
1025 | input_location set to our caller's location. */ | |
1026 | input_location = DECL_SOURCE_LOCATION (fun); | |
1027 | inform (input_location, | |
84fa214d | 1028 | "%qD is not usable as a %<constexpr%> function because:", fun); |
98e5a19a | 1029 | } |
2d76680f PC |
1030 | /* First check the declaration. */ |
1031 | if (is_valid_constexpr_fn (fun, true)) | |
1032 | { | |
1033 | /* Then if it's OK, the body. */ | |
98e5a19a | 1034 | if (!DECL_DECLARED_CONSTEXPR_P (fun) |
4f05d85a | 1035 | && DECL_DEFAULTED_FN (fun)) |
2d76680f PC |
1036 | explain_implicit_non_constexpr (fun); |
1037 | else | |
1038 | { | |
4f05d85a JM |
1039 | if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun)) |
1040 | body = fd->body; | |
1041 | else | |
1042 | body = DECL_SAVED_TREE (fun); | |
1043 | body = massage_constexpr_body (fun, body); | |
2d76680f PC |
1044 | require_potential_rvalue_constant_expression (body); |
1045 | if (DECL_CONSTRUCTOR_P (fun)) | |
3e4b91f2 | 1046 | cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true); |
2d76680f PC |
1047 | } |
1048 | } | |
2d76680f PC |
1049 | } |
1050 | ||
1051 | /* Objects of this type represent calls to constexpr functions | |
1052 | along with the bindings of parameters to their arguments, for | |
1053 | the purpose of compile time evaluation. */ | |
1054 | ||
1055 | struct GTY((for_user)) constexpr_call { | |
1056 | /* Description of the constexpr function definition. */ | |
1057 | constexpr_fundef *fundef; | |
3c961dc7 | 1058 | /* Parameter bindings environment. A TREE_VEC of arguments. */ |
2d76680f PC |
1059 | tree bindings; |
1060 | /* Result of the call. | |
1061 | NULL means the call is being evaluated. | |
1062 | error_mark_node means that the evaluation was erroneous; | |
1063 | otherwise, the actuall value of the call. */ | |
1064 | tree result; | |
1065 | /* The hash of this call; we remember it here to avoid having to | |
1066 | recalculate it when expanding the hash table. */ | |
1067 | hashval_t hash; | |
cce3ae91 | 1068 | /* Whether __builtin_is_constant_evaluated() should evaluate to true. */ |
13de99bc | 1069 | bool manifestly_const_eval; |
2d76680f PC |
1070 | }; |
1071 | ||
ca752f39 | 1072 | struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call> |
2d76680f PC |
1073 | { |
1074 | static hashval_t hash (constexpr_call *); | |
1075 | static bool equal (constexpr_call *, constexpr_call *); | |
3e605b20 JM |
1076 | }; |
1077 | ||
4b390698 JJ |
1078 | enum constexpr_switch_state { |
1079 | /* Used when processing a switch for the first time by cxx_eval_switch_expr | |
1080 | and default: label for that switch has not been seen yet. */ | |
1081 | css_default_not_seen, | |
1082 | /* Used when processing a switch for the first time by cxx_eval_switch_expr | |
1083 | and default: label for that switch has been seen already. */ | |
1084 | css_default_seen, | |
1085 | /* Used when processing a switch for the second time by | |
1086 | cxx_eval_switch_expr, where default: label should match. */ | |
1087 | css_default_processing | |
1088 | }; | |
1089 | ||
8e007055 JJ |
1090 | /* The constexpr expansion context part which needs one instance per |
1091 | cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of | |
1092 | variables initialized within the expression. */ | |
1093 | ||
1094 | struct constexpr_global_ctx { | |
1095 | /* Values for any temporaries or local variables within the | |
1096 | constant-expression. */ | |
1097 | hash_map<tree,tree> values; | |
1098 | /* Number of cxx_eval_constant_expression calls (except skipped ones, | |
1099 | on simple constants or location wrappers) encountered during current | |
1100 | cxx_eval_outermost_constant_expr call. */ | |
1101 | HOST_WIDE_INT constexpr_ops_count; | |
1102 | /* Heap VAR_DECLs created during the evaluation of the outermost constant | |
1103 | expression. */ | |
1104 | auto_vec<tree, 16> heap_vars; | |
ee1de08d JJ |
1105 | /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */ |
1106 | vec<tree> *cleanups; | |
f74f6092 JJ |
1107 | /* Number of heap VAR_DECL deallocations. */ |
1108 | unsigned heap_dealloc_count; | |
8e007055 | 1109 | /* Constructor. */ |
f74f6092 JJ |
1110 | constexpr_global_ctx () |
1111 | : constexpr_ops_count (0), cleanups (NULL), heap_dealloc_count (0) {} | |
8e007055 JJ |
1112 | }; |
1113 | ||
3e605b20 JM |
1114 | /* The constexpr expansion context. CALL is the current function |
1115 | expansion, CTOR is the current aggregate initializer, OBJECT is the | |
8e007055 | 1116 | object being initialized by CTOR, either a VAR_DECL or a _REF. */ |
3e605b20 JM |
1117 | |
1118 | struct constexpr_ctx { | |
8e007055 JJ |
1119 | /* The part of the context that needs to be unique to the whole |
1120 | cxx_eval_outermost_constant_expr invocation. */ | |
1121 | constexpr_global_ctx *global; | |
13f649f6 | 1122 | /* The innermost call we're evaluating. */ |
3e605b20 | 1123 | constexpr_call *call; |
ee1de08d JJ |
1124 | /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen |
1125 | within the current LOOP_EXPR. NULL if we aren't inside a loop. */ | |
0ee28590 | 1126 | vec<tree> *save_exprs; |
13f649f6 JM |
1127 | /* The CONSTRUCTOR we're currently building up for an aggregate |
1128 | initializer. */ | |
3e605b20 | 1129 | tree ctor; |
13f649f6 | 1130 | /* The object we're building the CONSTRUCTOR for. */ |
3e605b20 | 1131 | tree object; |
4b390698 JJ |
1132 | /* If inside SWITCH_EXPR. */ |
1133 | constexpr_switch_state *css_state; | |
49a86fce PP |
1134 | /* The aggregate initialization context inside which this one is nested. This |
1135 | is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */ | |
1136 | const constexpr_ctx *parent; | |
a15ffa22 | 1137 | |
aaa26bf4 JM |
1138 | /* Whether we should error on a non-constant expression or fail quietly. |
1139 | This flag needs to be here, but some of the others could move to global | |
1140 | if they get larger than a word. */ | |
2b3ab879 | 1141 | bool quiet; |
13f649f6 JM |
1142 | /* Whether we are strictly conforming to constant expression rules or |
1143 | trying harder to get a constant value. */ | |
69eb4fde | 1144 | bool strict; |
e4082611 | 1145 | /* Whether __builtin_is_constant_evaluated () should be true. */ |
13de99bc | 1146 | bool manifestly_const_eval; |
3e605b20 | 1147 | }; |
2d76680f | 1148 | |
f65a3299 PP |
1149 | /* This internal flag controls whether we should avoid doing anything during |
1150 | constexpr evaluation that would cause extra DECL_UID generation, such as | |
1151 | template instantiation and function body copying. */ | |
1152 | ||
1153 | static bool uid_sensitive_constexpr_evaluation_value; | |
1154 | ||
1155 | /* An internal counter that keeps track of the number of times | |
1156 | uid_sensitive_constexpr_evaluation_p returned true. */ | |
1157 | ||
1158 | static unsigned uid_sensitive_constexpr_evaluation_true_counter; | |
1159 | ||
1160 | /* The accessor for uid_sensitive_constexpr_evaluation_value which also | |
1161 | increments the corresponding counter. */ | |
1162 | ||
1163 | static bool | |
1164 | uid_sensitive_constexpr_evaluation_p () | |
1165 | { | |
1166 | if (uid_sensitive_constexpr_evaluation_value) | |
1167 | { | |
1168 | ++uid_sensitive_constexpr_evaluation_true_counter; | |
1169 | return true; | |
1170 | } | |
1171 | else | |
1172 | return false; | |
1173 | } | |
1174 | ||
1175 | /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel | |
1176 | enables the internal flag for uid_sensitive_constexpr_evaluation_p | |
1177 | during the lifetime of the sentinel object. Upon its destruction, the | |
1178 | previous value of uid_sensitive_constexpr_evaluation_p is restored. */ | |
1179 | ||
1180 | uid_sensitive_constexpr_evaluation_sentinel | |
1181 | ::uid_sensitive_constexpr_evaluation_sentinel () | |
1182 | : ovr (uid_sensitive_constexpr_evaluation_value, true) | |
1183 | { | |
1184 | } | |
1185 | ||
1186 | /* The default constructor for uid_sensitive_constexpr_evaluation_checker | |
1187 | records the current number of times that uid_sensitive_constexpr_evaluation_p | |
1188 | has been called and returned true. */ | |
1189 | ||
1190 | uid_sensitive_constexpr_evaluation_checker | |
1191 | ::uid_sensitive_constexpr_evaluation_checker () | |
1192 | : saved_counter (uid_sensitive_constexpr_evaluation_true_counter) | |
1193 | { | |
1194 | } | |
1195 | ||
1196 | /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and | |
1197 | some constexpr evaluation was restricted due to u_s_c_e_p being called | |
1198 | and returning true during the lifetime of this checker object. */ | |
1199 | ||
1200 | bool | |
1201 | uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const | |
1202 | { | |
1203 | return (uid_sensitive_constexpr_evaluation_value | |
1204 | && saved_counter != uid_sensitive_constexpr_evaluation_true_counter); | |
1205 | } | |
1206 | ||
1207 | ||
2d76680f PC |
1208 | /* A table of all constexpr calls that have been evaluated by the |
1209 | compiler in this translation unit. */ | |
1210 | ||
97f3003f | 1211 | static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table; |
2d76680f | 1212 | |
2d76680f PC |
1213 | /* Compute a hash value for a constexpr call representation. */ |
1214 | ||
1215 | inline hashval_t | |
1216 | constexpr_call_hasher::hash (constexpr_call *info) | |
1217 | { | |
1218 | return info->hash; | |
1219 | } | |
1220 | ||
1221 | /* Return true if the objects pointed to by P and Q represent calls | |
1222 | to the same constexpr function with the same arguments. | |
1223 | Otherwise, return false. */ | |
1224 | ||
1225 | bool | |
1226 | constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs) | |
1227 | { | |
2d76680f | 1228 | if (lhs == rhs) |
46cf7fa1 JJ |
1229 | return true; |
1230 | if (lhs->hash != rhs->hash) | |
1231 | return false; | |
13de99bc | 1232 | if (lhs->manifestly_const_eval != rhs->manifestly_const_eval) |
cce3ae91 | 1233 | return false; |
2d76680f | 1234 | if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef)) |
46cf7fa1 | 1235 | return false; |
3c961dc7 | 1236 | return cp_tree_equal (lhs->bindings, rhs->bindings); |
2d76680f PC |
1237 | } |
1238 | ||
1239 | /* Initialize the constexpr call table, if needed. */ | |
1240 | ||
1241 | static void | |
1242 | maybe_initialize_constexpr_call_table (void) | |
1243 | { | |
1244 | if (constexpr_call_table == NULL) | |
1245 | constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101); | |
1246 | } | |
1247 | ||
c0daf32d PP |
1248 | /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when |
1249 | a function happens to get called recursively, we unshare the callee | |
1250 | function's body and evaluate this unshared copy instead of evaluating the | |
1251 | original body. | |
1252 | ||
1253 | FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function | |
1254 | copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map | |
97f3003f JM |
1255 | that's keyed off of the original FUNCTION_DECL and whose value is a |
1256 | TREE_LIST of this function's unused copies awaiting reuse. | |
c0daf32d | 1257 | |
97f3003f | 1258 | This is not GC-deletable to avoid GC affecting UID generation. */ |
c0daf32d | 1259 | |
ba6730bd | 1260 | static GTY(()) decl_tree_map *fundef_copies_table; |
c0daf32d | 1261 | |
c0daf32d | 1262 | /* Reuse a copy or create a new unshared copy of the function FUN. |
3c98ff9b NS |
1263 | Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE |
1264 | is parms, TYPE is result. */ | |
c0daf32d | 1265 | |
97f3003f | 1266 | static tree |
f65a3299 | 1267 | get_fundef_copy (constexpr_fundef *fundef) |
c0daf32d | 1268 | { |
97f3003f | 1269 | tree copy; |
3c98ff9b | 1270 | bool existed; |
c89844e5 JM |
1271 | tree *slot = &(hash_map_safe_get_or_insert<hm_ggc> |
1272 | (fundef_copies_table, fundef->decl, &existed, 127)); | |
3c98ff9b NS |
1273 | |
1274 | if (!existed) | |
c0daf32d | 1275 | { |
3c98ff9b NS |
1276 | /* There is no cached function available, or in use. We can use |
1277 | the function directly. That the slot is now created records | |
1278 | that this function is now in use. */ | |
43574e4f JJ |
1279 | copy = build_tree_list (fundef->body, fundef->parms); |
1280 | TREE_TYPE (copy) = fundef->result; | |
3c98ff9b NS |
1281 | } |
1282 | else if (*slot == NULL_TREE) | |
1283 | { | |
f65a3299 | 1284 | if (uid_sensitive_constexpr_evaluation_p ()) |
aaa26bf4 JM |
1285 | return NULL_TREE; |
1286 | ||
3c98ff9b | 1287 | /* We've already used the function itself, so make a copy. */ |
97f3003f | 1288 | copy = build_tree_list (NULL, NULL); |
43574e4f JJ |
1289 | tree saved_body = DECL_SAVED_TREE (fundef->decl); |
1290 | tree saved_parms = DECL_ARGUMENTS (fundef->decl); | |
1291 | tree saved_result = DECL_RESULT (fundef->decl); | |
1292 | tree saved_fn = current_function_decl; | |
1293 | DECL_SAVED_TREE (fundef->decl) = fundef->body; | |
1294 | DECL_ARGUMENTS (fundef->decl) = fundef->parms; | |
1295 | DECL_RESULT (fundef->decl) = fundef->result; | |
1296 | current_function_decl = fundef->decl; | |
1297 | TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy), | |
1298 | TREE_TYPE (copy)); | |
1299 | current_function_decl = saved_fn; | |
1300 | DECL_RESULT (fundef->decl) = saved_result; | |
1301 | DECL_ARGUMENTS (fundef->decl) = saved_parms; | |
1302 | DECL_SAVED_TREE (fundef->decl) = saved_body; | |
c0daf32d PP |
1303 | } |
1304 | else | |
1305 | { | |
3c98ff9b | 1306 | /* We have a cached function available. */ |
c0daf32d | 1307 | copy = *slot; |
97f3003f | 1308 | *slot = TREE_CHAIN (copy); |
c0daf32d PP |
1309 | } |
1310 | ||
1311 | return copy; | |
1312 | } | |
1313 | ||
3c98ff9b NS |
1314 | /* Save the copy COPY of function FUN for later reuse by |
1315 | get_fundef_copy(). By construction, there will always be an entry | |
1316 | to find. */ | |
c0daf32d PP |
1317 | |
1318 | static void | |
97f3003f | 1319 | save_fundef_copy (tree fun, tree copy) |
c0daf32d | 1320 | { |
3c98ff9b | 1321 | tree *slot = fundef_copies_table->get (fun); |
97f3003f | 1322 | TREE_CHAIN (copy) = *slot; |
c0daf32d PP |
1323 | *slot = copy; |
1324 | } | |
1325 | ||
2d76680f PC |
1326 | /* We have an expression tree T that represents a call, either CALL_EXPR |
1327 | or AGGR_INIT_EXPR. Return the Nth argument. */ | |
1328 | ||
1329 | static inline tree | |
1330 | get_nth_callarg (tree t, int n) | |
1331 | { | |
1332 | switch (TREE_CODE (t)) | |
1333 | { | |
1334 | case CALL_EXPR: | |
1335 | return CALL_EXPR_ARG (t, n); | |
1336 | ||
1337 | case AGGR_INIT_EXPR: | |
1338 | return AGGR_INIT_EXPR_ARG (t, n); | |
1339 | ||
1340 | default: | |
1341 | gcc_unreachable (); | |
1342 | return NULL; | |
1343 | } | |
1344 | } | |
1345 | ||
72f76540 JM |
1346 | /* Whether our evaluation wants a prvalue (e.g. CONSTRUCTOR or _CST), |
1347 | a glvalue (e.g. VAR_DECL or _REF), or nothing. */ | |
1348 | ||
1349 | enum value_cat { | |
1350 | vc_prvalue = 0, | |
1351 | vc_glvalue = 1, | |
1352 | vc_discard = 2 | |
1353 | }; | |
1354 | ||
1355 | static tree cxx_eval_constant_expression (const constexpr_ctx *, tree, | |
1356 | value_cat, bool *, bool *, tree * = NULL); | |
1189c038 JM |
1357 | static tree cxx_fold_indirect_ref (const constexpr_ctx *, location_t, tree, tree, |
1358 | bool * = NULL); | |
72f76540 | 1359 | |
2d76680f PC |
1360 | /* Attempt to evaluate T which represents a call to a builtin function. |
1361 | We assume here that all builtin functions evaluate to scalar types | |
1362 | represented by _CST nodes. */ | |
1363 | ||
1364 | static tree | |
5756d0f9 | 1365 | cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun, |
72f76540 | 1366 | value_cat lval, |
2d76680f PC |
1367 | bool *non_constant_p, bool *overflow_p) |
1368 | { | |
1369 | const int nargs = call_expr_nargs (t); | |
1370 | tree *args = (tree *) alloca (nargs * sizeof (tree)); | |
1371 | tree new_call; | |
1372 | int i; | |
5756d0f9 JM |
1373 | |
1374 | /* Don't fold __builtin_constant_p within a constexpr function. */ | |
e4082611 | 1375 | bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun); |
b925d25d | 1376 | |
fe736ffd JM |
1377 | /* If we aren't requiring a constant expression, defer __builtin_constant_p |
1378 | in a constexpr function until we have values for the parameters. */ | |
b925d25d | 1379 | if (bi_const_p |
4cd3e7df | 1380 | && !ctx->manifestly_const_eval |
5756d0f9 JM |
1381 | && current_function_decl |
1382 | && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) | |
2d76680f | 1383 | { |
5756d0f9 JM |
1384 | *non_constant_p = true; |
1385 | return t; | |
2d76680f | 1386 | } |
5756d0f9 | 1387 | |
e4082611 | 1388 | /* For __builtin_is_constant_evaluated, defer it if not |
117c6426 JJ |
1389 | ctx->manifestly_const_eval (as sometimes we try to constant evaluate |
1390 | without manifestly_const_eval even expressions or parts thereof which | |
1391 | will later be manifestly const_eval evaluated), otherwise fold it to | |
1392 | true. */ | |
3d78e008 | 1393 | if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED, |
13de99bc | 1394 | BUILT_IN_FRONTEND)) |
e4082611 | 1395 | { |
13de99bc | 1396 | if (!ctx->manifestly_const_eval) |
e4082611 JJ |
1397 | { |
1398 | *non_constant_p = true; | |
1399 | return t; | |
1400 | } | |
1401 | return boolean_true_node; | |
1402 | } | |
1403 | ||
ff603745 | 1404 | if (fndecl_built_in_p (fun, CP_BUILT_IN_SOURCE_LOCATION, BUILT_IN_FRONTEND)) |
ba3d8dff JJ |
1405 | { |
1406 | temp_override<tree> ovr (current_function_decl); | |
1407 | if (ctx->call && ctx->call->fundef) | |
1408 | current_function_decl = ctx->call->fundef->decl; | |
1409 | return fold_builtin_source_location (EXPR_LOCATION (t)); | |
1410 | } | |
ff603745 | 1411 | |
69dc042f JM |
1412 | int strops = 0; |
1413 | int strret = 0; | |
1414 | if (fndecl_built_in_p (fun, BUILT_IN_NORMAL)) | |
1415 | switch (DECL_FUNCTION_CODE (fun)) | |
1416 | { | |
1417 | case BUILT_IN_STRLEN: | |
1418 | case BUILT_IN_STRNLEN: | |
1419 | strops = 1; | |
1420 | break; | |
1421 | case BUILT_IN_MEMCHR: | |
1422 | case BUILT_IN_STRCHR: | |
1423 | case BUILT_IN_STRRCHR: | |
1424 | strops = 1; | |
1425 | strret = 1; | |
1426 | break; | |
1427 | case BUILT_IN_MEMCMP: | |
1428 | case BUILT_IN_STRCMP: | |
1429 | strops = 2; | |
1430 | break; | |
1431 | case BUILT_IN_STRSTR: | |
1432 | strops = 2; | |
1433 | strret = 1; | |
bc13106e JJ |
1434 | break; |
1435 | case BUILT_IN_ASAN_POINTER_COMPARE: | |
1436 | case BUILT_IN_ASAN_POINTER_SUBTRACT: | |
1437 | /* These builtins shall be ignored during constant expression | |
1438 | evaluation. */ | |
1439 | return void_node; | |
69dc042f JM |
1440 | default: |
1441 | break; | |
1442 | } | |
1443 | ||
5756d0f9 JM |
1444 | /* Be permissive for arguments to built-ins; __builtin_constant_p should |
1445 | return constant false for a non-constant argument. */ | |
1446 | constexpr_ctx new_ctx = *ctx; | |
1447 | new_ctx.quiet = true; | |
5756d0f9 | 1448 | for (i = 0; i < nargs; ++i) |
b925d25d | 1449 | { |
69dc042f | 1450 | tree arg = CALL_EXPR_ARG (t, i); |
6f346913 | 1451 | tree oarg = arg; |
69dc042f JM |
1452 | |
1453 | /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since | |
1454 | expand_builtin doesn't know how to look in the values table. */ | |
1455 | bool strop = i < strops; | |
1456 | if (strop) | |
1457 | { | |
1458 | STRIP_NOPS (arg); | |
1459 | if (TREE_CODE (arg) == ADDR_EXPR) | |
1460 | arg = TREE_OPERAND (arg, 0); | |
1461 | else | |
1462 | strop = false; | |
1463 | } | |
1464 | ||
2ff7172a JJ |
1465 | /* If builtin_valid_in_constant_expr_p is true, |
1466 | potential_constant_expression_1 has not recursed into the arguments | |
1467 | of the builtin, verify it here. */ | |
1468 | if (!builtin_valid_in_constant_expr_p (fun) | |
69dc042f | 1469 | || potential_constant_expression (arg)) |
4cd3e7df JJ |
1470 | { |
1471 | bool dummy1 = false, dummy2 = false; | |
72f76540 | 1472 | arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue, |
69dc042f | 1473 | &dummy1, &dummy2); |
4cd3e7df JJ |
1474 | } |
1475 | ||
b925d25d | 1476 | if (bi_const_p) |
4cd3e7df | 1477 | /* For __builtin_constant_p, fold all expressions with constant values |
b925d25d | 1478 | even if they aren't C++ constant-expressions. */ |
69dc042f JM |
1479 | arg = cp_fold_rvalue (arg); |
1480 | else if (strop) | |
1481 | { | |
1482 | if (TREE_CODE (arg) == CONSTRUCTOR) | |
1483 | arg = braced_lists_to_strings (TREE_TYPE (arg), arg); | |
1484 | if (TREE_CODE (arg) == STRING_CST) | |
1485 | arg = build_address (arg); | |
6f346913 JM |
1486 | else |
1487 | arg = oarg; | |
69dc042f JM |
1488 | } |
1489 | ||
1490 | args[i] = arg; | |
b925d25d | 1491 | } |
5756d0f9 JM |
1492 | |
1493 | bool save_ffbcp = force_folding_builtin_constant_p; | |
b09a67ea | 1494 | force_folding_builtin_constant_p |= ctx->manifestly_const_eval; |
43574e4f JJ |
1495 | tree save_cur_fn = current_function_decl; |
1496 | /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */ | |
1497 | if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION) | |
1498 | && ctx->call | |
1499 | && ctx->call->fundef) | |
1500 | current_function_decl = ctx->call->fundef->decl; | |
6cd005a2 JJ |
1501 | if (fndecl_built_in_p (fun, |
1502 | CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS, | |
1503 | BUILT_IN_FRONTEND)) | |
1504 | { | |
1505 | location_t loc = EXPR_LOCATION (t); | |
1506 | if (nargs >= 1) | |
1507 | VERIFY_CONSTANT (args[0]); | |
1508 | new_call | |
1509 | = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs, | |
1510 | args); | |
1511 | } | |
32c3a753 JJ |
1512 | else if (fndecl_built_in_p (fun, |
1513 | CP_BUILT_IN_IS_CORRESPONDING_MEMBER, | |
1514 | BUILT_IN_FRONTEND)) | |
1515 | { | |
1516 | location_t loc = EXPR_LOCATION (t); | |
1517 | if (nargs >= 2) | |
1518 | { | |
1519 | VERIFY_CONSTANT (args[0]); | |
1520 | VERIFY_CONSTANT (args[1]); | |
1521 | } | |
1522 | new_call = fold_builtin_is_corresponding_member (loc, nargs, args); | |
1523 | } | |
6cd005a2 JJ |
1524 | else |
1525 | new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t), | |
1526 | CALL_EXPR_FN (t), nargs, args); | |
43574e4f | 1527 | current_function_decl = save_cur_fn; |
5756d0f9 | 1528 | force_folding_builtin_constant_p = save_ffbcp; |
109d2197 JJ |
1529 | if (new_call == NULL) |
1530 | { | |
1531 | if (!*non_constant_p && !ctx->quiet) | |
1532 | { | |
1b6fa695 ML |
1533 | /* Do not allow__builtin_unreachable in constexpr function. |
1534 | The __builtin_unreachable call with BUILTINS_LOCATION | |
1535 | comes from cp_maybe_instrument_return. */ | |
3d78e008 | 1536 | if (fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE) |
1b6fa695 | 1537 | && EXPR_LOCATION (t) == BUILTINS_LOCATION) |
84fa214d | 1538 | error ("%<constexpr%> call flows off the end of the function"); |
1b6fa695 ML |
1539 | else |
1540 | { | |
1541 | new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t), | |
1542 | CALL_EXPR_FN (t), nargs, args); | |
1543 | error ("%q+E is not a constant expression", new_call); | |
1544 | } | |
109d2197 JJ |
1545 | } |
1546 | *non_constant_p = true; | |
1547 | return t; | |
1548 | } | |
1549 | ||
43574e4f | 1550 | if (!potential_constant_expression (new_call)) |
109d2197 JJ |
1551 | { |
1552 | if (!*non_constant_p && !ctx->quiet) | |
1553 | error ("%q+E is not a constant expression", new_call); | |
1554 | *non_constant_p = true; | |
1555 | return t; | |
1556 | } | |
1557 | ||
69dc042f JM |
1558 | if (strret) |
1559 | { | |
1560 | /* memchr returns a pointer into the first argument, but we replaced the | |
1561 | argument above with a STRING_CST; put it back it now. */ | |
1562 | tree op = CALL_EXPR_ARG (t, strret-1); | |
1563 | STRIP_NOPS (new_call); | |
1564 | if (TREE_CODE (new_call) == POINTER_PLUS_EXPR) | |
1565 | TREE_OPERAND (new_call, 0) = op; | |
1566 | else if (TREE_CODE (new_call) == ADDR_EXPR) | |
1567 | new_call = op; | |
1568 | } | |
1569 | ||
109d2197 JJ |
1570 | return cxx_eval_constant_expression (&new_ctx, new_call, lval, |
1571 | non_constant_p, overflow_p); | |
2d76680f PC |
1572 | } |
1573 | ||
1574 | /* TEMP is the constant value of a temporary object of type TYPE. Adjust | |
1575 | the type of the value to match. */ | |
1576 | ||
1577 | static tree | |
1578 | adjust_temp_type (tree type, tree temp) | |
1579 | { | |
47be9509 | 1580 | if (same_type_p (TREE_TYPE (temp), type)) |
2d76680f PC |
1581 | return temp; |
1582 | /* Avoid wrapping an aggregate value in a NOP_EXPR. */ | |
1583 | if (TREE_CODE (temp) == CONSTRUCTOR) | |
b27f74e7 MP |
1584 | { |
1585 | /* build_constructor wouldn't retain various CONSTRUCTOR flags. */ | |
1586 | tree t = copy_node (temp); | |
1587 | TREE_TYPE (t) = type; | |
1588 | return t; | |
1589 | } | |
ff8ba86f JJ |
1590 | if (TREE_CODE (temp) == EMPTY_CLASS_EXPR) |
1591 | return build0 (EMPTY_CLASS_EXPR, type); | |
2d76680f | 1592 | gcc_assert (scalarish_type_p (type)); |
72b091f7 MP |
1593 | /* Now we know we're dealing with a scalar, and a prvalue of non-class |
1594 | type is cv-unqualified. */ | |
1595 | return cp_fold_convert (cv_unqualified (type), temp); | |
2d76680f PC |
1596 | } |
1597 | ||
9b9eb42a JM |
1598 | /* If T is a CONSTRUCTOR, return an unshared copy of T and any |
1599 | sub-CONSTRUCTORs. Otherwise return T. | |
0146e25f | 1600 | |
9b9eb42a JM |
1601 | We use this whenever we initialize an object as a whole, whether it's a |
1602 | parameter, a local variable, or a subobject, so that subsequent | |
1603 | modifications don't affect other places where it was used. */ | |
0146e25f | 1604 | |
1f6857ba | 1605 | tree |
3da7d774 | 1606 | unshare_constructor (tree t MEM_STAT_DECL) |
0146e25f | 1607 | { |
9b9eb42a JM |
1608 | if (!t || TREE_CODE (t) != CONSTRUCTOR) |
1609 | return t; | |
1610 | auto_vec <tree*, 4> ptrs; | |
1611 | ptrs.safe_push (&t); | |
1612 | while (!ptrs.is_empty ()) | |
1613 | { | |
1614 | tree *p = ptrs.pop (); | |
3da7d774 JM |
1615 | tree n = copy_node (*p PASS_MEM_STAT); |
1616 | CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT); | |
9b9eb42a JM |
1617 | *p = n; |
1618 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n); | |
1619 | constructor_elt *ce; | |
1620 | for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
6b7d53a2 | 1621 | if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR) |
9b9eb42a JM |
1622 | ptrs.safe_push (&ce->value); |
1623 | } | |
0146e25f PP |
1624 | return t; |
1625 | } | |
1626 | ||
620adbec JM |
1627 | /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */ |
1628 | ||
1629 | static void | |
1630 | free_constructor (tree t) | |
1631 | { | |
1632 | if (!t || TREE_CODE (t) != CONSTRUCTOR) | |
1633 | return; | |
1634 | releasing_vec ctors; | |
1635 | vec_safe_push (ctors, t); | |
1636 | while (!ctors->is_empty ()) | |
1637 | { | |
1638 | tree c = ctors->pop (); | |
1639 | if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c)) | |
1640 | { | |
1641 | constructor_elt *ce; | |
1642 | for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i) | |
1643 | if (TREE_CODE (ce->value) == CONSTRUCTOR) | |
1644 | vec_safe_push (ctors, ce->value); | |
1645 | ggc_free (elts); | |
1646 | } | |
1647 | ggc_free (c); | |
1648 | } | |
1649 | } | |
1650 | ||
559d2f1e JJ |
1651 | /* Helper function of cxx_bind_parameters_in_call. Return non-NULL |
1652 | if *TP is address of a static variable (or part of it) currently being | |
1653 | constructed or of a heap artificial variable. */ | |
1654 | ||
1655 | static tree | |
1656 | addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data) | |
1657 | { | |
1658 | if (TREE_CODE (*tp) == ADDR_EXPR) | |
1659 | if (tree var = get_base_address (TREE_OPERAND (*tp, 0))) | |
1660 | if (VAR_P (var) && TREE_STATIC (var)) | |
1661 | { | |
1662 | if (DECL_NAME (var) == heap_uninit_identifier | |
1663 | || DECL_NAME (var) == heap_identifier | |
1664 | || DECL_NAME (var) == heap_vec_uninit_identifier | |
1665 | || DECL_NAME (var) == heap_vec_identifier) | |
1666 | return var; | |
1667 | ||
1668 | constexpr_global_ctx *global = (constexpr_global_ctx *) data; | |
1669 | if (global->values.get (var)) | |
1670 | return var; | |
1671 | } | |
1672 | if (TYPE_P (*tp)) | |
1673 | *walk_subtrees = false; | |
1674 | return NULL_TREE; | |
1675 | } | |
1676 | ||
2d76680f PC |
1677 | /* Subroutine of cxx_eval_call_expression. |
1678 | We are processing a call expression (either CALL_EXPR or | |
3e605b20 | 1679 | AGGR_INIT_EXPR) in the context of CTX. Evaluate |
2d76680f PC |
1680 | all arguments and bind their values to correspondings |
1681 | parameters, making up the NEW_CALL context. */ | |
1682 | ||
1595fe44 JM |
1683 | static tree |
1684 | cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun, | |
12d9ce19 JM |
1685 | bool *non_constant_p, bool *overflow_p, |
1686 | bool *non_constant_args) | |
2d76680f PC |
1687 | { |
1688 | const int nargs = call_expr_nargs (t); | |
1595fe44 | 1689 | tree parms = DECL_ARGUMENTS (fun); |
2d76680f | 1690 | int i; |
3c961dc7 JM |
1691 | /* We don't record ellipsis args below. */ |
1692 | int nparms = list_length (parms); | |
1693 | int nbinds = nargs < nparms ? nargs : nparms; | |
1595fe44 | 1694 | tree binds = make_tree_vec (nbinds); |
2d76680f PC |
1695 | for (i = 0; i < nargs; ++i) |
1696 | { | |
1697 | tree x, arg; | |
1698 | tree type = parms ? TREE_TYPE (parms) : void_type_node; | |
1595fe44 JM |
1699 | if (parms && DECL_BY_REFERENCE (parms)) |
1700 | type = TREE_TYPE (type); | |
2d76680f | 1701 | x = get_nth_callarg (t, i); |
3e605b20 JM |
1702 | /* For member function, the first argument is a pointer to the implied |
1703 | object. For a constructor, it might still be a dummy object, in | |
60813a46 | 1704 | which case we get the real argument from ctx. */ |
3e605b20 JM |
1705 | if (i == 0 && DECL_CONSTRUCTOR_P (fun) |
1706 | && is_dummy_object (x)) | |
1707 | { | |
1708 | x = ctx->object; | |
84dd815f | 1709 | x = build_address (x); |
3e605b20 | 1710 | } |
43574e4f JJ |
1711 | if (TREE_ADDRESSABLE (type)) |
1712 | /* Undo convert_for_arg_passing work here. */ | |
1713 | x = convert_from_reference (x); | |
6c59b8a9 JM |
1714 | /* Normally we would strip a TARGET_EXPR in an initialization context |
1715 | such as this, but here we do the elision differently: we keep the | |
1716 | TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */ | |
72f76540 | 1717 | arg = cxx_eval_constant_expression (ctx, x, vc_prvalue, |
5a804683 | 1718 | non_constant_p, overflow_p); |
2d76680f | 1719 | /* Don't VERIFY_CONSTANT here. */ |
2b3ab879 | 1720 | if (*non_constant_p && ctx->quiet) |
1595fe44 | 1721 | break; |
2d76680f PC |
1722 | /* Just discard ellipsis args after checking their constantitude. */ |
1723 | if (!parms) | |
1724 | continue; | |
4727d4c6 NS |
1725 | |
1726 | if (!*non_constant_p) | |
1727 | { | |
1728 | /* Make sure the binding has the same type as the parm. But | |
1729 | only for constant args. */ | |
9f613f06 | 1730 | if (!TYPE_REF_P (type)) |
4727d4c6 NS |
1731 | arg = adjust_temp_type (type, arg); |
1732 | if (!TREE_CONSTANT (arg)) | |
1733 | *non_constant_args = true; | |
5afd90c5 JJ |
1734 | else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
1735 | /* The destructor needs to see any modifications the callee makes | |
1736 | to the argument. */ | |
1737 | *non_constant_args = true; | |
559d2f1e JJ |
1738 | /* If arg is or contains address of a heap artificial variable or |
1739 | of a static variable being constructed, avoid caching the | |
1740 | function call, as those variables might be modified by the | |
1741 | function, or might be modified by the callers in between | |
1742 | the cached function and just read by the function. */ | |
1743 | else if (!*non_constant_args | |
1744 | && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global, | |
1745 | NULL)) | |
1746 | *non_constant_args = true; | |
5afd90c5 | 1747 | |
5558a0da JJ |
1748 | /* For virtual calls, adjust the this argument, so that it is |
1749 | the object on which the method is called, rather than | |
1750 | one of its bases. */ | |
1751 | if (i == 0 && DECL_VIRTUAL_P (fun)) | |
1752 | { | |
1753 | tree addr = arg; | |
1754 | STRIP_NOPS (addr); | |
1755 | if (TREE_CODE (addr) == ADDR_EXPR) | |
1756 | { | |
1757 | tree obj = TREE_OPERAND (addr, 0); | |
1758 | while (TREE_CODE (obj) == COMPONENT_REF | |
1759 | && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1)) | |
1760 | && !same_type_ignoring_top_level_qualifiers_p | |
1761 | (TREE_TYPE (obj), DECL_CONTEXT (fun))) | |
1762 | obj = TREE_OPERAND (obj, 0); | |
1763 | if (obj != TREE_OPERAND (addr, 0)) | |
1764 | arg = build_fold_addr_expr_with_type (obj, | |
1765 | TREE_TYPE (arg)); | |
1766 | } | |
1767 | } | |
3c961dc7 | 1768 | TREE_VEC_ELT (binds, i) = arg; |
4727d4c6 | 1769 | } |
2d76680f PC |
1770 | parms = TREE_CHAIN (parms); |
1771 | } | |
1595fe44 JM |
1772 | |
1773 | return binds; | |
2d76680f PC |
1774 | } |
1775 | ||
1776 | /* Variables and functions to manage constexpr call expansion context. | |
1777 | These do not need to be marked for PCH or GC. */ | |
1778 | ||
1779 | /* FIXME remember and print actual constant arguments. */ | |
7de76362 | 1780 | static vec<tree> call_stack; |
2d76680f PC |
1781 | static int call_stack_tick; |
1782 | static int last_cx_error_tick; | |
1783 | ||
7ffc7de5 | 1784 | static int |
2d76680f PC |
1785 | push_cx_call_context (tree call) |
1786 | { | |
1787 | ++call_stack_tick; | |
1788 | if (!EXPR_HAS_LOCATION (call)) | |
1789 | SET_EXPR_LOCATION (call, input_location); | |
1790 | call_stack.safe_push (call); | |
7ffc7de5 JM |
1791 | int len = call_stack.length (); |
1792 | if (len > max_constexpr_depth) | |
2d76680f | 1793 | return false; |
7ffc7de5 | 1794 | return len; |
2d76680f PC |
1795 | } |
1796 | ||
1797 | static void | |
1798 | pop_cx_call_context (void) | |
1799 | { | |
1800 | ++call_stack_tick; | |
1801 | call_stack.pop (); | |
1802 | } | |
1803 | ||
1804 | vec<tree> | |
1805 | cx_error_context (void) | |
1806 | { | |
1807 | vec<tree> r = vNULL; | |
1808 | if (call_stack_tick != last_cx_error_tick | |
1809 | && !call_stack.is_empty ()) | |
1810 | r = call_stack; | |
1811 | last_cx_error_tick = call_stack_tick; | |
1812 | return r; | |
1813 | } | |
1814 | ||
44a845ca MS |
1815 | /* Evaluate a call T to a GCC internal function when possible and return |
1816 | the evaluated result or, under the control of CTX, give an error, set | |
1817 | NON_CONSTANT_P, and return the unevaluated call T otherwise. */ | |
1818 | ||
1819 | static tree | |
1820 | cxx_eval_internal_function (const constexpr_ctx *ctx, tree t, | |
72f76540 | 1821 | value_cat lval, |
44a845ca MS |
1822 | bool *non_constant_p, bool *overflow_p) |
1823 | { | |
1824 | enum tree_code opcode = ERROR_MARK; | |
1825 | ||
1826 | switch (CALL_EXPR_IFN (t)) | |
1827 | { | |
1828 | case IFN_UBSAN_NULL: | |
1829 | case IFN_UBSAN_BOUNDS: | |
1830 | case IFN_UBSAN_VPTR: | |
81fea426 | 1831 | case IFN_FALLTHROUGH: |
44a845ca MS |
1832 | return void_node; |
1833 | ||
1834 | case IFN_ADD_OVERFLOW: | |
1835 | opcode = PLUS_EXPR; | |
1836 | break; | |
1837 | case IFN_SUB_OVERFLOW: | |
1838 | opcode = MINUS_EXPR; | |
1839 | break; | |
1840 | case IFN_MUL_OVERFLOW: | |
1841 | opcode = MULT_EXPR; | |
1842 | break; | |
1843 | ||
e16f1cc7 JJ |
1844 | case IFN_LAUNDER: |
1845 | return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), | |
72f76540 JM |
1846 | vc_prvalue, non_constant_p, |
1847 | overflow_p); | |
e16f1cc7 | 1848 | |
d8fcab68 JJ |
1849 | case IFN_VEC_CONVERT: |
1850 | { | |
1851 | tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), | |
72f76540 | 1852 | vc_prvalue, non_constant_p, |
d8fcab68 JJ |
1853 | overflow_p); |
1854 | if (TREE_CODE (arg) == VECTOR_CST) | |
84993d94 JJ |
1855 | if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg)) |
1856 | return r; | |
d8fcab68 | 1857 | } |
84993d94 | 1858 | /* FALLTHRU */ |
d8fcab68 | 1859 | |
44a845ca MS |
1860 | default: |
1861 | if (!ctx->quiet) | |
f9d0ca40 | 1862 | error_at (cp_expr_loc_or_input_loc (t), |
44a845ca MS |
1863 | "call to internal function %qE", t); |
1864 | *non_constant_p = true; | |
1865 | return t; | |
1866 | } | |
1867 | ||
1868 | /* Evaluate constant arguments using OPCODE and return a complex | |
1869 | number containing the result and the overflow bit. */ | |
1870 | tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval, | |
1871 | non_constant_p, overflow_p); | |
1872 | tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval, | |
1873 | non_constant_p, overflow_p); | |
1874 | ||
1875 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) | |
1876 | { | |
f9d0ca40 | 1877 | location_t loc = cp_expr_loc_or_input_loc (t); |
44a845ca MS |
1878 | tree type = TREE_TYPE (TREE_TYPE (t)); |
1879 | tree result = fold_binary_loc (loc, opcode, type, | |
1880 | fold_convert_loc (loc, type, arg0), | |
1881 | fold_convert_loc (loc, type, arg1)); | |
1882 | tree ovf | |
1883 | = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1)); | |
1884 | /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */ | |
1885 | if (TREE_OVERFLOW (result)) | |
1886 | TREE_OVERFLOW (result) = 0; | |
1887 | ||
1888 | return build_complex (TREE_TYPE (t), result, ovf); | |
1889 | } | |
1890 | ||
1891 | *non_constant_p = true; | |
1892 | return t; | |
1893 | } | |
1894 | ||
e8c48716 | 1895 | /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */ |
ecc57615 JM |
1896 | |
1897 | static void | |
1898 | clear_no_implicit_zero (tree ctor) | |
1899 | { | |
e8c48716 | 1900 | if (CONSTRUCTOR_NO_CLEARING (ctor)) |
ecc57615 | 1901 | { |
e8c48716 | 1902 | CONSTRUCTOR_NO_CLEARING (ctor) = false; |
fae00a0a JM |
1903 | for (auto &e: CONSTRUCTOR_ELTS (ctor)) |
1904 | if (TREE_CODE (e.value) == CONSTRUCTOR) | |
1905 | clear_no_implicit_zero (e.value); | |
ecc57615 JM |
1906 | } |
1907 | } | |
1908 | ||
04e1749c MP |
1909 | /* Complain about a const object OBJ being modified in a constant expression. |
1910 | EXPR is the MODIFY_EXPR expression performing the modification. */ | |
1911 | ||
1912 | static void | |
1913 | modifying_const_object_error (tree expr, tree obj) | |
1914 | { | |
1915 | location_t loc = cp_expr_loc_or_input_loc (expr); | |
1916 | auto_diagnostic_group d; | |
1917 | error_at (loc, "modifying a const object %qE is not allowed in " | |
1918 | "a constant expression", TREE_OPERAND (expr, 0)); | |
1919 | inform (location_of (obj), "originally declared %<const%> here"); | |
1920 | } | |
1921 | ||
8e007055 JJ |
1922 | /* Return true if FNDECL is a replaceable global allocation function that |
1923 | should be useable during constant expression evaluation. */ | |
1924 | ||
1925 | static inline bool | |
1926 | cxx_replaceable_global_alloc_fn (tree fndecl) | |
1927 | { | |
b04445d4 | 1928 | return (cxx_dialect >= cxx20 |
8e007055 | 1929 | && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl)) |
cf650568 JJ |
1930 | && CP_DECL_CONTEXT (fndecl) == global_namespace |
1931 | && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl) | |
1932 | || DECL_IS_OPERATOR_DELETE_P (fndecl))); | |
1933 | } | |
1934 | ||
1935 | /* Return true if FNDECL is a placement new function that should be | |
1936 | useable during constant expression evaluation of std::construct_at. */ | |
1937 | ||
1938 | static inline bool | |
1939 | cxx_placement_new_fn (tree fndecl) | |
1940 | { | |
b04445d4 | 1941 | if (cxx_dialect >= cxx20 |
cf650568 JJ |
1942 | && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl)) |
1943 | && CP_DECL_CONTEXT (fndecl) == global_namespace | |
1944 | && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl) | |
1945 | && TREE_CODE (TREE_TYPE (fndecl)) == FUNCTION_TYPE) | |
1946 | { | |
1947 | tree first_arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl))); | |
1948 | if (TREE_VALUE (first_arg) == ptr_type_node | |
1949 | && TREE_CHAIN (first_arg) == void_list_node) | |
1950 | return true; | |
1951 | } | |
1952 | return false; | |
1953 | } | |
1954 | ||
1955 | /* Return true if FNDECL is std::construct_at. */ | |
1956 | ||
1957 | static inline bool | |
1958 | is_std_construct_at (tree fndecl) | |
1959 | { | |
1960 | if (!decl_in_std_namespace_p (fndecl)) | |
1961 | return false; | |
1962 | ||
1963 | tree name = DECL_NAME (fndecl); | |
1964 | return name && id_equal (name, "construct_at"); | |
8e007055 JJ |
1965 | } |
1966 | ||
2ffc2645 MP |
1967 | /* Overload for the above taking constexpr_call*. */ |
1968 | ||
1969 | static inline bool | |
1970 | is_std_construct_at (const constexpr_call *call) | |
1971 | { | |
1972 | return (call | |
1973 | && call->fundef | |
1974 | && is_std_construct_at (call->fundef->decl)); | |
1975 | } | |
1976 | ||
8412b939 JJ |
1977 | /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */ |
1978 | ||
1979 | static inline bool | |
1980 | is_std_allocator_allocate (tree fndecl) | |
1981 | { | |
1982 | tree name = DECL_NAME (fndecl); | |
1983 | if (name == NULL_TREE | |
1984 | || !(id_equal (name, "allocate") || id_equal (name, "deallocate"))) | |
1985 | return false; | |
1986 | ||
1987 | tree ctx = DECL_CONTEXT (fndecl); | |
1988 | if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx)) | |
1989 | return false; | |
1990 | ||
1991 | tree decl = TYPE_MAIN_DECL (ctx); | |
1992 | name = DECL_NAME (decl); | |
1993 | if (name == NULL_TREE || !id_equal (name, "allocator")) | |
1994 | return false; | |
1995 | ||
1996 | return decl_in_std_namespace_p (decl); | |
1997 | } | |
1998 | ||
2ffc2645 MP |
1999 | /* Overload for the above taking constexpr_call*. */ |
2000 | ||
2001 | static inline bool | |
2002 | is_std_allocator_allocate (const constexpr_call *call) | |
2003 | { | |
2004 | return (call | |
2005 | && call->fundef | |
2006 | && is_std_allocator_allocate (call->fundef->decl)); | |
2007 | } | |
2008 | ||
22edf943 MP |
2009 | /* Return true if FNDECL is __dynamic_cast. */ |
2010 | ||
2011 | static inline bool | |
2012 | cxx_dynamic_cast_fn_p (tree fndecl) | |
2013 | { | |
b04445d4 | 2014 | return (cxx_dialect >= cxx20 |
22edf943 MP |
2015 | && id_equal (DECL_NAME (fndecl), "__dynamic_cast") |
2016 | && CP_DECL_CONTEXT (fndecl) == global_namespace); | |
2017 | } | |
2018 | ||
2019 | /* Often, we have an expression in the form of address + offset, e.g. | |
2020 | "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */ | |
2021 | ||
2022 | static tree | |
2023 | extract_obj_from_addr_offset (tree expr) | |
2024 | { | |
2025 | if (TREE_CODE (expr) == POINTER_PLUS_EXPR) | |
2026 | expr = TREE_OPERAND (expr, 0); | |
2027 | STRIP_NOPS (expr); | |
2028 | if (TREE_CODE (expr) == ADDR_EXPR) | |
2029 | expr = TREE_OPERAND (expr, 0); | |
2030 | return expr; | |
2031 | } | |
2032 | ||
2033 | /* Given a PATH like | |
2034 | ||
2035 | g.D.2181.D.2154.D.2102.D.2093 | |
2036 | ||
2037 | find a component with type TYPE. Return NULL_TREE if not found, and | |
2038 | error_mark_node if the component is not accessible. If STOP is non-null, | |
2039 | this function will return NULL_TREE if STOP is found before TYPE. */ | |
2040 | ||
2041 | static tree | |
2042 | get_component_with_type (tree path, tree type, tree stop) | |
2043 | { | |
2044 | while (true) | |
2045 | { | |
2046 | if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type)) | |
2047 | /* Found it. */ | |
2048 | return path; | |
2049 | else if (stop | |
2050 | && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), | |
2051 | stop))) | |
2052 | return NULL_TREE; | |
2053 | else if (TREE_CODE (path) == COMPONENT_REF | |
2054 | && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1))) | |
2055 | { | |
2056 | /* We need to check that the component we're accessing is in fact | |
2057 | accessible. */ | |
2058 | if (TREE_PRIVATE (TREE_OPERAND (path, 1)) | |
2059 | || TREE_PROTECTED (TREE_OPERAND (path, 1))) | |
2060 | return error_mark_node; | |
2061 | path = TREE_OPERAND (path, 0); | |
2062 | } | |
2063 | else | |
2064 | return NULL_TREE; | |
2065 | } | |
2066 | } | |
2067 | ||
2068 | /* Evaluate a call to __dynamic_cast (permitted by P1327R1). | |
2069 | ||
2070 | The declaration of __dynamic_cast is: | |
2071 | ||
2072 | void* __dynamic_cast (const void* __src_ptr, | |
2073 | const __class_type_info* __src_type, | |
2074 | const __class_type_info* __dst_type, | |
2075 | ptrdiff_t __src2dst); | |
2076 | ||
2077 | where src2dst has the following possible values | |
2078 | ||
2079 | >-1: src_type is a unique public non-virtual base of dst_type | |
2080 | dst_ptr + src2dst == src_ptr | |
2081 | -1: unspecified relationship | |
2082 | -2: src_type is not a public base of dst_type | |
2083 | -3: src_type is a multiple public non-virtual base of dst_type | |
2084 | ||
2085 | Since literal types can't have virtual bases, we only expect hint >=0, | |
2086 | -2, or -3. */ | |
2087 | ||
2088 | static tree | |
2089 | cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call, | |
2090 | bool *non_constant_p, bool *overflow_p) | |
2091 | { | |
2092 | /* T will be something like | |
2093 | __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8) | |
2094 | dismantle it. */ | |
2095 | gcc_assert (call_expr_nargs (call) == 4); | |
2096 | tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; | |
2097 | tree obj = CALL_EXPR_ARG (call, 0); | |
2098 | tree type = CALL_EXPR_ARG (call, 2); | |
2099 | HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3)); | |
2100 | location_t loc = cp_expr_loc_or_input_loc (call); | |
2101 | ||
2102 | /* Get the target type of the dynamic_cast. */ | |
2103 | gcc_assert (TREE_CODE (type) == ADDR_EXPR); | |
2104 | type = TREE_OPERAND (type, 0); | |
2105 | type = TREE_TYPE (DECL_NAME (type)); | |
2106 | ||
2107 | /* TYPE can only be either T* or T&. We can't know which of these it | |
2108 | is by looking at TYPE, but OBJ will be "(T*) x" in the first case, | |
2109 | and something like "(T*)(T&)(T*) x" in the second case. */ | |
2110 | bool reference_p = false; | |
2111 | while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR) | |
2112 | { | |
2113 | reference_p |= TYPE_REF_P (TREE_TYPE (obj)); | |
2114 | obj = TREE_OPERAND (obj, 0); | |
2115 | } | |
2116 | ||
2117 | /* Evaluate the object so that we know its dynamic type. */ | |
72f76540 | 2118 | obj = cxx_eval_constant_expression (ctx, obj, vc_prvalue, non_constant_p, |
22edf943 MP |
2119 | overflow_p); |
2120 | if (*non_constant_p) | |
2121 | return call; | |
2122 | ||
2123 | /* We expect OBJ to be in form of &d.D.2102 when HINT == 0, | |
2124 | but when HINT is > 0, it can also be something like | |
2125 | &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */ | |
2126 | obj = extract_obj_from_addr_offset (obj); | |
2127 | const tree objtype = TREE_TYPE (obj); | |
2128 | /* If OBJ doesn't refer to a base field, we're done. */ | |
2129 | if (tree t = (TREE_CODE (obj) == COMPONENT_REF | |
2130 | ? TREE_OPERAND (obj, 1) : obj)) | |
2131 | if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t)) | |
de0684bf MP |
2132 | { |
2133 | if (reference_p) | |
2134 | { | |
2135 | if (!ctx->quiet) | |
2136 | { | |
2137 | error_at (loc, "reference %<dynamic_cast%> failed"); | |
2138 | inform (loc, "dynamic type %qT of its operand does " | |
2139 | "not have a base class of type %qT", | |
2140 | objtype, type); | |
2141 | } | |
2142 | *non_constant_p = true; | |
2143 | } | |
2144 | return integer_zero_node; | |
2145 | } | |
22edf943 MP |
2146 | |
2147 | /* [class.cdtor] When a dynamic_cast is used in a constructor ... | |
2148 | or in a destructor ... if the operand of the dynamic_cast refers | |
2149 | to the object under construction or destruction, this object is | |
2150 | considered to be a most derived object that has the type of the | |
2151 | constructor or destructor's class. */ | |
0221c656 | 2152 | tree vtable = build_vfield_ref (obj, objtype); |
72f76540 | 2153 | vtable = cxx_eval_constant_expression (ctx, vtable, vc_prvalue, |
22edf943 MP |
2154 | non_constant_p, overflow_p); |
2155 | if (*non_constant_p) | |
2156 | return call; | |
0221c656 MP |
2157 | /* With -fsanitize=vptr, we initialize all vtable pointers to null, |
2158 | so it's possible that we got a null pointer now. */ | |
2159 | if (integer_zerop (vtable)) | |
2160 | { | |
2161 | if (!ctx->quiet) | |
2162 | error_at (loc, "virtual table pointer is used uninitialized"); | |
2163 | *non_constant_p = true; | |
2164 | return integer_zero_node; | |
2165 | } | |
22edf943 MP |
2166 | /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */ |
2167 | vtable = extract_obj_from_addr_offset (vtable); | |
2168 | const tree mdtype = DECL_CONTEXT (vtable); | |
2169 | ||
2170 | /* Given dynamic_cast<T>(v), | |
2171 | ||
2172 | [expr.dynamic.cast] If C is the class type to which T points or refers, | |
2173 | the runtime check logically executes as follows: | |
2174 | ||
2175 | If, in the most derived object pointed (referred) to by v, v points | |
2176 | (refers) to a public base class subobject of a C object, and if only | |
2177 | one object of type C is derived from the subobject pointed (referred) | |
2178 | to by v the result points (refers) to that C object. | |
2179 | ||
2180 | In this case, HINT >= 0 or -3. */ | |
2181 | if (hint >= 0 || hint == -3) | |
2182 | { | |
2183 | /* Look for a component with type TYPE. */ | |
2184 | tree t = get_component_with_type (obj, type, mdtype); | |
2185 | /* If not accessible, give an error. */ | |
2186 | if (t == error_mark_node) | |
2187 | { | |
2188 | if (reference_p) | |
2189 | { | |
2190 | if (!ctx->quiet) | |
2191 | { | |
2192 | error_at (loc, "reference %<dynamic_cast%> failed"); | |
2193 | inform (loc, "static type %qT of its operand is a " | |
2194 | "non-public base class of dynamic type %qT", | |
2195 | objtype, type); | |
2196 | ||
2197 | } | |
2198 | *non_constant_p = true; | |
2199 | } | |
2200 | return integer_zero_node; | |
2201 | } | |
2202 | else if (t) | |
2203 | /* The result points to the TYPE object. */ | |
2204 | return cp_build_addr_expr (t, complain); | |
2205 | /* Else, TYPE was not found, because the HINT turned out to be wrong. | |
2206 | Fall through to the normal processing. */ | |
2207 | } | |
2208 | ||
2209 | /* Otherwise, if v points (refers) to a public base class subobject of the | |
2210 | most derived object, and the type of the most derived object has a base | |
2211 | class, of type C, that is unambiguous and public, the result points | |
2212 | (refers) to the C subobject of the most derived object. | |
2213 | ||
2214 | But it can also be an invalid case. */ | |
2215 | ||
2216 | /* Get the most derived object. */ | |
2217 | obj = get_component_with_type (obj, mdtype, NULL_TREE); | |
2218 | if (obj == error_mark_node) | |
2219 | { | |
2220 | if (reference_p) | |
2221 | { | |
2222 | if (!ctx->quiet) | |
2223 | { | |
2224 | error_at (loc, "reference %<dynamic_cast%> failed"); | |
2225 | inform (loc, "static type %qT of its operand is a non-public" | |
2226 | " base class of dynamic type %qT", objtype, mdtype); | |
2227 | } | |
2228 | *non_constant_p = true; | |
2229 | } | |
2230 | return integer_zero_node; | |
2231 | } | |
2232 | else | |
2233 | gcc_assert (obj); | |
2234 | ||
2235 | /* Check that the type of the most derived object has a base class | |
2236 | of type TYPE that is unambiguous and public. */ | |
2237 | base_kind b_kind; | |
2238 | tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none); | |
2239 | if (!binfo || binfo == error_mark_node) | |
2240 | { | |
2241 | if (reference_p) | |
2242 | { | |
2243 | if (!ctx->quiet) | |
2244 | { | |
2245 | error_at (loc, "reference %<dynamic_cast%> failed"); | |
2246 | if (b_kind == bk_ambig) | |
2247 | inform (loc, "%qT is an ambiguous base class of dynamic " | |
2248 | "type %qT of its operand", type, mdtype); | |
2249 | else | |
2250 | inform (loc, "dynamic type %qT of its operand does not " | |
2251 | "have an unambiguous public base class %qT", | |
2252 | mdtype, type); | |
2253 | } | |
2254 | *non_constant_p = true; | |
2255 | } | |
2256 | return integer_zero_node; | |
2257 | } | |
2258 | /* If so, return the TYPE subobject of the most derived object. */ | |
2259 | obj = convert_to_base_statically (obj, binfo); | |
2260 | return cp_build_addr_expr (obj, complain); | |
2261 | } | |
2262 | ||
4f6bc28f | 2263 | /* Data structure used by replace_decl and replace_decl_r. */ |
b2562229 | 2264 | |
4f6bc28f | 2265 | struct replace_decl_data |
b2562229 | 2266 | { |
4f6bc28f | 2267 | /* The _DECL we want to replace. */ |
b2562229 PP |
2268 | tree decl; |
2269 | /* The replacement for DECL. */ | |
2270 | tree replacement; | |
4f6bc28f JM |
2271 | /* Trees we've visited. */ |
2272 | hash_set<tree> *pset; | |
b2562229 PP |
2273 | /* Whether we've performed any replacements. */ |
2274 | bool changed; | |
2275 | }; | |
2276 | ||
4f6bc28f | 2277 | /* Helper function for replace_decl, called through cp_walk_tree. */ |
b2562229 PP |
2278 | |
2279 | static tree | |
4f6bc28f | 2280 | replace_decl_r (tree *tp, int *walk_subtrees, void *data) |
b2562229 | 2281 | { |
4f6bc28f | 2282 | replace_decl_data *d = (replace_decl_data *) data; |
b2562229 PP |
2283 | |
2284 | if (*tp == d->decl) | |
2285 | { | |
2286 | *tp = unshare_expr (d->replacement); | |
2287 | d->changed = true; | |
2288 | *walk_subtrees = 0; | |
2289 | } | |
4f6bc28f JM |
2290 | else if (TYPE_P (*tp) |
2291 | || d->pset->add (*tp)) | |
b2562229 PP |
2292 | *walk_subtrees = 0; |
2293 | ||
2294 | return NULL_TREE; | |
2295 | } | |
2296 | ||
4f6bc28f JM |
2297 | /* Replace every occurrence of DECL with (an unshared copy of) |
2298 | REPLACEMENT within the expression *TP. Returns true iff a | |
b2562229 PP |
2299 | replacement was performed. */ |
2300 | ||
4f6bc28f JM |
2301 | bool |
2302 | replace_decl (tree *tp, tree decl, tree replacement) | |
b2562229 | 2303 | { |
4f6bc28f JM |
2304 | gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p |
2305 | (TREE_TYPE (decl), TREE_TYPE (replacement))); | |
2306 | hash_set<tree> pset; | |
2307 | replace_decl_data data = { decl, replacement, &pset, false }; | |
2308 | cp_walk_tree (tp, replace_decl_r, &data, NULL); | |
b2562229 PP |
2309 | return data.changed; |
2310 | } | |
2311 | ||
e6321c45 JM |
2312 | /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */ |
2313 | ||
2314 | static tree | |
2315 | cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl, | |
72f76540 | 2316 | value_cat lval, |
e6321c45 JM |
2317 | bool *non_constant_p, bool *overflow_p) |
2318 | { | |
2319 | tree function = THUNK_TARGET (thunk_fndecl); | |
2320 | ||
e6321c45 JM |
2321 | if (THUNK_VIRTUAL_OFFSET (thunk_fndecl)) |
2322 | { | |
e6321c45 JM |
2323 | if (!ctx->quiet) |
2324 | { | |
267318a2 JM |
2325 | if (!DECL_DECLARED_CONSTEXPR_P (function)) |
2326 | { | |
2327 | error ("call to non-%<constexpr%> function %qD", function); | |
2328 | explain_invalid_constexpr_fn (function); | |
2329 | } | |
2330 | else | |
2331 | /* virtual_offset is only set for virtual bases, which make the | |
2332 | class non-literal, so we don't need to handle it here. */ | |
2333 | error ("calling constexpr member function %qD through virtual " | |
2334 | "base subobject", function); | |
e6321c45 JM |
2335 | } |
2336 | *non_constant_p = true; | |
2337 | return t; | |
2338 | } | |
2339 | ||
2340 | tree new_call = copy_node (t); | |
2341 | CALL_EXPR_FN (new_call) = function; | |
2342 | TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function)); | |
2343 | ||
2344 | tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl)); | |
2345 | ||
2346 | if (DECL_THIS_THUNK_P (thunk_fndecl)) | |
2347 | { | |
2348 | /* 'this'-adjusting thunk. */ | |
2349 | tree this_arg = CALL_EXPR_ARG (t, 0); | |
2350 | this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg), | |
2351 | this_arg, offset); | |
2352 | CALL_EXPR_ARG (new_call, 0) = this_arg; | |
2353 | } | |
2354 | else | |
2355 | /* Return-adjusting thunk. */ | |
2356 | new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call), | |
2357 | new_call, offset); | |
2358 | ||
2359 | return cxx_eval_constant_expression (ctx, new_call, lval, | |
2360 | non_constant_p, overflow_p); | |
2361 | } | |
2362 | ||
caf17f3a MP |
2363 | /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set |
2364 | its TREE_READONLY flag according to READONLY_P. Used for constexpr | |
2365 | 'tors to detect modifying const objects in a constexpr context. */ | |
2366 | ||
2367 | static void | |
2368 | cxx_set_object_constness (const constexpr_ctx *ctx, tree object, | |
2369 | bool readonly_p, bool *non_constant_p, | |
2370 | bool *overflow_p) | |
2371 | { | |
2372 | if (CLASS_TYPE_P (TREE_TYPE (object)) | |
2373 | && CP_TYPE_CONST_P (TREE_TYPE (object))) | |
2374 | { | |
2375 | /* Subobjects might not be stored in ctx->global->values but we | |
2376 | can get its CONSTRUCTOR by evaluating *this. */ | |
72f76540 | 2377 | tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue, |
caf17f3a MP |
2378 | non_constant_p, overflow_p); |
2379 | if (TREE_CODE (e) == CONSTRUCTOR && !*non_constant_p) | |
2380 | TREE_READONLY (e) = readonly_p; | |
2381 | } | |
2382 | } | |
2383 | ||
2d76680f PC |
2384 | /* Subroutine of cxx_eval_constant_expression. |
2385 | Evaluate the call expression tree T in the context of OLD_CALL expression | |
2386 | evaluation. */ | |
2387 | ||
2388 | static tree | |
3e605b20 | 2389 | cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, |
72f76540 | 2390 | value_cat lval, |
2d76680f PC |
2391 | bool *non_constant_p, bool *overflow_p) |
2392 | { | |
861d4af8 AS |
2393 | /* Handle concept checks separately. */ |
2394 | if (concept_check_p (t)) | |
662ef5b5 | 2395 | return evaluate_concept_check (t); |
861d4af8 | 2396 | |
f9d0ca40 | 2397 | location_t loc = cp_expr_loc_or_input_loc (t); |
2d76680f | 2398 | tree fun = get_function_named_in_call (t); |
cce3ae91 | 2399 | constexpr_call new_call |
13de99bc | 2400 | = { NULL, NULL, NULL, 0, ctx->manifestly_const_eval }; |
7ffc7de5 | 2401 | int depth_ok; |
2d76680f | 2402 | |
0b274c17 | 2403 | if (fun == NULL_TREE) |
44a845ca MS |
2404 | return cxx_eval_internal_function (ctx, t, lval, |
2405 | non_constant_p, overflow_p); | |
0b274c17 | 2406 | |
2d76680f PC |
2407 | if (TREE_CODE (fun) != FUNCTION_DECL) |
2408 | { | |
2409 | /* Might be a constexpr function pointer. */ | |
72f76540 JM |
2410 | fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue, |
2411 | non_constant_p, overflow_p); | |
2d76680f PC |
2412 | STRIP_NOPS (fun); |
2413 | if (TREE_CODE (fun) == ADDR_EXPR) | |
2414 | fun = TREE_OPERAND (fun, 0); | |
582d2481 JJ |
2415 | /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no |
2416 | indirection, the called expression is a pointer into the | |
2417 | virtual table which should contain FDESC_EXPR. Extract the | |
2418 | FUNCTION_DECL from there. */ | |
2419 | else if (TARGET_VTABLE_USES_DESCRIPTORS | |
2420 | && TREE_CODE (fun) == POINTER_PLUS_EXPR | |
2421 | && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR | |
2422 | && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST) | |
2423 | { | |
2424 | tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0); | |
2425 | if (VAR_P (d) | |
2426 | && DECL_VTABLE_OR_VTT_P (d) | |
2427 | && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE | |
2428 | && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type | |
2429 | && DECL_INITIAL (d) | |
2430 | && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR) | |
2431 | { | |
2432 | tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1), | |
2433 | TYPE_SIZE_UNIT (vtable_entry_type)); | |
2434 | HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i); | |
2435 | if (idx >= 0) | |
2436 | { | |
2437 | tree fdesc | |
2438 | = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value; | |
2439 | if (TREE_CODE (fdesc) == FDESC_EXPR | |
2440 | && integer_zerop (TREE_OPERAND (fdesc, 1))) | |
2441 | fun = TREE_OPERAND (fdesc, 0); | |
2442 | } | |
2443 | } | |
2444 | } | |
2d76680f PC |
2445 | } |
2446 | if (TREE_CODE (fun) != FUNCTION_DECL) | |
2447 | { | |
2b3ab879 | 2448 | if (!ctx->quiet && !*non_constant_p) |
84fa214d | 2449 | error_at (loc, "expression %qE does not designate a %<constexpr%> " |
2d76680f PC |
2450 | "function", fun); |
2451 | *non_constant_p = true; | |
2452 | return t; | |
2453 | } | |
81f97181 | 2454 | if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)) |
2d76680f | 2455 | fun = DECL_CLONED_FUNCTION (fun); |
0b274c17 MP |
2456 | |
2457 | if (is_ubsan_builtin_p (fun)) | |
2458 | return void_node; | |
2459 | ||
3d78e008 | 2460 | if (fndecl_built_in_p (fun)) |
5756d0f9 | 2461 | return cxx_eval_builtin_function_call (ctx, t, fun, |
92a596e8 | 2462 | lval, non_constant_p, overflow_p); |
e6321c45 JM |
2463 | if (DECL_THUNK_P (fun)) |
2464 | return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p); | |
87c2080b | 2465 | if (!maybe_constexpr_fn (fun)) |
2d76680f | 2466 | { |
8412b939 JJ |
2467 | if (TREE_CODE (t) == CALL_EXPR |
2468 | && cxx_replaceable_global_alloc_fn (fun) | |
2469 | && (CALL_FROM_NEW_OR_DELETE_P (t) | |
2ffc2645 | 2470 | || is_std_allocator_allocate (ctx->call))) |
8e007055 JJ |
2471 | { |
2472 | const int nargs = call_expr_nargs (t); | |
2473 | tree arg0 = NULL_TREE; | |
2474 | for (int i = 0; i < nargs; ++i) | |
2475 | { | |
2476 | tree arg = CALL_EXPR_ARG (t, i); | |
72f76540 | 2477 | arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue, |
8e007055 JJ |
2478 | non_constant_p, overflow_p); |
2479 | VERIFY_CONSTANT (arg); | |
2480 | if (i == 0) | |
2481 | arg0 = arg; | |
2482 | } | |
2483 | gcc_assert (arg0); | |
2484 | if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun))) | |
2485 | { | |
2486 | tree type = build_array_type_nelts (char_type_node, | |
2487 | tree_to_uhwi (arg0)); | |
815baade JJ |
2488 | tree var = build_decl (loc, VAR_DECL, |
2489 | (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) | |
2490 | & OVL_OP_FLAG_VEC) | |
2491 | ? heap_vec_uninit_identifier | |
2492 | : heap_uninit_identifier, | |
8e007055 JJ |
2493 | type); |
2494 | DECL_ARTIFICIAL (var) = 1; | |
2495 | TREE_STATIC (var) = 1; | |
a8db7887 JJ |
2496 | // Temporarily register the artificial var in varpool, |
2497 | // so that comparisons of its address against NULL are folded | |
2498 | // through nonzero_address even with | |
2499 | // -fno-delete-null-pointer-checks or that comparison of | |
2500 | // addresses of different heap artificial vars is folded too. | |
2501 | // See PR98988 and PR99031. | |
2502 | varpool_node::finalize_decl (var); | |
8e007055 JJ |
2503 | ctx->global->heap_vars.safe_push (var); |
2504 | ctx->global->values.put (var, NULL_TREE); | |
2505 | return fold_convert (ptr_type_node, build_address (var)); | |
2506 | } | |
2507 | else | |
2508 | { | |
2509 | STRIP_NOPS (arg0); | |
2510 | if (TREE_CODE (arg0) == ADDR_EXPR | |
2511 | && VAR_P (TREE_OPERAND (arg0, 0))) | |
2512 | { | |
2513 | tree var = TREE_OPERAND (arg0, 0); | |
2514 | if (DECL_NAME (var) == heap_uninit_identifier | |
2515 | || DECL_NAME (var) == heap_identifier) | |
2516 | { | |
815baade JJ |
2517 | if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) |
2518 | & OVL_OP_FLAG_VEC) | |
2519 | { | |
2520 | if (!ctx->quiet) | |
2521 | { | |
2522 | error_at (loc, "array deallocation of object " | |
2523 | "allocated with non-array " | |
2524 | "allocation"); | |
2525 | inform (DECL_SOURCE_LOCATION (var), | |
2526 | "allocation performed here"); | |
2527 | } | |
2528 | *non_constant_p = true; | |
2529 | return t; | |
2530 | } | |
2531 | DECL_NAME (var) = heap_deleted_identifier; | |
2532 | ctx->global->values.remove (var); | |
2533 | ctx->global->heap_dealloc_count++; | |
2534 | return void_node; | |
2535 | } | |
2536 | else if (DECL_NAME (var) == heap_vec_uninit_identifier | |
2537 | || DECL_NAME (var) == heap_vec_identifier) | |
2538 | { | |
2539 | if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) | |
2540 | & OVL_OP_FLAG_VEC) == 0) | |
2541 | { | |
2542 | if (!ctx->quiet) | |
2543 | { | |
2544 | error_at (loc, "non-array deallocation of " | |
2545 | "object allocated with array " | |
2546 | "allocation"); | |
2547 | inform (DECL_SOURCE_LOCATION (var), | |
2548 | "allocation performed here"); | |
2549 | } | |
2550 | *non_constant_p = true; | |
2551 | return t; | |
2552 | } | |
8e007055 JJ |
2553 | DECL_NAME (var) = heap_deleted_identifier; |
2554 | ctx->global->values.remove (var); | |
f74f6092 | 2555 | ctx->global->heap_dealloc_count++; |
8e007055 JJ |
2556 | return void_node; |
2557 | } | |
2558 | else if (DECL_NAME (var) == heap_deleted_identifier) | |
2559 | { | |
2560 | if (!ctx->quiet) | |
2561 | error_at (loc, "deallocation of already deallocated " | |
2562 | "storage"); | |
2563 | *non_constant_p = true; | |
2564 | return t; | |
2565 | } | |
2566 | } | |
2567 | if (!ctx->quiet) | |
2568 | error_at (loc, "deallocation of storage that was " | |
2569 | "not previously allocated"); | |
2570 | *non_constant_p = true; | |
2571 | return t; | |
2572 | } | |
2573 | } | |
cf650568 JJ |
2574 | /* Allow placement new in std::construct_at, just return the second |
2575 | argument. */ | |
8412b939 JJ |
2576 | if (TREE_CODE (t) == CALL_EXPR |
2577 | && cxx_placement_new_fn (fun) | |
2ffc2645 | 2578 | && is_std_construct_at (ctx->call)) |
cf650568 JJ |
2579 | { |
2580 | const int nargs = call_expr_nargs (t); | |
2581 | tree arg1 = NULL_TREE; | |
2582 | for (int i = 0; i < nargs; ++i) | |
2583 | { | |
2584 | tree arg = CALL_EXPR_ARG (t, i); | |
72f76540 | 2585 | arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue, |
cf650568 | 2586 | non_constant_p, overflow_p); |
cf650568 JJ |
2587 | if (i == 1) |
2588 | arg1 = arg; | |
2805fcb3 JJ |
2589 | else |
2590 | VERIFY_CONSTANT (arg); | |
cf650568 JJ |
2591 | } |
2592 | gcc_assert (arg1); | |
2593 | return arg1; | |
2594 | } | |
22edf943 MP |
2595 | else if (cxx_dynamic_cast_fn_p (fun)) |
2596 | return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p); | |
2597 | ||
2b3ab879 | 2598 | if (!ctx->quiet) |
2d76680f | 2599 | { |
11399477 | 2600 | if (!lambda_static_thunk_p (fun)) |
84fa214d | 2601 | error_at (loc, "call to non-%<constexpr%> function %qD", fun); |
2d76680f PC |
2602 | explain_invalid_constexpr_fn (fun); |
2603 | } | |
2604 | *non_constant_p = true; | |
2605 | return t; | |
2606 | } | |
2607 | ||
86461cad JM |
2608 | constexpr_ctx new_ctx = *ctx; |
2609 | if (DECL_CONSTRUCTOR_P (fun) && !ctx->object | |
2610 | && TREE_CODE (t) == AGGR_INIT_EXPR) | |
2611 | { | |
2612 | /* We want to have an initialization target for an AGGR_INIT_EXPR. | |
2613 | If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */ | |
2614 | new_ctx.object = AGGR_INIT_EXPR_SLOT (t); | |
2615 | tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL); | |
e8c48716 | 2616 | CONSTRUCTOR_NO_CLEARING (ctor) = true; |
8e007055 | 2617 | ctx->global->values.put (new_ctx.object, ctor); |
86461cad JM |
2618 | ctx = &new_ctx; |
2619 | } | |
2620 | ||
2d76680f PC |
2621 | /* Shortcut trivial constructor/op=. */ |
2622 | if (trivial_fn_p (fun)) | |
2623 | { | |
86461cad | 2624 | tree init = NULL_TREE; |
2d76680f | 2625 | if (call_expr_nargs (t) == 2) |
86461cad | 2626 | init = convert_from_reference (get_nth_callarg (t, 1)); |
2d76680f PC |
2627 | else if (TREE_CODE (t) == AGGR_INIT_EXPR |
2628 | && AGGR_INIT_ZERO_FIRST (t)) | |
86461cad JM |
2629 | init = build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false); |
2630 | if (init) | |
2631 | { | |
2632 | tree op = get_nth_callarg (t, 0); | |
2633 | if (is_dummy_object (op)) | |
2634 | op = ctx->object; | |
2635 | else | |
2636 | op = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (op)), op); | |
2637 | tree set = build2 (MODIFY_EXPR, TREE_TYPE (op), op, init); | |
c8816908 JM |
2638 | new_ctx.call = &new_call; |
2639 | return cxx_eval_constant_expression (&new_ctx, set, lval, | |
86461cad JM |
2640 | non_constant_p, overflow_p); |
2641 | } | |
2d76680f PC |
2642 | } |
2643 | ||
1595fe44 JM |
2644 | bool non_constant_args = false; |
2645 | new_call.bindings | |
2646 | = cxx_bind_parameters_in_call (ctx, t, fun, non_constant_p, | |
2647 | overflow_p, &non_constant_args); | |
2648 | ||
2649 | /* We build up the bindings list before we know whether we already have this | |
2650 | call cached. If we don't end up saving these bindings, ggc_free them when | |
2651 | this function exits. */ | |
2652 | class free_bindings | |
2653 | { | |
2654 | tree *bindings; | |
2655 | public: | |
2656 | free_bindings (tree &b): bindings (&b) { } | |
2657 | ~free_bindings () { if (bindings) ggc_free (*bindings); } | |
2658 | void preserve () { bindings = NULL; } | |
2659 | } fb (new_call.bindings); | |
2660 | ||
2661 | if (*non_constant_p) | |
2662 | return t; | |
2663 | ||
81371eff JM |
2664 | /* We can't defer instantiating the function any longer. */ |
2665 | if (!DECL_INITIAL (fun) | |
f65a3299 PP |
2666 | && DECL_TEMPLOID_INSTANTIATION (fun) |
2667 | && !uid_sensitive_constexpr_evaluation_p ()) | |
81371eff | 2668 | { |
f065303f JM |
2669 | location_t save_loc = input_location; |
2670 | input_location = loc; | |
81371eff | 2671 | ++function_depth; |
9aeadd8c JM |
2672 | if (ctx->manifestly_const_eval) |
2673 | FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true; | |
81371eff JM |
2674 | instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false); |
2675 | --function_depth; | |
f065303f | 2676 | input_location = save_loc; |
81371eff JM |
2677 | } |
2678 | ||
2d76680f | 2679 | /* If in direct recursive call, optimize definition search. */ |
c8816908 | 2680 | if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun) |
3e605b20 | 2681 | new_call.fundef = ctx->call->fundef; |
2d76680f PC |
2682 | else |
2683 | { | |
2684 | new_call.fundef = retrieve_constexpr_fundef (fun); | |
4ddcdbf9 | 2685 | if (new_call.fundef == NULL || new_call.fundef->body == NULL |
4f05d85a | 2686 | || new_call.fundef->result == error_mark_node |
4ddcdbf9 | 2687 | || fun == current_function_decl) |
2d76680f | 2688 | { |
2b3ab879 | 2689 | if (!ctx->quiet) |
2d76680f | 2690 | { |
4ddcdbf9 JM |
2691 | /* We need to check for current_function_decl here in case we're |
2692 | being called during cp_fold_function, because at that point | |
2693 | DECL_INITIAL is set properly and we have a fundef but we | |
2694 | haven't lowered invisirefs yet (c++/70344). */ | |
2695 | if (DECL_INITIAL (fun) == error_mark_node | |
2696 | || fun == current_function_decl) | |
ddd6d421 JM |
2697 | error_at (loc, "%qD called in a constant expression before its " |
2698 | "definition is complete", fun); | |
2699 | else if (DECL_INITIAL (fun)) | |
2d76680f | 2700 | { |
98e5a19a JM |
2701 | /* The definition of fun was somehow unsuitable. But pretend |
2702 | that lambda static thunks don't exist. */ | |
2703 | if (!lambda_static_thunk_p (fun)) | |
2704 | error_at (loc, "%qD called in a constant expression", fun); | |
2d76680f PC |
2705 | explain_invalid_constexpr_fn (fun); |
2706 | } | |
2707 | else | |
2708 | error_at (loc, "%qD used before its definition", fun); | |
2709 | } | |
2710 | *non_constant_p = true; | |
2711 | return t; | |
2712 | } | |
2713 | } | |
60813a46 | 2714 | |
2d76680f PC |
2715 | depth_ok = push_cx_call_context (t); |
2716 | ||
caf17f3a | 2717 | /* Remember the object we are constructing or destructing. */ |
04e1749c | 2718 | tree new_obj = NULL_TREE; |
caf17f3a | 2719 | if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun)) |
04e1749c | 2720 | { |
caf17f3a | 2721 | /* In a cdtor, it should be the first `this' argument. |
04e1749c MP |
2722 | At this point it has already been evaluated in the call |
2723 | to cxx_bind_parameters_in_call. */ | |
2724 | new_obj = TREE_VEC_ELT (new_call.bindings, 0); | |
1189c038 | 2725 | new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj); |
64da1b76 PP |
2726 | |
2727 | if (ctx->call && ctx->call->fundef | |
2728 | && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl)) | |
2729 | { | |
2730 | tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0); | |
2731 | STRIP_NOPS (cur_obj); | |
2732 | if (TREE_CODE (cur_obj) == ADDR_EXPR) | |
2733 | cur_obj = TREE_OPERAND (cur_obj, 0); | |
2734 | if (new_obj == cur_obj) | |
2735 | /* We're calling the target constructor of a delegating | |
2736 | constructor, or accessing a base subobject through a | |
2737 | NOP_EXPR as part of a call to a base constructor, so | |
2738 | there is no new (sub)object. */ | |
2739 | new_obj = NULL_TREE; | |
2740 | } | |
04e1749c MP |
2741 | } |
2742 | ||
12d9ce19 | 2743 | tree result = NULL_TREE; |
2d76680f | 2744 | |
12d9ce19 | 2745 | constexpr_call *entry = NULL; |
4a58d2fe | 2746 | if (depth_ok && !non_constant_args && ctx->strict) |
2d76680f | 2747 | { |
cce3ae91 JJ |
2748 | new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef); |
2749 | new_call.hash | |
2750 | = iterative_hash_template_arg (new_call.bindings, new_call.hash); | |
2751 | new_call.hash | |
13de99bc | 2752 | = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash); |
12d9ce19 JM |
2753 | |
2754 | /* If we have seen this call before, we are done. */ | |
2755 | maybe_initialize_constexpr_call_table (); | |
2756 | constexpr_call **slot | |
2757 | = constexpr_call_table->find_slot (&new_call, INSERT); | |
2758 | entry = *slot; | |
2759 | if (entry == NULL) | |
2760 | { | |
7ffc7de5 JM |
2761 | /* Only cache up to constexpr_cache_depth to limit memory use. */ |
2762 | if (depth_ok < constexpr_cache_depth) | |
2763 | { | |
2764 | /* We need to keep a pointer to the entry, not just the slot, as | |
2765 | the slot can move during evaluation of the body. */ | |
2766 | *slot = entry = ggc_alloc<constexpr_call> (); | |
2767 | *entry = new_call; | |
2768 | fb.preserve (); | |
2769 | } | |
12d9ce19 | 2770 | } |
7ffc7de5 JM |
2771 | /* Calls that are in progress have their result set to NULL, so that we |
2772 | can detect circular dependencies. Now that we only cache up to | |
2773 | constexpr_cache_depth this won't catch circular dependencies that | |
2774 | start deeper, but they'll hit the recursion or ops limit. */ | |
12d9ce19 JM |
2775 | else if (entry->result == NULL) |
2776 | { | |
2777 | if (!ctx->quiet) | |
2778 | error ("call has circular dependency"); | |
2779 | *non_constant_p = true; | |
2780 | entry->result = result = error_mark_node; | |
2781 | } | |
2782 | else | |
2783 | result = entry->result; | |
2d76680f PC |
2784 | } |
2785 | ||
2786 | if (!depth_ok) | |
2787 | { | |
2b3ab879 | 2788 | if (!ctx->quiet) |
84fa214d | 2789 | error ("%<constexpr%> evaluation depth exceeds maximum of %d (use " |
7e7a6ed7 | 2790 | "%<-fconstexpr-depth=%> to increase the maximum)", |
2d76680f PC |
2791 | max_constexpr_depth); |
2792 | *non_constant_p = true; | |
12d9ce19 | 2793 | result = error_mark_node; |
2d76680f PC |
2794 | } |
2795 | else | |
2796 | { | |
f74f6092 | 2797 | bool cacheable = true; |
de54de93 JM |
2798 | if (result && result != error_mark_node) |
2799 | /* OK */; | |
2800 | else if (!DECL_SAVED_TREE (fun)) | |
2801 | { | |
2802 | /* When at_eof >= 2, cgraph has started throwing away | |
2803 | DECL_SAVED_TREE, so fail quietly. FIXME we get here because of | |
2804 | late code generation for VEC_INIT_EXPR, which needs to be | |
2805 | completely reconsidered. */ | |
2806 | gcc_assert (at_eof >= 2 && ctx->quiet); | |
2807 | *non_constant_p = true; | |
2808 | } | |
f65a3299 | 2809 | else if (tree copy = get_fundef_copy (new_call.fundef)) |
3e605b20 | 2810 | { |
c0daf32d | 2811 | tree body, parms, res; |
620adbec | 2812 | releasing_vec ctors; |
0567dcd2 | 2813 | |
c0daf32d | 2814 | /* Reuse or create a new unshared copy of this function's body. */ |
97f3003f JM |
2815 | body = TREE_PURPOSE (copy); |
2816 | parms = TREE_VALUE (copy); | |
2817 | res = TREE_TYPE (copy); | |
0567dcd2 MP |
2818 | |
2819 | /* Associate the bindings with the remapped parms. */ | |
2820 | tree bound = new_call.bindings; | |
2821 | tree remapped = parms; | |
3c961dc7 | 2822 | for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i) |
60813a46 | 2823 | { |
3c961dc7 | 2824 | tree arg = TREE_VEC_ELT (bound, i); |
4953b790 JM |
2825 | if (entry) |
2826 | { | |
2827 | /* Unshare args going into the hash table to separate them | |
2828 | from the caller's context, for better GC and to avoid | |
2829 | problems with verify_gimple. */ | |
db38a029 | 2830 | arg = unshare_expr_without_location (arg); |
4953b790 | 2831 | TREE_VEC_ELT (bound, i) = arg; |
5afd90c5 JJ |
2832 | |
2833 | /* And then unshare again so the callee doesn't change the | |
2834 | argument values in the hash table. XXX Could we unshare | |
2835 | lazily in cxx_eval_store_expression? */ | |
2836 | arg = unshare_constructor (arg); | |
2837 | if (TREE_CODE (arg) == CONSTRUCTOR) | |
2838 | vec_safe_push (ctors, arg); | |
4953b790 | 2839 | } |
8e007055 | 2840 | ctx->global->values.put (remapped, arg); |
0567dcd2 | 2841 | remapped = DECL_CHAIN (remapped); |
60813a46 | 2842 | } |
0567dcd2 | 2843 | /* Add the RESULT_DECL to the values map, too. */ |
cd3ca6cb JM |
2844 | gcc_assert (!DECL_BY_REFERENCE (res)); |
2845 | ctx->global->values.put (res, NULL_TREE); | |
60813a46 | 2846 | |
ee1de08d JJ |
2847 | /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that |
2848 | we can forget their values after the call. */ | |
c0daf32d | 2849 | constexpr_ctx ctx_with_save_exprs = *ctx; |
0ee28590 | 2850 | auto_vec<tree, 10> save_exprs; |
c0daf32d | 2851 | ctx_with_save_exprs.save_exprs = &save_exprs; |
1e163090 | 2852 | ctx_with_save_exprs.call = &new_call; |
f74f6092 JJ |
2853 | unsigned save_heap_alloc_count = ctx->global->heap_vars.length (); |
2854 | unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count; | |
c0daf32d | 2855 | |
caf17f3a MP |
2856 | /* If this is a constexpr destructor, the object's const and volatile |
2857 | semantics are no longer in effect; see [class.dtor]p5. */ | |
2858 | if (new_obj && DECL_DESTRUCTOR_P (fun)) | |
2859 | cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false, | |
2860 | non_constant_p, overflow_p); | |
2861 | ||
0567dcd2 | 2862 | tree jump_target = NULL_TREE; |
c0daf32d | 2863 | cxx_eval_constant_expression (&ctx_with_save_exprs, body, |
72f76540 | 2864 | vc_discard, non_constant_p, overflow_p, |
0567dcd2 | 2865 | &jump_target); |
60813a46 | 2866 | |
0567dcd2 | 2867 | if (DECL_CONSTRUCTOR_P (fun)) |
de07cff9 PP |
2868 | { |
2869 | /* This can be null for a subobject constructor call, in | |
2870 | which case what we care about is the initialization | |
2871 | side-effects rather than the value. We could get at the | |
2872 | value by evaluating *this, but we don't bother; there's | |
2873 | no need to put such a call in the hash table. */ | |
2874 | result = lval ? ctx->object : ctx->ctor; | |
2875 | ||
2876 | /* If we've just evaluated a subobject constructor call for an | |
2877 | empty union member, it might not have produced a side effect | |
2878 | that actually activated the union member. So produce such a | |
2879 | side effect now to ensure the union appears initialized. */ | |
2880 | if (!result && new_obj | |
2881 | && TREE_CODE (new_obj) == COMPONENT_REF | |
2882 | && TREE_CODE (TREE_TYPE | |
2883 | (TREE_OPERAND (new_obj, 0))) == UNION_TYPE | |
2884 | && is_really_empty_class (TREE_TYPE (new_obj), | |
2885 | /*ignore_vptr*/false)) | |
2886 | { | |
2887 | tree activate = build2 (MODIFY_EXPR, TREE_TYPE (new_obj), | |
2888 | new_obj, | |
2889 | build_constructor (TREE_TYPE (new_obj), | |
2890 | NULL)); | |
2891 | cxx_eval_constant_expression (ctx, activate, lval, | |
2892 | non_constant_p, overflow_p); | |
2893 | ggc_free (activate); | |
2894 | } | |
2895 | } | |
0567dcd2 MP |
2896 | else if (VOID_TYPE_P (TREE_TYPE (res))) |
2897 | result = void_node; | |
2898 | else | |
2899 | { | |
cd3ca6cb | 2900 | result = *ctx->global->values.get (res); |
5ce08ecb AO |
2901 | if (result == NULL_TREE && !*non_constant_p |
2902 | && !DECL_DESTRUCTOR_P (fun)) | |
60813a46 | 2903 | { |
0567dcd2 | 2904 | if (!ctx->quiet) |
84fa214d | 2905 | error ("%<constexpr%> call flows off the end " |
0567dcd2 MP |
2906 | "of the function"); |
2907 | *non_constant_p = true; | |
60813a46 | 2908 | } |
60813a46 | 2909 | } |
0567dcd2 | 2910 | |
04e1749c MP |
2911 | /* At this point, the object's constructor will have run, so |
2912 | the object is no longer under construction, and its possible | |
2913 | 'const' semantics now apply. Make a note of this fact by | |
2914 | marking the CONSTRUCTOR TREE_READONLY. */ | |
caf17f3a MP |
2915 | if (new_obj && DECL_CONSTRUCTOR_P (fun)) |
2916 | cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true, | |
2917 | non_constant_p, overflow_p); | |
04e1749c | 2918 | |
ee1de08d JJ |
2919 | /* Forget the saved values of the callee's SAVE_EXPRs and |
2920 | TARGET_EXPRs. */ | |
3f207ab3 | 2921 | for (tree save_expr : save_exprs) |
8e007055 | 2922 | ctx->global->values.remove (save_expr); |
c0daf32d | 2923 | |
0567dcd2 MP |
2924 | /* Remove the parms/result from the values map. Is it worth |
2925 | bothering to do this when the map itself is only live for | |
2926 | one constexpr evaluation? If so, maybe also clear out | |
2927 | other vars from call, maybe in BIND_EXPR handling? */ | |
8e007055 | 2928 | ctx->global->values.remove (res); |
0567dcd2 | 2929 | for (tree parm = parms; parm; parm = TREE_CHAIN (parm)) |
8e007055 | 2930 | ctx->global->values.remove (parm); |
c0daf32d | 2931 | |
620adbec JM |
2932 | /* Free any parameter CONSTRUCTORs we aren't returning directly. */ |
2933 | while (!ctors->is_empty ()) | |
2934 | { | |
2935 | tree c = ctors->pop (); | |
2936 | if (c != result) | |
2937 | free_constructor (c); | |
2938 | } | |
2939 | ||
c0daf32d PP |
2940 | /* Make the unshared function copy we used available for re-use. */ |
2941 | save_fundef_copy (fun, copy); | |
f74f6092 JJ |
2942 | |
2943 | /* If the call allocated some heap object that hasn't been | |
2944 | deallocated during the call, or if it deallocated some heap | |
2945 | object it has not allocated, the call isn't really stateless | |
2946 | for the constexpr evaluation and should not be cached. | |
2947 | It is fine if the call allocates something and deallocates it | |
2948 | too. */ | |
2949 | if (entry | |
2950 | && (save_heap_alloc_count != ctx->global->heap_vars.length () | |
2951 | || (save_heap_dealloc_count | |
2952 | != ctx->global->heap_dealloc_count))) | |
2953 | { | |
2954 | tree heap_var; | |
2955 | unsigned int i; | |
2956 | if ((ctx->global->heap_vars.length () | |
2957 | - ctx->global->heap_dealloc_count) | |
2958 | != save_heap_alloc_count - save_heap_dealloc_count) | |
2959 | cacheable = false; | |
2960 | else | |
2961 | FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var, | |
2962 | save_heap_alloc_count) | |
2963 | if (DECL_NAME (heap_var) != heap_deleted_identifier) | |
2964 | { | |
2965 | cacheable = false; | |
2966 | break; | |
2967 | } | |
2968 | } | |
b2562229 PP |
2969 | |
2970 | /* Rewrite all occurrences of the function's RESULT_DECL with the | |
2971 | current object under construction. */ | |
2972 | if (!*non_constant_p && ctx->object | |
bb1f0b50 | 2973 | && CLASS_TYPE_P (TREE_TYPE (res)) |
b2562229 | 2974 | && !is_empty_class (TREE_TYPE (res))) |
4f6bc28f | 2975 | if (replace_decl (&result, res, ctx->object)) |
b2562229 | 2976 | cacheable = false; |
3e605b20 | 2977 | } |
aaa26bf4 JM |
2978 | else |
2979 | /* Couldn't get a function copy to evaluate. */ | |
2980 | *non_constant_p = true; | |
60813a46 | 2981 | |
2d76680f PC |
2982 | if (result == error_mark_node) |
2983 | *non_constant_p = true; | |
52228180 | 2984 | if (*non_constant_p || *overflow_p) |
12d9ce19 | 2985 | result = error_mark_node; |
0567dcd2 | 2986 | else if (!result) |
60813a46 | 2987 | result = void_node; |
12d9ce19 | 2988 | if (entry) |
f74f6092 | 2989 | entry->result = cacheable ? result : error_mark_node; |
2d76680f PC |
2990 | } |
2991 | ||
7906797e MP |
2992 | /* The result of a constexpr function must be completely initialized. |
2993 | ||
2994 | However, in C++20, a constexpr constructor doesn't necessarily have | |
2995 | to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING | |
2996 | in order to detect reading an unitialized object in constexpr instead | |
2997 | of value-initializing it. (reduced_constant_expression_p is expected to | |
2998 | take care of clearing the flag.) */ | |
2999 | if (TREE_CODE (result) == CONSTRUCTOR | |
b04445d4 | 3000 | && (cxx_dialect < cxx20 |
7906797e | 3001 | || !DECL_CONSTRUCTOR_P (fun))) |
ecc57615 | 3002 | clear_no_implicit_zero (result); |
f64e0c02 | 3003 | |
2d76680f | 3004 | pop_cx_call_context (); |
9b9eb42a | 3005 | return result; |
2d76680f PC |
3006 | } |
3007 | ||
7906797e MP |
3008 | /* Return true if T is a valid constant initializer. If a CONSTRUCTOR |
3009 | initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be | |
3010 | cleared. | |
3011 | FIXME speed this up, it's taking 16% of compile time on sieve testcase. */ | |
2d76680f PC |
3012 | |
3013 | bool | |
3014 | reduced_constant_expression_p (tree t) | |
3015 | { | |
4930c53e MP |
3016 | if (t == NULL_TREE) |
3017 | return false; | |
3018 | ||
2d76680f PC |
3019 | switch (TREE_CODE (t)) |
3020 | { | |
3021 | case PTRMEM_CST: | |
3022 | /* Even if we can't lower this yet, it's constant. */ | |
3023 | return true; | |
3024 | ||
3025 | case CONSTRUCTOR: | |
3026 | /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */ | |
fae00a0a | 3027 | tree field; |
e8c48716 | 3028 | if (CONSTRUCTOR_NO_CLEARING (t)) |
6f11ddd8 JM |
3029 | { |
3030 | if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE) | |
3031 | /* An initialized vector would have a VECTOR_CST. */ | |
3032 | return false; | |
e948436e | 3033 | else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) |
baf05d54 PP |
3034 | { |
3035 | /* There must be a valid constant initializer at every array | |
3036 | index. */ | |
3037 | tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t))); | |
3038 | tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t))); | |
3039 | tree cursor = min; | |
fae00a0a | 3040 | for (auto &e: CONSTRUCTOR_ELTS (t)) |
baf05d54 | 3041 | { |
fae00a0a | 3042 | if (!reduced_constant_expression_p (e.value)) |
baf05d54 | 3043 | return false; |
fae00a0a | 3044 | if (array_index_cmp (cursor, e.index) != 0) |
baf05d54 | 3045 | return false; |
fae00a0a JM |
3046 | if (TREE_CODE (e.index) == RANGE_EXPR) |
3047 | cursor = TREE_OPERAND (e.index, 1); | |
baf05d54 PP |
3048 | cursor = int_const_binop (PLUS_EXPR, cursor, size_one_node); |
3049 | } | |
3050 | if (find_array_ctor_elt (t, max) == -1) | |
3051 | return false; | |
3052 | goto ok; | |
3053 | } | |
b04445d4 | 3054 | else if (cxx_dialect >= cxx20 |
b599bf9d PP |
3055 | && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE) |
3056 | { | |
3057 | if (CONSTRUCTOR_NELTS (t) == 0) | |
3058 | /* An initialized union has a constructor element. */ | |
3059 | return false; | |
3060 | /* And it only initializes one member. */ | |
3061 | field = NULL_TREE; | |
3062 | } | |
6f11ddd8 | 3063 | else |
0c7bce0a | 3064 | field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t))); |
6f11ddd8 | 3065 | } |
7368cfa4 JM |
3066 | else |
3067 | field = NULL_TREE; | |
fae00a0a | 3068 | for (auto &e: CONSTRUCTOR_ELTS (t)) |
125db6a1 | 3069 | { |
4930c53e MP |
3070 | /* If VAL is null, we're in the middle of initializing this |
3071 | element. */ | |
fae00a0a | 3072 | if (!reduced_constant_expression_p (e.value)) |
125db6a1 | 3073 | return false; |
d6ff2207 | 3074 | /* Empty class field may or may not have an initializer. */ |
fae00a0a | 3075 | for (; field && e.index != field; |
0c7bce0a | 3076 | field = next_subobject_field (DECL_CHAIN (field))) |
d6ff2207 MP |
3077 | if (!is_really_empty_class (TREE_TYPE (field), |
3078 | /*ignore_vptr*/false)) | |
3079 | return false; | |
7368cfa4 | 3080 | if (field) |
0c7bce0a | 3081 | field = next_subobject_field (DECL_CHAIN (field)); |
125db6a1 | 3082 | } |
7906797e | 3083 | /* There could be a non-empty field at the end. */ |
0c7bce0a | 3084 | for (; field; field = next_subobject_field (DECL_CHAIN (field))) |
7906797e MP |
3085 | if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false)) |
3086 | return false; | |
baf05d54 | 3087 | ok: |
7906797e | 3088 | if (CONSTRUCTOR_NO_CLEARING (t)) |
7368cfa4 | 3089 | /* All the fields are initialized. */ |
e8c48716 | 3090 | CONSTRUCTOR_NO_CLEARING (t) = false; |
2d76680f PC |
3091 | return true; |
3092 | ||
3093 | default: | |
3094 | /* FIXME are we calling this too much? */ | |
3095 | return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE; | |
3096 | } | |
3097 | } | |
3098 | ||
3099 | /* Some expressions may have constant operands but are not constant | |
7d75ea04 JJ |
3100 | themselves, such as 1/0. Call this function to check for that |
3101 | condition. | |
2d76680f PC |
3102 | |
3103 | We only call this in places that require an arithmetic constant, not in | |
3104 | places where we might have a non-constant expression that can be a | |
3105 | component of a constant expression, such as the address of a constexpr | |
3106 | variable that might be dereferenced later. */ | |
3107 | ||
3108 | static bool | |
3109 | verify_constant (tree t, bool allow_non_constant, bool *non_constant_p, | |
3110 | bool *overflow_p) | |
3111 | { | |
596334fa JM |
3112 | if (!*non_constant_p && !reduced_constant_expression_p (t) |
3113 | && t != void_node) | |
2d76680f PC |
3114 | { |
3115 | if (!allow_non_constant) | |
3116 | error ("%q+E is not a constant expression", t); | |
3117 | *non_constant_p = true; | |
3118 | } | |
3119 | if (TREE_OVERFLOW_P (t)) | |
3120 | { | |
3121 | if (!allow_non_constant) | |
3122 | { | |
3123 | permerror (input_location, "overflow in constant expression"); | |
3124 | /* If we're being permissive (and are in an enforcing | |
3125 | context), ignore the overflow. */ | |
3126 | if (flag_permissive) | |
3127 | return *non_constant_p; | |
3128 | } | |
3129 | *overflow_p = true; | |
3130 | } | |
3131 | return *non_constant_p; | |
3132 | } | |
3133 | ||
253a921b MP |
3134 | /* Check whether the shift operation with code CODE and type TYPE on LHS |
3135 | and RHS is undefined. If it is, give an error with an explanation, | |
3136 | and return true; return false otherwise. */ | |
3137 | ||
3138 | static bool | |
3139 | cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx, | |
3140 | enum tree_code code, tree type, tree lhs, tree rhs) | |
3141 | { | |
3142 | if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR) | |
3143 | || TREE_CODE (lhs) != INTEGER_CST | |
3144 | || TREE_CODE (rhs) != INTEGER_CST) | |
3145 | return false; | |
3146 | ||
3147 | tree lhstype = TREE_TYPE (lhs); | |
3148 | unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs)); | |
3149 | ||
3150 | /* [expr.shift] The behavior is undefined if the right operand | |
3151 | is negative, or greater than or equal to the length in bits | |
3152 | of the promoted left operand. */ | |
3153 | if (tree_int_cst_sgn (rhs) == -1) | |
3154 | { | |
3155 | if (!ctx->quiet) | |
5342156c MP |
3156 | permerror (loc, "right operand of shift expression %q+E is negative", |
3157 | build2_loc (loc, code, type, lhs, rhs)); | |
3158 | return (!flag_permissive || ctx->quiet); | |
253a921b MP |
3159 | } |
3160 | if (compare_tree_int (rhs, uprec) >= 0) | |
3161 | { | |
3162 | if (!ctx->quiet) | |
a9c697b8 MS |
3163 | permerror (loc, "right operand of shift expression %q+E is greater " |
3164 | "than or equal to the precision %wu of the left operand", | |
3165 | build2_loc (loc, code, type, lhs, rhs), uprec); | |
5342156c | 3166 | return (!flag_permissive || ctx->quiet); |
253a921b MP |
3167 | } |
3168 | ||
3169 | /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...] | |
3170 | if E1 has a signed type and non-negative value, and E1x2^E2 is | |
3171 | representable in the corresponding unsigned type of the result type, | |
3172 | then that value, converted to the result type, is the resulting value; | |
8ee09943 | 3173 | otherwise, the behavior is undefined. |
b04445d4 | 3174 | For C++20: |
8ee09943 JJ |
3175 | The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo |
3176 | 2^N, where N is the range exponent of the type of the result. */ | |
3177 | if (code == LSHIFT_EXPR | |
d7651113 | 3178 | && !TYPE_OVERFLOW_WRAPS (lhstype) |
8ee09943 | 3179 | && cxx_dialect >= cxx11 |
b04445d4 | 3180 | && cxx_dialect < cxx20) |
253a921b MP |
3181 | { |
3182 | if (tree_int_cst_sgn (lhs) == -1) | |
3183 | { | |
3184 | if (!ctx->quiet) | |
5342156c MP |
3185 | permerror (loc, |
3186 | "left operand of shift expression %q+E is negative", | |
3187 | build2_loc (loc, code, type, lhs, rhs)); | |
3188 | return (!flag_permissive || ctx->quiet); | |
253a921b MP |
3189 | } |
3190 | /* For signed x << y the following: | |
3191 | (unsigned) x >> ((prec (lhs) - 1) - y) | |
3192 | if > 1, is undefined. The right-hand side of this formula | |
3193 | is the highest bit of the LHS that can be set (starting from 0), | |
3194 | so that the shift doesn't overflow. We then right-shift the LHS | |
3195 | to see whether any other bit is set making the original shift | |
3196 | undefined -- the result is not representable in the corresponding | |
3197 | unsigned type. */ | |
3198 | tree t = build_int_cst (unsigned_type_node, uprec - 1); | |
3199 | t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs); | |
3200 | tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs); | |
3201 | t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t); | |
3202 | if (tree_int_cst_lt (integer_one_node, t)) | |
3203 | { | |
3204 | if (!ctx->quiet) | |
5342156c MP |
3205 | permerror (loc, "shift expression %q+E overflows", |
3206 | build2_loc (loc, code, type, lhs, rhs)); | |
3207 | return (!flag_permissive || ctx->quiet); | |
253a921b MP |
3208 | } |
3209 | } | |
3210 | return false; | |
3211 | } | |
3212 | ||
2d76680f PC |
3213 | /* Subroutine of cxx_eval_constant_expression. |
3214 | Attempt to reduce the unary expression tree T to a compile time value. | |
3215 | If successful, return the value. Otherwise issue a diagnostic | |
3216 | and return error_mark_node. */ | |
3217 | ||
3218 | static tree | |
3e605b20 | 3219 | cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t, |
12d9ce19 | 3220 | bool /*lval*/, |
2d76680f PC |
3221 | bool *non_constant_p, bool *overflow_p) |
3222 | { | |
3223 | tree r; | |
3224 | tree orig_arg = TREE_OPERAND (t, 0); | |
72f76540 | 3225 | tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue, |
12d9ce19 | 3226 | non_constant_p, overflow_p); |
2d76680f | 3227 | VERIFY_CONSTANT (arg); |
f899317e JM |
3228 | location_t loc = EXPR_LOCATION (t); |
3229 | enum tree_code code = TREE_CODE (t); | |
3230 | tree type = TREE_TYPE (t); | |
3231 | r = fold_unary_loc (loc, code, type, arg); | |
3232 | if (r == NULL_TREE) | |
3233 | { | |
3234 | if (arg == orig_arg) | |
3235 | r = t; | |
3236 | else | |
3237 | r = build1_loc (loc, code, type, arg); | |
3238 | } | |
2d76680f PC |
3239 | VERIFY_CONSTANT (r); |
3240 | return r; | |
3241 | } | |
3242 | ||
ea8661cd JJ |
3243 | /* Helper function for cxx_eval_binary_expression. Try to optimize |
3244 | original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the | |
3245 | generic folding should be used. */ | |
3246 | ||
3247 | static tree | |
3248 | cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t, | |
3249 | tree lhs, tree rhs, bool *non_constant_p, | |
3250 | bool *overflow_p) | |
3251 | { | |
3252 | STRIP_NOPS (lhs); | |
3253 | if (TREE_CODE (lhs) != ADDR_EXPR) | |
3254 | return NULL_TREE; | |
3255 | ||
3256 | lhs = TREE_OPERAND (lhs, 0); | |
3257 | ||
3258 | /* &A[i] p+ j => &A[i + j] */ | |
3259 | if (TREE_CODE (lhs) == ARRAY_REF | |
3260 | && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST | |
3261 | && TREE_CODE (rhs) == INTEGER_CST | |
3262 | && TYPE_SIZE_UNIT (TREE_TYPE (lhs)) | |
3263 | && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST) | |
3264 | { | |
3265 | tree orig_type = TREE_TYPE (t); | |
3266 | location_t loc = EXPR_LOCATION (t); | |
3267 | tree type = TREE_TYPE (lhs); | |
3268 | ||
3269 | t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1)); | |
3270 | tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0))); | |
72f76540 JM |
3271 | nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue, |
3272 | non_constant_p, overflow_p); | |
ea8661cd JJ |
3273 | if (*non_constant_p) |
3274 | return NULL_TREE; | |
3275 | /* Don't fold an out-of-bound access. */ | |
3276 | if (!tree_int_cst_le (t, nelts)) | |
3277 | return NULL_TREE; | |
3278 | rhs = cp_fold_convert (ssizetype, rhs); | |
3279 | /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT. | |
3280 | constexpr int A[1]; ... (char *)&A[0] + 1 */ | |
3281 | if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype, | |
3282 | rhs, TYPE_SIZE_UNIT (type)))) | |
3283 | return NULL_TREE; | |
3284 | /* Make sure to treat the second operand of POINTER_PLUS_EXPR | |
3285 | as signed. */ | |
3286 | rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs, | |
3287 | TYPE_SIZE_UNIT (type)); | |
3288 | t = size_binop_loc (loc, PLUS_EXPR, rhs, t); | |
3289 | t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0), | |
3290 | t, NULL_TREE, NULL_TREE); | |
3291 | t = cp_build_addr_expr (t, tf_warning_or_error); | |
3292 | t = cp_fold_convert (orig_type, t); | |
72f76540 | 3293 | return cxx_eval_constant_expression (ctx, t, vc_prvalue, |
ea8661cd JJ |
3294 | non_constant_p, overflow_p); |
3295 | } | |
3296 | ||
3297 | return NULL_TREE; | |
3298 | } | |
3299 | ||
51d464b6 JJ |
3300 | /* Try to fold expressions like |
3301 | (struct S *) (&a[0].D.2378 + 12) | |
3302 | into | |
3303 | &MEM <struct T> [(void *)&a + 12B] | |
3304 | This is something normally done by gimple_fold_stmt_to_constant_1 | |
3305 | on GIMPLE, but is undesirable on GENERIC if we are e.g. going to | |
3306 | dereference the address because some details are lost. | |
3307 | For pointer comparisons we want such folding though so that | |
3308 | match.pd address_compare optimization works. */ | |
3309 | ||
3310 | static tree | |
3311 | cxx_maybe_fold_addr_pointer_plus (tree t) | |
3312 | { | |
3313 | while (CONVERT_EXPR_P (t) | |
3314 | && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0)))) | |
3315 | t = TREE_OPERAND (t, 0); | |
3316 | if (TREE_CODE (t) != POINTER_PLUS_EXPR) | |
3317 | return NULL_TREE; | |
3318 | tree op0 = TREE_OPERAND (t, 0); | |
3319 | tree op1 = TREE_OPERAND (t, 1); | |
3320 | if (TREE_CODE (op1) != INTEGER_CST) | |
3321 | return NULL_TREE; | |
3322 | while (CONVERT_EXPR_P (op0) | |
3323 | && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))) | |
3324 | op0 = TREE_OPERAND (op0, 0); | |
3325 | if (TREE_CODE (op0) != ADDR_EXPR) | |
3326 | return NULL_TREE; | |
3327 | op1 = fold_convert (ptr_type_node, op1); | |
3328 | tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1); | |
3329 | return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r); | |
3330 | } | |
3331 | ||
2d76680f PC |
3332 | /* Subroutine of cxx_eval_constant_expression. |
3333 | Like cxx_eval_unary_expression, except for binary expressions. */ | |
3334 | ||
3335 | static tree | |
3e605b20 | 3336 | cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t, |
72f76540 | 3337 | value_cat lval, |
2d76680f PC |
3338 | bool *non_constant_p, bool *overflow_p) |
3339 | { | |
618d6c1c | 3340 | tree r = NULL_TREE; |
2d76680f PC |
3341 | tree orig_lhs = TREE_OPERAND (t, 0); |
3342 | tree orig_rhs = TREE_OPERAND (t, 1); | |
3343 | tree lhs, rhs; | |
72f76540 | 3344 | lhs = cxx_eval_constant_expression (ctx, orig_lhs, vc_prvalue, |
5a804683 | 3345 | non_constant_p, overflow_p); |
c8a66fc9 JM |
3346 | /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer |
3347 | subtraction. */ | |
3348 | if (*non_constant_p) | |
3349 | return t; | |
72f76540 | 3350 | rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue, |
5a804683 | 3351 | non_constant_p, overflow_p); |
c8a66fc9 JM |
3352 | if (*non_constant_p) |
3353 | return t; | |
f899317e JM |
3354 | |
3355 | location_t loc = EXPR_LOCATION (t); | |
3356 | enum tree_code code = TREE_CODE (t); | |
3357 | tree type = TREE_TYPE (t); | |
618d6c1c PP |
3358 | |
3359 | if (code == EQ_EXPR || code == NE_EXPR) | |
3360 | { | |
3361 | bool is_code_eq = (code == EQ_EXPR); | |
3362 | ||
3363 | if (TREE_CODE (lhs) == PTRMEM_CST | |
3364 | && TREE_CODE (rhs) == PTRMEM_CST) | |
9b0607de JM |
3365 | { |
3366 | tree lmem = PTRMEM_CST_MEMBER (lhs); | |
3367 | tree rmem = PTRMEM_CST_MEMBER (rhs); | |
3368 | bool eq; | |
3369 | if (TREE_CODE (lmem) == TREE_CODE (rmem) | |
3370 | && TREE_CODE (lmem) == FIELD_DECL | |
3371 | && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE | |
3372 | && same_type_p (DECL_CONTEXT (lmem), | |
3373 | DECL_CONTEXT (rmem))) | |
3374 | /* If both refer to (possibly different) members of the same union | |
3375 | (12.3), they compare equal. */ | |
3376 | eq = true; | |
3377 | else | |
3378 | eq = cp_tree_equal (lhs, rhs); | |
3379 | r = constant_boolean_node (eq == is_code_eq, type); | |
3380 | } | |
618d6c1c PP |
3381 | else if ((TREE_CODE (lhs) == PTRMEM_CST |
3382 | || TREE_CODE (rhs) == PTRMEM_CST) | |
3383 | && (null_member_pointer_value_p (lhs) | |
3384 | || null_member_pointer_value_p (rhs))) | |
3385 | r = constant_boolean_node (!is_code_eq, type); | |
dd5dda56 JM |
3386 | else if (TREE_CODE (lhs) == PTRMEM_CST) |
3387 | lhs = cplus_expand_constant (lhs); | |
3388 | else if (TREE_CODE (rhs) == PTRMEM_CST) | |
3389 | rhs = cplus_expand_constant (rhs); | |
618d6c1c | 3390 | } |
51d464b6 JJ |
3391 | if (r == NULL_TREE |
3392 | && TREE_CODE_CLASS (code) == tcc_comparison | |
3393 | && POINTER_TYPE_P (TREE_TYPE (lhs))) | |
3394 | { | |
3395 | if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs)) | |
3396 | lhs = fold_convert (TREE_TYPE (lhs), lhso); | |
3397 | if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs)) | |
3398 | rhs = fold_convert (TREE_TYPE (rhs), rhso); | |
3399 | } | |
8bada5cd MS |
3400 | if (code == POINTER_PLUS_EXPR && !*non_constant_p |
3401 | && integer_zerop (lhs) && !integer_zerop (rhs)) | |
3402 | { | |
3403 | if (!ctx->quiet) | |
3404 | error ("arithmetic involving a null pointer in %qE", lhs); | |
bf533db8 | 3405 | *non_constant_p = true; |
8bada5cd MS |
3406 | return t; |
3407 | } | |
ea8661cd JJ |
3408 | else if (code == POINTER_PLUS_EXPR) |
3409 | r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p, | |
3410 | overflow_p); | |
b7689b96 JM |
3411 | else if (code == SPACESHIP_EXPR) |
3412 | { | |
4ed1dc12 | 3413 | r = genericize_spaceship (loc, type, lhs, rhs); |
5c64df80 JJ |
3414 | return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, |
3415 | overflow_p); | |
b7689b96 | 3416 | } |
618d6c1c PP |
3417 | |
3418 | if (r == NULL_TREE) | |
53caa472 JM |
3419 | { |
3420 | if (ctx->manifestly_const_eval | |
3421 | && (flag_constexpr_fp_except | |
3422 | || TREE_CODE (type) != REAL_TYPE)) | |
f9e900ce JJ |
3423 | { |
3424 | auto ofcc = make_temp_override (folding_cxx_constexpr, true); | |
3425 | r = fold_binary_initializer_loc (loc, code, type, lhs, rhs); | |
3426 | } | |
53caa472 JM |
3427 | else |
3428 | r = fold_binary_loc (loc, code, type, lhs, rhs); | |
3429 | } | |
618d6c1c | 3430 | |
4866b2f5 JJ |
3431 | if (r == NULL_TREE |
3432 | && (code == LSHIFT_EXPR || code == RSHIFT_EXPR) | |
3433 | && TREE_CODE (lhs) == INTEGER_CST | |
3434 | && TREE_CODE (rhs) == INTEGER_CST | |
3435 | && wi::neg_p (wi::to_wide (rhs))) | |
3436 | { | |
3437 | /* For diagnostics and -fpermissive emulate previous behavior of | |
3438 | handling shifts by negative amount. */ | |
3439 | tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs); | |
3440 | if (nrhs) | |
3441 | r = fold_binary_loc (loc, | |
3442 | code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR, | |
3443 | type, lhs, nrhs); | |
3444 | } | |
3445 | ||
f899317e JM |
3446 | if (r == NULL_TREE) |
3447 | { | |
3448 | if (lhs == orig_lhs && rhs == orig_rhs) | |
3449 | r = t; | |
3450 | else | |
3451 | r = build2_loc (loc, code, type, lhs, rhs); | |
3452 | } | |
253a921b MP |
3453 | else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs)) |
3454 | *non_constant_p = true; | |
c8a66fc9 JM |
3455 | /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to |
3456 | a local array in a constexpr function. */ | |
71a93b08 | 3457 | bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs)); |
caee690e | 3458 | if (!ptr) |
134efa82 | 3459 | VERIFY_CONSTANT (r); |
2d76680f PC |
3460 | return r; |
3461 | } | |
3462 | ||
3463 | /* Subroutine of cxx_eval_constant_expression. | |
3464 | Attempt to evaluate condition expressions. Dead branches are not | |
3465 | looked into. */ | |
3466 | ||
3467 | static tree | |
3e605b20 | 3468 | cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t, |
72f76540 | 3469 | value_cat lval, |
56632b27 JM |
3470 | bool *non_constant_p, bool *overflow_p, |
3471 | tree *jump_target) | |
2d76680f | 3472 | { |
3e605b20 | 3473 | tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
72f76540 | 3474 | vc_prvalue, |
5a804683 | 3475 | non_constant_p, overflow_p); |
2d76680f | 3476 | VERIFY_CONSTANT (val); |
117c6426 JJ |
3477 | if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t)) |
3478 | { | |
3479 | /* Evaluate the condition as if it was | |
3480 | if (__builtin_is_constant_evaluated ()), i.e. defer it if not | |
3481 | ctx->manifestly_const_eval (as sometimes we try to constant evaluate | |
3482 | without manifestly_const_eval even expressions or parts thereof which | |
3483 | will later be manifestly const_eval evaluated), otherwise fold it to | |
3484 | true. */ | |
3485 | if (ctx->manifestly_const_eval) | |
3486 | val = boolean_true_node; | |
3487 | else | |
3488 | { | |
3489 | *non_constant_p = true; | |
3490 | return t; | |
3491 | } | |
3492 | } | |
2d76680f PC |
3493 | /* Don't VERIFY_CONSTANT the other operands. */ |
3494 | if (integer_zerop (val)) | |
43574e4f JJ |
3495 | val = TREE_OPERAND (t, 2); |
3496 | else | |
3497 | val = TREE_OPERAND (t, 1); | |
3498 | if (TREE_CODE (t) == IF_STMT && !val) | |
3499 | val = void_node; | |
3500 | return cxx_eval_constant_expression (ctx, val, lval, non_constant_p, | |
3501 | overflow_p, jump_target); | |
2d76680f PC |
3502 | } |
3503 | ||
f370e36d JJ |
3504 | /* Subroutine of cxx_eval_constant_expression. |
3505 | Attempt to evaluate vector condition expressions. Unlike | |
3506 | cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal | |
3507 | ternary arithmetics operation, where all 3 arguments have to be | |
3508 | evaluated as constants and then folding computes the result from | |
3509 | them. */ | |
3510 | ||
3511 | static tree | |
3512 | cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t, | |
3513 | bool *non_constant_p, bool *overflow_p) | |
3514 | { | |
3515 | tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), | |
72f76540 | 3516 | vc_prvalue, |
f370e36d JJ |
3517 | non_constant_p, overflow_p); |
3518 | VERIFY_CONSTANT (arg1); | |
3519 | tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), | |
72f76540 | 3520 | vc_prvalue, |
f370e36d JJ |
3521 | non_constant_p, overflow_p); |
3522 | VERIFY_CONSTANT (arg2); | |
3523 | tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2), | |
72f76540 | 3524 | vc_prvalue, |
f370e36d JJ |
3525 | non_constant_p, overflow_p); |
3526 | VERIFY_CONSTANT (arg3); | |
3527 | location_t loc = EXPR_LOCATION (t); | |
3528 | tree type = TREE_TYPE (t); | |
3529 | tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); | |
3530 | if (r == NULL_TREE) | |
3531 | { | |
3532 | if (arg1 == TREE_OPERAND (t, 0) | |
3533 | && arg2 == TREE_OPERAND (t, 1) | |
3534 | && arg3 == TREE_OPERAND (t, 2)) | |
3535 | r = t; | |
3536 | else | |
3537 | r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); | |
3538 | } | |
3539 | VERIFY_CONSTANT (r); | |
3540 | return r; | |
3541 | } | |
3542 | ||
ceaaf873 JM |
3543 | /* Returns less than, equal to, or greater than zero if KEY is found to be |
3544 | less than, to match, or to be greater than the constructor_elt's INDEX. */ | |
3545 | ||
3546 | static int | |
3547 | array_index_cmp (tree key, tree index) | |
3548 | { | |
3549 | gcc_assert (TREE_CODE (key) == INTEGER_CST); | |
3550 | ||
3551 | switch (TREE_CODE (index)) | |
3552 | { | |
3553 | case INTEGER_CST: | |
3554 | return tree_int_cst_compare (key, index); | |
3555 | case RANGE_EXPR: | |
3556 | { | |
3557 | tree lo = TREE_OPERAND (index, 0); | |
3558 | tree hi = TREE_OPERAND (index, 1); | |
3559 | if (tree_int_cst_lt (key, lo)) | |
3560 | return -1; | |
3561 | else if (tree_int_cst_lt (hi, key)) | |
3562 | return 1; | |
3563 | else | |
3564 | return 0; | |
3565 | } | |
3566 | default: | |
3567 | gcc_unreachable (); | |
3568 | } | |
3569 | } | |
3570 | ||
3571 | /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1 | |
3572 | if none. If INSERT is true, insert a matching element rather than fail. */ | |
3573 | ||
3574 | static HOST_WIDE_INT | |
582d2481 | 3575 | find_array_ctor_elt (tree ary, tree dindex, bool insert) |
ceaaf873 JM |
3576 | { |
3577 | if (tree_int_cst_sgn (dindex) < 0) | |
3578 | return -1; | |
3579 | ||
3580 | unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex); | |
3581 | vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary); | |
3582 | unsigned HOST_WIDE_INT len = vec_safe_length (elts); | |
3583 | ||
3584 | unsigned HOST_WIDE_INT end = len; | |
3585 | unsigned HOST_WIDE_INT begin = 0; | |
3586 | ||
3587 | /* If the last element of the CONSTRUCTOR has its own index, we can assume | |
3588 | that the same is true of the other elements and index directly. */ | |
3589 | if (end > 0) | |
3590 | { | |
fe217ba0 | 3591 | tree cindex = (*elts)[end - 1].index; |
c7c09af8 JJ |
3592 | if (cindex == NULL_TREE) |
3593 | { | |
3594 | /* Verify that if the last index is missing, all indexes | |
3595 | are missing. */ | |
3596 | if (flag_checking) | |
3597 | for (unsigned int j = 0; j < len - 1; ++j) | |
3598 | gcc_assert ((*elts)[j].index == NULL_TREE); | |
3599 | if (i < end) | |
3600 | return i; | |
3601 | else | |
3602 | { | |
3603 | begin = end; | |
3604 | if (i == end) | |
3605 | /* If the element is to be added right at the end, | |
3606 | make sure it is added with cleared index too. */ | |
3607 | dindex = NULL_TREE; | |
3608 | else if (insert) | |
3609 | /* Otherwise, in order not to break the assumption | |
3610 | that CONSTRUCTOR either has all indexes or none, | |
3611 | we need to add indexes to all elements. */ | |
3612 | for (unsigned int j = 0; j < len; ++j) | |
3613 | (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j); | |
3614 | } | |
3615 | } | |
3616 | else if (TREE_CODE (cindex) == INTEGER_CST | |
3617 | && compare_tree_int (cindex, end - 1) == 0) | |
ceaaf873 JM |
3618 | { |
3619 | if (i < end) | |
3620 | return i; | |
3621 | else | |
3622 | begin = end; | |
3623 | } | |
3624 | } | |
3625 | ||
3626 | /* Otherwise, find a matching index by means of a binary search. */ | |
3627 | while (begin != end) | |
3628 | { | |
3629 | unsigned HOST_WIDE_INT middle = (begin + end) / 2; | |
a7ccb9e7 JM |
3630 | constructor_elt &elt = (*elts)[middle]; |
3631 | tree idx = elt.index; | |
ceaaf873 | 3632 | |
a7ccb9e7 | 3633 | int cmp = array_index_cmp (dindex, idx); |
ceaaf873 JM |
3634 | if (cmp < 0) |
3635 | end = middle; | |
3636 | else if (cmp > 0) | |
3637 | begin = middle + 1; | |
3638 | else | |
a7ccb9e7 JM |
3639 | { |
3640 | if (insert && TREE_CODE (idx) == RANGE_EXPR) | |
3641 | { | |
3642 | /* We need to split the range. */ | |
3643 | constructor_elt e; | |
3644 | tree lo = TREE_OPERAND (idx, 0); | |
3645 | tree hi = TREE_OPERAND (idx, 1); | |
fe217ba0 JJ |
3646 | tree value = elt.value; |
3647 | dindex = fold_convert (sizetype, dindex); | |
a7ccb9e7 JM |
3648 | if (tree_int_cst_lt (lo, dindex)) |
3649 | { | |
3650 | /* There are still some lower elts; shorten the range. */ | |
3651 | tree new_hi = int_const_binop (MINUS_EXPR, dindex, | |
3652 | size_one_node); | |
3653 | if (tree_int_cst_equal (lo, new_hi)) | |
3654 | /* Only one element left, no longer a range. */ | |
3655 | elt.index = lo; | |
3656 | else | |
3657 | TREE_OPERAND (idx, 1) = new_hi; | |
3658 | /* Append the element we want to insert. */ | |
3659 | ++middle; | |
3660 | e.index = dindex; | |
fe217ba0 | 3661 | e.value = unshare_constructor (value); |
a7ccb9e7 JM |
3662 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e); |
3663 | } | |
3664 | else | |
3665 | /* No lower elts, the range elt is now ours. */ | |
3666 | elt.index = dindex; | |
3667 | ||
3668 | if (tree_int_cst_lt (dindex, hi)) | |
3669 | { | |
3670 | /* There are still some higher elts; append a range. */ | |
3671 | tree new_lo = int_const_binop (PLUS_EXPR, dindex, | |
3672 | size_one_node); | |
3673 | if (tree_int_cst_equal (new_lo, hi)) | |
3674 | e.index = hi; | |
3675 | else | |
3676 | e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi); | |
fe217ba0 JJ |
3677 | e.value = unshare_constructor (value); |
3678 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e); | |
a7ccb9e7 JM |
3679 | } |
3680 | } | |
3681 | return middle; | |
3682 | } | |
ceaaf873 JM |
3683 | } |
3684 | ||
3685 | if (insert) | |
3686 | { | |
3687 | constructor_elt e = { dindex, NULL_TREE }; | |
3688 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e); | |
3689 | return end; | |
3690 | } | |
3691 | ||
3692 | return -1; | |
3693 | } | |
3694 | ||
37244b21 PP |
3695 | /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no |
3696 | matching constructor_elt exists, then add one to CTOR. | |
3697 | ||
3698 | As an optimization, if POS_HINT is non-negative then it is used as a guess | |
3699 | for the (integer) index of the matching constructor_elt within CTOR. */ | |
3700 | ||
3701 | static constructor_elt * | |
077dd9b3 | 3702 | get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1) |
37244b21 PP |
3703 | { |
3704 | /* Check the hint first. */ | |
3705 | if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor) | |
3706 | && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index) | |
3707 | return CONSTRUCTOR_ELT (ctor, pos_hint); | |
3708 | ||
3709 | tree type = TREE_TYPE (ctor); | |
3710 | if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE) | |
3711 | { | |
3712 | CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE); | |
3713 | return &CONSTRUCTOR_ELTS (ctor)->last(); | |
3714 | } | |
3715 | else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE) | |
3716 | { | |
e069285c PP |
3717 | if (TREE_CODE (index) == RANGE_EXPR) |
3718 | { | |
3719 | /* Support for RANGE_EXPR index lookups is currently limited to | |
3720 | accessing an existing element via POS_HINT, or appending a new | |
3721 | element to the end of CTOR. ??? Support for other access | |
3722 | patterns may also be needed. */ | |
3723 | vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor); | |
3724 | if (vec_safe_length (elts)) | |
3725 | { | |
3726 | tree lo = TREE_OPERAND (index, 0); | |
3727 | gcc_assert (array_index_cmp (elts->last().index, lo) < 0); | |
3728 | } | |
3729 | CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE); | |
3730 | return &elts->last(); | |
3731 | } | |
3732 | ||
37244b21 PP |
3733 | HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, /*insert*/true); |
3734 | gcc_assert (i >= 0); | |
3735 | constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i); | |
3736 | gcc_assert (cep->index == NULL_TREE | |
3737 | || TREE_CODE (cep->index) != RANGE_EXPR); | |
3738 | return cep; | |
3739 | } | |
3740 | else | |
3741 | { | |
94ff4c9d JM |
3742 | gcc_assert (TREE_CODE (index) == FIELD_DECL |
3743 | && (same_type_ignoring_top_level_qualifiers_p | |
3744 | (DECL_CONTEXT (index), TREE_TYPE (ctor)))); | |
37244b21 PP |
3745 | |
3746 | /* We must keep the CONSTRUCTOR's ELTS in FIELD order. | |
3747 | Usually we meet initializers in that order, but it is | |
3748 | possible for base types to be placed not in program | |
3749 | order. */ | |
3750 | tree fields = TYPE_FIELDS (DECL_CONTEXT (index)); | |
3751 | unsigned HOST_WIDE_INT idx = 0; | |
3752 | constructor_elt *cep = NULL; | |
3753 | ||
3754 | /* Check if we're changing the active member of a union. */ | |
3755 | if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor) | |
3756 | && CONSTRUCTOR_ELT (ctor, 0)->index != index) | |
3757 | vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0); | |
3758 | /* If the bit offset of INDEX is larger than that of the last | |
3759 | constructor_elt, then we can just immediately append a new | |
3760 | constructor_elt to the end of CTOR. */ | |
3761 | else if (CONSTRUCTOR_NELTS (ctor) | |
3762 | && tree_int_cst_compare (bit_position (index), | |
3763 | bit_position (CONSTRUCTOR_ELTS (ctor) | |
3764 | ->last().index)) > 0) | |
3765 | { | |
3766 | idx = CONSTRUCTOR_NELTS (ctor); | |
3767 | goto insert; | |
3768 | } | |
3769 | ||
3770 | /* Otherwise, we need to iterate over CTOR to find or insert INDEX | |
3771 | appropriately. */ | |
3772 | ||
3773 | for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep); | |
3774 | idx++, fields = DECL_CHAIN (fields)) | |
3775 | { | |
3776 | if (index == cep->index) | |
3777 | goto found; | |
3778 | ||
3779 | /* The field we're initializing must be on the field | |
3780 | list. Look to see if it is present before the | |
3781 | field the current ELT initializes. */ | |
3782 | for (; fields != cep->index; fields = DECL_CHAIN (fields)) | |
3783 | if (index == fields) | |
3784 | goto insert; | |
3785 | } | |
3786 | /* We fell off the end of the CONSTRUCTOR, so insert a new | |
3787 | entry at the end. */ | |
3788 | ||
3789 | insert: | |
3790 | { | |
3791 | constructor_elt ce = { index, NULL_TREE }; | |
3792 | ||
3793 | vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce); | |
3794 | cep = CONSTRUCTOR_ELT (ctor, idx); | |
3795 | } | |
3796 | found:; | |
3797 | ||
3798 | return cep; | |
3799 | } | |
3800 | } | |
3801 | ||
abdc16c8 MS |
3802 | /* Under the control of CTX, issue a detailed diagnostic for |
3803 | an out-of-bounds subscript INDEX into the expression ARRAY. */ | |
3804 | ||
3805 | static void | |
043666e0 | 3806 | diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index) |
abdc16c8 MS |
3807 | { |
3808 | if (!ctx->quiet) | |
3809 | { | |
3810 | tree arraytype = TREE_TYPE (array); | |
3811 | ||
3812 | /* Convert the unsigned array subscript to a signed integer to avoid | |
3813 | printing huge numbers for small negative values. */ | |
3814 | tree sidx = fold_convert (ssizetype, index); | |
043666e0 | 3815 | STRIP_ANY_LOCATION_WRAPPER (array); |
abdc16c8 MS |
3816 | if (DECL_P (array)) |
3817 | { | |
08b3748c | 3818 | if (TYPE_DOMAIN (arraytype)) |
043666e0 JM |
3819 | error_at (loc, "array subscript value %qE is outside the bounds " |
3820 | "of array %qD of type %qT", sidx, array, arraytype); | |
08b3748c | 3821 | else |
043666e0 JM |
3822 | error_at (loc, "nonzero array subscript %qE is used with array %qD of " |
3823 | "type %qT with unknown bounds", sidx, array, arraytype); | |
abdc16c8 MS |
3824 | inform (DECL_SOURCE_LOCATION (array), "declared here"); |
3825 | } | |
08b3748c | 3826 | else if (TYPE_DOMAIN (arraytype)) |
043666e0 JM |
3827 | error_at (loc, "array subscript value %qE is outside the bounds " |
3828 | "of array type %qT", sidx, arraytype); | |
08b3748c | 3829 | else |
043666e0 JM |
3830 | error_at (loc, "nonzero array subscript %qE is used with array of type %qT " |
3831 | "with unknown bounds", sidx, arraytype); | |
abdc16c8 MS |
3832 | } |
3833 | } | |
ceaaf873 | 3834 | |
74f8705e MP |
3835 | /* Return the number of elements for TYPE (which is an ARRAY_TYPE or |
3836 | a VECTOR_TYPE). */ | |
3837 | ||
3838 | static tree | |
3839 | get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type, | |
3840 | bool *non_constant_p, bool *overflow_p) | |
3841 | { | |
3842 | tree nelts; | |
3843 | if (TREE_CODE (type) == ARRAY_TYPE) | |
3844 | { | |
3845 | if (TYPE_DOMAIN (type)) | |
3846 | nelts = array_type_nelts_top (type); | |
3847 | else | |
3848 | nelts = size_zero_node; | |
3849 | } | |
3850 | else if (VECTOR_TYPE_P (type)) | |
3851 | nelts = size_int (TYPE_VECTOR_SUBPARTS (type)); | |
3852 | else | |
3853 | gcc_unreachable (); | |
3854 | ||
3855 | /* For VLAs, the number of elements won't be an integer constant. */ | |
72f76540 | 3856 | nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue, |
74f8705e MP |
3857 | non_constant_p, overflow_p); |
3858 | return nelts; | |
3859 | } | |
3860 | ||
d6b46fca NS |
3861 | /* Extract element INDEX consisting of CHARS_PER_ELT chars from |
3862 | STRING_CST STRING. */ | |
3863 | ||
3864 | static tree | |
3865 | extract_string_elt (tree string, unsigned chars_per_elt, unsigned index) | |
3866 | { | |
3867 | tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string))); | |
3868 | tree r; | |
3869 | ||
3870 | if (chars_per_elt == 1) | |
3871 | r = build_int_cst (type, TREE_STRING_POINTER (string)[index]); | |
3872 | else | |
3873 | { | |
3874 | const unsigned char *ptr | |
3875 | = ((const unsigned char *)TREE_STRING_POINTER (string) | |
3876 | + index * chars_per_elt); | |
3877 | r = native_interpret_expr (type, ptr, chars_per_elt); | |
3878 | } | |
3879 | return r; | |
3880 | } | |
3881 | ||
043666e0 JM |
3882 | /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the |
3883 | subscript, diagnose any problems with it, and return the result. */ | |
3884 | ||
3885 | static tree | |
3886 | eval_and_check_array_index (const constexpr_ctx *ctx, | |
3887 | tree t, bool allow_one_past, | |
3888 | bool *non_constant_p, bool *overflow_p) | |
3889 | { | |
f9d0ca40 | 3890 | location_t loc = cp_expr_loc_or_input_loc (t); |
043666e0 JM |
3891 | tree ary = TREE_OPERAND (t, 0); |
3892 | t = TREE_OPERAND (t, 1); | |
72f76540 | 3893 | tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue, |
043666e0 JM |
3894 | non_constant_p, overflow_p); |
3895 | VERIFY_CONSTANT (index); | |
3896 | ||
3897 | if (!tree_fits_shwi_p (index) | |
3898 | || tree_int_cst_sgn (index) < 0) | |
3899 | { | |
3900 | diag_array_subscript (loc, ctx, ary, index); | |
3901 | *non_constant_p = true; | |
3902 | return t; | |
3903 | } | |
3904 | ||
3905 | tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p, | |
3906 | overflow_p); | |
3907 | VERIFY_CONSTANT (nelts); | |
3908 | if (allow_one_past | |
3909 | ? !tree_int_cst_le (index, nelts) | |
3910 | : !tree_int_cst_lt (index, nelts)) | |
3911 | { | |
3912 | diag_array_subscript (loc, ctx, ary, index); | |
3913 | *non_constant_p = true; | |
3914 | return t; | |
3915 | } | |
3916 | ||
3917 | return index; | |
3918 | } | |
3919 | ||
2d76680f PC |
3920 | /* Subroutine of cxx_eval_constant_expression. |
3921 | Attempt to reduce a reference to an array slot. */ | |
3922 | ||
3923 | static tree | |
3e605b20 | 3924 | cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, |
72f76540 | 3925 | value_cat lval, |
2d76680f PC |
3926 | bool *non_constant_p, bool *overflow_p) |
3927 | { | |
3928 | tree oldary = TREE_OPERAND (t, 0); | |
3e605b20 | 3929 | tree ary = cxx_eval_constant_expression (ctx, oldary, |
92a596e8 | 3930 | lval, |
5a804683 | 3931 | non_constant_p, overflow_p); |
2d76680f PC |
3932 | if (*non_constant_p) |
3933 | return t; | |
bc2687dd JJ |
3934 | if (!lval |
3935 | && TREE_CODE (ary) == VIEW_CONVERT_EXPR | |
043666e0 JM |
3936 | && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0))) |
3937 | && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0)))) | |
3938 | ary = TREE_OPERAND (ary, 0); | |
3b9997bb | 3939 | |
043666e0 JM |
3940 | tree oldidx = TREE_OPERAND (t, 1); |
3941 | tree index = eval_and_check_array_index (ctx, t, lval, | |
3942 | non_constant_p, overflow_p); | |
3943 | if (*non_constant_p) | |
3944 | return t; | |
5453bfed | 3945 | |
bc2a38df JJ |
3946 | if (lval && ary == oldary && index == oldidx) |
3947 | return t; | |
72f76540 JM |
3948 | else if (lval == vc_discard) |
3949 | return t; | |
bc2a38df JJ |
3950 | else if (lval) |
3951 | return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL); | |
3952 | ||
043666e0 JM |
3953 | unsigned len = 0, elem_nchars = 1; |
3954 | tree elem_type = TREE_TYPE (TREE_TYPE (ary)); | |
3955 | if (TREE_CODE (ary) == CONSTRUCTOR) | |
3956 | len = CONSTRUCTOR_NELTS (ary); | |
3957 | else if (TREE_CODE (ary) == STRING_CST) | |
3958 | { | |
3959 | elem_nchars = (TYPE_PRECISION (elem_type) | |
3960 | / TYPE_PRECISION (char_type_node)); | |
3961 | len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars; | |
3962 | } | |
3963 | else if (TREE_CODE (ary) == VECTOR_CST) | |
3964 | /* We don't create variable-length VECTOR_CSTs. */ | |
3965 | len = VECTOR_CST_NELTS (ary).to_constant (); | |
3966 | else | |
3967 | { | |
3968 | /* We can't do anything with other tree codes, so use | |
3969 | VERIFY_CONSTANT to complain and fail. */ | |
3970 | VERIFY_CONSTANT (ary); | |
3971 | gcc_unreachable (); | |
3972 | } | |
3973 | ||
ceaaf873 | 3974 | bool found; |
043666e0 | 3975 | HOST_WIDE_INT i = 0; |
ceaaf873 JM |
3976 | if (TREE_CODE (ary) == CONSTRUCTOR) |
3977 | { | |
3978 | HOST_WIDE_INT ix = find_array_ctor_elt (ary, index); | |
3979 | found = (ix >= 0); | |
3980 | if (found) | |
3981 | i = ix; | |
3b9997bb | 3982 | } |
ceaaf873 | 3983 | else |
043666e0 JM |
3984 | { |
3985 | i = tree_to_shwi (index); | |
3986 | found = (i < len); | |
3987 | } | |
3b9997bb | 3988 | |
fd2bfee5 JM |
3989 | if (found) |
3990 | { | |
3991 | tree r; | |
3992 | if (TREE_CODE (ary) == CONSTRUCTOR) | |
3993 | r = (*CONSTRUCTOR_ELTS (ary))[i].value; | |
3994 | else if (TREE_CODE (ary) == VECTOR_CST) | |
3995 | r = VECTOR_CST_ELT (ary, i); | |
fd2bfee5 | 3996 | else |
d6b46fca NS |
3997 | r = extract_string_elt (ary, elem_nchars, i); |
3998 | ||
fd2bfee5 JM |
3999 | if (r) |
4000 | /* Don't VERIFY_CONSTANT here. */ | |
4001 | return r; | |
2d76680f | 4002 | |
fd2bfee5 | 4003 | /* Otherwise the element doesn't have a value yet. */ |
2d76680f | 4004 | } |
3b9997bb | 4005 | |
fd2bfee5 JM |
4006 | /* Not found. */ |
4007 | ||
4008 | if (TREE_CODE (ary) == CONSTRUCTOR | |
e8c48716 | 4009 | && CONSTRUCTOR_NO_CLEARING (ary)) |
2d76680f | 4010 | { |
fd2bfee5 JM |
4011 | /* 'ary' is part of the aggregate initializer we're currently |
4012 | building; if there's no initializer for this element yet, | |
4013 | that's an error. */ | |
4014 | if (!ctx->quiet) | |
4015 | error ("accessing uninitialized array element"); | |
4016 | *non_constant_p = true; | |
4017 | return t; | |
2d76680f | 4018 | } |
fd2bfee5 JM |
4019 | |
4020 | /* If it's within the array bounds but doesn't have an explicit | |
0712ea63 JM |
4021 | initializer, it's initialized from {}. But use build_value_init |
4022 | directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */ | |
4023 | tree val; | |
0df73bee | 4024 | constexpr_ctx new_ctx; |
a688c284 PP |
4025 | if (is_really_empty_class (elem_type, /*ignore_vptr*/false)) |
4026 | return build_constructor (elem_type, NULL); | |
4027 | else if (CP_AGGREGATE_TYPE_P (elem_type)) | |
0712ea63 JM |
4028 | { |
4029 | tree empty_ctor = build_constructor (init_list_type_node, NULL); | |
4030 | val = digest_init (elem_type, empty_ctor, tf_warning_or_error); | |
a42f8120 MP |
4031 | } |
4032 | else | |
4033 | val = build_value_init (elem_type, tf_warning_or_error); | |
4034 | ||
4035 | if (!SCALAR_TYPE_P (elem_type)) | |
4036 | { | |
0df73bee | 4037 | new_ctx = *ctx; |
a42f8120 MP |
4038 | if (ctx->object) |
4039 | /* If there was no object, don't add one: it could confuse us | |
4040 | into thinking we're modifying a const object. */ | |
4041 | new_ctx.object = t; | |
0df73bee MP |
4042 | new_ctx.ctor = build_constructor (elem_type, NULL); |
4043 | ctx = &new_ctx; | |
0712ea63 | 4044 | } |
0df73bee MP |
4045 | t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p, |
4046 | overflow_p); | |
a42f8120 | 4047 | if (!SCALAR_TYPE_P (elem_type) && t != ctx->ctor) |
0df73bee MP |
4048 | free_constructor (ctx->ctor); |
4049 | return t; | |
2d76680f PC |
4050 | } |
4051 | ||
4052 | /* Subroutine of cxx_eval_constant_expression. | |
4053 | Attempt to reduce a field access of a value of class type. */ | |
4054 | ||
4055 | static tree | |
3e605b20 | 4056 | cxx_eval_component_reference (const constexpr_ctx *ctx, tree t, |
72f76540 | 4057 | value_cat lval, |
2d76680f PC |
4058 | bool *non_constant_p, bool *overflow_p) |
4059 | { | |
4060 | unsigned HOST_WIDE_INT i; | |
4061 | tree field; | |
4062 | tree value; | |
4063 | tree part = TREE_OPERAND (t, 1); | |
4064 | tree orig_whole = TREE_OPERAND (t, 0); | |
3e605b20 | 4065 | tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
92a596e8 | 4066 | lval, |
5a804683 | 4067 | non_constant_p, overflow_p); |
a7f8415c | 4068 | if (INDIRECT_REF_P (whole) |
bf533db8 JJ |
4069 | && integer_zerop (TREE_OPERAND (whole, 0))) |
4070 | { | |
4071 | if (!ctx->quiet) | |
4072 | error ("dereferencing a null pointer in %qE", orig_whole); | |
4073 | *non_constant_p = true; | |
4074 | return t; | |
4075 | } | |
8bada5cd | 4076 | |
dcdbc004 JM |
4077 | if (TREE_CODE (whole) == PTRMEM_CST) |
4078 | whole = cplus_expand_constant (whole); | |
2d76680f PC |
4079 | if (whole == orig_whole) |
4080 | return t; | |
72f76540 JM |
4081 | if (lval == vc_discard) |
4082 | return t; | |
92a596e8 | 4083 | if (lval) |
2d76680f PC |
4084 | return fold_build3 (COMPONENT_REF, TREE_TYPE (t), |
4085 | whole, part, NULL_TREE); | |
4086 | /* Don't VERIFY_CONSTANT here; we only want to check that we got a | |
4087 | CONSTRUCTOR. */ | |
4088 | if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR) | |
4089 | { | |
2b3ab879 | 4090 | if (!ctx->quiet) |
2d76680f PC |
4091 | error ("%qE is not a constant expression", orig_whole); |
4092 | *non_constant_p = true; | |
4093 | } | |
4094 | if (DECL_MUTABLE_P (part)) | |
4095 | { | |
2b3ab879 | 4096 | if (!ctx->quiet) |
2d76680f PC |
4097 | error ("mutable %qD is not usable in a constant expression", part); |
4098 | *non_constant_p = true; | |
4099 | } | |
4100 | if (*non_constant_p) | |
4101 | return t; | |
2db613e5 | 4102 | bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole)); |
2d76680f PC |
4103 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) |
4104 | { | |
2db613e5 JM |
4105 | /* Use name match for PMF fields, as a variant will have a |
4106 | different FIELD_DECL with a different type. */ | |
4107 | if (pmf ? DECL_NAME (field) == DECL_NAME (part) | |
4108 | : field == part) | |
60813a46 JM |
4109 | { |
4110 | if (value) | |
5b884e94 JJ |
4111 | { |
4112 | STRIP_ANY_LOCATION_WRAPPER (value); | |
4113 | return value; | |
4114 | } | |
60813a46 JM |
4115 | else |
4116 | /* We're in the middle of initializing it. */ | |
4117 | break; | |
4118 | } | |
2d76680f PC |
4119 | } |
4120 | if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE | |
4121 | && CONSTRUCTOR_NELTS (whole) > 0) | |
4122 | { | |
4123 | /* DR 1188 says we don't have to deal with this. */ | |
2b3ab879 | 4124 | if (!ctx->quiet) |
b599bf9d PP |
4125 | { |
4126 | constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0); | |
4127 | if (cep->value == NULL_TREE) | |
4128 | error ("accessing uninitialized member %qD", part); | |
4129 | else | |
4130 | error ("accessing %qD member instead of initialized %qD member in " | |
4131 | "constant expression", part, cep->index); | |
4132 | } | |
2d76680f PC |
4133 | *non_constant_p = true; |
4134 | return t; | |
4135 | } | |
4136 | ||
188e53bd JM |
4137 | /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty |
4138 | classes never get represented; throw together a value now. */ | |
dbcd32f8 | 4139 | if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
188e53bd JM |
4140 | return build_constructor (TREE_TYPE (t), NULL); |
4141 | ||
2db613e5 JM |
4142 | gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole))); |
4143 | ||
e8c48716 | 4144 | if (CONSTRUCTOR_NO_CLEARING (whole)) |
3e605b20 JM |
4145 | { |
4146 | /* 'whole' is part of the aggregate initializer we're currently | |
4147 | building; if there's no initializer for this member yet, that's an | |
188e53bd | 4148 | error. */ |
2b3ab879 | 4149 | if (!ctx->quiet) |
3e605b20 JM |
4150 | error ("accessing uninitialized member %qD", part); |
4151 | *non_constant_p = true; | |
4152 | return t; | |
4153 | } | |
4154 | ||
2d76680f PC |
4155 | /* If there's no explicit init for this field, it's value-initialized. */ |
4156 | value = build_value_init (TREE_TYPE (t), tf_warning_or_error); | |
3e605b20 | 4157 | return cxx_eval_constant_expression (ctx, value, |
92a596e8 | 4158 | lval, |
5a804683 | 4159 | non_constant_p, overflow_p); |
2d76680f PC |
4160 | } |
4161 | ||
4162 | /* Subroutine of cxx_eval_constant_expression. | |
4163 | Attempt to reduce a field access of a value of class type that is | |
4164 | expressed as a BIT_FIELD_REF. */ | |
4165 | ||
4166 | static tree | |
3e605b20 | 4167 | cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t, |
72f76540 | 4168 | value_cat lval, |
2d76680f PC |
4169 | bool *non_constant_p, bool *overflow_p) |
4170 | { | |
4171 | tree orig_whole = TREE_OPERAND (t, 0); | |
4172 | tree retval, fldval, utype, mask; | |
4173 | bool fld_seen = false; | |
4174 | HOST_WIDE_INT istart, isize; | |
3e605b20 | 4175 | tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
92a596e8 | 4176 | lval, |
5a804683 | 4177 | non_constant_p, overflow_p); |
2d76680f PC |
4178 | tree start, field, value; |
4179 | unsigned HOST_WIDE_INT i; | |
4180 | ||
4181 | if (whole == orig_whole) | |
4182 | return t; | |
4183 | /* Don't VERIFY_CONSTANT here; we only want to check that we got a | |
4184 | CONSTRUCTOR. */ | |
4185 | if (!*non_constant_p | |
4186 | && TREE_CODE (whole) != VECTOR_CST | |
4187 | && TREE_CODE (whole) != CONSTRUCTOR) | |
4188 | { | |
2b3ab879 | 4189 | if (!ctx->quiet) |
2d76680f PC |
4190 | error ("%qE is not a constant expression", orig_whole); |
4191 | *non_constant_p = true; | |
4192 | } | |
4193 | if (*non_constant_p) | |
4194 | return t; | |
4195 | ||
4196 | if (TREE_CODE (whole) == VECTOR_CST) | |
4197 | return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole, | |
4198 | TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)); | |
4199 | ||
4200 | start = TREE_OPERAND (t, 2); | |
4201 | istart = tree_to_shwi (start); | |
4202 | isize = tree_to_shwi (TREE_OPERAND (t, 1)); | |
4203 | utype = TREE_TYPE (t); | |
4204 | if (!TYPE_UNSIGNED (utype)) | |
4205 | utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1); | |
4206 | retval = build_int_cst (utype, 0); | |
4207 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) | |
4208 | { | |
4209 | tree bitpos = bit_position (field); | |
5b884e94 | 4210 | STRIP_ANY_LOCATION_WRAPPER (value); |
2d76680f PC |
4211 | if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1)) |
4212 | return value; | |
4213 | if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE | |
4214 | && TREE_CODE (value) == INTEGER_CST | |
4215 | && tree_fits_shwi_p (bitpos) | |
4216 | && tree_fits_shwi_p (DECL_SIZE (field))) | |
4217 | { | |
4218 | HOST_WIDE_INT bit = tree_to_shwi (bitpos); | |
4219 | HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field)); | |
4220 | HOST_WIDE_INT shift; | |
4221 | if (bit >= istart && bit + sz <= istart + isize) | |
4222 | { | |
4223 | fldval = fold_convert (utype, value); | |
4224 | mask = build_int_cst_type (utype, -1); | |
4225 | mask = fold_build2 (LSHIFT_EXPR, utype, mask, | |
4226 | size_int (TYPE_PRECISION (utype) - sz)); | |
4227 | mask = fold_build2 (RSHIFT_EXPR, utype, mask, | |
4228 | size_int (TYPE_PRECISION (utype) - sz)); | |
4229 | fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask); | |
4230 | shift = bit - istart; | |
4231 | if (BYTES_BIG_ENDIAN) | |
4232 | shift = TYPE_PRECISION (utype) - shift - sz; | |
4233 | fldval = fold_build2 (LSHIFT_EXPR, utype, fldval, | |
4234 | size_int (shift)); | |
4235 | retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval); | |
4236 | fld_seen = true; | |
4237 | } | |
4238 | } | |
4239 | } | |
4240 | if (fld_seen) | |
4241 | return fold_convert (TREE_TYPE (t), retval); | |
4242 | gcc_unreachable (); | |
4243 | return error_mark_node; | |
4244 | } | |
4245 | ||
896048cf JJ |
4246 | /* Helper for cxx_eval_bit_cast. |
4247 | Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From | |
4248 | types and types of all subobjects have is_union_v<T>, is_pointer_v<T>, | |
4249 | is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static | |
4250 | data members of reference type. */ | |
4251 | ||
4252 | static bool | |
4253 | check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type, | |
4254 | tree orig_type) | |
4255 | { | |
4256 | if (TREE_CODE (type) == UNION_TYPE) | |
4257 | { | |
4258 | if (!ctx->quiet) | |
4259 | { | |
4260 | if (type == orig_type) | |
4261 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4262 | "a union type", "__builtin_bit_cast", type); | |
4263 | else | |
4264 | error_at (loc, "%qs is not a constant expression because %qT " | |
4265 | "contains a union type", "__builtin_bit_cast", | |
4266 | orig_type); | |
4267 | } | |
4268 | return true; | |
4269 | } | |
4270 | if (TREE_CODE (type) == POINTER_TYPE) | |
4271 | { | |
4272 | if (!ctx->quiet) | |
4273 | { | |
4274 | if (type == orig_type) | |
4275 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4276 | "a pointer type", "__builtin_bit_cast", type); | |
4277 | else | |
4278 | error_at (loc, "%qs is not a constant expression because %qT " | |
4279 | "contains a pointer type", "__builtin_bit_cast", | |
4280 | orig_type); | |
4281 | } | |
4282 | return true; | |
4283 | } | |
4284 | if (TREE_CODE (type) == REFERENCE_TYPE) | |
4285 | { | |
4286 | if (!ctx->quiet) | |
4287 | { | |
4288 | if (type == orig_type) | |
4289 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4290 | "a reference type", "__builtin_bit_cast", type); | |
4291 | else | |
4292 | error_at (loc, "%qs is not a constant expression because %qT " | |
4293 | "contains a reference type", "__builtin_bit_cast", | |
4294 | orig_type); | |
4295 | } | |
4296 | return true; | |
4297 | } | |
4298 | if (TYPE_PTRMEM_P (type)) | |
4299 | { | |
4300 | if (!ctx->quiet) | |
4301 | { | |
4302 | if (type == orig_type) | |
4303 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4304 | "a pointer to member type", "__builtin_bit_cast", | |
4305 | type); | |
4306 | else | |
4307 | error_at (loc, "%qs is not a constant expression because %qT " | |
4308 | "contains a pointer to member type", | |
4309 | "__builtin_bit_cast", orig_type); | |
4310 | } | |
4311 | return true; | |
4312 | } | |
4313 | if (TYPE_VOLATILE (type)) | |
4314 | { | |
4315 | if (!ctx->quiet) | |
4316 | { | |
4317 | if (type == orig_type) | |
4318 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4319 | "volatile", "__builtin_bit_cast", type); | |
4320 | else | |
4321 | error_at (loc, "%qs is not a constant expression because %qT " | |
4322 | "contains a volatile subobject", | |
4323 | "__builtin_bit_cast", orig_type); | |
4324 | } | |
4325 | return true; | |
4326 | } | |
4327 | if (TREE_CODE (type) == RECORD_TYPE) | |
4328 | for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) | |
4329 | if (TREE_CODE (field) == FIELD_DECL | |
4330 | && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type)) | |
4331 | return true; | |
4332 | return false; | |
4333 | } | |
4334 | ||
c57c910c JJ |
4335 | /* Helper function for cxx_eval_bit_cast. For unsigned char or |
4336 | std::byte members of CONSTRUCTOR (recursively) if they contain | |
4337 | some indeterminate bits (as set in MASK), remove the ctor elts, | |
4338 | mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the | |
4339 | bits in MASK. */ | |
4340 | ||
4341 | static void | |
4342 | clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask) | |
4343 | { | |
4344 | if (TREE_CODE (t) != CONSTRUCTOR) | |
4345 | return; | |
4346 | ||
4347 | unsigned i, j = 0; | |
4348 | tree index, value; | |
4349 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value) | |
4350 | { | |
4351 | tree type = TREE_TYPE (value); | |
4352 | if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE | |
4353 | && DECL_BIT_FIELD_TYPE (index) != NULL_TREE) | |
4354 | { | |
4355 | if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index))) | |
4356 | { | |
4357 | HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index)); | |
4358 | gcc_assert (fldsz != 0); | |
4359 | HOST_WIDE_INT pos = int_byte_position (index); | |
4360 | HOST_WIDE_INT bpos | |
4361 | = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index)); | |
4362 | bpos %= BITS_PER_UNIT; | |
4363 | HOST_WIDE_INT end | |
4364 | = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT; | |
4365 | gcc_assert (end == 1 || end == 2); | |
4366 | unsigned char *p = mask + pos; | |
4367 | unsigned char mask_save[2]; | |
4368 | mask_save[0] = mask[pos]; | |
4369 | mask_save[1] = end == 2 ? mask[pos + 1] : 0; | |
4370 | if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) | |
4371 | sorry_at (loc, "PDP11 bit-field handling unsupported" | |
4372 | " in %qs", "__builtin_bit_cast"); | |
4373 | else if (BYTES_BIG_ENDIAN) | |
4374 | { | |
4375 | /* Big endian. */ | |
4376 | if (bpos + fldsz <= BITS_PER_UNIT) | |
4377 | *p &= ~(((1 << fldsz) - 1) | |
4378 | << (BITS_PER_UNIT - bpos - fldsz)); | |
4379 | else | |
4380 | { | |
4381 | gcc_assert (bpos); | |
4382 | *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos); | |
4383 | p++; | |
4384 | fldsz -= BITS_PER_UNIT - bpos; | |
4385 | gcc_assert (fldsz && fldsz < BITS_PER_UNIT); | |
4386 | *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz; | |
4387 | } | |
4388 | } | |
4389 | else | |
4390 | { | |
4391 | /* Little endian. */ | |
4392 | if (bpos + fldsz <= BITS_PER_UNIT) | |
4393 | *p &= ~(((1 << fldsz) - 1) << bpos); | |
4394 | else | |
4395 | { | |
4396 | gcc_assert (bpos); | |
4397 | *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos); | |
4398 | p++; | |
4399 | fldsz -= BITS_PER_UNIT - bpos; | |
4400 | gcc_assert (fldsz && fldsz < BITS_PER_UNIT); | |
4401 | *p &= ~((1 << fldsz) - 1); | |
4402 | } | |
4403 | } | |
4404 | if (mask_save[0] != mask[pos] | |
4405 | || (end == 2 && mask_save[1] != mask[pos + 1])) | |
4406 | { | |
4407 | CONSTRUCTOR_NO_CLEARING (t) = 1; | |
4408 | continue; | |
4409 | } | |
4410 | } | |
4411 | } | |
4412 | else if (is_byte_access_type_not_plain_char (type)) | |
4413 | { | |
4414 | HOST_WIDE_INT pos; | |
4415 | if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) | |
4416 | pos = tree_to_shwi (index); | |
4417 | else | |
4418 | pos = int_byte_position (index); | |
4419 | if (mask[pos]) | |
4420 | { | |
4421 | CONSTRUCTOR_NO_CLEARING (t) = 1; | |
4422 | mask[pos] = 0; | |
4423 | continue; | |
4424 | } | |
4425 | } | |
4426 | if (TREE_CODE (value) == CONSTRUCTOR) | |
4427 | { | |
4428 | HOST_WIDE_INT pos; | |
4429 | if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) | |
4430 | pos = tree_to_shwi (index) | |
4431 | * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)))); | |
4432 | else | |
4433 | pos = int_byte_position (index); | |
4434 | clear_uchar_or_std_byte_in_mask (loc, value, mask + pos); | |
4435 | } | |
4436 | if (i != j) | |
4437 | { | |
4438 | CONSTRUCTOR_ELT (t, j)->index = index; | |
4439 | CONSTRUCTOR_ELT (t, j)->value = value; | |
4440 | } | |
4441 | ++j; | |
4442 | } | |
4443 | if (CONSTRUCTOR_NELTS (t) != j) | |
4444 | vec_safe_truncate (CONSTRUCTOR_ELTS (t), j); | |
4445 | } | |
4446 | ||
896048cf JJ |
4447 | /* Subroutine of cxx_eval_constant_expression. |
4448 | Attempt to evaluate a BIT_CAST_EXPR. */ | |
4449 | ||
4450 | static tree | |
4451 | cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p, | |
4452 | bool *overflow_p) | |
4453 | { | |
4454 | if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t), | |
4455 | TREE_TYPE (t)) | |
4456 | || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0), | |
4457 | EXPR_LOCATION (t)), | |
4458 | TREE_TYPE (TREE_OPERAND (t, 0)), | |
4459 | TREE_TYPE (TREE_OPERAND (t, 0)))) | |
4460 | { | |
4461 | *non_constant_p = true; | |
4462 | return t; | |
4463 | } | |
4464 | ||
72f76540 | 4465 | tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue, |
896048cf JJ |
4466 | non_constant_p, overflow_p); |
4467 | if (*non_constant_p) | |
4468 | return t; | |
4469 | ||
4470 | location_t loc = EXPR_LOCATION (t); | |
4471 | if (BITS_PER_UNIT != 8 || CHAR_BIT != 8) | |
4472 | { | |
4473 | if (!ctx->quiet) | |
4474 | sorry_at (loc, "%qs cannot be constant evaluated on the target", | |
4475 | "__builtin_bit_cast"); | |
4476 | *non_constant_p = true; | |
4477 | return t; | |
4478 | } | |
4479 | ||
4480 | if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t)))) | |
4481 | { | |
4482 | if (!ctx->quiet) | |
4483 | sorry_at (loc, "%qs cannot be constant evaluated because the " | |
4484 | "type is too large", "__builtin_bit_cast"); | |
4485 | *non_constant_p = true; | |
4486 | return t; | |
4487 | } | |
4488 | ||
4489 | HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t))); | |
4490 | if (len < 0 || (int) len != len) | |
4491 | { | |
4492 | if (!ctx->quiet) | |
4493 | sorry_at (loc, "%qs cannot be constant evaluated because the " | |
4494 | "type is too large", "__builtin_bit_cast"); | |
4495 | *non_constant_p = true; | |
4496 | return t; | |
4497 | } | |
4498 | ||
4499 | unsigned char buf[64]; | |
4500 | unsigned char *ptr, *mask; | |
4501 | size_t alen = (size_t) len * 2; | |
4502 | if (alen <= sizeof (buf)) | |
4503 | ptr = buf; | |
4504 | else | |
4505 | ptr = XNEWVEC (unsigned char, alen); | |
4506 | mask = ptr + (size_t) len; | |
4507 | /* At the beginning consider everything indeterminate. */ | |
4508 | memset (mask, ~0, (size_t) len); | |
4509 | ||
4510 | if (native_encode_initializer (op, ptr, len, 0, mask) != len) | |
4511 | { | |
4512 | if (!ctx->quiet) | |
4513 | sorry_at (loc, "%qs cannot be constant evaluated because the " | |
4514 | "argument cannot be encoded", "__builtin_bit_cast"); | |
4515 | *non_constant_p = true; | |
4516 | if (ptr != buf) | |
4517 | XDELETE (ptr); | |
4518 | return t; | |
4519 | } | |
4520 | ||
4521 | tree r = NULL_TREE; | |
4522 | if (can_native_interpret_type_p (TREE_TYPE (t))) | |
c57c910c JJ |
4523 | { |
4524 | r = native_interpret_expr (TREE_TYPE (t), ptr, len); | |
4525 | if (is_byte_access_type_not_plain_char (TREE_TYPE (t))) | |
4526 | { | |
4527 | gcc_assert (len == 1); | |
4528 | if (mask[0]) | |
4529 | { | |
4530 | memset (mask, 0, len); | |
4531 | r = build_constructor (TREE_TYPE (r), NULL); | |
4532 | CONSTRUCTOR_NO_CLEARING (r) = 1; | |
4533 | } | |
4534 | } | |
4535 | } | |
896048cf JJ |
4536 | else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE) |
4537 | { | |
4538 | r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len); | |
4539 | if (r != NULL_TREE) | |
c57c910c JJ |
4540 | { |
4541 | clear_type_padding_in_mask (TREE_TYPE (t), mask); | |
4542 | clear_uchar_or_std_byte_in_mask (loc, r, mask); | |
4543 | } | |
896048cf JJ |
4544 | } |
4545 | ||
4546 | if (r != NULL_TREE) | |
4547 | { | |
4548 | for (int i = 0; i < len; i++) | |
4549 | if (mask[i]) | |
4550 | { | |
4551 | if (!ctx->quiet) | |
4552 | error_at (loc, "%qs accessing uninitialized byte at offset %d", | |
4553 | "__builtin_bit_cast", i); | |
4554 | *non_constant_p = true; | |
4555 | r = t; | |
4556 | break; | |
4557 | } | |
4558 | if (ptr != buf) | |
4559 | XDELETE (ptr); | |
4560 | return r; | |
4561 | } | |
4562 | ||
4563 | if (!ctx->quiet) | |
4564 | sorry_at (loc, "%qs cannot be constant evaluated because the " | |
4565 | "argument cannot be interpreted", "__builtin_bit_cast"); | |
4566 | *non_constant_p = true; | |
4567 | if (ptr != buf) | |
4568 | XDELETE (ptr); | |
4569 | return t; | |
4570 | } | |
4571 | ||
2d76680f PC |
4572 | /* Subroutine of cxx_eval_constant_expression. |
4573 | Evaluate a short-circuited logical expression T in the context | |
4574 | of a given constexpr CALL. BAILOUT_VALUE is the value for | |
4575 | early return. CONTINUE_VALUE is used here purely for | |
4576 | sanity check purposes. */ | |
4577 | ||
4578 | static tree | |
3e605b20 | 4579 | cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t, |
2d76680f | 4580 | tree bailout_value, tree continue_value, |
605a80bb | 4581 | bool *non_constant_p, bool *overflow_p) |
2d76680f PC |
4582 | { |
4583 | tree r; | |
3e605b20 | 4584 | tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
72f76540 | 4585 | vc_prvalue, non_constant_p, |
93b65ed9 | 4586 | overflow_p); |
2d76680f PC |
4587 | VERIFY_CONSTANT (lhs); |
4588 | if (tree_int_cst_equal (lhs, bailout_value)) | |
4589 | return lhs; | |
4590 | gcc_assert (tree_int_cst_equal (lhs, continue_value)); | |
3e605b20 | 4591 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
72f76540 | 4592 | vc_prvalue, non_constant_p, |
5a804683 | 4593 | overflow_p); |
2d76680f PC |
4594 | VERIFY_CONSTANT (r); |
4595 | return r; | |
4596 | } | |
4597 | ||
4598 | /* REF is a COMPONENT_REF designating a particular field. V is a vector of | |
4599 | CONSTRUCTOR elements to initialize (part of) an object containing that | |
4600 | field. Return a pointer to the constructor_elt corresponding to the | |
4601 | initialization of the field. */ | |
4602 | ||
4603 | static constructor_elt * | |
4604 | base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref) | |
4605 | { | |
4606 | tree aggr = TREE_OPERAND (ref, 0); | |
4607 | tree field = TREE_OPERAND (ref, 1); | |
4608 | HOST_WIDE_INT i; | |
4609 | constructor_elt *ce; | |
4610 | ||
4611 | gcc_assert (TREE_CODE (ref) == COMPONENT_REF); | |
4612 | ||
4613 | if (TREE_CODE (aggr) == COMPONENT_REF) | |
4614 | { | |
4615 | constructor_elt *base_ce | |
4616 | = base_field_constructor_elt (v, aggr); | |
4617 | v = CONSTRUCTOR_ELTS (base_ce->value); | |
4618 | } | |
4619 | ||
4620 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
4621 | if (ce->index == field) | |
4622 | return ce; | |
4623 | ||
4624 | gcc_unreachable (); | |
4625 | return NULL; | |
4626 | } | |
4627 | ||
3e605b20 JM |
4628 | /* Some of the expressions fed to the constexpr mechanism are calls to |
4629 | constructors, which have type void. In that case, return the type being | |
4630 | initialized by the constructor. */ | |
4631 | ||
4632 | static tree | |
4633 | initialized_type (tree t) | |
4634 | { | |
4635 | if (TYPE_P (t)) | |
4636 | return t; | |
5dab8b11 | 4637 | tree type = TREE_TYPE (t); |
4d0c18c6 | 4638 | if (TREE_CODE (t) == CALL_EXPR) |
3e605b20 JM |
4639 | { |
4640 | /* A constructor call has void type, so we need to look deeper. */ | |
4641 | tree fn = get_function_named_in_call (t); | |
4642 | if (fn && TREE_CODE (fn) == FUNCTION_DECL | |
4643 | && DECL_CXX_CONSTRUCTOR_P (fn)) | |
4644 | type = DECL_CONTEXT (fn); | |
4645 | } | |
4d0c18c6 JM |
4646 | else if (TREE_CODE (t) == COMPOUND_EXPR) |
4647 | return initialized_type (TREE_OPERAND (t, 1)); | |
5dab8b11 JM |
4648 | else if (TREE_CODE (t) == AGGR_INIT_EXPR) |
4649 | type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t)); | |
4650 | return cv_unqualified (type); | |
3e605b20 JM |
4651 | } |
4652 | ||
4653 | /* We're about to initialize element INDEX of an array or class from VALUE. | |
4654 | Set up NEW_CTX appropriately by adjusting .object to refer to the | |
4655 | subobject and creating a new CONSTRUCTOR if the element is itself | |
4656 | a class or array. */ | |
4657 | ||
4658 | static void | |
4659 | init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx, | |
4660 | tree index, tree &value) | |
4661 | { | |
4662 | new_ctx = *ctx; | |
4663 | ||
4664 | if (index && TREE_CODE (index) != INTEGER_CST | |
3d423c6f PP |
4665 | && TREE_CODE (index) != FIELD_DECL |
4666 | && TREE_CODE (index) != RANGE_EXPR) | |
3e605b20 JM |
4667 | /* This won't have an element in the new CONSTRUCTOR. */ |
4668 | return; | |
4669 | ||
4670 | tree type = initialized_type (value); | |
4671 | if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type)) | |
4672 | /* A non-aggregate member doesn't get its own CONSTRUCTOR. */ | |
4673 | return; | |
abea1c9a JJ |
4674 | if (VECTOR_TYPE_P (type) |
4675 | && VECTOR_TYPE_P (TREE_TYPE (ctx->ctor)) | |
4676 | && index == NULL_TREE) | |
4677 | /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger | |
4678 | vector is constructed from smaller vectors, doesn't get its own | |
4679 | CONSTRUCTOR either. */ | |
4680 | return; | |
3e605b20 JM |
4681 | |
4682 | /* The sub-aggregate initializer might contain a placeholder; | |
4683 | update object to refer to the subobject and ctor to refer to | |
4684 | the (newly created) sub-initializer. */ | |
4685 | if (ctx->object) | |
3d423c6f PP |
4686 | { |
4687 | if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR) | |
4688 | /* There's no well-defined subobject for this index. */ | |
4689 | new_ctx.object = NULL_TREE; | |
4690 | else | |
4691 | new_ctx.object = build_ctor_subob_ref (index, type, ctx->object); | |
4692 | } | |
3e605b20 | 4693 | tree elt = build_constructor (type, NULL); |
e8c48716 | 4694 | CONSTRUCTOR_NO_CLEARING (elt) = true; |
3e605b20 JM |
4695 | new_ctx.ctor = elt; |
4696 | ||
4697 | if (TREE_CODE (value) == TARGET_EXPR) | |
4698 | /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */ | |
4699 | value = TARGET_EXPR_INITIAL (value); | |
4700 | } | |
4701 | ||
4702 | /* We're about to process an initializer for a class or array TYPE. Make | |
4703 | sure that CTX is set up appropriately. */ | |
4704 | ||
4705 | static void | |
4706 | verify_ctor_sanity (const constexpr_ctx *ctx, tree type) | |
4707 | { | |
4708 | /* We don't bother building a ctor for an empty base subobject. */ | |
4709 | if (is_empty_class (type)) | |
4710 | return; | |
4711 | ||
4712 | /* We're in the middle of an initializer that might involve placeholders; | |
4713 | our caller should have created a CONSTRUCTOR for us to put the | |
4714 | initializer into. We will either return that constructor or T. */ | |
4715 | gcc_assert (ctx->ctor); | |
4716 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
4717 | (type, TREE_TYPE (ctx->ctor))); | |
fe69277d JM |
4718 | /* We used to check that ctx->ctor was empty, but that isn't the case when |
4719 | the object is zero-initialized before calling the constructor. */ | |
3e605b20 | 4720 | if (ctx->object) |
176e79b5 JM |
4721 | { |
4722 | tree otype = TREE_TYPE (ctx->object); | |
4723 | gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype) | |
4724 | /* Handle flexible array members. */ | |
4725 | || (TREE_CODE (otype) == ARRAY_TYPE | |
4726 | && TYPE_DOMAIN (otype) == NULL_TREE | |
4727 | && TREE_CODE (type) == ARRAY_TYPE | |
4728 | && (same_type_ignoring_top_level_qualifiers_p | |
4729 | (TREE_TYPE (type), TREE_TYPE (otype))))); | |
4730 | } | |
3e605b20 | 4731 | gcc_assert (!ctx->object || !DECL_P (ctx->object) |
8e007055 | 4732 | || *(ctx->global->values.get (ctx->object)) == ctx->ctor); |
3e605b20 JM |
4733 | } |
4734 | ||
2d76680f PC |
4735 | /* Subroutine of cxx_eval_constant_expression. |
4736 | The expression tree T denotes a C-style array or a C-style | |
4737 | aggregate. Reduce it to a constant expression. */ | |
4738 | ||
4739 | static tree | |
3e605b20 | 4740 | cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t, |
72f76540 | 4741 | value_cat lval, |
2d76680f PC |
4742 | bool *non_constant_p, bool *overflow_p) |
4743 | { | |
4744 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); | |
2d76680f PC |
4745 | bool changed = false; |
4746 | gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t)); | |
8a29084d | 4747 | tree type = TREE_TYPE (t); |
3e605b20 | 4748 | |
8a29084d | 4749 | constexpr_ctx new_ctx; |
d4619dc1 | 4750 | if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type)) |
8a29084d | 4751 | { |
d4619dc1 NS |
4752 | /* We don't really need the ctx->ctor business for a PMF or |
4753 | vector, but it's simpler to use the same code. */ | |
8a29084d JM |
4754 | new_ctx = *ctx; |
4755 | new_ctx.ctor = build_constructor (type, NULL); | |
4756 | new_ctx.object = NULL_TREE; | |
4757 | ctx = &new_ctx; | |
4758 | }; | |
4759 | verify_ctor_sanity (ctx, type); | |
3e605b20 JM |
4760 | vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); |
4761 | vec_alloc (*p, vec_safe_length (v)); | |
4762 | ||
49a86fce PP |
4763 | if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t)) |
4764 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1; | |
4765 | ||
2d63bc39 JM |
4766 | unsigned i; |
4767 | tree index, value; | |
4768 | bool constant_p = true; | |
4769 | bool side_effects_p = false; | |
3e605b20 | 4770 | FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value) |
2d76680f | 4771 | { |
bcb5f3c9 | 4772 | tree orig_value = value; |
2168bfb8 JM |
4773 | /* Like in cxx_eval_store_expression, omit entries for empty fields. */ |
4774 | bool no_slot = TREE_CODE (type) == RECORD_TYPE && is_empty_field (index); | |
4775 | if (no_slot) | |
4776 | new_ctx = *ctx; | |
4777 | else | |
4778 | init_subob_ctx (ctx, new_ctx, index, value); | |
37244b21 | 4779 | int pos_hint = -1; |
3e605b20 | 4780 | if (new_ctx.ctor != ctx->ctor) |
37244b21 PP |
4781 | { |
4782 | /* If we built a new CONSTRUCTOR, attach it now so that other | |
4783 | initializers can refer to it. */ | |
077dd9b3 PP |
4784 | constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index); |
4785 | cep->value = new_ctx.ctor; | |
4786 | pos_hint = cep - (*p)->begin(); | |
37244b21 | 4787 | } |
b599bf9d | 4788 | else if (TREE_CODE (type) == UNION_TYPE) |
37244b21 PP |
4789 | /* Otherwise if we're constructing a non-aggregate union member, set |
4790 | the active union member now so that we can later detect and diagnose | |
4791 | if its initializer attempts to activate another member. */ | |
077dd9b3 | 4792 | get_or_insert_ctor_field (ctx->ctor, index); |
3e605b20 | 4793 | tree elt = cxx_eval_constant_expression (&new_ctx, value, |
92a596e8 | 4794 | lval, |
5a804683 | 4795 | non_constant_p, overflow_p); |
2d76680f | 4796 | /* Don't VERIFY_CONSTANT here. */ |
2b3ab879 | 4797 | if (ctx->quiet && *non_constant_p) |
3e605b20 | 4798 | break; |
bcb5f3c9 | 4799 | if (elt != orig_value) |
2d76680f | 4800 | changed = true; |
2d63bc39 JM |
4801 | |
4802 | if (!TREE_CONSTANT (elt)) | |
4803 | constant_p = false; | |
4804 | if (TREE_SIDE_EFFECTS (elt)) | |
4805 | side_effects_p = true; | |
3e605b20 | 4806 | if (index && TREE_CODE (index) == COMPONENT_REF) |
2d76680f PC |
4807 | { |
4808 | /* This is an initialization of a vfield inside a base | |
4809 | subaggregate that we already initialized; push this | |
4810 | initialization into the previous initialization. */ | |
3e605b20 | 4811 | constructor_elt *inner = base_field_constructor_elt (*p, index); |
2d76680f | 4812 | inner->value = elt; |
3e605b20 | 4813 | changed = true; |
2d76680f | 4814 | } |
3e605b20 JM |
4815 | else if (index |
4816 | && (TREE_CODE (index) == NOP_EXPR | |
4817 | || TREE_CODE (index) == POINTER_PLUS_EXPR)) | |
2d76680f PC |
4818 | { |
4819 | /* This is an initializer for an empty base; now that we've | |
4820 | checked that it's constant, we can ignore it. */ | |
3e605b20 JM |
4821 | gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index)))); |
4822 | changed = true; | |
4823 | } | |
2168bfb8 JM |
4824 | else if (no_slot) |
4825 | changed = true; | |
2d76680f | 4826 | else |
ac9ec198 | 4827 | { |
b599bf9d PP |
4828 | if (TREE_CODE (type) == UNION_TYPE |
4829 | && (*p)->last().index != index) | |
37244b21 PP |
4830 | /* The initializer erroneously changed the active union member that |
4831 | we're initializing. */ | |
b599bf9d | 4832 | gcc_assert (*non_constant_p); |
37244b21 | 4833 | else |
1bdbef09 | 4834 | { |
37244b21 PP |
4835 | /* The initializer might have mutated the underlying CONSTRUCTOR, |
4836 | so recompute the location of the target constructer_elt. */ | |
4837 | constructor_elt *cep | |
4838 | = get_or_insert_ctor_field (ctx->ctor, index, pos_hint); | |
4839 | cep->value = elt; | |
1bdbef09 | 4840 | } |
37244b21 | 4841 | |
1bdbef09 | 4842 | /* Adding or replacing an element might change the ctor's flags. */ |
ac9ec198 MP |
4843 | TREE_CONSTANT (ctx->ctor) = constant_p; |
4844 | TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p; | |
4845 | } | |
2d76680f PC |
4846 | } |
4847 | if (*non_constant_p || !changed) | |
3e605b20 JM |
4848 | return t; |
4849 | t = ctx->ctor; | |
4850 | /* We're done building this CONSTRUCTOR, so now we can interpret an | |
4851 | element without an explicit initializer as value-initialized. */ | |
e8c48716 | 4852 | CONSTRUCTOR_NO_CLEARING (t) = false; |
2d63bc39 JM |
4853 | TREE_CONSTANT (t) = constant_p; |
4854 | TREE_SIDE_EFFECTS (t) = side_effects_p; | |
8a29084d | 4855 | if (VECTOR_TYPE_P (type)) |
2d76680f PC |
4856 | t = fold (t); |
4857 | return t; | |
4858 | } | |
4859 | ||
4860 | /* Subroutine of cxx_eval_constant_expression. | |
4861 | The expression tree T is a VEC_INIT_EXPR which denotes the desired | |
4862 | initialization of a non-static data member of array type. Reduce it to a | |
4863 | CONSTRUCTOR. | |
4864 | ||
4865 | Note that apart from value-initialization (when VALUE_INIT is true), | |
4866 | this is only intended to support value-initialization and the | |
4867 | initializations done by defaulted constructors for classes with | |
4868 | non-static data members of array type. In this case, VEC_INIT_EXPR_INIT | |
4869 | will either be NULL_TREE for the default constructor, or a COMPONENT_REF | |
4870 | for the copy/move constructor. */ | |
4871 | ||
4872 | static tree | |
3e605b20 | 4873 | cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init, |
72f76540 | 4874 | bool value_init, value_cat lval, |
2d76680f PC |
4875 | bool *non_constant_p, bool *overflow_p) |
4876 | { | |
4877 | tree elttype = TREE_TYPE (atype); | |
3e605b20 JM |
4878 | verify_ctor_sanity (ctx, atype); |
4879 | vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); | |
2d76680f | 4880 | bool pre_init = false; |
4784470a | 4881 | unsigned HOST_WIDE_INT i; |
dc5ca6c8 | 4882 | tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; |
2d76680f | 4883 | |
a1c9c9ff JM |
4884 | if (init && TREE_CODE (init) == CONSTRUCTOR) |
4885 | return cxx_eval_bare_aggregate (ctx, init, lval, | |
4886 | non_constant_p, overflow_p); | |
4887 | ||
2d76680f PC |
4888 | /* For the default constructor, build up a call to the default |
4889 | constructor of the element type. We only need to handle class types | |
4890 | here, as for a constructor to be constexpr, all members must be | |
4891 | initialized, which for a defaulted default constructor means they must | |
4892 | be of a class type with a constexpr default constructor. */ | |
4893 | if (TREE_CODE (elttype) == ARRAY_TYPE) | |
4894 | /* We only do this at the lowest level. */; | |
4895 | else if (value_init) | |
4896 | { | |
dc5ca6c8 | 4897 | init = build_value_init (elttype, complain); |
2d76680f PC |
4898 | pre_init = true; |
4899 | } | |
4900 | else if (!init) | |
4901 | { | |
cd9cf97b | 4902 | releasing_vec argvec; |
2d76680f PC |
4903 | init = build_special_member_call (NULL_TREE, complete_ctor_identifier, |
4904 | &argvec, elttype, LOOKUP_NORMAL, | |
dc5ca6c8 | 4905 | complain); |
5dab8b11 | 4906 | init = build_aggr_init_expr (elttype, init); |
2d76680f PC |
4907 | pre_init = true; |
4908 | } | |
4909 | ||
d21252de PP |
4910 | bool zeroed_out = false; |
4911 | if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor)) | |
4912 | { | |
4913 | /* We're initializing an array object that had been zero-initialized | |
4914 | earlier. Truncate ctx->ctor, and propagate its zeroed state by | |
4915 | clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element | |
4916 | initializers we append to it. */ | |
4917 | gcc_checking_assert (initializer_zerop (ctx->ctor)); | |
4918 | zeroed_out = true; | |
4919 | vec_safe_truncate (*p, 0); | |
4920 | } | |
4921 | ||
74f8705e MP |
4922 | tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p, |
4923 | overflow_p); | |
4924 | unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts); | |
4784470a | 4925 | for (i = 0; i < max; ++i) |
2d76680f PC |
4926 | { |
4927 | tree idx = build_int_cst (size_type_node, i); | |
4928 | tree eltinit; | |
928af3bf | 4929 | bool reuse = false; |
3e605b20 JM |
4930 | constexpr_ctx new_ctx; |
4931 | init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype); | |
4932 | if (new_ctx.ctor != ctx->ctor) | |
d21252de PP |
4933 | { |
4934 | if (zeroed_out) | |
4935 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false; | |
4936 | CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor); | |
4937 | } | |
2d76680f PC |
4938 | if (TREE_CODE (elttype) == ARRAY_TYPE) |
4939 | { | |
4940 | /* A multidimensional array; recurse. */ | |
4941 | if (value_init || init == NULL_TREE) | |
928af3bf JJ |
4942 | { |
4943 | eltinit = NULL_TREE; | |
4944 | reuse = i == 0; | |
4945 | } | |
2d76680f | 4946 | else |
dc5ca6c8 | 4947 | eltinit = cp_build_array_ref (input_location, init, idx, complain); |
3e605b20 | 4948 | eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init, |
92a596e8 | 4949 | lval, |
2d76680f PC |
4950 | non_constant_p, overflow_p); |
4951 | } | |
4952 | else if (pre_init) | |
4953 | { | |
4954 | /* Initializing an element using value or default initialization | |
4955 | we just pre-built above. */ | |
3ee378fb JM |
4956 | if (init == void_node) |
4957 | /* Trivial default-init, don't do anything to the CONSTRUCTOR. */ | |
4958 | return ctx->ctor; | |
928af3bf JJ |
4959 | eltinit = cxx_eval_constant_expression (&new_ctx, init, lval, |
4960 | non_constant_p, overflow_p); | |
4961 | reuse = i == 0; | |
2d76680f PC |
4962 | } |
4963 | else | |
4964 | { | |
4965 | /* Copying an element. */ | |
4966 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
4967 | (atype, TREE_TYPE (init))); | |
dc5ca6c8 | 4968 | eltinit = cp_build_array_ref (input_location, init, idx, complain); |
72b3e203 | 4969 | if (!lvalue_p (init)) |
2d76680f | 4970 | eltinit = move (eltinit); |
dc5ca6c8 | 4971 | eltinit = force_rvalue (eltinit, complain); |
c2236b9b JJ |
4972 | eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval, |
4973 | non_constant_p, overflow_p); | |
2d76680f | 4974 | } |
6b7d53a2 | 4975 | if (*non_constant_p) |
3e605b20 JM |
4976 | break; |
4977 | if (new_ctx.ctor != ctx->ctor) | |
4978 | { | |
4979 | /* We appended this element above; update the value. */ | |
4980 | gcc_assert ((*p)->last().index == idx); | |
4981 | (*p)->last().value = eltinit; | |
4982 | } | |
4983 | else | |
4984 | CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit); | |
928af3bf | 4985 | /* Reuse the result of cxx_eval_constant_expression call |
c2236b9b JJ |
4986 | from the first iteration to all others if it is a constant |
4987 | initializer that doesn't require relocations. */ | |
928af3bf JJ |
4988 | if (reuse |
4989 | && max > 1 | |
c2236b9b JJ |
4990 | && (eltinit == NULL_TREE |
4991 | || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit)) | |
4992 | == null_pointer_node))) | |
928af3bf JJ |
4993 | { |
4994 | if (new_ctx.ctor != ctx->ctor) | |
4995 | eltinit = new_ctx.ctor; | |
1e027956 RB |
4996 | tree range = build2 (RANGE_EXPR, size_type_node, |
4997 | build_int_cst (size_type_node, 1), | |
4998 | build_int_cst (size_type_node, max - 1)); | |
4999 | CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit)); | |
928af3bf JJ |
5000 | break; |
5001 | } | |
1e027956 RB |
5002 | else if (i == 0) |
5003 | vec_safe_reserve (*p, max); | |
2d76680f PC |
5004 | } |
5005 | ||
5006 | if (!*non_constant_p) | |
5007 | { | |
3e605b20 | 5008 | init = ctx->ctor; |
e8c48716 | 5009 | CONSTRUCTOR_NO_CLEARING (init) = false; |
2d76680f | 5010 | } |
2d76680f PC |
5011 | return init; |
5012 | } | |
5013 | ||
5014 | static tree | |
3e605b20 | 5015 | cxx_eval_vec_init (const constexpr_ctx *ctx, tree t, |
72f76540 | 5016 | value_cat lval, |
2d76680f PC |
5017 | bool *non_constant_p, bool *overflow_p) |
5018 | { | |
5019 | tree atype = TREE_TYPE (t); | |
5020 | tree init = VEC_INIT_EXPR_INIT (t); | |
e948436e JM |
5021 | bool value_init = VEC_INIT_EXPR_VALUE_INIT (t); |
5022 | if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init)) | |
5023 | ; | |
4822108e JM |
5024 | else if (CONSTRUCTOR_NELTS (init) == 0 |
5025 | && !CP_AGGREGATE_TYPE_P (strip_array_types (atype))) | |
e948436e JM |
5026 | { |
5027 | /* Handle {} as value-init. */ | |
5028 | init = NULL_TREE; | |
5029 | value_init = true; | |
5030 | } | |
5031 | else | |
5032 | { | |
5033 | /* This is a more complicated case, like needing to loop over trailing | |
5034 | elements; call build_vec_init and evaluate the result. */ | |
5035 | tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; | |
5036 | constexpr_ctx new_ctx = *ctx; | |
5037 | if (!ctx->object) | |
5038 | { | |
5039 | /* We want to have an initialization target for an VEC_INIT_EXPR. | |
5040 | If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */ | |
5041 | new_ctx.object = VEC_INIT_EXPR_SLOT (t); | |
5042 | tree ctor = new_ctx.ctor = build_constructor (atype, NULL); | |
5043 | CONSTRUCTOR_NO_CLEARING (ctor) = true; | |
5044 | ctx->global->values.put (new_ctx.object, ctor); | |
5045 | ctx = &new_ctx; | |
5046 | } | |
5047 | init = expand_vec_init_expr (ctx->object, t, complain); | |
5048 | return cxx_eval_constant_expression (ctx, init, lval, non_constant_p, | |
5049 | overflow_p); | |
5050 | } | |
5051 | tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init, | |
92a596e8 | 5052 | lval, non_constant_p, overflow_p); |
2d76680f PC |
5053 | if (*non_constant_p) |
5054 | return t; | |
5055 | else | |
5056 | return r; | |
5057 | } | |
5058 | ||
e4511ca2 JM |
5059 | /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case |
5060 | where the desired type is an array of unknown bounds because the variable | |
5061 | has had its bounds deduced since the wrapping expression was created. */ | |
5062 | ||
5063 | static bool | |
5064 | same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2) | |
5065 | { | |
5066 | while (TREE_CODE (type1) == ARRAY_TYPE | |
5067 | && TREE_CODE (type2) == ARRAY_TYPE | |
5068 | && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2))) | |
5069 | { | |
5070 | type1 = TREE_TYPE (type1); | |
5071 | type2 = TREE_TYPE (type2); | |
5072 | } | |
5073 | return same_type_ignoring_top_level_qualifiers_p (type1, type2); | |
5074 | } | |
5075 | ||
43e84ce7 JJ |
5076 | /* Try to determine the currently active union member for an expression |
5077 | with UNION_TYPE. If it can be determined, return the FIELD_DECL, | |
5078 | otherwise return NULL_TREE. */ | |
5079 | ||
5080 | static tree | |
5081 | cxx_union_active_member (const constexpr_ctx *ctx, tree t) | |
5082 | { | |
5083 | constexpr_ctx new_ctx = *ctx; | |
5084 | new_ctx.quiet = true; | |
5085 | bool non_constant_p = false, overflow_p = false; | |
72f76540 | 5086 | tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue, |
43e84ce7 JJ |
5087 | &non_constant_p, |
5088 | &overflow_p); | |
5089 | if (TREE_CODE (ctor) == CONSTRUCTOR | |
5090 | && CONSTRUCTOR_NELTS (ctor) == 1 | |
5091 | && CONSTRUCTOR_ELT (ctor, 0)->index | |
5092 | && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL) | |
5093 | return CONSTRUCTOR_ELT (ctor, 0)->index; | |
5094 | return NULL_TREE; | |
5095 | } | |
5096 | ||
0fe2ae29 JJ |
5097 | /* Helper function for cxx_fold_indirect_ref_1, called recursively. */ |
5098 | ||
5099 | static tree | |
43e84ce7 JJ |
5100 | cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type, |
5101 | tree op, unsigned HOST_WIDE_INT off, bool *empty_base) | |
0fe2ae29 JJ |
5102 | { |
5103 | tree optype = TREE_TYPE (op); | |
5104 | unsigned HOST_WIDE_INT const_nunits; | |
bc99c54d JM |
5105 | if (off == 0 && similar_type_p (optype, type)) |
5106 | return op; | |
5107 | else if (TREE_CODE (optype) == COMPLEX_TYPE | |
5108 | && similar_type_p (type, TREE_TYPE (optype))) | |
0fe2ae29 | 5109 | { |
0fe2ae29 | 5110 | /* *(foo *)&complexfoo => __real__ complexfoo */ |
bc99c54d | 5111 | if (off == 0) |
0fe2ae29 | 5112 | return build1_loc (loc, REALPART_EXPR, type, op); |
bc99c54d JM |
5113 | /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */ |
5114 | else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off) | |
5115 | return build1_loc (loc, IMAGPART_EXPR, type, op); | |
0fe2ae29 JJ |
5116 | } |
5117 | /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */ | |
5118 | else if (VECTOR_TYPE_P (optype) | |
5119 | && similar_type_p (type, TREE_TYPE (optype)) | |
5120 | && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits)) | |
5121 | { | |
5122 | unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type)); | |
5123 | unsigned HOST_WIDE_INT max_offset = part_width * const_nunits; | |
5124 | if (off < max_offset && off % part_width == 0) | |
5125 | { | |
5126 | tree index = bitsize_int (off * BITS_PER_UNIT); | |
5127 | return build3_loc (loc, BIT_FIELD_REF, type, op, | |
5128 | TYPE_SIZE (type), index); | |
5129 | } | |
5130 | } | |
5131 | /* ((foo *)&fooarray)[x] => fooarray[x] */ | |
5132 | else if (TREE_CODE (optype) == ARRAY_TYPE | |
5133 | && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype))) | |
5134 | && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype)))) | |
5135 | { | |
5136 | tree type_domain = TYPE_DOMAIN (optype); | |
5137 | tree min_val = size_zero_node; | |
5138 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
5139 | min_val = TYPE_MIN_VALUE (type_domain); | |
5140 | unsigned HOST_WIDE_INT el_sz | |
5141 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype))); | |
5142 | unsigned HOST_WIDE_INT idx = off / el_sz; | |
5143 | unsigned HOST_WIDE_INT rem = off % el_sz; | |
5144 | if (tree_fits_uhwi_p (min_val)) | |
5145 | { | |
5146 | tree index = size_int (idx + tree_to_uhwi (min_val)); | |
5147 | op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index, | |
5148 | NULL_TREE, NULL_TREE); | |
43e84ce7 | 5149 | return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem, |
0fe2ae29 JJ |
5150 | empty_base); |
5151 | } | |
5152 | } | |
5153 | /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */ | |
43e84ce7 JJ |
5154 | else if (TREE_CODE (optype) == RECORD_TYPE |
5155 | || TREE_CODE (optype) == UNION_TYPE) | |
0fe2ae29 | 5156 | { |
43e84ce7 JJ |
5157 | if (TREE_CODE (optype) == UNION_TYPE) |
5158 | /* For unions prefer the currently active member. */ | |
5159 | if (tree field = cxx_union_active_member (ctx, op)) | |
5160 | { | |
5161 | unsigned HOST_WIDE_INT el_sz | |
5162 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field))); | |
5163 | if (off < el_sz) | |
5164 | { | |
5165 | tree cop = build3 (COMPONENT_REF, TREE_TYPE (field), | |
5166 | op, field, NULL_TREE); | |
5167 | if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop, | |
5168 | off, empty_base)) | |
5169 | return ret; | |
5170 | } | |
5171 | } | |
0fe2ae29 JJ |
5172 | for (tree field = TYPE_FIELDS (optype); |
5173 | field; field = DECL_CHAIN (field)) | |
5174 | if (TREE_CODE (field) == FIELD_DECL | |
5175 | && TREE_TYPE (field) != error_mark_node | |
5176 | && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field)))) | |
5177 | { | |
5178 | tree pos = byte_position (field); | |
5179 | if (!tree_fits_uhwi_p (pos)) | |
5180 | continue; | |
5181 | unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos); | |
43e84ce7 | 5182 | unsigned HOST_WIDE_INT el_sz |
0fe2ae29 JJ |
5183 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field))); |
5184 | if (upos <= off && off < upos + el_sz) | |
5185 | { | |
5186 | tree cop = build3 (COMPONENT_REF, TREE_TYPE (field), | |
5187 | op, field, NULL_TREE); | |
43e84ce7 | 5188 | if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop, |
0fe2ae29 JJ |
5189 | off - upos, |
5190 | empty_base)) | |
5191 | return ret; | |
5192 | } | |
5193 | } | |
bc99c54d JM |
5194 | /* Also handle conversion to an empty base class, which |
5195 | is represented with a NOP_EXPR. */ | |
5196 | if (is_empty_class (type) | |
5197 | && CLASS_TYPE_P (optype) | |
5198 | && DERIVED_FROM_P (type, optype)) | |
5199 | { | |
1189c038 JM |
5200 | if (empty_base) |
5201 | *empty_base = true; | |
bc99c54d JM |
5202 | return op; |
5203 | } | |
0fe2ae29 JJ |
5204 | } |
5205 | ||
5206 | return NULL_TREE; | |
5207 | } | |
5208 | ||
2d76680f PC |
5209 | /* A less strict version of fold_indirect_ref_1, which requires cv-quals to |
5210 | match. We want to be less strict for simple *& folding; if we have a | |
5211 | non-const temporary that we access through a const pointer, that should | |
5212 | work. We handle this here rather than change fold_indirect_ref_1 | |
5213 | because we're dealing with things like ADDR_EXPR of INTEGER_CST which | |
5214 | don't really make sense outside of constant expression evaluation. Also | |
5215 | we want to allow folding to COMPONENT_REF, which could cause trouble | |
0fe2ae29 | 5216 | with TBAA in fold_indirect_ref_1. */ |
2d76680f PC |
5217 | |
5218 | static tree | |
43e84ce7 | 5219 | cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type, |
1189c038 | 5220 | tree op0, bool *empty_base /* = NULL*/) |
2d76680f | 5221 | { |
ebe4bf41 MP |
5222 | tree sub = op0; |
5223 | tree subtype; | |
5224 | poly_uint64 const_op01; | |
2d76680f | 5225 | |
80de0002 JM |
5226 | /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */ |
5227 | while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR | |
5228 | || TREE_CODE (sub) == VIEW_CONVERT_EXPR) | |
5229 | { | |
5230 | if (TREE_CODE (sub) == NOP_EXPR | |
5231 | && REINTERPRET_CAST_P (sub)) | |
5232 | return NULL_TREE; | |
5233 | sub = TREE_OPERAND (sub, 0); | |
5234 | } | |
5235 | ||
2d76680f | 5236 | subtype = TREE_TYPE (sub); |
71a93b08 | 5237 | if (!INDIRECT_TYPE_P (subtype)) |
2d76680f PC |
5238 | return NULL_TREE; |
5239 | ||
ab36b554 PP |
5240 | /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing |
5241 | the innermost component into the offset until it would make the | |
5242 | offset positive, so that cxx_fold_indirect_ref_1 can identify | |
5243 | more folding opportunities. */ | |
5244 | auto canonicalize_obj_off = [] (tree& obj, tree& off) { | |
5245 | while (TREE_CODE (obj) == COMPONENT_REF | |
5246 | && (tree_int_cst_sign_bit (off) || integer_zerop (off))) | |
5247 | { | |
5248 | tree field = TREE_OPERAND (obj, 1); | |
5249 | tree pos = byte_position (field); | |
5250 | if (integer_zerop (off) && integer_nonzerop (pos)) | |
5251 | /* If the offset is already 0, keep going as long as the | |
5252 | component is at position 0. */ | |
5253 | break; | |
5254 | off = int_const_binop (PLUS_EXPR, off, pos); | |
5255 | obj = TREE_OPERAND (obj, 0); | |
5256 | } | |
5257 | }; | |
5258 | ||
2d76680f PC |
5259 | if (TREE_CODE (sub) == ADDR_EXPR) |
5260 | { | |
5261 | tree op = TREE_OPERAND (sub, 0); | |
5262 | tree optype = TREE_TYPE (op); | |
5263 | ||
5264 | /* *&CONST_DECL -> to the value of the const decl. */ | |
5265 | if (TREE_CODE (op) == CONST_DECL) | |
5266 | return DECL_INITIAL (op); | |
5267 | /* *&p => p; make sure to handle *&"str"[cst] here. */ | |
1a120ec1 | 5268 | if (similar_type_p (optype, type)) |
2d76680f PC |
5269 | { |
5270 | tree fop = fold_read_from_constant_string (op); | |
5271 | if (fop) | |
5272 | return fop; | |
5273 | else | |
5274 | return op; | |
5275 | } | |
0fe2ae29 | 5276 | else |
ab36b554 PP |
5277 | { |
5278 | tree off = integer_zero_node; | |
5279 | canonicalize_obj_off (op, off); | |
5280 | gcc_assert (integer_zerop (off)); | |
5281 | return cxx_fold_indirect_ref_1 (ctx, loc, type, op, 0, empty_base); | |
5282 | } | |
2d76680f PC |
5283 | } |
5284 | else if (TREE_CODE (sub) == POINTER_PLUS_EXPR | |
0fe2ae29 | 5285 | && tree_fits_uhwi_p (TREE_OPERAND (sub, 1))) |
2d76680f PC |
5286 | { |
5287 | tree op00 = TREE_OPERAND (sub, 0); | |
0120cd93 | 5288 | tree off = TREE_OPERAND (sub, 1); |
2d76680f PC |
5289 | |
5290 | STRIP_NOPS (op00); | |
5291 | if (TREE_CODE (op00) == ADDR_EXPR) | |
0120cd93 PP |
5292 | { |
5293 | tree obj = TREE_OPERAND (op00, 0); | |
ab36b554 | 5294 | canonicalize_obj_off (obj, off); |
0120cd93 PP |
5295 | return cxx_fold_indirect_ref_1 (ctx, loc, type, obj, |
5296 | tree_to_uhwi (off), empty_base); | |
5297 | } | |
2d76680f PC |
5298 | } |
5299 | /* *(foo *)fooarrptr => (*fooarrptr)[0] */ | |
5300 | else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE | |
1a120ec1 | 5301 | && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype)))) |
2d76680f PC |
5302 | { |
5303 | tree type_domain; | |
5304 | tree min_val = size_zero_node; | |
ebe4bf41 | 5305 | tree newsub |
43e84ce7 | 5306 | = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL); |
2d76680f PC |
5307 | if (newsub) |
5308 | sub = newsub; | |
5309 | else | |
5310 | sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub); | |
5311 | type_domain = TYPE_DOMAIN (TREE_TYPE (sub)); | |
5312 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
5313 | min_val = TYPE_MIN_VALUE (type_domain); | |
5314 | return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE, | |
5315 | NULL_TREE); | |
5316 | } | |
5317 | ||
5318 | return NULL_TREE; | |
5319 | } | |
5320 | ||
5321 | static tree | |
3e605b20 | 5322 | cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t, |
72f76540 | 5323 | value_cat lval, |
2d76680f PC |
5324 | bool *non_constant_p, bool *overflow_p) |
5325 | { | |
5326 | tree orig_op0 = TREE_OPERAND (t, 0); | |
2d76680f | 5327 | bool empty_base = false; |
2d76680f | 5328 | |
cda0a029 JM |
5329 | /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second |
5330 | operand is an integer-zero. Otherwise reject the MEM_REF for now. */ | |
5331 | ||
5332 | if (TREE_CODE (t) == MEM_REF | |
5333 | && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1)))) | |
5334 | { | |
5335 | gcc_assert (ctx->quiet); | |
5336 | *non_constant_p = true; | |
5337 | return t; | |
5338 | } | |
5339 | ||
255a48d6 | 5340 | /* First try to simplify it directly. */ |
43e84ce7 JJ |
5341 | tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), |
5342 | orig_op0, &empty_base); | |
255a48d6 | 5343 | if (!r) |
2d76680f | 5344 | { |
255a48d6 JM |
5345 | /* If that didn't work, evaluate the operand first. */ |
5346 | tree op0 = cxx_eval_constant_expression (ctx, orig_op0, | |
72f76540 | 5347 | vc_prvalue, non_constant_p, |
255a48d6 JM |
5348 | overflow_p); |
5349 | /* Don't VERIFY_CONSTANT here. */ | |
5350 | if (*non_constant_p) | |
5351 | return t; | |
5352 | ||
8bada5cd MS |
5353 | if (!lval && integer_zerop (op0)) |
5354 | { | |
5355 | if (!ctx->quiet) | |
5356 | error ("dereferencing a null pointer"); | |
5357 | *non_constant_p = true; | |
5358 | return t; | |
5359 | } | |
5360 | ||
43e84ce7 | 5361 | r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0, |
255a48d6 JM |
5362 | &empty_base); |
5363 | if (r == NULL_TREE) | |
2d76680f PC |
5364 | { |
5365 | /* We couldn't fold to a constant value. Make sure it's not | |
5366 | something we should have been able to fold. */ | |
255a48d6 JM |
5367 | tree sub = op0; |
5368 | STRIP_NOPS (sub); | |
5369 | if (TREE_CODE (sub) == ADDR_EXPR) | |
5370 | { | |
1a120ec1 | 5371 | gcc_assert (!similar_type_p |
255a48d6 JM |
5372 | (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t))); |
5373 | /* DR 1188 says we don't have to deal with this. */ | |
5374 | if (!ctx->quiet) | |
1a120ec1 JM |
5375 | error_at (cp_expr_loc_or_input_loc (t), |
5376 | "accessing value of %qE through a %qT glvalue in a " | |
5377 | "constant expression", build_fold_indirect_ref (sub), | |
5378 | TREE_TYPE (t)); | |
255a48d6 JM |
5379 | *non_constant_p = true; |
5380 | return t; | |
5381 | } | |
5382 | ||
72f76540 | 5383 | if (lval == vc_glvalue && op0 != orig_op0) |
255a48d6 JM |
5384 | return build1 (INDIRECT_REF, TREE_TYPE (t), op0); |
5385 | if (!lval) | |
5386 | VERIFY_CONSTANT (t); | |
2d76680f PC |
5387 | return t; |
5388 | } | |
5389 | } | |
5390 | ||
255a48d6 JM |
5391 | r = cxx_eval_constant_expression (ctx, r, |
5392 | lval, non_constant_p, overflow_p); | |
5393 | if (*non_constant_p) | |
5394 | return t; | |
5395 | ||
125db6a1 | 5396 | /* If we're pulling out the value of an empty base, just return an empty |
2d76680f | 5397 | CONSTRUCTOR. */ |
92a596e8 | 5398 | if (empty_base && !lval) |
2d76680f | 5399 | { |
2d76680f PC |
5400 | r = build_constructor (TREE_TYPE (t), NULL); |
5401 | TREE_CONSTANT (r) = true; | |
5402 | } | |
5403 | ||
2d76680f PC |
5404 | return r; |
5405 | } | |
5406 | ||
5407 | /* Complain about R, a VAR_DECL, not being usable in a constant expression. | |
5408 | Shared between potential_constant_expression and | |
5409 | cxx_eval_constant_expression. */ | |
5410 | ||
5411 | static void | |
6821245b | 5412 | non_const_var_error (location_t loc, tree r) |
2d76680f | 5413 | { |
8e007055 | 5414 | auto_diagnostic_group d; |
2d76680f | 5415 | tree type = TREE_TYPE (r); |
8e007055 | 5416 | if (DECL_NAME (r) == heap_uninit_identifier |
815baade JJ |
5417 | || DECL_NAME (r) == heap_identifier |
5418 | || DECL_NAME (r) == heap_vec_uninit_identifier | |
5419 | || DECL_NAME (r) == heap_vec_identifier) | |
8e007055 | 5420 | { |
6821245b JM |
5421 | error_at (loc, "the content of uninitialized storage is not usable " |
5422 | "in a constant expression"); | |
8e007055 JJ |
5423 | inform (DECL_SOURCE_LOCATION (r), "allocated here"); |
5424 | return; | |
5425 | } | |
5426 | if (DECL_NAME (r) == heap_deleted_identifier) | |
5427 | { | |
6821245b JM |
5428 | error_at (loc, "use of allocated storage after deallocation in a " |
5429 | "constant expression"); | |
8e007055 JJ |
5430 | inform (DECL_SOURCE_LOCATION (r), "allocated here"); |
5431 | return; | |
5432 | } | |
6821245b JM |
5433 | error_at (loc, "the value of %qD is not usable in a constant " |
5434 | "expression", r); | |
2d76680f PC |
5435 | /* Avoid error cascade. */ |
5436 | if (DECL_INITIAL (r) == error_mark_node) | |
5437 | return; | |
5438 | if (DECL_DECLARED_CONSTEXPR_P (r)) | |
5439 | inform (DECL_SOURCE_LOCATION (r), | |
5440 | "%qD used in its own initializer", r); | |
5441 | else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) | |
5442 | { | |
5443 | if (!CP_TYPE_CONST_P (type)) | |
5444 | inform (DECL_SOURCE_LOCATION (r), | |
5445 | "%q#D is not const", r); | |
5446 | else if (CP_TYPE_VOLATILE_P (type)) | |
5447 | inform (DECL_SOURCE_LOCATION (r), | |
5448 | "%q#D is volatile", r); | |
5449 | else if (!DECL_INITIAL (r) | |
a3e2b438 PP |
5450 | || !TREE_CONSTANT (DECL_INITIAL (r)) |
5451 | || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)) | |
2d76680f PC |
5452 | inform (DECL_SOURCE_LOCATION (r), |
5453 | "%qD was not initialized with a constant " | |
5454 | "expression", r); | |
5455 | else | |
5456 | gcc_unreachable (); | |
5457 | } | |
9f613f06 | 5458 | else if (TYPE_REF_P (type)) |
fd338b13 JM |
5459 | inform (DECL_SOURCE_LOCATION (r), |
5460 | "%qD was not initialized with a constant " | |
5461 | "expression", r); | |
2d76680f PC |
5462 | else |
5463 | { | |
5464 | if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r)) | |
5465 | inform (DECL_SOURCE_LOCATION (r), | |
5466 | "%qD was not declared %<constexpr%>", r); | |
5467 | else | |
5468 | inform (DECL_SOURCE_LOCATION (r), | |
5469 | "%qD does not have integral or enumeration type", | |
5470 | r); | |
5471 | } | |
5472 | } | |
5473 | ||
5474 | /* Subroutine of cxx_eval_constant_expression. | |
5475 | Like cxx_eval_unary_expression, except for trinary expressions. */ | |
5476 | ||
5477 | static tree | |
3e605b20 | 5478 | cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t, |
72f76540 | 5479 | value_cat lval, |
2d76680f PC |
5480 | bool *non_constant_p, bool *overflow_p) |
5481 | { | |
5482 | int i; | |
5483 | tree args[3]; | |
5484 | tree val; | |
5485 | ||
5486 | for (i = 0; i < 3; i++) | |
5487 | { | |
3e605b20 | 5488 | args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i), |
92a596e8 | 5489 | lval, |
5a804683 | 5490 | non_constant_p, overflow_p); |
2d76680f PC |
5491 | VERIFY_CONSTANT (args[i]); |
5492 | } | |
5493 | ||
5494 | val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t), | |
5495 | args[0], args[1], args[2]); | |
5496 | if (val == NULL_TREE) | |
5497 | return t; | |
5498 | VERIFY_CONSTANT (val); | |
5499 | return val; | |
5500 | } | |
5501 | ||
98e5a19a JM |
5502 | /* True if T was declared in a function declared to be constexpr, and |
5503 | therefore potentially constant in C++14. */ | |
5504 | ||
2d76680f PC |
5505 | bool |
5506 | var_in_constexpr_fn (tree t) | |
5507 | { | |
5508 | tree ctx = DECL_CONTEXT (t); | |
98e5a19a | 5509 | return (ctx && TREE_CODE (ctx) == FUNCTION_DECL |
2d76680f PC |
5510 | && DECL_DECLARED_CONSTEXPR_P (ctx)); |
5511 | } | |
5512 | ||
26dbe85a MP |
5513 | /* True if a function might be constexpr: either a function that was |
5514 | declared constexpr, or a C++17 lambda op(). */ | |
5515 | ||
5516 | bool | |
5517 | maybe_constexpr_fn (tree t) | |
5518 | { | |
5519 | return (DECL_DECLARED_CONSTEXPR_P (t) | |
87c2080b JM |
5520 | || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t)) |
5521 | || (flag_implicit_constexpr | |
5522 | && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t)))); | |
26dbe85a MP |
5523 | } |
5524 | ||
98e5a19a JM |
5525 | /* True if T was declared in a function that might be constexpr: either a |
5526 | function that was declared constexpr, or a C++17 lambda op(). */ | |
5527 | ||
5528 | bool | |
5529 | var_in_maybe_constexpr_fn (tree t) | |
5530 | { | |
87c2080b JM |
5531 | return (DECL_FUNCTION_SCOPE_P (t) |
5532 | && maybe_constexpr_fn (DECL_CONTEXT (t))); | |
98e5a19a JM |
5533 | } |
5534 | ||
68d01920 JM |
5535 | /* We're assigning INIT to TARGET. In do_build_copy_constructor and |
5536 | build_over_call we implement trivial copy of a class with tail padding using | |
5537 | assignment of character arrays, which is valid in normal code, but not in | |
5538 | constexpr evaluation. We don't need to worry about clobbering tail padding | |
5539 | in constexpr evaluation, so strip the type punning. */ | |
5540 | ||
5541 | static void | |
5542 | maybe_simplify_trivial_copy (tree &target, tree &init) | |
5543 | { | |
5544 | if (TREE_CODE (target) == MEM_REF | |
5545 | && TREE_CODE (init) == MEM_REF | |
5546 | && TREE_TYPE (target) == TREE_TYPE (init) | |
5547 | && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE | |
5548 | && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node) | |
5549 | { | |
5550 | target = build_fold_indirect_ref (TREE_OPERAND (target, 0)); | |
5551 | init = build_fold_indirect_ref (TREE_OPERAND (init, 0)); | |
5552 | } | |
5553 | } | |
5554 | ||
7eb5be6a MP |
5555 | /* Returns true if REF, which is a COMPONENT_REF, has any fields |
5556 | of constant type. This does not check for 'mutable', so the | |
5557 | caller is expected to be mindful of that. */ | |
5558 | ||
5559 | static bool | |
5560 | cref_has_const_field (tree ref) | |
5561 | { | |
5562 | while (TREE_CODE (ref) == COMPONENT_REF) | |
5563 | { | |
5564 | if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1)))) | |
5565 | return true; | |
5566 | ref = TREE_OPERAND (ref, 0); | |
5567 | } | |
5568 | return false; | |
5569 | } | |
5570 | ||
04e1749c MP |
5571 | /* Return true if we are modifying something that is const during constant |
5572 | expression evaluation. CODE is the code of the statement, OBJ is the | |
5573 | object in question, MUTABLE_P is true if one of the subobjects were | |
5574 | declared mutable. */ | |
5575 | ||
5576 | static bool | |
5577 | modifying_const_object_p (tree_code code, tree obj, bool mutable_p) | |
5578 | { | |
5579 | /* If this is initialization, there's no problem. */ | |
5580 | if (code != MODIFY_EXPR) | |
5581 | return false; | |
5582 | ||
5583 | /* [basic.type.qualifier] "A const object is an object of type | |
5584 | const T or a non-mutable subobject of a const object." */ | |
5585 | if (mutable_p) | |
5586 | return false; | |
5587 | ||
7eb5be6a MP |
5588 | if (TREE_READONLY (obj)) |
5589 | return true; | |
5590 | ||
5591 | if (CP_TYPE_CONST_P (TREE_TYPE (obj))) | |
5592 | { | |
5593 | /* Although a COMPONENT_REF may have a const type, we should | |
5594 | only consider it modifying a const object when any of the | |
5595 | field components is const. This can happen when using | |
5596 | constructs such as const_cast<const T &>(m), making something | |
5597 | const even though it wasn't declared const. */ | |
5598 | if (TREE_CODE (obj) == COMPONENT_REF) | |
5599 | return cref_has_const_field (obj); | |
5600 | else | |
5601 | return true; | |
5602 | } | |
5603 | ||
5604 | return false; | |
04e1749c MP |
5605 | } |
5606 | ||
60813a46 JM |
5607 | /* Evaluate an INIT_EXPR or MODIFY_EXPR. */ |
5608 | ||
5609 | static tree | |
5610 | cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, | |
72f76540 | 5611 | value_cat lval, |
60813a46 JM |
5612 | bool *non_constant_p, bool *overflow_p) |
5613 | { | |
5614 | constexpr_ctx new_ctx = *ctx; | |
5615 | ||
58cc255c JM |
5616 | tree init = TREE_OPERAND (t, 1); |
5617 | if (TREE_CLOBBER_P (init)) | |
5618 | /* Just ignore clobbers. */ | |
5619 | return void_node; | |
5620 | ||
60813a46 JM |
5621 | /* First we figure out where we're storing to. */ |
5622 | tree target = TREE_OPERAND (t, 0); | |
68d01920 JM |
5623 | |
5624 | maybe_simplify_trivial_copy (target, init); | |
5625 | ||
3f8e2835 | 5626 | tree type = TREE_TYPE (target); |
e8b3c1bc JM |
5627 | bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR; |
5628 | if (preeval) | |
5629 | { | |
5630 | /* Evaluate the value to be stored without knowing what object it will be | |
5631 | stored in, so that any side-effects happen first. */ | |
5632 | if (!SCALAR_TYPE_P (type)) | |
5633 | new_ctx.ctor = new_ctx.object = NULL_TREE; | |
72f76540 | 5634 | init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue, |
e8b3c1bc JM |
5635 | non_constant_p, overflow_p); |
5636 | if (*non_constant_p) | |
5637 | return t; | |
5638 | } | |
60813a46 | 5639 | |
d0aa42d2 | 5640 | bool evaluated = false; |
72f76540 | 5641 | if (lval == vc_glvalue) |
3f8e2835 | 5642 | { |
d0aa42d2 JM |
5643 | /* If we want to return a reference to the target, we need to evaluate it |
5644 | as a whole; otherwise, only evaluate the innermost piece to avoid | |
5645 | building up unnecessary *_REFs. */ | |
72f76540 | 5646 | target = cxx_eval_constant_expression (ctx, target, lval, |
d0aa42d2 JM |
5647 | non_constant_p, overflow_p); |
5648 | evaluated = true; | |
5649 | if (*non_constant_p) | |
5650 | return t; | |
3f8e2835 JM |
5651 | } |
5652 | ||
043666e0 | 5653 | /* Find the underlying variable. */ |
cd9cf97b | 5654 | releasing_vec refs; |
60813a46 | 5655 | tree object = NULL_TREE; |
04e1749c MP |
5656 | /* If we're modifying a const object, save it. */ |
5657 | tree const_object_being_modified = NULL_TREE; | |
5658 | bool mutable_p = false; | |
60813a46 JM |
5659 | for (tree probe = target; object == NULL_TREE; ) |
5660 | { | |
5661 | switch (TREE_CODE (probe)) | |
5662 | { | |
5663 | case BIT_FIELD_REF: | |
5664 | case COMPONENT_REF: | |
5665 | case ARRAY_REF: | |
043666e0 JM |
5666 | { |
5667 | tree ob = TREE_OPERAND (probe, 0); | |
5668 | tree elt = TREE_OPERAND (probe, 1); | |
7d349dd8 | 5669 | if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt)) |
04e1749c | 5670 | mutable_p = true; |
043666e0 JM |
5671 | if (TREE_CODE (probe) == ARRAY_REF) |
5672 | { | |
5673 | elt = eval_and_check_array_index (ctx, probe, false, | |
5674 | non_constant_p, overflow_p); | |
5675 | if (*non_constant_p) | |
5676 | return t; | |
5677 | } | |
0f184800 MP |
5678 | /* We don't check modifying_const_object_p for ARRAY_REFs. Given |
5679 | "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though | |
5680 | the array isn't const. Instead, check "a" in the next iteration; | |
5681 | that will detect modifying "const int a[10]". */ | |
5682 | else if (evaluated | |
5683 | && modifying_const_object_p (TREE_CODE (t), probe, | |
5684 | mutable_p) | |
5685 | && const_object_being_modified == NULL_TREE) | |
5686 | const_object_being_modified = probe; | |
043666e0 JM |
5687 | vec_safe_push (refs, elt); |
5688 | vec_safe_push (refs, TREE_TYPE (probe)); | |
5689 | probe = ob; | |
5690 | } | |
60813a46 JM |
5691 | break; |
5692 | ||
5693 | default: | |
d0aa42d2 JM |
5694 | if (evaluated) |
5695 | object = probe; | |
5696 | else | |
5697 | { | |
72f76540 | 5698 | probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue, |
d0aa42d2 JM |
5699 | non_constant_p, overflow_p); |
5700 | evaluated = true; | |
5701 | if (*non_constant_p) | |
5702 | return t; | |
5703 | } | |
5704 | break; | |
60813a46 JM |
5705 | } |
5706 | } | |
5707 | ||
04e1749c MP |
5708 | if (modifying_const_object_p (TREE_CODE (t), object, mutable_p) |
5709 | && const_object_being_modified == NULL_TREE) | |
5710 | const_object_being_modified = object; | |
5711 | ||
60813a46 JM |
5712 | /* And then find/build up our initializer for the path to the subobject |
5713 | we're initializing. */ | |
a1408b7e | 5714 | tree *valp; |
077dd9b3 | 5715 | if (DECL_P (object)) |
8e007055 | 5716 | valp = ctx->global->values.get (object); |
a1408b7e JM |
5717 | else |
5718 | valp = NULL; | |
60813a46 JM |
5719 | if (!valp) |
5720 | { | |
5721 | /* A constant-expression cannot modify objects from outside the | |
5722 | constant-expression. */ | |
2b3ab879 | 5723 | if (!ctx->quiet) |
64d6d399 | 5724 | error ("modification of %qE is not a constant expression", object); |
60813a46 JM |
5725 | *non_constant_p = true; |
5726 | return t; | |
5727 | } | |
3f8e2835 | 5728 | type = TREE_TYPE (object); |
c75ce530 | 5729 | bool no_zero_init = true; |
2d63bc39 | 5730 | |
37244b21 PP |
5731 | releasing_vec ctors, indexes; |
5732 | auto_vec<int> index_pos_hints; | |
5733 | bool activated_union_member_p = false; | |
967cdbe6 | 5734 | bool empty_base = false; |
276a52d5 | 5735 | while (!refs->is_empty ()) |
60813a46 JM |
5736 | { |
5737 | if (*valp == NULL_TREE) | |
5738 | { | |
5739 | *valp = build_constructor (type, NULL); | |
e8c48716 | 5740 | CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; |
60813a46 | 5741 | } |
d6b46fca NS |
5742 | else if (TREE_CODE (*valp) == STRING_CST) |
5743 | { | |
5744 | /* An array was initialized with a string constant, and now | |
5745 | we're writing into one of its elements. Explode the | |
5746 | single initialization into a set of element | |
5747 | initializations. */ | |
5748 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); | |
5749 | ||
5750 | tree string = *valp; | |
5751 | tree elt_type = TREE_TYPE (type); | |
5752 | unsigned chars_per_elt = (TYPE_PRECISION (elt_type) | |
5753 | / TYPE_PRECISION (char_type_node)); | |
5754 | unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt; | |
5755 | tree ary_ctor = build_constructor (type, NULL); | |
5756 | ||
5757 | vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts); | |
5758 | for (unsigned ix = 0; ix != num_elts; ix++) | |
5759 | { | |
5760 | constructor_elt elt = | |
5761 | { | |
5762 | build_int_cst (size_type_node, ix), | |
5763 | extract_string_elt (string, chars_per_elt, ix) | |
5764 | }; | |
5765 | CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt); | |
5766 | } | |
94ff4c9d | 5767 | |
d6b46fca NS |
5768 | *valp = ary_ctor; |
5769 | } | |
5770 | ||
c75ce530 JM |
5771 | /* If the value of object is already zero-initialized, any new ctors for |
5772 | subobjects will also be zero-initialized. */ | |
e8c48716 | 5773 | no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp); |
60813a46 | 5774 | |
ceaaf873 | 5775 | enum tree_code code = TREE_CODE (type); |
967cdbe6 | 5776 | tree reftype = refs->pop(); |
ceaaf873 | 5777 | tree index = refs->pop(); |
60813a46 | 5778 | |
a8dd2b3e | 5779 | if (code == RECORD_TYPE && is_empty_field (index)) |
a4dfd0f0 JM |
5780 | /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they |
5781 | have no data and might have an offset lower than previously declared | |
5782 | fields, which confuses the middle-end. The code below will notice | |
5783 | that we don't have a CONSTRUCTOR for our inner target and just | |
5784 | return init. */ | |
967cdbe6 JM |
5785 | { |
5786 | empty_base = true; | |
5787 | break; | |
5788 | } | |
5789 | ||
5790 | type = reftype; | |
94ff4c9d | 5791 | |
37244b21 PP |
5792 | if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp) |
5793 | && CONSTRUCTOR_ELT (*valp, 0)->index != index) | |
ceaaf873 | 5794 | { |
b04445d4 | 5795 | if (cxx_dialect < cxx20) |
ed4ec9ce | 5796 | { |
37244b21 PP |
5797 | if (!ctx->quiet) |
5798 | error_at (cp_expr_loc_or_input_loc (t), | |
5799 | "change of the active member of a union " | |
5800 | "from %qD to %qD", | |
5801 | CONSTRUCTOR_ELT (*valp, 0)->index, | |
5802 | index); | |
5803 | *non_constant_p = true; | |
ed4ec9ce | 5804 | } |
37244b21 PP |
5805 | else if (TREE_CODE (t) == MODIFY_EXPR |
5806 | && CONSTRUCTOR_NO_CLEARING (*valp)) | |
ceaaf873 | 5807 | { |
37244b21 PP |
5808 | /* Diagnose changing the active union member while the union |
5809 | is in the process of being initialized. */ | |
5810 | if (!ctx->quiet) | |
5811 | error_at (cp_expr_loc_or_input_loc (t), | |
5812 | "change of the active member of a union " | |
5813 | "from %qD to %qD during initialization", | |
5814 | CONSTRUCTOR_ELT (*valp, 0)->index, | |
5815 | index); | |
5816 | *non_constant_p = true; | |
ceaaf873 | 5817 | } |
37244b21 PP |
5818 | no_zero_init = true; |
5819 | } | |
88504f34 | 5820 | |
37244b21 PP |
5821 | vec_safe_push (ctors, *valp); |
5822 | vec_safe_push (indexes, index); | |
88504f34 | 5823 | |
37244b21 | 5824 | constructor_elt *cep |
077dd9b3 | 5825 | = get_or_insert_ctor_field (*valp, index); |
37244b21 PP |
5826 | index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin()); |
5827 | ||
5828 | if (code == UNION_TYPE) | |
5829 | activated_union_member_p = true; | |
b599bf9d | 5830 | |
60813a46 JM |
5831 | valp = &cep->value; |
5832 | } | |
60813a46 | 5833 | |
04e1749c MP |
5834 | /* Detect modifying a constant object in constexpr evaluation. |
5835 | We have found a const object that is being modified. Figure out | |
5836 | if we need to issue an error. Consider | |
5837 | ||
5838 | struct A { | |
5839 | int n; | |
5840 | constexpr A() : n(1) { n = 2; } // #1 | |
5841 | }; | |
5842 | struct B { | |
5843 | const A a; | |
5844 | constexpr B() { a.n = 3; } // #2 | |
5845 | }; | |
5846 | constexpr B b{}; | |
5847 | ||
5848 | #1 is OK, since we're modifying an object under construction, but | |
5849 | #2 is wrong, since "a" is const and has been fully constructed. | |
5850 | To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR | |
5851 | which means that the object is read-only. For the example above, the | |
5852 | *ctors stack at the point of #2 will look like: | |
5853 | ||
5854 | ctors[0] = {.a={.n=2}} TREE_READONLY = 0 | |
5855 | ctors[1] = {.n=2} TREE_READONLY = 1 | |
5856 | ||
5857 | and we're modifying "b.a", so we search the stack and see if the | |
5858 | constructor for "b.a" has already run. */ | |
5859 | if (const_object_being_modified) | |
5860 | { | |
5861 | bool fail = false; | |
276a52d5 JJ |
5862 | tree const_objtype |
5863 | = strip_array_types (TREE_TYPE (const_object_being_modified)); | |
5864 | if (!CLASS_TYPE_P (const_objtype)) | |
04e1749c MP |
5865 | fail = true; |
5866 | else | |
5867 | { | |
5868 | /* [class.ctor]p5 "A constructor can be invoked for a const, | |
5869 | volatile, or const volatile object. const and volatile | |
5870 | semantics are not applied on an object under construction. | |
5871 | They come into effect when the constructor for the most | |
5872 | derived object ends." */ | |
3f207ab3 | 5873 | for (tree elt : *ctors) |
04e1749c MP |
5874 | if (same_type_ignoring_top_level_qualifiers_p |
5875 | (TREE_TYPE (const_object_being_modified), TREE_TYPE (elt))) | |
5876 | { | |
5877 | fail = TREE_READONLY (elt); | |
5878 | break; | |
5879 | } | |
5880 | } | |
5881 | if (fail) | |
5882 | { | |
5883 | if (!ctx->quiet) | |
5884 | modifying_const_object_error (t, const_object_being_modified); | |
5885 | *non_constant_p = true; | |
5886 | return t; | |
5887 | } | |
5888 | } | |
5889 | ||
e8b3c1bc | 5890 | if (!preeval) |
60813a46 | 5891 | { |
6c59b8a9 JM |
5892 | /* We're handling an INIT_EXPR of class type, so the value of the |
5893 | initializer can depend on the object it's initializing. */ | |
5894 | ||
60813a46 JM |
5895 | /* Create a new CONSTRUCTOR in case evaluation of the initializer |
5896 | wants to modify it. */ | |
acb2970c | 5897 | if (*valp == NULL_TREE) |
73a84269 | 5898 | { |
664beaf2 | 5899 | *valp = build_constructor (type, NULL); |
e8c48716 | 5900 | CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; |
73a84269 | 5901 | } |
664beaf2 | 5902 | new_ctx.ctor = *valp; |
60813a46 | 5903 | new_ctx.object = target; |
10d2f801 JM |
5904 | /* Avoid temporary materialization when initializing from a TARGET_EXPR. |
5905 | We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because | |
5906 | expansion of those trees uses ctx instead. */ | |
5907 | if (TREE_CODE (init) == TARGET_EXPR) | |
5908 | if (tree tinit = TARGET_EXPR_INITIAL (init)) | |
5909 | init = tinit; | |
72f76540 | 5910 | init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue, |
e8b3c1bc | 5911 | non_constant_p, overflow_p); |
37244b21 PP |
5912 | /* The hash table might have moved since the get earlier, and the |
5913 | initializer might have mutated the underlying CONSTRUCTORs, so we must | |
5914 | recompute VALP. */ | |
5915 | valp = ctx->global->values.get (object); | |
5916 | for (unsigned i = 0; i < vec_safe_length (indexes); i++) | |
5917 | { | |
5918 | constructor_elt *cep | |
5919 | = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]); | |
5920 | valp = &cep->value; | |
5921 | } | |
60813a46 JM |
5922 | } |
5923 | ||
967cdbe6 JM |
5924 | if (*non_constant_p) |
5925 | return t; | |
5926 | ||
7574c916 | 5927 | /* Don't share a CONSTRUCTOR that might be changed later. */ |
0146e25f | 5928 | init = unshare_constructor (init); |
d96e8407 | 5929 | |
967cdbe6 JM |
5930 | gcc_checking_assert (!*valp || (same_type_ignoring_top_level_qualifiers_p |
5931 | (TREE_TYPE (*valp), type))); | |
5932 | if (empty_base || !(same_type_ignoring_top_level_qualifiers_p | |
2540e2c6 | 5933 | (initialized_type (init), type))) |
967cdbe6 JM |
5934 | { |
5935 | /* For initialization of an empty base, the original target will be | |
5936 | *(base*)this, evaluation of which resolves to the object | |
5937 | argument, which has the derived type rather than the base type. In | |
5938 | this situation, just evaluate the initializer and return, since | |
5939 | there's no actual data to store, and we didn't build a CONSTRUCTOR. */ | |
2540e2c6 | 5940 | gcc_assert (is_empty_class (TREE_TYPE (target))); |
967cdbe6 | 5941 | empty_base = true; |
967cdbe6 JM |
5942 | if (!*valp) |
5943 | { | |
5944 | /* But do make sure we have something in *valp. */ | |
5945 | *valp = build_constructor (type, nullptr); | |
5946 | CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; | |
5947 | } | |
5948 | } | |
5949 | else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR | |
5950 | && TREE_CODE (init) == CONSTRUCTOR) | |
acb2970c | 5951 | { |
d96e8407 JM |
5952 | /* An outer ctx->ctor might be pointing to *valp, so replace |
5953 | its contents. */ | |
5954 | CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init); | |
5955 | TREE_CONSTANT (*valp) = TREE_CONSTANT (init); | |
5956 | TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init); | |
e8c48716 JM |
5957 | CONSTRUCTOR_NO_CLEARING (*valp) |
5958 | = CONSTRUCTOR_NO_CLEARING (init); | |
acb2970c | 5959 | } |
60813a46 | 5960 | else |
d96e8407 | 5961 | *valp = init; |
2d63bc39 | 5962 | |
7eb5be6a MP |
5963 | /* After initialization, 'const' semantics apply to the value of the |
5964 | object. Make a note of this fact by marking the CONSTRUCTOR | |
5965 | TREE_READONLY. */ | |
5966 | if (TREE_CODE (t) == INIT_EXPR | |
5967 | && TREE_CODE (*valp) == CONSTRUCTOR | |
5968 | && TYPE_READONLY (type)) | |
64da1b76 PP |
5969 | { |
5970 | if (INDIRECT_REF_P (target) | |
5971 | && (is_this_parameter | |
5972 | (tree_strip_nop_conversions (TREE_OPERAND (target, 0))))) | |
5973 | /* We've just initialized '*this' (perhaps via the target | |
5974 | constructor of a delegating constructor). Leave it up to the | |
5975 | caller that set 'this' to set TREE_READONLY appropriately. */ | |
5976 | gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p | |
967cdbe6 | 5977 | (TREE_TYPE (target), type) || empty_base); |
64da1b76 PP |
5978 | else |
5979 | TREE_READONLY (*valp) = true; | |
5980 | } | |
7eb5be6a | 5981 | |
d96e8407 JM |
5982 | /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing |
5983 | CONSTRUCTORs, if any. */ | |
d96e8407 JM |
5984 | bool c = TREE_CONSTANT (init); |
5985 | bool s = TREE_SIDE_EFFECTS (init); | |
37244b21 | 5986 | if (!c || s || activated_union_member_p) |
3f207ab3 | 5987 | for (tree elt : *ctors) |
d96e8407 JM |
5988 | { |
5989 | if (!c) | |
5990 | TREE_CONSTANT (elt) = false; | |
5991 | if (s) | |
5992 | TREE_SIDE_EFFECTS (elt) = true; | |
b599bf9d PP |
5993 | /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of |
5994 | this union. */ | |
5995 | if (TREE_CODE (TREE_TYPE (elt)) == UNION_TYPE) | |
5996 | CONSTRUCTOR_NO_CLEARING (elt) = false; | |
d96e8407 | 5997 | } |
60813a46 | 5998 | |
967cdbe6 | 5999 | if (lval) |
60813a46 JM |
6000 | return target; |
6001 | else | |
3bdf0a9b | 6002 | return init; |
60813a46 JM |
6003 | } |
6004 | ||
6005 | /* Evaluate a ++ or -- expression. */ | |
6006 | ||
6007 | static tree | |
6008 | cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t, | |
72f76540 | 6009 | value_cat lval, |
60813a46 JM |
6010 | bool *non_constant_p, bool *overflow_p) |
6011 | { | |
6012 | enum tree_code code = TREE_CODE (t); | |
6013 | tree type = TREE_TYPE (t); | |
6014 | tree op = TREE_OPERAND (t, 0); | |
6015 | tree offset = TREE_OPERAND (t, 1); | |
6016 | gcc_assert (TREE_CONSTANT (offset)); | |
6017 | ||
d85569f6 MP |
6018 | /* OFFSET is constant, but perhaps not constant enough. We need to |
6019 | e.g. bash FLOAT_EXPRs to REAL_CSTs. */ | |
6020 | offset = fold_simple (offset); | |
6021 | ||
60813a46 | 6022 | /* The operand as an lvalue. */ |
72f76540 | 6023 | op = cxx_eval_constant_expression (ctx, op, vc_glvalue, |
5a804683 | 6024 | non_constant_p, overflow_p); |
60813a46 JM |
6025 | |
6026 | /* The operand as an rvalue. */ | |
84dd815f | 6027 | tree val |
72f76540 | 6028 | = cxx_eval_constant_expression (ctx, op, vc_prvalue, |
84dd815f | 6029 | non_constant_p, overflow_p); |
caee690e JM |
6030 | /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to |
6031 | a local array in a constexpr function. */ | |
71a93b08 | 6032 | bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val)); |
caee690e JM |
6033 | if (!ptr) |
6034 | VERIFY_CONSTANT (val); | |
60813a46 JM |
6035 | |
6036 | /* The modified value. */ | |
6037 | bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR); | |
ac2f8d26 | 6038 | tree mod; |
71a93b08 | 6039 | if (INDIRECT_TYPE_P (type)) |
ac2f8d26 JM |
6040 | { |
6041 | /* The middle end requires pointers to use POINTER_PLUS_EXPR. */ | |
6042 | offset = convert_to_ptrofftype (offset); | |
6043 | if (!inc) | |
6044 | offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset); | |
6045 | mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset); | |
6046 | } | |
6047 | else | |
6048 | mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset); | |
caee690e JM |
6049 | if (!ptr) |
6050 | VERIFY_CONSTANT (mod); | |
60813a46 JM |
6051 | |
6052 | /* Storing the modified value. */ | |
04e1749c MP |
6053 | tree store = build2_loc (cp_expr_loc_or_loc (t, input_location), |
6054 | MODIFY_EXPR, type, op, mod); | |
d1bba463 PP |
6055 | mod = cxx_eval_constant_expression (ctx, store, lval, |
6056 | non_constant_p, overflow_p); | |
ecdcd560 | 6057 | ggc_free (store); |
d1bba463 PP |
6058 | if (*non_constant_p) |
6059 | return t; | |
60813a46 JM |
6060 | |
6061 | /* And the value of the expression. */ | |
6062 | if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR) | |
d1bba463 PP |
6063 | /* Prefix ops are lvalues, but the caller might want an rvalue; |
6064 | lval has already been taken into account in the store above. */ | |
6065 | return mod; | |
60813a46 JM |
6066 | else |
6067 | /* Postfix ops are rvalues. */ | |
6068 | return val; | |
6069 | } | |
6070 | ||
56632b27 JM |
6071 | /* Predicates for the meaning of *jump_target. */ |
6072 | ||
6073 | static bool | |
6074 | returns (tree *jump_target) | |
6075 | { | |
6076 | return *jump_target | |
9c6a4bee | 6077 | && TREE_CODE (*jump_target) == RETURN_EXPR; |
56632b27 JM |
6078 | } |
6079 | ||
6080 | static bool | |
6081 | breaks (tree *jump_target) | |
6082 | { | |
6083 | return *jump_target | |
06ec22b7 JM |
6084 | && ((TREE_CODE (*jump_target) == LABEL_DECL |
6085 | && LABEL_DECL_BREAK (*jump_target)) | |
3075affd | 6086 | || TREE_CODE (*jump_target) == BREAK_STMT |
06ec22b7 | 6087 | || TREE_CODE (*jump_target) == EXIT_EXPR); |
56632b27 JM |
6088 | } |
6089 | ||
6090 | static bool | |
6091 | continues (tree *jump_target) | |
6092 | { | |
6093 | return *jump_target | |
3075affd JM |
6094 | && ((TREE_CODE (*jump_target) == LABEL_DECL |
6095 | && LABEL_DECL_CONTINUE (*jump_target)) | |
6096 | || TREE_CODE (*jump_target) == CONTINUE_STMT); | |
6097 | ||
56632b27 JM |
6098 | } |
6099 | ||
6100 | static bool | |
6101 | switches (tree *jump_target) | |
6102 | { | |
6103 | return *jump_target | |
6104 | && TREE_CODE (*jump_target) == INTEGER_CST; | |
6105 | } | |
6106 | ||
6107 | /* Subroutine of cxx_eval_statement_list. Determine whether the statement | |
4b390698 JJ |
6108 | STMT matches *jump_target. If we're looking for a case label and we see |
6109 | the default label, note it in ctx->css_state. */ | |
56632b27 JM |
6110 | |
6111 | static bool | |
4b390698 | 6112 | label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt) |
56632b27 | 6113 | { |
56632b27 JM |
6114 | switch (TREE_CODE (*jump_target)) |
6115 | { | |
6116 | case LABEL_DECL: | |
6117 | if (TREE_CODE (stmt) == LABEL_EXPR | |
6118 | && LABEL_EXPR_LABEL (stmt) == *jump_target) | |
6119 | return true; | |
6120 | break; | |
6121 | ||
6122 | case INTEGER_CST: | |
6123 | if (TREE_CODE (stmt) == CASE_LABEL_EXPR) | |
6124 | { | |
4b390698 | 6125 | gcc_assert (ctx->css_state != NULL); |
56632b27 | 6126 | if (!CASE_LOW (stmt)) |
4b390698 JJ |
6127 | { |
6128 | /* default: should appear just once in a SWITCH_EXPR | |
6129 | body (excluding nested SWITCH_EXPR). */ | |
6130 | gcc_assert (*ctx->css_state != css_default_seen); | |
6131 | /* When evaluating SWITCH_EXPR body for the second time, | |
6132 | return true for the default: label. */ | |
6133 | if (*ctx->css_state == css_default_processing) | |
6134 | return true; | |
6135 | *ctx->css_state = css_default_seen; | |
6136 | } | |
385ed708 JJ |
6137 | else if (CASE_HIGH (stmt)) |
6138 | { | |
6139 | if (tree_int_cst_le (CASE_LOW (stmt), *jump_target) | |
6140 | && tree_int_cst_le (*jump_target, CASE_HIGH (stmt))) | |
6141 | return true; | |
6142 | } | |
56632b27 JM |
6143 | else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt))) |
6144 | return true; | |
6145 | } | |
6146 | break; | |
6147 | ||
43574e4f JJ |
6148 | case BREAK_STMT: |
6149 | case CONTINUE_STMT: | |
6150 | /* These two are handled directly in cxx_eval_loop_expr by testing | |
6151 | breaks (jump_target) or continues (jump_target). */ | |
6152 | break; | |
6153 | ||
56632b27 JM |
6154 | default: |
6155 | gcc_unreachable (); | |
6156 | } | |
6157 | return false; | |
6158 | } | |
6159 | ||
6160 | /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump | |
6161 | semantics, for switch, break, continue, and return. */ | |
6162 | ||
6163 | static tree | |
6164 | cxx_eval_statement_list (const constexpr_ctx *ctx, tree t, | |
56632b27 JM |
6165 | bool *non_constant_p, bool *overflow_p, |
6166 | tree *jump_target) | |
6167 | { | |
58611fb6 | 6168 | tree local_target; |
345edb37 JJ |
6169 | /* In a statement-expression we want to return the last value. |
6170 | For empty statement expression return void_node. */ | |
6171 | tree r = void_node; | |
58611fb6 JM |
6172 | if (!jump_target) |
6173 | { | |
6174 | local_target = NULL_TREE; | |
6175 | jump_target = &local_target; | |
6176 | } | |
72f76540 | 6177 | for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i) |
56632b27 | 6178 | { |
72f76540 JM |
6179 | tree stmt = *i; |
6180 | ||
0250ba58 MP |
6181 | /* We've found a continue, so skip everything until we reach |
6182 | the label its jumping to. */ | |
6183 | if (continues (jump_target)) | |
6184 | { | |
6185 | if (label_matches (ctx, jump_target, stmt)) | |
6186 | /* Found it. */ | |
6187 | *jump_target = NULL_TREE; | |
6188 | else | |
6189 | continue; | |
6190 | } | |
6ef72c36 JJ |
6191 | if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT) |
6192 | continue; | |
72f76540 JM |
6193 | |
6194 | value_cat lval = vc_discard; | |
6195 | /* The result of a statement-expression is not wrapped in EXPR_STMT. */ | |
6196 | if (tsi_one_before_end_p (i) && TREE_CODE (stmt) != EXPR_STMT) | |
6197 | lval = vc_prvalue; | |
6198 | ||
6199 | r = cxx_eval_constant_expression (ctx, stmt, lval, | |
58611fb6 JM |
6200 | non_constant_p, overflow_p, |
6201 | jump_target); | |
56632b27 JM |
6202 | if (*non_constant_p) |
6203 | break; | |
6204 | if (returns (jump_target) || breaks (jump_target)) | |
6205 | break; | |
6206 | } | |
ce36ba09 JM |
6207 | if (*jump_target && jump_target == &local_target) |
6208 | { | |
6209 | /* We aren't communicating the jump to our caller, so give up. We don't | |
6210 | need to support evaluation of jumps out of statement-exprs. */ | |
6211 | if (!ctx->quiet) | |
f9d0ca40 | 6212 | error_at (cp_expr_loc_or_input_loc (r), |
ce36ba09 JM |
6213 | "statement is not a constant expression"); |
6214 | *non_constant_p = true; | |
6215 | } | |
58611fb6 | 6216 | return r; |
56632b27 JM |
6217 | } |
6218 | ||
6219 | /* Evaluate a LOOP_EXPR for side-effects. Handles break and return | |
6220 | semantics; continue semantics are covered by cxx_eval_statement_list. */ | |
6221 | ||
6222 | static tree | |
6223 | cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t, | |
56632b27 JM |
6224 | bool *non_constant_p, bool *overflow_p, |
6225 | tree *jump_target) | |
6226 | { | |
39dce2b7 | 6227 | constexpr_ctx new_ctx = *ctx; |
8e007055 JJ |
6228 | tree local_target; |
6229 | if (!jump_target) | |
6230 | { | |
6231 | local_target = NULL_TREE; | |
6232 | jump_target = &local_target; | |
6233 | } | |
39dce2b7 | 6234 | |
43574e4f | 6235 | tree body, cond = NULL_TREE, expr = NULL_TREE; |
5ec2cd9f | 6236 | int count = 0; |
43574e4f JJ |
6237 | switch (TREE_CODE (t)) |
6238 | { | |
6239 | case LOOP_EXPR: | |
6240 | body = LOOP_EXPR_BODY (t); | |
6241 | break; | |
6242 | case DO_STMT: | |
6243 | body = DO_BODY (t); | |
6244 | cond = DO_COND (t); | |
6245 | break; | |
6246 | case WHILE_STMT: | |
6247 | body = WHILE_BODY (t); | |
6248 | cond = WHILE_COND (t); | |
6249 | count = -1; | |
6250 | break; | |
6251 | case FOR_STMT: | |
6252 | if (FOR_INIT_STMT (t)) | |
72f76540 | 6253 | cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard, |
43574e4f JJ |
6254 | non_constant_p, overflow_p, jump_target); |
6255 | if (*non_constant_p) | |
6256 | return NULL_TREE; | |
6257 | body = FOR_BODY (t); | |
6258 | cond = FOR_COND (t); | |
6259 | expr = FOR_EXPR (t); | |
6260 | count = -1; | |
6261 | break; | |
6262 | default: | |
6263 | gcc_unreachable (); | |
6264 | } | |
0ee28590 | 6265 | auto_vec<tree, 10> save_exprs; |
43574e4f | 6266 | new_ctx.save_exprs = &save_exprs; |
d259b234 | 6267 | do |
56632b27 | 6268 | { |
43574e4f JJ |
6269 | if (count != -1) |
6270 | { | |
6271 | if (body) | |
72f76540 | 6272 | cxx_eval_constant_expression (&new_ctx, body, vc_discard, |
43574e4f JJ |
6273 | non_constant_p, overflow_p, |
6274 | jump_target); | |
6275 | if (breaks (jump_target)) | |
6276 | { | |
6277 | *jump_target = NULL_TREE; | |
6278 | break; | |
6279 | } | |
39dce2b7 | 6280 | |
43574e4f JJ |
6281 | if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target)) |
6282 | *jump_target = NULL_TREE; | |
6283 | ||
6284 | if (expr) | |
72f76540 | 6285 | cxx_eval_constant_expression (&new_ctx, expr, vc_prvalue, |
43574e4f JJ |
6286 | non_constant_p, overflow_p, |
6287 | jump_target); | |
6288 | } | |
6289 | ||
6290 | if (cond) | |
6291 | { | |
6292 | tree res | |
72f76540 | 6293 | = cxx_eval_constant_expression (&new_ctx, cond, vc_prvalue, |
43574e4f JJ |
6294 | non_constant_p, overflow_p, |
6295 | jump_target); | |
6296 | if (res) | |
6297 | { | |
6298 | if (verify_constant (res, ctx->quiet, non_constant_p, | |
6299 | overflow_p)) | |
6300 | break; | |
6301 | if (integer_zerop (res)) | |
6302 | break; | |
6303 | } | |
6304 | else | |
6305 | gcc_assert (*jump_target); | |
6306 | } | |
39dce2b7 | 6307 | |
ee1de08d | 6308 | /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */ |
3f207ab3 | 6309 | for (tree save_expr : save_exprs) |
8e007055 | 6310 | ctx->global->values.remove (save_expr); |
0ee28590 | 6311 | save_exprs.truncate (0); |
43574e4f | 6312 | |
5ec2cd9f JM |
6313 | if (++count >= constexpr_loop_limit) |
6314 | { | |
6315 | if (!ctx->quiet) | |
f9d0ca40 | 6316 | error_at (cp_expr_loc_or_input_loc (t), |
84fa214d | 6317 | "%<constexpr%> loop iteration count exceeds limit of %d " |
a3f9f006 | 6318 | "(use %<-fconstexpr-loop-limit=%> to increase the limit)", |
5ec2cd9f JM |
6319 | constexpr_loop_limit); |
6320 | *non_constant_p = true; | |
6321 | break; | |
6322 | } | |
56632b27 | 6323 | } |
4b390698 JJ |
6324 | while (!returns (jump_target) |
6325 | && !breaks (jump_target) | |
43574e4f JJ |
6326 | && !continues (jump_target) |
6327 | && (!switches (jump_target) || count == 0) | |
4b390698 | 6328 | && !*non_constant_p); |
d259b234 | 6329 | |
ee1de08d | 6330 | /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */ |
3f207ab3 | 6331 | for (tree save_expr : save_exprs) |
8e007055 | 6332 | ctx->global->values.remove (save_expr); |
39dce2b7 | 6333 | |
56632b27 JM |
6334 | return NULL_TREE; |
6335 | } | |
6336 | ||
6337 | /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump | |
6338 | semantics. */ | |
6339 | ||
6340 | static tree | |
6341 | cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t, | |
56632b27 JM |
6342 | bool *non_constant_p, bool *overflow_p, |
6343 | tree *jump_target) | |
6344 | { | |
43574e4f JJ |
6345 | tree cond |
6346 | = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t); | |
72f76540 | 6347 | cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue, |
5a804683 | 6348 | non_constant_p, overflow_p); |
56632b27 JM |
6349 | VERIFY_CONSTANT (cond); |
6350 | *jump_target = cond; | |
6351 | ||
43574e4f JJ |
6352 | tree body |
6353 | = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t); | |
4b390698 JJ |
6354 | constexpr_ctx new_ctx = *ctx; |
6355 | constexpr_switch_state css = css_default_not_seen; | |
6356 | new_ctx.css_state = &css; | |
72f76540 | 6357 | cxx_eval_constant_expression (&new_ctx, body, vc_discard, |
4b390698 JJ |
6358 | non_constant_p, overflow_p, jump_target); |
6359 | if (switches (jump_target) && css == css_default_seen) | |
6360 | { | |
6361 | /* If the SWITCH_EXPR body has default: label, process it once again, | |
6362 | this time instructing label_matches to return true for default: | |
6363 | label on switches (jump_target). */ | |
6364 | css = css_default_processing; | |
72f76540 | 6365 | cxx_eval_constant_expression (&new_ctx, body, vc_discard, |
4b390698 JJ |
6366 | non_constant_p, overflow_p, jump_target); |
6367 | } | |
56632b27 JM |
6368 | if (breaks (jump_target) || switches (jump_target)) |
6369 | *jump_target = NULL_TREE; | |
6370 | return NULL_TREE; | |
6371 | } | |
6372 | ||
89262ec6 JM |
6373 | /* Find the object of TYPE under initialization in CTX. */ |
6374 | ||
6375 | static tree | |
72f76540 | 6376 | lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type) |
89262ec6 | 6377 | { |
fbd603c4 | 6378 | if (!ctx) |
89262ec6 JM |
6379 | return NULL_TREE; |
6380 | ||
49a86fce PP |
6381 | /* Prefer the outermost matching object, but don't cross |
6382 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */ | |
6383 | if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor)) | |
6384 | if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type)) | |
6385 | return outer_ob; | |
6386 | ||
89262ec6 JM |
6387 | /* We could use ctx->object unconditionally, but using ctx->ctor when we |
6388 | can is a minor optimization. */ | |
fbd603c4 | 6389 | if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type)) |
89262ec6 JM |
6390 | return ctx->ctor; |
6391 | ||
fbd603c4 JM |
6392 | if (!ctx->object) |
6393 | return NULL_TREE; | |
6394 | ||
89262ec6 JM |
6395 | /* Since an object cannot have a field of its own type, we can search outward |
6396 | from ctx->object to find the unique containing object of TYPE. */ | |
6397 | tree ob = ctx->object; | |
6398 | while (ob) | |
6399 | { | |
6400 | if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type)) | |
6401 | break; | |
6402 | if (handled_component_p (ob)) | |
6403 | ob = TREE_OPERAND (ob, 0); | |
6404 | else | |
6405 | ob = NULL_TREE; | |
6406 | } | |
6407 | ||
6408 | return ob; | |
6409 | } | |
6410 | ||
7c814975 MP |
6411 | /* Complain about an attempt to evaluate inline assembly. */ |
6412 | ||
6413 | static void | |
6414 | inline_asm_in_constexpr_error (location_t loc) | |
6415 | { | |
6416 | auto_diagnostic_group d; | |
6417 | error_at (loc, "inline assembly is not a constant expression"); | |
6418 | inform (loc, "only unevaluated inline assembly is allowed in a " | |
b04445d4 | 6419 | "%<constexpr%> function in C++20"); |
7c814975 MP |
6420 | } |
6421 | ||
76b75018 JM |
6422 | /* We're getting the constant value of DECL in a manifestly constant-evaluated |
6423 | context; maybe complain about that. */ | |
6424 | ||
6425 | static void | |
6426 | maybe_warn_about_constant_value (location_t loc, tree decl) | |
6427 | { | |
6428 | static bool explained = false; | |
6429 | if (cxx_dialect >= cxx17 | |
6430 | && warn_interference_size | |
00f34291 | 6431 | && !OPTION_SET_P (param_destruct_interfere_size) |
76b75018 JM |
6432 | && DECL_CONTEXT (decl) == std_node |
6433 | && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size") | |
6434 | && (LOCATION_FILE (input_location) != main_input_filename | |
6435 | || module_exporting_p ()) | |
6436 | && warning_at (loc, OPT_Winterference_size, "use of %qD", decl) | |
6437 | && !explained) | |
6438 | { | |
6439 | explained = true; | |
6440 | inform (loc, "its value can vary between compiler versions or " | |
6441 | "with different %<-mtune%> or %<-mcpu%> flags"); | |
6442 | inform (loc, "if this use is part of a public ABI, change it to " | |
6443 | "instead use a constant variable you define"); | |
6444 | inform (loc, "the default value for the current CPU tuning " | |
6445 | "is %d bytes", param_destruct_interfere_size); | |
6446 | inform (loc, "you can stabilize this value with %<--param " | |
6447 | "hardware_destructive_interference_size=%d%>, or disable " | |
6448 | "this warning with %<-Wno-interference-size%>", | |
6449 | param_destruct_interfere_size); | |
6450 | } | |
6451 | } | |
6452 | ||
0a0c2c3f JJ |
6453 | /* For element type ELT_TYPE, return the appropriate type of the heap object |
6454 | containing such element(s). COOKIE_SIZE is NULL or the size of cookie | |
6455 | in bytes. If COOKIE_SIZE is NULL, return array type | |
6456 | ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return | |
6457 | struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; } | |
6458 | where N is is computed such that the size of the struct fits into FULL_SIZE. | |
6459 | If ARG_SIZE is non-NULL, it is the first argument to the new operator. | |
6460 | It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE | |
6461 | will be also 0 and so it is not possible to determine the actual array | |
6462 | size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant | |
6463 | expression evaluation of subexpressions of ARG_SIZE. */ | |
6464 | ||
6465 | static tree | |
6466 | build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type, | |
6467 | tree cookie_size, tree full_size, tree arg_size, | |
6468 | bool *non_constant_p, bool *overflow_p) | |
6469 | { | |
6470 | gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size)); | |
6471 | gcc_assert (tree_fits_uhwi_p (full_size)); | |
6472 | unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0; | |
6473 | if (arg_size) | |
6474 | { | |
6475 | STRIP_NOPS (arg_size); | |
6476 | if (cookie_size) | |
6477 | { | |
6478 | if (TREE_CODE (arg_size) != PLUS_EXPR) | |
6479 | arg_size = NULL_TREE; | |
6480 | else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST | |
6481 | && tree_int_cst_equal (cookie_size, | |
6482 | TREE_OPERAND (arg_size, 0))) | |
6483 | { | |
6484 | arg_size = TREE_OPERAND (arg_size, 1); | |
6485 | STRIP_NOPS (arg_size); | |
6486 | } | |
6487 | else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST | |
6488 | && tree_int_cst_equal (cookie_size, | |
6489 | TREE_OPERAND (arg_size, 1))) | |
6490 | { | |
6491 | arg_size = TREE_OPERAND (arg_size, 0); | |
6492 | STRIP_NOPS (arg_size); | |
6493 | } | |
6494 | else | |
6495 | arg_size = NULL_TREE; | |
6496 | } | |
6497 | if (arg_size && TREE_CODE (arg_size) == MULT_EXPR) | |
6498 | { | |
6499 | tree op0 = TREE_OPERAND (arg_size, 0); | |
6500 | tree op1 = TREE_OPERAND (arg_size, 1); | |
6501 | if (integer_zerop (op0)) | |
6502 | arg_size | |
72f76540 JM |
6503 | = cxx_eval_constant_expression (ctx, op1, vc_prvalue, |
6504 | non_constant_p, overflow_p); | |
0a0c2c3f JJ |
6505 | else if (integer_zerop (op1)) |
6506 | arg_size | |
72f76540 JM |
6507 | = cxx_eval_constant_expression (ctx, op0, vc_prvalue, |
6508 | non_constant_p, overflow_p); | |
0a0c2c3f JJ |
6509 | else |
6510 | arg_size = NULL_TREE; | |
6511 | } | |
6512 | else | |
6513 | arg_size = NULL_TREE; | |
6514 | } | |
6515 | ||
6516 | unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size); | |
6517 | if (!arg_size) | |
6518 | { | |
6519 | unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type); | |
6520 | gcc_assert (fsz >= csz); | |
6521 | fsz -= csz; | |
6522 | if (esz) | |
6523 | fsz /= esz; | |
6524 | } | |
6525 | tree itype2 = build_index_type (size_int (fsz - 1)); | |
6526 | if (!cookie_size) | |
6527 | return build_cplus_array_type (elt_type, itype2); | |
6528 | return build_new_constexpr_heap_type (elt_type, cookie_size, itype2); | |
6529 | } | |
6530 | ||
2d76680f PC |
6531 | /* Attempt to reduce the expression T to a constant value. |
6532 | On failure, issue diagnostic and return error_mark_node. */ | |
6533 | /* FIXME unify with c_fully_fold */ | |
60813a46 | 6534 | /* FIXME overflow_p is too global */ |
2d76680f PC |
6535 | |
6536 | static tree | |
3e605b20 | 6537 | cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, |
72f76540 | 6538 | value_cat lval, |
56632b27 | 6539 | bool *non_constant_p, bool *overflow_p, |
3075affd | 6540 | tree *jump_target /* = NULL */) |
2d76680f | 6541 | { |
4b390698 JJ |
6542 | if (jump_target && *jump_target) |
6543 | { | |
6544 | /* If we are jumping, ignore all statements/expressions except those | |
6545 | that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */ | |
6546 | switch (TREE_CODE (t)) | |
6547 | { | |
6548 | case BIND_EXPR: | |
6549 | case STATEMENT_LIST: | |
6550 | case LOOP_EXPR: | |
6551 | case COND_EXPR: | |
43574e4f JJ |
6552 | case IF_STMT: |
6553 | case DO_STMT: | |
6554 | case WHILE_STMT: | |
6555 | case FOR_STMT: | |
4b390698 JJ |
6556 | break; |
6557 | case LABEL_EXPR: | |
6558 | case CASE_LABEL_EXPR: | |
6559 | if (label_matches (ctx, jump_target, t)) | |
6560 | /* Found it. */ | |
6561 | *jump_target = NULL_TREE; | |
6562 | return NULL_TREE; | |
6563 | default: | |
6564 | return NULL_TREE; | |
6565 | } | |
6566 | } | |
3075affd | 6567 | if (error_operand_p (t)) |
2d76680f PC |
6568 | { |
6569 | *non_constant_p = true; | |
6570 | return t; | |
6571 | } | |
7a649ef5 | 6572 | |
6821245b JM |
6573 | location_t loc = cp_expr_loc_or_input_loc (t); |
6574 | ||
7a649ef5 JM |
6575 | STRIP_ANY_LOCATION_WRAPPER (t); |
6576 | ||
2d76680f PC |
6577 | if (CONSTANT_CLASS_P (t)) |
6578 | { | |
61637db3 JJ |
6579 | if (TREE_OVERFLOW (t)) |
6580 | { | |
6581 | if (!ctx->quiet) | |
6582 | permerror (input_location, "overflow in constant expression"); | |
6583 | if (!flag_permissive || ctx->quiet) | |
6584 | *overflow_p = true; | |
6585 | } | |
8bada5cd MS |
6586 | |
6587 | if (TREE_CODE (t) == INTEGER_CST | |
9f613f06 | 6588 | && TYPE_PTR_P (TREE_TYPE (t)) |
f45610a4 JJ |
6589 | /* INTEGER_CST with pointer-to-method type is only used |
6590 | for a virtual method in a pointer to member function. | |
6591 | Don't reject those. */ | |
6592 | && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE | |
8bada5cd MS |
6593 | && !integer_zerop (t)) |
6594 | { | |
6595 | if (!ctx->quiet) | |
6596 | error ("value %qE of type %qT is not a constant expression", | |
6597 | t, TREE_TYPE (t)); | |
6598 | *non_constant_p = true; | |
6599 | } | |
6600 | ||
2d76680f PC |
6601 | return t; |
6602 | } | |
2d76680f | 6603 | |
a15ffa22 | 6604 | /* Avoid excessively long constexpr evaluations. */ |
8e007055 | 6605 | if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit) |
a15ffa22 JJ |
6606 | { |
6607 | if (!ctx->quiet) | |
6821245b | 6608 | error_at (loc, |
a15ffa22 | 6609 | "%<constexpr%> evaluation operation count exceeds limit of " |
a9c697b8 | 6610 | "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)", |
a15ffa22 | 6611 | constexpr_ops_limit); |
8e007055 | 6612 | ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT); |
a15ffa22 JJ |
6613 | *non_constant_p = true; |
6614 | return t; | |
6615 | } | |
6616 | ||
7a649ef5 JM |
6617 | constexpr_ctx new_ctx; |
6618 | tree r = t; | |
6619 | ||
8bada5cd MS |
6620 | tree_code tcode = TREE_CODE (t); |
6621 | switch (tcode) | |
2d76680f | 6622 | { |
60813a46 | 6623 | case RESULT_DECL: |
92a596e8 | 6624 | if (lval) |
60813a46 JM |
6625 | return t; |
6626 | /* We ask for an rvalue for the RESULT_DECL when indirecting | |
bf66b9b4 AH |
6627 | through an invisible reference, or in named return value |
6628 | optimization. */ | |
8e007055 | 6629 | if (tree *p = ctx->global->values.get (t)) |
1efb1dc2 MP |
6630 | return *p; |
6631 | else | |
6632 | { | |
6633 | if (!ctx->quiet) | |
6634 | error ("%qE is not a constant expression", t); | |
6635 | *non_constant_p = true; | |
6636 | } | |
6637 | break; | |
60813a46 | 6638 | |
2d76680f | 6639 | case VAR_DECL: |
70f40fea | 6640 | if (DECL_HAS_VALUE_EXPR_P (t)) |
c59fa7ea JM |
6641 | { |
6642 | if (is_normal_capture_proxy (t) | |
6643 | && current_function_decl == DECL_CONTEXT (t)) | |
6644 | { | |
6645 | /* Function parms aren't constexpr within the function | |
6646 | definition, so don't try to look at the closure. But if the | |
6647 | captured variable is constant, try to evaluate it directly. */ | |
6648 | r = DECL_CAPTURED_VARIABLE (t); | |
6649 | tree type = TREE_TYPE (t); | |
6650 | if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r))) | |
6651 | { | |
6652 | /* Adjust r to match the reference-ness of t. */ | |
6653 | if (TYPE_REF_P (type)) | |
6654 | r = build_address (r); | |
6655 | else | |
6656 | r = convert_from_reference (r); | |
6657 | } | |
6658 | } | |
6659 | else | |
6660 | r = DECL_VALUE_EXPR (t); | |
6661 | return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, | |
6662 | overflow_p); | |
6663 | } | |
191816a3 | 6664 | /* fall through */ |
7c83622c | 6665 | case CONST_DECL: |
e53b6e56 | 6666 | /* We used to not check lval for CONST_DECL, but darwin.cc uses |
7c83622c | 6667 | CONST_DECL for aggregate constants. */ |
92a596e8 | 6668 | if (lval) |
2d76680f | 6669 | return t; |
ef529765 JM |
6670 | else if (t == ctx->object) |
6671 | return ctx->ctor; | |
6672 | if (VAR_P (t)) | |
6673 | if (tree *p = ctx->global->values.get (t)) | |
6674 | if (*p != NULL_TREE) | |
6675 | { | |
6676 | r = *p; | |
6677 | break; | |
6678 | } | |
76b75018 JM |
6679 | if (ctx->manifestly_const_eval) |
6680 | maybe_warn_about_constant_value (loc, t); | |
f4fce183 | 6681 | if (COMPLETE_TYPE_P (TREE_TYPE (t)) |
dbcd32f8 | 6682 | && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
7dc2b4a2 JM |
6683 | { |
6684 | /* If the class is empty, we aren't actually loading anything. */ | |
6685 | r = build_constructor (TREE_TYPE (t), NULL); | |
6686 | TREE_CONSTANT (r) = true; | |
6687 | } | |
6688 | else if (ctx->strict) | |
8c00059c | 6689 | r = decl_really_constant_value (t, /*unshare_p=*/false); |
69eb4fde | 6690 | else |
8c00059c | 6691 | r = decl_constant_value (t, /*unshare_p=*/false); |
2d76680f PC |
6692 | if (TREE_CODE (r) == TARGET_EXPR |
6693 | && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR) | |
6694 | r = TARGET_EXPR_INITIAL (r); | |
2d76680f PC |
6695 | if (DECL_P (r)) |
6696 | { | |
2b3ab879 | 6697 | if (!ctx->quiet) |
6821245b | 6698 | non_const_var_error (loc, r); |
2d76680f PC |
6699 | *non_constant_p = true; |
6700 | } | |
6701 | break; | |
6702 | ||
96a95ac1 AO |
6703 | case DEBUG_BEGIN_STMT: |
6704 | /* ??? It might be nice to retain this information somehow, so | |
6705 | as to be able to step into a constexpr function call. */ | |
6706 | /* Fall through. */ | |
6707 | ||
2d76680f PC |
6708 | case FUNCTION_DECL: |
6709 | case TEMPLATE_DECL: | |
6710 | case LABEL_DECL: | |
56632b27 JM |
6711 | case LABEL_EXPR: |
6712 | case CASE_LABEL_EXPR: | |
4b390698 | 6713 | case PREDICT_EXPR: |
2d76680f PC |
6714 | return t; |
6715 | ||
6716 | case PARM_DECL: | |
9f613f06 | 6717 | if (lval && !TYPE_REF_P (TREE_TYPE (t))) |
60813a46 | 6718 | /* glvalue use. */; |
8e007055 | 6719 | else if (tree *p = ctx->global->values.get (r)) |
60813a46 | 6720 | r = *p; |
92a596e8 | 6721 | else if (lval) |
2d76680f | 6722 | /* Defer in case this is only used for its type. */; |
1c2f613f | 6723 | else if (COMPLETE_TYPE_P (TREE_TYPE (t)) |
dbcd32f8 | 6724 | && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
052beba4 JM |
6725 | { |
6726 | /* If the class is empty, we aren't actually loading anything. */ | |
6727 | r = build_constructor (TREE_TYPE (t), NULL); | |
6728 | TREE_CONSTANT (r) = true; | |
6729 | } | |
2d76680f PC |
6730 | else |
6731 | { | |
2b3ab879 | 6732 | if (!ctx->quiet) |
2d76680f PC |
6733 | error ("%qE is not a constant expression", t); |
6734 | *non_constant_p = true; | |
6735 | } | |
6736 | break; | |
6737 | ||
6738 | case CALL_EXPR: | |
6739 | case AGGR_INIT_EXPR: | |
92a596e8 | 6740 | r = cxx_eval_call_expression (ctx, t, lval, |
2d76680f PC |
6741 | non_constant_p, overflow_p); |
6742 | break; | |
6743 | ||
60813a46 JM |
6744 | case DECL_EXPR: |
6745 | { | |
6746 | r = DECL_EXPR_DECL (t); | |
43574e4f JJ |
6747 | if (TREE_CODE (r) == USING_DECL) |
6748 | { | |
6749 | r = void_node; | |
6750 | break; | |
6751 | } | |
8892d532 JJ |
6752 | |
6753 | if (VAR_P (r) | |
72124f48 JJ |
6754 | && (TREE_STATIC (r) |
6755 | || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r))) | |
8892d532 JJ |
6756 | /* Allow __FUNCTION__ etc. */ |
6757 | && !DECL_ARTIFICIAL (r)) | |
6758 | { | |
8892d532 JJ |
6759 | if (!ctx->quiet) |
6760 | { | |
6761 | if (CP_DECL_THREAD_LOCAL_P (r)) | |
72124f48 | 6762 | error_at (loc, "control passes through definition of %qD " |
8892d532 JJ |
6763 | "with thread storage duration", r); |
6764 | else | |
72124f48 | 6765 | error_at (loc, "control passes through definition of %qD " |
8892d532 JJ |
6766 | "with static storage duration", r); |
6767 | } | |
6768 | *non_constant_p = true; | |
6769 | break; | |
6770 | } | |
6771 | ||
60813a46 JM |
6772 | if (AGGREGATE_TYPE_P (TREE_TYPE (r)) |
6773 | || VECTOR_TYPE_P (TREE_TYPE (r))) | |
6774 | { | |
6775 | new_ctx = *ctx; | |
6776 | new_ctx.object = r; | |
6777 | new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL); | |
e8c48716 | 6778 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; |
8e007055 | 6779 | ctx->global->values.put (r, new_ctx.ctor); |
60813a46 JM |
6780 | ctx = &new_ctx; |
6781 | } | |
6782 | ||
6783 | if (tree init = DECL_INITIAL (r)) | |
6784 | { | |
72f76540 | 6785 | init = cxx_eval_constant_expression (ctx, init, vc_prvalue, |
5a804683 | 6786 | non_constant_p, overflow_p); |
7f0e23e9 | 6787 | /* Don't share a CONSTRUCTOR that might be changed. */ |
0146e25f | 6788 | init = unshare_constructor (init); |
04e1749c MP |
6789 | /* Remember that a constant object's constructor has already |
6790 | run. */ | |
6791 | if (CLASS_TYPE_P (TREE_TYPE (r)) | |
6792 | && CP_TYPE_CONST_P (TREE_TYPE (r))) | |
6793 | TREE_READONLY (init) = true; | |
8e007055 | 6794 | ctx->global->values.put (r, init); |
60813a46 JM |
6795 | } |
6796 | else if (ctx == &new_ctx) | |
6797 | /* We gave it a CONSTRUCTOR above. */; | |
6798 | else | |
8e007055 | 6799 | ctx->global->values.put (r, NULL_TREE); |
60813a46 JM |
6800 | } |
6801 | break; | |
6802 | ||
2d76680f | 6803 | case TARGET_EXPR: |
595f1b12 JM |
6804 | { |
6805 | tree type = TREE_TYPE (t); | |
6806 | ||
6807 | if (!literal_type_p (type)) | |
6808 | { | |
6809 | if (!ctx->quiet) | |
6810 | { | |
6811 | auto_diagnostic_group d; | |
6812 | error ("temporary of non-literal type %qT in a " | |
6813 | "constant expression", type); | |
6814 | explain_non_literal_class (type); | |
6815 | } | |
6816 | *non_constant_p = true; | |
6817 | break; | |
6818 | } | |
6819 | gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t)); | |
6820 | /* Avoid evaluating a TARGET_EXPR more than once. */ | |
6821 | tree slot = TARGET_EXPR_SLOT (t); | |
6822 | if (tree *p = ctx->global->values.get (slot)) | |
6823 | { | |
6824 | if (lval) | |
6825 | return slot; | |
6826 | r = *p; | |
6827 | break; | |
6828 | } | |
595f1b12 JM |
6829 | if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))) |
6830 | { | |
6831 | /* We're being expanded without an explicit target, so start | |
6832 | initializing a new object; expansion with an explicit target | |
6833 | strips the TARGET_EXPR before we get here. */ | |
6834 | new_ctx = *ctx; | |
49a86fce PP |
6835 | /* Link CTX to NEW_CTX so that lookup_placeholder can resolve |
6836 | any PLACEHOLDER_EXPR within the initializer that refers to the | |
6837 | former object under construction. */ | |
6838 | new_ctx.parent = ctx; | |
595f1b12 JM |
6839 | new_ctx.ctor = build_constructor (type, NULL); |
6840 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; | |
6841 | new_ctx.object = slot; | |
6842 | ctx->global->values.put (new_ctx.object, new_ctx.ctor); | |
6843 | ctx = &new_ctx; | |
6844 | } | |
72f76540 | 6845 | /* Pass vc_prvalue because this indicates |
595f1b12 | 6846 | initialization of a temporary. */ |
72f76540 | 6847 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_prvalue, |
595f1b12 JM |
6848 | non_constant_p, overflow_p); |
6849 | if (*non_constant_p) | |
ee1de08d | 6850 | break; |
595f1b12 JM |
6851 | /* Adjust the type of the result to the type of the temporary. */ |
6852 | r = adjust_temp_type (type, r); | |
6853 | if (TARGET_EXPR_CLEANUP (t) && !CLEANUP_EH_ONLY (t)) | |
6854 | ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t)); | |
6855 | r = unshare_constructor (r); | |
6856 | ctx->global->values.put (slot, r); | |
6857 | if (ctx->save_exprs) | |
6858 | ctx->save_exprs->safe_push (slot); | |
6859 | if (lval) | |
6860 | return slot; | |
6861 | } | |
2d76680f PC |
6862 | break; |
6863 | ||
60813a46 | 6864 | case INIT_EXPR: |
60813a46 | 6865 | case MODIFY_EXPR: |
4b390698 | 6866 | gcc_assert (jump_target == NULL || *jump_target == NULL_TREE); |
92a596e8 | 6867 | r = cxx_eval_store_expression (ctx, t, lval, |
60813a46 JM |
6868 | non_constant_p, overflow_p); |
6869 | break; | |
6870 | ||
2d76680f | 6871 | case SCOPE_REF: |
3e605b20 | 6872 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
92a596e8 | 6873 | lval, |
5a804683 | 6874 | non_constant_p, overflow_p); |
2d76680f PC |
6875 | break; |
6876 | ||
6877 | case RETURN_EXPR: | |
75e0295b MP |
6878 | if (TREE_OPERAND (t, 0) != NULL_TREE) |
6879 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), | |
6880 | lval, | |
6881 | non_constant_p, overflow_p); | |
43574e4f JJ |
6882 | /* FALLTHRU */ |
6883 | case BREAK_STMT: | |
6884 | case CONTINUE_STMT: | |
019e0ae8 MP |
6885 | if (jump_target) |
6886 | *jump_target = t; | |
6887 | else | |
6888 | { | |
6889 | /* Can happen with ({ return true; }) && false; passed to | |
6890 | maybe_constant_value. There is nothing to jump over in this | |
6891 | case, and the bug will be diagnosed later. */ | |
6892 | gcc_assert (ctx->quiet); | |
6893 | *non_constant_p = true; | |
6894 | } | |
56632b27 JM |
6895 | break; |
6896 | ||
cabaf50a JM |
6897 | case SAVE_EXPR: |
6898 | /* Avoid evaluating a SAVE_EXPR more than once. */ | |
8e007055 | 6899 | if (tree *p = ctx->global->values.get (t)) |
cabaf50a JM |
6900 | r = *p; |
6901 | else | |
6902 | { | |
72f76540 | 6903 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue, |
cabaf50a | 6904 | non_constant_p, overflow_p); |
0e0ffbfc JJ |
6905 | if (*non_constant_p) |
6906 | break; | |
8e007055 | 6907 | ctx->global->values.put (t, r); |
39dce2b7 | 6908 | if (ctx->save_exprs) |
0ee28590 | 6909 | ctx->save_exprs->safe_push (t); |
cabaf50a JM |
6910 | } |
6911 | break; | |
6912 | ||
2d76680f | 6913 | case TRY_CATCH_EXPR: |
ee1de08d JJ |
6914 | if (TREE_OPERAND (t, 0) == NULL_TREE) |
6915 | { | |
6916 | r = void_node; | |
6917 | break; | |
6918 | } | |
6919 | /* FALLTHRU */ | |
6920 | case NON_LVALUE_EXPR: | |
769430b2 | 6921 | case TRY_BLOCK: |
2d76680f | 6922 | case MUST_NOT_THROW_EXPR: |
60813a46 JM |
6923 | case EXPR_STMT: |
6924 | case EH_SPEC_BLOCK: | |
3e605b20 | 6925 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
92a596e8 | 6926 | lval, |
56632b27 JM |
6927 | non_constant_p, overflow_p, |
6928 | jump_target); | |
2d76680f PC |
6929 | break; |
6930 | ||
ee1de08d JJ |
6931 | case CLEANUP_POINT_EXPR: |
6932 | { | |
6933 | auto_vec<tree, 2> cleanups; | |
6934 | vec<tree> *prev_cleanups = ctx->global->cleanups; | |
6935 | ctx->global->cleanups = &cleanups; | |
6936 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), | |
6937 | lval, | |
6938 | non_constant_p, overflow_p, | |
6939 | jump_target); | |
6940 | ctx->global->cleanups = prev_cleanups; | |
6941 | unsigned int i; | |
6942 | tree cleanup; | |
6943 | /* Evaluate the cleanups. */ | |
6944 | FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup) | |
72f76540 | 6945 | cxx_eval_constant_expression (ctx, cleanup, vc_discard, |
fc531c2e | 6946 | non_constant_p, overflow_p); |
ee1de08d JJ |
6947 | } |
6948 | break; | |
6949 | ||
769430b2 JM |
6950 | case TRY_FINALLY_EXPR: |
6951 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, | |
6952 | non_constant_p, overflow_p, | |
6953 | jump_target); | |
6954 | if (!*non_constant_p) | |
6955 | /* Also evaluate the cleanup. */ | |
72f76540 | 6956 | cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard, |
fc531c2e | 6957 | non_constant_p, overflow_p); |
769430b2 JM |
6958 | break; |
6959 | ||
43574e4f | 6960 | case CLEANUP_STMT: |
fc531c2e JJ |
6961 | r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval, |
6962 | non_constant_p, overflow_p, | |
6963 | jump_target); | |
6964 | if (!CLEANUP_EH_ONLY (t) && !*non_constant_p) | |
6965 | { | |
6966 | iloc_sentinel ils (loc); | |
6967 | /* Also evaluate the cleanup. */ | |
72f76540 | 6968 | cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), vc_discard, |
fc531c2e JJ |
6969 | non_constant_p, overflow_p); |
6970 | } | |
43574e4f JJ |
6971 | break; |
6972 | ||
2d76680f PC |
6973 | /* These differ from cxx_eval_unary_expression in that this doesn't |
6974 | check for a constant operand or result; an address can be | |
6975 | constant without its operand being, and vice versa. */ | |
cda0a029 | 6976 | case MEM_REF: |
2d76680f | 6977 | case INDIRECT_REF: |
92a596e8 | 6978 | r = cxx_eval_indirect_ref (ctx, t, lval, |
2d76680f PC |
6979 | non_constant_p, overflow_p); |
6980 | break; | |
6981 | ||
6982 | case ADDR_EXPR: | |
6983 | { | |
6984 | tree oldop = TREE_OPERAND (t, 0); | |
72f76540 | 6985 | tree op = cxx_eval_constant_expression (ctx, oldop, vc_glvalue, |
5a804683 | 6986 | non_constant_p, overflow_p); |
2d76680f PC |
6987 | /* Don't VERIFY_CONSTANT here. */ |
6988 | if (*non_constant_p) | |
6989 | return t; | |
f2acb8ad | 6990 | gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR); |
2d76680f PC |
6991 | /* This function does more aggressive folding than fold itself. */ |
6992 | r = build_fold_addr_expr_with_type (op, TREE_TYPE (t)); | |
6993 | if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop) | |
7a649ef5 JM |
6994 | { |
6995 | ggc_free (r); | |
6996 | return t; | |
6997 | } | |
2d76680f PC |
6998 | break; |
6999 | } | |
7000 | ||
7001 | case REALPART_EXPR: | |
7002 | case IMAGPART_EXPR: | |
87713c6a JJ |
7003 | if (lval) |
7004 | { | |
7005 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, | |
7006 | non_constant_p, overflow_p); | |
7007 | if (r == error_mark_node) | |
7008 | ; | |
72f76540 | 7009 | else if (r == TREE_OPERAND (t, 0) || lval == vc_discard) |
87713c6a JJ |
7010 | r = t; |
7011 | else | |
7012 | r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r); | |
7013 | break; | |
7014 | } | |
7015 | /* FALLTHRU */ | |
2d76680f PC |
7016 | case CONJ_EXPR: |
7017 | case FIX_TRUNC_EXPR: | |
7018 | case FLOAT_EXPR: | |
7019 | case NEGATE_EXPR: | |
7020 | case ABS_EXPR: | |
e28fadbc | 7021 | case ABSU_EXPR: |
2d76680f PC |
7022 | case BIT_NOT_EXPR: |
7023 | case TRUTH_NOT_EXPR: | |
7024 | case FIXED_CONVERT_EXPR: | |
92a596e8 | 7025 | r = cxx_eval_unary_expression (ctx, t, lval, |
2d76680f PC |
7026 | non_constant_p, overflow_p); |
7027 | break; | |
7028 | ||
7029 | case SIZEOF_EXPR: | |
99e764a2 MP |
7030 | r = fold_sizeof_expr (t); |
7031 | /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR, | |
7032 | which could lead to an infinite recursion. */ | |
7033 | if (TREE_CODE (r) != SIZEOF_EXPR) | |
7034 | r = cxx_eval_constant_expression (ctx, r, lval, | |
7035 | non_constant_p, overflow_p, | |
7036 | jump_target); | |
7037 | else | |
7038 | { | |
7039 | *non_constant_p = true; | |
7040 | gcc_assert (ctx->quiet); | |
7041 | } | |
7042 | ||
2d76680f PC |
7043 | break; |
7044 | ||
7045 | case COMPOUND_EXPR: | |
7046 | { | |
7047 | /* check_return_expr sometimes wraps a TARGET_EXPR in a | |
7048 | COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR | |
7049 | introduced by build_call_a. */ | |
7050 | tree op0 = TREE_OPERAND (t, 0); | |
7051 | tree op1 = TREE_OPERAND (t, 1); | |
7052 | STRIP_NOPS (op1); | |
7053 | if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) | |
7054 | || TREE_CODE (op1) == EMPTY_CLASS_EXPR) | |
2b3ab879 | 7055 | r = cxx_eval_constant_expression (ctx, op0, |
92a596e8 | 7056 | lval, non_constant_p, overflow_p, |
56632b27 | 7057 | jump_target); |
2d76680f PC |
7058 | else |
7059 | { | |
7060 | /* Check that the LHS is constant and then discard it. */ | |
72f76540 JM |
7061 | cxx_eval_constant_expression (ctx, op0, vc_discard, |
7062 | non_constant_p, overflow_p, | |
56632b27 | 7063 | jump_target); |
06ec22b7 JM |
7064 | if (*non_constant_p) |
7065 | return t; | |
2d76680f | 7066 | op1 = TREE_OPERAND (t, 1); |
2b3ab879 | 7067 | r = cxx_eval_constant_expression (ctx, op1, |
92a596e8 | 7068 | lval, non_constant_p, overflow_p, |
56632b27 | 7069 | jump_target); |
2d76680f PC |
7070 | } |
7071 | } | |
7072 | break; | |
7073 | ||
7074 | case POINTER_PLUS_EXPR: | |
1af4ebf5 | 7075 | case POINTER_DIFF_EXPR: |
2d76680f PC |
7076 | case PLUS_EXPR: |
7077 | case MINUS_EXPR: | |
7078 | case MULT_EXPR: | |
7079 | case TRUNC_DIV_EXPR: | |
7080 | case CEIL_DIV_EXPR: | |
7081 | case FLOOR_DIV_EXPR: | |
7082 | case ROUND_DIV_EXPR: | |
7083 | case TRUNC_MOD_EXPR: | |
7084 | case CEIL_MOD_EXPR: | |
7085 | case ROUND_MOD_EXPR: | |
7086 | case RDIV_EXPR: | |
7087 | case EXACT_DIV_EXPR: | |
7088 | case MIN_EXPR: | |
7089 | case MAX_EXPR: | |
7090 | case LSHIFT_EXPR: | |
7091 | case RSHIFT_EXPR: | |
81fa6d73 JJ |
7092 | case LROTATE_EXPR: |
7093 | case RROTATE_EXPR: | |
2d76680f PC |
7094 | case BIT_IOR_EXPR: |
7095 | case BIT_XOR_EXPR: | |
7096 | case BIT_AND_EXPR: | |
7097 | case TRUTH_XOR_EXPR: | |
7098 | case LT_EXPR: | |
7099 | case LE_EXPR: | |
7100 | case GT_EXPR: | |
7101 | case GE_EXPR: | |
7102 | case EQ_EXPR: | |
7103 | case NE_EXPR: | |
b7689b96 | 7104 | case SPACESHIP_EXPR: |
2d76680f PC |
7105 | case UNORDERED_EXPR: |
7106 | case ORDERED_EXPR: | |
7107 | case UNLT_EXPR: | |
7108 | case UNLE_EXPR: | |
7109 | case UNGT_EXPR: | |
7110 | case UNGE_EXPR: | |
7111 | case UNEQ_EXPR: | |
7112 | case LTGT_EXPR: | |
7113 | case RANGE_EXPR: | |
7114 | case COMPLEX_EXPR: | |
92a596e8 | 7115 | r = cxx_eval_binary_expression (ctx, t, lval, |
2d76680f PC |
7116 | non_constant_p, overflow_p); |
7117 | break; | |
7118 | ||
7119 | /* fold can introduce non-IF versions of these; still treat them as | |
7120 | short-circuiting. */ | |
7121 | case TRUTH_AND_EXPR: | |
7122 | case TRUTH_ANDIF_EXPR: | |
3e605b20 | 7123 | r = cxx_eval_logical_expression (ctx, t, boolean_false_node, |
2d76680f | 7124 | boolean_true_node, |
2d76680f PC |
7125 | non_constant_p, overflow_p); |
7126 | break; | |
7127 | ||
7128 | case TRUTH_OR_EXPR: | |
7129 | case TRUTH_ORIF_EXPR: | |
3e605b20 | 7130 | r = cxx_eval_logical_expression (ctx, t, boolean_true_node, |
2d76680f | 7131 | boolean_false_node, |
2d76680f PC |
7132 | non_constant_p, overflow_p); |
7133 | break; | |
7134 | ||
7135 | case ARRAY_REF: | |
92a596e8 | 7136 | r = cxx_eval_array_reference (ctx, t, lval, |
2d76680f PC |
7137 | non_constant_p, overflow_p); |
7138 | break; | |
7139 | ||
7140 | case COMPONENT_REF: | |
7141 | if (is_overloaded_fn (t)) | |
7142 | { | |
7143 | /* We can only get here in checking mode via | |
7144 | build_non_dependent_expr, because any expression that | |
7145 | calls or takes the address of the function will have | |
7146 | pulled a FUNCTION_DECL out of the COMPONENT_REF. */ | |
2b3ab879 | 7147 | gcc_checking_assert (ctx->quiet || errorcount); |
2d76680f PC |
7148 | *non_constant_p = true; |
7149 | return t; | |
7150 | } | |
92a596e8 | 7151 | r = cxx_eval_component_reference (ctx, t, lval, |
2d76680f PC |
7152 | non_constant_p, overflow_p); |
7153 | break; | |
7154 | ||
7155 | case BIT_FIELD_REF: | |
92a596e8 | 7156 | r = cxx_eval_bit_field_ref (ctx, t, lval, |
2d76680f PC |
7157 | non_constant_p, overflow_p); |
7158 | break; | |
7159 | ||
7160 | case COND_EXPR: | |
43574e4f | 7161 | case IF_STMT: |
4b390698 JJ |
7162 | if (jump_target && *jump_target) |
7163 | { | |
e9fa2f6d | 7164 | tree orig_jump = *jump_target; |
43574e4f JJ |
7165 | tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1)) |
7166 | ? TREE_OPERAND (t, 1) : void_node); | |
4b390698 JJ |
7167 | /* When jumping to a label, the label might be either in the |
7168 | then or else blocks, so process then block first in skipping | |
7169 | mode first, and if we are still in the skipping mode at its end, | |
7170 | process the else block too. */ | |
43574e4f JJ |
7171 | r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, |
7172 | overflow_p, jump_target); | |
e9fa2f6d MP |
7173 | /* It's possible that we found the label in the then block. But |
7174 | it could have been followed by another jumping statement, e.g. | |
7175 | say we're looking for case 1: | |
7176 | if (cond) | |
7177 | { | |
7178 | // skipped statements | |
7179 | case 1:; // clears up *jump_target | |
7180 | return 1; // and sets it to a RETURN_EXPR | |
7181 | } | |
7182 | else { ... } | |
7183 | in which case we need not go looking to the else block. | |
7184 | (goto is not allowed in a constexpr function.) */ | |
7185 | if (*jump_target == orig_jump) | |
43574e4f JJ |
7186 | { |
7187 | arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2)) | |
7188 | ? TREE_OPERAND (t, 2) : void_node); | |
7189 | r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, | |
7190 | overflow_p, jump_target); | |
7191 | } | |
4b390698 JJ |
7192 | break; |
7193 | } | |
92a596e8 | 7194 | r = cxx_eval_conditional_expression (ctx, t, lval, |
56632b27 JM |
7195 | non_constant_p, overflow_p, |
7196 | jump_target); | |
2d76680f | 7197 | break; |
f370e36d JJ |
7198 | case VEC_COND_EXPR: |
7199 | r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p, | |
7200 | overflow_p); | |
7201 | break; | |
2d76680f PC |
7202 | |
7203 | case CONSTRUCTOR: | |
35d87f01 | 7204 | if (TREE_CONSTANT (t) && reduced_constant_expression_p (t)) |
2d63bc39 JM |
7205 | { |
7206 | /* Don't re-process a constant CONSTRUCTOR, but do fold it to | |
7207 | VECTOR_CST if applicable. */ | |
ac9ec198 | 7208 | verify_constructor_flags (t); |
2d63bc39 JM |
7209 | if (TREE_CONSTANT (t)) |
7210 | return fold (t); | |
7211 | } | |
92a596e8 | 7212 | r = cxx_eval_bare_aggregate (ctx, t, lval, |
2d76680f PC |
7213 | non_constant_p, overflow_p); |
7214 | break; | |
7215 | ||
7216 | case VEC_INIT_EXPR: | |
7217 | /* We can get this in a defaulted constructor for a class with a | |
7218 | non-static data member of array type. Either the initializer will | |
7219 | be NULL, meaning default-initialization, or it will be an lvalue | |
7220 | or xvalue of the same type, meaning direct-initialization from the | |
7221 | corresponding member. */ | |
92a596e8 | 7222 | r = cxx_eval_vec_init (ctx, t, lval, |
2d76680f PC |
7223 | non_constant_p, overflow_p); |
7224 | break; | |
7225 | ||
2d76680f | 7226 | case VEC_PERM_EXPR: |
92a596e8 | 7227 | r = cxx_eval_trinary_expression (ctx, t, lval, |
2d76680f PC |
7228 | non_constant_p, overflow_p); |
7229 | break; | |
efb7c510 MK |
7230 | |
7231 | case PAREN_EXPR: | |
7232 | gcc_assert (!REF_PARENTHESIZED_P (t)); | |
7233 | /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in | |
7234 | constant expressions since it's unaffected by -fassociative-math. */ | |
7235 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, | |
7236 | non_constant_p, overflow_p); | |
7237 | break; | |
2d76680f | 7238 | |
7d75ea04 JJ |
7239 | case NOP_EXPR: |
7240 | if (REINTERPRET_CAST_P (t)) | |
7241 | { | |
7242 | if (!ctx->quiet) | |
6821245b | 7243 | error_at (loc, |
a9c697b8 | 7244 | "%<reinterpret_cast%> is not a constant expression"); |
7d75ea04 JJ |
7245 | *non_constant_p = true; |
7246 | return t; | |
7247 | } | |
7248 | /* FALLTHROUGH. */ | |
2d76680f PC |
7249 | case CONVERT_EXPR: |
7250 | case VIEW_CONVERT_EXPR: | |
cda0a029 | 7251 | case UNARY_PLUS_EXPR: |
2d76680f PC |
7252 | { |
7253 | tree oldop = TREE_OPERAND (t, 0); | |
cda0a029 | 7254 | |
3e605b20 | 7255 | tree op = cxx_eval_constant_expression (ctx, oldop, |
92a596e8 | 7256 | lval, |
5a804683 | 7257 | non_constant_p, overflow_p); |
2d76680f PC |
7258 | if (*non_constant_p) |
7259 | return t; | |
dcdbc004 | 7260 | tree type = TREE_TYPE (t); |
02a8575c JM |
7261 | |
7262 | if (VOID_TYPE_P (type)) | |
7263 | return void_node; | |
7264 | ||
eeb54a14 JJ |
7265 | if (TREE_CODE (t) == CONVERT_EXPR |
7266 | && ARITHMETIC_TYPE_P (type) | |
82bb6673 JJ |
7267 | && INDIRECT_TYPE_P (TREE_TYPE (op)) |
7268 | && ctx->manifestly_const_eval) | |
eeb54a14 JJ |
7269 | { |
7270 | if (!ctx->quiet) | |
7271 | error_at (loc, | |
7272 | "conversion from pointer type %qT to arithmetic type " | |
7273 | "%qT in a constant expression", TREE_TYPE (op), type); | |
7274 | *non_constant_p = true; | |
7275 | return t; | |
7276 | } | |
7277 | ||
2ffc2645 MP |
7278 | /* [expr.const]: a conversion from type cv void* to a pointer-to-object |
7279 | type cannot be part of a core constant expression as a resolution to | |
7280 | DR 1312. */ | |
6252e35c | 7281 | if (TYPE_PTROB_P (type) |
2ffc2645 MP |
7282 | && TYPE_PTR_P (TREE_TYPE (op)) |
7283 | && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op))) | |
7284 | /* Inside a call to std::construct_at or to | |
7285 | std::allocator<T>::{,de}allocate, we permit casting from void* | |
7286 | because that is compiler-generated code. */ | |
7287 | && !is_std_construct_at (ctx->call) | |
7288 | && !is_std_allocator_allocate (ctx->call)) | |
7289 | { | |
7290 | /* Likewise, don't error when casting from void* when OP is | |
7291 | &heap uninit and similar. */ | |
7292 | tree sop = tree_strip_nop_conversions (op); | |
7293 | if (TREE_CODE (sop) == ADDR_EXPR | |
7294 | && VAR_P (TREE_OPERAND (sop, 0)) | |
7295 | && DECL_ARTIFICIAL (TREE_OPERAND (sop, 0))) | |
7296 | /* OK */; | |
7297 | else | |
7298 | { | |
7299 | if (!ctx->quiet) | |
7300 | error_at (loc, "cast from %qT is not allowed", | |
7301 | TREE_TYPE (op)); | |
7302 | *non_constant_p = true; | |
7303 | return t; | |
7304 | } | |
7305 | } | |
7306 | ||
8e007055 | 7307 | if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type)) |
dcdbc004 | 7308 | op = cplus_expand_constant (op); |
7d75ea04 | 7309 | |
05bf54c3 MP |
7310 | if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR) |
7311 | { | |
7d75ea04 JJ |
7312 | if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op)) |
7313 | && !can_convert_qual (type, op)) | |
7314 | op = cplus_expand_constant (op); | |
7315 | return cp_fold_convert (type, op); | |
05bf54c3 | 7316 | } |
8bada5cd | 7317 | |
71a93b08 | 7318 | if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST) |
2d76680f | 7319 | { |
8bada5cd MS |
7320 | if (integer_zerop (op)) |
7321 | { | |
9f613f06 | 7322 | if (TYPE_REF_P (type)) |
8bada5cd MS |
7323 | { |
7324 | if (!ctx->quiet) | |
2ffc2645 | 7325 | error_at (loc, "dereferencing a null pointer"); |
8bada5cd MS |
7326 | *non_constant_p = true; |
7327 | return t; | |
7328 | } | |
8bada5cd MS |
7329 | } |
7330 | else | |
7331 | { | |
7332 | /* This detects for example: | |
7333 | reinterpret_cast<void*>(sizeof 0) | |
7334 | */ | |
7335 | if (!ctx->quiet) | |
6821245b | 7336 | error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not " |
64d6d399 | 7337 | "a constant expression", |
8bada5cd MS |
7338 | type, op); |
7339 | *non_constant_p = true; | |
7340 | return t; | |
7341 | } | |
2d76680f | 7342 | } |
7d75ea04 | 7343 | |
8e007055 JJ |
7344 | if (INDIRECT_TYPE_P (type) |
7345 | && TREE_CODE (op) == NOP_EXPR | |
7346 | && TREE_TYPE (op) == ptr_type_node | |
7347 | && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR | |
7348 | && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0)) | |
815baade JJ |
7349 | && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), |
7350 | 0)) == heap_uninit_identifier | |
7351 | || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), | |
7352 | 0)) == heap_vec_uninit_identifier)) | |
8e007055 JJ |
7353 | { |
7354 | tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0); | |
7355 | tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var)); | |
7356 | tree elt_type = TREE_TYPE (type); | |
7357 | tree cookie_size = NULL_TREE; | |
0a0c2c3f | 7358 | tree arg_size = NULL_TREE; |
8e007055 JJ |
7359 | if (TREE_CODE (elt_type) == RECORD_TYPE |
7360 | && TYPE_NAME (elt_type) == heap_identifier) | |
7361 | { | |
7362 | tree fld1 = TYPE_FIELDS (elt_type); | |
7363 | tree fld2 = DECL_CHAIN (fld1); | |
7364 | elt_type = TREE_TYPE (TREE_TYPE (fld2)); | |
7365 | cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1)); | |
7366 | } | |
815baade JJ |
7367 | DECL_NAME (var) |
7368 | = (DECL_NAME (var) == heap_uninit_identifier | |
7369 | ? heap_identifier : heap_vec_identifier); | |
0a0c2c3f JJ |
7370 | /* For zero sized elt_type, try to recover how many outer_nelts |
7371 | it should have. */ | |
7372 | if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size) | |
7373 | : integer_zerop (var_size)) | |
7374 | && !int_size_in_bytes (elt_type) | |
7375 | && TREE_CODE (oldop) == CALL_EXPR | |
7376 | && call_expr_nargs (oldop) >= 1) | |
7377 | if (tree fun = get_function_named_in_call (oldop)) | |
7378 | if (cxx_replaceable_global_alloc_fn (fun) | |
7379 | && IDENTIFIER_NEW_OP_P (DECL_NAME (fun))) | |
7380 | arg_size = CALL_EXPR_ARG (oldop, 0); | |
8e007055 | 7381 | TREE_TYPE (var) |
0a0c2c3f JJ |
7382 | = build_new_constexpr_heap_type (ctx, elt_type, cookie_size, |
7383 | var_size, arg_size, | |
7384 | non_constant_p, overflow_p); | |
8e007055 JJ |
7385 | TREE_TYPE (TREE_OPERAND (op, 0)) |
7386 | = build_pointer_type (TREE_TYPE (var)); | |
7387 | } | |
7388 | ||
cda0a029 | 7389 | if (op == oldop && tcode != UNARY_PLUS_EXPR) |
2d76680f PC |
7390 | /* We didn't fold at the top so we could check for ptr-int |
7391 | conversion. */ | |
7392 | return fold (t); | |
7d75ea04 | 7393 | |
02a8575c JM |
7394 | tree sop; |
7395 | ||
e4511ca2 JM |
7396 | /* Handle an array's bounds having been deduced after we built |
7397 | the wrapping expression. */ | |
7398 | if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op))) | |
7399 | r = op; | |
02a8575c JM |
7400 | else if (sop = tree_strip_nop_conversions (op), |
7401 | sop != op && (same_type_ignoring_tlq_and_bounds_p | |
7402 | (type, TREE_TYPE (sop)))) | |
7403 | r = sop; | |
e4511ca2 | 7404 | else if (tcode == UNARY_PLUS_EXPR) |
cda0a029 JM |
7405 | r = fold_convert (TREE_TYPE (t), op); |
7406 | else | |
7407 | r = fold_build1 (tcode, type, op); | |
7d75ea04 | 7408 | |
2d76680f PC |
7409 | /* Conversion of an out-of-range value has implementation-defined |
7410 | behavior; the language considers it different from arithmetic | |
7411 | overflow, which is undefined. */ | |
7412 | if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op)) | |
7413 | TREE_OVERFLOW (r) = false; | |
7414 | } | |
7415 | break; | |
7416 | ||
7417 | case EMPTY_CLASS_EXPR: | |
46fdced6 PP |
7418 | /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering |
7419 | it to an appropriate CONSTRUCTOR. */ | |
7420 | return build_constructor (TREE_TYPE (t), NULL); | |
2d76680f | 7421 | |
60813a46 | 7422 | case STATEMENT_LIST: |
56632b27 JM |
7423 | new_ctx = *ctx; |
7424 | new_ctx.ctor = new_ctx.object = NULL_TREE; | |
2b3ab879 | 7425 | return cxx_eval_statement_list (&new_ctx, t, |
56632b27 | 7426 | non_constant_p, overflow_p, jump_target); |
60813a46 JM |
7427 | |
7428 | case BIND_EXPR: | |
7429 | return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t), | |
92a596e8 | 7430 | lval, |
56632b27 JM |
7431 | non_constant_p, overflow_p, |
7432 | jump_target); | |
60813a46 | 7433 | |
2d76680f PC |
7434 | case PREINCREMENT_EXPR: |
7435 | case POSTINCREMENT_EXPR: | |
7436 | case PREDECREMENT_EXPR: | |
7437 | case POSTDECREMENT_EXPR: | |
2b3ab879 | 7438 | return cxx_eval_increment_expression (ctx, t, |
92a596e8 | 7439 | lval, non_constant_p, overflow_p); |
60813a46 JM |
7440 | |
7441 | case LAMBDA_EXPR: | |
2d76680f PC |
7442 | case NEW_EXPR: |
7443 | case VEC_NEW_EXPR: | |
7444 | case DELETE_EXPR: | |
7445 | case VEC_DELETE_EXPR: | |
7446 | case THROW_EXPR: | |
2d76680f PC |
7447 | case MODOP_EXPR: |
7448 | /* GCC internal stuff. */ | |
7449 | case VA_ARG_EXPR: | |
2d76680f PC |
7450 | case NON_DEPENDENT_EXPR: |
7451 | case BASELINK: | |
2d76680f | 7452 | case OFFSET_REF: |
2b3ab879 | 7453 | if (!ctx->quiet) |
6821245b | 7454 | error_at (loc, "expression %qE is not a constant expression", t); |
2d76680f PC |
7455 | *non_constant_p = true; |
7456 | break; | |
7457 | ||
bf8d8309 | 7458 | case OBJ_TYPE_REF: |
7ece3bd8 JM |
7459 | /* Virtual function lookup. We don't need to do anything fancy. */ |
7460 | return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t), | |
7461 | lval, non_constant_p, overflow_p); | |
bf8d8309 | 7462 | |
3e605b20 | 7463 | case PLACEHOLDER_EXPR: |
89262ec6 JM |
7464 | /* Use of the value or address of the current object. */ |
7465 | if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t))) | |
b599bf9d PP |
7466 | { |
7467 | if (TREE_CODE (ctor) == CONSTRUCTOR) | |
7468 | return ctor; | |
7469 | else | |
7470 | return cxx_eval_constant_expression (ctx, ctor, lval, | |
7471 | non_constant_p, overflow_p); | |
7472 | } | |
89262ec6 JM |
7473 | /* A placeholder without a referent. We can get here when |
7474 | checking whether NSDMIs are noexcept, or in massage_init_elt; | |
7475 | just say it's non-constant for now. */ | |
7476 | gcc_assert (ctx->quiet); | |
7477 | *non_constant_p = true; | |
3e605b20 JM |
7478 | break; |
7479 | ||
06ec22b7 JM |
7480 | case EXIT_EXPR: |
7481 | { | |
7482 | tree cond = TREE_OPERAND (t, 0); | |
72f76540 | 7483 | cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue, |
06ec22b7 JM |
7484 | non_constant_p, overflow_p); |
7485 | VERIFY_CONSTANT (cond); | |
7486 | if (integer_nonzerop (cond)) | |
7487 | *jump_target = t; | |
7488 | } | |
7489 | break; | |
7490 | ||
60813a46 | 7491 | case GOTO_EXPR: |
8892d532 | 7492 | if (breaks (&TREE_OPERAND (t, 0)) |
9c6a4bee | 7493 | || continues (&TREE_OPERAND (t, 0))) |
8892d532 JJ |
7494 | *jump_target = TREE_OPERAND (t, 0); |
7495 | else | |
7496 | { | |
7497 | gcc_assert (cxx_dialect >= cxx23); | |
7498 | if (!ctx->quiet) | |
7499 | error_at (loc, "%<goto%> is not a constant expression"); | |
7500 | *non_constant_p = true; | |
7501 | } | |
56632b27 JM |
7502 | break; |
7503 | ||
60813a46 | 7504 | case LOOP_EXPR: |
43574e4f JJ |
7505 | case DO_STMT: |
7506 | case WHILE_STMT: | |
7507 | case FOR_STMT: | |
2b3ab879 | 7508 | cxx_eval_loop_expr (ctx, t, |
56632b27 JM |
7509 | non_constant_p, overflow_p, jump_target); |
7510 | break; | |
7511 | ||
60813a46 | 7512 | case SWITCH_EXPR: |
43574e4f | 7513 | case SWITCH_STMT: |
2b3ab879 | 7514 | cxx_eval_switch_expr (ctx, t, |
56632b27 | 7515 | non_constant_p, overflow_p, jump_target); |
60813a46 JM |
7516 | break; |
7517 | ||
971e17ff AS |
7518 | case REQUIRES_EXPR: |
7519 | /* It's possible to get a requires-expression in a constant | |
7520 | expression. For example: | |
7521 | ||
7522 | template<typename T> concept bool C() { | |
7523 | return requires (T t) { t; }; | |
7524 | } | |
7525 | ||
7526 | template<typename T> requires !C<T>() void f(T); | |
7527 | ||
7528 | Normalization leaves f with the associated constraint | |
7529 | '!requires (T t) { ... }' which is not transformed into | |
7530 | a constraint. */ | |
7531 | if (!processing_template_decl) | |
662ef5b5 | 7532 | return evaluate_requires_expr (t); |
971e17ff AS |
7533 | else |
7534 | *non_constant_p = true; | |
7535 | return t; | |
7536 | ||
98e09245 | 7537 | case ANNOTATE_EXPR: |
98e09245 JJ |
7538 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
7539 | lval, | |
7540 | non_constant_p, overflow_p, | |
7541 | jump_target); | |
7542 | break; | |
7543 | ||
3664e317 JM |
7544 | case USING_STMT: |
7545 | r = void_node; | |
7546 | break; | |
7547 | ||
cb57504a JM |
7548 | case TEMPLATE_ID_EXPR: |
7549 | { | |
7550 | /* We can evaluate template-id that refers to a concept only if | |
7551 | the template arguments are non-dependent. */ | |
861d4af8 AS |
7552 | tree id = unpack_concept_check (t); |
7553 | tree tmpl = TREE_OPERAND (id, 0); | |
7554 | if (!concept_definition_p (tmpl)) | |
cb57504a JM |
7555 | internal_error ("unexpected template-id %qE", t); |
7556 | ||
861d4af8 AS |
7557 | if (function_concept_p (tmpl)) |
7558 | { | |
7559 | if (!ctx->quiet) | |
7560 | error_at (cp_expr_loc_or_input_loc (t), | |
7561 | "function concept must be called"); | |
7562 | r = error_mark_node; | |
7563 | break; | |
7564 | } | |
7565 | ||
9329344a | 7566 | if (!value_dependent_expression_p (t) |
6d1556ec | 7567 | && !uid_sensitive_constexpr_evaluation_p ()) |
662ef5b5 | 7568 | r = evaluate_concept_check (t); |
cb57504a JM |
7569 | else |
7570 | *non_constant_p = true; | |
861d4af8 AS |
7571 | |
7572 | break; | |
cb57504a JM |
7573 | } |
7574 | ||
529bc410 MP |
7575 | case ASM_EXPR: |
7576 | if (!ctx->quiet) | |
6821245b | 7577 | inline_asm_in_constexpr_error (loc); |
529bc410 MP |
7578 | *non_constant_p = true; |
7579 | return t; | |
7580 | ||
896048cf | 7581 | case BIT_CAST_EXPR: |
606f2af1 JJ |
7582 | if (lval) |
7583 | { | |
7584 | if (!ctx->quiet) | |
7585 | error_at (EXPR_LOCATION (t), | |
7586 | "address of a call to %qs is not a constant expression", | |
7587 | "__builtin_bit_cast"); | |
7588 | *non_constant_p = true; | |
7589 | return t; | |
7590 | } | |
896048cf JJ |
7591 | r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p); |
7592 | break; | |
7593 | ||
2d76680f | 7594 | default: |
c6e7c499 JM |
7595 | if (STATEMENT_CODE_P (TREE_CODE (t))) |
7596 | { | |
7597 | /* This function doesn't know how to deal with pre-genericize | |
7598 | statements; this can only happen with statement-expressions, | |
7599 | so for now just fail. */ | |
7600 | if (!ctx->quiet) | |
7601 | error_at (EXPR_LOCATION (t), | |
64d6d399 | 7602 | "statement is not a constant expression"); |
c6e7c499 JM |
7603 | } |
7604 | else | |
7605 | internal_error ("unexpected expression %qE of kind %s", t, | |
7606 | get_tree_code_name (TREE_CODE (t))); | |
2d76680f PC |
7607 | *non_constant_p = true; |
7608 | break; | |
7609 | } | |
7610 | ||
7611 | if (r == error_mark_node) | |
7612 | *non_constant_p = true; | |
7613 | ||
7614 | if (*non_constant_p) | |
7615 | return t; | |
7616 | else | |
7617 | return r; | |
7618 | } | |
7619 | ||
e079dced JM |
7620 | /* P0859: A function is needed for constant evaluation if it is a constexpr |
7621 | function that is named by an expression ([basic.def.odr]) that is | |
7622 | potentially constant evaluated. | |
7623 | ||
7624 | So we need to instantiate any constexpr functions mentioned by the | |
7625 | expression even if the definition isn't needed for evaluating the | |
7626 | expression. */ | |
7627 | ||
7628 | static tree | |
7629 | instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/) | |
7630 | { | |
7631 | if (TREE_CODE (*tp) == FUNCTION_DECL | |
7632 | && DECL_DECLARED_CONSTEXPR_P (*tp) | |
7633 | && !DECL_INITIAL (*tp) | |
4fdda3ce | 7634 | && !trivial_fn_p (*tp) |
f65a3299 PP |
7635 | && DECL_TEMPLOID_INSTANTIATION (*tp) |
7636 | && !uid_sensitive_constexpr_evaluation_p ()) | |
e079dced JM |
7637 | { |
7638 | ++function_depth; | |
7639 | instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false); | |
7640 | --function_depth; | |
7641 | } | |
7642 | else if (TREE_CODE (*tp) == CALL_EXPR | |
7643 | || TREE_CODE (*tp) == AGGR_INIT_EXPR) | |
7644 | { | |
7645 | if (EXPR_HAS_LOCATION (*tp)) | |
7646 | input_location = EXPR_LOCATION (*tp); | |
7647 | } | |
7648 | ||
7649 | if (!EXPR_P (*tp)) | |
7650 | *walk_subtrees = 0; | |
7651 | ||
7652 | return NULL_TREE; | |
7653 | } | |
8e007055 | 7654 | |
e079dced JM |
7655 | static void |
7656 | instantiate_constexpr_fns (tree t) | |
7657 | { | |
7658 | location_t loc = input_location; | |
7659 | cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL); | |
7660 | input_location = loc; | |
7661 | } | |
7662 | ||
8e007055 JJ |
7663 | /* Look for heap variables in the expression *TP. */ |
7664 | ||
7665 | static tree | |
7666 | find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/) | |
7667 | { | |
7668 | if (VAR_P (*tp) | |
7669 | && (DECL_NAME (*tp) == heap_uninit_identifier | |
7670 | || DECL_NAME (*tp) == heap_identifier | |
815baade JJ |
7671 | || DECL_NAME (*tp) == heap_vec_uninit_identifier |
7672 | || DECL_NAME (*tp) == heap_vec_identifier | |
8e007055 JJ |
7673 | || DECL_NAME (*tp) == heap_deleted_identifier)) |
7674 | return *tp; | |
7675 | ||
7676 | if (TYPE_P (*tp)) | |
7677 | *walk_subtrees = 0; | |
7678 | return NULL_TREE; | |
7679 | } | |
7680 | ||
f968ef9b JJ |
7681 | /* Find immediate function decls in *TP if any. */ |
7682 | ||
7683 | static tree | |
7684 | find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/) | |
7685 | { | |
7686 | if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp)) | |
7687 | return *tp; | |
4b2fda8b JJ |
7688 | if (TREE_CODE (*tp) == PTRMEM_CST |
7689 | && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL | |
7690 | && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp))) | |
7691 | return PTRMEM_CST_MEMBER (*tp); | |
f968ef9b JJ |
7692 | return NULL_TREE; |
7693 | } | |
7694 | ||
ab71d3fe PP |
7695 | /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant |
7696 | expression. Return a version of T that has TREE_CONSTANT cleared. */ | |
7697 | ||
7698 | static tree | |
7699 | mark_non_constant (tree t) | |
7700 | { | |
7701 | gcc_checking_assert (TREE_CONSTANT (t)); | |
7702 | ||
7703 | /* This isn't actually constant, so unset TREE_CONSTANT. | |
7704 | Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires | |
7705 | it to be set if it is invariant address, even when it is not | |
7706 | a valid C++ constant expression. Wrap it with a NOP_EXPR | |
7707 | instead. */ | |
7708 | if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR) | |
7709 | t = copy_node (t); | |
7710 | else if (TREE_CODE (t) == CONSTRUCTOR) | |
7711 | t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t); | |
7712 | else | |
7713 | t = build_nop (TREE_TYPE (t), t); | |
7714 | TREE_CONSTANT (t) = false; | |
7715 | return t; | |
7716 | } | |
7717 | ||
e4082611 | 7718 | /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression. |
d4accef3 JM |
7719 | STRICT has the same sense as for constant_value_1: true if we only allow |
7720 | conforming C++ constant expressions, or false if we want a constant value | |
7721 | even if it doesn't conform. | |
13de99bc | 7722 | MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as |
8e007055 JJ |
7723 | per P0595 even when ALLOW_NON_CONSTANT is true. |
7724 | CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable. | |
7725 | OBJECT must be non-NULL in that case. */ | |
e4082611 | 7726 | |
2d76680f | 7727 | static tree |
3e605b20 | 7728 | cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant, |
e4082611 | 7729 | bool strict = true, |
13de99bc | 7730 | bool manifestly_const_eval = false, |
8e007055 | 7731 | bool constexpr_dtor = false, |
f65a3299 | 7732 | tree object = NULL_TREE) |
2d76680f | 7733 | { |
8108ea30 JM |
7734 | auto_timevar time (TV_CONSTEXPR); |
7735 | ||
2d76680f PC |
7736 | bool non_constant_p = false; |
7737 | bool overflow_p = false; | |
39dce2b7 | 7738 | |
d419e176 MP |
7739 | if (BRACE_ENCLOSED_INITIALIZER_P (t)) |
7740 | { | |
7741 | gcc_checking_assert (allow_non_constant); | |
7742 | return t; | |
7743 | } | |
7744 | ||
8e007055 | 7745 | constexpr_global_ctx global_ctx; |
49a86fce | 7746 | constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL, |
8e007055 | 7747 | allow_non_constant, strict, |
f65a3299 | 7748 | manifestly_const_eval || !allow_non_constant }; |
39dce2b7 | 7749 | |
6641d6d3 JJ |
7750 | /* Turn off -frounding-math for manifestly constant evaluation. */ |
7751 | warning_sentinel rm (flag_rounding_math, ctx.manifestly_const_eval); | |
3e605b20 | 7752 | tree type = initialized_type (t); |
3e605b20 | 7753 | tree r = t; |
f968ef9b | 7754 | bool is_consteval = false; |
4d0c18c6 | 7755 | if (VOID_TYPE_P (type)) |
8e007055 JJ |
7756 | { |
7757 | if (constexpr_dtor) | |
7758 | /* Used for destructors of array elements. */ | |
7759 | type = TREE_TYPE (object); | |
7760 | else | |
f968ef9b | 7761 | { |
b04445d4 | 7762 | if (cxx_dialect < cxx20) |
f968ef9b JJ |
7763 | return t; |
7764 | if (TREE_CODE (t) != CALL_EXPR && TREE_CODE (t) != AGGR_INIT_EXPR) | |
7765 | return t; | |
7766 | /* Calls to immediate functions returning void need to be | |
7767 | evaluated. */ | |
7768 | tree fndecl = cp_get_callee_fndecl_nofold (t); | |
7769 | if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl)) | |
7770 | return t; | |
7771 | else | |
7772 | is_consteval = true; | |
7773 | } | |
7774 | } | |
b04445d4 | 7775 | else if (cxx_dialect >= cxx20 |
f968ef9b JJ |
7776 | && (TREE_CODE (t) == CALL_EXPR |
7777 | || TREE_CODE (t) == AGGR_INIT_EXPR | |
7778 | || TREE_CODE (t) == TARGET_EXPR)) | |
7779 | { | |
861d4af8 AS |
7780 | /* For non-concept checks, determine if it is consteval. */ |
7781 | if (!concept_check_p (t)) | |
7782 | { | |
7783 | tree x = t; | |
7784 | if (TREE_CODE (x) == TARGET_EXPR) | |
7785 | x = TARGET_EXPR_INITIAL (x); | |
7786 | tree fndecl = cp_get_callee_fndecl_nofold (x); | |
7787 | if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl)) | |
7788 | is_consteval = true; | |
7789 | } | |
8e007055 | 7790 | } |
3e605b20 JM |
7791 | if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)) |
7792 | { | |
7793 | /* In C++14 an NSDMI can participate in aggregate initialization, | |
7794 | and can refer to the address of the object being initialized, so | |
7795 | we need to pass in the relevant VAR_DECL if we want to do the | |
7796 | evaluation in a single pass. The evaluation will dynamically | |
7797 | update ctx.values for the VAR_DECL. We use the same strategy | |
7798 | for C++11 constexpr constructors that refer to the object being | |
7799 | initialized. */ | |
8e007055 JJ |
7800 | if (constexpr_dtor) |
7801 | { | |
7802 | gcc_assert (object && VAR_P (object)); | |
7803 | gcc_assert (DECL_DECLARED_CONSTEXPR_P (object)); | |
7804 | gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object)); | |
fce6467b JJ |
7805 | if (error_operand_p (DECL_INITIAL (object))) |
7806 | return t; | |
8e007055 JJ |
7807 | ctx.ctor = unshare_expr (DECL_INITIAL (object)); |
7808 | TREE_READONLY (ctx.ctor) = false; | |
7809 | /* Temporarily force decl_really_constant_value to return false | |
7810 | for it, we want to use ctx.ctor for the current value instead. */ | |
7811 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false; | |
7812 | } | |
7813 | else | |
7814 | { | |
7815 | ctx.ctor = build_constructor (type, NULL); | |
7816 | CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true; | |
7817 | } | |
60813a46 JM |
7818 | if (!object) |
7819 | { | |
7820 | if (TREE_CODE (t) == TARGET_EXPR) | |
7821 | object = TARGET_EXPR_SLOT (t); | |
7822 | else if (TREE_CODE (t) == AGGR_INIT_EXPR) | |
7823 | object = AGGR_INIT_EXPR_SLOT (t); | |
7824 | } | |
3e605b20 JM |
7825 | ctx.object = object; |
7826 | if (object) | |
7827 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
7828 | (type, TREE_TYPE (object))); | |
7829 | if (object && DECL_P (object)) | |
8e007055 | 7830 | global_ctx.values.put (object, ctx.ctor); |
3e605b20 JM |
7831 | if (TREE_CODE (r) == TARGET_EXPR) |
7832 | /* Avoid creating another CONSTRUCTOR when we expand the | |
7833 | TARGET_EXPR. */ | |
7834 | r = TARGET_EXPR_INITIAL (r); | |
7835 | } | |
7836 | ||
ee1de08d JJ |
7837 | auto_vec<tree, 16> cleanups; |
7838 | global_ctx.cleanups = &cleanups; | |
7839 | ||
1595fe44 JM |
7840 | if (manifestly_const_eval) |
7841 | instantiate_constexpr_fns (r); | |
72f76540 JM |
7842 | r = cxx_eval_constant_expression (&ctx, r, vc_prvalue, |
7843 | &non_constant_p, &overflow_p); | |
2d76680f | 7844 | |
8e007055 JJ |
7845 | if (!constexpr_dtor) |
7846 | verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p); | |
7847 | else | |
7848 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true; | |
2d76680f | 7849 | |
ee1de08d JJ |
7850 | unsigned int i; |
7851 | tree cleanup; | |
7852 | /* Evaluate the cleanups. */ | |
7853 | FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup) | |
72f76540 | 7854 | cxx_eval_constant_expression (&ctx, cleanup, vc_discard, |
ee1de08d JJ |
7855 | &non_constant_p, &overflow_p); |
7856 | ||
023d89c7 JM |
7857 | /* Mutable logic is a bit tricky: we want to allow initialization of |
7858 | constexpr variables with mutable members, but we can't copy those | |
7859 | members to another constexpr variable. */ | |
8e007055 | 7860 | if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_MUTABLE_POISON (r)) |
2d76680f | 7861 | { |
2d76680f | 7862 | if (!allow_non_constant) |
023d89c7 JM |
7863 | error ("%qE is not a constant expression because it refers to " |
7864 | "mutable subobjects of %qT", t, type); | |
2d76680f PC |
7865 | non_constant_p = true; |
7866 | } | |
7867 | ||
8e007055 | 7868 | if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r)) |
f64e0c02 JM |
7869 | { |
7870 | if (!allow_non_constant) | |
7871 | error ("%qE is not a constant expression because it refers to " | |
7872 | "an incompletely initialized variable", t); | |
7873 | TREE_CONSTANT (r) = false; | |
7874 | non_constant_p = true; | |
7875 | } | |
7876 | ||
8e007055 JJ |
7877 | if (!global_ctx.heap_vars.is_empty ()) |
7878 | { | |
7879 | tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs, | |
7880 | NULL); | |
7881 | unsigned int i; | |
7882 | if (heap_var) | |
7883 | { | |
7884 | if (!allow_non_constant && !non_constant_p) | |
7885 | error_at (DECL_SOURCE_LOCATION (heap_var), | |
7886 | "%qE is not a constant expression because it refers to " | |
7887 | "a result of %<operator new%>", t); | |
7888 | r = t; | |
7889 | non_constant_p = true; | |
7890 | } | |
7891 | FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var) | |
a8db7887 JJ |
7892 | { |
7893 | if (DECL_NAME (heap_var) != heap_deleted_identifier) | |
7894 | { | |
7895 | if (!allow_non_constant && !non_constant_p) | |
7896 | error_at (DECL_SOURCE_LOCATION (heap_var), | |
7897 | "%qE is not a constant expression because allocated " | |
7898 | "storage has not been deallocated", t); | |
7899 | r = t; | |
7900 | non_constant_p = true; | |
7901 | } | |
7902 | varpool_node::get (heap_var)->remove (); | |
7903 | } | |
8e007055 JJ |
7904 | } |
7905 | ||
f968ef9b | 7906 | /* Check that immediate invocation does not return an expression referencing |
7473b8a9 JJ |
7907 | any immediate function decls. */ |
7908 | if (is_consteval || in_immediate_context ()) | |
f968ef9b JJ |
7909 | if (tree immediate_fndecl |
7910 | = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl, | |
7911 | NULL)) | |
7912 | { | |
7913 | if (!allow_non_constant && !non_constant_p) | |
7914 | error_at (cp_expr_loc_or_input_loc (t), | |
7915 | "immediate evaluation returns address of immediate " | |
7916 | "function %qD", immediate_fndecl); | |
7917 | r = t; | |
7918 | non_constant_p = true; | |
7919 | } | |
7920 | ||
8895443a JM |
7921 | if (non_constant_p) |
7922 | /* If we saw something bad, go back to our argument. The wrapping below is | |
7923 | only for the cases of TREE_CONSTANT argument or overflow. */ | |
7924 | r = t; | |
2d76680f PC |
7925 | |
7926 | if (!non_constant_p && overflow_p) | |
7927 | non_constant_p = true; | |
7928 | ||
e0804c9b | 7929 | /* Unshare the result. */ |
56cfb596 | 7930 | bool should_unshare = true; |
e0804c9b JM |
7931 | if (r == t || (TREE_CODE (t) == TARGET_EXPR |
7932 | && TARGET_EXPR_INITIAL (t) == r)) | |
56cfb596 PP |
7933 | should_unshare = false; |
7934 | ||
2d76680f PC |
7935 | if (non_constant_p && !allow_non_constant) |
7936 | return error_mark_node; | |
8e007055 JJ |
7937 | else if (constexpr_dtor) |
7938 | return r; | |
2d76680f | 7939 | else if (non_constant_p && TREE_CONSTANT (r)) |
ab71d3fe | 7940 | r = mark_non_constant (r); |
5dab8b11 | 7941 | else if (non_constant_p) |
2d76680f PC |
7942 | return t; |
7943 | ||
56cfb596 PP |
7944 | if (should_unshare) |
7945 | r = unshare_expr (r); | |
7946 | ||
2d76680f PC |
7947 | if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r))) |
7948 | { | |
5dab8b11 | 7949 | r = adjust_temp_type (type, r); |
2d76680f PC |
7950 | if (TREE_CODE (t) == TARGET_EXPR |
7951 | && TARGET_EXPR_INITIAL (t) == r) | |
7952 | return t; | |
1a5145f1 JJ |
7953 | else if (TREE_CODE (t) == CONSTRUCTOR) |
7954 | ; | |
7955 | else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t)) | |
7956 | r = get_target_expr (r); | |
7957 | else | |
2d76680f | 7958 | { |
8e007055 | 7959 | r = get_target_expr_sfinae (r, tf_warning_or_error | tf_no_cleanup); |
2d76680f | 7960 | TREE_CONSTANT (r) = true; |
2d76680f PC |
7961 | } |
7962 | } | |
5dab8b11 | 7963 | |
729f6881 JM |
7964 | /* Remember the original location if that wouldn't need a wrapper. */ |
7965 | if (location_t loc = EXPR_LOCATION (t)) | |
8d970817 | 7966 | protected_set_expr_location (r, loc); |
729f6881 | 7967 | |
5dab8b11 | 7968 | return r; |
2d76680f PC |
7969 | } |
7970 | ||
2d76680f PC |
7971 | /* If T represents a constant expression returns its reduced value. |
7972 | Otherwise return error_mark_node. If T is dependent, then | |
7973 | return NULL. */ | |
7974 | ||
7975 | tree | |
3e605b20 | 7976 | cxx_constant_value (tree t, tree decl) |
2d76680f | 7977 | { |
8e007055 JJ |
7978 | return cxx_eval_outermost_constant_expr (t, false, true, true, false, decl); |
7979 | } | |
7980 | ||
a8de8324 JM |
7981 | /* As above, but respect SFINAE. */ |
7982 | ||
7983 | tree | |
647537ad | 7984 | cxx_constant_value_sfinae (tree t, tree decl, tsubst_flags_t complain) |
a8de8324 JM |
7985 | { |
7986 | bool sfinae = !(complain & tf_error); | |
647537ad | 7987 | tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, true, false, decl); |
a8de8324 JM |
7988 | if (sfinae && !TREE_CONSTANT (r)) |
7989 | r = error_mark_node; | |
7990 | return r; | |
7991 | } | |
7992 | ||
8e007055 JJ |
7993 | /* Like cxx_constant_value, but used for evaluation of constexpr destructors |
7994 | of constexpr variables. The actual initializer of DECL is not modified. */ | |
7995 | ||
7996 | void | |
7997 | cxx_constant_dtor (tree t, tree decl) | |
7998 | { | |
7999 | cxx_eval_outermost_constant_expr (t, false, true, true, true, decl); | |
2d76680f PC |
8000 | } |
8001 | ||
cda0a029 JM |
8002 | /* Helper routine for fold_simple function. Either return simplified |
8003 | expression T, otherwise NULL_TREE. | |
8004 | In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold | |
8005 | even if we are within template-declaration. So be careful on call, as in | |
8006 | such case types can be undefined. */ | |
8007 | ||
8008 | static tree | |
8009 | fold_simple_1 (tree t) | |
8010 | { | |
8011 | tree op1; | |
8012 | enum tree_code code = TREE_CODE (t); | |
8013 | ||
8014 | switch (code) | |
8015 | { | |
8016 | case INTEGER_CST: | |
8017 | case REAL_CST: | |
8018 | case VECTOR_CST: | |
8019 | case FIXED_CST: | |
8020 | case COMPLEX_CST: | |
8021 | return t; | |
8022 | ||
8023 | case SIZEOF_EXPR: | |
8024 | return fold_sizeof_expr (t); | |
8025 | ||
8026 | case ABS_EXPR: | |
e28fadbc | 8027 | case ABSU_EXPR: |
cda0a029 JM |
8028 | case CONJ_EXPR: |
8029 | case REALPART_EXPR: | |
8030 | case IMAGPART_EXPR: | |
8031 | case NEGATE_EXPR: | |
8032 | case BIT_NOT_EXPR: | |
8033 | case TRUTH_NOT_EXPR: | |
cda0a029 | 8034 | case VIEW_CONVERT_EXPR: |
82a344f2 | 8035 | CASE_CONVERT: |
cda0a029 JM |
8036 | case FLOAT_EXPR: |
8037 | case FIX_TRUNC_EXPR: | |
8038 | case FIXED_CONVERT_EXPR: | |
8039 | case ADDR_SPACE_CONVERT_EXPR: | |
8040 | ||
8041 | op1 = TREE_OPERAND (t, 0); | |
8042 | ||
8043 | t = const_unop (code, TREE_TYPE (t), op1); | |
8044 | if (!t) | |
8045 | return NULL_TREE; | |
8046 | ||
8047 | if (CONVERT_EXPR_CODE_P (code) | |
8048 | && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1)) | |
8049 | TREE_OVERFLOW (t) = false; | |
8050 | return t; | |
8051 | ||
8052 | default: | |
8053 | return NULL_TREE; | |
8054 | } | |
8055 | } | |
8056 | ||
8057 | /* If T is a simple constant expression, returns its simplified value. | |
dba99244 | 8058 | Otherwise returns T. In contrast to maybe_constant_value we |
cda0a029 JM |
8059 | simplify only few operations on constant-expressions, and we don't |
8060 | try to simplify constexpressions. */ | |
8061 | ||
8062 | tree | |
8063 | fold_simple (tree t) | |
8064 | { | |
cda0a029 JM |
8065 | if (processing_template_decl) |
8066 | return t; | |
8067 | ||
dba99244 MP |
8068 | tree r = fold_simple_1 (t); |
8069 | if (r) | |
8070 | return r; | |
cda0a029 | 8071 | |
dba99244 | 8072 | return t; |
cda0a029 JM |
8073 | } |
8074 | ||
2d76680f PC |
8075 | /* If T is a constant expression, returns its reduced value. |
8076 | Otherwise, if T does not have TREE_CONSTANT set, returns T. | |
13de99bc JJ |
8077 | Otherwise, returns a version of T without TREE_CONSTANT. |
8078 | MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated | |
f65a3299 | 8079 | as per P0595. */ |
2d76680f | 8080 | |
8a87daca JM |
8081 | static GTY((deletable)) hash_map<tree, tree> *cv_cache; |
8082 | ||
8083 | tree | |
f65a3299 | 8084 | maybe_constant_value (tree t, tree decl, bool manifestly_const_eval) |
2d76680f PC |
8085 | { |
8086 | tree r; | |
8087 | ||
a0ab7ccd | 8088 | if (!is_nondependent_constant_expression (t)) |
2d76680f | 8089 | { |
ab71d3fe PP |
8090 | if (TREE_OVERFLOW_P (t) |
8091 | || (!processing_template_decl && TREE_CONSTANT (t))) | |
8092 | t = mark_non_constant (t); | |
2d76680f PC |
8093 | return t; |
8094 | } | |
8a87daca JM |
8095 | else if (CONSTANT_CLASS_P (t)) |
8096 | /* No caching or evaluation needed. */ | |
8097 | return t; | |
8098 | ||
13de99bc | 8099 | if (manifestly_const_eval) |
f65a3299 | 8100 | return cxx_eval_outermost_constant_expr (t, true, true, true, false, decl); |
13de99bc | 8101 | |
8a87daca JM |
8102 | if (cv_cache == NULL) |
8103 | cv_cache = hash_map<tree, tree>::create_ggc (101); | |
8104 | if (tree *cached = cv_cache->get (t)) | |
173c8def JM |
8105 | { |
8106 | r = *cached; | |
8107 | if (r != t) | |
8108 | { | |
9f143008 | 8109 | r = break_out_target_exprs (r, /*clear_loc*/true); |
173c8def JM |
8110 | protected_set_expr_location (r, EXPR_LOCATION (t)); |
8111 | } | |
dfffecb8 | 8112 | return r; |
173c8def | 8113 | } |
2d76680f | 8114 | |
4df7f8c7 JM |
8115 | /* Don't evaluate an unevaluated operand. */ |
8116 | if (cp_unevaluated_operand) | |
8117 | return t; | |
8118 | ||
f65a3299 PP |
8119 | uid_sensitive_constexpr_evaluation_checker c; |
8120 | r = cxx_eval_outermost_constant_expr (t, true, true, false, false, decl); | |
595278be MM |
8121 | gcc_checking_assert (r == t |
8122 | || CONVERT_EXPR_P (t) | |
8123 | || TREE_CODE (t) == VIEW_CONVERT_EXPR | |
8124 | || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) | |
8125 | || !cp_tree_equal (r, t)); | |
f65a3299 PP |
8126 | if (!c.evaluation_restricted_p ()) |
8127 | cv_cache->put (t, r); | |
2d76680f PC |
8128 | return r; |
8129 | } | |
8130 | ||
7a7ac32a PP |
8131 | /* Dispose of the whole CV_CACHE. */ |
8132 | ||
8133 | static void | |
8134 | clear_cv_cache (void) | |
8135 | { | |
8136 | if (cv_cache != NULL) | |
8137 | cv_cache->empty (); | |
8138 | } | |
8139 | ||
bebabf70 | 8140 | /* Dispose of the whole CV_CACHE and FOLD_CACHE. */ |
1e297006 MP |
8141 | |
8142 | void | |
bebabf70 | 8143 | clear_cv_and_fold_caches () |
1e297006 | 8144 | { |
7a7ac32a | 8145 | clear_cv_cache (); |
eba9e839 | 8146 | clear_fold_cache (); |
1e297006 MP |
8147 | } |
8148 | ||
a81c8e8c MP |
8149 | /* Internal function handling expressions in templates for |
8150 | fold_non_dependent_expr and fold_non_dependent_init. | |
8151 | ||
8152 | If we're in a template, but T isn't value dependent, simplify | |
8153 | it. We're supposed to treat: | |
8154 | ||
8155 | template <typename T> void f(T[1 + 1]); | |
8156 | template <typename T> void f(T[2]); | |
8157 | ||
8158 | as two declarations of the same function, for example. */ | |
8159 | ||
8160 | static tree | |
8161 | fold_non_dependent_expr_template (tree t, tsubst_flags_t complain, | |
f968ef9b JJ |
8162 | bool manifestly_const_eval, |
8163 | tree object) | |
a81c8e8c MP |
8164 | { |
8165 | gcc_assert (processing_template_decl); | |
8166 | ||
8167 | if (is_nondependent_constant_expression (t)) | |
8168 | { | |
8169 | processing_template_decl_sentinel s; | |
8170 | t = instantiate_non_dependent_expr_internal (t, complain); | |
8171 | ||
8172 | if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t)) | |
8173 | { | |
8174 | if (TREE_OVERFLOW_P (t)) | |
8175 | { | |
8176 | t = build_nop (TREE_TYPE (t), t); | |
8177 | TREE_CONSTANT (t) = false; | |
8178 | } | |
8179 | return t; | |
8180 | } | |
8181 | ||
4df7f8c7 JM |
8182 | if (cp_unevaluated_operand && !manifestly_const_eval) |
8183 | return t; | |
8184 | ||
a81c8e8c MP |
8185 | tree r = cxx_eval_outermost_constant_expr (t, true, true, |
8186 | manifestly_const_eval, | |
f968ef9b | 8187 | false, object); |
a81c8e8c MP |
8188 | /* cp_tree_equal looks through NOPs, so allow them. */ |
8189 | gcc_checking_assert (r == t | |
8190 | || CONVERT_EXPR_P (t) | |
8191 | || TREE_CODE (t) == VIEW_CONVERT_EXPR | |
8192 | || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) | |
8193 | || !cp_tree_equal (r, t)); | |
8194 | return r; | |
8195 | } | |
8196 | else if (TREE_OVERFLOW_P (t)) | |
8197 | { | |
8198 | t = build_nop (TREE_TYPE (t), t); | |
8199 | TREE_CONSTANT (t) = false; | |
8200 | } | |
8201 | ||
8202 | return t; | |
8203 | } | |
8204 | ||
234bef96 PC |
8205 | /* Like maybe_constant_value but first fully instantiate the argument. |
8206 | ||
8207 | Note: this is equivalent to instantiate_non_dependent_expr_sfinae | |
e56f6629 JM |
8208 | (t, complain) followed by maybe_constant_value but is more efficient, |
8209 | because it calls instantiation_dependent_expression_p and | |
8210 | potential_constant_expression at most once. | |
4cd3e7df | 8211 | The manifestly_const_eval argument is passed to maybe_constant_value. |
e56f6629 JM |
8212 | |
8213 | Callers should generally pass their active complain, or if they are in a | |
8214 | non-template, diagnosing context, they can use the default of | |
8215 | tf_warning_or_error. Callers that might be within a template context, don't | |
8216 | have a complain parameter, and aren't going to remember the result for long | |
8217 | (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node | |
8218 | appropriately. */ | |
234bef96 PC |
8219 | |
8220 | tree | |
e56f6629 | 8221 | fold_non_dependent_expr (tree t, |
4cd3e7df | 8222 | tsubst_flags_t complain /* = tf_warning_or_error */, |
f968ef9b JJ |
8223 | bool manifestly_const_eval /* = false */, |
8224 | tree object /* = NULL_TREE */) | |
234bef96 PC |
8225 | { |
8226 | if (t == NULL_TREE) | |
8227 | return NULL_TREE; | |
8228 | ||
a81c8e8c MP |
8229 | if (processing_template_decl) |
8230 | return fold_non_dependent_expr_template (t, complain, | |
f968ef9b | 8231 | manifestly_const_eval, object); |
234bef96 | 8232 | |
f968ef9b | 8233 | return maybe_constant_value (t, object, manifestly_const_eval); |
a81c8e8c | 8234 | } |
234bef96 | 8235 | |
ae2ebf01 MP |
8236 | /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant, |
8237 | return the original expression. */ | |
8238 | ||
8239 | tree | |
8240 | maybe_fold_non_dependent_expr (tree expr, | |
8241 | tsubst_flags_t complain/*=tf_warning_or_error*/) | |
8242 | { | |
8243 | tree t = fold_non_dependent_expr (expr, complain); | |
8244 | if (t && TREE_CONSTANT (t)) | |
8245 | return t; | |
8246 | ||
8247 | return expr; | |
8248 | } | |
234bef96 | 8249 | |
a81c8e8c | 8250 | /* Like maybe_constant_init but first fully instantiate the argument. */ |
234bef96 | 8251 | |
a81c8e8c MP |
8252 | tree |
8253 | fold_non_dependent_init (tree t, | |
8254 | tsubst_flags_t complain /*=tf_warning_or_error*/, | |
69bf1c7d MP |
8255 | bool manifestly_const_eval /*=false*/, |
8256 | tree object /* = NULL_TREE */) | |
a81c8e8c MP |
8257 | { |
8258 | if (t == NULL_TREE) | |
8259 | return NULL_TREE; | |
8260 | ||
8261 | if (processing_template_decl) | |
8262 | { | |
8263 | t = fold_non_dependent_expr_template (t, complain, | |
69bf1c7d | 8264 | manifestly_const_eval, object); |
a81c8e8c MP |
8265 | /* maybe_constant_init does this stripping, so do it here too. */ |
8266 | if (TREE_CODE (t) == TARGET_EXPR) | |
234bef96 | 8267 | { |
a81c8e8c MP |
8268 | tree init = TARGET_EXPR_INITIAL (t); |
8269 | if (TREE_CODE (init) == CONSTRUCTOR) | |
8270 | t = init; | |
234bef96 PC |
8271 | } |
8272 | return t; | |
8273 | } | |
8274 | ||
69bf1c7d | 8275 | return maybe_constant_init (t, object, manifestly_const_eval); |
234bef96 PC |
8276 | } |
8277 | ||
2d76680f | 8278 | /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather |
e4082611 JJ |
8279 | than wrapped in a TARGET_EXPR. |
8280 | ALLOW_NON_CONSTANT is false if T is required to be a constant expression. | |
13de99bc | 8281 | MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as |
e4082611 | 8282 | per P0595 even when ALLOW_NON_CONSTANT is true. */ |
2d76680f | 8283 | |
118cd6ba | 8284 | static tree |
e4082611 | 8285 | maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant, |
13de99bc | 8286 | bool manifestly_const_eval) |
2d76680f | 8287 | { |
cda0a029 JM |
8288 | if (!t) |
8289 | return t; | |
2d76680f PC |
8290 | if (TREE_CODE (t) == EXPR_STMT) |
8291 | t = TREE_OPERAND (t, 0); | |
8292 | if (TREE_CODE (t) == CONVERT_EXPR | |
8293 | && VOID_TYPE_P (TREE_TYPE (t))) | |
8294 | t = TREE_OPERAND (t, 0); | |
60813a46 JM |
8295 | if (TREE_CODE (t) == INIT_EXPR) |
8296 | t = TREE_OPERAND (t, 1); | |
f64e0c02 JM |
8297 | if (TREE_CODE (t) == TARGET_EXPR) |
8298 | t = TARGET_EXPR_INITIAL (t); | |
a0ab7ccd | 8299 | if (!is_nondependent_static_init_expression (t)) |
69eb4fde | 8300 | /* Don't try to evaluate it. */; |
118cd6ba | 8301 | else if (CONSTANT_CLASS_P (t) && allow_non_constant) |
8a87daca | 8302 | /* No evaluation needed. */; |
69eb4fde | 8303 | else |
6209009d JM |
8304 | { |
8305 | /* [basic.start.static] allows constant-initialization of variables with | |
8306 | static or thread storage duration even if it isn't required, but we | |
8307 | shouldn't bend the rules the same way for automatic variables. */ | |
8308 | bool is_static = (decl && DECL_P (decl) | |
8309 | && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))); | |
8310 | t = cxx_eval_outermost_constant_expr (t, allow_non_constant, !is_static, | |
8311 | manifestly_const_eval, false, decl); | |
8312 | } | |
2d76680f PC |
8313 | if (TREE_CODE (t) == TARGET_EXPR) |
8314 | { | |
8315 | tree init = TARGET_EXPR_INITIAL (t); | |
8316 | if (TREE_CODE (init) == CONSTRUCTOR) | |
8317 | t = init; | |
8318 | } | |
8319 | return t; | |
8320 | } | |
8321 | ||
118cd6ba MP |
8322 | /* Wrapper for maybe_constant_init_1 which permits non constants. */ |
8323 | ||
8324 | tree | |
13de99bc | 8325 | maybe_constant_init (tree t, tree decl, bool manifestly_const_eval) |
118cd6ba | 8326 | { |
13de99bc | 8327 | return maybe_constant_init_1 (t, decl, true, manifestly_const_eval); |
118cd6ba MP |
8328 | } |
8329 | ||
8330 | /* Wrapper for maybe_constant_init_1 which does not permit non constants. */ | |
8331 | ||
8332 | tree | |
8333 | cxx_constant_init (tree t, tree decl) | |
8334 | { | |
e4082611 | 8335 | return maybe_constant_init_1 (t, decl, false, true); |
118cd6ba MP |
8336 | } |
8337 | ||
2d76680f PC |
8338 | #if 0 |
8339 | /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */ | |
8340 | /* Return true if the object referred to by REF has automatic or thread | |
8341 | local storage. */ | |
8342 | ||
8343 | enum { ck_ok, ck_bad, ck_unknown }; | |
8344 | static int | |
8345 | check_automatic_or_tls (tree ref) | |
8346 | { | |
ef4bddc2 | 8347 | machine_mode mode; |
f37fac2b | 8348 | poly_int64 bitsize, bitpos; |
2d76680f PC |
8349 | tree offset; |
8350 | int volatilep = 0, unsignedp = 0; | |
8351 | tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset, | |
8352 | &mode, &unsignedp, &volatilep, false); | |
8353 | duration_kind dk; | |
8354 | ||
8355 | /* If there isn't a decl in the middle, we don't know the linkage here, | |
8356 | and this isn't a constant expression anyway. */ | |
8357 | if (!DECL_P (decl)) | |
8358 | return ck_unknown; | |
8359 | dk = decl_storage_duration (decl); | |
8360 | return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok; | |
8361 | } | |
8362 | #endif | |
8363 | ||
c7a53bdb JJ |
8364 | /* Data structure for passing data from potential_constant_expression_1 |
8365 | to check_for_return_continue via cp_walk_tree. */ | |
8366 | struct check_for_return_continue_data { | |
8367 | hash_set<tree> *pset; | |
8368 | tree continue_stmt; | |
0fb7aa20 | 8369 | tree break_stmt; |
c7a53bdb JJ |
8370 | }; |
8371 | ||
8372 | /* Helper function for potential_constant_expression_1 SWITCH_STMT handling, | |
8373 | called through cp_walk_tree. Return the first RETURN_EXPR found, or note | |
0fb7aa20 | 8374 | the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */ |
c7a53bdb JJ |
8375 | static tree |
8376 | check_for_return_continue (tree *tp, int *walk_subtrees, void *data) | |
8377 | { | |
0fb7aa20 | 8378 | tree t = *tp, s, b; |
c7a53bdb JJ |
8379 | check_for_return_continue_data *d = (check_for_return_continue_data *) data; |
8380 | switch (TREE_CODE (t)) | |
8381 | { | |
8382 | case RETURN_EXPR: | |
8383 | return t; | |
8384 | ||
8385 | case CONTINUE_STMT: | |
8386 | if (d->continue_stmt == NULL_TREE) | |
8387 | d->continue_stmt = t; | |
8388 | break; | |
8389 | ||
0fb7aa20 JJ |
8390 | case BREAK_STMT: |
8391 | if (d->break_stmt == NULL_TREE) | |
8392 | d->break_stmt = t; | |
8393 | break; | |
8394 | ||
c7a53bdb JJ |
8395 | #define RECUR(x) \ |
8396 | if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \ | |
8397 | d->pset)) \ | |
8398 | return r | |
8399 | ||
8400 | /* For loops, walk subtrees manually, so that continue stmts found | |
8401 | inside of the bodies of the loops are ignored. */ | |
8402 | case DO_STMT: | |
8403 | *walk_subtrees = 0; | |
8404 | RECUR (DO_COND (t)); | |
8405 | s = d->continue_stmt; | |
0fb7aa20 | 8406 | b = d->break_stmt; |
c7a53bdb JJ |
8407 | RECUR (DO_BODY (t)); |
8408 | d->continue_stmt = s; | |
0fb7aa20 | 8409 | d->break_stmt = b; |
c7a53bdb JJ |
8410 | break; |
8411 | ||
8412 | case WHILE_STMT: | |
8413 | *walk_subtrees = 0; | |
8414 | RECUR (WHILE_COND (t)); | |
8415 | s = d->continue_stmt; | |
0fb7aa20 | 8416 | b = d->break_stmt; |
c7a53bdb JJ |
8417 | RECUR (WHILE_BODY (t)); |
8418 | d->continue_stmt = s; | |
0fb7aa20 | 8419 | d->break_stmt = b; |
c7a53bdb JJ |
8420 | break; |
8421 | ||
8422 | case FOR_STMT: | |
8423 | *walk_subtrees = 0; | |
8424 | RECUR (FOR_INIT_STMT (t)); | |
8425 | RECUR (FOR_COND (t)); | |
8426 | RECUR (FOR_EXPR (t)); | |
8427 | s = d->continue_stmt; | |
0fb7aa20 | 8428 | b = d->break_stmt; |
c7a53bdb JJ |
8429 | RECUR (FOR_BODY (t)); |
8430 | d->continue_stmt = s; | |
0fb7aa20 | 8431 | d->break_stmt = b; |
c7a53bdb JJ |
8432 | break; |
8433 | ||
8434 | case RANGE_FOR_STMT: | |
8435 | *walk_subtrees = 0; | |
8436 | RECUR (RANGE_FOR_EXPR (t)); | |
8437 | s = d->continue_stmt; | |
0fb7aa20 | 8438 | b = d->break_stmt; |
c7a53bdb JJ |
8439 | RECUR (RANGE_FOR_BODY (t)); |
8440 | d->continue_stmt = s; | |
0fb7aa20 JJ |
8441 | d->break_stmt = b; |
8442 | break; | |
8443 | ||
8444 | case SWITCH_STMT: | |
8445 | *walk_subtrees = 0; | |
8446 | RECUR (SWITCH_STMT_COND (t)); | |
8447 | b = d->break_stmt; | |
8448 | RECUR (SWITCH_STMT_BODY (t)); | |
8449 | d->break_stmt = b; | |
c7a53bdb JJ |
8450 | break; |
8451 | #undef RECUR | |
8452 | ||
8453 | case STATEMENT_LIST: | |
8454 | case CONSTRUCTOR: | |
8455 | break; | |
8456 | ||
8457 | default: | |
8458 | if (!EXPR_P (t)) | |
8459 | *walk_subtrees = 0; | |
8460 | break; | |
8461 | } | |
8462 | ||
8463 | return NULL_TREE; | |
8464 | } | |
8465 | ||
2d76680f PC |
8466 | /* Return true if T denotes a potentially constant expression. Issue |
8467 | diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true, | |
a0ab7ccd JM |
8468 | an lvalue-rvalue conversion is implied. If NOW is true, we want to |
8469 | consider the expression in the current context, independent of constexpr | |
8470 | substitution. | |
2d76680f PC |
8471 | |
8472 | C++0x [expr.const] used to say | |
8473 | ||
8474 | 6 An expression is a potential constant expression if it is | |
8475 | a constant expression where all occurrences of function | |
8476 | parameters are replaced by arbitrary constant expressions | |
8477 | of the appropriate type. | |
8478 | ||
8479 | 2 A conditional expression is a constant expression unless it | |
8480 | involves one of the following as a potentially evaluated | |
8481 | subexpression (3.2), but subexpressions of logical AND (5.14), | |
8482 | logical OR (5.15), and conditional (5.16) operations that are | |
8483 | not evaluated are not considered. */ | |
8484 | ||
8485 | static bool | |
a0ab7ccd | 8486 | potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, |
3075affd | 8487 | tsubst_flags_t flags, tree *jump_target) |
2d76680f | 8488 | { |
a0ab7ccd | 8489 | #define RECUR(T,RV) \ |
3075affd | 8490 | potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target) |
a0ab7ccd | 8491 | |
2d76680f PC |
8492 | enum { any = false, rval = true }; |
8493 | int i; | |
8494 | tree tmp; | |
8495 | ||
8496 | if (t == error_mark_node) | |
8497 | return false; | |
8498 | if (t == NULL_TREE) | |
8499 | return true; | |
f9d0ca40 | 8500 | location_t loc = cp_expr_loc_or_input_loc (t); |
3075affd JM |
8501 | |
8502 | if (*jump_target) | |
8503 | /* If we are jumping, ignore everything. This is simpler than the | |
8504 | cxx_eval_constant_expression handling because we only need to be | |
8505 | conservatively correct, and we don't necessarily have a constant value | |
8506 | available, so we don't bother with switch tracking. */ | |
8507 | return true; | |
8508 | ||
3c393a2c | 8509 | if (TREE_THIS_VOLATILE (t) && want_rval) |
2d76680f PC |
8510 | { |
8511 | if (flags & tf_error) | |
3c393a2c MP |
8512 | error_at (loc, "lvalue-to-rvalue conversion of a volatile lvalue " |
8513 | "%qE with type %qT", t, TREE_TYPE (t)); | |
2d76680f PC |
8514 | return false; |
8515 | } | |
8516 | if (CONSTANT_CLASS_P (t)) | |
8517 | return true; | |
7dc2b4a2 JM |
8518 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED) |
8519 | && TREE_TYPE (t) == error_mark_node) | |
8520 | return false; | |
2d76680f PC |
8521 | |
8522 | switch (TREE_CODE (t)) | |
8523 | { | |
8524 | case FUNCTION_DECL: | |
8525 | case BASELINK: | |
8526 | case TEMPLATE_DECL: | |
8527 | case OVERLOAD: | |
8528 | case TEMPLATE_ID_EXPR: | |
8529 | case LABEL_DECL: | |
60813a46 | 8530 | case CASE_LABEL_EXPR: |
81012684 | 8531 | case PREDICT_EXPR: |
2d76680f PC |
8532 | case CONST_DECL: |
8533 | case SIZEOF_EXPR: | |
8534 | case ALIGNOF_EXPR: | |
8535 | case OFFSETOF_EXPR: | |
8536 | case NOEXCEPT_EXPR: | |
8537 | case TEMPLATE_PARM_INDEX: | |
8538 | case TRAIT_EXPR: | |
8539 | case IDENTIFIER_NODE: | |
8540 | case USERDEF_LITERAL: | |
8541 | /* We can see a FIELD_DECL in a pointer-to-member expression. */ | |
8542 | case FIELD_DECL: | |
cda0a029 | 8543 | case RESULT_DECL: |
2d76680f | 8544 | case USING_DECL: |
60813a46 | 8545 | case USING_STMT: |
3e605b20 | 8546 | case PLACEHOLDER_EXPR: |
971e17ff | 8547 | case REQUIRES_EXPR: |
98e5a19a | 8548 | case STATIC_ASSERT: |
96a95ac1 | 8549 | case DEBUG_BEGIN_STMT: |
2d76680f PC |
8550 | return true; |
8551 | ||
3075affd JM |
8552 | case RETURN_EXPR: |
8553 | if (!RECUR (TREE_OPERAND (t, 0), any)) | |
8554 | return false; | |
8555 | /* FALLTHROUGH */ | |
8556 | ||
8557 | case BREAK_STMT: | |
8558 | case CONTINUE_STMT: | |
8559 | *jump_target = t; | |
8560 | return true; | |
8561 | ||
a0ab7ccd | 8562 | case PARM_DECL: |
8fda2c27 | 8563 | if (now && want_rval) |
a0ab7ccd | 8564 | { |
8fda2c27 | 8565 | tree type = TREE_TYPE (t); |
9eb7d0d7 | 8566 | if ((processing_template_decl && !COMPLETE_TYPE_P (type)) |
61466206 | 8567 | || dependent_type_p (type) |
8fda2c27 JM |
8568 | || is_really_empty_class (type, /*ignore_vptr*/false)) |
8569 | /* An empty class has no data to read. */ | |
8570 | return true; | |
a0ab7ccd JM |
8571 | if (flags & tf_error) |
8572 | error ("%qE is not a constant expression", t); | |
8573 | return false; | |
8574 | } | |
8575 | return true; | |
8576 | ||
2d76680f PC |
8577 | case AGGR_INIT_EXPR: |
8578 | case CALL_EXPR: | |
8579 | /* -- an invocation of a function other than a constexpr function | |
8580 | or a constexpr constructor. */ | |
8581 | { | |
8582 | tree fun = get_function_named_in_call (t); | |
8583 | const int nargs = call_expr_nargs (t); | |
8584 | i = 0; | |
8585 | ||
60813a46 JM |
8586 | if (fun == NULL_TREE) |
8587 | { | |
44a845ca MS |
8588 | /* Reset to allow the function to continue past the end |
8589 | of the block below. Otherwise return early. */ | |
8590 | bool bail = true; | |
8591 | ||
35228ac7 JJ |
8592 | if (TREE_CODE (t) == CALL_EXPR |
8593 | && CALL_EXPR_FN (t) == NULL_TREE) | |
8594 | switch (CALL_EXPR_IFN (t)) | |
8595 | { | |
8596 | /* These should be ignored, they are optimized away from | |
8597 | constexpr functions. */ | |
8598 | case IFN_UBSAN_NULL: | |
8599 | case IFN_UBSAN_BOUNDS: | |
8600 | case IFN_UBSAN_VPTR: | |
81fea426 | 8601 | case IFN_FALLTHROUGH: |
35228ac7 | 8602 | return true; |
44a845ca MS |
8603 | |
8604 | case IFN_ADD_OVERFLOW: | |
8605 | case IFN_SUB_OVERFLOW: | |
8606 | case IFN_MUL_OVERFLOW: | |
e16f1cc7 | 8607 | case IFN_LAUNDER: |
d8fcab68 | 8608 | case IFN_VEC_CONVERT: |
44a845ca | 8609 | bail = false; |
d8fcab68 | 8610 | break; |
44a845ca | 8611 | |
35228ac7 JJ |
8612 | default: |
8613 | break; | |
8614 | } | |
44a845ca MS |
8615 | |
8616 | if (bail) | |
8617 | { | |
8618 | /* fold_call_expr can't do anything with IFN calls. */ | |
8619 | if (flags & tf_error) | |
e40b6fc7 | 8620 | error_at (loc, "call to internal function %qE", t); |
44a845ca MS |
8621 | return false; |
8622 | } | |
60813a46 | 8623 | } |
44a845ca MS |
8624 | |
8625 | if (fun && is_overloaded_fn (fun)) | |
2d76680f PC |
8626 | { |
8627 | if (TREE_CODE (fun) == FUNCTION_DECL) | |
8628 | { | |
8629 | if (builtin_valid_in_constant_expr_p (fun)) | |
8630 | return true; | |
87c2080b | 8631 | if (!maybe_constexpr_fn (fun) |
2d76680f PC |
8632 | /* Allow any built-in function; if the expansion |
8633 | isn't constant, we'll deal with that then. */ | |
8e007055 | 8634 | && !fndecl_built_in_p (fun) |
b04445d4 | 8635 | /* In C++20, replaceable global allocation functions |
8e007055 | 8636 | are constant expressions. */ |
8412b939 JJ |
8637 | && (!cxx_replaceable_global_alloc_fn (fun) |
8638 | || TREE_CODE (t) != CALL_EXPR | |
8639 | || (!CALL_FROM_NEW_OR_DELETE_P (t) | |
8640 | && (current_function_decl == NULL_TREE | |
8641 | || !is_std_allocator_allocate | |
8642 | (current_function_decl)))) | |
cf650568 JJ |
8643 | /* Allow placement new in std::construct_at. */ |
8644 | && (!cxx_placement_new_fn (fun) | |
8412b939 | 8645 | || TREE_CODE (t) != CALL_EXPR |
cf650568 | 8646 | || current_function_decl == NULL_TREE |
22edf943 MP |
8647 | || !is_std_construct_at (current_function_decl)) |
8648 | && !cxx_dynamic_cast_fn_p (fun)) | |
2d76680f PC |
8649 | { |
8650 | if (flags & tf_error) | |
8651 | { | |
84fa214d | 8652 | error_at (loc, "call to non-%<constexpr%> function %qD", |
e40b6fc7 | 8653 | fun); |
2d76680f PC |
8654 | explain_invalid_constexpr_fn (fun); |
8655 | } | |
8656 | return false; | |
8657 | } | |
8658 | /* A call to a non-static member function takes the address | |
8659 | of the object as the first argument. But in a constant | |
8660 | expression the address will be folded away, so look | |
8661 | through it now. */ | |
8662 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) | |
8663 | && !DECL_CONSTRUCTOR_P (fun)) | |
8664 | { | |
8665 | tree x = get_nth_callarg (t, 0); | |
8666 | if (is_this_parameter (x)) | |
3e605b20 | 8667 | return true; |
a0ab7ccd JM |
8668 | /* Don't require an immediately constant value, as |
8669 | constexpr substitution might not use the value. */ | |
8670 | bool sub_now = false; | |
8671 | if (!potential_constant_expression_1 (x, rval, strict, | |
3075affd JM |
8672 | sub_now, flags, |
8673 | jump_target)) | |
2d76680f PC |
8674 | return false; |
8675 | i = 1; | |
8676 | } | |
8677 | } | |
8678 | else | |
8679 | { | |
69eb4fde | 8680 | if (!RECUR (fun, true)) |
2d76680f PC |
8681 | return false; |
8682 | fun = get_first_fn (fun); | |
8683 | } | |
8684 | /* Skip initial arguments to base constructors. */ | |
8685 | if (DECL_BASE_CONSTRUCTOR_P (fun)) | |
8686 | i = num_artificial_parms_for (fun); | |
8687 | fun = DECL_ORIGIN (fun); | |
8688 | } | |
44a845ca | 8689 | else if (fun) |
2d76680f | 8690 | { |
69eb4fde | 8691 | if (RECUR (fun, rval)) |
2d76680f PC |
8692 | /* Might end up being a constant function pointer. */; |
8693 | else | |
8694 | return false; | |
8695 | } | |
8696 | for (; i < nargs; ++i) | |
8697 | { | |
8698 | tree x = get_nth_callarg (t, i); | |
c3c29ba5 JM |
8699 | /* In a template, reference arguments haven't been converted to |
8700 | REFERENCE_TYPE and we might not even know if the parameter | |
8701 | is a reference, so accept lvalue constants too. */ | |
8702 | bool rv = processing_template_decl ? any : rval; | |
a0ab7ccd JM |
8703 | /* Don't require an immediately constant value, as constexpr |
8704 | substitution might not use the value of the argument. */ | |
8705 | bool sub_now = false; | |
8706 | if (!potential_constant_expression_1 (x, rv, strict, | |
3075affd | 8707 | sub_now, flags, jump_target)) |
2d76680f PC |
8708 | return false; |
8709 | } | |
8710 | return true; | |
8711 | } | |
8712 | ||
8713 | case NON_LVALUE_EXPR: | |
8714 | /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to | |
8715 | -- an lvalue of integral type that refers to a non-volatile | |
8716 | const variable or static data member initialized with | |
8717 | constant expressions, or | |
8718 | ||
8719 | -- an lvalue of literal type that refers to non-volatile | |
8720 | object defined with constexpr, or that refers to a | |
8721 | sub-object of such an object; */ | |
69eb4fde | 8722 | return RECUR (TREE_OPERAND (t, 0), rval); |
2d76680f PC |
8723 | |
8724 | case VAR_DECL: | |
70f40fea | 8725 | if (DECL_HAS_VALUE_EXPR_P (t)) |
c1051bf7 | 8726 | { |
68ad1bf7 | 8727 | if (now && is_normal_capture_proxy (t)) |
c1051bf7 JM |
8728 | { |
8729 | /* -- in a lambda-expression, a reference to this or to a | |
8730 | variable with automatic storage duration defined outside that | |
8731 | lambda-expression, where the reference would be an | |
8732 | odr-use. */ | |
04b89192 JM |
8733 | |
8734 | if (want_rval) | |
8735 | /* Since we're doing an lvalue-rvalue conversion, this might | |
8736 | not be an odr-use, so evaluate the variable directly. */ | |
8737 | return RECUR (DECL_CAPTURED_VARIABLE (t), rval); | |
8738 | ||
c1051bf7 JM |
8739 | if (flags & tf_error) |
8740 | { | |
8741 | tree cap = DECL_CAPTURED_VARIABLE (t); | |
8742 | error ("lambda capture of %qE is not a constant expression", | |
8743 | cap); | |
04b89192 | 8744 | if (decl_constant_var_p (cap)) |
c1051bf7 JM |
8745 | inform (input_location, "because it is used as a glvalue"); |
8746 | } | |
8747 | return false; | |
8748 | } | |
c2856cee MP |
8749 | /* Treat __PRETTY_FUNCTION__ inside a template function as |
8750 | potentially-constant. */ | |
8751 | else if (DECL_PRETTY_FUNCTION_P (t) | |
8752 | && DECL_VALUE_EXPR (t) == error_mark_node) | |
8753 | return true; | |
c1051bf7 JM |
8754 | return RECUR (DECL_VALUE_EXPR (t), rval); |
8755 | } | |
69eb4fde | 8756 | if (want_rval |
98e5a19a | 8757 | && !var_in_maybe_constexpr_fn (t) |
7dc2b4a2 | 8758 | && !type_dependent_expression_p (t) |
4b691b13 | 8759 | && !decl_maybe_constant_var_p (t) |
69eb4fde JM |
8760 | && (strict |
8761 | || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t)) | |
4b691b13 JM |
8762 | || (DECL_INITIAL (t) |
8763 | && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))) | |
7dc2b4a2 | 8764 | && COMPLETE_TYPE_P (TREE_TYPE (t)) |
dbcd32f8 | 8765 | && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
2d76680f PC |
8766 | { |
8767 | if (flags & tf_error) | |
6821245b | 8768 | non_const_var_error (loc, t); |
2d76680f PC |
8769 | return false; |
8770 | } | |
8771 | return true; | |
8772 | ||
8773 | case NOP_EXPR: | |
ed3ea9f2 JJ |
8774 | if (REINTERPRET_CAST_P (t)) |
8775 | { | |
8776 | if (flags & tf_error) | |
a9c697b8 | 8777 | error_at (loc, "%<reinterpret_cast%> is not a constant expression"); |
ed3ea9f2 JJ |
8778 | return false; |
8779 | } | |
8780 | /* FALLTHRU */ | |
2d76680f PC |
8781 | case CONVERT_EXPR: |
8782 | case VIEW_CONVERT_EXPR: | |
8783 | /* -- a reinterpret_cast. FIXME not implemented, and this rule | |
8784 | may change to something more specific to type-punning (DR 1312). */ | |
8785 | { | |
8786 | tree from = TREE_OPERAND (t, 0); | |
dfd7fdca DM |
8787 | if (location_wrapper_p (t)) |
8788 | return (RECUR (from, want_rval)); | |
8789 | if (INDIRECT_TYPE_P (TREE_TYPE (t))) | |
2d76680f | 8790 | { |
dfd7fdca DM |
8791 | STRIP_ANY_LOCATION_WRAPPER (from); |
8792 | if (TREE_CODE (from) == INTEGER_CST | |
8793 | && !integer_zerop (from)) | |
8794 | { | |
8795 | if (flags & tf_error) | |
a9c697b8 MS |
8796 | error_at (loc, |
8797 | "%<reinterpret_cast%> from integer to pointer"); | |
dfd7fdca DM |
8798 | return false; |
8799 | } | |
2d76680f | 8800 | } |
69eb4fde | 8801 | return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR)); |
2d76680f PC |
8802 | } |
8803 | ||
be845b04 JJ |
8804 | case ADDRESSOF_EXPR: |
8805 | /* This is like ADDR_EXPR, except it won't form pointer-to-member. */ | |
8806 | t = TREE_OPERAND (t, 0); | |
8807 | goto handle_addr_expr; | |
8808 | ||
2d76680f PC |
8809 | case ADDR_EXPR: |
8810 | /* -- a unary operator & that is applied to an lvalue that | |
8811 | designates an object with thread or automatic storage | |
8812 | duration; */ | |
8813 | t = TREE_OPERAND (t, 0); | |
8814 | ||
8815 | if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t)) | |
8816 | /* A pointer-to-member constant. */ | |
8817 | return true; | |
8818 | ||
be845b04 | 8819 | handle_addr_expr: |
2d76680f PC |
8820 | #if 0 |
8821 | /* FIXME adjust when issue 1197 is fully resolved. For now don't do | |
8822 | any checking here, as we might dereference the pointer later. If | |
8823 | we remove this code, also remove check_automatic_or_tls. */ | |
8824 | i = check_automatic_or_tls (t); | |
8825 | if (i == ck_ok) | |
8826 | return true; | |
8827 | if (i == ck_bad) | |
8828 | { | |
8829 | if (flags & tf_error) | |
8830 | error ("address-of an object %qE with thread local or " | |
8831 | "automatic storage is not a constant expression", t); | |
8832 | return false; | |
8833 | } | |
8834 | #endif | |
69eb4fde | 8835 | return RECUR (t, any); |
2d76680f PC |
8836 | |
8837 | case COMPONENT_REF: | |
2d76680f PC |
8838 | case ARROW_EXPR: |
8839 | case OFFSET_REF: | |
8840 | /* -- a class member access unless its postfix-expression is | |
8841 | of literal type or of pointer to literal type. */ | |
8842 | /* This test would be redundant, as it follows from the | |
8843 | postfix-expression being a potential constant expression. */ | |
19dedccf JM |
8844 | if (type_unknown_p (t)) |
8845 | return true; | |
8fda2c27 JM |
8846 | if (is_overloaded_fn (t)) |
8847 | /* In a template, a COMPONENT_REF of a function expresses ob.fn(), | |
8848 | which uses ob as an lvalue. */ | |
8849 | want_rval = false; | |
8850 | gcc_fallthrough (); | |
8851 | ||
8852 | case REALPART_EXPR: | |
8853 | case IMAGPART_EXPR: | |
8854 | case BIT_FIELD_REF: | |
69eb4fde | 8855 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
2d76680f PC |
8856 | |
8857 | case EXPR_PACK_EXPANSION: | |
69eb4fde | 8858 | return RECUR (PACK_EXPANSION_PATTERN (t), want_rval); |
2d76680f PC |
8859 | |
8860 | case INDIRECT_REF: | |
8861 | { | |
8862 | tree x = TREE_OPERAND (t, 0); | |
8863 | STRIP_NOPS (x); | |
948d0d91 | 8864 | if (is_this_parameter (x) && !is_capture_proxy (x)) |
2d76680f | 8865 | { |
5d4e573b | 8866 | if (!var_in_maybe_constexpr_fn (x)) |
2d76680f PC |
8867 | { |
8868 | if (flags & tf_error) | |
e40b6fc7 | 8869 | error_at (loc, "use of %<this%> in a constant expression"); |
2d76680f PC |
8870 | return false; |
8871 | } | |
2d76680f PC |
8872 | return true; |
8873 | } | |
69eb4fde | 8874 | return RECUR (x, rval); |
2d76680f PC |
8875 | } |
8876 | ||
60813a46 | 8877 | case STATEMENT_LIST: |
0f54cc9c JM |
8878 | for (tree stmt : tsi_range (t)) |
8879 | if (!RECUR (stmt, any)) | |
8880 | return false; | |
8881 | return true; | |
60813a46 JM |
8882 | |
8883 | case MODIFY_EXPR: | |
8884 | if (cxx_dialect < cxx14) | |
8885 | goto fail; | |
69eb4fde | 8886 | if (!RECUR (TREE_OPERAND (t, 0), any)) |
60813a46 | 8887 | return false; |
8e007055 JJ |
8888 | /* Just ignore clobbers. */ |
8889 | if (TREE_CLOBBER_P (TREE_OPERAND (t, 1))) | |
8890 | return true; | |
69eb4fde | 8891 | if (!RECUR (TREE_OPERAND (t, 1), rval)) |
60813a46 JM |
8892 | return false; |
8893 | return true; | |
8894 | ||
8895 | case MODOP_EXPR: | |
8896 | if (cxx_dialect < cxx14) | |
8897 | goto fail; | |
69eb4fde | 8898 | if (!RECUR (TREE_OPERAND (t, 0), rval)) |
60813a46 | 8899 | return false; |
69eb4fde | 8900 | if (!RECUR (TREE_OPERAND (t, 2), rval)) |
60813a46 JM |
8901 | return false; |
8902 | return true; | |
8903 | ||
60813a46 | 8904 | case DO_STMT: |
69eb4fde | 8905 | if (!RECUR (DO_COND (t), rval)) |
60813a46 | 8906 | return false; |
69eb4fde | 8907 | if (!RECUR (DO_BODY (t), any)) |
60813a46 | 8908 | return false; |
3075affd JM |
8909 | if (breaks (jump_target) || continues (jump_target)) |
8910 | *jump_target = NULL_TREE; | |
60813a46 JM |
8911 | return true; |
8912 | ||
8913 | case FOR_STMT: | |
69eb4fde | 8914 | if (!RECUR (FOR_INIT_STMT (t), any)) |
60813a46 | 8915 | return false; |
8e9558f0 MP |
8916 | tmp = FOR_COND (t); |
8917 | if (!RECUR (tmp, rval)) | |
60813a46 | 8918 | return false; |
8e9558f0 MP |
8919 | if (tmp) |
8920 | { | |
8921 | if (!processing_template_decl) | |
8922 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
a385474c MP |
8923 | /* If we couldn't evaluate the condition, it might not ever be |
8924 | true. */ | |
8925 | if (!integer_onep (tmp)) | |
0fb7aa20 JJ |
8926 | { |
8927 | /* Before returning true, check if the for body can contain | |
8928 | a return. */ | |
8929 | hash_set<tree> pset; | |
8930 | check_for_return_continue_data data = { &pset, NULL_TREE, | |
8931 | NULL_TREE }; | |
8932 | if (tree ret_expr | |
8933 | = cp_walk_tree (&FOR_BODY (t), check_for_return_continue, | |
8934 | &data, &pset)) | |
8935 | *jump_target = ret_expr; | |
8936 | return true; | |
8937 | } | |
8e9558f0 | 8938 | } |
69eb4fde | 8939 | if (!RECUR (FOR_EXPR (t), any)) |
60813a46 | 8940 | return false; |
69eb4fde | 8941 | if (!RECUR (FOR_BODY (t), any)) |
60813a46 | 8942 | return false; |
3075affd JM |
8943 | if (breaks (jump_target) || continues (jump_target)) |
8944 | *jump_target = NULL_TREE; | |
60813a46 JM |
8945 | return true; |
8946 | ||
98e5a19a | 8947 | case RANGE_FOR_STMT: |
8112667c MP |
8948 | if (!RECUR (RANGE_FOR_INIT_STMT (t), any)) |
8949 | return false; | |
98e5a19a JM |
8950 | if (!RECUR (RANGE_FOR_EXPR (t), any)) |
8951 | return false; | |
8952 | if (!RECUR (RANGE_FOR_BODY (t), any)) | |
8953 | return false; | |
3075affd JM |
8954 | if (breaks (jump_target) || continues (jump_target)) |
8955 | *jump_target = NULL_TREE; | |
98e5a19a JM |
8956 | return true; |
8957 | ||
60813a46 | 8958 | case WHILE_STMT: |
8e9558f0 MP |
8959 | tmp = WHILE_COND (t); |
8960 | if (!RECUR (tmp, rval)) | |
60813a46 | 8961 | return false; |
8e9558f0 MP |
8962 | if (!processing_template_decl) |
8963 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
a385474c MP |
8964 | /* If we couldn't evaluate the condition, it might not ever be true. */ |
8965 | if (!integer_onep (tmp)) | |
0fb7aa20 JJ |
8966 | { |
8967 | /* Before returning true, check if the while body can contain | |
8968 | a return. */ | |
8969 | hash_set<tree> pset; | |
8970 | check_for_return_continue_data data = { &pset, NULL_TREE, | |
8971 | NULL_TREE }; | |
8972 | if (tree ret_expr | |
8973 | = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue, | |
8974 | &data, &pset)) | |
8975 | *jump_target = ret_expr; | |
8976 | return true; | |
8977 | } | |
69eb4fde | 8978 | if (!RECUR (WHILE_BODY (t), any)) |
60813a46 | 8979 | return false; |
3075affd JM |
8980 | if (breaks (jump_target) || continues (jump_target)) |
8981 | *jump_target = NULL_TREE; | |
60813a46 JM |
8982 | return true; |
8983 | ||
8984 | case SWITCH_STMT: | |
69eb4fde | 8985 | if (!RECUR (SWITCH_STMT_COND (t), rval)) |
60813a46 | 8986 | return false; |
ce965730 | 8987 | /* FIXME we don't check SWITCH_STMT_BODY currently, because even |
c7a53bdb JJ |
8988 | unreachable labels would be checked and it is enough if there is |
8989 | a single switch cond value for which it is a valid constant | |
8990 | expression. We need to check if there are any RETURN_EXPRs | |
8991 | or CONTINUE_STMTs inside of the body though, as in that case | |
8992 | we need to set *jump_target. */ | |
8993 | else | |
8994 | { | |
8995 | hash_set<tree> pset; | |
0fb7aa20 JJ |
8996 | check_for_return_continue_data data = { &pset, NULL_TREE, |
8997 | NULL_TREE }; | |
c7a53bdb JJ |
8998 | if (tree ret_expr |
8999 | = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue, | |
9000 | &data, &pset)) | |
9001 | /* The switch might return. */ | |
9002 | *jump_target = ret_expr; | |
9003 | else if (data.continue_stmt) | |
9004 | /* The switch can't return, but might continue. */ | |
9005 | *jump_target = data.continue_stmt; | |
9006 | } | |
60813a46 JM |
9007 | return true; |
9008 | ||
58611fb6 | 9009 | case STMT_EXPR: |
69eb4fde | 9010 | return RECUR (STMT_EXPR_STMT (t), rval); |
58611fb6 | 9011 | |
2d76680f | 9012 | case LAMBDA_EXPR: |
899ac3b8 JM |
9013 | if (cxx_dialect >= cxx17) |
9014 | /* In C++17 lambdas can be constexpr, don't give up yet. */ | |
9015 | return true; | |
9016 | else if (flags & tf_error) | |
9017 | error_at (loc, "lambda-expression is not a constant expression " | |
9018 | "before C++17"); | |
9019 | return false; | |
9020 | ||
2d76680f PC |
9021 | case DYNAMIC_CAST_EXPR: |
9022 | case PSEUDO_DTOR_EXPR: | |
2d76680f PC |
9023 | case NEW_EXPR: |
9024 | case VEC_NEW_EXPR: | |
9025 | case DELETE_EXPR: | |
9026 | case VEC_DELETE_EXPR: | |
9027 | case THROW_EXPR: | |
e40b6fc7 JJ |
9028 | case OMP_PARALLEL: |
9029 | case OMP_TASK: | |
9030 | case OMP_FOR: | |
897064e2 | 9031 | case OMP_SIMD: |
e40b6fc7 JJ |
9032 | case OMP_DISTRIBUTE: |
9033 | case OMP_TASKLOOP: | |
d81ab49d | 9034 | case OMP_LOOP: |
e40b6fc7 JJ |
9035 | case OMP_TEAMS: |
9036 | case OMP_TARGET_DATA: | |
9037 | case OMP_TARGET: | |
9038 | case OMP_SECTIONS: | |
9039 | case OMP_ORDERED: | |
9040 | case OMP_CRITICAL: | |
9041 | case OMP_SINGLE: | |
9042 | case OMP_SECTION: | |
9043 | case OMP_MASTER: | |
4b1e1434 | 9044 | case OMP_MASKED: |
e40b6fc7 JJ |
9045 | case OMP_TASKGROUP: |
9046 | case OMP_TARGET_UPDATE: | |
9047 | case OMP_TARGET_ENTER_DATA: | |
9048 | case OMP_TARGET_EXIT_DATA: | |
2d76680f PC |
9049 | case OMP_ATOMIC: |
9050 | case OMP_ATOMIC_READ: | |
9051 | case OMP_ATOMIC_CAPTURE_OLD: | |
9052 | case OMP_ATOMIC_CAPTURE_NEW: | |
28567c40 | 9053 | case OMP_DEPOBJ: |
e40b6fc7 JJ |
9054 | case OACC_PARALLEL: |
9055 | case OACC_KERNELS: | |
62aee289 | 9056 | case OACC_SERIAL: |
e40b6fc7 JJ |
9057 | case OACC_DATA: |
9058 | case OACC_HOST_DATA: | |
9059 | case OACC_LOOP: | |
9060 | case OACC_CACHE: | |
9061 | case OACC_DECLARE: | |
9062 | case OACC_ENTER_DATA: | |
9063 | case OACC_EXIT_DATA: | |
9064 | case OACC_UPDATE: | |
2d76680f PC |
9065 | /* GCC internal stuff. */ |
9066 | case VA_ARG_EXPR: | |
2d76680f | 9067 | case TRANSACTION_EXPR: |
81b6a6c5 | 9068 | case AT_ENCODE_EXPR: |
60813a46 | 9069 | fail: |
2d76680f | 9070 | if (flags & tf_error) |
e40b6fc7 | 9071 | error_at (loc, "expression %qE is not a constant expression", t); |
2d76680f PC |
9072 | return false; |
9073 | ||
529bc410 | 9074 | case ASM_EXPR: |
7c814975 MP |
9075 | if (flags & tf_error) |
9076 | inline_asm_in_constexpr_error (loc); | |
9077 | return false; | |
529bc410 | 9078 | |
bf8d8309 | 9079 | case OBJ_TYPE_REF: |
b04445d4 JM |
9080 | if (cxx_dialect >= cxx20) |
9081 | /* In C++20 virtual calls can be constexpr, don't give up yet. */ | |
bf8d8309 MP |
9082 | return true; |
9083 | else if (flags & tf_error) | |
a9c697b8 | 9084 | error_at (loc, |
b04445d4 | 9085 | "virtual functions cannot be %<constexpr%> before C++20"); |
bf8d8309 MP |
9086 | return false; |
9087 | ||
2d76680f | 9088 | case TYPEID_EXPR: |
66acfb80 MP |
9089 | /* In C++20, a typeid expression whose operand is of polymorphic |
9090 | class type can be constexpr. */ | |
2d76680f PC |
9091 | { |
9092 | tree e = TREE_OPERAND (t, 0); | |
b04445d4 | 9093 | if (cxx_dialect < cxx20 |
66acfb80 MP |
9094 | && strict |
9095 | && !TYPE_P (e) | |
9096 | && !type_dependent_expression_p (e) | |
2d76680f PC |
9097 | && TYPE_POLYMORPHIC_P (TREE_TYPE (e))) |
9098 | { | |
9099 | if (flags & tf_error) | |
a9c697b8 | 9100 | error_at (loc, "%<typeid%> is not a constant expression " |
e40b6fc7 | 9101 | "because %qE is of polymorphic type", e); |
2d76680f PC |
9102 | return false; |
9103 | } | |
9104 | return true; | |
9105 | } | |
9106 | ||
1af4ebf5 | 9107 | case POINTER_DIFF_EXPR: |
2d76680f | 9108 | case MINUS_EXPR: |
2d76680f PC |
9109 | want_rval = true; |
9110 | goto binary; | |
9111 | ||
9112 | case LT_EXPR: | |
9113 | case LE_EXPR: | |
9114 | case GT_EXPR: | |
9115 | case GE_EXPR: | |
9116 | case EQ_EXPR: | |
9117 | case NE_EXPR: | |
b7689b96 | 9118 | case SPACESHIP_EXPR: |
2d76680f PC |
9119 | want_rval = true; |
9120 | goto binary; | |
9121 | ||
60813a46 JM |
9122 | case PREINCREMENT_EXPR: |
9123 | case POSTINCREMENT_EXPR: | |
9124 | case PREDECREMENT_EXPR: | |
9125 | case POSTDECREMENT_EXPR: | |
9126 | if (cxx_dialect < cxx14) | |
9127 | goto fail; | |
9128 | goto unary; | |
9129 | ||
2d76680f PC |
9130 | case BIT_NOT_EXPR: |
9131 | /* A destructor. */ | |
9132 | if (TYPE_P (TREE_OPERAND (t, 0))) | |
9133 | return true; | |
191816a3 | 9134 | /* fall through. */ |
2d76680f | 9135 | |
2d76680f PC |
9136 | case CONJ_EXPR: |
9137 | case SAVE_EXPR: | |
9138 | case FIX_TRUNC_EXPR: | |
9139 | case FLOAT_EXPR: | |
9140 | case NEGATE_EXPR: | |
9141 | case ABS_EXPR: | |
e197e64e | 9142 | case ABSU_EXPR: |
2d76680f PC |
9143 | case TRUTH_NOT_EXPR: |
9144 | case FIXED_CONVERT_EXPR: | |
9145 | case UNARY_PLUS_EXPR: | |
4856a1f0 PC |
9146 | case UNARY_LEFT_FOLD_EXPR: |
9147 | case UNARY_RIGHT_FOLD_EXPR: | |
60813a46 | 9148 | unary: |
69eb4fde | 9149 | return RECUR (TREE_OPERAND (t, 0), rval); |
2d76680f PC |
9150 | |
9151 | case CAST_EXPR: | |
9152 | case CONST_CAST_EXPR: | |
9153 | case STATIC_CAST_EXPR: | |
9154 | case REINTERPRET_CAST_EXPR: | |
9155 | case IMPLICIT_CONV_EXPR: | |
9156 | if (cxx_dialect < cxx11 | |
9157 | && !dependent_type_p (TREE_TYPE (t)) | |
9158 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))) | |
9159 | /* In C++98, a conversion to non-integral type can't be part of a | |
9160 | constant expression. */ | |
9161 | { | |
9162 | if (flags & tf_error) | |
e40b6fc7 JJ |
9163 | error_at (loc, |
9164 | "cast to non-integral type %qT in a constant expression", | |
9165 | TREE_TYPE (t)); | |
2d76680f PC |
9166 | return false; |
9167 | } | |
fe0604d3 MP |
9168 | /* This might be a conversion from a class to a (potentially) literal |
9169 | type. Let's consider it potentially constant since the conversion | |
9170 | might be a constexpr user-defined conversion. */ | |
9171 | else if (cxx_dialect >= cxx11 | |
9172 | && (dependent_type_p (TREE_TYPE (t)) | |
9173 | || !COMPLETE_TYPE_P (TREE_TYPE (t)) | |
9174 | || literal_type_p (TREE_TYPE (t))) | |
9175 | && TREE_OPERAND (t, 0)) | |
9176 | { | |
9177 | tree type = TREE_TYPE (TREE_OPERAND (t, 0)); | |
9178 | /* If this is a dependent type, it could end up being a class | |
9179 | with conversions. */ | |
9180 | if (type == NULL_TREE || WILDCARD_TYPE_P (type)) | |
9181 | return true; | |
9182 | /* Or a non-dependent class which has conversions. */ | |
9183 | else if (CLASS_TYPE_P (type) | |
9184 | && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type))) | |
9185 | return true; | |
9186 | } | |
2d76680f | 9187 | |
69eb4fde | 9188 | return (RECUR (TREE_OPERAND (t, 0), |
9f613f06 | 9189 | !TYPE_REF_P (TREE_TYPE (t)))); |
2d76680f | 9190 | |
60813a46 | 9191 | case BIND_EXPR: |
69eb4fde | 9192 | return RECUR (BIND_EXPR_BODY (t), want_rval); |
60813a46 | 9193 | |
c19f317a PP |
9194 | case NON_DEPENDENT_EXPR: |
9195 | /* Treat NON_DEPENDENT_EXPR as non-constant: it's not handled by | |
9196 | constexpr evaluation or tsubst, so fold_non_dependent_expr can't | |
9197 | do anything useful with it. And we shouldn't see it in a context | |
9198 | where a constant expression is strictly required, hence the assert. */ | |
9199 | gcc_checking_assert (!(flags & tf_error)); | |
9200 | return false; | |
9201 | ||
60813a46 JM |
9202 | case CLEANUP_POINT_EXPR: |
9203 | case MUST_NOT_THROW_EXPR: | |
9204 | case TRY_CATCH_EXPR: | |
769430b2 | 9205 | case TRY_BLOCK: |
60813a46 JM |
9206 | case EH_SPEC_BLOCK: |
9207 | case EXPR_STMT: | |
2d76680f | 9208 | case PAREN_EXPR: |
2d76680f | 9209 | /* For convenience. */ |
f937929e JM |
9210 | case LOOP_EXPR: |
9211 | case EXIT_EXPR: | |
69eb4fde | 9212 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
2d76680f | 9213 | |
98e5a19a JM |
9214 | case DECL_EXPR: |
9215 | tmp = DECL_EXPR_DECL (t); | |
9216 | if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)) | |
9217 | { | |
72124f48 | 9218 | if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp)) |
98e5a19a JM |
9219 | { |
9220 | if (flags & tf_error) | |
72124f48 | 9221 | error_at (DECL_SOURCE_LOCATION (tmp), "%qD defined " |
8892d532 | 9222 | "%<thread_local%> in %<constexpr%> context", tmp); |
98e5a19a JM |
9223 | return false; |
9224 | } | |
8892d532 | 9225 | else if (TREE_STATIC (tmp)) |
98e5a19a JM |
9226 | { |
9227 | if (flags & tf_error) | |
72124f48 | 9228 | error_at (DECL_SOURCE_LOCATION (tmp), "%qD defined " |
8892d532 | 9229 | "%<static%> in %<constexpr%> context", tmp); |
98e5a19a JM |
9230 | return false; |
9231 | } | |
3885527d PC |
9232 | else if (!check_for_uninitialized_const_var |
9233 | (tmp, /*constexpr_context_p=*/true, flags)) | |
9234 | return false; | |
98e5a19a JM |
9235 | } |
9236 | return RECUR (tmp, want_rval); | |
9237 | ||
769430b2 JM |
9238 | case TRY_FINALLY_EXPR: |
9239 | return (RECUR (TREE_OPERAND (t, 0), want_rval) | |
9240 | && RECUR (TREE_OPERAND (t, 1), any)); | |
9241 | ||
2d76680f | 9242 | case SCOPE_REF: |
69eb4fde | 9243 | return RECUR (TREE_OPERAND (t, 1), want_rval); |
2d76680f PC |
9244 | |
9245 | case TARGET_EXPR: | |
d3a9902e JM |
9246 | if (!TARGET_EXPR_DIRECT_INIT_P (t) |
9247 | && !literal_type_p (TREE_TYPE (t))) | |
2d76680f PC |
9248 | { |
9249 | if (flags & tf_error) | |
9250 | { | |
097f82ec | 9251 | auto_diagnostic_group d; |
e40b6fc7 JJ |
9252 | error_at (loc, "temporary of non-literal type %qT in a " |
9253 | "constant expression", TREE_TYPE (t)); | |
2d76680f PC |
9254 | explain_non_literal_class (TREE_TYPE (t)); |
9255 | } | |
9256 | return false; | |
9257 | } | |
191816a3 | 9258 | /* FALLTHRU */ |
2d76680f | 9259 | case INIT_EXPR: |
69eb4fde | 9260 | return RECUR (TREE_OPERAND (t, 1), rval); |
2d76680f PC |
9261 | |
9262 | case CONSTRUCTOR: | |
9263 | { | |
9264 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); | |
9265 | constructor_elt *ce; | |
9266 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
69eb4fde | 9267 | if (!RECUR (ce->value, want_rval)) |
2d76680f PC |
9268 | return false; |
9269 | return true; | |
9270 | } | |
9271 | ||
9272 | case TREE_LIST: | |
9273 | { | |
9274 | gcc_assert (TREE_PURPOSE (t) == NULL_TREE | |
9275 | || DECL_P (TREE_PURPOSE (t))); | |
69eb4fde | 9276 | if (!RECUR (TREE_VALUE (t), want_rval)) |
2d76680f PC |
9277 | return false; |
9278 | if (TREE_CHAIN (t) == NULL_TREE) | |
9279 | return true; | |
69eb4fde | 9280 | return RECUR (TREE_CHAIN (t), want_rval); |
2d76680f PC |
9281 | } |
9282 | ||
9283 | case TRUNC_DIV_EXPR: | |
9284 | case CEIL_DIV_EXPR: | |
9285 | case FLOOR_DIV_EXPR: | |
9286 | case ROUND_DIV_EXPR: | |
9287 | case TRUNC_MOD_EXPR: | |
9288 | case CEIL_MOD_EXPR: | |
9289 | case ROUND_MOD_EXPR: | |
9290 | { | |
9291 | tree denom = TREE_OPERAND (t, 1); | |
69eb4fde | 9292 | if (!RECUR (denom, rval)) |
2d76680f PC |
9293 | return false; |
9294 | /* We can't call cxx_eval_outermost_constant_expr on an expression | |
234bef96 | 9295 | that hasn't been through instantiate_non_dependent_expr yet. */ |
2d76680f PC |
9296 | if (!processing_template_decl) |
9297 | denom = cxx_eval_outermost_constant_expr (denom, true); | |
9298 | if (integer_zerop (denom)) | |
9299 | { | |
9300 | if (flags & tf_error) | |
64d6d399 | 9301 | error ("division by zero is not a constant expression"); |
2d76680f PC |
9302 | return false; |
9303 | } | |
9304 | else | |
9305 | { | |
9306 | want_rval = true; | |
69eb4fde | 9307 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
2d76680f PC |
9308 | } |
9309 | } | |
9310 | ||
9311 | case COMPOUND_EXPR: | |
9312 | { | |
9313 | /* check_return_expr sometimes wraps a TARGET_EXPR in a | |
46fdced6 | 9314 | COMPOUND_EXPR; don't get confused. */ |
2d76680f PC |
9315 | tree op0 = TREE_OPERAND (t, 0); |
9316 | tree op1 = TREE_OPERAND (t, 1); | |
9317 | STRIP_NOPS (op1); | |
46fdced6 | 9318 | if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) |
69eb4fde | 9319 | return RECUR (op0, want_rval); |
2d76680f PC |
9320 | else |
9321 | goto binary; | |
9322 | } | |
9323 | ||
9324 | /* If the first operand is the non-short-circuit constant, look at | |
9325 | the second operand; otherwise we only care about the first one for | |
9326 | potentiality. */ | |
9327 | case TRUTH_AND_EXPR: | |
9328 | case TRUTH_ANDIF_EXPR: | |
9329 | tmp = boolean_true_node; | |
9330 | goto truth; | |
9331 | case TRUTH_OR_EXPR: | |
9332 | case TRUTH_ORIF_EXPR: | |
9333 | tmp = boolean_false_node; | |
9334 | truth: | |
9335 | { | |
9927ecbb PP |
9336 | tree op0 = TREE_OPERAND (t, 0); |
9337 | tree op1 = TREE_OPERAND (t, 1); | |
9338 | if (!RECUR (op0, rval)) | |
2d76680f | 9339 | return false; |
9927ecbb PP |
9340 | if (!(flags & tf_error) && RECUR (op1, rval)) |
9341 | /* When quiet, try to avoid expensive trial evaluation by first | |
9342 | checking potentiality of the second operand. */ | |
9343 | return true; | |
2d76680f | 9344 | if (!processing_template_decl) |
9927ecbb PP |
9345 | op0 = cxx_eval_outermost_constant_expr (op0, true); |
9346 | if (tree_int_cst_equal (op0, tmp)) | |
9347 | return (flags & tf_error) ? RECUR (op1, rval) : false; | |
2d76680f PC |
9348 | else |
9349 | return true; | |
9350 | } | |
9351 | ||
9352 | case PLUS_EXPR: | |
9353 | case MULT_EXPR: | |
9354 | case POINTER_PLUS_EXPR: | |
9355 | case RDIV_EXPR: | |
9356 | case EXACT_DIV_EXPR: | |
9357 | case MIN_EXPR: | |
9358 | case MAX_EXPR: | |
9359 | case LSHIFT_EXPR: | |
9360 | case RSHIFT_EXPR: | |
81fa6d73 JJ |
9361 | case LROTATE_EXPR: |
9362 | case RROTATE_EXPR: | |
2d76680f PC |
9363 | case BIT_IOR_EXPR: |
9364 | case BIT_XOR_EXPR: | |
9365 | case BIT_AND_EXPR: | |
9366 | case TRUTH_XOR_EXPR: | |
9367 | case UNORDERED_EXPR: | |
9368 | case ORDERED_EXPR: | |
9369 | case UNLT_EXPR: | |
9370 | case UNLE_EXPR: | |
9371 | case UNGT_EXPR: | |
9372 | case UNGE_EXPR: | |
9373 | case UNEQ_EXPR: | |
9374 | case LTGT_EXPR: | |
9375 | case RANGE_EXPR: | |
9376 | case COMPLEX_EXPR: | |
9377 | want_rval = true; | |
9378 | /* Fall through. */ | |
9379 | case ARRAY_REF: | |
9380 | case ARRAY_RANGE_REF: | |
9381 | case MEMBER_REF: | |
9382 | case DOTSTAR_EXPR: | |
41b38772 | 9383 | case MEM_REF: |
4856a1f0 PC |
9384 | case BINARY_LEFT_FOLD_EXPR: |
9385 | case BINARY_RIGHT_FOLD_EXPR: | |
2d76680f PC |
9386 | binary: |
9387 | for (i = 0; i < 2; ++i) | |
69eb4fde | 9388 | if (!RECUR (TREE_OPERAND (t, i), want_rval)) |
2d76680f PC |
9389 | return false; |
9390 | return true; | |
9391 | ||
2d76680f PC |
9392 | case VEC_PERM_EXPR: |
9393 | for (i = 0; i < 3; ++i) | |
69eb4fde | 9394 | if (!RECUR (TREE_OPERAND (t, i), true)) |
2d76680f PC |
9395 | return false; |
9396 | return true; | |
9397 | ||
9398 | case COND_EXPR: | |
b04445d4 | 9399 | if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20) |
d5bcd6d4 JM |
9400 | { |
9401 | if (flags & tf_error) | |
e40b6fc7 | 9402 | error_at (loc, "%<delete[]%> is not a constant expression"); |
d5bcd6d4 JM |
9403 | return false; |
9404 | } | |
9405 | /* Fall through. */ | |
9406 | case IF_STMT: | |
2d76680f PC |
9407 | case VEC_COND_EXPR: |
9408 | /* If the condition is a known constant, we know which of the legs we | |
9409 | care about; otherwise we only require that the condition and | |
9410 | either of the legs be potentially constant. */ | |
9411 | tmp = TREE_OPERAND (t, 0); | |
69eb4fde | 9412 | if (!RECUR (tmp, rval)) |
2d76680f PC |
9413 | return false; |
9414 | if (!processing_template_decl) | |
9415 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
117c6426 JJ |
9416 | /* potential_constant_expression* isn't told if it is called for |
9417 | manifestly_const_eval or not, so for consteval if always | |
9418 | process both branches as if the condition is not a known | |
9419 | constant. */ | |
9420 | if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t)) | |
9421 | { | |
9422 | if (integer_zerop (tmp)) | |
9423 | return RECUR (TREE_OPERAND (t, 2), want_rval); | |
9424 | else if (TREE_CODE (tmp) == INTEGER_CST) | |
9425 | return RECUR (TREE_OPERAND (t, 1), want_rval); | |
9426 | } | |
0fb7aa20 | 9427 | tmp = *jump_target; |
2d76680f | 9428 | for (i = 1; i < 3; ++i) |
0fb7aa20 JJ |
9429 | { |
9430 | tree this_jump_target = tmp; | |
9431 | if (potential_constant_expression_1 (TREE_OPERAND (t, i), | |
9432 | want_rval, strict, now, | |
9433 | tf_none, &this_jump_target)) | |
9434 | { | |
9435 | if (returns (&this_jump_target)) | |
9436 | *jump_target = this_jump_target; | |
9437 | else if (!returns (jump_target)) | |
9438 | { | |
9439 | if (breaks (&this_jump_target) | |
9440 | || continues (&this_jump_target)) | |
9441 | *jump_target = this_jump_target; | |
9442 | if (i == 1) | |
9443 | { | |
9444 | /* If the then branch is potentially constant, but | |
9445 | does not return, check if the else branch | |
9446 | couldn't return, break or continue. */ | |
9447 | hash_set<tree> pset; | |
9448 | check_for_return_continue_data data = { &pset, NULL_TREE, | |
9449 | NULL_TREE }; | |
9450 | if (tree ret_expr | |
9451 | = cp_walk_tree (&TREE_OPERAND (t, 2), | |
9452 | check_for_return_continue, &data, | |
9453 | &pset)) | |
9454 | *jump_target = ret_expr; | |
9455 | else if (*jump_target == NULL_TREE) | |
9456 | { | |
9457 | if (data.continue_stmt) | |
9458 | *jump_target = data.continue_stmt; | |
9459 | else if (data.break_stmt) | |
9460 | *jump_target = data.break_stmt; | |
9461 | } | |
9462 | } | |
9463 | } | |
9464 | return true; | |
9465 | } | |
9466 | } | |
2d76680f | 9467 | if (flags & tf_error) |
ff465bd8 PP |
9468 | { |
9469 | if (TREE_CODE (t) == IF_STMT) | |
9470 | error_at (loc, "neither branch of %<if%> is a constant expression"); | |
9471 | else | |
9472 | error_at (loc, "expression %qE is not a constant expression", t); | |
9473 | } | |
2d76680f PC |
9474 | return false; |
9475 | ||
9476 | case VEC_INIT_EXPR: | |
9477 | if (VEC_INIT_EXPR_IS_CONSTEXPR (t)) | |
9478 | return true; | |
9479 | if (flags & tf_error) | |
9480 | { | |
e40b6fc7 | 9481 | error_at (loc, "non-constant array initialization"); |
2d76680f PC |
9482 | diagnose_non_constexpr_vec_init (t); |
9483 | } | |
9484 | return false; | |
9485 | ||
133bc698 JM |
9486 | case TYPE_DECL: |
9487 | case TAG_DEFN: | |
9488 | /* We can see these in statement-expressions. */ | |
9489 | return true; | |
9490 | ||
baf9ebc8 | 9491 | case CLEANUP_STMT: |
1006c9d4 JJ |
9492 | if (!RECUR (CLEANUP_BODY (t), any)) |
9493 | return false; | |
9494 | if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any)) | |
9495 | return false; | |
9496 | return true; | |
9497 | ||
cda0a029 | 9498 | case EMPTY_CLASS_EXPR: |
46fdced6 | 9499 | return true; |
cda0a029 | 9500 | |
f937929e JM |
9501 | case GOTO_EXPR: |
9502 | { | |
9503 | tree *target = &TREE_OPERAND (t, 0); | |
02a981a8 JJ |
9504 | /* Gotos representing break, continue and cdtor return are OK. */ |
9505 | if (breaks (target) || continues (target) || returns (target)) | |
3075affd JM |
9506 | { |
9507 | *jump_target = *target; | |
9508 | return true; | |
9509 | } | |
ab3af181 | 9510 | if (flags & tf_error) |
e40b6fc7 | 9511 | error_at (loc, "%<goto%> is not a constant expression"); |
ab3af181 | 9512 | return false; |
f937929e JM |
9513 | } |
9514 | ||
6f20c42c MP |
9515 | case LABEL_EXPR: |
9516 | t = LABEL_EXPR_LABEL (t); | |
8892d532 | 9517 | if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23) |
6f20c42c MP |
9518 | return true; |
9519 | else if (flags & tf_error) | |
8892d532 JJ |
9520 | error_at (loc, "label definition in %<constexpr%> function only " |
9521 | "available with %<-std=c++2b%> or %<-std=gnu++2b%>"); | |
6f20c42c MP |
9522 | return false; |
9523 | ||
98e09245 | 9524 | case ANNOTATE_EXPR: |
98e09245 JJ |
9525 | return RECUR (TREE_OPERAND (t, 0), rval); |
9526 | ||
896048cf JJ |
9527 | case BIT_CAST_EXPR: |
9528 | return RECUR (TREE_OPERAND (t, 0), rval); | |
9529 | ||
49789fd0 IS |
9530 | /* Coroutine await, yield and return expressions are not. */ |
9531 | case CO_AWAIT_EXPR: | |
9532 | case CO_YIELD_EXPR: | |
9533 | case CO_RETURN_EXPR: | |
9534 | return false; | |
9535 | ||
d4c470c3 PP |
9536 | case NONTYPE_ARGUMENT_PACK: |
9537 | { | |
9538 | tree args = ARGUMENT_PACK_ARGS (t); | |
9539 | int len = TREE_VEC_LENGTH (args); | |
9540 | for (int i = 0; i < len; ++i) | |
9541 | if (!RECUR (TREE_VEC_ELT (args, i), any)) | |
9542 | return false; | |
9543 | return true; | |
9544 | } | |
9545 | ||
2d76680f | 9546 | default: |
878cffbd | 9547 | if (objc_non_constant_expr_p (t)) |
2d76680f PC |
9548 | return false; |
9549 | ||
9550 | sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t))); | |
ab3af181 | 9551 | gcc_unreachable (); |
2d76680f PC |
9552 | return false; |
9553 | } | |
69eb4fde | 9554 | #undef RECUR |
2d76680f PC |
9555 | } |
9556 | ||
3075affd JM |
9557 | bool |
9558 | potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, | |
9559 | tsubst_flags_t flags) | |
9560 | { | |
9927ecbb PP |
9561 | if (flags & tf_error) |
9562 | { | |
9563 | /* Check potentiality quietly first, as that could be performed more | |
9564 | efficiently in some cases (currently only for TRUTH_*_EXPR). If | |
9565 | that fails, replay the check noisily to give errors. */ | |
9566 | flags &= ~tf_error; | |
9567 | if (potential_constant_expression_1 (t, want_rval, strict, now, flags)) | |
9568 | return true; | |
9569 | flags |= tf_error; | |
9570 | } | |
9571 | ||
3075affd JM |
9572 | tree target = NULL_TREE; |
9573 | return potential_constant_expression_1 (t, want_rval, strict, now, | |
9574 | flags, &target); | |
9575 | } | |
9576 | ||
2d76680f PC |
9577 | /* The main entry point to the above. */ |
9578 | ||
9579 | bool | |
9580 | potential_constant_expression (tree t) | |
9581 | { | |
a0ab7ccd | 9582 | return potential_constant_expression_1 (t, false, true, false, tf_none); |
2d76680f PC |
9583 | } |
9584 | ||
9585 | /* As above, but require a constant rvalue. */ | |
9586 | ||
9587 | bool | |
9588 | potential_rvalue_constant_expression (tree t) | |
9589 | { | |
a0ab7ccd | 9590 | return potential_constant_expression_1 (t, true, true, false, tf_none); |
2d76680f PC |
9591 | } |
9592 | ||
9593 | /* Like above, but complain about non-constant expressions. */ | |
9594 | ||
9595 | bool | |
9596 | require_potential_constant_expression (tree t) | |
9597 | { | |
d8cff23f MP |
9598 | return potential_constant_expression_1 (t, false, true, false, |
9599 | tf_warning_or_error); | |
2d76680f PC |
9600 | } |
9601 | ||
9602 | /* Cross product of the above. */ | |
9603 | ||
9604 | bool | |
9605 | require_potential_rvalue_constant_expression (tree t) | |
9606 | { | |
d8cff23f MP |
9607 | return potential_constant_expression_1 (t, true, true, false, |
9608 | tf_warning_or_error); | |
9609 | } | |
9610 | ||
9611 | /* Like above, but don't consider PARM_DECL a potential_constant_expression. */ | |
9612 | ||
9613 | bool | |
9614 | require_rvalue_constant_expression (tree t) | |
9615 | { | |
9616 | return potential_constant_expression_1 (t, true, true, true, | |
9617 | tf_warning_or_error); | |
a0ab7ccd JM |
9618 | } |
9619 | ||
9620 | /* Like potential_constant_expression, but don't consider possible constexpr | |
9621 | substitution of the current function. That is, PARM_DECL qualifies under | |
9622 | potential_constant_expression, but not here. | |
9623 | ||
9624 | This is basically what you can check when any actual constant values might | |
9625 | be value-dependent. */ | |
9626 | ||
9627 | bool | |
9628 | is_constant_expression (tree t) | |
9629 | { | |
9630 | return potential_constant_expression_1 (t, false, true, true, tf_none); | |
9631 | } | |
9632 | ||
beb019d3 JM |
9633 | /* As above, but expect an rvalue. */ |
9634 | ||
9635 | bool | |
9636 | is_rvalue_constant_expression (tree t) | |
9637 | { | |
9638 | return potential_constant_expression_1 (t, true, true, true, tf_none); | |
9639 | } | |
9640 | ||
a0ab7ccd JM |
9641 | /* Like above, but complain about non-constant expressions. */ |
9642 | ||
9643 | bool | |
9644 | require_constant_expression (tree t) | |
9645 | { | |
9646 | return potential_constant_expression_1 (t, false, true, true, | |
9647 | tf_warning_or_error); | |
9648 | } | |
9649 | ||
9650 | /* Like is_constant_expression, but allow const variables that are not allowed | |
9651 | under constexpr rules. */ | |
9652 | ||
9653 | bool | |
9654 | is_static_init_expression (tree t) | |
9655 | { | |
9656 | return potential_constant_expression_1 (t, false, false, true, tf_none); | |
2d76680f PC |
9657 | } |
9658 | ||
eb07f187 JM |
9659 | /* Returns true if T is a potential constant expression that is not |
9660 | instantiation-dependent, and therefore a candidate for constant folding even | |
9661 | in a template. */ | |
9662 | ||
9663 | bool | |
a0ab7ccd | 9664 | is_nondependent_constant_expression (tree t) |
eb07f187 JM |
9665 | { |
9666 | return (!type_unknown_p (t) | |
a0ab7ccd | 9667 | && is_constant_expression (t) |
eb07f187 JM |
9668 | && !instantiation_dependent_expression_p (t)); |
9669 | } | |
9670 | ||
9671 | /* Returns true if T is a potential static initializer expression that is not | |
9672 | instantiation-dependent. */ | |
9673 | ||
9674 | bool | |
a0ab7ccd | 9675 | is_nondependent_static_init_expression (tree t) |
eb07f187 JM |
9676 | { |
9677 | return (!type_unknown_p (t) | |
a0ab7ccd | 9678 | && is_static_init_expression (t) |
eb07f187 JM |
9679 | && !instantiation_dependent_expression_p (t)); |
9680 | } | |
9681 | ||
87c2080b JM |
9682 | /* True iff FN is an implicitly constexpr function. */ |
9683 | ||
9684 | bool | |
9685 | decl_implicit_constexpr_p (tree fn) | |
9686 | { | |
9687 | if (!(flag_implicit_constexpr | |
9688 | && TREE_CODE (fn) == FUNCTION_DECL | |
9689 | && DECL_DECLARED_CONSTEXPR_P (fn))) | |
9690 | return false; | |
9691 | ||
9692 | if (DECL_CLONED_FUNCTION_P (fn)) | |
9693 | fn = DECL_CLONED_FUNCTION (fn); | |
9694 | ||
9695 | return (DECL_LANG_SPECIFIC (fn) | |
9696 | && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr); | |
9697 | } | |
9698 | ||
97f3003f JM |
9699 | /* Finalize constexpr processing after parsing. */ |
9700 | ||
9701 | void | |
9702 | fini_constexpr (void) | |
9703 | { | |
9704 | /* The contexpr call and fundef copies tables are no longer needed. */ | |
9705 | constexpr_call_table = NULL; | |
9706 | fundef_copies_table = NULL; | |
9707 | } | |
9708 | ||
2d76680f | 9709 | #include "gt-cp-constexpr.h" |