]>
Commit | Line | Data |
---|---|---|
f9885b8a | 1 | /* Perform -*- C++ -*- constant expression evaluation, including calls to |
2 | constexpr functions. These routines are used both during actual parsing | |
09b42213 | 3 | and during the instantiation of template functions. |
4 | ||
8e8f6434 | 5 | Copyright (C) 1998-2018 Free Software Foundation, Inc. |
09b42213 | 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" | |
09b42213 | 26 | #include "cp-tree.h" |
4cba6f60 | 27 | #include "varasm.h" |
09b42213 | 28 | #include "c-family/c-objc.h" |
29 | #include "tree-iterator.h" | |
30 | #include "gimplify.h" | |
31 | #include "builtins.h" | |
9c96033c | 32 | #include "tree-inline.h" |
6b71bdb4 | 33 | #include "ubsan.h" |
732905bb | 34 | #include "gimple-fold.h" |
c300fe28 | 35 | #include "timevar.h" |
09b42213 | 36 | |
37 | static bool verify_constant (tree, bool, bool *, bool *); | |
38 | #define VERIFY_CONSTANT(X) \ | |
39 | do { \ | |
f83e6885 | 40 | if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \ |
09b42213 | 41 | return t; \ |
42 | } while (0) | |
43 | ||
44 | /* Returns true iff FUN is an instantiation of a constexpr function | |
45 | template or a defaulted constexpr function. */ | |
46 | ||
47 | bool | |
48 | is_instantiation_of_constexpr (tree fun) | |
49 | { | |
50 | return ((DECL_TEMPLOID_INSTANTIATION (fun) | |
51 | && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun))) | |
52 | || (DECL_DEFAULTED_FN (fun) | |
53 | && DECL_DECLARED_CONSTEXPR_P (fun))); | |
54 | } | |
55 | ||
56 | /* Return true if T is a literal type. */ | |
57 | ||
58 | bool | |
59 | literal_type_p (tree t) | |
60 | { | |
61 | if (SCALAR_TYPE_P (t) | |
76249021 | 62 | || VECTOR_TYPE_P (t) |
90ad495b | 63 | || TYPE_REF_P (t) |
dd418b39 | 64 | || (VOID_TYPE_P (t) && cxx_dialect >= cxx14)) |
09b42213 | 65 | return true; |
66 | if (CLASS_TYPE_P (t)) | |
67 | { | |
68 | t = complete_type (t); | |
69 | gcc_assert (COMPLETE_TYPE_P (t) || errorcount); | |
70 | return CLASSTYPE_LITERAL_P (t); | |
71 | } | |
72 | if (TREE_CODE (t) == ARRAY_TYPE) | |
73 | return literal_type_p (strip_array_types (t)); | |
74 | return false; | |
75 | } | |
76 | ||
77 | /* If DECL is a variable declared `constexpr', require its type | |
e89257db | 78 | be literal. Return error_mark_node if we give an error, the |
79 | DECL otherwise. */ | |
09b42213 | 80 | |
81 | tree | |
82 | ensure_literal_type_for_constexpr_object (tree decl) | |
83 | { | |
84 | tree type = TREE_TYPE (decl); | |
85 | if (VAR_P (decl) | |
86 | && (DECL_DECLARED_CONSTEXPR_P (decl) | |
87 | || var_in_constexpr_fn (decl)) | |
88 | && !processing_template_decl) | |
89 | { | |
90 | tree stype = strip_array_types (type); | |
91 | if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype))) | |
92 | /* Don't complain here, we'll complain about incompleteness | |
93 | when we try to initialize the variable. */; | |
03dbe51b | 94 | else if (type_uses_auto (type)) |
95 | /* We don't know the actual type yet. */; | |
09b42213 | 96 | else if (!literal_type_p (type)) |
97 | { | |
98 | if (DECL_DECLARED_CONSTEXPR_P (decl)) | |
e45408ff | 99 | { |
5967b28b | 100 | error ("the type %qT of %<constexpr%> variable %qD " |
101 | "is not literal", type, decl); | |
e45408ff | 102 | explain_non_literal_class (type); |
e89257db | 103 | decl = error_mark_node; |
e45408ff | 104 | } |
09b42213 | 105 | else |
9c96033c | 106 | { |
b7e4a558 | 107 | if (!is_instantiation_of_constexpr (current_function_decl)) |
e45408ff | 108 | { |
109 | error ("variable %qD of non-literal type %qT in %<constexpr%> " | |
110 | "function", decl, type); | |
111 | explain_non_literal_class (type); | |
e89257db | 112 | decl = error_mark_node; |
e45408ff | 113 | } |
9c96033c | 114 | cp_function_chain->invalid_constexpr = true; |
115 | } | |
09b42213 | 116 | } |
fdafeeb7 | 117 | else if (DECL_DECLARED_CONSTEXPR_P (decl) |
118 | && variably_modified_type_p (type, NULL_TREE)) | |
119 | { | |
120 | error ("%<constexpr%> variable %qD has variably-modified type %qT", | |
121 | decl, type); | |
122 | decl = error_mark_node; | |
123 | } | |
09b42213 | 124 | } |
125 | return decl; | |
126 | } | |
127 | ||
128 | /* Representation of entries in the constexpr function definition table. */ | |
129 | ||
130 | struct GTY((for_user)) constexpr_fundef { | |
131 | tree decl; | |
132 | tree body; | |
133 | }; | |
134 | ||
b594087e | 135 | struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef> |
09b42213 | 136 | { |
137 | static hashval_t hash (constexpr_fundef *); | |
138 | static bool equal (constexpr_fundef *, constexpr_fundef *); | |
139 | }; | |
140 | ||
141 | /* This table holds all constexpr function definitions seen in | |
142 | the current translation unit. */ | |
143 | ||
144 | static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table; | |
145 | ||
146 | /* Utility function used for managing the constexpr function table. | |
147 | Return true if the entries pointed to by P and Q are for the | |
148 | same constexpr function. */ | |
149 | ||
150 | inline bool | |
151 | constexpr_fundef_hasher::equal (constexpr_fundef *lhs, constexpr_fundef *rhs) | |
152 | { | |
153 | return lhs->decl == rhs->decl; | |
154 | } | |
155 | ||
156 | /* Utility function used for managing the constexpr function table. | |
157 | Return a hash value for the entry pointed to by Q. */ | |
158 | ||
159 | inline hashval_t | |
160 | constexpr_fundef_hasher::hash (constexpr_fundef *fundef) | |
161 | { | |
162 | return DECL_UID (fundef->decl); | |
163 | } | |
164 | ||
165 | /* Return a previously saved definition of function FUN. */ | |
166 | ||
167 | static constexpr_fundef * | |
168 | retrieve_constexpr_fundef (tree fun) | |
169 | { | |
170 | constexpr_fundef fundef = { NULL, NULL }; | |
171 | if (constexpr_fundef_table == NULL) | |
172 | return NULL; | |
173 | ||
174 | fundef.decl = fun; | |
175 | return constexpr_fundef_table->find (&fundef); | |
176 | } | |
177 | ||
178 | /* Check whether the parameter and return types of FUN are valid for a | |
179 | constexpr function, and complain if COMPLAIN. */ | |
180 | ||
33603066 | 181 | bool |
09b42213 | 182 | is_valid_constexpr_fn (tree fun, bool complain) |
183 | { | |
184 | bool ret = true; | |
185 | ||
7896267d | 186 | if (DECL_INHERITED_CTOR (fun) |
09b42213 | 187 | && TREE_CODE (fun) == TEMPLATE_DECL) |
188 | { | |
189 | ret = false; | |
190 | if (complain) | |
5967b28b | 191 | error ("inherited constructor %qD is not %<constexpr%>", |
7896267d | 192 | DECL_INHERITED_CTOR (fun)); |
09b42213 | 193 | } |
194 | else | |
195 | { | |
196 | for (tree parm = FUNCTION_FIRST_USER_PARM (fun); | |
197 | parm != NULL_TREE; parm = TREE_CHAIN (parm)) | |
198 | if (!literal_type_p (TREE_TYPE (parm))) | |
199 | { | |
200 | ret = false; | |
201 | if (complain) | |
202 | { | |
5967b28b | 203 | error ("invalid type for parameter %d of %<constexpr%> " |
09b42213 | 204 | "function %q+#D", DECL_PARM_INDEX (parm), fun); |
205 | explain_non_literal_class (TREE_TYPE (parm)); | |
206 | } | |
207 | } | |
208 | } | |
209 | ||
672871ce | 210 | if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17) |
211 | { | |
212 | ret = false; | |
213 | if (complain) | |
214 | inform (DECL_SOURCE_LOCATION (fun), | |
5967b28b | 215 | "lambdas are implicitly %<constexpr%> only in C++17 and later"); |
672871ce | 216 | } |
217 | else if (!DECL_CONSTRUCTOR_P (fun)) | |
09b42213 | 218 | { |
219 | tree rettype = TREE_TYPE (TREE_TYPE (fun)); | |
220 | if (!literal_type_p (rettype)) | |
221 | { | |
222 | ret = false; | |
223 | if (complain) | |
224 | { | |
5967b28b | 225 | error ("invalid return type %qT of %<constexpr%> function %q+D", |
09b42213 | 226 | rettype, fun); |
227 | explain_non_literal_class (rettype); | |
228 | } | |
229 | } | |
230 | ||
d66f34cf | 231 | /* C++14 DR 1684 removed this restriction. */ |
232 | if (cxx_dialect < cxx14 | |
233 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) | |
09b42213 | 234 | && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun))) |
235 | { | |
236 | ret = false; | |
d66f34cf | 237 | if (complain |
238 | && pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic, | |
5967b28b | 239 | "enclosing class of %<constexpr%> non-static member " |
d66f34cf | 240 | "function %q+#D is not a literal type", fun)) |
241 | explain_non_literal_class (DECL_CONTEXT (fun)); | |
09b42213 | 242 | } |
243 | } | |
244 | else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun))) | |
245 | { | |
246 | ret = false; | |
247 | if (complain) | |
248 | error ("%q#T has virtual base classes", DECL_CONTEXT (fun)); | |
249 | } | |
250 | ||
251 | return ret; | |
252 | } | |
253 | ||
254 | /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF | |
255 | for a member of an anonymous aggregate, INIT is the initializer for that | |
256 | member, and VEC_OUTER is the vector of constructor elements for the class | |
257 | whose constructor we are processing. Add the initializer to the vector | |
258 | and return true to indicate success. */ | |
259 | ||
260 | static bool | |
261 | build_anon_member_initialization (tree member, tree init, | |
262 | vec<constructor_elt, va_gc> **vec_outer) | |
263 | { | |
264 | /* MEMBER presents the relevant fields from the inside out, but we need | |
265 | to build up the initializer from the outside in so that we can reuse | |
266 | previously built CONSTRUCTORs if this is, say, the second field in an | |
267 | anonymous struct. So we use a vec as a stack. */ | |
268 | auto_vec<tree, 2> fields; | |
269 | do | |
270 | { | |
271 | fields.safe_push (TREE_OPERAND (member, 1)); | |
272 | member = TREE_OPERAND (member, 0); | |
273 | } | |
274 | while (ANON_AGGR_TYPE_P (TREE_TYPE (member)) | |
275 | && TREE_CODE (member) == COMPONENT_REF); | |
276 | ||
277 | /* VEC has the constructor elements vector for the context of FIELD. | |
278 | If FIELD is an anonymous aggregate, we will push inside it. */ | |
279 | vec<constructor_elt, va_gc> **vec = vec_outer; | |
280 | tree field; | |
281 | while (field = fields.pop(), | |
282 | ANON_AGGR_TYPE_P (TREE_TYPE (field))) | |
283 | { | |
284 | tree ctor; | |
285 | /* If there is already an outer constructor entry for the anonymous | |
286 | aggregate FIELD, use it; otherwise, insert one. */ | |
287 | if (vec_safe_is_empty (*vec) | |
288 | || (*vec)->last().index != field) | |
289 | { | |
290 | ctor = build_constructor (TREE_TYPE (field), NULL); | |
291 | CONSTRUCTOR_APPEND_ELT (*vec, field, ctor); | |
292 | } | |
293 | else | |
294 | ctor = (*vec)->last().value; | |
295 | vec = &CONSTRUCTOR_ELTS (ctor); | |
296 | } | |
297 | ||
298 | /* Now we're at the innermost field, the one that isn't an anonymous | |
299 | aggregate. Add its initializer to the CONSTRUCTOR and we're done. */ | |
300 | gcc_assert (fields.is_empty()); | |
301 | CONSTRUCTOR_APPEND_ELT (*vec, field, init); | |
302 | ||
303 | return true; | |
304 | } | |
305 | ||
306 | /* Subroutine of build_constexpr_constructor_member_initializers. | |
307 | The expression tree T represents a data member initialization | |
308 | in a (constexpr) constructor definition. Build a pairing of | |
309 | the data member with its initializer, and prepend that pair | |
310 | to the existing initialization pair INITS. */ | |
311 | ||
312 | static bool | |
313 | build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec) | |
314 | { | |
315 | tree member, init; | |
316 | if (TREE_CODE (t) == CLEANUP_POINT_EXPR) | |
317 | t = TREE_OPERAND (t, 0); | |
318 | if (TREE_CODE (t) == EXPR_STMT) | |
319 | t = TREE_OPERAND (t, 0); | |
320 | if (t == error_mark_node) | |
321 | return false; | |
322 | if (TREE_CODE (t) == STATEMENT_LIST) | |
323 | { | |
324 | tree_stmt_iterator i; | |
325 | for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) | |
326 | { | |
327 | if (! build_data_member_initialization (tsi_stmt (i), vec)) | |
328 | return false; | |
329 | } | |
330 | return true; | |
331 | } | |
332 | if (TREE_CODE (t) == CLEANUP_STMT) | |
333 | { | |
334 | /* We can't see a CLEANUP_STMT in a constructor for a literal class, | |
335 | but we can in a constexpr constructor for a non-literal class. Just | |
336 | ignore it; either all the initialization will be constant, in which | |
337 | case the cleanup can't run, or it can't be constexpr. | |
338 | Still recurse into CLEANUP_BODY. */ | |
339 | return build_data_member_initialization (CLEANUP_BODY (t), vec); | |
340 | } | |
341 | if (TREE_CODE (t) == CONVERT_EXPR) | |
342 | t = TREE_OPERAND (t, 0); | |
343 | if (TREE_CODE (t) == INIT_EXPR | |
9c96033c | 344 | /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only |
345 | use what this function builds for cx_check_missing_mem_inits, and | |
346 | assignment in the ctor body doesn't count. */ | |
347 | || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR)) | |
09b42213 | 348 | { |
349 | member = TREE_OPERAND (t, 0); | |
350 | init = break_out_target_exprs (TREE_OPERAND (t, 1)); | |
351 | } | |
352 | else if (TREE_CODE (t) == CALL_EXPR) | |
353 | { | |
9c96033c | 354 | tree fn = get_callee_fndecl (t); |
355 | if (!fn || !DECL_CONSTRUCTOR_P (fn)) | |
356 | /* We're only interested in calls to subobject constructors. */ | |
357 | return true; | |
09b42213 | 358 | member = CALL_EXPR_ARG (t, 0); |
359 | /* We don't use build_cplus_new here because it complains about | |
360 | abstract bases. Leaving the call unwrapped means that it has the | |
361 | wrong type, but cxx_eval_constant_expression doesn't care. */ | |
362 | init = break_out_target_exprs (t); | |
363 | } | |
364 | else if (TREE_CODE (t) == BIND_EXPR) | |
365 | return build_data_member_initialization (BIND_EXPR_BODY (t), vec); | |
09b42213 | 366 | else |
9c96033c | 367 | /* Don't add anything else to the CONSTRUCTOR. */ |
368 | return true; | |
09b42213 | 369 | if (INDIRECT_REF_P (member)) |
370 | member = TREE_OPERAND (member, 0); | |
371 | if (TREE_CODE (member) == NOP_EXPR) | |
372 | { | |
373 | tree op = member; | |
374 | STRIP_NOPS (op); | |
375 | if (TREE_CODE (op) == ADDR_EXPR) | |
376 | { | |
377 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
378 | (TREE_TYPE (TREE_TYPE (op)), | |
379 | TREE_TYPE (TREE_TYPE (member)))); | |
380 | /* Initializing a cv-qualified member; we need to look through | |
381 | the const_cast. */ | |
382 | member = op; | |
383 | } | |
384 | else if (op == current_class_ptr | |
385 | && (same_type_ignoring_top_level_qualifiers_p | |
386 | (TREE_TYPE (TREE_TYPE (member)), | |
387 | current_class_type))) | |
388 | /* Delegating constructor. */ | |
389 | member = op; | |
390 | else | |
391 | { | |
392 | /* This is an initializer for an empty base; keep it for now so | |
393 | we can check it in cxx_eval_bare_aggregate. */ | |
394 | gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member)))); | |
395 | } | |
396 | } | |
397 | if (TREE_CODE (member) == ADDR_EXPR) | |
398 | member = TREE_OPERAND (member, 0); | |
399 | if (TREE_CODE (member) == COMPONENT_REF) | |
400 | { | |
401 | tree aggr = TREE_OPERAND (member, 0); | |
4952bc85 | 402 | if (TREE_CODE (aggr) == VAR_DECL) |
403 | /* Initializing a local variable, don't add anything. */ | |
404 | return true; | |
09b42213 | 405 | if (TREE_CODE (aggr) != COMPONENT_REF) |
406 | /* Normal member initialization. */ | |
407 | member = TREE_OPERAND (member, 1); | |
408 | else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr))) | |
409 | /* Initializing a member of an anonymous union. */ | |
410 | return build_anon_member_initialization (member, init, vec); | |
411 | else | |
412 | /* We're initializing a vtable pointer in a base. Leave it as | |
413 | COMPONENT_REF so we remember the path to get to the vfield. */ | |
414 | gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node); | |
415 | } | |
416 | ||
c678e23e | 417 | /* Value-initialization can produce multiple initializers for the |
418 | same field; use the last one. */ | |
419 | if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member) | |
420 | (*vec)->last().value = init; | |
421 | else | |
422 | CONSTRUCTOR_APPEND_ELT (*vec, member, init); | |
09b42213 | 423 | return true; |
424 | } | |
425 | ||
426 | /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval. | |
427 | In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a | |
428 | BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */ | |
429 | ||
430 | static bool | |
431 | check_constexpr_bind_expr_vars (tree t) | |
432 | { | |
433 | gcc_assert (TREE_CODE (t) == BIND_EXPR); | |
434 | ||
09b42213 | 435 | for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var)) |
436 | if (TREE_CODE (var) == TYPE_DECL | |
04103c39 | 437 | && DECL_IMPLICIT_TYPEDEF_P (var) |
438 | && !LAMBDA_TYPE_P (TREE_TYPE (var))) | |
09b42213 | 439 | return false; |
440 | return true; | |
441 | } | |
442 | ||
443 | /* Subroutine of check_constexpr_ctor_body. */ | |
444 | ||
445 | static bool | |
446 | check_constexpr_ctor_body_1 (tree last, tree list) | |
447 | { | |
448 | switch (TREE_CODE (list)) | |
449 | { | |
450 | case DECL_EXPR: | |
810702a2 | 451 | if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL |
452 | || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL) | |
09b42213 | 453 | return true; |
09b42213 | 454 | return false; |
455 | ||
456 | case CLEANUP_POINT_EXPR: | |
457 | return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0), | |
458 | /*complain=*/false); | |
459 | ||
460 | case BIND_EXPR: | |
461 | if (!check_constexpr_bind_expr_vars (list) | |
462 | || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list), | |
463 | /*complain=*/false)) | |
464 | return false; | |
465 | return true; | |
466 | ||
467 | case USING_STMT: | |
468 | case STATIC_ASSERT: | |
90567983 | 469 | case DEBUG_BEGIN_STMT: |
09b42213 | 470 | return true; |
471 | ||
472 | default: | |
473 | return false; | |
474 | } | |
475 | } | |
476 | ||
477 | /* Make sure that there are no statements after LAST in the constructor | |
478 | body represented by LIST. */ | |
479 | ||
480 | bool | |
481 | check_constexpr_ctor_body (tree last, tree list, bool complain) | |
482 | { | |
9c96033c | 483 | /* C++14 doesn't require a constexpr ctor to have an empty body. */ |
484 | if (cxx_dialect >= cxx14) | |
485 | return true; | |
486 | ||
09b42213 | 487 | bool ok = true; |
488 | if (TREE_CODE (list) == STATEMENT_LIST) | |
489 | { | |
490 | tree_stmt_iterator i = tsi_last (list); | |
491 | for (; !tsi_end_p (i); tsi_prev (&i)) | |
492 | { | |
493 | tree t = tsi_stmt (i); | |
494 | if (t == last) | |
495 | break; | |
496 | if (!check_constexpr_ctor_body_1 (last, t)) | |
497 | { | |
498 | ok = false; | |
499 | break; | |
500 | } | |
501 | } | |
502 | } | |
503 | else if (list != last | |
504 | && !check_constexpr_ctor_body_1 (last, list)) | |
505 | ok = false; | |
506 | if (!ok) | |
507 | { | |
508 | if (complain) | |
5967b28b | 509 | error ("%<constexpr%> constructor does not have empty body"); |
09b42213 | 510 | DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false; |
511 | } | |
512 | return ok; | |
513 | } | |
514 | ||
515 | /* V is a vector of constructor elements built up for the base and member | |
516 | initializers of a constructor for TYPE. They need to be in increasing | |
517 | offset order, which they might not be yet if TYPE has a primary base | |
518 | which is not first in the base-clause or a vptr and at least one base | |
519 | all of which are non-primary. */ | |
520 | ||
521 | static vec<constructor_elt, va_gc> * | |
522 | sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v) | |
523 | { | |
524 | tree pri = CLASSTYPE_PRIMARY_BINFO (type); | |
525 | tree field_type; | |
526 | unsigned i; | |
527 | constructor_elt *ce; | |
528 | ||
529 | if (pri) | |
530 | field_type = BINFO_TYPE (pri); | |
531 | else if (TYPE_CONTAINS_VPTR_P (type)) | |
532 | field_type = vtbl_ptr_type_node; | |
533 | else | |
534 | return v; | |
535 | ||
536 | /* Find the element for the primary base or vptr and move it to the | |
537 | beginning of the vec. */ | |
538 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
539 | if (TREE_TYPE (ce->index) == field_type) | |
540 | break; | |
541 | ||
542 | if (i > 0 && i < vec_safe_length (v)) | |
543 | { | |
544 | vec<constructor_elt, va_gc> &vref = *v; | |
545 | constructor_elt elt = vref[i]; | |
546 | for (; i > 0; --i) | |
547 | vref[i] = vref[i-1]; | |
548 | vref[0] = elt; | |
549 | } | |
550 | ||
551 | return v; | |
552 | } | |
553 | ||
554 | /* Build compile-time evalable representations of member-initializer list | |
555 | for a constexpr constructor. */ | |
556 | ||
557 | static tree | |
558 | build_constexpr_constructor_member_initializers (tree type, tree body) | |
559 | { | |
560 | vec<constructor_elt, va_gc> *vec = NULL; | |
561 | bool ok = true; | |
816919e1 | 562 | while (true) |
563 | switch (TREE_CODE (body)) | |
564 | { | |
565 | case MUST_NOT_THROW_EXPR: | |
566 | case EH_SPEC_BLOCK: | |
567 | body = TREE_OPERAND (body, 0); | |
568 | break; | |
569 | ||
570 | case STATEMENT_LIST: | |
571 | for (tree_stmt_iterator i = tsi_start (body); | |
572 | !tsi_end_p (i); tsi_next (&i)) | |
573 | { | |
574 | body = tsi_stmt (i); | |
575 | if (TREE_CODE (body) == BIND_EXPR) | |
576 | break; | |
577 | } | |
578 | break; | |
579 | ||
580 | case BIND_EXPR: | |
581 | body = BIND_EXPR_BODY (body); | |
582 | goto found; | |
583 | ||
584 | default: | |
585 | gcc_unreachable (); | |
8a36d0ec | 586 | } |
816919e1 | 587 | found: |
09b42213 | 588 | if (TREE_CODE (body) == CLEANUP_POINT_EXPR) |
589 | { | |
590 | body = TREE_OPERAND (body, 0); | |
591 | if (TREE_CODE (body) == EXPR_STMT) | |
592 | body = TREE_OPERAND (body, 0); | |
593 | if (TREE_CODE (body) == INIT_EXPR | |
594 | && (same_type_ignoring_top_level_qualifiers_p | |
595 | (TREE_TYPE (TREE_OPERAND (body, 0)), | |
596 | current_class_type))) | |
597 | { | |
598 | /* Trivial copy. */ | |
599 | return TREE_OPERAND (body, 1); | |
600 | } | |
601 | ok = build_data_member_initialization (body, &vec); | |
602 | } | |
603 | else if (TREE_CODE (body) == STATEMENT_LIST) | |
604 | { | |
605 | tree_stmt_iterator i; | |
606 | for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i)) | |
607 | { | |
608 | ok = build_data_member_initialization (tsi_stmt (i), &vec); | |
609 | if (!ok) | |
610 | break; | |
611 | } | |
612 | } | |
613 | else if (TREE_CODE (body) == TRY_BLOCK) | |
614 | { | |
615 | error ("body of %<constexpr%> constructor cannot be " | |
616 | "a function-try-block"); | |
617 | return error_mark_node; | |
618 | } | |
619 | else if (EXPR_P (body)) | |
620 | ok = build_data_member_initialization (body, &vec); | |
621 | else | |
622 | gcc_assert (errorcount > 0); | |
623 | if (ok) | |
624 | { | |
625 | if (vec_safe_length (vec) > 0) | |
626 | { | |
627 | /* In a delegating constructor, return the target. */ | |
628 | constructor_elt *ce = &(*vec)[0]; | |
629 | if (ce->index == current_class_ptr) | |
630 | { | |
631 | body = ce->value; | |
632 | vec_free (vec); | |
633 | return body; | |
634 | } | |
635 | } | |
636 | vec = sort_constexpr_mem_initializers (type, vec); | |
637 | return build_constructor (type, vec); | |
638 | } | |
639 | else | |
640 | return error_mark_node; | |
641 | } | |
642 | ||
2fb20ba2 | 643 | /* We have an expression tree T that represents a call, either CALL_EXPR |
644 | or AGGR_INIT_EXPR. If the call is lexically to a named function, | |
645 | retrun the _DECL for that function. */ | |
646 | ||
647 | static tree | |
648 | get_function_named_in_call (tree t) | |
649 | { | |
650 | tree fun = cp_get_callee (t); | |
651 | if (fun && TREE_CODE (fun) == ADDR_EXPR | |
652 | && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL) | |
653 | fun = TREE_OPERAND (fun, 0); | |
654 | return fun; | |
655 | } | |
656 | ||
09b42213 | 657 | /* Subroutine of register_constexpr_fundef. BODY is the body of a function |
658 | declared to be constexpr, or a sub-statement thereof. Returns the | |
659 | return value if suitable, error_mark_node for a statement not allowed in | |
660 | a constexpr function, or NULL_TREE if no return value was found. */ | |
661 | ||
c49bd35e | 662 | tree |
09b42213 | 663 | constexpr_fn_retval (tree body) |
664 | { | |
665 | switch (TREE_CODE (body)) | |
666 | { | |
667 | case STATEMENT_LIST: | |
668 | { | |
669 | tree_stmt_iterator i; | |
670 | tree expr = NULL_TREE; | |
671 | for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i)) | |
672 | { | |
673 | tree s = constexpr_fn_retval (tsi_stmt (i)); | |
674 | if (s == error_mark_node) | |
675 | return error_mark_node; | |
676 | else if (s == NULL_TREE) | |
677 | /* Keep iterating. */; | |
678 | else if (expr) | |
679 | /* Multiple return statements. */ | |
680 | return error_mark_node; | |
681 | else | |
682 | expr = s; | |
683 | } | |
684 | return expr; | |
685 | } | |
686 | ||
687 | case RETURN_EXPR: | |
688 | return break_out_target_exprs (TREE_OPERAND (body, 0)); | |
689 | ||
690 | case DECL_EXPR: | |
394aed6a | 691 | { |
692 | tree decl = DECL_EXPR_DECL (body); | |
693 | if (TREE_CODE (decl) == USING_DECL | |
694 | /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */ | |
695 | || DECL_ARTIFICIAL (decl)) | |
696 | return NULL_TREE; | |
697 | return error_mark_node; | |
698 | } | |
09b42213 | 699 | |
700 | case CLEANUP_POINT_EXPR: | |
701 | return constexpr_fn_retval (TREE_OPERAND (body, 0)); | |
702 | ||
703 | case BIND_EXPR: | |
704 | if (!check_constexpr_bind_expr_vars (body)) | |
705 | return error_mark_node; | |
706 | return constexpr_fn_retval (BIND_EXPR_BODY (body)); | |
707 | ||
708 | case USING_STMT: | |
90567983 | 709 | case DEBUG_BEGIN_STMT: |
09b42213 | 710 | return NULL_TREE; |
711 | ||
2fb20ba2 | 712 | case CALL_EXPR: |
713 | { | |
714 | tree fun = get_function_named_in_call (body); | |
715 | if (fun != NULL_TREE | |
716 | && DECL_FUNCTION_CODE (fun) == BUILT_IN_UNREACHABLE) | |
717 | return NULL_TREE; | |
718 | } | |
719 | /* Fallthru. */ | |
720 | ||
09b42213 | 721 | default: |
722 | return error_mark_node; | |
723 | } | |
724 | } | |
725 | ||
726 | /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of | |
727 | FUN; do the necessary transformations to turn it into a single expression | |
728 | that we can store in the hash table. */ | |
729 | ||
730 | static tree | |
731 | massage_constexpr_body (tree fun, tree body) | |
732 | { | |
733 | if (DECL_CONSTRUCTOR_P (fun)) | |
734 | body = build_constexpr_constructor_member_initializers | |
735 | (DECL_CONTEXT (fun), body); | |
9c96033c | 736 | else if (cxx_dialect < cxx14) |
09b42213 | 737 | { |
738 | if (TREE_CODE (body) == EH_SPEC_BLOCK) | |
739 | body = EH_SPEC_STMTS (body); | |
740 | if (TREE_CODE (body) == MUST_NOT_THROW_EXPR) | |
741 | body = TREE_OPERAND (body, 0); | |
742 | body = constexpr_fn_retval (body); | |
743 | } | |
744 | return body; | |
745 | } | |
746 | ||
2f4cab3c | 747 | /* CTYPE is a type constructed from BODY. Return true if some |
748 | bases/fields are uninitialized, and complain if COMPLAIN. */ | |
09b42213 | 749 | |
750 | static bool | |
2f4cab3c | 751 | cx_check_missing_mem_inits (tree ctype, tree body, bool complain) |
09b42213 | 752 | { |
2f4cab3c | 753 | unsigned nelts = 0; |
754 | ||
755 | if (body) | |
756 | { | |
757 | if (TREE_CODE (body) != CONSTRUCTOR) | |
758 | return false; | |
759 | nelts = CONSTRUCTOR_NELTS (body); | |
760 | } | |
761 | tree field = TYPE_FIELDS (ctype); | |
09b42213 | 762 | |
763 | if (TREE_CODE (ctype) == UNION_TYPE) | |
764 | { | |
765 | if (nelts == 0 && next_initializable_field (field)) | |
766 | { | |
767 | if (complain) | |
768 | error ("%<constexpr%> constructor for union %qT must " | |
769 | "initialize exactly one non-static data member", ctype); | |
770 | return true; | |
771 | } | |
772 | return false; | |
773 | } | |
774 | ||
2f4cab3c | 775 | /* Iterate over the CONSTRUCTOR, checking any missing fields don't |
776 | need an explicit initialization. */ | |
777 | bool bad = false; | |
778 | for (unsigned i = 0; i <= nelts; ++i) | |
09b42213 | 779 | { |
2f4cab3c | 780 | tree index = NULL_TREE; |
781 | if (i < nelts) | |
09b42213 | 782 | { |
783 | index = CONSTRUCTOR_ELT (body, i)->index; | |
784 | /* Skip base and vtable inits. */ | |
785 | if (TREE_CODE (index) != FIELD_DECL | |
786 | || DECL_ARTIFICIAL (index)) | |
787 | continue; | |
788 | } | |
2f4cab3c | 789 | |
09b42213 | 790 | for (; field != index; field = DECL_CHAIN (field)) |
791 | { | |
792 | tree ftype; | |
2f4cab3c | 793 | if (TREE_CODE (field) != FIELD_DECL) |
794 | continue; | |
3cde3c29 | 795 | if (DECL_UNNAMED_BIT_FIELD (field)) |
09b42213 | 796 | continue; |
2f4cab3c | 797 | if (DECL_ARTIFICIAL (field)) |
798 | continue; | |
799 | if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) | |
800 | { | |
801 | /* Recurse to check the anonummous aggregate member. */ | |
802 | bad |= cx_check_missing_mem_inits | |
803 | (TREE_TYPE (field), NULL_TREE, complain); | |
804 | if (bad && !complain) | |
805 | return true; | |
806 | continue; | |
807 | } | |
09b42213 | 808 | ftype = strip_array_types (TREE_TYPE (field)); |
809 | if (type_has_constexpr_default_constructor (ftype)) | |
810 | { | |
811 | /* It's OK to skip a member with a trivial constexpr ctor. | |
812 | A constexpr ctor that isn't trivial should have been | |
813 | added in by now. */ | |
814 | gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype) | |
815 | || errorcount != 0); | |
816 | continue; | |
817 | } | |
818 | if (!complain) | |
819 | return true; | |
859e2c29 | 820 | error ("member %qD must be initialized by mem-initializer " |
821 | "in %<constexpr%> constructor", field); | |
822 | inform (DECL_SOURCE_LOCATION (field), "declared here"); | |
09b42213 | 823 | bad = true; |
824 | } | |
825 | if (field == NULL_TREE) | |
826 | break; | |
2f4cab3c | 827 | |
828 | if (ANON_AGGR_TYPE_P (TREE_TYPE (index))) | |
829 | { | |
830 | /* Check the anonymous aggregate initializer is valid. */ | |
831 | bad |= cx_check_missing_mem_inits | |
832 | (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain); | |
833 | if (bad && !complain) | |
834 | return true; | |
835 | } | |
09b42213 | 836 | field = DECL_CHAIN (field); |
837 | } | |
838 | ||
839 | return bad; | |
840 | } | |
841 | ||
842 | /* We are processing the definition of the constexpr function FUN. | |
843 | Check that its BODY fulfills the propriate requirements and | |
844 | enter it in the constexpr function definition table. | |
845 | For constructor BODY is actually the TREE_LIST of the | |
846 | member-initializer list. */ | |
847 | ||
848 | tree | |
849 | register_constexpr_fundef (tree fun, tree body) | |
850 | { | |
851 | constexpr_fundef entry; | |
852 | constexpr_fundef **slot; | |
853 | ||
854 | if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun))) | |
855 | return NULL; | |
856 | ||
6a385150 | 857 | tree massaged = massage_constexpr_body (fun, body); |
858 | if (massaged == NULL_TREE || massaged == error_mark_node) | |
09b42213 | 859 | { |
860 | if (!DECL_CONSTRUCTOR_P (fun)) | |
5967b28b | 861 | error ("body of %<constexpr%> function %qD not a return-statement", |
862 | fun); | |
09b42213 | 863 | return NULL; |
864 | } | |
865 | ||
6a385150 | 866 | if (!potential_rvalue_constant_expression (massaged)) |
09b42213 | 867 | { |
868 | if (!DECL_GENERATED_P (fun)) | |
6a385150 | 869 | require_potential_rvalue_constant_expression (massaged); |
09b42213 | 870 | return NULL; |
871 | } | |
872 | ||
873 | if (DECL_CONSTRUCTOR_P (fun) | |
2f4cab3c | 874 | && cx_check_missing_mem_inits (DECL_CONTEXT (fun), |
875 | massaged, !DECL_GENERATED_P (fun))) | |
09b42213 | 876 | return NULL; |
877 | ||
878 | /* Create the constexpr function table if necessary. */ | |
879 | if (constexpr_fundef_table == NULL) | |
880 | constexpr_fundef_table | |
881 | = hash_table<constexpr_fundef_hasher>::create_ggc (101); | |
882 | ||
883 | entry.decl = fun; | |
884 | entry.body = body; | |
885 | slot = constexpr_fundef_table->find_slot (&entry, INSERT); | |
886 | ||
887 | gcc_assert (*slot == NULL); | |
888 | *slot = ggc_alloc<constexpr_fundef> (); | |
889 | **slot = entry; | |
890 | ||
891 | return fun; | |
892 | } | |
893 | ||
894 | /* FUN is a non-constexpr function called in a context that requires a | |
895 | constant expression. If it comes from a constexpr template, explain why | |
896 | the instantiation isn't constexpr. */ | |
897 | ||
898 | void | |
899 | explain_invalid_constexpr_fn (tree fun) | |
900 | { | |
901 | static hash_set<tree> *diagnosed; | |
902 | tree body; | |
903 | location_t save_loc; | |
33603066 | 904 | /* Only diagnose defaulted functions, lambdas, or instantiations. */ |
09b42213 | 905 | if (!DECL_DEFAULTED_FN (fun) |
33603066 | 906 | && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) |
09b42213 | 907 | && !is_instantiation_of_constexpr (fun)) |
908 | return; | |
909 | if (diagnosed == NULL) | |
910 | diagnosed = new hash_set<tree>; | |
911 | if (diagnosed->add (fun)) | |
912 | /* Already explained. */ | |
913 | return; | |
914 | ||
915 | save_loc = input_location; | |
33603066 | 916 | if (!lambda_static_thunk_p (fun)) |
917 | { | |
918 | /* Diagnostics should completely ignore the static thunk, so leave | |
919 | input_location set to our caller's location. */ | |
920 | input_location = DECL_SOURCE_LOCATION (fun); | |
921 | inform (input_location, | |
5967b28b | 922 | "%qD is not usable as a %<constexpr%> function because:", fun); |
33603066 | 923 | } |
09b42213 | 924 | /* First check the declaration. */ |
925 | if (is_valid_constexpr_fn (fun, true)) | |
926 | { | |
927 | /* Then if it's OK, the body. */ | |
33603066 | 928 | if (!DECL_DECLARED_CONSTEXPR_P (fun) |
929 | && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))) | |
09b42213 | 930 | explain_implicit_non_constexpr (fun); |
931 | else | |
932 | { | |
933 | body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun)); | |
934 | require_potential_rvalue_constant_expression (body); | |
935 | if (DECL_CONSTRUCTOR_P (fun)) | |
2f4cab3c | 936 | cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true); |
09b42213 | 937 | } |
938 | } | |
939 | input_location = save_loc; | |
940 | } | |
941 | ||
942 | /* Objects of this type represent calls to constexpr functions | |
943 | along with the bindings of parameters to their arguments, for | |
944 | the purpose of compile time evaluation. */ | |
945 | ||
946 | struct GTY((for_user)) constexpr_call { | |
947 | /* Description of the constexpr function definition. */ | |
948 | constexpr_fundef *fundef; | |
949 | /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE | |
950 | is a parameter _DECL and the TREE_VALUE is the value of the parameter. | |
951 | Note: This arrangement is made to accommodate the use of | |
952 | iterative_hash_template_arg (see pt.c). If you change this | |
953 | representation, also change the hash calculation in | |
954 | cxx_eval_call_expression. */ | |
955 | tree bindings; | |
956 | /* Result of the call. | |
957 | NULL means the call is being evaluated. | |
958 | error_mark_node means that the evaluation was erroneous; | |
959 | otherwise, the actuall value of the call. */ | |
960 | tree result; | |
961 | /* The hash of this call; we remember it here to avoid having to | |
962 | recalculate it when expanding the hash table. */ | |
963 | hashval_t hash; | |
964 | }; | |
965 | ||
b594087e | 966 | struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call> |
09b42213 | 967 | { |
968 | static hashval_t hash (constexpr_call *); | |
969 | static bool equal (constexpr_call *, constexpr_call *); | |
cf72f34d | 970 | }; |
971 | ||
2a2770c6 | 972 | enum constexpr_switch_state { |
973 | /* Used when processing a switch for the first time by cxx_eval_switch_expr | |
974 | and default: label for that switch has not been seen yet. */ | |
975 | css_default_not_seen, | |
976 | /* Used when processing a switch for the first time by cxx_eval_switch_expr | |
977 | and default: label for that switch has been seen already. */ | |
978 | css_default_seen, | |
979 | /* Used when processing a switch for the second time by | |
980 | cxx_eval_switch_expr, where default: label should match. */ | |
981 | css_default_processing | |
982 | }; | |
983 | ||
cf72f34d | 984 | /* The constexpr expansion context. CALL is the current function |
985 | expansion, CTOR is the current aggregate initializer, OBJECT is the | |
986 | object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES | |
987 | is a map of values of variables initialized within the expression. */ | |
988 | ||
989 | struct constexpr_ctx { | |
f9885b8a | 990 | /* The innermost call we're evaluating. */ |
cf72f34d | 991 | constexpr_call *call; |
f9885b8a | 992 | /* Values for any temporaries or local variables within the |
993 | constant-expression. */ | |
cf72f34d | 994 | hash_map<tree,tree> *values; |
2631cb67 | 995 | /* SAVE_EXPRs that we've seen within the current LOOP_EXPR. NULL if we |
996 | aren't inside a loop. */ | |
997 | hash_set<tree> *save_exprs; | |
f9885b8a | 998 | /* The CONSTRUCTOR we're currently building up for an aggregate |
999 | initializer. */ | |
cf72f34d | 1000 | tree ctor; |
f9885b8a | 1001 | /* The object we're building the CONSTRUCTOR for. */ |
cf72f34d | 1002 | tree object; |
2a2770c6 | 1003 | /* If inside SWITCH_EXPR. */ |
1004 | constexpr_switch_state *css_state; | |
f9885b8a | 1005 | /* Whether we should error on a non-constant expression or fail quietly. */ |
f83e6885 | 1006 | bool quiet; |
f9885b8a | 1007 | /* Whether we are strictly conforming to constant expression rules or |
1008 | trying harder to get a constant value. */ | |
2055d27a | 1009 | bool strict; |
cf72f34d | 1010 | }; |
09b42213 | 1011 | |
1012 | /* A table of all constexpr calls that have been evaluated by the | |
1013 | compiler in this translation unit. */ | |
1014 | ||
a050099a | 1015 | static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table; |
09b42213 | 1016 | |
cf72f34d | 1017 | static tree cxx_eval_constant_expression (const constexpr_ctx *, tree, |
b2a43197 | 1018 | bool, bool *, bool *, tree * = NULL); |
09b42213 | 1019 | |
1020 | /* Compute a hash value for a constexpr call representation. */ | |
1021 | ||
1022 | inline hashval_t | |
1023 | constexpr_call_hasher::hash (constexpr_call *info) | |
1024 | { | |
1025 | return info->hash; | |
1026 | } | |
1027 | ||
1028 | /* Return true if the objects pointed to by P and Q represent calls | |
1029 | to the same constexpr function with the same arguments. | |
1030 | Otherwise, return false. */ | |
1031 | ||
1032 | bool | |
1033 | constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs) | |
1034 | { | |
1035 | tree lhs_bindings; | |
1036 | tree rhs_bindings; | |
1037 | if (lhs == rhs) | |
c751e2cd | 1038 | return true; |
1039 | if (lhs->hash != rhs->hash) | |
1040 | return false; | |
09b42213 | 1041 | if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef)) |
c751e2cd | 1042 | return false; |
09b42213 | 1043 | lhs_bindings = lhs->bindings; |
1044 | rhs_bindings = rhs->bindings; | |
1045 | while (lhs_bindings != NULL && rhs_bindings != NULL) | |
1046 | { | |
1047 | tree lhs_arg = TREE_VALUE (lhs_bindings); | |
1048 | tree rhs_arg = TREE_VALUE (rhs_bindings); | |
1049 | gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg)); | |
1050 | if (!cp_tree_equal (lhs_arg, rhs_arg)) | |
c751e2cd | 1051 | return false; |
09b42213 | 1052 | lhs_bindings = TREE_CHAIN (lhs_bindings); |
1053 | rhs_bindings = TREE_CHAIN (rhs_bindings); | |
1054 | } | |
1055 | return lhs_bindings == rhs_bindings; | |
1056 | } | |
1057 | ||
1058 | /* Initialize the constexpr call table, if needed. */ | |
1059 | ||
1060 | static void | |
1061 | maybe_initialize_constexpr_call_table (void) | |
1062 | { | |
1063 | if (constexpr_call_table == NULL) | |
1064 | constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101); | |
1065 | } | |
1066 | ||
4f7ebe46 | 1067 | /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when |
1068 | a function happens to get called recursively, we unshare the callee | |
1069 | function's body and evaluate this unshared copy instead of evaluating the | |
1070 | original body. | |
1071 | ||
1072 | FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function | |
1073 | copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map | |
a050099a | 1074 | that's keyed off of the original FUNCTION_DECL and whose value is a |
1075 | TREE_LIST of this function's unused copies awaiting reuse. | |
4f7ebe46 | 1076 | |
a050099a | 1077 | This is not GC-deletable to avoid GC affecting UID generation. */ |
4f7ebe46 | 1078 | |
a050099a | 1079 | static GTY(()) hash_map<tree, tree> *fundef_copies_table; |
4f7ebe46 | 1080 | |
1081 | /* Initialize FUNDEF_COPIES_TABLE if it's not initialized. */ | |
1082 | ||
1083 | static void | |
1084 | maybe_initialize_fundef_copies_table () | |
1085 | { | |
a050099a | 1086 | if (fundef_copies_table == NULL) |
1087 | fundef_copies_table = hash_map<tree,tree>::create_ggc (101); | |
4f7ebe46 | 1088 | } |
1089 | ||
1090 | /* Reuse a copy or create a new unshared copy of the function FUN. | |
886d5463 | 1091 | Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE |
1092 | is parms, TYPE is result. */ | |
4f7ebe46 | 1093 | |
a050099a | 1094 | static tree |
4f7ebe46 | 1095 | get_fundef_copy (tree fun) |
1096 | { | |
1097 | maybe_initialize_fundef_copies_table (); | |
1098 | ||
a050099a | 1099 | tree copy; |
886d5463 | 1100 | bool existed; |
1101 | tree *slot = &fundef_copies_table->get_or_insert (fun, &existed); | |
1102 | ||
1103 | if (!existed) | |
4f7ebe46 | 1104 | { |
886d5463 | 1105 | /* There is no cached function available, or in use. We can use |
1106 | the function directly. That the slot is now created records | |
1107 | that this function is now in use. */ | |
1108 | copy = build_tree_list (DECL_SAVED_TREE (fun), DECL_ARGUMENTS (fun)); | |
1109 | TREE_TYPE (copy) = DECL_RESULT (fun); | |
1110 | } | |
1111 | else if (*slot == NULL_TREE) | |
1112 | { | |
1113 | /* We've already used the function itself, so make a copy. */ | |
a050099a | 1114 | copy = build_tree_list (NULL, NULL); |
a050099a | 1115 | TREE_PURPOSE (copy) = copy_fn (fun, TREE_VALUE (copy), TREE_TYPE (copy)); |
4f7ebe46 | 1116 | } |
1117 | else | |
1118 | { | |
886d5463 | 1119 | /* We have a cached function available. */ |
4f7ebe46 | 1120 | copy = *slot; |
a050099a | 1121 | *slot = TREE_CHAIN (copy); |
4f7ebe46 | 1122 | } |
1123 | ||
1124 | return copy; | |
1125 | } | |
1126 | ||
886d5463 | 1127 | /* Save the copy COPY of function FUN for later reuse by |
1128 | get_fundef_copy(). By construction, there will always be an entry | |
1129 | to find. */ | |
4f7ebe46 | 1130 | |
1131 | static void | |
a050099a | 1132 | save_fundef_copy (tree fun, tree copy) |
4f7ebe46 | 1133 | { |
886d5463 | 1134 | tree *slot = fundef_copies_table->get (fun); |
a050099a | 1135 | TREE_CHAIN (copy) = *slot; |
4f7ebe46 | 1136 | *slot = copy; |
1137 | } | |
1138 | ||
09b42213 | 1139 | /* We have an expression tree T that represents a call, either CALL_EXPR |
1140 | or AGGR_INIT_EXPR. Return the Nth argument. */ | |
1141 | ||
1142 | static inline tree | |
1143 | get_nth_callarg (tree t, int n) | |
1144 | { | |
1145 | switch (TREE_CODE (t)) | |
1146 | { | |
1147 | case CALL_EXPR: | |
1148 | return CALL_EXPR_ARG (t, n); | |
1149 | ||
1150 | case AGGR_INIT_EXPR: | |
1151 | return AGGR_INIT_EXPR_ARG (t, n); | |
1152 | ||
1153 | default: | |
1154 | gcc_unreachable (); | |
1155 | return NULL; | |
1156 | } | |
1157 | } | |
1158 | ||
09b42213 | 1159 | /* Attempt to evaluate T which represents a call to a builtin function. |
1160 | We assume here that all builtin functions evaluate to scalar types | |
1161 | represented by _CST nodes. */ | |
1162 | ||
1163 | static tree | |
f1c6af10 | 1164 | cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun, |
1227ba74 | 1165 | bool lval, |
09b42213 | 1166 | bool *non_constant_p, bool *overflow_p) |
1167 | { | |
1168 | const int nargs = call_expr_nargs (t); | |
1169 | tree *args = (tree *) alloca (nargs * sizeof (tree)); | |
1170 | tree new_call; | |
1171 | int i; | |
f1c6af10 | 1172 | |
1173 | /* Don't fold __builtin_constant_p within a constexpr function. */ | |
efe6a40a | 1174 | bool bi_const_p = (DECL_FUNCTION_CODE (fun) == BUILT_IN_CONSTANT_P); |
1175 | ||
0f5cec54 | 1176 | /* If we aren't requiring a constant expression, defer __builtin_constant_p |
1177 | in a constexpr function until we have values for the parameters. */ | |
efe6a40a | 1178 | if (bi_const_p |
0f5cec54 | 1179 | && ctx->quiet |
f1c6af10 | 1180 | && current_function_decl |
1181 | && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) | |
09b42213 | 1182 | { |
f1c6af10 | 1183 | *non_constant_p = true; |
1184 | return t; | |
09b42213 | 1185 | } |
f1c6af10 | 1186 | |
1187 | /* Be permissive for arguments to built-ins; __builtin_constant_p should | |
1188 | return constant false for a non-constant argument. */ | |
1189 | constexpr_ctx new_ctx = *ctx; | |
1190 | new_ctx.quiet = true; | |
1191 | bool dummy1 = false, dummy2 = false; | |
1192 | for (i = 0; i < nargs; ++i) | |
efe6a40a | 1193 | { |
91b66c3c | 1194 | args[i] = CALL_EXPR_ARG (t, i); |
1195 | /* If builtin_valid_in_constant_expr_p is true, | |
1196 | potential_constant_expression_1 has not recursed into the arguments | |
1197 | of the builtin, verify it here. */ | |
1198 | if (!builtin_valid_in_constant_expr_p (fun) | |
1199 | || potential_constant_expression (args[i])) | |
1200 | args[i] = cxx_eval_constant_expression (&new_ctx, args[i], false, | |
1201 | &dummy1, &dummy2); | |
efe6a40a | 1202 | if (bi_const_p) |
1203 | /* For __built_in_constant_p, fold all expressions with constant values | |
1204 | even if they aren't C++ constant-expressions. */ | |
1205 | args[i] = cp_fully_fold (args[i]); | |
1206 | } | |
f1c6af10 | 1207 | |
1208 | bool save_ffbcp = force_folding_builtin_constant_p; | |
1209 | force_folding_builtin_constant_p = true; | |
051eb924 | 1210 | new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t), |
1211 | CALL_EXPR_FN (t), nargs, args); | |
f1c6af10 | 1212 | force_folding_builtin_constant_p = save_ffbcp; |
051eb924 | 1213 | if (new_call == NULL) |
1214 | { | |
1215 | if (!*non_constant_p && !ctx->quiet) | |
1216 | { | |
2fb20ba2 | 1217 | /* Do not allow__builtin_unreachable in constexpr function. |
1218 | The __builtin_unreachable call with BUILTINS_LOCATION | |
1219 | comes from cp_maybe_instrument_return. */ | |
1220 | if (DECL_FUNCTION_CODE (fun) == BUILT_IN_UNREACHABLE | |
1221 | && EXPR_LOCATION (t) == BUILTINS_LOCATION) | |
5967b28b | 1222 | error ("%<constexpr%> call flows off the end of the function"); |
2fb20ba2 | 1223 | else |
1224 | { | |
1225 | new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t), | |
1226 | CALL_EXPR_FN (t), nargs, args); | |
1227 | error ("%q+E is not a constant expression", new_call); | |
1228 | } | |
051eb924 | 1229 | } |
1230 | *non_constant_p = true; | |
1231 | return t; | |
1232 | } | |
1233 | ||
7e1f8be4 | 1234 | if (!is_constant_expression (new_call)) |
051eb924 | 1235 | { |
1236 | if (!*non_constant_p && !ctx->quiet) | |
1237 | error ("%q+E is not a constant expression", new_call); | |
1238 | *non_constant_p = true; | |
1239 | return t; | |
1240 | } | |
1241 | ||
1242 | return cxx_eval_constant_expression (&new_ctx, new_call, lval, | |
1243 | non_constant_p, overflow_p); | |
09b42213 | 1244 | } |
1245 | ||
1246 | /* TEMP is the constant value of a temporary object of type TYPE. Adjust | |
1247 | the type of the value to match. */ | |
1248 | ||
1249 | static tree | |
1250 | adjust_temp_type (tree type, tree temp) | |
1251 | { | |
1252 | if (TREE_TYPE (temp) == type) | |
1253 | return temp; | |
1254 | /* Avoid wrapping an aggregate value in a NOP_EXPR. */ | |
1255 | if (TREE_CODE (temp) == CONSTRUCTOR) | |
1256 | return build_constructor (type, CONSTRUCTOR_ELTS (temp)); | |
1257 | gcc_assert (scalarish_type_p (type)); | |
1258 | return cp_fold_convert (type, temp); | |
1259 | } | |
1260 | ||
e283bb4f | 1261 | /* Callback for walk_tree used by unshare_constructor. */ |
1262 | ||
1263 | static tree | |
1264 | find_constructor (tree *tp, int *walk_subtrees, void *) | |
1265 | { | |
1266 | if (TYPE_P (*tp)) | |
1267 | *walk_subtrees = 0; | |
1268 | if (TREE_CODE (*tp) == CONSTRUCTOR) | |
1269 | return *tp; | |
1270 | return NULL_TREE; | |
1271 | } | |
1272 | ||
1273 | /* If T is a CONSTRUCTOR or an expression that has a CONSTRUCTOR node as a | |
1274 | subexpression, return an unshared copy of T. Otherwise return T. */ | |
1275 | ||
1276 | static tree | |
1277 | unshare_constructor (tree t) | |
1278 | { | |
1279 | tree ctor = walk_tree (&t, find_constructor, NULL, NULL); | |
1280 | if (ctor != NULL_TREE) | |
1281 | return unshare_expr (t); | |
1282 | return t; | |
1283 | } | |
1284 | ||
09b42213 | 1285 | /* Subroutine of cxx_eval_call_expression. |
1286 | We are processing a call expression (either CALL_EXPR or | |
cf72f34d | 1287 | AGGR_INIT_EXPR) in the context of CTX. Evaluate |
09b42213 | 1288 | all arguments and bind their values to correspondings |
1289 | parameters, making up the NEW_CALL context. */ | |
1290 | ||
1291 | static void | |
cf72f34d | 1292 | cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, |
09b42213 | 1293 | constexpr_call *new_call, |
c8f6aeb1 | 1294 | bool *non_constant_p, bool *overflow_p, |
1295 | bool *non_constant_args) | |
09b42213 | 1296 | { |
1297 | const int nargs = call_expr_nargs (t); | |
1298 | tree fun = new_call->fundef->decl; | |
1299 | tree parms = DECL_ARGUMENTS (fun); | |
1300 | int i; | |
9c96033c | 1301 | tree *p = &new_call->bindings; |
09b42213 | 1302 | for (i = 0; i < nargs; ++i) |
1303 | { | |
1304 | tree x, arg; | |
1305 | tree type = parms ? TREE_TYPE (parms) : void_type_node; | |
09b42213 | 1306 | x = get_nth_callarg (t, i); |
cf72f34d | 1307 | /* For member function, the first argument is a pointer to the implied |
1308 | object. For a constructor, it might still be a dummy object, in | |
9c96033c | 1309 | which case we get the real argument from ctx. */ |
cf72f34d | 1310 | if (i == 0 && DECL_CONSTRUCTOR_P (fun) |
1311 | && is_dummy_object (x)) | |
1312 | { | |
1313 | x = ctx->object; | |
c017458d | 1314 | x = build_address (x); |
cf72f34d | 1315 | } |
2e1c71c2 | 1316 | arg = cxx_eval_constant_expression (ctx, x, /*lval=*/false, |
b2a43197 | 1317 | non_constant_p, overflow_p); |
09b42213 | 1318 | /* Don't VERIFY_CONSTANT here. */ |
f83e6885 | 1319 | if (*non_constant_p && ctx->quiet) |
09b42213 | 1320 | return; |
1321 | /* Just discard ellipsis args after checking their constantitude. */ | |
1322 | if (!parms) | |
1323 | continue; | |
2ba934c7 | 1324 | |
1325 | if (!*non_constant_p) | |
1326 | { | |
24d5b4f0 | 1327 | /* Don't share a CONSTRUCTOR that might be changed. */ |
1328 | arg = unshare_constructor (arg); | |
2ba934c7 | 1329 | /* Make sure the binding has the same type as the parm. But |
1330 | only for constant args. */ | |
90ad495b | 1331 | if (!TYPE_REF_P (type)) |
2ba934c7 | 1332 | arg = adjust_temp_type (type, arg); |
1333 | if (!TREE_CONSTANT (arg)) | |
1334 | *non_constant_args = true; | |
1335 | *p = build_tree_list (parms, arg); | |
1336 | p = &TREE_CHAIN (*p); | |
1337 | } | |
09b42213 | 1338 | parms = TREE_CHAIN (parms); |
1339 | } | |
1340 | } | |
1341 | ||
1342 | /* Variables and functions to manage constexpr call expansion context. | |
1343 | These do not need to be marked for PCH or GC. */ | |
1344 | ||
1345 | /* FIXME remember and print actual constant arguments. */ | |
16fb756f | 1346 | static vec<tree> call_stack; |
09b42213 | 1347 | static int call_stack_tick; |
1348 | static int last_cx_error_tick; | |
1349 | ||
1350 | static bool | |
1351 | push_cx_call_context (tree call) | |
1352 | { | |
1353 | ++call_stack_tick; | |
1354 | if (!EXPR_HAS_LOCATION (call)) | |
1355 | SET_EXPR_LOCATION (call, input_location); | |
1356 | call_stack.safe_push (call); | |
1357 | if (call_stack.length () > (unsigned) max_constexpr_depth) | |
1358 | return false; | |
1359 | return true; | |
1360 | } | |
1361 | ||
1362 | static void | |
1363 | pop_cx_call_context (void) | |
1364 | { | |
1365 | ++call_stack_tick; | |
1366 | call_stack.pop (); | |
1367 | } | |
1368 | ||
1369 | vec<tree> | |
1370 | cx_error_context (void) | |
1371 | { | |
1372 | vec<tree> r = vNULL; | |
1373 | if (call_stack_tick != last_cx_error_tick | |
1374 | && !call_stack.is_empty ()) | |
1375 | r = call_stack; | |
1376 | last_cx_error_tick = call_stack_tick; | |
1377 | return r; | |
1378 | } | |
1379 | ||
732905bb | 1380 | /* Evaluate a call T to a GCC internal function when possible and return |
1381 | the evaluated result or, under the control of CTX, give an error, set | |
1382 | NON_CONSTANT_P, and return the unevaluated call T otherwise. */ | |
1383 | ||
1384 | static tree | |
1385 | cxx_eval_internal_function (const constexpr_ctx *ctx, tree t, | |
1386 | bool lval, | |
1387 | bool *non_constant_p, bool *overflow_p) | |
1388 | { | |
1389 | enum tree_code opcode = ERROR_MARK; | |
1390 | ||
1391 | switch (CALL_EXPR_IFN (t)) | |
1392 | { | |
1393 | case IFN_UBSAN_NULL: | |
1394 | case IFN_UBSAN_BOUNDS: | |
1395 | case IFN_UBSAN_VPTR: | |
3c77f69c | 1396 | case IFN_FALLTHROUGH: |
732905bb | 1397 | return void_node; |
1398 | ||
1399 | case IFN_ADD_OVERFLOW: | |
1400 | opcode = PLUS_EXPR; | |
1401 | break; | |
1402 | case IFN_SUB_OVERFLOW: | |
1403 | opcode = MINUS_EXPR; | |
1404 | break; | |
1405 | case IFN_MUL_OVERFLOW: | |
1406 | opcode = MULT_EXPR; | |
1407 | break; | |
1408 | ||
6e1b2ffb | 1409 | case IFN_LAUNDER: |
1410 | return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), | |
1411 | false, non_constant_p, overflow_p); | |
1412 | ||
732905bb | 1413 | default: |
1414 | if (!ctx->quiet) | |
d3a3cfb8 | 1415 | error_at (cp_expr_loc_or_loc (t, input_location), |
732905bb | 1416 | "call to internal function %qE", t); |
1417 | *non_constant_p = true; | |
1418 | return t; | |
1419 | } | |
1420 | ||
1421 | /* Evaluate constant arguments using OPCODE and return a complex | |
1422 | number containing the result and the overflow bit. */ | |
1423 | tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval, | |
1424 | non_constant_p, overflow_p); | |
1425 | tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval, | |
1426 | non_constant_p, overflow_p); | |
1427 | ||
1428 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) | |
1429 | { | |
d3a3cfb8 | 1430 | location_t loc = cp_expr_loc_or_loc (t, input_location); |
732905bb | 1431 | tree type = TREE_TYPE (TREE_TYPE (t)); |
1432 | tree result = fold_binary_loc (loc, opcode, type, | |
1433 | fold_convert_loc (loc, type, arg0), | |
1434 | fold_convert_loc (loc, type, arg1)); | |
1435 | tree ovf | |
1436 | = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1)); | |
1437 | /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */ | |
1438 | if (TREE_OVERFLOW (result)) | |
1439 | TREE_OVERFLOW (result) = 0; | |
1440 | ||
1441 | return build_complex (TREE_TYPE (t), result, ovf); | |
1442 | } | |
1443 | ||
1444 | *non_constant_p = true; | |
1445 | return t; | |
1446 | } | |
1447 | ||
79cdb4f5 | 1448 | /* Clean CONSTRUCTOR_NO_IMPLICIT_ZERO from CTOR and its sub-aggregates. */ |
1449 | ||
1450 | static void | |
1451 | clear_no_implicit_zero (tree ctor) | |
1452 | { | |
1453 | if (CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor)) | |
1454 | { | |
1455 | CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor) = false; | |
1456 | tree elt; unsigned HOST_WIDE_INT idx; | |
1457 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), idx, elt) | |
1458 | if (TREE_CODE (elt) == CONSTRUCTOR) | |
1459 | clear_no_implicit_zero (elt); | |
1460 | } | |
1461 | } | |
1462 | ||
09b42213 | 1463 | /* Subroutine of cxx_eval_constant_expression. |
1464 | Evaluate the call expression tree T in the context of OLD_CALL expression | |
1465 | evaluation. */ | |
1466 | ||
1467 | static tree | |
cf72f34d | 1468 | cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, |
1227ba74 | 1469 | bool lval, |
09b42213 | 1470 | bool *non_constant_p, bool *overflow_p) |
1471 | { | |
d3a3cfb8 | 1472 | location_t loc = cp_expr_loc_or_loc (t, input_location); |
09b42213 | 1473 | tree fun = get_function_named_in_call (t); |
09b42213 | 1474 | constexpr_call new_call = { NULL, NULL, NULL, 0 }; |
09b42213 | 1475 | bool depth_ok; |
1476 | ||
6b71bdb4 | 1477 | if (fun == NULL_TREE) |
732905bb | 1478 | return cxx_eval_internal_function (ctx, t, lval, |
1479 | non_constant_p, overflow_p); | |
6b71bdb4 | 1480 | |
09b42213 | 1481 | if (TREE_CODE (fun) != FUNCTION_DECL) |
1482 | { | |
1483 | /* Might be a constexpr function pointer. */ | |
f83e6885 | 1484 | fun = cxx_eval_constant_expression (ctx, fun, |
1227ba74 | 1485 | /*lval*/false, non_constant_p, |
b2a43197 | 1486 | overflow_p); |
09b42213 | 1487 | STRIP_NOPS (fun); |
1488 | if (TREE_CODE (fun) == ADDR_EXPR) | |
1489 | fun = TREE_OPERAND (fun, 0); | |
1490 | } | |
1491 | if (TREE_CODE (fun) != FUNCTION_DECL) | |
1492 | { | |
f83e6885 | 1493 | if (!ctx->quiet && !*non_constant_p) |
5967b28b | 1494 | error_at (loc, "expression %qE does not designate a %<constexpr%> " |
09b42213 | 1495 | "function", fun); |
1496 | *non_constant_p = true; | |
1497 | return t; | |
1498 | } | |
1499 | if (DECL_CLONED_FUNCTION_P (fun)) | |
1500 | fun = DECL_CLONED_FUNCTION (fun); | |
6b71bdb4 | 1501 | |
1502 | if (is_ubsan_builtin_p (fun)) | |
1503 | return void_node; | |
1504 | ||
09b42213 | 1505 | if (is_builtin_fn (fun)) |
f1c6af10 | 1506 | return cxx_eval_builtin_function_call (ctx, t, fun, |
1227ba74 | 1507 | lval, non_constant_p, overflow_p); |
09b42213 | 1508 | if (!DECL_DECLARED_CONSTEXPR_P (fun)) |
1509 | { | |
f83e6885 | 1510 | if (!ctx->quiet) |
09b42213 | 1511 | { |
1dcd84cf | 1512 | if (!lambda_static_thunk_p (fun)) |
5967b28b | 1513 | error_at (loc, "call to non-%<constexpr%> function %qD", fun); |
09b42213 | 1514 | explain_invalid_constexpr_fn (fun); |
1515 | } | |
1516 | *non_constant_p = true; | |
1517 | return t; | |
1518 | } | |
1519 | ||
9ebe2ea3 | 1520 | constexpr_ctx new_ctx = *ctx; |
1521 | if (DECL_CONSTRUCTOR_P (fun) && !ctx->object | |
1522 | && TREE_CODE (t) == AGGR_INIT_EXPR) | |
1523 | { | |
1524 | /* We want to have an initialization target for an AGGR_INIT_EXPR. | |
1525 | If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */ | |
1526 | new_ctx.object = AGGR_INIT_EXPR_SLOT (t); | |
1527 | tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL); | |
1528 | CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor) = true; | |
1529 | ctx->values->put (new_ctx.object, ctor); | |
1530 | ctx = &new_ctx; | |
1531 | } | |
1532 | ||
09b42213 | 1533 | /* Shortcut trivial constructor/op=. */ |
1534 | if (trivial_fn_p (fun)) | |
1535 | { | |
9ebe2ea3 | 1536 | tree init = NULL_TREE; |
09b42213 | 1537 | if (call_expr_nargs (t) == 2) |
9ebe2ea3 | 1538 | init = convert_from_reference (get_nth_callarg (t, 1)); |
09b42213 | 1539 | else if (TREE_CODE (t) == AGGR_INIT_EXPR |
1540 | && AGGR_INIT_ZERO_FIRST (t)) | |
9ebe2ea3 | 1541 | init = build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false); |
1542 | if (init) | |
1543 | { | |
1544 | tree op = get_nth_callarg (t, 0); | |
1545 | if (is_dummy_object (op)) | |
1546 | op = ctx->object; | |
1547 | else | |
1548 | op = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (op)), op); | |
1549 | tree set = build2 (MODIFY_EXPR, TREE_TYPE (op), op, init); | |
0ce12360 | 1550 | new_ctx.call = &new_call; |
1551 | return cxx_eval_constant_expression (&new_ctx, set, lval, | |
9ebe2ea3 | 1552 | non_constant_p, overflow_p); |
1553 | } | |
09b42213 | 1554 | } |
1555 | ||
8afcf831 | 1556 | /* We can't defer instantiating the function any longer. */ |
1557 | if (!DECL_INITIAL (fun) | |
1558 | && DECL_TEMPLOID_INSTANTIATION (fun)) | |
1559 | { | |
31db86df | 1560 | location_t save_loc = input_location; |
1561 | input_location = loc; | |
8afcf831 | 1562 | ++function_depth; |
1563 | instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false); | |
1564 | --function_depth; | |
31db86df | 1565 | input_location = save_loc; |
8afcf831 | 1566 | } |
1567 | ||
09b42213 | 1568 | /* If in direct recursive call, optimize definition search. */ |
0ce12360 | 1569 | if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun) |
cf72f34d | 1570 | new_call.fundef = ctx->call->fundef; |
09b42213 | 1571 | else |
1572 | { | |
1573 | new_call.fundef = retrieve_constexpr_fundef (fun); | |
d2a9b1c1 | 1574 | if (new_call.fundef == NULL || new_call.fundef->body == NULL |
1575 | || fun == current_function_decl) | |
09b42213 | 1576 | { |
f83e6885 | 1577 | if (!ctx->quiet) |
09b42213 | 1578 | { |
d2a9b1c1 | 1579 | /* We need to check for current_function_decl here in case we're |
1580 | being called during cp_fold_function, because at that point | |
1581 | DECL_INITIAL is set properly and we have a fundef but we | |
1582 | haven't lowered invisirefs yet (c++/70344). */ | |
1583 | if (DECL_INITIAL (fun) == error_mark_node | |
1584 | || fun == current_function_decl) | |
a0efa758 | 1585 | error_at (loc, "%qD called in a constant expression before its " |
1586 | "definition is complete", fun); | |
1587 | else if (DECL_INITIAL (fun)) | |
09b42213 | 1588 | { |
33603066 | 1589 | /* The definition of fun was somehow unsuitable. But pretend |
1590 | that lambda static thunks don't exist. */ | |
1591 | if (!lambda_static_thunk_p (fun)) | |
1592 | error_at (loc, "%qD called in a constant expression", fun); | |
09b42213 | 1593 | explain_invalid_constexpr_fn (fun); |
1594 | } | |
1595 | else | |
1596 | error_at (loc, "%qD used before its definition", fun); | |
1597 | } | |
1598 | *non_constant_p = true; | |
1599 | return t; | |
1600 | } | |
1601 | } | |
9c96033c | 1602 | |
c8f6aeb1 | 1603 | bool non_constant_args = false; |
cf72f34d | 1604 | cxx_bind_parameters_in_call (ctx, t, &new_call, |
c8f6aeb1 | 1605 | non_constant_p, overflow_p, &non_constant_args); |
09b42213 | 1606 | if (*non_constant_p) |
1607 | return t; | |
1608 | ||
1609 | depth_ok = push_cx_call_context (t); | |
1610 | ||
c8f6aeb1 | 1611 | tree result = NULL_TREE; |
09b42213 | 1612 | |
c8f6aeb1 | 1613 | constexpr_call *entry = NULL; |
167a09fb | 1614 | if (depth_ok && !non_constant_args && ctx->strict) |
09b42213 | 1615 | { |
c8f6aeb1 | 1616 | new_call.hash = iterative_hash_template_arg |
1617 | (new_call.bindings, constexpr_fundef_hasher::hash (new_call.fundef)); | |
1618 | ||
1619 | /* If we have seen this call before, we are done. */ | |
1620 | maybe_initialize_constexpr_call_table (); | |
1621 | constexpr_call **slot | |
1622 | = constexpr_call_table->find_slot (&new_call, INSERT); | |
1623 | entry = *slot; | |
1624 | if (entry == NULL) | |
1625 | { | |
1626 | /* We need to keep a pointer to the entry, not just the slot, as the | |
1627 | slot can move in the call to cxx_eval_builtin_function_call. */ | |
1628 | *slot = entry = ggc_alloc<constexpr_call> (); | |
1629 | *entry = new_call; | |
1630 | } | |
2ba934c7 | 1631 | /* Calls that are in progress have their result set to NULL, |
c8f6aeb1 | 1632 | so that we can detect circular dependencies. */ |
1633 | else if (entry->result == NULL) | |
1634 | { | |
1635 | if (!ctx->quiet) | |
1636 | error ("call has circular dependency"); | |
1637 | *non_constant_p = true; | |
1638 | entry->result = result = error_mark_node; | |
1639 | } | |
1640 | else | |
1641 | result = entry->result; | |
09b42213 | 1642 | } |
1643 | ||
1644 | if (!depth_ok) | |
1645 | { | |
f83e6885 | 1646 | if (!ctx->quiet) |
5967b28b | 1647 | error ("%<constexpr%> evaluation depth exceeds maximum of %d (use " |
09b42213 | 1648 | "-fconstexpr-depth= to increase the maximum)", |
1649 | max_constexpr_depth); | |
1650 | *non_constant_p = true; | |
c8f6aeb1 | 1651 | result = error_mark_node; |
09b42213 | 1652 | } |
1653 | else | |
1654 | { | |
ab099088 | 1655 | if (result && result != error_mark_node) |
1656 | /* OK */; | |
1657 | else if (!DECL_SAVED_TREE (fun)) | |
1658 | { | |
1659 | /* When at_eof >= 2, cgraph has started throwing away | |
1660 | DECL_SAVED_TREE, so fail quietly. FIXME we get here because of | |
1661 | late code generation for VEC_INIT_EXPR, which needs to be | |
1662 | completely reconsidered. */ | |
1663 | gcc_assert (at_eof >= 2 && ctx->quiet); | |
1664 | *non_constant_p = true; | |
1665 | } | |
1666 | else | |
cf72f34d | 1667 | { |
4f7ebe46 | 1668 | tree body, parms, res; |
88a59139 | 1669 | |
4f7ebe46 | 1670 | /* Reuse or create a new unshared copy of this function's body. */ |
a050099a | 1671 | tree copy = get_fundef_copy (fun); |
1672 | body = TREE_PURPOSE (copy); | |
1673 | parms = TREE_VALUE (copy); | |
1674 | res = TREE_TYPE (copy); | |
88a59139 | 1675 | |
1676 | /* Associate the bindings with the remapped parms. */ | |
1677 | tree bound = new_call.bindings; | |
1678 | tree remapped = parms; | |
1679 | while (bound) | |
9c96033c | 1680 | { |
88a59139 | 1681 | tree oparm = TREE_PURPOSE (bound); |
1682 | tree arg = TREE_VALUE (bound); | |
1683 | gcc_assert (DECL_NAME (remapped) == DECL_NAME (oparm)); | |
5898f0d7 | 1684 | /* Don't share a CONSTRUCTOR that might be changed. */ |
e283bb4f | 1685 | arg = unshare_constructor (arg); |
88a59139 | 1686 | ctx->values->put (remapped, arg); |
1687 | bound = TREE_CHAIN (bound); | |
1688 | remapped = DECL_CHAIN (remapped); | |
9c96033c | 1689 | } |
88a59139 | 1690 | /* Add the RESULT_DECL to the values map, too. */ |
1691 | tree slot = NULL_TREE; | |
1692 | if (DECL_BY_REFERENCE (res)) | |
9c96033c | 1693 | { |
88a59139 | 1694 | slot = AGGR_INIT_EXPR_SLOT (t); |
1695 | tree addr = build_address (slot); | |
1696 | addr = build_nop (TREE_TYPE (res), addr); | |
1697 | ctx->values->put (res, addr); | |
1698 | ctx->values->put (slot, NULL_TREE); | |
1699 | } | |
1700 | else | |
1701 | ctx->values->put (res, NULL_TREE); | |
9c96033c | 1702 | |
4f7ebe46 | 1703 | /* Track the callee's evaluated SAVE_EXPRs so that we can forget |
1704 | their values after the call. */ | |
1705 | constexpr_ctx ctx_with_save_exprs = *ctx; | |
1706 | hash_set<tree> save_exprs; | |
1707 | ctx_with_save_exprs.save_exprs = &save_exprs; | |
43b7271d | 1708 | ctx_with_save_exprs.call = &new_call; |
4f7ebe46 | 1709 | |
88a59139 | 1710 | tree jump_target = NULL_TREE; |
4f7ebe46 | 1711 | cxx_eval_constant_expression (&ctx_with_save_exprs, body, |
88a59139 | 1712 | lval, non_constant_p, overflow_p, |
1713 | &jump_target); | |
9c96033c | 1714 | |
88a59139 | 1715 | if (DECL_CONSTRUCTOR_P (fun)) |
1716 | /* This can be null for a subobject constructor call, in | |
1717 | which case what we care about is the initialization | |
1718 | side-effects rather than the value. We could get at the | |
1719 | value by evaluating *this, but we don't bother; there's | |
1720 | no need to put such a call in the hash table. */ | |
1721 | result = lval ? ctx->object : ctx->ctor; | |
1722 | else if (VOID_TYPE_P (TREE_TYPE (res))) | |
1723 | result = void_node; | |
1724 | else | |
1725 | { | |
1726 | result = *ctx->values->get (slot ? slot : res); | |
1727 | if (result == NULL_TREE && !*non_constant_p) | |
9c96033c | 1728 | { |
88a59139 | 1729 | if (!ctx->quiet) |
5967b28b | 1730 | error ("%<constexpr%> call flows off the end " |
88a59139 | 1731 | "of the function"); |
1732 | *non_constant_p = true; | |
9c96033c | 1733 | } |
9c96033c | 1734 | } |
88a59139 | 1735 | |
4f7ebe46 | 1736 | /* Forget the saved values of the callee's SAVE_EXPRs. */ |
1737 | for (hash_set<tree>::iterator iter = save_exprs.begin(); | |
1738 | iter != save_exprs.end(); ++iter) | |
1739 | ctx_with_save_exprs.values->remove (*iter); | |
1740 | ||
88a59139 | 1741 | /* Remove the parms/result from the values map. Is it worth |
1742 | bothering to do this when the map itself is only live for | |
1743 | one constexpr evaluation? If so, maybe also clear out | |
1744 | other vars from call, maybe in BIND_EXPR handling? */ | |
1745 | ctx->values->remove (res); | |
1746 | if (slot) | |
1747 | ctx->values->remove (slot); | |
1748 | for (tree parm = parms; parm; parm = TREE_CHAIN (parm)) | |
1749 | ctx->values->remove (parm); | |
4f7ebe46 | 1750 | |
1751 | /* Make the unshared function copy we used available for re-use. */ | |
1752 | save_fundef_copy (fun, copy); | |
cf72f34d | 1753 | } |
9c96033c | 1754 | |
09b42213 | 1755 | if (result == error_mark_node) |
1756 | *non_constant_p = true; | |
c3f376bf | 1757 | if (*non_constant_p || *overflow_p) |
c8f6aeb1 | 1758 | result = error_mark_node; |
88a59139 | 1759 | else if (!result) |
9c96033c | 1760 | result = void_node; |
c8f6aeb1 | 1761 | if (entry) |
1762 | entry->result = result; | |
09b42213 | 1763 | } |
1764 | ||
c6d97004 | 1765 | /* The result of a constexpr function must be completely initialized. */ |
1766 | if (TREE_CODE (result) == CONSTRUCTOR) | |
79cdb4f5 | 1767 | clear_no_implicit_zero (result); |
c6d97004 | 1768 | |
09b42213 | 1769 | pop_cx_call_context (); |
e283bb4f | 1770 | return unshare_constructor (result); |
09b42213 | 1771 | } |
1772 | ||
1773 | /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */ | |
1774 | ||
1775 | bool | |
1776 | reduced_constant_expression_p (tree t) | |
1777 | { | |
a34934c2 | 1778 | if (t == NULL_TREE) |
1779 | return false; | |
1780 | ||
09b42213 | 1781 | switch (TREE_CODE (t)) |
1782 | { | |
1783 | case PTRMEM_CST: | |
1784 | /* Even if we can't lower this yet, it's constant. */ | |
1785 | return true; | |
1786 | ||
1787 | case CONSTRUCTOR: | |
1788 | /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */ | |
56da7fe6 | 1789 | tree idx, val, field; unsigned HOST_WIDE_INT i; |
1790 | if (CONSTRUCTOR_NO_IMPLICIT_ZERO (t)) | |
c4210852 | 1791 | { |
1792 | if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE) | |
1793 | /* An initialized vector would have a VECTOR_CST. */ | |
1794 | return false; | |
1795 | else | |
1796 | field = next_initializable_field (TYPE_FIELDS (TREE_TYPE (t))); | |
1797 | } | |
56da7fe6 | 1798 | else |
1799 | field = NULL_TREE; | |
1800 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, idx, val) | |
ce4ffd9e | 1801 | { |
a34934c2 | 1802 | /* If VAL is null, we're in the middle of initializing this |
1803 | element. */ | |
56da7fe6 | 1804 | if (!reduced_constant_expression_p (val)) |
ce4ffd9e | 1805 | return false; |
56da7fe6 | 1806 | if (field) |
1807 | { | |
1808 | if (idx != field) | |
1809 | return false; | |
1810 | field = next_initializable_field (DECL_CHAIN (field)); | |
1811 | } | |
ce4ffd9e | 1812 | } |
56da7fe6 | 1813 | if (field) |
1814 | return false; | |
1815 | else if (CONSTRUCTOR_NO_IMPLICIT_ZERO (t)) | |
1816 | /* All the fields are initialized. */ | |
1817 | CONSTRUCTOR_NO_IMPLICIT_ZERO (t) = false; | |
09b42213 | 1818 | return true; |
1819 | ||
1820 | default: | |
1821 | /* FIXME are we calling this too much? */ | |
1822 | return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE; | |
1823 | } | |
1824 | } | |
1825 | ||
1826 | /* Some expressions may have constant operands but are not constant | |
941fafa5 | 1827 | themselves, such as 1/0. Call this function to check for that |
1828 | condition. | |
09b42213 | 1829 | |
1830 | We only call this in places that require an arithmetic constant, not in | |
1831 | places where we might have a non-constant expression that can be a | |
1832 | component of a constant expression, such as the address of a constexpr | |
1833 | variable that might be dereferenced later. */ | |
1834 | ||
1835 | static bool | |
1836 | verify_constant (tree t, bool allow_non_constant, bool *non_constant_p, | |
1837 | bool *overflow_p) | |
1838 | { | |
1839 | if (!*non_constant_p && !reduced_constant_expression_p (t)) | |
1840 | { | |
1841 | if (!allow_non_constant) | |
1842 | error ("%q+E is not a constant expression", t); | |
1843 | *non_constant_p = true; | |
1844 | } | |
1845 | if (TREE_OVERFLOW_P (t)) | |
1846 | { | |
1847 | if (!allow_non_constant) | |
1848 | { | |
1849 | permerror (input_location, "overflow in constant expression"); | |
1850 | /* If we're being permissive (and are in an enforcing | |
1851 | context), ignore the overflow. */ | |
1852 | if (flag_permissive) | |
1853 | return *non_constant_p; | |
1854 | } | |
1855 | *overflow_p = true; | |
1856 | } | |
1857 | return *non_constant_p; | |
1858 | } | |
1859 | ||
2b035ffe | 1860 | /* Check whether the shift operation with code CODE and type TYPE on LHS |
1861 | and RHS is undefined. If it is, give an error with an explanation, | |
1862 | and return true; return false otherwise. */ | |
1863 | ||
1864 | static bool | |
1865 | cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx, | |
1866 | enum tree_code code, tree type, tree lhs, tree rhs) | |
1867 | { | |
1868 | if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR) | |
1869 | || TREE_CODE (lhs) != INTEGER_CST | |
1870 | || TREE_CODE (rhs) != INTEGER_CST) | |
1871 | return false; | |
1872 | ||
1873 | tree lhstype = TREE_TYPE (lhs); | |
1874 | unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs)); | |
1875 | ||
1876 | /* [expr.shift] The behavior is undefined if the right operand | |
1877 | is negative, or greater than or equal to the length in bits | |
1878 | of the promoted left operand. */ | |
1879 | if (tree_int_cst_sgn (rhs) == -1) | |
1880 | { | |
1881 | if (!ctx->quiet) | |
91d87e22 | 1882 | permerror (loc, "right operand of shift expression %q+E is negative", |
1883 | build2_loc (loc, code, type, lhs, rhs)); | |
1884 | return (!flag_permissive || ctx->quiet); | |
2b035ffe | 1885 | } |
1886 | if (compare_tree_int (rhs, uprec) >= 0) | |
1887 | { | |
1888 | if (!ctx->quiet) | |
91d87e22 | 1889 | permerror (loc, "right operand of shift expression %q+E is >= than " |
1890 | "the precision of the left operand", | |
1891 | build2_loc (loc, code, type, lhs, rhs)); | |
1892 | return (!flag_permissive || ctx->quiet); | |
2b035ffe | 1893 | } |
1894 | ||
1895 | /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...] | |
1896 | if E1 has a signed type and non-negative value, and E1x2^E2 is | |
1897 | representable in the corresponding unsigned type of the result type, | |
1898 | then that value, converted to the result type, is the resulting value; | |
1899 | otherwise, the behavior is undefined. */ | |
1900 | if (code == LSHIFT_EXPR && !TYPE_UNSIGNED (lhstype) | |
1901 | && (cxx_dialect >= cxx11)) | |
1902 | { | |
1903 | if (tree_int_cst_sgn (lhs) == -1) | |
1904 | { | |
1905 | if (!ctx->quiet) | |
91d87e22 | 1906 | permerror (loc, |
1907 | "left operand of shift expression %q+E is negative", | |
1908 | build2_loc (loc, code, type, lhs, rhs)); | |
1909 | return (!flag_permissive || ctx->quiet); | |
2b035ffe | 1910 | } |
1911 | /* For signed x << y the following: | |
1912 | (unsigned) x >> ((prec (lhs) - 1) - y) | |
1913 | if > 1, is undefined. The right-hand side of this formula | |
1914 | is the highest bit of the LHS that can be set (starting from 0), | |
1915 | so that the shift doesn't overflow. We then right-shift the LHS | |
1916 | to see whether any other bit is set making the original shift | |
1917 | undefined -- the result is not representable in the corresponding | |
1918 | unsigned type. */ | |
1919 | tree t = build_int_cst (unsigned_type_node, uprec - 1); | |
1920 | t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs); | |
1921 | tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs); | |
1922 | t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t); | |
1923 | if (tree_int_cst_lt (integer_one_node, t)) | |
1924 | { | |
1925 | if (!ctx->quiet) | |
91d87e22 | 1926 | permerror (loc, "shift expression %q+E overflows", |
1927 | build2_loc (loc, code, type, lhs, rhs)); | |
1928 | return (!flag_permissive || ctx->quiet); | |
2b035ffe | 1929 | } |
1930 | } | |
1931 | return false; | |
1932 | } | |
1933 | ||
09b42213 | 1934 | /* Subroutine of cxx_eval_constant_expression. |
1935 | Attempt to reduce the unary expression tree T to a compile time value. | |
1936 | If successful, return the value. Otherwise issue a diagnostic | |
1937 | and return error_mark_node. */ | |
1938 | ||
1939 | static tree | |
cf72f34d | 1940 | cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t, |
c8f6aeb1 | 1941 | bool /*lval*/, |
09b42213 | 1942 | bool *non_constant_p, bool *overflow_p) |
1943 | { | |
1944 | tree r; | |
1945 | tree orig_arg = TREE_OPERAND (t, 0); | |
c8f6aeb1 | 1946 | tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false, |
1947 | non_constant_p, overflow_p); | |
09b42213 | 1948 | VERIFY_CONSTANT (arg); |
93add516 | 1949 | location_t loc = EXPR_LOCATION (t); |
1950 | enum tree_code code = TREE_CODE (t); | |
1951 | tree type = TREE_TYPE (t); | |
1952 | r = fold_unary_loc (loc, code, type, arg); | |
1953 | if (r == NULL_TREE) | |
1954 | { | |
1955 | if (arg == orig_arg) | |
1956 | r = t; | |
1957 | else | |
1958 | r = build1_loc (loc, code, type, arg); | |
1959 | } | |
09b42213 | 1960 | VERIFY_CONSTANT (r); |
1961 | return r; | |
1962 | } | |
1963 | ||
46fad8d5 | 1964 | /* Helper function for cxx_eval_binary_expression. Try to optimize |
1965 | original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the | |
1966 | generic folding should be used. */ | |
1967 | ||
1968 | static tree | |
1969 | cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t, | |
1970 | tree lhs, tree rhs, bool *non_constant_p, | |
1971 | bool *overflow_p) | |
1972 | { | |
1973 | STRIP_NOPS (lhs); | |
1974 | if (TREE_CODE (lhs) != ADDR_EXPR) | |
1975 | return NULL_TREE; | |
1976 | ||
1977 | lhs = TREE_OPERAND (lhs, 0); | |
1978 | ||
1979 | /* &A[i] p+ j => &A[i + j] */ | |
1980 | if (TREE_CODE (lhs) == ARRAY_REF | |
1981 | && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST | |
1982 | && TREE_CODE (rhs) == INTEGER_CST | |
1983 | && TYPE_SIZE_UNIT (TREE_TYPE (lhs)) | |
1984 | && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST) | |
1985 | { | |
1986 | tree orig_type = TREE_TYPE (t); | |
1987 | location_t loc = EXPR_LOCATION (t); | |
1988 | tree type = TREE_TYPE (lhs); | |
1989 | ||
1990 | t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1)); | |
1991 | tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0))); | |
1992 | nelts = cxx_eval_constant_expression (ctx, nelts, false, non_constant_p, | |
1993 | overflow_p); | |
1994 | if (*non_constant_p) | |
1995 | return NULL_TREE; | |
1996 | /* Don't fold an out-of-bound access. */ | |
1997 | if (!tree_int_cst_le (t, nelts)) | |
1998 | return NULL_TREE; | |
1999 | rhs = cp_fold_convert (ssizetype, rhs); | |
2000 | /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT. | |
2001 | constexpr int A[1]; ... (char *)&A[0] + 1 */ | |
2002 | if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype, | |
2003 | rhs, TYPE_SIZE_UNIT (type)))) | |
2004 | return NULL_TREE; | |
2005 | /* Make sure to treat the second operand of POINTER_PLUS_EXPR | |
2006 | as signed. */ | |
2007 | rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs, | |
2008 | TYPE_SIZE_UNIT (type)); | |
2009 | t = size_binop_loc (loc, PLUS_EXPR, rhs, t); | |
2010 | t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0), | |
2011 | t, NULL_TREE, NULL_TREE); | |
2012 | t = cp_build_addr_expr (t, tf_warning_or_error); | |
2013 | t = cp_fold_convert (orig_type, t); | |
2014 | return cxx_eval_constant_expression (ctx, t, /*lval*/false, | |
2015 | non_constant_p, overflow_p); | |
2016 | } | |
2017 | ||
2018 | return NULL_TREE; | |
2019 | } | |
2020 | ||
09b42213 | 2021 | /* Subroutine of cxx_eval_constant_expression. |
2022 | Like cxx_eval_unary_expression, except for binary expressions. */ | |
2023 | ||
2024 | static tree | |
cf72f34d | 2025 | cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t, |
c8f6aeb1 | 2026 | bool /*lval*/, |
09b42213 | 2027 | bool *non_constant_p, bool *overflow_p) |
2028 | { | |
f7ef5392 | 2029 | tree r = NULL_TREE; |
09b42213 | 2030 | tree orig_lhs = TREE_OPERAND (t, 0); |
2031 | tree orig_rhs = TREE_OPERAND (t, 1); | |
2032 | tree lhs, rhs; | |
c8f6aeb1 | 2033 | lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false, |
b2a43197 | 2034 | non_constant_p, overflow_p); |
6d6737d9 | 2035 | /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer |
2036 | subtraction. */ | |
2037 | if (*non_constant_p) | |
2038 | return t; | |
c8f6aeb1 | 2039 | rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false, |
b2a43197 | 2040 | non_constant_p, overflow_p); |
6d6737d9 | 2041 | if (*non_constant_p) |
2042 | return t; | |
93add516 | 2043 | |
2044 | location_t loc = EXPR_LOCATION (t); | |
2045 | enum tree_code code = TREE_CODE (t); | |
2046 | tree type = TREE_TYPE (t); | |
f7ef5392 | 2047 | |
2048 | if (code == EQ_EXPR || code == NE_EXPR) | |
2049 | { | |
2050 | bool is_code_eq = (code == EQ_EXPR); | |
2051 | ||
2052 | if (TREE_CODE (lhs) == PTRMEM_CST | |
2053 | && TREE_CODE (rhs) == PTRMEM_CST) | |
e9f29fe5 | 2054 | { |
2055 | tree lmem = PTRMEM_CST_MEMBER (lhs); | |
2056 | tree rmem = PTRMEM_CST_MEMBER (rhs); | |
2057 | bool eq; | |
2058 | if (TREE_CODE (lmem) == TREE_CODE (rmem) | |
2059 | && TREE_CODE (lmem) == FIELD_DECL | |
2060 | && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE | |
2061 | && same_type_p (DECL_CONTEXT (lmem), | |
2062 | DECL_CONTEXT (rmem))) | |
2063 | /* If both refer to (possibly different) members of the same union | |
2064 | (12.3), they compare equal. */ | |
2065 | eq = true; | |
2066 | else | |
2067 | eq = cp_tree_equal (lhs, rhs); | |
2068 | r = constant_boolean_node (eq == is_code_eq, type); | |
2069 | } | |
f7ef5392 | 2070 | else if ((TREE_CODE (lhs) == PTRMEM_CST |
2071 | || TREE_CODE (rhs) == PTRMEM_CST) | |
2072 | && (null_member_pointer_value_p (lhs) | |
2073 | || null_member_pointer_value_p (rhs))) | |
2074 | r = constant_boolean_node (!is_code_eq, type); | |
f31f4e9c | 2075 | else if (TREE_CODE (lhs) == PTRMEM_CST) |
2076 | lhs = cplus_expand_constant (lhs); | |
2077 | else if (TREE_CODE (rhs) == PTRMEM_CST) | |
2078 | rhs = cplus_expand_constant (rhs); | |
f7ef5392 | 2079 | } |
cb768824 | 2080 | if (code == POINTER_PLUS_EXPR && !*non_constant_p |
2081 | && integer_zerop (lhs) && !integer_zerop (rhs)) | |
2082 | { | |
2083 | if (!ctx->quiet) | |
2084 | error ("arithmetic involving a null pointer in %qE", lhs); | |
2085 | return t; | |
2086 | } | |
46fad8d5 | 2087 | else if (code == POINTER_PLUS_EXPR) |
2088 | r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p, | |
2089 | overflow_p); | |
f7ef5392 | 2090 | |
2091 | if (r == NULL_TREE) | |
2092 | r = fold_binary_loc (loc, code, type, lhs, rhs); | |
2093 | ||
93add516 | 2094 | if (r == NULL_TREE) |
2095 | { | |
2096 | if (lhs == orig_lhs && rhs == orig_rhs) | |
2097 | r = t; | |
2098 | else | |
2099 | r = build2_loc (loc, code, type, lhs, rhs); | |
2100 | } | |
2b035ffe | 2101 | else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs)) |
2102 | *non_constant_p = true; | |
6d6737d9 | 2103 | /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to |
2104 | a local array in a constexpr function. */ | |
d03fa520 | 2105 | bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs)); |
35f2c28f | 2106 | if (!ptr) |
aae7465c | 2107 | VERIFY_CONSTANT (r); |
09b42213 | 2108 | return r; |
2109 | } | |
2110 | ||
2111 | /* Subroutine of cxx_eval_constant_expression. | |
2112 | Attempt to evaluate condition expressions. Dead branches are not | |
2113 | looked into. */ | |
2114 | ||
2115 | static tree | |
cf72f34d | 2116 | cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t, |
1227ba74 | 2117 | bool lval, |
00f21715 | 2118 | bool *non_constant_p, bool *overflow_p, |
2119 | tree *jump_target) | |
09b42213 | 2120 | { |
cf72f34d | 2121 | tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
c8f6aeb1 | 2122 | /*lval*/false, |
b2a43197 | 2123 | non_constant_p, overflow_p); |
09b42213 | 2124 | VERIFY_CONSTANT (val); |
2125 | /* Don't VERIFY_CONSTANT the other operands. */ | |
2126 | if (integer_zerop (val)) | |
cf72f34d | 2127 | return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2), |
1227ba74 | 2128 | lval, |
00f21715 | 2129 | non_constant_p, overflow_p, |
2130 | jump_target); | |
cf72f34d | 2131 | return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
1227ba74 | 2132 | lval, |
00f21715 | 2133 | non_constant_p, overflow_p, |
2134 | jump_target); | |
09b42213 | 2135 | } |
2136 | ||
31595caf | 2137 | /* Subroutine of cxx_eval_constant_expression. |
2138 | Attempt to evaluate vector condition expressions. Unlike | |
2139 | cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal | |
2140 | ternary arithmetics operation, where all 3 arguments have to be | |
2141 | evaluated as constants and then folding computes the result from | |
2142 | them. */ | |
2143 | ||
2144 | static tree | |
2145 | cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t, | |
2146 | bool *non_constant_p, bool *overflow_p) | |
2147 | { | |
2148 | tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), | |
2149 | /*lval*/false, | |
2150 | non_constant_p, overflow_p); | |
2151 | VERIFY_CONSTANT (arg1); | |
2152 | tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), | |
2153 | /*lval*/false, | |
2154 | non_constant_p, overflow_p); | |
2155 | VERIFY_CONSTANT (arg2); | |
2156 | tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2), | |
2157 | /*lval*/false, | |
2158 | non_constant_p, overflow_p); | |
2159 | VERIFY_CONSTANT (arg3); | |
2160 | location_t loc = EXPR_LOCATION (t); | |
2161 | tree type = TREE_TYPE (t); | |
2162 | tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); | |
2163 | if (r == NULL_TREE) | |
2164 | { | |
2165 | if (arg1 == TREE_OPERAND (t, 0) | |
2166 | && arg2 == TREE_OPERAND (t, 1) | |
2167 | && arg3 == TREE_OPERAND (t, 2)) | |
2168 | r = t; | |
2169 | else | |
2170 | r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); | |
2171 | } | |
2172 | VERIFY_CONSTANT (r); | |
2173 | return r; | |
2174 | } | |
2175 | ||
7963b19d | 2176 | /* Returns less than, equal to, or greater than zero if KEY is found to be |
2177 | less than, to match, or to be greater than the constructor_elt's INDEX. */ | |
2178 | ||
2179 | static int | |
2180 | array_index_cmp (tree key, tree index) | |
2181 | { | |
2182 | gcc_assert (TREE_CODE (key) == INTEGER_CST); | |
2183 | ||
2184 | switch (TREE_CODE (index)) | |
2185 | { | |
2186 | case INTEGER_CST: | |
2187 | return tree_int_cst_compare (key, index); | |
2188 | case RANGE_EXPR: | |
2189 | { | |
2190 | tree lo = TREE_OPERAND (index, 0); | |
2191 | tree hi = TREE_OPERAND (index, 1); | |
2192 | if (tree_int_cst_lt (key, lo)) | |
2193 | return -1; | |
2194 | else if (tree_int_cst_lt (hi, key)) | |
2195 | return 1; | |
2196 | else | |
2197 | return 0; | |
2198 | } | |
2199 | default: | |
2200 | gcc_unreachable (); | |
2201 | } | |
2202 | } | |
2203 | ||
2204 | /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1 | |
2205 | if none. If INSERT is true, insert a matching element rather than fail. */ | |
2206 | ||
2207 | static HOST_WIDE_INT | |
2208 | find_array_ctor_elt (tree ary, tree dindex, bool insert = false) | |
2209 | { | |
2210 | if (tree_int_cst_sgn (dindex) < 0) | |
2211 | return -1; | |
2212 | ||
2213 | unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex); | |
2214 | vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary); | |
2215 | unsigned HOST_WIDE_INT len = vec_safe_length (elts); | |
2216 | ||
2217 | unsigned HOST_WIDE_INT end = len; | |
2218 | unsigned HOST_WIDE_INT begin = 0; | |
2219 | ||
2220 | /* If the last element of the CONSTRUCTOR has its own index, we can assume | |
2221 | that the same is true of the other elements and index directly. */ | |
2222 | if (end > 0) | |
2223 | { | |
e99466d6 | 2224 | tree cindex = (*elts)[end - 1].index; |
7963b19d | 2225 | if (TREE_CODE (cindex) == INTEGER_CST |
e99466d6 | 2226 | && compare_tree_int (cindex, end - 1) == 0) |
7963b19d | 2227 | { |
2228 | if (i < end) | |
2229 | return i; | |
2230 | else | |
2231 | begin = end; | |
2232 | } | |
2233 | } | |
2234 | ||
2235 | /* Otherwise, find a matching index by means of a binary search. */ | |
2236 | while (begin != end) | |
2237 | { | |
2238 | unsigned HOST_WIDE_INT middle = (begin + end) / 2; | |
f0b916c7 | 2239 | constructor_elt &elt = (*elts)[middle]; |
2240 | tree idx = elt.index; | |
7963b19d | 2241 | |
f0b916c7 | 2242 | int cmp = array_index_cmp (dindex, idx); |
7963b19d | 2243 | if (cmp < 0) |
2244 | end = middle; | |
2245 | else if (cmp > 0) | |
2246 | begin = middle + 1; | |
2247 | else | |
f0b916c7 | 2248 | { |
2249 | if (insert && TREE_CODE (idx) == RANGE_EXPR) | |
2250 | { | |
2251 | /* We need to split the range. */ | |
2252 | constructor_elt e; | |
2253 | tree lo = TREE_OPERAND (idx, 0); | |
2254 | tree hi = TREE_OPERAND (idx, 1); | |
e99466d6 | 2255 | tree value = elt.value; |
2256 | dindex = fold_convert (sizetype, dindex); | |
f0b916c7 | 2257 | if (tree_int_cst_lt (lo, dindex)) |
2258 | { | |
2259 | /* There are still some lower elts; shorten the range. */ | |
2260 | tree new_hi = int_const_binop (MINUS_EXPR, dindex, | |
2261 | size_one_node); | |
2262 | if (tree_int_cst_equal (lo, new_hi)) | |
2263 | /* Only one element left, no longer a range. */ | |
2264 | elt.index = lo; | |
2265 | else | |
2266 | TREE_OPERAND (idx, 1) = new_hi; | |
2267 | /* Append the element we want to insert. */ | |
2268 | ++middle; | |
2269 | e.index = dindex; | |
e99466d6 | 2270 | e.value = unshare_constructor (value); |
f0b916c7 | 2271 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e); |
2272 | } | |
2273 | else | |
2274 | /* No lower elts, the range elt is now ours. */ | |
2275 | elt.index = dindex; | |
2276 | ||
2277 | if (tree_int_cst_lt (dindex, hi)) | |
2278 | { | |
2279 | /* There are still some higher elts; append a range. */ | |
2280 | tree new_lo = int_const_binop (PLUS_EXPR, dindex, | |
2281 | size_one_node); | |
2282 | if (tree_int_cst_equal (new_lo, hi)) | |
2283 | e.index = hi; | |
2284 | else | |
2285 | e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi); | |
e99466d6 | 2286 | e.value = unshare_constructor (value); |
2287 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e); | |
f0b916c7 | 2288 | } |
2289 | } | |
2290 | return middle; | |
2291 | } | |
7963b19d | 2292 | } |
2293 | ||
2294 | if (insert) | |
2295 | { | |
2296 | constructor_elt e = { dindex, NULL_TREE }; | |
2297 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e); | |
2298 | return end; | |
2299 | } | |
2300 | ||
2301 | return -1; | |
2302 | } | |
2303 | ||
40df9099 | 2304 | /* Under the control of CTX, issue a detailed diagnostic for |
2305 | an out-of-bounds subscript INDEX into the expression ARRAY. */ | |
2306 | ||
2307 | static void | |
2308 | diag_array_subscript (const constexpr_ctx *ctx, tree array, tree index) | |
2309 | { | |
2310 | if (!ctx->quiet) | |
2311 | { | |
2312 | tree arraytype = TREE_TYPE (array); | |
2313 | ||
2314 | /* Convert the unsigned array subscript to a signed integer to avoid | |
2315 | printing huge numbers for small negative values. */ | |
2316 | tree sidx = fold_convert (ssizetype, index); | |
2317 | if (DECL_P (array)) | |
2318 | { | |
d7d265db | 2319 | if (TYPE_DOMAIN (arraytype)) |
2320 | error ("array subscript value %qE is outside the bounds " | |
2321 | "of array %qD of type %qT", sidx, array, arraytype); | |
2322 | else | |
2323 | error ("non-zero array subscript %qE is used with array %qD of " | |
2324 | "type %qT with unknown bounds", sidx, array, arraytype); | |
40df9099 | 2325 | inform (DECL_SOURCE_LOCATION (array), "declared here"); |
2326 | } | |
d7d265db | 2327 | else if (TYPE_DOMAIN (arraytype)) |
40df9099 | 2328 | error ("array subscript value %qE is outside the bounds " |
2329 | "of array type %qT", sidx, arraytype); | |
d7d265db | 2330 | else |
2331 | error ("non-zero array subscript %qE is used with array of type %qT " | |
2332 | "with unknown bounds", sidx, arraytype); | |
40df9099 | 2333 | } |
2334 | } | |
7963b19d | 2335 | |
51f0c7e2 | 2336 | /* Return the number of elements for TYPE (which is an ARRAY_TYPE or |
2337 | a VECTOR_TYPE). */ | |
2338 | ||
2339 | static tree | |
2340 | get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type, | |
2341 | bool *non_constant_p, bool *overflow_p) | |
2342 | { | |
2343 | tree nelts; | |
2344 | if (TREE_CODE (type) == ARRAY_TYPE) | |
2345 | { | |
2346 | if (TYPE_DOMAIN (type)) | |
2347 | nelts = array_type_nelts_top (type); | |
2348 | else | |
2349 | nelts = size_zero_node; | |
2350 | } | |
2351 | else if (VECTOR_TYPE_P (type)) | |
2352 | nelts = size_int (TYPE_VECTOR_SUBPARTS (type)); | |
2353 | else | |
2354 | gcc_unreachable (); | |
2355 | ||
2356 | /* For VLAs, the number of elements won't be an integer constant. */ | |
2357 | nelts = cxx_eval_constant_expression (ctx, nelts, false, | |
2358 | non_constant_p, overflow_p); | |
2359 | return nelts; | |
2360 | } | |
2361 | ||
92fe8840 | 2362 | /* Extract element INDEX consisting of CHARS_PER_ELT chars from |
2363 | STRING_CST STRING. */ | |
2364 | ||
2365 | static tree | |
2366 | extract_string_elt (tree string, unsigned chars_per_elt, unsigned index) | |
2367 | { | |
2368 | tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string))); | |
2369 | tree r; | |
2370 | ||
2371 | if (chars_per_elt == 1) | |
2372 | r = build_int_cst (type, TREE_STRING_POINTER (string)[index]); | |
2373 | else | |
2374 | { | |
2375 | const unsigned char *ptr | |
2376 | = ((const unsigned char *)TREE_STRING_POINTER (string) | |
2377 | + index * chars_per_elt); | |
2378 | r = native_interpret_expr (type, ptr, chars_per_elt); | |
2379 | } | |
2380 | return r; | |
2381 | } | |
2382 | ||
09b42213 | 2383 | /* Subroutine of cxx_eval_constant_expression. |
2384 | Attempt to reduce a reference to an array slot. */ | |
2385 | ||
2386 | static tree | |
cf72f34d | 2387 | cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, |
1227ba74 | 2388 | bool lval, |
09b42213 | 2389 | bool *non_constant_p, bool *overflow_p) |
2390 | { | |
2391 | tree oldary = TREE_OPERAND (t, 0); | |
cf72f34d | 2392 | tree ary = cxx_eval_constant_expression (ctx, oldary, |
1227ba74 | 2393 | lval, |
b2a43197 | 2394 | non_constant_p, overflow_p); |
09b42213 | 2395 | tree index, oldidx; |
cf3cefc9 | 2396 | HOST_WIDE_INT i = 0; |
2397 | tree elem_type = NULL_TREE; | |
2398 | unsigned len = 0, elem_nchars = 1; | |
09b42213 | 2399 | if (*non_constant_p) |
2400 | return t; | |
2401 | oldidx = TREE_OPERAND (t, 1); | |
cf72f34d | 2402 | index = cxx_eval_constant_expression (ctx, oldidx, |
f83e6885 | 2403 | false, |
b2a43197 | 2404 | non_constant_p, overflow_p); |
09b42213 | 2405 | VERIFY_CONSTANT (index); |
cf3cefc9 | 2406 | if (!lval) |
09b42213 | 2407 | { |
cf3cefc9 | 2408 | elem_type = TREE_TYPE (TREE_TYPE (ary)); |
2409 | if (TREE_CODE (ary) == VIEW_CONVERT_EXPR | |
2410 | && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0))) | |
2411 | && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0)))) | |
2412 | ary = TREE_OPERAND (ary, 0); | |
2413 | if (TREE_CODE (ary) == CONSTRUCTOR) | |
2414 | len = CONSTRUCTOR_NELTS (ary); | |
2415 | else if (TREE_CODE (ary) == STRING_CST) | |
2416 | { | |
2417 | elem_nchars = (TYPE_PRECISION (elem_type) | |
2418 | / TYPE_PRECISION (char_type_node)); | |
2419 | len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars; | |
2420 | } | |
2421 | else if (TREE_CODE (ary) == VECTOR_CST) | |
f08ee65f | 2422 | /* We don't create variable-length VECTOR_CSTs. */ |
2423 | len = VECTOR_CST_NELTS (ary).to_constant (); | |
cf3cefc9 | 2424 | else |
2425 | { | |
2426 | /* We can't do anything with other tree codes, so use | |
2427 | VERIFY_CONSTANT to complain and fail. */ | |
2428 | VERIFY_CONSTANT (ary); | |
2429 | gcc_unreachable (); | |
2430 | } | |
aed8dc7f | 2431 | |
cf3cefc9 | 2432 | if (!tree_fits_shwi_p (index) |
2433 | || (i = tree_to_shwi (index)) < 0) | |
2434 | { | |
2435 | diag_array_subscript (ctx, ary, index); | |
2436 | *non_constant_p = true; | |
2437 | return t; | |
2438 | } | |
7963b19d | 2439 | } |
2440 | ||
51f0c7e2 | 2441 | tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p, |
2442 | overflow_p); | |
ce6a6978 | 2443 | VERIFY_CONSTANT (nelts); |
0acc6340 | 2444 | if ((lval |
2445 | ? !tree_int_cst_le (index, nelts) | |
2446 | : !tree_int_cst_lt (index, nelts)) | |
2447 | || tree_int_cst_sgn (index) < 0) | |
ce6a6978 | 2448 | { |
40df9099 | 2449 | diag_array_subscript (ctx, ary, index); |
ce6a6978 | 2450 | *non_constant_p = true; |
2451 | return t; | |
2452 | } | |
2453 | ||
cf3cefc9 | 2454 | if (lval && ary == oldary && index == oldidx) |
2455 | return t; | |
2456 | else if (lval) | |
2457 | return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL); | |
2458 | ||
7963b19d | 2459 | bool found; |
2460 | if (TREE_CODE (ary) == CONSTRUCTOR) | |
2461 | { | |
2462 | HOST_WIDE_INT ix = find_array_ctor_elt (ary, index); | |
2463 | found = (ix >= 0); | |
2464 | if (found) | |
2465 | i = ix; | |
aed8dc7f | 2466 | } |
7963b19d | 2467 | else |
2468 | found = (i < len); | |
aed8dc7f | 2469 | |
14869f28 | 2470 | if (found) |
2471 | { | |
2472 | tree r; | |
2473 | if (TREE_CODE (ary) == CONSTRUCTOR) | |
2474 | r = (*CONSTRUCTOR_ELTS (ary))[i].value; | |
2475 | else if (TREE_CODE (ary) == VECTOR_CST) | |
2476 | r = VECTOR_CST_ELT (ary, i); | |
14869f28 | 2477 | else |
92fe8840 | 2478 | r = extract_string_elt (ary, elem_nchars, i); |
2479 | ||
14869f28 | 2480 | if (r) |
2481 | /* Don't VERIFY_CONSTANT here. */ | |
2482 | return r; | |
09b42213 | 2483 | |
14869f28 | 2484 | /* Otherwise the element doesn't have a value yet. */ |
09b42213 | 2485 | } |
aed8dc7f | 2486 | |
14869f28 | 2487 | /* Not found. */ |
2488 | ||
2489 | if (TREE_CODE (ary) == CONSTRUCTOR | |
2490 | && CONSTRUCTOR_NO_IMPLICIT_ZERO (ary)) | |
09b42213 | 2491 | { |
14869f28 | 2492 | /* 'ary' is part of the aggregate initializer we're currently |
2493 | building; if there's no initializer for this element yet, | |
2494 | that's an error. */ | |
2495 | if (!ctx->quiet) | |
2496 | error ("accessing uninitialized array element"); | |
2497 | *non_constant_p = true; | |
2498 | return t; | |
09b42213 | 2499 | } |
14869f28 | 2500 | |
2501 | /* If it's within the array bounds but doesn't have an explicit | |
2502 | initializer, it's value-initialized. */ | |
2503 | tree val = build_value_init (elem_type, tf_warning_or_error); | |
2504 | return cxx_eval_constant_expression (ctx, val, lval, non_constant_p, | |
2505 | overflow_p); | |
09b42213 | 2506 | } |
2507 | ||
2508 | /* Subroutine of cxx_eval_constant_expression. | |
2509 | Attempt to reduce a field access of a value of class type. */ | |
2510 | ||
2511 | static tree | |
cf72f34d | 2512 | cxx_eval_component_reference (const constexpr_ctx *ctx, tree t, |
1227ba74 | 2513 | bool lval, |
09b42213 | 2514 | bool *non_constant_p, bool *overflow_p) |
2515 | { | |
2516 | unsigned HOST_WIDE_INT i; | |
2517 | tree field; | |
2518 | tree value; | |
2519 | tree part = TREE_OPERAND (t, 1); | |
2520 | tree orig_whole = TREE_OPERAND (t, 0); | |
cf72f34d | 2521 | tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
1227ba74 | 2522 | lval, |
b2a43197 | 2523 | non_constant_p, overflow_p); |
7ab20d7c | 2524 | if (INDIRECT_REF_P (whole) |
cb768824 | 2525 | && integer_zerop (TREE_OPERAND (whole, 0)) |
2526 | && !ctx->quiet) | |
2527 | error ("dereferencing a null pointer in %qE", orig_whole); | |
2528 | ||
f16ed232 | 2529 | if (TREE_CODE (whole) == PTRMEM_CST) |
2530 | whole = cplus_expand_constant (whole); | |
09b42213 | 2531 | if (whole == orig_whole) |
2532 | return t; | |
1227ba74 | 2533 | if (lval) |
09b42213 | 2534 | return fold_build3 (COMPONENT_REF, TREE_TYPE (t), |
2535 | whole, part, NULL_TREE); | |
2536 | /* Don't VERIFY_CONSTANT here; we only want to check that we got a | |
2537 | CONSTRUCTOR. */ | |
2538 | if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR) | |
2539 | { | |
f83e6885 | 2540 | if (!ctx->quiet) |
09b42213 | 2541 | error ("%qE is not a constant expression", orig_whole); |
2542 | *non_constant_p = true; | |
2543 | } | |
2544 | if (DECL_MUTABLE_P (part)) | |
2545 | { | |
f83e6885 | 2546 | if (!ctx->quiet) |
09b42213 | 2547 | error ("mutable %qD is not usable in a constant expression", part); |
2548 | *non_constant_p = true; | |
2549 | } | |
2550 | if (*non_constant_p) | |
2551 | return t; | |
a4360e57 | 2552 | bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole)); |
09b42213 | 2553 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) |
2554 | { | |
a4360e57 | 2555 | /* Use name match for PMF fields, as a variant will have a |
2556 | different FIELD_DECL with a different type. */ | |
2557 | if (pmf ? DECL_NAME (field) == DECL_NAME (part) | |
2558 | : field == part) | |
9c96033c | 2559 | { |
2560 | if (value) | |
2561 | return value; | |
2562 | else | |
2563 | /* We're in the middle of initializing it. */ | |
2564 | break; | |
2565 | } | |
09b42213 | 2566 | } |
2567 | if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE | |
2568 | && CONSTRUCTOR_NELTS (whole) > 0) | |
2569 | { | |
2570 | /* DR 1188 says we don't have to deal with this. */ | |
f83e6885 | 2571 | if (!ctx->quiet) |
09b42213 | 2572 | error ("accessing %qD member instead of initialized %qD member in " |
2573 | "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index); | |
2574 | *non_constant_p = true; | |
2575 | return t; | |
2576 | } | |
2577 | ||
f894a056 | 2578 | /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty |
2579 | classes never get represented; throw together a value now. */ | |
2580 | if (is_really_empty_class (TREE_TYPE (t))) | |
2581 | return build_constructor (TREE_TYPE (t), NULL); | |
2582 | ||
a4360e57 | 2583 | gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole))); |
2584 | ||
f894a056 | 2585 | if (CONSTRUCTOR_NO_IMPLICIT_ZERO (whole)) |
cf72f34d | 2586 | { |
2587 | /* 'whole' is part of the aggregate initializer we're currently | |
2588 | building; if there's no initializer for this member yet, that's an | |
f894a056 | 2589 | error. */ |
f83e6885 | 2590 | if (!ctx->quiet) |
cf72f34d | 2591 | error ("accessing uninitialized member %qD", part); |
2592 | *non_constant_p = true; | |
2593 | return t; | |
2594 | } | |
2595 | ||
09b42213 | 2596 | /* If there's no explicit init for this field, it's value-initialized. */ |
2597 | value = build_value_init (TREE_TYPE (t), tf_warning_or_error); | |
cf72f34d | 2598 | return cxx_eval_constant_expression (ctx, value, |
1227ba74 | 2599 | lval, |
b2a43197 | 2600 | non_constant_p, overflow_p); |
09b42213 | 2601 | } |
2602 | ||
2603 | /* Subroutine of cxx_eval_constant_expression. | |
2604 | Attempt to reduce a field access of a value of class type that is | |
2605 | expressed as a BIT_FIELD_REF. */ | |
2606 | ||
2607 | static tree | |
cf72f34d | 2608 | cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t, |
1227ba74 | 2609 | bool lval, |
09b42213 | 2610 | bool *non_constant_p, bool *overflow_p) |
2611 | { | |
2612 | tree orig_whole = TREE_OPERAND (t, 0); | |
2613 | tree retval, fldval, utype, mask; | |
2614 | bool fld_seen = false; | |
2615 | HOST_WIDE_INT istart, isize; | |
cf72f34d | 2616 | tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
1227ba74 | 2617 | lval, |
b2a43197 | 2618 | non_constant_p, overflow_p); |
09b42213 | 2619 | tree start, field, value; |
2620 | unsigned HOST_WIDE_INT i; | |
2621 | ||
2622 | if (whole == orig_whole) | |
2623 | return t; | |
2624 | /* Don't VERIFY_CONSTANT here; we only want to check that we got a | |
2625 | CONSTRUCTOR. */ | |
2626 | if (!*non_constant_p | |
2627 | && TREE_CODE (whole) != VECTOR_CST | |
2628 | && TREE_CODE (whole) != CONSTRUCTOR) | |
2629 | { | |
f83e6885 | 2630 | if (!ctx->quiet) |
09b42213 | 2631 | error ("%qE is not a constant expression", orig_whole); |
2632 | *non_constant_p = true; | |
2633 | } | |
2634 | if (*non_constant_p) | |
2635 | return t; | |
2636 | ||
2637 | if (TREE_CODE (whole) == VECTOR_CST) | |
2638 | return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole, | |
2639 | TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)); | |
2640 | ||
2641 | start = TREE_OPERAND (t, 2); | |
2642 | istart = tree_to_shwi (start); | |
2643 | isize = tree_to_shwi (TREE_OPERAND (t, 1)); | |
2644 | utype = TREE_TYPE (t); | |
2645 | if (!TYPE_UNSIGNED (utype)) | |
2646 | utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1); | |
2647 | retval = build_int_cst (utype, 0); | |
2648 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) | |
2649 | { | |
2650 | tree bitpos = bit_position (field); | |
2651 | if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1)) | |
2652 | return value; | |
2653 | if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE | |
2654 | && TREE_CODE (value) == INTEGER_CST | |
2655 | && tree_fits_shwi_p (bitpos) | |
2656 | && tree_fits_shwi_p (DECL_SIZE (field))) | |
2657 | { | |
2658 | HOST_WIDE_INT bit = tree_to_shwi (bitpos); | |
2659 | HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field)); | |
2660 | HOST_WIDE_INT shift; | |
2661 | if (bit >= istart && bit + sz <= istart + isize) | |
2662 | { | |
2663 | fldval = fold_convert (utype, value); | |
2664 | mask = build_int_cst_type (utype, -1); | |
2665 | mask = fold_build2 (LSHIFT_EXPR, utype, mask, | |
2666 | size_int (TYPE_PRECISION (utype) - sz)); | |
2667 | mask = fold_build2 (RSHIFT_EXPR, utype, mask, | |
2668 | size_int (TYPE_PRECISION (utype) - sz)); | |
2669 | fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask); | |
2670 | shift = bit - istart; | |
2671 | if (BYTES_BIG_ENDIAN) | |
2672 | shift = TYPE_PRECISION (utype) - shift - sz; | |
2673 | fldval = fold_build2 (LSHIFT_EXPR, utype, fldval, | |
2674 | size_int (shift)); | |
2675 | retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval); | |
2676 | fld_seen = true; | |
2677 | } | |
2678 | } | |
2679 | } | |
2680 | if (fld_seen) | |
2681 | return fold_convert (TREE_TYPE (t), retval); | |
2682 | gcc_unreachable (); | |
2683 | return error_mark_node; | |
2684 | } | |
2685 | ||
2686 | /* Subroutine of cxx_eval_constant_expression. | |
2687 | Evaluate a short-circuited logical expression T in the context | |
2688 | of a given constexpr CALL. BAILOUT_VALUE is the value for | |
2689 | early return. CONTINUE_VALUE is used here purely for | |
2690 | sanity check purposes. */ | |
2691 | ||
2692 | static tree | |
cf72f34d | 2693 | cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t, |
09b42213 | 2694 | tree bailout_value, tree continue_value, |
1227ba74 | 2695 | bool lval, |
09b42213 | 2696 | bool *non_constant_p, bool *overflow_p) |
2697 | { | |
2698 | tree r; | |
cf72f34d | 2699 | tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
1227ba74 | 2700 | lval, |
b2a43197 | 2701 | non_constant_p, overflow_p); |
09b42213 | 2702 | VERIFY_CONSTANT (lhs); |
2703 | if (tree_int_cst_equal (lhs, bailout_value)) | |
2704 | return lhs; | |
2705 | gcc_assert (tree_int_cst_equal (lhs, continue_value)); | |
cf72f34d | 2706 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
1227ba74 | 2707 | lval, non_constant_p, |
b2a43197 | 2708 | overflow_p); |
09b42213 | 2709 | VERIFY_CONSTANT (r); |
2710 | return r; | |
2711 | } | |
2712 | ||
2713 | /* REF is a COMPONENT_REF designating a particular field. V is a vector of | |
2714 | CONSTRUCTOR elements to initialize (part of) an object containing that | |
2715 | field. Return a pointer to the constructor_elt corresponding to the | |
2716 | initialization of the field. */ | |
2717 | ||
2718 | static constructor_elt * | |
2719 | base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref) | |
2720 | { | |
2721 | tree aggr = TREE_OPERAND (ref, 0); | |
2722 | tree field = TREE_OPERAND (ref, 1); | |
2723 | HOST_WIDE_INT i; | |
2724 | constructor_elt *ce; | |
2725 | ||
2726 | gcc_assert (TREE_CODE (ref) == COMPONENT_REF); | |
2727 | ||
2728 | if (TREE_CODE (aggr) == COMPONENT_REF) | |
2729 | { | |
2730 | constructor_elt *base_ce | |
2731 | = base_field_constructor_elt (v, aggr); | |
2732 | v = CONSTRUCTOR_ELTS (base_ce->value); | |
2733 | } | |
2734 | ||
2735 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
2736 | if (ce->index == field) | |
2737 | return ce; | |
2738 | ||
2739 | gcc_unreachable (); | |
2740 | return NULL; | |
2741 | } | |
2742 | ||
cf72f34d | 2743 | /* Some of the expressions fed to the constexpr mechanism are calls to |
2744 | constructors, which have type void. In that case, return the type being | |
2745 | initialized by the constructor. */ | |
2746 | ||
2747 | static tree | |
2748 | initialized_type (tree t) | |
2749 | { | |
2750 | if (TYPE_P (t)) | |
2751 | return t; | |
2752 | tree type = cv_unqualified (TREE_TYPE (t)); | |
2753 | if (TREE_CODE (t) == CALL_EXPR || TREE_CODE (t) == AGGR_INIT_EXPR) | |
2754 | { | |
2755 | /* A constructor call has void type, so we need to look deeper. */ | |
2756 | tree fn = get_function_named_in_call (t); | |
2757 | if (fn && TREE_CODE (fn) == FUNCTION_DECL | |
2758 | && DECL_CXX_CONSTRUCTOR_P (fn)) | |
2759 | type = DECL_CONTEXT (fn); | |
2760 | } | |
2761 | return type; | |
2762 | } | |
2763 | ||
2764 | /* We're about to initialize element INDEX of an array or class from VALUE. | |
2765 | Set up NEW_CTX appropriately by adjusting .object to refer to the | |
2766 | subobject and creating a new CONSTRUCTOR if the element is itself | |
2767 | a class or array. */ | |
2768 | ||
2769 | static void | |
2770 | init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx, | |
2771 | tree index, tree &value) | |
2772 | { | |
2773 | new_ctx = *ctx; | |
2774 | ||
2775 | if (index && TREE_CODE (index) != INTEGER_CST | |
2776 | && TREE_CODE (index) != FIELD_DECL) | |
2777 | /* This won't have an element in the new CONSTRUCTOR. */ | |
2778 | return; | |
2779 | ||
2780 | tree type = initialized_type (value); | |
2781 | if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type)) | |
2782 | /* A non-aggregate member doesn't get its own CONSTRUCTOR. */ | |
2783 | return; | |
2784 | ||
2785 | /* The sub-aggregate initializer might contain a placeholder; | |
2786 | update object to refer to the subobject and ctor to refer to | |
2787 | the (newly created) sub-initializer. */ | |
2788 | if (ctx->object) | |
2789 | new_ctx.object = build_ctor_subob_ref (index, type, ctx->object); | |
2790 | tree elt = build_constructor (type, NULL); | |
2791 | CONSTRUCTOR_NO_IMPLICIT_ZERO (elt) = true; | |
2792 | new_ctx.ctor = elt; | |
2793 | ||
2794 | if (TREE_CODE (value) == TARGET_EXPR) | |
2795 | /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */ | |
2796 | value = TARGET_EXPR_INITIAL (value); | |
2797 | } | |
2798 | ||
2799 | /* We're about to process an initializer for a class or array TYPE. Make | |
2800 | sure that CTX is set up appropriately. */ | |
2801 | ||
2802 | static void | |
2803 | verify_ctor_sanity (const constexpr_ctx *ctx, tree type) | |
2804 | { | |
2805 | /* We don't bother building a ctor for an empty base subobject. */ | |
2806 | if (is_empty_class (type)) | |
2807 | return; | |
2808 | ||
2809 | /* We're in the middle of an initializer that might involve placeholders; | |
2810 | our caller should have created a CONSTRUCTOR for us to put the | |
2811 | initializer into. We will either return that constructor or T. */ | |
2812 | gcc_assert (ctx->ctor); | |
2813 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
2814 | (type, TREE_TYPE (ctx->ctor))); | |
bbf58224 | 2815 | /* We used to check that ctx->ctor was empty, but that isn't the case when |
2816 | the object is zero-initialized before calling the constructor. */ | |
cf72f34d | 2817 | if (ctx->object) |
d9751a3c | 2818 | { |
2819 | tree otype = TREE_TYPE (ctx->object); | |
2820 | gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype) | |
2821 | /* Handle flexible array members. */ | |
2822 | || (TREE_CODE (otype) == ARRAY_TYPE | |
2823 | && TYPE_DOMAIN (otype) == NULL_TREE | |
2824 | && TREE_CODE (type) == ARRAY_TYPE | |
2825 | && (same_type_ignoring_top_level_qualifiers_p | |
2826 | (TREE_TYPE (type), TREE_TYPE (otype))))); | |
2827 | } | |
cf72f34d | 2828 | gcc_assert (!ctx->object || !DECL_P (ctx->object) |
2829 | || *(ctx->values->get (ctx->object)) == ctx->ctor); | |
2830 | } | |
2831 | ||
09b42213 | 2832 | /* Subroutine of cxx_eval_constant_expression. |
2833 | The expression tree T denotes a C-style array or a C-style | |
2834 | aggregate. Reduce it to a constant expression. */ | |
2835 | ||
2836 | static tree | |
cf72f34d | 2837 | cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t, |
1227ba74 | 2838 | bool lval, |
09b42213 | 2839 | bool *non_constant_p, bool *overflow_p) |
2840 | { | |
2841 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); | |
09b42213 | 2842 | bool changed = false; |
2843 | gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t)); | |
68464393 | 2844 | tree type = TREE_TYPE (t); |
cf72f34d | 2845 | |
68464393 | 2846 | constexpr_ctx new_ctx; |
d2dd85e3 | 2847 | if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type)) |
68464393 | 2848 | { |
d2dd85e3 | 2849 | /* We don't really need the ctx->ctor business for a PMF or |
2850 | vector, but it's simpler to use the same code. */ | |
68464393 | 2851 | new_ctx = *ctx; |
2852 | new_ctx.ctor = build_constructor (type, NULL); | |
2853 | new_ctx.object = NULL_TREE; | |
2854 | ctx = &new_ctx; | |
2855 | }; | |
2856 | verify_ctor_sanity (ctx, type); | |
cf72f34d | 2857 | vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); |
2858 | vec_alloc (*p, vec_safe_length (v)); | |
2859 | ||
58b0f9ce | 2860 | unsigned i; |
2861 | tree index, value; | |
2862 | bool constant_p = true; | |
2863 | bool side_effects_p = false; | |
cf72f34d | 2864 | FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value) |
09b42213 | 2865 | { |
5ae773e4 | 2866 | tree orig_value = value; |
cf72f34d | 2867 | init_subob_ctx (ctx, new_ctx, index, value); |
2868 | if (new_ctx.ctor != ctx->ctor) | |
2869 | /* If we built a new CONSTRUCTOR, attach it now so that other | |
2870 | initializers can refer to it. */ | |
2871 | CONSTRUCTOR_APPEND_ELT (*p, index, new_ctx.ctor); | |
2872 | tree elt = cxx_eval_constant_expression (&new_ctx, value, | |
1227ba74 | 2873 | lval, |
b2a43197 | 2874 | non_constant_p, overflow_p); |
09b42213 | 2875 | /* Don't VERIFY_CONSTANT here. */ |
f83e6885 | 2876 | if (ctx->quiet && *non_constant_p) |
cf72f34d | 2877 | break; |
5ae773e4 | 2878 | if (elt != orig_value) |
09b42213 | 2879 | changed = true; |
58b0f9ce | 2880 | |
2881 | if (!TREE_CONSTANT (elt)) | |
2882 | constant_p = false; | |
2883 | if (TREE_SIDE_EFFECTS (elt)) | |
2884 | side_effects_p = true; | |
cf72f34d | 2885 | if (index && TREE_CODE (index) == COMPONENT_REF) |
09b42213 | 2886 | { |
2887 | /* This is an initialization of a vfield inside a base | |
2888 | subaggregate that we already initialized; push this | |
2889 | initialization into the previous initialization. */ | |
cf72f34d | 2890 | constructor_elt *inner = base_field_constructor_elt (*p, index); |
09b42213 | 2891 | inner->value = elt; |
cf72f34d | 2892 | changed = true; |
09b42213 | 2893 | } |
cf72f34d | 2894 | else if (index |
2895 | && (TREE_CODE (index) == NOP_EXPR | |
2896 | || TREE_CODE (index) == POINTER_PLUS_EXPR)) | |
09b42213 | 2897 | { |
2898 | /* This is an initializer for an empty base; now that we've | |
2899 | checked that it's constant, we can ignore it. */ | |
cf72f34d | 2900 | gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index)))); |
2901 | changed = true; | |
2902 | } | |
09b42213 | 2903 | else |
79e65a6b | 2904 | { |
0dd49b5a | 2905 | if (new_ctx.ctor != ctx->ctor) |
2906 | { | |
2907 | /* We appended this element above; update the value. */ | |
2908 | gcc_assert ((*p)->last().index == index); | |
2909 | (*p)->last().value = elt; | |
2910 | } | |
2911 | else | |
2912 | CONSTRUCTOR_APPEND_ELT (*p, index, elt); | |
2913 | /* Adding or replacing an element might change the ctor's flags. */ | |
79e65a6b | 2914 | TREE_CONSTANT (ctx->ctor) = constant_p; |
2915 | TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p; | |
2916 | } | |
09b42213 | 2917 | } |
2918 | if (*non_constant_p || !changed) | |
cf72f34d | 2919 | return t; |
2920 | t = ctx->ctor; | |
2921 | /* We're done building this CONSTRUCTOR, so now we can interpret an | |
2922 | element without an explicit initializer as value-initialized. */ | |
2923 | CONSTRUCTOR_NO_IMPLICIT_ZERO (t) = false; | |
58b0f9ce | 2924 | TREE_CONSTANT (t) = constant_p; |
2925 | TREE_SIDE_EFFECTS (t) = side_effects_p; | |
68464393 | 2926 | if (VECTOR_TYPE_P (type)) |
09b42213 | 2927 | t = fold (t); |
2928 | return t; | |
2929 | } | |
2930 | ||
2931 | /* Subroutine of cxx_eval_constant_expression. | |
2932 | The expression tree T is a VEC_INIT_EXPR which denotes the desired | |
2933 | initialization of a non-static data member of array type. Reduce it to a | |
2934 | CONSTRUCTOR. | |
2935 | ||
2936 | Note that apart from value-initialization (when VALUE_INIT is true), | |
2937 | this is only intended to support value-initialization and the | |
2938 | initializations done by defaulted constructors for classes with | |
2939 | non-static data members of array type. In this case, VEC_INIT_EXPR_INIT | |
2940 | will either be NULL_TREE for the default constructor, or a COMPONENT_REF | |
2941 | for the copy/move constructor. */ | |
2942 | ||
2943 | static tree | |
cf72f34d | 2944 | cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init, |
1227ba74 | 2945 | bool value_init, bool lval, |
09b42213 | 2946 | bool *non_constant_p, bool *overflow_p) |
2947 | { | |
2948 | tree elttype = TREE_TYPE (atype); | |
cf72f34d | 2949 | verify_ctor_sanity (ctx, atype); |
2950 | vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); | |
09b42213 | 2951 | bool pre_init = false; |
3e520b97 | 2952 | unsigned HOST_WIDE_INT i; |
c4970310 | 2953 | tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; |
09b42213 | 2954 | |
2955 | /* For the default constructor, build up a call to the default | |
2956 | constructor of the element type. We only need to handle class types | |
2957 | here, as for a constructor to be constexpr, all members must be | |
2958 | initialized, which for a defaulted default constructor means they must | |
2959 | be of a class type with a constexpr default constructor. */ | |
2960 | if (TREE_CODE (elttype) == ARRAY_TYPE) | |
2961 | /* We only do this at the lowest level. */; | |
2962 | else if (value_init) | |
2963 | { | |
c4970310 | 2964 | init = build_value_init (elttype, complain); |
09b42213 | 2965 | pre_init = true; |
2966 | } | |
2967 | else if (!init) | |
2968 | { | |
2969 | vec<tree, va_gc> *argvec = make_tree_vector (); | |
2970 | init = build_special_member_call (NULL_TREE, complete_ctor_identifier, | |
2971 | &argvec, elttype, LOOKUP_NORMAL, | |
c4970310 | 2972 | complain); |
09b42213 | 2973 | release_tree_vector (argvec); |
9c96033c | 2974 | init = build_aggr_init_expr (TREE_TYPE (init), init); |
09b42213 | 2975 | pre_init = true; |
2976 | } | |
2977 | ||
51f0c7e2 | 2978 | tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p, |
2979 | overflow_p); | |
2980 | unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts); | |
3e520b97 | 2981 | for (i = 0; i < max; ++i) |
09b42213 | 2982 | { |
2983 | tree idx = build_int_cst (size_type_node, i); | |
2984 | tree eltinit; | |
d9377ad5 | 2985 | bool reuse = false; |
cf72f34d | 2986 | constexpr_ctx new_ctx; |
2987 | init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype); | |
2988 | if (new_ctx.ctor != ctx->ctor) | |
2989 | CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor); | |
09b42213 | 2990 | if (TREE_CODE (elttype) == ARRAY_TYPE) |
2991 | { | |
2992 | /* A multidimensional array; recurse. */ | |
2993 | if (value_init || init == NULL_TREE) | |
d9377ad5 | 2994 | { |
2995 | eltinit = NULL_TREE; | |
2996 | reuse = i == 0; | |
2997 | } | |
09b42213 | 2998 | else |
c4970310 | 2999 | eltinit = cp_build_array_ref (input_location, init, idx, complain); |
cf72f34d | 3000 | eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init, |
1227ba74 | 3001 | lval, |
09b42213 | 3002 | non_constant_p, overflow_p); |
3003 | } | |
3004 | else if (pre_init) | |
3005 | { | |
3006 | /* Initializing an element using value or default initialization | |
3007 | we just pre-built above. */ | |
d9377ad5 | 3008 | eltinit = cxx_eval_constant_expression (&new_ctx, init, lval, |
3009 | non_constant_p, overflow_p); | |
3010 | reuse = i == 0; | |
09b42213 | 3011 | } |
3012 | else | |
3013 | { | |
3014 | /* Copying an element. */ | |
3015 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
3016 | (atype, TREE_TYPE (init))); | |
c4970310 | 3017 | eltinit = cp_build_array_ref (input_location, init, idx, complain); |
18bede74 | 3018 | if (!lvalue_p (init)) |
09b42213 | 3019 | eltinit = move (eltinit); |
c4970310 | 3020 | eltinit = force_rvalue (eltinit, complain); |
eb7c25cc | 3021 | eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval, |
3022 | non_constant_p, overflow_p); | |
09b42213 | 3023 | } |
f83e6885 | 3024 | if (*non_constant_p && !ctx->quiet) |
cf72f34d | 3025 | break; |
3026 | if (new_ctx.ctor != ctx->ctor) | |
3027 | { | |
3028 | /* We appended this element above; update the value. */ | |
3029 | gcc_assert ((*p)->last().index == idx); | |
3030 | (*p)->last().value = eltinit; | |
3031 | } | |
3032 | else | |
3033 | CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit); | |
d9377ad5 | 3034 | /* Reuse the result of cxx_eval_constant_expression call |
eb7c25cc | 3035 | from the first iteration to all others if it is a constant |
3036 | initializer that doesn't require relocations. */ | |
d9377ad5 | 3037 | if (reuse |
3038 | && max > 1 | |
eb7c25cc | 3039 | && (eltinit == NULL_TREE |
3040 | || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit)) | |
3041 | == null_pointer_node))) | |
d9377ad5 | 3042 | { |
3043 | if (new_ctx.ctor != ctx->ctor) | |
3044 | eltinit = new_ctx.ctor; | |
228a2cd8 | 3045 | tree range = build2 (RANGE_EXPR, size_type_node, |
3046 | build_int_cst (size_type_node, 1), | |
3047 | build_int_cst (size_type_node, max - 1)); | |
3048 | CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit)); | |
d9377ad5 | 3049 | break; |
3050 | } | |
228a2cd8 | 3051 | else if (i == 0) |
3052 | vec_safe_reserve (*p, max); | |
09b42213 | 3053 | } |
3054 | ||
3055 | if (!*non_constant_p) | |
3056 | { | |
cf72f34d | 3057 | init = ctx->ctor; |
3058 | CONSTRUCTOR_NO_IMPLICIT_ZERO (init) = false; | |
09b42213 | 3059 | } |
09b42213 | 3060 | return init; |
3061 | } | |
3062 | ||
3063 | static tree | |
cf72f34d | 3064 | cxx_eval_vec_init (const constexpr_ctx *ctx, tree t, |
1227ba74 | 3065 | bool lval, |
09b42213 | 3066 | bool *non_constant_p, bool *overflow_p) |
3067 | { | |
3068 | tree atype = TREE_TYPE (t); | |
3069 | tree init = VEC_INIT_EXPR_INIT (t); | |
cf72f34d | 3070 | tree r = cxx_eval_vec_init_1 (ctx, atype, init, |
09b42213 | 3071 | VEC_INIT_EXPR_VALUE_INIT (t), |
1227ba74 | 3072 | lval, non_constant_p, overflow_p); |
09b42213 | 3073 | if (*non_constant_p) |
3074 | return t; | |
3075 | else | |
3076 | return r; | |
3077 | } | |
3078 | ||
3079 | /* A less strict version of fold_indirect_ref_1, which requires cv-quals to | |
3080 | match. We want to be less strict for simple *& folding; if we have a | |
3081 | non-const temporary that we access through a const pointer, that should | |
3082 | work. We handle this here rather than change fold_indirect_ref_1 | |
3083 | because we're dealing with things like ADDR_EXPR of INTEGER_CST which | |
3084 | don't really make sense outside of constant expression evaluation. Also | |
3085 | we want to allow folding to COMPONENT_REF, which could cause trouble | |
3086 | with TBAA in fold_indirect_ref_1. | |
3087 | ||
3088 | Try to keep this function synced with fold_indirect_ref_1. */ | |
3089 | ||
3090 | static tree | |
3091 | cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base) | |
3092 | { | |
7d8e655d | 3093 | tree sub = op0; |
3094 | tree subtype; | |
3095 | poly_uint64 const_op01; | |
09b42213 | 3096 | |
09b42213 | 3097 | STRIP_NOPS (sub); |
3098 | subtype = TREE_TYPE (sub); | |
d03fa520 | 3099 | if (!INDIRECT_TYPE_P (subtype)) |
09b42213 | 3100 | return NULL_TREE; |
3101 | ||
3102 | if (TREE_CODE (sub) == ADDR_EXPR) | |
3103 | { | |
3104 | tree op = TREE_OPERAND (sub, 0); | |
3105 | tree optype = TREE_TYPE (op); | |
3106 | ||
3107 | /* *&CONST_DECL -> to the value of the const decl. */ | |
3108 | if (TREE_CODE (op) == CONST_DECL) | |
3109 | return DECL_INITIAL (op); | |
3110 | /* *&p => p; make sure to handle *&"str"[cst] here. */ | |
c4118b0c | 3111 | if (same_type_ignoring_top_level_qualifiers_p (optype, type) |
3112 | /* Also handle the case where the desired type is an array of unknown | |
3113 | bounds because the variable has had its bounds deduced since the | |
3114 | ADDR_EXPR was created. */ | |
3115 | || (TREE_CODE (type) == ARRAY_TYPE | |
3116 | && TREE_CODE (optype) == ARRAY_TYPE | |
3117 | && TYPE_DOMAIN (type) == NULL_TREE | |
3118 | && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (optype), | |
3119 | TREE_TYPE (type)))) | |
09b42213 | 3120 | { |
3121 | tree fop = fold_read_from_constant_string (op); | |
3122 | if (fop) | |
3123 | return fop; | |
3124 | else | |
3125 | return op; | |
3126 | } | |
3127 | /* *(foo *)&fooarray => fooarray[0] */ | |
3128 | else if (TREE_CODE (optype) == ARRAY_TYPE | |
3129 | && (same_type_ignoring_top_level_qualifiers_p | |
3130 | (type, TREE_TYPE (optype)))) | |
3131 | { | |
3132 | tree type_domain = TYPE_DOMAIN (optype); | |
3133 | tree min_val = size_zero_node; | |
3134 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
3135 | min_val = TYPE_MIN_VALUE (type_domain); | |
3136 | return build4_loc (loc, ARRAY_REF, type, op, min_val, | |
3137 | NULL_TREE, NULL_TREE); | |
3138 | } | |
3139 | /* *(foo *)&complexfoo => __real__ complexfoo */ | |
3140 | else if (TREE_CODE (optype) == COMPLEX_TYPE | |
3141 | && (same_type_ignoring_top_level_qualifiers_p | |
3142 | (type, TREE_TYPE (optype)))) | |
3143 | return fold_build1_loc (loc, REALPART_EXPR, type, op); | |
3144 | /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */ | |
76249021 | 3145 | else if (VECTOR_TYPE_P (optype) |
09b42213 | 3146 | && (same_type_ignoring_top_level_qualifiers_p |
3147 | (type, TREE_TYPE (optype)))) | |
3148 | { | |
3149 | tree part_width = TYPE_SIZE (type); | |
3150 | tree index = bitsize_int (0); | |
7d8e655d | 3151 | return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, |
3152 | index); | |
09b42213 | 3153 | } |
3154 | /* Also handle conversion to an empty base class, which | |
3155 | is represented with a NOP_EXPR. */ | |
3156 | else if (is_empty_class (type) | |
3157 | && CLASS_TYPE_P (optype) | |
3158 | && DERIVED_FROM_P (type, optype)) | |
3159 | { | |
3160 | *empty_base = true; | |
3161 | return op; | |
3162 | } | |
3163 | /* *(foo *)&struct_with_foo_field => COMPONENT_REF */ | |
3164 | else if (RECORD_OR_UNION_TYPE_P (optype)) | |
3165 | { | |
3166 | tree field = TYPE_FIELDS (optype); | |
3167 | for (; field; field = DECL_CHAIN (field)) | |
3168 | if (TREE_CODE (field) == FIELD_DECL | |
a48667ee | 3169 | && TREE_TYPE (field) != error_mark_node |
09b42213 | 3170 | && integer_zerop (byte_position (field)) |
3171 | && (same_type_ignoring_top_level_qualifiers_p | |
3172 | (TREE_TYPE (field), type))) | |
a48667ee | 3173 | return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE); |
09b42213 | 3174 | } |
3175 | } | |
3176 | else if (TREE_CODE (sub) == POINTER_PLUS_EXPR | |
7d8e655d | 3177 | && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01)) |
09b42213 | 3178 | { |
3179 | tree op00 = TREE_OPERAND (sub, 0); | |
3180 | tree op01 = TREE_OPERAND (sub, 1); | |
3181 | ||
3182 | STRIP_NOPS (op00); | |
3183 | if (TREE_CODE (op00) == ADDR_EXPR) | |
3184 | { | |
3185 | tree op00type; | |
3186 | op00 = TREE_OPERAND (op00, 0); | |
3187 | op00type = TREE_TYPE (op00); | |
3188 | ||
3189 | /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */ | |
76249021 | 3190 | if (VECTOR_TYPE_P (op00type) |
7d8e655d | 3191 | && same_type_ignoring_top_level_qualifiers_p |
3192 | (type, TREE_TYPE (op00type)) | |
3193 | /* POINTER_PLUS_EXPR second operand is sizetype, unsigned, | |
3194 | but we want to treat offsets with MSB set as negative. | |
3195 | For the code below negative offsets are invalid and | |
3196 | TYPE_SIZE of the element is something unsigned, so | |
3197 | check whether op01 fits into poly_int64, which implies | |
3198 | it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and | |
3199 | then just use poly_uint64 because we want to treat the | |
3200 | value as unsigned. */ | |
3201 | && tree_fits_poly_int64_p (op01)) | |
09b42213 | 3202 | { |
09b42213 | 3203 | tree part_width = TYPE_SIZE (type); |
7d8e655d | 3204 | poly_uint64 max_offset |
3205 | = (tree_to_uhwi (part_width) / BITS_PER_UNIT | |
3206 | * TYPE_VECTOR_SUBPARTS (op00type)); | |
3207 | if (known_lt (const_op01, max_offset)) | |
3208 | { | |
3209 | tree index = bitsize_int (const_op01 * BITS_PER_UNIT); | |
3210 | return fold_build3_loc (loc, | |
3211 | BIT_FIELD_REF, type, op00, | |
3212 | part_width, index); | |
3213 | } | |
09b42213 | 3214 | } |
3215 | /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */ | |
3216 | else if (TREE_CODE (op00type) == COMPLEX_TYPE | |
3217 | && (same_type_ignoring_top_level_qualifiers_p | |
3218 | (type, TREE_TYPE (op00type)))) | |
3219 | { | |
7d8e655d | 3220 | if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)), |
3221 | const_op01)) | |
09b42213 | 3222 | return fold_build1_loc (loc, IMAGPART_EXPR, type, op00); |
3223 | } | |
3224 | /* ((foo *)&fooarray)[1] => fooarray[1] */ | |
3225 | else if (TREE_CODE (op00type) == ARRAY_TYPE | |
3226 | && (same_type_ignoring_top_level_qualifiers_p | |
3227 | (type, TREE_TYPE (op00type)))) | |
3228 | { | |
3229 | tree type_domain = TYPE_DOMAIN (op00type); | |
3230 | tree min_val = size_zero_node; | |
3231 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
3232 | min_val = TYPE_MIN_VALUE (type_domain); | |
53fa96c0 | 3233 | offset_int off = wi::to_offset (op01); |
3234 | offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (type)); | |
3235 | offset_int remainder; | |
3236 | off = wi::divmod_trunc (off, el_sz, SIGNED, &remainder); | |
3237 | if (remainder == 0 && TREE_CODE (min_val) == INTEGER_CST) | |
3238 | { | |
3239 | off = off + wi::to_offset (min_val); | |
3240 | op01 = wide_int_to_tree (sizetype, off); | |
3241 | return build4_loc (loc, ARRAY_REF, type, op00, op01, | |
3242 | NULL_TREE, NULL_TREE); | |
3243 | } | |
09b42213 | 3244 | } |
3245 | /* Also handle conversion to an empty base class, which | |
3246 | is represented with a NOP_EXPR. */ | |
3247 | else if (is_empty_class (type) | |
3248 | && CLASS_TYPE_P (op00type) | |
3249 | && DERIVED_FROM_P (type, op00type)) | |
3250 | { | |
3251 | *empty_base = true; | |
3252 | return op00; | |
3253 | } | |
3254 | /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */ | |
3255 | else if (RECORD_OR_UNION_TYPE_P (op00type)) | |
3256 | { | |
3257 | tree field = TYPE_FIELDS (op00type); | |
3258 | for (; field; field = DECL_CHAIN (field)) | |
3259 | if (TREE_CODE (field) == FIELD_DECL | |
a48667ee | 3260 | && TREE_TYPE (field) != error_mark_node |
09b42213 | 3261 | && tree_int_cst_equal (byte_position (field), op01) |
3262 | && (same_type_ignoring_top_level_qualifiers_p | |
3263 | (TREE_TYPE (field), type))) | |
a48667ee | 3264 | return fold_build3 (COMPONENT_REF, type, op00, |
3265 | field, NULL_TREE); | |
09b42213 | 3266 | } |
3267 | } | |
3268 | } | |
3269 | /* *(foo *)fooarrptr => (*fooarrptr)[0] */ | |
3270 | else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE | |
3271 | && (same_type_ignoring_top_level_qualifiers_p | |
3272 | (type, TREE_TYPE (TREE_TYPE (subtype))))) | |
3273 | { | |
3274 | tree type_domain; | |
3275 | tree min_val = size_zero_node; | |
7d8e655d | 3276 | tree newsub |
3277 | = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL); | |
09b42213 | 3278 | if (newsub) |
3279 | sub = newsub; | |
3280 | else | |
3281 | sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub); | |
3282 | type_domain = TYPE_DOMAIN (TREE_TYPE (sub)); | |
3283 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
3284 | min_val = TYPE_MIN_VALUE (type_domain); | |
3285 | return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE, | |
3286 | NULL_TREE); | |
3287 | } | |
3288 | ||
3289 | return NULL_TREE; | |
3290 | } | |
3291 | ||
3292 | static tree | |
cf72f34d | 3293 | cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t, |
1227ba74 | 3294 | bool lval, |
09b42213 | 3295 | bool *non_constant_p, bool *overflow_p) |
3296 | { | |
3297 | tree orig_op0 = TREE_OPERAND (t, 0); | |
09b42213 | 3298 | bool empty_base = false; |
09b42213 | 3299 | |
d2c63826 | 3300 | /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second |
3301 | operand is an integer-zero. Otherwise reject the MEM_REF for now. */ | |
3302 | ||
3303 | if (TREE_CODE (t) == MEM_REF | |
3304 | && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1)))) | |
3305 | { | |
3306 | gcc_assert (ctx->quiet); | |
3307 | *non_constant_p = true; | |
3308 | return t; | |
3309 | } | |
3310 | ||
7e0f3388 | 3311 | /* First try to simplify it directly. */ |
3312 | tree r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), orig_op0, | |
3313 | &empty_base); | |
3314 | if (!r) | |
09b42213 | 3315 | { |
7e0f3388 | 3316 | /* If that didn't work, evaluate the operand first. */ |
3317 | tree op0 = cxx_eval_constant_expression (ctx, orig_op0, | |
3318 | /*lval*/false, non_constant_p, | |
3319 | overflow_p); | |
3320 | /* Don't VERIFY_CONSTANT here. */ | |
3321 | if (*non_constant_p) | |
3322 | return t; | |
3323 | ||
cb768824 | 3324 | if (!lval && integer_zerop (op0)) |
3325 | { | |
3326 | if (!ctx->quiet) | |
3327 | error ("dereferencing a null pointer"); | |
3328 | *non_constant_p = true; | |
3329 | return t; | |
3330 | } | |
3331 | ||
7e0f3388 | 3332 | r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0, |
3333 | &empty_base); | |
3334 | if (r == NULL_TREE) | |
09b42213 | 3335 | { |
3336 | /* We couldn't fold to a constant value. Make sure it's not | |
3337 | something we should have been able to fold. */ | |
7e0f3388 | 3338 | tree sub = op0; |
3339 | STRIP_NOPS (sub); | |
3340 | if (TREE_CODE (sub) == ADDR_EXPR) | |
3341 | { | |
3342 | gcc_assert (!same_type_ignoring_top_level_qualifiers_p | |
3343 | (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t))); | |
3344 | /* DR 1188 says we don't have to deal with this. */ | |
3345 | if (!ctx->quiet) | |
3346 | error ("accessing value of %qE through a %qT glvalue in a " | |
3347 | "constant expression", build_fold_indirect_ref (sub), | |
3348 | TREE_TYPE (t)); | |
3349 | *non_constant_p = true; | |
3350 | return t; | |
3351 | } | |
3352 | ||
3353 | if (lval && op0 != orig_op0) | |
3354 | return build1 (INDIRECT_REF, TREE_TYPE (t), op0); | |
3355 | if (!lval) | |
3356 | VERIFY_CONSTANT (t); | |
09b42213 | 3357 | return t; |
3358 | } | |
3359 | } | |
3360 | ||
7e0f3388 | 3361 | r = cxx_eval_constant_expression (ctx, r, |
3362 | lval, non_constant_p, overflow_p); | |
3363 | if (*non_constant_p) | |
3364 | return t; | |
3365 | ||
ce4ffd9e | 3366 | /* If we're pulling out the value of an empty base, just return an empty |
09b42213 | 3367 | CONSTRUCTOR. */ |
1227ba74 | 3368 | if (empty_base && !lval) |
09b42213 | 3369 | { |
09b42213 | 3370 | r = build_constructor (TREE_TYPE (t), NULL); |
3371 | TREE_CONSTANT (r) = true; | |
3372 | } | |
3373 | ||
09b42213 | 3374 | return r; |
3375 | } | |
3376 | ||
3377 | /* Complain about R, a VAR_DECL, not being usable in a constant expression. | |
3378 | Shared between potential_constant_expression and | |
3379 | cxx_eval_constant_expression. */ | |
3380 | ||
3381 | static void | |
3382 | non_const_var_error (tree r) | |
3383 | { | |
3384 | tree type = TREE_TYPE (r); | |
3385 | error ("the value of %qD is not usable in a constant " | |
3386 | "expression", r); | |
3387 | /* Avoid error cascade. */ | |
3388 | if (DECL_INITIAL (r) == error_mark_node) | |
3389 | return; | |
3390 | if (DECL_DECLARED_CONSTEXPR_P (r)) | |
3391 | inform (DECL_SOURCE_LOCATION (r), | |
3392 | "%qD used in its own initializer", r); | |
3393 | else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) | |
3394 | { | |
3395 | if (!CP_TYPE_CONST_P (type)) | |
3396 | inform (DECL_SOURCE_LOCATION (r), | |
3397 | "%q#D is not const", r); | |
3398 | else if (CP_TYPE_VOLATILE_P (type)) | |
3399 | inform (DECL_SOURCE_LOCATION (r), | |
3400 | "%q#D is volatile", r); | |
3401 | else if (!DECL_INITIAL (r) | |
c8e3e744 | 3402 | || !TREE_CONSTANT (DECL_INITIAL (r)) |
3403 | || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)) | |
09b42213 | 3404 | inform (DECL_SOURCE_LOCATION (r), |
3405 | "%qD was not initialized with a constant " | |
3406 | "expression", r); | |
3407 | else | |
3408 | gcc_unreachable (); | |
3409 | } | |
90ad495b | 3410 | else if (TYPE_REF_P (type)) |
ac6641ca | 3411 | inform (DECL_SOURCE_LOCATION (r), |
3412 | "%qD was not initialized with a constant " | |
3413 | "expression", r); | |
09b42213 | 3414 | else |
3415 | { | |
3416 | if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r)) | |
3417 | inform (DECL_SOURCE_LOCATION (r), | |
3418 | "%qD was not declared %<constexpr%>", r); | |
3419 | else | |
3420 | inform (DECL_SOURCE_LOCATION (r), | |
3421 | "%qD does not have integral or enumeration type", | |
3422 | r); | |
3423 | } | |
3424 | } | |
3425 | ||
3426 | /* Subroutine of cxx_eval_constant_expression. | |
3427 | Like cxx_eval_unary_expression, except for trinary expressions. */ | |
3428 | ||
3429 | static tree | |
cf72f34d | 3430 | cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t, |
1227ba74 | 3431 | bool lval, |
09b42213 | 3432 | bool *non_constant_p, bool *overflow_p) |
3433 | { | |
3434 | int i; | |
3435 | tree args[3]; | |
3436 | tree val; | |
3437 | ||
3438 | for (i = 0; i < 3; i++) | |
3439 | { | |
cf72f34d | 3440 | args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i), |
1227ba74 | 3441 | lval, |
b2a43197 | 3442 | non_constant_p, overflow_p); |
09b42213 | 3443 | VERIFY_CONSTANT (args[i]); |
3444 | } | |
3445 | ||
3446 | val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t), | |
3447 | args[0], args[1], args[2]); | |
3448 | if (val == NULL_TREE) | |
3449 | return t; | |
3450 | VERIFY_CONSTANT (val); | |
3451 | return val; | |
3452 | } | |
3453 | ||
33603066 | 3454 | /* True if T was declared in a function declared to be constexpr, and |
3455 | therefore potentially constant in C++14. */ | |
3456 | ||
09b42213 | 3457 | bool |
3458 | var_in_constexpr_fn (tree t) | |
3459 | { | |
3460 | tree ctx = DECL_CONTEXT (t); | |
33603066 | 3461 | return (ctx && TREE_CODE (ctx) == FUNCTION_DECL |
09b42213 | 3462 | && DECL_DECLARED_CONSTEXPR_P (ctx)); |
3463 | } | |
3464 | ||
33603066 | 3465 | /* True if T was declared in a function that might be constexpr: either a |
3466 | function that was declared constexpr, or a C++17 lambda op(). */ | |
3467 | ||
3468 | bool | |
3469 | var_in_maybe_constexpr_fn (tree t) | |
3470 | { | |
40e2decb | 3471 | if (cxx_dialect >= cxx17 |
33603066 | 3472 | && DECL_FUNCTION_SCOPE_P (t) |
3473 | && LAMBDA_FUNCTION_P (DECL_CONTEXT (t))) | |
3474 | return true; | |
3475 | return var_in_constexpr_fn (t); | |
3476 | } | |
3477 | ||
93312944 | 3478 | /* We're assigning INIT to TARGET. In do_build_copy_constructor and |
3479 | build_over_call we implement trivial copy of a class with tail padding using | |
3480 | assignment of character arrays, which is valid in normal code, but not in | |
3481 | constexpr evaluation. We don't need to worry about clobbering tail padding | |
3482 | in constexpr evaluation, so strip the type punning. */ | |
3483 | ||
3484 | static void | |
3485 | maybe_simplify_trivial_copy (tree &target, tree &init) | |
3486 | { | |
3487 | if (TREE_CODE (target) == MEM_REF | |
3488 | && TREE_CODE (init) == MEM_REF | |
3489 | && TREE_TYPE (target) == TREE_TYPE (init) | |
3490 | && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE | |
3491 | && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node) | |
3492 | { | |
3493 | target = build_fold_indirect_ref (TREE_OPERAND (target, 0)); | |
3494 | init = build_fold_indirect_ref (TREE_OPERAND (init, 0)); | |
3495 | } | |
3496 | } | |
3497 | ||
9c96033c | 3498 | /* Evaluate an INIT_EXPR or MODIFY_EXPR. */ |
3499 | ||
3500 | static tree | |
3501 | cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, | |
1227ba74 | 3502 | bool lval, |
9c96033c | 3503 | bool *non_constant_p, bool *overflow_p) |
3504 | { | |
3505 | constexpr_ctx new_ctx = *ctx; | |
3506 | ||
8a36d0ec | 3507 | tree init = TREE_OPERAND (t, 1); |
3508 | if (TREE_CLOBBER_P (init)) | |
3509 | /* Just ignore clobbers. */ | |
3510 | return void_node; | |
3511 | ||
9c96033c | 3512 | /* First we figure out where we're storing to. */ |
3513 | tree target = TREE_OPERAND (t, 0); | |
93312944 | 3514 | |
3515 | maybe_simplify_trivial_copy (target, init); | |
3516 | ||
9db9fc38 | 3517 | tree type = TREE_TYPE (target); |
9c96033c | 3518 | target = cxx_eval_constant_expression (ctx, target, |
f83e6885 | 3519 | true, |
b2a43197 | 3520 | non_constant_p, overflow_p); |
9c96033c | 3521 | if (*non_constant_p) |
3522 | return t; | |
3523 | ||
cf3cefc9 | 3524 | /* cxx_eval_array_reference for lval = true allows references one past |
3525 | end of array, because it does not know if it is just taking address | |
3526 | (which is valid), or actual dereference. Here we know it is | |
3527 | a dereference, so diagnose it here. */ | |
3528 | for (tree probe = target; probe; ) | |
3529 | { | |
3530 | switch (TREE_CODE (probe)) | |
3531 | { | |
3532 | case ARRAY_REF: | |
3533 | tree nelts, ary; | |
3534 | ary = TREE_OPERAND (probe, 0); | |
51f0c7e2 | 3535 | nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), |
3536 | non_constant_p, overflow_p); | |
cf3cefc9 | 3537 | VERIFY_CONSTANT (nelts); |
3538 | gcc_assert (TREE_CODE (nelts) == INTEGER_CST | |
3539 | && TREE_CODE (TREE_OPERAND (probe, 1)) == INTEGER_CST); | |
cb7bca5f | 3540 | if (wi::to_widest (TREE_OPERAND (probe, 1)) == wi::to_widest (nelts)) |
cf3cefc9 | 3541 | { |
3542 | diag_array_subscript (ctx, ary, TREE_OPERAND (probe, 1)); | |
3543 | *non_constant_p = true; | |
3544 | return t; | |
3545 | } | |
3546 | /* FALLTHRU */ | |
3547 | ||
3548 | case BIT_FIELD_REF: | |
3549 | case COMPONENT_REF: | |
3550 | probe = TREE_OPERAND (probe, 0); | |
3551 | continue; | |
3552 | ||
3553 | default: | |
3554 | probe = NULL_TREE; | |
3555 | continue; | |
3556 | } | |
3557 | } | |
3558 | ||
9db9fc38 | 3559 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (target), type)) |
3560 | { | |
3561 | /* For initialization of an empty base, the original target will be | |
3562 | *(base*)this, which the above evaluation resolves to the object | |
3563 | argument, which has the derived type rather than the base type. In | |
3564 | this situation, just evaluate the initializer and return, since | |
3565 | there's no actual data to store. */ | |
3566 | gcc_assert (is_empty_class (type)); | |
3567 | return cxx_eval_constant_expression (ctx, init, false, | |
3568 | non_constant_p, overflow_p); | |
3569 | } | |
3570 | ||
9c96033c | 3571 | /* And then find the underlying variable. */ |
3572 | vec<tree,va_gc> *refs = make_tree_vector(); | |
3573 | tree object = NULL_TREE; | |
3574 | for (tree probe = target; object == NULL_TREE; ) | |
3575 | { | |
3576 | switch (TREE_CODE (probe)) | |
3577 | { | |
3578 | case BIT_FIELD_REF: | |
3579 | case COMPONENT_REF: | |
3580 | case ARRAY_REF: | |
3581 | vec_safe_push (refs, TREE_OPERAND (probe, 1)); | |
3582 | vec_safe_push (refs, TREE_TYPE (probe)); | |
3583 | probe = TREE_OPERAND (probe, 0); | |
3584 | break; | |
3585 | ||
3586 | default: | |
3587 | object = probe; | |
9c96033c | 3588 | } |
3589 | } | |
3590 | ||
3591 | /* And then find/build up our initializer for the path to the subobject | |
3592 | we're initializing. */ | |
6fe83e54 | 3593 | tree *valp; |
43b7271d | 3594 | if (object == ctx->object && VAR_P (object) |
3595 | && DECL_NAME (object) && ctx->call == NULL) | |
3596 | /* The variable we're building up an aggregate initializer for is outside | |
3597 | the constant-expression, so don't evaluate the store. We check | |
3598 | DECL_NAME to handle TARGET_EXPR temporaries, which are fair game. */ | |
3599 | valp = NULL; | |
3600 | else if (DECL_P (object)) | |
6fe83e54 | 3601 | valp = ctx->values->get (object); |
3602 | else | |
3603 | valp = NULL; | |
9c96033c | 3604 | if (!valp) |
3605 | { | |
3606 | /* A constant-expression cannot modify objects from outside the | |
3607 | constant-expression. */ | |
f83e6885 | 3608 | if (!ctx->quiet) |
ea034e2c | 3609 | error ("modification of %qE is not a constant expression", object); |
9c96033c | 3610 | *non_constant_p = true; |
3611 | return t; | |
3612 | } | |
9db9fc38 | 3613 | type = TREE_TYPE (object); |
a02b42fe | 3614 | bool no_zero_init = true; |
58b0f9ce | 3615 | |
3616 | vec<tree,va_gc> *ctors = make_tree_vector (); | |
9c96033c | 3617 | while (!refs->is_empty()) |
3618 | { | |
3619 | if (*valp == NULL_TREE) | |
3620 | { | |
3621 | *valp = build_constructor (type, NULL); | |
a02b42fe | 3622 | CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp) = no_zero_init; |
9c96033c | 3623 | } |
92fe8840 | 3624 | else if (TREE_CODE (*valp) == STRING_CST) |
3625 | { | |
3626 | /* An array was initialized with a string constant, and now | |
3627 | we're writing into one of its elements. Explode the | |
3628 | single initialization into a set of element | |
3629 | initializations. */ | |
3630 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); | |
3631 | ||
3632 | tree string = *valp; | |
3633 | tree elt_type = TREE_TYPE (type); | |
3634 | unsigned chars_per_elt = (TYPE_PRECISION (elt_type) | |
3635 | / TYPE_PRECISION (char_type_node)); | |
3636 | unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt; | |
3637 | tree ary_ctor = build_constructor (type, NULL); | |
3638 | ||
3639 | vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts); | |
3640 | for (unsigned ix = 0; ix != num_elts; ix++) | |
3641 | { | |
3642 | constructor_elt elt = | |
3643 | { | |
3644 | build_int_cst (size_type_node, ix), | |
3645 | extract_string_elt (string, chars_per_elt, ix) | |
3646 | }; | |
3647 | CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt); | |
3648 | } | |
3649 | ||
3650 | *valp = ary_ctor; | |
3651 | } | |
3652 | ||
a02b42fe | 3653 | /* If the value of object is already zero-initialized, any new ctors for |
3654 | subobjects will also be zero-initialized. */ | |
3655 | no_zero_init = CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp); | |
9c96033c | 3656 | |
58b0f9ce | 3657 | vec_safe_push (ctors, *valp); |
3658 | ||
7963b19d | 3659 | enum tree_code code = TREE_CODE (type); |
9c96033c | 3660 | type = refs->pop(); |
7963b19d | 3661 | tree index = refs->pop(); |
9c96033c | 3662 | |
9c96033c | 3663 | constructor_elt *cep = NULL; |
7963b19d | 3664 | if (code == ARRAY_TYPE) |
3665 | { | |
3666 | HOST_WIDE_INT i | |
3667 | = find_array_ctor_elt (*valp, index, /*insert*/true); | |
3668 | gcc_assert (i >= 0); | |
3669 | cep = CONSTRUCTOR_ELT (*valp, i); | |
3670 | gcc_assert (TREE_CODE (cep->index) != RANGE_EXPR); | |
3671 | } | |
3672 | else | |
3673 | { | |
3674 | gcc_assert (TREE_CODE (index) == FIELD_DECL); | |
13ee2de7 | 3675 | |
3676 | /* We must keep the CONSTRUCTOR's ELTS in FIELD order. | |
3677 | Usually we meet initializers in that order, but it is | |
3678 | possible for base types to be placed not in program | |
3679 | order. */ | |
3680 | tree fields = TYPE_FIELDS (DECL_CONTEXT (index)); | |
3681 | unsigned HOST_WIDE_INT idx; | |
3682 | ||
ef782365 | 3683 | if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp) |
3684 | && CONSTRUCTOR_ELT (*valp, 0)->index != index) | |
3685 | /* Changing active member. */ | |
3686 | vec_safe_truncate (CONSTRUCTOR_ELTS (*valp), 0); | |
3687 | ||
13ee2de7 | 3688 | for (idx = 0; |
7963b19d | 3689 | vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep); |
13ee2de7 | 3690 | idx++, fields = DECL_CHAIN (fields)) |
7963b19d | 3691 | { |
13ee2de7 | 3692 | if (index == cep->index) |
3693 | goto found; | |
3694 | ||
3695 | /* The field we're initializing must be on the field | |
3696 | list. Look to see if it is present before the | |
3697 | field the current ELT initializes. */ | |
3698 | for (; fields != cep->index; fields = DECL_CHAIN (fields)) | |
3699 | if (index == fields) | |
3700 | goto insert; | |
7963b19d | 3701 | } |
13ee2de7 | 3702 | |
3703 | /* We fell off the end of the CONSTRUCTOR, so insert a new | |
3704 | entry at the end. */ | |
3705 | insert: | |
3706 | { | |
3707 | constructor_elt ce = { index, NULL_TREE }; | |
3708 | ||
3709 | vec_safe_insert (CONSTRUCTOR_ELTS (*valp), idx, ce); | |
3710 | cep = CONSTRUCTOR_ELT (*valp, idx); | |
3711 | } | |
3712 | found:; | |
7963b19d | 3713 | } |
9c96033c | 3714 | valp = &cep->value; |
3715 | } | |
3716 | release_tree_vector (refs); | |
3717 | ||
469dc5a6 | 3718 | if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)) |
9c96033c | 3719 | { |
3720 | /* Create a new CONSTRUCTOR in case evaluation of the initializer | |
3721 | wants to modify it. */ | |
469dc5a6 | 3722 | if (*valp == NULL_TREE) |
6cded085 | 3723 | { |
6993e708 | 3724 | *valp = build_constructor (type, NULL); |
3725 | CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp) = no_zero_init; | |
6cded085 | 3726 | } |
6993e708 | 3727 | else if (TREE_CODE (*valp) == PTRMEM_CST) |
3728 | *valp = cplus_expand_constant (*valp); | |
3729 | new_ctx.ctor = *valp; | |
9c96033c | 3730 | new_ctx.object = target; |
3731 | } | |
3732 | ||
8a36d0ec | 3733 | init = cxx_eval_constant_expression (&new_ctx, init, false, |
3734 | non_constant_p, overflow_p); | |
7b9f713d | 3735 | /* Don't share a CONSTRUCTOR that might be changed later. */ |
e283bb4f | 3736 | init = unshare_constructor (init); |
9c96033c | 3737 | if (target == object) |
5579a199 | 3738 | /* The hash table might have moved since the get earlier. */ |
3739 | valp = ctx->values->get (object); | |
3740 | ||
3741 | if (TREE_CODE (init) == CONSTRUCTOR) | |
469dc5a6 | 3742 | { |
5579a199 | 3743 | /* An outer ctx->ctor might be pointing to *valp, so replace |
3744 | its contents. */ | |
3745 | CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init); | |
3746 | TREE_CONSTANT (*valp) = TREE_CONSTANT (init); | |
3747 | TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init); | |
b9786e35 | 3748 | CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp) |
3749 | = CONSTRUCTOR_NO_IMPLICIT_ZERO (init); | |
469dc5a6 | 3750 | } |
9c96033c | 3751 | else |
5579a199 | 3752 | *valp = init; |
58b0f9ce | 3753 | |
5579a199 | 3754 | /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing |
3755 | CONSTRUCTORs, if any. */ | |
3756 | tree elt; | |
3757 | unsigned i; | |
3758 | bool c = TREE_CONSTANT (init); | |
3759 | bool s = TREE_SIDE_EFFECTS (init); | |
3760 | if (!c || s) | |
3761 | FOR_EACH_VEC_SAFE_ELT (ctors, i, elt) | |
3762 | { | |
3763 | if (!c) | |
3764 | TREE_CONSTANT (elt) = false; | |
3765 | if (s) | |
3766 | TREE_SIDE_EFFECTS (elt) = true; | |
3767 | } | |
58b0f9ce | 3768 | release_tree_vector (ctors); |
9c96033c | 3769 | |
3770 | if (*non_constant_p) | |
3771 | return t; | |
1227ba74 | 3772 | else if (lval) |
9c96033c | 3773 | return target; |
3774 | else | |
dfac7dd2 | 3775 | return init; |
9c96033c | 3776 | } |
3777 | ||
3778 | /* Evaluate a ++ or -- expression. */ | |
3779 | ||
3780 | static tree | |
3781 | cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t, | |
1227ba74 | 3782 | bool lval, |
9c96033c | 3783 | bool *non_constant_p, bool *overflow_p) |
3784 | { | |
3785 | enum tree_code code = TREE_CODE (t); | |
3786 | tree type = TREE_TYPE (t); | |
3787 | tree op = TREE_OPERAND (t, 0); | |
3788 | tree offset = TREE_OPERAND (t, 1); | |
3789 | gcc_assert (TREE_CONSTANT (offset)); | |
3790 | ||
3791 | /* The operand as an lvalue. */ | |
f83e6885 | 3792 | op = cxx_eval_constant_expression (ctx, op, true, |
b2a43197 | 3793 | non_constant_p, overflow_p); |
9c96033c | 3794 | |
3795 | /* The operand as an rvalue. */ | |
c017458d | 3796 | tree val |
3797 | = cxx_eval_constant_expression (ctx, op, false, | |
3798 | non_constant_p, overflow_p); | |
35f2c28f | 3799 | /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to |
3800 | a local array in a constexpr function. */ | |
d03fa520 | 3801 | bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val)); |
35f2c28f | 3802 | if (!ptr) |
3803 | VERIFY_CONSTANT (val); | |
9c96033c | 3804 | |
3805 | /* The modified value. */ | |
3806 | bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR); | |
c8a8f7ad | 3807 | tree mod; |
d03fa520 | 3808 | if (INDIRECT_TYPE_P (type)) |
c8a8f7ad | 3809 | { |
3810 | /* The middle end requires pointers to use POINTER_PLUS_EXPR. */ | |
3811 | offset = convert_to_ptrofftype (offset); | |
3812 | if (!inc) | |
3813 | offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset); | |
3814 | mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset); | |
3815 | } | |
3816 | else | |
3817 | mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset); | |
35f2c28f | 3818 | if (!ptr) |
3819 | VERIFY_CONSTANT (mod); | |
9c96033c | 3820 | |
3821 | /* Storing the modified value. */ | |
3822 | tree store = build2 (MODIFY_EXPR, type, op, mod); | |
f83e6885 | 3823 | cxx_eval_constant_expression (ctx, store, |
b2a43197 | 3824 | true, non_constant_p, overflow_p); |
9c96033c | 3825 | |
3826 | /* And the value of the expression. */ | |
3827 | if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR) | |
3828 | { | |
3829 | /* Prefix ops are lvalues. */ | |
1227ba74 | 3830 | if (lval) |
9c96033c | 3831 | return op; |
3832 | else | |
3833 | /* But we optimize when the caller wants an rvalue. */ | |
3834 | return mod; | |
3835 | } | |
3836 | else | |
3837 | /* Postfix ops are rvalues. */ | |
3838 | return val; | |
3839 | } | |
3840 | ||
00f21715 | 3841 | /* Predicates for the meaning of *jump_target. */ |
3842 | ||
3843 | static bool | |
3844 | returns (tree *jump_target) | |
3845 | { | |
3846 | return *jump_target | |
d29e4e8c | 3847 | && (TREE_CODE (*jump_target) == RETURN_EXPR |
3848 | || (TREE_CODE (*jump_target) == LABEL_DECL | |
3849 | && LABEL_DECL_CDTOR (*jump_target))); | |
00f21715 | 3850 | } |
3851 | ||
3852 | static bool | |
3853 | breaks (tree *jump_target) | |
3854 | { | |
3855 | return *jump_target | |
cf03ba19 | 3856 | && ((TREE_CODE (*jump_target) == LABEL_DECL |
3857 | && LABEL_DECL_BREAK (*jump_target)) | |
3858 | || TREE_CODE (*jump_target) == EXIT_EXPR); | |
00f21715 | 3859 | } |
3860 | ||
3861 | static bool | |
3862 | continues (tree *jump_target) | |
3863 | { | |
3864 | return *jump_target | |
3865 | && TREE_CODE (*jump_target) == LABEL_DECL | |
3866 | && LABEL_DECL_CONTINUE (*jump_target); | |
3867 | } | |
3868 | ||
3869 | static bool | |
3870 | switches (tree *jump_target) | |
3871 | { | |
3872 | return *jump_target | |
3873 | && TREE_CODE (*jump_target) == INTEGER_CST; | |
3874 | } | |
3875 | ||
3876 | /* Subroutine of cxx_eval_statement_list. Determine whether the statement | |
2a2770c6 | 3877 | STMT matches *jump_target. If we're looking for a case label and we see |
3878 | the default label, note it in ctx->css_state. */ | |
00f21715 | 3879 | |
3880 | static bool | |
2a2770c6 | 3881 | label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt) |
00f21715 | 3882 | { |
00f21715 | 3883 | switch (TREE_CODE (*jump_target)) |
3884 | { | |
3885 | case LABEL_DECL: | |
3886 | if (TREE_CODE (stmt) == LABEL_EXPR | |
3887 | && LABEL_EXPR_LABEL (stmt) == *jump_target) | |
3888 | return true; | |
3889 | break; | |
3890 | ||
3891 | case INTEGER_CST: | |
3892 | if (TREE_CODE (stmt) == CASE_LABEL_EXPR) | |
3893 | { | |
2a2770c6 | 3894 | gcc_assert (ctx->css_state != NULL); |
00f21715 | 3895 | if (!CASE_LOW (stmt)) |
2a2770c6 | 3896 | { |
3897 | /* default: should appear just once in a SWITCH_EXPR | |
3898 | body (excluding nested SWITCH_EXPR). */ | |
3899 | gcc_assert (*ctx->css_state != css_default_seen); | |
3900 | /* When evaluating SWITCH_EXPR body for the second time, | |
3901 | return true for the default: label. */ | |
3902 | if (*ctx->css_state == css_default_processing) | |
3903 | return true; | |
3904 | *ctx->css_state = css_default_seen; | |
3905 | } | |
83c375ba | 3906 | else if (CASE_HIGH (stmt)) |
3907 | { | |
3908 | if (tree_int_cst_le (CASE_LOW (stmt), *jump_target) | |
3909 | && tree_int_cst_le (*jump_target, CASE_HIGH (stmt))) | |
3910 | return true; | |
3911 | } | |
00f21715 | 3912 | else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt))) |
3913 | return true; | |
3914 | } | |
3915 | break; | |
3916 | ||
3917 | default: | |
3918 | gcc_unreachable (); | |
3919 | } | |
3920 | return false; | |
3921 | } | |
3922 | ||
3923 | /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump | |
3924 | semantics, for switch, break, continue, and return. */ | |
3925 | ||
3926 | static tree | |
3927 | cxx_eval_statement_list (const constexpr_ctx *ctx, tree t, | |
00f21715 | 3928 | bool *non_constant_p, bool *overflow_p, |
3929 | tree *jump_target) | |
3930 | { | |
3931 | tree_stmt_iterator i; | |
da7981e0 | 3932 | tree local_target; |
efea7c64 | 3933 | /* In a statement-expression we want to return the last value. |
3934 | For empty statement expression return void_node. */ | |
3935 | tree r = void_node; | |
da7981e0 | 3936 | if (!jump_target) |
3937 | { | |
3938 | local_target = NULL_TREE; | |
3939 | jump_target = &local_target; | |
3940 | } | |
00f21715 | 3941 | for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) |
3942 | { | |
00f21715 | 3943 | tree stmt = tsi_stmt (i); |
442c4003 | 3944 | if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT) |
3945 | continue; | |
da7981e0 | 3946 | r = cxx_eval_constant_expression (ctx, stmt, false, |
3947 | non_constant_p, overflow_p, | |
3948 | jump_target); | |
00f21715 | 3949 | if (*non_constant_p) |
3950 | break; | |
3951 | if (returns (jump_target) || breaks (jump_target)) | |
3952 | break; | |
3953 | } | |
da7981e0 | 3954 | return r; |
00f21715 | 3955 | } |
3956 | ||
3957 | /* Evaluate a LOOP_EXPR for side-effects. Handles break and return | |
3958 | semantics; continue semantics are covered by cxx_eval_statement_list. */ | |
3959 | ||
3960 | static tree | |
3961 | cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t, | |
00f21715 | 3962 | bool *non_constant_p, bool *overflow_p, |
3963 | tree *jump_target) | |
3964 | { | |
2631cb67 | 3965 | constexpr_ctx new_ctx = *ctx; |
3966 | ||
00f21715 | 3967 | tree body = TREE_OPERAND (t, 0); |
21483ab7 | 3968 | int count = 0; |
5370eb8d | 3969 | do |
00f21715 | 3970 | { |
2631cb67 | 3971 | hash_set<tree> save_exprs; |
3972 | new_ctx.save_exprs = &save_exprs; | |
3973 | ||
cf03ba19 | 3974 | cxx_eval_constant_expression (&new_ctx, body, /*lval*/false, |
3975 | non_constant_p, overflow_p, jump_target); | |
2631cb67 | 3976 | |
3977 | /* Forget saved values of SAVE_EXPRs. */ | |
3978 | for (hash_set<tree>::iterator iter = save_exprs.begin(); | |
3979 | iter != save_exprs.end(); ++iter) | |
3980 | new_ctx.values->remove (*iter); | |
21483ab7 | 3981 | if (++count >= constexpr_loop_limit) |
3982 | { | |
3983 | if (!ctx->quiet) | |
d3a3cfb8 | 3984 | error_at (cp_expr_loc_or_loc (t, input_location), |
5967b28b | 3985 | "%<constexpr%> loop iteration count exceeds limit of %d " |
21483ab7 | 3986 | "(use -fconstexpr-loop-limit= to increase the limit)", |
3987 | constexpr_loop_limit); | |
3988 | *non_constant_p = true; | |
3989 | break; | |
3990 | } | |
00f21715 | 3991 | } |
2a2770c6 | 3992 | while (!returns (jump_target) |
3993 | && !breaks (jump_target) | |
3994 | && !switches (jump_target) | |
3995 | && !*non_constant_p); | |
5370eb8d | 3996 | |
00f21715 | 3997 | if (breaks (jump_target)) |
3998 | *jump_target = NULL_TREE; | |
2631cb67 | 3999 | |
00f21715 | 4000 | return NULL_TREE; |
4001 | } | |
4002 | ||
4003 | /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump | |
4004 | semantics. */ | |
4005 | ||
4006 | static tree | |
4007 | cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t, | |
00f21715 | 4008 | bool *non_constant_p, bool *overflow_p, |
4009 | tree *jump_target) | |
4010 | { | |
4011 | tree cond = TREE_OPERAND (t, 0); | |
f83e6885 | 4012 | cond = cxx_eval_constant_expression (ctx, cond, false, |
b2a43197 | 4013 | non_constant_p, overflow_p); |
00f21715 | 4014 | VERIFY_CONSTANT (cond); |
4015 | *jump_target = cond; | |
4016 | ||
4017 | tree body = TREE_OPERAND (t, 1); | |
2a2770c6 | 4018 | constexpr_ctx new_ctx = *ctx; |
4019 | constexpr_switch_state css = css_default_not_seen; | |
4020 | new_ctx.css_state = &css; | |
4021 | cxx_eval_constant_expression (&new_ctx, body, false, | |
4022 | non_constant_p, overflow_p, jump_target); | |
4023 | if (switches (jump_target) && css == css_default_seen) | |
4024 | { | |
4025 | /* If the SWITCH_EXPR body has default: label, process it once again, | |
4026 | this time instructing label_matches to return true for default: | |
4027 | label on switches (jump_target). */ | |
4028 | css = css_default_processing; | |
4029 | cxx_eval_constant_expression (&new_ctx, body, false, | |
4030 | non_constant_p, overflow_p, jump_target); | |
4031 | } | |
00f21715 | 4032 | if (breaks (jump_target) || switches (jump_target)) |
4033 | *jump_target = NULL_TREE; | |
4034 | return NULL_TREE; | |
4035 | } | |
4036 | ||
85de9604 | 4037 | /* Find the object of TYPE under initialization in CTX. */ |
4038 | ||
4039 | static tree | |
4040 | lookup_placeholder (const constexpr_ctx *ctx, bool lval, tree type) | |
4041 | { | |
def8e41f | 4042 | if (!ctx) |
85de9604 | 4043 | return NULL_TREE; |
4044 | ||
4045 | /* We could use ctx->object unconditionally, but using ctx->ctor when we | |
4046 | can is a minor optimization. */ | |
def8e41f | 4047 | if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type)) |
85de9604 | 4048 | return ctx->ctor; |
4049 | ||
def8e41f | 4050 | if (!ctx->object) |
4051 | return NULL_TREE; | |
4052 | ||
85de9604 | 4053 | /* Since an object cannot have a field of its own type, we can search outward |
4054 | from ctx->object to find the unique containing object of TYPE. */ | |
4055 | tree ob = ctx->object; | |
4056 | while (ob) | |
4057 | { | |
4058 | if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type)) | |
4059 | break; | |
4060 | if (handled_component_p (ob)) | |
4061 | ob = TREE_OPERAND (ob, 0); | |
4062 | else | |
4063 | ob = NULL_TREE; | |
4064 | } | |
4065 | ||
4066 | return ob; | |
4067 | } | |
4068 | ||
09b42213 | 4069 | /* Attempt to reduce the expression T to a constant value. |
4070 | On failure, issue diagnostic and return error_mark_node. */ | |
4071 | /* FIXME unify with c_fully_fold */ | |
9c96033c | 4072 | /* FIXME overflow_p is too global */ |
09b42213 | 4073 | |
4074 | static tree | |
cf72f34d | 4075 | cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, |
1227ba74 | 4076 | bool lval, |
00f21715 | 4077 | bool *non_constant_p, bool *overflow_p, |
4078 | tree *jump_target) | |
09b42213 | 4079 | { |
cf72f34d | 4080 | constexpr_ctx new_ctx; |
09b42213 | 4081 | tree r = t; |
4082 | ||
2a2770c6 | 4083 | if (jump_target && *jump_target) |
4084 | { | |
4085 | /* If we are jumping, ignore all statements/expressions except those | |
4086 | that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */ | |
4087 | switch (TREE_CODE (t)) | |
4088 | { | |
4089 | case BIND_EXPR: | |
4090 | case STATEMENT_LIST: | |
4091 | case LOOP_EXPR: | |
4092 | case COND_EXPR: | |
4093 | break; | |
4094 | case LABEL_EXPR: | |
4095 | case CASE_LABEL_EXPR: | |
4096 | if (label_matches (ctx, jump_target, t)) | |
4097 | /* Found it. */ | |
4098 | *jump_target = NULL_TREE; | |
4099 | return NULL_TREE; | |
4100 | default: | |
4101 | return NULL_TREE; | |
4102 | } | |
4103 | } | |
09b42213 | 4104 | if (t == error_mark_node) |
4105 | { | |
4106 | *non_constant_p = true; | |
4107 | return t; | |
4108 | } | |
4109 | if (CONSTANT_CLASS_P (t)) | |
4110 | { | |
c4fa85c9 | 4111 | if (TREE_OVERFLOW (t)) |
4112 | { | |
4113 | if (!ctx->quiet) | |
4114 | permerror (input_location, "overflow in constant expression"); | |
4115 | if (!flag_permissive || ctx->quiet) | |
4116 | *overflow_p = true; | |
4117 | } | |
cb768824 | 4118 | |
4119 | if (TREE_CODE (t) == INTEGER_CST | |
90ad495b | 4120 | && TYPE_PTR_P (TREE_TYPE (t)) |
cb768824 | 4121 | && !integer_zerop (t)) |
4122 | { | |
4123 | if (!ctx->quiet) | |
4124 | error ("value %qE of type %qT is not a constant expression", | |
4125 | t, TREE_TYPE (t)); | |
4126 | *non_constant_p = true; | |
4127 | } | |
4128 | ||
09b42213 | 4129 | return t; |
4130 | } | |
09b42213 | 4131 | |
cb768824 | 4132 | tree_code tcode = TREE_CODE (t); |
4133 | switch (tcode) | |
09b42213 | 4134 | { |
9c96033c | 4135 | case RESULT_DECL: |
1227ba74 | 4136 | if (lval) |
9c96033c | 4137 | return t; |
4138 | /* We ask for an rvalue for the RESULT_DECL when indirecting | |
92a9c89e | 4139 | through an invisible reference, or in named return value |
4140 | optimization. */ | |
8ea8d065 | 4141 | if (tree *p = ctx->values->get (t)) |
4142 | return *p; | |
4143 | else | |
4144 | { | |
4145 | if (!ctx->quiet) | |
4146 | error ("%qE is not a constant expression", t); | |
4147 | *non_constant_p = true; | |
4148 | } | |
4149 | break; | |
9c96033c | 4150 | |
09b42213 | 4151 | case VAR_DECL: |
9c8aeb66 | 4152 | if (DECL_HAS_VALUE_EXPR_P (t)) |
33603066 | 4153 | return cxx_eval_constant_expression (ctx, DECL_VALUE_EXPR (t), |
4154 | lval, non_constant_p, overflow_p); | |
e3533433 | 4155 | /* fall through */ |
8b2b2f24 | 4156 | case CONST_DECL: |
4157 | /* We used to not check lval for CONST_DECL, but darwin.c uses | |
4158 | CONST_DECL for aggregate constants. */ | |
1227ba74 | 4159 | if (lval) |
09b42213 | 4160 | return t; |
594cc00c | 4161 | if (COMPLETE_TYPE_P (TREE_TYPE (t)) |
4162 | && is_really_empty_class (TREE_TYPE (t))) | |
c7282bf2 | 4163 | { |
4164 | /* If the class is empty, we aren't actually loading anything. */ | |
4165 | r = build_constructor (TREE_TYPE (t), NULL); | |
4166 | TREE_CONSTANT (r) = true; | |
4167 | } | |
4168 | else if (ctx->strict) | |
2055d27a | 4169 | r = decl_really_constant_value (t); |
4170 | else | |
4171 | r = decl_constant_value (t); | |
09b42213 | 4172 | if (TREE_CODE (r) == TARGET_EXPR |
4173 | && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR) | |
4174 | r = TARGET_EXPR_INITIAL (r); | |
f4ae4202 | 4175 | if (VAR_P (r)) |
cf72f34d | 4176 | if (tree *p = ctx->values->get (r)) |
a143e277 | 4177 | if (*p != NULL_TREE) |
4178 | r = *p; | |
09b42213 | 4179 | if (DECL_P (r)) |
4180 | { | |
f83e6885 | 4181 | if (!ctx->quiet) |
09b42213 | 4182 | non_const_var_error (r); |
4183 | *non_constant_p = true; | |
4184 | } | |
4185 | break; | |
4186 | ||
90567983 | 4187 | case DEBUG_BEGIN_STMT: |
4188 | /* ??? It might be nice to retain this information somehow, so | |
4189 | as to be able to step into a constexpr function call. */ | |
4190 | /* Fall through. */ | |
4191 | ||
09b42213 | 4192 | case FUNCTION_DECL: |
4193 | case TEMPLATE_DECL: | |
4194 | case LABEL_DECL: | |
00f21715 | 4195 | case LABEL_EXPR: |
4196 | case CASE_LABEL_EXPR: | |
2a2770c6 | 4197 | case PREDICT_EXPR: |
09b42213 | 4198 | return t; |
4199 | ||
4200 | case PARM_DECL: | |
90ad495b | 4201 | if (lval && !TYPE_REF_P (TREE_TYPE (t))) |
9c96033c | 4202 | /* glvalue use. */; |
4203 | else if (tree *p = ctx->values->get (r)) | |
4204 | r = *p; | |
1227ba74 | 4205 | else if (lval) |
09b42213 | 4206 | /* Defer in case this is only used for its type. */; |
90ad495b | 4207 | else if (TYPE_REF_P (TREE_TYPE (t))) |
1eb418cc | 4208 | /* Defer, there's no lvalue->rvalue conversion. */; |
53d2f3de | 4209 | else if (COMPLETE_TYPE_P (TREE_TYPE (t)) |
4210 | && is_really_empty_class (TREE_TYPE (t))) | |
a6613de7 | 4211 | { |
4212 | /* If the class is empty, we aren't actually loading anything. */ | |
4213 | r = build_constructor (TREE_TYPE (t), NULL); | |
4214 | TREE_CONSTANT (r) = true; | |
4215 | } | |
09b42213 | 4216 | else |
4217 | { | |
f83e6885 | 4218 | if (!ctx->quiet) |
09b42213 | 4219 | error ("%qE is not a constant expression", t); |
4220 | *non_constant_p = true; | |
4221 | } | |
4222 | break; | |
4223 | ||
4224 | case CALL_EXPR: | |
4225 | case AGGR_INIT_EXPR: | |
1227ba74 | 4226 | r = cxx_eval_call_expression (ctx, t, lval, |
09b42213 | 4227 | non_constant_p, overflow_p); |
4228 | break; | |
4229 | ||
9c96033c | 4230 | case DECL_EXPR: |
4231 | { | |
4232 | r = DECL_EXPR_DECL (t); | |
4233 | if (AGGREGATE_TYPE_P (TREE_TYPE (r)) | |
4234 | || VECTOR_TYPE_P (TREE_TYPE (r))) | |
4235 | { | |
4236 | new_ctx = *ctx; | |
4237 | new_ctx.object = r; | |
4238 | new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL); | |
4239 | CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true; | |
4240 | new_ctx.values->put (r, new_ctx.ctor); | |
4241 | ctx = &new_ctx; | |
4242 | } | |
4243 | ||
4244 | if (tree init = DECL_INITIAL (r)) | |
4245 | { | |
4246 | init = cxx_eval_constant_expression (ctx, init, | |
f83e6885 | 4247 | false, |
b2a43197 | 4248 | non_constant_p, overflow_p); |
5898f0d7 | 4249 | /* Don't share a CONSTRUCTOR that might be changed. */ |
e283bb4f | 4250 | init = unshare_constructor (init); |
9c96033c | 4251 | ctx->values->put (r, init); |
4252 | } | |
4253 | else if (ctx == &new_ctx) | |
4254 | /* We gave it a CONSTRUCTOR above. */; | |
4255 | else | |
4256 | ctx->values->put (r, NULL_TREE); | |
4257 | } | |
4258 | break; | |
4259 | ||
09b42213 | 4260 | case TARGET_EXPR: |
4261 | if (!literal_type_p (TREE_TYPE (t))) | |
4262 | { | |
f83e6885 | 4263 | if (!ctx->quiet) |
09b42213 | 4264 | { |
4265 | error ("temporary of non-literal type %qT in a " | |
4266 | "constant expression", TREE_TYPE (t)); | |
4267 | explain_non_literal_class (TREE_TYPE (t)); | |
4268 | } | |
4269 | *non_constant_p = true; | |
4270 | break; | |
4271 | } | |
cf72f34d | 4272 | if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t)))) |
4273 | { | |
4274 | /* We're being expanded without an explicit target, so start | |
4275 | initializing a new object; expansion with an explicit target | |
4276 | strips the TARGET_EXPR before we get here. */ | |
4277 | new_ctx = *ctx; | |
4278 | new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL); | |
4279 | CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true; | |
4280 | new_ctx.object = TARGET_EXPR_SLOT (t); | |
4281 | ctx->values->put (new_ctx.object, new_ctx.ctor); | |
4282 | ctx = &new_ctx; | |
4283 | } | |
1227ba74 | 4284 | /* Pass false for 'lval' because this indicates |
09b42213 | 4285 | initialization of a temporary. */ |
cf72f34d | 4286 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
f83e6885 | 4287 | false, |
b2a43197 | 4288 | non_constant_p, overflow_p); |
09b42213 | 4289 | if (!*non_constant_p) |
4290 | /* Adjust the type of the result to the type of the temporary. */ | |
4291 | r = adjust_temp_type (TREE_TYPE (t), r); | |
1227ba74 | 4292 | if (lval) |
1eb418cc | 4293 | { |
4294 | tree slot = TARGET_EXPR_SLOT (t); | |
e283bb4f | 4295 | r = unshare_constructor (r); |
1eb418cc | 4296 | ctx->values->put (slot, r); |
4297 | return slot; | |
4298 | } | |
09b42213 | 4299 | break; |
4300 | ||
9c96033c | 4301 | case INIT_EXPR: |
9c96033c | 4302 | case MODIFY_EXPR: |
2a2770c6 | 4303 | gcc_assert (jump_target == NULL || *jump_target == NULL_TREE); |
1227ba74 | 4304 | r = cxx_eval_store_expression (ctx, t, lval, |
9c96033c | 4305 | non_constant_p, overflow_p); |
4306 | break; | |
4307 | ||
09b42213 | 4308 | case SCOPE_REF: |
cf72f34d | 4309 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
1227ba74 | 4310 | lval, |
b2a43197 | 4311 | non_constant_p, overflow_p); |
09b42213 | 4312 | break; |
4313 | ||
4314 | case RETURN_EXPR: | |
10fb495c | 4315 | if (TREE_OPERAND (t, 0) != NULL_TREE) |
4316 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), | |
4317 | lval, | |
4318 | non_constant_p, overflow_p); | |
2c572f5f | 4319 | if (jump_target) |
4320 | *jump_target = t; | |
4321 | else | |
4322 | { | |
4323 | /* Can happen with ({ return true; }) && false; passed to | |
4324 | maybe_constant_value. There is nothing to jump over in this | |
4325 | case, and the bug will be diagnosed later. */ | |
4326 | gcc_assert (ctx->quiet); | |
4327 | *non_constant_p = true; | |
4328 | } | |
00f21715 | 4329 | break; |
4330 | ||
e0e672a0 | 4331 | case SAVE_EXPR: |
4332 | /* Avoid evaluating a SAVE_EXPR more than once. */ | |
4333 | if (tree *p = ctx->values->get (t)) | |
4334 | r = *p; | |
4335 | else | |
4336 | { | |
c8f6aeb1 | 4337 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false, |
e0e672a0 | 4338 | non_constant_p, overflow_p); |
4339 | ctx->values->put (t, r); | |
2631cb67 | 4340 | if (ctx->save_exprs) |
4341 | ctx->save_exprs->add (t); | |
e0e672a0 | 4342 | } |
4343 | break; | |
4344 | ||
09b42213 | 4345 | case NON_LVALUE_EXPR: |
4346 | case TRY_CATCH_EXPR: | |
7f67d68c | 4347 | case TRY_BLOCK: |
09b42213 | 4348 | case CLEANUP_POINT_EXPR: |
4349 | case MUST_NOT_THROW_EXPR: | |
9c96033c | 4350 | case EXPR_STMT: |
4351 | case EH_SPEC_BLOCK: | |
cf72f34d | 4352 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
1227ba74 | 4353 | lval, |
00f21715 | 4354 | non_constant_p, overflow_p, |
4355 | jump_target); | |
09b42213 | 4356 | break; |
4357 | ||
7f67d68c | 4358 | case TRY_FINALLY_EXPR: |
4359 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, | |
4360 | non_constant_p, overflow_p, | |
4361 | jump_target); | |
4362 | if (!*non_constant_p) | |
4363 | /* Also evaluate the cleanup. */ | |
4364 | cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true, | |
4365 | non_constant_p, overflow_p, | |
4366 | jump_target); | |
4367 | break; | |
4368 | ||
09b42213 | 4369 | /* These differ from cxx_eval_unary_expression in that this doesn't |
4370 | check for a constant operand or result; an address can be | |
4371 | constant without its operand being, and vice versa. */ | |
d2c63826 | 4372 | case MEM_REF: |
09b42213 | 4373 | case INDIRECT_REF: |
1227ba74 | 4374 | r = cxx_eval_indirect_ref (ctx, t, lval, |
09b42213 | 4375 | non_constant_p, overflow_p); |
4376 | break; | |
4377 | ||
4378 | case ADDR_EXPR: | |
4379 | { | |
4380 | tree oldop = TREE_OPERAND (t, 0); | |
cf72f34d | 4381 | tree op = cxx_eval_constant_expression (ctx, oldop, |
1227ba74 | 4382 | /*lval*/true, |
b2a43197 | 4383 | non_constant_p, overflow_p); |
09b42213 | 4384 | /* Don't VERIFY_CONSTANT here. */ |
4385 | if (*non_constant_p) | |
4386 | return t; | |
1eb418cc | 4387 | gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR); |
09b42213 | 4388 | /* This function does more aggressive folding than fold itself. */ |
4389 | r = build_fold_addr_expr_with_type (op, TREE_TYPE (t)); | |
4390 | if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop) | |
4391 | return t; | |
4392 | break; | |
4393 | } | |
4394 | ||
4395 | case REALPART_EXPR: | |
4396 | case IMAGPART_EXPR: | |
6fe424c2 | 4397 | if (lval) |
4398 | { | |
4399 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, | |
4400 | non_constant_p, overflow_p); | |
4401 | if (r == error_mark_node) | |
4402 | ; | |
4403 | else if (r == TREE_OPERAND (t, 0)) | |
4404 | r = t; | |
4405 | else | |
4406 | r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r); | |
4407 | break; | |
4408 | } | |
4409 | /* FALLTHRU */ | |
09b42213 | 4410 | case CONJ_EXPR: |
4411 | case FIX_TRUNC_EXPR: | |
4412 | case FLOAT_EXPR: | |
4413 | case NEGATE_EXPR: | |
4414 | case ABS_EXPR: | |
4415 | case BIT_NOT_EXPR: | |
4416 | case TRUTH_NOT_EXPR: | |
4417 | case FIXED_CONVERT_EXPR: | |
1227ba74 | 4418 | r = cxx_eval_unary_expression (ctx, t, lval, |
09b42213 | 4419 | non_constant_p, overflow_p); |
4420 | break; | |
4421 | ||
4422 | case SIZEOF_EXPR: | |
d2c63826 | 4423 | r = fold_sizeof_expr (t); |
09b42213 | 4424 | VERIFY_CONSTANT (r); |
4425 | break; | |
4426 | ||
4427 | case COMPOUND_EXPR: | |
4428 | { | |
4429 | /* check_return_expr sometimes wraps a TARGET_EXPR in a | |
4430 | COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR | |
4431 | introduced by build_call_a. */ | |
4432 | tree op0 = TREE_OPERAND (t, 0); | |
4433 | tree op1 = TREE_OPERAND (t, 1); | |
4434 | STRIP_NOPS (op1); | |
4435 | if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) | |
4436 | || TREE_CODE (op1) == EMPTY_CLASS_EXPR) | |
f83e6885 | 4437 | r = cxx_eval_constant_expression (ctx, op0, |
1227ba74 | 4438 | lval, non_constant_p, overflow_p, |
00f21715 | 4439 | jump_target); |
09b42213 | 4440 | else |
4441 | { | |
4442 | /* Check that the LHS is constant and then discard it. */ | |
f83e6885 | 4443 | cxx_eval_constant_expression (ctx, op0, |
c8f6aeb1 | 4444 | true, non_constant_p, overflow_p, |
00f21715 | 4445 | jump_target); |
cf03ba19 | 4446 | if (*non_constant_p) |
4447 | return t; | |
09b42213 | 4448 | op1 = TREE_OPERAND (t, 1); |
f83e6885 | 4449 | r = cxx_eval_constant_expression (ctx, op1, |
1227ba74 | 4450 | lval, non_constant_p, overflow_p, |
00f21715 | 4451 | jump_target); |
09b42213 | 4452 | } |
4453 | } | |
4454 | break; | |
4455 | ||
4456 | case POINTER_PLUS_EXPR: | |
57e83b58 | 4457 | case POINTER_DIFF_EXPR: |
09b42213 | 4458 | case PLUS_EXPR: |
4459 | case MINUS_EXPR: | |
4460 | case MULT_EXPR: | |
4461 | case TRUNC_DIV_EXPR: | |
4462 | case CEIL_DIV_EXPR: | |
4463 | case FLOOR_DIV_EXPR: | |
4464 | case ROUND_DIV_EXPR: | |
4465 | case TRUNC_MOD_EXPR: | |
4466 | case CEIL_MOD_EXPR: | |
4467 | case ROUND_MOD_EXPR: | |
4468 | case RDIV_EXPR: | |
4469 | case EXACT_DIV_EXPR: | |
4470 | case MIN_EXPR: | |
4471 | case MAX_EXPR: | |
4472 | case LSHIFT_EXPR: | |
4473 | case RSHIFT_EXPR: | |
4474 | case LROTATE_EXPR: | |
4475 | case RROTATE_EXPR: | |
4476 | case BIT_IOR_EXPR: | |
4477 | case BIT_XOR_EXPR: | |
4478 | case BIT_AND_EXPR: | |
4479 | case TRUTH_XOR_EXPR: | |
4480 | case LT_EXPR: | |
4481 | case LE_EXPR: | |
4482 | case GT_EXPR: | |
4483 | case GE_EXPR: | |
4484 | case EQ_EXPR: | |
4485 | case NE_EXPR: | |
4486 | case UNORDERED_EXPR: | |
4487 | case ORDERED_EXPR: | |
4488 | case UNLT_EXPR: | |
4489 | case UNLE_EXPR: | |
4490 | case UNGT_EXPR: | |
4491 | case UNGE_EXPR: | |
4492 | case UNEQ_EXPR: | |
4493 | case LTGT_EXPR: | |
4494 | case RANGE_EXPR: | |
4495 | case COMPLEX_EXPR: | |
1227ba74 | 4496 | r = cxx_eval_binary_expression (ctx, t, lval, |
09b42213 | 4497 | non_constant_p, overflow_p); |
4498 | break; | |
4499 | ||
4500 | /* fold can introduce non-IF versions of these; still treat them as | |
4501 | short-circuiting. */ | |
4502 | case TRUTH_AND_EXPR: | |
4503 | case TRUTH_ANDIF_EXPR: | |
cf72f34d | 4504 | r = cxx_eval_logical_expression (ctx, t, boolean_false_node, |
09b42213 | 4505 | boolean_true_node, |
1227ba74 | 4506 | lval, |
09b42213 | 4507 | non_constant_p, overflow_p); |
4508 | break; | |
4509 | ||
4510 | case TRUTH_OR_EXPR: | |
4511 | case TRUTH_ORIF_EXPR: | |
cf72f34d | 4512 | r = cxx_eval_logical_expression (ctx, t, boolean_true_node, |
09b42213 | 4513 | boolean_false_node, |
1227ba74 | 4514 | lval, |
09b42213 | 4515 | non_constant_p, overflow_p); |
4516 | break; | |
4517 | ||
4518 | case ARRAY_REF: | |
1227ba74 | 4519 | r = cxx_eval_array_reference (ctx, t, lval, |
09b42213 | 4520 | non_constant_p, overflow_p); |
4521 | break; | |
4522 | ||
4523 | case COMPONENT_REF: | |
4524 | if (is_overloaded_fn (t)) | |
4525 | { | |
4526 | /* We can only get here in checking mode via | |
4527 | build_non_dependent_expr, because any expression that | |
4528 | calls or takes the address of the function will have | |
4529 | pulled a FUNCTION_DECL out of the COMPONENT_REF. */ | |
f83e6885 | 4530 | gcc_checking_assert (ctx->quiet || errorcount); |
09b42213 | 4531 | *non_constant_p = true; |
4532 | return t; | |
4533 | } | |
1227ba74 | 4534 | r = cxx_eval_component_reference (ctx, t, lval, |
09b42213 | 4535 | non_constant_p, overflow_p); |
4536 | break; | |
4537 | ||
4538 | case BIT_FIELD_REF: | |
1227ba74 | 4539 | r = cxx_eval_bit_field_ref (ctx, t, lval, |
09b42213 | 4540 | non_constant_p, overflow_p); |
4541 | break; | |
4542 | ||
4543 | case COND_EXPR: | |
2a2770c6 | 4544 | if (jump_target && *jump_target) |
4545 | { | |
4546 | /* When jumping to a label, the label might be either in the | |
4547 | then or else blocks, so process then block first in skipping | |
4548 | mode first, and if we are still in the skipping mode at its end, | |
4549 | process the else block too. */ | |
4550 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), | |
4551 | lval, non_constant_p, overflow_p, | |
4552 | jump_target); | |
4553 | if (*jump_target) | |
4554 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2), | |
4555 | lval, non_constant_p, overflow_p, | |
4556 | jump_target); | |
4557 | break; | |
4558 | } | |
1227ba74 | 4559 | r = cxx_eval_conditional_expression (ctx, t, lval, |
00f21715 | 4560 | non_constant_p, overflow_p, |
4561 | jump_target); | |
09b42213 | 4562 | break; |
31595caf | 4563 | case VEC_COND_EXPR: |
4564 | r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p, | |
4565 | overflow_p); | |
4566 | break; | |
09b42213 | 4567 | |
4568 | case CONSTRUCTOR: | |
1a3631bc | 4569 | if (TREE_CONSTANT (t)) |
58b0f9ce | 4570 | { |
4571 | /* Don't re-process a constant CONSTRUCTOR, but do fold it to | |
4572 | VECTOR_CST if applicable. */ | |
79e65a6b | 4573 | verify_constructor_flags (t); |
58b0f9ce | 4574 | if (TREE_CONSTANT (t)) |
4575 | return fold (t); | |
4576 | } | |
1227ba74 | 4577 | r = cxx_eval_bare_aggregate (ctx, t, lval, |
09b42213 | 4578 | non_constant_p, overflow_p); |
4579 | break; | |
4580 | ||
4581 | case VEC_INIT_EXPR: | |
4582 | /* We can get this in a defaulted constructor for a class with a | |
4583 | non-static data member of array type. Either the initializer will | |
4584 | be NULL, meaning default-initialization, or it will be an lvalue | |
4585 | or xvalue of the same type, meaning direct-initialization from the | |
4586 | corresponding member. */ | |
1227ba74 | 4587 | r = cxx_eval_vec_init (ctx, t, lval, |
09b42213 | 4588 | non_constant_p, overflow_p); |
4589 | break; | |
4590 | ||
09b42213 | 4591 | case VEC_PERM_EXPR: |
1227ba74 | 4592 | r = cxx_eval_trinary_expression (ctx, t, lval, |
09b42213 | 4593 | non_constant_p, overflow_p); |
4594 | break; | |
4595 | ||
941fafa5 | 4596 | case NOP_EXPR: |
4597 | if (REINTERPRET_CAST_P (t)) | |
4598 | { | |
4599 | if (!ctx->quiet) | |
d3a3cfb8 | 4600 | error_at (cp_expr_loc_or_loc (t, input_location), |
941fafa5 | 4601 | "a reinterpret_cast is not a constant expression"); |
4602 | *non_constant_p = true; | |
4603 | return t; | |
4604 | } | |
4605 | /* FALLTHROUGH. */ | |
09b42213 | 4606 | case CONVERT_EXPR: |
4607 | case VIEW_CONVERT_EXPR: | |
d2c63826 | 4608 | case UNARY_PLUS_EXPR: |
09b42213 | 4609 | { |
4610 | tree oldop = TREE_OPERAND (t, 0); | |
d2c63826 | 4611 | |
cf72f34d | 4612 | tree op = cxx_eval_constant_expression (ctx, oldop, |
1227ba74 | 4613 | lval, |
b2a43197 | 4614 | non_constant_p, overflow_p); |
09b42213 | 4615 | if (*non_constant_p) |
4616 | return t; | |
f16ed232 | 4617 | tree type = TREE_TYPE (t); |
4618 | if (TREE_CODE (op) == PTRMEM_CST | |
4619 | && !TYPE_PTRMEM_P (type)) | |
4620 | op = cplus_expand_constant (op); | |
941fafa5 | 4621 | |
6a9b35ef | 4622 | if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR) |
4623 | { | |
941fafa5 | 4624 | if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op)) |
4625 | && !can_convert_qual (type, op)) | |
4626 | op = cplus_expand_constant (op); | |
4627 | return cp_fold_convert (type, op); | |
6a9b35ef | 4628 | } |
cb768824 | 4629 | |
d03fa520 | 4630 | if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST) |
09b42213 | 4631 | { |
cb768824 | 4632 | if (integer_zerop (op)) |
4633 | { | |
90ad495b | 4634 | if (TYPE_REF_P (type)) |
cb768824 | 4635 | { |
4636 | if (!ctx->quiet) | |
d3a3cfb8 | 4637 | error_at (cp_expr_loc_or_loc (t, input_location), |
cb768824 | 4638 | "dereferencing a null pointer"); |
4639 | *non_constant_p = true; | |
4640 | return t; | |
4641 | } | |
90ad495b | 4642 | else if (TYPE_PTR_P (TREE_TYPE (op))) |
cb768824 | 4643 | { |
4644 | tree from = TREE_TYPE (op); | |
4645 | ||
4646 | if (!can_convert (type, from, tf_none)) | |
4647 | { | |
4648 | if (!ctx->quiet) | |
d3a3cfb8 | 4649 | error_at (cp_expr_loc_or_loc (t, input_location), |
cb768824 | 4650 | "conversion of %qT null pointer to %qT " |
4651 | "is not a constant expression", | |
4652 | from, type); | |
4653 | *non_constant_p = true; | |
4654 | return t; | |
4655 | } | |
4656 | } | |
4657 | } | |
4658 | else | |
4659 | { | |
4660 | /* This detects for example: | |
4661 | reinterpret_cast<void*>(sizeof 0) | |
4662 | */ | |
4663 | if (!ctx->quiet) | |
d3a3cfb8 | 4664 | error_at (cp_expr_loc_or_loc (t, input_location), |
cb768824 | 4665 | "%<reinterpret_cast<%T>(%E)%> is not " |
ea034e2c | 4666 | "a constant expression", |
cb768824 | 4667 | type, op); |
4668 | *non_constant_p = true; | |
4669 | return t; | |
4670 | } | |
09b42213 | 4671 | } |
941fafa5 | 4672 | |
d2c63826 | 4673 | if (op == oldop && tcode != UNARY_PLUS_EXPR) |
09b42213 | 4674 | /* We didn't fold at the top so we could check for ptr-int |
4675 | conversion. */ | |
4676 | return fold (t); | |
941fafa5 | 4677 | |
d2c63826 | 4678 | if (tcode == UNARY_PLUS_EXPR) |
4679 | r = fold_convert (TREE_TYPE (t), op); | |
4680 | else | |
4681 | r = fold_build1 (tcode, type, op); | |
941fafa5 | 4682 | |
09b42213 | 4683 | /* Conversion of an out-of-range value has implementation-defined |
4684 | behavior; the language considers it different from arithmetic | |
4685 | overflow, which is undefined. */ | |
4686 | if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op)) | |
4687 | TREE_OVERFLOW (r) = false; | |
4688 | } | |
4689 | break; | |
4690 | ||
4691 | case EMPTY_CLASS_EXPR: | |
4692 | /* This is good enough for a function argument that might not get | |
4693 | used, and they can't do anything with it, so just return it. */ | |
4694 | return t; | |
4695 | ||
9c96033c | 4696 | case STATEMENT_LIST: |
00f21715 | 4697 | new_ctx = *ctx; |
4698 | new_ctx.ctor = new_ctx.object = NULL_TREE; | |
f83e6885 | 4699 | return cxx_eval_statement_list (&new_ctx, t, |
00f21715 | 4700 | non_constant_p, overflow_p, jump_target); |
9c96033c | 4701 | |
4702 | case BIND_EXPR: | |
4703 | return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t), | |
1227ba74 | 4704 | lval, |
00f21715 | 4705 | non_constant_p, overflow_p, |
4706 | jump_target); | |
9c96033c | 4707 | |
09b42213 | 4708 | case PREINCREMENT_EXPR: |
4709 | case POSTINCREMENT_EXPR: | |
4710 | case PREDECREMENT_EXPR: | |
4711 | case POSTDECREMENT_EXPR: | |
f83e6885 | 4712 | return cxx_eval_increment_expression (ctx, t, |
1227ba74 | 4713 | lval, non_constant_p, overflow_p); |
9c96033c | 4714 | |
4715 | case LAMBDA_EXPR: | |
09b42213 | 4716 | case NEW_EXPR: |
4717 | case VEC_NEW_EXPR: | |
4718 | case DELETE_EXPR: | |
4719 | case VEC_DELETE_EXPR: | |
4720 | case THROW_EXPR: | |
09b42213 | 4721 | case MODOP_EXPR: |
4722 | /* GCC internal stuff. */ | |
4723 | case VA_ARG_EXPR: | |
4724 | case OBJ_TYPE_REF: | |
09b42213 | 4725 | case NON_DEPENDENT_EXPR: |
4726 | case BASELINK: | |
09b42213 | 4727 | case OFFSET_REF: |
f83e6885 | 4728 | if (!ctx->quiet) |
d3a3cfb8 | 4729 | error_at (cp_expr_loc_or_loc (t, input_location), |
ea034e2c | 4730 | "expression %qE is not a constant expression", t); |
09b42213 | 4731 | *non_constant_p = true; |
4732 | break; | |
4733 | ||
cf72f34d | 4734 | case PLACEHOLDER_EXPR: |
85de9604 | 4735 | /* Use of the value or address of the current object. */ |
4736 | if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t))) | |
4737 | return cxx_eval_constant_expression (ctx, ctor, lval, | |
4738 | non_constant_p, overflow_p); | |
4739 | /* A placeholder without a referent. We can get here when | |
4740 | checking whether NSDMIs are noexcept, or in massage_init_elt; | |
4741 | just say it's non-constant for now. */ | |
4742 | gcc_assert (ctx->quiet); | |
4743 | *non_constant_p = true; | |
cf72f34d | 4744 | break; |
4745 | ||
cf03ba19 | 4746 | case EXIT_EXPR: |
4747 | { | |
4748 | tree cond = TREE_OPERAND (t, 0); | |
4749 | cond = cxx_eval_constant_expression (ctx, cond, /*lval*/false, | |
4750 | non_constant_p, overflow_p); | |
4751 | VERIFY_CONSTANT (cond); | |
4752 | if (integer_nonzerop (cond)) | |
4753 | *jump_target = t; | |
4754 | } | |
4755 | break; | |
4756 | ||
9c96033c | 4757 | case GOTO_EXPR: |
00f21715 | 4758 | *jump_target = TREE_OPERAND (t, 0); |
d29e4e8c | 4759 | gcc_assert (breaks (jump_target) || continues (jump_target) |
4760 | /* Allow for jumping to a cdtor_label. */ | |
4761 | || returns (jump_target)); | |
00f21715 | 4762 | break; |
4763 | ||
9c96033c | 4764 | case LOOP_EXPR: |
f83e6885 | 4765 | cxx_eval_loop_expr (ctx, t, |
00f21715 | 4766 | non_constant_p, overflow_p, jump_target); |
4767 | break; | |
4768 | ||
9c96033c | 4769 | case SWITCH_EXPR: |
f83e6885 | 4770 | cxx_eval_switch_expr (ctx, t, |
00f21715 | 4771 | non_constant_p, overflow_p, jump_target); |
9c96033c | 4772 | break; |
4773 | ||
56c12fd4 | 4774 | case REQUIRES_EXPR: |
4775 | /* It's possible to get a requires-expression in a constant | |
4776 | expression. For example: | |
4777 | ||
4778 | template<typename T> concept bool C() { | |
4779 | return requires (T t) { t; }; | |
4780 | } | |
4781 | ||
4782 | template<typename T> requires !C<T>() void f(T); | |
4783 | ||
4784 | Normalization leaves f with the associated constraint | |
4785 | '!requires (T t) { ... }' which is not transformed into | |
4786 | a constraint. */ | |
4787 | if (!processing_template_decl) | |
4788 | return evaluate_constraint_expression (t, NULL_TREE); | |
4789 | else | |
4790 | *non_constant_p = true; | |
4791 | return t; | |
4792 | ||
24c6ee98 | 4793 | case ANNOTATE_EXPR: |
24c6ee98 | 4794 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
4795 | lval, | |
4796 | non_constant_p, overflow_p, | |
4797 | jump_target); | |
4798 | break; | |
4799 | ||
7285637a | 4800 | case USING_STMT: |
4801 | r = void_node; | |
4802 | break; | |
4803 | ||
09b42213 | 4804 | default: |
b9a3af23 | 4805 | if (STATEMENT_CODE_P (TREE_CODE (t))) |
4806 | { | |
4807 | /* This function doesn't know how to deal with pre-genericize | |
4808 | statements; this can only happen with statement-expressions, | |
4809 | so for now just fail. */ | |
4810 | if (!ctx->quiet) | |
4811 | error_at (EXPR_LOCATION (t), | |
ea034e2c | 4812 | "statement is not a constant expression"); |
b9a3af23 | 4813 | } |
4814 | else | |
4815 | internal_error ("unexpected expression %qE of kind %s", t, | |
4816 | get_tree_code_name (TREE_CODE (t))); | |
09b42213 | 4817 | *non_constant_p = true; |
4818 | break; | |
4819 | } | |
4820 | ||
4821 | if (r == error_mark_node) | |
4822 | *non_constant_p = true; | |
4823 | ||
4824 | if (*non_constant_p) | |
4825 | return t; | |
4826 | else | |
4827 | return r; | |
4828 | } | |
4829 | ||
6e1bbaae | 4830 | /* P0859: A function is needed for constant evaluation if it is a constexpr |
4831 | function that is named by an expression ([basic.def.odr]) that is | |
4832 | potentially constant evaluated. | |
4833 | ||
4834 | So we need to instantiate any constexpr functions mentioned by the | |
4835 | expression even if the definition isn't needed for evaluating the | |
4836 | expression. */ | |
4837 | ||
4838 | static tree | |
4839 | instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/) | |
4840 | { | |
4841 | if (TREE_CODE (*tp) == FUNCTION_DECL | |
4842 | && DECL_DECLARED_CONSTEXPR_P (*tp) | |
4843 | && !DECL_INITIAL (*tp) | |
eed138cf | 4844 | && !trivial_fn_p (*tp) |
6e1bbaae | 4845 | && DECL_TEMPLOID_INSTANTIATION (*tp)) |
4846 | { | |
4847 | ++function_depth; | |
4848 | instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false); | |
4849 | --function_depth; | |
4850 | } | |
4851 | else if (TREE_CODE (*tp) == CALL_EXPR | |
4852 | || TREE_CODE (*tp) == AGGR_INIT_EXPR) | |
4853 | { | |
4854 | if (EXPR_HAS_LOCATION (*tp)) | |
4855 | input_location = EXPR_LOCATION (*tp); | |
4856 | } | |
4857 | ||
4858 | if (!EXPR_P (*tp)) | |
4859 | *walk_subtrees = 0; | |
4860 | ||
4861 | return NULL_TREE; | |
4862 | } | |
4863 | static void | |
4864 | instantiate_constexpr_fns (tree t) | |
4865 | { | |
4866 | location_t loc = input_location; | |
4867 | cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL); | |
4868 | input_location = loc; | |
4869 | } | |
4870 | ||
09b42213 | 4871 | static tree |
cf72f34d | 4872 | cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant, |
2055d27a | 4873 | bool strict = true, tree object = NULL_TREE) |
09b42213 | 4874 | { |
c300fe28 | 4875 | auto_timevar time (TV_CONSTEXPR); |
4876 | ||
09b42213 | 4877 | bool non_constant_p = false; |
4878 | bool overflow_p = false; | |
cf72f34d | 4879 | hash_map<tree,tree> map; |
2631cb67 | 4880 | |
2a2770c6 | 4881 | constexpr_ctx ctx = { NULL, &map, NULL, NULL, NULL, NULL, |
2631cb67 | 4882 | allow_non_constant, strict }; |
4883 | ||
cf72f34d | 4884 | tree type = initialized_type (t); |
cf72f34d | 4885 | tree r = t; |
4886 | if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)) | |
4887 | { | |
4888 | /* In C++14 an NSDMI can participate in aggregate initialization, | |
4889 | and can refer to the address of the object being initialized, so | |
4890 | we need to pass in the relevant VAR_DECL if we want to do the | |
4891 | evaluation in a single pass. The evaluation will dynamically | |
4892 | update ctx.values for the VAR_DECL. We use the same strategy | |
4893 | for C++11 constexpr constructors that refer to the object being | |
4894 | initialized. */ | |
4895 | ctx.ctor = build_constructor (type, NULL); | |
4896 | CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx.ctor) = true; | |
9c96033c | 4897 | if (!object) |
4898 | { | |
4899 | if (TREE_CODE (t) == TARGET_EXPR) | |
4900 | object = TARGET_EXPR_SLOT (t); | |
4901 | else if (TREE_CODE (t) == AGGR_INIT_EXPR) | |
4902 | object = AGGR_INIT_EXPR_SLOT (t); | |
4903 | } | |
cf72f34d | 4904 | ctx.object = object; |
4905 | if (object) | |
4906 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
4907 | (type, TREE_TYPE (object))); | |
4908 | if (object && DECL_P (object)) | |
4909 | map.put (object, ctx.ctor); | |
4910 | if (TREE_CODE (r) == TARGET_EXPR) | |
4911 | /* Avoid creating another CONSTRUCTOR when we expand the | |
4912 | TARGET_EXPR. */ | |
4913 | r = TARGET_EXPR_INITIAL (r); | |
4914 | } | |
4915 | ||
6e1bbaae | 4916 | instantiate_constexpr_fns (r); |
f83e6885 | 4917 | r = cxx_eval_constant_expression (&ctx, r, |
b2a43197 | 4918 | false, &non_constant_p, &overflow_p); |
09b42213 | 4919 | |
4920 | verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p); | |
4921 | ||
bbcc9042 | 4922 | /* Mutable logic is a bit tricky: we want to allow initialization of |
4923 | constexpr variables with mutable members, but we can't copy those | |
4924 | members to another constexpr variable. */ | |
4925 | if (TREE_CODE (r) == CONSTRUCTOR | |
4926 | && CONSTRUCTOR_MUTABLE_POISON (r)) | |
09b42213 | 4927 | { |
09b42213 | 4928 | if (!allow_non_constant) |
bbcc9042 | 4929 | error ("%qE is not a constant expression because it refers to " |
4930 | "mutable subobjects of %qT", t, type); | |
09b42213 | 4931 | non_constant_p = true; |
4932 | } | |
4933 | ||
c6d97004 | 4934 | if (TREE_CODE (r) == CONSTRUCTOR |
4935 | && CONSTRUCTOR_NO_IMPLICIT_ZERO (r)) | |
4936 | { | |
4937 | if (!allow_non_constant) | |
4938 | error ("%qE is not a constant expression because it refers to " | |
4939 | "an incompletely initialized variable", t); | |
4940 | TREE_CONSTANT (r) = false; | |
4941 | non_constant_p = true; | |
4942 | } | |
4943 | ||
09b42213 | 4944 | /* Technically we should check this for all subexpressions, but that |
4945 | runs into problems with our internal representation of pointer | |
4946 | subtraction and the 5.19 rules are still in flux. */ | |
4947 | if (CONVERT_EXPR_CODE_P (TREE_CODE (r)) | |
4948 | && ARITHMETIC_TYPE_P (TREE_TYPE (r)) | |
4949 | && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR) | |
4950 | { | |
4951 | if (!allow_non_constant) | |
4952 | error ("conversion from pointer type %qT " | |
ea034e2c | 4953 | "to arithmetic type %qT in a constant expression", |
09b42213 | 4954 | TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r)); |
4955 | non_constant_p = true; | |
4956 | } | |
4957 | ||
4958 | if (!non_constant_p && overflow_p) | |
4959 | non_constant_p = true; | |
4960 | ||
c6c0523b | 4961 | /* Unshare the result unless it's a CONSTRUCTOR in which case it's already |
4962 | unshared. */ | |
4963 | bool should_unshare = true; | |
4964 | if (r == t || TREE_CODE (r) == CONSTRUCTOR) | |
4965 | should_unshare = false; | |
4966 | ||
09b42213 | 4967 | if (non_constant_p && !allow_non_constant) |
4968 | return error_mark_node; | |
4969 | else if (non_constant_p && TREE_CONSTANT (r)) | |
4970 | { | |
1d29f4ea | 4971 | /* This isn't actually constant, so unset TREE_CONSTANT. |
4972 | Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires | |
4973 | it to be set if it is invariant address, even when it is not | |
4974 | a valid C++ constant expression. Wrap it with a NOP_EXPR | |
4975 | instead. */ | |
4976 | if (EXPR_P (r) && TREE_CODE (r) != ADDR_EXPR) | |
09b42213 | 4977 | r = copy_node (r); |
4978 | else if (TREE_CODE (r) == CONSTRUCTOR) | |
4979 | r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r); | |
4980 | else | |
4981 | r = build_nop (TREE_TYPE (r), r); | |
4982 | TREE_CONSTANT (r) = false; | |
4983 | } | |
4984 | else if (non_constant_p || r == t) | |
4985 | return t; | |
4986 | ||
c6c0523b | 4987 | if (should_unshare) |
4988 | r = unshare_expr (r); | |
4989 | ||
09b42213 | 4990 | if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r))) |
4991 | { | |
4992 | if (TREE_CODE (t) == TARGET_EXPR | |
4993 | && TARGET_EXPR_INITIAL (t) == r) | |
4994 | return t; | |
4995 | else | |
4996 | { | |
4997 | r = get_target_expr (r); | |
4998 | TREE_CONSTANT (r) = true; | |
4999 | return r; | |
5000 | } | |
5001 | } | |
5002 | else | |
5003 | return r; | |
5004 | } | |
5005 | ||
5006 | /* Returns true if T is a valid subexpression of a constant expression, | |
5007 | even if it isn't itself a constant expression. */ | |
5008 | ||
5009 | bool | |
5010 | is_sub_constant_expr (tree t) | |
5011 | { | |
5012 | bool non_constant_p = false; | |
5013 | bool overflow_p = false; | |
cf72f34d | 5014 | hash_map <tree, tree> map; |
2631cb67 | 5015 | |
2a2770c6 | 5016 | constexpr_ctx ctx = { NULL, &map, NULL, NULL, NULL, NULL, true, true }; |
2631cb67 | 5017 | |
6e1bbaae | 5018 | instantiate_constexpr_fns (t); |
f83e6885 | 5019 | cxx_eval_constant_expression (&ctx, t, false, &non_constant_p, |
b2a43197 | 5020 | &overflow_p); |
09b42213 | 5021 | return !non_constant_p && !overflow_p; |
5022 | } | |
5023 | ||
5024 | /* If T represents a constant expression returns its reduced value. | |
5025 | Otherwise return error_mark_node. If T is dependent, then | |
5026 | return NULL. */ | |
5027 | ||
5028 | tree | |
cf72f34d | 5029 | cxx_constant_value (tree t, tree decl) |
09b42213 | 5030 | { |
2055d27a | 5031 | return cxx_eval_outermost_constant_expr (t, false, true, decl); |
09b42213 | 5032 | } |
5033 | ||
d2c63826 | 5034 | /* Helper routine for fold_simple function. Either return simplified |
5035 | expression T, otherwise NULL_TREE. | |
5036 | In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold | |
5037 | even if we are within template-declaration. So be careful on call, as in | |
5038 | such case types can be undefined. */ | |
5039 | ||
5040 | static tree | |
5041 | fold_simple_1 (tree t) | |
5042 | { | |
5043 | tree op1; | |
5044 | enum tree_code code = TREE_CODE (t); | |
5045 | ||
5046 | switch (code) | |
5047 | { | |
5048 | case INTEGER_CST: | |
5049 | case REAL_CST: | |
5050 | case VECTOR_CST: | |
5051 | case FIXED_CST: | |
5052 | case COMPLEX_CST: | |
5053 | return t; | |
5054 | ||
5055 | case SIZEOF_EXPR: | |
5056 | return fold_sizeof_expr (t); | |
5057 | ||
5058 | case ABS_EXPR: | |
5059 | case CONJ_EXPR: | |
5060 | case REALPART_EXPR: | |
5061 | case IMAGPART_EXPR: | |
5062 | case NEGATE_EXPR: | |
5063 | case BIT_NOT_EXPR: | |
5064 | case TRUTH_NOT_EXPR: | |
5065 | case NOP_EXPR: | |
5066 | case VIEW_CONVERT_EXPR: | |
5067 | case CONVERT_EXPR: | |
5068 | case FLOAT_EXPR: | |
5069 | case FIX_TRUNC_EXPR: | |
5070 | case FIXED_CONVERT_EXPR: | |
5071 | case ADDR_SPACE_CONVERT_EXPR: | |
5072 | ||
5073 | op1 = TREE_OPERAND (t, 0); | |
5074 | ||
5075 | t = const_unop (code, TREE_TYPE (t), op1); | |
5076 | if (!t) | |
5077 | return NULL_TREE; | |
5078 | ||
5079 | if (CONVERT_EXPR_CODE_P (code) | |
5080 | && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1)) | |
5081 | TREE_OVERFLOW (t) = false; | |
5082 | return t; | |
5083 | ||
5084 | default: | |
5085 | return NULL_TREE; | |
5086 | } | |
5087 | } | |
5088 | ||
5089 | /* If T is a simple constant expression, returns its simplified value. | |
6fe5fea9 | 5090 | Otherwise returns T. In contrast to maybe_constant_value we |
d2c63826 | 5091 | simplify only few operations on constant-expressions, and we don't |
5092 | try to simplify constexpressions. */ | |
5093 | ||
5094 | tree | |
5095 | fold_simple (tree t) | |
5096 | { | |
d2c63826 | 5097 | if (processing_template_decl) |
5098 | return t; | |
5099 | ||
6fe5fea9 | 5100 | tree r = fold_simple_1 (t); |
5101 | if (r) | |
5102 | return r; | |
d2c63826 | 5103 | |
6fe5fea9 | 5104 | return t; |
d2c63826 | 5105 | } |
5106 | ||
09b42213 | 5107 | /* If T is a constant expression, returns its reduced value. |
5108 | Otherwise, if T does not have TREE_CONSTANT set, returns T. | |
5109 | Otherwise, returns a version of T without TREE_CONSTANT. */ | |
5110 | ||
b601ad30 | 5111 | static GTY((deletable)) hash_map<tree, tree> *cv_cache; |
5112 | ||
5113 | tree | |
5114 | maybe_constant_value (tree t, tree decl) | |
09b42213 | 5115 | { |
5116 | tree r; | |
5117 | ||
7e1f8be4 | 5118 | if (!is_nondependent_constant_expression (t)) |
09b42213 | 5119 | { |
5120 | if (TREE_OVERFLOW_P (t)) | |
5121 | { | |
5122 | t = build_nop (TREE_TYPE (t), t); | |
5123 | TREE_CONSTANT (t) = false; | |
5124 | } | |
5125 | return t; | |
5126 | } | |
b601ad30 | 5127 | else if (CONSTANT_CLASS_P (t)) |
5128 | /* No caching or evaluation needed. */ | |
5129 | return t; | |
5130 | ||
5131 | if (cv_cache == NULL) | |
5132 | cv_cache = hash_map<tree, tree>::create_ggc (101); | |
5133 | if (tree *cached = cv_cache->get (t)) | |
5134 | return *cached; | |
09b42213 | 5135 | |
2055d27a | 5136 | r = cxx_eval_outermost_constant_expr (t, true, true, decl); |
5e8689fb | 5137 | gcc_checking_assert (r == t |
5138 | || CONVERT_EXPR_P (t) | |
5139 | || TREE_CODE (t) == VIEW_CONVERT_EXPR | |
5140 | || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) | |
5141 | || !cp_tree_equal (r, t)); | |
b601ad30 | 5142 | cv_cache->put (t, r); |
09b42213 | 5143 | return r; |
5144 | } | |
5145 | ||
2a655a4c | 5146 | /* Dispose of the whole CV_CACHE. */ |
5147 | ||
5148 | static void | |
5149 | clear_cv_cache (void) | |
5150 | { | |
5151 | if (cv_cache != NULL) | |
5152 | cv_cache->empty (); | |
5153 | } | |
5154 | ||
f553d9f8 | 5155 | /* Dispose of the whole CV_CACHE and FOLD_CACHE. */ |
a0c919f7 | 5156 | |
5157 | void | |
f553d9f8 | 5158 | clear_cv_and_fold_caches (void) |
a0c919f7 | 5159 | { |
2a655a4c | 5160 | clear_cv_cache (); |
f553d9f8 | 5161 | clear_fold_cache (); |
a0c919f7 | 5162 | } |
5163 | ||
21131a05 | 5164 | /* Like maybe_constant_value but first fully instantiate the argument. |
5165 | ||
5166 | Note: this is equivalent to instantiate_non_dependent_expr_sfinae | |
5167 | (t, tf_none) followed by maybe_constant_value but is more efficient, | |
5168 | because calls instantiation_dependent_expression_p and | |
5169 | potential_constant_expression at most once. */ | |
5170 | ||
5171 | tree | |
5172 | fold_non_dependent_expr (tree t) | |
5173 | { | |
5174 | if (t == NULL_TREE) | |
5175 | return NULL_TREE; | |
5176 | ||
5177 | /* If we're in a template, but T isn't value dependent, simplify | |
5178 | it. We're supposed to treat: | |
5179 | ||
5180 | template <typename T> void f(T[1 + 1]); | |
5181 | template <typename T> void f(T[2]); | |
5182 | ||
5183 | as two declarations of the same function, for example. */ | |
5184 | if (processing_template_decl) | |
5185 | { | |
7e1f8be4 | 5186 | if (is_nondependent_constant_expression (t)) |
21131a05 | 5187 | { |
c8a9f04c | 5188 | processing_template_decl_sentinel s; |
5189 | t = instantiate_non_dependent_expr_internal (t, tf_none); | |
21131a05 | 5190 | |
5191 | if (type_unknown_p (t) | |
5192 | || BRACE_ENCLOSED_INITIALIZER_P (t)) | |
5193 | { | |
5194 | if (TREE_OVERFLOW_P (t)) | |
5195 | { | |
5196 | t = build_nop (TREE_TYPE (t), t); | |
5197 | TREE_CONSTANT (t) = false; | |
5198 | } | |
5199 | return t; | |
5200 | } | |
5201 | ||
2055d27a | 5202 | tree r = cxx_eval_outermost_constant_expr (t, true, true, NULL_TREE); |
21131a05 | 5203 | /* cp_tree_equal looks through NOPs, so allow them. */ |
5e8689fb | 5204 | gcc_checking_assert (r == t |
5205 | || CONVERT_EXPR_P (t) | |
5206 | || TREE_CODE (t) == VIEW_CONVERT_EXPR | |
5207 | || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) | |
5208 | || !cp_tree_equal (r, t)); | |
21131a05 | 5209 | return r; |
5210 | } | |
5211 | else if (TREE_OVERFLOW_P (t)) | |
5212 | { | |
5213 | t = build_nop (TREE_TYPE (t), t); | |
5214 | TREE_CONSTANT (t) = false; | |
5215 | } | |
5216 | return t; | |
5217 | } | |
5218 | ||
5219 | return maybe_constant_value (t); | |
5220 | } | |
5221 | ||
09b42213 | 5222 | /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather |
5223 | than wrapped in a TARGET_EXPR. */ | |
5224 | ||
0c703e14 | 5225 | static tree |
5226 | maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant) | |
09b42213 | 5227 | { |
d2c63826 | 5228 | if (!t) |
5229 | return t; | |
09b42213 | 5230 | if (TREE_CODE (t) == EXPR_STMT) |
5231 | t = TREE_OPERAND (t, 0); | |
5232 | if (TREE_CODE (t) == CONVERT_EXPR | |
5233 | && VOID_TYPE_P (TREE_TYPE (t))) | |
5234 | t = TREE_OPERAND (t, 0); | |
9c96033c | 5235 | if (TREE_CODE (t) == INIT_EXPR) |
5236 | t = TREE_OPERAND (t, 1); | |
c6d97004 | 5237 | if (TREE_CODE (t) == TARGET_EXPR) |
5238 | t = TARGET_EXPR_INITIAL (t); | |
7e1f8be4 | 5239 | if (!is_nondependent_static_init_expression (t)) |
2055d27a | 5240 | /* Don't try to evaluate it. */; |
0c703e14 | 5241 | else if (CONSTANT_CLASS_P (t) && allow_non_constant) |
b601ad30 | 5242 | /* No evaluation needed. */; |
2055d27a | 5243 | else |
0c703e14 | 5244 | t = cxx_eval_outermost_constant_expr (t, allow_non_constant, false, decl); |
09b42213 | 5245 | if (TREE_CODE (t) == TARGET_EXPR) |
5246 | { | |
5247 | tree init = TARGET_EXPR_INITIAL (t); | |
5248 | if (TREE_CODE (init) == CONSTRUCTOR) | |
5249 | t = init; | |
5250 | } | |
5251 | return t; | |
5252 | } | |
5253 | ||
0c703e14 | 5254 | /* Wrapper for maybe_constant_init_1 which permits non constants. */ |
5255 | ||
5256 | tree | |
5257 | maybe_constant_init (tree t, tree decl) | |
5258 | { | |
5259 | return maybe_constant_init_1 (t, decl, true); | |
5260 | } | |
5261 | ||
5262 | /* Wrapper for maybe_constant_init_1 which does not permit non constants. */ | |
5263 | ||
5264 | tree | |
5265 | cxx_constant_init (tree t, tree decl) | |
5266 | { | |
5267 | return maybe_constant_init_1 (t, decl, false); | |
5268 | } | |
5269 | ||
09b42213 | 5270 | #if 0 |
5271 | /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */ | |
5272 | /* Return true if the object referred to by REF has automatic or thread | |
5273 | local storage. */ | |
5274 | ||
5275 | enum { ck_ok, ck_bad, ck_unknown }; | |
5276 | static int | |
5277 | check_automatic_or_tls (tree ref) | |
5278 | { | |
3754d046 | 5279 | machine_mode mode; |
81bc0f0f | 5280 | poly_int64 bitsize, bitpos; |
09b42213 | 5281 | tree offset; |
5282 | int volatilep = 0, unsignedp = 0; | |
5283 | tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset, | |
5284 | &mode, &unsignedp, &volatilep, false); | |
5285 | duration_kind dk; | |
5286 | ||
5287 | /* If there isn't a decl in the middle, we don't know the linkage here, | |
5288 | and this isn't a constant expression anyway. */ | |
5289 | if (!DECL_P (decl)) | |
5290 | return ck_unknown; | |
5291 | dk = decl_storage_duration (decl); | |
5292 | return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok; | |
5293 | } | |
5294 | #endif | |
5295 | ||
5296 | /* Return true if T denotes a potentially constant expression. Issue | |
5297 | diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true, | |
7e1f8be4 | 5298 | an lvalue-rvalue conversion is implied. If NOW is true, we want to |
5299 | consider the expression in the current context, independent of constexpr | |
5300 | substitution. | |
09b42213 | 5301 | |
5302 | C++0x [expr.const] used to say | |
5303 | ||
5304 | 6 An expression is a potential constant expression if it is | |
5305 | a constant expression where all occurrences of function | |
5306 | parameters are replaced by arbitrary constant expressions | |
5307 | of the appropriate type. | |
5308 | ||
5309 | 2 A conditional expression is a constant expression unless it | |
5310 | involves one of the following as a potentially evaluated | |
5311 | subexpression (3.2), but subexpressions of logical AND (5.14), | |
5312 | logical OR (5.15), and conditional (5.16) operations that are | |
5313 | not evaluated are not considered. */ | |
5314 | ||
5315 | static bool | |
7e1f8be4 | 5316 | potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, |
2055d27a | 5317 | tsubst_flags_t flags) |
09b42213 | 5318 | { |
7e1f8be4 | 5319 | #define RECUR(T,RV) \ |
5320 | potential_constant_expression_1 ((T), (RV), strict, now, flags) | |
5321 | ||
09b42213 | 5322 | enum { any = false, rval = true }; |
5323 | int i; | |
5324 | tree tmp; | |
5325 | ||
5326 | if (t == error_mark_node) | |
5327 | return false; | |
5328 | if (t == NULL_TREE) | |
5329 | return true; | |
d3a3cfb8 | 5330 | location_t loc = cp_expr_loc_or_loc (t, input_location); |
eade0940 | 5331 | if (TREE_THIS_VOLATILE (t) && !DECL_P (t)) |
09b42213 | 5332 | { |
5333 | if (flags & tf_error) | |
d6a08f5c | 5334 | error_at (loc, "expression %qE has side-effects", t); |
09b42213 | 5335 | return false; |
5336 | } | |
5337 | if (CONSTANT_CLASS_P (t)) | |
5338 | return true; | |
c7282bf2 | 5339 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED) |
5340 | && TREE_TYPE (t) == error_mark_node) | |
5341 | return false; | |
09b42213 | 5342 | |
5343 | switch (TREE_CODE (t)) | |
5344 | { | |
5345 | case FUNCTION_DECL: | |
5346 | case BASELINK: | |
5347 | case TEMPLATE_DECL: | |
5348 | case OVERLOAD: | |
5349 | case TEMPLATE_ID_EXPR: | |
5350 | case LABEL_DECL: | |
5351 | case LABEL_EXPR: | |
9c96033c | 5352 | case CASE_LABEL_EXPR: |
09b42213 | 5353 | case CONST_DECL: |
5354 | case SIZEOF_EXPR: | |
5355 | case ALIGNOF_EXPR: | |
5356 | case OFFSETOF_EXPR: | |
5357 | case NOEXCEPT_EXPR: | |
5358 | case TEMPLATE_PARM_INDEX: | |
5359 | case TRAIT_EXPR: | |
5360 | case IDENTIFIER_NODE: | |
5361 | case USERDEF_LITERAL: | |
5362 | /* We can see a FIELD_DECL in a pointer-to-member expression. */ | |
5363 | case FIELD_DECL: | |
d2c63826 | 5364 | case RESULT_DECL: |
09b42213 | 5365 | case USING_DECL: |
9c96033c | 5366 | case USING_STMT: |
cf72f34d | 5367 | case PLACEHOLDER_EXPR: |
9c96033c | 5368 | case BREAK_STMT: |
5369 | case CONTINUE_STMT: | |
56c12fd4 | 5370 | case REQUIRES_EXPR: |
33603066 | 5371 | case STATIC_ASSERT: |
90567983 | 5372 | case DEBUG_BEGIN_STMT: |
09b42213 | 5373 | return true; |
5374 | ||
7e1f8be4 | 5375 | case PARM_DECL: |
5376 | if (now) | |
5377 | { | |
5378 | if (flags & tf_error) | |
5379 | error ("%qE is not a constant expression", t); | |
5380 | return false; | |
5381 | } | |
5382 | return true; | |
5383 | ||
09b42213 | 5384 | case AGGR_INIT_EXPR: |
5385 | case CALL_EXPR: | |
5386 | /* -- an invocation of a function other than a constexpr function | |
5387 | or a constexpr constructor. */ | |
5388 | { | |
5389 | tree fun = get_function_named_in_call (t); | |
5390 | const int nargs = call_expr_nargs (t); | |
5391 | i = 0; | |
5392 | ||
9c96033c | 5393 | if (fun == NULL_TREE) |
5394 | { | |
732905bb | 5395 | /* Reset to allow the function to continue past the end |
5396 | of the block below. Otherwise return early. */ | |
5397 | bool bail = true; | |
5398 | ||
32cf7025 | 5399 | if (TREE_CODE (t) == CALL_EXPR |
5400 | && CALL_EXPR_FN (t) == NULL_TREE) | |
5401 | switch (CALL_EXPR_IFN (t)) | |
5402 | { | |
5403 | /* These should be ignored, they are optimized away from | |
5404 | constexpr functions. */ | |
5405 | case IFN_UBSAN_NULL: | |
5406 | case IFN_UBSAN_BOUNDS: | |
5407 | case IFN_UBSAN_VPTR: | |
3c77f69c | 5408 | case IFN_FALLTHROUGH: |
32cf7025 | 5409 | return true; |
732905bb | 5410 | |
5411 | case IFN_ADD_OVERFLOW: | |
5412 | case IFN_SUB_OVERFLOW: | |
5413 | case IFN_MUL_OVERFLOW: | |
6e1b2ffb | 5414 | case IFN_LAUNDER: |
732905bb | 5415 | bail = false; |
5416 | ||
32cf7025 | 5417 | default: |
5418 | break; | |
5419 | } | |
732905bb | 5420 | |
5421 | if (bail) | |
5422 | { | |
5423 | /* fold_call_expr can't do anything with IFN calls. */ | |
5424 | if (flags & tf_error) | |
d6a08f5c | 5425 | error_at (loc, "call to internal function %qE", t); |
732905bb | 5426 | return false; |
5427 | } | |
9c96033c | 5428 | } |
732905bb | 5429 | |
5430 | if (fun && is_overloaded_fn (fun)) | |
09b42213 | 5431 | { |
5432 | if (TREE_CODE (fun) == FUNCTION_DECL) | |
5433 | { | |
5434 | if (builtin_valid_in_constant_expr_p (fun)) | |
5435 | return true; | |
5436 | if (!DECL_DECLARED_CONSTEXPR_P (fun) | |
5437 | /* Allow any built-in function; if the expansion | |
5438 | isn't constant, we'll deal with that then. */ | |
5439 | && !is_builtin_fn (fun)) | |
5440 | { | |
5441 | if (flags & tf_error) | |
5442 | { | |
5967b28b | 5443 | error_at (loc, "call to non-%<constexpr%> function %qD", |
d6a08f5c | 5444 | fun); |
09b42213 | 5445 | explain_invalid_constexpr_fn (fun); |
5446 | } | |
5447 | return false; | |
5448 | } | |
5449 | /* A call to a non-static member function takes the address | |
5450 | of the object as the first argument. But in a constant | |
5451 | expression the address will be folded away, so look | |
5452 | through it now. */ | |
5453 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) | |
5454 | && !DECL_CONSTRUCTOR_P (fun)) | |
5455 | { | |
5456 | tree x = get_nth_callarg (t, 0); | |
5457 | if (is_this_parameter (x)) | |
cf72f34d | 5458 | return true; |
7e1f8be4 | 5459 | /* Don't require an immediately constant value, as |
5460 | constexpr substitution might not use the value. */ | |
5461 | bool sub_now = false; | |
5462 | if (!potential_constant_expression_1 (x, rval, strict, | |
5463 | sub_now, flags)) | |
09b42213 | 5464 | return false; |
5465 | i = 1; | |
5466 | } | |
5467 | } | |
5468 | else | |
5469 | { | |
2055d27a | 5470 | if (!RECUR (fun, true)) |
09b42213 | 5471 | return false; |
5472 | fun = get_first_fn (fun); | |
5473 | } | |
5474 | /* Skip initial arguments to base constructors. */ | |
5475 | if (DECL_BASE_CONSTRUCTOR_P (fun)) | |
5476 | i = num_artificial_parms_for (fun); | |
5477 | fun = DECL_ORIGIN (fun); | |
5478 | } | |
732905bb | 5479 | else if (fun) |
09b42213 | 5480 | { |
2055d27a | 5481 | if (RECUR (fun, rval)) |
09b42213 | 5482 | /* Might end up being a constant function pointer. */; |
5483 | else | |
5484 | return false; | |
5485 | } | |
5486 | for (; i < nargs; ++i) | |
5487 | { | |
5488 | tree x = get_nth_callarg (t, i); | |
b2742b58 | 5489 | /* In a template, reference arguments haven't been converted to |
5490 | REFERENCE_TYPE and we might not even know if the parameter | |
5491 | is a reference, so accept lvalue constants too. */ | |
5492 | bool rv = processing_template_decl ? any : rval; | |
7e1f8be4 | 5493 | /* Don't require an immediately constant value, as constexpr |
5494 | substitution might not use the value of the argument. */ | |
5495 | bool sub_now = false; | |
5496 | if (!potential_constant_expression_1 (x, rv, strict, | |
5497 | sub_now, flags)) | |
09b42213 | 5498 | return false; |
5499 | } | |
5500 | return true; | |
5501 | } | |
5502 | ||
5503 | case NON_LVALUE_EXPR: | |
5504 | /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to | |
5505 | -- an lvalue of integral type that refers to a non-volatile | |
5506 | const variable or static data member initialized with | |
5507 | constant expressions, or | |
5508 | ||
5509 | -- an lvalue of literal type that refers to non-volatile | |
5510 | object defined with constexpr, or that refers to a | |
5511 | sub-object of such an object; */ | |
2055d27a | 5512 | return RECUR (TREE_OPERAND (t, 0), rval); |
09b42213 | 5513 | |
5514 | case VAR_DECL: | |
9c8aeb66 | 5515 | if (DECL_HAS_VALUE_EXPR_P (t)) |
297de7bc | 5516 | { |
d0e2b7e7 | 5517 | if (now && is_normal_capture_proxy (t)) |
297de7bc | 5518 | { |
5519 | /* -- in a lambda-expression, a reference to this or to a | |
5520 | variable with automatic storage duration defined outside that | |
5521 | lambda-expression, where the reference would be an | |
5522 | odr-use. */ | |
5523 | if (flags & tf_error) | |
5524 | { | |
5525 | tree cap = DECL_CAPTURED_VARIABLE (t); | |
5526 | error ("lambda capture of %qE is not a constant expression", | |
5527 | cap); | |
5528 | if (!want_rval && decl_constant_var_p (cap)) | |
5529 | inform (input_location, "because it is used as a glvalue"); | |
5530 | } | |
5531 | return false; | |
5532 | } | |
5533 | return RECUR (DECL_VALUE_EXPR (t), rval); | |
5534 | } | |
2055d27a | 5535 | if (want_rval |
33603066 | 5536 | && !var_in_maybe_constexpr_fn (t) |
c7282bf2 | 5537 | && !type_dependent_expression_p (t) |
8d6486e1 | 5538 | && !decl_maybe_constant_var_p (t) |
2055d27a | 5539 | && (strict |
5540 | || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t)) | |
8d6486e1 | 5541 | || (DECL_INITIAL (t) |
5542 | && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))) | |
c7282bf2 | 5543 | && COMPLETE_TYPE_P (TREE_TYPE (t)) |
5544 | && !is_really_empty_class (TREE_TYPE (t))) | |
09b42213 | 5545 | { |
5546 | if (flags & tf_error) | |
5547 | non_const_var_error (t); | |
5548 | return false; | |
5549 | } | |
5550 | return true; | |
5551 | ||
5552 | case NOP_EXPR: | |
5553 | case CONVERT_EXPR: | |
5554 | case VIEW_CONVERT_EXPR: | |
5555 | /* -- a reinterpret_cast. FIXME not implemented, and this rule | |
5556 | may change to something more specific to type-punning (DR 1312). */ | |
5557 | { | |
5558 | tree from = TREE_OPERAND (t, 0); | |
d03fa520 | 5559 | if (INDIRECT_TYPE_P (TREE_TYPE (t)) |
09b42213 | 5560 | && TREE_CODE (from) == INTEGER_CST |
5561 | && !integer_zerop (from)) | |
5562 | { | |
5563 | if (flags & tf_error) | |
d6a08f5c | 5564 | error_at (loc, "reinterpret_cast from integer to pointer"); |
09b42213 | 5565 | return false; |
5566 | } | |
2055d27a | 5567 | return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR)); |
09b42213 | 5568 | } |
5569 | ||
cd45162d | 5570 | case ADDRESSOF_EXPR: |
5571 | /* This is like ADDR_EXPR, except it won't form pointer-to-member. */ | |
5572 | t = TREE_OPERAND (t, 0); | |
5573 | goto handle_addr_expr; | |
5574 | ||
09b42213 | 5575 | case ADDR_EXPR: |
5576 | /* -- a unary operator & that is applied to an lvalue that | |
5577 | designates an object with thread or automatic storage | |
5578 | duration; */ | |
5579 | t = TREE_OPERAND (t, 0); | |
5580 | ||
5581 | if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t)) | |
5582 | /* A pointer-to-member constant. */ | |
5583 | return true; | |
5584 | ||
cd45162d | 5585 | handle_addr_expr: |
09b42213 | 5586 | #if 0 |
5587 | /* FIXME adjust when issue 1197 is fully resolved. For now don't do | |
5588 | any checking here, as we might dereference the pointer later. If | |
5589 | we remove this code, also remove check_automatic_or_tls. */ | |
5590 | i = check_automatic_or_tls (t); | |
5591 | if (i == ck_ok) | |
5592 | return true; | |
5593 | if (i == ck_bad) | |
5594 | { | |
5595 | if (flags & tf_error) | |
5596 | error ("address-of an object %qE with thread local or " | |
5597 | "automatic storage is not a constant expression", t); | |
5598 | return false; | |
5599 | } | |
5600 | #endif | |
2055d27a | 5601 | return RECUR (t, any); |
09b42213 | 5602 | |
20b34a56 | 5603 | case REALPART_EXPR: |
5604 | case IMAGPART_EXPR: | |
09b42213 | 5605 | case COMPONENT_REF: |
5606 | case BIT_FIELD_REF: | |
5607 | case ARROW_EXPR: | |
5608 | case OFFSET_REF: | |
5609 | /* -- a class member access unless its postfix-expression is | |
5610 | of literal type or of pointer to literal type. */ | |
5611 | /* This test would be redundant, as it follows from the | |
5612 | postfix-expression being a potential constant expression. */ | |
eade0940 | 5613 | if (type_unknown_p (t)) |
5614 | return true; | |
2055d27a | 5615 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
09b42213 | 5616 | |
5617 | case EXPR_PACK_EXPANSION: | |
2055d27a | 5618 | return RECUR (PACK_EXPANSION_PATTERN (t), want_rval); |
09b42213 | 5619 | |
5620 | case INDIRECT_REF: | |
5621 | { | |
5622 | tree x = TREE_OPERAND (t, 0); | |
5623 | STRIP_NOPS (x); | |
997bbf70 | 5624 | if (is_this_parameter (x) && !is_capture_proxy (x)) |
09b42213 | 5625 | { |
b7e4a558 | 5626 | if (!var_in_maybe_constexpr_fn (x)) |
09b42213 | 5627 | { |
5628 | if (flags & tf_error) | |
d6a08f5c | 5629 | error_at (loc, "use of %<this%> in a constant expression"); |
09b42213 | 5630 | return false; |
5631 | } | |
09b42213 | 5632 | return true; |
5633 | } | |
2055d27a | 5634 | return RECUR (x, rval); |
09b42213 | 5635 | } |
5636 | ||
9c96033c | 5637 | case STATEMENT_LIST: |
5638 | { | |
5639 | tree_stmt_iterator i; | |
5640 | for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) | |
5641 | { | |
2055d27a | 5642 | if (!RECUR (tsi_stmt (i), any)) |
9c96033c | 5643 | return false; |
5644 | } | |
5645 | return true; | |
5646 | } | |
5647 | break; | |
5648 | ||
5649 | case MODIFY_EXPR: | |
5650 | if (cxx_dialect < cxx14) | |
5651 | goto fail; | |
2055d27a | 5652 | if (!RECUR (TREE_OPERAND (t, 0), any)) |
9c96033c | 5653 | return false; |
2055d27a | 5654 | if (!RECUR (TREE_OPERAND (t, 1), rval)) |
9c96033c | 5655 | return false; |
5656 | return true; | |
5657 | ||
5658 | case MODOP_EXPR: | |
5659 | if (cxx_dialect < cxx14) | |
5660 | goto fail; | |
2055d27a | 5661 | if (!RECUR (TREE_OPERAND (t, 0), rval)) |
9c96033c | 5662 | return false; |
2055d27a | 5663 | if (!RECUR (TREE_OPERAND (t, 2), rval)) |
9c96033c | 5664 | return false; |
5665 | return true; | |
5666 | ||
9c96033c | 5667 | case DO_STMT: |
2055d27a | 5668 | if (!RECUR (DO_COND (t), rval)) |
9c96033c | 5669 | return false; |
2055d27a | 5670 | if (!RECUR (DO_BODY (t), any)) |
9c96033c | 5671 | return false; |
5672 | return true; | |
5673 | ||
5674 | case FOR_STMT: | |
2055d27a | 5675 | if (!RECUR (FOR_INIT_STMT (t), any)) |
9c96033c | 5676 | return false; |
2055d27a | 5677 | if (!RECUR (FOR_COND (t), rval)) |
9c96033c | 5678 | return false; |
2055d27a | 5679 | if (!RECUR (FOR_EXPR (t), any)) |
9c96033c | 5680 | return false; |
2055d27a | 5681 | if (!RECUR (FOR_BODY (t), any)) |
9c96033c | 5682 | return false; |
5683 | return true; | |
5684 | ||
33603066 | 5685 | case RANGE_FOR_STMT: |
5686 | if (!RECUR (RANGE_FOR_EXPR (t), any)) | |
5687 | return false; | |
5688 | if (!RECUR (RANGE_FOR_BODY (t), any)) | |
5689 | return false; | |
5690 | return true; | |
5691 | ||
9c96033c | 5692 | case WHILE_STMT: |
2055d27a | 5693 | if (!RECUR (WHILE_COND (t), rval)) |
9c96033c | 5694 | return false; |
2055d27a | 5695 | if (!RECUR (WHILE_BODY (t), any)) |
9c96033c | 5696 | return false; |
5697 | return true; | |
5698 | ||
5699 | case SWITCH_STMT: | |
2055d27a | 5700 | if (!RECUR (SWITCH_STMT_COND (t), rval)) |
9c96033c | 5701 | return false; |
748c426a | 5702 | /* FIXME we don't check SWITCH_STMT_BODY currently, because even |
5703 | unreachable labels would be checked. */ | |
9c96033c | 5704 | return true; |
5705 | ||
da7981e0 | 5706 | case STMT_EXPR: |
2055d27a | 5707 | return RECUR (STMT_EXPR_STMT (t), rval); |
da7981e0 | 5708 | |
09b42213 | 5709 | case LAMBDA_EXPR: |
27db1f50 | 5710 | if (cxx_dialect >= cxx17) |
5711 | /* In C++17 lambdas can be constexpr, don't give up yet. */ | |
5712 | return true; | |
5713 | else if (flags & tf_error) | |
5714 | error_at (loc, "lambda-expression is not a constant expression " | |
5715 | "before C++17"); | |
5716 | return false; | |
5717 | ||
09b42213 | 5718 | case DYNAMIC_CAST_EXPR: |
5719 | case PSEUDO_DTOR_EXPR: | |
09b42213 | 5720 | case NEW_EXPR: |
5721 | case VEC_NEW_EXPR: | |
5722 | case DELETE_EXPR: | |
5723 | case VEC_DELETE_EXPR: | |
5724 | case THROW_EXPR: | |
d6a08f5c | 5725 | case OMP_PARALLEL: |
5726 | case OMP_TASK: | |
5727 | case OMP_FOR: | |
83728168 | 5728 | case OMP_SIMD: |
d6a08f5c | 5729 | case OMP_DISTRIBUTE: |
5730 | case OMP_TASKLOOP: | |
5731 | case OMP_TEAMS: | |
5732 | case OMP_TARGET_DATA: | |
5733 | case OMP_TARGET: | |
5734 | case OMP_SECTIONS: | |
5735 | case OMP_ORDERED: | |
5736 | case OMP_CRITICAL: | |
5737 | case OMP_SINGLE: | |
5738 | case OMP_SECTION: | |
5739 | case OMP_MASTER: | |
5740 | case OMP_TASKGROUP: | |
5741 | case OMP_TARGET_UPDATE: | |
5742 | case OMP_TARGET_ENTER_DATA: | |
5743 | case OMP_TARGET_EXIT_DATA: | |
09b42213 | 5744 | case OMP_ATOMIC: |
5745 | case OMP_ATOMIC_READ: | |
5746 | case OMP_ATOMIC_CAPTURE_OLD: | |
5747 | case OMP_ATOMIC_CAPTURE_NEW: | |
d6a08f5c | 5748 | case OACC_PARALLEL: |
5749 | case OACC_KERNELS: | |
5750 | case OACC_DATA: | |
5751 | case OACC_HOST_DATA: | |
5752 | case OACC_LOOP: | |
5753 | case OACC_CACHE: | |
5754 | case OACC_DECLARE: | |
5755 | case OACC_ENTER_DATA: | |
5756 | case OACC_EXIT_DATA: | |
5757 | case OACC_UPDATE: | |
09b42213 | 5758 | /* GCC internal stuff. */ |
5759 | case VA_ARG_EXPR: | |
5760 | case OBJ_TYPE_REF: | |
09b42213 | 5761 | case TRANSACTION_EXPR: |
9c96033c | 5762 | case ASM_EXPR: |
9abecca2 | 5763 | case AT_ENCODE_EXPR: |
9c96033c | 5764 | fail: |
09b42213 | 5765 | if (flags & tf_error) |
d6a08f5c | 5766 | error_at (loc, "expression %qE is not a constant expression", t); |
09b42213 | 5767 | return false; |
5768 | ||
5769 | case TYPEID_EXPR: | |
5770 | /* -- a typeid expression whose operand is of polymorphic | |
5771 | class type; */ | |
5772 | { | |
5773 | tree e = TREE_OPERAND (t, 0); | |
5774 | if (!TYPE_P (e) && !type_dependent_expression_p (e) | |
5775 | && TYPE_POLYMORPHIC_P (TREE_TYPE (e))) | |
5776 | { | |
5777 | if (flags & tf_error) | |
d6a08f5c | 5778 | error_at (loc, "typeid-expression is not a constant expression " |
5779 | "because %qE is of polymorphic type", e); | |
09b42213 | 5780 | return false; |
5781 | } | |
5782 | return true; | |
5783 | } | |
5784 | ||
57e83b58 | 5785 | case POINTER_DIFF_EXPR: |
09b42213 | 5786 | case MINUS_EXPR: |
09b42213 | 5787 | want_rval = true; |
5788 | goto binary; | |
5789 | ||
5790 | case LT_EXPR: | |
5791 | case LE_EXPR: | |
5792 | case GT_EXPR: | |
5793 | case GE_EXPR: | |
5794 | case EQ_EXPR: | |
5795 | case NE_EXPR: | |
09b42213 | 5796 | want_rval = true; |
5797 | goto binary; | |
5798 | ||
9c96033c | 5799 | case PREINCREMENT_EXPR: |
5800 | case POSTINCREMENT_EXPR: | |
5801 | case PREDECREMENT_EXPR: | |
5802 | case POSTDECREMENT_EXPR: | |
5803 | if (cxx_dialect < cxx14) | |
5804 | goto fail; | |
5805 | goto unary; | |
5806 | ||
09b42213 | 5807 | case BIT_NOT_EXPR: |
5808 | /* A destructor. */ | |
5809 | if (TYPE_P (TREE_OPERAND (t, 0))) | |
5810 | return true; | |
e3533433 | 5811 | /* fall through. */ |
09b42213 | 5812 | |
09b42213 | 5813 | case CONJ_EXPR: |
5814 | case SAVE_EXPR: | |
5815 | case FIX_TRUNC_EXPR: | |
5816 | case FLOAT_EXPR: | |
5817 | case NEGATE_EXPR: | |
5818 | case ABS_EXPR: | |
1c67942e | 5819 | case ABSU_EXPR: |
09b42213 | 5820 | case TRUTH_NOT_EXPR: |
5821 | case FIXED_CONVERT_EXPR: | |
5822 | case UNARY_PLUS_EXPR: | |
6fdf70f5 | 5823 | case UNARY_LEFT_FOLD_EXPR: |
5824 | case UNARY_RIGHT_FOLD_EXPR: | |
9c96033c | 5825 | unary: |
2055d27a | 5826 | return RECUR (TREE_OPERAND (t, 0), rval); |
09b42213 | 5827 | |
5828 | case CAST_EXPR: | |
5829 | case CONST_CAST_EXPR: | |
5830 | case STATIC_CAST_EXPR: | |
5831 | case REINTERPRET_CAST_EXPR: | |
5832 | case IMPLICIT_CONV_EXPR: | |
5833 | if (cxx_dialect < cxx11 | |
5834 | && !dependent_type_p (TREE_TYPE (t)) | |
5835 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))) | |
5836 | /* In C++98, a conversion to non-integral type can't be part of a | |
5837 | constant expression. */ | |
5838 | { | |
5839 | if (flags & tf_error) | |
d6a08f5c | 5840 | error_at (loc, |
5841 | "cast to non-integral type %qT in a constant expression", | |
5842 | TREE_TYPE (t)); | |
09b42213 | 5843 | return false; |
5844 | } | |
e0ccd480 | 5845 | /* This might be a conversion from a class to a (potentially) literal |
5846 | type. Let's consider it potentially constant since the conversion | |
5847 | might be a constexpr user-defined conversion. */ | |
5848 | else if (cxx_dialect >= cxx11 | |
5849 | && (dependent_type_p (TREE_TYPE (t)) | |
5850 | || !COMPLETE_TYPE_P (TREE_TYPE (t)) | |
5851 | || literal_type_p (TREE_TYPE (t))) | |
5852 | && TREE_OPERAND (t, 0)) | |
5853 | { | |
5854 | tree type = TREE_TYPE (TREE_OPERAND (t, 0)); | |
5855 | /* If this is a dependent type, it could end up being a class | |
5856 | with conversions. */ | |
5857 | if (type == NULL_TREE || WILDCARD_TYPE_P (type)) | |
5858 | return true; | |
5859 | /* Or a non-dependent class which has conversions. */ | |
5860 | else if (CLASS_TYPE_P (type) | |
5861 | && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type))) | |
5862 | return true; | |
5863 | } | |
09b42213 | 5864 | |
2055d27a | 5865 | return (RECUR (TREE_OPERAND (t, 0), |
90ad495b | 5866 | !TYPE_REF_P (TREE_TYPE (t)))); |
09b42213 | 5867 | |
9c96033c | 5868 | case BIND_EXPR: |
2055d27a | 5869 | return RECUR (BIND_EXPR_BODY (t), want_rval); |
9c96033c | 5870 | |
9c96033c | 5871 | case CLEANUP_POINT_EXPR: |
5872 | case MUST_NOT_THROW_EXPR: | |
5873 | case TRY_CATCH_EXPR: | |
7f67d68c | 5874 | case TRY_BLOCK: |
9c96033c | 5875 | case EH_SPEC_BLOCK: |
5876 | case EXPR_STMT: | |
09b42213 | 5877 | case PAREN_EXPR: |
5878 | case NON_DEPENDENT_EXPR: | |
5879 | /* For convenience. */ | |
5880 | case RETURN_EXPR: | |
d1980671 | 5881 | case LOOP_EXPR: |
5882 | case EXIT_EXPR: | |
2055d27a | 5883 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
09b42213 | 5884 | |
33603066 | 5885 | case DECL_EXPR: |
5886 | tmp = DECL_EXPR_DECL (t); | |
5887 | if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)) | |
5888 | { | |
5889 | if (TREE_STATIC (tmp)) | |
5890 | { | |
5891 | if (flags & tf_error) | |
5892 | error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared " | |
8d6486e1 | 5893 | "%<static%> in %<constexpr%> context", tmp); |
33603066 | 5894 | return false; |
5895 | } | |
5896 | else if (CP_DECL_THREAD_LOCAL_P (tmp)) | |
5897 | { | |
5898 | if (flags & tf_error) | |
5899 | error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared " | |
8d6486e1 | 5900 | "%<thread_local%> in %<constexpr%> context", tmp); |
33603066 | 5901 | return false; |
5902 | } | |
282ea908 | 5903 | else if (!check_for_uninitialized_const_var |
5904 | (tmp, /*constexpr_context_p=*/true, flags)) | |
5905 | return false; | |
33603066 | 5906 | } |
5907 | return RECUR (tmp, want_rval); | |
5908 | ||
7f67d68c | 5909 | case TRY_FINALLY_EXPR: |
5910 | return (RECUR (TREE_OPERAND (t, 0), want_rval) | |
5911 | && RECUR (TREE_OPERAND (t, 1), any)); | |
5912 | ||
09b42213 | 5913 | case SCOPE_REF: |
2055d27a | 5914 | return RECUR (TREE_OPERAND (t, 1), want_rval); |
09b42213 | 5915 | |
5916 | case TARGET_EXPR: | |
07d8961c | 5917 | if (!TARGET_EXPR_DIRECT_INIT_P (t) |
5918 | && !literal_type_p (TREE_TYPE (t))) | |
09b42213 | 5919 | { |
5920 | if (flags & tf_error) | |
5921 | { | |
d6a08f5c | 5922 | error_at (loc, "temporary of non-literal type %qT in a " |
5923 | "constant expression", TREE_TYPE (t)); | |
09b42213 | 5924 | explain_non_literal_class (TREE_TYPE (t)); |
5925 | } | |
5926 | return false; | |
5927 | } | |
e3533433 | 5928 | /* FALLTHRU */ |
09b42213 | 5929 | case INIT_EXPR: |
2055d27a | 5930 | return RECUR (TREE_OPERAND (t, 1), rval); |
09b42213 | 5931 | |
5932 | case CONSTRUCTOR: | |
5933 | { | |
5934 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); | |
5935 | constructor_elt *ce; | |
5936 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
2055d27a | 5937 | if (!RECUR (ce->value, want_rval)) |
09b42213 | 5938 | return false; |
5939 | return true; | |
5940 | } | |
5941 | ||
5942 | case TREE_LIST: | |
5943 | { | |
5944 | gcc_assert (TREE_PURPOSE (t) == NULL_TREE | |
5945 | || DECL_P (TREE_PURPOSE (t))); | |
2055d27a | 5946 | if (!RECUR (TREE_VALUE (t), want_rval)) |
09b42213 | 5947 | return false; |
5948 | if (TREE_CHAIN (t) == NULL_TREE) | |
5949 | return true; | |
2055d27a | 5950 | return RECUR (TREE_CHAIN (t), want_rval); |
09b42213 | 5951 | } |
5952 | ||
5953 | case TRUNC_DIV_EXPR: | |
5954 | case CEIL_DIV_EXPR: | |
5955 | case FLOOR_DIV_EXPR: | |
5956 | case ROUND_DIV_EXPR: | |
5957 | case TRUNC_MOD_EXPR: | |
5958 | case CEIL_MOD_EXPR: | |
5959 | case ROUND_MOD_EXPR: | |
5960 | { | |
5961 | tree denom = TREE_OPERAND (t, 1); | |
2055d27a | 5962 | if (!RECUR (denom, rval)) |
09b42213 | 5963 | return false; |
5964 | /* We can't call cxx_eval_outermost_constant_expr on an expression | |
21131a05 | 5965 | that hasn't been through instantiate_non_dependent_expr yet. */ |
09b42213 | 5966 | if (!processing_template_decl) |
5967 | denom = cxx_eval_outermost_constant_expr (denom, true); | |
5968 | if (integer_zerop (denom)) | |
5969 | { | |
5970 | if (flags & tf_error) | |
ea034e2c | 5971 | error ("division by zero is not a constant expression"); |
09b42213 | 5972 | return false; |
5973 | } | |
5974 | else | |
5975 | { | |
5976 | want_rval = true; | |
2055d27a | 5977 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
09b42213 | 5978 | } |
5979 | } | |
5980 | ||
5981 | case COMPOUND_EXPR: | |
5982 | { | |
5983 | /* check_return_expr sometimes wraps a TARGET_EXPR in a | |
5984 | COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR | |
5985 | introduced by build_call_a. */ | |
5986 | tree op0 = TREE_OPERAND (t, 0); | |
5987 | tree op1 = TREE_OPERAND (t, 1); | |
5988 | STRIP_NOPS (op1); | |
5989 | if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) | |
5990 | || TREE_CODE (op1) == EMPTY_CLASS_EXPR) | |
2055d27a | 5991 | return RECUR (op0, want_rval); |
09b42213 | 5992 | else |
5993 | goto binary; | |
5994 | } | |
5995 | ||
5996 | /* If the first operand is the non-short-circuit constant, look at | |
5997 | the second operand; otherwise we only care about the first one for | |
5998 | potentiality. */ | |
5999 | case TRUTH_AND_EXPR: | |
6000 | case TRUTH_ANDIF_EXPR: | |
6001 | tmp = boolean_true_node; | |
6002 | goto truth; | |
6003 | case TRUTH_OR_EXPR: | |
6004 | case TRUTH_ORIF_EXPR: | |
6005 | tmp = boolean_false_node; | |
6006 | truth: | |
6007 | { | |
6008 | tree op = TREE_OPERAND (t, 0); | |
2055d27a | 6009 | if (!RECUR (op, rval)) |
09b42213 | 6010 | return false; |
6011 | if (!processing_template_decl) | |
6012 | op = cxx_eval_outermost_constant_expr (op, true); | |
6013 | if (tree_int_cst_equal (op, tmp)) | |
2055d27a | 6014 | return RECUR (TREE_OPERAND (t, 1), rval); |
09b42213 | 6015 | else |
6016 | return true; | |
6017 | } | |
6018 | ||
6019 | case PLUS_EXPR: | |
6020 | case MULT_EXPR: | |
6021 | case POINTER_PLUS_EXPR: | |
6022 | case RDIV_EXPR: | |
6023 | case EXACT_DIV_EXPR: | |
6024 | case MIN_EXPR: | |
6025 | case MAX_EXPR: | |
6026 | case LSHIFT_EXPR: | |
6027 | case RSHIFT_EXPR: | |
6028 | case LROTATE_EXPR: | |
6029 | case RROTATE_EXPR: | |
6030 | case BIT_IOR_EXPR: | |
6031 | case BIT_XOR_EXPR: | |
6032 | case BIT_AND_EXPR: | |
6033 | case TRUTH_XOR_EXPR: | |
6034 | case UNORDERED_EXPR: | |
6035 | case ORDERED_EXPR: | |
6036 | case UNLT_EXPR: | |
6037 | case UNLE_EXPR: | |
6038 | case UNGT_EXPR: | |
6039 | case UNGE_EXPR: | |
6040 | case UNEQ_EXPR: | |
6041 | case LTGT_EXPR: | |
6042 | case RANGE_EXPR: | |
6043 | case COMPLEX_EXPR: | |
6044 | want_rval = true; | |
6045 | /* Fall through. */ | |
6046 | case ARRAY_REF: | |
6047 | case ARRAY_RANGE_REF: | |
6048 | case MEMBER_REF: | |
6049 | case DOTSTAR_EXPR: | |
f0abe78f | 6050 | case MEM_REF: |
6fdf70f5 | 6051 | case BINARY_LEFT_FOLD_EXPR: |
6052 | case BINARY_RIGHT_FOLD_EXPR: | |
09b42213 | 6053 | binary: |
6054 | for (i = 0; i < 2; ++i) | |
2055d27a | 6055 | if (!RECUR (TREE_OPERAND (t, i), want_rval)) |
09b42213 | 6056 | return false; |
6057 | return true; | |
6058 | ||
09b42213 | 6059 | case VEC_PERM_EXPR: |
6060 | for (i = 0; i < 3; ++i) | |
2055d27a | 6061 | if (!RECUR (TREE_OPERAND (t, i), true)) |
09b42213 | 6062 | return false; |
6063 | return true; | |
6064 | ||
6065 | case COND_EXPR: | |
b0fe8b95 | 6066 | if (COND_EXPR_IS_VEC_DELETE (t)) |
6067 | { | |
6068 | if (flags & tf_error) | |
d6a08f5c | 6069 | error_at (loc, "%<delete[]%> is not a constant expression"); |
b0fe8b95 | 6070 | return false; |
6071 | } | |
6072 | /* Fall through. */ | |
6073 | case IF_STMT: | |
09b42213 | 6074 | case VEC_COND_EXPR: |
6075 | /* If the condition is a known constant, we know which of the legs we | |
6076 | care about; otherwise we only require that the condition and | |
6077 | either of the legs be potentially constant. */ | |
6078 | tmp = TREE_OPERAND (t, 0); | |
2055d27a | 6079 | if (!RECUR (tmp, rval)) |
09b42213 | 6080 | return false; |
6081 | if (!processing_template_decl) | |
6082 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
6083 | if (integer_zerop (tmp)) | |
2055d27a | 6084 | return RECUR (TREE_OPERAND (t, 2), want_rval); |
09b42213 | 6085 | else if (TREE_CODE (tmp) == INTEGER_CST) |
2055d27a | 6086 | return RECUR (TREE_OPERAND (t, 1), want_rval); |
09b42213 | 6087 | for (i = 1; i < 3; ++i) |
6088 | if (potential_constant_expression_1 (TREE_OPERAND (t, i), | |
7e1f8be4 | 6089 | want_rval, strict, now, tf_none)) |
09b42213 | 6090 | return true; |
6091 | if (flags & tf_error) | |
d6a08f5c | 6092 | error_at (loc, "expression %qE is not a constant expression", t); |
09b42213 | 6093 | return false; |
6094 | ||
6095 | case VEC_INIT_EXPR: | |
6096 | if (VEC_INIT_EXPR_IS_CONSTEXPR (t)) | |
6097 | return true; | |
6098 | if (flags & tf_error) | |
6099 | { | |
d6a08f5c | 6100 | error_at (loc, "non-constant array initialization"); |
09b42213 | 6101 | diagnose_non_constexpr_vec_init (t); |
6102 | } | |
6103 | return false; | |
6104 | ||
db1ae94f | 6105 | case TYPE_DECL: |
6106 | case TAG_DEFN: | |
6107 | /* We can see these in statement-expressions. */ | |
6108 | return true; | |
6109 | ||
fd130325 | 6110 | case CLEANUP_STMT: |
d2c63826 | 6111 | case EMPTY_CLASS_EXPR: |
38ef3642 | 6112 | case PREDICT_EXPR: |
d2c63826 | 6113 | return false; |
6114 | ||
d1980671 | 6115 | case GOTO_EXPR: |
6116 | { | |
6117 | tree *target = &TREE_OPERAND (t, 0); | |
d40a1eac | 6118 | /* Gotos representing break and continue are OK. */ |
6119 | if (breaks (target) || continues (target)) | |
6120 | return true; | |
6121 | if (flags & tf_error) | |
d6a08f5c | 6122 | error_at (loc, "%<goto%> is not a constant expression"); |
d40a1eac | 6123 | return false; |
d1980671 | 6124 | } |
6125 | ||
24c6ee98 | 6126 | case ANNOTATE_EXPR: |
24c6ee98 | 6127 | return RECUR (TREE_OPERAND (t, 0), rval); |
6128 | ||
09b42213 | 6129 | default: |
6130 | if (objc_is_property_ref (t)) | |
6131 | return false; | |
6132 | ||
6133 | sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t))); | |
d40a1eac | 6134 | gcc_unreachable (); |
09b42213 | 6135 | return false; |
6136 | } | |
2055d27a | 6137 | #undef RECUR |
09b42213 | 6138 | } |
6139 | ||
6140 | /* The main entry point to the above. */ | |
6141 | ||
6142 | bool | |
6143 | potential_constant_expression (tree t) | |
6144 | { | |
7e1f8be4 | 6145 | return potential_constant_expression_1 (t, false, true, false, tf_none); |
09b42213 | 6146 | } |
6147 | ||
6148 | /* As above, but require a constant rvalue. */ | |
6149 | ||
6150 | bool | |
6151 | potential_rvalue_constant_expression (tree t) | |
6152 | { | |
7e1f8be4 | 6153 | return potential_constant_expression_1 (t, true, true, false, tf_none); |
09b42213 | 6154 | } |
6155 | ||
6156 | /* Like above, but complain about non-constant expressions. */ | |
6157 | ||
6158 | bool | |
6159 | require_potential_constant_expression (tree t) | |
6160 | { | |
786721dc | 6161 | return potential_constant_expression_1 (t, false, true, false, |
6162 | tf_warning_or_error); | |
09b42213 | 6163 | } |
6164 | ||
6165 | /* Cross product of the above. */ | |
6166 | ||
6167 | bool | |
6168 | require_potential_rvalue_constant_expression (tree t) | |
6169 | { | |
786721dc | 6170 | return potential_constant_expression_1 (t, true, true, false, |
6171 | tf_warning_or_error); | |
6172 | } | |
6173 | ||
6174 | /* Like above, but don't consider PARM_DECL a potential_constant_expression. */ | |
6175 | ||
6176 | bool | |
6177 | require_rvalue_constant_expression (tree t) | |
6178 | { | |
6179 | return potential_constant_expression_1 (t, true, true, true, | |
6180 | tf_warning_or_error); | |
7e1f8be4 | 6181 | } |
6182 | ||
6183 | /* Like potential_constant_expression, but don't consider possible constexpr | |
6184 | substitution of the current function. That is, PARM_DECL qualifies under | |
6185 | potential_constant_expression, but not here. | |
6186 | ||
6187 | This is basically what you can check when any actual constant values might | |
6188 | be value-dependent. */ | |
6189 | ||
6190 | bool | |
6191 | is_constant_expression (tree t) | |
6192 | { | |
6193 | return potential_constant_expression_1 (t, false, true, true, tf_none); | |
6194 | } | |
6195 | ||
6196 | /* Like above, but complain about non-constant expressions. */ | |
6197 | ||
6198 | bool | |
6199 | require_constant_expression (tree t) | |
6200 | { | |
6201 | return potential_constant_expression_1 (t, false, true, true, | |
6202 | tf_warning_or_error); | |
6203 | } | |
6204 | ||
6205 | /* Like is_constant_expression, but allow const variables that are not allowed | |
6206 | under constexpr rules. */ | |
6207 | ||
6208 | bool | |
6209 | is_static_init_expression (tree t) | |
6210 | { | |
6211 | return potential_constant_expression_1 (t, false, false, true, tf_none); | |
09b42213 | 6212 | } |
6213 | ||
29cf24ec | 6214 | /* Returns true if T is a potential constant expression that is not |
6215 | instantiation-dependent, and therefore a candidate for constant folding even | |
6216 | in a template. */ | |
6217 | ||
6218 | bool | |
7e1f8be4 | 6219 | is_nondependent_constant_expression (tree t) |
29cf24ec | 6220 | { |
6221 | return (!type_unknown_p (t) | |
6222 | && !BRACE_ENCLOSED_INITIALIZER_P (t) | |
7e1f8be4 | 6223 | && is_constant_expression (t) |
29cf24ec | 6224 | && !instantiation_dependent_expression_p (t)); |
6225 | } | |
6226 | ||
6227 | /* Returns true if T is a potential static initializer expression that is not | |
6228 | instantiation-dependent. */ | |
6229 | ||
6230 | bool | |
7e1f8be4 | 6231 | is_nondependent_static_init_expression (tree t) |
29cf24ec | 6232 | { |
6233 | return (!type_unknown_p (t) | |
6234 | && !BRACE_ENCLOSED_INITIALIZER_P (t) | |
7e1f8be4 | 6235 | && is_static_init_expression (t) |
29cf24ec | 6236 | && !instantiation_dependent_expression_p (t)); |
6237 | } | |
6238 | ||
a050099a | 6239 | /* Finalize constexpr processing after parsing. */ |
6240 | ||
6241 | void | |
6242 | fini_constexpr (void) | |
6243 | { | |
6244 | /* The contexpr call and fundef copies tables are no longer needed. */ | |
6245 | constexpr_call_table = NULL; | |
6246 | fundef_copies_table = NULL; | |
6247 | } | |
6248 | ||
09b42213 | 6249 | #include "gt-cp-constexpr.h" |