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>>;
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 {
70 s: format!("\\#{:x}:{:x}\\#", pos, len)
75 pub fn enc_ty(w: &mut MemWriter, cx: &ctxt, t: ty::t) {
76 match cx.abbrevs.borrow_mut().find(&t) {
77 Some(a) => { w.write(a.s.as_bytes()); return; }
80 let pos = w.tell().unwrap();
81 enc_sty(w, cx, &ty::get(t).sty);
82 let end = w.tell().unwrap();
84 fn estimate_sz(u: u64) -> u64 {
87 while n != 0 { len += 1; n = n >> 4; }
90 let abbrev_len = 3 + estimate_sz(pos) + estimate_sz(len);
92 // I.e. it's actually an abbreviation.
93 cx.abbrevs.borrow_mut().insert(t, ty_abbrev {
94 s: format!("#{:x}:{:x}#", pos, len)
99 fn enc_mutability(w: &mut MemWriter, mt: ast::Mutability) {
102 MutMutable => mywrite!(w, "m"),
106 fn enc_mt(w: &mut MemWriter, cx: &ctxt, mt: ty::mt) {
107 enc_mutability(w, mt.mutbl);
108 enc_ty(w, cx, mt.ty);
111 fn enc_opt<T>(w: &mut MemWriter, t: Option<T>, enc_f: |&mut MemWriter, T|) {
113 None => mywrite!(w, "n"),
121 pub fn enc_substs(w: &mut MemWriter, cx: &ctxt, substs: &subst::Substs) {
122 enc_region_substs(w, cx, &substs.regions);
123 enc_opt(w, substs.self_ty, |w, t| enc_ty(w, cx, t));
125 for t in substs.tps.iter() { enc_ty(w, cx, *t); }
129 fn enc_region_substs(w: &mut MemWriter, cx: &ctxt, substs: &subst::RegionSubsts) {
131 subst::ErasedRegions => {
134 subst::NonerasedRegions(ref regions) => {
136 for &r in regions.iter() {
137 enc_region(w, cx, r);
144 fn enc_region(w: &mut MemWriter, cx: &ctxt, r: ty::Region) {
146 ty::ReLateBound(id, br) => {
147 mywrite!(w, "b[{}|", id);
148 enc_bound_region(w, cx, br);
151 ty::ReEarlyBound(node_id, index, name) => {
152 mywrite!(w, "B[{}|{}|{}]",
155 token::get_name(name));
157 ty::ReFree(ref fr) => {
158 mywrite!(w, "f[{}|", fr.scope_id);
159 enc_bound_region(w, cx, fr.bound_region);
162 ty::ReScope(nid) => {
163 mywrite!(w, "s{}|", nid);
172 // these should not crop up after typeck
173 cx.diag.handler().bug("cannot encode region variables");
178 fn enc_bound_region(w: &mut MemWriter, cx: &ctxt, br: ty::BoundRegion) {
181 mywrite!(w, "a{}|", idx);
183 ty::BrNamed(d, name) => {
184 mywrite!(w, "[{}|{}]",
186 token::get_name(name));
189 mywrite!(w, "f{}|", id);
194 pub fn enc_trait_ref(w: &mut MemWriter, cx: &ctxt, s: &ty::TraitRef) {
195 mywrite!(w, "{}|", (cx.ds)(s.def_id));
196 enc_substs(w, cx, &s.substs);
199 pub fn enc_trait_store(w: &mut MemWriter, cx: &ctxt, s: ty::TraitStore) {
201 ty::UniqTraitStore => mywrite!(w, "~"),
202 ty::RegionTraitStore(re, m) => {
204 enc_region(w, cx, re);
205 enc_mutability(w, m);
210 fn enc_sty(w: &mut MemWriter, cx: &ctxt, st: &ty::sty) {
212 ty::ty_nil => mywrite!(w, "n"),
213 ty::ty_bot => mywrite!(w, "z"),
214 ty::ty_bool => mywrite!(w, "b"),
215 ty::ty_char => mywrite!(w, "c"),
218 TyI => mywrite!(w, "i"),
219 TyI8 => mywrite!(w, "MB"),
220 TyI16 => mywrite!(w, "MW"),
221 TyI32 => mywrite!(w, "ML"),
222 TyI64 => mywrite!(w, "MD")
227 TyU => mywrite!(w, "u"),
228 TyU8 => mywrite!(w, "Mb"),
229 TyU16 => mywrite!(w, "Mw"),
230 TyU32 => mywrite!(w, "Ml"),
231 TyU64 => mywrite!(w, "Md")
236 TyF32 => mywrite!(w, "Mf"),
237 TyF64 => mywrite!(w, "MF"),
238 TyF128 => mywrite!(w, "MQ")
241 ty::ty_enum(def, ref substs) => {
242 mywrite!(w, "t[{}|", (cx.ds)(def));
243 enc_substs(w, cx, substs);
246 ty::ty_trait(box ty::TyTrait {
252 mywrite!(w, "x[{}|", (cx.ds)(def_id));
253 enc_substs(w, cx, substs);
254 enc_trait_store(w, cx, store);
255 let bounds = ty::ParamBounds {builtin_bounds: bounds,
256 trait_bounds: Vec::new()};
257 enc_bounds(w, cx, &bounds);
260 ty::ty_tup(ref ts) => {
262 for t in ts.iter() { enc_ty(w, cx, *t); }
265 ty::ty_box(typ) => { mywrite!(w, "@"); enc_ty(w, cx, typ); }
266 ty::ty_uniq(typ) => { mywrite!(w, "~"); enc_ty(w, cx, typ); }
267 ty::ty_ptr(mt) => { mywrite!(w, "*"); enc_mt(w, cx, mt); }
268 ty::ty_rptr(r, mt) => {
270 enc_region(w, cx, r);
273 ty::ty_vec(mt, sz) => {
278 Some(n) => mywrite!(w, "{}|", n),
279 None => mywrite!(w, "|"),
285 ty::ty_closure(ref f) => {
287 enc_closure_ty(w, cx, *f);
289 ty::ty_bare_fn(ref f) => {
291 enc_bare_fn_ty(w, cx, f);
294 cx.diag.handler().bug("cannot encode inference variable types");
296 ty::ty_param(param_ty {idx: id, def_id: did}) => {
297 mywrite!(w, "p{}|{}", (cx.ds)(did), id);
299 ty::ty_self(did) => {
300 mywrite!(w, "s{}|", (cx.ds)(did));
302 ty::ty_struct(def, ref substs) => {
303 mywrite!(w, "a[{}|", (cx.ds)(def));
304 enc_substs(w, cx, substs);
307 ty::ty_err => fail!("shouldn't encode error type")
311 fn enc_fn_style(w: &mut MemWriter, p: FnStyle) {
313 NormalFn => mywrite!(w, "n"),
314 UnsafeFn => mywrite!(w, "u"),
318 fn enc_abi(w: &mut MemWriter, abi: Abi) {
320 mywrite!(w, "{}", abi.name());
324 fn enc_onceness(w: &mut MemWriter, o: Onceness) {
326 Once => mywrite!(w, "o"),
327 Many => mywrite!(w, "m")
331 pub fn enc_bare_fn_ty(w: &mut MemWriter, cx: &ctxt, ft: &ty::BareFnTy) {
332 enc_fn_style(w, ft.fn_style);
334 enc_fn_sig(w, cx, &ft.sig);
337 fn enc_closure_ty(w: &mut MemWriter, cx: &ctxt, ft: &ty::ClosureTy) {
338 enc_fn_style(w, ft.fn_style);
339 enc_onceness(w, ft.onceness);
340 enc_trait_store(w, cx, ft.store);
341 let bounds = ty::ParamBounds {builtin_bounds: ft.bounds,
342 trait_bounds: Vec::new()};
343 enc_bounds(w, cx, &bounds);
344 enc_fn_sig(w, cx, &ft.sig);
347 fn enc_fn_sig(w: &mut MemWriter, cx: &ctxt, fsig: &ty::FnSig) {
348 mywrite!(w, "[{}|", fsig.binder_id);
349 for ty in fsig.inputs.iter() {
358 enc_ty(w, cx, fsig.output);
361 fn enc_bounds(w: &mut MemWriter, cx: &ctxt, bs: &ty::ParamBounds) {
362 for bound in bs.builtin_bounds.iter() {
364 ty::BoundSend => mywrite!(w, "S"),
365 ty::BoundStatic => mywrite!(w, "O"),
366 ty::BoundSized => mywrite!(w, "Z"),
367 ty::BoundCopy => mywrite!(w, "P"),
368 ty::BoundShare => mywrite!(w, "T"),
372 for tp in bs.trait_bounds.iter() {
374 enc_trait_ref(w, cx, &**tp);
380 pub fn enc_type_param_def(w: &mut MemWriter, cx: &ctxt, v: &ty::TypeParameterDef) {
381 mywrite!(w, "{}:{}|", token::get_ident(v.ident), (cx.ds)(v.def_id));
382 enc_bounds(w, cx, &*v.bounds);
383 enc_opt(w, v.default, |w, t| enc_ty(w, cx, t));