Line data Source code
1 : // Copyright (C) 2021-2026 Free Software Foundation, Inc.
2 :
3 : // This file is part of GCC.
4 :
5 : // GCC is free software; you can redistribute it and/or modify it under
6 : // the terms of the GNU General Public License as published by the Free
7 : // Software Foundation; either version 3, or (at your option) any later
8 : // version.
9 :
10 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : // for more details.
14 :
15 : // You should have received a copy of the GNU General Public License
16 : // along with GCC; see the file COPYING3. If not see
17 : // <http://www.gnu.org/licenses/>.
18 :
19 : #ifndef RUST_HIR_LIVENESS
20 : #define RUST_HIR_LIVENESS
21 :
22 : #include "rust-hir-full-decls.h"
23 : #include "rust-hir-map.h"
24 : #include "rust-lint-marklive-base.h"
25 : #include "rust-name-resolver.h"
26 : #include "rust-finalized-name-resolution-context.h"
27 :
28 : namespace Rust {
29 : namespace Analysis {
30 :
31 : class MarkLive : public MarkLiveBase
32 : {
33 : using Rust::Analysis::MarkLiveBase::visit;
34 :
35 : public:
36 : static std::set<HirId> Analysis (HIR::Crate &crate);
37 : void go (HIR::Crate &crate);
38 :
39 : void visit (HIR::PathInExpression &expr) override;
40 : void visit (HIR::FieldAccessExpr &expr) override;
41 : void visit (HIR::TupleIndexExpr &expr) override;
42 : void visit (HIR::MethodCallExpr &expr) override;
43 : void visit (HIR::TypeAlias &alias) override;
44 :
45 2307 : void visit (HIR::BorrowExpr &expr) override
46 : {
47 2307 : expr.get_expr ().accept_vis (*this);
48 2307 : }
49 :
50 458 : void visit (HIR::DereferenceExpr &expr) override
51 : {
52 458 : expr.get_expr ().accept_vis (*this);
53 458 : }
54 :
55 791 : void visit (HIR::NegationExpr &expr) override
56 : {
57 791 : expr.get_expr ().accept_vis (*this);
58 791 : }
59 :
60 1582 : void visit (HIR::LazyBooleanExpr &expr) override
61 : {
62 1582 : expr.get_lhs ().accept_vis (*this);
63 1582 : expr.get_rhs ().accept_vis (*this);
64 1582 : }
65 :
66 10489 : void visit (HIR::TypeCastExpr &expr) override
67 : {
68 10489 : expr.get_expr ().accept_vis (*this);
69 10489 : }
70 :
71 458 : void visit (HIR::GroupedExpr &expr) override
72 : {
73 458 : expr.get_expr_in_parens ().accept_vis (*this);
74 458 : }
75 :
76 369 : void visit (HIR::ArrayExpr &expr) override
77 : {
78 369 : expr.get_internal_elements ().accept_vis (*this);
79 369 : }
80 :
81 397 : void visit (HIR::ArrayIndexExpr &expr) override
82 : {
83 397 : expr.get_array_expr ().accept_vis (*this);
84 397 : expr.get_index_expr ().accept_vis (*this);
85 397 : }
86 :
87 279 : void visit (HIR::ArrayElemsValues &expr) override
88 : {
89 1705 : for (auto &elem : expr.get_values ())
90 : {
91 1426 : elem->accept_vis (*this);
92 : }
93 279 : }
94 :
95 545 : void visit (HIR::TupleExpr &expr) override
96 : {
97 1397 : for (auto &elem : expr.get_tuple_elems ())
98 : {
99 852 : elem->accept_vis (*this);
100 : }
101 545 : }
102 :
103 24616 : void visit (HIR::BlockExpr &expr) override
104 : {
105 58484 : for (auto &s : expr.get_statements ())
106 : {
107 33868 : s->accept_vis (*this);
108 : }
109 24616 : if (expr.has_expr ())
110 : {
111 13973 : expr.get_final_expr ().accept_vis (*this);
112 : }
113 24616 : }
114 :
115 5563 : void visit (HIR::UnsafeBlockExpr &expr) override
116 : {
117 5563 : expr.get_block_expr ().accept_vis (*this);
118 5563 : }
119 :
120 207 : void visit (HIR::LoopExpr &expr) override
121 : {
122 207 : expr.get_loop_block ().accept_vis (*this);
123 207 : }
124 :
125 183 : void visit (HIR::BreakExpr &expr) override
126 : {
127 183 : if (expr.has_break_expr ())
128 20 : expr.get_expr ().accept_vis (*this);
129 183 : }
130 :
131 151 : void visit (HIR::WhileLoopExpr &expr) override
132 : {
133 151 : expr.get_loop_block ().accept_vis (*this);
134 151 : expr.get_predicate_expr ().accept_vis (*this);
135 151 : }
136 :
137 12758 : void visit (HIR::Function &function) override
138 : {
139 12758 : function.get_definition ().accept_vis (*this);
140 12758 : }
141 :
142 9 : void visit (HIR::BoxExpr &expr) override
143 : {
144 9 : expr.get_expr ().accept_vis (*this);
145 9 : }
146 :
147 1750 : void visit (HIR::ReturnExpr &expr) override
148 : {
149 1750 : if (expr.has_return_expr ())
150 1724 : expr.get_expr ().accept_vis (*this);
151 1750 : }
152 :
153 0 : void visit (HIR::WhileLetLoopExpr &expr) override
154 : {
155 0 : expr.get_loop_block ().accept_vis (*this);
156 0 : expr.get_cond ().accept_vis (*this);
157 0 : }
158 :
159 16489 : void visit (HIR::ExprStmt &stmt) override
160 : {
161 16489 : stmt.get_expr ().accept_vis (*this);
162 16489 : }
163 :
164 15500 : void visit (HIR::CallExpr &expr) override
165 : {
166 15500 : expr.get_fnexpr ().accept_vis (*this);
167 32650 : for (auto &argument : expr.get_arguments ())
168 17150 : argument->accept_vis (*this);
169 15500 : }
170 :
171 3281 : void visit (HIR::ArithmeticOrLogicalExpr &expr) override
172 : {
173 3281 : expr.visit_lhs (*this);
174 3281 : expr.visit_rhs (*this);
175 3281 : }
176 4715 : void visit (HIR::ComparisonExpr &expr) override
177 : {
178 4715 : expr.get_lhs ().accept_vis (*this);
179 4715 : expr.get_rhs ().accept_vis (*this);
180 4715 : }
181 :
182 2601 : void visit (HIR::AssignmentExpr &expr) override
183 : {
184 2601 : expr.visit_lhs (*this);
185 2601 : expr.visit_rhs (*this);
186 2601 : }
187 :
188 688 : void visit (HIR::CompoundAssignmentExpr &expr) override
189 : {
190 688 : expr.visit_lhs (*this);
191 688 : expr.visit_rhs (*this);
192 688 : }
193 :
194 2159 : void visit (HIR::IfExpr &expr) override
195 : {
196 2159 : expr.get_if_condition ().accept_vis (*this);
197 2159 : expr.get_if_block ().accept_vis (*this);
198 2159 : }
199 :
200 881 : void visit (HIR::IfExprConseqElse &expr) override
201 : {
202 881 : expr.get_if_condition ().accept_vis (*this);
203 881 : expr.get_if_block ().accept_vis (*this);
204 881 : expr.get_else_block ().accept_vis (*this);
205 881 : }
206 :
207 1632 : void visit (HIR::MatchExpr &expr) override
208 : {
209 1632 : expr.get_scrutinee_expr ().accept_vis (*this);
210 1632 : std::vector<HIR::MatchCase> &cases = expr.get_match_cases ();
211 6033 : for (auto &&caz : cases)
212 : {
213 4401 : auto case_arm = caz.get_arm ();
214 4401 : if (case_arm.has_match_arm_guard ())
215 345 : case_arm.get_guard_expr ().accept_vis (*this);
216 4401 : caz.get_expr ().accept_vis (*this);
217 4401 : }
218 1632 : }
219 :
220 0 : void visit (HIR::TraitItemFunc &item) override
221 : {
222 0 : item.get_block_expr ().accept_vis (*this);
223 0 : }
224 :
225 206 : void visit (HIR::ImplBlock &impl) override
226 : {
227 412 : for (auto &&item : impl.get_impl_items ())
228 : {
229 206 : item->accept_vis (*this);
230 : }
231 206 : }
232 :
233 16962 : void visit (HIR::LetStmt &stmt) override
234 : {
235 16962 : if (stmt.has_init_expr ())
236 : {
237 15769 : stmt.get_init_expr ().accept_vis (*this);
238 : }
239 16962 : }
240 :
241 77 : void visit (HIR::StructExprStruct &stct) override
242 : {
243 77 : stct.get_struct_name ().accept_vis (*this);
244 77 : }
245 :
246 1381 : void visit (HIR::StructExprStructFields &stct) override
247 : {
248 4258 : for (auto &field : stct.get_fields ())
249 : {
250 2877 : field->accept_vis (*this);
251 : }
252 :
253 1381 : stct.get_struct_name ().accept_vis (*this);
254 1381 : if (stct.has_struct_base ())
255 : {
256 63 : stct.get_struct_base ().get_base ().accept_vis (*this);
257 : }
258 1381 : }
259 :
260 2719 : virtual void visit (HIR::StructExprFieldIdentifierValue &field) override
261 : {
262 2719 : field.get_value ().accept_vis (*this);
263 2719 : }
264 :
265 0 : void visit (HIR::StructExprStructBase &stct) override
266 : {
267 0 : stct.get_struct_base ().get_base ().accept_vis (*this);
268 0 : }
269 :
270 635 : void visit (HIR::Module &module) override
271 : {
272 1992 : for (auto &item : module.get_items ())
273 1357 : item->accept_vis (*this);
274 635 : }
275 :
276 66 : void visit (HIR::ClosureExpr &expr) override
277 : {
278 66 : expr.get_expr ().accept_vis (*this);
279 66 : }
280 :
281 : private:
282 : std::vector<HirId> worklist;
283 : std::set<HirId> liveSymbols;
284 : std::set<HirId> scannedSymbols;
285 : Analysis::Mappings &mappings;
286 : const Resolver2_0::FinalizedNameResolutionContext &resolver;
287 : Resolver::TypeCheckContext *tyctx;
288 4628 : MarkLive (std::vector<HirId> worklist)
289 4628 : : worklist (worklist), mappings (Analysis::Mappings::get ()),
290 4628 : resolver (Resolver2_0::FinalizedNameResolutionContext::get ()),
291 9256 : tyctx (Resolver::TypeCheckContext::get ()){};
292 :
293 : void mark_hir_id (HirId);
294 : bool visit_path_segment (HIR::PathExprSegment);
295 : void find_value_definition (NodeId ast_node_id, NodeId &ref_node_id);
296 : };
297 :
298 : } // namespace Analysis
299 : } // namespace Rust
300 :
301 : #endif
|