2014-02-10 15:36:31 +01:00
|
|
|
// Copyright 2012-2014 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-01-29 18:42:19 -08:00
|
|
|
#[allow(unused_must_use)]; // as with encoding, everything is a no-fail MemWriter
|
2014-02-10 15:36:31 +01:00
|
|
|
#[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;
|
2014-02-19 19:29:58 -08:00
|
|
|
use collections::HashMap;
|
2013-11-10 22:46:32 -08:00
|
|
|
use std::io;
|
2014-01-15 13:25:09 -08:00
|
|
|
use std::io::MemWriter;
|
2013-10-21 17:11:42 -07:00
|
|
|
use std::str;
|
|
|
|
use std::fmt;
|
2014-03-08 21:36:22 +01:00
|
|
|
use std::vec_ng::Vec;
|
2013-05-17 15:28:44 -07:00
|
|
|
|
2013-03-26 16:38:07 -04:00
|
|
|
use middle::ty::param_ty;
|
2012-12-23 17:41:37 -05:00
|
|
|
use middle::ty;
|
|
|
|
|
2013-03-13 22:25:28 -04:00
|
|
|
use syntax::abi::AbiSet;
|
2013-04-02 03:40:57 -04:00
|
|
|
use syntax::ast;
|
2012-09-04 11:54:36 -07:00
|
|
|
use syntax::ast::*;
|
2013-12-31 23:17:59 +09:00
|
|
|
use syntax::diagnostic::SpanHandler;
|
2014-02-14 07:07:09 +02:00
|
|
|
use syntax::parse::token;
|
2012-09-04 11:54:36 -07:00
|
|
|
use syntax::print::pprust::*;
|
2011-06-27 14:37:02 -07:00
|
|
|
|
2013-10-21 17:11:42 -07:00
|
|
|
macro_rules! mywrite( ($wr:expr, $($arg:tt)*) => (
|
|
|
|
format_args!(|a| { mywrite($wr, a) }, $($arg)*)
|
|
|
|
) )
|
|
|
|
|
2014-03-06 05:07:47 +02:00
|
|
|
pub struct ctxt<'a> {
|
2013-12-27 13:48:00 -08:00
|
|
|
diag: @SpanHandler,
|
2011-09-02 15:34:58 -07:00
|
|
|
// Def -> str Callback:
|
2013-08-29 15:04:09 -07:00
|
|
|
ds: extern "Rust" fn(DefId) -> ~str,
|
2011-09-02 15:34:58 -07:00
|
|
|
// The type context.
|
2014-03-06 05:07:47 +02:00
|
|
|
tcx: &'a ty::ctxt,
|
2012-04-23 23:40:53 -07:00
|
|
|
abbrevs: abbrev_ctxt
|
2013-01-08 14:00:45 -08:00
|
|
|
}
|
2011-06-27 14:37:02 -07:00
|
|
|
|
|
|
|
// Compact string representation for ty.t values. API ty_str & parse_from_str.
|
|
|
|
// 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 {
|
|
|
|
pos: uint,
|
|
|
|
len: uint,
|
2014-01-31 12:36:00 -08:00
|
|
|
s: ~str
|
2013-02-19 02:40:42 -05:00
|
|
|
}
|
2011-06-27 14:37:02 -07:00
|
|
|
|
2013-01-29 16:51:16 -08:00
|
|
|
pub enum abbrev_ctxt {
|
|
|
|
ac_no_abbrevs,
|
2013-12-20 20:18:52 -08:00
|
|
|
ac_use_abbrevs(@RefCell<HashMap<ty::t, ty_abbrev>>),
|
2013-01-29 16:51:16 -08:00
|
|
|
}
|
2011-06-27 14:37:02 -07:00
|
|
|
|
2013-12-20 17:36:07 -08:00
|
|
|
fn mywrite(w: &mut MemWriter, fmt: &fmt::Arguments) {
|
2013-10-21 17:11:42 -07:00
|
|
|
fmt::write(&mut *w as &mut io::Writer, fmt);
|
|
|
|
}
|
|
|
|
|
2014-03-06 05:28:28 +02:00
|
|
|
pub fn enc_ty(w: &mut MemWriter, cx: &ctxt, t: ty::t) {
|
2012-08-06 12:34:08 -07:00
|
|
|
match cx.abbrevs {
|
2012-08-03 19:59:04 -07:00
|
|
|
ac_no_abbrevs => {
|
2013-12-18 16:11:23 -08:00
|
|
|
let result_str_opt;
|
|
|
|
{
|
|
|
|
let short_names_cache = cx.tcx.short_names_cache.borrow();
|
|
|
|
result_str_opt = short_names_cache.get()
|
|
|
|
.find(&t)
|
2014-01-31 12:36:00 -08:00
|
|
|
.map(|result| {
|
|
|
|
(*result).clone()
|
|
|
|
});
|
2013-12-18 16:11:23 -08:00
|
|
|
}
|
|
|
|
let result_str = match result_str_opt {
|
|
|
|
Some(s) => s,
|
2012-09-14 09:40:28 -07:00
|
|
|
None => {
|
2013-12-20 17:36:07 -08:00
|
|
|
let wr = &mut MemWriter::new();
|
2013-10-21 17:11:42 -07:00
|
|
|
enc_sty(wr, cx, &ty::get(t).sty);
|
2014-01-31 12:36:00 -08:00
|
|
|
let s = str::from_utf8(wr.get_ref()).unwrap();
|
2013-12-18 16:11:23 -08:00
|
|
|
let mut short_names_cache = cx.tcx
|
|
|
|
.short_names_cache
|
|
|
|
.borrow_mut();
|
2014-01-31 12:36:00 -08:00
|
|
|
short_names_cache.get().insert(t, s.to_str());
|
|
|
|
s.to_str()
|
2013-12-18 16:11:23 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
w.write(result_str.as_bytes());
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
ac_use_abbrevs(abbrevs) => {
|
2013-12-20 20:18:52 -08:00
|
|
|
{
|
|
|
|
let mut abbrevs = abbrevs.borrow_mut();
|
|
|
|
match abbrevs.get().find(&t) {
|
|
|
|
Some(a) => { w.write(a.s.as_bytes()); return; }
|
|
|
|
None => {}
|
|
|
|
}
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2014-01-29 18:42:19 -08:00
|
|
|
let pos = w.tell().unwrap();
|
2013-07-02 12:47:32 -07:00
|
|
|
enc_sty(w, cx, &ty::get(t).sty);
|
2014-01-29 18:42:19 -08:00
|
|
|
let end = w.tell().unwrap();
|
2013-03-15 15:24:24 -04:00
|
|
|
let len = end - pos;
|
2013-10-21 17:11:42 -07:00
|
|
|
fn estimate_sz(u: u64) -> u64 {
|
2013-03-15 15:24:24 -04:00
|
|
|
let mut n = u;
|
2013-10-21 17:11:42 -07:00
|
|
|
let mut len = 0;
|
|
|
|
while n != 0 { len += 1; n = n >> 4; }
|
2013-03-15 15:24:24 -04:00
|
|
|
return len;
|
|
|
|
}
|
2013-10-21 17:11:42 -07:00
|
|
|
let abbrev_len = 3 + estimate_sz(pos) + estimate_sz(len);
|
2013-03-15 15:24:24 -04:00
|
|
|
if abbrev_len < len {
|
|
|
|
// I.e. it's actually an abbreviation.
|
2014-01-31 12:36:00 -08:00
|
|
|
let s = format!("\\#{:x}:{:x}\\#", pos, len);
|
2013-10-21 17:11:42 -07:00
|
|
|
let a = ty_abbrev { pos: pos as uint,
|
|
|
|
len: len as uint,
|
|
|
|
s: s };
|
2013-12-20 20:18:52 -08:00
|
|
|
{
|
|
|
|
let mut abbrevs = abbrevs.borrow_mut();
|
|
|
|
abbrevs.get().insert(t, a);
|
|
|
|
}
|
2013-03-15 15:24:24 -04:00
|
|
|
}
|
|
|
|
return;
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2011-06-27 14:37:02 -07:00
|
|
|
}
|
|
|
|
}
|
2013-04-02 03:40:57 -04:00
|
|
|
|
2013-12-20 17:36:07 -08:00
|
|
|
fn enc_mutability(w: &mut MemWriter, mt: ast::Mutability) {
|
2013-04-02 03:40:57 -04:00
|
|
|
match mt {
|
2013-10-21 17:11:42 -07:00
|
|
|
MutImmutable => (),
|
|
|
|
MutMutable => mywrite!(w, "m"),
|
2011-06-27 14:37:02 -07:00
|
|
|
}
|
2013-04-02 03:40:57 -04:00
|
|
|
}
|
|
|
|
|
2014-03-06 05:28:28 +02:00
|
|
|
fn enc_mt(w: &mut MemWriter, cx: &ctxt, mt: ty::mt) {
|
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
|
|
|
|
2013-12-20 17:36:07 -08:00
|
|
|
fn enc_opt<T>(w: &mut MemWriter, t: Option<T>, enc_f: |&mut MemWriter, T|) {
|
2013-04-17 12:15:37 -04:00
|
|
|
match t {
|
2013-10-21 17:11:42 -07:00
|
|
|
None => mywrite!(w, "n"),
|
|
|
|
Some(v) => {
|
|
|
|
mywrite!(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
|
|
|
|
2014-03-06 05:28:28 +02:00
|
|
|
pub fn enc_substs(w: &mut MemWriter, cx: &ctxt, substs: &ty::substs) {
|
2013-07-24 16:52:57 -04:00
|
|
|
enc_region_substs(w, cx, &substs.regions);
|
2013-12-20 17:36:07 -08:00
|
|
|
enc_opt(w, substs.self_ty, |w, t| enc_ty(w, cx, t));
|
2013-10-21 17:11:42 -07:00
|
|
|
mywrite!(w, "[");
|
2013-08-03 12:45:23 -04:00
|
|
|
for t in substs.tps.iter() { enc_ty(w, cx, *t); }
|
2013-10-21 17:11:42 -07:00
|
|
|
mywrite!(w, "]");
|
2012-04-18 21:26:25 -07:00
|
|
|
}
|
|
|
|
|
2014-03-06 05:28:28 +02:00
|
|
|
fn enc_region_substs(w: &mut MemWriter, cx: &ctxt, substs: &ty::RegionSubsts) {
|
2013-07-24 16:52:57 -04:00
|
|
|
match *substs {
|
|
|
|
ty::ErasedRegions => {
|
2013-10-21 17:11:42 -07:00
|
|
|
mywrite!(w, "e");
|
2013-07-24 16:52:57 -04:00
|
|
|
}
|
|
|
|
ty::NonerasedRegions(ref regions) => {
|
2013-10-21 17:11:42 -07:00
|
|
|
mywrite!(w, "n");
|
2013-08-03 12:45:23 -04:00
|
|
|
for &r in regions.iter() {
|
2013-07-24 16:52:57 -04:00
|
|
|
enc_region(w, cx, r);
|
|
|
|
}
|
2013-10-21 17:11:42 -07:00
|
|
|
mywrite!(w, ".");
|
2013-07-24 16:52:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-06 05:28:28 +02:00
|
|
|
fn enc_region(w: &mut MemWriter, 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) => {
|
2013-10-29 06:03:32 -04:00
|
|
|
mywrite!(w, "b[{}|", id);
|
2013-10-21 17:11:42 -07:00
|
|
|
enc_bound_region(w, cx, br);
|
2013-10-29 06:03:32 -04:00
|
|
|
mywrite!(w, "]");
|
|
|
|
}
|
2014-03-05 06:59:35 +01:00
|
|
|
ty::ReEarlyBound(node_id, index, name) => {
|
2013-10-29 06:03:32 -04:00
|
|
|
mywrite!(w, "B[{}|{}|{}]",
|
|
|
|
node_id,
|
|
|
|
index,
|
2014-03-05 06:59:35 +01:00
|
|
|
token::get_name(name));
|
2013-10-21 17:11:42 -07:00
|
|
|
}
|
2013-10-29 10:34:11 -04:00
|
|
|
ty::ReFree(ref fr) => {
|
2013-10-21 17:11:42 -07:00
|
|
|
mywrite!(w, "f[{}|", fr.scope_id);
|
|
|
|
enc_bound_region(w, cx, fr.bound_region);
|
|
|
|
mywrite!(w, "]");
|
|
|
|
}
|
2013-10-29 10:34:11 -04:00
|
|
|
ty::ReScope(nid) => {
|
2013-10-29 06:03:32 -04:00
|
|
|
mywrite!(w, "s{}|", nid);
|
|
|
|
}
|
2013-10-29 10:34:11 -04:00
|
|
|
ty::ReStatic => {
|
2013-10-29 06:03:32 -04:00
|
|
|
mywrite!(w, "t");
|
|
|
|
}
|
2013-10-29 10:34:11 -04:00
|
|
|
ty::ReEmpty => {
|
2013-10-29 06:03:32 -04:00
|
|
|
mywrite!(w, "e");
|
|
|
|
}
|
2013-10-29 10:34:11 -04:00
|
|
|
ty::ReInfer(_) => {
|
2013-10-21 17:11:42 -07:00
|
|
|
// these should not crop up after typeck
|
2014-02-06 10:38:08 +01:00
|
|
|
cx.diag.handler().bug("cannot encode region variables");
|
2013-10-21 17:11:42 -07:00
|
|
|
}
|
2012-04-18 21:26:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-06 05:28:28 +02:00
|
|
|
fn enc_bound_region(w: &mut MemWriter, 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) => {
|
2013-10-29 06:03:32 -04:00
|
|
|
mywrite!(w, "a{}|", idx);
|
|
|
|
}
|
2014-03-05 06:59:35 +01:00
|
|
|
ty::BrNamed(d, name) => {
|
2013-10-29 06:03:32 -04:00
|
|
|
mywrite!(w, "[{}|{}]",
|
|
|
|
(cx.ds)(d),
|
2014-03-05 06:59:35 +01:00
|
|
|
token::get_name(name));
|
2013-10-29 06:03:32 -04:00
|
|
|
}
|
2013-10-29 10:34:11 -04:00
|
|
|
ty::BrFresh(id) => {
|
2013-10-29 06:03:32 -04:00
|
|
|
mywrite!(w, "f{}|", id);
|
|
|
|
}
|
2012-03-08 14:05:16 -08:00
|
|
|
}
|
|
|
|
}
|
2012-04-10 18:32:51 -07:00
|
|
|
|
2014-03-06 05:28:28 +02:00
|
|
|
pub fn enc_vstore(w: &mut MemWriter, cx: &ctxt, v: ty::vstore) {
|
2013-10-21 17:11:42 -07:00
|
|
|
mywrite!(w, "/");
|
2012-08-06 12:34:08 -07:00
|
|
|
match v {
|
2013-10-21 17:11:42 -07:00
|
|
|
ty::vstore_fixed(u) => mywrite!(w, "{}|", u),
|
|
|
|
ty::vstore_uniq => mywrite!(w, "~"),
|
|
|
|
ty::vstore_slice(r) => {
|
|
|
|
mywrite!(w, "&");
|
|
|
|
enc_region(w, cx, r);
|
|
|
|
}
|
2012-04-10 18:32:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-06 05:28:28 +02:00
|
|
|
pub fn enc_trait_ref(w: &mut MemWriter, cx: &ctxt, s: &ty::TraitRef) {
|
2013-10-21 17:11:42 -07:00
|
|
|
mywrite!(w, "{}|", (cx.ds)(s.def_id));
|
2013-04-17 12:15:37 -04:00
|
|
|
enc_substs(w, cx, &s.substs);
|
2013-03-27 06:16:28 -04:00
|
|
|
}
|
|
|
|
|
2014-03-06 05:28:28 +02:00
|
|
|
pub fn enc_trait_store(w: &mut MemWriter, cx: &ctxt, s: ty::TraitStore) {
|
2013-03-08 21:16:09 -08:00
|
|
|
match s {
|
2013-10-21 17:11:42 -07:00
|
|
|
ty::UniqTraitStore => mywrite!(w, "~"),
|
2013-03-08 21:16:09 -08:00
|
|
|
ty::RegionTraitStore(re) => {
|
2013-10-21 17:11:42 -07:00
|
|
|
mywrite!(w, "&");
|
2013-03-08 21:16:09 -08:00
|
|
|
enc_region(w, cx, re);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-06 05:28:28 +02:00
|
|
|
fn enc_sty(w: &mut MemWriter, cx: &ctxt, st: &ty::sty) {
|
2013-07-02 12:47:32 -07:00
|
|
|
match *st {
|
2013-10-21 17:11:42 -07:00
|
|
|
ty::ty_nil => mywrite!(w, "n"),
|
|
|
|
ty::ty_bot => mywrite!(w, "z"),
|
|
|
|
ty::ty_bool => mywrite!(w, "b"),
|
|
|
|
ty::ty_char => mywrite!(w, "c"),
|
|
|
|
ty::ty_int(t) => {
|
|
|
|
match t {
|
2014-01-09 15:05:33 +02:00
|
|
|
TyI => mywrite!(w, "i"),
|
|
|
|
TyI8 => mywrite!(w, "MB"),
|
|
|
|
TyI16 => mywrite!(w, "MW"),
|
|
|
|
TyI32 => mywrite!(w, "ML"),
|
|
|
|
TyI64 => mywrite!(w, "MD")
|
2013-10-21 17:11:42 -07:00
|
|
|
}
|
2011-12-07 21:06:12 +01:00
|
|
|
}
|
2013-10-21 17:11:42 -07:00
|
|
|
ty::ty_uint(t) => {
|
|
|
|
match t {
|
2014-01-09 15:05:33 +02:00
|
|
|
TyU => mywrite!(w, "u"),
|
|
|
|
TyU8 => mywrite!(w, "Mb"),
|
|
|
|
TyU16 => mywrite!(w, "Mw"),
|
|
|
|
TyU32 => mywrite!(w, "Ml"),
|
|
|
|
TyU64 => mywrite!(w, "Md")
|
2013-10-21 17:11:42 -07:00
|
|
|
}
|
2011-12-07 21:06:12 +01:00
|
|
|
}
|
2013-10-21 17:11:42 -07:00
|
|
|
ty::ty_float(t) => {
|
|
|
|
match t {
|
2014-01-09 15:05:33 +02:00
|
|
|
TyF32 => mywrite!(w, "Mf"),
|
|
|
|
TyF64 => mywrite!(w, "MF"),
|
2013-10-21 17:11:42 -07:00
|
|
|
}
|
2011-06-27 14:37:02 -07:00
|
|
|
}
|
2013-10-21 17:11:42 -07:00
|
|
|
ty::ty_enum(def, ref substs) => {
|
|
|
|
mywrite!(w, "t[{}|", (cx.ds)(def));
|
|
|
|
enc_substs(w, cx, substs);
|
|
|
|
mywrite!(w, "]");
|
|
|
|
}
|
|
|
|
ty::ty_trait(def, ref substs, store, mt, bounds) => {
|
|
|
|
mywrite!(w, "x[{}|", (cx.ds)(def));
|
|
|
|
enc_substs(w, cx, substs);
|
|
|
|
enc_trait_store(w, cx, store);
|
|
|
|
enc_mutability(w, mt);
|
|
|
|
let bounds = ty::ParamBounds {builtin_bounds: bounds,
|
2014-03-04 10:02:49 -08:00
|
|
|
trait_bounds: Vec::new()};
|
2013-10-21 17:11:42 -07:00
|
|
|
enc_bounds(w, cx, &bounds);
|
|
|
|
mywrite!(w, "]");
|
|
|
|
}
|
|
|
|
ty::ty_tup(ref ts) => {
|
|
|
|
mywrite!(w, "T[");
|
|
|
|
for t in ts.iter() { enc_ty(w, cx, *t); }
|
|
|
|
mywrite!(w, "]");
|
|
|
|
}
|
2013-12-30 18:57:48 -08:00
|
|
|
ty::ty_box(typ) => { mywrite!(w, "@"); enc_ty(w, cx, typ); }
|
2014-01-12 02:25:51 +02:00
|
|
|
ty::ty_uniq(typ) => { mywrite!(w, "~"); enc_ty(w, cx, typ); }
|
2013-10-21 17:11:42 -07:00
|
|
|
ty::ty_ptr(mt) => { mywrite!(w, "*"); enc_mt(w, cx, mt); }
|
|
|
|
ty::ty_rptr(r, mt) => {
|
|
|
|
mywrite!(w, "&");
|
|
|
|
enc_region(w, cx, r);
|
|
|
|
enc_mt(w, cx, mt);
|
|
|
|
}
|
2014-01-01 04:09:50 +02:00
|
|
|
ty::ty_vec(mt, v) => {
|
2013-10-21 17:11:42 -07:00
|
|
|
mywrite!(w, "V");
|
|
|
|
enc_mt(w, cx, mt);
|
|
|
|
enc_vstore(w, cx, v);
|
|
|
|
}
|
2014-01-01 04:09:50 +02:00
|
|
|
ty::ty_str(v) => {
|
2013-10-21 17:11:42 -07:00
|
|
|
mywrite!(w, "v");
|
|
|
|
enc_vstore(w, cx, v);
|
|
|
|
}
|
|
|
|
ty::ty_unboxed_vec(mt) => { mywrite!(w, "U"); enc_mt(w, cx, mt); }
|
|
|
|
ty::ty_closure(ref f) => {
|
|
|
|
mywrite!(w, "f");
|
|
|
|
enc_closure_ty(w, cx, f);
|
|
|
|
}
|
|
|
|
ty::ty_bare_fn(ref f) => {
|
|
|
|
mywrite!(w, "F");
|
|
|
|
enc_bare_fn_ty(w, cx, f);
|
|
|
|
}
|
|
|
|
ty::ty_infer(_) => {
|
2014-02-06 10:38:08 +01:00
|
|
|
cx.diag.handler().bug("cannot encode inference variable types");
|
2013-10-21 17:11:42 -07:00
|
|
|
}
|
|
|
|
ty::ty_param(param_ty {idx: id, def_id: did}) => {
|
|
|
|
mywrite!(w, "p{}|{}", (cx.ds)(did), id);
|
|
|
|
}
|
|
|
|
ty::ty_self(did) => {
|
|
|
|
mywrite!(w, "s{}|", (cx.ds)(did));
|
|
|
|
}
|
|
|
|
ty::ty_struct(def, ref substs) => {
|
|
|
|
mywrite!(w, "a[{}|", (cx.ds)(def));
|
|
|
|
enc_substs(w, cx, substs);
|
|
|
|
mywrite!(w, "]");
|
|
|
|
}
|
2014-02-06 10:38:08 +01:00
|
|
|
ty::ty_err => fail!("shouldn't encode error type")
|
2011-06-27 14:37:02 -07:00
|
|
|
}
|
|
|
|
}
|
2012-08-10 18:15:08 -07:00
|
|
|
|
2013-12-20 17:36:07 -08:00
|
|
|
fn enc_sigil(w: &mut MemWriter, sigil: Sigil) {
|
2013-01-31 17:12:29 -08:00
|
|
|
match sigil {
|
2013-10-21 17:11:42 -07:00
|
|
|
ManagedSigil => mywrite!(w, "@"),
|
|
|
|
OwnedSigil => mywrite!(w, "~"),
|
|
|
|
BorrowedSigil => mywrite!(w, "&"),
|
2011-06-27 14:37:02 -07:00
|
|
|
}
|
|
|
|
}
|
2011-07-19 17:52:34 -07:00
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
fn enc_purity(w: &mut MemWriter, p: Purity) {
|
2012-08-06 12:34:08 -07:00
|
|
|
match p {
|
2014-01-09 15:05:33 +02:00
|
|
|
ImpureFn => mywrite!(w, "i"),
|
|
|
|
UnsafeFn => mywrite!(w, "u"),
|
|
|
|
ExternFn => mywrite!(w, "c")
|
2012-05-24 23:44:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 17:36:07 -08:00
|
|
|
fn enc_abi_set(w: &mut MemWriter, abis: AbiSet) {
|
2013-10-21 17:11:42 -07:00
|
|
|
mywrite!(w, "[");
|
2013-11-21 15:42:55 -08:00
|
|
|
abis.each(|abi| {
|
2013-10-21 17:11:42 -07:00
|
|
|
mywrite!(w, "{},", abi.name());
|
2013-08-02 02:17:20 -04:00
|
|
|
true
|
2013-11-21 15:42:55 -08:00
|
|
|
});
|
2013-10-21 17:11:42 -07:00
|
|
|
mywrite!(w, "]")
|
2013-01-31 17:12:29 -08:00
|
|
|
}
|
|
|
|
|
2013-12-20 17:36:07 -08:00
|
|
|
fn enc_onceness(w: &mut MemWriter, o: Onceness) {
|
2012-11-02 13:33:51 -07:00
|
|
|
match o {
|
2013-10-21 17:11:42 -07:00
|
|
|
Once => mywrite!(w, "o"),
|
|
|
|
Many => mywrite!(w, "m")
|
2012-11-02 13:33:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-06 05:28:28 +02:00
|
|
|
pub fn enc_bare_fn_ty(w: &mut MemWriter, cx: &ctxt, ft: &ty::BareFnTy) {
|
2013-01-31 17:12:29 -08:00
|
|
|
enc_purity(w, ft.purity);
|
2013-03-13 22:25:28 -04:00
|
|
|
enc_abi_set(w, ft.abis);
|
2013-01-31 17:12:29 -08:00
|
|
|
enc_fn_sig(w, cx, &ft.sig);
|
|
|
|
}
|
|
|
|
|
2014-03-06 05:28:28 +02:00
|
|
|
fn enc_closure_ty(w: &mut MemWriter, cx: &ctxt, ft: &ty::ClosureTy) {
|
2013-01-31 17:12:29 -08:00
|
|
|
enc_sigil(w, ft.sigil);
|
|
|
|
enc_purity(w, ft.purity);
|
|
|
|
enc_onceness(w, ft.onceness);
|
|
|
|
enc_region(w, cx, ft.region);
|
2013-05-10 15:57:27 -04:00
|
|
|
let bounds = ty::ParamBounds {builtin_bounds: ft.bounds,
|
2014-03-04 10:02:49 -08:00
|
|
|
trait_bounds: Vec::new()};
|
2013-05-10 15:57:27 -04:00
|
|
|
enc_bounds(w, cx, &bounds);
|
2013-01-31 17:12:29 -08:00
|
|
|
enc_fn_sig(w, cx, &ft.sig);
|
|
|
|
}
|
|
|
|
|
2014-03-06 05:28:28 +02:00
|
|
|
fn enc_fn_sig(w: &mut MemWriter, cx: &ctxt, fsig: &ty::FnSig) {
|
2013-10-29 06:03:32 -04:00
|
|
|
mywrite!(w, "[{}|", fsig.binder_id);
|
2013-08-03 12:45:23 -04:00
|
|
|
for ty in fsig.inputs.iter() {
|
2013-04-26 19:13:38 -07:00
|
|
|
enc_ty(w, cx, *ty);
|
2011-06-27 14:37:02 -07:00
|
|
|
}
|
2013-10-21 17:11:42 -07:00
|
|
|
mywrite!(w, "]");
|
2013-10-25 01:56:34 -04:00
|
|
|
if fsig.variadic {
|
2013-10-29 06:03:32 -04:00
|
|
|
mywrite!(w, "V");
|
|
|
|
} else {
|
|
|
|
mywrite!(w, "N");
|
2013-10-25 01:56:34 -04:00
|
|
|
}
|
2013-01-31 17:12:29 -08:00
|
|
|
enc_ty(w, cx, fsig.output);
|
2011-06-27 14:37:02 -07:00
|
|
|
}
|
2011-07-19 17:52:34 -07:00
|
|
|
|
2014-03-06 05:28:28 +02:00
|
|
|
fn enc_bounds(w: &mut MemWriter, cx: &ctxt, bs: &ty::ParamBounds) {
|
2013-07-26 13:53:29 +09:00
|
|
|
for bound in bs.builtin_bounds.iter() {
|
2013-05-07 17:30:21 -04:00
|
|
|
match bound {
|
2013-10-21 17:11:42 -07:00
|
|
|
ty::BoundSend => mywrite!(w, "S"),
|
|
|
|
ty::BoundFreeze => mywrite!(w, "K"),
|
|
|
|
ty::BoundStatic => mywrite!(w, "O"),
|
|
|
|
ty::BoundSized => mywrite!(w, "Z"),
|
2013-12-11 12:40:51 -08:00
|
|
|
ty::BoundPod => mywrite!(w, "P"),
|
2011-12-28 17:50:12 +01:00
|
|
|
}
|
2013-07-26 13:53:29 +09:00
|
|
|
}
|
2013-05-07 17:30:21 -04:00
|
|
|
|
2013-08-03 12:45:23 -04:00
|
|
|
for &tp in bs.trait_bounds.iter() {
|
2013-10-21 17:11:42 -07:00
|
|
|
mywrite!(w, "I");
|
2013-05-07 17:30:21 -04:00
|
|
|
enc_trait_ref(w, cx, tp);
|
|
|
|
}
|
|
|
|
|
2013-10-21 17:11:42 -07:00
|
|
|
mywrite!(w, ".");
|
2011-12-28 17:50:12 +01:00
|
|
|
}
|
2011-06-27 14:37:02 -07:00
|
|
|
|
2014-03-06 05:28:28 +02:00
|
|
|
pub fn enc_type_param_def(w: &mut MemWriter, cx: &ctxt, v: &ty::TypeParameterDef) {
|
2014-02-14 07:07:09 +02:00
|
|
|
mywrite!(w, "{}:{}|", token::get_ident(v.ident), (cx.ds)(v.def_id));
|
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
|
|
|
enc_bounds(w, cx, v.bounds);
|
2014-01-30 19:28:02 +02:00
|
|
|
enc_opt(w, v.default, |w, t| enc_ty(w, cx, t));
|
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
|
|
|
}
|