1414//! any imports resolved.
1515
1616use resolve_imports:: ImportDirectiveSubclass :: { self , GlobImport } ;
17- use Module ;
17+ use { Module , ModuleS , ModuleKind } ;
1818use Namespace :: { self , TypeNS , ValueNS } ;
1919use { NameBinding , NameBindingKind , ToNameBinding } ;
20- use ParentLink :: { ModuleParentLink , BlockParentLink } ;
2120use Resolver ;
2221use { resolve_error, resolve_struct_error, ResolutionError } ;
2322
@@ -34,7 +33,7 @@ use syntax::parse::token;
3433
3534use syntax:: ast:: { Block , Crate } ;
3635use syntax:: ast:: { ForeignItem , ForeignItemKind , Item , ItemKind } ;
37- use syntax:: ast:: { Mutability , StmtKind , TraitItemKind } ;
36+ use syntax:: ast:: { Mutability , StmtKind , TraitItem , TraitItemKind } ;
3837use syntax:: ast:: { Variant , ViewPathGlob , ViewPathList , ViewPathSimple } ;
3938use syntax:: parse:: token:: keywords;
4039use syntax:: visit:: { self , Visitor } ;
@@ -56,8 +55,6 @@ impl<'a> ToNameBinding<'a> for (Def, Span, ty::Visibility) {
5655impl < ' b > Resolver < ' b > {
5756 /// Constructs the reduced graph for the entire crate.
5857 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) ;
6158 visit:: walk_crate ( & mut BuildReducedGraphVisitor { resolver : self } , krate) ;
6259 }
6360
@@ -196,22 +193,25 @@ impl<'b> Resolver<'b> {
196193 krate : crate_id,
197194 index : CRATE_DEF_INDEX ,
198195 } ;
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+ } ) ;
202201 self . define ( parent, name, TypeNS , ( module, sp, vis) ) ;
203202
204203 self . populate_module_if_necessary ( module) ;
205204 }
206205 }
207206
208207 ItemKind :: Mod ( ..) => {
209- let parent_link = ModuleParentLink ( parent, name) ;
210208 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 || {
214211 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) )
215215 } ) ;
216216 self . define ( parent, name, TypeNS , ( module, sp, vis) ) ;
217217 self . module_map . insert ( item. id , module) ;
@@ -244,9 +244,8 @@ impl<'b> Resolver<'b> {
244244 }
245245
246246 ItemKind :: Enum ( ref enum_definition, _) => {
247- let parent_link = ModuleParentLink ( parent, name) ;
248247 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 ) ;
250249 self . define ( parent, name, TypeNS , ( module, sp, vis) ) ;
251250
252251 for variant in & ( * enum_definition) . variants {
@@ -293,40 +292,17 @@ impl<'b> Resolver<'b> {
293292
294293 ItemKind :: DefaultImpl ( ..) | ItemKind :: Impl ( ..) => { }
295294
296- ItemKind :: Trait ( .., ref items ) => {
295+ ItemKind :: Trait ( ..) => {
297296 let def_id = self . definitions . local_def_id ( item. id ) ;
298297
299298 // 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;
324303 }
325304 ItemKind :: Mac ( _) => panic ! ( "unexpanded macro in resolve!" ) ,
326305 }
327-
328- visit:: walk_item ( & mut BuildReducedGraphVisitor { resolver : self } , item) ;
329- self . current_module = parent;
330306 }
331307
332308 // Constructs the reduced graph for one variant. Variants exist in the
@@ -375,14 +351,10 @@ impl<'b> Resolver<'b> {
375351 {}",
376352 block_id) ;
377353
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 ) ;
380355 self . module_map . insert ( block_id, new_module) ;
381356 self . current_module = new_module; // Descend into the block.
382357 }
383-
384- visit:: walk_block ( & mut BuildReducedGraphVisitor { resolver : self } , block) ;
385- self . current_module = parent;
386358 }
387359
388360 /// Builds the reduced graph for a single item in an external crate.
@@ -407,8 +379,7 @@ impl<'b> Resolver<'b> {
407379 Def :: Mod ( _) | Def :: Enum ( ..) => {
408380 debug ! ( "(building reduced graph for external crate) building module {} {:?}" ,
409381 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 ) ;
412383 let _ = self . try_define ( parent, name, TypeNS , ( module, DUMMY_SP , vis) ) ;
413384 }
414385 Def :: Variant ( variant_id) => {
@@ -451,8 +422,7 @@ impl<'b> Resolver<'b> {
451422 self . trait_item_map . insert ( ( trait_item_name, def_id) , false ) ;
452423 }
453424
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 ) ;
456426 let _ = self . try_define ( parent, name, TypeNS , ( module, DUMMY_SP , vis) ) ;
457427 }
458428 Def :: TyAlias ( ..) | Def :: AssociatedTy ( ..) => {
@@ -512,14 +482,47 @@ struct BuildReducedGraphVisitor<'a, 'b: 'a> {
512482
513483impl < ' a , ' b > Visitor for BuildReducedGraphVisitor < ' a , ' b > {
514484 fn visit_item ( & mut self , item : & Item ) {
485+ let parent = self . resolver . current_module ;
515486 self . resolver . build_reduced_graph_for_item ( item) ;
487+ visit:: walk_item ( self , item) ;
488+ self . resolver . current_module = parent;
516489 }
517490
518491 fn visit_foreign_item ( & mut self , foreign_item : & ForeignItem ) {
519492 self . resolver . build_reduced_graph_for_foreign_item ( foreign_item) ;
520493 }
521494
522495 fn visit_block ( & mut self , block : & Block ) {
496+ let parent = self . resolver . current_module ;
523497 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;
524527 }
525528}
0 commit comments