Skip to content

Commit 358a1ae

Browse files
committed
Keep resolve data in external hash table, rather than embedded defs
One step closer to removing fold and having a single, immutable AST. Resolve still uses fold, because it has to detect and transform expr_field expressions. If we go through on our plan of moving to a different syntax for module dereferencing, the parser can spit out expr_field expressions, and resolve can move to walk. (I am truly sorry for the things I did in typestate_check.rs. I expect we'll want to change that to walk as well in the near future, at which point it should probably pass around a context record, which could hold the def_map.)
1 parent 2b36e40 commit 358a1ae

File tree

14 files changed

+516
-481
lines changed

14 files changed

+516
-481
lines changed

src/comp/driver/rustc.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,28 +86,30 @@ fn compile_input(session.session sess,
8686
str input, str output) {
8787
auto time_passes = sess.get_opts().time_passes;
8888
auto def = tup(ast.local_crate, 0);
89-
auto p = parser.new_parser(sess, env, def, input, 0u);
90-
auto crate = time[@ast.crate](time_passes, "parsing",
91-
bind parse_input(sess, p, input));
89+
auto p = parser.new_parser(sess, env, def, input, 0u, 0u);
90+
auto crate = time(time_passes, "parsing",
91+
bind parse_input(sess, p, input));
9292
if (sess.get_opts().output_type == Link.output_type_none) {ret;}
9393

94-
crate = time[@ast.crate](time_passes, "external crate reading",
95-
bind creader.read_crates(sess, crate));
96-
crate = time[@ast.crate](time_passes, "resolution",
97-
bind resolve.resolve_crate(sess, crate));
94+
crate = time(time_passes, "external crate reading",
95+
bind creader.read_crates(sess, crate));
96+
auto res = time(time_passes, "resolution",
97+
bind resolve.resolve_crate(sess, crate));
98+
crate = res._0;
99+
auto def_map = res._1;
98100
time[()](time_passes, "capture checking",
99-
bind capture.check_for_captures(sess, crate));
101+
bind capture.check_for_captures(sess, crate, def_map));
100102

101-
auto ty_cx = ty.mk_ctxt(sess);
103+
auto ty_cx = ty.mk_ctxt(sess, def_map);
102104
auto typeck_result =
103105
time[typeck.typecheck_result](time_passes, "typechecking",
104106
bind typeck.check_crate(ty_cx, crate));
105107
crate = typeck_result._0;
106108
auto type_cache = typeck_result._1;
107109

108110
if (sess.get_opts().run_typestate) {
109-
crate = time[@ast.crate](time_passes, "typestate checking",
110-
bind typestate_check.check_crate(crate));
111+
crate = time(time_passes, "typestate checking",
112+
bind typestate_check.check_crate(crate, def_map));
111113
}
112114

113115
auto llmod = time[llvm.ModuleRef](time_passes, "translation",
@@ -121,7 +123,7 @@ fn pretty_print_input(session.session sess,
121123
eval.env env,
122124
str input) {
123125
auto def = tup(ast.local_crate, 0);
124-
auto p = front.parser.new_parser(sess, env, def, input, 0u);
126+
auto p = front.parser.new_parser(sess, env, def, input, 0u, 0u);
125127
auto crate = front.parser.parse_crate_from_source_file(p);
126128
pretty.pprust.print_file(sess, crate.node.module, input, std.IO.stdout());
127129
}

src/comp/front/ast.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ tag def {
5656
def_native_fn(def_id);
5757
}
5858

59+
fn variant_def_ids(&def d) -> tup(def_id, def_id) {
60+
alt (d) {
61+
case (def_variant(?tag_id, ?var_id)) {
62+
ret tup(tag_id, var_id);
63+
}
64+
}
65+
}
66+
5967
fn def_id_of_def(def d) -> def_id {
6068
alt (d) {
6169
case (def_fn(?id)) { ret id; }
@@ -106,14 +114,12 @@ type block_ = rec(vec[@stmt] stmts,
106114
Option.t[@expr] expr,
107115
ann a); /* ann is only meaningful for the ts_ann field */
108116

109-
type variant_def = tup(def_id /* tag */, def_id /* variant */);
110-
111117
type pat = spanned[pat_];
112118
tag pat_ {
113119
pat_wild(ann);
114120
pat_bind(ident, def_id, ann);
115121
pat_lit(@lit, ann);
116-
pat_tag(path, vec[@pat], Option.t[variant_def], ann);
122+
pat_tag(path, vec[@pat], ann);
117123
}
118124

119125
tag mutability {
@@ -277,7 +283,7 @@ tag expr_ {
277283
expr_recv(@expr /* TODO: @expr|is_lval */, @expr, ann);
278284
expr_field(@expr, ident, ann);
279285
expr_index(@expr, @expr, ann);
280-
expr_path(path, Option.t[def], ann);
286+
expr_path(path, ann);
281287
expr_ext(path, vec[@expr], Option.t[str], @expr, ann);
282288
expr_fail(ann);
283289
expr_break(ann);
@@ -333,7 +339,7 @@ tag ty_ {
333339
ty_rec(vec[ty_field]);
334340
ty_fn(proto, vec[ty_arg], @ty);
335341
ty_obj(vec[ty_method]);
336-
ty_path(path, Option.t[def]);
342+
ty_path(path, ann);
337343
ty_type;
338344
ty_constr(@ty, vec[@constr]);
339345
}
@@ -463,7 +469,7 @@ fn is_constraint_arg(@expr e) -> bool {
463469
case (expr_lit(_,_)) {
464470
ret true;
465471
}
466-
case (expr_path(_, Option.some[def](def_local(_)), _)) {
472+
case (expr_path(_, _)) {
467473
ret true;
468474
}
469475
case (_) {

src/comp/front/eval.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ type ctx = @rec(parser p,
3434
eval_mode mode,
3535
mutable vec[str] deps,
3636
session.session sess,
37-
mutable uint chpos);
37+
mutable uint chpos,
38+
mutable uint next_ann);
3839

3940
fn mk_env() -> env {
4041
let env e = vec();
@@ -113,7 +114,7 @@ fn eval_lit(ctx cx, span sp, @ast.lit lit) -> val {
113114

114115
fn eval_expr(ctx cx, env e, @ast.expr x) -> val {
115116
alt (x.node) {
116-
case (ast.expr_path(?pth, _, _)) {
117+
case (ast.expr_path(?pth, _)) {
117118
if (Vec.len[ident](pth.node.idents) == 1u &&
118119
Vec.len[@ast.ty](pth.node.types) == 0u) {
119120
ret lookup(cx.sess, e, x.span, pth.node.idents.(0));
@@ -383,12 +384,14 @@ fn eval_crate_directive(ctx cx,
383384
}
384385

385386
auto start_id = cx.p.next_def_id();
386-
auto p0 = new_parser(cx.sess, e, start_id, full_path, cx.chpos);
387+
auto p0 = new_parser(cx.sess, e, start_id, full_path, cx.chpos,
388+
cx.next_ann);
387389
auto m0 = parse_mod_items(p0, token.EOF);
388390
auto next_id = p0.next_def_id();
389391
// Thread defids and chpos through the parsers
390392
cx.p.set_def(next_id._1);
391393
cx.chpos = p0.get_chpos();
394+
cx.next_ann = p0.next_ann_num();
392395
auto im = ast.item_mod(id, m0, next_id);
393396
auto i = @spanned(cdir.span.lo, cdir.span.hi, im);
394397
Vec.push[@ast.item](items, i);

src/comp/front/extfmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ fn pieces_to_expr(parser p, vec[piece] pieces, vec[@ast.expr] args)
121121
let vec[@ast.ty] types = vec();
122122
auto path = rec(idents=idents, types=types);
123123
auto sp_path = rec(node=path, span=sp);
124-
auto pathexpr = ast.expr_path(sp_path, none[ast.def], p.get_ann());
124+
auto pathexpr = ast.expr_path(sp_path, p.get_ann());
125125
auto sp_pathexpr = @rec(node=pathexpr, span=sp);
126126
ret sp_pathexpr;
127127
}

src/comp/front/parser.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ state type parser =
4343
fn get_filemap() -> codemap.filemap;
4444
fn get_chpos() -> uint;
4545
fn get_ann() -> ast.ann;
46+
fn next_ann_num() -> uint;
4647
};
4748

4849
fn new_parser(session.session sess,
4950
eval.env env,
5051
ast.def_id initial_def,
51-
str path, uint pos) -> parser {
52+
str path, uint pos, uint next_ann) -> parser {
5253
state obj stdio_parser(session.session sess,
5354
eval.env env,
5455
file_type ftype,
@@ -134,6 +135,9 @@ fn new_parser(session.session sess,
134135
next_ann_var += 1u;
135136
ret rv;
136137
}
138+
fn next_ann_num() -> uint {
139+
ret next_ann_var;
140+
}
137141
}
138142
auto ftype = SOURCE_FILE;
139143
if (Str.ends_with(path, ".rc")) {
@@ -148,7 +152,7 @@ fn new_parser(session.session sess,
148152
auto npos = rdr.get_chpos();
149153
ret stdio_parser(sess, env, ftype, lexer.next_token(rdr),
150154
npos, npos, initial_def._1, UNRESTRICTED, initial_def._0,
151-
rdr, prec_table(), 0u);
155+
rdr, prec_table(), next_ann);
152156
}
153157

154158
fn unexpected(parser p, token.token t) {
@@ -474,7 +478,7 @@ fn parse_ty(parser p) -> @ast.ty {
474478

475479
case (token.IDENT(_)) {
476480
auto path = parse_path(p, GREEDY);
477-
t = ast.ty_path(path, none[ast.def]);
481+
t = ast.ty_path(path, p.get_ann());
478482
hi = path.span.hi;
479483
}
480484

@@ -693,7 +697,7 @@ fn parse_bottom_expr(parser p) -> @ast.expr {
693697
case (token.IDENT(_)) {
694698
auto pth = parse_path(p, MINIMAL);
695699
hi = pth.span.hi;
696-
ex = ast.expr_path(pth, none[ast.def], p.get_ann());
700+
ex = ast.expr_path(pth, p.get_ann());
697701
}
698702

699703
case (token.LPAREN) {
@@ -985,15 +989,15 @@ fn extend_expr_by_ident(parser p, uint lo, uint hi,
985989
@ast.expr e, ast.ident i) -> @ast.expr {
986990
auto e_ = e.node;
987991
alt (e.node) {
988-
case (ast.expr_path(?pth, ?def, ?ann)) {
992+
case (ast.expr_path(?pth, ?ann)) {
989993
if (Vec.len[@ast.ty](pth.node.types) == 0u) {
990994
auto idents_ = pth.node.idents;
991995
idents_ += vec(i);
992996
auto tys = parse_ty_args(p, hi);
993997
auto pth_ = spanned(pth.span.lo, tys.span.hi,
994998
rec(idents=idents_,
995999
types=tys.node));
996-
e_ = ast.expr_path(pth_, def, ann);
1000+
e_ = ast.expr_path(pth_, ann);
9971001
ret @spanned(pth_.span.lo, pth_.span.hi, e_);
9981002
} else {
9991003
e_ = ast.expr_field(e, i, ann);
@@ -1525,8 +1529,7 @@ fn parse_pat(parser p) -> @ast.pat {
15251529
case (_) { args = vec(); }
15261530
}
15271531

1528-
pat = ast.pat_tag(tag_path, args, none[ast.variant_def],
1529-
p.get_ann());
1532+
pat = ast.pat_tag(tag_path, args, p.get_ann());
15301533
}
15311534
case (_) {
15321535
auto lit = parse_lit(p);
@@ -1666,7 +1669,7 @@ fn stmt_ends_with_semi(@ast.stmt stmt) -> bool {
16661669
case (ast.expr_recv(_,_,_)) { ret true; }
16671670
case (ast.expr_field(_,_,_)) { ret true; }
16681671
case (ast.expr_index(_,_,_)) { ret true; }
1669-
case (ast.expr_path(_,_,_)) { ret true; }
1672+
case (ast.expr_path(_,_)) { ret true; }
16701673
case (ast.expr_fail(_)) { ret true; }
16711674
case (ast.expr_break(_)) { ret true; }
16721675
case (ast.expr_cont(_)) { ret true; }
@@ -2496,7 +2499,8 @@ fn parse_crate_from_crate_file(parser p) -> @ast.crate {
24962499
mode=eval.mode_parse,
24972500
mutable deps = deps,
24982501
sess=p.get_session(),
2499-
mutable chpos=p.get_chpos());
2502+
mutable chpos=p.get_chpos(),
2503+
mutable next_ann=p.next_ann_num());
25002504
auto m = eval.eval_crate_directives_to_mod(cx, p.get_env(),
25012505
cdirs, prefix);
25022506
auto hi = p.get_hi_pos();

src/comp/middle/capture.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import std.Option.none;
77
import std.Int;
88
import std.Vec;
99
import util.common;
10+
import resolve.def_map;
1011

1112
type fn_id_of_local = std.Map.hashmap[ast.def_id, ast.def_id];
1213
type env = rec(mutable vec[ast.def_id] current_context, // fn or obj
14+
def_map def_map,
1315
fn_id_of_local idmap,
1416
session.session sess);
1517

@@ -22,7 +24,7 @@ fn enter_item(@env e, &@ast.item i) {
2224
case (ast.item_fn(?name, _, _, ?id, _)) {
2325
Vec.push(e.current_context, id);
2426
}
25-
case (ast.item_obj(_, _, _, ?ids, _)) {
27+
case (ast.item_obj(?name, _, _, ?ids, _)) {
2628
Vec.push(e.current_context, ids.ty);
2729
}
2830
case (_) {}
@@ -59,15 +61,14 @@ fn walk_expr(@env e, &@ast.expr x) {
5961
case (_) { }
6062
}
6163
}
62-
case (ast.expr_path(_, ?def, _)) {
64+
case (ast.expr_path(?pt, ?ann)) {
6365
auto local_id;
64-
alt (Option.get(def)) {
66+
alt (e.def_map.get(ast.ann_tag(ann))) {
6567
case (ast.def_local(?id)) { local_id = id; }
6668
case (_) { ret; }
6769
}
68-
69-
auto df = ast.def_id_of_def(Option.get(def));
70-
auto def_context = Option.get(e.idmap.find(df));
70+
auto df = ast.def_id_of_def(e.def_map.get(ast.ann_tag(ann)));
71+
auto def_context = e.idmap.get(df);
7172

7273
if (current_context(*e) != def_context) {
7374
e.sess.span_err(x.span,
@@ -94,9 +95,10 @@ fn walk_block(@env e, &ast.block b) {
9495
}
9596
}
9697

97-
fn check_for_captures(session.session sess, @ast.crate crate) {
98+
fn check_for_captures(session.session sess, @ast.crate crate, def_map dm) {
9899
let vec[ast.def_id] curctx = vec();
99100
auto env = @rec(mutable current_context = curctx,
101+
def_map = dm,
100102
idmap = common.new_def_hash[ast.def_id](),
101103
sess = sess);
102104
auto visitor = rec(visit_item_pre = bind enter_item(env, _),

0 commit comments

Comments
 (0)