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 1715 : void visit (HIR::BorrowExpr &expr) override
46 : {
47 1715 : expr.get_expr ().accept_vis (*this);
48 1715 : }
49 :
50 409 : void visit (HIR::DereferenceExpr &expr) override
51 : {
52 409 : expr.get_expr ().accept_vis (*this);
53 409 : }
54 :
55 455 : void visit (HIR::NegationExpr &expr) override
56 : {
57 455 : expr.get_expr ().accept_vis (*this);
58 455 : }
59 :
60 921 : void visit (HIR::LazyBooleanExpr &expr) override
61 : {
62 921 : expr.get_lhs ().accept_vis (*this);
63 921 : expr.get_rhs ().accept_vis (*this);
64 921 : }
65 :
66 10216 : void visit (HIR::TypeCastExpr &expr) override
67 : {
68 10216 : expr.get_expr ().accept_vis (*this);
69 10216 : }
70 :
71 423 : void visit (HIR::GroupedExpr &expr) override
72 : {
73 423 : expr.get_expr_in_parens ().accept_vis (*this);
74 423 : }
75 :
76 362 : void visit (HIR::ArrayExpr &expr) override
77 : {
78 362 : expr.get_internal_elements ().accept_vis (*this);
79 362 : }
80 :
81 391 : void visit (HIR::ArrayIndexExpr &expr) override
82 : {
83 391 : expr.get_array_expr ().accept_vis (*this);
84 391 : expr.get_index_expr ().accept_vis (*this);
85 391 : }
86 :
87 274 : void visit (HIR::ArrayElemsValues &expr) override
88 : {
89 1683 : for (auto &elem : expr.get_values ())
90 : {
91 1409 : elem->accept_vis (*this);
92 : }
93 274 : }
94 :
95 500 : void visit (HIR::TupleExpr &expr) override
96 : {
97 1273 : for (auto &elem : expr.get_tuple_elems ())
98 : {
99 773 : elem->accept_vis (*this);
100 : }
101 500 : }
102 :
103 21974 : void visit (HIR::BlockExpr &expr) override
104 : {
105 52031 : for (auto &s : expr.get_statements ())
106 : {
107 30057 : s->accept_vis (*this);
108 : }
109 21974 : if (expr.has_expr ())
110 : {
111 12551 : expr.get_final_expr ().accept_vis (*this);
112 : }
113 21974 : }
114 :
115 5142 : void visit (HIR::UnsafeBlockExpr &expr) override
116 : {
117 5142 : expr.get_block_expr ().accept_vis (*this);
118 5142 : }
119 :
120 131 : void visit (HIR::LoopExpr &expr) override
121 : {
122 131 : expr.get_loop_block ().accept_vis (*this);
123 131 : }
124 :
125 90 : void visit (HIR::BreakExpr &expr) override
126 : {
127 90 : if (expr.has_break_expr ())
128 18 : expr.get_expr ().accept_vis (*this);
129 90 : }
130 :
131 132 : void visit (HIR::WhileLoopExpr &expr) override
132 : {
133 132 : expr.get_loop_block ().accept_vis (*this);
134 132 : expr.get_predicate_expr ().accept_vis (*this);
135 132 : }
136 :
137 11588 : void visit (HIR::Function &function) override
138 : {
139 11588 : function.get_definition ().accept_vis (*this);
140 11588 : }
141 :
142 2 : void visit (HIR::BoxExpr &expr) override
143 : {
144 2 : expr.get_expr ().accept_vis (*this);
145 2 : }
146 :
147 1040 : void visit (HIR::ReturnExpr &expr) override
148 : {
149 1040 : if (expr.has_return_expr ())
150 1020 : expr.get_expr ().accept_vis (*this);
151 1040 : }
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 14046 : void visit (HIR::ExprStmt &stmt) override
160 : {
161 14046 : stmt.get_expr ().accept_vis (*this);
162 14046 : }
163 :
164 14000 : void visit (HIR::CallExpr &expr) override
165 : {
166 14000 : expr.get_fnexpr ().accept_vis (*this);
167 29087 : for (auto &argument : expr.get_arguments ())
168 15087 : argument->accept_vis (*this);
169 14000 : }
170 :
171 2932 : void visit (HIR::ArithmeticOrLogicalExpr &expr) override
172 : {
173 2932 : expr.visit_lhs (*this);
174 2932 : expr.visit_rhs (*this);
175 2932 : }
176 3093 : void visit (HIR::ComparisonExpr &expr) override
177 : {
178 3093 : expr.get_lhs ().accept_vis (*this);
179 3093 : expr.get_rhs ().accept_vis (*this);
180 3093 : }
181 :
182 2375 : void visit (HIR::AssignmentExpr &expr) override
183 : {
184 2375 : expr.visit_lhs (*this);
185 2375 : expr.visit_rhs (*this);
186 2375 : }
187 :
188 533 : void visit (HIR::CompoundAssignmentExpr &expr) override
189 : {
190 533 : expr.visit_lhs (*this);
191 533 : expr.visit_rhs (*this);
192 533 : }
193 :
194 1367 : void visit (HIR::IfExpr &expr) override
195 : {
196 1367 : expr.get_if_condition ().accept_vis (*this);
197 1367 : expr.get_if_block ().accept_vis (*this);
198 1367 : }
199 :
200 825 : void visit (HIR::IfExprConseqElse &expr) override
201 : {
202 825 : expr.get_if_condition ().accept_vis (*this);
203 825 : expr.get_if_block ().accept_vis (*this);
204 825 : expr.get_else_block ().accept_vis (*this);
205 825 : }
206 :
207 1297 : void visit (HIR::MatchExpr &expr) override
208 : {
209 1297 : expr.get_scrutinee_expr ().accept_vis (*this);
210 1297 : std::vector<HIR::MatchCase> &cases = expr.get_match_cases ();
211 4650 : for (auto &&caz : cases)
212 : {
213 3353 : auto case_arm = caz.get_arm ();
214 3353 : if (case_arm.has_match_arm_guard ())
215 0 : case_arm.get_guard_expr ().accept_vis (*this);
216 3353 : caz.get_expr ().accept_vis (*this);
217 3353 : }
218 1297 : }
219 :
220 0 : void visit (HIR::TraitItemFunc &item) override
221 : {
222 0 : item.get_block_expr ().accept_vis (*this);
223 0 : }
224 :
225 193 : void visit (HIR::ImplBlock &impl) override
226 : {
227 386 : for (auto &&item : impl.get_impl_items ())
228 : {
229 193 : item->accept_vis (*this);
230 : }
231 193 : }
232 :
233 15597 : void visit (HIR::LetStmt &stmt) override
234 : {
235 15597 : if (stmt.has_init_expr ())
236 : {
237 14498 : stmt.get_init_expr ().accept_vis (*this);
238 : }
239 15597 : }
240 :
241 77 : void visit (HIR::StructExprStruct &stct) override
242 : {
243 77 : stct.get_struct_name ().accept_vis (*this);
244 77 : }
245 :
246 1297 : void visit (HIR::StructExprStructFields &stct) override
247 : {
248 4077 : for (auto &field : stct.get_fields ())
249 : {
250 2780 : field->accept_vis (*this);
251 : }
252 :
253 1297 : stct.get_struct_name ().accept_vis (*this);
254 1297 : if (stct.has_struct_base ())
255 : {
256 63 : stct.get_struct_base ().get_base ().accept_vis (*this);
257 : }
258 1297 : }
259 :
260 2626 : virtual void visit (HIR::StructExprFieldIdentifierValue &field) override
261 : {
262 2626 : field.get_value ().accept_vis (*this);
263 2626 : }
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 845 : void visit (HIR::Module &module) override
271 : {
272 2992 : for (auto &item : module.get_items ())
273 2147 : item->accept_vis (*this);
274 845 : }
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 4244 : MarkLive (std::vector<HirId> worklist)
289 4244 : : worklist (worklist), mappings (Analysis::Mappings::get ()),
290 4244 : resolver (Resolver2_0::FinalizedNameResolutionContext::get ()),
291 8488 : 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
|