@@ -9,6 +9,7 @@ import std::option;
9
9
import std:: option:: some;
10
10
import std:: option:: none;
11
11
import std:: ebml;
12
+ import std:: map;
12
13
import syntax:: ast:: * ;
13
14
import common:: * ;
14
15
import middle:: trans:: crate_ctxt;
@@ -19,6 +20,11 @@ import front::attr;
19
20
export encode_metadata;
20
21
export encoded_ty;
21
22
23
+ type abbrev_map = map:: hashmap [ ty:: t, tyencode:: ty_abbrev] ;
24
+
25
+ type encode_ctxt = rec ( @crate_ctxt ccx ,
26
+ abbrev_map type_abbrevs ) ;
27
+
22
28
// Path table encoding
23
29
fn encode_name ( & ebml:: writer ebml_w, & str name ) {
24
30
ebml:: start_tag ( ebml_w, tag_paths_data_name) ;
@@ -175,27 +181,27 @@ fn encode_variant_id(&ebml::writer ebml_w, &def_id vid) {
175
181
ebml:: end_tag( ebml_w) ;
176
182
}
177
183
178
- fn encode_type( & @crate_ctxt cx , & ebml:: writer ebml_w, & ty:: t typ) {
184
+ fn encode_type( & @encode_ctxt ecx , & ebml:: writer ebml_w, & ty:: t typ) {
179
185
ebml:: start_tag( ebml_w, tag_items_data_item_type) ;
180
186
auto f = def_to_str;
181
187
auto ty_str_ctxt =
182
- @rec( ds=f, tcx=cx . tcx,
183
- abbrevs=tyencode:: ac_use_abbrevs( cx . type_abbrevs) ) ;
188
+ @rec( ds=f, tcx=ecx . ccx . tcx,
189
+ abbrevs=tyencode:: ac_use_abbrevs( ecx . type_abbrevs) ) ;
184
190
tyencode:: enc_ty( io:: new_writer_( ebml_w. writer) , ty_str_ctxt, typ) ;
185
191
ebml:: end_tag( ebml_w) ;
186
192
}
187
193
188
- fn encode_symbol( & @crate_ctxt cx , & ebml:: writer ebml_w,
194
+ fn encode_symbol( & @encode_ctxt ecx , & ebml:: writer ebml_w,
189
195
node_id id) {
190
196
ebml:: start_tag( ebml_w, tag_items_data_item_symbol) ;
191
- ebml_w. writer. write( str:: bytes( cx . item_symbols. get( id) ) ) ;
197
+ ebml_w. writer. write( str:: bytes( ecx . ccx . item_symbols. get( id) ) ) ;
192
198
ebml:: end_tag( ebml_w) ;
193
199
}
194
200
195
- fn encode_discriminant( & @crate_ctxt cx , & ebml:: writer ebml_w,
201
+ fn encode_discriminant( & @encode_ctxt ecx , & ebml:: writer ebml_w,
196
202
node_id id) {
197
203
ebml:: start_tag( ebml_w, tag_items_data_item_symbol) ;
198
- ebml_w. writer. write( str:: bytes( cx . discrim_symbols. get( id) ) ) ;
204
+ ebml_w. writer. write( str:: bytes( ecx . ccx . discrim_symbols. get( id) ) ) ;
199
205
ebml:: end_tag( ebml_w) ;
200
206
}
201
207
@@ -205,7 +211,7 @@ fn encode_tag_id(&ebml::writer ebml_w, &def_id id) {
205
211
ebml:: end_tag( ebml_w) ;
206
212
}
207
213
208
- fn encode_tag_variant_info( & @crate_ctxt cx , & ebml:: writer ebml_w,
214
+ fn encode_tag_variant_info( & @encode_ctxt ecx , & ebml:: writer ebml_w,
209
215
node_id id, & variant[ ] variants,
210
216
& mutable vec[ tup( int, uint) ] index,
211
217
& ty_param[ ] ty_params) {
@@ -215,25 +221,27 @@ fn encode_tag_variant_info(&@crate_ctxt cx, &ebml::writer ebml_w,
215
221
encode_def_id( ebml_w, local_def( variant. node. id) ) ;
216
222
encode_kind( ebml_w, 'v' as u8 ) ;
217
223
encode_tag_id( ebml_w, local_def( id) ) ;
218
- encode_type( cx, ebml_w, node_id_to_monotype( cx. tcx, variant. node. id) ) ;
224
+ encode_type( ecx, ebml_w,
225
+ node_id_to_monotype( ecx. ccx. tcx, variant. node. id) ) ;
219
226
if ( ivec:: len[ variant_arg] ( variant. node. args) > 0 u) {
220
- encode_symbol( cx , ebml_w, variant. node. id) ;
227
+ encode_symbol( ecx , ebml_w, variant. node. id) ;
221
228
}
222
- encode_discriminant( cx , ebml_w, variant. node. id) ;
229
+ encode_discriminant( ecx , ebml_w, variant. node. id) ;
223
230
encode_type_param_count( ebml_w, ty_params) ;
224
231
ebml:: end_tag( ebml_w) ;
225
232
}
226
233
}
227
234
228
- fn encode_info_for_item( @crate_ctxt cx , & ebml:: writer ebml_w,
235
+ fn encode_info_for_item( @encode_ctxt ecx , & ebml:: writer ebml_w,
229
236
@item item, & mutable vec[ tup( int, uint) ] index) {
230
237
alt ( item. node) {
231
238
case ( item_const( _, _) ) {
232
239
ebml:: start_tag( ebml_w, tag_items_data_item) ;
233
240
encode_def_id( ebml_w, local_def( item. id) ) ;
234
241
encode_kind( ebml_w, 'c' as u8 ) ;
235
- encode_type( cx, ebml_w, node_id_to_monotype( cx. tcx, item. id) ) ;
236
- encode_symbol( cx, ebml_w, item. id) ;
242
+ encode_type( ecx, ebml_w,
243
+ node_id_to_monotype( ecx. ccx. tcx, item. id) ) ;
244
+ encode_symbol( ecx, ebml_w, item. id) ;
237
245
ebml:: end_tag( ebml_w) ;
238
246
}
239
247
case ( item_fn( ?fd, ?tps) ) {
@@ -243,8 +251,9 @@ fn encode_info_for_item(@crate_ctxt cx, &ebml::writer ebml_w,
243
251
case ( pure_fn) { 'p' }
244
252
case ( impure_fn) { 'f' } } as u8) ;
245
253
encode_type_param_count( ebml_w, tps) ;
246
- encode_type( cx, ebml_w, node_id_to_monotype( cx. tcx, item. id) ) ;
247
- encode_symbol( cx, ebml_w, item. id) ;
254
+ encode_type( ecx, ebml_w,
255
+ node_id_to_monotype( ecx. ccx. tcx, item. id) ) ;
256
+ encode_symbol( ecx, ebml_w, item. id) ;
248
257
ebml:: end_tag( ebml_w) ;
249
258
}
250
259
case ( item_mod( _) ) {
@@ -264,99 +273,102 @@ fn encode_info_for_item(@crate_ctxt cx, &ebml::writer ebml_w,
264
273
encode_def_id( ebml_w, local_def( item. id) ) ;
265
274
encode_kind( ebml_w, 'y' as u8 ) ;
266
275
encode_type_param_count( ebml_w, tps) ;
267
- encode_type( cx, ebml_w, node_id_to_monotype( cx. tcx, item. id) ) ;
276
+ encode_type( ecx, ebml_w,
277
+ node_id_to_monotype( ecx. ccx. tcx, item. id) ) ;
268
278
ebml:: end_tag( ebml_w) ;
269
279
}
270
280
case ( item_tag( ?variants, ?tps) ) {
271
281
ebml:: start_tag( ebml_w, tag_items_data_item) ;
272
282
encode_def_id( ebml_w, local_def( item. id) ) ;
273
283
encode_kind( ebml_w, 't' as u8 ) ;
274
284
encode_type_param_count( ebml_w, tps) ;
275
- encode_type( cx, ebml_w, node_id_to_monotype( cx. tcx, item. id) ) ;
285
+ encode_type( ecx, ebml_w,
286
+ node_id_to_monotype( ecx. ccx. tcx, item. id) ) ;
276
287
for ( variant v in variants) {
277
288
encode_variant_id( ebml_w, local_def( v. node. id) ) ;
278
289
}
279
290
ebml:: end_tag( ebml_w) ;
280
- encode_tag_variant_info( cx , ebml_w, item. id, variants, index,
291
+ encode_tag_variant_info( ecx , ebml_w, item. id, variants, index,
281
292
tps) ;
282
293
}
283
294
case ( item_res( _, _, ?tps, ?ctor_id) ) {
284
- auto fn_ty = node_id_to_monotype( cx . tcx, ctor_id) ;
295
+ auto fn_ty = node_id_to_monotype( ecx . ccx . tcx, ctor_id) ;
285
296
286
297
ebml:: start_tag( ebml_w, tag_items_data_item) ;
287
298
encode_def_id( ebml_w, local_def( ctor_id) ) ;
288
299
encode_kind( ebml_w, 'y' as u8 ) ;
289
300
encode_type_param_count( ebml_w, tps) ;
290
- encode_type( cx , ebml_w, ty:: ty_fn_ret( cx . tcx, fn_ty) ) ;
291
- encode_symbol( cx , ebml_w, item. id) ;
301
+ encode_type( ecx , ebml_w, ty:: ty_fn_ret( ecx . ccx . tcx, fn_ty) ) ;
302
+ encode_symbol( ecx , ebml_w, item. id) ;
292
303
ebml:: end_tag( ebml_w) ;
293
304
294
305
index += [ tup( ctor_id, ebml_w. writer. tell( ) ) ] ;
295
306
ebml:: start_tag( ebml_w, tag_items_data_item) ;
296
307
encode_def_id( ebml_w, local_def( ctor_id) ) ;
297
308
encode_kind( ebml_w, 'f' as u8 ) ;
298
309
encode_type_param_count( ebml_w, tps) ;
299
- encode_type( cx , ebml_w, fn_ty) ;
300
- encode_symbol( cx , ebml_w, ctor_id) ;
310
+ encode_type( ecx , ebml_w, fn_ty) ;
311
+ encode_symbol( ecx , ebml_w, ctor_id) ;
301
312
ebml:: end_tag( ebml_w) ;
302
313
}
303
314
case ( item_obj( _, ?tps, ?ctor_id) ) {
304
- auto fn_ty = node_id_to_monotype( cx . tcx, ctor_id) ;
315
+ auto fn_ty = node_id_to_monotype( ecx . ccx . tcx, ctor_id) ;
305
316
306
317
ebml:: start_tag( ebml_w, tag_items_data_item) ;
307
318
encode_def_id( ebml_w, local_def( item. id) ) ;
308
319
encode_kind( ebml_w, 'y' as u8 ) ;
309
320
encode_type_param_count( ebml_w, tps) ;
310
- encode_type( cx , ebml_w, ty:: ty_fn_ret( cx . tcx, fn_ty) ) ;
321
+ encode_type( ecx , ebml_w, ty:: ty_fn_ret( ecx . ccx . tcx, fn_ty) ) ;
311
322
ebml:: end_tag( ebml_w) ;
312
323
313
324
index += [ tup( ctor_id, ebml_w. writer. tell( ) ) ] ;
314
325
ebml:: start_tag( ebml_w, tag_items_data_item) ;
315
326
encode_def_id( ebml_w, local_def( ctor_id) ) ;
316
327
encode_kind( ebml_w, 'f' as u8 ) ;
317
328
encode_type_param_count( ebml_w, tps) ;
318
- encode_type( cx , ebml_w, fn_ty) ;
319
- encode_symbol( cx , ebml_w, ctor_id) ;
329
+ encode_type( ecx , ebml_w, fn_ty) ;
330
+ encode_symbol( ecx , ebml_w, ctor_id) ;
320
331
ebml:: end_tag( ebml_w) ;
321
332
}
322
333
}
323
334
}
324
335
325
- fn encode_info_for_native_item( & @crate_ctxt cx , & ebml:: writer ebml_w,
336
+ fn encode_info_for_native_item( & @encode_ctxt ecx , & ebml:: writer ebml_w,
326
337
& @native_item nitem) {
327
338
ebml:: start_tag( ebml_w, tag_items_data_item) ;
328
339
alt ( nitem. node) {
329
340
case ( native_item_ty) {
330
341
encode_def_id( ebml_w, local_def( nitem. id) ) ;
331
342
encode_kind( ebml_w, 'T' as u8 ) ;
332
- encode_type( cx , ebml_w,
333
- ty:: mk_native( cx . tcx, local_def( nitem. id) ) ) ;
343
+ encode_type( ecx , ebml_w,
344
+ ty:: mk_native( ecx . ccx . tcx, local_def( nitem. id) ) ) ;
334
345
}
335
346
case ( native_item_fn( _, _, ?tps) ) {
336
347
encode_def_id( ebml_w, local_def( nitem. id) ) ;
337
348
encode_kind( ebml_w, 'F' as u8 ) ;
338
349
encode_type_param_count( ebml_w, tps) ;
339
- encode_type( cx, ebml_w, node_id_to_monotype( cx. tcx, nitem. id) ) ;
340
- encode_symbol( cx, ebml_w, nitem. id) ;
350
+ encode_type( ecx, ebml_w,
351
+ node_id_to_monotype( ecx. ccx. tcx, nitem. id) ) ;
352
+ encode_symbol( ecx, ebml_w, nitem. id) ;
341
353
}
342
354
}
343
355
ebml:: end_tag( ebml_w) ;
344
356
}
345
357
346
- fn encode_info_for_items( & @crate_ctxt cx , & ebml:: writer ebml_w) ->
358
+ fn encode_info_for_items( & @encode_ctxt ecx , & ebml:: writer ebml_w) ->
347
359
vec[ tup( int, uint) ] {
348
360
let vec[ tup( int, uint) ] index = [ ] ;
349
361
ebml:: start_tag( ebml_w, tag_items_data) ;
350
362
for each ( @tup( node_id, middle:: ast_map:: ast_node) kvp in
351
- cx . ast_map. items( ) ) {
363
+ ecx . ccx . ast_map. items( ) ) {
352
364
alt ( kvp. _1) {
353
365
case ( middle:: ast_map:: node_item( ?i) ) {
354
366
index += [ tup( kvp. _0, ebml_w. writer. tell( ) ) ] ;
355
- encode_info_for_item( cx , ebml_w, i, index) ;
367
+ encode_info_for_item( ecx , ebml_w, i, index) ;
356
368
}
357
369
case ( middle:: ast_map:: node_native_item( ?i) ) {
358
370
index += [ tup( kvp. _0, ebml_w. writer. tell( ) ) ] ;
359
- encode_info_for_native_item( cx , ebml_w, i) ;
371
+ encode_info_for_native_item( ecx , ebml_w, i) ;
360
372
}
361
373
case ( _) { }
362
374
}
@@ -460,19 +472,19 @@ fn encode_attributes(&ebml::writer ebml_w, &vec[attribute] attrs) {
460
472
// metadata that Rust cares about for linking crates. This attribute requires
461
473
// 'name' and 'vers' items, so if the user didn't provide them we will throw
462
474
// them in anyway with default values.
463
- fn synthesize_crate_attrs( & @crate_ctxt cx ,
475
+ fn synthesize_crate_attrs( & @encode_ctxt ecx ,
464
476
& @crate crate ) -> vec[ attribute] {
465
477
466
- fn synthesize_link_attr( & @crate_ctxt cx , & ( @meta_item) [ ] items)
478
+ fn synthesize_link_attr( & @encode_ctxt ecx , & ( @meta_item) [ ] items)
467
479
-> attribute {
468
480
469
- assert cx . link_meta. name != "";
470
- assert cx . link_meta. vers != "";
481
+ assert ecx . ccx . link_meta. name != "";
482
+ assert ecx . ccx . link_meta. vers != "";
471
483
472
484
auto name_item = attr:: mk_name_value_item_str( "name",
473
- cx . link_meta. name) ;
485
+ ecx . ccx . link_meta. name) ;
474
486
auto vers_item = attr:: mk_name_value_item_str( "vers",
475
- cx . link_meta. vers) ;
487
+ ecx . ccx . link_meta. vers) ;
476
488
477
489
auto other_items = {
478
490
auto tmp = attr:: remove_meta_items_by_name( items, "name") ;
@@ -494,26 +506,30 @@ fn synthesize_crate_attrs(&@crate_ctxt cx,
494
506
alt ( attr. node. value. node) {
495
507
case ( meta_list( ?n, ?l) ) {
496
508
found_link_attr = true;
497
- [ synthesize_link_attr( cx , l) ]
509
+ [ synthesize_link_attr( ecx , l) ]
498
510
}
499
511
case ( _) { [ attr] }
500
512
}
501
513
}
502
514
}
503
515
504
516
if ( !found_link_attr) {
505
- attrs += [ synthesize_link_attr( cx , ~[ ] ) ] ;
517
+ attrs += [ synthesize_link_attr( ecx , ~[ ] ) ] ;
506
518
}
507
519
508
520
ret attrs;
509
521
}
510
522
511
523
fn encode_metadata( & @crate_ctxt cx, & @crate crate ) -> str {
524
+
525
+ auto abbrevs = map:: mk_hashmap( ty:: hash_ty, ty:: eq_ty) ;
526
+ auto ecx = @rec( ccx = cx, type_abbrevs = abbrevs) ;
527
+
512
528
auto string_w = io:: string_writer( ) ;
513
529
auto buf_w = string_w. get_writer( ) . get_buf_writer( ) ;
514
530
auto ebml_w = ebml:: create_writer( buf_w) ;
515
531
516
- auto crate_attrs = synthesize_crate_attrs( cx , crate ) ;
532
+ auto crate_attrs = synthesize_crate_attrs( ecx , crate ) ;
517
533
encode_attributes( ebml_w, crate_attrs) ;
518
534
// Encode and index the paths.
519
535
@@ -527,7 +543,7 @@ fn encode_metadata(&@crate_ctxt cx, &@crate crate) -> str {
527
543
// Encode and index the items.
528
544
529
545
ebml:: start_tag( ebml_w, tag_items) ;
530
- auto items_index = encode_info_for_items( cx , ebml_w) ;
546
+ auto items_index = encode_info_for_items( ecx , ebml_w) ;
531
547
auto int_writer = write_int;
532
548
auto item_hasher = hash_node_id;
533
549
auto items_buckets = create_index[ int] ( items_index, item_hasher) ;
@@ -544,7 +560,7 @@ fn encode_metadata(&@crate_ctxt cx, &@crate crate) -> str {
544
560
fn encoded_ty( & ty:: ctxt tcx, & ty:: t t) -> str {
545
561
auto cx = @rec( ds = def_to_str,
546
562
tcx = tcx,
547
- abbrevs = metadata :: tyencode:: ac_no_abbrevs) ;
563
+ abbrevs = tyencode:: ac_no_abbrevs) ;
548
564
auto sw = io:: string_writer( ) ;
549
565
tyencode:: enc_ty( sw. get_writer( ) , cx, t) ;
550
566
ret sw. get_str( ) ;
0 commit comments