2011-06-27 17:30:17 -05:00
|
|
|
// Type decoding
|
2011-06-27 18:03:01 -05:00
|
|
|
|
2011-12-13 18:25:51 -06:00
|
|
|
import core::{vec, str, uint};
|
|
|
|
import option::{none, some};
|
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-01-05 09:04:59 -06:00
|
|
|
export parse_ty_data, parse_def_id;
|
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-05 09:04:59 -06:00
|
|
|
type conv_did = block(ast::def_id) -> ast::def_id;
|
2011-06-27 17:30:17 -05:00
|
|
|
|
2012-01-05 06:57:27 -06:00
|
|
|
type pstate = {data: @[u8], crate: int, mutable pos: uint, tcx: ty::ctxt};
|
2011-06-27 17:30:17 -05:00
|
|
|
|
2011-12-28 10:50:12 -06:00
|
|
|
fn peek(st: @pstate) -> u8 {
|
2012-01-05 06:57:27 -06:00
|
|
|
st.data[st.pos]
|
2011-12-28 10:50:12 -06:00
|
|
|
}
|
2011-06-27 17:30:17 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn next(st: @pstate) -> u8 {
|
2011-08-19 17:16:48 -05:00
|
|
|
let ch = st.data[st.pos];
|
2011-06-27 17:30:17 -05:00
|
|
|
st.pos = st.pos + 1u;
|
|
|
|
ret ch;
|
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn parse_ident(st: @pstate, last: char) -> ast::ident {
|
2011-07-27 07:19:39 -05:00
|
|
|
fn is_last(b: char, c: char) -> bool { ret c == b; }
|
2012-01-05 09:04:59 -06:00
|
|
|
ret parse_ident_(st, bind is_last(last, _));
|
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 {
|
2011-09-02 17:34:58 -05:00
|
|
|
let rslt = "";
|
2011-07-27 07:19:39 -05:00
|
|
|
while !is_last(peek(st) as char) {
|
2011-09-01 19:27:58 -05:00
|
|
|
rslt += str::unsafe_from_byte(next(st));
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
ret rslt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn parse_ty_data(data: @[u8], crate_num: int, pos: uint, tcx: ty::ctxt,
|
|
|
|
conv: conv_did) -> ty::t {
|
2012-01-05 06:57:27 -06:00
|
|
|
let st = @{data: data, crate: crate_num, mutable 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) {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt peek(st) as char {
|
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_constrs(st: @pstate, conv: conv_did) -> [@ty::constr] {
|
2011-08-19 17:16:48 -05:00
|
|
|
let rslt: [@ty::constr] = [];
|
2011-07-27 07:19:39 -05:00
|
|
|
alt peek(st) as char {
|
|
|
|
':' {
|
|
|
|
do {
|
|
|
|
next(st);
|
2012-01-05 09:04:59 -06:00
|
|
|
rslt += [parse_constr(st, conv, parse_constr_arg)];
|
2011-07-27 07:19:39 -05:00
|
|
|
} while peek(st) as char == ';'
|
|
|
|
}
|
|
|
|
_ { }
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
ret rslt;
|
|
|
|
}
|
|
|
|
|
2011-07-19 22:15:27 -05:00
|
|
|
// FIXME less copy-and-paste
|
2012-01-05 09:04:59 -06:00
|
|
|
fn parse_ty_constrs(st: @pstate, conv: conv_did) -> [@ty::type_constr] {
|
2011-08-19 17:16:48 -05:00
|
|
|
let rslt: [@ty::type_constr] = [];
|
2011-07-27 07:19:39 -05:00
|
|
|
alt peek(st) as char {
|
|
|
|
':' {
|
|
|
|
do {
|
|
|
|
next(st);
|
2012-01-05 09:04:59 -06:00
|
|
|
rslt += [parse_constr(st, conv, parse_ty_constr_arg)];
|
2011-07-27 07:19:39 -05:00
|
|
|
} while peek(st) as char == ';'
|
|
|
|
}
|
|
|
|
_ { }
|
2011-07-19 22:15:27 -05:00
|
|
|
}
|
|
|
|
ret rslt;
|
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn parse_path(st: @pstate) -> @ast::path {
|
2011-08-19 17:16:48 -05:00
|
|
|
let idents: [ast::ident] = [];
|
2011-07-27 07:19:39 -05:00
|
|
|
fn is_last(c: char) -> bool { ret c == '(' || c == ':'; }
|
2012-01-05 09:04:59 -06:00
|
|
|
idents += [parse_ident_(st, is_last)];
|
2011-07-27 07:19:39 -05:00
|
|
|
while true {
|
|
|
|
alt peek(st) as char {
|
|
|
|
':' { next(st); next(st); }
|
|
|
|
c {
|
|
|
|
if c == '(' {
|
2011-11-30 06:38:38 -06:00
|
|
|
ret @respan(ast_util::dummy_sp(),
|
|
|
|
{global: false, idents: idents, types: []});
|
2012-01-05 09:04:59 -06:00
|
|
|
} else { idents += [parse_ident_(st, is_last)]; }
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fail "parse_path: ill-formed path";
|
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn parse_constr_arg(st: @pstate) -> ast::fn_constr_arg {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt peek(st) as char {
|
|
|
|
'*' { st.pos += 1u; ret ast::carg_base; }
|
|
|
|
c {
|
2011-07-19 19:52:34 -05:00
|
|
|
|
|
|
|
/* how will we disambiguate between
|
|
|
|
an arg index and a lit argument? */
|
2011-07-27 07:19:39 -05:00
|
|
|
if c >= '0' && c <= '9' {
|
2011-07-19 19:52:34 -05:00
|
|
|
next(st);
|
2011-07-27 07:19:39 -05:00
|
|
|
// FIXME
|
|
|
|
ret ast::carg_ident((c as uint) - 48u);
|
|
|
|
} else {
|
2011-12-22 16:42:52 -06:00
|
|
|
#error("Lit args are unimplemented");
|
2011-07-27 07:19:39 -05:00
|
|
|
fail; // FIXME
|
2011-07-19 19:52:34 -05:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
else {
|
2012-01-05 09:04:59 -06:00
|
|
|
auto lit = parse_lit(st, conv, ',');
|
2011-07-19 19:52:34 -05:00
|
|
|
args += [respan(st.span, ast::carg_lit(lit))];
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn parse_ty_constr_arg(st: @pstate) -> ast::constr_arg_general_<@path> {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt peek(st) as char {
|
|
|
|
'*' { st.pos += 1u; ret ast::carg_base; }
|
2012-01-05 09:04:59 -06:00
|
|
|
c { ret ast::carg_ident(parse_path(st)); }
|
2011-07-19 22:15:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn parse_constr<T: copy>(st: @pstate, conv: conv_did,
|
|
|
|
pser: block(@pstate) -> ast::constr_arg_general_<T>)
|
|
|
|
-> @ty::constr_general<T> {
|
2011-08-21 23:44:41 -05:00
|
|
|
let sp = ast_util::dummy_sp(); // FIXME: use a real span
|
2011-08-19 17:16:48 -05:00
|
|
|
let args: [@sp_constr_arg<T>] = [];
|
2012-01-05 09:04:59 -06:00
|
|
|
let pth = parse_path(st);
|
2011-07-27 07:19:39 -05:00
|
|
|
let ignore: char = next(st) as char;
|
2011-11-30 06:38:38 -06:00
|
|
|
assert (ignore == '(');
|
2012-01-05 09:04:59 -06:00
|
|
|
let def = parse_def(st, conv);
|
2011-08-12 09:15:18 -05:00
|
|
|
let an_arg: constr_arg_general_<T>;
|
2011-07-27 07:19:39 -05:00
|
|
|
do {
|
2012-01-05 09:04:59 -06:00
|
|
|
an_arg = pser(st);
|
2011-07-19 19:52:34 -05:00
|
|
|
// FIXME use a real span
|
2011-08-19 17:16:48 -05:00
|
|
|
args += [@respan(sp, an_arg)];
|
2011-06-27 17:30:17 -05:00
|
|
|
ignore = next(st) as char;
|
2011-07-27 07:19:39 -05:00
|
|
|
} while ignore == ';'
|
|
|
|
assert (ignore == ')');
|
|
|
|
ret @respan(sp, {path: pth, args: args, id: def});
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn parse_ty_rust_fn(st: @pstate, conv: conv_did, p: ast::proto) -> ty::t {
|
|
|
|
ret ty::mk_fn(st.tcx, {proto: p with parse_ty_fn(st, conv)});
|
2011-12-09 18:56:48 -06:00
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn parse_ty(st: @pstate, conv: conv_did) -> ty::t {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt next(st) as char {
|
|
|
|
'n' { ret ty::mk_nil(st.tcx); }
|
|
|
|
'z' { ret ty::mk_bot(st.tcx); }
|
|
|
|
'b' { ret ty::mk_bool(st.tcx); }
|
|
|
|
'i' { ret ty::mk_int(st.tcx); }
|
|
|
|
'u' { ret ty::mk_uint(st.tcx); }
|
|
|
|
'l' { ret ty::mk_float(st.tcx); }
|
|
|
|
'M' {
|
|
|
|
alt next(st) as char {
|
2011-12-07 14:06:12 -06:00
|
|
|
'b' { ret ty::mk_mach_uint(st.tcx, ast::ty_u8); }
|
|
|
|
'w' { ret ty::mk_mach_uint(st.tcx, ast::ty_u16); }
|
|
|
|
'l' { ret ty::mk_mach_uint(st.tcx, ast::ty_u32); }
|
|
|
|
'd' { ret ty::mk_mach_uint(st.tcx, ast::ty_u64); }
|
|
|
|
'B' { ret ty::mk_mach_int(st.tcx, ast::ty_i8); }
|
|
|
|
'W' { ret ty::mk_mach_int(st.tcx, ast::ty_i16); }
|
|
|
|
'L' { ret ty::mk_mach_int(st.tcx, ast::ty_i32); }
|
|
|
|
'D' { ret ty::mk_mach_int(st.tcx, ast::ty_i64); }
|
|
|
|
'f' { ret ty::mk_mach_float(st.tcx, ast::ty_f32); }
|
|
|
|
'F' { ret 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
|
|
|
}
|
|
|
|
'c' { ret ty::mk_char(st.tcx); }
|
2011-09-02 18:52:14 -05:00
|
|
|
'S' { ret ty::mk_str(st.tcx); }
|
2011-07-27 07:19:39 -05:00
|
|
|
't' {
|
|
|
|
assert (next(st) as char == '[');
|
2012-01-05 09:04:59 -06:00
|
|
|
let def = parse_def(st, conv);
|
2011-08-19 17:16:48 -05:00
|
|
|
let params: [ty::t] = [];
|
2012-01-05 09:04:59 -06:00
|
|
|
while peek(st) as char != ']' { params += [parse_ty(st, conv)]; }
|
2011-07-27 07:19:39 -05:00
|
|
|
st.pos = st.pos + 1u;
|
|
|
|
ret ty::mk_tag(st.tcx, def, params);
|
|
|
|
}
|
2011-12-29 04:23:35 -06:00
|
|
|
'x' {
|
|
|
|
assert (next(st) as char == '[');
|
2012-01-05 09:04:59 -06:00
|
|
|
let def = parse_def(st, conv);
|
2011-12-29 04:23:35 -06:00
|
|
|
let params: [ty::t] = [];
|
2012-01-05 09:04:59 -06:00
|
|
|
while peek(st) as char != ']' { params += [parse_ty(st, conv)]; }
|
2011-12-29 04:23:35 -06:00
|
|
|
st.pos = st.pos + 1u;
|
|
|
|
ret ty::mk_iface(st.tcx, def, params);
|
|
|
|
}
|
2011-07-28 15:29:29 -05:00
|
|
|
'p' {
|
2012-01-05 09:04:59 -06:00
|
|
|
let did = parse_def(st, conv);
|
2011-12-29 04:23:35 -06:00
|
|
|
ret ty::mk_param(st.tcx, parse_int(st) as uint, did);
|
2011-07-28 15:29:29 -05:00
|
|
|
}
|
2012-01-05 09:04:59 -06:00
|
|
|
'@' { ret ty::mk_box(st.tcx, parse_mt(st, conv)); }
|
|
|
|
'~' { ret ty::mk_uniq(st.tcx, parse_mt(st, conv)); }
|
|
|
|
'*' { ret ty::mk_ptr(st.tcx, parse_mt(st, conv)); }
|
|
|
|
'I' { ret ty::mk_vec(st.tcx, parse_mt(st, conv)); }
|
2011-07-27 07:19:39 -05:00
|
|
|
'R' {
|
|
|
|
assert (next(st) as char == '[');
|
2011-08-19 17:16:48 -05:00
|
|
|
let fields: [ty::field] = [];
|
2011-07-27 07:19:39 -05:00
|
|
|
while peek(st) as char != ']' {
|
2011-09-02 17:34:58 -05:00
|
|
|
let name = "";
|
2011-07-27 07:19:39 -05:00
|
|
|
while peek(st) as char != '=' {
|
2011-09-01 19:27:58 -05:00
|
|
|
name += str::unsafe_from_byte(next(st));
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
st.pos = st.pos + 1u;
|
2012-01-05 09:04:59 -06:00
|
|
|
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;
|
|
|
|
ret ty::mk_rec(st.tcx, fields);
|
|
|
|
}
|
2011-08-15 04:40:26 -05:00
|
|
|
'T' {
|
|
|
|
assert (next(st) as char == '[');
|
2011-08-19 17:16:48 -05:00
|
|
|
let params = [];
|
2012-01-05 09:04:59 -06:00
|
|
|
while peek(st) as char != ']' { params += [parse_ty(st, conv)]; }
|
2011-08-15 04:40:26 -05:00
|
|
|
st.pos = st.pos + 1u;
|
|
|
|
ret ty::mk_tup(st.tcx, params);
|
|
|
|
}
|
2011-12-09 18:56:48 -06:00
|
|
|
's' {
|
2012-01-05 09:04:59 -06:00
|
|
|
ret parse_ty_rust_fn(st, conv, ast::proto_send);
|
2011-12-09 18:56:48 -06:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
'F' {
|
2012-01-05 09:04:59 -06:00
|
|
|
ret parse_ty_rust_fn(st, conv, ast::proto_shared(ast::sugar_normal));
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-10-11 16:38:15 -05:00
|
|
|
'f' {
|
2012-01-05 09:04:59 -06:00
|
|
|
ret parse_ty_rust_fn(st, conv, ast::proto_bare);
|
2011-10-11 16:38:15 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
'B' {
|
2012-01-05 09:04:59 -06:00
|
|
|
ret parse_ty_rust_fn(st, conv, ast::proto_block);
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
'N' {
|
2012-01-05 09:04:59 -06:00
|
|
|
let func = parse_ty_fn(st, conv);
|
2011-12-23 09:09:52 -06:00
|
|
|
ret ty::mk_native_fn(st.tcx, func.inputs, func.output);
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
'O' {
|
|
|
|
assert (next(st) as char == '[');
|
2011-08-19 17:16:48 -05:00
|
|
|
let methods: [ty::method] = [];
|
2011-07-27 07:19:39 -05:00
|
|
|
while peek(st) as char != ']' {
|
|
|
|
let proto;
|
|
|
|
alt next(st) as char {
|
2011-10-18 21:39:09 -05:00
|
|
|
'f' { proto = ast::proto_bare; }
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
2011-09-02 17:34:58 -05:00
|
|
|
let name = "";
|
2011-07-27 07:19:39 -05:00
|
|
|
while peek(st) as char != '[' {
|
2011-09-01 19:27:58 -05:00
|
|
|
name += str::unsafe_from_byte(next(st));
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
2012-01-02 05:09:26 -06:00
|
|
|
methods += [{ident: name, tps: @[],
|
2012-01-05 09:04:59 -06:00
|
|
|
fty: {proto: proto with parse_ty_fn(st, conv)}}];
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
st.pos += 1u;
|
|
|
|
ret ty::mk_obj(st.tcx, methods);
|
|
|
|
}
|
|
|
|
'r' {
|
|
|
|
assert (next(st) as char == '[');
|
2012-01-05 09:04:59 -06:00
|
|
|
let def = parse_def(st, conv);
|
|
|
|
let inner = parse_ty(st, conv);
|
2011-08-19 17:16:48 -05:00
|
|
|
let params: [ty::t] = [];
|
2012-01-05 09:04:59 -06:00
|
|
|
while peek(st) as char != ']' { params += [parse_ty(st, conv)]; }
|
2011-07-27 07:19:39 -05:00
|
|
|
st.pos = st.pos + 1u;
|
|
|
|
ret ty::mk_res(st.tcx, def, inner, params);
|
|
|
|
}
|
|
|
|
'X' { ret ty::mk_var(st.tcx, parse_int(st)); }
|
2012-01-05 09:04:59 -06:00
|
|
|
'E' { let def = parse_def(st, conv); ret ty::mk_native(st.tcx, def); }
|
2011-07-27 07:19:39 -05:00
|
|
|
'Y' { ret ty::mk_type(st.tcx); }
|
2011-12-15 16:30:19 -06:00
|
|
|
'y' { ret ty::mk_send_type(st.tcx); }
|
|
|
|
'C' { ret ty::mk_opaque_closure(st.tcx); }
|
2011-07-27 07:19:39 -05:00
|
|
|
'#' {
|
|
|
|
let pos = parse_hex(st);
|
|
|
|
assert (next(st) as char == ':');
|
|
|
|
let len = parse_hex(st);
|
|
|
|
assert (next(st) as char == '#');
|
|
|
|
alt st.tcx.rcache.find({cnum: st.crate, pos: pos, len: len}) {
|
|
|
|
some(tt) { ret tt; }
|
|
|
|
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);
|
|
|
|
ret tt;
|
|
|
|
}
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
'A' {
|
|
|
|
assert (next(st) as char == '[');
|
2012-01-05 09:04:59 -06:00
|
|
|
let tt = parse_ty(st, conv);
|
|
|
|
let tcs = parse_ty_constrs(st, conv);
|
2011-07-27 07:19:39 -05:00
|
|
|
assert (next(st) as char == ']');
|
|
|
|
ret ty::mk_constr(st.tcx, tt, tcs);
|
|
|
|
}
|
2011-12-22 16:42:52 -06: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 {
|
2011-07-27 07:19:39 -05:00
|
|
|
let mut;
|
|
|
|
alt peek(st) as char {
|
|
|
|
'm' { next(st); mut = ast::mut; }
|
|
|
|
'?' { next(st); mut = ast::maybe_mut; }
|
|
|
|
_ { mut = ast::imm; }
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
2012-01-05 09:04:59 -06:00
|
|
|
ret {ty: parse_ty(st, conv), mut: mut};
|
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 {
|
|
|
|
let def = [];
|
|
|
|
while peek(st) as char != '|' { def += [next(st)]; }
|
2011-06-27 17:30:17 -05:00
|
|
|
st.pos = st.pos + 1u;
|
2012-01-05 09:04:59 -06:00
|
|
|
ret 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 {
|
|
|
|
let n = 0;
|
|
|
|
while true {
|
|
|
|
let cur = peek(st) as char;
|
|
|
|
if cur < '0' || cur > '9' { break; }
|
2011-06-27 17:30:17 -05:00
|
|
|
st.pos = st.pos + 1u;
|
|
|
|
n *= 10;
|
|
|
|
n += (cur as int) - ('0' as int);
|
|
|
|
}
|
|
|
|
ret n;
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn parse_hex(st: @pstate) -> uint {
|
|
|
|
let n = 0u;
|
|
|
|
while true {
|
|
|
|
let cur = peek(st) as char;
|
|
|
|
if (cur < '0' || cur > '9') && (cur < 'a' || cur > 'f') { break; }
|
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); }
|
|
|
|
}
|
|
|
|
ret n;
|
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn parse_ty_fn(st: @pstate, conv: conv_did) -> ty::fn_ty {
|
2011-06-27 17:30:17 -05:00
|
|
|
assert (next(st) as char == '[');
|
2011-08-19 17:16:48 -05:00
|
|
|
let inputs: [ty::arg] = [];
|
2011-07-27 07:19:39 -05:00
|
|
|
while peek(st) as char != ']' {
|
2011-10-06 05:26:12 -05:00
|
|
|
let mode = alt peek(st) as char {
|
|
|
|
'&' { ast::by_mut_ref }
|
|
|
|
'-' { 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-01-05 09:04:59 -06:00
|
|
|
inputs += [{mode: 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 cs = parse_constrs(st, conv);
|
|
|
|
let (ret_style, ret_ty) = parse_ret_ty(st, conv);
|
2011-12-23 09:09:52 -06:00
|
|
|
ret {proto: ast::proto_bare, inputs: inputs, output: ret_ty,
|
|
|
|
ret_style: ret_style, constraints: cs};
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Rust metadata parsing
|
2011-09-12 04:27:30 -05:00
|
|
|
fn parse_def_id(buf: [u8]) -> ast::def_id {
|
2011-07-27 07:19:39 -05:00
|
|
|
let 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 {
|
2011-12-22 16:42:52 -06:00
|
|
|
#error("didn't find ':' when parsing def id");
|
2011-06-27 17:30:17 -05:00
|
|
|
fail;
|
|
|
|
}
|
2011-08-13 02:09:25 -05:00
|
|
|
let crate_part = vec::slice::<u8>(buf, 0u, colon_idx);
|
|
|
|
let def_part = vec::slice::<u8>(buf, colon_idx + 1u, len);
|
2011-07-12 12:59:18 -05:00
|
|
|
|
2011-08-19 17:16:48 -05:00
|
|
|
let crate_part_vec = [];
|
|
|
|
let def_part_vec = [];
|
|
|
|
for b: u8 in crate_part { crate_part_vec += [b]; }
|
|
|
|
for b: u8 in def_part { def_part_vec += [b]; }
|
2011-07-12 12:59:18 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let crate_num = uint::parse_buf(crate_part_vec, 10u) as int;
|
|
|
|
let def_num = uint::parse_buf(def_part_vec, 10u) as int;
|
|
|
|
ret {crate: crate_num, node: def_num};
|
2011-06-27 17:30:17 -05:00
|
|
|
}
|
2011-07-19 19:52:34 -05:00
|
|
|
|
2012-01-05 06:57:27 -06: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)
|
2011-12-28 10:50:12 -06:00
|
|
|
-> @[ty::param_bound] {
|
2012-01-05 06:57:27 -06:00
|
|
|
let st = @{data: data, crate: crate_num, mutable 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-01-05 09:04:59 -06:00
|
|
|
fn parse_bounds(st: @pstate, conv: conv_did) -> @[ty::param_bound] {
|
2011-12-28 10:50:12 -06:00
|
|
|
let bounds = [];
|
2012-01-05 06:57:27 -06:00
|
|
|
while true {
|
2011-12-28 10:50:12 -06:00
|
|
|
bounds += [alt next(st) as char {
|
|
|
|
'S' { ty::bound_send }
|
|
|
|
'C' { ty::bound_copy }
|
2012-01-05 09:04:59 -06:00
|
|
|
'I' { ty::bound_iface(parse_ty(st, conv)) }
|
2012-01-05 06:57:27 -06:00
|
|
|
'.' { break; }
|
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:
|
|
|
|
//
|