rustc: Store cached crate metadata in the session

This commit is contained in:
Patrick Walton 2011-03-25 11:10:50 -07:00
parent 9ca7acb1f3
commit e2d6475308
6 changed files with 37 additions and 35 deletions

View File

@ -77,6 +77,10 @@ obj session(cfg targ, map.hashmap[int, crate_metadata] crates) {
fn set_external_crate(int num, &crate_metadata metadata) {
crates.insert(num, metadata);
}
fn has_external_crate(int num) -> bool {
ret crates.contains_key(num);
}
}

View File

@ -21,7 +21,6 @@ type ty_param = rec(ident ident, def_id id);
// Annotations added during successive passes.
tag ann {
ann_none;
ann_crate(@external_crate_info);
ann_type(@middle.ty.t, option.t[vec[@middle.ty.t]] /* ty param substs */);
}
@ -359,7 +358,7 @@ type variant = rec(str name, vec[variant_arg] args, def_id id, ann ann);
type view_item = spanned[view_item_];
tag view_item_ {
view_item_use(ident, vec[@meta_item], def_id, ann);
view_item_use(ident, vec[@meta_item], def_id, option.t[int]);
view_item_import(ident, vec[ident], def_id, option.t[def]);
view_item_export(ident);
}
@ -382,8 +381,6 @@ tag native_item_ {
fn_decl, vec[ty_param], def_id, ann);
}
type external_crate_info = rec(vec[u8] data);
fn index_view_item(mod_index index, @view_item it) {
alt (it.node) {
case(ast.view_item_use(?id, _, _, _)) {

View File

@ -17,14 +17,16 @@ import std._str;
import std._vec;
import std.fs;
import std.option;
import std.option.some;
import std.os;
import std.map.hashmap;
// TODO: map to a real type here.
type env = @rec(
session.session sess,
@hashmap[str, @ast.external_crate_info] crate_cache,
vec[str] library_search_paths
@hashmap[str, int] crate_cache,
vec[str] library_search_paths,
mutable int next_crate_num
);
// Type decoding
@ -215,7 +217,7 @@ impure fn parse_ty_fn(@pstate st, str_def sd) -> tup(vec[ty.arg], @ty.t) {
fn load_crate(session.session sess,
ast.ident ident,
vec[str] library_search_paths) -> @ast.external_crate_info {
vec[str] library_search_paths) {
auto filename = parser.default_native_name(sess, ident);
for (str library_search_path in library_search_paths) {
auto path = fs.connect(library_search_path, filename);
@ -232,8 +234,9 @@ fn load_crate(session.session sess,
auto cbuf = llvmext.LLVMGetSectionContents(si.llsi);
auto csz = llvmext.LLVMGetSectionSize(si.llsi);
auto cvbuf = cbuf as _vec.vbuf;
ret @rec(data=_vec.vec_from_vbuf[u8](cvbuf, csz));
ret;
}
llvmext.LLVMMoveToNextSection(si.llsi);
}
}
}
@ -244,18 +247,19 @@ fn load_crate(session.session sess,
}
fn fold_view_item_use(&env e, &span sp, ast.ident ident,
vec[@ast.meta_item] meta_items, ast.def_id id, ast.ann orig_ann)
vec[@ast.meta_item] meta_items, ast.def_id id, option.t[int] cnum_opt)
-> @ast.view_item {
auto external_crate;
auto cnum;
if (!e.crate_cache.contains_key(ident)) {
external_crate = load_crate(e.sess, ident, e.library_search_paths);
e.crate_cache.insert(ident, external_crate);
load_crate(e.sess, ident, e.library_search_paths);
cnum = e.next_crate_num;
e.crate_cache.insert(ident, e.next_crate_num);
e.next_crate_num += 1;
} else {
external_crate = e.crate_cache.get(ident);
cnum = e.crate_cache.get(ident);
}
auto ann = ast.ann_crate(external_crate);
auto viu = ast.view_item_use(ident, meta_items, id, ann);
auto viu = ast.view_item_use(ident, meta_items, id, some[int](cnum));
ret @fold.respan[ast.view_item_](sp, viu);
}
@ -265,8 +269,9 @@ fn read_crates(session.session sess,
vec[str] library_search_paths) -> @ast.crate {
auto e = @rec(
sess=sess,
crate_cache=@common.new_str_hash[@ast.external_crate_info](),
library_search_paths=library_search_paths
crate_cache=@common.new_str_hash[int](),
library_search_paths=library_search_paths,
mutable next_crate_num=1
);
auto f = fold_view_item_use;
@ -274,9 +279,8 @@ fn read_crates(session.session sess,
ret fold.fold_crate[env](e, fld, crate);
}
fn lookup_def(&span sp,
@ast.external_crate_info cinfo,
vec[ast.ident] path) -> ast.def {
fn lookup_def(session.session sess, &span sp, int cnum, vec[ast.ident] path)
-> ast.def {
// FIXME: fill in.
fail;
}

View File

@ -2204,7 +2204,7 @@ impure fn parse_use(parser p) -> @ast.view_item {
auto metadata = parse_optional_meta(p);
expect(p, token.SEMI);
auto use_decl = ast.view_item_use(ident, metadata, p.next_def_id(),
ast.ann_none);
none[int]);
ret @spanned(lo, hi, use_decl);
}

View File

@ -265,7 +265,7 @@ type ast_fold[ENV] =
// View Item folds.
(fn(&ENV e, &span sp, ident ident,
vec[@meta_item] meta_items,
def_id id, ann a) -> @view_item) fold_view_item_use,
def_id id, option.t[int]) -> @view_item) fold_view_item_use,
(fn(&ENV e, &span sp, ident i, vec[ident] idents,
def_id id, option.t[def]) -> @view_item) fold_view_item_import,
@ -883,9 +883,9 @@ fn fold_view_item[ENV](&ENV env, ast_fold[ENV] fld, @view_item vi)
}
alt (vi.node) {
case (ast.view_item_use(?ident, ?meta_items, ?def_id, ?ann)) {
case (ast.view_item_use(?ident, ?meta_items, ?def_id, ?cnum)) {
ret fld.fold_view_item_use(env_, vi.span, ident, meta_items,
def_id, ann);
def_id, cnum);
}
case (ast.view_item_import(?def_ident, ?idents, ?def_id,
?target_def)) {
@ -1400,8 +1400,9 @@ fn identity_fold_item_obj[ENV](&ENV e, &span sp, ident i,
fn identity_fold_view_item_use[ENV](&ENV e, &span sp, ident i,
vec[@meta_item] meta_items,
def_id id, ann a) -> @view_item {
ret @respan(sp, ast.view_item_use(i, meta_items, id, a));
def_id id, option.t[int] cnum)
-> @view_item {
ret @respan(sp, ast.view_item_use(i, meta_items, id, cnum));
}
fn identity_fold_view_item_import[ENV](&ENV e, &span sp, ident i,

View File

@ -137,11 +137,10 @@ fn find_final_def(&env e, import_map index,
}
fn found_crate(&env e, &import_map index, &span sp,
vec[ident] idents,
@ast.external_crate_info cinfo) -> def_wrap {
vec[ident] idents, int cnum) -> def_wrap {
auto len = _vec.len[ident](idents);
auto rest_idents = _vec.slice[ident](idents, 1u, len);
auto def = creader.lookup_def(sp, cinfo, rest_idents);
auto def = creader.lookup_def(e.sess, sp, cnum, rest_idents);
ret def_wrap_other(def);
}
@ -171,12 +170,9 @@ fn find_final_def(&env e, import_map index,
}
case (def_wrap_use(?vi)) {
alt (vi.node) {
case (ast.view_item_use(_, _, _, ?ann)) {
alt (ann) {
case (ast.ann_crate(?cinfo)) {
ret found_crate(e, index, sp, idents, cinfo);
}
}
case (ast.view_item_use(_, _, _, ?cnum_opt)) {
auto cnum = option.get[int](cnum_opt);
ret found_crate(e, index, sp, idents, cnum);
}
}
}