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::subst::VecPerParamSpace;
22 use middle::ty::ParamTy;
28 use syntax::diagnostic::SpanHandler;
29 use syntax::parse::token;
31 macro_rules! mywrite( ($($arg:tt)*) => ({ write!($($arg)*); }) )
34 pub diag: &'a SpanHandler,
35 // Def -> str Callback:
36 pub ds: fn(DefId) -> String,
38 pub tcx: &'a ty::ctxt,
39 pub abbrevs: &'a abbrev_map
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 {
49 pub type abbrev_map = RefCell<HashMap<ty::t, ty_abbrev>>;
52 pub fn enc_ty(w: &mut MemWriter, cx: &ctxt, t: ty::t) {
53 match cx.abbrevs.borrow_mut().find(&t) {
54 Some(a) => { w.write(a.s.as_bytes()); return; }
57 let pos = w.tell().unwrap();
58 enc_sty(w, cx, &ty::get(t).sty);
59 let end = w.tell().unwrap();
61 fn estimate_sz(u: u64) -> u64 {
64 while n != 0 { len += 1; n = n >> 4; }
67 let abbrev_len = 3 + estimate_sz(pos) + estimate_sz(len);
69 // I.e. it's actually an abbreviation.
70 cx.abbrevs.borrow_mut().insert(t, ty_abbrev {
71 s: format!("\\#{:x}:{:x}\\#", pos, len)
76 pub fn enc_ty(w: &mut MemWriter, cx: &ctxt, t: ty::t) {
77 match cx.abbrevs.borrow_mut().find(&t) {
78 Some(a) => { w.write(a.s.as_bytes()); return; }
81 let pos = w.tell().unwrap();
82 enc_sty(w, cx, &ty::get(t).sty);
83 let end = w.tell().unwrap();
85 fn estimate_sz(u: u64) -> u64 {
88 while n != 0 { len += 1; n = n >> 4; }
91 let abbrev_len = 3 + estimate_sz(pos) + estimate_sz(len);
93 // I.e. it's actually an abbreviation.
94 cx.abbrevs.borrow_mut().insert(t, ty_abbrev {
95 s: format!("#{:x}:{:x}#", pos, len)
100 fn enc_mutability(w: &mut MemWriter, mt: ast::Mutability) {
103 MutMutable => mywrite!(w, "m"),
107 fn enc_mt(w: &mut MemWriter, cx: &ctxt, mt: ty::mt) {
108 enc_mutability(w, mt.mutbl);
109 enc_ty(w, cx, mt.ty);
112 fn enc_opt<T>(w: &mut MemWriter, t: Option<T>, enc_f: |&mut MemWriter, T|) {
114 None => mywrite!(w, "n"),
122 fn enc_vec_per_param_space<T>(w: &mut MemWriter,
124 v: &VecPerParamSpace<T>,
125 op: |&mut MemWriter, &ctxt, &T|) {
126 for &space in subst::ParamSpace::all().iter() {
128 for t in v.get_vec(space).iter() {
135 pub fn enc_substs(w: &mut MemWriter, cx: &ctxt, substs: &subst::Substs) {
136 enc_region_substs(w, cx, &substs.regions);
137 enc_vec_per_param_space(w, cx, &substs.types,
138 |w, cx, &ty| enc_ty(w, cx, ty));
141 fn enc_region_substs(w: &mut MemWriter, cx: &ctxt, substs: &subst::RegionSubsts) {
143 subst::ErasedRegions => {
146 subst::NonerasedRegions(ref regions) => {
148 enc_vec_per_param_space(w, cx, regions,
149 |w, cx, &r| enc_region(w, cx, r));
154 fn enc_region(w: &mut MemWriter, cx: &ctxt, r: ty::Region) {
156 ty::ReLateBound(id, br) => {
157 mywrite!(w, "b[{}|", id);
158 enc_bound_region(w, cx, br);
161 ty::ReEarlyBound(node_id, space, index, name) => {
162 mywrite!(w, "B[{}|{}|{}|{}]",
166 token::get_name(name));
168 ty::ReFree(ref fr) => {
169 mywrite!(w, "f[{}|", fr.scope_id);
170 enc_bound_region(w, cx, fr.bound_region);
173 ty::ReScope(nid) => {
174 mywrite!(w, "s{}|", nid);
183 // these should not crop up after typeck
184 cx.diag.handler().bug("cannot encode region variables");
189 fn enc_bound_region(w: &mut MemWriter, cx: &ctxt, br: ty::BoundRegion) {
192 mywrite!(w, "a{}|", idx);
194 ty::BrNamed(d, name) => {
195 mywrite!(w, "[{}|{}]",
197 token::get_name(name));
200 mywrite!(w, "f{}|", id);
205 pub fn enc_trait_ref(w: &mut MemWriter, cx: &ctxt, s: &ty::TraitRef) {
206 mywrite!(w, "{}|", (cx.ds)(s.def_id));
207 enc_substs(w, cx, &s.substs);
210 pub fn enc_trait_store(w: &mut MemWriter, cx: &ctxt, s: ty::TraitStore) {
212 ty::UniqTraitStore => mywrite!(w, "~"),
213 ty::RegionTraitStore(re, m) => {
215 enc_region(w, cx, re);
216 enc_mutability(w, m);
221 fn enc_sty(w: &mut MemWriter, cx: &ctxt, st: &ty::sty) {
223 ty::ty_nil => mywrite!(w, "n"),
224 ty::ty_bot => mywrite!(w, "z"),
225 ty::ty_bool => mywrite!(w, "b"),
226 ty::ty_char => mywrite!(w, "c"),
229 TyI => mywrite!(w, "i"),
230 TyI8 => mywrite!(w, "MB"),
231 TyI16 => mywrite!(w, "MW"),
232 TyI32 => mywrite!(w, "ML"),
233 TyI64 => mywrite!(w, "MD")
238 TyU => mywrite!(w, "u"),
239 TyU8 => mywrite!(w, "Mb"),
240 TyU16 => mywrite!(w, "Mw"),
241 TyU32 => mywrite!(w, "Ml"),
242 TyU64 => mywrite!(w, "Md")
247 TyF32 => mywrite!(w, "Mf"),
248 TyF64 => mywrite!(w, "MF"),
249 TyF128 => mywrite!(w, "MQ")
252 ty::ty_enum(def, ref substs) => {
253 mywrite!(w, "t[{}|", (cx.ds)(def));
254 enc_substs(w, cx, substs);
257 ty::ty_trait(box ty::TyTrait {
263 mywrite!(w, "x[{}|", (cx.ds)(def_id));
264 enc_substs(w, cx, substs);
265 enc_trait_store(w, cx, store);
266 let bounds = ty::ParamBounds {builtin_bounds: bounds,
267 trait_bounds: Vec::new()};
268 enc_bounds(w, cx, &bounds);
271 ty::ty_tup(ref ts) => {
273 for t in ts.iter() { enc_ty(w, cx, *t); }
276 ty::ty_box(typ) => { mywrite!(w, "@"); enc_ty(w, cx, typ); }
277 ty::ty_uniq(typ) => { mywrite!(w, "~"); enc_ty(w, cx, typ); }
278 ty::ty_ptr(mt) => { mywrite!(w, "*"); enc_mt(w, cx, mt); }
279 ty::ty_rptr(r, mt) => {
281 enc_region(w, cx, r);
284 ty::ty_vec(mt, sz) => {
289 Some(n) => mywrite!(w, "{}|", n),
290 None => mywrite!(w, "|"),
296 ty::ty_closure(ref f) => {
298 enc_closure_ty(w, cx, *f);
300 ty::ty_bare_fn(ref f) => {
302 enc_bare_fn_ty(w, cx, f);
305 cx.diag.handler().bug("cannot encode inference variable types");
307 ty::ty_param(ParamTy {space, idx: id, def_id: did}) => {
308 mywrite!(w, "p{}|{}|{}|", (cx.ds)(did), id, space.to_uint())
310 ty::ty_struct(def, ref substs) => {
311 mywrite!(w, "a[{}|", (cx.ds)(def));
312 enc_substs(w, cx, substs);
321 fn enc_fn_style(w: &mut MemWriter, p: FnStyle) {
323 NormalFn => mywrite!(w, "n"),
324 UnsafeFn => mywrite!(w, "u"),
328 fn enc_abi(w: &mut MemWriter, abi: Abi) {
330 mywrite!(w, "{}", abi.name());
334 fn enc_onceness(w: &mut MemWriter, o: Onceness) {
336 Once => mywrite!(w, "o"),
337 Many => mywrite!(w, "m")
341 pub fn enc_bare_fn_ty(w: &mut MemWriter, cx: &ctxt, ft: &ty::BareFnTy) {
342 enc_fn_style(w, ft.fn_style);
344 enc_fn_sig(w, cx, &ft.sig);
347 fn enc_closure_ty(w: &mut MemWriter, cx: &ctxt, ft: &ty::ClosureTy) {
348 enc_fn_style(w, ft.fn_style);
349 enc_onceness(w, ft.onceness);
350 enc_trait_store(w, cx, ft.store);
351 let bounds = ty::ParamBounds {builtin_bounds: ft.bounds,
352 trait_bounds: Vec::new()};
353 enc_bounds(w, cx, &bounds);
354 enc_fn_sig(w, cx, &ft.sig);
357 fn enc_fn_sig(w: &mut MemWriter, cx: &ctxt, fsig: &ty::FnSig) {
358 mywrite!(w, "[{}|", fsig.binder_id);
359 for ty in fsig.inputs.iter() {
368 enc_ty(w, cx, fsig.output);
371 fn enc_bounds(w: &mut MemWriter, cx: &ctxt, bs: &ty::ParamBounds) {
372 for bound in bs.builtin_bounds.iter() {
374 ty::BoundSend => mywrite!(w, "S"),
375 ty::BoundStatic => mywrite!(w, "O"),
376 ty::BoundSized => mywrite!(w, "Z"),
377 ty::BoundCopy => mywrite!(w, "P"),
378 ty::BoundShare => mywrite!(w, "T"),
382 for tp in bs.trait_bounds.iter() {
384 enc_trait_ref(w, cx, &**tp);
390 pub fn enc_type_param_def(w: &mut MemWriter, cx: &ctxt, v: &ty::TypeParameterDef) {
391 mywrite!(w, "{}:{}|{}|{}|",
392 token::get_ident(v.ident), (cx.ds)(v.def_id),
393 v.space.to_uint(), v.index);
394 enc_bounds(w, cx, &*v.bounds);
395 enc_opt(w, v.default, |w, t| enc_ty(w, cx, t));