about summary refs log tree commit diff
path: root/src/comp/metadata/encoder.rs
blob: 92ab6db903f1677c1b3292218ea23e1f37bf1a5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
// Metadata encoding

import core::{vec, str, uint};
import std::{io, ebml, map};
import syntax::ast::*;
import syntax::ast_util;
import syntax::ast_util::local_def;
import common::*;
import middle::trans_common::crate_ctxt;
import middle::ty;
import middle::ty::node_id_to_monotype;
import front::attr;

export encode_metadata;
export encoded_ty;

type abbrev_map = map::hashmap<ty::t, tyencode::ty_abbrev>;

type encode_ctxt = {ccx: @crate_ctxt, type_abbrevs: abbrev_map};

// Path table encoding
fn encode_name(ebml_w: ebml::writer, name: str) {
    ebml::start_tag(ebml_w, tag_paths_data_name);
    ebml_w.writer.write(str::bytes(name));
    ebml::end_tag(ebml_w);
}

fn encode_def_id(ebml_w: ebml::writer, id: def_id) {
    ebml::start_tag(ebml_w, tag_def_id);
    ebml_w.writer.write(str::bytes(def_to_str(id)));
    ebml::end_tag(ebml_w);
}

type entry<T> = {val: T, pos: uint};

fn encode_tag_variant_paths(ebml_w: ebml::writer, variants: [variant],
                            path: [str], &index: [entry<str>]) {
    for variant: variant in variants {
        add_to_index(ebml_w, path, index, variant.node.name);
        ebml::start_tag(ebml_w, tag_paths_data_item);
        encode_name(ebml_w, variant.node.name);
        encode_def_id(ebml_w, local_def(variant.node.id));
        ebml::end_tag(ebml_w);
    }
}

fn add_to_index(ebml_w: ebml::writer, path: [str], &index: [entry<str>],
                name: str) {
    let full_path = path + [name];
    index +=
        [{val: str::connect(full_path, "::"), pos: ebml_w.writer.tell()}];
}

fn encode_native_module_item_paths(ebml_w: ebml::writer, nmod: native_mod,
                                   path: [str], &index: [entry<str>]) {
    for nitem: @native_item in nmod.items {
        add_to_index(ebml_w, path, index, nitem.ident);
        ebml::start_tag(ebml_w, tag_paths_data_item);
        encode_name(ebml_w, nitem.ident);
        encode_def_id(ebml_w, local_def(nitem.id));
        ebml::end_tag(ebml_w);
    }
}

