Skip to content
Permalink
Browse files

HIR: introduce a HirId to DefId map in Definitions

  • Loading branch information...
ljedrz committed Jul 25, 2019
1 parent 2a8e17a commit 0baccc7e7593dac83b126675a561380231a74950
@@ -433,14 +433,17 @@ impl<'a> LoweringContext<'a> {
match tree.kind {
UseTreeKind::Simple(_, id1, id2) => {
for &id in &[id1, id2] {
self.lctx.resolver.definitions().create_def_with_parent(
let def_index = self.lctx.resolver.definitions().create_def_with_parent(
owner,
id,
None,
DefPathData::Misc,
ExpnId::root(),
tree.prefix.span,
);
self.lctx.allocate_hir_id_counter(id);
let hir_id = self.lctx.allocate_hir_id_counter(id);

self.lctx.resolver.definitions().hir_to_def_index.insert(hir_id, def_index);
}
}
UseTreeKind::Glob => (),
@@ -659,6 +662,10 @@ impl<'a> LoweringContext<'a> {
.definitions()
.init_node_id_to_hir_id_mapping(self.node_id_to_hir_id);

self.resolver
.definitions()
.finalize_hir_to_def_index_mapping();

hir::Crate {
module,
attrs,
@@ -964,17 +971,20 @@ impl<'a> LoweringContext<'a> {
),
};

let hir_id = self.lower_node_id(node_id);

// Add a definition for the in-band lifetime def.
self.resolver.definitions().create_def_with_parent(
parent_index,
node_id,
Some(hir_id),
DefPathData::LifetimeNs(str_name),
ExpnId::root(),
span,
);

hir::GenericParam {
hir_id: self.lower_node_id(node_id),
hir_id,
name: hir_name,
attrs: hir_vec![],
bounds: hir_vec![],
@@ -1462,6 +1472,7 @@ impl<'a> LoweringContext<'a> {
self.resolver.definitions().create_def_with_parent(
parent_def_index,
impl_trait_node_id,
None,
DefPathData::ImplTrait,
ExpnId::root(),
DUMMY_SP
@@ -1921,6 +1932,7 @@ impl<'a> LoweringContext<'a> {
self.context.resolver.definitions().create_def_with_parent(
self.parent,
def_node_id,
Some(hir_id),
DefPathData::LifetimeNs(name.ident().as_interned_str()),
ExpnId::root(),
lifetime.span);
@@ -28,7 +28,8 @@ impl<'a> DefCollector<'a> {
-> DefIndex {
let parent_def = self.parent_def;
debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def);
self.definitions.create_def_with_parent(parent_def, node_id, data, self.expansion, span)
self.definitions.create_def_with_parent(
parent_def, node_id, None, data, self.expansion, span)
}

pub fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: DefIndex, f: F) {
@@ -14,11 +14,12 @@ use crate::session::CrateDisambiguator;
use std::borrow::Borrow;
use std::fmt::Write;
use std::hash::Hash;
use std::mem;
use syntax::ast;
use syntax::ext::hygiene::ExpnId;
use syntax::symbol::{Symbol, sym, InternedString};
use syntax_pos::{Span, DUMMY_SP};
use crate::util::nodemap::NodeMap;
use crate::util::nodemap::{HirIdMap, NodeMap};

/// The DefPathTable maps DefIndexes to DefKeys and vice versa.
/// Internally the DefPathTable holds a tree of DefKeys, where each DefKey
@@ -91,6 +92,10 @@ impl DefPathTable {
pub struct Definitions {
table: DefPathTable,
node_to_def_index: NodeMap<DefIndex>,
pub hir_to_def_index: HirIdMap<DefIndex>,
/// `DefIndex`es created by DefCollector::create_def; used only to complete the hir_to_def_index
/// mapping after the lowering
defs_awaiting_hir_id: NodeMap<DefIndex>,
def_index_to_node: Vec<ast::NodeId>,
pub(super) node_to_hir_id: IndexVec<ast::NodeId, hir::HirId>,
/// If `ExpnId` is an ID of some macro expansion,
@@ -353,11 +358,21 @@ impl Definitions {
self.node_to_def_index.get(&node).cloned()
}

#[inline]
pub fn opt_def_index_from_hir_id(&self, hir: hir::HirId) -> Option<DefIndex> {
self.hir_to_def_index.get(&hir).cloned()
}

#[inline]
pub fn opt_local_def_id(&self, node: ast::NodeId) -> Option<DefId> {
self.opt_def_index(node).map(DefId::local)
}

#[inline]
pub fn opt_local_def_id_from_hir_id(&self, hir: hir::HirId) -> Option<DefId> {
self.opt_def_index_from_hir_id(hir).map(DefId::local)
}

#[inline]
pub fn local_def_id(&self, node: ast::NodeId) -> DefId {
self.opt_local_def_id(node).unwrap()
@@ -437,6 +452,7 @@ impl Definitions {
assert!(self.def_index_to_node.is_empty());
self.def_index_to_node.push(ast::CRATE_NODE_ID);
self.node_to_def_index.insert(ast::CRATE_NODE_ID, root_index);
self.hir_to_def_index.insert(hir::CRATE_HIR_ID, root_index);
self.set_invocation_parent(ExpnId::root(), root_index);

// Allocate some other DefIndices that always must exist.
@@ -449,6 +465,7 @@ impl Definitions {
pub fn create_def_with_parent(&mut self,
parent: DefIndex,
node_id: ast::NodeId,
hir_id: Option<hir::HirId>,
data: DefPathData,
expn_id: ExpnId,
span: Span)
@@ -496,6 +513,12 @@ impl Definitions {
if node_id != ast::DUMMY_NODE_ID {
debug!("create_def_with_parent: def_index_to_node[{:?} <-> {:?}", index, node_id);
self.node_to_def_index.insert(node_id, index);

if let Some(hir_id) = hir_id {
self.hir_to_def_index.insert(hir_id, index);
} else {
self.defs_awaiting_hir_id.insert(node_id, index);
}
}

if expn_id != ExpnId::root() {
@@ -519,6 +542,15 @@ impl Definitions {
self.node_to_hir_id = mapping;
}

/// Fill the missing bits of the HirId to DefIndex mapping (the DefIndices created by
/// DefCollector::create_def can't be assigned to HirIds at that stage yet)
pub fn finalize_hir_to_def_index_mapping(&mut self) {
for (node_id, def_id) in mem::replace(&mut self.defs_awaiting_hir_id, Default::default()) {
let hir_id = self.node_to_hir_id[node_id];
self.hir_to_def_index.insert(hir_id, def_id);
}
}

pub fn expansion_that_defined(&self, index: DefIndex) -> ExpnId {
self.expansions_that_defined.get(&index).cloned().unwrap_or(ExpnId::root())
}
@@ -623,6 +655,7 @@ macro_rules! define_global_metadata_kind {
definitions.create_def_with_parent(
CRATE_DEF_INDEX,
ast::DUMMY_NODE_ID,
Some(hir::DUMMY_HIR_ID),
DefPathData::GlobalMetaData(instance.name().as_interned_str()),
ExpnId::root(),
DUMMY_SP
@@ -248,8 +248,7 @@ impl<'hir> Map<'hir> {

#[inline]
pub fn opt_local_def_id(&self, hir_id: HirId) -> Option<DefId> {
let node_id = self.hir_to_node_id(hir_id);
self.definitions.opt_local_def_id(node_id)
self.definitions.opt_local_def_id_from_hir_id(hir_id)
}

#[inline]
@@ -457,6 +457,7 @@ crate fn proc_macro_def_path_table(crate_root: &CrateRoot<'_>,
let def_index = definitions.create_def_with_parent(
crate_root,
ast::DUMMY_NODE_ID,
Some(hir::DUMMY_HIR_ID),
DefPathData::MacroNs(name.as_interned_str()),
ExpnId::root(),
DUMMY_SP);

0 comments on commit 0baccc7

Please sign in to comment.
You can’t perform that action at this time.