]>
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" |
44a845ca | 34 | #include "gimple-fold.h" |
8108ea30 | 35 | #include "timevar.h" |
d8fcab68 | 36 | #include "fold-const-call.h" |
8e007055 | 37 | #include "stor-layout.h" |
a8db7887 | 38 | #include "cgraph.h" |
00f34291 | 39 | #include "opts.h" |
87c2080b JM |
40 | #include "stringpool.h" |
41 | #include "attribs.h" | |
2d76680f PC |
42 | |
43 | static bool verify_constant (tree, bool, bool *, bool *); | |
44 | #define VERIFY_CONSTANT(X) \ | |
45 | do { \ | |
2b3ab879 | 46 | if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \ |
2d76680f PC |
47 | return t; \ |
48 | } while (0) | |
49 | ||
582d2481 JJ |
50 | static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex, |
51 | bool insert = false); | |
baf05d54 | 52 | static int array_index_cmp (tree key, tree index); |
582d2481 | 53 | |
2d76680f PC |
54 | /* Returns true iff FUN is an instantiation of a constexpr function |
55 | template or a defaulted constexpr function. */ | |
56 | ||
57 | bool | |
58 | is_instantiation_of_constexpr (tree fun) | |
59 | { | |
60 | return ((DECL_TEMPLOID_INSTANTIATION (fun) | |
61 | && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun))) | |
62 | || (DECL_DEFAULTED_FN (fun) | |
63 | && DECL_DECLARED_CONSTEXPR_P (fun))); | |
64 | } | |
65 | ||
66 | /* Return true if T is a literal type. */ | |
67 | ||
68 | bool | |
69 | literal_type_p (tree t) | |
70 | { | |
71 | if (SCALAR_TYPE_P (t) | |
b55b02ea | 72 | || VECTOR_TYPE_P (t) |
9f613f06 | 73 | || TYPE_REF_P (t) |
e42c407c | 74 | || (VOID_TYPE_P (t) && cxx_dialect >= cxx14)) |
2d76680f PC |
75 | return true; |
76 | if (CLASS_TYPE_P (t)) | |
77 | { | |
78 | t = complete_type (t); | |
79 | gcc_assert (COMPLETE_TYPE_P (t) || errorcount); | |
80 | return CLASSTYPE_LITERAL_P (t); | |
81 | } | |
82 | if (TREE_CODE (t) == ARRAY_TYPE) | |
83 | return literal_type_p (strip_array_types (t)); | |
84 | return false; | |
85 | } | |
86 | ||
87 | /* If DECL is a variable declared `constexpr', require its type | |
9638f320 PC |
88 | be literal. Return error_mark_node if we give an error, the |
89 | DECL otherwise. */ | |
2d76680f PC |
90 | |
91 | tree | |
92 | ensure_literal_type_for_constexpr_object (tree decl) | |
93 | { | |
94 | tree type = TREE_TYPE (decl); | |
95 | if (VAR_P (decl) | |
96 | && (DECL_DECLARED_CONSTEXPR_P (decl) | |
97 | || var_in_constexpr_fn (decl)) | |
98 | && !processing_template_decl) | |
99 | { | |
100 | tree stype = strip_array_types (type); | |
101 | if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype))) | |
102 | /* Don't complain here, we'll complain about incompleteness | |
103 | when we try to initialize the variable. */; | |
104 | else if (!literal_type_p (type)) | |
105 | { | |
106 | if (DECL_DECLARED_CONSTEXPR_P (decl)) | |
f1eac182 | 107 | { |
097f82ec | 108 | auto_diagnostic_group d; |
f2935576 PC |
109 | error_at (DECL_SOURCE_LOCATION (decl), |
110 | "the type %qT of %<constexpr%> variable %qD " | |
111 | "is not literal", type, decl); | |
f1eac182 | 112 | explain_non_literal_class (type); |
9638f320 | 113 | decl = error_mark_node; |
f1eac182 | 114 | } |
8892d532 | 115 | else if (cxx_dialect < cxx23) |
60813a46 | 116 | { |
5d4e573b | 117 | if (!is_instantiation_of_constexpr (current_function_decl)) |
f1eac182 | 118 | { |
097f82ec | 119 | auto_diagnostic_group d; |
f2935576 PC |
120 | error_at (DECL_SOURCE_LOCATION (decl), |
121 | "variable %qD of non-literal type %qT in " | |
8892d532 JJ |
122 | "%<constexpr%> function only available with " |
123 | "%<-std=c++2b%> or %<-std=gnu++2b%>", decl, type); | |
f1eac182 | 124 | explain_non_literal_class (type); |
9638f320 | 125 | decl = error_mark_node; |
f1eac182 | 126 | } |
60813a46 JM |
127 | cp_function_chain->invalid_constexpr = true; |
128 | } | |
2d76680f | 129 | } |
8e9589bd JM |
130 | else if (DECL_DECLARED_CONSTEXPR_P (decl) |
131 | && variably_modified_type_p (type, NULL_TREE)) | |
132 | { | |
f2935576 PC |
133 | error_at (DECL_SOURCE_LOCATION (decl), |
134 | "%<constexpr%> variable %qD has variably-modified " | |
135 | "type %qT", decl, type); | |
8e9589bd JM |
136 | decl = error_mark_node; |
137 | } | |
2d76680f PC |
138 | } |
139 | return decl; | |
140 | } | |
141 | ||
ca752f39 | 142 | struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef> |
2d76680f | 143 | { |
bfc139e2 NS |
144 | static hashval_t hash (const constexpr_fundef *); |
145 | static bool equal (const constexpr_fundef *, const constexpr_fundef *); | |
2d76680f PC |
146 | }; |
147 | ||
148 | /* This table holds all constexpr function definitions seen in | |
149 | the current translation unit. */ | |
150 | ||
151 | static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table; | |
152 | ||
153 | /* Utility function used for managing the constexpr function table. | |
154 | Return true if the entries pointed to by P and Q are for the | |
155 | same constexpr function. */ | |
156 | ||
157 | inline bool | |
bfc139e2 NS |
158 | constexpr_fundef_hasher::equal (const constexpr_fundef *lhs, |
159 | const constexpr_fundef *rhs) | |
2d76680f PC |
160 | { |
161 | return lhs->decl == rhs->decl; | |
162 | } | |
163 | ||
164 | /* Utility function used for managing the constexpr function table. | |
165 | Return a hash value for the entry pointed to by Q. */ | |
166 | ||
167 | inline hashval_t | |
bfc139e2 | 168 | constexpr_fundef_hasher::hash (const constexpr_fundef *fundef) |
2d76680f PC |
169 | { |
170 | return DECL_UID (fundef->decl); | |
171 | } | |
172 | ||
173 | /* Return a previously saved definition of function FUN. */ | |
174 | ||
bfc139e2 | 175 | constexpr_fundef * |
2d76680f PC |
176 | retrieve_constexpr_fundef (tree fun) |
177 | { | |
2d76680f PC |
178 | if (constexpr_fundef_table == NULL) |
179 | return NULL; | |
180 | ||
bfc139e2 | 181 | constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE }; |
2d76680f PC |
182 | return constexpr_fundef_table->find (&fundef); |
183 | } | |
184 | ||
185 | /* Check whether the parameter and return types of FUN are valid for a | |
186 | constexpr function, and complain if COMPLAIN. */ | |
187 | ||
98e5a19a | 188 | bool |
2d76680f PC |
189 | is_valid_constexpr_fn (tree fun, bool complain) |
190 | { | |
191 | bool ret = true; | |
192 | ||
31f7f784 | 193 | if (DECL_INHERITED_CTOR (fun) |
2d76680f PC |
194 | && TREE_CODE (fun) == TEMPLATE_DECL) |
195 | { | |
196 | ret = false; | |
197 | if (complain) | |
84fa214d | 198 | error ("inherited constructor %qD is not %<constexpr%>", |
31f7f784 | 199 | DECL_INHERITED_CTOR (fun)); |
2d76680f PC |
200 | } |
201 | else | |
202 | { | |
203 | for (tree parm = FUNCTION_FIRST_USER_PARM (fun); | |
204 | parm != NULL_TREE; parm = TREE_CHAIN (parm)) | |
205 | if (!literal_type_p (TREE_TYPE (parm))) | |
206 | { | |
207 | ret = false; | |
208 | if (complain) | |
209 | { | |
097f82ec | 210 | auto_diagnostic_group d; |
84fa214d | 211 | error ("invalid type for parameter %d of %<constexpr%> " |
2d76680f PC |
212 | "function %q+#D", DECL_PARM_INDEX (parm), fun); |
213 | explain_non_literal_class (TREE_TYPE (parm)); | |
214 | } | |
215 | } | |
216 | } | |
217 | ||
e1bea341 JM |
218 | if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17) |
219 | { | |
220 | ret = false; | |
221 | if (complain) | |
222 | inform (DECL_SOURCE_LOCATION (fun), | |
84fa214d | 223 | "lambdas are implicitly %<constexpr%> only in C++17 and later"); |
e1bea341 | 224 | } |
87c2080b JM |
225 | else if (DECL_DESTRUCTOR_P (fun)) |
226 | { | |
227 | if (cxx_dialect < cxx20) | |
228 | { | |
229 | ret = false; | |
230 | if (complain) | |
231 | error_at (DECL_SOURCE_LOCATION (fun), | |
232 | "%<constexpr%> destructors only available" | |
233 | " with %<-std=c++20%> or %<-std=gnu++20%>"); | |
234 | } | |
235 | } | |
e1bea341 | 236 | else if (!DECL_CONSTRUCTOR_P (fun)) |
2d76680f PC |
237 | { |
238 | tree rettype = TREE_TYPE (TREE_TYPE (fun)); | |
239 | if (!literal_type_p (rettype)) | |
240 | { | |
241 | ret = false; | |
242 | if (complain) | |
243 | { | |
097f82ec | 244 | auto_diagnostic_group d; |
84fa214d | 245 | error ("invalid return type %qT of %<constexpr%> function %q+D", |
2d76680f PC |
246 | rettype, fun); |
247 | explain_non_literal_class (rettype); | |
248 | } | |
249 | } | |
250 | ||
54069e59 JM |
251 | /* C++14 DR 1684 removed this restriction. */ |
252 | if (cxx_dialect < cxx14 | |
253 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) | |
2d76680f PC |
254 | && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun))) |
255 | { | |
256 | ret = false; | |
097f82ec DM |
257 | if (complain) |
258 | { | |
259 | auto_diagnostic_group d; | |
260 | if (pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic, | |
261 | "enclosing class of %<constexpr%> non-static" | |
262 | " member function %q+#D is not a literal type", | |
263 | fun)) | |
264 | explain_non_literal_class (DECL_CONTEXT (fun)); | |
265 | } | |
2d76680f PC |
266 | } |
267 | } | |
268 | else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun))) | |
269 | { | |
270 | ret = false; | |
271 | if (complain) | |
272 | error ("%q#T has virtual base classes", DECL_CONTEXT (fun)); | |
273 | } | |
274 | ||
275 | return ret; | |
276 | } | |
277 | ||
278 | /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF | |
279 | for a member of an anonymous aggregate, INIT is the initializer for that | |
280 | member, and VEC_OUTER is the vector of constructor elements for the class | |
281 | whose constructor we are processing. Add the initializer to the vector | |
282 | and return true to indicate success. */ | |
283 | ||
284 | static bool | |
285 | build_anon_member_initialization (tree member, tree init, | |
286 | vec<constructor_elt, va_gc> **vec_outer) | |
287 | { | |
288 | /* MEMBER presents the relevant fields from the inside out, but we need | |
289 | to build up the initializer from the outside in so that we can reuse | |
290 | previously built CONSTRUCTORs if this is, say, the second field in an | |
291 | anonymous struct. So we use a vec as a stack. */ | |
292 | auto_vec<tree, 2> fields; | |
293 | do | |
294 | { | |
295 | fields.safe_push (TREE_OPERAND (member, 1)); | |
296 | member = TREE_OPERAND (member, 0); | |
297 | } | |
298 | while (ANON_AGGR_TYPE_P (TREE_TYPE (member)) | |
299 | && TREE_CODE (member) == COMPONENT_REF); | |
300 | ||
301 | /* VEC has the constructor elements vector for the context of FIELD. | |
302 | If FIELD is an anonymous aggregate, we will push inside it. */ | |
303 | vec<constructor_elt, va_gc> **vec = vec_outer; | |
304 | tree field; | |
305 | while (field = fields.pop(), | |
306 | ANON_AGGR_TYPE_P (TREE_TYPE (field))) | |
307 | { | |
308 | tree ctor; | |
309 | /* If there is already an outer constructor entry for the anonymous | |
310 | aggregate FIELD, use it; otherwise, insert one. */ | |
311 | if (vec_safe_is_empty (*vec) | |
312 | || (*vec)->last().index != field) | |
313 | { | |
314 | ctor = build_constructor (TREE_TYPE (field), NULL); | |
315 | CONSTRUCTOR_APPEND_ELT (*vec, field, ctor); | |
316 | } | |
317 | else | |
318 | ctor = (*vec)->last().value; | |
319 | vec = &CONSTRUCTOR_ELTS (ctor); | |
320 | } | |
321 | ||
322 | /* Now we're at the innermost field, the one that isn't an anonymous | |
323 | aggregate. Add its initializer to the CONSTRUCTOR and we're done. */ | |
324 | gcc_assert (fields.is_empty()); | |
325 | CONSTRUCTOR_APPEND_ELT (*vec, field, init); | |
326 | ||
327 | return true; | |
328 | } | |
329 | ||
330 | /* Subroutine of build_constexpr_constructor_member_initializers. | |
331 | The expression tree T represents a data member initialization | |
332 | in a (constexpr) constructor definition. Build a pairing of | |
333 | the data member with its initializer, and prepend that pair | |
334 | to the existing initialization pair INITS. */ | |
335 | ||
336 | static bool | |
337 | build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec) | |
338 | { | |
339 | tree member, init; | |
340 | if (TREE_CODE (t) == CLEANUP_POINT_EXPR) | |
341 | t = TREE_OPERAND (t, 0); | |
342 | if (TREE_CODE (t) == EXPR_STMT) | |
343 | t = TREE_OPERAND (t, 0); | |
344 | if (t == error_mark_node) | |
345 | return false; | |
346 | if (TREE_CODE (t) == STATEMENT_LIST) | |
347 | { | |
0f54cc9c JM |
348 | for (tree stmt : tsi_range (t)) |
349 | if (! build_data_member_initialization (stmt, vec)) | |
350 | return false; | |
2d76680f PC |
351 | return true; |
352 | } | |
353 | if (TREE_CODE (t) == CLEANUP_STMT) | |
354 | { | |
355 | /* We can't see a CLEANUP_STMT in a constructor for a literal class, | |
356 | but we can in a constexpr constructor for a non-literal class. Just | |
357 | ignore it; either all the initialization will be constant, in which | |
358 | case the cleanup can't run, or it can't be constexpr. | |
359 | Still recurse into CLEANUP_BODY. */ | |
360 | return build_data_member_initialization (CLEANUP_BODY (t), vec); | |
361 | } | |
362 | if (TREE_CODE (t) == CONVERT_EXPR) | |
363 | t = TREE_OPERAND (t, 0); | |
364 | if (TREE_CODE (t) == INIT_EXPR | |
60813a46 JM |
365 | /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only |
366 | use what this function builds for cx_check_missing_mem_inits, and | |
367 | assignment in the ctor body doesn't count. */ | |
368 | || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR)) | |
2d76680f PC |
369 | { |
370 | member = TREE_OPERAND (t, 0); | |
371 | init = break_out_target_exprs (TREE_OPERAND (t, 1)); | |
372 | } | |
373 | else if (TREE_CODE (t) == CALL_EXPR) | |
374 | { | |
60813a46 JM |
375 | tree fn = get_callee_fndecl (t); |
376 | if (!fn || !DECL_CONSTRUCTOR_P (fn)) | |
377 | /* We're only interested in calls to subobject constructors. */ | |
378 | return true; | |
2d76680f PC |
379 | member = CALL_EXPR_ARG (t, 0); |
380 | /* We don't use build_cplus_new here because it complains about | |
381 | abstract bases. Leaving the call unwrapped means that it has the | |
382 | wrong type, but cxx_eval_constant_expression doesn't care. */ | |
383 | init = break_out_target_exprs (t); | |
384 | } | |
385 | else if (TREE_CODE (t) == BIND_EXPR) | |
386 | return build_data_member_initialization (BIND_EXPR_BODY (t), vec); | |
2d76680f | 387 | else |
60813a46 JM |
388 | /* Don't add anything else to the CONSTRUCTOR. */ |
389 | return true; | |
2d76680f PC |
390 | if (INDIRECT_REF_P (member)) |
391 | member = TREE_OPERAND (member, 0); | |
392 | if (TREE_CODE (member) == NOP_EXPR) | |
393 | { | |
394 | tree op = member; | |
395 | STRIP_NOPS (op); | |
396 | if (TREE_CODE (op) == ADDR_EXPR) | |
397 | { | |
398 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
399 | (TREE_TYPE (TREE_TYPE (op)), | |
400 | TREE_TYPE (TREE_TYPE (member)))); | |
401 | /* Initializing a cv-qualified member; we need to look through | |
402 | the const_cast. */ | |
403 | member = op; | |
404 | } | |
405 | else if (op == current_class_ptr | |
406 | && (same_type_ignoring_top_level_qualifiers_p | |
407 | (TREE_TYPE (TREE_TYPE (member)), | |
408 | current_class_type))) | |
409 | /* Delegating constructor. */ | |
410 | member = op; | |
411 | else | |
412 | { | |
413 | /* This is an initializer for an empty base; keep it for now so | |
414 | we can check it in cxx_eval_bare_aggregate. */ | |
415 | gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member)))); | |
416 | } | |
417 | } | |
418 | if (TREE_CODE (member) == ADDR_EXPR) | |
419 | member = TREE_OPERAND (member, 0); | |
420 | if (TREE_CODE (member) == COMPONENT_REF) | |
421 | { | |
422 | tree aggr = TREE_OPERAND (member, 0); | |
8cb7aaa1 JM |
423 | if (TREE_CODE (aggr) == VAR_DECL) |
424 | /* Initializing a local variable, don't add anything. */ | |
425 | return true; | |
2d76680f PC |
426 | if (TREE_CODE (aggr) != COMPONENT_REF) |
427 | /* Normal member initialization. */ | |
428 | member = TREE_OPERAND (member, 1); | |
429 | else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr))) | |
430 | /* Initializing a member of an anonymous union. */ | |
431 | return build_anon_member_initialization (member, init, vec); | |
432 | else | |
433 | /* We're initializing a vtable pointer in a base. Leave it as | |
434 | COMPONENT_REF so we remember the path to get to the vfield. */ | |
435 | gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node); | |
436 | } | |
437 | ||
49b5925f JM |
438 | /* Value-initialization can produce multiple initializers for the |
439 | same field; use the last one. */ | |
440 | if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member) | |
441 | (*vec)->last().value = init; | |
442 | else | |
443 | CONSTRUCTOR_APPEND_ELT (*vec, member, init); | |
2d76680f PC |
444 | return true; |
445 | } | |
446 | ||
447 | /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval. | |
448 | In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a | |
449 | BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */ | |
450 | ||
451 | static bool | |
452 | check_constexpr_bind_expr_vars (tree t) | |
453 | { | |
454 | gcc_assert (TREE_CODE (t) == BIND_EXPR); | |
455 | ||
2d76680f PC |
456 | for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var)) |
457 | if (TREE_CODE (var) == TYPE_DECL | |
4414e22e PC |
458 | && DECL_IMPLICIT_TYPEDEF_P (var) |
459 | && !LAMBDA_TYPE_P (TREE_TYPE (var))) | |
2d76680f PC |
460 | return false; |
461 | return true; | |
462 | } | |
463 | ||
464 | /* Subroutine of check_constexpr_ctor_body. */ | |
465 | ||
466 | static bool | |
467 | check_constexpr_ctor_body_1 (tree last, tree list) | |
468 | { | |
469 | switch (TREE_CODE (list)) | |
470 | { | |
471 | case DECL_EXPR: | |
a0008434 MP |
472 | if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL |
473 | || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL) | |
2d76680f | 474 | return true; |
2d76680f PC |
475 | return false; |
476 | ||
477 | case CLEANUP_POINT_EXPR: | |
478 | return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0), | |
479 | /*complain=*/false); | |
480 | ||
481 | case BIND_EXPR: | |
482 | if (!check_constexpr_bind_expr_vars (list) | |
483 | || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list), | |
484 | /*complain=*/false)) | |
485 | return false; | |
486 | return true; | |
487 | ||
488 | case USING_STMT: | |
489 | case STATIC_ASSERT: | |
96a95ac1 | 490 | case DEBUG_BEGIN_STMT: |
2d76680f PC |
491 | return true; |
492 | ||
493 | default: | |
494 | return false; | |
495 | } | |
496 | } | |
497 | ||
498 | /* Make sure that there are no statements after LAST in the constructor | |
499 | body represented by LIST. */ | |
500 | ||
501 | bool | |
502 | check_constexpr_ctor_body (tree last, tree list, bool complain) | |
503 | { | |
60813a46 JM |
504 | /* C++14 doesn't require a constexpr ctor to have an empty body. */ |
505 | if (cxx_dialect >= cxx14) | |
506 | return true; | |
507 | ||
2d76680f PC |
508 | bool ok = true; |
509 | if (TREE_CODE (list) == STATEMENT_LIST) | |
510 | { | |
511 | tree_stmt_iterator i = tsi_last (list); | |
512 | for (; !tsi_end_p (i); tsi_prev (&i)) | |
513 | { | |
514 | tree t = tsi_stmt (i); | |
515 | if (t == last) | |
516 | break; | |
517 | if (!check_constexpr_ctor_body_1 (last, t)) | |
518 | { | |
519 | ok = false; | |
520 | break; | |
521 | } | |
522 | } | |
523 | } | |
524 | else if (list != last | |
525 | && !check_constexpr_ctor_body_1 (last, list)) | |
526 | ok = false; | |
527 | if (!ok) | |
528 | { | |
529 | if (complain) | |
84fa214d | 530 | error ("%<constexpr%> constructor does not have empty body"); |
2d76680f PC |
531 | DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false; |
532 | } | |
533 | return ok; | |
534 | } | |
535 | ||
536 | /* V is a vector of constructor elements built up for the base and member | |
537 | initializers of a constructor for TYPE. They need to be in increasing | |
538 | offset order, which they might not be yet if TYPE has a primary base | |
539 | which is not first in the base-clause or a vptr and at least one base | |
540 | all of which are non-primary. */ | |
541 | ||
542 | static vec<constructor_elt, va_gc> * | |
543 | sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v) | |
544 | { | |
545 | tree pri = CLASSTYPE_PRIMARY_BINFO (type); | |
546 | tree field_type; | |
547 | unsigned i; | |
548 | constructor_elt *ce; | |
549 | ||
550 | if (pri) | |
551 | field_type = BINFO_TYPE (pri); | |
552 | else if (TYPE_CONTAINS_VPTR_P (type)) | |
553 | field_type = vtbl_ptr_type_node; | |
554 | else | |
555 | return v; | |
556 | ||
557 | /* Find the element for the primary base or vptr and move it to the | |
558 | beginning of the vec. */ | |
559 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
560 | if (TREE_TYPE (ce->index) == field_type) | |
561 | break; | |
562 | ||
563 | if (i > 0 && i < vec_safe_length (v)) | |
564 | { | |
565 | vec<constructor_elt, va_gc> &vref = *v; | |
566 | constructor_elt elt = vref[i]; | |
567 | for (; i > 0; --i) | |
568 | vref[i] = vref[i-1]; | |
569 | vref[0] = elt; | |
570 | } | |
571 | ||
572 | return v; | |
573 | } | |
574 | ||
575 | /* Build compile-time evalable representations of member-initializer list | |
576 | for a constexpr constructor. */ | |
577 | ||
578 | static tree | |
579 | build_constexpr_constructor_member_initializers (tree type, tree body) | |
580 | { | |
581 | vec<constructor_elt, va_gc> *vec = NULL; | |
582 | bool ok = true; | |
fa837fb6 JM |
583 | while (true) |
584 | switch (TREE_CODE (body)) | |
585 | { | |
586 | case MUST_NOT_THROW_EXPR: | |
587 | case EH_SPEC_BLOCK: | |
588 | body = TREE_OPERAND (body, 0); | |
589 | break; | |
590 | ||
591 | case STATEMENT_LIST: | |
0f54cc9c | 592 | for (tree stmt : tsi_range (body)) |
fa837fb6 | 593 | { |
0f54cc9c | 594 | body = stmt; |
fa837fb6 JM |
595 | if (TREE_CODE (body) == BIND_EXPR) |
596 | break; | |
597 | } | |
598 | break; | |
599 | ||
600 | case BIND_EXPR: | |
601 | body = BIND_EXPR_BODY (body); | |
602 | goto found; | |
603 | ||
604 | default: | |
605 | gcc_unreachable (); | |
58cc255c | 606 | } |
fa837fb6 | 607 | found: |
1259cb6d JJ |
608 | if (TREE_CODE (body) == TRY_BLOCK) |
609 | { | |
610 | body = TREE_OPERAND (body, 0); | |
611 | if (TREE_CODE (body) == BIND_EXPR) | |
612 | body = BIND_EXPR_BODY (body); | |
613 | } | |
2d76680f PC |
614 | if (TREE_CODE (body) == CLEANUP_POINT_EXPR) |
615 | { | |
616 | body = TREE_OPERAND (body, 0); | |
617 | if (TREE_CODE (body) == EXPR_STMT) | |
618 | body = TREE_OPERAND (body, 0); | |
619 | if (TREE_CODE (body) == INIT_EXPR | |
620 | && (same_type_ignoring_top_level_qualifiers_p | |
621 | (TREE_TYPE (TREE_OPERAND (body, 0)), | |
622 | current_class_type))) | |
623 | { | |
624 | /* Trivial copy. */ | |
625 | return TREE_OPERAND (body, 1); | |
626 | } | |
627 | ok = build_data_member_initialization (body, &vec); | |
628 | } | |
629 | else if (TREE_CODE (body) == STATEMENT_LIST) | |
630 | { | |
0f54cc9c | 631 | for (tree stmt : tsi_range (body)) |
2d76680f | 632 | { |
0f54cc9c | 633 | ok = build_data_member_initialization (stmt, &vec); |
2d76680f PC |
634 | if (!ok) |
635 | break; | |
636 | } | |
637 | } | |
2d76680f PC |
638 | else if (EXPR_P (body)) |
639 | ok = build_data_member_initialization (body, &vec); | |
640 | else | |
641 | gcc_assert (errorcount > 0); | |
642 | if (ok) | |
643 | { | |
644 | if (vec_safe_length (vec) > 0) | |
645 | { | |
646 | /* In a delegating constructor, return the target. */ | |
647 | constructor_elt *ce = &(*vec)[0]; | |
648 | if (ce->index == current_class_ptr) | |
649 | { | |
650 | body = ce->value; | |
651 | vec_free (vec); | |
652 | return body; | |
653 | } | |
654 | } | |
655 | vec = sort_constexpr_mem_initializers (type, vec); | |
656 | return build_constructor (type, vec); | |
657 | } | |
658 | else | |
659 | return error_mark_node; | |
660 | } | |
661 | ||
1b6fa695 ML |
662 | /* We have an expression tree T that represents a call, either CALL_EXPR |
663 | or AGGR_INIT_EXPR. If the call is lexically to a named function, | |
664 | retrun the _DECL for that function. */ | |
665 | ||
666 | static tree | |
667 | get_function_named_in_call (tree t) | |
668 | { | |
669 | tree fun = cp_get_callee (t); | |
670 | if (fun && TREE_CODE (fun) == ADDR_EXPR | |
671 | && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL) | |
672 | fun = TREE_OPERAND (fun, 0); | |
673 | return fun; | |
674 | } | |
675 | ||
bfc139e2 | 676 | /* Subroutine of check_constexpr_fundef. BODY is the body of a function |
2d76680f PC |
677 | declared to be constexpr, or a sub-statement thereof. Returns the |
678 | return value if suitable, error_mark_node for a statement not allowed in | |
679 | a constexpr function, or NULL_TREE if no return value was found. */ | |
680 | ||
5afef8b1 | 681 | tree |
2d76680f PC |
682 | constexpr_fn_retval (tree body) |
683 | { | |
684 | switch (TREE_CODE (body)) | |
685 | { | |
686 | case STATEMENT_LIST: | |
687 | { | |
2d76680f | 688 | tree expr = NULL_TREE; |
0f54cc9c | 689 | for (tree stmt : tsi_range (body)) |
2d76680f | 690 | { |
0f54cc9c | 691 | tree s = constexpr_fn_retval (stmt); |
2d76680f PC |
692 | if (s == error_mark_node) |
693 | return error_mark_node; | |
694 | else if (s == NULL_TREE) | |
695 | /* Keep iterating. */; | |
696 | else if (expr) | |
697 | /* Multiple return statements. */ | |
698 | return error_mark_node; | |
699 | else | |
700 | expr = s; | |
701 | } | |
702 | return expr; | |
703 | } | |
704 | ||
705 | case RETURN_EXPR: | |
706 | return break_out_target_exprs (TREE_OPERAND (body, 0)); | |
707 | ||
708 | case DECL_EXPR: | |
0162cb3b PC |
709 | { |
710 | tree decl = DECL_EXPR_DECL (body); | |
711 | if (TREE_CODE (decl) == USING_DECL | |
712 | /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */ | |
713 | || DECL_ARTIFICIAL (decl)) | |
714 | return NULL_TREE; | |
715 | return error_mark_node; | |
716 | } | |
2d76680f PC |
717 | |
718 | case CLEANUP_POINT_EXPR: | |
719 | return constexpr_fn_retval (TREE_OPERAND (body, 0)); | |
720 | ||
721 | case BIND_EXPR: | |
722 | if (!check_constexpr_bind_expr_vars (body)) | |
723 | return error_mark_node; | |
724 | return constexpr_fn_retval (BIND_EXPR_BODY (body)); | |
725 | ||
726 | case USING_STMT: | |
96a95ac1 | 727 | case DEBUG_BEGIN_STMT: |
2d76680f PC |
728 | return NULL_TREE; |
729 | ||
1b6fa695 ML |
730 | case CALL_EXPR: |
731 | { | |
732 | tree fun = get_function_named_in_call (body); | |
733 | if (fun != NULL_TREE | |
3d78e008 | 734 | && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE)) |
1b6fa695 ML |
735 | return NULL_TREE; |
736 | } | |
737 | /* Fallthru. */ | |
738 | ||
2d76680f PC |
739 | default: |
740 | return error_mark_node; | |
741 | } | |
742 | } | |
743 | ||
bfc139e2 | 744 | /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of |
2d76680f PC |
745 | FUN; do the necessary transformations to turn it into a single expression |
746 | that we can store in the hash table. */ | |
747 | ||
748 | static tree | |
749 | massage_constexpr_body (tree fun, tree body) | |
750 | { | |
751 | if (DECL_CONSTRUCTOR_P (fun)) | |
752 | body = build_constexpr_constructor_member_initializers | |
753 | (DECL_CONTEXT (fun), body); | |
60813a46 | 754 | else if (cxx_dialect < cxx14) |
2d76680f PC |
755 | { |
756 | if (TREE_CODE (body) == EH_SPEC_BLOCK) | |
757 | body = EH_SPEC_STMTS (body); | |
758 | if (TREE_CODE (body) == MUST_NOT_THROW_EXPR) | |
759 | body = TREE_OPERAND (body, 0); | |
760 | body = constexpr_fn_retval (body); | |
761 | } | |
762 | return body; | |
763 | } | |
764 | ||
3e4b91f2 NS |
765 | /* CTYPE is a type constructed from BODY. Return true if some |
766 | bases/fields are uninitialized, and complain if COMPLAIN. */ | |
2d76680f PC |
767 | |
768 | static bool | |
3e4b91f2 | 769 | cx_check_missing_mem_inits (tree ctype, tree body, bool complain) |
2d76680f | 770 | { |
7906797e | 771 | /* We allow uninitialized bases/fields in C++20. */ |
b04445d4 | 772 | if (cxx_dialect >= cxx20) |
7906797e MP |
773 | return false; |
774 | ||
3e4b91f2 NS |
775 | unsigned nelts = 0; |
776 | ||
777 | if (body) | |
778 | { | |
779 | if (TREE_CODE (body) != CONSTRUCTOR) | |
780 | return false; | |
781 | nelts = CONSTRUCTOR_NELTS (body); | |
782 | } | |
783 | tree field = TYPE_FIELDS (ctype); | |
2d76680f PC |
784 | |
785 | if (TREE_CODE (ctype) == UNION_TYPE) | |
786 | { | |
787 | if (nelts == 0 && next_initializable_field (field)) | |
788 | { | |
789 | if (complain) | |
790 | error ("%<constexpr%> constructor for union %qT must " | |
791 | "initialize exactly one non-static data member", ctype); | |
792 | return true; | |
793 | } | |
794 | return false; | |
795 | } | |
796 | ||
3e4b91f2 NS |
797 | /* Iterate over the CONSTRUCTOR, checking any missing fields don't |
798 | need an explicit initialization. */ | |
799 | bool bad = false; | |
800 | for (unsigned i = 0; i <= nelts; ++i) | |
2d76680f | 801 | { |
3e4b91f2 NS |
802 | tree index = NULL_TREE; |
803 | if (i < nelts) | |
2d76680f PC |
804 | { |
805 | index = CONSTRUCTOR_ELT (body, i)->index; | |
806 | /* Skip base and vtable inits. */ | |
807 | if (TREE_CODE (index) != FIELD_DECL | |
808 | || DECL_ARTIFICIAL (index)) | |
809 | continue; | |
810 | } | |
3e4b91f2 | 811 | |
2d76680f PC |
812 | for (; field != index; field = DECL_CHAIN (field)) |
813 | { | |
814 | tree ftype; | |
3e4b91f2 NS |
815 | if (TREE_CODE (field) != FIELD_DECL) |
816 | continue; | |
7c30b12a | 817 | if (DECL_UNNAMED_BIT_FIELD (field)) |
2d76680f | 818 | continue; |
3e4b91f2 NS |
819 | if (DECL_ARTIFICIAL (field)) |
820 | continue; | |
821 | if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) | |
822 | { | |
7906797e | 823 | /* Recurse to check the anonymous aggregate member. */ |
3e4b91f2 NS |
824 | bad |= cx_check_missing_mem_inits |
825 | (TREE_TYPE (field), NULL_TREE, complain); | |
826 | if (bad && !complain) | |
827 | return true; | |
828 | continue; | |
829 | } | |
a1c9c9ff JM |
830 | ftype = TREE_TYPE (field); |
831 | if (!ftype || !TYPE_P (ftype) || !COMPLETE_TYPE_P (ftype)) | |
832 | /* A flexible array can't be intialized here, so don't complain | |
833 | that it isn't. */ | |
834 | continue; | |
a4dfd0f0 | 835 | if (is_empty_field (field)) |
6876b269 JM |
836 | /* An empty field doesn't need an initializer. */ |
837 | continue; | |
a1c9c9ff | 838 | ftype = strip_array_types (ftype); |
2d76680f PC |
839 | if (type_has_constexpr_default_constructor (ftype)) |
840 | { | |
841 | /* It's OK to skip a member with a trivial constexpr ctor. | |
842 | A constexpr ctor that isn't trivial should have been | |
843 | added in by now. */ | |
844 | gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype) | |
845 | || errorcount != 0); | |
846 | continue; | |
847 | } | |
848 | if (!complain) | |
849 | return true; | |
097f82ec | 850 | auto_diagnostic_group d; |
b8cd3996 JM |
851 | error ("member %qD must be initialized by mem-initializer " |
852 | "in %<constexpr%> constructor", field); | |
853 | inform (DECL_SOURCE_LOCATION (field), "declared here"); | |
2d76680f PC |
854 | bad = true; |
855 | } | |
856 | if (field == NULL_TREE) | |
857 | break; | |
3e4b91f2 NS |
858 | |
859 | if (ANON_AGGR_TYPE_P (TREE_TYPE (index))) | |
860 | { | |
861 | /* Check the anonymous aggregate initializer is valid. */ | |
862 | bad |= cx_check_missing_mem_inits | |
863 | (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain); | |
864 | if (bad && !complain) | |
865 | return true; | |
866 | } | |
2d76680f PC |
867 | field = DECL_CHAIN (field); |
868 | } | |
869 | ||
870 | return bad; | |
871 | } | |
872 | ||
873 | /* We are processing the definition of the constexpr function FUN. | |
bfc139e2 NS |
874 | Check that its body fulfills the apropriate requirements and |
875 | enter it in the constexpr function definition table. */ | |
2d76680f | 876 | |
bfc139e2 NS |
877 | void |
878 | maybe_save_constexpr_fundef (tree fun) | |
2d76680f | 879 | { |
bfc139e2 | 880 | if (processing_template_decl |
bfc139e2 | 881 | || cp_function_chain->invalid_constexpr |
81f97181 | 882 | || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))) |
bfc139e2 | 883 | return; |
2d76680f | 884 | |
87c2080b JM |
885 | /* With -fimplicit-constexpr, try to make inlines constexpr. We'll |
886 | actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass. */ | |
887 | bool implicit = false; | |
888 | if (flag_implicit_constexpr) | |
889 | { | |
890 | if (DECL_DELETING_DESTRUCTOR_P (fun) | |
891 | && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun))) | |
892 | /* Don't inherit implicit constexpr from the non-deleting | |
893 | destructor. */ | |
894 | DECL_DECLARED_CONSTEXPR_P (fun) = false; | |
895 | ||
896 | if (!DECL_DECLARED_CONSTEXPR_P (fun) | |
897 | && DECL_DECLARED_INLINE_P (fun) | |
898 | && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun))) | |
899 | implicit = true; | |
900 | } | |
901 | ||
902 | if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit) | |
903 | return; | |
904 | ||
905 | bool complain = !DECL_GENERATED_P (fun) && !implicit; | |
37326651 JM |
906 | |
907 | if (!is_valid_constexpr_fn (fun, complain)) | |
bfc139e2 | 908 | return; |
2d76680f | 909 | |
bfc139e2 | 910 | tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun)); |
ca30abcd | 911 | if (massaged == NULL_TREE || massaged == error_mark_node) |
2d76680f | 912 | { |
87c2080b | 913 | if (!DECL_CONSTRUCTOR_P (fun) && complain) |
84fa214d ML |
914 | error ("body of %<constexpr%> function %qD not a return-statement", |
915 | fun); | |
bfc139e2 | 916 | return; |
2d76680f PC |
917 | } |
918 | ||
4f05d85a | 919 | bool potential = potential_rvalue_constant_expression (massaged); |
37326651 | 920 | if (!potential && complain) |
4f05d85a | 921 | require_potential_rvalue_constant_expression (massaged); |
2d76680f | 922 | |
37326651 JM |
923 | if (DECL_CONSTRUCTOR_P (fun) && potential) |
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 | |
3e605b20 | 1213 | static tree cxx_eval_constant_expression (const constexpr_ctx *, tree, |
5a804683 | 1214 | bool, bool *, bool *, tree * = NULL); |
2d76680f PC |
1215 | |
1216 | /* Compute a hash value for a constexpr call representation. */ | |
1217 | ||
1218 | inline hashval_t | |
1219 | constexpr_call_hasher::hash (constexpr_call *info) | |
1220 | { | |
1221 | return info->hash; | |
1222 | } | |
1223 | ||
1224 | /* Return true if the objects pointed to by P and Q represent calls | |
1225 | to the same constexpr function with the same arguments. | |
1226 | Otherwise, return false. */ | |
1227 | ||
1228 | bool | |
1229 | constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs) | |
1230 | { | |
2d76680f | 1231 | if (lhs == rhs) |
46cf7fa1 JJ |
1232 | return true; |
1233 | if (lhs->hash != rhs->hash) | |
1234 | return false; | |
13de99bc | 1235 | if (lhs->manifestly_const_eval != rhs->manifestly_const_eval) |
cce3ae91 | 1236 | return false; |
2d76680f | 1237 | if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef)) |
46cf7fa1 | 1238 | return false; |
3c961dc7 | 1239 | return cp_tree_equal (lhs->bindings, rhs->bindings); |
2d76680f PC |
1240 | } |
1241 | ||
1242 | /* Initialize the constexpr call table, if needed. */ | |
1243 | ||
1244 | static void | |
1245 | maybe_initialize_constexpr_call_table (void) | |
1246 | { | |
1247 | if (constexpr_call_table == NULL) | |
1248 | constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101); | |
1249 | } | |
1250 | ||
c0daf32d PP |
1251 | /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when |
1252 | a function happens to get called recursively, we unshare the callee | |
1253 | function's body and evaluate this unshared copy instead of evaluating the | |
1254 | original body. | |
1255 | ||
1256 | FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function | |
1257 | copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map | |
97f3003f JM |
1258 | that's keyed off of the original FUNCTION_DECL and whose value is a |
1259 | TREE_LIST of this function's unused copies awaiting reuse. | |
c0daf32d | 1260 | |
97f3003f | 1261 | This is not GC-deletable to avoid GC affecting UID generation. */ |
c0daf32d | 1262 | |
ba6730bd | 1263 | static GTY(()) decl_tree_map *fundef_copies_table; |
c0daf32d | 1264 | |
c0daf32d | 1265 | /* Reuse a copy or create a new unshared copy of the function FUN. |
3c98ff9b NS |
1266 | Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE |
1267 | is parms, TYPE is result. */ | |
c0daf32d | 1268 | |
97f3003f | 1269 | static tree |
f65a3299 | 1270 | get_fundef_copy (constexpr_fundef *fundef) |
c0daf32d | 1271 | { |
97f3003f | 1272 | tree copy; |
3c98ff9b | 1273 | bool existed; |
c89844e5 JM |
1274 | tree *slot = &(hash_map_safe_get_or_insert<hm_ggc> |
1275 | (fundef_copies_table, fundef->decl, &existed, 127)); | |
3c98ff9b NS |
1276 | |
1277 | if (!existed) | |
c0daf32d | 1278 | { |
3c98ff9b NS |
1279 | /* There is no cached function available, or in use. We can use |
1280 | the function directly. That the slot is now created records | |
1281 | that this function is now in use. */ | |
43574e4f JJ |
1282 | copy = build_tree_list (fundef->body, fundef->parms); |
1283 | TREE_TYPE (copy) = fundef->result; | |
3c98ff9b NS |
1284 | } |
1285 | else if (*slot == NULL_TREE) | |
1286 | { | |
f65a3299 | 1287 | if (uid_sensitive_constexpr_evaluation_p ()) |
aaa26bf4 JM |
1288 | return NULL_TREE; |
1289 | ||
3c98ff9b | 1290 | /* We've already used the function itself, so make a copy. */ |
97f3003f | 1291 | copy = build_tree_list (NULL, NULL); |
43574e4f JJ |
1292 | tree saved_body = DECL_SAVED_TREE (fundef->decl); |
1293 | tree saved_parms = DECL_ARGUMENTS (fundef->decl); | |
1294 | tree saved_result = DECL_RESULT (fundef->decl); | |
1295 | tree saved_fn = current_function_decl; | |
1296 | DECL_SAVED_TREE (fundef->decl) = fundef->body; | |
1297 | DECL_ARGUMENTS (fundef->decl) = fundef->parms; | |
1298 | DECL_RESULT (fundef->decl) = fundef->result; | |
1299 | current_function_decl = fundef->decl; | |
1300 | TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy), | |
1301 | TREE_TYPE (copy)); | |
1302 | current_function_decl = saved_fn; | |
1303 | DECL_RESULT (fundef->decl) = saved_result; | |
1304 | DECL_ARGUMENTS (fundef->decl) = saved_parms; | |
1305 | DECL_SAVED_TREE (fundef->decl) = saved_body; | |
c0daf32d PP |
1306 | } |
1307 | else | |
1308 | { | |
3c98ff9b | 1309 | /* We have a cached function available. */ |
c0daf32d | 1310 | copy = *slot; |
97f3003f | 1311 | *slot = TREE_CHAIN (copy); |
c0daf32d PP |
1312 | } |
1313 | ||
1314 | return copy; | |
1315 | } | |
1316 | ||
3c98ff9b NS |
1317 | /* Save the copy COPY of function FUN for later reuse by |
1318 | get_fundef_copy(). By construction, there will always be an entry | |
1319 | to find. */ | |
c0daf32d PP |
1320 | |
1321 | static void | |
97f3003f | 1322 | save_fundef_copy (tree fun, tree copy) |
c0daf32d | 1323 | { |
3c98ff9b | 1324 | tree *slot = fundef_copies_table->get (fun); |
97f3003f | 1325 | TREE_CHAIN (copy) = *slot; |
c0daf32d PP |
1326 | *slot = copy; |
1327 | } | |
1328 | ||
2d76680f PC |
1329 | /* We have an expression tree T that represents a call, either CALL_EXPR |
1330 | or AGGR_INIT_EXPR. Return the Nth argument. */ | |
1331 | ||
1332 | static inline tree | |
1333 | get_nth_callarg (tree t, int n) | |
1334 | { | |
1335 | switch (TREE_CODE (t)) | |
1336 | { | |
1337 | case CALL_EXPR: | |
1338 | return CALL_EXPR_ARG (t, n); | |
1339 | ||
1340 | case AGGR_INIT_EXPR: | |
1341 | return AGGR_INIT_EXPR_ARG (t, n); | |
1342 | ||
1343 | default: | |
1344 | gcc_unreachable (); | |
1345 | return NULL; | |
1346 | } | |
1347 | } | |
1348 | ||
2d76680f PC |
1349 | /* Attempt to evaluate T which represents a call to a builtin function. |
1350 | We assume here that all builtin functions evaluate to scalar types | |
1351 | represented by _CST nodes. */ | |
1352 | ||
1353 | static tree | |
5756d0f9 | 1354 | cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun, |
92a596e8 | 1355 | bool lval, |
2d76680f PC |
1356 | bool *non_constant_p, bool *overflow_p) |
1357 | { | |
1358 | const int nargs = call_expr_nargs (t); | |
1359 | tree *args = (tree *) alloca (nargs * sizeof (tree)); | |
1360 | tree new_call; | |
1361 | int i; | |
5756d0f9 JM |
1362 | |
1363 | /* Don't fold __builtin_constant_p within a constexpr function. */ | |
e4082611 | 1364 | bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun); |
b925d25d | 1365 | |
fe736ffd JM |
1366 | /* If we aren't requiring a constant expression, defer __builtin_constant_p |
1367 | in a constexpr function until we have values for the parameters. */ | |
b925d25d | 1368 | if (bi_const_p |
4cd3e7df | 1369 | && !ctx->manifestly_const_eval |
5756d0f9 JM |
1370 | && current_function_decl |
1371 | && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) | |
2d76680f | 1372 | { |
5756d0f9 JM |
1373 | *non_constant_p = true; |
1374 | return t; | |
2d76680f | 1375 | } |
5756d0f9 | 1376 | |
e4082611 | 1377 | /* For __builtin_is_constant_evaluated, defer it if not |
117c6426 JJ |
1378 | ctx->manifestly_const_eval (as sometimes we try to constant evaluate |
1379 | without manifestly_const_eval even expressions or parts thereof which | |
1380 | will later be manifestly const_eval evaluated), otherwise fold it to | |
1381 | true. */ | |
3d78e008 | 1382 | if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED, |
13de99bc | 1383 | BUILT_IN_FRONTEND)) |
e4082611 | 1384 | { |
13de99bc | 1385 | if (!ctx->manifestly_const_eval) |
e4082611 JJ |
1386 | { |
1387 | *non_constant_p = true; | |
1388 | return t; | |
1389 | } | |
1390 | return boolean_true_node; | |
1391 | } | |
1392 | ||
ff603745 | 1393 | if (fndecl_built_in_p (fun, CP_BUILT_IN_SOURCE_LOCATION, BUILT_IN_FRONTEND)) |
ba3d8dff JJ |
1394 | { |
1395 | temp_override<tree> ovr (current_function_decl); | |
1396 | if (ctx->call && ctx->call->fundef) | |
1397 | current_function_decl = ctx->call->fundef->decl; | |
1398 | return fold_builtin_source_location (EXPR_LOCATION (t)); | |
1399 | } | |
ff603745 | 1400 | |
69dc042f JM |
1401 | int strops = 0; |
1402 | int strret = 0; | |
1403 | if (fndecl_built_in_p (fun, BUILT_IN_NORMAL)) | |
1404 | switch (DECL_FUNCTION_CODE (fun)) | |
1405 | { | |
1406 | case BUILT_IN_STRLEN: | |
1407 | case BUILT_IN_STRNLEN: | |
1408 | strops = 1; | |
1409 | break; | |
1410 | case BUILT_IN_MEMCHR: | |
1411 | case BUILT_IN_STRCHR: | |
1412 | case BUILT_IN_STRRCHR: | |
1413 | strops = 1; | |
1414 | strret = 1; | |
1415 | break; | |
1416 | case BUILT_IN_MEMCMP: | |
1417 | case BUILT_IN_STRCMP: | |
1418 | strops = 2; | |
1419 | break; | |
1420 | case BUILT_IN_STRSTR: | |
1421 | strops = 2; | |
1422 | strret = 1; | |
bc13106e JJ |
1423 | break; |
1424 | case BUILT_IN_ASAN_POINTER_COMPARE: | |
1425 | case BUILT_IN_ASAN_POINTER_SUBTRACT: | |
1426 | /* These builtins shall be ignored during constant expression | |
1427 | evaluation. */ | |
1428 | return void_node; | |
69dc042f JM |
1429 | default: |
1430 | break; | |
1431 | } | |
1432 | ||
5756d0f9 JM |
1433 | /* Be permissive for arguments to built-ins; __builtin_constant_p should |
1434 | return constant false for a non-constant argument. */ | |
1435 | constexpr_ctx new_ctx = *ctx; | |
1436 | new_ctx.quiet = true; | |
5756d0f9 | 1437 | for (i = 0; i < nargs; ++i) |
b925d25d | 1438 | { |
69dc042f | 1439 | tree arg = CALL_EXPR_ARG (t, i); |
6f346913 | 1440 | tree oarg = arg; |
69dc042f JM |
1441 | |
1442 | /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since | |
1443 | expand_builtin doesn't know how to look in the values table. */ | |
1444 | bool strop = i < strops; | |
1445 | if (strop) | |
1446 | { | |
1447 | STRIP_NOPS (arg); | |
1448 | if (TREE_CODE (arg) == ADDR_EXPR) | |
1449 | arg = TREE_OPERAND (arg, 0); | |
1450 | else | |
1451 | strop = false; | |
1452 | } | |
1453 | ||
2ff7172a JJ |
1454 | /* If builtin_valid_in_constant_expr_p is true, |
1455 | potential_constant_expression_1 has not recursed into the arguments | |
1456 | of the builtin, verify it here. */ | |
1457 | if (!builtin_valid_in_constant_expr_p (fun) | |
69dc042f | 1458 | || potential_constant_expression (arg)) |
4cd3e7df JJ |
1459 | { |
1460 | bool dummy1 = false, dummy2 = false; | |
69dc042f JM |
1461 | arg = cxx_eval_constant_expression (&new_ctx, arg, false, |
1462 | &dummy1, &dummy2); | |
4cd3e7df JJ |
1463 | } |
1464 | ||
b925d25d | 1465 | if (bi_const_p) |
4cd3e7df | 1466 | /* For __builtin_constant_p, fold all expressions with constant values |
b925d25d | 1467 | even if they aren't C++ constant-expressions. */ |
69dc042f JM |
1468 | arg = cp_fold_rvalue (arg); |
1469 | else if (strop) | |
1470 | { | |
1471 | if (TREE_CODE (arg) == CONSTRUCTOR) | |
1472 | arg = braced_lists_to_strings (TREE_TYPE (arg), arg); | |
1473 | if (TREE_CODE (arg) == STRING_CST) | |
1474 | arg = build_address (arg); | |
6f346913 JM |
1475 | else |
1476 | arg = oarg; | |
69dc042f JM |
1477 | } |
1478 | ||
1479 | args[i] = arg; | |
b925d25d | 1480 | } |
5756d0f9 JM |
1481 | |
1482 | bool save_ffbcp = force_folding_builtin_constant_p; | |
b09a67ea | 1483 | force_folding_builtin_constant_p |= ctx->manifestly_const_eval; |
43574e4f JJ |
1484 | tree save_cur_fn = current_function_decl; |
1485 | /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */ | |
1486 | if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION) | |
1487 | && ctx->call | |
1488 | && ctx->call->fundef) | |
1489 | current_function_decl = ctx->call->fundef->decl; | |
6cd005a2 JJ |
1490 | if (fndecl_built_in_p (fun, |
1491 | CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS, | |
1492 | BUILT_IN_FRONTEND)) | |
1493 | { | |
1494 | location_t loc = EXPR_LOCATION (t); | |
1495 | if (nargs >= 1) | |
1496 | VERIFY_CONSTANT (args[0]); | |
1497 | new_call | |
1498 | = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs, | |
1499 | args); | |
1500 | } | |
32c3a753 JJ |
1501 | else if (fndecl_built_in_p (fun, |
1502 | CP_BUILT_IN_IS_CORRESPONDING_MEMBER, | |
1503 | BUILT_IN_FRONTEND)) | |
1504 | { | |
1505 | location_t loc = EXPR_LOCATION (t); | |
1506 | if (nargs >= 2) | |
1507 | { | |
1508 | VERIFY_CONSTANT (args[0]); | |
1509 | VERIFY_CONSTANT (args[1]); | |
1510 | } | |
1511 | new_call = fold_builtin_is_corresponding_member (loc, nargs, args); | |
1512 | } | |
6cd005a2 JJ |
1513 | else |
1514 | new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t), | |
1515 | CALL_EXPR_FN (t), nargs, args); | |
43574e4f | 1516 | current_function_decl = save_cur_fn; |
5756d0f9 | 1517 | force_folding_builtin_constant_p = save_ffbcp; |
109d2197 JJ |
1518 | if (new_call == NULL) |
1519 | { | |
1520 | if (!*non_constant_p && !ctx->quiet) | |
1521 | { | |
1b6fa695 ML |
1522 | /* Do not allow__builtin_unreachable in constexpr function. |
1523 | The __builtin_unreachable call with BUILTINS_LOCATION | |
1524 | comes from cp_maybe_instrument_return. */ | |
3d78e008 | 1525 | if (fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE) |
1b6fa695 | 1526 | && EXPR_LOCATION (t) == BUILTINS_LOCATION) |
84fa214d | 1527 | error ("%<constexpr%> call flows off the end of the function"); |
1b6fa695 ML |
1528 | else |
1529 | { | |
1530 | new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t), | |
1531 | CALL_EXPR_FN (t), nargs, args); | |
1532 | error ("%q+E is not a constant expression", new_call); | |
1533 | } | |
109d2197 JJ |
1534 | } |
1535 | *non_constant_p = true; | |
1536 | return t; | |
1537 | } | |
1538 | ||
43574e4f | 1539 | if (!potential_constant_expression (new_call)) |
109d2197 JJ |
1540 | { |
1541 | if (!*non_constant_p && !ctx->quiet) | |
1542 | error ("%q+E is not a constant expression", new_call); | |
1543 | *non_constant_p = true; | |
1544 | return t; | |
1545 | } | |
1546 | ||
69dc042f JM |
1547 | if (strret) |
1548 | { | |
1549 | /* memchr returns a pointer into the first argument, but we replaced the | |
1550 | argument above with a STRING_CST; put it back it now. */ | |
1551 | tree op = CALL_EXPR_ARG (t, strret-1); | |
1552 | STRIP_NOPS (new_call); | |
1553 | if (TREE_CODE (new_call) == POINTER_PLUS_EXPR) | |
1554 | TREE_OPERAND (new_call, 0) = op; | |
1555 | else if (TREE_CODE (new_call) == ADDR_EXPR) | |
1556 | new_call = op; | |
1557 | } | |
1558 | ||
109d2197 JJ |
1559 | return cxx_eval_constant_expression (&new_ctx, new_call, lval, |
1560 | non_constant_p, overflow_p); | |
2d76680f PC |
1561 | } |
1562 | ||
1563 | /* TEMP is the constant value of a temporary object of type TYPE. Adjust | |
1564 | the type of the value to match. */ | |
1565 | ||
1566 | static tree | |
1567 | adjust_temp_type (tree type, tree temp) | |
1568 | { | |
47be9509 | 1569 | if (same_type_p (TREE_TYPE (temp), type)) |
2d76680f PC |
1570 | return temp; |
1571 | /* Avoid wrapping an aggregate value in a NOP_EXPR. */ | |
1572 | if (TREE_CODE (temp) == CONSTRUCTOR) | |
b27f74e7 MP |
1573 | { |
1574 | /* build_constructor wouldn't retain various CONSTRUCTOR flags. */ | |
1575 | tree t = copy_node (temp); | |
1576 | TREE_TYPE (t) = type; | |
1577 | return t; | |
1578 | } | |
ff8ba86f JJ |
1579 | if (TREE_CODE (temp) == EMPTY_CLASS_EXPR) |
1580 | return build0 (EMPTY_CLASS_EXPR, type); | |
2d76680f | 1581 | gcc_assert (scalarish_type_p (type)); |
72b091f7 MP |
1582 | /* Now we know we're dealing with a scalar, and a prvalue of non-class |
1583 | type is cv-unqualified. */ | |
1584 | return cp_fold_convert (cv_unqualified (type), temp); | |
2d76680f PC |
1585 | } |
1586 | ||
9b9eb42a JM |
1587 | /* If T is a CONSTRUCTOR, return an unshared copy of T and any |
1588 | sub-CONSTRUCTORs. Otherwise return T. | |
0146e25f | 1589 | |
9b9eb42a JM |
1590 | We use this whenever we initialize an object as a whole, whether it's a |
1591 | parameter, a local variable, or a subobject, so that subsequent | |
1592 | modifications don't affect other places where it was used. */ | |
0146e25f | 1593 | |
1f6857ba | 1594 | tree |
3da7d774 | 1595 | unshare_constructor (tree t MEM_STAT_DECL) |
0146e25f | 1596 | { |
9b9eb42a JM |
1597 | if (!t || TREE_CODE (t) != CONSTRUCTOR) |
1598 | return t; | |
1599 | auto_vec <tree*, 4> ptrs; | |
1600 | ptrs.safe_push (&t); | |
1601 | while (!ptrs.is_empty ()) | |
1602 | { | |
1603 | tree *p = ptrs.pop (); | |
3da7d774 JM |
1604 | tree n = copy_node (*p PASS_MEM_STAT); |
1605 | CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT); | |
9b9eb42a JM |
1606 | *p = n; |
1607 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n); | |
1608 | constructor_elt *ce; | |
1609 | for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
6b7d53a2 | 1610 | if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR) |
9b9eb42a JM |
1611 | ptrs.safe_push (&ce->value); |
1612 | } | |
0146e25f PP |
1613 | return t; |
1614 | } | |
1615 | ||
620adbec JM |
1616 | /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */ |
1617 | ||
1618 | static void | |
1619 | free_constructor (tree t) | |
1620 | { | |
1621 | if (!t || TREE_CODE (t) != CONSTRUCTOR) | |
1622 | return; | |
1623 | releasing_vec ctors; | |
1624 | vec_safe_push (ctors, t); | |
1625 | while (!ctors->is_empty ()) | |
1626 | { | |
1627 | tree c = ctors->pop (); | |
1628 | if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c)) | |
1629 | { | |
1630 | constructor_elt *ce; | |
1631 | for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i) | |
1632 | if (TREE_CODE (ce->value) == CONSTRUCTOR) | |
1633 | vec_safe_push (ctors, ce->value); | |
1634 | ggc_free (elts); | |
1635 | } | |
1636 | ggc_free (c); | |
1637 | } | |
1638 | } | |
1639 | ||
559d2f1e JJ |
1640 | /* Helper function of cxx_bind_parameters_in_call. Return non-NULL |
1641 | if *TP is address of a static variable (or part of it) currently being | |
1642 | constructed or of a heap artificial variable. */ | |
1643 | ||
1644 | static tree | |
1645 | addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data) | |
1646 | { | |
1647 | if (TREE_CODE (*tp) == ADDR_EXPR) | |
1648 | if (tree var = get_base_address (TREE_OPERAND (*tp, 0))) | |
1649 | if (VAR_P (var) && TREE_STATIC (var)) | |
1650 | { | |
1651 | if (DECL_NAME (var) == heap_uninit_identifier | |
1652 | || DECL_NAME (var) == heap_identifier | |
1653 | || DECL_NAME (var) == heap_vec_uninit_identifier | |
1654 | || DECL_NAME (var) == heap_vec_identifier) | |
1655 | return var; | |
1656 | ||
1657 | constexpr_global_ctx *global = (constexpr_global_ctx *) data; | |
1658 | if (global->values.get (var)) | |
1659 | return var; | |
1660 | } | |
1661 | if (TYPE_P (*tp)) | |
1662 | *walk_subtrees = false; | |
1663 | return NULL_TREE; | |
1664 | } | |
1665 | ||
2d76680f PC |
1666 | /* Subroutine of cxx_eval_call_expression. |
1667 | We are processing a call expression (either CALL_EXPR or | |
3e605b20 | 1668 | AGGR_INIT_EXPR) in the context of CTX. Evaluate |
2d76680f PC |
1669 | all arguments and bind their values to correspondings |
1670 | parameters, making up the NEW_CALL context. */ | |
1671 | ||
1595fe44 JM |
1672 | static tree |
1673 | cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun, | |
12d9ce19 JM |
1674 | bool *non_constant_p, bool *overflow_p, |
1675 | bool *non_constant_args) | |
2d76680f PC |
1676 | { |
1677 | const int nargs = call_expr_nargs (t); | |
1595fe44 | 1678 | tree parms = DECL_ARGUMENTS (fun); |
2d76680f | 1679 | int i; |
3c961dc7 JM |
1680 | /* We don't record ellipsis args below. */ |
1681 | int nparms = list_length (parms); | |
1682 | int nbinds = nargs < nparms ? nargs : nparms; | |
1595fe44 | 1683 | tree binds = make_tree_vec (nbinds); |
2d76680f PC |
1684 | for (i = 0; i < nargs; ++i) |
1685 | { | |
1686 | tree x, arg; | |
1687 | tree type = parms ? TREE_TYPE (parms) : void_type_node; | |
1595fe44 JM |
1688 | if (parms && DECL_BY_REFERENCE (parms)) |
1689 | type = TREE_TYPE (type); | |
2d76680f | 1690 | x = get_nth_callarg (t, i); |
3e605b20 JM |
1691 | /* For member function, the first argument is a pointer to the implied |
1692 | object. For a constructor, it might still be a dummy object, in | |
60813a46 | 1693 | which case we get the real argument from ctx. */ |
3e605b20 JM |
1694 | if (i == 0 && DECL_CONSTRUCTOR_P (fun) |
1695 | && is_dummy_object (x)) | |
1696 | { | |
1697 | x = ctx->object; | |
84dd815f | 1698 | x = build_address (x); |
3e605b20 | 1699 | } |
43574e4f JJ |
1700 | if (TREE_ADDRESSABLE (type)) |
1701 | /* Undo convert_for_arg_passing work here. */ | |
1702 | x = convert_from_reference (x); | |
6c59b8a9 JM |
1703 | /* Normally we would strip a TARGET_EXPR in an initialization context |
1704 | such as this, but here we do the elision differently: we keep the | |
1705 | TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */ | |
0e038601 | 1706 | arg = cxx_eval_constant_expression (ctx, x, /*lval=*/false, |
5a804683 | 1707 | non_constant_p, overflow_p); |
2d76680f | 1708 | /* Don't VERIFY_CONSTANT here. */ |
2b3ab879 | 1709 | if (*non_constant_p && ctx->quiet) |
1595fe44 | 1710 | break; |
2d76680f PC |
1711 | /* Just discard ellipsis args after checking their constantitude. */ |
1712 | if (!parms) | |
1713 | continue; | |
4727d4c6 NS |
1714 | |
1715 | if (!*non_constant_p) | |
1716 | { | |
1717 | /* Make sure the binding has the same type as the parm. But | |
1718 | only for constant args. */ | |
9f613f06 | 1719 | if (!TYPE_REF_P (type)) |
4727d4c6 NS |
1720 | arg = adjust_temp_type (type, arg); |
1721 | if (!TREE_CONSTANT (arg)) | |
1722 | *non_constant_args = true; | |
5afd90c5 JJ |
1723 | else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
1724 | /* The destructor needs to see any modifications the callee makes | |
1725 | to the argument. */ | |
1726 | *non_constant_args = true; | |
559d2f1e JJ |
1727 | /* If arg is or contains address of a heap artificial variable or |
1728 | of a static variable being constructed, avoid caching the | |
1729 | function call, as those variables might be modified by the | |
1730 | function, or might be modified by the callers in between | |
1731 | the cached function and just read by the function. */ | |
1732 | else if (!*non_constant_args | |
1733 | && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global, | |
1734 | NULL)) | |
1735 | *non_constant_args = true; | |
5afd90c5 | 1736 | |
5558a0da JJ |
1737 | /* For virtual calls, adjust the this argument, so that it is |
1738 | the object on which the method is called, rather than | |
1739 | one of its bases. */ | |
1740 | if (i == 0 && DECL_VIRTUAL_P (fun)) | |
1741 | { | |
1742 | tree addr = arg; | |
1743 | STRIP_NOPS (addr); | |
1744 | if (TREE_CODE (addr) == ADDR_EXPR) | |
1745 | { | |
1746 | tree obj = TREE_OPERAND (addr, 0); | |
1747 | while (TREE_CODE (obj) == COMPONENT_REF | |
1748 | && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1)) | |
1749 | && !same_type_ignoring_top_level_qualifiers_p | |
1750 | (TREE_TYPE (obj), DECL_CONTEXT (fun))) | |
1751 | obj = TREE_OPERAND (obj, 0); | |
1752 | if (obj != TREE_OPERAND (addr, 0)) | |
1753 | arg = build_fold_addr_expr_with_type (obj, | |
1754 | TREE_TYPE (arg)); | |
1755 | } | |
1756 | } | |
3c961dc7 | 1757 | TREE_VEC_ELT (binds, i) = arg; |
4727d4c6 | 1758 | } |
2d76680f PC |
1759 | parms = TREE_CHAIN (parms); |
1760 | } | |
1595fe44 JM |
1761 | |
1762 | return binds; | |
2d76680f PC |
1763 | } |
1764 | ||
1765 | /* Variables and functions to manage constexpr call expansion context. | |
1766 | These do not need to be marked for PCH or GC. */ | |
1767 | ||
1768 | /* FIXME remember and print actual constant arguments. */ | |
7de76362 | 1769 | static vec<tree> call_stack; |
2d76680f PC |
1770 | static int call_stack_tick; |
1771 | static int last_cx_error_tick; | |
1772 | ||
7ffc7de5 | 1773 | static int |
2d76680f PC |
1774 | push_cx_call_context (tree call) |
1775 | { | |
1776 | ++call_stack_tick; | |
1777 | if (!EXPR_HAS_LOCATION (call)) | |
1778 | SET_EXPR_LOCATION (call, input_location); | |
1779 | call_stack.safe_push (call); | |
7ffc7de5 JM |
1780 | int len = call_stack.length (); |
1781 | if (len > max_constexpr_depth) | |
2d76680f | 1782 | return false; |
7ffc7de5 | 1783 | return len; |
2d76680f PC |
1784 | } |
1785 | ||
1786 | static void | |
1787 | pop_cx_call_context (void) | |
1788 | { | |
1789 | ++call_stack_tick; | |
1790 | call_stack.pop (); | |
1791 | } | |
1792 | ||
1793 | vec<tree> | |
1794 | cx_error_context (void) | |
1795 | { | |
1796 | vec<tree> r = vNULL; | |
1797 | if (call_stack_tick != last_cx_error_tick | |
1798 | && !call_stack.is_empty ()) | |
1799 | r = call_stack; | |
1800 | last_cx_error_tick = call_stack_tick; | |
1801 | return r; | |
1802 | } | |
1803 | ||
44a845ca MS |
1804 | /* Evaluate a call T to a GCC internal function when possible and return |
1805 | the evaluated result or, under the control of CTX, give an error, set | |
1806 | NON_CONSTANT_P, and return the unevaluated call T otherwise. */ | |
1807 | ||
1808 | static tree | |
1809 | cxx_eval_internal_function (const constexpr_ctx *ctx, tree t, | |
1810 | bool lval, | |
1811 | bool *non_constant_p, bool *overflow_p) | |
1812 | { | |
1813 | enum tree_code opcode = ERROR_MARK; | |
1814 | ||
1815 | switch (CALL_EXPR_IFN (t)) | |
1816 | { | |
1817 | case IFN_UBSAN_NULL: | |
1818 | case IFN_UBSAN_BOUNDS: | |
1819 | case IFN_UBSAN_VPTR: | |
81fea426 | 1820 | case IFN_FALLTHROUGH: |
44a845ca MS |
1821 | return void_node; |
1822 | ||
1823 | case IFN_ADD_OVERFLOW: | |
1824 | opcode = PLUS_EXPR; | |
1825 | break; | |
1826 | case IFN_SUB_OVERFLOW: | |
1827 | opcode = MINUS_EXPR; | |
1828 | break; | |
1829 | case IFN_MUL_OVERFLOW: | |
1830 | opcode = MULT_EXPR; | |
1831 | break; | |
1832 | ||
e16f1cc7 JJ |
1833 | case IFN_LAUNDER: |
1834 | return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), | |
1835 | false, non_constant_p, overflow_p); | |
1836 | ||
d8fcab68 JJ |
1837 | case IFN_VEC_CONVERT: |
1838 | { | |
1839 | tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), | |
1840 | false, non_constant_p, | |
1841 | overflow_p); | |
1842 | if (TREE_CODE (arg) == VECTOR_CST) | |
1843 | return fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg); | |
1844 | else | |
1845 | { | |
1846 | *non_constant_p = true; | |
1847 | return t; | |
1848 | } | |
1849 | } | |
1850 | ||
44a845ca MS |
1851 | default: |
1852 | if (!ctx->quiet) | |
f9d0ca40 | 1853 | error_at (cp_expr_loc_or_input_loc (t), |
44a845ca MS |
1854 | "call to internal function %qE", t); |
1855 | *non_constant_p = true; | |
1856 | return t; | |
1857 | } | |
1858 | ||
1859 | /* Evaluate constant arguments using OPCODE and return a complex | |
1860 | number containing the result and the overflow bit. */ | |
1861 | tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval, | |
1862 | non_constant_p, overflow_p); | |
1863 | tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval, | |
1864 | non_constant_p, overflow_p); | |
1865 | ||
1866 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) | |
1867 | { | |
f9d0ca40 | 1868 | location_t loc = cp_expr_loc_or_input_loc (t); |
44a845ca MS |
1869 | tree type = TREE_TYPE (TREE_TYPE (t)); |
1870 | tree result = fold_binary_loc (loc, opcode, type, | |
1871 | fold_convert_loc (loc, type, arg0), | |
1872 | fold_convert_loc (loc, type, arg1)); | |
1873 | tree ovf | |
1874 | = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1)); | |
1875 | /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */ | |
1876 | if (TREE_OVERFLOW (result)) | |
1877 | TREE_OVERFLOW (result) = 0; | |
1878 | ||
1879 | return build_complex (TREE_TYPE (t), result, ovf); | |
1880 | } | |
1881 | ||
1882 | *non_constant_p = true; | |
1883 | return t; | |
1884 | } | |
1885 | ||
e8c48716 | 1886 | /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */ |
ecc57615 JM |
1887 | |
1888 | static void | |
1889 | clear_no_implicit_zero (tree ctor) | |
1890 | { | |
e8c48716 | 1891 | if (CONSTRUCTOR_NO_CLEARING (ctor)) |
ecc57615 | 1892 | { |
e8c48716 | 1893 | CONSTRUCTOR_NO_CLEARING (ctor) = false; |
fae00a0a JM |
1894 | for (auto &e: CONSTRUCTOR_ELTS (ctor)) |
1895 | if (TREE_CODE (e.value) == CONSTRUCTOR) | |
1896 | clear_no_implicit_zero (e.value); | |
ecc57615 JM |
1897 | } |
1898 | } | |
1899 | ||
04e1749c MP |
1900 | /* Complain about a const object OBJ being modified in a constant expression. |
1901 | EXPR is the MODIFY_EXPR expression performing the modification. */ | |
1902 | ||
1903 | static void | |
1904 | modifying_const_object_error (tree expr, tree obj) | |
1905 | { | |
1906 | location_t loc = cp_expr_loc_or_input_loc (expr); | |
1907 | auto_diagnostic_group d; | |
1908 | error_at (loc, "modifying a const object %qE is not allowed in " | |
1909 | "a constant expression", TREE_OPERAND (expr, 0)); | |
1910 | inform (location_of (obj), "originally declared %<const%> here"); | |
1911 | } | |
1912 | ||
8e007055 JJ |
1913 | /* Return true if FNDECL is a replaceable global allocation function that |
1914 | should be useable during constant expression evaluation. */ | |
1915 | ||
1916 | static inline bool | |
1917 | cxx_replaceable_global_alloc_fn (tree fndecl) | |
1918 | { | |
b04445d4 | 1919 | return (cxx_dialect >= cxx20 |
8e007055 | 1920 | && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl)) |
cf650568 JJ |
1921 | && CP_DECL_CONTEXT (fndecl) == global_namespace |
1922 | && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl) | |
1923 | || DECL_IS_OPERATOR_DELETE_P (fndecl))); | |
1924 | } | |
1925 | ||
1926 | /* Return true if FNDECL is a placement new function that should be | |
1927 | useable during constant expression evaluation of std::construct_at. */ | |
1928 | ||
1929 | static inline bool | |
1930 | cxx_placement_new_fn (tree fndecl) | |
1931 | { | |
b04445d4 | 1932 | if (cxx_dialect >= cxx20 |
cf650568 JJ |
1933 | && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl)) |
1934 | && CP_DECL_CONTEXT (fndecl) == global_namespace | |
1935 | && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl) | |
1936 | && TREE_CODE (TREE_TYPE (fndecl)) == FUNCTION_TYPE) | |
1937 | { | |
1938 | tree first_arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl))); | |
1939 | if (TREE_VALUE (first_arg) == ptr_type_node | |
1940 | && TREE_CHAIN (first_arg) == void_list_node) | |
1941 | return true; | |
1942 | } | |
1943 | return false; | |
1944 | } | |
1945 | ||
1946 | /* Return true if FNDECL is std::construct_at. */ | |
1947 | ||
1948 | static inline bool | |
1949 | is_std_construct_at (tree fndecl) | |
1950 | { | |
1951 | if (!decl_in_std_namespace_p (fndecl)) | |
1952 | return false; | |
1953 | ||
1954 | tree name = DECL_NAME (fndecl); | |
1955 | return name && id_equal (name, "construct_at"); | |
8e007055 JJ |
1956 | } |
1957 | ||
2ffc2645 MP |
1958 | /* Overload for the above taking constexpr_call*. */ |
1959 | ||
1960 | static inline bool | |
1961 | is_std_construct_at (const constexpr_call *call) | |
1962 | { | |
1963 | return (call | |
1964 | && call->fundef | |
1965 | && is_std_construct_at (call->fundef->decl)); | |
1966 | } | |
1967 | ||
8412b939 JJ |
1968 | /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */ |
1969 | ||
1970 | static inline bool | |
1971 | is_std_allocator_allocate (tree fndecl) | |
1972 | { | |
1973 | tree name = DECL_NAME (fndecl); | |
1974 | if (name == NULL_TREE | |
1975 | || !(id_equal (name, "allocate") || id_equal (name, "deallocate"))) | |
1976 | return false; | |
1977 | ||
1978 | tree ctx = DECL_CONTEXT (fndecl); | |
1979 | if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx)) | |
1980 | return false; | |
1981 | ||
1982 | tree decl = TYPE_MAIN_DECL (ctx); | |
1983 | name = DECL_NAME (decl); | |
1984 | if (name == NULL_TREE || !id_equal (name, "allocator")) | |
1985 | return false; | |
1986 | ||
1987 | return decl_in_std_namespace_p (decl); | |
1988 | } | |
1989 | ||
2ffc2645 MP |
1990 | /* Overload for the above taking constexpr_call*. */ |
1991 | ||
1992 | static inline bool | |
1993 | is_std_allocator_allocate (const constexpr_call *call) | |
1994 | { | |
1995 | return (call | |
1996 | && call->fundef | |
1997 | && is_std_allocator_allocate (call->fundef->decl)); | |
1998 | } | |
1999 | ||
22edf943 MP |
2000 | /* Return true if FNDECL is __dynamic_cast. */ |
2001 | ||
2002 | static inline bool | |
2003 | cxx_dynamic_cast_fn_p (tree fndecl) | |
2004 | { | |
b04445d4 | 2005 | return (cxx_dialect >= cxx20 |
22edf943 MP |
2006 | && id_equal (DECL_NAME (fndecl), "__dynamic_cast") |
2007 | && CP_DECL_CONTEXT (fndecl) == global_namespace); | |
2008 | } | |
2009 | ||
2010 | /* Often, we have an expression in the form of address + offset, e.g. | |
2011 | "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */ | |
2012 | ||
2013 | static tree | |
2014 | extract_obj_from_addr_offset (tree expr) | |
2015 | { | |
2016 | if (TREE_CODE (expr) == POINTER_PLUS_EXPR) | |
2017 | expr = TREE_OPERAND (expr, 0); | |
2018 | STRIP_NOPS (expr); | |
2019 | if (TREE_CODE (expr) == ADDR_EXPR) | |
2020 | expr = TREE_OPERAND (expr, 0); | |
2021 | return expr; | |
2022 | } | |
2023 | ||
2024 | /* Given a PATH like | |
2025 | ||
2026 | g.D.2181.D.2154.D.2102.D.2093 | |
2027 | ||
2028 | find a component with type TYPE. Return NULL_TREE if not found, and | |
2029 | error_mark_node if the component is not accessible. If STOP is non-null, | |
2030 | this function will return NULL_TREE if STOP is found before TYPE. */ | |
2031 | ||
2032 | static tree | |
2033 | get_component_with_type (tree path, tree type, tree stop) | |
2034 | { | |
2035 | while (true) | |
2036 | { | |
2037 | if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type)) | |
2038 | /* Found it. */ | |
2039 | return path; | |
2040 | else if (stop | |
2041 | && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), | |
2042 | stop))) | |
2043 | return NULL_TREE; | |
2044 | else if (TREE_CODE (path) == COMPONENT_REF | |
2045 | && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1))) | |
2046 | { | |
2047 | /* We need to check that the component we're accessing is in fact | |
2048 | accessible. */ | |
2049 | if (TREE_PRIVATE (TREE_OPERAND (path, 1)) | |
2050 | || TREE_PROTECTED (TREE_OPERAND (path, 1))) | |
2051 | return error_mark_node; | |
2052 | path = TREE_OPERAND (path, 0); | |
2053 | } | |
2054 | else | |
2055 | return NULL_TREE; | |
2056 | } | |
2057 | } | |
2058 | ||
2059 | /* Evaluate a call to __dynamic_cast (permitted by P1327R1). | |
2060 | ||
2061 | The declaration of __dynamic_cast is: | |
2062 | ||
2063 | void* __dynamic_cast (const void* __src_ptr, | |
2064 | const __class_type_info* __src_type, | |
2065 | const __class_type_info* __dst_type, | |
2066 | ptrdiff_t __src2dst); | |
2067 | ||
2068 | where src2dst has the following possible values | |
2069 | ||
2070 | >-1: src_type is a unique public non-virtual base of dst_type | |
2071 | dst_ptr + src2dst == src_ptr | |
2072 | -1: unspecified relationship | |
2073 | -2: src_type is not a public base of dst_type | |
2074 | -3: src_type is a multiple public non-virtual base of dst_type | |
2075 | ||
2076 | Since literal types can't have virtual bases, we only expect hint >=0, | |
2077 | -2, or -3. */ | |
2078 | ||
2079 | static tree | |
2080 | cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call, | |
2081 | bool *non_constant_p, bool *overflow_p) | |
2082 | { | |
2083 | /* T will be something like | |
2084 | __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8) | |
2085 | dismantle it. */ | |
2086 | gcc_assert (call_expr_nargs (call) == 4); | |
2087 | tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; | |
2088 | tree obj = CALL_EXPR_ARG (call, 0); | |
2089 | tree type = CALL_EXPR_ARG (call, 2); | |
2090 | HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3)); | |
2091 | location_t loc = cp_expr_loc_or_input_loc (call); | |
2092 | ||
2093 | /* Get the target type of the dynamic_cast. */ | |
2094 | gcc_assert (TREE_CODE (type) == ADDR_EXPR); | |
2095 | type = TREE_OPERAND (type, 0); | |
2096 | type = TREE_TYPE (DECL_NAME (type)); | |
2097 | ||
2098 | /* TYPE can only be either T* or T&. We can't know which of these it | |
2099 | is by looking at TYPE, but OBJ will be "(T*) x" in the first case, | |
2100 | and something like "(T*)(T&)(T*) x" in the second case. */ | |
2101 | bool reference_p = false; | |
2102 | while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR) | |
2103 | { | |
2104 | reference_p |= TYPE_REF_P (TREE_TYPE (obj)); | |
2105 | obj = TREE_OPERAND (obj, 0); | |
2106 | } | |
2107 | ||
2108 | /* Evaluate the object so that we know its dynamic type. */ | |
2109 | obj = cxx_eval_constant_expression (ctx, obj, /*lval*/false, non_constant_p, | |
2110 | overflow_p); | |
2111 | if (*non_constant_p) | |
2112 | return call; | |
2113 | ||
2114 | /* We expect OBJ to be in form of &d.D.2102 when HINT == 0, | |
2115 | but when HINT is > 0, it can also be something like | |
2116 | &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */ | |
2117 | obj = extract_obj_from_addr_offset (obj); | |
2118 | const tree objtype = TREE_TYPE (obj); | |
2119 | /* If OBJ doesn't refer to a base field, we're done. */ | |
2120 | if (tree t = (TREE_CODE (obj) == COMPONENT_REF | |
2121 | ? TREE_OPERAND (obj, 1) : obj)) | |
2122 | if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t)) | |
de0684bf MP |
2123 | { |
2124 | if (reference_p) | |
2125 | { | |
2126 | if (!ctx->quiet) | |
2127 | { | |
2128 | error_at (loc, "reference %<dynamic_cast%> failed"); | |
2129 | inform (loc, "dynamic type %qT of its operand does " | |
2130 | "not have a base class of type %qT", | |
2131 | objtype, type); | |
2132 | } | |
2133 | *non_constant_p = true; | |
2134 | } | |
2135 | return integer_zero_node; | |
2136 | } | |
22edf943 MP |
2137 | |
2138 | /* [class.cdtor] When a dynamic_cast is used in a constructor ... | |
2139 | or in a destructor ... if the operand of the dynamic_cast refers | |
2140 | to the object under construction or destruction, this object is | |
2141 | considered to be a most derived object that has the type of the | |
2142 | constructor or destructor's class. */ | |
0221c656 | 2143 | tree vtable = build_vfield_ref (obj, objtype); |
22edf943 MP |
2144 | vtable = cxx_eval_constant_expression (ctx, vtable, /*lval*/false, |
2145 | non_constant_p, overflow_p); | |
2146 | if (*non_constant_p) | |
2147 | return call; | |
0221c656 MP |
2148 | /* With -fsanitize=vptr, we initialize all vtable pointers to null, |
2149 | so it's possible that we got a null pointer now. */ | |
2150 | if (integer_zerop (vtable)) | |
2151 | { | |
2152 | if (!ctx->quiet) | |
2153 | error_at (loc, "virtual table pointer is used uninitialized"); | |
2154 | *non_constant_p = true; | |
2155 | return integer_zero_node; | |
2156 | } | |
22edf943 MP |
2157 | /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */ |
2158 | vtable = extract_obj_from_addr_offset (vtable); | |
2159 | const tree mdtype = DECL_CONTEXT (vtable); | |
2160 | ||
2161 | /* Given dynamic_cast<T>(v), | |
2162 | ||
2163 | [expr.dynamic.cast] If C is the class type to which T points or refers, | |
2164 | the runtime check logically executes as follows: | |
2165 | ||
2166 | If, in the most derived object pointed (referred) to by v, v points | |
2167 | (refers) to a public base class subobject of a C object, and if only | |
2168 | one object of type C is derived from the subobject pointed (referred) | |
2169 | to by v the result points (refers) to that C object. | |
2170 | ||
2171 | In this case, HINT >= 0 or -3. */ | |
2172 | if (hint >= 0 || hint == -3) | |
2173 | { | |
2174 | /* Look for a component with type TYPE. */ | |
2175 | tree t = get_component_with_type (obj, type, mdtype); | |
2176 | /* If not accessible, give an error. */ | |
2177 | if (t == error_mark_node) | |
2178 | { | |
2179 | if (reference_p) | |
2180 | { | |
2181 | if (!ctx->quiet) | |
2182 | { | |
2183 | error_at (loc, "reference %<dynamic_cast%> failed"); | |
2184 | inform (loc, "static type %qT of its operand is a " | |
2185 | "non-public base class of dynamic type %qT", | |
2186 | objtype, type); | |
2187 | ||
2188 | } | |
2189 | *non_constant_p = true; | |
2190 | } | |
2191 | return integer_zero_node; | |
2192 | } | |
2193 | else if (t) | |
2194 | /* The result points to the TYPE object. */ | |
2195 | return cp_build_addr_expr (t, complain); | |
2196 | /* Else, TYPE was not found, because the HINT turned out to be wrong. | |
2197 | Fall through to the normal processing. */ | |
2198 | } | |
2199 | ||
2200 | /* Otherwise, if v points (refers) to a public base class subobject of the | |
2201 | most derived object, and the type of the most derived object has a base | |
2202 | class, of type C, that is unambiguous and public, the result points | |
2203 | (refers) to the C subobject of the most derived object. | |
2204 | ||
2205 | But it can also be an invalid case. */ | |
2206 | ||
2207 | /* Get the most derived object. */ | |
2208 | obj = get_component_with_type (obj, mdtype, NULL_TREE); | |
2209 | if (obj == error_mark_node) | |
2210 | { | |
2211 | if (reference_p) | |
2212 | { | |
2213 | if (!ctx->quiet) | |
2214 | { | |
2215 | error_at (loc, "reference %<dynamic_cast%> failed"); | |
2216 | inform (loc, "static type %qT of its operand is a non-public" | |
2217 | " base class of dynamic type %qT", objtype, mdtype); | |
2218 | } | |
2219 | *non_constant_p = true; | |
2220 | } | |
2221 | return integer_zero_node; | |
2222 | } | |
2223 | else | |
2224 | gcc_assert (obj); | |
2225 | ||
2226 | /* Check that the type of the most derived object has a base class | |
2227 | of type TYPE that is unambiguous and public. */ | |
2228 | base_kind b_kind; | |
2229 | tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none); | |
2230 | if (!binfo || binfo == error_mark_node) | |
2231 | { | |
2232 | if (reference_p) | |
2233 | { | |
2234 | if (!ctx->quiet) | |
2235 | { | |
2236 | error_at (loc, "reference %<dynamic_cast%> failed"); | |
2237 | if (b_kind == bk_ambig) | |
2238 | inform (loc, "%qT is an ambiguous base class of dynamic " | |
2239 | "type %qT of its operand", type, mdtype); | |
2240 | else | |
2241 | inform (loc, "dynamic type %qT of its operand does not " | |
2242 | "have an unambiguous public base class %qT", | |
2243 | mdtype, type); | |
2244 | } | |
2245 | *non_constant_p = true; | |
2246 | } | |
2247 | return integer_zero_node; | |
2248 | } | |
2249 | /* If so, return the TYPE subobject of the most derived object. */ | |
2250 | obj = convert_to_base_statically (obj, binfo); | |
2251 | return cp_build_addr_expr (obj, complain); | |
2252 | } | |
2253 | ||
4f6bc28f | 2254 | /* Data structure used by replace_decl and replace_decl_r. */ |
b2562229 | 2255 | |
4f6bc28f | 2256 | struct replace_decl_data |
b2562229 | 2257 | { |
4f6bc28f | 2258 | /* The _DECL we want to replace. */ |
b2562229 PP |
2259 | tree decl; |
2260 | /* The replacement for DECL. */ | |
2261 | tree replacement; | |
4f6bc28f JM |
2262 | /* Trees we've visited. */ |
2263 | hash_set<tree> *pset; | |
b2562229 PP |
2264 | /* Whether we've performed any replacements. */ |
2265 | bool changed; | |
2266 | }; | |
2267 | ||
4f6bc28f | 2268 | /* Helper function for replace_decl, called through cp_walk_tree. */ |
b2562229 PP |
2269 | |
2270 | static tree | |
4f6bc28f | 2271 | replace_decl_r (tree *tp, int *walk_subtrees, void *data) |
b2562229 | 2272 | { |
4f6bc28f | 2273 | replace_decl_data *d = (replace_decl_data *) data; |
b2562229 PP |
2274 | |
2275 | if (*tp == d->decl) | |
2276 | { | |
2277 | *tp = unshare_expr (d->replacement); | |
2278 | d->changed = true; | |
2279 | *walk_subtrees = 0; | |
2280 | } | |
4f6bc28f JM |
2281 | else if (TYPE_P (*tp) |
2282 | || d->pset->add (*tp)) | |
b2562229 PP |
2283 | *walk_subtrees = 0; |
2284 | ||
2285 | return NULL_TREE; | |
2286 | } | |
2287 | ||
4f6bc28f JM |
2288 | /* Replace every occurrence of DECL with (an unshared copy of) |
2289 | REPLACEMENT within the expression *TP. Returns true iff a | |
b2562229 PP |
2290 | replacement was performed. */ |
2291 | ||
4f6bc28f JM |
2292 | bool |
2293 | replace_decl (tree *tp, tree decl, tree replacement) | |
b2562229 | 2294 | { |
4f6bc28f JM |
2295 | gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p |
2296 | (TREE_TYPE (decl), TREE_TYPE (replacement))); | |
2297 | hash_set<tree> pset; | |
2298 | replace_decl_data data = { decl, replacement, &pset, false }; | |
2299 | cp_walk_tree (tp, replace_decl_r, &data, NULL); | |
b2562229 PP |
2300 | return data.changed; |
2301 | } | |
2302 | ||
e6321c45 JM |
2303 | /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */ |
2304 | ||
2305 | static tree | |
2306 | cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl, | |
2307 | bool lval, | |
2308 | bool *non_constant_p, bool *overflow_p) | |
2309 | { | |
2310 | tree function = THUNK_TARGET (thunk_fndecl); | |
2311 | ||
e6321c45 JM |
2312 | if (THUNK_VIRTUAL_OFFSET (thunk_fndecl)) |
2313 | { | |
e6321c45 JM |
2314 | if (!ctx->quiet) |
2315 | { | |
267318a2 JM |
2316 | if (!DECL_DECLARED_CONSTEXPR_P (function)) |
2317 | { | |
2318 | error ("call to non-%<constexpr%> function %qD", function); | |
2319 | explain_invalid_constexpr_fn (function); | |
2320 | } | |
2321 | else | |
2322 | /* virtual_offset is only set for virtual bases, which make the | |
2323 | class non-literal, so we don't need to handle it here. */ | |
2324 | error ("calling constexpr member function %qD through virtual " | |
2325 | "base subobject", function); | |
e6321c45 JM |
2326 | } |
2327 | *non_constant_p = true; | |
2328 | return t; | |
2329 | } | |
2330 | ||
2331 | tree new_call = copy_node (t); | |
2332 | CALL_EXPR_FN (new_call) = function; | |
2333 | TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function)); | |
2334 | ||
2335 | tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl)); | |
2336 | ||
2337 | if (DECL_THIS_THUNK_P (thunk_fndecl)) | |
2338 | { | |
2339 | /* 'this'-adjusting thunk. */ | |
2340 | tree this_arg = CALL_EXPR_ARG (t, 0); | |
2341 | this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg), | |
2342 | this_arg, offset); | |
2343 | CALL_EXPR_ARG (new_call, 0) = this_arg; | |
2344 | } | |
2345 | else | |
2346 | /* Return-adjusting thunk. */ | |
2347 | new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call), | |
2348 | new_call, offset); | |
2349 | ||
2350 | return cxx_eval_constant_expression (ctx, new_call, lval, | |
2351 | non_constant_p, overflow_p); | |
2352 | } | |
2353 | ||
caf17f3a MP |
2354 | /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set |
2355 | its TREE_READONLY flag according to READONLY_P. Used for constexpr | |
2356 | 'tors to detect modifying const objects in a constexpr context. */ | |
2357 | ||
2358 | static void | |
2359 | cxx_set_object_constness (const constexpr_ctx *ctx, tree object, | |
2360 | bool readonly_p, bool *non_constant_p, | |
2361 | bool *overflow_p) | |
2362 | { | |
2363 | if (CLASS_TYPE_P (TREE_TYPE (object)) | |
2364 | && CP_TYPE_CONST_P (TREE_TYPE (object))) | |
2365 | { | |
2366 | /* Subobjects might not be stored in ctx->global->values but we | |
2367 | can get its CONSTRUCTOR by evaluating *this. */ | |
2368 | tree e = cxx_eval_constant_expression (ctx, object, /*lval*/false, | |
2369 | non_constant_p, overflow_p); | |
2370 | if (TREE_CODE (e) == CONSTRUCTOR && !*non_constant_p) | |
2371 | TREE_READONLY (e) = readonly_p; | |
2372 | } | |
2373 | } | |
2374 | ||
2d76680f PC |
2375 | /* Subroutine of cxx_eval_constant_expression. |
2376 | Evaluate the call expression tree T in the context of OLD_CALL expression | |
2377 | evaluation. */ | |
2378 | ||
2379 | static tree | |
3e605b20 | 2380 | cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, |
92a596e8 | 2381 | bool lval, |
2d76680f PC |
2382 | bool *non_constant_p, bool *overflow_p) |
2383 | { | |
861d4af8 AS |
2384 | /* Handle concept checks separately. */ |
2385 | if (concept_check_p (t)) | |
662ef5b5 | 2386 | return evaluate_concept_check (t); |
861d4af8 | 2387 | |
f9d0ca40 | 2388 | location_t loc = cp_expr_loc_or_input_loc (t); |
2d76680f | 2389 | tree fun = get_function_named_in_call (t); |
cce3ae91 | 2390 | constexpr_call new_call |
13de99bc | 2391 | = { NULL, NULL, NULL, 0, ctx->manifestly_const_eval }; |
7ffc7de5 | 2392 | int depth_ok; |
2d76680f | 2393 | |
0b274c17 | 2394 | if (fun == NULL_TREE) |
44a845ca MS |
2395 | return cxx_eval_internal_function (ctx, t, lval, |
2396 | non_constant_p, overflow_p); | |
0b274c17 | 2397 | |
2d76680f PC |
2398 | if (TREE_CODE (fun) != FUNCTION_DECL) |
2399 | { | |
2400 | /* Might be a constexpr function pointer. */ | |
2b3ab879 | 2401 | fun = cxx_eval_constant_expression (ctx, fun, |
92a596e8 | 2402 | /*lval*/false, non_constant_p, |
5a804683 | 2403 | overflow_p); |
2d76680f PC |
2404 | STRIP_NOPS (fun); |
2405 | if (TREE_CODE (fun) == ADDR_EXPR) | |
2406 | fun = TREE_OPERAND (fun, 0); | |
582d2481 JJ |
2407 | /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no |
2408 | indirection, the called expression is a pointer into the | |
2409 | virtual table which should contain FDESC_EXPR. Extract the | |
2410 | FUNCTION_DECL from there. */ | |
2411 | else if (TARGET_VTABLE_USES_DESCRIPTORS | |
2412 | && TREE_CODE (fun) == POINTER_PLUS_EXPR | |
2413 | && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR | |
2414 | && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST) | |
2415 | { | |
2416 | tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0); | |
2417 | if (VAR_P (d) | |
2418 | && DECL_VTABLE_OR_VTT_P (d) | |
2419 | && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE | |
2420 | && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type | |
2421 | && DECL_INITIAL (d) | |
2422 | && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR) | |
2423 | { | |
2424 | tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1), | |
2425 | TYPE_SIZE_UNIT (vtable_entry_type)); | |
2426 | HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i); | |
2427 | if (idx >= 0) | |
2428 | { | |
2429 | tree fdesc | |
2430 | = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value; | |
2431 | if (TREE_CODE (fdesc) == FDESC_EXPR | |
2432 | && integer_zerop (TREE_OPERAND (fdesc, 1))) | |
2433 | fun = TREE_OPERAND (fdesc, 0); | |
2434 | } | |
2435 | } | |
2436 | } | |
2d76680f PC |
2437 | } |
2438 | if (TREE_CODE (fun) != FUNCTION_DECL) | |
2439 | { | |
2b3ab879 | 2440 | if (!ctx->quiet && !*non_constant_p) |
84fa214d | 2441 | error_at (loc, "expression %qE does not designate a %<constexpr%> " |
2d76680f PC |
2442 | "function", fun); |
2443 | *non_constant_p = true; | |
2444 | return t; | |
2445 | } | |
81f97181 | 2446 | if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)) |
2d76680f | 2447 | fun = DECL_CLONED_FUNCTION (fun); |
0b274c17 MP |
2448 | |
2449 | if (is_ubsan_builtin_p (fun)) | |
2450 | return void_node; | |
2451 | ||
3d78e008 | 2452 | if (fndecl_built_in_p (fun)) |
5756d0f9 | 2453 | return cxx_eval_builtin_function_call (ctx, t, fun, |
92a596e8 | 2454 | lval, non_constant_p, overflow_p); |
e6321c45 JM |
2455 | if (DECL_THUNK_P (fun)) |
2456 | return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p); | |
87c2080b | 2457 | if (!maybe_constexpr_fn (fun)) |
2d76680f | 2458 | { |
8412b939 JJ |
2459 | if (TREE_CODE (t) == CALL_EXPR |
2460 | && cxx_replaceable_global_alloc_fn (fun) | |
2461 | && (CALL_FROM_NEW_OR_DELETE_P (t) | |
2ffc2645 | 2462 | || is_std_allocator_allocate (ctx->call))) |
8e007055 JJ |
2463 | { |
2464 | const int nargs = call_expr_nargs (t); | |
2465 | tree arg0 = NULL_TREE; | |
2466 | for (int i = 0; i < nargs; ++i) | |
2467 | { | |
2468 | tree arg = CALL_EXPR_ARG (t, i); | |
2469 | arg = cxx_eval_constant_expression (ctx, arg, false, | |
2470 | non_constant_p, overflow_p); | |
2471 | VERIFY_CONSTANT (arg); | |
2472 | if (i == 0) | |
2473 | arg0 = arg; | |
2474 | } | |
2475 | gcc_assert (arg0); | |
2476 | if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun))) | |
2477 | { | |
2478 | tree type = build_array_type_nelts (char_type_node, | |
2479 | tree_to_uhwi (arg0)); | |
815baade JJ |
2480 | tree var = build_decl (loc, VAR_DECL, |
2481 | (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) | |
2482 | & OVL_OP_FLAG_VEC) | |
2483 | ? heap_vec_uninit_identifier | |
2484 | : heap_uninit_identifier, | |
8e007055 JJ |
2485 | type); |
2486 | DECL_ARTIFICIAL (var) = 1; | |
2487 | TREE_STATIC (var) = 1; | |
a8db7887 JJ |
2488 | // Temporarily register the artificial var in varpool, |
2489 | // so that comparisons of its address against NULL are folded | |
2490 | // through nonzero_address even with | |
2491 | // -fno-delete-null-pointer-checks or that comparison of | |
2492 | // addresses of different heap artificial vars is folded too. | |
2493 | // See PR98988 and PR99031. | |
2494 | varpool_node::finalize_decl (var); | |
8e007055 JJ |
2495 | ctx->global->heap_vars.safe_push (var); |
2496 | ctx->global->values.put (var, NULL_TREE); | |
2497 | return fold_convert (ptr_type_node, build_address (var)); | |
2498 | } | |
2499 | else | |
2500 | { | |
2501 | STRIP_NOPS (arg0); | |
2502 | if (TREE_CODE (arg0) == ADDR_EXPR | |
2503 | && VAR_P (TREE_OPERAND (arg0, 0))) | |
2504 | { | |
2505 | tree var = TREE_OPERAND (arg0, 0); | |
2506 | if (DECL_NAME (var) == heap_uninit_identifier | |
2507 | || DECL_NAME (var) == heap_identifier) | |
2508 | { | |
815baade JJ |
2509 | if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) |
2510 | & OVL_OP_FLAG_VEC) | |
2511 | { | |
2512 | if (!ctx->quiet) | |
2513 | { | |
2514 | error_at (loc, "array deallocation of object " | |
2515 | "allocated with non-array " | |
2516 | "allocation"); | |
2517 | inform (DECL_SOURCE_LOCATION (var), | |
2518 | "allocation performed here"); | |
2519 | } | |
2520 | *non_constant_p = true; | |
2521 | return t; | |
2522 | } | |
2523 | DECL_NAME (var) = heap_deleted_identifier; | |
2524 | ctx->global->values.remove (var); | |
2525 | ctx->global->heap_dealloc_count++; | |
2526 | return void_node; | |
2527 | } | |
2528 | else if (DECL_NAME (var) == heap_vec_uninit_identifier | |
2529 | || DECL_NAME (var) == heap_vec_identifier) | |
2530 | { | |
2531 | if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) | |
2532 | & OVL_OP_FLAG_VEC) == 0) | |
2533 | { | |
2534 | if (!ctx->quiet) | |
2535 | { | |
2536 | error_at (loc, "non-array deallocation of " | |
2537 | "object allocated with array " | |
2538 | "allocation"); | |
2539 | inform (DECL_SOURCE_LOCATION (var), | |
2540 | "allocation performed here"); | |
2541 | } | |
2542 | *non_constant_p = true; | |
2543 | return t; | |
2544 | } | |
8e007055 JJ |
2545 | DECL_NAME (var) = heap_deleted_identifier; |
2546 | ctx->global->values.remove (var); | |
f74f6092 | 2547 | ctx->global->heap_dealloc_count++; |
8e007055 JJ |
2548 | return void_node; |
2549 | } | |
2550 | else if (DECL_NAME (var) == heap_deleted_identifier) | |
2551 | { | |
2552 | if (!ctx->quiet) | |
2553 | error_at (loc, "deallocation of already deallocated " | |
2554 | "storage"); | |
2555 | *non_constant_p = true; | |
2556 | return t; | |
2557 | } | |
2558 | } | |
2559 | if (!ctx->quiet) | |
2560 | error_at (loc, "deallocation of storage that was " | |
2561 | "not previously allocated"); | |
2562 | *non_constant_p = true; | |
2563 | return t; | |
2564 | } | |
2565 | } | |
cf650568 JJ |
2566 | /* Allow placement new in std::construct_at, just return the second |
2567 | argument. */ | |
8412b939 JJ |
2568 | if (TREE_CODE (t) == CALL_EXPR |
2569 | && cxx_placement_new_fn (fun) | |
2ffc2645 | 2570 | && is_std_construct_at (ctx->call)) |
cf650568 JJ |
2571 | { |
2572 | const int nargs = call_expr_nargs (t); | |
2573 | tree arg1 = NULL_TREE; | |
2574 | for (int i = 0; i < nargs; ++i) | |
2575 | { | |
2576 | tree arg = CALL_EXPR_ARG (t, i); | |
2577 | arg = cxx_eval_constant_expression (ctx, arg, false, | |
2578 | non_constant_p, overflow_p); | |
cf650568 JJ |
2579 | if (i == 1) |
2580 | arg1 = arg; | |
2805fcb3 JJ |
2581 | else |
2582 | VERIFY_CONSTANT (arg); | |
cf650568 JJ |
2583 | } |
2584 | gcc_assert (arg1); | |
2585 | return arg1; | |
2586 | } | |
22edf943 MP |
2587 | else if (cxx_dynamic_cast_fn_p (fun)) |
2588 | return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p); | |
2589 | ||
2b3ab879 | 2590 | if (!ctx->quiet) |
2d76680f | 2591 | { |
11399477 | 2592 | if (!lambda_static_thunk_p (fun)) |
84fa214d | 2593 | error_at (loc, "call to non-%<constexpr%> function %qD", fun); |
2d76680f PC |
2594 | explain_invalid_constexpr_fn (fun); |
2595 | } | |
2596 | *non_constant_p = true; | |
2597 | return t; | |
2598 | } | |
2599 | ||
86461cad JM |
2600 | constexpr_ctx new_ctx = *ctx; |
2601 | if (DECL_CONSTRUCTOR_P (fun) && !ctx->object | |
2602 | && TREE_CODE (t) == AGGR_INIT_EXPR) | |
2603 | { | |
2604 | /* We want to have an initialization target for an AGGR_INIT_EXPR. | |
2605 | If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */ | |
2606 | new_ctx.object = AGGR_INIT_EXPR_SLOT (t); | |
2607 | tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL); | |
e8c48716 | 2608 | CONSTRUCTOR_NO_CLEARING (ctor) = true; |
8e007055 | 2609 | ctx->global->values.put (new_ctx.object, ctor); |
86461cad JM |
2610 | ctx = &new_ctx; |
2611 | } | |
2612 | ||
2d76680f PC |
2613 | /* Shortcut trivial constructor/op=. */ |
2614 | if (trivial_fn_p (fun)) | |
2615 | { | |
86461cad | 2616 | tree init = NULL_TREE; |
2d76680f | 2617 | if (call_expr_nargs (t) == 2) |
86461cad | 2618 | init = convert_from_reference (get_nth_callarg (t, 1)); |
2d76680f PC |
2619 | else if (TREE_CODE (t) == AGGR_INIT_EXPR |
2620 | && AGGR_INIT_ZERO_FIRST (t)) | |
86461cad JM |
2621 | init = build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false); |
2622 | if (init) | |
2623 | { | |
2624 | tree op = get_nth_callarg (t, 0); | |
2625 | if (is_dummy_object (op)) | |
2626 | op = ctx->object; | |
2627 | else | |
2628 | op = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (op)), op); | |
2629 | tree set = build2 (MODIFY_EXPR, TREE_TYPE (op), op, init); | |
c8816908 JM |
2630 | new_ctx.call = &new_call; |
2631 | return cxx_eval_constant_expression (&new_ctx, set, lval, | |
86461cad JM |
2632 | non_constant_p, overflow_p); |
2633 | } | |
2d76680f PC |
2634 | } |
2635 | ||
1595fe44 JM |
2636 | bool non_constant_args = false; |
2637 | new_call.bindings | |
2638 | = cxx_bind_parameters_in_call (ctx, t, fun, non_constant_p, | |
2639 | overflow_p, &non_constant_args); | |
2640 | ||
2641 | /* We build up the bindings list before we know whether we already have this | |
2642 | call cached. If we don't end up saving these bindings, ggc_free them when | |
2643 | this function exits. */ | |
2644 | class free_bindings | |
2645 | { | |
2646 | tree *bindings; | |
2647 | public: | |
2648 | free_bindings (tree &b): bindings (&b) { } | |
2649 | ~free_bindings () { if (bindings) ggc_free (*bindings); } | |
2650 | void preserve () { bindings = NULL; } | |
2651 | } fb (new_call.bindings); | |
2652 | ||
2653 | if (*non_constant_p) | |
2654 | return t; | |
2655 | ||
81371eff JM |
2656 | /* We can't defer instantiating the function any longer. */ |
2657 | if (!DECL_INITIAL (fun) | |
f65a3299 PP |
2658 | && DECL_TEMPLOID_INSTANTIATION (fun) |
2659 | && !uid_sensitive_constexpr_evaluation_p ()) | |
81371eff | 2660 | { |
f065303f JM |
2661 | location_t save_loc = input_location; |
2662 | input_location = loc; | |
81371eff | 2663 | ++function_depth; |
9aeadd8c JM |
2664 | if (ctx->manifestly_const_eval) |
2665 | FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true; | |
81371eff JM |
2666 | instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false); |
2667 | --function_depth; | |
f065303f | 2668 | input_location = save_loc; |
81371eff JM |
2669 | } |
2670 | ||
2d76680f | 2671 | /* If in direct recursive call, optimize definition search. */ |
c8816908 | 2672 | if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun) |
3e605b20 | 2673 | new_call.fundef = ctx->call->fundef; |
2d76680f PC |
2674 | else |
2675 | { | |
2676 | new_call.fundef = retrieve_constexpr_fundef (fun); | |
4ddcdbf9 | 2677 | if (new_call.fundef == NULL || new_call.fundef->body == NULL |
4f05d85a | 2678 | || new_call.fundef->result == error_mark_node |
4ddcdbf9 | 2679 | || fun == current_function_decl) |
2d76680f | 2680 | { |
2b3ab879 | 2681 | if (!ctx->quiet) |
2d76680f | 2682 | { |
4ddcdbf9 JM |
2683 | /* We need to check for current_function_decl here in case we're |
2684 | being called during cp_fold_function, because at that point | |
2685 | DECL_INITIAL is set properly and we have a fundef but we | |
2686 | haven't lowered invisirefs yet (c++/70344). */ | |
2687 | if (DECL_INITIAL (fun) == error_mark_node | |
2688 | || fun == current_function_decl) | |
ddd6d421 JM |
2689 | error_at (loc, "%qD called in a constant expression before its " |
2690 | "definition is complete", fun); | |
2691 | else if (DECL_INITIAL (fun)) | |
2d76680f | 2692 | { |
98e5a19a JM |
2693 | /* The definition of fun was somehow unsuitable. But pretend |
2694 | that lambda static thunks don't exist. */ | |
2695 | if (!lambda_static_thunk_p (fun)) | |
2696 | error_at (loc, "%qD called in a constant expression", fun); | |
2d76680f PC |
2697 | explain_invalid_constexpr_fn (fun); |
2698 | } | |
2699 | else | |
2700 | error_at (loc, "%qD used before its definition", fun); | |
2701 | } | |
2702 | *non_constant_p = true; | |
2703 | return t; | |
2704 | } | |
2705 | } | |
60813a46 | 2706 | |
2d76680f PC |
2707 | depth_ok = push_cx_call_context (t); |
2708 | ||
caf17f3a | 2709 | /* Remember the object we are constructing or destructing. */ |
04e1749c | 2710 | tree new_obj = NULL_TREE; |
caf17f3a | 2711 | if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun)) |
04e1749c | 2712 | { |
caf17f3a | 2713 | /* In a cdtor, it should be the first `this' argument. |
04e1749c MP |
2714 | At this point it has already been evaluated in the call |
2715 | to cxx_bind_parameters_in_call. */ | |
2716 | new_obj = TREE_VEC_ELT (new_call.bindings, 0); | |
2717 | STRIP_NOPS (new_obj); | |
2718 | if (TREE_CODE (new_obj) == ADDR_EXPR) | |
2719 | new_obj = TREE_OPERAND (new_obj, 0); | |
64da1b76 PP |
2720 | |
2721 | if (ctx->call && ctx->call->fundef | |
2722 | && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl)) | |
2723 | { | |
2724 | tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0); | |
2725 | STRIP_NOPS (cur_obj); | |
2726 | if (TREE_CODE (cur_obj) == ADDR_EXPR) | |
2727 | cur_obj = TREE_OPERAND (cur_obj, 0); | |
2728 | if (new_obj == cur_obj) | |
2729 | /* We're calling the target constructor of a delegating | |
2730 | constructor, or accessing a base subobject through a | |
2731 | NOP_EXPR as part of a call to a base constructor, so | |
2732 | there is no new (sub)object. */ | |
2733 | new_obj = NULL_TREE; | |
2734 | } | |
04e1749c MP |
2735 | } |
2736 | ||
12d9ce19 | 2737 | tree result = NULL_TREE; |
2d76680f | 2738 | |
12d9ce19 | 2739 | constexpr_call *entry = NULL; |
4a58d2fe | 2740 | if (depth_ok && !non_constant_args && ctx->strict) |
2d76680f | 2741 | { |
cce3ae91 JJ |
2742 | new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef); |
2743 | new_call.hash | |
2744 | = iterative_hash_template_arg (new_call.bindings, new_call.hash); | |
2745 | new_call.hash | |
13de99bc | 2746 | = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash); |
12d9ce19 JM |
2747 | |
2748 | /* If we have seen this call before, we are done. */ | |
2749 | maybe_initialize_constexpr_call_table (); | |
2750 | constexpr_call **slot | |
2751 | = constexpr_call_table->find_slot (&new_call, INSERT); | |
2752 | entry = *slot; | |
2753 | if (entry == NULL) | |
2754 | { | |
7ffc7de5 JM |
2755 | /* Only cache up to constexpr_cache_depth to limit memory use. */ |
2756 | if (depth_ok < constexpr_cache_depth) | |
2757 | { | |
2758 | /* We need to keep a pointer to the entry, not just the slot, as | |
2759 | the slot can move during evaluation of the body. */ | |
2760 | *slot = entry = ggc_alloc<constexpr_call> (); | |
2761 | *entry = new_call; | |
2762 | fb.preserve (); | |
2763 | } | |
12d9ce19 | 2764 | } |
7ffc7de5 JM |
2765 | /* Calls that are in progress have their result set to NULL, so that we |
2766 | can detect circular dependencies. Now that we only cache up to | |
2767 | constexpr_cache_depth this won't catch circular dependencies that | |
2768 | start deeper, but they'll hit the recursion or ops limit. */ | |
12d9ce19 JM |
2769 | else if (entry->result == NULL) |
2770 | { | |
2771 | if (!ctx->quiet) | |
2772 | error ("call has circular dependency"); | |
2773 | *non_constant_p = true; | |
2774 | entry->result = result = error_mark_node; | |
2775 | } | |
2776 | else | |
2777 | result = entry->result; | |
2d76680f PC |
2778 | } |
2779 | ||
2780 | if (!depth_ok) | |
2781 | { | |
2b3ab879 | 2782 | if (!ctx->quiet) |
84fa214d | 2783 | error ("%<constexpr%> evaluation depth exceeds maximum of %d (use " |
7e7a6ed7 | 2784 | "%<-fconstexpr-depth=%> to increase the maximum)", |
2d76680f PC |
2785 | max_constexpr_depth); |
2786 | *non_constant_p = true; | |
12d9ce19 | 2787 | result = error_mark_node; |
2d76680f PC |
2788 | } |
2789 | else | |
2790 | { | |
f74f6092 | 2791 | bool cacheable = true; |
de54de93 JM |
2792 | if (result && result != error_mark_node) |
2793 | /* OK */; | |
2794 | else if (!DECL_SAVED_TREE (fun)) | |
2795 | { | |
2796 | /* When at_eof >= 2, cgraph has started throwing away | |
2797 | DECL_SAVED_TREE, so fail quietly. FIXME we get here because of | |
2798 | late code generation for VEC_INIT_EXPR, which needs to be | |
2799 | completely reconsidered. */ | |
2800 | gcc_assert (at_eof >= 2 && ctx->quiet); | |
2801 | *non_constant_p = true; | |
2802 | } | |
f65a3299 | 2803 | else if (tree copy = get_fundef_copy (new_call.fundef)) |
3e605b20 | 2804 | { |
c0daf32d | 2805 | tree body, parms, res; |
620adbec | 2806 | releasing_vec ctors; |
0567dcd2 | 2807 | |
c0daf32d | 2808 | /* Reuse or create a new unshared copy of this function's body. */ |
97f3003f JM |
2809 | body = TREE_PURPOSE (copy); |
2810 | parms = TREE_VALUE (copy); | |
2811 | res = TREE_TYPE (copy); | |
0567dcd2 MP |
2812 | |
2813 | /* Associate the bindings with the remapped parms. */ | |
2814 | tree bound = new_call.bindings; | |
2815 | tree remapped = parms; | |
3c961dc7 | 2816 | for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i) |
60813a46 | 2817 | { |
3c961dc7 | 2818 | tree arg = TREE_VEC_ELT (bound, i); |
4953b790 JM |
2819 | if (entry) |
2820 | { | |
2821 | /* Unshare args going into the hash table to separate them | |
2822 | from the caller's context, for better GC and to avoid | |
2823 | problems with verify_gimple. */ | |
db38a029 | 2824 | arg = unshare_expr_without_location (arg); |
4953b790 | 2825 | TREE_VEC_ELT (bound, i) = arg; |
5afd90c5 JJ |
2826 | |
2827 | /* And then unshare again so the callee doesn't change the | |
2828 | argument values in the hash table. XXX Could we unshare | |
2829 | lazily in cxx_eval_store_expression? */ | |
2830 | arg = unshare_constructor (arg); | |
2831 | if (TREE_CODE (arg) == CONSTRUCTOR) | |
2832 | vec_safe_push (ctors, arg); | |
4953b790 | 2833 | } |
8e007055 | 2834 | ctx->global->values.put (remapped, arg); |
0567dcd2 | 2835 | remapped = DECL_CHAIN (remapped); |
60813a46 | 2836 | } |
0567dcd2 | 2837 | /* Add the RESULT_DECL to the values map, too. */ |
cd3ca6cb JM |
2838 | gcc_assert (!DECL_BY_REFERENCE (res)); |
2839 | ctx->global->values.put (res, NULL_TREE); | |
60813a46 | 2840 | |
ee1de08d JJ |
2841 | /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that |
2842 | we can forget their values after the call. */ | |
c0daf32d | 2843 | constexpr_ctx ctx_with_save_exprs = *ctx; |
0ee28590 | 2844 | auto_vec<tree, 10> save_exprs; |
c0daf32d | 2845 | ctx_with_save_exprs.save_exprs = &save_exprs; |
1e163090 | 2846 | ctx_with_save_exprs.call = &new_call; |
f74f6092 JJ |
2847 | unsigned save_heap_alloc_count = ctx->global->heap_vars.length (); |
2848 | unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count; | |
c0daf32d | 2849 | |
caf17f3a MP |
2850 | /* If this is a constexpr destructor, the object's const and volatile |
2851 | semantics are no longer in effect; see [class.dtor]p5. */ | |
2852 | if (new_obj && DECL_DESTRUCTOR_P (fun)) | |
2853 | cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false, | |
2854 | non_constant_p, overflow_p); | |
2855 | ||
0567dcd2 | 2856 | tree jump_target = NULL_TREE; |
c0daf32d | 2857 | cxx_eval_constant_expression (&ctx_with_save_exprs, body, |
0567dcd2 MP |
2858 | lval, non_constant_p, overflow_p, |
2859 | &jump_target); | |
60813a46 | 2860 | |
0567dcd2 | 2861 | if (DECL_CONSTRUCTOR_P (fun)) |
de07cff9 PP |
2862 | { |
2863 | /* This can be null for a subobject constructor call, in | |
2864 | which case what we care about is the initialization | |
2865 | side-effects rather than the value. We could get at the | |
2866 | value by evaluating *this, but we don't bother; there's | |
2867 | no need to put such a call in the hash table. */ | |
2868 | result = lval ? ctx->object : ctx->ctor; | |
2869 | ||
2870 | /* If we've just evaluated a subobject constructor call for an | |
2871 | empty union member, it might not have produced a side effect | |
2872 | that actually activated the union member. So produce such a | |
2873 | side effect now to ensure the union appears initialized. */ | |
2874 | if (!result && new_obj | |
2875 | && TREE_CODE (new_obj) == COMPONENT_REF | |
2876 | && TREE_CODE (TREE_TYPE | |
2877 | (TREE_OPERAND (new_obj, 0))) == UNION_TYPE | |
2878 | && is_really_empty_class (TREE_TYPE (new_obj), | |
2879 | /*ignore_vptr*/false)) | |
2880 | { | |
2881 | tree activate = build2 (MODIFY_EXPR, TREE_TYPE (new_obj), | |
2882 | new_obj, | |
2883 | build_constructor (TREE_TYPE (new_obj), | |
2884 | NULL)); | |
2885 | cxx_eval_constant_expression (ctx, activate, lval, | |
2886 | non_constant_p, overflow_p); | |
2887 | ggc_free (activate); | |
2888 | } | |
2889 | } | |
0567dcd2 MP |
2890 | else if (VOID_TYPE_P (TREE_TYPE (res))) |
2891 | result = void_node; | |
2892 | else | |
2893 | { | |
cd3ca6cb | 2894 | result = *ctx->global->values.get (res); |
0567dcd2 | 2895 | if (result == NULL_TREE && !*non_constant_p) |
60813a46 | 2896 | { |
0567dcd2 | 2897 | if (!ctx->quiet) |
84fa214d | 2898 | error ("%<constexpr%> call flows off the end " |
0567dcd2 MP |
2899 | "of the function"); |
2900 | *non_constant_p = true; | |
60813a46 | 2901 | } |
60813a46 | 2902 | } |
0567dcd2 | 2903 | |
04e1749c MP |
2904 | /* At this point, the object's constructor will have run, so |
2905 | the object is no longer under construction, and its possible | |
2906 | 'const' semantics now apply. Make a note of this fact by | |
2907 | marking the CONSTRUCTOR TREE_READONLY. */ | |
caf17f3a MP |
2908 | if (new_obj && DECL_CONSTRUCTOR_P (fun)) |
2909 | cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true, | |
2910 | non_constant_p, overflow_p); | |
04e1749c | 2911 | |
ee1de08d JJ |
2912 | /* Forget the saved values of the callee's SAVE_EXPRs and |
2913 | TARGET_EXPRs. */ | |
3f207ab3 | 2914 | for (tree save_expr : save_exprs) |
8e007055 | 2915 | ctx->global->values.remove (save_expr); |
c0daf32d | 2916 | |
0567dcd2 MP |
2917 | /* Remove the parms/result from the values map. Is it worth |
2918 | bothering to do this when the map itself is only live for | |
2919 | one constexpr evaluation? If so, maybe also clear out | |
2920 | other vars from call, maybe in BIND_EXPR handling? */ | |
8e007055 | 2921 | ctx->global->values.remove (res); |
0567dcd2 | 2922 | for (tree parm = parms; parm; parm = TREE_CHAIN (parm)) |
8e007055 | 2923 | ctx->global->values.remove (parm); |
c0daf32d | 2924 | |
620adbec JM |
2925 | /* Free any parameter CONSTRUCTORs we aren't returning directly. */ |
2926 | while (!ctors->is_empty ()) | |
2927 | { | |
2928 | tree c = ctors->pop (); | |
2929 | if (c != result) | |
2930 | free_constructor (c); | |
2931 | } | |
2932 | ||
c0daf32d PP |
2933 | /* Make the unshared function copy we used available for re-use. */ |
2934 | save_fundef_copy (fun, copy); | |
f74f6092 JJ |
2935 | |
2936 | /* If the call allocated some heap object that hasn't been | |
2937 | deallocated during the call, or if it deallocated some heap | |
2938 | object it has not allocated, the call isn't really stateless | |
2939 | for the constexpr evaluation and should not be cached. | |
2940 | It is fine if the call allocates something and deallocates it | |
2941 | too. */ | |
2942 | if (entry | |
2943 | && (save_heap_alloc_count != ctx->global->heap_vars.length () | |
2944 | || (save_heap_dealloc_count | |
2945 | != ctx->global->heap_dealloc_count))) | |
2946 | { | |
2947 | tree heap_var; | |
2948 | unsigned int i; | |
2949 | if ((ctx->global->heap_vars.length () | |
2950 | - ctx->global->heap_dealloc_count) | |
2951 | != save_heap_alloc_count - save_heap_dealloc_count) | |
2952 | cacheable = false; | |
2953 | else | |
2954 | FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var, | |
2955 | save_heap_alloc_count) | |
2956 | if (DECL_NAME (heap_var) != heap_deleted_identifier) | |
2957 | { | |
2958 | cacheable = false; | |
2959 | break; | |
2960 | } | |
2961 | } | |
b2562229 PP |
2962 | |
2963 | /* Rewrite all occurrences of the function's RESULT_DECL with the | |
2964 | current object under construction. */ | |
2965 | if (!*non_constant_p && ctx->object | |
bb1f0b50 | 2966 | && CLASS_TYPE_P (TREE_TYPE (res)) |
b2562229 | 2967 | && !is_empty_class (TREE_TYPE (res))) |
4f6bc28f | 2968 | if (replace_decl (&result, res, ctx->object)) |
b2562229 | 2969 | cacheable = false; |
3e605b20 | 2970 | } |
aaa26bf4 JM |
2971 | else |
2972 | /* Couldn't get a function copy to evaluate. */ | |
2973 | *non_constant_p = true; | |
60813a46 | 2974 | |
2d76680f PC |
2975 | if (result == error_mark_node) |
2976 | *non_constant_p = true; | |
52228180 | 2977 | if (*non_constant_p || *overflow_p) |
12d9ce19 | 2978 | result = error_mark_node; |
0567dcd2 | 2979 | else if (!result) |
60813a46 | 2980 | result = void_node; |
12d9ce19 | 2981 | if (entry) |
f74f6092 | 2982 | entry->result = cacheable ? result : error_mark_node; |
2d76680f PC |
2983 | } |
2984 | ||
7906797e MP |
2985 | /* The result of a constexpr function must be completely initialized. |
2986 | ||
2987 | However, in C++20, a constexpr constructor doesn't necessarily have | |
2988 | to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING | |
2989 | in order to detect reading an unitialized object in constexpr instead | |
2990 | of value-initializing it. (reduced_constant_expression_p is expected to | |
2991 | take care of clearing the flag.) */ | |
2992 | if (TREE_CODE (result) == CONSTRUCTOR | |
b04445d4 | 2993 | && (cxx_dialect < cxx20 |
7906797e | 2994 | || !DECL_CONSTRUCTOR_P (fun))) |
ecc57615 | 2995 | clear_no_implicit_zero (result); |
f64e0c02 | 2996 | |
2d76680f | 2997 | pop_cx_call_context (); |
9b9eb42a | 2998 | return result; |
2d76680f PC |
2999 | } |
3000 | ||
7906797e MP |
3001 | /* Return true if T is a valid constant initializer. If a CONSTRUCTOR |
3002 | initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be | |
3003 | cleared. | |
3004 | FIXME speed this up, it's taking 16% of compile time on sieve testcase. */ | |
2d76680f PC |
3005 | |
3006 | bool | |
3007 | reduced_constant_expression_p (tree t) | |
3008 | { | |
4930c53e MP |
3009 | if (t == NULL_TREE) |
3010 | return false; | |
3011 | ||
2d76680f PC |
3012 | switch (TREE_CODE (t)) |
3013 | { | |
3014 | case PTRMEM_CST: | |
3015 | /* Even if we can't lower this yet, it's constant. */ | |
3016 | return true; | |
3017 | ||
3018 | case CONSTRUCTOR: | |
3019 | /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */ | |
fae00a0a | 3020 | tree field; |
e8c48716 | 3021 | if (CONSTRUCTOR_NO_CLEARING (t)) |
6f11ddd8 JM |
3022 | { |
3023 | if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE) | |
3024 | /* An initialized vector would have a VECTOR_CST. */ | |
3025 | return false; | |
e948436e | 3026 | else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) |
baf05d54 PP |
3027 | { |
3028 | /* There must be a valid constant initializer at every array | |
3029 | index. */ | |
3030 | tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t))); | |
3031 | tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t))); | |
3032 | tree cursor = min; | |
fae00a0a | 3033 | for (auto &e: CONSTRUCTOR_ELTS (t)) |
baf05d54 | 3034 | { |
fae00a0a | 3035 | if (!reduced_constant_expression_p (e.value)) |
baf05d54 | 3036 | return false; |
fae00a0a | 3037 | if (array_index_cmp (cursor, e.index) != 0) |
baf05d54 | 3038 | return false; |
fae00a0a JM |
3039 | if (TREE_CODE (e.index) == RANGE_EXPR) |
3040 | cursor = TREE_OPERAND (e.index, 1); | |
baf05d54 PP |
3041 | cursor = int_const_binop (PLUS_EXPR, cursor, size_one_node); |
3042 | } | |
3043 | if (find_array_ctor_elt (t, max) == -1) | |
3044 | return false; | |
3045 | goto ok; | |
3046 | } | |
b04445d4 | 3047 | else if (cxx_dialect >= cxx20 |
b599bf9d PP |
3048 | && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE) |
3049 | { | |
3050 | if (CONSTRUCTOR_NELTS (t) == 0) | |
3051 | /* An initialized union has a constructor element. */ | |
3052 | return false; | |
3053 | /* And it only initializes one member. */ | |
3054 | field = NULL_TREE; | |
3055 | } | |
6f11ddd8 JM |
3056 | else |
3057 | field = next_initializable_field (TYPE_FIELDS (TREE_TYPE (t))); | |
3058 | } | |
7368cfa4 JM |
3059 | else |
3060 | field = NULL_TREE; | |
fae00a0a | 3061 | for (auto &e: CONSTRUCTOR_ELTS (t)) |
125db6a1 | 3062 | { |
4930c53e MP |
3063 | /* If VAL is null, we're in the middle of initializing this |
3064 | element. */ | |
fae00a0a | 3065 | if (!reduced_constant_expression_p (e.value)) |
125db6a1 | 3066 | return false; |
d6ff2207 | 3067 | /* Empty class field may or may not have an initializer. */ |
fae00a0a | 3068 | for (; field && e.index != field; |
d6ff2207 MP |
3069 | field = next_initializable_field (DECL_CHAIN (field))) |
3070 | if (!is_really_empty_class (TREE_TYPE (field), | |
3071 | /*ignore_vptr*/false)) | |
3072 | return false; | |
7368cfa4 | 3073 | if (field) |
d6ff2207 | 3074 | field = next_initializable_field (DECL_CHAIN (field)); |
125db6a1 | 3075 | } |
7906797e MP |
3076 | /* There could be a non-empty field at the end. */ |
3077 | for (; field; field = next_initializable_field (DECL_CHAIN (field))) | |
3078 | if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false)) | |
3079 | return false; | |
baf05d54 | 3080 | ok: |
7906797e | 3081 | if (CONSTRUCTOR_NO_CLEARING (t)) |
7368cfa4 | 3082 | /* All the fields are initialized. */ |
e8c48716 | 3083 | CONSTRUCTOR_NO_CLEARING (t) = false; |
2d76680f PC |
3084 | return true; |
3085 | ||
3086 | default: | |
3087 | /* FIXME are we calling this too much? */ | |
3088 | return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE; | |
3089 | } | |
3090 | } | |
3091 | ||
3092 | /* Some expressions may have constant operands but are not constant | |
7d75ea04 JJ |
3093 | themselves, such as 1/0. Call this function to check for that |
3094 | condition. | |
2d76680f PC |
3095 | |
3096 | We only call this in places that require an arithmetic constant, not in | |
3097 | places where we might have a non-constant expression that can be a | |
3098 | component of a constant expression, such as the address of a constexpr | |
3099 | variable that might be dereferenced later. */ | |
3100 | ||
3101 | static bool | |
3102 | verify_constant (tree t, bool allow_non_constant, bool *non_constant_p, | |
3103 | bool *overflow_p) | |
3104 | { | |
596334fa JM |
3105 | if (!*non_constant_p && !reduced_constant_expression_p (t) |
3106 | && t != void_node) | |
2d76680f PC |
3107 | { |
3108 | if (!allow_non_constant) | |
3109 | error ("%q+E is not a constant expression", t); | |
3110 | *non_constant_p = true; | |
3111 | } | |
3112 | if (TREE_OVERFLOW_P (t)) | |
3113 | { | |
3114 | if (!allow_non_constant) | |
3115 | { | |
3116 | permerror (input_location, "overflow in constant expression"); | |
3117 | /* If we're being permissive (and are in an enforcing | |
3118 | context), ignore the overflow. */ | |
3119 | if (flag_permissive) | |
3120 | return *non_constant_p; | |
3121 | } | |
3122 | *overflow_p = true; | |
3123 | } | |
3124 | return *non_constant_p; | |
3125 | } | |
3126 | ||
253a921b MP |
3127 | /* Check whether the shift operation with code CODE and type TYPE on LHS |
3128 | and RHS is undefined. If it is, give an error with an explanation, | |
3129 | and return true; return false otherwise. */ | |
3130 | ||
3131 | static bool | |
3132 | cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx, | |
3133 | enum tree_code code, tree type, tree lhs, tree rhs) | |
3134 | { | |
3135 | if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR) | |
3136 | || TREE_CODE (lhs) != INTEGER_CST | |
3137 | || TREE_CODE (rhs) != INTEGER_CST) | |
3138 | return false; | |
3139 | ||
3140 | tree lhstype = TREE_TYPE (lhs); | |
3141 | unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs)); | |
3142 | ||
3143 | /* [expr.shift] The behavior is undefined if the right operand | |
3144 | is negative, or greater than or equal to the length in bits | |
3145 | of the promoted left operand. */ | |
3146 | if (tree_int_cst_sgn (rhs) == -1) | |
3147 | { | |
3148 | if (!ctx->quiet) | |
5342156c MP |
3149 | permerror (loc, "right operand of shift expression %q+E is negative", |
3150 | build2_loc (loc, code, type, lhs, rhs)); | |
3151 | return (!flag_permissive || ctx->quiet); | |
253a921b MP |
3152 | } |
3153 | if (compare_tree_int (rhs, uprec) >= 0) | |
3154 | { | |
3155 | if (!ctx->quiet) | |
a9c697b8 MS |
3156 | permerror (loc, "right operand of shift expression %q+E is greater " |
3157 | "than or equal to the precision %wu of the left operand", | |
3158 | build2_loc (loc, code, type, lhs, rhs), uprec); | |
5342156c | 3159 | return (!flag_permissive || ctx->quiet); |
253a921b MP |
3160 | } |
3161 | ||
3162 | /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...] | |
3163 | if E1 has a signed type and non-negative value, and E1x2^E2 is | |
3164 | representable in the corresponding unsigned type of the result type, | |
3165 | then that value, converted to the result type, is the resulting value; | |
8ee09943 | 3166 | otherwise, the behavior is undefined. |
b04445d4 | 3167 | For C++20: |
8ee09943 JJ |
3168 | The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo |
3169 | 2^N, where N is the range exponent of the type of the result. */ | |
3170 | if (code == LSHIFT_EXPR | |
3171 | && !TYPE_UNSIGNED (lhstype) | |
3172 | && cxx_dialect >= cxx11 | |
b04445d4 | 3173 | && cxx_dialect < cxx20) |
253a921b MP |
3174 | { |
3175 | if (tree_int_cst_sgn (lhs) == -1) | |
3176 | { | |
3177 | if (!ctx->quiet) | |
5342156c MP |
3178 | permerror (loc, |
3179 | "left operand of shift expression %q+E is negative", | |
3180 | build2_loc (loc, code, type, lhs, rhs)); | |
3181 | return (!flag_permissive || ctx->quiet); | |
253a921b MP |
3182 | } |
3183 | /* For signed x << y the following: | |
3184 | (unsigned) x >> ((prec (lhs) - 1) - y) | |
3185 | if > 1, is undefined. The right-hand side of this formula | |
3186 | is the highest bit of the LHS that can be set (starting from 0), | |
3187 | so that the shift doesn't overflow. We then right-shift the LHS | |
3188 | to see whether any other bit is set making the original shift | |
3189 | undefined -- the result is not representable in the corresponding | |
3190 | unsigned type. */ | |
3191 | tree t = build_int_cst (unsigned_type_node, uprec - 1); | |
3192 | t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs); | |
3193 | tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs); | |
3194 | t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t); | |
3195 | if (tree_int_cst_lt (integer_one_node, t)) | |
3196 | { | |
3197 | if (!ctx->quiet) | |
5342156c MP |
3198 | permerror (loc, "shift expression %q+E overflows", |
3199 | build2_loc (loc, code, type, lhs, rhs)); | |
3200 | return (!flag_permissive || ctx->quiet); | |
253a921b MP |
3201 | } |
3202 | } | |
3203 | return false; | |
3204 | } | |
3205 | ||
2d76680f PC |
3206 | /* Subroutine of cxx_eval_constant_expression. |
3207 | Attempt to reduce the unary expression tree T to a compile time value. | |
3208 | If successful, return the value. Otherwise issue a diagnostic | |
3209 | and return error_mark_node. */ | |
3210 | ||
3211 | static tree | |
3e605b20 | 3212 | cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t, |
12d9ce19 | 3213 | bool /*lval*/, |
2d76680f PC |
3214 | bool *non_constant_p, bool *overflow_p) |
3215 | { | |
3216 | tree r; | |
3217 | tree orig_arg = TREE_OPERAND (t, 0); | |
12d9ce19 JM |
3218 | tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false, |
3219 | non_constant_p, overflow_p); | |
2d76680f | 3220 | VERIFY_CONSTANT (arg); |
f899317e JM |
3221 | location_t loc = EXPR_LOCATION (t); |
3222 | enum tree_code code = TREE_CODE (t); | |
3223 | tree type = TREE_TYPE (t); | |
3224 | r = fold_unary_loc (loc, code, type, arg); | |
3225 | if (r == NULL_TREE) | |
3226 | { | |
3227 | if (arg == orig_arg) | |
3228 | r = t; | |
3229 | else | |
3230 | r = build1_loc (loc, code, type, arg); | |
3231 | } | |
2d76680f PC |
3232 | VERIFY_CONSTANT (r); |
3233 | return r; | |
3234 | } | |
3235 | ||
ea8661cd JJ |
3236 | /* Helper function for cxx_eval_binary_expression. Try to optimize |
3237 | original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the | |
3238 | generic folding should be used. */ | |
3239 | ||
3240 | static tree | |
3241 | cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t, | |
3242 | tree lhs, tree rhs, bool *non_constant_p, | |
3243 | bool *overflow_p) | |
3244 | { | |
3245 | STRIP_NOPS (lhs); | |
3246 | if (TREE_CODE (lhs) != ADDR_EXPR) | |
3247 | return NULL_TREE; | |
3248 | ||
3249 | lhs = TREE_OPERAND (lhs, 0); | |
3250 | ||
3251 | /* &A[i] p+ j => &A[i + j] */ | |
3252 | if (TREE_CODE (lhs) == ARRAY_REF | |
3253 | && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST | |
3254 | && TREE_CODE (rhs) == INTEGER_CST | |
3255 | && TYPE_SIZE_UNIT (TREE_TYPE (lhs)) | |
3256 | && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST) | |
3257 | { | |
3258 | tree orig_type = TREE_TYPE (t); | |
3259 | location_t loc = EXPR_LOCATION (t); | |
3260 | tree type = TREE_TYPE (lhs); | |
3261 | ||
3262 | t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1)); | |
3263 | tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0))); | |
3264 | nelts = cxx_eval_constant_expression (ctx, nelts, false, non_constant_p, | |
3265 | overflow_p); | |
3266 | if (*non_constant_p) | |
3267 | return NULL_TREE; | |
3268 | /* Don't fold an out-of-bound access. */ | |
3269 | if (!tree_int_cst_le (t, nelts)) | |
3270 | return NULL_TREE; | |
3271 | rhs = cp_fold_convert (ssizetype, rhs); | |
3272 | /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT. | |
3273 | constexpr int A[1]; ... (char *)&A[0] + 1 */ | |
3274 | if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype, | |
3275 | rhs, TYPE_SIZE_UNIT (type)))) | |
3276 | return NULL_TREE; | |
3277 | /* Make sure to treat the second operand of POINTER_PLUS_EXPR | |
3278 | as signed. */ | |
3279 | rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs, | |
3280 | TYPE_SIZE_UNIT (type)); | |
3281 | t = size_binop_loc (loc, PLUS_EXPR, rhs, t); | |
3282 | t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0), | |
3283 | t, NULL_TREE, NULL_TREE); | |
3284 | t = cp_build_addr_expr (t, tf_warning_or_error); | |
3285 | t = cp_fold_convert (orig_type, t); | |
3286 | return cxx_eval_constant_expression (ctx, t, /*lval*/false, | |
3287 | non_constant_p, overflow_p); | |
3288 | } | |
3289 | ||
3290 | return NULL_TREE; | |
3291 | } | |
3292 | ||
51d464b6 JJ |
3293 | /* Try to fold expressions like |
3294 | (struct S *) (&a[0].D.2378 + 12) | |
3295 | into | |
3296 | &MEM <struct T> [(void *)&a + 12B] | |
3297 | This is something normally done by gimple_fold_stmt_to_constant_1 | |
3298 | on GIMPLE, but is undesirable on GENERIC if we are e.g. going to | |
3299 | dereference the address because some details are lost. | |
3300 | For pointer comparisons we want such folding though so that | |
3301 | match.pd address_compare optimization works. */ | |
3302 | ||
3303 | static tree | |
3304 | cxx_maybe_fold_addr_pointer_plus (tree t) | |
3305 | { | |
3306 | while (CONVERT_EXPR_P (t) | |
3307 | && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0)))) | |
3308 | t = TREE_OPERAND (t, 0); | |
3309 | if (TREE_CODE (t) != POINTER_PLUS_EXPR) | |
3310 | return NULL_TREE; | |
3311 | tree op0 = TREE_OPERAND (t, 0); | |
3312 | tree op1 = TREE_OPERAND (t, 1); | |
3313 | if (TREE_CODE (op1) != INTEGER_CST) | |
3314 | return NULL_TREE; | |
3315 | while (CONVERT_EXPR_P (op0) | |
3316 | && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))) | |
3317 | op0 = TREE_OPERAND (op0, 0); | |
3318 | if (TREE_CODE (op0) != ADDR_EXPR) | |
3319 | return NULL_TREE; | |
3320 | op1 = fold_convert (ptr_type_node, op1); | |
3321 | tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1); | |
3322 | return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r); | |
3323 | } | |
3324 | ||
2d76680f PC |
3325 | /* Subroutine of cxx_eval_constant_expression. |
3326 | Like cxx_eval_unary_expression, except for binary expressions. */ | |
3327 | ||
3328 | static tree | |
3e605b20 | 3329 | cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t, |
dfffecb8 | 3330 | bool lval, |
2d76680f PC |
3331 | bool *non_constant_p, bool *overflow_p) |
3332 | { | |
618d6c1c | 3333 | tree r = NULL_TREE; |
2d76680f PC |
3334 | tree orig_lhs = TREE_OPERAND (t, 0); |
3335 | tree orig_rhs = TREE_OPERAND (t, 1); | |
3336 | tree lhs, rhs; | |
12d9ce19 | 3337 | lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false, |
5a804683 | 3338 | non_constant_p, overflow_p); |
c8a66fc9 JM |
3339 | /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer |
3340 | subtraction. */ | |
3341 | if (*non_constant_p) | |
3342 | return t; | |
12d9ce19 | 3343 | rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false, |
5a804683 | 3344 | non_constant_p, overflow_p); |
c8a66fc9 JM |
3345 | if (*non_constant_p) |
3346 | return t; | |
f899317e JM |
3347 | |
3348 | location_t loc = EXPR_LOCATION (t); | |
3349 | enum tree_code code = TREE_CODE (t); | |
3350 | tree type = TREE_TYPE (t); | |
618d6c1c PP |
3351 | |
3352 | if (code == EQ_EXPR || code == NE_EXPR) | |
3353 | { | |
3354 | bool is_code_eq = (code == EQ_EXPR); | |
3355 | ||
3356 | if (TREE_CODE (lhs) == PTRMEM_CST | |
3357 | && TREE_CODE (rhs) == PTRMEM_CST) | |
9b0607de JM |
3358 | { |
3359 | tree lmem = PTRMEM_CST_MEMBER (lhs); | |
3360 | tree rmem = PTRMEM_CST_MEMBER (rhs); | |
3361 | bool eq; | |
3362 | if (TREE_CODE (lmem) == TREE_CODE (rmem) | |
3363 | && TREE_CODE (lmem) == FIELD_DECL | |
3364 | && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE | |
3365 | && same_type_p (DECL_CONTEXT (lmem), | |
3366 | DECL_CONTEXT (rmem))) | |
3367 | /* If both refer to (possibly different) members of the same union | |
3368 | (12.3), they compare equal. */ | |
3369 | eq = true; | |
3370 | else | |
3371 | eq = cp_tree_equal (lhs, rhs); | |
3372 | r = constant_boolean_node (eq == is_code_eq, type); | |
3373 | } | |
618d6c1c PP |
3374 | else if ((TREE_CODE (lhs) == PTRMEM_CST |
3375 | || TREE_CODE (rhs) == PTRMEM_CST) | |
3376 | && (null_member_pointer_value_p (lhs) | |
3377 | || null_member_pointer_value_p (rhs))) | |
3378 | r = constant_boolean_node (!is_code_eq, type); | |
dd5dda56 JM |
3379 | else if (TREE_CODE (lhs) == PTRMEM_CST) |
3380 | lhs = cplus_expand_constant (lhs); | |
3381 | else if (TREE_CODE (rhs) == PTRMEM_CST) | |
3382 | rhs = cplus_expand_constant (rhs); | |
618d6c1c | 3383 | } |
51d464b6 JJ |
3384 | if (r == NULL_TREE |
3385 | && TREE_CODE_CLASS (code) == tcc_comparison | |
3386 | && POINTER_TYPE_P (TREE_TYPE (lhs))) | |
3387 | { | |
3388 | if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs)) | |
3389 | lhs = fold_convert (TREE_TYPE (lhs), lhso); | |
3390 | if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs)) | |
3391 | rhs = fold_convert (TREE_TYPE (rhs), rhso); | |
3392 | } | |
8bada5cd MS |
3393 | if (code == POINTER_PLUS_EXPR && !*non_constant_p |
3394 | && integer_zerop (lhs) && !integer_zerop (rhs)) | |
3395 | { | |
3396 | if (!ctx->quiet) | |
3397 | error ("arithmetic involving a null pointer in %qE", lhs); | |
bf533db8 | 3398 | *non_constant_p = true; |
8bada5cd MS |
3399 | return t; |
3400 | } | |
ea8661cd JJ |
3401 | else if (code == POINTER_PLUS_EXPR) |
3402 | r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p, | |
3403 | overflow_p); | |
b7689b96 JM |
3404 | else if (code == SPACESHIP_EXPR) |
3405 | { | |
4ed1dc12 | 3406 | r = genericize_spaceship (loc, type, lhs, rhs); |
5c64df80 JJ |
3407 | return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, |
3408 | overflow_p); | |
b7689b96 | 3409 | } |
618d6c1c PP |
3410 | |
3411 | if (r == NULL_TREE) | |
53caa472 JM |
3412 | { |
3413 | if (ctx->manifestly_const_eval | |
3414 | && (flag_constexpr_fp_except | |
3415 | || TREE_CODE (type) != REAL_TYPE)) | |
3416 | r = fold_binary_initializer_loc (loc, code, type, lhs, rhs); | |
3417 | else | |
3418 | r = fold_binary_loc (loc, code, type, lhs, rhs); | |
3419 | } | |
618d6c1c | 3420 | |
4866b2f5 JJ |
3421 | if (r == NULL_TREE |
3422 | && (code == LSHIFT_EXPR || code == RSHIFT_EXPR) | |
3423 | && TREE_CODE (lhs) == INTEGER_CST | |
3424 | && TREE_CODE (rhs) == INTEGER_CST | |
3425 | && wi::neg_p (wi::to_wide (rhs))) | |
3426 | { | |
3427 | /* For diagnostics and -fpermissive emulate previous behavior of | |
3428 | handling shifts by negative amount. */ | |
3429 | tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs); | |
3430 | if (nrhs) | |
3431 | r = fold_binary_loc (loc, | |
3432 | code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR, | |
3433 | type, lhs, nrhs); | |
3434 | } | |
3435 | ||
f899317e JM |
3436 | if (r == NULL_TREE) |
3437 | { | |
3438 | if (lhs == orig_lhs && rhs == orig_rhs) | |
3439 | r = t; | |
3440 | else | |
3441 | r = build2_loc (loc, code, type, lhs, rhs); | |
3442 | } | |
253a921b MP |
3443 | else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs)) |
3444 | *non_constant_p = true; | |
c8a66fc9 JM |
3445 | /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to |
3446 | a local array in a constexpr function. */ | |
71a93b08 | 3447 | bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs)); |
caee690e | 3448 | if (!ptr) |
134efa82 | 3449 | VERIFY_CONSTANT (r); |
2d76680f PC |
3450 | return r; |
3451 | } | |
3452 | ||
3453 | /* Subroutine of cxx_eval_constant_expression. | |
3454 | Attempt to evaluate condition expressions. Dead branches are not | |
3455 | looked into. */ | |
3456 | ||
3457 | static tree | |
3e605b20 | 3458 | cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t, |
92a596e8 | 3459 | bool lval, |
56632b27 JM |
3460 | bool *non_constant_p, bool *overflow_p, |
3461 | tree *jump_target) | |
2d76680f | 3462 | { |
3e605b20 | 3463 | tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
12d9ce19 | 3464 | /*lval*/false, |
5a804683 | 3465 | non_constant_p, overflow_p); |
2d76680f | 3466 | VERIFY_CONSTANT (val); |
117c6426 JJ |
3467 | if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t)) |
3468 | { | |
3469 | /* Evaluate the condition as if it was | |
3470 | if (__builtin_is_constant_evaluated ()), i.e. defer it if not | |
3471 | ctx->manifestly_const_eval (as sometimes we try to constant evaluate | |
3472 | without manifestly_const_eval even expressions or parts thereof which | |
3473 | will later be manifestly const_eval evaluated), otherwise fold it to | |
3474 | true. */ | |
3475 | if (ctx->manifestly_const_eval) | |
3476 | val = boolean_true_node; | |
3477 | else | |
3478 | { | |
3479 | *non_constant_p = true; | |
3480 | return t; | |
3481 | } | |
3482 | } | |
2d76680f PC |
3483 | /* Don't VERIFY_CONSTANT the other operands. */ |
3484 | if (integer_zerop (val)) | |
43574e4f JJ |
3485 | val = TREE_OPERAND (t, 2); |
3486 | else | |
3487 | val = TREE_OPERAND (t, 1); | |
3488 | if (TREE_CODE (t) == IF_STMT && !val) | |
3489 | val = void_node; | |
3490 | return cxx_eval_constant_expression (ctx, val, lval, non_constant_p, | |
3491 | overflow_p, jump_target); | |
2d76680f PC |
3492 | } |
3493 | ||
f370e36d JJ |
3494 | /* Subroutine of cxx_eval_constant_expression. |
3495 | Attempt to evaluate vector condition expressions. Unlike | |
3496 | cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal | |
3497 | ternary arithmetics operation, where all 3 arguments have to be | |
3498 | evaluated as constants and then folding computes the result from | |
3499 | them. */ | |
3500 | ||
3501 | static tree | |
3502 | cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t, | |
3503 | bool *non_constant_p, bool *overflow_p) | |
3504 | { | |
3505 | tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), | |
3506 | /*lval*/false, | |
3507 | non_constant_p, overflow_p); | |
3508 | VERIFY_CONSTANT (arg1); | |
3509 | tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), | |
3510 | /*lval*/false, | |
3511 | non_constant_p, overflow_p); | |
3512 | VERIFY_CONSTANT (arg2); | |
3513 | tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2), | |
3514 | /*lval*/false, | |
3515 | non_constant_p, overflow_p); | |
3516 | VERIFY_CONSTANT (arg3); | |
3517 | location_t loc = EXPR_LOCATION (t); | |
3518 | tree type = TREE_TYPE (t); | |
3519 | tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); | |
3520 | if (r == NULL_TREE) | |
3521 | { | |
3522 | if (arg1 == TREE_OPERAND (t, 0) | |
3523 | && arg2 == TREE_OPERAND (t, 1) | |
3524 | && arg3 == TREE_OPERAND (t, 2)) | |
3525 | r = t; | |
3526 | else | |
3527 | r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); | |
3528 | } | |
3529 | VERIFY_CONSTANT (r); | |
3530 | return r; | |
3531 | } | |
3532 | ||
ceaaf873 JM |
3533 | /* Returns less than, equal to, or greater than zero if KEY is found to be |
3534 | less than, to match, or to be greater than the constructor_elt's INDEX. */ | |
3535 | ||
3536 | static int | |
3537 | array_index_cmp (tree key, tree index) | |
3538 | { | |
3539 | gcc_assert (TREE_CODE (key) == INTEGER_CST); | |
3540 | ||
3541 | switch (TREE_CODE (index)) | |
3542 | { | |
3543 | case INTEGER_CST: | |
3544 | return tree_int_cst_compare (key, index); | |
3545 | case RANGE_EXPR: | |
3546 | { | |
3547 | tree lo = TREE_OPERAND (index, 0); | |
3548 | tree hi = TREE_OPERAND (index, 1); | |
3549 | if (tree_int_cst_lt (key, lo)) | |
3550 | return -1; | |
3551 | else if (tree_int_cst_lt (hi, key)) | |
3552 | return 1; | |
3553 | else | |
3554 | return 0; | |
3555 | } | |
3556 | default: | |
3557 | gcc_unreachable (); | |
3558 | } | |
3559 | } | |
3560 | ||
3561 | /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1 | |
3562 | if none. If INSERT is true, insert a matching element rather than fail. */ | |
3563 | ||
3564 | static HOST_WIDE_INT | |
582d2481 | 3565 | find_array_ctor_elt (tree ary, tree dindex, bool insert) |
ceaaf873 JM |
3566 | { |
3567 | if (tree_int_cst_sgn (dindex) < 0) | |
3568 | return -1; | |
3569 | ||
3570 | unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex); | |
3571 | vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary); | |
3572 | unsigned HOST_WIDE_INT len = vec_safe_length (elts); | |
3573 | ||
3574 | unsigned HOST_WIDE_INT end = len; | |
3575 | unsigned HOST_WIDE_INT begin = 0; | |
3576 | ||
3577 | /* If the last element of the CONSTRUCTOR has its own index, we can assume | |
3578 | that the same is true of the other elements and index directly. */ | |
3579 | if (end > 0) | |
3580 | { | |
fe217ba0 | 3581 | tree cindex = (*elts)[end - 1].index; |
c7c09af8 JJ |
3582 | if (cindex == NULL_TREE) |
3583 | { | |
3584 | /* Verify that if the last index is missing, all indexes | |
3585 | are missing. */ | |
3586 | if (flag_checking) | |
3587 | for (unsigned int j = 0; j < len - 1; ++j) | |
3588 | gcc_assert ((*elts)[j].index == NULL_TREE); | |
3589 | if (i < end) | |
3590 | return i; | |
3591 | else | |
3592 | { | |
3593 | begin = end; | |
3594 | if (i == end) | |
3595 | /* If the element is to be added right at the end, | |
3596 | make sure it is added with cleared index too. */ | |
3597 | dindex = NULL_TREE; | |
3598 | else if (insert) | |
3599 | /* Otherwise, in order not to break the assumption | |
3600 | that CONSTRUCTOR either has all indexes or none, | |
3601 | we need to add indexes to all elements. */ | |
3602 | for (unsigned int j = 0; j < len; ++j) | |
3603 | (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j); | |
3604 | } | |
3605 | } | |
3606 | else if (TREE_CODE (cindex) == INTEGER_CST | |
3607 | && compare_tree_int (cindex, end - 1) == 0) | |
ceaaf873 JM |
3608 | { |
3609 | if (i < end) | |
3610 | return i; | |
3611 | else | |
3612 | begin = end; | |
3613 | } | |
3614 | } | |
3615 | ||
3616 | /* Otherwise, find a matching index by means of a binary search. */ | |
3617 | while (begin != end) | |
3618 | { | |
3619 | unsigned HOST_WIDE_INT middle = (begin + end) / 2; | |
a7ccb9e7 JM |
3620 | constructor_elt &elt = (*elts)[middle]; |
3621 | tree idx = elt.index; | |
ceaaf873 | 3622 | |
a7ccb9e7 | 3623 | int cmp = array_index_cmp (dindex, idx); |
ceaaf873 JM |
3624 | if (cmp < 0) |
3625 | end = middle; | |
3626 | else if (cmp > 0) | |
3627 | begin = middle + 1; | |
3628 | else | |
a7ccb9e7 JM |
3629 | { |
3630 | if (insert && TREE_CODE (idx) == RANGE_EXPR) | |
3631 | { | |
3632 | /* We need to split the range. */ | |
3633 | constructor_elt e; | |
3634 | tree lo = TREE_OPERAND (idx, 0); | |
3635 | tree hi = TREE_OPERAND (idx, 1); | |
fe217ba0 JJ |
3636 | tree value = elt.value; |
3637 | dindex = fold_convert (sizetype, dindex); | |
a7ccb9e7 JM |
3638 | if (tree_int_cst_lt (lo, dindex)) |
3639 | { | |
3640 | /* There are still some lower elts; shorten the range. */ | |
3641 | tree new_hi = int_const_binop (MINUS_EXPR, dindex, | |
3642 | size_one_node); | |
3643 | if (tree_int_cst_equal (lo, new_hi)) | |
3644 | /* Only one element left, no longer a range. */ | |
3645 | elt.index = lo; | |
3646 | else | |
3647 | TREE_OPERAND (idx, 1) = new_hi; | |
3648 | /* Append the element we want to insert. */ | |
3649 | ++middle; | |
3650 | e.index = dindex; | |
fe217ba0 | 3651 | e.value = unshare_constructor (value); |
a7ccb9e7 JM |
3652 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e); |
3653 | } | |
3654 | else | |
3655 | /* No lower elts, the range elt is now ours. */ | |
3656 | elt.index = dindex; | |
3657 | ||
3658 | if (tree_int_cst_lt (dindex, hi)) | |
3659 | { | |
3660 | /* There are still some higher elts; append a range. */ | |
3661 | tree new_lo = int_const_binop (PLUS_EXPR, dindex, | |
3662 | size_one_node); | |
3663 | if (tree_int_cst_equal (new_lo, hi)) | |
3664 | e.index = hi; | |
3665 | else | |
3666 | e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi); | |
fe217ba0 JJ |
3667 | e.value = unshare_constructor (value); |
3668 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e); | |
a7ccb9e7 JM |
3669 | } |
3670 | } | |
3671 | return middle; | |
3672 | } | |
ceaaf873 JM |
3673 | } |
3674 | ||
3675 | if (insert) | |
3676 | { | |
3677 | constructor_elt e = { dindex, NULL_TREE }; | |
3678 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e); | |
3679 | return end; | |
3680 | } | |
3681 | ||
3682 | return -1; | |
3683 | } | |
3684 | ||
37244b21 PP |
3685 | /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no |
3686 | matching constructor_elt exists, then add one to CTOR. | |
3687 | ||
3688 | As an optimization, if POS_HINT is non-negative then it is used as a guess | |
3689 | for the (integer) index of the matching constructor_elt within CTOR. */ | |
3690 | ||
3691 | static constructor_elt * | |
077dd9b3 | 3692 | get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1) |
37244b21 PP |
3693 | { |
3694 | /* Check the hint first. */ | |
3695 | if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor) | |
3696 | && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index) | |
3697 | return CONSTRUCTOR_ELT (ctor, pos_hint); | |
3698 | ||
3699 | tree type = TREE_TYPE (ctor); | |
3700 | if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE) | |
3701 | { | |
3702 | CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE); | |
3703 | return &CONSTRUCTOR_ELTS (ctor)->last(); | |
3704 | } | |
3705 | else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE) | |
3706 | { | |
e069285c PP |
3707 | if (TREE_CODE (index) == RANGE_EXPR) |
3708 | { | |
3709 | /* Support for RANGE_EXPR index lookups is currently limited to | |
3710 | accessing an existing element via POS_HINT, or appending a new | |
3711 | element to the end of CTOR. ??? Support for other access | |
3712 | patterns may also be needed. */ | |
3713 | vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor); | |
3714 | if (vec_safe_length (elts)) | |
3715 | { | |
3716 | tree lo = TREE_OPERAND (index, 0); | |
3717 | gcc_assert (array_index_cmp (elts->last().index, lo) < 0); | |
3718 | } | |
3719 | CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE); | |
3720 | return &elts->last(); | |
3721 | } | |
3722 | ||
37244b21 PP |
3723 | HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, /*insert*/true); |
3724 | gcc_assert (i >= 0); | |
3725 | constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i); | |
3726 | gcc_assert (cep->index == NULL_TREE | |
3727 | || TREE_CODE (cep->index) != RANGE_EXPR); | |
3728 | return cep; | |
3729 | } | |
3730 | else | |
3731 | { | |
94ff4c9d JM |
3732 | gcc_assert (TREE_CODE (index) == FIELD_DECL |
3733 | && (same_type_ignoring_top_level_qualifiers_p | |
3734 | (DECL_CONTEXT (index), TREE_TYPE (ctor)))); | |
37244b21 PP |
3735 | |
3736 | /* We must keep the CONSTRUCTOR's ELTS in FIELD order. | |
3737 | Usually we meet initializers in that order, but it is | |
3738 | possible for base types to be placed not in program | |
3739 | order. */ | |
3740 | tree fields = TYPE_FIELDS (DECL_CONTEXT (index)); | |
3741 | unsigned HOST_WIDE_INT idx = 0; | |
3742 | constructor_elt *cep = NULL; | |
3743 | ||
3744 | /* Check if we're changing the active member of a union. */ | |
3745 | if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor) | |
3746 | && CONSTRUCTOR_ELT (ctor, 0)->index != index) | |
3747 | vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0); | |
3748 | /* If the bit offset of INDEX is larger than that of the last | |
3749 | constructor_elt, then we can just immediately append a new | |
3750 | constructor_elt to the end of CTOR. */ | |
3751 | else if (CONSTRUCTOR_NELTS (ctor) | |
3752 | && tree_int_cst_compare (bit_position (index), | |
3753 | bit_position (CONSTRUCTOR_ELTS (ctor) | |
3754 | ->last().index)) > 0) | |
3755 | { | |
3756 | idx = CONSTRUCTOR_NELTS (ctor); | |
3757 | goto insert; | |
3758 | } | |
3759 | ||
3760 | /* Otherwise, we need to iterate over CTOR to find or insert INDEX | |
3761 | appropriately. */ | |
3762 | ||
3763 | for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep); | |
3764 | idx++, fields = DECL_CHAIN (fields)) | |
3765 | { | |
3766 | if (index == cep->index) | |
3767 | goto found; | |
3768 | ||
3769 | /* The field we're initializing must be on the field | |
3770 | list. Look to see if it is present before the | |
3771 | field the current ELT initializes. */ | |
3772 | for (; fields != cep->index; fields = DECL_CHAIN (fields)) | |
3773 | if (index == fields) | |
3774 | goto insert; | |
3775 | } | |
3776 | /* We fell off the end of the CONSTRUCTOR, so insert a new | |
3777 | entry at the end. */ | |
3778 | ||
3779 | insert: | |
3780 | { | |
3781 | constructor_elt ce = { index, NULL_TREE }; | |
3782 | ||
3783 | vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce); | |
3784 | cep = CONSTRUCTOR_ELT (ctor, idx); | |
3785 | } | |
3786 | found:; | |
3787 | ||
3788 | return cep; | |
3789 | } | |
3790 | } | |
3791 | ||
abdc16c8 MS |
3792 | /* Under the control of CTX, issue a detailed diagnostic for |
3793 | an out-of-bounds subscript INDEX into the expression ARRAY. */ | |
3794 | ||
3795 | static void | |
043666e0 | 3796 | diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index) |
abdc16c8 MS |
3797 | { |
3798 | if (!ctx->quiet) | |
3799 | { | |
3800 | tree arraytype = TREE_TYPE (array); | |
3801 | ||
3802 | /* Convert the unsigned array subscript to a signed integer to avoid | |
3803 | printing huge numbers for small negative values. */ | |
3804 | tree sidx = fold_convert (ssizetype, index); | |
043666e0 | 3805 | STRIP_ANY_LOCATION_WRAPPER (array); |
abdc16c8 MS |
3806 | if (DECL_P (array)) |
3807 | { | |
08b3748c | 3808 | if (TYPE_DOMAIN (arraytype)) |
043666e0 JM |
3809 | error_at (loc, "array subscript value %qE is outside the bounds " |
3810 | "of array %qD of type %qT", sidx, array, arraytype); | |
08b3748c | 3811 | else |
043666e0 JM |
3812 | error_at (loc, "nonzero array subscript %qE is used with array %qD of " |
3813 | "type %qT with unknown bounds", sidx, array, arraytype); | |
abdc16c8 MS |
3814 | inform (DECL_SOURCE_LOCATION (array), "declared here"); |
3815 | } | |
08b3748c | 3816 | else if (TYPE_DOMAIN (arraytype)) |
043666e0 JM |
3817 | error_at (loc, "array subscript value %qE is outside the bounds " |
3818 | "of array type %qT", sidx, arraytype); | |
08b3748c | 3819 | else |
043666e0 JM |
3820 | error_at (loc, "nonzero array subscript %qE is used with array of type %qT " |
3821 | "with unknown bounds", sidx, arraytype); | |
abdc16c8 MS |
3822 | } |
3823 | } | |
ceaaf873 | 3824 | |
74f8705e MP |
3825 | /* Return the number of elements for TYPE (which is an ARRAY_TYPE or |
3826 | a VECTOR_TYPE). */ | |
3827 | ||
3828 | static tree | |
3829 | get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type, | |
3830 | bool *non_constant_p, bool *overflow_p) | |
3831 | { | |
3832 | tree nelts; | |
3833 | if (TREE_CODE (type) == ARRAY_TYPE) | |
3834 | { | |
3835 | if (TYPE_DOMAIN (type)) | |
3836 | nelts = array_type_nelts_top (type); | |
3837 | else | |
3838 | nelts = size_zero_node; | |
3839 | } | |
3840 | else if (VECTOR_TYPE_P (type)) | |
3841 | nelts = size_int (TYPE_VECTOR_SUBPARTS (type)); | |
3842 | else | |
3843 | gcc_unreachable (); | |
3844 | ||
3845 | /* For VLAs, the number of elements won't be an integer constant. */ | |
3846 | nelts = cxx_eval_constant_expression (ctx, nelts, false, | |
3847 | non_constant_p, overflow_p); | |
3848 | return nelts; | |
3849 | } | |
3850 | ||
d6b46fca NS |
3851 | /* Extract element INDEX consisting of CHARS_PER_ELT chars from |
3852 | STRING_CST STRING. */ | |
3853 | ||
3854 | static tree | |
3855 | extract_string_elt (tree string, unsigned chars_per_elt, unsigned index) | |
3856 | { | |
3857 | tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string))); | |
3858 | tree r; | |
3859 | ||
3860 | if (chars_per_elt == 1) | |
3861 | r = build_int_cst (type, TREE_STRING_POINTER (string)[index]); | |
3862 | else | |
3863 | { | |
3864 | const unsigned char *ptr | |
3865 | = ((const unsigned char *)TREE_STRING_POINTER (string) | |
3866 | + index * chars_per_elt); | |
3867 | r = native_interpret_expr (type, ptr, chars_per_elt); | |
3868 | } | |
3869 | return r; | |
3870 | } | |
3871 | ||
043666e0 JM |
3872 | /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the |
3873 | subscript, diagnose any problems with it, and return the result. */ | |
3874 | ||
3875 | static tree | |
3876 | eval_and_check_array_index (const constexpr_ctx *ctx, | |
3877 | tree t, bool allow_one_past, | |
3878 | bool *non_constant_p, bool *overflow_p) | |
3879 | { | |
f9d0ca40 | 3880 | location_t loc = cp_expr_loc_or_input_loc (t); |
043666e0 JM |
3881 | tree ary = TREE_OPERAND (t, 0); |
3882 | t = TREE_OPERAND (t, 1); | |
3883 | tree index = cxx_eval_constant_expression (ctx, t, false, | |
3884 | non_constant_p, overflow_p); | |
3885 | VERIFY_CONSTANT (index); | |
3886 | ||
3887 | if (!tree_fits_shwi_p (index) | |
3888 | || tree_int_cst_sgn (index) < 0) | |
3889 | { | |
3890 | diag_array_subscript (loc, ctx, ary, index); | |
3891 | *non_constant_p = true; | |
3892 | return t; | |
3893 | } | |
3894 | ||
3895 | tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p, | |
3896 | overflow_p); | |
3897 | VERIFY_CONSTANT (nelts); | |
3898 | if (allow_one_past | |
3899 | ? !tree_int_cst_le (index, nelts) | |
3900 | : !tree_int_cst_lt (index, nelts)) | |
3901 | { | |
3902 | diag_array_subscript (loc, ctx, ary, index); | |
3903 | *non_constant_p = true; | |
3904 | return t; | |
3905 | } | |
3906 | ||
3907 | return index; | |
3908 | } | |
3909 | ||
2d76680f PC |
3910 | /* Subroutine of cxx_eval_constant_expression. |
3911 | Attempt to reduce a reference to an array slot. */ | |
3912 | ||
3913 | static tree | |
3e605b20 | 3914 | cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, |
92a596e8 | 3915 | bool lval, |
2d76680f PC |
3916 | bool *non_constant_p, bool *overflow_p) |
3917 | { | |
3918 | tree oldary = TREE_OPERAND (t, 0); | |
3e605b20 | 3919 | tree ary = cxx_eval_constant_expression (ctx, oldary, |
92a596e8 | 3920 | lval, |
5a804683 | 3921 | non_constant_p, overflow_p); |
2d76680f PC |
3922 | if (*non_constant_p) |
3923 | return t; | |
bc2687dd JJ |
3924 | if (!lval |
3925 | && TREE_CODE (ary) == VIEW_CONVERT_EXPR | |
043666e0 JM |
3926 | && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0))) |
3927 | && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0)))) | |
3928 | ary = TREE_OPERAND (ary, 0); | |
3b9997bb | 3929 | |
043666e0 JM |
3930 | tree oldidx = TREE_OPERAND (t, 1); |
3931 | tree index = eval_and_check_array_index (ctx, t, lval, | |
3932 | non_constant_p, overflow_p); | |
3933 | if (*non_constant_p) | |
3934 | return t; | |
5453bfed | 3935 | |
bc2a38df JJ |
3936 | if (lval && ary == oldary && index == oldidx) |
3937 | return t; | |
3938 | else if (lval) | |
3939 | return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL); | |
3940 | ||
043666e0 JM |
3941 | unsigned len = 0, elem_nchars = 1; |
3942 | tree elem_type = TREE_TYPE (TREE_TYPE (ary)); | |
3943 | if (TREE_CODE (ary) == CONSTRUCTOR) | |
3944 | len = CONSTRUCTOR_NELTS (ary); | |
3945 | else if (TREE_CODE (ary) == STRING_CST) | |
3946 | { | |
3947 | elem_nchars = (TYPE_PRECISION (elem_type) | |
3948 | / TYPE_PRECISION (char_type_node)); | |
3949 | len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars; | |
3950 | } | |
3951 | else if (TREE_CODE (ary) == VECTOR_CST) | |
3952 | /* We don't create variable-length VECTOR_CSTs. */ | |
3953 | len = VECTOR_CST_NELTS (ary).to_constant (); | |
3954 | else | |
3955 | { | |
3956 | /* We can't do anything with other tree codes, so use | |
3957 | VERIFY_CONSTANT to complain and fail. */ | |
3958 | VERIFY_CONSTANT (ary); | |
3959 | gcc_unreachable (); | |
3960 | } | |
3961 | ||
ceaaf873 | 3962 | bool found; |
043666e0 | 3963 | HOST_WIDE_INT i = 0; |
ceaaf873 JM |
3964 | if (TREE_CODE (ary) == CONSTRUCTOR) |
3965 | { | |
3966 | HOST_WIDE_INT ix = find_array_ctor_elt (ary, index); | |
3967 | found = (ix >= 0); | |
3968 | if (found) | |
3969 | i = ix; | |
3b9997bb | 3970 | } |
ceaaf873 | 3971 | else |
043666e0 JM |
3972 | { |
3973 | i = tree_to_shwi (index); | |
3974 | found = (i < len); | |
3975 | } | |
3b9997bb | 3976 | |
fd2bfee5 JM |
3977 | if (found) |
3978 | { | |
3979 | tree r; | |
3980 | if (TREE_CODE (ary) == CONSTRUCTOR) | |
3981 | r = (*CONSTRUCTOR_ELTS (ary))[i].value; | |
3982 | else if (TREE_CODE (ary) == VECTOR_CST) | |
3983 | r = VECTOR_CST_ELT (ary, i); | |
fd2bfee5 | 3984 | else |
d6b46fca NS |
3985 | r = extract_string_elt (ary, elem_nchars, i); |
3986 | ||
fd2bfee5 JM |
3987 | if (r) |
3988 | /* Don't VERIFY_CONSTANT here. */ | |
3989 | return r; | |
2d76680f | 3990 | |
fd2bfee5 | 3991 | /* Otherwise the element doesn't have a value yet. */ |
2d76680f | 3992 | } |
3b9997bb | 3993 | |
fd2bfee5 JM |
3994 | /* Not found. */ |
3995 | ||
3996 | if (TREE_CODE (ary) == CONSTRUCTOR | |
e8c48716 | 3997 | && CONSTRUCTOR_NO_CLEARING (ary)) |
2d76680f | 3998 | { |
fd2bfee5 JM |
3999 | /* 'ary' is part of the aggregate initializer we're currently |
4000 | building; if there's no initializer for this element yet, | |
4001 | that's an error. */ | |
4002 | if (!ctx->quiet) | |
4003 | error ("accessing uninitialized array element"); | |
4004 | *non_constant_p = true; | |
4005 | return t; | |
2d76680f | 4006 | } |
fd2bfee5 JM |
4007 | |
4008 | /* If it's within the array bounds but doesn't have an explicit | |
0712ea63 JM |
4009 | initializer, it's initialized from {}. But use build_value_init |
4010 | directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */ | |
4011 | tree val; | |
0df73bee | 4012 | constexpr_ctx new_ctx; |
a688c284 PP |
4013 | if (is_really_empty_class (elem_type, /*ignore_vptr*/false)) |
4014 | return build_constructor (elem_type, NULL); | |
4015 | else if (CP_AGGREGATE_TYPE_P (elem_type)) | |
0712ea63 JM |
4016 | { |
4017 | tree empty_ctor = build_constructor (init_list_type_node, NULL); | |
4018 | val = digest_init (elem_type, empty_ctor, tf_warning_or_error); | |
a42f8120 MP |
4019 | } |
4020 | else | |
4021 | val = build_value_init (elem_type, tf_warning_or_error); | |
4022 | ||
4023 | if (!SCALAR_TYPE_P (elem_type)) | |
4024 | { | |
0df73bee | 4025 | new_ctx = *ctx; |
a42f8120 MP |
4026 | if (ctx->object) |
4027 | /* If there was no object, don't add one: it could confuse us | |
4028 | into thinking we're modifying a const object. */ | |
4029 | new_ctx.object = t; | |
0df73bee MP |
4030 | new_ctx.ctor = build_constructor (elem_type, NULL); |
4031 | ctx = &new_ctx; | |
0712ea63 | 4032 | } |
0df73bee MP |
4033 | t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p, |
4034 | overflow_p); | |
a42f8120 | 4035 | if (!SCALAR_TYPE_P (elem_type) && t != ctx->ctor) |
0df73bee MP |
4036 | free_constructor (ctx->ctor); |
4037 | return t; | |
2d76680f PC |
4038 | } |
4039 | ||
4040 | /* Subroutine of cxx_eval_constant_expression. | |
4041 | Attempt to reduce a field access of a value of class type. */ | |
4042 | ||
4043 | static tree | |
3e605b20 | 4044 | cxx_eval_component_reference (const constexpr_ctx *ctx, tree t, |
92a596e8 | 4045 | bool lval, |
2d76680f PC |
4046 | bool *non_constant_p, bool *overflow_p) |
4047 | { | |
4048 | unsigned HOST_WIDE_INT i; | |
4049 | tree field; | |
4050 | tree value; | |
4051 | tree part = TREE_OPERAND (t, 1); | |
4052 | tree orig_whole = TREE_OPERAND (t, 0); | |
3e605b20 | 4053 | tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
92a596e8 | 4054 | lval, |
5a804683 | 4055 | non_constant_p, overflow_p); |
a7f8415c | 4056 | if (INDIRECT_REF_P (whole) |
bf533db8 JJ |
4057 | && integer_zerop (TREE_OPERAND (whole, 0))) |
4058 | { | |
4059 | if (!ctx->quiet) | |
4060 | error ("dereferencing a null pointer in %qE", orig_whole); | |
4061 | *non_constant_p = true; | |
4062 | return t; | |
4063 | } | |
8bada5cd | 4064 | |
dcdbc004 JM |
4065 | if (TREE_CODE (whole) == PTRMEM_CST) |
4066 | whole = cplus_expand_constant (whole); | |
2d76680f PC |
4067 | if (whole == orig_whole) |
4068 | return t; | |
92a596e8 | 4069 | if (lval) |
2d76680f PC |
4070 | return fold_build3 (COMPONENT_REF, TREE_TYPE (t), |
4071 | whole, part, NULL_TREE); | |
4072 | /* Don't VERIFY_CONSTANT here; we only want to check that we got a | |
4073 | CONSTRUCTOR. */ | |
4074 | if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR) | |
4075 | { | |
2b3ab879 | 4076 | if (!ctx->quiet) |
2d76680f PC |
4077 | error ("%qE is not a constant expression", orig_whole); |
4078 | *non_constant_p = true; | |
4079 | } | |
4080 | if (DECL_MUTABLE_P (part)) | |
4081 | { | |
2b3ab879 | 4082 | if (!ctx->quiet) |
2d76680f PC |
4083 | error ("mutable %qD is not usable in a constant expression", part); |
4084 | *non_constant_p = true; | |
4085 | } | |
4086 | if (*non_constant_p) | |
4087 | return t; | |
2db613e5 | 4088 | bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole)); |
2d76680f PC |
4089 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) |
4090 | { | |
2db613e5 JM |
4091 | /* Use name match for PMF fields, as a variant will have a |
4092 | different FIELD_DECL with a different type. */ | |
4093 | if (pmf ? DECL_NAME (field) == DECL_NAME (part) | |
4094 | : field == part) | |
60813a46 JM |
4095 | { |
4096 | if (value) | |
5b884e94 JJ |
4097 | { |
4098 | STRIP_ANY_LOCATION_WRAPPER (value); | |
4099 | return value; | |
4100 | } | |
60813a46 JM |
4101 | else |
4102 | /* We're in the middle of initializing it. */ | |
4103 | break; | |
4104 | } | |
2d76680f PC |
4105 | } |
4106 | if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE | |
4107 | && CONSTRUCTOR_NELTS (whole) > 0) | |
4108 | { | |
4109 | /* DR 1188 says we don't have to deal with this. */ | |
2b3ab879 | 4110 | if (!ctx->quiet) |
b599bf9d PP |
4111 | { |
4112 | constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0); | |
4113 | if (cep->value == NULL_TREE) | |
4114 | error ("accessing uninitialized member %qD", part); | |
4115 | else | |
4116 | error ("accessing %qD member instead of initialized %qD member in " | |
4117 | "constant expression", part, cep->index); | |
4118 | } | |
2d76680f PC |
4119 | *non_constant_p = true; |
4120 | return t; | |
4121 | } | |
4122 | ||
188e53bd JM |
4123 | /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty |
4124 | classes never get represented; throw together a value now. */ | |
dbcd32f8 | 4125 | if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
188e53bd JM |
4126 | return build_constructor (TREE_TYPE (t), NULL); |
4127 | ||
2db613e5 JM |
4128 | gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole))); |
4129 | ||
e8c48716 | 4130 | if (CONSTRUCTOR_NO_CLEARING (whole)) |
3e605b20 JM |
4131 | { |
4132 | /* 'whole' is part of the aggregate initializer we're currently | |
4133 | building; if there's no initializer for this member yet, that's an | |
188e53bd | 4134 | error. */ |
2b3ab879 | 4135 | if (!ctx->quiet) |
3e605b20 JM |
4136 | error ("accessing uninitialized member %qD", part); |
4137 | *non_constant_p = true; | |
4138 | return t; | |
4139 | } | |
4140 | ||
2d76680f PC |
4141 | /* If there's no explicit init for this field, it's value-initialized. */ |
4142 | value = build_value_init (TREE_TYPE (t), tf_warning_or_error); | |
3e605b20 | 4143 | return cxx_eval_constant_expression (ctx, value, |
92a596e8 | 4144 | lval, |
5a804683 | 4145 | non_constant_p, overflow_p); |
2d76680f PC |
4146 | } |
4147 | ||
4148 | /* Subroutine of cxx_eval_constant_expression. | |
4149 | Attempt to reduce a field access of a value of class type that is | |
4150 | expressed as a BIT_FIELD_REF. */ | |
4151 | ||
4152 | static tree | |
3e605b20 | 4153 | cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t, |
92a596e8 | 4154 | bool lval, |
2d76680f PC |
4155 | bool *non_constant_p, bool *overflow_p) |
4156 | { | |
4157 | tree orig_whole = TREE_OPERAND (t, 0); | |
4158 | tree retval, fldval, utype, mask; | |
4159 | bool fld_seen = false; | |
4160 | HOST_WIDE_INT istart, isize; | |
3e605b20 | 4161 | tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
92a596e8 | 4162 | lval, |
5a804683 | 4163 | non_constant_p, overflow_p); |
2d76680f PC |
4164 | tree start, field, value; |
4165 | unsigned HOST_WIDE_INT i; | |
4166 | ||
4167 | if (whole == orig_whole) | |
4168 | return t; | |
4169 | /* Don't VERIFY_CONSTANT here; we only want to check that we got a | |
4170 | CONSTRUCTOR. */ | |
4171 | if (!*non_constant_p | |
4172 | && TREE_CODE (whole) != VECTOR_CST | |
4173 | && TREE_CODE (whole) != CONSTRUCTOR) | |
4174 | { | |
2b3ab879 | 4175 | if (!ctx->quiet) |
2d76680f PC |
4176 | error ("%qE is not a constant expression", orig_whole); |
4177 | *non_constant_p = true; | |
4178 | } | |
4179 | if (*non_constant_p) | |
4180 | return t; | |
4181 | ||
4182 | if (TREE_CODE (whole) == VECTOR_CST) | |
4183 | return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole, | |
4184 | TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)); | |
4185 | ||
4186 | start = TREE_OPERAND (t, 2); | |
4187 | istart = tree_to_shwi (start); | |
4188 | isize = tree_to_shwi (TREE_OPERAND (t, 1)); | |
4189 | utype = TREE_TYPE (t); | |
4190 | if (!TYPE_UNSIGNED (utype)) | |
4191 | utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1); | |
4192 | retval = build_int_cst (utype, 0); | |
4193 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) | |
4194 | { | |
4195 | tree bitpos = bit_position (field); | |
5b884e94 | 4196 | STRIP_ANY_LOCATION_WRAPPER (value); |
2d76680f PC |
4197 | if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1)) |
4198 | return value; | |
4199 | if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE | |
4200 | && TREE_CODE (value) == INTEGER_CST | |
4201 | && tree_fits_shwi_p (bitpos) | |
4202 | && tree_fits_shwi_p (DECL_SIZE (field))) | |
4203 | { | |
4204 | HOST_WIDE_INT bit = tree_to_shwi (bitpos); | |
4205 | HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field)); | |
4206 | HOST_WIDE_INT shift; | |
4207 | if (bit >= istart && bit + sz <= istart + isize) | |
4208 | { | |
4209 | fldval = fold_convert (utype, value); | |
4210 | mask = build_int_cst_type (utype, -1); | |
4211 | mask = fold_build2 (LSHIFT_EXPR, utype, mask, | |
4212 | size_int (TYPE_PRECISION (utype) - sz)); | |
4213 | mask = fold_build2 (RSHIFT_EXPR, utype, mask, | |
4214 | size_int (TYPE_PRECISION (utype) - sz)); | |
4215 | fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask); | |
4216 | shift = bit - istart; | |
4217 | if (BYTES_BIG_ENDIAN) | |
4218 | shift = TYPE_PRECISION (utype) - shift - sz; | |
4219 | fldval = fold_build2 (LSHIFT_EXPR, utype, fldval, | |
4220 | size_int (shift)); | |
4221 | retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval); | |
4222 | fld_seen = true; | |
4223 | } | |
4224 | } | |
4225 | } | |
4226 | if (fld_seen) | |
4227 | return fold_convert (TREE_TYPE (t), retval); | |
4228 | gcc_unreachable (); | |
4229 | return error_mark_node; | |
4230 | } | |
4231 | ||
896048cf JJ |
4232 | /* Helper for cxx_eval_bit_cast. |
4233 | Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From | |
4234 | types and types of all subobjects have is_union_v<T>, is_pointer_v<T>, | |
4235 | is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static | |
4236 | data members of reference type. */ | |
4237 | ||
4238 | static bool | |
4239 | check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type, | |
4240 | tree orig_type) | |
4241 | { | |
4242 | if (TREE_CODE (type) == UNION_TYPE) | |
4243 | { | |
4244 | if (!ctx->quiet) | |
4245 | { | |
4246 | if (type == orig_type) | |
4247 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4248 | "a union type", "__builtin_bit_cast", type); | |
4249 | else | |
4250 | error_at (loc, "%qs is not a constant expression because %qT " | |
4251 | "contains a union type", "__builtin_bit_cast", | |
4252 | orig_type); | |
4253 | } | |
4254 | return true; | |
4255 | } | |
4256 | if (TREE_CODE (type) == POINTER_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 pointer type", "__builtin_bit_cast", type); | |
4263 | else | |
4264 | error_at (loc, "%qs is not a constant expression because %qT " | |
4265 | "contains a pointer type", "__builtin_bit_cast", | |
4266 | orig_type); | |
4267 | } | |
4268 | return true; | |
4269 | } | |
4270 | if (TREE_CODE (type) == REFERENCE_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 reference type", "__builtin_bit_cast", type); | |
4277 | else | |
4278 | error_at (loc, "%qs is not a constant expression because %qT " | |
4279 | "contains a reference type", "__builtin_bit_cast", | |
4280 | orig_type); | |
4281 | } | |
4282 | return true; | |
4283 | } | |
4284 | if (TYPE_PTRMEM_P (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 pointer to member type", "__builtin_bit_cast", | |
4291 | type); | |
4292 | else | |
4293 | error_at (loc, "%qs is not a constant expression because %qT " | |
4294 | "contains a pointer to member type", | |
4295 | "__builtin_bit_cast", orig_type); | |
4296 | } | |
4297 | return true; | |
4298 | } | |
4299 | if (TYPE_VOLATILE (type)) | |
4300 | { | |
4301 | if (!ctx->quiet) | |
4302 | { | |
4303 | if (type == orig_type) | |
4304 | error_at (loc, "%qs is not a constant expression because %qT is " | |
4305 | "volatile", "__builtin_bit_cast", type); | |
4306 | else | |
4307 | error_at (loc, "%qs is not a constant expression because %qT " | |
4308 | "contains a volatile subobject", | |
4309 | "__builtin_bit_cast", orig_type); | |
4310 | } | |
4311 | return true; | |
4312 | } | |
4313 | if (TREE_CODE (type) == RECORD_TYPE) | |
4314 | for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) | |
4315 | if (TREE_CODE (field) == FIELD_DECL | |
4316 | && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type)) | |
4317 | return true; | |
4318 | return false; | |
4319 | } | |
4320 | ||
c57c910c JJ |
4321 | /* Helper function for cxx_eval_bit_cast. For unsigned char or |
4322 | std::byte members of CONSTRUCTOR (recursively) if they contain | |
4323 | some indeterminate bits (as set in MASK), remove the ctor elts, | |
4324 | mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the | |
4325 | bits in MASK. */ | |
4326 | ||
4327 | static void | |
4328 | clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask) | |
4329 | { | |
4330 | if (TREE_CODE (t) != CONSTRUCTOR) | |
4331 | return; | |
4332 | ||
4333 | unsigned i, j = 0; | |
4334 | tree index, value; | |
4335 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value) | |
4336 | { | |
4337 | tree type = TREE_TYPE (value); | |
4338 | if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE | |
4339 | && DECL_BIT_FIELD_TYPE (index) != NULL_TREE) | |
4340 | { | |
4341 | if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index))) | |
4342 | { | |
4343 | HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index)); | |
4344 | gcc_assert (fldsz != 0); | |
4345 | HOST_WIDE_INT pos = int_byte_position (index); | |
4346 | HOST_WIDE_INT bpos | |
4347 | = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index)); | |
4348 | bpos %= BITS_PER_UNIT; | |
4349 | HOST_WIDE_INT end | |
4350 | = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT; | |
4351 | gcc_assert (end == 1 || end == 2); | |
4352 | unsigned char *p = mask + pos; | |
4353 | unsigned char mask_save[2]; | |
4354 | mask_save[0] = mask[pos]; | |
4355 | mask_save[1] = end == 2 ? mask[pos + 1] : 0; | |
4356 | if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) | |
4357 | sorry_at (loc, "PDP11 bit-field handling unsupported" | |
4358 | " in %qs", "__builtin_bit_cast"); | |
4359 | else if (BYTES_BIG_ENDIAN) | |
4360 | { | |
4361 | /* Big endian. */ | |
4362 | if (bpos + fldsz <= BITS_PER_UNIT) | |
4363 | *p &= ~(((1 << fldsz) - 1) | |
4364 | << (BITS_PER_UNIT - bpos - fldsz)); | |
4365 | else | |
4366 | { | |
4367 | gcc_assert (bpos); | |
4368 | *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos); | |
4369 | p++; | |
4370 | fldsz -= BITS_PER_UNIT - bpos; | |
4371 | gcc_assert (fldsz && fldsz < BITS_PER_UNIT); | |
4372 | *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz; | |
4373 | } | |
4374 | } | |
4375 | else | |
4376 | { | |
4377 | /* Little endian. */ | |
4378 | if (bpos + fldsz <= BITS_PER_UNIT) | |
4379 | *p &= ~(((1 << fldsz) - 1) << bpos); | |
4380 | else | |
4381 | { | |
4382 | gcc_assert (bpos); | |
4383 | *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos); | |
4384 | p++; | |
4385 | fldsz -= BITS_PER_UNIT - bpos; | |
4386 | gcc_assert (fldsz && fldsz < BITS_PER_UNIT); | |
4387 | *p &= ~((1 << fldsz) - 1); | |
4388 | } | |
4389 | } | |
4390 | if (mask_save[0] != mask[pos] | |
4391 | || (end == 2 && mask_save[1] != mask[pos + 1])) | |
4392 | { | |
4393 | CONSTRUCTOR_NO_CLEARING (t) = 1; | |
4394 | continue; | |
4395 | } | |
4396 | } | |
4397 | } | |
4398 | else if (is_byte_access_type_not_plain_char (type)) | |
4399 | { | |
4400 | HOST_WIDE_INT pos; | |
4401 | if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) | |
4402 | pos = tree_to_shwi (index); | |
4403 | else | |
4404 | pos = int_byte_position (index); | |
4405 | if (mask[pos]) | |
4406 | { | |
4407 | CONSTRUCTOR_NO_CLEARING (t) = 1; | |
4408 | mask[pos] = 0; | |
4409 | continue; | |
4410 | } | |
4411 | } | |
4412 | if (TREE_CODE (value) == CONSTRUCTOR) | |
4413 | { | |
4414 | HOST_WIDE_INT pos; | |
4415 | if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) | |
4416 | pos = tree_to_shwi (index) | |
4417 | * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)))); | |
4418 | else | |
4419 | pos = int_byte_position (index); | |
4420 | clear_uchar_or_std_byte_in_mask (loc, value, mask + pos); | |
4421 | } | |
4422 | if (i != j) | |
4423 | { | |
4424 | CONSTRUCTOR_ELT (t, j)->index = index; | |
4425 | CONSTRUCTOR_ELT (t, j)->value = value; | |
4426 | } | |
4427 | ++j; | |
4428 | } | |
4429 | if (CONSTRUCTOR_NELTS (t) != j) | |
4430 | vec_safe_truncate (CONSTRUCTOR_ELTS (t), j); | |
4431 | } | |
4432 | ||
896048cf JJ |
4433 | /* Subroutine of cxx_eval_constant_expression. |
4434 | Attempt to evaluate a BIT_CAST_EXPR. */ | |
4435 | ||
4436 | static tree | |
4437 | cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p, | |
4438 | bool *overflow_p) | |
4439 | { | |
4440 | if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t), | |
4441 | TREE_TYPE (t)) | |
4442 | || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0), | |
4443 | EXPR_LOCATION (t)), | |
4444 | TREE_TYPE (TREE_OPERAND (t, 0)), | |
4445 | TREE_TYPE (TREE_OPERAND (t, 0)))) | |
4446 | { | |
4447 | *non_constant_p = true; | |
4448 | return t; | |
4449 | } | |
4450 | ||
4451 | tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false, | |
4452 | non_constant_p, overflow_p); | |
4453 | if (*non_constant_p) | |
4454 | return t; | |
4455 | ||
4456 | location_t loc = EXPR_LOCATION (t); | |
4457 | if (BITS_PER_UNIT != 8 || CHAR_BIT != 8) | |
4458 | { | |
4459 | if (!ctx->quiet) | |
4460 | sorry_at (loc, "%qs cannot be constant evaluated on the target", | |
4461 | "__builtin_bit_cast"); | |
4462 | *non_constant_p = true; | |
4463 | return t; | |
4464 | } | |
4465 | ||
4466 | if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t)))) | |
4467 | { | |
4468 | if (!ctx->quiet) | |
4469 | sorry_at (loc, "%qs cannot be constant evaluated because the " | |
4470 | "type is too large", "__builtin_bit_cast"); | |
4471 | *non_constant_p = true; | |
4472 | return t; | |
4473 | } | |
4474 | ||
4475 | HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t))); | |
4476 | if (len < 0 || (int) len != len) | |
4477 | { | |
4478 | if (!ctx->quiet) | |
4479 | sorry_at (loc, "%qs cannot be constant evaluated because the " | |
4480 | "type is too large", "__builtin_bit_cast"); | |
4481 | *non_constant_p = true; | |
4482 | return t; | |
4483 | } | |
4484 | ||
4485 | unsigned char buf[64]; | |
4486 | unsigned char *ptr, *mask; | |
4487 | size_t alen = (size_t) len * 2; | |
4488 | if (alen <= sizeof (buf)) | |
4489 | ptr = buf; | |
4490 | else | |
4491 | ptr = XNEWVEC (unsigned char, alen); | |
4492 | mask = ptr + (size_t) len; | |
4493 | /* At the beginning consider everything indeterminate. */ | |
4494 | memset (mask, ~0, (size_t) len); | |
4495 | ||
4496 | if (native_encode_initializer (op, ptr, len, 0, mask) != len) | |
4497 | { | |
4498 | if (!ctx->quiet) | |
4499 | sorry_at (loc, "%qs cannot be constant evaluated because the " | |
4500 | "argument cannot be encoded", "__builtin_bit_cast"); | |
4501 | *non_constant_p = true; | |
4502 | if (ptr != buf) | |
4503 | XDELETE (ptr); | |
4504 | return t; | |
4505 | } | |
4506 | ||
4507 | tree r = NULL_TREE; | |
4508 | if (can_native_interpret_type_p (TREE_TYPE (t))) | |
c57c910c JJ |
4509 | { |
4510 | r = native_interpret_expr (TREE_TYPE (t), ptr, len); | |
4511 | if (is_byte_access_type_not_plain_char (TREE_TYPE (t))) | |
4512 | { | |
4513 | gcc_assert (len == 1); | |
4514 | if (mask[0]) | |
4515 | { | |
4516 | memset (mask, 0, len); | |
4517 | r = build_constructor (TREE_TYPE (r), NULL); | |
4518 | CONSTRUCTOR_NO_CLEARING (r) = 1; | |
4519 | } | |
4520 | } | |
4521 | } | |
896048cf JJ |
4522 | else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE) |
4523 | { | |
4524 | r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len); | |
4525 | if (r != NULL_TREE) | |
c57c910c JJ |
4526 | { |
4527 | clear_type_padding_in_mask (TREE_TYPE (t), mask); | |
4528 | clear_uchar_or_std_byte_in_mask (loc, r, mask); | |
4529 | } | |
896048cf JJ |
4530 | } |
4531 | ||
4532 | if (r != NULL_TREE) | |
4533 | { | |
4534 | for (int i = 0; i < len; i++) | |
4535 | if (mask[i]) | |
4536 | { | |
4537 | if (!ctx->quiet) | |
4538 | error_at (loc, "%qs accessing uninitialized byte at offset %d", | |
4539 | "__builtin_bit_cast", i); | |
4540 | *non_constant_p = true; | |
4541 | r = t; | |
4542 | break; | |
4543 | } | |
4544 | if (ptr != buf) | |
4545 | XDELETE (ptr); | |
4546 | return r; | |
4547 | } | |
4548 | ||
4549 | if (!ctx->quiet) | |
4550 | sorry_at (loc, "%qs cannot be constant evaluated because the " | |
4551 | "argument cannot be interpreted", "__builtin_bit_cast"); | |
4552 | *non_constant_p = true; | |
4553 | if (ptr != buf) | |
4554 | XDELETE (ptr); | |
4555 | return t; | |
4556 | } | |
4557 | ||
2d76680f PC |
4558 | /* Subroutine of cxx_eval_constant_expression. |
4559 | Evaluate a short-circuited logical expression T in the context | |
4560 | of a given constexpr CALL. BAILOUT_VALUE is the value for | |
4561 | early return. CONTINUE_VALUE is used here purely for | |
4562 | sanity check purposes. */ | |
4563 | ||
4564 | static tree | |
3e605b20 | 4565 | cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t, |
2d76680f | 4566 | tree bailout_value, tree continue_value, |
92a596e8 | 4567 | bool lval, |
2d76680f PC |
4568 | bool *non_constant_p, bool *overflow_p) |
4569 | { | |
4570 | tree r; | |
3e605b20 | 4571 | tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
92a596e8 | 4572 | lval, |
5a804683 | 4573 | non_constant_p, overflow_p); |
2d76680f PC |
4574 | VERIFY_CONSTANT (lhs); |
4575 | if (tree_int_cst_equal (lhs, bailout_value)) | |
4576 | return lhs; | |
4577 | gcc_assert (tree_int_cst_equal (lhs, continue_value)); | |
3e605b20 | 4578 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
92a596e8 | 4579 | lval, non_constant_p, |
5a804683 | 4580 | overflow_p); |
2d76680f PC |
4581 | VERIFY_CONSTANT (r); |
4582 | return r; | |
4583 | } | |
4584 | ||
4585 | /* REF is a COMPONENT_REF designating a particular field. V is a vector of | |
4586 | CONSTRUCTOR elements to initialize (part of) an object containing that | |
4587 | field. Return a pointer to the constructor_elt corresponding to the | |
4588 | initialization of the field. */ | |
4589 | ||
4590 | static constructor_elt * | |
4591 | base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref) | |
4592 | { | |
4593 | tree aggr = TREE_OPERAND (ref, 0); | |
4594 | tree field = TREE_OPERAND (ref, 1); | |
4595 | HOST_WIDE_INT i; | |
4596 | constructor_elt *ce; | |
4597 | ||
4598 | gcc_assert (TREE_CODE (ref) == COMPONENT_REF); | |
4599 | ||
4600 | if (TREE_CODE (aggr) == COMPONENT_REF) | |
4601 | { | |
4602 | constructor_elt *base_ce | |
4603 | = base_field_constructor_elt (v, aggr); | |
4604 | v = CONSTRUCTOR_ELTS (base_ce->value); | |
4605 | } | |
4606 | ||
4607 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
4608 | if (ce->index == field) | |
4609 | return ce; | |
4610 | ||
4611 | gcc_unreachable (); | |
4612 | return NULL; | |
4613 | } | |
4614 | ||
3e605b20 JM |
4615 | /* Some of the expressions fed to the constexpr mechanism are calls to |
4616 | constructors, which have type void. In that case, return the type being | |
4617 | initialized by the constructor. */ | |
4618 | ||
4619 | static tree | |
4620 | initialized_type (tree t) | |
4621 | { | |
4622 | if (TYPE_P (t)) | |
4623 | return t; | |
5dab8b11 | 4624 | tree type = TREE_TYPE (t); |
4d0c18c6 | 4625 | if (TREE_CODE (t) == CALL_EXPR) |
3e605b20 JM |
4626 | { |
4627 | /* A constructor call has void type, so we need to look deeper. */ | |
4628 | tree fn = get_function_named_in_call (t); | |
4629 | if (fn && TREE_CODE (fn) == FUNCTION_DECL | |
4630 | && DECL_CXX_CONSTRUCTOR_P (fn)) | |
4631 | type = DECL_CONTEXT (fn); | |
4632 | } | |
4d0c18c6 JM |
4633 | else if (TREE_CODE (t) == COMPOUND_EXPR) |
4634 | return initialized_type (TREE_OPERAND (t, 1)); | |
5dab8b11 JM |
4635 | else if (TREE_CODE (t) == AGGR_INIT_EXPR) |
4636 | type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t)); | |
4637 | return cv_unqualified (type); | |
3e605b20 JM |
4638 | } |
4639 | ||
4640 | /* We're about to initialize element INDEX of an array or class from VALUE. | |
4641 | Set up NEW_CTX appropriately by adjusting .object to refer to the | |
4642 | subobject and creating a new CONSTRUCTOR if the element is itself | |
4643 | a class or array. */ | |
4644 | ||
4645 | static void | |
4646 | init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx, | |
4647 | tree index, tree &value) | |
4648 | { | |
4649 | new_ctx = *ctx; | |
4650 | ||
4651 | if (index && TREE_CODE (index) != INTEGER_CST | |
3d423c6f PP |
4652 | && TREE_CODE (index) != FIELD_DECL |
4653 | && TREE_CODE (index) != RANGE_EXPR) | |
3e605b20 JM |
4654 | /* This won't have an element in the new CONSTRUCTOR. */ |
4655 | return; | |
4656 | ||
4657 | tree type = initialized_type (value); | |
4658 | if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type)) | |
4659 | /* A non-aggregate member doesn't get its own CONSTRUCTOR. */ | |
4660 | return; | |
4661 | ||
4662 | /* The sub-aggregate initializer might contain a placeholder; | |
4663 | update object to refer to the subobject and ctor to refer to | |
4664 | the (newly created) sub-initializer. */ | |
4665 | if (ctx->object) | |
3d423c6f PP |
4666 | { |
4667 | if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR) | |
4668 | /* There's no well-defined subobject for this index. */ | |
4669 | new_ctx.object = NULL_TREE; | |
4670 | else | |
4671 | new_ctx.object = build_ctor_subob_ref (index, type, ctx->object); | |
4672 | } | |
3e605b20 | 4673 | tree elt = build_constructor (type, NULL); |
e8c48716 | 4674 | CONSTRUCTOR_NO_CLEARING (elt) = true; |
3e605b20 JM |
4675 | new_ctx.ctor = elt; |
4676 | ||
4677 | if (TREE_CODE (value) == TARGET_EXPR) | |
4678 | /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */ | |
4679 | value = TARGET_EXPR_INITIAL (value); | |
4680 | } | |
4681 | ||
4682 | /* We're about to process an initializer for a class or array TYPE. Make | |
4683 | sure that CTX is set up appropriately. */ | |
4684 | ||
4685 | static void | |
4686 | verify_ctor_sanity (const constexpr_ctx *ctx, tree type) | |
4687 | { | |
4688 | /* We don't bother building a ctor for an empty base subobject. */ | |
4689 | if (is_empty_class (type)) | |
4690 | return; | |
4691 | ||
4692 | /* We're in the middle of an initializer that might involve placeholders; | |
4693 | our caller should have created a CONSTRUCTOR for us to put the | |
4694 | initializer into. We will either return that constructor or T. */ | |
4695 | gcc_assert (ctx->ctor); | |
4696 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
4697 | (type, TREE_TYPE (ctx->ctor))); | |
fe69277d JM |
4698 | /* We used to check that ctx->ctor was empty, but that isn't the case when |
4699 | the object is zero-initialized before calling the constructor. */ | |
3e605b20 | 4700 | if (ctx->object) |
176e79b5 JM |
4701 | { |
4702 | tree otype = TREE_TYPE (ctx->object); | |
4703 | gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype) | |
4704 | /* Handle flexible array members. */ | |
4705 | || (TREE_CODE (otype) == ARRAY_TYPE | |
4706 | && TYPE_DOMAIN (otype) == NULL_TREE | |
4707 | && TREE_CODE (type) == ARRAY_TYPE | |
4708 | && (same_type_ignoring_top_level_qualifiers_p | |
4709 | (TREE_TYPE (type), TREE_TYPE (otype))))); | |
4710 | } | |
3e605b20 | 4711 | gcc_assert (!ctx->object || !DECL_P (ctx->object) |
8e007055 | 4712 | || *(ctx->global->values.get (ctx->object)) == ctx->ctor); |
3e605b20 JM |
4713 | } |
4714 | ||
2d76680f PC |
4715 | /* Subroutine of cxx_eval_constant_expression. |
4716 | The expression tree T denotes a C-style array or a C-style | |
4717 | aggregate. Reduce it to a constant expression. */ | |
4718 | ||
4719 | static tree | |
3e605b20 | 4720 | cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t, |
92a596e8 | 4721 | bool lval, |
2d76680f PC |
4722 | bool *non_constant_p, bool *overflow_p) |
4723 | { | |
4724 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); | |
2d76680f PC |
4725 | bool changed = false; |
4726 | gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t)); | |
8a29084d | 4727 | tree type = TREE_TYPE (t); |
3e605b20 | 4728 | |
8a29084d | 4729 | constexpr_ctx new_ctx; |
d4619dc1 | 4730 | if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type)) |
8a29084d | 4731 | { |
d4619dc1 NS |
4732 | /* We don't really need the ctx->ctor business for a PMF or |
4733 | vector, but it's simpler to use the same code. */ | |
8a29084d JM |
4734 | new_ctx = *ctx; |
4735 | new_ctx.ctor = build_constructor (type, NULL); | |
4736 | new_ctx.object = NULL_TREE; | |
4737 | ctx = &new_ctx; | |
4738 | }; | |
4739 | verify_ctor_sanity (ctx, type); | |
3e605b20 JM |
4740 | vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); |
4741 | vec_alloc (*p, vec_safe_length (v)); | |
4742 | ||
49a86fce PP |
4743 | if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t)) |
4744 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1; | |
4745 | ||
2d63bc39 JM |
4746 | unsigned i; |
4747 | tree index, value; | |
4748 | bool constant_p = true; | |
4749 | bool side_effects_p = false; | |
3e605b20 | 4750 | FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value) |
2d76680f | 4751 | { |
bcb5f3c9 | 4752 | tree orig_value = value; |
2168bfb8 JM |
4753 | /* Like in cxx_eval_store_expression, omit entries for empty fields. */ |
4754 | bool no_slot = TREE_CODE (type) == RECORD_TYPE && is_empty_field (index); | |
4755 | if (no_slot) | |
4756 | new_ctx = *ctx; | |
4757 | else | |
4758 | init_subob_ctx (ctx, new_ctx, index, value); | |
37244b21 | 4759 | int pos_hint = -1; |
3e605b20 | 4760 | if (new_ctx.ctor != ctx->ctor) |
37244b21 PP |
4761 | { |
4762 | /* If we built a new CONSTRUCTOR, attach it now so that other | |
4763 | initializers can refer to it. */ | |
077dd9b3 PP |
4764 | constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index); |
4765 | cep->value = new_ctx.ctor; | |
4766 | pos_hint = cep - (*p)->begin(); | |
37244b21 | 4767 | } |
b599bf9d | 4768 | else if (TREE_CODE (type) == UNION_TYPE) |
37244b21 PP |
4769 | /* Otherwise if we're constructing a non-aggregate union member, set |
4770 | the active union member now so that we can later detect and diagnose | |
4771 | if its initializer attempts to activate another member. */ | |
077dd9b3 | 4772 | get_or_insert_ctor_field (ctx->ctor, index); |
3e605b20 | 4773 | tree elt = cxx_eval_constant_expression (&new_ctx, value, |
92a596e8 | 4774 | lval, |
5a804683 | 4775 | non_constant_p, overflow_p); |
2d76680f | 4776 | /* Don't VERIFY_CONSTANT here. */ |
2b3ab879 | 4777 | if (ctx->quiet && *non_constant_p) |
3e605b20 | 4778 | break; |
bcb5f3c9 | 4779 | if (elt != orig_value) |
2d76680f | 4780 | changed = true; |
2d63bc39 JM |
4781 | |
4782 | if (!TREE_CONSTANT (elt)) | |
4783 | constant_p = false; | |
4784 | if (TREE_SIDE_EFFECTS (elt)) | |
4785 | side_effects_p = true; | |
3e605b20 | 4786 | if (index && TREE_CODE (index) == COMPONENT_REF) |
2d76680f PC |
4787 | { |
4788 | /* This is an initialization of a vfield inside a base | |
4789 | subaggregate that we already initialized; push this | |
4790 | initialization into the previous initialization. */ | |
3e605b20 | 4791 | constructor_elt *inner = base_field_constructor_elt (*p, index); |
2d76680f | 4792 | inner->value = elt; |
3e605b20 | 4793 | changed = true; |
2d76680f | 4794 | } |
3e605b20 JM |
4795 | else if (index |
4796 | && (TREE_CODE (index) == NOP_EXPR | |
4797 | || TREE_CODE (index) == POINTER_PLUS_EXPR)) | |
2d76680f PC |
4798 | { |
4799 | /* This is an initializer for an empty base; now that we've | |
4800 | checked that it's constant, we can ignore it. */ | |
3e605b20 JM |
4801 | gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index)))); |
4802 | changed = true; | |
4803 | } | |
2168bfb8 JM |
4804 | else if (no_slot) |
4805 | changed = true; | |
2d76680f | 4806 | else |
ac9ec198 | 4807 | { |
b599bf9d PP |
4808 | if (TREE_CODE (type) == UNION_TYPE |
4809 | && (*p)->last().index != index) | |
37244b21 PP |
4810 | /* The initializer erroneously changed the active union member that |
4811 | we're initializing. */ | |
b599bf9d | 4812 | gcc_assert (*non_constant_p); |
37244b21 | 4813 | else |
1bdbef09 | 4814 | { |
37244b21 PP |
4815 | /* The initializer might have mutated the underlying CONSTRUCTOR, |
4816 | so recompute the location of the target constructer_elt. */ | |
4817 | constructor_elt *cep | |
4818 | = get_or_insert_ctor_field (ctx->ctor, index, pos_hint); | |
4819 | cep->value = elt; | |
1bdbef09 | 4820 | } |
37244b21 | 4821 | |
1bdbef09 | 4822 | /* Adding or replacing an element might change the ctor's flags. */ |
ac9ec198 MP |
4823 | TREE_CONSTANT (ctx->ctor) = constant_p; |
4824 | TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p; | |
4825 | } | |
2d76680f PC |
4826 | } |
4827 | if (*non_constant_p || !changed) | |
3e605b20 JM |
4828 | return t; |
4829 | t = ctx->ctor; | |
4830 | /* We're done building this CONSTRUCTOR, so now we can interpret an | |
4831 | element without an explicit initializer as value-initialized. */ | |
e8c48716 | 4832 | CONSTRUCTOR_NO_CLEARING (t) = false; |
2d63bc39 JM |
4833 | TREE_CONSTANT (t) = constant_p; |
4834 | TREE_SIDE_EFFECTS (t) = side_effects_p; | |
8a29084d | 4835 | if (VECTOR_TYPE_P (type)) |
2d76680f PC |
4836 | t = fold (t); |
4837 | return t; | |
4838 | } | |
4839 | ||
4840 | /* Subroutine of cxx_eval_constant_expression. | |
4841 | The expression tree T is a VEC_INIT_EXPR which denotes the desired | |
4842 | initialization of a non-static data member of array type. Reduce it to a | |
4843 | CONSTRUCTOR. | |
4844 | ||
4845 | Note that apart from value-initialization (when VALUE_INIT is true), | |
4846 | this is only intended to support value-initialization and the | |
4847 | initializations done by defaulted constructors for classes with | |
4848 | non-static data members of array type. In this case, VEC_INIT_EXPR_INIT | |
4849 | will either be NULL_TREE for the default constructor, or a COMPONENT_REF | |
4850 | for the copy/move constructor. */ | |
4851 | ||
4852 | static tree | |
3e605b20 | 4853 | cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init, |
92a596e8 | 4854 | bool value_init, bool lval, |
2d76680f PC |
4855 | bool *non_constant_p, bool *overflow_p) |
4856 | { | |
4857 | tree elttype = TREE_TYPE (atype); | |
3e605b20 JM |
4858 | verify_ctor_sanity (ctx, atype); |
4859 | vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); | |
2d76680f | 4860 | bool pre_init = false; |
4784470a | 4861 | unsigned HOST_WIDE_INT i; |
dc5ca6c8 | 4862 | tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; |
2d76680f | 4863 | |
a1c9c9ff JM |
4864 | if (init && TREE_CODE (init) == CONSTRUCTOR) |
4865 | return cxx_eval_bare_aggregate (ctx, init, lval, | |
4866 | non_constant_p, overflow_p); | |
4867 | ||
2d76680f PC |
4868 | /* For the default constructor, build up a call to the default |
4869 | constructor of the element type. We only need to handle class types | |
4870 | here, as for a constructor to be constexpr, all members must be | |
4871 | initialized, which for a defaulted default constructor means they must | |
4872 | be of a class type with a constexpr default constructor. */ | |
4873 | if (TREE_CODE (elttype) == ARRAY_TYPE) | |
4874 | /* We only do this at the lowest level. */; | |
4875 | else if (value_init) | |
4876 | { | |
dc5ca6c8 | 4877 | init = build_value_init (elttype, complain); |
2d76680f PC |
4878 | pre_init = true; |
4879 | } | |
4880 | else if (!init) | |
4881 | { | |
cd9cf97b | 4882 | releasing_vec argvec; |
2d76680f PC |
4883 | init = build_special_member_call (NULL_TREE, complete_ctor_identifier, |
4884 | &argvec, elttype, LOOKUP_NORMAL, | |
dc5ca6c8 | 4885 | complain); |
5dab8b11 | 4886 | init = build_aggr_init_expr (elttype, init); |
2d76680f PC |
4887 | pre_init = true; |
4888 | } | |
4889 | ||
d21252de PP |
4890 | bool zeroed_out = false; |
4891 | if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor)) | |
4892 | { | |
4893 | /* We're initializing an array object that had been zero-initialized | |
4894 | earlier. Truncate ctx->ctor, and propagate its zeroed state by | |
4895 | clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element | |
4896 | initializers we append to it. */ | |
4897 | gcc_checking_assert (initializer_zerop (ctx->ctor)); | |
4898 | zeroed_out = true; | |
4899 | vec_safe_truncate (*p, 0); | |
4900 | } | |
4901 | ||
74f8705e MP |
4902 | tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p, |
4903 | overflow_p); | |
4904 | unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts); | |
4784470a | 4905 | for (i = 0; i < max; ++i) |
2d76680f PC |
4906 | { |
4907 | tree idx = build_int_cst (size_type_node, i); | |
4908 | tree eltinit; | |
928af3bf | 4909 | bool reuse = false; |
3e605b20 JM |
4910 | constexpr_ctx new_ctx; |
4911 | init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype); | |
4912 | if (new_ctx.ctor != ctx->ctor) | |
d21252de PP |
4913 | { |
4914 | if (zeroed_out) | |
4915 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false; | |
4916 | CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor); | |
4917 | } | |
2d76680f PC |
4918 | if (TREE_CODE (elttype) == ARRAY_TYPE) |
4919 | { | |
4920 | /* A multidimensional array; recurse. */ | |
4921 | if (value_init || init == NULL_TREE) | |
928af3bf JJ |
4922 | { |
4923 | eltinit = NULL_TREE; | |
4924 | reuse = i == 0; | |
4925 | } | |
2d76680f | 4926 | else |
dc5ca6c8 | 4927 | eltinit = cp_build_array_ref (input_location, init, idx, complain); |
3e605b20 | 4928 | eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init, |
92a596e8 | 4929 | lval, |
2d76680f PC |
4930 | non_constant_p, overflow_p); |
4931 | } | |
4932 | else if (pre_init) | |
4933 | { | |
4934 | /* Initializing an element using value or default initialization | |
4935 | we just pre-built above. */ | |
3ee378fb JM |
4936 | if (init == void_node) |
4937 | /* Trivial default-init, don't do anything to the CONSTRUCTOR. */ | |
4938 | return ctx->ctor; | |
928af3bf JJ |
4939 | eltinit = cxx_eval_constant_expression (&new_ctx, init, lval, |
4940 | non_constant_p, overflow_p); | |
4941 | reuse = i == 0; | |
2d76680f PC |
4942 | } |
4943 | else | |
4944 | { | |
4945 | /* Copying an element. */ | |
4946 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
4947 | (atype, TREE_TYPE (init))); | |
dc5ca6c8 | 4948 | eltinit = cp_build_array_ref (input_location, init, idx, complain); |
72b3e203 | 4949 | if (!lvalue_p (init)) |
2d76680f | 4950 | eltinit = move (eltinit); |
dc5ca6c8 | 4951 | eltinit = force_rvalue (eltinit, complain); |
c2236b9b JJ |
4952 | eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval, |
4953 | non_constant_p, overflow_p); | |
2d76680f | 4954 | } |
6b7d53a2 | 4955 | if (*non_constant_p) |
3e605b20 JM |
4956 | break; |
4957 | if (new_ctx.ctor != ctx->ctor) | |
4958 | { | |
4959 | /* We appended this element above; update the value. */ | |
4960 | gcc_assert ((*p)->last().index == idx); | |
4961 | (*p)->last().value = eltinit; | |
4962 | } | |
4963 | else | |
4964 | CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit); | |
928af3bf | 4965 | /* Reuse the result of cxx_eval_constant_expression call |
c2236b9b JJ |
4966 | from the first iteration to all others if it is a constant |
4967 | initializer that doesn't require relocations. */ | |
928af3bf JJ |
4968 | if (reuse |
4969 | && max > 1 | |
c2236b9b JJ |
4970 | && (eltinit == NULL_TREE |
4971 | || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit)) | |
4972 | == null_pointer_node))) | |
928af3bf JJ |
4973 | { |
4974 | if (new_ctx.ctor != ctx->ctor) | |
4975 | eltinit = new_ctx.ctor; | |
1e027956 RB |
4976 | tree range = build2 (RANGE_EXPR, size_type_node, |
4977 | build_int_cst (size_type_node, 1), | |
4978 | build_int_cst (size_type_node, max - 1)); | |
4979 | CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit)); | |
928af3bf JJ |
4980 | break; |
4981 | } | |
1e027956 RB |
4982 | else if (i == 0) |
4983 | vec_safe_reserve (*p, max); | |
2d76680f PC |
4984 | } |
4985 | ||
4986 | if (!*non_constant_p) | |
4987 | { | |
3e605b20 | 4988 | init = ctx->ctor; |
e8c48716 | 4989 | CONSTRUCTOR_NO_CLEARING (init) = false; |
2d76680f | 4990 | } |
2d76680f PC |
4991 | return init; |
4992 | } | |
4993 | ||
4994 | static tree | |
3e605b20 | 4995 | cxx_eval_vec_init (const constexpr_ctx *ctx, tree t, |
92a596e8 | 4996 | bool lval, |
2d76680f PC |
4997 | bool *non_constant_p, bool *overflow_p) |
4998 | { | |
4999 | tree atype = TREE_TYPE (t); | |
5000 | tree init = VEC_INIT_EXPR_INIT (t); | |
e948436e JM |
5001 | bool value_init = VEC_INIT_EXPR_VALUE_INIT (t); |
5002 | if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init)) | |
5003 | ; | |
5004 | else if (CONSTRUCTOR_NELTS (init) == 0) | |
5005 | { | |
5006 | /* Handle {} as value-init. */ | |
5007 | init = NULL_TREE; | |
5008 | value_init = true; | |
5009 | } | |
5010 | else | |
5011 | { | |
5012 | /* This is a more complicated case, like needing to loop over trailing | |
5013 | elements; call build_vec_init and evaluate the result. */ | |
5014 | tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; | |
5015 | constexpr_ctx new_ctx = *ctx; | |
5016 | if (!ctx->object) | |
5017 | { | |
5018 | /* We want to have an initialization target for an VEC_INIT_EXPR. | |
5019 | If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */ | |
5020 | new_ctx.object = VEC_INIT_EXPR_SLOT (t); | |
5021 | tree ctor = new_ctx.ctor = build_constructor (atype, NULL); | |
5022 | CONSTRUCTOR_NO_CLEARING (ctor) = true; | |
5023 | ctx->global->values.put (new_ctx.object, ctor); | |
5024 | ctx = &new_ctx; | |
5025 | } | |
5026 | init = expand_vec_init_expr (ctx->object, t, complain); | |
5027 | return cxx_eval_constant_expression (ctx, init, lval, non_constant_p, | |
5028 | overflow_p); | |
5029 | } | |
5030 | tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init, | |
92a596e8 | 5031 | lval, non_constant_p, overflow_p); |
2d76680f PC |
5032 | if (*non_constant_p) |
5033 | return t; | |
5034 | else | |
5035 | return r; | |
5036 | } | |
5037 | ||
e4511ca2 JM |
5038 | /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case |
5039 | where the desired type is an array of unknown bounds because the variable | |
5040 | has had its bounds deduced since the wrapping expression was created. */ | |
5041 | ||
5042 | static bool | |
5043 | same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2) | |
5044 | { | |
5045 | while (TREE_CODE (type1) == ARRAY_TYPE | |
5046 | && TREE_CODE (type2) == ARRAY_TYPE | |
5047 | && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2))) | |
5048 | { | |
5049 | type1 = TREE_TYPE (type1); | |
5050 | type2 = TREE_TYPE (type2); | |
5051 | } | |
5052 | return same_type_ignoring_top_level_qualifiers_p (type1, type2); | |
5053 | } | |
5054 | ||
43e84ce7 JJ |
5055 | /* Try to determine the currently active union member for an expression |
5056 | with UNION_TYPE. If it can be determined, return the FIELD_DECL, | |
5057 | otherwise return NULL_TREE. */ | |
5058 | ||
5059 | static tree | |
5060 | cxx_union_active_member (const constexpr_ctx *ctx, tree t) | |
5061 | { | |
5062 | constexpr_ctx new_ctx = *ctx; | |
5063 | new_ctx.quiet = true; | |
5064 | bool non_constant_p = false, overflow_p = false; | |
5065 | tree ctor = cxx_eval_constant_expression (&new_ctx, t, false, | |
5066 | &non_constant_p, | |
5067 | &overflow_p); | |
5068 | if (TREE_CODE (ctor) == CONSTRUCTOR | |
5069 | && CONSTRUCTOR_NELTS (ctor) == 1 | |
5070 | && CONSTRUCTOR_ELT (ctor, 0)->index | |
5071 | && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL) | |
5072 | return CONSTRUCTOR_ELT (ctor, 0)->index; | |
5073 | return NULL_TREE; | |
5074 | } | |
5075 | ||
0fe2ae29 JJ |
5076 | /* Helper function for cxx_fold_indirect_ref_1, called recursively. */ |
5077 | ||
5078 | static tree | |
43e84ce7 JJ |
5079 | cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type, |
5080 | tree op, unsigned HOST_WIDE_INT off, bool *empty_base) | |
0fe2ae29 JJ |
5081 | { |
5082 | tree optype = TREE_TYPE (op); | |
5083 | unsigned HOST_WIDE_INT const_nunits; | |
bc99c54d JM |
5084 | if (off == 0 && similar_type_p (optype, type)) |
5085 | return op; | |
5086 | else if (TREE_CODE (optype) == COMPLEX_TYPE | |
5087 | && similar_type_p (type, TREE_TYPE (optype))) | |
0fe2ae29 | 5088 | { |
0fe2ae29 | 5089 | /* *(foo *)&complexfoo => __real__ complexfoo */ |
bc99c54d | 5090 | if (off == 0) |
0fe2ae29 | 5091 | return build1_loc (loc, REALPART_EXPR, type, op); |
bc99c54d JM |
5092 | /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */ |
5093 | else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off) | |
5094 | return build1_loc (loc, IMAGPART_EXPR, type, op); | |
0fe2ae29 JJ |
5095 | } |
5096 | /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */ | |
5097 | else if (VECTOR_TYPE_P (optype) | |
5098 | && similar_type_p (type, TREE_TYPE (optype)) | |
5099 | && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits)) | |
5100 | { | |
5101 | unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type)); | |
5102 | unsigned HOST_WIDE_INT max_offset = part_width * const_nunits; | |
5103 | if (off < max_offset && off % part_width == 0) | |
5104 | { | |
5105 | tree index = bitsize_int (off * BITS_PER_UNIT); | |
5106 | return build3_loc (loc, BIT_FIELD_REF, type, op, | |
5107 | TYPE_SIZE (type), index); | |
5108 | } | |
5109 | } | |
5110 | /* ((foo *)&fooarray)[x] => fooarray[x] */ | |
5111 | else if (TREE_CODE (optype) == ARRAY_TYPE | |
5112 | && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype))) | |
5113 | && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype)))) | |
5114 | { | |
5115 | tree type_domain = TYPE_DOMAIN (optype); | |
5116 | tree min_val = size_zero_node; | |
5117 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
5118 | min_val = TYPE_MIN_VALUE (type_domain); | |
5119 | unsigned HOST_WIDE_INT el_sz | |
5120 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype))); | |
5121 | unsigned HOST_WIDE_INT idx = off / el_sz; | |
5122 | unsigned HOST_WIDE_INT rem = off % el_sz; | |
5123 | if (tree_fits_uhwi_p (min_val)) | |
5124 | { | |
5125 | tree index = size_int (idx + tree_to_uhwi (min_val)); | |
5126 | op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index, | |
5127 | NULL_TREE, NULL_TREE); | |
43e84ce7 | 5128 | return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem, |
0fe2ae29 JJ |
5129 | empty_base); |
5130 | } | |
5131 | } | |
5132 | /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */ | |
43e84ce7 JJ |
5133 | else if (TREE_CODE (optype) == RECORD_TYPE |
5134 | || TREE_CODE (optype) == UNION_TYPE) | |
0fe2ae29 | 5135 | { |
43e84ce7 JJ |
5136 | if (TREE_CODE (optype) == UNION_TYPE) |
5137 | /* For unions prefer the currently active member. */ | |
5138 | if (tree field = cxx_union_active_member (ctx, op)) | |
5139 | { | |
5140 | unsigned HOST_WIDE_INT el_sz | |
5141 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field))); | |
5142 | if (off < el_sz) | |
5143 | { | |
5144 | tree cop = build3 (COMPONENT_REF, TREE_TYPE (field), | |
5145 | op, field, NULL_TREE); | |
5146 | if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop, | |
5147 | off, empty_base)) | |
5148 | return ret; | |
5149 | } | |
5150 | } | |
0fe2ae29 JJ |
5151 | for (tree field = TYPE_FIELDS (optype); |
5152 | field; field = DECL_CHAIN (field)) | |
5153 | if (TREE_CODE (field) == FIELD_DECL | |
5154 | && TREE_TYPE (field) != error_mark_node | |
5155 | && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field)))) | |
5156 | { | |
5157 | tree pos = byte_position (field); | |
5158 | if (!tree_fits_uhwi_p (pos)) | |
5159 | continue; | |
5160 | unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos); | |
43e84ce7 | 5161 | unsigned HOST_WIDE_INT el_sz |
0fe2ae29 JJ |
5162 | = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field))); |
5163 | if (upos <= off && off < upos + el_sz) | |
5164 | { | |
5165 | tree cop = build3 (COMPONENT_REF, TREE_TYPE (field), | |
5166 | op, field, NULL_TREE); | |
43e84ce7 | 5167 | if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop, |
0fe2ae29 JJ |
5168 | off - upos, |
5169 | empty_base)) | |
5170 | return ret; | |
5171 | } | |
5172 | } | |
bc99c54d JM |
5173 | /* Also handle conversion to an empty base class, which |
5174 | is represented with a NOP_EXPR. */ | |
5175 | if (is_empty_class (type) | |
5176 | && CLASS_TYPE_P (optype) | |
5177 | && DERIVED_FROM_P (type, optype)) | |
5178 | { | |
5179 | *empty_base = true; | |
5180 | return op; | |
5181 | } | |
0fe2ae29 JJ |
5182 | } |
5183 | ||
5184 | return NULL_TREE; | |
5185 | } | |
5186 | ||
2d76680f PC |
5187 | /* A less strict version of fold_indirect_ref_1, which requires cv-quals to |
5188 | match. We want to be less strict for simple *& folding; if we have a | |
5189 | non-const temporary that we access through a const pointer, that should | |
5190 | work. We handle this here rather than change fold_indirect_ref_1 | |
5191 | because we're dealing with things like ADDR_EXPR of INTEGER_CST which | |
5192 | don't really make sense outside of constant expression evaluation. Also | |
5193 | we want to allow folding to COMPONENT_REF, which could cause trouble | |
0fe2ae29 | 5194 | with TBAA in fold_indirect_ref_1. */ |
2d76680f PC |
5195 | |
5196 | static tree | |
43e84ce7 JJ |
5197 | cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type, |
5198 | tree op0, bool *empty_base) | |
2d76680f | 5199 | { |
ebe4bf41 MP |
5200 | tree sub = op0; |
5201 | tree subtype; | |
5202 | poly_uint64 const_op01; | |
2d76680f | 5203 | |
80de0002 JM |
5204 | /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */ |
5205 | while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR | |
5206 | || TREE_CODE (sub) == VIEW_CONVERT_EXPR) | |
5207 | { | |
5208 | if (TREE_CODE (sub) == NOP_EXPR | |
5209 | && REINTERPRET_CAST_P (sub)) | |
5210 | return NULL_TREE; | |
5211 | sub = TREE_OPERAND (sub, 0); | |
5212 | } | |
5213 | ||
2d76680f | 5214 | subtype = TREE_TYPE (sub); |
71a93b08 | 5215 | if (!INDIRECT_TYPE_P (subtype)) |
2d76680f PC |
5216 | return NULL_TREE; |
5217 | ||
ab36b554 PP |
5218 | /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing |
5219 | the innermost component into the offset until it would make the | |
5220 | offset positive, so that cxx_fold_indirect_ref_1 can identify | |
5221 | more folding opportunities. */ | |
5222 | auto canonicalize_obj_off = [] (tree& obj, tree& off) { | |
5223 | while (TREE_CODE (obj) == COMPONENT_REF | |
5224 | && (tree_int_cst_sign_bit (off) || integer_zerop (off))) | |
5225 | { | |
5226 | tree field = TREE_OPERAND (obj, 1); | |
5227 | tree pos = byte_position (field); | |
5228 | if (integer_zerop (off) && integer_nonzerop (pos)) | |
5229 | /* If the offset is already 0, keep going as long as the | |
5230 | component is at position 0. */ | |
5231 | break; | |
5232 | off = int_const_binop (PLUS_EXPR, off, pos); | |
5233 | obj = TREE_OPERAND (obj, 0); | |
5234 | } | |
5235 | }; | |
5236 | ||
2d76680f PC |
5237 | if (TREE_CODE (sub) == ADDR_EXPR) |
5238 | { | |
5239 | tree op = TREE_OPERAND (sub, 0); | |
5240 | tree optype = TREE_TYPE (op); | |
5241 | ||
5242 | /* *&CONST_DECL -> to the value of the const decl. */ | |
5243 | if (TREE_CODE (op) == CONST_DECL) | |
5244 | return DECL_INITIAL (op); | |
5245 | /* *&p => p; make sure to handle *&"str"[cst] here. */ | |
1a120ec1 | 5246 | if (similar_type_p (optype, type)) |
2d76680f PC |
5247 | { |
5248 | tree fop = fold_read_from_constant_string (op); | |
5249 | if (fop) | |
5250 | return fop; | |
5251 | else | |
5252 | return op; | |
5253 | } | |
0fe2ae29 | 5254 | else |
ab36b554 PP |
5255 | { |
5256 | tree off = integer_zero_node; | |
5257 | canonicalize_obj_off (op, off); | |
5258 | gcc_assert (integer_zerop (off)); | |
5259 | return cxx_fold_indirect_ref_1 (ctx, loc, type, op, 0, empty_base); | |
5260 | } | |
2d76680f PC |
5261 | } |
5262 | else if (TREE_CODE (sub) == POINTER_PLUS_EXPR | |
0fe2ae29 | 5263 | && tree_fits_uhwi_p (TREE_OPERAND (sub, 1))) |
2d76680f PC |
5264 | { |
5265 | tree op00 = TREE_OPERAND (sub, 0); | |
0120cd93 | 5266 | tree off = TREE_OPERAND (sub, 1); |
2d76680f PC |
5267 | |
5268 | STRIP_NOPS (op00); | |
5269 | if (TREE_CODE (op00) == ADDR_EXPR) | |
0120cd93 PP |
5270 | { |
5271 | tree obj = TREE_OPERAND (op00, 0); | |
ab36b554 | 5272 | canonicalize_obj_off (obj, off); |
0120cd93 PP |
5273 | return cxx_fold_indirect_ref_1 (ctx, loc, type, obj, |
5274 | tree_to_uhwi (off), empty_base); | |
5275 | } | |
2d76680f PC |
5276 | } |
5277 | /* *(foo *)fooarrptr => (*fooarrptr)[0] */ | |
5278 | else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE | |
1a120ec1 | 5279 | && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype)))) |
2d76680f PC |
5280 | { |
5281 | tree type_domain; | |
5282 | tree min_val = size_zero_node; | |
ebe4bf41 | 5283 | tree newsub |
43e84ce7 | 5284 | = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL); |
2d76680f PC |
5285 | if (newsub) |
5286 | sub = newsub; | |
5287 | else | |
5288 | sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub); | |
5289 | type_domain = TYPE_DOMAIN (TREE_TYPE (sub)); | |
5290 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
5291 | min_val = TYPE_MIN_VALUE (type_domain); | |
5292 | return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE, | |
5293 | NULL_TREE); | |
5294 | } | |
5295 | ||
5296 | return NULL_TREE; | |
5297 | } | |
5298 | ||
5299 | static tree | |
3e605b20 | 5300 | cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t, |
92a596e8 | 5301 | bool lval, |
2d76680f PC |
5302 | bool *non_constant_p, bool *overflow_p) |
5303 | { | |
5304 | tree orig_op0 = TREE_OPERAND (t, 0); | |
2d76680f | 5305 | bool empty_base = false; |
2d76680f | 5306 | |
cda0a029 JM |
5307 | /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second |
5308 | operand is an integer-zero. Otherwise reject the MEM_REF for now. */ | |
5309 | ||
5310 | if (TREE_CODE (t) == MEM_REF | |
5311 | && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1)))) | |
5312 | { | |
5313 | gcc_assert (ctx->quiet); | |
5314 | *non_constant_p = true; | |
5315 | return t; | |
5316 | } | |
5317 | ||
255a48d6 | 5318 | /* First try to simplify it directly. */ |
43e84ce7 JJ |
5319 | tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), |
5320 | orig_op0, &empty_base); | |
255a48d6 | 5321 | if (!r) |
2d76680f | 5322 | { |
255a48d6 JM |
5323 | /* If that didn't work, evaluate the operand first. */ |
5324 | tree op0 = cxx_eval_constant_expression (ctx, orig_op0, | |
5325 | /*lval*/false, non_constant_p, | |
5326 | overflow_p); | |
5327 | /* Don't VERIFY_CONSTANT here. */ | |
5328 | if (*non_constant_p) | |
5329 | return t; | |
5330 | ||
8bada5cd MS |
5331 | if (!lval && integer_zerop (op0)) |
5332 | { | |
5333 | if (!ctx->quiet) | |
5334 | error ("dereferencing a null pointer"); | |
5335 | *non_constant_p = true; | |
5336 | return t; | |
5337 | } | |
5338 | ||
43e84ce7 | 5339 | r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0, |
255a48d6 JM |
5340 | &empty_base); |
5341 | if (r == NULL_TREE) | |
2d76680f PC |
5342 | { |
5343 | /* We couldn't fold to a constant value. Make sure it's not | |
5344 | something we should have been able to fold. */ | |
255a48d6 JM |
5345 | tree sub = op0; |
5346 | STRIP_NOPS (sub); | |
5347 | if (TREE_CODE (sub) == ADDR_EXPR) | |
5348 | { | |
1a120ec1 | 5349 | gcc_assert (!similar_type_p |
255a48d6 JM |
5350 | (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t))); |
5351 | /* DR 1188 says we don't have to deal with this. */ | |
5352 | if (!ctx->quiet) | |
1a120ec1 JM |
5353 | error_at (cp_expr_loc_or_input_loc (t), |
5354 | "accessing value of %qE through a %qT glvalue in a " | |
5355 | "constant expression", build_fold_indirect_ref (sub), | |
5356 | TREE_TYPE (t)); | |
255a48d6 JM |
5357 | *non_constant_p = true; |
5358 | return t; | |
5359 | } | |
5360 | ||
5361 | if (lval && op0 != orig_op0) | |
5362 | return build1 (INDIRECT_REF, TREE_TYPE (t), op0); | |
5363 | if (!lval) | |
5364 | VERIFY_CONSTANT (t); | |
2d76680f PC |
5365 | return t; |
5366 | } | |
5367 | } | |
5368 | ||
255a48d6 JM |
5369 | r = cxx_eval_constant_expression (ctx, r, |
5370 | lval, non_constant_p, overflow_p); | |
5371 | if (*non_constant_p) | |
5372 | return t; | |
5373 | ||
125db6a1 | 5374 | /* If we're pulling out the value of an empty base, just return an empty |
2d76680f | 5375 | CONSTRUCTOR. */ |
92a596e8 | 5376 | if (empty_base && !lval) |
2d76680f | 5377 | { |
2d76680f PC |
5378 | r = build_constructor (TREE_TYPE (t), NULL); |
5379 | TREE_CONSTANT (r) = true; | |
5380 | } | |
5381 | ||
2d76680f PC |
5382 | return r; |
5383 | } | |
5384 | ||
5385 | /* Complain about R, a VAR_DECL, not being usable in a constant expression. | |
5386 | Shared between potential_constant_expression and | |
5387 | cxx_eval_constant_expression. */ | |
5388 | ||
5389 | static void | |
6821245b | 5390 | non_const_var_error (location_t loc, tree r) |
2d76680f | 5391 | { |
8e007055 | 5392 | auto_diagnostic_group d; |
2d76680f | 5393 | tree type = TREE_TYPE (r); |
8e007055 | 5394 | if (DECL_NAME (r) == heap_uninit_identifier |
815baade JJ |
5395 | || DECL_NAME (r) == heap_identifier |
5396 | || DECL_NAME (r) == heap_vec_uninit_identifier | |
5397 | || DECL_NAME (r) == heap_vec_identifier) | |
8e007055 | 5398 | { |
6821245b JM |
5399 | error_at (loc, "the content of uninitialized storage is not usable " |
5400 | "in a constant expression"); | |
8e007055 JJ |
5401 | inform (DECL_SOURCE_LOCATION (r), "allocated here"); |
5402 | return; | |
5403 | } | |
5404 | if (DECL_NAME (r) == heap_deleted_identifier) | |
5405 | { | |
6821245b JM |
5406 | error_at (loc, "use of allocated storage after deallocation in a " |
5407 | "constant expression"); | |
8e007055 JJ |
5408 | inform (DECL_SOURCE_LOCATION (r), "allocated here"); |
5409 | return; | |
5410 | } | |
6821245b JM |
5411 | error_at (loc, "the value of %qD is not usable in a constant " |
5412 | "expression", r); | |
2d76680f PC |
5413 | /* Avoid error cascade. */ |
5414 | if (DECL_INITIAL (r) == error_mark_node) | |
5415 | return; | |
5416 | if (DECL_DECLARED_CONSTEXPR_P (r)) | |
5417 | inform (DECL_SOURCE_LOCATION (r), | |
5418 | "%qD used in its own initializer", r); | |
5419 | else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) | |
5420 | { | |
5421 | if (!CP_TYPE_CONST_P (type)) | |
5422 | inform (DECL_SOURCE_LOCATION (r), | |
5423 | "%q#D is not const", r); | |
5424 | else if (CP_TYPE_VOLATILE_P (type)) | |
5425 | inform (DECL_SOURCE_LOCATION (r), | |
5426 | "%q#D is volatile", r); | |
5427 | else if (!DECL_INITIAL (r) | |
a3e2b438 PP |
5428 | || !TREE_CONSTANT (DECL_INITIAL (r)) |
5429 | || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)) | |
2d76680f PC |
5430 | inform (DECL_SOURCE_LOCATION (r), |
5431 | "%qD was not initialized with a constant " | |
5432 | "expression", r); | |
5433 | else | |
5434 | gcc_unreachable (); | |
5435 | } | |
9f613f06 | 5436 | else if (TYPE_REF_P (type)) |
fd338b13 JM |
5437 | inform (DECL_SOURCE_LOCATION (r), |
5438 | "%qD was not initialized with a constant " | |
5439 | "expression", r); | |
2d76680f PC |
5440 | else |
5441 | { | |
5442 | if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r)) | |
5443 | inform (DECL_SOURCE_LOCATION (r), | |
5444 | "%qD was not declared %<constexpr%>", r); | |
5445 | else | |
5446 | inform (DECL_SOURCE_LOCATION (r), | |
5447 | "%qD does not have integral or enumeration type", | |
5448 | r); | |
5449 | } | |
5450 | } | |
5451 | ||
5452 | /* Subroutine of cxx_eval_constant_expression. | |
5453 | Like cxx_eval_unary_expression, except for trinary expressions. */ | |
5454 | ||
5455 | static tree | |
3e605b20 | 5456 | cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t, |
92a596e8 | 5457 | bool lval, |
2d76680f PC |
5458 | bool *non_constant_p, bool *overflow_p) |
5459 | { | |
5460 | int i; | |
5461 | tree args[3]; | |
5462 | tree val; | |
5463 | ||
5464 | for (i = 0; i < 3; i++) | |
5465 | { | |
3e605b20 | 5466 | args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i), |
92a596e8 | 5467 | lval, |
5a804683 | 5468 | non_constant_p, overflow_p); |
2d76680f PC |
5469 | VERIFY_CONSTANT (args[i]); |
5470 | } | |
5471 | ||
5472 | val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t), | |
5473 | args[0], args[1], args[2]); | |
5474 | if (val == NULL_TREE) | |
5475 | return t; | |
5476 | VERIFY_CONSTANT (val); | |
5477 | return val; | |
5478 | } | |
5479 | ||
98e5a19a JM |
5480 | /* True if T was declared in a function declared to be constexpr, and |
5481 | therefore potentially constant in C++14. */ | |
5482 | ||
2d76680f PC |
5483 | bool |
5484 | var_in_constexpr_fn (tree t) | |
5485 | { | |
5486 | tree ctx = DECL_CONTEXT (t); | |
98e5a19a | 5487 | return (ctx && TREE_CODE (ctx) == FUNCTION_DECL |
2d76680f PC |
5488 | && DECL_DECLARED_CONSTEXPR_P (ctx)); |
5489 | } | |
5490 | ||
26dbe85a MP |
5491 | /* True if a function might be constexpr: either a function that was |
5492 | declared constexpr, or a C++17 lambda op(). */ | |
5493 | ||
5494 | bool | |
5495 | maybe_constexpr_fn (tree t) | |
5496 | { | |
5497 | return (DECL_DECLARED_CONSTEXPR_P (t) | |
87c2080b JM |
5498 | || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t)) |
5499 | || (flag_implicit_constexpr | |
5500 | && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t)))); | |
26dbe85a MP |
5501 | } |
5502 | ||
98e5a19a JM |
5503 | /* True if T was declared in a function that might be constexpr: either a |
5504 | function that was declared constexpr, or a C++17 lambda op(). */ | |
5505 | ||
5506 | bool | |
5507 | var_in_maybe_constexpr_fn (tree t) | |
5508 | { | |
87c2080b JM |
5509 | return (DECL_FUNCTION_SCOPE_P (t) |
5510 | && maybe_constexpr_fn (DECL_CONTEXT (t))); | |
98e5a19a JM |
5511 | } |
5512 | ||
68d01920 JM |
5513 | /* We're assigning INIT to TARGET. In do_build_copy_constructor and |
5514 | build_over_call we implement trivial copy of a class with tail padding using | |
5515 | assignment of character arrays, which is valid in normal code, but not in | |
5516 | constexpr evaluation. We don't need to worry about clobbering tail padding | |
5517 | in constexpr evaluation, so strip the type punning. */ | |
5518 | ||
5519 | static void | |
5520 | maybe_simplify_trivial_copy (tree &target, tree &init) | |
5521 | { | |
5522 | if (TREE_CODE (target) == MEM_REF | |
5523 | && TREE_CODE (init) == MEM_REF | |
5524 | && TREE_TYPE (target) == TREE_TYPE (init) | |
5525 | && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE | |
5526 | && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node) | |
5527 | { | |
5528 | target = build_fold_indirect_ref (TREE_OPERAND (target, 0)); | |
5529 | init = build_fold_indirect_ref (TREE_OPERAND (init, 0)); | |
5530 | } | |
5531 | } | |
5532 | ||
7eb5be6a MP |
5533 | /* Returns true if REF, which is a COMPONENT_REF, has any fields |
5534 | of constant type. This does not check for 'mutable', so the | |
5535 | caller is expected to be mindful of that. */ | |
5536 | ||
5537 | static bool | |
5538 | cref_has_const_field (tree ref) | |
5539 | { | |
5540 | while (TREE_CODE (ref) == COMPONENT_REF) | |
5541 | { | |
5542 | if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1)))) | |
5543 | return true; | |
5544 | ref = TREE_OPERAND (ref, 0); | |
5545 | } | |
5546 | return false; | |
5547 | } | |
5548 | ||
04e1749c MP |
5549 | /* Return true if we are modifying something that is const during constant |
5550 | expression evaluation. CODE is the code of the statement, OBJ is the | |
5551 | object in question, MUTABLE_P is true if one of the subobjects were | |
5552 | declared mutable. */ | |
5553 | ||
5554 | static bool | |
5555 | modifying_const_object_p (tree_code code, tree obj, bool mutable_p) | |
5556 | { | |
5557 | /* If this is initialization, there's no problem. */ | |
5558 | if (code != MODIFY_EXPR) | |
5559 | return false; | |
5560 | ||
5561 | /* [basic.type.qualifier] "A const object is an object of type | |
5562 | const T or a non-mutable subobject of a const object." */ | |
5563 | if (mutable_p) | |
5564 | return false; | |
5565 | ||
7eb5be6a MP |
5566 | if (TREE_READONLY (obj)) |
5567 | return true; | |
5568 | ||
5569 | if (CP_TYPE_CONST_P (TREE_TYPE (obj))) | |
5570 | { | |
5571 | /* Although a COMPONENT_REF may have a const type, we should | |
5572 | only consider it modifying a const object when any of the | |
5573 | field components is const. This can happen when using | |
5574 | constructs such as const_cast<const T &>(m), making something | |
5575 | const even though it wasn't declared const. */ | |
5576 | if (TREE_CODE (obj) == COMPONENT_REF) | |
5577 | return cref_has_const_field (obj); | |
5578 | else | |
5579 | return true; | |
5580 | } | |
5581 | ||
5582 | return false; | |
04e1749c MP |
5583 | } |
5584 | ||
60813a46 JM |
5585 | /* Evaluate an INIT_EXPR or MODIFY_EXPR. */ |
5586 | ||
5587 | static tree | |
5588 | cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, | |
92a596e8 | 5589 | bool lval, |
60813a46 JM |
5590 | bool *non_constant_p, bool *overflow_p) |
5591 | { | |
5592 | constexpr_ctx new_ctx = *ctx; | |
5593 | ||
58cc255c JM |
5594 | tree init = TREE_OPERAND (t, 1); |
5595 | if (TREE_CLOBBER_P (init)) | |
5596 | /* Just ignore clobbers. */ | |
5597 | return void_node; | |
5598 | ||
60813a46 JM |
5599 | /* First we figure out where we're storing to. */ |
5600 | tree target = TREE_OPERAND (t, 0); | |
68d01920 JM |
5601 | |
5602 | maybe_simplify_trivial_copy (target, init); | |
5603 | ||
3f8e2835 | 5604 | tree type = TREE_TYPE (target); |
e8b3c1bc JM |
5605 | bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR; |
5606 | if (preeval) | |
5607 | { | |
5608 | /* Evaluate the value to be stored without knowing what object it will be | |
5609 | stored in, so that any side-effects happen first. */ | |
5610 | if (!SCALAR_TYPE_P (type)) | |
5611 | new_ctx.ctor = new_ctx.object = NULL_TREE; | |
5612 | init = cxx_eval_constant_expression (&new_ctx, init, false, | |
5613 | non_constant_p, overflow_p); | |
5614 | if (*non_constant_p) | |
5615 | return t; | |
5616 | } | |
60813a46 | 5617 | |
d0aa42d2 JM |
5618 | bool evaluated = false; |
5619 | if (lval) | |
3f8e2835 | 5620 | { |
d0aa42d2 JM |
5621 | /* If we want to return a reference to the target, we need to evaluate it |
5622 | as a whole; otherwise, only evaluate the innermost piece to avoid | |
5623 | building up unnecessary *_REFs. */ | |
5624 | target = cxx_eval_constant_expression (ctx, target, true, | |
5625 | non_constant_p, overflow_p); | |
5626 | evaluated = true; | |
5627 | if (*non_constant_p) | |
5628 | return t; | |
3f8e2835 JM |
5629 | } |
5630 | ||
043666e0 | 5631 | /* Find the underlying variable. */ |
cd9cf97b | 5632 | releasing_vec refs; |
60813a46 | 5633 | tree object = NULL_TREE; |
04e1749c MP |
5634 | /* If we're modifying a const object, save it. */ |
5635 | tree const_object_being_modified = NULL_TREE; | |
5636 | bool mutable_p = false; | |
60813a46 JM |
5637 | for (tree probe = target; object == NULL_TREE; ) |
5638 | { | |
5639 | switch (TREE_CODE (probe)) | |
5640 | { | |
5641 | case BIT_FIELD_REF: | |
5642 | case COMPONENT_REF: | |
5643 | case ARRAY_REF: | |
043666e0 JM |
5644 | { |
5645 | tree ob = TREE_OPERAND (probe, 0); | |
5646 | tree elt = TREE_OPERAND (probe, 1); | |
7d349dd8 | 5647 | if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt)) |
04e1749c | 5648 | mutable_p = true; |
043666e0 JM |
5649 | if (TREE_CODE (probe) == ARRAY_REF) |
5650 | { | |
5651 | elt = eval_and_check_array_index (ctx, probe, false, | |
5652 | non_constant_p, overflow_p); | |
5653 | if (*non_constant_p) | |
5654 | return t; | |
5655 | } | |
0f184800 MP |
5656 | /* We don't check modifying_const_object_p for ARRAY_REFs. Given |
5657 | "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though | |
5658 | the array isn't const. Instead, check "a" in the next iteration; | |
5659 | that will detect modifying "const int a[10]". */ | |
5660 | else if (evaluated | |
5661 | && modifying_const_object_p (TREE_CODE (t), probe, | |
5662 | mutable_p) | |
5663 | && const_object_being_modified == NULL_TREE) | |
5664 | const_object_being_modified = probe; | |
043666e0 JM |
5665 | vec_safe_push (refs, elt); |
5666 | vec_safe_push (refs, TREE_TYPE (probe)); | |
5667 | probe = ob; | |
5668 | } | |
60813a46 JM |
5669 | break; |
5670 | ||
5671 | default: | |
d0aa42d2 JM |
5672 | if (evaluated) |
5673 | object = probe; | |
5674 | else | |
5675 | { | |
5676 | probe = cxx_eval_constant_expression (ctx, probe, true, | |
5677 | non_constant_p, overflow_p); | |
5678 | evaluated = true; | |
5679 | if (*non_constant_p) | |
5680 | return t; | |
5681 | } | |
5682 | break; | |
60813a46 JM |
5683 | } |
5684 | } | |
5685 | ||
04e1749c MP |
5686 | if (modifying_const_object_p (TREE_CODE (t), object, mutable_p) |
5687 | && const_object_being_modified == NULL_TREE) | |
5688 | const_object_being_modified = object; | |
5689 | ||
60813a46 JM |
5690 | /* And then find/build up our initializer for the path to the subobject |
5691 | we're initializing. */ | |
a1408b7e | 5692 | tree *valp; |
077dd9b3 | 5693 | if (DECL_P (object)) |
8e007055 | 5694 | valp = ctx->global->values.get (object); |
a1408b7e JM |
5695 | else |
5696 | valp = NULL; | |
60813a46 JM |
5697 | if (!valp) |
5698 | { | |
5699 | /* A constant-expression cannot modify objects from outside the | |
5700 | constant-expression. */ | |
2b3ab879 | 5701 | if (!ctx->quiet) |
64d6d399 | 5702 | error ("modification of %qE is not a constant expression", object); |
60813a46 JM |
5703 | *non_constant_p = true; |
5704 | return t; | |
5705 | } | |
3f8e2835 | 5706 | type = TREE_TYPE (object); |
c75ce530 | 5707 | bool no_zero_init = true; |
2d63bc39 | 5708 | |
37244b21 PP |
5709 | releasing_vec ctors, indexes; |
5710 | auto_vec<int> index_pos_hints; | |
5711 | bool activated_union_member_p = false; | |
276a52d5 | 5712 | while (!refs->is_empty ()) |
60813a46 JM |
5713 | { |
5714 | if (*valp == NULL_TREE) | |
5715 | { | |
5716 | *valp = build_constructor (type, NULL); | |
e8c48716 | 5717 | CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; |
60813a46 | 5718 | } |
d6b46fca NS |
5719 | else if (TREE_CODE (*valp) == STRING_CST) |
5720 | { | |
5721 | /* An array was initialized with a string constant, and now | |
5722 | we're writing into one of its elements. Explode the | |
5723 | single initialization into a set of element | |
5724 | initializations. */ | |
5725 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); | |
5726 | ||
5727 | tree string = *valp; | |
5728 | tree elt_type = TREE_TYPE (type); | |
5729 | unsigned chars_per_elt = (TYPE_PRECISION (elt_type) | |
5730 | / TYPE_PRECISION (char_type_node)); | |
5731 | unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt; | |
5732 | tree ary_ctor = build_constructor (type, NULL); | |
5733 | ||
5734 | vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts); | |
5735 | for (unsigned ix = 0; ix != num_elts; ix++) | |
5736 | { | |
5737 | constructor_elt elt = | |
5738 | { | |
5739 | build_int_cst (size_type_node, ix), | |
5740 | extract_string_elt (string, chars_per_elt, ix) | |
5741 | }; | |
5742 | CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt); | |
5743 | } | |
94ff4c9d | 5744 | |
d6b46fca NS |
5745 | *valp = ary_ctor; |
5746 | } | |
5747 | ||
c75ce530 JM |
5748 | /* If the value of object is already zero-initialized, any new ctors for |
5749 | subobjects will also be zero-initialized. */ | |
e8c48716 | 5750 | no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp); |
60813a46 | 5751 | |
ceaaf873 | 5752 | enum tree_code code = TREE_CODE (type); |
60813a46 | 5753 | type = refs->pop(); |
ceaaf873 | 5754 | tree index = refs->pop(); |
60813a46 | 5755 | |
a8dd2b3e | 5756 | if (code == RECORD_TYPE && is_empty_field (index)) |
a4dfd0f0 JM |
5757 | /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they |
5758 | have no data and might have an offset lower than previously declared | |
5759 | fields, which confuses the middle-end. The code below will notice | |
5760 | that we don't have a CONSTRUCTOR for our inner target and just | |
5761 | return init. */ | |
5762 | break; | |
94ff4c9d | 5763 | |
37244b21 PP |
5764 | if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp) |
5765 | && CONSTRUCTOR_ELT (*valp, 0)->index != index) | |
ceaaf873 | 5766 | { |
b04445d4 | 5767 | if (cxx_dialect < cxx20) |
ed4ec9ce | 5768 | { |
37244b21 PP |
5769 | if (!ctx->quiet) |
5770 | error_at (cp_expr_loc_or_input_loc (t), | |
5771 | "change of the active member of a union " | |
5772 | "from %qD to %qD", | |
5773 | CONSTRUCTOR_ELT (*valp, 0)->index, | |
5774 | index); | |
5775 | *non_constant_p = true; | |
ed4ec9ce | 5776 | } |
37244b21 PP |
5777 | else if (TREE_CODE (t) == MODIFY_EXPR |
5778 | && CONSTRUCTOR_NO_CLEARING (*valp)) | |
ceaaf873 | 5779 | { |
37244b21 PP |
5780 | /* Diagnose changing the active union member while the union |
5781 | is in the process of being initialized. */ | |
5782 | if (!ctx->quiet) | |
5783 | error_at (cp_expr_loc_or_input_loc (t), | |
5784 | "change of the active member of a union " | |
5785 | "from %qD to %qD during initialization", | |
5786 | CONSTRUCTOR_ELT (*valp, 0)->index, | |
5787 | index); | |
5788 | *non_constant_p = true; | |
ceaaf873 | 5789 | } |
37244b21 PP |
5790 | no_zero_init = true; |
5791 | } | |
88504f34 | 5792 | |
37244b21 PP |
5793 | vec_safe_push (ctors, *valp); |
5794 | vec_safe_push (indexes, index); | |
88504f34 | 5795 | |
37244b21 | 5796 | constructor_elt *cep |
077dd9b3 | 5797 | = get_or_insert_ctor_field (*valp, index); |
37244b21 PP |
5798 | index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin()); |
5799 | ||
5800 | if (code == UNION_TYPE) | |
5801 | activated_union_member_p = true; | |
b599bf9d | 5802 | |
60813a46 JM |
5803 | valp = &cep->value; |
5804 | } | |
60813a46 | 5805 | |
04e1749c MP |
5806 | /* Detect modifying a constant object in constexpr evaluation. |
5807 | We have found a const object that is being modified. Figure out | |
5808 | if we need to issue an error. Consider | |
5809 | ||
5810 | struct A { | |
5811 | int n; | |
5812 | constexpr A() : n(1) { n = 2; } // #1 | |
5813 | }; | |
5814 | struct B { | |
5815 | const A a; | |
5816 | constexpr B() { a.n = 3; } // #2 | |
5817 | }; | |
5818 | constexpr B b{}; | |
5819 | ||
5820 | #1 is OK, since we're modifying an object under construction, but | |
5821 | #2 is wrong, since "a" is const and has been fully constructed. | |
5822 | To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR | |
5823 | which means that the object is read-only. For the example above, the | |
5824 | *ctors stack at the point of #2 will look like: | |
5825 | ||
5826 | ctors[0] = {.a={.n=2}} TREE_READONLY = 0 | |
5827 | ctors[1] = {.n=2} TREE_READONLY = 1 | |
5828 | ||
5829 | and we're modifying "b.a", so we search the stack and see if the | |
5830 | constructor for "b.a" has already run. */ | |
5831 | if (const_object_being_modified) | |
5832 | { | |
5833 | bool fail = false; | |
276a52d5 JJ |
5834 | tree const_objtype |
5835 | = strip_array_types (TREE_TYPE (const_object_being_modified)); | |
5836 | if (!CLASS_TYPE_P (const_objtype)) | |
04e1749c MP |
5837 | fail = true; |
5838 | else | |
5839 | { | |
5840 | /* [class.ctor]p5 "A constructor can be invoked for a const, | |
5841 | volatile, or const volatile object. const and volatile | |
5842 | semantics are not applied on an object under construction. | |
5843 | They come into effect when the constructor for the most | |
5844 | derived object ends." */ | |
3f207ab3 | 5845 | for (tree elt : *ctors) |
04e1749c MP |
5846 | if (same_type_ignoring_top_level_qualifiers_p |
5847 | (TREE_TYPE (const_object_being_modified), TREE_TYPE (elt))) | |
5848 | { | |
5849 | fail = TREE_READONLY (elt); | |
5850 | break; | |
5851 | } | |
5852 | } | |
5853 | if (fail) | |
5854 | { | |
5855 | if (!ctx->quiet) | |
5856 | modifying_const_object_error (t, const_object_being_modified); | |
5857 | *non_constant_p = true; | |
5858 | return t; | |
5859 | } | |
5860 | } | |
5861 | ||
e8b3c1bc | 5862 | if (!preeval) |
60813a46 | 5863 | { |
6c59b8a9 JM |
5864 | /* We're handling an INIT_EXPR of class type, so the value of the |
5865 | initializer can depend on the object it's initializing. */ | |
5866 | ||
60813a46 JM |
5867 | /* Create a new CONSTRUCTOR in case evaluation of the initializer |
5868 | wants to modify it. */ | |
acb2970c | 5869 | if (*valp == NULL_TREE) |
73a84269 | 5870 | { |
664beaf2 | 5871 | *valp = build_constructor (type, NULL); |
e8c48716 | 5872 | CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; |
73a84269 | 5873 | } |
664beaf2 | 5874 | new_ctx.ctor = *valp; |
60813a46 | 5875 | new_ctx.object = target; |
10d2f801 JM |
5876 | /* Avoid temporary materialization when initializing from a TARGET_EXPR. |
5877 | We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because | |
5878 | expansion of those trees uses ctx instead. */ | |
5879 | if (TREE_CODE (init) == TARGET_EXPR) | |
5880 | if (tree tinit = TARGET_EXPR_INITIAL (init)) | |
5881 | init = tinit; | |
e8b3c1bc JM |
5882 | init = cxx_eval_constant_expression (&new_ctx, init, false, |
5883 | non_constant_p, overflow_p); | |
37244b21 PP |
5884 | /* The hash table might have moved since the get earlier, and the |
5885 | initializer might have mutated the underlying CONSTRUCTORs, so we must | |
5886 | recompute VALP. */ | |
5887 | valp = ctx->global->values.get (object); | |
5888 | for (unsigned i = 0; i < vec_safe_length (indexes); i++) | |
5889 | { | |
5890 | constructor_elt *cep | |
5891 | = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]); | |
5892 | valp = &cep->value; | |
5893 | } | |
60813a46 JM |
5894 | } |
5895 | ||
7574c916 | 5896 | /* Don't share a CONSTRUCTOR that might be changed later. */ |
0146e25f | 5897 | init = unshare_constructor (init); |
d96e8407 | 5898 | |
e8b3c1bc JM |
5899 | if (*valp && TREE_CODE (*valp) == CONSTRUCTOR |
5900 | && TREE_CODE (init) == CONSTRUCTOR) | |
acb2970c | 5901 | { |
d96e8407 JM |
5902 | /* An outer ctx->ctor might be pointing to *valp, so replace |
5903 | its contents. */ | |
d0aa42d2 JM |
5904 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), |
5905 | TREE_TYPE (*valp))) | |
5906 | { | |
5907 | /* For initialization of an empty base, the original target will be | |
5908 | *(base*)this, evaluation of which resolves to the object | |
5909 | argument, which has the derived type rather than the base type. In | |
5910 | this situation, just evaluate the initializer and return, since | |
5911 | there's no actual data to store. */ | |
21fd62e5 PP |
5912 | gcc_assert (is_empty_class (TREE_TYPE (init))); |
5913 | return lval ? target : init; | |
d0aa42d2 | 5914 | } |
d96e8407 JM |
5915 | CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init); |
5916 | TREE_CONSTANT (*valp) = TREE_CONSTANT (init); | |
5917 | TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init); | |
e8c48716 JM |
5918 | CONSTRUCTOR_NO_CLEARING (*valp) |
5919 | = CONSTRUCTOR_NO_CLEARING (init); | |
acb2970c | 5920 | } |
35e8b38a JJ |
5921 | else if (TREE_CODE (init) == CONSTRUCTOR |
5922 | && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), | |
5923 | type)) | |
5924 | { | |
5925 | /* See above on initialization of empty bases. */ | |
5926 | gcc_assert (is_empty_class (TREE_TYPE (init)) && !lval); | |
5927 | return init; | |
5928 | } | |
60813a46 | 5929 | else |
d96e8407 | 5930 | *valp = init; |
2d63bc39 | 5931 | |
7eb5be6a MP |
5932 | /* After initialization, 'const' semantics apply to the value of the |
5933 | object. Make a note of this fact by marking the CONSTRUCTOR | |
5934 | TREE_READONLY. */ | |
5935 | if (TREE_CODE (t) == INIT_EXPR | |
5936 | && TREE_CODE (*valp) == CONSTRUCTOR | |
5937 | && TYPE_READONLY (type)) | |
64da1b76 PP |
5938 | { |
5939 | if (INDIRECT_REF_P (target) | |
5940 | && (is_this_parameter | |
5941 | (tree_strip_nop_conversions (TREE_OPERAND (target, 0))))) | |
5942 | /* We've just initialized '*this' (perhaps via the target | |
5943 | constructor of a delegating constructor). Leave it up to the | |
5944 | caller that set 'this' to set TREE_READONLY appropriately. */ | |
5945 | gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p | |
5946 | (TREE_TYPE (target), type)); | |
5947 | else | |
5948 | TREE_READONLY (*valp) = true; | |
5949 | } | |
7eb5be6a | 5950 | |
d96e8407 JM |
5951 | /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing |
5952 | CONSTRUCTORs, if any. */ | |
d96e8407 JM |
5953 | bool c = TREE_CONSTANT (init); |
5954 | bool s = TREE_SIDE_EFFECTS (init); | |
37244b21 | 5955 | if (!c || s || activated_union_member_p) |
3f207ab3 | 5956 | for (tree elt : *ctors) |
d96e8407 JM |
5957 | { |
5958 | if (!c) | |
5959 | TREE_CONSTANT (elt) = false; | |
5960 | if (s) | |
5961 | TREE_SIDE_EFFECTS (elt) = true; | |
b599bf9d PP |
5962 | /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of |
5963 | this union. */ | |
5964 | if (TREE_CODE (TREE_TYPE (elt)) == UNION_TYPE) | |
5965 | CONSTRUCTOR_NO_CLEARING (elt) = false; | |
d96e8407 | 5966 | } |
60813a46 JM |
5967 | |
5968 | if (*non_constant_p) | |
5969 | return t; | |
92a596e8 | 5970 | else if (lval) |
60813a46 JM |
5971 | return target; |
5972 | else | |
3bdf0a9b | 5973 | return init; |
60813a46 JM |
5974 | } |
5975 | ||
5976 | /* Evaluate a ++ or -- expression. */ | |
5977 | ||
5978 | static tree | |
5979 | cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t, | |
92a596e8 | 5980 | bool lval, |
60813a46 JM |
5981 | bool *non_constant_p, bool *overflow_p) |
5982 | { | |
5983 | enum tree_code code = TREE_CODE (t); | |
5984 | tree type = TREE_TYPE (t); | |
5985 | tree op = TREE_OPERAND (t, 0); | |
5986 | tree offset = TREE_OPERAND (t, 1); | |
5987 | gcc_assert (TREE_CONSTANT (offset)); | |
5988 | ||
d85569f6 MP |
5989 | /* OFFSET is constant, but perhaps not constant enough. We need to |
5990 | e.g. bash FLOAT_EXPRs to REAL_CSTs. */ | |
5991 | offset = fold_simple (offset); | |
5992 | ||
60813a46 | 5993 | /* The operand as an lvalue. */ |
2b3ab879 | 5994 | op = cxx_eval_constant_expression (ctx, op, true, |
5a804683 | 5995 | non_constant_p, overflow_p); |
60813a46 JM |
5996 | |
5997 | /* The operand as an rvalue. */ | |
84dd815f JM |
5998 | tree val |
5999 | = cxx_eval_constant_expression (ctx, op, false, | |
6000 | non_constant_p, overflow_p); | |
caee690e JM |
6001 | /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to |
6002 | a local array in a constexpr function. */ | |
71a93b08 | 6003 | bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val)); |
caee690e JM |
6004 | if (!ptr) |
6005 | VERIFY_CONSTANT (val); | |
60813a46 JM |
6006 | |
6007 | /* The modified value. */ | |
6008 | bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR); | |
ac2f8d26 | 6009 | tree mod; |
71a93b08 | 6010 | if (INDIRECT_TYPE_P (type)) |
ac2f8d26 JM |
6011 | { |
6012 | /* The middle end requires pointers to use POINTER_PLUS_EXPR. */ | |
6013 | offset = convert_to_ptrofftype (offset); | |
6014 | if (!inc) | |
6015 | offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset); | |
6016 | mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset); | |
6017 | } | |
6018 | else | |
6019 | mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset); | |
caee690e JM |
6020 | if (!ptr) |
6021 | VERIFY_CONSTANT (mod); | |
60813a46 JM |
6022 | |
6023 | /* Storing the modified value. */ | |
04e1749c MP |
6024 | tree store = build2_loc (cp_expr_loc_or_loc (t, input_location), |
6025 | MODIFY_EXPR, type, op, mod); | |
d1bba463 PP |
6026 | mod = cxx_eval_constant_expression (ctx, store, lval, |
6027 | non_constant_p, overflow_p); | |
ecdcd560 | 6028 | ggc_free (store); |
d1bba463 PP |
6029 | if (*non_constant_p) |
6030 | return t; | |
60813a46 JM |
6031 | |
6032 | /* And the value of the expression. */ | |
6033 | if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR) | |
d1bba463 PP |
6034 | /* Prefix ops are lvalues, but the caller might want an rvalue; |
6035 | lval has already been taken into account in the store above. */ | |
6036 | return mod; | |
60813a46 JM |
6037 | else |
6038 | /* Postfix ops are rvalues. */ | |
6039 | return val; | |
6040 | } | |
6041 | ||
56632b27 JM |
6042 | /* Predicates for the meaning of *jump_target. */ |
6043 | ||
6044 | static bool | |
6045 | returns (tree *jump_target) | |
6046 | { | |
6047 | return *jump_target | |
d49318d9 PC |
6048 | && (TREE_CODE (*jump_target) == RETURN_EXPR |
6049 | || (TREE_CODE (*jump_target) == LABEL_DECL | |
6050 | && LABEL_DECL_CDTOR (*jump_target))); | |
56632b27 JM |
6051 | } |
6052 | ||
6053 | static bool | |
6054 | breaks (tree *jump_target) | |
6055 | { | |
6056 | return *jump_target | |
06ec22b7 JM |
6057 | && ((TREE_CODE (*jump_target) == LABEL_DECL |
6058 | && LABEL_DECL_BREAK (*jump_target)) | |
3075affd | 6059 | || TREE_CODE (*jump_target) == BREAK_STMT |
06ec22b7 | 6060 | || TREE_CODE (*jump_target) == EXIT_EXPR); |
56632b27 JM |
6061 | } |
6062 | ||
6063 | static bool | |
6064 | continues (tree *jump_target) | |
6065 | { | |
6066 | return *jump_target | |
3075affd JM |
6067 | && ((TREE_CODE (*jump_target) == LABEL_DECL |
6068 | && LABEL_DECL_CONTINUE (*jump_target)) | |
6069 | || TREE_CODE (*jump_target) == CONTINUE_STMT); | |
6070 | ||
56632b27 JM |
6071 | } |
6072 | ||
6073 | static bool | |
6074 | switches (tree *jump_target) | |
6075 | { | |
6076 | return *jump_target | |
6077 | && TREE_CODE (*jump_target) == INTEGER_CST; | |
6078 | } | |
6079 | ||
6080 | /* Subroutine of cxx_eval_statement_list. Determine whether the statement | |
4b390698 JJ |
6081 | STMT matches *jump_target. If we're looking for a case label and we see |
6082 | the default label, note it in ctx->css_state. */ | |
56632b27 JM |
6083 | |
6084 | static bool | |
4b390698 | 6085 | label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt) |
56632b27 | 6086 | { |
56632b27 JM |
6087 | switch (TREE_CODE (*jump_target)) |
6088 | { | |
6089 | case LABEL_DECL: | |
6090 | if (TREE_CODE (stmt) == LABEL_EXPR | |
6091 | && LABEL_EXPR_LABEL (stmt) == *jump_target) | |
6092 | return true; | |
6093 | break; | |
6094 | ||
6095 | case INTEGER_CST: | |
6096 | if (TREE_CODE (stmt) == CASE_LABEL_EXPR) | |
6097 | { | |
4b390698 | 6098 | gcc_assert (ctx->css_state != NULL); |
56632b27 | 6099 | if (!CASE_LOW (stmt)) |
4b390698 JJ |
6100 | { |
6101 | /* default: should appear just once in a SWITCH_EXPR | |
6102 | body (excluding nested SWITCH_EXPR). */ | |
6103 | gcc_assert (*ctx->css_state != css_default_seen); | |
6104 | /* When evaluating SWITCH_EXPR body for the second time, | |
6105 | return true for the default: label. */ | |
6106 | if (*ctx->css_state == css_default_processing) | |
6107 | return true; | |
6108 | *ctx->css_state = css_default_seen; | |
6109 | } | |
385ed708 JJ |
6110 | else if (CASE_HIGH (stmt)) |
6111 | { | |
6112 | if (tree_int_cst_le (CASE_LOW (stmt), *jump_target) | |
6113 | && tree_int_cst_le (*jump_target, CASE_HIGH (stmt))) | |
6114 | return true; | |
6115 | } | |
56632b27 JM |
6116 | else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt))) |
6117 | return true; | |
6118 | } | |
6119 | break; | |
6120 | ||
43574e4f JJ |
6121 | case BREAK_STMT: |
6122 | case CONTINUE_STMT: | |
6123 | /* These two are handled directly in cxx_eval_loop_expr by testing | |
6124 | breaks (jump_target) or continues (jump_target). */ | |
6125 | break; | |
6126 | ||
56632b27 JM |
6127 | default: |
6128 | gcc_unreachable (); | |
6129 | } | |
6130 | return false; | |
6131 | } | |
6132 | ||
6133 | /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump | |
6134 | semantics, for switch, break, continue, and return. */ | |
6135 | ||
6136 | static tree | |
6137 | cxx_eval_statement_list (const constexpr_ctx *ctx, tree t, | |
56632b27 JM |
6138 | bool *non_constant_p, bool *overflow_p, |
6139 | tree *jump_target) | |
6140 | { | |
58611fb6 | 6141 | tree local_target; |
345edb37 JJ |
6142 | /* In a statement-expression we want to return the last value. |
6143 | For empty statement expression return void_node. */ | |
6144 | tree r = void_node; | |
58611fb6 JM |
6145 | if (!jump_target) |
6146 | { | |
6147 | local_target = NULL_TREE; | |
6148 | jump_target = &local_target; | |
6149 | } | |
0f54cc9c | 6150 | for (tree stmt : tsi_range (t)) |
56632b27 | 6151 | { |
0250ba58 MP |
6152 | /* We've found a continue, so skip everything until we reach |
6153 | the label its jumping to. */ | |
6154 | if (continues (jump_target)) | |
6155 | { | |
6156 | if (label_matches (ctx, jump_target, stmt)) | |
6157 | /* Found it. */ | |
6158 | *jump_target = NULL_TREE; | |
6159 | else | |
6160 | continue; | |
6161 | } | |
6ef72c36 JJ |
6162 | if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT) |
6163 | continue; | |
58611fb6 JM |
6164 | r = cxx_eval_constant_expression (ctx, stmt, false, |
6165 | non_constant_p, overflow_p, | |
6166 | jump_target); | |
56632b27 JM |
6167 | if (*non_constant_p) |
6168 | break; | |
6169 | if (returns (jump_target) || breaks (jump_target)) | |
6170 | break; | |
6171 | } | |
ce36ba09 JM |
6172 | if (*jump_target && jump_target == &local_target) |
6173 | { | |
6174 | /* We aren't communicating the jump to our caller, so give up. We don't | |
6175 | need to support evaluation of jumps out of statement-exprs. */ | |
6176 | if (!ctx->quiet) | |
f9d0ca40 | 6177 | error_at (cp_expr_loc_or_input_loc (r), |
ce36ba09 JM |
6178 | "statement is not a constant expression"); |
6179 | *non_constant_p = true; | |
6180 | } | |
58611fb6 | 6181 | return r; |
56632b27 JM |
6182 | } |
6183 | ||
6184 | /* Evaluate a LOOP_EXPR for side-effects. Handles break and return | |
6185 | semantics; continue semantics are covered by cxx_eval_statement_list. */ | |
6186 | ||
6187 | static tree | |
6188 | cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t, | |
56632b27 JM |
6189 | bool *non_constant_p, bool *overflow_p, |
6190 | tree *jump_target) | |
6191 | { | |
39dce2b7 | 6192 | constexpr_ctx new_ctx = *ctx; |
8e007055 JJ |
6193 | tree local_target; |
6194 | if (!jump_target) | |
6195 | { | |
6196 | local_target = NULL_TREE; | |
6197 | jump_target = &local_target; | |
6198 | } | |
39dce2b7 | 6199 | |
43574e4f | 6200 | tree body, cond = NULL_TREE, expr = NULL_TREE; |
5ec2cd9f | 6201 | int count = 0; |
43574e4f JJ |
6202 | switch (TREE_CODE (t)) |
6203 | { | |
6204 | case LOOP_EXPR: | |
6205 | body = LOOP_EXPR_BODY (t); | |
6206 | break; | |
6207 | case DO_STMT: | |
6208 | body = DO_BODY (t); | |
6209 | cond = DO_COND (t); | |
6210 | break; | |
6211 | case WHILE_STMT: | |
6212 | body = WHILE_BODY (t); | |
6213 | cond = WHILE_COND (t); | |
6214 | count = -1; | |
6215 | break; | |
6216 | case FOR_STMT: | |
6217 | if (FOR_INIT_STMT (t)) | |
6218 | cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), /*lval*/false, | |
6219 | non_constant_p, overflow_p, jump_target); | |
6220 | if (*non_constant_p) | |
6221 | return NULL_TREE; | |
6222 | body = FOR_BODY (t); | |
6223 | cond = FOR_COND (t); | |
6224 | expr = FOR_EXPR (t); | |
6225 | count = -1; | |
6226 | break; | |
6227 | default: | |
6228 | gcc_unreachable (); | |
6229 | } | |
0ee28590 | 6230 | auto_vec<tree, 10> save_exprs; |
43574e4f | 6231 | new_ctx.save_exprs = &save_exprs; |
d259b234 | 6232 | do |
56632b27 | 6233 | { |
43574e4f JJ |
6234 | if (count != -1) |
6235 | { | |
6236 | if (body) | |
6237 | cxx_eval_constant_expression (&new_ctx, body, /*lval*/false, | |
6238 | non_constant_p, overflow_p, | |
6239 | jump_target); | |
6240 | if (breaks (jump_target)) | |
6241 | { | |
6242 | *jump_target = NULL_TREE; | |
6243 | break; | |
6244 | } | |
39dce2b7 | 6245 | |
43574e4f JJ |
6246 | if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target)) |
6247 | *jump_target = NULL_TREE; | |
6248 | ||
6249 | if (expr) | |
6250 | cxx_eval_constant_expression (&new_ctx, expr, /*lval*/false, | |
6251 | non_constant_p, overflow_p, | |
6252 | jump_target); | |
6253 | } | |
6254 | ||
6255 | if (cond) | |
6256 | { | |
6257 | tree res | |
6258 | = cxx_eval_constant_expression (&new_ctx, cond, /*lval*/false, | |
6259 | non_constant_p, overflow_p, | |
6260 | jump_target); | |
6261 | if (res) | |
6262 | { | |
6263 | if (verify_constant (res, ctx->quiet, non_constant_p, | |
6264 | overflow_p)) | |
6265 | break; | |
6266 | if (integer_zerop (res)) | |
6267 | break; | |
6268 | } | |
6269 | else | |
6270 | gcc_assert (*jump_target); | |
6271 | } | |
39dce2b7 | 6272 | |
ee1de08d | 6273 | /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */ |
3f207ab3 | 6274 | for (tree save_expr : save_exprs) |
8e007055 | 6275 | ctx->global->values.remove (save_expr); |
0ee28590 | 6276 | save_exprs.truncate (0); |
43574e4f | 6277 | |
5ec2cd9f JM |
6278 | if (++count >= constexpr_loop_limit) |
6279 | { | |
6280 | if (!ctx->quiet) | |
f9d0ca40 | 6281 | error_at (cp_expr_loc_or_input_loc (t), |
84fa214d | 6282 | "%<constexpr%> loop iteration count exceeds limit of %d " |
a3f9f006 | 6283 | "(use %<-fconstexpr-loop-limit=%> to increase the limit)", |
5ec2cd9f JM |
6284 | constexpr_loop_limit); |
6285 | *non_constant_p = true; | |
6286 | break; | |
6287 | } | |
56632b27 | 6288 | } |
4b390698 JJ |
6289 | while (!returns (jump_target) |
6290 | && !breaks (jump_target) | |
43574e4f JJ |
6291 | && !continues (jump_target) |
6292 | && (!switches (jump_target) || count == 0) | |
4b390698 | 6293 | && !*non_constant_p); |
d259b234 | 6294 | |
ee1de08d | 6295 | /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */ |
3f207ab3 | 6296 | for (tree save_expr : save_exprs) |
8e007055 | 6297 | ctx->global->values.remove (save_expr); |
39dce2b7 | 6298 | |
56632b27 JM |
6299 | return NULL_TREE; |
6300 | } | |
6301 | ||
6302 | /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump | |
6303 | semantics. */ | |
6304 | ||
6305 | static tree | |
6306 | cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t, | |
56632b27 JM |
6307 | bool *non_constant_p, bool *overflow_p, |
6308 | tree *jump_target) | |
6309 | { | |
43574e4f JJ |
6310 | tree cond |
6311 | = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t); | |
2b3ab879 | 6312 | cond = cxx_eval_constant_expression (ctx, cond, false, |
5a804683 | 6313 | non_constant_p, overflow_p); |
56632b27 JM |
6314 | VERIFY_CONSTANT (cond); |
6315 | *jump_target = cond; | |
6316 | ||
43574e4f JJ |
6317 | tree body |
6318 | = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t); | |
4b390698 JJ |
6319 | constexpr_ctx new_ctx = *ctx; |
6320 | constexpr_switch_state css = css_default_not_seen; | |
6321 | new_ctx.css_state = &css; | |
6322 | cxx_eval_constant_expression (&new_ctx, body, false, | |
6323 | non_constant_p, overflow_p, jump_target); | |
6324 | if (switches (jump_target) && css == css_default_seen) | |
6325 | { | |
6326 | /* If the SWITCH_EXPR body has default: label, process it once again, | |
6327 | this time instructing label_matches to return true for default: | |
6328 | label on switches (jump_target). */ | |
6329 | css = css_default_processing; | |
6330 | cxx_eval_constant_expression (&new_ctx, body, false, | |
6331 | non_constant_p, overflow_p, jump_target); | |
6332 | } | |
56632b27 JM |
6333 | if (breaks (jump_target) || switches (jump_target)) |
6334 | *jump_target = NULL_TREE; | |
6335 | return NULL_TREE; | |
6336 | } | |
6337 | ||
89262ec6 JM |
6338 | /* Find the object of TYPE under initialization in CTX. */ |
6339 | ||
6340 | static tree | |
6341 | lookup_placeholder (const constexpr_ctx *ctx, bool lval, tree type) | |
6342 | { | |
fbd603c4 | 6343 | if (!ctx) |
89262ec6 JM |
6344 | return NULL_TREE; |
6345 | ||
49a86fce PP |
6346 | /* Prefer the outermost matching object, but don't cross |
6347 | CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */ | |
6348 | if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor)) | |
6349 | if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type)) | |
6350 | return outer_ob; | |
6351 | ||
89262ec6 JM |
6352 | /* We could use ctx->object unconditionally, but using ctx->ctor when we |
6353 | can is a minor optimization. */ | |
fbd603c4 | 6354 | if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type)) |
89262ec6 JM |
6355 | return ctx->ctor; |
6356 | ||
fbd603c4 JM |
6357 | if (!ctx->object) |
6358 | return NULL_TREE; | |
6359 | ||
89262ec6 JM |
6360 | /* Since an object cannot have a field of its own type, we can search outward |
6361 | from ctx->object to find the unique containing object of TYPE. */ | |
6362 | tree ob = ctx->object; | |
6363 | while (ob) | |
6364 | { | |
6365 | if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type)) | |
6366 | break; | |
6367 | if (handled_component_p (ob)) | |
6368 | ob = TREE_OPERAND (ob, 0); | |
6369 | else | |
6370 | ob = NULL_TREE; | |
6371 | } | |
6372 | ||
6373 | return ob; | |
6374 | } | |
6375 | ||
7c814975 MP |
6376 | /* Complain about an attempt to evaluate inline assembly. */ |
6377 | ||
6378 | static void | |
6379 | inline_asm_in_constexpr_error (location_t loc) | |
6380 | { | |
6381 | auto_diagnostic_group d; | |
6382 | error_at (loc, "inline assembly is not a constant expression"); | |
6383 | inform (loc, "only unevaluated inline assembly is allowed in a " | |
b04445d4 | 6384 | "%<constexpr%> function in C++20"); |
7c814975 MP |
6385 | } |
6386 | ||
76b75018 JM |
6387 | /* We're getting the constant value of DECL in a manifestly constant-evaluated |
6388 | context; maybe complain about that. */ | |
6389 | ||
6390 | static void | |
6391 | maybe_warn_about_constant_value (location_t loc, tree decl) | |
6392 | { | |
6393 | static bool explained = false; | |
6394 | if (cxx_dialect >= cxx17 | |
6395 | && warn_interference_size | |
00f34291 | 6396 | && !OPTION_SET_P (param_destruct_interfere_size) |
76b75018 JM |
6397 | && DECL_CONTEXT (decl) == std_node |
6398 | && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size") | |
6399 | && (LOCATION_FILE (input_location) != main_input_filename | |
6400 | || module_exporting_p ()) | |
6401 | && warning_at (loc, OPT_Winterference_size, "use of %qD", decl) | |
6402 | && !explained) | |
6403 | { | |
6404 | explained = true; | |
6405 | inform (loc, "its value can vary between compiler versions or " | |
6406 | "with different %<-mtune%> or %<-mcpu%> flags"); | |
6407 | inform (loc, "if this use is part of a public ABI, change it to " | |
6408 | "instead use a constant variable you define"); | |
6409 | inform (loc, "the default value for the current CPU tuning " | |
6410 | "is %d bytes", param_destruct_interfere_size); | |
6411 | inform (loc, "you can stabilize this value with %<--param " | |
6412 | "hardware_destructive_interference_size=%d%>, or disable " | |
6413 | "this warning with %<-Wno-interference-size%>", | |
6414 | param_destruct_interfere_size); | |
6415 | } | |
6416 | } | |
6417 | ||
2d76680f PC |
6418 | /* Attempt to reduce the expression T to a constant value. |
6419 | On failure, issue diagnostic and return error_mark_node. */ | |
6420 | /* FIXME unify with c_fully_fold */ | |
60813a46 | 6421 | /* FIXME overflow_p is too global */ |
2d76680f PC |
6422 | |
6423 | static tree | |
3e605b20 | 6424 | cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, |
92a596e8 | 6425 | bool lval, |
56632b27 | 6426 | bool *non_constant_p, bool *overflow_p, |
3075affd | 6427 | tree *jump_target /* = NULL */) |
2d76680f | 6428 | { |
4b390698 JJ |
6429 | if (jump_target && *jump_target) |
6430 | { | |
6431 | /* If we are jumping, ignore all statements/expressions except those | |
6432 | that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */ | |
6433 | switch (TREE_CODE (t)) | |
6434 | { | |
6435 | case BIND_EXPR: | |
6436 | case STATEMENT_LIST: | |
6437 | case LOOP_EXPR: | |
6438 | case COND_EXPR: | |
43574e4f JJ |
6439 | case IF_STMT: |
6440 | case DO_STMT: | |
6441 | case WHILE_STMT: | |
6442 | case FOR_STMT: | |
4b390698 JJ |
6443 | break; |
6444 | case LABEL_EXPR: | |
6445 | case CASE_LABEL_EXPR: | |
6446 | if (label_matches (ctx, jump_target, t)) | |
6447 | /* Found it. */ | |
6448 | *jump_target = NULL_TREE; | |
6449 | return NULL_TREE; | |
6450 | default: | |
6451 | return NULL_TREE; | |
6452 | } | |
6453 | } | |
3075affd | 6454 | if (error_operand_p (t)) |
2d76680f PC |
6455 | { |
6456 | *non_constant_p = true; | |
6457 | return t; | |
6458 | } | |
7a649ef5 | 6459 | |
6821245b JM |
6460 | location_t loc = cp_expr_loc_or_input_loc (t); |
6461 | ||
7a649ef5 JM |
6462 | STRIP_ANY_LOCATION_WRAPPER (t); |
6463 | ||
2d76680f PC |
6464 | if (CONSTANT_CLASS_P (t)) |
6465 | { | |
61637db3 JJ |
6466 | if (TREE_OVERFLOW (t)) |
6467 | { | |
6468 | if (!ctx->quiet) | |
6469 | permerror (input_location, "overflow in constant expression"); | |
6470 | if (!flag_permissive || ctx->quiet) | |
6471 | *overflow_p = true; | |
6472 | } | |
8bada5cd MS |
6473 | |
6474 | if (TREE_CODE (t) == INTEGER_CST | |
9f613f06 | 6475 | && TYPE_PTR_P (TREE_TYPE (t)) |
f45610a4 JJ |
6476 | /* INTEGER_CST with pointer-to-method type is only used |
6477 | for a virtual method in a pointer to member function. | |
6478 | Don't reject those. */ | |
6479 | && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE | |
8bada5cd MS |
6480 | && !integer_zerop (t)) |
6481 | { | |
6482 | if (!ctx->quiet) | |
6483 | error ("value %qE of type %qT is not a constant expression", | |
6484 | t, TREE_TYPE (t)); | |
6485 | *non_constant_p = true; | |
6486 | } | |
6487 | ||
2d76680f PC |
6488 | return t; |
6489 | } | |
2d76680f | 6490 | |
a15ffa22 | 6491 | /* Avoid excessively long constexpr evaluations. */ |
8e007055 | 6492 | if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit) |
a15ffa22 JJ |
6493 | { |
6494 | if (!ctx->quiet) | |
6821245b | 6495 | error_at (loc, |
a15ffa22 | 6496 | "%<constexpr%> evaluation operation count exceeds limit of " |
a9c697b8 | 6497 | "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)", |
a15ffa22 | 6498 | constexpr_ops_limit); |
8e007055 | 6499 | ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT); |
a15ffa22 JJ |
6500 | *non_constant_p = true; |
6501 | return t; | |
6502 | } | |
6503 | ||
7a649ef5 JM |
6504 | constexpr_ctx new_ctx; |
6505 | tree r = t; | |
6506 | ||
8bada5cd MS |
6507 | tree_code tcode = TREE_CODE (t); |
6508 | switch (tcode) | |
2d76680f | 6509 | { |
60813a46 | 6510 | case RESULT_DECL: |
92a596e8 | 6511 | if (lval) |
60813a46 JM |
6512 | return t; |
6513 | /* We ask for an rvalue for the RESULT_DECL when indirecting | |
bf66b9b4 AH |
6514 | through an invisible reference, or in named return value |
6515 | optimization. */ | |
8e007055 | 6516 | if (tree *p = ctx->global->values.get (t)) |
1efb1dc2 MP |
6517 | return *p; |
6518 | else | |
6519 | { | |
6520 | if (!ctx->quiet) | |
6521 | error ("%qE is not a constant expression", t); | |
6522 | *non_constant_p = true; | |
6523 | } | |
6524 | break; | |
60813a46 | 6525 | |
2d76680f | 6526 | case VAR_DECL: |
70f40fea | 6527 | if (DECL_HAS_VALUE_EXPR_P (t)) |
c59fa7ea JM |
6528 | { |
6529 | if (is_normal_capture_proxy (t) | |
6530 | && current_function_decl == DECL_CONTEXT (t)) | |
6531 | { | |
6532 | /* Function parms aren't constexpr within the function | |
6533 | definition, so don't try to look at the closure. But if the | |
6534 | captured variable is constant, try to evaluate it directly. */ | |
6535 | r = DECL_CAPTURED_VARIABLE (t); | |
6536 | tree type = TREE_TYPE (t); | |
6537 | if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r))) | |
6538 | { | |
6539 | /* Adjust r to match the reference-ness of t. */ | |
6540 | if (TYPE_REF_P (type)) | |
6541 | r = build_address (r); | |
6542 | else | |
6543 | r = convert_from_reference (r); | |
6544 | } | |
6545 | } | |
6546 | else | |
6547 | r = DECL_VALUE_EXPR (t); | |
6548 | return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, | |
6549 | overflow_p); | |
6550 | } | |
191816a3 | 6551 | /* fall through */ |
7c83622c | 6552 | case CONST_DECL: |
e53b6e56 | 6553 | /* We used to not check lval for CONST_DECL, but darwin.cc uses |
7c83622c | 6554 | CONST_DECL for aggregate constants. */ |
92a596e8 | 6555 | if (lval) |
2d76680f | 6556 | return t; |
ef529765 JM |
6557 | else if (t == ctx->object) |
6558 | return ctx->ctor; | |
6559 | if (VAR_P (t)) | |
6560 | if (tree *p = ctx->global->values.get (t)) | |
6561 | if (*p != NULL_TREE) | |
6562 | { | |
6563 | r = *p; | |
6564 | break; | |
6565 | } | |
76b75018 JM |
6566 | if (ctx->manifestly_const_eval) |
6567 | maybe_warn_about_constant_value (loc, t); | |
f4fce183 | 6568 | if (COMPLETE_TYPE_P (TREE_TYPE (t)) |
dbcd32f8 | 6569 | && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
7dc2b4a2 JM |
6570 | { |
6571 | /* If the class is empty, we aren't actually loading anything. */ | |
6572 | r = build_constructor (TREE_TYPE (t), NULL); | |
6573 | TREE_CONSTANT (r) = true; | |
6574 | } | |
6575 | else if (ctx->strict) | |
8c00059c | 6576 | r = decl_really_constant_value (t, /*unshare_p=*/false); |
69eb4fde | 6577 | else |
8c00059c | 6578 | r = decl_constant_value (t, /*unshare_p=*/false); |
2d76680f PC |
6579 | if (TREE_CODE (r) == TARGET_EXPR |
6580 | && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR) | |
6581 | r = TARGET_EXPR_INITIAL (r); | |
2d76680f PC |
6582 | if (DECL_P (r)) |
6583 | { | |
2b3ab879 | 6584 | if (!ctx->quiet) |
6821245b | 6585 | non_const_var_error (loc, r); |
2d76680f PC |
6586 | *non_constant_p = true; |
6587 | } | |
6588 | break; | |
6589 | ||
96a95ac1 AO |
6590 | case DEBUG_BEGIN_STMT: |
6591 | /* ??? It might be nice to retain this information somehow, so | |
6592 | as to be able to step into a constexpr function call. */ | |
6593 | /* Fall through. */ | |
6594 | ||
2d76680f PC |
6595 | case FUNCTION_DECL: |
6596 | case TEMPLATE_DECL: | |
6597 | case LABEL_DECL: | |
56632b27 JM |
6598 | case LABEL_EXPR: |
6599 | case CASE_LABEL_EXPR: | |
4b390698 | 6600 | case PREDICT_EXPR: |
2d76680f PC |
6601 | return t; |
6602 | ||
6603 | case PARM_DECL: | |
9f613f06 | 6604 | if (lval && !TYPE_REF_P (TREE_TYPE (t))) |
60813a46 | 6605 | /* glvalue use. */; |
8e007055 | 6606 | else if (tree *p = ctx->global->values.get (r)) |
60813a46 | 6607 | r = *p; |
92a596e8 | 6608 | else if (lval) |
2d76680f | 6609 | /* Defer in case this is only used for its type. */; |
1c2f613f | 6610 | else if (COMPLETE_TYPE_P (TREE_TYPE (t)) |
dbcd32f8 | 6611 | && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
052beba4 JM |
6612 | { |
6613 | /* If the class is empty, we aren't actually loading anything. */ | |
6614 | r = build_constructor (TREE_TYPE (t), NULL); | |
6615 | TREE_CONSTANT (r) = true; | |
6616 | } | |
2d76680f PC |
6617 | else |
6618 | { | |
2b3ab879 | 6619 | if (!ctx->quiet) |
2d76680f PC |
6620 | error ("%qE is not a constant expression", t); |
6621 | *non_constant_p = true; | |
6622 | } | |
6623 | break; | |
6624 | ||
6625 | case CALL_EXPR: | |
6626 | case AGGR_INIT_EXPR: | |
92a596e8 | 6627 | r = cxx_eval_call_expression (ctx, t, lval, |
2d76680f PC |
6628 | non_constant_p, overflow_p); |
6629 | break; | |
6630 | ||
60813a46 JM |
6631 | case DECL_EXPR: |
6632 | { | |
6633 | r = DECL_EXPR_DECL (t); | |
43574e4f JJ |
6634 | if (TREE_CODE (r) == USING_DECL) |
6635 | { | |
6636 | r = void_node; | |
6637 | break; | |
6638 | } | |
8892d532 JJ |
6639 | |
6640 | if (VAR_P (r) | |
6641 | && (TREE_STATIC (r) || CP_DECL_THREAD_LOCAL_P (r)) | |
6642 | /* Allow __FUNCTION__ etc. */ | |
6643 | && !DECL_ARTIFICIAL (r)) | |
6644 | { | |
6645 | gcc_assert (cxx_dialect >= cxx23); | |
6646 | if (!ctx->quiet) | |
6647 | { | |
6648 | if (CP_DECL_THREAD_LOCAL_P (r)) | |
6649 | error_at (loc, "control passes through declaration of %qD " | |
6650 | "with thread storage duration", r); | |
6651 | else | |
6652 | error_at (loc, "control passes through declaration of %qD " | |
6653 | "with static storage duration", r); | |
6654 | } | |
6655 | *non_constant_p = true; | |
6656 | break; | |
6657 | } | |
6658 | ||
60813a46 JM |
6659 | if (AGGREGATE_TYPE_P (TREE_TYPE (r)) |
6660 | || VECTOR_TYPE_P (TREE_TYPE (r))) | |
6661 | { | |
6662 | new_ctx = *ctx; | |
6663 | new_ctx.object = r; | |
6664 | new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL); | |
e8c48716 | 6665 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; |
8e007055 | 6666 | ctx->global->values.put (r, new_ctx.ctor); |
60813a46 JM |
6667 | ctx = &new_ctx; |
6668 | } | |
6669 | ||
6670 | if (tree init = DECL_INITIAL (r)) | |
6671 | { | |
6672 | init = cxx_eval_constant_expression (ctx, init, | |
2b3ab879 | 6673 | false, |
5a804683 | 6674 | non_constant_p, overflow_p); |
7f0e23e9 | 6675 | /* Don't share a CONSTRUCTOR that might be changed. */ |
0146e25f | 6676 | init = unshare_constructor (init); |
04e1749c MP |
6677 | /* Remember that a constant object's constructor has already |
6678 | run. */ | |
6679 | if (CLASS_TYPE_P (TREE_TYPE (r)) | |
6680 | && CP_TYPE_CONST_P (TREE_TYPE (r))) | |
6681 | TREE_READONLY (init) = true; | |
8e007055 | 6682 | ctx->global->values.put (r, init); |
60813a46 JM |
6683 | } |
6684 | else if (ctx == &new_ctx) | |
6685 | /* We gave it a CONSTRUCTOR above. */; | |
6686 | else | |
8e007055 | 6687 | ctx->global->values.put (r, NULL_TREE); |
60813a46 JM |
6688 | } |
6689 | break; | |
6690 | ||
2d76680f | 6691 | case TARGET_EXPR: |
595f1b12 JM |
6692 | { |
6693 | tree type = TREE_TYPE (t); | |
6694 | ||
6695 | if (!literal_type_p (type)) | |
6696 | { | |
6697 | if (!ctx->quiet) | |
6698 | { | |
6699 | auto_diagnostic_group d; | |
6700 | error ("temporary of non-literal type %qT in a " | |
6701 | "constant expression", type); | |
6702 | explain_non_literal_class (type); | |
6703 | } | |
6704 | *non_constant_p = true; | |
6705 | break; | |
6706 | } | |
6707 | gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t)); | |
6708 | /* Avoid evaluating a TARGET_EXPR more than once. */ | |
6709 | tree slot = TARGET_EXPR_SLOT (t); | |
6710 | if (tree *p = ctx->global->values.get (slot)) | |
6711 | { | |
6712 | if (lval) | |
6713 | return slot; | |
6714 | r = *p; | |
6715 | break; | |
6716 | } | |
595f1b12 JM |
6717 | if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))) |
6718 | { | |
6719 | /* We're being expanded without an explicit target, so start | |
6720 | initializing a new object; expansion with an explicit target | |
6721 | strips the TARGET_EXPR before we get here. */ | |
6722 | new_ctx = *ctx; | |
49a86fce PP |
6723 | /* Link CTX to NEW_CTX so that lookup_placeholder can resolve |
6724 | any PLACEHOLDER_EXPR within the initializer that refers to the | |
6725 | former object under construction. */ | |
6726 | new_ctx.parent = ctx; | |
595f1b12 JM |
6727 | new_ctx.ctor = build_constructor (type, NULL); |
6728 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; | |
6729 | new_ctx.object = slot; | |
6730 | ctx->global->values.put (new_ctx.object, new_ctx.ctor); | |
6731 | ctx = &new_ctx; | |
6732 | } | |
6733 | /* Pass false for 'lval' because this indicates | |
6734 | initialization of a temporary. */ | |
6735 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), | |
6736 | false, | |
6737 | non_constant_p, overflow_p); | |
6738 | if (*non_constant_p) | |
ee1de08d | 6739 | break; |
595f1b12 JM |
6740 | /* Adjust the type of the result to the type of the temporary. */ |
6741 | r = adjust_temp_type (type, r); | |
6742 | if (TARGET_EXPR_CLEANUP (t) && !CLEANUP_EH_ONLY (t)) | |
6743 | ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t)); | |
6744 | r = unshare_constructor (r); | |
6745 | ctx->global->values.put (slot, r); | |
6746 | if (ctx->save_exprs) | |
6747 | ctx->save_exprs->safe_push (slot); | |
6748 | if (lval) | |
6749 | return slot; | |
6750 | } | |
2d76680f PC |
6751 | break; |
6752 | ||
60813a46 | 6753 | case INIT_EXPR: |
60813a46 | 6754 | case MODIFY_EXPR: |
4b390698 | 6755 | gcc_assert (jump_target == NULL || *jump_target == NULL_TREE); |
92a596e8 | 6756 | r = cxx_eval_store_expression (ctx, t, lval, |
60813a46 JM |
6757 | non_constant_p, overflow_p); |
6758 | break; | |
6759 | ||
2d76680f | 6760 | case SCOPE_REF: |
3e605b20 | 6761 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
92a596e8 | 6762 | lval, |
5a804683 | 6763 | non_constant_p, overflow_p); |
2d76680f PC |
6764 | break; |
6765 | ||
6766 | case RETURN_EXPR: | |
75e0295b MP |
6767 | if (TREE_OPERAND (t, 0) != NULL_TREE) |
6768 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), | |
6769 | lval, | |
6770 | non_constant_p, overflow_p); | |
43574e4f JJ |
6771 | /* FALLTHRU */ |
6772 | case BREAK_STMT: | |
6773 | case CONTINUE_STMT: | |
019e0ae8 MP |
6774 | if (jump_target) |
6775 | *jump_target = t; | |
6776 | else | |
6777 | { | |
6778 | /* Can happen with ({ return true; }) && false; passed to | |
6779 | maybe_constant_value. There is nothing to jump over in this | |
6780 | case, and the bug will be diagnosed later. */ | |
6781 | gcc_assert (ctx->quiet); | |
6782 | *non_constant_p = true; | |
6783 | } | |
56632b27 JM |
6784 | break; |
6785 | ||
cabaf50a JM |
6786 | case SAVE_EXPR: |
6787 | /* Avoid evaluating a SAVE_EXPR more than once. */ | |
8e007055 | 6788 | if (tree *p = ctx->global->values.get (t)) |
cabaf50a JM |
6789 | r = *p; |
6790 | else | |
6791 | { | |
12d9ce19 | 6792 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false, |
cabaf50a | 6793 | non_constant_p, overflow_p); |
0e0ffbfc JJ |
6794 | if (*non_constant_p) |
6795 | break; | |
8e007055 | 6796 | ctx->global->values.put (t, r); |
39dce2b7 | 6797 | if (ctx->save_exprs) |
0ee28590 | 6798 | ctx->save_exprs->safe_push (t); |
cabaf50a JM |
6799 | } |
6800 | break; | |
6801 | ||
2d76680f | 6802 | case TRY_CATCH_EXPR: |
ee1de08d JJ |
6803 | if (TREE_OPERAND (t, 0) == NULL_TREE) |
6804 | { | |
6805 | r = void_node; | |
6806 | break; | |
6807 | } | |
6808 | /* FALLTHRU */ | |
6809 | case NON_LVALUE_EXPR: | |
769430b2 | 6810 | case TRY_BLOCK: |
2d76680f | 6811 | case MUST_NOT_THROW_EXPR: |
60813a46 JM |
6812 | case EXPR_STMT: |
6813 | case EH_SPEC_BLOCK: | |
3e605b20 | 6814 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
92a596e8 | 6815 | lval, |
56632b27 JM |
6816 | non_constant_p, overflow_p, |
6817 | jump_target); | |
2d76680f PC |
6818 | break; |
6819 | ||
ee1de08d JJ |
6820 | case CLEANUP_POINT_EXPR: |
6821 | { | |
6822 | auto_vec<tree, 2> cleanups; | |
6823 | vec<tree> *prev_cleanups = ctx->global->cleanups; | |
6824 | ctx->global->cleanups = &cleanups; | |
6825 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), | |
6826 | lval, | |
6827 | non_constant_p, overflow_p, | |
6828 | jump_target); | |
6829 | ctx->global->cleanups = prev_cleanups; | |
6830 | unsigned int i; | |
6831 | tree cleanup; | |
6832 | /* Evaluate the cleanups. */ | |
6833 | FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup) | |
6834 | cxx_eval_constant_expression (ctx, cleanup, false, | |
fc531c2e | 6835 | non_constant_p, overflow_p); |
ee1de08d JJ |
6836 | } |
6837 | break; | |
6838 | ||
769430b2 JM |
6839 | case TRY_FINALLY_EXPR: |
6840 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, | |
6841 | non_constant_p, overflow_p, | |
6842 | jump_target); | |
6843 | if (!*non_constant_p) | |
6844 | /* Also evaluate the cleanup. */ | |
6845 | cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true, | |
fc531c2e | 6846 | non_constant_p, overflow_p); |
769430b2 JM |
6847 | break; |
6848 | ||
43574e4f | 6849 | case CLEANUP_STMT: |
fc531c2e JJ |
6850 | r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval, |
6851 | non_constant_p, overflow_p, | |
6852 | jump_target); | |
6853 | if (!CLEANUP_EH_ONLY (t) && !*non_constant_p) | |
6854 | { | |
6855 | iloc_sentinel ils (loc); | |
6856 | /* Also evaluate the cleanup. */ | |
6857 | cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), true, | |
6858 | non_constant_p, overflow_p); | |
6859 | } | |
43574e4f JJ |
6860 | break; |
6861 | ||
2d76680f PC |
6862 | /* These differ from cxx_eval_unary_expression in that this doesn't |
6863 | check for a constant operand or result; an address can be | |
6864 | constant without its operand being, and vice versa. */ | |
cda0a029 | 6865 | case MEM_REF: |
2d76680f | 6866 | case INDIRECT_REF: |
92a596e8 | 6867 | r = cxx_eval_indirect_ref (ctx, t, lval, |
2d76680f PC |
6868 | non_constant_p, overflow_p); |
6869 | break; | |
6870 | ||
6871 | case ADDR_EXPR: | |
6872 | { | |
6873 | tree oldop = TREE_OPERAND (t, 0); | |
3e605b20 | 6874 | tree op = cxx_eval_constant_expression (ctx, oldop, |
92a596e8 | 6875 | /*lval*/true, |
5a804683 | 6876 | non_constant_p, overflow_p); |
2d76680f PC |
6877 | /* Don't VERIFY_CONSTANT here. */ |
6878 | if (*non_constant_p) | |
6879 | return t; | |
f2acb8ad | 6880 | gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR); |
2d76680f PC |
6881 | /* This function does more aggressive folding than fold itself. */ |
6882 | r = build_fold_addr_expr_with_type (op, TREE_TYPE (t)); | |
6883 | if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop) | |
7a649ef5 JM |
6884 | { |
6885 | ggc_free (r); | |
6886 | return t; | |
6887 | } | |
2d76680f PC |
6888 | break; |
6889 | } | |
6890 | ||
6891 | case REALPART_EXPR: | |
6892 | case IMAGPART_EXPR: | |
87713c6a JJ |
6893 | if (lval) |
6894 | { | |
6895 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, | |
6896 | non_constant_p, overflow_p); | |
6897 | if (r == error_mark_node) | |
6898 | ; | |
6899 | else if (r == TREE_OPERAND (t, 0)) | |
6900 | r = t; | |
6901 | else | |
6902 | r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r); | |
6903 | break; | |
6904 | } | |
6905 | /* FALLTHRU */ | |
2d76680f PC |
6906 | case CONJ_EXPR: |
6907 | case FIX_TRUNC_EXPR: | |
6908 | case FLOAT_EXPR: | |
6909 | case NEGATE_EXPR: | |
6910 | case ABS_EXPR: | |
e28fadbc | 6911 | case ABSU_EXPR: |
2d76680f PC |
6912 | case BIT_NOT_EXPR: |
6913 | case TRUTH_NOT_EXPR: | |
6914 | case FIXED_CONVERT_EXPR: | |
92a596e8 | 6915 | r = cxx_eval_unary_expression (ctx, t, lval, |
2d76680f PC |
6916 | non_constant_p, overflow_p); |
6917 | break; | |
6918 | ||
6919 | case SIZEOF_EXPR: | |
99e764a2 MP |
6920 | r = fold_sizeof_expr (t); |
6921 | /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR, | |
6922 | which could lead to an infinite recursion. */ | |
6923 | if (TREE_CODE (r) != SIZEOF_EXPR) | |
6924 | r = cxx_eval_constant_expression (ctx, r, lval, | |
6925 | non_constant_p, overflow_p, | |
6926 | jump_target); | |
6927 | else | |
6928 | { | |
6929 | *non_constant_p = true; | |
6930 | gcc_assert (ctx->quiet); | |
6931 | } | |
6932 | ||
2d76680f PC |
6933 | break; |
6934 | ||
6935 | case COMPOUND_EXPR: | |
6936 | { | |
6937 | /* check_return_expr sometimes wraps a TARGET_EXPR in a | |
6938 | COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR | |
6939 | introduced by build_call_a. */ | |
6940 | tree op0 = TREE_OPERAND (t, 0); | |
6941 | tree op1 = TREE_OPERAND (t, 1); | |
6942 | STRIP_NOPS (op1); | |
6943 | if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) | |
6944 | || TREE_CODE (op1) == EMPTY_CLASS_EXPR) | |
2b3ab879 | 6945 | r = cxx_eval_constant_expression (ctx, op0, |
92a596e8 | 6946 | lval, non_constant_p, overflow_p, |
56632b27 | 6947 | jump_target); |
2d76680f PC |
6948 | else |
6949 | { | |
6950 | /* Check that the LHS is constant and then discard it. */ | |
2b3ab879 | 6951 | cxx_eval_constant_expression (ctx, op0, |
12d9ce19 | 6952 | true, non_constant_p, overflow_p, |
56632b27 | 6953 | jump_target); |
06ec22b7 JM |
6954 | if (*non_constant_p) |
6955 | return t; | |
2d76680f | 6956 | op1 = TREE_OPERAND (t, 1); |
2b3ab879 | 6957 | r = cxx_eval_constant_expression (ctx, op1, |
92a596e8 | 6958 | lval, non_constant_p, overflow_p, |
56632b27 | 6959 | jump_target); |
2d76680f PC |
6960 | } |
6961 | } | |
6962 | break; | |
6963 | ||
6964 | case POINTER_PLUS_EXPR: | |
1af4ebf5 | 6965 | case POINTER_DIFF_EXPR: |
2d76680f PC |
6966 | case PLUS_EXPR: |
6967 | case MINUS_EXPR: | |
6968 | case MULT_EXPR: | |
6969 | case TRUNC_DIV_EXPR: | |
6970 | case CEIL_DIV_EXPR: | |
6971 | case FLOOR_DIV_EXPR: | |
6972 | case ROUND_DIV_EXPR: | |
6973 | case TRUNC_MOD_EXPR: | |
6974 | case CEIL_MOD_EXPR: | |
6975 | case ROUND_MOD_EXPR: | |
6976 | case RDIV_EXPR: | |
6977 | case EXACT_DIV_EXPR: | |
6978 | case MIN_EXPR: | |
6979 | case MAX_EXPR: | |
6980 | case LSHIFT_EXPR: | |
6981 | case RSHIFT_EXPR: | |
81fa6d73 JJ |
6982 | case LROTATE_EXPR: |
6983 | case RROTATE_EXPR: | |
2d76680f PC |
6984 | case BIT_IOR_EXPR: |
6985 | case BIT_XOR_EXPR: | |
6986 | case BIT_AND_EXPR: | |
6987 | case TRUTH_XOR_EXPR: | |
6988 | case LT_EXPR: | |
6989 | case LE_EXPR: | |
6990 | case GT_EXPR: | |
6991 | case GE_EXPR: | |
6992 | case EQ_EXPR: | |
6993 | case NE_EXPR: | |
b7689b96 | 6994 | case SPACESHIP_EXPR: |
2d76680f PC |
6995 | case UNORDERED_EXPR: |
6996 | case ORDERED_EXPR: | |
6997 | case UNLT_EXPR: | |
6998 | case UNLE_EXPR: | |
6999 | case UNGT_EXPR: | |
7000 | case UNGE_EXPR: | |
7001 | case UNEQ_EXPR: | |
7002 | case LTGT_EXPR: | |
7003 | case RANGE_EXPR: | |
7004 | case COMPLEX_EXPR: | |
92a596e8 | 7005 | r = cxx_eval_binary_expression (ctx, t, lval, |
2d76680f PC |
7006 | non_constant_p, overflow_p); |
7007 | break; | |
7008 | ||
7009 | /* fold can introduce non-IF versions of these; still treat them as | |
7010 | short-circuiting. */ | |
7011 | case TRUTH_AND_EXPR: | |
7012 | case TRUTH_ANDIF_EXPR: | |
3e605b20 | 7013 | r = cxx_eval_logical_expression (ctx, t, boolean_false_node, |
2d76680f | 7014 | boolean_true_node, |
92a596e8 | 7015 | lval, |
2d76680f PC |
7016 | non_constant_p, overflow_p); |
7017 | break; | |
7018 | ||
7019 | case TRUTH_OR_EXPR: | |
7020 | case TRUTH_ORIF_EXPR: | |
3e605b20 | 7021 | r = cxx_eval_logical_expression (ctx, t, boolean_true_node, |
2d76680f | 7022 | boolean_false_node, |
92a596e8 | 7023 | lval, |
2d76680f PC |
7024 | non_constant_p, overflow_p); |
7025 | break; | |
7026 | ||
7027 | case ARRAY_REF: | |
92a596e8 | 7028 | r = cxx_eval_array_reference (ctx, t, lval, |
2d76680f PC |
7029 | non_constant_p, overflow_p); |
7030 | break; | |
7031 | ||
7032 | case COMPONENT_REF: | |
7033 | if (is_overloaded_fn (t)) | |
7034 | { | |
7035 | /* We can only get here in checking mode via | |
7036 | build_non_dependent_expr, because any expression that | |
7037 | calls or takes the address of the function will have | |
7038 | pulled a FUNCTION_DECL out of the COMPONENT_REF. */ | |
2b3ab879 | 7039 | gcc_checking_assert (ctx->quiet || errorcount); |
2d76680f PC |
7040 | *non_constant_p = true; |
7041 | return t; | |
7042 | } | |
92a596e8 | 7043 | r = cxx_eval_component_reference (ctx, t, lval, |
2d76680f PC |
7044 | non_constant_p, overflow_p); |
7045 | break; | |
7046 | ||
7047 | case BIT_FIELD_REF: | |
92a596e8 | 7048 | r = cxx_eval_bit_field_ref (ctx, t, lval, |
2d76680f PC |
7049 | non_constant_p, overflow_p); |
7050 | break; | |
7051 | ||
7052 | case COND_EXPR: | |
43574e4f | 7053 | case IF_STMT: |
4b390698 JJ |
7054 | if (jump_target && *jump_target) |
7055 | { | |
e9fa2f6d | 7056 | tree orig_jump = *jump_target; |
43574e4f JJ |
7057 | tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1)) |
7058 | ? TREE_OPERAND (t, 1) : void_node); | |
4b390698 JJ |
7059 | /* When jumping to a label, the label might be either in the |
7060 | then or else blocks, so process then block first in skipping | |
7061 | mode first, and if we are still in the skipping mode at its end, | |
7062 | process the else block too. */ | |
43574e4f JJ |
7063 | r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, |
7064 | overflow_p, jump_target); | |
e9fa2f6d MP |
7065 | /* It's possible that we found the label in the then block. But |
7066 | it could have been followed by another jumping statement, e.g. | |
7067 | say we're looking for case 1: | |
7068 | if (cond) | |
7069 | { | |
7070 | // skipped statements | |
7071 | case 1:; // clears up *jump_target | |
7072 | return 1; // and sets it to a RETURN_EXPR | |
7073 | } | |
7074 | else { ... } | |
7075 | in which case we need not go looking to the else block. | |
7076 | (goto is not allowed in a constexpr function.) */ | |
7077 | if (*jump_target == orig_jump) | |
43574e4f JJ |
7078 | { |
7079 | arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2)) | |
7080 | ? TREE_OPERAND (t, 2) : void_node); | |
7081 | r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, | |
7082 | overflow_p, jump_target); | |
7083 | } | |
4b390698 JJ |
7084 | break; |
7085 | } | |
92a596e8 | 7086 | r = cxx_eval_conditional_expression (ctx, t, lval, |
56632b27 JM |
7087 | non_constant_p, overflow_p, |
7088 | jump_target); | |
2d76680f | 7089 | break; |
f370e36d JJ |
7090 | case VEC_COND_EXPR: |
7091 | r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p, | |
7092 | overflow_p); | |
7093 | break; | |
2d76680f PC |
7094 | |
7095 | case CONSTRUCTOR: | |
35d87f01 | 7096 | if (TREE_CONSTANT (t) && reduced_constant_expression_p (t)) |
2d63bc39 JM |
7097 | { |
7098 | /* Don't re-process a constant CONSTRUCTOR, but do fold it to | |
7099 | VECTOR_CST if applicable. */ | |
ac9ec198 | 7100 | verify_constructor_flags (t); |
2d63bc39 JM |
7101 | if (TREE_CONSTANT (t)) |
7102 | return fold (t); | |
7103 | } | |
92a596e8 | 7104 | r = cxx_eval_bare_aggregate (ctx, t, lval, |
2d76680f PC |
7105 | non_constant_p, overflow_p); |
7106 | break; | |
7107 | ||
7108 | case VEC_INIT_EXPR: | |
7109 | /* We can get this in a defaulted constructor for a class with a | |
7110 | non-static data member of array type. Either the initializer will | |
7111 | be NULL, meaning default-initialization, or it will be an lvalue | |
7112 | or xvalue of the same type, meaning direct-initialization from the | |
7113 | corresponding member. */ | |
92a596e8 | 7114 | r = cxx_eval_vec_init (ctx, t, lval, |
2d76680f PC |
7115 | non_constant_p, overflow_p); |
7116 | break; | |
7117 | ||
2d76680f | 7118 | case VEC_PERM_EXPR: |
92a596e8 | 7119 | r = cxx_eval_trinary_expression (ctx, t, lval, |
2d76680f PC |
7120 | non_constant_p, overflow_p); |
7121 | break; | |
efb7c510 MK |
7122 | |
7123 | case PAREN_EXPR: | |
7124 | gcc_assert (!REF_PARENTHESIZED_P (t)); | |
7125 | /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in | |
7126 | constant expressions since it's unaffected by -fassociative-math. */ | |
7127 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, | |
7128 | non_constant_p, overflow_p); | |
7129 | break; | |
2d76680f | 7130 | |
7d75ea04 JJ |
7131 | case NOP_EXPR: |
7132 | if (REINTERPRET_CAST_P (t)) | |
7133 | { | |
7134 | if (!ctx->quiet) | |
6821245b | 7135 | error_at (loc, |
a9c697b8 | 7136 | "%<reinterpret_cast%> is not a constant expression"); |
7d75ea04 JJ |
7137 | *non_constant_p = true; |
7138 | return t; | |
7139 | } | |
7140 | /* FALLTHROUGH. */ | |
2d76680f PC |
7141 | case CONVERT_EXPR: |
7142 | case VIEW_CONVERT_EXPR: | |
cda0a029 | 7143 | case UNARY_PLUS_EXPR: |
2d76680f PC |
7144 | { |
7145 | tree oldop = TREE_OPERAND (t, 0); | |
cda0a029 | 7146 | |
3e605b20 | 7147 | tree op = cxx_eval_constant_expression (ctx, oldop, |
92a596e8 | 7148 | lval, |
5a804683 | 7149 | non_constant_p, overflow_p); |
2d76680f PC |
7150 | if (*non_constant_p) |
7151 | return t; | |
dcdbc004 | 7152 | tree type = TREE_TYPE (t); |
02a8575c JM |
7153 | |
7154 | if (VOID_TYPE_P (type)) | |
7155 | return void_node; | |
7156 | ||
eeb54a14 JJ |
7157 | if (TREE_CODE (t) == CONVERT_EXPR |
7158 | && ARITHMETIC_TYPE_P (type) | |
82bb6673 JJ |
7159 | && INDIRECT_TYPE_P (TREE_TYPE (op)) |
7160 | && ctx->manifestly_const_eval) | |
eeb54a14 JJ |
7161 | { |
7162 | if (!ctx->quiet) | |
7163 | error_at (loc, | |
7164 | "conversion from pointer type %qT to arithmetic type " | |
7165 | "%qT in a constant expression", TREE_TYPE (op), type); | |
7166 | *non_constant_p = true; | |
7167 | return t; | |
7168 | } | |
7169 | ||
2ffc2645 MP |
7170 | /* [expr.const]: a conversion from type cv void* to a pointer-to-object |
7171 | type cannot be part of a core constant expression as a resolution to | |
7172 | DR 1312. */ | |
6252e35c | 7173 | if (TYPE_PTROB_P (type) |
2ffc2645 MP |
7174 | && TYPE_PTR_P (TREE_TYPE (op)) |
7175 | && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op))) | |
7176 | /* Inside a call to std::construct_at or to | |
7177 | std::allocator<T>::{,de}allocate, we permit casting from void* | |
7178 | because that is compiler-generated code. */ | |
7179 | && !is_std_construct_at (ctx->call) | |
7180 | && !is_std_allocator_allocate (ctx->call)) | |
7181 | { | |
7182 | /* Likewise, don't error when casting from void* when OP is | |
7183 | &heap uninit and similar. */ | |
7184 | tree sop = tree_strip_nop_conversions (op); | |
7185 | if (TREE_CODE (sop) == ADDR_EXPR | |
7186 | && VAR_P (TREE_OPERAND (sop, 0)) | |
7187 | && DECL_ARTIFICIAL (TREE_OPERAND (sop, 0))) | |
7188 | /* OK */; | |
7189 | else | |
7190 | { | |
7191 | if (!ctx->quiet) | |
7192 | error_at (loc, "cast from %qT is not allowed", | |
7193 | TREE_TYPE (op)); | |
7194 | *non_constant_p = true; | |
7195 | return t; | |
7196 | } | |
7197 | } | |
7198 | ||
8e007055 | 7199 | if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type)) |
dcdbc004 | 7200 | op = cplus_expand_constant (op); |
7d75ea04 | 7201 | |
05bf54c3 MP |
7202 | if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR) |
7203 | { | |
7d75ea04 JJ |
7204 | if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op)) |
7205 | && !can_convert_qual (type, op)) | |
7206 | op = cplus_expand_constant (op); | |
7207 | return cp_fold_convert (type, op); | |
05bf54c3 | 7208 | } |
8bada5cd | 7209 | |
71a93b08 | 7210 | if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST) |
2d76680f | 7211 | { |
8bada5cd MS |
7212 | if (integer_zerop (op)) |
7213 | { | |
9f613f06 | 7214 | if (TYPE_REF_P (type)) |
8bada5cd MS |
7215 | { |
7216 | if (!ctx->quiet) | |
2ffc2645 | 7217 | error_at (loc, "dereferencing a null pointer"); |
8bada5cd MS |
7218 | *non_constant_p = true; |
7219 | return t; | |
7220 | } | |
8bada5cd MS |
7221 | } |
7222 | else | |
7223 | { | |
7224 | /* This detects for example: | |
7225 | reinterpret_cast<void*>(sizeof 0) | |
7226 | */ | |
7227 | if (!ctx->quiet) | |
6821245b | 7228 | error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not " |
64d6d399 | 7229 | "a constant expression", |
8bada5cd MS |
7230 | type, op); |
7231 | *non_constant_p = true; | |
7232 | return t; | |
7233 | } | |
2d76680f | 7234 | } |
7d75ea04 | 7235 | |
8e007055 JJ |
7236 | if (INDIRECT_TYPE_P (type) |
7237 | && TREE_CODE (op) == NOP_EXPR | |
7238 | && TREE_TYPE (op) == ptr_type_node | |
7239 | && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR | |
7240 | && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0)) | |
815baade JJ |
7241 | && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), |
7242 | 0)) == heap_uninit_identifier | |
7243 | || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), | |
7244 | 0)) == heap_vec_uninit_identifier)) | |
8e007055 JJ |
7245 | { |
7246 | tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0); | |
7247 | tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var)); | |
7248 | tree elt_type = TREE_TYPE (type); | |
7249 | tree cookie_size = NULL_TREE; | |
7250 | if (TREE_CODE (elt_type) == RECORD_TYPE | |
7251 | && TYPE_NAME (elt_type) == heap_identifier) | |
7252 | { | |
7253 | tree fld1 = TYPE_FIELDS (elt_type); | |
7254 | tree fld2 = DECL_CHAIN (fld1); | |
7255 | elt_type = TREE_TYPE (TREE_TYPE (fld2)); | |
7256 | cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1)); | |
7257 | } | |
815baade JJ |
7258 | DECL_NAME (var) |
7259 | = (DECL_NAME (var) == heap_uninit_identifier | |
7260 | ? heap_identifier : heap_vec_identifier); | |
8e007055 JJ |
7261 | TREE_TYPE (var) |
7262 | = build_new_constexpr_heap_type (elt_type, cookie_size, | |
7263 | var_size); | |
7264 | TREE_TYPE (TREE_OPERAND (op, 0)) | |
7265 | = build_pointer_type (TREE_TYPE (var)); | |
7266 | } | |
7267 | ||
cda0a029 | 7268 | if (op == oldop && tcode != UNARY_PLUS_EXPR) |
2d76680f PC |
7269 | /* We didn't fold at the top so we could check for ptr-int |
7270 | conversion. */ | |
7271 | return fold (t); | |
7d75ea04 | 7272 | |
02a8575c JM |
7273 | tree sop; |
7274 | ||
e4511ca2 JM |
7275 | /* Handle an array's bounds having been deduced after we built |
7276 | the wrapping expression. */ | |
7277 | if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op))) | |
7278 | r = op; | |
02a8575c JM |
7279 | else if (sop = tree_strip_nop_conversions (op), |
7280 | sop != op && (same_type_ignoring_tlq_and_bounds_p | |
7281 | (type, TREE_TYPE (sop)))) | |
7282 | r = sop; | |
e4511ca2 | 7283 | else if (tcode == UNARY_PLUS_EXPR) |
cda0a029 JM |
7284 | r = fold_convert (TREE_TYPE (t), op); |
7285 | else | |
7286 | r = fold_build1 (tcode, type, op); | |
7d75ea04 | 7287 | |
2d76680f PC |
7288 | /* Conversion of an out-of-range value has implementation-defined |
7289 | behavior; the language considers it different from arithmetic | |
7290 | overflow, which is undefined. */ | |
7291 | if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op)) | |
7292 | TREE_OVERFLOW (r) = false; | |
7293 | } | |
7294 | break; | |
7295 | ||
7296 | case EMPTY_CLASS_EXPR: | |
46fdced6 PP |
7297 | /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering |
7298 | it to an appropriate CONSTRUCTOR. */ | |
7299 | return build_constructor (TREE_TYPE (t), NULL); | |
2d76680f | 7300 | |
60813a46 | 7301 | case STATEMENT_LIST: |
56632b27 JM |
7302 | new_ctx = *ctx; |
7303 | new_ctx.ctor = new_ctx.object = NULL_TREE; | |
2b3ab879 | 7304 | return cxx_eval_statement_list (&new_ctx, t, |
56632b27 | 7305 | non_constant_p, overflow_p, jump_target); |
60813a46 JM |
7306 | |
7307 | case BIND_EXPR: | |
7308 | return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t), | |
92a596e8 | 7309 | lval, |
56632b27 JM |
7310 | non_constant_p, overflow_p, |
7311 | jump_target); | |
60813a46 | 7312 | |
2d76680f PC |
7313 | case PREINCREMENT_EXPR: |
7314 | case POSTINCREMENT_EXPR: | |
7315 | case PREDECREMENT_EXPR: | |
7316 | case POSTDECREMENT_EXPR: | |
2b3ab879 | 7317 | return cxx_eval_increment_expression (ctx, t, |
92a596e8 | 7318 | lval, non_constant_p, overflow_p); |
60813a46 JM |
7319 | |
7320 | case LAMBDA_EXPR: | |
2d76680f PC |
7321 | case NEW_EXPR: |
7322 | case VEC_NEW_EXPR: | |
7323 | case DELETE_EXPR: | |
7324 | case VEC_DELETE_EXPR: | |
7325 | case THROW_EXPR: | |
2d76680f PC |
7326 | case MODOP_EXPR: |
7327 | /* GCC internal stuff. */ | |
7328 | case VA_ARG_EXPR: | |
2d76680f PC |
7329 | case NON_DEPENDENT_EXPR: |
7330 | case BASELINK: | |
2d76680f | 7331 | case OFFSET_REF: |
2b3ab879 | 7332 | if (!ctx->quiet) |
6821245b | 7333 | error_at (loc, "expression %qE is not a constant expression", t); |
2d76680f PC |
7334 | *non_constant_p = true; |
7335 | break; | |
7336 | ||
bf8d8309 | 7337 | case OBJ_TYPE_REF: |
7ece3bd8 JM |
7338 | /* Virtual function lookup. We don't need to do anything fancy. */ |
7339 | return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t), | |
7340 | lval, non_constant_p, overflow_p); | |
bf8d8309 | 7341 | |
3e605b20 | 7342 | case PLACEHOLDER_EXPR: |
89262ec6 JM |
7343 | /* Use of the value or address of the current object. */ |
7344 | if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t))) | |
b599bf9d PP |
7345 | { |
7346 | if (TREE_CODE (ctor) == CONSTRUCTOR) | |
7347 | return ctor; | |
7348 | else | |
7349 | return cxx_eval_constant_expression (ctx, ctor, lval, | |
7350 | non_constant_p, overflow_p); | |
7351 | } | |
89262ec6 JM |
7352 | /* A placeholder without a referent. We can get here when |
7353 | checking whether NSDMIs are noexcept, or in massage_init_elt; | |
7354 | just say it's non-constant for now. */ | |
7355 | gcc_assert (ctx->quiet); | |
7356 | *non_constant_p = true; | |
3e605b20 JM |
7357 | break; |
7358 | ||
06ec22b7 JM |
7359 | case EXIT_EXPR: |
7360 | { | |
7361 | tree cond = TREE_OPERAND (t, 0); | |
7362 | cond = cxx_eval_constant_expression (ctx, cond, /*lval*/false, | |
7363 | non_constant_p, overflow_p); | |
7364 | VERIFY_CONSTANT (cond); | |
7365 | if (integer_nonzerop (cond)) | |
7366 | *jump_target = t; | |
7367 | } | |
7368 | break; | |
7369 | ||
60813a46 | 7370 | case GOTO_EXPR: |
8892d532 JJ |
7371 | if (breaks (&TREE_OPERAND (t, 0)) |
7372 | || continues (&TREE_OPERAND (t, 0)) | |
7373 | /* Allow for jumping to a cdtor_label. */ | |
7374 | || returns (&TREE_OPERAND (t, 0))) | |
7375 | *jump_target = TREE_OPERAND (t, 0); | |
7376 | else | |
7377 | { | |
7378 | gcc_assert (cxx_dialect >= cxx23); | |
7379 | if (!ctx->quiet) | |
7380 | error_at (loc, "%<goto%> is not a constant expression"); | |
7381 | *non_constant_p = true; | |
7382 | } | |
56632b27 JM |
7383 | break; |
7384 | ||
60813a46 | 7385 | case LOOP_EXPR: |
43574e4f JJ |
7386 | case DO_STMT: |
7387 | case WHILE_STMT: | |
7388 | case FOR_STMT: | |
2b3ab879 | 7389 | cxx_eval_loop_expr (ctx, t, |
56632b27 JM |
7390 | non_constant_p, overflow_p, jump_target); |
7391 | break; | |
7392 | ||
60813a46 | 7393 | case SWITCH_EXPR: |
43574e4f | 7394 | case SWITCH_STMT: |
2b3ab879 | 7395 | cxx_eval_switch_expr (ctx, t, |
56632b27 | 7396 | non_constant_p, overflow_p, jump_target); |
60813a46 JM |
7397 | break; |
7398 | ||
971e17ff AS |
7399 | case REQUIRES_EXPR: |
7400 | /* It's possible to get a requires-expression in a constant | |
7401 | expression. For example: | |
7402 | ||
7403 | template<typename T> concept bool C() { | |
7404 | return requires (T t) { t; }; | |
7405 | } | |
7406 | ||
7407 | template<typename T> requires !C<T>() void f(T); | |
7408 | ||
7409 | Normalization leaves f with the associated constraint | |
7410 | '!requires (T t) { ... }' which is not transformed into | |
7411 | a constraint. */ | |
7412 | if (!processing_template_decl) | |
662ef5b5 | 7413 | return evaluate_requires_expr (t); |
971e17ff AS |
7414 | else |
7415 | *non_constant_p = true; | |
7416 | return t; | |
7417 | ||
98e09245 | 7418 | case ANNOTATE_EXPR: |
98e09245 JJ |
7419 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
7420 | lval, | |
7421 | non_constant_p, overflow_p, | |
7422 | jump_target); | |
7423 | break; | |
7424 | ||
3664e317 JM |
7425 | case USING_STMT: |
7426 | r = void_node; | |
7427 | break; | |
7428 | ||
cb57504a JM |
7429 | case TEMPLATE_ID_EXPR: |
7430 | { | |
7431 | /* We can evaluate template-id that refers to a concept only if | |
7432 | the template arguments are non-dependent. */ | |
861d4af8 AS |
7433 | tree id = unpack_concept_check (t); |
7434 | tree tmpl = TREE_OPERAND (id, 0); | |
7435 | if (!concept_definition_p (tmpl)) | |
cb57504a JM |
7436 | internal_error ("unexpected template-id %qE", t); |
7437 | ||
861d4af8 AS |
7438 | if (function_concept_p (tmpl)) |
7439 | { | |
7440 | if (!ctx->quiet) | |
7441 | error_at (cp_expr_loc_or_input_loc (t), | |
7442 | "function concept must be called"); | |
7443 | r = error_mark_node; | |
7444 | break; | |
7445 | } | |
7446 | ||
9329344a | 7447 | if (!value_dependent_expression_p (t) |
6d1556ec | 7448 | && !uid_sensitive_constexpr_evaluation_p ()) |
662ef5b5 | 7449 | r = evaluate_concept_check (t); |
cb57504a JM |
7450 | else |
7451 | *non_constant_p = true; | |
861d4af8 AS |
7452 | |
7453 | break; | |
cb57504a JM |
7454 | } |
7455 | ||
529bc410 MP |
7456 | case ASM_EXPR: |
7457 | if (!ctx->quiet) | |
6821245b | 7458 | inline_asm_in_constexpr_error (loc); |
529bc410 MP |
7459 | *non_constant_p = true; |
7460 | return t; | |
7461 | ||
896048cf | 7462 | case BIT_CAST_EXPR: |
606f2af1 JJ |
7463 | if (lval) |
7464 | { | |
7465 | if (!ctx->quiet) | |
7466 | error_at (EXPR_LOCATION (t), | |
7467 | "address of a call to %qs is not a constant expression", | |
7468 | "__builtin_bit_cast"); | |
7469 | *non_constant_p = true; | |
7470 | return t; | |
7471 | } | |
896048cf JJ |
7472 | r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p); |
7473 | break; | |
7474 | ||
2d76680f | 7475 | default: |
c6e7c499 JM |
7476 | if (STATEMENT_CODE_P (TREE_CODE (t))) |
7477 | { | |
7478 | /* This function doesn't know how to deal with pre-genericize | |
7479 | statements; this can only happen with statement-expressions, | |
7480 | so for now just fail. */ | |
7481 | if (!ctx->quiet) | |
7482 | error_at (EXPR_LOCATION (t), | |
64d6d399 | 7483 | "statement is not a constant expression"); |
c6e7c499 JM |
7484 | } |
7485 | else | |
7486 | internal_error ("unexpected expression %qE of kind %s", t, | |
7487 | get_tree_code_name (TREE_CODE (t))); | |
2d76680f PC |
7488 | *non_constant_p = true; |
7489 | break; | |
7490 | } | |
7491 | ||
7492 | if (r == error_mark_node) | |
7493 | *non_constant_p = true; | |
7494 | ||
7495 | if (*non_constant_p) | |
7496 | return t; | |
7497 | else | |
7498 | return r; | |
7499 | } | |
7500 | ||
e079dced JM |
7501 | /* P0859: A function is needed for constant evaluation if it is a constexpr |
7502 | function that is named by an expression ([basic.def.odr]) that is | |
7503 | potentially constant evaluated. | |
7504 | ||
7505 | So we need to instantiate any constexpr functions mentioned by the | |
7506 | expression even if the definition isn't needed for evaluating the | |
7507 | expression. */ | |
7508 | ||
7509 | static tree | |
7510 | instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/) | |
7511 | { | |
7512 | if (TREE_CODE (*tp) == FUNCTION_DECL | |
7513 | && DECL_DECLARED_CONSTEXPR_P (*tp) | |
7514 | && !DECL_INITIAL (*tp) | |
4fdda3ce | 7515 | && !trivial_fn_p (*tp) |
f65a3299 PP |
7516 | && DECL_TEMPLOID_INSTANTIATION (*tp) |
7517 | && !uid_sensitive_constexpr_evaluation_p ()) | |
e079dced JM |
7518 | { |
7519 | ++function_depth; | |
7520 | instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false); | |
7521 | --function_depth; | |
7522 | } | |
7523 | else if (TREE_CODE (*tp) == CALL_EXPR | |
7524 | || TREE_CODE (*tp) == AGGR_INIT_EXPR) | |
7525 | { | |
7526 | if (EXPR_HAS_LOCATION (*tp)) | |
7527 | input_location = EXPR_LOCATION (*tp); | |
7528 | } | |
7529 | ||
7530 | if (!EXPR_P (*tp)) | |
7531 | *walk_subtrees = 0; | |
7532 | ||
7533 | return NULL_TREE; | |
7534 | } | |
8e007055 | 7535 | |
e079dced JM |
7536 | static void |
7537 | instantiate_constexpr_fns (tree t) | |
7538 | { | |
7539 | location_t loc = input_location; | |
7540 | cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL); | |
7541 | input_location = loc; | |
7542 | } | |
7543 | ||
8e007055 JJ |
7544 | /* Look for heap variables in the expression *TP. */ |
7545 | ||
7546 | static tree | |
7547 | find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/) | |
7548 | { | |
7549 | if (VAR_P (*tp) | |
7550 | && (DECL_NAME (*tp) == heap_uninit_identifier | |
7551 | || DECL_NAME (*tp) == heap_identifier | |
815baade JJ |
7552 | || DECL_NAME (*tp) == heap_vec_uninit_identifier |
7553 | || DECL_NAME (*tp) == heap_vec_identifier | |
8e007055 JJ |
7554 | || DECL_NAME (*tp) == heap_deleted_identifier)) |
7555 | return *tp; | |
7556 | ||
7557 | if (TYPE_P (*tp)) | |
7558 | *walk_subtrees = 0; | |
7559 | return NULL_TREE; | |
7560 | } | |
7561 | ||
f968ef9b JJ |
7562 | /* Find immediate function decls in *TP if any. */ |
7563 | ||
7564 | static tree | |
7565 | find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/) | |
7566 | { | |
7567 | if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp)) | |
7568 | return *tp; | |
4b2fda8b JJ |
7569 | if (TREE_CODE (*tp) == PTRMEM_CST |
7570 | && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL | |
7571 | && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp))) | |
7572 | return PTRMEM_CST_MEMBER (*tp); | |
f968ef9b JJ |
7573 | return NULL_TREE; |
7574 | } | |
7575 | ||
e4082611 | 7576 | /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression. |
d4accef3 JM |
7577 | STRICT has the same sense as for constant_value_1: true if we only allow |
7578 | conforming C++ constant expressions, or false if we want a constant value | |
7579 | even if it doesn't conform. | |
13de99bc | 7580 | MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as |
8e007055 JJ |
7581 | per P0595 even when ALLOW_NON_CONSTANT is true. |
7582 | CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable. | |
7583 | OBJECT must be non-NULL in that case. */ | |
e4082611 | 7584 | |
2d76680f | 7585 | static tree |
3e605b20 | 7586 | cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant, |
e4082611 | 7587 | bool strict = true, |
13de99bc | 7588 | bool manifestly_const_eval = false, |
8e007055 | 7589 | bool constexpr_dtor = false, |
f65a3299 | 7590 | tree object = NULL_TREE) |
2d76680f | 7591 | { |
8108ea30 JM |
7592 | auto_timevar time (TV_CONSTEXPR); |
7593 | ||
2d76680f PC |
7594 | bool non_constant_p = false; |
7595 | bool overflow_p = false; | |
39dce2b7 | 7596 | |
d419e176 MP |
7597 | if (BRACE_ENCLOSED_INITIALIZER_P (t)) |
7598 | { | |
7599 | gcc_checking_assert (allow_non_constant); | |
7600 | return t; | |
7601 | } | |
7602 | ||
8e007055 | 7603 | constexpr_global_ctx global_ctx; |
49a86fce | 7604 | constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL, |
8e007055 | 7605 | allow_non_constant, strict, |
f65a3299 | 7606 | manifestly_const_eval || !allow_non_constant }; |
39dce2b7 | 7607 | |
6641d6d3 JJ |
7608 | /* Turn off -frounding-math for manifestly constant evaluation. */ |
7609 | warning_sentinel rm (flag_rounding_math, ctx.manifestly_const_eval); | |
3e605b20 | 7610 | tree type = initialized_type (t); |
3e605b20 | 7611 | tree r = t; |
f968ef9b | 7612 | bool is_consteval = false; |
4d0c18c6 | 7613 | if (VOID_TYPE_P (type)) |
8e007055 JJ |
7614 | { |
7615 | if (constexpr_dtor) | |
7616 | /* Used for destructors of array elements. */ | |
7617 | type = TREE_TYPE (object); | |
7618 | else | |
f968ef9b | 7619 | { |
b04445d4 | 7620 | if (cxx_dialect < cxx20) |
f968ef9b JJ |
7621 | return t; |
7622 | if (TREE_CODE (t) != CALL_EXPR && TREE_CODE (t) != AGGR_INIT_EXPR) | |
7623 | return t; | |
7624 | /* Calls to immediate functions returning void need to be | |
7625 | evaluated. */ | |
7626 | tree fndecl = cp_get_callee_fndecl_nofold (t); | |
7627 | if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl)) | |
7628 | return t; | |
7629 | else | |
7630 | is_consteval = true; | |
7631 | } | |
7632 | } | |
b04445d4 | 7633 | else if (cxx_dialect >= cxx20 |
f968ef9b JJ |
7634 | && (TREE_CODE (t) == CALL_EXPR |
7635 | || TREE_CODE (t) == AGGR_INIT_EXPR | |
7636 | || TREE_CODE (t) == TARGET_EXPR)) | |
7637 | { | |
861d4af8 AS |
7638 | /* For non-concept checks, determine if it is consteval. */ |
7639 | if (!concept_check_p (t)) | |
7640 | { | |
7641 | tree x = t; | |
7642 | if (TREE_CODE (x) == TARGET_EXPR) | |
7643 | x = TARGET_EXPR_INITIAL (x); | |
7644 | tree fndecl = cp_get_callee_fndecl_nofold (x); | |
7645 | if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl)) | |
7646 | is_consteval = true; | |
7647 | } | |
8e007055 | 7648 | } |
3e605b20 JM |
7649 | if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)) |
7650 | { | |
7651 | /* In C++14 an NSDMI can participate in aggregate initialization, | |
7652 | and can refer to the address of the object being initialized, so | |
7653 | we need to pass in the relevant VAR_DECL if we want to do the | |
7654 | evaluation in a single pass. The evaluation will dynamically | |
7655 | update ctx.values for the VAR_DECL. We use the same strategy | |
7656 | for C++11 constexpr constructors that refer to the object being | |
7657 | initialized. */ | |
8e007055 JJ |
7658 | if (constexpr_dtor) |
7659 | { | |
7660 | gcc_assert (object && VAR_P (object)); | |
7661 | gcc_assert (DECL_DECLARED_CONSTEXPR_P (object)); | |
7662 | gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object)); | |
fce6467b JJ |
7663 | if (error_operand_p (DECL_INITIAL (object))) |
7664 | return t; | |
8e007055 JJ |
7665 | ctx.ctor = unshare_expr (DECL_INITIAL (object)); |
7666 | TREE_READONLY (ctx.ctor) = false; | |
7667 | /* Temporarily force decl_really_constant_value to return false | |
7668 | for it, we want to use ctx.ctor for the current value instead. */ | |
7669 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false; | |
7670 | } | |
7671 | else | |
7672 | { | |
7673 | ctx.ctor = build_constructor (type, NULL); | |
7674 | CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true; | |
7675 | } | |
60813a46 JM |
7676 | if (!object) |
7677 | { | |
7678 | if (TREE_CODE (t) == TARGET_EXPR) | |
7679 | object = TARGET_EXPR_SLOT (t); | |
7680 | else if (TREE_CODE (t) == AGGR_INIT_EXPR) | |
7681 | object = AGGR_INIT_EXPR_SLOT (t); | |
7682 | } | |
3e605b20 JM |
7683 | ctx.object = object; |
7684 | if (object) | |
7685 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
7686 | (type, TREE_TYPE (object))); | |
7687 | if (object && DECL_P (object)) | |
8e007055 | 7688 | global_ctx.values.put (object, ctx.ctor); |
3e605b20 JM |
7689 | if (TREE_CODE (r) == TARGET_EXPR) |
7690 | /* Avoid creating another CONSTRUCTOR when we expand the | |
7691 | TARGET_EXPR. */ | |
7692 | r = TARGET_EXPR_INITIAL (r); | |
7693 | } | |
7694 | ||
ee1de08d JJ |
7695 | auto_vec<tree, 16> cleanups; |
7696 | global_ctx.cleanups = &cleanups; | |
7697 | ||
1595fe44 JM |
7698 | if (manifestly_const_eval) |
7699 | instantiate_constexpr_fns (r); | |
2b3ab879 | 7700 | r = cxx_eval_constant_expression (&ctx, r, |
5a804683 | 7701 | false, &non_constant_p, &overflow_p); |
2d76680f | 7702 | |
8e007055 JJ |
7703 | if (!constexpr_dtor) |
7704 | verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p); | |
7705 | else | |
7706 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true; | |
2d76680f | 7707 | |
ee1de08d JJ |
7708 | unsigned int i; |
7709 | tree cleanup; | |
7710 | /* Evaluate the cleanups. */ | |
7711 | FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup) | |
7712 | cxx_eval_constant_expression (&ctx, cleanup, false, | |
7713 | &non_constant_p, &overflow_p); | |
7714 | ||
023d89c7 JM |
7715 | /* Mutable logic is a bit tricky: we want to allow initialization of |
7716 | constexpr variables with mutable members, but we can't copy those | |
7717 | members to another constexpr variable. */ | |
8e007055 | 7718 | if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_MUTABLE_POISON (r)) |
2d76680f | 7719 | { |
2d76680f | 7720 | if (!allow_non_constant) |
023d89c7 JM |
7721 | error ("%qE is not a constant expression because it refers to " |
7722 | "mutable subobjects of %qT", t, type); | |
2d76680f PC |
7723 | non_constant_p = true; |
7724 | } | |
7725 | ||
8e007055 | 7726 | if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r)) |
f64e0c02 JM |
7727 | { |
7728 | if (!allow_non_constant) | |
7729 | error ("%qE is not a constant expression because it refers to " | |
7730 | "an incompletely initialized variable", t); | |
7731 | TREE_CONSTANT (r) = false; | |
7732 | non_constant_p = true; | |
7733 | } | |
7734 | ||
8e007055 JJ |
7735 | if (!global_ctx.heap_vars.is_empty ()) |
7736 | { | |
7737 | tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs, | |
7738 | NULL); | |
7739 | unsigned int i; | |
7740 | if (heap_var) | |
7741 | { | |
7742 | if (!allow_non_constant && !non_constant_p) | |
7743 | error_at (DECL_SOURCE_LOCATION (heap_var), | |
7744 | "%qE is not a constant expression because it refers to " | |
7745 | "a result of %<operator new%>", t); | |
7746 | r = t; | |
7747 | non_constant_p = true; | |
7748 | } | |
7749 | FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var) | |
a8db7887 JJ |
7750 | { |
7751 | if (DECL_NAME (heap_var) != heap_deleted_identifier) | |
7752 | { | |
7753 | if (!allow_non_constant && !non_constant_p) | |
7754 | error_at (DECL_SOURCE_LOCATION (heap_var), | |
7755 | "%qE is not a constant expression because allocated " | |
7756 | "storage has not been deallocated", t); | |
7757 | r = t; | |
7758 | non_constant_p = true; | |
7759 | } | |
7760 | varpool_node::get (heap_var)->remove (); | |
7761 | } | |
8e007055 JJ |
7762 | } |
7763 | ||
f968ef9b | 7764 | /* Check that immediate invocation does not return an expression referencing |
7473b8a9 JJ |
7765 | any immediate function decls. */ |
7766 | if (is_consteval || in_immediate_context ()) | |
f968ef9b JJ |
7767 | if (tree immediate_fndecl |
7768 | = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl, | |
7769 | NULL)) | |
7770 | { | |
7771 | if (!allow_non_constant && !non_constant_p) | |
7772 | error_at (cp_expr_loc_or_input_loc (t), | |
7773 | "immediate evaluation returns address of immediate " | |
7774 | "function %qD", immediate_fndecl); | |
7775 | r = t; | |
7776 | non_constant_p = true; | |
7777 | } | |
7778 | ||
8895443a JM |
7779 | if (non_constant_p) |
7780 | /* If we saw something bad, go back to our argument. The wrapping below is | |
7781 | only for the cases of TREE_CONSTANT argument or overflow. */ | |
7782 | r = t; | |
2d76680f PC |
7783 | |
7784 | if (!non_constant_p && overflow_p) | |
7785 | non_constant_p = true; | |
7786 | ||
e0804c9b | 7787 | /* Unshare the result. */ |
56cfb596 | 7788 | bool should_unshare = true; |
e0804c9b JM |
7789 | if (r == t || (TREE_CODE (t) == TARGET_EXPR |
7790 | && TARGET_EXPR_INITIAL (t) == r)) | |
56cfb596 PP |
7791 | should_unshare = false; |
7792 | ||
2d76680f PC |
7793 | if (non_constant_p && !allow_non_constant) |
7794 | return error_mark_node; | |
8e007055 JJ |
7795 | else if (constexpr_dtor) |
7796 | return r; | |
2d76680f PC |
7797 | else if (non_constant_p && TREE_CONSTANT (r)) |
7798 | { | |
eddd715c JM |
7799 | /* This isn't actually constant, so unset TREE_CONSTANT. |
7800 | Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires | |
7801 | it to be set if it is invariant address, even when it is not | |
7802 | a valid C++ constant expression. Wrap it with a NOP_EXPR | |
7803 | instead. */ | |
7804 | if (EXPR_P (r) && TREE_CODE (r) != ADDR_EXPR) | |
2d76680f PC |
7805 | r = copy_node (r); |
7806 | else if (TREE_CODE (r) == CONSTRUCTOR) | |
7807 | r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r); | |
7808 | else | |
7809 | r = build_nop (TREE_TYPE (r), r); | |
7810 | TREE_CONSTANT (r) = false; | |
7811 | } | |
5dab8b11 | 7812 | else if (non_constant_p) |
2d76680f PC |
7813 | return t; |
7814 | ||
56cfb596 PP |
7815 | if (should_unshare) |
7816 | r = unshare_expr (r); | |
7817 | ||
2d76680f PC |
7818 | if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r))) |
7819 | { | |
5dab8b11 | 7820 | r = adjust_temp_type (type, r); |
2d76680f PC |
7821 | if (TREE_CODE (t) == TARGET_EXPR |
7822 | && TARGET_EXPR_INITIAL (t) == r) | |
7823 | return t; | |
5dab8b11 | 7824 | else if (TREE_CODE (t) != CONSTRUCTOR) |
2d76680f | 7825 | { |
8e007055 | 7826 | r = get_target_expr_sfinae (r, tf_warning_or_error | tf_no_cleanup); |
2d76680f | 7827 | TREE_CONSTANT (r) = true; |
2d76680f PC |
7828 | } |
7829 | } | |
5dab8b11 | 7830 | |
729f6881 JM |
7831 | /* Remember the original location if that wouldn't need a wrapper. */ |
7832 | if (location_t loc = EXPR_LOCATION (t)) | |
8d970817 | 7833 | protected_set_expr_location (r, loc); |
729f6881 | 7834 | |
5dab8b11 | 7835 | return r; |
2d76680f PC |
7836 | } |
7837 | ||
2d76680f PC |
7838 | /* If T represents a constant expression returns its reduced value. |
7839 | Otherwise return error_mark_node. If T is dependent, then | |
7840 | return NULL. */ | |
7841 | ||
7842 | tree | |
3e605b20 | 7843 | cxx_constant_value (tree t, tree decl) |
2d76680f | 7844 | { |
8e007055 JJ |
7845 | return cxx_eval_outermost_constant_expr (t, false, true, true, false, decl); |
7846 | } | |
7847 | ||
a8de8324 JM |
7848 | /* As above, but respect SFINAE. */ |
7849 | ||
7850 | tree | |
7851 | cxx_constant_value_sfinae (tree t, tsubst_flags_t complain) | |
7852 | { | |
7853 | bool sfinae = !(complain & tf_error); | |
7854 | tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, true); | |
7855 | if (sfinae && !TREE_CONSTANT (r)) | |
7856 | r = error_mark_node; | |
7857 | return r; | |
7858 | } | |
7859 | ||
8e007055 JJ |
7860 | /* Like cxx_constant_value, but used for evaluation of constexpr destructors |
7861 | of constexpr variables. The actual initializer of DECL is not modified. */ | |
7862 | ||
7863 | void | |
7864 | cxx_constant_dtor (tree t, tree decl) | |
7865 | { | |
7866 | cxx_eval_outermost_constant_expr (t, false, true, true, true, decl); | |
2d76680f PC |
7867 | } |
7868 | ||
cda0a029 JM |
7869 | /* Helper routine for fold_simple function. Either return simplified |
7870 | expression T, otherwise NULL_TREE. | |
7871 | In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold | |
7872 | even if we are within template-declaration. So be careful on call, as in | |
7873 | such case types can be undefined. */ | |
7874 | ||
7875 | static tree | |
7876 | fold_simple_1 (tree t) | |
7877 | { | |
7878 | tree op1; | |
7879 | enum tree_code code = TREE_CODE (t); | |
7880 | ||
7881 | switch (code) | |
7882 | { | |
7883 | case INTEGER_CST: | |
7884 | case REAL_CST: | |
7885 | case VECTOR_CST: | |
7886 | case FIXED_CST: | |
7887 | case COMPLEX_CST: | |
7888 | return t; | |
7889 | ||
7890 | case SIZEOF_EXPR: | |
7891 | return fold_sizeof_expr (t); | |
7892 | ||
7893 | case ABS_EXPR: | |
e28fadbc | 7894 | case ABSU_EXPR: |
cda0a029 JM |
7895 | case CONJ_EXPR: |
7896 | case REALPART_EXPR: | |
7897 | case IMAGPART_EXPR: | |
7898 | case NEGATE_EXPR: | |
7899 | case BIT_NOT_EXPR: | |
7900 | case TRUTH_NOT_EXPR: | |
7901 | case NOP_EXPR: | |
7902 | case VIEW_CONVERT_EXPR: | |
7903 | case CONVERT_EXPR: | |
7904 | case FLOAT_EXPR: | |
7905 | case FIX_TRUNC_EXPR: | |
7906 | case FIXED_CONVERT_EXPR: | |
7907 | case ADDR_SPACE_CONVERT_EXPR: | |
7908 | ||
7909 | op1 = TREE_OPERAND (t, 0); | |
7910 | ||
7911 | t = const_unop (code, TREE_TYPE (t), op1); | |
7912 | if (!t) | |
7913 | return NULL_TREE; | |
7914 | ||
7915 | if (CONVERT_EXPR_CODE_P (code) | |
7916 | && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1)) | |
7917 | TREE_OVERFLOW (t) = false; | |
7918 | return t; | |
7919 | ||
7920 | default: | |
7921 | return NULL_TREE; | |
7922 | } | |
7923 | } | |
7924 | ||
7925 | /* If T is a simple constant expression, returns its simplified value. | |
dba99244 | 7926 | Otherwise returns T. In contrast to maybe_constant_value we |
cda0a029 JM |
7927 | simplify only few operations on constant-expressions, and we don't |
7928 | try to simplify constexpressions. */ | |
7929 | ||
7930 | tree | |
7931 | fold_simple (tree t) | |
7932 | { | |
cda0a029 JM |
7933 | if (processing_template_decl) |
7934 | return t; | |
7935 | ||
dba99244 MP |
7936 | tree r = fold_simple_1 (t); |
7937 | if (r) | |
7938 | return r; | |
cda0a029 | 7939 | |
dba99244 | 7940 | return t; |
cda0a029 JM |
7941 | } |
7942 | ||
2d76680f PC |
7943 | /* If T is a constant expression, returns its reduced value. |
7944 | Otherwise, if T does not have TREE_CONSTANT set, returns T. | |
13de99bc JJ |
7945 | Otherwise, returns a version of T without TREE_CONSTANT. |
7946 | MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated | |
f65a3299 | 7947 | as per P0595. */ |
2d76680f | 7948 | |
8a87daca JM |
7949 | static GTY((deletable)) hash_map<tree, tree> *cv_cache; |
7950 | ||
7951 | tree | |
f65a3299 | 7952 | maybe_constant_value (tree t, tree decl, bool manifestly_const_eval) |
2d76680f PC |
7953 | { |
7954 | tree r; | |
7955 | ||
a0ab7ccd | 7956 | if (!is_nondependent_constant_expression (t)) |
2d76680f PC |
7957 | { |
7958 | if (TREE_OVERFLOW_P (t)) | |
7959 | { | |
7960 | t = build_nop (TREE_TYPE (t), t); | |
7961 | TREE_CONSTANT (t) = false; | |
7962 | } | |
7963 | return t; | |
7964 | } | |
8a87daca JM |
7965 | else if (CONSTANT_CLASS_P (t)) |
7966 | /* No caching or evaluation needed. */ | |
7967 | return t; | |
7968 | ||
13de99bc | 7969 | if (manifestly_const_eval) |
f65a3299 | 7970 | return cxx_eval_outermost_constant_expr (t, true, true, true, false, decl); |
13de99bc | 7971 | |
8a87daca JM |
7972 | if (cv_cache == NULL) |
7973 | cv_cache = hash_map<tree, tree>::create_ggc (101); | |
7974 | if (tree *cached = cv_cache->get (t)) | |
173c8def JM |
7975 | { |
7976 | r = *cached; | |
7977 | if (r != t) | |
7978 | { | |
9f143008 | 7979 | r = break_out_target_exprs (r, /*clear_loc*/true); |
173c8def JM |
7980 | protected_set_expr_location (r, EXPR_LOCATION (t)); |
7981 | } | |
dfffecb8 | 7982 | return r; |
173c8def | 7983 | } |
2d76680f | 7984 | |
4df7f8c7 JM |
7985 | /* Don't evaluate an unevaluated operand. */ |
7986 | if (cp_unevaluated_operand) | |
7987 | return t; | |
7988 | ||
f65a3299 PP |
7989 | uid_sensitive_constexpr_evaluation_checker c; |
7990 | r = cxx_eval_outermost_constant_expr (t, true, true, false, false, decl); | |
595278be MM |
7991 | gcc_checking_assert (r == t |
7992 | || CONVERT_EXPR_P (t) | |
7993 | || TREE_CODE (t) == VIEW_CONVERT_EXPR | |
7994 | || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) | |
7995 | || !cp_tree_equal (r, t)); | |
f65a3299 PP |
7996 | if (!c.evaluation_restricted_p ()) |
7997 | cv_cache->put (t, r); | |
2d76680f PC |
7998 | return r; |
7999 | } | |
8000 | ||
7a7ac32a PP |
8001 | /* Dispose of the whole CV_CACHE. */ |
8002 | ||
8003 | static void | |
8004 | clear_cv_cache (void) | |
8005 | { | |
8006 | if (cv_cache != NULL) | |
8007 | cv_cache->empty (); | |
8008 | } | |
8009 | ||
bebabf70 | 8010 | /* Dispose of the whole CV_CACHE and FOLD_CACHE. */ |
1e297006 MP |
8011 | |
8012 | void | |
bebabf70 | 8013 | clear_cv_and_fold_caches () |
1e297006 | 8014 | { |
7a7ac32a | 8015 | clear_cv_cache (); |
eba9e839 | 8016 | clear_fold_cache (); |
1e297006 MP |
8017 | } |
8018 | ||
a81c8e8c MP |
8019 | /* Internal function handling expressions in templates for |
8020 | fold_non_dependent_expr and fold_non_dependent_init. | |
8021 | ||
8022 | If we're in a template, but T isn't value dependent, simplify | |
8023 | it. We're supposed to treat: | |
8024 | ||
8025 | template <typename T> void f(T[1 + 1]); | |
8026 | template <typename T> void f(T[2]); | |
8027 | ||
8028 | as two declarations of the same function, for example. */ | |
8029 | ||
8030 | static tree | |
8031 | fold_non_dependent_expr_template (tree t, tsubst_flags_t complain, | |
f968ef9b JJ |
8032 | bool manifestly_const_eval, |
8033 | tree object) | |
a81c8e8c MP |
8034 | { |
8035 | gcc_assert (processing_template_decl); | |
8036 | ||
8037 | if (is_nondependent_constant_expression (t)) | |
8038 | { | |
8039 | processing_template_decl_sentinel s; | |
8040 | t = instantiate_non_dependent_expr_internal (t, complain); | |
8041 | ||
8042 | if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t)) | |
8043 | { | |
8044 | if (TREE_OVERFLOW_P (t)) | |
8045 | { | |
8046 | t = build_nop (TREE_TYPE (t), t); | |
8047 | TREE_CONSTANT (t) = false; | |
8048 | } | |
8049 | return t; | |
8050 | } | |
8051 | ||
4df7f8c7 JM |
8052 | if (cp_unevaluated_operand && !manifestly_const_eval) |
8053 | return t; | |
8054 | ||
a81c8e8c MP |
8055 | tree r = cxx_eval_outermost_constant_expr (t, true, true, |
8056 | manifestly_const_eval, | |
f968ef9b | 8057 | false, object); |
a81c8e8c MP |
8058 | /* cp_tree_equal looks through NOPs, so allow them. */ |
8059 | gcc_checking_assert (r == t | |
8060 | || CONVERT_EXPR_P (t) | |
8061 | || TREE_CODE (t) == VIEW_CONVERT_EXPR | |
8062 | || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) | |
8063 | || !cp_tree_equal (r, t)); | |
8064 | return r; | |
8065 | } | |
8066 | else if (TREE_OVERFLOW_P (t)) | |
8067 | { | |
8068 | t = build_nop (TREE_TYPE (t), t); | |
8069 | TREE_CONSTANT (t) = false; | |
8070 | } | |
8071 | ||
8072 | return t; | |
8073 | } | |
8074 | ||
234bef96 PC |
8075 | /* Like maybe_constant_value but first fully instantiate the argument. |
8076 | ||
8077 | Note: this is equivalent to instantiate_non_dependent_expr_sfinae | |
e56f6629 JM |
8078 | (t, complain) followed by maybe_constant_value but is more efficient, |
8079 | because it calls instantiation_dependent_expression_p and | |
8080 | potential_constant_expression at most once. | |
4cd3e7df | 8081 | The manifestly_const_eval argument is passed to maybe_constant_value. |
e56f6629 JM |
8082 | |
8083 | Callers should generally pass their active complain, or if they are in a | |
8084 | non-template, diagnosing context, they can use the default of | |
8085 | tf_warning_or_error. Callers that might be within a template context, don't | |
8086 | have a complain parameter, and aren't going to remember the result for long | |
8087 | (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node | |
8088 | appropriately. */ | |
234bef96 PC |
8089 | |
8090 | tree | |
e56f6629 | 8091 | fold_non_dependent_expr (tree t, |
4cd3e7df | 8092 | tsubst_flags_t complain /* = tf_warning_or_error */, |
f968ef9b JJ |
8093 | bool manifestly_const_eval /* = false */, |
8094 | tree object /* = NULL_TREE */) | |
234bef96 PC |
8095 | { |
8096 | if (t == NULL_TREE) | |
8097 | return NULL_TREE; | |
8098 | ||
a81c8e8c MP |
8099 | if (processing_template_decl) |
8100 | return fold_non_dependent_expr_template (t, complain, | |
f968ef9b | 8101 | manifestly_const_eval, object); |
234bef96 | 8102 | |
f968ef9b | 8103 | return maybe_constant_value (t, object, manifestly_const_eval); |
a81c8e8c | 8104 | } |
234bef96 | 8105 | |
ae2ebf01 MP |
8106 | /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant, |
8107 | return the original expression. */ | |
8108 | ||
8109 | tree | |
8110 | maybe_fold_non_dependent_expr (tree expr, | |
8111 | tsubst_flags_t complain/*=tf_warning_or_error*/) | |
8112 | { | |
8113 | tree t = fold_non_dependent_expr (expr, complain); | |
8114 | if (t && TREE_CONSTANT (t)) | |
8115 | return t; | |
8116 | ||
8117 | return expr; | |
8118 | } | |
234bef96 | 8119 | |
a81c8e8c | 8120 | /* Like maybe_constant_init but first fully instantiate the argument. */ |
234bef96 | 8121 | |
a81c8e8c MP |
8122 | tree |
8123 | fold_non_dependent_init (tree t, | |
8124 | tsubst_flags_t complain /*=tf_warning_or_error*/, | |
69bf1c7d MP |
8125 | bool manifestly_const_eval /*=false*/, |
8126 | tree object /* = NULL_TREE */) | |
a81c8e8c MP |
8127 | { |
8128 | if (t == NULL_TREE) | |
8129 | return NULL_TREE; | |
8130 | ||
8131 | if (processing_template_decl) | |
8132 | { | |
8133 | t = fold_non_dependent_expr_template (t, complain, | |
69bf1c7d | 8134 | manifestly_const_eval, object); |
a81c8e8c MP |
8135 | /* maybe_constant_init does this stripping, so do it here too. */ |
8136 | if (TREE_CODE (t) == TARGET_EXPR) | |
234bef96 | 8137 | { |
a81c8e8c MP |
8138 | tree init = TARGET_EXPR_INITIAL (t); |
8139 | if (TREE_CODE (init) == CONSTRUCTOR) | |
8140 | t = init; | |
234bef96 PC |
8141 | } |
8142 | return t; | |
8143 | } | |
8144 | ||
69bf1c7d | 8145 | return maybe_constant_init (t, object, manifestly_const_eval); |
234bef96 PC |
8146 | } |
8147 | ||
2d76680f | 8148 | /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather |
e4082611 JJ |
8149 | than wrapped in a TARGET_EXPR. |
8150 | ALLOW_NON_CONSTANT is false if T is required to be a constant expression. | |
13de99bc | 8151 | MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as |
e4082611 | 8152 | per P0595 even when ALLOW_NON_CONSTANT is true. */ |
2d76680f | 8153 | |
118cd6ba | 8154 | static tree |
e4082611 | 8155 | maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant, |
13de99bc | 8156 | bool manifestly_const_eval) |
2d76680f | 8157 | { |
cda0a029 JM |
8158 | if (!t) |
8159 | return t; | |
2d76680f PC |
8160 | if (TREE_CODE (t) == EXPR_STMT) |
8161 | t = TREE_OPERAND (t, 0); | |
8162 | if (TREE_CODE (t) == CONVERT_EXPR | |
8163 | && VOID_TYPE_P (TREE_TYPE (t))) | |
8164 | t = TREE_OPERAND (t, 0); | |
60813a46 JM |
8165 | if (TREE_CODE (t) == INIT_EXPR) |
8166 | t = TREE_OPERAND (t, 1); | |
f64e0c02 JM |
8167 | if (TREE_CODE (t) == TARGET_EXPR) |
8168 | t = TARGET_EXPR_INITIAL (t); | |
a0ab7ccd | 8169 | if (!is_nondependent_static_init_expression (t)) |
69eb4fde | 8170 | /* Don't try to evaluate it. */; |
118cd6ba | 8171 | else if (CONSTANT_CLASS_P (t) && allow_non_constant) |
8a87daca | 8172 | /* No evaluation needed. */; |
69eb4fde | 8173 | else |
e4082611 | 8174 | t = cxx_eval_outermost_constant_expr (t, allow_non_constant, |
38295573 | 8175 | /*strict*/false, |
8e007055 | 8176 | manifestly_const_eval, false, decl); |
2d76680f PC |
8177 | if (TREE_CODE (t) == TARGET_EXPR) |
8178 | { | |
8179 | tree init = TARGET_EXPR_INITIAL (t); | |
8180 | if (TREE_CODE (init) == CONSTRUCTOR) | |
8181 | t = init; | |
8182 | } | |
8183 | return t; | |
8184 | } | |
8185 | ||
118cd6ba MP |
8186 | /* Wrapper for maybe_constant_init_1 which permits non constants. */ |
8187 | ||
8188 | tree | |
13de99bc | 8189 | maybe_constant_init (tree t, tree decl, bool manifestly_const_eval) |
118cd6ba | 8190 | { |
13de99bc | 8191 | return maybe_constant_init_1 (t, decl, true, manifestly_const_eval); |
118cd6ba MP |
8192 | } |
8193 | ||
8194 | /* Wrapper for maybe_constant_init_1 which does not permit non constants. */ | |
8195 | ||
8196 | tree | |
8197 | cxx_constant_init (tree t, tree decl) | |
8198 | { | |
e4082611 | 8199 | return maybe_constant_init_1 (t, decl, false, true); |
118cd6ba MP |
8200 | } |
8201 | ||
2d76680f PC |
8202 | #if 0 |
8203 | /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */ | |
8204 | /* Return true if the object referred to by REF has automatic or thread | |
8205 | local storage. */ | |
8206 | ||
8207 | enum { ck_ok, ck_bad, ck_unknown }; | |
8208 | static int | |
8209 | check_automatic_or_tls (tree ref) | |
8210 | { | |
ef4bddc2 | 8211 | machine_mode mode; |
f37fac2b | 8212 | poly_int64 bitsize, bitpos; |
2d76680f PC |
8213 | tree offset; |
8214 | int volatilep = 0, unsignedp = 0; | |
8215 | tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset, | |
8216 | &mode, &unsignedp, &volatilep, false); | |
8217 | duration_kind dk; | |
8218 | ||
8219 | /* If there isn't a decl in the middle, we don't know the linkage here, | |
8220 | and this isn't a constant expression anyway. */ | |
8221 | if (!DECL_P (decl)) | |
8222 | return ck_unknown; | |
8223 | dk = decl_storage_duration (decl); | |
8224 | return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok; | |
8225 | } | |
8226 | #endif | |
8227 | ||
c7a53bdb JJ |
8228 | /* Data structure for passing data from potential_constant_expression_1 |
8229 | to check_for_return_continue via cp_walk_tree. */ | |
8230 | struct check_for_return_continue_data { | |
8231 | hash_set<tree> *pset; | |
8232 | tree continue_stmt; | |
0fb7aa20 | 8233 | tree break_stmt; |
c7a53bdb JJ |
8234 | }; |
8235 | ||
8236 | /* Helper function for potential_constant_expression_1 SWITCH_STMT handling, | |
8237 | called through cp_walk_tree. Return the first RETURN_EXPR found, or note | |
0fb7aa20 | 8238 | the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */ |
c7a53bdb JJ |
8239 | static tree |
8240 | check_for_return_continue (tree *tp, int *walk_subtrees, void *data) | |
8241 | { | |
0fb7aa20 | 8242 | tree t = *tp, s, b; |
c7a53bdb JJ |
8243 | check_for_return_continue_data *d = (check_for_return_continue_data *) data; |
8244 | switch (TREE_CODE (t)) | |
8245 | { | |
8246 | case RETURN_EXPR: | |
8247 | return t; | |
8248 | ||
8249 | case CONTINUE_STMT: | |
8250 | if (d->continue_stmt == NULL_TREE) | |
8251 | d->continue_stmt = t; | |
8252 | break; | |
8253 | ||
0fb7aa20 JJ |
8254 | case BREAK_STMT: |
8255 | if (d->break_stmt == NULL_TREE) | |
8256 | d->break_stmt = t; | |
8257 | break; | |
8258 | ||
c7a53bdb JJ |
8259 | #define RECUR(x) \ |
8260 | if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \ | |
8261 | d->pset)) \ | |
8262 | return r | |
8263 | ||
8264 | /* For loops, walk subtrees manually, so that continue stmts found | |
8265 | inside of the bodies of the loops are ignored. */ | |
8266 | case DO_STMT: | |
8267 | *walk_subtrees = 0; | |
8268 | RECUR (DO_COND (t)); | |
8269 | s = d->continue_stmt; | |
0fb7aa20 | 8270 | b = d->break_stmt; |
c7a53bdb JJ |
8271 | RECUR (DO_BODY (t)); |
8272 | d->continue_stmt = s; | |
0fb7aa20 | 8273 | d->break_stmt = b; |
c7a53bdb JJ |
8274 | break; |
8275 | ||
8276 | case WHILE_STMT: | |
8277 | *walk_subtrees = 0; | |
8278 | RECUR (WHILE_COND (t)); | |
8279 | s = d->continue_stmt; | |
0fb7aa20 | 8280 | b = d->break_stmt; |
c7a53bdb JJ |
8281 | RECUR (WHILE_BODY (t)); |
8282 | d->continue_stmt = s; | |
0fb7aa20 | 8283 | d->break_stmt = b; |
c7a53bdb JJ |
8284 | break; |
8285 | ||
8286 | case FOR_STMT: | |
8287 | *walk_subtrees = 0; | |
8288 | RECUR (FOR_INIT_STMT (t)); | |
8289 | RECUR (FOR_COND (t)); | |
8290 | RECUR (FOR_EXPR (t)); | |
8291 | s = d->continue_stmt; | |
0fb7aa20 | 8292 | b = d->break_stmt; |
c7a53bdb JJ |
8293 | RECUR (FOR_BODY (t)); |
8294 | d->continue_stmt = s; | |
0fb7aa20 | 8295 | d->break_stmt = b; |
c7a53bdb JJ |
8296 | break; |
8297 | ||
8298 | case RANGE_FOR_STMT: | |
8299 | *walk_subtrees = 0; | |
8300 | RECUR (RANGE_FOR_EXPR (t)); | |
8301 | s = d->continue_stmt; | |
0fb7aa20 | 8302 | b = d->break_stmt; |
c7a53bdb JJ |
8303 | RECUR (RANGE_FOR_BODY (t)); |
8304 | d->continue_stmt = s; | |
0fb7aa20 JJ |
8305 | d->break_stmt = b; |
8306 | break; | |
8307 | ||
8308 | case SWITCH_STMT: | |
8309 | *walk_subtrees = 0; | |
8310 | RECUR (SWITCH_STMT_COND (t)); | |
8311 | b = d->break_stmt; | |
8312 | RECUR (SWITCH_STMT_BODY (t)); | |
8313 | d->break_stmt = b; | |
c7a53bdb JJ |
8314 | break; |
8315 | #undef RECUR | |
8316 | ||
8317 | case STATEMENT_LIST: | |
8318 | case CONSTRUCTOR: | |
8319 | break; | |
8320 | ||
8321 | default: | |
8322 | if (!EXPR_P (t)) | |
8323 | *walk_subtrees = 0; | |
8324 | break; | |
8325 | } | |
8326 | ||
8327 | return NULL_TREE; | |
8328 | } | |
8329 | ||
2d76680f PC |
8330 | /* Return true if T denotes a potentially constant expression. Issue |
8331 | diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true, | |
a0ab7ccd JM |
8332 | an lvalue-rvalue conversion is implied. If NOW is true, we want to |
8333 | consider the expression in the current context, independent of constexpr | |
8334 | substitution. | |
2d76680f PC |
8335 | |
8336 | C++0x [expr.const] used to say | |
8337 | ||
8338 | 6 An expression is a potential constant expression if it is | |
8339 | a constant expression where all occurrences of function | |
8340 | parameters are replaced by arbitrary constant expressions | |
8341 | of the appropriate type. | |
8342 | ||
8343 | 2 A conditional expression is a constant expression unless it | |
8344 | involves one of the following as a potentially evaluated | |
8345 | subexpression (3.2), but subexpressions of logical AND (5.14), | |
8346 | logical OR (5.15), and conditional (5.16) operations that are | |
8347 | not evaluated are not considered. */ | |
8348 | ||
8349 | static bool | |
a0ab7ccd | 8350 | potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, |
3075affd | 8351 | tsubst_flags_t flags, tree *jump_target) |
2d76680f | 8352 | { |
a0ab7ccd | 8353 | #define RECUR(T,RV) \ |
3075affd | 8354 | potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target) |
a0ab7ccd | 8355 | |
2d76680f PC |
8356 | enum { any = false, rval = true }; |
8357 | int i; | |
8358 | tree tmp; | |
8359 | ||
8360 | if (t == error_mark_node) | |
8361 | return false; | |
8362 | if (t == NULL_TREE) | |
8363 | return true; | |
f9d0ca40 | 8364 | location_t loc = cp_expr_loc_or_input_loc (t); |
3075affd JM |
8365 | |
8366 | if (*jump_target) | |
8367 | /* If we are jumping, ignore everything. This is simpler than the | |
8368 | cxx_eval_constant_expression handling because we only need to be | |
8369 | conservatively correct, and we don't necessarily have a constant value | |
8370 | available, so we don't bother with switch tracking. */ | |
8371 | return true; | |
8372 | ||
3c393a2c | 8373 | if (TREE_THIS_VOLATILE (t) && want_rval) |
2d76680f PC |
8374 | { |
8375 | if (flags & tf_error) | |
3c393a2c MP |
8376 | error_at (loc, "lvalue-to-rvalue conversion of a volatile lvalue " |
8377 | "%qE with type %qT", t, TREE_TYPE (t)); | |
2d76680f PC |
8378 | return false; |
8379 | } | |
8380 | if (CONSTANT_CLASS_P (t)) | |
8381 | return true; | |
7dc2b4a2 JM |
8382 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED) |
8383 | && TREE_TYPE (t) == error_mark_node) | |
8384 | return false; | |
2d76680f PC |
8385 | |
8386 | switch (TREE_CODE (t)) | |
8387 | { | |
8388 | case FUNCTION_DECL: | |
8389 | case BASELINK: | |
8390 | case TEMPLATE_DECL: | |
8391 | case OVERLOAD: | |
8392 | case TEMPLATE_ID_EXPR: | |
8393 | case LABEL_DECL: | |
60813a46 | 8394 | case CASE_LABEL_EXPR: |
81012684 | 8395 | case PREDICT_EXPR: |
2d76680f PC |
8396 | case CONST_DECL: |
8397 | case SIZEOF_EXPR: | |
8398 | case ALIGNOF_EXPR: | |
8399 | case OFFSETOF_EXPR: | |
8400 | case NOEXCEPT_EXPR: | |
8401 | case TEMPLATE_PARM_INDEX: | |
8402 | case TRAIT_EXPR: | |
8403 | case IDENTIFIER_NODE: | |
8404 | case USERDEF_LITERAL: | |
8405 | /* We can see a FIELD_DECL in a pointer-to-member expression. */ | |
8406 | case FIELD_DECL: | |
cda0a029 | 8407 | case RESULT_DECL: |
2d76680f | 8408 | case USING_DECL: |
60813a46 | 8409 | case USING_STMT: |
3e605b20 | 8410 | case PLACEHOLDER_EXPR: |
971e17ff | 8411 | case REQUIRES_EXPR: |
98e5a19a | 8412 | case STATIC_ASSERT: |
96a95ac1 | 8413 | case DEBUG_BEGIN_STMT: |
2d76680f PC |
8414 | return true; |
8415 | ||
3075affd JM |
8416 | case RETURN_EXPR: |
8417 | if (!RECUR (TREE_OPERAND (t, 0), any)) | |
8418 | return false; | |
8419 | /* FALLTHROUGH */ | |
8420 | ||
8421 | case BREAK_STMT: | |
8422 | case CONTINUE_STMT: | |
8423 | *jump_target = t; | |
8424 | return true; | |
8425 | ||
a0ab7ccd | 8426 | case PARM_DECL: |
8fda2c27 | 8427 | if (now && want_rval) |
a0ab7ccd | 8428 | { |
8fda2c27 | 8429 | tree type = TREE_TYPE (t); |
9eb7d0d7 | 8430 | if ((processing_template_decl && !COMPLETE_TYPE_P (type)) |
61466206 | 8431 | || dependent_type_p (type) |
8fda2c27 JM |
8432 | || is_really_empty_class (type, /*ignore_vptr*/false)) |
8433 | /* An empty class has no data to read. */ | |
8434 | return true; | |
a0ab7ccd JM |
8435 | if (flags & tf_error) |
8436 | error ("%qE is not a constant expression", t); | |
8437 | return false; | |
8438 | } | |
8439 | return true; | |
8440 | ||
2d76680f PC |
8441 | case AGGR_INIT_EXPR: |
8442 | case CALL_EXPR: | |
8443 | /* -- an invocation of a function other than a constexpr function | |
8444 | or a constexpr constructor. */ | |
8445 | { | |
8446 | tree fun = get_function_named_in_call (t); | |
8447 | const int nargs = call_expr_nargs (t); | |
8448 | i = 0; | |
8449 | ||
60813a46 JM |
8450 | if (fun == NULL_TREE) |
8451 | { | |
44a845ca MS |
8452 | /* Reset to allow the function to continue past the end |
8453 | of the block below. Otherwise return early. */ | |
8454 | bool bail = true; | |
8455 | ||
35228ac7 JJ |
8456 | if (TREE_CODE (t) == CALL_EXPR |
8457 | && CALL_EXPR_FN (t) == NULL_TREE) | |
8458 | switch (CALL_EXPR_IFN (t)) | |
8459 | { | |
8460 | /* These should be ignored, they are optimized away from | |
8461 | constexpr functions. */ | |
8462 | case IFN_UBSAN_NULL: | |
8463 | case IFN_UBSAN_BOUNDS: | |
8464 | case IFN_UBSAN_VPTR: | |
81fea426 | 8465 | case IFN_FALLTHROUGH: |
35228ac7 | 8466 | return true; |
44a845ca MS |
8467 | |
8468 | case IFN_ADD_OVERFLOW: | |
8469 | case IFN_SUB_OVERFLOW: | |
8470 | case IFN_MUL_OVERFLOW: | |
e16f1cc7 | 8471 | case IFN_LAUNDER: |
d8fcab68 | 8472 | case IFN_VEC_CONVERT: |
44a845ca | 8473 | bail = false; |
d8fcab68 | 8474 | break; |
44a845ca | 8475 | |
35228ac7 JJ |
8476 | default: |
8477 | break; | |
8478 | } | |
44a845ca MS |
8479 | |
8480 | if (bail) | |
8481 | { | |
8482 | /* fold_call_expr can't do anything with IFN calls. */ | |
8483 | if (flags & tf_error) | |
e40b6fc7 | 8484 | error_at (loc, "call to internal function %qE", t); |
44a845ca MS |
8485 | return false; |
8486 | } | |
60813a46 | 8487 | } |
44a845ca MS |
8488 | |
8489 | if (fun && is_overloaded_fn (fun)) | |
2d76680f PC |
8490 | { |
8491 | if (TREE_CODE (fun) == FUNCTION_DECL) | |
8492 | { | |
8493 | if (builtin_valid_in_constant_expr_p (fun)) | |
8494 | return true; | |
87c2080b | 8495 | if (!maybe_constexpr_fn (fun) |
2d76680f PC |
8496 | /* Allow any built-in function; if the expansion |
8497 | isn't constant, we'll deal with that then. */ | |
8e007055 | 8498 | && !fndecl_built_in_p (fun) |
b04445d4 | 8499 | /* In C++20, replaceable global allocation functions |
8e007055 | 8500 | are constant expressions. */ |
8412b939 JJ |
8501 | && (!cxx_replaceable_global_alloc_fn (fun) |
8502 | || TREE_CODE (t) != CALL_EXPR | |
8503 | || (!CALL_FROM_NEW_OR_DELETE_P (t) | |
8504 | && (current_function_decl == NULL_TREE | |
8505 | || !is_std_allocator_allocate | |
8506 | (current_function_decl)))) | |
cf650568 JJ |
8507 | /* Allow placement new in std::construct_at. */ |
8508 | && (!cxx_placement_new_fn (fun) | |
8412b939 | 8509 | || TREE_CODE (t) != CALL_EXPR |
cf650568 | 8510 | || current_function_decl == NULL_TREE |
22edf943 MP |
8511 | || !is_std_construct_at (current_function_decl)) |
8512 | && !cxx_dynamic_cast_fn_p (fun)) | |
2d76680f PC |
8513 | { |
8514 | if (flags & tf_error) | |
8515 | { | |
84fa214d | 8516 | error_at (loc, "call to non-%<constexpr%> function %qD", |
e40b6fc7 | 8517 | fun); |
2d76680f PC |
8518 | explain_invalid_constexpr_fn (fun); |
8519 | } | |
8520 | return false; | |
8521 | } | |
8522 | /* A call to a non-static member function takes the address | |
8523 | of the object as the first argument. But in a constant | |
8524 | expression the address will be folded away, so look | |
8525 | through it now. */ | |
8526 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) | |
8527 | && !DECL_CONSTRUCTOR_P (fun)) | |
8528 | { | |
8529 | tree x = get_nth_callarg (t, 0); | |
8530 | if (is_this_parameter (x)) | |
3e605b20 | 8531 | return true; |
a0ab7ccd JM |
8532 | /* Don't require an immediately constant value, as |
8533 | constexpr substitution might not use the value. */ | |
8534 | bool sub_now = false; | |
8535 | if (!potential_constant_expression_1 (x, rval, strict, | |
3075affd JM |
8536 | sub_now, flags, |
8537 | jump_target)) | |
2d76680f PC |
8538 | return false; |
8539 | i = 1; | |
8540 | } | |
8541 | } | |
8542 | else | |
8543 | { | |
69eb4fde | 8544 | if (!RECUR (fun, true)) |
2d76680f PC |
8545 | return false; |
8546 | fun = get_first_fn (fun); | |
8547 | } | |
8548 | /* Skip initial arguments to base constructors. */ | |
8549 | if (DECL_BASE_CONSTRUCTOR_P (fun)) | |
8550 | i = num_artificial_parms_for (fun); | |
8551 | fun = DECL_ORIGIN (fun); | |
8552 | } | |
44a845ca | 8553 | else if (fun) |
2d76680f | 8554 | { |
69eb4fde | 8555 | if (RECUR (fun, rval)) |
2d76680f PC |
8556 | /* Might end up being a constant function pointer. */; |
8557 | else | |
8558 | return false; | |
8559 | } | |
8560 | for (; i < nargs; ++i) | |
8561 | { | |
8562 | tree x = get_nth_callarg (t, i); | |
c3c29ba5 JM |
8563 | /* In a template, reference arguments haven't been converted to |
8564 | REFERENCE_TYPE and we might not even know if the parameter | |
8565 | is a reference, so accept lvalue constants too. */ | |
8566 | bool rv = processing_template_decl ? any : rval; | |
a0ab7ccd JM |
8567 | /* Don't require an immediately constant value, as constexpr |
8568 | substitution might not use the value of the argument. */ | |
8569 | bool sub_now = false; | |
8570 | if (!potential_constant_expression_1 (x, rv, strict, | |
3075affd | 8571 | sub_now, flags, jump_target)) |
2d76680f PC |
8572 | return false; |
8573 | } | |
8574 | return true; | |
8575 | } | |
8576 | ||
8577 | case NON_LVALUE_EXPR: | |
8578 | /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to | |
8579 | -- an lvalue of integral type that refers to a non-volatile | |
8580 | const variable or static data member initialized with | |
8581 | constant expressions, or | |
8582 | ||
8583 | -- an lvalue of literal type that refers to non-volatile | |
8584 | object defined with constexpr, or that refers to a | |
8585 | sub-object of such an object; */ | |
69eb4fde | 8586 | return RECUR (TREE_OPERAND (t, 0), rval); |
2d76680f PC |
8587 | |
8588 | case VAR_DECL: | |
70f40fea | 8589 | if (DECL_HAS_VALUE_EXPR_P (t)) |
c1051bf7 | 8590 | { |
68ad1bf7 | 8591 | if (now && is_normal_capture_proxy (t)) |
c1051bf7 JM |
8592 | { |
8593 | /* -- in a lambda-expression, a reference to this or to a | |
8594 | variable with automatic storage duration defined outside that | |
8595 | lambda-expression, where the reference would be an | |
8596 | odr-use. */ | |
04b89192 JM |
8597 | |
8598 | if (want_rval) | |
8599 | /* Since we're doing an lvalue-rvalue conversion, this might | |
8600 | not be an odr-use, so evaluate the variable directly. */ | |
8601 | return RECUR (DECL_CAPTURED_VARIABLE (t), rval); | |
8602 | ||
c1051bf7 JM |
8603 | if (flags & tf_error) |
8604 | { | |
8605 | tree cap = DECL_CAPTURED_VARIABLE (t); | |
8606 | error ("lambda capture of %qE is not a constant expression", | |
8607 | cap); | |
04b89192 | 8608 | if (decl_constant_var_p (cap)) |
c1051bf7 JM |
8609 | inform (input_location, "because it is used as a glvalue"); |
8610 | } | |
8611 | return false; | |
8612 | } | |
c2856cee MP |
8613 | /* Treat __PRETTY_FUNCTION__ inside a template function as |
8614 | potentially-constant. */ | |
8615 | else if (DECL_PRETTY_FUNCTION_P (t) | |
8616 | && DECL_VALUE_EXPR (t) == error_mark_node) | |
8617 | return true; | |
c1051bf7 JM |
8618 | return RECUR (DECL_VALUE_EXPR (t), rval); |
8619 | } | |
69eb4fde | 8620 | if (want_rval |
98e5a19a | 8621 | && !var_in_maybe_constexpr_fn (t) |
7dc2b4a2 | 8622 | && !type_dependent_expression_p (t) |
4b691b13 | 8623 | && !decl_maybe_constant_var_p (t) |
69eb4fde JM |
8624 | && (strict |
8625 | || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t)) | |
4b691b13 JM |
8626 | || (DECL_INITIAL (t) |
8627 | && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))) | |
7dc2b4a2 | 8628 | && COMPLETE_TYPE_P (TREE_TYPE (t)) |
dbcd32f8 | 8629 | && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
2d76680f PC |
8630 | { |
8631 | if (flags & tf_error) | |
6821245b | 8632 | non_const_var_error (loc, t); |
2d76680f PC |
8633 | return false; |
8634 | } | |
8635 | return true; | |
8636 | ||
8637 | case NOP_EXPR: | |
ed3ea9f2 JJ |
8638 | if (REINTERPRET_CAST_P (t)) |
8639 | { | |
8640 | if (flags & tf_error) | |
a9c697b8 | 8641 | error_at (loc, "%<reinterpret_cast%> is not a constant expression"); |
ed3ea9f2 JJ |
8642 | return false; |
8643 | } | |
8644 | /* FALLTHRU */ | |
2d76680f PC |
8645 | case CONVERT_EXPR: |
8646 | case VIEW_CONVERT_EXPR: | |
8647 | /* -- a reinterpret_cast. FIXME not implemented, and this rule | |
8648 | may change to something more specific to type-punning (DR 1312). */ | |
8649 | { | |
8650 | tree from = TREE_OPERAND (t, 0); | |
dfd7fdca DM |
8651 | if (location_wrapper_p (t)) |
8652 | return (RECUR (from, want_rval)); | |
8653 | if (INDIRECT_TYPE_P (TREE_TYPE (t))) | |
2d76680f | 8654 | { |
dfd7fdca DM |
8655 | STRIP_ANY_LOCATION_WRAPPER (from); |
8656 | if (TREE_CODE (from) == INTEGER_CST | |
8657 | && !integer_zerop (from)) | |
8658 | { | |
8659 | if (flags & tf_error) | |
a9c697b8 MS |
8660 | error_at (loc, |
8661 | "%<reinterpret_cast%> from integer to pointer"); | |
dfd7fdca DM |
8662 | return false; |
8663 | } | |
2d76680f | 8664 | } |
69eb4fde | 8665 | return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR)); |
2d76680f PC |
8666 | } |
8667 | ||
be845b04 JJ |
8668 | case ADDRESSOF_EXPR: |
8669 | /* This is like ADDR_EXPR, except it won't form pointer-to-member. */ | |
8670 | t = TREE_OPERAND (t, 0); | |
8671 | goto handle_addr_expr; | |
8672 | ||
2d76680f PC |
8673 | case ADDR_EXPR: |
8674 | /* -- a unary operator & that is applied to an lvalue that | |
8675 | designates an object with thread or automatic storage | |
8676 | duration; */ | |
8677 | t = TREE_OPERAND (t, 0); | |
8678 | ||
8679 | if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t)) | |
8680 | /* A pointer-to-member constant. */ | |
8681 | return true; | |
8682 | ||
be845b04 | 8683 | handle_addr_expr: |
2d76680f PC |
8684 | #if 0 |
8685 | /* FIXME adjust when issue 1197 is fully resolved. For now don't do | |
8686 | any checking here, as we might dereference the pointer later. If | |
8687 | we remove this code, also remove check_automatic_or_tls. */ | |
8688 | i = check_automatic_or_tls (t); | |
8689 | if (i == ck_ok) | |
8690 | return true; | |
8691 | if (i == ck_bad) | |
8692 | { | |
8693 | if (flags & tf_error) | |
8694 | error ("address-of an object %qE with thread local or " | |
8695 | "automatic storage is not a constant expression", t); | |
8696 | return false; | |
8697 | } | |
8698 | #endif | |
69eb4fde | 8699 | return RECUR (t, any); |
2d76680f PC |
8700 | |
8701 | case COMPONENT_REF: | |
2d76680f PC |
8702 | case ARROW_EXPR: |
8703 | case OFFSET_REF: | |
8704 | /* -- a class member access unless its postfix-expression is | |
8705 | of literal type or of pointer to literal type. */ | |
8706 | /* This test would be redundant, as it follows from the | |
8707 | postfix-expression being a potential constant expression. */ | |
19dedccf JM |
8708 | if (type_unknown_p (t)) |
8709 | return true; | |
8fda2c27 JM |
8710 | if (is_overloaded_fn (t)) |
8711 | /* In a template, a COMPONENT_REF of a function expresses ob.fn(), | |
8712 | which uses ob as an lvalue. */ | |
8713 | want_rval = false; | |
8714 | gcc_fallthrough (); | |
8715 | ||
8716 | case REALPART_EXPR: | |
8717 | case IMAGPART_EXPR: | |
8718 | case BIT_FIELD_REF: | |
69eb4fde | 8719 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
2d76680f PC |
8720 | |
8721 | case EXPR_PACK_EXPANSION: | |
69eb4fde | 8722 | return RECUR (PACK_EXPANSION_PATTERN (t), want_rval); |
2d76680f PC |
8723 | |
8724 | case INDIRECT_REF: | |
8725 | { | |
8726 | tree x = TREE_OPERAND (t, 0); | |
8727 | STRIP_NOPS (x); | |
948d0d91 | 8728 | if (is_this_parameter (x) && !is_capture_proxy (x)) |
2d76680f | 8729 | { |
5d4e573b | 8730 | if (!var_in_maybe_constexpr_fn (x)) |
2d76680f PC |
8731 | { |
8732 | if (flags & tf_error) | |
e40b6fc7 | 8733 | error_at (loc, "use of %<this%> in a constant expression"); |
2d76680f PC |
8734 | return false; |
8735 | } | |
2d76680f PC |
8736 | return true; |
8737 | } | |
69eb4fde | 8738 | return RECUR (x, rval); |
2d76680f PC |
8739 | } |
8740 | ||
60813a46 | 8741 | case STATEMENT_LIST: |
0f54cc9c JM |
8742 | for (tree stmt : tsi_range (t)) |
8743 | if (!RECUR (stmt, any)) | |
8744 | return false; | |
8745 | return true; | |
60813a46 JM |
8746 | |
8747 | case MODIFY_EXPR: | |
8748 | if (cxx_dialect < cxx14) | |
8749 | goto fail; | |
69eb4fde | 8750 | if (!RECUR (TREE_OPERAND (t, 0), any)) |
60813a46 | 8751 | return false; |
8e007055 JJ |
8752 | /* Just ignore clobbers. */ |
8753 | if (TREE_CLOBBER_P (TREE_OPERAND (t, 1))) | |
8754 | return true; | |
69eb4fde | 8755 | if (!RECUR (TREE_OPERAND (t, 1), rval)) |
60813a46 JM |
8756 | return false; |
8757 | return true; | |
8758 | ||
8759 | case MODOP_EXPR: | |
8760 | if (cxx_dialect < cxx14) | |
8761 | goto fail; | |
69eb4fde | 8762 | if (!RECUR (TREE_OPERAND (t, 0), rval)) |
60813a46 | 8763 | return false; |
69eb4fde | 8764 | if (!RECUR (TREE_OPERAND (t, 2), rval)) |
60813a46 JM |
8765 | return false; |
8766 | return true; | |
8767 | ||
60813a46 | 8768 | case DO_STMT: |
69eb4fde | 8769 | if (!RECUR (DO_COND (t), rval)) |
60813a46 | 8770 | return false; |
69eb4fde | 8771 | if (!RECUR (DO_BODY (t), any)) |
60813a46 | 8772 | return false; |
3075affd JM |
8773 | if (breaks (jump_target) || continues (jump_target)) |
8774 | *jump_target = NULL_TREE; | |
60813a46 JM |
8775 | return true; |
8776 | ||
8777 | case FOR_STMT: | |
69eb4fde | 8778 | if (!RECUR (FOR_INIT_STMT (t), any)) |
60813a46 | 8779 | return false; |
8e9558f0 MP |
8780 | tmp = FOR_COND (t); |
8781 | if (!RECUR (tmp, rval)) | |
60813a46 | 8782 | return false; |
8e9558f0 MP |
8783 | if (tmp) |
8784 | { | |
8785 | if (!processing_template_decl) | |
8786 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
a385474c MP |
8787 | /* If we couldn't evaluate the condition, it might not ever be |
8788 | true. */ | |
8789 | if (!integer_onep (tmp)) | |
0fb7aa20 JJ |
8790 | { |
8791 | /* Before returning true, check if the for body can contain | |
8792 | a return. */ | |
8793 | hash_set<tree> pset; | |
8794 | check_for_return_continue_data data = { &pset, NULL_TREE, | |
8795 | NULL_TREE }; | |
8796 | if (tree ret_expr | |
8797 | = cp_walk_tree (&FOR_BODY (t), check_for_return_continue, | |
8798 | &data, &pset)) | |
8799 | *jump_target = ret_expr; | |
8800 | return true; | |
8801 | } | |
8e9558f0 | 8802 | } |
69eb4fde | 8803 | if (!RECUR (FOR_EXPR (t), any)) |
60813a46 | 8804 | return false; |
69eb4fde | 8805 | if (!RECUR (FOR_BODY (t), any)) |
60813a46 | 8806 | return false; |
3075affd JM |
8807 | if (breaks (jump_target) || continues (jump_target)) |
8808 | *jump_target = NULL_TREE; | |
60813a46 JM |
8809 | return true; |
8810 | ||
98e5a19a | 8811 | case RANGE_FOR_STMT: |
8112667c MP |
8812 | if (!RECUR (RANGE_FOR_INIT_STMT (t), any)) |
8813 | return false; | |
98e5a19a JM |
8814 | if (!RECUR (RANGE_FOR_EXPR (t), any)) |
8815 | return false; | |
8816 | if (!RECUR (RANGE_FOR_BODY (t), any)) | |
8817 | return false; | |
3075affd JM |
8818 | if (breaks (jump_target) || continues (jump_target)) |
8819 | *jump_target = NULL_TREE; | |
98e5a19a JM |
8820 | return true; |
8821 | ||
60813a46 | 8822 | case WHILE_STMT: |
8e9558f0 MP |
8823 | tmp = WHILE_COND (t); |
8824 | if (!RECUR (tmp, rval)) | |
60813a46 | 8825 | return false; |
8e9558f0 MP |
8826 | if (!processing_template_decl) |
8827 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
a385474c MP |
8828 | /* If we couldn't evaluate the condition, it might not ever be true. */ |
8829 | if (!integer_onep (tmp)) | |
0fb7aa20 JJ |
8830 | { |
8831 | /* Before returning true, check if the while body can contain | |
8832 | a return. */ | |
8833 | hash_set<tree> pset; | |
8834 | check_for_return_continue_data data = { &pset, NULL_TREE, | |
8835 | NULL_TREE }; | |
8836 | if (tree ret_expr | |
8837 | = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue, | |
8838 | &data, &pset)) | |
8839 | *jump_target = ret_expr; | |
8840 | return true; | |
8841 | } | |
69eb4fde | 8842 | if (!RECUR (WHILE_BODY (t), any)) |
60813a46 | 8843 | return false; |
3075affd JM |
8844 | if (breaks (jump_target) || continues (jump_target)) |
8845 | *jump_target = NULL_TREE; | |
60813a46 JM |
8846 | return true; |
8847 | ||
8848 | case SWITCH_STMT: | |
69eb4fde | 8849 | if (!RECUR (SWITCH_STMT_COND (t), rval)) |
60813a46 | 8850 | return false; |
ce965730 | 8851 | /* FIXME we don't check SWITCH_STMT_BODY currently, because even |
c7a53bdb JJ |
8852 | unreachable labels would be checked and it is enough if there is |
8853 | a single switch cond value for which it is a valid constant | |
8854 | expression. We need to check if there are any RETURN_EXPRs | |
8855 | or CONTINUE_STMTs inside of the body though, as in that case | |
8856 | we need to set *jump_target. */ | |
8857 | else | |
8858 | { | |
8859 | hash_set<tree> pset; | |
0fb7aa20 JJ |
8860 | check_for_return_continue_data data = { &pset, NULL_TREE, |
8861 | NULL_TREE }; | |
c7a53bdb JJ |
8862 | if (tree ret_expr |
8863 | = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue, | |
8864 | &data, &pset)) | |
8865 | /* The switch might return. */ | |
8866 | *jump_target = ret_expr; | |
8867 | else if (data.continue_stmt) | |
8868 | /* The switch can't return, but might continue. */ | |
8869 | *jump_target = data.continue_stmt; | |
8870 | } | |
60813a46 JM |
8871 | return true; |
8872 | ||
58611fb6 | 8873 | case STMT_EXPR: |
69eb4fde | 8874 | return RECUR (STMT_EXPR_STMT (t), rval); |
58611fb6 | 8875 | |
2d76680f | 8876 | case LAMBDA_EXPR: |
899ac3b8 JM |
8877 | if (cxx_dialect >= cxx17) |
8878 | /* In C++17 lambdas can be constexpr, don't give up yet. */ | |
8879 | return true; | |
8880 | else if (flags & tf_error) | |
8881 | error_at (loc, "lambda-expression is not a constant expression " | |
8882 | "before C++17"); | |
8883 | return false; | |
8884 | ||
2d76680f PC |
8885 | case DYNAMIC_CAST_EXPR: |
8886 | case PSEUDO_DTOR_EXPR: | |
2d76680f PC |
8887 | case NEW_EXPR: |
8888 | case VEC_NEW_EXPR: | |
8889 | case DELETE_EXPR: | |
8890 | case VEC_DELETE_EXPR: | |
8891 | case THROW_EXPR: | |
e40b6fc7 JJ |
8892 | case OMP_PARALLEL: |
8893 | case OMP_TASK: | |
8894 | case OMP_FOR: | |
897064e2 | 8895 | case OMP_SIMD: |
e40b6fc7 JJ |
8896 | case OMP_DISTRIBUTE: |
8897 | case OMP_TASKLOOP: | |
d81ab49d | 8898 | case OMP_LOOP: |
e40b6fc7 JJ |
8899 | case OMP_TEAMS: |
8900 | case OMP_TARGET_DATA: | |
8901 | case OMP_TARGET: | |
8902 | case OMP_SECTIONS: | |
8903 | case OMP_ORDERED: | |
8904 | case OMP_CRITICAL: | |
8905 | case OMP_SINGLE: | |
8906 | case OMP_SECTION: | |
8907 | case OMP_MASTER: | |
4b1e1434 | 8908 | case OMP_MASKED: |
e40b6fc7 JJ |
8909 | case OMP_TASKGROUP: |
8910 | case OMP_TARGET_UPDATE: | |
8911 | case OMP_TARGET_ENTER_DATA: | |
8912 | case OMP_TARGET_EXIT_DATA: | |
2d76680f PC |
8913 | case OMP_ATOMIC: |
8914 | case OMP_ATOMIC_READ: | |
8915 | case OMP_ATOMIC_CAPTURE_OLD: | |
8916 | case OMP_ATOMIC_CAPTURE_NEW: | |
28567c40 | 8917 | case OMP_DEPOBJ: |
e40b6fc7 JJ |
8918 | case OACC_PARALLEL: |
8919 | case OACC_KERNELS: | |
62aee289 | 8920 | case OACC_SERIAL: |
e40b6fc7 JJ |
8921 | case OACC_DATA: |
8922 | case OACC_HOST_DATA: | |
8923 | case OACC_LOOP: | |
8924 | case OACC_CACHE: | |
8925 | case OACC_DECLARE: | |
8926 | case OACC_ENTER_DATA: | |
8927 | case OACC_EXIT_DATA: | |
8928 | case OACC_UPDATE: | |
2d76680f PC |
8929 | /* GCC internal stuff. */ |
8930 | case VA_ARG_EXPR: | |
2d76680f | 8931 | case TRANSACTION_EXPR: |
81b6a6c5 | 8932 | case AT_ENCODE_EXPR: |
60813a46 | 8933 | fail: |
2d76680f | 8934 | if (flags & tf_error) |
e40b6fc7 | 8935 | error_at (loc, "expression %qE is not a constant expression", t); |
2d76680f PC |
8936 | return false; |
8937 | ||
529bc410 | 8938 | case ASM_EXPR: |
7c814975 MP |
8939 | if (flags & tf_error) |
8940 | inline_asm_in_constexpr_error (loc); | |
8941 | return false; | |
529bc410 | 8942 | |
bf8d8309 | 8943 | case OBJ_TYPE_REF: |
b04445d4 JM |
8944 | if (cxx_dialect >= cxx20) |
8945 | /* In C++20 virtual calls can be constexpr, don't give up yet. */ | |
bf8d8309 MP |
8946 | return true; |
8947 | else if (flags & tf_error) | |
a9c697b8 | 8948 | error_at (loc, |
b04445d4 | 8949 | "virtual functions cannot be %<constexpr%> before C++20"); |
bf8d8309 MP |
8950 | return false; |
8951 | ||
2d76680f | 8952 | case TYPEID_EXPR: |
66acfb80 MP |
8953 | /* In C++20, a typeid expression whose operand is of polymorphic |
8954 | class type can be constexpr. */ | |
2d76680f PC |
8955 | { |
8956 | tree e = TREE_OPERAND (t, 0); | |
b04445d4 | 8957 | if (cxx_dialect < cxx20 |
66acfb80 MP |
8958 | && strict |
8959 | && !TYPE_P (e) | |
8960 | && !type_dependent_expression_p (e) | |
2d76680f PC |
8961 | && TYPE_POLYMORPHIC_P (TREE_TYPE (e))) |
8962 | { | |
8963 | if (flags & tf_error) | |
a9c697b8 | 8964 | error_at (loc, "%<typeid%> is not a constant expression " |
e40b6fc7 | 8965 | "because %qE is of polymorphic type", e); |
2d76680f PC |
8966 | return false; |
8967 | } | |
8968 | return true; | |
8969 | } | |
8970 | ||
1af4ebf5 | 8971 | case POINTER_DIFF_EXPR: |
2d76680f | 8972 | case MINUS_EXPR: |
2d76680f PC |
8973 | want_rval = true; |
8974 | goto binary; | |
8975 | ||
8976 | case LT_EXPR: | |
8977 | case LE_EXPR: | |
8978 | case GT_EXPR: | |
8979 | case GE_EXPR: | |
8980 | case EQ_EXPR: | |
8981 | case NE_EXPR: | |
b7689b96 | 8982 | case SPACESHIP_EXPR: |
2d76680f PC |
8983 | want_rval = true; |
8984 | goto binary; | |
8985 | ||
60813a46 JM |
8986 | case PREINCREMENT_EXPR: |
8987 | case POSTINCREMENT_EXPR: | |
8988 | case PREDECREMENT_EXPR: | |
8989 | case POSTDECREMENT_EXPR: | |
8990 | if (cxx_dialect < cxx14) | |
8991 | goto fail; | |
8992 | goto unary; | |
8993 | ||
2d76680f PC |
8994 | case BIT_NOT_EXPR: |
8995 | /* A destructor. */ | |
8996 | if (TYPE_P (TREE_OPERAND (t, 0))) | |
8997 | return true; | |
191816a3 | 8998 | /* fall through. */ |
2d76680f | 8999 | |
2d76680f PC |
9000 | case CONJ_EXPR: |
9001 | case SAVE_EXPR: | |
9002 | case FIX_TRUNC_EXPR: | |
9003 | case FLOAT_EXPR: | |
9004 | case NEGATE_EXPR: | |
9005 | case ABS_EXPR: | |
e197e64e | 9006 | case ABSU_EXPR: |
2d76680f PC |
9007 | case TRUTH_NOT_EXPR: |
9008 | case FIXED_CONVERT_EXPR: | |
9009 | case UNARY_PLUS_EXPR: | |
4856a1f0 PC |
9010 | case UNARY_LEFT_FOLD_EXPR: |
9011 | case UNARY_RIGHT_FOLD_EXPR: | |
60813a46 | 9012 | unary: |
69eb4fde | 9013 | return RECUR (TREE_OPERAND (t, 0), rval); |
2d76680f PC |
9014 | |
9015 | case CAST_EXPR: | |
9016 | case CONST_CAST_EXPR: | |
9017 | case STATIC_CAST_EXPR: | |
9018 | case REINTERPRET_CAST_EXPR: | |
9019 | case IMPLICIT_CONV_EXPR: | |
9020 | if (cxx_dialect < cxx11 | |
9021 | && !dependent_type_p (TREE_TYPE (t)) | |
9022 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))) | |
9023 | /* In C++98, a conversion to non-integral type can't be part of a | |
9024 | constant expression. */ | |
9025 | { | |
9026 | if (flags & tf_error) | |
e40b6fc7 JJ |
9027 | error_at (loc, |
9028 | "cast to non-integral type %qT in a constant expression", | |
9029 | TREE_TYPE (t)); | |
2d76680f PC |
9030 | return false; |
9031 | } | |
fe0604d3 MP |
9032 | /* This might be a conversion from a class to a (potentially) literal |
9033 | type. Let's consider it potentially constant since the conversion | |
9034 | might be a constexpr user-defined conversion. */ | |
9035 | else if (cxx_dialect >= cxx11 | |
9036 | && (dependent_type_p (TREE_TYPE (t)) | |
9037 | || !COMPLETE_TYPE_P (TREE_TYPE (t)) | |
9038 | || literal_type_p (TREE_TYPE (t))) | |
9039 | && TREE_OPERAND (t, 0)) | |
9040 | { | |
9041 | tree type = TREE_TYPE (TREE_OPERAND (t, 0)); | |
9042 | /* If this is a dependent type, it could end up being a class | |
9043 | with conversions. */ | |
9044 | if (type == NULL_TREE || WILDCARD_TYPE_P (type)) | |
9045 | return true; | |
9046 | /* Or a non-dependent class which has conversions. */ | |
9047 | else if (CLASS_TYPE_P (type) | |
9048 | && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type))) | |
9049 | return true; | |
9050 | } | |
2d76680f | 9051 | |
69eb4fde | 9052 | return (RECUR (TREE_OPERAND (t, 0), |
9f613f06 | 9053 | !TYPE_REF_P (TREE_TYPE (t)))); |
2d76680f | 9054 | |
60813a46 | 9055 | case BIND_EXPR: |
69eb4fde | 9056 | return RECUR (BIND_EXPR_BODY (t), want_rval); |
60813a46 | 9057 | |
60813a46 JM |
9058 | case CLEANUP_POINT_EXPR: |
9059 | case MUST_NOT_THROW_EXPR: | |
9060 | case TRY_CATCH_EXPR: | |
769430b2 | 9061 | case TRY_BLOCK: |
60813a46 JM |
9062 | case EH_SPEC_BLOCK: |
9063 | case EXPR_STMT: | |
2d76680f PC |
9064 | case PAREN_EXPR: |
9065 | case NON_DEPENDENT_EXPR: | |
9066 | /* For convenience. */ | |
f937929e JM |
9067 | case LOOP_EXPR: |
9068 | case EXIT_EXPR: | |
69eb4fde | 9069 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
2d76680f | 9070 | |
98e5a19a JM |
9071 | case DECL_EXPR: |
9072 | tmp = DECL_EXPR_DECL (t); | |
9073 | if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)) | |
9074 | { | |
8892d532 | 9075 | if (CP_DECL_THREAD_LOCAL_P (tmp)) |
98e5a19a JM |
9076 | { |
9077 | if (flags & tf_error) | |
9078 | error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared " | |
8892d532 | 9079 | "%<thread_local%> in %<constexpr%> context", tmp); |
98e5a19a JM |
9080 | return false; |
9081 | } | |
8892d532 | 9082 | else if (TREE_STATIC (tmp)) |
98e5a19a JM |
9083 | { |
9084 | if (flags & tf_error) | |
9085 | error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared " | |
8892d532 | 9086 | "%<static%> in %<constexpr%> context", tmp); |
98e5a19a JM |
9087 | return false; |
9088 | } | |
3885527d PC |
9089 | else if (!check_for_uninitialized_const_var |
9090 | (tmp, /*constexpr_context_p=*/true, flags)) | |
9091 | return false; | |
98e5a19a JM |
9092 | } |
9093 | return RECUR (tmp, want_rval); | |
9094 | ||
769430b2 JM |
9095 | case TRY_FINALLY_EXPR: |
9096 | return (RECUR (TREE_OPERAND (t, 0), want_rval) | |
9097 | && RECUR (TREE_OPERAND (t, 1), any)); | |
9098 | ||
2d76680f | 9099 | case SCOPE_REF: |
69eb4fde | 9100 | return RECUR (TREE_OPERAND (t, 1), want_rval); |
2d76680f PC |
9101 | |
9102 | case TARGET_EXPR: | |
d3a9902e JM |
9103 | if (!TARGET_EXPR_DIRECT_INIT_P (t) |
9104 | && !literal_type_p (TREE_TYPE (t))) | |
2d76680f PC |
9105 | { |
9106 | if (flags & tf_error) | |
9107 | { | |
097f82ec | 9108 | auto_diagnostic_group d; |
e40b6fc7 JJ |
9109 | error_at (loc, "temporary of non-literal type %qT in a " |
9110 | "constant expression", TREE_TYPE (t)); | |
2d76680f PC |
9111 | explain_non_literal_class (TREE_TYPE (t)); |
9112 | } | |
9113 | return false; | |
9114 | } | |
191816a3 | 9115 | /* FALLTHRU */ |
2d76680f | 9116 | case INIT_EXPR: |
69eb4fde | 9117 | return RECUR (TREE_OPERAND (t, 1), rval); |
2d76680f PC |
9118 | |
9119 | case CONSTRUCTOR: | |
9120 | { | |
9121 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); | |
9122 | constructor_elt *ce; | |
9123 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
69eb4fde | 9124 | if (!RECUR (ce->value, want_rval)) |
2d76680f PC |
9125 | return false; |
9126 | return true; | |
9127 | } | |
9128 | ||
9129 | case TREE_LIST: | |
9130 | { | |
9131 | gcc_assert (TREE_PURPOSE (t) == NULL_TREE | |
9132 | || DECL_P (TREE_PURPOSE (t))); | |
69eb4fde | 9133 | if (!RECUR (TREE_VALUE (t), want_rval)) |
2d76680f PC |
9134 | return false; |
9135 | if (TREE_CHAIN (t) == NULL_TREE) | |
9136 | return true; | |
69eb4fde | 9137 | return RECUR (TREE_CHAIN (t), want_rval); |
2d76680f PC |
9138 | } |
9139 | ||
9140 | case TRUNC_DIV_EXPR: | |
9141 | case CEIL_DIV_EXPR: | |
9142 | case FLOOR_DIV_EXPR: | |
9143 | case ROUND_DIV_EXPR: | |
9144 | case TRUNC_MOD_EXPR: | |
9145 | case CEIL_MOD_EXPR: | |
9146 | case ROUND_MOD_EXPR: | |
9147 | { | |
9148 | tree denom = TREE_OPERAND (t, 1); | |
69eb4fde | 9149 | if (!RECUR (denom, rval)) |
2d76680f PC |
9150 | return false; |
9151 | /* We can't call cxx_eval_outermost_constant_expr on an expression | |
234bef96 | 9152 | that hasn't been through instantiate_non_dependent_expr yet. */ |
2d76680f PC |
9153 | if (!processing_template_decl) |
9154 | denom = cxx_eval_outermost_constant_expr (denom, true); | |
9155 | if (integer_zerop (denom)) | |
9156 | { | |
9157 | if (flags & tf_error) | |
64d6d399 | 9158 | error ("division by zero is not a constant expression"); |
2d76680f PC |
9159 | return false; |
9160 | } | |
9161 | else | |
9162 | { | |
9163 | want_rval = true; | |
69eb4fde | 9164 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
2d76680f PC |
9165 | } |
9166 | } | |
9167 | ||
9168 | case COMPOUND_EXPR: | |
9169 | { | |
9170 | /* check_return_expr sometimes wraps a TARGET_EXPR in a | |
46fdced6 | 9171 | COMPOUND_EXPR; don't get confused. */ |
2d76680f PC |
9172 | tree op0 = TREE_OPERAND (t, 0); |
9173 | tree op1 = TREE_OPERAND (t, 1); | |
9174 | STRIP_NOPS (op1); | |
46fdced6 | 9175 | if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) |
69eb4fde | 9176 | return RECUR (op0, want_rval); |
2d76680f PC |
9177 | else |
9178 | goto binary; | |
9179 | } | |
9180 | ||
9181 | /* If the first operand is the non-short-circuit constant, look at | |
9182 | the second operand; otherwise we only care about the first one for | |
9183 | potentiality. */ | |
9184 | case TRUTH_AND_EXPR: | |
9185 | case TRUTH_ANDIF_EXPR: | |
9186 | tmp = boolean_true_node; | |
9187 | goto truth; | |
9188 | case TRUTH_OR_EXPR: | |
9189 | case TRUTH_ORIF_EXPR: | |
9190 | tmp = boolean_false_node; | |
9191 | truth: | |
9192 | { | |
9927ecbb PP |
9193 | tree op0 = TREE_OPERAND (t, 0); |
9194 | tree op1 = TREE_OPERAND (t, 1); | |
9195 | if (!RECUR (op0, rval)) | |
2d76680f | 9196 | return false; |
9927ecbb PP |
9197 | if (!(flags & tf_error) && RECUR (op1, rval)) |
9198 | /* When quiet, try to avoid expensive trial evaluation by first | |
9199 | checking potentiality of the second operand. */ | |
9200 | return true; | |
2d76680f | 9201 | if (!processing_template_decl) |
9927ecbb PP |
9202 | op0 = cxx_eval_outermost_constant_expr (op0, true); |
9203 | if (tree_int_cst_equal (op0, tmp)) | |
9204 | return (flags & tf_error) ? RECUR (op1, rval) : false; | |
2d76680f PC |
9205 | else |
9206 | return true; | |
9207 | } | |
9208 | ||
9209 | case PLUS_EXPR: | |
9210 | case MULT_EXPR: | |
9211 | case POINTER_PLUS_EXPR: | |
9212 | case RDIV_EXPR: | |
9213 | case EXACT_DIV_EXPR: | |
9214 | case MIN_EXPR: | |
9215 | case MAX_EXPR: | |
9216 | case LSHIFT_EXPR: | |
9217 | case RSHIFT_EXPR: | |
81fa6d73 JJ |
9218 | case LROTATE_EXPR: |
9219 | case RROTATE_EXPR: | |
2d76680f PC |
9220 | case BIT_IOR_EXPR: |
9221 | case BIT_XOR_EXPR: | |
9222 | case BIT_AND_EXPR: | |
9223 | case TRUTH_XOR_EXPR: | |
9224 | case UNORDERED_EXPR: | |
9225 | case ORDERED_EXPR: | |
9226 | case UNLT_EXPR: | |
9227 | case UNLE_EXPR: | |
9228 | case UNGT_EXPR: | |
9229 | case UNGE_EXPR: | |
9230 | case UNEQ_EXPR: | |
9231 | case LTGT_EXPR: | |
9232 | case RANGE_EXPR: | |
9233 | case COMPLEX_EXPR: | |
9234 | want_rval = true; | |
9235 | /* Fall through. */ | |
9236 | case ARRAY_REF: | |
9237 | case ARRAY_RANGE_REF: | |
9238 | case MEMBER_REF: | |
9239 | case DOTSTAR_EXPR: | |
41b38772 | 9240 | case MEM_REF: |
4856a1f0 PC |
9241 | case BINARY_LEFT_FOLD_EXPR: |
9242 | case BINARY_RIGHT_FOLD_EXPR: | |
2d76680f PC |
9243 | binary: |
9244 | for (i = 0; i < 2; ++i) | |
69eb4fde | 9245 | if (!RECUR (TREE_OPERAND (t, i), want_rval)) |
2d76680f PC |
9246 | return false; |
9247 | return true; | |
9248 | ||
2d76680f PC |
9249 | case VEC_PERM_EXPR: |
9250 | for (i = 0; i < 3; ++i) | |
69eb4fde | 9251 | if (!RECUR (TREE_OPERAND (t, i), true)) |
2d76680f PC |
9252 | return false; |
9253 | return true; | |
9254 | ||
9255 | case COND_EXPR: | |
b04445d4 | 9256 | if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20) |
d5bcd6d4 JM |
9257 | { |
9258 | if (flags & tf_error) | |
e40b6fc7 | 9259 | error_at (loc, "%<delete[]%> is not a constant expression"); |
d5bcd6d4 JM |
9260 | return false; |
9261 | } | |
9262 | /* Fall through. */ | |
9263 | case IF_STMT: | |
2d76680f PC |
9264 | case VEC_COND_EXPR: |
9265 | /* If the condition is a known constant, we know which of the legs we | |
9266 | care about; otherwise we only require that the condition and | |
9267 | either of the legs be potentially constant. */ | |
9268 | tmp = TREE_OPERAND (t, 0); | |
69eb4fde | 9269 | if (!RECUR (tmp, rval)) |
2d76680f PC |
9270 | return false; |
9271 | if (!processing_template_decl) | |
9272 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
117c6426 JJ |
9273 | /* potential_constant_expression* isn't told if it is called for |
9274 | manifestly_const_eval or not, so for consteval if always | |
9275 | process both branches as if the condition is not a known | |
9276 | constant. */ | |
9277 | if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t)) | |
9278 | { | |
9279 | if (integer_zerop (tmp)) | |
9280 | return RECUR (TREE_OPERAND (t, 2), want_rval); | |
9281 | else if (TREE_CODE (tmp) == INTEGER_CST) | |
9282 | return RECUR (TREE_OPERAND (t, 1), want_rval); | |
9283 | } | |
0fb7aa20 | 9284 | tmp = *jump_target; |
2d76680f | 9285 | for (i = 1; i < 3; ++i) |
0fb7aa20 JJ |
9286 | { |
9287 | tree this_jump_target = tmp; | |
9288 | if (potential_constant_expression_1 (TREE_OPERAND (t, i), | |
9289 | want_rval, strict, now, | |
9290 | tf_none, &this_jump_target)) | |
9291 | { | |
9292 | if (returns (&this_jump_target)) | |
9293 | *jump_target = this_jump_target; | |
9294 | else if (!returns (jump_target)) | |
9295 | { | |
9296 | if (breaks (&this_jump_target) | |
9297 | || continues (&this_jump_target)) | |
9298 | *jump_target = this_jump_target; | |
9299 | if (i == 1) | |
9300 | { | |
9301 | /* If the then branch is potentially constant, but | |
9302 | does not return, check if the else branch | |
9303 | couldn't return, break or continue. */ | |
9304 | hash_set<tree> pset; | |
9305 | check_for_return_continue_data data = { &pset, NULL_TREE, | |
9306 | NULL_TREE }; | |
9307 | if (tree ret_expr | |
9308 | = cp_walk_tree (&TREE_OPERAND (t, 2), | |
9309 | check_for_return_continue, &data, | |
9310 | &pset)) | |
9311 | *jump_target = ret_expr; | |
9312 | else if (*jump_target == NULL_TREE) | |
9313 | { | |
9314 | if (data.continue_stmt) | |
9315 | *jump_target = data.continue_stmt; | |
9316 | else if (data.break_stmt) | |
9317 | *jump_target = data.break_stmt; | |
9318 | } | |
9319 | } | |
9320 | } | |
9321 | return true; | |
9322 | } | |
9323 | } | |
2d76680f | 9324 | if (flags & tf_error) |
e40b6fc7 | 9325 | error_at (loc, "expression %qE is not a constant expression", t); |
2d76680f PC |
9326 | return false; |
9327 | ||
9328 | case VEC_INIT_EXPR: | |
9329 | if (VEC_INIT_EXPR_IS_CONSTEXPR (t)) | |
9330 | return true; | |
9331 | if (flags & tf_error) | |
9332 | { | |
e40b6fc7 | 9333 | error_at (loc, "non-constant array initialization"); |
2d76680f PC |
9334 | diagnose_non_constexpr_vec_init (t); |
9335 | } | |
9336 | return false; | |
9337 | ||
133bc698 JM |
9338 | case TYPE_DECL: |
9339 | case TAG_DEFN: | |
9340 | /* We can see these in statement-expressions. */ | |
9341 | return true; | |
9342 | ||
baf9ebc8 | 9343 | case CLEANUP_STMT: |
1006c9d4 JJ |
9344 | if (!RECUR (CLEANUP_BODY (t), any)) |
9345 | return false; | |
9346 | if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any)) | |
9347 | return false; | |
9348 | return true; | |
9349 | ||
cda0a029 | 9350 | case EMPTY_CLASS_EXPR: |
46fdced6 | 9351 | return true; |
cda0a029 | 9352 | |
f937929e JM |
9353 | case GOTO_EXPR: |
9354 | { | |
9355 | tree *target = &TREE_OPERAND (t, 0); | |
ab3af181 JJ |
9356 | /* Gotos representing break and continue are OK. */ |
9357 | if (breaks (target) || continues (target)) | |
3075affd JM |
9358 | { |
9359 | *jump_target = *target; | |
9360 | return true; | |
9361 | } | |
ab3af181 | 9362 | if (flags & tf_error) |
e40b6fc7 | 9363 | error_at (loc, "%<goto%> is not a constant expression"); |
ab3af181 | 9364 | return false; |
f937929e JM |
9365 | } |
9366 | ||
6f20c42c MP |
9367 | case LABEL_EXPR: |
9368 | t = LABEL_EXPR_LABEL (t); | |
8892d532 | 9369 | if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23) |
6f20c42c MP |
9370 | return true; |
9371 | else if (flags & tf_error) | |
8892d532 JJ |
9372 | error_at (loc, "label definition in %<constexpr%> function only " |
9373 | "available with %<-std=c++2b%> or %<-std=gnu++2b%>"); | |
6f20c42c MP |
9374 | return false; |
9375 | ||
98e09245 | 9376 | case ANNOTATE_EXPR: |
98e09245 JJ |
9377 | return RECUR (TREE_OPERAND (t, 0), rval); |
9378 | ||
896048cf JJ |
9379 | case BIT_CAST_EXPR: |
9380 | return RECUR (TREE_OPERAND (t, 0), rval); | |
9381 | ||
49789fd0 IS |
9382 | /* Coroutine await, yield and return expressions are not. */ |
9383 | case CO_AWAIT_EXPR: | |
9384 | case CO_YIELD_EXPR: | |
9385 | case CO_RETURN_EXPR: | |
9386 | return false; | |
9387 | ||
d4c470c3 PP |
9388 | case NONTYPE_ARGUMENT_PACK: |
9389 | { | |
9390 | tree args = ARGUMENT_PACK_ARGS (t); | |
9391 | int len = TREE_VEC_LENGTH (args); | |
9392 | for (int i = 0; i < len; ++i) | |
9393 | if (!RECUR (TREE_VEC_ELT (args, i), any)) | |
9394 | return false; | |
9395 | return true; | |
9396 | } | |
9397 | ||
2d76680f | 9398 | default: |
878cffbd | 9399 | if (objc_non_constant_expr_p (t)) |
2d76680f PC |
9400 | return false; |
9401 | ||
9402 | sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t))); | |
ab3af181 | 9403 | gcc_unreachable (); |
2d76680f PC |
9404 | return false; |
9405 | } | |
69eb4fde | 9406 | #undef RECUR |
2d76680f PC |
9407 | } |
9408 | ||
3075affd JM |
9409 | bool |
9410 | potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, | |
9411 | tsubst_flags_t flags) | |
9412 | { | |
9927ecbb PP |
9413 | if (flags & tf_error) |
9414 | { | |
9415 | /* Check potentiality quietly first, as that could be performed more | |
9416 | efficiently in some cases (currently only for TRUTH_*_EXPR). If | |
9417 | that fails, replay the check noisily to give errors. */ | |
9418 | flags &= ~tf_error; | |
9419 | if (potential_constant_expression_1 (t, want_rval, strict, now, flags)) | |
9420 | return true; | |
9421 | flags |= tf_error; | |
9422 | } | |
9423 | ||
3075affd JM |
9424 | tree target = NULL_TREE; |
9425 | return potential_constant_expression_1 (t, want_rval, strict, now, | |
9426 | flags, &target); | |
9427 | } | |
9428 | ||
2d76680f PC |
9429 | /* The main entry point to the above. */ |
9430 | ||
9431 | bool | |
9432 | potential_constant_expression (tree t) | |
9433 | { | |
a0ab7ccd | 9434 | return potential_constant_expression_1 (t, false, true, false, tf_none); |
2d76680f PC |
9435 | } |
9436 | ||
9437 | /* As above, but require a constant rvalue. */ | |
9438 | ||
9439 | bool | |
9440 | potential_rvalue_constant_expression (tree t) | |
9441 | { | |
a0ab7ccd | 9442 | return potential_constant_expression_1 (t, true, true, false, tf_none); |
2d76680f PC |
9443 | } |
9444 | ||
9445 | /* Like above, but complain about non-constant expressions. */ | |
9446 | ||
9447 | bool | |
9448 | require_potential_constant_expression (tree t) | |
9449 | { | |
d8cff23f MP |
9450 | return potential_constant_expression_1 (t, false, true, false, |
9451 | tf_warning_or_error); | |
2d76680f PC |
9452 | } |
9453 | ||
9454 | /* Cross product of the above. */ | |
9455 | ||
9456 | bool | |
9457 | require_potential_rvalue_constant_expression (tree t) | |
9458 | { | |
d8cff23f MP |
9459 | return potential_constant_expression_1 (t, true, true, false, |
9460 | tf_warning_or_error); | |
9461 | } | |
9462 | ||
9463 | /* Like above, but don't consider PARM_DECL a potential_constant_expression. */ | |
9464 | ||
9465 | bool | |
9466 | require_rvalue_constant_expression (tree t) | |
9467 | { | |
9468 | return potential_constant_expression_1 (t, true, true, true, | |
9469 | tf_warning_or_error); | |
a0ab7ccd JM |
9470 | } |
9471 | ||
9472 | /* Like potential_constant_expression, but don't consider possible constexpr | |
9473 | substitution of the current function. That is, PARM_DECL qualifies under | |
9474 | potential_constant_expression, but not here. | |
9475 | ||
9476 | This is basically what you can check when any actual constant values might | |
9477 | be value-dependent. */ | |
9478 | ||
9479 | bool | |
9480 | is_constant_expression (tree t) | |
9481 | { | |
9482 | return potential_constant_expression_1 (t, false, true, true, tf_none); | |
9483 | } | |
9484 | ||
beb019d3 JM |
9485 | /* As above, but expect an rvalue. */ |
9486 | ||
9487 | bool | |
9488 | is_rvalue_constant_expression (tree t) | |
9489 | { | |
9490 | return potential_constant_expression_1 (t, true, true, true, tf_none); | |
9491 | } | |
9492 | ||
a0ab7ccd JM |
9493 | /* Like above, but complain about non-constant expressions. */ |
9494 | ||
9495 | bool | |
9496 | require_constant_expression (tree t) | |
9497 | { | |
9498 | return potential_constant_expression_1 (t, false, true, true, | |
9499 | tf_warning_or_error); | |
9500 | } | |
9501 | ||
9502 | /* Like is_constant_expression, but allow const variables that are not allowed | |
9503 | under constexpr rules. */ | |
9504 | ||
9505 | bool | |
9506 | is_static_init_expression (tree t) | |
9507 | { | |
9508 | return potential_constant_expression_1 (t, false, false, true, tf_none); | |
2d76680f PC |
9509 | } |
9510 | ||
eb07f187 JM |
9511 | /* Returns true if T is a potential constant expression that is not |
9512 | instantiation-dependent, and therefore a candidate for constant folding even | |
9513 | in a template. */ | |
9514 | ||
9515 | bool | |
a0ab7ccd | 9516 | is_nondependent_constant_expression (tree t) |
eb07f187 JM |
9517 | { |
9518 | return (!type_unknown_p (t) | |
a0ab7ccd | 9519 | && is_constant_expression (t) |
eb07f187 JM |
9520 | && !instantiation_dependent_expression_p (t)); |
9521 | } | |
9522 | ||
9523 | /* Returns true if T is a potential static initializer expression that is not | |
9524 | instantiation-dependent. */ | |
9525 | ||
9526 | bool | |
a0ab7ccd | 9527 | is_nondependent_static_init_expression (tree t) |
eb07f187 JM |
9528 | { |
9529 | return (!type_unknown_p (t) | |
a0ab7ccd | 9530 | && is_static_init_expression (t) |
eb07f187 JM |
9531 | && !instantiation_dependent_expression_p (t)); |
9532 | } | |
9533 | ||
87c2080b JM |
9534 | /* True iff FN is an implicitly constexpr function. */ |
9535 | ||
9536 | bool | |
9537 | decl_implicit_constexpr_p (tree fn) | |
9538 | { | |
9539 | if (!(flag_implicit_constexpr | |
9540 | && TREE_CODE (fn) == FUNCTION_DECL | |
9541 | && DECL_DECLARED_CONSTEXPR_P (fn))) | |
9542 | return false; | |
9543 | ||
9544 | if (DECL_CLONED_FUNCTION_P (fn)) | |
9545 | fn = DECL_CLONED_FUNCTION (fn); | |
9546 | ||
9547 | return (DECL_LANG_SPECIFIC (fn) | |
9548 | && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr); | |
9549 | } | |
9550 | ||
97f3003f JM |
9551 | /* Finalize constexpr processing after parsing. */ |
9552 | ||
9553 | void | |
9554 | fini_constexpr (void) | |
9555 | { | |
9556 | /* The contexpr call and fundef copies tables are no longer needed. */ | |
9557 | constexpr_call_table = NULL; | |
9558 | fundef_copies_table = NULL; | |
9559 | } | |
9560 | ||
2d76680f | 9561 | #include "gt-cp-constexpr.h" |