| 1 |
/* Perform -*- C++ -*- constant expression evaluation, including calls to |
| 2 |
constexpr functions. These routines are used both during actual parsing |
| 3 |
and during the instantiation of template functions. |
| 4 |
|
| 5 |
Copyright (C) 1998-2025 Free Software Foundation, Inc. |
| 6 |
|
| 7 |
This file is part of GCC. |
| 8 |
|
| 9 |
GCC is free software; you can redistribute it and/or modify it |
| 10 |
under the terms of the GNU General Public License as published by |
| 11 |
the Free Software Foundation; either version 3, or (at your option) |
| 12 |
any later version. |
| 13 |
|
| 14 |
GCC is distributed in the hope that it will be useful, but |
| 15 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License |
| 20 |
along with GCC; see the file COPYING3. If not see |
| 21 |
<http://www.gnu.org/licenses/>. */ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
#include "system.h" |
| 25 |
#include "coretypes.h" |
| 26 |
#include "cp-tree.h" |
| 27 |
#include "varasm.h" |
| 28 |
#include "c-family/c-objc.h" |
| 29 |
#include "tree-iterator.h" |
| 30 |
#include "gimplify.h" |
| 31 |
#include "builtins.h" |
| 32 |
#include "tree-inline.h" |
| 33 |
#include "ubsan.h" |
| 34 |
#include "timevar.h" |
| 35 |
#include "fold-const-call.h" |
| 36 |
#include "stor-layout.h" |
| 37 |
#include "cgraph.h" |
| 38 |
#include "opts.h" |
| 39 |
#include "stringpool.h" |
| 40 |
#include "attribs.h" |
| 41 |
#include "fold-const.h" |
| 42 |
#include "intl.h" |
| 43 |
#include "toplev.h" |
| 44 |
|
| 45 |
static bool verify_constant (tree, bool, bool *, bool *); |
| 46 |
#define VERIFY_CONSTANT(X) \ |
| 47 |
do { \ |
| 48 |
if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \ |
| 49 |
return t; \ |
| 50 |
} while (0) |
| 51 |
|
| 52 |
static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex, |
| 53 |
bool insert = false); |
| 54 |
static int array_index_cmp (tree key, tree index); |
| 55 |
|
| 56 |
/* Returns true iff FUN is an instantiation of a constexpr function |
| 57 |
template or a defaulted constexpr function. */ |
| 58 |
|
| 59 |
bool |
| 60 |
is_instantiation_of_constexpr (tree fun) |
| 61 |
{ |
| 62 |
return ((DECL_TEMPLOID_INSTANTIATION (fun) |
| 63 |
&& DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun))) |
| 64 |
|| (DECL_DEFAULTED_FN (fun) |
| 65 |
&& DECL_DECLARED_CONSTEXPR_P (fun))); |
| 66 |
} |
| 67 |
|
| 68 |
/* Return true if T is a literal type. */ |
| 69 |
|
| 70 |
bool |
| 71 |
literal_type_p (tree t) |
| 72 |
{ |
| 73 |
if (SCALAR_TYPE_P (t) |
| 74 |
|| VECTOR_TYPE_P (t) |
| 75 |
|| TYPE_REF_P (t) |
| 76 |
|| (VOID_TYPE_P (t) && cxx_dialect >= cxx14)) |
| 77 |
return true; |
| 78 |
if (CLASS_TYPE_P (t)) |
| 79 |
{ |
| 80 |
t = complete_type (t); |
| 81 |
gcc_assert (COMPLETE_TYPE_P (t) || errorcount); |
| 82 |
return CLASSTYPE_LITERAL_P (t); |
| 83 |
} |
| 84 |
if (TREE_CODE (t) == ARRAY_TYPE) |
| 85 |
return literal_type_p (strip_array_types (t)); |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
/* If DECL is a variable declared `constexpr', require its type |
| 90 |
be literal. Return error_mark_node if we give an error, the |
| 91 |
DECL otherwise. */ |
| 92 |
|
| 93 |
tree |
| 94 |
ensure_literal_type_for_constexpr_object (tree decl) |
| 95 |
{ |
| 96 |
tree type = TREE_TYPE (decl); |
| 97 |
if (VAR_P (decl) |
| 98 |
&& (DECL_DECLARED_CONSTEXPR_P (decl) |
| 99 |
|| var_in_constexpr_fn (decl)) |
| 100 |
&& !processing_template_decl) |
| 101 |
{ |
| 102 |
tree stype = strip_array_types (type); |
| 103 |
if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype))) |
| 104 |
/* Don't complain here, we'll complain about incompleteness |
| 105 |
when we try to initialize the variable. */; |
| 106 |
else if (!literal_type_p (type)) |
| 107 |
{ |
| 108 |
if (DECL_DECLARED_CONSTEXPR_P (decl)) |
| 109 |
{ |
| 110 |
auto_diagnostic_group d; |
| 111 |
error_at (DECL_SOURCE_LOCATION (decl), |
| 112 |
"the type %qT of %<constexpr%> variable %qD " |
| 113 |
"is not literal", type, decl); |
| 114 |
explain_non_literal_class (type); |
| 115 |
decl = error_mark_node; |
| 116 |
} |
| 117 |
else if (cxx_dialect < cxx23) |
| 118 |
{ |
| 119 |
if (!is_instantiation_of_constexpr (current_function_decl)) |
| 120 |
{ |
| 121 |
auto_diagnostic_group d; |
| 122 |
error_at (DECL_SOURCE_LOCATION (decl), |
| 123 |
"variable %qD of non-literal type %qT in " |
| 124 |
"%<constexpr%> function only available with " |
| 125 |
"%<-std=c++23%> or %<-std=gnu++23%>", decl, type); |
| 126 |
explain_non_literal_class (type); |
| 127 |
decl = error_mark_node; |
| 128 |
} |
| 129 |
cp_function_chain->invalid_constexpr = true; |
| 130 |
} |
| 131 |
} |
| 132 |
else if (DECL_DECLARED_CONSTEXPR_P (decl) |
| 133 |
&& variably_modified_type_p (type, NULL_TREE)) |
| 134 |
{ |
| 135 |
error_at (DECL_SOURCE_LOCATION (decl), |
| 136 |
"%<constexpr%> variable %qD has variably-modified " |
| 137 |
"type %qT", decl, type); |
| 138 |
decl = error_mark_node; |
| 139 |
} |
| 140 |
} |
| 141 |
return decl; |
| 142 |
} |
| 143 |
|
| 144 |
/* Issue a diagnostic with text GMSGID for constructs that are invalid in |
| 145 |
constexpr functions. CONSTEXPR_FUNDEF_P is true if we're checking |
| 146 |
a constexpr function body; if so, don't report hard errors and issue |
| 147 |
a pedwarn pre-C++23, or a warning in C++23, if requested by |
| 148 |
-Winvalid-constexpr. Otherwise, we're not in the context where we are |
| 149 |
checking if a function can be marked 'constexpr', so give a hard error. */ |
| 150 |
|
| 151 |
ATTRIBUTE_GCC_DIAG(3,4) |
| 152 |
static bool |
| 153 |
constexpr_error (location_t location, bool constexpr_fundef_p, |
| 154 |
const char *gmsgid, ...) |
| 155 |
{ |
| 156 |
diagnostic_info diagnostic; |
| 157 |
va_list ap; |
| 158 |
rich_location richloc (line_table, location); |
| 159 |
va_start (ap, gmsgid); |
| 160 |
bool ret; |
| 161 |
if (!constexpr_fundef_p) |
| 162 |
{ |
| 163 |
/* Report an error that cannot be suppressed. */ |
| 164 |
diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ERROR); |
| 165 |
ret = diagnostic_report_diagnostic (global_dc, &diagnostic); |
| 166 |
} |
| 167 |
else if (warn_invalid_constexpr) |
| 168 |
{ |
| 169 |
diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, |
| 170 |
cxx_dialect < cxx23 ? DK_PEDWARN : DK_WARNING); |
| 171 |
diagnostic.option_id = OPT_Winvalid_constexpr; |
| 172 |
ret = diagnostic_report_diagnostic (global_dc, &diagnostic); |
| 173 |
} |
| 174 |
else |
| 175 |
ret = false; |
| 176 |
va_end (ap); |
| 177 |
return ret; |
| 178 |
} |
| 179 |
|
| 180 |
struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef> |
| 181 |
{ |
| 182 |
static hashval_t hash (const constexpr_fundef *); |
| 183 |
static bool equal (const constexpr_fundef *, const constexpr_fundef *); |
| 184 |
}; |
| 185 |
|
| 186 |
/* This table holds all constexpr function definitions seen in |
| 187 |
the current translation unit. */ |
| 188 |
|
| 189 |
static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table; |
| 190 |
|
| 191 |
/* Utility function used for managing the constexpr function table. |
| 192 |
Return true if the entries pointed to by P and Q are for the |
| 193 |
same constexpr function. */ |
| 194 |
|
| 195 |
inline bool |
| 196 |
constexpr_fundef_hasher::equal (const constexpr_fundef *lhs, |
| 197 |
const constexpr_fundef *rhs) |
| 198 |
{ |
| 199 |
return lhs->decl == rhs->decl; |
| 200 |
} |
| 201 |
|
| 202 |
/* Utility function used for managing the constexpr function table. |
| 203 |
Return a hash value for the entry pointed to by Q. */ |
| 204 |
|
| 205 |
inline hashval_t |
| 206 |
constexpr_fundef_hasher::hash (const constexpr_fundef *fundef) |
| 207 |
{ |
| 208 |
return DECL_UID (fundef->decl); |
| 209 |
} |
| 210 |
|
| 211 |
/* Return a previously saved definition of function FUN. */ |
| 212 |
|
| 213 |
constexpr_fundef * |
| 214 |
retrieve_constexpr_fundef (tree fun) |
| 215 |
{ |
| 216 |
if (constexpr_fundef_table == NULL) |
| 217 |
return NULL; |
| 218 |
|
| 219 |
constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE }; |
| 220 |
return constexpr_fundef_table->find (&fundef); |
| 221 |
} |
| 222 |
|
| 223 |
/* Check whether the parameter and return types of FUN are valid for a |
| 224 |
constexpr function, and complain if COMPLAIN. */ |
| 225 |
|
| 226 |
bool |
| 227 |
is_valid_constexpr_fn (tree fun, bool complain) |
| 228 |
{ |
| 229 |
bool ret = true; |
| 230 |
|
| 231 |
if (DECL_INHERITED_CTOR (fun) |
| 232 |
&& TREE_CODE (fun) == TEMPLATE_DECL) |
| 233 |
{ |
| 234 |
ret = false; |
| 235 |
if (complain) |
| 236 |
error ("inherited constructor %qD is not %<constexpr%>", |
| 237 |
DECL_INHERITED_CTOR (fun)); |
| 238 |
} |
| 239 |
else |
| 240 |
{ |
| 241 |
for (tree parm = FUNCTION_FIRST_USER_PARM (fun); |
| 242 |
parm != NULL_TREE; parm = TREE_CHAIN (parm)) |
| 243 |
if (!literal_type_p (TREE_TYPE (parm))) |
| 244 |
{ |
| 245 |
ret = false; |
| 246 |
if (complain) |
| 247 |
{ |
| 248 |
auto_diagnostic_group d; |
| 249 |
if (constexpr_error (input_location, /*constexpr_fundef_p*/true, |
| 250 |
"invalid type for parameter %d of " |
| 251 |
"%<constexpr%> function %q+#D", |
| 252 |
DECL_PARM_INDEX (parm), fun)) |
| 253 |
explain_non_literal_class (TREE_TYPE (parm)); |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17) |
| 259 |
{ |
| 260 |
ret = false; |
| 261 |
if (complain) |
| 262 |
inform (DECL_SOURCE_LOCATION (fun), |
| 263 |
"lambdas are implicitly %<constexpr%> only in C++17 and later"); |
| 264 |
} |
| 265 |
else if (DECL_DESTRUCTOR_P (fun) && cxx_dialect < cxx20) |
| 266 |
{ |
| 267 |
ret = false; |
| 268 |
if (complain) |
| 269 |
error_at (DECL_SOURCE_LOCATION (fun), |
| 270 |
"%<constexpr%> destructors only available with " |
| 271 |
"%<-std=c++20%> or %<-std=gnu++20%>"); |
| 272 |
} |
| 273 |
else if (!DECL_CONSTRUCTOR_P (fun) && !DECL_DESTRUCTOR_P (fun)) |
| 274 |
{ |
| 275 |
tree rettype = TREE_TYPE (TREE_TYPE (fun)); |
| 276 |
if (!literal_type_p (rettype)) |
| 277 |
{ |
| 278 |
ret = false; |
| 279 |
if (complain) |
| 280 |
{ |
| 281 |
auto_diagnostic_group d; |
| 282 |
if (constexpr_error (input_location, /*constexpr_fundef_p*/true, |
| 283 |
"invalid return type %qT of %<constexpr%> " |
| 284 |
"function %q+D", rettype, fun)) |
| 285 |
explain_non_literal_class (rettype); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
/* C++14 DR 1684 removed this restriction. */ |
| 290 |
if (cxx_dialect < cxx14 |
| 291 |
&& DECL_IOBJ_MEMBER_FUNCTION_P (fun) |
| 292 |
&& !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun))) |
| 293 |
{ |
| 294 |
ret = false; |
| 295 |
if (complain) |
| 296 |
{ |
| 297 |
auto_diagnostic_group d; |
| 298 |
if (pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic, |
| 299 |
"enclosing class of %<constexpr%> non-static" |
| 300 |
" member function %q+#D is not a literal type", |
| 301 |
fun)) |
| 302 |
explain_non_literal_class (DECL_CONTEXT (fun)); |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun))) |
| 307 |
{ |
| 308 |
ret = false; |
| 309 |
if (complain) |
| 310 |
error ("%q#T has virtual base classes", DECL_CONTEXT (fun)); |
| 311 |
} |
| 312 |
|
| 313 |
return ret; |
| 314 |
} |
| 315 |
|
| 316 |
/* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF |
| 317 |
for a member of an anonymous aggregate, INIT is the initializer for that |
| 318 |
member, and VEC_OUTER is the vector of constructor elements for the class |
| 319 |
whose constructor we are processing. Add the initializer to the vector |
| 320 |
and return true to indicate success. */ |
| 321 |
|
| 322 |
static bool |
| 323 |
build_anon_member_initialization (tree member, tree init, |
| 324 |
vec<constructor_elt, va_gc> **vec_outer) |
| 325 |
{ |
| 326 |
/* MEMBER presents the relevant fields from the inside out, but we need |
| 327 |
to build up the initializer from the outside in so that we can reuse |
| 328 |
previously built CONSTRUCTORs if this is, say, the second field in an |
| 329 |
anonymous struct. So we use a vec as a stack. */ |
| 330 |
auto_vec<tree, 2> fields; |
| 331 |
do |
| 332 |
{ |
| 333 |
fields.safe_push (TREE_OPERAND (member, 1)); |
| 334 |
member = TREE_OPERAND (member, 0); |
| 335 |
} |
| 336 |
while (ANON_AGGR_TYPE_P (TREE_TYPE (member)) |
| 337 |
&& TREE_CODE (member) == COMPONENT_REF); |
| 338 |
|
| 339 |
/* VEC has the constructor elements vector for the context of FIELD. |
| 340 |
If FIELD is an anonymous aggregate, we will push inside it. */ |
| 341 |
vec<constructor_elt, va_gc> **vec = vec_outer; |
| 342 |
tree field; |
| 343 |
while (field = fields.pop(), |
| 344 |
ANON_AGGR_TYPE_P (TREE_TYPE (field))) |
| 345 |
{ |
| 346 |
tree ctor; |
| 347 |
/* If there is already an outer constructor entry for the anonymous |
| 348 |
aggregate FIELD, use it; otherwise, insert one. */ |
| 349 |
if (vec_safe_is_empty (*vec) |
| 350 |
|| (*vec)->last().index != field) |
| 351 |
{ |
| 352 |
ctor = build_constructor (TREE_TYPE (field), NULL); |
| 353 |
CONSTRUCTOR_APPEND_ELT (*vec, field, ctor); |
| 354 |
} |
| 355 |
else |
| 356 |
ctor = (*vec)->last().value; |
| 357 |
vec = &CONSTRUCTOR_ELTS (ctor); |
| 358 |
} |
| 359 |
|
| 360 |
/* Now we're at the innermost field, the one that isn't an anonymous |
| 361 |
aggregate. Add its initializer to the CONSTRUCTOR and we're done. */ |
| 362 |
gcc_assert (fields.is_empty()); |
| 363 |
CONSTRUCTOR_APPEND_ELT (*vec, field, init); |
| 364 |
|
| 365 |
return true; |
| 366 |
} |
| 367 |
|
| 368 |
/* Subroutine of build_constexpr_constructor_member_initializers. |
| 369 |
The expression tree T represents a data member initialization |
| 370 |
in a (constexpr) constructor definition. Build a pairing of |
| 371 |
the data member with its initializer, and prepend that pair |
| 372 |
to the existing initialization pair INITS. */ |
| 373 |
|
| 374 |
static bool |
| 375 |
build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec) |
| 376 |
{ |
| 377 |
tree member, init; |
| 378 |
if (TREE_CODE (t) == CLEANUP_POINT_EXPR) |
| 379 |
t = TREE_OPERAND (t, 0); |
| 380 |
if (TREE_CODE (t) == EXPR_STMT) |
| 381 |
t = TREE_OPERAND (t, 0); |
| 382 |
if (t == error_mark_node) |
| 383 |
return false; |
| 384 |
if (TREE_CODE (t) == STATEMENT_LIST) |
| 385 |
{ |
| 386 |
for (tree stmt : tsi_range (t)) |
| 387 |
if (! build_data_member_initialization (stmt, vec)) |
| 388 |
return false; |
| 389 |
return true; |
| 390 |
} |
| 391 |
if (TREE_CODE (t) == CLEANUP_STMT) |
| 392 |
{ |
| 393 |
/* We can't see a CLEANUP_STMT in a constructor for a literal class, |
| 394 |
but we can in a constexpr constructor for a non-literal class. Just |
| 395 |
ignore it; either all the initialization will be constant, in which |
| 396 |
case the cleanup can't run, or it can't be constexpr. |
| 397 |
Still recurse into CLEANUP_BODY. */ |
| 398 |
return build_data_member_initialization (CLEANUP_BODY (t), vec); |
| 399 |
} |
| 400 |
if (TREE_CODE (t) == CONVERT_EXPR) |
| 401 |
t = TREE_OPERAND (t, 0); |
| 402 |
if (TREE_CODE (t) == INIT_EXPR) |
| 403 |
{ |
| 404 |
member = TREE_OPERAND (t, 0); |
| 405 |
init = break_out_target_exprs (TREE_OPERAND (t, 1)); |
| 406 |
} |
| 407 |
else if (TREE_CODE (t) == CALL_EXPR) |
| 408 |
{ |
| 409 |
tree fn = get_callee_fndecl (t); |
| 410 |
if (!fn || !DECL_CONSTRUCTOR_P (fn)) |
| 411 |
/* We're only interested in calls to subobject constructors. */ |
| 412 |
return true; |
| 413 |
member = CALL_EXPR_ARG (t, 0); |
| 414 |
/* We don't use build_cplus_new here because it complains about |
| 415 |
abstract bases. Leaving the call unwrapped means that it has the |
| 416 |
wrong type, but cxx_eval_constant_expression doesn't care. */ |
| 417 |
init = break_out_target_exprs (t); |
| 418 |
} |
| 419 |
else if (TREE_CODE (t) == BIND_EXPR) |
| 420 |
return build_data_member_initialization (BIND_EXPR_BODY (t), vec); |
| 421 |
else |
| 422 |
/* Don't add anything else to the CONSTRUCTOR. */ |
| 423 |
return true; |
| 424 |
if (INDIRECT_REF_P (member)) |
| 425 |
member = TREE_OPERAND (member, 0); |
| 426 |
if (TREE_CODE (member) == NOP_EXPR) |
| 427 |
{ |
| 428 |
tree op = member; |
| 429 |
STRIP_NOPS (op); |
| 430 |
if (TREE_CODE (op) == ADDR_EXPR) |
| 431 |
{ |
| 432 |
gcc_assert (same_type_ignoring_top_level_qualifiers_p |
| 433 |
(TREE_TYPE (TREE_TYPE (op)), |
| 434 |
TREE_TYPE (TREE_TYPE (member)))); |
| 435 |
/* Initializing a cv-qualified member; we need to look through |
| 436 |
the const_cast. */ |
| 437 |
member = op; |
| 438 |
} |
| 439 |
else if (op == current_class_ptr |
| 440 |
&& (same_type_ignoring_top_level_qualifiers_p |
| 441 |
(TREE_TYPE (TREE_TYPE (member)), |
| 442 |
current_class_type))) |
| 443 |
/* Delegating constructor. */ |
| 444 |
member = op; |
| 445 |
else |
| 446 |
{ |
| 447 |
/* This is an initializer for an empty base; keep it for now so |
| 448 |
we can check it in cxx_eval_bare_aggregate. */ |
| 449 |
gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member)))); |
| 450 |
} |
| 451 |
} |
| 452 |
if (TREE_CODE (member) == ADDR_EXPR) |
| 453 |
member = TREE_OPERAND (member, 0); |
| 454 |
if (TREE_CODE (member) == COMPONENT_REF) |
| 455 |
{ |
| 456 |
tree aggr = TREE_OPERAND (member, 0); |
| 457 |
if (TREE_CODE (aggr) == VAR_DECL) |
| 458 |
/* Initializing a local variable, don't add anything. */ |
| 459 |
return true; |
| 460 |
if (TREE_CODE (aggr) != COMPONENT_REF) |
| 461 |
/* Normal member initialization. */ |
| 462 |
member = TREE_OPERAND (member, 1); |
| 463 |
else if (VAR_P (get_base_address (aggr))) |
| 464 |
/* Initializing a local variable, don't add anything. */ |
| 465 |
return true; |
| 466 |
else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr))) |
| 467 |
/* Initializing a member of an anonymous union. */ |
| 468 |
return build_anon_member_initialization (member, init, vec); |
| 469 |
else |
| 470 |
/* We're initializing a vtable pointer in a base. Leave it as |
| 471 |
COMPONENT_REF so we remember the path to get to the vfield. */ |
| 472 |
gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node); |
| 473 |
} |
| 474 |
|
| 475 |
/* Value-initialization can produce multiple initializers for the |
| 476 |
same field; use the last one. */ |
| 477 |
if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member) |
| 478 |
(*vec)->last().value = init; |
| 479 |
else |
| 480 |
CONSTRUCTOR_APPEND_ELT (*vec, member, init); |
| 481 |
return true; |
| 482 |
} |
| 483 |
|
| 484 |
/* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval. |
| 485 |
In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a |
| 486 |
BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */ |
| 487 |
|
| 488 |
static bool |
| 489 |
check_constexpr_bind_expr_vars (tree t) |
| 490 |
{ |
| 491 |
gcc_assert (TREE_CODE (t) == BIND_EXPR); |
| 492 |
|
| 493 |
for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var)) |
| 494 |
if (TREE_CODE (var) == TYPE_DECL |
| 495 |
&& DECL_IMPLICIT_TYPEDEF_P (var) |
| 496 |
&& !LAMBDA_TYPE_P (TREE_TYPE (var))) |
| 497 |
return false; |
| 498 |
return true; |
| 499 |
} |
| 500 |
|
| 501 |
/* Subroutine of check_constexpr_ctor_body. */ |
| 502 |
|
| 503 |
static bool |
| 504 |
check_constexpr_ctor_body_1 (tree last, tree list) |
| 505 |
{ |
| 506 |
switch (TREE_CODE (list)) |
| 507 |
{ |
| 508 |
case DECL_EXPR: |
| 509 |
if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL |
| 510 |
|| TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL) |
| 511 |
return true; |
| 512 |
return false; |
| 513 |
|
| 514 |
case CLEANUP_POINT_EXPR: |
| 515 |
return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0), |
| 516 |
/*complain=*/false); |
| 517 |
|
| 518 |
case BIND_EXPR: |
| 519 |
if (!check_constexpr_bind_expr_vars (list) |
| 520 |
|| !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list), |
| 521 |
/*complain=*/false)) |
| 522 |
return false; |
| 523 |
return true; |
| 524 |
|
| 525 |
case USING_STMT: |
| 526 |
case STATIC_ASSERT: |
| 527 |
case DEBUG_BEGIN_STMT: |
| 528 |
return true; |
| 529 |
|
| 530 |
default: |
| 531 |
return false; |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
/* Make sure that there are no statements after LAST in the constructor |
| 536 |
body represented by LIST. */ |
| 537 |
|
| 538 |
bool |
| 539 |
check_constexpr_ctor_body (tree last, tree list, bool complain) |
| 540 |
{ |
| 541 |
/* C++14 doesn't require a constexpr ctor to have an empty body. */ |
| 542 |
if (cxx_dialect >= cxx14) |
| 543 |
return true; |
| 544 |
|
| 545 |
bool ok = true; |
| 546 |
if (TREE_CODE (list) == STATEMENT_LIST) |
| 547 |
{ |
| 548 |
tree_stmt_iterator i = tsi_last (list); |
| 549 |
for (; !tsi_end_p (i); tsi_prev (&i)) |
| 550 |
{ |
| 551 |
tree t = tsi_stmt (i); |
| 552 |
if (t == last) |
| 553 |
break; |
| 554 |
if (!check_constexpr_ctor_body_1 (last, t)) |
| 555 |
{ |
| 556 |
ok = false; |
| 557 |
break; |
| 558 |
} |
| 559 |
} |
| 560 |
} |
| 561 |
else if (list != last |
| 562 |
&& !check_constexpr_ctor_body_1 (last, list)) |
| 563 |
ok = false; |
| 564 |
if (!ok && complain) |
| 565 |
{ |
| 566 |
pedwarn (input_location, OPT_Wc__14_extensions, |
| 567 |
"%<constexpr%> constructor does not have empty body"); |
| 568 |
ok = true; |
| 569 |
} |
| 570 |
return ok; |
| 571 |
} |
| 572 |
|
| 573 |
/* V is a vector of constructor elements built up for the base and member |
| 574 |
initializers of a constructor for TYPE. They need to be in increasing |
| 575 |
offset order, which they might not be yet if TYPE has a primary base |
| 576 |
which is not first in the base-clause or a vptr and at least one base |
| 577 |
all of which are non-primary. */ |
| 578 |
|
| 579 |
static vec<constructor_elt, va_gc> * |
| 580 |
sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v) |
| 581 |
{ |
| 582 |
tree pri = CLASSTYPE_PRIMARY_BINFO (type); |
| 583 |
tree field_type; |
| 584 |
unsigned i; |
| 585 |
constructor_elt *ce; |
| 586 |
|
| 587 |
if (pri) |
| 588 |
field_type = BINFO_TYPE (pri); |
| 589 |
else if (TYPE_CONTAINS_VPTR_P (type)) |
| 590 |
field_type = vtbl_ptr_type_node; |
| 591 |
else |
| 592 |
return v; |
| 593 |
|
| 594 |
/* Find the element for the primary base or vptr and move it to the |
| 595 |
beginning of the vec. */ |
| 596 |
for (i = 0; vec_safe_iterate (v, i, &ce); ++i) |
| 597 |
if (TREE_TYPE (ce->index) == field_type) |
| 598 |
break; |
| 599 |
|
| 600 |
if (i > 0 && i < vec_safe_length (v)) |
| 601 |
{ |
| 602 |
vec<constructor_elt, va_gc> &vref = *v; |
| 603 |
constructor_elt elt = vref[i]; |
| 604 |
for (; i > 0; --i) |
| 605 |
vref[i] = vref[i-1]; |
| 606 |
vref[0] = elt; |
| 607 |
} |
| 608 |
|
| 609 |
return v; |
| 610 |
} |
| 611 |
|
| 612 |
/* Build compile-time evalable representations of member-initializer list |
| 613 |
for a constexpr constructor. */ |
| 614 |
|
| 615 |
static tree |
| 616 |
build_constexpr_constructor_member_initializers (tree type, tree body) |
| 617 |
{ |
| 618 |
vec<constructor_elt, va_gc> *vec = NULL; |
| 619 |
bool ok = true; |
| 620 |
while (true) |
| 621 |
switch (TREE_CODE (body)) |
| 622 |
{ |
| 623 |
case MUST_NOT_THROW_EXPR: |
| 624 |
case EH_SPEC_BLOCK: |
| 625 |
body = TREE_OPERAND (body, 0); |
| 626 |
break; |
| 627 |
|
| 628 |
case STATEMENT_LIST: |
| 629 |
for (tree stmt : tsi_range (body)) |
| 630 |
{ |
| 631 |
body = stmt; |
| 632 |
if (TREE_CODE (body) == BIND_EXPR) |
| 633 |
break; |
| 634 |
} |
| 635 |
break; |
| 636 |
|
| 637 |
case BIND_EXPR: |
| 638 |
body = BIND_EXPR_BODY (body); |
| 639 |
goto found; |
| 640 |
|
| 641 |
default: |
| 642 |
gcc_unreachable (); |
| 643 |
} |
| 644 |
found: |
| 645 |
if (TREE_CODE (body) == TRY_BLOCK) |
| 646 |
{ |
| 647 |
body = TREE_OPERAND (body, 0); |
| 648 |
if (TREE_CODE (body) == BIND_EXPR) |
| 649 |
body = BIND_EXPR_BODY (body); |
| 650 |
} |
| 651 |
if (TREE_CODE (body) == CLEANUP_POINT_EXPR) |
| 652 |
{ |
| 653 |
body = TREE_OPERAND (body, 0); |
| 654 |
if (TREE_CODE (body) == EXPR_STMT) |
| 655 |
body = TREE_OPERAND (body, 0); |
| 656 |
if (TREE_CODE (body) == INIT_EXPR |
| 657 |
&& (same_type_ignoring_top_level_qualifiers_p |
| 658 |
(TREE_TYPE (TREE_OPERAND (body, 0)), |
| 659 |
current_class_type))) |
| 660 |
{ |
| 661 |
/* Trivial copy. */ |
| 662 |
return TREE_OPERAND (body, 1); |
| 663 |
} |
| 664 |
ok = build_data_member_initialization (body, &vec); |
| 665 |
} |
| 666 |
else if (TREE_CODE (body) == STATEMENT_LIST) |
| 667 |
{ |
| 668 |
for (tree stmt : tsi_range (body)) |
| 669 |
{ |
| 670 |
ok = build_data_member_initialization (stmt, &vec); |
| 671 |
if (!ok) |
| 672 |
break; |
| 673 |
} |
| 674 |
} |
| 675 |
else if (EXPR_P (body)) |
| 676 |
ok = build_data_member_initialization (body, &vec); |
| 677 |
else |
| 678 |
gcc_assert (errorcount > 0); |
| 679 |
if (ok) |
| 680 |
{ |
| 681 |
if (vec_safe_length (vec) > 0) |
| 682 |
{ |
| 683 |
/* In a delegating constructor, return the target. */ |
| 684 |
constructor_elt *ce = &(*vec)[0]; |
| 685 |
if (ce->index == current_class_ptr) |
| 686 |
{ |
| 687 |
body = ce->value; |
| 688 |
vec_free (vec); |
| 689 |
return body; |
| 690 |
} |
| 691 |
} |
| 692 |
vec = sort_constexpr_mem_initializers (type, vec); |
| 693 |
return build_constructor (type, vec); |
| 694 |
} |
| 695 |
else |
| 696 |
return error_mark_node; |
| 697 |
} |
| 698 |
|
| 699 |
/* We have an expression tree T that represents a call, either CALL_EXPR |
| 700 |
or AGGR_INIT_EXPR. If the call is lexically to a named function, |
| 701 |
return the _DECL for that function. */ |
| 702 |
|
| 703 |
static tree |
| 704 |
get_function_named_in_call (tree t) |
| 705 |
{ |
| 706 |
tree callee = cp_get_callee (t); |
| 707 |
tree fun = cp_get_fndecl_from_callee (callee, /*fold*/false); |
| 708 |
return fun ? fun : callee; |
| 709 |
} |
| 710 |
|
| 711 |
/* Subroutine of check_constexpr_fundef. BODY is the body of a function |
| 712 |
declared to be constexpr, or a sub-statement thereof. Returns the |
| 713 |
return value if suitable, error_mark_node for a statement not allowed in |
| 714 |
a constexpr function, or NULL_TREE if no return value was found. */ |
| 715 |
|
| 716 |
tree |
| 717 |
constexpr_fn_retval (tree body) |
| 718 |
{ |
| 719 |
switch (TREE_CODE (body)) |
| 720 |
{ |
| 721 |
case STATEMENT_LIST: |
| 722 |
{ |
| 723 |
tree expr = NULL_TREE; |
| 724 |
for (tree stmt : tsi_range (body)) |
| 725 |
{ |
| 726 |
tree s = constexpr_fn_retval (stmt); |
| 727 |
if (s == error_mark_node) |
| 728 |
return error_mark_node; |
| 729 |
else if (s == NULL_TREE) |
| 730 |
/* Keep iterating. */; |
| 731 |
else if (expr) |
| 732 |
/* Multiple return statements. */ |
| 733 |
return error_mark_node; |
| 734 |
else |
| 735 |
expr = s; |
| 736 |
} |
| 737 |
return expr; |
| 738 |
} |
| 739 |
|
| 740 |
case RETURN_EXPR: |
| 741 |
return break_out_target_exprs (TREE_OPERAND (body, 0)); |
| 742 |
|
| 743 |
case DECL_EXPR: |
| 744 |
{ |
| 745 |
tree decl = DECL_EXPR_DECL (body); |
| 746 |
if (TREE_CODE (decl) == USING_DECL |
| 747 |
/* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */ |
| 748 |
|| DECL_ARTIFICIAL (decl)) |
| 749 |
return NULL_TREE; |
| 750 |
return error_mark_node; |
| 751 |
} |
| 752 |
|
| 753 |
case CLEANUP_POINT_EXPR: |
| 754 |
return constexpr_fn_retval (TREE_OPERAND (body, 0)); |
| 755 |
|
| 756 |
case BIND_EXPR: |
| 757 |
if (!check_constexpr_bind_expr_vars (body)) |
| 758 |
return error_mark_node; |
| 759 |
return constexpr_fn_retval (BIND_EXPR_BODY (body)); |
| 760 |
|
| 761 |
case USING_STMT: |
| 762 |
case DEBUG_BEGIN_STMT: |
| 763 |
return NULL_TREE; |
| 764 |
|
| 765 |
case CALL_EXPR: |
| 766 |
{ |
| 767 |
tree fun = get_function_named_in_call (body); |
| 768 |
if (fun != NULL_TREE |
| 769 |
&& fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE)) |
| 770 |
return NULL_TREE; |
| 771 |
} |
| 772 |
/* Fallthru. */ |
| 773 |
|
| 774 |
default: |
| 775 |
return error_mark_node; |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
/* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of |
| 780 |
FUN; do the necessary transformations to turn it into a single expression |
| 781 |
that we can store in the hash table. */ |
| 782 |
|
| 783 |
static tree |
| 784 |
massage_constexpr_body (tree fun, tree body) |
| 785 |
{ |
| 786 |
if (DECL_CONSTRUCTOR_P (fun)) |
| 787 |
body = build_constexpr_constructor_member_initializers |
| 788 |
(DECL_CONTEXT (fun), body); |
| 789 |
else if (cxx_dialect < cxx14) |
| 790 |
{ |
| 791 |
if (TREE_CODE (body) == EH_SPEC_BLOCK) |
| 792 |
body = EH_SPEC_STMTS (body); |
| 793 |
if (TREE_CODE (body) == MUST_NOT_THROW_EXPR) |
| 794 |
body = TREE_OPERAND (body, 0); |
| 795 |
body = constexpr_fn_retval (body); |
| 796 |
} |
| 797 |
return body; |
| 798 |
} |
| 799 |
|
| 800 |
/* CTYPE is a type constructed from BODY. Return true if some |
| 801 |
bases/fields are uninitialized, and complain if COMPLAIN. */ |
| 802 |
|
| 803 |
static bool |
| 804 |
cx_check_missing_mem_inits (tree ctype, tree body, bool complain) |
| 805 |
{ |
| 806 |
/* We allow uninitialized bases/fields in C++20. */ |
| 807 |
if (cxx_dialect >= cxx20) |
| 808 |
return false; |
| 809 |
|
| 810 |
unsigned nelts = 0; |
| 811 |
|
| 812 |
if (body) |
| 813 |
{ |
| 814 |
if (TREE_CODE (body) != CONSTRUCTOR) |
| 815 |
return false; |
| 816 |
nelts = CONSTRUCTOR_NELTS (body); |
| 817 |
} |
| 818 |
tree field = TYPE_FIELDS (ctype); |
| 819 |
|
| 820 |
if (TREE_CODE (ctype) == UNION_TYPE) |
| 821 |
{ |
| 822 |
if (nelts == 0 && next_aggregate_field (field)) |
| 823 |
{ |
| 824 |
if (complain) |
| 825 |
error ("%<constexpr%> constructor for union %qT must " |
| 826 |
"initialize exactly one non-static data member", ctype); |
| 827 |
return true; |
| 828 |
} |
| 829 |
return false; |
| 830 |
} |
| 831 |
|
| 832 |
/* Iterate over the CONSTRUCTOR, checking any missing fields don't |
| 833 |
need an explicit initialization. */ |
| 834 |
bool bad = false; |
| 835 |
for (unsigned i = 0; i <= nelts; ++i) |
| 836 |
{ |
| 837 |
tree index = NULL_TREE; |
| 838 |
if (i < nelts) |
| 839 |
{ |
| 840 |
index = CONSTRUCTOR_ELT (body, i)->index; |
| 841 |
/* Skip vptr adjustment represented with a COMPONENT_REF. */ |
| 842 |
if (TREE_CODE (index) != FIELD_DECL) |
| 843 |
continue; |
| 844 |
} |
| 845 |
|
| 846 |
for (; field != index; field = DECL_CHAIN (field)) |
| 847 |
{ |
| 848 |
tree ftype; |
| 849 |
if (TREE_CODE (field) != FIELD_DECL) |
| 850 |
continue; |
| 851 |
if (DECL_UNNAMED_BIT_FIELD (field)) |
| 852 |
continue; |
| 853 |
/* Artificial fields can be ignored unless they're bases. */ |
| 854 |
if (DECL_ARTIFICIAL (field) && !DECL_FIELD_IS_BASE (field)) |
| 855 |
continue; |
| 856 |
if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) |
| 857 |
{ |
| 858 |
/* Recurse to check the anonymous aggregate member. */ |
| 859 |
bad |= cx_check_missing_mem_inits |
| 860 |
(TREE_TYPE (field), NULL_TREE, complain); |
| 861 |
if (bad && !complain) |
| 862 |
return true; |
| 863 |
continue; |
| 864 |
} |
| 865 |
ftype = TREE_TYPE (field); |
| 866 |
if (!ftype || !TYPE_P (ftype) || !COMPLETE_TYPE_P (ftype)) |
| 867 |
/* A flexible array can't be intialized here, so don't complain |
| 868 |
that it isn't. */ |
| 869 |
continue; |
| 870 |
if (is_empty_field (field)) |
| 871 |
/* An empty field doesn't need an initializer. */ |
| 872 |
continue; |
| 873 |
ftype = strip_array_types (ftype); |
| 874 |
if (type_has_constexpr_default_constructor (ftype)) |
| 875 |
{ |
| 876 |
/* It's OK to skip a member with a trivial constexpr ctor. |
| 877 |
A constexpr ctor that isn't trivial should have been |
| 878 |
added in by now. */ |
| 879 |
gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype) |
| 880 |
|| errorcount != 0); |
| 881 |
continue; |
| 882 |
} |
| 883 |
if (!complain) |
| 884 |
return true; |
| 885 |
auto_diagnostic_group d; |
| 886 |
error ("member %qD must be initialized by mem-initializer " |
| 887 |
"in %<constexpr%> constructor", field); |
| 888 |
inform (DECL_SOURCE_LOCATION (field), "declared here"); |
| 889 |
bad = true; |
| 890 |
} |
| 891 |
if (field == NULL_TREE) |
| 892 |
break; |
| 893 |
|
| 894 |
if (ANON_AGGR_TYPE_P (TREE_TYPE (index))) |
| 895 |
{ |
| 896 |
/* Check the anonymous aggregate initializer is valid. */ |
| 897 |
bad |= cx_check_missing_mem_inits |
| 898 |
(TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain); |
| 899 |
if (bad && !complain) |
| 900 |
return true; |
| 901 |
} |
| 902 |
field = DECL_CHAIN (field); |
| 903 |
} |
| 904 |
|
| 905 |
return bad; |
| 906 |
} |
| 907 |
|
| 908 |
/* We are processing the definition of the constexpr function FUN. |
| 909 |
Check that its body fulfills the apropriate requirements and |
| 910 |
enter it in the constexpr function definition table. */ |
| 911 |
|
| 912 |
void |
| 913 |
maybe_save_constexpr_fundef (tree fun) |
| 914 |
{ |
| 915 |
if (processing_template_decl |
| 916 |
|| cp_function_chain->invalid_constexpr |
| 917 |
|| (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))) |
| 918 |
return; |
| 919 |
|
| 920 |
/* With -fimplicit-constexpr, try to make inlines constexpr. We'll |
| 921 |
actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass. */ |
| 922 |
bool implicit = false; |
| 923 |
if (flag_implicit_constexpr) |
| 924 |
{ |
| 925 |
if (DECL_DELETING_DESTRUCTOR_P (fun) |
| 926 |
&& decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun))) |
| 927 |
/* Don't inherit implicit constexpr from the non-deleting |
| 928 |
destructor. */ |
| 929 |
DECL_DECLARED_CONSTEXPR_P (fun) = false; |
| 930 |
|
| 931 |
if (!DECL_DECLARED_CONSTEXPR_P (fun) |
| 932 |
&& DECL_DECLARED_INLINE_P (fun) |
| 933 |
&& !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun))) |
| 934 |
implicit = true; |
| 935 |
} |
| 936 |
|
| 937 |
if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit) |
| 938 |
return; |
| 939 |
|
| 940 |
bool complain = !DECL_GENERATED_P (fun) && !implicit; |
| 941 |
|
| 942 |
if (!is_valid_constexpr_fn (fun, complain)) |
| 943 |
return; |
| 944 |
|
| 945 |
tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun)); |
| 946 |
if (massaged == NULL_TREE || massaged == error_mark_node) |
| 947 |
{ |
| 948 |
if (!DECL_CONSTRUCTOR_P (fun) && complain) |
| 949 |
error ("body of %<constexpr%> function %qD not a return-statement", |
| 950 |
fun); |
| 951 |
return; |
| 952 |
} |
| 953 |
|
| 954 |
bool potential = potential_rvalue_constant_expression (massaged); |
| 955 |
if (!potential && complain) |
| 956 |
require_potential_rvalue_constant_expression_fncheck (massaged); |
| 957 |
|
| 958 |
if (DECL_CONSTRUCTOR_P (fun) && potential |
| 959 |
&& !DECL_DEFAULTED_FN (fun)) |
| 960 |
{ |
| 961 |
if (cx_check_missing_mem_inits (DECL_CONTEXT (fun), |
| 962 |
massaged, complain)) |
| 963 |
potential = false; |
| 964 |
else if (cxx_dialect > cxx11) |
| 965 |
{ |
| 966 |
/* What we got from massage_constexpr_body is pretty much just the |
| 967 |
ctor-initializer, also check the body. */ |
| 968 |
massaged = DECL_SAVED_TREE (fun); |
| 969 |
potential = potential_rvalue_constant_expression (massaged); |
| 970 |
if (!potential && complain) |
| 971 |
require_potential_rvalue_constant_expression_fncheck (massaged); |
| 972 |
} |
| 973 |
} |
| 974 |
|
| 975 |
if (!potential && complain |
| 976 |
/* If -Wno-invalid-constexpr was specified, we haven't complained |
| 977 |
about non-constant expressions yet. Register the function and |
| 978 |
complain in explain_invalid_constexpr_fn if the function is |
| 979 |
called. */ |
| 980 |
&& warn_invalid_constexpr != 0) |
| 981 |
return; |
| 982 |
|
| 983 |
if (implicit) |
| 984 |
{ |
| 985 |
if (potential) |
| 986 |
{ |
| 987 |
DECL_DECLARED_CONSTEXPR_P (fun) = true; |
| 988 |
DECL_LANG_SPECIFIC (fun)->u.fn.implicit_constexpr = true; |
| 989 |
if (DECL_CONSTRUCTOR_P (fun)) |
| 990 |
TYPE_HAS_CONSTEXPR_CTOR (DECL_CONTEXT (fun)) = true; |
| 991 |
} |
| 992 |
else |
| 993 |
/* Don't bother keeping the pre-generic body of unsuitable functions |
| 994 |
not explicitly declared constexpr. */ |
| 995 |
return; |
| 996 |
} |
| 997 |
|
| 998 |
constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE}; |
| 999 |
bool clear_ctx = false; |
| 1000 |
if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE) |
| 1001 |
{ |
| 1002 |
clear_ctx = true; |
| 1003 |
DECL_CONTEXT (DECL_RESULT (fun)) = fun; |
| 1004 |
} |
| 1005 |
tree saved_fn = current_function_decl; |
| 1006 |
current_function_decl = fun; |
| 1007 |
entry.body = copy_fn (entry.decl, entry.parms, entry.result); |
| 1008 |
current_function_decl = saved_fn; |
| 1009 |
if (clear_ctx) |
| 1010 |
DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE; |
| 1011 |
if (!potential) |
| 1012 |
/* For a template instantiation, we want to remember the pre-generic body |
| 1013 |
for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression |
| 1014 |
that it doesn't need to bother trying to expand the function. */ |
| 1015 |
entry.result = error_mark_node; |
| 1016 |
|
| 1017 |
register_constexpr_fundef (entry); |
| 1018 |
} |
| 1019 |
|
| 1020 |
/* BODY is a validated and massaged definition of a constexpr |
| 1021 |
function. Register it in the hash table. */ |
| 1022 |
|
| 1023 |
void |
| 1024 |
register_constexpr_fundef (const constexpr_fundef &value) |
| 1025 |
{ |
| 1026 |
/* Create the constexpr function table if necessary. */ |
| 1027 |
if (constexpr_fundef_table == NULL) |
| 1028 |
constexpr_fundef_table |
| 1029 |
= hash_table<constexpr_fundef_hasher>::create_ggc (101); |
| 1030 |
|
| 1031 |
constexpr_fundef **slot = constexpr_fundef_table->find_slot |
| 1032 |
(const_cast<constexpr_fundef *> (&value), INSERT); |
| 1033 |
|
| 1034 |
gcc_assert (*slot == NULL); |
| 1035 |
*slot = ggc_alloc<constexpr_fundef> (); |
| 1036 |
**slot = value; |
| 1037 |
} |
| 1038 |
|
| 1039 |
/* FUN is a non-constexpr (or, with -Wno-invalid-constexpr, a constexpr |
| 1040 |
function called in a context that requires a constant expression). |
| 1041 |
If it comes from a constexpr template, explain why the instantiation |
| 1042 |
isn't constexpr. Otherwise, explain why the function cannot be used |
| 1043 |
in a constexpr context. */ |
| 1044 |
|
| 1045 |
void |
| 1046 |
explain_invalid_constexpr_fn (tree fun) |
| 1047 |
{ |
| 1048 |
static hash_set<tree> *diagnosed; |
| 1049 |
tree body; |
| 1050 |
|
| 1051 |
/* Don't try to explain a function we already complained about. */ |
| 1052 |
if (function *f = DECL_STRUCT_FUNCTION (fun)) |
| 1053 |
if (f->language->erroneous) |
| 1054 |
return; |
| 1055 |
|
| 1056 |
/* In C++23, a function marked 'constexpr' may not actually be a constant |
| 1057 |
expression. We haven't diagnosed the problem yet: -Winvalid-constexpr |
| 1058 |
wasn't enabled. The function was called, so diagnose why it cannot be |
| 1059 |
used in a constant expression. */ |
| 1060 |
if (warn_invalid_constexpr == 0 && DECL_DECLARED_CONSTEXPR_P (fun)) |
| 1061 |
/* Go on. */; |
| 1062 |
/* Only diagnose defaulted functions, lambdas, or instantiations. */ |
| 1063 |
else if (!DECL_DEFAULTED_FN (fun) |
| 1064 |
&& !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) |
| 1065 |
&& !(flag_implicit_constexpr |
| 1066 |
&& !DECL_DECLARED_CONSTEXPR_P (fun) |
| 1067 |
&& DECL_DECLARED_INLINE_P (fun)) |
| 1068 |
&& !is_instantiation_of_constexpr (fun)) |
| 1069 |
{ |
| 1070 |
inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun); |
| 1071 |
if (flag_implicit_constexpr && !maybe_constexpr_fn (fun) |
| 1072 |
&& decl_defined_p (fun)) |
| 1073 |
inform (DECL_SOURCE_LOCATION (fun), |
| 1074 |
"%<-fimplicit-constexpr%> only affects %<inline%> functions"); |
| 1075 |
return; |
| 1076 |
} |
| 1077 |
if (diagnosed == NULL) |
| 1078 |
diagnosed = new hash_set<tree>; |
| 1079 |
if (diagnosed->add (fun)) |
| 1080 |
/* Already explained. */ |
| 1081 |
return; |
| 1082 |
|
| 1083 |
iloc_sentinel ils = input_location; |
| 1084 |
if (!lambda_static_thunk_p (fun)) |
| 1085 |
{ |
| 1086 |
/* Diagnostics should completely ignore the static thunk, so leave |
| 1087 |
input_location set to our caller's location. */ |
| 1088 |
input_location = DECL_SOURCE_LOCATION (fun); |
| 1089 |
inform (input_location, |
| 1090 |
"%qD is not usable as a %<constexpr%> function because:", fun); |
| 1091 |
} |
| 1092 |
/* First check the declaration. */ |
| 1093 |
if (is_valid_constexpr_fn (fun, true)) |
| 1094 |
{ |
| 1095 |
/* Then if it's OK, the body. */ |
| 1096 |
if (!DECL_DECLARED_CONSTEXPR_P (fun) |
| 1097 |
&& DECL_DEFAULTED_FN (fun)) |
| 1098 |
explain_implicit_non_constexpr (fun); |
| 1099 |
else |
| 1100 |
{ |
| 1101 |
if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun)) |
| 1102 |
body = fd->body; |
| 1103 |
else |
| 1104 |
body = DECL_SAVED_TREE (fun); |
| 1105 |
body = massage_constexpr_body (fun, body); |
| 1106 |
require_potential_rvalue_constant_expression (body); |
| 1107 |
if (DECL_CONSTRUCTOR_P (fun)) |
| 1108 |
{ |
| 1109 |
cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true); |
| 1110 |
if (cxx_dialect > cxx11) |
| 1111 |
{ |
| 1112 |
/* Also check the body, not just the ctor-initializer. */ |
| 1113 |
body = DECL_SAVED_TREE (fun); |
| 1114 |
require_potential_rvalue_constant_expression (body); |
| 1115 |
} |
| 1116 |
} |
| 1117 |
else if (body == NULL_TREE || body == error_mark_node) |
| 1118 |
error ("body of %<constexpr%> function %qD not a return-statement", |
| 1119 |
fun); |
| 1120 |
} |
| 1121 |
} |
| 1122 |
} |
| 1123 |
|
| 1124 |
/* Objects of this type represent calls to constexpr functions |
| 1125 |
along with the bindings of parameters to their arguments, for |
| 1126 |
the purpose of compile time evaluation. */ |
| 1127 |
|
| 1128 |
struct GTY((for_user)) constexpr_call { |
| 1129 |
/* Description of the constexpr function definition. */ |
| 1130 |
constexpr_fundef *fundef = nullptr; |
| 1131 |
/* Parameter bindings environment. A TREE_VEC of arguments. */ |
| 1132 |
tree bindings = NULL_TREE; |
| 1133 |
/* Result of the call, indexed by the value of |
| 1134 |
constexpr_ctx::manifestly_const_eval. |
| 1135 |
unknown_type_node means the call is being evaluated. |
| 1136 |
error_mark_node means that the evaluation was erroneous or otherwise |
| 1137 |
uncacheable (e.g. because it depends on the caller). |
| 1138 |
Otherwise, the actual value of the call. */ |
| 1139 |
tree results[3] = { NULL_TREE, NULL_TREE, NULL_TREE }; |
| 1140 |
/* The hash of this call; we remember it here to avoid having to |
| 1141 |
recalculate it when expanding the hash table. */ |
| 1142 |
hashval_t hash = 0; |
| 1143 |
|
| 1144 |
/* The result slot corresponding to the given mce_value. */ |
| 1145 |
tree& result (mce_value mce) { return results[1 + int(mce)]; } |
| 1146 |
}; |
| 1147 |
|
| 1148 |
struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call> |
| 1149 |
{ |
| 1150 |
static hashval_t hash (constexpr_call *); |
| 1151 |
static bool equal (constexpr_call *, constexpr_call *); |
| 1152 |
}; |
| 1153 |
|
| 1154 |
enum constexpr_switch_state { |
| 1155 |
/* Used when processing a switch for the first time by cxx_eval_switch_expr |
| 1156 |
and default: label for that switch has not been seen yet. */ |
| 1157 |
css_default_not_seen, |
| 1158 |
/* Used when processing a switch for the first time by cxx_eval_switch_expr |
| 1159 |
and default: label for that switch has been seen already. */ |
| 1160 |
css_default_seen, |
| 1161 |
/* Used when processing a switch for the second time by |
| 1162 |
cxx_eval_switch_expr, where default: label should match. */ |
| 1163 |
css_default_processing |
| 1164 |
}; |
| 1165 |
|
| 1166 |
/* The constexpr expansion context part which needs one instance per |
| 1167 |
cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of |
| 1168 |
variables initialized within the expression. */ |
| 1169 |
|
| 1170 |
class constexpr_global_ctx { |
| 1171 |
/* Values for any temporaries or local variables within the |
| 1172 |
constant-expression. Objects outside their lifetime have |
| 1173 |
value 'void_node'. */ |
| 1174 |
hash_map<tree,tree> values; |
| 1175 |
public: |
| 1176 |
/* Number of cxx_eval_constant_expression calls (except skipped ones, |
| 1177 |
on simple constants or location wrappers) encountered during current |
| 1178 |
cxx_eval_outermost_constant_expr call. */ |
| 1179 |
HOST_WIDE_INT constexpr_ops_count; |
| 1180 |
/* Heap VAR_DECLs created during the evaluation of the outermost constant |
| 1181 |
expression. */ |
| 1182 |
auto_vec<tree, 16> heap_vars; |
| 1183 |
/* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */ |
| 1184 |
vec<tree> *cleanups; |
| 1185 |
/* If non-null, only allow modification of existing values of the variables |
| 1186 |
in this set. Set by modifiable_tracker, below. */ |
| 1187 |
hash_set<tree> *modifiable; |
| 1188 |
/* Number of heap VAR_DECL deallocations. */ |
| 1189 |
unsigned heap_dealloc_count; |
| 1190 |
/* Constructor. */ |
| 1191 |
constexpr_global_ctx () |
| 1192 |
: constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr), |
| 1193 |
heap_dealloc_count (0) {} |
| 1194 |
|
| 1195 |
bool is_outside_lifetime (tree t) |
| 1196 |
{ |
| 1197 |
if (tree *p = values.get (t)) |
| 1198 |
if (*p == void_node) |
| 1199 |
return true; |
| 1200 |
return false; |
| 1201 |
} |
| 1202 |
tree get_value (tree t) |
| 1203 |
{ |
| 1204 |
if (tree *p = values.get (t)) |
| 1205 |
if (*p != void_node) |
| 1206 |
return *p; |
| 1207 |
return NULL_TREE; |
| 1208 |
} |
| 1209 |
tree *get_value_ptr (tree t, bool initializing) |
| 1210 |
{ |
| 1211 |
if (modifiable && !modifiable->contains (t)) |
| 1212 |
return nullptr; |
| 1213 |
if (tree *p = values.get (t)) |
| 1214 |
{ |
| 1215 |
if (*p != void_node) |
| 1216 |
return p; |
| 1217 |
else if (initializing) |
| 1218 |
{ |
| 1219 |
*p = NULL_TREE; |
| 1220 |
return p; |
| 1221 |
} |
| 1222 |
} |
| 1223 |
return nullptr; |
| 1224 |
} |
| 1225 |
void put_value (tree t, tree v) |
| 1226 |
{ |
| 1227 |
bool already_in_map = values.put (t, v); |
| 1228 |
if (!already_in_map && modifiable) |
| 1229 |
modifiable->add (t); |
| 1230 |
} |
| 1231 |
void destroy_value (tree t) |
| 1232 |
{ |
| 1233 |
if (TREE_CODE (t) == VAR_DECL |
| 1234 |
|| TREE_CODE (t) == PARM_DECL |
| 1235 |
|| TREE_CODE (t) == RESULT_DECL) |
| 1236 |
values.put (t, void_node); |
| 1237 |
else |
| 1238 |
values.remove (t); |
| 1239 |
} |
| 1240 |
void clear_value (tree t) |
| 1241 |
{ |
| 1242 |
values.remove (t); |
| 1243 |
} |
| 1244 |
}; |
| 1245 |
|
| 1246 |
/* Helper class for constexpr_global_ctx. In some cases we want to avoid |
| 1247 |
side-effects from evaluation of a particular subexpression of a |
| 1248 |
constant-expression. In such cases we use modifiable_tracker to prevent |
| 1249 |
modification of variables created outside of that subexpression. |
| 1250 |
|
| 1251 |
??? We could change the hash_set to a hash_map, allow and track external |
| 1252 |
modifications, and roll them back in the destructor. It's not clear to me |
| 1253 |
that this would be worthwhile. */ |
| 1254 |
|
| 1255 |
class modifiable_tracker |
| 1256 |
{ |
| 1257 |
hash_set<tree> set; |
| 1258 |
constexpr_global_ctx *global; |
| 1259 |
public: |
| 1260 |
modifiable_tracker (constexpr_global_ctx *g): global(g) |
| 1261 |
{ |
| 1262 |
global->modifiable = &set; |
| 1263 |
} |
| 1264 |
~modifiable_tracker () |
| 1265 |
{ |
| 1266 |
for (tree t: set) |
| 1267 |
global->clear_value (t); |
| 1268 |
global->modifiable = nullptr; |
| 1269 |
} |
| 1270 |
}; |
| 1271 |
|
| 1272 |
/* The constexpr expansion context. CALL is the current function |
| 1273 |
expansion, CTOR is the current aggregate initializer, OBJECT is the |
| 1274 |
object being initialized by CTOR, either a VAR_DECL or a _REF. */ |
| 1275 |
|
| 1276 |
struct constexpr_ctx { |
| 1277 |
/* The part of the context that needs to be unique to the whole |
| 1278 |
cxx_eval_outermost_constant_expr invocation. */ |
| 1279 |
constexpr_global_ctx *global; |
| 1280 |
/* The innermost call we're evaluating. */ |
| 1281 |
constexpr_call *call; |
| 1282 |
/* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen |
| 1283 |
within the current LOOP_EXPR. NULL if we aren't inside a loop. */ |
| 1284 |
vec<tree> *save_exprs; |
| 1285 |
/* The CONSTRUCTOR we're currently building up for an aggregate |
| 1286 |
initializer. */ |
| 1287 |
tree ctor; |
| 1288 |
/* The object we're building the CONSTRUCTOR for. */ |
| 1289 |
tree object; |
| 1290 |
/* If inside SWITCH_EXPR. */ |
| 1291 |
constexpr_switch_state *css_state; |
| 1292 |
/* The aggregate initialization context inside which this one is nested. This |
| 1293 |
is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */ |
| 1294 |
const constexpr_ctx *parent; |
| 1295 |
|
| 1296 |
/* Whether we should error on a non-constant expression or fail quietly. |
| 1297 |
This flag needs to be here, but some of the others could move to global |
| 1298 |
if they get larger than a word. */ |
| 1299 |
bool quiet; |
| 1300 |
/* Whether we are strictly conforming to constant expression rules or |
| 1301 |
trying harder to get a constant value. */ |
| 1302 |
bool strict; |
| 1303 |
/* Whether __builtin_is_constant_evaluated () should be true. */ |
| 1304 |
mce_value manifestly_const_eval; |
| 1305 |
}; |
| 1306 |
|
| 1307 |
/* True if the constexpr relaxations afforded by P2280R4 for unknown |
| 1308 |
references and objects are in effect. */ |
| 1309 |
|
| 1310 |
static bool |
| 1311 |
p2280_active_p (const constexpr_ctx *ctx) |
| 1312 |
{ |
| 1313 |
if (ctx->manifestly_const_eval != mce_true) |
| 1314 |
/* Disable these relaxations during speculative constexpr folding, |
| 1315 |
as it can significantly increase compile time/memory use |
| 1316 |
(PR119387). */ |
| 1317 |
return false; |
| 1318 |
|
| 1319 |
/* P2280R4 was accepted as a DR against C++11. */ |
| 1320 |
return cxx_dialect >= cxx11; |
| 1321 |
} |
| 1322 |
|
| 1323 |
/* Remove T from the global values map, checking for attempts to destroy |
| 1324 |
a value that has already finished its lifetime. */ |
| 1325 |
|
| 1326 |
static void |
| 1327 |
destroy_value_checked (const constexpr_ctx* ctx, tree t, bool *non_constant_p) |
| 1328 |
{ |
| 1329 |
if (t == error_mark_node || TREE_TYPE (t) == error_mark_node) |
| 1330 |
return; |
| 1331 |
|
| 1332 |
/* Don't error again here if we've already reported a problem. */ |
| 1333 |
if (!*non_constant_p |
| 1334 |
&& DECL_P (t) |
| 1335 |
/* Non-trivial destructors have their lifetimes ended explicitly |
| 1336 |
with a clobber, so don't worry about it here. */ |
| 1337 |
&& (!TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t)) |
| 1338 |
/* ...except parameters are remapped in cxx_eval_call_expression, |
| 1339 |
and the destructor call during cleanup won't be able to tell that |
| 1340 |
this value has already been destroyed, so complain now. This is |
| 1341 |
not quite unobservable, but is extremely unlikely to crop up in |
| 1342 |
practice; see g++.dg/cpp2a/constexpr-lifetime2.C. */ |
| 1343 |
|| TREE_CODE (t) == PARM_DECL) |
| 1344 |
&& ctx->global->is_outside_lifetime (t)) |
| 1345 |
{ |
| 1346 |
if (!ctx->quiet) |
| 1347 |
{ |
| 1348 |
auto_diagnostic_group d; |
| 1349 |
error ("destroying %qE outside its lifetime", t); |
| 1350 |
inform (DECL_SOURCE_LOCATION (t), "declared here"); |
| 1351 |
} |
| 1352 |
*non_constant_p = true; |
| 1353 |
} |
| 1354 |
ctx->global->destroy_value (t); |
| 1355 |
} |
| 1356 |
|
| 1357 |
/* This internal flag controls whether we should avoid doing anything during |
| 1358 |
constexpr evaluation that would cause extra DECL_UID generation, such as |
| 1359 |
template instantiation and function body copying. */ |
| 1360 |
|
| 1361 |
static bool uid_sensitive_constexpr_evaluation_value; |
| 1362 |
|
| 1363 |
/* An internal counter that keeps track of the number of times |
| 1364 |
uid_sensitive_constexpr_evaluation_p returned true. */ |
| 1365 |
|
| 1366 |
static unsigned uid_sensitive_constexpr_evaluation_true_counter; |
| 1367 |
|
| 1368 |
/* The accessor for uid_sensitive_constexpr_evaluation_value which also |
| 1369 |
increments the corresponding counter. */ |
| 1370 |
|
| 1371 |
static bool |
| 1372 |
uid_sensitive_constexpr_evaluation_p () |
| 1373 |
{ |
| 1374 |
if (uid_sensitive_constexpr_evaluation_value) |
| 1375 |
{ |
| 1376 |
++uid_sensitive_constexpr_evaluation_true_counter; |
| 1377 |
return true; |
| 1378 |
} |
| 1379 |
else |
| 1380 |
return false; |
| 1381 |
} |
| 1382 |
|
| 1383 |
/* The default constructor for uid_sensitive_constexpr_evaluation_sentinel |
| 1384 |
enables the internal flag for uid_sensitive_constexpr_evaluation_p |
| 1385 |
during the lifetime of the sentinel object. Upon its destruction, the |
| 1386 |
previous value of uid_sensitive_constexpr_evaluation_p is restored. */ |
| 1387 |
|
| 1388 |
uid_sensitive_constexpr_evaluation_sentinel |
| 1389 |
::uid_sensitive_constexpr_evaluation_sentinel () |
| 1390 |
: ovr (uid_sensitive_constexpr_evaluation_value, true) |
| 1391 |
{ |
| 1392 |
} |
| 1393 |
|
| 1394 |
/* The default constructor for uid_sensitive_constexpr_evaluation_checker |
| 1395 |
records the current number of times that uid_sensitive_constexpr_evaluation_p |
| 1396 |
has been called and returned true. */ |
| 1397 |
|
| 1398 |
uid_sensitive_constexpr_evaluation_checker |
| 1399 |
::uid_sensitive_constexpr_evaluation_checker () |
| 1400 |
: saved_counter (uid_sensitive_constexpr_evaluation_true_counter) |
| 1401 |
{ |
| 1402 |
} |
| 1403 |
|
| 1404 |
/* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and |
| 1405 |
some constexpr evaluation was restricted due to u_s_c_e_p being called |
| 1406 |
and returning true during the lifetime of this checker object. */ |
| 1407 |
|
| 1408 |
bool |
| 1409 |
uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const |
| 1410 |
{ |
| 1411 |
return (uid_sensitive_constexpr_evaluation_value |
| 1412 |
&& saved_counter != uid_sensitive_constexpr_evaluation_true_counter); |
| 1413 |
} |
| 1414 |
|
| 1415 |
|
| 1416 |
/* A table of all constexpr calls that have been evaluated by the |
| 1417 |
compiler in this translation unit. */ |
| 1418 |
|
| 1419 |
static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table; |
| 1420 |
|
| 1421 |
/* Compute a hash value for a constexpr call representation. */ |
| 1422 |
|
| 1423 |
inline hashval_t |
| 1424 |
constexpr_call_hasher::hash (constexpr_call *info) |
| 1425 |
{ |
| 1426 |
return info->hash; |
| 1427 |
} |
| 1428 |
|
| 1429 |
/* Return true if the objects pointed to by P and Q represent calls |
| 1430 |
to the same constexpr function with the same arguments. |
| 1431 |
Otherwise, return false. */ |
| 1432 |
|
| 1433 |
bool |
| 1434 |
constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs) |
| 1435 |
{ |
| 1436 |
if (lhs == rhs) |
| 1437 |
return true; |
| 1438 |
if (lhs->hash != rhs->hash) |
| 1439 |
return false; |
| 1440 |
if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef)) |
| 1441 |
return false; |
| 1442 |
return cp_tree_equal (lhs->bindings, rhs->bindings); |
| 1443 |
} |
| 1444 |
|
| 1445 |
/* Initialize the constexpr call table, if needed. */ |
| 1446 |
|
| 1447 |
static void |
| 1448 |
maybe_initialize_constexpr_call_table (void) |
| 1449 |
{ |
| 1450 |
if (constexpr_call_table == NULL) |
| 1451 |
constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101); |
| 1452 |
} |
| 1453 |
|
| 1454 |
/* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when |
| 1455 |
a function happens to get called recursively, we unshare the callee |
| 1456 |
function's body and evaluate this unshared copy instead of evaluating the |
| 1457 |
original body. |
| 1458 |
|
| 1459 |
FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function |
| 1460 |
copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map |
| 1461 |
that's keyed off of the original FUNCTION_DECL and whose value is a |
| 1462 |
TREE_LIST of this function's unused copies awaiting reuse. |
| 1463 |
|
| 1464 |
This is not GC-deletable to avoid GC affecting UID generation. */ |
| 1465 |
|
| 1466 |
static GTY(()) decl_tree_map *fundef_copies_table; |
| 1467 |
|
| 1468 |
/* Reuse a copy or create a new unshared copy of the function FUN. |
| 1469 |
Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE |
| 1470 |
is parms, TYPE is result. */ |
| 1471 |
|
| 1472 |
static tree |
| 1473 |
get_fundef_copy (constexpr_fundef *fundef) |
| 1474 |
{ |
| 1475 |
tree copy; |
| 1476 |
bool existed; |
| 1477 |
tree *slot = &(hash_map_safe_get_or_insert<hm_ggc> |
| 1478 |
(fundef_copies_table, fundef->decl, &existed, 127)); |
| 1479 |
|
| 1480 |
if (!existed) |
| 1481 |
{ |
| 1482 |
/* There is no cached function available, or in use. We can use |
| 1483 |
the function directly. That the slot is now created records |
| 1484 |
that this function is now in use. */ |
| 1485 |
copy = build_tree_list (fundef->body, fundef->parms); |
| 1486 |
TREE_TYPE (copy) = fundef->result; |
| 1487 |
} |
| 1488 |
else if (*slot == NULL_TREE) |
| 1489 |
{ |
| 1490 |
if (uid_sensitive_constexpr_evaluation_p ()) |
| 1491 |
return NULL_TREE; |
| 1492 |
|
| 1493 |
/* We've already used the function itself, so make a copy. */ |
| 1494 |
copy = build_tree_list (NULL, NULL); |
| 1495 |
tree saved_body = DECL_SAVED_TREE (fundef->decl); |
| 1496 |
tree saved_parms = DECL_ARGUMENTS (fundef->decl); |
| 1497 |
tree saved_result = DECL_RESULT (fundef->decl); |
| 1498 |
tree saved_fn = current_function_decl; |
| 1499 |
DECL_SAVED_TREE (fundef->decl) = fundef->body; |
| 1500 |
DECL_ARGUMENTS (fundef->decl) = fundef->parms; |
| 1501 |
DECL_RESULT (fundef->decl) = fundef->result; |
| 1502 |
current_function_decl = fundef->decl; |
| 1503 |
TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy), |
| 1504 |
TREE_TYPE (copy)); |
| 1505 |
current_function_decl = saved_fn; |
| 1506 |
DECL_RESULT (fundef->decl) = saved_result; |
| 1507 |
DECL_ARGUMENTS (fundef->decl) = saved_parms; |
| 1508 |
DECL_SAVED_TREE (fundef->decl) = saved_body; |
| 1509 |
} |
| 1510 |
else |
| 1511 |
{ |
| 1512 |
/* We have a cached function available. */ |
| 1513 |
copy = *slot; |
| 1514 |
*slot = TREE_CHAIN (copy); |
| 1515 |
} |
| 1516 |
|
| 1517 |
return copy; |
| 1518 |
} |
| 1519 |
|
| 1520 |
/* Save the copy COPY of function FUN for later reuse by |
| 1521 |
get_fundef_copy(). By construction, there will always be an entry |
| 1522 |
to find. */ |
| 1523 |
|
| 1524 |
static void |
| 1525 |
save_fundef_copy (tree fun, tree copy) |
| 1526 |
{ |
| 1527 |
tree *slot = fundef_copies_table->get (fun); |
| 1528 |
TREE_CHAIN (copy) = *slot; |
| 1529 |
*slot = copy; |
| 1530 |
} |
| 1531 |
|
| 1532 |
/* Whether our evaluation wants a prvalue (e.g. CONSTRUCTOR or _CST), |
| 1533 |
a glvalue (e.g. VAR_DECL or _REF), or nothing. */ |
| 1534 |
|
| 1535 |
enum value_cat { |
| 1536 |
vc_prvalue = 0, |
| 1537 |
vc_glvalue = 1, |
| 1538 |
vc_discard = 2 |
| 1539 |
}; |
| 1540 |
|
| 1541 |
static tree cxx_eval_constant_expression (const constexpr_ctx *, tree, |
| 1542 |
value_cat, bool *, bool *, tree * = NULL); |
| 1543 |
static tree cxx_eval_bare_aggregate (const constexpr_ctx *, tree, |
| 1544 |
value_cat, bool *, bool *); |
| 1545 |
static tree cxx_fold_indirect_ref (const constexpr_ctx *, location_t, tree, tree, |
| 1546 |
bool * = NULL); |
| 1547 |
static tree find_heap_var_refs (tree *, int *, void *); |
| 1548 |
static tree find_deleted_heap_var (tree *, int *, void *); |
| 1549 |
|
| 1550 |
/* Attempt to evaluate T which represents a call to a builtin function. |
| 1551 |
We assume here that all builtin functions evaluate to scalar types |
| 1552 |
represented by _CST nodes. */ |
| 1553 |
|
| 1554 |
static tree |
| 1555 |
cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun, |
| 1556 |
value_cat lval, |
| 1557 |
bool *non_constant_p, bool *overflow_p) |
| 1558 |
{ |
| 1559 |
const int nargs = call_expr_nargs (t); |
| 1560 |
tree *args = (tree *) alloca (nargs * sizeof (tree)); |
| 1561 |
tree new_call; |
| 1562 |
int i; |
| 1563 |
|
| 1564 |
/* Don't fold __builtin_constant_p within a constexpr function. */ |
| 1565 |
bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun); |
| 1566 |
|
| 1567 |
/* If we aren't requiring a constant expression, defer __builtin_constant_p |
| 1568 |
in a constexpr function until we have values for the parameters. */ |
| 1569 |
if (bi_const_p |
| 1570 |
&& ctx->manifestly_const_eval != mce_true |
| 1571 |
&& current_function_decl |
| 1572 |
&& DECL_DECLARED_CONSTEXPR_P (current_function_decl)) |
| 1573 |
{ |
| 1574 |
*non_constant_p = true; |
| 1575 |
return t; |
| 1576 |
} |
| 1577 |
|
| 1578 |
/* For __builtin_is_constant_evaluated, defer it if not |
| 1579 |
ctx->manifestly_const_eval (as sometimes we try to constant evaluate |
| 1580 |
without manifestly_const_eval even expressions or parts thereof which |
| 1581 |
will later be manifestly const_eval evaluated), otherwise fold it to |
| 1582 |
true. */ |
| 1583 |
if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED, |
| 1584 |
BUILT_IN_FRONTEND)) |
| 1585 |
{ |
| 1586 |
if (ctx->manifestly_const_eval == mce_unknown) |
| 1587 |
{ |
| 1588 |
*non_constant_p = true; |
| 1589 |
return t; |
| 1590 |
} |
| 1591 |
return constant_boolean_node (ctx->manifestly_const_eval == mce_true, |
| 1592 |
boolean_type_node); |
| 1593 |
} |
| 1594 |
|
| 1595 |
if (fndecl_built_in_p (fun, CP_BUILT_IN_SOURCE_LOCATION, BUILT_IN_FRONTEND)) |
| 1596 |
{ |
| 1597 |
temp_override<tree> ovr (current_function_decl); |
| 1598 |
if (ctx->call && ctx->call->fundef) |
| 1599 |
current_function_decl = ctx->call->fundef->decl; |
| 1600 |
return fold_builtin_source_location (t); |
| 1601 |
} |
| 1602 |
|
| 1603 |
int strops = 0; |
| 1604 |
int strret = 0; |
| 1605 |
if (fndecl_built_in_p (fun, BUILT_IN_NORMAL)) |
| 1606 |
switch (DECL_FUNCTION_CODE (fun)) |
| 1607 |
{ |
| 1608 |
case BUILT_IN_STRLEN: |
| 1609 |
case BUILT_IN_STRNLEN: |
| 1610 |
strops = 1; |
| 1611 |
break; |
| 1612 |
case BUILT_IN_MEMCHR: |
| 1613 |
case BUILT_IN_STRCHR: |
| 1614 |
case BUILT_IN_STRRCHR: |
| 1615 |
strops = 1; |
| 1616 |
strret = 1; |
| 1617 |
break; |
| 1618 |
case BUILT_IN_MEMCMP: |
| 1619 |
case BUILT_IN_STRCMP: |
| 1620 |
strops = 2; |
| 1621 |
break; |
| 1622 |
case BUILT_IN_STRSTR: |
| 1623 |
strops = 2; |
| 1624 |
strret = 1; |
| 1625 |
break; |
| 1626 |
case BUILT_IN_ASAN_POINTER_COMPARE: |
| 1627 |
case BUILT_IN_ASAN_POINTER_SUBTRACT: |
| 1628 |
/* These builtins shall be ignored during constant expression |
| 1629 |
evaluation. */ |
| 1630 |
return void_node; |
| 1631 |
case BUILT_IN_UNREACHABLE: |
| 1632 |
case BUILT_IN_TRAP: |
| 1633 |
if (!*non_constant_p && !ctx->quiet) |
| 1634 |
{ |
| 1635 |
/* Do not allow__builtin_unreachable in constexpr function. |
| 1636 |
The __builtin_unreachable call with BUILTINS_LOCATION |
| 1637 |
comes from cp_maybe_instrument_return. */ |
| 1638 |
if (EXPR_LOCATION (t) == BUILTINS_LOCATION) |
| 1639 |
error ("%<constexpr%> call flows off the end of the function"); |
| 1640 |
else |
| 1641 |
error ("%q+E is not a constant expression", t); |
| 1642 |
} |
| 1643 |
*non_constant_p = true; |
| 1644 |
return t; |
| 1645 |
default: |
| 1646 |
break; |
| 1647 |
} |
| 1648 |
|
| 1649 |
/* Be permissive for arguments to built-ins; __builtin_constant_p should |
| 1650 |
return constant false for a non-constant argument. */ |
| 1651 |
constexpr_ctx new_ctx = *ctx; |
| 1652 |
new_ctx.quiet = true; |
| 1653 |
for (i = 0; i < nargs; ++i) |
| 1654 |
{ |
| 1655 |
tree arg = CALL_EXPR_ARG (t, i); |
| 1656 |
tree oarg = arg; |
| 1657 |
|
| 1658 |
/* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since |
| 1659 |
expand_builtin doesn't know how to look in the values table. */ |
| 1660 |
bool strop = i < strops; |
| 1661 |
if (strop) |
| 1662 |
{ |
| 1663 |
STRIP_NOPS (arg); |
| 1664 |
if (TREE_CODE (arg) == ADDR_EXPR) |
| 1665 |
arg = TREE_OPERAND (arg, 0); |
| 1666 |
else |
| 1667 |
strop = false; |
| 1668 |
} |
| 1669 |
|
| 1670 |
/* If builtin_valid_in_constant_expr_p is true, |
| 1671 |
potential_constant_expression_1 has not recursed into the arguments |
| 1672 |
of the builtin, verify it here. */ |
| 1673 |
if (!builtin_valid_in_constant_expr_p (fun) |
| 1674 |
|| potential_constant_expression (arg)) |
| 1675 |
{ |
| 1676 |
bool dummy1 = false, dummy2 = false; |
| 1677 |
arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue, |
| 1678 |
&dummy1, &dummy2); |
| 1679 |
} |
| 1680 |
|
| 1681 |
if (bi_const_p) |
| 1682 |
/* For __builtin_constant_p, fold all expressions with constant values |
| 1683 |
even if they aren't C++ constant-expressions. */ |
| 1684 |
arg = cp_fold_rvalue (arg); |
| 1685 |
else if (strop) |
| 1686 |
{ |
| 1687 |
if (TREE_CODE (arg) == CONSTRUCTOR) |
| 1688 |
arg = braced_lists_to_strings (TREE_TYPE (arg), arg); |
| 1689 |
if (TREE_CODE (arg) == STRING_CST) |
| 1690 |
arg = build_address (arg); |
| 1691 |
else |
| 1692 |
arg = oarg; |
| 1693 |
} |
| 1694 |
|
| 1695 |
args[i] = arg; |
| 1696 |
} |
| 1697 |
|
| 1698 |
bool save_ffbcp = force_folding_builtin_constant_p; |
| 1699 |
force_folding_builtin_constant_p |= ctx->manifestly_const_eval == mce_true; |
| 1700 |
tree save_cur_fn = current_function_decl; |
| 1701 |
/* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */ |
| 1702 |
if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION) |
| 1703 |
&& ctx->call |
| 1704 |
&& ctx->call->fundef) |
| 1705 |
current_function_decl = ctx->call->fundef->decl; |
| 1706 |
if (fndecl_built_in_p (fun, |
| 1707 |
CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS, |
| 1708 |
BUILT_IN_FRONTEND)) |
| 1709 |
{ |
| 1710 |
location_t loc = EXPR_LOCATION (t); |
| 1711 |
if (nargs >= 1) |
| 1712 |
VERIFY_CONSTANT (args[0]); |
| 1713 |
new_call |
| 1714 |
= fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs, |
| 1715 |
args); |
| 1716 |
} |
| 1717 |
else if (fndecl_built_in_p (fun, |
| 1718 |
CP_BUILT_IN_IS_CORRESPONDING_MEMBER, |
| 1719 |
BUILT_IN_FRONTEND)) |
| 1720 |
{ |
| 1721 |
location_t loc = EXPR_LOCATION (t); |
| 1722 |
if (nargs >= 2) |
| 1723 |
{ |
| 1724 |
VERIFY_CONSTANT (args[0]); |
| 1725 |
VERIFY_CONSTANT (args[1]); |
| 1726 |
} |
| 1727 |
new_call = fold_builtin_is_corresponding_member (loc, nargs, args); |
| 1728 |
} |
| 1729 |
else |
| 1730 |
new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t), |
| 1731 |
CALL_EXPR_FN (t), nargs, args); |
| 1732 |
current_function_decl = save_cur_fn; |
| 1733 |
force_folding_builtin_constant_p = save_ffbcp; |
| 1734 |
if (new_call == NULL) |
| 1735 |
{ |
| 1736 |
if (!*non_constant_p && !ctx->quiet) |
| 1737 |
{ |
| 1738 |
new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t), |
| 1739 |
CALL_EXPR_FN (t), nargs, args); |
| 1740 |
error ("%q+E is not a constant expression", new_call); |
| 1741 |
} |
| 1742 |
*non_constant_p = true; |
| 1743 |
return t; |
| 1744 |
} |
| 1745 |
|
| 1746 |
if (!potential_constant_expression (new_call)) |
| 1747 |
{ |
| 1748 |
if (!*non_constant_p && !ctx->quiet) |
| 1749 |
error ("%q+E is not a constant expression", new_call); |
| 1750 |
*non_constant_p = true; |
| 1751 |
return t; |
| 1752 |
} |
| 1753 |
|
| 1754 |
if (strret) |
| 1755 |
{ |
| 1756 |
/* memchr returns a pointer into the first argument, but we replaced the |
| 1757 |
argument above with a STRING_CST; put it back it now. */ |
| 1758 |
tree op = CALL_EXPR_ARG (t, strret-1); |
| 1759 |
STRIP_NOPS (new_call); |
| 1760 |
if (TREE_CODE (new_call) == POINTER_PLUS_EXPR) |
| 1761 |
TREE_OPERAND (new_call, 0) = op; |
| 1762 |
else if (TREE_CODE (new_call) == ADDR_EXPR) |
| 1763 |
new_call = op; |
| 1764 |
} |
| 1765 |
|
| 1766 |
return cxx_eval_constant_expression (&new_ctx, new_call, lval, |
| 1767 |
non_constant_p, overflow_p); |
| 1768 |
} |
| 1769 |
|
| 1770 |
/* TEMP is the constant value of a temporary object of type TYPE. Adjust |
| 1771 |
the type of the value to match. */ |
| 1772 |
|
| 1773 |
static tree |
| 1774 |
adjust_temp_type (tree type, tree temp) |
| 1775 |
{ |
| 1776 |
if (same_type_p (TREE_TYPE (temp), type)) |
| 1777 |
return temp; |
| 1778 |
/* Avoid wrapping an aggregate value in a NOP_EXPR. */ |
| 1779 |
if (TREE_CODE (temp) == CONSTRUCTOR) |
| 1780 |
{ |
| 1781 |
/* build_constructor wouldn't retain various CONSTRUCTOR flags. */ |
| 1782 |
tree t = copy_node (temp); |
| 1783 |
TREE_TYPE (t) = type; |
| 1784 |
return t; |
| 1785 |
} |
| 1786 |
if (TREE_CODE (temp) == EMPTY_CLASS_EXPR) |
| 1787 |
return build0 (EMPTY_CLASS_EXPR, type); |
| 1788 |
gcc_assert (scalarish_type_p (type)); |
| 1789 |
/* Now we know we're dealing with a scalar, and a prvalue of non-class |
| 1790 |
type is cv-unqualified. */ |
| 1791 |
return cp_fold_convert (cv_unqualified (type), temp); |
| 1792 |
} |
| 1793 |
|
| 1794 |
/* If T is a CONSTRUCTOR, return an unshared copy of T and any |
| 1795 |
sub-CONSTRUCTORs. Otherwise return T. |
| 1796 |
|
| 1797 |
We use this whenever we initialize an object as a whole, whether it's a |
| 1798 |
parameter, a local variable, or a subobject, so that subsequent |
| 1799 |
modifications don't affect other places where it was used. */ |
| 1800 |
|
| 1801 |
tree |
| 1802 |
unshare_constructor (tree t MEM_STAT_DECL) |
| 1803 |
{ |
| 1804 |
if (!t || TREE_CODE (t) != CONSTRUCTOR) |
| 1805 |
return t; |
| 1806 |
auto_vec <tree*, 4> ptrs; |
| 1807 |
ptrs.safe_push (&t); |
| 1808 |
while (!ptrs.is_empty ()) |
| 1809 |
{ |
| 1810 |
tree *p = ptrs.pop (); |
| 1811 |
tree n = copy_node (*p PASS_MEM_STAT); |
| 1812 |
CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT); |
| 1813 |
*p = n; |
| 1814 |
vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n); |
| 1815 |
constructor_elt *ce; |
| 1816 |
for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i) |
| 1817 |
if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR) |
| 1818 |
ptrs.safe_push (&ce->value); |
| 1819 |
} |
| 1820 |
return t; |
| 1821 |
} |
| 1822 |
|
| 1823 |
/* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */ |
| 1824 |
|
| 1825 |
static void |
| 1826 |
free_constructor (tree t) |
| 1827 |
{ |
| 1828 |
if (!t || TREE_CODE (t) != CONSTRUCTOR) |
| 1829 |
return; |
| 1830 |
releasing_vec ctors; |
| 1831 |
vec_safe_push (ctors, t); |
| 1832 |
while (!ctors->is_empty ()) |
| 1833 |
{ |
| 1834 |
tree c = ctors->pop (); |
| 1835 |
if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c)) |
| 1836 |
{ |
| 1837 |
constructor_elt *ce; |
| 1838 |
for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i) |
| 1839 |
if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR) |
| 1840 |
vec_safe_push (ctors, ce->value); |
| 1841 |
ggc_free (elts); |
| 1842 |
} |
| 1843 |
ggc_free (c); |
| 1844 |
} |
| 1845 |
} |
| 1846 |
|
| 1847 |
/* Helper function of cxx_bind_parameters_in_call. Return non-NULL |
| 1848 |
if *TP is address of a static variable (or part of it) currently being |
| 1849 |
constructed or of a heap artificial variable. */ |
| 1850 |
|
| 1851 |
static tree |
| 1852 |
addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data) |
| 1853 |
{ |
| 1854 |
if (TREE_CODE (*tp) == ADDR_EXPR) |
| 1855 |
if (tree var = get_base_address (TREE_OPERAND (*tp, 0))) |
| 1856 |
if (VAR_P (var) && TREE_STATIC (var)) |
| 1857 |
{ |
| 1858 |
if (DECL_NAME (var) == heap_uninit_identifier |
| 1859 |
|| DECL_NAME (var) == heap_identifier |
| 1860 |
|| DECL_NAME (var) == heap_vec_uninit_identifier |
| 1861 |
|| DECL_NAME (var) == heap_vec_identifier) |
| 1862 |
return var; |
| 1863 |
|
| 1864 |
constexpr_global_ctx *global = (constexpr_global_ctx *) data; |
| 1865 |
if (global->get_value (var)) |
| 1866 |
return var; |
| 1867 |
} |
| 1868 |
if (TYPE_P (*tp)) |
| 1869 |
*walk_subtrees = false; |
| 1870 |
return NULL_TREE; |
| 1871 |
} |
| 1872 |
|
| 1873 |
/* Subroutine of cxx_eval_call_expression. |
| 1874 |
We are processing a call expression (either CALL_EXPR or |
| 1875 |
AGGR_INIT_EXPR) in the context of CTX. Evaluate |
| 1876 |
all arguments and bind their values to correspondings |
| 1877 |
parameters, making up the NEW_CALL context. */ |
| 1878 |
|
| 1879 |
static tree |
| 1880 |
cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun, |
| 1881 |
bool *non_constant_p, bool *overflow_p, |
| 1882 |
bool *non_constant_args) |
| 1883 |
{ |
| 1884 |
const int nargs = call_expr_nargs (t); |
| 1885 |
tree parms = DECL_ARGUMENTS (fun); |
| 1886 |
int i; |
| 1887 |
/* We don't record ellipsis args below. */ |
| 1888 |
int nparms = list_length (parms); |
| 1889 |
int nbinds = nargs < nparms ? nargs : nparms; |
| 1890 |
tree binds = make_tree_vec (nbinds); |
| 1891 |
|
| 1892 |
/* The call is not a constant expression if it involves the cdtor for a type |
| 1893 |
with virtual bases. */ |
| 1894 |
if (DECL_HAS_IN_CHARGE_PARM_P (fun) || DECL_HAS_VTT_PARM_P (fun)) |
| 1895 |
{ |
| 1896 |
if (!ctx->quiet) |
| 1897 |
{ |
| 1898 |
error_at (cp_expr_loc_or_input_loc (t), |
| 1899 |
"call to non-%<constexpr%> function %qD", fun); |
| 1900 |
explain_invalid_constexpr_fn (fun); |
| 1901 |
} |
| 1902 |
*non_constant_p = true; |
| 1903 |
return binds; |
| 1904 |
} |
| 1905 |
|
| 1906 |
for (i = 0; i < nargs; ++i) |
| 1907 |
{ |
| 1908 |
tree x, arg; |
| 1909 |
tree type = parms ? TREE_TYPE (parms) : void_type_node; |
| 1910 |
if (parms && DECL_BY_REFERENCE (parms)) |
| 1911 |
type = TREE_TYPE (type); |
| 1912 |
x = get_nth_callarg (t, i); |
| 1913 |
/* For member function, the first argument is a pointer to the implied |
| 1914 |
object. For a constructor, it might still be a dummy object, in |
| 1915 |
which case we get the real argument from ctx. */ |
| 1916 |
if (i == 0 && DECL_CONSTRUCTOR_P (fun) |
| 1917 |
&& is_dummy_object (x)) |
| 1918 |
{ |
| 1919 |
x = ctx->object; |
| 1920 |
x = build_address (x); |
| 1921 |
} |
| 1922 |
if (TREE_ADDRESSABLE (type)) |
| 1923 |
{ |
| 1924 |
/* Undo convert_for_arg_passing work here. */ |
| 1925 |
x = convert_from_reference (x); |
| 1926 |
arg = cxx_eval_constant_expression (ctx, x, vc_glvalue, |
| 1927 |
non_constant_p, overflow_p); |
| 1928 |
} |
| 1929 |
else |
| 1930 |
/* Normally we would strip a TARGET_EXPR in an initialization context |
| 1931 |
such as this, but here we do the elision differently: we keep the |
| 1932 |
TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */ |
| 1933 |
arg = cxx_eval_constant_expression (ctx, x, vc_prvalue, |
| 1934 |
non_constant_p, overflow_p); |
| 1935 |
/* Check we aren't dereferencing a null pointer when calling a non-static |
| 1936 |
member function, which is undefined behaviour. */ |
| 1937 |
if (i == 0 && DECL_OBJECT_MEMBER_FUNCTION_P (fun) |
| 1938 |
&& integer_zerop (arg) |
| 1939 |
/* But ignore calls from within compiler-generated code, to handle |
| 1940 |
cases like lambda function pointer conversion operator thunks |
| 1941 |
which pass NULL as the 'this' pointer. */ |
| 1942 |
&& !(TREE_CODE (t) == CALL_EXPR && CALL_FROM_THUNK_P (t))) |
| 1943 |
{ |
| 1944 |
if (!ctx->quiet) |
| 1945 |
error_at (cp_expr_loc_or_input_loc (x), |
| 1946 |
"dereferencing a null pointer"); |
| 1947 |
*non_constant_p = true; |
| 1948 |
} |
| 1949 |
/* Don't VERIFY_CONSTANT here. */ |
| 1950 |
if (*non_constant_p && ctx->quiet) |
| 1951 |
break; |
| 1952 |
/* Just discard ellipsis args after checking their constantitude. */ |
| 1953 |
if (!parms) |
| 1954 |
continue; |
| 1955 |
|
| 1956 |
if (!*non_constant_p) |
| 1957 |
{ |
| 1958 |
/* Make sure the binding has the same type as the parm. But |
| 1959 |
only for constant args. */ |
| 1960 |
if (TREE_ADDRESSABLE (type)) |
| 1961 |
{ |
| 1962 |
if (!same_type_p (type, TREE_TYPE (arg))) |
| 1963 |
{ |
| 1964 |
arg = build_fold_addr_expr (arg); |
| 1965 |
arg = cp_fold_convert (build_reference_type (type), arg); |
| 1966 |
arg = convert_from_reference (arg); |
| 1967 |
} |
| 1968 |
} |
| 1969 |
else if (!TYPE_REF_P (type)) |
| 1970 |
arg = adjust_temp_type (type, arg); |
| 1971 |
if (!TREE_CONSTANT (arg)) |
| 1972 |
*non_constant_args = true; |
| 1973 |
else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
| 1974 |
/* The destructor needs to see any modifications the callee makes |
| 1975 |
to the argument. */ |
| 1976 |
*non_constant_args = true; |
| 1977 |
/* If arg is or contains address of a heap artificial variable or |
| 1978 |
of a static variable being constructed, avoid caching the |
| 1979 |
function call, as those variables might be modified by the |
| 1980 |
function, or might be modified by the callers in between |
| 1981 |
the cached function and just read by the function. */ |
| 1982 |
else if (!*non_constant_args |
| 1983 |
&& cp_walk_tree (&arg, addr_of_non_const_var, ctx->global, |
| 1984 |
NULL)) |
| 1985 |
*non_constant_args = true; |
| 1986 |
|
| 1987 |
/* For virtual calls, adjust the this argument, so that it is |
| 1988 |
the object on which the method is called, rather than |
| 1989 |
one of its bases. */ |
| 1990 |
if (i == 0 && DECL_VIRTUAL_P (fun)) |
| 1991 |
{ |
| 1992 |
tree addr = arg; |
| 1993 |
STRIP_NOPS (addr); |
| 1994 |
if (TREE_CODE (addr) == ADDR_EXPR) |
| 1995 |
{ |
| 1996 |
tree obj = TREE_OPERAND (addr, 0); |
| 1997 |
while (TREE_CODE (obj) == COMPONENT_REF |
| 1998 |
&& DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1)) |
| 1999 |
&& !same_type_ignoring_top_level_qualifiers_p |
| 2000 |
(TREE_TYPE (obj), DECL_CONTEXT (fun))) |
| 2001 |
obj = TREE_OPERAND (obj, 0); |
| 2002 |
if (obj != TREE_OPERAND (addr, 0)) |
| 2003 |
arg = build_fold_addr_expr_with_type (obj, |
| 2004 |
TREE_TYPE (arg)); |
| 2005 |
} |
| 2006 |
} |
| 2007 |
TREE_VEC_ELT (binds, i) = arg; |
| 2008 |
} |
| 2009 |
parms = TREE_CHAIN (parms); |
| 2010 |
} |
| 2011 |
|
| 2012 |
return binds; |
| 2013 |
} |
| 2014 |
|
| 2015 |
/* Variables and functions to manage constexpr call expansion context. |
| 2016 |
These do not need to be marked for PCH or GC. */ |
| 2017 |
|
| 2018 |
/* FIXME remember and print actual constant arguments. */ |
| 2019 |
static vec<tree> call_stack; |
| 2020 |
static int call_stack_tick; |
| 2021 |
static int last_cx_error_tick; |
| 2022 |
|
| 2023 |
static int |
| 2024 |
push_cx_call_context (tree call) |
| 2025 |
{ |
| 2026 |
++call_stack_tick; |
| 2027 |
if (!EXPR_HAS_LOCATION (call)) |
| 2028 |
SET_EXPR_LOCATION (call, input_location); |
| 2029 |
call_stack.safe_push (call); |
| 2030 |
int len = call_stack.length (); |
| 2031 |
if (len > max_constexpr_depth) |
| 2032 |
return false; |
| 2033 |
return len; |
| 2034 |
} |
| 2035 |
|
| 2036 |
static void |
| 2037 |
pop_cx_call_context (void) |
| 2038 |
{ |
| 2039 |
++call_stack_tick; |
| 2040 |
call_stack.pop (); |
| 2041 |
} |
| 2042 |
|
| 2043 |
vec<tree> |
| 2044 |
cx_error_context (void) |
| 2045 |
{ |
| 2046 |
vec<tree> r = vNULL; |
| 2047 |
if (call_stack_tick != last_cx_error_tick |
| 2048 |
&& !call_stack.is_empty ()) |
| 2049 |
r = call_stack; |
| 2050 |
last_cx_error_tick = call_stack_tick; |
| 2051 |
return r; |
| 2052 |
} |
| 2053 |
|
| 2054 |
/* E is an operand of a failed assertion, fold it either with or without |
| 2055 |
constexpr context. */ |
| 2056 |
|
| 2057 |
static tree |
| 2058 |
fold_operand (tree e, const constexpr_ctx *ctx) |
| 2059 |
{ |
| 2060 |
if (ctx) |
| 2061 |
{ |
| 2062 |
bool new_non_constant_p = false, new_overflow_p = false; |
| 2063 |
e = cxx_eval_constant_expression (ctx, e, vc_prvalue, |
| 2064 |
&new_non_constant_p, |
| 2065 |
&new_overflow_p); |
| 2066 |
} |
| 2067 |
else |
| 2068 |
e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true); |
| 2069 |
return e; |
| 2070 |
} |
| 2071 |
|
| 2072 |
/* If we have a condition in conjunctive normal form (CNF), find the first |
| 2073 |
failing clause. In other words, given an expression like |
| 2074 |
|
| 2075 |
true && true && false && true && false |
| 2076 |
|
| 2077 |
return the first 'false'. EXPR is the expression. */ |
| 2078 |
|
| 2079 |
static tree |
| 2080 |
find_failing_clause_r (const constexpr_ctx *ctx, tree expr) |
| 2081 |
{ |
| 2082 |
if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR) |
| 2083 |
{ |
| 2084 |
/* First check the left side... */ |
| 2085 |
tree e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 0)); |
| 2086 |
if (e == NULL_TREE) |
| 2087 |
/* ...if we didn't find a false clause, check the right side. */ |
| 2088 |
e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 1)); |
| 2089 |
return e; |
| 2090 |
} |
| 2091 |
tree e = contextual_conv_bool (expr, tf_none); |
| 2092 |
e = fold_operand (e, ctx); |
| 2093 |
if (integer_zerop (e)) |
| 2094 |
/* This is the failing clause. */ |
| 2095 |
return expr; |
| 2096 |
return NULL_TREE; |
| 2097 |
} |
| 2098 |
|
| 2099 |
/* Wrapper for find_failing_clause_r. */ |
| 2100 |
|
| 2101 |
tree |
| 2102 |
find_failing_clause (const constexpr_ctx *ctx, tree expr) |
| 2103 |
{ |
| 2104 |
if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR) |
| 2105 |
if (tree e = find_failing_clause_r (ctx, expr)) |
| 2106 |
expr = e; |
| 2107 |
return expr; |
| 2108 |
} |
| 2109 |
|
| 2110 |
/* Emit additional diagnostics for failing condition BAD. |
| 2111 |
Used by finish_static_assert and IFN_ASSUME constexpr diagnostics. |
| 2112 |
If SHOW_EXPR_P is true, print the condition (because it was |
| 2113 |
instantiation-dependent). */ |
| 2114 |
|
| 2115 |
void |
| 2116 |
diagnose_failing_condition (tree bad, location_t cloc, bool show_expr_p, |
| 2117 |
const constexpr_ctx *ctx /* = nullptr */) |
| 2118 |
{ |
| 2119 |
/* Nobody wants to see the artificial (bool) cast. */ |
| 2120 |
bad = tree_strip_nop_conversions (bad); |
| 2121 |
if (TREE_CODE (bad) == CLEANUP_POINT_EXPR) |
| 2122 |
bad = TREE_OPERAND (bad, 0); |
| 2123 |
|
| 2124 |
/* Actually explain the failure if this is a concept check or a |
| 2125 |
requires-expression. */ |
| 2126 |
if (concept_check_p (bad) || TREE_CODE (bad) == REQUIRES_EXPR) |
| 2127 |
diagnose_constraints (cloc, bad, NULL_TREE); |
| 2128 |
else if (COMPARISON_CLASS_P (bad) |
| 2129 |
&& ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0)))) |
| 2130 |
{ |
| 2131 |
tree op0 = fold_operand (TREE_OPERAND (bad, 0), ctx); |
| 2132 |
tree op1 = fold_operand (TREE_OPERAND (bad, 1), ctx); |
| 2133 |
tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1); |
| 2134 |
inform (cloc, "the comparison reduces to %qE", cond); |
| 2135 |
} |
| 2136 |
else if (show_expr_p) |
| 2137 |
inform (cloc, "%qE evaluates to false", bad); |
| 2138 |
} |
| 2139 |
|
| 2140 |
/* Process an assert/assume of ORIG_ARG. If it's not supposed to be evaluated, |
| 2141 |
do it without changing the current evaluation state. If it evaluates to |
| 2142 |
false, complain and return false; otherwise, return true. */ |
| 2143 |
|
| 2144 |
static bool |
| 2145 |
cxx_eval_assert (const constexpr_ctx *ctx, tree arg, const char *msg, |
| 2146 |
location_t loc, bool evaluated, |
| 2147 |
bool *non_constant_p, bool *overflow_p) |
| 2148 |
{ |
| 2149 |
if (*non_constant_p) |
| 2150 |
return true; |
| 2151 |
|
| 2152 |
tree eval; |
| 2153 |
if (!evaluated) |
| 2154 |
{ |
| 2155 |
if (!potential_rvalue_constant_expression (arg)) |
| 2156 |
return true; |
| 2157 |
|
| 2158 |
constexpr_ctx new_ctx = *ctx; |
| 2159 |
new_ctx.quiet = true; |
| 2160 |
bool new_non_constant_p = false, new_overflow_p = false; |
| 2161 |
/* Avoid modification of existing values. */ |
| 2162 |
modifiable_tracker ms (new_ctx.global); |
| 2163 |
eval = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue, |
| 2164 |
&new_non_constant_p, |
| 2165 |
&new_overflow_p); |
| 2166 |
} |
| 2167 |
else |
| 2168 |
eval = cxx_eval_constant_expression (ctx, arg, vc_prvalue, |
| 2169 |
non_constant_p, |
| 2170 |
overflow_p); |
| 2171 |
if (!*non_constant_p && integer_zerop (eval)) |
| 2172 |
{ |
| 2173 |
if (!ctx->quiet) |
| 2174 |
{ |
| 2175 |
/* See if we can find which clause was failing |
| 2176 |
(for logical AND). */ |
| 2177 |
tree bad = find_failing_clause (ctx, arg); |
| 2178 |
/* If not, or its location is unusable, fall back to the |
| 2179 |
previous location. */ |
| 2180 |
location_t cloc = cp_expr_loc_or_loc (bad, loc); |
| 2181 |
|
| 2182 |
/* Report the error. */ |
| 2183 |
auto_diagnostic_group d; |
| 2184 |
error_at (cloc, msg); |
| 2185 |
diagnose_failing_condition (bad, cloc, true, ctx); |
| 2186 |
return bad; |
| 2187 |
} |
| 2188 |
*non_constant_p = true; |
| 2189 |
return false; |
| 2190 |
} |
| 2191 |
|
| 2192 |
return true; |
| 2193 |
} |
| 2194 |
|
| 2195 |
/* Evaluate a call T to a GCC internal function when possible and return |
| 2196 |
the evaluated result or, under the control of CTX, give an error, set |
| 2197 |
NON_CONSTANT_P, and return the unevaluated call T otherwise. */ |
| 2198 |
|
| 2199 |
static tree |
| 2200 |
cxx_eval_internal_function (const constexpr_ctx *ctx, tree t, |
| 2201 |
value_cat lval, |
| 2202 |
bool *non_constant_p, bool *overflow_p) |
| 2203 |
{ |
| 2204 |
enum tree_code opcode = ERROR_MARK; |
| 2205 |
|
| 2206 |
switch (CALL_EXPR_IFN (t)) |
| 2207 |
{ |
| 2208 |
case IFN_UBSAN_NULL: |
| 2209 |
case IFN_UBSAN_BOUNDS: |
| 2210 |
case IFN_UBSAN_VPTR: |
| 2211 |
case IFN_FALLTHROUGH: |
| 2212 |
return void_node; |
| 2213 |
|
| 2214 |
case IFN_ASSUME: |
| 2215 |
if (!cxx_eval_assert (ctx, CALL_EXPR_ARG (t, 0), |
| 2216 |
G_("failed %<assume%> attribute assumption"), |
| 2217 |
EXPR_LOCATION (t), /*eval*/false, |
| 2218 |
non_constant_p, overflow_p)) |
| 2219 |
return t; |
| 2220 |
return void_node; |
| 2221 |
|
| 2222 |
case IFN_ADD_OVERFLOW: |
| 2223 |
opcode = PLUS_EXPR; |
| 2224 |
break; |
| 2225 |
case IFN_SUB_OVERFLOW: |
| 2226 |
opcode = MINUS_EXPR; |
| 2227 |
break; |
| 2228 |
case IFN_MUL_OVERFLOW: |
| 2229 |
opcode = MULT_EXPR; |
| 2230 |
break; |
| 2231 |
|
| 2232 |
case IFN_LAUNDER: |
| 2233 |
return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), |
| 2234 |
vc_prvalue, non_constant_p, |
| 2235 |
overflow_p); |
| 2236 |
|
| 2237 |
case IFN_VEC_CONVERT: |
| 2238 |
{ |
| 2239 |
tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), |
| 2240 |
vc_prvalue, non_constant_p, |
| 2241 |
overflow_p); |
| 2242 |
if (TREE_CODE (arg) == VECTOR_CST) |
| 2243 |
if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg)) |
| 2244 |
return r; |
| 2245 |
} |
| 2246 |
/* FALLTHRU */ |
| 2247 |
|
| 2248 |
default: |
| 2249 |
if (!ctx->quiet) |
| 2250 |
error_at (cp_expr_loc_or_input_loc (t), |
| 2251 |
"call to internal function %qE", t); |
| 2252 |
*non_constant_p = true; |
| 2253 |
return t; |
| 2254 |
} |
| 2255 |
|
| 2256 |
/* Evaluate constant arguments using OPCODE and return a complex |
| 2257 |
number containing the result and the overflow bit. */ |
| 2258 |
tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval, |
| 2259 |
non_constant_p, overflow_p); |
| 2260 |
tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval, |
| 2261 |
non_constant_p, overflow_p); |
| 2262 |
|
| 2263 |
if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) |
| 2264 |
{ |
| 2265 |
location_t loc = cp_expr_loc_or_input_loc (t); |
| 2266 |
tree type = TREE_TYPE (TREE_TYPE (t)); |
| 2267 |
tree result = fold_binary_loc (loc, opcode, type, |
| 2268 |
fold_convert_loc (loc, type, arg0), |
| 2269 |
fold_convert_loc (loc, type, arg1)); |
| 2270 |
tree ovf |
| 2271 |
= build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1)); |
| 2272 |
/* Reset TREE_OVERFLOW to avoid warnings for the overflow. */ |
| 2273 |
if (TREE_OVERFLOW (result)) |
| 2274 |
TREE_OVERFLOW (result) = 0; |
| 2275 |
|
| 2276 |
return build_complex (TREE_TYPE (t), result, ovf); |
| 2277 |
} |
| 2278 |
|
| 2279 |
*non_constant_p = true; |
| 2280 |
return t; |
| 2281 |
} |
| 2282 |
|
| 2283 |
/* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */ |
| 2284 |
|
| 2285 |
static void |
| 2286 |
clear_no_implicit_zero (tree ctor) |
| 2287 |
{ |
| 2288 |
if (CONSTRUCTOR_NO_CLEARING (ctor)) |
| 2289 |
{ |
| 2290 |
CONSTRUCTOR_NO_CLEARING (ctor) = false; |
| 2291 |
for (auto &e: CONSTRUCTOR_ELTS (ctor)) |
| 2292 |
if (TREE_CODE (e.value) == CONSTRUCTOR) |
| 2293 |
clear_no_implicit_zero (e.value); |
| 2294 |
} |
| 2295 |
} |
| 2296 |
|
| 2297 |
/* Complain about a const object OBJ being modified in a constant expression. |
| 2298 |
EXPR is the MODIFY_EXPR expression performing the modification. */ |
| 2299 |
|
| 2300 |
static void |
| 2301 |
modifying_const_object_error (tree expr, tree obj) |
| 2302 |
{ |
| 2303 |
location_t loc = cp_expr_loc_or_input_loc (expr); |
| 2304 |
auto_diagnostic_group d; |
| 2305 |
error_at (loc, "modifying a const object %qE is not allowed in " |
| 2306 |
"a constant expression", TREE_OPERAND (expr, 0)); |
| 2307 |
|
| 2308 |
/* Find the underlying object that was declared as const. */ |
| 2309 |
location_t decl_loc = UNKNOWN_LOCATION; |
| 2310 |
for (tree probe = obj; decl_loc == UNKNOWN_LOCATION; ) |
| 2311 |
switch (TREE_CODE (probe)) |
| 2312 |
{ |
| 2313 |
case BIT_FIELD_REF: |
| 2314 |
case COMPONENT_REF: |
| 2315 |
{ |
| 2316 |
tree elt = TREE_OPERAND (probe, 1); |
| 2317 |
if (CP_TYPE_CONST_P (TREE_TYPE (elt))) |
| 2318 |
decl_loc = DECL_SOURCE_LOCATION (elt); |
| 2319 |
probe = TREE_OPERAND (probe, 0); |
| 2320 |
} |
| 2321 |
break; |
| 2322 |
|
| 2323 |
case ARRAY_REF: |
| 2324 |
case REALPART_EXPR: |
| 2325 |
case IMAGPART_EXPR: |
| 2326 |
probe = TREE_OPERAND (probe, 0); |
| 2327 |
break; |
| 2328 |
|
| 2329 |
default: |
| 2330 |
decl_loc = location_of (probe); |
| 2331 |
break; |
| 2332 |
} |
| 2333 |
inform (decl_loc, "originally declared %<const%> here"); |
| 2334 |
} |
| 2335 |
|
| 2336 |
/* Return true if FNDECL is a replaceable global allocation function that |
| 2337 |
should be useable during constant expression evaluation. */ |
| 2338 |
|
| 2339 |
static inline bool |
| 2340 |
cxx_replaceable_global_alloc_fn (tree fndecl) |
| 2341 |
{ |
| 2342 |
return (cxx_dialect >= cxx20 |
| 2343 |
&& IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl)) |
| 2344 |
&& CP_DECL_CONTEXT (fndecl) == global_namespace |
| 2345 |
&& (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl) |
| 2346 |
|| DECL_IS_OPERATOR_DELETE_P (fndecl))); |
| 2347 |
} |
| 2348 |
|
| 2349 |
/* Return true if FNDECL is a placement new function that should be |
| 2350 |
useable during constant expression evaluation of std::construct_at. */ |
| 2351 |
|
| 2352 |
static inline bool |
| 2353 |
cxx_placement_new_fn (tree fndecl) |
| 2354 |
{ |
| 2355 |
return (cxx_dialect >= cxx20 && std_placement_new_fn_p (fndecl)); |
| 2356 |
} |
| 2357 |
|
| 2358 |
/* Return true if FNDECL is std::construct_at. */ |
| 2359 |
|
| 2360 |
static inline bool |
| 2361 |
is_std_construct_at (tree fndecl) |
| 2362 |
{ |
| 2363 |
if (!decl_in_std_namespace_p (fndecl)) |
| 2364 |
return false; |
| 2365 |
|
| 2366 |
tree name = DECL_NAME (fndecl); |
| 2367 |
return name && id_equal (name, "construct_at"); |
| 2368 |
} |
| 2369 |
|
| 2370 |
/* Overload for the above taking constexpr_call*. */ |
| 2371 |
|
| 2372 |
static inline bool |
| 2373 |
is_std_construct_at (const constexpr_call *call) |
| 2374 |
{ |
| 2375 |
return (call |
| 2376 |
&& call->fundef |
| 2377 |
&& is_std_construct_at (call->fundef->decl)); |
| 2378 |
} |
| 2379 |
|
| 2380 |
/* True if CTX is an instance of std::NAME class. */ |
| 2381 |
|
| 2382 |
bool |
| 2383 |
is_std_class (tree ctx, const char *name) |
| 2384 |
{ |
| 2385 |
if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx)) |
| 2386 |
return false; |
| 2387 |
|
| 2388 |
tree decl = TYPE_MAIN_DECL (ctx); |
| 2389 |
tree dname = DECL_NAME (decl); |
| 2390 |
if (dname == NULL_TREE || !id_equal (dname, name)) |
| 2391 |
return false; |
| 2392 |
|
| 2393 |
return decl_in_std_namespace_p (decl); |
| 2394 |
} |
| 2395 |
|
| 2396 |
/* True if CTX is an instance of std::allocator. */ |
| 2397 |
|
| 2398 |
bool |
| 2399 |
is_std_allocator (tree ctx) |
| 2400 |
{ |
| 2401 |
return is_std_class (ctx, "allocator"); |
| 2402 |
} |
| 2403 |
|
| 2404 |
/* Return true if FNDECL is std::allocator<T>::{,de}allocate. */ |
| 2405 |
|
| 2406 |
static inline bool |
| 2407 |
is_std_allocator_allocate (tree fndecl) |
| 2408 |
{ |
| 2409 |
tree name = DECL_NAME (fndecl); |
| 2410 |
if (name == NULL_TREE |
| 2411 |
|| !(id_equal (name, "allocate") || id_equal (name, "deallocate"))) |
| 2412 |
return false; |
| 2413 |
|
| 2414 |
return is_std_allocator (DECL_CONTEXT (fndecl)); |
| 2415 |
} |
| 2416 |
|
| 2417 |
/* Overload for the above taking constexpr_call*. */ |
| 2418 |
|
| 2419 |
static inline bool |
| 2420 |
is_std_allocator_allocate (const constexpr_call *call) |
| 2421 |
{ |
| 2422 |
return (call |
| 2423 |
&& call->fundef |
| 2424 |
&& is_std_allocator_allocate (call->fundef->decl)); |
| 2425 |
} |
| 2426 |
|
| 2427 |
/* Return true if FNDECL is std::source_location::current. */ |
| 2428 |
|
| 2429 |
static inline bool |
| 2430 |
is_std_source_location_current (tree fndecl) |
| 2431 |
{ |
| 2432 |
if (!decl_in_std_namespace_p (fndecl)) |
| 2433 |
return false; |
| 2434 |
|
| 2435 |
tree name = DECL_NAME (fndecl); |
| 2436 |
if (name == NULL_TREE || !id_equal (name, "current")) |
| 2437 |
return false; |
| 2438 |
|
| 2439 |
tree ctx = DECL_CONTEXT (fndecl); |
| 2440 |
if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx)) |
| 2441 |
return false; |
| 2442 |
|
| 2443 |
name = DECL_NAME (TYPE_MAIN_DECL (ctx)); |
| 2444 |
return name && id_equal (name, "source_location"); |
| 2445 |
} |
| 2446 |
|
| 2447 |
/* Overload for the above taking constexpr_call*. */ |
| 2448 |
|
| 2449 |
static inline bool |
| 2450 |
is_std_source_location_current (const constexpr_call *call) |
| 2451 |
{ |
| 2452 |
return (call |
| 2453 |
&& call->fundef |
| 2454 |
&& is_std_source_location_current (call->fundef->decl)); |
| 2455 |
} |
| 2456 |
|
| 2457 |
/* Return true if FNDECL is __dynamic_cast. */ |
| 2458 |
|
| 2459 |
static inline bool |
| 2460 |
cxx_dynamic_cast_fn_p (tree fndecl) |
| 2461 |
{ |
| 2462 |
return (cxx_dialect >= cxx20 |
| 2463 |
&& id_equal (DECL_NAME (fndecl), "__dynamic_cast") |
| 2464 |
&& CP_DECL_CONTEXT (fndecl) == abi_node |
| 2465 |
/* Only consider implementation-detail __dynamic_cast calls that |
| 2466 |
correspond to a dynamic_cast, and ignore direct calls to |
| 2467 |
abi::__dynamic_cast. */ |
| 2468 |
&& DECL_ARTIFICIAL (fndecl)); |
| 2469 |
} |
| 2470 |
|
| 2471 |
/* Often, we have an expression in the form of address + offset, e.g. |
| 2472 |
"&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */ |
| 2473 |
|
| 2474 |
static tree |
| 2475 |
extract_obj_from_addr_offset (tree expr) |
| 2476 |
{ |
| 2477 |
if (TREE_CODE (expr) == POINTER_PLUS_EXPR) |
| 2478 |
expr = TREE_OPERAND (expr, 0); |
| 2479 |
STRIP_NOPS (expr); |
| 2480 |
if (TREE_CODE (expr) == ADDR_EXPR) |
| 2481 |
expr = TREE_OPERAND (expr, 0); |
| 2482 |
return expr; |
| 2483 |
} |
| 2484 |
|
| 2485 |
/* Given a PATH like |
| 2486 |
|
| 2487 |
g.D.2181.D.2154.D.2102.D.2093 |
| 2488 |
|
| 2489 |
find a component with type TYPE. Return NULL_TREE if not found, and |
| 2490 |
error_mark_node if the component is not accessible. If STOP is non-null, |
| 2491 |
this function will return NULL_TREE if STOP is found before TYPE. */ |
| 2492 |
|
| 2493 |
static tree |
| 2494 |
get_component_with_type (tree path, tree type, tree stop) |
| 2495 |
{ |
| 2496 |
while (true) |
| 2497 |
{ |
| 2498 |
if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type)) |
| 2499 |
/* Found it. */ |
| 2500 |
return path; |
| 2501 |
else if (stop |
| 2502 |
&& (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), |
| 2503 |
stop))) |
| 2504 |
return NULL_TREE; |
| 2505 |
else if (TREE_CODE (path) == COMPONENT_REF |
| 2506 |
&& DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1))) |
| 2507 |
{ |
| 2508 |
/* We need to check that the component we're accessing is in fact |
| 2509 |
accessible. */ |
| 2510 |
if (TREE_PRIVATE (TREE_OPERAND (path, 1)) |
| 2511 |
|| TREE_PROTECTED (TREE_OPERAND (path, 1))) |
| 2512 |
return error_mark_node; |
| 2513 |
path = TREE_OPERAND (path, 0); |
| 2514 |
} |
| 2515 |
else |
| 2516 |
return NULL_TREE; |
| 2517 |
} |
| 2518 |
} |
| 2519 |
|
| 2520 |
/* Evaluate a call to __dynamic_cast (permitted by P1327R1). |
| 2521 |
|
| 2522 |
The declaration of __dynamic_cast is: |
| 2523 |
|
| 2524 |
void* __dynamic_cast (const void* __src_ptr, |
| 2525 |
const __class_type_info* __src_type, |
| 2526 |
const __class_type_info* __dst_type, |
| 2527 |
ptrdiff_t __src2dst); |
| 2528 |
|
| 2529 |
where src2dst has the following possible values |
| 2530 |
|
| 2531 |
>-1: src_type is a unique public non-virtual base of dst_type |
| 2532 |
dst_ptr + src2dst == src_ptr |
| 2533 |
-1: unspecified relationship |
| 2534 |
-2: src_type is not a public base of dst_type |
| 2535 |
-3: src_type is a multiple public non-virtual base of dst_type |
| 2536 |
|
| 2537 |
Since literal types can't have virtual bases, we only expect hint >=0, |
| 2538 |
-2, or -3. */ |
| 2539 |
|
| 2540 |
static tree |
| 2541 |
cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call, |
| 2542 |
bool *non_constant_p, bool *overflow_p) |
| 2543 |
{ |
| 2544 |
/* T will be something like |
| 2545 |
__dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8) |
| 2546 |
dismantle it. */ |
| 2547 |
gcc_assert (call_expr_nargs (call) == 4); |
| 2548 |
tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; |
| 2549 |
tree obj = CALL_EXPR_ARG (call, 0); |
| 2550 |
tree type = CALL_EXPR_ARG (call, 2); |
| 2551 |
HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3)); |
| 2552 |
location_t loc = cp_expr_loc_or_input_loc (call); |
| 2553 |
|
| 2554 |
/* Get the target type of the dynamic_cast. */ |
| 2555 |
gcc_assert (TREE_CODE (type) == ADDR_EXPR); |
| 2556 |
type = TREE_OPERAND (type, 0); |
| 2557 |
type = TREE_TYPE (DECL_NAME (type)); |
| 2558 |
|
| 2559 |
/* TYPE can only be either T* or T&. We can't know which of these it |
| 2560 |
is by looking at TYPE, but OBJ will be "(T*) x" in the first case, |
| 2561 |
and something like "(T*)(T&)(T*) x" in the second case. */ |
| 2562 |
bool reference_p = false; |
| 2563 |
while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR) |
| 2564 |
{ |
| 2565 |
reference_p |= TYPE_REF_P (TREE_TYPE (obj)); |
| 2566 |
obj = TREE_OPERAND (obj, 0); |
| 2567 |
} |
| 2568 |
|
| 2569 |
/* Evaluate the object so that we know its dynamic type. */ |
| 2570 |
obj = cxx_eval_constant_expression (ctx, obj, vc_prvalue, non_constant_p, |
| 2571 |
overflow_p); |
| 2572 |
if (*non_constant_p) |
| 2573 |
return call; |
| 2574 |
|
| 2575 |
/* We expect OBJ to be in form of &d.D.2102 when HINT == 0, |
| 2576 |
but when HINT is > 0, it can also be something like |
| 2577 |
&d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */ |
| 2578 |
obj = extract_obj_from_addr_offset (obj); |
| 2579 |
const tree objtype = TREE_TYPE (obj); |
| 2580 |
/* If OBJ doesn't refer to a base field, we're done. */ |
| 2581 |
if (tree t = (TREE_CODE (obj) == COMPONENT_REF |
| 2582 |
? TREE_OPERAND (obj, 1) : obj)) |
| 2583 |
if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t)) |
| 2584 |
{ |
| 2585 |
if (reference_p) |
| 2586 |
{ |
| 2587 |
if (!ctx->quiet) |
| 2588 |
{ |
| 2589 |
auto_diagnostic_group d; |
| 2590 |
error_at (loc, "reference %<dynamic_cast%> failed"); |
| 2591 |
inform (loc, "dynamic type %qT of its operand does " |
| 2592 |
"not have a base class of type %qT", |
| 2593 |
objtype, type); |
| 2594 |
} |
| 2595 |
*non_constant_p = true; |
| 2596 |
} |
| 2597 |
return integer_zero_node; |
| 2598 |
} |
| 2599 |
|
| 2600 |
/* [class.cdtor] When a dynamic_cast is used in a constructor ... |
| 2601 |
or in a destructor ... if the operand of the dynamic_cast refers |
| 2602 |
to the object under construction or destruction, this object is |
| 2603 |
considered to be a most derived object that has the type of the |
| 2604 |
constructor or destructor's class. */ |
| 2605 |
tree vtable = build_vfield_ref (obj, objtype); |
| 2606 |
vtable = cxx_eval_constant_expression (ctx, vtable, vc_prvalue, |
| 2607 |
non_constant_p, overflow_p); |
| 2608 |
if (*non_constant_p) |
| 2609 |
return call; |
| 2610 |
/* With -fsanitize=vptr, we initialize all vtable pointers to null, |
| 2611 |
so it's possible that we got a null pointer now. */ |
| 2612 |
if (integer_zerop (vtable)) |
| 2613 |
{ |
| 2614 |
if (!ctx->quiet) |
| 2615 |
error_at (loc, "virtual table pointer is used uninitialized"); |
| 2616 |
*non_constant_p = true; |
| 2617 |
return integer_zero_node; |
| 2618 |
} |
| 2619 |
/* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */ |
| 2620 |
vtable = extract_obj_from_addr_offset (vtable); |
| 2621 |
const tree mdtype = DECL_CONTEXT (vtable); |
| 2622 |
|
| 2623 |
/* Given dynamic_cast<T>(v), |
| 2624 |
|
| 2625 |
[expr.dynamic.cast] If C is the class type to which T points or refers, |
| 2626 |
the runtime check logically executes as follows: |
| 2627 |
|
| 2628 |
If, in the most derived object pointed (referred) to by v, v points |
| 2629 |
(refers) to a public base class subobject of a C object, and if only |
| 2630 |
one object of type C is derived from the subobject pointed (referred) |
| 2631 |
to by v the result points (refers) to that C object. |
| 2632 |
|
| 2633 |
In this case, HINT >= 0 or -3. */ |
| 2634 |
if (hint >= 0 || hint == -3) |
| 2635 |
{ |
| 2636 |
/* Look for a component with type TYPE. */ |
| 2637 |
tree t = get_component_with_type (obj, type, mdtype); |
| 2638 |
/* If not accessible, give an error. */ |
| 2639 |
if (t == error_mark_node) |
| 2640 |
{ |
| 2641 |
if (reference_p) |
| 2642 |
{ |
| 2643 |
if (!ctx->quiet) |
| 2644 |
{ |
| 2645 |
auto_diagnostic_group d; |
| 2646 |
error_at (loc, "reference %<dynamic_cast%> failed"); |
| 2647 |
inform (loc, "static type %qT of its operand is a " |
| 2648 |
"non-public base class of dynamic type %qT", |
| 2649 |
objtype, type); |
| 2650 |
|
| 2651 |
} |
| 2652 |
*non_constant_p = true; |
| 2653 |
} |
| 2654 |
return integer_zero_node; |
| 2655 |
} |
| 2656 |
else if (t) |
| 2657 |
/* The result points to the TYPE object. */ |
| 2658 |
return cp_build_addr_expr (t, complain); |
| 2659 |
/* Else, TYPE was not found, because the HINT turned out to be wrong. |
| 2660 |
Fall through to the normal processing. */ |
| 2661 |
} |
| 2662 |
|
| 2663 |
/* Otherwise, if v points (refers) to a public base class subobject of the |
| 2664 |
most derived object, and the type of the most derived object has a base |
| 2665 |
class, of type C, that is unambiguous and public, the result points |
| 2666 |
(refers) to the C subobject of the most derived object. |
| 2667 |
|
| 2668 |
But it can also be an invalid case. */ |
| 2669 |
|
| 2670 |
/* Get the most derived object. */ |
| 2671 |
obj = get_component_with_type (obj, mdtype, NULL_TREE); |
| 2672 |
if (obj == error_mark_node) |
| 2673 |
{ |
| 2674 |
if (reference_p) |
| 2675 |
{ |
| 2676 |
if (!ctx->quiet) |
| 2677 |
{ |
| 2678 |
auto_diagnostic_group d; |
| 2679 |
error_at (loc, "reference %<dynamic_cast%> failed"); |
| 2680 |
inform (loc, "static type %qT of its operand is a non-public" |
| 2681 |
" base class of dynamic type %qT", objtype, mdtype); |
| 2682 |
} |
| 2683 |
*non_constant_p = true; |
| 2684 |
} |
| 2685 |
return integer_zero_node; |
| 2686 |
} |
| 2687 |
else |
| 2688 |
gcc_assert (obj); |
| 2689 |
|
| 2690 |
/* Check that the type of the most derived object has a base class |
| 2691 |
of type TYPE that is unambiguous and public. */ |
| 2692 |
base_kind b_kind; |
| 2693 |
tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none); |
| 2694 |
if (!binfo || binfo == error_mark_node) |
| 2695 |
{ |
| 2696 |
if (reference_p) |
| 2697 |
{ |
| 2698 |
if (!ctx->quiet) |
| 2699 |
{ |
| 2700 |
auto_diagnostic_group d; |
| 2701 |
error_at (loc, "reference %<dynamic_cast%> failed"); |
| 2702 |
if (b_kind == bk_ambig) |
| 2703 |
inform (loc, "%qT is an ambiguous base class of dynamic " |
| 2704 |
"type %qT of its operand", type, mdtype); |
| 2705 |
else |
| 2706 |
inform (loc, "dynamic type %qT of its operand does not " |
| 2707 |
"have an unambiguous public base class %qT", |
| 2708 |
mdtype, type); |
| 2709 |
} |
| 2710 |
*non_constant_p = true; |
| 2711 |
} |
| 2712 |
return integer_zero_node; |
| 2713 |
} |
| 2714 |
/* If so, return the TYPE subobject of the most derived object. */ |
| 2715 |
obj = convert_to_base_statically (obj, binfo); |
| 2716 |
return cp_build_addr_expr (obj, complain); |
| 2717 |
} |
| 2718 |
|
| 2719 |
/* Data structure used by replace_decl and replace_decl_r. */ |
| 2720 |
|
| 2721 |
struct replace_decl_data |
| 2722 |
{ |
| 2723 |
/* The _DECL we want to replace. */ |
| 2724 |
tree decl; |
| 2725 |
/* The replacement for DECL. */ |
| 2726 |
tree replacement; |
| 2727 |
/* Trees we've visited. */ |
| 2728 |
hash_set<tree> *pset; |
| 2729 |
/* Whether we've performed any replacements. */ |
| 2730 |
bool changed; |
| 2731 |
}; |
| 2732 |
|
| 2733 |
/* Helper function for replace_decl, called through cp_walk_tree. */ |
| 2734 |
|
| 2735 |
static tree |
| 2736 |
replace_decl_r (tree *tp, int *walk_subtrees, void *data) |
| 2737 |
{ |
| 2738 |
replace_decl_data *d = (replace_decl_data *) data; |
| 2739 |
|
| 2740 |
/* We could be replacing |
| 2741 |
&<retval>.bar -> &foo.bar |
| 2742 |
where foo is a static VAR_DECL, so we need to recompute TREE_CONSTANT |
| 2743 |
on the ADDR_EXPR around it. */ |
| 2744 |
if (TREE_CODE (*tp) == ADDR_EXPR) |
| 2745 |
{ |
| 2746 |
d->pset->add (*tp); |
| 2747 |
auto save_changed = d->changed; |
| 2748 |
d->changed = false; |
| 2749 |
cp_walk_tree (&TREE_OPERAND (*tp, 0), replace_decl_r, d, nullptr); |
| 2750 |
if (d->changed) |
| 2751 |
{ |
| 2752 |
cxx_mark_addressable (*tp); |
| 2753 |
recompute_tree_invariant_for_addr_expr (*tp); |
| 2754 |
} |
| 2755 |
else |
| 2756 |
d->changed = save_changed; |
| 2757 |
*walk_subtrees = 0; |
| 2758 |
} |
| 2759 |
else if (*tp == d->decl) |
| 2760 |
{ |
| 2761 |
*tp = unshare_expr (d->replacement); |
| 2762 |
d->changed = true; |
| 2763 |
*walk_subtrees = 0; |
| 2764 |
} |
| 2765 |
else if (TYPE_P (*tp) |
| 2766 |
|| d->pset->add (*tp)) |
| 2767 |
*walk_subtrees = 0; |
| 2768 |
|
| 2769 |
return NULL_TREE; |
| 2770 |
} |
| 2771 |
|
| 2772 |
/* Replace every occurrence of DECL with (an unshared copy of) |
| 2773 |
REPLACEMENT within the expression *TP. Returns true iff a |
| 2774 |
replacement was performed. */ |
| 2775 |
|
| 2776 |
bool |
| 2777 |
replace_decl (tree *tp, tree decl, tree replacement) |
| 2778 |
{ |
| 2779 |
gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p |
| 2780 |
(TREE_TYPE (decl), TREE_TYPE (replacement))); |
| 2781 |
hash_set<tree> pset; |
| 2782 |
replace_decl_data data = { decl, replacement, &pset, false }; |
| 2783 |
cp_walk_tree (tp, replace_decl_r, &data, NULL); |
| 2784 |
return data.changed; |
| 2785 |
} |
| 2786 |
|
| 2787 |
/* Evaluate the call T to virtual function thunk THUNK_FNDECL. */ |
| 2788 |
|
| 2789 |
static tree |
| 2790 |
cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl, |
| 2791 |
value_cat lval, |
| 2792 |
bool *non_constant_p, bool *overflow_p) |
| 2793 |
{ |
| 2794 |
tree function = THUNK_TARGET (thunk_fndecl); |
| 2795 |
|
| 2796 |
if (THUNK_VIRTUAL_OFFSET (thunk_fndecl)) |
| 2797 |
{ |
| 2798 |
if (!ctx->quiet) |
| 2799 |
{ |
| 2800 |
if (!DECL_DECLARED_CONSTEXPR_P (function)) |
| 2801 |
{ |
| 2802 |
error ("call to non-%<constexpr%> function %qD", function); |
| 2803 |
explain_invalid_constexpr_fn (function); |
| 2804 |
} |
| 2805 |
else |
| 2806 |
/* virtual_offset is only set for virtual bases, which make the |
| 2807 |
class non-literal, so we don't need to handle it here. */ |
| 2808 |
error ("calling constexpr member function %qD through virtual " |
| 2809 |
"base subobject", function); |
| 2810 |
} |
| 2811 |
*non_constant_p = true; |
| 2812 |
return t; |
| 2813 |
} |
| 2814 |
|
| 2815 |
tree new_call = copy_node (t); |
| 2816 |
CALL_EXPR_FN (new_call) = function; |
| 2817 |
TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function)); |
| 2818 |
|
| 2819 |
tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl)); |
| 2820 |
|
| 2821 |
if (DECL_THIS_THUNK_P (thunk_fndecl)) |
| 2822 |
{ |
| 2823 |
/* 'this'-adjusting thunk. */ |
| 2824 |
tree this_arg = CALL_EXPR_ARG (t, 0); |
| 2825 |
this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg), |
| 2826 |
this_arg, offset); |
| 2827 |
CALL_EXPR_ARG (new_call, 0) = this_arg; |
| 2828 |
} |
| 2829 |
else |
| 2830 |
/* Return-adjusting thunk. */ |
| 2831 |
new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call), |
| 2832 |
new_call, offset); |
| 2833 |
|
| 2834 |
return cxx_eval_constant_expression (ctx, new_call, lval, |
| 2835 |
non_constant_p, overflow_p); |
| 2836 |
} |
| 2837 |
|
| 2838 |
/* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set |
| 2839 |
its TREE_READONLY flag according to READONLY_P. Used for constexpr |
| 2840 |
'tors to detect modifying const objects in a constexpr context. */ |
| 2841 |
|
| 2842 |
static void |
| 2843 |
cxx_set_object_constness (const constexpr_ctx *ctx, tree object, |
| 2844 |
bool readonly_p, bool *non_constant_p, |
| 2845 |
bool *overflow_p) |
| 2846 |
{ |
| 2847 |
if (CLASS_TYPE_P (TREE_TYPE (object)) |
| 2848 |
&& CP_TYPE_CONST_P (TREE_TYPE (object))) |
| 2849 |
{ |
| 2850 |
/* Subobjects might not be stored in ctx->global->values but we |
| 2851 |
can get its CONSTRUCTOR by evaluating *this. */ |
| 2852 |
tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue, |
| 2853 |
non_constant_p, overflow_p); |
| 2854 |
if (TREE_CODE (e) == CONSTRUCTOR && !*non_constant_p) |
| 2855 |
TREE_READONLY (e) = readonly_p; |
| 2856 |
} |
| 2857 |
} |
| 2858 |
|
| 2859 |
/* Subroutine of cxx_eval_constant_expression. |
| 2860 |
Evaluate the call expression tree T in the context of OLD_CALL expression |
| 2861 |
evaluation. */ |
| 2862 |
|
| 2863 |
static tree |
| 2864 |
cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, |
| 2865 |
value_cat lval, |
| 2866 |
bool *non_constant_p, bool *overflow_p) |
| 2867 |
{ |
| 2868 |
location_t loc = cp_expr_loc_or_input_loc (t); |
| 2869 |
tree fun = get_function_named_in_call (t); |
| 2870 |
|
| 2871 |
if (fun == NULL_TREE) |
| 2872 |
return cxx_eval_internal_function (ctx, t, lval, |
| 2873 |
non_constant_p, overflow_p); |
| 2874 |
|
| 2875 |
if (TREE_CODE (fun) != FUNCTION_DECL) |
| 2876 |
{ |
| 2877 |
/* Might be a constexpr function pointer. */ |
| 2878 |
fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue, |
| 2879 |
non_constant_p, overflow_p); |
| 2880 |
STRIP_NOPS (fun); |
| 2881 |
if (TREE_CODE (fun) == ADDR_EXPR) |
| 2882 |
fun = TREE_OPERAND (fun, 0); |
| 2883 |
/* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no |
| 2884 |
indirection, the called expression is a pointer into the |
| 2885 |
virtual table which should contain FDESC_EXPR. Extract the |
| 2886 |
FUNCTION_DECL from there. */ |
| 2887 |
else if (TARGET_VTABLE_USES_DESCRIPTORS |
| 2888 |
&& TREE_CODE (fun) == POINTER_PLUS_EXPR |
| 2889 |
&& TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR |
| 2890 |
&& TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST) |
| 2891 |
{ |
| 2892 |
tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0); |
| 2893 |
if (VAR_P (d) |
| 2894 |
&& DECL_VTABLE_OR_VTT_P (d) |
| 2895 |
&& TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE |
| 2896 |
&& TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type |
| 2897 |
&& DECL_INITIAL (d) |
| 2898 |
&& TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR) |
| 2899 |
{ |
| 2900 |
tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1), |
| 2901 |
TYPE_SIZE_UNIT (vtable_entry_type)); |
| 2902 |
HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i); |
| 2903 |
if (idx >= 0) |
| 2904 |
{ |
| 2905 |
tree fdesc |
| 2906 |
= (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value; |
| 2907 |
if (TREE_CODE (fdesc) == FDESC_EXPR |
| 2908 |
&& integer_zerop (TREE_OPERAND (fdesc, 1))) |
| 2909 |
fun = TREE_OPERAND (fdesc, 0); |
| 2910 |
} |
| 2911 |
} |
| 2912 |
} |
| 2913 |
} |
| 2914 |
if (TREE_CODE (fun) != FUNCTION_DECL) |
| 2915 |
{ |
| 2916 |
if (!ctx->quiet && !*non_constant_p) |
| 2917 |
error_at (loc, "expression %qE does not designate a %<constexpr%> " |
| 2918 |
"function", fun); |
| 2919 |
*non_constant_p = true; |
| 2920 |
return t; |
| 2921 |
} |
| 2922 |
if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)) |
| 2923 |
fun = DECL_CLONED_FUNCTION (fun); |
| 2924 |
|
| 2925 |
if (is_ubsan_builtin_p (fun)) |
| 2926 |
return void_node; |
| 2927 |
|
| 2928 |
if (fndecl_built_in_p (fun)) |
| 2929 |
return cxx_eval_builtin_function_call (ctx, t, fun, |
| 2930 |
lval, non_constant_p, overflow_p); |
| 2931 |
if (DECL_THUNK_P (fun)) |
| 2932 |
return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p); |
| 2933 |
if (!maybe_constexpr_fn (fun)) |
| 2934 |
{ |
| 2935 |
if (TREE_CODE (t) == CALL_EXPR |
| 2936 |
&& cxx_replaceable_global_alloc_fn (fun) |
| 2937 |
&& (CALL_FROM_NEW_OR_DELETE_P (t) |
| 2938 |
|| is_std_allocator_allocate (ctx->call))) |
| 2939 |
{ |
| 2940 |
const bool new_op_p = IDENTIFIER_NEW_OP_P (DECL_NAME (fun)); |
| 2941 |
const int nargs = call_expr_nargs (t); |
| 2942 |
tree arg0 = NULL_TREE; |
| 2943 |
for (int i = 0; i < nargs; ++i) |
| 2944 |
{ |
| 2945 |
tree arg = CALL_EXPR_ARG (t, i); |
| 2946 |
arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue, |
| 2947 |
non_constant_p, overflow_p); |
| 2948 |
/* Deleting a non-constant pointer has a better error message |
| 2949 |
below. */ |
| 2950 |
if (new_op_p || i != 0) |
| 2951 |
VERIFY_CONSTANT (arg); |
| 2952 |
if (i == 0) |
| 2953 |
arg0 = arg; |
| 2954 |
} |
| 2955 |
gcc_assert (arg0); |
| 2956 |
if (new_op_p) |
| 2957 |
{ |
| 2958 |
/* FIXME: We should not get here; the VERIFY_CONSTANT above |
| 2959 |
should have already caught it. But currently a conversion |
| 2960 |
from pointer type to arithmetic type is only considered |
| 2961 |
non-constant for CONVERT_EXPRs, not NOP_EXPRs. */ |
| 2962 |
if (!tree_fits_uhwi_p (arg0)) |
| 2963 |
{ |
| 2964 |
if (!ctx->quiet) |
| 2965 |
error_at (loc, "cannot allocate array: size not constant"); |
| 2966 |
*non_constant_p = true; |
| 2967 |
return t; |
| 2968 |
} |
| 2969 |
tree type = build_array_type_nelts (char_type_node, |
| 2970 |
tree_to_uhwi (arg0)); |
| 2971 |
tree var = build_decl (loc, VAR_DECL, |
| 2972 |
(IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) |
| 2973 |
& OVL_OP_FLAG_VEC) |
| 2974 |
? heap_vec_uninit_identifier |
| 2975 |
: heap_uninit_identifier, |
| 2976 |
type); |
| 2977 |
DECL_ARTIFICIAL (var) = 1; |
| 2978 |
TREE_STATIC (var) = 1; |
| 2979 |
// Temporarily register the artificial var in varpool, |
| 2980 |
// so that comparisons of its address against NULL are folded |
| 2981 |
// through nonzero_address even with |
| 2982 |
// -fno-delete-null-pointer-checks or that comparison of |
| 2983 |
// addresses of different heap artificial vars is folded too. |
| 2984 |
// See PR98988 and PR99031. |
| 2985 |
varpool_node::finalize_decl (var); |
| 2986 |
ctx->global->heap_vars.safe_push (var); |
| 2987 |
ctx->global->put_value (var, NULL_TREE); |
| 2988 |
return fold_convert (ptr_type_node, build_address (var)); |
| 2989 |
} |
| 2990 |
else |
| 2991 |
{ |
| 2992 |
STRIP_NOPS (arg0); |
| 2993 |
if (TREE_CODE (arg0) == ADDR_EXPR |
| 2994 |
&& VAR_P (TREE_OPERAND (arg0, 0))) |
| 2995 |
{ |
| 2996 |
tree var = TREE_OPERAND (arg0, 0); |
| 2997 |
if (DECL_NAME (var) == heap_uninit_identifier |
| 2998 |
|| DECL_NAME (var) == heap_identifier) |
| 2999 |
{ |
| 3000 |
if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) |
| 3001 |
& OVL_OP_FLAG_VEC) |
| 3002 |
{ |
| 3003 |
if (!ctx->quiet) |
| 3004 |
{ |
| 3005 |
auto_diagnostic_group d; |
| 3006 |
error_at (loc, "array deallocation of object " |
| 3007 |
"allocated with non-array " |
| 3008 |
"allocation"); |
| 3009 |
inform (DECL_SOURCE_LOCATION (var), |
| 3010 |
"allocation performed here"); |
| 3011 |
} |
| 3012 |
*non_constant_p = true; |
| 3013 |
return t; |
| 3014 |
} |
| 3015 |
DECL_NAME (var) = heap_deleted_identifier; |
| 3016 |
ctx->global->destroy_value (var); |
| 3017 |
ctx->global->heap_dealloc_count++; |
| 3018 |
return void_node; |
| 3019 |
} |
| 3020 |
else if (DECL_NAME (var) == heap_vec_uninit_identifier |
| 3021 |
|| DECL_NAME (var) == heap_vec_identifier) |
| 3022 |
{ |
| 3023 |
if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun)) |
| 3024 |
& OVL_OP_FLAG_VEC) == 0) |
| 3025 |
{ |
| 3026 |
if (!ctx->quiet) |
| 3027 |
{ |
| 3028 |
auto_diagnostic_group d; |
| 3029 |
error_at (loc, "non-array deallocation of " |
| 3030 |
"object allocated with array " |
| 3031 |
"allocation"); |
| 3032 |
inform (DECL_SOURCE_LOCATION (var), |
| 3033 |
"allocation performed here"); |
| 3034 |
} |
| 3035 |
*non_constant_p = true; |
| 3036 |
return t; |
| 3037 |
} |
| 3038 |
DECL_NAME (var) = heap_deleted_identifier; |
| 3039 |
ctx->global->destroy_value (var); |
| 3040 |
ctx->global->heap_dealloc_count++; |
| 3041 |
return void_node; |
| 3042 |
} |
| 3043 |
else if (DECL_NAME (var) == heap_deleted_identifier) |
| 3044 |
{ |
| 3045 |
if (!ctx->quiet) |
| 3046 |
error_at (loc, "deallocation of already deallocated " |
| 3047 |
"storage"); |
| 3048 |
*non_constant_p = true; |
| 3049 |
return t; |
| 3050 |
} |
| 3051 |
} |
| 3052 |
if (!ctx->quiet) |
| 3053 |
error_at (loc, "deallocation of storage that was " |
| 3054 |
"not previously allocated"); |
| 3055 |
*non_constant_p = true; |
| 3056 |
return t; |
| 3057 |
} |
| 3058 |
} |
| 3059 |
/* Allow placement new in std::construct_at, just return the second |
| 3060 |
argument. */ |
| 3061 |
if (TREE_CODE (t) == CALL_EXPR |
| 3062 |
&& cxx_placement_new_fn (fun) |
| 3063 |
&& is_std_construct_at (ctx->call)) |
| 3064 |
{ |
| 3065 |
const int nargs = call_expr_nargs (t); |
| 3066 |
tree arg1 = NULL_TREE; |
| 3067 |
for (int i = 0; i < nargs; ++i) |
| 3068 |
{ |
| 3069 |
tree arg = CALL_EXPR_ARG (t, i); |
| 3070 |
arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue, |
| 3071 |
non_constant_p, overflow_p); |
| 3072 |
if (i == 1) |
| 3073 |
arg1 = arg; |
| 3074 |
else |
| 3075 |
VERIFY_CONSTANT (arg); |
| 3076 |
} |
| 3077 |
gcc_assert (arg1); |
| 3078 |
return arg1; |
| 3079 |
} |
| 3080 |
else if (cxx_dynamic_cast_fn_p (fun)) |
| 3081 |
return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p); |
| 3082 |
|
| 3083 |
if (!ctx->quiet) |
| 3084 |
{ |
| 3085 |
if (!lambda_static_thunk_p (fun)) |
| 3086 |
error_at (loc, "call to non-%<constexpr%> function %qD", fun); |
| 3087 |
explain_invalid_constexpr_fn (fun); |
| 3088 |
} |
| 3089 |
*non_constant_p = true; |
| 3090 |
return t; |
| 3091 |
} |
| 3092 |
|
| 3093 |
constexpr_ctx new_ctx = *ctx; |
| 3094 |
ctx = &new_ctx; |
| 3095 |
if (DECL_CONSTRUCTOR_P (fun) && !ctx->object |
| 3096 |
&& TREE_CODE (t) == AGGR_INIT_EXPR) |
| 3097 |
{ |
| 3098 |
/* We want to have an initialization target for an AGGR_INIT_EXPR. |
| 3099 |
If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */ |
| 3100 |
new_ctx.object = AGGR_INIT_EXPR_SLOT (t); |
| 3101 |
tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL); |
| 3102 |
CONSTRUCTOR_NO_CLEARING (ctor) = true; |
| 3103 |
ctx->global->put_value (new_ctx.object, ctor); |
| 3104 |
} |
| 3105 |
/* An immediate invocation is manifestly constant evaluated including the |
| 3106 |
arguments of the call, so use mce_true even for the argument |
| 3107 |
evaluation. */ |
| 3108 |
if (DECL_IMMEDIATE_FUNCTION_P (fun)) |
| 3109 |
new_ctx.manifestly_const_eval = mce_true; |
| 3110 |
|
| 3111 |
/* We used to shortcut trivial constructor/op= here, but nowadays |
| 3112 |
we can only get a trivial function here with -fno-elide-constructors. */ |
| 3113 |
gcc_checking_assert (!trivial_fn_p (fun) |
| 3114 |
|| !flag_elide_constructors |
| 3115 |
/* Or it's a call from maybe_thunk_body (111075). */ |
| 3116 |
|| (TREE_CODE (t) == CALL_EXPR ? CALL_FROM_THUNK_P (t) |
| 3117 |
: AGGR_INIT_FROM_THUNK_P (t)) |
| 3118 |
/* We don't elide constructors when processing |
| 3119 |
a noexcept-expression. */ |
| 3120 |
|| cp_noexcept_operand); |
| 3121 |
|
| 3122 |
bool non_constant_args = false; |
| 3123 |
constexpr_call new_call; |
| 3124 |
new_call.bindings |
| 3125 |
= cxx_bind_parameters_in_call (ctx, t, fun, non_constant_p, |
| 3126 |
overflow_p, &non_constant_args); |
| 3127 |
|
| 3128 |
/* We build up the bindings list before we know whether we already have this |
| 3129 |
call cached. If we don't end up saving these bindings, ggc_free them when |
| 3130 |
this function exits. */ |
| 3131 |
class free_bindings |
| 3132 |
{ |
| 3133 |
tree *bindings; |
| 3134 |
public: |
| 3135 |
free_bindings (tree &b): bindings (&b) { } |
| 3136 |
~free_bindings () { if (bindings) ggc_free (*bindings); } |
| 3137 |
void preserve () { bindings = NULL; } |
| 3138 |
} fb (new_call.bindings); |
| 3139 |
|
| 3140 |
if (*non_constant_p) |
| 3141 |
return t; |
| 3142 |
|
| 3143 |
/* We can't defer instantiating the function any longer. */ |
| 3144 |
if (!DECL_INITIAL (fun) |
| 3145 |
&& (DECL_TEMPLOID_INSTANTIATION (fun) || DECL_DEFAULTED_FN (fun)) |
| 3146 |
&& !uid_sensitive_constexpr_evaluation_p ()) |
| 3147 |
{ |
| 3148 |
location_t save_loc = input_location; |
| 3149 |
input_location = loc; |
| 3150 |
++function_depth; |
| 3151 |
if (ctx->manifestly_const_eval == mce_true) |
| 3152 |
FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true; |
| 3153 |
if (DECL_TEMPLOID_INSTANTIATION (fun)) |
| 3154 |
instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false); |
| 3155 |
else |
| 3156 |
synthesize_method (fun); |
| 3157 |
--function_depth; |
| 3158 |
input_location = save_loc; |
| 3159 |
} |
| 3160 |
|
| 3161 |
/* If in direct recursive call, optimize definition search. */ |
| 3162 |
if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun) |
| 3163 |
new_call.fundef = ctx->call->fundef; |
| 3164 |
else |
| 3165 |
{ |
| 3166 |
new_call.fundef = retrieve_constexpr_fundef (fun); |
| 3167 |
if (new_call.fundef == NULL || new_call.fundef->body == NULL |
| 3168 |
|| new_call.fundef->result == error_mark_node |
| 3169 |
|| fun == current_function_decl) |
| 3170 |
{ |
| 3171 |
if (!ctx->quiet) |
| 3172 |
{ |
| 3173 |
/* We need to check for current_function_decl here in case we're |
| 3174 |
being called during cp_fold_function, because at that point |
| 3175 |
DECL_INITIAL is set properly and we have a fundef but we |
| 3176 |
haven't lowered invisirefs yet (c++/70344). */ |
| 3177 |
if (DECL_INITIAL (fun) == error_mark_node |
| 3178 |
|| fun == current_function_decl) |
| 3179 |
error_at (loc, "%qD called in a constant expression before its " |
| 3180 |
"definition is complete", fun); |
| 3181 |
else if (DECL_INITIAL (fun)) |
| 3182 |
{ |
| 3183 |
/* The definition of fun was somehow unsuitable. But pretend |
| 3184 |
that lambda static thunks don't exist. */ |
| 3185 |
if (!lambda_static_thunk_p (fun)) |
| 3186 |
error_at (loc, "%qD called in a constant expression", fun); |
| 3187 |
explain_invalid_constexpr_fn (fun); |
| 3188 |
} |
| 3189 |
else |
| 3190 |
error_at (loc, "%qD used before its definition", fun); |
| 3191 |
} |
| 3192 |
*non_constant_p = true; |
| 3193 |
return t; |
| 3194 |
} |
| 3195 |
} |
| 3196 |
|
| 3197 |
/* Don't complain about problems evaluating an ill-formed function. */ |
| 3198 |
if (function *f = DECL_STRUCT_FUNCTION (fun)) |
| 3199 |
if (f->language->erroneous) |
| 3200 |
new_ctx.quiet = true; |
| 3201 |
|
| 3202 |
int depth_ok = push_cx_call_context (t); |
| 3203 |
|
| 3204 |
/* Remember the object we are constructing or destructing. */ |
| 3205 |
tree new_obj = NULL_TREE; |
| 3206 |
if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun)) |
| 3207 |
{ |
| 3208 |
/* In a cdtor, it should be the first `this' argument. |
| 3209 |
At this point it has already been evaluated in the call |
| 3210 |
to cxx_bind_parameters_in_call. */ |
| 3211 |
new_obj = TREE_VEC_ELT (new_call.bindings, 0); |
| 3212 |
bool empty_base = false; |
| 3213 |
new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj, |
| 3214 |
&empty_base); |
| 3215 |
/* If we're initializing an empty class, don't set constness, because |
| 3216 |
cxx_fold_indirect_ref will return the wrong object to set constness |
| 3217 |
of. */ |
| 3218 |
if (empty_base) |
| 3219 |
new_obj = NULL_TREE; |
| 3220 |
else if (ctx->call && ctx->call->fundef |
| 3221 |
&& DECL_CONSTRUCTOR_P (ctx->call->fundef->decl)) |
| 3222 |
{ |
| 3223 |
tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0); |
| 3224 |
STRIP_NOPS (cur_obj); |
| 3225 |
if (TREE_CODE (cur_obj) == ADDR_EXPR) |
| 3226 |
cur_obj = TREE_OPERAND (cur_obj, 0); |
| 3227 |
if (new_obj == cur_obj) |
| 3228 |
/* We're calling the target constructor of a delegating |
| 3229 |
constructor, or accessing a base subobject through a |
| 3230 |
NOP_EXPR as part of a call to a base constructor, so |
| 3231 |
there is no new (sub)object. */ |
| 3232 |
new_obj = NULL_TREE; |
| 3233 |
} |
| 3234 |
} |
| 3235 |
|
| 3236 |
tree result = NULL_TREE; |
| 3237 |
|
| 3238 |
constexpr_call *entry = NULL; |
| 3239 |
if (depth_ok && !non_constant_args && ctx->strict) |
| 3240 |
{ |
| 3241 |
new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef); |
| 3242 |
new_call.hash |
| 3243 |
= iterative_hash_template_arg (new_call.bindings, new_call.hash); |
| 3244 |
|
| 3245 |
/* If we have seen this call before, we are done. */ |
| 3246 |
maybe_initialize_constexpr_call_table (); |
| 3247 |
bool insert = depth_ok < constexpr_cache_depth; |
| 3248 |
constexpr_call **slot |
| 3249 |
= constexpr_call_table->find_slot (&new_call, |
| 3250 |
insert ? INSERT : NO_INSERT); |
| 3251 |
entry = slot ? *slot : NULL; |
| 3252 |
if (entry == NULL) |
| 3253 |
{ |
| 3254 |
/* Only cache up to constexpr_cache_depth to limit memory use. */ |
| 3255 |
if (insert) |
| 3256 |
{ |
| 3257 |
/* We need to keep a pointer to the entry, not just the slot, as |
| 3258 |
the slot can move during evaluation of the body. */ |
| 3259 |
*slot = entry = ggc_alloc<constexpr_call> (); |
| 3260 |
*entry = new_call; |
| 3261 |
entry->result (ctx->manifestly_const_eval) = unknown_type_node; |
| 3262 |
fb.preserve (); |
| 3263 |
|
| 3264 |
/* Unshare args going into the hash table to separate them |
| 3265 |
from the caller's context, for better GC and to avoid |
| 3266 |
problems with verify_gimple. */ |
| 3267 |
tree bound = new_call.bindings; |
| 3268 |
for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i) |
| 3269 |
{ |
| 3270 |
tree &arg = TREE_VEC_ELT (bound, i); |
| 3271 |
arg = unshare_expr_without_location (arg); |
| 3272 |
} |
| 3273 |
} |
| 3274 |
} |
| 3275 |
/* Calls that are in progress have their result set to unknown_type_node, |
| 3276 |
so that we can detect circular dependencies. Now that we only cache |
| 3277 |
up to constexpr_cache_depth this won't catch circular dependencies that |
| 3278 |
start deeper, but they'll hit the recursion or ops limit. */ |
| 3279 |
else if (entry->result (ctx->manifestly_const_eval) == unknown_type_node) |
| 3280 |
{ |
| 3281 |
if (!ctx->quiet) |
| 3282 |
error ("call has circular dependency"); |
| 3283 |
*non_constant_p = true; |
| 3284 |
entry->result (ctx->manifestly_const_eval) = result = error_mark_node; |
| 3285 |
} |
| 3286 |
else |
| 3287 |
result = entry->result (ctx->manifestly_const_eval); |
| 3288 |
} |
| 3289 |
|
| 3290 |
if (!depth_ok) |
| 3291 |
{ |
| 3292 |
if (!ctx->quiet) |
| 3293 |
error ("%<constexpr%> evaluation depth exceeds maximum of %d (use " |
| 3294 |
"%<-fconstexpr-depth=%> to increase the maximum)", |
| 3295 |
max_constexpr_depth); |
| 3296 |
*non_constant_p = true; |
| 3297 |
result = error_mark_node; |
| 3298 |
} |
| 3299 |
else |
| 3300 |
{ |
| 3301 |
bool cacheable = !!entry; |
| 3302 |
if (result && result != error_mark_node) |
| 3303 |
/* OK */; |
| 3304 |
else if (!DECL_SAVED_TREE (fun)) |
| 3305 |
{ |
| 3306 |
/* When at_eof >= 3, cgraph has started throwing away |
| 3307 |
DECL_SAVED_TREE, so fail quietly. FIXME we get here because of |
| 3308 |
late code generation for VEC_INIT_EXPR, which needs to be |
| 3309 |
completely reconsidered. */ |
| 3310 |
gcc_assert (at_eof >= 3 && ctx->quiet); |
| 3311 |
*non_constant_p = true; |
| 3312 |
} |
| 3313 |
else if (tree copy = get_fundef_copy (new_call.fundef)) |
| 3314 |
{ |
| 3315 |
tree body, parms, res; |
| 3316 |
releasing_vec ctors; |
| 3317 |
|
| 3318 |
/* Reuse or create a new unshared copy of this function's body. */ |
| 3319 |
body = TREE_PURPOSE (copy); |
| 3320 |
parms = TREE_VALUE (copy); |
| 3321 |
res = TREE_TYPE (copy); |
| 3322 |
|
| 3323 |
/* Associate the bindings with the remapped parms. */ |
| 3324 |
tree bound = new_call.bindings; |
| 3325 |
tree remapped = parms; |
| 3326 |
for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i) |
| 3327 |
{ |
| 3328 |
tree arg = TREE_VEC_ELT (bound, i); |
| 3329 |
if (entry) |
| 3330 |
{ |
| 3331 |
/* Unshare again so the callee doesn't change the |
| 3332 |
argument values in the hash table. XXX Could we unshare |
| 3333 |
lazily in cxx_eval_store_expression? */ |
| 3334 |
arg = unshare_constructor (arg); |
| 3335 |
if (TREE_CODE (arg) == CONSTRUCTOR) |
| 3336 |
vec_safe_push (ctors, arg); |
| 3337 |
} |
| 3338 |
ctx->global->put_value (remapped, arg); |
| 3339 |
remapped = DECL_CHAIN (remapped); |
| 3340 |
} |
| 3341 |
if (remapped) |
| 3342 |
{ |
| 3343 |
/* We shouldn't have any parms without args, but fail gracefully |
| 3344 |
in error recovery. */ |
| 3345 |
gcc_checking_assert (seen_error ()); |
| 3346 |
*non_constant_p = true; |
| 3347 |
} |
| 3348 |
/* Add the RESULT_DECL to the values map, too. */ |
| 3349 |
gcc_assert (!DECL_BY_REFERENCE (res)); |
| 3350 |
ctx->global->put_value (res, NULL_TREE); |
| 3351 |
|
| 3352 |
/* Remember the current call we're evaluating. */ |
| 3353 |
constexpr_ctx call_ctx = *ctx; |
| 3354 |
call_ctx.call = &new_call; |
| 3355 |
unsigned save_heap_alloc_count = ctx->global->heap_vars.length (); |
| 3356 |
unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count; |
| 3357 |
|
| 3358 |
/* Make sure we fold std::is_constant_evaluated to true in an |
| 3359 |
immediate function. */ |
| 3360 |
if (DECL_IMMEDIATE_FUNCTION_P (fun)) |
| 3361 |
call_ctx.manifestly_const_eval = mce_true; |
| 3362 |
|
| 3363 |
/* If this is a constexpr destructor, the object's const and volatile |
| 3364 |
semantics are no longer in effect; see [class.dtor]p5. */ |
| 3365 |
if (new_obj && DECL_DESTRUCTOR_P (fun)) |
| 3366 |
cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false, |
| 3367 |
non_constant_p, overflow_p); |
| 3368 |
|
| 3369 |
/* If this is a constructor, we are beginning the lifetime of the |
| 3370 |
object we are initializing. */ |
| 3371 |
if (new_obj |
| 3372 |
&& DECL_CONSTRUCTOR_P (fun) |
| 3373 |
&& TREE_CODE (new_obj) == COMPONENT_REF |
| 3374 |
&& TREE_CODE (TREE_TYPE (TREE_OPERAND (new_obj, 0))) == UNION_TYPE) |
| 3375 |
{ |
| 3376 |
tree ctor = build_constructor (TREE_TYPE (new_obj), NULL); |
| 3377 |
CONSTRUCTOR_NO_CLEARING (ctor) = true; |
| 3378 |
tree activate = build2 (INIT_EXPR, TREE_TYPE (new_obj), |
| 3379 |
new_obj, ctor); |
| 3380 |
cxx_eval_constant_expression (ctx, activate, |
| 3381 |
lval, non_constant_p, overflow_p); |
| 3382 |
ggc_free (activate); |
| 3383 |
} |
| 3384 |
|
| 3385 |
tree jump_target = NULL_TREE; |
| 3386 |
cxx_eval_constant_expression (&call_ctx, body, |
| 3387 |
vc_discard, non_constant_p, overflow_p, |
| 3388 |
&jump_target); |
| 3389 |
|
| 3390 |
if (DECL_CONSTRUCTOR_P (fun)) |
| 3391 |
/* This can be null for a subobject constructor call, in |
| 3392 |
which case what we care about is the initialization |
| 3393 |
side-effects rather than the value. We could get at the |
| 3394 |
value by evaluating *this, but we don't bother; there's |
| 3395 |
no need to put such a call in the hash table. */ |
| 3396 |
result = lval ? ctx->object : ctx->ctor; |
| 3397 |
else if (VOID_TYPE_P (TREE_TYPE (res))) |
| 3398 |
result = void_node; |
| 3399 |
else |
| 3400 |
{ |
| 3401 |
result = ctx->global->get_value (res); |
| 3402 |
if (result == NULL_TREE && !*non_constant_p |
| 3403 |
&& !DECL_DESTRUCTOR_P (fun)) |
| 3404 |
{ |
| 3405 |
if (!ctx->quiet) |
| 3406 |
error ("%<constexpr%> call flows off the end " |
| 3407 |
"of the function"); |
| 3408 |
*non_constant_p = true; |
| 3409 |
} |
| 3410 |
} |
| 3411 |
|
| 3412 |
/* At this point, the object's constructor will have run, so |
| 3413 |
the object is no longer under construction, and its possible |
| 3414 |
'const' semantics now apply. Make a note of this fact by |
| 3415 |
marking the CONSTRUCTOR TREE_READONLY. */ |
| 3416 |
if (new_obj && DECL_CONSTRUCTOR_P (fun)) |
| 3417 |
cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true, |
| 3418 |
non_constant_p, overflow_p); |
| 3419 |
|
| 3420 |
/* Remove the parms/result from the values map. */ |
| 3421 |
destroy_value_checked (ctx, res, non_constant_p); |
| 3422 |
for (tree parm = parms; parm; parm = TREE_CHAIN (parm)) |
| 3423 |
destroy_value_checked (ctx, parm, non_constant_p); |
| 3424 |
|
| 3425 |
/* Free any parameter CONSTRUCTORs we aren't returning directly. */ |
| 3426 |
while (!ctors->is_empty ()) |
| 3427 |
{ |
| 3428 |
tree c = ctors->pop (); |
| 3429 |
if (c != result) |
| 3430 |
free_constructor (c); |
| 3431 |
} |
| 3432 |
|
| 3433 |
/* Make the unshared function copy we used available for re-use. */ |
| 3434 |
save_fundef_copy (fun, copy); |
| 3435 |
|
| 3436 |
/* If the call allocated some heap object that hasn't been |
| 3437 |
deallocated during the call, or if it deallocated some heap |
| 3438 |
object it has not allocated, the call isn't really stateless |
| 3439 |
for the constexpr evaluation and should not be cached. |
| 3440 |
It is fine if the call allocates something and deallocates it |
| 3441 |
too. */ |
| 3442 |
if (cacheable |
| 3443 |
&& (save_heap_alloc_count != ctx->global->heap_vars.length () |
| 3444 |
|| (save_heap_dealloc_count |
| 3445 |
!= ctx->global->heap_dealloc_count))) |
| 3446 |
{ |
| 3447 |
tree heap_var; |
| 3448 |
unsigned int i; |
| 3449 |
if ((ctx->global->heap_vars.length () |
| 3450 |
- ctx->global->heap_dealloc_count) |
| 3451 |
!= save_heap_alloc_count - save_heap_dealloc_count) |
| 3452 |
cacheable = false; |
| 3453 |
else |
| 3454 |
FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var, |
| 3455 |
save_heap_alloc_count) |
| 3456 |
if (DECL_NAME (heap_var) != heap_deleted_identifier) |
| 3457 |
{ |
| 3458 |
cacheable = false; |
| 3459 |
break; |
| 3460 |
} |
| 3461 |
/* And don't cache a ref to a deleted heap variable (119162). */ |
| 3462 |
if (cacheable |
| 3463 |
&& (cp_walk_tree_without_duplicates |
| 3464 |
(&result, find_deleted_heap_var, NULL))) |
| 3465 |
cacheable = false; |
| 3466 |
} |
| 3467 |
|
| 3468 |
/* Rewrite all occurrences of the function's RESULT_DECL with the |
| 3469 |
current object under construction. */ |
| 3470 |
if (!*non_constant_p |
| 3471 |
&& ctx->object |
| 3472 |
&& CLASS_TYPE_P (TREE_TYPE (res)) |
| 3473 |
&& !is_empty_class (TREE_TYPE (res))) |
| 3474 |
{ |
| 3475 |
if (!same_type_ignoring_top_level_qualifiers_p |
| 3476 |
(TREE_TYPE (res), TREE_TYPE (ctx->object))) |
| 3477 |
*non_constant_p = true; |
| 3478 |
else if (replace_decl (&result, res, ctx->object)) |
| 3479 |
{ |
| 3480 |
cacheable = false; |
| 3481 |
result = cxx_eval_constant_expression (ctx, result, lval, |
| 3482 |
non_constant_p, |
| 3483 |
overflow_p); |
| 3484 |
} |
| 3485 |
} |
| 3486 |
|
| 3487 |
/* Only cache a permitted result of a constant expression. */ |
| 3488 |
if (cacheable && !reduced_constant_expression_p (result)) |
| 3489 |
cacheable = false; |
| 3490 |
} |
| 3491 |
else |
| 3492 |
/* Couldn't get a function copy to evaluate. */ |
| 3493 |
*non_constant_p = true; |
| 3494 |
|
| 3495 |
if (result == error_mark_node) |
| 3496 |
*non_constant_p = true; |
| 3497 |
if (*non_constant_p || *overflow_p) |
| 3498 |
result = error_mark_node; |
| 3499 |
else if (!result) |
| 3500 |
result = void_node; |
| 3501 |
if (entry) |
| 3502 |
{ |
| 3503 |
entry->result (ctx->manifestly_const_eval) |
| 3504 |
= cacheable ? result : error_mark_node; |
| 3505 |
|
| 3506 |
if (result != error_mark_node |
| 3507 |
&& ctx->manifestly_const_eval == mce_unknown) |
| 3508 |
{ |
| 3509 |
/* Evaluation succeeded and was independent of whether we're in a |
| 3510 |
manifestly constant-evaluated context, so we can also reuse |
| 3511 |
this result when evaluating this call with a fixed context. */ |
| 3512 |
if (!entry->result (mce_true)) |
| 3513 |
entry->result (mce_true) = entry->result (mce_unknown); |
| 3514 |
if (!entry->result (mce_false)) |
| 3515 |
entry->result (mce_false) = entry->result (mce_unknown); |
| 3516 |
} |
| 3517 |
} |
| 3518 |
} |
| 3519 |
|
| 3520 |
/* The result of a constexpr function must be completely initialized. |
| 3521 |
|
| 3522 |
However, in C++20, a constexpr constructor doesn't necessarily have |
| 3523 |
to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING |
| 3524 |
in order to detect reading an unitialized object in constexpr instead |
| 3525 |
of value-initializing it. (reduced_constant_expression_p is expected to |
| 3526 |
take care of clearing the flag.) */ |
| 3527 |
if (TREE_CODE (result) == CONSTRUCTOR |
| 3528 |
&& (cxx_dialect < cxx20 |
| 3529 |
|| !DECL_CONSTRUCTOR_P (fun))) |
| 3530 |
clear_no_implicit_zero (result); |
| 3531 |
|
| 3532 |
pop_cx_call_context (); |
| 3533 |
return result; |
| 3534 |
} |
| 3535 |
|
| 3536 |
/* Return true if T is a valid constant initializer. If a CONSTRUCTOR |
| 3537 |
initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be |
| 3538 |
cleared. |
| 3539 |
FIXME speed this up, it's taking 16% of compile time on sieve testcase. */ |
| 3540 |
|
| 3541 |
bool |
| 3542 |
reduced_constant_expression_p (tree t) |
| 3543 |
{ |
| 3544 |
if (t == NULL_TREE) |
| 3545 |
return false; |
| 3546 |
|
| 3547 |
switch (TREE_CODE (t)) |
| 3548 |
{ |
| 3549 |
case PTRMEM_CST: |
| 3550 |
/* Even if we can't lower this yet, it's constant. */ |
| 3551 |
return true; |
| 3552 |
|
| 3553 |
case CONSTRUCTOR: |
| 3554 |
/* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */ |
| 3555 |
tree field; |
| 3556 |
if (!AGGREGATE_TYPE_P (TREE_TYPE (t))) |
| 3557 |
/* A constant vector would be folded to VECTOR_CST. |
| 3558 |
A CONSTRUCTOR of scalar type means uninitialized. */ |
| 3559 |
return false; |
| 3560 |
if (CONSTRUCTOR_NO_CLEARING (t)) |
| 3561 |
{ |
| 3562 |
if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) |
| 3563 |
{ |
| 3564 |
/* There must be a valid constant initializer at every array |
| 3565 |
index. */ |
| 3566 |
tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t))); |
| 3567 |
tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t))); |
| 3568 |
tree cursor = min; |
| 3569 |
for (auto &e: CONSTRUCTOR_ELTS (t)) |
| 3570 |
{ |
| 3571 |
if (!reduced_constant_expression_p (e.value)) |
| 3572 |
return false; |
| 3573 |
if (array_index_cmp (cursor, e.index) != 0) |
| 3574 |
return false; |
| 3575 |
if (TREE_CODE (e.index) == RANGE_EXPR) |
| 3576 |
cursor = TREE_OPERAND (e.index, 1); |
| 3577 |
if (TREE_CODE (e.value) == RAW_DATA_CST) |
| 3578 |
cursor |
| 3579 |
= int_const_binop (PLUS_EXPR, cursor, |
| 3580 |
size_int (RAW_DATA_LENGTH (e.value))); |
| 3581 |
else |
| 3582 |
cursor = int_const_binop (PLUS_EXPR, cursor, |
| 3583 |
size_one_node); |
| 3584 |
} |
| 3585 |
if (find_array_ctor_elt (t, max) == -1) |
| 3586 |
return false; |
| 3587 |
goto ok; |
| 3588 |
} |
| 3589 |
else if (cxx_dialect >= cxx20 |
| 3590 |
&& TREE_CODE (TREE_TYPE (t)) == UNION_TYPE) |
| 3591 |
{ |
| 3592 |
if (CONSTRUCTOR_NELTS (t) == 0) |
| 3593 |
/* An initialized union has a constructor element. */ |
| 3594 |
return false; |
| 3595 |
/* And it only initializes one member. */ |
| 3596 |
field = NULL_TREE; |
| 3597 |
} |
| 3598 |
else |
| 3599 |
field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t))); |
| 3600 |
} |
| 3601 |
else |
| 3602 |
field = NULL_TREE; |
| 3603 |
for (auto &e: CONSTRUCTOR_ELTS (t)) |
| 3604 |
{ |
| 3605 |
/* If VAL is null, we're in the middle of initializing this |
| 3606 |
element. */ |
| 3607 |
if (!reduced_constant_expression_p (e.value)) |
| 3608 |
return false; |
| 3609 |
/* We want to remove initializers for empty fields in a struct to |
| 3610 |
avoid confusing output_constructor. */ |
| 3611 |
if (is_empty_field (e.index) |
| 3612 |
&& TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE) |
| 3613 |
return false; |
| 3614 |
/* Check for non-empty fields between initialized fields when |
| 3615 |
CONSTRUCTOR_NO_CLEARING. */ |
| 3616 |
for (; field && e.index != field; |
| 3617 |
field = next_subobject_field (DECL_CHAIN (field))) |
| 3618 |
if (!is_really_empty_class (TREE_TYPE (field), |
| 3619 |
/*ignore_vptr*/false)) |
| 3620 |
return false; |
| 3621 |
if (field) |
| 3622 |
field = next_subobject_field (DECL_CHAIN (field)); |
| 3623 |
} |
| 3624 |
/* There could be a non-empty field at the end. */ |
| 3625 |
for (; field; field = next_subobject_field (DECL_CHAIN (field))) |
| 3626 |
if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false)) |
| 3627 |
return false; |
| 3628 |
ok: |
| 3629 |
if (CONSTRUCTOR_NO_CLEARING (t)) |
| 3630 |
/* All the fields are initialized. */ |
| 3631 |
CONSTRUCTOR_NO_CLEARING (t) = false; |
| 3632 |
return true; |
| 3633 |
|
| 3634 |
default: |
| 3635 |
/* FIXME are we calling this too much? */ |
| 3636 |
return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE; |
| 3637 |
} |
| 3638 |
} |
| 3639 |
|
| 3640 |
/* *TP was not deemed constant by reduced_constant_expression_p. Explain |
| 3641 |
why and suggest what could be done about it. */ |
| 3642 |
|
| 3643 |
static tree |
| 3644 |
verify_constant_explain_r (tree *tp, int *walk_subtrees, void *) |
| 3645 |
{ |
| 3646 |
bool ref_p = false; |
| 3647 |
|
| 3648 |
/* No need to look into types or unevaluated operands. */ |
| 3649 |
if (TYPE_P (*tp) || unevaluated_p (TREE_CODE (*tp))) |
| 3650 |
{ |
| 3651 |
*walk_subtrees = false; |
| 3652 |
return NULL_TREE; |
| 3653 |
} |
| 3654 |
|
| 3655 |
switch (TREE_CODE (*tp)) |
| 3656 |
{ |
| 3657 |
CASE_CONVERT: |
| 3658 |
if (TREE_CODE (TREE_OPERAND (*tp, 0)) != ADDR_EXPR) |
| 3659 |
break; |
| 3660 |
ref_p = TYPE_REF_P (TREE_TYPE (*tp)); |
| 3661 |
*tp = TREE_OPERAND (*tp, 0); |
| 3662 |
gcc_fallthrough (); |
| 3663 |
case ADDR_EXPR: |
| 3664 |
{ |
| 3665 |
tree op = TREE_OPERAND (*tp, 0); |
| 3666 |
if (VAR_P (op) |
| 3667 |
&& DECL_DECLARED_CONSTEXPR_P (op) |
| 3668 |
&& !TREE_STATIC (op) |
| 3669 |
/* ??? We should also say something about temporaries. */ |
| 3670 |
&& !DECL_ARTIFICIAL (op)) |
| 3671 |
{ |
| 3672 |
if (ref_p) |
| 3673 |
inform (location_of (*tp), "reference to %qD is not a constant " |
| 3674 |
"expression", op); |
| 3675 |
else |
| 3676 |
inform (location_of (*tp), "pointer to %qD is not a constant " |
| 3677 |
"expression", op); |
| 3678 |
const location_t op_loc = DECL_SOURCE_LOCATION (op); |
| 3679 |
rich_location richloc (line_table, op_loc); |
| 3680 |
richloc.add_fixit_insert_before (op_loc, "static "); |
| 3681 |
inform (&richloc, |
| 3682 |
"address of non-static constexpr variable %qD may differ on " |
| 3683 |
"each invocation of the enclosing function; add %<static%> " |
| 3684 |
"to give it a constant address", op); |
| 3685 |
} |
| 3686 |
break; |
| 3687 |
} |
| 3688 |
default: |
| 3689 |
break; |
| 3690 |
} |
| 3691 |
|
| 3692 |
return NULL_TREE; |
| 3693 |
} |
| 3694 |
|
| 3695 |
/* Some expressions may have constant operands but are not constant |
| 3696 |
themselves, such as 1/0. Call this function to check for that |
| 3697 |
condition. |
| 3698 |
|
| 3699 |
We only call this in places that require an arithmetic constant, not in |
| 3700 |
places where we might have a non-constant expression that can be a |
| 3701 |
component of a constant expression, such as the address of a constexpr |
| 3702 |
variable that might be dereferenced later. */ |
| 3703 |
|
| 3704 |
static bool |
| 3705 |
verify_constant (tree t, bool allow_non_constant, bool *non_constant_p, |
| 3706 |
bool *overflow_p) |
| 3707 |
{ |
| 3708 |
if (!*non_constant_p && !reduced_constant_expression_p (t) |
| 3709 |
&& t != void_node) |
| 3710 |
{ |
| 3711 |
if (!allow_non_constant) |
| 3712 |
{ |
| 3713 |
auto_diagnostic_group d; |
| 3714 |
error_at (cp_expr_loc_or_input_loc (t), |
| 3715 |
"%q+E is not a constant expression", t); |
| 3716 |
cp_walk_tree_without_duplicates (&t, verify_constant_explain_r, |
| 3717 |
nullptr); |
| 3718 |
} |
| 3719 |
*non_constant_p = true; |
| 3720 |
} |
| 3721 |
if (TREE_OVERFLOW_P (t)) |
| 3722 |
{ |
| 3723 |
if (!allow_non_constant) |
| 3724 |
{ |
| 3725 |
permerror (input_location, "overflow in constant expression"); |
| 3726 |
/* If we're being permissive (and are in an enforcing |
| 3727 |
context), ignore the overflow. */ |
| 3728 |
if (flag_permissive) |
| 3729 |
return *non_constant_p; |
| 3730 |
} |
| 3731 |
*overflow_p = true; |
| 3732 |
} |
| 3733 |
return *non_constant_p; |
| 3734 |
} |
| 3735 |
|
| 3736 |
/* Check whether the shift operation with code CODE and type TYPE on LHS |
| 3737 |
and RHS is undefined. If it is, give an error with an explanation, |
| 3738 |
and return true; return false otherwise. */ |
| 3739 |
|
| 3740 |
static bool |
| 3741 |
cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx, |
| 3742 |
enum tree_code code, tree type, tree lhs, tree rhs) |
| 3743 |
{ |
| 3744 |
if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR) |
| 3745 |
|| TREE_CODE (lhs) != INTEGER_CST |
| 3746 |
|| TREE_CODE (rhs) != INTEGER_CST) |
| 3747 |
return false; |
| 3748 |
|
| 3749 |
tree lhstype = TREE_TYPE (lhs); |
| 3750 |
unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs)); |
| 3751 |
|
| 3752 |
/* [expr.shift] The behavior is undefined if the right operand |
| 3753 |
is negative, or greater than or equal to the length in bits |
| 3754 |
of the promoted left operand. */ |
| 3755 |
if (tree_int_cst_sgn (rhs) == -1) |
| 3756 |
{ |
| 3757 |
if (!ctx->quiet) |
| 3758 |
permerror (loc, "right operand of shift expression %q+E is negative", |
| 3759 |
build2_loc (loc, code, type, lhs, rhs)); |
| 3760 |
return (!flag_permissive || ctx->quiet); |
| 3761 |
} |
| 3762 |
if (compare_tree_int (rhs, uprec) >= 0) |
| 3763 |
{ |
| 3764 |
if (!ctx->quiet) |
| 3765 |
permerror (loc, "right operand of shift expression %q+E is greater " |
| 3766 |
"than or equal to the precision %wu of the left operand", |
| 3767 |
build2_loc (loc, code, type, lhs, rhs), uprec); |
| 3768 |
return (!flag_permissive || ctx->quiet); |
| 3769 |
} |
| 3770 |
|
| 3771 |
/* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...] |
| 3772 |
if E1 has a signed type and non-negative value, and E1x2^E2 is |
| 3773 |
representable in the corresponding unsigned type of the result type, |
| 3774 |
then that value, converted to the result type, is the resulting value; |
| 3775 |
otherwise, the behavior is undefined. |
| 3776 |
For C++20: |
| 3777 |
The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo |
| 3778 |
2^N, where N is the range exponent of the type of the result. */ |
| 3779 |
if (code == LSHIFT_EXPR |
| 3780 |
&& !TYPE_OVERFLOW_WRAPS (lhstype) |
| 3781 |
&& cxx_dialect >= cxx11 |
| 3782 |
&& cxx_dialect < cxx20) |
| 3783 |
{ |
| 3784 |
if (tree_int_cst_sgn (lhs) == -1) |
| 3785 |
{ |
| 3786 |
if (!ctx->quiet) |
| 3787 |
permerror (loc, |
| 3788 |
"left operand of shift expression %q+E is negative", |
| 3789 |
build2_loc (loc, code, type, lhs, rhs)); |
| 3790 |
return (!flag_permissive || ctx->quiet); |
| 3791 |
} |
| 3792 |
/* For signed x << y the following: |
| 3793 |
(unsigned) x >> ((prec (lhs) - 1) - y) |
| 3794 |
if > 1, is undefined. The right-hand side of this formula |
| 3795 |
is the highest bit of the LHS that can be set (starting from 0), |
| 3796 |
so that the shift doesn't overflow. We then right-shift the LHS |
| 3797 |
to see whether any other bit is set making the original shift |
| 3798 |
undefined -- the result is not representable in the corresponding |
| 3799 |
unsigned type. */ |
| 3800 |
tree t = build_int_cst (unsigned_type_node, uprec - 1); |
| 3801 |
t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs); |
| 3802 |
tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs); |
| 3803 |
t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t); |
| 3804 |
if (tree_int_cst_lt (integer_one_node, t)) |
| 3805 |
{ |
| 3806 |
if (!ctx->quiet) |
| 3807 |
permerror (loc, "shift expression %q+E overflows", |
| 3808 |
build2_loc (loc, code, type, lhs, rhs)); |
| 3809 |
return (!flag_permissive || ctx->quiet); |
| 3810 |
} |
| 3811 |
} |
| 3812 |
return false; |
| 3813 |
} |
| 3814 |
|
| 3815 |
/* Subroutine of cxx_eval_constant_expression. |
| 3816 |
Attempt to reduce the unary expression tree T to a compile time value. |
| 3817 |
If successful, return the value. Otherwise issue a diagnostic |
| 3818 |
and return error_mark_node. */ |
| 3819 |
|
| 3820 |
static tree |
| 3821 |
cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t, |
| 3822 |
bool /*lval*/, |
| 3823 |
bool *non_constant_p, bool *overflow_p) |
| 3824 |
{ |
| 3825 |
tree r; |
| 3826 |
tree orig_arg = TREE_OPERAND (t, 0); |
| 3827 |
tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue, |
| 3828 |
non_constant_p, overflow_p); |
| 3829 |
VERIFY_CONSTANT (arg); |
| 3830 |
location_t loc = EXPR_LOCATION (t); |
| 3831 |
enum tree_code code = TREE_CODE (t); |
| 3832 |
tree type = TREE_TYPE (t); |
| 3833 |
r = fold_unary_loc (loc, code, type, arg); |
| 3834 |
if (r == NULL_TREE) |
| 3835 |
{ |
| 3836 |
if (arg == orig_arg) |
| 3837 |
r = t; |
| 3838 |
else |
| 3839 |
r = build1_loc (loc, code, type, arg); |
| 3840 |
} |
| 3841 |
VERIFY_CONSTANT (r); |
| 3842 |
return r; |
| 3843 |
} |
| 3844 |
|
| 3845 |
/* Helper function for cxx_eval_binary_expression. Try to optimize |
| 3846 |
original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the |
| 3847 |
generic folding should be used. */ |
| 3848 |
|
| 3849 |
static tree |
| 3850 |
cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t, |
| 3851 |
tree lhs, tree rhs, bool *non_constant_p, |
| 3852 |
bool *overflow_p) |
| 3853 |
{ |
| 3854 |
STRIP_NOPS (lhs); |
| 3855 |
if (TREE_CODE (lhs) != ADDR_EXPR) |
| 3856 |
return NULL_TREE; |
| 3857 |
|
| 3858 |
lhs = TREE_OPERAND (lhs, 0); |
| 3859 |
|
| 3860 |
/* &A[i] p+ j => &A[i + j] */ |
| 3861 |
if (TREE_CODE (lhs) == ARRAY_REF |
| 3862 |
&& TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST |
| 3863 |
&& TREE_CODE (rhs) == INTEGER_CST |
| 3864 |
&& TYPE_SIZE_UNIT (TREE_TYPE (lhs)) |
| 3865 |
&& TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST) |
| 3866 |
{ |
| 3867 |
tree orig_type = TREE_TYPE (t); |
| 3868 |
location_t loc = EXPR_LOCATION (t); |
| 3869 |
tree type = TREE_TYPE (lhs); |
| 3870 |
|
| 3871 |
t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1)); |
| 3872 |
tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0))); |
| 3873 |
nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue, |
| 3874 |
non_constant_p, overflow_p); |
| 3875 |
if (*non_constant_p) |
| 3876 |
return NULL_TREE; |
| 3877 |
/* Don't fold an out-of-bound access. */ |
| 3878 |
if (!tree_int_cst_le (t, nelts)) |
| 3879 |
return NULL_TREE; |
| 3880 |
rhs = cp_fold_convert (ssizetype, rhs); |
| 3881 |
/* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT. |
| 3882 |
constexpr int A[1]; ... (char *)&A[0] + 1 */ |
| 3883 |
if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype, |
| 3884 |
rhs, TYPE_SIZE_UNIT (type)))) |
| 3885 |
return NULL_TREE; |
| 3886 |
/* Make sure to treat the second operand of POINTER_PLUS_EXPR |
| 3887 |
as signed. */ |
| 3888 |
rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs, |
| 3889 |
TYPE_SIZE_UNIT (type)); |
| 3890 |
t = size_binop_loc (loc, PLUS_EXPR, rhs, t); |
| 3891 |
t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0), |
| 3892 |
t, NULL_TREE, NULL_TREE); |
| 3893 |
t = cp_build_addr_expr (t, tf_warning_or_error); |
| 3894 |
t = cp_fold_convert (orig_type, t); |
| 3895 |
return cxx_eval_constant_expression (ctx, t, vc_prvalue, |
| 3896 |
non_constant_p, overflow_p); |
| 3897 |
} |
| 3898 |
|
| 3899 |
return NULL_TREE; |
| 3900 |
} |
| 3901 |
|
| 3902 |
/* Try to fold expressions like |
| 3903 |
(struct S *) (&a[0].D.2378 + 12) |
| 3904 |
into |
| 3905 |
&MEM <struct T> [(void *)&a + 12B] |
| 3906 |
This is something normally done by gimple_fold_stmt_to_constant_1 |
| 3907 |
on GIMPLE, but is undesirable on GENERIC if we are e.g. going to |
| 3908 |
dereference the address because some details are lost. |
| 3909 |
For pointer comparisons we want such folding though so that |
| 3910 |
match.pd address_compare optimization works. */ |
| 3911 |
|
| 3912 |
static tree |
| 3913 |
cxx_maybe_fold_addr_pointer_plus (tree t) |
| 3914 |
{ |
| 3915 |
while (CONVERT_EXPR_P (t) |
| 3916 |
&& POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0)))) |
| 3917 |
t = TREE_OPERAND (t, 0); |
| 3918 |
if (TREE_CODE (t) != POINTER_PLUS_EXPR) |
| 3919 |
return NULL_TREE; |
| 3920 |
tree op0 = TREE_OPERAND (t, 0); |
| 3921 |
tree op1 = TREE_OPERAND (t, 1); |
| 3922 |
if (TREE_CODE (op1) != INTEGER_CST) |
| 3923 |
return NULL_TREE; |
| 3924 |
while (CONVERT_EXPR_P (op0) |
| 3925 |
&& POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))) |
| 3926 |
op0 = TREE_OPERAND (op0, 0); |
| 3927 |
if (TREE_CODE (op0) != ADDR_EXPR) |
| 3928 |
return NULL_TREE; |
| 3929 |
op1 = fold_convert (ptr_type_node, op1); |
| 3930 |
tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1); |
| 3931 |
return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r); |
| 3932 |
} |
| 3933 |
|
| 3934 |
/* Subroutine of cxx_eval_constant_expression. |
| 3935 |
Like cxx_eval_unary_expression, except for binary expressions. */ |
| 3936 |
|
| 3937 |
static tree |
| 3938 |
cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t, |
| 3939 |
value_cat lval, |
| 3940 |
bool *non_constant_p, bool *overflow_p) |
| 3941 |
{ |
| 3942 |
tree r = NULL_TREE; |
| 3943 |
tree orig_lhs = TREE_OPERAND (t, 0); |
| 3944 |
tree orig_rhs = TREE_OPERAND (t, 1); |
| 3945 |
tree lhs, rhs; |
| 3946 |
lhs = cxx_eval_constant_expression (ctx, orig_lhs, vc_prvalue, |
| 3947 |
non_constant_p, overflow_p); |
| 3948 |
/* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer |
| 3949 |
subtraction. */ |
| 3950 |
if (*non_constant_p) |
| 3951 |
return t; |
| 3952 |
rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue, |
| 3953 |
non_constant_p, overflow_p); |
| 3954 |
if (*non_constant_p) |
| 3955 |
return t; |
| 3956 |
|
| 3957 |
location_t loc = EXPR_LOCATION (t); |
| 3958 |
enum tree_code code = TREE_CODE (t); |
| 3959 |
tree type = TREE_TYPE (t); |
| 3960 |
|
| 3961 |
if (code == EQ_EXPR || code == NE_EXPR) |
| 3962 |
{ |
| 3963 |
bool is_code_eq = (code == EQ_EXPR); |
| 3964 |
|
| 3965 |
if (TREE_CODE (lhs) == PTRMEM_CST |
| 3966 |
&& TREE_CODE (rhs) == PTRMEM_CST) |
| 3967 |
{ |
| 3968 |
tree lmem = PTRMEM_CST_MEMBER (lhs); |
| 3969 |
tree rmem = PTRMEM_CST_MEMBER (rhs); |
| 3970 |
bool eq; |
| 3971 |
if (TREE_CODE (lmem) == TREE_CODE (rmem) |
| 3972 |
&& TREE_CODE (lmem) == FIELD_DECL |
| 3973 |
&& TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE |
| 3974 |
&& same_type_p (DECL_CONTEXT (lmem), |
| 3975 |
DECL_CONTEXT (rmem))) |
| 3976 |
/* If both refer to (possibly different) members of the same union |
| 3977 |
(12.3), they compare equal. */ |
| 3978 |
eq = true; |
| 3979 |
else |
| 3980 |
eq = cp_tree_equal (lhs, rhs); |
| 3981 |
r = constant_boolean_node (eq == is_code_eq, type); |
| 3982 |
} |
| 3983 |
else if ((TREE_CODE (lhs) == PTRMEM_CST |
| 3984 |
|| TREE_CODE (rhs) == PTRMEM_CST) |
| 3985 |
&& (null_member_pointer_value_p (lhs) |
| 3986 |
|| null_member_pointer_value_p (rhs))) |
| 3987 |
r = constant_boolean_node (!is_code_eq, type); |
| 3988 |
else if (TREE_CODE (lhs) == PTRMEM_CST) |
| 3989 |
lhs = cplus_expand_constant (lhs); |
| 3990 |
else if (TREE_CODE (rhs) == PTRMEM_CST) |
| 3991 |
rhs = cplus_expand_constant (rhs); |
| 3992 |
} |
| 3993 |
if (r == NULL_TREE |
| 3994 |
&& TREE_CODE_CLASS (code) == tcc_comparison |
| 3995 |
&& POINTER_TYPE_P (TREE_TYPE (lhs))) |
| 3996 |
{ |
| 3997 |
if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs)) |
| 3998 |
lhs = fold_convert (TREE_TYPE (lhs), lhso); |
| 3999 |
if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs)) |
| 4000 |
rhs = fold_convert (TREE_TYPE (rhs), rhso); |
| 4001 |
} |
| 4002 |
if (code == POINTER_PLUS_EXPR && !*non_constant_p |
| 4003 |
&& integer_zerop (lhs) && !integer_zerop (rhs)) |
| 4004 |
{ |
| 4005 |
if (!ctx->quiet) |
| 4006 |
error ("arithmetic involving a null pointer in %qE", lhs); |
| 4007 |
*non_constant_p = true; |
| 4008 |
return t; |
| 4009 |
} |
| 4010 |
else if (code == POINTER_PLUS_EXPR) |
| 4011 |
r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p, |
| 4012 |
overflow_p); |
| 4013 |
else if (code == SPACESHIP_EXPR) |
| 4014 |
{ |
| 4015 |
r = genericize_spaceship (loc, type, lhs, rhs); |
| 4016 |
return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, |
| 4017 |
overflow_p); |
| 4018 |
} |
| 4019 |
|
| 4020 |
if (r == NULL_TREE) |
| 4021 |
{ |
| 4022 |
if (ctx->manifestly_const_eval == mce_true |
| 4023 |
&& (flag_constexpr_fp_except |
| 4024 |
|| TREE_CODE (type) != REAL_TYPE)) |
| 4025 |
{ |
| 4026 |
auto ofcc = make_temp_override (folding_cxx_constexpr, true); |
| 4027 |
r = fold_binary_initializer_loc (loc, code, type, lhs, rhs); |
| 4028 |
} |
| 4029 |
else |
| 4030 |
r = fold_binary_loc (loc, code, type, lhs, rhs); |
| 4031 |
} |
| 4032 |
|
| 4033 |
if (r == NULL_TREE |
| 4034 |
&& (code == LSHIFT_EXPR || code == RSHIFT_EXPR) |
| 4035 |
&& TREE_CODE (lhs) == INTEGER_CST |
| 4036 |
&& TREE_CODE (rhs) == INTEGER_CST |
| 4037 |
&& wi::neg_p (wi::to_wide (rhs))) |
| 4038 |
{ |
| 4039 |
/* For diagnostics and -fpermissive emulate previous behavior of |
| 4040 |
handling shifts by negative amount. */ |
| 4041 |
tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs); |
| 4042 |
if (nrhs) |
| 4043 |
r = fold_binary_loc (loc, |
| 4044 |
code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR, |
| 4045 |
type, lhs, nrhs); |
| 4046 |
} |
| 4047 |
|
| 4048 |
if (r == NULL_TREE) |
| 4049 |
{ |
| 4050 |
if (lhs == orig_lhs && rhs == orig_rhs) |
| 4051 |
r = t; |
| 4052 |
else |
| 4053 |
r = build2_loc (loc, code, type, lhs, rhs); |
| 4054 |
} |
| 4055 |
else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs)) |
| 4056 |
*non_constant_p = true; |
| 4057 |
/* Don't VERIFY_CONSTANT if this might be dealing with a pointer to |
| 4058 |
a local array in a constexpr function. */ |
| 4059 |
bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs)); |
| 4060 |
if (!ptr) |
| 4061 |
VERIFY_CONSTANT (r); |
| 4062 |
return r; |
| 4063 |
} |
| 4064 |
|
| 4065 |
/* Subroutine of cxx_eval_constant_expression. |
| 4066 |
Attempt to evaluate condition expressions. */ |
| 4067 |
|
| 4068 |
static tree |
| 4069 |
cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t, |
| 4070 |
value_cat lval, |
| 4071 |
bool *non_constant_p, bool *overflow_p, |
| 4072 |
tree *jump_target) |
| 4073 |
{ |
| 4074 |
tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
| 4075 |
vc_prvalue, |
| 4076 |
non_constant_p, overflow_p); |
| 4077 |
VERIFY_CONSTANT (val); |
| 4078 |
if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t)) |
| 4079 |
{ |
| 4080 |
/* Evaluate the condition as if it was |
| 4081 |
if (__builtin_is_constant_evaluated ()), i.e. defer it if not |
| 4082 |
ctx->manifestly_const_eval (as sometimes we try to constant evaluate |
| 4083 |
without manifestly_const_eval even expressions or parts thereof which |
| 4084 |
will later be manifestly const_eval evaluated), otherwise fold it to |
| 4085 |
true. */ |
| 4086 |
if (ctx->manifestly_const_eval == mce_unknown) |
| 4087 |
{ |
| 4088 |
*non_constant_p = true; |
| 4089 |
return t; |
| 4090 |
} |
| 4091 |
val = constant_boolean_node (ctx->manifestly_const_eval == mce_true, |
| 4092 |
boolean_type_node); |
| 4093 |
} |
| 4094 |
/* Don't VERIFY_CONSTANT the other operands. */ |
| 4095 |
const bool zero_p = integer_zerop (val); |
| 4096 |
if (zero_p) |
| 4097 |
val = TREE_OPERAND (t, 2); |
| 4098 |
else |
| 4099 |
val = TREE_OPERAND (t, 1); |
| 4100 |
if (TREE_CODE (t) == IF_STMT && !val) |
| 4101 |
val = void_node; |
| 4102 |
|
| 4103 |
/* P2564: If we aren't in immediate function context (including a manifestly |
| 4104 |
constant-evaluated expression), check any uses of immediate functions in |
| 4105 |
the arm we're discarding. But don't do this inside a call; we already |
| 4106 |
checked when parsing the function. */ |
| 4107 |
if (ctx->manifestly_const_eval != mce_true |
| 4108 |
&& !in_immediate_context () |
| 4109 |
&& !ctx->call |
| 4110 |
&& cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2), |
| 4111 |
ctx->manifestly_const_eval)) |
| 4112 |
{ |
| 4113 |
*non_constant_p = true; |
| 4114 |
return t; |
| 4115 |
} |
| 4116 |
|
| 4117 |
/* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still |
| 4118 |
serve as the initializer for the same object as the outer TARGET_EXPR, |
| 4119 |
as in |
| 4120 |
A a = true ? A{} : A{}; |
| 4121 |
so strip the inner TARGET_EXPR so we don't materialize a temporary. */ |
| 4122 |
if (TREE_CODE (val) == TARGET_EXPR) |
| 4123 |
val = TARGET_EXPR_INITIAL (val); |
| 4124 |
return cxx_eval_constant_expression (ctx, val, lval, non_constant_p, |
| 4125 |
overflow_p, jump_target); |
| 4126 |
} |
| 4127 |
|
| 4128 |
/* Subroutine of cxx_eval_constant_expression. |
| 4129 |
Attempt to evaluate vector condition expressions. Unlike |
| 4130 |
cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal |
| 4131 |
ternary arithmetics operation, where all 3 arguments have to be |
| 4132 |
evaluated as constants and then folding computes the result from |
| 4133 |
them. */ |
| 4134 |
|
| 4135 |
static tree |
| 4136 |
cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t, |
| 4137 |
bool *non_constant_p, bool *overflow_p) |
| 4138 |
{ |
| 4139 |
tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
| 4140 |
vc_prvalue, |
| 4141 |
non_constant_p, overflow_p); |
| 4142 |
VERIFY_CONSTANT (arg1); |
| 4143 |
tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
| 4144 |
vc_prvalue, |
| 4145 |
non_constant_p, overflow_p); |
| 4146 |
VERIFY_CONSTANT (arg2); |
| 4147 |
tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2), |
| 4148 |
vc_prvalue, |
| 4149 |
non_constant_p, overflow_p); |
| 4150 |
VERIFY_CONSTANT (arg3); |
| 4151 |
location_t loc = EXPR_LOCATION (t); |
| 4152 |
tree type = TREE_TYPE (t); |
| 4153 |
tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); |
| 4154 |
if (r == NULL_TREE) |
| 4155 |
{ |
| 4156 |
if (arg1 == TREE_OPERAND (t, 0) |
| 4157 |
&& arg2 == TREE_OPERAND (t, 1) |
| 4158 |
&& arg3 == TREE_OPERAND (t, 2)) |
| 4159 |
r = t; |
| 4160 |
else |
| 4161 |
r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3); |
| 4162 |
} |
| 4163 |
VERIFY_CONSTANT (r); |
| 4164 |
return r; |
| 4165 |
} |
| 4166 |
|
| 4167 |
/* Returns less than, equal to, or greater than zero if KEY is found to be |
| 4168 |
less than, to match, or to be greater than the constructor_elt's INDEX. */ |
| 4169 |
|
| 4170 |
static int |
| 4171 |
array_index_cmp (tree key, tree index) |
| 4172 |
{ |
| 4173 |
gcc_assert (TREE_CODE (key) == INTEGER_CST); |
| 4174 |
|
| 4175 |
switch (TREE_CODE (index)) |
| 4176 |
{ |
| 4177 |
case INTEGER_CST: |
| 4178 |
return tree_int_cst_compare (key, index); |
| 4179 |
case RANGE_EXPR: |
| 4180 |
{ |
| 4181 |
tree lo = TREE_OPERAND (index, 0); |
| 4182 |
tree hi = TREE_OPERAND (index, 1); |
| 4183 |
if (tree_int_cst_lt (key, lo)) |
| 4184 |
return -1; |
| 4185 |
else if (tree_int_cst_lt (hi, key)) |
| 4186 |
return 1; |
| 4187 |
else |
| 4188 |
return 0; |
| 4189 |
} |
| 4190 |
default: |
| 4191 |
gcc_unreachable (); |
| 4192 |
} |
| 4193 |
} |
| 4194 |
|
| 4195 |
/* Extract a single INTEGER_CST from RAW_DATA_CST RAW_DATA at |
| 4196 |
relative index OFF. */ |
| 4197 |
|
| 4198 |
static tree |
| 4199 |
raw_data_cst_elt (tree raw_data, unsigned int off) |
| 4200 |
{ |
| 4201 |
return build_int_cst (TREE_TYPE (raw_data), |
| 4202 |
TYPE_UNSIGNED (TREE_TYPE (raw_data)) |
| 4203 |
? (HOST_WIDE_INT) |
| 4204 |
RAW_DATA_UCHAR_ELT (raw_data, off) |
| 4205 |
: (HOST_WIDE_INT) |
| 4206 |
RAW_DATA_SCHAR_ELT (raw_data, off)); |
| 4207 |
} |
| 4208 |
|
| 4209 |
/* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1 |
| 4210 |
if none. If INSERT is true, insert a matching element rather than fail. */ |
| 4211 |
|
| 4212 |
static HOST_WIDE_INT |
| 4213 |
find_array_ctor_elt (tree ary, tree dindex, bool insert) |
| 4214 |
{ |
| 4215 |
if (tree_int_cst_sgn (dindex) < 0) |
| 4216 |
return -1; |
| 4217 |
|
| 4218 |
unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex); |
| 4219 |
vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary); |
| 4220 |
unsigned HOST_WIDE_INT len = vec_safe_length (elts); |
| 4221 |
|
| 4222 |
unsigned HOST_WIDE_INT end = len; |
| 4223 |
unsigned HOST_WIDE_INT begin = 0; |
| 4224 |
|
| 4225 |
/* If the last element of the CONSTRUCTOR has its own index, we can assume |
| 4226 |
that the same is true of the other elements and index directly. */ |
| 4227 |
if (end > 0) |
| 4228 |
{ |
| 4229 |
tree cindex = (*elts)[end - 1].index; |
| 4230 |
if (cindex == NULL_TREE) |
| 4231 |
{ |
| 4232 |
/* Verify that if the last index is missing, all indexes |
| 4233 |
are missing and there is no RAW_DATA_CST. */ |
| 4234 |
if (flag_checking) |
| 4235 |
for (unsigned int j = 0; j < len - 1; ++j) |
| 4236 |
gcc_assert ((*elts)[j].index == NULL_TREE |
| 4237 |
&& TREE_CODE ((*elts)[j].value) != RAW_DATA_CST); |
| 4238 |
if (i < end) |
| 4239 |
return i; |
| 4240 |
else |
| 4241 |
{ |
| 4242 |
begin = end; |
| 4243 |
if (i == end) |
| 4244 |
/* If the element is to be added right at the end, |
| 4245 |
make sure it is added with cleared index too. */ |
| 4246 |
dindex = NULL_TREE; |
| 4247 |
else if (insert) |
| 4248 |
/* Otherwise, in order not to break the assumption |
| 4249 |
that CONSTRUCTOR either has all indexes or none, |
| 4250 |
we need to add indexes to all elements. */ |
| 4251 |
for (unsigned int j = 0; j < len; ++j) |
| 4252 |
(*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j); |
| 4253 |
} |
| 4254 |
} |
| 4255 |
else if (TREE_CODE (cindex) == INTEGER_CST |
| 4256 |
&& compare_tree_int (cindex, end - 1) == 0) |
| 4257 |
{ |
| 4258 |
tree value = (*elts)[end - 1].value; |
| 4259 |
if (i < end) |
| 4260 |
{ |
| 4261 |
if (i == end - 1 && TREE_CODE (value) == RAW_DATA_CST) |
| 4262 |
begin = end - 1; |
| 4263 |
else |
| 4264 |
return i; |
| 4265 |
} |
| 4266 |
else if (TREE_CODE (value) == RAW_DATA_CST |
| 4267 |
&& wi::to_offset (dindex) < (wi::to_offset (cindex) |
| 4268 |
+ RAW_DATA_LENGTH (value))) |
| 4269 |
begin = end - 1; |
| 4270 |
else |
| 4271 |
begin = end; |
| 4272 |
} |
| 4273 |
} |
| 4274 |
|
| 4275 |
/* Otherwise, find a matching index by means of a binary search. */ |
| 4276 |
while (begin != end) |
| 4277 |
{ |
| 4278 |
unsigned HOST_WIDE_INT middle = (begin + end) / 2; |
| 4279 |
constructor_elt &elt = (*elts)[middle]; |
| 4280 |
tree idx = elt.index; |
| 4281 |
|
| 4282 |
int cmp = array_index_cmp (dindex, idx); |
| 4283 |
if (cmp > 0 |
| 4284 |
&& TREE_CODE (elt.value) == RAW_DATA_CST |
| 4285 |
&& wi::to_offset (dindex) < (wi::to_offset (idx) |
| 4286 |
+ RAW_DATA_LENGTH (elt.value))) |
| 4287 |
cmp = 0; |
| 4288 |
if (cmp < 0) |
| 4289 |
end = middle; |
| 4290 |
else if (cmp > 0) |
| 4291 |
begin = middle + 1; |
| 4292 |
else |
| 4293 |
{ |
| 4294 |
if (insert && TREE_CODE (elt.value) == RAW_DATA_CST) |
| 4295 |
{ |
| 4296 |
/* We need to split the RAW_DATA_CST elt. */ |
| 4297 |
constructor_elt e; |
| 4298 |
gcc_checking_assert (TREE_CODE (idx) != RANGE_EXPR); |
| 4299 |
unsigned int off = (wi::to_offset (dindex) |
| 4300 |
- wi::to_offset (idx)).to_uhwi (); |
| 4301 |
tree value = elt.value; |
| 4302 |
unsigned int len = RAW_DATA_LENGTH (value); |
| 4303 |
if (off > 1 && len >= off + 3) |
| 4304 |
value = copy_node (elt.value); |
| 4305 |
if (off) |
| 4306 |
{ |
| 4307 |
if (off > 1) |
| 4308 |
RAW_DATA_LENGTH (elt.value) = off; |
| 4309 |
else |
| 4310 |
elt.value = raw_data_cst_elt (elt.value, 0); |
| 4311 |
e.index = size_binop (PLUS_EXPR, elt.index, |
| 4312 |
build_int_cst (TREE_TYPE (elt.index), |
| 4313 |
off)); |
| 4314 |
e.value = NULL_TREE; |
| 4315 |
++middle; |
| 4316 |
vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e); |
| 4317 |
} |
| 4318 |
(*elts)[middle].value = raw_data_cst_elt (value, off); |
| 4319 |
if (len >= off + 2) |
| 4320 |
{ |
| 4321 |
e.index = (*elts)[middle].index; |
| 4322 |
e.index = size_binop (PLUS_EXPR, e.index, |
| 4323 |
build_one_cst (TREE_TYPE (e.index))); |
| 4324 |
if (len >= off + 3) |
| 4325 |
{ |
| 4326 |
RAW_DATA_LENGTH (value) -= off + 1; |
| 4327 |
RAW_DATA_POINTER (value) += off + 1; |
| 4328 |
e.value = value; |
| 4329 |
} |
| 4330 |
else |
| 4331 |
e.value = raw_data_cst_elt (value, off + 1); |
| 4332 |
vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e); |
| 4333 |
} |
| 4334 |
return middle; |
| 4335 |
} |
| 4336 |
if (insert && TREE_CODE (idx) == RANGE_EXPR) |
| 4337 |
{ |
| 4338 |
/* We need to split the range. */ |
| 4339 |
constructor_elt e; |
| 4340 |
tree lo = TREE_OPERAND (idx, 0); |
| 4341 |
tree hi = TREE_OPERAND (idx, 1); |
| 4342 |
tree value = elt.value; |
| 4343 |
dindex = fold_convert (sizetype, dindex); |
| 4344 |
if (tree_int_cst_lt (lo, dindex)) |
| 4345 |
{ |
| 4346 |
/* There are still some lower elts; shorten the range. */ |
| 4347 |
tree new_hi = int_const_binop (MINUS_EXPR, dindex, |
| 4348 |
size_one_node); |
| 4349 |
if (tree_int_cst_equal (lo, new_hi)) |
| 4350 |
/* Only one element left, no longer a range. */ |
| 4351 |
elt.index = lo; |
| 4352 |
else |
| 4353 |
TREE_OPERAND (idx, 1) = new_hi; |
| 4354 |
/* Append the element we want to insert. */ |
| 4355 |
++middle; |
| 4356 |
e.index = dindex; |
| 4357 |
e.value = unshare_constructor (value); |
| 4358 |
vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e); |
| 4359 |
} |
| 4360 |
else |
| 4361 |
/* No lower elts, the range elt is now ours. */ |
| 4362 |
elt.index = dindex; |
| 4363 |
|
| 4364 |
if (tree_int_cst_lt (dindex, hi)) |
| 4365 |
{ |
| 4366 |
/* There are still some higher elts; append a range. */ |
| 4367 |
tree new_lo = int_const_binop (PLUS_EXPR, dindex, |
| 4368 |
size_one_node); |
| 4369 |
if (tree_int_cst_equal (new_lo, hi)) |
| 4370 |
e.index = hi; |
| 4371 |
else |
| 4372 |
e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi); |
| 4373 |
e.value = unshare_constructor (value); |
| 4374 |
vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e); |
| 4375 |
} |
| 4376 |
} |
| 4377 |
return middle; |
| 4378 |
} |
| 4379 |
} |
| 4380 |
|
| 4381 |
if (insert) |
| 4382 |
{ |
| 4383 |
constructor_elt e = { dindex, NULL_TREE }; |
| 4384 |
vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e); |
| 4385 |
return end; |
| 4386 |
} |
| 4387 |
|
| 4388 |
return -1; |
| 4389 |
} |
| 4390 |
|
| 4391 |
/* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no |
| 4392 |
matching constructor_elt exists, then add one to CTOR. |
| 4393 |
|
| 4394 |
As an optimization, if POS_HINT is non-negative then it is used as a guess |
| 4395 |
for the (integer) index of the matching constructor_elt within CTOR. */ |
| 4396 |
|
| 4397 |
static constructor_elt * |
| 4398 |
get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1) |
| 4399 |
{ |
| 4400 |
/* Check the hint first. */ |
| 4401 |
if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor) |
| 4402 |
&& CONSTRUCTOR_ELT (ctor, pos_hint)->index == index) |
| 4403 |
return CONSTRUCTOR_ELT (ctor, pos_hint); |
| 4404 |
|
| 4405 |
tree type = TREE_TYPE (ctor); |
| 4406 |
if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE) |
| 4407 |
{ |
| 4408 |
CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE); |
| 4409 |
return &CONSTRUCTOR_ELTS (ctor)->last(); |
| 4410 |
} |
| 4411 |
else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE) |
| 4412 |
{ |
| 4413 |
if (TREE_CODE (index) == RANGE_EXPR) |
| 4414 |
{ |
| 4415 |
/* Support for RANGE_EXPR index lookups is currently limited to |
| 4416 |
accessing an existing element via POS_HINT, or appending a new |
| 4417 |
element to the end of CTOR. ??? Support for other access |
| 4418 |
patterns may also be needed. */ |
| 4419 |
vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor); |
| 4420 |
if (vec_safe_length (elts)) |
| 4421 |
{ |
| 4422 |
tree lo = TREE_OPERAND (index, 0); |
| 4423 |
gcc_assert (array_index_cmp (elts->last().index, lo) < 0); |
| 4424 |
} |
| 4425 |
CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE); |
| 4426 |
return &elts->last(); |
| 4427 |
} |
| 4428 |
|
| 4429 |
HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, /*insert*/true); |
| 4430 |
gcc_assert (i >= 0); |
| 4431 |
constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i); |
| 4432 |
gcc_assert (cep->index == NULL_TREE |
| 4433 |
|| TREE_CODE (cep->index) != RANGE_EXPR); |
| 4434 |
return cep; |
| 4435 |
} |
| 4436 |
else |
| 4437 |
{ |
| 4438 |
gcc_assert (TREE_CODE (index) == FIELD_DECL |
| 4439 |
&& (same_type_ignoring_top_level_qualifiers_p |
| 4440 |
(DECL_CONTEXT (index), TREE_TYPE (ctor)))); |
| 4441 |
|
| 4442 |
/* We must keep the CONSTRUCTOR's ELTS in FIELD order. |
| 4443 |
Usually we meet initializers in that order, but it is |
| 4444 |
possible for base types to be placed not in program |
| 4445 |
order. */ |
| 4446 |
tree fields = TYPE_FIELDS (DECL_CONTEXT (index)); |
| 4447 |
unsigned HOST_WIDE_INT idx = 0; |
| 4448 |
constructor_elt *cep = NULL; |
| 4449 |
|
| 4450 |
/* Check if we're changing the active member of a union. */ |
| 4451 |
if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor) |
| 4452 |
&& CONSTRUCTOR_ELT (ctor, 0)->index != index) |
| 4453 |
vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0); |
| 4454 |
/* If the bit offset of INDEX is larger than that of the last |
| 4455 |
constructor_elt, then we can just immediately append a new |
| 4456 |
constructor_elt to the end of CTOR. */ |
| 4457 |
else if (CONSTRUCTOR_NELTS (ctor) |
| 4458 |
&& tree_int_cst_compare (bit_position (index), |
| 4459 |
bit_position (CONSTRUCTOR_ELTS (ctor) |
| 4460 |
->last().index)) > 0) |
| 4461 |
{ |
| 4462 |
idx = CONSTRUCTOR_NELTS (ctor); |
| 4463 |
goto insert; |
| 4464 |
} |
| 4465 |
|
| 4466 |
/* Otherwise, we need to iterate over CTOR to find or insert INDEX |
| 4467 |
appropriately. */ |
| 4468 |
|
| 4469 |
for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep); |
| 4470 |
idx++, fields = DECL_CHAIN (fields)) |
| 4471 |
{ |
| 4472 |
if (index == cep->index) |
| 4473 |
goto found; |
| 4474 |
|
| 4475 |
/* The field we're initializing must be on the field |
| 4476 |
list. Look to see if it is present before the |
| 4477 |
field the current ELT initializes. */ |
| 4478 |
for (; fields != cep->index; fields = DECL_CHAIN (fields)) |
| 4479 |
if (index == fields) |
| 4480 |
goto insert; |
| 4481 |
} |
| 4482 |
/* We fell off the end of the CONSTRUCTOR, so insert a new |
| 4483 |
entry at the end. */ |
| 4484 |
|
| 4485 |
insert: |
| 4486 |
{ |
| 4487 |
constructor_elt ce = { index, NULL_TREE }; |
| 4488 |
|
| 4489 |
vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce); |
| 4490 |
cep = CONSTRUCTOR_ELT (ctor, idx); |
| 4491 |
} |
| 4492 |
found:; |
| 4493 |
|
| 4494 |
return cep; |
| 4495 |
} |
| 4496 |
} |
| 4497 |
|
| 4498 |
/* Under the control of CTX, issue a detailed diagnostic for |
| 4499 |
an out-of-bounds subscript INDEX into the expression ARRAY. */ |
| 4500 |
|
| 4501 |
static void |
| 4502 |
diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index) |
| 4503 |
{ |
| 4504 |
if (!ctx->quiet) |
| 4505 |
{ |
| 4506 |
tree arraytype = TREE_TYPE (array); |
| 4507 |
|
| 4508 |
/* Convert the unsigned array subscript to a signed integer to avoid |
| 4509 |
printing huge numbers for small negative values. */ |
| 4510 |
tree sidx = fold_convert (ssizetype, index); |
| 4511 |
STRIP_ANY_LOCATION_WRAPPER (array); |
| 4512 |
if (DECL_P (array)) |
| 4513 |
{ |
| 4514 |
auto_diagnostic_group d; |
| 4515 |
if (TYPE_DOMAIN (arraytype)) |
| 4516 |
error_at (loc, "array subscript value %qE is outside the bounds " |
| 4517 |
"of array %qD of type %qT", sidx, array, arraytype); |
| 4518 |
else |
| 4519 |
error_at (loc, "nonzero array subscript %qE is used with array %qD of " |
| 4520 |
"type %qT with unknown bounds", sidx, array, arraytype); |
| 4521 |
inform (DECL_SOURCE_LOCATION (array), "declared here"); |
| 4522 |
} |
| 4523 |
else if (TYPE_DOMAIN (arraytype)) |
| 4524 |
error_at (loc, "array subscript value %qE is outside the bounds " |
| 4525 |
"of array type %qT", sidx, arraytype); |
| 4526 |
else |
| 4527 |
error_at (loc, "nonzero array subscript %qE is used with array of type %qT " |
| 4528 |
"with unknown bounds", sidx, arraytype); |
| 4529 |
} |
| 4530 |
} |
| 4531 |
|
| 4532 |
/* Return the number of elements for TYPE (which is an ARRAY_TYPE or |
| 4533 |
a VECTOR_TYPE). */ |
| 4534 |
|
| 4535 |
static tree |
| 4536 |
get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type, |
| 4537 |
bool *non_constant_p, bool *overflow_p) |
| 4538 |
{ |
| 4539 |
tree nelts; |
| 4540 |
if (TREE_CODE (type) == ARRAY_TYPE) |
| 4541 |
{ |
| 4542 |
if (TYPE_DOMAIN (type)) |
| 4543 |
nelts = array_type_nelts_top (type); |
| 4544 |
else |
| 4545 |
nelts = size_zero_node; |
| 4546 |
} |
| 4547 |
else if (VECTOR_TYPE_P (type)) |
| 4548 |
nelts = size_int (TYPE_VECTOR_SUBPARTS (type)); |
| 4549 |
else |
| 4550 |
gcc_unreachable (); |
| 4551 |
|
| 4552 |
/* For VLAs, the number of elements won't be an integer constant. */ |
| 4553 |
nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue, |
| 4554 |
non_constant_p, overflow_p); |
| 4555 |
return nelts; |
| 4556 |
} |
| 4557 |
|
| 4558 |
/* Extract element INDEX consisting of CHARS_PER_ELT chars from |
| 4559 |
STRING_CST STRING. */ |
| 4560 |
|
| 4561 |
static tree |
| 4562 |
extract_string_elt (tree string, unsigned chars_per_elt, unsigned index) |
| 4563 |
{ |
| 4564 |
tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string))); |
| 4565 |
tree r; |
| 4566 |
|
| 4567 |
if (chars_per_elt == 1) |
| 4568 |
r = build_int_cst (type, TREE_STRING_POINTER (string)[index]); |
| 4569 |
else |
| 4570 |
{ |
| 4571 |
const unsigned char *ptr |
| 4572 |
= ((const unsigned char *)TREE_STRING_POINTER (string) |
| 4573 |
+ index * chars_per_elt); |
| 4574 |
r = native_interpret_expr (type, ptr, chars_per_elt); |
| 4575 |
} |
| 4576 |
return r; |
| 4577 |
} |
| 4578 |
|
| 4579 |
/* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the |
| 4580 |
subscript, diagnose any problems with it, and return the result. */ |
| 4581 |
|
| 4582 |
static tree |
| 4583 |
eval_and_check_array_index (const constexpr_ctx *ctx, |
| 4584 |
tree t, bool allow_one_past, |
| 4585 |
bool *non_constant_p, bool *overflow_p) |
| 4586 |
{ |
| 4587 |
location_t loc = cp_expr_loc_or_input_loc (t); |
| 4588 |
tree ary = TREE_OPERAND (t, 0); |
| 4589 |
t = TREE_OPERAND (t, 1); |
| 4590 |
tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue, |
| 4591 |
non_constant_p, overflow_p); |
| 4592 |
VERIFY_CONSTANT (index); |
| 4593 |
|
| 4594 |
if (!tree_fits_shwi_p (index) |
| 4595 |
|| tree_int_cst_sgn (index) < 0) |
| 4596 |
{ |
| 4597 |
diag_array_subscript (loc, ctx, ary, index); |
| 4598 |
*non_constant_p = true; |
| 4599 |
return t; |
| 4600 |
} |
| 4601 |
|
| 4602 |
tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p, |
| 4603 |
overflow_p); |
| 4604 |
VERIFY_CONSTANT (nelts); |
| 4605 |
if (allow_one_past |
| 4606 |
? !tree_int_cst_le (index, nelts) |
| 4607 |
: !tree_int_cst_lt (index, nelts)) |
| 4608 |
{ |
| 4609 |
diag_array_subscript (loc, ctx, ary, index); |
| 4610 |
*non_constant_p = true; |
| 4611 |
return t; |
| 4612 |
} |
| 4613 |
|
| 4614 |
return index; |
| 4615 |
} |
| 4616 |
|
| 4617 |
/* Subroutine of cxx_eval_constant_expression. |
| 4618 |
Attempt to reduce a reference to an array slot. */ |
| 4619 |
|
| 4620 |
static tree |
| 4621 |
cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, |
| 4622 |
value_cat lval, |
| 4623 |
bool *non_constant_p, bool *overflow_p) |
| 4624 |
{ |
| 4625 |
tree oldary = TREE_OPERAND (t, 0); |
| 4626 |
tree ary = cxx_eval_constant_expression (ctx, oldary, |
| 4627 |
lval, |
| 4628 |
non_constant_p, overflow_p); |
| 4629 |
if (*non_constant_p) |
| 4630 |
return t; |
| 4631 |
if (!lval |
| 4632 |
&& TREE_CODE (ary) == VIEW_CONVERT_EXPR |
| 4633 |
&& VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0))) |
| 4634 |
&& (TYPE_MAIN_VARIANT (TREE_TYPE (t)) |
| 4635 |
== TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0)))))) |
| 4636 |
ary = TREE_OPERAND (ary, 0); |
| 4637 |
|
| 4638 |
tree oldidx = TREE_OPERAND (t, 1); |
| 4639 |
tree index = eval_and_check_array_index (ctx, t, lval, |
| 4640 |
non_constant_p, overflow_p); |
| 4641 |
if (*non_constant_p) |
| 4642 |
return t; |
| 4643 |
|
| 4644 |
if (lval && ary == oldary && index == oldidx) |
| 4645 |
return t; |
| 4646 |
else if (lval == vc_discard) |
| 4647 |
return t; |
| 4648 |
else if (lval) |
| 4649 |
return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL); |
| 4650 |
|
| 4651 |
unsigned len = 0, elem_nchars = 1; |
| 4652 |
tree elem_type = TREE_TYPE (TREE_TYPE (ary)); |
| 4653 |
if (TREE_CODE (ary) == CONSTRUCTOR) |
| 4654 |
len = CONSTRUCTOR_NELTS (ary); |
| 4655 |
else if (TREE_CODE (ary) == STRING_CST) |
| 4656 |
{ |
| 4657 |
elem_nchars = (TYPE_PRECISION (elem_type) |
| 4658 |
/ TYPE_PRECISION (char_type_node)); |
| 4659 |
len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars; |
| 4660 |
} |
| 4661 |
else if (TREE_CODE (ary) == VECTOR_CST) |
| 4662 |
/* We don't create variable-length VECTOR_CSTs. */ |
| 4663 |
len = VECTOR_CST_NELTS (ary).to_constant (); |
| 4664 |
else |
| 4665 |
{ |
| 4666 |
/* We can't do anything with other tree codes, so use |
| 4667 |
VERIFY_CONSTANT to complain and fail. */ |
| 4668 |
VERIFY_CONSTANT (ary); |
| 4669 |
gcc_unreachable (); |
| 4670 |
} |
| 4671 |
|
| 4672 |
bool found; |
| 4673 |
HOST_WIDE_INT i = 0; |
| 4674 |
if (TREE_CODE (ary) == CONSTRUCTOR) |
| 4675 |
{ |
| 4676 |
HOST_WIDE_INT ix = find_array_ctor_elt (ary, index); |
| 4677 |
found = (ix >= 0); |
| 4678 |
if (found) |
| 4679 |
i = ix; |
| 4680 |
} |
| 4681 |
else |
| 4682 |
{ |
| 4683 |
i = tree_to_shwi (index); |
| 4684 |
found = (i < len); |
| 4685 |
} |
| 4686 |
|
| 4687 |
if (found) |
| 4688 |
{ |
| 4689 |
tree r; |
| 4690 |
if (TREE_CODE (ary) == CONSTRUCTOR) |
| 4691 |
{ |
| 4692 |
r = (*CONSTRUCTOR_ELTS (ary))[i].value; |
| 4693 |
if (TREE_CODE (r) == RAW_DATA_CST) |
| 4694 |
{ |
| 4695 |
tree ridx = (*CONSTRUCTOR_ELTS (ary))[i].index; |
| 4696 |
gcc_checking_assert (ridx); |
| 4697 |
unsigned int off |
| 4698 |
= (wi::to_offset (index) - wi::to_offset (ridx)).to_uhwi (); |
| 4699 |
r = raw_data_cst_elt (r, off); |
| 4700 |
} |
| 4701 |
} |
| 4702 |
else if (TREE_CODE (ary) == VECTOR_CST) |
| 4703 |
r = VECTOR_CST_ELT (ary, i); |
| 4704 |
else |
| 4705 |
r = extract_string_elt (ary, elem_nchars, i); |
| 4706 |
|
| 4707 |
if (r) |
| 4708 |
/* Don't VERIFY_CONSTANT here. */ |
| 4709 |
return r; |
| 4710 |
|
| 4711 |
/* Otherwise the element doesn't have a value yet. */ |
| 4712 |
} |
| 4713 |
|
| 4714 |
/* Not found. */ |
| 4715 |
|
| 4716 |
if (is_really_empty_class (elem_type, /*ignore_vptr*/false)) |
| 4717 |
return build_constructor (elem_type, NULL); |
| 4718 |
|
| 4719 |
if (TREE_CODE (ary) == CONSTRUCTOR |
| 4720 |
&& CONSTRUCTOR_NO_CLEARING (ary)) |
| 4721 |
{ |
| 4722 |
/* 'ary' is part of the aggregate initializer we're currently |
| 4723 |
building; if there's no initializer for this element yet, |
| 4724 |
that's an error. */ |
| 4725 |
if (!ctx->quiet) |
| 4726 |
error ("accessing uninitialized array element"); |
| 4727 |
*non_constant_p = true; |
| 4728 |
return t; |
| 4729 |
} |
| 4730 |
|
| 4731 |
/* If it's within the array bounds but doesn't have an explicit |
| 4732 |
initializer, it's initialized from {}. But use build_value_init |
| 4733 |
directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */ |
| 4734 |
tree val; |
| 4735 |
constexpr_ctx new_ctx; |
| 4736 |
if (CP_AGGREGATE_TYPE_P (elem_type)) |
| 4737 |
{ |
| 4738 |
tree empty_ctor = build_constructor (init_list_type_node, NULL); |
| 4739 |
val = digest_init (elem_type, empty_ctor, tf_warning_or_error); |
| 4740 |
} |
| 4741 |
else |
| 4742 |
val = build_value_init (elem_type, tf_warning_or_error); |
| 4743 |
|
| 4744 |
/* Create a new constructor only if we don't already have a suitable one. */ |
| 4745 |
const bool new_ctor = (!SCALAR_TYPE_P (elem_type) |
| 4746 |
&& (!ctx->ctor |
| 4747 |
|| !same_type_ignoring_top_level_qualifiers_p |
| 4748 |
(elem_type, TREE_TYPE (ctx->ctor)))); |
| 4749 |
if (new_ctor) |
| 4750 |
{ |
| 4751 |
new_ctx = *ctx; |
| 4752 |
/* We clear the object here. We used to replace it with T, but that |
| 4753 |
caused problems (101371, 108158); and anyway, T is the initializer, |
| 4754 |
not the target object. */ |
| 4755 |
new_ctx.object = NULL_TREE; |
| 4756 |
new_ctx.ctor = build_constructor (elem_type, NULL); |
| 4757 |
ctx = &new_ctx; |
| 4758 |
} |
| 4759 |
t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p, |
| 4760 |
overflow_p); |
| 4761 |
if (new_ctor && t != ctx->ctor) |
| 4762 |
free_constructor (ctx->ctor); |
| 4763 |
return t; |
| 4764 |
} |
| 4765 |
|
| 4766 |
/* Subroutine of cxx_eval_constant_expression. |
| 4767 |
Attempt to reduce a field access of a value of class type. */ |
| 4768 |
|
| 4769 |
static tree |
| 4770 |
cxx_eval_component_reference (const constexpr_ctx *ctx, tree t, |
| 4771 |
value_cat lval, |
| 4772 |
bool *non_constant_p, bool *overflow_p) |
| 4773 |
{ |
| 4774 |
unsigned HOST_WIDE_INT i; |
| 4775 |
tree field; |
| 4776 |
tree value; |
| 4777 |
tree part = TREE_OPERAND (t, 1); |
| 4778 |
tree orig_whole = TREE_OPERAND (t, 0); |
| 4779 |
tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
| 4780 |
lval, |
| 4781 |
non_constant_p, overflow_p); |
| 4782 |
if (*non_constant_p) |
| 4783 |
return t; |
| 4784 |
if (INDIRECT_REF_P (whole) |
| 4785 |
&& integer_zerop (TREE_OPERAND (whole, 0))) |
| 4786 |
{ |
| 4787 |
if (!ctx->quiet) |
| 4788 |
error ("dereferencing a null pointer in %qE", orig_whole); |
| 4789 |
*non_constant_p = true; |
| 4790 |
return t; |
| 4791 |
} |
| 4792 |
|
| 4793 |
if (TREE_CODE (whole) == PTRMEM_CST) |
| 4794 |
whole = cplus_expand_constant (whole); |
| 4795 |
if (whole == orig_whole) |
| 4796 |
return t; |
| 4797 |
if (lval == vc_discard) |
| 4798 |
return t; |
| 4799 |
if (lval) |
| 4800 |
return fold_build3 (COMPONENT_REF, TREE_TYPE (t), |
| 4801 |
whole, part, NULL_TREE); |
| 4802 |
/* Don't VERIFY_CONSTANT here; we only want to check that we got a |
| 4803 |
CONSTRUCTOR. */ |
| 4804 |
if (TREE_CODE (whole) != CONSTRUCTOR) |
| 4805 |
{ |
| 4806 |
if (!ctx->quiet) |
| 4807 |
error ("%qE is not a constant expression", orig_whole); |
| 4808 |
*non_constant_p = true; |
| 4809 |
return t; |
| 4810 |
} |
| 4811 |
if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole)) |
| 4812 |
&& DECL_MUTABLE_P (part)) |
| 4813 |
{ |
| 4814 |
if (!ctx->quiet) |
| 4815 |
error ("mutable %qD is not usable in a constant expression", part); |
| 4816 |
*non_constant_p = true; |
| 4817 |
return t; |
| 4818 |
} |
| 4819 |
|
| 4820 |
bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole)); |
| 4821 |
FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) |
| 4822 |
{ |
| 4823 |
/* Use name match for PMF fields, as a variant will have a |
| 4824 |
different FIELD_DECL with a different type. */ |
| 4825 |
if (pmf ? DECL_NAME (field) == DECL_NAME (part) |
| 4826 |
: field == part) |
| 4827 |
{ |
| 4828 |
if (value) |
| 4829 |
{ |
| 4830 |
STRIP_ANY_LOCATION_WRAPPER (value); |
| 4831 |
return value; |
| 4832 |
} |
| 4833 |
else |
| 4834 |
/* We're in the middle of initializing it. */ |
| 4835 |
break; |
| 4836 |
} |
| 4837 |
} |
| 4838 |
if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE) |
| 4839 |
{ |
| 4840 |
if (CONSTRUCTOR_NELTS (whole) > 0) |
| 4841 |
{ |
| 4842 |
/* DR 1188 says we don't have to deal with this. */ |
| 4843 |
if (!ctx->quiet) |
| 4844 |
{ |
| 4845 |
constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0); |
| 4846 |
if (cep->value == NULL_TREE) |
| 4847 |
error ("accessing uninitialized member %qD", part); |
| 4848 |
else |
| 4849 |
error ("accessing %qD member instead of initialized %qD member " |
| 4850 |
"in constant expression", part, cep->index); |
| 4851 |
} |
| 4852 |
*non_constant_p = true; |
| 4853 |
return t; |
| 4854 |
} |
| 4855 |
else if (!CONSTRUCTOR_NO_CLEARING (whole)) |
| 4856 |
{ |
| 4857 |
/* Value-initialized union, check if looking at the first member. */ |
| 4858 |
tree first = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (whole))); |
| 4859 |
if (first != part) |
| 4860 |
{ |
| 4861 |
if (!ctx->quiet) |
| 4862 |
error ("accessing %qD member instead of initialized %qD " |
| 4863 |
"member in constant expression", part, first); |
| 4864 |
*non_constant_p = true; |
| 4865 |
return t; |
| 4866 |
} |
| 4867 |
} |
| 4868 |
} |
| 4869 |
|
| 4870 |
/* We only create a CONSTRUCTOR for a subobject when we modify it, so empty |
| 4871 |
classes never get represented; throw together a value now. */ |
| 4872 |
if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
| 4873 |
return build_constructor (TREE_TYPE (t), NULL); |
| 4874 |
|
| 4875 |
gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole))); |
| 4876 |
|
| 4877 |
if (CONSTRUCTOR_NO_CLEARING (whole)) |
| 4878 |
{ |
| 4879 |
/* 'whole' is part of the aggregate initializer we're currently |
| 4880 |
building; if there's no initializer for this member yet, that's an |
| 4881 |
error. */ |
| 4882 |
if (!ctx->quiet) |
| 4883 |
error ("accessing uninitialized member %qD", part); |
| 4884 |
*non_constant_p = true; |
| 4885 |
return t; |
| 4886 |
} |
| 4887 |
|
| 4888 |
/* If there's no explicit init for this field, it's value-initialized. */ |
| 4889 |
|
| 4890 |
if (AGGREGATE_TYPE_P (TREE_TYPE (t))) |
| 4891 |
{ |
| 4892 |
/* As in cxx_eval_store_expression, insert an empty CONSTRUCTOR |
| 4893 |
and copy the flags. */ |
| 4894 |
constructor_elt *e = get_or_insert_ctor_field (whole, part); |
| 4895 |
e->value = value = build_constructor (TREE_TYPE (part), NULL); |
| 4896 |
CONSTRUCTOR_ZERO_PADDING_BITS (value) |
| 4897 |
= CONSTRUCTOR_ZERO_PADDING_BITS (whole); |
| 4898 |
return value; |
| 4899 |
} |
| 4900 |
|
| 4901 |
value = build_value_init (TREE_TYPE (t), tf_warning_or_error); |
| 4902 |
return cxx_eval_constant_expression (ctx, value, |
| 4903 |
lval, |
| 4904 |
non_constant_p, overflow_p); |
| 4905 |
} |
| 4906 |
|
| 4907 |
/* Subroutine of cxx_eval_constant_expression. |
| 4908 |
Attempt to reduce a field access of a value of class type that is |
| 4909 |
expressed as a BIT_FIELD_REF. */ |
| 4910 |
|
| 4911 |
static tree |
| 4912 |
cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t, |
| 4913 |
value_cat lval, |
| 4914 |
bool *non_constant_p, bool *overflow_p) |
| 4915 |
{ |
| 4916 |
tree orig_whole = TREE_OPERAND (t, 0); |
| 4917 |
tree retval, fldval, utype, mask; |
| 4918 |
bool fld_seen = false; |
| 4919 |
HOST_WIDE_INT istart, isize; |
| 4920 |
tree whole = cxx_eval_constant_expression (ctx, orig_whole, |
| 4921 |
lval, |
| 4922 |
non_constant_p, overflow_p); |
| 4923 |
tree start, field, value; |
| 4924 |
unsigned HOST_WIDE_INT i; |
| 4925 |
|
| 4926 |
if (whole == orig_whole) |
| 4927 |
return t; |
| 4928 |
/* Don't VERIFY_CONSTANT here; we only want to check that we got a |
| 4929 |
CONSTRUCTOR. */ |
| 4930 |
if (!*non_constant_p |
| 4931 |
&& TREE_CODE (whole) != VECTOR_CST |
| 4932 |
&& TREE_CODE (whole) != CONSTRUCTOR) |
| 4933 |
{ |
| 4934 |
if (!ctx->quiet) |
| 4935 |
error ("%qE is not a constant expression", orig_whole); |
| 4936 |
*non_constant_p = true; |
| 4937 |
} |
| 4938 |
if (*non_constant_p) |
| 4939 |
return t; |
| 4940 |
|
| 4941 |
if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t))) |
| 4942 |
{ |
| 4943 |
if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole, |
| 4944 |
TREE_OPERAND (t, 1), TREE_OPERAND (t, 2))) |
| 4945 |
return r; |
| 4946 |
if (!ctx->quiet) |
| 4947 |
error ("%qE is not a constant expression", orig_whole); |
| 4948 |
*non_constant_p = true; |
| 4949 |
return t; |
| 4950 |
} |
| 4951 |
|
| 4952 |
start = TREE_OPERAND (t, 2); |
| 4953 |
istart = tree_to_shwi (start); |
| 4954 |
isize = tree_to_shwi (TREE_OPERAND (t, 1)); |
| 4955 |
utype = TREE_TYPE (t); |
| 4956 |
if (!TYPE_UNSIGNED (utype)) |
| 4957 |
utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1); |
| 4958 |
retval = build_int_cst (utype, 0); |
| 4959 |
FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) |
| 4960 |
{ |
| 4961 |
tree bitpos = bit_position (field); |
| 4962 |
STRIP_ANY_LOCATION_WRAPPER (value); |
| 4963 |
if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1)) |
| 4964 |
return value; |
| 4965 |
if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE |
| 4966 |
&& TREE_CODE (value) == INTEGER_CST |
| 4967 |
&& tree_fits_shwi_p (bitpos) |
| 4968 |
&& tree_fits_shwi_p (DECL_SIZE (field))) |
| 4969 |
{ |
| 4970 |
HOST_WIDE_INT bit = tree_to_shwi (bitpos); |
| 4971 |
HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field)); |
| 4972 |
HOST_WIDE_INT shift; |
| 4973 |
if (bit >= istart && bit + sz <= istart + isize) |
| 4974 |
{ |
| 4975 |
fldval = fold_convert (utype, value); |
| 4976 |
mask = build_int_cst_type (utype, -1); |
| 4977 |
mask = fold_build2 (LSHIFT_EXPR, utype, mask, |
| 4978 |
size_int (TYPE_PRECISION (utype) - sz)); |
| 4979 |
mask = fold_build2 (RSHIFT_EXPR, utype, mask, |
| 4980 |
size_int (TYPE_PRECISION (utype) - sz)); |
| 4981 |
fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask); |
| 4982 |
shift = bit - istart; |
| 4983 |
if (BYTES_BIG_ENDIAN) |
| 4984 |
shift = TYPE_PRECISION (utype) - shift - sz; |
| 4985 |
fldval = fold_build2 (LSHIFT_EXPR, utype, fldval, |
| 4986 |
size_int (shift)); |
| 4987 |
retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval); |
| 4988 |
fld_seen = true; |
| 4989 |
} |
| 4990 |
} |
| 4991 |
} |
| 4992 |
if (fld_seen) |
| 4993 |
return fold_convert (TREE_TYPE (t), retval); |
| 4994 |
gcc_unreachable (); |
| 4995 |
return error_mark_node; |
| 4996 |
} |
| 4997 |
|
| 4998 |
/* Helper for cxx_eval_bit_cast. |
| 4999 |
Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From |
| 5000 |
types and types of all subobjects have is_union_v<T>, is_pointer_v<T>, |
| 5001 |
is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static |
| 5002 |
data members of reference type. */ |
| 5003 |
|
| 5004 |
static bool |
| 5005 |
check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type, |
| 5006 |
tree orig_type) |
| 5007 |
{ |
| 5008 |
if (TREE_CODE (type) == UNION_TYPE) |
| 5009 |
{ |
| 5010 |
if (!ctx->quiet) |
| 5011 |
{ |
| 5012 |
if (type == orig_type) |
| 5013 |
error_at (loc, "%qs is not a constant expression because %qT is " |
| 5014 |
"a union type", "__builtin_bit_cast", type); |
| 5015 |
else |
| 5016 |
error_at (loc, "%qs is not a constant expression because %qT " |
| 5017 |
"contains a union type", "__builtin_bit_cast", |
| 5018 |
orig_type); |
| 5019 |
} |
| 5020 |
return true; |
| 5021 |
} |
| 5022 |
if (TREE_CODE (type) == POINTER_TYPE) |
| 5023 |
{ |
| 5024 |
if (!ctx->quiet) |
| 5025 |
{ |
| 5026 |
if (type == orig_type) |
| 5027 |
error_at (loc, "%qs is not a constant expression because %qT is " |
| 5028 |
"a pointer type", "__builtin_bit_cast", type); |
| 5029 |
else |
| 5030 |
error_at (loc, "%qs is not a constant expression because %qT " |
| 5031 |
"contains a pointer type", "__builtin_bit_cast", |
| 5032 |
orig_type); |
| 5033 |
} |
| 5034 |
return true; |
| 5035 |
} |
| 5036 |
if (TREE_CODE (type) == REFERENCE_TYPE) |
| 5037 |
{ |
| 5038 |
if (!ctx->quiet) |
| 5039 |
{ |
| 5040 |
if (type == orig_type) |
| 5041 |
error_at (loc, "%qs is not a constant expression because %qT is " |
| 5042 |
"a reference type", "__builtin_bit_cast", type); |
| 5043 |
else |
| 5044 |
error_at (loc, "%qs is not a constant expression because %qT " |
| 5045 |
"contains a reference type", "__builtin_bit_cast", |
| 5046 |
orig_type); |
| 5047 |
} |
| 5048 |
return true; |
| 5049 |
} |
| 5050 |
if (TYPE_PTRMEM_P (type)) |
| 5051 |
{ |
| 5052 |
if (!ctx->quiet) |
| 5053 |
{ |
| 5054 |
if (type == orig_type) |
| 5055 |
error_at (loc, "%qs is not a constant expression because %qT is " |
| 5056 |
"a pointer to member type", "__builtin_bit_cast", |
| 5057 |
type); |
| 5058 |
else |
| 5059 |
error_at (loc, "%qs is not a constant expression because %qT " |
| 5060 |
"contains a pointer to member type", |
| 5061 |
"__builtin_bit_cast", orig_type); |
| 5062 |
} |
| 5063 |
return true; |
| 5064 |
} |
| 5065 |
if (TYPE_VOLATILE (type)) |
| 5066 |
{ |
| 5067 |
if (!ctx->quiet) |
| 5068 |
{ |
| 5069 |
if (type == orig_type) |
| 5070 |
error_at (loc, "%qs is not a constant expression because %qT is " |
| 5071 |
"volatile", "__builtin_bit_cast", type); |
| 5072 |
else |
| 5073 |
error_at (loc, "%qs is not a constant expression because %qT " |
| 5074 |
"contains a volatile subobject", |
| 5075 |
"__builtin_bit_cast", orig_type); |
| 5076 |
} |
| 5077 |
return true; |
| 5078 |
} |
| 5079 |
if (TREE_CODE (type) == RECORD_TYPE) |
| 5080 |
for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
| 5081 |
if (TREE_CODE (field) == FIELD_DECL |
| 5082 |
&& check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type)) |
| 5083 |
return true; |
| 5084 |
if (TREE_CODE (type) == ARRAY_TYPE) |
| 5085 |
return check_bit_cast_type (ctx, loc, TREE_TYPE (type), orig_type); |
| 5086 |
return false; |
| 5087 |
} |
| 5088 |
|
| 5089 |
/* Helper function for cxx_eval_bit_cast. For unsigned char or |
| 5090 |
std::byte members of CONSTRUCTOR (recursively) if they contain |
| 5091 |
some indeterminate bits (as set in MASK), remove the ctor elts, |
| 5092 |
mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the |
| 5093 |
bits in MASK. */ |
| 5094 |
|
| 5095 |
static void |
| 5096 |
clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask) |
| 5097 |
{ |
| 5098 |
if (TREE_CODE (t) != CONSTRUCTOR) |
| 5099 |
return; |
| 5100 |
|
| 5101 |
unsigned i, j = 0; |
| 5102 |
tree index, value; |
| 5103 |
FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value) |
| 5104 |
{ |
| 5105 |
tree type = TREE_TYPE (value); |
| 5106 |
if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE |
| 5107 |
&& DECL_BIT_FIELD_TYPE (index) != NULL_TREE) |
| 5108 |
{ |
| 5109 |
if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index))) |
| 5110 |
{ |
| 5111 |
HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index)); |
| 5112 |
gcc_assert (fldsz != 0); |
| 5113 |
HOST_WIDE_INT pos = int_byte_position (index); |
| 5114 |
HOST_WIDE_INT bpos |
| 5115 |
= tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index)); |
| 5116 |
bpos %= BITS_PER_UNIT; |
| 5117 |
HOST_WIDE_INT end |
| 5118 |
= ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT; |
| 5119 |
gcc_assert (end == 1 || end == 2); |
| 5120 |
unsigned char *p = mask + pos; |
| 5121 |
unsigned char mask_save[2]; |
| 5122 |
mask_save[0] = mask[pos]; |
| 5123 |
mask_save[1] = end == 2 ? mask[pos + 1] : 0; |
| 5124 |
if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) |
| 5125 |
sorry_at (loc, "PDP11 bit-field handling unsupported" |
| 5126 |
" in %qs", "__builtin_bit_cast"); |
| 5127 |
else if (BYTES_BIG_ENDIAN) |
| 5128 |
{ |
| 5129 |
/* Big endian. */ |
| 5130 |
if (bpos + fldsz <= BITS_PER_UNIT) |
| 5131 |
*p &= ~(((1 << fldsz) - 1) |
| 5132 |
<< (BITS_PER_UNIT - bpos - fldsz)); |
| 5133 |
else |
| 5134 |
{ |
| 5135 |
gcc_assert (bpos); |
| 5136 |
*p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos); |
| 5137 |
p++; |
| 5138 |
fldsz -= BITS_PER_UNIT - bpos; |
| 5139 |
gcc_assert (fldsz && fldsz < BITS_PER_UNIT); |
| 5140 |
*p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz; |
| 5141 |
} |
| 5142 |
} |
| 5143 |
else |
| 5144 |
{ |
| 5145 |
/* Little endian. */ |
| 5146 |
if (bpos + fldsz <= BITS_PER_UNIT) |
| 5147 |
*p &= ~(((1 << fldsz) - 1) << bpos); |
| 5148 |
else |
| 5149 |
{ |
| 5150 |
gcc_assert (bpos); |
| 5151 |
*p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos); |
| 5152 |
p++; |
| 5153 |
fldsz -= BITS_PER_UNIT - bpos; |
| 5154 |
gcc_assert (fldsz && fldsz < BITS_PER_UNIT); |
| 5155 |
*p &= ~((1 << fldsz) - 1); |
| 5156 |
} |
| 5157 |
} |
| 5158 |
if (mask_save[0] != mask[pos] |
| 5159 |
|| (end == 2 && mask_save[1] != mask[pos + 1])) |
| 5160 |
{ |
| 5161 |
CONSTRUCTOR_NO_CLEARING (t) = 1; |
| 5162 |
continue; |
| 5163 |
} |
| 5164 |
} |
| 5165 |
} |
| 5166 |
else if (is_byte_access_type_not_plain_char (type)) |
| 5167 |
{ |
| 5168 |
HOST_WIDE_INT pos; |
| 5169 |
if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) |
| 5170 |
pos = tree_to_shwi (index); |
| 5171 |
else |
| 5172 |
pos = int_byte_position (index); |
| 5173 |
if (mask[pos]) |
| 5174 |
{ |
| 5175 |
CONSTRUCTOR_NO_CLEARING (t) = 1; |
| 5176 |
mask[pos] = 0; |
| 5177 |
continue; |
| 5178 |
} |
| 5179 |
} |
| 5180 |
if (TREE_CODE (value) == CONSTRUCTOR) |
| 5181 |
{ |
| 5182 |
HOST_WIDE_INT pos; |
| 5183 |
if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) |
| 5184 |
pos = tree_to_shwi (index) |
| 5185 |
* tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)))); |
| 5186 |
else |
| 5187 |
pos = int_byte_position (index); |
| 5188 |
clear_uchar_or_std_byte_in_mask (loc, value, mask + pos); |
| 5189 |
} |
| 5190 |
if (i != j) |
| 5191 |
{ |
| 5192 |
CONSTRUCTOR_ELT (t, j)->index = index; |
| 5193 |
CONSTRUCTOR_ELT (t, j)->value = value; |
| 5194 |
} |
| 5195 |
++j; |
| 5196 |
} |
| 5197 |
if (CONSTRUCTOR_NELTS (t) != j) |
| 5198 |
vec_safe_truncate (CONSTRUCTOR_ELTS (t), j); |
| 5199 |
} |
| 5200 |
|
| 5201 |
/* Subroutine of cxx_eval_constant_expression. |
| 5202 |
Attempt to evaluate a BIT_CAST_EXPR. */ |
| 5203 |
|
| 5204 |
static tree |
| 5205 |
cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p, |
| 5206 |
bool *overflow_p) |
| 5207 |
{ |
| 5208 |
if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t), |
| 5209 |
TREE_TYPE (t)) |
| 5210 |
|| check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0), |
| 5211 |
EXPR_LOCATION (t)), |
| 5212 |
TREE_TYPE (TREE_OPERAND (t, 0)), |
| 5213 |
TREE_TYPE (TREE_OPERAND (t, 0)))) |
| 5214 |
{ |
| 5215 |
*non_constant_p = true; |
| 5216 |
return t; |
| 5217 |
} |
| 5218 |
|
| 5219 |
tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue, |
| 5220 |
non_constant_p, overflow_p); |
| 5221 |
if (*non_constant_p) |
| 5222 |
return t; |
| 5223 |
|
| 5224 |
location_t loc = EXPR_LOCATION (t); |
| 5225 |
if (BITS_PER_UNIT != 8 || CHAR_BIT != 8) |
| 5226 |
{ |
| 5227 |
if (!ctx->quiet) |
| 5228 |
sorry_at (loc, "%qs cannot be constant evaluated on the target", |
| 5229 |
"__builtin_bit_cast"); |
| 5230 |
*non_constant_p = true; |
| 5231 |
return t; |
| 5232 |
} |
| 5233 |
|
| 5234 |
if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t)))) |
| 5235 |
{ |
| 5236 |
if (!ctx->quiet) |
| 5237 |
sorry_at (loc, "%qs cannot be constant evaluated because the " |
| 5238 |
"type is too large", "__builtin_bit_cast"); |
| 5239 |
*non_constant_p = true; |
| 5240 |
return t; |
| 5241 |
} |
| 5242 |
|
| 5243 |
HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t))); |
| 5244 |
if (len < 0 || (int) len != len) |
| 5245 |
{ |
| 5246 |
if (!ctx->quiet) |
| 5247 |
sorry_at (loc, "%qs cannot be constant evaluated because the " |
| 5248 |
"type is too large", "__builtin_bit_cast"); |
| 5249 |
*non_constant_p = true; |
| 5250 |
return t; |
| 5251 |
} |
| 5252 |
|
| 5253 |
unsigned char buf[64]; |
| 5254 |
unsigned char *ptr, *mask; |
| 5255 |
size_t alen = (size_t) len * 2; |
| 5256 |
if (alen <= sizeof (buf)) |
| 5257 |
ptr = buf; |
| 5258 |
else |
| 5259 |
ptr = XNEWVEC (unsigned char, alen); |
| 5260 |
mask = ptr + (size_t) len; |
| 5261 |
/* At the beginning consider everything indeterminate. */ |
| 5262 |
memset (mask, ~0, (size_t) len); |
| 5263 |
|
| 5264 |
if (native_encode_initializer (op, ptr, len, 0, mask) != len) |
| 5265 |
{ |
| 5266 |
if (!ctx->quiet) |
| 5267 |
sorry_at (loc, "%qs cannot be constant evaluated because the " |
| 5268 |
"argument cannot be encoded", "__builtin_bit_cast"); |
| 5269 |
*non_constant_p = true; |
| 5270 |
if (ptr != buf) |
| 5271 |
XDELETE (ptr); |
| 5272 |
return t; |
| 5273 |
} |
| 5274 |
|
| 5275 |
tree r = NULL_TREE; |
| 5276 |
if (can_native_interpret_type_p (TREE_TYPE (t))) |
| 5277 |
{ |
| 5278 |
r = native_interpret_expr (TREE_TYPE (t), ptr, len); |
| 5279 |
if (is_byte_access_type_not_plain_char (TREE_TYPE (t))) |
| 5280 |
{ |
| 5281 |
gcc_assert (len == 1); |
| 5282 |
if (mask[0]) |
| 5283 |
{ |
| 5284 |
memset (mask, 0, len); |
| 5285 |
r = build_constructor (TREE_TYPE (r), NULL); |
| 5286 |
CONSTRUCTOR_NO_CLEARING (r) = 1; |
| 5287 |
} |
| 5288 |
} |
| 5289 |
} |
| 5290 |
else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE) |
| 5291 |
{ |
| 5292 |
r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len); |
| 5293 |
if (r != NULL_TREE) |
| 5294 |
{ |
| 5295 |
clear_type_padding_in_mask (TREE_TYPE (t), mask); |
| 5296 |
clear_uchar_or_std_byte_in_mask (loc, r, mask); |
| 5297 |
if (CHECKING_P) |
| 5298 |
{ |
| 5299 |
tree e = cxx_eval_bare_aggregate (ctx, r, vc_prvalue, |
| 5300 |
non_constant_p, overflow_p); |
| 5301 |
gcc_checking_assert (e == r); |
| 5302 |
r = e; |
| 5303 |
} |
| 5304 |
} |
| 5305 |
} |
| 5306 |
|
| 5307 |
if (r != NULL_TREE) |
| 5308 |
{ |
| 5309 |
for (int i = 0; i < len; i++) |
| 5310 |
if (mask[i]) |
| 5311 |
{ |
| 5312 |
if (!ctx->quiet) |
| 5313 |
error_at (loc, "%qs accessing uninitialized byte at offset %d", |
| 5314 |
"__builtin_bit_cast", i); |
| 5315 |
*non_constant_p = true; |
| 5316 |
r = t; |
| 5317 |
break; |
| 5318 |
} |
| 5319 |
if (ptr != buf) |
| 5320 |
XDELETE (ptr); |
| 5321 |
return r; |
| 5322 |
} |
| 5323 |
|
| 5324 |
if (!ctx->quiet) |
| 5325 |
sorry_at (loc, "%qs cannot be constant evaluated because the " |
| 5326 |
"argument cannot be interpreted", "__builtin_bit_cast"); |
| 5327 |
*non_constant_p = true; |
| 5328 |
if (ptr != buf) |
| 5329 |
XDELETE (ptr); |
| 5330 |
return t; |
| 5331 |
} |
| 5332 |
|
| 5333 |
/* Subroutine of cxx_eval_constant_expression. |
| 5334 |
Evaluate a short-circuited logical expression T in the context |
| 5335 |
of a given constexpr CALL. BAILOUT_VALUE is the value for |
| 5336 |
early return. CONTINUE_VALUE is used here purely for |
| 5337 |
sanity check purposes. */ |
| 5338 |
|
| 5339 |
static tree |
| 5340 |
cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t, |
| 5341 |
tree bailout_value, tree continue_value, |
| 5342 |
bool *non_constant_p, bool *overflow_p) |
| 5343 |
{ |
| 5344 |
tree r; |
| 5345 |
tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
| 5346 |
vc_prvalue, non_constant_p, |
| 5347 |
overflow_p); |
| 5348 |
VERIFY_CONSTANT (lhs); |
| 5349 |
if (tree_int_cst_equal (lhs, bailout_value)) |
| 5350 |
return lhs; |
| 5351 |
gcc_assert (tree_int_cst_equal (lhs, continue_value)); |
| 5352 |
r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
| 5353 |
vc_prvalue, non_constant_p, |
| 5354 |
overflow_p); |
| 5355 |
VERIFY_CONSTANT (r); |
| 5356 |
return r; |
| 5357 |
} |
| 5358 |
|
| 5359 |
/* REF is a COMPONENT_REF designating a particular field. V is a vector of |
| 5360 |
CONSTRUCTOR elements to initialize (part of) an object containing that |
| 5361 |
field. Return a pointer to the constructor_elt corresponding to the |
| 5362 |
initialization of the field. */ |
| 5363 |
|
| 5364 |
static constructor_elt * |
| 5365 |
base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref) |
| 5366 |
{ |
| 5367 |
tree aggr = TREE_OPERAND (ref, 0); |
| 5368 |
tree field = TREE_OPERAND (ref, 1); |
| 5369 |
HOST_WIDE_INT i; |
| 5370 |
constructor_elt *ce; |
| 5371 |
|
| 5372 |
gcc_assert (TREE_CODE (ref) == COMPONENT_REF); |
| 5373 |
|
| 5374 |
if (TREE_CODE (aggr) == COMPONENT_REF) |
| 5375 |
{ |
| 5376 |
constructor_elt *base_ce |
| 5377 |
= base_field_constructor_elt (v, aggr); |
| 5378 |
v = CONSTRUCTOR_ELTS (base_ce->value); |
| 5379 |
} |
| 5380 |
|
| 5381 |
for (i = 0; vec_safe_iterate (v, i, &ce); ++i) |
| 5382 |
if (ce->index == field) |
| 5383 |
return ce; |
| 5384 |
|
| 5385 |
gcc_unreachable (); |
| 5386 |
return NULL; |
| 5387 |
} |
| 5388 |
|
| 5389 |
/* Some of the expressions fed to the constexpr mechanism are calls to |
| 5390 |
constructors, which have type void. In that case, return the type being |
| 5391 |
initialized by the constructor. */ |
| 5392 |
|
| 5393 |
static tree |
| 5394 |
initialized_type (tree t) |
| 5395 |
{ |
| 5396 |
if (TYPE_P (t)) |
| 5397 |
return t; |
| 5398 |
tree type = TREE_TYPE (t); |
| 5399 |
if (TREE_CODE (t) == CALL_EXPR) |
| 5400 |
{ |
| 5401 |
/* A constructor call has void type, so we need to look deeper. */ |
| 5402 |
tree fn = get_function_named_in_call (t); |
| 5403 |
if (fn && TREE_CODE (fn) == FUNCTION_DECL |
| 5404 |
&& DECL_CXX_CONSTRUCTOR_P (fn)) |
| 5405 |
type = DECL_CONTEXT (fn); |
| 5406 |
} |
| 5407 |
else if (TREE_CODE (t) == COMPOUND_EXPR) |
| 5408 |
return initialized_type (TREE_OPERAND (t, 1)); |
| 5409 |
else if (TREE_CODE (t) == AGGR_INIT_EXPR) |
| 5410 |
type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t)); |
| 5411 |
return cv_unqualified (type); |
| 5412 |
} |
| 5413 |
|
| 5414 |
/* We're about to initialize element INDEX of an array or class from VALUE. |
| 5415 |
Set up NEW_CTX appropriately by adjusting .object to refer to the |
| 5416 |
subobject and creating a new CONSTRUCTOR if the element is itself |
| 5417 |
a class or array. */ |
| 5418 |
|
| 5419 |
static void |
| 5420 |
init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx, |
| 5421 |
tree index, tree &value) |
| 5422 |
{ |
| 5423 |
new_ctx = *ctx; |
| 5424 |
|
| 5425 |
if (index && TREE_CODE (index) != INTEGER_CST |
| 5426 |
&& TREE_CODE (index) != FIELD_DECL |
| 5427 |
&& TREE_CODE (index) != RANGE_EXPR) |
| 5428 |
/* This won't have an element in the new CONSTRUCTOR. */ |
| 5429 |
return; |
| 5430 |
|
| 5431 |
tree type = initialized_type (value); |
| 5432 |
if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type)) |
| 5433 |
/* A non-aggregate member doesn't get its own CONSTRUCTOR. */ |
| 5434 |
return; |
| 5435 |
if (VECTOR_TYPE_P (type) |
| 5436 |
&& VECTOR_TYPE_P (TREE_TYPE (ctx->ctor)) |
| 5437 |
&& index == NULL_TREE) |
| 5438 |
/* A vector inside of a vector CONSTRUCTOR, e.g. when a larger |
| 5439 |
vector is constructed from smaller vectors, doesn't get its own |
| 5440 |
CONSTRUCTOR either. */ |
| 5441 |
return; |
| 5442 |
|
| 5443 |
/* The sub-aggregate initializer might contain a placeholder; |
| 5444 |
update object to refer to the subobject and ctor to refer to |
| 5445 |
the (newly created) sub-initializer. */ |
| 5446 |
if (ctx->object) |
| 5447 |
{ |
| 5448 |
if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR) |
| 5449 |
/* There's no well-defined subobject for this index. */ |
| 5450 |
new_ctx.object = NULL_TREE; |
| 5451 |
else |
| 5452 |
new_ctx.object = build_ctor_subob_ref (index, type, ctx->object); |
| 5453 |
} |
| 5454 |
|
| 5455 |
if (is_empty_class (type)) |
| 5456 |
/* Leave ctor null for an empty subobject, they aren't represented in the |
| 5457 |
result of evaluation. */ |
| 5458 |
new_ctx.ctor = NULL_TREE; |
| 5459 |
else |
| 5460 |
{ |
| 5461 |
tree elt = build_constructor (type, NULL); |
| 5462 |
CONSTRUCTOR_NO_CLEARING (elt) = true; |
| 5463 |
new_ctx.ctor = elt; |
| 5464 |
} |
| 5465 |
|
| 5466 |
if (TREE_CODE (value) == TARGET_EXPR) |
| 5467 |
/* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */ |
| 5468 |
value = TARGET_EXPR_INITIAL (value); |
| 5469 |
} |
| 5470 |
|
| 5471 |
/* We're about to process an initializer for a class or array TYPE. Make |
| 5472 |
sure that CTX is set up appropriately. */ |
| 5473 |
|
| 5474 |
static void |
| 5475 |
verify_ctor_sanity (const constexpr_ctx *ctx, tree type) |
| 5476 |
{ |
| 5477 |
/* We don't bother building a ctor for an empty base subobject. */ |
| 5478 |
if (is_empty_class (type)) |
| 5479 |
return; |
| 5480 |
|
| 5481 |
/* We're in the middle of an initializer that might involve placeholders; |
| 5482 |
our caller should have created a CONSTRUCTOR for us to put the |
| 5483 |
initializer into. We will either return that constructor or T. */ |
| 5484 |
gcc_assert (ctx->ctor); |
| 5485 |
gcc_assert (same_type_ignoring_top_level_qualifiers_p |
| 5486 |
(type, TREE_TYPE (ctx->ctor))); |
| 5487 |
/* We used to check that ctx->ctor was empty, but that isn't the case when |
| 5488 |
the object is zero-initialized before calling the constructor. */ |
| 5489 |
if (ctx->object) |
| 5490 |
{ |
| 5491 |
tree otype = TREE_TYPE (ctx->object); |
| 5492 |
gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype) |
| 5493 |
/* Handle flexible array members. */ |
| 5494 |
|| (TREE_CODE (otype) == ARRAY_TYPE |
| 5495 |
&& TYPE_DOMAIN (otype) == NULL_TREE |
| 5496 |
&& TREE_CODE (type) == ARRAY_TYPE |
| 5497 |
&& (same_type_ignoring_top_level_qualifiers_p |
| 5498 |
(TREE_TYPE (type), TREE_TYPE (otype))))); |
| 5499 |
} |
| 5500 |
gcc_assert (!ctx->object || !DECL_P (ctx->object) |
| 5501 |
|| ctx->global->get_value (ctx->object) == ctx->ctor); |
| 5502 |
} |
| 5503 |
|
| 5504 |
/* Subroutine of cxx_eval_constant_expression. |
| 5505 |
The expression tree T denotes a C-style array or a C-style |
| 5506 |
aggregate. Reduce it to a constant expression. */ |
| 5507 |
|
| 5508 |
static tree |
| 5509 |
cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t, |
| 5510 |
value_cat lval, |
| 5511 |
bool *non_constant_p, bool *overflow_p) |
| 5512 |
{ |
| 5513 |
vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); |
| 5514 |
bool changed = false; |
| 5515 |
gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t)); |
| 5516 |
tree type = TREE_TYPE (t); |
| 5517 |
|
| 5518 |
constexpr_ctx new_ctx; |
| 5519 |
if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type)) |
| 5520 |
{ |
| 5521 |
/* We don't really need the ctx->ctor business for a PMF or |
| 5522 |
vector, but it's simpler to use the same code. */ |
| 5523 |
new_ctx = *ctx; |
| 5524 |
new_ctx.ctor = build_constructor (type, NULL); |
| 5525 |
new_ctx.object = NULL_TREE; |
| 5526 |
ctx = &new_ctx; |
| 5527 |
}; |
| 5528 |
verify_ctor_sanity (ctx, type); |
| 5529 |
vec<constructor_elt, va_gc> **p = nullptr; |
| 5530 |
if (ctx->ctor) |
| 5531 |
{ |
| 5532 |
p = &CONSTRUCTOR_ELTS (ctx->ctor); |
| 5533 |
vec_alloc (*p, vec_safe_length (v)); |
| 5534 |
if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t)) |
| 5535 |
CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1; |
| 5536 |
} |
| 5537 |
|
| 5538 |
unsigned i; |
| 5539 |
tree index, value; |
| 5540 |
bool constant_p = true; |
| 5541 |
bool side_effects_p = false; |
| 5542 |
FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value) |
| 5543 |
{ |
| 5544 |
tree orig_value = value; |
| 5545 |
init_subob_ctx (ctx, new_ctx, index, value); |
| 5546 |
/* Like in cxx_eval_store_expression, omit entries for empty fields. */ |
| 5547 |
bool no_slot = new_ctx.ctor == NULL_TREE; |
| 5548 |
int pos_hint = -1; |
| 5549 |
if (new_ctx.ctor != ctx->ctor && !no_slot) |
| 5550 |
{ |
| 5551 |
/* If we built a new CONSTRUCTOR, attach it now so that other |
| 5552 |
initializers can refer to it. */ |
| 5553 |
constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index); |
| 5554 |
cep->value = new_ctx.ctor; |
| 5555 |
pos_hint = cep - (*p)->begin(); |
| 5556 |
} |
| 5557 |
else if (TREE_CODE (type) == UNION_TYPE) |
| 5558 |
/* Otherwise if we're constructing a non-aggregate union member, set |
| 5559 |
the active union member now so that we can later detect and diagnose |
| 5560 |
if its initializer attempts to activate another member. */ |
| 5561 |
get_or_insert_ctor_field (ctx->ctor, index); |
| 5562 |
tree elt = cxx_eval_constant_expression (&new_ctx, value, |
| 5563 |
lval, |
| 5564 |
non_constant_p, overflow_p); |
| 5565 |
/* Don't VERIFY_CONSTANT here. */ |
| 5566 |
if (ctx->quiet && *non_constant_p) |
| 5567 |
break; |
| 5568 |
if (elt != orig_value) |
| 5569 |
changed = true; |
| 5570 |
|
| 5571 |
if (!TREE_CONSTANT (elt)) |
| 5572 |
constant_p = false; |
| 5573 |
if (TREE_SIDE_EFFECTS (elt)) |
| 5574 |
side_effects_p = true; |
| 5575 |
if (index && TREE_CODE (index) == COMPONENT_REF) |
| 5576 |
{ |
| 5577 |
/* This is an initialization of a vfield inside a base |
| 5578 |
subaggregate that we already initialized; push this |
| 5579 |
initialization into the previous initialization. */ |
| 5580 |
constructor_elt *inner = base_field_constructor_elt (*p, index); |
| 5581 |
inner->value = elt; |
| 5582 |
changed = true; |
| 5583 |
} |
| 5584 |
else if (no_slot) |
| 5585 |
/* This is an initializer for an empty field; now that we've |
| 5586 |
checked that it's constant, we can ignore it. */ |
| 5587 |
changed = true; |
| 5588 |
else if (index |
| 5589 |
&& (TREE_CODE (index) == NOP_EXPR |
| 5590 |
|| TREE_CODE (index) == POINTER_PLUS_EXPR)) |
| 5591 |
{ |
| 5592 |
/* Old representation of empty bases. FIXME remove. */ |
| 5593 |
gcc_checking_assert (false); |
| 5594 |
gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index)))); |
| 5595 |
changed = true; |
| 5596 |
} |
| 5597 |
else |
| 5598 |
{ |
| 5599 |
if (TREE_CODE (type) == UNION_TYPE |
| 5600 |
&& (*p)->last().index != index) |
| 5601 |
/* The initializer erroneously changed the active union member that |
| 5602 |
we're initializing. */ |
| 5603 |
gcc_assert (*non_constant_p); |
| 5604 |
else |
| 5605 |
{ |
| 5606 |
/* The initializer might have mutated the underlying CONSTRUCTOR, |
| 5607 |
so recompute the location of the target constructer_elt. */ |
| 5608 |
constructor_elt *cep |
| 5609 |
= get_or_insert_ctor_field (ctx->ctor, index, pos_hint); |
| 5610 |
cep->value = elt; |
| 5611 |
} |
| 5612 |
|
| 5613 |
/* Adding or replacing an element might change the ctor's flags. */ |
| 5614 |
TREE_CONSTANT (ctx->ctor) = constant_p; |
| 5615 |
TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p; |
| 5616 |
} |
| 5617 |
} |
| 5618 |
if (*non_constant_p) |
| 5619 |
return t; |
| 5620 |
if (!changed) |
| 5621 |
{ |
| 5622 |
if (VECTOR_TYPE_P (type)) |
| 5623 |
t = fold (t); |
| 5624 |
return t; |
| 5625 |
} |
| 5626 |
t = ctx->ctor; |
| 5627 |
if (!t) |
| 5628 |
t = build_constructor (type, NULL); |
| 5629 |
/* We're done building this CONSTRUCTOR, so now we can interpret an |
| 5630 |
element without an explicit initializer as value-initialized. */ |
| 5631 |
CONSTRUCTOR_NO_CLEARING (t) = false; |
| 5632 |
TREE_CONSTANT (t) = constant_p; |
| 5633 |
TREE_SIDE_EFFECTS (t) = side_effects_p; |
| 5634 |
if (VECTOR_TYPE_P (type)) |
| 5635 |
t = fold (t); |
| 5636 |
return t; |
| 5637 |
} |
| 5638 |
|
| 5639 |
/* Subroutine of cxx_eval_constant_expression. |
| 5640 |
The expression tree T is a VEC_INIT_EXPR which denotes the desired |
| 5641 |
initialization of a non-static data member of array type. Reduce it to a |
| 5642 |
CONSTRUCTOR. |
| 5643 |
|
| 5644 |
Note that apart from value-initialization (when VALUE_INIT is true), |
| 5645 |
this is only intended to support value-initialization and the |
| 5646 |
initializations done by defaulted constructors for classes with |
| 5647 |
non-static data members of array type. In this case, VEC_INIT_EXPR_INIT |
| 5648 |
will either be NULL_TREE for the default constructor, or a COMPONENT_REF |
| 5649 |
for the copy/move constructor. */ |
| 5650 |
|
| 5651 |
static tree |
| 5652 |
cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init, |
| 5653 |
bool value_init, value_cat lval, |
| 5654 |
bool *non_constant_p, bool *overflow_p) |
| 5655 |
{ |
| 5656 |
tree elttype = TREE_TYPE (atype); |
| 5657 |
verify_ctor_sanity (ctx, atype); |
| 5658 |
vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor); |
| 5659 |
bool pre_init = false; |
| 5660 |
unsigned HOST_WIDE_INT i; |
| 5661 |
tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; |
| 5662 |
|
| 5663 |
if (init && TREE_CODE (init) == CONSTRUCTOR) |
| 5664 |
return cxx_eval_bare_aggregate (ctx, init, lval, |
| 5665 |
non_constant_p, overflow_p); |
| 5666 |
|
| 5667 |
/* We already checked access when building the VEC_INIT_EXPR. */ |
| 5668 |
deferring_access_check_sentinel acs (dk_deferred); |
| 5669 |
|
| 5670 |
/* For the default constructor, build up a call to the default |
| 5671 |
constructor of the element type. We only need to handle class types |
| 5672 |
here, as for a constructor to be constexpr, all members must be |
| 5673 |
initialized, which for a defaulted default constructor means they must |
| 5674 |
be of a class type with a constexpr default constructor. */ |
| 5675 |
if (TREE_CODE (elttype) == ARRAY_TYPE) |
| 5676 |
/* We only do this at the lowest level. */; |
| 5677 |
else if (value_init) |
| 5678 |
{ |
| 5679 |
init = build_value_init (elttype, complain); |
| 5680 |
pre_init = true; |
| 5681 |
} |
| 5682 |
else if (!init) |
| 5683 |
{ |
| 5684 |
releasing_vec argvec; |
| 5685 |
init = build_special_member_call (NULL_TREE, complete_ctor_identifier, |
| 5686 |
&argvec, elttype, LOOKUP_NORMAL, |
| 5687 |
complain); |
| 5688 |
init = build_aggr_init_expr (elttype, init); |
| 5689 |
pre_init = true; |
| 5690 |
} |
| 5691 |
|
| 5692 |
bool zeroed_out = false; |
| 5693 |
if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor)) |
| 5694 |
{ |
| 5695 |
/* We're initializing an array object that had been zero-initialized |
| 5696 |
earlier. Truncate ctx->ctor, and propagate its zeroed state by |
| 5697 |
clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element |
| 5698 |
initializers we append to it. */ |
| 5699 |
gcc_checking_assert (initializer_zerop (ctx->ctor)); |
| 5700 |
zeroed_out = true; |
| 5701 |
vec_safe_truncate (*p, 0); |
| 5702 |
} |
| 5703 |
|
| 5704 |
tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p, |
| 5705 |
overflow_p); |
| 5706 |
unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts); |
| 5707 |
for (i = 0; i < max; ++i) |
| 5708 |
{ |
| 5709 |
tree idx = build_int_cst (size_type_node, i); |
| 5710 |
tree eltinit; |
| 5711 |
bool reuse = false; |
| 5712 |
constexpr_ctx new_ctx; |
| 5713 |
init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype); |
| 5714 |
bool no_slot = new_ctx.ctor == NULL_TREE; |
| 5715 |
if (new_ctx.ctor != ctx->ctor && !no_slot) |
| 5716 |
{ |
| 5717 |
if (zeroed_out) |
| 5718 |
CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false; |
| 5719 |
CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor); |
| 5720 |
} |
| 5721 |
if (TREE_CODE (elttype) == ARRAY_TYPE) |
| 5722 |
{ |
| 5723 |
/* A multidimensional array; recurse. */ |
| 5724 |
if (value_init || init == NULL_TREE) |
| 5725 |
{ |
| 5726 |
eltinit = NULL_TREE; |
| 5727 |
reuse = i == 0; |
| 5728 |
} |
| 5729 |
else |
| 5730 |
eltinit = cp_build_array_ref (input_location, init, idx, complain); |
| 5731 |
eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init, |
| 5732 |
lval, |
| 5733 |
non_constant_p, overflow_p); |
| 5734 |
} |
| 5735 |
else if (pre_init) |
| 5736 |
{ |
| 5737 |
/* Initializing an element using value or default initialization |
| 5738 |
we just pre-built above. */ |
| 5739 |
if (init == void_node) |
| 5740 |
/* Trivial default-init, don't do anything to the CONSTRUCTOR. */ |
| 5741 |
return ctx->ctor; |
| 5742 |
eltinit = init; |
| 5743 |
if (CLASS_TYPE_P (elttype) && new_ctx.object) |
| 5744 |
/* Clarify what object is being initialized (118285). */ |
| 5745 |
eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit); |
| 5746 |
eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval, |
| 5747 |
non_constant_p, overflow_p); |
| 5748 |
reuse = i == 0; |
| 5749 |
} |
| 5750 |
else |
| 5751 |
{ |
| 5752 |
/* Copying an element. */ |
| 5753 |
eltinit = cp_build_array_ref (input_location, init, idx, complain); |
| 5754 |
if (!lvalue_p (init)) |
| 5755 |
eltinit = move (eltinit); |
| 5756 |
eltinit = (perform_implicit_conversion_flags |
| 5757 |
(elttype, eltinit, complain, |
| 5758 |
LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING)); |
| 5759 |
if (CLASS_TYPE_P (elttype) |
| 5760 |
&& new_ctx.object |
| 5761 |
&& !error_operand_p (eltinit)) |
| 5762 |
/* Clarify what object is being initialized (118285). */ |
| 5763 |
eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit); |
| 5764 |
eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval, |
| 5765 |
non_constant_p, overflow_p); |
| 5766 |
} |
| 5767 |
if (*non_constant_p) |
| 5768 |
break; |
| 5769 |
if (no_slot) |
| 5770 |
{ |
| 5771 |
/* This is an initializer for an empty subobject; now that we've |
| 5772 |
checked that it's constant, we can ignore it. */ |
| 5773 |
gcc_checking_assert (i == 0); |
| 5774 |
break; |
| 5775 |
} |
| 5776 |
else if (new_ctx.ctor != ctx->ctor) |
| 5777 |
{ |
| 5778 |
/* We appended this element above; update the value. */ |
| 5779 |
gcc_assert ((*p)->last().index == idx); |
| 5780 |
(*p)->last().value = eltinit; |
| 5781 |
} |
| 5782 |
else |
| 5783 |
CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit); |
| 5784 |
/* Reuse the result of cxx_eval_constant_expression call |
| 5785 |
from the first iteration to all others if it is a constant |
| 5786 |
initializer that doesn't require relocations. */ |
| 5787 |
if (reuse |
| 5788 |
&& max > 1 |
| 5789 |
&& (eltinit == NULL_TREE |
| 5790 |
|| (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit)) |
| 5791 |
== null_pointer_node))) |
| 5792 |
{ |
| 5793 |
if (new_ctx.ctor != ctx->ctor) |
| 5794 |
eltinit = new_ctx.ctor; |
| 5795 |
tree range = build2 (RANGE_EXPR, size_type_node, |
| 5796 |
build_int_cst (size_type_node, 1), |
| 5797 |
build_int_cst (size_type_node, max - 1)); |
| 5798 |
CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit)); |
| 5799 |
break; |
| 5800 |
} |
| 5801 |
else if (i == 0) |
| 5802 |
vec_safe_reserve (*p, max); |
| 5803 |
} |
| 5804 |
|
| 5805 |
if (!*non_constant_p) |
| 5806 |
{ |
| 5807 |
init = ctx->ctor; |
| 5808 |
CONSTRUCTOR_NO_CLEARING (init) = false; |
| 5809 |
} |
| 5810 |
return init; |
| 5811 |
} |
| 5812 |
|
| 5813 |
static tree |
| 5814 |
cxx_eval_vec_init (const constexpr_ctx *ctx, tree t, |
| 5815 |
value_cat lval, |
| 5816 |
bool *non_constant_p, bool *overflow_p) |
| 5817 |
{ |
| 5818 |
tree atype = TREE_TYPE (t); |
| 5819 |
tree init = VEC_INIT_EXPR_INIT (t); |
| 5820 |
bool value_init = VEC_INIT_EXPR_VALUE_INIT (t); |
| 5821 |
if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init)) |
| 5822 |
; |
| 5823 |
else if (CONSTRUCTOR_NELTS (init) == 0 |
| 5824 |
&& !CP_AGGREGATE_TYPE_P (strip_array_types (atype))) |
| 5825 |
{ |
| 5826 |
/* Handle {} as value-init. */ |
| 5827 |
init = NULL_TREE; |
| 5828 |
value_init = true; |
| 5829 |
} |
| 5830 |
else |
| 5831 |
{ |
| 5832 |
/* This is a more complicated case, like needing to loop over trailing |
| 5833 |
elements; call build_vec_init and evaluate the result. */ |
| 5834 |
tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error; |
| 5835 |
constexpr_ctx new_ctx = *ctx; |
| 5836 |
if (!ctx->object) |
| 5837 |
{ |
| 5838 |
/* We want to have an initialization target for an VEC_INIT_EXPR. |
| 5839 |
If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */ |
| 5840 |
new_ctx.object = VEC_INIT_EXPR_SLOT (t); |
| 5841 |
tree ctor = new_ctx.ctor = build_constructor (atype, NULL); |
| 5842 |
CONSTRUCTOR_NO_CLEARING (ctor) = true; |
| 5843 |
ctx->global->put_value (new_ctx.object, ctor); |
| 5844 |
ctx = &new_ctx; |
| 5845 |
} |
| 5846 |
init = expand_vec_init_expr (ctx->object, t, complain); |
| 5847 |
return cxx_eval_constant_expression (ctx, init, lval, non_constant_p, |
| 5848 |
overflow_p); |
| 5849 |
} |
| 5850 |
tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init, |
| 5851 |
lval, non_constant_p, overflow_p); |
| 5852 |
if (*non_constant_p) |
| 5853 |
return t; |
| 5854 |
else |
| 5855 |
return r; |
| 5856 |
} |
| 5857 |
|
| 5858 |
/* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case |
| 5859 |
where the desired type is an array of unknown bounds because the variable |
| 5860 |
has had its bounds deduced since the wrapping expression was created. */ |
| 5861 |
|
| 5862 |
static bool |
| 5863 |
same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2) |
| 5864 |
{ |
| 5865 |
while (TREE_CODE (type1) == ARRAY_TYPE |
| 5866 |
&& TREE_CODE (type2) == ARRAY_TYPE |
| 5867 |
&& (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2))) |
| 5868 |
{ |
| 5869 |
type1 = TREE_TYPE (type1); |
| 5870 |
type2 = TREE_TYPE (type2); |
| 5871 |
} |
| 5872 |
return same_type_ignoring_top_level_qualifiers_p (type1, type2); |
| 5873 |
} |
| 5874 |
|
| 5875 |
/* Try to determine the currently active union member for an expression |
| 5876 |
with UNION_TYPE. If it can be determined, return the FIELD_DECL, |
| 5877 |
otherwise return NULL_TREE. */ |
| 5878 |
|
| 5879 |
static tree |
| 5880 |
cxx_union_active_member (const constexpr_ctx *ctx, tree t) |
| 5881 |
{ |
| 5882 |
constexpr_ctx new_ctx = *ctx; |
| 5883 |
new_ctx.quiet = true; |
| 5884 |
bool non_constant_p = false, overflow_p = false; |
| 5885 |
tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue, |
| 5886 |
&non_constant_p, |
| 5887 |
&overflow_p); |
| 5888 |
if (TREE_CODE (ctor) == CONSTRUCTOR |
| 5889 |
&& CONSTRUCTOR_NELTS (ctor) == 1 |
| 5890 |
&& CONSTRUCTOR_ELT (ctor, 0)->index |
| 5891 |
&& TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL) |
| 5892 |
return CONSTRUCTOR_ELT (ctor, 0)->index; |
| 5893 |
return NULL_TREE; |
| 5894 |
} |
| 5895 |
|
| 5896 |
/* Helper function for cxx_fold_indirect_ref_1, called recursively. */ |
| 5897 |
|
| 5898 |
static tree |
| 5899 |
cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type, |
| 5900 |
tree op, unsigned HOST_WIDE_INT off, bool *empty_base) |
| 5901 |
{ |
| 5902 |
tree optype = TREE_TYPE (op); |
| 5903 |
unsigned HOST_WIDE_INT const_nunits; |
| 5904 |
if (off == 0 && similar_type_p (optype, type)) |
| 5905 |
return op; |
| 5906 |
else if (TREE_CODE (optype) == COMPLEX_TYPE |
| 5907 |
&& similar_type_p (type, TREE_TYPE (optype))) |
| 5908 |
{ |
| 5909 |
/* *(foo *)&complexfoo => __real__ complexfoo */ |
| 5910 |
if (off == 0) |
| 5911 |
return build1_loc (loc, REALPART_EXPR, type, op); |
| 5912 |
/* ((foo*)&complexfoo)[1] => __imag__ complexfoo */ |
| 5913 |
else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off) |
| 5914 |
return build1_loc (loc, IMAGPART_EXPR, type, op); |
| 5915 |
} |
| 5916 |
/* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */ |
| 5917 |
else if (VECTOR_TYPE_P (optype) |
| 5918 |
&& similar_type_p (type, TREE_TYPE (optype)) |
| 5919 |
&& TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits)) |
| 5920 |
{ |
| 5921 |
unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type)); |
| 5922 |
unsigned HOST_WIDE_INT max_offset = part_width * const_nunits; |
| 5923 |
if (off < max_offset && off % part_width == 0) |
| 5924 |
{ |
| 5925 |
tree index = bitsize_int (off * BITS_PER_UNIT); |
| 5926 |
return build3_loc (loc, BIT_FIELD_REF, type, op, |
| 5927 |
TYPE_SIZE (type), index); |
| 5928 |
} |
| 5929 |
} |
| 5930 |
/* ((foo *)&fooarray)[x] => fooarray[x] */ |
| 5931 |
else if (TREE_CODE (optype) == ARRAY_TYPE |
| 5932 |
&& tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype))) |
| 5933 |
&& !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype)))) |
| 5934 |
{ |
| 5935 |
tree type_domain = TYPE_DOMAIN (optype); |
| 5936 |
tree min_val = size_zero_node; |
| 5937 |
if (type_domain && TYPE_MIN_VALUE (type_domain)) |
| 5938 |
min_val = TYPE_MIN_VALUE (type_domain); |
| 5939 |
unsigned HOST_WIDE_INT el_sz |
| 5940 |
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype))); |
| 5941 |
unsigned HOST_WIDE_INT idx = off / el_sz; |
| 5942 |
unsigned HOST_WIDE_INT rem = off % el_sz; |
| 5943 |
if (tree_fits_uhwi_p (min_val)) |
| 5944 |
{ |
| 5945 |
tree index = size_int (idx + tree_to_uhwi (min_val)); |
| 5946 |
op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index, |
| 5947 |
NULL_TREE, NULL_TREE); |
| 5948 |
return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem, |
| 5949 |
empty_base); |
| 5950 |
} |
| 5951 |
} |
| 5952 |
/* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */ |
| 5953 |
else if (TREE_CODE (optype) == RECORD_TYPE |
| 5954 |
|| TREE_CODE (optype) == UNION_TYPE) |
| 5955 |
{ |
| 5956 |
if (TREE_CODE (optype) == UNION_TYPE) |
| 5957 |
/* For unions prefer the currently active member. */ |
| 5958 |
if (tree field = cxx_union_active_member (ctx, op)) |
| 5959 |
{ |
| 5960 |
unsigned HOST_WIDE_INT el_sz |
| 5961 |
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field))); |
| 5962 |
if (off < el_sz) |
| 5963 |
{ |
| 5964 |
tree cop = build3 (COMPONENT_REF, TREE_TYPE (field), |
| 5965 |
op, field, NULL_TREE); |
| 5966 |
if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop, |
| 5967 |
off, empty_base)) |
| 5968 |
return ret; |
| 5969 |
} |
| 5970 |
} |
| 5971 |
|
| 5972 |
/* Handle conversion to "as base" type. */ |
| 5973 |
if (CLASS_TYPE_P (optype) |
| 5974 |
&& CLASSTYPE_AS_BASE (optype) == type) |
| 5975 |
return op; |
| 5976 |
|
| 5977 |
/* Handle conversion to an empty base class, which is represented with a |
| 5978 |
NOP_EXPR. Do this before spelunking into the non-empty subobjects, |
| 5979 |
which is likely to be a waste of time (109678). */ |
| 5980 |
if (is_empty_class (type) |
| 5981 |
&& CLASS_TYPE_P (optype) |
| 5982 |
&& lookup_base (optype, type, ba_any, NULL, tf_none, off)) |
| 5983 |
{ |
| 5984 |
if (empty_base) |
| 5985 |
*empty_base = true; |
| 5986 |
return op; |
| 5987 |
} |
| 5988 |
|
| 5989 |
for (tree field = TYPE_FIELDS (optype); |
| 5990 |
field; field = DECL_CHAIN (field)) |
| 5991 |
if (TREE_CODE (field) == FIELD_DECL |
| 5992 |
&& TREE_TYPE (field) != error_mark_node |
| 5993 |
&& tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field)))) |
| 5994 |
{ |
| 5995 |
tree pos = byte_position (field); |
| 5996 |
if (!tree_fits_uhwi_p (pos)) |
| 5997 |
continue; |
| 5998 |
unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos); |
| 5999 |
unsigned HOST_WIDE_INT el_sz |
| 6000 |
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field))); |
| 6001 |
if (upos <= off && off < upos + el_sz) |
| 6002 |
{ |
| 6003 |
tree cop = build3 (COMPONENT_REF, TREE_TYPE (field), |
| 6004 |
op, field, NULL_TREE); |
| 6005 |
if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop, |
| 6006 |
off - upos, |
| 6007 |
empty_base)) |
| 6008 |
return ret; |
| 6009 |
} |
| 6010 |
} |
| 6011 |
} |
| 6012 |
|
| 6013 |
return NULL_TREE; |
| 6014 |
} |
| 6015 |
|
| 6016 |
/* A less strict version of fold_indirect_ref_1, which requires cv-quals to |
| 6017 |
match. We want to be less strict for simple *& folding; if we have a |
| 6018 |
non-const temporary that we access through a const pointer, that should |
| 6019 |
work. We handle this here rather than change fold_indirect_ref_1 |
| 6020 |
because we're dealing with things like ADDR_EXPR of INTEGER_CST which |
| 6021 |
don't really make sense outside of constant expression evaluation. Also |
| 6022 |
we want to allow folding to COMPONENT_REF, which could cause trouble |
| 6023 |
with TBAA in fold_indirect_ref_1. */ |
| 6024 |
|
| 6025 |
static tree |
| 6026 |
cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type, |
| 6027 |
tree op0, bool *empty_base /* = NULL*/) |
| 6028 |
{ |
| 6029 |
tree sub = op0; |
| 6030 |
tree subtype; |
| 6031 |
|
| 6032 |
/* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */ |
| 6033 |
while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR |
| 6034 |
|| TREE_CODE (sub) == VIEW_CONVERT_EXPR) |
| 6035 |
{ |
| 6036 |
if (TREE_CODE (sub) == NOP_EXPR |
| 6037 |
&& REINTERPRET_CAST_P (sub)) |
| 6038 |
return NULL_TREE; |
| 6039 |
sub = TREE_OPERAND (sub, 0); |
| 6040 |
} |
| 6041 |
|
| 6042 |
subtype = TREE_TYPE (sub); |
| 6043 |
if (!INDIRECT_TYPE_P (subtype)) |
| 6044 |
return NULL_TREE; |
| 6045 |
|
| 6046 |
/* Canonicalizes the given OBJ/OFF pair by iteratively absorbing |
| 6047 |
the innermost component into the offset until it would make the |
| 6048 |
offset positive, so that cxx_fold_indirect_ref_1 can identify |
| 6049 |
more folding opportunities. */ |
| 6050 |
auto canonicalize_obj_off = [] (tree& obj, tree& off) { |
| 6051 |
while (TREE_CODE (obj) == COMPONENT_REF |
| 6052 |
/* We need to preserve union member accesses so that we can |
| 6053 |
later properly diagnose accessing the wrong member. */ |
| 6054 |
&& TREE_CODE (TREE_TYPE (TREE_OPERAND (obj, 0))) == RECORD_TYPE |
| 6055 |
&& (tree_int_cst_sign_bit (off) || integer_zerop (off))) |
| 6056 |
{ |
| 6057 |
tree field = TREE_OPERAND (obj, 1); |
| 6058 |
tree pos = byte_position (field); |
| 6059 |
if (integer_zerop (off) && integer_nonzerop (pos)) |
| 6060 |
/* If the offset is already 0, keep going as long as the |
| 6061 |
component is at position 0. */ |
| 6062 |
break; |
| 6063 |
off = int_const_binop (PLUS_EXPR, off, pos); |
| 6064 |
obj = TREE_OPERAND (obj, 0); |
| 6065 |
} |
| 6066 |
}; |
| 6067 |
|
| 6068 |
if (TREE_CODE (sub) == ADDR_EXPR) |
| 6069 |
{ |
| 6070 |
tree op = TREE_OPERAND (sub, 0); |
| 6071 |
tree optype = TREE_TYPE (op); |
| 6072 |
|
| 6073 |
/* *&CONST_DECL -> to the value of the const decl. */ |
| 6074 |
if (TREE_CODE (op) == CONST_DECL) |
| 6075 |
return DECL_INITIAL (op); |
| 6076 |
/* *&p => p; make sure to handle *&"str"[cst] here. */ |
| 6077 |
if (similar_type_p (optype, type)) |
| 6078 |
{ |
| 6079 |
tree fop = fold_read_from_constant_string (op); |
| 6080 |
if (fop) |
| 6081 |
return fop; |
| 6082 |
else |
| 6083 |
return op; |
| 6084 |
} |
| 6085 |
else |
| 6086 |
{ |
| 6087 |
tree off = integer_zero_node; |
| 6088 |
canonicalize_obj_off (op, off); |
| 6089 |
gcc_assert (integer_zerop (off)); |
| 6090 |
return cxx_fold_indirect_ref_1 (ctx, loc, type, op, 0, empty_base); |
| 6091 |
} |
| 6092 |
} |
| 6093 |
else if (TREE_CODE (sub) == POINTER_PLUS_EXPR |
| 6094 |
&& tree_fits_uhwi_p (TREE_OPERAND (sub, 1))) |
| 6095 |
{ |
| 6096 |
tree op00 = TREE_OPERAND (sub, 0); |
| 6097 |
tree off = TREE_OPERAND (sub, 1); |
| 6098 |
|
| 6099 |
STRIP_NOPS (op00); |
| 6100 |
if (TREE_CODE (op00) == ADDR_EXPR) |
| 6101 |
{ |
| 6102 |
tree obj = TREE_OPERAND (op00, 0); |
| 6103 |
canonicalize_obj_off (obj, off); |
| 6104 |
return cxx_fold_indirect_ref_1 (ctx, loc, type, obj, |
| 6105 |
tree_to_uhwi (off), empty_base); |
| 6106 |
} |
| 6107 |
} |
| 6108 |
/* *(foo *)fooarrptr => (*fooarrptr)[0] */ |
| 6109 |
else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE |
| 6110 |
&& similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype)))) |
| 6111 |
{ |
| 6112 |
tree type_domain; |
| 6113 |
tree min_val = size_zero_node; |
| 6114 |
tree newsub |
| 6115 |
= cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL); |
| 6116 |
if (newsub) |
| 6117 |
sub = newsub; |
| 6118 |
else |
| 6119 |
sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub); |
| 6120 |
type_domain = TYPE_DOMAIN (TREE_TYPE (sub)); |
| 6121 |
if (type_domain && TYPE_MIN_VALUE (type_domain)) |
| 6122 |
min_val = TYPE_MIN_VALUE (type_domain); |
| 6123 |
return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE, |
| 6124 |
NULL_TREE); |
| 6125 |
} |
| 6126 |
|
| 6127 |
return NULL_TREE; |
| 6128 |
} |
| 6129 |
|
| 6130 |
static tree |
| 6131 |
cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t, |
| 6132 |
value_cat lval, |
| 6133 |
bool *non_constant_p, bool *overflow_p) |
| 6134 |
{ |
| 6135 |
tree orig_op0 = TREE_OPERAND (t, 0); |
| 6136 |
bool empty_base = false; |
| 6137 |
|
| 6138 |
/* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second |
| 6139 |
operand is an integer-zero. Otherwise reject the MEM_REF for now. */ |
| 6140 |
|
| 6141 |
if (TREE_CODE (t) == MEM_REF |
| 6142 |
&& (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1)))) |
| 6143 |
{ |
| 6144 |
gcc_assert (ctx->quiet); |
| 6145 |
*non_constant_p = true; |
| 6146 |
return t; |
| 6147 |
} |
| 6148 |
|
| 6149 |
/* First try to simplify it directly. */ |
| 6150 |
tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), |
| 6151 |
orig_op0, &empty_base); |
| 6152 |
if (!r) |
| 6153 |
{ |
| 6154 |
/* If that didn't work, evaluate the operand first. */ |
| 6155 |
tree op0 = cxx_eval_constant_expression (ctx, orig_op0, |
| 6156 |
vc_prvalue, non_constant_p, |
| 6157 |
overflow_p); |
| 6158 |
/* Don't VERIFY_CONSTANT here. */ |
| 6159 |
if (*non_constant_p) |
| 6160 |
return t; |
| 6161 |
|
| 6162 |
if (!lval && integer_zerop (op0)) |
| 6163 |
{ |
| 6164 |
if (!ctx->quiet) |
| 6165 |
error ("dereferencing a null pointer"); |
| 6166 |
*non_constant_p = true; |
| 6167 |
return t; |
| 6168 |
} |
| 6169 |
|
| 6170 |
r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0, |
| 6171 |
&empty_base); |
| 6172 |
if (r == NULL_TREE) |
| 6173 |
{ |
| 6174 |
/* We couldn't fold to a constant value. Make sure it's not |
| 6175 |
something we should have been able to fold. */ |
| 6176 |
tree sub = op0; |
| 6177 |
STRIP_NOPS (sub); |
| 6178 |
if (TREE_CODE (sub) == ADDR_EXPR) |
| 6179 |
{ |
| 6180 |
gcc_assert (!similar_type_p |
| 6181 |
(TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t))); |
| 6182 |
/* DR 1188 says we don't have to deal with this. */ |
| 6183 |
if (!ctx->quiet) |
| 6184 |
error_at (cp_expr_loc_or_input_loc (t), |
| 6185 |
"accessing value of %qE through a %qT glvalue in a " |
| 6186 |
"constant expression", build_fold_indirect_ref (sub), |
| 6187 |
TREE_TYPE (t)); |
| 6188 |
*non_constant_p = true; |
| 6189 |
return t; |
| 6190 |
} |
| 6191 |
|
| 6192 |
if (lval == vc_glvalue && op0 != orig_op0) |
| 6193 |
return build1 (INDIRECT_REF, TREE_TYPE (t), op0); |
| 6194 |
if (!lval) |
| 6195 |
VERIFY_CONSTANT (t); |
| 6196 |
return t; |
| 6197 |
} |
| 6198 |
} |
| 6199 |
|
| 6200 |
r = cxx_eval_constant_expression (ctx, r, |
| 6201 |
lval, non_constant_p, overflow_p); |
| 6202 |
if (*non_constant_p) |
| 6203 |
return t; |
| 6204 |
|
| 6205 |
/* If we're pulling out the value of an empty base, just return an empty |
| 6206 |
CONSTRUCTOR. */ |
| 6207 |
if (empty_base && !lval) |
| 6208 |
{ |
| 6209 |
r = build_constructor (TREE_TYPE (t), NULL); |
| 6210 |
TREE_CONSTANT (r) = true; |
| 6211 |
} |
| 6212 |
|
| 6213 |
return r; |
| 6214 |
} |
| 6215 |
|
| 6216 |
/* Complain about R, a DECL that is accessed outside its lifetime. */ |
| 6217 |
|
| 6218 |
static void |
| 6219 |
outside_lifetime_error (location_t loc, tree r) |
| 6220 |
{ |
| 6221 |
auto_diagnostic_group d; |
| 6222 |
if (DECL_NAME (r) == heap_deleted_identifier) |
| 6223 |
{ |
| 6224 |
/* Provide a more accurate message for deleted variables. */ |
| 6225 |
error_at (loc, "use of allocated storage after deallocation " |
| 6226 |
"in a constant expression"); |
| 6227 |
inform (DECL_SOURCE_LOCATION (r), "allocated here"); |
| 6228 |
} |
| 6229 |
else |
| 6230 |
{ |
| 6231 |
error_at (loc, "accessing %qE outside its lifetime", r); |
| 6232 |
inform (DECL_SOURCE_LOCATION (r), "declared here"); |
| 6233 |
} |
| 6234 |
} |
| 6235 |
|
| 6236 |
/* Complain about R, a VAR_DECL, not being usable in a constant expression. |
| 6237 |
FUNDEF_P is true if we're checking a constexpr function body. |
| 6238 |
Shared between potential_constant_expression and |
| 6239 |
cxx_eval_constant_expression. */ |
| 6240 |
|
| 6241 |
static void |
| 6242 |
non_const_var_error (location_t loc, tree r, bool fundef_p) |
| 6243 |
{ |
| 6244 |
auto_diagnostic_group d; |
| 6245 |
tree type = TREE_TYPE (r); |
| 6246 |
if (DECL_NAME (r) == heap_uninit_identifier |
| 6247 |
|| DECL_NAME (r) == heap_identifier |
| 6248 |
|| DECL_NAME (r) == heap_vec_uninit_identifier |
| 6249 |
|| DECL_NAME (r) == heap_vec_identifier) |
| 6250 |
{ |
| 6251 |
if (constexpr_error (loc, fundef_p, "the content of uninitialized " |
| 6252 |
"storage is not usable in a constant expression")) |
| 6253 |
inform (DECL_SOURCE_LOCATION (r), "allocated here"); |
| 6254 |
return; |
| 6255 |
} |
| 6256 |
if (DECL_NAME (r) == heap_deleted_identifier) |
| 6257 |
{ |
| 6258 |
if (constexpr_error (loc, fundef_p, "use of allocated storage after " |
| 6259 |
"deallocation in a constant expression")) |
| 6260 |
inform (DECL_SOURCE_LOCATION (r), "allocated here"); |
| 6261 |
return; |
| 6262 |
} |
| 6263 |
if (!constexpr_error (loc, fundef_p, "the value of %qD is not usable in " |
| 6264 |
"a constant expression", r)) |
| 6265 |
return; |
| 6266 |
/* Avoid error cascade. */ |
| 6267 |
if (DECL_INITIAL (r) == error_mark_node) |
| 6268 |
return; |
| 6269 |
if (DECL_DECLARED_CONSTEXPR_P (r)) |
| 6270 |
inform (DECL_SOURCE_LOCATION (r), |
| 6271 |
"%qD used in its own initializer", r); |
| 6272 |
else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) |
| 6273 |
{ |
| 6274 |
if (!CP_TYPE_CONST_P (type)) |
| 6275 |
inform (DECL_SOURCE_LOCATION (r), |
| 6276 |
"%q#D is not const", r); |
| 6277 |
else if (CP_TYPE_VOLATILE_P (type)) |
| 6278 |
inform (DECL_SOURCE_LOCATION (r), |
| 6279 |
"%q#D is volatile", r); |
| 6280 |
else if (!DECL_INITIAL (r) |
| 6281 |
|| !TREE_CONSTANT (DECL_INITIAL (r)) |
| 6282 |
|| !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)) |
| 6283 |
inform (DECL_SOURCE_LOCATION (r), |
| 6284 |
"%qD was not initialized with a constant " |
| 6285 |
"expression", r); |
| 6286 |
else |
| 6287 |
gcc_unreachable (); |
| 6288 |
} |
| 6289 |
else if (TYPE_REF_P (type)) |
| 6290 |
inform (DECL_SOURCE_LOCATION (r), |
| 6291 |
"%qD was not initialized with a constant " |
| 6292 |
"expression", r); |
| 6293 |
else |
| 6294 |
{ |
| 6295 |
if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r)) |
| 6296 |
inform (DECL_SOURCE_LOCATION (r), |
| 6297 |
"%qD was not declared %<constexpr%>", r); |
| 6298 |
else |
| 6299 |
inform (DECL_SOURCE_LOCATION (r), |
| 6300 |
"%qD does not have integral or enumeration type", |
| 6301 |
r); |
| 6302 |
} |
| 6303 |
} |
| 6304 |
|
| 6305 |
/* Subroutine of cxx_eval_constant_expression. |
| 6306 |
Like cxx_eval_unary_expression, except for trinary expressions. */ |
| 6307 |
|
| 6308 |
static tree |
| 6309 |
cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t, |
| 6310 |
value_cat lval, |
| 6311 |
bool *non_constant_p, bool *overflow_p) |
| 6312 |
{ |
| 6313 |
int i; |
| 6314 |
tree args[3]; |
| 6315 |
tree val; |
| 6316 |
|
| 6317 |
for (i = 0; i < 3; i++) |
| 6318 |
{ |
| 6319 |
args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i), |
| 6320 |
lval, |
| 6321 |
non_constant_p, overflow_p); |
| 6322 |
VERIFY_CONSTANT (args[i]); |
| 6323 |
} |
| 6324 |
|
| 6325 |
val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t), |
| 6326 |
args[0], args[1], args[2]); |
| 6327 |
if (val == NULL_TREE) |
| 6328 |
return t; |
| 6329 |
VERIFY_CONSTANT (val); |
| 6330 |
return val; |
| 6331 |
} |
| 6332 |
|
| 6333 |
/* True if T was declared in a function declared to be constexpr, and |
| 6334 |
therefore potentially constant in C++14. */ |
| 6335 |
|
| 6336 |
bool |
| 6337 |
var_in_constexpr_fn (tree t) |
| 6338 |
{ |
| 6339 |
tree ctx = DECL_CONTEXT (t); |
| 6340 |
return (ctx && TREE_CODE (ctx) == FUNCTION_DECL |
| 6341 |
&& DECL_DECLARED_CONSTEXPR_P (ctx)); |
| 6342 |
} |
| 6343 |
|
| 6344 |
/* True if a function might be constexpr: either a function that was |
| 6345 |
declared constexpr, or a C++17 lambda op(). */ |
| 6346 |
|
| 6347 |
bool |
| 6348 |
maybe_constexpr_fn (tree t) |
| 6349 |
{ |
| 6350 |
return (DECL_DECLARED_CONSTEXPR_P (t) |
| 6351 |
|| (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t)) |
| 6352 |
|| (flag_implicit_constexpr |
| 6353 |
&& DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t)))); |
| 6354 |
} |
| 6355 |
|
| 6356 |
/* True if T was declared in a function that might be constexpr: either a |
| 6357 |
function that was declared constexpr, or a C++17 lambda op(). */ |
| 6358 |
|
| 6359 |
bool |
| 6360 |
var_in_maybe_constexpr_fn (tree t) |
| 6361 |
{ |
| 6362 |
return (DECL_FUNCTION_SCOPE_P (t) |
| 6363 |
&& maybe_constexpr_fn (DECL_CONTEXT (t))); |
| 6364 |
} |
| 6365 |
|
| 6366 |
/* We're assigning INIT to TARGET. In do_build_copy_constructor and |
| 6367 |
build_over_call we implement trivial copy of a class with tail padding using |
| 6368 |
assignment of character arrays, which is valid in normal code, but not in |
| 6369 |
constexpr evaluation. We don't need to worry about clobbering tail padding |
| 6370 |
in constexpr evaluation, so strip the type punning. */ |
| 6371 |
|
| 6372 |
static void |
| 6373 |
maybe_simplify_trivial_copy (tree &target, tree &init) |
| 6374 |
{ |
| 6375 |
if (TREE_CODE (target) == MEM_REF |
| 6376 |
&& TREE_CODE (init) == MEM_REF |
| 6377 |
&& TREE_TYPE (target) == TREE_TYPE (init) |
| 6378 |
&& TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE |
| 6379 |
&& TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node) |
| 6380 |
{ |
| 6381 |
target = build_fold_indirect_ref (TREE_OPERAND (target, 0)); |
| 6382 |
init = build_fold_indirect_ref (TREE_OPERAND (init, 0)); |
| 6383 |
} |
| 6384 |
} |
| 6385 |
|
| 6386 |
/* Returns true if REF, which is a COMPONENT_REF, has any fields |
| 6387 |
of constant type. This does not check for 'mutable', so the |
| 6388 |
caller is expected to be mindful of that. */ |
| 6389 |
|
| 6390 |
static bool |
| 6391 |
cref_has_const_field (tree ref) |
| 6392 |
{ |
| 6393 |
while (TREE_CODE (ref) == COMPONENT_REF) |
| 6394 |
{ |
| 6395 |
if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1)))) |
| 6396 |
return true; |
| 6397 |
ref = TREE_OPERAND (ref, 0); |
| 6398 |
} |
| 6399 |
return false; |
| 6400 |
} |
| 6401 |
|
| 6402 |
/* Return true if we are modifying something that is const during constant |
| 6403 |
expression evaluation. CODE is the code of the statement, OBJ is the |
| 6404 |
object in question, MUTABLE_P is true if one of the subobjects were |
| 6405 |
declared mutable. */ |
| 6406 |
|
| 6407 |
static bool |
| 6408 |
modifying_const_object_p (tree_code code, tree obj, bool mutable_p) |
| 6409 |
{ |
| 6410 |
/* If this is initialization, there's no problem. */ |
| 6411 |
if (code != MODIFY_EXPR) |
| 6412 |
return false; |
| 6413 |
|
| 6414 |
/* [basic.type.qualifier] "A const object is an object of type |
| 6415 |
const T or a non-mutable subobject of a const object." */ |
| 6416 |
if (mutable_p) |
| 6417 |
return false; |
| 6418 |
|
| 6419 |
if (TREE_READONLY (obj)) |
| 6420 |
return true; |
| 6421 |
|
| 6422 |
if (CP_TYPE_CONST_P (TREE_TYPE (obj))) |
| 6423 |
{ |
| 6424 |
/* Although a COMPONENT_REF may have a const type, we should |
| 6425 |
only consider it modifying a const object when any of the |
| 6426 |
field components is const. This can happen when using |
| 6427 |
constructs such as const_cast<const T &>(m), making something |
| 6428 |
const even though it wasn't declared const. */ |
| 6429 |
if (TREE_CODE (obj) == COMPONENT_REF) |
| 6430 |
return cref_has_const_field (obj); |
| 6431 |
else |
| 6432 |
return true; |
| 6433 |
} |
| 6434 |
|
| 6435 |
return false; |
| 6436 |
} |
| 6437 |
|
| 6438 |
/* Evaluate an INIT_EXPR or MODIFY_EXPR. */ |
| 6439 |
|
| 6440 |
static tree |
| 6441 |
cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, |
| 6442 |
value_cat lval, |
| 6443 |
bool *non_constant_p, bool *overflow_p) |
| 6444 |
{ |
| 6445 |
constexpr_ctx new_ctx = *ctx; |
| 6446 |
|
| 6447 |
tree init = TREE_OPERAND (t, 1); |
| 6448 |
|
| 6449 |
if (TREE_CLOBBER_P (init) |
| 6450 |
&& CLOBBER_KIND (init) < CLOBBER_OBJECT_END) |
| 6451 |
/* Only handle clobbers ending the lifetime of objects. |
| 6452 |
??? We should probably set CONSTRUCTOR_NO_CLEARING. */ |
| 6453 |
return void_node; |
| 6454 |
|
| 6455 |
/* First we figure out where we're storing to. */ |
| 6456 |
tree target = TREE_OPERAND (t, 0); |
| 6457 |
|
| 6458 |
maybe_simplify_trivial_copy (target, init); |
| 6459 |
|
| 6460 |
tree type = TREE_TYPE (target); |
| 6461 |
bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR; |
| 6462 |
if (preeval && !TREE_CLOBBER_P (init)) |
| 6463 |
{ |
| 6464 |
/* Evaluate the value to be stored without knowing what object it will be |
| 6465 |
stored in, so that any side-effects happen first. */ |
| 6466 |
if (!SCALAR_TYPE_P (type)) |
| 6467 |
new_ctx.ctor = new_ctx.object = NULL_TREE; |
| 6468 |
init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue, |
| 6469 |
non_constant_p, overflow_p); |
| 6470 |
if (*non_constant_p) |
| 6471 |
return t; |
| 6472 |
} |
| 6473 |
|
| 6474 |
bool evaluated = false; |
| 6475 |
if (lval == vc_glvalue) |
| 6476 |
{ |
| 6477 |
/* If we want to return a reference to the target, we need to evaluate it |
| 6478 |
as a whole; otherwise, only evaluate the innermost piece to avoid |
| 6479 |
building up unnecessary *_REFs. */ |
| 6480 |
target = cxx_eval_constant_expression (ctx, target, lval, |
| 6481 |
non_constant_p, overflow_p); |
| 6482 |
evaluated = true; |
| 6483 |
if (*non_constant_p) |
| 6484 |
return t; |
| 6485 |
} |
| 6486 |
|
| 6487 |
/* Find the underlying variable. */ |
| 6488 |
releasing_vec refs; |
| 6489 |
tree object = NULL_TREE; |
| 6490 |
/* If we're modifying a const object, save it. */ |
| 6491 |
tree const_object_being_modified = NULL_TREE; |
| 6492 |
bool mutable_p = false; |
| 6493 |
for (tree probe = target; object == NULL_TREE; ) |
| 6494 |
{ |
| 6495 |
switch (TREE_CODE (probe)) |
| 6496 |
{ |
| 6497 |
case BIT_FIELD_REF: |
| 6498 |
case COMPONENT_REF: |
| 6499 |
case ARRAY_REF: |
| 6500 |
{ |
| 6501 |
tree ob = TREE_OPERAND (probe, 0); |
| 6502 |
tree elt = TREE_OPERAND (probe, 1); |
| 6503 |
if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt)) |
| 6504 |
mutable_p = true; |
| 6505 |
if (TREE_CODE (probe) == ARRAY_REF) |
| 6506 |
{ |
| 6507 |
elt = eval_and_check_array_index (ctx, probe, false, |
| 6508 |
non_constant_p, overflow_p); |
| 6509 |
if (*non_constant_p) |
| 6510 |
return t; |
| 6511 |
} |
| 6512 |
/* We don't check modifying_const_object_p for ARRAY_REFs. Given |
| 6513 |
"int a[10]", an ARRAY_REF "a[2]" can be "const int", even though |
| 6514 |
the array isn't const. Instead, check "a" in the next iteration; |
| 6515 |
that will detect modifying "const int a[10]". */ |
| 6516 |
else if (evaluated |
| 6517 |
&& modifying_const_object_p (TREE_CODE (t), probe, |
| 6518 |
mutable_p) |
| 6519 |
&& const_object_being_modified == NULL_TREE) |
| 6520 |
const_object_being_modified = probe; |
| 6521 |
|
| 6522 |
/* Track named member accesses for unions to validate modifications |
| 6523 |
that change active member. */ |
| 6524 |
if (!evaluated && TREE_CODE (probe) == COMPONENT_REF) |
| 6525 |
vec_safe_push (refs, probe); |
| 6526 |
else |
| 6527 |
vec_safe_push (refs, NULL_TREE); |
| 6528 |
|
| 6529 |
vec_safe_push (refs, elt); |
| 6530 |
vec_safe_push (refs, TREE_TYPE (probe)); |
| 6531 |
probe = ob; |
| 6532 |
} |
| 6533 |
break; |
| 6534 |
|
| 6535 |
case REALPART_EXPR: |
| 6536 |
gcc_assert (refs->is_empty ()); |
| 6537 |
vec_safe_push (refs, NULL_TREE); |
| 6538 |
vec_safe_push (refs, probe); |
| 6539 |
vec_safe_push (refs, TREE_TYPE (probe)); |
| 6540 |
probe = TREE_OPERAND (probe, 0); |
| 6541 |
break; |
| 6542 |
|
| 6543 |
case IMAGPART_EXPR: |
| 6544 |
gcc_assert (refs->is_empty ()); |
| 6545 |
vec_safe_push (refs, NULL_TREE); |
| 6546 |
vec_safe_push (refs, probe); |
| 6547 |
vec_safe_push (refs, TREE_TYPE (probe)); |
| 6548 |
probe = TREE_OPERAND (probe, 0); |
| 6549 |
break; |
| 6550 |
|
| 6551 |
default: |
| 6552 |
if (evaluated) |
| 6553 |
object = probe; |
| 6554 |
else |
| 6555 |
{ |
| 6556 |
tree pvar = tree_strip_any_location_wrapper (probe); |
| 6557 |
if (VAR_P (pvar) && DECL_ANON_UNION_VAR_P (pvar)) |
| 6558 |
{ |
| 6559 |
/* Stores to DECL_ANON_UNION_VAR_P var are allowed to change |
| 6560 |
active union member. */ |
| 6561 |
probe = DECL_VALUE_EXPR (pvar); |
| 6562 |
break; |
| 6563 |
} |
| 6564 |
probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue, |
| 6565 |
non_constant_p, overflow_p); |
| 6566 |
evaluated = true; |
| 6567 |
if (*non_constant_p) |
| 6568 |
return t; |
| 6569 |
} |
| 6570 |
break; |
| 6571 |
} |
| 6572 |
} |
| 6573 |
|
| 6574 |
if (modifying_const_object_p (TREE_CODE (t), object, mutable_p) |
| 6575 |
&& const_object_being_modified == NULL_TREE) |
| 6576 |
const_object_being_modified = object; |
| 6577 |
|
| 6578 |
if (DECL_P (object) |
| 6579 |
&& TREE_CLOBBER_P (init) |
| 6580 |
&& DECL_NAME (object) == heap_deleted_identifier) |
| 6581 |
/* Ignore clobbers of deleted allocations for now; we'll get a better error |
| 6582 |
message later when operator delete is called. */ |
| 6583 |
return void_node; |
| 6584 |
|
| 6585 |
/* And then find/build up our initializer for the path to the subobject |
| 6586 |
we're initializing. */ |
| 6587 |
tree *valp; |
| 6588 |
if (DECL_P (object)) |
| 6589 |
valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR); |
| 6590 |
else |
| 6591 |
valp = NULL; |
| 6592 |
if (!valp) |
| 6593 |
{ |
| 6594 |
/* A constant-expression cannot modify objects from outside the |
| 6595 |
constant-expression. */ |
| 6596 |
if (!ctx->quiet) |
| 6597 |
{ |
| 6598 |
auto_diagnostic_group d; |
| 6599 |
if (DECL_P (object) && DECL_NAME (object) == heap_deleted_identifier) |
| 6600 |
{ |
| 6601 |
error ("modification of allocated storage after deallocation " |
| 6602 |
"is not a constant expression"); |
| 6603 |
inform (DECL_SOURCE_LOCATION (object), "allocated here"); |
| 6604 |
} |
| 6605 |
else if (DECL_P (object) && ctx->global->is_outside_lifetime (object)) |
| 6606 |
{ |
| 6607 |
if (TREE_CLOBBER_P (init)) |
| 6608 |
error ("destroying %qE outside its lifetime", object); |
| 6609 |
else |
| 6610 |
error ("modification of %qE outside its lifetime " |
| 6611 |
"is not a constant expression", object); |
| 6612 |
inform (DECL_SOURCE_LOCATION (object), "declared here"); |
| 6613 |
} |
| 6614 |
else |
| 6615 |
{ |
| 6616 |
if (TREE_CLOBBER_P (init)) |
| 6617 |
error ("destroying %qE from outside current evaluation " |
| 6618 |
"is not a constant expression", object); |
| 6619 |
else |
| 6620 |
error ("modification of %qE from outside current evaluation " |
| 6621 |
"is not a constant expression", object); |
| 6622 |
} |
| 6623 |
} |
| 6624 |
*non_constant_p = true; |
| 6625 |
return t; |
| 6626 |
} |
| 6627 |
|
| 6628 |
/* Handle explicit end-of-lifetime. */ |
| 6629 |
if (TREE_CLOBBER_P (init)) |
| 6630 |
{ |
| 6631 |
if (refs->is_empty ()) |
| 6632 |
ctx->global->destroy_value (object); |
| 6633 |
return void_node; |
| 6634 |
} |
| 6635 |
|
| 6636 |
type = TREE_TYPE (object); |
| 6637 |
bool no_zero_init = true; |
| 6638 |
bool zero_padding_bits = false; |
| 6639 |
|
| 6640 |
auto_vec<tree *> ctors; |
| 6641 |
releasing_vec indexes; |
| 6642 |
auto_vec<int> index_pos_hints; |
| 6643 |
bool activated_union_member_p = false; |
| 6644 |
bool empty_base = false; |
| 6645 |
while (!refs->is_empty ()) |
| 6646 |
{ |
| 6647 |
if (*valp == NULL_TREE) |
| 6648 |
{ |
| 6649 |
*valp = build_constructor (type, NULL); |
| 6650 |
CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; |
| 6651 |
CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits; |
| 6652 |
} |
| 6653 |
else if (STRIP_ANY_LOCATION_WRAPPER (*valp), |
| 6654 |
TREE_CODE (*valp) == STRING_CST) |
| 6655 |
{ |
| 6656 |
/* An array was initialized with a string constant, and now |
| 6657 |
we're writing into one of its elements. Explode the |
| 6658 |
single initialization into a set of element |
| 6659 |
initializations. */ |
| 6660 |
gcc_assert (TREE_CODE (type) == ARRAY_TYPE); |
| 6661 |
|
| 6662 |
tree string = *valp; |
| 6663 |
tree elt_type = TREE_TYPE (type); |
| 6664 |
unsigned chars_per_elt = (TYPE_PRECISION (elt_type) |
| 6665 |
/ TYPE_PRECISION (char_type_node)); |
| 6666 |
unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt; |
| 6667 |
tree ary_ctor = build_constructor (type, NULL); |
| 6668 |
|
| 6669 |
vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts); |
| 6670 |
for (unsigned ix = 0; ix != num_elts; ix++) |
| 6671 |
{ |
| 6672 |
constructor_elt elt = |
| 6673 |
{ |
| 6674 |
build_int_cst (size_type_node, ix), |
| 6675 |
extract_string_elt (string, chars_per_elt, ix) |
| 6676 |
}; |
| 6677 |
CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt); |
| 6678 |
} |
| 6679 |
|
| 6680 |
*valp = ary_ctor; |
| 6681 |
} |
| 6682 |
|
| 6683 |
enum tree_code code = TREE_CODE (type); |
| 6684 |
tree reftype = refs->pop(); |
| 6685 |
tree index = refs->pop(); |
| 6686 |
bool is_access_expr = refs->pop() != NULL_TREE; |
| 6687 |
|
| 6688 |
if (code == COMPLEX_TYPE) |
| 6689 |
{ |
| 6690 |
if (TREE_CODE (*valp) == COMPLEX_CST) |
| 6691 |
*valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp), |
| 6692 |
TREE_IMAGPART (*valp)); |
| 6693 |
else if (TREE_CODE (*valp) == CONSTRUCTOR |
| 6694 |
&& CONSTRUCTOR_NELTS (*valp) == 0 |
| 6695 |
&& CONSTRUCTOR_NO_CLEARING (*valp)) |
| 6696 |
{ |
| 6697 |
tree r = build_constructor (reftype, NULL); |
| 6698 |
CONSTRUCTOR_NO_CLEARING (r) = 1; |
| 6699 |
*valp = build2 (COMPLEX_EXPR, type, r, r); |
| 6700 |
} |
| 6701 |
gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR); |
| 6702 |
ctors.safe_push (valp); |
| 6703 |
vec_safe_push (indexes, index); |
| 6704 |
valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR); |
| 6705 |
gcc_checking_assert (refs->is_empty ()); |
| 6706 |
type = reftype; |
| 6707 |
break; |
| 6708 |
} |
| 6709 |
|
| 6710 |
/* If the value of object is already zero-initialized, any new ctors for |
| 6711 |
subobjects will also be zero-initialized. Similarly with zeroing of |
| 6712 |
padding bits. */ |
| 6713 |
no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp); |
| 6714 |
zero_padding_bits = CONSTRUCTOR_ZERO_PADDING_BITS (*valp); |
| 6715 |
|
| 6716 |
if (code == RECORD_TYPE && is_empty_field (index)) |
| 6717 |
/* Don't build a sub-CONSTRUCTOR for an empty base or field, as they |
| 6718 |
have no data and might have an offset lower than previously declared |
| 6719 |
fields, which confuses the middle-end. The code below will notice |
| 6720 |
that we don't have a CONSTRUCTOR for our inner target and just |
| 6721 |
return init. */ |
| 6722 |
{ |
| 6723 |
empty_base = true; |
| 6724 |
break; |
| 6725 |
} |
| 6726 |
|
| 6727 |
/* If a union is zero-initialized, its first non-static named data member |
| 6728 |
is zero-initialized (and therefore active). */ |
| 6729 |
if (code == UNION_TYPE |
| 6730 |
&& !no_zero_init |
| 6731 |
&& CONSTRUCTOR_NELTS (*valp) == 0) |
| 6732 |
if (tree first = next_aggregate_field (TYPE_FIELDS (type))) |
| 6733 |
CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (*valp), first, NULL_TREE); |
| 6734 |
|
| 6735 |
/* Check for implicit change of active member for a union. */ |
| 6736 |
if (code == UNION_TYPE |
| 6737 |
&& (CONSTRUCTOR_NELTS (*valp) == 0 |
| 6738 |
|| CONSTRUCTOR_ELT (*valp, 0)->index != index) |
| 6739 |
/* An INIT_EXPR of the last member in an access chain is always OK, |
| 6740 |
but still check implicit change of members earlier on; see |
| 6741 |
cpp2a/constexpr-union6.C. */ |
| 6742 |
&& !(TREE_CODE (t) == INIT_EXPR && refs->is_empty ())) |
| 6743 |
{ |
| 6744 |
bool has_active_member = CONSTRUCTOR_NELTS (*valp) != 0; |
| 6745 |
tree inner = strip_array_types (reftype); |
| 6746 |
|
| 6747 |
if (has_active_member && cxx_dialect < cxx20) |
| 6748 |
{ |
| 6749 |
if (!ctx->quiet) |
| 6750 |
error_at (cp_expr_loc_or_input_loc (t), |
| 6751 |
"change of the active member of a union " |
| 6752 |
"from %qD to %qD is not a constant expression " |
| 6753 |
"before C++20", |
| 6754 |
CONSTRUCTOR_ELT (*valp, 0)->index, |
| 6755 |
index); |
| 6756 |
*non_constant_p = true; |
| 6757 |
} |
| 6758 |
else if (!is_access_expr |
| 6759 |
|| (TREE_CODE (t) == MODIFY_EXPR |
| 6760 |
&& CLASS_TYPE_P (inner) |
| 6761 |
&& !type_has_non_deleted_trivial_default_ctor (inner))) |
| 6762 |
{ |
| 6763 |
/* Diagnose changing active union member after initialization |
| 6764 |
without a valid member access expression, as described in |
| 6765 |
[class.union.general] p5. */ |
| 6766 |
if (!ctx->quiet) |
| 6767 |
{ |
| 6768 |
auto_diagnostic_group d; |
| 6769 |
if (has_active_member) |
| 6770 |
error_at (cp_expr_loc_or_input_loc (t), |
| 6771 |
"accessing %qD member instead of initialized " |
| 6772 |
"%qD member in constant expression", |
| 6773 |
index, CONSTRUCTOR_ELT (*valp, 0)->index); |
| 6774 |
else |
| 6775 |
error_at (cp_expr_loc_or_input_loc (t), |
| 6776 |
"accessing uninitialized member %qD", |
| 6777 |
index); |
| 6778 |
if (is_access_expr) |
| 6779 |
inform (DECL_SOURCE_LOCATION (index), |
| 6780 |
"%qD does not implicitly begin its lifetime " |
| 6781 |
"because %qT does not have a non-deleted " |
| 6782 |
"trivial default constructor, use " |
| 6783 |
"%<std::construct_at%> instead", |
| 6784 |
index, inner); |
| 6785 |
else |
| 6786 |
inform (DECL_SOURCE_LOCATION (index), |
| 6787 |
"initializing %qD requires a member access " |
| 6788 |
"expression as the left operand of the assignment", |
| 6789 |
index); |
| 6790 |
} |
| 6791 |
*non_constant_p = true; |
| 6792 |
} |
| 6793 |
else if (has_active_member && CONSTRUCTOR_NO_CLEARING (*valp)) |
| 6794 |
{ |
| 6795 |
/* Diagnose changing the active union member while the union |
| 6796 |
is in the process of being initialized. */ |
| 6797 |
if (!ctx->quiet) |
| 6798 |
error_at (cp_expr_loc_or_input_loc (t), |
| 6799 |
"change of the active member of a union " |
| 6800 |
"from %qD to %qD during initialization", |
| 6801 |
CONSTRUCTOR_ELT (*valp, 0)->index, |
| 6802 |
index); |
| 6803 |
*non_constant_p = true; |
| 6804 |
} |
| 6805 |
no_zero_init = true; |
| 6806 |
} |
| 6807 |
|
| 6808 |
ctors.safe_push (valp); |
| 6809 |
vec_safe_push (indexes, index); |
| 6810 |
|
| 6811 |
constructor_elt *cep |
| 6812 |
= get_or_insert_ctor_field (*valp, index); |
| 6813 |
index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin()); |
| 6814 |
|
| 6815 |
if (code == UNION_TYPE) |
| 6816 |
activated_union_member_p = true; |
| 6817 |
|
| 6818 |
valp = &cep->value; |
| 6819 |
type = reftype; |
| 6820 |
} |
| 6821 |
|
| 6822 |
/* For initialization of an empty base, the original target will be |
| 6823 |
*(base*)this, evaluation of which resolves to the object |
| 6824 |
argument, which has the derived type rather than the base type. */ |
| 6825 |
if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p |
| 6826 |
(initialized_type (init), type))) |
| 6827 |
{ |
| 6828 |
gcc_assert (is_empty_class (TREE_TYPE (target))); |
| 6829 |
empty_base = true; |
| 6830 |
} |
| 6831 |
|
| 6832 |
/* Detect modifying a constant object in constexpr evaluation. |
| 6833 |
We have found a const object that is being modified. Figure out |
| 6834 |
if we need to issue an error. Consider |
| 6835 |
|
| 6836 |
struct A { |
| 6837 |
int n; |
| 6838 |
constexpr A() : n(1) { n = 2; } // #1 |
| 6839 |
}; |
| 6840 |
struct B { |
| 6841 |
const A a; |
| 6842 |
constexpr B() { a.n = 3; } // #2 |
| 6843 |
}; |
| 6844 |
constexpr B b{}; |
| 6845 |
|
| 6846 |
#1 is OK, since we're modifying an object under construction, but |
| 6847 |
#2 is wrong, since "a" is const and has been fully constructed. |
| 6848 |
To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR |
| 6849 |
which means that the object is read-only. For the example above, the |
| 6850 |
*ctors stack at the point of #2 will look like: |
| 6851 |
|
| 6852 |
ctors[0] = {.a={.n=2}} TREE_READONLY = 0 |
| 6853 |
ctors[1] = {.n=2} TREE_READONLY = 1 |
| 6854 |
|
| 6855 |
and we're modifying "b.a", so we search the stack and see if the |
| 6856 |
constructor for "b.a" has already run. */ |
| 6857 |
if (const_object_being_modified) |
| 6858 |
{ |
| 6859 |
bool fail = false; |
| 6860 |
tree const_objtype |
| 6861 |
= strip_array_types (TREE_TYPE (const_object_being_modified)); |
| 6862 |
if (!CLASS_TYPE_P (const_objtype)) |
| 6863 |
fail = true; |
| 6864 |
else |
| 6865 |
{ |
| 6866 |
/* [class.ctor]p5 "A constructor can be invoked for a const, |
| 6867 |
volatile, or const volatile object. const and volatile |
| 6868 |
semantics are not applied on an object under construction. |
| 6869 |
They come into effect when the constructor for the most |
| 6870 |
derived object ends." */ |
| 6871 |
for (tree *elt : ctors) |
| 6872 |
if (same_type_ignoring_top_level_qualifiers_p |
| 6873 |
(TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt))) |
| 6874 |
{ |
| 6875 |
fail = TREE_READONLY (*elt); |
| 6876 |
break; |
| 6877 |
} |
| 6878 |
} |
| 6879 |
if (fail) |
| 6880 |
{ |
| 6881 |
if (!ctx->quiet) |
| 6882 |
modifying_const_object_error (t, const_object_being_modified); |
| 6883 |
*non_constant_p = true; |
| 6884 |
return t; |
| 6885 |
} |
| 6886 |
} |
| 6887 |
|
| 6888 |
if (!preeval) |
| 6889 |
{ |
| 6890 |
/* We're handling an INIT_EXPR of class type, so the value of the |
| 6891 |
initializer can depend on the object it's initializing. */ |
| 6892 |
|
| 6893 |
/* Create a new CONSTRUCTOR in case evaluation of the initializer |
| 6894 |
wants to modify it. */ |
| 6895 |
if (*valp == NULL_TREE) |
| 6896 |
{ |
| 6897 |
*valp = build_constructor (type, NULL); |
| 6898 |
CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; |
| 6899 |
CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits; |
| 6900 |
} |
| 6901 |
new_ctx.ctor = empty_base ? NULL_TREE : *valp; |
| 6902 |
new_ctx.object = target; |
| 6903 |
/* Avoid temporary materialization when initializing from a TARGET_EXPR. |
| 6904 |
We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because |
| 6905 |
expansion of those trees uses ctx instead. */ |
| 6906 |
if (TREE_CODE (init) == TARGET_EXPR) |
| 6907 |
if (tree tinit = TARGET_EXPR_INITIAL (init)) |
| 6908 |
init = tinit; |
| 6909 |
init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue, |
| 6910 |
non_constant_p, overflow_p); |
| 6911 |
/* The hash table might have moved since the get earlier, and the |
| 6912 |
initializer might have mutated the underlying CONSTRUCTORs, so we must |
| 6913 |
recompute VALP. */ |
| 6914 |
valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR); |
| 6915 |
for (unsigned i = 0; i < vec_safe_length (indexes); i++) |
| 6916 |
{ |
| 6917 |
ctors[i] = valp; |
| 6918 |
constructor_elt *cep |
| 6919 |
= get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]); |
| 6920 |
valp = &cep->value; |
| 6921 |
} |
| 6922 |
} |
| 6923 |
|
| 6924 |
if (*non_constant_p) |
| 6925 |
return t; |
| 6926 |
|
| 6927 |
/* Don't share a CONSTRUCTOR that might be changed later. */ |
| 6928 |
init = unshare_constructor (init); |
| 6929 |
|
| 6930 |
gcc_checking_assert (!*valp || (same_type_ignoring_top_level_qualifiers_p |
| 6931 |
(TREE_TYPE (*valp), type))); |
| 6932 |
if (empty_base) |
| 6933 |
{ |
| 6934 |
/* Just evaluate the initializer and return, since there's no actual data |
| 6935 |
to store, and we didn't build a CONSTRUCTOR. */ |
| 6936 |
if (!*valp) |
| 6937 |
{ |
| 6938 |
/* But do make sure we have something in *valp. */ |
| 6939 |
*valp = build_constructor (type, nullptr); |
| 6940 |
CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init; |
| 6941 |
CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits; |
| 6942 |
} |
| 6943 |
} |
| 6944 |
else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR |
| 6945 |
&& TREE_CODE (init) == CONSTRUCTOR) |
| 6946 |
{ |
| 6947 |
/* An outer ctx->ctor might be pointing to *valp, so replace |
| 6948 |
its contents. */ |
| 6949 |
CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init); |
| 6950 |
TREE_CONSTANT (*valp) = TREE_CONSTANT (init); |
| 6951 |
TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init); |
| 6952 |
CONSTRUCTOR_NO_CLEARING (*valp) |
| 6953 |
= CONSTRUCTOR_NO_CLEARING (init); |
| 6954 |
CONSTRUCTOR_ZERO_PADDING_BITS (*valp) |
| 6955 |
= CONSTRUCTOR_ZERO_PADDING_BITS (init); |
| 6956 |
} |
| 6957 |
else |
| 6958 |
*valp = init; |
| 6959 |
|
| 6960 |
/* After initialization, 'const' semantics apply to the value of the |
| 6961 |
object. Make a note of this fact by marking the CONSTRUCTOR |
| 6962 |
TREE_READONLY. */ |
| 6963 |
if (TREE_CODE (t) == INIT_EXPR |
| 6964 |
&& !empty_base |
| 6965 |
&& TREE_CODE (*valp) == CONSTRUCTOR |
| 6966 |
&& TYPE_READONLY (type)) |
| 6967 |
{ |
| 6968 |
if (INDIRECT_REF_P (target) |
| 6969 |
&& (is_this_parameter |
| 6970 |
(tree_strip_nop_conversions (TREE_OPERAND (target, 0))))) |
| 6971 |
/* We've just initialized '*this' (perhaps via the target |
| 6972 |
constructor of a delegating constructor). Leave it up to the |
| 6973 |
caller that set 'this' to set TREE_READONLY appropriately. */ |
| 6974 |
gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p |
| 6975 |
(TREE_TYPE (target), type) || empty_base); |
| 6976 |
else |
| 6977 |
TREE_READONLY (*valp) = true; |
| 6978 |
} |
| 6979 |
|
| 6980 |
/* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing |
| 6981 |
CONSTRUCTORs, if any. */ |
| 6982 |
bool c = TREE_CONSTANT (init); |
| 6983 |
bool s = TREE_SIDE_EFFECTS (init); |
| 6984 |
if (!indexes->is_empty ()) |
| 6985 |
{ |
| 6986 |
tree last = indexes->last (); |
| 6987 |
if (TREE_CODE (last) == REALPART_EXPR |
| 6988 |
|| TREE_CODE (last) == IMAGPART_EXPR) |
| 6989 |
{ |
| 6990 |
/* And canonicalize COMPLEX_EXPR into COMPLEX_CST if |
| 6991 |
possible. */ |
| 6992 |
tree *cexpr = ctors.last (); |
| 6993 |
if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr), |
| 6994 |
TREE_OPERAND (*cexpr, 0), |
| 6995 |
TREE_OPERAND (*cexpr, 1))) |
| 6996 |
*cexpr = c; |
| 6997 |
else |
| 6998 |
{ |
| 6999 |
TREE_CONSTANT (*cexpr) |
| 7000 |
= (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0)) |
| 7001 |
& TREE_CONSTANT (TREE_OPERAND (*cexpr, 1))); |
| 7002 |
TREE_SIDE_EFFECTS (*cexpr) |
| 7003 |
= (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0)) |
| 7004 |
| TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1))); |
| 7005 |
} |
| 7006 |
c = TREE_CONSTANT (*cexpr); |
| 7007 |
s = TREE_SIDE_EFFECTS (*cexpr); |
| 7008 |
} |
| 7009 |
} |
| 7010 |
if (!c || s || activated_union_member_p) |
| 7011 |
for (tree *elt : ctors) |
| 7012 |
{ |
| 7013 |
if (TREE_CODE (*elt) != CONSTRUCTOR) |
| 7014 |
continue; |
| 7015 |
if (!c) |
| 7016 |
TREE_CONSTANT (*elt) = false; |
| 7017 |
if (s) |
| 7018 |
TREE_SIDE_EFFECTS (*elt) = true; |
| 7019 |
/* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of |
| 7020 |
this union. */ |
| 7021 |
if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE) |
| 7022 |
CONSTRUCTOR_NO_CLEARING (*elt) = false; |
| 7023 |
} |
| 7024 |
|
| 7025 |
if (lval) |
| 7026 |
return target; |
| 7027 |
else |
| 7028 |
return init; |
| 7029 |
} |
| 7030 |
|
| 7031 |
/* Evaluate a ++ or -- expression. */ |
| 7032 |
|
| 7033 |
static tree |
| 7034 |
cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t, |
| 7035 |
value_cat lval, |
| 7036 |
bool *non_constant_p, bool *overflow_p) |
| 7037 |
{ |
| 7038 |
enum tree_code code = TREE_CODE (t); |
| 7039 |
tree type = TREE_TYPE (t); |
| 7040 |
tree op = TREE_OPERAND (t, 0); |
| 7041 |
tree offset = TREE_OPERAND (t, 1); |
| 7042 |
gcc_assert (TREE_CONSTANT (offset)); |
| 7043 |
|
| 7044 |
/* OFFSET is constant, but perhaps not constant enough. We need to |
| 7045 |
e.g. bash FLOAT_EXPRs to REAL_CSTs. */ |
| 7046 |
offset = fold_simple (offset); |
| 7047 |
|
| 7048 |
/* The operand as an lvalue. */ |
| 7049 |
op = cxx_eval_constant_expression (ctx, op, vc_glvalue, |
| 7050 |
non_constant_p, overflow_p); |
| 7051 |
|
| 7052 |
/* The operand as an rvalue. */ |
| 7053 |
tree val |
| 7054 |
= cxx_eval_constant_expression (ctx, op, vc_prvalue, |
| 7055 |
non_constant_p, overflow_p); |
| 7056 |
/* Don't VERIFY_CONSTANT if this might be dealing with a pointer to |
| 7057 |
a local array in a constexpr function. */ |
| 7058 |
bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val)); |
| 7059 |
if (!ptr) |
| 7060 |
VERIFY_CONSTANT (val); |
| 7061 |
|
| 7062 |
/* The modified value. */ |
| 7063 |
bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR); |
| 7064 |
tree mod; |
| 7065 |
if (INDIRECT_TYPE_P (type)) |
| 7066 |
{ |
| 7067 |
/* The middle end requires pointers to use POINTER_PLUS_EXPR. */ |
| 7068 |
offset = convert_to_ptrofftype (offset); |
| 7069 |
if (!inc) |
| 7070 |
offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset); |
| 7071 |
mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset); |
| 7072 |
} |
| 7073 |
else if (c_promoting_integer_type_p (type) |
| 7074 |
&& !TYPE_UNSIGNED (type) |
| 7075 |
&& TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) |
| 7076 |
{ |
| 7077 |
offset = fold_convert (integer_type_node, offset); |
| 7078 |
mod = fold_convert (integer_type_node, val); |
| 7079 |
tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node, |
| 7080 |
mod, offset); |
| 7081 |
mod = fold_convert (type, t); |
| 7082 |
if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t)) |
| 7083 |
TREE_OVERFLOW (mod) = false; |
| 7084 |
} |
| 7085 |
else |
| 7086 |
mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset); |
| 7087 |
if (!ptr) |
| 7088 |
VERIFY_CONSTANT (mod); |
| 7089 |
|
| 7090 |
/* Storing the modified value. */ |
| 7091 |
tree store = build2_loc (cp_expr_loc_or_loc (t, input_location), |
| 7092 |
MODIFY_EXPR, type, op, mod); |
| 7093 |
mod = cxx_eval_constant_expression (ctx, store, lval, |
| 7094 |
non_constant_p, overflow_p); |
| 7095 |
ggc_free (store); |
| 7096 |
if (*non_constant_p) |
| 7097 |
return t; |
| 7098 |
|
| 7099 |
/* And the value of the expression. */ |
| 7100 |
if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR) |
| 7101 |
/* Prefix ops are lvalues, but the caller might want an rvalue; |
| 7102 |
lval has already been taken into account in the store above. */ |
| 7103 |
return mod; |
| 7104 |
else |
| 7105 |
/* Postfix ops are rvalues. */ |
| 7106 |
return val; |
| 7107 |
} |
| 7108 |
|
| 7109 |
/* Predicates for the meaning of *jump_target. */ |
| 7110 |
|
| 7111 |
static bool |
| 7112 |
returns (tree *jump_target) |
| 7113 |
{ |
| 7114 |
return *jump_target |
| 7115 |
&& TREE_CODE (*jump_target) == RETURN_EXPR; |
| 7116 |
} |
| 7117 |
|
| 7118 |
static bool |
| 7119 |
breaks (tree *jump_target) |
| 7120 |
{ |
| 7121 |
return *jump_target |
| 7122 |
&& ((TREE_CODE (*jump_target) == LABEL_DECL |
| 7123 |
&& LABEL_DECL_BREAK (*jump_target)) |
| 7124 |
|| TREE_CODE (*jump_target) == BREAK_STMT |
| 7125 |
|| TREE_CODE (*jump_target) == EXIT_EXPR); |
| 7126 |
} |
| 7127 |
|
| 7128 |
static bool |
| 7129 |
continues (tree *jump_target) |
| 7130 |
{ |
| 7131 |
return *jump_target |
| 7132 |
&& ((TREE_CODE (*jump_target) == LABEL_DECL |
| 7133 |
&& LABEL_DECL_CONTINUE (*jump_target)) |
| 7134 |
|| TREE_CODE (*jump_target) == CONTINUE_STMT); |
| 7135 |
|
| 7136 |
} |
| 7137 |
|
| 7138 |
static bool |
| 7139 |
switches (tree *jump_target) |
| 7140 |
{ |
| 7141 |
return *jump_target |
| 7142 |
&& TREE_CODE (*jump_target) == INTEGER_CST; |
| 7143 |
} |
| 7144 |
|
| 7145 |
/* Subroutine of cxx_eval_statement_list. Determine whether the statement |
| 7146 |
STMT matches *jump_target. If we're looking for a case label and we see |
| 7147 |
the default label, note it in ctx->css_state. */ |
| 7148 |
|
| 7149 |
static bool |
| 7150 |
label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt) |
| 7151 |
{ |
| 7152 |
switch (TREE_CODE (*jump_target)) |
| 7153 |
{ |
| 7154 |
case LABEL_DECL: |
| 7155 |
if (TREE_CODE (stmt) == LABEL_EXPR |
| 7156 |
&& LABEL_EXPR_LABEL (stmt) == *jump_target) |
| 7157 |
return true; |
| 7158 |
break; |
| 7159 |
|
| 7160 |
case INTEGER_CST: |
| 7161 |
if (TREE_CODE (stmt) == CASE_LABEL_EXPR) |
| 7162 |
{ |
| 7163 |
gcc_assert (ctx->css_state != NULL); |
| 7164 |
if (!CASE_LOW (stmt)) |
| 7165 |
{ |
| 7166 |
/* default: should appear just once in a SWITCH_EXPR |
| 7167 |
body (excluding nested SWITCH_EXPR). */ |
| 7168 |
gcc_assert (*ctx->css_state != css_default_seen); |
| 7169 |
/* When evaluating SWITCH_EXPR body for the second time, |
| 7170 |
return true for the default: label. */ |
| 7171 |
if (*ctx->css_state == css_default_processing) |
| 7172 |
return true; |
| 7173 |
*ctx->css_state = css_default_seen; |
| 7174 |
} |
| 7175 |
else if (CASE_HIGH (stmt)) |
| 7176 |
{ |
| 7177 |
if (tree_int_cst_le (CASE_LOW (stmt), *jump_target) |
| 7178 |
&& tree_int_cst_le (*jump_target, CASE_HIGH (stmt))) |
| 7179 |
return true; |
| 7180 |
} |
| 7181 |
else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt))) |
| 7182 |
return true; |
| 7183 |
} |
| 7184 |
break; |
| 7185 |
|
| 7186 |
case BREAK_STMT: |
| 7187 |
case CONTINUE_STMT: |
| 7188 |
/* These two are handled directly in cxx_eval_loop_expr by testing |
| 7189 |
breaks (jump_target) or continues (jump_target). */ |
| 7190 |
break; |
| 7191 |
|
| 7192 |
default: |
| 7193 |
gcc_unreachable (); |
| 7194 |
} |
| 7195 |
return false; |
| 7196 |
} |
| 7197 |
|
| 7198 |
/* Evaluate a STATEMENT_LIST for side-effects. Handles various jump |
| 7199 |
semantics, for switch, break, continue, and return. */ |
| 7200 |
|
| 7201 |
static tree |
| 7202 |
cxx_eval_statement_list (const constexpr_ctx *ctx, tree t, |
| 7203 |
bool *non_constant_p, bool *overflow_p, |
| 7204 |
tree *jump_target) |
| 7205 |
{ |
| 7206 |
tree local_target; |
| 7207 |
/* In a statement-expression we want to return the last value. |
| 7208 |
For empty statement expression return void_node. */ |
| 7209 |
tree r = void_node; |
| 7210 |
if (!jump_target) |
| 7211 |
{ |
| 7212 |
local_target = NULL_TREE; |
| 7213 |
jump_target = &local_target; |
| 7214 |
} |
| 7215 |
for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i) |
| 7216 |
{ |
| 7217 |
tree stmt = *i; |
| 7218 |
|
| 7219 |
/* We've found a continue, so skip everything until we reach |
| 7220 |
the label its jumping to. */ |
| 7221 |
if (continues (jump_target)) |
| 7222 |
{ |
| 7223 |
if (label_matches (ctx, jump_target, stmt)) |
| 7224 |
/* Found it. */ |
| 7225 |
*jump_target = NULL_TREE; |
| 7226 |
else |
| 7227 |
continue; |
| 7228 |
} |
| 7229 |
if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT) |
| 7230 |
continue; |
| 7231 |
|
| 7232 |
value_cat lval = vc_discard; |
| 7233 |
/* The result of a statement-expression is not wrapped in EXPR_STMT. */ |
| 7234 |
if (tsi_one_before_end_p (i) && TREE_CODE (stmt) != EXPR_STMT) |
| 7235 |
lval = vc_prvalue; |
| 7236 |
|
| 7237 |
r = cxx_eval_constant_expression (ctx, stmt, lval, |
| 7238 |
non_constant_p, overflow_p, |
| 7239 |
jump_target); |
| 7240 |
if (*non_constant_p) |
| 7241 |
break; |
| 7242 |
if (returns (jump_target) || breaks (jump_target)) |
| 7243 |
break; |
| 7244 |
} |
| 7245 |
if (*jump_target && jump_target == &local_target) |
| 7246 |
{ |
| 7247 |
/* We aren't communicating the jump to our caller, so give up. We don't |
| 7248 |
need to support evaluation of jumps out of statement-exprs. */ |
| 7249 |
if (!ctx->quiet) |
| 7250 |
error_at (cp_expr_loc_or_input_loc (r), |
| 7251 |
"statement is not a constant expression"); |
| 7252 |
*non_constant_p = true; |
| 7253 |
} |
| 7254 |
return r; |
| 7255 |
} |
| 7256 |
|
| 7257 |
/* Evaluate a LOOP_EXPR for side-effects. Handles break and return |
| 7258 |
semantics; continue semantics are covered by cxx_eval_statement_list. */ |
| 7259 |
|
| 7260 |
static tree |
| 7261 |
cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t, |
| 7262 |
bool *non_constant_p, bool *overflow_p, |
| 7263 |
tree *jump_target) |
| 7264 |
{ |
| 7265 |
tree local_target; |
| 7266 |
if (!jump_target) |
| 7267 |
{ |
| 7268 |
local_target = NULL_TREE; |
| 7269 |
jump_target = &local_target; |
| 7270 |
} |
| 7271 |
|
| 7272 |
tree body, cond = NULL_TREE, expr = NULL_TREE; |
| 7273 |
tree cond_prep = NULL_TREE, cond_cleanup = NULL_TREE; |
| 7274 |
unsigned cond_cleanup_depth = 0; |
| 7275 |
int count = 0; |
| 7276 |
switch (TREE_CODE (t)) |
| 7277 |
{ |
| 7278 |
case LOOP_EXPR: |
| 7279 |
body = LOOP_EXPR_BODY (t); |
| 7280 |
break; |
| 7281 |
case DO_STMT: |
| 7282 |
body = DO_BODY (t); |
| 7283 |
cond = DO_COND (t); |
| 7284 |
break; |
| 7285 |
case WHILE_STMT: |
| 7286 |
body = WHILE_BODY (t); |
| 7287 |
cond = WHILE_COND (t); |
| 7288 |
cond_prep = WHILE_COND_PREP (t); |
| 7289 |
cond_cleanup = WHILE_COND_CLEANUP (t); |
| 7290 |
count = -1; |
| 7291 |
break; |
| 7292 |
case FOR_STMT: |
| 7293 |
if (FOR_INIT_STMT (t)) |
| 7294 |
cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard, |
| 7295 |
non_constant_p, overflow_p, jump_target); |
| 7296 |
if (*non_constant_p) |
| 7297 |
return NULL_TREE; |
| 7298 |
body = FOR_BODY (t); |
| 7299 |
cond = FOR_COND (t); |
| 7300 |
expr = FOR_EXPR (t); |
| 7301 |
cond_prep = FOR_COND_PREP (t); |
| 7302 |
cond_cleanup = FOR_COND_CLEANUP (t); |
| 7303 |
count = -1; |
| 7304 |
break; |
| 7305 |
default: |
| 7306 |
gcc_unreachable (); |
| 7307 |
} |
| 7308 |
if (cond_prep) |
| 7309 |
gcc_assert (TREE_CODE (cond_prep) == BIND_EXPR); |
| 7310 |
auto cleanup_cond = [&] { |
| 7311 |
/* Clean up the condition variable after each iteration. */ |
| 7312 |
if (cond_cleanup_depth && !*non_constant_p) |
| 7313 |
{ |
| 7314 |
auto_vec<tree, 4> cleanups (cond_cleanup_depth); |
| 7315 |
tree s = BIND_EXPR_BODY (cond_prep); |
| 7316 |
unsigned i; |
| 7317 |
for (i = cond_cleanup_depth; i; --i) |
| 7318 |
{ |
| 7319 |
tree_stmt_iterator iter = tsi_last (s); |
| 7320 |
s = tsi_stmt (iter); |
| 7321 |
cleanups.quick_push (CLEANUP_EXPR (s)); |
| 7322 |
s = CLEANUP_BODY (s); |
| 7323 |
} |
| 7324 |
tree c; |
| 7325 |
FOR_EACH_VEC_ELT_REVERSE (cleanups, i, c) |
| 7326 |
cxx_eval_constant_expression (ctx, c, vc_discard, non_constant_p, |
| 7327 |
overflow_p); |
| 7328 |
} |
| 7329 |
if (cond_prep) |
| 7330 |
for (tree decl = BIND_EXPR_VARS (cond_prep); |
| 7331 |
decl; decl = DECL_CHAIN (decl)) |
| 7332 |
destroy_value_checked (ctx, decl, non_constant_p); |
| 7333 |
}; |
| 7334 |
do |
| 7335 |
{ |
| 7336 |
if (count != -1) |
| 7337 |
{ |
| 7338 |
if (body) |
| 7339 |
cxx_eval_constant_expression (ctx, body, vc_discard, |
| 7340 |
non_constant_p, overflow_p, |
| 7341 |
jump_target); |
| 7342 |
if (breaks (jump_target)) |
| 7343 |
{ |
| 7344 |
*jump_target = NULL_TREE; |
| 7345 |
break; |
| 7346 |
} |
| 7347 |
|
| 7348 |
if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target)) |
| 7349 |
*jump_target = NULL_TREE; |
| 7350 |
|
| 7351 |
if (expr) |
| 7352 |
cxx_eval_constant_expression (ctx, expr, vc_prvalue, |
| 7353 |
non_constant_p, overflow_p, |
| 7354 |
jump_target); |
| 7355 |
cleanup_cond (); |
| 7356 |
} |
| 7357 |
|
| 7358 |
if (cond_prep) |
| 7359 |
{ |
| 7360 |
for (tree decl = BIND_EXPR_VARS (cond_prep); |
| 7361 |
decl; decl = DECL_CHAIN (decl)) |
| 7362 |
ctx->global->clear_value (decl); |
| 7363 |
if (cond_cleanup) |
| 7364 |
{ |
| 7365 |
/* If COND_CLEANUP is non-NULL, we need to evaluate DEPTH |
| 7366 |
nested STATEMENT_LISTs from inside of BIND_EXPR_BODY, |
| 7367 |
but defer the evaluation of CLEANUP_EXPRs of CLEANUP_STMT |
| 7368 |
at the end of those STATEMENT_LISTs. */ |
| 7369 |
cond_cleanup_depth = 0; |
| 7370 |
tree s = BIND_EXPR_BODY (cond_prep); |
| 7371 |
for (unsigned depth = tree_to_uhwi (cond_cleanup); |
| 7372 |
depth; --depth) |
| 7373 |
{ |
| 7374 |
for (tree_stmt_iterator i = tsi_start (s); |
| 7375 |
!tsi_end_p (i); ++i) |
| 7376 |
{ |
| 7377 |
tree stmt = *i; |
| 7378 |
if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT) |
| 7379 |
continue; |
| 7380 |
if (tsi_one_before_end_p (i)) |
| 7381 |
{ |
| 7382 |
/* The last statement in the STATEMENT_LIST |
| 7383 |
has to be a CLEANUP_STMT (verified in |
| 7384 |
finish_loop_cond_prep). We want to |
| 7385 |
evaluate just its CLEANUP_BODY part but not |
| 7386 |
CLEANUP_EXPR part just yet. */ |
| 7387 |
gcc_assert (TREE_CODE (stmt) == CLEANUP_STMT); |
| 7388 |
/* If the CLEANUP_STMT is not actually to be |
| 7389 |
evaluated, don't increment cond_cleanup_depth |
| 7390 |
so that we don't evaluate the CLEANUP_EXPR |
| 7391 |
for it later either. */ |
| 7392 |
if (*jump_target) |
| 7393 |
{ |
| 7394 |
depth = 1; |
| 7395 |
break; |
| 7396 |
} |
| 7397 |
++cond_cleanup_depth; |
| 7398 |
/* If not in the innermost one, next iteration |
| 7399 |
will handle CLEANUP_BODY similarly. */ |
| 7400 |
if (depth > 1) |
| 7401 |
{ |
| 7402 |
s = CLEANUP_BODY (stmt); |
| 7403 |
break; |
| 7404 |
} |
| 7405 |
/* The innermost one can be evaluated normally. */ |
| 7406 |
cxx_eval_constant_expression (ctx, |
| 7407 |
CLEANUP_BODY (stmt), |
| 7408 |
vc_discard, |
| 7409 |
non_constant_p, |
| 7410 |
overflow_p, |
| 7411 |
jump_target); |
| 7412 |
break; |
| 7413 |
} |
| 7414 |
/* And so should be evaluated statements which aren't |
| 7415 |
last in the STATEMENT_LIST. */ |
| 7416 |
cxx_eval_constant_expression (ctx, stmt, vc_discard, |
| 7417 |
non_constant_p, overflow_p, |
| 7418 |
jump_target); |
| 7419 |
if (*non_constant_p |
| 7420 |
|| returns (jump_target) |
| 7421 |
|| breaks (jump_target) |
| 7422 |
|| continues (jump_target)) |
| 7423 |
{ |
| 7424 |
depth = 1; |
| 7425 |
break; |
| 7426 |
} |
| 7427 |
} |
| 7428 |
} |
| 7429 |
} |
| 7430 |
else |
| 7431 |
cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (cond_prep), |
| 7432 |
vc_discard, non_constant_p, |
| 7433 |
overflow_p, jump_target); |
| 7434 |
} |
| 7435 |
|
| 7436 |
if (cond) |
| 7437 |
{ |
| 7438 |
tree res |
| 7439 |
= cxx_eval_constant_expression (ctx, cond, vc_prvalue, |
| 7440 |
non_constant_p, overflow_p, |
| 7441 |
jump_target); |
| 7442 |
if (res) |
| 7443 |
{ |
| 7444 |
if (verify_constant (res, ctx->quiet, non_constant_p, |
| 7445 |
overflow_p)) |
| 7446 |
break; |
| 7447 |
if (integer_zerop (res)) |
| 7448 |
break; |
| 7449 |
} |
| 7450 |
else |
| 7451 |
gcc_assert (*jump_target); |
| 7452 |
} |
| 7453 |
|
| 7454 |
if (++count >= constexpr_loop_limit) |
| 7455 |
{ |
| 7456 |
if (!ctx->quiet) |
| 7457 |
error_at (cp_expr_loc_or_input_loc (t), |
| 7458 |
"%<constexpr%> loop iteration count exceeds limit of %d " |
| 7459 |
"(use %<-fconstexpr-loop-limit=%> to increase the limit)", |
| 7460 |
constexpr_loop_limit); |
| 7461 |
*non_constant_p = true; |
| 7462 |
break; |
| 7463 |
} |
| 7464 |
} |
| 7465 |
while (!returns (jump_target) |
| 7466 |
&& !breaks (jump_target) |
| 7467 |
&& !continues (jump_target) |
| 7468 |
&& (!switches (jump_target) || count == 0) |
| 7469 |
&& !*non_constant_p); |
| 7470 |
|
| 7471 |
cleanup_cond (); |
| 7472 |
|
| 7473 |
return NULL_TREE; |
| 7474 |
} |
| 7475 |
|
| 7476 |
/* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump |
| 7477 |
semantics. */ |
| 7478 |
|
| 7479 |
static tree |
| 7480 |
cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t, |
| 7481 |
bool *non_constant_p, bool *overflow_p, |
| 7482 |
tree *jump_target) |
| 7483 |
{ |
| 7484 |
tree cond |
| 7485 |
= TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t); |
| 7486 |
cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue, |
| 7487 |
non_constant_p, overflow_p); |
| 7488 |
VERIFY_CONSTANT (cond); |
| 7489 |
if (TREE_CODE (cond) != INTEGER_CST) |
| 7490 |
{ |
| 7491 |
/* If the condition doesn't reduce to an INTEGER_CST it isn't a usable |
| 7492 |
switch condition even if it's constant enough for other things |
| 7493 |
(c++/113545). */ |
| 7494 |
gcc_checking_assert (ctx->quiet); |
| 7495 |
*non_constant_p = true; |
| 7496 |
return t; |
| 7497 |
} |
| 7498 |
|
| 7499 |
*jump_target = cond; |
| 7500 |
|
| 7501 |
tree body |
| 7502 |
= TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t); |
| 7503 |
constexpr_ctx new_ctx = *ctx; |
| 7504 |
constexpr_switch_state css = css_default_not_seen; |
| 7505 |
new_ctx.css_state = &css; |
| 7506 |
cxx_eval_constant_expression (&new_ctx, body, vc_discard, |
| 7507 |
non_constant_p, overflow_p, jump_target); |
| 7508 |
if (switches (jump_target) && css == css_default_seen) |
| 7509 |
{ |
| 7510 |
/* If the SWITCH_EXPR body has default: label, process it once again, |
| 7511 |
this time instructing label_matches to return true for default: |
| 7512 |
label on switches (jump_target). */ |
| 7513 |
css = css_default_processing; |
| 7514 |
cxx_eval_constant_expression (&new_ctx, body, vc_discard, |
| 7515 |
non_constant_p, overflow_p, jump_target); |
| 7516 |
} |
| 7517 |
if (breaks (jump_target) || switches (jump_target)) |
| 7518 |
*jump_target = NULL_TREE; |
| 7519 |
return NULL_TREE; |
| 7520 |
} |
| 7521 |
|
| 7522 |
/* Find the object of TYPE under initialization in CTX. */ |
| 7523 |
|
| 7524 |
static tree |
| 7525 |
lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type) |
| 7526 |
{ |
| 7527 |
if (!ctx) |
| 7528 |
return NULL_TREE; |
| 7529 |
|
| 7530 |
/* Prefer the outermost matching object, but don't cross |
| 7531 |
CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */ |
| 7532 |
if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor)) |
| 7533 |
if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type)) |
| 7534 |
return outer_ob; |
| 7535 |
|
| 7536 |
/* We could use ctx->object unconditionally, but using ctx->ctor when we |
| 7537 |
can is a minor optimization. */ |
| 7538 |
if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type)) |
| 7539 |
return ctx->ctor; |
| 7540 |
|
| 7541 |
if (!ctx->object) |
| 7542 |
return NULL_TREE; |
| 7543 |
|
| 7544 |
/* Since an object cannot have a field of its own type, we can search outward |
| 7545 |
from ctx->object to find the unique containing object of TYPE. */ |
| 7546 |
tree ob = ctx->object; |
| 7547 |
while (ob) |
| 7548 |
{ |
| 7549 |
if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type)) |
| 7550 |
break; |
| 7551 |
if (handled_component_p (ob)) |
| 7552 |
ob = TREE_OPERAND (ob, 0); |
| 7553 |
else |
| 7554 |
ob = NULL_TREE; |
| 7555 |
} |
| 7556 |
|
| 7557 |
return ob; |
| 7558 |
} |
| 7559 |
|
| 7560 |
/* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is |
| 7561 |
true, we're checking a constexpr function body. */ |
| 7562 |
|
| 7563 |
static void |
| 7564 |
inline_asm_in_constexpr_error (location_t loc, bool fundef_p) |
| 7565 |
{ |
| 7566 |
auto_diagnostic_group d; |
| 7567 |
if (constexpr_error (loc, fundef_p, "inline assembly is not a " |
| 7568 |
"constant expression")) |
| 7569 |
inform (loc, "only unevaluated inline assembly is allowed in a " |
| 7570 |
"%<constexpr%> function in C++20"); |
| 7571 |
} |
| 7572 |
|
| 7573 |
/* We're getting the constant value of DECL in a manifestly constant-evaluated |
| 7574 |
context; maybe complain about that. */ |
| 7575 |
|
| 7576 |
static void |
| 7577 |
maybe_warn_about_constant_value (location_t loc, tree decl) |
| 7578 |
{ |
| 7579 |
static bool explained = false; |
| 7580 |
if (cxx_dialect >= cxx17 |
| 7581 |
&& warn_interference_size |
| 7582 |
&& !OPTION_SET_P (param_destruct_interfere_size) |
| 7583 |
&& DECL_CONTEXT (decl) == std_node |
| 7584 |
&& DECL_NAME (decl) |
| 7585 |
&& id_equal (DECL_NAME (decl), "hardware_destructive_interference_size") |
| 7586 |
&& (LOCATION_FILE (input_location) != main_input_filename |
| 7587 |
|| module_exporting_p ()) |
| 7588 |
&& warning_at (loc, OPT_Winterference_size, "use of %qD", decl) |
| 7589 |
&& !explained) |
| 7590 |
{ |
| 7591 |
explained = true; |
| 7592 |
inform (loc, "its value can vary between compiler versions or " |
| 7593 |
"with different %<-mtune%> or %<-mcpu%> flags"); |
| 7594 |
inform (loc, "if this use is part of a public ABI, change it to " |
| 7595 |
"instead use a constant variable you define"); |
| 7596 |
inform (loc, "the default value for the current CPU tuning " |
| 7597 |
"is %d bytes", param_destruct_interfere_size); |
| 7598 |
inform (loc, "you can stabilize this value with %<--param " |
| 7599 |
"hardware_destructive_interference_size=%d%>, or disable " |
| 7600 |
"this warning with %<-Wno-interference-size%>", |
| 7601 |
param_destruct_interfere_size); |
| 7602 |
} |
| 7603 |
} |
| 7604 |
|
| 7605 |
/* For element type ELT_TYPE, return the appropriate type of the heap object |
| 7606 |
containing such element(s). COOKIE_SIZE is NULL or the size of cookie |
| 7607 |
in bytes. If COOKIE_SIZE is NULL, return array type |
| 7608 |
ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return |
| 7609 |
struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; } |
| 7610 |
where N is computed such that the size of the struct fits into FULL_SIZE. |
| 7611 |
If ARG_SIZE is non-NULL, it is the first argument to the new operator. |
| 7612 |
It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE |
| 7613 |
will be also 0 and so it is not possible to determine the actual array |
| 7614 |
size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant |
| 7615 |
expression evaluation of subexpressions of ARG_SIZE. */ |
| 7616 |
|
| 7617 |
static tree |
| 7618 |
build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type, |
| 7619 |
tree cookie_size, tree full_size, tree arg_size, |
| 7620 |
bool *non_constant_p, bool *overflow_p) |
| 7621 |
{ |
| 7622 |
gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size)); |
| 7623 |
gcc_assert (tree_fits_uhwi_p (full_size)); |
| 7624 |
unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0; |
| 7625 |
if (arg_size) |
| 7626 |
{ |
| 7627 |
STRIP_NOPS (arg_size); |
| 7628 |
if (cookie_size) |
| 7629 |
{ |
| 7630 |
if (TREE_CODE (arg_size) != PLUS_EXPR) |
| 7631 |
arg_size = NULL_TREE; |
| 7632 |
else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST |
| 7633 |
&& tree_int_cst_equal (cookie_size, |
| 7634 |
TREE_OPERAND (arg_size, 0))) |
| 7635 |
{ |
| 7636 |
arg_size = TREE_OPERAND (arg_size, 1); |
| 7637 |
STRIP_NOPS (arg_size); |
| 7638 |
} |
| 7639 |
else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST |
| 7640 |
&& tree_int_cst_equal (cookie_size, |
| 7641 |
TREE_OPERAND (arg_size, 1))) |
| 7642 |
{ |
| 7643 |
arg_size = TREE_OPERAND (arg_size, 0); |
| 7644 |
STRIP_NOPS (arg_size); |
| 7645 |
} |
| 7646 |
else |
| 7647 |
arg_size = NULL_TREE; |
| 7648 |
} |
| 7649 |
if (arg_size && TREE_CODE (arg_size) == MULT_EXPR) |
| 7650 |
{ |
| 7651 |
tree op0 = TREE_OPERAND (arg_size, 0); |
| 7652 |
tree op1 = TREE_OPERAND (arg_size, 1); |
| 7653 |
if (integer_zerop (op0)) |
| 7654 |
arg_size |
| 7655 |
= cxx_eval_constant_expression (ctx, op1, vc_prvalue, |
| 7656 |
non_constant_p, overflow_p); |
| 7657 |
else if (integer_zerop (op1)) |
| 7658 |
arg_size |
| 7659 |
= cxx_eval_constant_expression (ctx, op0, vc_prvalue, |
| 7660 |
non_constant_p, overflow_p); |
| 7661 |
else |
| 7662 |
arg_size = NULL_TREE; |
| 7663 |
} |
| 7664 |
else |
| 7665 |
arg_size = NULL_TREE; |
| 7666 |
} |
| 7667 |
|
| 7668 |
unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size); |
| 7669 |
if (!arg_size) |
| 7670 |
{ |
| 7671 |
unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type); |
| 7672 |
gcc_assert (fsz >= csz); |
| 7673 |
fsz -= csz; |
| 7674 |
if (esz) |
| 7675 |
fsz /= esz; |
| 7676 |
} |
| 7677 |
tree itype2 = build_index_type (size_int (fsz - 1)); |
| 7678 |
if (!cookie_size) |
| 7679 |
return build_cplus_array_type (elt_type, itype2); |
| 7680 |
return build_new_constexpr_heap_type (elt_type, cookie_size, itype2); |
| 7681 |
} |
| 7682 |
|
| 7683 |
/* Attempt to reduce the expression T to a constant value. |
| 7684 |
On failure, issue diagnostic and return error_mark_node. */ |
| 7685 |
/* FIXME unify with c_fully_fold */ |
| 7686 |
/* FIXME overflow_p is too global */ |
| 7687 |
|
| 7688 |
static tree |
| 7689 |
cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, |
| 7690 |
value_cat lval, |
| 7691 |
bool *non_constant_p, bool *overflow_p, |
| 7692 |
tree *jump_target /* = NULL */) |
| 7693 |
{ |
| 7694 |
if (jump_target && *jump_target) |
| 7695 |
{ |
| 7696 |
/* If we are jumping, ignore all statements/expressions except those |
| 7697 |
that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */ |
| 7698 |
switch (TREE_CODE (t)) |
| 7699 |
{ |
| 7700 |
case BIND_EXPR: |
| 7701 |
case STATEMENT_LIST: |
| 7702 |
case LOOP_EXPR: |
| 7703 |
case COND_EXPR: |
| 7704 |
case IF_STMT: |
| 7705 |
case DO_STMT: |
| 7706 |
case WHILE_STMT: |
| 7707 |
case FOR_STMT: |
| 7708 |
break; |
| 7709 |
case LABEL_EXPR: |
| 7710 |
case CASE_LABEL_EXPR: |
| 7711 |
if (label_matches (ctx, jump_target, t)) |
| 7712 |
/* Found it. */ |
| 7713 |
*jump_target = NULL_TREE; |
| 7714 |
return NULL_TREE; |
| 7715 |
default: |
| 7716 |
return NULL_TREE; |
| 7717 |
} |
| 7718 |
} |
| 7719 |
if (error_operand_p (t)) |
| 7720 |
{ |
| 7721 |
*non_constant_p = true; |
| 7722 |
return t; |
| 7723 |
} |
| 7724 |
|
| 7725 |
/* Change the input location to the currently processed expression for |
| 7726 |
better error messages when a subexpression has no location. */ |
| 7727 |
location_t loc = cp_expr_loc_or_input_loc (t); |
| 7728 |
iloc_sentinel sentinel (loc); |
| 7729 |
|
| 7730 |
STRIP_ANY_LOCATION_WRAPPER (t); |
| 7731 |
|
| 7732 |
if (CONSTANT_CLASS_P (t)) |
| 7733 |
{ |
| 7734 |
if (TREE_OVERFLOW (t)) |
| 7735 |
{ |
| 7736 |
if (!ctx->quiet) |
| 7737 |
permerror (input_location, "overflow in constant expression"); |
| 7738 |
if (!flag_permissive || ctx->quiet) |
| 7739 |
*overflow_p = true; |
| 7740 |
} |
| 7741 |
|
| 7742 |
if (TREE_CODE (t) == INTEGER_CST |
| 7743 |
&& TYPE_PTR_P (TREE_TYPE (t)) |
| 7744 |
/* INTEGER_CST with pointer-to-method type is only used |
| 7745 |
for a virtual method in a pointer to member function. |
| 7746 |
Don't reject those. */ |
| 7747 |
&& TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE |
| 7748 |
&& !integer_zerop (t)) |
| 7749 |
{ |
| 7750 |
if (!ctx->quiet) |
| 7751 |
error ("value %qE of type %qT is not a constant expression", |
| 7752 |
t, TREE_TYPE (t)); |
| 7753 |
*non_constant_p = true; |
| 7754 |
} |
| 7755 |
|
| 7756 |
return t; |
| 7757 |
} |
| 7758 |
|
| 7759 |
/* Avoid excessively long constexpr evaluations. */ |
| 7760 |
if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit) |
| 7761 |
{ |
| 7762 |
if (!ctx->quiet) |
| 7763 |
error_at (loc, |
| 7764 |
"%<constexpr%> evaluation operation count exceeds limit of " |
| 7765 |
"%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)", |
| 7766 |
constexpr_ops_limit); |
| 7767 |
ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT); |
| 7768 |
*non_constant_p = true; |
| 7769 |
return t; |
| 7770 |
} |
| 7771 |
|
| 7772 |
constexpr_ctx new_ctx; |
| 7773 |
tree r = t; |
| 7774 |
|
| 7775 |
tree_code tcode = TREE_CODE (t); |
| 7776 |
switch (tcode) |
| 7777 |
{ |
| 7778 |
case RESULT_DECL: |
| 7779 |
if (lval) |
| 7780 |
return t; |
| 7781 |
/* We ask for an rvalue for the RESULT_DECL when indirecting |
| 7782 |
through an invisible reference, or in named return value |
| 7783 |
optimization. */ |
| 7784 |
if (tree v = ctx->global->get_value (t)) |
| 7785 |
return v; |
| 7786 |
else |
| 7787 |
{ |
| 7788 |
if (!ctx->quiet) |
| 7789 |
error ("%qE is not a constant expression", t); |
| 7790 |
*non_constant_p = true; |
| 7791 |
} |
| 7792 |
break; |
| 7793 |
|
| 7794 |
case VAR_DECL: |
| 7795 |
if (DECL_HAS_VALUE_EXPR_P (t)) |
| 7796 |
{ |
| 7797 |
if (is_normal_capture_proxy (t) |
| 7798 |
&& current_function_decl == DECL_CONTEXT (t)) |
| 7799 |
{ |
| 7800 |
/* Function parms aren't constexpr within the function |
| 7801 |
definition, so don't try to look at the closure. But if the |
| 7802 |
captured variable is constant, try to evaluate it directly. */ |
| 7803 |
r = DECL_CAPTURED_VARIABLE (t); |
| 7804 |
} |
| 7805 |
else |
| 7806 |
r = DECL_VALUE_EXPR (t); |
| 7807 |
|
| 7808 |
tree type = TREE_TYPE (t); |
| 7809 |
if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r))) |
| 7810 |
{ |
| 7811 |
/* Adjust r to match the reference-ness of t. */ |
| 7812 |
if (TYPE_REF_P (type)) |
| 7813 |
r = build_address (r); |
| 7814 |
else |
| 7815 |
r = convert_from_reference (r); |
| 7816 |
} |
| 7817 |
return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, |
| 7818 |
overflow_p); |
| 7819 |
} |
| 7820 |
/* fall through */ |
| 7821 |
case CONST_DECL: |
| 7822 |
/* We used to not check lval for CONST_DECL, but darwin.cc uses |
| 7823 |
CONST_DECL for aggregate constants. */ |
| 7824 |
if (lval) |
| 7825 |
return t; |
| 7826 |
else if (t == ctx->object) |
| 7827 |
return ctx->ctor; |
| 7828 |
if (VAR_P (t)) |
| 7829 |
{ |
| 7830 |
if (tree v = ctx->global->get_value (t)) |
| 7831 |
{ |
| 7832 |
r = v; |
| 7833 |
break; |
| 7834 |
} |
| 7835 |
if (ctx->global->is_outside_lifetime (t)) |
| 7836 |
{ |
| 7837 |
if (!ctx->quiet) |
| 7838 |
outside_lifetime_error (loc, t); |
| 7839 |
*non_constant_p = true; |
| 7840 |
break; |
| 7841 |
} |
| 7842 |
} |
| 7843 |
if (ctx->manifestly_const_eval == mce_true) |
| 7844 |
maybe_warn_about_constant_value (loc, t); |
| 7845 |
if (COMPLETE_TYPE_P (TREE_TYPE (t)) |
| 7846 |
&& is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
| 7847 |
{ |
| 7848 |
/* If the class is empty, we aren't actually loading anything. */ |
| 7849 |
r = build_constructor (TREE_TYPE (t), NULL); |
| 7850 |
TREE_CONSTANT (r) = true; |
| 7851 |
} |
| 7852 |
else if (ctx->strict) |
| 7853 |
r = decl_really_constant_value (t, /*unshare_p=*/false); |
| 7854 |
else |
| 7855 |
r = decl_constant_value (t, /*unshare_p=*/false); |
| 7856 |
if (TREE_CODE (r) == TARGET_EXPR |
| 7857 |
&& TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR) |
| 7858 |
r = TARGET_EXPR_INITIAL (r); |
| 7859 |
if (DECL_P (r) |
| 7860 |
/* P2280 allows references to unknown. */ |
| 7861 |
&& !(p2280_active_p (ctx) && VAR_P (t) && TYPE_REF_P (TREE_TYPE (t)))) |
| 7862 |
{ |
| 7863 |
if (!ctx->quiet) |
| 7864 |
non_const_var_error (loc, r, /*fundef_p*/false); |
| 7865 |
*non_constant_p = true; |
| 7866 |
} |
| 7867 |
break; |
| 7868 |
|
| 7869 |
case DEBUG_BEGIN_STMT: |
| 7870 |
/* ??? It might be nice to retain this information somehow, so |
| 7871 |
as to be able to step into a constexpr function call. */ |
| 7872 |
/* Fall through. */ |
| 7873 |
|
| 7874 |
case FUNCTION_DECL: |
| 7875 |
case TEMPLATE_DECL: |
| 7876 |
case LABEL_DECL: |
| 7877 |
case LABEL_EXPR: |
| 7878 |
case CASE_LABEL_EXPR: |
| 7879 |
case PREDICT_EXPR: |
| 7880 |
return t; |
| 7881 |
|
| 7882 |
case PARM_DECL: |
| 7883 |
if (lval && !TYPE_REF_P (TREE_TYPE (t))) |
| 7884 |
{ |
| 7885 |
/* glvalue use. */ |
| 7886 |
if (TREE_ADDRESSABLE (TREE_TYPE (t))) |
| 7887 |
if (tree v = ctx->global->get_value (t)) |
| 7888 |
r = v; |
| 7889 |
} |
| 7890 |
else if (tree v = ctx->global->get_value (t)) |
| 7891 |
{ |
| 7892 |
r = v; |
| 7893 |
if (TREE_ADDRESSABLE (TREE_TYPE (t))) |
| 7894 |
r = cxx_eval_constant_expression (ctx, r, vc_prvalue, |
| 7895 |
non_constant_p, overflow_p); |
| 7896 |
} |
| 7897 |
else if (lval) |
| 7898 |
/* Defer in case this is only used for its type. */; |
| 7899 |
else if (ctx->global->is_outside_lifetime (t)) |
| 7900 |
{ |
| 7901 |
if (!ctx->quiet) |
| 7902 |
outside_lifetime_error (loc, t); |
| 7903 |
*non_constant_p = true; |
| 7904 |
break; |
| 7905 |
} |
| 7906 |
else if (COMPLETE_TYPE_P (TREE_TYPE (t)) |
| 7907 |
&& is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
| 7908 |
{ |
| 7909 |
/* If the class is empty, we aren't actually loading anything. */ |
| 7910 |
r = build_constructor (TREE_TYPE (t), NULL); |
| 7911 |
TREE_CONSTANT (r) = true; |
| 7912 |
} |
| 7913 |
else if (p2280_active_p (ctx) && TYPE_REF_P (TREE_TYPE (t))) |
| 7914 |
/* P2280 allows references to unknown... */; |
| 7915 |
else if (p2280_active_p (ctx) && is_this_parameter (t)) |
| 7916 |
/* ...as well as the this pointer. */; |
| 7917 |
else |
| 7918 |
{ |
| 7919 |
if (!ctx->quiet) |
| 7920 |
error ("%qE is not a constant expression", t); |
| 7921 |
*non_constant_p = true; |
| 7922 |
} |
| 7923 |
break; |
| 7924 |
|
| 7925 |
case CALL_EXPR: |
| 7926 |
case AGGR_INIT_EXPR: |
| 7927 |
r = cxx_eval_call_expression (ctx, t, lval, |
| 7928 |
non_constant_p, overflow_p); |
| 7929 |
break; |
| 7930 |
|
| 7931 |
case DECL_EXPR: |
| 7932 |
{ |
| 7933 |
r = DECL_EXPR_DECL (t); |
| 7934 |
if (TREE_CODE (r) == USING_DECL) |
| 7935 |
{ |
| 7936 |
r = void_node; |
| 7937 |
break; |
| 7938 |
} |
| 7939 |
|
| 7940 |
if (VAR_P (r) |
| 7941 |
&& (TREE_STATIC (r) |
| 7942 |
|| (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r))) |
| 7943 |
/* Allow __FUNCTION__ etc. */ |
| 7944 |
&& !DECL_ARTIFICIAL (r) |
| 7945 |
&& !decl_constant_var_p (r)) |
| 7946 |
{ |
| 7947 |
if (!ctx->quiet) |
| 7948 |
{ |
| 7949 |
if (CP_DECL_THREAD_LOCAL_P (r)) |
| 7950 |
error_at (loc, "control passes through definition of %qD " |
| 7951 |
"with thread storage duration", r); |
| 7952 |
else |
| 7953 |
error_at (loc, "control passes through definition of %qD " |
| 7954 |
"with static storage duration", r); |
| 7955 |
} |
| 7956 |
*non_constant_p = true; |
| 7957 |
break; |
| 7958 |
} |
| 7959 |
|
| 7960 |
/* make_rtl_for_nonlocal_decl could have deferred emission of |
| 7961 |
a local static var, but if it appears in a statement expression |
| 7962 |
which is constant expression evaluated to e.g. just the address |
| 7963 |
of the variable, its DECL_EXPR will never be seen during |
| 7964 |
gimple lowering's record_vars_into as the statement expression |
| 7965 |
will not be in the IL at all. */ |
| 7966 |
if (VAR_P (r) |
| 7967 |
&& TREE_STATIC (r) |
| 7968 |
&& !DECL_REALLY_EXTERN (r) |
| 7969 |
&& DECL_FUNCTION_SCOPE_P (r) |
| 7970 |
&& !var_in_maybe_constexpr_fn (r) |
| 7971 |
&& decl_constant_var_p (r)) |
| 7972 |
{ |
| 7973 |
varpool_node *node = varpool_node::get (r); |
| 7974 |
if (node == NULL || !node->definition) |
| 7975 |
rest_of_decl_compilation (r, 0, at_eof); |
| 7976 |
} |
| 7977 |
|
| 7978 |
if (AGGREGATE_TYPE_P (TREE_TYPE (r)) |
| 7979 |
|| VECTOR_TYPE_P (TREE_TYPE (r))) |
| 7980 |
{ |
| 7981 |
new_ctx = *ctx; |
| 7982 |
new_ctx.object = r; |
| 7983 |
new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL); |
| 7984 |
CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; |
| 7985 |
ctx->global->put_value (r, new_ctx.ctor); |
| 7986 |
ctx = &new_ctx; |
| 7987 |
} |
| 7988 |
|
| 7989 |
if (tree init = DECL_INITIAL (r)) |
| 7990 |
{ |
| 7991 |
init = cxx_eval_constant_expression (ctx, init, vc_prvalue, |
| 7992 |
non_constant_p, overflow_p); |
| 7993 |
/* Don't share a CONSTRUCTOR that might be changed. */ |
| 7994 |
init = unshare_constructor (init); |
| 7995 |
/* Remember that a constant object's constructor has already |
| 7996 |
run. */ |
| 7997 |
if (CLASS_TYPE_P (TREE_TYPE (r)) |
| 7998 |
&& CP_TYPE_CONST_P (TREE_TYPE (r))) |
| 7999 |
TREE_READONLY (init) = true; |
| 8000 |
ctx->global->put_value (r, init); |
| 8001 |
} |
| 8002 |
else if (ctx == &new_ctx) |
| 8003 |
/* We gave it a CONSTRUCTOR above. */; |
| 8004 |
else |
| 8005 |
ctx->global->put_value (r, NULL_TREE); |
| 8006 |
} |
| 8007 |
break; |
| 8008 |
|
| 8009 |
case TARGET_EXPR: |
| 8010 |
{ |
| 8011 |
tree type = TREE_TYPE (t); |
| 8012 |
|
| 8013 |
if (!literal_type_p (type)) |
| 8014 |
{ |
| 8015 |
if (!ctx->quiet) |
| 8016 |
{ |
| 8017 |
auto_diagnostic_group d; |
| 8018 |
error ("temporary of non-literal type %qT in a " |
| 8019 |
"constant expression", type); |
| 8020 |
explain_non_literal_class (type); |
| 8021 |
} |
| 8022 |
*non_constant_p = true; |
| 8023 |
break; |
| 8024 |
} |
| 8025 |
gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t)); |
| 8026 |
/* Avoid evaluating a TARGET_EXPR more than once. */ |
| 8027 |
tree slot = TARGET_EXPR_SLOT (t); |
| 8028 |
if (tree v = ctx->global->get_value (slot)) |
| 8029 |
{ |
| 8030 |
if (lval) |
| 8031 |
return slot; |
| 8032 |
r = v; |
| 8033 |
break; |
| 8034 |
} |
| 8035 |
if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))) |
| 8036 |
{ |
| 8037 |
/* We're being expanded without an explicit target, so start |
| 8038 |
initializing a new object; expansion with an explicit target |
| 8039 |
strips the TARGET_EXPR before we get here. */ |
| 8040 |
new_ctx = *ctx; |
| 8041 |
/* Link CTX to NEW_CTX so that lookup_placeholder can resolve |
| 8042 |
any PLACEHOLDER_EXPR within the initializer that refers to the |
| 8043 |
former object under construction. */ |
| 8044 |
new_ctx.parent = ctx; |
| 8045 |
new_ctx.ctor = build_constructor (type, NULL); |
| 8046 |
CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true; |
| 8047 |
new_ctx.object = slot; |
| 8048 |
ctx->global->put_value (new_ctx.object, new_ctx.ctor); |
| 8049 |
ctx = &new_ctx; |
| 8050 |
} |
| 8051 |
|
| 8052 |
/* If the initializer is complex, evaluate it to initialize slot. */ |
| 8053 |
bool is_complex = target_expr_needs_replace (t); |
| 8054 |
if (is_complex) |
| 8055 |
/* In case no initialization actually happens, clear out any |
| 8056 |
void_node from a previous evaluation. */ |
| 8057 |
ctx->global->put_value (slot, NULL_TREE); |
| 8058 |
|
| 8059 |
/* Pass vc_prvalue because this indicates |
| 8060 |
initialization of a temporary. */ |
| 8061 |
r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_prvalue, |
| 8062 |
non_constant_p, overflow_p); |
| 8063 |
if (*non_constant_p) |
| 8064 |
break; |
| 8065 |
if (!is_complex) |
| 8066 |
{ |
| 8067 |
r = unshare_constructor (r); |
| 8068 |
/* Adjust the type of the result to the type of the temporary. */ |
| 8069 |
r = adjust_temp_type (type, r); |
| 8070 |
ctx->global->put_value (slot, r); |
| 8071 |
} |
| 8072 |
if (TARGET_EXPR_CLEANUP (t) && !CLEANUP_EH_ONLY (t)) |
| 8073 |
ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t)); |
| 8074 |
if (ctx->save_exprs) |
| 8075 |
ctx->save_exprs->safe_push (slot); |
| 8076 |
if (lval) |
| 8077 |
return slot; |
| 8078 |
if (is_complex) |
| 8079 |
r = ctx->global->get_value (slot); |
| 8080 |
} |
| 8081 |
break; |
| 8082 |
|
| 8083 |
case INIT_EXPR: |
| 8084 |
case MODIFY_EXPR: |
| 8085 |
gcc_assert (jump_target == NULL || *jump_target == NULL_TREE); |
| 8086 |
r = cxx_eval_store_expression (ctx, t, lval, |
| 8087 |
non_constant_p, overflow_p); |
| 8088 |
break; |
| 8089 |
|
| 8090 |
case SCOPE_REF: |
| 8091 |
r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), |
| 8092 |
lval, |
| 8093 |
non_constant_p, overflow_p); |
| 8094 |
break; |
| 8095 |
|
| 8096 |
case RETURN_EXPR: |
| 8097 |
if (TREE_OPERAND (t, 0) != NULL_TREE) |
| 8098 |
r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
| 8099 |
lval, |
| 8100 |
non_constant_p, overflow_p); |
| 8101 |
/* FALLTHRU */ |
| 8102 |
case BREAK_STMT: |
| 8103 |
case CONTINUE_STMT: |
| 8104 |
if (jump_target) |
| 8105 |
*jump_target = t; |
| 8106 |
else |
| 8107 |
{ |
| 8108 |
/* Can happen with ({ return true; }) && false; passed to |
| 8109 |
maybe_constant_value. There is nothing to jump over in this |
| 8110 |
case, and the bug will be diagnosed later. */ |
| 8111 |
gcc_assert (ctx->quiet); |
| 8112 |
*non_constant_p = true; |
| 8113 |
} |
| 8114 |
break; |
| 8115 |
|
| 8116 |
case SAVE_EXPR: |
| 8117 |
/* Avoid evaluating a SAVE_EXPR more than once. */ |
| 8118 |
if (tree v = ctx->global->get_value (t)) |
| 8119 |
r = v; |
| 8120 |
else |
| 8121 |
{ |
| 8122 |
r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue, |
| 8123 |
non_constant_p, overflow_p); |
| 8124 |
if (*non_constant_p) |
| 8125 |
break; |
| 8126 |
ctx->global->put_value (t, r); |
| 8127 |
if (ctx->save_exprs) |
| 8128 |
ctx->save_exprs->safe_push (t); |
| 8129 |
} |
| 8130 |
break; |
| 8131 |
|
| 8132 |
case TRY_CATCH_EXPR: |
| 8133 |
if (TREE_OPERAND (t, 0) == NULL_TREE) |
| 8134 |
{ |
| 8135 |
r = void_node; |
| 8136 |
break; |
| 8137 |
} |
| 8138 |
/* FALLTHRU */ |
| 8139 |
case NON_LVALUE_EXPR: |
| 8140 |
case TRY_BLOCK: |
| 8141 |
case MUST_NOT_THROW_EXPR: |
| 8142 |
case EXPR_STMT: |
| 8143 |
case EH_SPEC_BLOCK: |
| 8144 |
r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
| 8145 |
lval, |
| 8146 |
non_constant_p, overflow_p, |
| 8147 |
jump_target); |
| 8148 |
break; |
| 8149 |
|
| 8150 |
case CLEANUP_POINT_EXPR: |
| 8151 |
{ |
| 8152 |
auto_vec<tree, 2> cleanups; |
| 8153 |
vec<tree> *prev_cleanups = ctx->global->cleanups; |
| 8154 |
ctx->global->cleanups = &cleanups; |
| 8155 |
|
| 8156 |
auto_vec<tree, 10> save_exprs; |
| 8157 |
constexpr_ctx new_ctx = *ctx; |
| 8158 |
new_ctx.save_exprs = &save_exprs; |
| 8159 |
|
| 8160 |
r = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 0), |
| 8161 |
lval, |
| 8162 |
non_constant_p, overflow_p, |
| 8163 |
jump_target); |
| 8164 |
|
| 8165 |
ctx->global->cleanups = prev_cleanups; |
| 8166 |
unsigned int i; |
| 8167 |
tree cleanup; |
| 8168 |
/* Evaluate the cleanups. */ |
| 8169 |
FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup) |
| 8170 |
cxx_eval_constant_expression (&new_ctx, cleanup, vc_discard, |
| 8171 |
non_constant_p, overflow_p); |
| 8172 |
|
| 8173 |
/* Forget SAVE_EXPRs and TARGET_EXPRs created by this |
| 8174 |
full-expression. */ |
| 8175 |
for (tree save_expr : save_exprs) |
| 8176 |
destroy_value_checked (ctx, save_expr, non_constant_p); |
| 8177 |
} |
| 8178 |
break; |
| 8179 |
|
| 8180 |
case TRY_FINALLY_EXPR: |
| 8181 |
r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, |
| 8182 |
non_constant_p, overflow_p, |
| 8183 |
jump_target); |
| 8184 |
if (!*non_constant_p) |
| 8185 |
/* Also evaluate the cleanup. */ |
| 8186 |
cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard, |
| 8187 |
non_constant_p, overflow_p); |
| 8188 |
break; |
| 8189 |
|
| 8190 |
case EH_ELSE_EXPR: |
| 8191 |
/* Evaluate any cleanup that applies to non-EH exits. */ |
| 8192 |
cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_discard, |
| 8193 |
non_constant_p, overflow_p); |
| 8194 |
|
| 8195 |
/* We do not have constexpr exceptions yet, so skip the EH path. */ |
| 8196 |
break; |
| 8197 |
|
| 8198 |
case CLEANUP_STMT: |
| 8199 |
r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval, |
| 8200 |
non_constant_p, overflow_p, |
| 8201 |
jump_target); |
| 8202 |
if (!CLEANUP_EH_ONLY (t) && !*non_constant_p) |
| 8203 |
{ |
| 8204 |
iloc_sentinel ils (loc); |
| 8205 |
/* Also evaluate the cleanup. */ |
| 8206 |
cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), vc_discard, |
| 8207 |
non_constant_p, overflow_p); |
| 8208 |
} |
| 8209 |
break; |
| 8210 |
|
| 8211 |
/* These differ from cxx_eval_unary_expression in that this doesn't |
| 8212 |
check for a constant operand or result; an address can be |
| 8213 |
constant without its operand being, and vice versa. */ |
| 8214 |
case MEM_REF: |
| 8215 |
case INDIRECT_REF: |
| 8216 |
r = cxx_eval_indirect_ref (ctx, t, lval, |
| 8217 |
non_constant_p, overflow_p); |
| 8218 |
break; |
| 8219 |
|
| 8220 |
case ADDR_EXPR: |
| 8221 |
{ |
| 8222 |
tree oldop = TREE_OPERAND (t, 0); |
| 8223 |
tree op = cxx_eval_constant_expression (ctx, oldop, vc_glvalue, |
| 8224 |
non_constant_p, overflow_p); |
| 8225 |
/* Don't VERIFY_CONSTANT here. */ |
| 8226 |
if (*non_constant_p) |
| 8227 |
return t; |
| 8228 |
gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR); |
| 8229 |
/* This function does more aggressive folding than fold itself. */ |
| 8230 |
r = build_fold_addr_expr_with_type (op, TREE_TYPE (t)); |
| 8231 |
if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop) |
| 8232 |
{ |
| 8233 |
ggc_free (r); |
| 8234 |
return t; |
| 8235 |
} |
| 8236 |
break; |
| 8237 |
} |
| 8238 |
|
| 8239 |
case REALPART_EXPR: |
| 8240 |
case IMAGPART_EXPR: |
| 8241 |
if (lval) |
| 8242 |
{ |
| 8243 |
r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, |
| 8244 |
non_constant_p, overflow_p); |
| 8245 |
if (r == error_mark_node) |
| 8246 |
; |
| 8247 |
else if (r == TREE_OPERAND (t, 0) || lval == vc_discard) |
| 8248 |
r = t; |
| 8249 |
else |
| 8250 |
r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r); |
| 8251 |
break; |
| 8252 |
} |
| 8253 |
/* FALLTHRU */ |
| 8254 |
case CONJ_EXPR: |
| 8255 |
case FIX_TRUNC_EXPR: |
| 8256 |
case FLOAT_EXPR: |
| 8257 |
case NEGATE_EXPR: |
| 8258 |
case ABS_EXPR: |
| 8259 |
case ABSU_EXPR: |
| 8260 |
case BIT_NOT_EXPR: |
| 8261 |
case TRUTH_NOT_EXPR: |
| 8262 |
case FIXED_CONVERT_EXPR: |
| 8263 |
case VEC_DUPLICATE_EXPR: |
| 8264 |
r = cxx_eval_unary_expression (ctx, t, lval, |
| 8265 |
non_constant_p, overflow_p); |
| 8266 |
break; |
| 8267 |
|
| 8268 |
case SIZEOF_EXPR: |
| 8269 |
r = fold_sizeof_expr (t); |
| 8270 |
/* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR, |
| 8271 |
which could lead to an infinite recursion. */ |
| 8272 |
if (TREE_CODE (r) != SIZEOF_EXPR) |
| 8273 |
r = cxx_eval_constant_expression (ctx, r, lval, |
| 8274 |
non_constant_p, overflow_p, |
| 8275 |
jump_target); |
| 8276 |
else |
| 8277 |
{ |
| 8278 |
*non_constant_p = true; |
| 8279 |
gcc_assert (ctx->quiet); |
| 8280 |
} |
| 8281 |
|
| 8282 |
break; |
| 8283 |
|
| 8284 |
case COMPOUND_EXPR: |
| 8285 |
{ |
| 8286 |
/* check_return_expr sometimes wraps a TARGET_EXPR in a |
| 8287 |
COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR |
| 8288 |
introduced by build_call_a. */ |
| 8289 |
tree op0 = TREE_OPERAND (t, 0); |
| 8290 |
tree op1 = TREE_OPERAND (t, 1); |
| 8291 |
STRIP_NOPS (op1); |
| 8292 |
if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) |
| 8293 |
|| TREE_CODE (op1) == EMPTY_CLASS_EXPR) |
| 8294 |
r = cxx_eval_constant_expression (ctx, op0, |
| 8295 |
lval, non_constant_p, overflow_p, |
| 8296 |
jump_target); |
| 8297 |
else |
| 8298 |
{ |
| 8299 |
/* Check that the LHS is constant and then discard it. */ |
| 8300 |
cxx_eval_constant_expression (ctx, op0, vc_discard, |
| 8301 |
non_constant_p, overflow_p, |
| 8302 |
jump_target); |
| 8303 |
if (*non_constant_p) |
| 8304 |
return t; |
| 8305 |
op1 = TREE_OPERAND (t, 1); |
| 8306 |
r = cxx_eval_constant_expression (ctx, op1, |
| 8307 |
lval, non_constant_p, overflow_p, |
| 8308 |
jump_target); |
| 8309 |
} |
| 8310 |
} |
| 8311 |
break; |
| 8312 |
|
| 8313 |
case POINTER_PLUS_EXPR: |
| 8314 |
case POINTER_DIFF_EXPR: |
| 8315 |
case PLUS_EXPR: |
| 8316 |
case MINUS_EXPR: |
| 8317 |
case MULT_EXPR: |
| 8318 |
case TRUNC_DIV_EXPR: |
| 8319 |
case CEIL_DIV_EXPR: |
| 8320 |
case FLOOR_DIV_EXPR: |
| 8321 |
case ROUND_DIV_EXPR: |
| 8322 |
case TRUNC_MOD_EXPR: |
| 8323 |
case CEIL_MOD_EXPR: |
| 8324 |
case ROUND_MOD_EXPR: |
| 8325 |
case RDIV_EXPR: |
| 8326 |
case EXACT_DIV_EXPR: |
| 8327 |
case MIN_EXPR: |
| 8328 |
case MAX_EXPR: |
| 8329 |
case LSHIFT_EXPR: |
| 8330 |
case RSHIFT_EXPR: |
| 8331 |
case LROTATE_EXPR: |
| 8332 |
case RROTATE_EXPR: |
| 8333 |
case BIT_IOR_EXPR: |
| 8334 |
case BIT_XOR_EXPR: |
| 8335 |
case BIT_AND_EXPR: |
| 8336 |
case TRUTH_XOR_EXPR: |
| 8337 |
case LT_EXPR: |
| 8338 |
case LE_EXPR: |
| 8339 |
case GT_EXPR: |
| 8340 |
case GE_EXPR: |
| 8341 |
case EQ_EXPR: |
| 8342 |
case NE_EXPR: |
| 8343 |
case SPACESHIP_EXPR: |
| 8344 |
case UNORDERED_EXPR: |
| 8345 |
case ORDERED_EXPR: |
| 8346 |
case UNLT_EXPR: |
| 8347 |
case UNLE_EXPR: |
| 8348 |
case UNGT_EXPR: |
| 8349 |
case UNGE_EXPR: |
| 8350 |
case UNEQ_EXPR: |
| 8351 |
case LTGT_EXPR: |
| 8352 |
case RANGE_EXPR: |
| 8353 |
case COMPLEX_EXPR: |
| 8354 |
r = cxx_eval_binary_expression (ctx, t, lval, |
| 8355 |
non_constant_p, overflow_p); |
| 8356 |
break; |
| 8357 |
|
| 8358 |
/* fold can introduce non-IF versions of these; still treat them as |
| 8359 |
short-circuiting. */ |
| 8360 |
case TRUTH_AND_EXPR: |
| 8361 |
case TRUTH_ANDIF_EXPR: |
| 8362 |
r = cxx_eval_logical_expression (ctx, t, boolean_false_node, |
| 8363 |
boolean_true_node, |
| 8364 |
non_constant_p, overflow_p); |
| 8365 |
break; |
| 8366 |
|
| 8367 |
case TRUTH_OR_EXPR: |
| 8368 |
case TRUTH_ORIF_EXPR: |
| 8369 |
r = cxx_eval_logical_expression (ctx, t, boolean_true_node, |
| 8370 |
boolean_false_node, |
| 8371 |
non_constant_p, overflow_p); |
| 8372 |
break; |
| 8373 |
|
| 8374 |
case ARRAY_REF: |
| 8375 |
r = cxx_eval_array_reference (ctx, t, lval, |
| 8376 |
non_constant_p, overflow_p); |
| 8377 |
break; |
| 8378 |
|
| 8379 |
case COMPONENT_REF: |
| 8380 |
if (is_overloaded_fn (t)) |
| 8381 |
{ |
| 8382 |
/* We can only get here in checking mode via |
| 8383 |
build_non_dependent_expr, because any expression that |
| 8384 |
calls or takes the address of the function will have |
| 8385 |
pulled a FUNCTION_DECL out of the COMPONENT_REF. */ |
| 8386 |
gcc_checking_assert (ctx->quiet || errorcount); |
| 8387 |
*non_constant_p = true; |
| 8388 |
return t; |
| 8389 |
} |
| 8390 |
r = cxx_eval_component_reference (ctx, t, lval, |
| 8391 |
non_constant_p, overflow_p); |
| 8392 |
break; |
| 8393 |
|
| 8394 |
case BIT_FIELD_REF: |
| 8395 |
r = cxx_eval_bit_field_ref (ctx, t, lval, |
| 8396 |
non_constant_p, overflow_p); |
| 8397 |
break; |
| 8398 |
|
| 8399 |
case COND_EXPR: |
| 8400 |
case IF_STMT: |
| 8401 |
if (jump_target && *jump_target) |
| 8402 |
{ |
| 8403 |
tree orig_jump = *jump_target; |
| 8404 |
tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1)) |
| 8405 |
? TREE_OPERAND (t, 1) : void_node); |
| 8406 |
/* When jumping to a label, the label might be either in the |
| 8407 |
then or else blocks, so process then block first in skipping |
| 8408 |
mode first, and if we are still in the skipping mode at its end, |
| 8409 |
process the else block too. */ |
| 8410 |
r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, |
| 8411 |
overflow_p, jump_target); |
| 8412 |
/* It's possible that we found the label in the then block. But |
| 8413 |
it could have been followed by another jumping statement, e.g. |
| 8414 |
say we're looking for case 1: |
| 8415 |
if (cond) |
| 8416 |
{ |
| 8417 |
// skipped statements |
| 8418 |
case 1:; // clears up *jump_target |
| 8419 |
return 1; // and sets it to a RETURN_EXPR |
| 8420 |
} |
| 8421 |
else { ... } |
| 8422 |
in which case we need not go looking to the else block. |
| 8423 |
(goto is not allowed in a constexpr function.) */ |
| 8424 |
if (*jump_target == orig_jump) |
| 8425 |
{ |
| 8426 |
arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2)) |
| 8427 |
? TREE_OPERAND (t, 2) : void_node); |
| 8428 |
r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p, |
| 8429 |
overflow_p, jump_target); |
| 8430 |
} |
| 8431 |
break; |
| 8432 |
} |
| 8433 |
r = cxx_eval_conditional_expression (ctx, t, lval, |
| 8434 |
non_constant_p, overflow_p, |
| 8435 |
jump_target); |
| 8436 |
break; |
| 8437 |
case VEC_COND_EXPR: |
| 8438 |
r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p, |
| 8439 |
overflow_p); |
| 8440 |
break; |
| 8441 |
|
| 8442 |
case CONSTRUCTOR: |
| 8443 |
if (TREE_CONSTANT (t) && reduced_constant_expression_p (t)) |
| 8444 |
{ |
| 8445 |
/* Don't re-process a constant CONSTRUCTOR. */ |
| 8446 |
verify_constructor_flags (t); |
| 8447 |
if (TREE_CONSTANT (t)) |
| 8448 |
return t; |
| 8449 |
} |
| 8450 |
r = cxx_eval_bare_aggregate (ctx, t, lval, |
| 8451 |
non_constant_p, overflow_p); |
| 8452 |
break; |
| 8453 |
|
| 8454 |
case VEC_INIT_EXPR: |
| 8455 |
/* We can get this in a defaulted constructor for a class with a |
| 8456 |
non-static data member of array type. Either the initializer will |
| 8457 |
be NULL, meaning default-initialization, or it will be an lvalue |
| 8458 |
or xvalue of the same type, meaning direct-initialization from the |
| 8459 |
corresponding member. */ |
| 8460 |
r = cxx_eval_vec_init (ctx, t, lval, |
| 8461 |
non_constant_p, overflow_p); |
| 8462 |
break; |
| 8463 |
|
| 8464 |
case VEC_PERM_EXPR: |
| 8465 |
r = cxx_eval_trinary_expression (ctx, t, lval, |
| 8466 |
non_constant_p, overflow_p); |
| 8467 |
break; |
| 8468 |
|
| 8469 |
case PAREN_EXPR: |
| 8470 |
gcc_assert (!REF_PARENTHESIZED_P (t)); |
| 8471 |
/* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in |
| 8472 |
constant expressions since it's unaffected by -fassociative-math. */ |
| 8473 |
r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, |
| 8474 |
non_constant_p, overflow_p); |
| 8475 |
break; |
| 8476 |
|
| 8477 |
case NOP_EXPR: |
| 8478 |
if (REINTERPRET_CAST_P (t)) |
| 8479 |
{ |
| 8480 |
if (!ctx->quiet) |
| 8481 |
error_at (loc, |
| 8482 |
"%<reinterpret_cast%> is not a constant expression"); |
| 8483 |
*non_constant_p = true; |
| 8484 |
return t; |
| 8485 |
} |
| 8486 |
/* FALLTHROUGH. */ |
| 8487 |
case CONVERT_EXPR: |
| 8488 |
case VIEW_CONVERT_EXPR: |
| 8489 |
case UNARY_PLUS_EXPR: |
| 8490 |
{ |
| 8491 |
tree oldop = TREE_OPERAND (t, 0); |
| 8492 |
|
| 8493 |
tree op = cxx_eval_constant_expression (ctx, oldop, |
| 8494 |
VOID_TYPE_P (TREE_TYPE (t)) |
| 8495 |
? vc_discard |
| 8496 |
: tcode == VIEW_CONVERT_EXPR |
| 8497 |
? lval : vc_prvalue, |
| 8498 |
non_constant_p, overflow_p); |
| 8499 |
if (*non_constant_p) |
| 8500 |
return t; |
| 8501 |
tree type = TREE_TYPE (t); |
| 8502 |
|
| 8503 |
if (VOID_TYPE_P (type)) |
| 8504 |
return void_node; |
| 8505 |
|
| 8506 |
if (TREE_CODE (t) == CONVERT_EXPR |
| 8507 |
&& ARITHMETIC_TYPE_P (type) |
| 8508 |
&& INDIRECT_TYPE_P (TREE_TYPE (op)) |
| 8509 |
&& ctx->manifestly_const_eval == mce_true) |
| 8510 |
{ |
| 8511 |
if (!ctx->quiet) |
| 8512 |
error_at (loc, |
| 8513 |
"conversion from pointer type %qT to arithmetic type " |
| 8514 |
"%qT in a constant expression", TREE_TYPE (op), type); |
| 8515 |
*non_constant_p = true; |
| 8516 |
return t; |
| 8517 |
} |
| 8518 |
|
| 8519 |
/* [expr.const]: a conversion from type cv void* to a pointer-to-object |
| 8520 |
type cannot be part of a core constant expression as a resolution to |
| 8521 |
DR 1312. */ |
| 8522 |
if (TYPE_PTROB_P (type) |
| 8523 |
&& TYPE_PTR_P (TREE_TYPE (op)) |
| 8524 |
&& VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op))) |
| 8525 |
/* Inside a call to std::construct_at, |
| 8526 |
std::allocator<T>::{,de}allocate, or |
| 8527 |
std::source_location::current, we permit casting from void* |
| 8528 |
because that is compiler-generated code. */ |
| 8529 |
&& !is_std_construct_at (ctx->call) |
| 8530 |
&& !is_std_allocator_allocate (ctx->call) |
| 8531 |
&& !is_std_source_location_current (ctx->call)) |
| 8532 |
{ |
| 8533 |
/* Likewise, don't error when casting from void* when OP is |
| 8534 |
&heap uninit and similar. */ |
| 8535 |
tree sop = tree_strip_nop_conversions (op); |
| 8536 |
tree decl = NULL_TREE; |
| 8537 |
if (TREE_CODE (sop) == ADDR_EXPR) |
| 8538 |
decl = TREE_OPERAND (sop, 0); |
| 8539 |
if (decl |
| 8540 |
&& VAR_P (decl) |
| 8541 |
&& DECL_ARTIFICIAL (decl) |
| 8542 |
&& (DECL_NAME (decl) == heap_identifier |
| 8543 |
|| DECL_NAME (decl) == heap_uninit_identifier |
| 8544 |
|| DECL_NAME (decl) == heap_vec_identifier |
| 8545 |
|| DECL_NAME (decl) == heap_vec_uninit_identifier)) |
| 8546 |
/* OK */; |
| 8547 |
/* P2738 (C++26): a conversion from a prvalue P of type "pointer to |
| 8548 |
cv void" to a pointer-to-object type T unless P is a null |
| 8549 |
pointer value or points to an object whose type is similar to |
| 8550 |
T. */ |
| 8551 |
else if (cxx_dialect > cxx23) |
| 8552 |
{ |
| 8553 |
if (integer_zerop (sop)) |
| 8554 |
return build_int_cst (type, 0); |
| 8555 |
r = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), sop); |
| 8556 |
if (r) |
| 8557 |
{ |
| 8558 |
r = build1 (ADDR_EXPR, type, r); |
| 8559 |
break; |
| 8560 |
} |
| 8561 |
if (!ctx->quiet) |
| 8562 |
{ |
| 8563 |
gcc_assert (TREE_CODE (sop) == ADDR_EXPR); |
| 8564 |
auto_diagnostic_group d; |
| 8565 |
error_at (loc, "cast from %qT is not allowed in a " |
| 8566 |
"constant expression because " |
| 8567 |
"pointed-to type %qT is not similar to %qT", |
| 8568 |
TREE_TYPE (op), TREE_TYPE (TREE_TYPE (sop)), |
| 8569 |
TREE_TYPE (type)); |
| 8570 |
tree obj = build_fold_indirect_ref (sop); |
| 8571 |
if (TREE_CODE (obj) == COMPONENT_REF) |
| 8572 |
obj = TREE_OPERAND (obj, 1); |
| 8573 |
if (DECL_P (obj)) |
| 8574 |
inform (DECL_SOURCE_LOCATION (obj), |
| 8575 |
"pointed-to object declared here"); |
| 8576 |
} |
| 8577 |
*non_constant_p = true; |
| 8578 |
return t; |
| 8579 |
} |
| 8580 |
else |
| 8581 |
{ |
| 8582 |
if (!ctx->quiet) |
| 8583 |
error_at (loc, "cast from %qT is not allowed in a " |
| 8584 |
"constant expression before C++26", |
| 8585 |
TREE_TYPE (op)); |
| 8586 |
*non_constant_p = true; |
| 8587 |
return t; |
| 8588 |
} |
| 8589 |
} |
| 8590 |
|
| 8591 |
if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type)) |
| 8592 |
{ |
| 8593 |
op = cplus_expand_constant (op); |
| 8594 |
if (TREE_CODE (op) == PTRMEM_CST) |
| 8595 |
{ |
| 8596 |
if (!ctx->quiet) |
| 8597 |
error_at (loc, "%qE is not a constant expression when the " |
| 8598 |
"class %qT is still incomplete", op, |
| 8599 |
PTRMEM_CST_CLASS (op)); |
| 8600 |
*non_constant_p = true; |
| 8601 |
return t; |
| 8602 |
} |
| 8603 |
} |
| 8604 |
|
| 8605 |
if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR) |
| 8606 |
{ |
| 8607 |
if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op)) |
| 8608 |
&& !can_convert_qual (type, op)) |
| 8609 |
op = cplus_expand_constant (op); |
| 8610 |
return cp_fold_convert (type, op); |
| 8611 |
} |
| 8612 |
|
| 8613 |
if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST) |
| 8614 |
{ |
| 8615 |
if (integer_zerop (op)) |
| 8616 |
{ |
| 8617 |
if (TYPE_REF_P (type)) |
| 8618 |
{ |
| 8619 |
if (!ctx->quiet) |
| 8620 |
error_at (loc, "dereferencing a null pointer"); |
| 8621 |
*non_constant_p = true; |
| 8622 |
return t; |
| 8623 |
} |
| 8624 |
} |
| 8625 |
else if (TYPE_PTR_P (type) |
| 8626 |
&& TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE) |
| 8627 |
/* INTEGER_CST with pointer-to-method type is only used |
| 8628 |
for a virtual method in a pointer to member function. |
| 8629 |
Don't reject those. */ |
| 8630 |
; |
| 8631 |
else |
| 8632 |
{ |
| 8633 |
/* This detects for example: |
| 8634 |
reinterpret_cast<void*>(sizeof 0) |
| 8635 |
*/ |
| 8636 |
if (!ctx->quiet) |
| 8637 |
error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not " |
| 8638 |
"a constant expression", |
| 8639 |
type, op); |
| 8640 |
*non_constant_p = true; |
| 8641 |
return t; |
| 8642 |
} |
| 8643 |
} |
| 8644 |
|
| 8645 |
if (INDIRECT_TYPE_P (type) |
| 8646 |
&& TREE_CODE (op) == NOP_EXPR |
| 8647 |
&& TREE_TYPE (op) == ptr_type_node |
| 8648 |
&& TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR |
| 8649 |
&& VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0)) |
| 8650 |
&& (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), |
| 8651 |
0)) == heap_uninit_identifier |
| 8652 |
|| DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), |
| 8653 |
0)) == heap_vec_uninit_identifier)) |
| 8654 |
{ |
| 8655 |
tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0); |
| 8656 |
tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var)); |
| 8657 |
tree elt_type = TREE_TYPE (type); |
| 8658 |
tree cookie_size = NULL_TREE; |
| 8659 |
tree arg_size = NULL_TREE; |
| 8660 |
if (TREE_CODE (elt_type) == RECORD_TYPE |
| 8661 |
&& TYPE_NAME (elt_type) == heap_identifier) |
| 8662 |
{ |
| 8663 |
tree fld1 = TYPE_FIELDS (elt_type); |
| 8664 |
tree fld2 = DECL_CHAIN (fld1); |
| 8665 |
elt_type = TREE_TYPE (TREE_TYPE (fld2)); |
| 8666 |
cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1)); |
| 8667 |
} |
| 8668 |
DECL_NAME (var) |
| 8669 |
= (DECL_NAME (var) == heap_uninit_identifier |
| 8670 |
? heap_identifier : heap_vec_identifier); |
| 8671 |
/* For zero sized elt_type, try to recover how many outer_nelts |
| 8672 |
it should have. */ |
| 8673 |
if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size) |
| 8674 |
: integer_zerop (var_size)) |
| 8675 |
&& !int_size_in_bytes (elt_type) |
| 8676 |
&& TREE_CODE (oldop) == CALL_EXPR |
| 8677 |
&& call_expr_nargs (oldop) >= 1) |
| 8678 |
if (tree fun = get_function_named_in_call (oldop)) |
| 8679 |
if (cxx_replaceable_global_alloc_fn (fun) |
| 8680 |
&& IDENTIFIER_NEW_OP_P (DECL_NAME (fun))) |
| 8681 |
arg_size = CALL_EXPR_ARG (oldop, 0); |
| 8682 |
TREE_TYPE (var) |
| 8683 |
= build_new_constexpr_heap_type (ctx, elt_type, cookie_size, |
| 8684 |
var_size, arg_size, |
| 8685 |
non_constant_p, overflow_p); |
| 8686 |
TREE_TYPE (TREE_OPERAND (op, 0)) |
| 8687 |
= build_pointer_type (TREE_TYPE (var)); |
| 8688 |
} |
| 8689 |
|
| 8690 |
if (op == oldop && tcode != UNARY_PLUS_EXPR) |
| 8691 |
/* We didn't fold at the top so we could check for ptr-int |
| 8692 |
conversion. */ |
| 8693 |
return fold (t); |
| 8694 |
|
| 8695 |
tree sop; |
| 8696 |
|
| 8697 |
/* Handle an array's bounds having been deduced after we built |
| 8698 |
the wrapping expression. */ |
| 8699 |
if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op))) |
| 8700 |
r = op; |
| 8701 |
else if (sop = tree_strip_nop_conversions (op), |
| 8702 |
sop != op && (same_type_ignoring_tlq_and_bounds_p |
| 8703 |
(type, TREE_TYPE (sop)))) |
| 8704 |
r = sop; |
| 8705 |
else if (tcode == UNARY_PLUS_EXPR) |
| 8706 |
r = fold_convert (TREE_TYPE (t), op); |
| 8707 |
else |
| 8708 |
r = fold_build1 (tcode, type, op); |
| 8709 |
|
| 8710 |
/* Conversion of an out-of-range value has implementation-defined |
| 8711 |
behavior; the language considers it different from arithmetic |
| 8712 |
overflow, which is undefined. */ |
| 8713 |
if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op)) |
| 8714 |
TREE_OVERFLOW (r) = false; |
| 8715 |
} |
| 8716 |
break; |
| 8717 |
|
| 8718 |
case EXCESS_PRECISION_EXPR: |
| 8719 |
{ |
| 8720 |
tree oldop = TREE_OPERAND (t, 0); |
| 8721 |
|
| 8722 |
tree op = cxx_eval_constant_expression (ctx, oldop, |
| 8723 |
lval, |
| 8724 |
non_constant_p, overflow_p); |
| 8725 |
if (*non_constant_p) |
| 8726 |
return t; |
| 8727 |
r = fold_convert (TREE_TYPE (t), op); |
| 8728 |
break; |
| 8729 |
} |
| 8730 |
|
| 8731 |
case EMPTY_CLASS_EXPR: |
| 8732 |
/* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering |
| 8733 |
it to an appropriate CONSTRUCTOR. */ |
| 8734 |
return build_constructor (TREE_TYPE (t), NULL); |
| 8735 |
|
| 8736 |
case STATEMENT_LIST: |
| 8737 |
new_ctx = *ctx; |
| 8738 |
new_ctx.ctor = new_ctx.object = NULL_TREE; |
| 8739 |
return cxx_eval_statement_list (&new_ctx, t, |
| 8740 |
non_constant_p, overflow_p, jump_target); |
| 8741 |
|
| 8742 |
case BIND_EXPR: |
| 8743 |
/* Pre-emptively clear the vars declared by this BIND_EXPR from the value |
| 8744 |
map, so that when checking whether they're already destroyed later we |
| 8745 |
don't get confused by remnants of previous calls. */ |
| 8746 |
for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl)) |
| 8747 |
ctx->global->clear_value (decl); |
| 8748 |
r = cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t), |
| 8749 |
lval, |
| 8750 |
non_constant_p, overflow_p, |
| 8751 |
jump_target); |
| 8752 |
for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl)) |
| 8753 |
destroy_value_checked (ctx, decl, non_constant_p); |
| 8754 |
break; |
| 8755 |
|
| 8756 |
case PREINCREMENT_EXPR: |
| 8757 |
case POSTINCREMENT_EXPR: |
| 8758 |
case PREDECREMENT_EXPR: |
| 8759 |
case POSTDECREMENT_EXPR: |
| 8760 |
return cxx_eval_increment_expression (ctx, t, |
| 8761 |
lval, non_constant_p, overflow_p); |
| 8762 |
|
| 8763 |
case LAMBDA_EXPR: |
| 8764 |
case NEW_EXPR: |
| 8765 |
case VEC_NEW_EXPR: |
| 8766 |
case DELETE_EXPR: |
| 8767 |
case VEC_DELETE_EXPR: |
| 8768 |
case THROW_EXPR: |
| 8769 |
case MODOP_EXPR: |
| 8770 |
/* GCC internal stuff. */ |
| 8771 |
case VA_ARG_EXPR: |
| 8772 |
case BASELINK: |
| 8773 |
case OFFSET_REF: |
| 8774 |
if (!ctx->quiet) |
| 8775 |
error_at (loc, "expression %qE is not a constant expression", t); |
| 8776 |
*non_constant_p = true; |
| 8777 |
break; |
| 8778 |
|
| 8779 |
case OBJ_TYPE_REF: |
| 8780 |
/* Virtual function lookup. We don't need to do anything fancy. */ |
| 8781 |
return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t), |
| 8782 |
lval, non_constant_p, overflow_p); |
| 8783 |
|
| 8784 |
case PLACEHOLDER_EXPR: |
| 8785 |
/* Use of the value or address of the current object. */ |
| 8786 |
if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t))) |
| 8787 |
{ |
| 8788 |
if (TREE_CODE (ctor) == CONSTRUCTOR) |
| 8789 |
return ctor; |
| 8790 |
else |
| 8791 |
return cxx_eval_constant_expression (ctx, ctor, lval, |
| 8792 |
non_constant_p, overflow_p); |
| 8793 |
} |
| 8794 |
/* A placeholder without a referent. We can get here when |
| 8795 |
checking whether NSDMIs are noexcept, or in massage_init_elt; |
| 8796 |
just say it's non-constant for now. */ |
| 8797 |
gcc_assert (ctx->quiet); |
| 8798 |
*non_constant_p = true; |
| 8799 |
break; |
| 8800 |
|
| 8801 |
case EXIT_EXPR: |
| 8802 |
{ |
| 8803 |
tree cond = TREE_OPERAND (t, 0); |
| 8804 |
cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue, |
| 8805 |
non_constant_p, overflow_p); |
| 8806 |
VERIFY_CONSTANT (cond); |
| 8807 |
if (integer_nonzerop (cond)) |
| 8808 |
*jump_target = t; |
| 8809 |
} |
| 8810 |
break; |
| 8811 |
|
| 8812 |
case GOTO_EXPR: |
| 8813 |
if (breaks (&TREE_OPERAND (t, 0)) |
| 8814 |
|| continues (&TREE_OPERAND (t, 0))) |
| 8815 |
*jump_target = TREE_OPERAND (t, 0); |
| 8816 |
else |
| 8817 |
{ |
| 8818 |
if (!ctx->quiet) |
| 8819 |
error_at (loc, "%<goto%> is not a constant expression"); |
| 8820 |
*non_constant_p = true; |
| 8821 |
} |
| 8822 |
break; |
| 8823 |
|
| 8824 |
case LOOP_EXPR: |
| 8825 |
case DO_STMT: |
| 8826 |
case WHILE_STMT: |
| 8827 |
case FOR_STMT: |
| 8828 |
cxx_eval_loop_expr (ctx, t, |
| 8829 |
non_constant_p, overflow_p, jump_target); |
| 8830 |
break; |
| 8831 |
|
| 8832 |
case SWITCH_EXPR: |
| 8833 |
case SWITCH_STMT: |
| 8834 |
cxx_eval_switch_expr (ctx, t, |
| 8835 |
non_constant_p, overflow_p, jump_target); |
| 8836 |
break; |
| 8837 |
|
| 8838 |
case REQUIRES_EXPR: |
| 8839 |
/* It's possible to get a requires-expression in a constant |
| 8840 |
expression. For example: |
| 8841 |
|
| 8842 |
template<typename T> concept bool C() { |
| 8843 |
return requires (T t) { t; }; |
| 8844 |
} |
| 8845 |
|
| 8846 |
template<typename T> requires !C<T>() void f(T); |
| 8847 |
|
| 8848 |
Normalization leaves f with the associated constraint |
| 8849 |
'!requires (T t) { ... }' which is not transformed into |
| 8850 |
a constraint. */ |
| 8851 |
if (!processing_template_decl) |
| 8852 |
return evaluate_requires_expr (t); |
| 8853 |
else |
| 8854 |
*non_constant_p = true; |
| 8855 |
return t; |
| 8856 |
|
| 8857 |
case ANNOTATE_EXPR: |
| 8858 |
r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), |
| 8859 |
lval, |
| 8860 |
non_constant_p, overflow_p, |
| 8861 |
jump_target); |
| 8862 |
break; |
| 8863 |
|
| 8864 |
case USING_STMT: |
| 8865 |
r = void_node; |
| 8866 |
break; |
| 8867 |
|
| 8868 |
case ASSERTION_STMT: |
| 8869 |
case PRECONDITION_STMT: |
| 8870 |
case POSTCONDITION_STMT: |
| 8871 |
{ |
| 8872 |
contract_semantic semantic = get_contract_semantic (t); |
| 8873 |
if (semantic == CCS_IGNORE) |
| 8874 |
break; |
| 8875 |
|
| 8876 |
if (!cxx_eval_assert (ctx, CONTRACT_CONDITION (t), |
| 8877 |
G_("contract predicate is false in " |
| 8878 |
"constant expression"), |
| 8879 |
EXPR_LOCATION (t), checked_contract_p (semantic), |
| 8880 |
non_constant_p, overflow_p)) |
| 8881 |
*non_constant_p = true; |
| 8882 |
r = void_node; |
| 8883 |
} |
| 8884 |
break; |
| 8885 |
|
| 8886 |
case TEMPLATE_ID_EXPR: |
| 8887 |
{ |
| 8888 |
/* We can evaluate template-id that refers to a concept only if |
| 8889 |
the template arguments are non-dependent. */ |
| 8890 |
gcc_assert (concept_check_p (t)); |
| 8891 |
|
| 8892 |
if (!value_dependent_expression_p (t) |
| 8893 |
&& !uid_sensitive_constexpr_evaluation_p ()) |
| 8894 |
r = evaluate_concept_check (t); |
| 8895 |
else |
| 8896 |
*non_constant_p = true; |
| 8897 |
|
| 8898 |
break; |
| 8899 |
} |
| 8900 |
|
| 8901 |
case ASM_EXPR: |
| 8902 |
if (!ctx->quiet) |
| 8903 |
inline_asm_in_constexpr_error (loc, /*constexpr_fundef_p*/false); |
| 8904 |
*non_constant_p = true; |
| 8905 |
return t; |
| 8906 |
|
| 8907 |
case BIT_CAST_EXPR: |
| 8908 |
if (lval) |
| 8909 |
{ |
| 8910 |
if (!ctx->quiet) |
| 8911 |
error_at (EXPR_LOCATION (t), |
| 8912 |
"address of a call to %qs is not a constant expression", |
| 8913 |
"__builtin_bit_cast"); |
| 8914 |
*non_constant_p = true; |
| 8915 |
return t; |
| 8916 |
} |
| 8917 |
r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p); |
| 8918 |
break; |
| 8919 |
|
| 8920 |
case OMP_PARALLEL: |
| 8921 |
case OMP_TASK: |
| 8922 |
case OMP_FOR: |
| 8923 |
case OMP_SIMD: |
| 8924 |
case OMP_DISTRIBUTE: |
| 8925 |
case OMP_TASKLOOP: |
| 8926 |
case OMP_LOOP: |
| 8927 |
case OMP_TEAMS: |
| 8928 |
case OMP_TARGET_DATA: |
| 8929 |
case OMP_TARGET: |
| 8930 |
case OMP_SECTIONS: |
| 8931 |
case OMP_ORDERED: |
| 8932 |
case OMP_CRITICAL: |
| 8933 |
case OMP_SINGLE: |
| 8934 |
case OMP_SCAN: |
| 8935 |
case OMP_SCOPE: |
| 8936 |
case OMP_SECTION: |
| 8937 |
case OMP_STRUCTURED_BLOCK: |
| 8938 |
case OMP_MASTER: |
| 8939 |
case OMP_MASKED: |
| 8940 |
case OMP_TASKGROUP: |
| 8941 |
case OMP_TARGET_UPDATE: |
| 8942 |
case OMP_TARGET_ENTER_DATA: |
| 8943 |
case OMP_TARGET_EXIT_DATA: |
| 8944 |
case OMP_ATOMIC: |
| 8945 |
case OMP_ATOMIC_READ: |
| 8946 |
case OMP_ATOMIC_CAPTURE_OLD: |
| 8947 |
case OMP_ATOMIC_CAPTURE_NEW: |
| 8948 |
case OMP_DEPOBJ: |
| 8949 |
case OACC_PARALLEL: |
| 8950 |
case OACC_KERNELS: |
| 8951 |
case OACC_SERIAL: |
| 8952 |
case OACC_DATA: |
| 8953 |
case OACC_HOST_DATA: |
| 8954 |
case OACC_LOOP: |
| 8955 |
case OACC_CACHE: |
| 8956 |
case OACC_DECLARE: |
| 8957 |
case OACC_ENTER_DATA: |
| 8958 |
case OACC_EXIT_DATA: |
| 8959 |
case OACC_UPDATE: |
| 8960 |
if (!ctx->quiet) |
| 8961 |
error_at (EXPR_LOCATION (t), |
| 8962 |
"statement is not a constant expression"); |
| 8963 |
*non_constant_p = true; |
| 8964 |
break; |
| 8965 |
|
| 8966 |
default: |
| 8967 |
if (STATEMENT_CODE_P (TREE_CODE (t))) |
| 8968 |
{ |
| 8969 |
/* This function doesn't know how to deal with pre-genericize |
| 8970 |
statements; this can only happen with statement-expressions, |
| 8971 |
so for now just fail. */ |
| 8972 |
if (!ctx->quiet) |
| 8973 |
error_at (EXPR_LOCATION (t), |
| 8974 |
"statement is not a constant expression"); |
| 8975 |
} |
| 8976 |
else if (flag_checking) |
| 8977 |
internal_error ("unexpected expression %qE of kind %s", t, |
| 8978 |
get_tree_code_name (TREE_CODE (t))); |
| 8979 |
*non_constant_p = true; |
| 8980 |
break; |
| 8981 |
} |
| 8982 |
|
| 8983 |
if (r == error_mark_node) |
| 8984 |
*non_constant_p = true; |
| 8985 |
|
| 8986 |
if (*non_constant_p) |
| 8987 |
return t; |
| 8988 |
else |
| 8989 |
return r; |
| 8990 |
} |
| 8991 |
|
| 8992 |
/* P0859: A function is needed for constant evaluation if it is a constexpr |
| 8993 |
function that is named by an expression ([basic.def.odr]) that is |
| 8994 |
potentially constant evaluated. |
| 8995 |
|
| 8996 |
So we need to instantiate any constexpr functions mentioned by the |
| 8997 |
expression even if the definition isn't needed for evaluating the |
| 8998 |
expression. */ |
| 8999 |
|
| 9000 |
static tree |
| 9001 |
instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/) |
| 9002 |
{ |
| 9003 |
if (TREE_CODE (*tp) == FUNCTION_DECL |
| 9004 |
&& DECL_DECLARED_CONSTEXPR_P (*tp) |
| 9005 |
&& !DECL_INITIAL (*tp) |
| 9006 |
&& !trivial_fn_p (*tp) |
| 9007 |
&& (DECL_TEMPLOID_INSTANTIATION (*tp) || DECL_DEFAULTED_FN (*tp)) |
| 9008 |
&& !uid_sensitive_constexpr_evaluation_p ()) |
| 9009 |
{ |
| 9010 |
++function_depth; |
| 9011 |
if (DECL_TEMPLOID_INSTANTIATION (*tp)) |
| 9012 |
instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false); |
| 9013 |
else |
| 9014 |
synthesize_method (*tp); |
| 9015 |
--function_depth; |
| 9016 |
} |
| 9017 |
else if (TREE_CODE (*tp) == CALL_EXPR |
| 9018 |
|| TREE_CODE (*tp) == AGGR_INIT_EXPR) |
| 9019 |
{ |
| 9020 |
if (EXPR_HAS_LOCATION (*tp)) |
| 9021 |
input_location = EXPR_LOCATION (*tp); |
| 9022 |
} |
| 9023 |
|
| 9024 |
if (!EXPR_P (*tp)) |
| 9025 |
*walk_subtrees = 0; |
| 9026 |
|
| 9027 |
return NULL_TREE; |
| 9028 |
} |
| 9029 |
|
| 9030 |
static void |
| 9031 |
instantiate_constexpr_fns (tree t) |
| 9032 |
{ |
| 9033 |
location_t loc = input_location; |
| 9034 |
cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL); |
| 9035 |
input_location = loc; |
| 9036 |
} |
| 9037 |
|
| 9038 |
/* Look for heap variables in the expression *TP. */ |
| 9039 |
|
| 9040 |
static tree |
| 9041 |
find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/) |
| 9042 |
{ |
| 9043 |
if (VAR_P (*tp) |
| 9044 |
&& (DECL_NAME (*tp) == heap_uninit_identifier |
| 9045 |
|| DECL_NAME (*tp) == heap_identifier |
| 9046 |
|| DECL_NAME (*tp) == heap_vec_uninit_identifier |
| 9047 |
|| DECL_NAME (*tp) == heap_vec_identifier |
| 9048 |
|| DECL_NAME (*tp) == heap_deleted_identifier)) |
| 9049 |
return *tp; |
| 9050 |
|
| 9051 |
if (TYPE_P (*tp)) |
| 9052 |
*walk_subtrees = 0; |
| 9053 |
return NULL_TREE; |
| 9054 |
} |
| 9055 |
|
| 9056 |
/* Look for deleted heap variables in the expression *TP. */ |
| 9057 |
|
| 9058 |
static tree |
| 9059 |
find_deleted_heap_var (tree *tp, int *walk_subtrees, void */*data*/) |
| 9060 |
{ |
| 9061 |
if (VAR_P (*tp) |
| 9062 |
&& DECL_NAME (*tp) == heap_deleted_identifier) |
| 9063 |
return *tp; |
| 9064 |
|
| 9065 |
if (TYPE_P (*tp)) |
| 9066 |
*walk_subtrees = 0; |
| 9067 |
return NULL_TREE; |
| 9068 |
} |
| 9069 |
|
| 9070 |
/* Find immediate function decls in *TP if any. */ |
| 9071 |
|
| 9072 |
static tree |
| 9073 |
find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/) |
| 9074 |
{ |
| 9075 |
if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp)) |
| 9076 |
return *tp; |
| 9077 |
if (TREE_CODE (*tp) == PTRMEM_CST |
| 9078 |
&& TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL |
| 9079 |
&& DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp))) |
| 9080 |
return PTRMEM_CST_MEMBER (*tp); |
| 9081 |
return NULL_TREE; |
| 9082 |
} |
| 9083 |
|
| 9084 |
/* T has TREE_CONSTANT set but has been deemed not a valid C++ constant |
| 9085 |
expression. Return a version of T that has TREE_CONSTANT cleared. */ |
| 9086 |
|
| 9087 |
static tree |
| 9088 |
mark_non_constant (tree t) |
| 9089 |
{ |
| 9090 |
gcc_checking_assert (TREE_CONSTANT (t)); |
| 9091 |
|
| 9092 |
/* This isn't actually constant, so unset TREE_CONSTANT. |
| 9093 |
Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires |
| 9094 |
it to be set if it is invariant address, even when it is not |
| 9095 |
a valid C++ constant expression. Wrap it with a NOP_EXPR |
| 9096 |
instead. */ |
| 9097 |
if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR) |
| 9098 |
t = copy_node (t); |
| 9099 |
else if (TREE_CODE (t) == CONSTRUCTOR) |
| 9100 |
t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t); |
| 9101 |
else |
| 9102 |
t = build_nop (TREE_TYPE (t), t); |
| 9103 |
TREE_CONSTANT (t) = false; |
| 9104 |
return t; |
| 9105 |
} |
| 9106 |
|
| 9107 |
/* ALLOW_NON_CONSTANT is false if T is required to be a constant expression. |
| 9108 |
STRICT has the same sense as for constant_value_1: true if we only allow |
| 9109 |
conforming C++ constant expressions, or false if we want a constant value |
| 9110 |
even if it doesn't conform. |
| 9111 |
MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as |
| 9112 |
per P0595 even when ALLOW_NON_CONSTANT is true. |
| 9113 |
CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable. |
| 9114 |
OBJECT must be non-NULL in that case. */ |
| 9115 |
|
| 9116 |
static tree |
| 9117 |
cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant, |
| 9118 |
bool strict = true, |
| 9119 |
mce_value manifestly_const_eval = mce_unknown, |
| 9120 |
bool constexpr_dtor = false, |
| 9121 |
tree object = NULL_TREE) |
| 9122 |
{ |
| 9123 |
auto_timevar time (TV_CONSTEXPR); |
| 9124 |
|
| 9125 |
bool non_constant_p = false; |
| 9126 |
bool overflow_p = false; |
| 9127 |
|
| 9128 |
if (BRACE_ENCLOSED_INITIALIZER_P (t)) |
| 9129 |
{ |
| 9130 |
gcc_checking_assert (allow_non_constant); |
| 9131 |
return t; |
| 9132 |
} |
| 9133 |
|
| 9134 |
constexpr_global_ctx global_ctx; |
| 9135 |
constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL, |
| 9136 |
allow_non_constant, strict, |
| 9137 |
!allow_non_constant ? mce_true : manifestly_const_eval }; |
| 9138 |
|
| 9139 |
/* Turn off -frounding-math for manifestly constant evaluation. */ |
| 9140 |
warning_sentinel rm (flag_rounding_math, |
| 9141 |
ctx.manifestly_const_eval == mce_true); |
| 9142 |
tree type = (object |
| 9143 |
? cv_unqualified (TREE_TYPE (object)) |
| 9144 |
: initialized_type (t)); |
| 9145 |
tree r = t; |
| 9146 |
bool is_consteval = false; |
| 9147 |
if (VOID_TYPE_P (type)) |
| 9148 |
{ |
| 9149 |
if (!constexpr_dtor) |
| 9150 |
{ |
| 9151 |
if (cxx_dialect < cxx20) |
| 9152 |
return t; |
| 9153 |
if (TREE_CODE (t) != CALL_EXPR && TREE_CODE (t) != AGGR_INIT_EXPR) |
| 9154 |
return t; |
| 9155 |
/* Calls to immediate functions returning void need to be |
| 9156 |
evaluated. */ |
| 9157 |
tree fndecl = cp_get_callee_fndecl_nofold (t); |
| 9158 |
if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl)) |
| 9159 |
return t; |
| 9160 |
else |
| 9161 |
is_consteval = true; |
| 9162 |
} |
| 9163 |
} |
| 9164 |
else if (cxx_dialect >= cxx20 |
| 9165 |
&& (TREE_CODE (t) == CALL_EXPR |
| 9166 |
|| TREE_CODE (t) == AGGR_INIT_EXPR |
| 9167 |
|| TREE_CODE (t) == TARGET_EXPR)) |
| 9168 |
{ |
| 9169 |
tree x = t; |
| 9170 |
if (TREE_CODE (x) == TARGET_EXPR) |
| 9171 |
x = TARGET_EXPR_INITIAL (x); |
| 9172 |
tree fndecl = cp_get_callee_fndecl_nofold (x); |
| 9173 |
if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl)) |
| 9174 |
is_consteval = true; |
| 9175 |
/* Don't try to evaluate a std::vector constructor taking an integer, it |
| 9176 |
will fail in the 'if (heap_var)' block below after doing all the work |
| 9177 |
(c++/113835). This will need adjustment if P3554 is accepted. Note |
| 9178 |
that evaluation of e.g. the vector default constructor can succeed, so |
| 9179 |
we don't shortcut all vector constructors. */ |
| 9180 |
if (fndecl && DECL_CONSTRUCTOR_P (fndecl) && allow_non_constant |
| 9181 |
&& is_std_class (type, "vector") && call_expr_nargs (x) > 1 |
| 9182 |
&& TREE_CODE (TREE_TYPE (get_nth_callarg (x, 1))) == INTEGER_TYPE) |
| 9183 |
return t; |
| 9184 |
} |
| 9185 |
if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)) |
| 9186 |
{ |
| 9187 |
/* In C++14 an NSDMI can participate in aggregate initialization, |
| 9188 |
and can refer to the address of the object being initialized, so |
| 9189 |
we need to pass in the relevant VAR_DECL if we want to do the |
| 9190 |
evaluation in a single pass. The evaluation will dynamically |
| 9191 |
update ctx.values for the VAR_DECL. We use the same strategy |
| 9192 |
for C++11 constexpr constructors that refer to the object being |
| 9193 |
initialized. */ |
| 9194 |
if (constexpr_dtor) |
| 9195 |
{ |
| 9196 |
gcc_assert (object && VAR_P (object)); |
| 9197 |
gcc_assert (DECL_DECLARED_CONSTEXPR_P (object)); |
| 9198 |
gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object)); |
| 9199 |
if (error_operand_p (DECL_INITIAL (object))) |
| 9200 |
return t; |
| 9201 |
ctx.ctor = unshare_expr (DECL_INITIAL (object)); |
| 9202 |
TREE_READONLY (ctx.ctor) = false; |
| 9203 |
/* Temporarily force decl_really_constant_value to return false |
| 9204 |
for it, we want to use ctx.ctor for the current value instead. */ |
| 9205 |
DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false; |
| 9206 |
} |
| 9207 |
else |
| 9208 |
{ |
| 9209 |
ctx.ctor = build_constructor (type, NULL); |
| 9210 |
CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true; |
| 9211 |
} |
| 9212 |
if (!object) |
| 9213 |
{ |
| 9214 |
if (TREE_CODE (t) == CALL_EXPR) |
| 9215 |
{ |
| 9216 |
/* If T is calling a constructor to initialize an object, reframe |
| 9217 |
it as an AGGR_INIT_EXPR to avoid trying to modify an object |
| 9218 |
from outside the constant evaluation, which will fail even if |
| 9219 |
the value is actually constant (is_constant_evaluated3.C). */ |
| 9220 |
tree fn = cp_get_callee_fndecl_nofold (t); |
| 9221 |
if (fn && DECL_CONSTRUCTOR_P (fn)) |
| 9222 |
{ |
| 9223 |
object = CALL_EXPR_ARG (t, 0); |
| 9224 |
object = build_fold_indirect_ref (object); |
| 9225 |
r = build_aggr_init_expr (type, r); |
| 9226 |
} |
| 9227 |
} |
| 9228 |
else if (TREE_CODE (t) == TARGET_EXPR) |
| 9229 |
object = TARGET_EXPR_SLOT (t); |
| 9230 |
else if (TREE_CODE (t) == AGGR_INIT_EXPR) |
| 9231 |
object = AGGR_INIT_EXPR_SLOT (t); |
| 9232 |
} |
| 9233 |
ctx.object = object; |
| 9234 |
if (object) |
| 9235 |
gcc_assert (same_type_ignoring_top_level_qualifiers_p |
| 9236 |
(type, TREE_TYPE (object))); |
| 9237 |
if (object && DECL_P (object)) |
| 9238 |
global_ctx.put_value (object, ctx.ctor); |
| 9239 |
if (TREE_CODE (r) == TARGET_EXPR) |
| 9240 |
/* Avoid creating another CONSTRUCTOR when we expand the |
| 9241 |
TARGET_EXPR. */ |
| 9242 |
r = TARGET_EXPR_INITIAL (r); |
| 9243 |
} |
| 9244 |
|
| 9245 |
/* uid_sensitive_constexpr_evaluation_value restricts warning-dependent |
| 9246 |
constexpr evaluation to avoid unnecessary template instantiation, and is |
| 9247 |
always done with mce_unknown. But due to gaps in the restriction logic |
| 9248 |
we may still end up taking an evaluation path that in turn requires |
| 9249 |
manifestly constant evaluation, and such evaluation must not be |
| 9250 |
restricted since it likely has semantic consequences. |
| 9251 |
TODO: Remove/replace the mechanism in GCC 17. */ |
| 9252 |
auto uids = make_temp_override (uid_sensitive_constexpr_evaluation_value); |
| 9253 |
if (ctx.manifestly_const_eval == mce_true) |
| 9254 |
uid_sensitive_constexpr_evaluation_value = false; |
| 9255 |
|
| 9256 |
auto_vec<tree, 16> cleanups; |
| 9257 |
global_ctx.cleanups = &cleanups; |
| 9258 |
|
| 9259 |
if (manifestly_const_eval == mce_true) |
| 9260 |
instantiate_constexpr_fns (r); |
| 9261 |
r = cxx_eval_constant_expression (&ctx, r, vc_prvalue, |
| 9262 |
&non_constant_p, &overflow_p); |
| 9263 |
|
| 9264 |
/* If we got a non-simple TARGET_EXPR, the initializer was a sequence |
| 9265 |
of statements, and the result ought to be stored in ctx.ctor. */ |
| 9266 |
if (r == void_node && !constexpr_dtor && ctx.ctor) |
| 9267 |
r = ctx.ctor; |
| 9268 |
|
| 9269 |
if (!constexpr_dtor) |
| 9270 |
verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p); |
| 9271 |
else |
| 9272 |
DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true; |
| 9273 |
|
| 9274 |
unsigned int i; |
| 9275 |
tree cleanup; |
| 9276 |
/* Evaluate the cleanups. */ |
| 9277 |
FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup) |
| 9278 |
cxx_eval_constant_expression (&ctx, cleanup, vc_discard, |
| 9279 |
&non_constant_p, &overflow_p); |
| 9280 |
|
| 9281 |
/* Mutable logic is a bit tricky: we want to allow initialization of |
| 9282 |
constexpr variables with mutable members, but we can't copy those |
| 9283 |
members to another constexpr variable. */ |
| 9284 |
if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_MUTABLE_POISON (r)) |
| 9285 |
{ |
| 9286 |
if (!allow_non_constant) |
| 9287 |
error ("%qE is not a constant expression because it refers to " |
| 9288 |
"mutable subobjects of %qT", t, type); |
| 9289 |
non_constant_p = true; |
| 9290 |
} |
| 9291 |
|
| 9292 |
if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r)) |
| 9293 |
{ |
| 9294 |
if (!allow_non_constant) |
| 9295 |
error ("%qE is not a constant expression because it refers to " |
| 9296 |
"an incompletely initialized variable", t); |
| 9297 |
TREE_CONSTANT (r) = false; |
| 9298 |
non_constant_p = true; |
| 9299 |
} |
| 9300 |
|
| 9301 |
if (!non_constant_p && cxx_dialect >= cxx20 |
| 9302 |
&& !global_ctx.heap_vars.is_empty ()) |
| 9303 |
{ |
| 9304 |
tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs, |
| 9305 |
NULL); |
| 9306 |
unsigned int i; |
| 9307 |
if (heap_var) |
| 9308 |
{ |
| 9309 |
if (!allow_non_constant && !non_constant_p) |
| 9310 |
error_at (DECL_SOURCE_LOCATION (heap_var), |
| 9311 |
"%qE is not a constant expression because it refers to " |
| 9312 |
"a result of %<operator new%>", t); |
| 9313 |
r = t; |
| 9314 |
non_constant_p = true; |
| 9315 |
} |
| 9316 |
FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var) |
| 9317 |
{ |
| 9318 |
if (DECL_NAME (heap_var) != heap_deleted_identifier) |
| 9319 |
{ |
| 9320 |
if (!allow_non_constant && !non_constant_p) |
| 9321 |
error_at (DECL_SOURCE_LOCATION (heap_var), |
| 9322 |
"%qE is not a constant expression because allocated " |
| 9323 |
"storage has not been deallocated", t); |
| 9324 |
r = t; |
| 9325 |
non_constant_p = true; |
| 9326 |
} |
| 9327 |
varpool_node::get (heap_var)->remove (); |
| 9328 |
} |
| 9329 |
} |
| 9330 |
|
| 9331 |
/* Check that immediate invocation does not return an expression referencing |
| 9332 |
any immediate function decls. */ |
| 9333 |
if (!non_constant_p && cxx_dialect >= cxx20) |
| 9334 |
if (tree immediate_fndecl |
| 9335 |
= cp_walk_tree_without_duplicates (&r, find_immediate_fndecl, |
| 9336 |
NULL)) |
| 9337 |
{ |
| 9338 |
if (!allow_non_constant && !non_constant_p) |
| 9339 |
{ |
| 9340 |
if (is_consteval) |
| 9341 |
error_at (cp_expr_loc_or_input_loc (t), |
| 9342 |
"immediate evaluation returns address of immediate " |
| 9343 |
"function %qD", immediate_fndecl); |
| 9344 |
else |
| 9345 |
error_at (cp_expr_loc_or_input_loc (t), |
| 9346 |
"constant evaluation returns address of immediate " |
| 9347 |
"function %qD", immediate_fndecl); |
| 9348 |
} |
| 9349 |
r = t; |
| 9350 |
non_constant_p = true; |
| 9351 |
} |
| 9352 |
|
| 9353 |
if (non_constant_p) |
| 9354 |
/* If we saw something bad, go back to our argument. The wrapping below is |
| 9355 |
only for the cases of TREE_CONSTANT argument or overflow. */ |
| 9356 |
r = t; |
| 9357 |
|
| 9358 |
if (!non_constant_p && overflow_p) |
| 9359 |
non_constant_p = true; |
| 9360 |
|
| 9361 |
/* Unshare the result. */ |
| 9362 |
bool should_unshare = true; |
| 9363 |
if (r == t || (TREE_CODE (t) == TARGET_EXPR |
| 9364 |
&& TARGET_EXPR_INITIAL (t) == r)) |
| 9365 |
should_unshare = false; |
| 9366 |
|
| 9367 |
if (non_constant_p && !allow_non_constant) |
| 9368 |
return error_mark_node; |
| 9369 |
else if (constexpr_dtor) |
| 9370 |
return r; |
| 9371 |
else if (non_constant_p && TREE_CONSTANT (r)) |
| 9372 |
r = mark_non_constant (r); |
| 9373 |
else if (non_constant_p) |
| 9374 |
return t; |
| 9375 |
|
| 9376 |
/* Check we are not trying to return the wrong type. */ |
| 9377 |
if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (r))) |
| 9378 |
{ |
| 9379 |
/* If so, this is not a constant expression. */ |
| 9380 |
if (!allow_non_constant) |
| 9381 |
error ("%qE is not a constant expression because it initializes " |
| 9382 |
"a %qT rather than %qT", t, TREE_TYPE (t), type); |
| 9383 |
return t; |
| 9384 |
} |
| 9385 |
|
| 9386 |
if (should_unshare) |
| 9387 |
r = unshare_expr (r); |
| 9388 |
|
| 9389 |
if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r))) |
| 9390 |
{ |
| 9391 |
r = adjust_temp_type (type, r); |
| 9392 |
if (TREE_CODE (t) == TARGET_EXPR |
| 9393 |
&& TARGET_EXPR_INITIAL (t) == r) |
| 9394 |
return t; |
| 9395 |
else if (TREE_CODE (t) == CONSTRUCTOR || TREE_CODE (t) == CALL_EXPR |
| 9396 |
|| TREE_CODE (t) == AGGR_INIT_EXPR) |
| 9397 |
/* Don't add a TARGET_EXPR if our argument didn't have one. */; |
| 9398 |
else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t)) |
| 9399 |
r = get_target_expr (r); |
| 9400 |
else |
| 9401 |
{ |
| 9402 |
r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup); |
| 9403 |
TREE_CONSTANT (r) = true; |
| 9404 |
} |
| 9405 |
} |
| 9406 |
|
| 9407 |
if (TREE_CODE (t) == TARGET_EXPR |
| 9408 |
&& TREE_CODE (r) == TARGET_EXPR) |
| 9409 |
{ |
| 9410 |
/* Preserve this flag for potential_constant_expression, and the others |
| 9411 |
for good measure. */ |
| 9412 |
TARGET_EXPR_ELIDING_P (r) = TARGET_EXPR_ELIDING_P (t); |
| 9413 |
TARGET_EXPR_IMPLICIT_P (r) = TARGET_EXPR_IMPLICIT_P (t); |
| 9414 |
TARGET_EXPR_LIST_INIT_P (r) = TARGET_EXPR_LIST_INIT_P (t); |
| 9415 |
TARGET_EXPR_DIRECT_INIT_P (r) = TARGET_EXPR_DIRECT_INIT_P (t); |
| 9416 |
} |
| 9417 |
|
| 9418 |
/* Remember the original location if that wouldn't need a wrapper. */ |
| 9419 |
if (location_t loc = EXPR_LOCATION (t)) |
| 9420 |
protected_set_expr_location (r, loc); |
| 9421 |
|
| 9422 |
return r; |
| 9423 |
} |
| 9424 |
|
| 9425 |
/* If T represents a constant expression returns its reduced value. |
| 9426 |
Otherwise return error_mark_node. */ |
| 9427 |
|
| 9428 |
tree |
| 9429 |
cxx_constant_value (tree t, tree decl /* = NULL_TREE */, |
| 9430 |
tsubst_flags_t complain /* = tf_error */) |
| 9431 |
{ |
| 9432 |
bool sfinae = !(complain & tf_error); |
| 9433 |
tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, mce_true, false, decl); |
| 9434 |
if (sfinae && !TREE_CONSTANT (r)) |
| 9435 |
r = error_mark_node; |
| 9436 |
return r; |
| 9437 |
} |
| 9438 |
|
| 9439 |
/* Like cxx_constant_value, but used for evaluation of constexpr destructors |
| 9440 |
of constexpr variables. The actual initializer of DECL is not modified. */ |
| 9441 |
|
| 9442 |
void |
| 9443 |
cxx_constant_dtor (tree t, tree decl) |
| 9444 |
{ |
| 9445 |
cxx_eval_outermost_constant_expr (t, false, true, mce_true, true, decl); |
| 9446 |
} |
| 9447 |
|
| 9448 |
/* Helper routine for fold_simple function. Either return simplified |
| 9449 |
expression T, otherwise NULL_TREE. |
| 9450 |
In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold |
| 9451 |
even if we are within template-declaration. So be careful on call, as in |
| 9452 |
such case types can be undefined. */ |
| 9453 |
|
| 9454 |
static tree |
| 9455 |
fold_simple_1 (tree t) |
| 9456 |
{ |
| 9457 |
tree op1; |
| 9458 |
enum tree_code code = TREE_CODE (t); |
| 9459 |
|
| 9460 |
switch (code) |
| 9461 |
{ |
| 9462 |
case INTEGER_CST: |
| 9463 |
case REAL_CST: |
| 9464 |
case VECTOR_CST: |
| 9465 |
case FIXED_CST: |
| 9466 |
case COMPLEX_CST: |
| 9467 |
return t; |
| 9468 |
|
| 9469 |
case SIZEOF_EXPR: |
| 9470 |
return fold_sizeof_expr (t); |
| 9471 |
|
| 9472 |
case ABS_EXPR: |
| 9473 |
case ABSU_EXPR: |
| 9474 |
case CONJ_EXPR: |
| 9475 |
case REALPART_EXPR: |
| 9476 |
case IMAGPART_EXPR: |
| 9477 |
case NEGATE_EXPR: |
| 9478 |
case BIT_NOT_EXPR: |
| 9479 |
case TRUTH_NOT_EXPR: |
| 9480 |
case VIEW_CONVERT_EXPR: |
| 9481 |
CASE_CONVERT: |
| 9482 |
case FLOAT_EXPR: |
| 9483 |
case FIX_TRUNC_EXPR: |
| 9484 |
case FIXED_CONVERT_EXPR: |
| 9485 |
case ADDR_SPACE_CONVERT_EXPR: |
| 9486 |
|
| 9487 |
op1 = TREE_OPERAND (t, 0); |
| 9488 |
|
| 9489 |
t = const_unop (code, TREE_TYPE (t), op1); |
| 9490 |
if (!t) |
| 9491 |
return NULL_TREE; |
| 9492 |
|
| 9493 |
if (CONVERT_EXPR_CODE_P (code) |
| 9494 |
&& TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1)) |
| 9495 |
TREE_OVERFLOW (t) = false; |
| 9496 |
return t; |
| 9497 |
|
| 9498 |
default: |
| 9499 |
return NULL_TREE; |
| 9500 |
} |
| 9501 |
} |
| 9502 |
|
| 9503 |
/* If T is a simple constant expression, returns its simplified value. |
| 9504 |
Otherwise returns T. In contrast to maybe_constant_value we |
| 9505 |
simplify only few operations on constant-expressions, and we don't |
| 9506 |
try to simplify constexpressions. */ |
| 9507 |
|
| 9508 |
tree |
| 9509 |
fold_simple (tree t) |
| 9510 |
{ |
| 9511 |
if (processing_template_decl) |
| 9512 |
return t; |
| 9513 |
|
| 9514 |
tree r = fold_simple_1 (t); |
| 9515 |
if (r) |
| 9516 |
return r; |
| 9517 |
|
| 9518 |
return t; |
| 9519 |
} |
| 9520 |
|
| 9521 |
/* Try folding the expression T to a simple constant. |
| 9522 |
Returns that constant, otherwise returns T. */ |
| 9523 |
|
| 9524 |
tree |
| 9525 |
fold_to_constant (tree t) |
| 9526 |
{ |
| 9527 |
tree r = fold (t); |
| 9528 |
if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r)) |
| 9529 |
return r; |
| 9530 |
else |
| 9531 |
return t; |
| 9532 |
} |
| 9533 |
|
| 9534 |
/* If T is a constant expression, returns its reduced value. |
| 9535 |
Otherwise, if T does not have TREE_CONSTANT set, returns T. |
| 9536 |
Otherwise, returns a version of T without TREE_CONSTANT. |
| 9537 |
MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated |
| 9538 |
as per P0595. */ |
| 9539 |
|
| 9540 |
static GTY((deletable)) hash_map<tree, tree> *cv_cache; |
| 9541 |
|
| 9542 |
tree |
| 9543 |
maybe_constant_value (tree t, tree decl /* = NULL_TREE */, |
| 9544 |
mce_value manifestly_const_eval /* = mce_unknown */) |
| 9545 |
{ |
| 9546 |
tree r; |
| 9547 |
|
| 9548 |
if (!is_nondependent_constant_expression (t)) |
| 9549 |
{ |
| 9550 |
if (TREE_OVERFLOW_P (t) |
| 9551 |
|| (!processing_template_decl && TREE_CONSTANT (t))) |
| 9552 |
t = mark_non_constant (t); |
| 9553 |
return t; |
| 9554 |
} |
| 9555 |
else if (CONSTANT_CLASS_P (t)) |
| 9556 |
/* No caching or evaluation needed. */ |
| 9557 |
return t; |
| 9558 |
|
| 9559 |
/* Don't constant evaluate an unevaluated non-manifestly-constant operand, |
| 9560 |
but at least try folding it to a simple constant. */ |
| 9561 |
if (cp_unevaluated_operand && manifestly_const_eval != mce_true) |
| 9562 |
return fold_to_constant (t); |
| 9563 |
|
| 9564 |
if (manifestly_const_eval != mce_unknown) |
| 9565 |
return cxx_eval_outermost_constant_expr (t, true, true, |
| 9566 |
manifestly_const_eval, false, decl); |
| 9567 |
|
| 9568 |
if (cv_cache == NULL) |
| 9569 |
cv_cache = hash_map<tree, tree>::create_ggc (101); |
| 9570 |
if (tree *cached = cv_cache->get (t)) |
| 9571 |
{ |
| 9572 |
r = *cached; |
| 9573 |
if (r != t) |
| 9574 |
{ |
| 9575 |
/* Clear processing_template_decl for sake of break_out_target_exprs; |
| 9576 |
entries in the cv_cache are non-templated. */ |
| 9577 |
processing_template_decl_sentinel ptds; |
| 9578 |
|
| 9579 |
r = break_out_target_exprs (r, /*clear_loc*/true); |
| 9580 |
protected_set_expr_location (r, EXPR_LOCATION (t)); |
| 9581 |
} |
| 9582 |
return r; |
| 9583 |
} |
| 9584 |
|
| 9585 |
uid_sensitive_constexpr_evaluation_checker c; |
| 9586 |
r = cxx_eval_outermost_constant_expr (t, true, true, |
| 9587 |
manifestly_const_eval, false, decl); |
| 9588 |
gcc_checking_assert (r == t |
| 9589 |
|| CONVERT_EXPR_P (t) |
| 9590 |
|| TREE_CODE (t) == VIEW_CONVERT_EXPR |
| 9591 |
|| (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) |
| 9592 |
|| !cp_tree_equal (r, t)); |
| 9593 |
if (!c.evaluation_restricted_p ()) |
| 9594 |
cv_cache->put (t, r); |
| 9595 |
return r; |
| 9596 |
} |
| 9597 |
|
| 9598 |
/* Dispose of the whole CV_CACHE. */ |
| 9599 |
|
| 9600 |
static void |
| 9601 |
clear_cv_cache (void) |
| 9602 |
{ |
| 9603 |
if (cv_cache != NULL) |
| 9604 |
cv_cache->empty (); |
| 9605 |
} |
| 9606 |
|
| 9607 |
/* Dispose of the whole CV_CACHE and FOLD_CACHE. */ |
| 9608 |
|
| 9609 |
void |
| 9610 |
clear_cv_and_fold_caches () |
| 9611 |
{ |
| 9612 |
clear_cv_cache (); |
| 9613 |
clear_fold_cache (); |
| 9614 |
} |
| 9615 |
|
| 9616 |
/* Internal function handling expressions in templates for |
| 9617 |
fold_non_dependent_expr and fold_non_dependent_init. |
| 9618 |
|
| 9619 |
If we're in a template, but T isn't value dependent, simplify |
| 9620 |
it. We're supposed to treat: |
| 9621 |
|
| 9622 |
template <typename T> void f(T[1 + 1]); |
| 9623 |
template <typename T> void f(T[2]); |
| 9624 |
|
| 9625 |
as two declarations of the same function, for example. */ |
| 9626 |
|
| 9627 |
static tree |
| 9628 |
fold_non_dependent_expr_template (tree t, tsubst_flags_t complain, |
| 9629 |
bool manifestly_const_eval, |
| 9630 |
tree object) |
| 9631 |
{ |
| 9632 |
gcc_assert (processing_template_decl); |
| 9633 |
|
| 9634 |
if (is_nondependent_constant_expression (t)) |
| 9635 |
{ |
| 9636 |
processing_template_decl_sentinel s; |
| 9637 |
t = instantiate_non_dependent_expr_internal (t, complain); |
| 9638 |
|
| 9639 |
if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t)) |
| 9640 |
{ |
| 9641 |
if (TREE_OVERFLOW_P (t)) |
| 9642 |
{ |
| 9643 |
t = build_nop (TREE_TYPE (t), t); |
| 9644 |
TREE_CONSTANT (t) = false; |
| 9645 |
} |
| 9646 |
return t; |
| 9647 |
} |
| 9648 |
else if (CONSTANT_CLASS_P (t)) |
| 9649 |
/* No evaluation needed. */ |
| 9650 |
return t; |
| 9651 |
|
| 9652 |
/* Don't constant evaluate an unevaluated non-manifestly-constant operand, |
| 9653 |
but at least try folding it to a simple constant. */ |
| 9654 |
if (cp_unevaluated_operand && !manifestly_const_eval) |
| 9655 |
return fold_to_constant (t); |
| 9656 |
|
| 9657 |
tree r = cxx_eval_outermost_constant_expr (t, true, true, |
| 9658 |
mce_value (manifestly_const_eval), |
| 9659 |
false, object); |
| 9660 |
/* cp_tree_equal looks through NOPs, so allow them. */ |
| 9661 |
gcc_checking_assert (r == t |
| 9662 |
|| CONVERT_EXPR_P (t) |
| 9663 |
|| TREE_CODE (t) == VIEW_CONVERT_EXPR |
| 9664 |
|| (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) |
| 9665 |
|| !cp_tree_equal (r, t)); |
| 9666 |
return r; |
| 9667 |
} |
| 9668 |
else if (TREE_OVERFLOW_P (t)) |
| 9669 |
{ |
| 9670 |
t = build_nop (TREE_TYPE (t), t); |
| 9671 |
TREE_CONSTANT (t) = false; |
| 9672 |
} |
| 9673 |
|
| 9674 |
return t; |
| 9675 |
} |
| 9676 |
|
| 9677 |
/* Like maybe_constant_value but first fully instantiate the argument. |
| 9678 |
|
| 9679 |
Note: this is equivalent to instantiate_non_dependent_expr (t, complain) |
| 9680 |
followed by maybe_constant_value but is more efficient, |
| 9681 |
because it calls instantiation_dependent_expression_p and |
| 9682 |
potential_constant_expression at most once. |
| 9683 |
The manifestly_const_eval argument is passed to maybe_constant_value. |
| 9684 |
|
| 9685 |
Callers should generally pass their active complain, or if they are in a |
| 9686 |
non-template, diagnosing context, they can use the default of |
| 9687 |
tf_warning_or_error. Callers that might be within a template context, don't |
| 9688 |
have a complain parameter, and aren't going to remember the result for long |
| 9689 |
(e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node |
| 9690 |
appropriately. */ |
| 9691 |
|
| 9692 |
tree |
| 9693 |
fold_non_dependent_expr (tree t, |
| 9694 |
tsubst_flags_t complain /* = tf_warning_or_error */, |
| 9695 |
bool manifestly_const_eval /* = false */, |
| 9696 |
tree object /* = NULL_TREE */) |
| 9697 |
{ |
| 9698 |
if (t == NULL_TREE) |
| 9699 |
return NULL_TREE; |
| 9700 |
|
| 9701 |
if (processing_template_decl) |
| 9702 |
return fold_non_dependent_expr_template (t, complain, |
| 9703 |
manifestly_const_eval, object); |
| 9704 |
|
| 9705 |
return maybe_constant_value (t, object, mce_value (manifestly_const_eval)); |
| 9706 |
} |
| 9707 |
|
| 9708 |
/* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant, |
| 9709 |
return the original expression. */ |
| 9710 |
|
| 9711 |
tree |
| 9712 |
maybe_fold_non_dependent_expr (tree expr, |
| 9713 |
tsubst_flags_t complain/*=tf_warning_or_error*/) |
| 9714 |
{ |
| 9715 |
tree t = fold_non_dependent_expr (expr, complain); |
| 9716 |
if (t && TREE_CONSTANT (t)) |
| 9717 |
return t; |
| 9718 |
|
| 9719 |
return expr; |
| 9720 |
} |
| 9721 |
|
| 9722 |
/* Like maybe_constant_init but first fully instantiate the argument. */ |
| 9723 |
|
| 9724 |
tree |
| 9725 |
fold_non_dependent_init (tree t, |
| 9726 |
tsubst_flags_t complain /*=tf_warning_or_error*/, |
| 9727 |
bool manifestly_const_eval /*=false*/, |
| 9728 |
tree object /* = NULL_TREE */) |
| 9729 |
{ |
| 9730 |
if (t == NULL_TREE) |
| 9731 |
return NULL_TREE; |
| 9732 |
|
| 9733 |
if (processing_template_decl) |
| 9734 |
{ |
| 9735 |
t = fold_non_dependent_expr_template (t, complain, |
| 9736 |
manifestly_const_eval, object); |
| 9737 |
/* maybe_constant_init does this stripping, so do it here too. */ |
| 9738 |
if (TREE_CODE (t) == TARGET_EXPR) |
| 9739 |
{ |
| 9740 |
tree init = TARGET_EXPR_INITIAL (t); |
| 9741 |
if (TREE_CODE (init) == CONSTRUCTOR) |
| 9742 |
t = init; |
| 9743 |
} |
| 9744 |
return t; |
| 9745 |
} |
| 9746 |
|
| 9747 |
return maybe_constant_init (t, object, manifestly_const_eval); |
| 9748 |
} |
| 9749 |
|
| 9750 |
/* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather |
| 9751 |
than wrapped in a TARGET_EXPR. |
| 9752 |
ALLOW_NON_CONSTANT is false if T is required to be a constant expression. |
| 9753 |
MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as |
| 9754 |
per P0595 even when ALLOW_NON_CONSTANT is true. */ |
| 9755 |
|
| 9756 |
static tree |
| 9757 |
maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant, |
| 9758 |
mce_value manifestly_const_eval) |
| 9759 |
{ |
| 9760 |
if (!t) |
| 9761 |
return t; |
| 9762 |
if (TREE_CODE (t) == EXPR_STMT) |
| 9763 |
t = TREE_OPERAND (t, 0); |
| 9764 |
if (TREE_CODE (t) == CONVERT_EXPR |
| 9765 |
&& VOID_TYPE_P (TREE_TYPE (t))) |
| 9766 |
t = TREE_OPERAND (t, 0); |
| 9767 |
/* If the types don't match, the INIT_EXPR is initializing a subobject of |
| 9768 |
DECL and losing that information would cause mischief later. */ |
| 9769 |
if (TREE_CODE (t) == INIT_EXPR |
| 9770 |
&& (!decl |
| 9771 |
|| same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (decl), |
| 9772 |
TREE_TYPE (t)))) |
| 9773 |
t = TREE_OPERAND (t, 1); |
| 9774 |
if (TREE_CODE (t) == TARGET_EXPR) |
| 9775 |
t = TARGET_EXPR_INITIAL (t); |
| 9776 |
if (!is_nondependent_static_init_expression (t)) |
| 9777 |
/* Don't try to evaluate it. */; |
| 9778 |
else if (CONSTANT_CLASS_P (t) && TREE_CODE (t) != PTRMEM_CST) |
| 9779 |
/* No evaluation needed. PTRMEM_CST needs the immediate fn check. */; |
| 9780 |
else |
| 9781 |
{ |
| 9782 |
/* [basic.start.static] allows constant-initialization of variables with |
| 9783 |
static or thread storage duration even if it isn't required, but we |
| 9784 |
shouldn't bend the rules the same way for automatic variables. */ |
| 9785 |
bool is_static = (decl && DECL_P (decl) |
| 9786 |
&& (TREE_STATIC (decl) || DECL_EXTERNAL (decl))); |
| 9787 |
if (is_static) |
| 9788 |
manifestly_const_eval = mce_true; |
| 9789 |
|
| 9790 |
if (cp_unevaluated_operand && manifestly_const_eval != mce_true) |
| 9791 |
return fold_to_constant (t); |
| 9792 |
|
| 9793 |
t = cxx_eval_outermost_constant_expr (t, allow_non_constant, !is_static, |
| 9794 |
manifestly_const_eval, |
| 9795 |
false, decl); |
| 9796 |
} |
| 9797 |
if (TREE_CODE (t) == TARGET_EXPR) |
| 9798 |
{ |
| 9799 |
tree init = TARGET_EXPR_INITIAL (t); |
| 9800 |
if (TREE_CODE (init) == CONSTRUCTOR) |
| 9801 |
t = init; |
| 9802 |
} |
| 9803 |
return t; |
| 9804 |
} |
| 9805 |
|
| 9806 |
/* Wrapper for maybe_constant_init_1 which permits non constants. */ |
| 9807 |
|
| 9808 |
tree |
| 9809 |
maybe_constant_init (tree t, tree decl, bool manifestly_const_eval) |
| 9810 |
{ |
| 9811 |
return maybe_constant_init_1 (t, decl, true, mce_value (manifestly_const_eval)); |
| 9812 |
} |
| 9813 |
|
| 9814 |
tree |
| 9815 |
maybe_constant_init (tree t, tree decl, mce_value manifestly_const_eval) |
| 9816 |
{ |
| 9817 |
return maybe_constant_init_1 (t, decl, true, manifestly_const_eval); |
| 9818 |
} |
| 9819 |
|
| 9820 |
/* Wrapper for maybe_constant_init_1 which does not permit non constants. */ |
| 9821 |
|
| 9822 |
tree |
| 9823 |
cxx_constant_init (tree t, tree decl) |
| 9824 |
{ |
| 9825 |
return maybe_constant_init_1 (t, decl, false, mce_true); |
| 9826 |
} |
| 9827 |
|
| 9828 |
#if 0 |
| 9829 |
/* FIXME see ADDR_EXPR section in potential_constant_expression_1. */ |
| 9830 |
/* Return true if the object referred to by REF has automatic or thread |
| 9831 |
local storage. */ |
| 9832 |
|
| 9833 |
enum { ck_ok, ck_bad, ck_unknown }; |
| 9834 |
static int |
| 9835 |
check_automatic_or_tls (tree ref) |
| 9836 |
{ |
| 9837 |
machine_mode mode; |
| 9838 |
poly_int64 bitsize, bitpos; |
| 9839 |
tree offset; |
| 9840 |
int volatilep = 0, unsignedp = 0; |
| 9841 |
tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset, |
| 9842 |
&mode, &unsignedp, &volatilep, false); |
| 9843 |
duration_kind dk; |
| 9844 |
|
| 9845 |
/* If there isn't a decl in the middle, we don't know the linkage here, |
| 9846 |
and this isn't a constant expression anyway. */ |
| 9847 |
if (!DECL_P (decl)) |
| 9848 |
return ck_unknown; |
| 9849 |
dk = decl_storage_duration (decl); |
| 9850 |
return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok; |
| 9851 |
} |
| 9852 |
#endif |
| 9853 |
|
| 9854 |
/* Data structure for passing data from potential_constant_expression_1 |
| 9855 |
to check_for_return_continue via cp_walk_tree. */ |
| 9856 |
struct check_for_return_continue_data { |
| 9857 |
hash_set<tree> *pset; |
| 9858 |
tree continue_stmt; |
| 9859 |
tree break_stmt; |
| 9860 |
}; |
| 9861 |
|
| 9862 |
/* Helper function for potential_constant_expression_1 SWITCH_STMT handling, |
| 9863 |
called through cp_walk_tree. Return the first RETURN_EXPR found, or note |
| 9864 |
the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */ |
| 9865 |
static tree |
| 9866 |
check_for_return_continue (tree *tp, int *walk_subtrees, void *data) |
| 9867 |
{ |
| 9868 |
tree t = *tp, s, b; |
| 9869 |
check_for_return_continue_data *d = (check_for_return_continue_data *) data; |
| 9870 |
switch (TREE_CODE (t)) |
| 9871 |
{ |
| 9872 |
case RETURN_EXPR: |
| 9873 |
return t; |
| 9874 |
|
| 9875 |
case CONTINUE_STMT: |
| 9876 |
if (d->continue_stmt == NULL_TREE) |
| 9877 |
d->continue_stmt = t; |
| 9878 |
break; |
| 9879 |
|
| 9880 |
case BREAK_STMT: |
| 9881 |
if (d->break_stmt == NULL_TREE) |
| 9882 |
d->break_stmt = t; |
| 9883 |
break; |
| 9884 |
|
| 9885 |
#define RECUR(x) \ |
| 9886 |
if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \ |
| 9887 |
d->pset)) \ |
| 9888 |
return r |
| 9889 |
|
| 9890 |
/* For loops, walk subtrees manually, so that continue stmts found |
| 9891 |
inside of the bodies of the loops are ignored. */ |
| 9892 |
case DO_STMT: |
| 9893 |
*walk_subtrees = 0; |
| 9894 |
RECUR (DO_COND (t)); |
| 9895 |
s = d->continue_stmt; |
| 9896 |
b = d->break_stmt; |
| 9897 |
RECUR (DO_BODY (t)); |
| 9898 |
d->continue_stmt = s; |
| 9899 |
d->break_stmt = b; |
| 9900 |
break; |
| 9901 |
|
| 9902 |
case WHILE_STMT: |
| 9903 |
*walk_subtrees = 0; |
| 9904 |
RECUR (WHILE_COND_PREP (t)); |
| 9905 |
RECUR (WHILE_COND (t)); |
| 9906 |
s = d->continue_stmt; |
| 9907 |
b = d->break_stmt; |
| 9908 |
RECUR (WHILE_BODY (t)); |
| 9909 |
d->continue_stmt = s; |
| 9910 |
d->break_stmt = b; |
| 9911 |
break; |
| 9912 |
|
| 9913 |
case FOR_STMT: |
| 9914 |
*walk_subtrees = 0; |
| 9915 |
RECUR (FOR_INIT_STMT (t)); |
| 9916 |
RECUR (FOR_COND_PREP (t)); |
| 9917 |
RECUR (FOR_COND (t)); |
| 9918 |
RECUR (FOR_EXPR (t)); |
| 9919 |
s = d->continue_stmt; |
| 9920 |
b = d->break_stmt; |
| 9921 |
RECUR (FOR_BODY (t)); |
| 9922 |
d->continue_stmt = s; |
| 9923 |
d->break_stmt = b; |
| 9924 |
break; |
| 9925 |
|
| 9926 |
case RANGE_FOR_STMT: |
| 9927 |
*walk_subtrees = 0; |
| 9928 |
RECUR (RANGE_FOR_EXPR (t)); |
| 9929 |
s = d->continue_stmt; |
| 9930 |
b = d->break_stmt; |
| 9931 |
RECUR (RANGE_FOR_BODY (t)); |
| 9932 |
d->continue_stmt = s; |
| 9933 |
d->break_stmt = b; |
| 9934 |
break; |
| 9935 |
|
| 9936 |
case SWITCH_STMT: |
| 9937 |
*walk_subtrees = 0; |
| 9938 |
RECUR (SWITCH_STMT_COND (t)); |
| 9939 |
b = d->break_stmt; |
| 9940 |
RECUR (SWITCH_STMT_BODY (t)); |
| 9941 |
d->break_stmt = b; |
| 9942 |
break; |
| 9943 |
#undef RECUR |
| 9944 |
|
| 9945 |
case STATEMENT_LIST: |
| 9946 |
case CONSTRUCTOR: |
| 9947 |
break; |
| 9948 |
|
| 9949 |
default: |
| 9950 |
if (!EXPR_P (t)) |
| 9951 |
*walk_subtrees = 0; |
| 9952 |
break; |
| 9953 |
} |
| 9954 |
|
| 9955 |
return NULL_TREE; |
| 9956 |
} |
| 9957 |
|
| 9958 |
/* Return true if T denotes a potentially constant expression. Issue |
| 9959 |
diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true, |
| 9960 |
an lvalue-rvalue conversion is implied. If NOW is true, we want to |
| 9961 |
consider the expression in the current context, independent of constexpr |
| 9962 |
substitution. If FUNDEF_P is true, we're checking a constexpr function body |
| 9963 |
and hard errors should not be reported by constexpr_error. |
| 9964 |
|
| 9965 |
C++0x [expr.const] used to say |
| 9966 |
|
| 9967 |
6 An expression is a potential constant expression if it is |
| 9968 |
a constant expression where all occurrences of function |
| 9969 |
parameters are replaced by arbitrary constant expressions |
| 9970 |
of the appropriate type. |
| 9971 |
|
| 9972 |
2 A conditional expression is a constant expression unless it |
| 9973 |
involves one of the following as a potentially evaluated |
| 9974 |
subexpression (3.2), but subexpressions of logical AND (5.14), |
| 9975 |
logical OR (5.15), and conditional (5.16) operations that are |
| 9976 |
not evaluated are not considered. */ |
| 9977 |
|
| 9978 |
static bool |
| 9979 |
potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, |
| 9980 |
bool fundef_p, tsubst_flags_t flags, |
| 9981 |
tree *jump_target) |
| 9982 |
{ |
| 9983 |
#define RECUR(T,RV) \ |
| 9984 |
potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \ |
| 9985 |
jump_target) |
| 9986 |
|
| 9987 |
enum { any = false, rval = true }; |
| 9988 |
int i; |
| 9989 |
tree tmp; |
| 9990 |
|
| 9991 |
if (t == error_mark_node) |
| 9992 |
return false; |
| 9993 |
if (t == NULL_TREE) |
| 9994 |
return true; |
| 9995 |
location_t loc = cp_expr_loc_or_input_loc (t); |
| 9996 |
|
| 9997 |
if (*jump_target) |
| 9998 |
/* If we are jumping, ignore everything. This is simpler than the |
| 9999 |
cxx_eval_constant_expression handling because we only need to be |
| 10000 |
conservatively correct, and we don't necessarily have a constant value |
| 10001 |
available, so we don't bother with switch tracking. */ |
| 10002 |
return true; |
| 10003 |
|
| 10004 |
if (TREE_THIS_VOLATILE (t) && want_rval |
| 10005 |
&& !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t)) |
| 10006 |
&& !NULLPTR_TYPE_P (TREE_TYPE (t))) |
| 10007 |
{ |
| 10008 |
if (flags & tf_error) |
| 10009 |
constexpr_error (loc, fundef_p, "lvalue-to-rvalue conversion of " |
| 10010 |
"a volatile lvalue %qE with type %qT", t, |
| 10011 |
TREE_TYPE (t)); |
| 10012 |
return false; |
| 10013 |
} |
| 10014 |
if (CONSTANT_CLASS_P (t)) |
| 10015 |
return true; |
| 10016 |
if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED) |
| 10017 |
&& TREE_TYPE (t) == error_mark_node) |
| 10018 |
return false; |
| 10019 |
|
| 10020 |
switch (TREE_CODE (t)) |
| 10021 |
{ |
| 10022 |
case FUNCTION_DECL: |
| 10023 |
case BASELINK: |
| 10024 |
case TEMPLATE_DECL: |
| 10025 |
case OVERLOAD: |
| 10026 |
case TEMPLATE_ID_EXPR: |
| 10027 |
case LABEL_DECL: |
| 10028 |
case CASE_LABEL_EXPR: |
| 10029 |
case PREDICT_EXPR: |
| 10030 |
case CONST_DECL: |
| 10031 |
case SIZEOF_EXPR: |
| 10032 |
case ALIGNOF_EXPR: |
| 10033 |
case OFFSETOF_EXPR: |
| 10034 |
case NOEXCEPT_EXPR: |
| 10035 |
case TEMPLATE_PARM_INDEX: |
| 10036 |
case TRAIT_EXPR: |
| 10037 |
case IDENTIFIER_NODE: |
| 10038 |
case USERDEF_LITERAL: |
| 10039 |
/* We can see a FIELD_DECL in a pointer-to-member expression. */ |
| 10040 |
case FIELD_DECL: |
| 10041 |
case RESULT_DECL: |
| 10042 |
case USING_DECL: |
| 10043 |
case USING_STMT: |
| 10044 |
case PLACEHOLDER_EXPR: |
| 10045 |
case REQUIRES_EXPR: |
| 10046 |
case STATIC_ASSERT: |
| 10047 |
case DEBUG_BEGIN_STMT: |
| 10048 |
return true; |
| 10049 |
|
| 10050 |
case RETURN_EXPR: |
| 10051 |
if (!RECUR (TREE_OPERAND (t, 0), any)) |
| 10052 |
return false; |
| 10053 |
/* FALLTHROUGH */ |
| 10054 |
|
| 10055 |
case BREAK_STMT: |
| 10056 |
case CONTINUE_STMT: |
| 10057 |
*jump_target = t; |
| 10058 |
return true; |
| 10059 |
|
| 10060 |
case PARM_DECL: |
| 10061 |
if (now && want_rval) |
| 10062 |
{ |
| 10063 |
tree type = TREE_TYPE (t); |
| 10064 |
if (dependent_type_p (type) |
| 10065 |
|| !COMPLETE_TYPE_P (processing_template_decl |
| 10066 |
? type : complete_type (type)) |
| 10067 |
|| is_really_empty_class (type, /*ignore_vptr*/false)) |
| 10068 |
/* An empty class has no data to read. */ |
| 10069 |
return true; |
| 10070 |
if (flags & tf_error) |
| 10071 |
constexpr_error (input_location, fundef_p, |
| 10072 |
"%qE is not a constant expression", t); |
| 10073 |
return false; |
| 10074 |
} |
| 10075 |
return true; |
| 10076 |
|
| 10077 |
case AGGR_INIT_EXPR: |
| 10078 |
case CALL_EXPR: |
| 10079 |
/* -- an invocation of a function other than a constexpr function |
| 10080 |
or a constexpr constructor. */ |
| 10081 |
{ |
| 10082 |
tree fun = get_function_named_in_call (t); |
| 10083 |
const int nargs = call_expr_nargs (t); |
| 10084 |
i = 0; |
| 10085 |
|
| 10086 |
if (fun == NULL_TREE) |
| 10087 |
{ |
| 10088 |
/* Reset to allow the function to continue past the end |
| 10089 |
of the block below. Otherwise return early. */ |
| 10090 |
bool bail = true; |
| 10091 |
|
| 10092 |
if (TREE_CODE (t) == CALL_EXPR |
| 10093 |
&& CALL_EXPR_FN (t) == NULL_TREE) |
| 10094 |
switch (CALL_EXPR_IFN (t)) |
| 10095 |
{ |
| 10096 |
/* These should be ignored, they are optimized away from |
| 10097 |
constexpr functions. */ |
| 10098 |
case IFN_UBSAN_NULL: |
| 10099 |
case IFN_UBSAN_BOUNDS: |
| 10100 |
case IFN_UBSAN_VPTR: |
| 10101 |
case IFN_FALLTHROUGH: |
| 10102 |
case IFN_ASSUME: |
| 10103 |
return true; |
| 10104 |
|
| 10105 |
case IFN_ADD_OVERFLOW: |
| 10106 |
case IFN_SUB_OVERFLOW: |
| 10107 |
case IFN_MUL_OVERFLOW: |
| 10108 |
case IFN_LAUNDER: |
| 10109 |
case IFN_VEC_CONVERT: |
| 10110 |
bail = false; |
| 10111 |
break; |
| 10112 |
|
| 10113 |
default: |
| 10114 |
break; |
| 10115 |
} |
| 10116 |
|
| 10117 |
if (bail) |
| 10118 |
{ |
| 10119 |
/* fold_call_expr can't do anything with IFN calls. */ |
| 10120 |
if (flags & tf_error) |
| 10121 |
constexpr_error (loc, fundef_p, |
| 10122 |
"call to internal function %qE", t); |
| 10123 |
return false; |
| 10124 |
} |
| 10125 |
} |
| 10126 |
|
| 10127 |
if (fun && is_overloaded_fn (fun)) |
| 10128 |
{ |
| 10129 |
if (!RECUR (fun, true)) |
| 10130 |
return false; |
| 10131 |
fun = get_fns (fun); |
| 10132 |
|
| 10133 |
if (TREE_CODE (fun) == FUNCTION_DECL) |
| 10134 |
{ |
| 10135 |
if (builtin_valid_in_constant_expr_p (fun)) |
| 10136 |
return true; |
| 10137 |
if (!maybe_constexpr_fn (fun) |
| 10138 |
/* Allow any built-in function; if the expansion |
| 10139 |
isn't constant, we'll deal with that then. */ |
| 10140 |
&& !fndecl_built_in_p (fun) |
| 10141 |
/* In C++20, replaceable global allocation functions |
| 10142 |
are constant expressions. */ |
| 10143 |
&& (!cxx_replaceable_global_alloc_fn (fun) |
| 10144 |
|| TREE_CODE (t) != CALL_EXPR |
| 10145 |
|| (!CALL_FROM_NEW_OR_DELETE_P (t) |
| 10146 |
&& (current_function_decl == NULL_TREE |
| 10147 |
|| !is_std_allocator_allocate |
| 10148 |
(current_function_decl)))) |
| 10149 |
/* Allow placement new in std::construct_at. */ |
| 10150 |
&& (!cxx_placement_new_fn (fun) |
| 10151 |
|| TREE_CODE (t) != CALL_EXPR |
| 10152 |
|| current_function_decl == NULL_TREE |
| 10153 |
|| !is_std_construct_at (current_function_decl)) |
| 10154 |
&& !cxx_dynamic_cast_fn_p (fun)) |
| 10155 |
{ |
| 10156 |
if ((flags & tf_error) |
| 10157 |
&& constexpr_error (loc, fundef_p, |
| 10158 |
"call to non-%<constexpr%> " |
| 10159 |
"function %qD", fun)) |
| 10160 |
explain_invalid_constexpr_fn (fun); |
| 10161 |
return false; |
| 10162 |
} |
| 10163 |
} |
| 10164 |
|
| 10165 |
fun = OVL_FIRST (fun); |
| 10166 |
/* Skip initial arguments to base constructors. */ |
| 10167 |
if (DECL_BASE_CONSTRUCTOR_P (fun)) |
| 10168 |
i = num_artificial_parms_for (fun); |
| 10169 |
} |
| 10170 |
else if (fun) |
| 10171 |
{ |
| 10172 |
if (TREE_TYPE (fun) |
| 10173 |
&& FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun))) |
| 10174 |
want_rval = rval; |
| 10175 |
else |
| 10176 |
want_rval = any; |
| 10177 |
if (RECUR (fun, want_rval)) |
| 10178 |
/* Might end up being a constant function pointer. But it |
| 10179 |
could also be a function object with constexpr op(), so |
| 10180 |
we pass 'any' so that the underlying VAR_DECL is deemed |
| 10181 |
as potentially-constant even though it wasn't declared |
| 10182 |
constexpr. */; |
| 10183 |
else |
| 10184 |
return false; |
| 10185 |
} |
| 10186 |
for (; i < nargs; ++i) |
| 10187 |
{ |
| 10188 |
tree x = get_nth_callarg (t, i); |
| 10189 |
/* In a template, reference arguments haven't been converted to |
| 10190 |
REFERENCE_TYPE and we might not even know if the parameter |
| 10191 |
is a reference, so accept lvalue constants too. */ |
| 10192 |
bool rv = processing_template_decl ? any : rval; |
| 10193 |
/* Don't require an immediately constant value, as constexpr |
| 10194 |
substitution might not use the value of the argument. */ |
| 10195 |
bool sub_now = false; |
| 10196 |
if (!potential_constant_expression_1 (x, rv, strict, |
| 10197 |
sub_now, fundef_p, flags, |
| 10198 |
jump_target)) |
| 10199 |
return false; |
| 10200 |
} |
| 10201 |
return true; |
| 10202 |
} |
| 10203 |
|
| 10204 |
case NON_LVALUE_EXPR: |
| 10205 |
/* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to |
| 10206 |
-- an lvalue of integral type that refers to a non-volatile |
| 10207 |
const variable or static data member initialized with |
| 10208 |
constant expressions, or |
| 10209 |
|
| 10210 |
-- an lvalue of literal type that refers to non-volatile |
| 10211 |
object defined with constexpr, or that refers to a |
| 10212 |
sub-object of such an object; */ |
| 10213 |
return RECUR (TREE_OPERAND (t, 0), rval); |
| 10214 |
|
| 10215 |
case EXCESS_PRECISION_EXPR: |
| 10216 |
return RECUR (TREE_OPERAND (t, 0), rval); |
| 10217 |
|
| 10218 |
case VAR_DECL: |
| 10219 |
if (DECL_HAS_VALUE_EXPR_P (t)) |
| 10220 |
{ |
| 10221 |
if (now && is_normal_capture_proxy (t)) |
| 10222 |
{ |
| 10223 |
/* -- in a lambda-expression, a reference to this or to a |
| 10224 |
variable with automatic storage duration defined outside that |
| 10225 |
lambda-expression, where the reference would be an |
| 10226 |
odr-use. */ |
| 10227 |
|
| 10228 |
if (want_rval) |
| 10229 |
/* Since we're doing an lvalue-rvalue conversion, this might |
| 10230 |
not be an odr-use, so evaluate the variable directly. */ |
| 10231 |
return RECUR (DECL_CAPTURED_VARIABLE (t), rval); |
| 10232 |
|
| 10233 |
if (flags & tf_error) |
| 10234 |
{ |
| 10235 |
tree cap = DECL_CAPTURED_VARIABLE (t); |
| 10236 |
auto_diagnostic_group d; |
| 10237 |
if (constexpr_error (input_location, fundef_p, |
| 10238 |
"lambda capture of %qE is not a " |
| 10239 |
"constant expression", cap) |
| 10240 |
&& decl_constant_var_p (cap)) |
| 10241 |
inform (input_location, "because it is used as a glvalue"); |
| 10242 |
} |
| 10243 |
return false; |
| 10244 |
} |
| 10245 |
/* Treat __PRETTY_FUNCTION__ inside a template function as |
| 10246 |
potentially-constant. */ |
| 10247 |
else if (DECL_PRETTY_FUNCTION_P (t) |
| 10248 |
&& DECL_VALUE_EXPR (t) == error_mark_node) |
| 10249 |
return true; |
| 10250 |
return RECUR (DECL_VALUE_EXPR (t), rval); |
| 10251 |
} |
| 10252 |
if (want_rval |
| 10253 |
&& (now || !var_in_maybe_constexpr_fn (t)) |
| 10254 |
&& !type_dependent_expression_p (t) |
| 10255 |
&& !decl_maybe_constant_var_p (t) |
| 10256 |
&& (strict |
| 10257 |
|| !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t)) |
| 10258 |
|| (DECL_INITIAL (t) |
| 10259 |
&& !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))) |
| 10260 |
&& COMPLETE_TYPE_P (TREE_TYPE (t)) |
| 10261 |
&& !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false)) |
| 10262 |
{ |
| 10263 |
if (flags & tf_error) |
| 10264 |
non_const_var_error (loc, t, fundef_p); |
| 10265 |
return false; |
| 10266 |
} |
| 10267 |
return true; |
| 10268 |
|
| 10269 |
case NOP_EXPR: |
| 10270 |
if (REINTERPRET_CAST_P (t)) |
| 10271 |
{ |
| 10272 |
if (flags & tf_error) |
| 10273 |
constexpr_error (loc, fundef_p, "%<reinterpret_cast%> is not a " |
| 10274 |
"constant expression"); |
| 10275 |
return false; |
| 10276 |
} |
| 10277 |
/* FALLTHRU */ |
| 10278 |
case CONVERT_EXPR: |
| 10279 |
case VIEW_CONVERT_EXPR: |
| 10280 |
/* -- a reinterpret_cast. FIXME not implemented, and this rule |
| 10281 |
may change to something more specific to type-punning (DR 1312). */ |
| 10282 |
{ |
| 10283 |
tree from = TREE_OPERAND (t, 0); |
| 10284 |
if (location_wrapper_p (t)) |
| 10285 |
{ |
| 10286 |
iloc_sentinel ils = loc; |
| 10287 |
return (RECUR (from, want_rval)); |
| 10288 |
} |
| 10289 |
if (INDIRECT_TYPE_P (TREE_TYPE (t))) |
| 10290 |
{ |
| 10291 |
STRIP_ANY_LOCATION_WRAPPER (from); |
| 10292 |
if (TREE_CODE (from) == INTEGER_CST |
| 10293 |
&& !integer_zerop (from)) |
| 10294 |
{ |
| 10295 |
if (flags & tf_error) |
| 10296 |
constexpr_error (loc, fundef_p, |
| 10297 |
"%<reinterpret_cast%> from integer to " |
| 10298 |
"pointer"); |
| 10299 |
return false; |
| 10300 |
} |
| 10301 |
} |
| 10302 |
return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR)); |
| 10303 |
} |
| 10304 |
|
| 10305 |
case ADDRESSOF_EXPR: |
| 10306 |
/* This is like ADDR_EXPR, except it won't form pointer-to-member. */ |
| 10307 |
t = TREE_OPERAND (t, 0); |
| 10308 |
goto handle_addr_expr; |
| 10309 |
|
| 10310 |
case ADDR_EXPR: |
| 10311 |
/* -- a unary operator & that is applied to an lvalue that |
| 10312 |
designates an object with thread or automatic storage |
| 10313 |
duration; */ |
| 10314 |
t = TREE_OPERAND (t, 0); |
| 10315 |
|
| 10316 |
if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t)) |
| 10317 |
/* A pointer-to-member constant. */ |
| 10318 |
return true; |
| 10319 |
|
| 10320 |
handle_addr_expr: |
| 10321 |
#if 0 |
| 10322 |
/* FIXME adjust when issue 1197 is fully resolved. For now don't do |
| 10323 |
any checking here, as we might dereference the pointer later. If |
| 10324 |
we remove this code, also remove check_automatic_or_tls. */ |
| 10325 |
i = check_automatic_or_tls (t); |
| 10326 |
if (i == ck_ok) |
| 10327 |
return true; |
| 10328 |
if (i == ck_bad) |
| 10329 |
{ |
| 10330 |
if (flags & tf_error) |
| 10331 |
error ("address-of an object %qE with thread local or " |
| 10332 |
"automatic storage is not a constant expression", t); |
| 10333 |
return false; |
| 10334 |
} |
| 10335 |
#endif |
| 10336 |
return RECUR (t, any); |
| 10337 |
|
| 10338 |
case COMPONENT_REF: |
| 10339 |
case ARROW_EXPR: |
| 10340 |
case OFFSET_REF: |
| 10341 |
/* -- a class member access unless its postfix-expression is |
| 10342 |
of literal type or of pointer to literal type. */ |
| 10343 |
/* This test would be redundant, as it follows from the |
| 10344 |
postfix-expression being a potential constant expression. */ |
| 10345 |
if (type_unknown_p (t)) |
| 10346 |
return true; |
| 10347 |
if (is_overloaded_fn (t)) |
| 10348 |
/* In a template, a COMPONENT_REF of a function expresses ob.fn(), |
| 10349 |
which uses ob as an lvalue. */ |
| 10350 |
want_rval = false; |
| 10351 |
gcc_fallthrough (); |
| 10352 |
|
| 10353 |
case REALPART_EXPR: |
| 10354 |
case IMAGPART_EXPR: |
| 10355 |
case BIT_FIELD_REF: |
| 10356 |
return RECUR (TREE_OPERAND (t, 0), want_rval); |
| 10357 |
|
| 10358 |
case EXPR_PACK_EXPANSION: |
| 10359 |
return RECUR (PACK_EXPANSION_PATTERN (t), want_rval); |
| 10360 |
|
| 10361 |
case PACK_INDEX_EXPR: |
| 10362 |
return true; |
| 10363 |
|
| 10364 |
case INDIRECT_REF: |
| 10365 |
return RECUR (TREE_OPERAND (t, 0), rval); |
| 10366 |
|
| 10367 |
case STATEMENT_LIST: |
| 10368 |
for (tree stmt : tsi_range (t)) |
| 10369 |
if (!RECUR (stmt, any)) |
| 10370 |
return false; |
| 10371 |
return true; |
| 10372 |
|
| 10373 |
case MODIFY_EXPR: |
| 10374 |
if (cxx_dialect < cxx14) |
| 10375 |
goto fail; |
| 10376 |
if (!RECUR (TREE_OPERAND (t, 0), any)) |
| 10377 |
return false; |
| 10378 |
/* Just ignore clobbers. */ |
| 10379 |
if (TREE_CLOBBER_P (TREE_OPERAND (t, 1))) |
| 10380 |
return true; |
| 10381 |
if (!RECUR (TREE_OPERAND (t, 1), rval)) |
| 10382 |
return false; |
| 10383 |
return true; |
| 10384 |
|
| 10385 |
case MODOP_EXPR: |
| 10386 |
if (cxx_dialect < cxx14) |
| 10387 |
goto fail; |
| 10388 |
if (!RECUR (TREE_OPERAND (t, 0), rval)) |
| 10389 |
return false; |
| 10390 |
if (!RECUR (TREE_OPERAND (t, 2), rval)) |
| 10391 |
return false; |
| 10392 |
return true; |
| 10393 |
|
| 10394 |
case DO_STMT: |
| 10395 |
if (!RECUR (DO_COND (t), rval)) |
| 10396 |
return false; |
| 10397 |
if (!RECUR (DO_BODY (t), any)) |
| 10398 |
return false; |
| 10399 |
if (breaks (jump_target) || continues (jump_target)) |
| 10400 |
*jump_target = NULL_TREE; |
| 10401 |
return true; |
| 10402 |
|
| 10403 |
case FOR_STMT: |
| 10404 |
if (!RECUR (FOR_INIT_STMT (t), any)) |
| 10405 |
return false; |
| 10406 |
if (!RECUR (FOR_COND_PREP (t), any)) |
| 10407 |
return false; |
| 10408 |
tmp = FOR_COND (t); |
| 10409 |
if (!RECUR (tmp, rval)) |
| 10410 |
return false; |
| 10411 |
if (tmp) |
| 10412 |
{ |
| 10413 |
if (!processing_template_decl) |
| 10414 |
tmp = cxx_eval_outermost_constant_expr (tmp, true); |
| 10415 |
/* If we couldn't evaluate the condition, it might not ever be |
| 10416 |
true. */ |
| 10417 |
if (!integer_onep (tmp)) |
| 10418 |
{ |
| 10419 |
/* Before returning true, check if the for body can contain |
| 10420 |
a return. */ |
| 10421 |
hash_set<tree> pset; |
| 10422 |
check_for_return_continue_data data = { &pset, NULL_TREE, |
| 10423 |
NULL_TREE }; |
| 10424 |
if (tree ret_expr |
| 10425 |
= cp_walk_tree (&FOR_BODY (t), check_for_return_continue, |
| 10426 |
&data, &pset)) |
| 10427 |
*jump_target = ret_expr; |
| 10428 |
return true; |
| 10429 |
} |
| 10430 |
} |
| 10431 |
if (!RECUR (FOR_EXPR (t), any)) |
| 10432 |
return false; |
| 10433 |
if (!RECUR (FOR_BODY (t), any)) |
| 10434 |
return false; |
| 10435 |
if (!RECUR (FOR_COND_CLEANUP (t), any)) |
| 10436 |
return false; |
| 10437 |
if (breaks (jump_target) || continues (jump_target)) |
| 10438 |
*jump_target = NULL_TREE; |
| 10439 |
return true; |
| 10440 |
|
| 10441 |
case RANGE_FOR_STMT: |
| 10442 |
if (!RECUR (RANGE_FOR_INIT_STMT (t), any)) |
| 10443 |
return false; |
| 10444 |
if (!RECUR (RANGE_FOR_EXPR (t), any)) |
| 10445 |
return false; |
| 10446 |
if (!RECUR (RANGE_FOR_BODY (t), any)) |
| 10447 |
return false; |
| 10448 |
if (breaks (jump_target) || continues (jump_target)) |
| 10449 |
*jump_target = NULL_TREE; |
| 10450 |
return true; |
| 10451 |
|
| 10452 |
case WHILE_STMT: |
| 10453 |
if (!RECUR (WHILE_COND_PREP (t), any)) |
| 10454 |
return false; |
| 10455 |
tmp = WHILE_COND (t); |
| 10456 |
if (!RECUR (tmp, rval)) |
| 10457 |
return false; |
| 10458 |
if (!processing_template_decl) |
| 10459 |
tmp = cxx_eval_outermost_constant_expr (tmp, true); |
| 10460 |
/* If we couldn't evaluate the condition, it might not ever be true. */ |
| 10461 |
if (!integer_onep (tmp)) |
| 10462 |
{ |
| 10463 |
/* Before returning true, check if the while body can contain |
| 10464 |
a return. */ |
| 10465 |
hash_set<tree> pset; |
| 10466 |
check_for_return_continue_data data = { &pset, NULL_TREE, |
| 10467 |
NULL_TREE }; |
| 10468 |
if (tree ret_expr |
| 10469 |
= cp_walk_tree (&WHILE_BODY (t), check_for_return_continue, |
| 10470 |
&data, &pset)) |
| 10471 |
*jump_target = ret_expr; |
| 10472 |
return true; |
| 10473 |
} |
| 10474 |
if (!RECUR (WHILE_BODY (t), any)) |
| 10475 |
return false; |
| 10476 |
if (!RECUR (WHILE_COND_CLEANUP (t), any)) |
| 10477 |
return false; |
| 10478 |
if (breaks (jump_target) || continues (jump_target)) |
| 10479 |
*jump_target = NULL_TREE; |
| 10480 |
return true; |
| 10481 |
|
| 10482 |
case SWITCH_STMT: |
| 10483 |
if (!RECUR (SWITCH_STMT_COND (t), rval)) |
| 10484 |
return false; |
| 10485 |
/* FIXME we don't check SWITCH_STMT_BODY currently, because even |
| 10486 |
unreachable labels would be checked and it is enough if there is |
| 10487 |
a single switch cond value for which it is a valid constant |
| 10488 |
expression. We need to check if there are any RETURN_EXPRs |
| 10489 |
or CONTINUE_STMTs inside of the body though, as in that case |
| 10490 |
we need to set *jump_target. */ |
| 10491 |
else |
| 10492 |
{ |
| 10493 |
hash_set<tree> pset; |
| 10494 |
check_for_return_continue_data data = { &pset, NULL_TREE, |
| 10495 |
NULL_TREE }; |
| 10496 |
if (tree ret_expr |
| 10497 |
= cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue, |
| 10498 |
&data, &pset)) |
| 10499 |
/* The switch might return. */ |
| 10500 |
*jump_target = ret_expr; |
| 10501 |
else if (data.continue_stmt) |
| 10502 |
/* The switch can't return, but might continue. */ |
| 10503 |
*jump_target = data.continue_stmt; |
| 10504 |
} |
| 10505 |
return true; |
| 10506 |
|
| 10507 |
case STMT_EXPR: |
| 10508 |
return RECUR (STMT_EXPR_STMT (t), rval); |
| 10509 |
|
| 10510 |
case LAMBDA_EXPR: |
| 10511 |
if (cxx_dialect >= cxx17) |
| 10512 |
/* In C++17 lambdas can be constexpr, don't give up yet. */ |
| 10513 |
return true; |
| 10514 |
else if (flags & tf_error) |
| 10515 |
constexpr_error (loc, fundef_p, "lambda-expression is not a " |
| 10516 |
"constant expression before C++17"); |
| 10517 |
return false; |
| 10518 |
|
| 10519 |
case NEW_EXPR: |
| 10520 |
case VEC_NEW_EXPR: |
| 10521 |
case DELETE_EXPR: |
| 10522 |
case VEC_DELETE_EXPR: |
| 10523 |
if (cxx_dialect >= cxx20) |
| 10524 |
/* In C++20, new-expressions are potentially constant. */ |
| 10525 |
return true; |
| 10526 |
else if (flags & tf_error) |
| 10527 |
constexpr_error (loc, fundef_p, "new-expression is not a " |
| 10528 |
"constant expression before C++20"); |
| 10529 |
return false; |
| 10530 |
|
| 10531 |
case DYNAMIC_CAST_EXPR: |
| 10532 |
case PSEUDO_DTOR_EXPR: |
| 10533 |
case THROW_EXPR: |
| 10534 |
case OMP_PARALLEL: |
| 10535 |
case OMP_TASK: |
| 10536 |
case OMP_FOR: |
| 10537 |
case OMP_SIMD: |
| 10538 |
case OMP_DISTRIBUTE: |
| 10539 |
case OMP_TASKLOOP: |
| 10540 |
case OMP_LOOP: |
| 10541 |
case OMP_TEAMS: |
| 10542 |
case OMP_TARGET_DATA: |
| 10543 |
case OMP_TARGET: |
| 10544 |
case OMP_SECTIONS: |
| 10545 |
case OMP_ORDERED: |
| 10546 |
case OMP_CRITICAL: |
| 10547 |
case OMP_SINGLE: |
| 10548 |
case OMP_SCAN: |
| 10549 |
case OMP_SCOPE: |
| 10550 |
case OMP_SECTION: |
| 10551 |
case OMP_MASTER: |
| 10552 |
case OMP_MASKED: |
| 10553 |
case OMP_TASKGROUP: |
| 10554 |
case OMP_TARGET_UPDATE: |
| 10555 |
case OMP_TARGET_ENTER_DATA: |
| 10556 |
case OMP_TARGET_EXIT_DATA: |
| 10557 |
case OMP_ATOMIC: |
| 10558 |
case OMP_ATOMIC_READ: |
| 10559 |
case OMP_ATOMIC_CAPTURE_OLD: |
| 10560 |
case OMP_ATOMIC_CAPTURE_NEW: |
| 10561 |
case OMP_DEPOBJ: |
| 10562 |
case OACC_PARALLEL: |
| 10563 |
case OACC_KERNELS: |
| 10564 |
case OACC_SERIAL: |
| 10565 |
case OACC_DATA: |
| 10566 |
case OACC_HOST_DATA: |
| 10567 |
case OACC_LOOP: |
| 10568 |
case OACC_CACHE: |
| 10569 |
case OACC_DECLARE: |
| 10570 |
case OACC_ENTER_DATA: |
| 10571 |
case OACC_EXIT_DATA: |
| 10572 |
case OACC_UPDATE: |
| 10573 |
case OMP_ARRAY_SECTION: |
| 10574 |
/* GCC internal stuff. */ |
| 10575 |
case VA_ARG_EXPR: |
| 10576 |
case TRANSACTION_EXPR: |
| 10577 |
case AT_ENCODE_EXPR: |
| 10578 |
fail: |
| 10579 |
if (flags & tf_error) |
| 10580 |
constexpr_error (loc, fundef_p, "expression %qE is not a constant " |
| 10581 |
"expression", t); |
| 10582 |
return false; |
| 10583 |
|
| 10584 |
case ASM_EXPR: |
| 10585 |
if (flags & tf_error) |
| 10586 |
inline_asm_in_constexpr_error (loc, fundef_p); |
| 10587 |
return false; |
| 10588 |
|
| 10589 |
case OBJ_TYPE_REF: |
| 10590 |
if (cxx_dialect >= cxx20) |
| 10591 |
/* In C++20 virtual calls can be constexpr, don't give up yet. */ |
| 10592 |
return true; |
| 10593 |
else if (flags & tf_error) |
| 10594 |
constexpr_error (loc, fundef_p, "virtual functions cannot be " |
| 10595 |
"%<constexpr%> before C++20"); |
| 10596 |
return false; |
| 10597 |
|
| 10598 |
case TYPEID_EXPR: |
| 10599 |
/* In C++20, a typeid expression whose operand is of polymorphic |
| 10600 |
class type can be constexpr. */ |
| 10601 |
{ |
| 10602 |
tree e = TREE_OPERAND (t, 0); |
| 10603 |
if (cxx_dialect < cxx20 |
| 10604 |
&& strict |
| 10605 |
&& !TYPE_P (e) |
| 10606 |
&& !type_dependent_expression_p (e) |
| 10607 |
&& TYPE_POLYMORPHIC_P (TREE_TYPE (e))) |
| 10608 |
{ |
| 10609 |
if (flags & tf_error) |
| 10610 |
constexpr_error (loc, fundef_p, "%<typeid%> is not a " |
| 10611 |
"constant expression because %qE is " |
| 10612 |
"of polymorphic type", e); |
| 10613 |
return false; |
| 10614 |
} |
| 10615 |
return true; |
| 10616 |
} |
| 10617 |
|
| 10618 |
case POINTER_DIFF_EXPR: |
| 10619 |
case MINUS_EXPR: |
| 10620 |
want_rval = true; |
| 10621 |
goto binary; |
| 10622 |
|
| 10623 |
case LT_EXPR: |
| 10624 |
case LE_EXPR: |
| 10625 |
case GT_EXPR: |
| 10626 |
case GE_EXPR: |
| 10627 |
case EQ_EXPR: |
| 10628 |
case NE_EXPR: |
| 10629 |
case SPACESHIP_EXPR: |
| 10630 |
want_rval = true; |
| 10631 |
goto binary; |
| 10632 |
|
| 10633 |
case PREINCREMENT_EXPR: |
| 10634 |
case POSTINCREMENT_EXPR: |
| 10635 |
case PREDECREMENT_EXPR: |
| 10636 |
case POSTDECREMENT_EXPR: |
| 10637 |
if (cxx_dialect < cxx14) |
| 10638 |
goto fail; |
| 10639 |
goto unary; |
| 10640 |
|
| 10641 |
case BIT_NOT_EXPR: |
| 10642 |
/* A destructor. */ |
| 10643 |
if (TYPE_P (TREE_OPERAND (t, 0))) |
| 10644 |
return true; |
| 10645 |
/* fall through. */ |
| 10646 |
|
| 10647 |
case CONJ_EXPR: |
| 10648 |
case SAVE_EXPR: |
| 10649 |
case FIX_TRUNC_EXPR: |
| 10650 |
case FLOAT_EXPR: |
| 10651 |
case NEGATE_EXPR: |
| 10652 |
case ABS_EXPR: |
| 10653 |
case ABSU_EXPR: |
| 10654 |
case TRUTH_NOT_EXPR: |
| 10655 |
case FIXED_CONVERT_EXPR: |
| 10656 |
case UNARY_PLUS_EXPR: |
| 10657 |
case UNARY_LEFT_FOLD_EXPR: |
| 10658 |
case UNARY_RIGHT_FOLD_EXPR: |
| 10659 |
case VEC_DUPLICATE_EXPR: |
| 10660 |
unary: |
| 10661 |
return RECUR (TREE_OPERAND (t, 0), rval); |
| 10662 |
|
| 10663 |
case CAST_EXPR: |
| 10664 |
case CONST_CAST_EXPR: |
| 10665 |
case STATIC_CAST_EXPR: |
| 10666 |
case REINTERPRET_CAST_EXPR: |
| 10667 |
case IMPLICIT_CONV_EXPR: |
| 10668 |
if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t))) |
| 10669 |
/* In C++98, a conversion to non-integral type can't be part of a |
| 10670 |
constant expression. */ |
| 10671 |
{ |
| 10672 |
if (flags & tf_error) |
| 10673 |
constexpr_error (loc, fundef_p, |
| 10674 |
"cast to non-integral type %qT in a constant " |
| 10675 |
"expression", TREE_TYPE (t)); |
| 10676 |
return false; |
| 10677 |
} |
| 10678 |
/* This might be a conversion from a class to a (potentially) literal |
| 10679 |
type. Let's consider it potentially constant since the conversion |
| 10680 |
might be a constexpr user-defined conversion. */ |
| 10681 |
else if (cxx_dialect >= cxx11 |
| 10682 |
&& (dependent_type_p (TREE_TYPE (t)) |
| 10683 |
|| !COMPLETE_TYPE_P (TREE_TYPE (t)) |
| 10684 |
|| literal_type_p (TREE_TYPE (t))) |
| 10685 |
&& TREE_OPERAND (t, 0) |
| 10686 |
&& (TREE_CODE (t) != CAST_EXPR |
| 10687 |
|| !TREE_CHAIN (TREE_OPERAND (t, 0)))) |
| 10688 |
{ |
| 10689 |
tree from = TREE_OPERAND (t, 0); |
| 10690 |
if (TREE_CODE (t) == CAST_EXPR) |
| 10691 |
from = TREE_VALUE (from); |
| 10692 |
tree type = TREE_TYPE (from); |
| 10693 |
/* If this is a dependent type, it could end up being a class |
| 10694 |
with conversions. */ |
| 10695 |
if (type == NULL_TREE || WILDCARD_TYPE_P (type)) |
| 10696 |
return true; |
| 10697 |
/* Or a non-dependent class which has conversions. */ |
| 10698 |
else if (CLASS_TYPE_P (type) |
| 10699 |
&& (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type))) |
| 10700 |
return true; |
| 10701 |
} |
| 10702 |
|
| 10703 |
return (RECUR (TREE_OPERAND (t, 0), |
| 10704 |
!TYPE_REF_P (TREE_TYPE (t)))); |
| 10705 |
|
| 10706 |
case BIND_EXPR: |
| 10707 |
return RECUR (BIND_EXPR_BODY (t), want_rval); |
| 10708 |
|
| 10709 |
case CLEANUP_POINT_EXPR: |
| 10710 |
case MUST_NOT_THROW_EXPR: |
| 10711 |
case TRY_CATCH_EXPR: |
| 10712 |
case TRY_BLOCK: |
| 10713 |
case EH_SPEC_BLOCK: |
| 10714 |
case EXPR_STMT: |
| 10715 |
case PAREN_EXPR: |
| 10716 |
/* For convenience. */ |
| 10717 |
case LOOP_EXPR: |
| 10718 |
case EXIT_EXPR: |
| 10719 |
return RECUR (TREE_OPERAND (t, 0), want_rval); |
| 10720 |
|
| 10721 |
case DECL_EXPR: |
| 10722 |
tmp = DECL_EXPR_DECL (t); |
| 10723 |
if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp) |
| 10724 |
&& (processing_template_decl |
| 10725 |
? !decl_maybe_constant_var_p (tmp) |
| 10726 |
: !decl_constant_var_p (tmp))) |
| 10727 |
{ |
| 10728 |
if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp)) |
| 10729 |
{ |
| 10730 |
if (flags & tf_error) |
| 10731 |
constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p, |
| 10732 |
"%qD defined %<thread_local%> in " |
| 10733 |
"%<constexpr%> context", tmp); |
| 10734 |
return false; |
| 10735 |
} |
| 10736 |
else if (TREE_STATIC (tmp)) |
| 10737 |
{ |
| 10738 |
if (flags & tf_error) |
| 10739 |
constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p, |
| 10740 |
"%qD defined %<static%> in %<constexpr%> " |
| 10741 |
"context", tmp); |
| 10742 |
return false; |
| 10743 |
} |
| 10744 |
else if (!check_for_uninitialized_const_var |
| 10745 |
(tmp, /*constexpr_context_p=*/true, flags)) |
| 10746 |
return false; |
| 10747 |
} |
| 10748 |
if (VAR_P (tmp)) |
| 10749 |
return RECUR (DECL_INITIAL (tmp), want_rval); |
| 10750 |
return true; |
| 10751 |
|
| 10752 |
case TRY_FINALLY_EXPR: |
| 10753 |
return (RECUR (TREE_OPERAND (t, 0), want_rval) |
| 10754 |
&& RECUR (TREE_OPERAND (t, 1), any)); |
| 10755 |
|
| 10756 |
case SCOPE_REF: |
| 10757 |
return RECUR (TREE_OPERAND (t, 1), want_rval); |
| 10758 |
|
| 10759 |
case TARGET_EXPR: |
| 10760 |
if (!TARGET_EXPR_DIRECT_INIT_P (t) |
| 10761 |
&& !TARGET_EXPR_ELIDING_P (t) |
| 10762 |
&& !literal_type_p (TREE_TYPE (t))) |
| 10763 |
{ |
| 10764 |
if (flags & tf_error) |
| 10765 |
{ |
| 10766 |
auto_diagnostic_group d; |
| 10767 |
if (constexpr_error (loc, fundef_p, |
| 10768 |
"temporary of non-literal type %qT in a " |
| 10769 |
"constant expression", TREE_TYPE (t))) |
| 10770 |
explain_non_literal_class (TREE_TYPE (t)); |
| 10771 |
} |
| 10772 |
return false; |
| 10773 |
} |
| 10774 |
/* FALLTHRU */ |
| 10775 |
case INIT_EXPR: |
| 10776 |
return RECUR (TREE_OPERAND (t, 1), rval); |
| 10777 |
|
| 10778 |
case CONSTRUCTOR: |
| 10779 |
{ |
| 10780 |
vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t); |
| 10781 |
constructor_elt *ce; |
| 10782 |
for (i = 0; vec_safe_iterate (v, i, &ce); ++i) |
| 10783 |
if (!RECUR (ce->value, want_rval)) |
| 10784 |
return false; |
| 10785 |
return true; |
| 10786 |
} |
| 10787 |
|
| 10788 |
case TREE_LIST: |
| 10789 |
{ |
| 10790 |
gcc_assert (TREE_PURPOSE (t) == NULL_TREE |
| 10791 |
|| DECL_P (TREE_PURPOSE (t))); |
| 10792 |
if (!RECUR (TREE_VALUE (t), want_rval)) |
| 10793 |
return false; |
| 10794 |
if (TREE_CHAIN (t) == NULL_TREE) |
| 10795 |
return true; |
| 10796 |
return RECUR (TREE_CHAIN (t), want_rval); |
| 10797 |
} |
| 10798 |
|
| 10799 |
case TRUNC_DIV_EXPR: |
| 10800 |
case CEIL_DIV_EXPR: |
| 10801 |
case FLOOR_DIV_EXPR: |
| 10802 |
case ROUND_DIV_EXPR: |
| 10803 |
case TRUNC_MOD_EXPR: |
| 10804 |
case CEIL_MOD_EXPR: |
| 10805 |
case ROUND_MOD_EXPR: |
| 10806 |
{ |
| 10807 |
tree denom = TREE_OPERAND (t, 1); |
| 10808 |
if (!RECUR (denom, rval)) |
| 10809 |
return false; |
| 10810 |
/* We can't call cxx_eval_outermost_constant_expr on an expression |
| 10811 |
that hasn't been through instantiate_non_dependent_expr yet. */ |
| 10812 |
if (!processing_template_decl) |
| 10813 |
denom = cxx_eval_outermost_constant_expr (denom, true); |
| 10814 |
if (integer_zerop (denom)) |
| 10815 |
{ |
| 10816 |
if (flags & tf_error) |
| 10817 |
constexpr_error (input_location, fundef_p, |
| 10818 |
"division by zero is not a constant expression"); |
| 10819 |
return false; |
| 10820 |
} |
| 10821 |
else |
| 10822 |
{ |
| 10823 |
want_rval = true; |
| 10824 |
return RECUR (TREE_OPERAND (t, 0), want_rval); |
| 10825 |
} |
| 10826 |
} |
| 10827 |
|
| 10828 |
case COMPOUND_EXPR: |
| 10829 |
{ |
| 10830 |
/* check_return_expr sometimes wraps a TARGET_EXPR in a |
| 10831 |
COMPOUND_EXPR; don't get confused. */ |
| 10832 |
tree op0 = TREE_OPERAND (t, 0); |
| 10833 |
tree op1 = TREE_OPERAND (t, 1); |
| 10834 |
STRIP_NOPS (op1); |
| 10835 |
if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0)) |
| 10836 |
return RECUR (op0, want_rval); |
| 10837 |
else |
| 10838 |
goto binary; |
| 10839 |
} |
| 10840 |
|
| 10841 |
/* If the first operand is the non-short-circuit constant, look at |
| 10842 |
the second operand; otherwise we only care about the first one for |
| 10843 |
potentiality. */ |
| 10844 |
case TRUTH_AND_EXPR: |
| 10845 |
case TRUTH_ANDIF_EXPR: |
| 10846 |
tmp = boolean_true_node; |
| 10847 |
goto truth; |
| 10848 |
case TRUTH_OR_EXPR: |
| 10849 |
case TRUTH_ORIF_EXPR: |
| 10850 |
tmp = boolean_false_node; |
| 10851 |
truth: |
| 10852 |
{ |
| 10853 |
tree op0 = TREE_OPERAND (t, 0); |
| 10854 |
tree op1 = TREE_OPERAND (t, 1); |
| 10855 |
if (!RECUR (op0, rval)) |
| 10856 |
return false; |
| 10857 |
if (!(flags & tf_error) && RECUR (op1, rval)) |
| 10858 |
/* When quiet, try to avoid expensive trial evaluation by first |
| 10859 |
checking potentiality of the second operand. */ |
| 10860 |
return true; |
| 10861 |
if (!processing_template_decl) |
| 10862 |
op0 = cxx_eval_outermost_constant_expr (op0, true); |
| 10863 |
if (tree_int_cst_equal (op0, tmp)) |
| 10864 |
return (flags & tf_error) ? RECUR (op1, rval) : false; |
| 10865 |
else |
| 10866 |
return true; |
| 10867 |
} |
| 10868 |
|
| 10869 |
case PLUS_EXPR: |
| 10870 |
case MULT_EXPR: |
| 10871 |
case POINTER_PLUS_EXPR: |
| 10872 |
case RDIV_EXPR: |
| 10873 |
case EXACT_DIV_EXPR: |
| 10874 |
case MIN_EXPR: |
| 10875 |
case MAX_EXPR: |
| 10876 |
case LSHIFT_EXPR: |
| 10877 |
case RSHIFT_EXPR: |
| 10878 |
case LROTATE_EXPR: |
| 10879 |
case RROTATE_EXPR: |
| 10880 |
case BIT_IOR_EXPR: |
| 10881 |
case BIT_XOR_EXPR: |
| 10882 |
case BIT_AND_EXPR: |
| 10883 |
case TRUTH_XOR_EXPR: |
| 10884 |
case UNORDERED_EXPR: |
| 10885 |
case ORDERED_EXPR: |
| 10886 |
case UNLT_EXPR: |
| 10887 |
case UNLE_EXPR: |
| 10888 |
case UNGT_EXPR: |
| 10889 |
case UNGE_EXPR: |
| 10890 |
case UNEQ_EXPR: |
| 10891 |
case LTGT_EXPR: |
| 10892 |
case RANGE_EXPR: |
| 10893 |
case COMPLEX_EXPR: |
| 10894 |
want_rval = true; |
| 10895 |
/* Fall through. */ |
| 10896 |
case ARRAY_REF: |
| 10897 |
case ARRAY_RANGE_REF: |
| 10898 |
case MEMBER_REF: |
| 10899 |
case DOTSTAR_EXPR: |
| 10900 |
case MEM_REF: |
| 10901 |
case BINARY_LEFT_FOLD_EXPR: |
| 10902 |
case BINARY_RIGHT_FOLD_EXPR: |
| 10903 |
binary: |
| 10904 |
for (i = 0; i < 2; ++i) |
| 10905 |
if (!RECUR (TREE_OPERAND (t, i), want_rval)) |
| 10906 |
return false; |
| 10907 |
return true; |
| 10908 |
|
| 10909 |
case VEC_PERM_EXPR: |
| 10910 |
for (i = 0; i < 3; ++i) |
| 10911 |
if (!RECUR (TREE_OPERAND (t, i), true)) |
| 10912 |
return false; |
| 10913 |
return true; |
| 10914 |
|
| 10915 |
case COND_EXPR: |
| 10916 |
if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20) |
| 10917 |
{ |
| 10918 |
if (flags & tf_error) |
| 10919 |
constexpr_error (loc, fundef_p, "%<delete[]%> is not a " |
| 10920 |
"constant expression"); |
| 10921 |
return false; |
| 10922 |
} |
| 10923 |
/* Fall through. */ |
| 10924 |
case IF_STMT: |
| 10925 |
case VEC_COND_EXPR: |
| 10926 |
/* If the condition is a known constant, we know which of the legs we |
| 10927 |
care about; otherwise we only require that the condition and |
| 10928 |
either of the legs be potentially constant. */ |
| 10929 |
tmp = TREE_OPERAND (t, 0); |
| 10930 |
if (!RECUR (tmp, rval)) |
| 10931 |
return false; |
| 10932 |
if (!processing_template_decl) |
| 10933 |
tmp = cxx_eval_outermost_constant_expr (tmp, true); |
| 10934 |
/* potential_constant_expression* isn't told if it is called for |
| 10935 |
manifestly_const_eval or not, so for consteval if always |
| 10936 |
process both branches as if the condition is not a known |
| 10937 |
constant. */ |
| 10938 |
if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t)) |
| 10939 |
{ |
| 10940 |
if (integer_zerop (tmp)) |
| 10941 |
return RECUR (TREE_OPERAND (t, 2), want_rval); |
| 10942 |
else if (TREE_CODE (tmp) == INTEGER_CST) |
| 10943 |
return RECUR (TREE_OPERAND (t, 1), want_rval); |
| 10944 |
} |
| 10945 |
tmp = *jump_target; |
| 10946 |
for (i = 1; i < 3; ++i) |
| 10947 |
{ |
| 10948 |
tree this_jump_target = tmp; |
| 10949 |
if (potential_constant_expression_1 (TREE_OPERAND (t, i), |
| 10950 |
want_rval, strict, now, fundef_p, |
| 10951 |
tf_none, &this_jump_target)) |
| 10952 |
{ |
| 10953 |
if (returns (&this_jump_target)) |
| 10954 |
*jump_target = this_jump_target; |
| 10955 |
else if (!returns (jump_target)) |
| 10956 |
{ |
| 10957 |
if (breaks (&this_jump_target) |
| 10958 |
|| continues (&this_jump_target)) |
| 10959 |
*jump_target = this_jump_target; |
| 10960 |
if (i == 1) |
| 10961 |
{ |
| 10962 |
/* If the then branch is potentially constant, but |
| 10963 |
does not return, check if the else branch |
| 10964 |
couldn't return, break or continue. */ |
| 10965 |
hash_set<tree> pset; |
| 10966 |
check_for_return_continue_data data = { &pset, NULL_TREE, |
| 10967 |
NULL_TREE }; |
| 10968 |
if (tree ret_expr |
| 10969 |
= cp_walk_tree (&TREE_OPERAND (t, 2), |
| 10970 |
check_for_return_continue, &data, |
| 10971 |
&pset)) |
| 10972 |
*jump_target = ret_expr; |
| 10973 |
else if (*jump_target == NULL_TREE) |
| 10974 |
{ |
| 10975 |
if (data.continue_stmt) |
| 10976 |
*jump_target = data.continue_stmt; |
| 10977 |
else if (data.break_stmt) |
| 10978 |
*jump_target = data.break_stmt; |
| 10979 |
} |
| 10980 |
} |
| 10981 |
} |
| 10982 |
return true; |
| 10983 |
} |
| 10984 |
} |
| 10985 |
if (flags & tf_error) |
| 10986 |
{ |
| 10987 |
if (TREE_CODE (t) == IF_STMT) |
| 10988 |
constexpr_error (loc, fundef_p, "neither branch of %<if%> is a " |
| 10989 |
"constant expression"); |
| 10990 |
else |
| 10991 |
constexpr_error (loc, fundef_p, "expression %qE is not a " |
| 10992 |
"constant expression", t); |
| 10993 |
} |
| 10994 |
return false; |
| 10995 |
|
| 10996 |
case VEC_INIT_EXPR: |
| 10997 |
if (VEC_INIT_EXPR_IS_CONSTEXPR (t)) |
| 10998 |
return true; |
| 10999 |
if (flags & tf_error) |
| 11000 |
{ |
| 11001 |
if (constexpr_error (loc, fundef_p, "non-constant array " |
| 11002 |
"initialization")) |
| 11003 |
diagnose_non_constexpr_vec_init (t); |
| 11004 |
} |
| 11005 |
return false; |
| 11006 |
|
| 11007 |
case TYPE_DECL: |
| 11008 |
case TAG_DEFN: |
| 11009 |
/* We can see these in statement-expressions. */ |
| 11010 |
return true; |
| 11011 |
|
| 11012 |
case CLEANUP_STMT: |
| 11013 |
if (!RECUR (CLEANUP_BODY (t), any)) |
| 11014 |
return false; |
| 11015 |
if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any)) |
| 11016 |
return false; |
| 11017 |
return true; |
| 11018 |
|
| 11019 |
case EMPTY_CLASS_EXPR: |
| 11020 |
return true; |
| 11021 |
|
| 11022 |
case GOTO_EXPR: |
| 11023 |
{ |
| 11024 |
tree *target = &TREE_OPERAND (t, 0); |
| 11025 |
/* Gotos representing break, continue and cdtor return are OK. */ |
| 11026 |
if (breaks (target) || continues (target) || returns (target)) |
| 11027 |
{ |
| 11028 |
*jump_target = *target; |
| 11029 |
return true; |
| 11030 |
} |
| 11031 |
if (flags & tf_error) |
| 11032 |
constexpr_error (loc, fundef_p, "%<goto%> is not a constant " |
| 11033 |
"expression"); |
| 11034 |
return false; |
| 11035 |
} |
| 11036 |
|
| 11037 |
case ASSERTION_STMT: |
| 11038 |
case PRECONDITION_STMT: |
| 11039 |
case POSTCONDITION_STMT: |
| 11040 |
if (!checked_contract_p (get_contract_semantic (t))) |
| 11041 |
return true; |
| 11042 |
return RECUR (CONTRACT_CONDITION (t), rval); |
| 11043 |
|
| 11044 |
case LABEL_EXPR: |
| 11045 |
t = LABEL_EXPR_LABEL (t); |
| 11046 |
if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23) |
| 11047 |
return true; |
| 11048 |
else if (flags & tf_error) |
| 11049 |
constexpr_error (loc, fundef_p, "label definition in %<constexpr%> " |
| 11050 |
"function only available with %<-std=c++23%> or " |
| 11051 |
"%<-std=gnu++23%>"); |
| 11052 |
return false; |
| 11053 |
|
| 11054 |
case ANNOTATE_EXPR: |
| 11055 |
return RECUR (TREE_OPERAND (t, 0), rval); |
| 11056 |
|
| 11057 |
case BIT_CAST_EXPR: |
| 11058 |
return RECUR (TREE_OPERAND (t, 0), rval); |
| 11059 |
|
| 11060 |
/* Coroutine await, yield and return expressions are not. */ |
| 11061 |
case CO_AWAIT_EXPR: |
| 11062 |
case CO_YIELD_EXPR: |
| 11063 |
case CO_RETURN_EXPR: |
| 11064 |
if (flags & tf_error) |
| 11065 |
constexpr_error (cp_expr_loc_or_loc (t, input_location), fundef_p, |
| 11066 |
"%qE is not a constant expression", t); |
| 11067 |
return false; |
| 11068 |
|
| 11069 |
/* Assume a TU-local entity is not constant, we'll error later when |
| 11070 |
instantiating. */ |
| 11071 |
case TU_LOCAL_ENTITY: |
| 11072 |
return false; |
| 11073 |
|
| 11074 |
case NONTYPE_ARGUMENT_PACK: |
| 11075 |
{ |
| 11076 |
tree args = ARGUMENT_PACK_ARGS (t); |
| 11077 |
int len = TREE_VEC_LENGTH (args); |
| 11078 |
for (int i = 0; i < len; ++i) |
| 11079 |
if (!RECUR (TREE_VEC_ELT (args, i), any)) |
| 11080 |
return false; |
| 11081 |
return true; |
| 11082 |
} |
| 11083 |
|
| 11084 |
default: |
| 11085 |
if (objc_non_constant_expr_p (t)) |
| 11086 |
return false; |
| 11087 |
|
| 11088 |
sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t))); |
| 11089 |
gcc_unreachable (); |
| 11090 |
return false; |
| 11091 |
} |
| 11092 |
#undef RECUR |
| 11093 |
} |
| 11094 |
|
| 11095 |
bool |
| 11096 |
potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, |
| 11097 |
bool fundef_p, tsubst_flags_t flags) |
| 11098 |
{ |
| 11099 |
if (flags & tf_error) |
| 11100 |
{ |
| 11101 |
/* Check potentiality quietly first, as that could be performed more |
| 11102 |
efficiently in some cases (currently only for TRUTH_*_EXPR). If |
| 11103 |
that fails, replay the check noisily to give errors. */ |
| 11104 |
flags &= ~tf_error; |
| 11105 |
if (potential_constant_expression_1 (t, want_rval, strict, now, fundef_p, |
| 11106 |
flags)) |
| 11107 |
return true; |
| 11108 |
flags |= tf_error; |
| 11109 |
} |
| 11110 |
|
| 11111 |
tree target = NULL_TREE; |
| 11112 |
return potential_constant_expression_1 (t, want_rval, strict, now, fundef_p, |
| 11113 |
flags, &target); |
| 11114 |
} |
| 11115 |
|
| 11116 |
/* The main entry point to the above. */ |
| 11117 |
|
| 11118 |
bool |
| 11119 |
potential_constant_expression (tree t) |
| 11120 |
{ |
| 11121 |
return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true, |
| 11122 |
/*now*/false, /*fundef_p*/false, |
| 11123 |
tf_none); |
| 11124 |
} |
| 11125 |
|
| 11126 |
/* As above, but require a constant rvalue. */ |
| 11127 |
|
| 11128 |
bool |
| 11129 |
potential_rvalue_constant_expression (tree t) |
| 11130 |
{ |
| 11131 |
return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true, |
| 11132 |
/*now*/false, /*fundef_p*/false, |
| 11133 |
tf_none); |
| 11134 |
} |
| 11135 |
|
| 11136 |
/* Like above, but complain about non-constant expressions. */ |
| 11137 |
|
| 11138 |
bool |
| 11139 |
require_potential_constant_expression (tree t) |
| 11140 |
{ |
| 11141 |
return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true, |
| 11142 |
/*now*/false, /*fundef_p*/false, |
| 11143 |
tf_warning_or_error); |
| 11144 |
} |
| 11145 |
|
| 11146 |
/* Cross product of the above. */ |
| 11147 |
|
| 11148 |
bool |
| 11149 |
require_potential_rvalue_constant_expression (tree t) |
| 11150 |
{ |
| 11151 |
return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true, |
| 11152 |
/*now*/false, /*fundef_p*/false, |
| 11153 |
tf_warning_or_error); |
| 11154 |
} |
| 11155 |
|
| 11156 |
/* Like require_potential_rvalue_constant_expression, but fundef_p is true. */ |
| 11157 |
|
| 11158 |
bool |
| 11159 |
require_potential_rvalue_constant_expression_fncheck (tree t) |
| 11160 |
{ |
| 11161 |
return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true, |
| 11162 |
/*now*/false, /*fundef_p*/true, |
| 11163 |
tf_warning_or_error); |
| 11164 |
} |
| 11165 |
|
| 11166 |
/* Like above, but don't consider PARM_DECL a potential_constant_expression. */ |
| 11167 |
|
| 11168 |
bool |
| 11169 |
require_rvalue_constant_expression (tree t) |
| 11170 |
{ |
| 11171 |
return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true, |
| 11172 |
/*now*/true, /*fundef_p*/false, |
| 11173 |
tf_warning_or_error); |
| 11174 |
} |
| 11175 |
|
| 11176 |
/* Like potential_constant_expression, but don't consider possible constexpr |
| 11177 |
substitution of the current function. That is, PARM_DECL qualifies under |
| 11178 |
potential_constant_expression, but not here. |
| 11179 |
|
| 11180 |
This is basically what you can check when any actual constant values might |
| 11181 |
be value-dependent. */ |
| 11182 |
|
| 11183 |
bool |
| 11184 |
is_constant_expression (tree t) |
| 11185 |
{ |
| 11186 |
return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true, |
| 11187 |
/*now*/true, /*fundef_p*/false, |
| 11188 |
tf_none); |
| 11189 |
} |
| 11190 |
|
| 11191 |
/* As above, but expect an rvalue. */ |
| 11192 |
|
| 11193 |
bool |
| 11194 |
is_rvalue_constant_expression (tree t) |
| 11195 |
{ |
| 11196 |
return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true, |
| 11197 |
/*now*/true, /*fundef_p*/false, |
| 11198 |
tf_none); |
| 11199 |
} |
| 11200 |
|
| 11201 |
/* Like above, but complain about non-constant expressions. */ |
| 11202 |
|
| 11203 |
bool |
| 11204 |
require_constant_expression (tree t) |
| 11205 |
{ |
| 11206 |
return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true, |
| 11207 |
/*now*/true, /*fundef_p*/false, |
| 11208 |
tf_warning_or_error); |
| 11209 |
} |
| 11210 |
|
| 11211 |
/* Like is_constant_expression, but allow const variables that are not allowed |
| 11212 |
under constexpr rules. */ |
| 11213 |
|
| 11214 |
bool |
| 11215 |
is_static_init_expression (tree t) |
| 11216 |
{ |
| 11217 |
return potential_constant_expression_1 (t, /*want_rval*/false, |
| 11218 |
/*strict*/false, /*now*/true, |
| 11219 |
/*fundef_p*/false, tf_none); |
| 11220 |
} |
| 11221 |
|
| 11222 |
/* Returns true if T is a potential constant expression that is not |
| 11223 |
instantiation-dependent, and therefore a candidate for constant folding even |
| 11224 |
in a template. */ |
| 11225 |
|
| 11226 |
bool |
| 11227 |
is_nondependent_constant_expression (tree t) |
| 11228 |
{ |
| 11229 |
return (!type_unknown_p (t) |
| 11230 |
&& is_constant_expression (t) |
| 11231 |
&& !instantiation_dependent_expression_p (t)); |
| 11232 |
} |
| 11233 |
|
| 11234 |
/* Returns true if T is a potential static initializer expression that is not |
| 11235 |
instantiation-dependent. */ |
| 11236 |
|
| 11237 |
bool |
| 11238 |
is_nondependent_static_init_expression (tree t) |
| 11239 |
{ |
| 11240 |
return (!type_unknown_p (t) |
| 11241 |
&& is_static_init_expression (t) |
| 11242 |
&& !instantiation_dependent_expression_p (t)); |
| 11243 |
} |
| 11244 |
|
| 11245 |
/* True iff FN is an implicitly constexpr function. */ |
| 11246 |
|
| 11247 |
bool |
| 11248 |
decl_implicit_constexpr_p (tree fn) |
| 11249 |
{ |
| 11250 |
if (!(flag_implicit_constexpr |
| 11251 |
&& TREE_CODE (fn) == FUNCTION_DECL |
| 11252 |
&& DECL_DECLARED_CONSTEXPR_P (fn))) |
| 11253 |
return false; |
| 11254 |
|
| 11255 |
if (DECL_CLONED_FUNCTION_P (fn)) |
| 11256 |
fn = DECL_CLONED_FUNCTION (fn); |
| 11257 |
|
| 11258 |
return (DECL_LANG_SPECIFIC (fn) |
| 11259 |
&& DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr); |
| 11260 |
} |
| 11261 |
|
| 11262 |
/* Finalize constexpr processing after parsing. */ |
| 11263 |
|
| 11264 |
void |
| 11265 |
fini_constexpr (void) |
| 11266 |
{ |
| 11267 |
/* The contexpr call and fundef copies tables are no longer needed. */ |
| 11268 |
constexpr_call_table = NULL; |
| 11269 |
fundef_copies_table = NULL; |
| 11270 |
} |
| 11271 |
|
| 11272 |
#include "gt-cp-constexpr.h" |