2012-01-09 15:24:53 +00:00
|
|
|
import std::map;
|
2011-07-05 09:48:19 +00:00
|
|
|
import syntax::ast::*;
|
2011-09-15 11:18:20 +00:00
|
|
|
import syntax::ast_util;
|
2011-09-12 23:13:28 +00:00
|
|
|
import syntax::{visit, codemap};
|
2011-06-19 20:41:21 +00:00
|
|
|
|
2012-02-03 08:53:37 +00:00
|
|
|
enum path_elt { path_mod(str), path_name(str) }
|
|
|
|
type path = [path_elt];
|
|
|
|
|
2012-02-10 14:01:32 +00:00
|
|
|
fn path_to_str_with_sep(p: path, sep: str) -> str {
|
|
|
|
let strs = vec::map(p) {|e|
|
|
|
|
alt e {
|
|
|
|
path_mod(s) { s }
|
|
|
|
path_name(s) { s }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
str::connect(strs, sep)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path_to_str(p: path) -> str {
|
2012-02-12 01:14:29 +00:00
|
|
|
path_to_str_with_sep(p, "::")
|
2012-02-10 14:01:32 +00:00
|
|
|
}
|
|
|
|
|
2012-01-19 22:24:03 +00:00
|
|
|
enum ast_node {
|
2012-02-03 08:53:37 +00:00
|
|
|
node_item(@item, @path),
|
|
|
|
node_native_item(@native_item, @path),
|
2012-02-09 10:17:11 +00:00
|
|
|
node_method(@method, node_id, @path),
|
2012-02-08 09:05:44 +00:00
|
|
|
node_variant(variant, def_id, @path),
|
2012-01-20 01:56:05 +00:00
|
|
|
node_expr(@expr),
|
2011-09-15 11:18:20 +00:00
|
|
|
// Locals are numbered, because the alias analysis needs to know in which
|
|
|
|
// order they are introduced.
|
2012-01-20 01:56:05 +00:00
|
|
|
node_arg(arg, uint),
|
|
|
|
node_local(uint),
|
|
|
|
node_res_ctor(@item),
|
2011-06-19 20:41:21 +00:00
|
|
|
}
|
|
|
|
|
2012-01-09 15:24:53 +00:00
|
|
|
type map = std::map::map<node_id, ast_node>;
|
2012-02-03 08:53:37 +00:00
|
|
|
type ctx = {map: map, mutable path: path, mutable local_id: uint};
|
|
|
|
type vt = visit::vt<ctx>;
|
2011-06-19 20:41:21 +00:00
|
|
|
|
2011-09-12 09:27:30 +00:00
|
|
|
fn map_crate(c: crate) -> map {
|
2012-02-03 08:53:37 +00:00
|
|
|
let cx = {map: std::map::new_int_hash(),
|
|
|
|
mutable path: [],
|
|
|
|
mutable local_id: 0u};
|
|
|
|
visit::visit_crate(c, cx, visit::mk_vt(@{
|
|
|
|
visit_item: map_item,
|
|
|
|
visit_native_item: map_native_item,
|
|
|
|
visit_expr: map_expr,
|
|
|
|
visit_fn: map_fn,
|
|
|
|
visit_local: map_local,
|
|
|
|
visit_arm: map_arm
|
|
|
|
with *visit::default_visitor()
|
|
|
|
}));
|
2011-09-15 11:18:20 +00:00
|
|
|
ret cx.map;
|
2011-06-19 20:41:21 +00:00
|
|
|
}
|
|
|
|
|
2012-02-03 08:53:37 +00:00
|
|
|
fn map_fn(fk: visit::fn_kind, decl: fn_decl, body: blk,
|
|
|
|
sp: codemap::span, id: node_id, cx: ctx, v: vt) {
|
2011-12-20 19:03:21 +00:00
|
|
|
for a in decl.inputs {
|
2011-09-15 11:18:20 +00:00
|
|
|
cx.map.insert(a.id, node_arg(a, cx.local_id));
|
|
|
|
cx.local_id += 1u;
|
|
|
|
}
|
2012-02-03 08:53:37 +00:00
|
|
|
visit::visit_fn(fk, decl, body, sp, id, cx, v);
|
2011-09-15 11:18:20 +00:00
|
|
|
}
|
|
|
|
|
2012-02-03 08:53:37 +00:00
|
|
|
fn map_local(loc: @local, cx: ctx, v: vt) {
|
2012-01-31 05:00:57 +00:00
|
|
|
pat_util::pat_bindings(loc.node.pat) {|p_id, _s, _p|
|
|
|
|
cx.map.insert(p_id, node_local(cx.local_id));
|
2011-09-15 11:18:20 +00:00
|
|
|
cx.local_id += 1u;
|
2011-10-21 10:41:42 +00:00
|
|
|
};
|
2012-02-03 08:53:37 +00:00
|
|
|
visit::visit_local(loc, cx, v);
|
2011-09-15 11:18:20 +00:00
|
|
|
}
|
|
|
|
|
2012-02-03 08:53:37 +00:00
|
|
|
fn map_arm(arm: arm, cx: ctx, v: vt) {
|
2012-01-31 05:00:57 +00:00
|
|
|
pat_util::pat_bindings(arm.pats[0]) {|p_id, _s, _p|
|
|
|
|
cx.map.insert(p_id, node_local(cx.local_id));
|
2011-09-15 11:18:20 +00:00
|
|
|
cx.local_id += 1u;
|
2011-10-21 10:41:42 +00:00
|
|
|
};
|
2012-02-03 08:53:37 +00:00
|
|
|
visit::visit_arm(arm, cx, v);
|
2011-09-14 13:30:59 +00:00
|
|
|
}
|
|
|
|
|
2012-02-03 08:53:37 +00:00
|
|
|
fn map_item(i: @item, cx: ctx, v: vt) {
|
|
|
|
cx.map.insert(i.id, node_item(i, @cx.path));
|
2011-07-27 12:19:39 +00:00
|
|
|
alt i.node {
|
2011-12-20 15:33:55 +00:00
|
|
|
item_impl(_, _, _, ms) {
|
2012-02-09 10:17:11 +00:00
|
|
|
for m in ms { cx.map.insert(m.id, node_method(m, i.id, @cx.path)); }
|
2011-12-19 06:36:37 +00:00
|
|
|
}
|
2011-12-22 16:49:54 +00:00
|
|
|
item_res(_, _, _, dtor_id, ctor_id) {
|
2011-12-19 04:32:38 +00:00
|
|
|
cx.map.insert(ctor_id, node_res_ctor(i));
|
2012-02-03 08:53:37 +00:00
|
|
|
cx.map.insert(dtor_id, node_item(i, @cx.path));
|
2011-12-16 13:41:12 +00:00
|
|
|
}
|
2012-02-08 09:05:44 +00:00
|
|
|
item_enum(vs, _) {
|
|
|
|
for v in vs {
|
|
|
|
cx.map.insert(v.node.id, node_variant(
|
|
|
|
v, ast_util::local_def(i.id),
|
|
|
|
@(cx.path + [path_name(i.ident)])));
|
|
|
|
}
|
|
|
|
}
|
2011-07-27 12:19:39 +00:00
|
|
|
_ { }
|
2011-06-19 20:41:21 +00:00
|
|
|
}
|
2012-02-03 08:53:37 +00:00
|
|
|
alt i.node {
|
|
|
|
item_mod(_) | item_native_mod(_) { cx.path += [path_mod(i.ident)]; }
|
|
|
|
_ { cx.path += [path_name(i.ident)]; }
|
|
|
|
}
|
|
|
|
visit::visit_item(i, cx, v);
|
|
|
|
vec::pop(cx.path);
|
2011-06-19 20:41:21 +00:00
|
|
|
}
|
|
|
|
|
2012-02-03 08:53:37 +00:00
|
|
|
fn map_native_item(i: @native_item, cx: ctx, v: vt) {
|
|
|
|
cx.map.insert(i.id, node_native_item(i, @cx.path));
|
|
|
|
visit::visit_native_item(i, cx, v);
|
2011-06-19 20:41:21 +00:00
|
|
|
}
|
|
|
|
|
2012-02-03 08:53:37 +00:00
|
|
|
fn map_expr(ex: @expr, cx: ctx, v: vt) {
|
2011-09-15 11:18:20 +00:00
|
|
|
cx.map.insert(ex.id, node_expr(ex));
|
2012-02-03 08:53:37 +00:00
|
|
|
visit::visit_expr(ex, cx, v);
|
2011-06-19 20:41:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|