diff options
| author | Brian Anderson <[email protected]> | 2011-03-07 21:21:01 -0500 |
|---|---|---|
| committer | Brian Anderson <[email protected]> | 2011-03-07 21:21:01 -0500 |
| commit | 9fc4db6b89213afdf45c02fc2bd2be62b0ddc40c (patch) | |
| tree | 6c84574116273f91cbe89abd256b9f809adf97de /src/comp/middle/resolve.rs | |
| parent | Allow the else part of an expr_if to be either expr_if or expr_block (diff) | |
| parent | rustc: Cast the LLVM representations of tag types when constructing boxes. Un... (diff) | |
| download | rust-9fc4db6b89213afdf45c02fc2bd2be62b0ddc40c.tar.xz rust-9fc4db6b89213afdf45c02fc2bd2be62b0ddc40c.zip | |
Merge branch 'master' into recursive-elseif
Conflicts:
src/Makefile
src/comp/front/ast.rs
src/comp/front/parser.rs
src/comp/middle/fold.rs
src/comp/middle/trans.rs
Diffstat (limited to 'src/comp/middle/resolve.rs')
| -rw-r--r-- | src/comp/middle/resolve.rs | 142 |
1 files changed, 111 insertions, 31 deletions
diff --git a/src/comp/middle/resolve.rs b/src/comp/middle/resolve.rs index 1af3b205..5b6db631 100644 --- a/src/comp/middle/resolve.rs +++ b/src/comp/middle/resolve.rs @@ -18,6 +18,7 @@ import std._vec; tag scope { scope_crate(@ast.crate); scope_item(@ast.item); + scope_native_item(@ast.native_item); scope_loop(@ast.decl); // there's only 1 decl per loop. scope_block(ast.block); scope_arm(ast.arm); @@ -34,6 +35,7 @@ tag def_wrap { def_wrap_use(@ast.view_item); def_wrap_import(@ast.view_item); def_wrap_mod(@ast.item); + def_wrap_native_mod(@ast.item); def_wrap_other(def); def_wrap_expr_field(uint, def); def_wrap_resolving; @@ -103,6 +105,29 @@ fn find_final_def(&env e, import_map index, // should return what a.b.c.d points to in the end. fn found_something(&env e, import_map index, &span sp, vec[ident] idents, def_wrap d) -> def_wrap { + + fn found_mod(&env e, &import_map index, &span sp, + vec[ident] idents, @ast.item i) -> def_wrap { + auto len = _vec.len[ident](idents); + auto rest_idents = _vec.slice[ident](idents, 1u, len); + auto empty_e = rec(scopes = nil[scope], + sess = e.sess); + auto tmp_e = update_env_for_item(empty_e, i); + auto next_i = rest_idents.(0); + auto next_ = lookup_name_wrapped(tmp_e, next_i); + alt (next_) { + case (none[tup(@env, def_wrap)]) { + e.sess.span_err(sp, "unresolved name: " + next_i); + fail; + } + case (some[tup(@env, def_wrap)](?next)) { + auto combined_e = update_env_for_item(e, i); + ret found_something(combined_e, index, sp, + rest_idents, next._1); + } + } + } + alt (d) { case (def_wrap_import(?imp)) { alt (imp.node) { @@ -122,23 +147,10 @@ fn find_final_def(&env e, import_map index, } alt (d) { case (def_wrap_mod(?i)) { - auto rest_idents = _vec.slice[ident](idents, 1u, len); - auto empty_e = rec(scopes = nil[scope], - sess = e.sess); - auto tmp_e = update_env_for_item(empty_e, i); - auto next_i = rest_idents.(0); - auto next_ = lookup_name_wrapped(tmp_e, next_i); - alt (next_) { - case (none[tup(@env, def_wrap)]) { - e.sess.span_err(sp, "unresolved name: " + next_i); - fail; - } - case (some[tup(@env, def_wrap)](?next)) { - auto combined_e = update_env_for_item(e, i); - ret found_something(combined_e, index, sp, - rest_idents, next._1); - } - } + ret found_mod(e, index, sp, idents, i); + } + case (def_wrap_native_mod(?i)) { + ret found_mod(e, index, sp, idents, i); } case (def_wrap_use(?c)) { e.sess.span_err(sp, "Crate access is not implemented"); @@ -201,6 +213,9 @@ fn lookup_name_wrapped(&env e, ast.ident i) -> option.t[tup(@env, def_wrap)] { case (ast.item_mod(_, _, ?id)) { ret def_wrap_mod(i); } + case (ast.item_native_mod(_, _, ?id)) { + ret def_wrap_native_mod(i); + } case (ast.item_ty(_, _, _, ?id, _)) { ret def_wrap_other(ast.def_ty(id)); } @@ -213,6 +228,17 @@ fn lookup_name_wrapped(&env e, ast.ident i) -> option.t[tup(@env, def_wrap)] { } } + fn found_def_native_item(@ast.native_item i) -> def_wrap { + alt (i.node) { + case (ast.native_item_ty(_, ?id)) { + ret def_wrap_other(ast.def_native_ty(id)); + } + case (ast.native_item_fn(_, _, _, ?id, _)) { + ret def_wrap_other(ast.def_native_fn(id)); + } + } + } + fn found_decl_stmt(@ast.stmt s) -> def_wrap { alt (s.node) { case (ast.stmt_decl(?d)) { @@ -267,11 +293,47 @@ fn lookup_name_wrapped(&env e, ast.ident i) -> option.t[tup(@env, def_wrap)] { } } } - case (none[ast.mod_index_entry]) { /* fall through */ } + case (none[ast.mod_index_entry]) { + ret none[def_wrap]; + } + } + } + + fn check_native_mod(ast.ident i, ast.native_mod m) -> option.t[def_wrap] { + + alt (m.index.find(i)) { + case (some[ast.native_mod_index_entry](?ent)) { + alt (ent) { + case (ast.nmie_view_item(?view_item)) { + ret some(found_def_view(view_item)); + } + case (ast.nmie_item(?item)) { + ret some(found_def_native_item(item)); + } + } + } + case (none[ast.native_mod_index_entry]) { + ret none[def_wrap]; + } } - ret none[def_wrap]; } + fn handle_fn_decl(ast.ident i, &ast.fn_decl decl, + &vec[ast.ty_param] ty_params) -> option.t[def_wrap] { + for (ast.arg a in decl.inputs) { + if (_str.eq(a.ident, i)) { + auto t = ast.def_arg(a.id); + ret some(def_wrap_other(t)); + } + } + for (ast.ty_param tp in ty_params) { + if (_str.eq(tp.ident, i)) { + auto t = ast.def_ty_arg(tp.id); + ret some(def_wrap_other(t)); + } + } + ret none[def_wrap]; + } fn in_scope(ast.ident i, &scope s) -> option.t[def_wrap] { alt (s) { @@ -283,9 +345,12 @@ fn lookup_name_wrapped(&env e, ast.ident i) -> option.t[tup(@env, def_wrap)] { case (scope_item(?it)) { alt (it.node) { case (ast.item_fn(_, ?f, ?ty_params, _, _)) { - for (ast.arg a in f.inputs) { - if (_str.eq(a.ident, i)) { - auto t = ast.def_arg(a.id); + ret handle_fn_decl(i, f.decl, ty_params); + } + case (ast.item_obj(_, ?ob, ?ty_params, _, _)) { + for (ast.obj_field f in ob.fields) { + if (_str.eq(f.ident, i)) { + auto t = ast.def_obj_field(f.id); ret some(def_wrap_other(t)); } } @@ -296,13 +361,7 @@ fn lookup_name_wrapped(&env e, ast.ident i) -> option.t[tup(@env, def_wrap)] { } } } - case (ast.item_obj(_, ?ob, ?ty_params, _, _)) { - for (ast.obj_field f in ob.fields) { - if (_str.eq(f.ident, i)) { - auto t = ast.def_obj_field(f.id); - ret some(def_wrap_other(t)); - } - } + case (ast.item_tag(_, _, ?ty_params, _)) { for (ast.ty_param tp in ty_params) { if (_str.eq(tp.ident, i)) { auto t = ast.def_ty_arg(tp.id); @@ -313,6 +372,9 @@ fn lookup_name_wrapped(&env e, ast.ident i) -> option.t[tup(@env, def_wrap)] { case (ast.item_mod(_, ?m, _)) { ret check_mod(i, m); } + case (ast.item_native_mod(_, ?m, _)) { + ret check_native_mod(i, m); + } case (ast.item_ty(_, _, ?ty_params, _, _)) { for (ast.ty_param tp in ty_params) { if (_str.eq(tp.ident, i)) { @@ -325,6 +387,14 @@ fn lookup_name_wrapped(&env e, ast.ident i) -> option.t[tup(@env, def_wrap)] { } } + case (scope_native_item(?it)) { + alt (it.node) { + case (ast.native_item_fn(_, ?decl, ?ty_params, _, _)) { + ret handle_fn_decl(i, decl, ty_params); + } + } + } + case (scope_loop(?d)) { alt (d.node) { case (ast.decl_local(?local)) { @@ -432,8 +502,7 @@ fn fold_expr_path(&env e, &span sp, &ast.path p, &option.t[def] d, path_len = n_idents - remaining + 1u; } case (def_wrap_other(_)) { - check (n_idents == 1u); - path_len = 1u; + path_len = n_idents; } case (def_wrap_mod(?m)) { e.sess.span_err(sp, @@ -491,6 +560,10 @@ fn update_env_for_item(&env e, @ast.item i) -> env { ret rec(scopes = cons[scope](scope_item(i), @e.scopes) with e); } +fn update_env_for_native_item(&env e, @ast.native_item i) -> env { + ret rec(scopes = cons[scope](scope_native_item(i), @e.scopes) with e); +} + fn update_env_for_block(&env e, &ast.block b) -> env { ret rec(scopes = cons[scope](scope_block(b), @e.scopes) with e); } @@ -500,6 +573,9 @@ fn update_env_for_expr(&env e, @ast.expr x) -> env { case (ast.expr_for(?d, _, _, _)) { ret rec(scopes = cons[scope](scope_loop(d), @e.scopes) with e); } + case (ast.expr_for_each(?d, _, _, _)) { + ret rec(scopes = cons[scope](scope_loop(d), @e.scopes) with e); + } case (_) { } } ret e; @@ -517,6 +593,8 @@ fn resolve_imports(session.session sess, @ast.crate crate) -> @ast.crate { = bind fold_view_item_import(_,_,import_index,_,_,_,_), update_env_for_crate = bind update_env_for_crate(_,_), update_env_for_item = bind update_env_for_item(_,_), + update_env_for_native_item = + bind update_env_for_native_item(_,_), update_env_for_block = bind update_env_for_block(_,_), update_env_for_arm = bind update_env_for_arm(_,_), update_env_for_expr = bind update_env_for_expr(_,_) @@ -539,6 +617,8 @@ fn resolve_crate(session.session sess, @ast.crate crate) -> @ast.crate { fold_ty_path = bind fold_ty_path(_,_,_,_), update_env_for_crate = bind update_env_for_crate(_,_), update_env_for_item = bind update_env_for_item(_,_), + update_env_for_native_item = + bind update_env_for_native_item(_,_), update_env_for_block = bind update_env_for_block(_,_), update_env_for_arm = bind update_env_for_arm(_,_), update_env_for_expr = bind update_env_for_expr(_,_) |