Rollup merge of #58139 - ljedrz:HirIdification_phase_2.5, r=Zoxc

hir: add more HirId methods

Adds a few more methods operating on `HirId` instead of `NodeId` with the intention of replacing the old ones in the near future.

r? @Zoxc
This commit is contained in:
kennytm 2019-02-06 00:29:10 +09:00 committed by GitHub
commit 3fc7373eff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 60 additions and 0 deletions

View File

@ -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()
}
@ -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`.
@ -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) {
@ -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,
@ -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,
@ -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) {
@ -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] {
@ -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.)
///
@ -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))
}