1 // Copyright 2012-2015 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::io::prelude::*;
21 use middle::subst::VecPerParamSpace;
22 use middle::ty::ParamTy;
23 use middle::ty::{self, Ty};
24 use util::nodemap::FnvHashMap;
28 use syntax::diagnostic::SpanHandler;
29 use syntax::parse::token;
31 use rbml::writer::Encoder;
33 macro_rules! mywrite { ($w:expr, $($arg:tt)*) => ({ write!($w.writer, $($arg)*); }) }
35 pub struct ctxt<'a, 'tcx: 'a> {
36 pub diag: &'a SpanHandler,
37 // Def -> str Callback:
38 pub ds: fn(ast::DefId) -> String,
40 pub tcx: &'a ty::ctxt<'tcx>,
41 pub abbrevs: &'a abbrev_map<'tcx>
44 // Compact string representation for Ty values. API ty_str & parse_from_str.
45 // Extra parameters are for converting to/from def_ids in the string rep.
46 // Whatever format you choose should not contain pipe characters.
47 pub struct ty_abbrev {
51 pub type abbrev_map<'tcx> = RefCell<FnvHashMap<Ty<'tcx>, ty_abbrev>>;
53 pub fn enc_ty<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx>) {
54 match cx.abbrevs.borrow_mut().get(&t) {
55 Some(a) => { w.writer.write_all(a.s.as_bytes()); return; }
59 // type abbreviations needs a stable position
60 let pos = w.mark_stable_position();
63 ty::ty_bool => mywrite!(w, "b"),
64 ty::ty_char => mywrite!(w, "c"),
67 ast::TyIs(_) => mywrite!(w, "is"),
68 ast::TyI8 => mywrite!(w, "MB"),
69 ast::TyI16 => mywrite!(w, "MW"),
70 ast::TyI32 => mywrite!(w, "ML"),
71 ast::TyI64 => mywrite!(w, "MD")
76 ast::TyUs(_) => mywrite!(w, "us"),
77 ast::TyU8 => mywrite!(w, "Mb"),
78 ast::TyU16 => mywrite!(w, "Mw"),
79 ast::TyU32 => mywrite!(w, "Ml"),
80 ast::TyU64 => mywrite!(w, "Md")
85 ast::TyF32 => mywrite!(w, "Mf"),
86 ast::TyF64 => mywrite!(w, "MF"),
89 ty::ty_enum(def, substs) => {
90 mywrite!(w, "t[{}|", (cx.ds)(def));
91 enc_substs(w, cx, substs);
94 ty::ty_trait(box ty::TyTrait { ref principal,
97 enc_trait_ref(w, cx, &*principal.0);
98 enc_existential_bounds(w, cx, bounds);
101 ty::ty_tup(ref ts) => {
103 for t in ts { enc_ty(w, cx, *t); }
106 ty::ty_uniq(typ) => { mywrite!(w, "~"); enc_ty(w, cx, typ); }
107 ty::ty_ptr(mt) => { mywrite!(w, "*"); enc_mt(w, cx, mt); }
108 ty::ty_rptr(r, mt) => {
110 enc_region(w, cx, *r);
113 ty::ty_vec(t, sz) => {
118 Some(n) => mywrite!(w, "{}|", n),
119 None => mywrite!(w, "|"),
125 ty::ty_bare_fn(Some(def_id), f) => {
127 mywrite!(w, "{}|", (cx.ds)(def_id));
128 enc_bare_fn_ty(w, cx, f);
130 ty::ty_bare_fn(None, f) => {
132 enc_bare_fn_ty(w, cx, f);
135 cx.diag.handler().bug("cannot encode inference variable types");
137 ty::ty_param(ParamTy {space, idx, name}) => {
138 mywrite!(w, "p[{}|{}|{}]", idx, space.to_uint(), token::get_name(name))
140 ty::ty_struct(def, substs) => {
141 mywrite!(w, "a[{}|", (cx.ds)(def));
142 enc_substs(w, cx, substs);
145 ty::ty_closure(def, substs) => {
146 mywrite!(w, "k[{}|", (cx.ds)(def));
147 enc_substs(w, cx, substs);
150 ty::ty_projection(ref data) => {
152 enc_trait_ref(w, cx, &*data.trait_ref);
153 mywrite!(w, "{}]", token::get_name(data.item_name));
160 let end = w.mark_stable_position();
162 fn estimate_sz(u: u64) -> u64 {
165 while n != 0 { len += 1; n = n >> 4; }
168 let abbrev_len = 3 + estimate_sz(pos) + estimate_sz(len);
169 if abbrev_len < len {
170 // I.e. it's actually an abbreviation.
171 cx.abbrevs.borrow_mut().insert(t, ty_abbrev {
172 s: format!("#{:x}:{:x}#", pos, len)
177 fn enc_mutability(w: &mut Encoder, mt: ast::Mutability) {
179 ast::MutImmutable => (),
180 ast::MutMutable => mywrite!(w, "m"),
184 fn enc_mt<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>,
186 enc_mutability(w, mt.mutbl);
187 enc_ty(w, cx, mt.ty);
190 fn enc_opt<T, F>(w: &mut Encoder, t: Option<T>, enc_f: F) where
191 F: FnOnce(&mut Encoder, T),
194 None => mywrite!(w, "n"),
202 fn enc_vec_per_param_space<'a, 'tcx, T, F>(w: &mut Encoder,
204 v: &VecPerParamSpace<T>,
206 F: FnMut(&mut Encoder, &ctxt<'a, 'tcx>, &T),
208 for &space in &subst::ParamSpace::all() {
210 for t in v.get_slice(space) {
217 pub fn enc_substs<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>,
218 substs: &subst::Substs<'tcx>) {
219 enc_region_substs(w, cx, &substs.regions);
220 enc_vec_per_param_space(w, cx, &substs.types,
221 |w, cx, &ty| enc_ty(w, cx, ty));
224 fn enc_region_substs(w: &mut Encoder, cx: &ctxt, substs: &subst::RegionSubsts) {
226 subst::ErasedRegions => {
229 subst::NonerasedRegions(ref regions) => {
231 enc_vec_per_param_space(w, cx, regions,
232 |w, cx, &r| enc_region(w, cx, r));
237 pub fn enc_region(w: &mut Encoder, cx: &ctxt, r: ty::Region) {
239 ty::ReLateBound(id, br) => {
240 mywrite!(w, "b[{}|", id.depth);
241 enc_bound_region(w, cx, br);
244 ty::ReEarlyBound(node_id, space, index, name) => {
245 mywrite!(w, "B[{}|{}|{}|{}]",
249 token::get_name(name));
251 ty::ReFree(ref fr) => {
253 enc_destruction_scope_data(w, fr.scope);
255 enc_bound_region(w, cx, fr.bound_region);
258 ty::ReScope(scope) => {
260 enc_scope(w, cx, scope);
270 // these should not crop up after typeck
271 cx.diag.handler().bug("cannot encode region variables");
276 fn enc_scope(w: &mut Encoder, _cx: &ctxt, scope: region::CodeExtent) {
278 region::CodeExtent::Misc(node_id) => mywrite!(w, "M{}", node_id),
279 region::CodeExtent::Remainder(region::BlockRemainder {
280 block: b, first_statement_index: i }) => mywrite!(w, "B{}{}", b, i),
281 region::CodeExtent::DestructionScope(node_id) => mywrite!(w, "D{}", node_id),
285 fn enc_destruction_scope_data(w: &mut Encoder,
286 d: region::DestructionScopeData) {
287 mywrite!(w, "{}", d.node_id);
290 fn enc_bound_region(w: &mut Encoder, cx: &ctxt, br: ty::BoundRegion) {
293 mywrite!(w, "a{}|", idx);
295 ty::BrNamed(d, name) => {
296 mywrite!(w, "[{}|{}]",
298 token::get_name(name));
301 mywrite!(w, "f{}|", id);
309 pub fn enc_trait_ref<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>,
310 s: &ty::TraitRef<'tcx>) {
311 mywrite!(w, "{}|", (cx.ds)(s.def_id));
312 enc_substs(w, cx, s.substs);
315 fn enc_unsafety(w: &mut Encoder, p: ast::Unsafety) {
317 ast::Unsafety::Normal => mywrite!(w, "n"),
318 ast::Unsafety::Unsafe => mywrite!(w, "u"),
322 fn enc_abi(w: &mut Encoder, abi: Abi) {
324 mywrite!(w, "{}", abi.name());
328 pub fn enc_bare_fn_ty<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>,
329 ft: &ty::BareFnTy<'tcx>) {
330 enc_unsafety(w, ft.unsafety);
332 enc_fn_sig(w, cx, &ft.sig);
335 pub fn enc_closure_ty<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>,
336 ft: &ty::ClosureTy<'tcx>) {
337 enc_unsafety(w, ft.unsafety);
338 enc_fn_sig(w, cx, &ft.sig);
342 fn enc_fn_sig<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>,
343 fsig: &ty::PolyFnSig<'tcx>) {
345 for ty in &fsig.0.inputs {
354 match fsig.0.output {
355 ty::FnConverging(result_type) => {
356 enc_ty(w, cx, result_type);
364 pub fn enc_builtin_bounds(w: &mut Encoder, _cx: &ctxt, bs: &ty::BuiltinBounds) {
367 ty::BoundSend => mywrite!(w, "S"),
368 ty::BoundSized => mywrite!(w, "Z"),
369 ty::BoundCopy => mywrite!(w, "P"),
370 ty::BoundSync => mywrite!(w, "T"),
377 pub fn enc_existential_bounds<'a,'tcx>(w: &mut Encoder,
379 bs: &ty::ExistentialBounds<'tcx>) {
380 let param_bounds = ty::ParamBounds { trait_bounds: vec!(),
381 region_bounds: vec!(bs.region_bound),
382 builtin_bounds: bs.builtin_bounds,
383 projection_bounds: bs.projection_bounds.clone() };
384 enc_bounds(w, cx, ¶m_bounds);
387 pub fn enc_bounds<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>,
388 bs: &ty::ParamBounds<'tcx>) {
389 enc_builtin_bounds(w, cx, &bs.builtin_bounds);
391 enc_region_bounds(w, cx, &bs.region_bounds);
393 for tp in &bs.trait_bounds {
395 enc_trait_ref(w, cx, &*tp.0);
398 for tp in &bs.projection_bounds {
400 enc_projection_predicate(w, cx, &tp.0);
406 pub fn enc_region_bounds<'a, 'tcx>(w: &mut Encoder,
411 enc_region(w, cx, r);
417 pub fn enc_type_param_def<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>,
418 v: &ty::TypeParameterDef<'tcx>) {
419 mywrite!(w, "{}:{}|{}|{}|",
420 token::get_name(v.name), (cx.ds)(v.def_id),
421 v.space.to_uint(), v.index);
422 enc_opt(w, v.default, |w, t| enc_ty(w, cx, t));
423 enc_object_lifetime_default(w, cx, v.object_lifetime_default);
426 fn enc_object_lifetime_default<'a, 'tcx>(w: &mut Encoder,
428 default: Option<ty::ObjectLifetimeDefault>)
431 None => mywrite!(w, "n"),
432 Some(ty::ObjectLifetimeDefault::Ambiguous) => mywrite!(w, "a"),
433 Some(ty::ObjectLifetimeDefault::Specific(r)) => {
435 enc_region(w, cx, r);
440 pub fn enc_predicate<'a, 'tcx>(w: &mut Encoder,
442 p: &ty::Predicate<'tcx>)
445 ty::Predicate::Trait(ref trait_ref) => {
447 enc_trait_ref(w, cx, &*trait_ref.0.trait_ref);
449 ty::Predicate::Equate(ty::Binder(ty::EquatePredicate(a, b))) => {
454 ty::Predicate::RegionOutlives(ty::Binder(ty::OutlivesPredicate(a, b))) => {
456 enc_region(w, cx, a);
457 enc_region(w, cx, b);
459 ty::Predicate::TypeOutlives(ty::Binder(ty::OutlivesPredicate(a, b))) => {
462 enc_region(w, cx, b);
464 ty::Predicate::Projection(ty::Binder(ref data)) => {
466 enc_projection_predicate(w, cx, data)
471 fn enc_projection_predicate<'a, 'tcx>(w: &mut Encoder,
473 data: &ty::ProjectionPredicate<'tcx>) {
474 enc_trait_ref(w, cx, &*data.projection_ty.trait_ref);
475 mywrite!(w, "{}|", token::get_name(data.projection_ty.item_name));
476 enc_ty(w, cx, data.ty);