// 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 or the MIT license // , 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)] use std::cell::RefCell; use middle::subst; use middle::subst::VecPerParamSpace; use middle::ty::ParamTy; use middle::ty; use util::nodemap::FnvHashMap; use syntax::abi::Abi; use syntax::ast; use syntax::diagnostic::SpanHandler; use syntax::parse::token; use rbml::io::SeekableMemWriter; macro_rules! mywrite( ($($arg:tt)*) => ({ write!($($arg)*); }) ) pub struct ctxt<'a, 'tcx: 'a> { pub diag: &'a SpanHandler, // Def -> str Callback: pub ds: fn(ast::DefId) -> String, // The type context. pub tcx: &'a ty::ctxt<'tcx>, 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 { s: String } pub type abbrev_map = RefCell>; pub fn enc_ty(w: &mut SeekableMemWriter, cx: &ctxt, t: ty::t) { match cx.abbrevs.borrow_mut().get(&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 { s: format!("#{:x}:{:x}#", pos, len) }); } } fn enc_mutability(w: &mut SeekableMemWriter, mt: ast::Mutability) { match mt { ast::MutImmutable => (), ast::MutMutable => mywrite!(w, "m"), } } fn enc_mt(w: &mut SeekableMemWriter, cx: &ctxt, mt: ty::mt) { enc_mutability(w, mt.mutbl); enc_ty(w, cx, mt.ty); } fn enc_opt(w: &mut SeekableMemWriter, t: Option, enc_f: |&mut SeekableMemWriter, T|) { match t { None => mywrite!(w, "n"), Some(v) => { mywrite!(w, "s"); enc_f(w, v); } } } fn enc_vec_per_param_space(w: &mut SeekableMemWriter, cx: &ctxt, v: &VecPerParamSpace, op: |&mut SeekableMemWriter, &ctxt, &T|) { for &space in subst::ParamSpace::all().iter() { mywrite!(w, "["); for t in v.get_slice(space).iter() { op(w, cx, t); } mywrite!(w, "]"); } } pub fn enc_substs(w: &mut SeekableMemWriter, cx: &ctxt, substs: &subst::Substs) { enc_region_substs(w, cx, &substs.regions); enc_vec_per_param_space(w, cx, &substs.types, |w, cx, &ty| enc_ty(w, cx, ty)); } fn enc_region_substs(w: &mut SeekableMemWriter, cx: &ctxt, substs: &subst::RegionSubsts) { match *substs { subst::ErasedRegions => { mywrite!(w, "e"); } subst::NonerasedRegions(ref regions) => { mywrite!(w, "n"); enc_vec_per_param_space(w, cx, regions, |w, cx, &r| enc_region(w, cx, r)); } } } pub fn enc_region(w: &mut SeekableMemWriter, cx: &ctxt, r: ty::Region) { match r { ty::ReLateBound(id, br) => { mywrite!(w, "b[{}|", id.depth); enc_bound_region(w, cx, br); mywrite!(w, "]"); } ty::ReEarlyBound(node_id, space, index, name) => { mywrite!(w, "B[{}|{}|{}|{}]", node_id, space.to_uint(), index, token::get_name(name)); } ty::ReFree(ref fr) => { mywrite!(w, "f[{}|", fr.scope_id); enc_bound_region(w, cx, fr.bound_region); mywrite!(w, "]"); } ty::ReScope(nid) => { mywrite!(w, "s{}|", nid); } ty::ReStatic => { mywrite!(w, "t"); } ty::ReEmpty => { mywrite!(w, "e"); } ty::ReInfer(_) => { // these should not crop up after typeck cx.diag.handler().bug("cannot encode region variables"); } } } fn enc_bound_region(w: &mut SeekableMemWriter, cx: &ctxt, br: ty::BoundRegion) { match br { ty::BrAnon(idx) => { mywrite!(w, "a{}|", idx); } ty::BrNamed(d, name) => { mywrite!(w, "[{}|{}]", (cx.ds)(d), token::get_name(name)); } ty::BrFresh(id) => { mywrite!(w, "f{}|", id); } ty::BrEnv => { mywrite!(w, "e|"); } } } pub fn enc_trait_ref(w: &mut SeekableMemWriter, cx: &ctxt, s: &ty::TraitRef) { mywrite!(w, "{}|", (cx.ds)(s.def_id)); enc_substs(w, cx, &s.substs); } pub fn enc_trait_store(w: &mut SeekableMemWriter, cx: &ctxt, s: ty::TraitStore) { match s { ty::UniqTraitStore => mywrite!(w, "~"), ty::RegionTraitStore(re, m) => { mywrite!(w, "&"); enc_region(w, cx, re); enc_mutability(w, m); } } } fn enc_sty(w: &mut SeekableMemWriter, cx: &ctxt, st: &ty::sty) { match *st { ty::ty_bool => mywrite!(w, "b"), ty::ty_char => mywrite!(w, "c"), ty::ty_int(t) => { match t { ast::TyI => mywrite!(w, "i"), ast::TyI8 => mywrite!(w, "MB"), ast::TyI16 => mywrite!(w, "MW"), ast::TyI32 => mywrite!(w, "ML"), ast::TyI64 => mywrite!(w, "MD") } } ty::ty_uint(t) => { match t { ast::TyU => mywrite!(w, "u"), ast::TyU8 => mywrite!(w, "Mb"), ast::TyU16 => mywrite!(w, "Mw"), ast::TyU32 => mywrite!(w, "Ml"), ast::TyU64 => mywrite!(w, "Md") } } ty::ty_float(t) => { match t { ast::TyF32 => mywrite!(w, "Mf"), ast::TyF64 => mywrite!(w, "MF"), } } 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 { ref principal, ref bounds }) => { mywrite!(w, "x["); enc_trait_ref(w, cx, principal); enc_existential_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_uniq(typ) => { mywrite!(w, "~"); enc_ty(w, cx, typ); } 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(t, sz) => { mywrite!(w, "V"); enc_ty(w, cx, t); mywrite!(w, "/"); match sz { Some(n) => mywrite!(w, "{}|", n), None => mywrite!(w, "|"), } } ty::ty_str => { mywrite!(w, "v"); } ty::ty_closure(ref f) => { mywrite!(w, "f"); enc_closure_ty(w, cx, &**f); } 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"); } ty::ty_param(ParamTy {space, idx: id, def_id: did}) => { mywrite!(w, "p{}|{}|{}|", (cx.ds)(did), id, space.to_uint()) } ty::ty_struct(def, ref substs) => { mywrite!(w, "a[{}|", (cx.ds)(def)); enc_substs(w, cx, substs); mywrite!(w, "]"); } ty::ty_unboxed_closure(def, region, ref substs) => { mywrite!(w, "k[{}|", (cx.ds)(def)); enc_region(w, cx, region); enc_substs(w, cx, substs); mywrite!(w, "]"); } ty::ty_err => { mywrite!(w, "e"); } ty::ty_open(_) => { cx.diag.handler().bug("unexpected type in enc_sty (ty_open)"); } } } fn enc_fn_style(w: &mut SeekableMemWriter, p: ast::FnStyle) { match p { ast::NormalFn => mywrite!(w, "n"), ast::UnsafeFn => mywrite!(w, "u"), } } fn enc_abi(w: &mut SeekableMemWriter, abi: Abi) { mywrite!(w, "["); mywrite!(w, "{}", abi.name()); mywrite!(w, "]") } fn enc_onceness(w: &mut SeekableMemWriter, o: ast::Onceness) { match o { ast::Once => mywrite!(w, "o"), ast::Many => mywrite!(w, "m") } } pub fn enc_bare_fn_ty(w: &mut SeekableMemWriter, cx: &ctxt, ft: &ty::BareFnTy) { enc_fn_style(w, ft.fn_style); enc_abi(w, ft.abi); enc_fn_sig(w, cx, &ft.sig); } pub fn enc_closure_ty(w: &mut SeekableMemWriter, cx: &ctxt, ft: &ty::ClosureTy) { enc_fn_style(w, ft.fn_style); enc_onceness(w, ft.onceness); enc_trait_store(w, cx, ft.store); enc_existential_bounds(w, cx, &ft.bounds); enc_fn_sig(w, cx, &ft.sig); enc_abi(w, ft.abi); } fn enc_fn_sig(w: &mut SeekableMemWriter, cx: &ctxt, fsig: &ty::FnSig) { mywrite!(w, "["); for ty in fsig.inputs.iter() { enc_ty(w, cx, *ty); } mywrite!(w, "]"); if fsig.variadic { mywrite!(w, "V"); } else { mywrite!(w, "N"); } match fsig.output { ty::FnConverging(result_type) => { enc_ty(w, cx, result_type); } ty::FnDiverging => { mywrite!(w, "z"); } } } pub fn enc_builtin_bounds(w: &mut SeekableMemWriter, _cx: &ctxt, bs: &ty::BuiltinBounds) { for bound in bs.iter() { match bound { ty::BoundSend => mywrite!(w, "S"), ty::BoundSized => mywrite!(w, "Z"), ty::BoundCopy => mywrite!(w, "P"), ty::BoundSync => mywrite!(w, "T"), } } mywrite!(w, "."); } pub fn enc_existential_bounds(w: &mut SeekableMemWriter, cx: &ctxt, bs: &ty::ExistentialBounds) { enc_region(w, cx, bs.region_bound); enc_builtin_bounds(w, cx, &bs.builtin_bounds); } pub fn enc_bounds(w: &mut SeekableMemWriter, cx: &ctxt, bs: &ty::ParamBounds) { enc_builtin_bounds(w, cx, &bs.builtin_bounds); for &r in bs.region_bounds.iter() { mywrite!(w, "R"); enc_region(w, cx, r); } for tp in bs.trait_bounds.iter() { mywrite!(w, "I"); enc_trait_ref(w, cx, &**tp); } mywrite!(w, "."); } pub fn enc_type_param_def(w: &mut SeekableMemWriter, cx: &ctxt, v: &ty::TypeParameterDef) { mywrite!(w, "{}:{}|{}|{}|", token::get_name(v.name), (cx.ds)(v.def_id), v.space.to_uint(), v.index); enc_opt(w, v.associated_with, |w, did| mywrite!(w, "{}", (cx.ds)(did))); mywrite!(w, "|"); enc_bounds(w, cx, &v.bounds); enc_opt(w, v.default, |w, t| enc_ty(w, cx, t)); }