2011-06-27 17:30:17 -05:00
|
|
|
// Type decoding
|
2011-06-27 18:03:01 -05:00
|
|
|
|
2011-07-05 04:48:19 -05:00
|
|
|
import syntax::ast;
|
2011-07-19 19:52:34 -05:00
|
|
|
import syntax::ast::*;
|
2011-08-21 23:44:41 -05:00
|
|
|
import syntax::ast_util;
|
|
|
|
import syntax::ast_util::respan;
|
2011-06-27 17:30:17 -05:00
|
|
|
import middle::ty;
|
2012-03-07 18:48:57 -06:00
|
|
|
import std::map::hashmap;
|
2011-06-27 17:30:17 -05:00
|
|
|
|
2012-03-06 10:02:13 -06:00
|
|
|
export parse_ty_data, parse_def_id, parse_ident;
|
2011-12-28 10:50:12 -06:00
|
|
|
export parse_bounds_data;
|
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
|
|
|
|
|
|
|
// Callback to translate defs to strs or back:
|
2012-01-23 16:59:00 -06:00
|
|
|
type conv_did = fn(ast::def_id) -> ast::def_id;
|
2011-06-27 17:30:17 -05:00
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
type pstate = {data: @~[u8], crate: int, mut pos: uint, tcx: ty::ctxt};
|
2011-06-27 17:30:17 -05:00
|
|
|
|
2012-02-13 13:55:23 -06:00
|
|
|
fn peek(st: @pstate) -> char {
|
|
|
|
st.data[st.pos] as char
|
2011-12-28 10:50:12 -06:00
|
|
|
}
|
2011-06-27 17:30:17 -05:00
|
|
|
|
2012-02-13 13:55:23 -06:00
|
|
|
fn next(st: @pstate) -> char {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-02-13 13:55:23 -06:00
|
|
|
fn next_byte(st: @pstate) -> u8 {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn parse_ident(st: @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
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn parse_ident_(st: @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-08-01 19:30:05 -05:00
|
|
|
return @rslt;
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn parse_ty_data(data: @~[u8], crate_num: int, pos: uint, tcx: ty::ctxt,
|
2012-01-05 09:04:59 -06:00
|
|
|
conv: conv_did) -> ty::t {
|
2012-03-26 20:35:18 -05:00
|
|
|
let st = @{data: data, crate: crate_num, mut pos: pos, tcx: tcx};
|
2012-01-05 09:04:59 -06:00
|
|
|
parse_ty(st, conv)
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn parse_ret_ty(st: @pstate, conv: conv_did) -> (ast::ret_style, ty::t) {
|
2012-02-13 13:55:23 -06:00
|
|
|
alt peek(st) {
|
2011-09-14 10:18:48 -05:00
|
|
|
'!' { next(st); (ast::noreturn, ty::mk_bot(st.tcx)) }
|
2012-01-05 09:04:59 -06:00
|
|
|
_ { (ast::return_val, parse_ty(st, conv)) }
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn parse_path(st: @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-06-26 02:39:18 -05:00
|
|
|
vec::push(idents, parse_ident_(st, is_last));
|
2012-03-10 22:34:17 -06:00
|
|
|
loop {
|
2012-02-13 13:55:23 -06:00
|
|
|
alt peek(st) {
|
2011-07-27 07:19:39 -05:00
|
|
|
':' { next(st); next(st); }
|
|
|
|
c {
|
|
|
|
if c == '(' {
|
2012-08-01 19:30:05 -05:00
|
|
|
return @{span: ast_util::dummy_sp(),
|
2012-04-24 17:52:52 -05:00
|
|
|
global: false, idents: idents,
|
2012-06-29 18:26:56 -05:00
|
|
|
rp: none, types: ~[]};
|
2012-06-26 02:39:18 -05:00
|
|
|
} else { vec::push(idents, 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
|
|
|
}
|
|
|
|
|
2012-05-25 01:44:58 -05:00
|
|
|
fn parse_ty_rust_fn(st: @pstate, conv: conv_did) -> ty::t {
|
2012-08-01 19:30:05 -05:00
|
|
|
return ty::mk_fn(st.tcx, parse_ty_fn(st, conv));
|
2011-12-09 18:56:48 -06:00
|
|
|
}
|
|
|
|
|
2012-01-12 17:38:44 -06:00
|
|
|
fn parse_proto(c: char) -> ast::proto {
|
|
|
|
alt c {
|
|
|
|
'~' { ast::proto_uniq }
|
|
|
|
'@' { ast::proto_box }
|
|
|
|
'&' { ast::proto_block }
|
|
|
|
'n' { ast::proto_bare }
|
2012-07-14 00:57:48 -05:00
|
|
|
_ { fail ~"illegal fn type kind " + str::from_char(c); }
|
2012-01-12 17:38:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-10 20:32:51 -05:00
|
|
|
fn parse_vstore(st: @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
|
|
|
}
|
|
|
|
|
|
|
|
alt check next(st) {
|
|
|
|
'~' { ty::vstore_uniq }
|
|
|
|
'@' { ty::vstore_box }
|
|
|
|
'&' { ty::vstore_slice(parse_region(st)) }
|
|
|
|
}
|
2012-04-10 20:32:51 -05:00
|
|
|
}
|
|
|
|
|
2012-04-18 23:26:25 -05:00
|
|
|
fn parse_substs(st: @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-06-26 02:39:18 -05:00
|
|
|
while peek(st) != ']' { vec::push(params, parse_ty(st, conv)); }
|
2012-04-18 23:26:25 -05:00
|
|
|
st.pos = st.pos + 1u;
|
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
return {self_r: self_r,
|
2012-05-09 08:09:58 -05:00
|
|
|
self_ty: self_ty,
|
|
|
|
tps: params};
|
2012-04-18 23:26:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_bound_region(st: @pstate) -> ty::bound_region {
|
|
|
|
alt check next(st) {
|
|
|
|
's' { ty::br_self }
|
|
|
|
'a' { ty::br_anon }
|
2012-06-10 02:49:59 -05:00
|
|
|
'[' { ty::br_named(@parse_str(st, ']')) }
|
2012-07-25 11:19:59 -05:00
|
|
|
'c' {
|
|
|
|
let id = parse_int(st);
|
|
|
|
assert next(st) == '|';
|
|
|
|
ty::br_cap_avoid(id, @parse_bound_region(st))
|
|
|
|
}
|
2012-04-18 23:26:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_region(st: @pstate) -> ty::region {
|
|
|
|
alt check next(st) {
|
|
|
|
'b' {
|
|
|
|
ty::re_bound(parse_bound_region(st))
|
|
|
|
}
|
|
|
|
'f' {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
's' {
|
|
|
|
let id = parse_int(st);
|
|
|
|
assert next(st) == '|';
|
|
|
|
ty::re_scope(id)
|
|
|
|
}
|
|
|
|
't' {
|
|
|
|
ty::re_static
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_opt<T>(st: @pstate, f: fn() -> T) -> option<T> {
|
|
|
|
alt check next(st) {
|
|
|
|
'n' { none }
|
|
|
|
's' { some(f()) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn parse_str(st: @pstate, term: char) -> ~str {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn parse_ty(st: @pstate, conv: conv_did) -> ty::t {
|
2012-02-15 02:40:42 -06:00
|
|
|
alt check next(st) {
|
2012-08-01 19:30:05 -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); }
|
2011-07-27 07:19:39 -05:00
|
|
|
'M' {
|
2012-02-15 02:40:42 -06:00
|
|
|
alt check next(st) {
|
2012-08-01 19:30:05 -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); }
|
|
|
|
'F' { return ty::mk_mach_float(st.tcx, ast::ty_f64); }
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
'c' { return ty::mk_char(st.tcx); }
|
2011-07-27 07:19:39 -05:00
|
|
|
't' {
|
2012-02-13 13:55:23 -06:00
|
|
|
assert (next(st) == '[');
|
2012-01-05 09:04:59 -06:00
|
|
|
let def = parse_def(st, 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
|
|
|
}
|
2011-12-29 04:23:35 -06:00
|
|
|
'x' {
|
2012-04-24 17:52:52 -05:00
|
|
|
assert next(st) == '[';
|
2012-01-05 09:04:59 -06:00
|
|
|
let def = parse_def(st, conv);
|
2012-04-24 17:52:52 -05:00
|
|
|
let substs = parse_substs(st, conv);
|
|
|
|
assert next(st) == ']';
|
2012-08-01 19:30:05 -05:00
|
|
|
return ty::mk_trait(st.tcx, def, substs);
|
2011-12-29 04:23:35 -06:00
|
|
|
}
|
2011-07-28 15:29:29 -05:00
|
|
|
'p' {
|
2012-01-05 09:04:59 -06:00
|
|
|
let did = parse_def(st, conv);
|
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-01-30 04:52:34 -06: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-01 19:30:05 -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-01 19:30:05 -05:00
|
|
|
'U' { return ty::mk_unboxed_vec(st.tcx, parse_mt(st, conv)); }
|
2012-04-10 20:32:51 -05:00
|
|
|
'V' {
|
|
|
|
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
|
|
|
}
|
|
|
|
'v' {
|
|
|
|
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
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
'R' {
|
2012-02-13 13:55:23 -06:00
|
|
|
assert (next(st) == '[');
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut fields: ~[ty::field] = ~[];
|
2012-02-13 13:55:23 -06:00
|
|
|
while peek(st) != ']' {
|
2012-06-10 02:49:59 -05:00
|
|
|
let name = @parse_str(st, '=');
|
2012-06-26 02:39:18 -05:00
|
|
|
vec::push(fields, {ident: name, mt: parse_mt(st, conv)});
|
2011-06-28 18:11:41 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
st.pos = st.pos + 1u;
|
2012-08-01 19:30:05 -05:00
|
|
|
return ty::mk_rec(st.tcx, fields);
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-08-15 04:40:26 -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-06-26 02:39:18 -05:00
|
|
|
while peek(st) != ']' { vec::push(params, 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
|
|
|
}
|
2011-10-11 16:38:15 -05:00
|
|
|
'f' {
|
2012-05-25 01:44:58 -05:00
|
|
|
parse_ty_rust_fn(st, conv)
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-04-01 16:28:30 -05:00
|
|
|
'X' {
|
2012-08-01 19:30:05 -05:00
|
|
|
return ty::mk_var(st.tcx, ty::tv_vid(parse_int(st) as uint));
|
2012-04-01 16:28:30 -05:00
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
'Y' { return ty::mk_type(st.tcx); }
|
2012-01-05 18:19:12 -06:00
|
|
|
'C' {
|
2012-02-15 02:40:42 -06:00
|
|
|
let ck = alt check next(st) {
|
2012-01-10 08:49:15 -06:00
|
|
|
'&' { ty::ck_block }
|
|
|
|
'@' { ty::ck_box }
|
|
|
|
'~' { ty::ck_uniq }
|
2012-01-05 18:19:12 -06:00
|
|
|
};
|
2012-08-01 19:30:05 -05:00
|
|
|
return ty::mk_opaque_closure_ptr(st.tcx, ck);
|
2012-01-05 18:19:12 -06: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) == '#');
|
2011-07-27 07:19:39 -05:00
|
|
|
alt st.tcx.rcache.find({cnum: st.crate, pos: pos, len: len}) {
|
2012-08-01 19:30:05 -05:00
|
|
|
some(tt) { return tt; }
|
2012-01-19 00:37:22 -06:00
|
|
|
none {
|
2012-01-05 06:57:27 -06:00
|
|
|
let ps = @{pos: pos with *st};
|
2012-01-05 09:04:59 -06:00
|
|
|
let tt = parse_ty(ps, conv);
|
2011-07-27 07:19:39 -05:00
|
|
|
st.tcx.rcache.insert({cnum: st.crate, pos: pos, len: len}, 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-01-16 04:45:18 -06:00
|
|
|
'"' {
|
2012-02-10 12:28:35 -06:00
|
|
|
let def = parse_def(st, 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-02-07 04:25:04 -06:00
|
|
|
'B' { ty::mk_opaque_box(st.tcx) }
|
2012-03-06 10:02:13 -06:00
|
|
|
'a' {
|
2012-07-30 18:01:07 -05:00
|
|
|
debug!{"saw a class"};
|
2012-03-06 10:02:13 -06:00
|
|
|
assert (next(st) == '[');
|
2012-07-30 18:01:07 -05:00
|
|
|
debug!{"saw a ["};
|
2012-03-06 10:02:13 -06:00
|
|
|
let did = parse_def(st, conv);
|
2012-07-30 18:01:07 -05:00
|
|
|
debug!{"parsed a def_id %?", did};
|
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-08-01 19:30:05 -05:00
|
|
|
return ty::mk_class(st.tcx, did, substs);
|
2012-03-06 10:02:13 -06:00
|
|
|
}
|
2012-07-30 18:01:07 -05:00
|
|
|
c { error!{"unexpected char in type string: %c", c}; fail;}
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn parse_mt(st: @pstate, conv: conv_did) -> ty::mt {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut m;
|
2012-02-13 13:55:23 -06:00
|
|
|
alt peek(st) {
|
2012-02-15 13:25:39 -06: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
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return {ty: parse_ty(st, conv), mutbl: m};
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn parse_def(st: @pstate, conv: conv_did) -> ast::def_id {
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut def = ~[];
|
2012-06-26 02:39:18 -05:00
|
|
|
while peek(st) != '|' { vec::push(def, next_byte(st)); }
|
2011-06-27 17:30:17 -05:00
|
|
|
st.pos = st.pos + 1u;
|
2012-08-01 19:30:05 -05:00
|
|
|
return conv(parse_def_id(def));
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn parse_int(st: @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
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn parse_hex(st: @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 {
|
|
|
|
alt check c {
|
|
|
|
'u' {unsafe_fn}
|
|
|
|
'p' {pure_fn}
|
|
|
|
'i' {impure_fn}
|
2012-06-26 18:18:37 -05:00
|
|
|
'c' {extern_fn}
|
2012-05-25 01:44:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn parse_ty_fn(st: @pstate, conv: conv_did) -> ty::fn_ty {
|
2012-05-25 01:44:58 -05:00
|
|
|
let proto = parse_proto(next(st));
|
|
|
|
let purity = parse_purity(next(st));
|
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-02-15 02:40:42 -06:00
|
|
|
let mode = alt check peek(st) {
|
2012-02-15 13:25:39 -06:00
|
|
|
'&' { ast::by_mutbl_ref }
|
2011-10-06 05:26:12 -05:00
|
|
|
'-' { ast::by_move }
|
2011-11-16 05:32:38 -06:00
|
|
|
'+' { ast::by_copy }
|
2011-10-06 05:26:12 -05:00
|
|
|
'=' { ast::by_ref }
|
2011-11-16 05:32:38 -06:00
|
|
|
'#' { ast::by_val }
|
2011-10-06 05:26:12 -05:00
|
|
|
};
|
|
|
|
st.pos += 1u;
|
2012-06-26 02:39:18 -05:00
|
|
|
vec::push(inputs, {mode: ast::expl(mode), ty: parse_ty(st, conv)});
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
st.pos += 1u; // eat the ']'
|
2012-01-05 09:04:59 -06:00
|
|
|
let (ret_style, ret_ty) = parse_ret_ty(st, conv);
|
2012-08-01 19:30:05 -05:00
|
|
|
return {purity: purity, proto: proto, inputs: inputs, output: ret_ty,
|
2012-07-13 20:43:52 -05:00
|
|
|
ret_style: ret_style};
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Rust metadata parsing
|
2012-07-03 19:11:39 -05:00
|
|
|
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-07-30 18:01:07 -05:00
|
|
|
error!{"didn't find ':' when parsing def id"};
|
2011-06-27 17:30:17 -05:00
|
|
|
fail;
|
|
|
|
}
|
2012-04-06 13:01:43 -05: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-04-06 13:01:43 -05:00
|
|
|
let crate_num = alt uint::parse_buf(crate_part, 10u) {
|
2012-03-06 10:02:13 -06:00
|
|
|
some(cn) { cn as int }
|
2012-07-30 18:01:07 -05:00
|
|
|
none { fail (fmt!{"internal error: parse_def_id: crate number \
|
|
|
|
expected, but found %?", crate_part}); }
|
2012-03-06 10:02:13 -06:00
|
|
|
};
|
2012-04-06 13:01:43 -05:00
|
|
|
let def_num = alt uint::parse_buf(def_part, 10u) {
|
2012-03-06 10:02:13 -06:00
|
|
|
some(dn) { dn as int }
|
2012-07-30 18:01:07 -05:00
|
|
|
none { fail (fmt!{"internal error: parse_def_id: id expected, but \
|
|
|
|
found %?", def_part}); }
|
2012-03-06 10:02:13 -06:00
|
|
|
};
|
2012-08-01 19:30:05 -05:00
|
|
|
return {crate: crate_num, node: def_num};
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
2011-07-19 19:52:34 -05:00
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn parse_bounds_data(data: @~[u8], start: uint,
|
2012-01-05 09:04:59 -06:00
|
|
|
crate_num: int, tcx: ty::ctxt, conv: conv_did)
|
2012-06-29 18:26:56 -05:00
|
|
|
-> @~[ty::param_bound] {
|
2012-03-26 20:35:18 -05:00
|
|
|
let st = @{data: data, crate: crate_num, mut pos: start, tcx: tcx};
|
2012-01-05 09:04:59 -06:00
|
|
|
parse_bounds(st, conv)
|
2011-12-28 10:50:12 -06:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn parse_bounds(st: @pstate, conv: conv_did) -> @~[ty::param_bound] {
|
|
|
|
let mut bounds = ~[];
|
2012-03-10 22:34:17 -06:00
|
|
|
loop {
|
2012-06-26 02:39:18 -05:00
|
|
|
vec::push(bounds, alt check next(st) {
|
2011-12-28 10:50:12 -06:00
|
|
|
'S' { ty::bound_send }
|
|
|
|
'C' { ty::bound_copy }
|
2012-05-24 12:23:46 -05:00
|
|
|
'K' { ty::bound_const }
|
2012-07-16 22:17:57 -05:00
|
|
|
'O' { ty::bound_owned }
|
2012-07-03 18:30:42 -05:00
|
|
|
'I' { ty::bound_trait(parse_ty(st, conv)) }
|
2012-01-05 06:57:27 -06:00
|
|
|
'.' { break; }
|
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:
|
|
|
|
//
|