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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use abi::AbiSet;
use ast::*;
use ast;
use ast_util::{inlined_item_utils, stmt_id};
use ast_util;
use codemap;
use diagnostic::span_handler;
use parse::token::ident_interner;
use print::pprust;
use visit;
use syntax::parse::token::special_idents;
use core::hashmap::HashMap;
pub enum path_elt {
path_mod(ident),
path_name(ident)
}
impl cmp::Eq for path_elt {
fn eq(&self, other: &path_elt) -> bool {
match (*self) {
path_mod(e0a) => {
match (*other) {
path_mod(e0b) => e0a == e0b,
_ => false
}
}
path_name(e0a) => {
match (*other) {
path_name(e0b) => e0a == e0b,
_ => false
}
}
}
}
fn ne(&self, other: &path_elt) -> bool { !(*self).eq(other) }
}
pub type path = ~[path_elt];
pub fn path_to_str_with_sep(p: &[path_elt], sep: ~str, itr: @ident_interner)
-> ~str {
let strs = do p.map |e| {
match *e {
path_mod(s) => copy *itr.get(s),
path_name(s) => copy *itr.get(s)
}
};
str::connect(strs, sep)
}
pub fn path_ident_to_str(p: &path, i: ident, itr: @ident_interner) -> ~str {
if vec::is_empty(*p) {
//FIXME /* FIXME (#2543) */ copy *i
copy *itr.get(i)
} else {
fmt!("%s::%s", path_to_str(*p, itr), *itr.get(i))
}
}
pub fn path_to_str(p: &[path_elt], itr: @ident_interner) -> ~str {
path_to_str_with_sep(p, ~"::", itr)
}
pub fn path_elt_to_str(pe: path_elt, itr: @ident_interner) -> ~str {
match pe {
path_mod(s) => copy *itr.get(s),
path_name(s) => copy *itr.get(s)
}
}
pub enum ast_node {
node_item(@item, @path),
node_foreign_item(@foreign_item, AbiSet, visibility, @path),
node_trait_method(@trait_method, def_id /* trait did */,
@path /* path to the trait */),
node_method(@method, def_id /* impl did */, @path /* path to the impl */),
node_variant(variant, @item, @path),
node_expr(@expr),
node_stmt(@stmt),
node_arg,
node_local(ident),
node_block(blk),
node_struct_ctor(@struct_def, @item, @path),
node_callee_scope(@expr)
}
pub type map = @mut HashMap<node_id, ast_node>;
pub struct Ctx {
map: map,
path: path,
diag: @span_handler,
}
pub type vt = visit::vt<@mut Ctx>;
pub fn extend(cx: @mut Ctx, elt: ident) -> @path {
@(vec::append(copy cx.path, ~[path_name(elt)]))
}
pub fn mk_ast_map_visitor() -> vt {
return visit::mk_vt(@visit::Visitor {
visit_item: map_item,
visit_expr: map_expr,
visit_stmt: map_stmt,
visit_fn: map_fn,
visit_block: map_block,
visit_pat: map_pat,
.. *visit::default_visitor()
});
}
pub fn map_crate(diag: @span_handler, c: @crate) -> map {
let cx = @mut Ctx {
map: @mut HashMap::new(),
path: ~[],
diag: diag,
};
visit::visit_crate(c, cx, mk_ast_map_visitor());
cx.map
}
// Used for items loaded from external crate that are being inlined into this
// crate. The `path` should be the path to the item but should not include
// the item itself.
pub fn map_decoded_item(diag: @span_handler,
map: map,
path: path,
ii: &inlined_item) {
// I believe it is ok for the local IDs of inlined items from other crates
// to overlap with the local ids from this crate, so just generate the ids
// starting from 0. (In particular, I think these ids are only used in
// alias analysis, which we will not be running on the inlined items, and
// even if we did I think it only needs an ordering between local
// variables that are simultaneously in scope).
let cx = @mut Ctx {
map: map,
path: copy path,
diag: diag,
};
let v = mk_ast_map_visitor();
// methods get added to the AST map when their impl is visited. Since we
// don't decode and instantiate the impl, but just the method, we have to
// add it to the table now:
match *ii {
ii_item(*) => { /* fallthrough */ }
ii_foreign(i) => {
cx.map.insert(i.id, node_foreign_item(i,
AbiSet::Intrinsic(),
i.vis, // Wrong but OK
@path));
}
ii_method(impl_did, m) => {
map_method(impl_did, @path, m, cx);
}
}
// visit the item / method contents and add those to the map:
ii.accept(cx, v);
}
pub fn map_fn(
fk: &visit::fn_kind,
decl: &fn_decl,
body: &blk,
sp: codemap::span,
id: node_id,
cx: @mut Ctx,
v: visit::vt<@mut Ctx>
) {
for decl.inputs.each |a| {
cx.map.insert(a.id, node_arg);
}
visit::visit_fn(fk, decl, body, sp, id, cx, v);
}
pub fn map_block(b: &blk, cx: @mut Ctx, v: visit::vt<@mut Ctx>) {
cx.map.insert(b.node.id, node_block(/* FIXME (#2543) */ copy *b));
visit::visit_block(b, cx, v);
}
pub fn map_pat(pat: @pat, cx: @mut Ctx, v: visit::vt<@mut Ctx>) {
match pat.node {
pat_ident(_, path, _) => {
// Note: this is at least *potentially* a pattern...
cx.map.insert(pat.id, node_local(ast_util::path_to_ident(path)));
}
_ => ()
}
visit::visit_pat(pat, cx, v);
}
pub fn map_method(impl_did: def_id, impl_path: @path,
m: @method, cx: @mut Ctx) {
cx.map.insert(m.id, node_method(m, impl_did, impl_path));
cx.map.insert(m.self_id, node_local(special_idents::self_));
}
pub fn map_item(i: @item, cx: @mut Ctx, v: visit::vt<@mut Ctx>) {
let item_path = @/* FIXME (#2543) */ copy cx.path;
cx.map.insert(i.id, node_item(i, item_path));
match i.node {
item_impl(_, _, _, ref ms) => {
let impl_did = ast_util::local_def(i.id);
for ms.each |m| {
map_method(impl_did, extend(cx, i.ident), *m, cx);
}
}
item_enum(ref enum_definition, _) => {
for (*enum_definition).variants.each |v| {
cx.map.insert(v.node.id, node_variant(
/* FIXME (#2543) */ copy *v, i,
extend(cx, i.ident)));
}
}
item_foreign_mod(ref nm) => {
for nm.items.each |nitem| {
// Compute the visibility for this native item.
let visibility = match nitem.vis {
public => public,
private => private,
inherited => i.vis
};
cx.map.insert(nitem.id,
node_foreign_item(
*nitem,
nm.abis,
visibility,
// FIXME (#2543)
if nm.sort == ast::named {
extend(cx, i.ident)
} else {
// Anonymous extern mods go in the parent scope
@copy cx.path
}
)
);
}
}
item_struct(struct_def, _) => {
map_struct_def(
struct_def,
node_item(i, item_path),
i.ident,
cx,
v
);
}
item_trait(_, ref traits, ref methods) => {
for traits.each |p| {
cx.map.insert(p.ref_id, node_item(i, item_path));
}
for methods.each |tm| {
let id = ast_util::trait_method_to_ty_method(tm).id;
let d_id = ast_util::local_def(i.id);
cx.map.insert(
id,
node_trait_method(@copy *tm, d_id, item_path)
);
}
}
_ => ()
}
match i.node {
item_mod(_) | item_foreign_mod(_) => {
cx.path.push(path_mod(i.ident));
}
_ => cx.path.push(path_name(i.ident))
}
visit::visit_item(i, cx, v);
cx.path.pop();
}
pub fn map_struct_def(
struct_def: @ast::struct_def,
parent_node: ast_node,
ident: ast::ident,
cx: @mut Ctx,
_v: visit::vt<@mut Ctx>
) {
let p = extend(cx, ident);
// If this is a tuple-like struct, register the constructor.
match struct_def.ctor_id {
None => {}
Some(ctor_id) => {
match parent_node {
node_item(item, _) => {
cx.map.insert(ctor_id,
node_struct_ctor(struct_def, item, p));
}
_ => fail!("struct def parent wasn't an item")
}
}
}
}
pub fn map_expr(ex: @expr, cx: @mut Ctx, v: visit::vt<@mut Ctx>) {
cx.map.insert(ex.id, node_expr(ex));
match ex.node {
// Expressions which are or might be calls:
ast::expr_call(*) |
ast::expr_method_call(*) |
ast::expr_index(*) |
ast::expr_binary(*) |
ast::expr_assign_op(*) |
ast::expr_unary(*) => {
cx.map.insert(ex.callee_id, node_callee_scope(ex));
}
_ => {}
}
visit::visit_expr(ex, cx, v);
}
pub fn map_stmt(stmt: @stmt, cx: @mut Ctx, v: visit::vt<@mut Ctx>) {
cx.map.insert(stmt_id(stmt), node_stmt(stmt));
visit::visit_stmt(stmt, cx, v);
}
pub fn node_id_to_str(map: map, id: node_id, itr: @ident_interner) -> ~str {
match map.find(&id) {
None => {
fmt!("unknown node (id=%d)", id)
}
Some(&node_item(item, path)) => {
let path_str = path_ident_to_str(path, item.ident, itr);
let item_str = match item.node {
item_const(*) => ~"const",
item_fn(*) => ~"fn",
item_mod(*) => ~"mod",
item_foreign_mod(*) => ~"foreign mod",
item_ty(*) => ~"ty",
item_enum(*) => ~"enum",
item_struct(*) => ~"struct",
item_trait(*) => ~"trait",
item_impl(*) => ~"impl",
item_mac(*) => ~"macro"
};
fmt!("%s %s (id=%?)", item_str, path_str, id)
}
Some(&node_foreign_item(item, abi, _, path)) => {
fmt!("foreign item %s with abi %? (id=%?)",
path_ident_to_str(path, item.ident, itr), abi, id)
}
Some(&node_method(m, _, path)) => {
fmt!("method %s in %s (id=%?)",
*itr.get(m.ident), path_to_str(*path, itr), id)
}
Some(&node_trait_method(ref tm, _, path)) => {
let m = ast_util::trait_method_to_ty_method(&**tm);
fmt!("method %s in %s (id=%?)",
*itr.get(m.ident), path_to_str(*path, itr), id)
}
Some(&node_variant(ref variant, _, path)) => {
fmt!("variant %s in %s (id=%?)",
*itr.get(variant.node.name), path_to_str(*path, itr), id)
}
Some(&node_expr(expr)) => {
fmt!("expr %s (id=%?)", pprust::expr_to_str(expr, itr), id)
}
Some(&node_callee_scope(expr)) => {
fmt!("callee_scope %s (id=%?)", pprust::expr_to_str(expr, itr), id)
}
Some(&node_stmt(stmt)) => {
fmt!("stmt %s (id=%?)",
pprust::stmt_to_str(stmt, itr), id)
}
Some(&node_arg) => {
fmt!("arg (id=%?)", id)
}
Some(&node_local(ident)) => {
fmt!("local (id=%?, name=%s)", id, *itr.get(ident))
}
Some(&node_block(_)) => {
fmt!("block")
}
Some(&node_struct_ctor(*)) => {
fmt!("struct_ctor")
}
}
}
pub fn node_item_query<Result>(items: map, id: node_id,
query: &fn(@item) -> Result,
error_msg: ~str) -> Result {
match items.find(&id) {
Some(&node_item(it, _)) => query(it),
_ => fail!(error_msg)
}
}
|