Skip to content

Commit c67eb1a

Browse files
committed
rustc: Partially resolve external module imports
1 parent 6a5feff commit c67eb1a

File tree

2 files changed

+104
-20
lines changed

2 files changed

+104
-20
lines changed

src/comp/front/creader.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -540,18 +540,15 @@ fn read_crates(session.session sess,
540540

541541
// Crate metadata queries
542542

543-
fn lookup_def(session.session sess, &span sp, int cnum, vec[ast.ident] path)
544-
-> ast.def {
543+
fn lookup_def(session.session sess, int cnum, vec[ast.ident] path)
544+
-> option.t[ast.def] {
545545
auto data = sess.get_external_crate(cnum);
546546

547547
auto did;
548548
alt (resolve_path(path, data)) {
549549
case (rr_ok(?di)) { did = di; }
550550
case (rr_not_found(?prev, ?name)) {
551-
sess.span_err(sp,
552-
#fmt("unbound name '%s' (no item named '%s' found in '%s')",
553-
_str.connect(path, "."), name, _str.connect(prev, ".")));
554-
fail;
551+
ret none[ast.def];
555552
}
556553
}
557554

@@ -561,21 +558,24 @@ fn lookup_def(session.session sess, &span sp, int cnum, vec[ast.ident] path)
561558
did = tup(cnum, did._1);
562559

563560
// FIXME: It'd be great if we had u8 char literals.
564-
if (kind_ch == ('c' as u8)) { ret ast.def_const(did); }
565-
else if (kind_ch == ('f' as u8)) { ret ast.def_fn(did); }
566-
else if (kind_ch == ('y' as u8)) { ret ast.def_ty(did); }
567-
else if (kind_ch == ('o' as u8)) { ret ast.def_obj(did); }
568-
else if (kind_ch == ('t' as u8)) { ret ast.def_ty(did); }
569-
else if (kind_ch == ('m' as u8)) { ret ast.def_mod(did); }
570-
else if (kind_ch == ('n' as u8)) { ret ast.def_native_mod(did); }
561+
auto def;
562+
if (kind_ch == ('c' as u8)) { def = ast.def_const(did); }
563+
else if (kind_ch == ('f' as u8)) { def = ast.def_fn(did); }
564+
else if (kind_ch == ('y' as u8)) { def = ast.def_ty(did); }
565+
else if (kind_ch == ('o' as u8)) { def = ast.def_obj(did); }
566+
else if (kind_ch == ('t' as u8)) { def = ast.def_ty(did); }
567+
else if (kind_ch == ('m' as u8)) { def = ast.def_mod(did); }
568+
else if (kind_ch == ('n' as u8)) { def = ast.def_native_mod(did); }
571569
else if (kind_ch == ('v' as u8)) {
572570
auto tid = get_variant_tag_id(ebml_r);
573571
tid = tup(cnum, tid._1);
574-
ret ast.def_variant(tid, did);
572+
def = ast.def_variant(tid, did);
573+
} else {
574+
log #fmt("lookup_def(): unknown kind char: %d", kind_ch as int);
575+
fail;
575576
}
576577

577-
log #fmt("lookup_def(): unknown kind char: %d", kind_ch as int);
578-
fail;
578+
ret some[ast.def](def);
579579
}
580580

581581
fn get_type(session.session sess, ast.def_id def) -> ty.ty_params_and_ty {

src/comp/middle/resolve.rs

+88-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ tag scope {
2020
scope_crate(@ast.crate);
2121
scope_item(@ast.item);
2222
scope_native_item(@ast.native_item);
23+
scope_external_mod(ast.def_id, vec[ast.ident]);
2324
scope_loop(@ast.decl); // there's only 1 decl per loop.
2425
scope_block(ast.block);
2526
scope_arm(ast.arm);
@@ -37,6 +38,8 @@ tag def_wrap {
3738
def_wrap_import(@ast.view_item);
3839
def_wrap_mod(@ast.item);
3940
def_wrap_native_mod(@ast.item);
41+
def_wrap_external_mod(ast.def_id);
42+
def_wrap_external_native_mod(ast.def_id);
4043
def_wrap_other(def);
4144
def_wrap_expr_field(uint, def);
4245
def_wrap_resolving;
@@ -79,6 +82,12 @@ fn unwrap_def(def_wrap d) -> def {
7982
}
8083
}
8184
}
85+
case (def_wrap_external_mod(?mod_id)) {
86+
ret ast.def_mod(mod_id);
87+
}
88+
case (def_wrap_external_native_mod(?mod_id)) {
89+
ret ast.def_native_mod(mod_id);
90+
}
8291
case (def_wrap_other(?d)) {
8392
ret d;
8493
}
@@ -100,6 +109,30 @@ fn lookup_name(&env e, ast.ident i) -> option.t[def] {
100109
}
101110
}
102111

112+
fn lookup_external_def(session.session sess, int cnum, vec[ident] idents)
113+
-> option.t[def_wrap] {
114+
alt (creader.lookup_def(sess, cnum, idents)) {
115+
case (none[ast.def]) {
116+
ret none[def_wrap];
117+
}
118+
case (some[ast.def](?def)) {
119+
auto dw;
120+
alt (def) {
121+
case (ast.def_mod(?mod_id)) {
122+
dw = def_wrap_external_mod(mod_id);
123+
}
124+
case (ast.def_native_mod(?mod_id)) {
125+
dw = def_wrap_external_native_mod(mod_id);
126+
}
127+
case (_) {
128+
dw = def_wrap_other(def);
129+
}
130+
}
131+
ret some[def_wrap](dw);
132+
}
133+
}
134+
}
135+
103136
// Follow the path of an import and return what it ultimately points to.
104137

105138
// If used after imports are resolved, import_id is none.
@@ -136,12 +169,43 @@ fn find_final_def(&env e, import_map index,
136169
}
137170
}
138171

