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.llvmext;
|
|
|
|
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-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-21 13:35:04 -05:00
|
|
|
import std._vec;
|
2011-03-15 19:33:05 -05:00
|
|
|
import std.fs;
|
2011-03-21 13:35:04 -05:00
|
|
|
import std.option;
|
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-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-03-21 14:03:34 -05:00
|
|
|
impure 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);
|
|
|
|
check(st.pos == len);
|
|
|
|
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-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-21 13:35:04 -05:00
|
|
|
auto def = "";
|
2011-03-21 14:03:34 -05:00
|
|
|
while (peek(st) as char != '|') {
|
|
|
|
def += _str.unsafe_from_byte(next(st));
|
|
|
|
}
|
2011-03-21 13:35:04 -05:00
|
|
|
st.pos = st.pos + 1u;
|
|
|
|
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;
|
|
|
|
ret ty.ty_tag(sd(def), params);
|
2011-03-24 13:37:50 -05:00
|
|
|
}
|
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;}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
ret ty.ty_obj(methods);
|
|
|
|
}
|
|
|
|
case ('X') {ret ty.ty_var(parse_int(st));}
|
|
|
|
case ('E') {ret ty.ty_native;}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-24 19:21:09 -05:00
|
|
|
// TODO
|
|
|
|
|
|
|
|
|
|
|
|
fn load_crate(session.session sess,
|
|
|
|
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);
|
|
|
|
auto pbuf = _str.buf(path);
|
|
|
|
auto mb = llvmext.LLVMRustCreateMemoryBufferWithContentsOfFile(pbuf);
|
|
|
|
if (mb as int != 0) {
|
|
|
|
auto of = mk_object_file(mb);
|
|
|
|
auto si = mk_section_iter(of.llof);
|
|
|
|
while (llvmext.LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) ==
|
|
|
|
False) {
|
|
|
|
auto name_buf = llvmext.LLVMGetSectionName(si.llsi);
|
|
|
|
auto name = _str.str_from_cstr(name_buf);
|
|
|
|
if (_str.eq(name, x86.get_meta_sect_name())) {
|
|
|
|
auto cbuf = llvmext.LLVMGetSectionContents(si.llsi);
|
|
|
|
auto csz = llvmext.LLVMGetSectionSize(si.llsi);
|
|
|
|
auto cvbuf = cbuf as _vec.vbuf;
|
2011-03-25 13:10:50 -05:00
|
|
|
ret;
|
2011-03-24 19:21:09 -05:00
|
|
|
}
|
2011-03-25 13:10:50 -05:00
|
|
|
llvmext.LLVMMoveToNextSection(si.llsi);
|
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
|
|
|
load_crate(e.sess, ident, e.library_search_paths);
|
|
|
|
cnum = e.next_crate_num;
|
|
|
|
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 13:10:50 -05:00
|
|
|
fn lookup_def(session.session sess, &span sp, int cnum, vec[ast.ident] path)
|
|
|
|
-> ast.def {
|
2011-03-24 19:35:27 -05:00
|
|
|
// FIXME: fill in.
|
|
|
|
fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 16:05:38 -05:00
|
|
|
// compile-command: "make -k -C ../.. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2011-03-24 19:35:27 -05:00
|
|
|
// End:
|