rust/src/comp/metadata/tyencode.rs

277 lines
8.1 KiB
Rust
Raw Normal View History

// Type encoding
2011-09-12 18:13:28 -05:00
import std::{str, io, int, uint};
import std::map::hashmap;
2011-09-12 18:13:28 -05:00
import std::option::{some, none};
import syntax::ast::*;
import middle::ty;
import syntax::print::pprust::*;
import util::common;
2011-06-27 16:45:38 -05:00
export ctxt;
export ty_abbrev;
export ac_no_abbrevs;
export ac_use_abbrevs;
export enc_ty;
type ctxt =
2011-09-02 17:34:58 -05:00
// Def -> str Callback:
// The type context.
{ds: fn(def_id) -> str, tcx: ty::ctxt, abbrevs: abbrev_ctxt};
// Compact string representation for ty.t values. API ty_str & parse_from_str.
// Extra parameters are for converting to/from def_ids in the string rep.
// Whatever format you choose should not contain pipe characters.
2011-09-02 17:34:58 -05:00
type ty_abbrev = {pos: uint, len: uint, s: @str};
tag abbrev_ctxt { ac_no_abbrevs; ac_use_abbrevs(hashmap<ty::t, ty_abbrev>); }
fn cx_uses_abbrevs(cx: @ctxt) -> bool {
2011-07-27 07:19:39 -05:00
alt cx.abbrevs {
ac_no_abbrevs. { ret false; }
ac_use_abbrevs(_) { ret true; }
}
}
fn enc_ty(w: io::writer, cx: @ctxt, t: ty::t) {
2011-07-27 07:19:39 -05:00
alt cx.abbrevs {
ac_no_abbrevs. {
2011-09-02 17:34:58 -05:00
let result_str: @str;
2011-07-27 07:19:39 -05:00
alt cx.tcx.short_names_cache.find(t) {
some(s) { result_str = s; }
none. {
2011-08-11 21:14:38 -05:00
let sw = io::string_writer();
2011-07-27 07:19:39 -05:00
enc_sty(sw.get_writer(), cx, ty::struct(cx.tcx, t));
result_str = @sw.get_str();
2011-07-27 07:19:39 -05:00
cx.tcx.short_names_cache.insert(t, result_str);
}
}
w.write_str(*result_str);
2011-07-27 07:19:39 -05:00
}
ac_use_abbrevs(abbrevs) {
alt abbrevs.find(t) {
some(a) { w.write_str(*a.s); ret; }
2011-07-27 07:19:39 -05:00
none. {
let pos = w.get_buf_writer().tell();
enc_sty(w, cx, ty::struct(cx.tcx, t));
let end = w.get_buf_writer().tell();
let len = end - pos;
fn estimate_sz(u: uint) -> uint {
let n = u;
let len = 0u;
while n != 0u { len += 1u; n = n >> 4u; }
ret len;
}
let abbrev_len = 3u + estimate_sz(pos) + estimate_sz(len);
if abbrev_len < len {
// I.e. it's actually an abbreviation.
2011-07-27 07:19:39 -05:00
let s =
2011-09-02 17:34:58 -05:00
"#" + uint::to_str(pos, 16u) + ":" +
uint::to_str(len, 16u) + "#";
let a = {pos: pos, len: len, s: @s};
2011-07-27 07:19:39 -05:00
abbrevs.insert(t, a);
}
2011-07-27 07:19:39 -05:00
ret;
}
}
2011-07-27 07:19:39 -05:00
}
}
}
fn enc_mt(w: io::writer, cx: @ctxt, mt: ty::mt) {
2011-07-27 07:19:39 -05:00
alt mt.mut {
imm. { }
mut. { w.write_char('m'); }
maybe_mut. { w.write_char('?'); }
}
enc_ty(w, cx, mt.ty);
}
fn enc_sty(w: io::writer, cx: @ctxt, st: ty::sty) {
2011-07-27 07:19:39 -05:00
alt st {
ty::ty_nil. { w.write_char('n'); }
ty::ty_bot. { w.write_char('z'); }
ty::ty_bool. { w.write_char('b'); }
ty::ty_int. { w.write_char('i'); }
ty::ty_uint. { w.write_char('u'); }
ty::ty_float. { w.write_char('l'); }
ty::ty_machine(mach) {
alt mach {
2011-09-02 17:34:58 -05:00
ty_u8. { w.write_str("Mb"); }
ty_u16. { w.write_str("Mw"); }
ty_u32. { w.write_str("Ml"); }
ty_u64. { w.write_str("Md"); }
ty_i8. { w.write_str("MB"); }
ty_i16. { w.write_str("MW"); }
ty_i32. { w.write_str("ML"); }
ty_i64. { w.write_str("MD"); }
ty_f32. { w.write_str("Mf"); }
ty_f64. { w.write_str("MF"); }
}
2011-07-27 07:19:39 -05:00
}
ty::ty_char. { w.write_char('c'); }
2011-09-02 18:45:00 -05:00
ty::ty_str. { w.write_char('S'); }
2011-07-27 07:19:39 -05:00
ty::ty_tag(def, tys) {
2011-09-02 17:34:58 -05:00
w.write_str("t[");
w.write_str(cx.ds(def));
2011-07-27 07:19:39 -05:00
w.write_char('|');
for t: ty::t in tys { enc_ty(w, cx, t); }
2011-07-27 07:19:39 -05:00
w.write_char(']');
}
2011-08-15 05:08:05 -05:00
ty::ty_tup(ts) {
2011-09-02 17:34:58 -05:00
w.write_str("T[");
2011-08-15 05:08:05 -05:00
for t in ts { enc_ty(w, cx, t); }
w.write_char(']');
}
2011-07-27 07:19:39 -05:00
ty::ty_box(mt) { w.write_char('@'); enc_mt(w, cx, mt); }
ty::ty_uniq(mt) { w.write_char('~'); enc_mt(w, cx, mt); }
2011-07-27 07:19:39 -05:00
ty::ty_ptr(mt) { w.write_char('*'); enc_mt(w, cx, mt); }
ty::ty_vec(mt) { w.write_char('I'); enc_mt(w, cx, mt); }
2011-07-27 07:19:39 -05:00
ty::ty_rec(fields) {
2011-09-02 17:34:58 -05:00
w.write_str("R[");
for field: ty::field in fields {
2011-08-25 19:00:12 -05:00
w.write_str(field.ident);
2011-07-27 07:19:39 -05:00
w.write_char('=');
enc_mt(w, cx, field.mt);
}
2011-07-27 07:19:39 -05:00
w.write_char(']');
}
ty::ty_fn(proto, args, out, cf, constrs) {
enc_proto(w, proto);
enc_ty_fn(w, cx, args, out, cf, constrs);
}
ty::ty_native_fn(abi, args, out) {
w.write_char('N');
alt abi {
native_abi_rust_intrinsic. { w.write_char('i'); }
native_abi_c_stack_cdecl. { w.write_char('C'); }
native_abi_stdcall. { w.write_char('S'); }
}
enc_ty_fn(w, cx, args, out, return_val, []);
2011-07-27 07:19:39 -05:00
}
ty::ty_obj(methods) {
2011-09-02 17:34:58 -05:00
w.write_str("O[");
for m: ty::method in methods {
2011-07-27 07:19:39 -05:00
enc_proto(w, m.proto);
2011-08-25 19:00:12 -05:00
w.write_str(m.ident);
2011-07-27 07:19:39 -05:00
enc_ty_fn(w, cx, m.inputs, m.output, m.cf, m.constrs);
}
2011-07-27 07:19:39 -05:00
w.write_char(']');
}
ty::ty_res(def, ty, tps) {
2011-09-02 17:34:58 -05:00
w.write_str("r[");
w.write_str(cx.ds(def));
2011-07-27 07:19:39 -05:00
w.write_char('|');
enc_ty(w, cx, ty);
for t: ty::t in tps { enc_ty(w, cx, t); }
2011-07-27 07:19:39 -05:00
w.write_char(']');
}
2011-09-02 17:34:58 -05:00
ty::ty_var(id) { w.write_char('X'); w.write_str(int::str(id)); }
2011-07-27 07:19:39 -05:00
ty::ty_native(def) {
w.write_char('E');
w.write_str(cx.ds(def));
2011-07-27 07:19:39 -05:00
w.write_char('|');
}
ty::ty_param(id, k) {
alt k {
2011-09-02 17:34:58 -05:00
kind_unique. { w.write_str("pu"); }
kind_shared. { w.write_str("ps"); }
kind_pinned. { w.write_str("pp"); }
}
2011-08-24 23:26:19 -05:00
w.write_str(uint::str(id));
}
2011-07-27 07:19:39 -05:00
ty::ty_type. { w.write_char('Y'); }
ty::ty_constr(ty, cs) {
2011-09-02 17:34:58 -05:00
w.write_str("A[");
2011-07-27 07:19:39 -05:00
enc_ty(w, cx, ty);
for tc: @ty::type_constr in cs { enc_ty_constr(w, cx, tc); }
2011-07-27 07:19:39 -05:00
w.write_char(']');
}
}
}
fn enc_proto(w: io::writer, proto: proto) {
2011-07-27 07:19:39 -05:00
alt proto {
proto_shared(_) { w.write_char('F'); }
2011-07-27 07:19:39 -05:00
proto_block. { w.write_char('B'); }
proto_bare. { w.write_char('f'); }
}
}
fn enc_ty_fn(w: io::writer, cx: @ctxt, args: [ty::arg], out: ty::t,
cf: ret_style, constrs: [@ty::constr]) {
w.write_char('[');
for arg: ty::arg in args {
2011-07-27 07:19:39 -05:00
alt arg.mode {
by_mut_ref. { w.write_char('&'); }
by_move. { w.write_char('-'); }
by_ref. { w.write_char('='); }
by_val. { w.write_char('+'); }
}
enc_ty(w, cx, arg.ty);
}
w.write_char(']');
2011-07-27 07:19:39 -05:00
let colon = true;
for c: @ty::constr in constrs {
2011-07-27 07:19:39 -05:00
if colon {
w.write_char(':');
colon = false;
} else { w.write_char(';'); }
enc_constr(w, cx, c);
}
alt cf {
noreturn. { w.write_char('!'); }
return_ref(mut, arg) {
w.write_char(mut ? '^' : '&');
w.write_bytes([arg as u8]);
enc_ty(w, cx, out);
}
_ { enc_ty(w, cx, out); }
}
}
// FIXME less copy-and-paste
fn enc_constr(w: io::writer, cx: @ctxt, c: @ty::constr) {
w.write_str(path_to_str(c.node.path));
w.write_char('(');
w.write_str(cx.ds(c.node.id));
w.write_char('|');
2011-07-27 07:19:39 -05:00
let semi = false;
for a: @constr_arg in c.node.args {
2011-07-27 07:19:39 -05:00
if semi { w.write_char(';'); } else { semi = true; }
alt a.node {
carg_base. { w.write_char('*'); }
carg_ident(i) { w.write_uint(i); }
2011-09-02 17:34:58 -05:00
carg_lit(l) { w.write_str(lit_to_str(l)); }
}
}
w.write_char(')');
}
fn enc_ty_constr(w: io::writer, cx: @ctxt, c: @ty::type_constr) {
w.write_str(path_to_str(c.node.path));
w.write_char('(');
w.write_str(cx.ds(c.node.id));
w.write_char('|');
2011-07-27 07:19:39 -05:00
let semi = false;
for a: @ty::ty_constr_arg in c.node.args {
2011-07-27 07:19:39 -05:00
if semi { w.write_char(';'); } else { semi = true; }
alt a.node {
carg_base. { w.write_char('*'); }
2011-09-02 17:34:58 -05:00
carg_ident(p) { w.write_str(path_to_str(p)); }
carg_lit(l) { w.write_str(lit_to_str(l)); }
}
}
w.write_char(')');
}
//
// 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:
//