2015-03-02 14:34:16 +09:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2011-06-27 14:37:02 -07:00
|
|
|
// Type encoding
|
|
|
|
|
2014-03-21 18:05:05 -07:00
|
|
|
#![allow(unused_must_use)] // as with encoding, everything is a no-fail MemWriter
|
|
|
|
#![allow(non_camel_case_types)]
|
2014-01-29 18:42:19 -08:00
|
|
|
|
2013-12-20 20:18:52 -08:00
|
|
|
use std::cell::RefCell;
|
2015-09-17 22:12:39 +03:00
|
|
|
use std::io::Cursor;
|
2015-03-11 15:24:14 -07:00
|
|
|
use std::io::prelude::*;
|
2013-05-17 15:28:44 -07:00
|
|
|
|
2015-08-16 06:32:28 -04:00
|
|
|
use middle::def_id::DefId;
|
2014-11-18 14:22:59 +01:00
|
|
|
use middle::region;
|
2014-05-13 11:35:42 -04:00
|
|
|
use middle::subst;
|
2014-05-31 18:53:13 -04:00
|
|
|
use middle::subst::VecPerParamSpace;
|
|
|
|
use middle::ty::ParamTy;
|
2015-01-03 22:42:21 -05:00
|
|
|
use middle::ty::{self, Ty};
|
2015-11-25 00:00:26 +02:00
|
|
|
use rustc::util::nodemap::FnvHashMap;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
use rustc_front::hir;
|
|
|
|
|
2014-04-02 01:19:41 -07:00
|
|
|
use syntax::abi::Abi;
|
2015-09-14 21:58:20 +12:00
|
|
|
use syntax::ast;
|
2015-12-14 11:17:55 +13:00
|
|
|
use syntax::errors::Handler;
|
2011-06-27 14:37:02 -07:00
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
use rbml::leb128;
|
|
|
|
use encoder;
|
2013-10-21 17:11:42 -07:00
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
pub struct ctxt<'a, 'tcx: 'a> {
|
2015-12-14 11:17:55 +13:00
|
|
|
pub diag: &'a Handler,
|
2011-09-02 15:34:58 -07:00
|
|
|
// Def -> str Callback:
|
2015-08-16 06:32:28 -04:00
|
|
|
pub ds: fn(DefId) -> String,
|
2011-09-02 15:34:58 -07:00
|
|
|
// The type context.
|
2014-04-22 15:56:37 +03:00
|
|
|
pub tcx: &'a ty::ctxt<'tcx>,
|
2014-09-29 22:11:30 +03:00
|
|
|
pub abbrevs: &'a abbrev_map<'tcx>
|
2013-01-08 14:00:45 -08:00
|
|
|
}
|
2011-06-27 14:37:02 -07:00
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
impl<'a, 'tcx> encoder::EncodeContext<'a, 'tcx> {
|
|
|
|
pub fn ty_str_ctxt<'b>(&'b self) -> ctxt<'b, 'tcx> {
|
|
|
|
ctxt {
|
|
|
|
diag: self.tcx.sess.diagnostic(),
|
|
|
|
ds: encoder::def_to_string,
|
|
|
|
tcx: self.tcx,
|
|
|
|
abbrevs: &self.type_abbrevs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-11 16:21:46 -07:00
|
|
|
// Compact string representation for Ty values. API TyStr & parse_from_str.
|
2011-06-27 14:37:02 -07:00
|
|
|
// Extra parameters are for converting to/from def_ids in the string rep.
|
|
|
|
// Whatever format you choose should not contain pipe characters.
|
2013-02-19 02:40:42 -05:00
|
|
|
pub struct ty_abbrev {
|
2015-09-17 22:12:39 +03:00
|
|
|
s: Vec<u8>
|
2013-02-19 02:40:42 -05:00
|
|
|
}
|
2011-06-27 14:37:02 -07:00
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub type abbrev_map<'tcx> = RefCell<FnvHashMap<Ty<'tcx>, ty_abbrev>>;
|
2011-06-27 14:37:02 -07:00
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
pub fn enc_ty<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx>) {
|
2014-11-06 12:25:16 -05:00
|
|
|
match cx.abbrevs.borrow_mut().get(&t) {
|
2015-12-25 13:59:02 -05:00
|
|
|
Some(a) => { w.write_all(&a.s); return; }
|
2014-05-28 09:24:28 -07:00
|
|
|
None => {}
|
|
|
|
}
|
2015-03-02 14:34:16 +09:00
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
let pos = w.position();
|
2014-12-16 18:38:06 +02:00
|
|
|
|
|
|
|
match t.sty {
|
2015-12-25 13:59:02 -05:00
|
|
|
ty::TyBool => { write!(w, "b"); }
|
|
|
|
ty::TyChar => { write!(w, "c"); }
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyInt(t) => {
|
2014-12-16 18:38:06 +02:00
|
|
|
match t {
|
2016-02-08 16:20:57 +01:00
|
|
|
ast::IntTy::Is => write!(w, "is"),
|
|
|
|
ast::IntTy::I8 => write!(w, "MB"),
|
|
|
|
ast::IntTy::I16 => write!(w, "MW"),
|
|
|
|
ast::IntTy::I32 => write!(w, "ML"),
|
|
|
|
ast::IntTy::I64 => write!(w, "MD")
|
2015-12-25 13:59:02 -05:00
|
|
|
};
|
2014-12-16 18:38:06 +02:00
|
|
|
}
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyUint(t) => {
|
2014-12-16 18:38:06 +02:00
|
|
|
match t {
|
2016-02-08 16:20:57 +01:00
|
|
|
ast::UintTy::Us => write!(w, "us"),
|
|
|
|
ast::UintTy::U8 => write!(w, "Mb"),
|
|
|
|
ast::UintTy::U16 => write!(w, "Mw"),
|
|
|
|
ast::UintTy::U32 => write!(w, "Ml"),
|
|
|
|
ast::UintTy::U64 => write!(w, "Md")
|
2015-12-25 13:59:02 -05:00
|
|
|
};
|
2014-12-16 18:38:06 +02:00
|
|
|
}
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyFloat(t) => {
|
2014-12-16 18:38:06 +02:00
|
|
|
match t {
|
2016-02-08 16:09:01 +01:00
|
|
|
ast::FloatTy::F32 => write!(w, "Mf"),
|
|
|
|
ast::FloatTy::F64 => write!(w, "MF"),
|
2015-12-25 13:59:02 -05:00
|
|
|
};
|
2014-12-16 18:38:06 +02:00
|
|
|
}
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyEnum(def, substs) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "t[{}|", (cx.ds)(def.did));
|
2014-12-16 18:38:06 +02:00
|
|
|
enc_substs(w, cx, substs);
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "]");
|
2014-12-16 18:38:06 +02:00
|
|
|
}
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyTrait(box ty::TraitTy { ref principal,
|
2014-12-16 18:38:06 +02:00
|
|
|
ref bounds }) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "x[");
|
2015-04-21 18:59:58 +03:00
|
|
|
enc_trait_ref(w, cx, principal.0);
|
2014-12-16 18:38:06 +02:00
|
|
|
enc_existential_bounds(w, cx, bounds);
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "]");
|
2014-12-16 18:38:06 +02:00
|
|
|
}
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyTuple(ref ts) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "T[");
|
2015-01-31 12:20:46 -05:00
|
|
|
for t in ts { enc_ty(w, cx, *t); }
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "]");
|
2014-12-16 18:38:06 +02:00
|
|
|
}
|
2015-12-25 13:59:02 -05:00
|
|
|
ty::TyBox(typ) => { write!(w, "~"); enc_ty(w, cx, typ); }
|
|
|
|
ty::TyRawPtr(mt) => { write!(w, "*"); enc_mt(w, cx, mt); }
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyRef(r, mt) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "&");
|
2014-12-26 22:33:56 +11:00
|
|
|
enc_region(w, cx, *r);
|
2014-12-16 18:38:06 +02:00
|
|
|
enc_mt(w, cx, mt);
|
|
|
|
}
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyArray(t, sz) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "V");
|
2014-12-16 18:38:06 +02:00
|
|
|
enc_ty(w, cx, t);
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "/{}|", sz);
|
2015-06-12 16:50:13 -07:00
|
|
|
}
|
|
|
|
ty::TySlice(t) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "V");
|
2015-06-12 16:50:13 -07:00
|
|
|
enc_ty(w, cx, t);
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "/|");
|
2014-12-16 18:38:06 +02:00
|
|
|
}
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyStr => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "v");
|
2014-12-16 18:38:06 +02:00
|
|
|
}
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyBareFn(Some(def_id), f) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "F");
|
|
|
|
write!(w, "{}|", (cx.ds)(def_id));
|
2014-11-26 05:53:15 -05:00
|
|
|
enc_bare_fn_ty(w, cx, f);
|
|
|
|
}
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyBareFn(None, f) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "G");
|
2014-12-16 18:38:06 +02:00
|
|
|
enc_bare_fn_ty(w, cx, f);
|
|
|
|
}
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyInfer(_) => {
|
2015-12-14 11:17:55 +13:00
|
|
|
cx.diag.bug("cannot encode inference variable types");
|
2014-12-16 18:38:06 +02:00
|
|
|
}
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyParam(ParamTy {space, idx, name}) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "p[{}|{}|{}]", idx, space.to_uint(), name);
|
2014-12-16 18:38:06 +02:00
|
|
|
}
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyStruct(def, substs) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "a[{}|", (cx.ds)(def.did));
|
2014-12-16 18:38:06 +02:00
|
|
|
enc_substs(w, cx, substs);
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "]");
|
2014-12-16 18:38:06 +02:00
|
|
|
}
|
2015-07-16 09:46:35 -04:00
|
|
|
ty::TyClosure(def, ref substs) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "k[{}|", (cx.ds)(def));
|
2015-07-16 09:46:35 -04:00
|
|
|
enc_substs(w, cx, &substs.func_substs);
|
|
|
|
for ty in &substs.upvar_tys {
|
2015-07-16 05:32:45 -04:00
|
|
|
enc_ty(w, cx, ty);
|
|
|
|
}
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, ".");
|
|
|
|
write!(w, "]");
|
2014-12-16 18:38:06 +02:00
|
|
|
}
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyProjection(ref data) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "P[");
|
2015-04-21 18:59:58 +03:00
|
|
|
enc_trait_ref(w, cx, data.trait_ref);
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "{}]", data.item_name);
|
2014-12-17 14:16:28 -05:00
|
|
|
}
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyError => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "e");
|
2014-12-16 18:38:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
let end = w.position();
|
2014-05-28 09:24:28 -07:00
|
|
|
let len = end - pos;
|
2015-09-17 22:12:39 +03:00
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
let mut abbrev = Cursor::new(Vec::with_capacity(16));
|
2015-09-17 22:12:39 +03:00
|
|
|
abbrev.write_all(b"#");
|
2015-12-25 13:59:02 -05:00
|
|
|
{
|
|
|
|
let start_position = abbrev.position() as usize;
|
|
|
|
let bytes_written = leb128::write_unsigned_leb128(abbrev.get_mut(),
|
|
|
|
start_position,
|
|
|
|
pos);
|
|
|
|
abbrev.set_position((start_position + bytes_written) as u64);
|
|
|
|
}
|
2015-09-17 22:12:39 +03:00
|
|
|
|
2015-09-17 20:50:27 +03:00
|
|
|
cx.abbrevs.borrow_mut().insert(t, ty_abbrev {
|
2015-09-17 22:12:39 +03:00
|
|
|
s: if abbrev.position() < len {
|
|
|
|
abbrev.get_ref()[..abbrev.position() as usize].to_owned()
|
2015-09-17 20:50:27 +03:00
|
|
|
} else {
|
|
|
|
// if the abbreviation is longer than the real type,
|
|
|
|
// don't use #-notation. However, insert it here so
|
|
|
|
// other won't have to `mark_stable_position`
|
2015-12-25 13:59:02 -05:00
|
|
|
w.get_ref()[pos as usize .. end as usize].to_owned()
|
2015-09-17 20:50:27 +03:00
|
|
|
}
|
|
|
|
});
|
2014-05-28 09:24:28 -07:00
|
|
|
}
|
2013-04-02 03:40:57 -04:00
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
fn enc_mutability(w: &mut Cursor<Vec<u8>>, mt: hir::Mutability) {
|
2013-04-02 03:40:57 -04:00
|
|
|
match mt {
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::MutImmutable => (),
|
2015-12-25 13:59:02 -05:00
|
|
|
hir::MutMutable => {
|
|
|
|
write!(w, "m");
|
|
|
|
}
|
|
|
|
};
|
2013-04-02 03:40:57 -04:00
|
|
|
}
|
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
fn enc_mt<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
|
2015-07-10 18:27:06 -07:00
|
|
|
mt: ty::TypeAndMut<'tcx>) {
|
2013-04-02 03:40:57 -04:00
|
|
|
enc_mutability(w, mt.mutbl);
|
2011-06-27 14:37:02 -07:00
|
|
|
enc_ty(w, cx, mt.ty);
|
|
|
|
}
|
2012-04-18 21:26:25 -07:00
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
fn enc_opt<T, F>(w: &mut Cursor<Vec<u8>>, t: Option<T>, enc_f: F) where
|
|
|
|
F: FnOnce(&mut Cursor<Vec<u8>>, T),
|
2014-12-08 20:26:43 -05:00
|
|
|
{
|
2013-04-17 12:15:37 -04:00
|
|
|
match t {
|
2015-12-25 13:59:02 -05:00
|
|
|
None => {
|
|
|
|
write!(w, "n");
|
|
|
|
}
|
2013-10-21 17:11:42 -07:00
|
|
|
Some(v) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "s");
|
2013-12-20 17:36:07 -08:00
|
|
|
enc_f(w, v);
|
2013-10-21 17:11:42 -07:00
|
|
|
}
|
2012-04-01 14:28:30 -07:00
|
|
|
}
|
|
|
|
}
|
2012-04-18 21:26:25 -07:00
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
fn enc_vec_per_param_space<'a, 'tcx, T, F>(w: &mut Cursor<Vec<u8>>,
|
2014-12-08 20:26:43 -05:00
|
|
|
cx: &ctxt<'a, 'tcx>,
|
|
|
|
v: &VecPerParamSpace<T>,
|
|
|
|
mut op: F) where
|
2015-12-25 13:59:02 -05:00
|
|
|
F: FnMut(&mut Cursor<Vec<u8>>, &ctxt<'a, 'tcx>, &T),
|
2014-12-08 20:26:43 -05:00
|
|
|
{
|
2015-01-31 12:20:46 -05:00
|
|
|
for &space in &subst::ParamSpace::all() {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "[");
|
2015-01-31 12:20:46 -05:00
|
|
|
for t in v.get_slice(space) {
|
2014-05-31 18:53:13 -04:00
|
|
|
op(w, cx, t);
|
|
|
|
}
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "]");
|
2014-05-31 18:53:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
pub fn enc_substs<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
|
2014-09-29 22:11:30 +03:00
|
|
|
substs: &subst::Substs<'tcx>) {
|
2013-07-24 16:52:57 -04:00
|
|
|
enc_region_substs(w, cx, &substs.regions);
|
2014-05-31 18:53:13 -04:00
|
|
|
enc_vec_per_param_space(w, cx, &substs.types,
|
|
|
|
|w, cx, &ty| enc_ty(w, cx, ty));
|
2012-04-18 21:26:25 -07:00
|
|
|
}
|
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
fn enc_region_substs(w: &mut Cursor<Vec<u8>>, cx: &ctxt, substs: &subst::RegionSubsts) {
|
2013-07-24 16:52:57 -04:00
|
|
|
match *substs {
|
2014-05-13 11:35:42 -04:00
|
|
|
subst::ErasedRegions => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "e");
|
2013-07-24 16:52:57 -04:00
|
|
|
}
|
2014-05-13 11:35:42 -04:00
|
|
|
subst::NonerasedRegions(ref regions) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "n");
|
2014-05-31 18:53:13 -04:00
|
|
|
enc_vec_per_param_space(w, cx, regions,
|
|
|
|
|w, cx, &r| enc_region(w, cx, r));
|
2013-07-24 16:52:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
pub fn enc_region(w: &mut Cursor<Vec<u8>>, cx: &ctxt, r: ty::Region) {
|
2012-08-06 12:34:08 -07:00
|
|
|
match r {
|
2013-10-29 10:34:11 -04:00
|
|
|
ty::ReLateBound(id, br) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "b[{}|", id.depth);
|
2013-10-21 17:11:42 -07:00
|
|
|
enc_bound_region(w, cx, br);
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "]");
|
2013-10-29 06:03:32 -04:00
|
|
|
}
|
2015-04-15 09:13:20 -04:00
|
|
|
ty::ReEarlyBound(ref data) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "B[{}|{}|{}]",
|
|
|
|
data.space.to_uint(),
|
|
|
|
data.index,
|
|
|
|
data.name);
|
2013-10-21 17:11:42 -07:00
|
|
|
}
|
2013-10-29 10:34:11 -04:00
|
|
|
ty::ReFree(ref fr) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "f[");
|
2015-08-22 17:39:21 +03:00
|
|
|
enc_scope(w, cx, fr.scope);
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "|");
|
2013-10-21 17:11:42 -07:00
|
|
|
enc_bound_region(w, cx, fr.bound_region);
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "]");
|
2013-10-21 17:11:42 -07:00
|
|
|
}
|
2014-11-18 14:22:59 +01:00
|
|
|
ty::ReScope(scope) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "s");
|
2014-11-18 14:22:59 +01:00
|
|
|
enc_scope(w, cx, scope);
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "|");
|
2013-10-29 06:03:32 -04:00
|
|
|
}
|
2013-10-29 10:34:11 -04:00
|
|
|
ty::ReStatic => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "t");
|
2013-10-29 06:03:32 -04:00
|
|
|
}
|
2013-10-29 10:34:11 -04:00
|
|
|
ty::ReEmpty => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "e");
|
2013-10-29 06:03:32 -04:00
|
|
|
}
|
2015-08-18 23:21:29 +03:00
|
|
|
ty::ReVar(_) | ty::ReSkolemized(..) => {
|
2013-10-21 17:11:42 -07:00
|
|
|
// these should not crop up after typeck
|
2015-12-14 11:17:55 +13:00
|
|
|
cx.diag.bug("cannot encode region variables");
|
2013-10-21 17:11:42 -07:00
|
|
|
}
|
2012-04-18 21:26:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
fn enc_scope(w: &mut Cursor<Vec<u8>>, cx: &ctxt, scope: region::CodeExtent) {
|
2015-08-20 01:46:28 +03:00
|
|
|
match cx.tcx.region_maps.code_extent_data(scope) {
|
2015-12-08 23:38:36 +01:00
|
|
|
region::CodeExtentData::CallSiteScope {
|
2015-12-25 13:59:02 -05:00
|
|
|
fn_id, body_id } => write!(w, "C[{}|{}]", fn_id, body_id),
|
2015-08-20 01:46:28 +03:00
|
|
|
region::CodeExtentData::ParameterScope {
|
2015-12-25 13:59:02 -05:00
|
|
|
fn_id, body_id } => write!(w, "P[{}|{}]", fn_id, body_id),
|
|
|
|
region::CodeExtentData::Misc(node_id) => write!(w, "M{}", node_id),
|
2015-08-20 01:46:28 +03:00
|
|
|
region::CodeExtentData::Remainder(region::BlockRemainder {
|
2015-12-25 13:59:02 -05:00
|
|
|
block: b, first_statement_index: i }) => write!(w, "B[{}|{}]", b, i),
|
|
|
|
region::CodeExtentData::DestructionScope(node_id) => write!(w, "D{}", node_id),
|
|
|
|
};
|
2014-11-18 14:22:59 +01:00
|
|
|
}
|
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
fn enc_bound_region(w: &mut Cursor<Vec<u8>>, cx: &ctxt, br: ty::BoundRegion) {
|
2012-08-06 12:34:08 -07:00
|
|
|
match br {
|
2013-10-29 10:34:11 -04:00
|
|
|
ty::BrAnon(idx) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "a{}|", idx);
|
2013-10-29 06:03:32 -04:00
|
|
|
}
|
2014-03-05 06:59:35 +01:00
|
|
|
ty::BrNamed(d, name) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "[{}|{}]",
|
2013-10-29 06:03:32 -04:00
|
|
|
(cx.ds)(d),
|
2015-07-28 18:07:20 +02:00
|
|
|
name);
|
2013-10-29 06:03:32 -04:00
|
|
|
}
|
2013-10-29 10:34:11 -04:00
|
|
|
ty::BrFresh(id) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "f{}|", id);
|
2013-10-29 06:03:32 -04:00
|
|
|
}
|
2014-10-07 20:04:45 -07:00
|
|
|
ty::BrEnv => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "e|");
|
2014-10-07 20:04:45 -07:00
|
|
|
}
|
2012-03-08 14:05:16 -08:00
|
|
|
}
|
|
|
|
}
|
2012-04-10 18:32:51 -07:00
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
pub fn enc_trait_ref<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
|
2015-04-21 18:59:58 +03:00
|
|
|
s: ty::TraitRef<'tcx>) {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "{}|", (cx.ds)(s.def_id));
|
2014-12-03 16:01:29 -08:00
|
|
|
enc_substs(w, cx, s.substs);
|
2013-03-27 06:16:28 -04:00
|
|
|
}
|
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
fn enc_unsafety(w: &mut Cursor<Vec<u8>>, p: hir::Unsafety) {
|
2012-08-06 12:34:08 -07:00
|
|
|
match p {
|
2015-12-25 13:59:02 -05:00
|
|
|
hir::Unsafety::Normal => write!(w, "n"),
|
|
|
|
hir::Unsafety::Unsafe => write!(w, "u"),
|
|
|
|
};
|
2012-05-24 23:44:58 -07:00
|
|
|
}
|
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
fn enc_abi(w: &mut Cursor<Vec<u8>>, abi: Abi) {
|
|
|
|
write!(w, "[");
|
|
|
|
write!(w, "{}", abi.name());
|
|
|
|
write!(w, "]");
|
2013-01-31 17:12:29 -08:00
|
|
|
}
|
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
pub fn enc_bare_fn_ty<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
|
2014-09-29 22:11:30 +03:00
|
|
|
ft: &ty::BareFnTy<'tcx>) {
|
2014-12-09 10:36:46 -05:00
|
|
|
enc_unsafety(w, ft.unsafety);
|
2014-04-02 01:19:41 -07:00
|
|
|
enc_abi(w, ft.abi);
|
2013-01-31 17:12:29 -08:00
|
|
|
enc_fn_sig(w, cx, &ft.sig);
|
|
|
|
}
|
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
pub fn enc_closure_ty<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
|
2014-09-29 22:11:30 +03:00
|
|
|
ft: &ty::ClosureTy<'tcx>) {
|
2014-12-09 10:36:46 -05:00
|
|
|
enc_unsafety(w, ft.unsafety);
|
2013-01-31 17:12:29 -08:00
|
|
|
enc_fn_sig(w, cx, &ft.sig);
|
2014-05-28 22:26:56 -07:00
|
|
|
enc_abi(w, ft.abi);
|
2013-01-31 17:12:29 -08:00
|
|
|
}
|
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
fn enc_fn_sig<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
|
2014-12-12 11:28:35 -05:00
|
|
|
fsig: &ty::PolyFnSig<'tcx>) {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "[");
|
2015-01-31 12:20:46 -05:00
|
|
|
for ty in &fsig.0.inputs {
|
2013-04-26 19:13:38 -07:00
|
|
|
enc_ty(w, cx, *ty);
|
2011-06-27 14:37:02 -07:00
|
|
|
}
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "]");
|
2014-12-12 11:28:35 -05:00
|
|
|
if fsig.0.variadic {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "V");
|
2013-10-29 06:03:32 -04:00
|
|
|
} else {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "N");
|
2013-10-25 01:56:34 -04:00
|
|
|
}
|
2014-12-12 11:28:35 -05:00
|
|
|
match fsig.0.output {
|
2014-10-24 21:14:37 +02:00
|
|
|
ty::FnConverging(result_type) => {
|
|
|
|
enc_ty(w, cx, result_type);
|
|
|
|
}
|
|
|
|
ty::FnDiverging => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "z");
|
2014-10-24 21:14:37 +02:00
|
|
|
}
|
|
|
|
}
|
2011-06-27 14:37:02 -07:00
|
|
|
}
|
2011-07-19 17:52:34 -07:00
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
pub fn enc_builtin_bounds(w: &mut Cursor<Vec<u8>>, _cx: &ctxt, bs: &ty::BuiltinBounds) {
|
2015-01-31 12:20:46 -05:00
|
|
|
for bound in bs {
|
2013-05-07 17:30:21 -04:00
|
|
|
match bound {
|
2015-12-25 13:59:02 -05:00
|
|
|
ty::BoundSend => write!(w, "S"),
|
|
|
|
ty::BoundSized => write!(w, "Z"),
|
|
|
|
ty::BoundCopy => write!(w, "P"),
|
|
|
|
ty::BoundSync => write!(w, "T"),
|
|
|
|
};
|
2013-07-26 13:53:29 +09:00
|
|
|
}
|
2013-05-07 17:30:21 -04:00
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, ".");
|
2014-08-27 21:46:52 -04:00
|
|
|
}
|
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
pub fn enc_existential_bounds<'a,'tcx>(w: &mut Cursor<Vec<u8>>,
|
2014-12-26 04:36:04 -05:00
|
|
|
cx: &ctxt<'a,'tcx>,
|
|
|
|
bs: &ty::ExistentialBounds<'tcx>) {
|
2014-08-27 21:46:52 -04:00
|
|
|
enc_builtin_bounds(w, cx, &bs.builtin_bounds);
|
|
|
|
|
2015-06-16 15:53:13 -07:00
|
|
|
enc_region(w, cx, bs.region_bound);
|
2014-12-17 14:16:28 -05:00
|
|
|
|
2015-01-31 12:20:46 -05:00
|
|
|
for tp in &bs.projection_bounds {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "P");
|
2014-12-17 14:16:28 -05:00
|
|
|
enc_projection_predicate(w, cx, &tp.0);
|
2013-05-07 17:30:21 -04:00
|
|
|
}
|
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, ".");
|
2011-12-28 17:50:12 +01:00
|
|
|
}
|
2011-06-27 14:37:02 -07:00
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
pub fn enc_type_param_def<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
|
2014-09-29 22:11:30 +03:00
|
|
|
v: &ty::TypeParameterDef<'tcx>) {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "{}:{}|{}|{}|{}|",
|
2015-07-28 18:07:20 +02:00
|
|
|
v.name, (cx.ds)(v.def_id),
|
2015-07-16 11:26:02 -07:00
|
|
|
v.space.to_uint(), v.index, (cx.ds)(v.default_def_id));
|
2014-01-30 19:28:02 +02:00
|
|
|
enc_opt(w, v.default, |w, t| enc_ty(w, cx, t));
|
2015-02-11 16:37:36 -05:00
|
|
|
enc_object_lifetime_default(w, cx, v.object_lifetime_default);
|
|
|
|
}
|
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
pub fn enc_region_param_def(w: &mut Cursor<Vec<u8>>, cx: &ctxt,
|
2015-08-20 01:46:28 +03:00
|
|
|
v: &ty::RegionParameterDef) {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "{}:{}|{}|{}|",
|
2015-08-20 01:46:28 +03:00
|
|
|
v.name, (cx.ds)(v.def_id),
|
|
|
|
v.space.to_uint(), v.index);
|
|
|
|
for &r in &v.bounds {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "R");
|
2015-08-20 01:46:28 +03:00
|
|
|
enc_region(w, cx, r);
|
|
|
|
}
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, ".");
|
2015-08-20 01:46:28 +03:00
|
|
|
}
|
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
fn enc_object_lifetime_default<'a, 'tcx>(w: &mut Cursor<Vec<u8>>,
|
2015-02-11 16:37:36 -05:00
|
|
|
cx: &ctxt<'a, 'tcx>,
|
2015-06-17 10:02:32 -04:00
|
|
|
default: ty::ObjectLifetimeDefault)
|
2015-02-11 16:37:36 -05:00
|
|
|
{
|
|
|
|
match default {
|
2015-12-25 13:59:02 -05:00
|
|
|
ty::ObjectLifetimeDefault::Ambiguous => {
|
|
|
|
write!(w, "a");
|
|
|
|
}
|
|
|
|
ty::ObjectLifetimeDefault::BaseDefault => {
|
|
|
|
write!(w, "b");
|
|
|
|
}
|
2015-06-17 10:02:32 -04:00
|
|
|
ty::ObjectLifetimeDefault::Specific(r) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "s");
|
2015-02-11 16:37:36 -05:00
|
|
|
enc_region(w, cx, r);
|
|
|
|
}
|
|
|
|
}
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-08 22:54:49 -07:00
|
|
|
}
|
2014-12-07 11:10:48 -05:00
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
pub fn enc_predicate<'a, 'tcx>(w: &mut Cursor<Vec<u8>>,
|
2014-12-07 11:10:48 -05:00
|
|
|
cx: &ctxt<'a, 'tcx>,
|
|
|
|
p: &ty::Predicate<'tcx>)
|
|
|
|
{
|
|
|
|
match *p {
|
|
|
|
ty::Predicate::Trait(ref trait_ref) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "t");
|
2015-04-21 18:59:58 +03:00
|
|
|
enc_trait_ref(w, cx, trait_ref.0.trait_ref);
|
2014-12-07 11:10:48 -05:00
|
|
|
}
|
2014-12-13 05:34:34 -05:00
|
|
|
ty::Predicate::Equate(ty::Binder(ty::EquatePredicate(a, b))) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "e");
|
2014-12-07 11:10:48 -05:00
|
|
|
enc_ty(w, cx, a);
|
|
|
|
enc_ty(w, cx, b);
|
|
|
|
}
|
2014-12-13 05:34:34 -05:00
|
|
|
ty::Predicate::RegionOutlives(ty::Binder(ty::OutlivesPredicate(a, b))) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "r");
|
2014-12-07 11:10:48 -05:00
|
|
|
enc_region(w, cx, a);
|
|
|
|
enc_region(w, cx, b);
|
|
|
|
}
|
2014-12-13 05:34:34 -05:00
|
|
|
ty::Predicate::TypeOutlives(ty::Binder(ty::OutlivesPredicate(a, b))) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "o");
|
2014-12-07 11:10:48 -05:00
|
|
|
enc_ty(w, cx, a);
|
|
|
|
enc_region(w, cx, b);
|
|
|
|
}
|
2014-12-17 14:16:28 -05:00
|
|
|
ty::Predicate::Projection(ty::Binder(ref data)) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "p");
|
|
|
|
enc_projection_predicate(w, cx, data);
|
2014-12-17 14:16:28 -05:00
|
|
|
}
|
2015-08-07 09:30:19 -04:00
|
|
|
ty::Predicate::WellFormed(data) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "w");
|
2015-08-07 09:30:19 -04:00
|
|
|
enc_ty(w, cx, data);
|
|
|
|
}
|
|
|
|
ty::Predicate::ObjectSafe(trait_def_id) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "O{}|", (cx.ds)(trait_def_id));
|
2015-08-07 09:30:19 -04:00
|
|
|
}
|
2014-12-07 11:10:48 -05:00
|
|
|
}
|
|
|
|
}
|
2014-12-17 14:16:28 -05:00
|
|
|
|
2015-12-25 13:59:02 -05:00
|
|
|
fn enc_projection_predicate<'a, 'tcx>(w: &mut Cursor<Vec<u8>>,
|
2014-12-17 14:16:28 -05:00
|
|
|
cx: &ctxt<'a, 'tcx>,
|
|
|
|
data: &ty::ProjectionPredicate<'tcx>) {
|
2015-04-21 18:59:58 +03:00
|
|
|
enc_trait_ref(w, cx, data.projection_ty.trait_ref);
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "{}|", data.projection_ty.item_name);
|
2014-12-17 14:16:28 -05:00
|
|
|
enc_ty(w, cx, data.ty);
|
|
|
|
}
|