1 // Copyright 2012 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 use std::cell::RefCell;
14 use std::hashmap::HashMap;
16 use std::io::MemWriter;
20 use middle::ty::param_ty;
23 use syntax::abi::AbiSet;
26 use syntax::diagnostic::SpanHandler;
27 use syntax::print::pprust::*;
29 macro_rules! mywrite( ($wr:expr, $($arg:tt)*) => (
30 format_args!(|a| { mywrite($wr, a) }, $($arg)*)
35 // Def -> str Callback:
36 ds: extern "Rust" fn(DefId) -> ~str,
42 // Compact string representation for ty.t values. API ty_str & parse_from_str.
43 // Extra parameters are for converting to/from def_ids in the string rep.
44 // Whatever format you choose should not contain pipe characters.
45 pub struct ty_abbrev {
51 pub enum abbrev_ctxt {
53 ac_use_abbrevs(@RefCell<HashMap<ty::t, ty_abbrev>>),
56 fn mywrite(w: &mut MemWriter, fmt: &fmt::Arguments) {
57 fmt::write(&mut *w as &mut io::Writer, fmt);
60 pub fn enc_ty(w: &mut MemWriter, cx: @ctxt, t: ty::t) {
65 let short_names_cache = cx.tcx.short_names_cache.borrow();
66 result_str_opt = short_names_cache.get()
68 .map(|result| *result);
70 let result_str = match result_str_opt {
73 let wr = &mut MemWriter::new();
74 enc_sty(wr, cx, &ty::get(t).sty);
75 let s = str::from_utf8(wr.get_ref()).to_managed();
76 let mut short_names_cache = cx.tcx
79 short_names_cache.get().insert(t, s);
83 w.write(result_str.as_bytes());
85 ac_use_abbrevs(abbrevs) => {
87 let mut abbrevs = abbrevs.borrow_mut();
88 match abbrevs.get().find(&t) {
89 Some(a) => { w.write(a.s.as_bytes()); return; }
94 enc_sty(w, cx, &ty::get(t).sty);
97 fn estimate_sz(u: u64) -> u64 {
100 while n != 0 { len += 1; n = n >> 4; }
103 let abbrev_len = 3 + estimate_sz(pos) + estimate_sz(len);
104 if abbrev_len < len {
105 // I.e. it's actually an abbreviation.
106 let s = format!("\\#{:x}:{:x}\\#", pos, len).to_managed();
107 let a = ty_abbrev { pos: pos as uint,
111 let mut abbrevs = abbrevs.borrow_mut();
112 abbrevs.get().insert(t, a);
120 fn enc_mutability(w: &mut MemWriter, mt: ast::Mutability) {
123 MutMutable => mywrite!(w, "m"),
127 fn enc_mt(w: &mut MemWriter, cx: @ctxt, mt: ty::mt) {
128 enc_mutability(w, mt.mutbl);
129 enc_ty(w, cx, mt.ty);
132 fn enc_opt<T>(w: &mut MemWriter, t: Option<T>, enc_f: |&mut MemWriter, T|) {
134 None => mywrite!(w, "n"),
142 pub fn enc_substs(w: &mut MemWriter, cx: @ctxt, substs: &ty::substs) {
143 enc_region_substs(w, cx, &substs.regions);
144 enc_opt(w, substs.self_ty, |w, t| enc_ty(w, cx, t));
146 for t in substs.tps.iter() { enc_ty(w, cx, *t); }
150 fn enc_region_substs(w: &mut MemWriter, cx: @ctxt, substs: &ty::RegionSubsts) {
152 ty::ErasedRegions => {
155 ty::NonerasedRegions(ref regions) => {
157 for &r in regions.iter() {
158 enc_region(w, cx, r);
165 fn enc_region(w: &mut MemWriter, cx: @ctxt, r: ty::Region) {
167 ty::ReLateBound(id, br) => {
168 mywrite!(w, "b[{}|", id);
169 enc_bound_region(w, cx, br);
172 ty::ReEarlyBound(node_id, index, ident) => {
173 mywrite!(w, "B[{}|{}|{}]",
176 cx.tcx.sess.str_of(ident));
178 ty::ReFree(ref fr) => {
179 mywrite!(w, "f[{}|", fr.scope_id);
180 enc_bound_region(w, cx, fr.bound_region);
183 ty::ReScope(nid) => {
184 mywrite!(w, "s{}|", nid);
193 // these should not crop up after typeck
194 cx.diag.handler().bug("Cannot encode region variables");
199 fn enc_bound_region(w: &mut MemWriter, cx: @ctxt, br: ty::BoundRegion) {
202 mywrite!(w, "a{}|", idx);
204 ty::BrNamed(d, s) => {
205 mywrite!(w, "[{}|{}]",
207 cx.tcx.sess.str_of(s));
210 mywrite!(w, "f{}|", id);
215 pub fn enc_vstore(w: &mut MemWriter, cx: @ctxt, v: ty::vstore) {
218 ty::vstore_fixed(u) => mywrite!(w, "{}|", u),
219 ty::vstore_uniq => mywrite!(w, "~"),
220 ty::vstore_box => mywrite!(w, "@"),
221 ty::vstore_slice(r) => {
223 enc_region(w, cx, r);
228 pub fn enc_trait_ref(w: &mut MemWriter, cx: @ctxt, s: &ty::TraitRef) {
229 mywrite!(w, "{}|", (cx.ds)(s.def_id));
230 enc_substs(w, cx, &s.substs);
233 pub fn enc_trait_store(w: &mut MemWriter, cx: @ctxt, s: ty::TraitStore) {
235 ty::UniqTraitStore => mywrite!(w, "~"),
236 ty::BoxTraitStore => mywrite!(w, "@"),
237 ty::RegionTraitStore(re) => {
239 enc_region(w, cx, re);
244 fn enc_sty(w: &mut MemWriter, cx: @ctxt, st: &ty::sty) {
246 ty::ty_nil => mywrite!(w, "n"),
247 ty::ty_bot => mywrite!(w, "z"),
248 ty::ty_bool => mywrite!(w, "b"),
249 ty::ty_char => mywrite!(w, "c"),
252 TyI => mywrite!(w, "i"),
253 TyI8 => mywrite!(w, "MB"),
254 TyI16 => mywrite!(w, "MW"),
255 TyI32 => mywrite!(w, "ML"),
256 TyI64 => mywrite!(w, "MD")
261 TyU => mywrite!(w, "u"),
262 TyU8 => mywrite!(w, "Mb"),
263 TyU16 => mywrite!(w, "Mw"),
264 TyU32 => mywrite!(w, "Ml"),
265 TyU64 => mywrite!(w, "Md")
270 TyF32 => mywrite!(w, "Mf"),
271 TyF64 => mywrite!(w, "MF"),
274 ty::ty_enum(def, ref substs) => {
275 mywrite!(w, "t[{}|", (cx.ds)(def));
276 enc_substs(w, cx, substs);
279 ty::ty_trait(def, ref substs, store, mt, bounds) => {
280 mywrite!(w, "x[{}|", (cx.ds)(def));
281 enc_substs(w, cx, substs);
282 enc_trait_store(w, cx, store);
283 enc_mutability(w, mt);
284 let bounds = ty::ParamBounds {builtin_bounds: bounds,
286 enc_bounds(w, cx, &bounds);
289 ty::ty_tup(ref ts) => {
291 for t in ts.iter() { enc_ty(w, cx, *t); }
294 ty::ty_box(typ) => { mywrite!(w, "@"); enc_ty(w, cx, typ); }
295 ty::ty_uniq(typ) => { mywrite!(w, "~"); enc_ty(w, cx, typ); }
296 ty::ty_ptr(mt) => { mywrite!(w, "*"); enc_mt(w, cx, mt); }
297 ty::ty_rptr(r, mt) => {
299 enc_region(w, cx, r);
302 ty::ty_vec(mt, v) => {
305 enc_vstore(w, cx, v);
309 enc_vstore(w, cx, v);
311 ty::ty_unboxed_vec(mt) => { mywrite!(w, "U"); enc_mt(w, cx, mt); }
312 ty::ty_closure(ref f) => {
314 enc_closure_ty(w, cx, f);
316 ty::ty_bare_fn(ref f) => {
318 enc_bare_fn_ty(w, cx, f);
321 cx.diag.handler().bug("Cannot encode inference variable types");
323 ty::ty_param(param_ty {idx: id, def_id: did}) => {
324 mywrite!(w, "p{}|{}", (cx.ds)(did), id);
326 ty::ty_self(did) => {
327 mywrite!(w, "s{}|", (cx.ds)(did));
329 ty::ty_type => mywrite!(w, "Y"),
330 ty::ty_opaque_closure_ptr(p) => {
334 ty::ty_struct(def, ref substs) => {
335 mywrite!(w, "a[{}|", (cx.ds)(def));
336 enc_substs(w, cx, substs);
339 ty::ty_err => fail!("Shouldn't encode error type")
343 fn enc_sigil(w: &mut MemWriter, sigil: Sigil) {
345 ManagedSigil => mywrite!(w, "@"),
346 OwnedSigil => mywrite!(w, "~"),
347 BorrowedSigil => mywrite!(w, "&"),
351 fn enc_purity(w: &mut MemWriter, p: Purity) {
353 ImpureFn => mywrite!(w, "i"),
354 UnsafeFn => mywrite!(w, "u"),
355 ExternFn => mywrite!(w, "c")
359 fn enc_abi_set(w: &mut MemWriter, abis: AbiSet) {
362 mywrite!(w, "{},", abi.name());
368 fn enc_onceness(w: &mut MemWriter, o: Onceness) {
370 Once => mywrite!(w, "o"),
371 Many => mywrite!(w, "m")
375 pub fn enc_bare_fn_ty(w: &mut MemWriter, cx: @ctxt, ft: &ty::BareFnTy) {
376 enc_purity(w, ft.purity);
377 enc_abi_set(w, ft.abis);
378 enc_fn_sig(w, cx, &ft.sig);
381 fn enc_closure_ty(w: &mut MemWriter, cx: @ctxt, ft: &ty::ClosureTy) {
382 enc_sigil(w, ft.sigil);
383 enc_purity(w, ft.purity);
384 enc_onceness(w, ft.onceness);
385 enc_region(w, cx, ft.region);
386 let bounds = ty::ParamBounds {builtin_bounds: ft.bounds,
388 enc_bounds(w, cx, &bounds);
389 enc_fn_sig(w, cx, &ft.sig);
392 fn enc_fn_sig(w: &mut MemWriter, cx: @ctxt, fsig: &ty::FnSig) {
393 mywrite!(w, "[{}|", fsig.binder_id);
394 for ty in fsig.inputs.iter() {
403 enc_ty(w, cx, fsig.output);
406 fn enc_bounds(w: &mut MemWriter, cx: @ctxt, bs: &ty::ParamBounds) {
407 for bound in bs.builtin_bounds.iter() {
409 ty::BoundSend => mywrite!(w, "S"),
410 ty::BoundFreeze => mywrite!(w, "K"),
411 ty::BoundStatic => mywrite!(w, "O"),
412 ty::BoundSized => mywrite!(w, "Z"),
413 ty::BoundPod => mywrite!(w, "P"),
417 for &tp in bs.trait_bounds.iter() {
419 enc_trait_ref(w, cx, tp);
425 pub fn enc_type_param_def(w: &mut MemWriter, cx: @ctxt, v: &ty::TypeParameterDef) {
426 mywrite!(w, "{}:{}|", cx.tcx.sess.str_of(v.ident), (cx.ds)(v.def_id));
427 enc_bounds(w, cx, v.bounds);