14
14
//! any imports resolved.
15
15
16
16
use resolve_imports:: ImportDirectiveSubclass :: { self , GlobImport } ;
17
- use Module ;
17
+ use { Module , ModuleS , ModuleKind } ;
18
18
use Namespace :: { self , TypeNS , ValueNS } ;
19
19
use { NameBinding , NameBindingKind , ToNameBinding } ;
20
- use ParentLink :: { ModuleParentLink , BlockParentLink } ;
21
20
use Resolver ;
22
21
use { resolve_error, resolve_struct_error, ResolutionError } ;
23
22
@@ -34,7 +33,7 @@ use syntax::parse::token;
34
33
35
34
use syntax:: ast:: { Block , Crate } ;
36
35
use syntax:: ast:: { ForeignItem , ForeignItemKind , Item , ItemKind } ;
37
- use syntax:: ast:: { Mutability , StmtKind , TraitItemKind } ;
36
+ use syntax:: ast:: { Mutability , StmtKind , TraitItem , TraitItemKind } ;
38
37
use syntax:: ast:: { Variant , ViewPathGlob , ViewPathList , ViewPathSimple } ;
39
38
use syntax:: parse:: token:: keywords;
40
39
use syntax:: visit:: { self , Visitor } ;
@@ -56,8 +55,6 @@ impl<'a> ToNameBinding<'a> for (Def, Span, ty::Visibility) {
56
55
impl < ' b > Resolver < ' b > {
57
56
/// Constructs the reduced graph for the entire crate.
58
57
pub fn build_reduced_graph ( & mut self , krate : & Crate ) {
59
- let no_implicit_prelude = attr:: contains_name ( & krate. attrs , "no_implicit_prelude" ) ;
60
- self . graph_root . no_implicit_prelude . set ( no_implicit_prelude) ;
61
58
visit:: walk_crate ( & mut BuildReducedGraphVisitor { resolver : self } , krate) ;
62
59
}
63
60
@@ -196,22 +193,25 @@ impl<'b> Resolver<'b> {
196
193
krate : crate_id,
197
194
index : CRATE_DEF_INDEX ,
198
195
} ;
199
- let parent_link = ModuleParentLink ( parent, name) ;
200
- let def = Def :: Mod ( def_id) ;
201
- let module = self . new_extern_crate_module ( parent_link, def, item. id ) ;
196
+ let module = self . arenas . alloc_module ( ModuleS {
197
+ extern_crate_id : Some ( item. id ) ,
198
+ populated : Cell :: new ( false ) ,
199
+ ..ModuleS :: new ( Some ( parent) , ModuleKind :: Def ( Def :: Mod ( def_id) , name) )
200
+ } ) ;
202
201
self . define ( parent, name, TypeNS , ( module, sp, vis) ) ;
203
202
204
203
self . populate_module_if_necessary ( module) ;
205
204
}
206
205
}
207
206
208
207
ItemKind :: Mod ( ..) => {
209
- let parent_link = ModuleParentLink ( parent, name) ;
210
208
let def = Def :: Mod ( self . definitions . local_def_id ( item. id ) ) ;
211
- let module = self . new_module ( parent_link, Some ( def) , Some ( item. id ) ) ;
212
- module. no_implicit_prelude . set ( {
213
- parent. no_implicit_prelude . get ( ) ||
209
+ let module = self . arenas . alloc_module ( ModuleS {
210
+ no_implicit_prelude : parent. no_implicit_prelude || {
214
211
attr:: contains_name ( & item. attrs , "no_implicit_prelude" )
212
+ } ,
213
+ normal_ancestor_id : Some ( item. id ) ,
214
+ ..ModuleS :: new ( Some ( parent) , ModuleKind :: Def ( def, name) )
215
215
} ) ;
216
216
self . define ( parent, name, TypeNS , ( module, sp, vis) ) ;
217
217
self . module_map . insert ( item. id , module) ;
@@ -244,9 +244,8 @@ impl<'b> Resolver<'b> {
244
244
}
245
245
246
246
ItemKind :: Enum ( ref enum_definition, _) => {
247
- let parent_link = ModuleParentLink ( parent, name) ;
248
247
let def = Def :: Enum ( self . definitions . local_def_id ( item. id ) ) ;
249
- let module = self . new_module ( parent_link , Some ( def) , parent . normal_ancestor_id ) ;
248
+ let module = self . new_module ( parent , ModuleKind :: Def ( def, name ) , true ) ;
250
249
self . define ( parent, name, TypeNS , ( module, sp, vis) ) ;
251
250
252
251
for variant in & ( * enum_definition) . variants {
@@ -293,40 +292,17 @@ impl<'b> Resolver<'b> {
293
292
294
293
ItemKind :: DefaultImpl ( ..) | ItemKind :: Impl ( ..) => { }
295
294
296
- ItemKind :: Trait ( .., ref items ) => {
295
+ ItemKind :: Trait ( ..) => {
297
296
let def_id = self . definitions . local_def_id ( item. id ) ;
298
297
299
298
// Add all the items within to a new module.
300
- let parent_link = ModuleParentLink ( parent, name) ;
301
- let def = Def :: Trait ( def_id) ;
302
- let module_parent =
303
- self . new_module ( parent_link, Some ( def) , parent. normal_ancestor_id ) ;
304
- self . define ( parent, name, TypeNS , ( module_parent, sp, vis) ) ;
305
-
306
- // Add the names of all the items to the trait info.
307
- for item in items {
308
- let item_def_id = self . definitions . local_def_id ( item. id ) ;
309
- let mut is_static_method = false ;
310
- let ( def, ns) = match item. node {
311
- TraitItemKind :: Const ( ..) => ( Def :: AssociatedConst ( item_def_id) , ValueNS ) ,
312
- TraitItemKind :: Method ( ref sig, _) => {
313
- is_static_method = !sig. decl . has_self ( ) ;
314
- ( Def :: Method ( item_def_id) , ValueNS )
315
- }
316
- TraitItemKind :: Type ( ..) => ( Def :: AssociatedTy ( item_def_id) , TypeNS ) ,
317
- TraitItemKind :: Macro ( _) => panic ! ( "unexpanded macro in resolve!" ) ,
318
- } ;
319
-
320
- self . define ( module_parent, item. ident . name , ns, ( def, item. span , vis) ) ;
321
-
322
- self . trait_item_map . insert ( ( item. ident . name , def_id) , is_static_method) ;
323
- }
299
+ let module =
300
+ self . new_module ( parent, ModuleKind :: Def ( Def :: Trait ( def_id) , name) , true ) ;
301
+ self . define ( parent, name, TypeNS , ( module, sp, vis) ) ;
302
+ self . current_module = module;
324
303
}
325
304
ItemKind :: Mac ( _) => panic ! ( "unexpanded macro in resolve!" ) ,
326
305
}
327
-
328
- visit:: walk_item ( & mut BuildReducedGraphVisitor { resolver : self } , item) ;
329
- self . current_module = parent;
330
306
}
331
307
332
308
// Constructs the reduced graph for one variant. Variants exist in the
@@ -375,14 +351,10 @@ impl<'b> Resolver<'b> {
375
351
{}",
376
352
block_id) ;
377
353
378
- let parent_link = BlockParentLink ( parent, block_id) ;
379
- let new_module = self . new_module ( parent_link, None , parent. normal_ancestor_id ) ;
354
+ let new_module = self . new_module ( parent, ModuleKind :: Block ( block_id) , true ) ;
380
355
self . module_map . insert ( block_id, new_module) ;
381
356
self . current_module = new_module; // Descend into the block.
382
357
}
383
-
384
- visit:: walk_block ( & mut BuildReducedGraphVisitor { resolver : self } , block) ;
385
- self . current_module = parent;
386
358
}
387
359
388
360
/// Builds the reduced graph for a single item in an external crate.
@@ -407,8 +379,7 @@ impl<'b> Resolver<'b> {
407
379
Def :: Mod ( _) | Def :: Enum ( ..) => {
408
380
debug ! ( "(building reduced graph for external crate) building module {} {:?}" ,
409
381
name, vis) ;
410
- let parent_link = ModuleParentLink ( parent, name) ;
411
- let module = self . new_module ( parent_link, Some ( def) , None ) ;
382
+ let module = self . new_module ( parent, ModuleKind :: Def ( def, name) , false ) ;
412
383
let _ = self . try_define ( parent, name, TypeNS , ( module, DUMMY_SP , vis) ) ;
413
384
}
414
385
Def :: Variant ( variant_id) => {
@@ -451,8 +422,7 @@ impl<'b> Resolver<'b> {
451
422
self . trait_item_map . insert ( ( trait_item_name, def_id) , false ) ;
452
423
}
453
424
454
- let parent_link = ModuleParentLink ( parent, name) ;
455
- let module = self . new_module ( parent_link, Some ( def) , None ) ;
425
+ let module = self . new_module ( parent, ModuleKind :: Def ( def, name) , false ) ;
456
426
let _ = self . try_define ( parent, name, TypeNS , ( module, DUMMY_SP , vis) ) ;
457
427
}
458
428
Def :: TyAlias ( ..) | Def :: AssociatedTy ( ..) => {
@@ -512,14 +482,47 @@ struct BuildReducedGraphVisitor<'a, 'b: 'a> {
512
482
513
483
impl < ' a , ' b > Visitor for BuildReducedGraphVisitor < ' a , ' b > {
514
484
fn visit_item ( & mut self , item : & Item ) {
485
+ let parent = self . resolver . current_module ;
515
486
self . resolver . build_reduced_graph_for_item ( item) ;
487
+ visit:: walk_item ( self , item) ;
488
+ self . resolver . current_module = parent;
516
489
}
517
490
518
491
fn visit_foreign_item ( & mut self , foreign_item : & ForeignItem ) {
519
492
self . resolver . build_reduced_graph_for_foreign_item ( foreign_item) ;
520
493
}
521
494
522
495
fn visit_block ( & mut self , block : & Block ) {
496
+ let parent = self . resolver . current_module ;
523
497
self . resolver . build_reduced_graph_for_block ( block) ;
498
+ visit:: walk_block ( self , block) ;
499
+ self . resolver . current_module = parent;
500
+ }
501
+
502
+ fn visit_trait_item ( & mut self , item : & TraitItem ) {
503
+ let parent = self . resolver . current_module ;
504
+ let def_id = parent. def_id ( ) . unwrap ( ) ;
505
+
506
+ // Add the item to the trait info.
507
+ let item_def_id = self . resolver . definitions . local_def_id ( item. id ) ;
508
+ let mut is_static_method = false ;
509
+ let ( def, ns) = match item. node {
510
+ TraitItemKind :: Const ( ..) => ( Def :: AssociatedConst ( item_def_id) , ValueNS ) ,
511
+ TraitItemKind :: Method ( ref sig, _) => {
512
+ is_static_method = !sig. decl . has_self ( ) ;
513
+ ( Def :: Method ( item_def_id) , ValueNS )
514
+ }
515
+ TraitItemKind :: Type ( ..) => ( Def :: AssociatedTy ( item_def_id) , TypeNS ) ,
516
+ TraitItemKind :: Macro ( _) => panic ! ( "unexpanded macro in resolve!" ) ,
517
+ } ;
518
+
519
+ self . resolver . trait_item_map . insert ( ( item. ident . name , def_id) , is_static_method) ;
520
+
521
+ let vis = ty:: Visibility :: Public ;
522
+ self . resolver . define ( parent, item. ident . name , ns, ( def, item. span , vis) ) ;
523
+
524
+ self . resolver . current_module = parent. parent . unwrap ( ) ; // nearest normal ancestor
525
+ visit:: walk_trait_item ( self , item) ;
526
+ self . resolver . current_module = parent;
524
527
}
525
528
}
0 commit comments