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