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