Skip to content

Commit 8bec29f

Browse files
committed
wip
1 parent 47e574e commit 8bec29f

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

src/librustc/arena.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ macro_rules! arena_types {
159159
[] type_binding: rustc_hir::TypeBinding<$tcx>,
160160
[] variant: rustc_hir::Variant<$tcx>,
161161
[] where_predicate: rustc_hir::WherePredicate<$tcx>,
162+
163+
// HIR query types
164+
[] hir_owner: rustc::hir::HirOwner<$tcx>,
165+
[] hir_owner_items: rustc::hir::HirOwnerItems<$tcx>,
162166
], $tcx);
163167
)
164168
}

src/librustc/hir/map/collector.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use crate::arena::Arena;
12
use crate::dep_graph::{DepGraph, DepKind, DepNode, DepNodeIndex};
23
use crate::hir::map::definitions::{self, DefPathHash};
34
use crate::hir::map::{Entry, HirEntryMap, Map};
5+
use crate::hir::{HirItem, HirOwner, HirOwnerItems};
46
use crate::ich::StableHashingContext;
57
use crate::middle::cstore::CrateStore;
68
use rustc_data_structures::fingerprint::Fingerprint;
@@ -22,12 +24,17 @@ use std::iter::repeat;
2224

2325
/// A visitor that walks over the HIR and collects `Node`s into a HIR map.
2426
pub(super) struct NodeCollector<'a, 'hir> {
27+
arena: &'hir Arena<'hir>,
28+
2529
/// The crate
2630
krate: &'hir Crate<'hir>,
2731

2832
/// Source map
2933
source_map: &'a SourceMap,
3034

35+
owner_map: FxHashMap<DefIndex, &'hir HirOwner<'hir>>,
36+
owner_items_map: FxHashMap<DefIndex, &'hir mut HirOwnerItems<'hir>>,
37+
3138
/// The node map
3239
map: HirEntryMap<'hir>,
3340
/// The parent of this node
@@ -112,6 +119,7 @@ fn upstream_crates(cstore: &dyn CrateStore) -> Vec<(Symbol, Fingerprint, Svh)> {
112119
impl<'a, 'hir> NodeCollector<'a, 'hir> {
113120
pub(super) fn root(
114121
sess: &'a Session,
122+
arena: &'hir Arena<'hir>,
115123
krate: &'hir Crate<'hir>,
116124
dep_graph: &'a DepGraph,
117125
definitions: &'a definitions::Definitions,
@@ -160,6 +168,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
160168
}
161169

162170
let mut collector = NodeCollector {
171+
arena,
163172
krate,
164173
source_map: sess.source_map(),
165174
map: IndexVec::from_elem_n(IndexVec::new(), definitions.def_index_count()),
@@ -173,6 +182,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
173182
hir_to_node_id,
174183
hcx,
175184
hir_body_nodes,
185+
owner_map: FxHashMap::default(),
186+
owner_items_map: FxHashMap::default(),
176187
};
177188
collector.insert_entry(
178189
hir::CRATE_HIR_ID,
@@ -232,9 +243,30 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
232243
}
233244

234245
fn insert_entry(&mut self, id: HirId, entry: Entry<'hir>) {
246+
let i = id.local_id.as_u32() as usize;
247+
248+
let owner = HirOwner { parent: entry.parent, node: entry.node };
249+
250+
let arena = self.arena;
251+
252+
let items = self.owner_items_map.entry(id.owner).or_insert_with(|| {
253+
arena.alloc(HirOwnerItems { items: IndexVec::new(), bodies: FxHashMap::default() })
254+
});
255+
256+
if i == 0 {
257+
assert!(self.owner_map.insert(id.owner, self.arena.alloc(owner)).is_none());
258+
} else {
259+
let len = items.items.len();
260+
if i >= len {
261+
items.items.extend(repeat(None).take(i - len + 1));
262+
}
263+
assert_eq!(entry.parent.owner, id.owner);
264+
items.items[id.local_id] =
265+
Some(HirItem { parent: entry.parent.local_id, node: entry.node });
266+
}
267+
235268
debug!("hir_map: {:?} => {:?}", id, entry);
236269
let local_map = &mut self.map[id.owner];
237-
let i = id.local_id.as_u32() as usize;
238270
let len = local_map.len();
239271
if i >= len {
240272
local_map.extend(repeat(None).take(i - len + 1));

src/librustc/hir/map/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub use self::definitions::{
33
DefKey, DefPath, DefPathData, DefPathHash, Definitions, DisambiguatedDefPathData,
44
};
55

6+
use crate::arena::Arena;
67
use crate::dep_graph::{DepGraph, DepKind, DepNode, DepNodeIndex};
78
use crate::middle::cstore::CrateStoreDyn;
89
use crate::ty::query::Providers;
@@ -1206,6 +1207,7 @@ impl Named for ImplItem<'_> {
12061207

12071208
pub fn map_crate<'hir>(
12081209
sess: &rustc_session::Session,
1210+
arena: &'hir Arena<'hir>,
12091211
cstore: &CrateStoreDyn,
12101212
krate: &'hir Crate<'hir>,
12111213
dep_graph: DepGraph,
@@ -1224,7 +1226,7 @@ pub fn map_crate<'hir>(
12241226
let hcx = crate::ich::StableHashingContext::new(sess, krate, &definitions, cstore);
12251227

12261228
let mut collector =
1227-
NodeCollector::root(sess, krate, &dep_graph, &definitions, &hir_to_node_id, hcx);
1229+
NodeCollector::root(sess, arena, krate, &dep_graph, &definitions, &hir_to_node_id, hcx);
12281230
intravisit::walk_crate(&mut collector, krate);
12291231

12301232
let crate_disambiguator = sess.local_crate_disambiguator();

src/librustc/hir/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ pub struct HirOwner<'tcx> {
2525
node: Node<'tcx>,
2626
}
2727

28-
#[derive(HashStable)]
28+
#[derive(HashStable, Clone)]
2929
pub struct HirItem<'tcx> {
3030
parent: ItemLocalId,
3131
node: Node<'tcx>,
3232
}
3333

3434
#[derive(HashStable)]
3535
pub struct HirOwnerItems<'tcx> {
36-
owner: &'tcx HirOwner<'tcx>,
36+
//owner: &'tcx HirOwner<'tcx>,
3737
items: IndexVec<ItemLocalId, Option<HirItem<'tcx>>>,
3838
bodies: FxHashMap<ItemLocalId, &'tcx Body<'tcx>>,
3939
}

src/librustc_interface/passes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ pub fn create_global_ctxt<'tcx>(
716716
let defs = mem::take(&mut resolver_outputs.definitions);
717717

718718
// Construct the HIR map.
719-
let hir_map = map::map_crate(sess, &*resolver_outputs.cstore, krate, dep_graph, defs);
719+
let hir_map = map::map_crate(sess, &**arena, &*resolver_outputs.cstore, krate, dep_graph, defs);
720720

721721
let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);
722722

0 commit comments

Comments
 (0)