@@ -14,9 +14,7 @@ use rustc::hir::map as ast_map;
14
14
15
15
use rustc:: hir:: intravisit:: { Visitor , IdRangeComputingVisitor , IdRange } ;
16
16
17
- use common as c;
18
- use cstore;
19
-
17
+ use cstore:: CrateMetadata ;
20
18
use decoder:: DecodeContext ;
21
19
use encoder:: EncodeContext ;
22
20
@@ -28,60 +26,53 @@ use rustc::ty::{self, TyCtxt};
28
26
29
27
use syntax:: ast;
30
28
31
- use rbml:: reader;
32
29
use rbml;
33
30
use rustc_serialize:: { Decodable , Encodable } ;
34
31
35
32
// ______________________________________________________________________
36
33
// Top-level methods.
37
34
38
35
pub fn encode_inlined_item ( ecx : & mut EncodeContext , ii : InlinedItemRef ) {
39
- ecx. tag ( c:: tag_ast, |ecx| {
40
- ecx. tag ( c:: tag_id_range, |ecx| {
41
- let mut visitor = IdRangeComputingVisitor :: new ( ) ;
42
- match ii {
43
- InlinedItemRef :: Item ( _, i) => visitor. visit_item ( i) ,
44
- InlinedItemRef :: TraitItem ( _, ti) => visitor. visit_trait_item ( ti) ,
45
- InlinedItemRef :: ImplItem ( _, ii) => visitor. visit_impl_item ( ii)
46
- }
47
- visitor. result ( ) . encode ( & mut ecx. opaque ( ) ) . unwrap ( )
48
- } ) ;
36
+ ecx. tag ( :: common:: tag_ast, |ecx| {
37
+ let mut visitor = IdRangeComputingVisitor :: new ( ) ;
38
+ match ii {
39
+ InlinedItemRef :: Item ( _, i) => visitor. visit_item ( i) ,
40
+ InlinedItemRef :: TraitItem ( _, ti) => visitor. visit_trait_item ( ti) ,
41
+ InlinedItemRef :: ImplItem ( _, ii) => visitor. visit_impl_item ( ii)
42
+ }
43
+ visitor. result ( ) . encode ( ecx) . unwrap ( ) ;
49
44
50
- ecx . tag ( c :: tag_tree , |ecx| ii. encode ( ecx) . unwrap ( ) ) ;
45
+ ii. encode ( ecx) . unwrap ( ) ;
51
46
52
- ecx. tag ( c:: tag_table, |ecx| {
53
- let mut visitor = SideTableEncodingIdVisitor {
54
- ecx : ecx
55
- } ;
56
- match ii {
57
- InlinedItemRef :: Item ( _, i) => visitor. visit_item ( i) ,
58
- InlinedItemRef :: TraitItem ( _, ti) => visitor. visit_trait_item ( ti) ,
59
- InlinedItemRef :: ImplItem ( _, ii) => visitor. visit_impl_item ( ii)
60
- }
61
- } ) ;
47
+ let mut visitor = SideTableEncodingIdVisitor {
48
+ ecx : ecx
49
+ } ;
50
+ match ii {
51
+ InlinedItemRef :: Item ( _, i) => visitor. visit_item ( i) ,
52
+ InlinedItemRef :: TraitItem ( _, ti) => visitor. visit_trait_item ( ti) ,
53
+ InlinedItemRef :: ImplItem ( _, ii) => visitor. visit_impl_item ( ii)
54
+ }
62
55
} ) ;
63
56
}
64
57
65
58
/// Decodes an item from its AST in the cdata's metadata and adds it to the
66
59
/// ast-map.
67
- pub fn decode_inlined_item < ' a , ' tcx > ( cdata : & cstore :: CrateMetadata ,
60
+ pub fn decode_inlined_item < ' a , ' tcx > ( cdata : & CrateMetadata ,
68
61
tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
69
62
parent_def_path : ast_map:: DefPath ,
70
63
parent_did : DefId ,
71
64
ast_doc : rbml:: Doc ,
72
65
orig_did : DefId )
73
66
-> & ' tcx InlinedItem {
74
67
debug ! ( "> Decoding inlined fn: {:?}" , tcx. item_path_str( orig_did) ) ;
75
- let from_id_range = {
76
- let decoder = & mut ast_doc. get ( c:: tag_id_range) . opaque ( ) ;
77
- IdRange {
78
- min : ast:: NodeId :: from_u32 ( u32:: decode ( decoder) . unwrap ( ) ) ,
79
- max : ast:: NodeId :: from_u32 ( u32:: decode ( decoder) . unwrap ( ) )
80
- }
81
- } ;
82
- let mut dcx = DecodeContext :: new ( tcx, cdata, from_id_range,
83
- ast_doc. get ( c:: tag_tree) ) ;
84
- let ii = InlinedItem :: decode ( & mut dcx) . unwrap ( ) ;
68
+ let dcx = & mut ast_doc. decoder ( ) ;
69
+ dcx. tcx = Some ( tcx) ;
70
+ dcx. cdata = Some ( cdata) ;
71
+ dcx. from_id_range = IdRange :: decode ( dcx) . unwrap ( ) ;
72
+ let cnt = dcx. from_id_range . max . as_usize ( ) - dcx. from_id_range . min . as_usize ( ) ;
73
+ dcx. to_id_range . min = tcx. sess . reserve_node_ids ( cnt) ;
74
+ dcx. to_id_range . max = ast:: NodeId :: new ( dcx. to_id_range . min . as_usize ( ) + cnt) ;
75
+ let ii = InlinedItem :: decode ( dcx) . unwrap ( ) ;
85
76
86
77
let ii = ast_map:: map_decoded_item ( & tcx. map ,
87
78
parent_def_path,
@@ -97,7 +88,7 @@ pub fn decode_inlined_item<'a, 'tcx>(cdata: &cstore::CrateMetadata,
97
88
let inlined_did = tcx. map . local_def_id ( item_node_id) ;
98
89
tcx. register_item_type ( inlined_did, tcx. lookup_item_type ( orig_did) ) ;
99
90
100
- decode_side_tables ( & mut dcx, ast_doc) ;
91
+ decode_side_tables ( dcx, ast_doc) ;
101
92
102
93
ii
103
94
}
@@ -116,7 +107,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
116
107
self . end_tag ( ) . unwrap ( ) ;
117
108
}
118
109
119
- fn id ( & mut self , id : ast:: NodeId ) {
110
+ fn entry ( & mut self , table : Table , id : ast:: NodeId ) {
111
+ table. encode ( self ) . unwrap ( ) ;
120
112
id. encode ( self ) . unwrap ( ) ;
121
113
}
122
114
}
@@ -131,67 +123,67 @@ impl<'a, 'b, 'tcx, 'v> Visitor<'v> for SideTableEncodingIdVisitor<'a, 'b, 'tcx>
131
123
}
132
124
}
133
125
126
+ #[ derive( RustcEncodable , RustcDecodable , Debug ) ]
127
+ enum Table {
128
+ Def ,
129
+ NodeType ,
130
+ ItemSubsts ,
131
+ Freevars ,
132
+ MethodMap ,
133
+ Adjustment ,
134
+ UpvarCaptureMap ,
135
+ ConstQualif ,
136
+ CastKind
137
+ }
138
+
134
139
fn encode_side_tables_for_id ( ecx : & mut EncodeContext , id : ast:: NodeId ) {
135
140
let tcx = ecx. tcx ;
136
141
137
142
debug ! ( "Encoding side tables for id {}" , id) ;
138
143
139
144
if let Some ( def) = tcx. expect_def_or_none ( id) {
140
- ecx. tag ( c:: tag_table_def, |ecx| {
141
- ecx. id ( id) ;
142
- def. encode ( ecx) . unwrap ( ) ;
143
- } )
145
+ ecx. entry ( Table :: Def , id) ;
146
+ def. encode ( ecx) . unwrap ( ) ;
144
147
}
145
148
146
149
if let Some ( ty) = tcx. node_types ( ) . get ( & id) {
147
- ecx. tag ( c:: tag_table_node_type, |ecx| {
148
- ecx. id ( id) ;
149
- ty. encode ( ecx) . unwrap ( ) ;
150
- } )
150
+ ecx. entry ( Table :: NodeType , id) ;
151
+ ty. encode ( ecx) . unwrap ( ) ;
151
152
}
152
153
153
154
if let Some ( item_substs) = tcx. tables . borrow ( ) . item_substs . get ( & id) {
154
- ecx. tag ( c:: tag_table_item_subst, |ecx| {
155
- ecx. id ( id) ;
156
- item_substs. substs . encode ( ecx) . unwrap ( ) ;
157
- } )
155
+ ecx. entry ( Table :: ItemSubsts , id) ;
156
+ item_substs. substs . encode ( ecx) . unwrap ( ) ;
158
157
}
159
158
160
159
if let Some ( fv) = tcx. freevars . borrow ( ) . get ( & id) {
161
- ecx. tag ( c:: tag_table_freevars, |ecx| {
162
- ecx. id ( id) ;
163
- fv. encode ( ecx) . unwrap ( ) ;
164
- } ) ;
160
+ ecx. entry ( Table :: Freevars , id) ;
161
+ fv. encode ( ecx) . unwrap ( ) ;
165
162
166
163
for freevar in fv {
167
- ecx. tag ( c:: tag_table_upvar_capture_map, |ecx| {
168
- ecx. id ( id) ;
169
-
170
- let def_id = freevar. def . def_id ( ) ;
171
- let var_id = tcx. map . as_local_node_id ( def_id) . unwrap ( ) ;
172
- let upvar_id = ty:: UpvarId {
173
- var_id : var_id,
174
- closure_expr_id : id
175
- } ;
176
- let upvar_capture = tcx. tables
177
- . borrow ( )
178
- . upvar_capture_map
179
- . get ( & upvar_id)
180
- . unwrap ( )
181
- . clone ( ) ;
182
- var_id. encode ( ecx) . unwrap ( ) ;
183
- upvar_capture. encode ( ecx) . unwrap ( ) ;
184
- } )
164
+ ecx. entry ( Table :: UpvarCaptureMap , id) ;
165
+ let def_id = freevar. def . def_id ( ) ;
166
+ let var_id = tcx. map . as_local_node_id ( def_id) . unwrap ( ) ;
167
+ let upvar_id = ty:: UpvarId {
168
+ var_id : var_id,
169
+ closure_expr_id : id
170
+ } ;
171
+ let upvar_capture = tcx. tables
172
+ . borrow ( )
173
+ . upvar_capture_map
174
+ . get ( & upvar_id)
175
+ . unwrap ( )
176
+ . clone ( ) ;
177
+ var_id. encode ( ecx) . unwrap ( ) ;
178
+ upvar_capture. encode ( ecx) . unwrap ( ) ;
185
179
}
186
180
}
187
181
188
182
let method_call = ty:: MethodCall :: expr ( id) ;
189
183
if let Some ( method) = tcx. tables . borrow ( ) . method_map . get ( & method_call) {
190
- ecx. tag ( c:: tag_table_method_map, |ecx| {
191
- ecx. id ( id) ;
192
- method_call. autoderef . encode ( ecx) . unwrap ( ) ;
193
- method. encode ( ecx) . unwrap ( ) ;
194
- } )
184
+ ecx. entry ( Table :: MethodMap , id) ;
185
+ method_call. autoderef . encode ( ecx) . unwrap ( ) ;
186
+ method. encode ( ecx) . unwrap ( ) ;
195
187
}
196
188
197
189
if let Some ( adjustment) = tcx. tables . borrow ( ) . adjustments . get ( & id) {
@@ -200,91 +192,79 @@ fn encode_side_tables_for_id(ecx: &mut EncodeContext, id: ast::NodeId) {
200
192
for autoderef in 0 ..adj. autoderefs {
201
193
let method_call = ty:: MethodCall :: autoderef ( id, autoderef as u32 ) ;
202
194
if let Some ( method) = tcx. tables . borrow ( ) . method_map . get ( & method_call) {
203
- ecx. tag ( c:: tag_table_method_map, |ecx| {
204
- ecx. id ( id) ;
205
- method_call. autoderef . encode ( ecx) . unwrap ( ) ;
206
- method. encode ( ecx) . unwrap ( ) ;
207
- } )
195
+ ecx. entry ( Table :: MethodMap , id) ;
196
+ method_call. autoderef . encode ( ecx) . unwrap ( ) ;
197
+ method. encode ( ecx) . unwrap ( ) ;
208
198
}
209
199
}
210
200
}
211
201
_ => { }
212
202
}
213
203
214
- ecx. tag ( c:: tag_table_adjustments, |ecx| {
215
- ecx. id ( id) ;
216
- adjustment. encode ( ecx) . unwrap ( ) ;
217
- } )
204
+ ecx. entry ( Table :: Adjustment , id) ;
205
+ adjustment. encode ( ecx) . unwrap ( ) ;
218
206
}
219
207
220
208
if let Some ( cast_kind) = tcx. cast_kinds . borrow ( ) . get ( & id) {
221
- ecx. tag ( c:: tag_table_cast_kinds, |ecx| {
222
- ecx. id ( id) ;
223
- cast_kind. encode ( ecx) . unwrap ( )
224
- } )
209
+ ecx. entry ( Table :: CastKind , id) ;
210
+ cast_kind. encode ( ecx) . unwrap ( ) ;
225
211
}
226
212
227
213
if let Some ( qualif) = tcx. const_qualif_map . borrow ( ) . get ( & id) {
228
- ecx. tag ( c:: tag_table_const_qualif, |ecx| {
229
- ecx. id ( id) ;
230
- qualif. encode ( ecx) . unwrap ( )
231
- } )
214
+ ecx. entry ( Table :: ConstQualif , id) ;
215
+ qualif. encode ( ecx) . unwrap ( ) ;
232
216
}
233
217
}
234
218
235
- fn decode_side_tables < ' a , ' tcx > ( dcx : & mut DecodeContext < ' a , ' tcx > ,
236
- ast_doc : rbml:: Doc < ' a > ) {
237
- for ( tag, entry_doc) in reader:: docs ( ast_doc. get ( c:: tag_table) ) {
238
- dcx. rbml_r = reader:: Decoder :: new ( entry_doc) ;
219
+ fn decode_side_tables ( dcx : & mut DecodeContext , ast_doc : rbml:: Doc ) {
220
+ while dcx. position ( ) < ast_doc. end {
221
+ let table = Decodable :: decode ( dcx) . unwrap ( ) ;
239
222
let id = Decodable :: decode ( dcx) . unwrap ( ) ;
240
- debug ! ( "decode_side_tables: entry for id={}, tag=0x{:x }" , id, tag ) ;
241
- match tag {
242
- c :: tag_table_def => {
223
+ debug ! ( "decode_side_tables: entry for id={}, table={:? }" , id, table ) ;
224
+ match table {
225
+ Table :: Def => {
243
226
let def = Decodable :: decode ( dcx) . unwrap ( ) ;
244
- dcx. tcx . def_map . borrow_mut ( ) . insert ( id, def:: PathResolution :: new ( def) ) ;
227
+ dcx. tcx ( ) . def_map . borrow_mut ( ) . insert ( id, def:: PathResolution :: new ( def) ) ;
245
228
}
246
- c :: tag_table_node_type => {
229
+ Table :: NodeType => {
247
230
let ty = Decodable :: decode ( dcx) . unwrap ( ) ;
248
- dcx. tcx . node_type_insert ( id, ty) ;
231
+ dcx. tcx ( ) . node_type_insert ( id, ty) ;
249
232
}
250
- c :: tag_table_item_subst => {
233
+ Table :: ItemSubsts => {
251
234
let item_substs = Decodable :: decode ( dcx) . unwrap ( ) ;
252
- dcx. tcx . tables . borrow_mut ( ) . item_substs . insert ( id, item_substs) ;
235
+ dcx. tcx ( ) . tables . borrow_mut ( ) . item_substs . insert ( id, item_substs) ;
253
236
}
254
- c :: tag_table_freevars => {
237
+ Table :: Freevars => {
255
238
let fv_info = Decodable :: decode ( dcx) . unwrap ( ) ;
256
- dcx. tcx . freevars . borrow_mut ( ) . insert ( id, fv_info) ;
239
+ dcx. tcx ( ) . freevars . borrow_mut ( ) . insert ( id, fv_info) ;
257
240
}
258
- c :: tag_table_upvar_capture_map => {
241
+ Table :: UpvarCaptureMap => {
259
242
let upvar_id = ty:: UpvarId {
260
243
var_id : Decodable :: decode ( dcx) . unwrap ( ) ,
261
244
closure_expr_id : id
262
245
} ;
263
246
let ub = Decodable :: decode ( dcx) . unwrap ( ) ;
264
- dcx. tcx . tables . borrow_mut ( ) . upvar_capture_map . insert ( upvar_id, ub) ;
247
+ dcx. tcx ( ) . tables . borrow_mut ( ) . upvar_capture_map . insert ( upvar_id, ub) ;
265
248
}
266
- c :: tag_table_method_map => {
249
+ Table :: MethodMap => {
267
250
let method_call = ty:: MethodCall {
268
251
expr_id : id,
269
252
autoderef : Decodable :: decode ( dcx) . unwrap ( )
270
253
} ;
271
254
let method = Decodable :: decode ( dcx) . unwrap ( ) ;
272
- dcx. tcx . tables . borrow_mut ( ) . method_map . insert ( method_call, method) ;
255
+ dcx. tcx ( ) . tables . borrow_mut ( ) . method_map . insert ( method_call, method) ;
273
256
}
274
- c :: tag_table_adjustments => {
257
+ Table :: Adjustment => {
275
258
let adj = Decodable :: decode ( dcx) . unwrap ( ) ;
276
- dcx. tcx . tables . borrow_mut ( ) . adjustments . insert ( id, adj) ;
259
+ dcx. tcx ( ) . tables . borrow_mut ( ) . adjustments . insert ( id, adj) ;
277
260
}
278
- c :: tag_table_cast_kinds => {
261
+ Table :: CastKind => {
279
262
let cast_kind = Decodable :: decode ( dcx) . unwrap ( ) ;
280
- dcx. tcx . cast_kinds . borrow_mut ( ) . insert ( id, cast_kind) ;
263
+ dcx. tcx ( ) . cast_kinds . borrow_mut ( ) . insert ( id, cast_kind) ;
281
264
}
282
- c :: tag_table_const_qualif => {
265
+ Table :: ConstQualif => {
283
266
let qualif = Decodable :: decode ( dcx) . unwrap ( ) ;
284
- dcx. tcx . const_qualif_map . borrow_mut ( ) . insert ( id, qualif) ;
285
- }
286
- _ => {
287
- bug ! ( "unknown tag found in side tables: 0x{:x}" , tag) ;
267
+ dcx. tcx ( ) . const_qualif_map . borrow_mut ( ) . insert ( id, qualif) ;
288
268
}
289
269
}
290
270
}
0 commit comments