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;
18 use std::io::prelude::*;
20 use middle::def_id::DefId;
23 use middle::subst::VecPerParamSpace;
24 use middle::ty::ParamTy;
25 use middle::ty::{self, Ty};
26 use rustc::util::nodemap::FnvHashMap;
32 use syntax::errors::Handler;
37 pub struct ctxt<'a, 'tcx: 'a> {
38 pub diag: &'a Handler,
39 // Def -> str Callback:
40 pub ds: fn(DefId) -> String,
42 pub tcx: &'a ty::ctxt<'tcx>,
43 pub abbrevs: &'a abbrev_map<'tcx>
46 impl<'a, 'tcx> encoder::EncodeContext<'a, 'tcx> {
47 pub fn ty_str_ctxt<'b>(&'b self) -> ctxt<'b, 'tcx> {
49 diag: self.tcx.sess.diagnostic(),
50 ds: encoder::def_to_string,
52 abbrevs: &self.type_abbrevs
57 // Compact string representation for Ty values. API TyStr & parse_from_str.
58 // Extra parameters are for converting to/from def_ids in the string rep.
59 // Whatever format you choose should not contain pipe characters.
60 pub struct ty_abbrev {
64 pub type abbrev_map<'tcx> = RefCell<FnvHashMap<Ty<'tcx>, ty_abbrev>>;
66 pub fn enc_ty<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx>) {
67 match cx.abbrevs.borrow_mut().get(&t) {
68 Some(a) => { w.write_all(&a.s); return; }
72 let pos = w.position();
75 ty::TyBool => { write!(w, "b"); }
76 ty::TyChar => { write!(w, "c"); }
79 ast::TyIs => write!(w, "is"),
80 ast::TyI8 => write!(w, "MB"),
81 ast::TyI16 => write!(w, "MW"),
82 ast::TyI32 => write!(w, "ML"),
83 ast::TyI64 => write!(w, "MD")
88 ast::TyUs => write!(w, "us"),
89 ast::TyU8 => write!(w, "Mb"),
90 ast::TyU16 => write!(w, "Mw"),
91 ast::TyU32 => write!(w, "Ml"),
92 ast::TyU64 => write!(w, "Md")
97 ast::TyF32 => write!(w, "Mf"),
98 ast::TyF64 => write!(w, "MF"),
101 ty::TyEnum(def, substs) => {
102 write!(w, "t[{}|", (cx.ds)(def.did));
103 enc_substs(w, cx, substs);
106 ty::TyTrait(box ty::TraitTy { ref principal,
109 enc_trait_ref(w, cx, principal.0);
110 enc_existential_bounds(w, cx, bounds);
113 ty::TyTuple(ref ts) => {
115 for t in ts { enc_ty(w, cx, *t); }
118 ty::TyBox(typ) => { write!(w, "~"); enc_ty(w, cx, typ); }
119 ty::TyRawPtr(mt) => { write!(w, "*"); enc_mt(w, cx, mt); }
120 ty::TyRef(r, mt) => {
122 enc_region(w, cx, *r);
125 ty::TyArray(t, sz) => {
128 write!(w, "/{}|", sz);
138 ty::TyBareFn(Some(def_id), f) => {
140 write!(w, "{}|", (cx.ds)(def_id));
141 enc_bare_fn_ty(w, cx, f);
143 ty::TyBareFn(None, f) => {
145 enc_bare_fn_ty(w, cx, f);
148 cx.diag.bug("cannot encode inference variable types");
150 ty::TyParam(ParamTy {space, idx, name}) => {
151 write!(w, "p[{}|{}|{}]", idx, space.to_uint(), name);
153 ty::TyStruct(def, substs) => {
154 write!(w, "a[{}|", (cx.ds)(def.did));
155 enc_substs(w, cx, substs);
158 ty::TyClosure(def, ref substs) => {
159 write!(w, "k[{}|", (cx.ds)(def));
160 enc_substs(w, cx, &substs.func_substs);
161 for ty in &substs.upvar_tys {
167 ty::TyProjection(ref data) => {
169 enc_trait_ref(w, cx, data.trait_ref);
170 write!(w, "{}]", data.item_name);
177 let end = w.position();
180 let mut abbrev = Cursor::new(Vec::with_capacity(16));
181 abbrev.write_all(b"#");
183 let start_position = abbrev.position() as usize;
184 let bytes_written = leb128::write_unsigned_leb128(abbrev.get_mut(),
187 abbrev.set_position((start_position + bytes_written) as u64);
190 cx.abbrevs.borrow_mut().insert(t, ty_abbrev {
191 s: if abbrev.position() < len {
192 abbrev.get_ref()[..abbrev.position() as usize].to_owned()
194 // if the abbreviation is longer than the real type,
195 // don't use #-notation. However, insert it here so
196 // other won't have to `mark_stable_position`
197 w.get_ref()[pos as usize .. end as usize].to_owned()
202 fn enc_mutability(w: &mut Cursor<Vec<u8>>, mt: hir::Mutability) {
204 hir::MutImmutable => (),
211 fn enc_mt<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
212 mt: ty::TypeAndMut<'tcx>) {
213 enc_mutability(w, mt.mutbl);
214 enc_ty(w, cx, mt.ty);
217 fn enc_opt<T, F>(w: &mut Cursor<Vec<u8>>, t: Option<T>, enc_f: F) where
218 F: FnOnce(&mut Cursor<Vec<u8>>, T),
231 fn enc_vec_per_param_space<'a, 'tcx, T, F>(w: &mut Cursor<Vec<u8>>,
233 v: &VecPerParamSpace<T>,
235 F: FnMut(&mut Cursor<Vec<u8>>, &ctxt<'a, 'tcx>, &T),
237 for &space in &subst::ParamSpace::all() {
239 for t in v.get_slice(space) {
246 pub fn enc_substs<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
247 substs: &subst::Substs<'tcx>) {
248 enc_region_substs(w, cx, &substs.regions);
249 enc_vec_per_param_space(w, cx, &substs.types,
250 |w, cx, &ty| enc_ty(w, cx, ty));
253 fn enc_region_substs(w: &mut Cursor<Vec<u8>>, cx: &ctxt, substs: &subst::RegionSubsts) {
255 subst::ErasedRegions => {
258 subst::NonerasedRegions(ref regions) => {
260 enc_vec_per_param_space(w, cx, regions,
261 |w, cx, &r| enc_region(w, cx, r));
266 pub fn enc_region(w: &mut Cursor<Vec<u8>>, cx: &ctxt, r: ty::Region) {
268 ty::ReLateBound(id, br) => {
269 write!(w, "b[{}|", id.depth);
270 enc_bound_region(w, cx, br);
273 ty::ReEarlyBound(ref data) => {
274 write!(w, "B[{}|{}|{}]",
275 data.space.to_uint(),
279 ty::ReFree(ref fr) => {
281 enc_scope(w, cx, fr.scope);
283 enc_bound_region(w, cx, fr.bound_region);
286 ty::ReScope(scope) => {
288 enc_scope(w, cx, scope);
297 ty::ReVar(_) | ty::ReSkolemized(..) => {
298 // these should not crop up after typeck
299 cx.diag.bug("cannot encode region variables");
304 fn enc_scope(w: &mut Cursor<Vec<u8>>, cx: &ctxt, scope: region::CodeExtent) {
305 match cx.tcx.region_maps.code_extent_data(scope) {
306 region::CodeExtentData::CallSiteScope {
307 fn_id, body_id } => write!(w, "C[{}|{}]", fn_id, body_id),
308 region::CodeExtentData::ParameterScope {
309 fn_id, body_id } => write!(w, "P[{}|{}]", fn_id, body_id),
310 region::CodeExtentData::Misc(node_id) => write!(w, "M{}", node_id),
311 region::CodeExtentData::Remainder(region::BlockRemainder {
312 block: b, first_statement_index: i }) => write!(w, "B[{}|{}]", b, i),
313 region::CodeExtentData::DestructionScope(node_id) => write!(w, "D{}", node_id),
317 fn enc_bound_region(w: &mut Cursor<Vec<u8>>, cx: &ctxt, br: ty::BoundRegion) {
320 write!(w, "a{}|", idx);
322 ty::BrNamed(d, name) => {
328 write!(w, "f{}|", id);
336 pub fn enc_trait_ref<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
337 s: ty::TraitRef<'tcx>) {
338 write!(w, "{}|", (cx.ds)(s.def_id));
339 enc_substs(w, cx, s.substs);
342 fn enc_unsafety(w: &mut Cursor<Vec<u8>>, p: hir::Unsafety) {
344 hir::Unsafety::Normal => write!(w, "n"),
345 hir::Unsafety::Unsafe => write!(w, "u"),
349 fn enc_abi(w: &mut Cursor<Vec<u8>>, abi: Abi) {
351 write!(w, "{}", abi.name());
355 pub fn enc_bare_fn_ty<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
356 ft: &ty::BareFnTy<'tcx>) {
357 enc_unsafety(w, ft.unsafety);
359 enc_fn_sig(w, cx, &ft.sig);
362 pub fn enc_closure_ty<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
363 ft: &ty::ClosureTy<'tcx>) {
364 enc_unsafety(w, ft.unsafety);
365 enc_fn_sig(w, cx, &ft.sig);
369 fn enc_fn_sig<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
370 fsig: &ty::PolyFnSig<'tcx>) {
372 for ty in &fsig.0.inputs {
381 match fsig.0.output {
382 ty::FnConverging(result_type) => {
383 enc_ty(w, cx, result_type);
391 pub fn enc_builtin_bounds(w: &mut Cursor<Vec<u8>>, _cx: &ctxt, bs: &ty::BuiltinBounds) {
394 ty::BoundSend => write!(w, "S"),
395 ty::BoundSized => write!(w, "Z"),
396 ty::BoundCopy => write!(w, "P"),
397 ty::BoundSync => write!(w, "T"),
404 pub fn enc_existential_bounds<'a,'tcx>(w: &mut Cursor<Vec<u8>>,
406 bs: &ty::ExistentialBounds<'tcx>) {
407 enc_builtin_bounds(w, cx, &bs.builtin_bounds);
409 enc_region(w, cx, bs.region_bound);
411 for tp in &bs.projection_bounds {
413 enc_projection_predicate(w, cx, &tp.0);
419 pub fn enc_type_param_def<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
420 v: &ty::TypeParameterDef<'tcx>) {
421 write!(w, "{}:{}|{}|{}|{}|",
422 v.name, (cx.ds)(v.def_id),
423 v.space.to_uint(), v.index, (cx.ds)(v.default_def_id));
424 enc_opt(w, v.default, |w, t| enc_ty(w, cx, t));
425 enc_object_lifetime_default(w, cx, v.object_lifetime_default);
428 pub fn enc_region_param_def(w: &mut Cursor<Vec<u8>>, cx: &ctxt,
429 v: &ty::RegionParameterDef) {
430 write!(w, "{}:{}|{}|{}|",
431 v.name, (cx.ds)(v.def_id),
432 v.space.to_uint(), v.index);
433 for &r in &v.bounds {
435 enc_region(w, cx, r);
440 fn enc_object_lifetime_default<'a, 'tcx>(w: &mut Cursor<Vec<u8>>,
442 default: ty::ObjectLifetimeDefault)
445 ty::ObjectLifetimeDefault::Ambiguous => {
448 ty::ObjectLifetimeDefault::BaseDefault => {
451 ty::ObjectLifetimeDefault::Specific(r) => {
453 enc_region(w, cx, r);
458 pub fn enc_predicate<'a, 'tcx>(w: &mut Cursor<Vec<u8>>,
460 p: &ty::Predicate<'tcx>)
463 ty::Predicate::Trait(ref trait_ref) => {
465 enc_trait_ref(w, cx, trait_ref.0.trait_ref);
467 ty::Predicate::Equate(ty::Binder(ty::EquatePredicate(a, b))) => {
472 ty::Predicate::RegionOutlives(ty::Binder(ty::OutlivesPredicate(a, b))) => {
474 enc_region(w, cx, a);
475 enc_region(w, cx, b);
477 ty::Predicate::TypeOutlives(ty::Binder(ty::OutlivesPredicate(a, b))) => {
480 enc_region(w, cx, b);
482 ty::Predicate::Projection(ty::Binder(ref data)) => {
484 enc_projection_predicate(w, cx, data);
486 ty::Predicate::WellFormed(data) => {
490 ty::Predicate::ObjectSafe(trait_def_id) => {
491 write!(w, "O{}|", (cx.ds)(trait_def_id));
496 fn enc_projection_predicate<'a, 'tcx>(w: &mut Cursor<Vec<u8>>,
498 data: &ty::ProjectionPredicate<'tcx>) {
499 enc_trait_ref(w, cx, data.projection_ty.trait_ref);
500 write!(w, "{}|", data.projection_ty.item_name);
501 enc_ty(w, cx, data.ty);