2011-03-15 18:30:43 -05:00
|
|
|
// -*- rust -*-
|
|
|
|
|
|
|
|
import driver.session;
|
|
|
|
import front.ast;
|
2011-03-24 19:21:09 -05:00
|
|
|
import lib.llvm.False;
|
|
|
|
import lib.llvm.llvm;
|
2011-03-15 20:05:29 -05:00
|
|
|
import lib.llvm.mk_object_file;
|
|
|
|
import lib.llvm.mk_section_iter;
|
2011-03-15 18:30:43 -05:00
|
|
|
import middle.fold;
|
2011-03-25 19:53:46 -05:00
|
|
|
import middle.metadata;
|
2011-04-01 15:02:44 -05:00
|
|
|
import middle.trans;
|
2011-03-21 13:35:04 -05:00
|
|
|
import middle.ty;
|
2011-03-24 19:21:09 -05:00
|
|
|
import back.x86;
|
2011-03-15 18:30:43 -05:00
|
|
|
import util.common;
|
|
|
|
import util.common.span;
|
2011-03-15 19:33:05 -05:00
|
|
|
|
2011-03-15 20:05:29 -05:00
|
|
|
import std._str;
|
2011-03-25 20:34:21 -05:00
|
|
|
import std._uint;
|
2011-03-21 13:35:04 -05:00
|
|
|
import std._vec;
|
2011-03-25 19:53:46 -05:00
|
|
|
import std.ebml;
|
2011-03-15 19:33:05 -05:00
|
|
|
import std.fs;
|
2011-03-25 19:53:46 -05:00
|
|
|
import std.io;
|
2011-03-21 13:35:04 -05:00
|
|
|
import std.option;
|
2011-03-25 19:53:46 -05:00
|
|
|
import std.option.none;
|
2011-03-25 13:10:50 -05:00
|
|
|
import std.option.some;
|
2011-03-15 20:05:29 -05:00
|
|
|
import std.os;
|
2011-03-15 18:30:43 -05:00
|
|
|
import std.map.hashmap;
|
|
|
|
|
|
|
|
// TODO: map to a real type here.
|
2011-03-15 19:33:05 -05:00
|
|
|
type env = @rec(
|
2011-03-24 19:21:09 -05:00
|
|
|
session.session sess,
|
2011-03-25 13:10:50 -05:00
|
|
|
@hashmap[str, int] crate_cache,
|
|
|
|
vec[str] library_search_paths,
|
|
|
|
mutable int next_crate_num
|
2011-03-15 19:33:05 -05:00
|
|
|
);
|
|
|
|
|
2011-03-25 19:53:46 -05:00
|
|
|
tag resolve_result {
|
|
|
|
rr_ok(ast.def_id);
|
2011-03-31 20:28:32 -05:00
|
|
|
rr_not_found(ast.ident);
|
2011-03-25 19:53:46 -05:00
|
|
|
}
|
|
|
|
|
2011-03-21 13:35:04 -05:00
|
|
|
// Type decoding
|
|
|
|
|
|
|
|
// Compact string representation for ty.t values. API ty_str & parse_from_str.
|
|
|
|
// (The second has to be authed pure.) Extra parameters are for converting
|
|
|
|
// to/from def_ids in the string rep. 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 pstate = rec(str rep, mutable uint pos, uint len);
|
|
|
|
|
2011-03-21 14:03:34 -05:00
|
|
|
fn peek(@pstate st) -> u8 {
|
|
|
|
if (st.pos < st.len) {ret st.rep.(st.pos) as u8;}
|
|
|
|
else {ret ' ' as u8;}
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
2011-03-21 14:03:34 -05:00
|
|
|
impure fn next(@pstate st) -> u8 { // ?? somehow not recognized as impure
|
2011-03-21 13:35:04 -05:00
|
|
|
if (st.pos >= st.len) {fail;}
|
|
|
|
auto ch = st.rep.(st.pos);
|
|
|
|
st.pos = st.pos + 1u;
|
2011-03-21 14:03:34 -05:00
|
|
|
ret ch as u8;
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
|
2011-04-06 19:53:25 -05:00
|
|
|
fn parse_ty_str(str rep, str_def sd) -> @ty.t {
|
2011-03-21 13:35:04 -05:00
|
|
|
auto len = _str.byte_len(rep);
|
|
|
|
auto st = @rec(rep=rep, mutable pos=0u, len=len);
|
|
|
|
auto result = parse_ty(st, sd);
|
2011-03-31 17:57:36 -05:00
|
|
|
if (st.pos != len) {
|
|
|
|
log "parse_ty_str: incomplete parse, stopped at byte "
|
|
|
|
+ _uint.to_str(st.pos, 10u) + " of "
|
|
|
|
+ _uint.to_str(len, 10u) + " in str '" + rep + "'";
|
|
|
|
}
|
2011-03-21 13:35:04 -05:00
|
|
|
ret result;
|
|
|
|
}
|
|
|
|
|
|
|
|
impure fn parse_ty(@pstate st, str_def sd) -> @ty.t {
|
|
|
|
ret @rec(struct=parse_sty(st, sd),
|
|
|
|
cname=option.none[str]);
|
|
|
|
}
|
|
|
|
|
2011-03-21 13:44:08 -05:00
|
|
|
impure fn parse_mt(@pstate st, str_def sd) -> ty.mt {
|
|
|
|
auto mut;
|
2011-03-21 14:03:34 -05:00
|
|
|
alt (peek(st) as char) {
|
2011-03-21 13:44:08 -05:00
|
|
|
case ('m') {next(st); mut = ast.mut;}
|
|
|
|
case ('?') {next(st); mut = ast.maybe_mut;}
|
|
|
|
case (_) {mut=ast.imm;}
|
|
|
|
}
|
|
|
|
ret rec(ty=parse_ty(st, sd), mut=mut);
|
|
|
|
}
|
|
|
|
|
2011-03-29 19:25:55 -05:00
|
|
|
impure fn parse_def(@pstate st, str_def sd) -> ast.def_id {
|
|
|
|
auto def = "";
|
|
|
|
while (peek(st) as char != '|') {
|
|
|
|
def += _str.unsafe_from_byte(next(st));
|
|
|
|
}
|
|
|
|
st.pos = st.pos + 1u;
|
|
|
|
ret sd(def);
|
|
|
|
}
|
|
|
|
|
2011-03-21 13:35:04 -05:00
|
|
|
impure fn parse_sty(@pstate st, str_def sd) -> ty.sty {
|
2011-03-21 14:03:34 -05:00
|
|
|
alt (next(st) as char) {
|
2011-03-21 13:35:04 -05:00
|
|
|
case ('n') {ret ty.ty_nil;}
|
|
|
|
case ('b') {ret ty.ty_bool;}
|
|
|
|
case ('i') {ret ty.ty_int;}
|
|
|
|
case ('u') {ret ty.ty_uint;}
|
2011-03-24 13:37:50 -05:00
|
|
|
case ('l') {ret ty.ty_float;}
|
2011-03-21 13:35:04 -05:00
|
|
|
case ('M') {
|
2011-03-21 14:03:34 -05:00
|
|
|
alt (next(st) as char) {
|
2011-03-21 13:35:04 -05:00
|
|
|
case ('b') {ret ty.ty_machine(common.ty_u8);}
|
|
|
|
case ('w') {ret ty.ty_machine(common.ty_u16);}
|
|
|
|
case ('l') {ret ty.ty_machine(common.ty_u32);}
|
|
|
|
case ('d') {ret ty.ty_machine(common.ty_u64);}
|
|
|
|
case ('B') {ret ty.ty_machine(common.ty_i8);}
|
|
|
|
case ('W') {ret ty.ty_machine(common.ty_i16);}
|
|
|
|
case ('L') {ret ty.ty_machine(common.ty_i32);}
|
|
|
|
case ('D') {ret ty.ty_machine(common.ty_i64);}
|
|
|
|
case ('f') {ret ty.ty_machine(common.ty_f32);}
|
|
|
|
case ('F') {ret ty.ty_machine(common.ty_f64);}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case ('c') {ret ty.ty_char;}
|
|
|
|
case ('s') {ret ty.ty_str;}
|
|
|
|
case ('t') {
|
2011-03-21 14:03:34 -05:00
|
|
|
check(next(st) as char == '[');
|
2011-03-29 19:25:55 -05:00
|
|
|
auto def = parse_def(st, sd);
|
2011-03-21 13:35:04 -05:00
|
|
|
let vec[@ty.t] params = vec();
|
2011-03-21 14:03:34 -05:00
|
|
|
while (peek(st) as char != ']') {
|
|
|
|
params += vec(parse_ty(st, sd));
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
st.pos = st.pos + 1u;
|
2011-03-29 19:25:55 -05:00
|
|
|
ret ty.ty_tag(def, params);
|
2011-03-24 13:37:50 -05:00
|
|
|
}
|
2011-03-29 19:25:55 -05:00
|
|
|
case ('p') {ret ty.ty_param(parse_def(st, sd));}
|
2011-03-21 13:44:08 -05:00
|
|
|
case ('@') {ret ty.ty_box(parse_mt(st, sd));}
|
|
|
|
case ('V') {ret ty.ty_vec(parse_mt(st, sd));}
|
2011-03-21 13:35:04 -05:00
|
|
|
case ('P') {ret ty.ty_port(parse_ty(st, sd));}
|
|
|
|
case ('C') {ret ty.ty_chan(parse_ty(st, sd));}
|
|
|
|
case ('T') {
|
2011-03-21 14:03:34 -05:00
|
|
|
check(next(st) as char == '[');
|
2011-03-21 13:44:08 -05:00
|
|
|
let vec[ty.mt] params = vec();
|
2011-03-21 14:03:34 -05:00
|
|
|
while (peek(st) as char != ']') {
|
|
|
|
params += vec(parse_mt(st, sd));
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
st.pos = st.pos + 1u;
|
|
|
|
ret ty.ty_tup(params);
|
|
|
|
}
|
|
|
|
case ('R') {
|
2011-03-21 14:03:34 -05:00
|
|
|
check(next(st) as char == '[');
|
2011-03-21 13:35:04 -05:00
|
|
|
let vec[ty.field] fields = vec();
|
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 != '=') {
|
|
|
|
name += _str.unsafe_from_byte(next(st));
|
|
|
|
}
|
2011-03-21 13:35:04 -05:00
|
|
|
st.pos = st.pos + 1u;
|
2011-03-21 14:03:34 -05:00
|
|
|
fields += vec(rec(ident=name, mt=parse_mt(st, sd)));
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
st.pos = st.pos + 1u;
|
|
|
|
ret ty.ty_rec(fields);
|
|
|
|
}
|
|
|
|
case ('F') {
|
|
|
|
auto func = parse_ty_fn(st, sd);
|
|
|
|
ret ty.ty_fn(ast.proto_fn, func._0, func._1);
|
|
|
|
}
|
|
|
|
case ('W') {
|
|
|
|
auto func = parse_ty_fn(st, sd);
|
|
|
|
ret ty.ty_fn(ast.proto_iter, func._0, func._1);
|
|
|
|
}
|
|
|
|
case ('N') {
|
|
|
|
auto abi;
|
2011-03-21 14:03:34 -05:00
|
|
|
alt (next(st) as char) {
|
2011-03-21 13:35:04 -05:00
|
|
|
case ('r') {abi = ast.native_abi_rust;}
|
|
|
|
case ('c') {abi = ast.native_abi_cdecl;}
|
2011-03-28 10:24:11 -05:00
|
|
|
case ('l') {abi = ast.native_abi_llvm;}
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
auto func = parse_ty_fn(st, sd);
|
|
|
|
ret ty.ty_native_fn(abi,func._0,func._1);
|
|
|
|
}
|
|
|
|
case ('O') {
|
2011-03-21 14:03:34 -05:00
|
|
|
check(next(st) as char == '[');
|
2011-03-21 13:35:04 -05:00
|
|
|
let vec[ty.method] methods = vec();
|
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-03-21 13:35:04 -05:00
|
|
|
case ('W') {proto = ast.proto_iter;}
|
|
|
|
case ('F') {proto = ast.proto_fn;}
|
|
|
|
}
|
|
|
|
auto name = "";
|
2011-03-21 14:03:34 -05:00
|
|
|
while (peek(st) as char != '[') {
|
|
|
|
name += _str.unsafe_from_byte(next(st));
|
|
|
|
}
|
2011-03-21 13:35:04 -05:00
|
|
|
auto func = parse_ty_fn(st, sd);
|
2011-03-21 14:03:34 -05:00
|
|
|
methods += vec(rec(proto=proto,
|
|
|
|
ident=name,
|
|
|
|
inputs=func._0,
|
|
|
|
output=func._1));
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
2011-03-31 17:57:36 -05:00
|
|
|
st.pos += 1u;
|
2011-03-21 13:35:04 -05:00
|
|
|
ret ty.ty_obj(methods);
|
|
|
|
}
|
|
|
|
case ('X') {ret ty.ty_var(parse_int(st));}
|
|
|
|
case ('E') {ret ty.ty_native;}
|
2011-03-26 19:35:51 -05:00
|
|
|
case ('Y') {ret ty.ty_type;}
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impure fn parse_int(@pstate st) -> int {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
impure fn parse_ty_fn(@pstate st, str_def sd) -> tup(vec[ty.arg], @ty.t) {
|
2011-03-21 14:03:34 -05:00
|
|
|
check(next(st) as char == '[');
|
2011-03-21 13:35:04 -05:00
|
|
|
let vec[ty.arg] inputs = vec();
|
2011-03-21 14:03:34 -05:00
|
|
|
while (peek(st) as char != ']') {
|
2011-03-21 13:35:04 -05:00
|
|
|
auto mode = ast.val;
|
2011-03-21 14:03:34 -05:00
|
|
|
if (peek(st) as char == '&') {
|
2011-03-21 13:35:04 -05:00
|
|
|
mode = ast.alias;
|
|
|
|
st.pos = st.pos + 1u;
|
|
|
|
}
|
2011-03-21 14:03:34 -05:00
|
|
|
inputs += vec(rec(mode=mode, ty=parse_ty(st, sd)));
|
2011-03-21 13:35:04 -05:00
|
|
|
}
|
|
|
|
st.pos = st.pos + 1u;
|
|
|
|
ret tup(inputs, parse_ty(st, sd));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-24 19:21:09 -05:00
|
|
|
// Rust metadata parsing
|
2011-03-21 13:35:04 -05:00
|
|
|
|
2011-03-25 20:34:21 -05:00
|
|
|
fn parse_def_id(vec[u8] buf) -> ast.def_id {
|
|
|
|
auto colon_idx = 0u;
|
|
|
|
auto len = _vec.len[u8](buf);
|
|
|
|
while (colon_idx < len && buf.(colon_idx) != (':' as u8)) {
|
|
|
|
colon_idx += 1u;
|
|
|
|
}
|
|
|
|
if (colon_idx == len) {
|
|
|
|
log "didn't find ':' when parsing def id";
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
ret tup(crate_num, def_num);
|
2011-03-25 19:53:46 -05:00
|
|
|
}
|
|
|
|
|
2011-04-06 19:53:25 -05:00
|
|
|
fn lookup_hash(&ebml.doc d, fn(vec[u8]) -> bool eq_fn, uint hash)
|
|
|
|
-> option.t[ebml.doc] {
|
|
|
|
auto index = ebml.get_doc(d, metadata.tag_index);
|
|
|
|
auto table = ebml.get_doc(index, metadata.tag_index_table);
|
|
|
|
|
|
|
|
auto hash_pos = table.start + (hash % 256u) * 4u;
|
|
|
|
auto pos = ebml.be_uint_from_bytes(d.data, hash_pos, 4u);
|
|
|
|
auto bucket = ebml.doc_at(d.data, pos);
|
|
|
|
// Awkward logic because we can't ret from foreach yet
|
|
|
|
auto result = option.none[ebml.doc];
|
|
|
|
auto belt = metadata.tag_index_buckets_bucket_elt;
|
|
|
|
for each (ebml.doc elt in ebml.tagged_docs(bucket, belt)) {
|
|
|
|
alt (result) {
|
|
|
|
case (option.none[ebml.doc]) {
|
|
|
|
auto pos = ebml.be_uint_from_bytes(elt.data, elt.start, 4u);
|
|
|
|
if (eq_fn(_vec.slice[u8](elt.data, elt.start+4u, elt.end))) {
|
|
|
|
result = option.some[ebml.doc](ebml.doc_at(d.data, pos));
|
|
|
|
}
|
2011-03-25 21:01:44 -05:00
|
|
|
}
|
2011-04-06 19:53:25 -05:00
|
|
|
case (_) {}
|
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-04-06 19:53:25 -05:00
|
|
|
fn resolve_path(vec[ast.ident] path, vec[u8] data) -> resolve_result {
|
|
|
|
fn eq_item(vec[u8] data, str s) -> bool {
|
|
|
|
ret _str.eq(_str.unsafe_from_bytes(data), s);
|
2011-03-31 20:28:32 -05:00
|
|
|
}
|
|
|
|
auto s = _str.connect(path, ".");
|
2011-04-06 19:53:25 -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-04-06 19:53:25 -05:00
|
|
|
alt (lookup_hash(paths, eqer, metadata.hash_path(s))) {
|
|
|
|
case (option.some[ebml.doc](?d)) {
|
|
|
|
auto did_doc = ebml.get_doc(d, metadata.tag_def_id);
|
|
|
|
ret rr_ok(parse_def_id(ebml.doc_data(did_doc)));
|
|
|
|
}
|
|
|
|
case (option.none[ebml.doc]) {
|
|
|
|
ret rr_not_found(s);
|
|
|
|
}
|
2011-03-31 20:28:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-06 19:53:25 -05:00
|
|
|
fn maybe_find_item(int item_id, &ebml.doc items) -> option.t[ebml.doc] {
|
|
|
|
fn eq_item(vec[u8] bytes, int item_id) -> bool {
|
|
|
|
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-04-06 19:53:25 -05:00
|
|
|
ret lookup_hash(items, eqer, metadata.hash_def_num(item_id));
|
2011-03-28 15:11:09 -05:00
|
|
|
}
|
|
|
|
|
2011-04-06 19:53:25 -05:00
|
|
|
fn find_item(int item_id, &ebml.doc items) -> ebml.doc {
|
|
|
|
alt (maybe_find_item(item_id, items)) {
|
|
|
|
case (option.some[ebml.doc](?d)) {ret d;}
|
2011-03-28 18:49:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-06 19:53:25 -05:00
|
|
|
// Looks up an item in the given metadata and returns an EBML doc pointing
|
|
|
|
// to the item data.
|
|
|
|
fn lookup_item(int item_id, vec[u8] data) -> ebml.doc {
|
|
|
|
auto items = ebml.get_doc(ebml.new_doc(data), metadata.tag_items);
|
|
|
|
ret find_item(item_id, items);
|
2011-03-29 15:21:16 -05:00
|
|
|
}
|
|
|
|
|
2011-04-06 19:53:25 -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-04-06 19:53:25 -05:00
|
|
|
fn item_symbol(&ebml.doc item) -> str {
|
|
|
|
auto sym = ebml.get_doc(item, metadata.tag_items_data_item_symbol);
|
|
|
|
ret _str.unsafe_from_bytes(ebml.doc_data(sym));
|
2011-03-28 18:49:26 -05:00
|
|
|
}
|
|
|
|
|
2011-04-06 19:53:25 -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-04-06 19:53:25 -05:00
|
|
|
fn item_type(&ebml.doc item, int this_cnum) -> @ty.t {
|
|
|
|
fn parse_external_def_id(int this_cnum, str s) -> ast.def_id {
|
|
|
|
// 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
|
|
|
|
// metadata.
|
|
|
|
auto buf = _str.bytes(s);
|
|
|
|
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-04-06 19:53:25 -05:00
|
|
|
auto tp = ebml.get_doc(item, metadata.tag_items_data_item_type);
|
|
|
|
auto s = _str.unsafe_from_bytes(ebml.doc_data(tp));
|
|
|
|
ret parse_ty_str(s, bind parse_external_def_id(this_cnum, _));
|
2011-04-01 15:02:44 -05:00
|
|
|
}
|
|
|
|
|
2011-04-06 19:53:25 -05:00
|
|
|
fn item_ty_params(&ebml.doc item, int this_cnum) -> vec[ast.def_id] {
|
|
|
|
let vec[ast.def_id] params = vec();
|
|
|
|
auto tp = metadata.tag_items_data_item_ty_param;
|
|
|
|
for each (ebml.doc p in ebml.tagged_docs(item, tp)) {
|
|
|
|
auto ext = parse_def_id(ebml.doc_data(p));
|
|
|
|
_vec.push[ast.def_id](params, tup(this_cnum, ext._1));
|
|
|
|
}
|
|
|
|
ret params;
|
2011-04-01 15:02:44 -05:00
|
|
|
}
|
|
|
|
|
2011-04-06 19:53:25 -05:00
|
|
|
fn tag_variant_ids(&ebml.doc item, int this_cnum) -> vec[ast.def_id] {
|
|
|
|
let vec[ast.def_id] ids = vec();
|
|
|
|
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));
|
|
|
|
_vec.push[ast.def_id](ids, tup(this_cnum, ext._1));
|
|
|
|
}
|
|
|
|
ret ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_metadata_section(str filename) -> option.t[vec[u8]] {
|
|
|
|
auto mb = llvm.LLVMRustCreateMemoryBufferWithContentsOfFile
|
|
|
|
(_str.buf(filename));
|
|
|
|
if (mb as int == 0) {ret option.none[vec[u8]];}
|
|
|
|
auto of = mk_object_file(mb);
|
|
|
|
auto si = mk_section_iter(of.llof);
|
|
|
|
while (llvm.LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False) {
|
|
|
|
auto name_buf = llvm.LLVMGetSectionName(si.llsi);
|
|
|
|
auto name = _str.str_from_cstr(name_buf);
|
|
|
|
if (_str.eq(name, x86.get_meta_sect_name())) {
|
|
|
|
auto cbuf = llvm.LLVMGetSectionContents(si.llsi);
|
|
|
|
auto csz = llvm.LLVMGetSectionSize(si.llsi);
|
|
|
|
auto cvbuf = cbuf as _vec.vbuf;
|
|
|
|
ret option.some[vec[u8]](_vec.vec_from_vbuf[u8](cvbuf, csz));
|
|
|
|
}
|
|
|
|
llvm.LLVMMoveToNextSection(si.llsi);
|
|
|
|
}
|
|
|
|
ret option.none[vec[u8]];
|
2011-03-25 21:01:44 -05:00
|
|
|
}
|
|
|
|
|
2011-03-24 19:21:09 -05:00
|
|
|
|
|
|
|
fn load_crate(session.session sess,
|
2011-03-25 19:53:46 -05:00
|
|
|
int cnum,
|
2011-03-24 19:21:09 -05:00
|
|
|
ast.ident ident,
|
2011-03-25 13:10:50 -05:00
|
|
|
vec[str] library_search_paths) {
|
2011-03-24 19:21:09 -05:00
|
|
|
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-03-24 19:21:09 -05:00
|
|
|
auto path = fs.connect(library_search_path, filename);
|
2011-04-06 19:53:25 -05:00
|
|
|
alt (get_metadata_section(path)) {
|
|
|
|
case (option.some[vec[u8]](?cvec)) {
|
|
|
|
sess.set_external_crate(cnum, cvec);
|
|
|
|
ret;
|
2011-03-24 19:21:09 -05:00
|
|
|
}
|
2011-04-06 19:53:25 -05:00
|
|
|
case (_) {}
|
2011-03-24 19:21:09 -05:00
|
|
|
}
|
2011-03-15 19:33:05 -05:00
|
|
|
}
|
|
|
|
|
2011-03-24 19:21:09 -05:00
|
|
|
log #fmt("can't open crate '%s' (looked for '%s' in lib search paths)",
|
|
|
|
ident, filename);
|
|
|
|
fail;
|
2011-03-15 19:33:05 -05:00
|
|
|
}
|
2011-03-15 18:30:43 -05:00
|
|
|
|
|
|
|
fn fold_view_item_use(&env e, &span sp, ast.ident ident,
|
2011-03-25 13:10:50 -05:00
|
|
|
vec[@ast.meta_item] meta_items, ast.def_id id, option.t[int] cnum_opt)
|
2011-03-15 19:33:05 -05:00
|
|
|
-> @ast.view_item {
|
2011-03-25 13:10:50 -05:00
|
|
|
auto cnum;
|
2011-03-15 19:33:05 -05:00
|
|
|
if (!e.crate_cache.contains_key(ident)) {
|
2011-03-25 13:10:50 -05:00
|
|
|
cnum = e.next_crate_num;
|
2011-03-25 19:53:46 -05:00
|
|
|
load_crate(e.sess, cnum, ident, e.library_search_paths);
|
2011-03-25 13:10:50 -05:00
|
|
|
e.crate_cache.insert(ident, e.next_crate_num);
|
|
|
|
e.next_crate_num += 1;
|
2011-03-15 19:33:05 -05:00
|
|
|
} else {
|
2011-03-25 13:10:50 -05:00
|
|
|
cnum = e.crate_cache.get(ident);
|
2011-03-15 19:33:05 -05:00
|
|
|
}
|
2011-03-15 18:30:43 -05:00
|
|
|
|
2011-03-25 13:10:50 -05:00
|
|
|
auto viu = ast.view_item_use(ident, meta_items, id, some[int](cnum));
|
2011-03-15 18:30:43 -05:00
|
|
|
ret @fold.respan[ast.view_item_](sp, viu);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads external crates referenced by "use" directives.
|
2011-03-15 19:33:05 -05:00
|
|
|
fn read_crates(session.session sess,
|
|
|
|
@ast.crate crate,
|
|
|
|
vec[str] library_search_paths) -> @ast.crate {
|
|
|
|
auto e = @rec(
|
2011-03-24 19:21:09 -05:00
|
|
|
sess=sess,
|
2011-03-25 13:10:50 -05:00
|
|
|
crate_cache=@common.new_str_hash[int](),
|
|
|
|
library_search_paths=library_search_paths,
|
|
|
|
mutable next_crate_num=1
|
2011-03-15 19:33:05 -05:00
|
|
|
);
|
|
|
|
|
2011-03-15 18:30:43 -05:00
|
|
|
auto f = fold_view_item_use;
|
|
|
|
auto fld = @rec(fold_view_item_use=f with *fold.new_identity_fold[env]());
|
2011-03-15 19:33:05 -05:00
|
|
|
ret fold.fold_crate[env](e, fld, 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 {
|
|
|
|
// FIXME: It'd be great if we had u8 char literals.
|
|
|
|
if (kind_ch == ('c' as u8)) { ret false; }
|
|
|
|
else if (kind_ch == ('f' as u8)) { ret true; }
|
2011-03-31 17:57:36 -05:00
|
|
|
else if (kind_ch == ('F' as u8)) { ret true; }
|
2011-03-30 20:28:33 -05:00
|
|
|
else if (kind_ch == ('y' as u8)) { ret true; }
|
2011-03-30 19:23:25 -05:00
|
|
|
else if (kind_ch == ('o' as u8)) { ret true; }
|
|
|
|
else if (kind_ch == ('t' as u8)) { ret true; }
|
2011-04-01 19:04:51 -05:00
|
|
|
else if (kind_ch == ('T' as u8)) { ret false; }
|
2011-03-30 19:23:25 -05:00
|
|
|
else if (kind_ch == ('m' as u8)) { ret false; }
|
|
|
|
else if (kind_ch == ('n' as u8)) { ret false; }
|
|
|
|
else if (kind_ch == ('v' as u8)) { ret true; }
|
|
|
|
else {
|
|
|
|
log #fmt("kind_has_type_params(): unknown kind char: %d",
|
|
|
|
kind_ch as int);
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-25 19:53:46 -05:00
|
|
|
// Crate metadata queries
|
|
|
|
|
2011-03-29 19:01:27 -05:00
|
|
|
fn lookup_def(session.session sess, int cnum, vec[ast.ident] path)
|
|
|
|
-> option.t[ast.def] {
|
2011-03-25 19:53:46 -05:00
|
|
|
auto data = sess.get_external_crate(cnum);
|
|
|
|
|
|
|
|
auto did;
|
|
|
|
alt (resolve_path(path, data)) {
|
|
|
|
case (rr_ok(?di)) { did = di; }
|
2011-03-31 20:28:32 -05:00
|
|
|
case (rr_not_found(?name)) {
|
2011-03-29 19:01:27 -05:00
|
|
|
ret none[ast.def];
|
2011-03-25 19:53:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-06 19:53:25 -05:00
|
|
|
auto item = lookup_item(did._1, data);
|
|
|
|
auto kind_ch = item_kind(item);
|
2011-03-28 15:11:09 -05:00
|
|
|
|
|
|
|
did = tup(cnum, did._1);
|
|
|
|
|
|
|
|
// FIXME: It'd be great if we had u8 char literals.
|
2011-03-29 19:01:27 -05:00
|
|
|
auto def;
|
2011-03-30 20:28:33 -05:00
|
|
|
if (kind_ch == ('c' as u8)) { def = ast.def_const(did); }
|
|
|
|
else if (kind_ch == ('f' as u8)) { def = ast.def_fn(did); }
|
2011-03-31 17:57:36 -05:00
|
|
|
else if (kind_ch == ('F' as u8)) { def = ast.def_native_fn(did); }
|
2011-03-30 20:28:33 -05:00
|
|
|
else if (kind_ch == ('y' as u8)) { def = ast.def_ty(did); }
|
|
|
|
else if (kind_ch == ('o' as u8)) { def = ast.def_obj(did); }
|
2011-04-01 19:04:51 -05:00
|
|
|
else if (kind_ch == ('T' as u8)) { def = ast.def_native_ty(did); }
|
2011-03-30 20:28:33 -05:00
|
|
|
else if (kind_ch == ('t' as u8)) {
|
|
|
|
// We treat references to tags as references to types.
|
|
|
|
def = ast.def_ty(did);
|
|
|
|
} else if (kind_ch == ('m' as u8)) { def = ast.def_mod(did); }
|
|
|
|
else if (kind_ch == ('n' as u8)) { def = ast.def_native_mod(did); }
|
2011-03-28 15:11:09 -05:00
|
|
|
else if (kind_ch == ('v' as u8)) {
|
2011-04-06 19:53:25 -05:00
|
|
|
auto tid = variant_tag_id(item);
|
2011-03-28 15:11:09 -05:00
|
|
|
tid = tup(cnum, tid._1);
|
2011-03-29 19:01:27 -05:00
|
|
|
def = ast.def_variant(tid, did);
|
|
|
|
} else {
|
|
|
|
log #fmt("lookup_def(): unknown kind char: %d", kind_ch as int);
|
|
|
|
fail;
|
2011-03-28 15:11:09 -05:00
|
|
|
}
|
2011-03-25 19:53:46 -05:00
|
|
|
|
2011-03-29 19:01:27 -05:00
|
|
|
ret some[ast.def](def);
|
2011-03-24 19:35:27 -05:00
|
|
|
}
|
|
|
|
|
2011-03-30 19:23:25 -05:00
|
|
|
fn get_type(session.session sess, ast.def_id def) -> ty.ty_params_opt_and_ty {
|
2011-03-28 18:49:26 -05:00
|
|
|
auto external_crate_id = def._0;
|
|
|
|
auto data = sess.get_external_crate(external_crate_id);
|
2011-04-06 19:53:25 -05:00
|
|
|
auto item = lookup_item(def._1, data);
|
|
|
|
auto t = item_type(item, external_crate_id);
|
2011-03-30 19:23:25 -05:00
|
|
|
|
|
|
|
auto tps_opt;
|
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-06 19:53:25 -05:00
|
|
|
auto tps = item_ty_params(item, external_crate_id);
|
2011-03-30 19:23:25 -05:00
|
|
|
tps_opt = some[vec[ast.def_id]](tps);
|
|
|
|
} else {
|
|
|
|
tps_opt = none[vec[ast.def_id]];
|
|
|
|
}
|
|
|
|
|
|
|
|
ret tup(tps_opt, t);
|
2011-03-25 16:32:39 -05:00
|
|
|
}
|
|
|
|
|
2011-03-29 15:21:16 -05:00
|
|
|
fn get_symbol(session.session sess, ast.def_id def) -> str {
|
|
|
|
auto external_crate_id = def._0;
|
|
|
|
auto data = sess.get_external_crate(external_crate_id);
|
2011-04-06 19:53:25 -05:00
|
|
|
auto item = lookup_item(def._1, data);
|
|
|
|
ret item_symbol(item);
|
2011-03-29 15:21:16 -05:00
|
|
|
}
|
|
|
|
|
2011-04-01 15:02:44 -05:00
|
|
|
fn get_tag_variants(session.session sess, ast.def_id def)
|
|
|
|
-> vec[trans.variant_info] {
|
|
|
|
auto external_crate_id = def._0;
|
|
|
|
auto data = sess.get_external_crate(external_crate_id);
|
2011-04-06 19:53:25 -05:00
|
|
|
auto items = ebml.get_doc(ebml.new_doc(data), metadata.tag_items);
|
|
|
|
auto item = find_item(def._1, items);
|
2011-04-01 15:02:44 -05:00
|
|
|
|
|
|
|
let vec[trans.variant_info] infos = vec();
|
2011-04-06 19:53:25 -05:00
|
|
|
auto variant_ids = tag_variant_ids(item, external_crate_id);
|
2011-04-01 15:02:44 -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);
|
|
|
|
auto ctor_ty = item_type(item, external_crate_id);
|
2011-04-01 15:02:44 -05:00
|
|
|
let vec[@ty.t] arg_tys = vec();
|
|
|
|
alt (ctor_ty.struct) {
|
|
|
|
case (ty.ty_fn(_, ?args, _)) {
|
|
|
|
for (ty.arg a in args) {
|
|
|
|
arg_tys += vec(a.ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case (_) {
|
|
|
|
// Nullary tag variant.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
infos += vec(rec(args=arg_tys, ctor_ty=ctor_ty, id=did));
|
|
|
|
}
|
|
|
|
|
|
|
|
ret infos;
|
|
|
|
}
|
|
|
|
|
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:
|