@@ -85,6 +85,8 @@ fn lower_span(raw_span: &raw::SpanData, base_dir: &Path) -> Span {
85
85
}
86
86
87
87
struct CrateReader {
88
+ /// This is effectively a map from local crate id -> global crate id, where
89
+ /// local crate id are indices 0...external_crate_count.
88
90
crate_map : Vec < u32 > ,
89
91
base_dir : PathBuf ,
90
92
crate_name : String ,
@@ -96,23 +98,27 @@ impl CrateReader {
96
98
master_crate_map : & mut HashMap < String , u32 > ,
97
99
base_dir : & Path ,
98
100
) -> CrateReader {
99
- // println!("building crate map for {}", crate_name);
100
- let next = master_crate_map. len ( ) as u32 ;
101
- let mut crate_map = vec ! [
102
- * master_crate_map
103
- . entry( prelude. crate_name. clone( ) )
104
- . or_insert_with( || next) ,
105
- ] ;
106
- // println!(" {} -> {}", crate_name, master_crate_map[&crate_name]);
101
+ fn fetch_crate_id ( map : & mut HashMap < String , u32 > , crate_name : & String ) -> u32 {
102
+ let next = map. len ( ) as u32 ;
103
+ * map. entry ( crate_name. clone ( ) ) . or_insert ( next)
104
+ }
105
+ // When reading a local crate and its external crates, we need to:
106
+ // 1. Update a global crate id map if we encounter any new crate
107
+ // 2. Prepare a local crate id -> global crate id map, so we can easily
108
+ // map those when lowering symbols with local crate ids into global registry
109
+ // It's worth noting, that we assume that local crate id is 0, whereas
110
+ // the external crates will have num in 1..count contiguous range.
111
+ trace ! ( "building crate map for {}" , prelude. crate_name) ;
112
+ let id = fetch_crate_id ( master_crate_map, & prelude. crate_name ) ;
113
+ let mut crate_map = vec ! [ id] ;
114
+ trace ! ( " {} -> {}" , prelude. crate_name, master_crate_map[ & prelude. crate_name] ) ;
107
115
108
116
prelude. external_crates . sort_by ( |a, b| a. num . cmp ( & b. num ) ) ;
109
117
for c in prelude. external_crates {
110
118
assert ! ( c. num == crate_map. len( ) as u32 ) ;
111
- let next = master_crate_map. len ( ) as u32 ;
112
- crate_map. push ( * master_crate_map
113
- . entry ( c. name . clone ( ) )
114
- . or_insert_with ( || next) ) ;
115
- // println!(" {} -> {}", c.name, master_crate_map[&c.name]);
119
+ let id = fetch_crate_id ( master_crate_map, & c. name ) ;
120
+ crate_map. push ( id) ;
121
+ trace ! ( " {} -> {}" , c. name, master_crate_map[ & c. name] ) ;
116
122
}
117
123
118
124
CrateReader {
@@ -221,7 +227,7 @@ impl CrateReader {
221
227
docs : d. docs ,
222
228
// sig: d.sig.map(|ref s| self.lower_sig(s, &self.base_dir)),
223
229
} ;
224
- info ! (
230
+ trace ! (
225
231
"record def: {:?}/{:?} ({}): {:?}" ,
226
232
id,
227
233
d. id,
@@ -334,15 +340,15 @@ impl CrateReader {
334
340
// }
335
341
// }
336
342
343
+ /// Recreates resulting crate-local (u32, u32) id from compiler to a global
344
+ /// `u64` `Id`, mapping from a local to global crate id.
337
345
fn id_from_compiler_id ( & self , id : & data:: Id ) -> Id {
338
346
if id. krate == u32:: MAX || id. index == u32:: MAX {
339
347
return NULL ;
340
348
}
341
- // We build an id by looking up the local crate number into a global crate number and using
342
- // that for the high order bits, then use the index for the least significant bits.
343
349
344
350
let krate = self . crate_map [ id. krate as usize ] as u64 ;
345
-
351
+ // Use global crate number for high order bits, then index for least significant bits.
346
352
Id ( ( krate << 32 ) | ( id. index as u64 ) )
347
353
}
348
354
}
0 commit comments