@@ -22,6 +22,8 @@ use hir::map::DefPathHash;
22
22
use lint:: { self , Lint } ;
23
23
use ich:: { self , StableHashingContext , NodeIdHashingMode } ;
24
24
use middle:: const_val:: ConstVal ;
25
+ use middle:: cstore:: { CrateStore , LinkMeta , EncodedMetadataHashes } ;
26
+ use middle:: cstore:: EncodedMetadata ;
25
27
use middle:: free_region:: FreeRegionMap ;
26
28
use middle:: lang_items;
27
29
use middle:: resolve_lifetime:: { self , ObjectLifetimeDefault } ;
@@ -52,6 +54,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
52
54
use arena:: { TypedArena , DroplessArena } ;
53
55
use rustc_const_math:: { ConstInt , ConstUsize } ;
54
56
use rustc_data_structures:: indexed_vec:: IndexVec ;
57
+ use std:: any:: Any ;
55
58
use std:: borrow:: Borrow ;
56
59
use std:: cell:: { Cell , RefCell } ;
57
60
use std:: cmp:: Ordering ;
@@ -810,8 +813,11 @@ pub struct GlobalCtxt<'tcx> {
810
813
global_arenas : & ' tcx GlobalArenas < ' tcx > ,
811
814
global_interners : CtxtInterners < ' tcx > ,
812
815
816
+ cstore : & ' tcx CrateStore ,
817
+
813
818
pub sess : & ' tcx Session ,
814
819
820
+
815
821
pub trans_trait_caches : traits:: trans:: TransTraitCaches < ' tcx > ,
816
822
817
823
pub dep_graph : DepGraph ,
@@ -1009,6 +1015,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
1009
1015
/// value (types, substs, etc.) can only be used while `ty::tls` has a valid
1010
1016
/// reference to the context, to allow formatting values that need it.
1011
1017
pub fn create_and_enter < F , R > ( s : & ' tcx Session ,
1018
+ cstore : & ' tcx CrateStore ,
1012
1019
local_providers : ty:: maps:: Providers < ' tcx > ,
1013
1020
extern_providers : ty:: maps:: Providers < ' tcx > ,
1014
1021
mir_passes : Rc < Passes > ,
@@ -1025,16 +1032,15 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
1025
1032
let interners = CtxtInterners :: new ( arena) ;
1026
1033
let common_types = CommonTypes :: new ( & interners) ;
1027
1034
let dep_graph = hir. dep_graph . clone ( ) ;
1028
- let max_cnum = s . cstore . crates_untracked ( ) . iter ( ) . map ( |c| c. as_usize ( ) ) . max ( ) . unwrap_or ( 0 ) ;
1035
+ let max_cnum = cstore. crates_untracked ( ) . iter ( ) . map ( |c| c. as_usize ( ) ) . max ( ) . unwrap_or ( 0 ) ;
1029
1036
let mut providers = IndexVec :: from_elem_n ( extern_providers, max_cnum + 1 ) ;
1030
1037
providers[ LOCAL_CRATE ] = local_providers;
1031
1038
1032
1039
let def_path_hash_to_def_id = if s. opts . build_dep_graph ( ) {
1033
- let upstream_def_path_tables: Vec < ( CrateNum , Rc < _ > ) > = s
1034
- . cstore
1040
+ let upstream_def_path_tables: Vec < ( CrateNum , Rc < _ > ) > = cstore
1035
1041
. crates_untracked ( )
1036
1042
. iter ( )
1037
- . map ( |& cnum| ( cnum, s . cstore . def_path_table ( cnum) ) )
1043
+ . map ( |& cnum| ( cnum, cstore. def_path_table ( cnum) ) )
1038
1044
. collect ( ) ;
1039
1045
1040
1046
let def_path_tables = || {
@@ -1093,6 +1099,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
1093
1099
1094
1100
tls:: enter_global ( GlobalCtxt {
1095
1101
sess : s,
1102
+ cstore,
1096
1103
trans_trait_caches : traits:: trans:: TransTraitCaches :: new ( dep_graph. clone ( ) ) ,
1097
1104
global_arenas : arenas,
1098
1105
global_interners : interners,
@@ -1171,6 +1178,54 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
1171
1178
pub fn crates ( self ) -> Rc < Vec < CrateNum > > {
1172
1179
self . all_crate_nums ( LOCAL_CRATE )
1173
1180
}
1181
+
1182
+ pub fn def_key ( self , id : DefId ) -> hir_map:: DefKey {
1183
+ if id. is_local ( ) {
1184
+ self . hir . def_key ( id)
1185
+ } else {
1186
+ self . cstore . def_key ( id)
1187
+ }
1188
+ }
1189
+
1190
+ /// Convert a `DefId` into its fully expanded `DefPath` (every
1191
+ /// `DefId` is really just an interned def-path).
1192
+ ///
1193
+ /// Note that if `id` is not local to this crate, the result will
1194
+ /// be a non-local `DefPath`.
1195
+ pub fn def_path ( self , id : DefId ) -> hir_map:: DefPath {
1196
+ if id. is_local ( ) {
1197
+ self . hir . def_path ( id)
1198
+ } else {
1199
+ self . cstore . def_path ( id)
1200
+ }
1201
+ }
1202
+
1203
+ #[ inline]
1204
+ pub fn def_path_hash ( self , def_id : DefId ) -> hir_map:: DefPathHash {
1205
+ if def_id. is_local ( ) {
1206
+ self . hir . definitions ( ) . def_path_hash ( def_id. index )
1207
+ } else {
1208
+ self . cstore . def_path_hash ( def_id)
1209
+ }
1210
+ }
1211
+
1212
+ pub fn metadata_encoding_version ( self ) -> Vec < u8 > {
1213
+ self . cstore . metadata_encoding_version ( ) . to_vec ( )
1214
+ }
1215
+
1216
+ // Note that this is *untracked* and should only be used within the query
1217
+ // system if the result is otherwise tracked through queries
1218
+ pub fn crate_data_as_rc_any ( self , cnum : CrateNum ) -> Rc < Any > {
1219
+ self . cstore . crate_data_as_rc_any ( cnum)
1220
+ }
1221
+ }
1222
+
1223
+ impl < ' a , ' tcx > TyCtxt < ' a , ' tcx , ' tcx > {
1224
+ pub fn encode_metadata ( self , link_meta : & LinkMeta , reachable : & NodeSet )
1225
+ -> ( EncodedMetadata , EncodedMetadataHashes )
1226
+ {
1227
+ self . cstore . encode_metadata ( self , link_meta, reachable)
1228
+ }
1174
1229
}
1175
1230
1176
1231
impl < ' gcx : ' tcx , ' tcx > GlobalCtxt < ' gcx > {
@@ -2151,4 +2206,16 @@ pub fn provide(providers: &mut ty::maps::Providers) {
2151
2206
let id = tcx. hir . definitions ( ) . def_index_to_hir_id ( id. index ) ;
2152
2207
tcx. stability ( ) . local_deprecation_entry ( id)
2153
2208
} ;
2209
+ providers. extern_mod_stmt_cnum = |tcx, id| {
2210
+ let id = tcx. hir . as_local_node_id ( id) . unwrap ( ) ;
2211
+ tcx. cstore . extern_mod_stmt_cnum_untracked ( id)
2212
+ } ;
2213
+ providers. all_crate_nums = |tcx, cnum| {
2214
+ assert_eq ! ( cnum, LOCAL_CRATE ) ;
2215
+ Rc :: new ( tcx. cstore . crates_untracked ( ) )
2216
+ } ;
2217
+ providers. postorder_cnums = |tcx, cnum| {
2218
+ assert_eq ! ( cnum, LOCAL_CRATE ) ;
2219
+ Rc :: new ( tcx. cstore . postorder_cnums_untracked ( ) )
2220
+ } ;
2154
2221
}
0 commit comments