summary refs log tree commit diff
path: root/src/librustc/rustc.rc
blob: 85d440ebff9cc8b5e6f65c5e7224f41d2ccedadf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// -*- rust -*-
// Copyright 2012 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",
       vers = "0.5",
       uuid = "0ce89b41-2f92-459e-bbc1-8f5fe32f16cf",
       url = "https://github.com/mozilla/rust/tree/master/src/rustc")];

#[comment = "The Rust compiler"];
#[license = "MIT"];
#[crate_type = "lib"];

#[no_core];

#[legacy_modes];
#[legacy_exports];

#[allow(vecs_implicitly_copyable)];
#[allow(non_camel_case_types)];
#[allow(deprecated_mode)];
#[warn(deprecated_pattern)];

extern mod core(vers = "0.5");
extern mod std(vers = "0.5");
extern mod syntax(vers = "0.5");

use core::*;

/*
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.
*/
use util_ = util;
use lib_ = lib;
use driver_ = driver;
use middle_ = middle;
use back_ = back;

mod middle {
    #[legacy_exports];
    mod trans {
        #[legacy_exports];
        #[legacy_exports]
        mod inline;
        #[legacy_exports]
        mod monomorphize;
        #[legacy_exports]
        mod controlflow;
        #[legacy_exports]
        mod glue;
        #[legacy_exports]
        mod datum;
        #[legacy_exports]
        mod callee;
        #[legacy_exports]
        mod expr;
        #[legacy_exports]
        mod common;
        #[legacy_exports]
        mod consts;
        #[legacy_exports]
        mod type_of;
        #[legacy_exports]
        mod build;
        #[legacy_exports]
        mod base;
        #[legacy_exports]
        mod alt;
        #[legacy_exports]
        mod uniq;
        #[legacy_exports]
        mod closure;
        #[legacy_exports]
        mod tvec;
        #[legacy_exports]
        mod meth;
        #[legacy_exports]
        mod foreign;
        #[legacy_exports]
        mod reflect;
        #[legacy_exports]
        mod shape;
        #[legacy_exports]
        mod debuginfo;
        #[legacy_exports]
        mod type_use;
        #[legacy_exports]
        mod reachable;
        mod machine;
    }
    #[legacy_exports]
    mod ty;
    #[legacy_exports]
    mod resolve;
    #[path = "typeck/mod.rs"]
    pub mod typeck;
    #[legacy_exports]
    mod check_loop;
    #[legacy_exports]
    mod check_alt;
    #[legacy_exports]
    mod check_const;
    #[legacy_exports]
    mod lint;
    #[path = "borrowck/mod.rs"]
    pub mod borrowck;
    #[legacy_exports]
    mod mem_categorization;
    #[legacy_exports]
    mod liveness;
    #[legacy_exports]
    mod kind;
    #[legacy_exports]
    mod freevars;
    #[legacy_exports]
    mod capture;
    #[legacy_exports]
    mod pat_util;
    #[legacy_exports]
    mod region;
    #[legacy_exports]
    mod const_eval;
    #[legacy_exports]
    mod astencode;
    #[legacy_exports]
    mod lang_items;
    #[legacy_exports]
    mod privacy;
    mod mode;
}

mod front {
    #[legacy_exports];
    #[legacy_exports]
    mod config;
    #[legacy_exports]
    mod test;
    #[legacy_exports]
    mod core_inject;
    #[legacy_exports]
    mod intrinsic_inject;
}

mod back {
    #[legacy_exports];
    #[legacy_exports]
    mod link;
    #[legacy_exports]
    mod abi;
    #[legacy_exports]
    mod upcall;
    #[legacy_exports]
    mod x86;
    #[legacy_exports]
    mod x86_64;
    #[legacy_exports]
    mod rpath;
    #[legacy_exports]
    mod target_strs;
}

#[path = "metadata/mod.rs"]
mod metadata;

#[path = "driver/mod.rs"]
mod driver;

mod util {
    #[legacy_exports];
    #[legacy_exports]
    mod common;
    #[legacy_exports]
    mod ppaux;
}

mod lib {
    #[legacy_exports];
    #[legacy_exports]
    mod llvm;
}

use result::{Ok, Err};
use io::ReaderUtil;
use std::getopts;
use std::map::HashMap;
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;

fn version(argv0: &str) {
    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()));
}

fn usage(argv0: &str) {
    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
");
}

fn describe_warnings() {
    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); }
    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, ~"_", ~"-");
        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(~"");
}

fn describe_debug_flags() {
    io::println(fmt!("\nAvailable debug options:\n"));
    for session::debugging_opts_map().each |pair| {
        let (name, desc, _) = *pair;
        io::println(fmt!("    -Z %-20s -- %s", name, desc));
    }
}

fn run_compiler(args: &~[~str], demitter: diagnostic::emitter) {
    // Don't display log spew by default. Can override with RUST_LOG.
    logging::console_off();

    let mut args = *args;
    let binary = args.shift();

    if args.is_empty() { usage(binary); return; }

    let matches =
        &match getopts::groups::getopts(args, optgroups()) {
          Ok(ref m) => (*m),
          Err(ref f) => {
            early_error(demitter, getopts::fail_str((*f)))
          }
        };

    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 = matches.free[0];
        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")
    };

    let sopts = build_session_options(binary, matches, demitter);
    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());
          }
          str_input(_) => {
            early_error(demitter, ~"can not list metadata for stdin");
          }
        }
        return;
    }

    compile_input(sess, cfg, input, &odir, &ofile);
}

enum monitor_msg {
    fatal,
    done,
}

impl monitor_msg : cmp::Eq {
    pure fn eq(&self, other: &monitor_msg) -> bool {
        ((*self) as uint) == ((*other) as uint)
    }
    pure fn ne(&self, other: &monitor_msg) -> bool { !(*self).eq(other) }
}

/*
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.
*/
fn monitor(+f: fn~(diagnostic::emitter)) {
    let p = oldcomm::Port();
    let ch = oldcomm::Chan(&p);

    match do task::try |move f| {

        // 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 {
                oldcomm::send(ch, fatal);
            }
            diagnostic::emit(cmsp, msg, lvl);
        };

        struct finally {
            ch: oldcomm::Chan<monitor_msg>,
            drop { oldcomm::send(self.ch, done); }
        }

        let _finally = finally { ch: ch };

        f(demitter)
    } {
        result::Ok(_) => { /* fallthrough */ }
        result::Err(_) => {
            // Task failed without emitting a fatal diagnostic
            if oldcomm::recv(p) == done {
                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;
        }
    }
}

fn main() {
    let mut args = os::args();
    do monitor |move args, demitter| {
        run_compiler(&args, demitter);
    }
}


// Local Variables:
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End: