|
| 1 | +// Finds items that are externally reachable, to determine which items |
| 2 | +// need to have their metadata (and possibly their AST) serialized. |
| 3 | +// All items that can be referred to through an exported name are |
| 4 | +// reachable, and when a reachable thing is inline or generic, it |
| 5 | +// makes all other generics or inline functions that it references |
| 6 | +// reachable as well. |
| 7 | + |
| 8 | +import middle::{resolve, ast_map, typeck}; |
| 9 | +import syntax::ast::*; |
| 10 | +import syntax::visit; |
| 11 | +import syntax::ast_util::def_id_of_def; |
| 12 | +import front::attr; |
| 13 | + |
| 14 | +export map, find_reachable; |
| 15 | + |
| 16 | +type map = std::map::map<node_id, ()>; |
| 17 | + |
| 18 | +type ctx = {ccx: middle::trans::common::crate_ctxt, |
| 19 | + rmap: map}; |
| 20 | + |
| 21 | +fn find_reachable(ccx: middle::trans::common::crate_ctxt, crate_mod: _mod) |
| 22 | + -> map { |
| 23 | + let rmap = std::map::new_int_hash(); |
| 24 | + traverse_public_mod({ccx: ccx, rmap: rmap}, crate_mod); |
| 25 | + rmap |
| 26 | +} |
| 27 | + |
| 28 | +fn traverse_exports(cx: ctx, vis: [@view_item]) -> bool { |
| 29 | + let found_export = false; |
| 30 | + for vi in vis { |
| 31 | + alt vi.node { |
| 32 | + view_item_export(vps) { |
| 33 | + found_export = true; |
| 34 | + for vp in vps { |
| 35 | + alt vp.node { |
| 36 | + view_path_simple(_, _, id) | view_path_glob(_, id) | |
| 37 | + view_path_list(_, _, id) { |
| 38 | + traverse_export(cx, id); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + _ {} |
| 44 | + } |
| 45 | + } |
| 46 | + found_export |
| 47 | +} |
| 48 | + |
| 49 | +fn traverse_export(cx: ctx, exp_id: node_id) { |
| 50 | + option::may(cx.ccx.exp_map.find(exp_id)) {|defs| |
| 51 | + for def in defs { traverse_def_id(cx, def.id); } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +fn traverse_def_id(cx: ctx, did: def_id) { |
| 56 | + if did.crate != local_crate { ret; } |
| 57 | + alt cx.ccx.tcx.items.get(did.node) { |
| 58 | + ast_map::node_item(item, _) { traverse_public_item(cx, item); } |
| 59 | + ast_map::node_method(_, impl_id, _) { traverse_def_id(cx, impl_id); } |
| 60 | + ast_map::node_native_item(item, _) { cx.rmap.insert(item.id, ()); } |
| 61 | + ast_map::node_variant(v, _, _) { cx.rmap.insert(v.node.id, ()); } |
| 62 | + _ {} |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +fn traverse_public_mod(cx: ctx, m: _mod) { |
| 67 | + if !traverse_exports(cx, m.view_items) { |
| 68 | + // No exports, so every local item is exported |
| 69 | + for item in m.items { traverse_public_item(cx, item); } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +fn traverse_public_item(cx: ctx, item: @item) { |
| 74 | + if cx.rmap.contains_key(item.id) { ret; } |
| 75 | + cx.rmap.insert(item.id, ()); |
| 76 | + alt item.node { |
| 77 | + item_mod(m) { traverse_public_mod(cx, m); } |
| 78 | + item_native_mod(nm) { |
| 79 | + if !traverse_exports(cx, nm.view_items) { |
| 80 | + for item in nm.items { cx.rmap.insert(item.id, ()); } |
| 81 | + } |
| 82 | + } |
| 83 | + item_res(_, tps, blk, _, _) | item_fn(_, tps, blk) { |
| 84 | + if tps.len() > 0u || |
| 85 | + attr::find_inline_attr(item.attrs) != attr::ia_none { |
| 86 | + traverse_inline_body(cx, blk); |
| 87 | + } |
| 88 | + } |
| 89 | + item_impl(tps, _, _, ms) { |
| 90 | + for m in ms { |
| 91 | + if tps.len() > 0u || m.tps.len() > 0u || |
| 92 | + attr::find_inline_attr(m.attrs) != attr::ia_none { |
| 93 | + traverse_inline_body(cx, m.body); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + item_class(_tps, _items, _) {} // FIXME handle these when stable |
| 98 | + item_const(_, _) | item_ty(_, _) | item_enum(_, _) | item_iface(_, _) {} |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +fn traverse_inline_body(cx: ctx, body: blk) { |
| 103 | + fn traverse_expr(e: @expr, cx: ctx, v: visit::vt<ctx>) { |
| 104 | + alt e.node { |
| 105 | + expr_path(_) { |
| 106 | + traverse_def_id(cx, def_id_of_def(cx.ccx.tcx.def_map.get(e.id))); |
| 107 | + } |
| 108 | + expr_field(_, _, _) { |
| 109 | + alt cx.ccx.maps.method_map.find(e.id) { |
| 110 | + some(typeck::method_static(did)) { traverse_def_id(cx, did); } |
| 111 | + _ {} |
| 112 | + } |
| 113 | + } |
| 114 | + _ {} |
| 115 | + } |
| 116 | + visit::visit_expr(e, cx, v); |
| 117 | + } |
| 118 | + // Ignore nested items |
| 119 | + fn traverse_item(_i: @item, _cx: ctx, _v: visit::vt<ctx>) {} |
| 120 | + visit::visit_block(body, cx, visit::mk_vt(@{ |
| 121 | + visit_expr: traverse_expr, |
| 122 | + visit_item: traverse_item |
| 123 | + with *visit::default_visitor() |
| 124 | + })); |
| 125 | +} |
0 commit comments