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