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