rust/src/librustc/rustc.rs

393 lines
11 KiB
Rust
Raw Normal View History

2013-02-28 07:15:32 -06:00
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[link(name = "rustc",
2013-07-08 12:25:45 -05:00
vers = "0.8-pre",
uuid = "0ce89b41-2f92-459e-bbc1-8f5fe32f16cf",
url = "https://github.com/mozilla/rust/tree/master/src/rustc")];
#[comment = "The Rust compiler"];
#[license = "MIT/ASL2"];
#[crate_type = "lib"];
2011-05-05 21:44:00 -05:00
// Rustc tasks always run on a fixed_stack_segment, so code in this
// module can call C functions (in particular, LLVM functions) with
// impunity.
#[allow(cstack)];
extern mod extra;
extern mod syntax;
use driver::driver::{host_triple, optgroups, early_error};
use driver::driver::{str_input, file_input, build_session_options};
use driver::driver::{build_session, build_configuration, parse_pretty};
use driver::driver::{PpMode, pretty_print_input, list_metadata};
use driver::driver::{compile_input};
use driver::session;
use middle::lint;
use std::io;
use std::num;
use std::os;
use std::result;
use std::str;
use std::task;
use std::vec;
use extra::getopts::{groups, opt_present};
use extra::getopts;
use syntax::codemap;
use syntax::diagnostic;
pub mod middle {
2013-06-12 18:18:58 -05:00
pub mod trans;
pub mod ty;
pub mod subst;
pub mod resolve;
2012-11-28 14:33:00 -06:00
pub mod typeck;
pub mod check_loop;
pub mod check_match;
pub mod check_const;
pub mod lint;
pub mod borrowck;
2013-04-17 17:05:17 -05:00
pub mod dataflow;
pub mod mem_categorization;
pub mod liveness;
pub mod kind;
pub mod freevars;
pub mod pat_util;
pub mod region;
pub mod const_eval;
pub mod astencode;
pub mod lang_items;
pub mod privacy;
pub mod moves;
pub mod entry;
pub mod effect;
pub mod reachable;
pub mod graph;
pub mod cfg;
pub mod stack_check;
2011-04-19 18:40:46 -05:00
}
pub mod front {
pub mod config;
pub mod test;
pub mod std_inject;
pub mod assign_node_ids;
2010-06-23 23:03:09 -05:00
}
pub mod back {
pub mod link;
pub mod abi;
pub mod upcall;
pub mod arm;
2013-01-29 08:28:08 -06:00
pub mod mips;
pub mod x86;
pub mod x86_64;
pub mod rpath;
pub mod target_strs;
}
pub mod metadata;
pub mod driver;
2010-06-23 23:03:09 -05:00
pub mod util {
pub mod common;
pub mod ppaux;
2010-08-18 13:34:47 -05:00
}
pub mod lib {
pub mod llvm;
2010-07-12 19:47:40 -05:00
}
// A curious inner module that allows ::std::foo to be available in here for
// macros.
/*
mod std {
2013-07-02 14:47:32 -05:00
pub use std::clone;
pub use std::cmp;
pub use std::os;
pub use std::str;
pub use std::sys;
pub use std::to_bytes;
pub use std::unstable;
pub use extra::serialize;
}
*/
pub fn version(argv0: &str) {
let vers = match option_env!("CFG_VERSION") {
Some(vers) => vers,
None => "unknown version"
};
printfln!("%s %s", argv0, vers);
printfln!("host: %s", host_triple());
}
pub fn usage(argv0: &str) {
2012-11-28 14:33:00 -06:00
let message = fmt!("Usage: %s [OPTIONS] INPUT", argv0);
printfln!("%s\
2013-06-21 18:08:37 -05:00
Additional help:
-W help Print 'lint' options and default settings
-Z help Print internal options for debugging rustc\n",
groups::usage(message, optgroups()));
2012-11-28 14:33:00 -06:00
}
pub fn describe_warnings() {
use extra::sort::Sort;
2013-08-05 22:04:58 -05:00
println("
2012-11-28 14:33:00 -06:00
Available lint options:
-W <foo> Warn about <foo>
-A <foo> Allow <foo>
-D <foo> Deny <foo>
-F <foo> Forbid <foo> (deny, and deny all overrides)
");
2012-11-28 14:33:00 -06:00
let lint_dict = lint::get_lint_dict();
let mut lint_dict = lint_dict.move_iter()
.map(|(k, v)| (v, k))
.collect::<~[(lint::LintSpec, &'static str)]>();
lint_dict.qsort();
2012-11-28 14:33:00 -06:00
let mut max_key = 0;
for &(_, name) in lint_dict.iter() {
max_key = num::max(name.len(), max_key);
}
2012-11-28 14:33:00 -06:00
fn padded(max: uint, s: &str) -> ~str {
str::from_utf8(vec::from_elem(max - s.len(), ' ' as u8)) + s
2012-11-28 14:33:00 -06:00
}
2013-08-05 22:04:58 -05:00
println("\nAvailable lint checks:\n");
printfln!(" %s %7.7s %s",
padded(max_key, "name"), "default", "meaning");
printfln!(" %s %7.7s %s\n",
padded(max_key, "----"), "-------", "-------");
for (spec, name) in lint_dict.move_iter() {
let name = name.replace("_", "-");
printfln!(" %s %7.7s %s",
padded(max_key, name),
lint::level_to_str(spec.default),
spec.desc);
2012-11-28 14:33:00 -06:00
}
io::println("");
2012-11-28 14:33:00 -06:00
}
pub fn describe_debug_flags() {
2013-08-05 22:04:58 -05:00
println("\nAvailable debug options:\n");
let r = session::debugging_opts_map();
for tuple in r.iter() {
2013-07-02 14:47:32 -05:00
match *tuple {
(ref name, ref desc, _) => {
printfln!(" -Z %-20s -- %s", *name, *desc);
2013-07-02 14:47:32 -05:00
}
}
2012-11-28 14:33:00 -06:00
}
}
pub fn run_compiler(args: &[~str], demitter: diagnostic::Emitter) {
2012-11-28 14:33:00 -06:00
// Don't display log spew by default. Can override with RUST_LOG.
::std::logging::console_off();
2012-11-28 14:33:00 -06:00
let mut args = args.to_owned();
let binary = args.shift().to_managed();
2012-11-28 14:33:00 -06:00
if args.is_empty() { usage(binary); return; }
2012-11-28 14:33:00 -06:00
let matches =
&match getopts::groups::getopts(args, optgroups()) {
Ok(m) => m,
Err(f) => {
early_error(demitter, getopts::fail_str(f));
2012-11-28 14:33:00 -06:00
}
};
if opt_present(matches, "h") || opt_present(matches, "help") {
usage(binary);
2012-11-28 14:33:00 -06:00
return;
}
2013-05-24 14:32:30 -05:00
// Display the available lint options if "-W help" or only "-W" is given.
let lint_flags = vec::append(getopts::opt_strs(matches, "W"),
getopts::opt_strs(matches, "warn"));
2013-05-24 14:32:30 -05:00
2013-07-04 21:13:26 -05:00
let show_lint_options = lint_flags.iter().any(|x| x == &~"help") ||
2013-05-24 14:32:30 -05:00
(opt_present(matches, "W") && lint_flags.is_empty());
if show_lint_options {
2012-11-28 14:33:00 -06:00
describe_warnings();
return;
}
let r = getopts::opt_strs(matches, "Z");
2013-07-04 21:13:26 -05:00
if r.iter().any(|x| x == &~"help") {
2012-11-28 14:33:00 -06:00
describe_debug_flags();
return;
}
if getopts::opt_maybe_str(matches, "passes") == Some(~"list") {
2013-08-22 22:58:42 -05:00
unsafe { lib::llvm::llvm::LLVMRustPrintPasses(); }
return;
}
if opt_present(matches, "v") || opt_present(matches, "version") {
version(binary);
2012-11-28 14:33:00 -06:00
return;
}
let input = match matches.free.len() {
2012-11-28 14:33:00 -06:00
0u => early_error(demitter, ~"no input filename given"),
1u => {
let ifile = matches.free[0].as_slice();
if "-" == ifile {
let src = str::from_utf8(io::stdin().read_whole_stream());
str_input(src.to_managed())
2012-11-28 14:33:00 -06:00
} else {
file_input(Path(ifile))
}
}
_ => early_error(demitter, ~"multiple input filenames provided")
};
2013-04-16 18:10:21 -05:00
let sopts = build_session_options(binary, matches, demitter);
2012-11-28 14:33:00 -06:00
let sess = build_session(sopts, demitter);
let odir = getopts::opt_maybe_str(matches, "out-dir").map_move(|o| Path(o));
let ofile = getopts::opt_maybe_str(matches, "o").map_move(|o| Path(o));
let cfg = build_configuration(sess);
let pretty = do getopts::opt_default(matches, "pretty", "normal").map_move |a| {
parse_pretty(sess, a)
};
2012-11-28 14:33:00 -06:00
match pretty {
Some::<PpMode>(ppm) => {
pretty_print_input(sess, cfg, &input, ppm);
2012-11-28 14:33:00 -06:00
return;
}
None::<PpMode> => {/* continue */ }
2012-11-28 14:33:00 -06:00
}
let ls = opt_present(matches, "ls");
2012-11-28 14:33:00 -06:00
if ls {
match input {
file_input(ref ifile) => {
list_metadata(sess, &(*ifile), io::stdout());
2012-11-28 14:33:00 -06:00
}
str_input(_) => {
early_error(demitter, ~"can not list metadata for stdin");
}
}
return;
}
compile_input(sess, cfg, &input, &odir, &ofile);
2012-11-28 14:33:00 -06:00
}
#[deriving(Eq)]
pub enum monitor_msg {
2012-11-28 14:33:00 -06:00
fatal,
done,
}
/*
This is a sanity check that any failure of the compiler is performed
through the diagnostic module and reported properly - we shouldn't be calling
plain-old-fail on any execution path that might be taken. Since we have
console logging off by default, hitting a plain fail statement would make the
compiler silently exit, which would be terrible.
This method wraps the compiler in a subtask and injects a function into the
diagnostic emitter which records when we hit a fatal error. If the task
fails without recording a fatal error then we've encountered a compiler
bug and need to present an error.
*/
pub fn monitor(f: ~fn(diagnostic::Emitter)) {
use std::comm::*;
// XXX: This is a hack for newsched since it doesn't support split stacks.
// rustc needs a lot of stack!
2013-08-09 18:37:39 -05:00
static STACK_SIZE: uint = 6000000;
2013-01-30 03:52:01 -06:00
let (p, ch) = stream();
let ch = SharedChan::new(ch);
2013-01-30 03:52:01 -06:00
let ch_capture = ch.clone();
let mut task_builder = task::task();
task_builder.supervised();
// XXX: Hacks on hacks. If the env is trying to override the stack size
// then *don't* set it explicitly.
if os::getenv("RUST_MIN_STACK").is_none() {
task_builder.opts.stack_size = Some(STACK_SIZE);
}
match do task_builder.try {
2013-01-30 03:52:01 -06:00
let ch = ch_capture.clone();
let ch_capture = ch.clone();
2012-11-28 14:33:00 -06:00
// The 'diagnostics emitter'. Every error, warning, etc. should
// go through this function.
let demitter: @fn(Option<(@codemap::CodeMap, codemap::Span)>,
&str,
diagnostic::level) =
|cmsp, msg, lvl| {
2012-11-28 14:33:00 -06:00
if lvl == diagnostic::fatal {
2013-01-30 03:52:01 -06:00
ch_capture.send(fatal);
2012-11-28 14:33:00 -06:00
}
diagnostic::emit(cmsp, msg, lvl);
};
struct finally {
2013-01-30 03:52:01 -06:00
ch: SharedChan<monitor_msg>,
}
impl Drop for finally {
2013-06-20 20:06:13 -05:00
fn drop(&self) { self.ch.send(done); }
2012-11-28 14:33:00 -06:00
}
let _finally = finally { ch: ch };
2013-07-13 01:26:23 -05:00
f(demitter);
// Due reasons explain in #7732, if there was a jit execution context it
// must be consumed and passed along to our parent task.
back::link::jit::consume_engine()
2012-11-28 14:33:00 -06:00
} {
result::Ok(_) => { /* fallthrough */ }
result::Err(_) => {
// Task failed without emitting a fatal diagnostic
2013-01-30 03:52:01 -06:00
if p.recv() == done {
2012-11-28 14:33:00 -06:00
diagnostic::emit(
None,
diagnostic::ice_msg("unexpected failure"),
2012-11-28 14:33:00 -06:00
diagnostic::error);
let xs = [
2012-11-28 14:33:00 -06:00
~"the compiler hit an unexpected failure path. \
this is a bug",
~"try running with RUST_LOG=rustc=1 \
2012-11-28 14:33:00 -06:00
to get further details and report the results \
to github.com/mozilla/rust/issues"
];
for note in xs.iter() {
2012-11-28 14:33:00 -06:00
diagnostic::emit(None, *note, diagnostic::note)
}
}
// Fail so the process returns a failure code
fail!();
2012-11-28 14:33:00 -06:00
}
}
}
pub fn main() {
let args = os::args();
main_args(args);
}
pub fn main_args(args: &[~str]) {
let owned_args = args.to_owned();
2013-02-15 03:14:34 -06:00
do monitor |demitter| {
run_compiler(owned_args, demitter);
2012-11-28 14:33:00 -06:00
}
}