@@ -2,6 +2,7 @@ import syntax::ast;
2
2
import syntax:: fold;
3
3
import syntax:: visit;
4
4
import syntax:: ast_util;
5
+ import syntax:: ast_util:: inlined_item_methods;
5
6
import syntax:: codemap:: span;
6
7
import std:: map:: map;
7
8
import std:: smallintmap:: map;
@@ -31,8 +32,6 @@ import syntax::print::pprust;
31
32
32
33
export encode_inlined_item;
33
34
export decode_inlined_item;
34
- export encode_inlined_method;
35
- export decode_inlined_method;
36
35
37
36
type decode_ctxt = @{
38
37
cdata : cstore:: crate_metadata ,
@@ -53,123 +52,49 @@ iface tr {
53
52
// ______________________________________________________________________
54
53
// Top-level methods.
55
54
56
- // The type inline_fn should be a type that can represent both methods
57
- // and top-level items. As it happens, the type ast::method is perfect
58
- // for this purpose, but I use this typedef just to keep clear when
59
- // the thing may not, in fact, be an actual method in the AST but
60
- // rather some sort of function.
61
- enum inline_fn = @ast:: method;
62
-
63
55
fn encode_inlined_item ( ecx : @e:: encode_ctxt ,
64
56
ebml_w : ebml:: writer ,
65
57
path : ast_map:: path ,
66
- item : @ast:: item ) {
67
- let ifn = inline_fn ( alt item. node {
68
- ast:: item_fn ( decl, tps, body) {
69
- @{ ident: item. ident ,
70
- attrs: item. attrs ,
71
- tps: tps,
72
- decl: decl,
73
- body: body,
74
- id: item. id ,
75
- span: item. span }
76
- }
77
-
78
- _ {
79
- ecx. ccx . sess . span_bug ( item. span , "Cannot inline non-function" )
80
- }
81
- } ) ;
82
-
83
- encode_inlined_fn ( ecx, ebml_w, path, ifn) ;
84
- }
85
-
86
- fn decode_inlined_item ( cdata : cstore:: crate_metadata ,
87
- tcx : ty:: ctxt ,
88
- maps : maps ,
89
- path : ast_map:: path ,
90
- par_doc : ebml:: doc ) -> option < @ast:: item > {
91
- let oifn = decode_inlined_fn ( cdata, tcx, maps, path, par_doc) ;
92
- option:: map ( oifn) { |ifn|
93
- let item = @{ ident: ifn. ident ,
94
- attrs: ifn. attrs ,
95
- id: ifn. id ,
96
- node: ast:: item_fn ( ifn. decl , ifn. tps , ifn. body ) ,
97
- span: ifn. span } ;
98
- ast_map:: map_decoded_item ( tcx. items , path, item) ;
99
- item
100
- }
101
- }
102
-
103
- fn encode_inlined_method ( ecx : @e:: encode_ctxt ,
104
- ebml_w : ebml:: writer ,
105
- path : ast_map:: path ,
106
- mthd : @ast:: method ) {
107
- encode_inlined_fn ( ecx, ebml_w, path, inline_fn ( mthd) )
108
- }
109
-
110
- fn decode_inlined_method ( cdata : cstore:: crate_metadata ,
111
- tcx : ty:: ctxt ,
112
- maps : maps ,
113
- path : ast_map:: path ,
114
- par_doc : ebml:: doc ) -> option < @ast:: method > {
115
- let oifn = decode_inlined_fn ( cdata, tcx, maps, path, par_doc) ;
116
- option:: map ( oifn) { |ifn|
117
- ast_map:: map_decoded_method ( tcx. items , path, * ifn) ;
118
- * ifn
119
- }
120
- }
121
-
122
- fn encode_inlined_fn ( ecx : @e:: encode_ctxt ,
123
- ebml_w : ebml:: writer ,
124
- path : ast_map:: path ,
125
- ifn : inline_fn ) {
126
-
58
+ ii : ast:: inlined_item ) {
127
59
#debug[ "> Encoding inlined item: %s::%s (%u)" ,
128
- ast_map:: path_to_str ( path) ,
129
- ifn. ident ,
60
+ ast_map:: path_to_str ( path) , ii. ident ( ) ,
130
61
ebml_w. writer . tell ( ) ] ;
131
62
132
- let id_range = compute_id_range ( ifn ) ;
63
+ let id_range = compute_id_range ( ii ) ;
133
64
ebml_w. wr_tag ( c:: tag_ast as uint ) { ||
134
65
encode_id_range ( ebml_w, id_range) ;
135
- encode_ast ( ebml_w, ifn ) ;
136
- encode_side_tables_for_ifn ( ecx, ebml_w, ifn ) ;
66
+ encode_ast ( ebml_w, ii ) ;
67
+ encode_side_tables_for_ii ( ecx, ebml_w, ii ) ;
137
68
}
138
69
139
70
#debug[ "< Encoded inlined fn: %s::%s (%u)" ,
140
- ast_map:: path_to_str ( path) ,
141
- ifn. ident ,
71
+ ast_map:: path_to_str ( path) , ii. ident ( ) ,
142
72
ebml_w. writer . tell ( ) ] ;
143
73
}
144
74
145
- // Decodes the inlined function and associated side tables. Does
146
- // *not* insert the function into the ast_map, since the correct way
147
- // to do this depends on whether this is an inlined item or method;
148
- // therefore, you ought to be invoking decode_inlined_item() or
149
- // decode_inlined_method() and not this helper function.
150
- fn decode_inlined_fn ( cdata : cstore:: crate_metadata ,
151
- tcx : ty:: ctxt ,
152
- maps : maps ,
153
- path : ast_map:: path ,
154
- par_doc : ebml:: doc ) -> option < inline_fn > {
75
+ fn decode_inlined_item ( cdata : cstore:: crate_metadata ,
76
+ tcx : ty:: ctxt ,
77
+ maps : maps ,
78
+ path : ast_map:: path ,
79
+ par_doc : ebml:: doc ) -> option < ast:: inlined_item > {
155
80
let dcx = @{ cdata: cdata, tcx: tcx, maps: maps} ;
156
81
alt par_doc. opt_child ( c:: tag_ast) {
157
82
none { none }
158
83
some( ast_doc) {
159
- #debug[ "> Decoding inlined fn: %s" , ast_map:: path_to_str ( path) ] ;
84
+ #debug[ "> Decoding inlined fn: %s::? " , ast_map:: path_to_str ( path) ] ;
160
85
let from_id_range = decode_id_range ( ast_doc) ;
161
86
let to_id_range = reserve_id_range ( dcx. tcx . sess , from_id_range) ;
162
87
let xcx = @{ dcx: dcx,
163
88
from_id_range: from_id_range,
164
89
to_id_range: to_id_range} ;
165
- let raw_ifn = decode_ast ( ast_doc) ;
166
- let ifn = renumber_ast ( xcx, raw_ifn) ;
167
- #debug[ "Fn named: %s" , ifn. ident ] ;
90
+ let raw_ii = decode_ast ( ast_doc) ;
91
+ let ii = renumber_ast ( xcx, raw_ii) ;
92
+ ast_map:: map_decoded_item ( dcx. tcx . items , path, ii) ;
93
+ #debug[ "Fn named: %s" , ii. ident ( ) ] ;
168
94
decode_side_tables ( xcx, ast_doc) ;
169
95
#debug[ "< Decoded inlined fn: %s::%s" ,
170
- ast_map:: path_to_str ( path) ,
171
- ifn. ident ] ;
172
- some ( ifn)
96
+ ast_map:: path_to_str ( path) , ii. ident ( ) ] ;
97
+ some ( ii)
173
98
}
174
99
}
175
100
}
@@ -183,7 +108,7 @@ fn empty(range: id_range) -> bool {
183
108
range. min >= range. max
184
109
}
185
110
186
- fn visit_ids ( ifn : inline_fn , vfn : fn @( ast:: node_id ) ) {
111
+ fn visit_ids ( item : ast :: inlined_item , vfn : fn @( ast:: node_id ) ) {
187
112
let visitor = visit:: mk_simple_visitor ( @{
188
113
visit_mod : fn @( _m: ast:: _mod, _sp: span, id: ast:: node_id) {
189
114
vfn ( id)
@@ -292,13 +217,13 @@ fn visit_ids(ifn: inline_fn, vfn: fn@(ast::node_id)) {
292
217
}
293
218
} ) ;
294
219
295
- visit :: visit_method_helper ( * ifn , ( ) , visitor) ;
220
+ item . accept ( ( ) , visitor)
296
221
}
297
222
298
- fn compute_id_range ( ifn : inline_fn ) -> id_range {
223
+ fn compute_id_range ( item : ast :: inlined_item ) -> id_range {
299
224
let min = @mutable int:: max_value;
300
225
let max = @mutable int:: min_value;
301
- visit_ids ( ifn ) { |id|
226
+ visit_ids ( item ) { |id|
302
227
* min = int:: min ( * min, id) ;
303
228
* max = int:: max ( * max, id + 1 ) ;
304
229
}
@@ -395,25 +320,34 @@ impl deserializer_helpers<D: serialization::deserializer> for D {
395
320
// We also have to adjust the spans: for now we just insert a dummy span,
396
321
// but eventually we should add entries to the local codemap as required.
397
322
398
- fn encode_ast ( ebml_w : ebml:: writer , ifn : inline_fn ) {
323
+ fn encode_ast ( ebml_w : ebml:: writer , item : ast :: inlined_item ) {
399
324
ebml_w. wr_tag ( c:: tag_tree as uint ) { ||
400
- astencode_gen:: serialize_syntax_ast_method ( ebml_w, * * ifn )
325
+ astencode_gen:: serialize_syntax_ast_inlined_item ( ebml_w, item )
401
326
}
402
327
}
403
328
404
- fn decode_ast ( par_doc : ebml:: doc ) -> inline_fn {
329
+ fn decode_ast ( par_doc : ebml:: doc ) -> ast :: inlined_item {
405
330
let chi_doc = par_doc[ c:: tag_tree] ;
406
331
let d = serialization:: mk_ebml_deserializer ( chi_doc) ;
407
- inline_fn ( @ astencode_gen:: deserialize_syntax_ast_method ( d ) )
332
+ astencode_gen:: deserialize_syntax_ast_inlined_item ( d )
408
333
}
409
334
410
- fn renumber_ast ( xcx : extended_decode_ctxt , ifn : inline_fn ) -> inline_fn {
335
+ fn renumber_ast ( xcx : extended_decode_ctxt , ii : ast:: inlined_item )
336
+ -> ast:: inlined_item {
411
337
let fld = fold:: make_fold ( {
412
338
new_id: xcx. tr_id ( _) ,
413
339
new_span: xcx. tr_span ( _)
414
340
with * fold:: default_ast_fold ( )
415
341
} ) ;
416
- inline_fn ( fld. fold_method ( * ifn) )
342
+
343
+ alt ii {
344
+ ast : : ii_item ( i) {
345
+ ast:: ii_item ( fld. fold_item ( i) )
346
+ }
347
+ ast:: ii_method ( d, m) {
348
+ ast:: ii_method ( xcx. tr_def_id ( d) , fld. fold_method ( m) )
349
+ }
350
+ }
417
351
}
418
352
419
353
// ______________________________________________________________________
@@ -664,11 +598,11 @@ impl writer for ebml::writer {
664
598
}
665
599
}
666
600
667
- fn encode_side_tables_for_ifn ( ecx : @e:: encode_ctxt ,
668
- ebml_w : ebml:: writer ,
669
- ifn : inline_fn ) {
601
+ fn encode_side_tables_for_ii ( ecx : @e:: encode_ctxt ,
602
+ ebml_w : ebml:: writer ,
603
+ ii : ast :: inlined_item ) {
670
604
ebml_w. wr_tag ( c:: tag_table as uint ) { ||
671
- visit_ids ( ifn , fn @( id: ast:: node_id) {
605
+ visit_ids ( ii , fn @( id: ast:: node_id) {
672
606
// Note: this will cause a copy of ebml_w, which is bad as
673
607
// it has mutable fields. But I believe it's harmless since
674
608
// we generate balanced EBML.
0 commit comments