Skip to content

Commit 093116d

Browse files
committed
---
yaml --- r: 11117 b: refs/heads/master c: 48769b5 h: refs/heads/master i: 11115: be8f212 v: v3
1 parent 9b21cc8 commit 093116d

File tree

5 files changed

+151
-97
lines changed

5 files changed

+151
-97
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 6e680e36a70e94f307928fee1c99b29a28eeba6e
2+
refs/heads/master: 48769b57e0f184ff42f5226641447e41cf3053a7
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
55
refs/heads/try: 2898dcc5d97da9427ac367542382b6239d9c0bbf

trunk/src/comp/middle/resolve.rs

Lines changed: 131 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import syntax::{ast, ast_util, codemap};
33
import syntax::ast::*;
44
import ast::{ident, fn_ident, def, def_id, node_id};
5-
import syntax::ast_util::{local_def, def_id_of_def};
5+
import syntax::ast_util::{local_def, def_id_of_def, class_item_ident};
66
import pat_util::*;
77

88
import front::attr;
@@ -91,6 +91,8 @@ enum mod_index_entry {
9191
mie_view_item(ident, node_id, span),
9292
mie_import_ident(node_id, span),
9393
mie_item(@ast::item),
94+
mie_class_item(node_id, /* parent class name */
95+
@ast::class_item), /* class member */
9496
mie_native_item(@ast::native_item),
9597
mie_enum_variant(/* variant index */uint,
9698
/*parts of enum item*/ [variant],
@@ -267,27 +269,30 @@ fn map_crate(e: @env, c: @ast::crate) {
267269
alt vi.node {
268270
//if it really is a glob import, that is
269271
ast::view_item_import_glob(path, _) {
270-
let imp = follow_import(*e, sc, *path, vi.span);
271-
if option::is_some(imp) {
272-
let glob = {def: option::get(imp), item: vi};
273-
check list::is_not_empty(sc);
274-
alt list::head(sc) {
275-
scope_item(i) {
276-
e.mod_map.get(i.id).glob_imports += [glob];
277-
}
278-
scope_block(b, _, _) {
279-
let globs = alt e.block_map.find(b.node.id) {
280-
some(globs) { globs + [glob] } none { [glob] }
281-
};
282-
e.block_map.insert(b.node.id, globs);
283-
}
284-
scope_crate {
285-
e.mod_map.get(ast::crate_node_id).glob_imports += [glob];
286-
}
287-
_ { e.sess.span_bug(vi.span, "Unexpected scope in a glob \
288-
import"); }
272+
alt follow_import(*e, sc, *path, vi.span) {
273+
some(imp) {
274+
let glob = {def: imp, item: vi};
275+
check list::is_not_empty(sc);
276+
alt list::head(sc) {
277+
scope_item(i) {
278+
e.mod_map.get(i.id).glob_imports += [glob];
279+
}
280+
scope_block(b, _, _) {
281+
let globs = alt e.block_map.find(b.node.id) {
282+
some(globs) { globs + [glob] } none { [glob] }
283+
};
284+
e.block_map.insert(b.node.id, globs);
285+
}
286+
scope_crate {
287+
e.mod_map.get(ast::crate_node_id).glob_imports
288+
+= [glob];
289+
}
290+
_ { e.sess.span_bug(vi.span, "Unexpected scope in a \
291+
glob import"); }
292+
}
289293
}
290-
}
294+
_ { }
295+
}
291296
}
292297
_ { }
293298
}
@@ -329,7 +334,10 @@ fn resolve_capture_item(e: @env, sc: scopes, &&cap_item: @ast::capture_item) {
329334
}
330335

331336
fn maybe_insert(e: @env, id: node_id, def: option<def>) {
332-
if option::is_some(def) { e.def_map.insert(id, option::get(def)); }
337+
alt def {
338+
some(df) { e.def_map.insert(id, df); }
339+
_ {}
340+
}
333341
}
334342

335343
fn resolve_names(e: @env, c: @ast::crate) {
@@ -401,11 +409,10 @@ fn resolve_names(e: @env, c: @ast::crate) {
401409
visit::visit_pat(pat, sc, v);
402410
alt pat.node {
403411
ast::pat_enum(p, _) {
404-
let fnd = lookup_path_strict(*e, sc, p.span, p.node,
405-
ns_val(ns_any_value));
406-
alt option::get(fnd) {
407-
ast::def_variant(did, vid) {
408-
e.def_map.insert(pat.id, option::get(fnd));
412+
alt lookup_path_strict(*e, sc, p.span, p.node,
413+
ns_val(ns_any_value)) {
414+
some(fnd@ast::def_variant(_,_)) {
415+
e.def_map.insert(pat.id, fnd);
409416
}
410417
_ {
411418
e.sess.span_err(p.span,
@@ -417,11 +424,10 @@ fn resolve_names(e: @env, c: @ast::crate) {
417424
/* Here we determine whether a given pat_ident binds a new
418425
variable a refers to a nullary enum. */
419426
ast::pat_ident(p, none) {
420-
let fnd = lookup_in_scope(*e, sc, p.span, path_to_ident(p),
421-
ns_val(ns_a_enum));
422-
alt fnd {
423-
some(ast::def_variant(did, vid)) {
424-
e.def_map.insert(pat.id, ast::def_variant(did, vid));
427+
alt lookup_in_scope(*e, sc, p.span, path_to_ident(p),
428+
ns_val(ns_a_enum)) {
429+
some(fnd@ast::def_variant(_,_)) {
430+
e.def_map.insert(pat.id, fnd);
425431
}
426432
_ {
427433
// Binds a var -- nothing needs to be done
@@ -587,16 +593,21 @@ fn follow_import(e: env, sc: scopes, path: [ident], sp: span) ->
587593
let path_len = vec::len(path);
588594
let dcur = lookup_in_scope_strict(e, sc, sp, path[0], ns_module);
589595
let i = 1u;
590-
while true && option::is_some(dcur) {
591-
if i == path_len { break; }
592-
dcur =
593-
lookup_in_mod_strict(e, option::get(dcur), sp, path[i],
596+
while true {
597+
alt dcur {
598+
some(dcur_def) {
599+
if i == path_len { break; }
600+
dcur =
601+
lookup_in_mod_strict(e, dcur_def, sp, path[i],
594602
ns_module, outside);
595-
i += 1u;
603+
i += 1u;
604+
}
605+
_ { break; }
606+
}
596607
}
597608
if i == path_len {
598-
alt option::get(dcur) {
599-
ast::def_mod(_) | ast::def_native_mod(_) { ret dcur; }
609+
alt dcur {
610+
some(ast::def_mod(_)) | some(ast::def_native_mod(_)) { ret dcur; }
600611
_ {
601612
e.sess.span_err(sp, str::connect(path, "::") +
602613
" does not name a module.");
@@ -607,20 +618,16 @@ fn follow_import(e: env, sc: scopes, path: [ident], sp: span) ->
607618
}
608619

609620
fn resolve_constr(e: @env, c: @ast::constr, sc: scopes, _v: vt<scopes>) {
610-
let new_def =
611-
lookup_path_strict(*e, sc, c.span, c.node.path.node,
612-
ns_val(ns_any_value));
613-
if option::is_some(new_def) {
614-
alt option::get(new_def) {
615-
ast::def_fn(pred_id, ast::pure_fn) {
616-
e.def_map.insert(c.node.id, ast::def_fn(pred_id, ast::pure_fn));
617-
}
618-
_ {
619-
e.sess.span_err(c.span,
620-
"Non-predicate in constraint: " +
621-
path_to_str(c.node.path));
622-
}
623-
}
621+
alt lookup_path_strict(*e, sc, c.span, c.node.path.node,
622+
ns_val(ns_any_value)) {
623+
some(d@ast::def_fn(_,ast::pure_fn)) {
624+
e.def_map.insert(c.node.id, d);
625+
}
626+
_ {
627+
e.sess.span_err(c.span,
628+
"Non-predicate in constraint: " +
629+
path_to_str(c.node.path));
630+
}
624631
}
625632
}
626633

@@ -744,7 +751,7 @@ fn ns_name(ns: namespace) -> str {
744751
ns_a_enum { "enum" }
745752
}
746753
}
747-
ns_module { ret "modulename" }
754+
ns_module { "modulename" }
748755
}
749756
}
750757

@@ -814,18 +821,26 @@ fn lookup_path_strict(e: env, sc: scopes, sp: span, pth: ast::path_,
814821

815822
let first_scope = if pth.global { top_scope() } else { sc };
816823

817-
let dcur =
824+
let dcur_ =
818825
lookup_in_scope_strict(e, first_scope, sp, pth.idents[0], headns);
819826

820-
let i = 1u;
821-
while i < n_idents && option::is_some(dcur) {
822-
let curns = if n_idents == i + 1u { ns } else { ns_module };
823-
dcur =
824-
lookup_in_mod_strict(e, option::get(dcur), sp, pth.idents[i],
825-
curns, outside);
826-
i += 1u;
827+
alt dcur_ {
828+
none { ret none; }
829+
some(dcur__) {
830+
let i = 1u;
831+
let dcur = dcur__;
832+
while i < n_idents {
833+
let curns = if n_idents == i + 1u { ns } else { ns_module };
834+
alt lookup_in_mod_strict(e, dcur, sp, pth.idents[i],
835+
curns, outside) {
836+
none { break; }
837+
some(thing) { dcur = thing; }
838+
}
839+
i += 1u;
840+
}
841+
ret some(dcur);
842+
}
827843
}
828-
ret dcur;
829844
}
830845

831846
fn lookup_in_scope_strict(e: env, sc: scopes, sp: span, name: ident,
@@ -985,11 +1000,11 @@ fn lookup_in_scope(e: env, sc: scopes, sp: span, name: ident, ns: namespace)
9851000
alt copy sc {
9861001
nil { ret none; }
9871002
cons(hd, tl) {
988-
let fnd = in_scope(e, sp, name, hd, ns);
989-
if !is_none(fnd) {
990-
let df = option::get(fnd);
991-
let local = def_is_local(df), self_scope = def_is_self(df);
992-
if left_fn && local || left_fn_level2 && self_scope
1003+
alt in_scope(e, sp, name, hd, ns) {
1004+
some(df_) {
1005+
let df = df_;
1006+
let local = def_is_local(df), self_scope = def_is_self(df);
1007+
if left_fn && local || left_fn_level2 && self_scope
9931008
|| scope_is_fn(hd) && left_fn && def_is_ty_arg(df) {
9941009
let msg = alt ns {
9951010
ns_type {
@@ -1013,12 +1028,13 @@ fn lookup_in_scope(e: env, sc: scopes, sp: span, name: ident, ns: namespace)
10131028
i -= 1u;
10141029
df = ast::def_upvar(def_id_of_def(df), @df,
10151030
closing[i]);
1016-
fnd = some(df);
10171031
}
10181032
}
1019-
ret fnd;
1033+
ret some(df);
10201034
}
1021-
if left_fn {
1035+
_ {}
1036+
}
1037+
if left_fn {
10221038
left_fn_level2 = true;
10231039
} else if ns != ns_module {
10241040
left_fn = scope_is_fn(hd);
@@ -1193,7 +1209,10 @@ fn found_def_item(i: @ast::item, ns: namespace) -> option<def> {
11931209
_ { }
11941210
}
11951211
}
1196-
_ { }
1212+
ast::item_class(_, _, _, _, _) {
1213+
fail "class! don't know what to do";
1214+
}
1215+
ast::item_impl(_,_,_,_) { /* ??? */ }
11971216
}
11981217
ret none;
11991218
}
@@ -1220,12 +1239,13 @@ fn lookup_in_mod(e: env, m: def, sp: span, name: ident, ns: namespace,
12201239
if defid.node != ast::crate_node_id {
12211240
path = cstore::get_path(e.cstore, defid) + path;
12221241
}
1223-
let fnd = lookup_external(e, defid.crate, path, ns);
1224-
if !is_none(fnd) {
1225-
e.ext_cache.insert({did: defid, ident: name, ns: ns},
1226-
option::get(fnd));
1242+
alt lookup_external(e, defid.crate, path, ns) {
1243+
some(df) {
1244+
e.ext_cache.insert({did: defid, ident: name, ns: ns}, df);
1245+
ret some(df);
1246+
}
1247+
_ { ret none; }
12271248
}
1228-
ret fnd;
12291249
}
12301250
alt m {
12311251
ast::def_mod(defid) {
@@ -1263,7 +1283,7 @@ fn lookup_import(e: env, defid: def_id, ns: namespace) -> option<def> {
12631283
e.used_imports.data += [defid.node];
12641284
}
12651285
ret alt ns { ns_val(_) { val } ns_type { typ }
1266-
ns_module { md } };
1286+
ns_module { md } };
12671287
}
12681288
is_glob(_,_,_) {
12691289
e.sess.bug("lookup_import: can't handle is_glob");
@@ -1398,6 +1418,18 @@ fn lookup_in_mie(e: env, mie: mod_index_entry, ns: namespace) ->
13981418
}
13991419
}
14001420
}
1421+
mie_class_item(parent_id, class_item) {
1422+
alt class_item.node.decl {
1423+
instance_var(_,_,_,id) {
1424+
ret some(ast::def_class_field(local_def(parent_id),
1425+
local_def(id)));
1426+
}
1427+
class_method(it) {
1428+
ret some(ast::def_class_method(local_def(parent_id),
1429+
local_def(it.id)));
1430+
}
1431+
}
1432+
}
14011433
}
14021434
ret none;
14031435
}
@@ -1452,8 +1484,21 @@ fn index_mod(md: ast::_mod) -> mod_index {
14521484
variant_idx += 1u;
14531485
}
14541486
}
1455-
ast::item_class(_, items, _, ctor_decl, _) {
1456-
fail "resolve::index_mod: item_class";
1487+
ast::item_class(tps, items, ctor_id, ctor_decl, ctor_body) {
1488+
// add the class name itself
1489+
add_to_index(index, it.ident, mie_item(it));
1490+
// add the constructor decl
1491+
add_to_index(index, it.ident,
1492+
mie_item(@{ident: it.ident, attrs: [],
1493+
id: ctor_id,
1494+
node:
1495+
item_fn(ctor_decl, tps, ctor_body),
1496+
span: ctor_body.span}));
1497+
// add the members
1498+
for ci in items {
1499+
add_to_index(index, class_item_ident(ci),
1500+
mie_class_item(it.id, ci));
1501+
}
14571502
}
14581503
}
14591504
}
@@ -1494,11 +1539,13 @@ fn ns_for_def(d: def) -> namespace {
14941539
ast::def_variant(_, _) { ns_val(ns_a_enum) }
14951540
ast::def_fn(_, _) | ast::def_self(_) |
14961541
ast::def_const(_) | ast::def_arg(_, _) | ast::def_local(_, _) |
1497-
ast::def_upvar(_, _, _) | ast::def_self(_) { ns_val(ns_any_value) }
1542+
ast::def_upvar(_, _, _) | ast::def_self(_) |
1543+
ast::def_class_field(_,_) | ast::def_class_method(_,_)
1544+
{ ns_val(ns_any_value) }
14981545
ast::def_mod(_) | ast::def_native_mod(_) { ns_module }
14991546
ast::def_ty(_) | ast::def_binding(_) | ast::def_use(_) |
1500-
ast::def_ty_param(_, _) | ast::def_prim_ty(_) { ns_type }
1501-
_ { fail "Dead"; }
1547+
ast::def_ty_param(_, _) | ast::def_prim_ty(_) | ast::def_class(_)
1548+
{ ns_type }
15021549
}
15031550
}
15041551

@@ -1583,6 +1630,7 @@ fn mie_span(mie: mod_index_entry) -> span {
15831630
mie_item(item) { item.span }
15841631
mie_enum_variant(_, _, _, span) { span }
15851632
mie_native_item(item) { item.span }
1633+
mie_class_item(_,item) { item.span }
15861634
};
15871635
}
15881636

trunk/src/comp/syntax/ast_util.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ fn def_id_of_def(d: def) -> def_id {
3030
def_fn(id, _) | def_self(id) | def_mod(id) |
3131
def_native_mod(id) | def_const(id) | def_arg(id, _) | def_local(id, _) |
3232
def_variant(_, id) | def_ty(id) | def_ty_param(id, _) |
33-
def_binding(id) | def_use(id) | def_upvar(id, _, _) { id }
33+
def_binding(id) | def_use(id) | def_upvar(id, _, _) |
34+
def_class(id) | def_class_field(_, id) | def_class_method(_, id) { id }
3435
def_prim_ty(_) { fail; }
35-
_ { fail "Dead"; }
3636
}
3737
}
3838

@@ -374,6 +374,13 @@ pure fn unguarded_pat(a: arm) -> option<[@pat]> {
374374
// for reserving this id.
375375
fn op_expr_callee_id(e: @expr) -> node_id { e.id - 1 }
376376

377+
pure fn class_item_ident(ci: @class_item) -> ident {
378+
alt ci.node.decl {
379+
instance_var(i,_,_,_) { i }
380+
class_method(it) { it.ident }
381+
}
382+
}
383+
377384
// Local Variables:
378385
// mode: rust
379386
// fill-column: 78;

0 commit comments

Comments
 (0)