2011-06-15 13:19:50 -05:00
|
|
|
|
2011-09-12 18:13:28 -05:00
|
|
|
import syntax::{ast, codemap};
|
2011-07-13 19:26:06 -05:00
|
|
|
import syntax::ast::node_id;
|
2011-07-05 04:48:19 -05:00
|
|
|
import codemap::span;
|
2011-12-07 14:06:12 -06:00
|
|
|
import syntax::ast::{int_ty, uint_ty, float_ty};
|
2011-12-13 18:25:51 -06:00
|
|
|
import option;
|
|
|
|
import option::{some, none};
|
2011-07-06 17:22:23 -05:00
|
|
|
import syntax::parse::parser::parse_sess;
|
2011-10-03 14:46:22 -05:00
|
|
|
import util::filesearch;
|
2011-10-12 14:29:08 -05:00
|
|
|
import back::target_strs;
|
2010-09-01 15:24:14 -05:00
|
|
|
|
2011-12-30 02:18:55 -06:00
|
|
|
tag os { os_win32; os_macos; os_linux; os_freebsd; }
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-10-13 14:23:50 -05:00
|
|
|
tag arch { arch_x86; arch_x86_64; arch_arm; }
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-12-08 22:08:00 -06:00
|
|
|
tag crate_type { bin_crate; lib_crate; unknown_crate; }
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
type config =
|
2011-07-27 07:19:39 -05:00
|
|
|
{os: os,
|
|
|
|
arch: arch,
|
2011-10-12 14:29:08 -05:00
|
|
|
target_strs: target_strs::t,
|
2011-12-07 14:06:12 -06:00
|
|
|
int_type: int_ty,
|
|
|
|
uint_type: uint_ty,
|
|
|
|
float_type: float_ty};
|
2011-06-15 13:19:50 -05:00
|
|
|
|
|
|
|
type options =
|
2011-07-27 07:19:39 -05:00
|
|
|
// The crate config requested for the session, which may be combined
|
|
|
|
// with additional crate configurations during the compile process
|
2011-12-08 22:08:00 -06:00
|
|
|
{crate_type: crate_type,
|
2011-07-27 07:19:39 -05:00
|
|
|
static: bool,
|
2011-12-06 16:39:58 -06:00
|
|
|
libcore: bool,
|
2011-07-27 07:19:39 -05:00
|
|
|
optimize: uint,
|
|
|
|
debuginfo: bool,
|
2011-12-14 14:47:05 -06:00
|
|
|
extra_debuginfo: bool,
|
2011-07-27 07:19:39 -05:00
|
|
|
verify: bool,
|
|
|
|
save_temps: bool,
|
|
|
|
stats: bool,
|
|
|
|
time_passes: bool,
|
|
|
|
time_llvm_passes: bool,
|
|
|
|
output_type: back::link::output_type,
|
2011-10-03 14:46:22 -05:00
|
|
|
addl_lib_search_paths: [str],
|
|
|
|
maybe_sysroot: option::t<str>,
|
2011-09-21 10:46:18 -05:00
|
|
|
target_triple: str,
|
2011-07-27 07:19:39 -05:00
|
|
|
cfg: ast::crate_cfg,
|
|
|
|
test: bool,
|
2011-07-30 20:17:35 -05:00
|
|
|
parse_only: bool,
|
2011-08-11 00:12:42 -05:00
|
|
|
no_trans: bool,
|
2011-09-30 20:20:28 -05:00
|
|
|
do_gc: bool,
|
2011-11-16 22:23:43 -06:00
|
|
|
no_asm_comments: bool,
|
|
|
|
warn_unused_imports: bool};
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
type crate_metadata = {name: str, data: [u8]};
|
2011-07-27 07:19:39 -05:00
|
|
|
|
|
|
|
obj session(targ_cfg: @config,
|
|
|
|
opts: @options,
|
|
|
|
cstore: metadata::cstore::cstore,
|
|
|
|
parse_sess: parse_sess,
|
2011-03-25 12:42:57 -05:00
|
|
|
|
2011-07-13 19:26:06 -05:00
|
|
|
// For a library crate, this is always none
|
2011-08-12 09:15:18 -05:00
|
|
|
mutable main_fn: option::t<node_id>,
|
2011-10-03 14:46:22 -05:00
|
|
|
mutable err_count: uint,
|
2011-12-08 23:31:50 -06:00
|
|
|
filesearch: filesearch::filesearch,
|
2011-12-19 02:42:58 -06:00
|
|
|
mutable building_library: bool,
|
|
|
|
working_dir: str) {
|
2011-06-15 13:19:50 -05:00
|
|
|
fn get_targ_cfg() -> @config { ret targ_cfg; }
|
|
|
|
fn get_opts() -> @options { ret opts; }
|
2011-07-07 20:00:16 -05:00
|
|
|
fn get_cstore() -> metadata::cstore::cstore { cstore }
|
2011-09-12 04:27:30 -05:00
|
|
|
fn span_fatal(sp: span, msg: str) -> ! {
|
2011-04-28 12:50:54 -05:00
|
|
|
// FIXME: Use constants, but rustboot doesn't know how to export them.
|
2011-08-27 18:36:48 -05:00
|
|
|
codemap::emit_error(some(sp), msg, parse_sess.cm);
|
2010-09-01 15:24:14 -05:00
|
|
|
fail;
|
|
|
|
}
|
2011-09-12 04:27:30 -05:00
|
|
|
fn fatal(msg: str) -> ! {
|
2011-08-27 18:36:48 -05:00
|
|
|
codemap::emit_error(none, msg, parse_sess.cm);
|
2010-09-01 15:24:14 -05:00
|
|
|
fail;
|
|
|
|
}
|
2011-09-12 04:27:30 -05:00
|
|
|
fn span_err(sp: span, msg: str) {
|
2011-08-27 18:36:48 -05:00
|
|
|
codemap::emit_error(some(sp), msg, parse_sess.cm);
|
2011-06-19 00:55:53 -05:00
|
|
|
err_count += 1u;
|
|
|
|
}
|
2011-09-12 04:27:30 -05:00
|
|
|
fn err(msg: str) {
|
2011-08-27 18:36:48 -05:00
|
|
|
codemap::emit_error(none, msg, parse_sess.cm);
|
2011-06-19 00:55:53 -05:00
|
|
|
err_count += 1u;
|
|
|
|
}
|
2011-11-15 04:16:41 -06:00
|
|
|
fn has_errors() -> bool { err_count > 0u }
|
2011-06-19 00:55:53 -05:00
|
|
|
fn abort_if_errors() {
|
2011-09-02 17:34:58 -05:00
|
|
|
if err_count > 0u { self.fatal("aborting due to previous errors"); }
|
2011-06-19 00:55:53 -05:00
|
|
|
}
|
2011-09-12 04:27:30 -05:00
|
|
|
fn span_warn(sp: span, msg: str) {
|
2011-04-28 12:50:54 -05:00
|
|
|
// FIXME: Use constants, but rustboot doesn't know how to export them.
|
2011-08-27 18:36:48 -05:00
|
|
|
codemap::emit_warning(some(sp), msg, parse_sess.cm);
|
2011-08-27 16:57:47 -05:00
|
|
|
}
|
2011-09-12 04:27:30 -05:00
|
|
|
fn warn(msg: str) { codemap::emit_warning(none, msg, parse_sess.cm); }
|
|
|
|
fn span_note(sp: span, msg: str) {
|
2011-05-17 16:12:49 -05:00
|
|
|
// FIXME: Use constants, but rustboot doesn't know how to export them.
|
2011-08-27 18:36:48 -05:00
|
|
|
codemap::emit_note(some(sp), msg, parse_sess.cm);
|
2011-08-27 16:57:47 -05:00
|
|
|
}
|
2011-09-12 04:27:30 -05:00
|
|
|
fn note(msg: str) { codemap::emit_note(none, msg, parse_sess.cm); }
|
|
|
|
fn span_bug(sp: span, msg: str) -> ! {
|
2011-09-02 17:34:58 -05:00
|
|
|
self.span_fatal(sp, #fmt["internal compiler error %s", msg]);
|
2011-05-17 16:12:49 -05:00
|
|
|
}
|
2011-09-12 04:27:30 -05:00
|
|
|
fn bug(msg: str) -> ! {
|
2011-09-02 17:34:58 -05:00
|
|
|
self.fatal(#fmt["internal compiler error %s", msg]);
|
2010-11-22 18:27:00 -06:00
|
|
|
}
|
2011-09-12 04:27:30 -05:00
|
|
|
fn span_unimpl(sp: span, msg: str) -> ! {
|
2011-09-02 17:34:58 -05:00
|
|
|
self.span_bug(sp, "unimplemented " + msg);
|
2011-03-18 14:30:44 -05:00
|
|
|
}
|
2011-09-12 04:27:30 -05:00
|
|
|
fn unimpl(msg: str) -> ! { self.bug("unimplemented " + msg); }
|
2011-07-06 17:22:23 -05:00
|
|
|
fn get_codemap() -> codemap::codemap { ret parse_sess.cm; }
|
2011-07-27 07:19:39 -05:00
|
|
|
fn lookup_pos(pos: uint) -> codemap::loc {
|
2011-07-16 01:01:10 -05:00
|
|
|
ret codemap::lookup_char_pos(parse_sess.cm, pos);
|
2011-07-06 17:22:23 -05:00
|
|
|
}
|
|
|
|
fn get_parse_sess() -> parse_sess { ret parse_sess; }
|
|
|
|
fn next_node_id() -> ast::node_id {
|
|
|
|
ret syntax::parse::parser::next_node_id(parse_sess);
|
2011-04-08 11:44:20 -05:00
|
|
|
}
|
2011-09-02 17:34:58 -05:00
|
|
|
fn span_str(sp: span) -> str {
|
2011-08-27 18:36:48 -05:00
|
|
|
ret codemap::span_to_str(sp, self.get_codemap());
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn set_main_id(d: node_id) { main_fn = some(d); }
|
2011-08-12 09:15:18 -05:00
|
|
|
fn get_main_id() -> option::t<node_id> { main_fn }
|
2011-10-03 14:46:22 -05:00
|
|
|
fn filesearch() -> filesearch::filesearch { filesearch }
|
2011-12-08 23:31:50 -06:00
|
|
|
fn building_library() -> bool { building_library }
|
|
|
|
fn set_building_library(crate: @ast::crate) {
|
|
|
|
building_library = session::building_library(opts.crate_type, crate);
|
|
|
|
}
|
2011-12-19 02:42:58 -06:00
|
|
|
fn get_working_dir() -> str {
|
|
|
|
ret working_dir;
|
|
|
|
}
|
2010-09-01 15:24:14 -05:00
|
|
|
}
|
2011-12-08 23:05:44 -06:00
|
|
|
|
|
|
|
fn building_library(req_crate_type: crate_type, crate: @ast::crate) -> bool {
|
|
|
|
alt req_crate_type {
|
|
|
|
bin_crate. { false }
|
|
|
|
lib_crate. { true }
|
|
|
|
unknown_crate. {
|
|
|
|
alt front::attr::get_meta_item_value_str_by_name(
|
|
|
|
crate.node.attrs,
|
|
|
|
"crate_type") {
|
|
|
|
option::some("lib") { true }
|
|
|
|
_ { false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
import syntax::ast_util;
|
|
|
|
|
|
|
|
fn make_crate_type_attr(t: str) -> ast::attribute {
|
|
|
|
ast_util::respan(ast_util::dummy_sp(), {
|
|
|
|
style: ast::attr_outer,
|
|
|
|
value: ast_util::respan(ast_util::dummy_sp(),
|
|
|
|
ast::meta_name_value(
|
|
|
|
"crate_type",
|
|
|
|
ast_util::respan(ast_util::dummy_sp(),
|
|
|
|
ast::lit_str(t))))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_crate(with_bin: bool, with_lib: bool) -> @ast::crate {
|
|
|
|
let attrs = [];
|
|
|
|
if with_bin { attrs += [make_crate_type_attr("bin")]; }
|
|
|
|
if with_lib { attrs += [make_crate_type_attr("lib")]; }
|
|
|
|
@ast_util::respan(ast_util::dummy_sp(), {
|
|
|
|
directives: [],
|
|
|
|
module: {view_items: [], items: []},
|
|
|
|
attrs: attrs,
|
|
|
|
config: []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bin_crate_type_attr_results_in_bin_output() {
|
|
|
|
let crate = make_crate(true, false);
|
|
|
|
assert !building_library(unknown_crate, crate);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lib_crate_type_attr_results_in_lib_output() {
|
|
|
|
let crate = make_crate(false, true);
|
|
|
|
assert building_library(unknown_crate, crate);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bin_option_overrides_lib_crate_type() {
|
|
|
|
let crate = make_crate(false, true);
|
|
|
|
assert !building_library(bin_crate, crate);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lib_option_overrides_bin_crate_type() {
|
|
|
|
let crate = make_crate(true, false);
|
|
|
|
assert building_library(lib_crate, crate);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bin_crate_type_is_default() {
|
|
|
|
let crate = make_crate(false, false);
|
|
|
|
assert !building_library(unknown_crate, crate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-01 15:24:14 -05:00
|
|
|
// Local Variables:
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|