]>
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 | ||
a5544970 | 5 | Copyright (C) 1998-2019 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" | |
2d76680f | 26 | #include "cp-tree.h" |
2adfab87 | 27 | #include "varasm.h" |
2d76680f PC |
28 | #include "c-family/c-objc.h" |
29 | #include "tree-iterator.h" | |
30 | #include "gimplify.h" | |
31 | #include "builtins.h" | |
60813a46 | 32 | #include "tree-inline.h" |
0b274c17 | 33 | #include "ubsan.h" |
44a845ca | 34 | #include "gimple-fold.h" |
8108ea30 | 35 | #include "timevar.h" |
d8fcab68 | 36 | #include "fold-const-call.h" |
2d76680f PC |
37 | |
38 | static bool verify_constant (tree, bool, bool *, bool *); | |
39 | #define VERIFY_CONSTANT(X) \ | |
40 | do { \ | |
2b3ab879 | 41 | if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \ |
2d76680f PC |
42 | return t; \ |
43 | } while (0) | |
44 | ||
582d2481 JJ |
45 | static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex, |
46 | bool insert = false); | |
47 | ||
2d76680f PC |
48 | /* Returns true iff FUN is an instantiation of a constexpr function |
49 | template or a defaulted constexpr function. */ | |
50 | ||
51 | bool | |
52 | is_instantiation_of_constexpr (tree fun) | |
53 | { | |
54 | return ((DECL_TEMPLOID_INSTANTIATION (fun) | |
55 | && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun))) | |
56 | || (DECL_DEFAULTED_FN (fun) | |
57 | && DECL_DECLARED_CONSTEXPR_P (fun))); | |
58 | } | |
59 | ||
60 | /* Return true if T is a literal type. */ | |
61 | ||
62 | bool | |
63 | literal_type_p (tree t) | |
64 | { | |
65 | if (SCALAR_TYPE_P (t) | |
b55b02ea | 66 | || VECTOR_TYPE_P (t) |
9f613f06 | 67 | || TYPE_REF_P (t) |
e42c407c | 68 | || (VOID_TYPE_P (t) && cxx_dialect >= cxx14)) |
2d76680f PC |
69 | return true; |
70 | if (CLASS_TYPE_P (t)) | |
71 | { | |
72 | t = complete_type (t); | |
73 | gcc_assert (COMPLETE_TYPE_P (t) || errorcount); | |
74 | return CLASSTYPE_LITERAL_P (t); | |
75 | } | |
76 | if (TREE_CODE (t) == ARRAY_TYPE) | |
77 | return literal_type_p (strip_array_types (t)); | |
78 | return false; | |
79 | } | |
80 | ||
81 | /* If DECL is a variable declared `constexpr', require its type | |
9638f320 PC |
82 | be literal. Return error_mark_node if we give an error, the |
83 | DECL otherwise. */ | |
2d76680f PC |
84 | |
85 | tree | |
86 | ensure_literal_type_for_constexpr_object (tree decl) | |
87 | { | |
88 | tree type = TREE_TYPE (decl); | |
89 | if (VAR_P (decl) | |
90 | && (DECL_DECLARED_CONSTEXPR_P (decl) | |
91 | || var_in_constexpr_fn (decl)) | |
92 | && !processing_template_decl) | |
93 | { | |
94 | tree stype = strip_array_types (type); | |
95 | if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype))) | |
96 | /* Don't complain here, we'll complain about incompleteness | |
97 | when we try to initialize the variable. */; | |
683be2f7 JM |
98 | else if (type_uses_auto (type)) |
99 | /* We don't know the actual type yet. */; | |
2d76680f PC |
100 | else if (!literal_type_p (type)) |
101 | { | |
102 | if (DECL_DECLARED_CONSTEXPR_P (decl)) | |
f1eac182 | 103 | { |
097f82ec | 104 | auto_diagnostic_group d; |
f2935576 PC |
105 | error_at (DECL_SOURCE_LOCATION (decl), |
106 | "the type %qT of %<constexpr%> variable %qD " | |
107 | "is not literal", type, decl); | |
f1eac182 | 108 | explain_non_literal_class (type); |
9638f320 | 109 | decl = error_mark_node; |
f1eac182 | 110 | } |
2d76680f | 111 | else |
60813a46 | 112 | { |
5d4e573b | 113 | if (!is_instantiation_of_constexpr (current_function_decl)) |
f1eac182 | 114 | { |
097f82ec | 115 | auto_diagnostic_group d; |
f2935576 PC |
116 | error_at (DECL_SOURCE_LOCATION (decl), |
117 | "variable %qD of non-literal type %qT in " | |
118 | "%<constexpr%> function", decl, type); | |
f1eac182 | 119 | explain_non_literal_class (type); |
9638f320 | 120 | decl = error_mark_node; |
f1eac182 | 121 | } |
60813a46 JM |
122 | cp_function_chain->invalid_constexpr = true; |
123 | } | |
2d76680f | 124 | } |
8e9589bd JM |
125 | else if (DECL_DECLARED_CONSTEXPR_P (decl) |
126 | && variably_modified_type_p (type, NULL_TREE)) | |
127 | { | |
f2935576 PC |
128 | error_at (DECL_SOURCE_LOCATION (decl), |
129 | "%<constexpr%> variable %qD has variably-modified " | |
130 | "type %qT", decl, type); | |
8e9589bd JM |
131 | decl = error_mark_node; |
132 | } | |
2d76680f PC |
133 | } |
134 | return decl; | |
135 | } | |
136 | ||
137 | /* Representation of entries in the constexpr function definition table. */ | |
138 | ||
139 | struct GTY((for_user)) constexpr_fundef { | |
140 | tree decl; | |
141 | tree body; | |
43574e4f JJ |
142 | tree parms; |
143 | tree result; | |
2d76680f PC |
144 | }; |
145 | ||
ca752f39 | 146 | struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef> |
2d76680f PC |
147 | { |
148 | static hashval_t hash (constexpr_fundef *); | |
149 | static bool equal (constexpr_fundef *, constexpr_fundef *); | |
150 | }; | |
151 | ||
152 | /* This table holds all constexpr function definitions seen in | |
153 | the current translation unit. */ | |
154 | ||
155 | static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table; | |
156 | ||
157 | /* Utility function used for managing the constexpr function table. | |
158 | Return true if the entries pointed to by P and Q are for the | |
159 | same constexpr function. */ | |
160 | ||
161 | inline bool | |
162 | constexpr_fundef_hasher::equal (constexpr_fundef *lhs, constexpr_fundef *rhs) | |
163 | { | |
164 | return lhs->decl == rhs->decl; | |
165 | } | |
166 | ||
167 | /* Utility function used for managing the constexpr function table. | |
168 | Return a hash value for the entry pointed to by Q. */ | |
169 | ||
170 | inline hashval_t | |
171 | constexpr_fundef_hasher::hash (constexpr_fundef *fundef) | |
172 | { | |
173 | return DECL_UID (fundef->decl); | |
174 | } | |
175 | ||
176 | /* Return a previously saved definition of function FUN. */ | |
177 | ||
178 | static constexpr_fundef * | |
179 | retrieve_constexpr_fundef (tree fun) | |
180 | { | |
2d76680f PC |
181 | if (constexpr_fundef_table == NULL) |
182 | return NULL; | |
183 | ||
43574e4f | 184 | constexpr_fundef fundef = { fun, NULL, NULL, NULL }; |
2d76680f PC |
185 | return constexpr_fundef_table->find (&fundef); |
186 | } | |
187 | ||
188 | /* Check whether the parameter and return types of FUN are valid for a | |
189 | constexpr function, and complain if COMPLAIN. */ | |
190 | ||
98e5a19a | 191 | bool |
2d76680f PC |
192 | is_valid_constexpr_fn (tree fun, bool complain) |
193 | { | |
194 | bool ret = true; | |
195 | ||
31f7f784 | 196 | if (DECL_INHERITED_CTOR (fun) |
2d76680f PC |
197 | && TREE_CODE (fun) == TEMPLATE_DECL) |
198 | { | |
199 | ret = false; | |
200 | if (complain) | |
84fa214d | 201 | error ("inherited constructor %qD is not %<constexpr%>", |
31f7f784 | 202 | DECL_INHERITED_CTOR (fun)); |
2d76680f PC |
203 | } |
204 | else | |
205 | { | |
206 | for (tree parm = FUNCTION_FIRST_USER_PARM (fun); | |
207 | parm != NULL_TREE; parm = TREE_CHAIN (parm)) | |
208 | if (!literal_type_p (TREE_TYPE (parm))) | |
209 | { | |
210 | ret = false; | |
211 | if (complain) | |
212 | { | |
097f82ec | 213 | auto_diagnostic_group d; |
84fa214d | 214 | error ("invalid type for parameter %d of %<constexpr%> " |
2d76680f PC |
215 | "function %q+#D", DECL_PARM_INDEX (parm), fun); |
216 | explain_non_literal_class (TREE_TYPE (parm)); | |
217 | } | |
218 | } | |
219 | } | |
220 | ||
e1bea341 JM |
221 | if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17) |
222 | { | |
223 | ret = false; | |
224 | if (complain) | |
225 | inform (DECL_SOURCE_LOCATION (fun), | |
84fa214d | 226 | "lambdas are implicitly %<constexpr%> only in C++17 and later"); |
e1bea341 JM |
227 | } |
228 | else if (!DECL_CONSTRUCTOR_P (fun)) | |
2d76680f PC |
229 | { |
230 | tree rettype = TREE_TYPE (TREE_TYPE (fun)); | |
231 | if (!literal_type_p (rettype)) | |
232 | { | |
233 | ret = false; | |
234 | if (complain) | |
235 | { | |
097f82ec | 236 | auto_diagnostic_group d; |
84fa214d | 237 | error ("invalid return type %qT of %<constexpr%> function %q+D", |
2d76680f PC |
238 | rettype, fun); |
239 | explain_non_literal_class (rettype); | |
240 | } | |
241 | } | |
242 | ||
54069e59 JM |
243 | /* C++14 DR 1684 removed this restriction. */ |
244 | if (cxx_dialect < cxx14 | |
245 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) | |
2d76680f PC |
246 | && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun))) |
247 | { | |
248 | ret = false; | |
097f82ec DM |
249 | if (complain) |
250 | { | |
251 | auto_diagnostic_group d; | |
252 | if (pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic, | |
253 | "enclosing class of %<constexpr%> non-static" | |
254 | " member function %q+#D is not a literal type", | |
255 | fun)) | |
256 | explain_non_literal_class (DECL_CONTEXT (fun)); | |
257 | } | |
2d76680f PC |
258 | } |
259 | } | |
260 | else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun))) | |
261 | { | |
262 | ret = false; | |
263 | if (complain) | |
264 | error ("%q#T has virtual base classes", DECL_CONTEXT (fun)); | |
265 | } | |
266 | ||
267 | return ret; | |
268 | } | |
269 | ||
270 | /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF | |
271 | for a member of an anonymous aggregate, INIT is the initializer for that | |
272 | member, and VEC_OUTER is the vector of constructor elements for the class | |
273 | whose constructor we are processing. Add the initializer to the vector | |
274 | and return true to indicate success. */ | |
275 | ||
276 | static bool | |
277 | build_anon_member_initialization (tree member, tree init, | |
278 | vec<constructor_elt, va_gc> **vec_outer) | |
279 | { | |
280 | /* MEMBER presents the relevant fields from the inside out, but we need | |
281 | to build up the initializer from the outside in so that we can reuse | |
282 | previously built CONSTRUCTORs if this is, say, the second field in an | |
283 | anonymous struct. So we use a vec as a stack. */ | |
284 | auto_vec<tree, 2> fields; | |
285 | do | |
286 | { | |
287 | fields.safe_push (TREE_OPERAND (member, 1)); | |
288 | member = TREE_OPERAND (member, 0); | |
289 | } | |
290 | while (ANON_AGGR_TYPE_P (TREE_TYPE (member)) | |
291 | && TREE_CODE (member) == COMPONENT_REF); | |
292 | ||
293 | /* VEC has the constructor elements vector for the context of FIELD. | |
294 | If FIELD is an anonymous aggregate, we will push inside it. */ | |
295 | vec<constructor_elt, va_gc> **vec = vec_outer; | |
296 | tree field; | |
297 | while (field = fields.pop(), | |
298 | ANON_AGGR_TYPE_P (TREE_TYPE (field))) | |
299 | { | |
300 | tree ctor; | |
301 | /* If there is already an outer constructor entry for the anonymous | |
302 | aggregate FIELD, use it; otherwise, insert one. */ | |
303 | if (vec_safe_is_empty (*vec) | |
304 | || (*vec)->last().index != field) | |
305 | { | |
306 | ctor = build_constructor (TREE_TYPE (field), NULL); | |
307 | CONSTRUCTOR_APPEND_ELT (*vec, field, ctor); | |
308 | } | |
309 | else | |
310 | ctor = (*vec)->last().value; | |
311 | vec = &CONSTRUCTOR_ELTS (ctor); | |
312 | } | |
313 | ||
314 | /* Now we're at the innermost field, the one that isn't an anonymous | |
315 | aggregate. Add its initializer to the CONSTRUCTOR and we're done. */ | |
316 | gcc_assert (fields.is_empty()); | |
317 | CONSTRUCTOR_APPEND_ELT (*vec, field, init); | |
318 | ||
319 | return true; | |
320 | } | |
321 | ||
322 | /* Subroutine of build_constexpr_constructor_member_initializers. | |
323 | The expression tree T represents a data member initialization | |
324 | in a (constexpr) constructor definition. Build a pairing of | |
325 | the data member with its initializer, and prepend that pair | |
326 | to the existing initialization pair INITS. */ | |
327 | ||
328 | static bool | |
329 | build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec) | |
330 | { | |
331 | tree member, init; | |
332 | if (TREE_CODE (t) == CLEANUP_POINT_EXPR) | |
333 | t = TREE_OPERAND (t, 0); | |
334 | if (TREE_CODE (t) == EXPR_STMT) | |
335 | t = TREE_OPERAND (t, 0); | |
336 | if (t == error_mark_node) | |
337 | return false; | |
338 | if (TREE_CODE (t) == STATEMENT_LIST) | |
339 | { | |
340 | tree_stmt_iterator i; | |
341 | for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) | |
342 | { | |
343 | if (! build_data_member_initialization (tsi_stmt (i), vec)) | |
344 | return false; | |
345 | } | |
346 | return true; | |
347 | } | |
348 | if (TREE_CODE (t) == CLEANUP_STMT) | |
349 | { | |
350 | /* We can't see a CLEANUP_STMT in a constructor for a literal class, | |
351 | but we can in a constexpr constructor for a non-literal class. Just | |
352 | ignore it; either all the initialization will be constant, in which | |
353 | case the cleanup can't run, or it can't be constexpr. | |
354 | Still recurse into CLEANUP_BODY. */ | |
355 | return build_data_member_initialization (CLEANUP_BODY (t), vec); | |
356 | } | |
357 | if (TREE_CODE (t) == CONVERT_EXPR) | |
358 | t = TREE_OPERAND (t, 0); | |
359 | if (TREE_CODE (t) == INIT_EXPR | |
60813a46 JM |
360 | /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only |
361 | use what this function builds for cx_check_missing_mem_inits, and | |
362 | assignment in the ctor body doesn't count. */ | |
363 | || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR)) | |
2d76680f PC |
364 | { |
365 | member = TREE_OPERAND (t, 0); | |
366 | init = break_out_target_exprs (TREE_OPERAND (t, 1)); | |
367 | } | |
368 | else if (TREE_CODE (t) == CALL_EXPR) | |
369 | { | |
60813a46 JM |
370 | tree fn = get_callee_fndecl (t); |
371 | if (!fn || !DECL_CONSTRUCTOR_P (fn)) | |
372 | /* We're only interested in calls to subobject constructors. */ | |
373 | return true; | |
2d76680f PC |
374 | member = CALL_EXPR_ARG (t, 0); |
375 | /* We don't use build_cplus_new here because it complains about | |
376 | abstract bases. Leaving the call unwrapped means that it has the | |
377 | wrong type, but cxx_eval_constant_expression doesn't care. */ | |
378 | init = break_out_target_exprs (t); | |
379 | } | |
380 | else if (TREE_CODE (t) == BIND_EXPR) | |
381 | return build_data_member_initialization (BIND_EXPR_BODY (t), vec); | |
2d76680f | 382 | else |
60813a46 JM |
383 | /* Don't add anything else to the CONSTRUCTOR. */ |
384 | return true; | |
2d76680f PC |
385 | if (INDIRECT_REF_P (member)) |
386 | member = TREE_OPERAND (member, 0); | |
387 | if (TREE_CODE (member) == NOP_EXPR) | |
388 | { | |
389 | tree op = member; | |
390 | STRIP_NOPS (op); | |
391 | if (TREE_CODE (op) == ADDR_EXPR) | |
392 | { | |
393 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
394 | (TREE_TYPE (TREE_TYPE (op)), | |
395 | TREE_TYPE (TREE_TYPE (member)))); | |
396 | /* Initializing a cv-qualified member; we need to look through | |
397 | the const_cast. */ | |
398 | member = op; | |
399 | } | |
400 | else if (op == current_class_ptr | |
401 | && (same_type_ignoring_top_level_qualifiers_p | |
402 | (TREE_TYPE (TREE_TYPE (member)), | |
403 | current_class_type))) | |
404 | /* Delegating constructor. */ | |
405 | member = op; | |
406 | else | |
407 | { | |
408 | /* This is an initializer for an empty base; keep it for now so | |
409 | we can check it in cxx_eval_bare_aggregate. */ | |
410 | gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member)))); | |
411 | } | |
412 | } | |
413 | if (TREE_CODE (member) == ADDR_EXPR) | |
414 | member = TREE_OPERAND (member, 0); | |
415 | if (TREE_CODE (member) == COMPONENT_REF) | |
416 | { | |
417 | tree aggr = TREE_OPERAND (member, 0); | |
8cb7aaa1 JM |
418 | if (TREE_CODE (aggr) == VAR_DECL) |
419 | /* Initializing a local variable, don't add anything. */ | |
420 | return true; | |
2d76680f PC |
421 | if (TREE_CODE (aggr) != COMPONENT_REF) |
422 | /* Normal member initialization. */ | |
423 | member = TREE_OPERAND (member, 1); | |
424 | else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr))) | |
425 | /* Initializing a member of an anonymous union. */ | |
426 | return build_anon_member_initialization (member, init, vec); | |
427 | else | |
428 | /* We're initializing a vtable pointer in a base. Leave it as | |
429 | COMPONENT_REF so we remember the path to get to the vfield. */ | |
430 | gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node); | |
431 | } | |
432 | ||
49b5925f JM |
433 | /* Value-initialization can produce multiple initializers for the |
434 | same field; use the last one. */ | |
435 | if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member) | |
436 | (*vec)->last().value = init; | |
437 | else | |
438 | CONSTRUCTOR_APPEND_ELT (*vec, member, init); | |
2d76680f PC |
439 | return true; |
440 | } | |
441 | ||
442 | /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval. | |
443 | In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a | |
444 | BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */ | |
445 | ||
446 | static bool | |
447 | check_constexpr_bind_expr_vars (tree t) | |
448 | { | |
449 | gcc_assert (TREE_CODE (t) == BIND_EXPR); | |
450 | ||
2d76680f PC |
451 | for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var)) |
452 | if (TREE_CODE (var) == TYPE_DECL | |
4414e22e PC |
453 | && DECL_IMPLICIT_TYPEDEF_P (var) |
454 | && !LAMBDA_TYPE_P (TREE_TYPE (var))) | |
2d76680f PC |
455 | return false; |
456 | return true; | |
457 | } | |
458 | ||
459 | /* Subroutine of check_constexpr_ctor_body. */ | |
460 | ||
461 | static bool | |
462 | check_constexpr_ctor_body_1 (tree last, tree list) | |
463 | { | |
464 | switch (TREE_CODE (list)) | |
465 | { | |
466 | case DECL_EXPR: | |
a0008434 MP |
467 | if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL |
468 | || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL) | |
2d76680f | 469 | return true; |
2d76680f PC |
470 | return false; |
471 | ||
472 | case CLEANUP_POINT_EXPR: | |
473 | return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0), | |
474 | /*complain=*/false); | |
475 | ||
476 | case BIND_EXPR: | |
477 | if (!check_constexpr_bind_expr_vars (list) | |
478 | || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list), | |
479 | /*complain=*/false)) | |
480 | return false; | |
481 | return true; | |
482 | ||
483 | case USING_STMT: | |
484 | case STATIC_ASSERT: | |
96a95ac1 | 485 | case DEBUG_BEGIN_STMT: |
2d76680f PC |
486 | return true; |
487 | ||
488 | default: | |
489 | return false; | |
490 | } | |
491 | } | |
492 | ||
493 | /* Make sure that there are no statements after LAST in the constructor | |
494 | body represented by LIST. */ | |
495 | ||
496 | bool | |
497 | check_constexpr_ctor_body (tree last, tree list, bool complain) | |
498 | { | |
60813a46 JM |
499 | /* C++14 doesn't require a constexpr ctor to have an empty body. */ |
500 | if (cxx_dialect >= cxx14) | |
501 | return true; | |
502 | ||
2d76680f PC |
503 | bool ok = true; |
504 | if (TREE_CODE (list) == STATEMENT_LIST) | |
505 | { | |
506 | tree_stmt_iterator i = tsi_last (list); | |
507 | for (; !tsi_end_p (i); tsi_prev (&i)) | |
508 | { | |
509 | tree t = tsi_stmt (i); | |
510 | if (t == last) | |
511 | break; | |
512 | if (!check_constexpr_ctor_body_1 (last, t)) | |
513 | { | |
514 | ok = false; | |
515 | break; | |
516 | } | |
517 | } | |
518 | } | |
519 | else if (list != last | |
520 | && !check_constexpr_ctor_body_1 (last, list)) | |
521 | ok = false; | |
522 | if (!ok) | |
523 | { | |
524 | if (complain) | |
84fa214d | 525 | error ("%<constexpr%> constructor does not have empty body"); |
2d76680f PC |
526 | DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false; |
527 | } | |
528 | return ok; | |
529 | } | |
530 | ||
531 | /* V is a vector of constructor elements built up for the base and member | |
532 | initializers of a constructor for TYPE. They need to be in increasing | |
533 | offset order, which they might not be yet if TYPE has a primary base | |
534 | which is not first in the base-clause or a vptr and at least one base | |
535 | all of which are non-primary. */ | |
536 | ||
537 | static vec<constructor_elt, va_gc> * | |
538 | sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v) | |
539 | { | |
540 | tree pri = CLASSTYPE_PRIMARY_BINFO (type); | |
541 | tree field_type; | |
542 | unsigned i; | |
543 | constructor_elt *ce; | |
544 | ||
545 | if (pri) | |
546 | field_type = BINFO_TYPE (pri); | |
547 | else if (TYPE_CONTAINS_VPTR_P (type)) | |
548 | field_type = vtbl_ptr_type_node; | |
549 | else | |
550 | return v; | |
551 | ||
552 | /* Find the element for the primary base or vptr and move it to the | |
553 | beginning of the vec. */ | |
554 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
555 | if (TREE_TYPE (ce->index) == field_type) | |
556 | break; | |
557 | ||
558 | if (i > 0 && i < vec_safe_length (v)) | |
559 | { | |
560 | vec<constructor_elt, va_gc> &vref = *v; | |
561 | constructor_elt elt = vref[i]; | |
562 | for (; i > 0; --i) | |
563 | vref[i] = vref[i-1]; | |
564 | vref[0] = elt; | |
565 | } | |
566 | ||
567 | return v; | |
568 | } | |
569 | ||
570 | /* Build compile-time evalable representations of member-initializer list | |
571 | for a constexpr constructor. */ | |
572 | ||
573 | static tree | |
574 | build_constexpr_constructor_member_initializers (tree type, tree body) | |
575 | { | |
576 | vec<constructor_elt, va_gc> *vec = NULL; | |
577 | bool ok = true; | |
fa837fb6 JM |
578 | while (true) |
579 | switch (TREE_CODE (body)) | |
580 | { | |
581 | case MUST_NOT_THROW_EXPR: | |
582 | case EH_SPEC_BLOCK: | |
583 | body = TREE_OPERAND (body, 0); | |
584 | break; | |
585 | ||
586 | case STATEMENT_LIST: | |
587 | for (tree_stmt_iterator i = tsi_start (body); | |
588 | !tsi_end_p (i); tsi_next (&i)) | |
589 | { | |
590 | body = tsi_stmt (i); | |
591 | if (TREE_CODE (body) == BIND_EXPR) | |
592 | break; | |
593 | } | |
594 | break; | |
595 | ||
596 | case BIND_EXPR: | |
597 | body = BIND_EXPR_BODY (body); | |
598 | goto found; | |
599 | ||
600 | default: | |
601 | gcc_unreachable (); | |
58cc255c | 602 | } |
fa837fb6 | 603 | found: |
1259cb6d JJ |
604 | if (TREE_CODE (body) == TRY_BLOCK) |
605 | { | |
606 | body = TREE_OPERAND (body, 0); | |
607 | if (TREE_CODE (body) == BIND_EXPR) | |
608 | body = BIND_EXPR_BODY (body); | |
609 | } | |
2d76680f PC |
610 | if (TREE_CODE (body) == CLEANUP_POINT_EXPR) |
611 | { | |
612 | body = TREE_OPERAND (body, 0); | |
613 | if (TREE_CODE (body) == EXPR_STMT) | |
614 | body = TREE_OPERAND (body, 0); | |
615 | if (TREE_CODE (body) == INIT_EXPR | |
616 | && (same_type_ignoring_top_level_qualifiers_p | |
617 | (TREE_TYPE (TREE_OPERAND (body, 0)), | |
618 | current_class_type))) | |
619 | { | |
620 | /* Trivial copy. */ | |
621 | return TREE_OPERAND (body, 1); | |
622 | } | |
623 | ok = build_data_member_initialization (body, &vec); | |
624 | } | |
625 | else if (TREE_CODE (body) == STATEMENT_LIST) | |
626 | { | |
627 | tree_stmt_iterator i; | |
628 | for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i)) | |
629 | { | |
630 | ok = build_data_member_initialization (tsi_stmt (i), &vec); | |
631 | if (!ok) | |
632 | break; | |
633 | } | |
634 | } | |
2d76680f PC |
635 | else if (EXPR_P (body)) |
636 | ok = build_data_member_initialization (body, &vec); | |
637 | else | |
638 | gcc_assert (errorcount > 0); | |
639 | if (ok) | |
640 | { | |
641 | if (vec_safe_length (vec) > 0) | |
642 | { | |
643 | /* In a delegating constructor, return the target. */ | |
644 | constructor_elt *ce = &(*vec)[0]; | |
645 | if (ce->index == current_class_ptr) | |
646 | { | |
647 | body = ce->value; | |
648 | vec_free (vec); | |
649 | return body; | |
650 | } | |
651 | } | |
652 | vec = sort_constexpr_mem_initializers (type, vec); | |
653 | return build_constructor (type, vec); | |
654 | } | |
655 | else | |
656 | return error_mark_node; | |
657 | } | |
658 | ||
1b6fa695 ML |
659 | /* We have an expression tree T that represents a call, either CALL_EXPR |
660 | or AGGR_INIT_EXPR. If the call is lexically to a named function, | |
661 | retrun the _DECL for that function. */ | |
662 | ||
663 | static tree | |
664 | get_function_named_in_call (tree t) | |
665 | { | |
666 | tree fun = cp_get_callee (t); | |
667 | if (fun && TREE_CODE (fun) == ADDR_EXPR | |
668 | && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL) | |
669 | fun = TREE_OPERAND (fun, 0); | |
670 | return fun; | |
671 | } | |
672 | ||
2d76680f PC |
673 | /* Subroutine of register_constexpr_fundef. BODY is the body of a function |
674 | declared to be constexpr, or a sub-statement thereof. Returns the | |
675 | return value if suitable, error_mark_node for a statement not allowed in | |
676 | a constexpr function, or NULL_TREE if no return value was found. */ | |
677 | ||
5afef8b1 | 678 | tree |
2d76680f PC |
679 | constexpr_fn_retval (tree body) |
680 | { | |
681 | switch (TREE_CODE (body)) | |
682 | { | |
683 | case STATEMENT_LIST: | |
684 | { | |
685 | tree_stmt_iterator i; | |
686 | tree expr = NULL_TREE; | |
687 | for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i)) | |
688 | { | |
689 | tree s = constexpr_fn_retval (tsi_stmt (i)); | |
690 | if (s == error_mark_node) | |
691 | return error_mark_node; | |
692 | else if (s == NULL_TREE) | |
693 | /* Keep iterating. */; | |
694 | else if (expr) | |
695 | /* Multiple return statements. */ | |
696 | return error_mark_node; | |
697 | else | |
698 | expr = s; | |
699 | } | |
700 | return expr; | |
701 | } | |
702 | ||
703 | case RETURN_EXPR: | |
704 | return break_out_target_exprs (TREE_OPERAND (body, 0)); | |
705 | ||
706 | case DECL_EXPR: | |
0162cb3b PC |
707 | { |
708 | tree decl = DECL_EXPR_DECL (body); | |
709 | if (TREE_CODE (decl) == USING_DECL | |
710 | /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */ | |
711 | || DECL_ARTIFICIAL (decl)) | |
712 | return NULL_TREE; | |
713 | return error_mark_node; | |
714 | } | |
2d76680f PC |
715 | |
716 | case CLEANUP_POINT_EXPR: | |
717 | return constexpr_fn_retval (TREE_OPERAND (body, 0)); | |
718 | ||
719 | case BIND_EXPR: | |
720 | if (!check_constexpr_bind_expr_vars (body)) | |
721 | return error_mark_node; | |
722 | return constexpr_fn_retval (BIND_EXPR_BODY (body)); | |
723 | ||
724 | case USING_STMT: | |
96a95ac1 | 725 | case DEBUG_BEGIN_STMT: |
2d76680f PC |
726 | return NULL_TREE; |
727 | ||
1b6fa695 ML |
728 | case CALL_EXPR: |
729 | { | |
730 | tree fun = get_function_named_in_call (body); | |
731 | if (fun != NULL_TREE | |
3d78e008 | 732 | && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE)) |
1b6fa695 ML |
733 | return NULL_TREE; |
734 | } | |
735 | /* Fallthru. */ | |
736 | ||
2d76680f PC |
737 | default: |
738 | return error_mark_node; | |
739 | } | |
740 | } | |
741 | ||
742 | /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of | |
743 | FUN; do the necessary transformations to turn it into a single expression | |
744 | that we can store in the hash table. */ | |
745 | ||
746 | static tree | |
747 | massage_constexpr_body (tree fun, tree body) | |
748 | { | |
749 | if (DECL_CONSTRUCTOR_P (fun)) | |
750 | body = build_constexpr_constructor_member_initializers | |
751 | (DECL_CONTEXT (fun), body); | |
60813a46 | 752 | else if (cxx_dialect < cxx14) |
2d76680f PC |
753 | { |
754 | if (TREE_CODE (body) == EH_SPEC_BLOCK) | |
755 | body = EH_SPEC_STMTS (body); | |
756 | if (TREE_CODE (body) == MUST_NOT_THROW_EXPR) | |
757 | body = TREE_OPERAND (body, 0); | |
758 | body = constexpr_fn_retval (body); | |
759 | } | |
760 | return body; | |
761 | } | |
762 | ||
3e4b91f2 NS |
763 | /* CTYPE is a type constructed from BODY. Return true if some |
764 | bases/fields are uninitialized, and complain if COMPLAIN. */ | |
2d76680f PC |
765 | |
766 | static bool | |
3e4b91f2 | 767 | cx_check_missing_mem_inits (tree ctype, tree body, bool complain) |
2d76680f | 768 | { |
3e4b91f2 NS |
769 | unsigned nelts = 0; |
770 | ||
771 | if (body) | |
772 | { | |
773 | if (TREE_CODE (body) != CONSTRUCTOR) | |
774 | return false; | |
775 | nelts = CONSTRUCTOR_NELTS (body); | |
776 | } | |
777 | tree field = TYPE_FIELDS (ctype); | |
2d76680f PC |
778 | |
779 | if (TREE_CODE (ctype) == UNION_TYPE) | |
780 | { | |
781 | if (nelts == 0 && next_initializable_field (field)) | |
782 | { | |
783 | if (complain) | |
784 | error ("%<constexpr%> constructor for union %qT must " | |
785 | "initialize exactly one non-static data member", ctype); | |
786 | return true; | |
787 | } | |
788 | return false; | |
789 | } | |
790 | ||
3e4b91f2 NS |
791 | /* Iterate over the CONSTRUCTOR, checking any missing fields don't |
792 | need an explicit initialization. */ | |
793 | bool bad = false; | |
794 | for (unsigned i = 0; i <= nelts; ++i) | |
2d76680f | 795 | { |
3e4b91f2 NS |
796 | tree index = NULL_TREE; |
797 | if (i < nelts) | |
2d76680f PC |
798 | { |
799 | index = CONSTRUCTOR_ELT (body, i)->index; | |
800 | /* Skip base and vtable inits. */ | |
801 | if (TREE_CODE (index) != FIELD_DECL | |
802 | || DECL_ARTIFICIAL (index)) | |
803 | continue; | |
804 | } | |
3e4b91f2 | 805 | |
2d76680f PC |
806 | for (; field != index; field = DECL_CHAIN (field)) |
807 | { | |
808 | tree ftype; | |
3e4b91f2 NS |
809 | if (TREE_CODE (field) != FIELD_DECL) |
810 | continue; | |
7c30b12a | 811 | if (DECL_UNNAMED_BIT_FIELD (field)) |
2d76680f | 812 | continue; |
3e4b91f2 NS |
813 | if (DECL_ARTIFICIAL (field)) |
814 | continue; | |
815 | if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) | |
816 | { | |
817 | /* Recurse to check the anonummous aggregate member. */ | |
818 | bad |= cx_check_missing_mem_inits | |
819 | (TREE_TYPE (field), NULL_TREE, complain); | |
820 | if (bad && !complain) | |
821 | return true; | |
822 | continue; | |
823 | } | |
2d76680f PC |
824 | ftype = strip_array_types (TREE_TYPE (field)); |
825 | if (type_has_constexpr_default_constructor (ftype)) | |
826 | { | |
827 | /* It's OK to skip a member with a trivial constexpr ctor. | |
828 | A constexpr ctor that isn't trivial should have been | |
829 | added in by now. */ | |
830 | gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype) | |
831 | || errorcount != 0); | |
832 | continue; | |
833 | } | |
834 | if (!complain) | |
835 | return true; | |
097f82ec | 836 | auto_diagnostic_group d; |
b8cd3996 JM |
837 | error ("member %qD must be initialized by mem-initializer " |
838 | "in %<constexpr%> constructor", field); | |
839 | inform (DECL_SOURCE_LOCATION (field), "declared here"); | |
2d76680f PC |
840 | bad = true; |
841 | } | |
842 | if (field == NULL_TREE) | |
843 | break; | |
3e4b91f2 NS |
844 | |
845 | if (ANON_AGGR_TYPE_P (TREE_TYPE (index))) | |
846 | { | |
847 | /* Check the anonymous aggregate initializer is valid. */ | |
848 | bad |= cx_check_missing_mem_inits | |
849 | (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain); | |
850 | if (bad && !complain) | |
851 | return true; | |
852 | } | |
2d76680f PC |
853 | field = DECL_CHAIN (field); |
854 | } | |
855 | ||
856 | return bad; | |
857 | } | |
858 | ||
859 | /* We are processing the definition of the constexpr function FUN. | |
860 | Check that its BODY fulfills the propriate requirements and | |
861 | enter it in the constexpr function definition table. | |
862 | For constructor BODY is actually the TREE_LIST of the | |
863 | member-initializer list. */ | |
864 | ||
865 | tree | |
866 | register_constexpr_fundef (tree fun, tree body) | |
867 | { | |
868 | constexpr_fundef entry; | |
869 | constexpr_fundef **slot; | |
870 | ||
871 | if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun))) | |
872 | return NULL; | |
873 | ||
ca30abcd JM |
874 | tree massaged = massage_constexpr_body (fun, body); |
875 | if (massaged == NULL_TREE || massaged == error_mark_node) | |
2d76680f PC |
876 | { |
877 | if (!DECL_CONSTRUCTOR_P (fun)) | |
84fa214d ML |
878 | error ("body of %<constexpr%> function %qD not a return-statement", |
879 | fun); | |
2d76680f PC |
880 | return NULL; |
881 | } | |
882 | ||
ca30abcd | 883 | if (!potential_rvalue_constant_expression (massaged)) |
2d76680f PC |
884 | { |
885 | if (!DECL_GENERATED_P (fun)) | |
ca30abcd | 886 | require_potential_rvalue_constant_expression (massaged); |
2d76680f PC |
887 | return NULL; |
888 | } | |
889 | ||
890 | if (DECL_CONSTRUCTOR_P (fun) | |
3e4b91f2 NS |
891 | && cx_check_missing_mem_inits (DECL_CONTEXT (fun), |
892 | massaged, !DECL_GENERATED_P (fun))) | |
2d76680f PC |
893 | return NULL; |
894 | ||
895 | /* Create the constexpr function table if necessary. */ | |
896 | if (constexpr_fundef_table == NULL) | |
897 | constexpr_fundef_table | |
898 | = hash_table<constexpr_fundef_hasher>::create_ggc (101); | |
899 | ||
900 | entry.decl = fun; | |
43574e4f JJ |
901 | tree saved_fn = current_function_decl; |
902 | bool clear_ctx = false; | |
903 | current_function_decl = fun; | |
904 | if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE) | |
905 | { | |
906 | clear_ctx = true; | |
907 | DECL_CONTEXT (DECL_RESULT (fun)) = fun; | |
908 | } | |
909 | entry.body = copy_fn (fun, entry.parms, entry.result); | |
910 | current_function_decl = saved_fn; | |
2d76680f | 911 | slot = constexpr_fundef_table->find_slot (&entry, INSERT); |
43574e4f JJ |
912 | if (clear_ctx) |
913 | DECL_CONTEXT (DECL_RESULT (fun)) = NULL_TREE; | |
2d76680f PC |
914 | |
915 | gcc_assert (*slot == NULL); | |
916 | *slot = ggc_alloc<constexpr_fundef> (); | |
917 | **slot = entry; | |
918 | ||
919 | return fun; | |
920 | } | |
921 | ||
922 | /* FUN is a non-constexpr function called in a context that requires a | |
923 | constant expression. If it comes from a constexpr template, explain why | |
924 | the instantiation isn't constexpr. */ | |
925 | ||
926 | void | |
927 | explain_invalid_constexpr_fn (tree fun) | |
928 | { | |
929 | static hash_set<tree> *diagnosed; | |
930 | tree body; | |
931 | location_t save_loc; | |
98e5a19a | 932 | /* Only diagnose defaulted functions, lambdas, or instantiations. */ |
2d76680f | 933 | if (!DECL_DEFAULTED_FN (fun) |
98e5a19a | 934 | && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) |
2d76680f PC |
935 | && !is_instantiation_of_constexpr (fun)) |
936 | return; | |
937 | if (diagnosed == NULL) | |
938 | diagnosed = new hash_set<tree>; | |
939 | if (diagnosed->add (fun)) | |
940 | /* Already explained. */ | |
941 | return; | |
942 | ||
943 | save_loc = input_location; | |
98e5a19a JM |
944 | if (!lambda_static_thunk_p (fun)) |
945 | { | |
946 | /* Diagnostics should completely ignore the static thunk, so leave | |
947 | input_location set to our caller's location. */ | |
948 | input_location = DECL_SOURCE_LOCATION (fun); | |
949 | inform (input_location, | |
84fa214d | 950 | "%qD is not usable as a %<constexpr%> function because:", fun); |
98e5a19a | 951 | } |
2d76680f PC |
952 | /* First check the declaration. */ |
953 | if (is_valid_constexpr_fn (fun, true)) | |
954 | { | |
955 | /* Then if it's OK, the body. */ | |
98e5a19a JM |
956 | if (!DECL_DECLARED_CONSTEXPR_P (fun) |
957 | && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))) | |
2d76680f PC |
958 | explain_implicit_non_constexpr (fun); |
959 | else | |
960 | { | |
961 | body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun)); | |
962 | require_potential_rvalue_constant_expression (body); | |
963 | if (DECL_CONSTRUCTOR_P (fun)) | |
3e4b91f2 | 964 | cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true); |
2d76680f PC |
965 | } |
966 | } | |
967 | input_location = save_loc; | |
968 | } | |
969 | ||
970 | /* Objects of this type represent calls to constexpr functions | |
971 | along with the bindings of parameters to their arguments, for | |
972 | the purpose of compile time evaluation. */ | |
973 | ||
974 | struct GTY((for_user)) constexpr_call { | |
975 | /* Description of the constexpr function definition. */ | |
976 | constexpr_fundef *fundef; | |
3c961dc7 | 977 | /* Parameter bindings environment. A TREE_VEC of arguments. */ |
2d76680f PC |
978 | tree bindings; |
979 | /* Result of the call. | |
980 | NULL means the call is being evaluated. | |
981 | error_mark_node means that the evaluation was erroneous; | |
982 | otherwise, the actuall value of the call. */ | |
983 | tree result; | |
984 | /* The hash of this call; we remember it here to avoid having to | |
985 | recalculate it when expanding the hash table. */ | |
986 | hashval_t hash; | |
cce3ae91 | 987 | /* Whether __builtin_is_constant_evaluated() should evaluate to true. */ |
13de99bc | 988 | bool manifestly_const_eval; |
2d76680f PC |
989 | }; |
990 | ||
ca752f39 | 991 | struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call> |
2d76680f PC |
992 | { |
993 | static hashval_t hash (constexpr_call *); | |
994 | static bool equal (constexpr_call *, constexpr_call *); | |
3e605b20 JM |
995 | }; |
996 | ||
4b390698 JJ |
997 | enum constexpr_switch_state { |
998 | /* Used when processing a switch for the first time by cxx_eval_switch_expr | |
999 | and default: label for that switch has not been seen yet. */ | |
1000 | css_default_not_seen, | |
1001 | /* Used when processing a switch for the first time by cxx_eval_switch_expr | |
1002 | and default: label for that switch has been seen already. */ | |
1003 | css_default_seen, | |
1004 | /* Used when processing a switch for the second time by | |
1005 | cxx_eval_switch_expr, where default: label should match. */ | |
1006 | css_default_processing | |
1007 | }; | |
1008 | ||
3e605b20 JM |
1009 | /* The constexpr expansion context. CALL is the current function |
1010 | expansion, CTOR is the current aggregate initializer, OBJECT is the | |
1011 | object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES | |
1012 | is a map of values of variables initialized within the expression. */ | |
1013 | ||
1014 | struct constexpr_ctx { | |
13f649f6 | 1015 | /* The innermost call we're evaluating. */ |
3e605b20 | 1016 | constexpr_call *call; |
13f649f6 JM |
1017 | /* Values for any temporaries or local variables within the |
1018 | constant-expression. */ | |
3e605b20 | 1019 | hash_map<tree,tree> *values; |
39dce2b7 JM |
1020 | /* SAVE_EXPRs that we've seen within the current LOOP_EXPR. NULL if we |
1021 | aren't inside a loop. */ | |
0ee28590 | 1022 | vec<tree> *save_exprs; |
13f649f6 JM |
1023 | /* The CONSTRUCTOR we're currently building up for an aggregate |
1024 | initializer. */ | |
3e605b20 | 1025 | tree ctor; |
13f649f6 | 1026 | /* The object we're building the CONSTRUCTOR for. */ |
3e605b20 | 1027 | tree object; |
4b390698 JJ |
1028 | /* If inside SWITCH_EXPR. */ |
1029 | constexpr_switch_state *css_state; | |
a15ffa22 JJ |
1030 | /* Number of cxx_eval_constant_expression calls (except skipped ones, |
1031 | on simple constants or location wrappers) encountered during current | |
1032 | cxx_eval_outermost_constant_expr call. */ | |
1033 | HOST_WIDE_INT *constexpr_ops_count; | |
1034 | ||
13f649f6 | 1035 | /* Whether we should error on a non-constant expression or fail quietly. */ |
2b3ab879 | 1036 | bool quiet; |
13f649f6 JM |
1037 | /* Whether we are strictly conforming to constant expression rules or |
1038 | trying harder to get a constant value. */ | |
69eb4fde | 1039 | bool strict; |
e4082611 | 1040 | /* Whether __builtin_is_constant_evaluated () should be true. */ |
13de99bc | 1041 | bool manifestly_const_eval; |
3e605b20 | 1042 | }; |
2d76680f PC |
1043 | |
1044 | /* A table of all constexpr calls that have been evaluated by the | |
1045 | compiler in this translation unit. */ | |
1046 | ||
97f3003f | 1047 | static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table; |
2d76680f | 1048 | |
3e605b20 | 1049 | static tree cxx_eval_constant_expression (const constexpr_ctx *, tree, |
5a804683 | 1050 | bool, bool *, bool *, tree * = NULL); |
2d76680f PC |
1051 | |
1052 | /* Compute a hash value for a constexpr call representation. */ | |
1053 | ||
1054 | inline hashval_t | |
1055 | constexpr_call_hasher::hash (constexpr_call *info) | |
1056 | { | |
1057 | return info->hash; | |
1058 | } | |
1059 | ||
1060 | /* Return true if the objects pointed to by P and Q represent calls | |
1061 | to the same constexpr function with the same arguments. | |
1062 | Otherwise, return false. */ | |
1063 | ||
1064 | bool | |
1065 | constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs) | |
1066 | { | |
2d76680f | 1067 | if (lhs == rhs) |
46cf7fa1 JJ |
1068 | return true; |
1069 | if (lhs->hash != rhs->hash) | |
1070 | return false; | |
13de99bc | 1071 | if (lhs->manifestly_const_eval != rhs->manifestly_const_eval) |
cce3ae91 | 1072 | return false; |
2d76680f | 1073 | if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef)) |
46cf7fa1 | 1074 | return false; |
3c961dc7 | 1075 | return cp_tree_equal (lhs->bindings, rhs->bindings); |
2d76680f PC |
1076 | } |
1077 | ||
1078 | /* Initialize the constexpr call table, if needed. */ | |
1079 | ||
1080 | static void | |
1081 | maybe_initialize_constexpr_call_table (void) | |
1082 | { | |
1083 | if (constexpr_call_table == NULL) | |
1084 | constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101); | |
1085 | } | |
1086 | ||
c0daf32d PP |
1087 | /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when |
1088 | a function happens to get called recursively, we unshare the callee | |
1089 | function's body and evaluate this unshared copy instead of evaluating the | |
1090 | original body. | |
1091 | ||
1092 | FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function | |
1093 | copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map | |
97f3003f JM |
1094 | that's keyed off of the original FUNCTION_DECL and whose value is a |
1095 | TREE_LIST of this function's unused copies awaiting reuse. | |
c0daf32d | 1096 | |
97f3003f | 1097 | This is not GC-deletable to avoid GC affecting UID generation. */ |
c0daf32d | 1098 | |
97f3003f | 1099 | static GTY(()) hash_map<tree, tree> *fundef_copies_table; |
c0daf32d | 1100 | |
c0daf32d | 1101 | /* Reuse a copy or create a new unshared copy of the function FUN. |
3c98ff9b NS |
1102 | Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE |
1103 | is parms, TYPE is result. */ | |
c0daf32d | 1104 | |
97f3003f | 1105 | static tree |
43574e4f | 1106 | get_fundef_copy (constexpr_fundef *fundef) |
c0daf32d | 1107 | { |
97f3003f | 1108 | tree copy; |
3c98ff9b | 1109 | bool existed; |
c89844e5 JM |
1110 | tree *slot = &(hash_map_safe_get_or_insert<hm_ggc> |
1111 | (fundef_copies_table, fundef->decl, &existed, 127)); | |
3c98ff9b NS |
1112 | |
1113 | if (!existed) | |
c0daf32d | 1114 | { |
3c98ff9b NS |
1115 | /* There is no cached function available, or in use. We can use |
1116 | the function directly. That the slot is now created records | |
1117 | that this function is now in use. */ | |
43574e4f JJ |
1118 | copy = build_tree_list (fundef->body, fundef->parms); |
1119 | TREE_TYPE (copy) = fundef->result; | |
3c98ff9b NS |
1120 | } |
1121 | else if (*slot == NULL_TREE) | |
1122 | { | |
1123 | /* We've already used the function itself, so make a copy. */ | |
97f3003f | 1124 | copy = build_tree_list (NULL, NULL); |
43574e4f JJ |
1125 | tree saved_body = DECL_SAVED_TREE (fundef->decl); |
1126 | tree saved_parms = DECL_ARGUMENTS (fundef->decl); | |
1127 | tree saved_result = DECL_RESULT (fundef->decl); | |
1128 | tree saved_fn = current_function_decl; | |
1129 | DECL_SAVED_TREE (fundef->decl) = fundef->body; | |
1130 | DECL_ARGUMENTS (fundef->decl) = fundef->parms; | |
1131 | DECL_RESULT (fundef->decl) = fundef->result; | |
1132 | current_function_decl = fundef->decl; | |
1133 | TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy), | |
1134 | TREE_TYPE (copy)); | |
1135 | current_function_decl = saved_fn; | |
1136 | DECL_RESULT (fundef->decl) = saved_result; | |
1137 | DECL_ARGUMENTS (fundef->decl) = saved_parms; | |
1138 | DECL_SAVED_TREE (fundef->decl) = saved_body; | |
c0daf32d PP |
1139 | } |
1140 | else | |
1141 | { | |
3c98ff9b | 1142 | /* We have a cached function available. */ |
c0daf32d | 1143 | copy = *slot; |
97f3003f | 1144 | *slot = TREE_CHAIN (copy); |
c0daf32d PP |
1145 | } |
1146 | ||
1147 | return copy; | |
1148 | } | |
1149 | ||
3c98ff9b NS |
1150 | /* Save the copy COPY of function FUN for later reuse by |
1151 | get_fundef_copy(). By construction, there will always be an entry | |
1152 | to find. */ | |
c0daf32d PP |
1153 | |
1154 | static void | |
97f3003f | 1155 | save_fundef_copy (tree fun, tree copy) |
c0daf32d | 1156 | { |
3c98ff9b | 1157 | tree *slot = fundef_copies_table->get (fun); |
97f3003f | 1158 | TREE_CHAIN (copy) = *slot; |
c0daf32d PP |
1159 | *slot = copy; |
1160 | } | |
1161 | ||
2d76680f PC |
1162 | /* We have an expression tree T that represents a call, either CALL_EXPR |
1163 | or AGGR_INIT_EXPR. Return the Nth argument. */ | |
1164 | ||
1165 | static inline tree | |
1166 | get_nth_callarg (tree t, int n) | |
1167 | { | |
1168 | switch (TREE_CODE (t)) | |
1169 | { | |
1170 | case CALL_EXPR: | |
1171 | return CALL_EXPR_ARG (t, n); | |
1172 | ||
1173 | case AGGR_INIT_EXPR: | |
1174 | return AGGR_INIT_EXPR_ARG (t, n); | |
1175 | ||
1176 | default: | |
1177 | gcc_unreachable (); | |
1178 | return NULL; | |
1179 | } | |
1180 | } | |
1181 | ||
2d76680f PC |
1182 | /* Attempt to evaluate T which represents a call to a builtin function. |
1183 | We assume here that all builtin functions evaluate to scalar types | |
1184 | represented by _CST nodes. */ | |
1185 | ||
1186 | static tree | |
5756d0f9 | 1187 | cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun, |
92a596e8 | 1188 | bool lval, |
2d76680f PC |
1189 | bool *non_constant_p, bool *overflow_p) |
1190 | { | |
1191 | const int nargs = call_expr_nargs (t); | |
1192 | tree *args = (tree *) alloca (nargs * sizeof (tree)); | |
1193 | tree new_call; | |
1194 | int i; | |
5756d0f9 JM |
1195 | |
1196 | /* Don't fold __builtin_constant_p within a constexpr function. */ | |
e4082611 | 1197 | bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun); |
b925d25d | 1198 | |
fe736ffd JM |
1199 | /* If we aren't requiring a constant expression, defer __builtin_constant_p |
1200 | in a constexpr function until we have values for the parameters. */ | |
b925d25d | 1201 | if (bi_const_p |
4cd3e7df | 1202 | && !ctx->manifestly_const_eval |
5756d0f9 JM |
1203 | && current_function_decl |
1204 | && DECL_DECLARED_CONSTEXPR_P (current_function_decl)) | |
2d76680f | 1205 | { |
5756d0f9 JM |
1206 | *non_constant_p = true; |
1207 | return t; | |
2d76680f | 1208 | } |
5756d0f9 | 1209 | |
e4082611 | 1210 | /* For __builtin_is_constant_evaluated, defer it if not |
13de99bc | 1211 | ctx->manifestly_const_eval, otherwise fold it to true. */ |
3d78e008 | 1212 | if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED, |
13de99bc | 1213 | BUILT_IN_FRONTEND)) |
e4082611 | 1214 | { |
13de99bc | 1215 | if (!ctx->manifestly_const_eval) |
e4082611 JJ |
1216 | { |
1217 | *non_constant_p = true; | |
1218 | return t; | |
1219 | } | |
1220 | return boolean_true_node; | |
1221 | } | |
1222 | ||
5756d0f9 JM |
1223 | /* Be permissive for arguments to built-ins; __builtin_constant_p should |
1224 | return constant false for a non-constant argument. */ | |
1225 | constexpr_ctx new_ctx = *ctx; | |
1226 | new_ctx.quiet = true; | |
5756d0f9 | 1227 | for (i = 0; i < nargs; ++i) |
b925d25d | 1228 | { |
2ff7172a JJ |
1229 | args[i] = CALL_EXPR_ARG (t, i); |
1230 | /* If builtin_valid_in_constant_expr_p is true, | |
1231 | potential_constant_expression_1 has not recursed into the arguments | |
1232 | of the builtin, verify it here. */ | |
1233 | if (!builtin_valid_in_constant_expr_p (fun) | |
1234 | || potential_constant_expression (args[i])) | |
4cd3e7df JJ |
1235 | { |
1236 | bool dummy1 = false, dummy2 = false; | |
1237 | args[i] = cxx_eval_constant_expression (&new_ctx, args[i], false, | |
1238 | &dummy1, &dummy2); | |
1239 | } | |
1240 | ||
b925d25d | 1241 | if (bi_const_p) |
4cd3e7df | 1242 | /* For __builtin_constant_p, fold all expressions with constant values |
b925d25d | 1243 | even if they aren't C++ constant-expressions. */ |
4cd3e7df | 1244 | args[i] = cp_fold_rvalue (args[i]); |
b925d25d | 1245 | } |
5756d0f9 JM |
1246 | |
1247 | bool save_ffbcp = force_folding_builtin_constant_p; | |
1248 | force_folding_builtin_constant_p = true; | |
43574e4f JJ |
1249 | tree save_cur_fn = current_function_decl; |
1250 | /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */ | |
1251 | if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION) | |
1252 | && ctx->call | |
1253 | && ctx->call->fundef) | |
1254 | current_function_decl = ctx->call->fundef->decl; | |
109d2197 JJ |
1255 | new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t), |
1256 | CALL_EXPR_FN (t), nargs, args); | |
43574e4f | 1257 | current_function_decl = save_cur_fn; |
5756d0f9 | 1258 | force_folding_builtin_constant_p = save_ffbcp; |
109d2197 JJ |
1259 | if (new_call == NULL) |
1260 | { | |
1261 | if (!*non_constant_p && !ctx->quiet) | |
1262 | { | |
1b6fa695 ML |
1263 | /* Do not allow__builtin_unreachable in constexpr function. |
1264 | The __builtin_unreachable call with BUILTINS_LOCATION | |
1265 | comes from cp_maybe_instrument_return. */ | |
3d78e008 | 1266 | if (fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE) |
1b6fa695 | 1267 | && EXPR_LOCATION (t) == BUILTINS_LOCATION) |
84fa214d | 1268 | error ("%<constexpr%> call flows off the end of the function"); |
1b6fa695 ML |
1269 | else |
1270 | { | |
1271 | new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t), | |
1272 | CALL_EXPR_FN (t), nargs, args); | |
1273 | error ("%q+E is not a constant expression", new_call); | |
1274 | } | |
109d2197 JJ |
1275 | } |
1276 | *non_constant_p = true; | |
1277 | return t; | |
1278 | } | |
1279 | ||
43574e4f | 1280 | if (!potential_constant_expression (new_call)) |
109d2197 JJ |
1281 | { |
1282 | if (!*non_constant_p && !ctx->quiet) | |
1283 | error ("%q+E is not a constant expression", new_call); | |
1284 | *non_constant_p = true; | |
1285 | return t; | |
1286 | } | |
1287 | ||
1288 | return cxx_eval_constant_expression (&new_ctx, new_call, lval, | |
1289 | non_constant_p, overflow_p); | |
2d76680f PC |
1290 | } |
1291 | ||
1292 | /* TEMP is the constant value of a temporary object of type TYPE. Adjust | |
1293 | the type of the value to match. */ | |
1294 | ||
1295 | static tree | |
1296 | adjust_temp_type (tree type, tree temp) | |
1297 | { | |
47be9509 | 1298 | if (same_type_p (TREE_TYPE (temp), type)) |
2d76680f PC |
1299 | return temp; |
1300 | /* Avoid wrapping an aggregate value in a NOP_EXPR. */ | |
1301 | if (TREE_CODE (temp) == CONSTRUCTOR) | |
b27f74e7 MP |
1302 | { |
1303 | /* build_constructor wouldn't retain various CONSTRUCTOR flags. */ | |
1304 | tree t = copy_node (temp); | |
1305 | TREE_TYPE (t) = type; | |
1306 | return t; | |
1307 | } | |
ff8ba86f JJ |
1308 | if (TREE_CODE (temp) == EMPTY_CLASS_EXPR) |
1309 | return build0 (EMPTY_CLASS_EXPR, type); | |
2d76680f | 1310 | gcc_assert (scalarish_type_p (type)); |
72b091f7 MP |
1311 | /* Now we know we're dealing with a scalar, and a prvalue of non-class |
1312 | type is cv-unqualified. */ | |
1313 | return cp_fold_convert (cv_unqualified (type), temp); | |
2d76680f PC |
1314 | } |
1315 | ||
9b9eb42a JM |
1316 | /* If T is a CONSTRUCTOR, return an unshared copy of T and any |
1317 | sub-CONSTRUCTORs. Otherwise return T. | |
0146e25f | 1318 | |
9b9eb42a JM |
1319 | We use this whenever we initialize an object as a whole, whether it's a |
1320 | parameter, a local variable, or a subobject, so that subsequent | |
1321 | modifications don't affect other places where it was used. */ | |
0146e25f | 1322 | |
1f6857ba | 1323 | tree |
3da7d774 | 1324 | unshare_constructor (tree t MEM_STAT_DECL) |
0146e25f | 1325 | { |
9b9eb42a JM |
1326 | if (!t || TREE_CODE (t) != CONSTRUCTOR) |
1327 | return t; | |
1328 | auto_vec <tree*, 4> ptrs; | |
1329 | ptrs.safe_push (&t); | |
1330 | while (!ptrs.is_empty ()) | |
1331 | { | |
1332 | tree *p = ptrs.pop (); | |
3da7d774 JM |
1333 | tree n = copy_node (*p PASS_MEM_STAT); |
1334 | CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT); | |
9b9eb42a JM |
1335 | *p = n; |
1336 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n); | |
1337 | constructor_elt *ce; | |
1338 | for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
1339 | if (TREE_CODE (ce->value) == CONSTRUCTOR) | |
1340 | ptrs.safe_push (&ce->value); | |
1341 | } | |
0146e25f PP |
1342 | return t; |
1343 | } | |
1344 | ||
620adbec JM |
1345 | /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */ |
1346 | ||
1347 | static void | |
1348 | free_constructor (tree t) | |
1349 | { | |
1350 | if (!t || TREE_CODE (t) != CONSTRUCTOR) | |
1351 | return; | |
1352 | releasing_vec ctors; | |
1353 | vec_safe_push (ctors, t); | |
1354 | while (!ctors->is_empty ()) | |
1355 | { | |
1356 | tree c = ctors->pop (); | |
1357 | if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c)) | |
1358 | { | |
1359 | constructor_elt *ce; | |
1360 | for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i) | |
1361 | if (TREE_CODE (ce->value) == CONSTRUCTOR) | |
1362 | vec_safe_push (ctors, ce->value); | |
1363 | ggc_free (elts); | |
1364 | } | |
1365 | ggc_free (c); | |
1366 | } | |
1367 | } | |
1368 | ||
2d76680f PC |
1369 | /* Subroutine of cxx_eval_call_expression. |
1370 | We are processing a call expression (either CALL_EXPR or | |
3e605b20 | 1371 | AGGR_INIT_EXPR) in the context of CTX. Evaluate |
2d76680f PC |
1372 | all arguments and bind their values to correspondings |
1373 | parameters, making up the NEW_CALL context. */ | |
1374 | ||
1375 | static void | |
3e605b20 | 1376 | cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, |
2d76680f | 1377 | constexpr_call *new_call, |
12d9ce19 JM |
1378 | bool *non_constant_p, bool *overflow_p, |
1379 | bool *non_constant_args) | |
2d76680f PC |
1380 | { |
1381 | const int nargs = call_expr_nargs (t); | |
1382 | tree fun = new_call->fundef->decl; | |
43574e4f | 1383 | tree parms = new_call->fundef->parms; |
2d76680f | 1384 | int i; |
3c961dc7 JM |
1385 | /* We don't record ellipsis args below. */ |
1386 | int nparms = list_length (parms); | |
1387 | int nbinds = nargs < nparms ? nargs : nparms; | |
1388 | tree binds = new_call->bindings = make_tree_vec (nbinds); | |
2d76680f PC |
1389 | for (i = 0; i < nargs; ++i) |
1390 | { | |
1391 | tree x, arg; | |
1392 | tree type = parms ? TREE_TYPE (parms) : void_type_node; | |
2d76680f | 1393 | x = get_nth_callarg (t, i); |
3e605b20 JM |
1394 | /* For member function, the first argument is a pointer to the implied |
1395 | object. For a constructor, it might still be a dummy object, in | |
60813a46 | 1396 | which case we get the real argument from ctx. */ |
3e605b20 JM |
1397 | if (i == 0 && DECL_CONSTRUCTOR_P (fun) |
1398 | && is_dummy_object (x)) | |
1399 | { | |
1400 | x = ctx->object; | |
84dd815f | 1401 | x = build_address (x); |
3e605b20 | 1402 | } |
43574e4f JJ |
1403 | if (TREE_ADDRESSABLE (type)) |
1404 | /* Undo convert_for_arg_passing work here. */ | |
1405 | x = convert_from_reference (x); | |
0e038601 | 1406 | arg = cxx_eval_constant_expression (ctx, x, /*lval=*/false, |
5a804683 | 1407 | non_constant_p, overflow_p); |
2d76680f | 1408 | /* Don't VERIFY_CONSTANT here. */ |
2b3ab879 | 1409 | if (*non_constant_p && ctx->quiet) |
2d76680f PC |
1410 | return; |
1411 | /* Just discard ellipsis args after checking their constantitude. */ | |
1412 | if (!parms) | |
1413 | continue; | |
4727d4c6 NS |
1414 | |
1415 | if (!*non_constant_p) | |
1416 | { | |
620adbec JM |
1417 | /* Unsharing here isn't necessary for correctness, but it |
1418 | significantly improves memory performance for some reason. */ | |
1dc23505 | 1419 | arg = unshare_constructor (arg); |
4727d4c6 NS |
1420 | /* Make sure the binding has the same type as the parm. But |
1421 | only for constant args. */ | |
9f613f06 | 1422 | if (!TYPE_REF_P (type)) |
4727d4c6 NS |
1423 | arg = adjust_temp_type (type, arg); |
1424 | if (!TREE_CONSTANT (arg)) | |
1425 | *non_constant_args = true; | |
3c961dc7 | 1426 | TREE_VEC_ELT (binds, i) = arg; |
4727d4c6 | 1427 | } |
2d76680f PC |
1428 | parms = TREE_CHAIN (parms); |
1429 | } | |
1430 | } | |
1431 | ||
1432 | /* Variables and functions to manage constexpr call expansion context. | |
1433 | These do not need to be marked for PCH or GC. */ | |
1434 | ||
1435 | /* FIXME remember and print actual constant arguments. */ | |
7de76362 | 1436 | static vec<tree> call_stack; |
2d76680f PC |
1437 | static int call_stack_tick; |
1438 | static int last_cx_error_tick; | |
1439 | ||
7ffc7de5 | 1440 | static int |
2d76680f PC |
1441 | push_cx_call_context (tree call) |
1442 | { | |
1443 | ++call_stack_tick; | |
1444 | if (!EXPR_HAS_LOCATION (call)) | |
1445 | SET_EXPR_LOCATION (call, input_location); | |
1446 | call_stack.safe_push (call); | |
7ffc7de5 JM |
1447 | int len = call_stack.length (); |
1448 | if (len > max_constexpr_depth) | |
2d76680f | 1449 | return false; |
7ffc7de5 | 1450 | return len; |
2d76680f PC |
1451 | } |
1452 | ||
1453 | static void | |
1454 | pop_cx_call_context (void) | |
1455 | { | |
1456 | ++call_stack_tick; | |
1457 | call_stack.pop (); | |
1458 | } | |
1459 | ||
1460 | vec<tree> | |
1461 | cx_error_context (void) | |
1462 | { | |
1463 | vec<tree> r = vNULL; | |
1464 | if (call_stack_tick != last_cx_error_tick | |
1465 | && !call_stack.is_empty ()) | |
1466 | r = call_stack; | |
1467 | last_cx_error_tick = call_stack_tick; | |
1468 | return r; | |
1469 | } | |
1470 | ||
44a845ca MS |
1471 | /* Evaluate a call T to a GCC internal function when possible and return |
1472 | the evaluated result or, under the control of CTX, give an error, set | |
1473 | NON_CONSTANT_P, and return the unevaluated call T otherwise. */ | |
1474 | ||
1475 | static tree | |
1476 | cxx_eval_internal_function (const constexpr_ctx *ctx, tree t, | |
1477 | bool lval, | |
1478 | bool *non_constant_p, bool *overflow_p) | |
1479 | { | |
1480 | enum tree_code opcode = ERROR_MARK; | |
1481 | ||
1482 | switch (CALL_EXPR_IFN (t)) | |
1483 | { | |
1484 | case IFN_UBSAN_NULL: | |
1485 | case IFN_UBSAN_BOUNDS: | |
1486 | case IFN_UBSAN_VPTR: | |
81fea426 | 1487 | case IFN_FALLTHROUGH: |
44a845ca MS |
1488 | return void_node; |
1489 | ||
1490 | case IFN_ADD_OVERFLOW: | |
1491 | opcode = PLUS_EXPR; | |
1492 | break; | |
1493 | case IFN_SUB_OVERFLOW: | |
1494 | opcode = MINUS_EXPR; | |
1495 | break; | |
1496 | case IFN_MUL_OVERFLOW: | |
1497 | opcode = MULT_EXPR; | |
1498 | break; | |
1499 | ||
e16f1cc7 JJ |
1500 | case IFN_LAUNDER: |
1501 | return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), | |
1502 | false, non_constant_p, overflow_p); | |
1503 | ||
d8fcab68 JJ |
1504 | case IFN_VEC_CONVERT: |
1505 | { | |
1506 | tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), | |
1507 | false, non_constant_p, | |
1508 | overflow_p); | |
1509 | if (TREE_CODE (arg) == VECTOR_CST) | |
1510 | return fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg); | |
1511 | else | |
1512 | { | |
1513 | *non_constant_p = true; | |
1514 | return t; | |
1515 | } | |
1516 | } | |
1517 | ||
44a845ca MS |
1518 | default: |
1519 | if (!ctx->quiet) | |
f9d0ca40 | 1520 | error_at (cp_expr_loc_or_input_loc (t), |
44a845ca MS |
1521 | "call to internal function %qE", t); |
1522 | *non_constant_p = true; | |
1523 | return t; | |
1524 | } | |
1525 | ||
1526 | /* Evaluate constant arguments using OPCODE and return a complex | |
1527 | number containing the result and the overflow bit. */ | |
1528 | tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval, | |
1529 | non_constant_p, overflow_p); | |
1530 | tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval, | |
1531 | non_constant_p, overflow_p); | |
1532 | ||
1533 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) | |
1534 | { | |
f9d0ca40 | 1535 | location_t loc = cp_expr_loc_or_input_loc (t); |
44a845ca MS |
1536 | tree type = TREE_TYPE (TREE_TYPE (t)); |
1537 | tree result = fold_binary_loc (loc, opcode, type, | |
1538 | fold_convert_loc (loc, type, arg0), | |
1539 | fold_convert_loc (loc, type, arg1)); | |
1540 | tree ovf | |
1541 | = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1)); | |
1542 | /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */ | |
1543 | if (TREE_OVERFLOW (result)) | |
1544 | TREE_OVERFLOW (result) = 0; | |
1545 | ||
1546 | return build_complex (TREE_TYPE (t), result, ovf); | |
1547 | } | |
1548 | ||
1549 | *non_constant_p = true; | |
1550 | return t; | |
1551 | } | |
1552 | ||
e8c48716 | 1553 | /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */ |
ecc57615 JM |
1554 | |
1555 | static void | |
1556 | clear_no_implicit_zero (tree ctor) | |
1557 | { | |
e8c48716 | 1558 | if (CONSTRUCTOR_NO_CLEARING (ctor)) |
ecc57615 | 1559 | { |
e8c48716 | 1560 | CONSTRUCTOR_NO_CLEARING (ctor) = false; |
ecc57615 JM |
1561 | tree elt; unsigned HOST_WIDE_INT idx; |
1562 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), idx, elt) | |
1563 | if (TREE_CODE (elt) == CONSTRUCTOR) | |
1564 | clear_no_implicit_zero (elt); | |
1565 | } | |
1566 | } | |
1567 | ||
04e1749c MP |
1568 | /* Complain about a const object OBJ being modified in a constant expression. |
1569 | EXPR is the MODIFY_EXPR expression performing the modification. */ | |
1570 | ||
1571 | static void | |
1572 | modifying_const_object_error (tree expr, tree obj) | |
1573 | { | |
1574 | location_t loc = cp_expr_loc_or_input_loc (expr); | |
1575 | auto_diagnostic_group d; | |
1576 | error_at (loc, "modifying a const object %qE is not allowed in " | |
1577 | "a constant expression", TREE_OPERAND (expr, 0)); | |
1578 | inform (location_of (obj), "originally declared %<const%> here"); | |
1579 | } | |
1580 | ||
2d76680f PC |
1581 | /* Subroutine of cxx_eval_constant_expression. |
1582 | Evaluate the call expression tree T in the context of OLD_CALL expression | |
1583 | evaluation. */ | |
1584 | ||
1585 | static tree | |
3e605b20 | 1586 | cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, |
92a596e8 | 1587 | bool lval, |
2d76680f PC |
1588 | bool *non_constant_p, bool *overflow_p) |
1589 | { | |
f9d0ca40 | 1590 | location_t loc = cp_expr_loc_or_input_loc (t); |
2d76680f | 1591 | tree fun = get_function_named_in_call (t); |
cce3ae91 | 1592 | constexpr_call new_call |
13de99bc | 1593 | = { NULL, NULL, NULL, 0, ctx->manifestly_const_eval }; |
7ffc7de5 | 1594 | int depth_ok; |
2d76680f | 1595 | |
0b274c17 | 1596 | if (fun == NULL_TREE) |
44a845ca MS |
1597 | return cxx_eval_internal_function (ctx, t, lval, |
1598 | non_constant_p, overflow_p); | |
0b274c17 | 1599 | |
2d76680f PC |
1600 | if (TREE_CODE (fun) != FUNCTION_DECL) |
1601 | { | |
1602 | /* Might be a constexpr function pointer. */ | |
2b3ab879 | 1603 | fun = cxx_eval_constant_expression (ctx, fun, |
92a596e8 | 1604 | /*lval*/false, non_constant_p, |
5a804683 | 1605 | overflow_p); |
2d76680f PC |
1606 | STRIP_NOPS (fun); |
1607 | if (TREE_CODE (fun) == ADDR_EXPR) | |
1608 | fun = TREE_OPERAND (fun, 0); | |
582d2481 JJ |
1609 | /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no |
1610 | indirection, the called expression is a pointer into the | |
1611 | virtual table which should contain FDESC_EXPR. Extract the | |
1612 | FUNCTION_DECL from there. */ | |
1613 | else if (TARGET_VTABLE_USES_DESCRIPTORS | |
1614 | && TREE_CODE (fun) == POINTER_PLUS_EXPR | |
1615 | && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR | |
1616 | && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST) | |
1617 | { | |
1618 | tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0); | |
1619 | if (VAR_P (d) | |
1620 | && DECL_VTABLE_OR_VTT_P (d) | |
1621 | && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE | |
1622 | && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type | |
1623 | && DECL_INITIAL (d) | |
1624 | && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR) | |
1625 | { | |
1626 | tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1), | |
1627 | TYPE_SIZE_UNIT (vtable_entry_type)); | |
1628 | HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i); | |
1629 | if (idx >= 0) | |
1630 | { | |
1631 | tree fdesc | |
1632 | = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value; | |
1633 | if (TREE_CODE (fdesc) == FDESC_EXPR | |
1634 | && integer_zerop (TREE_OPERAND (fdesc, 1))) | |
1635 | fun = TREE_OPERAND (fdesc, 0); | |
1636 | } | |
1637 | } | |
1638 | } | |
2d76680f PC |
1639 | } |
1640 | if (TREE_CODE (fun) != FUNCTION_DECL) | |
1641 | { | |
2b3ab879 | 1642 | if (!ctx->quiet && !*non_constant_p) |
84fa214d | 1643 | error_at (loc, "expression %qE does not designate a %<constexpr%> " |
2d76680f PC |
1644 | "function", fun); |
1645 | *non_constant_p = true; | |
1646 | return t; | |
1647 | } | |
1648 | if (DECL_CLONED_FUNCTION_P (fun)) | |
1649 | fun = DECL_CLONED_FUNCTION (fun); | |
0b274c17 MP |
1650 | |
1651 | if (is_ubsan_builtin_p (fun)) | |
1652 | return void_node; | |
1653 | ||
3d78e008 | 1654 | if (fndecl_built_in_p (fun)) |
5756d0f9 | 1655 | return cxx_eval_builtin_function_call (ctx, t, fun, |
92a596e8 | 1656 | lval, non_constant_p, overflow_p); |
2d76680f PC |
1657 | if (!DECL_DECLARED_CONSTEXPR_P (fun)) |
1658 | { | |
2b3ab879 | 1659 | if (!ctx->quiet) |
2d76680f | 1660 | { |
11399477 | 1661 | if (!lambda_static_thunk_p (fun)) |
84fa214d | 1662 | error_at (loc, "call to non-%<constexpr%> function %qD", fun); |
2d76680f PC |
1663 | explain_invalid_constexpr_fn (fun); |
1664 | } | |
1665 | *non_constant_p = true; | |
1666 | return t; | |
1667 | } | |
1668 | ||
86461cad JM |
1669 | constexpr_ctx new_ctx = *ctx; |
1670 | if (DECL_CONSTRUCTOR_P (fun) && !ctx->object | |
1671 | && TREE_CODE (t) == AGGR_INIT_EXPR) | |
1672 | { | |
1673 | /* We want to have an initialization target for an AGGR_INIT_EXPR. | |
1674 | If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */ | |
1675 | new_ctx.object = AGGR_INIT_EXPR_SLOT (t); | |
1676 | tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL); | |
e8c48716 | 1677 | CONSTRUCTOR_NO_CLEARING (ctor) = true; |
86461cad JM |
1678 | ctx->values->put (new_ctx.object, ctor); |
1679 | ctx = &new_ctx; | |
1680 | } | |
1681 | ||
2d76680f PC |
1682 | /* Shortcut trivial constructor/op=. */ |
1683 | if (trivial_fn_p (fun)) | |
1684 | { | |
86461cad | 1685 | tree init = NULL_TREE; |
2d76680f | 1686 | if (call_expr_nargs (t) == 2) |
86461cad | 1687 | init = convert_from_reference (get_nth_callarg (t, 1)); |
2d76680f PC |
1688 | else if (TREE_CODE (t) == AGGR_INIT_EXPR |
1689 | && AGGR_INIT_ZERO_FIRST (t)) | |
86461cad JM |
1690 | init = build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false); |
1691 | if (init) | |
1692 | { | |
1693 | tree op = get_nth_callarg (t, 0); | |
1694 | if (is_dummy_object (op)) | |
1695 | op = ctx->object; | |
1696 | else | |
1697 | op = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (op)), op); | |
1698 | tree set = build2 (MODIFY_EXPR, TREE_TYPE (op), op, init); | |
c8816908 JM |
1699 | new_ctx.call = &new_call; |
1700 | return cxx_eval_constant_expression (&new_ctx, set, lval, | |
86461cad JM |
1701 | non_constant_p, overflow_p); |
1702 | } | |
2d76680f PC |
1703 | } |
1704 | ||
81371eff JM |
1705 | /* We can't defer instantiating the function any longer. */ |
1706 | if (!DECL_INITIAL (fun) | |
1707 | && DECL_TEMPLOID_INSTANTIATION (fun)) | |
1708 | { | |
f065303f JM |
1709 | location_t save_loc = input_location; |
1710 | input_location = loc; | |
81371eff JM |
1711 | ++function_depth; |
1712 | instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false); | |
1713 | --function_depth; | |
f065303f | 1714 | input_location = save_loc; |
81371eff JM |
1715 | } |
1716 | ||
2d76680f | 1717 | /* If in direct recursive call, optimize definition search. */ |
c8816908 | 1718 | if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun) |
3e605b20 | 1719 | new_call.fundef = ctx->call->fundef; |
2d76680f PC |
1720 | else |
1721 | { | |
1722 | new_call.fundef = retrieve_constexpr_fundef (fun); | |
4ddcdbf9 JM |
1723 | if (new_call.fundef == NULL || new_call.fundef->body == NULL |
1724 | || fun == current_function_decl) | |
2d76680f | 1725 | { |
2b3ab879 | 1726 | if (!ctx->quiet) |
2d76680f | 1727 | { |
4ddcdbf9 JM |
1728 | /* We need to check for current_function_decl here in case we're |
1729 | being called during cp_fold_function, because at that point | |
1730 | DECL_INITIAL is set properly and we have a fundef but we | |
1731 | haven't lowered invisirefs yet (c++/70344). */ | |
1732 | if (DECL_INITIAL (fun) == error_mark_node | |
1733 | || fun == current_function_decl) | |
ddd6d421 JM |
1734 | error_at (loc, "%qD called in a constant expression before its " |
1735 | "definition is complete", fun); | |
1736 | else if (DECL_INITIAL (fun)) | |
2d76680f | 1737 | { |
98e5a19a JM |
1738 | /* The definition of fun was somehow unsuitable. But pretend |
1739 | that lambda static thunks don't exist. */ | |
1740 | if (!lambda_static_thunk_p (fun)) | |
1741 | error_at (loc, "%qD called in a constant expression", fun); | |
2d76680f PC |
1742 | explain_invalid_constexpr_fn (fun); |
1743 | } | |
1744 | else | |
1745 | error_at (loc, "%qD used before its definition", fun); | |
1746 | } | |
1747 | *non_constant_p = true; | |
1748 | return t; | |
1749 | } | |
1750 | } | |
60813a46 | 1751 | |
12d9ce19 | 1752 | bool non_constant_args = false; |
3e605b20 | 1753 | cxx_bind_parameters_in_call (ctx, t, &new_call, |
12d9ce19 | 1754 | non_constant_p, overflow_p, &non_constant_args); |
ecdcd560 JM |
1755 | |
1756 | /* We build up the bindings list before we know whether we already have this | |
1757 | call cached. If we don't end up saving these bindings, ggc_free them when | |
1758 | this function exits. */ | |
6c1dae73 | 1759 | class free_bindings |
ecdcd560 | 1760 | { |
6c1dae73 | 1761 | public: |
ecdcd560 JM |
1762 | tree &bindings; |
1763 | bool do_free; | |
1764 | free_bindings (tree &b): bindings (b), do_free(true) { } | |
1765 | void preserve () { do_free = false; } | |
1766 | ~free_bindings () { | |
1767 | if (do_free) | |
620adbec JM |
1768 | { |
1769 | for (int i = 0; i < TREE_VEC_LENGTH (bindings); ++i) | |
1770 | free_constructor (TREE_VEC_ELT (bindings, i)); | |
1771 | ggc_free (bindings); | |
1772 | } | |
ecdcd560 JM |
1773 | } |
1774 | } fb (new_call.bindings); | |
1775 | ||
2d76680f PC |
1776 | if (*non_constant_p) |
1777 | return t; | |
1778 | ||
1779 | depth_ok = push_cx_call_context (t); | |
1780 | ||
04e1749c MP |
1781 | /* Remember the object we are constructing. */ |
1782 | tree new_obj = NULL_TREE; | |
1783 | if (DECL_CONSTRUCTOR_P (fun)) | |
1784 | { | |
1785 | /* In a constructor, it should be the first `this' argument. | |
1786 | At this point it has already been evaluated in the call | |
1787 | to cxx_bind_parameters_in_call. */ | |
1788 | new_obj = TREE_VEC_ELT (new_call.bindings, 0); | |
1789 | STRIP_NOPS (new_obj); | |
1790 | if (TREE_CODE (new_obj) == ADDR_EXPR) | |
1791 | new_obj = TREE_OPERAND (new_obj, 0); | |
1792 | } | |
1793 | ||
12d9ce19 | 1794 | tree result = NULL_TREE; |
2d76680f | 1795 | |
12d9ce19 | 1796 | constexpr_call *entry = NULL; |
4a58d2fe | 1797 | if (depth_ok && !non_constant_args && ctx->strict) |
2d76680f | 1798 | { |
cce3ae91 JJ |
1799 | new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef); |
1800 | new_call.hash | |
1801 | = iterative_hash_template_arg (new_call.bindings, new_call.hash); | |
1802 | new_call.hash | |
13de99bc | 1803 | = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash); |
12d9ce19 JM |
1804 | |
1805 | /* If we have seen this call before, we are done. */ | |
1806 | maybe_initialize_constexpr_call_table (); | |
1807 | constexpr_call **slot | |
1808 | = constexpr_call_table->find_slot (&new_call, INSERT); | |
1809 | entry = *slot; | |
1810 | if (entry == NULL) | |
1811 | { | |
7ffc7de5 JM |
1812 | /* Only cache up to constexpr_cache_depth to limit memory use. */ |
1813 | if (depth_ok < constexpr_cache_depth) | |
1814 | { | |
1815 | /* We need to keep a pointer to the entry, not just the slot, as | |
1816 | the slot can move during evaluation of the body. */ | |
1817 | *slot = entry = ggc_alloc<constexpr_call> (); | |
1818 | *entry = new_call; | |
1819 | fb.preserve (); | |
1820 | } | |
12d9ce19 | 1821 | } |
7ffc7de5 JM |
1822 | /* Calls that are in progress have their result set to NULL, so that we |
1823 | can detect circular dependencies. Now that we only cache up to | |
1824 | constexpr_cache_depth this won't catch circular dependencies that | |
1825 | start deeper, but they'll hit the recursion or ops limit. */ | |
12d9ce19 JM |
1826 | else if (entry->result == NULL) |
1827 | { | |
1828 | if (!ctx->quiet) | |
1829 | error ("call has circular dependency"); | |
1830 | *non_constant_p = true; | |
1831 | entry->result = result = error_mark_node; | |
1832 | } | |
1833 | else | |
1834 | result = entry->result; | |
2d76680f PC |
1835 | } |
1836 | ||
1837 | if (!depth_ok) | |
1838 | { | |
2b3ab879 | 1839 | if (!ctx->quiet) |
84fa214d | 1840 | error ("%<constexpr%> evaluation depth exceeds maximum of %d (use " |
7e7a6ed7 | 1841 | "%<-fconstexpr-depth=%> to increase the maximum)", |
2d76680f PC |
1842 | max_constexpr_depth); |
1843 | *non_constant_p = true; | |
12d9ce19 | 1844 | result = error_mark_node; |
2d76680f PC |
1845 | } |
1846 | else | |
1847 | { | |
de54de93 JM |
1848 | if (result && result != error_mark_node) |
1849 | /* OK */; | |
1850 | else if (!DECL_SAVED_TREE (fun)) | |
1851 | { | |
1852 | /* When at_eof >= 2, cgraph has started throwing away | |
1853 | DECL_SAVED_TREE, so fail quietly. FIXME we get here because of | |
1854 | late code generation for VEC_INIT_EXPR, which needs to be | |
1855 | completely reconsidered. */ | |
1856 | gcc_assert (at_eof >= 2 && ctx->quiet); | |
1857 | *non_constant_p = true; | |
1858 | } | |
1859 | else | |
3e605b20 | 1860 | { |
c0daf32d | 1861 | tree body, parms, res; |
620adbec | 1862 | releasing_vec ctors; |
0567dcd2 | 1863 | |
c0daf32d | 1864 | /* Reuse or create a new unshared copy of this function's body. */ |
43574e4f | 1865 | tree copy = get_fundef_copy (new_call.fundef); |
97f3003f JM |
1866 | body = TREE_PURPOSE (copy); |
1867 | parms = TREE_VALUE (copy); | |
1868 | res = TREE_TYPE (copy); | |
0567dcd2 MP |
1869 | |
1870 | /* Associate the bindings with the remapped parms. */ | |
1871 | tree bound = new_call.bindings; | |
1872 | tree remapped = parms; | |
3c961dc7 | 1873 | for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i) |
60813a46 | 1874 | { |
3c961dc7 | 1875 | tree arg = TREE_VEC_ELT (bound, i); |
7f0e23e9 | 1876 | /* Don't share a CONSTRUCTOR that might be changed. */ |
0146e25f | 1877 | arg = unshare_constructor (arg); |
620adbec JM |
1878 | if (TREE_CODE (arg) == CONSTRUCTOR) |
1879 | vec_safe_push (ctors, arg); | |
0567dcd2 | 1880 | ctx->values->put (remapped, arg); |
0567dcd2 | 1881 | remapped = DECL_CHAIN (remapped); |
60813a46 | 1882 | } |
0567dcd2 MP |
1883 | /* Add the RESULT_DECL to the values map, too. */ |
1884 | tree slot = NULL_TREE; | |
1885 | if (DECL_BY_REFERENCE (res)) | |
60813a46 | 1886 | { |
0567dcd2 MP |
1887 | slot = AGGR_INIT_EXPR_SLOT (t); |
1888 | tree addr = build_address (slot); | |
1889 | addr = build_nop (TREE_TYPE (res), addr); | |
1890 | ctx->values->put (res, addr); | |
1891 | ctx->values->put (slot, NULL_TREE); | |
1892 | } | |
1893 | else | |
1894 | ctx->values->put (res, NULL_TREE); | |
60813a46 | 1895 | |
c0daf32d PP |
1896 | /* Track the callee's evaluated SAVE_EXPRs so that we can forget |
1897 | their values after the call. */ | |
1898 | constexpr_ctx ctx_with_save_exprs = *ctx; | |
0ee28590 | 1899 | auto_vec<tree, 10> save_exprs; |
c0daf32d | 1900 | ctx_with_save_exprs.save_exprs = &save_exprs; |
1e163090 | 1901 | ctx_with_save_exprs.call = &new_call; |
c0daf32d | 1902 | |
0567dcd2 | 1903 | tree jump_target = NULL_TREE; |
c0daf32d | 1904 | cxx_eval_constant_expression (&ctx_with_save_exprs, body, |
0567dcd2 MP |
1905 | lval, non_constant_p, overflow_p, |
1906 | &jump_target); | |
60813a46 | 1907 | |
0567dcd2 MP |
1908 | if (DECL_CONSTRUCTOR_P (fun)) |
1909 | /* This can be null for a subobject constructor call, in | |
1910 | which case what we care about is the initialization | |
1911 | side-effects rather than the value. We could get at the | |
1912 | value by evaluating *this, but we don't bother; there's | |
1913 | no need to put such a call in the hash table. */ | |
1914 | result = lval ? ctx->object : ctx->ctor; | |
1915 | else if (VOID_TYPE_P (TREE_TYPE (res))) | |
1916 | result = void_node; | |
1917 | else | |
1918 | { | |
1919 | result = *ctx->values->get (slot ? slot : res); | |
1920 | if (result == NULL_TREE && !*non_constant_p) | |
60813a46 | 1921 | { |
0567dcd2 | 1922 | if (!ctx->quiet) |
84fa214d | 1923 | error ("%<constexpr%> call flows off the end " |
0567dcd2 MP |
1924 | "of the function"); |
1925 | *non_constant_p = true; | |
60813a46 | 1926 | } |
60813a46 | 1927 | } |
0567dcd2 | 1928 | |
04e1749c MP |
1929 | /* At this point, the object's constructor will have run, so |
1930 | the object is no longer under construction, and its possible | |
1931 | 'const' semantics now apply. Make a note of this fact by | |
1932 | marking the CONSTRUCTOR TREE_READONLY. */ | |
1933 | if (new_obj | |
1934 | && CLASS_TYPE_P (TREE_TYPE (new_obj)) | |
1935 | && CP_TYPE_CONST_P (TREE_TYPE (new_obj))) | |
1936 | { | |
1937 | /* Subobjects might not be stored in ctx->values but we can | |
1938 | get its CONSTRUCTOR by evaluating *this. */ | |
1939 | tree e = cxx_eval_constant_expression (ctx, new_obj, | |
1940 | /*lval*/false, | |
1941 | non_constant_p, | |
1942 | overflow_p); | |
1943 | TREE_READONLY (e) = true; | |
1944 | } | |
1945 | ||
c0daf32d | 1946 | /* Forget the saved values of the callee's SAVE_EXPRs. */ |
0ee28590 JJ |
1947 | unsigned int i; |
1948 | tree save_expr; | |
1949 | FOR_EACH_VEC_ELT (save_exprs, i, save_expr) | |
1950 | ctx_with_save_exprs.values->remove (save_expr); | |
c0daf32d | 1951 | |
0567dcd2 MP |
1952 | /* Remove the parms/result from the values map. Is it worth |
1953 | bothering to do this when the map itself is only live for | |
1954 | one constexpr evaluation? If so, maybe also clear out | |
1955 | other vars from call, maybe in BIND_EXPR handling? */ | |
1956 | ctx->values->remove (res); | |
1957 | if (slot) | |
1958 | ctx->values->remove (slot); | |
1959 | for (tree parm = parms; parm; parm = TREE_CHAIN (parm)) | |
1960 | ctx->values->remove (parm); | |
c0daf32d | 1961 | |
620adbec JM |
1962 | /* Free any parameter CONSTRUCTORs we aren't returning directly. */ |
1963 | while (!ctors->is_empty ()) | |
1964 | { | |
1965 | tree c = ctors->pop (); | |
1966 | if (c != result) | |
1967 | free_constructor (c); | |
1968 | } | |
1969 | ||
c0daf32d PP |
1970 | /* Make the unshared function copy we used available for re-use. */ |
1971 | save_fundef_copy (fun, copy); | |
3e605b20 | 1972 | } |
60813a46 | 1973 | |
2d76680f PC |
1974 | if (result == error_mark_node) |
1975 | *non_constant_p = true; | |
52228180 | 1976 | if (*non_constant_p || *overflow_p) |
12d9ce19 | 1977 | result = error_mark_node; |
0567dcd2 | 1978 | else if (!result) |
60813a46 | 1979 | result = void_node; |
12d9ce19 JM |
1980 | if (entry) |
1981 | entry->result = result; | |
2d76680f PC |
1982 | } |
1983 | ||
f64e0c02 JM |
1984 | /* The result of a constexpr function must be completely initialized. */ |
1985 | if (TREE_CODE (result) == CONSTRUCTOR) | |
ecc57615 | 1986 | clear_no_implicit_zero (result); |
f64e0c02 | 1987 | |
2d76680f | 1988 | pop_cx_call_context (); |
9b9eb42a | 1989 | return result; |
2d76680f PC |
1990 | } |
1991 | ||
1992 | /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */ | |
1993 | ||
1994 | bool | |
1995 | reduced_constant_expression_p (tree t) | |
1996 | { | |
4930c53e MP |
1997 | if (t == NULL_TREE) |
1998 | return false; | |
1999 | ||
2d76680f PC |
2000 | switch (TREE_CODE (t)) |
2001 | { | |
2002 | case PTRMEM_CST: | |
2003 | /* Even if we can't lower this yet, it's constant. */ | |
2004 | return true; | |
2005 | ||
2006 | case CONSTRUCTOR: | |
2007 | /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */ | |
7368cfa4 | 2008 | tree idx, val, field; unsigned HOST_WIDE_INT i; |
e8c48716 | 2009 | if (CONSTRUCTOR_NO_CLEARING (t)) |
6f11ddd8 JM |
2010 | { |
2011 | if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE) | |
2012 | /* An initialized vector would have a VECTOR_CST. */ | |
2013 | return false; | |
2014 | else | |
2015 | field = next_initializable_field (TYPE_FIELDS (TREE_TYPE (t))); | |
2016 | } | |
7368cfa4 JM |
2017 | else |
2018 | field = NULL_TREE; | |
2019 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, idx, val) | |
125db6a1 | 2020 | { |
4930c53e MP |
2021 | /* If VAL is null, we're in the middle of initializing this |
2022 | element. */ | |
7368cfa4 | 2023 | if (!reduced_constant_expression_p (val)) |
125db6a1 | 2024 | return false; |
7368cfa4 JM |
2025 | if (field) |
2026 | { | |
2027 | if (idx != field) | |
2028 | return false; | |
2029 | field = next_initializable_field (DECL_CHAIN (field)); | |
2030 | } | |
125db6a1 | 2031 | } |
7368cfa4 JM |
2032 | if (field) |
2033 | return false; | |
e8c48716 | 2034 | else if (CONSTRUCTOR_NO_CLEARING (t)) |
7368cfa4 | 2035 | /* All the fields are initialized. */ |
e8c48716 | 2036 | CONSTRUCTOR_NO_CLEARING (t) = false; |
2d76680f PC |
2037 | return true; |
2038 | ||
2039 | default: | |
2040 | /* FIXME are we calling this too much? */ | |
2041 | return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE; | |
2042 | } | |
2043 | } | |
2044 | ||
2045 | /* Some expressions may have constant operands but are not constant | |
7d75ea04 JJ |
2046 | themselves, such as 1/0. Call this function to check for that |
2047 | condition. | |
2d76680f PC |
2048 | |
2049 | We only call this in places that require an arithmetic constant, not in | |
2050 | places where we might have a non-constant expression that can be a | |
2051 | component of a constant expression, such as the address of a constexpr | |
2052 | variable that might be dereferenced later. */ | |
2053 | ||
2054 | static bool | |
2055 | verify_constant (tree t, bool allow_non_constant, bool *non_constant_p, | |
2056 | bool *overflow_p) | |
2057 | { | |
2058 | if (!*non_constant_p && !reduced_constant_expression_p (t)) | |
2059 | { | |
2060 | if (!allow_non_constant) | |
2061 | error ("%q+E is not a constant expression", t); | |
2062 | *non_constant_p = true; | |
2063 | } | |
2064 | if (TREE_OVERFLOW_P (t)) | |
2065 | { | |
2066 | if (!allow_non_constant) | |
2067 | { | |
2068 | permerror (input_location, "overflow in constant expression"); | |
2069 | /* If we're being permissive (and are in an enforcing | |
2070 | context), ignore the overflow. */ | |
2071 | if (flag_permissive) | |
2072 | return *non_constant_p; | |
2073 | } | |
2074 | *overflow_p = true; | |
2075 | } | |
2076 | return *non_constant_p; | |
2077 | } | |
2078 | ||
253a921b MP |
2079 | /* Check whether the shift operation with code CODE and type TYPE on LHS |
2080 | and RHS is undefined. If it is, give an error with an explanation, | |
2081 | and return true; return false otherwise. */ | |
2082 | ||
2083 | static bool | |
2084 | cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx, | |
2085 | enum tree_code code, tree type, tree lhs, tree rhs) | |
2086 | { | |
2087 | if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR) | |
2088 | || TREE_CODE (lhs) != INTEGER_CST | |
2089 | || TREE_CODE (rhs) != INTEGER_CST) | |
2090 | return false; | |
2091 | ||
2092 | tree lhstype = TREE_TYPE (lhs); | |
2093 | unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs)); | |
2094 | ||
2095 | /* [expr.shift] The behavior is undefined if the right operand | |
2096 | is negative, or greater than or equal to the length in bits | |
2097 | of the promoted left operand. */ | |
2098 | if (tree_int_cst_sgn (rhs) == -1) | |
2099 | { | |
2100 | if (!ctx->quiet) | |
5342156c MP |
2101 | permerror (loc, "right operand of shift expression %q+E is negative", |
2102 | build2_loc (loc, code, type, lhs, rhs)); | |
2103 | return (!flag_permissive || ctx->quiet); | |
253a921b MP |
2104 | } |
2105 | if (compare_tree_int (rhs, uprec) >= 0) | |
2106 | { | |
2107 | if (!ctx->quiet) | |
a9c697b8 MS |
2108 | permerror (loc, "right operand of shift expression %q+E is greater " |
2109 | "than or equal to the precision %wu of the left operand", | |
2110 | build2_loc (loc, code, type, lhs, rhs), uprec); | |
5342156c | 2111 | return (!flag_permissive || ctx->quiet); |
253a921b MP |
2112 | } |
2113 | ||
2114 | /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...] | |
2115 | if E1 has a signed type and non-negative value, and E1x2^E2 is | |
2116 | representable in the corresponding unsigned type of the result type, | |
2117 | then that value, converted to the result type, is the resulting value; | |
8ee09943 JJ |
2118 | otherwise, the behavior is undefined. |
2119 | For C++2a: | |
2120 | The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo | |
2121 | 2^N, where N is the range exponent of the type of the result. */ | |
2122 | if (code == LSHIFT_EXPR | |
2123 | && !TYPE_UNSIGNED (lhstype) | |
2124 | && cxx_dialect >= cxx11 | |
2125 | && cxx_dialect < cxx2a) | |
253a921b MP |
2126 | { |
2127 | if (tree_int_cst_sgn (lhs) == -1) | |
2128 | { | |
2129 | if (!ctx->quiet) | |
5342156c MP |
2130 | permerror (loc, |
2131 | "left operand of shift expression %q+E is negative", | |
2132 | build2_loc (loc, code, type, lhs, rhs)); | |
2133 | return (!flag_permissive || ctx->quiet); | |
253a921b MP |
2134 | } |
2135 | /* For signed x << y the following: | |
2136 | (unsigned) x >> ((prec (lhs) - 1) - y) | |
2137 | if > 1, is undefined. The right-hand side of this formula | |
2138 | is the highest bit of the LHS that can be set (starting from 0), | |
2139 | so that the shift doesn't overflow. We then right-shift the LHS | |
2140 | to see whether any other bit is set making the original shift | |
2141 | undefined -- the result is not representable in the corresponding | |
2142 | unsigned type. */ | |
2143 | tree t = build_int_cst (unsigned_type_node, uprec - 1); | |
2144 | t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs); | |
2145 | tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs); | |
2146 | t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t); | |
2147 | if (tree_int_cst_lt (integer_one_node, t)) | |
2148 | { | |
2149 | if (!ctx->quiet) | |
5342156c MP |
2150 | permerror (loc, "shift expression %q+E overflows", |
2151 | build2_loc (loc, code, type, lhs, rhs)); | |
2152 | return (!flag_permissive || ctx->quiet); | |
253a921b MP |
2153 | } |
2154 | } | |
2155 | return false; | |
2156 | } | |
2157 | ||
2d76680f PC |
2158 | /* Subroutine of cxx_eval_constant_expression. |
2159 | Attempt to reduce the unary expression tree T to a compile time value. | |
2160 | If successful, return the value. Otherwise issue a diagnostic | |
2161 | and return error_mark_node. */ | |
2162 | ||
2163 | static tree | |
3e605b20 | 2164 | cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t, |
12d9ce19 | 2165 | bool /*lval*/, |
2d76680f PC |
2166 | bool *non_constant_p, bool *overflow_p) |
2167 | { | |
2168 | tree r; | |
2169 | tree orig_arg = TREE_OPERAND (t, 0); | |
12d9ce19 JM |
2170 | tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false, |
2171 | non_constant_p, overflow_p); | |
2d76680f | 2172 | VERIFY_CONSTANT (arg); |
f899317e JM |
2173 | location_t loc = EXPR_LOCATION (t); |
2174 | enum tree_code code = TREE_CODE (t); | |
2175 | tree type = TREE_TYPE (t); | |
2176 | r = fold_unary_loc (loc, code, type, arg); | |
2177 | if (r == NULL_TREE) | |
2178 | { | |
2179 | if (arg == orig_arg) | |
2180 | r = t; | |
2181 | else | |
2182 | r = build1_loc (loc, code, type, arg); | |
2183 | } | |
2d76680f PC |
2184 | VERIFY_CONSTANT (r); |
2185 | return r; | |
2186 | } | |
2187 | ||
ea8661cd JJ |
2188 | /* Helper function for cxx_eval_binary_expression. Try to optimize |
2189 | original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the | |
2190 | generic folding should be used. */ | |
2191 | ||
2192 | static tree | |
2193 | cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t, | |
2194 | tree lhs, tree rhs, bool *non_constant_p, | |
2195 | bool *overflow_p) | |
2196 | { | |
2197 | STRIP_NOPS (lhs); | |
2198 | if (TREE_CODE (lhs) != ADDR_EXPR) | |
2199 | return NULL_TREE; | |
2200 | ||
2201 | lhs = TREE_OPERAND (lhs, 0); | |
2202 | ||
2203 | /* &A[i] p+ j => &A[i + j] */ | |
2204 | if (TREE_CODE (lhs) == ARRAY_REF | |
2205 | && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST | |
2206 | && TREE_CODE (rhs) == INTEGER_CST | |
2207 | && TYPE_SIZE_UNIT (TREE_TYPE (lhs)) | |
2208 | && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST) | |
2209 | { | |
2210 | tree orig_type = TREE_TYPE (t); | |
2211 | location_t loc = EXPR_LOCATION (t); | |
2212 | tree type = TREE_TYPE (lhs); | |
2213 | ||
2214 | t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1)); | |
2215 | tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0))); | |
2216 | nelts = cxx_eval_constant_expression (ctx, nelts, false, non_constant_p, | |
2217 | overflow_p); | |
2218 | if (*non_constant_p) | |
2219 | return NULL_TREE; | |
2220 | /* Don't fold an out-of-bound access. */ | |
2221 | if (!tree_int_cst_le (t, nelts)) | |
2222 | return NULL_TREE; | |
2223 | rhs = cp_fold_convert (ssizetype, rhs); | |
2224 | /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT. | |
2225 | constexpr int A[1]; ... (char *)&A[0] + 1 */ | |
2226 | if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype, | |
2227 | rhs, TYPE_SIZE_UNIT (type)))) | |
2228 | return NULL_TREE; | |
2229 | /* Make sure to treat the second operand of POINTER_PLUS_EXPR | |
2230 | as signed. */ | |
2231 | rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs, | |
2232 | TYPE_SIZE_UNIT (type)); | |
2233 | t = size_binop_loc (loc, PLUS_EXPR, rhs, t); | |
2234 | t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0), | |
2235 | t, NULL_TREE, NULL_TREE); | |
2236 | t = cp_build_addr_expr (t, tf_warning_or_error); | |
2237 | t = cp_fold_convert (orig_type, t); | |
2238 | return cxx_eval_constant_expression (ctx, t, /*lval*/false, | |
2239 | non_constant_p, overflow_p); | |
2240 | } | |
2241 | ||
2242 | return NULL_TREE; | |
2243 | } | |
2244 | ||
2d76680f PC |
2245 | /* Subroutine of cxx_eval_constant_expression. |
2246 | Like cxx_eval_unary_expression, except for binary expressions. */ | |
2247 | ||
2248 | static tree | |
3e605b20 | 2249 | cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t, |
12d9ce19 | 2250 | bool /*lval*/, |
2d76680f PC |
2251 | bool *non_constant_p, bool *overflow_p) |
2252 | { | |
618d6c1c | 2253 | tree r = NULL_TREE; |
2d76680f PC |
2254 | tree orig_lhs = TREE_OPERAND (t, 0); |
2255 | tree orig_rhs = TREE_OPERAND (t, 1); | |
2256 | tree lhs, rhs; | |
12d9ce19 | 2257 | lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false, |
5a804683 | 2258 | non_constant_p, overflow_p); |
c8a66fc9 JM |
2259 | /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer |
2260 | subtraction. */ | |
2261 | if (*non_constant_p) | |
2262 | return t; | |
12d9ce19 | 2263 | rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false, |
5a804683 | 2264 | non_constant_p, overflow_p); |
c8a66fc9 JM |
2265 | if (*non_constant_p) |
2266 | return t; | |
f899317e JM |
2267 | |
2268 | location_t loc = EXPR_LOCATION (t); | |
2269 | enum tree_code code = TREE_CODE (t); | |
2270 | tree type = TREE_TYPE (t); | |
618d6c1c PP |
2271 | |
2272 | if (code == EQ_EXPR || code == NE_EXPR) | |
2273 | { | |
2274 | bool is_code_eq = (code == EQ_EXPR); | |
2275 | ||
2276 | if (TREE_CODE (lhs) == PTRMEM_CST | |
2277 | && TREE_CODE (rhs) == PTRMEM_CST) | |
9b0607de JM |
2278 | { |
2279 | tree lmem = PTRMEM_CST_MEMBER (lhs); | |
2280 | tree rmem = PTRMEM_CST_MEMBER (rhs); | |
2281 | bool eq; | |
2282 | if (TREE_CODE (lmem) == TREE_CODE (rmem) | |
2283 | && TREE_CODE (lmem) == FIELD_DECL | |
2284 | && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE | |
2285 | && same_type_p (DECL_CONTEXT (lmem), | |
2286 | DECL_CONTEXT (rmem))) | |
2287 | /* If both refer to (possibly different) members of the same union | |
2288 | (12.3), they compare equal. */ | |
2289 | eq = true; | |
2290 | else | |
2291 | eq = cp_tree_equal (lhs, rhs); | |
2292 | r = constant_boolean_node (eq == is_code_eq, type); | |
2293 | } | |
618d6c1c PP |
2294 | else if ((TREE_CODE (lhs) == PTRMEM_CST |
2295 | || TREE_CODE (rhs) == PTRMEM_CST) | |
2296 | && (null_member_pointer_value_p (lhs) | |
2297 | || null_member_pointer_value_p (rhs))) | |
2298 | r = constant_boolean_node (!is_code_eq, type); | |
dd5dda56 JM |
2299 | else if (TREE_CODE (lhs) == PTRMEM_CST) |
2300 | lhs = cplus_expand_constant (lhs); | |
2301 | else if (TREE_CODE (rhs) == PTRMEM_CST) | |
2302 | rhs = cplus_expand_constant (rhs); | |
618d6c1c | 2303 | } |
8bada5cd MS |
2304 | if (code == POINTER_PLUS_EXPR && !*non_constant_p |
2305 | && integer_zerop (lhs) && !integer_zerop (rhs)) | |
2306 | { | |
2307 | if (!ctx->quiet) | |
2308 | error ("arithmetic involving a null pointer in %qE", lhs); | |
bf533db8 | 2309 | *non_constant_p = true; |
8bada5cd MS |
2310 | return t; |
2311 | } | |
ea8661cd JJ |
2312 | else if (code == POINTER_PLUS_EXPR) |
2313 | r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p, | |
2314 | overflow_p); | |
618d6c1c PP |
2315 | |
2316 | if (r == NULL_TREE) | |
2317 | r = fold_binary_loc (loc, code, type, lhs, rhs); | |
2318 | ||
f899317e JM |
2319 | if (r == NULL_TREE) |
2320 | { | |
2321 | if (lhs == orig_lhs && rhs == orig_rhs) | |
2322 | r = t; | |
2323 | else | |
2324 | r = build2_loc (loc, code, type, lhs, rhs); | |
2325 | } | |
253a921b MP |
2326 | else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs)) |
2327 | *non_constant_p = true; | |
c8a66fc9 JM |
2328 | /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to |
2329 | a local array in a constexpr function. */ | |
71a93b08 | 2330 | bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs)); |
caee690e | 2331 | if (!ptr) |
134efa82 | 2332 | VERIFY_CONSTANT (r); |
2d76680f PC |
2333 | return r; |
2334 | } | |
2335 | ||
2336 | /* Subroutine of cxx_eval_constant_expression. | |
2337 | Attempt to evaluate condition expressions. Dead branches are not | |
2338 | looked into. */ | |
2339 | ||
2340 | static tree | |
3e605b20 | 2341 | cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t, |
92a596e8 | 2342 | bool lval, |
56632b27 JM |
2343 | bool *non_constant_p, bool *overflow_p, |
2344 | tree *jump_target) | |
2d76680f | 2345 | { |
3e605b20 | 2346 | tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
12d9ce19 | 2347 | /*lval*/false, |
5a804683 | 2348 | non_constant_p, overflow_p); |
2d76680f PC |
2349 | VERIFY_CONSTANT (val); |
2350 | /* Don't VERIFY_CONSTANT the other operands. */ | |
2351 | if (integer_zerop (val)) | |
43574e4f JJ |
2352 | val = TREE_OPERAND (t, 2); |
2353 | else | |
2354 | val = TREE_OPERAND (t, 1); | |
2355 | if (TREE_CODE (t) == IF_STMT && !val) | |
2356 | val = void_node; | |
2357 | return cxx_eval_constant_expression (ctx, val, lval, non_constant_p, | |
2358 | overflow_p, jump_target); | |
2d76680f PC |
2359 | } |
2360 | ||
f370e36d JJ |
2361 | /* Subroutine of cxx_eval_constant_expression. |
2362 | Attempt to evaluate vector condition expressions. Unlike | |
2363 | cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal | |
2364 | ternary arithmetics operation, where all 3 arguments have to be | |
2365 | evaluated as constants and then folding computes the result from | |
2366 | them. */ | |
2367 | ||
2368 | static tree | |
2369 | cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t, | |
2370 | bool *non_constant_p, bool *overflow_p) | |
2371 | { | |
2372 | tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), | |
2373 | /*lval*/false, | |
2374 | non_constant_p, overflow_p); | |
2375 | VERIFY_CONSTANT (arg1); | |
2376 | tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), | |
2377 | /*lval*/false, | |
2378 | non_constant_p, overflow_p); | |
2379 | VERIFY_CONSTANT (arg2); | |
2380 | tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2), | |
2381 | /*lval*/false, | |
2382 | non_constant_p, overflow_p); | |
2383 | VERIFY_CONSTANT (arg3); | |
2384 | location_t loc = EXPR_LOCATION (t); | |
2385 | tree type = TREE_TYPE (t); | |
2386 | tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); | |
2387 | if (r == NULL_TREE) | |
2388 | { | |
2389 | if (arg1 == TREE_OPERAND (t, 0) | |
2390 | && arg2 == TREE_OPERAND (t, 1) | |
2391 | && arg3 == TREE_OPERAND (t, 2)) | |
2392 | r = t; | |
2393 | else | |
2394 | r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); | |
2395 | } | |
2396 | VERIFY_CONSTANT (r); | |
2397 | return r; | |
2398 | } | |
2399 | ||
ceaaf873 JM |
2400 | /* Returns less than, equal to, or greater than zero if KEY is found to be |
2401 | less than, to match, or to be greater than the constructor_elt's INDEX. */ | |
2402 | ||
2403 | static int | |
2404 | array_index_cmp (tree key, tree index) | |
2405 | { | |
2406 | gcc_assert (TREE_CODE (key) == INTEGER_CST); | |
2407 | ||
2408 | switch (TREE_CODE (index)) | |
2409 | { | |
2410 | case INTEGER_CST: | |
2411 | return tree_int_cst_compare (key, index); | |
2412 | case RANGE_EXPR: | |
2413 | { | |
2414 | tree lo = TREE_OPERAND (index, 0); | |
2415 | tree hi = TREE_OPERAND (index, 1); | |
2416 | if (tree_int_cst_lt (key, lo)) | |
2417 | return -1; | |
2418 | else if (tree_int_cst_lt (hi, key)) | |
2419 | return 1; | |
2420 | else | |
2421 | return 0; | |
2422 | } | |
2423 | default: | |
2424 | gcc_unreachable (); | |
2425 | } | |
2426 | } | |
2427 | ||
2428 | /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1 | |
2429 | if none. If INSERT is true, insert a matching element rather than fail. */ | |
2430 | ||
2431 | static HOST_WIDE_INT | |
582d2481 | 2432 | find_array_ctor_elt (tree ary, tree dindex, bool insert) |
ceaaf873 JM |
2433 | { |
2434 | if (tree_int_cst_sgn (dindex) < 0) | |
2435 | return -1; | |
2436 | ||
2437 | unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex); | |
2438 | vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary); | |
2439 | unsigned HOST_WIDE_INT len = vec_safe_length (elts); | |
2440 | ||
2441 | unsigned HOST_WIDE_INT end = len; | |
2442 | unsigned HOST_WIDE_INT begin = 0; | |
2443 | ||
2444 | /* If the last element of the CONSTRUCTOR has its own index, we can assume | |
2445 | that the same is true of the other elements and index directly. */ | |
2446 | if (end > 0) | |
2447 | { | |
fe217ba0 | 2448 | tree cindex = (*elts)[end - 1].index; |
ceaaf873 | 2449 | if (TREE_CODE (cindex) == INTEGER_CST |
fe217ba0 | 2450 | && compare_tree_int (cindex, end - 1) == 0) |
ceaaf873 JM |
2451 | { |
2452 | if (i < end) | |
2453 | return i; | |
2454 | else | |
2455 | begin = end; | |
2456 | } | |
2457 | } | |
2458 | ||
2459 | /* Otherwise, find a matching index by means of a binary search. */ | |
2460 | while (begin != end) | |
2461 | { | |
2462 | unsigned HOST_WIDE_INT middle = (begin + end) / 2; | |
a7ccb9e7 JM |
2463 | constructor_elt &elt = (*elts)[middle]; |
2464 | tree idx = elt.index; | |
ceaaf873 | 2465 | |
a7ccb9e7 | 2466 | int cmp = array_index_cmp (dindex, idx); |
ceaaf873 JM |
2467 | if (cmp < 0) |
2468 | end = middle; | |
2469 | else if (cmp > 0) | |
2470 | begin = middle + 1; | |
2471 | else | |
a7ccb9e7 JM |
2472 | { |
2473 | if (insert && TREE_CODE (idx) == RANGE_EXPR) | |
2474 | { | |
2475 | /* We need to split the range. */ | |
2476 | constructor_elt e; | |
2477 | tree lo = TREE_OPERAND (idx, 0); | |
2478 | tree hi = TREE_OPERAND (idx, 1); | |
fe217ba0 JJ |
2479 | tree value = elt.value; |
2480 | dindex = fold_convert (sizetype, dindex); | |
a7ccb9e7 JM |
2481 | if (tree_int_cst_lt (lo, dindex)) |
2482 | { | |
2483 | /* There are still some lower elts; shorten the range. */ | |
2484 | tree new_hi = int_const_binop (MINUS_EXPR, dindex, | |
2485 | size_one_node); | |
2486 | if (tree_int_cst_equal (lo, new_hi)) | |
2487 | /* Only one element left, no longer a range. */ | |
2488 | elt.index = lo; | |
2489 | else | |
2490 | TREE_OPERAND (idx, 1) = new_hi; | |
2491 | /* Append the element we want to insert. */ | |
2492 | ++middle; | |
2493 | e.index = dindex; | |
fe217ba0 | 2494 | e.value = unshare_constructor (value); |
a7ccb9e7 JM |
2495 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e); |
2496 | } | |
2497 | else | |
2498 | /* No lower elts, the range elt is now ours. */ | |
2499 | elt.index = dindex; | |
2500 | ||
2501 | if (tree_int_cst_lt (dindex, hi)) | |
2502 | { | |
2503 | /* There are still some higher elts; append a range. */ | |
2504 | tree new_lo = int_const_binop (PLUS_EXPR, dindex, | |
2505 | size_one_node); | |
2506 | if (tree_int_cst_equal (new_lo, hi)) | |
2507 | e.index = hi; | |
2508 | else | |
2509 | e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi); | |
fe217ba0 JJ |
2510 | e.value = unshare_constructor (value); |
2511 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e); | |
a7ccb9e7 JM |
2512 | } |
2513 | } | |
2514 | return middle; | |
2515 | } | |
ceaaf873 JM |
2516 | } |
2517 | ||
2518 | if (insert) | |
2519 | { | |
2520 | constructor_elt e = { dindex, NULL_TREE }; | |
2521 | vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e); | |
2522 | return end; | |
2523 | } | |
2524 | ||
2525 | return -1; | |
2526 | } | |
2527 | ||
abdc16c8 MS |
2528 | /* Under the control of CTX, issue a detailed diagnostic for |
2529 | an out-of-bounds subscript INDEX into the expression ARRAY. */ | |
2530 | ||
2531 | static void | |
043666e0 | 2532 | diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index) |
abdc16c8 MS |
2533 | { |
2534 | if (!ctx->quiet) | |
2535 | { | |
2536 | tree arraytype = TREE_TYPE (array); | |
2537 | ||
2538 | /* Convert the unsigned array subscript to a signed integer to avoid | |
2539 | printing huge numbers for small negative values. */ | |
2540 | tree sidx = fold_convert (ssizetype, index); | |
043666e0 | 2541 | STRIP_ANY_LOCATION_WRAPPER (array); |
abdc16c8 MS |
2542 | if (DECL_P (array)) |
2543 | { | |
08b3748c | 2544 | if (TYPE_DOMAIN (arraytype)) |
043666e0 JM |
2545 | error_at (loc, "array subscript value %qE is outside the bounds " |
2546 | "of array %qD of type %qT", sidx, array, arraytype); | |
08b3748c | 2547 | else |
043666e0 JM |
2548 | error_at (loc, "nonzero array subscript %qE is used with array %qD of " |
2549 | "type %qT with unknown bounds", sidx, array, arraytype); | |
abdc16c8 MS |
2550 | inform (DECL_SOURCE_LOCATION (array), "declared here"); |
2551 | } | |
08b3748c | 2552 | else if (TYPE_DOMAIN (arraytype)) |
043666e0 JM |
2553 | error_at (loc, "array subscript value %qE is outside the bounds " |
2554 | "of array type %qT", sidx, arraytype); | |
08b3748c | 2555 | else |
043666e0 JM |
2556 | error_at (loc, "nonzero array subscript %qE is used with array of type %qT " |
2557 | "with unknown bounds", sidx, arraytype); | |
abdc16c8 MS |
2558 | } |
2559 | } | |
ceaaf873 | 2560 | |
74f8705e MP |
2561 | /* Return the number of elements for TYPE (which is an ARRAY_TYPE or |
2562 | a VECTOR_TYPE). */ | |
2563 | ||
2564 | static tree | |
2565 | get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type, | |
2566 | bool *non_constant_p, bool *overflow_p) | |
2567 | { | |
2568 | tree nelts; | |
2569 | if (TREE_CODE (type) == ARRAY_TYPE) | |
2570 | { | |
2571 | if (TYPE_DOMAIN (type)) | |
2572 | nelts = array_type_nelts_top (type); | |
2573 | else | |
2574 | nelts = size_zero_node; | |
2575 | } | |
2576 | else if (VECTOR_TYPE_P (type)) | |
2577 | nelts = size_int (TYPE_VECTOR_SUBPARTS (type)); | |
2578 | else | |
2579 | gcc_unreachable (); | |
2580 | ||
2581 | /* For VLAs, the number of elements won't be an integer constant. */ | |
2582 | nelts = cxx_eval_constant_expression (ctx, nelts, false, | |
2583 | non_constant_p, overflow_p); | |
2584 | return nelts; | |
2585 | } | |
2586 | ||
d6b46fca NS |
2587 | /* Extract element INDEX consisting of CHARS_PER_ELT chars from |
2588 | STRING_CST STRING. */ | |
2589 | ||
2590 | static tree | |
2591 | extract_string_elt (tree string, unsigned chars_per_elt, unsigned index) | |
2592 | { | |
2593 | tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string))); | |
2594 | tree r; | |
2595 | ||
2596 | if (chars_per_elt == 1) | |
2597 | r = build_int_cst (type, TREE_STRING_POINTER (string)[index]); | |
2598 | else | |
2599 | { | |
2600 | const unsigned char *ptr | |
2601 | = ((const unsigned char *)TREE_STRING_POINTER (string) | |
2602 | + index * chars_per_elt); | |
2603 | r = native_interpret_expr (type, ptr, chars_per_elt); | |
2604 | } | |
2605 | return r; | |
2606 | } | |
2607 | ||
043666e0 JM |
2608 | /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the |
2609 | subscript, diagnose any problems with it, and return the result. */ | |
2610 | ||
2611 | static tree | |
2612 | eval_and_check_array_index (const constexpr_ctx *ctx, | |
2613 | tree t, bool allow_one_past, | |
2614 | bool *non_constant_p, bool *overflow_p) | |
2615 | { | |
f9d0ca40 | 2616 | location_t loc = cp_expr_loc_or_input_loc (t); |
043666e0 JM |
2617 | tree ary = TREE_OPERAND (t, 0); |
2618 | t = TREE_OPERAND (t, 1); | |
2619 | tree index = cxx_eval_constant_expression (ctx, t, false, | |
2620 | non_constant_p, overflow_p); | |
2621 | VERIFY_CONSTANT (index); | |
2622 | ||
2623 | if (!tree_fits_shwi_p (index) | |
2624 | || tree_int_cst_sgn (index) < 0) | |
2625 | { | |
2626 | diag_array_subscript (loc, ctx, ary, index); | |
2627 | *non_constant_p = true; | |
2628 | return t; | |
2629 | } | |
2630 | ||
2631 | tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p, | |
2632 | overflow_p); | |
2633 | VERIFY_CONSTANT (nelts); | |
2634 | if (allow_one_past | |
2635 | ? !tree_int_cst_le (index, nelts) | |
2636 | : !tree_int_cst_lt (index, nelts)) | |
2637 | { | |
2638 | diag_array_subscript (loc, ctx, ary, index); | |
2639 | *non_constant_p = true; | |
2640 | return t; | |
2641 | } | |
2642 | ||
2643 | return index; | |
2644 | } | |
2645 | ||
2d76680f PC |
2646 | /* Subroutine of cxx_eval_constant_expression. |
2647 | Attempt to reduce a reference to an array slot. */ | |
2648 | ||
2649 | static tree | |
3e605b20 | 2650 | cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, |
92a596e8 | 2651 | bool lval, |
2d76680f PC |
2652 | bool *non_constant_p, bool *overflow_p) |
2653 | { | |
2654 | tree oldary = TREE_OPERAND (t, 0); | |
3e605b20 | 2655 | tree ary = cxx_eval_constant_expression (ctx, oldary, |
92a596e8 | 2656 | lval, |
5a804683 | 2657 | non_constant_p, overflow_p); |
2d76680f PC |
2658 | if (*non_constant_p) |
2659 | return t; | |
bc2687dd JJ |
2660 | if (!lval |
2661 | && TREE_CODE (ary) == VIEW_CONVERT_EXPR | |
043666e0 JM |
2662 | && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0))) |
2663 | && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0)))) | |
2664 | ary = TREE_OPERAND (ary, 0); | |
3b9997bb | 2665 | |
043666e0 JM |
2666 | tree oldidx = TREE_OPERAND (t, 1); |
2667 | tree index = eval_and_check_array_index (ctx, t, lval, | |
2668 | non_constant_p, overflow_p); | |
2669 | if (*non_constant_p) | |
2670 | return t; | |
5453bfed | 2671 | |
bc2a38df JJ |
2672 | if (lval && ary == oldary && index == oldidx) |
2673 | return t; | |
2674 | else if (lval) | |
2675 | return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL); | |
2676 | ||
043666e0 JM |
2677 | unsigned len = 0, elem_nchars = 1; |
2678 | tree elem_type = TREE_TYPE (TREE_TYPE (ary)); | |
2679 | if (TREE_CODE (ary) == CONSTRUCTOR) | |
2680 | len = CONSTRUCTOR_NELTS (ary); | |
2681 | else if (TREE_CODE (ary) == STRING_CST) | |
2682 | { | |
2683 | elem_nchars = (TYPE_PRECISION (elem_type) | |
2684 | / TYPE_PRECISION (char_type_node)); | |
2685 | len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars; | |
2686 | } | |
2687 | else if (TREE_CODE (ary) == VECTOR_CST) | |
2688 | /* We don't create variable-length VECTOR_CSTs. */ | |
2689 | len = VECTOR_CST_NELTS (ary).to_constant (); | |
2690 | else | |
2691 | { | |
2692 | /* We can't do anything with other tree codes, so use | |
2693 | VERIFY_CONSTANT to complain and fail. */ | |
2694 | VERIFY_CONSTANT (ary); | |
2695 | gcc_unreachable (); | |
2696 | } | |
2697 | ||
ceaaf873 | 2698 | bool found; |
043666e0 | 2699 | HOST_WIDE_INT i = 0; |
ceaaf873 JM |
2700 | if (TREE_CODE (ary) == CONSTRUCTOR) |
2701 | { | |
2702 | HOST_WIDE_INT ix = find_array_ctor_elt (ary, index); | |
2703 | found = (ix >= 0); | |
2704 | if (found) | |
2705 | i = ix; | |
3b9997bb | 2706 | } |
ceaaf873 | 2707 | else |
043666e0 JM |
2708 | { |
2709 | i = tree_to_shwi (index); | |
2710 | found = (i < len); | |
2711 | } | |
3b9997bb | 2712 | |
fd2bfee5 JM |
2713 | if (found) |
2714 | { | |
2715 | tree r; | |
2716 | if (TREE_CODE (ary) == CONSTRUCTOR) | |
2717 | r = (*CONSTRUCTOR_ELTS (ary))[i].value; | |
2718 | else if (TREE_CODE (ary) == VECTOR_CST) | |
2719 | r = VECTOR_CST_ELT (ary, i); | |
fd2bfee5 | 2720 | else |
d6b46fca NS |
2721 | r = extract_string_elt (ary, elem_nchars, i); |
2722 | ||
fd2bfee5 JM |
2723 | if (r) |
2724 | /* Don't VERIFY_CONSTANT here. */ | |
2725 | return r; | |
2d76680f | 2726 | |
fd2bfee5 | 2727 | /* Otherwise the element doesn't have a value yet. */ |
2d76680f | 2728 | } |
3b9997bb | 2729 | |
fd2bfee5 JM |
2730 | /* Not found. */ |
2731 | ||
2732 | if (TREE_CODE (ary) == CONSTRUCTOR | |
e8c48716 | 2733 | && CONSTRUCTOR_NO_CLEARING (ary)) |
2d76680f | 2734 | { |
fd2bfee5 JM |
2735 | /* 'ary' is part of the aggregate initializer we're currently |
2736 | building; if there's no initializer for this element yet, | |
2737 | that's an error. */ | |
2738 | if (!ctx->quiet) | |
2739 | error ("accessing uninitialized array element"); | |
2740 | *non_constant_p = true; | |
2741 | return t; | |
2d76680f | 2742 | } |
fd2bfee5 JM |
2743 | |
2744 | /* If it's within the array bounds but doesn't have an explicit | |
2745 | initializer, it's value-initialized. */ | |
2746 | tree val = build_value_init (elem_type, tf_warning_or_error); | |
2747 | return cxx_eval_constant_expression (ctx, val, lval, non_constant_p, | |
2748 | overflow_p); | |
2d76680f PC |
2749 | } |
2750 | ||
2751 | /* Subroutine of cxx_eval_constant_expression. | |
2752 | Attempt to reduce a field access of a value of class type. */ | |
2753 | ||
2754 | static tree | |
3e605b20 | 2755 | cxx_eval_component_reference (const constexpr_ctx *ctx, tree t, |
92a596e8 | 2756 | bool lval, |
2d76680f PC |
2757 | bool *non_constant_p, bool *overflow_p) |
2758 | { | |
2759 | unsigned HOST_WIDE_INT i; | |
2760 | tree field; | |
2761 | tree value; | |
2762 | tree part = TREE_OPERAND (t, 1); | |
2763 | tree orig_whole = TREE_OPERAND (t, 0); | |
3e605b20 | 2764 | tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
92a596e8 | 2765 | lval, |
5a804683 | 2766 | non_constant_p, overflow_p); |
a7f8415c | 2767 | if (INDIRECT_REF_P (whole) |
bf533db8 JJ |
2768 | && integer_zerop (TREE_OPERAND (whole, 0))) |
2769 | { | |
2770 | if (!ctx->quiet) | |
2771 | error ("dereferencing a null pointer in %qE", orig_whole); | |
2772 | *non_constant_p = true; | |
2773 | return t; | |
2774 | } | |
8bada5cd | 2775 | |
dcdbc004 JM |
2776 | if (TREE_CODE (whole) == PTRMEM_CST) |
2777 | whole = cplus_expand_constant (whole); | |
2d76680f PC |
2778 | if (whole == orig_whole) |
2779 | return t; | |
92a596e8 | 2780 | if (lval) |
2d76680f PC |
2781 | return fold_build3 (COMPONENT_REF, TREE_TYPE (t), |
2782 | whole, part, NULL_TREE); | |
2783 | /* Don't VERIFY_CONSTANT here; we only want to check that we got a | |
2784 | CONSTRUCTOR. */ | |
2785 | if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR) | |
2786 | { | |
2b3ab879 | 2787 | if (!ctx->quiet) |
2d76680f PC |
2788 | error ("%qE is not a constant expression", orig_whole); |
2789 | *non_constant_p = true; | |
2790 | } | |
2791 | if (DECL_MUTABLE_P (part)) | |
2792 | { | |
2b3ab879 | 2793 | if (!ctx->quiet) |
2d76680f PC |
2794 | error ("mutable %qD is not usable in a constant expression", part); |
2795 | *non_constant_p = true; | |
2796 | } | |
2797 | if (*non_constant_p) | |
2798 | return t; | |
2db613e5 | 2799 | bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole)); |
2d76680f PC |
2800 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) |
2801 | { | |
2db613e5 JM |
2802 | /* Use name match for PMF fields, as a variant will have a |
2803 | different FIELD_DECL with a different type. */ | |
2804 | if (pmf ? DECL_NAME (field) == DECL_NAME (part) | |
2805 | : field == part) | |
60813a46 JM |
2806 | { |
2807 | if (value) | |
2808 | return value; | |
2809 | else | |
2810 | /* We're in the middle of initializing it. */ | |
2811 | break; | |
2812 | } | |
2d76680f PC |
2813 | } |
2814 | if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE | |
2815 | && CONSTRUCTOR_NELTS (whole) > 0) | |
2816 | { | |
2817 | /* DR 1188 says we don't have to deal with this. */ | |
2b3ab879 | 2818 | if (!ctx->quiet) |
2d76680f PC |
2819 | error ("accessing %qD member instead of initialized %qD member in " |
2820 | "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index); | |
2821 | *non_constant_p = true; | |
2822 | return t; | |
2823 | } | |
2824 | ||
188e53bd JM |
2825 | /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty |
2826 | classes never get represented; throw together a value now. */ | |
dbcd32f8 | 2827 | if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
188e53bd JM |
2828 | return build_constructor (TREE_TYPE (t), NULL); |
2829 | ||
2db613e5 JM |
2830 | gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole))); |
2831 | ||
e8c48716 | 2832 | if (CONSTRUCTOR_NO_CLEARING (whole)) |
3e605b20 JM |
2833 | { |
2834 | /* 'whole' is part of the aggregate initializer we're currently | |
2835 | building; if there's no initializer for this member yet, that's an | |
188e53bd | 2836 | error. */ |
2b3ab879 | 2837 | if (!ctx->quiet) |
3e605b20 JM |
2838 | error ("accessing uninitialized member %qD", part); |
2839 | *non_constant_p = true; | |
2840 | return t; | |
2841 | } | |
2842 | ||
2d76680f PC |
2843 | /* If there's no explicit init for this field, it's value-initialized. */ |
2844 | value = build_value_init (TREE_TYPE (t), tf_warning_or_error); | |
3e605b20 | 2845 | return cxx_eval_constant_expression (ctx, value, |
92a596e8 | 2846 | lval, |
5a804683 | 2847 | non_constant_p, overflow_p); |
2d76680f PC |
2848 | } |
2849 | ||
2850 | /* Subroutine of cxx_eval_constant_expression. | |
2851 | Attempt to reduce a field access of a value of class type that is | |
2852 | expressed as a BIT_FIELD_REF. */ | |
2853 | ||
2854 | static tree | |
3e605b20 | 2855 | cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t, |
92a596e8 | 2856 | bool lval, |
2d76680f PC |
2857 | bool *non_constant_p, bool *overflow_p) |
2858 | { | |
2859 | tree orig_whole = TREE_OPERAND (t, 0); | |
2860 | tree retval, fldval, utype, mask; | |
2861 | bool fld_seen = false; | |
2862 | HOST_WIDE_INT istart, isize; | |
3e605b20 | 2863 | tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
92a596e8 | 2864 | lval, |
5a804683 | 2865 | non_constant_p, overflow_p); |
2d76680f PC |
2866 | tree start, field, value; |
2867 | unsigned HOST_WIDE_INT i; | |
2868 | ||
2869 | if (whole == orig_whole) | |
2870 | return t; | |
2871 | /* Don't VERIFY_CONSTANT here; we only want to check that we got a | |
2872 | CONSTRUCTOR. */ | |
2873 | if (!*non_constant_p | |
2874 | && TREE_CODE (whole) != VECTOR_CST | |
2875 | && TREE_CODE (whole) != CONSTRUCTOR) | |
2876 | { | |
2b3ab879 | 2877 | if (!ctx->quiet) |
2d76680f PC |
2878 | error ("%qE is not a constant expression", orig_whole); |
2879 | *non_constant_p = true; | |
2880 | } | |
2881 | if (*non_constant_p) | |
2882 | return t; | |
2883 | ||
2884 | if (TREE_CODE (whole) == VECTOR_CST) | |
2885 | return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole, | |
2886 | TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)); | |
2887 | ||
2888 | start = TREE_OPERAND (t, 2); | |
2889 | istart = tree_to_shwi (start); | |
2890 | isize = tree_to_shwi (TREE_OPERAND (t, 1)); | |
2891 | utype = TREE_TYPE (t); | |
2892 | if (!TYPE_UNSIGNED (utype)) | |
2893 | utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1); | |
2894 | retval = build_int_cst (utype, 0); | |
2895 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) | |
2896 | { | |
2897 | tree bitpos = bit_position (field); | |
2898 | if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1)) | |
2899 | return value; | |
2900 | if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE | |
2901 | && TREE_CODE (value) == INTEGER_CST | |
2902 | && tree_fits_shwi_p (bitpos) | |
2903 | && tree_fits_shwi_p (DECL_SIZE (field))) | |
2904 | { | |
2905 | HOST_WIDE_INT bit = tree_to_shwi (bitpos); | |
2906 | HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field)); | |
2907 | HOST_WIDE_INT shift; | |
2908 | if (bit >= istart && bit + sz <= istart + isize) | |
2909 | { | |
2910 | fldval = fold_convert (utype, value); | |
2911 | mask = build_int_cst_type (utype, -1); | |
2912 | mask = fold_build2 (LSHIFT_EXPR, utype, mask, | |
2913 | size_int (TYPE_PRECISION (utype) - sz)); | |
2914 | mask = fold_build2 (RSHIFT_EXPR, utype, mask, | |
2915 | size_int (TYPE_PRECISION (utype) - sz)); | |
2916 | fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask); | |
2917 | shift = bit - istart; | |
2918 | if (BYTES_BIG_ENDIAN) | |
2919 | shift = TYPE_PRECISION (utype) - shift - sz; | |
2920 | fldval = fold_build2 (LSHIFT_EXPR, utype, fldval, | |
2921 | size_int (shift)); | |
2922 | retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval); | |
2923 | fld_seen = true; | |
2924 | } | |
2925 | } | |
2926 | } | |
2927 | if (fld_seen) | |
2928 | return fold_convert (TREE_TYPE (t), retval); | |
2929 | gcc_unreachable (); | |
2930 | return error_mark_node; | |
2931 | } | |
2932 | ||
2933 | /* Subroutine of cxx_eval_constant_expression. | |
2934 | Evaluate a short-circuited logical expression T in the context | |
2935 | of a given constexpr CALL. BAILOUT_VALUE is the value for | |
2936 | early return. CONTINUE_VALUE is used here purely for | |
2937 | sanity check purposes. */ | |
2938 | ||
2939 | static tree | |
3e605b20 | 2940 | cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t, |
2d76680f | 2941 | tree bailout_value, tree continue_value, |
92a596e8 | 2942 | bool lval, |
2d76680f PC |
2943 | bool *non_constant_p, bool *overflow_p) |
2944 | { | |
2945 | tree r; | |
3e605b20 | 2946 | tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
92a596e8 | 2947 | lval, |
5a804683 | 2948 | non_constant_p, overflow_p); |
2d76680f PC |
2949 | VERIFY_CONSTANT (lhs); |
2950 | if (tree_int_cst_equal (lhs, bailout_value)) | |
2951 | return lhs; | |
2952 | gcc_assert (tree_int_cst_equal (lhs, continue_value)); | |
3e605b20 | 2953 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
92a596e8 | 2954 | lval, non_constant_p, |
5a804683 | 2955 | overflow_p); |
2d76680f PC |
2956 | VERIFY_CONSTANT (r); |
2957 | return r; | |
2958 | } | |
2959 | ||
2960 | /* REF is a COMPONENT_REF designating a particular field. V is a vector of | |
2961 | CONSTRUCTOR elements to initialize (part of) an object containing that | |
2962 | field. Return a pointer to the constructor_elt corresponding to the | |
2963 | initialization of the field. */ | |
2964 | ||
2965 | static constructor_elt * | |
2966 | base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref) | |
2967 | { | |
2968 | tree aggr = TREE_OPERAND (ref, 0); | |
2969 | tree field = TREE_OPERAND (ref, 1); | |
2970 | HOST_WIDE_INT i; | |
2971 | constructor_elt *ce; | |
2972 | ||
2973 | gcc_assert (TREE_CODE (ref) == COMPONENT_REF); | |
2974 | ||
2975 | if (TREE_CODE (aggr) == COMPONENT_REF) | |
2976 | { | |
2977 | constructor_elt *base_ce | |
2978 | = base_field_constructor_elt (v, aggr); | |
2979 | v = CONSTRUCTOR_ELTS (base_ce->value); | |
2980 | } | |
2981 | ||
2982 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
2983 | if (ce->index == field) | |
2984 | return ce; | |
2985 | ||
2986 | gcc_unreachable (); | |
2987 | return NULL; | |
2988 | } | |
2989 | ||
3e605b20 JM |
2990 | /* Some of the expressions fed to the constexpr mechanism are calls to |
2991 | constructors, which have type void. In that case, return the type being | |
2992 | initialized by the constructor. */ | |
2993 | ||
2994 | static tree | |
2995 | initialized_type (tree t) | |
2996 | { | |
2997 | if (TYPE_P (t)) | |
2998 | return t; | |
5dab8b11 | 2999 | tree type = TREE_TYPE (t); |
4d0c18c6 | 3000 | if (TREE_CODE (t) == CALL_EXPR) |
3e605b20 JM |
3001 | { |
3002 | /* A constructor call has void type, so we need to look deeper. */ | |
3003 | tree fn = get_function_named_in_call (t); | |
3004 | if (fn && TREE_CODE (fn) == FUNCTION_DECL | |
3005 | && DECL_CXX_CONSTRUCTOR_P (fn)) | |
3006 | type = DECL_CONTEXT (fn); | |
3007 | } | |
4d0c18c6 JM |
3008 | else if (TREE_CODE (t) == COMPOUND_EXPR) |
3009 | return initialized_type (TREE_OPERAND (t, 1)); | |
5dab8b11 JM |
3010 | else if (TREE_CODE (t) == AGGR_INIT_EXPR) |
3011 | type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t)); | |
3012 | return cv_unqualified (type); | |
3e605b20 JM |
3013 | } |
3014 | ||
3015 | /* We're about to initialize element INDEX of an array or class from VALUE. | |
3016 | Set up NEW_CTX appropriately by adjusting .object to refer to the | |
3017 | subobject and creating a new CONSTRUCTOR if the element is itself | |
3018 | a class or array. */ | |
3019 | ||
3020 | static void | |
3021 | init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx, | |
3022 | tree index, tree &value) | |
3023 | { | |
3024 | new_ctx = *ctx; | |
3025 | ||
3026 | if (index && TREE_CODE (index) != INTEGER_CST | |
3027 | && TREE_CODE (index) != FIELD_DECL) | |
3028 | /* This won't have an element in the new CONSTRUCTOR. */ | |
3029 | return; | |
3030 | ||
3031 | tree type = initialized_type (value); | |
3032 | if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type)) | |
3033 | /* A non-aggregate member doesn't get its own CONSTRUCTOR. */ | |
3034 | return; | |
3035 | ||
3036 | /* The sub-aggregate initializer might contain a placeholder; | |
3037 | update object to refer to the subobject and ctor to refer to | |
3038 | the (newly created) sub-initializer. */ | |
3039 | if (ctx->object) | |
3040 | new_ctx.object = build_ctor_subob_ref (index, type, ctx->object); | |
3041 | tree elt = build_constructor (type, NULL); | |
e8c48716 | 3042 | CONSTRUCTOR_NO_CLEARING (elt) = true; |
3e605b20 JM |
3043 | new_ctx.ctor = elt; |
3044 | ||
3045 | if (TREE_CODE (value) == TARGET_EXPR) | |
3046 | /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */ | |
3047 | value = TARGET_EXPR_INITIAL (value); | |
3048 | } | |
3049 | ||
3050 | /* We're about to process an initializer for a class or array TYPE. Make | |
3051 | sure that CTX is set up appropriately. */ | |
3052 | ||
3053 | static void | |
3054 | verify_ctor_sanity (const constexpr_ctx *ctx, tree type) | |
3055 | { | |
3056 | /* We don't bother building a ctor for an empty base subobject. */ | |
3057 | if (is_empty_class (type)) | |
3058 | return; | |
3059 | ||
3060 | /* We're in the middle of an initializer that might involve placeholders; | |
3061 | our caller should have created a CONSTRUCTOR for us to put the | |
3062 | initializer into. We will either return that constructor or T. */ | |
3063 | gcc_assert (ctx->ctor); | |
3064 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
3065 | (type, TREE_TYPE (ctx->ctor))); | |
fe69277d JM |
3066 | /* We used to check that ctx->ctor was empty, but that isn't the case when |
3067 | the object is zero-initialized before calling the constructor. */ | |
3e605b20 | 3068 | if (ctx->object) |
176e79b5 JM |
3069 | { |
3070 | tree otype = TREE_TYPE (ctx->object); | |
3071 | gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype) | |
3072 | /* Handle flexible array members. */ | |
3073 | || (TREE_CODE (otype) == ARRAY_TYPE | |
3074 | && TYPE_DOMAIN (otype) == NULL_TREE | |
3075 | && TREE_CODE (type) == ARRAY_TYPE | |
3076 | && (same_type_ignoring_top_level_qualifiers_p | |
3077 | (TREE_TYPE (type), TREE_TYPE (otype))))); | |
3078 | } | |
3e605b20 JM |
3079 | gcc_assert (!ctx->object || !DECL_P (ctx->object) |
3080 | || *(ctx->values->get (ctx->object)) == ctx->ctor); | |
3081 | } | |
3082 | ||
2d76680f PC |
3083 | /* Subroutine of cxx_eval_constant_expression. |
3084 | The expression tree T denotes a C-style array or a C-style | |
3085 | aggregate. Reduce it to a constant expression. */ | |
3086 | ||
3087 | static tree | |
3e605b20 | 3088 | cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t, |
92a596e8 | 3089 | bool lval, |
2d76680f PC |
3090 | bool *non_constant_p, bool *overflow_p) |
3091 | { | |
3092 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); | |
2d76680f PC |
3093 | bool changed = false; |
3094 | gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t)); | |
8a29084d | 3095 | tree type = TREE_TYPE (t); |
3e605b20 | 3096 | |
8a29084d | 3097 | constexpr_ctx new_ctx; |
d4619dc1 | 3098 | if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type)) |
8a29084d | 3099 | { |
d4619dc1 NS |
3100 | /* We don't really need the ctx->ctor business for a PMF or |
3101 | vector, but it's simpler to use the same code. */ | |
8a29084d JM |
3102 | new_ctx = *ctx; |
3103 | new_ctx.ctor = build_constructor (type, NULL); | |
3104 | new_ctx.object = NULL_TREE; | |
3105 | ctx = &new_ctx; | |
3106 | }; | |
3107 | verify_ctor_sanity (ctx, type); | |
3e605b20 JM |
3108 | vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); |
3109 | vec_alloc (*p, vec_safe_length (v)); | |
3110 | ||
2d63bc39 JM |
3111 | unsigned i; |
3112 | tree index, value; | |
3113 | bool constant_p = true; | |
3114 | bool side_effects_p = false; | |
3e605b20 | 3115 | FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value) |
2d76680f | 3116 | { |
bcb5f3c9 | 3117 | tree orig_value = value; |
3e605b20 JM |
3118 | init_subob_ctx (ctx, new_ctx, index, value); |
3119 | if (new_ctx.ctor != ctx->ctor) | |
3120 | /* If we built a new CONSTRUCTOR, attach it now so that other | |
3121 | initializers can refer to it. */ | |
3122 | CONSTRUCTOR_APPEND_ELT (*p, index, new_ctx.ctor); | |
3123 | tree elt = cxx_eval_constant_expression (&new_ctx, value, | |
92a596e8 | 3124 | lval, |
5a804683 | 3125 | non_constant_p, overflow_p); |
2d76680f | 3126 | /* Don't VERIFY_CONSTANT here. */ |
2b3ab879 | 3127 | if (ctx->quiet && *non_constant_p) |
3e605b20 | 3128 | break; |
bcb5f3c9 | 3129 | if (elt != orig_value) |
2d76680f | 3130 | changed = true; |
2d63bc39 JM |
3131 | |
3132 | if (!TREE_CONSTANT (elt)) | |
3133 | constant_p = false; | |
3134 | if (TREE_SIDE_EFFECTS (elt)) | |
3135 | side_effects_p = true; | |
3e605b20 | 3136 | if (index && TREE_CODE (index) == COMPONENT_REF) |
2d76680f PC |
3137 | { |
3138 | /* This is an initialization of a vfield inside a base | |
3139 | subaggregate that we already initialized; push this | |
3140 | initialization into the previous initialization. */ | |
3e605b20 | 3141 | constructor_elt *inner = base_field_constructor_elt (*p, index); |
2d76680f | 3142 | inner->value = elt; |
3e605b20 | 3143 | changed = true; |
2d76680f | 3144 | } |
3e605b20 JM |
3145 | else if (index |
3146 | && (TREE_CODE (index) == NOP_EXPR | |
3147 | || TREE_CODE (index) == POINTER_PLUS_EXPR)) | |
2d76680f PC |
3148 | { |
3149 | /* This is an initializer for an empty base; now that we've | |
3150 | checked that it's constant, we can ignore it. */ | |
3e605b20 JM |
3151 | gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index)))); |
3152 | changed = true; | |
3153 | } | |
2d76680f | 3154 | else |
ac9ec198 | 3155 | { |
1bdbef09 MP |
3156 | if (new_ctx.ctor != ctx->ctor) |
3157 | { | |
3158 | /* We appended this element above; update the value. */ | |
3159 | gcc_assert ((*p)->last().index == index); | |
3160 | (*p)->last().value = elt; | |
3161 | } | |
3162 | else | |
3163 | CONSTRUCTOR_APPEND_ELT (*p, index, elt); | |
3164 | /* Adding or replacing an element might change the ctor's flags. */ | |
ac9ec198 MP |
3165 | TREE_CONSTANT (ctx->ctor) = constant_p; |
3166 | TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p; | |
3167 | } | |
2d76680f PC |
3168 | } |
3169 | if (*non_constant_p || !changed) | |
3e605b20 JM |
3170 | return t; |
3171 | t = ctx->ctor; | |
3172 | /* We're done building this CONSTRUCTOR, so now we can interpret an | |
3173 | element without an explicit initializer as value-initialized. */ | |
e8c48716 | 3174 | CONSTRUCTOR_NO_CLEARING (t) = false; |
2d63bc39 JM |
3175 | TREE_CONSTANT (t) = constant_p; |
3176 | TREE_SIDE_EFFECTS (t) = side_effects_p; | |
8a29084d | 3177 | if (VECTOR_TYPE_P (type)) |
2d76680f PC |
3178 | t = fold (t); |
3179 | return t; | |
3180 | } | |
3181 | ||
3182 | /* Subroutine of cxx_eval_constant_expression. | |
3183 | The expression tree T is a VEC_INIT_EXPR which denotes the desired | |
3184 | initialization of a non-static data member of array type. Reduce it to a | |
3185 | CONSTRUCTOR. | |
3186 | ||
3187 | Note that apart from value-initialization (when VALUE_INIT is true), | |
3188 | this is only intended to support value-initialization and the | |
3189 | initializations done by defaulted constructors for classes with | |
3190 | non-static data members of array type. In this case, VEC_INIT_EXPR_INIT | |
3191 | will either be NULL_TREE for the default constructor, or a COMPONENT_REF | |
3192 | for the copy/move constructor. */ | |
3193 | ||
3194 | static tree | |
3e605b20 | 3195 | cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init, |
92a596e8 | 3196 | bool value_init, bool lval, |
2d76680f PC |
3197 | bool *non_constant_p, bool *overflow_p) |
3198 | { | |
3199 | tree elttype = TREE_TYPE (atype); | |
3e605b20 JM |
3200 | verify_ctor_sanity (ctx, atype); |
3201 | vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); | |
2d76680f | 3202 | bool pre_init = false; |
4784470a | 3203 | unsigned HOST_WIDE_INT i; |
dc5ca6c8 | 3204 | tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; |
2d76680f PC |
3205 | |
3206 | /* For the default constructor, build up a call to the default | |
3207 | constructor of the element type. We only need to handle class types | |
3208 | here, as for a constructor to be constexpr, all members must be | |
3209 | initialized, which for a defaulted default constructor means they must | |
3210 | be of a class type with a constexpr default constructor. */ | |
3211 | if (TREE_CODE (elttype) == ARRAY_TYPE) | |
3212 | /* We only do this at the lowest level. */; | |
3213 | else if (value_init) | |
3214 | { | |
dc5ca6c8 | 3215 | init = build_value_init (elttype, complain); |
2d76680f PC |
3216 | pre_init = true; |
3217 | } | |
3218 | else if (!init) | |
3219 | { | |
cd9cf97b | 3220 | releasing_vec argvec; |
2d76680f PC |
3221 | init = build_special_member_call (NULL_TREE, complete_ctor_identifier, |
3222 | &argvec, elttype, LOOKUP_NORMAL, | |
dc5ca6c8 | 3223 | complain); |
5dab8b11 | 3224 | init = build_aggr_init_expr (elttype, init); |
2d76680f PC |
3225 | pre_init = true; |
3226 | } | |
3227 | ||
74f8705e MP |
3228 | tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p, |
3229 | overflow_p); | |
3230 | unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts); | |
4784470a | 3231 | for (i = 0; i < max; ++i) |
2d76680f PC |
3232 | { |
3233 | tree idx = build_int_cst (size_type_node, i); | |
3234 | tree eltinit; | |
928af3bf | 3235 | bool reuse = false; |
3e605b20 JM |
3236 | constexpr_ctx new_ctx; |
3237 | init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype); | |
3238 | if (new_ctx.ctor != ctx->ctor) | |
3239 | CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor); | |
2d76680f PC |
3240 | if (TREE_CODE (elttype) == ARRAY_TYPE) |
3241 | { | |
3242 | /* A multidimensional array; recurse. */ | |
3243 | if (value_init || init == NULL_TREE) | |
928af3bf JJ |
3244 | { |
3245 | eltinit = NULL_TREE; | |
3246 | reuse = i == 0; | |
3247 | } | |
2d76680f | 3248 | else |
dc5ca6c8 | 3249 | eltinit = cp_build_array_ref (input_location, init, idx, complain); |
3e605b20 | 3250 | eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init, |
92a596e8 | 3251 | lval, |
2d76680f PC |
3252 | non_constant_p, overflow_p); |
3253 | } | |
3254 | else if (pre_init) | |
3255 | { | |
3256 | /* Initializing an element using value or default initialization | |
3257 | we just pre-built above. */ | |
3ee378fb JM |
3258 | if (init == void_node) |
3259 | /* Trivial default-init, don't do anything to the CONSTRUCTOR. */ | |
3260 | return ctx->ctor; | |
928af3bf JJ |
3261 | eltinit = cxx_eval_constant_expression (&new_ctx, init, lval, |
3262 | non_constant_p, overflow_p); | |
3263 | reuse = i == 0; | |
2d76680f PC |
3264 | } |
3265 | else | |
3266 | { | |
3267 | /* Copying an element. */ | |
3268 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
3269 | (atype, TREE_TYPE (init))); | |
dc5ca6c8 | 3270 | eltinit = cp_build_array_ref (input_location, init, idx, complain); |
72b3e203 | 3271 | if (!lvalue_p (init)) |
2d76680f | 3272 | eltinit = move (eltinit); |
dc5ca6c8 | 3273 | eltinit = force_rvalue (eltinit, complain); |
c2236b9b JJ |
3274 | eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval, |
3275 | non_constant_p, overflow_p); | |
2d76680f | 3276 | } |
2b3ab879 | 3277 | if (*non_constant_p && !ctx->quiet) |
3e605b20 JM |
3278 | break; |
3279 | if (new_ctx.ctor != ctx->ctor) | |
3280 | { | |
3281 | /* We appended this element above; update the value. */ | |
3282 | gcc_assert ((*p)->last().index == idx); | |
3283 | (*p)->last().value = eltinit; | |
3284 | } | |
3285 | else | |
3286 | CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit); | |
928af3bf | 3287 | /* Reuse the result of cxx_eval_constant_expression call |
c2236b9b JJ |
3288 | from the first iteration to all others if it is a constant |
3289 | initializer that doesn't require relocations. */ | |
928af3bf JJ |
3290 | if (reuse |
3291 | && max > 1 | |
c2236b9b JJ |
3292 | && (eltinit == NULL_TREE |
3293 | || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit)) | |
3294 | == null_pointer_node))) | |
928af3bf JJ |
3295 | { |
3296 | if (new_ctx.ctor != ctx->ctor) | |
3297 | eltinit = new_ctx.ctor; | |
1e027956 RB |
3298 | tree range = build2 (RANGE_EXPR, size_type_node, |
3299 | build_int_cst (size_type_node, 1), | |
3300 | build_int_cst (size_type_node, max - 1)); | |
3301 | CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit)); | |
928af3bf JJ |
3302 | break; |
3303 | } | |
1e027956 RB |
3304 | else if (i == 0) |
3305 | vec_safe_reserve (*p, max); | |
2d76680f PC |
3306 | } |
3307 | ||
3308 | if (!*non_constant_p) | |
3309 | { | |
3e605b20 | 3310 | init = ctx->ctor; |
e8c48716 | 3311 | CONSTRUCTOR_NO_CLEARING (init) = false; |
2d76680f | 3312 | } |
2d76680f PC |
3313 | return init; |
3314 | } | |
3315 | ||
3316 | static tree | |
3e605b20 | 3317 | cxx_eval_vec_init (const constexpr_ctx *ctx, tree t, |
92a596e8 | 3318 | bool lval, |
2d76680f PC |
3319 | bool *non_constant_p, bool *overflow_p) |
3320 | { | |
3321 | tree atype = TREE_TYPE (t); | |
3322 | tree init = VEC_INIT_EXPR_INIT (t); | |
3e605b20 | 3323 | tree r = cxx_eval_vec_init_1 (ctx, atype, init, |
2d76680f | 3324 | VEC_INIT_EXPR_VALUE_INIT (t), |
92a596e8 | 3325 | lval, non_constant_p, overflow_p); |
2d76680f PC |
3326 | if (*non_constant_p) |
3327 | return t; | |
3328 | else | |
3329 | return r; | |
3330 | } | |
3331 | ||
e4511ca2 JM |
3332 | /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case |
3333 | where the desired type is an array of unknown bounds because the variable | |
3334 | has had its bounds deduced since the wrapping expression was created. */ | |
3335 | ||
3336 | static bool | |
3337 | same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2) | |
3338 | { | |
3339 | while (TREE_CODE (type1) == ARRAY_TYPE | |
3340 | && TREE_CODE (type2) == ARRAY_TYPE | |
3341 | && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2))) | |
3342 | { | |
3343 | type1 = TREE_TYPE (type1); | |
3344 | type2 = TREE_TYPE (type2); | |
3345 | } | |
3346 | return same_type_ignoring_top_level_qualifiers_p (type1, type2); | |
3347 | } | |
3348 | ||
2d76680f PC |
3349 | /* A less strict version of fold_indirect_ref_1, which requires cv-quals to |
3350 | match. We want to be less strict for simple *& folding; if we have a | |
3351 | non-const temporary that we access through a const pointer, that should | |
3352 | work. We handle this here rather than change fold_indirect_ref_1 | |
3353 | because we're dealing with things like ADDR_EXPR of INTEGER_CST which | |
3354 | don't really make sense outside of constant expression evaluation. Also | |
3355 | we want to allow folding to COMPONENT_REF, which could cause trouble | |
3356 | with TBAA in fold_indirect_ref_1. | |
3357 | ||
3358 | Try to keep this function synced with fold_indirect_ref_1. */ | |
3359 | ||
3360 | static tree | |
3361 | cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base) | |
3362 | { | |
ebe4bf41 MP |
3363 | tree sub = op0; |
3364 | tree subtype; | |
3365 | poly_uint64 const_op01; | |
2d76680f | 3366 | |
2d76680f PC |
3367 | STRIP_NOPS (sub); |
3368 | subtype = TREE_TYPE (sub); | |
71a93b08 | 3369 | if (!INDIRECT_TYPE_P (subtype)) |
2d76680f PC |
3370 | return NULL_TREE; |
3371 | ||
3372 | if (TREE_CODE (sub) == ADDR_EXPR) | |
3373 | { | |
3374 | tree op = TREE_OPERAND (sub, 0); | |
3375 | tree optype = TREE_TYPE (op); | |
3376 | ||
3377 | /* *&CONST_DECL -> to the value of the const decl. */ | |
3378 | if (TREE_CODE (op) == CONST_DECL) | |
3379 | return DECL_INITIAL (op); | |
3380 | /* *&p => p; make sure to handle *&"str"[cst] here. */ | |
1a120ec1 | 3381 | if (similar_type_p (optype, type)) |
2d76680f PC |
3382 | { |
3383 | tree fop = fold_read_from_constant_string (op); | |
3384 | if (fop) | |
3385 | return fop; | |
3386 | else | |
3387 | return op; | |
3388 | } | |
3389 | /* *(foo *)&fooarray => fooarray[0] */ | |
3390 | else if (TREE_CODE (optype) == ARRAY_TYPE | |
1a120ec1 | 3391 | && similar_type_p (type, TREE_TYPE (optype))) |
2d76680f PC |
3392 | { |
3393 | tree type_domain = TYPE_DOMAIN (optype); | |
3394 | tree min_val = size_zero_node; | |
3395 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
3396 | min_val = TYPE_MIN_VALUE (type_domain); | |
3397 | return build4_loc (loc, ARRAY_REF, type, op, min_val, | |
3398 | NULL_TREE, NULL_TREE); | |
3399 | } | |
3400 | /* *(foo *)&complexfoo => __real__ complexfoo */ | |
3401 | else if (TREE_CODE (optype) == COMPLEX_TYPE | |
1a120ec1 | 3402 | && similar_type_p (type, TREE_TYPE (optype))) |
2d76680f PC |
3403 | return fold_build1_loc (loc, REALPART_EXPR, type, op); |
3404 | /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */ | |
b55b02ea | 3405 | else if (VECTOR_TYPE_P (optype) |
1a120ec1 | 3406 | && similar_type_p (type, TREE_TYPE (optype))) |
2d76680f PC |
3407 | { |
3408 | tree part_width = TYPE_SIZE (type); | |
3409 | tree index = bitsize_int (0); | |
ebe4bf41 MP |
3410 | return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, |
3411 | index); | |
2d76680f PC |
3412 | } |
3413 | /* Also handle conversion to an empty base class, which | |
3414 | is represented with a NOP_EXPR. */ | |
3415 | else if (is_empty_class (type) | |
3416 | && CLASS_TYPE_P (optype) | |
3417 | && DERIVED_FROM_P (type, optype)) | |
3418 | { | |
3419 | *empty_base = true; | |
3420 | return op; | |
3421 | } | |
3422 | /* *(foo *)&struct_with_foo_field => COMPONENT_REF */ | |
3423 | else if (RECORD_OR_UNION_TYPE_P (optype)) | |
3424 | { | |
3425 | tree field = TYPE_FIELDS (optype); | |
3426 | for (; field; field = DECL_CHAIN (field)) | |
3427 | if (TREE_CODE (field) == FIELD_DECL | |
b912f962 | 3428 | && TREE_TYPE (field) != error_mark_node |
2d76680f | 3429 | && integer_zerop (byte_position (field)) |
1a120ec1 | 3430 | && similar_type_p (TREE_TYPE (field), type)) |
b912f962 | 3431 | return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE); |
2d76680f PC |
3432 | } |
3433 | } | |
3434 | else if (TREE_CODE (sub) == POINTER_PLUS_EXPR | |
ebe4bf41 | 3435 | && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01)) |
2d76680f PC |
3436 | { |
3437 | tree op00 = TREE_OPERAND (sub, 0); | |
3438 | tree op01 = TREE_OPERAND (sub, 1); | |
3439 | ||
3440 | STRIP_NOPS (op00); | |
3441 | if (TREE_CODE (op00) == ADDR_EXPR) | |
3442 | { | |
3443 | tree op00type; | |
3444 | op00 = TREE_OPERAND (op00, 0); | |
3445 | op00type = TREE_TYPE (op00); | |
3446 | ||
3447 | /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */ | |
b55b02ea | 3448 | if (VECTOR_TYPE_P (op00type) |
1a120ec1 | 3449 | && similar_type_p (type, TREE_TYPE (op00type)) |
ebe4bf41 MP |
3450 | /* POINTER_PLUS_EXPR second operand is sizetype, unsigned, |
3451 | but we want to treat offsets with MSB set as negative. | |
3452 | For the code below negative offsets are invalid and | |
3453 | TYPE_SIZE of the element is something unsigned, so | |
3454 | check whether op01 fits into poly_int64, which implies | |
3455 | it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and | |
3456 | then just use poly_uint64 because we want to treat the | |
3457 | value as unsigned. */ | |
3458 | && tree_fits_poly_int64_p (op01)) | |
2d76680f | 3459 | { |
2d76680f | 3460 | tree part_width = TYPE_SIZE (type); |
ebe4bf41 MP |
3461 | poly_uint64 max_offset |
3462 | = (tree_to_uhwi (part_width) / BITS_PER_UNIT | |
3463 | * TYPE_VECTOR_SUBPARTS (op00type)); | |
3464 | if (known_lt (const_op01, max_offset)) | |
3465 | { | |
3466 | tree index = bitsize_int (const_op01 * BITS_PER_UNIT); | |
3467 | return fold_build3_loc (loc, | |
3468 | BIT_FIELD_REF, type, op00, | |
3469 | part_width, index); | |
3470 | } | |
2d76680f PC |
3471 | } |
3472 | /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */ | |
3473 | else if (TREE_CODE (op00type) == COMPLEX_TYPE | |
1a120ec1 | 3474 | && similar_type_p (type, TREE_TYPE (op00type))) |
2d76680f | 3475 | { |
ebe4bf41 MP |
3476 | if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)), |
3477 | const_op01)) | |
2d76680f PC |
3478 | return fold_build1_loc (loc, IMAGPART_EXPR, type, op00); |
3479 | } | |
3480 | /* ((foo *)&fooarray)[1] => fooarray[1] */ | |
3481 | else if (TREE_CODE (op00type) == ARRAY_TYPE | |
1a120ec1 | 3482 | && similar_type_p (type, TREE_TYPE (op00type))) |
2d76680f PC |
3483 | { |
3484 | tree type_domain = TYPE_DOMAIN (op00type); | |
3485 | tree min_val = size_zero_node; | |
3486 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
3487 | min_val = TYPE_MIN_VALUE (type_domain); | |
b07c3651 MP |
3488 | offset_int off = wi::to_offset (op01); |
3489 | offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (type)); | |
3490 | offset_int remainder; | |
3491 | off = wi::divmod_trunc (off, el_sz, SIGNED, &remainder); | |
3492 | if (remainder == 0 && TREE_CODE (min_val) == INTEGER_CST) | |
3493 | { | |
3494 | off = off + wi::to_offset (min_val); | |
3495 | op01 = wide_int_to_tree (sizetype, off); | |
3496 | return build4_loc (loc, ARRAY_REF, type, op00, op01, | |
3497 | NULL_TREE, NULL_TREE); | |
3498 | } | |
2d76680f PC |
3499 | } |
3500 | /* Also handle conversion to an empty base class, which | |
3501 | is represented with a NOP_EXPR. */ | |
3502 | else if (is_empty_class (type) | |
3503 | && CLASS_TYPE_P (op00type) | |
3504 | && DERIVED_FROM_P (type, op00type)) | |
3505 | { | |
3506 | *empty_base = true; | |
3507 | return op00; | |
3508 | } | |
3509 | /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */ | |
3510 | else if (RECORD_OR_UNION_TYPE_P (op00type)) | |
3511 | { | |
3512 | tree field = TYPE_FIELDS (op00type); | |
3513 | for (; field; field = DECL_CHAIN (field)) | |
3514 | if (TREE_CODE (field) == FIELD_DECL | |
b912f962 | 3515 | && TREE_TYPE (field) != error_mark_node |
2d76680f | 3516 | && tree_int_cst_equal (byte_position (field), op01) |
1a120ec1 | 3517 | && similar_type_p (TREE_TYPE (field), type)) |
b912f962 JJ |
3518 | return fold_build3 (COMPONENT_REF, type, op00, |
3519 | field, NULL_TREE); | |
2d76680f PC |
3520 | } |
3521 | } | |
3522 | } | |
3523 | /* *(foo *)fooarrptr => (*fooarrptr)[0] */ | |
3524 | else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE | |
1a120ec1 | 3525 | && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype)))) |
2d76680f PC |
3526 | { |
3527 | tree type_domain; | |
3528 | tree min_val = size_zero_node; | |
ebe4bf41 MP |
3529 | tree newsub |
3530 | = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL); | |
2d76680f PC |
3531 | if (newsub) |
3532 | sub = newsub; | |
3533 | else | |
3534 | sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub); | |
3535 | type_domain = TYPE_DOMAIN (TREE_TYPE (sub)); | |
3536 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
3537 | min_val = TYPE_MIN_VALUE (type_domain); | |
3538 | return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE, | |
3539 | NULL_TREE); | |
3540 | } | |
3541 | ||
3542 | return NULL_TREE; | |
3543 | } | |
3544 | ||
3545 | static tree | |
3e605b20 | 3546 | cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t, |
92a596e8 | 3547 | bool lval, |
2d76680f PC |
3548 | bool *non_constant_p, bool *overflow_p) |
3549 | { | |
3550 | tree orig_op0 = TREE_OPERAND (t, 0); | |
2d76680f | 3551 | bool empty_base = false; |
2d76680f | 3552 | |
cda0a029 JM |
3553 | /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second |
3554 | operand is an integer-zero. Otherwise reject the MEM_REF for now. */ | |
3555 | ||
3556 | if (TREE_CODE (t) == MEM_REF | |
3557 | && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1)))) | |
3558 | { | |
3559 | gcc_assert (ctx->quiet); | |
3560 | *non_constant_p = true; | |
3561 | return t; | |
3562 | } | |
3563 | ||
255a48d6 JM |
3564 | /* First try to simplify it directly. */ |
3565 | tree r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), orig_op0, | |
3566 | &empty_base); | |
3567 | if (!r) | |
2d76680f | 3568 | { |
255a48d6 JM |
3569 | /* If that didn't work, evaluate the operand first. */ |
3570 | tree op0 = cxx_eval_constant_expression (ctx, orig_op0, | |
3571 | /*lval*/false, non_constant_p, | |
3572 | overflow_p); | |
3573 | /* Don't VERIFY_CONSTANT here. */ | |
3574 | if (*non_constant_p) | |
3575 | return t; | |
3576 | ||
8bada5cd MS |
3577 | if (!lval && integer_zerop (op0)) |
3578 | { | |
3579 | if (!ctx->quiet) | |
3580 | error ("dereferencing a null pointer"); | |
3581 | *non_constant_p = true; | |
3582 | return t; | |
3583 | } | |
3584 | ||
255a48d6 JM |
3585 | r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0, |
3586 | &empty_base); | |
3587 | if (r == NULL_TREE) | |
2d76680f PC |
3588 | { |
3589 | /* We couldn't fold to a constant value. Make sure it's not | |
3590 | something we should have been able to fold. */ | |
255a48d6 JM |
3591 | tree sub = op0; |
3592 | STRIP_NOPS (sub); | |
3593 | if (TREE_CODE (sub) == ADDR_EXPR) | |
3594 | { | |
1a120ec1 | 3595 | gcc_assert (!similar_type_p |
255a48d6 JM |
3596 | (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t))); |
3597 | /* DR 1188 says we don't have to deal with this. */ | |
3598 | if (!ctx->quiet) | |
1a120ec1 JM |
3599 | error_at (cp_expr_loc_or_input_loc (t), |
3600 | "accessing value of %qE through a %qT glvalue in a " | |
3601 | "constant expression", build_fold_indirect_ref (sub), | |
3602 | TREE_TYPE (t)); | |
255a48d6 JM |
3603 | *non_constant_p = true; |
3604 | return t; | |
3605 | } | |
3606 | ||
3607 | if (lval && op0 != orig_op0) | |
3608 | return build1 (INDIRECT_REF, TREE_TYPE (t), op0); | |
3609 | if (!lval) | |
3610 | VERIFY_CONSTANT (t); | |
2d76680f PC |
3611 | return t; |
3612 | } | |
3613 | } | |
3614 | ||
255a48d6 JM |
3615 | r = cxx_eval_constant_expression (ctx, r, |
3616 | lval, non_constant_p, overflow_p); | |
3617 | if (*non_constant_p) | |
3618 | return t; | |
3619 | ||
125db6a1 | 3620 | /* If we're pulling out the value of an empty base, just return an empty |
2d76680f | 3621 | CONSTRUCTOR. */ |
92a596e8 | 3622 | if (empty_base && !lval) |
2d76680f | 3623 | { |
2d76680f PC |
3624 | r = build_constructor (TREE_TYPE (t), NULL); |
3625 | TREE_CONSTANT (r) = true; | |
3626 | } | |
3627 | ||
2d76680f PC |
3628 | return r; |
3629 | } | |
3630 | ||
3631 | /* Complain about R, a VAR_DECL, not being usable in a constant expression. | |
3632 | Shared between potential_constant_expression and | |
3633 | cxx_eval_constant_expression. */ | |
3634 | ||
3635 | static void | |
3636 | non_const_var_error (tree r) | |
3637 | { | |
3638 | tree type = TREE_TYPE (r); | |
3639 | error ("the value of %qD is not usable in a constant " | |
3640 | "expression", r); | |
3641 | /* Avoid error cascade. */ | |
3642 | if (DECL_INITIAL (r) == error_mark_node) | |
3643 | return; | |
3644 | if (DECL_DECLARED_CONSTEXPR_P (r)) | |
3645 | inform (DECL_SOURCE_LOCATION (r), | |
3646 | "%qD used in its own initializer", r); | |
3647 | else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) | |
3648 | { | |
3649 | if (!CP_TYPE_CONST_P (type)) | |
3650 | inform (DECL_SOURCE_LOCATION (r), | |
3651 | "%q#D is not const", r); | |
3652 | else if (CP_TYPE_VOLATILE_P (type)) | |
3653 | inform (DECL_SOURCE_LOCATION (r), | |
3654 | "%q#D is volatile", r); | |
3655 | else if (!DECL_INITIAL (r) | |
a3e2b438 PP |
3656 | || !TREE_CONSTANT (DECL_INITIAL (r)) |
3657 | || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)) | |
2d76680f PC |
3658 | inform (DECL_SOURCE_LOCATION (r), |
3659 | "%qD was not initialized with a constant " | |
3660 | "expression", r); | |
3661 | else | |
3662 | gcc_unreachable (); | |
3663 | } | |
9f613f06 | 3664 | else if (TYPE_REF_P (type)) |
fd338b13 JM |
3665 | inform (DECL_SOURCE_LOCATION (r), |
3666 | "%qD was not initialized with a constant " | |
3667 | "expression", r); | |
2d76680f PC |
3668 | else |
3669 | { | |
3670 | if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r)) | |
3671 | inform (DECL_SOURCE_LOCATION (r), | |
3672 | "%qD was not declared %<constexpr%>", r); | |
3673 | else | |
3674 | inform (DECL_SOURCE_LOCATION (r), | |
3675 | "%qD does not have integral or enumeration type", | |
3676 | r); | |
3677 | } | |
3678 | } | |
3679 | ||
3680 | /* Subroutine of cxx_eval_constant_expression. | |
3681 | Like cxx_eval_unary_expression, except for trinary expressions. */ | |
3682 | ||
3683 | static tree | |
3e605b20 | 3684 | cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t, |
92a596e8 | 3685 | bool lval, |
2d76680f PC |
3686 | bool *non_constant_p, bool *overflow_p) |
3687 | { | |
3688 | int i; | |
3689 | tree args[3]; | |
3690 | tree val; | |
3691 | ||
3692 | for (i = 0; i < 3; i++) | |
3693 | { | |
3e605b20 | 3694 | args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i), |
92a596e8 | 3695 | lval, |
5a804683 | 3696 | non_constant_p, overflow_p); |
2d76680f PC |
3697 | VERIFY_CONSTANT (args[i]); |
3698 | } | |
3699 | ||
3700 | val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t), | |
3701 | args[0], args[1], args[2]); | |
3702 | if (val == NULL_TREE) | |
3703 | return t; | |
3704 | VERIFY_CONSTANT (val); | |
3705 | return val; | |
3706 | } | |
3707 | ||
98e5a19a JM |
3708 | /* True if T was declared in a function declared to be constexpr, and |
3709 | therefore potentially constant in C++14. */ | |
3710 | ||
2d76680f PC |
3711 | bool |
3712 | var_in_constexpr_fn (tree t) | |
3713 | { | |
3714 | tree ctx = DECL_CONTEXT (t); | |
98e5a19a | 3715 | return (ctx && TREE_CODE (ctx) == FUNCTION_DECL |
2d76680f PC |
3716 | && DECL_DECLARED_CONSTEXPR_P (ctx)); |
3717 | } | |
3718 | ||
98e5a19a JM |
3719 | /* True if T was declared in a function that might be constexpr: either a |
3720 | function that was declared constexpr, or a C++17 lambda op(). */ | |
3721 | ||
3722 | bool | |
3723 | var_in_maybe_constexpr_fn (tree t) | |
3724 | { | |
7b936140 | 3725 | if (cxx_dialect >= cxx17 |
98e5a19a JM |
3726 | && DECL_FUNCTION_SCOPE_P (t) |
3727 | && LAMBDA_FUNCTION_P (DECL_CONTEXT (t))) | |
3728 | return true; | |
3729 | return var_in_constexpr_fn (t); | |
3730 | } | |
3731 | ||
68d01920 JM |
3732 | /* We're assigning INIT to TARGET. In do_build_copy_constructor and |
3733 | build_over_call we implement trivial copy of a class with tail padding using | |
3734 | assignment of character arrays, which is valid in normal code, but not in | |
3735 | constexpr evaluation. We don't need to worry about clobbering tail padding | |
3736 | in constexpr evaluation, so strip the type punning. */ | |
3737 | ||
3738 | static void | |
3739 | maybe_simplify_trivial_copy (tree &target, tree &init) | |
3740 | { | |
3741 | if (TREE_CODE (target) == MEM_REF | |
3742 | && TREE_CODE (init) == MEM_REF | |
3743 | && TREE_TYPE (target) == TREE_TYPE (init) | |
3744 | && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE | |
3745 | && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node) | |
3746 | { | |
3747 | target = build_fold_indirect_ref (TREE_OPERAND (target, 0)); | |
3748 | init = build_fold_indirect_ref (TREE_OPERAND (init, 0)); | |
3749 | } | |
3750 | } | |
3751 | ||
04e1749c MP |
3752 | /* Return true if we are modifying something that is const during constant |
3753 | expression evaluation. CODE is the code of the statement, OBJ is the | |
3754 | object in question, MUTABLE_P is true if one of the subobjects were | |
3755 | declared mutable. */ | |
3756 | ||
3757 | static bool | |
3758 | modifying_const_object_p (tree_code code, tree obj, bool mutable_p) | |
3759 | { | |
3760 | /* If this is initialization, there's no problem. */ | |
3761 | if (code != MODIFY_EXPR) | |
3762 | return false; | |
3763 | ||
3764 | /* [basic.type.qualifier] "A const object is an object of type | |
3765 | const T or a non-mutable subobject of a const object." */ | |
3766 | if (mutable_p) | |
3767 | return false; | |
3768 | ||
3769 | return (TREE_READONLY (obj) || CP_TYPE_CONST_P (TREE_TYPE (obj))); | |
3770 | } | |
3771 | ||
60813a46 JM |
3772 | /* Evaluate an INIT_EXPR or MODIFY_EXPR. */ |
3773 | ||
3774 | static tree | |
3775 | cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, | |
92a596e8 | 3776 | bool lval, |
60813a46 JM |
3777 | bool *non_constant_p, bool *overflow_p) |
3778 | { | |
3779 | constexpr_ctx new_ctx = *ctx; | |
3780 | ||
58cc255c JM |
3781 | tree init = TREE_OPERAND (t, 1); |
3782 | if (TREE_CLOBBER_P (init)) | |
3783 | /* Just ignore clobbers. */ | |
3784 | return void_node; | |
3785 | ||
60813a46 JM |
3786 | /* First we figure out where we're storing to. */ |
3787 | tree target = TREE_OPERAND (t, 0); | |
68d01920 JM |
3788 | |
3789 | maybe_simplify_trivial_copy (target, init); | |
3790 | ||
3f8e2835 | 3791 | tree type = TREE_TYPE (target); |
e8b3c1bc JM |
3792 | bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR; |
3793 | if (preeval) | |
3794 | { | |
3795 | /* Evaluate the value to be stored without knowing what object it will be | |
3796 | stored in, so that any side-effects happen first. */ | |
3797 | if (!SCALAR_TYPE_P (type)) | |
3798 | new_ctx.ctor = new_ctx.object = NULL_TREE; | |
3799 | init = cxx_eval_constant_expression (&new_ctx, init, false, | |
3800 | non_constant_p, overflow_p); | |
3801 | if (*non_constant_p) | |
3802 | return t; | |
3803 | } | |
60813a46 | 3804 | |
d0aa42d2 JM |
3805 | bool evaluated = false; |
3806 | if (lval) | |
3f8e2835 | 3807 | { |
d0aa42d2 JM |
3808 | /* If we want to return a reference to the target, we need to evaluate it |
3809 | as a whole; otherwise, only evaluate the innermost piece to avoid | |
3810 | building up unnecessary *_REFs. */ | |
3811 | target = cxx_eval_constant_expression (ctx, target, true, | |
3812 | non_constant_p, overflow_p); | |
3813 | evaluated = true; | |
3814 | if (*non_constant_p) | |
3815 | return t; | |
3f8e2835 JM |
3816 | } |
3817 | ||
043666e0 | 3818 | /* Find the underlying variable. */ |
cd9cf97b | 3819 | releasing_vec refs; |
60813a46 | 3820 | tree object = NULL_TREE; |
04e1749c MP |
3821 | /* If we're modifying a const object, save it. */ |
3822 | tree const_object_being_modified = NULL_TREE; | |
3823 | bool mutable_p = false; | |
60813a46 JM |
3824 | for (tree probe = target; object == NULL_TREE; ) |
3825 | { | |
3826 | switch (TREE_CODE (probe)) | |
3827 | { | |
3828 | case BIT_FIELD_REF: | |
3829 | case COMPONENT_REF: | |
3830 | case ARRAY_REF: | |
043666e0 JM |
3831 | { |
3832 | tree ob = TREE_OPERAND (probe, 0); | |
3833 | tree elt = TREE_OPERAND (probe, 1); | |
7d349dd8 | 3834 | if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt)) |
04e1749c MP |
3835 | mutable_p = true; |
3836 | if (evaluated | |
3837 | && modifying_const_object_p (TREE_CODE (t), probe, mutable_p) | |
3838 | && const_object_being_modified == NULL_TREE) | |
3839 | const_object_being_modified = probe; | |
043666e0 JM |
3840 | if (TREE_CODE (probe) == ARRAY_REF) |
3841 | { | |
3842 | elt = eval_and_check_array_index (ctx, probe, false, | |
3843 | non_constant_p, overflow_p); | |
3844 | if (*non_constant_p) | |
3845 | return t; | |
3846 | } | |
3847 | vec_safe_push (refs, elt); | |
3848 | vec_safe_push (refs, TREE_TYPE (probe)); | |
3849 | probe = ob; | |
3850 | } | |
60813a46 JM |
3851 | break; |
3852 | ||
3853 | default: | |
d0aa42d2 JM |
3854 | if (evaluated) |
3855 | object = probe; | |
3856 | else | |
3857 | { | |
3858 | probe = cxx_eval_constant_expression (ctx, probe, true, | |
3859 | non_constant_p, overflow_p); | |
3860 | evaluated = true; | |
3861 | if (*non_constant_p) | |
3862 | return t; | |
3863 | } | |
3864 | break; | |
60813a46 JM |
3865 | } |
3866 | } | |
3867 | ||
04e1749c MP |
3868 | if (modifying_const_object_p (TREE_CODE (t), object, mutable_p) |
3869 | && const_object_being_modified == NULL_TREE) | |
3870 | const_object_being_modified = object; | |
3871 | ||
60813a46 JM |
3872 | /* And then find/build up our initializer for the path to the subobject |
3873 | we're initializing. */ | |
a1408b7e | 3874 | tree *valp; |
1e163090 JM |
3875 | if (object == ctx->object && VAR_P (object) |
3876 | && DECL_NAME (object) && ctx->call == NULL) | |
3877 | /* The variable we're building up an aggregate initializer for is outside | |
3878 | the constant-expression, so don't evaluate the store. We check | |
3879 | DECL_NAME to handle TARGET_EXPR temporaries, which are fair game. */ | |
3880 | valp = NULL; | |
3881 | else if (DECL_P (object)) | |
a1408b7e JM |
3882 | valp = ctx->values->get (object); |
3883 | else | |
3884 | valp = NULL; | |
60813a46 JM |
3885 | if (!valp) |
3886 | { | |
3887 | /* A constant-expression cannot modify objects from outside the | |
3888 | constant-expression. */ | |
2b3ab879 | 3889 | if (!ctx->quiet) |
64d6d399 | 3890 | error ("modification of %qE is not a constant expression", object); |
60813a46 JM |
3891 | *non_constant_p = true; |
3892 | return t; | |
3893 | } | |
3f8e2835 | 3894 | type = TREE_TYPE (object); |
c75ce530 | 3895 | bool no_zero_init = true; |
2d63bc39 | 3896 | |
cd9cf97b | 3897 | releasing_vec ctors; |
60813a46 JM |
3898 | while (!refs->is_empty()) |
3899 | { | |
3900 | if (*valp == NULL_TREE) | |
3901 | { | |
3902 | *valp = build_constructor (type, NULL); | |
e8c48716 | 3903 | CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; |
60813a46 | 3904 | } |
d6b46fca NS |
3905 | else if (TREE_CODE (*valp) == STRING_CST) |
3906 | { | |
3907 | /* An array was initialized with a string constant, and now | |
3908 | we're writing into one of its elements. Explode the | |
3909 | single initialization into a set of element | |
3910 | initializations. */ | |
3911 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE); | |
3912 | ||
3913 | tree string = *valp; | |
3914 | tree elt_type = TREE_TYPE (type); | |
3915 | unsigned chars_per_elt = (TYPE_PRECISION (elt_type) | |
3916 | / TYPE_PRECISION (char_type_node)); | |
3917 | unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt; | |
3918 | tree ary_ctor = build_constructor (type, NULL); | |
3919 | ||
3920 | vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts); | |
3921 | for (unsigned ix = 0; ix != num_elts; ix++) | |
3922 | { | |
3923 | constructor_elt elt = | |
3924 | { | |
3925 | build_int_cst (size_type_node, ix), | |
3926 | extract_string_elt (string, chars_per_elt, ix) | |
3927 | }; | |
3928 | CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt); | |
3929 | } | |
3930 | ||
3931 | *valp = ary_ctor; | |
3932 | } | |
3933 | ||
c75ce530 JM |
3934 | /* If the value of object is already zero-initialized, any new ctors for |
3935 | subobjects will also be zero-initialized. */ | |
e8c48716 | 3936 | no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp); |
60813a46 | 3937 | |
2d63bc39 JM |
3938 | vec_safe_push (ctors, *valp); |
3939 | ||
ceaaf873 | 3940 | enum tree_code code = TREE_CODE (type); |
60813a46 | 3941 | type = refs->pop(); |
ceaaf873 | 3942 | tree index = refs->pop(); |
60813a46 | 3943 | |
60813a46 | 3944 | constructor_elt *cep = NULL; |
ceaaf873 JM |
3945 | if (code == ARRAY_TYPE) |
3946 | { | |
3947 | HOST_WIDE_INT i | |
3948 | = find_array_ctor_elt (*valp, index, /*insert*/true); | |
3949 | gcc_assert (i >= 0); | |
3950 | cep = CONSTRUCTOR_ELT (*valp, i); | |
3951 | gcc_assert (TREE_CODE (cep->index) != RANGE_EXPR); | |
3952 | } | |
3953 | else | |
3954 | { | |
3955 | gcc_assert (TREE_CODE (index) == FIELD_DECL); | |
88504f34 NS |
3956 | |
3957 | /* We must keep the CONSTRUCTOR's ELTS in FIELD order. | |
3958 | Usually we meet initializers in that order, but it is | |
3959 | possible for base types to be placed not in program | |
3960 | order. */ | |
3961 | tree fields = TYPE_FIELDS (DECL_CONTEXT (index)); | |
3962 | unsigned HOST_WIDE_INT idx; | |
3963 | ||
5c97093b JM |
3964 | if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp) |
3965 | && CONSTRUCTOR_ELT (*valp, 0)->index != index) | |
ed4ec9ce JJ |
3966 | { |
3967 | if (cxx_dialect < cxx2a) | |
3968 | { | |
3969 | if (!ctx->quiet) | |
f9d0ca40 | 3970 | error_at (cp_expr_loc_or_input_loc (t), |
ed4ec9ce JJ |
3971 | "change of the active member of a union " |
3972 | "from %qD to %qD", | |
3973 | CONSTRUCTOR_ELT (*valp, 0)->index, | |
3974 | index); | |
3975 | *non_constant_p = true; | |
3976 | } | |
3977 | /* Changing active member. */ | |
3978 | vec_safe_truncate (CONSTRUCTOR_ELTS (*valp), 0); | |
c7f7c313 | 3979 | no_zero_init = true; |
ed4ec9ce | 3980 | } |
5c97093b | 3981 | |
88504f34 | 3982 | for (idx = 0; |
ceaaf873 | 3983 | vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep); |
88504f34 | 3984 | idx++, fields = DECL_CHAIN (fields)) |
ceaaf873 | 3985 | { |
88504f34 NS |
3986 | if (index == cep->index) |
3987 | goto found; | |
3988 | ||
3989 | /* The field we're initializing must be on the field | |
3990 | list. Look to see if it is present before the | |
3991 | field the current ELT initializes. */ | |
3992 | for (; fields != cep->index; fields = DECL_CHAIN (fields)) | |
3993 | if (index == fields) | |
3994 | goto insert; | |
ceaaf873 | 3995 | } |
88504f34 NS |
3996 | |
3997 | /* We fell off the end of the CONSTRUCTOR, so insert a new | |
3998 | entry at the end. */ | |
3999 | insert: | |
4000 | { | |
4001 | constructor_elt ce = { index, NULL_TREE }; | |
4002 | ||
4003 | vec_safe_insert (CONSTRUCTOR_ELTS (*valp), idx, ce); | |
4004 | cep = CONSTRUCTOR_ELT (*valp, idx); | |
4005 | } | |
4006 | found:; | |
ceaaf873 | 4007 | } |
60813a46 JM |
4008 | valp = &cep->value; |
4009 | } | |
60813a46 | 4010 | |
04e1749c MP |
4011 | /* Detect modifying a constant object in constexpr evaluation. |
4012 | We have found a const object that is being modified. Figure out | |
4013 | if we need to issue an error. Consider | |
4014 | ||
4015 | struct A { | |
4016 | int n; | |
4017 | constexpr A() : n(1) { n = 2; } // #1 | |
4018 | }; | |
4019 | struct B { | |
4020 | const A a; | |
4021 | constexpr B() { a.n = 3; } // #2 | |
4022 | }; | |
4023 | constexpr B b{}; | |
4024 | ||
4025 | #1 is OK, since we're modifying an object under construction, but | |
4026 | #2 is wrong, since "a" is const and has been fully constructed. | |
4027 | To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR | |
4028 | which means that the object is read-only. For the example above, the | |
4029 | *ctors stack at the point of #2 will look like: | |
4030 | ||
4031 | ctors[0] = {.a={.n=2}} TREE_READONLY = 0 | |
4032 | ctors[1] = {.n=2} TREE_READONLY = 1 | |
4033 | ||
4034 | and we're modifying "b.a", so we search the stack and see if the | |
4035 | constructor for "b.a" has already run. */ | |
4036 | if (const_object_being_modified) | |
4037 | { | |
4038 | bool fail = false; | |
4039 | if (!CLASS_TYPE_P (TREE_TYPE (const_object_being_modified))) | |
4040 | fail = true; | |
4041 | else | |
4042 | { | |
4043 | /* [class.ctor]p5 "A constructor can be invoked for a const, | |
4044 | volatile, or const volatile object. const and volatile | |
4045 | semantics are not applied on an object under construction. | |
4046 | They come into effect when the constructor for the most | |
4047 | derived object ends." */ | |
4048 | tree elt; | |
4049 | unsigned int i; | |
4050 | FOR_EACH_VEC_ELT (*ctors, i, elt) | |
4051 | if (same_type_ignoring_top_level_qualifiers_p | |
4052 | (TREE_TYPE (const_object_being_modified), TREE_TYPE (elt))) | |
4053 | { | |
4054 | fail = TREE_READONLY (elt); | |
4055 | break; | |
4056 | } | |
4057 | } | |
4058 | if (fail) | |
4059 | { | |
4060 | if (!ctx->quiet) | |
4061 | modifying_const_object_error (t, const_object_being_modified); | |
4062 | *non_constant_p = true; | |
4063 | return t; | |
4064 | } | |
4065 | } | |
4066 | ||
e8b3c1bc | 4067 | if (!preeval) |
60813a46 JM |
4068 | { |
4069 | /* Create a new CONSTRUCTOR in case evaluation of the initializer | |
4070 | wants to modify it. */ | |
acb2970c | 4071 | if (*valp == NULL_TREE) |
73a84269 | 4072 | { |
664beaf2 | 4073 | *valp = build_constructor (type, NULL); |
e8c48716 | 4074 | CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; |
73a84269 | 4075 | } |
664beaf2 | 4076 | new_ctx.ctor = *valp; |
60813a46 | 4077 | new_ctx.object = target; |
e8b3c1bc JM |
4078 | init = cxx_eval_constant_expression (&new_ctx, init, false, |
4079 | non_constant_p, overflow_p); | |
d0aa42d2 | 4080 | if (ctors->is_empty()) |
e8b3c1bc JM |
4081 | /* The hash table might have moved since the get earlier. */ |
4082 | valp = ctx->values->get (object); | |
60813a46 JM |
4083 | } |
4084 | ||
7574c916 | 4085 | /* Don't share a CONSTRUCTOR that might be changed later. */ |
0146e25f | 4086 | init = unshare_constructor (init); |
d96e8407 | 4087 | |
e8b3c1bc JM |
4088 | if (*valp && TREE_CODE (*valp) == CONSTRUCTOR |
4089 | && TREE_CODE (init) == CONSTRUCTOR) | |
acb2970c | 4090 | { |
d96e8407 JM |
4091 | /* An outer ctx->ctor might be pointing to *valp, so replace |
4092 | its contents. */ | |
d0aa42d2 JM |
4093 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init), |
4094 | TREE_TYPE (*valp))) | |
4095 | { | |
4096 | /* For initialization of an empty base, the original target will be | |
4097 | *(base*)this, evaluation of which resolves to the object | |
4098 | argument, which has the derived type rather than the base type. In | |
4099 | this situation, just evaluate the initializer and return, since | |
4100 | there's no actual data to store. */ | |
4101 | gcc_assert (is_empty_class (TREE_TYPE (init)) && !lval); | |
4102 | return init; | |
4103 | } | |
d96e8407 JM |
4104 | CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init); |
4105 | TREE_CONSTANT (*valp) = TREE_CONSTANT (init); | |
4106 | TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init); | |
e8c48716 JM |
4107 | CONSTRUCTOR_NO_CLEARING (*valp) |
4108 | = CONSTRUCTOR_NO_CLEARING (init); | |
acb2970c | 4109 | } |
60813a46 | 4110 | else |
d96e8407 | 4111 | *valp = init; |
2d63bc39 | 4112 | |
d96e8407 JM |
4113 | /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing |
4114 | CONSTRUCTORs, if any. */ | |
4115 | tree elt; | |
4116 | unsigned i; | |
4117 | bool c = TREE_CONSTANT (init); | |
4118 | bool s = TREE_SIDE_EFFECTS (init); | |
4119 | if (!c || s) | |
cd9cf97b | 4120 | FOR_EACH_VEC_ELT (*ctors, i, elt) |
d96e8407 JM |
4121 | { |
4122 | if (!c) | |
4123 | TREE_CONSTANT (elt) = false; | |
4124 | if (s) | |
4125 | TREE_SIDE_EFFECTS (elt) = true; | |
4126 | } | |
60813a46 JM |
4127 | |
4128 | if (*non_constant_p) | |
4129 | return t; | |
92a596e8 | 4130 | else if (lval) |
60813a46 JM |
4131 | return target; |
4132 | else | |
3bdf0a9b | 4133 | return init; |
60813a46 JM |
4134 | } |
4135 | ||
4136 | /* Evaluate a ++ or -- expression. */ | |
4137 | ||
4138 | static tree | |
4139 | cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t, | |
92a596e8 | 4140 | bool lval, |
60813a46 JM |
4141 | bool *non_constant_p, bool *overflow_p) |
4142 | { | |
4143 | enum tree_code code = TREE_CODE (t); | |
4144 | tree type = TREE_TYPE (t); | |
4145 | tree op = TREE_OPERAND (t, 0); | |
4146 | tree offset = TREE_OPERAND (t, 1); | |
4147 | gcc_assert (TREE_CONSTANT (offset)); | |
4148 | ||
d85569f6 MP |
4149 | /* OFFSET is constant, but perhaps not constant enough. We need to |
4150 | e.g. bash FLOAT_EXPRs to REAL_CSTs. */ | |
4151 | offset = fold_simple (offset); | |
4152 | ||
60813a46 | 4153 | /* The operand as an lvalue. */ |
2b3ab879 | 4154 | op = cxx_eval_constant_expression (ctx, op, true, |
5a804683 | 4155 | non_constant_p, overflow_p); |
60813a46 JM |
4156 | |
4157 | /* The operand as an rvalue. */ | |
84dd815f JM |
4158 | tree val |
4159 | = cxx_eval_constant_expression (ctx, op, false, | |
4160 | non_constant_p, overflow_p); | |
caee690e JM |
4161 | /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to |
4162 | a local array in a constexpr function. */ | |
71a93b08 | 4163 | bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val)); |
caee690e JM |
4164 | if (!ptr) |
4165 | VERIFY_CONSTANT (val); | |
60813a46 JM |
4166 | |
4167 | /* The modified value. */ | |
4168 | bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR); | |
ac2f8d26 | 4169 | tree mod; |
71a93b08 | 4170 | if (INDIRECT_TYPE_P (type)) |
ac2f8d26 JM |
4171 | { |
4172 | /* The middle end requires pointers to use POINTER_PLUS_EXPR. */ | |
4173 | offset = convert_to_ptrofftype (offset); | |
4174 | if (!inc) | |
4175 | offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset); | |
4176 | mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset); | |
4177 | } | |
4178 | else | |
4179 | mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset); | |
caee690e JM |
4180 | if (!ptr) |
4181 | VERIFY_CONSTANT (mod); | |
60813a46 JM |
4182 | |
4183 | /* Storing the modified value. */ | |
04e1749c MP |
4184 | tree store = build2_loc (cp_expr_loc_or_loc (t, input_location), |
4185 | MODIFY_EXPR, type, op, mod); | |
2b3ab879 | 4186 | cxx_eval_constant_expression (ctx, store, |
5a804683 | 4187 | true, non_constant_p, overflow_p); |
ecdcd560 | 4188 | ggc_free (store); |
60813a46 JM |
4189 | |
4190 | /* And the value of the expression. */ | |
4191 | if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR) | |
4192 | { | |
4193 | /* Prefix ops are lvalues. */ | |
92a596e8 | 4194 | if (lval) |
60813a46 JM |
4195 | return op; |
4196 | else | |
4197 | /* But we optimize when the caller wants an rvalue. */ | |
4198 | return mod; | |
4199 | } | |
4200 | else | |
4201 | /* Postfix ops are rvalues. */ | |
4202 | return val; | |
4203 | } | |
4204 | ||
56632b27 JM |
4205 | /* Predicates for the meaning of *jump_target. */ |
4206 | ||
4207 | static bool | |
4208 | returns (tree *jump_target) | |
4209 | { | |
4210 | return *jump_target | |
d49318d9 PC |
4211 | && (TREE_CODE (*jump_target) == RETURN_EXPR |
4212 | || (TREE_CODE (*jump_target) == LABEL_DECL | |
4213 | && LABEL_DECL_CDTOR (*jump_target))); | |
56632b27 JM |
4214 | } |
4215 | ||
4216 | static bool | |
4217 | breaks (tree *jump_target) | |
4218 | { | |
4219 | return *jump_target | |
06ec22b7 JM |
4220 | && ((TREE_CODE (*jump_target) == LABEL_DECL |
4221 | && LABEL_DECL_BREAK (*jump_target)) | |
3075affd | 4222 | || TREE_CODE (*jump_target) == BREAK_STMT |
06ec22b7 | 4223 | || TREE_CODE (*jump_target) == EXIT_EXPR); |
56632b27 JM |
4224 | } |
4225 | ||
4226 | static bool | |
4227 | continues (tree *jump_target) | |
4228 | { | |
4229 | return *jump_target | |
3075affd JM |
4230 | && ((TREE_CODE (*jump_target) == LABEL_DECL |
4231 | && LABEL_DECL_CONTINUE (*jump_target)) | |
4232 | || TREE_CODE (*jump_target) == CONTINUE_STMT); | |
4233 | ||
56632b27 JM |
4234 | } |
4235 | ||
4236 | static bool | |
4237 | switches (tree *jump_target) | |
4238 | { | |
4239 | return *jump_target | |
4240 | && TREE_CODE (*jump_target) == INTEGER_CST; | |
4241 | } | |
4242 | ||
4243 | /* Subroutine of cxx_eval_statement_list. Determine whether the statement | |
4b390698 JJ |
4244 | STMT matches *jump_target. If we're looking for a case label and we see |
4245 | the default label, note it in ctx->css_state. */ | |
56632b27 JM |
4246 | |
4247 | static bool | |
4b390698 | 4248 | label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt) |
56632b27 | 4249 | { |
56632b27 JM |
4250 | switch (TREE_CODE (*jump_target)) |
4251 | { | |
4252 | case LABEL_DECL: | |
4253 | if (TREE_CODE (stmt) == LABEL_EXPR | |
4254 | && LABEL_EXPR_LABEL (stmt) == *jump_target) | |
4255 | return true; | |
4256 | break; | |
4257 | ||
4258 | case INTEGER_CST: | |
4259 | if (TREE_CODE (stmt) == CASE_LABEL_EXPR) | |
4260 | { | |
4b390698 | 4261 | gcc_assert (ctx->css_state != NULL); |
56632b27 | 4262 | if (!CASE_LOW (stmt)) |
4b390698 JJ |
4263 | { |
4264 | /* default: should appear just once in a SWITCH_EXPR | |
4265 | body (excluding nested SWITCH_EXPR). */ | |
4266 | gcc_assert (*ctx->css_state != css_default_seen); | |
4267 | /* When evaluating SWITCH_EXPR body for the second time, | |
4268 | return true for the default: label. */ | |
4269 | if (*ctx->css_state == css_default_processing) | |
4270 | return true; | |
4271 | *ctx->css_state = css_default_seen; | |
4272 | } | |
385ed708 JJ |
4273 | else if (CASE_HIGH (stmt)) |
4274 | { | |
4275 | if (tree_int_cst_le (CASE_LOW (stmt), *jump_target) | |
4276 | && tree_int_cst_le (*jump_target, CASE_HIGH (stmt))) | |
4277 | return true; | |
4278 | } | |
56632b27 JM |
4279 | else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt))) |
4280 | return true; | |
4281 | } | |
4282 | break; | |
4283 | ||
43574e4f JJ |
4284 | case BREAK_STMT: |
4285 | case CONTINUE_STMT: | |
4286 | /* These two are handled directly in cxx_eval_loop_expr by testing | |
4287 | breaks (jump_target) or continues (jump_target). */ | |
4288 | break; | |
4289 | ||
56632b27 JM |
4290 | default: |
4291 | gcc_unreachable (); | |
4292 | } | |
4293 | return false; | |
4294 | } | |
4295 | ||
4296 | /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump | |
4297 | semantics, for switch, break, continue, and return. */ | |
4298 | ||
4299 | static tree | |
4300 | cxx_eval_statement_list (const constexpr_ctx *ctx, tree t, | |
56632b27 JM |
4301 | bool *non_constant_p, bool *overflow_p, |
4302 | tree *jump_target) | |
4303 | { | |
4304 | tree_stmt_iterator i; | |
58611fb6 | 4305 | tree local_target; |
345edb37 JJ |
4306 | /* In a statement-expression we want to return the last value. |
4307 | For empty statement expression return void_node. */ | |
4308 | tree r = void_node; | |
58611fb6 JM |
4309 | if (!jump_target) |
4310 | { | |
4311 | local_target = NULL_TREE; | |
4312 | jump_target = &local_target; | |
4313 | } | |
56632b27 JM |
4314 | for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) |
4315 | { | |
56632b27 | 4316 | tree stmt = tsi_stmt (i); |
0250ba58 MP |
4317 | /* We've found a continue, so skip everything until we reach |
4318 | the label its jumping to. */ | |
4319 | if (continues (jump_target)) | |
4320 | { | |
4321 | if (label_matches (ctx, jump_target, stmt)) | |
4322 | /* Found it. */ | |
4323 | *jump_target = NULL_TREE; | |
4324 | else | |
4325 | continue; | |
4326 | } | |
6ef72c36 JJ |
4327 | if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT) |
4328 | continue; | |
58611fb6 JM |
4329 | r = cxx_eval_constant_expression (ctx, stmt, false, |
4330 | non_constant_p, overflow_p, | |
4331 | jump_target); | |
56632b27 JM |
4332 | if (*non_constant_p) |
4333 | break; | |
4334 | if (returns (jump_target) || breaks (jump_target)) | |
4335 | break; | |
4336 | } | |
ce36ba09 JM |
4337 | if (*jump_target && jump_target == &local_target) |
4338 | { | |
4339 | /* We aren't communicating the jump to our caller, so give up. We don't | |
4340 | need to support evaluation of jumps out of statement-exprs. */ | |
4341 | if (!ctx->quiet) | |
f9d0ca40 | 4342 | error_at (cp_expr_loc_or_input_loc (r), |
ce36ba09 JM |
4343 | "statement is not a constant expression"); |
4344 | *non_constant_p = true; | |
4345 | } | |
58611fb6 | 4346 | return r; |
56632b27 JM |
4347 | } |
4348 | ||
4349 | /* Evaluate a LOOP_EXPR for side-effects. Handles break and return | |
4350 | semantics; continue semantics are covered by cxx_eval_statement_list. */ | |
4351 | ||
4352 | static tree | |
4353 | cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t, | |
56632b27 JM |
4354 | bool *non_constant_p, bool *overflow_p, |
4355 | tree *jump_target) | |
4356 | { | |
39dce2b7 JM |
4357 | constexpr_ctx new_ctx = *ctx; |
4358 | ||
43574e4f | 4359 | tree body, cond = NULL_TREE, expr = NULL_TREE; |
5ec2cd9f | 4360 | int count = 0; |
43574e4f JJ |
4361 | switch (TREE_CODE (t)) |
4362 | { | |
4363 | case LOOP_EXPR: | |
4364 | body = LOOP_EXPR_BODY (t); | |
4365 | break; | |
4366 | case DO_STMT: | |
4367 | body = DO_BODY (t); | |
4368 | cond = DO_COND (t); | |
4369 | break; | |
4370 | case WHILE_STMT: | |
4371 | body = WHILE_BODY (t); | |
4372 | cond = WHILE_COND (t); | |
4373 | count = -1; | |
4374 | break; | |
4375 | case FOR_STMT: | |
4376 | if (FOR_INIT_STMT (t)) | |
4377 | cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), /*lval*/false, | |
4378 | non_constant_p, overflow_p, jump_target); | |
4379 | if (*non_constant_p) | |
4380 | return NULL_TREE; | |
4381 | body = FOR_BODY (t); | |
4382 | cond = FOR_COND (t); | |
4383 | expr = FOR_EXPR (t); | |
4384 | count = -1; | |
4385 | break; | |
4386 | default: | |
4387 | gcc_unreachable (); | |
4388 | } | |
0ee28590 | 4389 | auto_vec<tree, 10> save_exprs; |
43574e4f | 4390 | new_ctx.save_exprs = &save_exprs; |
d259b234 | 4391 | do |
56632b27 | 4392 | { |
43574e4f JJ |
4393 | if (count != -1) |
4394 | { | |
4395 | if (body) | |
4396 | cxx_eval_constant_expression (&new_ctx, body, /*lval*/false, | |
4397 | non_constant_p, overflow_p, | |
4398 | jump_target); | |
4399 | if (breaks (jump_target)) | |
4400 | { | |
4401 | *jump_target = NULL_TREE; | |
4402 | break; | |
4403 | } | |
39dce2b7 | 4404 | |
43574e4f JJ |
4405 | if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target)) |
4406 | *jump_target = NULL_TREE; | |
4407 | ||
4408 | if (expr) | |
4409 | cxx_eval_constant_expression (&new_ctx, expr, /*lval*/false, | |
4410 | non_constant_p, overflow_p, | |
4411 | jump_target); | |
4412 | } | |
4413 | ||
4414 | if (cond) | |
4415 | { | |
4416 | tree res | |
4417 | = cxx_eval_constant_expression (&new_ctx, cond, /*lval*/false, | |
4418 | non_constant_p, overflow_p, | |
4419 | jump_target); | |
4420 | if (res) | |
4421 | { | |
4422 | if (verify_constant (res, ctx->quiet, non_constant_p, | |
4423 | overflow_p)) | |
4424 | break; | |
4425 | if (integer_zerop (res)) | |
4426 | break; | |
4427 | } | |
4428 | else | |
4429 | gcc_assert (*jump_target); | |
4430 | } | |
39dce2b7 JM |
4431 | |
4432 | /* Forget saved values of SAVE_EXPRs. */ | |
0ee28590 JJ |
4433 | unsigned int i; |
4434 | tree save_expr; | |
4435 | FOR_EACH_VEC_ELT (save_exprs, i, save_expr) | |
4436 | new_ctx.values->remove (save_expr); | |
4437 | save_exprs.truncate (0); | |
43574e4f | 4438 | |
5ec2cd9f JM |
4439 | if (++count >= constexpr_loop_limit) |
4440 | { | |
4441 | if (!ctx->quiet) | |
f9d0ca40 | 4442 | error_at (cp_expr_loc_or_input_loc (t), |
84fa214d | 4443 | "%<constexpr%> loop iteration count exceeds limit of %d " |
a3f9f006 | 4444 | "(use %<-fconstexpr-loop-limit=%> to increase the limit)", |
5ec2cd9f JM |
4445 | constexpr_loop_limit); |
4446 | *non_constant_p = true; | |
4447 | break; | |
4448 | } | |
56632b27 | 4449 | } |
4b390698 JJ |
4450 | while (!returns (jump_target) |
4451 | && !breaks (jump_target) | |
43574e4f JJ |
4452 | && !continues (jump_target) |
4453 | && (!switches (jump_target) || count == 0) | |
4b390698 | 4454 | && !*non_constant_p); |
d259b234 | 4455 | |
43574e4f | 4456 | /* Forget saved values of SAVE_EXPRs. */ |
0ee28590 JJ |
4457 | unsigned int i; |
4458 | tree save_expr; | |
4459 | FOR_EACH_VEC_ELT (save_exprs, i, save_expr) | |
4460 | new_ctx.values->remove (save_expr); | |
39dce2b7 | 4461 | |
56632b27 JM |
4462 | return NULL_TREE; |
4463 | } | |
4464 | ||
4465 | /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump | |
4466 | semantics. */ | |
4467 | ||
4468 | static tree | |
4469 | cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t, | |
56632b27 JM |
4470 | bool *non_constant_p, bool *overflow_p, |
4471 | tree *jump_target) | |
4472 | { | |
43574e4f JJ |
4473 | tree cond |
4474 | = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t); | |
2b3ab879 | 4475 | cond = cxx_eval_constant_expression (ctx, cond, false, |
5a804683 | 4476 | non_constant_p, overflow_p); |
56632b27 JM |
4477 | VERIFY_CONSTANT (cond); |
4478 | *jump_target = cond; | |
4479 | ||
43574e4f JJ |
4480 | tree body |
4481 | = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t); | |
4b390698 JJ |
4482 | constexpr_ctx new_ctx = *ctx; |
4483 | constexpr_switch_state css = css_default_not_seen; | |
4484 | new_ctx.css_state = &css; | |
4485 | cxx_eval_constant_expression (&new_ctx, body, false, | |
4486 | non_constant_p, overflow_p, jump_target); | |
4487 | if (switches (jump_target) && css == css_default_seen) | |
4488 | { | |
4489 | /* If the SWITCH_EXPR body has default: label, process it once again, | |
4490 | this time instructing label_matches to return true for default: | |
4491 | label on switches (jump_target). */ | |
4492 | css = css_default_processing; | |
4493 | cxx_eval_constant_expression (&new_ctx, body, false, | |
4494 | non_constant_p, overflow_p, jump_target); | |
4495 | } | |
56632b27 JM |
4496 | if (breaks (jump_target) || switches (jump_target)) |
4497 | *jump_target = NULL_TREE; | |
4498 | return NULL_TREE; | |
4499 | } | |
4500 | ||
89262ec6 JM |
4501 | /* Find the object of TYPE under initialization in CTX. */ |
4502 | ||
4503 | static tree | |
4504 | lookup_placeholder (const constexpr_ctx *ctx, bool lval, tree type) | |
4505 | { | |
fbd603c4 | 4506 | if (!ctx) |
89262ec6 JM |
4507 | return NULL_TREE; |
4508 | ||
4509 | /* We could use ctx->object unconditionally, but using ctx->ctor when we | |
4510 | can is a minor optimization. */ | |
fbd603c4 | 4511 | if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type)) |
89262ec6 JM |
4512 | return ctx->ctor; |
4513 | ||
fbd603c4 JM |
4514 | if (!ctx->object) |
4515 | return NULL_TREE; | |
4516 | ||
89262ec6 JM |
4517 | /* Since an object cannot have a field of its own type, we can search outward |
4518 | from ctx->object to find the unique containing object of TYPE. */ | |
4519 | tree ob = ctx->object; | |
4520 | while (ob) | |
4521 | { | |
4522 | if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type)) | |
4523 | break; | |
4524 | if (handled_component_p (ob)) | |
4525 | ob = TREE_OPERAND (ob, 0); | |
4526 | else | |
4527 | ob = NULL_TREE; | |
4528 | } | |
4529 | ||
4530 | return ob; | |
4531 | } | |
4532 | ||
7c814975 MP |
4533 | /* Complain about an attempt to evaluate inline assembly. */ |
4534 | ||
4535 | static void | |
4536 | inline_asm_in_constexpr_error (location_t loc) | |
4537 | { | |
4538 | auto_diagnostic_group d; | |
4539 | error_at (loc, "inline assembly is not a constant expression"); | |
4540 | inform (loc, "only unevaluated inline assembly is allowed in a " | |
4541 | "%<constexpr%> function in C++2a"); | |
4542 | } | |
4543 | ||
2d76680f PC |
4544 | /* Attempt to reduce the expression T to a constant value. |
4545 | On failure, issue diagnostic and return error_mark_node. */ | |
4546 | /* FIXME unify with c_fully_fold */ | |
60813a46 | 4547 | /* FIXME overflow_p is too global */ |
2d76680f PC |
4548 | |
4549 | static tree | |
3e605b20 | 4550 | cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, |
92a596e8 | 4551 | bool lval, |
56632b27 | 4552 | bool *non_constant_p, bool *overflow_p, |
3075affd | 4553 | tree *jump_target /* = NULL */) |
2d76680f | 4554 | { |
4b390698 JJ |
4555 | if (jump_target && *jump_target) |
4556 | { | |
4557 | /* If we are jumping, ignore all statements/expressions except those | |
4558 | that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */ | |
4559 | switch (TREE_CODE (t)) | |
4560 | { | |
4561 | case BIND_EXPR: | |
4562 | case STATEMENT_LIST: | |
4563 | case LOOP_EXPR: | |
4564 | case COND_EXPR: | |
43574e4f JJ |
4565 | case IF_STMT: |
4566 | case DO_STMT: | |
4567 | case WHILE_STMT: | |
4568 | case FOR_STMT: | |
4b390698 JJ |
4569 | break; |
4570 | case LABEL_EXPR: | |
4571 | case CASE_LABEL_EXPR: | |
4572 | if (label_matches (ctx, jump_target, t)) | |
4573 | /* Found it. */ | |
4574 | *jump_target = NULL_TREE; | |
4575 | return NULL_TREE; | |
4576 | default: | |
4577 | return NULL_TREE; | |
4578 | } | |
4579 | } | |
3075affd | 4580 | if (error_operand_p (t)) |
2d76680f PC |
4581 | { |
4582 | *non_constant_p = true; | |
4583 | return t; | |
4584 | } | |
7a649ef5 JM |
4585 | |
4586 | STRIP_ANY_LOCATION_WRAPPER (t); | |
4587 | ||
2d76680f PC |
4588 | if (CONSTANT_CLASS_P (t)) |
4589 | { | |
61637db3 JJ |
4590 | if (TREE_OVERFLOW (t)) |
4591 | { | |
4592 | if (!ctx->quiet) | |
4593 | permerror (input_location, "overflow in constant expression"); | |
4594 | if (!flag_permissive || ctx->quiet) | |
4595 | *overflow_p = true; | |
4596 | } | |
8bada5cd MS |
4597 | |
4598 | if (TREE_CODE (t) == INTEGER_CST | |
9f613f06 | 4599 | && TYPE_PTR_P (TREE_TYPE (t)) |
8bada5cd MS |
4600 | && !integer_zerop (t)) |
4601 | { | |
4602 | if (!ctx->quiet) | |
4603 | error ("value %qE of type %qT is not a constant expression", | |
4604 | t, TREE_TYPE (t)); | |
4605 | *non_constant_p = true; | |
4606 | } | |
4607 | ||
2d76680f PC |
4608 | return t; |
4609 | } | |
2d76680f | 4610 | |
a15ffa22 | 4611 | /* Avoid excessively long constexpr evaluations. */ |
7a649ef5 | 4612 | if (++*ctx->constexpr_ops_count >= constexpr_ops_limit) |
a15ffa22 JJ |
4613 | { |
4614 | if (!ctx->quiet) | |
f9d0ca40 | 4615 | error_at (cp_expr_loc_or_input_loc (t), |
a15ffa22 | 4616 | "%<constexpr%> evaluation operation count exceeds limit of " |
a9c697b8 | 4617 | "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)", |
a15ffa22 JJ |
4618 | constexpr_ops_limit); |
4619 | *ctx->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT); | |
4620 | *non_constant_p = true; | |
4621 | return t; | |
4622 | } | |
4623 | ||
7a649ef5 JM |
4624 | constexpr_ctx new_ctx; |
4625 | tree r = t; | |
4626 | ||
8bada5cd MS |
4627 | tree_code tcode = TREE_CODE (t); |
4628 | switch (tcode) | |
2d76680f | 4629 | { |
60813a46 | 4630 | case RESULT_DECL: |
92a596e8 | 4631 | if (lval) |
60813a46 JM |
4632 | return t; |
4633 | /* We ask for an rvalue for the RESULT_DECL when indirecting | |
bf66b9b4 AH |
4634 | through an invisible reference, or in named return value |
4635 | optimization. */ | |
1efb1dc2 MP |
4636 | if (tree *p = ctx->values->get (t)) |
4637 | return *p; | |
4638 | else | |
4639 | { | |
4640 | if (!ctx->quiet) | |
4641 | error ("%qE is not a constant expression", t); | |
4642 | *non_constant_p = true; | |
4643 | } | |
4644 | break; | |
60813a46 | 4645 | |
2d76680f | 4646 | case VAR_DECL: |
70f40fea | 4647 | if (DECL_HAS_VALUE_EXPR_P (t)) |
c59fa7ea JM |
4648 | { |
4649 | if (is_normal_capture_proxy (t) | |
4650 | && current_function_decl == DECL_CONTEXT (t)) | |
4651 | { | |
4652 | /* Function parms aren't constexpr within the function | |
4653 | definition, so don't try to look at the closure. But if the | |
4654 | captured variable is constant, try to evaluate it directly. */ | |
4655 | r = DECL_CAPTURED_VARIABLE (t); | |
4656 | tree type = TREE_TYPE (t); | |
4657 | if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r))) | |
4658 | { | |
4659 | /* Adjust r to match the reference-ness of t. */ | |
4660 | if (TYPE_REF_P (type)) | |
4661 | r = build_address (r); | |
4662 | else | |
4663 | r = convert_from_reference (r); | |
4664 | } | |
4665 | } | |
4666 | else | |
4667 | r = DECL_VALUE_EXPR (t); | |
4668 | return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, | |
4669 | overflow_p); | |
4670 | } | |
191816a3 | 4671 | /* fall through */ |
7c83622c JM |
4672 | case CONST_DECL: |
4673 | /* We used to not check lval for CONST_DECL, but darwin.c uses | |
4674 | CONST_DECL for aggregate constants. */ | |
92a596e8 | 4675 | if (lval) |
2d76680f | 4676 | return t; |
f4fce183 | 4677 | if (COMPLETE_TYPE_P (TREE_TYPE (t)) |
dbcd32f8 | 4678 | && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
7dc2b4a2 JM |
4679 | { |
4680 | /* If the class is empty, we aren't actually loading anything. */ | |
4681 | r = build_constructor (TREE_TYPE (t), NULL); | |
4682 | TREE_CONSTANT (r) = true; | |
4683 | } | |
4684 | else if (ctx->strict) | |
69eb4fde JM |
4685 | r = decl_really_constant_value (t); |
4686 | else | |
4687 | r = decl_constant_value (t); | |
2d76680f PC |
4688 | if (TREE_CODE (r) == TARGET_EXPR |
4689 | && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR) | |
4690 | r = TARGET_EXPR_INITIAL (r); | |
56a6f1d3 | 4691 | if (VAR_P (r)) |
3e605b20 | 4692 | if (tree *p = ctx->values->get (r)) |
bd8f5bb2 MP |
4693 | if (*p != NULL_TREE) |
4694 | r = *p; | |
2d76680f PC |
4695 | if (DECL_P (r)) |
4696 | { | |
2b3ab879 | 4697 | if (!ctx->quiet) |
2d76680f PC |
4698 | non_const_var_error (r); |
4699 | *non_constant_p = true; | |
4700 | } | |
4701 | break; | |
4702 | ||
96a95ac1 AO |
4703 | case DEBUG_BEGIN_STMT: |
4704 | /* ??? It might be nice to retain this information somehow, so | |
4705 | as to be able to step into a constexpr function call. */ | |
4706 | /* Fall through. */ | |
4707 | ||
2d76680f PC |
4708 | case FUNCTION_DECL: |
4709 | case TEMPLATE_DECL: | |
4710 | case LABEL_DECL: | |
56632b27 JM |
4711 | case LABEL_EXPR: |
4712 | case CASE_LABEL_EXPR: | |
4b390698 | 4713 | case PREDICT_EXPR: |
2d76680f PC |
4714 | return t; |
4715 | ||
4716 | case PARM_DECL: | |
9f613f06 | 4717 | if (lval && !TYPE_REF_P (TREE_TYPE (t))) |
60813a46 JM |
4718 | /* glvalue use. */; |
4719 | else if (tree *p = ctx->values->get (r)) | |
4720 | r = *p; | |
92a596e8 | 4721 | else if (lval) |
2d76680f | 4722 | /* Defer in case this is only used for its type. */; |
9f613f06 | 4723 | else if (TYPE_REF_P (TREE_TYPE (t))) |
f2acb8ad | 4724 | /* Defer, there's no lvalue->rvalue conversion. */; |
1c2f613f | 4725 | else if (COMPLETE_TYPE_P (TREE_TYPE (t)) |
dbcd32f8 | 4726 | && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
052beba4 JM |
4727 | { |
4728 | /* If the class is empty, we aren't actually loading anything. */ | |
4729 | r = build_constructor (TREE_TYPE (t), NULL); | |
4730 | TREE_CONSTANT (r) = true; | |
4731 | } | |
2d76680f PC |
4732 | else |
4733 | { | |
2b3ab879 | 4734 | if (!ctx->quiet) |
2d76680f PC |
4735 | error ("%qE is not a constant expression", t); |
4736 | *non_constant_p = true; | |
4737 | } | |
4738 | break; | |
4739 | ||
4740 | case CALL_EXPR: | |
4741 | case AGGR_INIT_EXPR: | |
92a596e8 | 4742 | r = cxx_eval_call_expression (ctx, t, lval, |
2d76680f PC |
4743 | non_constant_p, overflow_p); |
4744 | break; | |
4745 | ||
60813a46 JM |
4746 | case DECL_EXPR: |
4747 | { | |
4748 | r = DECL_EXPR_DECL (t); | |
43574e4f JJ |
4749 | if (TREE_CODE (r) == USING_DECL) |
4750 | { | |
4751 | r = void_node; | |
4752 | break; | |
4753 | } | |
60813a46 JM |
4754 | if (AGGREGATE_TYPE_P (TREE_TYPE (r)) |
4755 | || VECTOR_TYPE_P (TREE_TYPE (r))) | |
4756 | { | |
4757 | new_ctx = *ctx; | |
4758 | new_ctx.object = r; | |
4759 | new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL); | |
e8c48716 | 4760 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; |
60813a46 JM |
4761 | new_ctx.values->put (r, new_ctx.ctor); |
4762 | ctx = &new_ctx; | |
4763 | } | |
4764 | ||
4765 | if (tree init = DECL_INITIAL (r)) | |
4766 | { | |
4767 | init = cxx_eval_constant_expression (ctx, init, | |
2b3ab879 | 4768 | false, |
5a804683 | 4769 | non_constant_p, overflow_p); |
7f0e23e9 | 4770 | /* Don't share a CONSTRUCTOR that might be changed. */ |
0146e25f | 4771 | init = unshare_constructor (init); |
04e1749c MP |
4772 | /* Remember that a constant object's constructor has already |
4773 | run. */ | |
4774 | if (CLASS_TYPE_P (TREE_TYPE (r)) | |
4775 | && CP_TYPE_CONST_P (TREE_TYPE (r))) | |
4776 | TREE_READONLY (init) = true; | |
60813a46 JM |
4777 | ctx->values->put (r, init); |
4778 | } | |
4779 | else if (ctx == &new_ctx) | |
4780 | /* We gave it a CONSTRUCTOR above. */; | |
4781 | else | |
4782 | ctx->values->put (r, NULL_TREE); | |
4783 | } | |
4784 | break; | |
4785 | ||
2d76680f PC |
4786 | case TARGET_EXPR: |
4787 | if (!literal_type_p (TREE_TYPE (t))) | |
4788 | { | |
2b3ab879 | 4789 | if (!ctx->quiet) |
2d76680f | 4790 | { |
097f82ec | 4791 | auto_diagnostic_group d; |
2d76680f PC |
4792 | error ("temporary of non-literal type %qT in a " |
4793 | "constant expression", TREE_TYPE (t)); | |
4794 | explain_non_literal_class (TREE_TYPE (t)); | |
4795 | } | |
4796 | *non_constant_p = true; | |
4797 | break; | |
4798 | } | |
3e605b20 JM |
4799 | if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t)))) |
4800 | { | |
4801 | /* We're being expanded without an explicit target, so start | |
4802 | initializing a new object; expansion with an explicit target | |
4803 | strips the TARGET_EXPR before we get here. */ | |
4804 | new_ctx = *ctx; | |
4805 | new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL); | |
e8c48716 | 4806 | CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; |
3e605b20 JM |
4807 | new_ctx.object = TARGET_EXPR_SLOT (t); |
4808 | ctx->values->put (new_ctx.object, new_ctx.ctor); | |
4809 | ctx = &new_ctx; | |
4810 | } | |
92a596e8 | 4811 | /* Pass false for 'lval' because this indicates |
2d76680f | 4812 | initialization of a temporary. */ |
3e605b20 | 4813 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
2b3ab879 | 4814 | false, |
5a804683 | 4815 | non_constant_p, overflow_p); |
2d76680f PC |
4816 | if (!*non_constant_p) |
4817 | /* Adjust the type of the result to the type of the temporary. */ | |
4818 | r = adjust_temp_type (TREE_TYPE (t), r); | |
92a596e8 | 4819 | if (lval) |
f2acb8ad JM |
4820 | { |
4821 | tree slot = TARGET_EXPR_SLOT (t); | |
0146e25f | 4822 | r = unshare_constructor (r); |
f2acb8ad JM |
4823 | ctx->values->put (slot, r); |
4824 | return slot; | |
4825 | } | |
2d76680f PC |
4826 | break; |
4827 | ||
60813a46 | 4828 | case INIT_EXPR: |
60813a46 | 4829 | case MODIFY_EXPR: |
4b390698 | 4830 | gcc_assert (jump_target == NULL || *jump_target == NULL_TREE); |
92a596e8 | 4831 | r = cxx_eval_store_expression (ctx, t, lval, |
60813a46 JM |
4832 | non_constant_p, overflow_p); |
4833 | break; | |
4834 | ||
2d76680f | 4835 | case SCOPE_REF: |
3e605b20 | 4836 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
92a596e8 | 4837 | lval, |
5a804683 | 4838 | non_constant_p, overflow_p); |
2d76680f PC |
4839 | break; |
4840 | ||
4841 | case RETURN_EXPR: | |
75e0295b MP |
4842 | if (TREE_OPERAND (t, 0) != NULL_TREE) |
4843 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), | |
4844 | lval, | |
4845 | non_constant_p, overflow_p); | |
43574e4f JJ |
4846 | /* FALLTHRU */ |
4847 | case BREAK_STMT: | |
4848 | case CONTINUE_STMT: | |
019e0ae8 MP |
4849 | if (jump_target) |
4850 | *jump_target = t; | |
4851 | else | |
4852 | { | |
4853 | /* Can happen with ({ return true; }) && false; passed to | |
4854 | maybe_constant_value. There is nothing to jump over in this | |
4855 | case, and the bug will be diagnosed later. */ | |
4856 | gcc_assert (ctx->quiet); | |
4857 | *non_constant_p = true; | |
4858 | } | |
56632b27 JM |
4859 | break; |
4860 | ||
cabaf50a JM |
4861 | case SAVE_EXPR: |
4862 | /* Avoid evaluating a SAVE_EXPR more than once. */ | |
4863 | if (tree *p = ctx->values->get (t)) | |
4864 | r = *p; | |
4865 | else | |
4866 | { | |
12d9ce19 | 4867 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false, |
cabaf50a JM |
4868 | non_constant_p, overflow_p); |
4869 | ctx->values->put (t, r); | |
39dce2b7 | 4870 | if (ctx->save_exprs) |
0ee28590 | 4871 | ctx->save_exprs->safe_push (t); |
cabaf50a JM |
4872 | } |
4873 | break; | |
4874 | ||
2d76680f PC |
4875 | case NON_LVALUE_EXPR: |
4876 | case TRY_CATCH_EXPR: | |
769430b2 | 4877 | case TRY_BLOCK: |
2d76680f PC |
4878 | case CLEANUP_POINT_EXPR: |
4879 | case MUST_NOT_THROW_EXPR: | |
60813a46 JM |
4880 | case EXPR_STMT: |
4881 | case EH_SPEC_BLOCK: | |
3e605b20 | 4882 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
92a596e8 | 4883 | lval, |
56632b27 JM |
4884 | non_constant_p, overflow_p, |
4885 | jump_target); | |
2d76680f PC |
4886 | break; |
4887 | ||
769430b2 JM |
4888 | case TRY_FINALLY_EXPR: |
4889 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, | |
4890 | non_constant_p, overflow_p, | |
4891 | jump_target); | |
4892 | if (!*non_constant_p) | |
4893 | /* Also evaluate the cleanup. */ | |
4894 | cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true, | |
4895 | non_constant_p, overflow_p, | |
4896 | jump_target); | |
4897 | break; | |
4898 | ||
43574e4f JJ |
4899 | case CLEANUP_STMT: |
4900 | r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval, | |
4901 | non_constant_p, overflow_p, | |
4902 | jump_target); | |
4903 | if (!CLEANUP_EH_ONLY (t) && !*non_constant_p) | |
4904 | /* Also evaluate the cleanup. */ | |
4905 | cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), true, | |
4906 | non_constant_p, overflow_p, | |
4907 | jump_target); | |
4908 | break; | |
4909 | ||
2d76680f PC |
4910 | /* These differ from cxx_eval_unary_expression in that this doesn't |
4911 | check for a constant operand or result; an address can be | |
4912 | constant without its operand being, and vice versa. */ | |
cda0a029 | 4913 | case MEM_REF: |
2d76680f | 4914 | case INDIRECT_REF: |
92a596e8 | 4915 | r = cxx_eval_indirect_ref (ctx, t, lval, |
2d76680f PC |
4916 | non_constant_p, overflow_p); |
4917 | break; | |
4918 | ||
4919 | case ADDR_EXPR: | |
4920 | { | |
4921 | tree oldop = TREE_OPERAND (t, 0); | |
3e605b20 | 4922 | tree op = cxx_eval_constant_expression (ctx, oldop, |
92a596e8 | 4923 | /*lval*/true, |
5a804683 | 4924 | non_constant_p, overflow_p); |
2d76680f PC |
4925 | /* Don't VERIFY_CONSTANT here. */ |
4926 | if (*non_constant_p) | |
4927 | return t; | |
f2acb8ad | 4928 | gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR); |
2d76680f PC |
4929 | /* This function does more aggressive folding than fold itself. */ |
4930 | r = build_fold_addr_expr_with_type (op, TREE_TYPE (t)); | |
4931 | if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop) | |
7a649ef5 JM |
4932 | { |
4933 | ggc_free (r); | |
4934 | return t; | |
4935 | } | |
2d76680f PC |
4936 | break; |
4937 | } | |
4938 | ||
4939 | case REALPART_EXPR: | |
4940 | case IMAGPART_EXPR: | |
87713c6a JJ |
4941 | if (lval) |
4942 | { | |
4943 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, | |
4944 | non_constant_p, overflow_p); | |
4945 | if (r == error_mark_node) | |
4946 | ; | |
4947 | else if (r == TREE_OPERAND (t, 0)) | |
4948 | r = t; | |
4949 | else | |
4950 | r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r); | |
4951 | break; | |
4952 | } | |
4953 | /* FALLTHRU */ | |
2d76680f PC |
4954 | case CONJ_EXPR: |
4955 | case FIX_TRUNC_EXPR: | |
4956 | case FLOAT_EXPR: | |
4957 | case NEGATE_EXPR: | |
4958 | case ABS_EXPR: | |
e28fadbc | 4959 | case ABSU_EXPR: |
2d76680f PC |
4960 | case BIT_NOT_EXPR: |
4961 | case TRUTH_NOT_EXPR: | |
4962 | case FIXED_CONVERT_EXPR: | |
92a596e8 | 4963 | r = cxx_eval_unary_expression (ctx, t, lval, |
2d76680f PC |
4964 | non_constant_p, overflow_p); |
4965 | break; | |
4966 | ||
4967 | case SIZEOF_EXPR: | |
99e764a2 MP |
4968 | r = fold_sizeof_expr (t); |
4969 | /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR, | |
4970 | which could lead to an infinite recursion. */ | |
4971 | if (TREE_CODE (r) != SIZEOF_EXPR) | |
4972 | r = cxx_eval_constant_expression (ctx, r, lval, | |
4973 | non_constant_p, overflow_p, | |
4974 | jump_target); | |
4975 | else | |
4976 | { | |
4977 | *non_constant_p = true; | |
4978 | gcc_assert (ctx->quiet); | |
4979 | } | |
4980 | ||
2d76680f PC |
4981 | break; |
4982 | ||
4983 | case COMPOUND_EXPR: | |
4984 | { | |
4985 | /* check_return_expr sometimes wraps a TARGET_EXPR in a | |
4986 | COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR | |
4987 | introduced by build_call_a. */ | |
4988 | tree op0 = TREE_OPERAND (t, 0); | |
4989 | tree op1 = TREE_OPERAND (t, 1); | |
4990 | STRIP_NOPS (op1); | |
4991 | if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) | |
4992 | || TREE_CODE (op1) == EMPTY_CLASS_EXPR) | |
2b3ab879 | 4993 | r = cxx_eval_constant_expression (ctx, op0, |
92a596e8 | 4994 | lval, non_constant_p, overflow_p, |
56632b27 | 4995 | jump_target); |
2d76680f PC |
4996 | else |
4997 | { | |
4998 | /* Check that the LHS is constant and then discard it. */ | |
2b3ab879 | 4999 | cxx_eval_constant_expression (ctx, op0, |
12d9ce19 | 5000 | true, non_constant_p, overflow_p, |
56632b27 | 5001 | jump_target); |
06ec22b7 JM |
5002 | if (*non_constant_p) |
5003 | return t; | |
2d76680f | 5004 | op1 = TREE_OPERAND (t, 1); |
2b3ab879 | 5005 | r = cxx_eval_constant_expression (ctx, op1, |
92a596e8 | 5006 | lval, non_constant_p, overflow_p, |
56632b27 | 5007 | jump_target); |
2d76680f PC |
5008 | } |
5009 | } | |
5010 | break; | |
5011 | ||
5012 | case POINTER_PLUS_EXPR: | |
1af4ebf5 | 5013 | case POINTER_DIFF_EXPR: |
2d76680f PC |
5014 | case PLUS_EXPR: |
5015 | case MINUS_EXPR: | |
5016 | case MULT_EXPR: | |
5017 | case TRUNC_DIV_EXPR: | |
5018 | case CEIL_DIV_EXPR: | |
5019 | case FLOOR_DIV_EXPR: | |
5020 | case ROUND_DIV_EXPR: | |
5021 | case TRUNC_MOD_EXPR: | |
5022 | case CEIL_MOD_EXPR: | |
5023 | case ROUND_MOD_EXPR: | |
5024 | case RDIV_EXPR: | |
5025 | case EXACT_DIV_EXPR: | |
5026 | case MIN_EXPR: | |
5027 | case MAX_EXPR: | |
5028 | case LSHIFT_EXPR: | |
5029 | case RSHIFT_EXPR: | |
5030 | case LROTATE_EXPR: | |
5031 | case RROTATE_EXPR: | |
5032 | case BIT_IOR_EXPR: | |
5033 | case BIT_XOR_EXPR: | |
5034 | case BIT_AND_EXPR: | |
5035 | case TRUTH_XOR_EXPR: | |
5036 | case LT_EXPR: | |
5037 | case LE_EXPR: | |
5038 | case GT_EXPR: | |
5039 | case GE_EXPR: | |
5040 | case EQ_EXPR: | |
5041 | case NE_EXPR: | |
5042 | case UNORDERED_EXPR: | |
5043 | case ORDERED_EXPR: | |
5044 | case UNLT_EXPR: | |
5045 | case UNLE_EXPR: | |
5046 | case UNGT_EXPR: | |
5047 | case UNGE_EXPR: | |
5048 | case UNEQ_EXPR: | |
5049 | case LTGT_EXPR: | |
5050 | case RANGE_EXPR: | |
5051 | case COMPLEX_EXPR: | |
92a596e8 | 5052 | r = cxx_eval_binary_expression (ctx, t, lval, |
2d76680f PC |
5053 | non_constant_p, overflow_p); |
5054 | break; | |
5055 | ||
5056 | /* fold can introduce non-IF versions of these; still treat them as | |
5057 | short-circuiting. */ | |
5058 | case TRUTH_AND_EXPR: | |
5059 | case TRUTH_ANDIF_EXPR: | |
3e605b20 | 5060 | r = cxx_eval_logical_expression (ctx, t, boolean_false_node, |
2d76680f | 5061 | boolean_true_node, |
92a596e8 | 5062 | lval, |
2d76680f PC |
5063 | non_constant_p, overflow_p); |
5064 | break; | |
5065 | ||
5066 | case TRUTH_OR_EXPR: | |
5067 | case TRUTH_ORIF_EXPR: | |
3e605b20 | 5068 | r = cxx_eval_logical_expression (ctx, t, boolean_true_node, |
2d76680f | 5069 | boolean_false_node, |
92a596e8 | 5070 | lval, |
2d76680f PC |
5071 | non_constant_p, overflow_p); |
5072 | break; | |
5073 | ||
5074 | case ARRAY_REF: | |
92a596e8 | 5075 | r = cxx_eval_array_reference (ctx, t, lval, |
2d76680f PC |
5076 | non_constant_p, overflow_p); |
5077 | break; | |
5078 | ||
5079 | case COMPONENT_REF: | |
5080 | if (is_overloaded_fn (t)) | |
5081 | { | |
5082 | /* We can only get here in checking mode via | |
5083 | build_non_dependent_expr, because any expression that | |
5084 | calls or takes the address of the function will have | |
5085 | pulled a FUNCTION_DECL out of the COMPONENT_REF. */ | |
2b3ab879 | 5086 | gcc_checking_assert (ctx->quiet || errorcount); |
2d76680f PC |
5087 | *non_constant_p = true; |
5088 | return t; | |
5089 | } | |
92a596e8 | 5090 | r = cxx_eval_component_reference (ctx, t, lval, |
2d76680f PC |
5091 | non_constant_p, overflow_p); |
5092 | break; | |
5093 | ||
5094 | case BIT_FIELD_REF: | |
92a596e8 | 5095 | r = cxx_eval_bit_field_ref (ctx, t, lval, |
2d76680f PC |
5096 | non_constant_p, overflow_p); |
5097 | break; | |
5098 | ||
5099 | case COND_EXPR: | |
43574e4f | 5100 | case IF_STMT: |
4b390698 JJ |
5101 | if (jump_target && *jump_target) |
5102 | { | |
e9fa2f6d | 5103 | tree orig_jump = *jump_target; |
43574e4f JJ |
5104 | tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1)) |
5105 | ? TREE_OPERAND (t, 1) : void_node); | |
4b390698 JJ |
5106 | /* When jumping to a label, the label might be either in the |
5107 | then or else blocks, so process then block first in skipping | |
5108 | mode first, and if we are still in the skipping mode at its end, | |
5109 | process the else block too. */ | |
43574e4f JJ |
5110 | r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, |
5111 | overflow_p, jump_target); | |
e9fa2f6d MP |
5112 | /* It's possible that we found the label in the then block. But |
5113 | it could have been followed by another jumping statement, e.g. | |
5114 | say we're looking for case 1: | |
5115 | if (cond) | |
5116 | { | |
5117 | // skipped statements | |
5118 | case 1:; // clears up *jump_target | |
5119 | return 1; // and sets it to a RETURN_EXPR | |
5120 | } | |
5121 | else { ... } | |
5122 | in which case we need not go looking to the else block. | |
5123 | (goto is not allowed in a constexpr function.) */ | |
5124 | if (*jump_target == orig_jump) | |
43574e4f JJ |
5125 | { |
5126 | arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2)) | |
5127 | ? TREE_OPERAND (t, 2) : void_node); | |
5128 | r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, | |
5129 | overflow_p, jump_target); | |
5130 | } | |
4b390698 JJ |
5131 | break; |
5132 | } | |
92a596e8 | 5133 | r = cxx_eval_conditional_expression (ctx, t, lval, |
56632b27 JM |
5134 | non_constant_p, overflow_p, |
5135 | jump_target); | |
2d76680f | 5136 | break; |
f370e36d JJ |
5137 | case VEC_COND_EXPR: |
5138 | r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p, | |
5139 | overflow_p); | |
5140 | break; | |
2d76680f PC |
5141 | |
5142 | case CONSTRUCTOR: | |
35d87f01 | 5143 | if (TREE_CONSTANT (t) && reduced_constant_expression_p (t)) |
2d63bc39 JM |
5144 | { |
5145 | /* Don't re-process a constant CONSTRUCTOR, but do fold it to | |
5146 | VECTOR_CST if applicable. */ | |
ac9ec198 | 5147 | verify_constructor_flags (t); |
2d63bc39 JM |
5148 | if (TREE_CONSTANT (t)) |
5149 | return fold (t); | |
5150 | } | |
92a596e8 | 5151 | r = cxx_eval_bare_aggregate (ctx, t, lval, |
2d76680f PC |
5152 | non_constant_p, overflow_p); |
5153 | break; | |
5154 | ||
5155 | case VEC_INIT_EXPR: | |
5156 | /* We can get this in a defaulted constructor for a class with a | |
5157 | non-static data member of array type. Either the initializer will | |
5158 | be NULL, meaning default-initialization, or it will be an lvalue | |
5159 | or xvalue of the same type, meaning direct-initialization from the | |
5160 | corresponding member. */ | |
92a596e8 | 5161 | r = cxx_eval_vec_init (ctx, t, lval, |
2d76680f PC |
5162 | non_constant_p, overflow_p); |
5163 | break; | |
5164 | ||
2d76680f | 5165 | case VEC_PERM_EXPR: |
92a596e8 | 5166 | r = cxx_eval_trinary_expression (ctx, t, lval, |
2d76680f PC |
5167 | non_constant_p, overflow_p); |
5168 | break; | |
5169 | ||
7d75ea04 JJ |
5170 | case NOP_EXPR: |
5171 | if (REINTERPRET_CAST_P (t)) | |
5172 | { | |
5173 | if (!ctx->quiet) | |
f9d0ca40 | 5174 | error_at (cp_expr_loc_or_input_loc (t), |
a9c697b8 | 5175 | "%<reinterpret_cast%> is not a constant expression"); |
7d75ea04 JJ |
5176 | *non_constant_p = true; |
5177 | return t; | |
5178 | } | |
5179 | /* FALLTHROUGH. */ | |
2d76680f PC |
5180 | case CONVERT_EXPR: |
5181 | case VIEW_CONVERT_EXPR: | |
cda0a029 | 5182 | case UNARY_PLUS_EXPR: |
2d76680f PC |
5183 | { |
5184 | tree oldop = TREE_OPERAND (t, 0); | |
cda0a029 | 5185 | |
3e605b20 | 5186 | tree op = cxx_eval_constant_expression (ctx, oldop, |
92a596e8 | 5187 | lval, |
5a804683 | 5188 | non_constant_p, overflow_p); |
2d76680f PC |
5189 | if (*non_constant_p) |
5190 | return t; | |
dcdbc004 | 5191 | tree type = TREE_TYPE (t); |
02a8575c JM |
5192 | |
5193 | if (VOID_TYPE_P (type)) | |
5194 | return void_node; | |
5195 | ||
dcdbc004 JM |
5196 | if (TREE_CODE (op) == PTRMEM_CST |
5197 | && !TYPE_PTRMEM_P (type)) | |
5198 | op = cplus_expand_constant (op); | |
7d75ea04 | 5199 | |
05bf54c3 MP |
5200 | if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR) |
5201 | { | |
7d75ea04 JJ |
5202 | if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op)) |
5203 | && !can_convert_qual (type, op)) | |
5204 | op = cplus_expand_constant (op); | |
5205 | return cp_fold_convert (type, op); | |
05bf54c3 | 5206 | } |
8bada5cd | 5207 | |
71a93b08 | 5208 | if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST) |
2d76680f | 5209 | { |
8bada5cd MS |
5210 | if (integer_zerop (op)) |
5211 | { | |
9f613f06 | 5212 | if (TYPE_REF_P (type)) |
8bada5cd MS |
5213 | { |
5214 | if (!ctx->quiet) | |
f9d0ca40 | 5215 | error_at (cp_expr_loc_or_input_loc (t), |
8bada5cd MS |
5216 | "dereferencing a null pointer"); |
5217 | *non_constant_p = true; | |
5218 | return t; | |
5219 | } | |
9f613f06 | 5220 | else if (TYPE_PTR_P (TREE_TYPE (op))) |
8bada5cd MS |
5221 | { |
5222 | tree from = TREE_TYPE (op); | |
5223 | ||
5224 | if (!can_convert (type, from, tf_none)) | |
5225 | { | |
5226 | if (!ctx->quiet) | |
f9d0ca40 | 5227 | error_at (cp_expr_loc_or_input_loc (t), |
8bada5cd MS |
5228 | "conversion of %qT null pointer to %qT " |
5229 | "is not a constant expression", | |
5230 | from, type); | |
5231 | *non_constant_p = true; | |
5232 | return t; | |
5233 | } | |
5234 | } | |
5235 | } | |
5236 | else | |
5237 | { | |
5238 | /* This detects for example: | |
5239 | reinterpret_cast<void*>(sizeof 0) | |
5240 | */ | |
5241 | if (!ctx->quiet) | |
f9d0ca40 | 5242 | error_at (cp_expr_loc_or_input_loc (t), |
8bada5cd | 5243 | "%<reinterpret_cast<%T>(%E)%> is not " |
64d6d399 | 5244 | "a constant expression", |
8bada5cd MS |
5245 | type, op); |
5246 | *non_constant_p = true; | |
5247 | return t; | |
5248 | } | |
2d76680f | 5249 | } |
7d75ea04 | 5250 | |
cda0a029 | 5251 | if (op == oldop && tcode != UNARY_PLUS_EXPR) |
2d76680f PC |
5252 | /* We didn't fold at the top so we could check for ptr-int |
5253 | conversion. */ | |
5254 | return fold (t); | |
7d75ea04 | 5255 | |
02a8575c JM |
5256 | tree sop; |
5257 | ||
e4511ca2 JM |
5258 | /* Handle an array's bounds having been deduced after we built |
5259 | the wrapping expression. */ | |
5260 | if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op))) | |
5261 | r = op; | |
02a8575c JM |
5262 | else if (sop = tree_strip_nop_conversions (op), |
5263 | sop != op && (same_type_ignoring_tlq_and_bounds_p | |
5264 | (type, TREE_TYPE (sop)))) | |
5265 | r = sop; | |
e4511ca2 | 5266 | else if (tcode == UNARY_PLUS_EXPR) |
cda0a029 JM |
5267 | r = fold_convert (TREE_TYPE (t), op); |
5268 | else | |
5269 | r = fold_build1 (tcode, type, op); | |
7d75ea04 | 5270 | |
2d76680f PC |
5271 | /* Conversion of an out-of-range value has implementation-defined |
5272 | behavior; the language considers it different from arithmetic | |
5273 | overflow, which is undefined. */ | |
5274 | if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op)) | |
5275 | TREE_OVERFLOW (r) = false; | |
5276 | } | |
5277 | break; | |
5278 | ||
5279 | case EMPTY_CLASS_EXPR: | |
5280 | /* This is good enough for a function argument that might not get | |
5281 | used, and they can't do anything with it, so just return it. */ | |
5282 | return t; | |
5283 | ||
60813a46 | 5284 | case STATEMENT_LIST: |
56632b27 JM |
5285 | new_ctx = *ctx; |
5286 | new_ctx.ctor = new_ctx.object = NULL_TREE; | |
2b3ab879 | 5287 | return cxx_eval_statement_list (&new_ctx, t, |
56632b27 | 5288 | non_constant_p, overflow_p, jump_target); |
60813a46 JM |
5289 | |
5290 | case BIND_EXPR: | |
5291 | return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t), | |
92a596e8 | 5292 | lval, |
56632b27 JM |
5293 | non_constant_p, overflow_p, |
5294 | jump_target); | |
60813a46 | 5295 | |
2d76680f PC |
5296 | case PREINCREMENT_EXPR: |
5297 | case POSTINCREMENT_EXPR: | |
5298 | case PREDECREMENT_EXPR: | |
5299 | case POSTDECREMENT_EXPR: | |
2b3ab879 | 5300 | return cxx_eval_increment_expression (ctx, t, |
92a596e8 | 5301 | lval, non_constant_p, overflow_p); |
60813a46 JM |
5302 | |
5303 | case LAMBDA_EXPR: | |
2d76680f PC |
5304 | case NEW_EXPR: |
5305 | case VEC_NEW_EXPR: | |
5306 | case DELETE_EXPR: | |
5307 | case VEC_DELETE_EXPR: | |
5308 | case THROW_EXPR: | |
2d76680f PC |
5309 | case MODOP_EXPR: |
5310 | /* GCC internal stuff. */ | |
5311 | case VA_ARG_EXPR: | |
2d76680f PC |
5312 | case NON_DEPENDENT_EXPR: |
5313 | case BASELINK: | |
2d76680f | 5314 | case OFFSET_REF: |
2b3ab879 | 5315 | if (!ctx->quiet) |
f9d0ca40 | 5316 | error_at (cp_expr_loc_or_input_loc (t), |
64d6d399 | 5317 | "expression %qE is not a constant expression", t); |
2d76680f PC |
5318 | *non_constant_p = true; |
5319 | break; | |
5320 | ||
bf8d8309 MP |
5321 | case OBJ_TYPE_REF: |
5322 | { | |
5323 | /* Virtual function call. Let the constexpr machinery figure out | |
5324 | the dynamic type. */ | |
5325 | int token = tree_to_shwi (OBJ_TYPE_REF_TOKEN (t)); | |
5326 | tree obj = OBJ_TYPE_REF_OBJECT (t); | |
5327 | obj = cxx_eval_constant_expression (ctx, obj, lval, non_constant_p, | |
5328 | overflow_p); | |
5329 | /* We expect something in the form of &x.D.2103.D.2094; get x. */ | |
6a3ebcc6 JJ |
5330 | if (TREE_CODE (obj) != ADDR_EXPR |
5331 | || !DECL_P (get_base_address (TREE_OPERAND (obj, 0)))) | |
bf8d8309 MP |
5332 | { |
5333 | if (!ctx->quiet) | |
f9d0ca40 | 5334 | error_at (cp_expr_loc_or_input_loc (t), |
bf8d8309 MP |
5335 | "expression %qE is not a constant expression", t); |
5336 | *non_constant_p = true; | |
5337 | return t; | |
5338 | } | |
5339 | obj = TREE_OPERAND (obj, 0); | |
69deaf14 JJ |
5340 | while (TREE_CODE (obj) == COMPONENT_REF |
5341 | && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))) | |
bf8d8309 MP |
5342 | obj = TREE_OPERAND (obj, 0); |
5343 | tree objtype = TREE_TYPE (obj); | |
5344 | /* Find the function decl in the virtual functions list. TOKEN is | |
5345 | the DECL_VINDEX that says which function we're looking for. */ | |
5346 | tree virtuals = BINFO_VIRTUALS (TYPE_BINFO (objtype)); | |
582d2481 JJ |
5347 | if (TARGET_VTABLE_USES_DESCRIPTORS) |
5348 | token /= MAX (TARGET_VTABLE_USES_DESCRIPTORS, 1); | |
bf8d8309 MP |
5349 | r = TREE_VALUE (chain_index (token, virtuals)); |
5350 | break; | |
5351 | } | |
5352 | ||
3e605b20 | 5353 | case PLACEHOLDER_EXPR: |
89262ec6 JM |
5354 | /* Use of the value or address of the current object. */ |
5355 | if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t))) | |
5356 | return cxx_eval_constant_expression (ctx, ctor, lval, | |
5357 | non_constant_p, overflow_p); | |
5358 | /* A placeholder without a referent. We can get here when | |
5359 | checking whether NSDMIs are noexcept, or in massage_init_elt; | |
5360 | just say it's non-constant for now. */ | |
5361 | gcc_assert (ctx->quiet); | |
5362 | *non_constant_p = true; | |
3e605b20 JM |
5363 | break; |
5364 | ||
06ec22b7 JM |
5365 | case EXIT_EXPR: |
5366 | { | |
5367 | tree cond = TREE_OPERAND (t, 0); | |
5368 | cond = cxx_eval_constant_expression (ctx, cond, /*lval*/false, | |
5369 | non_constant_p, overflow_p); | |
5370 | VERIFY_CONSTANT (cond); | |
5371 | if (integer_nonzerop (cond)) | |
5372 | *jump_target = t; | |
5373 | } | |
5374 | break; | |
5375 | ||
60813a46 | 5376 | case GOTO_EXPR: |
56632b27 | 5377 | *jump_target = TREE_OPERAND (t, 0); |
d49318d9 PC |
5378 | gcc_assert (breaks (jump_target) || continues (jump_target) |
5379 | /* Allow for jumping to a cdtor_label. */ | |
5380 | || returns (jump_target)); | |
56632b27 JM |
5381 | break; |
5382 | ||
60813a46 | 5383 | case LOOP_EXPR: |
43574e4f JJ |
5384 | case DO_STMT: |
5385 | case WHILE_STMT: | |
5386 | case FOR_STMT: | |
2b3ab879 | 5387 | cxx_eval_loop_expr (ctx, t, |
56632b27 JM |
5388 | non_constant_p, overflow_p, jump_target); |
5389 | break; | |
5390 | ||
60813a46 | 5391 | case SWITCH_EXPR: |
43574e4f | 5392 | case SWITCH_STMT: |
2b3ab879 | 5393 | cxx_eval_switch_expr (ctx, t, |
56632b27 | 5394 | non_constant_p, overflow_p, jump_target); |
60813a46 JM |
5395 | break; |
5396 | ||
971e17ff AS |
5397 | case REQUIRES_EXPR: |
5398 | /* It's possible to get a requires-expression in a constant | |
5399 | expression. For example: | |
5400 | ||
5401 | template<typename T> concept bool C() { | |
5402 | return requires (T t) { t; }; | |
5403 | } | |
5404 | ||
5405 | template<typename T> requires !C<T>() void f(T); | |
5406 | ||
5407 | Normalization leaves f with the associated constraint | |
5408 | '!requires (T t) { ... }' which is not transformed into | |
5409 | a constraint. */ | |
5410 | if (!processing_template_decl) | |
5411 | return evaluate_constraint_expression (t, NULL_TREE); | |
5412 | else | |
5413 | *non_constant_p = true; | |
5414 | return t; | |
5415 | ||
98e09245 | 5416 | case ANNOTATE_EXPR: |
98e09245 JJ |
5417 | r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
5418 | lval, | |
5419 | non_constant_p, overflow_p, | |
5420 | jump_target); | |
5421 | break; | |
5422 | ||
3664e317 JM |
5423 | case USING_STMT: |
5424 | r = void_node; | |
5425 | break; | |
5426 | ||
529bc410 MP |
5427 | case ASM_EXPR: |
5428 | if (!ctx->quiet) | |
7c814975 | 5429 | inline_asm_in_constexpr_error (cp_expr_loc_or_input_loc (t)); |
529bc410 MP |
5430 | *non_constant_p = true; |
5431 | return t; | |
5432 | ||
2d76680f | 5433 | default: |
c6e7c499 JM |
5434 | if (STATEMENT_CODE_P (TREE_CODE (t))) |
5435 | { | |
5436 | /* This function doesn't know how to deal with pre-genericize | |
5437 | statements; this can only happen with statement-expressions, | |
5438 | so for now just fail. */ | |
5439 | if (!ctx->quiet) | |
5440 | error_at (EXPR_LOCATION (t), | |
64d6d399 | 5441 | "statement is not a constant expression"); |
c6e7c499 JM |
5442 | } |
5443 | else | |
5444 | internal_error ("unexpected expression %qE of kind %s", t, | |
5445 | get_tree_code_name (TREE_CODE (t))); | |
2d76680f PC |
5446 | *non_constant_p = true; |
5447 | break; | |
5448 | } | |
5449 | ||
5450 | if (r == error_mark_node) | |
5451 | *non_constant_p = true; | |
5452 | ||
5453 | if (*non_constant_p) | |
5454 | return t; | |
5455 | else | |
5456 | return r; | |
5457 | } | |
5458 | ||
e079dced JM |
5459 | /* P0859: A function is needed for constant evaluation if it is a constexpr |
5460 | function that is named by an expression ([basic.def.odr]) that is | |
5461 | potentially constant evaluated. | |
5462 | ||
5463 | So we need to instantiate any constexpr functions mentioned by the | |
5464 | expression even if the definition isn't needed for evaluating the | |
5465 | expression. */ | |
5466 | ||
5467 | static tree | |
5468 | instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/) | |
5469 | { | |
5470 | if (TREE_CODE (*tp) == FUNCTION_DECL | |
5471 | && DECL_DECLARED_CONSTEXPR_P (*tp) | |
5472 | && !DECL_INITIAL (*tp) | |
4fdda3ce | 5473 | && !trivial_fn_p (*tp) |
e079dced JM |
5474 | && DECL_TEMPLOID_INSTANTIATION (*tp)) |
5475 | { | |
5476 | ++function_depth; | |
5477 | instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false); | |
5478 | --function_depth; | |
5479 | } | |
5480 | else if (TREE_CODE (*tp) == CALL_EXPR | |
5481 | || TREE_CODE (*tp) == AGGR_INIT_EXPR) | |
5482 | { | |
5483 | if (EXPR_HAS_LOCATION (*tp)) | |
5484 | input_location = EXPR_LOCATION (*tp); | |
5485 | } | |
5486 | ||
5487 | if (!EXPR_P (*tp)) | |
5488 | *walk_subtrees = 0; | |
5489 | ||
5490 | return NULL_TREE; | |
5491 | } | |
5492 | static void | |
5493 | instantiate_constexpr_fns (tree t) | |
5494 | { | |
5495 | location_t loc = input_location; | |
5496 | cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL); | |
5497 | input_location = loc; | |
5498 | } | |
5499 | ||
e4082611 | 5500 | /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression. |
d4accef3 JM |
5501 | STRICT has the same sense as for constant_value_1: true if we only allow |
5502 | conforming C++ constant expressions, or false if we want a constant value | |
5503 | even if it doesn't conform. | |
13de99bc | 5504 | MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as |
e4082611 JJ |
5505 | per P0595 even when ALLOW_NON_CONSTANT is true. */ |
5506 | ||
2d76680f | 5507 | static tree |
3e605b20 | 5508 | cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant, |
e4082611 | 5509 | bool strict = true, |
13de99bc | 5510 | bool manifestly_const_eval = false, |
e4082611 | 5511 | tree object = NULL_TREE) |
2d76680f | 5512 | { |
8108ea30 JM |
5513 | auto_timevar time (TV_CONSTEXPR); |
5514 | ||
2d76680f PC |
5515 | bool non_constant_p = false; |
5516 | bool overflow_p = false; | |
3e605b20 | 5517 | hash_map<tree,tree> map; |
a15ffa22 | 5518 | HOST_WIDE_INT constexpr_ctx_count = 0; |
39dce2b7 | 5519 | |
4b390698 | 5520 | constexpr_ctx ctx = { NULL, &map, NULL, NULL, NULL, NULL, |
a15ffa22 | 5521 | &constexpr_ctx_count, allow_non_constant, strict, |
13de99bc | 5522 | manifestly_const_eval || !allow_non_constant }; |
39dce2b7 | 5523 | |
3e605b20 | 5524 | tree type = initialized_type (t); |
3e605b20 | 5525 | tree r = t; |
4d0c18c6 JM |
5526 | if (VOID_TYPE_P (type)) |
5527 | return t; | |
3e605b20 JM |
5528 | if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)) |
5529 | { | |
5530 | /* In C++14 an NSDMI can participate in aggregate initialization, | |
5531 | and can refer to the address of the object being initialized, so | |
5532 | we need to pass in the relevant VAR_DECL if we want to do the | |
5533 | evaluation in a single pass. The evaluation will dynamically | |
5534 | update ctx.values for the VAR_DECL. We use the same strategy | |
5535 | for C++11 constexpr constructors that refer to the object being | |
5536 | initialized. */ | |
5537 | ctx.ctor = build_constructor (type, NULL); | |
e8c48716 | 5538 | CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true; |
60813a46 JM |
5539 | if (!object) |
5540 | { | |
5541 | if (TREE_CODE (t) == TARGET_EXPR) | |
5542 | object = TARGET_EXPR_SLOT (t); | |
5543 | else if (TREE_CODE (t) == AGGR_INIT_EXPR) | |
5544 | object = AGGR_INIT_EXPR_SLOT (t); | |
5545 | } | |
3e605b20 JM |
5546 | ctx.object = object; |
5547 | if (object) | |
5548 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
5549 | (type, TREE_TYPE (object))); | |
5550 | if (object && DECL_P (object)) | |
5551 | map.put (object, ctx.ctor); | |
5552 | if (TREE_CODE (r) == TARGET_EXPR) | |
5553 | /* Avoid creating another CONSTRUCTOR when we expand the | |
5554 | TARGET_EXPR. */ | |
5555 | r = TARGET_EXPR_INITIAL (r); | |
5556 | } | |
5557 | ||
e079dced | 5558 | instantiate_constexpr_fns (r); |
2b3ab879 | 5559 | r = cxx_eval_constant_expression (&ctx, r, |
5a804683 | 5560 | false, &non_constant_p, &overflow_p); |
2d76680f PC |
5561 | |
5562 | verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p); | |
5563 | ||
023d89c7 JM |
5564 | /* Mutable logic is a bit tricky: we want to allow initialization of |
5565 | constexpr variables with mutable members, but we can't copy those | |
5566 | members to another constexpr variable. */ | |
5567 | if (TREE_CODE (r) == CONSTRUCTOR | |
5568 | && CONSTRUCTOR_MUTABLE_POISON (r)) | |
2d76680f | 5569 | { |
2d76680f | 5570 | if (!allow_non_constant) |
023d89c7 JM |
5571 | error ("%qE is not a constant expression because it refers to " |
5572 | "mutable subobjects of %qT", t, type); | |
2d76680f PC |
5573 | non_constant_p = true; |
5574 | } | |
5575 | ||
f64e0c02 | 5576 | if (TREE_CODE (r) == CONSTRUCTOR |
e8c48716 | 5577 | && CONSTRUCTOR_NO_CLEARING (r)) |
f64e0c02 JM |
5578 | { |
5579 | if (!allow_non_constant) | |
5580 | error ("%qE is not a constant expression because it refers to " | |
5581 | "an incompletely initialized variable", t); | |
5582 | TREE_CONSTANT (r) = false; | |
5583 | non_constant_p = true; | |
5584 | } | |
5585 | ||
2d76680f PC |
5586 | /* Technically we should check this for all subexpressions, but that |
5587 | runs into problems with our internal representation of pointer | |
5588 | subtraction and the 5.19 rules are still in flux. */ | |
5589 | if (CONVERT_EXPR_CODE_P (TREE_CODE (r)) | |
5590 | && ARITHMETIC_TYPE_P (TREE_TYPE (r)) | |
5591 | && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR) | |
5592 | { | |
5593 | if (!allow_non_constant) | |
5594 | error ("conversion from pointer type %qT " | |
64d6d399 | 5595 | "to arithmetic type %qT in a constant expression", |
2d76680f PC |
5596 | TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r)); |
5597 | non_constant_p = true; | |
5598 | } | |
5599 | ||
5600 | if (!non_constant_p && overflow_p) | |
5601 | non_constant_p = true; | |
5602 | ||
56cfb596 PP |
5603 | /* Unshare the result unless it's a CONSTRUCTOR in which case it's already |
5604 | unshared. */ | |
5605 | bool should_unshare = true; | |
5606 | if (r == t || TREE_CODE (r) == CONSTRUCTOR) | |
5607 | should_unshare = false; | |
5608 | ||
2d76680f PC |
5609 | if (non_constant_p && !allow_non_constant) |
5610 | return error_mark_node; | |
5611 | else if (non_constant_p && TREE_CONSTANT (r)) | |
5612 | { | |
e4082611 JJ |
5613 | /* If __builtin_is_constant_evaluated () was evaluated to true |
5614 | and the result is not a valid constant expression, we need to | |
5615 | punt. */ | |
13de99bc | 5616 | if (manifestly_const_eval) |
e4082611 JJ |
5617 | return cxx_eval_outermost_constant_expr (t, true, strict, |
5618 | false, object); | |
eddd715c JM |
5619 | /* This isn't actually constant, so unset TREE_CONSTANT. |
5620 | Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires | |
5621 | it to be set if it is invariant address, even when it is not | |
5622 | a valid C++ constant expression. Wrap it with a NOP_EXPR | |
5623 | instead. */ | |
5624 | if (EXPR_P (r) && TREE_CODE (r) != ADDR_EXPR) | |
2d76680f PC |
5625 | r = copy_node (r); |
5626 | else if (TREE_CODE (r) == CONSTRUCTOR) | |
5627 | r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r); | |
5628 | else | |
5629 | r = build_nop (TREE_TYPE (r), r); | |
5630 | TREE_CONSTANT (r) = false; | |
5631 | } | |
5dab8b11 | 5632 | else if (non_constant_p) |
2d76680f PC |
5633 | return t; |
5634 | ||
56cfb596 PP |
5635 | if (should_unshare) |
5636 | r = unshare_expr (r); | |
5637 | ||
2d76680f PC |
5638 | if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r))) |
5639 | { | |
5dab8b11 | 5640 | r = adjust_temp_type (type, r); |
2d76680f PC |
5641 | if (TREE_CODE (t) == TARGET_EXPR |
5642 | && TARGET_EXPR_INITIAL (t) == r) | |
5643 | return t; | |
5dab8b11 | 5644 | else if (TREE_CODE (t) != CONSTRUCTOR) |
2d76680f PC |
5645 | { |
5646 | r = get_target_expr (r); | |
5647 | TREE_CONSTANT (r) = true; | |
2d76680f PC |
5648 | } |
5649 | } | |
5dab8b11 JM |
5650 | |
5651 | return r; | |
2d76680f PC |
5652 | } |
5653 | ||
2d76680f PC |
5654 | /* If T represents a constant expression returns its reduced value. |
5655 | Otherwise return error_mark_node. If T is dependent, then | |
5656 | return NULL. */ | |
5657 | ||
5658 | tree | |
3e605b20 | 5659 | cxx_constant_value (tree t, tree decl) |
2d76680f | 5660 | { |
e4082611 | 5661 | return cxx_eval_outermost_constant_expr (t, false, true, true, decl); |
2d76680f PC |
5662 | } |
5663 | ||
cda0a029 JM |
5664 | /* Helper routine for fold_simple function. Either return simplified |
5665 | expression T, otherwise NULL_TREE. | |
5666 | In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold | |
5667 | even if we are within template-declaration. So be careful on call, as in | |
5668 | such case types can be undefined. */ | |
5669 | ||
5670 | static tree | |
5671 | fold_simple_1 (tree t) | |
5672 | { | |
5673 | tree op1; | |
5674 | enum tree_code code = TREE_CODE (t); | |
5675 | ||
5676 | switch (code) | |
5677 | { | |
5678 | case INTEGER_CST: | |
5679 | case REAL_CST: | |
5680 | case VECTOR_CST: | |
5681 | case FIXED_CST: | |
5682 | case COMPLEX_CST: | |
5683 | return t; | |
5684 | ||
5685 | case SIZEOF_EXPR: | |
5686 | return fold_sizeof_expr (t); | |
5687 | ||
5688 | case ABS_EXPR: | |
e28fadbc | 5689 | case ABSU_EXPR: |
cda0a029 JM |
5690 | case CONJ_EXPR: |
5691 | case REALPART_EXPR: | |
5692 | case IMAGPART_EXPR: | |
5693 | case NEGATE_EXPR: | |
5694 | case BIT_NOT_EXPR: | |
5695 | case TRUTH_NOT_EXPR: | |
5696 | case NOP_EXPR: | |
5697 | case VIEW_CONVERT_EXPR: | |
5698 | case CONVERT_EXPR: | |
5699 | case FLOAT_EXPR: | |
5700 | case FIX_TRUNC_EXPR: | |
5701 | case FIXED_CONVERT_EXPR: | |
5702 | case ADDR_SPACE_CONVERT_EXPR: | |
5703 | ||
5704 | op1 = TREE_OPERAND (t, 0); | |
5705 | ||
5706 | t = const_unop (code, TREE_TYPE (t), op1); | |
5707 | if (!t) | |
5708 | return NULL_TREE; | |
5709 | ||
5710 | if (CONVERT_EXPR_CODE_P (code) | |
5711 | && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1)) | |
5712 | TREE_OVERFLOW (t) = false; | |
5713 | return t; | |
5714 | ||
5715 | default: | |
5716 | return NULL_TREE; | |
5717 | } | |
5718 | } | |
5719 | ||
5720 | /* If T is a simple constant expression, returns its simplified value. | |
dba99244 | 5721 | Otherwise returns T. In contrast to maybe_constant_value we |
cda0a029 JM |
5722 | simplify only few operations on constant-expressions, and we don't |
5723 | try to simplify constexpressions. */ | |
5724 | ||
5725 | tree | |
5726 | fold_simple (tree t) | |
5727 | { | |
cda0a029 JM |
5728 | if (processing_template_decl) |
5729 | return t; | |
5730 | ||
dba99244 MP |
5731 | tree r = fold_simple_1 (t); |
5732 | if (r) | |
5733 | return r; | |
cda0a029 | 5734 | |
dba99244 | 5735 | return t; |
cda0a029 JM |
5736 | } |
5737 | ||
2d76680f PC |
5738 | /* If T is a constant expression, returns its reduced value. |
5739 | Otherwise, if T does not have TREE_CONSTANT set, returns T. | |
13de99bc JJ |
5740 | Otherwise, returns a version of T without TREE_CONSTANT. |
5741 | MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated | |
5742 | as per P0595. */ | |
2d76680f | 5743 | |
8a87daca JM |
5744 | static GTY((deletable)) hash_map<tree, tree> *cv_cache; |
5745 | ||
5746 | tree | |
13de99bc | 5747 | maybe_constant_value (tree t, tree decl, bool manifestly_const_eval) |
2d76680f PC |
5748 | { |
5749 | tree r; | |
5750 | ||
a0ab7ccd | 5751 | if (!is_nondependent_constant_expression (t)) |
2d76680f PC |
5752 | { |
5753 | if (TREE_OVERFLOW_P (t)) | |
5754 | { | |
5755 | t = build_nop (TREE_TYPE (t), t); | |
5756 | TREE_CONSTANT (t) = false; | |
5757 | } | |
5758 | return t; | |
5759 | } | |
8a87daca JM |
5760 | else if (CONSTANT_CLASS_P (t)) |
5761 | /* No caching or evaluation needed. */ | |
5762 | return t; | |
5763 | ||
13de99bc JJ |
5764 | if (manifestly_const_eval) |
5765 | return cxx_eval_outermost_constant_expr (t, true, true, true, decl); | |
5766 | ||
8a87daca JM |
5767 | if (cv_cache == NULL) |
5768 | cv_cache = hash_map<tree, tree>::create_ggc (101); | |
5769 | if (tree *cached = cv_cache->get (t)) | |
5770 | return *cached; | |
2d76680f | 5771 | |
e4082611 | 5772 | r = cxx_eval_outermost_constant_expr (t, true, true, false, decl); |
595278be MM |
5773 | gcc_checking_assert (r == t |
5774 | || CONVERT_EXPR_P (t) | |
5775 | || TREE_CODE (t) == VIEW_CONVERT_EXPR | |
5776 | || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) | |
5777 | || !cp_tree_equal (r, t)); | |
8a87daca | 5778 | cv_cache->put (t, r); |
2d76680f PC |
5779 | return r; |
5780 | } | |
5781 | ||
7a7ac32a PP |
5782 | /* Dispose of the whole CV_CACHE. */ |
5783 | ||
5784 | static void | |
5785 | clear_cv_cache (void) | |
5786 | { | |
5787 | if (cv_cache != NULL) | |
5788 | cv_cache->empty (); | |
5789 | } | |
5790 | ||
eba9e839 | 5791 | /* Dispose of the whole CV_CACHE and FOLD_CACHE. */ |
1e297006 MP |
5792 | |
5793 | void | |
eba9e839 | 5794 | clear_cv_and_fold_caches (void) |
1e297006 | 5795 | { |
7a7ac32a | 5796 | clear_cv_cache (); |
eba9e839 | 5797 | clear_fold_cache (); |
1e297006 MP |
5798 | } |
5799 | ||
a81c8e8c MP |
5800 | /* Internal function handling expressions in templates for |
5801 | fold_non_dependent_expr and fold_non_dependent_init. | |
5802 | ||
5803 | If we're in a template, but T isn't value dependent, simplify | |
5804 | it. We're supposed to treat: | |
5805 | ||
5806 | template <typename T> void f(T[1 + 1]); | |
5807 | template <typename T> void f(T[2]); | |
5808 | ||
5809 | as two declarations of the same function, for example. */ | |
5810 | ||
5811 | static tree | |
5812 | fold_non_dependent_expr_template (tree t, tsubst_flags_t complain, | |
5813 | bool manifestly_const_eval) | |
5814 | { | |
5815 | gcc_assert (processing_template_decl); | |
5816 | ||
5817 | if (is_nondependent_constant_expression (t)) | |
5818 | { | |
5819 | processing_template_decl_sentinel s; | |
5820 | t = instantiate_non_dependent_expr_internal (t, complain); | |
5821 | ||
5822 | if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t)) | |
5823 | { | |
5824 | if (TREE_OVERFLOW_P (t)) | |
5825 | { | |
5826 | t = build_nop (TREE_TYPE (t), t); | |
5827 | TREE_CONSTANT (t) = false; | |
5828 | } | |
5829 | return t; | |
5830 | } | |
5831 | ||
5832 | tree r = cxx_eval_outermost_constant_expr (t, true, true, | |
5833 | manifestly_const_eval, | |
5834 | NULL_TREE); | |
5835 | /* cp_tree_equal looks through NOPs, so allow them. */ | |
5836 | gcc_checking_assert (r == t | |
5837 | || CONVERT_EXPR_P (t) | |
5838 | || TREE_CODE (t) == VIEW_CONVERT_EXPR | |
5839 | || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) | |
5840 | || !cp_tree_equal (r, t)); | |
5841 | return r; | |
5842 | } | |
5843 | else if (TREE_OVERFLOW_P (t)) | |
5844 | { | |
5845 | t = build_nop (TREE_TYPE (t), t); | |
5846 | TREE_CONSTANT (t) = false; | |
5847 | } | |
5848 | ||
5849 | return t; | |
5850 | } | |
5851 | ||
234bef96 PC |
5852 | /* Like maybe_constant_value but first fully instantiate the argument. |
5853 | ||
5854 | Note: this is equivalent to instantiate_non_dependent_expr_sfinae | |
e56f6629 JM |
5855 | (t, complain) followed by maybe_constant_value but is more efficient, |
5856 | because it calls instantiation_dependent_expression_p and | |
5857 | potential_constant_expression at most once. | |
4cd3e7df | 5858 | The manifestly_const_eval argument is passed to maybe_constant_value. |
e56f6629 JM |
5859 | |
5860 | Callers should generally pass their active complain, or if they are in a | |
5861 | non-template, diagnosing context, they can use the default of | |
5862 | tf_warning_or_error. Callers that might be within a template context, don't | |
5863 | have a complain parameter, and aren't going to remember the result for long | |
5864 | (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node | |
5865 | appropriately. */ | |
234bef96 PC |
5866 | |
5867 | tree | |
e56f6629 | 5868 | fold_non_dependent_expr (tree t, |
4cd3e7df JJ |
5869 | tsubst_flags_t complain /* = tf_warning_or_error */, |
5870 | bool manifestly_const_eval /* = false */) | |
234bef96 PC |
5871 | { |
5872 | if (t == NULL_TREE) | |
5873 | return NULL_TREE; | |
5874 | ||
a81c8e8c MP |
5875 | if (processing_template_decl) |
5876 | return fold_non_dependent_expr_template (t, complain, | |
5877 | manifestly_const_eval); | |
234bef96 | 5878 | |
a81c8e8c MP |
5879 | return maybe_constant_value (t, NULL_TREE, manifestly_const_eval); |
5880 | } | |
234bef96 | 5881 | |
234bef96 | 5882 | |
a81c8e8c | 5883 | /* Like maybe_constant_init but first fully instantiate the argument. */ |
234bef96 | 5884 | |
a81c8e8c MP |
5885 | tree |
5886 | fold_non_dependent_init (tree t, | |
5887 | tsubst_flags_t complain /*=tf_warning_or_error*/, | |
5888 | bool manifestly_const_eval /*=false*/) | |
5889 | { | |
5890 | if (t == NULL_TREE) | |
5891 | return NULL_TREE; | |
5892 | ||
5893 | if (processing_template_decl) | |
5894 | { | |
5895 | t = fold_non_dependent_expr_template (t, complain, | |
5896 | manifestly_const_eval); | |
5897 | /* maybe_constant_init does this stripping, so do it here too. */ | |
5898 | if (TREE_CODE (t) == TARGET_EXPR) | |
234bef96 | 5899 | { |
a81c8e8c MP |
5900 | tree init = TARGET_EXPR_INITIAL (t); |
5901 | if (TREE_CODE (init) == CONSTRUCTOR) | |
5902 | t = init; | |
234bef96 PC |
5903 | } |
5904 | return t; | |
5905 | } | |
5906 | ||
a81c8e8c | 5907 | return maybe_constant_init (t, NULL_TREE, manifestly_const_eval); |
234bef96 PC |
5908 | } |
5909 | ||
2d76680f | 5910 | /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather |
e4082611 JJ |
5911 | than wrapped in a TARGET_EXPR. |
5912 | ALLOW_NON_CONSTANT is false if T is required to be a constant expression. | |
13de99bc | 5913 | MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as |
e4082611 | 5914 | per P0595 even when ALLOW_NON_CONSTANT is true. */ |
2d76680f | 5915 | |
118cd6ba | 5916 | static tree |
e4082611 | 5917 | maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant, |
13de99bc | 5918 | bool manifestly_const_eval) |
2d76680f | 5919 | { |
cda0a029 JM |
5920 | if (!t) |
5921 | return t; | |
2d76680f PC |
5922 | if (TREE_CODE (t) == EXPR_STMT) |
5923 | t = TREE_OPERAND (t, 0); | |
5924 | if (TREE_CODE (t) == CONVERT_EXPR | |
5925 | && VOID_TYPE_P (TREE_TYPE (t))) | |
5926 | t = TREE_OPERAND (t, 0); | |
60813a46 JM |
5927 | if (TREE_CODE (t) == INIT_EXPR) |
5928 | t = TREE_OPERAND (t, 1); | |
f64e0c02 JM |
5929 | if (TREE_CODE (t) == TARGET_EXPR) |
5930 | t = TARGET_EXPR_INITIAL (t); | |
a0ab7ccd | 5931 | if (!is_nondependent_static_init_expression (t)) |
69eb4fde | 5932 | /* Don't try to evaluate it. */; |
118cd6ba | 5933 | else if (CONSTANT_CLASS_P (t) && allow_non_constant) |
8a87daca | 5934 | /* No evaluation needed. */; |
69eb4fde | 5935 | else |
e4082611 | 5936 | t = cxx_eval_outermost_constant_expr (t, allow_non_constant, |
38295573 | 5937 | /*strict*/false, |
13de99bc | 5938 | manifestly_const_eval, decl); |
2d76680f PC |
5939 | if (TREE_CODE (t) == TARGET_EXPR) |
5940 | { | |
5941 | tree init = TARGET_EXPR_INITIAL (t); | |
5942 | if (TREE_CODE (init) == CONSTRUCTOR) | |
5943 | t = init; | |
5944 | } | |
5945 | return t; | |
5946 | } | |
5947 | ||
118cd6ba MP |
5948 | /* Wrapper for maybe_constant_init_1 which permits non constants. */ |
5949 | ||
5950 | tree | |
13de99bc | 5951 | maybe_constant_init (tree t, tree decl, bool manifestly_const_eval) |
118cd6ba | 5952 | { |
13de99bc | 5953 | return maybe_constant_init_1 (t, decl, true, manifestly_const_eval); |
118cd6ba MP |
5954 | } |
5955 | ||
5956 | /* Wrapper for maybe_constant_init_1 which does not permit non constants. */ | |
5957 | ||
5958 | tree | |
5959 | cxx_constant_init (tree t, tree decl) | |
5960 | { | |
e4082611 | 5961 | return maybe_constant_init_1 (t, decl, false, true); |
118cd6ba MP |
5962 | } |
5963 | ||
2d76680f PC |
5964 | #if 0 |
5965 | /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */ | |
5966 | /* Return true if the object referred to by REF has automatic or thread | |
5967 | local storage. */ | |
5968 | ||
5969 | enum { ck_ok, ck_bad, ck_unknown }; | |
5970 | static int | |
5971 | check_automatic_or_tls (tree ref) | |
5972 | { | |
ef4bddc2 | 5973 | machine_mode mode; |
f37fac2b | 5974 | poly_int64 bitsize, bitpos; |
2d76680f PC |
5975 | tree offset; |
5976 | int volatilep = 0, unsignedp = 0; | |
5977 | tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset, | |
5978 | &mode, &unsignedp, &volatilep, false); | |
5979 | duration_kind dk; | |
5980 | ||
5981 | /* If there isn't a decl in the middle, we don't know the linkage here, | |
5982 | and this isn't a constant expression anyway. */ | |
5983 | if (!DECL_P (decl)) | |
5984 | return ck_unknown; | |
5985 | dk = decl_storage_duration (decl); | |
5986 | return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok; | |
5987 | } | |
5988 | #endif | |
5989 | ||
c7a53bdb JJ |
5990 | /* Data structure for passing data from potential_constant_expression_1 |
5991 | to check_for_return_continue via cp_walk_tree. */ | |
5992 | struct check_for_return_continue_data { | |
5993 | hash_set<tree> *pset; | |
5994 | tree continue_stmt; | |
5995 | }; | |
5996 | ||
5997 | /* Helper function for potential_constant_expression_1 SWITCH_STMT handling, | |
5998 | called through cp_walk_tree. Return the first RETURN_EXPR found, or note | |
5999 | the first CONTINUE_STMT if RETURN_EXPR is not found. */ | |
6000 | static tree | |
6001 | check_for_return_continue (tree *tp, int *walk_subtrees, void *data) | |
6002 | { | |
6003 | tree t = *tp, s; | |
6004 | check_for_return_continue_data *d = (check_for_return_continue_data *) data; | |
6005 | switch (TREE_CODE (t)) | |
6006 | { | |
6007 | case RETURN_EXPR: | |
6008 | return t; | |
6009 | ||
6010 | case CONTINUE_STMT: | |
6011 | if (d->continue_stmt == NULL_TREE) | |
6012 | d->continue_stmt = t; | |
6013 | break; | |
6014 | ||
6015 | #define RECUR(x) \ | |
6016 | if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \ | |
6017 | d->pset)) \ | |
6018 | return r | |
6019 | ||
6020 | /* For loops, walk subtrees manually, so that continue stmts found | |
6021 | inside of the bodies of the loops are ignored. */ | |
6022 | case DO_STMT: | |
6023 | *walk_subtrees = 0; | |
6024 | RECUR (DO_COND (t)); | |
6025 | s = d->continue_stmt; | |
6026 | RECUR (DO_BODY (t)); | |
6027 | d->continue_stmt = s; | |
6028 | break; | |
6029 | ||
6030 | case WHILE_STMT: | |
6031 | *walk_subtrees = 0; | |
6032 | RECUR (WHILE_COND (t)); | |
6033 | s = d->continue_stmt; | |
6034 | RECUR (WHILE_BODY (t)); | |
6035 | d->continue_stmt = s; | |
6036 | break; | |
6037 | ||
6038 | case FOR_STMT: | |
6039 | *walk_subtrees = 0; | |
6040 | RECUR (FOR_INIT_STMT (t)); | |
6041 | RECUR (FOR_COND (t)); | |
6042 | RECUR (FOR_EXPR (t)); | |
6043 | s = d->continue_stmt; | |
6044 | RECUR (FOR_BODY (t)); | |
6045 | d->continue_stmt = s; | |
6046 | break; | |
6047 | ||
6048 | case RANGE_FOR_STMT: | |
6049 | *walk_subtrees = 0; | |
6050 | RECUR (RANGE_FOR_EXPR (t)); | |
6051 | s = d->continue_stmt; | |
6052 | RECUR (RANGE_FOR_BODY (t)); | |
6053 | d->continue_stmt = s; | |
6054 | break; | |
6055 | #undef RECUR | |
6056 | ||
6057 | case STATEMENT_LIST: | |
6058 | case CONSTRUCTOR: | |
6059 | break; | |
6060 | ||
6061 | default: | |
6062 | if (!EXPR_P (t)) | |
6063 | *walk_subtrees = 0; | |
6064 | break; | |
6065 | } | |
6066 | ||
6067 | return NULL_TREE; | |
6068 | } | |
6069 | ||
2d76680f PC |
6070 | /* Return true if T denotes a potentially constant expression. Issue |
6071 | diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true, | |
a0ab7ccd JM |
6072 | an lvalue-rvalue conversion is implied. If NOW is true, we want to |
6073 | consider the expression in the current context, independent of constexpr | |
6074 | substitution. | |
2d76680f PC |
6075 | |
6076 | C++0x [expr.const] used to say | |
6077 | ||
6078 | 6 An expression is a potential constant expression if it is | |
6079 | a constant expression where all occurrences of function | |
6080 | parameters are replaced by arbitrary constant expressions | |
6081 | of the appropriate type. | |
6082 | ||
6083 | 2 A conditional expression is a constant expression unless it | |
6084 | involves one of the following as a potentially evaluated | |
6085 | subexpression (3.2), but subexpressions of logical AND (5.14), | |
6086 | logical OR (5.15), and conditional (5.16) operations that are | |
6087 | not evaluated are not considered. */ | |
6088 | ||
6089 | static bool | |
a0ab7ccd | 6090 | potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, |
3075affd | 6091 | tsubst_flags_t flags, tree *jump_target) |
2d76680f | 6092 | { |
a0ab7ccd | 6093 | #define RECUR(T,RV) \ |
3075affd | 6094 | potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target) |
a0ab7ccd | 6095 | |
2d76680f PC |
6096 | enum { any = false, rval = true }; |
6097 | int i; | |
6098 | tree tmp; | |
6099 | ||
6100 | if (t == error_mark_node) | |
6101 | return false; | |
6102 | if (t == NULL_TREE) | |
6103 | return true; | |
f9d0ca40 | 6104 | location_t loc = cp_expr_loc_or_input_loc (t); |
3075affd JM |
6105 | |
6106 | if (*jump_target) | |
6107 | /* If we are jumping, ignore everything. This is simpler than the | |
6108 | cxx_eval_constant_expression handling because we only need to be | |
6109 | conservatively correct, and we don't necessarily have a constant value | |
6110 | available, so we don't bother with switch tracking. */ | |
6111 | return true; | |
6112 | ||
3c393a2c | 6113 | if (TREE_THIS_VOLATILE (t) && want_rval) |
2d76680f PC |
6114 | { |
6115 | if (flags & tf_error) | |
3c393a2c MP |
6116 | error_at (loc, "lvalue-to-rvalue conversion of a volatile lvalue " |
6117 | "%qE with type %qT", t, TREE_TYPE (t)); | |
2d76680f PC |
6118 | return false; |
6119 | } | |
6120 | if (CONSTANT_CLASS_P (t)) | |
6121 | return true; | |
7dc2b4a2 JM |
6122 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED) |
6123 | && TREE_TYPE (t) == error_mark_node) | |
6124 | return false; | |
2d76680f PC |
6125 | |
6126 | switch (TREE_CODE (t)) | |
6127 | { | |
6128 | case FUNCTION_DECL: | |
6129 | case BASELINK: | |
6130 | case TEMPLATE_DECL: | |
6131 | case OVERLOAD: | |
6132 | case TEMPLATE_ID_EXPR: | |
6133 | case LABEL_DECL: | |
6134 | case LABEL_EXPR: | |
60813a46 | 6135 | case CASE_LABEL_EXPR: |
2d76680f PC |
6136 | case CONST_DECL: |
6137 | case SIZEOF_EXPR: | |
6138 | case ALIGNOF_EXPR: | |
6139 | case OFFSETOF_EXPR: | |
6140 | case NOEXCEPT_EXPR: | |
6141 | case TEMPLATE_PARM_INDEX: | |
6142 | case TRAIT_EXPR: | |
6143 | case IDENTIFIER_NODE: | |
6144 | case USERDEF_LITERAL: | |
6145 | /* We can see a FIELD_DECL in a pointer-to-member expression. */ | |
6146 | case FIELD_DECL: | |
cda0a029 | 6147 | case RESULT_DECL: |
2d76680f | 6148 | case USING_DECL: |
60813a46 | 6149 | case USING_STMT: |
3e605b20 | 6150 | case PLACEHOLDER_EXPR: |
971e17ff | 6151 | case REQUIRES_EXPR: |
98e5a19a | 6152 | case STATIC_ASSERT: |
96a95ac1 | 6153 | case DEBUG_BEGIN_STMT: |
2d76680f PC |
6154 | return true; |
6155 | ||
3075affd JM |
6156 | case RETURN_EXPR: |
6157 | if (!RECUR (TREE_OPERAND (t, 0), any)) | |
6158 | return false; | |
6159 | /* FALLTHROUGH */ | |
6160 | ||
6161 | case BREAK_STMT: | |
6162 | case CONTINUE_STMT: | |
6163 | *jump_target = t; | |
6164 | return true; | |
6165 | ||
a0ab7ccd JM |
6166 | case PARM_DECL: |
6167 | if (now) | |
6168 | { | |
6169 | if (flags & tf_error) | |
6170 | error ("%qE is not a constant expression", t); | |
6171 | return false; | |
6172 | } | |
6173 | return true; | |
6174 | ||
2d76680f PC |
6175 | case AGGR_INIT_EXPR: |
6176 | case CALL_EXPR: | |
6177 | /* -- an invocation of a function other than a constexpr function | |
6178 | or a constexpr constructor. */ | |
6179 | { | |
6180 | tree fun = get_function_named_in_call (t); | |
6181 | const int nargs = call_expr_nargs (t); | |
6182 | i = 0; | |
6183 | ||
60813a46 JM |
6184 | if (fun == NULL_TREE) |
6185 | { | |
44a845ca MS |
6186 | /* Reset to allow the function to continue past the end |
6187 | of the block below. Otherwise return early. */ | |
6188 | bool bail = true; | |
6189 | ||
35228ac7 JJ |
6190 | if (TREE_CODE (t) == CALL_EXPR |
6191 | && CALL_EXPR_FN (t) == NULL_TREE) | |
6192 | switch (CALL_EXPR_IFN (t)) | |
6193 | { | |
6194 | /* These should be ignored, they are optimized away from | |
6195 | constexpr functions. */ | |
6196 | case IFN_UBSAN_NULL: | |
6197 | case IFN_UBSAN_BOUNDS: | |
6198 | case IFN_UBSAN_VPTR: | |
81fea426 | 6199 | case IFN_FALLTHROUGH: |
35228ac7 | 6200 | return true; |
44a845ca MS |
6201 | |
6202 | case IFN_ADD_OVERFLOW: | |
6203 | case IFN_SUB_OVERFLOW: | |
6204 | case IFN_MUL_OVERFLOW: | |
e16f1cc7 | 6205 | case IFN_LAUNDER: |
d8fcab68 | 6206 | case IFN_VEC_CONVERT: |
44a845ca | 6207 | bail = false; |
d8fcab68 | 6208 | break; |
44a845ca | 6209 | |
35228ac7 JJ |
6210 | default: |
6211 | break; | |
6212 | } | |
44a845ca MS |
6213 | |
6214 | if (bail) | |
6215 | { | |
6216 | /* fold_call_expr can't do anything with IFN calls. */ | |
6217 | if (flags & tf_error) | |
e40b6fc7 | 6218 | error_at (loc, "call to internal function %qE", t); |
44a845ca MS |
6219 | return false; |
6220 | } | |
60813a46 | 6221 | } |
44a845ca MS |
6222 | |
6223 | if (fun && is_overloaded_fn (fun)) | |
2d76680f PC |
6224 | { |
6225 | if (TREE_CODE (fun) == FUNCTION_DECL) | |
6226 | { | |
6227 | if (builtin_valid_in_constant_expr_p (fun)) | |
6228 | return true; | |
6229 | if (!DECL_DECLARED_CONSTEXPR_P (fun) | |
6230 | /* Allow any built-in function; if the expansion | |
6231 | isn't constant, we'll deal with that then. */ | |
3d78e008 | 6232 | && !fndecl_built_in_p (fun)) |
2d76680f PC |
6233 | { |
6234 | if (flags & tf_error) | |
6235 | { | |
84fa214d | 6236 | error_at (loc, "call to non-%<constexpr%> function %qD", |
e40b6fc7 | 6237 | fun); |
2d76680f PC |
6238 | explain_invalid_constexpr_fn (fun); |
6239 | } | |
6240 | return false; | |
6241 | } | |
6242 | /* A call to a non-static member function takes the address | |
6243 | of the object as the first argument. But in a constant | |
6244 | expression the address will be folded away, so look | |
6245 | through it now. */ | |
6246 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) | |
6247 | && !DECL_CONSTRUCTOR_P (fun)) | |
6248 | { | |
6249 | tree x = get_nth_callarg (t, 0); | |
6250 | if (is_this_parameter (x)) | |
3e605b20 | 6251 | return true; |
a0ab7ccd JM |
6252 | /* Don't require an immediately constant value, as |
6253 | constexpr substitution might not use the value. */ | |
6254 | bool sub_now = false; | |
6255 | if (!potential_constant_expression_1 (x, rval, strict, | |
3075affd JM |
6256 | sub_now, flags, |
6257 | jump_target)) | |
2d76680f PC |
6258 | return false; |
6259 | i = 1; | |
6260 | } | |
6261 | } | |
6262 | else | |
6263 | { | |
69eb4fde | 6264 | if (!RECUR (fun, true)) |
2d76680f PC |
6265 | return false; |
6266 | fun = get_first_fn (fun); | |
6267 | } | |
6268 | /* Skip initial arguments to base constructors. */ | |
6269 | if (DECL_BASE_CONSTRUCTOR_P (fun)) | |
6270 | i = num_artificial_parms_for (fun); | |
6271 | fun = DECL_ORIGIN (fun); | |
6272 | } | |
44a845ca | 6273 | else if (fun) |
2d76680f | 6274 | { |
69eb4fde | 6275 | if (RECUR (fun, rval)) |
2d76680f PC |
6276 | /* Might end up being a constant function pointer. */; |
6277 | else | |
6278 | return false; | |
6279 | } | |
6280 | for (; i < nargs; ++i) | |
6281 | { | |
6282 | tree x = get_nth_callarg (t, i); | |
c3c29ba5 JM |
6283 | /* In a template, reference arguments haven't been converted to |
6284 | REFERENCE_TYPE and we might not even know if the parameter | |
6285 | is a reference, so accept lvalue constants too. */ | |
6286 | bool rv = processing_template_decl ? any : rval; | |
a0ab7ccd JM |
6287 | /* Don't require an immediately constant value, as constexpr |
6288 | substitution might not use the value of the argument. */ | |
6289 | bool sub_now = false; | |
6290 | if (!potential_constant_expression_1 (x, rv, strict, | |
3075affd | 6291 | sub_now, flags, jump_target)) |
2d76680f PC |
6292 | return false; |
6293 | } | |
6294 | return true; | |
6295 | } | |
6296 | ||
6297 | case NON_LVALUE_EXPR: | |
6298 | /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to | |
6299 | -- an lvalue of integral type that refers to a non-volatile | |
6300 | const variable or static data member initialized with | |
6301 | constant expressions, or | |
6302 | ||
6303 | -- an lvalue of literal type that refers to non-volatile | |
6304 | object defined with constexpr, or that refers to a | |
6305 | sub-object of such an object; */ | |
69eb4fde | 6306 | return RECUR (TREE_OPERAND (t, 0), rval); |
2d76680f PC |
6307 | |
6308 | case VAR_DECL: | |
70f40fea | 6309 | if (DECL_HAS_VALUE_EXPR_P (t)) |
c1051bf7 | 6310 | { |
68ad1bf7 | 6311 | if (now && is_normal_capture_proxy (t)) |
c1051bf7 JM |
6312 | { |
6313 | /* -- in a lambda-expression, a reference to this or to a | |
6314 | variable with automatic storage duration defined outside that | |
6315 | lambda-expression, where the reference would be an | |
6316 | odr-use. */ | |
6317 | if (flags & tf_error) | |
6318 | { | |
6319 | tree cap = DECL_CAPTURED_VARIABLE (t); | |
6320 | error ("lambda capture of %qE is not a constant expression", | |
6321 | cap); | |
6322 | if (!want_rval && decl_constant_var_p (cap)) | |
6323 | inform (input_location, "because it is used as a glvalue"); | |
6324 | } | |
6325 | return false; | |
6326 | } | |
6327 | return RECUR (DECL_VALUE_EXPR (t), rval); | |
6328 | } | |
69eb4fde | 6329 | if (want_rval |
98e5a19a | 6330 | && !var_in_maybe_constexpr_fn (t) |
7dc2b4a2 | 6331 | && !type_dependent_expression_p (t) |
4b691b13 | 6332 | && !decl_maybe_constant_var_p (t) |
69eb4fde JM |
6333 | && (strict |
6334 | || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t)) | |
4b691b13 JM |
6335 | || (DECL_INITIAL (t) |
6336 | && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))) | |
7dc2b4a2 | 6337 | && COMPLETE_TYPE_P (TREE_TYPE (t)) |
dbcd32f8 | 6338 | && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
2d76680f PC |
6339 | { |
6340 | if (flags & tf_error) | |
6341 | non_const_var_error (t); | |
6342 | return false; | |
6343 | } | |
6344 | return true; | |
6345 | ||
6346 | case NOP_EXPR: | |
ed3ea9f2 JJ |
6347 | if (REINTERPRET_CAST_P (t)) |
6348 | { | |
6349 | if (flags & tf_error) | |
a9c697b8 | 6350 | error_at (loc, "%<reinterpret_cast%> is not a constant expression"); |
ed3ea9f2 JJ |
6351 | return false; |
6352 | } | |
6353 | /* FALLTHRU */ | |
2d76680f PC |
6354 | case CONVERT_EXPR: |
6355 | case VIEW_CONVERT_EXPR: | |
6356 | /* -- a reinterpret_cast. FIXME not implemented, and this rule | |
6357 | may change to something more specific to type-punning (DR 1312). */ | |
6358 | { | |
6359 | tree from = TREE_OPERAND (t, 0); | |
dfd7fdca DM |
6360 | if (location_wrapper_p (t)) |
6361 | return (RECUR (from, want_rval)); | |
6362 | if (INDIRECT_TYPE_P (TREE_TYPE (t))) | |
2d76680f | 6363 | { |
dfd7fdca DM |
6364 | STRIP_ANY_LOCATION_WRAPPER (from); |
6365 | if (TREE_CODE (from) == INTEGER_CST | |
6366 | && !integer_zerop (from)) | |
6367 | { | |
6368 | if (flags & tf_error) | |
a9c697b8 MS |
6369 | error_at (loc, |
6370 | "%<reinterpret_cast%> from integer to pointer"); | |
dfd7fdca DM |
6371 | return false; |
6372 | } | |
2d76680f | 6373 | } |
69eb4fde | 6374 | return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR)); |
2d76680f PC |
6375 | } |
6376 | ||
be845b04 JJ |
6377 | case ADDRESSOF_EXPR: |
6378 | /* This is like ADDR_EXPR, except it won't form pointer-to-member. */ | |
6379 | t = TREE_OPERAND (t, 0); | |
6380 | goto handle_addr_expr; | |
6381 | ||
2d76680f PC |
6382 | case ADDR_EXPR: |
6383 | /* -- a unary operator & that is applied to an lvalue that | |
6384 | designates an object with thread or automatic storage | |
6385 | duration; */ | |
6386 | t = TREE_OPERAND (t, 0); | |
6387 | ||
6388 | if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t)) | |
6389 | /* A pointer-to-member constant. */ | |
6390 | return true; | |
6391 | ||
be845b04 | 6392 | handle_addr_expr: |
2d76680f PC |
6393 | #if 0 |
6394 | /* FIXME adjust when issue 1197 is fully resolved. For now don't do | |
6395 | any checking here, as we might dereference the pointer later. If | |
6396 | we remove this code, also remove check_automatic_or_tls. */ | |
6397 | i = check_automatic_or_tls (t); | |
6398 | if (i == ck_ok) | |
6399 | return true; | |
6400 | if (i == ck_bad) | |
6401 | { | |
6402 | if (flags & tf_error) | |
6403 | error ("address-of an object %qE with thread local or " | |
6404 | "automatic storage is not a constant expression", t); | |
6405 | return false; | |
6406 | } | |
6407 | #endif | |
69eb4fde | 6408 | return RECUR (t, any); |
2d76680f | 6409 | |
dc8d2d00 JM |
6410 | case REALPART_EXPR: |
6411 | case IMAGPART_EXPR: | |
2d76680f PC |
6412 | case COMPONENT_REF: |
6413 | case BIT_FIELD_REF: | |
6414 | case ARROW_EXPR: | |
6415 | case OFFSET_REF: | |
6416 | /* -- a class member access unless its postfix-expression is | |
6417 | of literal type or of pointer to literal type. */ | |
6418 | /* This test would be redundant, as it follows from the | |
6419 | postfix-expression being a potential constant expression. */ | |
19dedccf JM |
6420 | if (type_unknown_p (t)) |
6421 | return true; | |
69eb4fde | 6422 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
2d76680f PC |
6423 | |
6424 | case EXPR_PACK_EXPANSION: | |
69eb4fde | 6425 | return RECUR (PACK_EXPANSION_PATTERN (t), want_rval); |
2d76680f PC |
6426 | |
6427 | case INDIRECT_REF: | |
6428 | { | |
6429 | tree x = TREE_OPERAND (t, 0); | |
6430 | STRIP_NOPS (x); | |
948d0d91 | 6431 | if (is_this_parameter (x) && !is_capture_proxy (x)) |
2d76680f | 6432 | { |
5d4e573b | 6433 | if (!var_in_maybe_constexpr_fn (x)) |
2d76680f PC |
6434 | { |
6435 | if (flags & tf_error) | |
e40b6fc7 | 6436 | error_at (loc, "use of %<this%> in a constant expression"); |
2d76680f PC |
6437 | return false; |
6438 | } | |
2d76680f PC |
6439 | return true; |
6440 | } | |
69eb4fde | 6441 | return RECUR (x, rval); |
2d76680f PC |
6442 | } |
6443 | ||
60813a46 JM |
6444 | case STATEMENT_LIST: |
6445 | { | |
6446 | tree_stmt_iterator i; | |
6447 | for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) | |
6448 | { | |
69eb4fde | 6449 | if (!RECUR (tsi_stmt (i), any)) |
60813a46 JM |
6450 | return false; |
6451 | } | |
6452 | return true; | |
6453 | } | |
6454 | break; | |
6455 | ||
6456 | case MODIFY_EXPR: | |
6457 | if (cxx_dialect < cxx14) | |
6458 | goto fail; | |
69eb4fde | 6459 | if (!RECUR (TREE_OPERAND (t, 0), any)) |
60813a46 | 6460 | return false; |
69eb4fde | 6461 | if (!RECUR (TREE_OPERAND (t, 1), rval)) |
60813a46 JM |
6462 | return false; |
6463 | return true; | |
6464 | ||
6465 | case MODOP_EXPR: | |
6466 | if (cxx_dialect < cxx14) | |
6467 | goto fail; | |
69eb4fde | 6468 | if (!RECUR (TREE_OPERAND (t, 0), rval)) |
60813a46 | 6469 | return false; |
69eb4fde | 6470 | if (!RECUR (TREE_OPERAND (t, 2), rval)) |
60813a46 JM |
6471 | return false; |
6472 | return true; | |
6473 | ||
60813a46 | 6474 | case DO_STMT: |
69eb4fde | 6475 | if (!RECUR (DO_COND (t), rval)) |
60813a46 | 6476 | return false; |
69eb4fde | 6477 | if (!RECUR (DO_BODY (t), any)) |
60813a46 | 6478 | return false; |
3075affd JM |
6479 | if (breaks (jump_target) || continues (jump_target)) |
6480 | *jump_target = NULL_TREE; | |
60813a46 JM |
6481 | return true; |
6482 | ||
6483 | case FOR_STMT: | |
69eb4fde | 6484 | if (!RECUR (FOR_INIT_STMT (t), any)) |
60813a46 | 6485 | return false; |
8e9558f0 MP |
6486 | tmp = FOR_COND (t); |
6487 | if (!RECUR (tmp, rval)) | |
60813a46 | 6488 | return false; |
8e9558f0 MP |
6489 | if (tmp) |
6490 | { | |
6491 | if (!processing_template_decl) | |
6492 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
a385474c MP |
6493 | /* If we couldn't evaluate the condition, it might not ever be |
6494 | true. */ | |
6495 | if (!integer_onep (tmp)) | |
8e9558f0 MP |
6496 | return true; |
6497 | } | |
69eb4fde | 6498 | if (!RECUR (FOR_EXPR (t), any)) |
60813a46 | 6499 | return false; |
69eb4fde | 6500 | if (!RECUR (FOR_BODY (t), any)) |
60813a46 | 6501 | return false; |
3075affd JM |
6502 | if (breaks (jump_target) || continues (jump_target)) |
6503 | *jump_target = NULL_TREE; | |
60813a46 JM |
6504 | return true; |
6505 | ||
98e5a19a | 6506 | case RANGE_FOR_STMT: |
8112667c MP |
6507 | if (!RECUR (RANGE_FOR_INIT_STMT (t), any)) |
6508 | return false; | |
98e5a19a JM |
6509 | if (!RECUR (RANGE_FOR_EXPR (t), any)) |
6510 | return false; | |
6511 | if (!RECUR (RANGE_FOR_BODY (t), any)) | |
6512 | return false; | |
3075affd JM |
6513 | if (breaks (jump_target) || continues (jump_target)) |
6514 | *jump_target = NULL_TREE; | |
98e5a19a JM |
6515 | return true; |
6516 | ||
60813a46 | 6517 | case WHILE_STMT: |
8e9558f0 MP |
6518 | tmp = WHILE_COND (t); |
6519 | if (!RECUR (tmp, rval)) | |
60813a46 | 6520 | return false; |
8e9558f0 MP |
6521 | if (!processing_template_decl) |
6522 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
a385474c MP |
6523 | /* If we couldn't evaluate the condition, it might not ever be true. */ |
6524 | if (!integer_onep (tmp)) | |
8e9558f0 | 6525 | return true; |
69eb4fde | 6526 | if (!RECUR (WHILE_BODY (t), any)) |
60813a46 | 6527 | return false; |
3075affd JM |
6528 | if (breaks (jump_target) || continues (jump_target)) |
6529 | *jump_target = NULL_TREE; | |
60813a46 JM |
6530 | return true; |
6531 | ||
6532 | case SWITCH_STMT: | |
69eb4fde | 6533 | if (!RECUR (SWITCH_STMT_COND (t), rval)) |
60813a46 | 6534 | return false; |
ce965730 | 6535 | /* FIXME we don't check SWITCH_STMT_BODY currently, because even |
c7a53bdb JJ |
6536 | unreachable labels would be checked and it is enough if there is |
6537 | a single switch cond value for which it is a valid constant | |
6538 | expression. We need to check if there are any RETURN_EXPRs | |
6539 | or CONTINUE_STMTs inside of the body though, as in that case | |
6540 | we need to set *jump_target. */ | |
6541 | else | |
6542 | { | |
6543 | hash_set<tree> pset; | |
6544 | check_for_return_continue_data data = { &pset, NULL_TREE }; | |
6545 | if (tree ret_expr | |
6546 | = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue, | |
6547 | &data, &pset)) | |
6548 | /* The switch might return. */ | |
6549 | *jump_target = ret_expr; | |
6550 | else if (data.continue_stmt) | |
6551 | /* The switch can't return, but might continue. */ | |
6552 | *jump_target = data.continue_stmt; | |
6553 | } | |
60813a46 JM |
6554 | return true; |
6555 | ||
58611fb6 | 6556 | case STMT_EXPR: |
69eb4fde | 6557 | return RECUR (STMT_EXPR_STMT (t), rval); |
58611fb6 | 6558 | |
2d76680f | 6559 | case LAMBDA_EXPR: |
899ac3b8 JM |
6560 | if (cxx_dialect >= cxx17) |
6561 | /* In C++17 lambdas can be constexpr, don't give up yet. */ | |
6562 | return true; | |
6563 | else if (flags & tf_error) | |
6564 | error_at (loc, "lambda-expression is not a constant expression " | |
6565 | "before C++17"); | |
6566 | return false; | |
6567 | ||
2d76680f PC |
6568 | case DYNAMIC_CAST_EXPR: |
6569 | case PSEUDO_DTOR_EXPR: | |
2d76680f PC |
6570 | case NEW_EXPR: |
6571 | case VEC_NEW_EXPR: | |
6572 | case DELETE_EXPR: | |
6573 | case VEC_DELETE_EXPR: | |
6574 | case THROW_EXPR: | |
e40b6fc7 JJ |
6575 | case OMP_PARALLEL: |
6576 | case OMP_TASK: | |
6577 | case OMP_FOR: | |
897064e2 | 6578 | case OMP_SIMD: |
e40b6fc7 JJ |
6579 | case OMP_DISTRIBUTE: |
6580 | case OMP_TASKLOOP: | |
d81ab49d | 6581 | case OMP_LOOP: |
e40b6fc7 JJ |
6582 | case OMP_TEAMS: |
6583 | case OMP_TARGET_DATA: | |
6584 | case OMP_TARGET: | |
6585 | case OMP_SECTIONS: | |
6586 | case OMP_ORDERED: | |
6587 | case OMP_CRITICAL: | |
6588 | case OMP_SINGLE: | |
6589 | case OMP_SECTION: | |
6590 | case OMP_MASTER: | |
6591 | case OMP_TASKGROUP: | |
6592 | case OMP_TARGET_UPDATE: | |
6593 | case OMP_TARGET_ENTER_DATA: | |
6594 | case OMP_TARGET_EXIT_DATA: | |
2d76680f PC |
6595 | case OMP_ATOMIC: |
6596 | case OMP_ATOMIC_READ: | |
6597 | case OMP_ATOMIC_CAPTURE_OLD: | |
6598 | case OMP_ATOMIC_CAPTURE_NEW: | |
28567c40 | 6599 | case OMP_DEPOBJ: |
e40b6fc7 JJ |
6600 | case OACC_PARALLEL: |
6601 | case OACC_KERNELS: | |
6602 | case OACC_DATA: | |
6603 | case OACC_HOST_DATA: | |
6604 | case OACC_LOOP: | |
6605 | case OACC_CACHE: | |
6606 | case OACC_DECLARE: | |
6607 | case OACC_ENTER_DATA: | |
6608 | case OACC_EXIT_DATA: | |
6609 | case OACC_UPDATE: | |
2d76680f PC |
6610 | /* GCC internal stuff. */ |
6611 | case VA_ARG_EXPR: | |
2d76680f | 6612 | case TRANSACTION_EXPR: |
81b6a6c5 | 6613 | case AT_ENCODE_EXPR: |
60813a46 | 6614 | fail: |
2d76680f | 6615 | if (flags & tf_error) |
e40b6fc7 | 6616 | error_at (loc, "expression %qE is not a constant expression", t); |
2d76680f PC |
6617 | return false; |
6618 | ||
529bc410 | 6619 | case ASM_EXPR: |
7c814975 MP |
6620 | if (flags & tf_error) |
6621 | inline_asm_in_constexpr_error (loc); | |
6622 | return false; | |
529bc410 | 6623 | |
bf8d8309 MP |
6624 | case OBJ_TYPE_REF: |
6625 | if (cxx_dialect >= cxx2a) | |
6626 | /* In C++2a virtual calls can be constexpr, don't give up yet. */ | |
6627 | return true; | |
6628 | else if (flags & tf_error) | |
a9c697b8 MS |
6629 | error_at (loc, |
6630 | "virtual functions cannot be %<constexpr%> before C++2a"); | |
bf8d8309 MP |
6631 | return false; |
6632 | ||
2d76680f PC |
6633 | case TYPEID_EXPR: |
6634 | /* -- a typeid expression whose operand is of polymorphic | |
6635 | class type; */ | |
6636 | { | |
6637 | tree e = TREE_OPERAND (t, 0); | |
6638 | if (!TYPE_P (e) && !type_dependent_expression_p (e) | |
6639 | && TYPE_POLYMORPHIC_P (TREE_TYPE (e))) | |
6640 | { | |
6641 | if (flags & tf_error) | |
a9c697b8 | 6642 | error_at (loc, "%<typeid%> is not a constant expression " |
e40b6fc7 | 6643 | "because %qE is of polymorphic type", e); |
2d76680f PC |
6644 | return false; |
6645 | } | |
6646 | return true; | |
6647 | } | |
6648 | ||
1af4ebf5 | 6649 | case POINTER_DIFF_EXPR: |
2d76680f | 6650 | case MINUS_EXPR: |
2d76680f PC |
6651 | want_rval = true; |
6652 | goto binary; | |
6653 | ||
6654 | case LT_EXPR: | |
6655 | case LE_EXPR: | |
6656 | case GT_EXPR: | |
6657 | case GE_EXPR: | |
6658 | case EQ_EXPR: | |
6659 | case NE_EXPR: | |
2d76680f PC |
6660 | want_rval = true; |
6661 | goto binary; | |
6662 | ||
60813a46 JM |
6663 | case PREINCREMENT_EXPR: |
6664 | case POSTINCREMENT_EXPR: | |
6665 | case PREDECREMENT_EXPR: | |
6666 | case POSTDECREMENT_EXPR: | |
6667 | if (cxx_dialect < cxx14) | |
6668 | goto fail; | |
6669 | goto unary; | |
6670 | ||
2d76680f PC |
6671 | case BIT_NOT_EXPR: |
6672 | /* A destructor. */ | |
6673 | if (TYPE_P (TREE_OPERAND (t, 0))) | |
6674 | return true; | |
191816a3 | 6675 | /* fall through. */ |
2d76680f | 6676 | |
2d76680f PC |
6677 | case CONJ_EXPR: |
6678 | case SAVE_EXPR: | |
6679 | case FIX_TRUNC_EXPR: | |
6680 | case FLOAT_EXPR: | |
6681 | case NEGATE_EXPR: | |
6682 | case ABS_EXPR: | |
e197e64e | 6683 | case ABSU_EXPR: |
2d76680f PC |
6684 | case TRUTH_NOT_EXPR: |
6685 | case FIXED_CONVERT_EXPR: | |
6686 | case UNARY_PLUS_EXPR: | |
4856a1f0 PC |
6687 | case UNARY_LEFT_FOLD_EXPR: |
6688 | case UNARY_RIGHT_FOLD_EXPR: | |
60813a46 | 6689 | unary: |
69eb4fde | 6690 | return RECUR (TREE_OPERAND (t, 0), rval); |
2d76680f PC |
6691 | |
6692 | case CAST_EXPR: | |
6693 | case CONST_CAST_EXPR: | |
6694 | case STATIC_CAST_EXPR: | |
6695 | case REINTERPRET_CAST_EXPR: | |
6696 | case IMPLICIT_CONV_EXPR: | |
6697 | if (cxx_dialect < cxx11 | |
6698 | && !dependent_type_p (TREE_TYPE (t)) | |
6699 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))) | |
6700 | /* In C++98, a conversion to non-integral type can't be part of a | |
6701 | constant expression. */ | |
6702 | { | |
6703 | if (flags & tf_error) | |
e40b6fc7 JJ |
6704 | error_at (loc, |
6705 | "cast to non-integral type %qT in a constant expression", | |
6706 | TREE_TYPE (t)); | |
2d76680f PC |
6707 | return false; |
6708 | } | |
fe0604d3 MP |
6709 | /* This might be a conversion from a class to a (potentially) literal |
6710 | type. Let's consider it potentially constant since the conversion | |
6711 | might be a constexpr user-defined conversion. */ | |
6712 | else if (cxx_dialect >= cxx11 | |
6713 | && (dependent_type_p (TREE_TYPE (t)) | |
6714 | || !COMPLETE_TYPE_P (TREE_TYPE (t)) | |
6715 | || literal_type_p (TREE_TYPE (t))) | |
6716 | && TREE_OPERAND (t, 0)) | |
6717 | { | |
6718 | tree type = TREE_TYPE (TREE_OPERAND (t, 0)); | |
6719 | /* If this is a dependent type, it could end up being a class | |
6720 | with conversions. */ | |
6721 | if (type == NULL_TREE || WILDCARD_TYPE_P (type)) | |
6722 | return true; | |
6723 | /* Or a non-dependent class which has conversions. */ | |
6724 | else if (CLASS_TYPE_P (type) | |
6725 | && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type))) | |
6726 | return true; | |
6727 | } | |
2d76680f | 6728 | |
69eb4fde | 6729 | return (RECUR (TREE_OPERAND (t, 0), |
9f613f06 | 6730 | !TYPE_REF_P (TREE_TYPE (t)))); |
2d76680f | 6731 | |
60813a46 | 6732 | case BIND_EXPR: |
69eb4fde | 6733 | return RECUR (BIND_EXPR_BODY (t), want_rval); |
60813a46 | 6734 | |
60813a46 JM |
6735 | case CLEANUP_POINT_EXPR: |
6736 | case MUST_NOT_THROW_EXPR: | |
6737 | case TRY_CATCH_EXPR: | |
769430b2 | 6738 | case TRY_BLOCK: |
60813a46 JM |
6739 | case EH_SPEC_BLOCK: |
6740 | case EXPR_STMT: | |
2d76680f PC |
6741 | case PAREN_EXPR: |
6742 | case NON_DEPENDENT_EXPR: | |
6743 | /* For convenience. */ | |
f937929e JM |
6744 | case LOOP_EXPR: |
6745 | case EXIT_EXPR: | |
69eb4fde | 6746 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
2d76680f | 6747 | |
98e5a19a JM |
6748 | case DECL_EXPR: |
6749 | tmp = DECL_EXPR_DECL (t); | |
6750 | if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)) | |
6751 | { | |
6752 | if (TREE_STATIC (tmp)) | |
6753 | { | |
6754 | if (flags & tf_error) | |
6755 | error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared " | |
4b691b13 | 6756 | "%<static%> in %<constexpr%> context", tmp); |
98e5a19a JM |
6757 | return false; |
6758 | } | |
6759 | else if (CP_DECL_THREAD_LOCAL_P (tmp)) | |
6760 | { | |
6761 | if (flags & tf_error) | |
6762 | error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared " | |
4b691b13 | 6763 | "%<thread_local%> in %<constexpr%> context", tmp); |
98e5a19a JM |
6764 | return false; |
6765 | } | |
3885527d PC |
6766 | else if (!check_for_uninitialized_const_var |
6767 | (tmp, /*constexpr_context_p=*/true, flags)) | |
6768 | return false; | |
98e5a19a JM |
6769 | } |
6770 | return RECUR (tmp, want_rval); | |
6771 | ||
769430b2 JM |
6772 | case TRY_FINALLY_EXPR: |
6773 | return (RECUR (TREE_OPERAND (t, 0), want_rval) | |
6774 | && RECUR (TREE_OPERAND (t, 1), any)); | |
6775 | ||
2d76680f | 6776 | case SCOPE_REF: |
69eb4fde | 6777 | return RECUR (TREE_OPERAND (t, 1), want_rval); |
2d76680f PC |
6778 | |
6779 | case TARGET_EXPR: | |
d3a9902e JM |
6780 | if (!TARGET_EXPR_DIRECT_INIT_P (t) |
6781 | && !literal_type_p (TREE_TYPE (t))) | |
2d76680f PC |
6782 | { |
6783 | if (flags & tf_error) | |
6784 | { | |
097f82ec | 6785 | auto_diagnostic_group d; |
e40b6fc7 JJ |
6786 | error_at (loc, "temporary of non-literal type %qT in a " |
6787 | "constant expression", TREE_TYPE (t)); | |
2d76680f PC |
6788 | explain_non_literal_class (TREE_TYPE (t)); |
6789 | } | |
6790 | return false; | |
6791 | } | |
191816a3 | 6792 | /* FALLTHRU */ |
2d76680f | 6793 | case INIT_EXPR: |
69eb4fde | 6794 | return RECUR (TREE_OPERAND (t, 1), rval); |
2d76680f PC |
6795 | |
6796 | case CONSTRUCTOR: | |
6797 | { | |
6798 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); | |
6799 | constructor_elt *ce; | |
6800 | for (i = 0; vec_safe_iterate (v, i, &ce); ++i) | |
69eb4fde | 6801 | if (!RECUR (ce->value, want_rval)) |
2d76680f PC |
6802 | return false; |
6803 | return true; | |
6804 | } | |
6805 | ||
6806 | case TREE_LIST: | |
6807 | { | |
6808 | gcc_assert (TREE_PURPOSE (t) == NULL_TREE | |
6809 | || DECL_P (TREE_PURPOSE (t))); | |
69eb4fde | 6810 | if (!RECUR (TREE_VALUE (t), want_rval)) |
2d76680f PC |
6811 | return false; |
6812 | if (TREE_CHAIN (t) == NULL_TREE) | |
6813 | return true; | |
69eb4fde | 6814 | return RECUR (TREE_CHAIN (t), want_rval); |
2d76680f PC |
6815 | } |
6816 | ||
6817 | case TRUNC_DIV_EXPR: | |
6818 | case CEIL_DIV_EXPR: | |
6819 | case FLOOR_DIV_EXPR: | |
6820 | case ROUND_DIV_EXPR: | |
6821 | case TRUNC_MOD_EXPR: | |
6822 | case CEIL_MOD_EXPR: | |
6823 | case ROUND_MOD_EXPR: | |
6824 | { | |
6825 | tree denom = TREE_OPERAND (t, 1); | |
69eb4fde | 6826 | if (!RECUR (denom, rval)) |
2d76680f PC |
6827 | return false; |
6828 | /* We can't call cxx_eval_outermost_constant_expr on an expression | |
234bef96 | 6829 | that hasn't been through instantiate_non_dependent_expr yet. */ |
2d76680f PC |
6830 | if (!processing_template_decl) |
6831 | denom = cxx_eval_outermost_constant_expr (denom, true); | |
6832 | if (integer_zerop (denom)) | |
6833 | { | |
6834 | if (flags & tf_error) | |
64d6d399 | 6835 | error ("division by zero is not a constant expression"); |
2d76680f PC |
6836 | return false; |
6837 | } | |
6838 | else | |
6839 | { | |
6840 | want_rval = true; | |
69eb4fde | 6841 | return RECUR (TREE_OPERAND (t, 0), want_rval); |
2d76680f PC |
6842 | } |
6843 | } | |
6844 | ||
6845 | case COMPOUND_EXPR: | |
6846 | { | |
6847 | /* check_return_expr sometimes wraps a TARGET_EXPR in a | |
6848 | COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR | |
6849 | introduced by build_call_a. */ | |
6850 | tree op0 = TREE_OPERAND (t, 0); | |
6851 | tree op1 = TREE_OPERAND (t, 1); | |
6852 | STRIP_NOPS (op1); | |
6853 | if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) | |
6854 | || TREE_CODE (op1) == EMPTY_CLASS_EXPR) | |
69eb4fde | 6855 | return RECUR (op0, want_rval); |
2d76680f PC |
6856 | else |
6857 | goto binary; | |
6858 | } | |
6859 | ||
6860 | /* If the first operand is the non-short-circuit constant, look at | |
6861 | the second operand; otherwise we only care about the first one for | |
6862 | potentiality. */ | |
6863 | case TRUTH_AND_EXPR: | |
6864 | case TRUTH_ANDIF_EXPR: | |
6865 | tmp = boolean_true_node; | |
6866 | goto truth; | |
6867 | case TRUTH_OR_EXPR: | |
6868 | case TRUTH_ORIF_EXPR: | |
6869 | tmp = boolean_false_node; | |
6870 | truth: | |
6871 | { | |
6872 | tree op = TREE_OPERAND (t, 0); | |
69eb4fde | 6873 | if (!RECUR (op, rval)) |
2d76680f PC |
6874 | return false; |
6875 | if (!processing_template_decl) | |
6876 | op = cxx_eval_outermost_constant_expr (op, true); | |
6877 | if (tree_int_cst_equal (op, tmp)) | |
69eb4fde | 6878 | return RECUR (TREE_OPERAND (t, 1), rval); |
2d76680f PC |
6879 | else |
6880 | return true; | |
6881 | } | |
6882 | ||
6883 | case PLUS_EXPR: | |
6884 | case MULT_EXPR: | |
6885 | case POINTER_PLUS_EXPR: | |
6886 | case RDIV_EXPR: | |
6887 | case EXACT_DIV_EXPR: | |
6888 | case MIN_EXPR: | |
6889 | case MAX_EXPR: | |
6890 | case LSHIFT_EXPR: | |
6891 | case RSHIFT_EXPR: | |
6892 | case LROTATE_EXPR: | |
6893 | case RROTATE_EXPR: | |
6894 | case BIT_IOR_EXPR: | |
6895 | case BIT_XOR_EXPR: | |
6896 | case BIT_AND_EXPR: | |
6897 | case TRUTH_XOR_EXPR: | |
6898 | case UNORDERED_EXPR: | |
6899 | case ORDERED_EXPR: | |
6900 | case UNLT_EXPR: | |
6901 | case UNLE_EXPR: | |
6902 | case UNGT_EXPR: | |
6903 | case UNGE_EXPR: | |
6904 | case UNEQ_EXPR: | |
6905 | case LTGT_EXPR: | |
6906 | case RANGE_EXPR: | |
6907 | case COMPLEX_EXPR: | |
6908 | want_rval = true; | |
6909 | /* Fall through. */ | |
6910 | case ARRAY_REF: | |
6911 | case ARRAY_RANGE_REF: | |
6912 | case MEMBER_REF: | |
6913 | case DOTSTAR_EXPR: | |
41b38772 | 6914 | case MEM_REF: |
4856a1f0 PC |
6915 | case BINARY_LEFT_FOLD_EXPR: |
6916 | case BINARY_RIGHT_FOLD_EXPR: | |
2d76680f PC |
6917 | binary: |
6918 | for (i = 0; i < 2; ++i) | |
69eb4fde | 6919 | if (!RECUR (TREE_OPERAND (t, i), want_rval)) |
2d76680f PC |
6920 | return false; |
6921 | return true; | |
6922 | ||
2d76680f PC |
6923 | case VEC_PERM_EXPR: |
6924 | for (i = 0; i < 3; ++i) | |
69eb4fde | 6925 | if (!RECUR (TREE_OPERAND (t, i), true)) |
2d76680f PC |
6926 | return false; |
6927 | return true; | |
6928 | ||
6929 | case COND_EXPR: | |
d5bcd6d4 JM |
6930 | if (COND_EXPR_IS_VEC_DELETE (t)) |
6931 | { | |
6932 | if (flags & tf_error) | |
e40b6fc7 | 6933 | error_at (loc, "%<delete[]%> is not a constant expression"); |
d5bcd6d4 JM |
6934 | return false; |
6935 | } | |
6936 | /* Fall through. */ | |
6937 | case IF_STMT: | |
2d76680f PC |
6938 | case VEC_COND_EXPR: |
6939 | /* If the condition is a known constant, we know which of the legs we | |
6940 | care about; otherwise we only require that the condition and | |
6941 | either of the legs be potentially constant. */ | |
6942 | tmp = TREE_OPERAND (t, 0); | |
69eb4fde | 6943 | if (!RECUR (tmp, rval)) |
2d76680f PC |
6944 | return false; |
6945 | if (!processing_template_decl) | |
6946 | tmp = cxx_eval_outermost_constant_expr (tmp, true); | |
6947 | if (integer_zerop (tmp)) | |
69eb4fde | 6948 | return RECUR (TREE_OPERAND (t, 2), want_rval); |
2d76680f | 6949 | else if (TREE_CODE (tmp) == INTEGER_CST) |
69eb4fde | 6950 | return RECUR (TREE_OPERAND (t, 1), want_rval); |
2d76680f PC |
6951 | for (i = 1; i < 3; ++i) |
6952 | if (potential_constant_expression_1 (TREE_OPERAND (t, i), | |
3075affd JM |
6953 | want_rval, strict, now, |
6954 | tf_none, jump_target)) | |
2d76680f PC |
6955 | return true; |
6956 | if (flags & tf_error) | |
e40b6fc7 | 6957 | error_at (loc, "expression %qE is not a constant expression", t); |
2d76680f PC |
6958 | return false; |
6959 | ||
6960 | case VEC_INIT_EXPR: | |
6961 | if (VEC_INIT_EXPR_IS_CONSTEXPR (t)) | |
6962 | return true; | |
6963 | if (flags & tf_error) | |
6964 | { | |
e40b6fc7 | 6965 | error_at (loc, "non-constant array initialization"); |
2d76680f PC |
6966 | diagnose_non_constexpr_vec_init (t); |
6967 | } | |
6968 | return false; | |
6969 | ||
133bc698 JM |
6970 | case TYPE_DECL: |
6971 | case TAG_DEFN: | |
6972 | /* We can see these in statement-expressions. */ | |
6973 | return true; | |
6974 | ||
baf9ebc8 | 6975 | case CLEANUP_STMT: |
cda0a029 | 6976 | case EMPTY_CLASS_EXPR: |
7fef86d3 | 6977 | case PREDICT_EXPR: |
cda0a029 JM |
6978 | return false; |
6979 | ||
f937929e JM |
6980 | case GOTO_EXPR: |
6981 | { | |
6982 | tree *target = &TREE_OPERAND (t, 0); | |
ab3af181 JJ |
6983 | /* Gotos representing break and continue are OK. */ |
6984 | if (breaks (target) || continues (target)) | |
3075affd JM |
6985 | { |
6986 | *jump_target = *target; | |
6987 | return true; | |
6988 | } | |
ab3af181 | 6989 | if (flags & tf_error) |
e40b6fc7 | 6990 | error_at (loc, "%<goto%> is not a constant expression"); |
ab3af181 | 6991 | return false; |
f937929e JM |
6992 | } |
6993 | ||
98e09245 | 6994 | case ANNOTATE_EXPR: |
98e09245 JJ |
6995 | return RECUR (TREE_OPERAND (t, 0), rval); |
6996 | ||
2d76680f PC |
6997 | default: |
6998 | if (objc_is_property_ref (t)) | |
6999 | return false; | |
7000 | ||
7001 | sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t))); | |
ab3af181 | 7002 | gcc_unreachable (); |
2d76680f PC |
7003 | return false; |
7004 | } | |
69eb4fde | 7005 | #undef RECUR |
2d76680f PC |
7006 | } |
7007 | ||
3075affd JM |
7008 | bool |
7009 | potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, | |
7010 | tsubst_flags_t flags) | |
7011 | { | |
7012 | tree target = NULL_TREE; | |
7013 | return potential_constant_expression_1 (t, want_rval, strict, now, | |
7014 | flags, &target); | |
7015 | } | |
7016 | ||
2d76680f PC |
7017 | /* The main entry point to the above. */ |
7018 | ||
7019 | bool | |
7020 | potential_constant_expression (tree t) | |
7021 | { | |
a0ab7ccd | 7022 | return potential_constant_expression_1 (t, false, true, false, tf_none); |
2d76680f PC |
7023 | } |
7024 | ||
7025 | /* As above, but require a constant rvalue. */ | |
7026 | ||
7027 | bool | |
7028 | potential_rvalue_constant_expression (tree t) | |
7029 | { | |
a0ab7ccd | 7030 | return potential_constant_expression_1 (t, true, true, false, tf_none); |
2d76680f PC |
7031 | } |
7032 | ||
7033 | /* Like above, but complain about non-constant expressions. */ | |
7034 | ||
7035 | bool | |
7036 | require_potential_constant_expression (tree t) | |
7037 | { | |
d8cff23f MP |
7038 | return potential_constant_expression_1 (t, false, true, false, |
7039 | tf_warning_or_error); | |
2d76680f PC |
7040 | } |
7041 | ||
7042 | /* Cross product of the above. */ | |
7043 | ||
7044 | bool | |
7045 | require_potential_rvalue_constant_expression (tree t) | |
7046 | { | |
d8cff23f MP |
7047 | return potential_constant_expression_1 (t, true, true, false, |
7048 | tf_warning_or_error); | |
7049 | } | |
7050 | ||
7051 | /* Like above, but don't consider PARM_DECL a potential_constant_expression. */ | |
7052 | ||
7053 | bool | |
7054 | require_rvalue_constant_expression (tree t) | |
7055 | { | |
7056 | return potential_constant_expression_1 (t, true, true, true, | |
7057 | tf_warning_or_error); | |
a0ab7ccd JM |
7058 | } |
7059 | ||
7060 | /* Like potential_constant_expression, but don't consider possible constexpr | |
7061 | substitution of the current function. That is, PARM_DECL qualifies under | |
7062 | potential_constant_expression, but not here. | |
7063 | ||
7064 | This is basically what you can check when any actual constant values might | |
7065 | be value-dependent. */ | |
7066 | ||
7067 | bool | |
7068 | is_constant_expression (tree t) | |
7069 | { | |
7070 | return potential_constant_expression_1 (t, false, true, true, tf_none); | |
7071 | } | |
7072 | ||
7073 | /* Like above, but complain about non-constant expressions. */ | |
7074 | ||
7075 | bool | |
7076 | require_constant_expression (tree t) | |
7077 | { | |
7078 | return potential_constant_expression_1 (t, false, true, true, | |
7079 | tf_warning_or_error); | |
7080 | } | |
7081 | ||
7082 | /* Like is_constant_expression, but allow const variables that are not allowed | |
7083 | under constexpr rules. */ | |
7084 | ||
7085 | bool | |
7086 | is_static_init_expression (tree t) | |
7087 | { | |
7088 | return potential_constant_expression_1 (t, false, false, true, tf_none); | |
2d76680f PC |
7089 | } |
7090 | ||
eb07f187 JM |
7091 | /* Returns true if T is a potential constant expression that is not |
7092 | instantiation-dependent, and therefore a candidate for constant folding even | |
7093 | in a template. */ | |
7094 | ||
7095 | bool | |
a0ab7ccd | 7096 | is_nondependent_constant_expression (tree t) |
eb07f187 JM |
7097 | { |
7098 | return (!type_unknown_p (t) | |
7099 | && !BRACE_ENCLOSED_INITIALIZER_P (t) | |
a0ab7ccd | 7100 | && is_constant_expression (t) |
eb07f187 JM |
7101 | && !instantiation_dependent_expression_p (t)); |
7102 | } | |
7103 | ||
7104 | /* Returns true if T is a potential static initializer expression that is not | |
7105 | instantiation-dependent. */ | |
7106 | ||
7107 | bool | |
a0ab7ccd | 7108 | is_nondependent_static_init_expression (tree t) |
eb07f187 JM |
7109 | { |
7110 | return (!type_unknown_p (t) | |
7111 | && !BRACE_ENCLOSED_INITIALIZER_P (t) | |
a0ab7ccd | 7112 | && is_static_init_expression (t) |
eb07f187 JM |
7113 | && !instantiation_dependent_expression_p (t)); |
7114 | } | |
7115 | ||
97f3003f JM |
7116 | /* Finalize constexpr processing after parsing. */ |
7117 | ||
7118 | void | |
7119 | fini_constexpr (void) | |
7120 | { | |
7121 | /* The contexpr call and fundef copies tables are no longer needed. */ | |
7122 | constexpr_call_table = NULL; | |
7123 | fundef_copies_table = NULL; | |
7124 | } | |
7125 | ||
2d76680f | 7126 | #include "gt-cp-constexpr.h" |