rustc: Resolve tag variant names

This commit is contained in:
Patrick Walton 2010-12-01 10:19:20 -08:00
parent 4dc98e54d1
commit 42282a25c0
3 changed files with 65 additions and 18 deletions

View file

@ -30,7 +30,7 @@ tag def {
def_const(def_id);
def_arg(def_id);
def_local(def_id);
def_variant(def_id);
def_variant(def_id /* tag */, def_id /* variant */);
def_ty(def_id);
def_ty_arg(def_id);
}
@ -175,8 +175,13 @@ type _fn = rec(vec[arg] inputs,
@ty output,
block body);
tag mod_index_entry {
mie_item(uint);
mie_tag_variant(uint /* tag item index */, uint /* variant index */);
}
type _mod = rec(vec[@item] items,
hashmap[ident,uint] index);
hashmap[ident,mod_index_entry] index);
type variant = rec(str name, vec[@ty] args, def_id id);

View file

@ -1155,7 +1155,7 @@ impure fn parse_ty_params(parser p) -> vec[ast.ty_param] {
ret ty_params;
}
impure fn parse_item_fn(parser p) -> tup(ast.ident, @ast.item) {
impure fn parse_item_fn(parser p) -> @ast.item {
auto lo = p.get_span();
expect(p, token.FN);
auto id = parse_ident(p);
@ -1186,23 +1186,46 @@ impure fn parse_item_fn(parser p) -> tup(ast.ident, @ast.item) {
body = body);
auto item = ast.item_fn(id, f, ty_params, p.next_def_id(), ast.ann_none);
ret tup(id, @spanned(lo, body.span, item));
ret @spanned(lo, body.span, item);
}
impure fn parse_mod_items(parser p, token.token term) -> ast._mod {
let vec[@ast.item] items = vec();
let hashmap[ast.ident,uint] index = new_str_hash[uint]();
let vec[@ast.item] items = vec();
auto index = new_str_hash[ast.mod_index_entry]();
let uint u = 0u;
while (p.peek() != term) {
auto pair = parse_item(p);
append[@ast.item](items, pair._1);
index.insert(pair._0, u);
auto item = parse_item(p);
items += vec(item);
// Index the item.
alt (item.node) {
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;
}
}
}
u += 1u;
}
ret rec(items=items, index=index);
}
impure fn parse_item_mod(parser p) -> tup(ast.ident, @ast.item) {
impure fn parse_item_mod(parser p) -> @ast.item {
auto lo = p.get_span();
expect(p, token.MOD);
auto id = parse_ident(p);
@ -1211,10 +1234,10 @@ impure fn parse_item_mod(parser p) -> tup(ast.ident, @ast.item) {
auto hi = p.get_span();
expect(p, token.RBRACE);
auto item = ast.item_mod(id, m, p.next_def_id());
ret tup(id, @spanned(lo, hi, item));
ret @spanned(lo, hi, item);
}
impure fn parse_item_type(parser p) -> tup(ast.ident, @ast.item) {
impure fn parse_item_type(parser p) -> @ast.item {
auto lo = p.get_span();
expect(p, token.TYPE);
auto id = parse_ident(p);
@ -1225,10 +1248,10 @@ impure fn parse_item_type(parser p) -> tup(ast.ident, @ast.item) {
auto hi = p.get_span();
expect(p, token.SEMI);
auto item = ast.item_ty(id, ty, tps, p.next_def_id(), ast.ann_none);
ret tup(id, @spanned(lo, hi, item));
ret @spanned(lo, hi, item);
}
impure fn parse_item_tag(parser p) -> tup(ast.ident, @ast.item) {
impure fn parse_item_tag(parser p) -> @ast.item {
auto lo = p.get_span();
expect(p, token.TAG);
auto id = parse_ident(p);
@ -1273,10 +1296,10 @@ impure fn parse_item_tag(parser p) -> tup(ast.ident, @ast.item) {
auto hi = p.get_span();
auto item = ast.item_tag(id, variants, ty_params, p.next_def_id());
ret tup(id, @spanned(lo, hi, item));
ret @spanned(lo, hi, item);
}
impure fn parse_item(parser p) -> tup(ast.ident, @ast.item) {
impure fn parse_item(parser p) -> @ast.item {
alt (p.peek()) {
case (token.FN) {
ret parse_item_fn(p);

View file

@ -38,6 +38,9 @@ fn lookup_name(&env e, ast.ident i) -> option.t[def] {
case (ast.item_ty(_, _, _, ?id, _)) {
ret some[def](ast.def_ty(id));
}
case (ast.item_tag(_, _, _, ?id)) {
ret some[def](ast.def_ty(id));
}
}
}
@ -59,8 +62,24 @@ fn lookup_name(&env e, ast.ident i) -> option.t[def] {
fn check_mod(ast.ident i, ast._mod m) -> option.t[def] {
alt (m.index.find(i)) {
case (some[uint](?ix)) {
ret found_def_item(m.items.(ix));
case (some[ast.mod_index_entry](?ent)) {
alt (ent) {
case (ast.mie_item(?ix)) {
ret found_def_item(m.items.(ix));
}
case (ast.mie_tag_variant(?item_idx, ?variant_idx)) {
alt (m.items.(item_idx).node) {
case (ast.item_tag(_, ?variants, _, ?tid)) {
auto vid = variants.(variant_idx).id;
ret some[def](ast.def_variant(tid, vid));
}
case (_) {
log "tag item not actually a tag";
fail;
}
}
}
}
}
}
ret none[def];