rust/src/librustc/metadata/tyencode.rs

363 lines
10 KiB
Rust
Raw Normal View History

// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Type encoding
#![allow(unused_must_use)] // as with encoding, everything is a no-fail MemWriter
#![allow(non_camel_case_types)]
2014-01-29 18:42:19 -08:00
use std::cell::RefCell;
use std::collections::HashMap;
use std::io::MemWriter;
2013-03-26 16:38:07 -04:00
use middle::ty::param_ty;
use middle::ty;
use syntax::abi::Abi;
use syntax::ast;
2012-09-04 11:54:36 -07:00
use syntax::ast::*;
use syntax::diagnostic::SpanHandler;
use syntax::parse::token;
macro_rules! mywrite( ($($arg:tt)*) => ({ write!($($arg)*); }) )
2013-10-21 17:11:42 -07:00
2014-03-06 05:07:47 +02:00
pub struct ctxt<'a> {
pub diag: &'a SpanHandler,
2011-09-02 15:34:58 -07:00
// Def -> str Callback:
pub ds: fn(DefId) -> String,
2011-09-02 15:34:58 -07:00
// The type context.
pub tcx: &'a ty::ctxt,
2014-04-22 19:06:43 +03:00
pub abbrevs: &'a abbrev_map
}
// 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.
pub struct ty_abbrev {
pos: uint,
len: uint,
s: String
}
2014-04-22 19:06:43 +03:00
pub type abbrev_map = RefCell<HashMap<ty::t, ty_abbrev>>;
pub fn enc_ty(w: &mut MemWriter, cx: &ctxt, t: ty::t) {
2014-04-22 19:06:43 +03:00
match cx.abbrevs.borrow_mut().find(&t) {
Some(a) => { w.write(a.s.as_bytes()); return; }
None => {}
}
let pos = w.tell().unwrap();
enc_sty(w, cx, &ty::get(t).sty);
let end = w.tell().unwrap();
let len = end - pos;
fn estimate_sz(u: u64) -> u64 {
let mut n = u;
let mut len = 0;
while n != 0 { len += 1; n = n >> 4; }
return len;
}
let abbrev_len = 3 + estimate_sz(pos) + estimate_sz(len);
if abbrev_len < len {
// I.e. it's actually an abbreviation.
cx.abbrevs.borrow_mut().insert(t, ty_abbrev {
pos: pos as uint,
len: len as uint,
s: format!("\\#{:x}:{:x}\\#", pos, len)
2014-04-22 19:06:43 +03:00
});
}
}
fn enc_mutability(w: &mut MemWriter, mt: ast::Mutability) {
match mt {
2013-10-21 17:11:42 -07:00
MutImmutable => (),
MutMutable => mywrite!(w, "m"),
}
}
fn enc_mt(w: &mut MemWriter, cx: &ctxt, mt: ty::mt) {
enc_mutability(w, mt.mutbl);
enc_ty(w, cx, mt.ty);
}
fn enc_opt<T>(w: &mut MemWriter, t: Option<T>, enc_f: |&mut MemWriter, T|) {
match t {
2013-10-21 17:11:42 -07:00
None => mywrite!(w, "n"),
Some(v) => {
mywrite!(w, "s");
enc_f(w, v);
2013-10-21 17:11:42 -07:00
}
}
}
pub fn enc_substs(w: &mut MemWriter, cx: &ctxt, substs: &ty::substs) {
enc_region_substs(w, cx, &substs.regions);
enc_opt(w, substs.self_ty, |w, t| enc_ty(w, cx, t));
2013-10-21 17:11:42 -07:00
mywrite!(w, "[");
for t in substs.tps.iter() { enc_ty(w, cx, *t); }
2013-10-21 17:11:42 -07:00
mywrite!(w, "]");
}
fn enc_region_substs(w: &mut MemWriter, cx: &ctxt, substs: &ty::RegionSubsts) {
match *substs {
ty::ErasedRegions => {
2013-10-21 17:11:42 -07:00
mywrite!(w, "e");
}
ty::NonerasedRegions(ref regions) => {
2013-10-21 17:11:42 -07:00
mywrite!(w, "n");
for &r in regions.iter() {
enc_region(w, cx, r);
}
2013-10-21 17:11:42 -07:00
mywrite!(w, ".");
}
}
}
fn enc_region(w: &mut MemWriter, cx: &ctxt, r: ty::Region) {
2012-08-06 12:34:08 -07:00
match r {
2013-10-29 10:34:11 -04:00
ty::ReLateBound(id, br) => {
mywrite!(w, "b[{}|", id);
2013-10-21 17:11:42 -07:00
enc_bound_region(w, cx, br);
mywrite!(w, "]");
}
ty::ReEarlyBound(node_id, index, name) => {
mywrite!(w, "B[{}|{}|{}]",
node_id,
index,
token::get_name(name));
2013-10-21 17:11:42 -07:00
}
2013-10-29 10:34:11 -04:00
ty::ReFree(ref fr) => {
2013-10-21 17:11:42 -07:00
mywrite!(w, "f[{}|", fr.scope_id);
enc_bound_region(w, cx, fr.bound_region);
mywrite!(w, "]");
}
2013-10-29 10:34:11 -04:00
ty::ReScope(nid) => {
mywrite!(w, "s{}|", nid);
}
2013-10-29 10:34:11 -04:00
ty::ReStatic => {
mywrite!(w, "t");
}
2013-10-29 10:34:11 -04:00
ty::ReEmpty => {
mywrite!(w, "e");
}
2013-10-29 10:34:11 -04:00
ty::ReInfer(_) => {
2013-10-21 17:11:42 -07:00
// these should not crop up after typeck
cx.diag.handler().bug("cannot encode region variables");
2013-10-21 17:11:42 -07:00
}
}
}
fn enc_bound_region(w: &mut MemWriter, cx: &ctxt, br: ty::BoundRegion) {
2012-08-06 12:34:08 -07:00
match br {
2013-10-29 10:34:11 -04:00
ty::BrAnon(idx) => {
mywrite!(w, "a{}|", idx);
}
ty::BrNamed(d, name) => {
mywrite!(w, "[{}|{}]",
(cx.ds)(d),
token::get_name(name));
}
2013-10-29 10:34:11 -04:00
ty::BrFresh(id) => {
mywrite!(w, "f{}|", id);
}
2012-03-08 14:05:16 -08:00
}
}
pub fn enc_trait_ref(w: &mut MemWriter, cx: &ctxt, s: &ty::TraitRef) {
2013-10-21 17:11:42 -07:00
mywrite!(w, "{}|", (cx.ds)(s.def_id));
enc_substs(w, cx, &s.substs);
}
pub fn enc_trait_store(w: &mut MemWriter, cx: &ctxt, s: ty::TraitStore) {
match s {
2013-10-21 17:11:42 -07:00
ty::UniqTraitStore => mywrite!(w, "~"),
ty::RegionTraitStore(re, m) => {
2013-10-21 17:11:42 -07:00
mywrite!(w, "&");
enc_region(w, cx, re);
enc_mutability(w, m);
}
}
}
fn enc_sty(w: &mut MemWriter, cx: &ctxt, st: &ty::sty) {
2013-07-02 12:47:32 -07:00
match *st {
2013-10-21 17:11:42 -07:00
ty::ty_nil => mywrite!(w, "n"),
ty::ty_bot => mywrite!(w, "z"),
ty::ty_bool => mywrite!(w, "b"),
ty::ty_char => mywrite!(w, "c"),
ty::ty_int(t) => {
match t {
TyI => mywrite!(w, "i"),
TyI8 => mywrite!(w, "MB"),
TyI16 => mywrite!(w, "MW"),
TyI32 => mywrite!(w, "ML"),
TyI64 => mywrite!(w, "MD")
2013-10-21 17:11:42 -07:00
}
}
2013-10-21 17:11:42 -07:00
ty::ty_uint(t) => {
match t {
TyU => mywrite!(w, "u"),
TyU8 => mywrite!(w, "Mb"),
TyU16 => mywrite!(w, "Mw"),
TyU32 => mywrite!(w, "Ml"),
TyU64 => mywrite!(w, "Md")
2013-10-21 17:11:42 -07:00
}
}
2013-10-21 17:11:42 -07:00
ty::ty_float(t) => {
match t {
TyF32 => mywrite!(w, "Mf"),
TyF64 => mywrite!(w, "MF"),
TyF128 => mywrite!(w, "MQ")
2013-10-21 17:11:42 -07:00
}
}
2013-10-21 17:11:42 -07:00
ty::ty_enum(def, ref substs) => {
mywrite!(w, "t[{}|", (cx.ds)(def));
enc_substs(w, cx, substs);
mywrite!(w, "]");
}
ty::ty_trait(box ty::TyTrait {
def_id,
ref substs,
store,
bounds
}) => {
mywrite!(w, "x[{}|", (cx.ds)(def_id));
2013-10-21 17:11:42 -07:00
enc_substs(w, cx, substs);
enc_trait_store(w, cx, store);
let bounds = ty::ParamBounds {builtin_bounds: bounds,
trait_bounds: Vec::new()};
2013-10-21 17:11:42 -07:00
enc_bounds(w, cx, &bounds);
mywrite!(w, "]");
}
ty::ty_tup(ref ts) => {
mywrite!(w, "T[");
for t in ts.iter() { enc_ty(w, cx, *t); }
mywrite!(w, "]");
}
ty::ty_box(typ) => { mywrite!(w, "@"); enc_ty(w, cx, typ); }
ty::ty_uniq(typ) => { mywrite!(w, "~"); enc_ty(w, cx, typ); }
2013-10-21 17:11:42 -07:00
ty::ty_ptr(mt) => { mywrite!(w, "*"); enc_mt(w, cx, mt); }
ty::ty_rptr(r, mt) => {
mywrite!(w, "&");
enc_region(w, cx, r);
enc_mt(w, cx, mt);
}
ty::ty_vec(mt, sz) => {
2013-10-21 17:11:42 -07:00
mywrite!(w, "V");
enc_mt(w, cx, mt);
mywrite!(w, "/");
match sz {
Some(n) => mywrite!(w, "{}|", n),
None => mywrite!(w, "|"),
}
2013-10-21 17:11:42 -07:00
}
ty::ty_str => {
2013-10-21 17:11:42 -07:00
mywrite!(w, "v");
}
ty::ty_closure(ref f) => {
mywrite!(w, "f");
enc_closure_ty(w, cx, *f);
2013-10-21 17:11:42 -07:00
}
ty::ty_bare_fn(ref f) => {
mywrite!(w, "F");
enc_bare_fn_ty(w, cx, f);
}
ty::ty_infer(_) => {
cx.diag.handler().bug("cannot encode inference variable types");
2013-10-21 17:11:42 -07:00
}
ty::ty_param(param_ty {idx: id, def_id: did}) => {
mywrite!(w, "p{}|{}", (cx.ds)(did), id);
}
ty::ty_self(did) => {
mywrite!(w, "s{}|", (cx.ds)(did));
}
ty::ty_struct(def, ref substs) => {
mywrite!(w, "a[{}|", (cx.ds)(def));
enc_substs(w, cx, substs);
mywrite!(w, "]");
}
ty::ty_err => fail!("shouldn't encode error type")
}
}
fn enc_fn_style(w: &mut MemWriter, p: FnStyle) {
2012-08-06 12:34:08 -07:00
match p {
NormalFn => mywrite!(w, "n"),
UnsafeFn => mywrite!(w, "u"),
2012-05-24 23:44:58 -07:00
}
}
fn enc_abi(w: &mut MemWriter, abi: Abi) {
2013-10-21 17:11:42 -07:00
mywrite!(w, "[");
mywrite!(w, "{}", abi.name());
2013-10-21 17:11:42 -07:00
mywrite!(w, "]")
}
fn enc_onceness(w: &mut MemWriter, o: Onceness) {
match o {
2013-10-21 17:11:42 -07:00
Once => mywrite!(w, "o"),
Many => mywrite!(w, "m")
}
}
pub fn enc_bare_fn_ty(w: &mut MemWriter, cx: &ctxt, ft: &ty::BareFnTy) {
enc_fn_style(w, ft.fn_style);
enc_abi(w, ft.abi);
enc_fn_sig(w, cx, &ft.sig);
}
fn enc_closure_ty(w: &mut MemWriter, cx: &ctxt, ft: &ty::ClosureTy) {
enc_fn_style(w, ft.fn_style);
enc_onceness(w, ft.onceness);
enc_trait_store(w, cx, ft.store);
let bounds = ty::ParamBounds {builtin_bounds: ft.bounds,
trait_bounds: Vec::new()};
enc_bounds(w, cx, &bounds);
enc_fn_sig(w, cx, &ft.sig);
}
fn enc_fn_sig(w: &mut MemWriter, cx: &ctxt, fsig: &ty::FnSig) {
mywrite!(w, "[{}|", fsig.binder_id);
for ty in fsig.inputs.iter() {
2013-04-26 19:13:38 -07:00
enc_ty(w, cx, *ty);
}
2013-10-21 17:11:42 -07:00
mywrite!(w, "]");
if fsig.variadic {
mywrite!(w, "V");
} else {
mywrite!(w, "N");
}
enc_ty(w, cx, fsig.output);
}
fn enc_bounds(w: &mut MemWriter, cx: &ctxt, bs: &ty::ParamBounds) {
2013-07-26 13:53:29 +09:00
for bound in bs.builtin_bounds.iter() {
match bound {
2013-10-21 17:11:42 -07:00
ty::BoundSend => mywrite!(w, "S"),
ty::BoundStatic => mywrite!(w, "O"),
ty::BoundSized => mywrite!(w, "Z"),
ty::BoundCopy => mywrite!(w, "P"),
2014-03-03 23:27:46 +01:00
ty::BoundShare => mywrite!(w, "T"),
}
2013-07-26 13:53:29 +09:00
}
2014-04-22 02:21:52 +03:00
for tp in bs.trait_bounds.iter() {
2013-10-21 17:11:42 -07:00
mywrite!(w, "I");
2014-04-22 02:21:52 +03:00
enc_trait_ref(w, cx, &**tp);
}
2013-10-21 17:11:42 -07:00
mywrite!(w, ".");
}
pub fn enc_type_param_def(w: &mut MemWriter, cx: &ctxt, v: &ty::TypeParameterDef) {
mywrite!(w, "{}:{}|", token::get_ident(v.ident), (cx.ds)(v.def_id));
2014-04-20 15:11:18 +03:00
enc_bounds(w, cx, &*v.bounds);
enc_opt(w, v.default, |w, t| enc_ty(w, cx, t));
}