fn encode_module_item_paths(ebml_w: ebml::writer, module: _mod, path: [str],
                            &index: [entry<str>]) {
    for it: @item in module.items {
        if !ast_util::is_exported(it.ident, module) { cont; }
        alt it.node {
          item_const(_, _) {
            add_to_index(ebml_w, path, index, it.ident);
            ebml::start_tag(ebml_w, tag_paths_data_item);
            encode_name(ebml_w, it.ident);
            encode_def_id(ebml_w, local_def(it.id));
            ebml::end_tag(ebml_w);
          }
          item_fn(_, tps) {
            add_to_index(ebml_w, path, index, it.ident);
            ebml::start_tag(ebml_w, tag_paths_data_item);
            encode_name(ebml_w, it.ident);
            encode_def_id(ebml_w, local_def(it.id));
            ebml::end_tag(ebml_w);
          }
          item_mod(_mod) {
            add_to_index(ebml_w, path, index, it.ident);
            ebml::start_tag(ebml_w, tag_paths_data_mod);
            encode_name(ebml_w, it.ident);
            encode_def_id(ebml_w, local_def(it.id));
            encode_module_item_paths(ebml_w, _mod, path + [it.ident], index);
            ebml::end_tag(ebml_w);
          }
          item_native_mod(nmod) {
            add_to_index(ebml_w, path, index, it.ident);
            ebml::start_tag(ebml_w, tag_paths_data_mod);
            encode_name(ebml_w, it.ident);
            encode_def_id(ebml_w, local_def(it.id));
            encode_native_module_item_paths(ebml_w, nmod, path + [it.ident],
                                            index);
            ebml::end_tag(ebml_w);
          }
          item_ty(_, tps) {
            add_to_index(ebml_w, path, index, it.ident);
            ebml::start_tag(ebml_w, tag_paths_data_item);
            encode_name(ebml_w, it.ident);
            encode_def_id(ebml_w, local_def(it.id));
            ebml::end_tag(ebml_w);
          }
          item_res(_, _, tps, ctor_id) {
            add_to_index(ebml_w, path, index, it.ident);
            ebml::start_tag(ebml_w, tag_paths_data_item);
            encode_name(ebml_w, it.ident);
            encode_def_id(ebml_w, local_def(ctor_id));
            ebml::end_tag(ebml_w);
            add_to_index(ebml_w, path, index, it.ident);
            ebml::start_tag(ebml_w, tag_paths_data_item);
            encode_name(ebml_w, it.ident);
            encode_def_id(ebml_w, local_def(it.id));
            ebml::end_tag(ebml_w);
          }
          item_tag(variants, tps) {
            add_to_index(ebml_w, path, index, it.ident);
            ebml::start_tag(ebml_w, tag_paths_data_item);
            encode_name(ebml_w, it.ident);
            encode_def_id(ebml_w, local_def(it.id));
            ebml::end_tag(ebml_w);
            encode_tag_variant_paths(ebml_w, variants, path, index);
          }
          item_obj(_, tps, ctor_id) {
            add_to_index(ebml_w, path, index, it.ident);
            ebml::start_tag(ebml_w, tag_paths_data_item);
            encode_name(ebml_w, it.ident);
            encode_def_id(ebml_w, local_def(ctor_id));
            ebml::end_tag(ebml_w);
            add_to_index(ebml_w, path, index, it.ident);
            ebml::start_tag(ebml_w, tag_paths_data_item);
            encode_name(ebml_w, it.ident);
            encode_def_id(ebml_w, local_def(it.id));
            ebml::end_tag(ebml_w);
          }
          item_impl(_, _, _) {}
        }
    }
}

fn encode_item_paths(ebml_w: ebml::writer, ecx: @encode_ctxt, crate: @crate)
    -> [entry<str>] {
    let index: [entry<str>] = [];
    let path: [str] = [];
    ebml::start_tag(ebml_w, tag_paths);
    encode_module_item_paths(ebml_w, crate.node.module, path, index);
    encode_reexport_paths(ebml_w, ecx, index);
    ebml::end_tag(ebml_w);
    ret index;
}

fn encode_reexport_paths(ebml_w: ebml::writer,
                         ecx: @encode_ctxt, &index: [entry<str>]) {
    ecx.ccx.exp_map.items {|path, def|
        index += [{val: path, pos: ebml_w.writer.tell()}];
        ebml::start_tag(ebml_w, tag_paths_data_item);
        encode_name(ebml_w, path);
        encode_def_id(ebml_w, ast_util::def_id_of_def(def));
        ebml::end_tag(ebml_w);
    }
}


// Item info table encoding
fn encode_family(ebml_w: ebml::writer, c: u8) {
    ebml::start_tag(ebml_w, tag_items_data_item_family);
    ebml_w.writer.write([c]);
    ebml::end_tag(ebml_w);
}

fn encode_inlineness(ebml_w: ebml::writer, c: u8) {
    ebml::start_tag(ebml_w, tag_items_data_item_inlineness);
    ebml_w.writer.write([c]);
    ebml::end_tag(ebml_w);
}

