Clean up translating of def_ids in metadata reader

Reduces the amount of closure allocation, and makes the code cleaner.
This commit is contained in:
Marijn Haverbeke 2012-01-05 16:04:59 +01:00
parent 60ae1590af
commit cc929fc9f9
5 changed files with 149 additions and 188 deletions

View File

@ -260,7 +260,8 @@ fn resolve_crate(e: env, ident: ast::ident, metas: [@ast::meta_item],
// Now resolve the crates referenced by this crate
let cnum_map = resolve_crate_deps(e, cdata);
let cmeta = {name: ident, data: cdata, cnum_map: cnum_map};
let cmeta = @{name: ident, data: cdata,
cnum_map: cnum_map, cnum: cnum};
let cstore = e.sess.get_cstore();
cstore::set_crate_data(cstore, cnum, cmeta);

View File

@ -58,32 +58,27 @@ fn resolve_path(cstore: cstore::cstore, cnum: ast::crate_num,
fn get_tag_variants(tcx: ty::ctxt, def: ast::def_id) -> [ty::variant_info] {
let cstore = tcx.sess.get_cstore();
let cnum = def.crate;
let cdata = cstore::get_crate_data(cstore, cnum).data;
let resolver = bind translate_def_id(cstore, cnum, _);
ret decoder::get_tag_variants(cdata, def, tcx, resolver)
let cdata = cstore::get_crate_data(cstore, def.crate);
ret decoder::get_tag_variants(cdata, def.node, tcx)
}
fn get_impls_for_mod(cstore: cstore::cstore, def: ast::def_id,
name: option::t<ast::ident>)
-> @[@middle::resolve::_impl] {
let cdata = cstore::get_crate_data(cstore, def.crate).data;
let resolver = bind translate_def_id(cstore, def.crate, _);
decoder::get_impls_for_mod(cdata, def, name, resolver)
let cdata = cstore::get_crate_data(cstore, def.crate);
decoder::get_impls_for_mod(cdata, def.node, name)
}
fn get_iface_methods(tcx: ty::ctxt, def: ast::def_id) -> @[ty::method] {
let cstore = tcx.sess.get_cstore();
let cdata = cstore::get_crate_data(cstore, def.crate).data;
let resolver = bind translate_def_id(cstore, def.crate, _);
decoder::get_iface_methods(cdata, def, tcx, resolver)
let cdata = cstore::get_crate_data(cstore, def.crate);
decoder::get_iface_methods(cdata, def.node, tcx)
}
fn get_type(tcx: ty::ctxt, def: ast::def_id) -> ty::ty_param_bounds_and_ty {
let cstore = tcx.sess.get_cstore();
let cdata = cstore::get_crate_data(cstore, def.crate).data;
let resolver = bind translate_def_id(cstore, def.crate, _);
decoder::get_type(cdata, def, tcx, resolver)
let cdata = cstore::get_crate_data(cstore, def.crate);
decoder::get_type(cdata, def.node, tcx)
}
fn get_item_name(cstore: cstore::cstore, cnum: int, id: int) -> ast::ident {
@ -94,34 +89,8 @@ fn get_item_name(cstore: cstore::cstore, cnum: int, id: int) -> ast::ident {
fn get_impl_iface(tcx: ty::ctxt, def: ast::def_id)
-> option::t<ty::t> {
let cstore = tcx.sess.get_cstore();
let cdata = cstore::get_crate_data(cstore, def.crate).data;
let resolver = bind translate_def_id(cstore, def.crate, _);
decoder::get_impl_iface(cdata, def, tcx, resolver)
}
// Translates a def_id from an external crate to a def_id for the current
// compilation environment. We use this when trying to load types from
// external crates - if those types further refer to types in other crates
// then we must translate the crate number from that encoded in the external
// crate to the correct local crate number.
fn translate_def_id(cstore: cstore::cstore, searched_crate: ast::crate_num,
def_id: ast::def_id) -> ast::def_id {
let ext_cnum = def_id.crate;
let node_id = def_id.node;
assert (searched_crate != ast::local_crate);
assert (ext_cnum != ast::local_crate);
let cmeta = cstore::get_crate_data(cstore, searched_crate);
let local_cnum =
alt cmeta.cnum_map.find(ext_cnum) {
option::some(n) { n }
option::none. { fail "didn't find a crate in the cnum_map"; }
};
ret {crate: local_cnum, node: node_id};
let cdata = cstore::get_crate_data(cstore, def.crate);
decoder::get_impl_iface(cdata, def.node, tcx)
}
// Local Variables:

View File

@ -30,7 +30,10 @@ export get_dep_hashes;
// own crate numbers.
type cnum_map = map::hashmap<ast::crate_num, ast::crate_num>;
type crate_metadata = {name: str, data: @[u8], cnum_map: cnum_map};
type crate_metadata = @{name: str,
data: @[u8],
cnum_map: cnum_map,
cnum: ast::crate_num};
// This is a bit of an experiment at encapsulating the data in cstore. By
// keeping all the data in a non-exported tag variant, it's impossible for

View File

@ -1,14 +1,13 @@
// Decoding metadata from a single crate's metadata
import core::{vec, option, str};
import std::{ebml, io};
import syntax::{ast, ast_util};
import front::attr;
import middle::ty;
import common::*;
import tydecode::{parse_def_id, parse_ty_data};
import tydecode::{parse_ty_data, parse_def_id, parse_bounds_data};
import syntax::print::pprust;
import cstore;
import cmd=cstore::crate_metadata;
export get_symbol;
export get_tag_variants;
@ -24,7 +23,6 @@ export list_crate_metadata;
export crate_dep;
export get_crate_deps;
export get_crate_hash;
export external_resolver;
export get_impls_for_mod;
export get_iface_methods;
// A function that takes a def_id relative to the crate being searched and
@ -32,7 +30,6 @@ export get_iface_methods;
// def_id for an item defined in another crate, somebody needs to figure out
// what crate that's in and give us a def_id that makes sense for the current
// build.
type external_resolver = fn@(ast::def_id) -> ast::def_id;
fn lookup_hash(d: ebml::doc, eq_fn: fn@([u8]) -> bool, hash: uint) ->
[ebml::doc] {
@ -91,66 +88,50 @@ fn variant_tag_id(d: ebml::doc) -> ast::def_id {
ret parse_def_id(ebml::doc_data(tagdoc));
}
fn parse_external_def_id(this_cnum: ast::crate_num,
extres: external_resolver, s: str) ->
ast::def_id {
let buf = str::bytes(s);
let external_def_id = parse_def_id(buf);
// This item was defined in the crate we're searching if it's has the
// local crate number, otherwise we need to search a different crate
if external_def_id.crate == ast::local_crate {
ret {crate: this_cnum, node: external_def_id.node};
} else { ret extres(external_def_id); }
}
fn doc_type(doc: ebml::doc, this_cnum: ast::crate_num, tcx: ty::ctxt,
extres: external_resolver) -> ty::t {
fn doc_type(doc: ebml::doc, tcx: ty::ctxt, cdata: cmd) -> ty::t {
let tp = ebml::get_doc(doc, tag_items_data_item_type);
let def_parser = bind parse_external_def_id(this_cnum, extres, _);
parse_ty_data(tp.data, this_cnum, tp.start, def_parser, tcx)
parse_ty_data(tp.data, cdata.cnum, tp.start, tcx, {|did|
translate_def_id(cdata, did)
})
}
fn item_type(item: ebml::doc, this_cnum: ast::crate_num, tcx: ty::ctxt,
extres: external_resolver) -> ty::t {
let t = doc_type(item, this_cnum, tcx, extres);
fn item_type(item: ebml::doc, tcx: ty::ctxt, cdata: cmd) -> ty::t {
let t = doc_type(item, tcx, cdata);
if family_names_type(item_family(item)) {
ty::mk_named(tcx, t, @item_name(item))
} else { t }
}
fn item_impl_iface(item: ebml::doc, this_cnum: ast::crate_num, tcx: ty::ctxt,
extres: external_resolver) -> option::t<ty::t> {
fn item_impl_iface(item: ebml::doc, tcx: ty::ctxt, cdata: cmd)
-> option::t<ty::t> {
let result = none;
ebml::tagged_docs(item, tag_impl_iface) {|ity|
let def_parser = bind parse_external_def_id(this_cnum, extres, _);
let t = parse_ty_data(ity.data, this_cnum, ity.start, def_parser,
tcx);
let t = parse_ty_data(ity.data, cdata.cnum, ity.start, tcx, {|did|
translate_def_id(cdata, did)
});
result = some(t);
}
result
}
fn item_impl_iface_did(item: ebml::doc, this_cnum: ast::crate_num,
extres: external_resolver)
fn item_impl_iface_did(item: ebml::doc, cdata: cmd)
-> option::t<ast::def_id> {
let result = none;
ebml::tagged_docs(item, tag_impl_iface_did) {|doc|
let s = str::unsafe_from_bytes(ebml::doc_data(doc));
result = some(parse_external_def_id(this_cnum, extres, s));
let did = translate_def_id(cdata, parse_def_id(ebml::doc_data(doc)));
result = some(did);
}
result
}
fn item_ty_param_bounds(item: ebml::doc, this_cnum: ast::crate_num,
tcx: ty::ctxt, extres: external_resolver)
fn item_ty_param_bounds(item: ebml::doc, tcx: ty::ctxt, cdata: cmd)
-> @[ty::param_bounds] {
let bounds = [];
let def_parser = bind parse_external_def_id(this_cnum, extres, _);
ebml::tagged_docs(item, tag_items_data_item_ty_param_bounds) {|p|
bounds += [tydecode::parse_bounds_data(p.data, p.start,
this_cnum, def_parser, tcx)];
let bd = parse_bounds_data(p.data, p.start, cdata.cnum, tcx, {|did|
translate_def_id(cdata, did)
});
bounds += [bd];
}
@bounds
}
@ -162,13 +143,12 @@ fn item_ty_param_count(item: ebml::doc) -> uint {
n
}
fn tag_variant_ids(item: ebml::doc, this_cnum: ast::crate_num) ->
[ast::def_id] {
fn tag_variant_ids(item: ebml::doc, cdata: cmd) -> [ast::def_id] {
let ids: [ast::def_id] = [];
let v = tag_items_data_item_variant;
ebml::tagged_docs(item, v) {|p|
let ext = parse_def_id(ebml::doc_data(p));
ids += [{crate: this_cnum, node: ext.node}];
ids += [{crate: cdata.cnum, node: ext.node}];
};
ret ids;
}
@ -230,12 +210,12 @@ fn lookup_def(cnum: ast::crate_num, data: @[u8], did_: ast::def_id) ->
ret def;
}
fn get_type(data: @[u8], def: ast::def_id, tcx: ty::ctxt,
extres: external_resolver) -> ty::ty_param_bounds_and_ty {
let item = lookup_item(def.node, data);
let t = item_type(item, def.crate, tcx, extres);
fn get_type(cdata: cmd, id: ast::node_id, tcx: ty::ctxt)
-> ty::ty_param_bounds_and_ty {
let item = lookup_item(id, cdata.data);
let t = item_type(item, tcx, cdata);
let tp_bounds = if family_has_type_params(item_family(item)) {
item_ty_param_bounds(item, def.crate, tcx, extres)
item_ty_param_bounds(item, tcx, cdata)
} else { @[] };
ret {bounds: tp_bounds, ty: t};
}
@ -244,27 +224,25 @@ fn get_type_param_count(data: @[u8], id: ast::node_id) -> uint {
item_ty_param_count(lookup_item(id, data))
}
fn get_impl_iface(data: @[u8], def: ast::def_id, tcx: ty::ctxt,
extres: external_resolver) -> option::t<ty::t> {
item_impl_iface(lookup_item(def.node, data), def.crate, tcx, extres)
fn get_impl_iface(cdata: cmd, id: ast::node_id, tcx: ty::ctxt)
-> option::t<ty::t> {
item_impl_iface(lookup_item(id, cdata.data), tcx, cdata)
}
fn get_symbol(data: @[u8], id: ast::node_id) -> str {
ret item_symbol(lookup_item(id, data));
}
fn get_tag_variants(_data: @[u8], def: ast::def_id, tcx: ty::ctxt,
extres: external_resolver) -> [ty::variant_info] {
let external_crate_id = def.crate;
let data =
cstore::get_crate_data(tcx.sess.get_cstore(), external_crate_id).data;
fn get_tag_variants(cdata: cmd, id: ast::node_id, tcx: ty::ctxt)
-> [ty::variant_info] {
let data = cdata.data;
let items = ebml::get_doc(ebml::new_doc(data), tag_items);
let item = find_item(def.node, items);
let item = find_item(id, items);
let infos: [ty::variant_info] = [];
let variant_ids = tag_variant_ids(item, external_crate_id);
let variant_ids = tag_variant_ids(item, cdata);
for did: ast::def_id in variant_ids {
let item = find_item(did.node, items);
let ctor_ty = item_type(item, external_crate_id, tcx, extres);
let ctor_ty = item_type(item, tcx, cdata);
let arg_tys: [ty::t] = [];
alt ty::struct(tcx, ctor_ty) {
ty::ty_fn(f) {
@ -290,17 +268,17 @@ fn item_impl_methods(data: @[u8], item: ebml::doc, base_tps: uint)
rslt
}
fn get_impls_for_mod(data: @[u8], m_def: ast::def_id,
name: option::t<ast::ident>, extres: external_resolver)
fn get_impls_for_mod(cdata: cmd, m_id: ast::node_id,
name: option::t<ast::ident>)
-> @[@middle::resolve::_impl] {
let mod_item = lookup_item(m_def.node, data), result = [];
let data = cdata.data;
let mod_item = lookup_item(m_id, data), result = [];
ebml::tagged_docs(mod_item, tag_mod_impl) {|doc|
let did = parse_external_def_id(
m_def.crate, extres, str::unsafe_from_bytes(ebml::doc_data(doc)));
let did = translate_def_id(cdata, parse_def_id(ebml::doc_data(doc)));
let item = lookup_item(did.node, data), nm = item_name(item);
if alt name { some(n) { n == nm } none. { true } } {
let base_tps = item_ty_param_count(doc);
let i_did = item_impl_iface_did(item, m_def.crate, extres);
let i_did = item_impl_iface_did(item, cdata);
result += [@{did: did, iface_did: i_did, ident: nm,
methods: item_impl_methods(data, doc, base_tps)}];
}
@ -308,13 +286,14 @@ fn get_impls_for_mod(data: @[u8], m_def: ast::def_id,
@result
}
fn get_iface_methods(data: @[u8], def: ast::def_id, tcx: ty::ctxt,
extres: external_resolver) -> @[ty::method] {
let item = lookup_item(def.node, data), result = [];
fn get_iface_methods(cdata: cmd, id: ast::node_id, tcx: ty::ctxt)
-> @[ty::method] {
let data = cdata.data;
let item = lookup_item(id, data), result = [];
ebml::tagged_docs(item, tag_item_method) {|mth|
let bounds = item_ty_param_bounds(mth, def.crate, tcx, extres);
let bounds = item_ty_param_bounds(mth, tcx, cdata);
let name = item_name(mth);
let ty = doc_type(mth, def.crate, tcx, extres);
let ty = doc_type(mth, tcx, cdata);
let fty = alt ty::struct(tcx, ty) { ty::ty_fn(f) { f } };
result += [{ident: name, tps: bounds, fty: fty}];
}
@ -489,6 +468,22 @@ fn list_crate_metadata(bytes: @[u8], out: io::writer) {
list_crate_items(bytes, md, out);
}
// Translates a def_id from an external crate to a def_id for the current
// compilation environment. We use this when trying to load types from
// external crates - if those types further refer to types in other crates
// then we must translate the crate number from that encoded in the external
// crate to the correct local crate number.
fn translate_def_id(cdata: cmd, did: ast::def_id) -> ast::def_id {
if did.crate == ast::local_crate {
ret {crate: cdata.cnum, node: did.node};
}
alt cdata.cnum_map.find(did.crate) {
option::some(n) { ret {crate: n, node: did.node}; }
option::none. { fail "didn't find a crate in the cnum_map"; }
}
}
// Local Variables:
// mode: rust
// fill-column: 78;

View File

@ -8,8 +8,7 @@ import syntax::ast_util;
import syntax::ast_util::respan;
import middle::ty;
export parse_def_id;
export parse_ty_data;
export parse_ty_data, parse_def_id;
export parse_bounds_data;
// Compact string representation for ty::t values. API ty_str &
@ -17,7 +16,7 @@ export parse_bounds_data;
// data buffer. Whatever format you choose should not contain pipe characters.
// Callback to translate defs to strs or back:
type str_def = fn@(str) -> ast::def_id;
type conv_did = block(ast::def_id) -> ast::def_id;
type pstate = {data: @[u8], crate: int, mutable pos: uint, tcx: ty::ctxt};
@ -31,12 +30,12 @@ fn next(st: @pstate) -> u8 {
ret ch;
}
fn parse_ident(st: @pstate, sd: str_def, last: char) -> ast::ident {
fn parse_ident(st: @pstate, last: char) -> ast::ident {
fn is_last(b: char, c: char) -> bool { ret c == b; }
ret parse_ident_(st, sd, bind is_last(last, _));
ret parse_ident_(st, bind is_last(last, _));
}
fn parse_ident_(st: @pstate, _sd: str_def, is_last: fn@(char) -> bool) ->
fn parse_ident_(st: @pstate, is_last: fn@(char) -> bool) ->
ast::ident {
let rslt = "";
while !is_last(peek(st) as char) {
@ -46,28 +45,26 @@ fn parse_ident_(st: @pstate, _sd: str_def, is_last: fn@(char) -> bool) ->
}
fn parse_ty_data(data: @[u8], crate_num: int, pos: uint, sd: str_def,
tcx: ty::ctxt) -> ty::t {
fn parse_ty_data(data: @[u8], crate_num: int, pos: uint, tcx: ty::ctxt,
conv: conv_did) -> ty::t {
let st = @{data: data, crate: crate_num, mutable pos: pos, tcx: tcx};
parse_ty(st, sd)
parse_ty(st, conv)
}
fn parse_ret_ty(st: @pstate, sd: str_def) -> (ast::ret_style, ty::t) {
fn parse_ret_ty(st: @pstate, conv: conv_did) -> (ast::ret_style, ty::t) {
alt peek(st) as char {
'!' { next(st); (ast::noreturn, ty::mk_bot(st.tcx)) }
_ { (ast::return_val, parse_ty(st, sd)) }
_ { (ast::return_val, parse_ty(st, conv)) }
}
}
fn parse_constrs(st: @pstate, sd: str_def) -> [@ty::constr] {
fn parse_constrs(st: @pstate, conv: conv_did) -> [@ty::constr] {
let rslt: [@ty::constr] = [];
alt peek(st) as char {
':' {
do {
next(st);
let one: @ty::constr =
parse_constr::<uint>(st, sd, parse_constr_arg);
rslt += [one];
rslt += [parse_constr(st, conv, parse_constr_arg)];
} while peek(st) as char == ';'
}
_ { }
@ -76,15 +73,13 @@ fn parse_constrs(st: @pstate, sd: str_def) -> [@ty::constr] {
}
// FIXME less copy-and-paste
fn parse_ty_constrs(st: @pstate, sd: str_def) -> [@ty::type_constr] {
fn parse_ty_constrs(st: @pstate, conv: conv_did) -> [@ty::type_constr] {
let rslt: [@ty::type_constr] = [];
alt peek(st) as char {
':' {
do {
next(st);
let one: @ty::type_constr =
parse_constr::<@path>(st, sd, parse_ty_constr_arg);
rslt += [one];
rslt += [parse_constr(st, conv, parse_ty_constr_arg)];
} while peek(st) as char == ';'
}
_ { }
@ -92,10 +87,10 @@ fn parse_ty_constrs(st: @pstate, sd: str_def) -> [@ty::type_constr] {
ret rslt;
}
fn parse_path(st: @pstate, sd: str_def) -> @ast::path {
fn parse_path(st: @pstate) -> @ast::path {
let idents: [ast::ident] = [];
fn is_last(c: char) -> bool { ret c == '(' || c == ':'; }
idents += [parse_ident_(st, sd, is_last)];
idents += [parse_ident_(st, is_last)];
while true {
alt peek(st) as char {
':' { next(st); next(st); }
@ -103,16 +98,14 @@ fn parse_path(st: @pstate, sd: str_def) -> @ast::path {
if c == '(' {
ret @respan(ast_util::dummy_sp(),
{global: false, idents: idents, types: []});
} else { idents += [parse_ident_(st, sd, is_last)]; }
} else { idents += [parse_ident_(st, is_last)]; }
}
}
}
fail "parse_path: ill-formed path";
}
type arg_parser<T> = fn(@pstate, str_def) -> ast::constr_arg_general_<T>;
fn parse_constr_arg(st: @pstate, _sd: str_def) -> ast::fn_constr_arg {
fn parse_constr_arg(st: @pstate) -> ast::fn_constr_arg {
alt peek(st) as char {
'*' { st.pos += 1u; ret ast::carg_base; }
c {
@ -129,7 +122,7 @@ fn parse_constr_arg(st: @pstate, _sd: str_def) -> ast::fn_constr_arg {
}
/*
else {
auto lit = parse_lit(st, sd, ',');
auto lit = parse_lit(st, conv, ',');
args += [respan(st.span, ast::carg_lit(lit))];
}
*/
@ -137,25 +130,25 @@ fn parse_constr_arg(st: @pstate, _sd: str_def) -> ast::fn_constr_arg {
}
}
fn parse_ty_constr_arg(st: @pstate, sd: str_def) ->
ast::constr_arg_general_<@path> {
fn parse_ty_constr_arg(st: @pstate) -> ast::constr_arg_general_<@path> {
alt peek(st) as char {
'*' { st.pos += 1u; ret ast::carg_base; }
c { ret ast::carg_ident(parse_path(st, sd)); }
c { ret ast::carg_ident(parse_path(st)); }
}
}
fn parse_constr<T: copy>(st: @pstate, sd: str_def, pser: arg_parser<T>) ->
@ty::constr_general<T> {
fn parse_constr<T: copy>(st: @pstate, conv: conv_did,
pser: block(@pstate) -> ast::constr_arg_general_<T>)
-> @ty::constr_general<T> {
let sp = ast_util::dummy_sp(); // FIXME: use a real span
let args: [@sp_constr_arg<T>] = [];
let pth = parse_path(st, sd);
let pth = parse_path(st);
let ignore: char = next(st) as char;
assert (ignore == '(');
let def = parse_def(st, sd);
let def = parse_def(st, conv);
let an_arg: constr_arg_general_<T>;
do {
an_arg = pser(st, sd);
an_arg = pser(st);
// FIXME use a real span
args += [@respan(sp, an_arg)];
ignore = next(st) as char;
@ -164,11 +157,11 @@ fn parse_constr<T: copy>(st: @pstate, sd: str_def, pser: arg_parser<T>) ->
ret @respan(sp, {path: pth, args: args, id: def});
}
fn parse_ty_rust_fn(st: @pstate, sd: str_def, p: ast::proto) -> ty::t {
ret ty::mk_fn(st.tcx, {proto: p with parse_ty_fn(st, sd)});
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)});
}
fn parse_ty(st: @pstate, sd: str_def) -> ty::t {
fn parse_ty(st: @pstate, conv: conv_did) -> ty::t {
alt next(st) as char {
'n' { ret ty::mk_nil(st.tcx); }
'z' { ret ty::mk_bot(st.tcx); }
@ -194,28 +187,28 @@ fn parse_ty(st: @pstate, sd: str_def) -> ty::t {
'S' { ret ty::mk_str(st.tcx); }
't' {
assert (next(st) as char == '[');
let def = parse_def(st, sd);
let def = parse_def(st, conv);
let params: [ty::t] = [];
while peek(st) as char != ']' { params += [parse_ty(st, sd)]; }
while peek(st) as char != ']' { params += [parse_ty(st, conv)]; }
st.pos = st.pos + 1u;
ret ty::mk_tag(st.tcx, def, params);
}
'x' {
assert (next(st) as char == '[');
let def = parse_def(st, sd);
let def = parse_def(st, conv);
let params: [ty::t] = [];
while peek(st) as char != ']' { params += [parse_ty(st, sd)]; }
while peek(st) as char != ']' { params += [parse_ty(st, conv)]; }
st.pos = st.pos + 1u;
ret ty::mk_iface(st.tcx, def, params);
}
'p' {
let did = parse_def(st, sd);
let did = parse_def(st, conv);
ret ty::mk_param(st.tcx, parse_int(st) as uint, did);
}
'@' { ret ty::mk_box(st.tcx, parse_mt(st, sd)); }
'~' { ret ty::mk_uniq(st.tcx, parse_mt(st, sd)); }
'*' { ret ty::mk_ptr(st.tcx, parse_mt(st, sd)); }
'I' { ret ty::mk_vec(st.tcx, parse_mt(st, sd)); }
'@' { 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)); }
'R' {
assert (next(st) as char == '[');
let fields: [ty::field] = [];
@ -225,7 +218,7 @@ fn parse_ty(st: @pstate, sd: str_def) -> ty::t {
name += str::unsafe_from_byte(next(st));
}
st.pos = st.pos + 1u;
fields += [{ident: name, mt: parse_mt(st, sd)}];
fields += [{ident: name, mt: parse_mt(st, conv)}];
}
st.pos = st.pos + 1u;
ret ty::mk_rec(st.tcx, fields);
@ -233,24 +226,24 @@ fn parse_ty(st: @pstate, sd: str_def) -> ty::t {
'T' {
assert (next(st) as char == '[');
let params = [];
while peek(st) as char != ']' { params += [parse_ty(st, sd)]; }
while peek(st) as char != ']' { params += [parse_ty(st, conv)]; }
st.pos = st.pos + 1u;
ret ty::mk_tup(st.tcx, params);
}
's' {
ret parse_ty_rust_fn(st, sd, ast::proto_send);
ret parse_ty_rust_fn(st, conv, ast::proto_send);
}
'F' {
ret parse_ty_rust_fn(st, sd, ast::proto_shared(ast::sugar_normal));
ret parse_ty_rust_fn(st, conv, ast::proto_shared(ast::sugar_normal));
}
'f' {
ret parse_ty_rust_fn(st, sd, ast::proto_bare);
ret parse_ty_rust_fn(st, conv, ast::proto_bare);
}
'B' {
ret parse_ty_rust_fn(st, sd, ast::proto_block);
ret parse_ty_rust_fn(st, conv, ast::proto_block);
}
'N' {
let func = parse_ty_fn(st, sd);
let func = parse_ty_fn(st, conv);
ret ty::mk_native_fn(st.tcx, func.inputs, func.output);
}
'O' {
@ -266,22 +259,22 @@ fn parse_ty(st: @pstate, sd: str_def) -> ty::t {
name += str::unsafe_from_byte(next(st));
}
methods += [{ident: name, tps: @[],
fty: {proto: proto with parse_ty_fn(st, sd)}}];
fty: {proto: proto with parse_ty_fn(st, conv)}}];
}
st.pos += 1u;
ret ty::mk_obj(st.tcx, methods);
}
'r' {
assert (next(st) as char == '[');
let def = parse_def(st, sd);
let inner = parse_ty(st, sd);
let def = parse_def(st, conv);
let inner = parse_ty(st, conv);
let params: [ty::t] = [];
while peek(st) as char != ']' { params += [parse_ty(st, sd)]; }
while peek(st) as char != ']' { params += [parse_ty(st, conv)]; }
st.pos = st.pos + 1u;
ret ty::mk_res(st.tcx, def, inner, params);
}
'X' { ret ty::mk_var(st.tcx, parse_int(st)); }
'E' { let def = parse_def(st, sd); ret ty::mk_native(st.tcx, def); }
'E' { let def = parse_def(st, conv); ret ty::mk_native(st.tcx, def); }
'Y' { ret ty::mk_type(st.tcx); }
'y' { ret ty::mk_send_type(st.tcx); }
'C' { ret ty::mk_opaque_closure(st.tcx); }
@ -294,7 +287,7 @@ fn parse_ty(st: @pstate, sd: str_def) -> ty::t {
some(tt) { ret tt; }
none. {
let ps = @{pos: pos with *st};
let tt = parse_ty(ps, sd);
let tt = parse_ty(ps, conv);
st.tcx.rcache.insert({cnum: st.crate, pos: pos, len: len}, tt);
ret tt;
}
@ -302,8 +295,8 @@ fn parse_ty(st: @pstate, sd: str_def) -> ty::t {
}
'A' {
assert (next(st) as char == '[');
let tt = parse_ty(st, sd);
let tcs = parse_ty_constrs(st, sd);
let tt = parse_ty(st, conv);
let tcs = parse_ty_constrs(st, conv);
assert (next(st) as char == ']');
ret ty::mk_constr(st.tcx, tt, tcs);
}
@ -311,21 +304,21 @@ fn parse_ty(st: @pstate, sd: str_def) -> ty::t {
}
}
fn parse_mt(st: @pstate, sd: str_def) -> ty::mt {
fn parse_mt(st: @pstate, conv: conv_did) -> ty::mt {
let mut;
alt peek(st) as char {
'm' { next(st); mut = ast::mut; }
'?' { next(st); mut = ast::maybe_mut; }
_ { mut = ast::imm; }
}
ret {ty: parse_ty(st, sd), mut: mut};
ret {ty: parse_ty(st, conv), mut: mut};
}
fn parse_def(st: @pstate, sd: str_def) -> ast::def_id {
let def = "";
while peek(st) as char != '|' { def += str::unsafe_from_byte(next(st)); }
fn parse_def(st: @pstate, conv: conv_did) -> ast::def_id {
let def = [];
while peek(st) as char != '|' { def += [next(st)]; }
st.pos = st.pos + 1u;
ret sd(def);
ret conv(parse_def_id(def));
}
fn parse_int(st: @pstate) -> int {
@ -354,7 +347,7 @@ fn parse_hex(st: @pstate) -> uint {
ret n;
}
fn parse_ty_fn(st: @pstate, sd: str_def) -> ty::fn_ty {
fn parse_ty_fn(st: @pstate, conv: conv_did) -> ty::fn_ty {
assert (next(st) as char == '[');
let inputs: [ty::arg] = [];
while peek(st) as char != ']' {
@ -366,11 +359,11 @@ fn parse_ty_fn(st: @pstate, sd: str_def) -> ty::fn_ty {
'#' { ast::by_val }
};
st.pos += 1u;
inputs += [{mode: mode, ty: parse_ty(st, sd)}];
inputs += [{mode: mode, ty: parse_ty(st, conv)}];
}
st.pos += 1u; // eat the ']'
let cs = parse_constrs(st, sd);
let (ret_style, ret_ty) = parse_ret_ty(st, sd);
let cs = parse_constrs(st, conv);
let (ret_style, ret_ty) = parse_ret_ty(st, conv);
ret {proto: ast::proto_bare, inputs: inputs, output: ret_ty,
ret_style: ret_style, constraints: cs};
}
@ -399,19 +392,19 @@ fn parse_def_id(buf: [u8]) -> ast::def_id {
}
fn parse_bounds_data(data: @[u8], start: uint,
crate_num: int, sd: str_def, tcx: ty::ctxt)
crate_num: int, tcx: ty::ctxt, conv: conv_did)
-> @[ty::param_bound] {
let st = @{data: data, crate: crate_num, mutable pos: start, tcx: tcx};
parse_bounds(st, sd)
parse_bounds(st, conv)
}
fn parse_bounds(st: @pstate, sd: str_def) -> @[ty::param_bound] {
fn parse_bounds(st: @pstate, conv: conv_did) -> @[ty::param_bound] {
let bounds = [];
while true {
bounds += [alt next(st) as char {
'S' { ty::bound_send }
'C' { ty::bound_copy }
'I' { ty::bound_iface(parse_ty(st, sd)) }
'I' { ty::bound_iface(parse_ty(st, conv)) }
'.' { break; }
}];
}