1
Fork 0

rustc_metadata: Optimize and document module children decoding

This commit is contained in:
Vadim Petrochenkov 2021-12-18 20:07:58 +08:00
parent 23ce5fc465
commit 96c6a50e96
5 changed files with 50 additions and 38 deletions

View file

@ -1075,15 +1075,16 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
}
}
/// Iterates over each child of the given item.
/// Iterates over all named children of the given module,
/// including both proper items and reexports.
/// Module here is understood in name resolution sense - it can be a `mod` item,
/// or a crate root, or an enum, or a trait.
fn each_child_of_item(&self, id: DefIndex, mut callback: impl FnMut(Export), sess: &Session) {
if let Some(data) = &self.root.proc_macro_data {
/* If we are loading as a proc macro, we want to return the view of this crate
* as a proc macro crate.
*/
// If we are loading as a proc macro, we want to return
// the view of this crate as a proc macro crate.
if id == CRATE_DEF_INDEX {
let macros = data.macros.decode(self);
for def_index in macros {
for def_index in data.macros.decode(self) {
let raw_macro = self.raw_proc_macro(def_index);
let res = Res::Def(
DefKind::Macro(macro_kind(raw_macro)),
@ -1096,12 +1097,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
return;
}
// Find the item.
let kind = match self.maybe_kind(id) {
None => return,
Some(kind) => kind,
};
// Iterate over all children.
if let Some(children) = self.root.tables.children.get(self, id) {
for child_index in children.decode((self, sess)) {
@ -1162,10 +1157,14 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
}
}
if let EntryKind::Mod(exports) = kind {
for exp in exports.decode((self, sess)) {
callback(exp);
match self.kind(id) {
EntryKind::Mod(exports) => {
for exp in exports.decode((self, sess)) {
callback(exp);
}
}
EntryKind::Enum(..) | EntryKind::Trait(..) => {}
_ => bug!("`each_child_of_item` is called on a non-module: {:?}", self.def_kind(id)),
}
}

View file

@ -4,7 +4,7 @@ use crate::native_libs;
use rustc_ast as ast;
use rustc_data_structures::stable_map::FxHashMap;
use rustc_hir::def::{CtorKind, DefKind};
use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
use rustc_middle::hir::exports::Export;
@ -309,28 +309,33 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
bfs_queue.push_back(DefId { krate: cnum, index: CRATE_DEF_INDEX });
}
let mut add_child = |bfs_queue: &mut VecDeque<_>, export: &Export, parent: DefId| {
if !export.vis.is_public() {
let mut add_child = |bfs_queue: &mut VecDeque<_>, child: &Export, parent: DefId| {
if !child.vis.is_public() {
return;
}
if let Some(child) = export.res.opt_def_id() {
if export.ident.name == kw::Underscore {
fallback_map.insert(child, parent);
if let Some(def_id) = child.res.opt_def_id() {
if child.ident.name == kw::Underscore {
fallback_map.insert(def_id, parent);
return;
}
match visible_parent_map.entry(child) {
match visible_parent_map.entry(def_id) {
Entry::Occupied(mut entry) => {
// If `child` is defined in crate `cnum`, ensure
// that it is mapped to a parent in `cnum`.
if child.is_local() && entry.get().is_local() {
if def_id.is_local() && entry.get().is_local() {
entry.insert(parent);
}
}
Entry::Vacant(entry) => {
entry.insert(parent);
bfs_queue.push_back(child);
if matches!(
child.res,
Res::Def(DefKind::Mod | DefKind::Enum | DefKind::Trait, _)
) {
bfs_queue.push_back(def_id);
}
}
}
}

View file

@ -1104,7 +1104,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
record!(self.tables.kind[def_id] <- EntryKind::Mod(reexports));
if self.is_proc_macro {
record!(self.tables.children[def_id] <- &[]);
// Encode this here because we don't do it in encode_def_ids.
record!(self.tables.expn_that_defined[def_id] <- tcx.expn_that_defined(local_def_id));
} else {