Line data Source code
1 : /* Convert language-specific tree expression to rtl instructions,
2 : for GNU compiler.
3 : Copyright (C) 1988-2026 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 95509453 : cplus_expand_constant (tree cst)
31 : {
32 95509453 : switch (TREE_CODE (cst))
33 : {
34 34463 : case PTRMEM_CST:
35 34463 : {
36 34463 : tree type = TREE_TYPE (cst);
37 34463 : tree member;
38 :
39 : /* Find the member. */
40 34463 : member = PTRMEM_CST_MEMBER (cst);
41 :
42 : /* We can't lower this until the class is complete. */
43 34463 : if (!COMPLETE_TYPE_P (DECL_CONTEXT (member)))
44 : return cst;
45 :
46 34427 : if (TREE_CODE (member) == FIELD_DECL)
47 : {
48 : /* Find the offset for the field. */
49 2895 : cst = byte_position (member);
50 5923 : 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 133 : member = lookup_anon_field (TYPE_PTRMEM_CLASS_TYPE (type),
57 133 : DECL_CONTEXT (member));
58 133 : cst = size_binop (PLUS_EXPR, cst, byte_position (member));
59 : }
60 2895 : cst = fold (build_nop (type, cst));
61 : }
62 : else
63 : {
64 31532 : tree delta;
65 31532 : tree pfn;
66 :
67 31532 : expand_ptrmemfunc_cst (cst, &delta, &pfn);
68 31532 : 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 70729090 : FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (cst), idx, elt)
78 65633700 : 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 6624532033 : 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 6624532033 : if (expr == NULL_TREE || error_operand_p (expr))
100 : return expr;
101 :
102 6624527029 : if (reject_builtin && reject_gcc_builtin (expr, loc))
103 67 : return error_mark_node;
104 :
105 6624526962 : if (TREE_TYPE (expr) && VOID_TYPE_P (TREE_TYPE (expr)))
106 : read_p = false;
107 :
108 6507499723 : if (read_p)
109 6438591522 : mark_exp_read (expr);
110 :
111 6624526962 : tree oexpr = expr;
112 6624526962 : bool recurse_op[3] = { false, false, false };
113 6624526962 : switch (TREE_CODE (expr))
114 : {
115 1680075698 : case VAR_DECL:
116 1680075698 : case PARM_DECL:
117 1680075698 : if (rvalue_p && is_normal_capture_proxy (expr))
118 : {
119 : /* Look through capture by copy. */
120 6318119 : tree cap = DECL_CAPTURED_VARIABLE (expr);
121 6318119 : if (TREE_CODE (TREE_TYPE (cap)) == TREE_CODE (TREE_TYPE (expr))
122 6318119 : && decl_constant_var_p (cap))
123 : {
124 184 : tree val = RECUR (cap);
125 184 : if (!is_capture_proxy (val))
126 : {
127 145 : tree l = current_lambda_expr ();
128 145 : LAMBDA_EXPR_CAPTURE_OPTIMIZED (l) = true;
129 : }
130 184 : return val;
131 : }
132 : }
133 1680075514 : if (outer_automatic_var_p (expr)
134 1680075514 : && decl_constant_var_p (expr))
135 : {
136 86234 : if (rvalue_p)
137 : {
138 86225 : tree t = maybe_constant_value (expr);
139 86225 : if (TREE_CONSTANT (t))
140 : {
141 : expr = t;
142 6084478381 : break;
143 : }
144 : }
145 69 : iloc_sentinel l (loc);
146 69 : expr = process_outer_var_ref (expr, tf_warning_or_error, true);
147 69 : if (!(TREE_TYPE (oexpr)
148 69 : && TYPE_REF_P (TREE_TYPE (oexpr))))
149 51 : expr = convert_from_reference (expr);
150 69 : }
151 : break;
152 149570112 : case COMPONENT_REF:
153 149570112 : recurse_op[0] = true;
154 149570112 : break;
155 13957522 : case COMPOUND_EXPR:
156 13957522 : recurse_op[1] = true;
157 13957522 : break;
158 17962469 : case COND_EXPR:
159 17962469 : recurse_op[2] = true;
160 17962469 : if (TREE_OPERAND (expr, 1))
161 17962430 : recurse_op[1] = true;
162 : break;
163 358375722 : case INDIRECT_REF:
164 358375722 : if (REFERENCE_REF_P (expr))
165 : {
166 : /* Try to look through the reference. */
167 200418219 : tree ref = TREE_OPERAND (expr, 0);
168 200418219 : if (rvalue_p && is_normal_capture_proxy (ref))
169 : {
170 : /* Look through capture by reference. */
171 713054 : tree cap = DECL_CAPTURED_VARIABLE (ref);
172 713054 : if (!TYPE_REF_P (TREE_TYPE (cap))
173 713054 : && decl_constant_var_p (cap))
174 : {
175 1733 : tree val = RECUR (cap);
176 1733 : if (!is_capture_proxy (val))
177 : {
178 1733 : tree l = current_lambda_expr ();
179 1733 : LAMBDA_EXPR_CAPTURE_OPTIMIZED (l) = true;
180 : }
181 1733 : return val;
182 : }
183 : }
184 200416486 : tree r = mark_rvalue_use (ref, loc, reject_builtin);
185 200416486 : if (r != ref)
186 : {
187 21 : if (!rvalue_p)
188 : {
189 : /* Make sure we still return an lvalue. */
190 12 : gcc_assert (TREE_CODE (r) == NOP_EXPR);
191 12 : TREE_TYPE (r) = cp_build_reference_type (TREE_TYPE (r),
192 : false);
193 : }
194 21 : expr = convert_from_reference (r);
195 : }
196 : }
197 : break;
198 :
199 549536448 : case VIEW_CONVERT_EXPR:
200 549536448 : if (location_wrapper_p (expr))
201 : {
202 540046664 : loc = EXPR_LOCATION (expr);
203 540046664 : tree op = TREE_OPERAND (expr, 0);
204 540046664 : tree nop = RECUR (op);
205 540046664 : if (nop == error_mark_node)
206 : return error_mark_node;
207 540046652 : else if (op == nop)
208 : /* No change. */;
209 610 : else if (DECL_P (nop) || CONSTANT_CLASS_P (nop))
210 : {
211 : /* Reuse the location wrapper. */
212 581 : TREE_OPERAND (expr, 0) = nop;
213 : /* If we're replacing a DECL with a constant, we also need to
214 : change the TREE_CODE of the location wrapper. */
215 581 : if (rvalue_p)
216 581 : TREE_SET_CODE (expr, NON_LVALUE_EXPR);
217 : }
218 : else
219 : {
220 : /* Drop the location wrapper. */
221 29 : expr = nop;
222 29 : protected_set_expr_location (expr, loc);
223 : }
224 540046652 : return expr;
225 : }
226 784601717 : gcc_fallthrough ();
227 784601717 : CASE_CONVERT:
228 784601717 : recurse_op[0] = true;
229 784601717 : break;
230 :
231 2962273 : case MODIFY_EXPR:
232 2962273 : {
233 2962273 : tree lhs = TREE_OPERAND (expr, 0);
234 : /* [expr.ass] "An assignment whose left operand is of
235 : a volatile-qualified type is deprecated unless the assignment
236 : is either a discarded-value expression or appears in an
237 : unevaluated context." */
238 2962273 : if (!cp_unevaluated_operand
239 2962147 : && (TREE_THIS_VOLATILE (lhs)
240 2961180 : || CP_TYPE_VOLATILE_P (TREE_TYPE (lhs)))
241 2963240 : && !TREE_THIS_VOLATILE (expr))
242 : {
243 581 : if (warning_at (location_of (expr), OPT_Wvolatile,
244 : "using value of assignment with "
245 : "%<volatile%>-qualified left operand is "
246 : "deprecated"))
247 : /* Make sure not to warn about this assignment again. */
248 90 : TREE_THIS_VOLATILE (expr) = true;
249 : }
250 : break;
251 : }
252 :
253 : default:
254 : break;
255 : }
256 :
257 24337913506 : for (int i = 0; i < 3; ++i)
258 18253435131 : if (recurse_op[i])
259 : {
260 984054250 : tree op = TREE_OPERAND (expr, i);
261 984054250 : op = RECUR (op);
262 984054250 : if (op == error_mark_node)
263 : return error_mark_node;
264 984054244 : TREE_OPERAND (expr, i) = op;
265 : }
266 :
267 : return expr;
268 : #undef RECUR
269 : }
270 :
271 : /* Called whenever the expression EXPR is used in an rvalue context.
272 : When REJECT_BUILTIN is true the expression is checked to make sure
273 : it doesn't make it possible to obtain the address of a GCC built-in
274 : function with no library fallback (or any of its bits, such as in
275 : a conversion to bool). */
276 :
277 : tree
278 3930460841 : mark_rvalue_use (tree e,
279 : location_t loc /* = UNKNOWN_LOCATION */,
280 : bool reject_builtin /* = true */)
281 : {
282 3930460841 : return mark_use (e, true, true, loc, reject_builtin);
283 : }
284 :
285 : /* Called whenever an expression is used in an lvalue context. */
286 :
287 : tree
288 397321445 : mark_lvalue_use (tree expr)
289 : {
290 397321445 : return mark_use (expr, false, true, input_location, false);
291 : }
292 :
293 : /* As above, but don't consider this use a read. */
294 :
295 : tree
296 36949069 : mark_lvalue_use_nonread (tree expr)
297 : {
298 36949069 : return mark_use (expr, false, false, input_location, false);
299 : }
300 :
301 : /* Called when expr appears as a discarded-value expression. */
302 :
303 : tree
304 73138423 : mark_discarded_use (tree expr)
305 : {
306 : /* The lvalue-to-rvalue conversion (7.1) is applied if and only if the
307 : expression is a glvalue of volatile-qualified type and it is one of the
308 : following:
309 : * ( expression ), where expression is one of these expressions,
310 : * id-expression (8.1.4),
311 : * subscripting (8.2.1),
312 : * class member access (8.2.5),
313 : * indirection (8.3.1),
314 : * pointer-to-member operation (8.5),
315 : * conditional expression (8.16) where both the second and the third
316 : operands are one of these expressions, or
317 : * comma expression (8.19) where the right operand is one of these
318 : expressions. */
319 73138423 : if (expr == NULL_TREE)
320 : return expr;
321 :
322 73138417 : STRIP_ANY_LOCATION_WRAPPER (expr);
323 :
324 73138417 : switch (TREE_CODE (expr))
325 : {
326 121744 : case COND_EXPR:
327 121744 : TREE_OPERAND (expr, 2) = mark_discarded_use (TREE_OPERAND (expr, 2));
328 589496 : gcc_fallthrough ();
329 589496 : case COMPOUND_EXPR:
330 589496 : TREE_OPERAND (expr, 1) = mark_discarded_use (TREE_OPERAND (expr, 1));
331 589496 : return expr;
332 :
333 : case COMPONENT_REF:
334 : case ARRAY_REF:
335 : case INDIRECT_REF:
336 : case MEMBER_REF:
337 : break;
338 65136471 : default:
339 65136471 : if (DECL_P (expr))
340 : break;
341 : else
342 : return expr;
343 : }
344 :
345 : /* Like mark_rvalue_use, but don't reject built-ins. */
346 7440845 : return mark_use (expr, true, true, input_location, false);
347 : }
348 :
349 : /* Called whenever an expression is used in a type use context. */
350 :
351 : tree
352 114643829 : mark_type_use (tree expr)
353 : {
354 114643829 : mark_exp_read (expr);
355 114643829 : return expr;
356 : }
357 :
358 : /* Mark EXP as read, not just set, for set but not used -Wunused
359 : warning purposes. */
360 :
361 : void
362 6975060266 : mark_exp_read (tree exp)
363 : {
364 10455219281 : if (exp == NULL)
365 : return;
366 :
367 10450887956 : if (TREE_TYPE (exp) && VOID_TYPE_P (TREE_TYPE (exp)))
368 : return;
369 :
370 10320544078 : switch (TREE_CODE (exp))
371 : {
372 1178591205 : case VAR_DECL:
373 1178591205 : if (DECL_DECOMPOSITION_P (exp))
374 7487424 : mark_exp_read (DECL_DECOMP_BASE (exp));
375 3437332600 : gcc_fallthrough ();
376 3437332600 : case PARM_DECL:
377 3437332600 : DECL_READ_P (exp) = 1;
378 3437332600 : break;
379 3432724116 : CASE_CONVERT:
380 3432724116 : case ARRAY_REF:
381 3432724116 : case COMPONENT_REF:
382 3432724116 : case MODIFY_EXPR:
383 3432724116 : case REALPART_EXPR:
384 3432724116 : case IMAGPART_EXPR:
385 3432724116 : case ADDR_EXPR:
386 3432724116 : case INDIRECT_REF:
387 3432724116 : case FLOAT_EXPR:
388 3432724116 : case VIEW_CONVERT_EXPR:
389 3432724116 : case PREINCREMENT_EXPR:
390 3432724116 : case PREDECREMENT_EXPR:
391 3432724116 : case POSTINCREMENT_EXPR:
392 3432724116 : case POSTDECREMENT_EXPR:
393 3432724116 : mark_exp_read (TREE_OPERAND (exp, 0));
394 3432724116 : break;
395 22672156 : case COMPOUND_EXPR:
396 22672156 : mark_exp_read (TREE_OPERAND (exp, 1));
397 22672156 : break;
398 24762743 : case COND_EXPR:
399 24762743 : if (TREE_OPERAND (exp, 1))
400 24762694 : mark_exp_read (TREE_OPERAND (exp, 1));
401 24762743 : if (TREE_OPERAND (exp, 2))
402 24762743 : mark_exp_read (TREE_OPERAND (exp, 2));
403 : break;
404 : default:
405 : break;
406 : }
407 : }
408 :
409 : /* Fold X for consideration by one of the warning functions when checking
410 : whether an expression has a constant value. */
411 :
412 : tree
413 76458843 : fold_for_warn (tree x)
414 : {
415 : /* C++ implementation. */
416 :
417 76458843 : if (cp_unevaluated_operand)
418 : /* In an unevaluated context we don't care about the reduced value
419 : of an expression, so neither should any warnings. */
420 : return x;
421 :
422 : /* Prevent warning-dependent constexpr evaluation from changing
423 : DECL_UID (which breaks -fcompare-debug) and from instantiating
424 : templates. */
425 76406324 : uid_sensitive_constexpr_evaluation_sentinel s;
426 :
427 : /* It's not generally safe to fully fold inside of a template, so
428 : call fold_non_dependent_expr instead. */
429 76406324 : if (processing_template_decl)
430 : {
431 3789575 : tree f = fold_non_dependent_expr (x, tf_none);
432 3789575 : if (f == error_mark_node)
433 : return x;
434 : else
435 3789573 : return f;
436 : }
437 72616749 : else if (cxx_dialect >= cxx11)
438 72145159 : x = maybe_constant_value (x);
439 :
440 72616749 : return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
441 76406324 : }
442 :
443 : /* Make EXPR only execute during constant evaluation by wrapping it in a
444 : statement-expression containing 'if consteval'. */
445 :
446 : tree
447 1 : wrap_with_if_consteval (tree expr)
448 : {
449 1 : tree stmtex = begin_stmt_expr ();
450 1 : tree ifcev = begin_if_stmt ();
451 1 : IF_STMT_CONSTEVAL_P (ifcev) = true;
452 1 : finish_if_stmt_cond (boolean_false_node, ifcev);
453 1 : finish_expr_stmt (expr);
454 1 : finish_then_clause (ifcev);
455 1 : finish_if_stmt (ifcev);
456 1 : return finish_stmt_expr (stmtex, /*no scope*/true);
457 : }
|