aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPatrick Walton <[email protected]>2010-12-01 10:19:20 -0800
committerPatrick Walton <[email protected]>2010-12-01 10:19:38 -0800
commit42282a25c03182a19e80fb1a3294d3869859d6fa (patch)
treed1e68961c74eae3f52e5ab937121b62e562166a7 /src
parentMake the ugly detailed leak-spray on rustc failures optional. (diff)
downloadrust-42282a25c03182a19e80fb1a3294d3869859d6fa.tar.xz
rust-42282a25c03182a19e80fb1a3294d3869859d6fa.zip
rustc: Resolve tag variant names
Diffstat (limited to 'src')
-rw-r--r--src/comp/front/ast.rs9
-rw-r--r--src/comp/front/parser.rs51
-rw-r--r--src/comp/middle/resolve.rs23
3 files changed, 65 insertions, 18 deletions
diff --git a/src/comp/front/ast.rs b/src/comp/front/ast.rs
index 9b503c71..06e80d7b 100644
--- a/src/comp/front/ast.rs
+++ b/src/comp/front/ast.rs
@@ -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);
diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs
index 709dbc05..eeb279a7 100644
--- a/src/comp/front/parser.rs
+++ b/src/comp/front/parser.rs
@@ -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);
diff --git a/src/comp/middle/resolve.rs b/src/comp/middle/resolve.rs
index ed4d6c04..2655a6c5 100644
--- a/src/comp/middle/resolve.rs
+++ b/src/comp/middle/resolve.rs
@@ -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];