@@ -85,6 +85,8 @@ fn lower_span(raw_span: &raw::SpanData, base_dir: &Path) -> Span {
8585}
8686
8787struct 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.
8890 crate_map : Vec < u32 > ,
8991 base_dir : PathBuf ,
9092 crate_name : String ,
@@ -96,23 +98,27 @@ impl CrateReader {
9698 master_crate_map : & mut HashMap < String , u32 > ,
9799 base_dir : & Path ,
98100 ) -> 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] ) ;
107115
108116 prelude. external_crates . sort_by ( |a, b| a. num . cmp ( & b. num ) ) ;
109117 for c in prelude. external_crates {
110118 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] ) ;
116122 }
117123
118124 CrateReader {
@@ -221,7 +227,7 @@ impl CrateReader {
221227 docs : d. docs ,
222228 // sig: d.sig.map(|ref s| self.lower_sig(s, &self.base_dir)),
223229 } ;
224- info ! (
230+ trace ! (
225231 "record def: {:?}/{:?} ({}): {:?}" ,
226232 id,
227233 d. id,
@@ -334,15 +340,15 @@ impl CrateReader {
334340 // }
335341 // }
336342
343+ /// Recreates resulting crate-local (u32, u32) id from compiler to a global
344+ /// `u64` `Id`, mapping from a local to global crate id.
337345 fn id_from_compiler_id ( & self , id : & data:: Id ) -> Id {
338346 if id. krate == u32:: MAX || id. index == u32:: MAX {
339347 return NULL ;
340348 }
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.
343349
344350 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.
346352 Id ( ( krate << 32 ) | ( id. index as u64 ) )
347353 }
348354}
0 commit comments