@@ -21,6 +21,8 @@ use hir::map as hir_map;
21
21
use hir:: map:: DefPathHash ;
22
22
use lint:: { self , Lint } ;
23
23
use ich:: { self , StableHashingContext , NodeIdHashingMode } ;
24
+ use middle:: cstore:: { CrateStore , LinkMeta , EncodedMetadataHashes } ;
25
+ use middle:: cstore:: EncodedMetadata ;
24
26
use middle:: free_region:: FreeRegionMap ;
25
27
use middle:: lang_items;
26
28
use middle:: resolve_lifetime:: { self , ObjectLifetimeDefault } ;
@@ -50,6 +52,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
50
52
51
53
use arena:: { TypedArena , DroplessArena } ;
52
54
use rustc_data_structures:: indexed_vec:: IndexVec ;
55
+ use std:: any:: Any ;
53
56
use std:: borrow:: Borrow ;
54
57
use std:: cell:: { Cell , RefCell } ;
55
58
use std:: cmp:: Ordering ;
@@ -806,8 +809,11 @@ pub struct GlobalCtxt<'tcx> {
806
809
global_arenas : & ' tcx GlobalArenas < ' tcx > ,
807
810
global_interners : CtxtInterners < ' tcx > ,
808
811
812
+ cstore : & ' tcx CrateStore ,
813
+
809
814
pub sess : & ' tcx Session ,
810
815
816
+
811
817
pub trans_trait_caches : traits:: trans:: TransTraitCaches < ' tcx > ,
812
818
813
819
pub dep_graph : DepGraph ,
@@ -979,6 +985,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
979
985
/// value (types, substs, etc.) can only be used while `ty::tls` has a valid
980
986
/// reference to the context, to allow formatting values that need it.
981
987
pub fn create_and_enter < F , R > ( s : & ' tcx Session ,
988
+ cstore : & ' tcx CrateStore ,
982
989
local_providers : ty:: maps:: Providers < ' tcx > ,
983
990
extern_providers : ty:: maps:: Providers < ' tcx > ,
984
991
mir_passes : Rc < Passes > ,
@@ -995,16 +1002,15 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
995
1002
let interners = CtxtInterners :: new ( arena) ;
996
1003
let common_types = CommonTypes :: new ( & interners) ;
997
1004
let dep_graph = hir. dep_graph . clone ( ) ;
998
- let max_cnum = s . cstore . crates_untracked ( ) . iter ( ) . map ( |c| c. as_usize ( ) ) . max ( ) . unwrap_or ( 0 ) ;
1005
+ let max_cnum = cstore. crates_untracked ( ) . iter ( ) . map ( |c| c. as_usize ( ) ) . max ( ) . unwrap_or ( 0 ) ;
999
1006
let mut providers = IndexVec :: from_elem_n ( extern_providers, max_cnum + 1 ) ;
1000
1007
providers[ LOCAL_CRATE ] = local_providers;
1001
1008
1002
1009
let def_path_hash_to_def_id = if s. opts . build_dep_graph ( ) {
1003
- let upstream_def_path_tables: Vec < ( CrateNum , Rc < _ > ) > = s
1004
- . cstore
1010
+ let upstream_def_path_tables: Vec < ( CrateNum , Rc < _ > ) > = cstore
1005
1011
. crates_untracked ( )
1006
1012
. iter ( )
1007
- . map ( |& cnum| ( cnum, s . cstore . def_path_table ( cnum) ) )
1013
+ . map ( |& cnum| ( cnum, cstore. def_path_table ( cnum) ) )
1008
1014
. collect ( ) ;
1009
1015
1010
1016
let def_path_tables = || {
@@ -1034,6 +1040,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
1034
1040
1035
1041
tls:: enter_global ( GlobalCtxt {
1036
1042
sess : s,
1043
+ cstore,
1037
1044
trans_trait_caches : traits:: trans:: TransTraitCaches :: new ( dep_graph. clone ( ) ) ,
1038
1045
global_arenas : arenas,
1039
1046
global_interners : interners,
@@ -1126,6 +1133,54 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
1126
1133
pub fn crates ( self ) -> Rc < Vec < CrateNum > > {
1127
1134
self . all_crate_nums ( LOCAL_CRATE )
1128
1135
}
1136
+
1137
+ pub fn def_key ( self , id : DefId ) -> hir_map:: DefKey {
1138
+ if id. is_local ( ) {
1139
+ self . hir . def_key ( id)
1140
+ } else {
1141
+ self . cstore . def_key ( id)
1142
+ }
1143
+ }
1144
+
1145
+ /// Convert a `DefId` into its fully expanded `DefPath` (every
1146
+ /// `DefId` is really just an interned def-path).
1147
+ ///
1148
+ /// Note that if `id` is not local to this crate, the result will
1149
+ /// be a non-local `DefPath`.
1150
+ pub fn def_path ( self , id : DefId ) -> hir_map:: DefPath {
1151
+ if id. is_local ( ) {
1152
+ self . hir . def_path ( id)
1153
+ } else {
1154
+ self . cstore . def_path ( id)
1155
+ }
1156
+ }
1157
+
1158
+ #[ inline]
1159
+ pub fn def_path_hash ( self , def_id : DefId ) -> hir_map:: DefPathHash {
1160
+ if def_id. is_local ( ) {
1161
+ self . hir . definitions ( ) . def_path_hash ( def_id. index )
1162
+ } else {
1163
+ self . cstore . def_path_hash ( def_id)
1164
+ }
1165
+ }
1166
+
1167
+ pub fn metadata_encoding_version ( self ) -> Vec < u8 > {
1168
+ self . cstore . metadata_encoding_version ( ) . to_vec ( )
1169
+ }
1170
+
1171
+ // Note that this is *untracked* and should only be used within the query
1172
+ // system if the result is otherwise tracked through queries
1173
+ pub fn crate_data_as_rc_any ( self , cnum : CrateNum ) -> Rc < Any > {
1174
+ self . cstore . crate_data_as_rc_any ( cnum)
1175
+ }
1176
+ }
1177
+
1178
+ impl < ' a , ' tcx > TyCtxt < ' a , ' tcx , ' tcx > {
1179
+ pub fn encode_metadata ( self , link_meta : & LinkMeta , reachable : & NodeSet )
1180
+ -> ( EncodedMetadata , EncodedMetadataHashes )
1181
+ {
1182
+ self . cstore . encode_metadata ( self , link_meta, reachable)
1183
+ }
1129
1184
}
1130
1185
1131
1186
impl < ' gcx : ' tcx , ' tcx > GlobalCtxt < ' gcx > {
@@ -2061,4 +2116,16 @@ pub fn provide(providers: &mut ty::maps::Providers) {
2061
2116
let id = tcx. hir . definitions ( ) . def_index_to_hir_id ( id. index ) ;
2062
2117
tcx. stability ( ) . local_deprecation_entry ( id)
2063
2118
} ;
2119
+ providers. extern_mod_stmt_cnum = |tcx, id| {
2120
+ let id = tcx. hir . definitions ( ) . find_node_for_hir_id ( id) ;
2121
+ tcx. cstore . extern_mod_stmt_cnum_untracked ( id)
2122
+ } ;
2123
+ providers. all_crate_nums = |tcx, cnum| {
2124
+ assert_eq ! ( cnum, LOCAL_CRATE ) ;
2125
+ Rc :: new ( tcx. cstore . crates_untracked ( ) )
2126
+ } ;
2127
+ providers. postorder_cnums = |tcx, cnum| {
2128
+ assert_eq ! ( cnum, LOCAL_CRATE ) ;
2129
+ Rc :: new ( tcx. cstore . postorder_cnums_untracked ( ) )
2130
+ } ;
2064
2131
}
0 commit comments