diff options
| author | Rafael Ávila de Espíndola <[email protected]> | 2011-01-01 13:13:00 -0500 |
|---|---|---|
| committer | Rafael Ávila de Espíndola <[email protected]> | 2011-01-04 11:35:13 -0500 |
| commit | e0fe271d3459d04af9e1580c396b4698063e1999 (patch) | |
| tree | ab869f8a2779d8c5819c1493881225cdc863d043 /src/comp | |
| parent | Refactor the view_item code so that it is similar to the code used for (diff) | |
| download | rust-e0fe271d3459d04af9e1580c396b4698063e1999.tar.xz rust-e0fe271d3459d04af9e1580c396b4698063e1999.zip | |
Add support for looking up a name introduced by a 'use'.
With this we go from "error: unresolved name: foo" to
"unimplemented definition variant for: foo" in
use foo;
fn main(vec[str] args) {
foo.bar();
}
Diffstat (limited to 'src/comp')
| -rw-r--r-- | src/comp/front/ast.rs | 12 | ||||
| -rw-r--r-- | src/comp/front/parser.rs | 23 | ||||
| -rw-r--r-- | src/comp/middle/resolve.rs | 14 |
3 files changed, 38 insertions, 11 deletions
diff --git a/src/comp/front/ast.rs b/src/comp/front/ast.rs index 55207de0..15c0e499 100644 --- a/src/comp/front/ast.rs +++ b/src/comp/front/ast.rs @@ -35,6 +35,7 @@ tag def { def_ty(def_id); def_ty_arg(def_id); def_binding(def_id); + def_use(def_id); } type crate = spanned[crate_]; @@ -212,20 +213,23 @@ type _obj = rec(vec[obj_field] fields, tag mod_index_entry { + mie_use(uint); mie_item(uint); mie_tag_variant(uint /* tag item index */, uint /* variant index */); } -type _mod = rec(vec[@item] items, - hashmap[ident,mod_index_entry] index); +type mod_index = hashmap[ident,mod_index_entry]; +type _mod = rec(vec[@view_item] view_items, + vec[@item] items, + mod_index index); type variant_arg = rec(@ty ty, def_id id); 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]); - view_item_import(vec[ident]); + view_item_use(ident, vec[@meta_item], def_id); + view_item_import(vec[ident], def_id); } type item = spanned[item_]; diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs index cdf2ae17..8c3db0e1 100644 --- a/src/comp/front/parser.rs +++ b/src/comp/front/parser.rs @@ -1405,11 +1405,10 @@ impure fn parse_item_obj(parser p, ast.layer lyr) -> @ast.item { } impure fn parse_mod_items(parser p, token.token term) -> ast._mod { - parse_view(p); - - let vec[@ast.item] items = vec(); 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); @@ -1443,7 +1442,7 @@ impure fn parse_mod_items(parser p, token.token term) -> ast._mod { u += 1u; } - ret rec(items=items, index=index); + ret rec(view_items=view_items, items=items, index=index); } impure fn parse_item_const(parser p) -> @ast.item { @@ -1669,7 +1668,7 @@ impure fn parse_use(parser p) -> @ast.view_item { auto ident = parse_ident(p); auto metadata = parse_optional_meta(p); expect(p, token.SEMI); - auto use_decl = ast.view_item_use(ident, metadata); + auto use_decl = ast.view_item_use(ident, metadata, p.next_def_id()); ret @spanned(lo, hi, use_decl); } @@ -1684,7 +1683,7 @@ impure fn parse_rest_import_name(parser p, ast.ident id) -> @ast.view_item { identifiers += i; } p.bump(); - auto import_decl = ast.view_item_import(identifiers); + auto import_decl = ast.view_item_import(identifiers, p.next_def_id()); ret @spanned(lo, hi, import_decl); } @@ -1744,11 +1743,21 @@ fn is_use_or_import(token.token t) -> bool { ret false; } -impure fn parse_view(parser p) -> vec[@ast.view_item] { +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_use(u)); + } + case(ast.view_item_import(?ids,_)) { + // FIXME + } + } + u = u + 1u; } ret items; } diff --git a/src/comp/middle/resolve.rs b/src/comp/middle/resolve.rs index 7f344d95..551993dd 100644 --- a/src/comp/middle/resolve.rs +++ b/src/comp/middle/resolve.rs @@ -67,10 +67,24 @@ fn lookup_name(&env e, ast.ident i) -> option.t[def] { ret none[def]; } + fn found_def_view(@ast.view_item i) -> option.t[def] { + alt (i.node) { + case (ast.view_item_use(_, _, ?id)) { + ret some[def](ast.def_use(id)); + } + case (ast.view_item_import(_,?id)) { + fail; + } + } + } + fn check_mod(ast.ident i, ast._mod m) -> option.t[def] { alt (m.index.find(i)) { case (some[ast.mod_index_entry](?ent)) { alt (ent) { + case (ast.mie_use(?ix)) { + ret found_def_view(m.view_items.(ix)); + } case (ast.mie_item(?ix)) { ret found_def_item(m.items.(ix)); } |