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
|
|
|
|
2016-03-29 12:54:26 +03:00
|
|
|
use rustc::hir::def_id::DefId;
|
2014-11-18 14:22:59 +01:00
|
|
|
use middle::region;
|
2016-08-17 06:32:00 +03:00
|
|
|
use rustc::ty::subst::Substs;
|
2016-03-22 17:30:57 +02:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt};
|
2015-11-25 00:00:26 +02:00
|
|
|
use rustc::util::nodemap::FnvHashMap;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2016-03-29 08:50:44 +03:00
|
|
|
use rustc::hir;
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2014-04-02 01:19:41 -07:00
|
|
|
use syntax::abi::Abi;
|
2015-09-14 21:58:20 +12:00
|
|
|
use syntax::ast;
|
2016-06-21 18:08:13 -04:00
|
|
|
use 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:
|
2016-05-03 05:23:22 +03:00
|
|
|
pub ds: for<'b> fn(TyCtxt<'b, 'tcx, 'tcx>, DefId) -> String,
|
2011-09-02 15:34:58 -07:00
|
|
|
// The type context.
|
2016-05-03 05:23:22 +03:00
|
|
|
pub tcx: TyCtxt<'a, 'tcx, '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>) {
|
2016-07-03 14:38:37 -07:00
|
|
|
if let Some(a) = cx.abbrevs.borrow_mut().get(&t) {
|
|
|
|
w.write_all(&a.s);
|
|
|
|
return;
|
2014-05-28 09:24:28 -07:00
|
|
|
}
|
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"); }
|
2016-08-02 15:56:20 +08:00
|
|
|
ty::TyNever => { write!(w, "!"); }
|
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
|
|
|
}
|
2016-08-04 15:52:57 +03:00
|
|
|
ty::TyTrait(ref obj) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "x[");
|
2016-08-04 15:52:57 +03:00
|
|
|
enc_existential_trait_ref(w, cx, obj.principal.0);
|
|
|
|
enc_builtin_bounds(w, cx, &obj.builtin_bounds);
|
|
|
|
|
|
|
|
enc_region(w, cx, obj.region_bound);
|
|
|
|
|
|
|
|
// Encode projection_bounds in a stable order
|
|
|
|
let mut projection_bounds: Vec<_> = obj.projection_bounds
|
|
|
|
.iter()
|
|
|
|
.map(|b| (b.item_name().as_str(), b))
|
|
|
|
.collect();
|
|
|
|
projection_bounds.sort_by_key(|&(ref name, _)| name.clone());
|
|
|
|
|
|
|
|
for tp in projection_bounds.iter().map(|&(_, tp)| tp) {
|
|
|
|
write!(w, "P");
|
|
|
|
enc_existential_projection(w, cx, &tp.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(w, ".");
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "]");
|
2014-12-16 18:38:06 +02:00
|
|
|
}
|
2016-04-29 08:30:54 +03:00
|
|
|
ty::TyTuple(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, "&");
|
2016-08-25 23:58:52 +03: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
|
|
|
}
|
2016-02-16 18:36:41 +02:00
|
|
|
ty::TyFnDef(def_id, substs, f) => {
|
2015-12-25 13:59:02 -05:00
|
|
|
write!(w, "F");
|
2016-02-10 10:04:45 -05:00
|
|
|
write!(w, "{}|", (cx.ds)(cx.tcx, def_id));
|
2016-02-16 18:36:41 +02:00
|
|
|
enc_substs(w, cx, substs);
|
2014-11-26 05:53:15 -05:00
|
|
|
enc_bare_fn_ty(w, cx, f);
|
|
|
|
}
|
2015-06-13 13:15:03 -07:00
|
|
|
ty::TyFnPtr(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(_) => {
|
2016-03-28 23:00:01 +02:00
|
|
|
bug!("cannot encode inference variable types");
|
2014-12-16 18:38:06 +02:00
|
|
|
}
|
2016-08-17 06:32:00 +03:00
|
|
|
ty::TyParam(p) => {
|
|
|
|
write!(w, "p[{}|{}]", p.idx, p.name);
|
2014-12-16 18:38:06 +02:00
|
|
|
}
|
2016-09-06 01:26:02 +03:00
|
|
|
ty::TyAdt(def, substs) => {
|
2016-02-10 10:04:45 -05:00
|
|
|
write!(w, "a[{}|", (cx.ds)(cx.tcx, 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
|
|
|
}
|
2016-04-29 08:30:54 +03:00
|
|
|
ty::TyClosure(def, substs) => {
|
2016-02-10 10:04:45 -05:00
|
|
|
write!(w, "k[{}|", (cx.ds)(cx.tcx, def));
|
2016-04-29 08:30:54 +03: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
|
|
|
}
|
2016-07-22 18:56:22 +03:00
|
|
|
ty::TyAnon(def_id, substs) => {
|
|
|
|
write!(w, "A[{}|", (cx.ds)(cx.tcx, def_id));
|
|
|
|
enc_substs(w, cx, substs);
|
|
|
|
write!(w, "]");
|
|
|
|
}
|
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
|
|
|
pub fn enc_substs<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
|
2016-08-08 23:39:49 +03:00
|
|
|
substs: &Substs<'tcx>) {
|
2016-08-17 06:32:00 +03:00
|
|
|
write!(w, "[");
|
2016-08-27 01:13:48 +03:00
|
|
|
for &k in substs.params() {
|
|
|
|
if let Some(ty) = k.as_type() {
|
|
|
|
write!(w, "t");
|
|
|
|
enc_ty(w, cx, ty);
|
|
|
|
} else if let Some(r) = k.as_region() {
|
|
|
|
write!(w, "r");
|
|
|
|
enc_region(w, cx, r);
|
|
|
|
} else {
|
|
|
|
bug!()
|
|
|
|
}
|
2016-08-17 06:32:00 +03:00
|
|
|
}
|
|
|
|
write!(w, "]");
|
2012-04-18 21:26:25 -07:00
|
|
|
}
|
|
|
|
|
2016-08-08 23:39:49 +03:00
|
|
|
pub fn enc_generics<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
|
|
|
|
generics: &ty::Generics<'tcx>) {
|
2016-08-10 20:39:09 +03:00
|
|
|
enc_opt(w, generics.parent, |w, def_id| {
|
|
|
|
write!(w, "{}|", (cx.ds)(cx.tcx, def_id));
|
|
|
|
});
|
|
|
|
write!(w, "{}|{}[",
|
|
|
|
generics.parent_regions,
|
|
|
|
generics.parent_types);
|
|
|
|
|
|
|
|
for r in &generics.regions {
|
|
|
|
enc_region_param_def(w, cx, r)
|
|
|
|
}
|
2016-08-17 06:32:00 +03:00
|
|
|
write!(w, "|");
|
2016-08-10 20:39:09 +03:00
|
|
|
for t in &generics.types {
|
|
|
|
enc_type_param_def(w, cx, t);
|
|
|
|
}
|
|
|
|
write!(w, "]");
|
2016-08-08 23:39:49 +03:00
|
|
|
|
|
|
|
if generics.has_self {
|
|
|
|
write!(w, "S");
|
|
|
|
} else {
|
|
|
|
write!(w, "N");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-25 23:58:52 +03:00
|
|
|
pub fn enc_region(w: &mut Cursor<Vec<u8>>, cx: &ctxt, r: &ty::Region) {
|
|
|
|
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) => {
|
2016-08-17 06:32:00 +03:00
|
|
|
write!(w, "B[{}|{}]",
|
2015-12-25 13:59:02 -05:00
|
|
|
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
|
|
|
}
|
2016-06-01 15:10:44 +03:00
|
|
|
ty::ReErased => {
|
|
|
|
write!(w, "E");
|
|
|
|
}
|
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
|
2016-03-28 23:00:01 +02:00
|
|
|
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
|
|
|
}
|
2016-04-21 05:10:10 -04:00
|
|
|
ty::BrNamed(d, name, issue32330) => {
|
|
|
|
write!(w, "[{}|{}|",
|
|
|
|
(cx.ds)(cx.tcx, d),
|
|
|
|
name);
|
|
|
|
|
|
|
|
match issue32330 {
|
|
|
|
ty::Issue32330::WontChange =>
|
|
|
|
write!(w, "n]"),
|
|
|
|
ty::Issue32330::WillChange { fn_def_id, region_name } =>
|
|
|
|
write!(w, "y{}|{}]", (cx.ds)(cx.tcx, fn_def_id), region_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>) {
|
2016-02-10 10:04:45 -05:00
|
|
|
write!(w, "{}|", (cx.ds)(cx.tcx, 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
|
|
|
}
|
|
|
|
|
2016-08-04 15:52:57 +03:00
|
|
|
fn enc_existential_trait_ref<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
|
|
|
|
s: ty::ExistentialTraitRef<'tcx>) {
|
|
|
|
write!(w, "{}|", (cx.ds)(cx.tcx, s.def_id));
|
|
|
|
enc_substs(w, cx, s.substs);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2016-07-31 22:33:41 +08:00
|
|
|
enc_ty(w, cx, fsig.0.output);
|
2011-06-27 14:37:02 -07:00
|
|
|
}
|
2011-07-19 17:52:34 -07:00
|
|
|
|
2016-08-04 15:52:57 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-08-08 23:39:49 +03:00
|
|
|
fn enc_type_param_def<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>,
|
|
|
|
v: &ty::TypeParameterDef<'tcx>) {
|
2016-08-17 06:32:00 +03:00
|
|
|
write!(w, "{}:{}|{}|{}|",
|
|
|
|
v.name, (cx.ds)(cx.tcx, v.def_id),
|
|
|
|
v.index, (cx.ds)(cx.tcx, 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);
|
|
|
|
}
|
|
|
|
|
2016-08-08 23:39:49 +03:00
|
|
|
fn enc_region_param_def(w: &mut Cursor<Vec<u8>>, cx: &ctxt,
|
|
|
|
v: &ty::RegionParameterDef) {
|
2016-08-17 06:32:00 +03:00
|
|
|
write!(w, "{}:{}|{}|",
|
|
|
|
v.name, (cx.ds)(cx.tcx, v.def_id), v.index);
|
2015-08-20 01:46:28 +03:00
|
|
|
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");
|
2016-08-04 15:52:57 +03:00
|
|
|
enc_trait_ref(w, cx, data.projection_ty.trait_ref);
|
|
|
|
write!(w, "{}|", data.projection_ty.item_name);
|
|
|
|
enc_ty(w, cx, data.ty);
|
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) => {
|
2016-02-10 10:04:45 -05:00
|
|
|
write!(w, "O{}|", (cx.ds)(cx.tcx, trait_def_id));
|
2015-08-07 09:30:19 -04:00
|
|
|
}
|
2016-04-06 00:20:59 -07:00
|
|
|
ty::Predicate::ClosureKind(closure_def_id, kind) => {
|
|
|
|
let kind_char = match kind {
|
|
|
|
ty::ClosureKind::Fn => 'f',
|
|
|
|
ty::ClosureKind::FnMut => 'm',
|
|
|
|
ty::ClosureKind::FnOnce => 'o',
|
|
|
|
};
|
|
|
|
write!(w, "c{}|{}|", (cx.ds)(cx.tcx, closure_def_id), kind_char);
|
|
|
|
}
|
2014-12-07 11:10:48 -05:00
|
|
|
}
|
|
|
|
}
|
2014-12-17 14:16:28 -05:00
|
|
|
|
2016-08-04 15:52:57 +03:00
|
|
|
fn enc_existential_projection<'a, 'tcx>(w: &mut Cursor<Vec<u8>>,
|
|
|
|
cx: &ctxt<'a, 'tcx>,
|
|
|
|
data: &ty::ExistentialProjection<'tcx>) {
|
|
|
|
enc_existential_trait_ref(w, cx, data.trait_ref);
|
|
|
|
write!(w, "{}|", data.item_name);
|
2014-12-17 14:16:28 -05:00
|
|
|
enc_ty(w, cx, data.ty);
|
|
|
|
}
|