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