1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
pub use self::Code::*;
use abi;
use ast::{Block, FnDecl, NodeId};
use ast;
use ast_map::{Node};
use ast_map;
use ast_util::PostExpansionMethod;
use codemap::Span;
use visit;
#[derive(Copy)]
pub struct FnLikeNode<'a> { node: ast_map::Node<'a> }
pub trait MaybeFnLike { fn is_fn_like(&self) -> bool; }
pub struct FnParts<'a> {
pub decl: &'a FnDecl,
pub body: &'a Block,
pub kind: visit::FnKind<'a>,
pub span: Span,
pub id: NodeId,
}
impl MaybeFnLike for ast::Item {
fn is_fn_like(&self) -> bool {
match self.node { ast::ItemFn(..) => true, _ => false, }
}
}
impl MaybeFnLike for ast::TraitItem {
fn is_fn_like(&self) -> bool {
match *self { ast::ProvidedMethod(_) => true, _ => false, }
}
}
impl MaybeFnLike for ast::Expr {
fn is_fn_like(&self) -> bool {
match self.node {
ast::ExprClosure(..) => true,
_ => false,
}
}
}
#[derive(Copy)]
pub enum Code<'a> {
FnLikeCode(FnLikeNode<'a>),
BlockCode(&'a Block),
}
impl<'a> Code<'a> {
pub fn id(&self) -> ast::NodeId {
match *self {
FnLikeCode(node) => node.id(),
BlockCode(block) => block.id,
}
}
pub fn from_node(node: Node) -> Option<Code> {
fn new(node: Node) -> FnLikeNode { FnLikeNode { node: node } }
match node {
ast_map::NodeItem(item) if item.is_fn_like() =>
Some(FnLikeCode(new(node))),
ast_map::NodeTraitItem(tm) if tm.is_fn_like() =>
Some(FnLikeCode(new(node))),
ast_map::NodeImplItem(_) =>
Some(FnLikeCode(new(node))),
ast_map::NodeExpr(e) if e.is_fn_like() =>
Some(FnLikeCode(new(node))),
ast_map::NodeBlock(block) =>
Some(BlockCode(block)),
_ =>
None,
}
}
}
struct ItemFnParts<'a> {
ident: ast::Ident,
decl: &'a ast::FnDecl,
unsafety: ast::Unsafety,
abi: abi::Abi,
generics: &'a ast::Generics,
body: &'a Block,
id: ast::NodeId,
span: Span
}
struct ClosureParts<'a> {
decl: &'a FnDecl,
body: &'a Block,
id: NodeId,
span: Span
}
impl<'a> ClosureParts<'a> {
fn new(d: &'a FnDecl, b: &'a Block, id: NodeId, s: Span) -> ClosureParts<'a> {
ClosureParts { decl: d, body: b, id: id, span: s }
}
}
impl<'a> FnLikeNode<'a> {
pub fn to_fn_parts(self) -> FnParts<'a> {
FnParts {
decl: self.decl(),
body: self.body(),
kind: self.kind(),
span: self.span(),
id: self.id(),
}
}
pub fn body(self) -> &'a Block {
self.handle(|i: ItemFnParts<'a>| &*i.body,
|m: &'a ast::Method| m.pe_body(),
|c: ClosureParts<'a>| c.body)
}
pub fn decl(self) -> &'a FnDecl {
self.handle(|i: ItemFnParts<'a>| &*i.decl,
|m: &'a ast::Method| m.pe_fn_decl(),
|c: ClosureParts<'a>| c.decl)
}
pub fn span(self) -> Span {
self.handle(|i: ItemFnParts| i.span,
|m: &'a ast::Method| m.span,
|c: ClosureParts| c.span)
}
pub fn id(self) -> NodeId {
self.handle(|i: ItemFnParts| i.id,
|m: &'a ast::Method| m.id,
|c: ClosureParts| c.id)
}
pub fn kind(self) -> visit::FnKind<'a> {
let item = |p: ItemFnParts<'a>| -> visit::FnKind<'a> {
visit::FkItemFn(p.ident, p.generics, p.unsafety, p.abi)
};
let closure = |_: ClosureParts| {
visit::FkFnBlock
};
let method = |m: &'a ast::Method| {
visit::FkMethod(m.pe_ident(), m.pe_generics(), m)
};
self.handle(item, method, closure)
}
fn handle<A, I, M, C>(self, item_fn: I, method: M, closure: C) -> A where
I: FnOnce(ItemFnParts<'a>) -> A,
M: FnOnce(&'a ast::Method) -> A,
C: FnOnce(ClosureParts<'a>) -> A,
{
match self.node {
ast_map::NodeItem(i) => match i.node {
ast::ItemFn(ref decl, unsafety, abi, ref generics, ref block) =>
item_fn(ItemFnParts{
ident: i.ident, decl: &**decl, unsafety: unsafety, body: &**block,
generics: generics, abi: abi, id: i.id, span: i.span
}),
_ => panic!("item FnLikeNode that is not fn-like"),
},
ast_map::NodeTraitItem(t) => match *t {
ast::ProvidedMethod(ref m) => method(&**m),
_ => panic!("trait method FnLikeNode that is not fn-like"),
},
ast_map::NodeImplItem(ii) => {
match *ii {
ast::MethodImplItem(ref m) => method(&**m),
ast::TypeImplItem(_) => {
panic!("impl method FnLikeNode that is not fn-like")
}
}
}
ast_map::NodeExpr(e) => match e.node {
ast::ExprClosure(_, ref decl, ref block) =>
closure(ClosureParts::new(&**decl, &**block, e.id, e.span)),
_ => panic!("expr FnLikeNode that is not fn-like"),
},
_ => panic!("other FnLikeNode that is not fn-like"),
}
}
}