172+
// TODO: Refactor with above.
173+
fn found_external_mod(&env e, &import_map index, &span sp,
174+
vec[ident] idents, ast.def_id mod_id)
175+
-> def_wrap {
176+
auto len = _vec.len[ident](idents);
177+
auto rest_idents = _vec.slice[ident](idents, 1u, len);
178+
auto empty_e = rec(scopes = nil[scope], sess = e.sess);
179+
auto tmp_e = update_env_for_external_mod(empty_e, mod_id, idents);
180+
auto next_i = rest_idents.(0);
181+
auto next_ = lookup_name_wrapped(tmp_e, next_i);
182+
alt (next_) {
183+
case (none[tup(@env, def_wrap)]) {
184+
e.sess.span_err(sp, "unresolved name: " + next_i);
185+
fail;
186+
}
187+
case (some[tup(@env, def_wrap)](?next)) {
188+
auto combined_e = update_env_for_external_mod(e,
189+
mod_id,
190+
idents);
191+
ret found_something(combined_e, index, sp,
192+
rest_idents, next._1);
193+
}
194+
}
195+
}
196+
139197
fn found_crate(&env e, &import_map index, &span sp,
140198
vec[ident] idents, int cnum) -> def_wrap {
141199
auto len = _vec.len[ident](idents);
142200
auto rest_idents = _vec.slice[ident](idents, 1u, len);
143-
auto def = creader.lookup_def(e.sess, sp, cnum, rest_idents);
144-
ret def_wrap_other(def);
201+
alt (lookup_external_def(e.sess, cnum, rest_idents)) {
202+
case (none[def_wrap]) {
203+
e.sess.span_err(sp, #fmt("unbound name '%s'",
204+
_str.connect(idents, ".")));
205+
fail;
206+
}
207+
case (some[def_wrap](?dw)) { ret dw; }
208+
}
145209
}
146210

147211
alt (d) {
@@ -168,6 +232,12 @@ fn find_final_def(&env e, import_map index,
168232
case (def_wrap_native_mod(?i)) {
169233
ret found_mod(e, index, sp, idents, i);
170234
}
235+
case (def_wrap_external_mod(?mod_id)) {
236+
ret found_external_mod(e, index, sp, idents, mod_id);
237+
}
238+
case (def_wrap_external_native_mod(?mod_id)) {
239+
ret found_external_mod(e, index, sp, idents, mod_id);
240+
}
171241
case (def_wrap_use(?vi)) {
172242
alt (vi.node) {
173243
case (ast.view_item_use(_, _, _, ?cnum_opt)) {
@@ -390,7 +460,8 @@ fn lookup_name_wrapped(&env e, ast.ident i) -> option.t[tup(@env, def_wrap)] {
390460
}
391461
}
392462

393-
fn in_scope(ast.ident i, &scope s) -> option.t[def_wrap] {
463+
fn in_scope(&session.session sess, ast.ident i, &scope s)
464+
-> option.t[def_wrap] {
394465
alt (s) {
395466

396467
case (scope_crate(?c)) {
@@ -450,6 +521,10 @@ fn lookup_name_wrapped(&env e, ast.ident i) -> option.t[tup(@env, def_wrap)] {
450521
}
451522
}
452523

524+
case (scope_external_mod(?mod_id, ?path)) {
525+
ret lookup_external_def(sess, mod_id._0, path);
526+
}
527+
453528
case (scope_loop(?d)) {
454529
alt (d.node) {
455530
case (ast.decl_local(?local)) {
@@ -483,7 +558,7 @@ fn lookup_name_wrapped(&env e, ast.ident i) -> option.t[tup(@env, def_wrap)] {
483558
ret none[tup(@env, def_wrap)];
484559
}
485560
case (cons[scope](?hd, ?tl)) {
486-
auto x = in_scope(i, hd);
561+
auto x = in_scope(e.sess, i, hd);
487562
alt (x) {
488563
case (some[def_wrap](?x)) {
489564
ret some(tup(@e, x));
@@ -613,6 +688,15 @@ fn update_env_for_native_item(&env e, @ast.native_item i) -> env {
613688
ret rec(scopes = cons[scope](scope_native_item(i), @e.scopes) with e);
614689
}
615690

691+
// Not actually called by fold, but here since this is analogous to
692+
// update_env_for_item() above and is called by find_final_def().
693+
fn update_env_for_external_mod(&env e, ast.def_id mod_id,
694+
vec[ast.ident] idents) -> env {
695+
ret rec(scopes = cons[scope](scope_external_mod(mod_id, idents),
696+
@e.scopes)
697+
with e);
698+
}
699+
616700
fn update_env_for_block(&env e, &ast.block b) -> env {
617701
ret rec(scopes = cons[scope](scope_block(b), @e.scopes) with e);
618702
}

0 commit comments

Comments
 (0)