rust/src/librustc/rustc.rc

388 lines
11 KiB
Plaintext
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.
2010-06-23 23:03:09 -05:00
#[link(name = "rustc",
vers = "0.6",
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
#[legacy_modes];
#[allow(non_implicitly_copyable_typarams)];
#[allow(non_camel_case_types)];
#[allow(deprecated_mode)];
#[warn(deprecated_pattern)];
#[allow(deprecated_self)];
#[no_core];
extern mod core(vers = "0.6");
use core::*;
extern mod std(vers = "0.6");
extern mod syntax(vers = "0.6");
2012-04-05 19:30:26 -05:00
/*
Alternate names for some modules.
I am using this to help extract metadata into its own crate. In metadata.rs
it redefines all these modules in order to gate access from metadata to the
rest of the compiler, then uses these to access the original implementation.
*/
2012-09-05 13:46:25 -05:00
use util_ = util;
use lib_ = lib;
use driver_ = driver;
use middle_ = middle;
use back_ = back;
pub mod middle {
pub mod trans {
pub mod macros;
pub mod inline;
pub mod monomorphize;
pub mod controlflow;
pub mod glue;
pub mod datum;
pub mod callee;
pub mod expr;
pub mod common;
pub mod consts;
pub mod type_of;
pub mod build;
pub mod base;
pub mod _match;
pub mod uniq;
pub mod closure;
pub mod tvec;
pub mod meth;
pub mod cabi;
pub mod cabi_x86_64;
2013-02-26 05:24:15 -06:00
pub mod cabi_arm;
pub mod foreign;
pub mod reflect;
pub mod shape;
pub mod debuginfo;
pub mod type_use;
pub mod reachable;
pub mod machine;
}
pub mod ty;
pub mod resolve;
2012-12-19 16:17:53 -06:00
#[path = "typeck/mod.rs"]
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;
2012-12-19 16:17:53 -06:00
#[path = "borrowck/mod.rs"]
pub mod borrowck;
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;
2011-04-19 18:40:46 -05:00
}
pub mod front {
pub mod config;
pub mod test;
pub mod core_inject;
pub mod intrinsic_inject;
2010-06-23 23:03:09 -05:00
}
pub mod back {
pub mod link;
pub mod abi;
pub mod upcall;
pub mod arm;
pub mod x86;
pub mod x86_64;
pub mod rpath;
pub mod target_strs;
}
2012-11-28 18:20:41 -06:00
#[path = "metadata/mod.rs"]
pub mod metadata;
2012-11-28 18:20:41 -06:00
#[path = "driver/mod.rs"]
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
}
2012-11-28 14:33:00 -06:00
use result::{Ok, Err};
use io::ReaderUtil;
use std::getopts;
use std::oldmap::HashMap;
2012-11-28 14:33:00 -06:00
use getopts::{opt_present};
use getopts::groups;
use syntax::codemap;
use syntax::diagnostic;
use driver::driver::{host_triple, optgroups, early_error,
str_input, file_input, build_session_options,
build_session, build_configuration, parse_pretty,
pp_mode, pretty_print_input, list_metadata,
compile_input};
use driver::session;
use middle::lint;
pub fn version(argv0: &str) {
2012-11-28 14:33:00 -06:00
let mut vers = ~"unknown version";
let env_vers = env!("CFG_VERSION");
if env_vers.len() != 0 { vers = env_vers; }
io::println(fmt!("%s %s", argv0, vers));
io::println(fmt!("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);
io::println(groups::usage(message, optgroups()) +
~"Additional help:
-W help Print 'lint' options and default settings
-Z help Print internal options for debugging rustc
");
}
pub fn describe_warnings() {
2012-11-28 14:33:00 -06:00
io::println(fmt!("
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)
"));
let lint_dict = lint::get_lint_dict();
let mut max_key = 0;
for lint_dict.each_key |&k| { max_key = uint::max(k.len(), max_key); }
2012-11-28 14:33:00 -06:00
fn padded(max: uint, s: &str) -> ~str {
str::from_bytes(vec::from_elem(max - s.len(), ' ' as u8)) + s
}
io::println(fmt!("\nAvailable lint checks:\n"));
io::println(fmt!(" %s %7.7s %s",
padded(max_key, ~"name"), ~"default", ~"meaning"));
io::println(fmt!(" %s %7.7s %s\n",
padded(max_key, ~"----"), ~"-------", ~"-------"));
for lint_dict.each |&k, &v| {
let k = str::replace(*k, ~"_", ~"-");
2012-11-28 14:33:00 -06:00
io::println(fmt!(" %s %7.7s %s",
padded(max_key, k),
match v.default {
lint::allow => ~"allow",
lint::warn => ~"warn",
lint::deny => ~"deny",
lint::forbid => ~"forbid"
},
v.desc));
}
io::println(~"");
}
pub fn describe_debug_flags() {
2012-11-28 14:33:00 -06:00
io::println(fmt!("\nAvailable debug options:\n"));
for session::debugging_opts_map().each |pair| {
let (name, desc, _) = /*bad*/copy *pair;
2012-11-28 14:33:00 -06:00
io::println(fmt!(" -Z %-20s -- %s", name, desc));
}
}
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.
logging::console_off();
let mut args = /*bad*/copy *args;
2012-11-28 14:33:00 -06:00
let binary = args.shift();
if args.is_empty() { usage(binary); return; }
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);
return;
}
let lint_flags = vec::append(getopts::opt_strs(matches, ~"W"),
getopts::opt_strs(matches, ~"warn"));
if lint_flags.contains(&~"help") {
describe_warnings();
return;
}
if getopts::opt_strs(matches, ~"Z").contains(&~"help") {
describe_debug_flags();
return;
}
if opt_present(matches, ~"v") || opt_present(matches, ~"version") {
version(binary);
return;
}
let input = match vec::len(matches.free) {
0u => early_error(demitter, ~"no input filename given"),
1u => {
let ifile = /*bad*/copy matches.free[0];
2012-11-28 14:33:00 -06:00
if ifile == ~"-" {
let src = str::from_bytes(io::stdin().read_whole_stream());
str_input(src)
} else {
file_input(Path(ifile))
}
}
_ => early_error(demitter, ~"multiple input filenames provided")
};
// XXX: Bad copy.
let sopts = build_session_options(copy 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");
let odir = odir.map(|o| Path(*o));
let ofile = getopts::opt_maybe_str(matches, ~"o");
let ofile = ofile.map(|o| Path(*o));
let cfg = build_configuration(sess, binary, input);
let pretty =
option::map(&getopts::opt_default(matches, ~"pretty",
~"normal"),
|a| parse_pretty(sess, *a) );
match pretty {
Some::<pp_mode>(ppm) => {
pretty_print_input(sess, cfg, input, ppm);
return;
}
None::<pp_mode> => {/* continue */ }
}
let ls = opt_present(matches, ~"ls");
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);
}
#[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 core::cell::Cell;
2013-02-02 05:10:12 -06:00
use core::comm::*;
2013-01-30 03:52:01 -06:00
let (p, ch) = stream();
let ch = SharedChan(ch);
let ch_capture = ch.clone();
2013-02-15 03:14:34 -06:00
match do task::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@(cmsp: Option<(@codemap::CodeMap, codemap::span)>,
msg: &str, lvl: diagnostic::level) {
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 {
fn finalize(&self) { self.ch.send(done); }
2012-11-28 14:33:00 -06:00
}
let _finally = finally { ch: ch };
f(demitter)
} {
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"),
diagnostic::error);
for [
~"the compiler hit an unexpected failure path. \
this is a bug",
~"try running with RUST_LOG=rustc=1,::rt::backtrace \
to get further details and report the results \
to github.com/mozilla/rust/issues"
].each |note| {
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();
2013-02-15 03:14:34 -06:00
do monitor |demitter| {
2012-11-28 14:33:00 -06:00
run_compiler(&args, demitter);
}
}
2010-06-23 23:03:09 -05:00
// Local Variables:
// fill-column: 78;
// indent-tabs-mode: nil
2010-08-18 13:34:22 -05:00
// c-basic-offset: 4
2010-06-23 23:03:09 -05:00
// buffer-file-coding-system: utf-8-unix
// End: