| 1 |
// Copyright (C) 2021-2025 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 |
|
| 27 |
namespace Rust { |
| 28 |
namespace Analysis { |
| 29 |
|
| 30 |
class MarkLive : public MarkLiveBase |
| 31 |
{ |
| 32 |
using Rust::Analysis::MarkLiveBase::visit; |
| 33 |
|
| 34 |
public: |
| 35 |
static std::set<HirId> Analysis (HIR::Crate &crate); |
| 36 |
void go (HIR::Crate &crate); |
| 37 |
|
| 38 |
void visit (HIR::PathInExpression &expr) override; |
| 39 |
void visit (HIR::FieldAccessExpr &expr) override; |
| 40 |
void visit (HIR::TupleIndexExpr &expr) override; |
| 41 |
void visit (HIR::MethodCallExpr &expr) override; |
| 42 |
void visit (HIR::TypeAlias &alias) override; |
| 43 |
|
| 44 |
void visit (HIR::BorrowExpr &expr) override |
| 45 |
{ |
| 46 |
expr.get_expr ().accept_vis (*this); |
| 47 |
} |
| 48 |
|
| 49 |
void visit (HIR::DereferenceExpr &expr) override |
| 50 |
{ |
| 51 |
expr.get_expr ().accept_vis (*this); |
| 52 |
} |
| 53 |
|
| 54 |
void visit (HIR::NegationExpr &expr) override |
| 55 |
{ |
| 56 |
expr.get_expr ().accept_vis (*this); |
| 57 |
} |
| 58 |
|
| 59 |
void visit (HIR::LazyBooleanExpr &expr) override |
| 60 |
{ |
| 61 |
expr.get_lhs ().accept_vis (*this); |
| 62 |
expr.get_rhs ().accept_vis (*this); |
| 63 |
} |
| 64 |
|
| 65 |
void visit (HIR::TypeCastExpr &expr) override |
| 66 |
{ |
| 67 |
expr.get_expr ().accept_vis (*this); |
| 68 |
} |
| 69 |
|
| 70 |
void visit (HIR::GroupedExpr &expr) override |
| 71 |
{ |
| 72 |
expr.get_expr_in_parens ().accept_vis (*this); |
| 73 |
} |
| 74 |
|
| 75 |
void visit (HIR::ArrayExpr &expr) override |
| 76 |
{ |
| 77 |
expr.get_internal_elements ().accept_vis (*this); |
| 78 |
} |
| 79 |
|
| 80 |
void visit (HIR::ArrayIndexExpr &expr) override |
| 81 |
{ |
| 82 |
expr.get_array_expr ().accept_vis (*this); |
| 83 |
expr.get_index_expr ().accept_vis (*this); |
| 84 |
} |
| 85 |
|
| 86 |
void visit (HIR::ArrayElemsValues &expr) override |
| 87 |
{ |
| 88 |
for (auto &elem : expr.get_values ()) |
| 89 |
{ |
| 90 |
elem->accept_vis (*this); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
void visit (HIR::TupleExpr &expr) override |
| 95 |
{ |
| 96 |
for (auto &elem : expr.get_tuple_elems ()) |
| 97 |
{ |
| 98 |
elem->accept_vis (*this); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
void visit (HIR::BlockExpr &expr) override |
| 103 |
{ |
| 104 |
for (auto &s : expr.get_statements ()) |
| 105 |
{ |
| 106 |
s->accept_vis (*this); |
| 107 |
} |
| 108 |
if (expr.has_expr ()) |
| 109 |
{ |
| 110 |
expr.get_final_expr ().accept_vis (*this); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
void visit (HIR::UnsafeBlockExpr &expr) override |
| 115 |
{ |
| 116 |
expr.get_block_expr ().accept_vis (*this); |
| 117 |
} |
| 118 |
|
| 119 |
void visit (HIR::LoopExpr &expr) override |
| 120 |
{ |
| 121 |
expr.get_loop_block ().accept_vis (*this); |
| 122 |
} |
| 123 |
|
| 124 |
void visit (HIR::BreakExpr &expr) override |
| 125 |
{ |
| 126 |
if (expr.has_break_expr ()) |
| 127 |
expr.get_expr ().accept_vis (*this); |
| 128 |
} |
| 129 |
|
| 130 |
void visit (HIR::WhileLoopExpr &expr) override |
| 131 |
{ |
| 132 |
expr.get_loop_block ().accept_vis (*this); |
| 133 |
expr.get_predicate_expr ().accept_vis (*this); |
| 134 |
} |
| 135 |
|
| 136 |
void visit (HIR::Function &function) override |
| 137 |
{ |
| 138 |
function.get_definition ().accept_vis (*this); |
| 139 |
} |
| 140 |
|
| 141 |
void visit (HIR::ReturnExpr &expr) override |
| 142 |
{ |
| 143 |
if (expr.has_return_expr ()) |
| 144 |
expr.get_expr ().accept_vis (*this); |
| 145 |
} |
| 146 |
|
| 147 |
void visit (HIR::WhileLetLoopExpr &expr) override |
| 148 |
{ |
| 149 |
expr.get_loop_block ().accept_vis (*this); |
| 150 |
expr.get_cond ().accept_vis (*this); |
| 151 |
} |
| 152 |
|
| 153 |
void visit (HIR::ExprStmt &stmt) override |
| 154 |
{ |
| 155 |
stmt.get_expr ().accept_vis (*this); |
| 156 |
} |
| 157 |
|
| 158 |
void visit (HIR::CallExpr &expr) override |
| 159 |
{ |
| 160 |
expr.get_fnexpr ().accept_vis (*this); |
| 161 |
for (auto &argument : expr.get_arguments ()) |
| 162 |
argument->accept_vis (*this); |
| 163 |
} |
| 164 |
|
| 165 |
void visit (HIR::ArithmeticOrLogicalExpr &expr) override |
| 166 |
{ |
| 167 |
expr.visit_lhs (*this); |
| 168 |
expr.visit_rhs (*this); |
| 169 |
} |
| 170 |
void visit (HIR::ComparisonExpr &expr) override |
| 171 |
{ |
| 172 |
expr.get_lhs ().accept_vis (*this); |
| 173 |
expr.get_rhs ().accept_vis (*this); |
| 174 |
} |
| 175 |
|
| 176 |
void visit (HIR::AssignmentExpr &expr) override |
| 177 |
{ |
| 178 |
expr.visit_lhs (*this); |
| 179 |
expr.visit_rhs (*this); |
| 180 |
} |
| 181 |
|
| 182 |
void visit (HIR::CompoundAssignmentExpr &expr) override |
| 183 |
{ |
| 184 |
expr.visit_lhs (*this); |
| 185 |
expr.visit_rhs (*this); |
| 186 |
} |
| 187 |
|
| 188 |
void visit (HIR::IfExpr &expr) override |
| 189 |
{ |
| 190 |
expr.get_if_condition ().accept_vis (*this); |
| 191 |
expr.get_if_block ().accept_vis (*this); |
| 192 |
} |
| 193 |
|
| 194 |
void visit (HIR::IfExprConseqElse &expr) override |
| 195 |
{ |
| 196 |
expr.get_if_condition ().accept_vis (*this); |
| 197 |
expr.get_if_block ().accept_vis (*this); |
| 198 |
expr.get_else_block ().accept_vis (*this); |
| 199 |
} |
| 200 |
|
| 201 |
void visit (HIR::MatchExpr &expr) override |
| 202 |
{ |
| 203 |
expr.get_scrutinee_expr ().accept_vis (*this); |
| 204 |
std::vector<HIR::MatchCase> &cases = expr.get_match_cases (); |
| 205 |
for (auto &&caz : cases) |
| 206 |
{ |
| 207 |
auto case_arm = caz.get_arm (); |
| 208 |
if (case_arm.has_match_arm_guard ()) |
| 209 |
case_arm.get_guard_expr ().accept_vis (*this); |
| 210 |
caz.get_expr ().accept_vis (*this); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
void visit (HIR::TraitItemFunc &item) override |
| 215 |
{ |
| 216 |
item.get_block_expr ().accept_vis (*this); |
| 217 |
} |
| 218 |
|
| 219 |
void visit (HIR::ImplBlock &impl) override |
| 220 |
{ |
| 221 |
for (auto &&item : impl.get_impl_items ()) |
| 222 |
{ |
| 223 |
item->accept_vis (*this); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
void visit (HIR::LetStmt &stmt) override |
| 228 |
{ |
| 229 |
if (stmt.has_init_expr ()) |
| 230 |
{ |
| 231 |
stmt.get_init_expr ().accept_vis (*this); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
void visit (HIR::StructExprStruct &stct) override |
| 236 |
{ |
| 237 |
stct.get_struct_name ().accept_vis (*this); |
| 238 |
} |
| 239 |
|
| 240 |
void visit (HIR::StructExprStructFields &stct) override |
| 241 |
{ |
| 242 |
for (auto &field : stct.get_fields ()) |
| 243 |
{ |
| 244 |
field->accept_vis (*this); |
| 245 |
} |
| 246 |
|
| 247 |
stct.get_struct_name ().accept_vis (*this); |
| 248 |
if (stct.has_struct_base ()) |
| 249 |
{ |
| 250 |
stct.get_struct_base ().get_base ().accept_vis (*this); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
virtual void visit (HIR::StructExprFieldIdentifierValue &field) override |
| 255 |
{ |
| 256 |
field.get_value ().accept_vis (*this); |
| 257 |
} |
| 258 |
|
| 259 |
void visit (HIR::StructExprStructBase &stct) override |
| 260 |
{ |
| 261 |
stct.get_struct_base ().get_base ().accept_vis (*this); |
| 262 |
} |
| 263 |
|
| 264 |
void visit (HIR::Module &module) override |
| 265 |
{ |
| 266 |
for (auto &item : module.get_items ()) |
| 267 |
item->accept_vis (*this); |
| 268 |
} |
| 269 |
|
| 270 |
void visit (HIR::ClosureExpr &expr) override |
| 271 |
{ |
| 272 |
expr.get_expr ().accept_vis (*this); |
| 273 |
} |
| 274 |
|
| 275 |
private: |
| 276 |
std::vector<HirId> worklist; |
| 277 |
std::set<HirId> liveSymbols; |
| 278 |
std::set<HirId> scannedSymbols; |
| 279 |
Analysis::Mappings &mappings; |
| 280 |
Resolver::Resolver *resolver; |
| 281 |
Resolver::TypeCheckContext *tyctx; |
| 282 |
MarkLive (std::vector<HirId> worklist) |
| 283 |
: worklist (worklist), mappings (Analysis::Mappings::get ()), |
| 284 |
resolver (Resolver::Resolver::get ()), |
| 285 |
tyctx (Resolver::TypeCheckContext::get ()){}; |
| 286 |
|
| 287 |
void mark_hir_id (HirId); |
| 288 |
bool visit_path_segment (HIR::PathExprSegment); |
| 289 |
void find_ref_node_id (NodeId ast_node_id, NodeId &ref_node_id); |
| 290 |
}; |
| 291 |
|
| 292 |
} // namespace Analysis |
| 293 |
} // namespace Rust |
| 294 |
|
| 295 |
#endif |