Skip to content

Commit c05bf7e

Browse files
committed
---
yaml --- r: 2503 b: refs/heads/master c: 89490e4 h: refs/heads/master i: 2501: bf7dc14 2499: 6a8816b 2495: b39f66b v: v3
1 parent b2d2461 commit c05bf7e

File tree

3 files changed

+165
-105
lines changed

3 files changed

+165
-105
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 7082bf4478d3c4dab5a46bf2df18b57a496023f4
2+
refs/heads/master: 89490e416b9eff7d22cbea235475db1cba581ff6

trunk/src/comp/driver/rustc.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,8 @@ fn compile_input(session::session sess,
9292

9393
crate = time(time_passes, "external crate reading",
9494
bind creader::read_crates(sess, crate));
95-
auto res = time(time_passes, "resolution",
96-
bind resolve::resolve_crate(sess, crate));
97-
crate = res._0;
98-
auto def_map = res._1;
95+
auto def_map = time(time_passes, "resolution",
96+
bind resolve::resolve_crate(sess, crate));
9997
time[()](time_passes, "capture checking",
10098
bind capture::check_for_captures(sess, crate, def_map));
10199

trunk/src/comp/middle/resolve.rs

Lines changed: 162 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ tag namespace {
9393
ns_type;
9494
}
9595

96-
fn resolve_crate(session sess, @ast::crate crate)
97-
-> tup(@ast::crate, def_map) {
96+
fn resolve_crate(session sess, @ast::crate crate) -> def_map {
9897
auto e = @rec(def_map = new_uint_hash[def](),
9998
imports = new_int_hash[import_state](),
10099
mod_map = new_int_hash[@indexed_mod](),
@@ -104,7 +103,8 @@ fn resolve_crate(session sess, @ast::crate crate)
104103
sess = sess);
105104
map_crate(e, *crate);
106105
resolve_imports(*e);
107-
ret tup(resolve_names(e, *crate), e.def_map);
106+
resolve_names(e, *crate);
107+
ret e.def_map;
108108
}
109109

110110
// Locate all modules and imports and index them, so that the next passes can
@@ -115,24 +115,24 @@ fn map_crate(&@env e, &ast::crate c) {
115115
auto v = rec(visit_crate_pre = bind push_env_for_crate(cell, _),
116116
visit_crate_post = bind pop_env_for_crate(cell, _),
117117
visit_view_item_pre = bind visit_view_item(e, cell, _),
118-
visit_item_pre = bind push_env_for_item_map_mod(e, cell, _),
118+
visit_item_pre = bind visit_item(e, cell, _),
119119
visit_item_post = bind pop_env_for_item(cell, _)
120120
with walk::default_visitor());
121121
// Register the top-level mod
122122
e.mod_map.insert(-1, @rec(m=c.node.module,
123123
index=index_mod(c.node.module)));
124124
walk::walk_crate(v, c);
125125

126-
// Helpers for this pass.
127-
fn push_env_for_crate(@mutable list[scope] sc, &ast::crate c) {
128-
*sc = cons[scope](scope_crate(@c), @*sc);
129-
}
130-
fn pop_env_for_crate(@mutable list[scope] sc, &ast::crate c) {
131-
*sc = std::list::cdr(*sc);
126+
fn visit_view_item(@env e, @mutable list[scope] sc, &@ast::view_item i) {
127+
alt (i.node) {
128+
case (ast::view_item_import(_, ?ids, ?defid)) {
129+
e.imports.insert(defid._1, todo(i, *sc));
130+
}
131+
case (_) {}
132+
}
132133
}
133-
fn push_env_for_item_map_mod(@env e, @mutable list[scope] sc,
134-
&@ast::item i) {
135-
*sc = cons[scope](scope_item(i), @*sc);
134+
fn visit_item(@env e, @mutable list[scope] sc, &@ast::item i) {
135+
push_env_for_item(sc, i);
136136
alt (i.node) {
137137
case (ast::item_mod(_, ?md, ?defid)) {
138138
auto index = index_mod(md);
@@ -145,17 +145,6 @@ fn map_crate(&@env e, &ast::crate c) {
145145
case (_) {}
146146
}
147147
}
148-
fn pop_env_for_item(@mutable list[scope] sc, &@ast::item i) {
149-
*sc = std::list::cdr(*sc);
150-
}
151-
fn visit_view_item(@env e, @mutable list[scope] sc, &@ast::view_item i) {
152-
alt (i.node) {
153-
case (ast::view_item_import(_, ?ids, ?defid)) {
154-
e.imports.insert(defid._1, todo(i, *sc));
155-
}
156-
case (_) {}
157-
}
158-
}
159148
}
160149

161150
fn resolve_imports(&env e) {
@@ -169,68 +158,151 @@ fn resolve_imports(&env e) {
169158
}
170159
}
171160

172-
// FIXME this should use walk (will need to add walk_arm)
173-
fn resolve_names(&@env e, &ast::crate c) -> @ast::crate {
174-
auto fld = @rec(fold_pat_tag = bind fold_pat_tag(e,_,_,_,_,_),
175-
fold_expr_path = bind fold_expr_path(e,_,_,_,_),
176-
fold_ty_path = bind fold_ty_path(e,_,_,_,_),
177-
update_env_for_crate = bind update_env_for_crate(_,_),
178-
update_env_for_item = bind update_env_for_item(_,_),
179-
update_env_for_native_item =
180-
bind update_env_for_native_item(_,_),
181-
update_env_for_block = bind update_env_for_block(_,_),
182-
update_env_for_arm = bind update_env_for_arm(_,_),
183-
update_env_for_expr = bind update_env_for_expr(_,_)
184-
with *fold::new_identity_fold[list[scope]]());
185-
ret fold::fold_crate(nil[scope], fld, @c);
186-
187-
// Helpers for this pass
161+
fn resolve_names(&@env e, &ast::crate c) {
162+
auto cell = @mutable nil[scope];
163+
auto v = rec(visit_crate_pre = bind push_env_for_crate(cell, _),
164+
visit_crate_post = bind pop_env_for_crate(cell, _),
165+
visit_item_pre = bind push_env_for_item(cell, _),
166+
visit_item_post = bind pop_env_for_item(cell, _),
167+
visit_method_pre = bind push_env_for_method(cell, _),
168+
visit_method_post = bind pop_env_for_method(cell, _),
169+
visit_native_item_pre = bind push_env_for_n_item(cell, _),
170+
visit_native_item_post = bind pop_env_for_n_item(cell, _),
171+
visit_block_pre = bind push_env_for_block(cell, _),
172+
visit_block_post = bind pop_env_for_block(cell, _),
173+
visit_arm_pre = bind walk_arm(e, cell, _),
174+
visit_arm_post = bind pop_env_for_arm(cell, _),
175+
visit_expr_pre = bind walk_expr(e, cell, _),
176+
visit_expr_post = bind pop_env_for_expr(cell, _),
177+
visit_ty_pre = bind walk_ty(e, cell, _)
178+
with walk::default_visitor());
179+
walk::walk_crate(v, c);
188180

189-
fn update_env_for_crate(&list[scope] sc, &@ast::crate c) -> list[scope] {
190-
ret cons[scope](scope_crate(c), @sc);
191-
}
192-
fn update_env_for_item(&list[scope] sc, &@ast::item i) -> list[scope] {
193-
ret cons[scope](scope_item(i), @sc);
181+
fn walk_expr(@env e, @mutable list[scope] sc, &@ast::expr exp) {
182+
push_env_for_expr(sc, exp);
183+
alt (exp.node) {
184+
case (ast::expr_path(?p, ?a)) {
185+
auto df = lookup_path_strict(*e, *sc, exp.span, p.node.idents,
186+
ns_value);
187+
e.def_map.insert(ast::ann_tag(a), df);
188+
}
189+
case (_) {}
190+
}
194191
}
195-
fn update_env_for_native_item(&list[scope] sc, &@ast::native_item i)
196-
-> list[scope] {
197-
ret cons[scope](scope_native_item(i), @sc);
192+
fn walk_ty(@env e, @mutable list[scope] sc, &@ast::ty t) {
193+
alt (t.node) {
194+
case (ast::ty_path(?p, ?a)) {
195+
auto new_def = lookup_path_strict(*e, *sc, t.span,
196+
p.node.idents, ns_type);
197+
e.def_map.insert(ast::ann_tag(a), new_def);
198+
}
199+
case (_) {}
200+
}
198201
}
199-
fn update_env_for_block(&list[scope] sc, &ast::block b) -> list[scope] {
200-
ret cons[scope](scope_block(b), @sc);
202+
fn walk_arm(@env e, @mutable list[scope] sc, &ast::arm a) {
203+
walk_pat(*e, *sc, a.pat);
204+
push_env_for_arm(sc, a);
201205
}
202-
fn update_env_for_expr(&list[scope] sc, &@ast::expr x) -> list[scope] {
203-
alt (x.node) {
204-
case (ast::expr_for(?d, _, _, _)) {
205-
ret cons[scope](scope_loop(d), @sc);
206-
}
207-
case (ast::expr_for_each(?d, _, _, _)) {
208-
ret cons[scope](scope_loop(d), @sc);
206+
fn walk_pat(&env e, &list[scope] sc, &@ast::pat pat) {
207+
alt (pat.node) {
208+
case (ast::pat_tag(?p, ?children, ?a)) {
209+
auto fnd = lookup_path_strict(e, sc, p.span, p.node.idents,
210+
ns_value);
211+
alt (fnd) {
212+
case (ast::def_variant(?did, ?vid)) {
213+
e.def_map.insert(ast::ann_tag(a), fnd);
214+
}
215+
case (_) {
216+
e.sess.span_err(p.span, "not a tag variant: " +
217+
_str::connect(p.node.idents, "::"));
218+
fail;
219+
}
220+
}
221+
for (@ast::pat child in children) {
222+
walk_pat(e, sc, child);
223+
}
209224
}
210-
case (_) { ret sc; }
225+
case (_) {}
211226
}
212227
}
213-
fn update_env_for_arm(&list[scope] sc, &ast::arm p) -> list[scope] {
214-
ret cons[scope](scope_arm(p), @sc);
215-
}
216228
}
217229

218-
fn lookup_import(&env e, def_id defid, namespace ns) -> option::t[def] {
219-
alt (e.imports.get(defid._1)) {
220-
case (todo(?item, ?sc)) {
221-
resolve_import(e, item, sc);
222-
ret lookup_import(e, defid, ns);
230+
// Helpers for tracking scope during a walk
231+
232+
fn push_env_for_crate(@mutable list[scope] sc, &ast::crate c) {
233+
*sc = cons[scope](scope_crate(@c), @*sc);
234+
}
235+
fn pop_env_for_crate(@mutable list[scope] sc, &ast::crate c) {
236+
*sc = std::list::cdr(*sc);
237+
}
238+
239+
fn push_env_for_item(@mutable list[scope] sc, &@ast::item i) {
240+
*sc = cons[scope](scope_item(i), @*sc);
241+
}
242+
fn pop_env_for_item(@mutable list[scope] sc, &@ast::item i) {
243+
*sc = std::list::cdr(*sc);
244+
}
245+
246+
fn push_env_for_method(@mutable list[scope] sc, &@ast::method m) {
247+
let vec[ast::ty_param] tp = vec();
248+
let @ast::item i = @rec(node=ast::item_fn(m.node.ident,
249+
m.node.meth,
250+
tp,
251+
m.node.id,
252+
m.node.ann),
253+
span=m.span);
254+
*sc = cons[scope](scope_item(i), @*sc);
255+
}
256+
fn pop_env_for_method(@mutable list[scope] sc, &@ast::method m) {
257+
*sc = std::list::cdr(*sc);
258+
}
259+
260+
fn push_env_for_n_item(@mutable list[scope] sc, &@ast::native_item i) {
261+
*sc = cons[scope](scope_native_item(i), @*sc);
262+
}
263+
fn pop_env_for_n_item(@mutable list[scope] sc, &@ast::native_item i) {
264+
*sc = std::list::cdr(*sc);
265+
}
266+
267+
fn push_env_for_block(@mutable list[scope] sc, &ast::block b) {
268+
*sc = cons[scope](scope_block(b), @*sc);
269+
}
270+
fn pop_env_for_block(@mutable list[scope] sc, &ast::block b) {
271+
*sc = std::list::cdr(*sc);
272+
}
273+
274+
fn push_env_for_expr(@mutable list[scope] sc, &@ast::expr x) {
275+
alt (x.node) {
276+
case (ast::expr_for(?d, _, _, _)) {
277+
*sc = cons[scope](scope_loop(d), @*sc);
223278
}
224-
case (resolving(?sp)) {
225-
e.sess.span_err(sp, "cyclic import");
279+
case (ast::expr_for_each(?d, _, _, _)) {
280+
*sc = cons[scope](scope_loop(d), @*sc);
226281
}
227-
case (resolved(?val, ?typ)) {
228-
ret alt (ns) { case (ns_value) { val }
229-
case (ns_type) { typ } };
282+
case (_) {}
283+
}
284+
}
285+
fn pop_env_for_expr(@mutable list[scope] sc, &@ast::expr x) {
286+
alt (x.node) {
287+
case (ast::expr_for(?d, _, _, _)) {
288+
*sc = std::list::cdr(*sc);
230289
}
290+
case (ast::expr_for_each(?d, _, _, _)) {
291+
*sc = std::list::cdr(*sc);
292+
}
293+
case (_) {}
231294
}
232295
}
233296

297+
fn push_env_for_arm(@mutable list[scope] sc, &ast::arm p) {
298+
*sc = cons[scope](scope_arm(p), @*sc);
299+
}
300+
fn pop_env_for_arm(@mutable list[scope] sc, &ast::arm p) {
301+
*sc = std::list::cdr(*sc);
302+
}
303+
304+
// Import resolution
305+
234306
fn resolve_import(&env e, &@ast::view_item it, &list[scope] sc) {
235307
auto defid; auto ids;
236308
alt (it.node) {
@@ -277,35 +349,7 @@ fn resolve_import(&env e, &@ast::view_item it, &list[scope] sc) {
277349
}
278350
}
279351

280-
fn fold_expr_path(@env e, &list[scope] sc, &span sp, &ast::path p, &ann a)
281-
-> @ast::expr {
282-
auto df = lookup_path_strict(*e, sc, sp, p.node.idents, ns_value);
283-
e.def_map.insert(ast::ann_tag(a), df);
284-
ret @fold::respan(sp, ast::expr_path(p, a));
285-
}
286-
287-
288-
fn fold_pat_tag(@env e, &list[scope] sc, &span sp, &ast::path p,
289-
&vec[@ast::pat] args, &ann a) -> @ast::pat {
290-
alt (lookup_path_strict(*e, sc, sp, p.node.idents, ns_value)) {
291-
case (ast::def_variant(?did, ?vid)) {
292-
e.def_map.insert(ast::ann_tag(a), ast::def_variant(did, vid));
293-
ret @fold::respan[ast::pat_](sp, ast::pat_tag(p, args, a));
294-
}
295-
case (_) {
296-
e.sess.span_err(sp, "not a tag variant: " +
297-
_str::connect(p.node.idents, "::"));
298-
fail;
299-
}
300-
}
301-
}
302-
303-
fn fold_ty_path(@env e, &list[scope] sc, &span sp, &ast::path p,
304-
&ast::ann a) -> @ast::ty {
305-
auto new_def = lookup_path_strict(*e, sc, sp, p.node.idents, ns_type);
306-
e.def_map.insert(ast::ann_tag(a), new_def);
307-
ret @fold::respan[ast::ty_](sp, ast::ty_path(p, a));
308-
}
352+
// Utilities
309353

310354
fn is_module(def d) -> bool {
311355
alt (d) {
@@ -326,6 +370,8 @@ fn unresolved(&env e, &span sp, ident id, str kind) {
326370
e.sess.span_err(sp, "unresolved " + kind + ": " + id);
327371
}
328372

373+
// Lookup helpers
374+
329375
fn lookup_path_strict(&env e, &list[scope] sc, &span sp, vec[ident] idents,
330376
namespace ns) -> def {
331377
auto n_idents = _vec::len(idents);
@@ -628,6 +674,22 @@ fn found_view_item(&env e, @ast::view_item vi, namespace ns)
628674
}
629675
}
630676

677+
fn lookup_import(&env e, def_id defid, namespace ns) -> option::t[def] {
678+
alt (e.imports.get(defid._1)) {
679+
case (todo(?item, ?sc)) {
680+
resolve_import(e, item, sc);
681+
ret lookup_import(e, defid, ns);
682+
}
683+
case (resolving(?sp)) {
684+
e.sess.span_err(sp, "cyclic import");
685+
}
686+
case (resolved(?val, ?typ)) {
687+
ret alt (ns) { case (ns_value) { val }
688+
case (ns_type) { typ } };
689+
}
690+
}
691+
}
692+
631693
fn lookup_in_regular_mod(&env e, def_id defid, ident id, namespace ns, dir dr)
632694
-> option::t[def] {
633695
auto info = e.mod_map.get(defid._1);

0 commit comments

Comments
 (0)