Skip to content

Commit

Permalink
merge visitors in queries
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
  • Loading branch information
kckeiks committed Jul 13, 2022
1 parent b599cf4 commit 275497c
Showing 1 changed file with 75 additions and 135 deletions.
210 changes: 75 additions & 135 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,20 +1211,12 @@ fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
}

pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalDefId) -> ModuleItems {
let mut collector = ModuleCollector {
tcx,
submodules: Vec::default(),
items: Vec::default(),
trait_items: Vec::default(),
impl_items: Vec::default(),
foreign_items: Vec::default(),
body_owners: Vec::default(),
};
let mut collector = ItemCollector::new(tcx, false);

let (hir_mod, span, hir_id) = tcx.hir().get_module(module_id);
collector.visit_mod(hir_mod, span, hir_id);

let ModuleCollector {
let ItemCollector {
submodules,
items,
trait_items,
Expand All @@ -1241,90 +1233,14 @@ pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalDefId) -> Module
foreign_items: foreign_items.into_boxed_slice(),
body_owners: body_owners.into_boxed_slice(),
};

struct ModuleCollector<'tcx> {
tcx: TyCtxt<'tcx>,
submodules: Vec<LocalDefId>,
items: Vec<ItemId>,
trait_items: Vec<TraitItemId>,
impl_items: Vec<ImplItemId>,
foreign_items: Vec<ForeignItemId>,
body_owners: Vec<LocalDefId>,
}

impl<'hir> Visitor<'hir> for ModuleCollector<'hir> {
type NestedFilter = nested_filter::All;

fn nested_visit_map(&mut self) -> Self::Map {
self.tcx.hir()
}

fn visit_item(&mut self, item: &'hir Item<'hir>) {
if associated_body(Node::Item(item)).is_some() {
self.body_owners.push(item.def_id);
}

self.items.push(item.item_id());

if let ItemKind::Mod(..) = item.kind {
// If this declares another module, do not recurse inside it.
self.submodules.push(item.def_id);
} else {
intravisit::walk_item(self, item)
}
}

fn visit_trait_item(&mut self, item: &'hir TraitItem<'hir>) {
if associated_body(Node::TraitItem(item)).is_some() {
self.body_owners.push(item.def_id);
}

self.trait_items.push(item.trait_item_id());
intravisit::walk_trait_item(self, item)
}

fn visit_impl_item(&mut self, item: &'hir ImplItem<'hir>) {
if associated_body(Node::ImplItem(item)).is_some() {
self.body_owners.push(item.def_id);
}

self.impl_items.push(item.impl_item_id());
intravisit::walk_impl_item(self, item)
}

fn visit_foreign_item(&mut self, item: &'hir ForeignItem<'hir>) {
self.foreign_items.push(item.foreign_item_id());
intravisit::walk_foreign_item(self, item)
}

fn visit_expr(&mut self, ex: &'hir Expr<'hir>) {
if matches!(ex.kind, ExprKind::Closure { .. }) {
self.body_owners.push(self.tcx.hir().local_def_id(ex.hir_id));
}
intravisit::walk_expr(self, ex)
}

fn visit_anon_const(&mut self, c: &'hir AnonConst) {
self.body_owners.push(self.tcx.hir().local_def_id(c.hir_id));
intravisit::walk_anon_const(self, c)
}
}
}

