| 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 |
// The idea is that all reachable symbols are live, codes called |
| 20 |
// from live codes are live, and everything else is dead. |
| 21 |
|
| 22 |
#include "rust-lint-marklive.h" |
| 23 |
#include "options.h" |
| 24 |
#include "rust-hir-full.h" |
| 25 |
#include "rust-hir-map.h" |
| 26 |
#include "rust-hir-path.h" |
| 27 |
#include "rust-name-resolver.h" |
| 28 |
#include "rust-immutable-name-resolution-context.h" |
| 29 |
#include "rust-system.h" |
| 30 |
|
| 31 |
namespace Rust { |
| 32 |
namespace Analysis { |
| 33 |
|
| 34 |
// This class trys to find the live symbols which can be used as |
| 35 |
// seeds in MarkLive |
| 36 |
// |
| 37 |
// 1. TODO: explicit live |
| 38 |
// - Attribute like #[allow(dead_code)] |
| 39 |
// - Attribute like #[lang=".."], it's not a intra-crate item. |
| 40 |
// 2. TODO: foreign item |
| 41 |
class FindEntryPoint : public MarkLiveBase |
| 42 |
{ |
| 43 |
using Rust::Analysis::MarkLiveBase::visit; |
| 44 |
|
| 45 |
public: |
| 46 |
static std::vector<HirId> find (HIR::Crate &crate) |
| 47 |
{ |
| 48 |
FindEntryPoint findEntryPoint; |
| 49 |
for (auto &it : crate.get_items ()) |
| 50 |
it->accept_vis (findEntryPoint); |
| 51 |
return findEntryPoint.getEntryPoint (); |
| 52 |
} |
| 53 |
|
| 54 |
// TODO not only fn main can be a entry point. |
| 55 |
void visit (HIR::Function &function) override |
| 56 |
{ |
| 57 |
if (function.get_function_name ().as_string () == "main") |
| 58 |
{ |
| 59 |
entryPoints.push_back (function.get_mappings ().get_hirid ()); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
private: |
| 64 |
FindEntryPoint () : MarkLiveBase () {} |
| 65 |
std::vector<HirId> entryPoints; |
| 66 |
std::vector<HirId> getEntryPoint () { return entryPoints; } |
| 67 |
}; |
| 68 |
|
| 69 |
std::set<HirId> |
| 70 |
MarkLive::Analysis (HIR::Crate &crate) |
| 71 |
{ |
| 72 |
MarkLive marklive (FindEntryPoint::find (crate)); |
| 73 |
marklive.go (crate); |
| 74 |
|
| 75 |
return marklive.liveSymbols; |
| 76 |
} |
| 77 |
|
| 78 |
// pop a live symbol from worklist every iteration, |
| 79 |
// if it's a function then walk the function body, and |
| 80 |
// 1. save all the live symbols in worklist which is |
| 81 |
// visited first time |
| 82 |
// 2. save all the live symbols in liveSymbols |
| 83 |
void |
| 84 |
MarkLive::go (HIR::Crate &) |
| 85 |
{ |
| 86 |
while (!worklist.empty ()) |
| 87 |
{ |
| 88 |
HirId hirId = worklist.back (); |
| 89 |
worklist.pop_back (); |
| 90 |
scannedSymbols.emplace (hirId); |
| 91 |
liveSymbols.emplace (hirId); |
| 92 |
if (auto item = mappings.lookup_hir_item (hirId)) |
| 93 |
item.value ()->accept_vis (*this); |
| 94 |
else if (auto implItem = mappings.lookup_hir_implitem (hirId)) |
| 95 |
implItem->first->accept_vis (*this); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
void |
| 100 |
MarkLive::visit (HIR::PathInExpression &expr) |
| 101 |
{ |
| 102 |
// We should iterate every path segment in order to mark the struct which |
| 103 |
// is used in expression like Foo::bar(), we should mark the Foo alive. |
| 104 |
if (!expr.is_lang_item ()) |
| 105 |
expr.iterate_path_segments ([&] (HIR::PathExprSegment &seg) -> bool { |
| 106 |
return visit_path_segment (seg); |
| 107 |
}); |
| 108 |
|
| 109 |
// after iterate the path segments, we should mark functions and associated |
| 110 |
// functions alive. |
| 111 |
NodeId ast_node_id = expr.get_mappings ().get_nodeid (); |
| 112 |
NodeId ref_node_id = UNKNOWN_NODEID; |
| 113 |
|
| 114 |
if (expr.is_lang_item ()) |
| 115 |
ref_node_id |
| 116 |
= Analysis::Mappings::get ().get_lang_item_node (expr.get_lang_item ()); |
| 117 |
else |
| 118 |
find_ref_node_id (ast_node_id, ref_node_id); |
| 119 |
|
| 120 |
// node back to HIR |
| 121 |
tl::optional<HirId> hid = mappings.lookup_node_to_hir (ref_node_id); |
| 122 |
rust_assert (hid.has_value ()); |
| 123 |
auto ref = hid.value (); |
| 124 |
|
| 125 |
// it must resolve to some kind of HIR::Item or HIR::InheritImplItem |
| 126 |
if (auto resolved_item = mappings.lookup_hir_item (ref)) |
| 127 |
mark_hir_id (resolved_item.value ()->get_mappings ().get_hirid ()); |
| 128 |
else if (auto resolved_item = mappings.lookup_hir_implitem (ref)) |
| 129 |
mark_hir_id (resolved_item->first->get_impl_mappings ().get_hirid ()); |
| 130 |
} |
| 131 |
|
| 132 |
void |
| 133 |
MarkLive::visit (HIR::MethodCallExpr &expr) |
| 134 |
{ |
| 135 |
expr.get_receiver ().accept_vis (*this); |
| 136 |
visit_path_segment (expr.get_method_name ()); |
| 137 |
for (auto &argument : expr.get_arguments ()) |
| 138 |
argument->accept_vis (*this); |
| 139 |
|
| 140 |
// Trying to find the method definition and mark it alive. |
| 141 |
NodeId ast_node_id = expr.get_mappings ().get_nodeid (); |
| 142 |
NodeId ref_node_id = UNKNOWN_NODEID; |
| 143 |
find_ref_node_id (ast_node_id, ref_node_id); |
| 144 |
|
| 145 |
// node back to HIR |
| 146 |
if (auto hid = mappings.lookup_node_to_hir (ref_node_id)) |
| 147 |
mark_hir_id (*hid); |
| 148 |
else |
| 149 |
rust_unreachable (); |
| 150 |
} |
| 151 |
|
| 152 |
bool |
| 153 |
MarkLive::visit_path_segment (HIR::PathExprSegment seg) |
| 154 |
{ |
| 155 |
NodeId ast_node_id = seg.get_mappings ().get_nodeid (); |
| 156 |
NodeId ref_node_id = UNKNOWN_NODEID; |
| 157 |
|
| 158 |
// There are two different kinds of segment for us. |
| 159 |
// 1. function segment |
| 160 |
// like the symbol "foo" in expression `foo()`. |
| 161 |
// 2. type segment |
| 162 |
// like the symbol "Foo" in expression `Foo{a: 1, b: 2}` |
| 163 |
// |
| 164 |
// We should mark them alive all and ignoring other kind of segments. |
| 165 |
// If the segment we dont care then just return false is fine |
| 166 |
if (flag_name_resolution_2_0) |
| 167 |
{ |
| 168 |
auto &nr_ctx |
| 169 |
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); |
| 170 |
|
| 171 |
if (auto id = nr_ctx.lookup (ast_node_id)) |
| 172 |
ref_node_id = *id; |
| 173 |
else |
| 174 |
return false; |
| 175 |
} |
| 176 |
else if (!resolver->lookup_resolved_name (ast_node_id, &ref_node_id)) |
| 177 |
{ |
| 178 |
if (!resolver->lookup_resolved_type (ast_node_id, &ref_node_id)) |
| 179 |
return false; |
| 180 |
} |
| 181 |
if (auto hid = mappings.lookup_node_to_hir (ref_node_id)) |
| 182 |
{ |
| 183 |
mark_hir_id (*hid); |
| 184 |
return true; |
| 185 |
} |
| 186 |
rust_unreachable (); |
| 187 |
} |
| 188 |
|
| 189 |
void |
| 190 |
MarkLive::visit (HIR::FieldAccessExpr &expr) |
| 191 |
{ |
| 192 |
// visit receiver at first |
| 193 |
expr.get_receiver_expr ().accept_vis (*this); |
| 194 |
|
| 195 |
// resolve the receiver back to ADT type |
| 196 |
TyTy::BaseType *receiver = nullptr; |
| 197 |
if (!tyctx->lookup_type ( |
| 198 |
expr.get_receiver_expr ().get_mappings ().get_hirid (), &receiver)) |
| 199 |
{ |
| 200 |
rust_error_at (expr.get_receiver_expr ().get_locus (), |
| 201 |
"unresolved type for receiver"); |
| 202 |
} |
| 203 |
|
| 204 |
TyTy::ADTType *adt = nullptr; |
| 205 |
if (receiver->get_kind () == TyTy::TypeKind::ADT) |
| 206 |
{ |
| 207 |
adt = static_cast<TyTy::ADTType *> (receiver); |
| 208 |
} |
| 209 |
else if (receiver->get_kind () == TyTy::TypeKind::REF) |
| 210 |
{ |
| 211 |
TyTy::ReferenceType *r = static_cast<TyTy::ReferenceType *> (receiver); |
| 212 |
TyTy::BaseType *b = r->get_base (); |
| 213 |
rust_assert (b->get_kind () == TyTy::TypeKind::ADT); |
| 214 |
|
| 215 |
adt = static_cast<TyTy::ADTType *> (b); |
| 216 |
} |
| 217 |
|
| 218 |
rust_assert (adt != nullptr); |
| 219 |
rust_assert (!adt->is_enum ()); |
| 220 |
rust_assert (adt->number_of_variants () == 1); |
| 221 |
|
| 222 |
TyTy::VariantDef *variant = adt->get_variants ().at (0); |
| 223 |
|
| 224 |
// get the field index |
| 225 |
size_t index; |
| 226 |
TyTy::StructFieldType *field; |
| 227 |
bool ok = variant->lookup_field (expr.get_field_name ().as_string (), &field, |
| 228 |
&index); |
| 229 |
rust_assert (ok); |
| 230 |
if (index >= variant->num_fields ()) |
| 231 |
{ |
| 232 |
rust_error_at (expr.get_receiver_expr ().get_locus (), |
| 233 |
"cannot access struct %s by index: %lu", |
| 234 |
adt->get_name ().c_str (), (unsigned long) index); |
| 235 |
return; |
| 236 |
} |
| 237 |
|
| 238 |
// get the field hir id |
| 239 |
HirId field_id = field->get_ref (); |
| 240 |
mark_hir_id (field_id); |
| 241 |
} |
| 242 |
|
| 243 |
void |
| 244 |
MarkLive::visit (HIR::TupleIndexExpr &expr) |
| 245 |
{ |
| 246 |
// TODO: unused tuple field detection |
| 247 |
expr.get_tuple_expr ().accept_vis (*this); |
| 248 |
} |
| 249 |
|
| 250 |
void |
| 251 |
MarkLive::visit (HIR::TypeAlias &alias) |
| 252 |
{ |
| 253 |
NodeId ast_node_id = UNKNOWN_NODEID; |
| 254 |
if (flag_name_resolution_2_0) |
| 255 |
{ |
| 256 |
auto &nr_ctx |
| 257 |
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); |
| 258 |
|
| 259 |
if (auto id = nr_ctx.lookup ( |
| 260 |
alias.get_type_aliased ().get_mappings ().get_nodeid ())) |
| 261 |
ast_node_id = *id; |
| 262 |
} |
| 263 |
else |
| 264 |
{ |
| 265 |
resolver->lookup_resolved_type ( |
| 266 |
alias.get_type_aliased ().get_mappings ().get_nodeid (), &ast_node_id); |
| 267 |
} |
| 268 |
|
| 269 |
if (auto hid = mappings.lookup_node_to_hir (ast_node_id)) |
| 270 |
mark_hir_id (*hid); |
| 271 |
else |
| 272 |
rust_unreachable (); |
| 273 |
} |
| 274 |
|
| 275 |
void |
| 276 |
MarkLive::mark_hir_id (HirId id) |
| 277 |
{ |
| 278 |
if (scannedSymbols.find (id) == scannedSymbols.end ()) |
| 279 |
{ |
| 280 |
worklist.push_back (id); |
| 281 |
} |
| 282 |
liveSymbols.emplace (id); |
| 283 |
} |
| 284 |
|
| 285 |
void |
| 286 |
MarkLive::find_ref_node_id (NodeId ast_node_id, NodeId &ref_node_id) |
| 287 |
{ |
| 288 |
if (flag_name_resolution_2_0) |
| 289 |
{ |
| 290 |
auto &nr_ctx |
| 291 |
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); |
| 292 |
|
| 293 |
nr_ctx.lookup (ast_node_id).map ([&ref_node_id] (NodeId resolved) { |
| 294 |
ref_node_id = resolved; |
| 295 |
}); |
| 296 |
} |
| 297 |
else |
| 298 |
{ |
| 299 |
if (!resolver->lookup_resolved_name (ast_node_id, &ref_node_id)) |
| 300 |
{ |
| 301 |
if (!resolver->lookup_resolved_type (ast_node_id, &ref_node_id)) |
| 302 |
{ |
| 303 |
bool ok |
| 304 |
= resolver->lookup_resolved_misc (ast_node_id, &ref_node_id); |
| 305 |
rust_assert (ok); |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
} // namespace Analysis |
| 312 |
} // namespace Rust |