fn def_to_str(did: def_id) -> str { ret #fmt["%d:%d", did.crate, did.node]; }

fn encode_type_param_kinds(ebml_w: ebml::writer, tps: [ty_param]) {
    ebml::start_tag(ebml_w, tag_items_data_item_ty_param_kinds);
    ebml::write_vint(ebml_w.writer, vec::len::<ty_param>(tps));
    for tp: ty_param in tps {
        let c = alt ast_util::ty_param_kind(tp) {
          kind_sendable. { 's' }
          kind_copyable. { 'c' }
          kind_noncopyable. { 'a' }
        };
        ebml_w.writer.write([c as u8]);
    }
    ebml::end_tag(ebml_w);
}

fn encode_variant_id(ebml_w: ebml::writer, vid: def_id) {
    ebml::start_tag(ebml_w, tag_items_data_item_variant);
    ebml_w.writer.write(str::bytes(def_to_str(vid)));
    ebml::end_tag(ebml_w);
}

fn encode_type(ecx: @encode_ctxt, ebml_w: ebml::writer, typ: ty::t) {
    ebml::start_tag(ebml_w, tag_items_data_item_type);
    let f = def_to_str;
    let ty_str_ctxt =
        @{ds: f,
          tcx: ecx.ccx.tcx,
          abbrevs: tyencode::ac_use_abbrevs(ecx.type_abbrevs)};
    tyencode::enc_ty(io::new_writer(ebml_w.writer), ty_str_ctxt, typ);
    ebml::end_tag(ebml_w);
}

fn encode_symbol(ecx: @encode_ctxt, ebml_w: ebml::writer, id: node_id) {
    ebml::start_tag(ebml_w, tag_items_data_item_symbol);
    ebml_w.writer.write(str::bytes(ecx.ccx.item_symbols.get(id)));
    ebml::end_tag(ebml_w);
}

fn encode_discriminant(ecx: @encode_ctxt, ebml_w: ebml::writer, id: node_id) {
    ebml::start_tag(ebml_w, tag_items_data_item_symbol);
    ebml_w.writer.write(str::bytes(ecx.ccx.discrim_symbols.get(id)));
    ebml::end_tag(ebml_w);
}

fn encode_tag_id(ebml_w: ebml::writer, id: def_id) {
    ebml::start_tag(ebml_w, tag_items_data_item_tag_id);
    ebml_w.writer.write(str::bytes(def_to_str(id)));
    ebml::end_tag(ebml_w);
}

fn encode_tag_variant_info(ecx: @encode_ctxt, ebml_w: ebml::writer,
                           id: node_id, variants: [variant],
                           &index: [entry<int>], ty_params: [ty_param]) {
    for variant: variant in variants {
        index += [{val: variant.node.id, pos: ebml_w.writer.tell()}];
        ebml::start_tag(ebml_w, tag_items_data_item);
        encode_def_id(ebml_w, local_def(variant.node.id));
        encode_family(ebml_w, 'v' as u8);
        encode_tag_id(ebml_w, local_def(id));
        encode_type(ecx, ebml_w,
                    node_id_to_monotype(ecx.ccx.tcx, variant.node.id));
        if vec::len::<variant_arg>(variant.node.args) > 0u {
            encode_symbol(ecx, ebml_w, variant.node.id);
        }
        encode_discriminant(ecx, ebml_w, variant.node.id);
        encode_type_param_kinds(ebml_w, ty_params);
        ebml::end_tag(ebml_w);
    }
}

fn encode_info_for_mod(ebml_w: ebml::writer, md: _mod,
                       id: node_id) {
    ebml::start_tag(ebml_w, tag_items_data_item);
    encode_def_id(ebml_w, local_def(id));
    encode_family(ebml_w, 'm' as u8);
    for i in md.items {
        alt i.node {
          item_impl(_, _, _) {
            if ast_util::is_exported(i.ident, md) {
                ebml::start_tag(ebml_w, tag_mod_impl);
                ebml_w.writer.write(str::bytes(def_to_str(local_def(i.id))));
                ebml::end_tag(ebml_w);
            }
          }
          _ {}
        }
    }
    ebml::end_tag(ebml_w);
}

fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::writer, item: @item,
                        &index: [entry<int>]) {
    alt item.node {
      item_const(_, _) {
        ebml::start_tag(ebml_w, tag_items_data_item);
        encode_def_id(ebml_w, local_def(item.id));
        encode_family(ebml_w, 'c' as u8);
        encode_type(ecx, ebml_w, node_id_to_monotype(ecx.ccx.tcx, item.id));
        encode_symbol(ecx, ebml_w, item.id);
        ebml::end_tag(ebml_w);
      }
      item_fn(fd, tps) {
        ebml::start_tag(ebml_w, tag_items_data_item);
        encode_def_id(ebml_w, local_def(item.id));
        encode_family(ebml_w,
                      alt fd.decl.purity {
                        unsafe_fn. { 'u' }
                        pure_fn. { 'p' }
                        impure_fn. { 'f' }
                      } as u8);
        encode_inlineness(ebml_w,
                          alt fd.decl.il {
                            il_normal. { 'n' }
                            il_inline. { 'i' }
                          } as u8);
        encode_type_param_kinds(ebml_w, tps);
        encode_type(ecx, ebml_w, node_id_to_monotype(ecx.ccx.tcx, item.id));
        encode_symbol(ecx, ebml_w, item.id);
        ebml::end_tag(ebml_w);
      }
      item_mod(m) {
        encode_info_for_mod(ebml_w, m, item.id);
      }
      item_native_mod(_) {
        ebml::start_tag(ebml_w, tag_items_data_item);
        encode_def_id(ebml_w, local_def(item.id));
        encode_family(ebml_w, 'n' as u8);
        ebml::end_tag(ebml_w);
      }
      item_ty(_, tps) {
        ebml::start_tag(ebml_w, tag_items_data_item);
        encode_def_id(ebml_w, local_def(item.id));
        encode_family(ebml_w, 'y' as u8);
        encode_type_param_kinds(ebml_w, tps);
        encode_type(ecx, ebml_w, node_id_to_monotype(ecx.ccx.tcx, item.id));
        ebml::end_tag(ebml_w);
      }
      item_tag(variants, tps) {
        ebml::start_tag(ebml_w, tag_items_data_item);
        encode_def_id(ebml_w, local_def(item.id));
        encode_family(ebml_w, 't' as u8);
        encode_type_param_kinds(ebml_w, tps);
        encode_type(ecx, ebml_w, node_id_to_monotype(ecx.ccx.tcx, item.id));
        for v: variant in variants {
            encode_variant_id(ebml_w, local_def(v.node.id));
        }
        ebml::end_tag(ebml_w);
        encode_tag_variant_info(ecx, ebml_w, item.id, variants, index, tps);
      }
      item_res(_, _, tps, ctor_id) {
        let fn_ty = node_id_to_monotype(ecx.ccx.tcx, ctor_id);

        ebml::start_tag(ebml_w, tag_items_data_item);
        encode_def_id(ebml_w, local_def(ctor_id));
        encode_family(ebml_w, 'y' as u8);
        encode_type_param_kinds(ebml_w, tps);
        encode_type(ecx, ebml_w, ty::ty_fn_ret(ecx.ccx.tcx, fn_ty));
        encode_symbol(ecx, ebml_w, item.id);
        ebml::end_tag(ebml_w);

        index += [{val: ctor_id, pos: ebml_w.writer.tell()}];
        ebml::start_tag(ebml_w, tag_items_data_item);
        encode_def_id(ebml_w, local_def(ctor_id));
        encode_family(ebml_w, 'f' as u8);
        encode_type_param_kinds(ebml_w, tps);
        encode_type(ecx, ebml_w, fn_ty);
        encode_symbol(ecx, ebml_w, ctor_id);
        ebml::end_tag(ebml_w);
      }
      item_obj(_, tps, ctor_id) {
        let fn_ty = node_id_to_monotype(ecx.ccx.tcx, ctor_id);

        ebml::start_tag(ebml_w, tag_items_data_item);
        encode_def_id(ebml_w, local_def(item.id));
        encode_family(ebml_w, 'y' as u8);
        encode_type_param_kinds(ebml_w, tps);
        encode_type(ecx, ebml_w, ty::ty_fn_ret(ecx.ccx.tcx, fn_ty));
        ebml::end_tag(ebml_w);

        index += [{val: ctor_id, pos: ebml_w.writer.tell()}];
        ebml::start_tag(ebml_w, tag_items_data_item);
        encode_def_id(ebml_w, local_def(ctor_id));
        encode_family(ebml_w, 'f' as u8);
        encode_type_param_kinds(ebml_w, tps);
        encode_type(ecx, ebml_w, fn_ty);
        encode_symbol(ecx, ebml_w, ctor_id);
        ebml::end_tag(ebml_w);
      }
      item_impl(tps, _, methods) {
        ebml::start_tag(ebml_w, tag_items_data_item);
        encode_def_id(ebml_w, local_def(item.id));
        encode_family(ebml_w, 'i' as u8);
        encode_type_param_kinds(ebml_w, tps);
        encode_type(ecx, ebml_w, node_id_to_monotype(ecx.ccx.tcx, item.id));
        encode_name(ebml_w, item.ident);
        for m in methods {
            ebml::start_tag(ebml_w, tag_impl_method);
            ebml_w.writer.write(str::bytes(def_to_str(local_def(m.node.id))));
            ebml::end_tag(ebml_w);
        }
        ebml::end_tag(ebml_w);

        for m in methods {
            index += [{val: m.node.id, pos: ebml_w.writer.tell()}];
            ebml::start_tag(ebml_w, tag_items_data_item);
            encode_def_id(ebml_w, local_def(m.node.id));
            encode_family(ebml_w, 'f' as u8);
            encode_inlineness(ebml_w, 'n' as u8);
            encode_type_param_kinds(ebml_w, tps + m.node.tps);
            encode_type(ecx, ebml_w,
                        node_id_to_monotype(ecx.ccx.tcx, m.node.id));
            encode_name(ebml_w, m.node.ident);
            encode_symbol(ecx, ebml_w, m.node.id);
            ebml::end_tag(ebml_w);
        }
      }
    }
}