pub(crate) fn hir_crate_items(tcx: TyCtxt<'_>, _: ()) -> ModuleItems {
let mut collector = CrateCollector {
tcx,
submodules: Vec::default(),
items: Vec::default(),
trait_items: Vec::default(),
impl_items: Vec::default(),
foreign_items: Vec::default(),
body_owners: Vec::default(),
};
let mut collector = ItemCollector::new(tcx, true);

tcx.hir().walk_toplevel_module(&mut collector);

let CrateCollector {
let ItemCollector {
submodules,
items,
trait_items,
Expand All @@ -1342,71 +1258,95 @@ pub(crate) fn hir_crate_items(tcx: TyCtxt<'_>, _: ()) -> ModuleItems {
foreign_items: foreign_items.into_boxed_slice(),
body_owners: body_owners.into_boxed_slice(),
};
}

struct CrateCollector<'tcx> {
tcx: TyCtxt<'tcx>,
submodules: Vec<LocalDefId>,
items: Vec<ItemId>,
trait_items: Vec<TraitItemId>,
impl_items: Vec<ImplItemId>,
foreign_items: Vec<ForeignItemId>,
body_owners: Vec<LocalDefId>,
struct ItemCollector<'tcx> {
// When true, it collects all items in the create,
// otherwise it collects items in some module.
crate_collector: bool,
tcx: TyCtxt<'tcx>,
submodules: Vec<LocalDefId>,
items: Vec<ItemId>,
trait_items: Vec<TraitItemId>,
impl_items: Vec<ImplItemId>,
foreign_items: Vec<ForeignItemId>,
body_owners: Vec<LocalDefId>,
}

impl<'tcx> ItemCollector<'tcx> {
fn new(tcx: TyCtxt<'tcx>, crate_collector: bool) -> ItemCollector<'tcx> {
ItemCollector {
crate_collector,
tcx,
submodules: Vec::default(),
items: Vec::default(),
trait_items: Vec::default(),
impl_items: Vec::default(),
foreign_items: Vec::default(),
body_owners: Vec::default(),
}
}
}

impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
type NestedFilter = nested_filter::All;

impl<'hir> Visitor<'hir> for CrateCollector<'hir> {
type NestedFilter = nested_filter::All;
fn nested_visit_map(&mut self) -> Self::Map {
self.tcx.hir()
}

fn nested_visit_map(&mut self) -> Self::Map {
self.tcx.hir()
fn visit_item(&mut self, item: &'hir Item<'hir>) {
if associated_body(Node::Item(item)).is_some() {
self.body_owners.push(item.def_id);
}

fn visit_item(&mut self, item: &'hir Item<'hir>) {
if associated_body(Node::Item(item)).is_some() {
self.body_owners.push(item.def_id);
}
self.items.push(item.item_id());

self.items.push(item.item_id());
if !self.crate_collector && let ItemKind::Mod(..) = item.kind {
// If this declares another module, do not recurse inside it.
self.submodules.push(item.def_id);
} else {
intravisit::walk_item(self, item)
}
}

fn visit_mod(&mut self, m: &'hir Mod<'hir>, _s: Span, n: HirId) {
self.submodules.push(n.owner);
intravisit::walk_mod(self, m, n);
}
fn visit_mod(&mut self, m: &'hir Mod<'hir>, _s: Span, n: HirId) {
self.submodules.push(n.owner);
intravisit::walk_mod(self, m, n);
}

fn visit_foreign_item(&mut self, item: &'hir ForeignItem<'hir>) {
self.foreign_items.push(item.foreign_item_id());
intravisit::walk_foreign_item(self, item)
}
fn visit_foreign_item(&mut self, item: &'hir ForeignItem<'hir>) {
self.foreign_items.push(item.foreign_item_id());
intravisit::walk_foreign_item(self, item)
}

fn visit_trait_item(&mut self, item: &'hir TraitItem<'hir>) {
if associated_body(Node::TraitItem(item)).is_some() {
self.body_owners.push(item.def_id);
}
fn visit_anon_const(&mut self, c: &'hir AnonConst) {
self.body_owners.push(self.tcx.hir().local_def_id(c.hir_id));
intravisit::walk_anon_const(self, c)
}

self.trait_items.push(item.trait_item_id());
intravisit::walk_trait_item(self, item)
fn visit_expr(&mut self, ex: &'hir Expr<'hir>) {
if matches!(ex.kind, ExprKind::Closure { .. }) {
self.body_owners.push(self.tcx.hir().local_def_id(ex.hir_id));
}
intravisit::walk_expr(self, ex)
}

fn visit_impl_item(&mut self, item: &'hir ImplItem<'hir>) {
if associated_body(Node::ImplItem(item)).is_some() {
self.body_owners.push(item.def_id);
}

self.impl_items.push(item.impl_item_id());
intravisit::walk_impl_item(self, item)
fn visit_trait_item(&mut self, item: &'hir TraitItem<'hir>) {
if associated_body(Node::TraitItem(item)).is_some() {
self.body_owners.push(item.def_id);
}

fn visit_expr(&mut self, ex: &'hir Expr<'hir>) {
if matches!(ex.kind, ExprKind::Closure { .. }) {
self.body_owners.push(self.tcx.hir().local_def_id(ex.hir_id));
}
intravisit::walk_expr(self, ex)
}
self.trait_items.push(item.trait_item_id());
intravisit::walk_trait_item(self, item)
}

fn visit_anon_const(&mut self, c: &'hir AnonConst) {
self.body_owners.push(self.tcx.hir().local_def_id(c.hir_id));
intravisit::walk_anon_const(self, c)
fn visit_impl_item(&mut self, item: &'hir ImplItem<'hir>) {
if associated_body(Node::ImplItem(item)).is_some() {
self.body_owners.push(item.def_id);
}

self.impl_items.push(item.impl_item_id());
intravisit::walk_impl_item(self, item)
}
}

0 comments on commit 275497c

Please sign in to comment.