More work on resolving names in rustc. Basic expr_name lookup working on items and args.
This commit is contained in:
parent
23a00fd092
commit
865bbae685
6 changed files with 123 additions and 93 deletions
|
|
@ -17,9 +17,9 @@ import front.ast.expr;
|
|||
import front.ast.stmt;
|
||||
import front.ast.block;
|
||||
import front.ast.item;
|
||||
import front.ast.slot;
|
||||
import front.ast.arg;
|
||||
import front.ast.decl;
|
||||
import front.ast.referent;
|
||||
import front.ast.def;
|
||||
|
||||
import std._vec;
|
||||
|
||||
|
|
@ -44,7 +44,7 @@ type ast_fold[ENV] =
|
|||
vec[tup(bool, @ty)] elts) -> @ty) fold_ty_tup,
|
||||
|
||||
(fn(&ENV e, &span sp, ast.path p,
|
||||
&option[referent] r) -> @ty) fold_ty_path,
|
||||
&option[def] d) -> @ty) fold_ty_path,
|
||||
|
||||
// Expr folds.
|
||||
(fn(&ENV e, &span sp,
|
||||
|
|
@ -87,7 +87,7 @@ type ast_fold[ENV] =
|
|||
|
||||
(fn(&ENV e, &span sp,
|
||||
&name n,
|
||||
&option[referent] r) -> @expr) fold_expr_name,
|
||||
&option[def] d) -> @expr) fold_expr_name,
|
||||
|
||||
// Decl folds.
|
||||
(fn(&ENV e, &span sp,
|
||||
|
|
@ -113,19 +113,19 @@ type ast_fold[ENV] =
|
|||
|
||||
// Item folds.
|
||||
(fn(&ENV e, &span sp,
|
||||
&ast._fn f, ast.item_id id) -> @item) fold_item_fn,
|
||||
&ast._fn f, ast.def_id id) -> @item) fold_item_fn,
|
||||
|
||||
(fn(&ENV e, &span sp,
|
||||
&ast._mod m) -> @item) fold_item_mod,
|
||||
&ast._mod m, ast.def_id id) -> @item) fold_item_mod,
|
||||
|
||||
(fn(&ENV e, &span sp,
|
||||
@ty t, ast.item_id id) -> @item) fold_item_ty,
|
||||
@ty t, ast.def_id id) -> @item) fold_item_ty,
|
||||
|
||||
// Additional nodes.
|
||||
(fn(&ENV e, &span sp,
|
||||
vec[@stmt] stmts) -> block) fold_block,
|
||||
|
||||
(fn(&ENV e, vec[ast.input] inputs,
|
||||
(fn(&ENV e, vec[arg] inputs,
|
||||
&ty output, block body) -> ast._fn) fold_fn,
|
||||
|
||||
(fn(&ENV e, &ast._mod m) -> ast._mod) fold_mod,
|
||||
|
|
@ -412,18 +412,17 @@ fn fold_block[ENV](&ENV env, ast_fold[ENV] fld, &block blk) -> block {
|
|||
ret respan(blk.span, stmts);
|
||||
}
|
||||
|
||||
fn fold_slot[ENV](&ENV env, ast_fold[ENV] fld, &slot s) -> slot {
|
||||
auto ty = fold_ty(env, fld, s.ty);
|
||||
ret rec(ty=ty, mode=s.mode, id=s.id);
|
||||
fn fold_arg[ENV](&ENV env, ast_fold[ENV] fld, &arg a) -> arg {
|
||||
auto ty = fold_ty(env, fld, a.ty);
|
||||
ret rec(ty=ty with a);
|
||||
}
|
||||
|
||||
|
||||
fn fold_fn[ENV](&ENV env, ast_fold[ENV] fld, &ast._fn f) -> ast._fn {
|
||||
|
||||
let vec[ast.input] inputs = vec();
|
||||
for (ast.input i in f.inputs) {
|
||||
inputs += rec(slot=fold_slot(env, fld, i.slot),
|
||||
ident=i.ident);
|
||||
let vec[ast.arg] inputs = vec();
|
||||
for (ast.arg a in f.inputs) {
|
||||
inputs += fold_arg(env, fld, a);
|
||||
}
|
||||
auto output = fold_ty[ENV](env, fld, @f.output);
|
||||
auto body = fold_block[ENV](env, fld, f.body);
|
||||
|
|
@ -446,9 +445,9 @@ fn fold_item[ENV](&ENV env, ast_fold[ENV] fld, @item i) -> @item {
|
|||
ret fld.fold_item_fn(env_, i.span, ff_, id);
|
||||
}
|
||||
|
||||
case (ast.item_mod(?mm)) {
|
||||
case (ast.item_mod(?mm, ?id)) {
|
||||
let ast._mod mm_ = fold_mod[ENV](env_, fld, mm);
|
||||
ret fld.fold_item_mod(env_, i.span, mm_);
|
||||
ret fld.fold_item_mod(env_, i.span, mm_, id);
|
||||
}
|
||||
|
||||
case (ast.item_ty(?ty, ?id)) {
|
||||
|
|
@ -537,8 +536,8 @@ fn identity_fold_ty_tup[ENV](&ENV env, &span sp, vec[tup(bool,@ty)] elts)
|
|||
}
|
||||
|
||||
fn identity_fold_ty_path[ENV](&ENV env, &span sp, ast.path p,
|
||||
&option[referent] r) -> @ty {
|
||||
ret @respan(sp, ast.ty_path(p, r));
|
||||
&option[def] d) -> @ty {
|
||||
ret @respan(sp, ast.ty_path(p, d));
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -604,8 +603,8 @@ fn identity_fold_expr_index[ENV](&ENV env, &span sp,
|
|||
}
|
||||
|
||||
fn identity_fold_expr_name[ENV](&ENV env, &span sp,
|
||||
&name n, &option[referent] r) -> @expr {
|
||||
ret @respan(sp, ast.expr_name(n, r));
|
||||
&name n, &option[def] d) -> @expr {
|
||||
ret @respan(sp, ast.expr_name(n, d));
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -646,16 +645,17 @@ fn identity_fold_stmt_expr[ENV](&ENV e, &span sp, @expr x) -> @stmt {
|
|||
// Item identities.
|
||||
|
||||
fn identity_fold_item_fn[ENV](&ENV e, &span sp, &ast._fn f,
|
||||
ast.item_id id) -> @item {
|
||||
ast.def_id id) -> @item {
|
||||
ret @respan(sp, ast.item_fn(f, id));
|
||||
}
|
||||
|
||||
fn identity_fold_item_mod[ENV](&ENV e, &span sp, &ast._mod m) -> @item {
|
||||
ret @respan(sp, ast.item_mod(m));
|
||||
fn identity_fold_item_mod[ENV](&ENV e, &span sp, &ast._mod m,
|
||||
ast.def_id id) -> @item {
|
||||
ret @respan(sp, ast.item_mod(m, id));
|
||||
}
|
||||
|
||||
fn identity_fold_item_ty[ENV](&ENV e, &span sp, @ty t,
|
||||
ast.item_id id) -> @item {
|
||||
ast.def_id id) -> @item {
|
||||
ret @respan(sp, ast.item_ty(t, id));
|
||||
}
|
||||
|
||||
|
|
@ -667,7 +667,7 @@ fn identity_fold_block[ENV](&ENV e, &span sp, vec[@stmt] stmts) -> block {
|
|||
}
|
||||
|
||||
fn identity_fold_fn[ENV](&ENV e,
|
||||
vec[ast.input] inputs,
|
||||
vec[arg] inputs,
|
||||
&ast.ty output,
|
||||
block body) -> ast._fn {
|
||||
ret rec(inputs=inputs, output=output, body=body);
|
||||
|
|
@ -760,7 +760,7 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] {
|
|||
fold_stmt_expr = bind identity_fold_stmt_expr[ENV](_,_,_),
|
||||
|
||||
fold_item_fn = bind identity_fold_item_fn[ENV](_,_,_,_),
|
||||
fold_item_mod = bind identity_fold_item_mod[ENV](_,_,_),
|
||||
fold_item_mod = bind identity_fold_item_mod[ENV](_,_,_,_),
|
||||
fold_item_ty = bind identity_fold_item_ty[ENV](_,_,_,_),
|
||||
|
||||
fold_block = bind identity_fold_block[ENV](_,_,_),
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
import front.ast;
|
||||
import front.ast.ident;
|
||||
import front.ast.def;
|
||||
import driver.session;
|
||||
import util.common.span;
|
||||
import std.map.hashmap;
|
||||
|
|
@ -18,67 +20,89 @@ tag scope {
|
|||
|
||||
type env = list[scope];
|
||||
|
||||
fn resolve_name(&env e, &span sp, ast.name_ n) -> ast.name {
|
||||
fn lookup_name(&env e, ast.ident i) -> option[def] {
|
||||
|
||||
log "resolving name " + n.ident;
|
||||
log "resolving name " + i;
|
||||
|
||||
fn in_scope(ast.ident i, &scope s) -> option[scope] {
|
||||
alt (s) {
|
||||
case (scope_crate(?c)) {
|
||||
if (c.node.module.contains_key(i)) {
|
||||
ret some[scope](s);
|
||||
fn check_mod(ast.ident i, ast._mod m) -> option[def] {
|
||||
alt (m.find(i)) {
|
||||
case (some[@ast.item](?it)) {
|
||||
alt (it.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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ret none[def];
|
||||
}
|
||||
|
||||
fn in_scope(ast.ident i, &scope s) -> option[def] {
|
||||
alt (s) {
|
||||
|
||||
case (scope_crate(?c)) {
|
||||
ret check_mod(i, c.node.module);
|
||||
}
|
||||
|
||||
case (scope_item(?it)) {
|
||||
alt (it.node) {
|
||||
case (ast.item_fn(?f, _)) {
|
||||
for (ast.input inp in f.inputs) {
|
||||
if (_str.eq(inp.ident, i)) {
|
||||
ret some[scope](s);
|
||||
for (ast.arg a in f.inputs) {
|
||||
if (_str.eq(a.ident, i)) {
|
||||
ret some[def](ast.def_arg(a.id));
|
||||
}
|
||||
}
|
||||
}
|
||||
case (ast.item_mod(?m)) {
|
||||
if (m.contains_key(i)) {
|
||||
ret some[scope](s);
|
||||
}
|
||||
case (ast.item_mod(?m, _)) {
|
||||
ret check_mod(i, m);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ret none[scope];
|
||||
ret none[def];
|
||||
}
|
||||
|
||||
alt (std.list.find[scope](e, bind in_scope(n.ident, _))) {
|
||||
case (some[scope](?s)) {
|
||||
log "resolved name " + n.ident;
|
||||
ret std.list.find[scope,def](e, bind in_scope(i, _));
|
||||
}
|
||||
|
||||
fn fold_expr_name(&env e, &span sp, &ast.name n,
|
||||
&option[def] d) -> @ast.expr {
|
||||
|
||||
auto d_ = lookup_name(e, n.node.ident);
|
||||
|
||||
alt (d_) {
|
||||
case (some[def](_)) {
|
||||
log "resolved name " + n.node.ident;
|
||||
}
|
||||
case (none[scope]) {
|
||||
log "unresolved name " + n.ident;
|
||||
case (none[def]) {
|
||||
log "unresolved name " + n.node.ident;
|
||||
}
|
||||
}
|
||||
|
||||
ret fold.respan[ast.name_](sp, n);
|
||||
ret @fold.respan[ast.expr_](sp, ast.expr_name(n, d_));
|
||||
}
|
||||
|
||||
fn update_env_for_crate(&env e, @ast.crate c) -> env {
|
||||
log "updating env with crate";
|
||||
ret cons[scope](scope_crate(c), @e);
|
||||
}
|
||||
|
||||
fn update_env_for_item(&env e, @ast.item i) -> env {
|
||||
log "updating env with item";
|
||||
ret cons[scope](scope_item(i), @e);
|
||||
}
|
||||
|
||||
fn update_env_for_block(&env e, ast.block b) -> env {
|
||||
log "updating env with block";
|
||||
ret cons[scope](scope_block(b), @e);
|
||||
}
|
||||
|
||||
fn resolve_crate(session.session sess, @ast.crate crate) -> @ast.crate {
|
||||
let fold.ast_fold[env] fld = fold.new_identity_fold[env]();
|
||||
fld = @rec( fold_name = bind resolve_name(_,_,_),
|
||||
fld = @rec( fold_expr_name = bind fold_expr_name(_,_,_,_),
|
||||
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(_,_)
|
||||
|
|
|
|||
|
|
@ -730,7 +730,7 @@ fn trans_item(@trans_ctxt cx, &str name, &ast.item item) {
|
|||
case (ast.item_fn(?f, _)) {
|
||||
trans_fn(sub_cx, f);
|
||||
}
|
||||
case (ast.item_mod(?m)) {
|
||||
case (ast.item_mod(?m, _)) {
|
||||
trans_mod(sub_cx, m);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue