2012-03-06 05:52:13 -06:00
|
|
|
// Finds items that are externally reachable, to determine which items
|
|
|
|
// need to have their metadata (and possibly their AST) serialized.
|
|
|
|
// All items that can be referred to through an exported name are
|
|
|
|
// reachable, and when a reachable thing is inline or generic, it
|
|
|
|
// makes all other generics or inline functions that it references
|
|
|
|
// reachable as well.
|
|
|
|
|
|
|
|
import syntax::ast::*;
|
2012-05-22 01:34:34 -05:00
|
|
|
import syntax::{visit, ast_util, ast_map};
|
2012-03-06 05:52:13 -06:00
|
|
|
import syntax::ast_util::def_id_of_def;
|
2012-03-22 20:16:22 -05:00
|
|
|
import syntax::attr;
|
2012-06-12 18:25:09 -05:00
|
|
|
import syntax::print::pprust::expr_to_str;
|
2012-03-07 18:48:57 -06:00
|
|
|
import std::map::hashmap;
|
2012-06-12 18:25:09 -05:00
|
|
|
import driver::session::*;
|
2012-03-06 05:52:13 -06:00
|
|
|
|
|
|
|
export map, find_reachable;
|
|
|
|
|
2012-03-07 18:48:57 -06:00
|
|
|
type map = std::map::hashmap<node_id, ()>;
|
2012-03-06 05:52:13 -06:00
|
|
|
|
2012-03-20 06:28:46 -05:00
|
|
|
type ctx = {exp_map: resolve::exp_map,
|
|
|
|
tcx: ty::ctxt,
|
|
|
|
method_map: typeck::method_map,
|
2012-03-06 05:52:13 -06:00
|
|
|
rmap: map};
|
|
|
|
|
2012-03-20 06:28:46 -05:00
|
|
|
fn find_reachable(crate_mod: _mod, exp_map: resolve::exp_map,
|
|
|
|
tcx: ty::ctxt, method_map: typeck::method_map) -> map {
|
2012-03-14 14:07:23 -05:00
|
|
|
let rmap = std::map::int_hash();
|
2012-03-20 06:28:46 -05:00
|
|
|
let cx = {exp_map: exp_map, tcx: tcx, method_map: method_map, rmap: rmap};
|
2012-03-08 14:16:04 -06:00
|
|
|
traverse_public_mod(cx, crate_mod);
|
|
|
|
traverse_all_resources(cx, crate_mod);
|
2012-03-06 05:52:13 -06:00
|
|
|
rmap
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn traverse_exports(cx: ctx, vis: ~[@view_item]) -> bool {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut found_export = false;
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(vis) |vi| {
|
2012-03-06 05:52:13 -06:00
|
|
|
alt vi.node {
|
|
|
|
view_item_export(vps) {
|
|
|
|
found_export = true;
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(vps) |vp| {
|
2012-03-06 05:52:13 -06:00
|
|
|
alt vp.node {
|
|
|
|
view_path_simple(_, _, id) | view_path_glob(_, id) |
|
|
|
|
view_path_list(_, _, id) {
|
|
|
|
traverse_export(cx, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
found_export
|
|
|
|
}
|
|
|
|
|
|
|
|
fn traverse_export(cx: ctx, exp_id: node_id) {
|
2012-06-30 18:19:07 -05:00
|
|
|
do option::iter(cx.exp_map.find(exp_id)) |defs| {
|
|
|
|
for vec::each(defs) |def| { traverse_def_id(cx, def.id); }
|
2012-03-06 05:52:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn traverse_def_id(cx: ctx, did: def_id) {
|
|
|
|
if did.crate != local_crate { ret; }
|
2012-06-12 18:25:09 -05:00
|
|
|
let n = alt cx.tcx.items.find(did.node) {
|
|
|
|
none { ret; } // This can happen for self, for example
|
|
|
|
some(n) { n }
|
|
|
|
};
|
|
|
|
alt n {
|
2012-03-06 05:52:13 -06:00
|
|
|
ast_map::node_item(item, _) { traverse_public_item(cx, item); }
|
|
|
|
ast_map::node_method(_, impl_id, _) { traverse_def_id(cx, impl_id); }
|
2012-06-26 18:18:37 -05:00
|
|
|
ast_map::node_foreign_item(item, _, _) { cx.rmap.insert(item.id, ()); }
|
2012-03-06 05:52:13 -06:00
|
|
|
ast_map::node_variant(v, _, _) { cx.rmap.insert(v.node.id, ()); }
|
2012-06-24 17:09:57 -05:00
|
|
|
// If it's a ctor, consider the parent reachable
|
|
|
|
ast_map::node_ctor(_, _, _, parent_id, _) {
|
2012-06-05 23:17:16 -05:00
|
|
|
traverse_def_id(cx, parent_id);
|
|
|
|
}
|
2012-03-06 05:52:13 -06:00
|
|
|
_ {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn traverse_public_mod(cx: ctx, m: _mod) {
|
|
|
|
if !traverse_exports(cx, m.view_items) {
|
|
|
|
// No exports, so every local item is exported
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(m.items) |item| { traverse_public_item(cx, item); }
|
2012-05-22 12:54:12 -05:00
|
|
|
} else {
|
|
|
|
// Make impls always reachable.
|
|
|
|
for vec::each(m.items) |item| {
|
|
|
|
alt item.node {
|
|
|
|
item_impl(*) {
|
|
|
|
traverse_public_item(cx, item);
|
|
|
|
}
|
|
|
|
_ {
|
|
|
|
// Nothing to do.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-06 05:52:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn traverse_public_item(cx: ctx, item: @item) {
|
|
|
|
if cx.rmap.contains_key(item.id) { ret; }
|
|
|
|
cx.rmap.insert(item.id, ());
|
|
|
|
alt item.node {
|
|
|
|
item_mod(m) { traverse_public_mod(cx, m); }
|
2012-06-26 18:18:37 -05:00
|
|
|
item_foreign_mod(nm) {
|
2012-03-06 05:52:13 -06:00
|
|
|
if !traverse_exports(cx, nm.view_items) {
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(nm.items) |item| { cx.rmap.insert(item.id, ()); }
|
2012-03-06 05:52:13 -06:00
|
|
|
}
|
|
|
|
}
|
2012-04-09 17:55:02 -05:00
|
|
|
item_fn(_, tps, blk) {
|
2012-03-06 05:52:13 -06:00
|
|
|
if tps.len() > 0u ||
|
|
|
|
attr::find_inline_attr(item.attrs) != attr::ia_none {
|
|
|
|
traverse_inline_body(cx, blk);
|
|
|
|
}
|
|
|
|
}
|
2012-04-24 17:52:52 -05:00
|
|
|
item_impl(tps, _, _, _, ms) {
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(ms) |m| {
|
2012-03-06 05:52:13 -06:00
|
|
|
if tps.len() > 0u || m.tps.len() > 0u ||
|
|
|
|
attr::find_inline_attr(m.attrs) != attr::ia_none {
|
2012-05-29 16:39:22 -05:00
|
|
|
cx.rmap.insert(m.id, ());
|
2012-03-06 05:52:13 -06:00
|
|
|
traverse_inline_body(cx, m.body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-03 18:30:42 -05:00
|
|
|
item_class(tps, _traits, items, ctor, m_dtor, _) {
|
2012-03-20 07:19:33 -05:00
|
|
|
cx.rmap.insert(ctor.node.id, ());
|
2012-06-30 18:19:07 -05:00
|
|
|
do option::iter(m_dtor) |dtor| {
|
2012-05-14 16:13:32 -05:00
|
|
|
cx.rmap.insert(dtor.node.id, ());
|
2012-06-12 18:25:09 -05:00
|
|
|
// dtors don't have attrs
|
|
|
|
if tps.len() > 0u {
|
|
|
|
traverse_inline_body(cx, dtor.node.body);
|
|
|
|
}
|
2012-05-14 16:13:32 -05:00
|
|
|
}
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(items) |item| {
|
2012-03-28 20:50:33 -05:00
|
|
|
alt item.node {
|
2012-03-19 12:19:00 -05:00
|
|
|
class_method(m) {
|
|
|
|
cx.rmap.insert(m.id, ());
|
2012-03-20 07:19:33 -05:00
|
|
|
if tps.len() > 0u ||
|
2012-03-19 12:19:00 -05:00
|
|
|
attr::find_inline_attr(m.attrs) != attr::ia_none {
|
|
|
|
traverse_inline_body(cx, m.body);
|
2012-03-20 07:19:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-13 13:20:21 -05:00
|
|
|
item_ty(t, _, _) {
|
|
|
|
traverse_ty(t, cx, mk_ty_visitor());
|
|
|
|
}
|
|
|
|
item_const(*) |
|
2012-07-03 18:30:42 -05:00
|
|
|
item_enum(*) | item_trait(*) {}
|
2012-03-06 05:52:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-13 13:20:21 -05:00
|
|
|
fn mk_ty_visitor() -> visit::vt<ctx> {
|
|
|
|
visit::mk_vt(@{visit_ty: traverse_ty with *visit::default_visitor()})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn traverse_ty(ty: @ty, cx: ctx, v: visit::vt<ctx>) {
|
|
|
|
if cx.rmap.contains_key(ty.id) { ret; }
|
|
|
|
cx.rmap.insert(ty.id, ());
|
|
|
|
|
|
|
|
alt ty.node {
|
|
|
|
ty_path(p, p_id) {
|
|
|
|
alt cx.tcx.def_map.find(p_id) {
|
|
|
|
// Kind of a hack to check this here, but I'm not sure what else
|
|
|
|
// to do
|
|
|
|
some(def_prim_ty(_)) { /* do nothing */ }
|
|
|
|
some(d) { traverse_def_id(cx, def_id_of_def(d)); }
|
|
|
|
none { /* do nothing -- but should we fail here? */ }
|
|
|
|
}
|
2012-06-30 18:19:07 -05:00
|
|
|
for p.types.each |t| { v.visit_ty(t, cx, v); };
|
2012-06-13 13:20:21 -05:00
|
|
|
}
|
|
|
|
_ { visit::visit_ty(ty, cx, v); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-06 05:52:13 -06:00
|
|
|
fn traverse_inline_body(cx: ctx, body: blk) {
|
|
|
|
fn traverse_expr(e: @expr, cx: ctx, v: visit::vt<ctx>) {
|
|
|
|
alt e.node {
|
|
|
|
expr_path(_) {
|
2012-06-12 18:25:09 -05:00
|
|
|
alt cx.tcx.def_map.find(e.id) {
|
|
|
|
some(d) {
|
|
|
|
traverse_def_id(cx, def_id_of_def(d));
|
|
|
|
}
|
|
|
|
none { cx.tcx.sess.span_bug(e.span, #fmt("Unbound node \
|
|
|
|
id %? while traversing %s", e.id, expr_to_str(e))); }
|
|
|
|
}
|
2012-03-06 05:52:13 -06:00
|
|
|
}
|
|
|
|
expr_field(_, _, _) {
|
2012-03-20 06:28:46 -05:00
|
|
|
alt cx.method_map.find(e.id) {
|
2012-06-07 12:51:21 -05:00
|
|
|
some({origin: typeck::method_static(did), _}) {
|
|
|
|
traverse_def_id(cx, did);
|
|
|
|
}
|
2012-03-06 05:52:13 -06:00
|
|
|
_ {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ {}
|
|
|
|
}
|
|
|
|
visit::visit_expr(e, cx, v);
|
|
|
|
}
|
2012-05-29 16:39:22 -05:00
|
|
|
// Don't ignore nested items: for example if a generic fn contains a
|
|
|
|
// generic impl (as in deque::create), we need to monomorphize the
|
|
|
|
// impl as well
|
|
|
|
fn traverse_item(i: @item, cx: ctx, _v: visit::vt<ctx>) {
|
|
|
|
traverse_public_item(cx, i);
|
|
|
|
}
|
|
|
|
visit::visit_block(body, cx, visit::mk_vt(@{
|
2012-03-06 05:52:13 -06:00
|
|
|
visit_expr: traverse_expr,
|
|
|
|
visit_item: traverse_item
|
|
|
|
with *visit::default_visitor()
|
|
|
|
}));
|
|
|
|
}
|
2012-03-08 14:16:04 -06:00
|
|
|
|
|
|
|
fn traverse_all_resources(cx: ctx, crate_mod: _mod) {
|
|
|
|
visit::visit_mod(crate_mod, ast_util::dummy_sp(), 0, cx, visit::mk_vt(@{
|
2012-06-30 18:19:07 -05:00
|
|
|
visit_expr: |_e, _cx, _v| { },
|
|
|
|
visit_item: |i, cx, v| {
|
2012-03-08 14:16:04 -06:00
|
|
|
visit::visit_item(i, cx, v);
|
|
|
|
alt i.node {
|
2012-06-21 23:46:12 -05:00
|
|
|
item_class(_, _, _, _, some(_), _) {
|
2012-04-18 23:26:25 -05:00
|
|
|
traverse_public_item(cx, i);
|
|
|
|
}
|
2012-03-08 14:16:04 -06:00
|
|
|
_ {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
with *visit::default_visitor()
|
|
|
|
}));
|
|
|
|
}
|