]> git.lizzy.rs Git - rust.git/commitdiff
Add `fn fn_decl` to `Hir`, for looking up the `FnDecl` of a body owner.
authorFelix S. Klock II <pnkfelix@pnkfx.org>
Thu, 7 Jun 2018 15:03:58 +0000 (17:03 +0200)
committerFelix S. Klock II <pnkfelix@pnkfx.org>
Tue, 19 Jun 2018 17:38:37 +0000 (19:38 +0200)
src/librustc/hir/map/mod.rs

index 9df55e52061634962c67b5e5dcd338cf497f1fea..d2e04ef31c86afffe55638868b4cbd52ea1824c6 100644 (file)
@@ -170,6 +170,40 @@ fn to_node(self) -> Option<Node<'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) => {
@@ -502,6 +536,14 @@ pub fn body(&self, id: BodyId) -> &'hir Body {
         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`.