Skip to content

Commit

Permalink
Add fn fn_decl to Hir, for looking up the FnDecl of a body owner.
Browse files Browse the repository at this point in the history
  • Loading branch information
pnkfelix committed Jun 19, 2018
1 parent e848fe0 commit c5c4c5e
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/librustc/hir/map/mod.rs
Expand Up @@ -170,6 +170,40 @@ impl<'hir> MapEntry<'hir> {
})
}

fn fn_decl(&self) -> Option<&FnDecl> {
match self {
EntryItem(_, _, ref item) => {
match item.node {
ItemFn(ref fn_decl, _, _, _, _, _) => Some(&fn_decl),
_ => None,
}
}

EntryTraitItem(_, _, ref item) => {
match item.node {
TraitItemKind::Method(ref method_sig, _) => Some(&method_sig.decl),
_ => None
}
}

EntryImplItem(_, _, ref item) => {
match item.node {
ImplItemKind::Method(ref method_sig, _) => Some(&method_sig.decl),
_ => None,
}
}

EntryExpr(_, _, ref expr) => {
match expr.node {
ExprClosure(_, ref fn_decl, ..) => Some(&fn_decl),
_ => None,
}
}

_ => None
}
}

fn associated_body(self) -> Option<BodyId> {
match self {
EntryItem(_, _, item) => {
Expand Down Expand Up @@ -502,6 +536,14 @@ impl<'hir> Map<'hir> {
self.forest.krate.body(id)
}

pub fn fn_decl(&self, node_id: ast::NodeId) -> Option<FnDecl> {
if let Some(entry) = self.find_entry(node_id) {
entry.fn_decl().map(|fd| fd.clone())
} else {
bug!("no entry for node_id `{}`", 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

0 comments on commit c5c4c5e

Please sign in to comment.