2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
|
|
|
|
2013-01-07 16:16:52 -06:00
|
|
|
|
2011-06-27 17:30:17 -05:00
|
|
|
// Type decoding
|
2011-06-27 18:03:01 -05:00
|
|
|
|
2012-08-22 19:58:05 -05:00
|
|
|
// tjc note: Would be great to have a `match check` macro equivalent
|
|
|
|
// for some of these
|
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::ty;
|
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::str;
|
|
|
|
use core::uint;
|
|
|
|
use core::vec;
|
2012-09-04 13:54:36 -05:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::ast::*;
|
2013-01-30 11:56:33 -06:00
|
|
|
use syntax::codemap::{respan, dummy_sp};
|
2011-06-27 17:30:17 -05:00
|
|
|
|
2011-07-04 21:07:56 -05: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
|
|
|
|
// data buffer. Whatever format you choose should not contain pipe characters.
|
2011-06-27 17:30:17 -05:00
|
|
|
|
2013-01-17 08:13:26 -06:00
|
|
|
// 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.
|
2013-01-29 18:51:16 -06:00
|
|
|
pub enum DefIdSource {
|
2013-01-17 08:13:26 -06:00
|
|
|
// Identifies a struct, trait, enum, etc.
|
|
|
|
NominalType,
|
|
|
|
|
|
|
|
// Identifies a type alias (`type X = ...`).
|
|
|
|
TypeWithId,
|
|
|
|
|
|
|
|
// Identifies a type parameter (`fn foo<X>() { ... }`).
|
|
|
|
TypeParameter
|
|
|
|
}
|
2013-02-26 13:34:00 -06:00
|
|
|
type conv_did = &self/fn(source: DefIdSource, ast::def_id) -> ast::def_id;
|
2011-06-27 17:30:17 -05:00
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub struct PState {
|
|
|
|
data: @~[u8],
|
|
|
|
crate: int,
|
|
|
|
pos: uint,
|
|
|
|
tcx: ty::ctxt
|
|
|
|
}
|
2011-06-27 17:30:17 -05:00
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn peek(st: @mut PState) -> char {
|
2012-02-13 13:55:23 -06:00
|
|
|
st.data[st.pos] as char
|
2011-12-28 10:50:12 -06:00
|
|
|
}
|
2011-06-27 17:30:17 -05:00
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn next(st: @mut PState) -> char {
|
2012-02-13 13:55:23 -06:00
|
|
|
let ch = st.data[st.pos] as char;
|
2011-06-27 17:30:17 -05:00
|
|
|
st.pos = st.pos + 1u;
|
2012-08-01 19:30:05 -05:00
|
|
|
return ch;
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn next_byte(st: @mut PState) -> u8 {
|
2012-02-13 13:55:23 -06:00
|
|
|
let b = st.data[st.pos];
|
|
|
|
st.pos = st.pos + 1u;
|
2012-08-01 19:30:05 -05:00
|
|
|
return b;
|
2012-02-13 13:55:23 -06:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06: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) );
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
|
2013-03-01 14:11:07 -06:00
|
|
|
fn parse_ident_(st: @mut PState, is_last: @fn(char) -> bool) ->
|
2011-07-27 07:19:39 -05:00
|
|
|
ast::ident {
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut rslt = ~"";
|
2012-02-13 13:55:23 -06:00
|
|
|
while !is_last(peek(st)) {
|
|
|
|
rslt += str::from_byte(next_byte(st));
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
2012-07-18 18:18:02 -05:00
|
|
|
return st.tcx.sess.ident_of(rslt);
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 18:51:16 -06:00
|
|
|
pub fn parse_state_from_data(data: @~[u8], crate_num: int,
|
2013-02-04 16:02:01 -06:00
|
|
|
pos: uint, tcx: ty::ctxt) -> @mut PState {
|
|
|
|
@mut PState {
|
|
|
|
data: data,
|
|
|
|
crate: crate_num,
|
|
|
|
pos: pos,
|
|
|
|
tcx: tcx
|
|
|
|
}
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
2011-06-27 17:30:17 -05:00
|
|
|
|
2013-01-29 18:51:16 -06:00
|
|
|
pub fn parse_ty_data(data: @~[u8], crate_num: int, pos: uint, tcx: ty::ctxt,
|
|
|
|
conv: conv_did) -> ty::t {
|
2012-09-11 23:25:01 -05:00
|
|
|
let st = parse_state_from_data(data, crate_num, pos, tcx);
|
2012-01-05 09:04:59 -06:00
|
|
|
parse_ty(st, conv)
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 18:51:16 -06:00
|
|
|
pub fn parse_arg_data(data: @~[u8], crate_num: int, pos: uint, tcx: ty::ctxt,
|
|
|
|
conv: conv_did) -> ty::arg {
|
2012-09-11 23:25:01 -05:00
|
|
|
let st = parse_state_from_data(data, crate_num, pos, tcx);
|
|
|
|
parse_arg(st, conv)
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn parse_path(st: @mut PState) -> @ast::path {
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut idents: ~[ast::ident] = ~[];
|
2012-08-01 19:30:05 -05:00
|
|
|
fn is_last(c: char) -> bool { return c == '(' || c == ':'; }
|
2012-09-26 19:33:34 -05:00
|
|
|
idents.push(parse_ident_(st, is_last));
|
2012-03-10 22:34:17 -06:00
|
|
|
loop {
|
2012-08-06 14:34:08 -05:00
|
|
|
match peek(st) {
|
2012-08-03 21:59:04 -05:00
|
|
|
':' => { next(st); next(st); }
|
|
|
|
c => {
|
2011-07-27 07:19:39 -05:00
|
|
|
if c == '(' {
|
2013-01-30 11:56:33 -06:00
|
|
|
return @ast::path { span: dummy_sp(),
|
2013-01-13 12:48:09 -06:00
|
|
|
global: false,
|
|
|
|
idents: idents,
|
|
|
|
rp: None,
|
|
|
|
types: ~[] };
|
2012-09-26 19:33:34 -05:00
|
|
|
} else { idents.push(parse_ident_(st, is_last)); }
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
2012-03-10 22:34:17 -06:00
|
|
|
};
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn parse_sigil(st: @mut PState) -> ast::Sigil {
|
2012-08-10 20:15:08 -05:00
|
|
|
match next(st) {
|
2013-01-31 19:12:29 -06:00
|
|
|
'@' => ast::ManagedSigil,
|
|
|
|
'~' => ast::OwnedSigil,
|
|
|
|
'&' => ast::BorrowedSigil,
|
|
|
|
c => st.tcx.sess.bug(fmt!("parse_sigil(): bad input '%c'", c))
|
2012-01-12 17:38:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn parse_vstore(st: @mut PState) -> ty::vstore {
|
2012-04-20 18:56:19 -05:00
|
|
|
assert next(st) == '/';
|
|
|
|
|
|
|
|
let c = peek(st);
|
|
|
|
if '0' <= c && c <= '9' {
|
|
|
|
let n = parse_int(st) as uint;
|
|
|
|
assert next(st) == '|';
|
2012-08-01 19:30:05 -05:00
|
|
|
return ty::vstore_fixed(n);
|
2012-04-20 18:56:19 -05:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:58:05 -05:00
|
|
|
match next(st) {
|
2012-08-03 21:59:04 -05:00
|
|
|
'~' => ty::vstore_uniq,
|
|
|
|
'@' => ty::vstore_box,
|
2012-08-22 19:58:05 -05:00
|
|
|
'&' => ty::vstore_slice(parse_region(st)),
|
2013-01-31 19:12:29 -06:00
|
|
|
c => st.tcx.sess.bug(fmt!("parse_vstore(): bad input '%c'", c))
|
2012-04-20 18:56:19 -05:00
|
|
|
}
|
2012-04-10 20:32:51 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn parse_substs(st: @mut PState, conv: conv_did) -> ty::substs {
|
2012-06-30 18:19:07 -05:00
|
|
|
let self_r = parse_opt(st, || parse_region(st) );
|
2012-04-18 23:26:25 -05:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
let self_ty = parse_opt(st, || parse_ty(st, conv) );
|
2012-05-09 08:09:58 -05:00
|
|
|
|
2012-04-18 23:26:25 -05:00
|
|
|
assert next(st) == '[';
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut params: ~[ty::t] = ~[];
|
2012-09-26 19:33:34 -05:00
|
|
|
while peek(st) != ']' { params.push(parse_ty(st, conv)); }
|
2012-04-18 23:26:25 -05:00
|
|
|
st.pos = st.pos + 1u;
|
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
return ty::substs {
|
2013-01-25 18:57:39 -06:00
|
|
|
self_r: self_r,
|
|
|
|
self_ty: self_ty,
|
|
|
|
tps: params
|
|
|
|
};
|
2012-04-18 23:26:25 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn parse_bound_region(st: @mut PState) -> ty::bound_region {
|
2012-08-22 19:58:05 -05:00
|
|
|
match next(st) {
|
2012-08-03 21:59:04 -05:00
|
|
|
's' => ty::br_self,
|
2012-08-20 18:53:33 -05:00
|
|
|
'a' => {
|
|
|
|
let id = parse_int(st) as uint;
|
|
|
|
assert next(st) == '|';
|
|
|
|
ty::br_anon(id)
|
|
|
|
}
|
2012-07-18 18:18:02 -05:00
|
|
|
'[' => ty::br_named(st.tcx.sess.ident_of(parse_str(st, ']'))),
|
2012-08-03 21:59:04 -05:00
|
|
|
'c' => {
|
2012-07-25 11:19:59 -05:00
|
|
|
let id = parse_int(st);
|
|
|
|
assert next(st) == '|';
|
|
|
|
ty::br_cap_avoid(id, @parse_bound_region(st))
|
2012-08-22 19:58:05 -05:00
|
|
|
},
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => fail!(~"parse_bound_region: bad input")
|
2012-04-18 23:26:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn parse_region(st: @mut PState) -> ty::Region {
|
2012-08-22 19:58:05 -05:00
|
|
|
match next(st) {
|
2012-08-03 21:59:04 -05:00
|
|
|
'b' => {
|
2012-04-18 23:26:25 -05:00
|
|
|
ty::re_bound(parse_bound_region(st))
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
'f' => {
|
2012-04-18 23:26:25 -05:00
|
|
|
assert next(st) == '[';
|
|
|
|
let id = parse_int(st);
|
|
|
|
assert next(st) == '|';
|
|
|
|
let br = parse_bound_region(st);
|
|
|
|
assert next(st) == ']';
|
|
|
|
ty::re_free(id, br)
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
's' => {
|
2012-04-18 23:26:25 -05:00
|
|
|
let id = parse_int(st);
|
|
|
|
assert next(st) == '|';
|
|
|
|
ty::re_scope(id)
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
't' => {
|
2012-04-18 23:26:25 -05:00
|
|
|
ty::re_static
|
|
|
|
}
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => fail!(~"parse_region: bad input")
|
2012-04-18 23:26:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn parse_opt<T>(st: @mut PState, f: fn() -> T) -> Option<T> {
|
2012-08-22 19:58:05 -05:00
|
|
|
match next(st) {
|
2012-08-20 14:23:37 -05:00
|
|
|
'n' => None,
|
|
|
|
's' => Some(f()),
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => fail!(~"parse_opt: bad input")
|
2012-04-18 23:26:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn parse_str(st: @mut PState, term: char) -> ~str {
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut result = ~"";
|
2012-04-18 23:26:25 -05:00
|
|
|
while peek(st) != term {
|
|
|
|
result += str::from_byte(next_byte(st));
|
|
|
|
}
|
|
|
|
next(st);
|
2012-08-01 19:30:05 -05:00
|
|
|
return result;
|
2012-04-18 23:26:25 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn parse_ty(st: @mut PState, conv: conv_did) -> ty::t {
|
2012-08-22 19:58:05 -05:00
|
|
|
match next(st) {
|
2012-08-03 21:59:04 -05:00
|
|
|
'n' => return ty::mk_nil(st.tcx),
|
|
|
|
'z' => return ty::mk_bot(st.tcx),
|
|
|
|
'b' => return ty::mk_bool(st.tcx),
|
|
|
|
'i' => return ty::mk_int(st.tcx),
|
|
|
|
'u' => return ty::mk_uint(st.tcx),
|
|
|
|
'l' => return ty::mk_float(st.tcx),
|
|
|
|
'M' => {
|
2012-08-22 19:58:05 -05:00
|
|
|
match next(st) {
|
2012-08-03 21:59:04 -05:00
|
|
|
'b' => return ty::mk_mach_uint(st.tcx, ast::ty_u8),
|
|
|
|
'w' => return ty::mk_mach_uint(st.tcx, ast::ty_u16),
|
|
|
|
'l' => return ty::mk_mach_uint(st.tcx, ast::ty_u32),
|
|
|
|
'd' => return ty::mk_mach_uint(st.tcx, ast::ty_u64),
|
|
|
|
'B' => return ty::mk_mach_int(st.tcx, ast::ty_i8),
|
|
|
|
'W' => return ty::mk_mach_int(st.tcx, ast::ty_i16),
|
|
|
|
'L' => return ty::mk_mach_int(st.tcx, ast::ty_i32),
|
|
|
|
'D' => return ty::mk_mach_int(st.tcx, ast::ty_i64),
|
|
|
|
'f' => return ty::mk_mach_float(st.tcx, ast::ty_f32),
|
2012-08-22 19:58:05 -05:00
|
|
|
'F' => return ty::mk_mach_float(st.tcx, ast::ty_f64),
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => fail!(~"parse_ty: bad numeric type")
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
'c' => return ty::mk_char(st.tcx),
|
|
|
|
't' => {
|
2012-02-13 13:55:23 -06:00
|
|
|
assert (next(st) == '[');
|
2013-01-17 08:13:26 -06:00
|
|
|
let def = parse_def(st, NominalType, conv);
|
2012-04-18 23:26:25 -05:00
|
|
|
let substs = parse_substs(st, conv);
|
|
|
|
assert 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' => {
|
2012-04-24 17:52:52 -05:00
|
|
|
assert next(st) == '[';
|
2013-01-17 08:13:26 -06:00
|
|
|
let def = parse_def(st, NominalType, conv);
|
2012-04-24 17:52:52 -05:00
|
|
|
let substs = parse_substs(st, conv);
|
2012-08-14 17:27:06 -05:00
|
|
|
let vstore = parse_vstore(st);
|
2012-04-24 17:52:52 -05:00
|
|
|
assert next(st) == ']';
|
2012-08-14 17:27:06 -05:00
|
|
|
return ty::mk_trait(st.tcx, def, substs, vstore);
|
2011-12-29 04:23:35 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
'p' => {
|
2013-01-17 08:13:26 -06:00
|
|
|
let did = parse_def(st, TypeParameter, conv);
|
2013-01-10 12:59:58 -06:00
|
|
|
debug!("parsed ty_param: did=%?", did);
|
2012-08-01 19:30:05 -05:00
|
|
|
return ty::mk_param(st.tcx, parse_int(st) as uint, did);
|
2011-07-28 15:29:29 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
's' => {
|
2012-08-01 19:30:05 -05:00
|
|
|
return ty::mk_self(st.tcx);
|
2012-01-30 04:52:34 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
'@' => return ty::mk_box(st.tcx, parse_mt(st, conv)),
|
|
|
|
'~' => return ty::mk_uniq(st.tcx, parse_mt(st, conv)),
|
|
|
|
'*' => return ty::mk_ptr(st.tcx, parse_mt(st, conv)),
|
|
|
|
'&' => {
|
2012-04-23 17:23:20 -05:00
|
|
|
let r = parse_region(st);
|
|
|
|
let mt = parse_mt(st, conv);
|
2012-08-01 19:30:05 -05:00
|
|
|
return ty::mk_rptr(st.tcx, r, mt);
|
2012-04-23 17:23:20 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
'U' => return ty::mk_unboxed_vec(st.tcx, parse_mt(st, conv)),
|
|
|
|
'V' => {
|
2012-04-10 20:32:51 -05:00
|
|
|
let mt = parse_mt(st, conv);
|
|
|
|
let v = parse_vstore(st);
|
2012-08-01 19:30:05 -05:00
|
|
|
return ty::mk_evec(st.tcx, mt, v);
|
2012-04-10 20:32:51 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
'v' => {
|
2012-04-10 20:32:51 -05:00
|
|
|
let v = parse_vstore(st);
|
2012-08-01 19:30:05 -05:00
|
|
|
return ty::mk_estr(st.tcx, v);
|
2012-04-10 20:32:51 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
'T' => {
|
2012-02-13 13:55:23 -06:00
|
|
|
assert (next(st) == '[');
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut params = ~[];
|
2012-09-26 19:33:34 -05:00
|
|
|
while peek(st) != ']' { params.push(parse_ty(st, conv)); }
|
2011-08-15 04:40:26 -05:00
|
|
|
st.pos = st.pos + 1u;
|
2012-08-01 19:30:05 -05:00
|
|
|
return ty::mk_tup(st.tcx, params);
|
2011-08-15 04:40:26 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
'f' => {
|
2013-01-31 19:12:29 -06:00
|
|
|
return ty::mk_closure(st.tcx, parse_closure_ty(st, conv));
|
|
|
|
}
|
|
|
|
'F' => {
|
|
|
|
return ty::mk_bare_fn(st.tcx, parse_bare_fn_ty(st, conv));
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
'Y' => return ty::mk_type(st.tcx),
|
|
|
|
'C' => {
|
2013-01-31 19:12:29 -06:00
|
|
|
let sigil = parse_sigil(st);
|
|
|
|
return ty::mk_opaque_closure_ptr(st.tcx, sigil);
|
2012-01-05 18:19:12 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
'#' => {
|
2011-07-27 07:19:39 -05:00
|
|
|
let pos = parse_hex(st);
|
2012-02-13 13:55:23 -06:00
|
|
|
assert (next(st) == ':');
|
2011-07-27 07:19:39 -05:00
|
|
|
let len = parse_hex(st);
|
2012-02-13 13:55:23 -06:00
|
|
|
assert (next(st) == '#');
|
2013-01-31 19:12:29 -06:00
|
|
|
let key = ty::creader_cache_key {cnum: st.crate,
|
|
|
|
pos: pos,
|
|
|
|
len: len };
|
2013-02-05 21:41:45 -06:00
|
|
|
match st.tcx.rcache.find(&key) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(tt) => return tt,
|
|
|
|
None => {
|
2013-02-04 16:02:01 -06:00
|
|
|
let ps = @mut PState {pos: pos ,.. copy *st};
|
2012-01-05 09:04:59 -06:00
|
|
|
let tt = parse_ty(ps, conv);
|
2013-01-25 18:57:39 -06:00
|
|
|
st.tcx.rcache.insert(key, tt);
|
2012-08-01 19:30:05 -05:00
|
|
|
return tt;
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
'"' => {
|
2013-01-17 08:13:26 -06:00
|
|
|
let def = parse_def(st, TypeWithId, conv);
|
2012-01-16 04:45:18 -06:00
|
|
|
let inner = parse_ty(st, conv);
|
2012-02-10 12:28:35 -06:00
|
|
|
ty::mk_with_id(st.tcx, inner, def)
|
2012-01-16 04:45:18 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
'B' => ty::mk_opaque_box(st.tcx),
|
|
|
|
'a' => {
|
2012-03-06 10:02:13 -06:00
|
|
|
assert (next(st) == '[');
|
2013-01-17 08:13:26 -06:00
|
|
|
let did = parse_def(st, NominalType, conv);
|
2012-04-18 23:26:25 -05:00
|
|
|
let substs = parse_substs(st, conv);
|
2012-03-06 10:02:13 -06:00
|
|
|
assert (next(st) == ']');
|
2012-12-10 15:47:54 -06:00
|
|
|
return ty::mk_struct(st.tcx, did, substs);
|
2012-03-06 10:02:13 -06:00
|
|
|
}
|
2013-02-11 21:26:38 -06:00
|
|
|
c => { error!("unexpected char in type string: %c", c); fail!();}
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn parse_mt(st: @mut PState, conv: conv_did) -> ty::mt {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut m;
|
2012-08-06 14:34:08 -05:00
|
|
|
match peek(st) {
|
2012-08-03 21:59:04 -05:00
|
|
|
'm' => { next(st); m = ast::m_mutbl; }
|
|
|
|
'?' => { next(st); m = ast::m_const; }
|
|
|
|
_ => { m = ast::m_imm; }
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
2013-01-14 23:36:27 -06:00
|
|
|
ty::mt { ty: parse_ty(st, conv), mutbl: m }
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn parse_def(st: @mut PState, source: DefIdSource,
|
2013-01-17 08:13:26 -06:00
|
|
|
conv: conv_did) -> ast::def_id {
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut def = ~[];
|
2012-09-26 19:33:34 -05:00
|
|
|
while peek(st) != '|' { def.push(next_byte(st)); }
|
2011-06-27 17:30:17 -05:00
|
|
|
st.pos = st.pos + 1u;
|
2013-01-17 08:13:26 -06:00
|
|
|
return conv(source, parse_def_id(def));
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn parse_int(st: @mut PState) -> int {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut n = 0;
|
2012-03-10 22:34:17 -06:00
|
|
|
loop {
|
2012-02-13 13:55:23 -06:00
|
|
|
let cur = peek(st);
|
2012-08-01 19:30:05 -05:00
|
|
|
if cur < '0' || cur > '9' { return n; }
|
2011-06-27 17:30:17 -05:00
|
|
|
st.pos = st.pos + 1u;
|
|
|
|
n *= 10;
|
|
|
|
n += (cur as int) - ('0' as int);
|
2012-03-10 22:34:17 -06:00
|
|
|
};
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn parse_hex(st: @mut PState) -> uint {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut n = 0u;
|
2012-03-10 22:34:17 -06:00
|
|
|
loop {
|
2012-02-13 13:55:23 -06:00
|
|
|
let cur = peek(st);
|
2012-08-01 19:30:05 -05:00
|
|
|
if (cur < '0' || cur > '9') && (cur < 'a' || cur > 'f') { return n; }
|
2011-06-27 17:30:17 -05:00
|
|
|
st.pos = st.pos + 1u;
|
|
|
|
n *= 16u;
|
2011-07-27 07:19:39 -05:00
|
|
|
if '0' <= cur && cur <= '9' {
|
2011-06-27 17:30:17 -05:00
|
|
|
n += (cur as uint) - ('0' as uint);
|
|
|
|
} else { n += 10u + (cur as uint) - ('a' as uint); }
|
2012-03-10 22:34:17 -06:00
|
|
|
};
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
|
2012-05-25 01:44:58 -05:00
|
|
|
fn parse_purity(c: char) -> purity {
|
2012-08-22 19:58:05 -05:00
|
|
|
match c {
|
2012-08-03 21:59:04 -05:00
|
|
|
'u' => unsafe_fn,
|
|
|
|
'p' => pure_fn,
|
|
|
|
'i' => impure_fn,
|
2012-08-22 19:58:05 -05:00
|
|
|
'c' => extern_fn,
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => fail!(~"parse_purity: bad purity")
|
2012-05-25 01:44:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
fn parse_abi(c: char) -> Abi {
|
|
|
|
match c {
|
|
|
|
'r' => ast::RustAbi,
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => fail!(fmt!("parse_abi: bad ABI '%c'", c))
|
2013-01-31 19:12:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-02 15:33:51 -05:00
|
|
|
fn parse_onceness(c: char) -> ast::Onceness {
|
|
|
|
match c {
|
|
|
|
'o' => ast::Once,
|
|
|
|
'm' => ast::Many,
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => fail!(~"parse_onceness: bad onceness")
|
2012-11-02 15:33:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn parse_arg(st: @mut PState, conv: conv_did) -> ty::arg {
|
2013-01-25 18:57:39 -06:00
|
|
|
ty::arg { mode: parse_mode(st), ty: parse_ty(st, conv) }
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn parse_mode(st: @mut PState) -> ast::mode {
|
2012-09-11 23:25:01 -05:00
|
|
|
let m = ast::expl(match next(st) {
|
|
|
|
'+' => ast::by_copy,
|
|
|
|
'=' => ast::by_ref,
|
|
|
|
'#' => ast::by_val,
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => fail!(~"bad mode")
|
2012-09-11 23:25:01 -05:00
|
|
|
});
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn parse_closure_ty(st: @mut PState, conv: conv_did) -> ty::ClosureTy {
|
2013-01-31 19:12:29 -06:00
|
|
|
let sigil = parse_sigil(st);
|
2012-05-25 01:44:58 -05:00
|
|
|
let purity = parse_purity(next(st));
|
2012-11-02 15:33:51 -05:00
|
|
|
let onceness = parse_onceness(next(st));
|
2012-11-04 22:41:00 -06:00
|
|
|
let region = parse_region(st);
|
2013-01-31 19:12:29 -06:00
|
|
|
let sig = parse_sig(st, conv);
|
|
|
|
ty::ClosureTy {
|
|
|
|
purity: purity,
|
|
|
|
sigil: sigil,
|
|
|
|
onceness: onceness,
|
|
|
|
region: region,
|
|
|
|
sig: sig
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn parse_bare_fn_ty(st: @mut PState, conv: conv_did) -> ty::BareFnTy {
|
2013-01-31 19:12:29 -06:00
|
|
|
let purity = parse_purity(next(st));
|
|
|
|
let abi = parse_abi(next(st));
|
|
|
|
let sig = parse_sig(st, conv);
|
|
|
|
ty::BareFnTy {
|
|
|
|
purity: purity,
|
|
|
|
abi: abi,
|
|
|
|
sig: sig
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn parse_sig(st: @mut PState, conv: conv_did) -> ty::FnSig {
|
2012-02-13 13:55:23 -06:00
|
|
|
assert (next(st) == '[');
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut inputs: ~[ty::arg] = ~[];
|
2012-02-13 13:55:23 -06:00
|
|
|
while peek(st) != ']' {
|
2012-09-11 23:25:01 -05:00
|
|
|
let mode = parse_mode(st);
|
2013-01-25 18:57:39 -06:00
|
|
|
inputs.push(ty::arg { mode: mode, ty: parse_ty(st, conv) });
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
st.pos += 1u; // eat the ']'
|
2013-01-08 08:21:19 -06:00
|
|
|
let ret_ty = parse_ty(st, conv);
|
2013-01-31 19:12:29 -06:00
|
|
|
ty::FnSig {inputs: inputs, output: ret_ty}
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rust metadata parsing
|
2013-01-29 18:51:16 -06:00
|
|
|
pub fn parse_def_id(buf: &[u8]) -> ast::def_id {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut colon_idx = 0u;
|
2012-01-05 06:57:27 -06:00
|
|
|
let len = vec::len(buf);
|
2011-08-19 17:16:48 -05:00
|
|
|
while colon_idx < len && buf[colon_idx] != ':' as u8 { colon_idx += 1u; }
|
2011-07-27 07:19:39 -05:00
|
|
|
if colon_idx == len {
|
2012-08-22 19:24:52 -05:00
|
|
|
error!("didn't find ':' when parsing def id");
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!();
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
2012-08-17 17:54:18 -05:00
|
|
|
|
2013-02-08 13:28:20 -06:00
|
|
|
let crate_part = vec::slice(buf, 0u, colon_idx);
|
|
|
|
let def_part = vec::slice(buf, colon_idx + 1u, len);
|
2011-07-12 12:59:18 -05:00
|
|
|
|
2012-09-14 11:55:33 -05:00
|
|
|
let crate_num = match uint::parse_bytes(crate_part, 10u) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(cn) => cn as int,
|
2013-02-11 21:26:38 -06:00
|
|
|
None => fail!(fmt!("internal error: parse_def_id: crate number \
|
2012-08-22 19:24:52 -05:00
|
|
|
expected, but found %?", crate_part))
|
2012-03-06 10:02:13 -06:00
|
|
|
};
|
2012-09-14 11:55:33 -05:00
|
|
|
let def_num = match uint::parse_bytes(def_part, 10u) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(dn) => dn as int,
|
2013-02-11 21:26:38 -06:00
|
|
|
None => fail!(fmt!("internal error: parse_def_id: id expected, but \
|
2012-08-22 19:24:52 -05:00
|
|
|
found %?", def_part))
|
2012-03-06 10:02:13 -06:00
|
|
|
};
|
2013-01-13 13:05:40 -06:00
|
|
|
ast::def_id { crate: crate_num, node: def_num }
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
2011-07-19 19:52:34 -05:00
|
|
|
|
2013-01-29 18:51:16 -06:00
|
|
|
pub fn parse_bounds_data(data: @~[u8], start: uint,
|
|
|
|
crate_num: int, tcx: ty::ctxt, conv: conv_did)
|
|
|
|
-> @~[ty::param_bound] {
|
2012-09-11 23:25:01 -05:00
|
|
|
let st = parse_state_from_data(data, crate_num, start, tcx);
|
2012-01-05 09:04:59 -06:00
|
|
|
parse_bounds(st, conv)
|
2011-12-28 10:50:12 -06:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
fn parse_bounds(st: @mut PState, conv: conv_did) -> @~[ty::param_bound] {
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut bounds = ~[];
|
2012-03-10 22:34:17 -06:00
|
|
|
loop {
|
2012-09-26 19:33:34 -05:00
|
|
|
bounds.push(match next(st) {
|
2012-12-11 15:50:04 -06:00
|
|
|
'S' => ty::bound_owned,
|
2012-08-03 21:59:04 -05:00
|
|
|
'C' => ty::bound_copy,
|
|
|
|
'K' => ty::bound_const,
|
2012-12-11 13:59:45 -06:00
|
|
|
'O' => ty::bound_durable,
|
2012-08-03 21:59:04 -05:00
|
|
|
'I' => ty::bound_trait(parse_ty(st, conv)),
|
2012-08-22 19:58:05 -05:00
|
|
|
'.' => break,
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => fail!(~"parse_bounds: bad bounds")
|
2012-06-26 02:39:18 -05:00
|
|
|
});
|
2011-12-28 10:50:12 -06:00
|
|
|
}
|
|
|
|
@bounds
|
|
|
|
}
|
|
|
|
|
2011-07-19 19:52:34 -05:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|
|
|
|
//
|