2011-12-13 16:25:51 -08:00
|
|
|
import option;
|
2012-01-09 16:24:53 +01:00
|
|
|
import std::map;
|
2011-07-05 11:48:19 +02:00
|
|
|
import syntax::ast::*;
|
2011-09-15 13:18:20 +02:00
|
|
|
import syntax::ast_util;
|
2011-09-12 16:13:28 -07:00
|
|
|
import syntax::{visit, codemap};
|
2011-06-19 22:41:21 +02:00
|
|
|
|
2012-01-19 14:24:03 -08:00
|
|
|
enum ast_node {
|
2012-01-19 17:56:05 -08:00
|
|
|
node_item(@item),
|
|
|
|
node_native_item(@native_item),
|
|
|
|
node_method(@method),
|
|
|
|
node_expr(@expr),
|
2011-09-15 13:18:20 +02:00
|
|
|
// Locals are numbered, because the alias analysis needs to know in which
|
|
|
|
// order they are introduced.
|
2012-01-19 17:56:05 -08:00
|
|
|
node_arg(arg, uint),
|
|
|
|
node_local(uint),
|
|
|
|
node_res_ctor(@item),
|
2011-06-19 22:41:21 +02:00
|
|
|
}
|
|
|
|
|
2012-01-09 16:24:53 +01:00
|
|
|
type map = std::map::map<node_id, ast_node>;
|
2011-09-15 13:18:20 +02:00
|
|
|
type ctx = @{map: map, mutable local_id: uint};
|
2011-06-19 22:41:21 +02:00
|
|
|
|
2011-09-12 11:27:30 +02:00
|
|
|
fn map_crate(c: crate) -> map {
|
2012-01-09 16:24:53 +01:00
|
|
|
let cx = @{map: std::map::new_int_hash(),
|
2011-09-15 13:18:20 +02:00
|
|
|
mutable local_id: 0u};
|
2011-06-19 22:41:21 +02:00
|
|
|
|
2011-09-14 15:30:59 +02:00
|
|
|
let v_map = visit::mk_simple_visitor
|
2011-09-15 13:18:20 +02:00
|
|
|
(@{visit_item: bind map_item(cx, _),
|
|
|
|
visit_native_item: bind map_native_item(cx, _),
|
|
|
|
visit_expr: bind map_expr(cx, _),
|
2011-12-29 20:07:55 -08:00
|
|
|
visit_fn: bind map_fn(cx, _, _, _, _, _),
|
2011-09-15 13:18:20 +02:00
|
|
|
visit_local: bind map_local(cx, _),
|
|
|
|
visit_arm: bind map_arm(cx, _)
|
|
|
|
with *visit::default_simple_visitor()});
|
2011-09-14 15:30:59 +02:00
|
|
|
visit::visit_crate(c, (), v_map);
|
2011-09-15 13:18:20 +02:00
|
|
|
ret cx.map;
|
2011-06-19 22:41:21 +02:00
|
|
|
}
|
|
|
|
|
2011-12-29 20:07:55 -08:00
|
|
|
fn map_fn(cx: ctx, _fk: visit::fn_kind, decl: fn_decl, _body: blk,
|
|
|
|
_sp: codemap::span, _id: node_id) {
|
2011-12-20 11:03:21 -08:00
|
|
|
for a in decl.inputs {
|
2011-09-15 13:18:20 +02:00
|
|
|
cx.map.insert(a.id, node_arg(a, cx.local_id));
|
|
|
|
cx.local_id += 1u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn map_local(cx: ctx, loc: @local) {
|
2012-01-14 16:05:07 -08:00
|
|
|
pat_util::pat_bindings(loc.node.pat) {|p|
|
2011-09-15 13:18:20 +02:00
|
|
|
cx.map.insert(p.id, node_local(cx.local_id));
|
|
|
|
cx.local_id += 1u;
|
2011-10-21 12:41:42 +02:00
|
|
|
};
|
2011-09-15 13:18:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn map_arm(cx: ctx, arm: arm) {
|
2012-01-14 16:05:07 -08:00
|
|
|
pat_util::pat_bindings(arm.pats[0]) {|p|
|
2011-09-15 13:18:20 +02:00
|
|
|
cx.map.insert(p.id, node_local(cx.local_id));
|
|
|
|
cx.local_id += 1u;
|
2011-10-21 12:41:42 +02:00
|
|
|
};
|
2011-09-14 15:30:59 +02:00
|
|
|
}
|
|
|
|
|
2011-09-15 13:18:20 +02:00
|
|
|
fn map_item(cx: ctx, i: @item) {
|
|
|
|
cx.map.insert(i.id, node_item(i));
|
2011-07-27 14:19:39 +02:00
|
|
|
alt i.node {
|
2011-12-20 16:33:55 +01:00
|
|
|
item_impl(_, _, _, ms) {
|
2011-12-22 17:49:54 +01:00
|
|
|
for m in ms { cx.map.insert(m.id, node_method(m)); }
|
2011-12-19 01:36:37 -05:00
|
|
|
}
|
2011-12-22 17:49:54 +01:00
|
|
|
item_res(_, _, _, dtor_id, ctor_id) {
|
2011-12-18 23:32:38 -05:00
|
|
|
cx.map.insert(ctor_id, node_res_ctor(i));
|
|
|
|
cx.map.insert(dtor_id, node_item(i));
|
2011-12-16 14:41:12 +01:00
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
_ { }
|
2011-06-19 22:41:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-15 13:18:20 +02:00
|
|
|
fn map_native_item(cx: ctx, i: @native_item) {
|
|
|
|
cx.map.insert(i.id, node_native_item(i));
|
2011-06-19 22:41:21 +02:00
|
|
|
}
|
|
|
|
|
2011-09-15 13:18:20 +02:00
|
|
|
fn map_expr(cx: ctx, ex: @expr) {
|
|
|
|
cx.map.insert(ex.id, node_expr(ex));
|
2011-06-19 22:41:21 +02:00
|
|
|
}
|
|
|
|
|
2011-09-12 11:27:30 +02:00
|
|
|
fn node_span(node: ast_node) -> codemap::span {
|
2011-08-10 21:43:36 -07:00
|
|
|
alt node {
|
|
|
|
node_item(item) { item.span }
|
|
|
|
node_native_item(nitem) { nitem.span }
|
|
|
|
node_expr(expr) { expr.span }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2011-08-21 21:44:41 -07:00
|
|
|
import syntax::ast_util;
|
|
|
|
|
2011-11-14 19:37:35 -08:00
|
|
|
#[test]
|
2011-08-10 21:43:36 -07:00
|
|
|
fn test_node_span_item() {
|
2011-08-21 21:44:41 -07:00
|
|
|
let expected: codemap::span = ast_util::mk_sp(20u, 30u);
|
2011-08-19 15:16:48 -07:00
|
|
|
let node =
|
2011-09-02 15:34:58 -07:00
|
|
|
node_item(@{ident: "test",
|
2011-08-19 15:16:48 -07:00
|
|
|
attrs: [],
|
|
|
|
id: 0,
|
|
|
|
node: item_mod({view_items: [], items: []}),
|
|
|
|
span: expected});
|
|
|
|
assert (node_span(node) == expected);
|
2011-08-10 21:43:36 -07:00
|
|
|
}
|
|
|
|
|
2011-11-14 19:37:35 -08:00
|
|
|
#[test]
|
2011-08-10 21:43:36 -07:00
|
|
|
fn test_node_span_native_item() {
|
2011-08-21 21:44:41 -07:00
|
|
|
let expected: codemap::span = ast_util::mk_sp(20u, 30u);
|
2011-08-19 15:16:48 -07:00
|
|
|
let node =
|
2011-09-02 15:34:58 -07:00
|
|
|
node_native_item(@{ident: "test",
|
2011-08-19 15:16:48 -07:00
|
|
|
attrs: [],
|
|
|
|
node: native_item_ty,
|
|
|
|
id: 0,
|
|
|
|
span: expected});
|
|
|
|
assert (node_span(node) == expected);
|
2011-08-10 21:43:36 -07:00
|
|
|
}
|
|
|
|
|
2011-11-14 19:37:35 -08:00
|
|
|
#[test]
|
2011-08-10 21:43:36 -07:00
|
|
|
fn test_node_span_expr() {
|
2011-08-21 21:44:41 -07:00
|
|
|
let expected: codemap::span = ast_util::mk_sp(20u, 30u);
|
2011-08-19 15:16:48 -07:00
|
|
|
let node = node_expr(@{id: 0, node: expr_break, span: expected});
|
|
|
|
assert (node_span(node) == expected);
|
2011-08-10 21:43:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-19 22:41:21 +02:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|