@@ -13,43 +13,92 @@ use rustc::hir::map as ast_map;
13
13
use rustc:: hir:: intravisit:: { Visitor , IdRangeComputingVisitor , IdRange } ;
14
14
15
15
use cstore:: CrateMetadata ;
16
- use decoder:: DecodeContext ;
17
16
use encoder:: EncodeContext ;
17
+ use schema:: * ;
18
18
19
19
use rustc:: middle:: cstore:: { InlinedItem , InlinedItemRef } ;
20
- use rustc:: hir:: def;
20
+ use rustc:: middle:: const_qualif:: ConstQualif ;
21
+ use rustc:: hir:: def:: { self , Def } ;
21
22
use rustc:: hir:: def_id:: DefId ;
22
- use rustc:: ty:: TyCtxt ;
23
+ use rustc:: ty:: { self , TyCtxt , Ty } ;
23
24
24
25
use syntax:: ast;
25
26
26
- use rbml;
27
- use rustc_serialize:: { Decodable , Encodable } ;
27
+ use rustc_serialize:: Encodable ;
28
28
29
- // ______________________________________________________________________
30
- // Top-level methods.
29
+ #[ derive( RustcEncodable , RustcDecodable ) ]
30
+ pub struct Ast < ' tcx > {
31
+ id_range : IdRange ,
32
+ item : Lazy < InlinedItem > ,
33
+ side_tables : LazySeq < ( ast:: NodeId , TableEntry < ' tcx > ) >
34
+ }
35
+
36
+ #[ derive( RustcEncodable , RustcDecodable ) ]
37
+ enum TableEntry < ' tcx > {
38
+ Def ( Def ) ,
39
+ NodeType ( Ty < ' tcx > ) ,
40
+ ItemSubsts ( ty:: ItemSubsts < ' tcx > ) ,
41
+ Adjustment ( ty:: adjustment:: AutoAdjustment < ' tcx > ) ,
42
+ ConstQualif ( ConstQualif )
43
+ }
31
44
32
- pub fn encode_inlined_item ( ecx : & mut EncodeContext , ii : InlinedItemRef ) {
33
- ecx . tag ( :: common :: item_tag :: ast , |ecx| {
34
- let mut visitor = IdRangeComputingVisitor :: new ( ) ;
45
+ impl < ' a , ' tcx > EncodeContext < ' a , ' tcx > {
46
+ pub fn encode_inlined_item ( & mut self , ii : InlinedItemRef ) -> Lazy < Ast < ' tcx > > {
47
+ let mut id_visitor = IdRangeComputingVisitor :: new ( ) ;
35
48
match ii {
36
- InlinedItemRef :: Item ( _, i) => visitor . visit_item ( i) ,
37
- InlinedItemRef :: TraitItem ( _, ti) => visitor . visit_trait_item ( ti) ,
38
- InlinedItemRef :: ImplItem ( _, ii) => visitor . visit_impl_item ( ii)
49
+ InlinedItemRef :: Item ( _, i) => id_visitor . visit_item ( i) ,
50
+ InlinedItemRef :: TraitItem ( _, ti) => id_visitor . visit_trait_item ( ti) ,
51
+ InlinedItemRef :: ImplItem ( _, ii) => id_visitor . visit_impl_item ( ii)
39
52
}
40
- visitor. result ( ) . encode ( ecx) . unwrap ( ) ;
41
53
42
- ii. encode ( ecx) . unwrap ( ) ;
54
+ let ii_pos = self . position ( ) ;
55
+ ii. encode ( self ) . unwrap ( ) ;
56
+
57
+ let tables_pos = self . position ( ) ;
58
+ let tables_count = {
59
+ let mut visitor = SideTableEncodingIdVisitor {
60
+ ecx : self ,
61
+ count : 0
62
+ } ;
63
+ match ii {
64
+ InlinedItemRef :: Item ( _, i) => visitor. visit_item ( i) ,
65
+ InlinedItemRef :: TraitItem ( _, ti) => visitor. visit_trait_item ( ti) ,
66
+ InlinedItemRef :: ImplItem ( _, ii) => visitor. visit_impl_item ( ii)
67
+ }
68
+ visitor. count
69
+ } ;
70
+
71
+ self . lazy ( & Ast {
72
+ id_range : id_visitor. result ( ) ,
73
+ item : Lazy :: with_position ( ii_pos) ,
74
+ side_tables : LazySeq :: with_position_and_length ( tables_pos, tables_count)
75
+ } )
76
+ }
77
+ }
78
+
79
+ struct SideTableEncodingIdVisitor < ' a , ' b : ' a , ' tcx : ' b > {
80
+ ecx : & ' a mut EncodeContext < ' b , ' tcx > ,
81
+ count : usize
82
+ }
83
+
84
+ impl < ' a , ' b , ' tcx , ' v > Visitor < ' v > for SideTableEncodingIdVisitor < ' a , ' b , ' tcx > {
85
+ fn visit_id ( & mut self , id : ast:: NodeId ) {
86
+ debug ! ( "Encoding side tables for id {}" , id) ;
43
87
44
- let mut visitor = SideTableEncodingIdVisitor {
45
- ecx : ecx
88
+ let tcx = self . ecx . tcx ;
89
+ let mut encode = |entry : Option < TableEntry > | {
90
+ if let Some ( entry) = entry {
91
+ ( id, entry) . encode ( self . ecx ) . unwrap ( ) ;
92
+ self . count += 1 ;
93
+ }
46
94
} ;
47
- match ii {
48
- InlinedItemRef :: Item ( _, i) => visitor. visit_item ( i) ,
49
- InlinedItemRef :: TraitItem ( _, ti) => visitor. visit_trait_item ( ti) ,
50
- InlinedItemRef :: ImplItem ( _, ii) => visitor. visit_impl_item ( ii)
51
- }
52
- } ) ;
95
+
96
+ encode ( tcx. expect_def_or_none ( id) . map ( TableEntry :: Def ) ) ;
97
+ encode ( tcx. node_types ( ) . get ( & id) . cloned ( ) . map ( TableEntry :: NodeType ) ) ;
98
+ encode ( tcx. tables . borrow ( ) . item_substs . get ( & id) . cloned ( ) . map ( TableEntry :: ItemSubsts ) ) ;
99
+ encode ( tcx. tables . borrow ( ) . adjustments . get ( & id) . cloned ( ) . map ( TableEntry :: Adjustment ) ) ;
100
+ encode ( tcx. const_qualif_map . borrow ( ) . get ( & id) . cloned ( ) . map ( TableEntry :: ConstQualif ) ) ;
101
+ }
53
102
}
54
103
55
104
/// Decodes an item from its AST in the cdata's metadata and adds it to the
@@ -58,17 +107,19 @@ pub fn decode_inlined_item<'a, 'tcx>(cdata: &CrateMetadata,
58
107
tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
59
108
parent_def_path : ast_map:: DefPath ,
60
109
parent_did : DefId ,
61
- ast_doc : rbml :: Doc ,
110
+ ast : Ast < ' tcx > ,
62
111
orig_did : DefId )
63
112
-> & ' tcx InlinedItem {
64
113
debug ! ( "> Decoding inlined fn: {:?}" , tcx. item_path_str( orig_did) ) ;
65
- let dcx = & mut DecodeContext :: new ( ast_doc, Some ( cdata) ) . typed ( tcx) ;
66
- dcx. from_id_range = IdRange :: decode ( dcx) . unwrap ( ) ;
67
- let cnt = dcx. from_id_range . max . as_usize ( ) - dcx. from_id_range . min . as_usize ( ) ;
68
- dcx. to_id_range . min = tcx. sess . reserve_node_ids ( cnt) ;
69
- dcx. to_id_range . max = ast:: NodeId :: new ( dcx. to_id_range . min . as_usize ( ) + cnt) ;
70
- let ii = InlinedItem :: decode ( dcx) . unwrap ( ) ;
71
114
115
+ let cnt = ast. id_range . max . as_usize ( ) - ast. id_range . min . as_usize ( ) ;
116
+ let start = tcx. sess . reserve_node_ids ( cnt) ;
117
+ let id_ranges = [ ast. id_range , IdRange {
118
+ min : start,
119
+ max : ast:: NodeId :: new ( start. as_usize ( ) + cnt)
120
+ } ] ;
121
+
122
+ let ii = ast. item . decode ( ( cdata, tcx, id_ranges) ) ;
72
123
let ii = ast_map:: map_decoded_item ( & tcx. map ,
73
124
parent_def_path,
74
125
parent_did,
@@ -83,107 +134,25 @@ pub fn decode_inlined_item<'a, 'tcx>(cdata: &CrateMetadata,
83
134
let inlined_did = tcx. map . local_def_id ( item_node_id) ;
84
135
tcx. register_item_type ( inlined_did, tcx. lookup_item_type ( orig_did) ) ;
85
136
86
- decode_side_tables ( dcx, ast_doc) ;
87
-
88
- ii
89
- }
90
-
91
- // ______________________________________________________________________
92
- // Encoding and decoding the side tables
93
-
94
- impl < ' a , ' tcx > EncodeContext < ' a , ' tcx > {
95
- fn tag < F > ( & mut self ,
96
- tag_id : usize ,
97
- f : F ) where
98
- F : FnOnce ( & mut Self ) ,
99
- {
100
- self . start_tag ( tag_id) . unwrap ( ) ;
101
- f ( self ) ;
102
- self . end_tag ( ) . unwrap ( ) ;
103
- }
104
-
105
- fn entry ( & mut self , table : Table , id : ast:: NodeId ) {
106
- table. encode ( self ) . unwrap ( ) ;
107
- id. encode ( self ) . unwrap ( ) ;
108
- }
109
- }
110
-
111
- struct SideTableEncodingIdVisitor < ' a , ' b : ' a , ' tcx : ' b > {
112
- ecx : & ' a mut EncodeContext < ' b , ' tcx > ,
113
- }
114
-
115
- impl < ' a , ' b , ' tcx , ' v > Visitor < ' v > for SideTableEncodingIdVisitor < ' a , ' b , ' tcx > {
116
- fn visit_id ( & mut self , id : ast:: NodeId ) {
117
- encode_side_tables_for_id ( self . ecx , id)
118
- }
119
- }
120
-
121
- #[ derive( RustcEncodable , RustcDecodable , Debug ) ]
122
- enum Table {
123
- Def ,
124
- NodeType ,
125
- ItemSubsts ,
126
- Adjustment ,
127
- ConstQualif
128
- }
129
-
130
- fn encode_side_tables_for_id ( ecx : & mut EncodeContext , id : ast:: NodeId ) {
131
- let tcx = ecx. tcx ;
132
-
133
- debug ! ( "Encoding side tables for id {}" , id) ;
134
-
135
- if let Some ( def) = tcx. expect_def_or_none ( id) {
136
- ecx. entry ( Table :: Def , id) ;
137
- def. encode ( ecx) . unwrap ( ) ;
138
- }
139
-
140
- if let Some ( ty) = tcx. node_types ( ) . get ( & id) {
141
- ecx. entry ( Table :: NodeType , id) ;
142
- ty. encode ( ecx) . unwrap ( ) ;
143
- }
144
-
145
- if let Some ( item_substs) = tcx. tables . borrow ( ) . item_substs . get ( & id) {
146
- ecx. entry ( Table :: ItemSubsts , id) ;
147
- item_substs. substs . encode ( ecx) . unwrap ( ) ;
148
- }
149
-
150
- if let Some ( adjustment) = tcx. tables . borrow ( ) . adjustments . get ( & id) {
151
- ecx. entry ( Table :: Adjustment , id) ;
152
- adjustment. encode ( ecx) . unwrap ( ) ;
153
- }
154
-
155
- if let Some ( qualif) = tcx. const_qualif_map . borrow ( ) . get ( & id) {
156
- ecx. entry ( Table :: ConstQualif , id) ;
157
- qualif. encode ( ecx) . unwrap ( ) ;
158
- }
159
- }
160
-
161
- fn decode_side_tables ( dcx : & mut DecodeContext , ast_doc : rbml:: Doc ) {
162
- while dcx. opaque . position ( ) < ast_doc. end {
163
- let table = Decodable :: decode ( dcx) . unwrap ( ) ;
164
- let id = Decodable :: decode ( dcx) . unwrap ( ) ;
165
- debug ! ( "decode_side_tables: entry for id={}, table={:?}" , id, table) ;
166
- match table {
167
- Table :: Def => {
168
- let def = Decodable :: decode ( dcx) . unwrap ( ) ;
169
- dcx. tcx ( ) . def_map . borrow_mut ( ) . insert ( id, def:: PathResolution :: new ( def) ) ;
137
+ for ( id, entry) in ast. side_tables . decode ( ( cdata, tcx, id_ranges) ) {
138
+ match entry {
139
+ TableEntry :: Def ( def) => {
140
+ tcx. def_map . borrow_mut ( ) . insert ( id, def:: PathResolution :: new ( def) ) ;
170
141
}
171
- Table :: NodeType => {
172
- let ty = Decodable :: decode ( dcx) . unwrap ( ) ;
173
- dcx. tcx ( ) . node_type_insert ( id, ty) ;
142
+ TableEntry :: NodeType ( ty) => {
143
+ tcx. node_type_insert ( id, ty) ;
174
144
}
175
- Table :: ItemSubsts => {
176
- let item_substs = Decodable :: decode ( dcx) . unwrap ( ) ;
177
- dcx. tcx ( ) . tables . borrow_mut ( ) . item_substs . insert ( id, item_substs) ;
145
+ TableEntry :: ItemSubsts ( item_substs) => {
146
+ tcx. tables . borrow_mut ( ) . item_substs . insert ( id, item_substs) ;
178
147
}
179
- Table :: Adjustment => {
180
- let adj = Decodable :: decode ( dcx) . unwrap ( ) ;
181
- dcx. tcx ( ) . tables . borrow_mut ( ) . adjustments . insert ( id, adj) ;
148
+ TableEntry :: Adjustment ( adj) => {
149
+ tcx. tables . borrow_mut ( ) . adjustments . insert ( id, adj) ;
182
150
}
183
- Table :: ConstQualif => {
184
- let qualif = Decodable :: decode ( dcx) . unwrap ( ) ;
185
- dcx. tcx ( ) . const_qualif_map . borrow_mut ( ) . insert ( id, qualif) ;
151
+ TableEntry :: ConstQualif ( qualif) => {
152
+ tcx. const_qualif_map . borrow_mut ( ) . insert ( id, qualif) ;
186
153
}
187
154
}
188
155
}
156
+
157
+ ii
189
158
}
0 commit comments