about summary refs log tree commit diff
path: root/src/comp
diff options
context:
space:
mode:
authorGraydon Hoare <graydon@mozilla.com>2010-10-18 16:15:25 -0700
committerGraydon Hoare <graydon@mozilla.com>2010-10-18 16:15:25 -0700
commit865bbae685fbf9bd1583a3f1715dd8093c0cbda2 (patch)
tree76ccfd49924368ddebaabe535ec02e9b654f7247 /src/comp
parent23a00fd09211e83be2c21ca944f5f6c11b6c665b (diff)
downloadrust-865bbae685fbf9bd1583a3f1715dd8093c0cbda2.tar.gz
rust-865bbae685fbf9bd1583a3f1715dd8093c0cbda2.zip
More work on resolving names in rustc. Basic expr_name lookup working on items and args.
Diffstat (limited to 'src/comp')
-rw-r--r--src/comp/driver/rustc.rs2
-rw-r--r--src/comp/front/ast.rs41
-rw-r--r--src/comp/front/parser.rs45
-rw-r--r--src/comp/middle/fold.rs54
-rw-r--r--src/comp/middle/resolve.rs72
-rw-r--r--src/comp/middle/trans.rs2
6 files changed, 123 insertions, 93 deletions
diff --git a/src/comp/driver/rustc.rs b/src/comp/driver/rustc.rs
index 43a45c48d53..69030f3c808 100644
--- a/src/comp/driver/rustc.rs
+++ b/src/comp/driver/rustc.rs
@@ -16,7 +16,7 @@ io fn main(vec[str] args) {
   auto sess = session.session();
   for (str filename in args) {
       if (i > 0) {
-          auto p = parser.new_parser(sess, filename);
+          auto p = parser.new_parser(sess, 0, filename);
           auto crate = parser.parse_crate(p);
           crate = resolve.resolve_crate(sess, crate);
           trans.trans_crate(sess, crate);
diff --git a/src/comp/front/ast.rs b/src/comp/front/ast.rs
index a33373f4305..04c64bea0b1 100644
--- a/src/comp/front/ast.rs
+++ b/src/comp/front/ast.rs
@@ -12,20 +12,17 @@ type name = spanned[name_];
 type path = vec[name];
 
 type crate_num = int;
-type slot_num = int;
-type item_num = int;
-
-tag slot_id {
-    id_slot(crate_num, slot_num);
-}
-
-tag item_id {
-    id_item(crate_num, slot_num);
-}
-
-tag referent {
-    ref_slot(slot_id);
-    ref_item(item_id);
+type def_num = int;
+type def_id = tup(crate_num, def_num);
+
+tag def {
+    def_fn(def_id);
+    def_mod(def_id);
+    def_const(def_id);
+    def_arg(def_id);
+    def_local(def_id);
+    def_ty(def_id);
+    def_ty_arg(def_id);
 }
 
 type crate = spanned[crate_];
@@ -93,7 +90,7 @@ tag expr_ {
     expr_assign(@expr /* TODO: @expr : is_lval(@expr) */, @expr);
     expr_field(@expr, ident);
     expr_index(@expr, @expr);
-    expr_name(name, option[referent]);
+    expr_name(name, option[def]);
 }
 
 type lit = spanned[lit_];
@@ -118,7 +115,7 @@ tag ty_ {
     ty_box(@ty);
     ty_vec(@ty);
     ty_tup(vec[tup(bool /* mutability */, @ty)]);
-    ty_path(path, option[referent]);
+    ty_path(path, option[def]);
 }
 
 tag mode {
@@ -126,10 +123,8 @@ tag mode {
     alias;
 }
 
-type slot = rec(@ty ty, mode mode, option[slot_id] id);
-type input = rec(slot slot, ident ident);
-
-type _fn = rec(vec[input] inputs,
+type arg = rec(mode mode, @ty ty, ident ident, def_id id);
+type _fn = rec(vec[arg] inputs,
                ty output,
                block body);
 
@@ -137,9 +132,9 @@ type _mod = hashmap[ident,@item];
 
 type item = spanned[item_];
 tag item_ {
-    item_fn(_fn, item_id);
-    item_mod(_mod);
-    item_ty(@ty, item_id);
+    item_fn(_fn, def_id);
+    item_mod(_mod, def_id);
+    item_ty(@ty, def_id);
 }
 
 
diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs
index 9393a02f07c..1d07299b9a8 100644
--- a/src/comp/front/parser.rs
+++ b/src/comp/front/parser.rs
@@ -15,13 +15,17 @@ state type parser =
           io fn err(str s);
           fn get_session() -> session.session;
           fn get_span() -> common.span;
+          fn next_def_id() -> ast.def_id;
     };
 
-io fn new_parser(session.session sess, str path) -> parser {
+io fn new_parser(session.session sess,
+                 ast.crate_num crate, str path) -> parser {
     state obj stdio_parser(session.session sess,
                            mutable token.token tok,
                            mutable common.pos lo,
                            mutable common.pos hi,
+                           mutable ast.def_num def,
+                           ast.crate_num crate,
                            lexer.reader rdr)
         {
             fn peek() -> token.token {
@@ -49,11 +53,17 @@ io fn new_parser(session.session sess, str path) -> parser {
                 ret rec(filename = rdr.get_filename(),
                         lo = lo, hi = hi);
             }
+
+            fn next_def_id() -> ast.def_id {
+                def += 1;
+                ret tup(crate, def);
+            }
         }
     auto srdr = _io.new_stdio_reader(path);
     auto rdr = lexer.new_reader(srdr, path);
     auto npos = rdr.get_curr_pos();
-    ret stdio_parser(sess, lexer.next_token(rdr), npos, npos, rdr);
+    ret stdio_parser(sess, lexer.next_token(rdr),
+                     npos, npos, 0, crate, rdr);
 }
 
 io fn expect(parser p, token.token t) {
@@ -132,14 +142,15 @@ io fn parse_ty(parser p) -> @ast.ty {
     ret @spanned(lo, lo, t);
 }
 
-io fn parse_slot(parser p) -> ast.slot {
+io fn parse_arg(parser p) -> ast.arg {
     let ast.mode m = ast.val;
     if (p.peek() == token.BINOP(token.AND)) {
         m = ast.alias;
         p.bump();
     }
     let @ast.ty t = parse_ty(p);
-    ret rec(ty=t, mode=m, id=none[ast.slot_id]);
+    let ast.ident i = parse_ident(p);
+    ret rec(mode=m, ty=t, ident=i, id=p.next_def_id());
 }
 
 io fn parse_seq[T](token.token bra,
@@ -249,6 +260,13 @@ io fn parse_bottom_expr(parser p) -> @ast.expr {
     let ast.expr_ ex = ast.expr_lit(@spanned(lo, lo, ast.lit_nil));
 
     alt (p.peek()) {
+
+        case (token.IDENT(?i)) {
+            auto n = parse_name(p, i);
+            hi = n.span;
+            ex = ast.expr_name(n, none[ast.def]);
+        }
+
         case (token.LPAREN) {
             p.bump();
             auto e = parse_expr(p);
@@ -665,22 +683,15 @@ io fn parse_block(parser p) -> ast.block {
                              f, p);
 }
 
-io fn parse_slot_ident_pair(parser p) ->
-    rec(ast.slot slot, ast.ident ident) {
-    auto s = parse_slot(p);
-    auto i = parse_ident(p);
-    ret rec(slot=s, ident=i);
-}
-
 io fn parse_fn(parser p) -> tup(ast.ident, @ast.item) {
     auto lo = p.get_span();
     expect(p, token.FN);
     auto id = parse_ident(p);
-    auto pf = parse_slot_ident_pair;
-    let util.common.spanned[vec[rec(ast.slot slot, ast.ident ident)]] inputs =
-        // FIXME: passing parse_slot_ident_pair as an lval doesn't work at the
+    auto pf = parse_arg;
+    let util.common.spanned[vec[ast.arg]] inputs =
+        // FIXME: passing parse_arg as an lval doesn't work at the
         // moment.
-        parse_seq[rec(ast.slot slot, ast.ident ident)]
+        parse_seq[ast.arg]
         (token.LPAREN,
          token.RPAREN,
          some(token.COMMA),
@@ -701,7 +712,7 @@ io fn parse_fn(parser p) -> tup(ast.ident, @ast.item) {
                         body = body);
 
     let @ast.item i = @spanned(lo, body.span,
-                               ast.item_fn(f, ast.id_item(0,0)));
+                               ast.item_fn(f, p.next_def_id()));
     ret tup(id, i);
 }
 
@@ -717,7 +728,7 @@ io fn parse_mod(parser p) -> tup(ast.ident, @ast.item) {
     }
     auto hi = p.get_span();
     expect(p, token.RBRACE);
-    ret tup(id, @spanned(lo, hi, ast.item_mod(m)));
+    ret tup(id, @spanned(lo, hi, ast.item_mod(m, p.next_def_id())));
 }
 
 io fn parse_item(parser p) -> tup(ast.ident, @ast.item) {
diff --git a/src/comp/middle/fold.rs b/src/comp/middle/fold.rs
index fa14e1e3208..53614754bb9 100644
--- a/src/comp/middle/fold.rs
+++ b/src/comp/middle/fold.rs
@@ -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](_,_,_),
diff --git a/src/comp/middle/resolve.rs b/src/comp/middle/resolve.rs
index 44e312ba823..ded63bb5fcb 100644
--- a/src/comp/middle/resolve.rs
+++ b/src/comp/middle/resolve.rs
@@ -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] {
+    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)) {
-                if (c.node.module.contains_key(i)) {
-                    ret some[scope](s);
-                }
+                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(_,_)
diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs
index b43dff0ea5a..a8bb23acde8 100644
--- a/src/comp/middle/trans.rs
+++ b/src/comp/middle/trans.rs
@@ -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);
         }
     }