1
1
import std. _str ;
2
+ import std. _uint ;
2
3
import std. _vec ;
3
4
import std. ebml ;
4
5
import std. io ;
@@ -22,15 +23,23 @@ const uint tag_paths_name = 0x03u;
22
23
const uint tag_paths_item = 0x04 u;
23
24
const uint tag_paths_mod = 0x05 u;
24
25
25
- const uint tag_items_item = 0x06 u;
26
- const uint tag_items_def_id = 0x07 u;
27
- const uint tag_items_kind = 0x08 u;
28
- const uint tag_items_ty_param = 0x09 u;
29
- const uint tag_items_type = 0x0a u;
30
- const uint tag_items_symbol = 0x0b u;
31
- const uint tag_items_variant = 0x0c u;
32
- const uint tag_items_tag_id = 0x0d u;
33
- const uint tag_items_obj_type_id = 0x0e u;
26
+ const uint tag_def_id = 0x06 u;
27
+
28
+ const uint tag_items_data = 0x07 u;
29
+ const uint tag_items_data_item = 0x08 u;
30
+ const uint tag_items_data_item_kind = 0x09 u;
31
+ const uint tag_items_data_item_ty_param = 0x0a u;
32
+ const uint tag_items_data_item_type = 0x0b u;
33
+ const uint tag_items_data_item_symbol = 0x0c u;
34
+ const uint tag_items_data_item_variant = 0x0d u;
35
+ const uint tag_items_data_item_tag_id = 0x0e u;
36
+ const uint tag_items_data_item_obj_type_id = 0x0f u;
37
+
38
+ const uint tag_items_index = 0x10 u;
39
+ const uint tag_items_index_buckets = 0x11 u;
40
+ const uint tag_items_index_buckets_bucket = 0x12 u;
41
+ const uint tag_items_index_buckets_bucket_elt = 0x13 u;
42
+ const uint tag_items_index_table = 0x14 u;
34
43
35
44
// Type encoding
36
45
@@ -161,7 +170,7 @@ fn encode_name(&ebml.writer ebml_w, str name) {
161
170
}
162
171
163
172
fn encode_def_id ( & ebml. writer ebml_w , & ast. def_id id ) {
164
- ebml. start_tag ( ebml_w, tag_items_def_id ) ;
173
+ ebml. start_tag ( ebml_w, tag_def_id ) ;
165
174
ebml_w. writer . write ( _str. bytes ( def_to_str ( id) ) ) ;
166
175
ebml. end_tag ( ebml_w) ;
167
176
}
@@ -259,7 +268,7 @@ fn encode_item_paths(&ebml.writer ebml_w, @ast.crate crate) {
259
268
// Item info table encoding
260
269
261
270
fn encode_kind ( & ebml. writer ebml_w , u8 c) {
262
- ebml. start_tag ( ebml_w, tag_items_kind ) ;
271
+ ebml. start_tag ( ebml_w, tag_items_data_item_kind ) ;
263
272
ebml_w. writer . write ( vec ( c) ) ;
264
273
ebml. end_tag ( ebml_w) ;
265
274
}
@@ -271,40 +280,40 @@ fn def_to_str(ast.def_id did) -> str {
271
280
// TODO: We need to encode the "crate numbers" somewhere for diamond imports.
272
281
fn encode_type_params ( & ebml. writer ebml_w , vec[ ast. ty_param] tps ) {
273
282
for ( ast. ty_param tp in tps) {
274
- ebml. start_tag ( ebml_w, tag_items_ty_param ) ;
283
+ ebml. start_tag ( ebml_w, tag_items_data_item_ty_param ) ;
275
284
ebml_w. writer . write ( _str. bytes ( def_to_str ( tp. id ) ) ) ;
276
285
ebml. end_tag ( ebml_w) ;
277
286
}
278
287
}
279
288
280
289
fn encode_type ( & ebml. writer ebml_w , @ty. t typ ) {
281
- ebml. start_tag ( ebml_w, tag_items_type ) ;
290
+ ebml. start_tag ( ebml_w, tag_items_data_item_type ) ;
282
291
auto f = def_to_str;
283
292
ebml_w. writer . write ( _str. bytes ( ty_str ( typ, f) ) ) ;
284
293
ebml. end_tag ( ebml_w) ;
285
294
}
286
295
287
296
fn encode_symbol ( @trans. crate_ctxt cx , & ebml. writer ebml_w , ast. def_id did ) {
288
- ebml. start_tag ( ebml_w, tag_items_symbol ) ;
297
+ ebml. start_tag ( ebml_w, tag_items_data_item_symbol ) ;
289
298
ebml_w. writer . write ( _str. bytes ( cx. item_symbols . get ( did) ) ) ;
290
299
ebml. end_tag ( ebml_w) ;
291
300
}
292
301
293
302
fn encode_discriminant ( @trans. crate_ctxt cx , & ebml. writer ebml_w ,
294
303
ast. def_id did ) {
295
- ebml. start_tag ( ebml_w, tag_items_symbol ) ;
304
+ ebml. start_tag ( ebml_w, tag_items_data_item_symbol ) ;
296
305
ebml_w. writer . write ( _str. bytes ( cx. discrim_symbols . get ( did) ) ) ;
297
306
ebml. end_tag ( ebml_w) ;
298
307
}
299
308
300
309
fn encode_tag_id ( & ebml. writer ebml_w , & ast. def_id id ) {
301
- ebml. start_tag ( ebml_w, tag_items_tag_id ) ;
310
+ ebml. start_tag ( ebml_w, tag_items_data_item_tag_id ) ;
302
311
ebml_w. writer . write ( _str. bytes ( def_to_str ( id) ) ) ;
303
312
ebml. end_tag ( ebml_w) ;
304
313
}
305
314
306
315
fn encode_obj_type_id ( & ebml. writer ebml_w , & ast. def_id id ) {
307
- ebml. start_tag ( ebml_w, tag_items_obj_type_id ) ;
316
+ ebml. start_tag ( ebml_w, tag_items_data_item_obj_type_id ) ;
308
317
ebml_w. writer . write ( _str. bytes ( def_to_str ( id) ) ) ;
309
318
ebml. end_tag ( ebml_w) ;
310
319
}
@@ -313,7 +322,7 @@ fn encode_obj_type_id(&ebml.writer ebml_w, &ast.def_id id) {
313
322
fn encode_tag_variant_info ( @trans. crate_ctxt cx , & ebml. writer ebml_w ,
314
323
ast. def_id did , vec[ ast. variant] variants ) {
315
324
for ( ast. variant variant in variants) {
316
- ebml. start_tag ( ebml_w, tag_items_item ) ;
325
+ ebml. start_tag ( ebml_w, tag_items_data_item ) ;
317
326
encode_def_id ( ebml_w, variant. node . id ) ;
318
327
encode_kind ( ebml_w, 'v' as u8 ) ;
319
328
encode_tag_id ( ebml_w, did) ;
@@ -330,15 +339,15 @@ fn encode_info_for_item(@trans.crate_ctxt cx, &ebml.writer ebml_w,
330
339
@ast . item item) {
331
340
alt ( item. node ) {
332
341
case ( ast. item_const ( _, _, _, ?did, ?ann) ) {
333
- ebml. start_tag ( ebml_w, tag_items_item ) ;
342
+ ebml. start_tag ( ebml_w, tag_items_data_item ) ;
334
343
encode_def_id ( ebml_w, did) ;
335
344
encode_kind ( ebml_w, 'c' as u8 ) ;
336
345
encode_type ( ebml_w, trans. node_ann_type ( cx, ann) ) ;
337
346
encode_symbol ( cx, ebml_w, did) ;
338
347
ebml. end_tag ( ebml_w) ;
339
348
}
340
349
case ( ast. item_fn ( _, _, ?tps, ?did, ?ann) ) {
341
- ebml. start_tag ( ebml_w, tag_items_item ) ;
350
+ ebml. start_tag ( ebml_w, tag_items_data_item ) ;
342
351
encode_def_id ( ebml_w, did) ;
343
352
encode_kind ( ebml_w, 'f' as u8 ) ;
344
353
encode_type_params ( ebml_w, tps) ;
@@ -347,27 +356,27 @@ fn encode_info_for_item(@trans.crate_ctxt cx, &ebml.writer ebml_w,
347
356
ebml. end_tag ( ebml_w) ;
348
357
}
349
358
case ( ast. item_mod ( _, _, ?did) ) {
350
- ebml. start_tag ( ebml_w, tag_items_item ) ;
359
+ ebml. start_tag ( ebml_w, tag_items_data_item ) ;
351
360
encode_def_id ( ebml_w, did) ;
352
361
encode_kind ( ebml_w, 'm' as u8 ) ;
353
362
ebml. end_tag ( ebml_w) ;
354
363
}
355
364
case ( ast. item_native_mod ( _, _, ?did) ) {
356
- ebml. start_tag ( ebml_w, tag_items_item ) ;
365
+ ebml. start_tag ( ebml_w, tag_items_data_item ) ;
357
366
encode_def_id ( ebml_w, did) ;
358
367
encode_kind ( ebml_w, 'n' as u8 ) ;
359
368
ebml. end_tag ( ebml_w) ;
360
369
}
361
370
case ( ast. item_ty ( ?id, _, ?tps, ?did, ?ann) ) {
362
- ebml. start_tag ( ebml_w, tag_items_item ) ;
371
+ ebml. start_tag ( ebml_w, tag_items_data_item ) ;
363
372
encode_def_id ( ebml_w, did) ;
364
373
encode_kind ( ebml_w, 'y' as u8 ) ;
365
374
encode_type_params ( ebml_w, tps) ;
366
375
encode_type ( ebml_w, trans. node_ann_type ( cx, ann) ) ;
367
376
ebml. end_tag ( ebml_w) ;
368
377
}
369
378
case ( ast. item_tag ( ?id, ?variants, ?tps, ?did) ) {
370
- ebml. start_tag ( ebml_w, tag_items_item ) ;
379
+ ebml. start_tag ( ebml_w, tag_items_data_item ) ;
371
380
encode_def_id ( ebml_w, did) ;
372
381
encode_kind ( ebml_w, 't' as u8 ) ;
373
382
encode_type_params ( ebml_w, tps) ;
@@ -376,7 +385,7 @@ fn encode_info_for_item(@trans.crate_ctxt cx, &ebml.writer ebml_w,
376
385
encode_tag_variant_info ( cx, ebml_w, did, variants) ;
377
386
}
378
387
case ( ast. item_obj ( ?id, _, ?tps, ?odid, ?ann) ) {
379
- ebml. start_tag ( ebml_w, tag_items_item ) ;
388
+ ebml. start_tag ( ebml_w, tag_items_data_item ) ;
380
389
encode_def_id ( ebml_w, odid. ctor ) ;
381
390
encode_kind ( ebml_w, 'o' as u8 ) ;
382
391
encode_type_params ( ebml_w, tps) ;
@@ -385,7 +394,7 @@ fn encode_info_for_item(@trans.crate_ctxt cx, &ebml.writer ebml_w,
385
394
encode_symbol ( cx, ebml_w, odid. ctor ) ;
386
395
ebml. end_tag ( ebml_w) ;
387
396
388
- ebml. start_tag ( ebml_w, tag_items_item ) ;
397
+ ebml. start_tag ( ebml_w, tag_items_data_item ) ;
389
398
encode_def_id ( ebml_w, odid. ty ) ;
390
399
encode_kind ( ebml_w, 'y' as u8 ) ;
391
400
encode_type_params ( ebml_w, tps) ;
@@ -397,7 +406,7 @@ fn encode_info_for_item(@trans.crate_ctxt cx, &ebml.writer ebml_w,
397
406
398
407
fn encode_info_for_native_item ( @trans. crate_ctxt cx , & ebml. writer ebml_w ,
399
408
@ast. native_item nitem ) {
400
- ebml. start_tag ( ebml_w, tag_items_item ) ;
409
+ ebml. start_tag ( ebml_w, tag_items_data_item ) ;
401
410
alt ( nitem. node ) {
402
411
case ( ast. native_item_ty ( _, ?did) ) {
403
412
encode_def_id ( ebml_w, did) ;
@@ -413,26 +422,87 @@ fn encode_info_for_native_item(@trans.crate_ctxt cx, &ebml.writer ebml_w,
413
422
ebml. end_tag ( ebml_w) ;
414
423
}
415
424
416
- fn encode_info_for_items ( @trans. crate_ctxt cx , & ebml. writer ebml_w ) {
425
+ fn encode_info_for_items ( @trans. crate_ctxt cx , & ebml. writer ebml_w )
426
+ -> vec [ tup ( ast. def_id , uint) ] {
427
+ let vec[ tup ( ast. def_id , uint) ] index = vec ( ) ;
428
+
417
429
ebml. start_tag ( ebml_w, tag_items) ;
418
430
for each ( @tup( ast. def_id, @ast. item) kvp in cx. items . items ( ) ) {
431
+ index += vec ( tup ( kvp. _0 , ebml_w. writer . tell ( ) ) ) ;
419
432
encode_info_for_item ( cx, ebml_w, kvp. _1 ) ;
420
433
}
421
434
for each ( @tup( ast. def_id, @ast. native_item) kvp in
422
435
cx. native_items . items ( ) ) {
436
+ index += vec ( tup ( kvp. _0 , ebml_w. writer . tell ( ) ) ) ;
423
437
encode_info_for_native_item ( cx, ebml_w, kvp. _1 ) ;
424
438
}
425
439
ebml. end_tag ( ebml_w) ;
440
+
441
+ ret index;
442
+ }
443
+
444
+
445
+ // Definition ID indexing
446
+
447
+ fn create_index ( vec[ tup( ast. def_id, uint) ] index )
448
+ -> vec [ vec[ tup ( ast. def_id , uint) ] ] {
449
+ let vec[ vec[ tup ( ast. def_id , uint) ] ] buckets = vec ( ) ;
450
+ for each ( uint i in _uint. range( 0 u, 256 u) ) {
451
+ let vec[ tup ( ast. def_id , uint) ] bucket = vec ( ) ;
452
+ buckets += vec ( bucket) ;
453
+ }
454
+
455
+ for ( tup( ast. def_id, uint) elt in index) {
456
+ auto h = common. hash_def( elt. _0) ;
457
+ buckets. ( h % 256 u) += vec( elt) ;
458
+ }
459
+
460
+ ret buckets;
461
+ }
462
+
463
+ impure fn encode_index( & ebml. writer ebml_w,
464
+ vec[ tup( ast. def_id, uint) ] index) {
465
+ auto writer = io. new_writer_( ebml_w. writer) ;
466
+
467
+ auto buckets = create_index( index) ;
468
+
469
+ ebml. start_tag( ebml_w, tag_items_index) ;
470
+
471
+ let vec[ uint] bucket_locs = vec( ) ;
472
+ ebml. start_tag( ebml_w, tag_items_index_buckets) ;
473
+ for ( vec[ tup( ast. def_id, uint) ] bucket in buckets) {
474
+ bucket_locs += vec( ebml_w. writer. tell( ) ) ;
475
+
476
+ ebml. start_tag( ebml_w, tag_items_index_buckets_bucket) ;
477
+ for ( tup( ast. def_id, uint) elt in bucket) {
478
+ ebml. start_tag( ebml_w, tag_items_index_buckets_bucket_elt) ;
479
+ writer. write_be_uint( elt. _1, 4 u) ;
480
+ writer. write_str( def_to_str( elt. _0) ) ;
481
+ ebml. end_tag( ebml_w) ;
482
+ }
483
+ ebml. end_tag( ebml_w) ;
484
+ }
485
+ ebml. end_tag( ebml_w) ;
486
+
487
+ ebml. start_tag( ebml_w, tag_items_index_table) ;
488
+ for ( uint pos in bucket_locs) {
489
+ writer. write_be_uint( pos, 4 u) ;
490
+ }
491
+ ebml. end_tag( ebml_w) ;
492
+
493
+ ebml. end_tag( ebml_w) ;
426
494
}
427
495
428
496
429
- fn encode_metadata ( @trans. crate_ctxt cx , @ast. crate crate) -> ValueRef {
497
+ impure fn encode_metadata( @trans. crate_ctxt cx, @ast. crate crate)
498
+ -> ValueRef {
430
499
auto string_w = io. string_writer( ) ;
431
500
auto buf_w = string_w. get_writer( ) . get_buf_writer( ) ;
432
501
auto ebml_w = ebml. create_writer( buf_w) ;
433
502
434
503
encode_item_paths( ebml_w, crate ) ;
435
- encode_info_for_items ( cx, ebml_w) ;
504
+ auto index = encode_info_for_items( cx, ebml_w) ;
505
+ encode_index( ebml_w, index) ;
436
506
437
507
ret C_postr ( string_w. get_str( ) ) ;
438
508
}
0 commit comments