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 std::collections::HashMap;
18 use std::io::MemWriter;
21 use middle::ty::param_ty;
27 use syntax::diagnostic::SpanHandler;
28 use syntax::parse::token;
30 macro_rules! mywrite( ($($arg:tt)*) => ({ write!($($arg)*); }) )
33 pub diag: &'a SpanHandler,
34 // Def -> str Callback:
35 pub ds: fn(DefId) -> String,
37 pub tcx: &'a ty::ctxt,
38 pub abbrevs: &'a abbrev_map
41 // Compact string representation for ty.t values. API ty_str & parse_from_str.
42 // Extra parameters are for converting to/from def_ids in the string rep.
43 // Whatever format you choose should not contain pipe characters.
44 pub struct ty_abbrev {
48 pub type abbrev_map = RefCell<HashMap<ty::t, ty_abbrev>>;
50 pub fn enc_ty(w: &mut MemWriter, cx: &ctxt, t: ty::t) {
51 match cx.abbrevs.borrow_mut().find(&t) {
52 Some(a) => { w.write(a.s.as_bytes()); return; }
55 let pos = w.tell().unwrap();
56 enc_sty(w, cx, &ty::get(t).sty);
57 let end = w.tell().unwrap();
59 fn estimate_sz(u: u64) -> u64 {
62 while n != 0 { len += 1; n = n >> 4; }
65 let abbrev_len = 3 + estimate_sz(pos) + estimate_sz(len);
67 // I.e. it's actually an abbreviation.
68 cx.abbrevs.borrow_mut().insert(t, ty_abbrev {
69 s: format!("\\#{:x}:{:x}\\#", pos, len)
74 fn enc_mutability(w: &mut MemWriter, mt: ast::Mutability) {
77 MutMutable => mywrite!(w, "m"),
81 fn enc_mt(w: &mut MemWriter, cx: &ctxt, mt: ty::mt) {
82 enc_mutability(w, mt.mutbl);
86 fn enc_opt<T>(w: &mut MemWriter, t: Option<T>, enc_f: |&mut MemWriter, T|) {
88 None => mywrite!(w, "n"),
96 pub fn enc_substs(w: &mut MemWriter, cx: &ctxt, substs: &subst::Substs) {
97 enc_region_substs(w, cx, &substs.regions);
98 enc_opt(w, substs.self_ty, |w, t| enc_ty(w, cx, t));
100 for t in substs.tps.iter() { enc_ty(w, cx, *t); }
104 fn enc_region_substs(w: &mut MemWriter, cx: &ctxt, substs: &subst::RegionSubsts) {
106 subst::ErasedRegions => {
109 subst::NonerasedRegions(ref regions) => {
111 for &r in regions.iter() {
112 enc_region(w, cx, r);
119 fn enc_region(w: &mut MemWriter, cx: &ctxt, r: ty::Region) {
121 ty::ReLateBound(id, br) => {
122 mywrite!(w, "b[{}|", id);
123 enc_bound_region(w, cx, br);
126 ty::ReEarlyBound(node_id, index, name) => {
127 mywrite!(w, "B[{}|{}|{}]",
130 token::get_name(name));
132 ty::ReFree(ref fr) => {
133 mywrite!(w, "f[{}|", fr.scope_id);
134 enc_bound_region(w, cx, fr.bound_region);
137 ty::ReScope(nid) => {
138 mywrite!(w, "s{}|", nid);
147 // these should not crop up after typeck
148 cx.diag.handler().bug("cannot encode region variables");
153 fn enc_bound_region(w: &mut MemWriter, cx: &ctxt, br: ty::BoundRegion) {
156 mywrite!(w, "a{}|", idx);
158 ty::BrNamed(d, name) => {
159 mywrite!(w, "[{}|{}]",
161 token::get_name(name));
164 mywrite!(w, "f{}|", id);
169 pub fn enc_trait_ref(w: &mut MemWriter, cx: &ctxt, s: &ty::TraitRef) {
170 mywrite!(w, "{}|", (cx.ds)(s.def_id));
171 enc_substs(w, cx, &s.substs);
174 pub fn enc_trait_store(w: &mut MemWriter, cx: &ctxt, s: ty::TraitStore) {
176 ty::UniqTraitStore => mywrite!(w, "~"),
177 ty::RegionTraitStore(re, m) => {
179 enc_region(w, cx, re);
180 enc_mutability(w, m);
185 fn enc_sty(w: &mut MemWriter, cx: &ctxt, st: &ty::sty) {
187 ty::ty_nil => mywrite!(w, "n"),
188 ty::ty_bot => mywrite!(w, "z"),
189 ty::ty_bool => mywrite!(w, "b"),
190 ty::ty_char => mywrite!(w, "c"),
193 TyI => mywrite!(w, "i"),
194 TyI8 => mywrite!(w, "MB"),
195 TyI16 => mywrite!(w, "MW"),
196 TyI32 => mywrite!(w, "ML"),
197 TyI64 => mywrite!(w, "MD")
202 TyU => mywrite!(w, "u"),
203 TyU8 => mywrite!(w, "Mb"),
204 TyU16 => mywrite!(w, "Mw"),
205 TyU32 => mywrite!(w, "Ml"),
206 TyU64 => mywrite!(w, "Md")
211 TyF32 => mywrite!(w, "Mf"),
212 TyF64 => mywrite!(w, "MF"),
213 TyF128 => mywrite!(w, "MQ")
216 ty::ty_enum(def, ref substs) => {
217 mywrite!(w, "t[{}|", (cx.ds)(def));
218 enc_substs(w, cx, substs);
221 ty::ty_trait(box ty::TyTrait {
227 mywrite!(w, "x[{}|", (cx.ds)(def_id));
228 enc_substs(w, cx, substs);
229 enc_trait_store(w, cx, store);
230 let bounds = ty::ParamBounds {builtin_bounds: bounds,
231 trait_bounds: Vec::new()};
232 enc_bounds(w, cx, &bounds);
235 ty::ty_tup(ref ts) => {
237 for t in ts.iter() { enc_ty(w, cx, *t); }
240 ty::ty_box(typ) => { mywrite!(w, "@"); enc_ty(w, cx, typ); }
241 ty::ty_uniq(typ) => { mywrite!(w, "~"); enc_ty(w, cx, typ); }
242 ty::ty_ptr(mt) => { mywrite!(w, "*"); enc_mt(w, cx, mt); }
243 ty::ty_rptr(r, mt) => {
245 enc_region(w, cx, r);
248 ty::ty_vec(mt, sz) => {
253 Some(n) => mywrite!(w, "{}|", n),
254 None => mywrite!(w, "|"),
260 ty::ty_closure(ref f) => {
262 enc_closure_ty(w, cx, *f);
264 ty::ty_bare_fn(ref f) => {
266 enc_bare_fn_ty(w, cx, f);
269 cx.diag.handler().bug("cannot encode inference variable types");
271 ty::ty_param(param_ty {idx: id, def_id: did}) => {
272 mywrite!(w, "p{}|{}", (cx.ds)(did), id);
274 ty::ty_self(did) => {
275 mywrite!(w, "s{}|", (cx.ds)(did));
277 ty::ty_struct(def, ref substs) => {
278 mywrite!(w, "a[{}|", (cx.ds)(def));
279 enc_substs(w, cx, substs);
282 ty::ty_err => fail!("shouldn't encode error type")
286 fn enc_fn_style(w: &mut MemWriter, p: FnStyle) {
288 NormalFn => mywrite!(w, "n"),
289 UnsafeFn => mywrite!(w, "u"),
293 fn enc_abi(w: &mut MemWriter, abi: Abi) {
295 mywrite!(w, "{}", abi.name());
299 fn enc_onceness(w: &mut MemWriter, o: Onceness) {
301 Once => mywrite!(w, "o"),
302 Many => mywrite!(w, "m")
306 pub fn enc_bare_fn_ty(w: &mut MemWriter, cx: &ctxt, ft: &ty::BareFnTy) {
307 enc_fn_style(w, ft.fn_style);
309 enc_fn_sig(w, cx, &ft.sig);
312 fn enc_closure_ty(w: &mut MemWriter, cx: &ctxt, ft: &ty::ClosureTy) {
313 enc_fn_style(w, ft.fn_style);
314 enc_onceness(w, ft.onceness);
315 enc_trait_store(w, cx, ft.store);
316 let bounds = ty::ParamBounds {builtin_bounds: ft.bounds,
317 trait_bounds: Vec::new()};
318 enc_bounds(w, cx, &bounds);
319 enc_fn_sig(w, cx, &ft.sig);
322 fn enc_fn_sig(w: &mut MemWriter, cx: &ctxt, fsig: &ty::FnSig) {
323 mywrite!(w, "[{}|", fsig.binder_id);
324 for ty in fsig.inputs.iter() {
333 enc_ty(w, cx, fsig.output);
336 fn enc_bounds(w: &mut MemWriter, cx: &ctxt, bs: &ty::ParamBounds) {
337 for bound in bs.builtin_bounds.iter() {
339 ty::BoundSend => mywrite!(w, "S"),
340 ty::BoundStatic => mywrite!(w, "O"),
341 ty::BoundSized => mywrite!(w, "Z"),
342 ty::BoundCopy => mywrite!(w, "P"),
343 ty::BoundShare => mywrite!(w, "T"),
347 for tp in bs.trait_bounds.iter() {
349 enc_trait_ref(w, cx, &**tp);
355 pub fn enc_type_param_def(w: &mut MemWriter, cx: &ctxt, v: &ty::TypeParameterDef) {
356 mywrite!(w, "{}:{}|", token::get_ident(v.ident), (cx.ds)(v.def_id));
357 enc_bounds(w, cx, &*v.bounds);
358 enc_opt(w, v.default, |w, t| enc_ty(w, cx, t));