rust/src/librustc/metadata/tydecode.rs

620 lines
19 KiB
Rust
Raw Normal View History

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// 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.
// Type decoding
2011-06-27 18:03:01 -05:00
// tjc note: Would be great to have a `match check` macro equivalent
// for some of these
use middle::ty;
use std::str;
use std::uint;
use syntax::abi::AbiSet;
use syntax::abi;
2012-09-04 13:54:36 -05:00
use syntax::ast;
use syntax::ast::*;
use syntax::opt_vec;
// Compact string representation for ty::t values. API ty_str &
// parse_from_str. Extra parameters are for converting to/from def_ids in the
// data buffer. Whatever format you choose should not contain pipe characters.
// Def id conversion: when we encounter def-ids, they have to be translated.
// For example, the crate number must be converted from the crate number used
// in the library we are reading from into the local crate numbers in use
// here. To perform this translation, the type decoder is supplied with a
// conversion function of type `conv_did`.
//
// Sometimes, particularly when inlining, the correct translation of the
// def-id will depend on where it originated from. Therefore, the conversion
// function is given an indicator of the source of the def-id. See
// astencode.rs for more information.
pub enum DefIdSource {
// Identifies a struct, trait, enum, etc.
NominalType,
// Identifies a type alias (`type X = ...`).
TypeWithId,
// Identifies a type parameter (`fn foo<X>() { ... }`).
TypeParameter,
// Identifies a region parameter (`fn foo<'X>() { ... }`).
RegionParameter,
}
type conv_did<'a> =
'a |source: DefIdSource, ast::DefId| -> ast::DefId;
pub struct PState<'a> {
data: &'a [u8],
crate: ast::CrateNum,
pos: uint,
tcx: ty::ctxt
}
fn peek(st: &PState) -> char {
st.data[st.pos] as char
}
fn next(st: &mut PState) -> char {
let ch = st.data[st.pos] as char;
st.pos = st.pos + 1u;
2012-08-01 19:30:05 -05:00
return ch;
}
fn next_byte(st: &mut PState) -> u8 {
let b = st.data[st.pos];
st.pos = st.pos + 1u;
2012-08-01 19:30:05 -05:00
return b;
}
fn scan<R>(st: &mut PState, is_last: |char| -> bool, op: |&[u8]| -> R) -> R {
let start_pos = st.pos;
debug!("scan: '{}' (start)", st.data[st.pos] as char);
while !is_last(st.data[st.pos] as char) {
st.pos += 1;
debug!("scan: '{}'", st.data[st.pos] as char);
}
let end_pos = st.pos;
st.pos += 1;
return op(st.data.slice(start_pos, end_pos));
}
2013-09-01 19:50:59 -05:00
pub fn parse_ident(st: &mut PState, last: char) -> ast::Ident {
2012-08-01 19:30:05 -05:00
fn is_last(b: char, c: char) -> bool { return c == b; }
return parse_ident_(st, |a| is_last(last, a) );
}
fn parse_ident_(st: &mut PState, is_last: |char| -> bool) -> ast::Ident {
scan(st, is_last, |bytes| {
st.tcx.sess.ident_of(str::from_utf8(bytes))
})
}
pub fn parse_state_from_data<'a>(data: &'a [u8], crate_num: ast::CrateNum,
pos: uint, tcx: ty::ctxt) -> PState<'a> {
PState {
data: data,
crate: crate_num,
pos: pos,
tcx: tcx
}
}
pub fn parse_ty_data(data: &[u8], crate_num: ast::CrateNum, pos: uint, tcx: ty::ctxt,
conv: conv_did) -> ty::t {
let mut st = parse_state_from_data(data, crate_num, pos, tcx);
parse_ty(&mut st, conv)
}
pub fn parse_bare_fn_ty_data(data: &[u8], crate_num: ast::CrateNum, pos: uint, tcx: ty::ctxt,
conv: conv_did) -> ty::BareFnTy {
let mut st = parse_state_from_data(data, crate_num, pos, tcx);
parse_bare_fn_ty(&mut st, conv)
}
pub fn parse_trait_ref_data(data: &[u8], crate_num: ast::CrateNum, pos: uint, tcx: ty::ctxt,
conv: conv_did) -> ty::TraitRef {
let mut st = parse_state_from_data(data, crate_num, pos, tcx);
parse_trait_ref(&mut st, conv)
}
pub fn parse_substs_data(data: &[u8], crate_num: ast::CrateNum, pos: uint, tcx: ty::ctxt,
conv: conv_did) -> ty::substs {
let mut st = parse_state_from_data(data, crate_num, pos, tcx);
parse_substs(&mut st, conv)
}
fn parse_sigil(st: &mut PState) -> ast::Sigil {
match next(st) {
'@' => ast::ManagedSigil,
'~' => ast::OwnedSigil,
'&' => ast::BorrowedSigil,
2013-09-28 00:38:08 -05:00
c => st.tcx.sess.bug(format!("parse_sigil(): bad input '{}'", c))
}
}
fn parse_vstore(st: &mut PState, conv: conv_did) -> ty::vstore {
assert_eq!(next(st), '/');
2012-04-20 18:56:19 -05:00
let c = peek(st);
if '0' <= c && c <= '9' {
let n = parse_uint(st);
assert_eq!(next(st), '|');
2012-08-01 19:30:05 -05:00
return ty::vstore_fixed(n);
2012-04-20 18:56:19 -05:00
}
match next(st) {
2012-08-03 21:59:04 -05:00
'~' => ty::vstore_uniq,
'@' => ty::vstore_box,
'&' => ty::vstore_slice(parse_region(st, conv)),
2013-09-28 00:38:08 -05:00
c => st.tcx.sess.bug(format!("parse_vstore(): bad input '{}'", c))
2012-04-20 18:56:19 -05:00
}
}
fn parse_trait_store(st: &mut PState, conv: conv_did) -> ty::TraitStore {
match next(st) {
'~' => ty::UniqTraitStore,
'@' => ty::BoxTraitStore,
'&' => ty::RegionTraitStore(parse_region(st, conv)),
2013-09-28 00:38:08 -05:00
c => st.tcx.sess.bug(format!("parse_trait_store(): bad input '{}'", c))
}
}
fn parse_substs(st: &mut PState, conv: conv_did) -> ty::substs {
let regions = parse_region_substs(st, |x,y| conv(x,y));
let self_ty = parse_opt(st, |st| parse_ty(st, |x,y| conv(x,y)) );
assert_eq!(next(st), '[');
let mut params: ~[ty::t] = ~[];
while peek(st) != ']' { params.push(parse_ty(st, |x,y| conv(x,y))); }
st.pos = st.pos + 1u;
return ty::substs {
regions: regions,
self_ty: self_ty,
tps: params
};
}
fn parse_region_substs(st: &mut PState, conv: conv_did) -> ty::RegionSubsts {
match next(st) {
'e' => ty::ErasedRegions,
'n' => {
let mut regions = opt_vec::Empty;
while peek(st) != '.' {
let r = parse_region(st, |x,y| conv(x,y));
regions.push(r);
}
assert_eq!(next(st), '.');
ty::NonerasedRegions(regions)
}
_ => fail!("parse_bound_region: bad input")
}
}
2013-10-29 09:34:11 -05:00
fn parse_bound_region(st: &mut PState, conv: conv_did) -> ty::BoundRegion {
match next(st) {
'a' => {
let id = parse_uint(st);
assert_eq!(next(st), '|');
2013-10-29 09:34:11 -05:00
ty::BrAnon(id)
}
'[' => {
let def = parse_def(st, RegionParameter, |x,y| conv(x,y));
let ident = st.tcx.sess.ident_of(parse_str(st, ']'));
2013-10-29 09:34:11 -05:00
ty::BrNamed(def, ident)
}
'f' => {
let id = parse_uint(st);
assert_eq!(next(st), '|');
2013-10-29 09:34:11 -05:00
ty::BrFresh(id)
}
_ => fail!("parse_bound_region: bad input")
}
}
fn parse_region(st: &mut PState, conv: conv_did) -> ty::Region {
match next(st) {
2012-08-03 21:59:04 -05:00
'b' => {
assert_eq!(next(st), '[');
let id = parse_uint(st) as ast::NodeId;
assert_eq!(next(st), '|');
let br = parse_bound_region(st, |x,y| conv(x,y));
assert_eq!(next(st), ']');
2013-10-29 09:34:11 -05:00
ty::ReLateBound(id, br)
}
'B' => {
assert_eq!(next(st), '[');
let node_id = parse_uint(st) as ast::NodeId;
assert_eq!(next(st), '|');
let index = parse_uint(st);
assert_eq!(next(st), '|');
let nm = st.tcx.sess.ident_of(parse_str(st, ']'));
2013-10-29 09:34:11 -05:00
ty::ReEarlyBound(node_id, index, nm)
}
2012-08-03 21:59:04 -05:00
'f' => {
assert_eq!(next(st), '[');
let id = parse_uint(st) as ast::NodeId;
assert_eq!(next(st), '|');
let br = parse_bound_region(st, |x,y| conv(x,y));
assert_eq!(next(st), ']');
2013-10-29 09:34:11 -05:00
ty::ReFree(ty::FreeRegion {scope_id: id,
bound_region: br})
}
2012-08-03 21:59:04 -05:00
's' => {
let id = parse_uint(st) as ast::NodeId;
assert_eq!(next(st), '|');
2013-10-29 09:34:11 -05:00
ty::ReScope(id)
}
2012-08-03 21:59:04 -05:00
't' => {
2013-10-29 09:34:11 -05:00
ty::ReStatic
}
2013-03-15 14:24:24 -05:00
'e' => {
2013-10-29 09:34:11 -05:00
ty::ReStatic
2013-03-15 14:24:24 -05:00
}
_ => fail!("parse_region: bad input")
}
}
fn parse_opt<T>(st: &mut PState, f: |&mut PState| -> T) -> Option<T> {
match next(st) {
2012-08-20 14:23:37 -05:00
'n' => None,
's' => Some(f(st)),
_ => fail!("parse_opt: bad input")
}
}
fn parse_str(st: &mut PState, term: char) -> ~str {
let mut result = ~"";
while peek(st) != term {
unsafe {
str::raw::push_byte(&mut result, next_byte(st));
}
}
next(st);
2012-08-01 19:30:05 -05:00
return result;
}
fn parse_trait_ref(st: &mut PState, conv: conv_did) -> ty::TraitRef {
let def = parse_def(st, NominalType, |x,y| conv(x,y));
let substs = parse_substs(st, |x,y| conv(x,y));
ty::TraitRef {def_id: def, substs: substs}
}
fn parse_ty(st: &mut PState, conv: conv_did) -> ty::t {
match next(st) {
2013-04-22 22:19:05 -05:00
'n' => return ty::mk_nil(),
'z' => return ty::mk_bot(),
'b' => return ty::mk_bool(),
'i' => return ty::mk_int(),
'u' => return ty::mk_uint(),
2012-08-03 21:59:04 -05:00
'M' => {
match next(st) {
2013-04-22 22:19:05 -05:00
'b' => return ty::mk_mach_uint(ast::ty_u8),
'w' => return ty::mk_mach_uint(ast::ty_u16),
'l' => return ty::mk_mach_uint(ast::ty_u32),
'd' => return ty::mk_mach_uint(ast::ty_u64),
'B' => return ty::mk_mach_int(ast::ty_i8),
'W' => return ty::mk_mach_int(ast::ty_i16),
'L' => return ty::mk_mach_int(ast::ty_i32),
'D' => return ty::mk_mach_int(ast::ty_i64),
'f' => return ty::mk_mach_float(ast::ty_f32),
'F' => return ty::mk_mach_float(ast::ty_f64),
_ => fail!("parse_ty: bad numeric type")
}
2011-07-27 07:19:39 -05:00
}
2013-04-22 22:19:05 -05:00
'c' => return ty::mk_char(),
2012-08-03 21:59:04 -05:00
't' => {
assert_eq!(next(st), '[');
let def = parse_def(st, NominalType, |x,y| conv(x,y));
let substs = parse_substs(st, |x,y| conv(x,y));
assert_eq!(next(st), ']');
2012-08-01 19:30:05 -05:00
return ty::mk_enum(st.tcx, def, substs);
2011-07-27 07:19:39 -05:00
}
2012-08-03 21:59:04 -05:00
'x' => {
assert_eq!(next(st), '[');
let def = parse_def(st, NominalType, |x,y| conv(x,y));
let substs = parse_substs(st, |x,y| conv(x,y));
let store = parse_trait_store(st, |x,y| conv(x,y));
let mt = parse_mutability(st);
let bounds = parse_bounds(st, |x,y| conv(x,y));
assert_eq!(next(st), ']');
return ty::mk_trait(st.tcx, def, substs, store, mt, bounds.builtin_bounds);
}
2012-08-03 21:59:04 -05:00
'p' => {
let did = parse_def(st, TypeParameter, |x,y| conv(x,y));
debug!("parsed ty_param: did={:?}", did);
return ty::mk_param(st.tcx, parse_uint(st), did);
}
2012-08-03 21:59:04 -05:00
's' => {
let did = parse_def(st, TypeParameter, |x,y| conv(x,y));
return ty::mk_self(st.tcx, did);
}
'@' => return ty::mk_box(st.tcx, parse_mt(st, |x,y| conv(x,y))),
'~' => return ty::mk_uniq(st.tcx, parse_mt(st, |x,y| conv(x,y))),
'*' => return ty::mk_ptr(st.tcx, parse_mt(st, |x,y| conv(x,y))),
2012-08-03 21:59:04 -05:00
'&' => {
let r = parse_region(st, |x,y| conv(x,y));
let mt = parse_mt(st, |x,y| conv(x,y));
2012-08-01 19:30:05 -05:00
return ty::mk_rptr(st.tcx, r, mt);
}
'U' => return ty::mk_unboxed_vec(st.tcx, parse_mt(st, |x,y| conv(x,y))),
2012-08-03 21:59:04 -05:00
'V' => {
let mt = parse_mt(st, |x,y| conv(x,y));
let v = parse_vstore(st, |x,y| conv(x,y));
2012-08-01 19:30:05 -05:00
return ty::mk_evec(st.tcx, mt, v);
}
2012-08-03 21:59:04 -05:00
'v' => {
let v = parse_vstore(st, |x,y| conv(x,y));
2012-08-01 19:30:05 -05:00
return ty::mk_estr(st.tcx, v);
}
2012-08-03 21:59:04 -05:00
'T' => {
assert_eq!(next(st), '[');
let mut params = ~[];
while peek(st) != ']' { params.push(parse_ty(st, |x,y| conv(x,y))); }
st.pos = st.pos + 1u;
2012-08-01 19:30:05 -05:00
return ty::mk_tup(st.tcx, params);
}
2012-08-03 21:59:04 -05:00
'f' => {
return ty::mk_closure(st.tcx, parse_closure_ty(st, |x,y| conv(x,y)));
}
'F' => {
return ty::mk_bare_fn(st.tcx, parse_bare_fn_ty(st, |x,y| conv(x,y)));
2011-07-27 07:19:39 -05:00
}
2012-08-03 21:59:04 -05:00
'Y' => return ty::mk_type(st.tcx),
'C' => {
let sigil = parse_sigil(st);
return ty::mk_opaque_closure_ptr(st.tcx, sigil);
}
2012-08-03 21:59:04 -05:00
'#' => {
2011-07-27 07:19:39 -05:00
let pos = parse_hex(st);
assert_eq!(next(st), ':');
2011-07-27 07:19:39 -05:00
let len = parse_hex(st);
assert_eq!(next(st), '#');
let key = ty::creader_cache_key {cnum: st.crate,
pos: pos,
len: len };
let tt_opt = {
let rcache = st.tcx.rcache.borrow();
rcache.get().find_copy(&key)
};
match tt_opt {
Some(tt) => return tt,
2012-08-20 14:23:37 -05:00
None => {
let mut ps = PState {
pos: pos,
.. *st
};
let tt = parse_ty(&mut ps, |x,y| conv(x,y));
let mut rcache = st.tcx.rcache.borrow_mut();
rcache.get().insert(key, tt);
2012-08-01 19:30:05 -05:00
return tt;
2011-07-27 07:19:39 -05:00
}
}
2011-07-27 07:19:39 -05:00
}
2012-08-03 21:59:04 -05:00
'"' => {
let _ = parse_def(st, TypeWithId, |x,y| conv(x,y));
let inner = parse_ty(st, |x,y| conv(x,y));
2013-04-02 15:41:18 -05:00
inner
}
2012-08-03 21:59:04 -05:00
'B' => ty::mk_opaque_box(st.tcx),
'a' => {
assert_eq!(next(st), '[');
let did = parse_def(st, NominalType, |x,y| conv(x,y));
let substs = parse_substs(st, |x,y| conv(x,y));
assert_eq!(next(st), ']');
return ty::mk_struct(st.tcx, did, substs);
}
c => { error!("unexpected char in type string: {}", c); fail!();}
}
}
fn parse_mutability(st: &mut PState) -> ast::Mutability {
2012-08-06 14:34:08 -05:00
match peek(st) {
'm' => { next(st); ast::MutMutable }
_ => { ast::MutImmutable }
}
}
fn parse_mt(st: &mut PState, conv: conv_did) -> ty::mt {
let m = parse_mutability(st);
ty::mt { ty: parse_ty(st, |x,y| conv(x,y)), mutbl: m }
}
fn parse_def(st: &mut PState, source: DefIdSource,
conv: conv_did) -> ast::DefId {
return conv(source, scan(st, |c| { c == '|' }, parse_def_id));
}
fn parse_uint(st: &mut PState) -> uint {
let mut n = 0;
loop {
let cur = peek(st);
2012-08-01 19:30:05 -05:00
if cur < '0' || cur > '9' { return n; }
st.pos = st.pos + 1u;
n *= 10;
n += (cur as uint) - ('0' as uint);
};
}
fn parse_hex(st: &mut PState) -> uint {
let mut n = 0u;
loop {
let cur = peek(st);
2012-08-01 19:30:05 -05:00
if (cur < '0' || cur > '9') && (cur < 'a' || cur > 'f') { return n; }
st.pos = st.pos + 1u;
n *= 16u;
2011-07-27 07:19:39 -05:00
if '0' <= cur && cur <= '9' {
n += (cur as uint) - ('0' as uint);
} else { n += 10u + (cur as uint) - ('a' as uint); }
};
}
2012-05-25 01:44:58 -05:00
fn parse_purity(c: char) -> purity {
match c {
2012-08-03 21:59:04 -05:00
'u' => unsafe_fn,
'i' => impure_fn,
'c' => extern_fn,
_ => fail!("parse_purity: bad purity {}", c)
2012-05-25 01:44:58 -05:00
}
}
fn parse_abi_set(st: &mut PState) -> AbiSet {
assert_eq!(next(st), '[');
let mut abis = AbiSet::empty();
while peek(st) != ']' {
scan(st, |c| c == ',', |bytes| {
let abi_str = str::from_utf8(bytes).to_owned();
let abi = abi::lookup(abi_str).expect(abi_str);
abis.add(abi);
});
}
assert_eq!(next(st), ']');
return abis;
}
fn parse_onceness(c: char) -> ast::Onceness {
match c {
'o' => ast::Once,
'm' => ast::Many,
_ => fail!("parse_onceness: bad onceness")
}
}
fn parse_closure_ty(st: &mut PState, conv: conv_did) -> ty::ClosureTy {
let sigil = parse_sigil(st);
2012-05-25 01:44:58 -05:00
let purity = parse_purity(next(st));
let onceness = parse_onceness(next(st));
let region = parse_region(st, |x,y| conv(x,y));
let bounds = parse_bounds(st, |x,y| conv(x,y));
let sig = parse_sig(st, |x,y| conv(x,y));
ty::ClosureTy {
purity: purity,
sigil: sigil,
onceness: onceness,
region: region,
bounds: bounds.builtin_bounds,
sig: sig
}
}
fn parse_bare_fn_ty(st: &mut PState, conv: conv_did) -> ty::BareFnTy {
let purity = parse_purity(next(st));
let abi = parse_abi_set(st);
let sig = parse_sig(st, |x,y| conv(x,y));
ty::BareFnTy {
purity: purity,
abis: abi,
sig: sig
}
}
fn parse_sig(st: &mut PState, conv: conv_did) -> ty::FnSig {
assert_eq!(next(st), '[');
let id = parse_uint(st) as ast::NodeId;
assert_eq!(next(st), '|');
2013-04-26 21:13:38 -05:00
let mut inputs = ~[];
while peek(st) != ']' {
inputs.push(parse_ty(st, |x,y| conv(x,y)));
}
st.pos += 1u; // eat the ']'
let variadic = match next(st) {
'V' => true,
'N' => false,
2013-11-08 19:59:43 -06:00
r => fail!(format!("Bad variadic: {}", r)),
};
let ret_ty = parse_ty(st, |x,y| conv(x,y));
ty::FnSig {binder_id: id,
inputs: inputs,
output: ret_ty,
variadic: variadic}
}
// Rust metadata parsing
pub fn parse_def_id(buf: &[u8]) -> ast::DefId {
let mut colon_idx = 0u;
2013-05-14 04:52:12 -05:00
let len = buf.len();
while colon_idx < len && buf[colon_idx] != ':' as u8 { colon_idx += 1u; }
2011-07-27 07:19:39 -05:00
if colon_idx == len {
error!("didn't find ':' when parsing def id");
fail!();
}
let crate_part = buf.slice(0u, colon_idx);
let def_part = buf.slice(colon_idx + 1u, len);
let crate_num = match uint::parse_bytes(crate_part, 10u) {
Some(cn) => cn as ast::CrateNum,
None => fail!("internal error: parse_def_id: crate number expected, but found {:?}",
2013-05-09 06:52:07 -05:00
crate_part)
};
let def_num = match uint::parse_bytes(def_part, 10u) {
Some(dn) => dn as ast::NodeId,
None => fail!("internal error: parse_def_id: id expected, but found {:?}",
2013-05-09 06:52:07 -05:00
def_part)
};
ast::DefId { crate: crate_num, node: def_num }
}
pub fn parse_type_param_def_data(data: &[u8], start: uint,
crate_num: ast::CrateNum, tcx: ty::ctxt,
conv: conv_did) -> ty::TypeParameterDef
{
let mut st = parse_state_from_data(data, crate_num, start, tcx);
parse_type_param_def(&mut st, conv)
}
fn parse_type_param_def(st: &mut PState, conv: conv_did) -> ty::TypeParameterDef {
ty::TypeParameterDef {ident: parse_ident(st, ':'),
def_id: parse_def(st, NominalType, |x,y| conv(x,y)),
bounds: @parse_bounds(st, |x,y| conv(x,y))}
}
fn parse_bounds(st: &mut PState, conv: conv_did) -> ty::ParamBounds {
let mut param_bounds = ty::ParamBounds {
builtin_bounds: ty::EmptyBuiltinBounds(),
trait_bounds: ~[]
};
loop {
match next(st) {
'S' => {
param_bounds.builtin_bounds.add(ty::BoundSend);
}
'K' => {
param_bounds.builtin_bounds.add(ty::BoundFreeze);
}
'O' => {
param_bounds.builtin_bounds.add(ty::BoundStatic);
}
'Z' => {
param_bounds.builtin_bounds.add(ty::BoundSized);
}
'P' => {
param_bounds.builtin_bounds.add(ty::BoundPod);
}
'I' => {
param_bounds.trait_bounds.push(@parse_trait_ref(st, |x,y| conv(x,y)));
}
'.' => {
return param_bounds;
}
c => {
fail!("parse_bounds: bad bounds ('{}')", c)
}
}
}
}