@@ -471,12 +471,18 @@ pub struct Module {
471
471
472
472
// The index of the import we're resolving.
473
473
resolved_import_count : uint ,
474
+
475
+ // Whether this module is populated. If not populated, any attempt to
476
+ // access the children must be preceded with a
477
+ // `populate_module_if_necessary` call.
478
+ populated : bool ,
474
479
}
475
480
476
481
pub fn Module ( parent_link : ParentLink ,
477
482
def_id : Option < def_id > ,
478
- kind : ModuleKind )
479
- -> Module {
483
+ kind : ModuleKind ,
484
+ external : bool )
485
+ -> Module {
480
486
Module {
481
487
parent_link : parent_link,
482
488
def_id : def_id,
@@ -488,6 +494,7 @@ pub fn Module(parent_link: ParentLink,
488
494
import_resolutions : @mut HashMap :: new ( ) ,
489
495
glob_count : 0 ,
490
496
resolved_import_count : 0 ,
497
+ populated : !external,
491
498
}
492
499
}
493
500
@@ -534,9 +541,10 @@ impl NameBindings {
534
541
parent_link : ParentLink ,
535
542
def_id : Option < def_id > ,
536
543
kind : ModuleKind ,
544
+ external : bool ,
537
545
sp : span ) {
538
546
// Merges the module with the existing type def or creates a new one.
539
- let module_ = @mut Module ( parent_link, def_id, kind) ;
547
+ let module_ = @mut Module ( parent_link, def_id, kind, external ) ;
540
548
match self . type_def {
541
549
None => {
542
550
self . type_def = Some ( TypeNsDef {
@@ -563,10 +571,11 @@ impl NameBindings {
563
571
parent_link : ParentLink ,
564
572
def_id : Option < def_id > ,
565
573
kind : ModuleKind ,
574
+ external : bool ,
566
575
_sp : span ) {
567
576
match self . type_def {
568
577
None => {
569
- let module = @mut Module ( parent_link, def_id, kind) ;
578
+ let module = @mut Module ( parent_link, def_id, kind, external ) ;
570
579
self . type_def = Some ( TypeNsDef {
571
580
privacy : privacy,
572
581
module_def : Some ( module) ,
@@ -577,7 +586,10 @@ impl NameBindings {
577
586
Some ( type_def) => {
578
587
match type_def. module_def {
579
588
None => {
580
- let module = @mut Module ( parent_link, def_id, kind) ;
589
+ let module = @mut Module ( parent_link,
590
+ def_id,
591
+ kind,
592
+ external) ;
581
593
self . type_def = Some ( TypeNsDef {
582
594
privacy : privacy,
583
595
module_def : Some ( module) ,
@@ -799,6 +811,7 @@ pub fn Resolver(session: Session,
799
811
NoParentLink ,
800
812
Some ( def_id { crate : 0 , node : 0 } ) ,
801
813
NormalModuleKind ,
814
+ false ,
802
815
crate . span) ;
803
816
804
817
let current_module = graph_root. get_module ( ) ;
@@ -1164,6 +1177,7 @@ impl Resolver {
1164
1177
parent_link,
1165
1178
Some ( def_id) ,
1166
1179
NormalModuleKind ,
1180
+ false ,
1167
1181
sp) ;
1168
1182
1169
1183
let new_parent =
@@ -1186,6 +1200,7 @@ impl Resolver {
1186
1200
parent_link,
1187
1201
Some ( def_id) ,
1188
1202
ExternModuleKind ,
1203
+ false ,
1189
1204
sp) ;
1190
1205
1191
1206
ModuleReducedGraphParent ( name_bindings. get_module ( ) )
@@ -1310,6 +1325,7 @@ impl Resolver {
1310
1325
parent_link,
1311
1326
Some ( def_id) ,
1312
1327
ImplModuleKind ,
1328
+ false ,
1313
1329
sp) ;
1314
1330
1315
1331
ModuleReducedGraphParent (
@@ -1367,6 +1383,7 @@ impl Resolver {
1367
1383
parent_link,
1368
1384
Some ( local_def ( item. id ) ) ,
1369
1385
TraitModuleKind ,
1386
+ false ,
1370
1387
sp) ;
1371
1388
let module_parent = ModuleReducedGraphParent ( name_bindings.
1372
1389
get_module ( ) ) ;
@@ -1556,7 +1573,8 @@ impl Resolver {
1556
1573
( self . get_module_from_parent ( parent) , name) ;
1557
1574
let external_module = @mut Module ( parent_link,
1558
1575
Some ( def_id) ,
1559
- NormalModuleKind ) ;
1576
+ NormalModuleKind ,
1577
+ false ) ;
1560
1578
1561
1579
parent. external_module_children . insert (
1562
1580
name,
@@ -1620,7 +1638,8 @@ impl Resolver {
1620
1638
let new_module = @mut Module (
1621
1639
BlockParentLink ( parent_module, block_id) ,
1622
1640
None ,
1623
- AnonymousModuleKind ) ;
1641
+ AnonymousModuleKind ,
1642
+ false ) ;
1624
1643
parent_module. anonymous_children . insert ( block_id, new_module) ;
1625
1644
new_parent = ModuleReducedGraphParent ( new_module) ;
1626
1645
} else {
@@ -1656,6 +1675,7 @@ impl Resolver {
1656
1675
parent_link,
1657
1676
Some ( def_id) ,
1658
1677
NormalModuleKind ,
1678
+ true ,
1659
1679
dummy_sp ( ) ) ;
1660
1680
}
1661
1681
}
@@ -1717,6 +1737,7 @@ impl Resolver {
1717
1737
parent_link,
1718
1738
Some ( def_id) ,
1719
1739
TraitModuleKind ,
1740
+ true ,
1720
1741
dummy_sp ( ) )
1721
1742
}
1722
1743
def_ty( _) => {
@@ -1755,8 +1776,19 @@ impl Resolver {
1755
1776
match def_like {
1756
1777
dl_def( def) => {
1757
1778
// Add the new child item, if necessary.
1758
- let optional_module = match def {
1759
- def_foreign_mod( * ) => Some ( root) ,
1779
+ match def {
1780
+ def_foreign_mod( def_id) => {
1781
+ // Foreign modules have no names. Recur and populate
1782
+ // eagerly.
1783
+ do csearch:: each_child_of_item ( self . session . cstore ,
1784
+ def_id)
1785
+ |def_like, child_ident| {
1786
+ self . build_reduced_graph_for_external_crate_def (
1787
+ root,
1788
+ def_like,
1789
+ child_ident)
1790
+ }
1791
+ }
1760
1792
_ => {
1761
1793
let ( child_name_bindings, new_parent) =
1762
1794
self . add_child ( ident,
@@ -1770,28 +1802,6 @@ impl Resolver {
1770
1802
self . session . str_of ( ident) ,
1771
1803
ident,
1772
1804
new_parent) ;
1773
-
1774
- /*println(fmt!(">>> child item added: %s",
1775
- self.session.str_of(ident)));*/
1776
-
1777
- child_name_bindings. get_module_if_available ( )
1778
- }
1779
- } ;
1780
-
1781
- match optional_module {
1782
- None => { }
1783
- Some ( module) => {
1784
- do csearch:: each_child_of_item ( self . session . cstore ,
1785
- def_id_of_def ( def) )
1786
- |def_like, child_ident| {
1787
- /*println(fmt!(">>> each_child_of_item: %s %s",
1788
- self.session.str_of(ident),
1789
- self.session.str_of(child_ident)));*/
1790
- self . build_reduced_graph_for_external_crate_def (
1791
- module,
1792
- def_like,
1793
- child_ident)
1794
- }
1795
1805
}
1796
1806
}
1797
1807
}
@@ -1844,6 +1854,7 @@ impl Resolver {
1844
1854
parent_link,
1845
1855
Some ( def) ,
1846
1856
ImplModuleKind ,
1857
+ true ,
1847
1858
dummy_sp ( ) ) ;
1848
1859
type_module =
1849
1860
child_name_bindings.
@@ -1890,6 +1901,31 @@ impl Resolver {
1890
1901
}
1891
1902
}
1892
1903
1904
+ /// Builds the reduced graph rooted at the given external module.
1905
+ fn populate_external_module ( @mut self , module : @mut Module ) {
1906
+ let def_id = match module. def_id {
1907
+ None => return ,
1908
+ Some ( def_id) => def_id,
1909
+ } ;
1910
+
1911
+ do csearch:: each_child_of_item ( self . session . cstore , def_id)
1912
+ |def_like, child_ident| {
1913
+ self . build_reduced_graph_for_external_crate_def ( module,
1914
+ def_like,
1915
+ child_ident)
1916
+ }
1917
+ module. populated = true
1918
+ }
1919
+
1920
+ /// Ensures that the reduced graph rooted at the given external module
1921
+ /// is built, building it if it is not.
1922
+ fn populate_module_if_necessary ( @mut self , module : @mut Module ) {
1923
+ if !module. populated {
1924
+ self . populate_external_module ( module)
1925
+ }
1926
+ assert ! ( module. populated)
1927
+ }
1928
+
1893
1929
/// Builds the reduced graph rooted at the 'use' directive for an external
1894
1930
/// crate.
1895
1931
pub fn build_reduced_graph_for_external_crate ( @mut self ,
@@ -1999,6 +2035,7 @@ impl Resolver {
1999
2035
self . module_to_str( module_) ) ;
2000
2036
self . resolve_imports_for_module ( module_) ;
2001
2037
2038
+ self . populate_module_if_necessary ( module_) ;
2002
2039
for ( _, & child_node) in module_. children . iter ( ) {
2003
2040
match child_node. get_module_if_available ( ) {
2004
2041
None => {
@@ -2224,6 +2261,7 @@ impl Resolver {
2224
2261
let mut type_result = UnknownResult ;
2225
2262
2226
2263
// Search for direct children of the containing module.
2264
+ self . populate_module_if_necessary ( containing_module) ;
2227
2265
match containing_module. children . find ( & source) {
2228
2266
None => {
2229
2267
// Continue.
@@ -2542,6 +2580,7 @@ impl Resolver {
2542
2580
} ;
2543
2581
2544
2582
// Add all children from the containing module.
2583
+ self . populate_module_if_necessary ( containing_module) ;
2545
2584
for ( & ident, name_bindings) in containing_module. children . iter ( ) {
2546
2585
merge_import_resolution ( ident, * name_bindings) ;
2547
2586
}
@@ -2775,6 +2814,7 @@ impl Resolver {
2775
2814
2776
2815
// The current module node is handled specially. First, check for
2777
2816
// its immediate children.
2817
+ self . populate_module_if_necessary ( module_) ;
2778
2818
match module_. children . find ( & name) {
2779
2819
Some ( name_bindings)
2780
2820
if name_bindings. defined_in_namespace ( namespace) => {
@@ -3029,6 +3069,7 @@ impl Resolver {
3029
3069
self . module_to_str( module_) ) ;
3030
3070
3031
3071
// First, check the direct children of the module.
3072
+ self . populate_module_if_necessary ( module_) ;
3032
3073
match module_. children . find ( & name) {
3033
3074
Some ( name_bindings)
3034
3075
if name_bindings. defined_in_namespace ( namespace) => {
@@ -3118,6 +3159,7 @@ impl Resolver {
3118
3159
}
3119
3160
3120
3161
// Descend into children and anonymous children.
3162
+ self . populate_module_if_necessary ( module_) ;
3121
3163
for ( _, & child_node) in module_. children . iter ( ) {
3122
3164
match child_node. get_module_if_available ( ) {
3123
3165
None => {
@@ -3176,6 +3218,7 @@ impl Resolver {
3176
3218
}
3177
3219
3178
3220
self . record_exports_for_module ( module_) ;
3221
+ self . populate_module_if_necessary ( module_) ;
3179
3222
3180
3223
for ( _, & child_name_bindings) in module_. children . iter ( ) {
3181
3224
match child_name_bindings. get_module_if_available ( ) {
@@ -3289,6 +3332,7 @@ impl Resolver {
3289
3332
// Nothing to do.
3290
3333
}
3291
3334
Some ( name) => {
3335
+ self . populate_module_if_necessary ( orig_module) ;
3292
3336
match orig_module. children . find ( & name) {
3293
3337
None => {
3294
3338
debug ! ( "!!! (with scope) didn't find `%s` in `%s`" ,
@@ -4569,6 +4613,7 @@ impl Resolver {
4569
4613
xray : XrayFlag )
4570
4614
-> NameDefinition {
4571
4615
// First, search children.
4616
+ self . populate_module_if_necessary ( containing_module) ;
4572
4617
match containing_module. children . find ( & name) {
4573
4618
Some ( child_name_bindings) => {
4574
4619
match ( child_name_bindings. def_for_namespace ( namespace) ,
@@ -5233,7 +5278,9 @@ impl Resolver {
5233
5278
}
5234
5279
5235
5280
// Look for trait children.
5236
- for ( _, & child_name_bindings) in search_module. children . iter ( ) {
5281
+ self . populate_module_if_necessary ( search_module) ;
5282
+ for ( _, & child_name_bindings) in
5283
+ search_module. children . iter ( ) {
5237
5284
match child_name_bindings. def_for_namespace ( TypeNS ) {
5238
5285
Some ( def) => {
5239
5286
match def {
@@ -5432,6 +5479,7 @@ impl Resolver {
5432
5479
debug ! ( "Dump of module `%s`:" , self . module_to_str( module_) ) ;
5433
5480
5434
5481
debug ! ( "Children:" ) ;
5482
+ self . populate_module_if_necessary ( module_) ;
5435
5483
for ( & name, _) in module_. children . iter ( ) {
5436
5484
debug ! ( "* %s" , self . session. str_of( name) ) ;
5437
5485
}
0 commit comments