2011-05-17 20:41:41 +02:00
|
|
|
import std::str;
|
|
|
|
import std::uint;
|
|
|
|
import std::vec;
|
2011-05-12 17:24:54 +02:00
|
|
|
import std::map::hashmap;
|
|
|
|
import std::ebml;
|
|
|
|
import std::io;
|
|
|
|
import std::option;
|
|
|
|
import std::option::some;
|
|
|
|
import std::option::none;
|
|
|
|
|
|
|
|
import front::ast;
|
|
|
|
import middle::trans;
|
|
|
|
import middle::ty;
|
|
|
|
import back::x86;
|
|
|
|
import util::common;
|
|
|
|
|
|
|
|
import lib::llvm::llvm;
|
|
|
|
import lib::llvm::llvm::ValueRef;
|
|
|
|
import lib::llvm::False;
|
2011-03-11 15:35:20 -08:00
|
|
|
|
2011-03-23 15:43:18 -07:00
|
|
|
const uint tag_paths = 0x01u;
|
|
|
|
const uint tag_items = 0x02u;
|
|
|
|
|
2011-03-31 17:38:03 -07:00
|
|
|
const uint tag_paths_data = 0x03u;
|
|
|
|
const uint tag_paths_data_name = 0x04u;
|
|
|
|
const uint tag_paths_data_item = 0x05u;
|
|
|
|
const uint tag_paths_data_mod = 0x06u;
|
|
|
|
|
|
|
|
const uint tag_def_id = 0x07u;
|
|
|
|
|
|
|
|
const uint tag_items_data = 0x08u;
|
|
|
|
const uint tag_items_data_item = 0x09u;
|
|
|
|
const uint tag_items_data_item_kind = 0x0au;
|
2011-04-12 15:09:50 -07:00
|
|
|
const uint tag_items_data_item_ty_param_count = 0x0bu;
|
2011-03-31 17:38:03 -07:00
|
|
|
const uint tag_items_data_item_type = 0x0cu;
|
|
|
|
const uint tag_items_data_item_symbol = 0x0du;
|
|
|
|
const uint tag_items_data_item_variant = 0x0eu;
|
|
|
|
const uint tag_items_data_item_tag_id = 0x0fu;
|
|
|
|
|
|
|
|
const uint tag_index = 0x11u;
|
|
|
|
const uint tag_index_buckets = 0x12u;
|
|
|
|
const uint tag_index_buckets_bucket = 0x13u;
|
|
|
|
const uint tag_index_buckets_bucket_elt = 0x14u;
|
|
|
|
const uint tag_index_table = 0x15u;
|
2011-03-23 15:43:18 -07:00
|
|
|
|
2011-03-21 11:35:04 -07:00
|
|
|
// Type encoding
|
|
|
|
|
|
|
|
// Compact string representation for ty.t values. API ty_str & parse_from_str.
|
2011-04-25 16:59:49 -07:00
|
|
|
// Extra parameters are for converting to/from def_ids in the string rep.
|
|
|
|
// Whatever format you choose should not contain pipe characters.
|
2011-03-21 11:35:04 -07:00
|
|
|
|
2011-04-29 15:26:28 +00:00
|
|
|
type ty_abbrev = rec(uint pos, uint len, str s);
|
|
|
|
|
2011-05-03 14:36:04 -07:00
|
|
|
tag abbrev_ctxt {
|
|
|
|
ac_no_abbrevs;
|
2011-05-12 17:24:54 +02:00
|
|
|
ac_use_abbrevs(hashmap[ty::t, ty_abbrev]);
|
2011-05-03 14:36:04 -07:00
|
|
|
}
|
|
|
|
|
2011-04-22 14:48:25 -07:00
|
|
|
mod Encode {
|
2011-03-21 11:35:04 -07:00
|
|
|
|
2011-04-22 14:48:25 -07:00
|
|
|
type ctxt = rec(
|
2011-05-12 17:24:54 +02:00
|
|
|
fn(&ast::def_id) -> str ds, // Def -> str Callback:
|
|
|
|
ty::ctxt tcx, // The type context.
|
2011-05-03 14:36:04 -07:00
|
|
|
abbrev_ctxt abbrevs
|
2011-04-22 14:48:25 -07:00
|
|
|
);
|
2011-03-21 11:35:04 -07:00
|
|
|
|
2011-05-09 16:09:00 -07:00
|
|
|
fn cx_uses_abbrevs(&@ctxt cx) -> bool {
|
2011-05-03 14:36:04 -07:00
|
|
|
alt (cx.abbrevs) {
|
|
|
|
case (ac_no_abbrevs) { ret false; }
|
|
|
|
case (ac_use_abbrevs(_)) { ret true; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn ty_str(&@ctxt cx, &ty::t t) -> str {
|
2011-05-03 14:36:04 -07:00
|
|
|
assert (!cx_uses_abbrevs(cx));
|
2011-05-12 17:24:54 +02:00
|
|
|
auto sw = io::string_writer();
|
2011-04-29 15:26:28 +00:00
|
|
|
enc_ty(sw.get_writer(), cx, t);
|
|
|
|
ret sw.get_str();
|
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn enc_ty(&io::writer w, &@ctxt cx, &ty::t t) {
|
2011-05-03 14:36:04 -07:00
|
|
|
alt (cx.abbrevs) {
|
2011-05-10 14:47:28 -07:00
|
|
|
case (ac_no_abbrevs) {
|
|
|
|
auto result_str;
|
|
|
|
alt (cx.tcx.short_names_cache.find(t)) {
|
2011-05-31 00:39:19 -04:00
|
|
|
case (some(?s)) { result_str = s; }
|
|
|
|
case (none) {
|
2011-05-12 17:24:54 +02:00
|
|
|
auto sw = io::string_writer();
|
|
|
|
enc_sty(sw.get_writer(), cx, ty::struct(cx.tcx, t));
|
2011-05-10 14:47:28 -07:00
|
|
|
result_str = sw.get_str();
|
|
|
|
cx.tcx.short_names_cache.insert(t, result_str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.write_str(result_str);
|
|
|
|
}
|
2011-05-03 14:36:04 -07:00
|
|
|
case (ac_use_abbrevs(?abbrevs)) {
|
|
|
|
alt (abbrevs.find(t)) {
|
2011-05-31 00:39:19 -04:00
|
|
|
case (some(?a)) {
|
2011-05-03 14:36:04 -07:00
|
|
|
w.write_str(a.s);
|
|
|
|
ret;
|
2011-04-29 15:26:28 +00:00
|
|
|
}
|
2011-05-31 00:39:19 -04:00
|
|
|
case (none) {
|
2011-05-03 14:36:04 -07:00
|
|
|
auto pos = w.get_buf_writer().tell();
|
2011-05-12 17:24:54 +02:00
|
|
|
auto ss = enc_sty(w, cx, ty::struct(cx.tcx, t));
|
2011-05-03 14:36:04 -07:00
|
|
|
auto end = w.get_buf_writer().tell();
|
|
|
|
auto len = end-pos;
|
|
|
|
fn estimate_sz(uint u) -> uint {
|
|
|
|
auto n = u;
|
|
|
|
auto len = 0u;
|
|
|
|
while (n != 0u) {
|
|
|
|
len += 1u;
|
|
|
|
n = n >> 4u;
|
|
|
|
}
|
|
|
|
ret len;
|
|
|
|
}
|
|
|
|
auto abbrev_len =
|
|
|
|
3u + estimate_sz(pos) + estimate_sz(len);
|
|
|
|
|
|
|
|
if (abbrev_len < len) {
|
|
|
|
// I.e. it's actually an abbreviation.
|
|
|
|
auto s = ("#"
|
2011-05-17 20:41:41 +02:00
|
|
|
+ uint::to_str(pos, 16u) + ":"
|
|
|
|
+ uint::to_str(len, 16u) + "#");
|
2011-05-03 14:36:04 -07:00
|
|
|
auto a = rec(pos=pos, len=len, s=s);
|
|
|
|
abbrevs.insert(t, a);
|
|
|
|
}
|
|
|
|
ret;
|
2011-04-29 15:26:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-03-21 11:44:08 -07:00
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn enc_mt(&io::writer w, &@ctxt cx, &ty::mt mt) {
|
2011-04-22 14:48:25 -07:00
|
|
|
alt (mt.mut) {
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::imm) { }
|
|
|
|
case (ast::mut) { w.write_char('m'); }
|
|
|
|
case (ast::maybe_mut) { w.write_char('?'); }
|
2011-03-21 11:35:04 -07:00
|
|
|
}
|
2011-04-29 15:26:28 +00:00
|
|
|
enc_ty(w, cx, mt.ty);
|
2011-04-22 14:48:25 -07:00
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn enc_sty(&io::writer w, &@ctxt cx, &ty::sty st) {
|
2011-04-22 14:48:25 -07:00
|
|
|
alt (st) {
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ty::ty_nil) { w.write_char('n'); }
|
2011-05-20 14:15:39 -07:00
|
|
|
case (ty::ty_bot) { w.write_char('z'); }
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ty::ty_bool) { w.write_char('b'); }
|
|
|
|
case (ty::ty_int) { w.write_char('i'); }
|
|
|
|
case (ty::ty_uint) { w.write_char('u'); }
|
|
|
|
case (ty::ty_float) { w.write_char('l'); }
|
|
|
|
case (ty::ty_machine(?mach)) {
|
2011-04-22 14:48:25 -07:00
|
|
|
alt (mach) {
|
2011-05-12 17:24:54 +02:00
|
|
|
case (common::ty_u8) { w.write_str("Mb"); }
|
|
|
|
case (common::ty_u16) { w.write_str("Mw"); }
|
|
|
|
case (common::ty_u32) { w.write_str("Ml"); }
|
|
|
|
case (common::ty_u64) { w.write_str("Md"); }
|
|
|
|
case (common::ty_i8) { w.write_str("MB"); }
|
|
|
|
case (common::ty_i16) { w.write_str("MW"); }
|
|
|
|
case (common::ty_i32) { w.write_str("ML"); }
|
|
|
|
case (common::ty_i64) { w.write_str("MD"); }
|
|
|
|
case (common::ty_f32) { w.write_str("Mf"); }
|
|
|
|
case (common::ty_f64) { w.write_str("MF"); }
|
2011-04-22 14:48:25 -07:00
|
|
|
}
|
2011-03-21 11:35:04 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ty::ty_char) {w.write_char('c');}
|
|
|
|
case (ty::ty_str) {w.write_char('s');}
|
2011-05-13 15:17:24 +02:00
|
|
|
case (ty::ty_tag(?def,?tys)) {
|
2011-04-29 15:26:28 +00:00
|
|
|
w.write_str("t[");
|
|
|
|
w.write_str(cx.ds(def));
|
|
|
|
w.write_char('|');
|
2011-05-12 17:24:54 +02:00
|
|
|
for (ty::t t in tys) {
|
2011-04-29 15:26:28 +00:00
|
|
|
enc_ty(w, cx, t);
|
|
|
|
}
|
|
|
|
w.write_char(']');
|
2011-03-21 11:35:04 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ty::ty_box(?mt)) {w.write_char('@'); enc_mt(w, cx, mt); }
|
2011-06-03 15:02:58 -04:00
|
|
|
case (ty::ty_ptr(?mt)) {w.write_char('*'); enc_mt(w, cx, mt); }
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ty::ty_vec(?mt)) {w.write_char('V'); enc_mt(w, cx, mt); }
|
|
|
|
case (ty::ty_port(?t)) {w.write_char('P'); enc_ty(w, cx, t); }
|
|
|
|
case (ty::ty_chan(?t)) {w.write_char('C'); enc_ty(w, cx, t); }
|
|
|
|
case (ty::ty_tup(?mts)) {
|
2011-04-29 15:26:28 +00:00
|
|
|
w.write_str("T[");
|
2011-05-12 17:24:54 +02:00
|
|
|
for (ty::mt mt in mts) {
|
2011-04-29 15:26:28 +00:00
|
|
|
enc_mt(w, cx, mt);
|
|
|
|
}
|
|
|
|
w.write_char(']');
|
2011-04-22 14:48:25 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ty::ty_rec(?fields)) {
|
2011-04-29 15:26:28 +00:00
|
|
|
w.write_str("R[");
|
2011-05-12 17:24:54 +02:00
|
|
|
for (ty::field field in fields) {
|
2011-04-29 15:26:28 +00:00
|
|
|
w.write_str(field.ident);
|
|
|
|
w.write_char('=');
|
|
|
|
enc_mt(w, cx, field.mt);
|
2011-04-22 14:48:25 -07:00
|
|
|
}
|
2011-04-29 15:26:28 +00:00
|
|
|
w.write_char(']');
|
2011-03-21 11:35:04 -07:00
|
|
|
}
|
2011-05-20 14:15:39 -07:00
|
|
|
case (ty::ty_fn(?proto,?args,?out,?cf)) {
|
2011-04-29 15:26:28 +00:00
|
|
|
enc_proto(w, proto);
|
2011-05-20 14:15:39 -07:00
|
|
|
enc_ty_fn(w, cx, args, out, cf);
|
2011-04-22 14:48:25 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ty::ty_native_fn(?abi,?args,?out)) {
|
2011-04-29 15:26:28 +00:00
|
|
|
w.write_char('N');
|
2011-04-22 14:48:25 -07:00
|
|
|
alt (abi) {
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::native_abi_rust) { w.write_char('r'); }
|
|
|
|
case (ast::native_abi_rust_intrinsic) {
|
2011-05-05 14:34:22 -07:00
|
|
|
w.write_char('i');
|
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::native_abi_cdecl) { w.write_char('c'); }
|
|
|
|
case (ast::native_abi_llvm) { w.write_char('l'); }
|
2011-04-22 14:48:25 -07:00
|
|
|
}
|
2011-05-20 14:15:39 -07:00
|
|
|
enc_ty_fn(w, cx, args, out, ast::return);
|
2011-04-22 14:48:25 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ty::ty_obj(?methods)) {
|
2011-04-29 15:26:28 +00:00
|
|
|
w.write_str("O[");
|
2011-05-12 17:24:54 +02:00
|
|
|
for (ty::method m in methods) {
|
2011-04-29 15:26:28 +00:00
|
|
|
enc_proto(w, m.proto);
|
|
|
|
w.write_str(m.ident);
|
2011-05-20 14:15:39 -07:00
|
|
|
enc_ty_fn(w, cx, m.inputs, m.output, m.cf);
|
2011-04-22 14:48:25 -07:00
|
|
|
}
|
2011-04-29 15:26:28 +00:00
|
|
|
w.write_char(']');
|
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ty::ty_var(?id)) {
|
2011-04-29 15:26:28 +00:00
|
|
|
w.write_char('X');
|
2011-05-12 17:24:54 +02:00
|
|
|
w.write_str(common::istr(id));
|
2011-04-22 14:48:25 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ty::ty_native) {w.write_char('E');}
|
|
|
|
case (ty::ty_param(?id)) {
|
2011-04-29 15:26:28 +00:00
|
|
|
w.write_char('p');
|
2011-05-12 17:24:54 +02:00
|
|
|
w.write_str(common::uistr(id));
|
2011-04-29 15:26:28 +00:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ty::ty_type) {w.write_char('Y');}
|
2011-05-14 18:56:44 -07:00
|
|
|
case (ty::ty_task) {w.write_char('a');}
|
2011-03-21 11:35:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn enc_proto(&io::writer w, ast::proto proto) {
|
2011-04-22 14:48:25 -07:00
|
|
|
alt (proto) {
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::proto_iter) { w.write_char('W'); }
|
|
|
|
case (ast::proto_fn) { w.write_char('F'); }
|
2011-04-22 14:48:25 -07:00
|
|
|
}
|
2011-03-21 11:35:04 -07:00
|
|
|
}
|
|
|
|
|
2011-05-20 14:15:39 -07:00
|
|
|
fn enc_ty_fn(&io::writer w, &@ctxt cx, &vec[ty::arg] args, &ty::t out,
|
|
|
|
&ast::controlflow cf) {
|
2011-04-29 15:26:28 +00:00
|
|
|
w.write_char('[');
|
2011-05-12 17:24:54 +02:00
|
|
|
for (ty::arg arg in args) {
|
|
|
|
if (arg.mode == ty::mo_alias) { w.write_char('&'); }
|
2011-04-29 15:26:28 +00:00
|
|
|
enc_ty(w, cx, arg.ty);
|
2011-04-22 14:48:25 -07:00
|
|
|
}
|
2011-04-29 15:26:28 +00:00
|
|
|
w.write_char(']');
|
2011-05-20 14:15:39 -07:00
|
|
|
alt (cf) {
|
|
|
|
case (ast::noreturn) {
|
|
|
|
w.write_char('!');
|
|
|
|
}
|
|
|
|
case (_) {
|
|
|
|
enc_ty(w, cx, out);
|
|
|
|
}
|
|
|
|
}
|
2011-03-21 11:35:04 -07:00
|
|
|
}
|
2011-04-22 14:48:25 -07:00
|
|
|
|
2011-03-21 11:35:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
// Returns a Plain Old LLVM String:
|
2011-05-09 16:09:00 -07:00
|
|
|
fn C_postr(&str s) -> ValueRef {
|
2011-05-17 20:41:41 +02:00
|
|
|
ret llvm::LLVMConstString(str::buf(s), str::byte_len(s), False);
|
2011-03-11 15:35:20 -08:00
|
|
|
}
|
|
|
|
|
2011-03-23 15:43:18 -07:00
|
|
|
|
|
|
|
// Path table encoding
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn encode_name(&ebml::writer ebml_w, &str name) {
|
|
|
|
ebml::start_tag(ebml_w, tag_paths_data_name);
|
2011-05-17 20:41:41 +02:00
|
|
|
ebml_w.writer.write(str::bytes(name));
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn encode_def_id(&ebml::writer ebml_w, &ast::def_id id) {
|
|
|
|
ebml::start_tag(ebml_w, tag_def_id);
|
2011-05-17 20:41:41 +02:00
|
|
|
ebml_w.writer.write(str::bytes(def_to_str(id)));
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn encode_tag_variant_paths(&ebml::writer ebml_w,
|
|
|
|
&vec[ast::variant] variants,
|
2011-05-09 16:09:00 -07:00
|
|
|
&vec[str] path,
|
2011-04-01 13:02:44 -07:00
|
|
|
&mutable vec[tup(str, uint)] index) {
|
2011-05-12 17:24:54 +02:00
|
|
|
for (ast::variant variant in variants) {
|
2011-04-01 13:02:44 -07:00
|
|
|
add_to_index(ebml_w, path, index, variant.node.name);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::start_tag(ebml_w, tag_paths_data_item);
|
2011-03-29 12:46:55 +02:00
|
|
|
encode_name(ebml_w, variant.node.name);
|
|
|
|
encode_def_id(ebml_w, variant.node.id);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn add_to_index(&ebml::writer ebml_w,
|
2011-05-09 16:09:00 -07:00
|
|
|
&vec[str] path,
|
2011-03-31 17:38:03 -07:00
|
|
|
&mutable vec[tup(str, uint)] index,
|
2011-05-09 16:09:00 -07:00
|
|
|
&str name) {
|
2011-05-16 18:21:22 -07:00
|
|
|
auto full_path = path + [name];
|
2011-05-17 20:41:41 +02:00
|
|
|
index += [tup(str::connect(full_path, "::"), ebml_w.writer.tell())];
|
2011-03-31 17:38:03 -07:00
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn encode_native_module_item_paths(&ebml::writer ebml_w,
|
|
|
|
&ast::native_mod nmod,
|
2011-05-09 16:09:00 -07:00
|
|
|
&vec[str] path,
|
2011-03-31 17:38:03 -07:00
|
|
|
&mutable vec[tup(str, uint)] index) {
|
2011-05-12 17:24:54 +02:00
|
|
|
for (@ast::native_item nitem in nmod.items) {
|
2011-03-23 15:43:18 -07:00
|
|
|
alt (nitem.node) {
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::native_item_ty(?id, ?did)) {
|
2011-03-31 17:38:03 -07:00
|
|
|
add_to_index(ebml_w, path, index, id);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::start_tag(ebml_w, tag_paths_data_item);
|
2011-03-23 15:43:18 -07:00
|
|
|
encode_name(ebml_w, id);
|
|
|
|
encode_def_id(ebml_w, did);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::native_item_fn(?id, _, _, _, ?did, _)) {
|
2011-03-31 17:38:03 -07:00
|
|
|
add_to_index(ebml_w, path, index, id);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::start_tag(ebml_w, tag_paths_data_item);
|
2011-03-23 15:43:18 -07:00
|
|
|
encode_name(ebml_w, id);
|
|
|
|
encode_def_id(ebml_w, did);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn encode_module_item_paths(&ebml::writer ebml_w,
|
|
|
|
&ast::_mod module,
|
2011-05-09 16:09:00 -07:00
|
|
|
&vec[str] path,
|
2011-03-31 17:38:03 -07:00
|
|
|
&mutable vec[tup(str, uint)] index) {
|
2011-05-12 17:24:54 +02:00
|
|
|
for (@ast::item it in module.items) {
|
2011-05-20 21:27:54 -04:00
|
|
|
if (!ast::is_exported(ast::item_ident(it), module)) {
|
|
|
|
cont;
|
|
|
|
}
|
|
|
|
|
2011-03-23 15:43:18 -07:00
|
|
|
alt (it.node) {
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::item_const(?id, _, ?tps, ?did, ?ann)) {
|
2011-03-31 17:38:03 -07:00
|
|
|
add_to_index(ebml_w, path, index, id);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::start_tag(ebml_w, tag_paths_data_item);
|
2011-03-23 15:43:18 -07:00
|
|
|
encode_name(ebml_w, id);
|
|
|
|
encode_def_id(ebml_w, did);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::item_fn(?id, _, ?tps, ?did, ?ann)) {
|
2011-03-31 17:38:03 -07:00
|
|
|
add_to_index(ebml_w, path, index, id);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::start_tag(ebml_w, tag_paths_data_item);
|
2011-03-23 15:43:18 -07:00
|
|
|
encode_name(ebml_w, id);
|
|
|
|
encode_def_id(ebml_w, did);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::item_mod(?id, ?_mod, ?did)) {
|
2011-03-31 17:38:03 -07:00
|
|
|
add_to_index(ebml_w, path, index, id);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::start_tag(ebml_w, tag_paths_data_mod);
|
2011-03-23 15:43:18 -07:00
|
|
|
encode_name(ebml_w, id);
|
2011-03-29 14:46:42 -07:00
|
|
|
encode_def_id(ebml_w, did);
|
2011-05-16 18:21:22 -07:00
|
|
|
encode_module_item_paths(ebml_w, _mod, path + [id], index);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::item_native_mod(?id, ?nmod, ?did)) {
|
2011-03-31 17:38:03 -07:00
|
|
|
add_to_index(ebml_w, path, index, id);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::start_tag(ebml_w, tag_paths_data_mod);
|
2011-03-23 15:43:18 -07:00
|
|
|
encode_name(ebml_w, id);
|
2011-03-29 14:46:42 -07:00
|
|
|
encode_def_id(ebml_w, did);
|
2011-05-16 18:21:22 -07:00
|
|
|
encode_native_module_item_paths(ebml_w, nmod, path + [id],
|
2011-03-31 17:38:03 -07:00
|
|
|
index);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::item_ty(?id, _, ?tps, ?did, ?ann)) {
|
2011-03-31 17:38:03 -07:00
|
|
|
add_to_index(ebml_w, path, index, id);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::start_tag(ebml_w, tag_paths_data_item);
|
2011-03-23 15:43:18 -07:00
|
|
|
encode_name(ebml_w, id);
|
|
|
|
encode_def_id(ebml_w, did);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::item_tag(?id, ?variants, ?tps, ?did, _)) {
|
2011-03-31 17:38:03 -07:00
|
|
|
add_to_index(ebml_w, path, index, id);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::start_tag(ebml_w, tag_paths_data_item);
|
2011-03-23 15:43:18 -07:00
|
|
|
encode_name(ebml_w, id);
|
|
|
|
encode_def_id(ebml_w, did);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-04-01 13:02:44 -07:00
|
|
|
|
|
|
|
encode_tag_variant_paths(ebml_w, variants, path, index);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::item_obj(?id, _, ?tps, ?odid, ?ann)) {
|
2011-03-31 17:38:03 -07:00
|
|
|
add_to_index(ebml_w, path, index, id);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::start_tag(ebml_w, tag_paths_data_item);
|
2011-03-23 15:43:18 -07:00
|
|
|
encode_name(ebml_w, id);
|
2011-03-30 17:23:25 -07:00
|
|
|
encode_def_id(ebml_w, odid.ctor);
|
2011-05-13 15:17:24 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
|
|
|
|
|
|
|
add_to_index(ebml_w, path, index, id);
|
|
|
|
ebml::start_tag(ebml_w, tag_paths_data_item);
|
|
|
|
encode_name(ebml_w, id);
|
|
|
|
encode_def_id(ebml_w, odid.ty);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn encode_item_paths(&ebml::writer ebml_w, &@ast::crate crate)
|
2011-03-31 17:38:03 -07:00
|
|
|
-> vec[tup(str, uint)] {
|
2011-05-16 18:21:22 -07:00
|
|
|
let vec[tup(str, uint)] index = [];
|
|
|
|
let vec[str] path = [];
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::start_tag(ebml_w, tag_paths);
|
2011-03-31 17:38:03 -07:00
|
|
|
encode_module_item_paths(ebml_w, crate.node.module, path, index);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-31 17:38:03 -07:00
|
|
|
ret index;
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Item info table encoding
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn encode_kind(&ebml::writer ebml_w, u8 c) {
|
|
|
|
ebml::start_tag(ebml_w, tag_items_data_item_kind);
|
2011-05-16 18:21:22 -07:00
|
|
|
ebml_w.writer.write([c]);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn def_to_str(&ast::def_id did) -> str {
|
2011-03-23 15:43:18 -07:00
|
|
|
ret #fmt("%d:%d", did._0, did._1);
|
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn encode_type_param_count(&ebml::writer ebml_w, &vec[ast::ty_param] tps) {
|
|
|
|
ebml::start_tag(ebml_w, tag_items_data_item_ty_param_count);
|
2011-05-17 20:41:41 +02:00
|
|
|
ebml::write_vint(ebml_w.writer, vec::len[ast::ty_param](tps));
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn encode_variant_id(&ebml::writer ebml_w, &ast::def_id vid) {
|
|
|
|
ebml::start_tag(ebml_w, tag_items_data_item_variant);
|
2011-05-17 20:41:41 +02:00
|
|
|
ebml_w.writer.write(str::bytes(def_to_str(vid)));
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-04-01 13:02:44 -07:00
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn encode_type(&@trans::crate_ctxt cx, &ebml::writer ebml_w, &ty::t typ) {
|
|
|
|
ebml::start_tag(ebml_w, tag_items_data_item_type);
|
2011-04-22 14:48:25 -07:00
|
|
|
|
2011-03-23 15:43:18 -07:00
|
|
|
auto f = def_to_str;
|
2011-04-29 15:26:28 +00:00
|
|
|
auto ty_str_ctxt = @rec(ds=f, tcx=cx.tcx,
|
2011-05-03 14:36:04 -07:00
|
|
|
abbrevs=ac_use_abbrevs(cx.type_abbrevs));
|
2011-05-12 17:24:54 +02:00
|
|
|
Encode::enc_ty(io::new_writer_(ebml_w.writer), ty_str_ctxt, typ);
|
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn encode_symbol(&@trans::crate_ctxt cx, &ebml::writer ebml_w,
|
|
|
|
&ast::def_id did) {
|
|
|
|
ebml::start_tag(ebml_w, tag_items_data_item_symbol);
|
2011-05-17 20:41:41 +02:00
|
|
|
ebml_w.writer.write(str::bytes(cx.item_symbols.get(did)));
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn encode_discriminant(&@trans::crate_ctxt cx, &ebml::writer ebml_w,
|
|
|
|
&ast::def_id did) {
|
|
|
|
ebml::start_tag(ebml_w, tag_items_data_item_symbol);
|
2011-05-17 20:41:41 +02:00
|
|
|
ebml_w.writer.write(str::bytes(cx.discrim_symbols.get(did)));
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn encode_tag_id(&ebml::writer ebml_w, &ast::def_id id) {
|
|
|
|
ebml::start_tag(ebml_w, tag_items_data_item_tag_id);
|
2011-05-17 20:41:41 +02:00
|
|
|
ebml_w.writer.write(str::bytes(def_to_str(id)));
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
|
|
|
|
2011-03-30 17:23:25 -07:00
|
|
|
|
2011-03-23 15:43:18 -07:00
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn encode_tag_variant_info(&@trans::crate_ctxt cx, &ebml::writer ebml_w,
|
|
|
|
&ast::def_id did, &vec[ast::variant] variants,
|
2011-04-01 18:53:08 -07:00
|
|
|
&mutable vec[tup(int, uint)] index,
|
2011-05-12 17:24:54 +02:00
|
|
|
&vec[ast::ty_param] ty_params) {
|
|
|
|
for (ast::variant variant in variants) {
|
2011-05-16 18:21:22 -07:00
|
|
|
index += [tup(variant.node.id._1, ebml_w.writer.tell())];
|
2011-04-01 13:02:44 -07:00
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::start_tag(ebml_w, tag_items_data_item);
|
2011-03-29 12:46:55 +02:00
|
|
|
encode_def_id(ebml_w, variant.node.id);
|
2011-03-28 13:10:10 -07:00
|
|
|
encode_kind(ebml_w, 'v' as u8);
|
2011-03-23 15:43:18 -07:00
|
|
|
encode_tag_id(ebml_w, did);
|
2011-05-12 17:24:54 +02:00
|
|
|
encode_type(cx, ebml_w, trans::node_ann_type(cx, variant.node.ann));
|
2011-05-17 20:41:41 +02:00
|
|
|
if (vec::len[ast::variant_arg](variant.node.args) > 0u) {
|
2011-04-01 14:14:10 -07:00
|
|
|
encode_symbol(cx, ebml_w, variant.node.id);
|
|
|
|
}
|
2011-03-29 12:46:55 +02:00
|
|
|
encode_discriminant(cx, ebml_w, variant.node.id);
|
2011-04-12 15:09:50 -07:00
|
|
|
encode_type_param_count(ebml_w, ty_params);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn encode_info_for_item(@trans::crate_ctxt cx, &ebml::writer ebml_w,
|
|
|
|
@ast::item item, &mutable vec[tup(int, uint)] index) {
|
2011-03-23 15:43:18 -07:00
|
|
|
alt (item.node) {
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::item_const(_, _, _, ?did, ?ann)) {
|
|
|
|
ebml::start_tag(ebml_w, tag_items_data_item);
|
2011-03-23 15:43:18 -07:00
|
|
|
encode_def_id(ebml_w, did);
|
|
|
|
encode_kind(ebml_w, 'c' as u8);
|
2011-05-12 17:24:54 +02:00
|
|
|
encode_type(cx, ebml_w, trans::node_ann_type(cx, ann));
|
2011-03-23 15:43:18 -07:00
|
|
|
encode_symbol(cx, ebml_w, did);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::item_fn(_, _, ?tps, ?did, ?ann)) {
|
|
|
|
ebml::start_tag(ebml_w, tag_items_data_item);
|
2011-03-23 15:43:18 -07:00
|
|
|
encode_def_id(ebml_w, did);
|
|
|
|
encode_kind(ebml_w, 'f' as u8);
|
2011-04-12 15:09:50 -07:00
|
|
|
encode_type_param_count(ebml_w, tps);
|
2011-05-12 17:24:54 +02:00
|
|
|
encode_type(cx, ebml_w, trans::node_ann_type(cx, ann));
|
2011-03-23 15:43:18 -07:00
|
|
|
encode_symbol(cx, ebml_w, did);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::item_mod(_, _, ?did)) {
|
|
|
|
ebml::start_tag(ebml_w, tag_items_data_item);
|
2011-03-29 14:46:42 -07:00
|
|
|
encode_def_id(ebml_w, did);
|
|
|
|
encode_kind(ebml_w, 'm' as u8);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::item_native_mod(?id, _, ?did)) {
|
|
|
|
ebml::start_tag(ebml_w, tag_items_data_item);
|
2011-03-29 14:46:42 -07:00
|
|
|
encode_def_id(ebml_w, did);
|
|
|
|
encode_kind(ebml_w, 'n' as u8);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::item_ty(?id, _, ?tps, ?did, ?ann)) {
|
|
|
|
ebml::start_tag(ebml_w, tag_items_data_item);
|
2011-03-23 15:43:18 -07:00
|
|
|
encode_def_id(ebml_w, did);
|
|
|
|
encode_kind(ebml_w, 'y' as u8);
|
2011-04-12 15:09:50 -07:00
|
|
|
encode_type_param_count(ebml_w, tps);
|
2011-05-12 17:24:54 +02:00
|
|
|
encode_type(cx, ebml_w, trans::node_ann_type(cx, ann));
|
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::item_tag(?id, ?variants, ?tps, ?did, ?ann)) {
|
|
|
|
ebml::start_tag(ebml_w, tag_items_data_item);
|
2011-03-23 15:43:18 -07:00
|
|
|
encode_def_id(ebml_w, did);
|
|
|
|
encode_kind(ebml_w, 't' as u8);
|
2011-04-12 15:09:50 -07:00
|
|
|
encode_type_param_count(ebml_w, tps);
|
2011-05-12 17:24:54 +02:00
|
|
|
encode_type(cx, ebml_w, trans::node_ann_type(cx, ann));
|
|
|
|
for (ast::variant v in variants) {
|
2011-04-01 13:02:44 -07:00
|
|
|
encode_variant_id(ebml_w, v.node.id);
|
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
|
2011-04-01 18:53:08 -07:00
|
|
|
encode_tag_variant_info(cx, ebml_w, did, variants, index, tps);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::item_obj(?id, _, ?tps, ?odid, ?ann)) {
|
|
|
|
ebml::start_tag(ebml_w, tag_items_data_item);
|
2011-03-30 17:23:25 -07:00
|
|
|
encode_def_id(ebml_w, odid.ctor);
|
2011-03-23 15:43:18 -07:00
|
|
|
encode_kind(ebml_w, 'o' as u8);
|
2011-04-12 15:09:50 -07:00
|
|
|
encode_type_param_count(ebml_w, tps);
|
2011-05-12 17:24:54 +02:00
|
|
|
auto fn_ty = trans::node_ann_type(cx, ann);
|
2011-04-22 17:00:46 -07:00
|
|
|
encode_type(cx, ebml_w, fn_ty);
|
2011-03-30 17:23:25 -07:00
|
|
|
encode_symbol(cx, ebml_w, odid.ctor);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-30 17:23:25 -07:00
|
|
|
|
2011-05-16 18:21:22 -07:00
|
|
|
index += [tup(odid.ty._1, ebml_w.writer.tell())];
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::start_tag(ebml_w, tag_items_data_item);
|
2011-03-30 17:23:25 -07:00
|
|
|
encode_def_id(ebml_w, odid.ty);
|
|
|
|
encode_kind(ebml_w, 'y' as u8);
|
2011-04-12 15:09:50 -07:00
|
|
|
encode_type_param_count(ebml_w, tps);
|
2011-05-12 17:24:54 +02:00
|
|
|
encode_type(cx, ebml_w, ty::ty_fn_ret(cx.tcx, fn_ty));
|
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn encode_info_for_native_item(&@trans::crate_ctxt cx, &ebml::writer ebml_w,
|
|
|
|
&@ast::native_item nitem) {
|
|
|
|
ebml::start_tag(ebml_w, tag_items_data_item);
|
2011-03-23 15:43:18 -07:00
|
|
|
alt (nitem.node) {
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::native_item_ty(_, ?did)) {
|
2011-03-23 15:43:18 -07:00
|
|
|
encode_def_id(ebml_w, did);
|
|
|
|
encode_kind(ebml_w, 'T' as u8);
|
2011-05-12 17:24:54 +02:00
|
|
|
encode_type(cx, ebml_w, ty::mk_native(cx.tcx));
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
case (ast::native_item_fn(_, _, _, ?tps, ?did, ?ann)) {
|
2011-03-23 15:43:18 -07:00
|
|
|
encode_def_id(ebml_w, did);
|
|
|
|
encode_kind(ebml_w, 'F' as u8);
|
2011-04-12 15:09:50 -07:00
|
|
|
encode_type_param_count(ebml_w, tps);
|
2011-05-12 17:24:54 +02:00
|
|
|
encode_type(cx, ebml_w, trans::node_ann_type(cx, ann));
|
2011-04-01 14:18:46 -07:00
|
|
|
encode_symbol(cx, ebml_w, did);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn encode_info_for_items(&@trans::crate_ctxt cx, &ebml::writer ebml_w)
|
2011-03-31 12:24:59 -07:00
|
|
|
-> vec[tup(int, uint)] {
|
2011-05-16 18:21:22 -07:00
|
|
|
let vec[tup(int, uint)] index = [];
|
2011-03-31 11:56:02 -07:00
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::start_tag(ebml_w, tag_items_data);
|
|
|
|
for each (@tup(ast::def_id, @ast::item) kvp in cx.items.items()) {
|
2011-05-16 18:21:22 -07:00
|
|
|
index += [tup(kvp._0._1, ebml_w.writer.tell())];
|
2011-04-01 13:02:44 -07:00
|
|
|
encode_info_for_item(cx, ebml_w, kvp._1, index);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
for each (@tup(ast::def_id, @ast::native_item) kvp in
|
2011-03-23 15:43:18 -07:00
|
|
|
cx.native_items.items()) {
|
2011-05-16 18:21:22 -07:00
|
|
|
index += [tup(kvp._0._1, ebml_w.writer.tell())];
|
2011-03-23 15:43:18 -07:00
|
|
|
encode_info_for_native_item(cx, ebml_w, kvp._1);
|
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-31 11:56:02 -07:00
|
|
|
|
|
|
|
ret index;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-31 17:38:03 -07:00
|
|
|
// Path and definition ID indexing
|
|
|
|
|
|
|
|
// djb's cdb hashes.
|
2011-03-31 11:56:02 -07:00
|
|
|
|
2011-03-31 17:38:03 -07:00
|
|
|
fn hash_def_num(&int def_num) -> uint {
|
2011-03-31 12:24:59 -07:00
|
|
|
ret 177573u ^ (def_num as uint);
|
|
|
|
}
|
|
|
|
|
2011-03-31 17:38:03 -07:00
|
|
|
fn hash_path(&str s) -> uint {
|
|
|
|
auto h = 5381u;
|
2011-05-17 20:41:41 +02:00
|
|
|
for (u8 ch in str::bytes(s)) {
|
2011-03-31 17:38:03 -07:00
|
|
|
h = ((h << 5u) + h) ^ (ch as uint);
|
|
|
|
}
|
|
|
|
ret h;
|
|
|
|
}
|
|
|
|
|
2011-05-09 16:09:00 -07:00
|
|
|
fn create_index[T](&vec[tup(T, uint)] index, fn(&T) -> uint hash_fn)
|
2011-03-31 17:38:03 -07:00
|
|
|
-> vec[vec[tup(T, uint)]] {
|
2011-05-16 18:21:22 -07:00
|
|
|
let vec[vec[tup(T, uint)]] buckets = [];
|
2011-05-17 20:41:41 +02:00
|
|
|
for each (uint i in uint::range(0u, 256u)) {
|
2011-05-16 18:21:22 -07:00
|
|
|
let vec[tup(T, uint)] bucket = [];
|
|
|
|
buckets += [bucket];
|
2011-03-31 11:56:02 -07:00
|
|
|
}
|
|
|
|
|
2011-03-31 17:38:03 -07:00
|
|
|
for (tup(T, uint) elt in index) {
|
|
|
|
auto h = hash_fn(elt._0);
|
2011-05-16 18:21:22 -07:00
|
|
|
buckets.(h % 256u) += [elt];
|
2011-03-31 11:56:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ret buckets;
|
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn encode_index[T](&ebml::writer ebml_w, &vec[vec[tup(T, uint)]] buckets,
|
|
|
|
fn(&io::writer, &T) write_fn) {
|
|
|
|
auto writer = io::new_writer_(ebml_w.writer);
|
2011-03-31 11:56:02 -07:00
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::start_tag(ebml_w, tag_index);
|
2011-03-31 11:56:02 -07:00
|
|
|
|
2011-05-16 18:21:22 -07:00
|
|
|
let vec[uint] bucket_locs = [];
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::start_tag(ebml_w, tag_index_buckets);
|
2011-03-31 17:38:03 -07:00
|
|
|
for (vec[tup(T, uint)] bucket in buckets) {
|
2011-05-16 18:21:22 -07:00
|
|
|
bucket_locs += [ebml_w.writer.tell()];
|
2011-03-31 11:56:02 -07:00
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::start_tag(ebml_w, tag_index_buckets_bucket);
|
2011-03-31 17:38:03 -07:00
|
|
|
for (tup(T, uint) elt in bucket) {
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::start_tag(ebml_w, tag_index_buckets_bucket_elt);
|
2011-03-31 11:56:02 -07:00
|
|
|
writer.write_be_uint(elt._1, 4u);
|
2011-03-31 17:38:03 -07:00
|
|
|
write_fn(writer, elt._0);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-31 11:56:02 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-31 11:56:02 -07:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-31 11:56:02 -07:00
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::start_tag(ebml_w, tag_index_table);
|
2011-03-31 11:56:02 -07:00
|
|
|
for (uint pos in bucket_locs) {
|
|
|
|
writer.write_be_uint(pos, 4u);
|
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-31 11:56:02 -07:00
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn write_str(&io::writer writer, &str s) {
|
2011-03-31 17:38:03 -07:00
|
|
|
writer.write_str(s);
|
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn write_int(&io::writer writer, &int n) {
|
2011-03-31 17:38:03 -07:00
|
|
|
writer.write_be_uint(n as uint, 4u);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn encode_metadata(&@trans::crate_ctxt cx, &@ast::crate crate)
|
2011-03-31 11:56:02 -07:00
|
|
|
-> ValueRef {
|
2011-05-12 17:24:54 +02:00
|
|
|
auto string_w = io::string_writer();
|
2011-03-23 15:43:18 -07:00
|
|
|
auto buf_w = string_w.get_writer().get_buf_writer();
|
2011-05-12 17:24:54 +02:00
|
|
|
auto ebml_w = ebml::create_writer(buf_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
|
2011-03-31 17:38:03 -07:00
|
|
|
// Encode and index the paths.
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::start_tag(ebml_w, tag_paths);
|
2011-03-31 17:38:03 -07:00
|
|
|
auto paths_index = encode_item_paths(ebml_w, crate);
|
|
|
|
auto str_writer = write_str;
|
|
|
|
auto path_hasher = hash_path;
|
|
|
|
auto paths_buckets = create_index[str](paths_index, path_hasher);
|
|
|
|
encode_index[str](ebml_w, paths_buckets, str_writer);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-31 12:24:59 -07:00
|
|
|
|
2011-03-31 17:38:03 -07:00
|
|
|
// Encode and index the items.
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::start_tag(ebml_w, tag_items);
|
2011-03-31 17:38:03 -07:00
|
|
|
auto items_index = encode_info_for_items(cx, ebml_w);
|
|
|
|
auto int_writer = write_int;
|
|
|
|
auto item_hasher = hash_def_num;
|
|
|
|
auto items_buckets = create_index[int](items_index, item_hasher);
|
|
|
|
encode_index[int](ebml_w, items_buckets, int_writer);
|
2011-05-12 17:24:54 +02:00
|
|
|
ebml::end_tag(ebml_w);
|
2011-03-23 15:43:18 -07:00
|
|
|
|
2011-04-07 20:17:11 +02:00
|
|
|
// Pad this, since something (LLVM, presumably) is cutting off the
|
|
|
|
// remaining % 4 bytes.
|
2011-05-16 18:21:22 -07:00
|
|
|
buf_w.write([0u8, 0u8, 0u8, 0u8]);
|
2011-04-07 20:17:11 +02:00
|
|
|
|
2011-03-23 15:43:18 -07:00
|
|
|
ret C_postr(string_w.get_str());
|
2011-03-11 15:35:20 -08:00
|
|
|
}
|
|
|
|
|
2011-05-12 17:24:54 +02:00
|
|
|
fn write_metadata(&@trans::crate_ctxt cx, &@ast::crate crate) {
|
2011-06-01 15:59:28 -04:00
|
|
|
if (!cx.sess.get_opts().shared) {
|
|
|
|
ret;
|
2011-04-29 19:20:10 +00:00
|
|
|
}
|
2011-03-11 15:35:20 -08:00
|
|
|
|
2011-06-01 15:59:28 -04:00
|
|
|
auto llmeta = encode_metadata(cx, crate);
|
2011-05-16 18:21:22 -07:00
|
|
|
auto llconst = trans::C_struct([llmeta]);
|
2011-05-12 17:24:54 +02:00
|
|
|
auto llglobal = llvm::LLVMAddGlobal(cx.llmod, trans::val_ty(llconst),
|
2011-05-17 20:41:41 +02:00
|
|
|
str::buf("rust_metadata"));
|
2011-05-12 17:24:54 +02:00
|
|
|
llvm::LLVMSetInitializer(llglobal, llconst);
|
2011-05-17 20:41:41 +02:00
|
|
|
llvm::LLVMSetSection(llglobal, str::buf(x86::get_meta_sect_name()));
|
2011-03-11 15:35:20 -08:00
|
|
|
}
|
|
|
|
|
2011-03-26 17:35:51 -07:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|
|
|
|
//
|