]> git.lizzy.rs Git - rust.git/blob - src/librustc/rustc.rc
Remove 'Local Variable' comments
[rust.git] / src / librustc / rustc.rc
1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #[link(name = "rustc",
12        vers = "0.7-pre",
13        uuid = "0ce89b41-2f92-459e-bbc1-8f5fe32f16cf",
14        url = "https://github.com/mozilla/rust/tree/master/src/rustc")];
15
16 #[comment = "The Rust compiler"];
17 #[license = "MIT/ASL2"];
18 #[crate_type = "lib"];
19
20 #[allow(non_implicitly_copyable_typarams)];
21 #[allow(non_camel_case_types)];
22 #[deny(deprecated_pattern)];
23 #[deny(deprecated_mode)];
24
25 extern mod std(vers = "0.7-pre");
26 extern mod syntax(vers = "0.7-pre");
27
28
29 use driver::driver::{host_triple, optgroups, early_error};
30 use driver::driver::{str_input, file_input, build_session_options};
31 use driver::driver::{build_session, build_configuration, parse_pretty};
32 use driver::driver::{pp_mode, pretty_print_input, list_metadata};
33 use driver::driver::{compile_input};
34 use driver::session;
35 use middle::lint;
36
37 use std::getopts::{groups, opt_present};
38 use std::getopts;
39 use syntax::codemap;
40 use syntax::diagnostic;
41
42 pub mod middle {
43     pub mod trans {
44         pub mod macros;
45         pub mod inline;
46         pub mod monomorphize;
47         pub mod controlflow;
48         pub mod glue;
49         pub mod datum;
50         pub mod callee;
51         pub mod expr;
52         pub mod common;
53         pub mod consts;
54         pub mod type_of;
55         pub mod build;
56         pub mod base;
57         pub mod _match;
58         pub mod uniq;
59         pub mod closure;
60         pub mod tvec;
61         pub mod meth;
62         pub mod cabi;
63         pub mod cabi_x86;
64         pub mod cabi_x86_64;
65         pub mod cabi_arm;
66         pub mod cabi_mips;
67         pub mod foreign;
68         pub mod reflect;
69         pub mod shape;
70         pub mod debuginfo;
71         pub mod type_use;
72         pub mod reachable;
73         pub mod machine;
74         pub mod adt;
75         pub mod asm;
76     }
77     pub mod ty;
78     pub mod subst;
79     pub mod resolve;
80     #[path = "typeck/mod.rs"]
81     pub mod typeck;
82     pub mod check_loop;
83     pub mod check_match;
84     pub mod check_const;
85     pub mod lint;
86     #[path = "borrowck/mod.rs"]
87     pub mod borrowck;
88     pub mod mem_categorization;
89     pub mod liveness;
90     pub mod kind;
91     pub mod freevars;
92     pub mod pat_util;
93     pub mod region;
94     pub mod const_eval;
95     pub mod astencode;
96     pub mod lang_items;
97     pub mod privacy;
98     pub mod moves;
99 }
100
101 pub mod front {
102     pub mod config;
103     pub mod test;
104     pub mod core_inject;
105     pub mod intrinsic_inject;
106 }
107
108 pub mod back {
109     pub mod link;
110     pub mod abi;
111     pub mod upcall;
112     pub mod arm;
113     pub mod mips;
114     pub mod x86;
115     pub mod x86_64;
116     pub mod rpath;
117     pub mod target_strs;
118 }
119
120 #[path = "metadata/mod.rs"]
121 pub mod metadata;
122
123 #[path = "driver/mod.rs"]
124 pub mod driver;
125
126 pub mod util {
127     pub mod common;
128     pub mod ppaux;
129 }
130
131 pub mod lib {
132     pub mod llvm;
133 }
134
135 pub fn version(argv0: &str) {
136     let mut vers = ~"unknown version";
137     let env_vers = env!("CFG_VERSION");
138     if env_vers.len() != 0 { vers = env_vers; }
139     io::println(fmt!("%s %s", argv0, vers));
140     io::println(fmt!("host: %s", host_triple()));
141 }
142
143 pub fn usage(argv0: &str) {
144     let message = fmt!("Usage: %s [OPTIONS] INPUT", argv0);
145     io::println(groups::usage(message, optgroups()) +
146                 ~"Additional help:
147     -W help             Print 'lint' options and default settings
148     -Z help             Print internal options for debugging rustc
149 ");
150 }
151
152 pub fn describe_warnings() {
153     io::println(fmt!("
154 Available lint options:
155     -W <foo>           Warn about <foo>
156     -A <foo>           Allow <foo>
157     -D <foo>           Deny <foo>
158     -F <foo>           Forbid <foo> (deny, and deny all overrides)
159 "));
160
161     let lint_dict = lint::get_lint_dict();
162     let mut max_key = 0;
163     for lint_dict.each_key |k| { max_key = uint::max(k.len(), max_key); }
164     fn padded(max: uint, s: &str) -> ~str {
165         str::from_bytes(vec::from_elem(max - s.len(), ' ' as u8)) + s
166     }
167     io::println(fmt!("\nAvailable lint checks:\n"));
168     io::println(fmt!("    %s  %7.7s  %s",
169                      padded(max_key, ~"name"), ~"default", ~"meaning"));
170     io::println(fmt!("    %s  %7.7s  %s\n",
171                      padded(max_key, ~"----"), ~"-------", ~"-------"));
172     for lint_dict.each |k, v| {
173         let k = str::replace(*k, ~"_", ~"-");
174         io::println(fmt!("    %s  %7.7s  %s",
175                          padded(max_key, k),
176                          match v.default {
177                              lint::allow => ~"allow",
178                              lint::warn => ~"warn",
179                              lint::deny => ~"deny",
180                              lint::forbid => ~"forbid"
181                          },
182                          v.desc));
183     }
184     io::println(~"");
185 }
186
187 pub fn describe_debug_flags() {
188     io::println(fmt!("\nAvailable debug options:\n"));
189     for session::debugging_opts_map().each |pair| {
190         let (name, desc, _) = /*bad*/copy *pair;
191         io::println(fmt!("    -Z %-20s -- %s", name, desc));
192     }
193 }
194
195 pub fn run_compiler(args: &~[~str], demitter: diagnostic::Emitter) {
196     // Don't display log spew by default. Can override with RUST_LOG.
197     ::core::logging::console_off();
198
199     let mut args = /*bad*/copy *args;
200     let binary = @args.shift();
201
202     if args.is_empty() { usage(*binary); return; }
203
204     let matches =
205         &match getopts::groups::getopts(args, optgroups()) {
206           Ok(m) => m,
207           Err(f) => {
208             early_error(demitter, getopts::fail_str(f));
209           }
210         };
211
212     if opt_present(matches, ~"h") || opt_present(matches, ~"help") {
213         usage(*binary);
214         return;
215     }
216
217     let lint_flags = vec::append(getopts::opt_strs(matches, ~"W"),
218                                  getopts::opt_strs(matches, ~"warn"));
219     if lint_flags.contains(&~"help") {
220         describe_warnings();
221         return;
222     }
223
224     if getopts::opt_strs(matches, ~"Z").contains(&~"help") {
225         describe_debug_flags();
226         return;
227     }
228
229     if opt_present(matches, ~"v") || opt_present(matches, ~"version") {
230         version(*binary);
231         return;
232     }
233     let input = match vec::len(matches.free) {
234       0u => early_error(demitter, ~"no input filename given"),
235       1u => {
236         let ifile = /*bad*/copy matches.free[0];
237         if ifile == ~"-" {
238             let src = str::from_bytes(io::stdin().read_whole_stream());
239             str_input(src)
240         } else {
241             file_input(Path(ifile))
242         }
243       }
244       _ => early_error(demitter, ~"multiple input filenames provided")
245     };
246
247     let sopts = build_session_options(binary, matches, demitter);
248     let sess = build_session(sopts, demitter);
249     let odir = getopts::opt_maybe_str(matches, ~"out-dir");
250     let odir = odir.map(|o| Path(*o));
251     let ofile = getopts::opt_maybe_str(matches, ~"o");
252     let ofile = ofile.map(|o| Path(*o));
253     let cfg = build_configuration(sess, binary, &input);
254     let pretty = getopts::opt_default(matches, ~"pretty", "normal").map(
255                     |a| parse_pretty(sess, *a));
256     match pretty {
257       Some::<pp_mode>(ppm) => {
258         pretty_print_input(sess, cfg, &input, ppm);
259         return;
260       }
261       None::<pp_mode> => {/* continue */ }
262     }
263     let ls = opt_present(matches, ~"ls");
264     if ls {
265         match input {
266           file_input(ref ifile) => {
267             list_metadata(sess, &(*ifile), io::stdout());
268           }
269           str_input(_) => {
270             early_error(demitter, ~"can not list metadata for stdin");
271           }
272         }
273         return;
274     }
275
276     compile_input(sess, cfg, &input, &odir, &ofile);
277 }
278
279 #[deriving(Eq)]
280 pub enum monitor_msg {
281     fatal,
282     done,
283 }
284
285 /*
286 This is a sanity check that any failure of the compiler is performed
287 through the diagnostic module and reported properly - we shouldn't be calling
288 plain-old-fail on any execution path that might be taken. Since we have
289 console logging off by default, hitting a plain fail statement would make the
290 compiler silently exit, which would be terrible.
291
292 This method wraps the compiler in a subtask and injects a function into the
293 diagnostic emitter which records when we hit a fatal error. If the task
294 fails without recording a fatal error then we've encountered a compiler
295 bug and need to present an error.
296 */
297 pub fn monitor(f: ~fn(diagnostic::Emitter)) {
298     use core::comm::*;
299     let (p, ch) = stream();
300     let ch = SharedChan::new(ch);
301     let ch_capture = ch.clone();
302     match do task::try || {
303         let ch = ch_capture.clone();
304         let ch_capture = ch.clone();
305         // The 'diagnostics emitter'. Every error, warning, etc. should
306         // go through this function.
307         let demitter: @fn(Option<(@codemap::CodeMap, codemap::span)>,
308                           &str,
309                           diagnostic::level) =
310                           |cmsp, msg, lvl| {
311             if lvl == diagnostic::fatal {
312                 ch_capture.send(fatal);
313             }
314             diagnostic::emit(cmsp, msg, lvl);
315         };
316
317         struct finally {
318             ch: SharedChan<monitor_msg>,
319         }
320
321         impl Drop for finally {
322             fn finalize(&self) { self.ch.send(done); }
323         }
324
325         let _finally = finally { ch: ch };
326
327         f(demitter)
328     } {
329         result::Ok(_) => { /* fallthrough */ }
330         result::Err(_) => {
331             // Task failed without emitting a fatal diagnostic
332             if p.recv() == done {
333                 diagnostic::emit(
334                     None,
335                     diagnostic::ice_msg(~"unexpected failure"),
336                     diagnostic::error);
337
338                 for [
339                     ~"the compiler hit an unexpected failure path. \
340                      this is a bug",
341                     ~"try running with RUST_LOG=rustc=1,::rt::backtrace \
342                      to get further details and report the results \
343                      to github.com/mozilla/rust/issues"
344                 ].each |note| {
345                     diagnostic::emit(None, *note, diagnostic::note)
346                 }
347             }
348             // Fail so the process returns a failure code
349             fail!();
350         }
351     }
352 }
353
354 pub fn main() {
355     let args = os::args();
356     do monitor |demitter| {
357         run_compiler(&args, demitter);
358     }
359 }