@@ -93,8 +93,7 @@ tag namespace {
93
93
ns_type;
94
94
}
95
95
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 {
98
97
auto e = @rec ( def_map = new_uint_hash[ def] ( ) ,
99
98
imports = new_int_hash[ import_state] ( ) ,
100
99
mod_map = new_int_hash[ @indexed_mod] ( ) ,
@@ -104,7 +103,8 @@ fn resolve_crate(session sess, @ast::crate crate)
104
103
sess = sess) ;
105
104
map_crate ( e, * crate ) ;
106
105
resolve_imports ( * e) ;
107
- ret tup( resolve_names ( e, * crate ) , e. def_map ) ;
106
+ resolve_names ( e, * crate ) ;
107
+ ret e. def_map ;
108
108
}
109
109
110
110
// 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) {
115
115
auto v = rec ( visit_crate_pre = bind push_env_for_crate ( cell, _) ,
116
116
visit_crate_post = bind pop_env_for_crate ( cell, _) ,
117
117
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, _) ,
119
119
visit_item_post = bind pop_env_for_item ( cell, _)
120
120
with walk:: default_visitor ( ) ) ;
121
121
// Register the top-level mod
122
122
e. mod_map . insert ( -1 , @rec ( m=c. node . module ,
123
123
index=index_mod ( c. node . module ) ) ) ;
124
124
walk:: walk_crate ( v, c) ;
125
125
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
+ }
132
133
}
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) ;
136
136
alt ( i. node ) {
137
137
case ( ast:: item_mod ( _, ?md, ?defid) ) {
138
138
auto index = index_mod ( md) ;
@@ -145,17 +145,6 @@ fn map_crate(&@env e, &ast::crate c) {
145
145
case ( _) { }
146
146
}
147
147
}
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
- }
159
148
}
160
149
161
150
fn resolve_imports ( & env e) {
@@ -169,68 +158,151 @@ fn resolve_imports(&env e) {
169
158
}
170
159
}
171
160
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) ;
188
180
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
+ }
194
191
}
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
+ }
198
201
}
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) ;
201
205
}
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
+ }
209
224
}
210
- case ( _) { ret sc ; }
225
+ case ( _) { }
211
226
}
212
227
}
213
- fn update_env_for_arm ( & list[ scope] sc , & ast:: arm p) -> list[ scope ] {
214
- ret cons[ scope] ( scope_arm ( p) , @sc) ;
215
- }
216
228
}
217
229
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) ;
223
278
}
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 ) ;
226
281
}
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) ;
230
289
}
290
+ case ( ast:: expr_for_each ( ?d, _, _, _) ) {
291
+ * sc = std:: list:: cdr ( * sc) ;
292
+ }
293
+ case ( _) { }
231
294
}
232
295
}
233
296
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
+
234
306
fn resolve_import ( & env e, & @ast:: view_item it, & list[ scope] sc ) {
235
307
auto defid; auto ids;
236
308
alt ( it. node ) {
@@ -277,35 +349,7 @@ fn resolve_import(&env e, &@ast::view_item it, &list[scope] sc) {
277
349
}
278
350
}
279
351
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
309
353
310
354
fn is_module ( def d) -> bool {
311
355
alt ( d) {
@@ -326,6 +370,8 @@ fn unresolved(&env e, &span sp, ident id, str kind) {
326
370
e. sess . span_err ( sp, "unresolved " + kind + ": " + id) ;
327
371
}
328
372
373
+ // Lookup helpers
374
+
329
375
fn lookup_path_strict ( & env e, & list[ scope] sc , & span sp, vec[ ident] idents ,
330
376
namespace ns) -> def {
331
377
auto n_idents = _vec:: len ( idents) ;
@@ -628,6 +674,22 @@ fn found_view_item(&env e, @ast::view_item vi, namespace ns)
628
674
}
629
675
}
630
676
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
+
631
693
fn lookup_in_regular_mod ( & env e, def_id defid, ident id, namespace ns, dir dr)
632
694
-> option:: t [ def ] {
633
695
auto info = e. mod_map . get ( defid. _1 ) ;
0 commit comments