1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
import front.ast;
import front.ast.ident;
import front.ast.def;
import front.ast.ann;
import driver.session;
import util.common.span;
import std.map.hashmap;
import std.list.list;
import std.list.nil;
import std.list.cons;
import std.option;
import std.option.some;
import std.option.none;
import std._str;
import std._vec;
tag scope {
scope_crate(@ast.crate);
scope_item(@ast.item);
scope_block(ast.block);
}
type env = rec(list[scope] scopes,
session.session sess);
fn lookup_name(&env e, ast.ident i) -> option.t[def] {
// log "resolving name " + i;
fn found_def_item(@ast.item i) -> option.t[def] {
alt (i.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));
}
}
}
fn found_decl_stmt(@ast.stmt s) -> option.t[def] {
alt (s.node) {
case (ast.stmt_decl(?d)) {
alt (d.node) {
case (ast.decl_local(?loc)) {
ret some[def](ast.def_local(loc.id));
}
case (ast.decl_item(?it)) {
ret found_def_item(it);
}
}
}
}
ret none[def];
}
fn check_mod(ast.ident i, ast._mod m) -> option.t[def] {
alt (m.index.find(i)) {
case (some[uint](?ix)) {
ret found_def_item(m.items.(ix));
}
}
ret none[def];
}
fn in_scope(ast.ident i, &scope s) -> option.t[def] {
alt (s) {
case (scope_crate(?c)) {
ret check_mod(i, c.node.module);
}
case (scope_item(?it)) {
alt (it.node) {
case (ast.item_fn(_, ?f, _, _, _)) {
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, _)) {
ret check_mod(i, m);
}
}
}
case (scope_block(?b)) {
alt (b.node.index.find(i)) {
case (some[uint](?ix)) {
ret found_decl_stmt(b.node.stmts.(ix));
}
}
}
}
ret none[def];
}
ret std.list.find[scope,def](e.scopes, bind in_scope(i, _));
}
fn fold_expr_name(&env e, &span sp, &ast.name n,
&option.t[def] d, ann a) -> @ast.expr {
if (_vec.len[@ast.ty](n.node.types) > 0u) {
e.sess.unimpl("resoling name expr with ty params");
}
auto d_ = lookup_name(e, n.node.ident);
alt (d_) {
case (some[def](_)) {
// log "resolved name " + n.node.ident;
}
case (none[def]) {
e.sess.err("unresolved name: " + n.node.ident);
}
}
ret @fold.respan[ast.expr_](sp, ast.expr_name(n, d_, a));
}
fn fold_ty_path(&env e, &span sp, ast.path p,
&option.t[def] d) -> @ast.ty {
let uint len = _vec.len[ast.name](p);
check (len != 0u);
if (len > 1u) {
e.sess.unimpl("resoling path ty with >1 component");
}
let ast.name n = p.(0);
if (_vec.len[@ast.ty](n.node.types) > 0u) {
e.sess.unimpl("resoling path ty with ty params");
}
auto d_ = lookup_name(e, n.node.ident);
alt (d_) {
case (some[def](_)) {
// log "resolved name " + n.node.ident;
}
case (none[def]) {
e.sess.err("unresolved name: " + n.node.ident);
}
}
ret @fold.respan[ast.ty_](sp, ast.ty_path(p, d_));
}
fn update_env_for_crate(&env e, @ast.crate c) -> env {
ret rec(scopes = cons[scope](scope_crate(c), @e.scopes) with e);
}
fn update_env_for_item(&env e, @ast.item i) -> env {
ret rec(scopes = cons[scope](scope_item(i), @e.scopes) with e);
}
fn update_env_for_block(&env e, &ast.block b) -> env {
ret rec(scopes = cons[scope](scope_block(b), @e.scopes) with 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_expr_name = bind fold_expr_name(_,_,_,_,_),
fold_ty_path = bind fold_ty_path(_,_,_,_),
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(_,_)
with *fld );
auto e = rec(scopes = nil[scope],
sess = sess);
ret fold.fold_crate[env](e, fld, crate);
}
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C ../.. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:
|