Move crate_map from resolve to cstore

This commit is contained in:
Brian Anderson 2011-07-07 21:37:56 -07:00
parent 513b2276c2
commit 85535fc3e0
3 changed files with 66 additions and 58 deletions

View File

@ -30,11 +30,10 @@ export list_file_metadata;
// Traverses an AST, reading all the information about use'd crates and native
// libraries necessary for later resolving, typechecking, linking, etc.
fn read_crates(session::session sess, resolve::crate_map crate_map,
fn read_crates(session::session sess,
&ast::crate crate) {
auto e =
@rec(sess=sess,
crate_map=crate_map,
crate_cache=@std::map::new_str_hash[int](),
library_search_paths=sess.get_opts().library_search_paths,
mutable next_crate_num=1);
@ -45,6 +44,55 @@ fn read_crates(session::session sess, resolve::crate_map crate_map,
walk::walk_crate(v, crate);
}
type env =
@rec(session::session sess,
@hashmap[str, int] crate_cache,
vec[str] library_search_paths,
mutable int next_crate_num);
fn visit_view_item(env e, &@ast::view_item i) {
alt (i.node) {
case (ast::view_item_use(?ident, ?meta_items, ?id)) {
auto cnum;
if (!e.crate_cache.contains_key(ident)) {
cnum = e.next_crate_num;
load_library_crate(e.sess, i.span, cnum, ident,
meta_items, e.library_search_paths);
e.crate_cache.insert(ident, e.next_crate_num);
e.next_crate_num += 1;
} else { cnum = e.crate_cache.get(ident); }
cstore::add_use_stmt_cnum(e.sess.get_cstore(), id, cnum);
}
case (_) { }
}
}
fn visit_item(env e, &@ast::item i) {
alt (i.node) {
case (ast::item_native_mod(?m)) {
if (m.abi != ast::native_abi_rust &&
m.abi != ast::native_abi_cdecl) {
ret;
}
auto cstore = e.sess.get_cstore();
if (!cstore::add_used_library(cstore, m.native_name)) {
ret;
}
for (ast::attribute a in
attr::find_attrs_by_name(i.attrs, "link_args")) {
alt (attr::get_meta_item_value_str(attr::attr_meta(a))) {
case (some(?linkarg)) {
cstore::add_used_link_args(cstore, linkarg);
}
case (none) { /* fallthrough */ }
}
}
}
case (_) {
}
}
}
// A diagnostic function for dumping crate metadata to an output stream
fn list_file_metadata(str path, io::writer out) {
alt (get_metadata_section(path)) {
@ -178,55 +226,6 @@ fn load_library_crate(&session::session sess, span span, int cnum,
sess.span_fatal(span, #fmt("can't find crate for '%s'", ident));
}
type env =
@rec(session::session sess,
resolve::crate_map crate_map,
@hashmap[str, int] crate_cache,
vec[str] library_search_paths,
mutable int next_crate_num);
fn visit_view_item(env e, &@ast::view_item i) {
alt (i.node) {
case (ast::view_item_use(?ident, ?meta_items, ?id)) {
auto cnum;
if (!e.crate_cache.contains_key(ident)) {
cnum = e.next_crate_num;
load_library_crate(e.sess, i.span, cnum, ident,
meta_items, e.library_search_paths);
e.crate_cache.insert(ident, e.next_crate_num);
e.next_crate_num += 1;
} else { cnum = e.crate_cache.get(ident); }
e.crate_map.insert(id, cnum);
}
case (_) { }
}
}
fn visit_item(env e, &@ast::item i) {
alt (i.node) {
case (ast::item_native_mod(?m)) {
if (m.abi != ast::native_abi_rust &&
m.abi != ast::native_abi_cdecl) {
ret;
}
auto cstore = e.sess.get_cstore();
if (!cstore::add_used_library(cstore, m.native_name)) {
ret;
}
for (ast::attribute a in
attr::find_attrs_by_name(i.attrs, "link_args")) {
alt (attr::get_meta_item_value_str(attr::attr_meta(a))) {
case (some(?linkarg)) {
cstore::add_used_link_args(cstore, linkarg);
}
case (none) { /* fallthrough */ }
}
}
}
case (_) {
}
}
}
// Local Variables:

View File

@ -4,17 +4,24 @@
import std::map;
import std::vec;
import std::str;
import syntax::ast;
type crate_metadata = rec(str name, vec[u8] data);
// Map from node_id's of local use statements to crate numbers
type use_crate_map = map::hashmap[ast::node_id, ast::crate_num];
type cstore = @rec(map::hashmap[int, crate_metadata] metas,
use_crate_map use_crate_map,
mutable vec[str] used_crate_files,
mutable vec[str] used_libraries,
mutable vec[str] used_link_args);
fn mk_cstore() -> cstore {
auto meta_cache = map::new_int_hash[crate_metadata]();
auto crate_map = map::new_int_hash[ast::crate_num]();
ret @rec(metas = meta_cache,
use_crate_map = crate_map,
mutable used_crate_files = [],
mutable used_libraries = [],
mutable used_link_args = []);
@ -65,6 +72,11 @@ fn get_used_link_args(&cstore cstore) -> vec[str] {
ret cstore.used_link_args;
}
fn add_use_stmt_cnum(&cstore cstore, ast::node_id use_id, int cnum) {
cstore.use_crate_map.insert(use_id, cnum);
}
// Local Variables:
// mode: rust
// fill-column: 78;

View File

@ -9,6 +9,7 @@ import ast::local_def;
import metadata::creader;
import metadata::decoder;
import metadata::cstore;
import driver::session::session;
import util::common::new_def_hash;
import std::map::new_int_hash;
@ -32,7 +33,6 @@ import std::vec;
export resolve_crate;
export def_map;
export crate_map;
// Resolving happens in two passes. The first pass collects defids of all
// (internal) imports and modules, so that they can be looked up when needed,
@ -103,13 +103,10 @@ type indexed_mod =
/* native modules can't contain tags, and we don't store their ASTs because we
only need to look at them to determine exports, which they can't control.*/
// It should be safe to use index to memoize lookups of globbed names.
type crate_map = hashmap[node_id, ast::crate_num];
type def_map = hashmap[node_id, def];
type env =
rec(crate_map crate_map,
rec(cstore::use_crate_map crate_map,
def_map def_map,
constr_table fn_constrs,
ast_map::map ast_map,
@ -128,8 +125,9 @@ tag namespace { ns_value; ns_type; ns_module; }
fn resolve_crate(session sess, &ast_map::map amap, @ast::crate crate) ->
tup(def_map, constr_table) {
creader::read_crates(sess, *crate);
auto e =
@rec(crate_map=new_int_hash[ast::crate_num](),
@rec(crate_map=sess.get_cstore().use_crate_map,
def_map=new_int_hash[def](),
fn_constrs = new_int_hash[ty::constr_def[]](),
ast_map=amap,
@ -138,7 +136,6 @@ fn resolve_crate(session sess, &ast_map::map amap, @ast::crate crate) ->
ext_map=new_def_hash[vec[ident]](),
ext_cache=new_ext_hash(),
sess=sess);
creader::read_crates(sess, e.crate_map, *crate);
map_crate(e, crate);
resolve_imports(*e);
check_for_collisions(e, *crate);