fn encode_info_for_native_item(ecx: @encode_ctxt, ebml_w: ebml::writer,
                               nitem: @native_item) {
    ebml::start_tag(ebml_w, tag_items_data_item);
    alt nitem.node {
      native_item_ty. {
        encode_def_id(ebml_w, local_def(nitem.id));
        encode_family(ebml_w, 'T' as u8);
        encode_type(ecx, ebml_w,
                    ty::mk_native(ecx.ccx.tcx, local_def(nitem.id)));
      }
      native_item_fn(fn_decl, tps) {
        let letter =
            alt fn_decl.purity {
              unsafe_fn. { 'U' }
              pure_fn. { 'P' }   // this is currently impossible, but hey.
              impure_fn. { 'F' }
            } as u8;
        encode_def_id(ebml_w, local_def(nitem.id));
        encode_family(ebml_w, letter);
        encode_type_param_kinds(ebml_w, tps);
        encode_type(ecx, ebml_w, node_id_to_monotype(ecx.ccx.tcx, nitem.id));
        encode_symbol(ecx, ebml_w, nitem.id);
      }
    }
    ebml::end_tag(ebml_w);
}

fn encode_info_for_items(ecx: @encode_ctxt, ebml_w: ebml::writer,
                         crate_mod: _mod) -> [entry<int>] {
    let index: [entry<int>] = [];
    ebml::start_tag(ebml_w, tag_items_data);
    index += [{val: crate_node_id, pos: ebml_w.writer.tell()}];
    encode_info_for_mod(ebml_w, crate_mod, crate_node_id);
    ecx.ccx.ast_map.items {|key, val|
        alt val {
          middle::ast_map::node_item(i) {
            index += [{val: key, pos: ebml_w.writer.tell()}];
            encode_info_for_item(ecx, ebml_w, i, index);
          }
          middle::ast_map::node_native_item(i) {
            index += [{val: key, pos: ebml_w.writer.tell()}];
            encode_info_for_native_item(ecx, ebml_w, i);
          }
          _ { }
        }
    };
    ebml::end_tag(ebml_w);
    ret index;
}


