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