Skip to content

Commit

Permalink
hir: more HirId methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ljedrz committed Feb 4, 2019
1 parent 4314dba commit 927614f
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,12 @@ impl<'hir> Map<'hir> {
}
}

// FIXME(@ljedrz): replace the NodeId variant
pub fn describe_def_by_hir_id(&self, hir_id: HirId) -> Option<Def> {
let node_id = self.hir_to_node_id(hir_id);
self.describe_def(node_id)
}

fn entry_count(&self) -> usize {
self.map.len()
}
Expand Down Expand Up @@ -445,6 +451,12 @@ impl<'hir> Map<'hir> {
}
}

// FIXME(@ljedrz): replace the NodeId variant
pub fn fn_decl_by_hir_id(&self, hir_id: HirId) -> Option<FnDecl> {
let node_id = self.hir_to_node_id(hir_id);
self.fn_decl(node_id)
}

/// Returns the `NodeId` that corresponds to the definition of
/// which this is the body of, i.e., a `fn`, `const` or `static`
/// item (possibly associated), a closure, or a `hir::AnonConst`.
Expand Down Expand Up @@ -855,6 +867,12 @@ impl<'hir> Map<'hir> {
self.local_def_id(self.get_parent(id))
}

// FIXME(@ljedrz): replace the NodeId variant
pub fn get_parent_did_by_hir_id(&self, id: HirId) -> DefId {
let node_id = self.hir_to_node_id(id);
self.get_parent_did(node_id)
}

pub fn get_foreign_abi(&self, id: NodeId) -> Abi {
let parent = self.get_parent(id);
if let Some(entry) = self.find_entry(parent) {
Expand All @@ -868,6 +886,12 @@ impl<'hir> Map<'hir> {
bug!("expected foreign mod or inlined parent, found {}", self.node_to_string(parent))
}

// FIXME(@ljedrz): replace the NodeId variant
pub fn get_foreign_abi_by_hir_id(&self, id: HirId) -> Abi {
let node_id = self.hir_to_node_id(id);
self.get_foreign_abi(node_id)
}

pub fn expect_item(&self, id: NodeId) -> &'hir Item {
match self.find(id) { // read recorded by `find`
Some(Node::Item(item)) => item,
Expand All @@ -888,6 +912,18 @@ impl<'hir> Map<'hir> {
}
}

// FIXME(@ljedrz): replace the NodeId variant
pub fn expect_impl_item_by_hir_id(&self, id: HirId) -> &'hir ImplItem {
let node_id = self.hir_to_node_id(id);
self.expect_impl_item(node_id)
}

// FIXME(@ljedrz): replace the NodeId variant
pub fn expect_trait_item_by_hir_id(&self, id: HirId) -> &'hir TraitItem {
let node_id = self.hir_to_node_id(id);
self.expect_trait_item(node_id)
}

pub fn expect_trait_item(&self, id: NodeId) -> &'hir TraitItem {
match self.find(id) {
Some(Node::TraitItem(item)) => item,
Expand Down Expand Up @@ -931,6 +967,12 @@ impl<'hir> Map<'hir> {
}
}

// FIXME(@ljedrz): replace the NodeId variant
pub fn expect_expr_by_hir_id(&self, id: HirId) -> &'hir Expr {
let node_id = self.hir_to_node_id(id);
self.expect_expr(node_id)
}

/// Returns the name associated with the given NodeId's AST.
pub fn name(&self, id: NodeId) -> Name {
match self.get(id) {
Expand All @@ -948,6 +990,12 @@ impl<'hir> Map<'hir> {
}
}

// FIXME(@ljedrz): replace the NodeId variant
pub fn name_by_hir_id(&self, id: HirId) -> Name {
let node_id = self.hir_to_node_id(id);
self.name(node_id)
}

/// Given a node ID, get a list of attributes associated with the AST
/// corresponding to the Node ID
pub fn attrs(&self, id: NodeId) -> &'hir [ast::Attribute] {
Expand All @@ -970,6 +1018,12 @@ impl<'hir> Map<'hir> {
attrs.unwrap_or(&[])
}

// FIXME(@ljedrz): replace the NodeId variant
pub fn attrs_by_hir_id(&self, id: HirId) -> &'hir [ast::Attribute] {
let node_id = self.hir_to_node_id(id);
self.attrs(node_id)
}

/// Returns an iterator that yields the node id's with paths that
/// match `parts`. (Requires `parts` is non-empty.)
///
Expand Down Expand Up @@ -1019,6 +1073,12 @@ impl<'hir> Map<'hir> {
}
}

// FIXME(@ljedrz): replace the NodeId variant
pub fn span_by_hir_id(&self, id: HirId) -> Span {
let node_id = self.hir_to_node_id(id);
self.span(node_id)
}

pub fn span_if_local(&self, id: DefId) -> Option<Span> {
self.as_local_node_id(id).map(|id| self.span(id))
}
Expand Down

0 comments on commit 927614f

Please sign in to comment.