2010-10-06 20:36:28 -05:00
|
|
|
import front.ast;
|
2010-10-18 18:15:25 -05:00
|
|
|
import front.ast.ident;
|
|
|
|
import front.ast.def;
|
2010-10-06 20:36:28 -05:00
|
|
|
import driver.session;
|
2010-10-07 20:34:22 -05:00
|
|
|
import util.common.span;
|
2010-10-18 16:37:30 -05:00
|
|
|
import std.map.hashmap;
|
|
|
|
import std.list.list;
|
|
|
|
import std.list.nil;
|
|
|
|
import std.list.cons;
|
|
|
|
import std.util.option;
|
|
|
|
import std.util.some;
|
|
|
|
import std.util.none;
|
|
|
|
import std._str;
|
2010-10-06 20:36:28 -05:00
|
|
|
|
2010-10-18 16:37:30 -05:00
|
|
|
tag scope {
|
|
|
|
scope_crate(@ast.crate);
|
|
|
|
scope_item(@ast.item);
|
|
|
|
scope_block(ast.block);
|
|
|
|
}
|
|
|
|
|
2010-10-19 16:54:10 -05:00
|
|
|
type env = rec(list[scope] scopes,
|
|
|
|
session.session sess);
|
2010-10-07 20:34:22 -05:00
|
|
|
|
2010-10-18 18:15:25 -05:00
|
|
|
fn lookup_name(&env e, ast.ident i) -> option[def] {
|
2010-10-18 16:37:30 -05:00
|
|
|
|
2010-10-18 18:15:25 -05:00
|
|
|
log "resolving name " + i;
|
2010-10-18 16:37:30 -05:00
|
|
|
|
2010-10-18 20:19:16 -05:00
|
|
|
fn found_def_item(@ast.item i) -> option[def] {
|
|
|
|
alt (i.node) {
|
|
|
|
case (ast.item_fn(_, _, ?id)) {
|
|
|
|
ret some[def](ast.def_fn(id));
|
|
|
|
}
|
|
|
|
case (ast.item_mod(_, _, ?id)) {
|
|
|
|
ret some[def](ast.def_mod(id));
|
|
|
|
}
|
|
|
|
case (ast.item_ty(_, _, ?id)) {
|
|
|
|
ret some[def](ast.def_ty(id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn found_decl_stmt(@ast.stmt s) -> option[def] {
|
|
|
|
alt (s.node) {
|
|
|
|
case (ast.stmt_decl(?d)) {
|
|
|
|
alt (d.node) {
|
|
|
|
case (ast.decl_local(?loc)) {
|
|
|
|
ret some[def](ast.def_local(loc.id));
|
2010-10-18 18:15:25 -05:00
|
|
|
}
|
2010-10-18 20:19:16 -05:00
|
|
|
case (ast.decl_item(?it)) {
|
|
|
|
ret found_def_item(it);
|
2010-10-18 18:15:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret none[def];
|
|
|
|
}
|
|
|
|
|
2010-10-18 20:19:16 -05:00
|
|
|
fn check_mod(ast.ident i, ast._mod m) -> option[def] {
|
|
|
|
alt (m.index.find(i)) {
|
|
|
|
case (some[uint](?ix)) {
|
|
|
|
ret found_def_item(m.items.(ix));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret none[def];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-18 18:15:25 -05:00
|
|
|
fn in_scope(ast.ident i, &scope s) -> option[def] {
|
2010-10-18 16:37:30 -05:00
|
|
|
alt (s) {
|
2010-10-18 18:15:25 -05:00
|
|
|
|
2010-10-18 16:37:30 -05:00
|
|
|
case (scope_crate(?c)) {
|
2010-10-18 18:15:25 -05:00
|
|
|
ret check_mod(i, c.node.module);
|
2010-10-18 16:37:30 -05:00
|
|
|
}
|
2010-10-18 18:15:25 -05:00
|
|
|
|
2010-10-18 16:37:30 -05:00
|
|
|
case (scope_item(?it)) {
|
|
|
|
alt (it.node) {
|
2010-10-18 20:19:16 -05:00
|
|
|
case (ast.item_fn(_, ?f, _)) {
|
2010-10-18 18:15:25 -05:00
|
|
|
for (ast.arg a in f.inputs) {
|
|
|
|
if (_str.eq(a.ident, i)) {
|
|
|
|
ret some[def](ast.def_arg(a.id));
|
2010-10-18 16:37:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-18 20:19:16 -05:00
|
|
|
case (ast.item_mod(_, ?m, _)) {
|
2010-10-18 18:15:25 -05:00
|
|
|
ret check_mod(i, m);
|
2010-10-18 16:37:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-18 20:19:16 -05:00
|
|
|
|
|
|
|
case (scope_block(?b)) {
|
|
|
|
alt (b.node.index.find(i)) {
|
|
|
|
case (some[uint](?ix)) {
|
|
|
|
ret found_decl_stmt(b.node.stmts.(ix));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-18 16:37:30 -05:00
|
|
|
}
|
2010-10-18 18:15:25 -05:00
|
|
|
ret none[def];
|
2010-10-18 16:37:30 -05:00
|
|
|
}
|
|
|
|
|
2010-10-19 16:54:10 -05:00
|
|
|
ret std.list.find[scope,def](e.scopes, bind in_scope(i, _));
|
2010-10-18 18:15:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_expr_name(&env e, &span sp, &ast.name n,
|
2010-10-19 15:28:43 -05:00
|
|
|
&option[def] d, option[@ast.ty] t) -> @ast.expr {
|
2010-10-18 18:15:25 -05:00
|
|
|
|
|
|
|
auto d_ = lookup_name(e, n.node.ident);
|
|
|
|
|
|
|
|
alt (d_) {
|
|
|
|
case (some[def](_)) {
|
|
|
|
log "resolved name " + n.node.ident;
|
2010-10-18 16:37:30 -05:00
|
|
|
}
|
2010-10-18 18:15:25 -05:00
|
|
|
case (none[def]) {
|
2010-10-19 16:54:10 -05:00
|
|
|
e.sess.err("unresolved name: " + n.node.ident);
|
2010-10-18 16:37:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-19 15:28:43 -05:00
|
|
|
ret @fold.respan[ast.expr_](sp, ast.expr_name(n, d_, t));
|
2010-10-07 20:34:22 -05:00
|
|
|
}
|
|
|
|
|
2010-10-18 16:37:30 -05:00
|
|
|
fn update_env_for_crate(&env e, @ast.crate c) -> env {
|
2010-10-19 16:54:10 -05:00
|
|
|
ret rec(scopes = cons[scope](scope_crate(c), @e.scopes) with e);
|
2010-10-18 16:37:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn update_env_for_item(&env e, @ast.item i) -> env {
|
2010-10-19 16:54:10 -05:00
|
|
|
ret rec(scopes = cons[scope](scope_item(i), @e.scopes) with e);
|
2010-10-18 16:37:30 -05:00
|
|
|
}
|
|
|
|
|
2010-10-18 20:19:16 -05:00
|
|
|
fn update_env_for_block(&env e, &ast.block b) -> env {
|
2010-10-19 16:54:10 -05:00
|
|
|
ret rec(scopes = cons[scope](scope_block(b), @e.scopes) with e);
|
2010-10-18 16:37:30 -05:00
|
|
|
}
|
|
|
|
|
2010-10-07 20:34:22 -05:00
|
|
|
fn resolve_crate(session.session sess, @ast.crate crate) -> @ast.crate {
|
2010-10-19 16:54:10 -05:00
|
|
|
|
2010-10-07 20:34:22 -05:00
|
|
|
let fold.ast_fold[env] fld = fold.new_identity_fold[env]();
|
2010-10-19 16:54:10 -05:00
|
|
|
|
2010-10-19 15:28:43 -05:00
|
|
|
fld = @rec( fold_expr_name = bind fold_expr_name(_,_,_,_,_),
|
2010-10-18 16:37:30 -05:00
|
|
|
update_env_for_crate = bind update_env_for_crate(_,_),
|
|
|
|
update_env_for_item = bind update_env_for_item(_,_),
|
|
|
|
update_env_for_block = bind update_env_for_block(_,_)
|
2010-10-07 20:34:22 -05:00
|
|
|
with *fld );
|
2010-10-19 16:54:10 -05:00
|
|
|
|
|
|
|
auto e = rec(scopes = nil[scope],
|
|
|
|
sess = sess);
|
|
|
|
|
|
|
|
ret fold.fold_crate[env](e, fld, crate);
|
2010-10-07 20:34:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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:
|