| 1 |
/* Convert language-specific tree expression to rtl instructions, |
| 2 |
for GNU compiler. |
| 3 |
Copyright (C) 1988-2025 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This file is part of GCC. |
| 6 |
|
| 7 |
GCC is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 3, or (at your option) |
| 10 |
any later version. |
| 11 |
|
| 12 |
GCC is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with GCC; see the file COPYING3. If not see |
| 19 |
<http://www.gnu.org/licenses/>. */ |
| 20 |
|
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
#include "system.h" |
| 24 |
#include "coretypes.h" |
| 25 |
#include "cp-tree.h" |
| 26 |
|
| 27 |
/* Expand C++-specific constants. Currently, this means PTRMEM_CST. */ |
| 28 |
|
| 29 |
tree |
| 30 |
cplus_expand_constant (tree cst) |
| 31 |
{ |
| 32 |
switch (TREE_CODE (cst)) |
| 33 |
{ |
| 34 |
case PTRMEM_CST: |
| 35 |
{ |
| 36 |
tree type = TREE_TYPE (cst); |
| 37 |
tree member; |
| 38 |
|
| 39 |
/* Find the member. */ |
| 40 |
member = PTRMEM_CST_MEMBER (cst); |
| 41 |
|
| 42 |
/* We can't lower this until the class is complete. */ |
| 43 |
if (!COMPLETE_TYPE_P (DECL_CONTEXT (member))) |
| 44 |
return cst; |
| 45 |
|
| 46 |
if (TREE_CODE (member) == FIELD_DECL) |
| 47 |
{ |
| 48 |
/* Find the offset for the field. */ |
| 49 |
cst = byte_position (member); |
| 50 |
while (!same_type_p (DECL_CONTEXT (member), |
| 51 |
TYPE_PTRMEM_CLASS_TYPE (type))) |
| 52 |
{ |
| 53 |
/* The MEMBER must have been nestled within an |
| 54 |
anonymous aggregate contained in TYPE. Find the |
| 55 |
anonymous aggregate. */ |
| 56 |
member = lookup_anon_field (TYPE_PTRMEM_CLASS_TYPE (type), |
| 57 |
DECL_CONTEXT (member)); |
| 58 |
cst = size_binop (PLUS_EXPR, cst, byte_position (member)); |
| 59 |
} |
| 60 |
cst = fold (build_nop (type, cst)); |
| 61 |
} |
| 62 |
else |
| 63 |
{ |
| 64 |
tree delta; |
| 65 |
tree pfn; |
| 66 |
|
| 67 |
expand_ptrmemfunc_cst (cst, &delta, &pfn); |
| 68 |
cst = build_ptrmemfunc1 (type, delta, pfn); |
| 69 |
} |
| 70 |
} |
| 71 |
break; |
| 72 |
|
| 73 |
case CONSTRUCTOR: |
| 74 |
{ |
| 75 |
constructor_elt *elt; |
| 76 |
unsigned HOST_WIDE_INT idx; |
| 77 |
FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (cst), idx, elt) |
| 78 |
elt->value = cplus_expand_constant (elt->value); |
| 79 |
} |
| 80 |
|
| 81 |
default: |
| 82 |
/* There's nothing to do. */ |
| 83 |
break; |
| 84 |
} |
| 85 |
|
| 86 |
return cst; |
| 87 |
} |
| 88 |
|
| 89 |
/* We've seen an actual use of EXPR. Possibly replace an outer variable |
| 90 |
reference inside with its constant value or a lambda capture. */ |
| 91 |
|
| 92 |
tree |
| 93 |
mark_use (tree expr, bool rvalue_p, bool read_p, |
| 94 |
location_t loc /* = UNKNOWN_LOCATION */, |
| 95 |
bool reject_builtin /* = true */) |
| 96 |
{ |
| 97 |
#define RECUR(t) mark_use ((t), rvalue_p, read_p, loc, reject_builtin) |
| 98 |
|
| 99 |
if (expr == NULL_TREE || error_operand_p (expr)) |
| 100 |
return expr; |
| 101 |
|
| 102 |
if (reject_builtin && reject_gcc_builtin (expr, loc)) |
| 103 |
return error_mark_node; |
| 104 |
|
| 105 |
if (read_p) |
| 106 |
mark_exp_read (expr); |
| 107 |
|
| 108 |
tree oexpr = expr; |
| 109 |
bool recurse_op[3] = { false, false, false }; |
| 110 |
switch (TREE_CODE (expr)) |
| 111 |
{ |
| 112 |
case VAR_DECL: |
| 113 |
case PARM_DECL: |
| 114 |
if (rvalue_p && is_normal_capture_proxy (expr)) |
| 115 |
{ |
| 116 |
/* Look through capture by copy. */ |
| 117 |
tree cap = DECL_CAPTURED_VARIABLE (expr); |
| 118 |
if (TREE_CODE (TREE_TYPE (cap)) == TREE_CODE (TREE_TYPE (expr)) |
| 119 |
&& decl_constant_var_p (cap)) |
| 120 |
{ |
| 121 |
tree val = RECUR (cap); |
| 122 |
if (!is_capture_proxy (val)) |
| 123 |
{ |
| 124 |
tree l = current_lambda_expr (); |
| 125 |
LAMBDA_EXPR_CAPTURE_OPTIMIZED (l) = true; |
| 126 |
} |
| 127 |
return val; |
| 128 |
} |
| 129 |
} |
| 130 |
if (outer_automatic_var_p (expr) |
| 131 |
&& decl_constant_var_p (expr)) |
| 132 |
{ |
| 133 |
if (rvalue_p) |
| 134 |
{ |
| 135 |
tree t = maybe_constant_value (expr); |
| 136 |
if (TREE_CONSTANT (t)) |
| 137 |
{ |
| 138 |
expr = t; |
| 139 |
break; |
| 140 |
} |
| 141 |
} |
| 142 |
iloc_sentinel l (loc); |
| 143 |
expr = process_outer_var_ref (expr, tf_warning_or_error, true); |
| 144 |
if (!(TREE_TYPE (oexpr) |
| 145 |
&& TYPE_REF_P (TREE_TYPE (oexpr)))) |
| 146 |
expr = convert_from_reference (expr); |
| 147 |
} |
| 148 |
break; |
| 149 |
case COMPONENT_REF: |
| 150 |
recurse_op[0] = true; |
| 151 |
break; |
| 152 |
case COMPOUND_EXPR: |
| 153 |
recurse_op[1] = true; |
| 154 |
break; |
| 155 |
case COND_EXPR: |
| 156 |
recurse_op[2] = true; |
| 157 |
if (TREE_OPERAND (expr, 1)) |
| 158 |
recurse_op[1] = true; |
| 159 |
break; |
| 160 |
case INDIRECT_REF: |
| 161 |
if (REFERENCE_REF_P (expr)) |
| 162 |
{ |
| 163 |
/* Try to look through the reference. */ |
| 164 |
tree ref = TREE_OPERAND (expr, 0); |
| 165 |
if (rvalue_p && is_normal_capture_proxy (ref)) |
| 166 |
{ |
| 167 |
/* Look through capture by reference. */ |
| 168 |
tree cap = DECL_CAPTURED_VARIABLE (ref); |
| 169 |
if (!TYPE_REF_P (TREE_TYPE (cap)) |
| 170 |
&& decl_constant_var_p (cap)) |
| 171 |
{ |
| 172 |
tree val = RECUR (cap); |
| 173 |
if (!is_capture_proxy (val)) |
| 174 |
{ |
| 175 |
tree l = current_lambda_expr (); |
| 176 |
LAMBDA_EXPR_CAPTURE_OPTIMIZED (l) = true; |
| 177 |
} |
| 178 |
return val; |
| 179 |
} |
| 180 |
} |
| 181 |
tree r = mark_rvalue_use (ref, loc, reject_builtin); |
| 182 |
if (r != ref) |
| 183 |
{ |
| 184 |
if (!rvalue_p) |
| 185 |
{ |
| 186 |
/* Make sure we still return an lvalue. */ |
| 187 |
gcc_assert (TREE_CODE (r) == NOP_EXPR); |
| 188 |
TREE_TYPE (r) = cp_build_reference_type (TREE_TYPE (r), |
| 189 |
false); |
| 190 |
} |
| 191 |
expr = convert_from_reference (r); |
| 192 |
} |
| 193 |
} |
| 194 |
break; |
| 195 |
|
| 196 |
case VIEW_CONVERT_EXPR: |
| 197 |
if (location_wrapper_p (expr)) |
| 198 |
{ |
| 199 |
loc = EXPR_LOCATION (expr); |
| 200 |
tree op = TREE_OPERAND (expr, 0); |
| 201 |
tree nop = RECUR (op); |
| 202 |
if (nop == error_mark_node) |
| 203 |
return error_mark_node; |
| 204 |
else if (op == nop) |
| 205 |
/* No change. */; |
| 206 |
else if (DECL_P (nop) || CONSTANT_CLASS_P (nop)) |
| 207 |
{ |
| 208 |
/* Reuse the location wrapper. */ |
| 209 |
TREE_OPERAND (expr, 0) = nop; |
| 210 |
/* If we're replacing a DECL with a constant, we also need to |
| 211 |
change the TREE_CODE of the location wrapper. */ |
| 212 |
if (rvalue_p) |
| 213 |
TREE_SET_CODE (expr, NON_LVALUE_EXPR); |
| 214 |
} |
| 215 |
else |
| 216 |
{ |
| 217 |
/* Drop the location wrapper. */ |
| 218 |
expr = nop; |
| 219 |
protected_set_expr_location (expr, loc); |
| 220 |
} |
| 221 |
return expr; |
| 222 |
} |
| 223 |
gcc_fallthrough(); |
| 224 |
CASE_CONVERT: |
| 225 |
recurse_op[0] = true; |
| 226 |
break; |
| 227 |
|
| 228 |
case MODIFY_EXPR: |
| 229 |
{ |
| 230 |
tree lhs = TREE_OPERAND (expr, 0); |
| 231 |
/* [expr.ass] "An assignment whose left operand is of |
| 232 |
a volatile-qualified type is deprecated unless the assignment |
| 233 |
is either a discarded-value expression or appears in an |
| 234 |
unevaluated context." */ |
| 235 |
if (!cp_unevaluated_operand |
| 236 |
&& (TREE_THIS_VOLATILE (lhs) |
| 237 |
|| CP_TYPE_VOLATILE_P (TREE_TYPE (lhs))) |
| 238 |
&& !TREE_THIS_VOLATILE (expr)) |
| 239 |
{ |
| 240 |
if (warning_at (location_of (expr), OPT_Wvolatile, |
| 241 |
"using value of assignment with " |
| 242 |
"%<volatile%>-qualified left operand is " |
| 243 |
"deprecated")) |
| 244 |
/* Make sure not to warn about this assignment again. */ |
| 245 |
TREE_THIS_VOLATILE (expr) = true; |
| 246 |
} |
| 247 |
break; |
| 248 |
} |
| 249 |
|
| 250 |
default: |
| 251 |
break; |
| 252 |
} |
| 253 |
|
| 254 |
for (int i = 0; i < 3; ++i) |
| 255 |
if (recurse_op[i]) |
| 256 |
{ |
| 257 |
tree op = TREE_OPERAND (expr, i); |
| 258 |
op = RECUR (op); |
| 259 |
if (op == error_mark_node) |
| 260 |
return error_mark_node; |
| 261 |
TREE_OPERAND (expr, i) = op; |
| 262 |
} |
| 263 |
|
| 264 |
return expr; |
| 265 |
#undef RECUR |
| 266 |
} |
| 267 |
|
| 268 |
/* Called whenever the expression EXPR is used in an rvalue context. |
| 269 |
When REJECT_BUILTIN is true the expression is checked to make sure |
| 270 |
it doesn't make it possible to obtain the address of a GCC built-in |
| 271 |
function with no library fallback (or any of its bits, such as in |
| 272 |
a conversion to bool). */ |
| 273 |
|
| 274 |
tree |
| 275 |
mark_rvalue_use (tree e, |
| 276 |
location_t loc /* = UNKNOWN_LOCATION */, |
| 277 |
bool reject_builtin /* = true */) |
| 278 |
{ |
| 279 |
return mark_use (e, true, true, loc, reject_builtin); |
| 280 |
} |
| 281 |
|
| 282 |
/* Called whenever an expression is used in an lvalue context. */ |
| 283 |
|
| 284 |
tree |
| 285 |
mark_lvalue_use (tree expr) |
| 286 |
{ |
| 287 |
return mark_use (expr, false, true, input_location, false); |
| 288 |
} |
| 289 |
|
| 290 |
/* As above, but don't consider this use a read. */ |
| 291 |
|
| 292 |
tree |
| 293 |
mark_lvalue_use_nonread (tree expr) |
| 294 |
{ |
| 295 |
return mark_use (expr, false, false, input_location, false); |
| 296 |
} |
| 297 |
|
| 298 |
/* Called when expr appears as a discarded-value expression. */ |
| 299 |
|
| 300 |
tree |
| 301 |
mark_discarded_use (tree expr) |
| 302 |
{ |
| 303 |
/* The lvalue-to-rvalue conversion (7.1) is applied if and only if the |
| 304 |
expression is a glvalue of volatile-qualified type and it is one of the |
| 305 |
following: |
| 306 |
* ( expression ), where expression is one of these expressions, |
| 307 |
* id-expression (8.1.4), |
| 308 |
* subscripting (8.2.1), |
| 309 |
* class member access (8.2.5), |
| 310 |
* indirection (8.3.1), |
| 311 |
* pointer-to-member operation (8.5), |
| 312 |
* conditional expression (8.16) where both the second and the third |
| 313 |
operands are one of these expressions, or |
| 314 |
* comma expression (8.19) where the right operand is one of these |
| 315 |
expressions. */ |
| 316 |
if (expr == NULL_TREE) |
| 317 |
return expr; |
| 318 |
|
| 319 |
STRIP_ANY_LOCATION_WRAPPER (expr); |
| 320 |
|
| 321 |
switch (TREE_CODE (expr)) |
| 322 |
{ |
| 323 |
case COND_EXPR: |
| 324 |
TREE_OPERAND (expr, 2) = mark_discarded_use (TREE_OPERAND (expr, 2)); |
| 325 |
gcc_fallthrough (); |
| 326 |
case COMPOUND_EXPR: |
| 327 |
TREE_OPERAND (expr, 1) = mark_discarded_use (TREE_OPERAND (expr, 1)); |
| 328 |
return expr; |
| 329 |
|
| 330 |
case COMPONENT_REF: |
| 331 |
case ARRAY_REF: |
| 332 |
case INDIRECT_REF: |
| 333 |
case MEMBER_REF: |
| 334 |
break; |
| 335 |
default: |
| 336 |
if (DECL_P (expr)) |
| 337 |
break; |
| 338 |
else |
| 339 |
return expr; |
| 340 |
} |
| 341 |
|
| 342 |
/* Like mark_rvalue_use, but don't reject built-ins. */ |
| 343 |
return mark_use (expr, true, true, input_location, false); |
| 344 |
} |
| 345 |
|
| 346 |
/* Called whenever an expression is used in a type use context. */ |
| 347 |
|
| 348 |
tree |
| 349 |
mark_type_use (tree expr) |
| 350 |
{ |
| 351 |
mark_exp_read (expr); |
| 352 |
return expr; |
| 353 |
} |
| 354 |
|
| 355 |
/* Mark EXP as read, not just set, for set but not used -Wunused |
| 356 |
warning purposes. */ |
| 357 |
|
| 358 |
void |
| 359 |
mark_exp_read (tree exp) |
| 360 |
{ |
| 361 |
if (exp == NULL) |
| 362 |
return; |
| 363 |
|
| 364 |
switch (TREE_CODE (exp)) |
| 365 |
{ |
| 366 |
case VAR_DECL: |
| 367 |
if (DECL_DECOMPOSITION_P (exp)) |
| 368 |
mark_exp_read (DECL_DECOMP_BASE (exp)); |
| 369 |
gcc_fallthrough (); |
| 370 |
case PARM_DECL: |
| 371 |
DECL_READ_P (exp) = 1; |
| 372 |
break; |
| 373 |
case ARRAY_REF: |
| 374 |
case COMPONENT_REF: |
| 375 |
case MODIFY_EXPR: |
| 376 |
case REALPART_EXPR: |
| 377 |
case IMAGPART_EXPR: |
| 378 |
CASE_CONVERT: |
| 379 |
case ADDR_EXPR: |
| 380 |
case INDIRECT_REF: |
| 381 |
case FLOAT_EXPR: |
| 382 |
case VIEW_CONVERT_EXPR: |
| 383 |
mark_exp_read (TREE_OPERAND (exp, 0)); |
| 384 |
break; |
| 385 |
case COMPOUND_EXPR: |
| 386 |
mark_exp_read (TREE_OPERAND (exp, 1)); |
| 387 |
break; |
| 388 |
case COND_EXPR: |
| 389 |
if (TREE_OPERAND (exp, 1)) |
| 390 |
mark_exp_read (TREE_OPERAND (exp, 1)); |
| 391 |
if (TREE_OPERAND (exp, 2)) |
| 392 |
mark_exp_read (TREE_OPERAND (exp, 2)); |
| 393 |
break; |
| 394 |
default: |
| 395 |
break; |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
/* Fold X for consideration by one of the warning functions when checking |
| 400 |
whether an expression has a constant value. */ |
| 401 |
|
| 402 |
tree |
| 403 |
fold_for_warn (tree x) |
| 404 |
{ |
| 405 |
/* C++ implementation. */ |
| 406 |
|
| 407 |
if (cp_unevaluated_operand) |
| 408 |
/* In an unevaluated context we don't care about the reduced value |
| 409 |
of an expression, so neither should any warnings. */ |
| 410 |
return x; |
| 411 |
|
| 412 |
/* Prevent warning-dependent constexpr evaluation from changing |
| 413 |
DECL_UID (which breaks -fcompare-debug) and from instantiating |
| 414 |
templates. */ |
| 415 |
uid_sensitive_constexpr_evaluation_sentinel s; |
| 416 |
|
| 417 |
/* It's not generally safe to fully fold inside of a template, so |
| 418 |
call fold_non_dependent_expr instead. */ |
| 419 |
if (processing_template_decl) |
| 420 |
{ |
| 421 |
tree f = fold_non_dependent_expr (x, tf_none); |
| 422 |
if (f == error_mark_node) |
| 423 |
return x; |
| 424 |
else |
| 425 |
return f; |
| 426 |
} |
| 427 |
else if (cxx_dialect >= cxx11) |
| 428 |
x = maybe_constant_value (x); |
| 429 |
|
| 430 |
return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL); |
| 431 |
} |