// Path and definition ID indexing

fn create_index<copy T>(index: [entry<T>], hash_fn: fn(T) -> uint) ->
   [@[entry<T>]] {
    let buckets: [@mutable [entry<T>]] = [];
    uint::range(0u, 256u) {|_i| buckets += [@mutable []]; };
    for elt: entry<T> in index {
        let h = hash_fn(elt.val);
        *buckets[h % 256u] += [elt];
    }

    let buckets_frozen = [];
    for bucket: @mutable [entry<T>] in buckets {
        buckets_frozen += [@*bucket];
    }
    ret buckets_frozen;
}

fn encode_index<T>(ebml_w: ebml::writer, buckets: [@[entry<T>]],
                   write_fn: fn(io::writer, T)) {
    let writer = io::new_writer(ebml_w.writer);
    ebml::start_tag(ebml_w, tag_index);
    let bucket_locs: [uint] = [];
    ebml::start_tag(ebml_w, tag_index_buckets);
    for bucket: @[entry<T>] in buckets {
        bucket_locs += [ebml_w.writer.tell()];
        ebml::start_tag(ebml_w, tag_index_buckets_bucket);
        for elt: entry<T> in *bucket {
            ebml::start_tag(ebml_w, tag_index_buckets_bucket_elt);
            writer.write_be_uint(elt.pos, 4u);
            write_fn(writer, elt.val);
            ebml::end_tag(ebml_w);
        }
        ebml::end_tag(ebml_w);
    }
    ebml::end_tag(ebml_w);
    ebml::start_tag(ebml_w, tag_index_table);
    for pos: uint in bucket_locs { writer.write_be_uint(pos, 4u); }
    ebml::end_tag(ebml_w);
    ebml::end_tag(ebml_w);
}

