forked from dobkeratops/rustfind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visit_rust_ast.rs
331 lines (266 loc) · 10.5 KB
/
visit_rust_ast.rs
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
use rf_common::*;
pub use syntax::{ast,abi};
pub use syntax::visit;
pub use syntax::parse::token;
pub use syntax::visit::{Visitor};
pub use syntax::codemap;
pub use syntax::codemap::BytePos;
pub use rustfindctx::*;
//use rustc::middle::mem_categorization::ast_node;
use rustc::middle::ty;
use rustfindctx::{RustFindCtx,};
use codemaput::{ZTextFilePos,ToZIndexFilePos,dump_span,get_span_str};
use find_ast_node::{AstNodeAccessors,FNodeInfo,FNodeInfoMap,AstNode_,mkAstSPtrClone,AstSPtr,astnode_trait_ref,astnode_variant,astnode_item,KindToStr,NodeTreeLoc,NodeKind};
use fa=find_ast_node;
pub struct FNodeInfoMapBuilder<'astl> {
pub all_nodes: FNodeInfoMap<'astl>
}
pub fn FNodeInfoMapBuilder_new<'astl>() -> FNodeInfoMapBuilder<'astl> {
FNodeInfoMapBuilder::<'astl> {
all_nodes: FNodeInfoMap::new()
}
}
impl<'astl> self::FNodeInfoMapBuilder<'astl> {
// use visit_rust_ast::FNodeInfoMapBuilder;
pub fn trait_ref(&mut self, tr:&ast::TraitRef, p: ast::NodeId) {
push_span(&mut self.all_nodes, tr.ref_id, p,None, fa::NK_TraitRef, tr.path.span, astnode_trait_ref(mkAstSPtrClone(tr)) );
}
pub fn variant(&mut self, va:&ast::Variant, p: ast::NodeId) {
push_span(&mut self.all_nodes, va.node.id,p, Some(va.node.name),fa::NK_Variant, va.span, astnode_variant(mkAstSPtrClone(&va.node)))
// visit_item(va,(s,va.node.id,v)) - TODO , are we actually suppoed to iterate here? why was't it done
}
}
pub fn rf_push_parent_child<'astl>(spt:&mut FNodeInfoMap<'astl>, parent_id:ast::NodeId, child_id: ast::NodeId) {
let parent_node = spt.find_mut(&parent_id);
match parent_node {
Some(p)=> p.children.push(child_id),
_=>{},
}
}
pub fn push_span<'astl>(spt:&mut FNodeInfoMap<'astl>,node_id:ast::NodeId, parent:ast::NodeId, _:Option<ast::Ident>,k:NodeKind, s:codemap::Span,nd:AstNode_<'astl>) {
// grr. we would really like to traverse children and gather array of ptrs to chilren as a child pointer,
// instead we're 'push_back' reallocing all over the place, yuk.
spt.insert(node_id,
FNodeInfo{
// id: node_id,
// ident:nd.rf_get_ident(),
kind:k,
span:s,node:nd,
parent_id:parent,
children:Vec::new()
}
);
rf_push_parent_child(spt, parent,node_id);
}
pub fn push_spanned<T:AstNodeAccessors>(spt:&mut FNodeInfoMap,k:NodeKind,s:&codemap::Spanned<T>,ast_node:AstNode_,parent:ast::NodeId) {
match s.node.rf_get_id() {
Some(node_id)=>{
spt.insert(node_id,
FNodeInfo{
// id:node_id,
// ident:ast_node.rf_get_ident(),
kind:k,
span:s.span,
node:ast_node,
parent_id:parent,
children:Vec::new()
}
);
rf_push_parent_child(spt, parent,node_id);
},
None=>{}
}
}
impl<'astl> Visitor<ast::NodeId> for FNodeInfoMapBuilder<'astl> {
// use default impl
// fn visit_view_item(&mut self, a:&ast::ViewItem, p: ast::NodeId) {
// walk_view_item(self, a, s);
// }
fn visit_generics(&mut self, g:&ast::Generics, p: ast::NodeId) {
for typ in g.ty_params.iter() {
// unfortunately no span for type param
// push_span(&mut self.node_spans, g.def_id.id, Some(g.ident), "ty_param_def", g.def_id.span, astnode_ty_param_def(tp))
for type_bound in typ.bounds.iter() {
match type_bound {
&ast::TraitTyParamBound(ref tr) => {
self.trait_ref(tr, p);
}
_ => {}
}
}
}
visit::walk_generics(self, g, p);
}
fn visit_item(&mut self, a:&ast::Item, p: ast::NodeId) {
push_span(&mut self.all_nodes,a.id,p,item_get_ident(a),a.get_kind(),a.span,astnode_item(mkAstSPtrClone(a)));
// TODO: Push nodes for type-params... since we want to click on their defs...
match a.node {
ast::ItemImpl(_, ref o_traitref, _, ref methods) => {
// self.visit_generics(g, p);
match *o_traitref {
None => {}
Some(ref tr) => self.trait_ref(tr, p),
}
for m in methods.iter() {
push_span(&mut self.all_nodes, m.id, p, Some(a.ident), fa::NK_Method, m.span, fa::astnode_method(*m));
}
}
ast::ItemEnum(ref ed, _) => {
for v in ed.variants.iter() {
self.variant(*v, p);
}
}
ast::ItemTrait(_,_, ref tr, _) => {
for t in tr.iter() {
self.trait_ref(t, p);
}
}
_ => {}
}
visit::walk_item(self, a, a.id);
}
fn visit_local(&mut self, a:&ast::Local, p: ast::NodeId) {
push_span(&mut self.all_nodes, a.id, p, None, fa::NK_Local, a.span, fa::astnode_local(@*a.clone())); // How to remove this?!
visit::walk_local(self, a, a.id);
}
fn visit_block(&mut self, a: &ast::Block, p: ast::NodeId) {
push_span(&mut self.all_nodes, a.id, p, None, fa::NK_Block, a.span, fa::astnode_none);
visit::walk_block(self, a, a.id);
}
fn visit_stmt(&mut self, a:&ast::Stmt, p: ast::NodeId) {
push_spanned(&mut self.all_nodes, fa::NK_Stmt, a, fa::astnode_stmt(mkAstSPtrClone(a)), p);
visit::walk_stmt(self, a, p);
}
// we do nothing yet, use default impl
// fn visit_arm(&mut self, a:&ast::Arm, p: ast::NodeId) {}
fn visit_pat(&mut self, a: &ast::Pat, p: ast::NodeId) {
push_span(&mut self.all_nodes, a.id, p, None, fa::NK_Pat, a.span, fa::astnode_pat(mkAstSPtrClone(a)));
visit::walk_pat(self, a, a.id);
}
fn visit_decl(&mut self, a:&ast::Decl, p: ast::NodeId) {
// TODO - check if 'decl' should be emitted at all - shouldn't we drill down and get a specific decl .. (struct,fn..)
push_spanned(&mut self.all_nodes, fa::NK_Decl, a, fa::astnode_decl(@*a), p);
visit::walk_decl(self, a, p);
}
// we do nothing, use default for now
// fn visit_struct_def(&mut self, s)
fn visit_expr(&mut self, a:&ast::Expr, p: ast::NodeId) {
push_span(&mut self.all_nodes, a.id, p, expr_get_ident(a), a.get_kind(), a.span, fa::astnode_expr(mkAstSPtrClone(a)));
visit::walk_expr(self, a, a.id);
}
// default, we do nothing
// fn visit_expr_post()
fn visit_ty(&mut self, a:&ast::Ty, p: ast::NodeId) {
push_span(&mut self.all_nodes, a.id, p, None, fa::NK_Type, a.span, fa::astnode_ty(mkAstSPtrClone(a)));
visit::walk_ty(self, a, a.id);
}
// default, we do nothing
// fn visit_fn()
fn visit_struct_field(&mut self, a: &ast::StructField, p: ast::NodeId) {
push_spanned(&mut self.all_nodes, fa::NK_StructField, a, fa::astnode_struct_field(mkAstSPtrClone(a)), p);
visit::walk_struct_field(self, a, p);
}
fn visit_ty_method(&mut self, a:&ast::TypeMethod, p: ast::NodeId) {
push_span(&mut self.all_nodes, a.id, p, Some(a.ident), fa::NK_TyMethod, a.span, fa::astnode_ty_method(mkAstSPtrClone(a)));
visit::walk_ty_method(self, a, a.id);
}
}
fn item_get_ident(a:&ast::Item)->Option<ast::Ident> { Some(a.ident) }
fn expr_get_ident(_ :&ast::Expr)->Option<ast::Ident> {
None
}
#[deriving(Clone)]
pub struct FindAstNodeSt<'astl> {
pub result: NodeTreeLoc<'astl>, // todo - full tree path, all the parent nodes.
pub location: u32,
pub stop: bool,
// node_spans: HashMap<ast::node_id,codemap::span>
}
pub struct Finder<'astl> {
pub env: FindAstNodeSt<'astl>
}
impl<'astl> Finder<'astl> {
pub fn new (location: u32) -> Finder<'astl> {
let env = FindAstNodeSt{
result:Vec::from_elem(1,fa::astnode_root), location:location, stop:false
};
Finder {
env: env
}
}
}
pub fn span_contains(x: u32, s: codemap::Span)->bool {
BytePos(x)>=s.lo && BytePos(x)<s.hi
}
impl<'astl> Visitor<()> for Finder<'astl> {
fn visit_view_item(&mut self, a:&ast::ViewItem, _: ()) {
if span_contains(self.env.location, a.span) {
self.env.result.push(fa::astnode_view_item(mkAstSPtrClone(a)));;
}
visit::walk_view_item(self, a, ());
}
fn visit_item(&mut self, a:&ast::Item, _: ()) {
if span_contains(self.env.location, a.span) {
self.env.result.push(fa::astnode_item(mkAstSPtrClone(a)));
}
visit::walk_item(self, a, ());
}
fn visit_local(&mut self, a:&ast::Local, _: ()) {
if span_contains(self.env.location, a.span) {
self.env.result.push(fa::astnode_local(@*a.clone()));
}
visit::walk_local(self, a, ());
}
fn visit_block(&mut self, a:&ast::Block, _: ()) {
if span_contains(self.env.location, a.span) {
self.env.result.push(fa::astnode_block(mkAstSPtrClone(a)));
}
visit::walk_block(self, a, ());
}
fn visit_stmt(&mut self, a:&ast::Stmt, _: ()) {
if span_contains(self.env.location, a.span) {
self.env.result.push(fa::astnode_stmt(mkAstSPtrClone(a)));
}
visit::walk_stmt(self, a, ());
}
fn visit_arm(&mut self, a:&ast::Arm, _: ()) {
// The whole arm doesn't have a span.
// if span.contains(self.env.location, a.span) {
// self.env.result.push(astnode_pat(a));
// }
visit::walk_arm(self, a, ());
}
fn visit_pat(&mut self, a: &ast::Pat, _: ()) {
if span_contains(self.env.location, a.span) {
self.env.result.push(fa::astnode_pat(mkAstSPtrClone(a)));
}
visit::walk_pat(self, a, ());
}
fn visit_decl(&mut self, a:&ast::Decl, _: ()) {
if span_contains(self.env.location, a.span) {
self.env.result.push(fa::astnode_decl(@*a));
}
visit::walk_decl(self, a, ());
}
fn visit_expr(&mut self, a:&ast::Expr, _: ()) {
if span_contains(self.env.location, a.span) {
self.env.result.push(fa::astnode_expr(mkAstSPtrClone(a)));
}
visit::walk_expr(self, a, ());
}
fn visit_ty(&mut self, a:&ast::Ty, _: ()) {
if span_contains(self.env.location, a.span) {
self.env.result.push(fa::astnode_ty(mkAstSPtrClone(a)));
}
visit::walk_ty(self, a, ());
}
// use default impl for now as we don't do anything here
// fn visit_fn(fk:&vist::fn_kind, fd:&as::fn_decl, body:&ast::Block,
// sp:codemap::Span, nid:ast::NodeId, s: FindAstNodeSt) {}
fn visit_struct_field(&mut self, a: &ast::StructField, _: ()) {
if span_contains(self.env.location, a.span) {
self.env.result.push(fa::astnode_struct_field(mkAstSPtrClone(a)));
}
visit::walk_struct_field(self, a, ());
}
}