From c5c4c5e93887b1e2355fe7fb86e622e6888eede7 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Thu, 7 Jun 2018 17:03:58 +0200 Subject: [PATCH] Add `fn fn_decl` to `Hir`, for looking up the `FnDecl` of a body owner. --- src/librustc/hir/map/mod.rs | 42 +++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 9df55e5206163..d2e04ef31c86a 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -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 { match self { EntryItem(_, _, item) => { @@ -502,6 +536,14 @@ impl<'hir> Map<'hir> { self.forest.krate.body(id) } + pub fn fn_decl(&self, node_id: ast::NodeId) -> Option { + 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`.