Get rid of block indices

This commit is contained in:
Marijn Haverbeke 2011-05-11 15:10:24 +02:00
parent 7d086df095
commit 5405f45274
6 changed files with 62 additions and 116 deletions

View file

@ -94,15 +94,8 @@ type meta_item = spanned[meta_item_];
type meta_item_ = rec(ident name, str value);
type block = spanned[block_];
type block_index = hashmap[ident, block_index_entry];
tag block_index_entry {
bie_item(@item);
bie_local(@local);
bie_tag_variant(@item /* tag item */, uint /* variant index */);
}
type block_ = rec(vec[@stmt] stmts,
Option.t[@expr] expr,
hashmap[ident,block_index_entry] index,
ann a); /* ann is only meaningful for the ts_ann field */
type variant_def = tup(def_id /* tag */, def_id /* variant */);
@ -421,6 +414,18 @@ tag item_ {
item_obj(ident, _obj, vec[ty_param], obj_def_ids, ann);
}
fn item_ident(@item it) -> ident {
ret alt (it.node) {
case (item_const(?ident, _, _, _, _)) { ident }
case (item_fn(?ident, _, _, _, _)) { ident }
case (item_mod(?ident, _, _)) { ident }
case (item_native_mod(?ident, _, _)) { ident }
case (item_ty(?ident, _, _, _, _)) { ident }
case (item_tag(?ident, _, _, _, _)) { ident }
case (item_obj(?ident, _, _, _, _)) { ident }
}
}
type native_item = spanned[native_item_];
tag native_item_ {
native_item_ty(ident, def_id);
@ -500,44 +505,6 @@ fn index_native_view_item(native_mod_index index, @view_item it) {
}
}
fn index_stmt(block_index index, @stmt s) {
alt (s.node) {
case (ast.stmt_decl(?d,_)) {
alt (d.node) {
case (ast.decl_local(?loc)) {
index.insert(loc.ident, ast.bie_local(loc));
}
case (ast.decl_item(?it)) {
alt (it.node) {
case (ast.item_fn(?i, _, _, _, _)) {
index.insert(i, ast.bie_item(it));
}
case (ast.item_mod(?i, _, _)) {
index.insert(i, ast.bie_item(it));
}
case (ast.item_ty(?i, _, _, _, _)) {
index.insert(i, ast.bie_item(it));
}
case (ast.item_tag(?i, ?variants, _, _, _)) {
index.insert(i, ast.bie_item(it));
let uint vid = 0u;
for (ast.variant v in variants) {
auto t = ast.bie_tag_variant(it, vid);
index.insert(v.node.name, t);
vid += 1u;
}
}
case (ast.item_obj(?i, _, _, _, _)) {
index.insert(i, ast.bie_item(it));
}
}
}
}
}
case (_) { /* fall through */ }
}
}
fn is_exported(ident i, _mod m) -> bool {
auto count = 0;
for (@ast.view_item vi in m.view_items) {

View file

@ -1626,15 +1626,6 @@ fn parse_source_stmt(parser p) -> @ast.stmt {
fail;
}
fn index_block(parser p, vec[@ast.stmt] stmts, Option.t[@ast.expr] expr)
-> ast.block_ {
auto index = new_str_hash[ast.block_index_entry]();
for (@ast.stmt s in stmts) {
ast.index_stmt(index, s);
}
ret rec(stmts=stmts, expr=expr, index=index, a=p.get_ann());
}
fn index_arm(@ast.pat pat) -> hashmap[ast.ident,ast.def_id] {
fn do_index_arm(&hashmap[ast.ident,ast.def_id] index, @ast.pat pat) {
alt (pat.node) {
@ -1770,7 +1761,7 @@ fn parse_block(parser p) -> ast.block {
auto hi = p.get_hi_pos();
p.bump();
auto bloc = index_block(p, stmts, expr);
auto bloc = rec(stmts=stmts, expr=expr, a=p.get_ann());
ret spanned[ast.block_](lo, hi, bloc);
}

View file

@ -862,7 +862,6 @@ fn fold_stmt[ENV](&ENV env, &ast_fold[ENV] fld, &@stmt s) -> @stmt {
fn fold_block[ENV](&ENV env, &ast_fold[ENV] fld, &block blk) -> block {
auto index = new_str_hash[ast.block_index_entry]();
let ENV env_ = fld.update_env_for_block(env, blk);
if (!fld.keep_going(env_)) {
@ -873,7 +872,6 @@ fn fold_block[ENV](&ENV env, &ast_fold[ENV] fld, &block blk) -> block {
for (@ast.stmt s in blk.node.stmts) {
auto new_stmt = fold_stmt[ENV](env_, fld, s);
Vec.push[@ast.stmt](stmts, new_stmt);
ast.index_stmt(index, new_stmt);
}
auto expr = none[@ast.expr];
@ -887,7 +885,7 @@ fn fold_block[ENV](&ENV env, &ast_fold[ENV] fld, &block blk) -> block {
}
auto aa = fld.fold_ann(env, blk.node.a);
ret respan(blk.span, rec(stmts=stmts, expr=expr, index=index, a=aa));
ret respan(blk.span, rec(stmts=stmts, expr=expr, a=aa));
}
fn fold_arm[ENV](&ENV env, &ast_fold[ENV] fld, &arm a) -> arm {

View file

@ -500,82 +500,77 @@ fn lookup_in_obj(ident id, &ast._obj ob, &vec[ast.ty_param] ty_params,
fn lookup_in_block(ident id, &ast.block_ b, namespace ns)
-> Option.t[def] {
alt (b.index.find(id)) {
case (some[ast.block_index_entry](?ix)) {
alt (ix) {
case (ast.bie_item(?it)) {
ret found_def_item(it, ns);
}
case (ast.bie_local(?l)) {
if (ns == ns_value) {
ret some(ast.def_local(l.id));
for (@ast.stmt st in b.stmts) {
alt (st.node) {
case (ast.stmt_decl(?d,_)) {
alt (d.node) {
case (ast.decl_local(?loc)) {
if (ns == ns_value && Str.eq(id, loc.ident)) {
ret some(ast.def_local(loc.id));
}
}
case (ast.bie_tag_variant(?item, ?variant_idx)) {
if (ns == ns_value) {
ret some(found_def_tag(item, variant_idx));
case (ast.decl_item(?it)) {
alt (it.node) {
case (ast.item_tag(?name, ?variants, _,
?defid, _)) {
if (ns == ns_type) {
if (Str.eq(name, id)) {
ret some(ast.def_ty(defid));
}
} else {
for (ast.variant v in variants) {
if (Str.eq(v.node.name, id)) {
ret some(ast.def_variant(
defid, v.node.id));
}
}
}
}
case (_) { }
case (_) {
if (Str.eq(ast.item_ident(it), id)) {
auto found = found_def_item(it, ns);
if (found != none[def]) { ret found; }
}
}
}
}
}
}
case (_) {}
}
}
ret none[def];
}
fn found_def_item(@ast.item i, namespace ns) -> Option.t[def] {
alt (i.node) {
case (ast.item_const(_, _, _, ?id, _)) {
if (ns == ns_value) {
ret some(ast.def_const(id));
case (ast.item_const(_, _, _, ?defid, _)) {
if (ns == ns_value) { ret some(ast.def_const(defid)); }
}
case (ast.item_fn(_, _, _, ?defid, _)) {
if (ns == ns_value) { ret some(ast.def_fn(defid)); }
}
case (ast.item_fn(_, _, _, ?id, _)) {
if (ns == ns_value) {
ret some(ast.def_fn(id));
case (ast.item_mod(_, _, ?defid)) {
ret some(ast.def_mod(defid));
}
case (ast.item_native_mod(_, _, ?defid)) {
ret some(ast.def_native_mod(defid));
}
case (ast.item_mod(_, _, ?id)) {
ret some(ast.def_mod(id));
}
case (ast.item_native_mod(_, _, ?id)) {
ret some(ast.def_native_mod(id));
}
case (ast.item_ty(_, _, _, ?id, _)) {
if (ns == ns_type) {
ret some(ast.def_ty(id));
}
}
case (ast.item_tag(_, _, _, ?id, _)) {
if (ns == ns_type) {
ret some(ast.def_ty(id));
case (ast.item_ty(_, _, _, ?defid, _)) {
if (ns == ns_type) { ret some(ast.def_ty(defid)); }
}
case (ast.item_tag(_, _, _, ?defid, _)) {
if (ns == ns_type) { ret some(ast.def_ty(defid)); }
}
case (ast.item_obj(_, _, _, ?odid, _)) {
if (ns == ns_value) {
ret some(ast.def_obj(odid.ctor));
} else {
ret some(ast.def_obj(odid.ty));
}
if (ns == ns_value) { ret some(ast.def_obj(odid.ctor)); }
else { ret some(ast.def_obj(odid.ty)); }
}
case (_) { }
}
ret none[def];
}
fn found_def_tag(@ast.item item, uint variant_idx) -> def {
alt (item.node) {
case (ast.item_tag(_, ?variants, _, ?tid, _)) {
auto vid = variants.(variant_idx).node.id;
ret ast.def_variant(tid, vid);
}
case (_) {
log_err "tag item not actually a tag";
fail;
}
}
}
fn lookup_in_mod_strict(&env e, def m, &span sp, ident id,
namespace ns, dir dr) -> def {
alt (lookup_in_mod(e, m, id, ns, dr)) {

View file

@ -1507,7 +1507,6 @@ mod Pushdown {
auto e_1 = pushdown_expr(fcx, expected, e_0);
auto block_ = rec(stmts=bloc.node.stmts,
expr=some[@ast.expr](e_1),
index=bloc.node.index,
a=plain_ann(fcx.ccx.tcx));
ret fold.respan[ast.block_](bloc.span, block_);
}
@ -2806,7 +2805,6 @@ fn check_block(&@fn_ctxt fcx, &ast.block block) -> ast.block {
ret fold.respan[ast.block_](block.span,
rec(stmts=stmts, expr=expr,
index=block.node.index,
a=plain_ann(fcx.ccx.tcx)));
}

View file

@ -6,7 +6,6 @@ import front.ast.mutability;
import front.ast.item;
import front.ast.block;
import front.ast.block_;
import front.ast.block_index_entry;
import front.ast.mod_index_entry;
import front.ast.obj_field;
import front.ast.decl;
@ -2337,12 +2336,10 @@ fn annotate_stmt(&fn_info_map fm, &@stmt s) -> @stmt {
}
fn annotate_block(&fn_info_map fm, &block b) -> block {
let vec[@stmt] new_stmts = vec();
auto new_index = new_str_hash[block_index_entry]();
for (@stmt s in b.node.stmts) {
auto new_s = annotate_stmt(fm, s);
Vec.push[@stmt](new_stmts, new_s);
ast.index_stmt(new_index, new_s);
}
fn ann_e(fn_info_map fm, &@expr e) -> @expr {
ret annotate_expr(fm, e);
@ -2352,7 +2349,7 @@ fn annotate_block(&fn_info_map fm, &block b) -> block {
auto new_e = Option.map[@expr, @expr](f, b.node.expr);
ret respan(b.span,
rec(stmts=new_stmts, expr=new_e, index=new_index with b.node));
rec(stmts=new_stmts, expr=new_e with b.node));
}
fn annotate_fn(&fn_info_map fm, &ast._fn f) -> ast._fn {
// subexps have *already* been annotated based on