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;
19 use std::io::MemWriter;
23 use middle::ty::param_ty;
26 use syntax::abi::AbiSet;
29 use syntax::diagnostic::SpanHandler;
30 use syntax::parse::token;
32 macro_rules! mywrite( ($wr:expr, $($arg:tt)*) => (
33 format_args!(|a| { mywrite($wr, a) }, $($arg)*)
37 diag: &'a SpanHandler,
38 // Def -> str Callback:
39 ds: fn(DefId) -> ~str,
45 // Compact string representation for ty.t values. API ty_str & parse_from_str.
46 // Extra parameters are for converting to/from def_ids in the string rep.
47 // Whatever format you choose should not contain pipe characters.
48 pub struct ty_abbrev {
54 pub enum abbrev_ctxt {
56 ac_use_abbrevs(@RefCell<HashMap<ty::t, ty_abbrev>>),
59 fn mywrite(w: &mut MemWriter, fmt: &fmt::Arguments) {
60 fmt::write(&mut *w as &mut io::Writer, fmt);
63 pub fn enc_ty(w: &mut MemWriter, cx: &ctxt, t: ty::t) {
68 let short_names_cache = cx.tcx.short_names_cache.borrow();
69 result_str_opt = short_names_cache.get()
75 let result_str = match result_str_opt {
78 let wr = &mut MemWriter::new();
79 enc_sty(wr, cx, &ty::get(t).sty);
80 let s = str::from_utf8(wr.get_ref()).unwrap();
81 let mut short_names_cache = cx.tcx
84 short_names_cache.get().insert(t, s.to_str());
88 w.write(result_str.as_bytes());
90 ac_use_abbrevs(abbrevs) => {
92 let mut abbrevs = abbrevs.borrow_mut();
93 match abbrevs.get().find(&t) {
94 Some(a) => { w.write(a.s.as_bytes()); return; }
98 let pos = w.tell().unwrap();
99 enc_sty(w, cx, &ty::get(t).sty);
100 let end = w.tell().unwrap();
102 fn estimate_sz(u: u64) -> u64 {
105 while n != 0 { len += 1; n = n >> 4; }
108 let abbrev_len = 3 + estimate_sz(pos) + estimate_sz(len);
109 if abbrev_len < len {
110 // I.e. it's actually an abbreviation.
111 let s = format!("\\#{:x}:{:x}\\#", pos, len);
112 let a = ty_abbrev { pos: pos as uint,
116 let mut abbrevs = abbrevs.borrow_mut();
117 abbrevs.get().insert(t, a);
125 fn enc_mutability(w: &mut MemWriter, mt: ast::Mutability) {
128 MutMutable => mywrite!(w, "m"),
132 fn enc_mt(w: &mut MemWriter, cx: &ctxt, mt: ty::mt) {
133 enc_mutability(w, mt.mutbl);
134 enc_ty(w, cx, mt.ty);
137 fn enc_opt<T>(w: &mut MemWriter, t: Option<T>, enc_f: |&mut MemWriter, T|) {
139 None => mywrite!(w, "n"),
147 pub fn enc_substs(w: &mut MemWriter, cx: &ctxt, substs: &ty::substs) {
148 enc_region_substs(w, cx, &substs.regions);
149 enc_opt(w, substs.self_ty, |w, t| enc_ty(w, cx, t));
151 for t in substs.tps.iter() { enc_ty(w, cx, *t); }
155 fn enc_region_substs(w: &mut MemWriter, cx: &ctxt, substs: &ty::RegionSubsts) {
157 ty::ErasedRegions => {
160 ty::NonerasedRegions(ref regions) => {
162 for &r in regions.iter() {
163 enc_region(w, cx, r);
170 fn enc_region(w: &mut MemWriter, cx: &ctxt, r: ty::Region) {
172 ty::ReLateBound(id, br) => {
173 mywrite!(w, "b[{}|", id);
174 enc_bound_region(w, cx, br);
177 ty::ReEarlyBound(node_id, index, name) => {
178 mywrite!(w, "B[{}|{}|{}]",
181 token::get_name(name));
183 ty::ReFree(ref fr) => {
184 mywrite!(w, "f[{}|", fr.scope_id);
185 enc_bound_region(w, cx, fr.bound_region);
188 ty::ReScope(nid) => {
189 mywrite!(w, "s{}|", nid);
198 // these should not crop up after typeck
199 cx.diag.handler().bug("cannot encode region variables");
204 fn enc_bound_region(w: &mut MemWriter, cx: &ctxt, br: ty::BoundRegion) {
207 mywrite!(w, "a{}|", idx);
209 ty::BrNamed(d, name) => {
210 mywrite!(w, "[{}|{}]",
212 token::get_name(name));
215 mywrite!(w, "f{}|", id);
220 pub fn enc_vstore(w: &mut MemWriter, cx: &ctxt, v: ty::vstore) {
223 ty::vstore_fixed(u) => mywrite!(w, "{}|", u),
224 ty::vstore_uniq => mywrite!(w, "~"),
225 ty::vstore_slice(r) => {
227 enc_region(w, cx, r);
232 pub fn enc_trait_ref(w: &mut MemWriter, cx: &ctxt, s: &ty::TraitRef) {
233 mywrite!(w, "{}|", (cx.ds)(s.def_id));
234 enc_substs(w, cx, &s.substs);
237 pub fn enc_trait_store(w: &mut MemWriter, cx: &ctxt, s: ty::TraitStore) {
239 ty::UniqTraitStore => mywrite!(w, "~"),
240 ty::RegionTraitStore(re) => {
242 enc_region(w, cx, re);
247 fn enc_sty(w: &mut MemWriter, cx: &ctxt, st: &ty::sty) {
249 ty::ty_nil => mywrite!(w, "n"),
250 ty::ty_bot => mywrite!(w, "z"),
251 ty::ty_bool => mywrite!(w, "b"),
252 ty::ty_char => mywrite!(w, "c"),
255 TyI => mywrite!(w, "i"),
256 TyI8 => mywrite!(w, "MB"),
257 TyI16 => mywrite!(w, "MW"),
258 TyI32 => mywrite!(w, "ML"),
259 TyI64 => mywrite!(w, "MD")
264 TyU => mywrite!(w, "u"),
265 TyU8 => mywrite!(w, "Mb"),
266 TyU16 => mywrite!(w, "Mw"),
267 TyU32 => mywrite!(w, "Ml"),
268 TyU64 => mywrite!(w, "Md")
273 TyF32 => mywrite!(w, "Mf"),
274 TyF64 => mywrite!(w, "MF"),
277 ty::ty_enum(def, ref substs) => {
278 mywrite!(w, "t[{}|", (cx.ds)(def));
279 enc_substs(w, cx, substs);
282 ty::ty_trait(~ty::TyTrait { def_id, ref substs, store, mutability, bounds }) => {
283 mywrite!(w, "x[{}|", (cx.ds)(def_id));
284 enc_substs(w, cx, substs);
285 enc_trait_store(w, cx, store);
286 enc_mutability(w, mutability);
287 let bounds = ty::ParamBounds {builtin_bounds: bounds,
288 trait_bounds: Vec::new()};
289 enc_bounds(w, cx, &bounds);
292 ty::ty_tup(ref ts) => {
294 for t in ts.iter() { enc_ty(w, cx, *t); }
297 ty::ty_box(typ) => { mywrite!(w, "@"); enc_ty(w, cx, typ); }
298 ty::ty_uniq(typ) => { mywrite!(w, "~"); enc_ty(w, cx, typ); }
299 ty::ty_ptr(mt) => { mywrite!(w, "*"); enc_mt(w, cx, mt); }
300 ty::ty_rptr(r, mt) => {
302 enc_region(w, cx, r);
305 ty::ty_vec(mt, v) => {
308 enc_vstore(w, cx, v);
312 enc_vstore(w, cx, v);
314 ty::ty_unboxed_vec(mt) => { mywrite!(w, "U"); enc_mt(w, cx, mt); }
315 ty::ty_closure(ref f) => {
317 enc_closure_ty(w, cx, *f);
319 ty::ty_bare_fn(ref f) => {
321 enc_bare_fn_ty(w, cx, f);
324 cx.diag.handler().bug("cannot encode inference variable types");
326 ty::ty_param(param_ty {idx: id, def_id: did}) => {
327 mywrite!(w, "p{}|{}", (cx.ds)(did), id);
329 ty::ty_self(did) => {
330 mywrite!(w, "s{}|", (cx.ds)(did));
332 ty::ty_struct(def, ref substs) => {
333 mywrite!(w, "a[{}|", (cx.ds)(def));
334 enc_substs(w, cx, substs);
337 ty::ty_err => fail!("shouldn't encode error type")
341 fn enc_sigil(w: &mut MemWriter, sigil: Sigil) {
343 ManagedSigil => mywrite!(w, "@"),
344 OwnedSigil => mywrite!(w, "~"),
345 BorrowedSigil => mywrite!(w, "&"),
349 fn enc_purity(w: &mut MemWriter, p: Purity) {
351 ImpureFn => mywrite!(w, "i"),
352 UnsafeFn => mywrite!(w, "u"),
353 ExternFn => mywrite!(w, "c")
357 fn enc_abi_set(w: &mut MemWriter, abis: AbiSet) {
360 mywrite!(w, "{},", abi.name());
366 fn enc_onceness(w: &mut MemWriter, o: Onceness) {
368 Once => mywrite!(w, "o"),
369 Many => mywrite!(w, "m")
373 pub fn enc_bare_fn_ty(w: &mut MemWriter, cx: &ctxt, ft: &ty::BareFnTy) {
374 enc_purity(w, ft.purity);
375 enc_abi_set(w, ft.abis);
376 enc_fn_sig(w, cx, &ft.sig);
379 fn enc_closure_ty(w: &mut MemWriter, cx: &ctxt, ft: &ty::ClosureTy) {
380 enc_sigil(w, ft.sigil);
381 enc_purity(w, ft.purity);
382 enc_onceness(w, ft.onceness);
383 enc_region(w, cx, ft.region);
384 let bounds = ty::ParamBounds {builtin_bounds: ft.bounds,
385 trait_bounds: Vec::new()};
386 enc_bounds(w, cx, &bounds);
387 enc_fn_sig(w, cx, &ft.sig);
390 fn enc_fn_sig(w: &mut MemWriter, cx: &ctxt, fsig: &ty::FnSig) {
391 mywrite!(w, "[{}|", fsig.binder_id);
392 for ty in fsig.inputs.iter() {
401 enc_ty(w, cx, fsig.output);
404 fn enc_bounds(w: &mut MemWriter, cx: &ctxt, bs: &ty::ParamBounds) {
405 for bound in bs.builtin_bounds.iter() {
407 ty::BoundSend => mywrite!(w, "S"),
408 ty::BoundFreeze => mywrite!(w, "K"),
409 ty::BoundStatic => mywrite!(w, "O"),
410 ty::BoundSized => mywrite!(w, "Z"),
411 ty::BoundPod => mywrite!(w, "P"),
412 ty::BoundShare => mywrite!(w, "T"),
416 for &tp in bs.trait_bounds.iter() {
418 enc_trait_ref(w, cx, tp);
424 pub fn enc_type_param_def(w: &mut MemWriter, cx: &ctxt, v: &ty::TypeParameterDef) {
425 mywrite!(w, "{}:{}|", token::get_ident(v.ident), (cx.ds)(v.def_id));
426 enc_bounds(w, cx, v.bounds);
427 enc_opt(w, v.default, |w, t| enc_ty(w, cx, t));