Skip to content

Commit c5c4c5e

Browse files
committed
Add fn fn_decl to Hir, for looking up the FnDecl of a body owner.
1 parent e848fe0 commit c5c4c5e

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/librustc/hir/map/mod.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,40 @@ impl<'hir> MapEntry<'hir> {
170170
})
171171
}
172172

173+
fn fn_decl(&self) -> Option<&FnDecl> {
174+
match self {
175+
EntryItem(_, _, ref item) => {
176+
match item.node {
177+
ItemFn(ref fn_decl, _, _, _, _, _) => Some(&fn_decl),
178+
_ => None,
179+
}
180+
}
181+
182+
EntryTraitItem(_, _, ref item) => {
183+
match item.node {
184+
TraitItemKind::Method(ref method_sig, _) => Some(&method_sig.decl),
185+
_ => None
186+
}
187+
}
188+
189+
EntryImplItem(_, _, ref item) => {
190+
match item.node {
191+
ImplItemKind::Method(ref method_sig, _) => Some(&method_sig.decl),
192+
_ => None,
193+
}
194+
}
195+
196+
EntryExpr(_, _, ref expr) => {
197+
match expr.node {
198+
ExprClosure(_, ref fn_decl, ..) => Some(&fn_decl),
199+
_ => None,
200+
}
201+
}
202+
203+
_ => None
204+
}
205+
}
206+
173207
fn associated_body(self) -> Option<BodyId> {
174208
match self {
175209
EntryItem(_, _, item) => {
@@ -502,6 +536,14 @@ impl<'hir> Map<'hir> {
502536
self.forest.krate.body(id)
503537
}
504538

539+
pub fn fn_decl(&self, node_id: ast::NodeId) -> Option<FnDecl> {
540+
if let Some(entry) = self.find_entry(node_id) {
541+
entry.fn_decl().map(|fd| fd.clone())
542+
} else {
543+
bug!("no entry for node_id `{}`", node_id)
544+
}
545+
}
546+
505547
/// Returns the `NodeId` that corresponds to the definition of
506548
/// which this is the body of, i.e. a `fn`, `const` or `static`
507549
/// item (possibly associated), a closure, or a `hir::AnonConst`.

0 commit comments

Comments
 (0)