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>>;
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 fn enc_mutability(w: &mut MemWriter, mt: ast::Mutability) {
78 MutMutable => mywrite!(w, "m"),
82 fn enc_mt(w: &mut MemWriter, cx: &ctxt, mt: ty::mt) {
83 enc_mutability(w, mt.mutbl);
87 fn enc_opt<T>(w: &mut MemWriter, t: Option<T>, enc_f: |&mut MemWriter, T|) {
89 None => mywrite!(w, "n"),
97 fn enc_vec_per_param_space<T>(w: &mut MemWriter,
99 v: &VecPerParamSpace<T>,
100 op: |&mut MemWriter, &ctxt, &T|) {
101 for &space in subst::ParamSpace::all().iter() {
103 for t in v.get_vec(space).iter() {
110 pub fn enc_substs(w: &mut MemWriter, cx: &ctxt, substs: &subst::Substs) {
111 enc_region_substs(w, cx, &substs.regions);
112 enc_vec_per_param_space(w, cx, &substs.types,
113 |w, cx, &ty| enc_ty(w, cx, ty));
116 fn enc_region_substs(w: &mut MemWriter, cx: &ctxt, substs: &subst::RegionSubsts) {
118 subst::ErasedRegions => {
121 subst::NonerasedRegions(ref regions) => {
123 enc_vec_per_param_space(w, cx, regions,
124 |w, cx, &r| enc_region(w, cx, r));
129 fn enc_region(w: &mut MemWriter, cx: &ctxt, r: ty::Region) {
131 ty::ReLateBound(id, br) => {
132 mywrite!(w, "b[{}|", id);
133 enc_bound_region(w, cx, br);
136 ty::ReEarlyBound(node_id, space, index, name) => {
137 mywrite!(w, "B[{}|{}|{}|{}]",
141 token::get_name(name));
143 ty::ReFree(ref fr) => {
144 mywrite!(w, "f[{}|", fr.scope_id);
145 enc_bound_region(w, cx, fr.bound_region);
148 ty::ReScope(nid) => {
149 mywrite!(w, "s{}|", nid);
158 // these should not crop up after typeck
159 cx.diag.handler().bug("cannot encode region variables");
164 fn enc_bound_region(w: &mut MemWriter, cx: &ctxt, br: ty::BoundRegion) {
167 mywrite!(w, "a{}|", idx);
169 ty::BrNamed(d, name) => {
170 mywrite!(w, "[{}|{}]",
172 token::get_name(name));
175 mywrite!(w, "f{}|", id);
180 pub fn enc_trait_ref(w: &mut MemWriter, cx: &ctxt, s: &ty::TraitRef) {
181 mywrite!(w, "{}|", (cx.ds)(s.def_id));
182 enc_substs(w, cx, &s.substs);
185 pub fn enc_trait_store(w: &mut MemWriter, cx: &ctxt, s: ty::TraitStore) {
187 ty::UniqTraitStore => mywrite!(w, "~"),
188 ty::RegionTraitStore(re, m) => {
190 enc_region(w, cx, re);
191 enc_mutability(w, m);
196 fn enc_sty(w: &mut MemWriter, cx: &ctxt, st: &ty::sty) {
198 ty::ty_nil => mywrite!(w, "n"),
199 ty::ty_bot => mywrite!(w, "z"),
200 ty::ty_bool => mywrite!(w, "b"),
201 ty::ty_char => mywrite!(w, "c"),
204 TyI => mywrite!(w, "i"),
205 TyI8 => mywrite!(w, "MB"),
206 TyI16 => mywrite!(w, "MW"),
207 TyI32 => mywrite!(w, "ML"),
208 TyI64 => mywrite!(w, "MD")
213 TyU => mywrite!(w, "u"),
214 TyU8 => mywrite!(w, "Mb"),
215 TyU16 => mywrite!(w, "Mw"),
216 TyU32 => mywrite!(w, "Ml"),
217 TyU64 => mywrite!(w, "Md")
222 TyF32 => mywrite!(w, "Mf"),
223 TyF64 => mywrite!(w, "MF"),
226 ty::ty_enum(def, ref substs) => {
227 mywrite!(w, "t[{}|", (cx.ds)(def));
228 enc_substs(w, cx, substs);
231 ty::ty_trait(box ty::TyTrait {
236 mywrite!(w, "x[{}|", (cx.ds)(def_id));
237 enc_substs(w, cx, substs);
238 let bounds = ty::ParamBounds {builtin_bounds: bounds,
239 trait_bounds: Vec::new()};
240 enc_bounds(w, cx, &bounds);
243 ty::ty_tup(ref ts) => {
245 for t in ts.iter() { enc_ty(w, cx, *t); }
248 ty::ty_box(typ) => { mywrite!(w, "@"); enc_ty(w, cx, typ); }
249 ty::ty_uniq(typ) => { mywrite!(w, "~"); enc_ty(w, cx, typ); }
250 ty::ty_ptr(mt) => { mywrite!(w, "*"); enc_mt(w, cx, mt); }
251 ty::ty_rptr(r, mt) => {
253 enc_region(w, cx, r);
256 ty::ty_vec(mt, sz) => {
261 Some(n) => mywrite!(w, "{}|", n),
262 None => mywrite!(w, "|"),
268 ty::ty_closure(ref f) => {
270 enc_closure_ty(w, cx, *f);
272 ty::ty_bare_fn(ref f) => {
274 enc_bare_fn_ty(w, cx, f);
277 cx.diag.handler().bug("cannot encode inference variable types");
279 ty::ty_param(ParamTy {space, idx: id, def_id: did}) => {
280 mywrite!(w, "p{}|{}|{}|", (cx.ds)(did), id, space.to_uint())
282 ty::ty_struct(def, ref substs) => {
283 mywrite!(w, "a[{}|", (cx.ds)(def));
284 enc_substs(w, cx, substs);
293 fn enc_fn_style(w: &mut MemWriter, p: FnStyle) {
295 NormalFn => mywrite!(w, "n"),
296 UnsafeFn => mywrite!(w, "u"),
300 fn enc_abi(w: &mut MemWriter, abi: Abi) {
302 mywrite!(w, "{}", abi.name());
306 fn enc_onceness(w: &mut MemWriter, o: Onceness) {
308 Once => mywrite!(w, "o"),
309 Many => mywrite!(w, "m")
313 pub fn enc_bare_fn_ty(w: &mut MemWriter, cx: &ctxt, ft: &ty::BareFnTy) {
314 enc_fn_style(w, ft.fn_style);
316 enc_fn_sig(w, cx, &ft.sig);
319 fn enc_closure_ty(w: &mut MemWriter, cx: &ctxt, ft: &ty::ClosureTy) {
320 enc_fn_style(w, ft.fn_style);
321 enc_onceness(w, ft.onceness);
322 enc_trait_store(w, cx, ft.store);
323 let bounds = ty::ParamBounds {builtin_bounds: ft.bounds,
324 trait_bounds: Vec::new()};
325 enc_bounds(w, cx, &bounds);
326 enc_fn_sig(w, cx, &ft.sig);
329 fn enc_fn_sig(w: &mut MemWriter, cx: &ctxt, fsig: &ty::FnSig) {
330 mywrite!(w, "[{}|", fsig.binder_id);
331 for ty in fsig.inputs.iter() {
340 enc_ty(w, cx, fsig.output);
343 fn enc_bounds(w: &mut MemWriter, cx: &ctxt, bs: &ty::ParamBounds) {
344 for bound in bs.builtin_bounds.iter() {
346 ty::BoundSend => mywrite!(w, "S"),
347 ty::BoundStatic => mywrite!(w, "O"),
348 ty::BoundSized => mywrite!(w, "Z"),
349 ty::BoundCopy => mywrite!(w, "P"),
350 ty::BoundShare => mywrite!(w, "T"),
354 for tp in bs.trait_bounds.iter() {
356 enc_trait_ref(w, cx, &**tp);
362 pub fn enc_type_param_def(w: &mut MemWriter, cx: &ctxt, v: &ty::TypeParameterDef) {
363 mywrite!(w, "{}:{}|{}|{}|",
364 token::get_ident(v.ident), (cx.ds)(v.def_id),
365 v.space.to_uint(), v.index);
366 enc_bounds(w, cx, &*v.bounds);
367 enc_opt(w, v.default, |w, t| enc_ty(w, cx, t));