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;
31 use syntax::print::pprust::*;
33 macro_rules! mywrite( ($wr:expr, $($arg:tt)*) => (
34 format_args!(|a| { mywrite($wr, a) }, $($arg)*)
39 // Def -> str Callback:
40 ds: extern "Rust" fn(DefId) -> ~str,
46 // Compact string representation for ty.t values. API ty_str & parse_from_str.
47 // Extra parameters are for converting to/from def_ids in the string rep.
48 // Whatever format you choose should not contain pipe characters.
49 pub struct ty_abbrev {
55 pub enum abbrev_ctxt {
57 ac_use_abbrevs(@RefCell<HashMap<ty::t, ty_abbrev>>),
60 fn mywrite(w: &mut MemWriter, fmt: &fmt::Arguments) {
61 fmt::write(&mut *w as &mut io::Writer, fmt);
64 pub fn enc_ty(w: &mut MemWriter, cx: @ctxt, t: ty::t) {
69 let short_names_cache = cx.tcx.short_names_cache.borrow();
70 result_str_opt = short_names_cache.get()
76 let result_str = match result_str_opt {
79 let wr = &mut MemWriter::new();
80 enc_sty(wr, cx, &ty::get(t).sty);
81 let s = str::from_utf8(wr.get_ref()).unwrap();
82 let mut short_names_cache = cx.tcx
85 short_names_cache.get().insert(t, s.to_str());
89 w.write(result_str.as_bytes());
91 ac_use_abbrevs(abbrevs) => {
93 let mut abbrevs = abbrevs.borrow_mut();
94 match abbrevs.get().find(&t) {
95 Some(a) => { w.write(a.s.as_bytes()); return; }
99 let pos = w.tell().unwrap();
100 enc_sty(w, cx, &ty::get(t).sty);
101 let end = w.tell().unwrap();
103 fn estimate_sz(u: u64) -> u64 {
106 while n != 0 { len += 1; n = n >> 4; }
109 let abbrev_len = 3 + estimate_sz(pos) + estimate_sz(len);
110 if abbrev_len < len {
111 // I.e. it's actually an abbreviation.
112 let s = format!("\\#{:x}:{:x}\\#", pos, len);
113 let a = ty_abbrev { pos: pos as uint,
117 let mut abbrevs = abbrevs.borrow_mut();
118 abbrevs.get().insert(t, a);
126 fn enc_mutability(w: &mut MemWriter, mt: ast::Mutability) {
129 MutMutable => mywrite!(w, "m"),
133 fn enc_mt(w: &mut MemWriter, cx: @ctxt, mt: ty::mt) {
134 enc_mutability(w, mt.mutbl);
135 enc_ty(w, cx, mt.ty);
138 fn enc_opt<T>(w: &mut MemWriter, t: Option<T>, enc_f: |&mut MemWriter, T|) {
140 None => mywrite!(w, "n"),
148 pub fn enc_substs(w: &mut MemWriter, cx: @ctxt, substs: &ty::substs) {
149 enc_region_substs(w, cx, &substs.regions);
150 enc_opt(w, substs.self_ty, |w, t| enc_ty(w, cx, t));
152 for t in substs.tps.iter() { enc_ty(w, cx, *t); }
156 fn enc_region_substs(w: &mut MemWriter, cx: @ctxt, substs: &ty::RegionSubsts) {
158 ty::ErasedRegions => {
161 ty::NonerasedRegions(ref regions) => {
163 for &r in regions.iter() {
164 enc_region(w, cx, r);
171 fn enc_region(w: &mut MemWriter, cx: @ctxt, r: ty::Region) {
173 ty::ReLateBound(id, br) => {
174 mywrite!(w, "b[{}|", id);
175 enc_bound_region(w, cx, br);
178 ty::ReEarlyBound(node_id, index, ident) => {
179 mywrite!(w, "B[{}|{}|{}]",
182 token::get_name(ident));
184 ty::ReFree(ref fr) => {
185 mywrite!(w, "f[{}|", fr.scope_id);
186 enc_bound_region(w, cx, fr.bound_region);
189 ty::ReScope(nid) => {
190 mywrite!(w, "s{}|", nid);
199 // these should not crop up after typeck
200 cx.diag.handler().bug("cannot encode region variables");
205 fn enc_bound_region(w: &mut MemWriter, cx: @ctxt, br: ty::BoundRegion) {
208 mywrite!(w, "a{}|", idx);
210 ty::BrNamed(d, s) => {
211 mywrite!(w, "[{}|{}]",
216 mywrite!(w, "f{}|", id);
221 pub fn enc_vstore(w: &mut MemWriter, cx: @ctxt, v: ty::vstore) {
224 ty::vstore_fixed(u) => mywrite!(w, "{}|", u),
225 ty::vstore_uniq => mywrite!(w, "~"),
226 ty::vstore_slice(r) => {
228 enc_region(w, cx, r);
233 pub fn enc_trait_ref(w: &mut MemWriter, cx: @ctxt, s: &ty::TraitRef) {
234 mywrite!(w, "{}|", (cx.ds)(s.def_id));
235 enc_substs(w, cx, &s.substs);
238 pub fn enc_trait_store(w: &mut MemWriter, cx: @ctxt, s: ty::TraitStore) {
240 ty::UniqTraitStore => mywrite!(w, "~"),
241 ty::RegionTraitStore(re) => {
243 enc_region(w, cx, re);
248 fn enc_sty(w: &mut MemWriter, cx: @ctxt, st: &ty::sty) {
250 ty::ty_nil => mywrite!(w, "n"),
251 ty::ty_bot => mywrite!(w, "z"),
252 ty::ty_bool => mywrite!(w, "b"),
253 ty::ty_char => mywrite!(w, "c"),
256 TyI => mywrite!(w, "i"),
257 TyI8 => mywrite!(w, "MB"),
258 TyI16 => mywrite!(w, "MW"),
259 TyI32 => mywrite!(w, "ML"),
260 TyI64 => mywrite!(w, "MD")
265 TyU => mywrite!(w, "u"),
266 TyU8 => mywrite!(w, "Mb"),
267 TyU16 => mywrite!(w, "Mw"),
268 TyU32 => mywrite!(w, "Ml"),
269 TyU64 => mywrite!(w, "Md")
274 TyF32 => mywrite!(w, "Mf"),
275 TyF64 => mywrite!(w, "MF"),
278 ty::ty_enum(def, ref substs) => {
279 mywrite!(w, "t[{}|", (cx.ds)(def));
280 enc_substs(w, cx, substs);
283 ty::ty_trait(def, ref substs, store, mt, bounds) => {
284 mywrite!(w, "x[{}|", (cx.ds)(def));
285 enc_substs(w, cx, substs);
286 enc_trait_store(w, cx, store);
287 enc_mutability(w, mt);
288 let bounds = ty::ParamBounds {builtin_bounds: bounds,
289 trait_bounds: Vec::new()};
290 enc_bounds(w, cx, &bounds);
293 ty::ty_tup(ref ts) => {
295 for t in ts.iter() { enc_ty(w, cx, *t); }
298 ty::ty_box(typ) => { mywrite!(w, "@"); enc_ty(w, cx, typ); }
299 ty::ty_uniq(typ) => { mywrite!(w, "~"); enc_ty(w, cx, typ); }
300 ty::ty_ptr(mt) => { mywrite!(w, "*"); enc_mt(w, cx, mt); }
301 ty::ty_rptr(r, mt) => {
303 enc_region(w, cx, r);
306 ty::ty_vec(mt, v) => {
309 enc_vstore(w, cx, v);
313 enc_vstore(w, cx, v);
315 ty::ty_unboxed_vec(mt) => { mywrite!(w, "U"); enc_mt(w, cx, mt); }
316 ty::ty_closure(ref f) => {
318 enc_closure_ty(w, cx, f);
320 ty::ty_bare_fn(ref f) => {
322 enc_bare_fn_ty(w, cx, f);
325 cx.diag.handler().bug("cannot encode inference variable types");
327 ty::ty_param(param_ty {idx: id, def_id: did}) => {
328 mywrite!(w, "p{}|{}", (cx.ds)(did), id);
330 ty::ty_self(did) => {
331 mywrite!(w, "s{}|", (cx.ds)(did));
333 ty::ty_struct(def, ref substs) => {
334 mywrite!(w, "a[{}|", (cx.ds)(def));
335 enc_substs(w, cx, substs);
338 ty::ty_err => fail!("shouldn't encode error type")
342 fn enc_sigil(w: &mut MemWriter, sigil: Sigil) {
344 ManagedSigil => mywrite!(w, "@"),
345 OwnedSigil => mywrite!(w, "~"),
346 BorrowedSigil => mywrite!(w, "&"),
350 fn enc_purity(w: &mut MemWriter, p: Purity) {
352 ImpureFn => mywrite!(w, "i"),
353 UnsafeFn => mywrite!(w, "u"),
354 ExternFn => mywrite!(w, "c")
358 fn enc_abi_set(w: &mut MemWriter, abis: AbiSet) {
361 mywrite!(w, "{},", abi.name());
367 fn enc_onceness(w: &mut MemWriter, o: Onceness) {
369 Once => mywrite!(w, "o"),
370 Many => mywrite!(w, "m")
374 pub fn enc_bare_fn_ty(w: &mut MemWriter, cx: @ctxt, ft: &ty::BareFnTy) {
375 enc_purity(w, ft.purity);
376 enc_abi_set(w, ft.abis);
377 enc_fn_sig(w, cx, &ft.sig);
380 fn enc_closure_ty(w: &mut MemWriter, cx: @ctxt, ft: &ty::ClosureTy) {
381 enc_sigil(w, ft.sigil);
382 enc_purity(w, ft.purity);
383 enc_onceness(w, ft.onceness);
384 enc_region(w, cx, ft.region);
385 let bounds = ty::ParamBounds {builtin_bounds: ft.bounds,
386 trait_bounds: Vec::new()};
387 enc_bounds(w, cx, &bounds);
388 enc_fn_sig(w, cx, &ft.sig);
391 fn enc_fn_sig(w: &mut MemWriter, cx: @ctxt, fsig: &ty::FnSig) {
392 mywrite!(w, "[{}|", fsig.binder_id);
393 for ty in fsig.inputs.iter() {
402 enc_ty(w, cx, fsig.output);
405 fn enc_bounds(w: &mut MemWriter, cx: @ctxt, bs: &ty::ParamBounds) {
406 for bound in bs.builtin_bounds.iter() {
408 ty::BoundSend => mywrite!(w, "S"),
409 ty::BoundFreeze => mywrite!(w, "K"),
410 ty::BoundStatic => mywrite!(w, "O"),
411 ty::BoundSized => mywrite!(w, "Z"),
412 ty::BoundPod => mywrite!(w, "P"),
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));