Change mod_index_entry to point directly to items and view_items.

This commit is contained in:
Rafael Ávila de Espíndola 2011-01-04 18:19:36 -05:00 committed by Graydon Hoare
parent 2aa36777f1
commit 359d72b4d0
4 changed files with 66 additions and 58 deletions

View file

@ -1,6 +1,7 @@
import std.map.hashmap;
import std.option;
import std._vec;
import util.common.span;
import util.common.spanned;
import util.common.ty_mach;
@ -213,9 +214,9 @@ type _obj = rec(vec[obj_field] fields,
tag mod_index_entry {
mie_view_item(uint);
mie_item(uint);
mie_tag_variant(uint /* tag item index */, uint /* variant index */);
mie_view_item(@view_item);
mie_item(@item);
mie_tag_variant(@item /* tag item */, uint /* variant index */);
}
type mod_index = hashmap[ident,mod_index_entry];
@ -242,6 +243,47 @@ tag item_ {
item_obj(ident, _obj, vec[ty_param], def_id, ann);
}
fn index_view_item(mod_index index, @view_item it) {
alt (it.node) {
case(ast.view_item_use(?id, _, _)) {
index.insert(id, ast.mie_view_item(it));
}
case(ast.view_item_import(?ids,_)) {
auto len = _vec.len[ast.ident](ids);
auto last_id = ids.(len - 1u);
index.insert(last_id, ast.mie_view_item(it));
}
}
}
fn index_item(mod_index index, @item it) {
alt (it.node) {
case (ast.item_const(?id, _, _, _, _)) {
index.insert(id, ast.mie_item(it));
}
case (ast.item_fn(?id, _, _, _, _)) {
index.insert(id, ast.mie_item(it));
}
case (ast.item_mod(?id, _, _)) {
index.insert(id, ast.mie_item(it));
}
case (ast.item_ty(?id, _, _, _, _)) {
index.insert(id, ast.mie_item(it));
}
case (ast.item_tag(?id, ?variants, _, _)) {
index.insert(id, ast.mie_item(it));
let uint variant_idx = 0u;
for (ast.variant v in variants) {
index.insert(v.name,
ast.mie_tag_variant(it, variant_idx));
variant_idx += 1u;
}
}
case (ast.item_obj(?id, _, _, _, _)) {
index.insert(id, ast.mie_item(it));
}
}
}
//
// Local Variables:

View file

@ -1418,44 +1418,16 @@ impure fn parse_item_obj(parser p, ast.layer lyr) -> @ast.item {
ret @spanned(lo, meths.span, item);
}
fn index_mod_item(@ast.item item, ast.mod_index index, uint u) {
alt (item.node) {
case (ast.item_const(?id, _, _, _, _)) {
index.insert(id, ast.mie_item(u));
}
case (ast.item_fn(?id, _, _, _, _)) {
index.insert(id, ast.mie_item(u));
}
case (ast.item_mod(?id, _, _)) {
index.insert(id, ast.mie_item(u));
}
case (ast.item_ty(?id, _, _, _, _)) {
index.insert(id, ast.mie_item(u));
}
case (ast.item_tag(?id, ?variants, _, _)) {
index.insert(id, ast.mie_item(u));
let uint variant_idx = 0u;
for (ast.variant v in variants) {
index.insert(v.name, ast.mie_tag_variant(u, variant_idx));
variant_idx += 1u;
}
}
case (ast.item_obj(?id, _, _, _, _)) {
index.insert(id, ast.mie_item(u));
}
}
}
impure fn parse_mod_items(parser p, token.token term) -> ast._mod {
auto index = new_str_hash[ast.mod_index_entry]();
auto view_items = parse_view(p, index);
let uint u = 0u;
let vec[@ast.item] items = vec();
while (p.peek() != term) {
auto item = parse_item(p);
items += vec(item);
index_mod_item(item, index, u);
u += 1u;
// Index the item.
ast.index_item(index, item);
}
ret rec(view_items=view_items, items=items, index=index);
}
@ -1760,21 +1732,11 @@ fn is_use_or_import(token.token t) -> bool {
impure fn parse_view(parser p, ast.mod_index index) -> vec[@ast.view_item] {
let vec[@ast.view_item] items = vec();
let uint u = 0u;
while (is_use_or_import(p.peek())) {
auto item = parse_use_or_import(p);
items += vec(item);
alt (item.node) {
case(ast.view_item_use(?id, _, _)) {
index.insert(id, ast.mie_view_item(u));
}
case(ast.view_item_import(?ids,_)) {
auto len = _vec.len[ast.ident](ids);
auto last_id = ids.(len - 1u);
index.insert(last_id, ast.mie_view_item(u));
}
}
u = u + 1u;
ast.index_view_item(index, item);
}
ret items;
}
@ -1801,7 +1763,7 @@ impure fn parse_crate_directive(str prefix, parser p,
alt (p.peek()) {
case (token.CONST) {
auto c = parse_item_const(p);
index_mod_item(c, index, _vec.len[@ast.item](items));
ast.index_item(index, c);
append[@ast.item](items, c);
}
case (token.MOD) {
@ -1834,7 +1796,7 @@ impure fn parse_crate_directive(str prefix, parser p,
auto m0 = parse_mod_items(p0, token.EOF);
auto im = ast.item_mod(id, m0, p.next_def_id());
auto i = @spanned(lo, hi, im);
index_mod_item(i, index, _vec.len[@ast.item](items));
ast.index_item(index, i);
append[@ast.item](items, i);
}
@ -1848,7 +1810,7 @@ impure fn parse_crate_directive(str prefix, parser p,
expect(p, token.RBRACE);
auto im = ast.item_mod(id, m0, p.next_def_id());
auto i = @spanned(lo, hi, im);
index_mod_item(i, index, _vec.len[@ast.item](items));
ast.index_item(index, i);
append[@ast.item](items, i);
}

View file

@ -779,14 +779,18 @@ fn fold_mod[ENV](&ENV e, ast_fold[ENV] fld, &ast._mod m) -> ast._mod {
let vec[@view_item] view_items = vec();
let vec[@item] items = vec();
auto index = m.index;
auto index = new_str_hash[ast.mod_index_entry]();
for (@view_item vi in m.view_items) {
append[@view_item](view_items, fold_view_item[ENV](e, fld, vi));
auto new_vi = fold_view_item[ENV](e, fld, vi);
append[@view_item](view_items, new_vi);
ast.index_view_item(index, new_vi);
}
for (@item i in m.items) {
append[@item](items, fold_item[ENV](e, fld, i));
auto new_item = fold_item[ENV](e, fld, i);
append[@item](items, new_item);
ast.index_item(index, new_item);
}
ret fld.fold_mod(e, rec(view_items=view_items, items=items, index=index));

View file

@ -166,14 +166,14 @@ fn lookup_name(&env e, option.t[import_map] index,
alt (m.index.find(i)) {
case (some[ast.mod_index_entry](?ent)) {
alt (ent) {
case (ast.mie_view_item(?ix)) {
ret found_def_view(e, index, m.view_items.(ix));
case (ast.mie_view_item(?view_item)) {
ret found_def_view(e, index, view_item);
}
case (ast.mie_item(?ix)) {
ret found_def_item(m.items.(ix));
case (ast.mie_item(?item)) {
ret found_def_item(item);
}
case (ast.mie_tag_variant(?item_idx, ?variant_idx)) {
alt (m.items.(item_idx).node) {
case (ast.mie_tag_variant(?item, ?variant_idx)) {
alt (item.node) {
case (ast.item_tag(_, ?variants, _, ?tid)) {
auto vid = variants.(variant_idx).id;
auto t = ast.def_variant(tid, vid);