2011-03-15 18:30:43 -05:00
|
|
|
|
2011-06-15 13:19:50 -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-06-27 16:18:32 -05:00
|
|
|
import cwriter;
|
2011-05-12 10:24:54 -05:00
|
|
|
import middle::trans;
|
|
|
|
import middle::ty;
|
|
|
|
import back::x86;
|
|
|
|
import util::common;
|
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-06-27 16:54:59 -05:00
|
|
|
import defs::*;
|
2011-06-27 17:30:17 -05:00
|
|
|
import tydecode::parse_def_id;
|
|
|
|
import tydecode::parse_ty_data;
|
2011-03-15 18:30:43 -05:00
|
|
|
|
2011-06-22 21:04:04 -05:00
|
|
|
export get_symbol;
|
|
|
|
export get_tag_variants;
|
|
|
|
export get_type;
|
|
|
|
export read_crates;
|
|
|
|
export lookup_defs;
|
|
|
|
export get_type;
|
|
|
|
export list_file_metadata;
|
2011-03-15 19:33:05 -05:00
|
|
|
|
2011-03-25 19:53:46 -05:00
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
fn lookup_hash(&ebml::doc d, fn(vec[u8]) -> bool eq_fn, uint hash) ->
|
|
|
|
vec[ebml::doc] {
|
2011-06-27 16:54:59 -05:00
|
|
|
auto index = ebml::get_doc(d, tag_index);
|
|
|
|
auto table = ebml::get_doc(index, tag_index_table);
|
2011-06-15 13:19:50 -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-06-15 13:19:50 -05:00
|
|
|
|
2011-05-16 20:21:22 -05:00
|
|
|
let vec[ebml::doc] result = [];
|
2011-06-27 16:54:59 -05:00
|
|
|
auto belt = tag_index_buckets_bucket_elt;
|
2011-05-12 10:24:54 -05:00
|
|
|
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-06-15 13:19:50 -05:00
|
|
|
if (eq_fn(vec::slice[u8](elt.data, elt.start + 4u, elt.end))) {
|
2011-05-17 13:41:41 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
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);
|
2011-06-27 16:54:59 -05:00
|
|
|
auto paths = ebml::get_doc(md, 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-06-27 17:20:17 -05:00
|
|
|
for (ebml::doc doc in lookup_hash(paths, eqer, encoder::hash_path(s))) {
|
2011-06-27 16:54:59 -05:00
|
|
|
auto did_doc = ebml::get_doc(doc, 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-06-27 17:20:17 -05:00
|
|
|
auto found = lookup_hash(items, eqer, encoder::hash_def_id(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];
|
2011-06-15 13:19:50 -05:00
|
|
|
} 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-06-15 13:19:50 -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 {
|
2011-06-27 16:54:59 -05:00
|
|
|
auto items = ebml::get_doc(ebml::new_doc(data), 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 {
|
2011-06-27 16:54:59 -05:00
|
|
|
auto kind = ebml::get_doc(item, tag_items_data_item_kind);
|
2011-05-12 10:24:54 -05:00
|
|
|
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 {
|
2011-06-27 16:54:59 -05:00
|
|
|
auto sym = ebml::get_doc(item, 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 {
|
2011-06-27 16:54:59 -05:00
|
|
|
auto tagdoc = ebml::get_doc(d, tag_items_data_item_tag_id);
|
2011-05-12 10:24:54 -05:00
|
|
|
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-06-15 13:19:50 -05:00
|
|
|
|
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-06-27 16:54:59 -05:00
|
|
|
auto tp = ebml::get_doc(item, 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-06-27 16:54:59 -05:00
|
|
|
auto tp = tag_items_data_item_ty_param_count;
|
2011-05-12 10:24:54 -05:00
|
|
|
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-06-27 16:54:59 -05:00
|
|
|
auto v = tag_items_data_item_variant;
|
2011-05-12 10:24:54 -05:00
|
|
|
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-06-15 13:19:50 -05:00
|
|
|
fn get_exported_metadata(&session::session sess, &str path, &vec[u8] data) ->
|
|
|
|
hashmap[str, str] {
|
|
|
|
auto meta_items =
|
2011-06-27 16:54:59 -05:00
|
|
|
ebml::get_doc(ebml::new_doc(data), tag_meta_export);
|
2011-06-10 17:54:41 -05:00
|
|
|
auto mm = common::new_str_hash[str]();
|
2011-06-15 13:19:50 -05:00
|
|
|
for each (ebml::doc m in
|
2011-06-27 16:54:59 -05:00
|
|
|
ebml::tagged_docs(meta_items, tag_meta_item)) {
|
|
|
|
auto kd = ebml::get_doc(m, tag_meta_item_key);
|
|
|
|
auto vd = ebml::get_doc(m, tag_meta_item_value);
|
2011-06-10 17:54:41 -05:00
|
|
|
auto k = str::unsafe_from_bytes(ebml::doc_data(kd));
|
|
|
|
auto v = str::unsafe_from_bytes(ebml::doc_data(vd));
|
|
|
|
log #fmt("metadata in %s: %s = %s", path, k, v);
|
2011-06-15 13:19:50 -05:00
|
|
|
if (!mm.insert(k, v)) {
|
2011-06-10 17:54:41 -05:00
|
|
|
sess.warn(#fmt("Duplicate metadata item in %s: %s", path, k));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret mm;
|
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
fn metadata_matches(hashmap[str, str] mm, &vec[@ast::meta_item] metas) ->
|
|
|
|
bool {
|
2011-06-10 17:54:41 -05:00
|
|
|
log #fmt("matching %u metadata requirements against %u metadata items",
|
|
|
|
vec::len(metas), mm.size());
|
|
|
|
for (@ast::meta_item mi in metas) {
|
2011-06-21 16:23:16 -05:00
|
|
|
alt (mi.node) {
|
|
|
|
case (ast::meta_key_value(?key, ?value)) {
|
|
|
|
alt (mm.find(key)) {
|
|
|
|
case (some(?v)) {
|
|
|
|
if (v == value) {
|
|
|
|
log #fmt("matched '%s': '%s'", key,
|
|
|
|
value);
|
|
|
|
} else {
|
|
|
|
log #fmt("missing '%s': '%s' (got '%s')",
|
|
|
|
key,
|
|
|
|
value, v);
|
|
|
|
ret false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case (none) {
|
|
|
|
log #fmt("missing '%s': '%s'",
|
|
|
|
key, value);
|
|
|
|
ret false;
|
|
|
|
}
|
2011-06-10 17:54:41 -05:00
|
|
|
}
|
|
|
|
}
|
2011-06-21 16:23:16 -05:00
|
|
|
case (_) {
|
|
|
|
// FIXME (#487): Support all forms of meta_item
|
|
|
|
log_err "unimplemented meta_item variant in metadata_matches";
|
2011-06-15 13:19:50 -05:00
|
|
|
ret false;
|
2011-06-10 17:54:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-10 14:56:42 -05:00
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
|
2011-06-27 14:24:44 -05:00
|
|
|
fn default_native_lib_naming(session::session sess) ->
|
|
|
|
rec(str prefix, str suffix) {
|
|
|
|
alt (sess.get_targ_cfg().os) {
|
|
|
|
case (session::os_win32) { ret rec(prefix="", suffix=".dll"); }
|
|
|
|
case (session::os_macos) { ret rec(prefix="lib", suffix=".dylib"); }
|
|
|
|
case (session::os_linux) { ret rec(prefix="lib", suffix=".so"); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
fn find_library_crate(&session::session sess, &ast::ident ident,
|
2011-06-10 14:56:42 -05:00
|
|
|
&vec[@ast::meta_item] metas,
|
2011-06-15 13:19:50 -05:00
|
|
|
&vec[str] library_search_paths) ->
|
|
|
|
option::t[tup(str, vec[u8])] {
|
2011-06-10 14:56:42 -05:00
|
|
|
let str crate_name = ident;
|
|
|
|
for (@ast::meta_item mi in metas) {
|
2011-06-21 16:23:16 -05:00
|
|
|
alt (mi.node) {
|
|
|
|
case (ast::meta_key_value(?key, ?value)) {
|
|
|
|
if (key == "name") {
|
|
|
|
crate_name = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case (_) {
|
|
|
|
// FIXME (#487)
|
|
|
|
sess.unimpl("meta_item variant")
|
|
|
|
}
|
|
|
|
}
|
2011-06-10 14:56:42 -05:00
|
|
|
}
|
2011-06-27 14:24:44 -05:00
|
|
|
auto nn = default_native_lib_naming(sess);
|
2011-06-10 14:56:42 -05:00
|
|
|
let str prefix = nn.prefix + crate_name;
|
|
|
|
// FIXME: we could probably use a 'glob' function in std::fs but it will
|
|
|
|
// be much easier to write once the unsafe module knows more about FFI
|
|
|
|
// tricks. Currently the glob(3) interface is a bit more than we can
|
|
|
|
// stomach from here, and writing a C++ wrapper is more work than just
|
|
|
|
// manually filtering fs::list_dir here.
|
|
|
|
|
2011-03-15 19:33:05 -05:00
|
|
|
for (str library_search_path in library_search_paths) {
|
2011-06-10 14:56:42 -05:00
|
|
|
for (str path in fs::list_dir(library_search_path)) {
|
|
|
|
let str f = fs::basename(path);
|
2011-06-15 13:19:50 -05:00
|
|
|
if (!(str::starts_with(f, prefix) &&
|
|
|
|
str::ends_with(f, nn.suffix))) {
|
|
|
|
log #fmt("skipping %s, doesn't look like %s*%s", path, prefix,
|
|
|
|
nn.suffix);
|
2011-06-10 14:56:42 -05:00
|
|
|
cont;
|
|
|
|
}
|
|
|
|
alt (get_metadata_section(path)) {
|
|
|
|
case (option::some(?cvec)) {
|
2011-06-10 17:54:41 -05:00
|
|
|
auto mm = get_exported_metadata(sess, path, cvec);
|
|
|
|
if (!metadata_matches(mm, metas)) {
|
2011-06-10 14:56:42 -05:00
|
|
|
log #fmt("skipping %s, metadata doesn't match", path);
|
|
|
|
cont;
|
|
|
|
}
|
|
|
|
log #fmt("found %s with matching metadata", path);
|
|
|
|
ret some(tup(path, cvec));
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
case (_) { }
|
2011-03-24 19:21:09 -05:00
|
|
|
}
|
|
|
|
}
|
2011-03-15 19:33:05 -05:00
|
|
|
}
|
2011-06-10 14:56:42 -05:00
|
|
|
ret none;
|
|
|
|
}
|
2011-03-15 19:33:05 -05:00
|
|
|
|
2011-06-27 17:30:17 -05:00
|
|
|
fn get_metadata_section(str filename) -> option::t[vec[u8]] {
|
|
|
|
auto b = str::buf(filename);
|
|
|
|
auto mb = llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(b);
|
|
|
|
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-06-20 16:36:15 -05:00
|
|
|
fn load_library_crate(&session::session sess, int cnum, &ast::ident ident,
|
2011-06-10 14:56:42 -05:00
|
|
|
&vec[@ast::meta_item] metas,
|
|
|
|
&vec[str] library_search_paths) {
|
|
|
|
alt (find_library_crate(sess, ident, metas, library_search_paths)) {
|
|
|
|
case (some(?t)) {
|
2011-06-15 13:19:50 -05:00
|
|
|
sess.set_external_crate(cnum, rec(name=ident, data=t._1));
|
2011-06-27 14:24:44 -05:00
|
|
|
sess.add_used_crate_file(t._0);
|
2011-06-10 14:56:42 -05:00
|
|
|
ret;
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
case (_) { }
|
2011-06-10 14:56:42 -05:00
|
|
|
}
|
|
|
|
log_err #fmt("can't find crate for '%s'", ident);
|
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-06-15 13:19:50 -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) {
|
2011-06-19 15:41:21 -05:00
|
|
|
case (ast::view_item_use(?ident, ?meta_items, ?id)) {
|
2011-05-26 19:16:54 -05:00
|
|
|
auto cnum;
|
|
|
|
if (!e.crate_cache.contains_key(ident)) {
|
|
|
|
cnum = e.next_crate_num;
|
2011-06-10 14:56:42 -05:00
|
|
|
load_library_crate(e.sess, cnum, ident, meta_items,
|
|
|
|
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;
|
2011-06-15 13:19:50 -05:00
|
|
|
} else { cnum = e.crate_cache.get(ident); }
|
2011-06-19 15:41:21 -05:00
|
|
|
e.crate_map.insert(id, cnum);
|
2011-05-26 19:16:54 -05:00
|
|
|
}
|
|
|
|
case (_) { }
|
|
|
|
}
|
2011-03-15 18:30:43 -05:00
|
|
|
}
|
|
|
|
|
2011-06-27 14:24:44 -05:00
|
|
|
fn visit_item(env e, &@ast::item i) {
|
|
|
|
alt (i.node) {
|
|
|
|
case (ast::item_native_mod(?m)) {
|
|
|
|
auto name;
|
|
|
|
if (m.native_name == "" ) {
|
|
|
|
name = i.ident;
|
|
|
|
} else {
|
|
|
|
name = m.native_name;
|
|
|
|
}
|
|
|
|
alt (m.abi) {
|
|
|
|
case (ast::native_abi_rust) {
|
|
|
|
e.sess.add_used_library(name);
|
|
|
|
}
|
|
|
|
case (ast::native_abi_cdecl) {
|
|
|
|
e.sess.add_used_library(name);
|
|
|
|
}
|
|
|
|
case (ast::native_abi_llvm) {
|
|
|
|
}
|
|
|
|
case (ast::native_abi_rust_intrinsic) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case (_) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-26 19:16:54 -05:00
|
|
|
|
2011-03-15 18:30:43 -05:00
|
|
|
// Reads external crates referenced by "use" directives.
|
2011-06-15 13:19:50 -05:00
|
|
|
fn read_crates(session::session sess, resolve::crate_map crate_map,
|
2011-05-26 19:16:54 -05:00
|
|
|
&ast::crate crate) {
|
2011-06-15 13:19:50 -05:00
|
|
|
auto e =
|
|
|
|
@rec(sess=sess,
|
|
|
|
crate_map=crate_map,
|
|
|
|
crate_cache=@common::new_str_hash[int](),
|
|
|
|
library_search_paths=sess.get_opts().library_search_paths,
|
|
|
|
mutable next_crate_num=1);
|
|
|
|
auto v =
|
2011-06-27 14:24:44 -05:00
|
|
|
rec(visit_view_item_pre=bind visit_view_item(e, _),
|
|
|
|
visit_item_pre=bind visit_item(e, _)
|
2011-06-15 13:19:50 -05:00
|
|
|
with walk::default_visitor());
|
2011-05-26 19:16:54 -05:00
|
|
|
walk::walk_crate(v, crate);
|
2011-03-15 18:30:43 -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) {
|
2011-06-15 13:19:50 -05:00
|
|
|
case ('c') { false }
|
|
|
|
case ('f') { true }
|
2011-06-20 19:29:54 -05:00
|
|
|
case ('p') { true }
|
2011-06-15 13:19:50 -05:00
|
|
|
case ('F') { true }
|
|
|
|
case ('y') { 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
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
// Crate metadata queries
|
|
|
|
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-06-15 13:19:50 -05:00
|
|
|
ret vec::map(bind lookup_def(cnum, data, _), resolve_path(path, data));
|
2011-05-13 08:17:24 -05:00
|
|
|
}
|
2011-03-25 19:53:46 -05:00
|
|
|
|
2011-06-15 13:19:50 -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-06-10 05:02:57 -05:00
|
|
|
auto did = tup(cnum, did_._1);
|
2011-06-15 13:19:50 -05:00
|
|
|
auto def =
|
|
|
|
alt (kind_ch as char) {
|
|
|
|
case ('c') { ast::def_const(did) }
|
2011-06-20 19:29:54 -05:00
|
|
|
case ('f') { ast::def_fn(did, ast::impure_fn) }
|
|
|
|
case ('p') { ast::def_fn(did, ast::pure_fn) }
|
2011-06-15 13:19:50 -05:00
|
|
|
case ('F') { ast::def_native_fn(did) }
|
|
|
|
case ('y') { ast::def_ty(did) }
|
|
|
|
case ('T') { ast::def_native_ty(did) }
|
2011-06-23 11:07:58 -05:00
|
|
|
// We treat references to tags as references to types.
|
|
|
|
case ('t') { ast::def_ty(did) }
|
2011-06-15 13:19:50 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
};
|
2011-05-13 08:17:24 -05:00
|
|
|
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-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-06-15 13:19:50 -05:00
|
|
|
} else { tp_count = 0u; }
|
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-06-15 13:19:50 -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-06-27 16:54:59 -05:00
|
|
|
auto items = ebml::get_doc(ebml::new_doc(data), tag_items);
|
2011-04-06 19:53:25 -05:00
|
|
|
auto item = find_item(def._1, items);
|
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-06-15 13:19:50 -05:00
|
|
|
for (ty::arg a in args) { arg_tys += [a.ty]; }
|
2011-04-01 15:02:44 -05:00
|
|
|
}
|
|
|
|
case (_) {
|
|
|
|
// Nullary tag variant.
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-04-01 15:02:44 -05:00
|
|
|
}
|
|
|
|
}
|
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-06-15 13:19:50 -05:00
|
|
|
case (option::some(?bytes)) { 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);
|
2011-06-27 16:54:59 -05:00
|
|
|
auto paths = ebml::get_doc(md, tag_paths);
|
|
|
|
auto items = ebml::get_doc(md, tag_items);
|
|
|
|
auto index = ebml::get_doc(paths, tag_index);
|
|
|
|
auto bs = ebml::get_doc(index, tag_index_buckets);
|
2011-05-12 10:24:54 -05:00
|
|
|
for each (ebml::doc bucket in
|
2011-06-27 16:54:59 -05:00
|
|
|
ebml::tagged_docs(bs, tag_index_buckets_bucket)) {
|
|
|
|
auto et = tag_index_buckets_bucket_elt;
|
2011-05-12 10:24:54 -05:00
|
|
|
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);
|
2011-06-27 16:54:59 -05:00
|
|
|
auto did_doc = ebml::get_doc(def, tag_def_id);
|
2011-05-12 10:24:54 -05:00
|
|
|
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) {
|
2011-06-15 13:19:50 -05:00
|
|
|
case ('c') { ret "const"; }
|
|
|
|
case ('f') { ret "fn"; }
|
2011-06-20 19:29:54 -05:00
|
|
|
case ('p') { ret "pred"; }
|
2011-06-15 13:19:50 -05:00
|
|
|
case ('F') { ret "native fn"; }
|
|
|
|
case ('y') { ret "type"; }
|
|
|
|
case ('T') { ret "native type"; }
|
|
|
|
case ('t') { ret "type"; }
|
|
|
|
case ('m') { ret "mod"; }
|
|
|
|
case ('n') { ret "native mod"; }
|
|
|
|
case ('v') { ret "tag"; }
|
2011-04-07 13:20:57 -05:00
|
|
|
}
|
|
|
|
}
|
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:
|