rustc: Eliminate metadata's dependency on session

This commit is contained in:
Brian Anderson 2012-05-22 17:48:04 -07:00
parent 98b93b6c86
commit c0a36b71be
5 changed files with 64 additions and 49 deletions

View File

@ -162,7 +162,11 @@ fn compile_upto(sess: session, cfg: ast::crate_cfg,
time(time_passes, "ast indexing", time(time_passes, "ast indexing",
bind syntax::ast_map::map_crate(sess.diagnostic(), *crate)); bind syntax::ast_map::map_crate(sess.diagnostic(), *crate));
time(time_passes, "external crate/lib resolution", time(time_passes, "external crate/lib resolution",
bind creader::read_crates(sess, *crate)); bind creader::read_crates(
sess.diagnostic(), *crate, sess.cstore,
sess.filesearch,
session::sess_os_to_meta_os(sess.targ_cfg.os),
sess.opts.static));
let {def_map, exp_map, impl_map} = let {def_map, exp_map, impl_map} =
time(time_passes, "resolution", time(time_passes, "resolution",
bind resolve::resolve_crate(sess, ast_map, crate)); bind resolve::resolve_crate(sess, ast_map, crate));

View File

@ -14,8 +14,6 @@ mod back {
} }
mod driver { mod driver {
import session = driver_::session;
export session;
} }
mod util { mod util {

View File

@ -4,22 +4,28 @@ Validates all used crates and native libraries and loads their metadata
"]; "];
import driver::session; import syntax::diagnostic::span_handler;
import session::session;
import syntax::{ast, ast_util}; import syntax::{ast, ast_util};
import syntax::attr; import syntax::attr;
import syntax::visit; import syntax::visit;
import syntax::codemap::span; import syntax::codemap::span;
import std::map::{hashmap, int_hash}; import std::map::{hashmap, int_hash};
import syntax::print::pprust; import syntax::print::pprust;
import util::filesearch::filesearch;
import common::*; import common::*;
export read_crates; export read_crates;
// Traverses an AST, reading all the information about use'd crates and native // Traverses an AST, reading all the information about use'd crates and native
// libraries necessary for later resolving, typechecking, linking, etc. // libraries necessary for later resolving, typechecking, linking, etc.
fn read_crates(sess: session::session, crate: ast::crate) { fn read_crates(diag: span_handler, crate: ast::crate,
let e = @{sess: sess, cstore: cstore::cstore, filesearch: filesearch,
os: loader::os, static: bool) {
let e = @{diag: diag,
filesearch: filesearch,
cstore: cstore,
os: os,
static: static,
mut crate_cache: [], mut crate_cache: [],
mut next_crate_num: 1}; mut next_crate_num: 1};
let v = let v =
@ -29,7 +35,7 @@ fn read_crates(sess: session::session, crate: ast::crate) {
with *visit::default_simple_visitor()}); with *visit::default_simple_visitor()});
visit::visit_crate(crate, (), v); visit::visit_crate(crate, (), v);
dump_crates(e.crate_cache); dump_crates(e.crate_cache);
warn_if_multiple_versions(sess, copy e.crate_cache); warn_if_multiple_versions(diag, copy e.crate_cache);
} }
type cache_entry = { type cache_entry = {
@ -54,7 +60,7 @@ fn dump_crates(crate_cache: [cache_entry]) {
} }
} }
fn warn_if_multiple_versions(sess: session::session, fn warn_if_multiple_versions(diag: span_handler,
crate_cache: [cache_entry]) { crate_cache: [cache_entry]) {
import either::*; import either::*;
@ -73,21 +79,26 @@ fn warn_if_multiple_versions(sess: session::session,
assert matches.is_not_empty(); assert matches.is_not_empty();
if matches.len() != 1u { if matches.len() != 1u {
sess.warn(#fmt("using multiple versions of crate `%s`", name)); diag.handler().warn(
#fmt("using multiple versions of crate `%s`", name));
for matches.each {|match| for matches.each {|match|
sess.span_note(match.span, "used here"); diag.span_note(match.span, "used here");
let attrs = [ let attrs = [
attr::mk_attr(attr::mk_list_item("link", *match.metas)) attr::mk_attr(attr::mk_list_item("link", *match.metas))
]; ];
loader::note_linkage_attrs(sess, attrs); loader::note_linkage_attrs(diag, attrs);
} }
} }
warn_if_multiple_versions(sess, non_matches); warn_if_multiple_versions(diag, non_matches);
} }
} }
type env = @{sess: session::session, type env = @{diag: span_handler,
filesearch: filesearch,
cstore: cstore::cstore,
os: loader::os,
static: bool,
mut crate_cache: [cache_entry], mut crate_cache: [cache_entry],
mut next_crate_num: ast::crate_num}; mut next_crate_num: ast::crate_num};
@ -96,7 +107,7 @@ fn visit_view_item(e: env, i: @ast::view_item) {
ast::view_item_use(ident, meta_items, id) { ast::view_item_use(ident, meta_items, id) {
#debug("resolving use stmt. ident: %?, meta: %?", ident, meta_items); #debug("resolving use stmt. ident: %?, meta: %?", ident, meta_items);
let cnum = resolve_crate(e, ident, meta_items, "", i.span); let cnum = resolve_crate(e, ident, meta_items, "", i.span);
cstore::add_use_stmt_cnum(e.sess.cstore, id, cnum); cstore::add_use_stmt_cnum(e.cstore, id, cnum);
} }
_ { } _ { }
} }
@ -110,15 +121,15 @@ fn visit_item(e: env, i: @ast::item) {
if abi != ast::native_abi_cdecl && if abi != ast::native_abi_cdecl &&
abi != ast::native_abi_stdcall { ret; } abi != ast::native_abi_stdcall { ret; }
} }
either::left(msg) { e.sess.span_fatal(i.span, msg); } either::left(msg) { e.diag.span_fatal(i.span, msg); }
} }
let cstore = e.sess.cstore; let cstore = e.cstore;
let native_name = let native_name =
alt attr::first_attr_value_str_by_name(i.attrs, "link_name") { alt attr::first_attr_value_str_by_name(i.attrs, "link_name") {
some(nn) { some(nn) {
if nn == "" { if nn == "" {
e.sess.span_fatal( e.diag.span_fatal(
i.span, i.span,
"empty #[link_name] not allowed; use #[nolink]."); "empty #[link_name] not allowed; use #[nolink].");
} }
@ -132,7 +143,7 @@ fn visit_item(e: env, i: @ast::item) {
} }
let link_args = attr::find_attrs_by_name(i.attrs, "link_args"); let link_args = attr::find_attrs_by_name(i.attrs, "link_args");
if vec::len(link_args) > 0u && already_added { if vec::len(link_args) > 0u && already_added {
e.sess.span_fatal(i.span, "library '" + native_name + e.diag.span_fatal(i.span, "library '" + native_name +
"' already added: can't specify link_args."); "' already added: can't specify link_args.");
} }
for link_args.each {|a| for link_args.each {|a|
@ -180,13 +191,14 @@ fn resolve_crate(e: env, ident: ast::ident, metas: [@ast::meta_item],
alt existing_match(e, metas, hash) { alt existing_match(e, metas, hash) {
none { none {
let load_ctxt: loader::ctxt = { let load_ctxt: loader::ctxt = {
sess: e.sess, diag: e.diag,
filesearch: e.filesearch,
span: span, span: span,
ident: ident, ident: ident,
metas: metas, metas: metas,
hash: hash, hash: hash,
os: session::sess_os_to_meta_os(e.sess.targ_cfg.os), os: e.os,
static: e.sess.opts.static static: e.static
}; };
let cinfo = loader::load_library_crate(load_ctxt); let cinfo = loader::load_library_crate(load_ctxt);
@ -214,7 +226,7 @@ fn resolve_crate(e: env, ident: ast::ident, metas: [@ast::meta_item],
let cmeta = @{name: cname, data: cdata, let cmeta = @{name: cname, data: cdata,
cnum_map: cnum_map, cnum: cnum}; cnum_map: cnum_map, cnum: cnum};
let cstore = e.sess.cstore; let cstore = e.cstore;
cstore::set_crate_data(cstore, cnum, cmeta); cstore::set_crate_data(cstore, cnum, cmeta);
cstore::add_used_crate_file(cstore, cfilename); cstore::add_used_crate_file(cstore, cfilename);
ret cnum; ret cnum;

View File

@ -6,8 +6,8 @@ import syntax::ast_util;
import syntax::ast_map; import syntax::ast_map;
import middle::ty; import middle::ty;
import option::{some, none}; import option::{some, none};
import driver::session; import syntax::diagnostic::span_handler;
import driver::session::expect; import syntax::diagnostic::expect;
import common::*; import common::*;
import std::map::hashmap; import std::map::hashmap;
@ -82,7 +82,7 @@ fn resolve_path(cstore: cstore::cstore, cnum: ast::crate_num,
} }
fn get_item_path(tcx: ty::ctxt, def: ast::def_id) -> ast_map::path { fn get_item_path(tcx: ty::ctxt, def: ast::def_id) -> ast_map::path {
let cstore = tcx.sess.cstore; let cstore = tcx.cstore;
let cdata = cstore::get_crate_data(cstore, def.crate); let cdata = cstore::get_crate_data(cstore, def.crate);
let path = decoder::get_item_path(cdata, def.node); let path = decoder::get_item_path(cdata, def.node);
@ -103,14 +103,14 @@ enum found_ast {
fn maybe_get_item_ast(tcx: ty::ctxt, def: ast::def_id, fn maybe_get_item_ast(tcx: ty::ctxt, def: ast::def_id,
decode_inlined_item: decoder::decode_inlined_item) decode_inlined_item: decoder::decode_inlined_item)
-> found_ast { -> found_ast {
let cstore = tcx.sess.cstore; let cstore = tcx.cstore;
let cdata = cstore::get_crate_data(cstore, def.crate); let cdata = cstore::get_crate_data(cstore, def.crate);
decoder::maybe_get_item_ast(cdata, tcx, def.node, decoder::maybe_get_item_ast(cdata, tcx, def.node,
decode_inlined_item) decode_inlined_item)
} }
fn get_enum_variants(tcx: ty::ctxt, def: ast::def_id) -> [ty::variant_info] { fn get_enum_variants(tcx: ty::ctxt, def: ast::def_id) -> [ty::variant_info] {
let cstore = tcx.sess.cstore; let cstore = tcx.cstore;
let cdata = cstore::get_crate_data(cstore, def.crate); let cdata = cstore::get_crate_data(cstore, def.crate);
ret decoder::get_enum_variants(cdata, def.node, tcx) ret decoder::get_enum_variants(cdata, def.node, tcx)
} }
@ -125,35 +125,35 @@ fn get_impls_for_mod(cstore: cstore::cstore, def: ast::def_id,
} }
fn get_iface_methods(tcx: ty::ctxt, def: ast::def_id) -> @[ty::method] { fn get_iface_methods(tcx: ty::ctxt, def: ast::def_id) -> @[ty::method] {
let cstore = tcx.sess.cstore; let cstore = tcx.cstore;
let cdata = cstore::get_crate_data(cstore, def.crate); let cdata = cstore::get_crate_data(cstore, def.crate);
decoder::get_iface_methods(cdata, def.node, tcx) decoder::get_iface_methods(cdata, def.node, tcx)
} }
fn get_class_fields(tcx: ty::ctxt, def: ast::def_id) -> [ty::field_ty] { fn get_class_fields(tcx: ty::ctxt, def: ast::def_id) -> [ty::field_ty] {
let cstore = tcx.sess.cstore; let cstore = tcx.cstore;
let cdata = cstore::get_crate_data(cstore, def.crate); let cdata = cstore::get_crate_data(cstore, def.crate);
decoder::get_class_fields(cdata, def.node) decoder::get_class_fields(cdata, def.node)
} }
fn get_type(tcx: ty::ctxt, def: ast::def_id) -> ty::ty_param_bounds_and_ty { fn get_type(tcx: ty::ctxt, def: ast::def_id) -> ty::ty_param_bounds_and_ty {
let cstore = tcx.sess.cstore; let cstore = tcx.cstore;
let cdata = cstore::get_crate_data(cstore, def.crate); let cdata = cstore::get_crate_data(cstore, def.crate);
decoder::get_type(cdata, def.node, tcx) decoder::get_type(cdata, def.node, tcx)
} }
fn get_field_type(tcx: ty::ctxt, class_id: ast::def_id, fn get_field_type(tcx: ty::ctxt, class_id: ast::def_id,
def: ast::def_id) -> ty::ty_param_bounds_and_ty { def: ast::def_id) -> ty::ty_param_bounds_and_ty {
let cstore = tcx.sess.cstore; let cstore = tcx.cstore;
let cdata = cstore::get_crate_data(cstore, class_id.crate); let cdata = cstore::get_crate_data(cstore, class_id.crate);
let all_items = ebml::get_doc(ebml::doc(cdata.data), tag_items); let all_items = ebml::get_doc(ebml::doc(cdata.data), tag_items);
#debug("Looking up %?", class_id); #debug("Looking up %?", class_id);
let class_doc = expect(tcx.sess, let class_doc = expect(tcx.diag,
decoder::maybe_find_item(class_id.node, all_items), decoder::maybe_find_item(class_id.node, all_items),
{|| #fmt("get_field_type: class ID %? not found", {|| #fmt("get_field_type: class ID %? not found",
class_id)}); class_id)});
#debug("looking up %? : %?", def, class_doc); #debug("looking up %? : %?", def, class_doc);
let the_field = expect(tcx.sess, let the_field = expect(tcx.diag,
decoder::maybe_find_item(def.node, class_doc), decoder::maybe_find_item(def.node, class_doc),
{|| #fmt("get_field_type: in class %?, field ID %? not found", {|| #fmt("get_field_type: in class %?, field ID %? not found",
class_id, def)}); class_id, def)});
@ -165,7 +165,7 @@ fn get_field_type(tcx: ty::ctxt, class_id: ast::def_id,
// Given a def_id for an impl or class, return the iface it implements, // Given a def_id for an impl or class, return the iface it implements,
// or none if it's not for an impl or for a class that implements ifaces // or none if it's not for an impl or for a class that implements ifaces
fn get_impl_iface(tcx: ty::ctxt, def: ast::def_id) -> option<ty::t> { fn get_impl_iface(tcx: ty::ctxt, def: ast::def_id) -> option<ty::t> {
let cstore = tcx.sess.cstore; let cstore = tcx.cstore;
let cdata = cstore::get_crate_data(cstore, def.crate); let cdata = cstore::get_crate_data(cstore, def.crate);
decoder::get_impl_iface(cdata, def.node, tcx) decoder::get_impl_iface(cdata, def.node, tcx)
} }

View File

@ -4,13 +4,13 @@ Finds crate binaries and loads their metadata
"]; "];
import driver::session; import syntax::diagnostic::span_handler;
import session::session;
import syntax::{ast, attr}; import syntax::{ast, attr};
import syntax::print::pprust; import syntax::print::pprust;
import syntax::codemap::span; import syntax::codemap::span;
import lib::llvm::{False, llvm, mk_object_file, mk_section_iter}; import lib::llvm::{False, llvm, mk_object_file, mk_section_iter};
import util::{filesearch}; import util::filesearch;
import filesearch::filesearch;
import io::writer_util; import io::writer_util;
export os; export os;
@ -30,7 +30,8 @@ enum os {
} }
type ctxt = { type ctxt = {
sess: session, diag: span_handler,
filesearch: filesearch,
span: span, span: span,
ident: ast::ident, ident: ast::ident,
metas: [@ast::meta_item], metas: [@ast::meta_item],
@ -43,15 +44,15 @@ fn load_library_crate(cx: ctxt) -> {ident: str, data: @[u8]} {
alt find_library_crate(cx) { alt find_library_crate(cx) {
some(t) { ret t; } some(t) { ret t; }
none { none {
cx.sess.span_fatal( cx.diag.span_fatal(
cx.span, #fmt["can't find crate for '%s'", cx.ident]); cx.span, #fmt["can't find crate for '%s'", cx.ident]);
} }
} }
} }
fn find_library_crate(cx: ctxt) -> option<{ident: str, data: @[u8]}> { fn find_library_crate(cx: ctxt) -> option<{ident: str, data: @[u8]}> {
attr::require_unique_names(cx.sess.diagnostic(), cx.metas); attr::require_unique_names(cx.diag, cx.metas);
find_library_crate_aux(cx, libname(cx), cx.sess.filesearch) find_library_crate_aux(cx, libname(cx), cx.filesearch)
} }
fn libname(cx: ctxt) -> {prefix: str, suffix: str} { fn libname(cx: ctxt) -> {prefix: str, suffix: str} {
@ -106,15 +107,15 @@ fn find_library_crate_aux(cx: ctxt,
} else if matches.len() == 1u { } else if matches.len() == 1u {
some(matches[0]) some(matches[0])
} else { } else {
cx.sess.span_err( cx.diag.span_err(
cx.span, #fmt("multiple matching crates for `%s`", crate_name)); cx.span, #fmt("multiple matching crates for `%s`", crate_name));
cx.sess.note("candidates:"); cx.diag.handler().note("candidates:");
for matches.each {|match| for matches.each {|match|
cx.sess.note(#fmt("path: %s", match.ident)); cx.diag.handler().note(#fmt("path: %s", match.ident));
let attrs = decoder::get_crate_attributes(match.data); let attrs = decoder::get_crate_attributes(match.data);
note_linkage_attrs(cx.sess, attrs); note_linkage_attrs(cx.diag, attrs);
} }
cx.sess.abort_if_errors(); cx.diag.handler().abort_if_errors();
none none
} }
} }
@ -134,9 +135,9 @@ fn crate_name_from_metas(metas: [@ast::meta_item]) -> str {
} }
} }
fn note_linkage_attrs(sess: session::session, attrs: [ast::attribute]) { fn note_linkage_attrs(diag: span_handler, attrs: [ast::attribute]) {
for attr::find_linkage_attrs(attrs).each {|attr| for attr::find_linkage_attrs(attrs).each {|attr|
sess.note(#fmt("meta: %s", pprust::attr_to_str(attr))); diag.handler().note(#fmt("meta: %s", pprust::attr_to_str(attr)));
} }
} }