2011-03-15 18:30:43 -05:00
|
|
|
// -*- rust -*-
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
import driver::session;
|
|
|
|
import front::ast;
|
|
|
|
import lib::llvm::False;
|
|
|
|
import lib::llvm::llvm;
|
|
|
|
import lib::llvm::mk_object_file;
|
|
|
|
import lib::llvm::mk_section_iter;
|
2011-05-26 19:16:54 -05:00
|
|
|
import middle::resolve;
|
|
|
|
import middle::walk;
|
2011-05-12 10:24:54 -05:00
|
|
|
import middle::metadata;
|
|
|
|
import middle::trans;
|
|
|
|
import middle::ty;
|
|
|
|
import back::x86;
|
|
|
|
import util::common;
|
|
|
|
import util::common::span;
|
2011-06-09 11:48:16 -05:00
|
|
|
import util::common::respan;
|
2011-05-20 16:15:39 -05:00
|
|
|
import util::common::a_bang;
|
|
|
|
import util::common::a_ty;
|
2011-06-09 11:48:16 -05:00
|
|
|
import util::common::may_begin_ident;
|
2011-05-12 10:24:54 -05:00
|
|
|
|
2011-05-17 13:41:41 -05:00
|
|
|
import std::str;
|
|
|
|
import std::uint;
|
|
|
|
import std::vec;
|
2011-05-12 10:24:54 -05:00
|
|
|
import std::ebml;
|
|
|
|
import std::fs;
|
|
|
|
import std::io;
|
|
|
|
import std::option;
|
|
|
|
import std::option::none;
|
|
|
|
import std::option::some;
|
|
|
|
import std::os;
|
|
|
|
import std::map::hashmap;
|
2011-03-15 18:30:43 -05:00
|
|
|
|
2011-03-15 19:33:05 -05:00
|
|
|
|
2011-03-21 13:35:04 -05:00
|
|
|
// Type decoding
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
// Compact string representation for ty::t values. API ty_str & parse_from_str
|
2011-03-21 13:35:04 -05:00
|
|
|
// (The second has to be authed pure.) Extra parameters are for converting
|
2011-04-29 10:26:28 -05:00
|
|
|
// to/from def_ids in the data buffer. Whatever format you choose should not
|
2011-03-21 13:35:04 -05:00
|
|
|
// contain pipe characters.
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
// Callback to translate defs to strs or back:
|
|
|
|
type str_def = fn(str) -> ast::def_id;
|
2011-03-21 13:35:04 -05:00
|
|
|
|
2011-04-29 10:26:28 -05:00
|
|
|
type pstate = rec(vec[u8] data, int crate,
|
2011-05-12 10:24:54 -05:00
|
|
|
mutable uint pos, uint len, ty::ctxt tcx);
|
2011-03-21 13:35:04 -05:00
|
|
|
|
2011-05-20 16:15:39 -05:00
|
|
|
type ty_or_bang = util::common::ty_or_bang[ty::t];
|
|
|
|
|
2011-03-21 14:03:34 -05:00
|
|
|
fn peek(@pstate st) -> u8 {
|
2011-04-29 10:26:28 -05:00
|
|
|
ret st.data.(st.pos);
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
2011-04-20 20:52:04 -05:00
|
|
|
fn next(@pstate st) -> u8 {
|
2011-04-29 10:26:28 -05:00
|
|
|
auto ch = st.data.(st.pos);
|
2011-03-21 13:35:04 -05:00
|
|
|
st.pos = st.pos + 1u;
|
2011-04-29 10:26:28 -05:00
|
|
|
ret ch;
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
|
2011-06-09 11:48:16 -05:00
|
|
|
fn parse_ident(@pstate st, str_def sd, char last) -> ast::ident {
|
|
|
|
auto res = "";
|
|
|
|
while (peek(st) as char != last) {
|
|
|
|
res += str::unsafe_from_byte(next(st));
|
|
|
|
}
|
|
|
|
ret res;
|
|
|
|
}
|
|
|
|
|
2011-04-29 10:26:28 -05:00
|
|
|
fn parse_ty_data(vec[u8] data, int crate_num, uint pos, uint len,
|
2011-05-12 10:24:54 -05:00
|
|
|
str_def sd, ty::ctxt tcx) -> ty::t {
|
2011-04-29 10:26:28 -05:00
|
|
|
auto st = @rec(data=data, crate=crate_num,
|
|
|
|
mutable pos=pos, len=len, tcx=tcx);
|
2011-03-21 13:35:04 -05:00
|
|
|
auto result = parse_ty(st, sd);
|
|
|
|
ret result;
|
|
|
|
}
|
|
|
|
|
2011-05-20 16:15:39 -05:00
|
|
|
fn parse_ty_or_bang(@pstate st, str_def sd) -> ty_or_bang {
|
|
|
|
alt (peek(st) as char) {
|
|
|
|
case ('!') { auto ignore = next(st); ret a_bang[ty::t]; }
|
|
|
|
case (_) { ret a_ty[ty::t](parse_ty(st, sd)); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-09 11:48:16 -05:00
|
|
|
fn parse_constrs(@pstate st, str_def sd) -> vec[@ast::constr] {
|
|
|
|
let vec[@ast::constr] res = [];
|
|
|
|
alt (peek(st) as char) {
|
|
|
|
case (':') {
|
|
|
|
do {
|
|
|
|
auto ignore = next(st);
|
|
|
|
vec::push(res, parse_constr(st, sd));
|
|
|
|
} while (peek(st) as char == ',')
|
|
|
|
}
|
|
|
|
case (_) {}
|
|
|
|
}
|
|
|
|
ret res;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_constr(@pstate st, str_def sd) -> @ast::constr {
|
|
|
|
let vec[@ast::constr_arg] args = [];
|
|
|
|
auto sp = rec(lo=0u,hi=0u); // FIXME
|
|
|
|
let vec[ast::ident] ids = [];
|
|
|
|
let vec[@ast::ty] tys = [];
|
|
|
|
let ast::path pth = respan(sp,
|
|
|
|
rec(idents=ids, types=tys)); // FIXME
|
|
|
|
let ast::ident p1 = parse_ident(st, sd, '(');
|
|
|
|
log_err("ignore=");
|
|
|
|
log_err(p1);
|
|
|
|
let char ignore = next(st) as char;
|
|
|
|
assert(ignore as char == '(');
|
|
|
|
do {
|
|
|
|
alt (peek(st) as char) {
|
|
|
|
case ('*') {
|
|
|
|
auto ignore = next(st);
|
|
|
|
args += [@respan(sp, ast::carg_base)];
|
|
|
|
}
|
|
|
|
case (?c) {
|
|
|
|
log_err("c =");
|
|
|
|
log_err(str::from_bytes([c as u8]));
|
|
|
|
if (may_begin_ident(c)) {
|
|
|
|
auto id = parse_ident(st, sd, ',');
|
|
|
|
args += [@respan(sp, ast::carg_ident(id))];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_err("Lit args are unimplemented");
|
|
|
|
fail; // FIXME
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
else {
|
|
|
|
auto lit = parse_lit(st, sd, ',');
|
|
|
|
args += [respan(st.span, ast::carg_lit(lit))];
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (next(st) as char == ',');
|
|
|
|
ignore = next(st) as char;
|
|
|
|
ret @respan(sp, rec(path=pth, args=args));
|
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
fn parse_ty(@pstate st, str_def sd) -> ty::t {
|
2011-03-21 14:03:34 -05:00
|
|
|
alt (next(st) as char) {
|
2011-05-12 10:24:54 -05:00
|
|
|
case ('n') { ret ty::mk_nil(st.tcx); }
|
2011-05-20 16:15:39 -05:00
|
|
|
case ('z') { ret ty::mk_bot(st.tcx); }
|
2011-05-12 10:24:54 -05:00
|
|
|
case ('b') { ret ty::mk_bool(st.tcx); }
|
|
|
|
case ('i') { ret ty::mk_int(st.tcx); }
|
|
|
|
case ('u') { ret ty::mk_uint(st.tcx); }
|
|
|
|
case ('l') { ret ty::mk_float(st.tcx); }
|
2011-03-21 13:35:04 -05:00
|
|
|
case ('M') {
|
2011-03-21 14:03:34 -05:00
|
|
|
alt (next(st) as char) {
|
2011-05-12 10:24:54 -05:00
|
|
|
case ('b') { ret ty::mk_mach(st.tcx, common::ty_u8); }
|
|
|
|
case ('w') { ret ty::mk_mach(st.tcx, common::ty_u16); }
|
|
|
|
case ('l') { ret ty::mk_mach(st.tcx, common::ty_u32); }
|
|
|
|
case ('d') { ret ty::mk_mach(st.tcx, common::ty_u64); }
|
|
|
|
case ('B') { ret ty::mk_mach(st.tcx, common::ty_i8); }
|
|
|
|
case ('W') { ret ty::mk_mach(st.tcx, common::ty_i16); }
|
|
|
|
case ('L') { ret ty::mk_mach(st.tcx, common::ty_i32); }
|
|
|
|
case ('D') { ret ty::mk_mach(st.tcx, common::ty_i64); }
|
|
|
|
case ('f') { ret ty::mk_mach(st.tcx, common::ty_f32); }
|
|
|
|
case ('F') { ret ty::mk_mach(st.tcx, common::ty_f64); }
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
}
|
2011-05-12 10:24:54 -05:00
|
|
|
case ('c') { ret ty::mk_char(st.tcx); }
|
|
|
|
case ('s') { ret ty::mk_str(st.tcx); }
|
2011-06-09 20:00:11 -05:00
|
|
|
case ('S') { ret ty::mk_istr(st.tcx); }
|
2011-03-21 13:35:04 -05:00
|
|
|
case ('t') {
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (next(st) as char == '[');
|
2011-03-29 19:25:55 -05:00
|
|
|
auto def = parse_def(st, sd);
|
2011-05-16 20:21:22 -05:00
|
|
|
let vec[ty::t] params = [];
|
2011-03-21 14:03:34 -05:00
|
|
|
while (peek(st) as char != ']') {
|
2011-05-16 20:21:22 -05:00
|
|
|
params += [parse_ty(st, sd)];
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
st.pos = st.pos + 1u;
|
2011-05-12 10:24:54 -05:00
|
|
|
ret ty::mk_tag(st.tcx, def, params);
|
2011-03-24 13:37:50 -05:00
|
|
|
}
|
2011-05-12 10:24:54 -05:00
|
|
|
case ('p') { ret ty::mk_param(st.tcx, parse_int(st) as uint); }
|
|
|
|
case ('@') { ret ty::mk_box(st.tcx, parse_mt(st, sd)); }
|
2011-06-03 14:13:10 -05:00
|
|
|
case ('*') { ret ty::mk_ptr(st.tcx, parse_mt(st, sd)); }
|
2011-05-12 10:24:54 -05:00
|
|
|
case ('V') { ret ty::mk_vec(st.tcx, parse_mt(st, sd)); }
|
2011-06-09 20:00:11 -05:00
|
|
|
case ('I') { ret ty::mk_ivec(st.tcx, parse_mt(st, sd)); }
|
2011-05-31 18:27:39 -05:00
|
|
|
case ('a') { ret ty::mk_task(st.tcx); }
|
2011-05-12 10:24:54 -05:00
|
|
|
case ('P') { ret ty::mk_port(st.tcx, parse_ty(st, sd)); }
|
|
|
|
case ('C') { ret ty::mk_chan(st.tcx, parse_ty(st, sd)); }
|
2011-03-21 13:35:04 -05:00
|
|
|
case ('T') {
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (next(st) as char == '[');
|
2011-05-16 20:21:22 -05:00
|
|
|
let vec[ty::mt] params = [];
|
2011-03-21 14:03:34 -05:00
|
|
|
while (peek(st) as char != ']') {
|
2011-05-16 20:21:22 -05:00
|
|
|
params += [parse_mt(st, sd)];
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
st.pos = st.pos + 1u;
|
2011-05-12 10:24:54 -05:00
|
|
|
ret ty::mk_tup(st.tcx, params);
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
case ('R') {
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (next(st) as char == '[');
|
2011-05-16 20:21:22 -05:00
|
|
|
let vec[ty::field] fields = [];
|
2011-03-21 14:03:34 -05:00
|
|
|
while (peek(st) as char != ']') {
|
2011-03-21 13:35:04 -05:00
|
|
|
auto name = "";
|
2011-03-21 14:03:34 -05:00
|
|
|
while (peek(st) as char != '=') {
|
2011-05-17 13:41:41 -05:00
|
|
|
name += str::unsafe_from_byte(next(st));
|
2011-03-21 14:03:34 -05:00
|
|
|
}
|
2011-03-21 13:35:04 -05:00
|
|
|
st.pos = st.pos + 1u;
|
2011-05-16 20:21:22 -05:00
|
|
|
fields += [rec(ident=name, mt=parse_mt(st, sd))];
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
st.pos = st.pos + 1u;
|
2011-05-12 10:24:54 -05:00
|
|
|
ret ty::mk_rec(st.tcx, fields);
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
case ('F') {
|
|
|
|
auto func = parse_ty_fn(st, sd);
|
2011-06-09 11:48:16 -05:00
|
|
|
ret ty::mk_fn(st.tcx, ast::proto_fn, func._0,
|
|
|
|
func._1, func._2, func._3);
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
case ('W') {
|
|
|
|
auto func = parse_ty_fn(st, sd);
|
2011-06-09 11:48:16 -05:00
|
|
|
ret ty::mk_fn(st.tcx, ast::proto_iter, func._0,
|
|
|
|
func._1, func._2, func._3);
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
case ('N') {
|
|
|
|
auto abi;
|
2011-03-21 14:03:34 -05:00
|
|
|
alt (next(st) as char) {
|
2011-05-12 10:24:54 -05:00
|
|
|
case ('r') { abi = ast::native_abi_rust; }
|
|
|
|
case ('i') { abi = ast::native_abi_rust_intrinsic; }
|
|
|
|
case ('c') { abi = ast::native_abi_cdecl; }
|
|
|
|
case ('l') { abi = ast::native_abi_llvm; }
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
auto func = parse_ty_fn(st, sd);
|
2011-05-12 10:24:54 -05:00
|
|
|
ret ty::mk_native_fn(st.tcx,abi,func._0,func._1);
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
case ('O') {
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (next(st) as char == '[');
|
2011-05-16 20:21:22 -05:00
|
|
|
let vec[ty::method] methods = [];
|
2011-03-21 14:03:34 -05:00
|
|
|
while (peek(st) as char != ']') {
|
2011-03-21 13:35:04 -05:00
|
|
|
auto proto;
|
2011-03-21 14:03:34 -05:00
|
|
|
alt (next(st) as char) {
|
2011-05-12 10:24:54 -05:00
|
|
|
case ('W') {proto = ast::proto_iter;}
|
|
|
|
case ('F') {proto = ast::proto_fn;}
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
auto name = "";
|
2011-03-21 14:03:34 -05:00
|
|
|
while (peek(st) as char != '[') {
|
2011-05-17 13:41:41 -05:00
|
|
|
name += str::unsafe_from_byte(next(st));
|
2011-03-21 14:03:34 -05:00
|
|
|
}
|
2011-03-21 13:35:04 -05:00
|
|
|
auto func = parse_ty_fn(st, sd);
|
2011-05-16 20:21:22 -05:00
|
|
|
methods += [rec(proto=proto,
|
2011-05-20 16:15:39 -05:00
|
|
|
ident=name,
|
|
|
|
inputs=func._0,
|
|
|
|
output=func._1,
|
2011-06-09 11:48:16 -05:00
|
|
|
cf=func._2,
|
|
|
|
constrs=func._3)];
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
2011-03-31 17:57:36 -05:00
|
|
|
st.pos += 1u;
|
2011-05-12 10:24:54 -05:00
|
|
|
ret ty::mk_obj(st.tcx, methods);
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
2011-05-12 10:24:54 -05:00
|
|
|
case ('X') { ret ty::mk_var(st.tcx, parse_int(st)); }
|
|
|
|
case ('E') { ret ty::mk_native(st.tcx); }
|
|
|
|
case ('Y') { ret ty::mk_type(st.tcx); }
|
2011-04-29 10:26:28 -05:00
|
|
|
case ('#') {
|
|
|
|
auto pos = parse_hex(st);
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (next(st) as char == ':');
|
2011-04-29 10:26:28 -05:00
|
|
|
auto len = parse_hex(st);
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (next(st) as char == '#');
|
2011-04-29 10:26:28 -05:00
|
|
|
alt (st.tcx.rcache.find(tup(st.crate,pos,len))) {
|
2011-05-30 23:39:19 -05:00
|
|
|
case (some(?tt)) { ret tt; }
|
|
|
|
case (none) {
|
2011-04-29 10:26:28 -05:00
|
|
|
auto ps = @rec(pos=pos, len=len with *st);
|
|
|
|
auto tt = parse_ty(ps, sd);
|
|
|
|
st.tcx.rcache.insert(tup(st.crate,pos,len), tt);
|
|
|
|
ret tt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case (?c) {
|
|
|
|
log_err "unexpected char in type string: ";
|
|
|
|
log_err c;
|
|
|
|
fail;
|
|
|
|
}
|
2011-04-20 13:59:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
fn parse_mt(@pstate st, str_def sd) -> ty::mt {
|
2011-04-20 13:59:10 -05:00
|
|
|
auto mut;
|
|
|
|
alt (peek(st) as char) {
|
2011-05-12 10:24:54 -05:00
|
|
|
case ('m') {next(st); mut = ast::mut;}
|
|
|
|
case ('?') {next(st); mut = ast::maybe_mut;}
|
|
|
|
case (_) {mut=ast::imm;}
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
2011-04-20 13:59:10 -05:00
|
|
|
ret rec(ty=parse_ty(st, sd), mut=mut);
|
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
fn parse_def(@pstate st, str_def sd) -> ast::def_id {
|
2011-04-20 13:59:10 -05:00
|
|
|
auto def = "";
|
|
|
|
while (peek(st) as char != '|') {
|
2011-05-17 13:41:41 -05:00
|
|
|
def += str::unsafe_from_byte(next(st));
|
2011-04-20 13:59:10 -05:00
|
|
|
}
|
|
|
|
st.pos = st.pos + 1u;
|
|
|
|
ret sd(def);
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
|
2011-04-19 15:35:49 -05:00
|
|
|
fn parse_int(@pstate st) -> int {
|
2011-03-21 13:35:04 -05:00
|
|
|
auto n = 0;
|
|
|
|
while (true) {
|
2011-03-21 14:03:34 -05:00
|
|
|
auto cur = peek(st) as char;
|
2011-03-21 13:35:04 -05:00
|
|
|
if (cur < '0' || cur > '9') {break;}
|
|
|
|
st.pos = st.pos + 1u;
|
|
|
|
n *= 10;
|
|
|
|
n += (cur as int) - ('0' as int);
|
|
|
|
}
|
|
|
|
ret n;
|
|
|
|
}
|
|
|
|
|
2011-04-29 10:26:28 -05:00
|
|
|
fn parse_hex(@pstate st) -> uint {
|
|
|
|
auto n = 0u;
|
|
|
|
while (true) {
|
|
|
|
auto cur = peek(st) as char;
|
|
|
|
if ((cur < '0' || cur > '9') &&
|
|
|
|
(cur < 'a' || cur > 'f')) {break;}
|
|
|
|
st.pos = st.pos + 1u;
|
|
|
|
n *= 16u;
|
|
|
|
if ('0' <= cur && cur <= '9') {
|
|
|
|
n += (cur as uint) - ('0' as uint);
|
|
|
|
} else {
|
|
|
|
n += (10u + (cur as uint) - ('a' as uint));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret n;
|
|
|
|
}
|
|
|
|
|
2011-05-20 16:15:39 -05:00
|
|
|
fn parse_ty_fn(@pstate st, str_def sd) -> tup(vec[ty::arg], ty::t,
|
2011-06-09 11:48:16 -05:00
|
|
|
ast::controlflow,
|
|
|
|
vec[@ast::constr]) {
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (next(st) as char == '[');
|
2011-05-16 20:21:22 -05:00
|
|
|
let vec[ty::arg] inputs = [];
|
2011-03-21 14:03:34 -05:00
|
|
|
while (peek(st) as char != ']') {
|
2011-05-12 10:24:54 -05:00
|
|
|
auto mode = ty::mo_val;
|
2011-03-21 14:03:34 -05:00
|
|
|
if (peek(st) as char == '&') {
|
2011-06-10 05:03:50 -05:00
|
|
|
mode = ty::mo_alias(false);
|
|
|
|
st.pos += 1u;
|
|
|
|
if (peek(st) as char == 'm') {
|
|
|
|
mode = ty::mo_alias(true);
|
|
|
|
st.pos += 1u;
|
|
|
|
}
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
2011-05-16 20:21:22 -05:00
|
|
|
inputs += [rec(mode=mode, ty=parse_ty(st, sd))];
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
st.pos = st.pos + 1u;
|
2011-06-09 11:48:16 -05:00
|
|
|
auto cs = parse_constrs(st, sd);
|
2011-05-20 16:15:39 -05:00
|
|
|
auto res = parse_ty_or_bang(st, sd);
|
|
|
|
alt (res) {
|
2011-05-30 23:39:19 -05:00
|
|
|
case (a_bang) {
|
2011-06-09 11:48:16 -05:00
|
|
|
ret tup(inputs, ty::mk_bot(st.tcx), ast::noreturn, cs);
|
2011-05-20 16:15:39 -05:00
|
|
|
}
|
2011-05-30 23:39:19 -05:00
|
|
|
case (a_ty(?t)) {
|
2011-06-09 11:48:16 -05:00
|
|
|
ret tup(inputs, t, ast::return, cs);
|
2011-05-20 16:15:39 -05:00
|
|
|
}
|
|
|
|
}
|
2011-06-09 11:48:16 -05:00
|
|
|
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-24 19:21:09 -05:00
|
|
|
// Rust metadata parsing
|
2011-03-21 13:35:04 -05:00
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
fn parse_def_id(vec[u8] buf) -> ast::def_id {
|
2011-03-25 20:34:21 -05:00
|
|
|
auto colon_idx = 0u;
|
2011-05-17 13:41:41 -05:00
|
|
|
auto len = vec::len[u8](buf);
|
2011-03-25 20:34:21 -05:00
|
|
|
while (colon_idx < len && buf.(colon_idx) != (':' as u8)) {
|
|
|
|
colon_idx += 1u;
|
|
|
|
}
|
|
|
|
if (colon_idx == len) {
|
2011-04-19 05:02:06 -05:00
|
|
|
log_err "didn't find ':' when parsing def id";
|
2011-03-25 20:34:21 -05:00
|
|
|
fail;
|
|
|
|
}
|
|
|
|
|
2011-05-17 13:41:41 -05:00
|
|
|
auto crate_part = vec::slice[u8](buf, 0u, colon_idx);
|
|
|
|
auto def_part = vec::slice[u8](buf, colon_idx + 1u, len);
|
|
|
|
auto crate_num = uint::parse_buf(crate_part, 10u) as int;
|
|
|
|
auto def_num = uint::parse_buf(def_part, 10u) as int;
|
2011-03-25 20:34:21 -05:00
|
|
|
ret tup(crate_num, def_num);
|
2011-03-25 19:53:46 -05:00
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
fn lookup_hash(&ebml::doc d, fn(vec[u8]) -> bool eq_fn, uint hash)
|
2011-05-13 08:17:24 -05:00
|
|
|
-> vec[ebml::doc] {
|
2011-05-12 10:24:54 -05:00
|
|
|
auto index = ebml::get_doc(d, metadata::tag_index);
|
|
|
|
auto table = ebml::get_doc(index, metadata::tag_index_table);
|
2011-04-06 19:53:25 -05:00
|
|
|
|
|
|
|
auto hash_pos = table.start + (hash % 256u) * 4u;
|
2011-05-12 10:24:54 -05:00
|
|
|
auto pos = ebml::be_uint_from_bytes(d.data, hash_pos, 4u);
|
|
|
|
auto bucket = ebml::doc_at(d.data, pos);
|
2011-04-06 19:53:25 -05:00
|
|
|
// Awkward logic because we can't ret from foreach yet
|
2011-05-16 20:21:22 -05:00
|
|
|
let vec[ebml::doc] result = [];
|
2011-05-12 10:24:54 -05:00
|
|
|
auto belt = metadata::tag_index_buckets_bucket_elt;
|
|
|
|
for each (ebml::doc elt in ebml::tagged_docs(bucket, belt)) {
|
2011-05-13 08:17:24 -05:00
|
|
|
auto pos = ebml::be_uint_from_bytes(elt.data, elt.start, 4u);
|
2011-05-17 13:41:41 -05:00
|
|
|
if (eq_fn(vec::slice[u8](elt.data, elt.start+4u, elt.end))) {
|
|
|
|
vec::push(result, ebml::doc_at(d.data, pos));
|
2011-03-25 21:01:44 -05:00
|
|
|
}
|
|
|
|
}
|
2011-04-06 19:53:25 -05:00
|
|
|
ret result;
|
2011-03-31 20:28:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Given a path and serialized crate metadata, returns the ID of the
|
|
|
|
// definition the path refers to.
|
2011-05-13 08:17:24 -05:00
|
|
|
fn resolve_path(vec[ast::ident] path, vec[u8] data) -> vec[ast::def_id] {
|
2011-04-06 19:53:25 -05:00
|
|
|
fn eq_item(vec[u8] data, str s) -> bool {
|
2011-05-17 13:41:41 -05:00
|
|
|
ret str::eq(str::unsafe_from_bytes(data), s);
|
2011-03-31 20:28:32 -05:00
|
|
|
}
|
2011-05-17 13:41:41 -05:00
|
|
|
auto s = str::connect(path, "::");
|
2011-05-12 10:24:54 -05:00
|
|
|
auto md = ebml::new_doc(data);
|
|
|
|
auto paths = ebml::get_doc(md, metadata::tag_paths);
|
2011-03-31 20:28:32 -05:00
|
|
|
auto eqer = bind eq_item(_, s);
|
2011-05-16 20:21:22 -05:00
|
|
|
let vec[ast::def_id] result = [];
|
2011-05-13 08:17:24 -05:00
|
|
|
for (ebml::doc doc in lookup_hash(paths, eqer, metadata::hash_path(s))) {
|
|
|
|
auto did_doc = ebml::get_doc(doc, metadata::tag_def_id);
|
2011-05-17 13:41:41 -05:00
|
|
|
vec::push(result, parse_def_id(ebml::doc_data(did_doc)));
|
2011-03-31 20:28:32 -05:00
|
|
|
}
|
2011-05-13 08:17:24 -05:00
|
|
|
ret result;
|
2011-03-31 20:28:32 -05:00
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
fn maybe_find_item(int item_id, &ebml::doc items) -> option::t[ebml::doc] {
|
2011-04-06 19:53:25 -05:00
|
|
|
fn eq_item(vec[u8] bytes, int item_id) -> bool {
|
2011-05-12 10:24:54 -05:00
|
|
|
ret ebml::be_uint_from_bytes(bytes, 0u, 4u) as int == item_id;
|
2011-03-31 20:28:32 -05:00
|
|
|
}
|
|
|
|
auto eqer = bind eq_item(_, item_id);
|
2011-05-13 08:17:24 -05:00
|
|
|
auto found = lookup_hash(items, eqer, metadata::hash_def_num(item_id));
|
2011-05-17 13:41:41 -05:00
|
|
|
if (vec::len(found) == 0u) {
|
2011-05-13 08:17:24 -05:00
|
|
|
ret option::none[ebml::doc];
|
|
|
|
} else {
|
|
|
|
ret option::some[ebml::doc](found.(0));
|
|
|
|
}
|
2011-03-28 15:11:09 -05:00
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
fn find_item(int item_id, &ebml::doc items) -> ebml::doc {
|
2011-05-13 08:17:24 -05:00
|
|
|
ret option::get(maybe_find_item(item_id, items));
|
2011-03-28 18:49:26 -05:00
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
// Looks up an item in the given metadata and returns an ebml doc pointing
|
2011-04-06 19:53:25 -05:00
|
|
|
// to the item data.
|
2011-05-12 10:24:54 -05:00
|
|
|
fn lookup_item(int item_id, vec[u8] data) -> ebml::doc {
|
|
|
|
auto items = ebml::get_doc(ebml::new_doc(data), metadata::tag_items);
|
2011-04-06 19:53:25 -05:00
|
|
|
ret find_item(item_id, items);
|
2011-03-29 15:21:16 -05:00
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
fn item_kind(&ebml::doc item) -> u8 {
|
|
|
|
auto kind = ebml::get_doc(item, metadata::tag_items_data_item_kind);
|
|
|
|
ret ebml::doc_as_uint(kind) as u8;
|
2011-03-28 18:49:26 -05:00
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
fn item_symbol(&ebml::doc item) -> str {
|
|
|
|
auto sym = ebml::get_doc(item, metadata::tag_items_data_item_symbol);
|
2011-05-17 13:41:41 -05:00
|
|
|
ret str::unsafe_from_bytes(ebml::doc_data(sym));
|
2011-03-28 18:49:26 -05:00
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
fn variant_tag_id(&ebml::doc d) -> ast::def_id {
|
|
|
|
auto tagdoc = ebml::get_doc(d, metadata::tag_items_data_item_tag_id);
|
|
|
|
ret parse_def_id(ebml::doc_data(tagdoc));
|
2011-03-28 18:49:26 -05:00
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
fn item_type(&ebml::doc item, int this_cnum, ty::ctxt tcx) -> ty::t {
|
|
|
|
fn parse_external_def_id(int this_cnum, str s) -> ast::def_id {
|
2011-04-06 19:53:25 -05:00
|
|
|
// FIXME: This is completely wrong when linking against a crate
|
|
|
|
// that, in turn, links against another crate. We need a mapping
|
|
|
|
// from crate ID to crate "meta" attributes as part of the crate
|
2011-05-12 10:24:54 -05:00
|
|
|
// metadata:
|
2011-05-17 13:41:41 -05:00
|
|
|
auto buf = str::bytes(s);
|
2011-04-06 19:53:25 -05:00
|
|
|
auto external_def_id = parse_def_id(buf);
|
|
|
|
ret tup(this_cnum, external_def_id._1);
|
2011-03-28 15:11:09 -05:00
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
auto tp = ebml::get_doc(item, metadata::tag_items_data_item_type);
|
2011-05-17 13:41:41 -05:00
|
|
|
auto s = str::unsafe_from_bytes(ebml::doc_data(tp));
|
2011-04-29 10:26:28 -05:00
|
|
|
ret parse_ty_data(item.data, this_cnum, tp.start, tp.end - tp.start,
|
|
|
|
bind parse_external_def_id(this_cnum, _), tcx);
|
2011-04-01 15:02:44 -05:00
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
fn item_ty_param_count(&ebml::doc item, int this_cnum) -> uint {
|
2011-04-12 17:09:50 -05:00
|
|
|
let uint ty_param_count = 0u;
|
2011-05-12 10:24:54 -05:00
|
|
|
auto tp = metadata::tag_items_data_item_ty_param_count;
|
|
|
|
for each (ebml::doc p in ebml::tagged_docs(item, tp)) {
|
|
|
|
ty_param_count = ebml::vint_at(ebml::doc_data(p), 0u)._0;
|
2011-04-06 19:53:25 -05:00
|
|
|
}
|
2011-04-12 17:09:50 -05:00
|
|
|
ret ty_param_count;
|
2011-04-01 15:02:44 -05:00
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
fn tag_variant_ids(&ebml::doc item, int this_cnum) -> vec[ast::def_id] {
|
2011-05-16 20:21:22 -05:00
|
|
|
let vec[ast::def_id] ids = [];
|
2011-05-12 10:24:54 -05:00
|
|
|
auto v = metadata::tag_items_data_item_variant;
|
|
|
|
for each (ebml::doc p in ebml::tagged_docs(item, v)) {
|
|
|
|
auto ext = parse_def_id(ebml::doc_data(p));
|
2011-05-17 13:41:41 -05:00
|
|
|
vec::push[ast::def_id](ids, tup(this_cnum, ext._1));
|
2011-04-06 19:53:25 -05:00
|
|
|
}
|
|
|
|
ret ids;
|
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
fn get_metadata_section(str filename) -> option::t[vec[u8]] {
|
|
|
|
auto mb = llvm::LLVMRustCreateMemoryBufferWithContentsOfFile
|
2011-05-17 13:41:41 -05:00
|
|
|
(str::buf(filename));
|
2011-05-12 10:24:54 -05:00
|
|
|
if (mb as int == 0) {ret option::none[vec[u8]];}
|
2011-04-06 19:53:25 -05:00
|
|
|
auto of = mk_object_file(mb);
|
|
|
|
auto si = mk_section_iter(of.llof);
|
2011-05-12 10:24:54 -05:00
|
|
|
while (llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False) {
|
|
|
|
auto name_buf = llvm::LLVMGetSectionName(si.llsi);
|
2011-05-17 13:41:41 -05:00
|
|
|
auto name = str::str_from_cstr(name_buf);
|
|
|
|
if (str::eq(name, x86::get_meta_sect_name())) {
|
2011-05-12 10:24:54 -05:00
|
|
|
auto cbuf = llvm::LLVMGetSectionContents(si.llsi);
|
|
|
|
auto csz = llvm::LLVMGetSectionSize(si.llsi);
|
2011-05-17 13:41:41 -05:00
|
|
|
auto cvbuf = cbuf as vec::vbuf;
|
|
|
|
ret option::some[vec[u8]](vec::vec_from_vbuf[u8](cvbuf, csz));
|
2011-04-06 19:53:25 -05:00
|
|
|
}
|
2011-05-12 10:24:54 -05:00
|
|
|
llvm::LLVMMoveToNextSection(si.llsi);
|
2011-04-06 19:53:25 -05:00
|
|
|
}
|
2011-05-12 10:24:54 -05:00
|
|
|
ret option::none[vec[u8]];
|
2011-03-25 21:01:44 -05:00
|
|
|
}
|
|
|
|
|
2011-03-24 19:21:09 -05:00
|
|
|
|
2011-06-09 20:15:13 -05:00
|
|
|
fn load_crate(session::session sess,
|
|
|
|
int cnum,
|
|
|
|
ast::ident ident,
|
|
|
|
vec[str] library_search_paths) {
|
|
|
|
auto filename = parser::default_native_name(sess, ident);
|
2011-03-15 19:33:05 -05:00
|
|
|
for (str library_search_path in library_search_paths) {
|
2011-06-09 20:15:13 -05:00
|
|
|
auto path = fs::connect(library_search_path, filename);
|
|
|
|
alt (get_metadata_section(path)) {
|
|
|
|
case (option::some(?cvec)) {
|
|
|
|
sess.set_external_crate(cnum, rec(name=ident, data=cvec));
|
|
|
|
ret;
|
2011-03-24 19:21:09 -05:00
|
|
|
}
|
2011-06-09 20:15:13 -05:00
|
|
|
case (_) {}
|
2011-03-24 19:21:09 -05:00
|
|
|
}
|
2011-03-15 19:33:05 -05:00
|
|
|
}
|
|
|
|
|
2011-06-09 20:15:13 -05:00
|
|
|
log_err #fmt("can't open crate '%s' (looked for '%s' in lib search path)",
|
|
|
|
ident, filename);
|
2011-03-24 19:21:09 -05:00
|
|
|
fail;
|
2011-03-15 19:33:05 -05:00
|
|
|
}
|
2011-03-15 18:30:43 -05:00
|
|
|
|
2011-05-26 19:16:54 -05:00
|
|
|
type env = @rec(
|
|
|
|
session::session sess,
|
|
|
|
resolve::crate_map crate_map,
|
|
|
|
@hashmap[str, int] crate_cache,
|
|
|
|
vec[str] library_search_paths,
|
|
|
|
mutable int next_crate_num
|
|
|
|
);
|
2011-03-15 18:30:43 -05:00
|
|
|
|
2011-05-26 19:16:54 -05:00
|
|
|
fn visit_view_item(env e, &@ast::view_item i) {
|
|
|
|
alt (i.node) {
|
|
|
|
case (ast::view_item_use(?ident, ?meta_items, ?id, ?ann)) {
|
|
|
|
auto cnum;
|
|
|
|
if (!e.crate_cache.contains_key(ident)) {
|
|
|
|
cnum = e.next_crate_num;
|
2011-06-09 20:15:13 -05:00
|
|
|
load_crate(e.sess, cnum, ident,
|
|
|
|
e.library_search_paths);
|
2011-05-26 19:16:54 -05:00
|
|
|
e.crate_cache.insert(ident, e.next_crate_num);
|
|
|
|
e.next_crate_num += 1;
|
|
|
|
} else {
|
|
|
|
cnum = e.crate_cache.get(ident);
|
|
|
|
}
|
|
|
|
e.crate_map.insert(ann.id, cnum);
|
|
|
|
}
|
|
|
|
case (_) { }
|
|
|
|
}
|
2011-03-15 18:30:43 -05:00
|
|
|
}
|
|
|
|
|
2011-05-26 19:16:54 -05:00
|
|
|
|
2011-03-15 18:30:43 -05:00
|
|
|
// Reads external crates referenced by "use" directives.
|
2011-05-12 10:24:54 -05:00
|
|
|
fn read_crates(session::session sess,
|
2011-05-26 19:16:54 -05:00
|
|
|
resolve::crate_map crate_map,
|
|
|
|
&ast::crate crate) {
|
2011-03-15 19:33:05 -05:00
|
|
|
auto e = @rec(
|
2011-03-24 19:21:09 -05:00
|
|
|
sess=sess,
|
2011-05-26 19:16:54 -05:00
|
|
|
crate_map=crate_map,
|
2011-05-12 10:24:54 -05:00
|
|
|
crate_cache=@common::new_str_hash[int](),
|
2011-05-04 18:53:42 -05:00
|
|
|
library_search_paths=sess.get_opts().library_search_paths,
|
2011-03-25 13:10:50 -05:00
|
|
|
mutable next_crate_num=1
|
2011-03-15 19:33:05 -05:00
|
|
|
);
|
|
|
|
|
2011-05-26 19:16:54 -05:00
|
|
|
auto v = rec(visit_view_item_pre=bind visit_view_item(e, _)
|
|
|
|
with walk::default_visitor());
|
|
|
|
walk::walk_crate(v, crate);
|
2011-03-15 18:30:43 -05:00
|
|
|
}
|
|
|
|
|
2011-03-25 19:53:46 -05:00
|
|
|
|
2011-03-30 19:23:25 -05:00
|
|
|
fn kind_has_type_params(u8 kind_ch) -> bool {
|
2011-05-13 08:17:24 -05:00
|
|
|
ret alt (kind_ch as char) {
|
|
|
|
case ('c') { false } case ('f') { true } case ('F') { true }
|
|
|
|
case ('y') { true } case ('o') { true } case ('t') { true }
|
|
|
|
case ('T') { false } case ('m') { false } case ('n') { false }
|
|
|
|
case ('v') { true }
|
|
|
|
};
|
2011-03-30 19:23:25 -05:00
|
|
|
}
|
|
|
|
|
2011-03-25 19:53:46 -05:00
|
|
|
// Crate metadata queries
|
|
|
|
|
2011-05-13 08:17:24 -05:00
|
|
|
fn lookup_defs(session::session sess, int cnum, vec[ast::ident] path)
|
|
|
|
-> vec[ast::def] {
|
2011-04-18 02:22:23 -05:00
|
|
|
auto data = sess.get_external_crate(cnum).data;
|
2011-03-25 19:53:46 -05:00
|
|
|
|
2011-05-17 13:41:41 -05:00
|
|
|
ret vec::map(bind lookup_def(cnum, data, _),
|
2011-06-10 05:02:57 -05:00
|
|
|
resolve_path(path, data));
|
2011-05-13 08:17:24 -05:00
|
|
|
}
|
2011-03-25 19:53:46 -05:00
|
|
|
|
2011-05-13 08:17:24 -05:00
|
|
|
// FIXME doesn't yet handle re-exported externals
|
2011-06-10 05:02:57 -05:00
|
|
|
fn lookup_def(int cnum, vec[u8] data, &ast::def_id did_) -> ast::def {
|
|
|
|
auto item = lookup_item(did_._1, data);
|
2011-04-06 19:53:25 -05:00
|
|
|
auto kind_ch = item_kind(item);
|
2011-03-28 15:11:09 -05:00
|
|
|
|
2011-06-10 05:02:57 -05:00
|
|
|
auto did = tup(cnum, did_._1);
|
2011-03-28 15:11:09 -05:00
|
|
|
|
2011-05-13 08:17:24 -05:00
|
|
|
auto def = alt (kind_ch as char) {
|
|
|
|
case ('c') { ast::def_const(did) }
|
|
|
|
case ('f') { ast::def_fn(did) }
|
|
|
|
case ('F') { ast::def_native_fn(did) }
|
|
|
|
case ('y') { ast::def_ty(did) }
|
|
|
|
case ('o') { ast::def_obj(did) }
|
|
|
|
case ('T') { ast::def_native_ty(did) }
|
2011-03-30 20:28:33 -05:00
|
|
|
// We treat references to tags as references to types.
|
2011-05-13 08:17:24 -05:00
|
|
|
case ('t') { ast::def_ty(did) }
|
|
|
|
case ('m') { ast::def_mod(did) }
|
|
|
|
case ('n') { ast::def_native_mod(did) }
|
|
|
|
case ('v') {
|
|
|
|
auto tid = variant_tag_id(item);
|
|
|
|
tid = tup(cnum, tid._1);
|
|
|
|
ast::def_variant(tid, did)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
ret def;
|
2011-03-24 19:35:27 -05:00
|
|
|
}
|
|
|
|
|
2011-05-19 19:21:21 -05:00
|
|
|
fn get_type(ty::ctxt tcx, ast::def_id def) -> ty::ty_param_count_and_ty {
|
2011-03-28 18:49:26 -05:00
|
|
|
auto external_crate_id = def._0;
|
2011-05-19 19:21:21 -05:00
|
|
|
auto data = tcx.sess.get_external_crate(external_crate_id).data;
|
2011-04-06 19:53:25 -05:00
|
|
|
auto item = lookup_item(def._1, data);
|
2011-04-25 14:15:55 -05:00
|
|
|
auto t = item_type(item, external_crate_id, tcx);
|
2011-03-30 19:23:25 -05:00
|
|
|
|
2011-04-12 17:09:50 -05:00
|
|
|
auto tp_count;
|
2011-04-06 19:53:25 -05:00
|
|
|
auto kind_ch = item_kind(item);
|
2011-03-30 19:23:25 -05:00
|
|
|
auto has_ty_params = kind_has_type_params(kind_ch);
|
|
|
|
if (has_ty_params) {
|
2011-04-12 17:09:50 -05:00
|
|
|
tp_count = item_ty_param_count(item, external_crate_id);
|
2011-03-30 19:23:25 -05:00
|
|
|
} else {
|
2011-04-12 17:09:50 -05:00
|
|
|
tp_count = 0u;
|
2011-03-30 19:23:25 -05:00
|
|
|
}
|
|
|
|
|
2011-04-12 17:09:50 -05:00
|
|
|
ret tup(tp_count, t);
|
2011-03-25 16:32:39 -05:00
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
fn get_symbol(session::session sess, ast::def_id def) -> str {
|
2011-03-29 15:21:16 -05:00
|
|
|
auto external_crate_id = def._0;
|
2011-04-18 02:22:23 -05:00
|
|
|
auto data = sess.get_external_crate(external_crate_id).data;
|
2011-05-13 08:17:24 -05:00
|
|
|
ret item_symbol(lookup_item(def._1, data));
|
2011-03-29 15:21:16 -05:00
|
|
|
}
|
|
|
|
|
2011-05-19 19:21:21 -05:00
|
|
|
fn get_tag_variants(ty::ctxt tcx, ast::def_id def)
|
|
|
|
-> vec[ty::variant_info] {
|
2011-04-01 15:02:44 -05:00
|
|
|
auto external_crate_id = def._0;
|
2011-05-19 19:21:21 -05:00
|
|
|
auto data = tcx.sess.get_external_crate(external_crate_id).data;
|
2011-05-12 10:24:54 -05:00
|
|
|
auto items = ebml::get_doc(ebml::new_doc(data), metadata::tag_items);
|
2011-04-06 19:53:25 -05:00
|
|
|
auto item = find_item(def._1, items);
|
2011-04-01 15:02:44 -05:00
|
|
|
|
2011-05-19 19:21:21 -05:00
|
|
|
let vec[ty::variant_info] infos = [];
|
2011-04-06 19:53:25 -05:00
|
|
|
auto variant_ids = tag_variant_ids(item, external_crate_id);
|
2011-05-12 10:24:54 -05:00
|
|
|
for (ast::def_id did in variant_ids) {
|
2011-04-06 19:53:25 -05:00
|
|
|
auto item = find_item(did._1, items);
|
2011-04-25 14:15:55 -05:00
|
|
|
auto ctor_ty = item_type(item, external_crate_id, tcx);
|
2011-05-16 20:21:22 -05:00
|
|
|
let vec[ty::t] arg_tys = [];
|
2011-05-12 10:24:54 -05:00
|
|
|
alt (ty::struct(tcx, ctor_ty)) {
|
2011-06-09 11:48:16 -05:00
|
|
|
case (ty::ty_fn(_, ?args, _, _, _)) {
|
2011-05-12 10:24:54 -05:00
|
|
|
for (ty::arg a in args) {
|
2011-05-16 20:21:22 -05:00
|
|
|
arg_tys += [a.ty];
|
2011-04-01 15:02:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case (_) {
|
|
|
|
// Nullary tag variant.
|
|
|
|
}
|
|
|
|
}
|
2011-05-16 20:21:22 -05:00
|
|
|
infos += [rec(args=arg_tys, ctor_ty=ctor_ty, id=did)];
|
2011-04-01 15:02:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ret infos;
|
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
fn list_file_metadata(str path, io::writer out) {
|
2011-04-07 13:20:57 -05:00
|
|
|
alt (get_metadata_section(path)) {
|
2011-05-30 23:39:19 -05:00
|
|
|
case (option::some(?bytes)) {
|
2011-04-07 13:20:57 -05:00
|
|
|
list_crate_metadata(bytes, out);
|
|
|
|
}
|
2011-05-30 23:39:19 -05:00
|
|
|
case (option::none) {
|
2011-04-07 13:20:57 -05:00
|
|
|
out.write_str("Could not find metadata in " + path + ".\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
fn read_path(&ebml::doc d) -> tup(str, uint) {
|
|
|
|
auto desc = ebml::doc_data(d);
|
|
|
|
auto pos = ebml::be_uint_from_bytes(desc, 0u, 4u);
|
2011-05-17 13:41:41 -05:00
|
|
|
auto pathbytes = vec::slice[u8](desc, 4u, vec::len[u8](desc));
|
|
|
|
auto path = str::unsafe_from_bytes(pathbytes);
|
2011-04-07 13:20:57 -05:00
|
|
|
ret tup(path, pos);
|
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
fn list_crate_metadata(vec[u8] bytes, io::writer out) {
|
|
|
|
auto md = ebml::new_doc(bytes);
|
|
|
|
auto paths = ebml::get_doc(md, metadata::tag_paths);
|
|
|
|
auto items = ebml::get_doc(md, metadata::tag_items);
|
|
|
|
auto index = ebml::get_doc(paths, metadata::tag_index);
|
|
|
|
auto bs = ebml::get_doc(index, metadata::tag_index_buckets);
|
|
|
|
for each (ebml::doc bucket in
|
|
|
|
ebml::tagged_docs(bs, metadata::tag_index_buckets_bucket)) {
|
|
|
|
auto et = metadata::tag_index_buckets_bucket_elt;
|
|
|
|
for each (ebml::doc elt in ebml::tagged_docs(bucket, et)) {
|
2011-04-07 13:20:57 -05:00
|
|
|
auto data = read_path(elt);
|
2011-05-12 10:24:54 -05:00
|
|
|
auto def = ebml::doc_at(bytes, data._1);
|
|
|
|
auto did_doc = ebml::get_doc(def, metadata::tag_def_id);
|
|
|
|
auto did = parse_def_id(ebml::doc_data(did_doc));
|
2011-04-07 13:20:57 -05:00
|
|
|
out.write_str(#fmt("%s (%s)\n", data._0,
|
|
|
|
describe_def(items, did)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
fn describe_def(&ebml::doc items, ast::def_id id) -> str {
|
2011-05-13 08:17:24 -05:00
|
|
|
if (id._0 != 0) { ret "external"; }
|
|
|
|
ret item_kind_to_str(item_kind(find_item(id._1, items)));
|
2011-04-07 13:20:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn item_kind_to_str(u8 kind) -> str {
|
|
|
|
alt (kind as char) {
|
|
|
|
case ('c') {ret "const";}
|
|
|
|
case ('f') {ret "fn";}
|
|
|
|
case ('F') {ret "native fn";}
|
|
|
|
case ('y') {ret "type";}
|
|
|
|
case ('o') {ret "obj";}
|
|
|
|
case ('T') {ret "native type";}
|
|
|
|
case ('t') {ret "type";}
|
|
|
|
case ('m') {ret "mod";}
|
|
|
|
case ('n') {ret "native mod";}
|
|
|
|
case ('v') {ret "tag";}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-24 19:35:27 -05:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-03-25 17:07:27 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2011-03-24 19:35:27 -05:00
|
|
|
// End:
|