1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 #![allow(unused_must_use)] // as with encoding, everything is a no-fail MemWriter
14 #![allow(non_camel_case_types)]
16 use std::cell::RefCell;
17 use collections::HashMap;
18 use std::io::MemWriter;
20 use middle::ty::param_ty;
26 use syntax::diagnostic::SpanHandler;
27 use syntax::parse::token;
29 macro_rules! mywrite( ($($arg:tt)*) => ({ write!($($arg)*); }) )
32 pub diag: &'a SpanHandler,
33 // Def -> str Callback:
34 pub ds: fn(DefId) -> String,
36 pub tcx: &'a ty::ctxt,
37 pub abbrevs: &'a abbrev_map
40 // Compact string representation for ty.t values. API ty_str & parse_from_str.
41 // Extra parameters are for converting to/from def_ids in the string rep.
42 // Whatever format you choose should not contain pipe characters.
43 pub struct ty_abbrev {
49 pub type abbrev_map = RefCell<HashMap<ty::t, ty_abbrev>>;
51 pub fn enc_ty(w: &mut MemWriter, cx: &ctxt, t: ty::t) {
52 match cx.abbrevs.borrow_mut().find(&t) {
53 Some(a) => { w.write(a.s.as_bytes()); return; }
56 let pos = w.tell().unwrap();
57 enc_sty(w, cx, &ty::get(t).sty);
58 let end = w.tell().unwrap();
60 fn estimate_sz(u: u64) -> u64 {
63 while n != 0 { len += 1; n = n >> 4; }
66 let abbrev_len = 3 + estimate_sz(pos) + estimate_sz(len);
68 // I.e. it's actually an abbreviation.
69 cx.abbrevs.borrow_mut().insert(t, ty_abbrev {
72 s: format_strbuf!("\\#{:x}:{:x}\\#", pos, len)
77 fn enc_mutability(w: &mut MemWriter, mt: ast::Mutability) {
80 MutMutable => mywrite!(w, "m"),
84 fn enc_mt(w: &mut MemWriter, cx: &ctxt, mt: ty::mt) {
85 enc_mutability(w, mt.mutbl);
89 fn enc_opt<T>(w: &mut MemWriter, t: Option<T>, enc_f: |&mut MemWriter, T|) {
91 None => mywrite!(w, "n"),
99 pub fn enc_substs(w: &mut MemWriter, cx: &ctxt, substs: &ty::substs) {
100 enc_region_substs(w, cx, &substs.regions);
101 enc_opt(w, substs.self_ty, |w, t| enc_ty(w, cx, t));
103 for t in substs.tps.iter() { enc_ty(w, cx, *t); }
107 fn enc_region_substs(w: &mut MemWriter, cx: &ctxt, substs: &ty::RegionSubsts) {
109 ty::ErasedRegions => {
112 ty::NonerasedRegions(ref regions) => {
114 for &r in regions.iter() {
115 enc_region(w, cx, r);
122 fn enc_region(w: &mut MemWriter, cx: &ctxt, r: ty::Region) {
124 ty::ReLateBound(id, br) => {
125 mywrite!(w, "b[{}|", id);
126 enc_bound_region(w, cx, br);
129 ty::ReEarlyBound(node_id, index, name) => {
130 mywrite!(w, "B[{}|{}|{}]",
133 token::get_name(name));
135 ty::ReFree(ref fr) => {
136 mywrite!(w, "f[{}|", fr.scope_id);
137 enc_bound_region(w, cx, fr.bound_region);
140 ty::ReScope(nid) => {
141 mywrite!(w, "s{}|", nid);
150 // these should not crop up after typeck
151 cx.diag.handler().bug("cannot encode region variables");
156 fn enc_bound_region(w: &mut MemWriter, cx: &ctxt, br: ty::BoundRegion) {
159 mywrite!(w, "a{}|", idx);
161 ty::BrNamed(d, name) => {
162 mywrite!(w, "[{}|{}]",
164 token::get_name(name));
167 mywrite!(w, "f{}|", id);
172 pub fn enc_trait_ref(w: &mut MemWriter, cx: &ctxt, s: &ty::TraitRef) {
173 mywrite!(w, "{}|", (cx.ds)(s.def_id));
174 enc_substs(w, cx, &s.substs);
177 pub fn enc_trait_store(w: &mut MemWriter, cx: &ctxt, s: ty::TraitStore) {
179 ty::UniqTraitStore => mywrite!(w, "~"),
180 ty::RegionTraitStore(re, m) => {
182 enc_region(w, cx, re);
183 enc_mutability(w, m);
188 fn enc_sty(w: &mut MemWriter, cx: &ctxt, st: &ty::sty) {
190 ty::ty_nil => mywrite!(w, "n"),
191 ty::ty_bot => mywrite!(w, "z"),
192 ty::ty_bool => mywrite!(w, "b"),
193 ty::ty_char => mywrite!(w, "c"),
196 TyI => mywrite!(w, "i"),
197 TyI8 => mywrite!(w, "MB"),
198 TyI16 => mywrite!(w, "MW"),
199 TyI32 => mywrite!(w, "ML"),
200 TyI64 => mywrite!(w, "MD")
205 TyU => mywrite!(w, "u"),
206 TyU8 => mywrite!(w, "Mb"),
207 TyU16 => mywrite!(w, "Mw"),
208 TyU32 => mywrite!(w, "Ml"),
209 TyU64 => mywrite!(w, "Md")
214 TyF32 => mywrite!(w, "Mf"),
215 TyF64 => mywrite!(w, "MF"),
216 TyF128 => mywrite!(w, "MQ")
219 ty::ty_enum(def, ref substs) => {
220 mywrite!(w, "t[{}|", (cx.ds)(def));
221 enc_substs(w, cx, substs);
224 ty::ty_trait(box ty::TyTrait {
230 mywrite!(w, "x[{}|", (cx.ds)(def_id));
231 enc_substs(w, cx, substs);
232 enc_trait_store(w, cx, store);
233 let bounds = ty::ParamBounds {builtin_bounds: bounds,
234 trait_bounds: Vec::new()};
235 enc_bounds(w, cx, &bounds);
238 ty::ty_tup(ref ts) => {
240 for t in ts.iter() { enc_ty(w, cx, *t); }
243 ty::ty_box(typ) => { mywrite!(w, "@"); enc_ty(w, cx, typ); }
244 ty::ty_uniq(typ) => { mywrite!(w, "~"); enc_ty(w, cx, typ); }
245 ty::ty_ptr(mt) => { mywrite!(w, "*"); enc_mt(w, cx, mt); }
246 ty::ty_rptr(r, mt) => {
248 enc_region(w, cx, r);
251 ty::ty_vec(mt, sz) => {
256 Some(n) => mywrite!(w, "{}|", n),
257 None => mywrite!(w, "|"),
263 ty::ty_closure(ref f) => {
265 enc_closure_ty(w, cx, *f);
267 ty::ty_bare_fn(ref f) => {
269 enc_bare_fn_ty(w, cx, f);
272 cx.diag.handler().bug("cannot encode inference variable types");
274 ty::ty_param(param_ty {idx: id, def_id: did}) => {
275 mywrite!(w, "p{}|{}", (cx.ds)(did), id);
277 ty::ty_self(did) => {
278 mywrite!(w, "s{}|", (cx.ds)(did));
280 ty::ty_struct(def, ref substs) => {
281 mywrite!(w, "a[{}|", (cx.ds)(def));
282 enc_substs(w, cx, substs);
285 ty::ty_err => fail!("shouldn't encode error type")
289 fn enc_fn_style(w: &mut MemWriter, p: FnStyle) {
291 NormalFn => mywrite!(w, "n"),
292 UnsafeFn => mywrite!(w, "u"),
296 fn enc_abi(w: &mut MemWriter, abi: Abi) {
298 mywrite!(w, "{}", abi.name());
302 fn enc_onceness(w: &mut MemWriter, o: Onceness) {
304 Once => mywrite!(w, "o"),
305 Many => mywrite!(w, "m")
309 pub fn enc_bare_fn_ty(w: &mut MemWriter, cx: &ctxt, ft: &ty::BareFnTy) {
310 enc_fn_style(w, ft.fn_style);
312 enc_fn_sig(w, cx, &ft.sig);
315 fn enc_closure_ty(w: &mut MemWriter, cx: &ctxt, ft: &ty::ClosureTy) {
316 enc_fn_style(w, ft.fn_style);
317 enc_onceness(w, ft.onceness);
318 enc_trait_store(w, cx, ft.store);
319 let bounds = ty::ParamBounds {builtin_bounds: ft.bounds,
320 trait_bounds: Vec::new()};
321 enc_bounds(w, cx, &bounds);
322 enc_fn_sig(w, cx, &ft.sig);
325 fn enc_fn_sig(w: &mut MemWriter, cx: &ctxt, fsig: &ty::FnSig) {
326 mywrite!(w, "[{}|", fsig.binder_id);
327 for ty in fsig.inputs.iter() {
336 enc_ty(w, cx, fsig.output);
339 fn enc_bounds(w: &mut MemWriter, cx: &ctxt, bs: &ty::ParamBounds) {
340 for bound in bs.builtin_bounds.iter() {
342 ty::BoundSend => mywrite!(w, "S"),
343 ty::BoundStatic => mywrite!(w, "O"),
344 ty::BoundSized => mywrite!(w, "Z"),
345 ty::BoundCopy => mywrite!(w, "P"),
346 ty::BoundShare => mywrite!(w, "T"),
350 for tp in bs.trait_bounds.iter() {
352 enc_trait_ref(w, cx, &**tp);
358 pub fn enc_type_param_def(w: &mut MemWriter, cx: &ctxt, v: &ty::TypeParameterDef) {
359 mywrite!(w, "{}:{}|", token::get_ident(v.ident), (cx.ds)(v.def_id));
360 enc_bounds(w, cx, &*v.bounds);
361 enc_opt(w, v.default, |w, t| enc_ty(w, cx, t));