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