fn write_str(writer: io::writer, &&s: str) { writer.write_str(s); }

fn write_int(writer: io::writer, &&n: int) {
    writer.write_be_uint(n as uint, 4u);
}

fn encode_meta_item(ebml_w: ebml::writer, mi: meta_item) {
    alt mi.node {
      meta_word(name) {
        ebml::start_tag(ebml_w, tag_meta_item_word);
        ebml::start_tag(ebml_w, tag_meta_item_name);
        ebml_w.writer.write(str::bytes(name));
        ebml::end_tag(ebml_w);
        ebml::end_tag(ebml_w);
      }
      meta_name_value(name, value) {
        alt value.node {
          lit_str(value) {
            ebml::start_tag(ebml_w, tag_meta_item_name_value);
            ebml::start_tag(ebml_w, tag_meta_item_name);
            ebml_w.writer.write(str::bytes(name));
            ebml::end_tag(ebml_w);
            ebml::start_tag(ebml_w, tag_meta_item_value);
            ebml_w.writer.write(str::bytes(value));
            ebml::end_tag(ebml_w);
            ebml::end_tag(ebml_w);
          }
          _ {/* FIXME (#611) */ }
        }
      }
      meta_list(name, items) {
        ebml::start_tag(ebml_w, tag_meta_item_list);
        ebml::start_tag(ebml_w, tag_meta_item_name);
        ebml_w.writer.write(str::bytes(name));
        ebml::end_tag(ebml_w);
        for inner_item: @meta_item in items {
            encode_meta_item(ebml_w, *inner_item);
        }
        ebml::end_tag(ebml_w);
      }
    }
}

fn encode_attributes(ebml_w: ebml::writer, attrs: [attribute]) {
    ebml::start_tag(ebml_w, tag_attributes);
    for attr: attribute in attrs {
        ebml::start_tag(ebml_w, tag_attribute);
        encode_meta_item(ebml_w, attr.node.value);
        ebml::end_tag(ebml_w);
    }
    ebml::end_tag(ebml_w);
}

// So there's a special crate attribute called 'link' which defines the
// metadata that Rust cares about for linking crates. This attribute requires
// 'name' and 'vers' items, so if the user didn't provide them we will throw
// them in anyway with default values.
fn synthesize_crate_attrs(ecx: @encode_ctxt, crate: @crate) -> [attribute] {

    fn synthesize_link_attr(ecx: @encode_ctxt, items: [@meta_item]) ->
       attribute {

        assert (ecx.ccx.link_meta.name != "");
        assert (ecx.ccx.link_meta.vers != "");

        let name_item =
            attr::mk_name_value_item_str("name", ecx.ccx.link_meta.name);
        let vers_item =
            attr::mk_name_value_item_str("vers", ecx.ccx.link_meta.vers);

        let other_items =
            {
                let tmp = attr::remove_meta_items_by_name(items, "name");
                attr::remove_meta_items_by_name(tmp, "vers")
            };

        let meta_items = [name_item, vers_item] + other_items;
        let link_item = attr::mk_list_item("link", meta_items);

        ret attr::mk_attr(link_item);
    }

    let attrs: [attribute] = [];
    let found_link_attr = false;
    for attr: attribute in crate.node.attrs {
        attrs +=
            if attr::get_attr_name(attr) != "link" {
                [attr]
            } else {
                alt attr.node.value.node {
                  meta_list(n, l) {
                    found_link_attr = true;;
                    [synthesize_link_attr(ecx, l)]
                  }
                  _ { [attr] }
                }
            };
    }

    if !found_link_attr { attrs += [synthesize_link_attr(ecx, [])]; }

    ret attrs;
}

