rust/src/comp/middle/resolve.rs

408 lines
13 KiB
Rust
Raw Normal View History

import front.ast;
import front.ast.ident;
import front.ast.def;
import front.ast.ann;
import driver.session;
import util.common.new_def_hash;
import util.common.span;
2010-10-18 14:37:30 -07:00
import std.map.hashmap;
import std.list.list;
import std.list.nil;
import std.list.cons;
2010-11-03 17:10:37 -07:00
import std.option;
import std.option.some;
import std.option.none;
2010-10-18 14:37:30 -07:00
import std._str;
2010-11-22 13:22:23 -08:00
import std._vec;
2010-10-18 14:37:30 -07:00
tag scope {
scope_crate(@ast.crate);
scope_item(@ast.item);
scope_block(ast.block);
2010-12-10 18:08:32 -08:00
scope_arm(ast.arm);
2010-10-18 14:37:30 -07:00
}
type env = rec(list[scope] scopes,
session.session sess);
type import_map = std.map.hashmap[ast.def_id,def_wrap];
// A simple wrapper over defs that stores a bit more information about modules
// and uses so that we can use the regular lookup_name when resolving imports.
tag def_wrap {
def_wrap_use(@ast.view_item);
def_wrap_mod(@ast.item);
def_wrap_other(def);
}
fn unwrap_def(option.t[def_wrap] d_) -> option.t[def] {
alt (d_) {
case (none[def_wrap]) {
ret none[def];
}
case (some[def_wrap](?d)) {
alt (d) {
case (def_wrap_use(?it)) {
alt (it.node) {
case (ast.view_item_use(_, _, ?id)) {
ret some[def](ast.def_use(id));
}
}
fail;
}
case (def_wrap_mod(?i)) {
alt (i.node) {
case (ast.item_mod(_, _, ?id)) {
ret some[def](ast.def_mod(id));
}
}
fail;
}
case (def_wrap_other(?d)) {
ret some[def](d);
}
}
}
}
fail;
}
// Follow the path of an import and return what it ultimately points to.
fn find_final_def(&env e, &span sp, vec[ident] idents) -> option.t[def_wrap] {
auto len = _vec.len[ident](idents);
auto first = idents.(0);
auto d_ = lookup_name(e, none[import_map], first);
if (len == 1u) {
ret d_;
}
alt (d_) {
case (none[def_wrap]) {
e.sess.span_err(sp, "unresolved name: " + first);
ret d_;
}
case (some[def_wrap](?d)) {
alt (d) {
case (def_wrap_mod(?i)) {
auto new_idents = _vec.slice[ident](idents, 1u, len);
auto tmp_e = rec(scopes = nil[scope],
sess = e.sess);
auto new_e = update_env_for_item(tmp_e, i);
ret find_final_def(new_e, sp, new_idents);
}
}
}
}
fail;
}
fn lookup_name(&env e, option.t[import_map] index,
ast.ident i) -> option.t[def_wrap] {
2010-10-18 14:37:30 -07:00
// log "resolving name " + i;
2010-10-18 14:37:30 -07:00
fn found_def_item(@ast.item i) -> option.t[def_wrap] {
alt (i.node) {
case (ast.item_const(_, _, _, ?id, _)) {
ret some[def_wrap](def_wrap_other(ast.def_const(id)));
}
2010-11-24 16:52:49 -08:00
case (ast.item_fn(_, _, _, ?id, _)) {
ret some[def_wrap](def_wrap_other(ast.def_fn(id)));
}
case (ast.item_mod(_, _, ?id)) {
ret some[def_wrap](def_wrap_mod(i));
}
2010-11-24 17:36:22 -08:00
case (ast.item_ty(_, _, _, ?id, _)) {
ret some[def_wrap](def_wrap_other(ast.def_ty(id)));
}
2010-12-01 10:19:20 -08:00
case (ast.item_tag(_, _, _, ?id)) {
ret some[def_wrap](def_wrap_other(ast.def_ty(id)));
2010-12-01 10:19:20 -08:00
}
2010-12-14 17:09:37 -08:00
case (ast.item_obj(_, _, _, ?id, _)) {
ret some[def_wrap](def_wrap_other(ast.def_obj(id)));
2010-12-14 17:09:37 -08:00
}
}
}
fn found_decl_stmt(@ast.stmt s) -> option.t[def_wrap] {
alt (s.node) {
case (ast.stmt_decl(?d)) {
alt (d.node) {
case (ast.decl_local(?loc)) {
auto t = ast.def_local(loc.id);
ret some[def_wrap](def_wrap_other(t));
}
case (ast.decl_item(?it)) {
ret found_def_item(it);
}
}
}
}
ret none[def_wrap];
}
fn found_def_view(&env e, option.t[import_map] index,
@ast.view_item i) -> option.t[def_wrap] {
alt (i.node) {
case (ast.view_item_use(_, _, ?id)) {
ret some[def_wrap](def_wrap_use(i));
}
case (ast.view_item_import(?idents,?d)) {
alt (index) {
case (some[import_map](?idx)) {
ret idx.find(d);
}
case (none[import_map]) {
ret find_final_def(e, i.span, idents);
}
}
}
}
fail;
}
fn check_mod(&env e, option.t[import_map] index, ast.ident i,
ast._mod m) -> option.t[def_wrap] {
alt (m.index.find(i)) {
2010-12-01 10:19:20 -08:00
case (some[ast.mod_index_entry](?ent)) {
alt (ent) {
case (ast.mie_view_item(?view_item)) {
ret found_def_view(e, index, view_item);
}
case (ast.mie_item(?item)) {
ret found_def_item(item);
2010-12-01 10:19:20 -08:00
}
case (ast.mie_tag_variant(?item, ?variant_idx)) {
alt (item.node) {
2010-12-01 10:19:20 -08:00
case (ast.item_tag(_, ?variants, _, ?tid)) {
auto vid = variants.(variant_idx).id;
auto t = ast.def_variant(tid, vid);
ret some[def_wrap](def_wrap_other(t));
2010-12-01 10:19:20 -08:00
}
case (_) {
log "tag item not actually a tag";
fail;
}
}
}
}
}
case (none[ast.mod_index_entry]) { /* fall through */ }
}
ret none[def_wrap];
}
fn in_scope(ast.ident i, env e, option.t[import_map] index,
&scope s) -> option.t[def_wrap] {
2010-10-18 14:37:30 -07:00
alt (s) {
2010-10-18 14:37:30 -07:00
case (scope_crate(?c)) {
ret check_mod(e, index, i, c.node.module);
2010-10-18 14:37:30 -07:00
}
2010-10-18 14:37:30 -07:00
case (scope_item(?it)) {
alt (it.node) {
2010-12-15 13:12:39 -08:00
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 some[def_wrap](def_wrap_other(t));
2010-10-18 14:37:30 -07:00
}
}
2010-12-15 13:12:39 -08:00
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](def_wrap_other(t));
2010-12-15 13:12:39 -08:00
}
}
2010-10-18 14:37:30 -07:00
}
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](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](def_wrap_other(t));
}
}
}
case (ast.item_mod(_, ?m, _)) {
ret check_mod(e, index, i, m);
2010-10-18 14:37:30 -07:00
}
case (_) { /* fall through */ }
2010-10-18 14:37:30 -07:00
}
}
case (scope_block(?b)) {
alt (b.node.index.find(i)) {
case (some[uint](?ix)) {
ret found_decl_stmt(b.node.stmts.(ix));
}
case (_) { /* fall through */ }
}
}
2010-12-10 18:08:32 -08:00
case (scope_arm(?a)) {
alt (a.index.find(i)) {
case (some[ast.def_id](?did)) {
auto t = ast.def_binding(did);
ret some[def_wrap](def_wrap_other(t));
2010-12-10 18:08:32 -08:00
}
case (_) { /* fall through */ }
}
}
2010-10-18 14:37:30 -07:00
}
ret none[def_wrap];
2010-10-18 14:37:30 -07:00
}
ret std.list.find[scope,def_wrap](e.scopes,
bind in_scope(i, e, index, _));
}
fn fold_pat_tag(&env e, &span sp, import_map index, ident i,
vec[@ast.pat] args, option.t[ast.variant_def] old_def,
ann a) -> @ast.pat {
auto new_def;
alt (unwrap_def(lookup_name(e, some(index), i))) {
case (some[def](?d)) {
alt (d) {
case (ast.def_variant(?did, ?vid)) {
new_def = some[ast.variant_def](tup(did, vid));
}
case (_) {
2010-12-31 14:43:43 -08:00
e.sess.span_err(sp, "not a tag variant: " + i);
new_def = none[ast.variant_def];
}
}
}
case (none[def]) {
new_def = none[ast.variant_def];
2010-12-31 14:43:43 -08:00
e.sess.span_err(sp, "unresolved name: " + i);
}
}
ret @fold.respan[ast.pat_](sp, ast.pat_tag(i, args, new_def, a));
}
fn fold_expr_name(&env e, &span sp, import_map index,
&ast.name n, &option.t[def] d, ann a) -> @ast.expr {
2010-11-22 13:22:23 -08:00
if (_vec.len[@ast.ty](n.node.types) > 0u) {
2010-12-12 16:37:31 -08:00
e.sess.unimpl("resolving name expr with ty params");
2010-11-22 13:22:23 -08:00
}
auto d_ = unwrap_def(lookup_name(e, some(index), n.node.ident));
alt (d_) {
case (some[def](_)) {
// log "resolved name " + n.node.ident;
2010-10-18 14:37:30 -07:00
}
case (none[def]) {
2010-12-31 14:43:43 -08:00
e.sess.span_err(sp, "unresolved name: " + n.node.ident);
2010-10-18 14:37:30 -07:00
}
}
ret @fold.respan[ast.expr_](sp, ast.expr_name(n, d_, a));
}
fn fold_view_item_import(&env e, &span sp,
import_map index,
vec[ident] is, ast.def_id id) -> @ast.view_item {
// Produce errors for invalid imports
auto len = _vec.len[ast.ident](is);
auto last_id = is.(len - 1u);
auto d = lookup_name(e, none[import_map], last_id);
alt (d) {
case (none[def_wrap]) {
e.sess.span_err(sp, "unresolved name: " + last_id);
}
case (some[def_wrap](?d2)) {
index.insert(id, d2);
}
}
ret @fold.respan[ast.view_item_](sp, ast.view_item_import(is, id));
}
fn fold_ty_path(&env e, &span sp, import_map index, ast.path p,
2010-11-22 13:22:23 -08:00
&option.t[def] d) -> @ast.ty {
let uint len = _vec.len[ast.name](p);
check (len != 0u);
if (len > 1u) {
2010-12-12 16:37:31 -08:00
e.sess.unimpl("resolving path ty with >1 component");
2010-11-22 13:22:23 -08:00
}
let ast.name n = p.(0);
if (_vec.len[@ast.ty](n.node.types) > 0u) {
2010-12-12 16:37:31 -08:00
e.sess.unimpl("resolving path ty with ty params");
2010-11-22 13:22:23 -08:00
}
auto d_ = unwrap_def(lookup_name(e, some(index), n.node.ident));
2010-11-22 13:22:23 -08:00
alt (d_) {
case (some[def](_)) {
// log "resolved name " + n.node.ident;
}
case (none[def]) {
2010-12-31 14:43:43 -08:00
e.sess.span_err(sp, "unresolved name: " + n.node.ident);
2010-11-22 13:22:23 -08:00
}
}
ret @fold.respan[ast.ty_](sp, ast.ty_path(p, d_));
}
2010-10-18 14:37:30 -07:00
fn update_env_for_crate(&env e, @ast.crate c) -> env {
ret rec(scopes = cons[scope](scope_crate(c), @e.scopes) with e);
2010-10-18 14:37:30 -07:00
}
fn update_env_for_item(&env e, @ast.item i) -> env {
ret rec(scopes = cons[scope](scope_item(i), @e.scopes) with e);
2010-10-18 14:37:30 -07:00
}
fn update_env_for_block(&env e, &ast.block b) -> env {
ret rec(scopes = cons[scope](scope_block(b), @e.scopes) with e);
2010-10-18 14:37:30 -07:00
}
2010-12-10 18:08:32 -08:00
fn update_env_for_arm(&env e, &ast.arm p) -> env {
ret rec(scopes = cons[scope](scope_arm(p), @e.scopes) with e);
}
fn resolve_crate(session.session sess, @ast.crate crate) -> @ast.crate {
let fold.ast_fold[env] fld = fold.new_identity_fold[env]();
auto import_index = new_def_hash[def_wrap]();
fld = @rec( fold_pat_tag = bind fold_pat_tag(_,_,import_index,_,_,_,_),
fold_expr_name = bind fold_expr_name(_,_,import_index,_,_,_),
fold_view_item_import
= bind fold_view_item_import(_,_,import_index,_,_),
fold_ty_path = bind fold_ty_path(_,_,import_index,_,_),
2010-10-18 14:37:30 -07:00
update_env_for_crate = bind update_env_for_crate(_,_),
update_env_for_item = bind update_env_for_item(_,_),
2010-12-10 18:08:32 -08:00
update_env_for_block = bind update_env_for_block(_,_),
update_env_for_arm = bind update_env_for_arm(_,_)
with *fld );
auto e = rec(scopes = nil[scope],
sess = sess);
ret fold.fold_crate[env](e, fld, crate);
}
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C ../.. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End: