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;
20 use middle::subst::VecPerParamSpace;
21 use middle::ty::ParamTy;
22 use middle::ty::{mod, Ty};
23 use util::nodemap::FnvHashMap;
27 use syntax::diagnostic::SpanHandler;
28 use syntax::parse::token;
30 use rbml::io::SeekableMemWriter;
32 macro_rules! mywrite { ($($arg:tt)*) => ({ write!($($arg)*); }) }
34 pub struct ctxt<'a, 'tcx: 'a> {
35 pub diag: &'a SpanHandler,
36 // Def -> str Callback:
37 pub ds: fn(ast::DefId) -> String,
39 pub tcx: &'a ty::ctxt<'tcx>,
40 pub abbrevs: &'a abbrev_map<'tcx>
43 // Compact string representation for Ty values. API ty_str & parse_from_str.
44 // Extra parameters are for converting to/from def_ids in the string rep.
45 // Whatever format you choose should not contain pipe characters.
46 pub struct ty_abbrev {
50 pub type abbrev_map<'tcx> = RefCell<FnvHashMap<Ty<'tcx>, ty_abbrev>>;
52 pub fn enc_ty<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx>) {
53 match cx.abbrevs.borrow_mut().get(&t) {
54 Some(a) => { w.write(a.s.as_bytes()); return; }
57 let pos = w.tell().unwrap();
60 ty::ty_bool => mywrite!(w, "b"),
61 ty::ty_char => mywrite!(w, "c"),
64 ast::TyI => mywrite!(w, "i"),
65 ast::TyI8 => mywrite!(w, "MB"),
66 ast::TyI16 => mywrite!(w, "MW"),
67 ast::TyI32 => mywrite!(w, "ML"),
68 ast::TyI64 => mywrite!(w, "MD")
73 ast::TyU => mywrite!(w, "u"),
74 ast::TyU8 => mywrite!(w, "Mb"),
75 ast::TyU16 => mywrite!(w, "Mw"),
76 ast::TyU32 => mywrite!(w, "Ml"),
77 ast::TyU64 => mywrite!(w, "Md")
82 ast::TyF32 => mywrite!(w, "Mf"),
83 ast::TyF64 => mywrite!(w, "MF"),
86 ty::ty_enum(def, substs) => {
87 mywrite!(w, "t[{}|", (cx.ds)(def));
88 enc_substs(w, cx, substs);
91 ty::ty_trait(box ty::TyTrait { ref principal,
94 enc_trait_ref(w, cx, &*principal.0);
95 enc_existential_bounds(w, cx, bounds);
98 ty::ty_tup(ref ts) => {
100 for t in ts.iter() { enc_ty(w, cx, *t); }
103 ty::ty_uniq(typ) => { mywrite!(w, "~"); enc_ty(w, cx, typ); }
104 ty::ty_ptr(mt) => { mywrite!(w, "*"); enc_mt(w, cx, mt); }
105 ty::ty_rptr(r, mt) => {
107 enc_region(w, cx, *r);
110 ty::ty_vec(t, sz) => {
115 Some(n) => mywrite!(w, "{}|", n),
116 None => mywrite!(w, "|"),
122 ty::ty_closure(ref f) => {
124 enc_closure_ty(w, cx, &**f);
126 ty::ty_bare_fn(Some(def_id), f) => {
128 mywrite!(w, "{}|", (cx.ds)(def_id));
129 enc_bare_fn_ty(w, cx, f);
131 ty::ty_bare_fn(None, f) => {
133 enc_bare_fn_ty(w, cx, f);
136 cx.diag.handler().bug("cannot encode inference variable types");
138 ty::ty_param(ParamTy {space, idx, name}) => {
139 mywrite!(w, "p[{}|{}|{}]", idx, space.to_uint(), token::get_name(name))
141 ty::ty_struct(def, substs) => {
142 mywrite!(w, "a[{}|", (cx.ds)(def));
143 enc_substs(w, cx, substs);
146 ty::ty_unboxed_closure(def, region, substs) => {
147 mywrite!(w, "k[{}|", (cx.ds)(def));
148 enc_region(w, cx, *region);
149 enc_substs(w, cx, substs);
152 ty::ty_projection(ref data) => {
154 enc_trait_ref(w, cx, &*data.trait_ref);
155 mywrite!(w, "{}]", token::get_name(data.item_name));
161 cx.diag.handler().bug("unexpected type in enc_sty (ty_open)");
165 let end = w.tell().unwrap();
167 fn estimate_sz(u: u64) -> u64 {
170 while n != 0 { len += 1; n = n >> 4; }
173 let abbrev_len = 3 + estimate_sz(pos) + estimate_sz(len);
174 if abbrev_len < len {
175 // I.e. it's actually an abbreviation.
176 cx.abbrevs.borrow_mut().insert(t, ty_abbrev {
177 s: format!("#{:x}:{:x}#", pos, len)
182 fn enc_mutability(w: &mut SeekableMemWriter, mt: ast::Mutability) {
184 ast::MutImmutable => (),
185 ast::MutMutable => mywrite!(w, "m"),
189 fn enc_mt<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>,
191 enc_mutability(w, mt.mutbl);
192 enc_ty(w, cx, mt.ty);
195 fn enc_opt<T, F>(w: &mut SeekableMemWriter, t: Option<T>, enc_f: F) where
196 F: FnOnce(&mut SeekableMemWriter, T),
199 None => mywrite!(w, "n"),
207 fn enc_vec_per_param_space<'a, 'tcx, T, F>(w: &mut SeekableMemWriter,
209 v: &VecPerParamSpace<T>,
211 F: FnMut(&mut SeekableMemWriter, &ctxt<'a, 'tcx>, &T),
213 for &space in subst::ParamSpace::all().iter() {
215 for t in v.get_slice(space).iter() {
222 pub fn enc_substs<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>,
223 substs: &subst::Substs<'tcx>) {
224 enc_region_substs(w, cx, &substs.regions);
225 enc_vec_per_param_space(w, cx, &substs.types,
226 |w, cx, &ty| enc_ty(w, cx, ty));
229 fn enc_region_substs(w: &mut SeekableMemWriter, cx: &ctxt, substs: &subst::RegionSubsts) {
231 subst::ErasedRegions => {
234 subst::NonerasedRegions(ref regions) => {
236 enc_vec_per_param_space(w, cx, regions,
237 |w, cx, &r| enc_region(w, cx, r));
242 pub fn enc_region(w: &mut SeekableMemWriter, cx: &ctxt, r: ty::Region) {
244 ty::ReLateBound(id, br) => {
245 mywrite!(w, "b[{}|", id.depth);
246 enc_bound_region(w, cx, br);
249 ty::ReEarlyBound(node_id, space, index, name) => {
250 mywrite!(w, "B[{}|{}|{}|{}]",
254 token::get_name(name));
256 ty::ReFree(ref fr) => {
258 enc_scope(w, cx, fr.scope);
260 enc_bound_region(w, cx, fr.bound_region);
263 ty::ReScope(scope) => {
265 enc_scope(w, cx, scope);
275 // these should not crop up after typeck
276 cx.diag.handler().bug("cannot encode region variables");
281 fn enc_scope(w: &mut SeekableMemWriter, _cx: &ctxt, scope: region::CodeExtent) {
283 region::CodeExtent::Misc(node_id) => mywrite!(w, "M{}", node_id)
287 fn enc_bound_region(w: &mut SeekableMemWriter, cx: &ctxt, br: ty::BoundRegion) {
290 mywrite!(w, "a{}|", idx);
292 ty::BrNamed(d, name) => {
293 mywrite!(w, "[{}|{}]",
295 token::get_name(name));
298 mywrite!(w, "f{}|", id);
306 pub fn enc_trait_ref<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>,
307 s: &ty::TraitRef<'tcx>) {
308 mywrite!(w, "{}|", (cx.ds)(s.def_id));
309 enc_substs(w, cx, s.substs);
312 pub fn enc_trait_store(w: &mut SeekableMemWriter, cx: &ctxt, s: ty::TraitStore) {
314 ty::UniqTraitStore => mywrite!(w, "~"),
315 ty::RegionTraitStore(re, m) => {
317 enc_region(w, cx, re);
318 enc_mutability(w, m);
323 fn enc_unsafety(w: &mut SeekableMemWriter, p: ast::Unsafety) {
325 ast::Unsafety::Normal => mywrite!(w, "n"),
326 ast::Unsafety::Unsafe => mywrite!(w, "u"),
330 fn enc_abi(w: &mut SeekableMemWriter, abi: Abi) {
332 mywrite!(w, "{}", abi.name());
336 fn enc_onceness(w: &mut SeekableMemWriter, o: ast::Onceness) {
338 ast::Once => mywrite!(w, "o"),
339 ast::Many => mywrite!(w, "m")
343 pub fn enc_bare_fn_ty<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>,
344 ft: &ty::BareFnTy<'tcx>) {
345 enc_unsafety(w, ft.unsafety);
347 enc_fn_sig(w, cx, &ft.sig);
350 pub fn enc_closure_ty<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>,
351 ft: &ty::ClosureTy<'tcx>) {
352 enc_unsafety(w, ft.unsafety);
353 enc_onceness(w, ft.onceness);
354 enc_trait_store(w, cx, ft.store);
355 enc_existential_bounds(w, cx, &ft.bounds);
356 enc_fn_sig(w, cx, &ft.sig);
360 fn enc_fn_sig<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>,
361 fsig: &ty::PolyFnSig<'tcx>) {
363 for ty in fsig.0.inputs.iter() {
372 match fsig.0.output {
373 ty::FnConverging(result_type) => {
374 enc_ty(w, cx, result_type);
382 pub fn enc_builtin_bounds(w: &mut SeekableMemWriter, _cx: &ctxt, bs: &ty::BuiltinBounds) {
383 for bound in bs.iter() {
385 ty::BoundSend => mywrite!(w, "S"),
386 ty::BoundSized => mywrite!(w, "Z"),
387 ty::BoundCopy => mywrite!(w, "P"),
388 ty::BoundSync => mywrite!(w, "T"),
395 pub fn enc_existential_bounds<'a,'tcx>(w: &mut SeekableMemWriter,
397 bs: &ty::ExistentialBounds<'tcx>) {
398 let param_bounds = ty::ParamBounds { trait_bounds: vec!(),
399 region_bounds: vec!(bs.region_bound),
400 builtin_bounds: bs.builtin_bounds,
401 projection_bounds: bs.projection_bounds.clone() };
402 enc_bounds(w, cx, ¶m_bounds);
405 pub fn enc_bounds<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>,
406 bs: &ty::ParamBounds<'tcx>) {
407 enc_builtin_bounds(w, cx, &bs.builtin_bounds);
409 for &r in bs.region_bounds.iter() {
411 enc_region(w, cx, r);
414 for tp in bs.trait_bounds.iter() {
416 enc_trait_ref(w, cx, &*tp.0);
419 for tp in bs.projection_bounds.iter() {
421 enc_projection_predicate(w, cx, &tp.0);
427 pub fn enc_type_param_def<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>,
428 v: &ty::TypeParameterDef<'tcx>) {
429 mywrite!(w, "{}:{}|{}|{}|",
430 token::get_name(v.name), (cx.ds)(v.def_id),
431 v.space.to_uint(), v.index);
432 enc_bounds(w, cx, &v.bounds);
433 enc_opt(w, v.default, |w, t| enc_ty(w, cx, t));
436 pub fn enc_predicate<'a, 'tcx>(w: &mut SeekableMemWriter,
438 p: &ty::Predicate<'tcx>)
441 ty::Predicate::Trait(ref trait_ref) => {
443 enc_trait_ref(w, cx, &*trait_ref.0.trait_ref);
445 ty::Predicate::Equate(ty::Binder(ty::EquatePredicate(a, b))) => {
450 ty::Predicate::RegionOutlives(ty::Binder(ty::OutlivesPredicate(a, b))) => {
452 enc_region(w, cx, a);
453 enc_region(w, cx, b);
455 ty::Predicate::TypeOutlives(ty::Binder(ty::OutlivesPredicate(a, b))) => {
458 enc_region(w, cx, b);
460 ty::Predicate::Projection(ty::Binder(ref data)) => {
462 enc_projection_predicate(w, cx, data)
467 fn enc_projection_predicate<'a, 'tcx>(w: &mut SeekableMemWriter,
469 data: &ty::ProjectionPredicate<'tcx>) {
470 enc_trait_ref(w, cx, &*data.projection_ty.trait_ref);
471 mywrite!(w, "{}|", token::get_name(data.projection_ty.item_name));
472 enc_ty(w, cx, data.ty);