fn encode_crate_deps(ebml_w: ebml::writer, cstore: cstore::cstore) {

    fn get_ordered_names(cstore: cstore::cstore) -> [str] {
        type hashkv = @{key: crate_num, val: cstore::crate_metadata};
        type numname = {crate: crate_num, ident: str};

        // Pull the cnums and names out of cstore
        let pairs: [mutable numname] = [mutable];
        cstore::iter_crate_data(cstore) {|key, val|
            pairs += [mutable {crate: key, ident: val.name}];
        };

        // Sort by cnum
        fn lteq(kv1: numname, kv2: numname) -> bool { kv1.crate <= kv2.crate }
        std::sort::quick_sort(lteq, pairs);

        // Sanity-check the crate numbers
        let expected_cnum = 1;
        for n: numname in pairs {
            assert (n.crate == expected_cnum);
            expected_cnum += 1;
        }

        // Return just the names
        fn name(kv: numname) -> str { kv.ident }
        // mutable -> immutable hack for vec::map
        let immpairs = vec::slice(pairs, 0u, vec::len(pairs));
        ret vec::map(immpairs, name);
    }

    // We're just going to write a list of crate names, with the assumption
    // that they are numbered 1 to n.
    // FIXME: This is not nearly enough to support correct versioning
    // but is enough to get transitive crate dependencies working.
    ebml::start_tag(ebml_w, tag_crate_deps);
    for cname: str in get_ordered_names(cstore) {
        ebml::start_tag(ebml_w, tag_crate_dep);
        ebml_w.writer.write(str::bytes(cname));
        ebml::end_tag(ebml_w);
    }
    ebml::end_tag(ebml_w);
}

fn encode_hash(ebml_w: ebml::writer, hash: str) {
    ebml::start_tag(ebml_w, tag_crate_hash);
    ebml_w.writer.write(str::bytes(hash));
    ebml::end_tag(ebml_w);
}

fn encode_metadata(cx: @crate_ctxt, crate: @crate) -> str {

    let abbrevs = map::mk_hashmap(ty::hash_ty, ty::eq_ty);
    let ecx = @{ccx: cx, type_abbrevs: abbrevs};

    let string_w = io::string_writer();
    let buf_w = string_w.get_writer().get_buf_writer();
    let ebml_w = ebml::create_writer(buf_w);

    encode_hash(ebml_w, cx.link_meta.extras_hash);

    let crate_attrs = synthesize_crate_attrs(ecx, crate);
    encode_attributes(ebml_w, crate_attrs);

    encode_crate_deps(ebml_w, cx.sess.get_cstore());

    // Encode and index the paths.

    ebml::start_tag(ebml_w, tag_paths);
    let paths_index = encode_item_paths(ebml_w, ecx, crate);
    let paths_buckets = create_index(paths_index, hash_path);
    encode_index(ebml_w, paths_buckets, write_str);
    ebml::end_tag(ebml_w);
    // Encode and index the items.

    ebml::start_tag(ebml_w, tag_items);
    let items_index = encode_info_for_items(ecx, ebml_w, crate.node.module);
    let items_buckets = create_index(items_index, hash_node_id);
    encode_index(ebml_w, items_buckets, write_int);
    ebml::end_tag(ebml_w);
    // Pad this, since something (LLVM, presumably) is cutting off the
    // remaining % 4 bytes.

    buf_w.write([0u8, 0u8, 0u8, 0u8]);
    ret string_w.get_str();
}

// Get the encoded string for a type
fn encoded_ty(tcx: ty::ctxt, t: ty::t) -> str {
    let cx = @{ds: def_to_str, tcx: tcx, abbrevs: tyencode::ac_no_abbrevs};
    let sw = io::string_writer();
    tyencode::enc_ty(sw.get_writer(), cx, t);
    ret sw.get_str();
}


// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End: