2012-01-09 09:24:53 -06:00
|
|
|
import std::map;
|
2012-03-07 18:48:57 -06:00
|
|
|
import std::map::hashmap;
|
2012-05-22 01:34:34 -05:00
|
|
|
import ast::*;
|
|
|
|
import print::pprust;
|
|
|
|
import ast_util::path_to_ident;
|
|
|
|
import ast_util::inlined_item_methods;
|
|
|
|
import diagnostic::span_handler;
|
2011-06-19 15:41:21 -05:00
|
|
|
|
2012-06-10 02:49:59 -05:00
|
|
|
enum path_elt { path_mod(ident), path_name(ident) }
|
2012-06-29 18:26:56 -05:00
|
|
|
type path = ~[path_elt];
|
2012-02-03 02:53:37 -06:00
|
|
|
|
2012-06-14 20:46:33 -05:00
|
|
|
/* FIXMEs that say "bad" are as per #2543 */
|
2012-02-10 08:01:32 -06:00
|
|
|
fn path_to_str_with_sep(p: path, sep: str) -> str {
|
2012-06-30 18:19:07 -05:00
|
|
|
let strs = do vec::map(p) |e| {
|
2012-02-10 08:01:32 -06:00
|
|
|
alt e {
|
2012-06-21 18:44:10 -05:00
|
|
|
path_mod(s) { /* FIXME (#2543) */ copy *s }
|
|
|
|
path_name(s) { /* FIXME (#2543) */ copy *s }
|
2012-02-10 08:01:32 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
str::connect(strs, sep)
|
|
|
|
}
|
|
|
|
|
2012-03-28 14:54:06 -05:00
|
|
|
fn path_ident_to_str(p: path, i: ident) -> str {
|
|
|
|
if vec::is_empty(p) {
|
2012-06-21 18:44:10 -05:00
|
|
|
/* FIXME (#2543) */ copy *i
|
2012-03-28 14:54:06 -05:00
|
|
|
} else {
|
2012-06-10 02:49:59 -05:00
|
|
|
#fmt["%s::%s", path_to_str(p), *i]
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-10 08:01:32 -06:00
|
|
|
fn path_to_str(p: path) -> str {
|
2012-02-11 19:14:29 -06:00
|
|
|
path_to_str_with_sep(p, "::")
|
2012-02-10 08:01:32 -06:00
|
|
|
}
|
|
|
|
|
2012-01-19 16:24:03 -06:00
|
|
|
enum ast_node {
|
2012-02-03 02:53:37 -06:00
|
|
|
node_item(@item, @path),
|
2012-06-26 18:18:37 -05:00
|
|
|
node_foreign_item(@foreign_item, foreign_abi, @path),
|
2012-03-02 15:14:10 -06:00
|
|
|
node_method(@method, def_id /* impl did */, @path /* path to the impl */),
|
2012-03-07 05:21:08 -06:00
|
|
|
node_variant(variant, @item, @path),
|
2012-01-19 19:56:05 -06:00
|
|
|
node_expr(@expr),
|
2012-03-06 06:57:07 -06:00
|
|
|
node_export(@view_path, @path),
|
2011-09-15 06:18:20 -05:00
|
|
|
// Locals are numbered, because the alias analysis needs to know in which
|
|
|
|
// order they are introduced.
|
2012-01-19 19:56:05 -06:00
|
|
|
node_arg(arg, uint),
|
|
|
|
node_local(uint),
|
2012-06-24 17:09:57 -05:00
|
|
|
// Constructor for a class
|
|
|
|
// def_id is parent id
|
2012-06-29 18:26:56 -05:00
|
|
|
node_ctor(ident, ~[ty_param], @class_ctor, def_id, @path),
|
2012-05-14 16:13:32 -05:00
|
|
|
// Destructor for a class
|
2012-06-29 18:26:56 -05:00
|
|
|
node_dtor(~[ty_param], @class_dtor, def_id, @path),
|
2012-03-15 20:46:18 -05:00
|
|
|
node_block(blk),
|
2011-06-19 15:41:21 -05:00
|
|
|
}
|
|
|
|
|
2012-03-07 18:48:57 -06:00
|
|
|
type map = std::map::hashmap<node_id, ast_node>;
|
2012-03-26 20:35:18 -05:00
|
|
|
type ctx = {map: map, mut path: path,
|
2012-05-22 00:57:43 -05:00
|
|
|
mut local_id: uint, diag: span_handler};
|
2012-02-03 02:53:37 -06:00
|
|
|
type vt = visit::vt<ctx>;
|
2011-06-19 15:41:21 -05:00
|
|
|
|
2012-06-10 02:49:59 -05:00
|
|
|
fn extend(cx: ctx, +elt: ident) -> @path {
|
2012-06-29 18:26:56 -05:00
|
|
|
@(vec::append(cx.path, ~[path_name(elt)]))
|
2012-03-06 06:57:07 -06:00
|
|
|
}
|
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
fn mk_ast_map_visitor() -> vt {
|
|
|
|
ret visit::mk_vt(@{
|
2012-02-03 02:53:37 -06:00
|
|
|
visit_item: map_item,
|
|
|
|
visit_expr: map_expr,
|
|
|
|
visit_fn: map_fn,
|
|
|
|
visit_local: map_local,
|
2012-03-06 06:57:07 -06:00
|
|
|
visit_arm: map_arm,
|
2012-03-15 21:05:38 -05:00
|
|
|
visit_view_item: map_view_item,
|
|
|
|
visit_block: map_block
|
2012-02-03 02:53:37 -06:00
|
|
|
with *visit::default_visitor()
|
2012-02-14 17:21:53 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-05-22 00:57:43 -05:00
|
|
|
fn map_crate(diag: span_handler, c: crate) -> map {
|
2012-03-14 14:07:23 -05:00
|
|
|
let cx = {map: std::map::int_hash(),
|
2012-06-29 18:26:56 -05:00
|
|
|
mut path: ~[],
|
2012-03-26 20:35:18 -05:00
|
|
|
mut local_id: 0u,
|
2012-05-22 00:57:43 -05:00
|
|
|
diag: diag};
|
2012-02-14 17:21:53 -06:00
|
|
|
visit::visit_crate(c, cx, mk_ast_map_visitor());
|
2011-09-15 06:18:20 -05:00
|
|
|
ret cx.map;
|
2011-06-19 15:41:21 -05:00
|
|
|
}
|
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
// Used for items loaded from external crate that are being inlined into this
|
2012-03-02 15:14:10 -06:00
|
|
|
// crate. The `path` should be the path to the item but should not include
|
|
|
|
// the item itself.
|
2012-05-22 00:57:43 -05:00
|
|
|
fn map_decoded_item(diag: span_handler,
|
2012-06-07 22:12:05 -05:00
|
|
|
map: map, +path: path, ii: inlined_item) {
|
2012-02-14 17:21:53 -06:00
|
|
|
// 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 = {map: map,
|
2012-06-21 18:44:10 -05:00
|
|
|
mut path: /* FIXME (#2543) */ copy path,
|
2012-03-26 20:35:18 -05:00
|
|
|
mut local_id: 0u,
|
2012-05-22 00:57:43 -05:00
|
|
|
diag: diag};
|
2012-02-14 17:21:53 -06:00
|
|
|
let v = mk_ast_map_visitor();
|
2012-03-02 15:14:10 -06:00
|
|
|
|
|
|
|
// 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:
|
|
|
|
alt ii {
|
2012-06-12 18:25:09 -05:00
|
|
|
ii_item(*) | ii_ctor(*) | ii_dtor(*) { /* fallthrough */ }
|
2012-06-26 18:18:37 -05:00
|
|
|
ii_foreign(i) {
|
|
|
|
cx.map.insert(i.id, node_foreign_item(i, foreign_abi_rust_intrinsic,
|
2012-03-21 09:42:20 -05:00
|
|
|
@path));
|
|
|
|
}
|
2012-03-02 15:14:10 -06:00
|
|
|
ii_method(impl_did, m) {
|
2012-03-07 05:21:08 -06:00
|
|
|
map_method(impl_did, @path, m, cx);
|
2012-03-02 15:14:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// visit the item / method contents and add those to the map:
|
2012-03-01 21:37:52 -06:00
|
|
|
ii.accept(cx, v);
|
2012-03-01 12:36:22 -06:00
|
|
|
}
|
|
|
|
|
2012-02-03 02:53:37 -06:00
|
|
|
fn map_fn(fk: visit::fn_kind, decl: fn_decl, body: blk,
|
|
|
|
sp: codemap::span, id: node_id, cx: ctx, v: vt) {
|
2012-06-30 18:19:07 -05:00
|
|
|
for decl.inputs.each |a| {
|
2012-06-21 18:44:10 -05:00
|
|
|
cx.map.insert(a.id,
|
|
|
|
node_arg(/* FIXME (#2543) */
|
|
|
|
copy a, cx.local_id));
|
2011-09-15 06:18:20 -05:00
|
|
|
cx.local_id += 1u;
|
|
|
|
}
|
2012-04-10 12:52:06 -05:00
|
|
|
alt fk {
|
|
|
|
visit::fk_ctor(nm, tps, self_id, parent_id) {
|
2012-06-07 22:12:05 -05:00
|
|
|
let ct = @{node: {id: id,
|
|
|
|
self_id: self_id,
|
2012-06-21 18:44:10 -05:00
|
|
|
dec: /* FIXME (#2543) */ copy decl,
|
|
|
|
body: /* FIXME (#2543) */ copy body},
|
2012-04-10 12:52:06 -05:00
|
|
|
span: sp};
|
2012-06-21 18:44:10 -05:00
|
|
|
cx.map.insert(id, node_ctor(/* FIXME (#2543) */ copy nm,
|
|
|
|
/* FIXME (#2543) */ copy tps,
|
2012-06-24 17:09:57 -05:00
|
|
|
ct, parent_id,
|
2012-06-21 18:44:10 -05:00
|
|
|
@/* FIXME (#2543) */ copy cx.path));
|
2012-04-10 12:52:06 -05:00
|
|
|
}
|
2012-05-14 16:13:32 -05:00
|
|
|
visit::fk_dtor(tps, self_id, parent_id) {
|
2012-06-07 23:53:47 -05:00
|
|
|
let dt = @{node: {id: id, self_id: self_id,
|
2012-06-21 18:44:10 -05:00
|
|
|
body: /* FIXME (#2543) */ copy body}, span: sp};
|
|
|
|
cx.map.insert(id, node_dtor(/* FIXME (#2543) */ copy tps, dt,
|
2012-06-07 23:53:47 -05:00
|
|
|
parent_id,
|
2012-06-21 18:44:10 -05:00
|
|
|
@/* FIXME (#2543) */ copy cx.path));
|
2012-05-14 16:13:32 -05:00
|
|
|
}
|
|
|
|
|
2012-04-10 12:52:06 -05:00
|
|
|
_ {}
|
|
|
|
}
|
2012-02-03 02:53:37 -06:00
|
|
|
visit::visit_fn(fk, decl, body, sp, id, cx, v);
|
2011-09-15 06:18:20 -05:00
|
|
|
}
|
|
|
|
|
2012-03-15 20:46:18 -05:00
|
|
|
fn map_block(b: blk, cx: ctx, v: vt) {
|
2012-06-21 18:44:10 -05:00
|
|
|
cx.map.insert(b.node.id, node_block(/* FIXME (#2543) */ copy b));
|
2012-03-15 20:46:18 -05:00
|
|
|
visit::visit_block(b, cx, v);
|
|
|
|
}
|
|
|
|
|
2012-02-22 09:57:23 -06:00
|
|
|
fn number_pat(cx: ctx, pat: @pat) {
|
2012-06-30 18:19:07 -05:00
|
|
|
do ast_util::walk_pat(pat) |p| {
|
2012-02-22 09:57:23 -06:00
|
|
|
alt p.node {
|
|
|
|
pat_ident(_, _) {
|
|
|
|
cx.map.insert(p.id, node_local(cx.local_id));
|
|
|
|
cx.local_id += 1u;
|
|
|
|
}
|
|
|
|
_ {}
|
|
|
|
}
|
2011-10-21 05:41:42 -05:00
|
|
|
};
|
2012-02-22 09:57:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn map_local(loc: @local, cx: ctx, v: vt) {
|
|
|
|
number_pat(cx, loc.node.pat);
|
2012-02-03 02:53:37 -06:00
|
|
|
visit::visit_local(loc, cx, v);
|
2011-09-15 06:18:20 -05:00
|
|
|
}
|
|
|
|
|
2012-02-03 02:53:37 -06:00
|
|
|
fn map_arm(arm: arm, cx: ctx, v: vt) {
|
2012-02-22 09:57:23 -06:00
|
|
|
number_pat(cx, arm.pats[0]);
|
2012-02-03 02:53:37 -06:00
|
|
|
visit::visit_arm(arm, cx, v);
|
2011-09-14 08:30:59 -05:00
|
|
|
}
|
|
|
|
|
2012-03-02 15:14:10 -06:00
|
|
|
fn map_method(impl_did: def_id, impl_path: @path,
|
|
|
|
m: @method, cx: ctx) {
|
|
|
|
cx.map.insert(m.id, node_method(m, impl_did, impl_path));
|
2012-03-07 05:54:00 -06:00
|
|
|
cx.map.insert(m.self_id, node_local(cx.local_id));
|
|
|
|
cx.local_id += 1u;
|
2012-03-02 15:14:10 -06:00
|
|
|
}
|
|
|
|
|
2012-02-03 02:53:37 -06:00
|
|
|
fn map_item(i: @item, cx: ctx, v: vt) {
|
2012-06-21 18:44:10 -05:00
|
|
|
let item_path = @/* FIXME (#2543) */ copy cx.path;
|
2012-03-02 15:14:10 -06:00
|
|
|
cx.map.insert(i.id, node_item(i, item_path));
|
2011-07-27 07:19:39 -05:00
|
|
|
alt i.node {
|
2012-06-26 18:25:52 -05:00
|
|
|
item_impl(_, opt_ir, _, ms) {
|
2012-03-02 15:14:10 -06:00
|
|
|
let impl_did = ast_util::local_def(i.id);
|
2012-06-30 18:19:07 -05:00
|
|
|
for ms.each |m| {
|
2012-06-10 02:49:59 -05:00
|
|
|
map_method(impl_did, extend(cx, i.ident), m,
|
2012-06-07 23:53:47 -05:00
|
|
|
cx);
|
2012-03-02 15:14:10 -06:00
|
|
|
}
|
2011-12-19 00:36:37 -06:00
|
|
|
}
|
2012-07-11 12:28:30 -05:00
|
|
|
item_enum(vs, _) {
|
2012-06-30 18:19:07 -05:00
|
|
|
for vs.each |v| {
|
2012-02-08 03:05:44 -06:00
|
|
|
cx.map.insert(v.node.id, node_variant(
|
2012-06-21 18:44:10 -05:00
|
|
|
/* FIXME (#2543) */ copy v, i,
|
2012-06-10 02:49:59 -05:00
|
|
|
extend(cx, i.ident)));
|
2012-02-08 03:05:44 -06:00
|
|
|
}
|
|
|
|
}
|
2012-06-26 18:18:37 -05:00
|
|
|
item_foreign_mod(nm) {
|
|
|
|
let abi = alt attr::foreign_abi(i.attrs) {
|
2012-05-22 00:57:43 -05:00
|
|
|
either::left(msg) { cx.diag.span_fatal(i.span, msg); }
|
2012-03-07 05:21:08 -06:00
|
|
|
either::right(abi) { abi }
|
|
|
|
};
|
2012-06-30 18:19:07 -05:00
|
|
|
for nm.items.each |nitem| {
|
2012-06-07 23:53:47 -05:00
|
|
|
cx.map.insert(nitem.id,
|
2012-06-26 18:18:37 -05:00
|
|
|
node_foreign_item(nitem, abi,
|
2012-06-21 18:44:10 -05:00
|
|
|
/* FIXME (#2543) */
|
2012-07-03 20:25:50 -05:00
|
|
|
extend(cx, i.ident)));
|
2012-03-07 05:21:08 -06:00
|
|
|
}
|
|
|
|
}
|
2012-07-11 12:28:30 -05:00
|
|
|
item_class(tps, traits, items, ctor, dtor) {
|
2012-04-13 14:22:35 -05:00
|
|
|
let (_, ms) = ast_util::split_class_items(items);
|
2012-07-03 18:30:42 -05:00
|
|
|
// Map trait refs to their parent classes. This is
|
2012-04-13 14:22:35 -05:00
|
|
|
// so we can find the self_ty
|
2012-06-26 18:25:52 -05:00
|
|
|
do vec::iter(traits) |p| { cx.map.insert(p.ref_id,
|
|
|
|
node_item(i, item_path));
|
|
|
|
// This is so we can look up the right things when
|
|
|
|
// encoding/decoding
|
|
|
|
cx.map.insert(p.impl_id,
|
|
|
|
node_item(i, item_path));
|
|
|
|
|
|
|
|
};
|
2012-03-19 12:19:00 -05:00
|
|
|
let d_id = ast_util::local_def(i.id);
|
2012-06-10 02:49:59 -05:00
|
|
|
let p = extend(cx, i.ident);
|
2012-03-19 12:19:00 -05:00
|
|
|
// only need to handle methods
|
2012-06-30 18:19:07 -05:00
|
|
|
do vec::iter(ms) |m| { map_method(d_id, p, m, cx); }
|
2012-03-07 05:21:08 -06:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
_ { }
|
2011-06-19 15:41:21 -05:00
|
|
|
}
|
2012-02-03 02:53:37 -06:00
|
|
|
alt i.node {
|
2012-06-26 18:18:37 -05:00
|
|
|
item_mod(_) | item_foreign_mod(_) {
|
2012-06-26 02:39:18 -05:00
|
|
|
vec::push(cx.path, path_mod(i.ident));
|
2012-03-06 06:57:07 -06:00
|
|
|
}
|
2012-06-26 02:39:18 -05:00
|
|
|
_ { vec::push(cx.path, path_name(i.ident)); }
|
2012-02-03 02:53:37 -06:00
|
|
|
}
|
|
|
|
visit::visit_item(i, cx, v);
|
|
|
|
vec::pop(cx.path);
|
2011-06-19 15:41:21 -05:00
|
|
|
}
|
|
|
|
|
2012-03-06 06:57:07 -06:00
|
|
|
fn map_view_item(vi: @view_item, cx: ctx, _v: vt) {
|
|
|
|
alt vi.node {
|
|
|
|
view_item_export(vps) {
|
2012-06-30 18:19:07 -05:00
|
|
|
for vps.each |vp| {
|
2012-03-06 06:57:07 -06:00
|
|
|
let (id, name) = alt vp.node {
|
2012-06-21 18:44:10 -05:00
|
|
|
view_path_simple(nm, _, id) {
|
|
|
|
(id, /* FIXME (#2543) */ copy nm)
|
|
|
|
}
|
2012-03-06 06:57:07 -06:00
|
|
|
view_path_glob(pth, id) | view_path_list(pth, _, id) {
|
2012-04-13 03:46:56 -05:00
|
|
|
(id, path_to_ident(pth))
|
2012-03-06 06:57:07 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
cx.map.insert(id, node_export(vp, extend(cx, name)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-03 02:53:37 -06:00
|
|
|
fn map_expr(ex: @expr, cx: ctx, v: vt) {
|
2011-09-15 06:18:20 -05:00
|
|
|
cx.map.insert(ex.id, node_expr(ex));
|
2012-02-03 02:53:37 -06:00
|
|
|
visit::visit_expr(ex, cx, v);
|
2011-06-19 15:41:21 -05:00
|
|
|
}
|
|
|
|
|
2012-03-28 14:54:06 -05:00
|
|
|
fn node_id_to_str(map: map, id: node_id) -> str {
|
|
|
|
alt map.find(id) {
|
|
|
|
none {
|
|
|
|
#fmt["unknown node (id=%d)", id]
|
|
|
|
}
|
|
|
|
some(node_item(item, path)) {
|
|
|
|
#fmt["item %s (id=%?)", path_ident_to_str(*path, item.ident), id]
|
|
|
|
}
|
2012-06-26 18:18:37 -05:00
|
|
|
some(node_foreign_item(item, abi, path)) {
|
2012-07-03 18:11:00 -05:00
|
|
|
#fmt["foreign item %s with abi %? (id=%?)",
|
2012-03-28 14:54:06 -05:00
|
|
|
path_ident_to_str(*path, item.ident), abi, id]
|
|
|
|
}
|
|
|
|
some(node_method(m, impl_did, path)) {
|
|
|
|
#fmt["method %s in %s (id=%?)",
|
2012-06-10 02:49:59 -05:00
|
|
|
*m.ident, path_to_str(*path), id]
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
|
|
|
some(node_variant(variant, def_id, path)) {
|
|
|
|
#fmt["variant %s in %s (id=%?)",
|
2012-06-10 02:49:59 -05:00
|
|
|
*variant.node.name, path_to_str(*path), id]
|
2012-03-28 14:54:06 -05:00
|
|
|
}
|
|
|
|
some(node_expr(expr)) {
|
|
|
|
#fmt["expr %s (id=%?)",
|
|
|
|
pprust::expr_to_str(expr), id]
|
|
|
|
}
|
2012-06-14 20:46:33 -05:00
|
|
|
// FIXMEs are as per #2410
|
2012-03-28 14:54:06 -05:00
|
|
|
some(node_export(_, path)) {
|
2012-06-21 18:44:10 -05:00
|
|
|
#fmt["export %s (id=%?)", // add more info here
|
2012-03-28 14:54:06 -05:00
|
|
|
path_to_str(*path), id]
|
|
|
|
}
|
2012-06-21 18:44:10 -05:00
|
|
|
some(node_arg(_, _)) { // add more info here
|
2012-03-28 14:54:06 -05:00
|
|
|
#fmt["arg (id=%?)", id]
|
|
|
|
}
|
2012-06-21 18:44:10 -05:00
|
|
|
some(node_local(_)) { // add more info here
|
2012-03-28 14:54:06 -05:00
|
|
|
#fmt["local (id=%?)", id]
|
|
|
|
}
|
2012-06-21 18:44:10 -05:00
|
|
|
some(node_ctor(*)) { // add more info here
|
2012-03-28 14:54:06 -05:00
|
|
|
#fmt["node_ctor (id=%?)", id]
|
|
|
|
}
|
2012-06-21 18:44:10 -05:00
|
|
|
some(node_dtor(*)) { // add more info here
|
2012-05-14 16:13:32 -05:00
|
|
|
#fmt["node_dtor (id=%?)", id]
|
|
|
|
}
|
2012-03-28 14:54:06 -05:00
|
|
|
some(node_block(_)) {
|
|
|
|
#fmt["block"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-19 15:41:21 -05:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|