rust/src/rustc/metadata/tyencode.rs

392 lines
10 KiB
Rust
Raw Normal View History

// Type encoding
2012-09-04 13:54:36 -05:00
use io::WriterUtil;
2012-09-10 17:38:28 -05:00
use std::map::HashMap;
2012-09-04 13:54:36 -05:00
use syntax::ast::*;
use syntax::diagnostic::span_handler;
use middle::ty;
use middle::ty::vid;
use syntax::print::pprust::*;
2011-06-27 16:45:38 -05:00
export ctxt;
export ty_abbrev;
export ac_no_abbrevs;
export ac_use_abbrevs;
export enc_ty;
export enc_bounds;
export enc_mode;
export enc_arg;
2011-06-27 16:45:38 -05:00
type ctxt = {
diag: span_handler,
2011-09-02 17:34:58 -05:00
// Def -> str Callback:
ds: fn@(def_id) -> ~str,
2011-09-02 17:34:58 -05:00
// The type context.
tcx: ty::ctxt,
reachable: fn@(node_id) -> bool,
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.
type ty_abbrev = {pos: uint, len: uint, s: @~str};
2012-09-10 17:38:28 -05:00
enum abbrev_ctxt { ac_no_abbrevs, ac_use_abbrevs(HashMap<ty::t, ty_abbrev>), }
fn cx_uses_abbrevs(cx: @ctxt) -> bool {
2012-08-06 14:34:08 -05:00
match cx.abbrevs {
2012-08-03 21:59:04 -05:00
ac_no_abbrevs => return false,
ac_use_abbrevs(_) => return true
}
}
2012-08-14 15:38:35 -05:00
fn enc_ty(w: io::Writer, cx: @ctxt, t: ty::t) {
2012-08-06 14:34:08 -05:00
match cx.abbrevs {
2012-08-03 21:59:04 -05:00
ac_no_abbrevs => {
2012-08-06 14:34:08 -05:00
let result_str = match cx.tcx.short_names_cache.find(t) {
Some(s) => *s,
None => {
let s = do io::with_str_writer |wr| {
enc_sty(wr, cx, ty::get(t).sty);
};
cx.tcx.short_names_cache.insert(t, @s);
s
2011-07-27 07:19:39 -05:00
}
};
w.write_str(result_str);
2011-07-27 07:19:39 -05:00
}
2012-08-03 21:59:04 -05:00
ac_use_abbrevs(abbrevs) => {
2012-08-06 14:34:08 -05:00
match abbrevs.find(t) {
2012-08-20 14:23:37 -05:00
Some(a) => { w.write_str(*a.s); return; }
None => {
let pos = w.tell();
2012-08-06 14:34:08 -05:00
match ty::type_def_id(t) {
2012-08-20 14:23:37 -05:00
Some(def_id) => {
2012-03-22 22:06:01 -05:00
// Do not emit node ids that map to unexported names. Those
// are not helpful.
if def_id.crate != local_crate ||
cx.reachable(def_id.node) {
2012-03-22 22:06:01 -05:00
w.write_char('"');
w.write_str(cx.ds(def_id));
w.write_char('|');
}
}
2012-08-03 21:59:04 -05:00
_ => {}
}
enc_sty(w, cx, ty::get(t).sty);
let end = w.tell();
2011-07-27 07:19:39 -05:00
let len = end - pos;
fn estimate_sz(u: uint) -> uint {
let mut n = u;
let mut len = 0u;
2011-07-27 07:19:39 -05:00
while n != 0u { len += 1u; n = n >> 4u; }
2012-08-01 19:30:05 -05:00
return len;
2011-07-27 07:19:39 -05:00
}
let abbrev_len = 3u + estimate_sz(pos) + estimate_sz(len);
if abbrev_len < len {
// I.e. it's actually an abbreviation.
let s = ~"#" + 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);
}
2012-08-01 19:30:05 -05:00
return;
2011-07-27 07:19:39 -05:00
}
}
2011-07-27 07:19:39 -05:00
}
}
}
2012-08-14 15:38:35 -05:00
fn enc_mt(w: io::Writer, cx: @ctxt, mt: ty::mt) {
2012-08-06 14:34:08 -05:00
match mt.mutbl {
2012-08-03 21:59:04 -05:00
m_imm => (),
m_mutbl => w.write_char('m'),
m_const => w.write_char('?')
}
enc_ty(w, cx, mt.ty);
}
2012-08-20 14:23:37 -05:00
fn enc_opt<T>(w: io::Writer, t: Option<T>, enc_f: fn(T)) {
2012-08-06 14:34:08 -05:00
match t {
2012-08-20 14:23:37 -05:00
None => w.write_char('n'),
Some(v) => {
w.write_char('s');
enc_f(v);
}
}
}
2012-08-14 15:38:35 -05:00
fn enc_substs(w: io::Writer, cx: @ctxt, substs: ty::substs) {
2012-06-30 18:19:07 -05:00
do enc_opt(w, substs.self_r) |r| { enc_region(w, cx, r) }
do enc_opt(w, substs.self_ty) |t| { enc_ty(w, cx, t) }
w.write_char('[');
for substs.tps.each |t| { enc_ty(w, cx, *t); }
w.write_char(']');
}
2012-08-14 15:38:35 -05:00
fn enc_region(w: io::Writer, cx: @ctxt, r: ty::region) {
2012-08-06 14:34:08 -05:00
match r {
2012-08-03 21:59:04 -05:00
ty::re_bound(br) => {
w.write_char('b');
2012-07-18 18:18:02 -05:00
enc_bound_region(w, cx, br);
}
2012-08-03 21:59:04 -05:00
ty::re_free(id, br) => {
w.write_char('f');
w.write_char('[');
w.write_int(id);
w.write_char('|');
2012-07-18 18:18:02 -05:00
enc_bound_region(w, cx, br);
w.write_char(']');
}
2012-08-03 21:59:04 -05:00
ty::re_scope(nid) => {
w.write_char('s');
w.write_int(nid);
w.write_char('|');
}
2012-08-03 21:59:04 -05:00
ty::re_static => {
w.write_char('t');
}
2012-08-03 21:59:04 -05:00
ty::re_var(_) => {
// these should not crop up after typeck
cx.diag.handler().bug(~"Cannot encode region variables");
}
}
}
2012-07-18 18:18:02 -05:00
fn enc_bound_region(w: io::Writer, cx: @ctxt, br: ty::bound_region) {
2012-08-06 14:34:08 -05:00
match br {
2012-08-03 21:59:04 -05:00
ty::br_self => w.write_char('s'),
ty::br_anon(idx) => {
w.write_char('a');
w.write_uint(idx);
w.write_char('|');
}
2012-08-03 21:59:04 -05:00
ty::br_named(s) => {
w.write_char('[');
2012-07-18 18:18:02 -05:00
w.write_str(cx.tcx.sess.str_of(s));
w.write_char(']')
}
2012-08-03 21:59:04 -05:00
ty::br_cap_avoid(id, br) => {
w.write_char('c');
w.write_int(id);
w.write_char('|');
2012-07-18 18:18:02 -05:00
enc_bound_region(w, cx, *br);
}
2012-03-08 16:05:16 -06:00
}
}
2012-08-14 15:38:35 -05:00
fn enc_vstore(w: io::Writer, cx: @ctxt, v: ty::vstore) {
w.write_char('/');
2012-08-06 14:34:08 -05:00
match v {
2012-08-03 21:59:04 -05:00
ty::vstore_fixed(u) => {
w.write_uint(u);
w.write_char('|');
}
2012-08-03 21:59:04 -05:00
ty::vstore_uniq => {
w.write_char('~');
}
2012-08-03 21:59:04 -05:00
ty::vstore_box => {
w.write_char('@');
}
2012-08-03 21:59:04 -05:00
ty::vstore_slice(r) => {
w.write_char('&');
enc_region(w, cx, r);
}
}
}
2012-08-14 15:38:35 -05:00
fn enc_sty(w: io::Writer, cx: @ctxt, st: ty::sty) {
2012-08-06 14:34:08 -05:00
match st {
2012-08-03 21:59:04 -05:00
ty::ty_nil => w.write_char('n'),
ty::ty_bot => w.write_char('z'),
ty::ty_bool => w.write_char('b'),
ty::ty_int(t) => {
2012-08-06 14:34:08 -05:00
match t {
2012-08-03 21:59:04 -05:00
ty_i => w.write_char('i'),
ty_char => w.write_char('c'),
ty_i8 => w.write_str(&"MB"),
ty_i16 => w.write_str(&"MW"),
ty_i32 => w.write_str(&"ML"),
ty_i64 => w.write_str(&"MD")
}
}
2012-08-03 21:59:04 -05:00
ty::ty_uint(t) => {
2012-08-06 14:34:08 -05:00
match t {
2012-08-03 21:59:04 -05:00
ty_u => w.write_char('u'),
ty_u8 => w.write_str(&"Mb"),
ty_u16 => w.write_str(&"Mw"),
ty_u32 => w.write_str(&"Ml"),
ty_u64 => w.write_str(&"Md")
}
}
2012-08-03 21:59:04 -05:00
ty::ty_float(t) => {
2012-08-06 14:34:08 -05:00
match t {
2012-08-03 21:59:04 -05:00
ty_f => w.write_char('l'),
ty_f32 => w.write_str(&"Mf"),
ty_f64 => w.write_str(&"MF"),
}
2011-07-27 07:19:39 -05:00
}
2012-08-03 21:59:04 -05:00
ty::ty_enum(def, substs) => {
w.write_str(&"t[");
w.write_str(cx.ds(def));
2011-07-27 07:19:39 -05:00
w.write_char('|');
enc_substs(w, cx, substs);
2011-07-27 07:19:39 -05:00
w.write_char(']');
}
ty::ty_trait(def, substs, vstore) => {
w.write_str(&"x[");
w.write_str(cx.ds(def));
w.write_char('|');
enc_substs(w, cx, substs);
enc_vstore(w, cx, vstore);
w.write_char(']');
}
2012-08-03 21:59:04 -05:00
ty::ty_tup(ts) => {
w.write_str(&"T[");
for ts.each |t| { enc_ty(w, cx, *t); }
w.write_char(']');
}
2012-08-03 21:59:04 -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); }
ty::ty_ptr(mt) => { w.write_char('*'); enc_mt(w, cx, mt); }
ty::ty_rptr(r, mt) => {
2012-03-08 16:05:16 -06:00
w.write_char('&');
enc_region(w, cx, r);
2012-03-08 16:05:16 -06:00
enc_mt(w, cx, mt);
}
2012-08-03 21:59:04 -05:00
ty::ty_evec(mt, v) => {
w.write_char('V');
enc_mt(w, cx, mt);
enc_vstore(w, cx, v);
}
2012-08-03 21:59:04 -05:00
ty::ty_estr(v) => {
w.write_char('v');
enc_vstore(w, cx, v);
}
2012-08-03 21:59:04 -05:00
ty::ty_unboxed_vec(mt) => { w.write_char('U'); enc_mt(w, cx, mt); }
ty::ty_rec(fields) => {
w.write_str(&"R[");
2012-06-30 18:19:07 -05:00
for fields.each |field| {
2012-07-18 18:18:02 -05:00
w.write_str(cx.tcx.sess.str_of(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(']');
}
2012-08-03 21:59:04 -05:00
ty::ty_fn(f) => {
enc_ty_fn(w, cx, f);
2011-07-27 07:19:39 -05:00
}
ty::ty_infer(ty::TyVar(id)) => {
w.write_char('X');
w.write_uint(id.to_uint());
}
ty::ty_infer(ty::IntVar(id)) => {
w.write_char('X');
w.write_char('I');
w.write_uint(id.to_uint());
}
2012-08-03 21:59:04 -05:00
ty::ty_param({idx: id, def_id: did}) => {
w.write_char('p');
w.write_str(cx.ds(did));
w.write_char('|');
2011-08-24 23:26:19 -05:00
w.write_str(uint::str(id));
}
2012-08-03 21:59:04 -05:00
ty::ty_self => {
w.write_char('s');
}
2012-08-03 21:59:04 -05:00
ty::ty_type => w.write_char('Y'),
ty::ty_opaque_closure_ptr(ty::ck_block) => w.write_str(&"C&"),
ty::ty_opaque_closure_ptr(ty::ck_box) => w.write_str(&"C@"),
ty::ty_opaque_closure_ptr(ty::ck_uniq) => w.write_str(&"C~"),
ty::ty_opaque_box => w.write_char('B'),
ty::ty_class(def, substs) => {
2012-08-22 19:24:52 -05:00
debug!("~~~~ %s", ~"a[");
w.write_str(&"a[");
let s = cx.ds(def);
2012-08-22 19:24:52 -05:00
debug!("~~~~ %s", s);
w.write_str(s);
2012-08-22 19:24:52 -05:00
debug!("~~~~ %s", ~"|");
w.write_char('|');
enc_substs(w, cx, substs);
2012-08-22 19:24:52 -05:00
debug!("~~~~ %s", ~"]");
w.write_char(']');
}
}
}
2012-08-14 15:38:35 -05:00
fn enc_proto(w: io::Writer, cx: @ctxt, proto: ty::fn_proto) {
w.write_str(&"f");
2012-08-06 14:34:08 -05:00
match proto {
ty::proto_bare => w.write_str(&"n"),
ty::proto_vstore(vstore) => {
w.write_str(&"v");
enc_vstore(w, cx, vstore);
}
}
}
fn enc_arg(w: io::Writer, cx: @ctxt, arg: ty::arg) {
enc_mode(w, cx, arg.mode);
enc_ty(w, cx, arg.ty);
}
2012-08-14 15:38:35 -05:00
fn enc_mode(w: io::Writer, cx: @ctxt, m: mode) {
2012-08-06 14:34:08 -05:00
match ty::resolved_mode(cx.tcx, m) {
by_mutbl_ref => w.write_char('&'),
2012-08-03 21:59:04 -05:00
by_move => w.write_char('-'),
by_copy => w.write_char('+'),
by_ref => w.write_char('='),
by_val => w.write_char('#')
}
}
2012-08-14 15:38:35 -05:00
fn enc_purity(w: io::Writer, p: purity) {
2012-08-06 14:34:08 -05:00
match p {
2012-08-03 21:59:04 -05:00
pure_fn => w.write_char('p'),
impure_fn => w.write_char('i'),
unsafe_fn => w.write_char('u'),
extern_fn => w.write_char('c')
2012-05-25 01:44:58 -05:00
}
}
2012-09-07 09:37:19 -05:00
fn enc_ty_fn(w: io::Writer, cx: @ctxt, ft: ty::FnTy) {
enc_proto(w, cx, ft.meta.proto);
enc_purity(w, ft.meta.purity);
enc_bounds(w, cx, ft.meta.bounds);
w.write_char('[');
2012-09-07 09:37:19 -05:00
for ft.sig.inputs.each |arg| {
enc_arg(w, cx, *arg);
}
w.write_char(']');
2012-09-07 09:37:19 -05:00
match ft.meta.ret_style {
2012-08-03 21:59:04 -05:00
noreturn => w.write_char('!'),
2012-09-07 09:37:19 -05:00
_ => enc_ty(w, cx, ft.sig.output)
}
}
2012-08-14 15:38:35 -05:00
fn enc_bounds(w: io::Writer, cx: @ctxt, bs: @~[ty::param_bound]) {
for vec::each(*bs) |bound| {
match *bound {
2012-08-03 21:59:04 -05:00
ty::bound_send => w.write_char('S'),
ty::bound_copy => w.write_char('C'),
ty::bound_const => w.write_char('K'),
ty::bound_owned => w.write_char('O'),
ty::bound_trait(tp) => {
w.write_char('I');
enc_ty(w, cx, tp);
}
}
}
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
// End:
//