1
1
// Decoding metadata from a single crate's metadata
2
2
3
+ use crate :: creader:: CrateMetadataRef ;
3
4
use crate :: rmeta:: table:: { FixedSizeEncoding , Table } ;
4
5
use crate :: rmeta:: * ;
5
6
@@ -125,7 +126,7 @@ struct ImportedSourceFile {
125
126
126
127
pub ( super ) struct DecodeContext < ' a , ' tcx > {
127
128
opaque : opaque:: Decoder < ' a > ,
128
- cdata : Option < & ' a CrateMetadata > ,
129
+ cdata : Option < CrateMetadataRef < ' a > > ,
129
130
sess : Option < & ' tcx Session > ,
130
131
tcx : Option < TyCtxt < ' tcx > > ,
131
132
@@ -141,7 +142,7 @@ pub(super) struct DecodeContext<'a, 'tcx> {
141
142
/// Abstract over the various ways one can create metadata decoders.
142
143
pub ( super ) trait Metadata < ' a , ' tcx > : Copy {
143
144
fn raw_bytes ( self ) -> & ' a [ u8 ] ;
144
- fn cdata ( self ) -> Option < & ' a CrateMetadata > {
145
+ fn cdata ( self ) -> Option < CrateMetadataRef < ' a > > {
145
146
None
146
147
}
147
148
fn sess ( self ) -> Option < & ' tcx Session > {
@@ -162,7 +163,7 @@ pub(super) trait Metadata<'a, 'tcx>: Copy {
162
163
lazy_state : LazyState :: NoNode ,
163
164
alloc_decoding_session : self
164
165
. cdata ( )
165
- . map ( |cdata| cdata. alloc_decoding_state . new_decoding_session ( ) ) ,
166
+ . map ( |cdata| cdata. cdata . alloc_decoding_state . new_decoding_session ( ) ) ,
166
167
}
167
168
}
168
169
}
@@ -185,33 +186,33 @@ impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a MetadataBlob, &'tcx Session) {
185
186
}
186
187
}
187
188
188
- impl < ' a , ' tcx > Metadata < ' a , ' tcx > for & ' a CrateMetadata {
189
+ impl < ' a , ' tcx > Metadata < ' a , ' tcx > for & ' a CrateMetadataRef < ' a > {
189
190
fn raw_bytes ( self ) -> & ' a [ u8 ] {
190
191
self . blob . raw_bytes ( )
191
192
}
192
- fn cdata ( self ) -> Option < & ' a CrateMetadata > {
193
- Some ( self )
193
+ fn cdata ( self ) -> Option < CrateMetadataRef < ' a > > {
194
+ Some ( * self )
194
195
}
195
196
}
196
197
197
- impl < ' a , ' tcx > Metadata < ' a , ' tcx > for ( & ' a CrateMetadata , & ' tcx Session ) {
198
+ impl < ' a , ' tcx > Metadata < ' a , ' tcx > for ( & ' a CrateMetadataRef < ' a > , & ' tcx Session ) {
198
199
fn raw_bytes ( self ) -> & ' a [ u8 ] {
199
200
self . 0 . raw_bytes ( )
200
201
}
201
- fn cdata ( self ) -> Option < & ' a CrateMetadata > {
202
- Some ( self . 0 )
202
+ fn cdata ( self ) -> Option < CrateMetadataRef < ' a > > {
203
+ Some ( * self . 0 )
203
204
}
204
205
fn sess ( self ) -> Option < & ' tcx Session > {
205
206
Some ( & self . 1 )
206
207
}
207
208
}
208
209
209
- impl < ' a , ' tcx > Metadata < ' a , ' tcx > for ( & ' a CrateMetadata , TyCtxt < ' tcx > ) {
210
+ impl < ' a , ' tcx > Metadata < ' a , ' tcx > for ( & ' a CrateMetadataRef < ' a > , TyCtxt < ' tcx > ) {
210
211
fn raw_bytes ( self ) -> & ' a [ u8 ] {
211
212
self . 0 . raw_bytes ( )
212
213
}
213
- fn cdata ( self ) -> Option < & ' a CrateMetadata > {
214
- Some ( self . 0 )
214
+ fn cdata ( self ) -> Option < CrateMetadataRef < ' a > > {
215
+ Some ( * self . 0 )
215
216
}
216
217
fn tcx ( self ) -> Option < TyCtxt < ' tcx > > {
217
218
Some ( self . 1 )
@@ -242,7 +243,7 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
242
243
self . tcx . expect ( "missing TyCtxt in DecodeContext" )
243
244
}
244
245
245
- fn cdata ( & self ) -> & ' a CrateMetadata {
246
+ fn cdata ( & self ) -> CrateMetadataRef < ' a > {
246
247
self . cdata . expect ( "missing CrateMetadata in DecodeContext" )
247
248
}
248
249
@@ -558,50 +559,7 @@ impl CrateRoot<'_> {
558
559
}
559
560
}
560
561
561
- impl < ' a , ' tcx > CrateMetadata {
562
- crate fn new (
563
- sess : & Session ,
564
- blob : MetadataBlob ,
565
- root : CrateRoot < ' static > ,
566
- raw_proc_macros : Option < & ' static [ ProcMacro ] > ,
567
- cnum : CrateNum ,
568
- cnum_map : CrateNumMap ,
569
- dep_kind : DepKind ,
570
- source : CrateSource ,
571
- private_dep : bool ,
572
- host_hash : Option < Svh > ,
573
- ) -> CrateMetadata {
574
- let def_path_table = record_time ( & sess. perf_stats . decode_def_path_tables_time , || {
575
- root. def_path_table . decode ( ( & blob, sess) )
576
- } ) ;
577
- let trait_impls = root
578
- . impls
579
- . decode ( ( & blob, sess) )
580
- . map ( |trait_impls| ( trait_impls. trait_id , trait_impls. impls ) )
581
- . collect ( ) ;
582
- let alloc_decoding_state =
583
- AllocDecodingState :: new ( root. interpret_alloc_index . decode ( & blob) . collect ( ) ) ;
584
- let dependencies = Lock :: new ( cnum_map. iter ( ) . cloned ( ) . collect ( ) ) ;
585
- CrateMetadata {
586
- blob,
587
- root,
588
- def_path_table,
589
- trait_impls,
590
- raw_proc_macros,
591
- source_map_import_info : Once :: new ( ) ,
592
- alloc_decoding_state,
593
- dep_node_index : AtomicCell :: new ( DepNodeIndex :: INVALID ) ,
594
- cnum,
595
- cnum_map,
596
- dependencies,
597
- dep_kind : Lock :: new ( dep_kind) ,
598
- source,
599
- private_dep,
600
- host_hash,
601
- extern_crate : Lock :: new ( None ) ,
602
- }
603
- }
604
-
562
+ impl < ' a , ' tcx > CrateMetadataRef < ' a > {
605
563
fn is_proc_macro ( & self , id : DefIndex ) -> bool {
606
564
self . root . proc_macro_data . and_then ( |data| data. decode ( self ) . find ( |x| * x == id) ) . is_some ( )
607
565
}
@@ -622,10 +580,6 @@ impl<'a, 'tcx> CrateMetadata {
622
580
} )
623
581
}
624
582
625
- fn local_def_id ( & self , index : DefIndex ) -> DefId {
626
- DefId { krate : self . cnum , index }
627
- }
628
-
629
583
fn raw_proc_macro ( & self , id : DefIndex ) -> & ProcMacro {
630
584
// DefIndex's in root.proc_macro_data have a one-to-one correspondence
631
585
// with items in 'raw_proc_macros'.
@@ -1191,18 +1145,6 @@ impl<'a, 'tcx> CrateMetadata {
1191
1145
. collect ( )
1192
1146
}
1193
1147
1194
- // Translate a DefId from the current compilation environment to a DefId
1195
- // for an external crate.
1196
- fn reverse_translate_def_id ( & self , did : DefId ) -> Option < DefId > {
1197
- for ( local, & global) in self . cnum_map . iter_enumerated ( ) {
1198
- if global == did. krate {
1199
- return Some ( DefId { krate : local, index : did. index } ) ;
1200
- }
1201
- }
1202
-
1203
- None
1204
- }
1205
-
1206
1148
fn get_inherent_implementations_for_type (
1207
1149
& self ,
1208
1150
tcx : TyCtxt < ' tcx > ,
@@ -1409,11 +1351,6 @@ impl<'a, 'tcx> CrateMetadata {
1409
1351
DefPath :: make ( self . cnum , id, |parent| self . def_key ( parent) )
1410
1352
}
1411
1353
1412
- #[ inline]
1413
- fn def_path_hash ( & self , index : DefIndex ) -> DefPathHash {
1414
- self . def_path_table . def_path_hash ( index)
1415
- }
1416
-
1417
1354
/// Imports the source_map from an external crate into the source_map of the crate
1418
1355
/// currently being compiled (the "local crate").
1419
1356
///
@@ -1440,10 +1377,10 @@ impl<'a, 'tcx> CrateMetadata {
1440
1377
/// Proc macro crates don't currently export spans, so this function does not have
1441
1378
/// to work for them.
1442
1379
fn imported_source_files (
1443
- & ' a self ,
1380
+ & self ,
1444
1381
local_source_map : & source_map:: SourceMap ,
1445
- ) -> & [ ImportedSourceFile ] {
1446
- self . source_map_import_info . init_locking ( || {
1382
+ ) -> & ' a [ ImportedSourceFile ] {
1383
+ self . cdata . source_map_import_info . init_locking ( || {
1447
1384
let external_source_map = self . root . source_map . decode ( self ) ;
1448
1385
1449
1386
external_source_map
@@ -1516,29 +1453,50 @@ impl<'a, 'tcx> CrateMetadata {
1516
1453
. collect ( )
1517
1454
} )
1518
1455
}
1456
+ }
1519
1457
1520
- /// Get the `DepNodeIndex` corresponding this crate. The result of this
1521
- /// method is cached in the `dep_node_index` field.
1522
- fn get_crate_dep_node_index ( & self , tcx : TyCtxt < ' tcx > ) -> DepNodeIndex {
1523
- let mut dep_node_index = self . dep_node_index . load ( ) ;
1524
-
1525
- if unlikely ! ( dep_node_index == DepNodeIndex :: INVALID ) {
1526
- // We have not cached the DepNodeIndex for this upstream crate yet,
1527
- // so use the dep-graph to find it out and cache it.
1528
- // Note that multiple threads can enter this block concurrently.
1529
- // That is fine because the DepNodeIndex remains constant
1530
- // throughout the whole compilation session, and multiple stores
1531
- // would always write the same value.
1532
-
1533
- let def_path_hash = self . def_path_hash ( CRATE_DEF_INDEX ) ;
1534
- let dep_node = def_path_hash. to_dep_node ( dep_graph:: DepKind :: CrateMetadata ) ;
1535
-
1536
- dep_node_index = tcx. dep_graph . dep_node_index_of ( & dep_node) ;
1537
- assert ! ( dep_node_index != DepNodeIndex :: INVALID ) ;
1538
- self . dep_node_index . store ( dep_node_index) ;
1458
+ impl CrateMetadata {
1459
+ crate fn new (
1460
+ sess : & Session ,
1461
+ blob : MetadataBlob ,
1462
+ root : CrateRoot < ' static > ,
1463
+ raw_proc_macros : Option < & ' static [ ProcMacro ] > ,
1464
+ cnum : CrateNum ,
1465
+ cnum_map : CrateNumMap ,
1466
+ dep_kind : DepKind ,
1467
+ source : CrateSource ,
1468
+ private_dep : bool ,
1469
+ host_hash : Option < Svh > ,
1470
+ ) -> CrateMetadata {
1471
+ let def_path_table = record_time ( & sess. perf_stats . decode_def_path_tables_time , || {
1472
+ root. def_path_table . decode ( ( & blob, sess) )
1473
+ } ) ;
1474
+ let trait_impls = root
1475
+ . impls
1476
+ . decode ( ( & blob, sess) )
1477
+ . map ( |trait_impls| ( trait_impls. trait_id , trait_impls. impls ) )
1478
+ . collect ( ) ;
1479
+ let alloc_decoding_state =
1480
+ AllocDecodingState :: new ( root. interpret_alloc_index . decode ( & blob) . collect ( ) ) ;
1481
+ let dependencies = Lock :: new ( cnum_map. iter ( ) . cloned ( ) . collect ( ) ) ;
1482
+ CrateMetadata {
1483
+ blob,
1484
+ root,
1485
+ def_path_table,
1486
+ trait_impls,
1487
+ raw_proc_macros,
1488
+ source_map_import_info : Once :: new ( ) ,
1489
+ alloc_decoding_state,
1490
+ dep_node_index : AtomicCell :: new ( DepNodeIndex :: INVALID ) ,
1491
+ cnum,
1492
+ cnum_map,
1493
+ dependencies,
1494
+ dep_kind : Lock :: new ( dep_kind) ,
1495
+ source,
1496
+ private_dep,
1497
+ host_hash,
1498
+ extern_crate : Lock :: new ( None ) ,
1539
1499
}
1540
-
1541
- dep_node_index
1542
1500
}
1543
1501
1544
1502
crate fn dependencies ( & self ) -> LockGuard < ' _ , Vec < CrateNum > > {
@@ -1613,6 +1571,51 @@ impl<'a, 'tcx> CrateMetadata {
1613
1571
crate fn hash ( & self ) -> Svh {
1614
1572
self . root . hash
1615
1573
}
1574
+
1575
+ fn local_def_id ( & self , index : DefIndex ) -> DefId {
1576
+ DefId { krate : self . cnum , index }
1577
+ }
1578
+
1579
+ // Translate a DefId from the current compilation environment to a DefId
1580
+ // for an external crate.
1581
+ fn reverse_translate_def_id ( & self , did : DefId ) -> Option < DefId > {
1582
+ for ( local, & global) in self . cnum_map . iter_enumerated ( ) {
1583
+ if global == did. krate {
1584
+ return Some ( DefId { krate : local, index : did. index } ) ;
1585
+ }
1586
+ }
1587
+
1588
+ None
1589
+ }
1590
+
1591
+ #[ inline]
1592
+ fn def_path_hash ( & self , index : DefIndex ) -> DefPathHash {
1593
+ self . def_path_table . def_path_hash ( index)
1594
+ }
1595
+
1596
+ /// Get the `DepNodeIndex` corresponding this crate. The result of this
1597
+ /// method is cached in the `dep_node_index` field.
1598
+ fn get_crate_dep_node_index ( & self , tcx : TyCtxt < ' tcx > ) -> DepNodeIndex {
1599
+ let mut dep_node_index = self . dep_node_index . load ( ) ;
1600
+
1601
+ if unlikely ! ( dep_node_index == DepNodeIndex :: INVALID ) {
1602
+ // We have not cached the DepNodeIndex for this upstream crate yet,
1603
+ // so use the dep-graph to find it out and cache it.
1604
+ // Note that multiple threads can enter this block concurrently.
1605
+ // That is fine because the DepNodeIndex remains constant
1606
+ // throughout the whole compilation session, and multiple stores
1607
+ // would always write the same value.
1608
+
1609
+ let def_path_hash = self . def_path_hash ( CRATE_DEF_INDEX ) ;
1610
+ let dep_node = def_path_hash. to_dep_node ( dep_graph:: DepKind :: CrateMetadata ) ;
1611
+
1612
+ dep_node_index = tcx. dep_graph . dep_node_index_of ( & dep_node) ;
1613
+ assert ! ( dep_node_index != DepNodeIndex :: INVALID ) ;
1614
+ self . dep_node_index . store ( dep_node_index) ;
1615
+ }
1616
+
1617
+ dep_node_index
1618
+ }
1616
1619
}
1617
1620
1618
1621
// Cannot be implemented on 'ProcMacro', as libproc_macro
0 commit comments