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