]> git.lizzy.rs Git - rust.git/blob - src/librustc/driver/session.rs
d725e2db1eba109474b71b0a65eaf6d655fd8e6c
[rust.git] / src / librustc / driver / session.rs
1 // Copyright 2012 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
12 use back::link;
13 use back::target_strs;
14 use back;
15 use driver::driver::host_triple;
16 use driver::session;
17 use metadata::filesearch;
18 use metadata;
19 use middle::lint;
20
21 use syntax::ast::NodeId;
22 use syntax::ast::{int_ty, uint_ty, float_ty};
23 use syntax::codemap::span;
24 use syntax::diagnostic;
25 use syntax::parse::ParseSess;
26 use syntax::{ast, codemap};
27 use syntax::abi;
28 use syntax::parse::token;
29 use syntax;
30
31 use std::hashmap::HashMap;
32
33 #[deriving(Eq)]
34 pub enum os { os_win32, os_macos, os_linux, os_android, os_freebsd, }
35
36 #[deriving(Clone)]
37 pub enum crate_type {
38     bin_crate,
39     lib_crate,
40     unknown_crate,
41 }
42
43 pub struct config {
44     os: os,
45     arch: abi::Architecture,
46     target_strs: target_strs::t,
47     int_type: int_ty,
48     uint_type: uint_ty,
49     float_type: float_ty
50 }
51
52 pub static verbose:                 uint = 1 <<  0;
53 pub static time_passes:             uint = 1 <<  1;
54 pub static count_llvm_insns:        uint = 1 <<  2;
55 pub static time_llvm_passes:        uint = 1 <<  3;
56 pub static trans_stats:             uint = 1 <<  4;
57 pub static asm_comments:            uint = 1 <<  5;
58 pub static no_verify:               uint = 1 <<  6;
59 pub static trace:                   uint = 1 <<  7;
60 pub static coherence:               uint = 1 <<  8;
61 pub static borrowck_stats:          uint = 1 <<  9;
62 pub static borrowck_note_pure:      uint = 1 << 10;
63 pub static borrowck_note_loan:      uint = 1 << 11;
64 pub static no_landing_pads:         uint = 1 << 12;
65 pub static debug_llvm:              uint = 1 << 13;
66 pub static count_type_sizes:        uint = 1 << 14;
67 pub static meta_stats:              uint = 1 << 15;
68 pub static no_opt:                  uint = 1 << 16;
69 pub static no_monomorphic_collapse: uint = 1 << 17;
70 pub static gc:                      uint = 1 << 18;
71 pub static jit:                     uint = 1 << 19;
72 pub static debug_info:              uint = 1 << 20;
73 pub static extra_debug_info:        uint = 1 << 21;
74 pub static statik:                  uint = 1 << 22;
75 pub static print_link_args:         uint = 1 << 23;
76 pub static no_debug_borrows:        uint = 1 << 24;
77 pub static lint_llvm:               uint = 1 << 25;
78 pub static once_fns:                uint = 1 << 26;
79
80 pub fn debugging_opts_map() -> ~[(~str, ~str, uint)] {
81     ~[(~"verbose", ~"in general, enable more debug printouts", verbose),
82      (~"time-passes", ~"measure time of each rustc pass", time_passes),
83      (~"count-llvm-insns", ~"count where LLVM \
84                            instrs originate", count_llvm_insns),
85      (~"time-llvm-passes", ~"measure time of each LLVM pass",
86       time_llvm_passes),
87      (~"trans-stats", ~"gather trans statistics", trans_stats),
88      (~"asm-comments", ~"generate comments into the assembly (may change behavior)", asm_comments),
89      (~"no-verify", ~"skip LLVM verification", no_verify),
90      (~"trace", ~"emit trace logs", trace),
91      (~"coherence", ~"perform coherence checking", coherence),
92      (~"borrowck-stats", ~"gather borrowck statistics",  borrowck_stats),
93      (~"borrowck-note-pure", ~"note where purity is req'd",
94       borrowck_note_pure),
95      (~"borrowck-note-loan", ~"note where loans are req'd",
96       borrowck_note_loan),
97      (~"no-landing-pads", ~"omit landing pads for unwinding",
98       no_landing_pads),
99      (~"debug-llvm", ~"enable debug output from LLVM", debug_llvm),
100      (~"count-type-sizes", ~"count the sizes of aggregate types",
101       count_type_sizes),
102      (~"meta-stats", ~"gather metadata statistics", meta_stats),
103      (~"no-opt", ~"do not optimize, even if -O is passed", no_opt),
104      (~"no-monomorphic-collapse", ~"do not collapse template instantiations",
105       no_monomorphic_collapse),
106      (~"print-link-args", ~"Print the arguments passed to the linker", print_link_args),
107      (~"gc", ~"Garbage collect shared data (experimental)", gc),
108      (~"jit", ~"Execute using JIT (experimental)", jit),
109      (~"extra-debug-info", ~"Extra debugging info (experimental)",
110       extra_debug_info),
111      (~"debug-info", ~"Produce debug info (experimental)", debug_info),
112      (~"static", ~"Use or produce static libraries or binaries " +
113       "(experimental)", statik),
114      (~"no-debug-borrows",
115       ~"do not show where borrow checks fail",
116       no_debug_borrows),
117      (~"lint-llvm",
118       ~"Run the LLVM lint pass on the pre-optimization IR",
119       lint_llvm),
120      (~"once-fns",
121       ~"Allow 'once fn' closures to deinitialize captured variables",
122       once_fns),
123     ]
124 }
125
126 #[deriving(Clone, Eq)]
127 pub enum OptLevel {
128     No, // -O0
129     Less, // -O1
130     Default, // -O2
131     Aggressive // -O3
132 }
133
134 #[deriving(Clone)]
135 pub struct options {
136     // The crate config requested for the session, which may be combined
137     // with additional crate configurations during the compile process
138     crate_type: crate_type,
139     is_static: bool,
140     gc: bool,
141     optimize: OptLevel,
142     custom_passes: ~[~str],
143     debuginfo: bool,
144     extra_debuginfo: bool,
145     lint_opts: ~[(lint::lint, lint::level)],
146     save_temps: bool,
147     jit: bool,
148     output_type: back::link::output_type,
149     addl_lib_search_paths: @mut ~[Path], // This is mutable for rustpkg, which
150                                          // updates search paths based on the
151                                          // parsed code
152     linker: Option<~str>,
153     linker_args: ~[~str],
154     maybe_sysroot: Option<@Path>,
155     target_triple: ~str,
156     target_feature: ~str,
157     // User-specified cfg meta items. The compiler itself will add additional
158     // items to the crate config, and during parsing the entire crate config
159     // will be added to the crate AST node.  This should not be used for
160     // anything except building the full crate config prior to parsing.
161     cfg: ast::CrateConfig,
162     binary: @str,
163     test: bool,
164     parse_only: bool,
165     no_trans: bool,
166     debugging_opts: uint,
167     android_cross_path: Option<~str>,
168 }
169
170 pub struct crate_metadata {
171     name: ~str,
172     data: ~[u8]
173 }
174
175 // The type of entry function, so
176 // users can have their own entry
177 // functions that don't start a
178 // scheduler
179 #[deriving(Eq)]
180 pub enum EntryFnType {
181     EntryMain,
182     EntryStart,
183     EntryNone,
184 }
185
186 pub struct Session_ {
187     targ_cfg: @config,
188     opts: @options,
189     cstore: @mut metadata::cstore::CStore,
190     parse_sess: @mut ParseSess,
191     codemap: @codemap::CodeMap,
192     // For a library crate, this is always none
193     entry_fn: @mut Option<(NodeId, codemap::span)>,
194     entry_type: @mut Option<EntryFnType>,
195     span_diagnostic: @diagnostic::span_handler,
196     filesearch: @filesearch::FileSearch,
197     building_library: @mut bool,
198     working_dir: Path,
199     lints: @mut HashMap<ast::NodeId, ~[(lint::lint, codemap::span, ~str)]>,
200 }
201
202 pub type Session = @Session_;
203
204 impl Session_ {
205     pub fn span_fatal(@self, sp: span, msg: &str) -> ! {
206         self.span_diagnostic.span_fatal(sp, msg)
207     }
208     pub fn fatal(@self, msg: &str) -> ! {
209         self.span_diagnostic.handler().fatal(msg)
210     }
211     pub fn span_err(@self, sp: span, msg: &str) {
212         self.span_diagnostic.span_err(sp, msg)
213     }
214     pub fn err(@self, msg: &str) {
215         self.span_diagnostic.handler().err(msg)
216     }
217     pub fn err_count(@self) -> uint {
218         self.span_diagnostic.handler().err_count()
219     }
220     pub fn has_errors(@self) -> bool {
221         self.span_diagnostic.handler().has_errors()
222     }
223     pub fn abort_if_errors(@self) {
224         self.span_diagnostic.handler().abort_if_errors()
225     }
226     pub fn span_warn(@self, sp: span, msg: &str) {
227         self.span_diagnostic.span_warn(sp, msg)
228     }
229     pub fn warn(@self, msg: &str) {
230         self.span_diagnostic.handler().warn(msg)
231     }
232     pub fn span_note(@self, sp: span, msg: &str) {
233         self.span_diagnostic.span_note(sp, msg)
234     }
235     pub fn note(@self, msg: &str) {
236         self.span_diagnostic.handler().note(msg)
237     }
238     pub fn span_bug(@self, sp: span, msg: &str) -> ! {
239         self.span_diagnostic.span_bug(sp, msg)
240     }
241     pub fn bug(@self, msg: &str) -> ! {
242         self.span_diagnostic.handler().bug(msg)
243     }
244     pub fn span_unimpl(@self, sp: span, msg: &str) -> ! {
245         self.span_diagnostic.span_unimpl(sp, msg)
246     }
247     pub fn unimpl(@self, msg: &str) -> ! {
248         self.span_diagnostic.handler().unimpl(msg)
249     }
250     pub fn add_lint(@self,
251                     lint: lint::lint,
252                     id: ast::NodeId,
253                     sp: span,
254                     msg: ~str) {
255         match self.lints.find_mut(&id) {
256             Some(arr) => { arr.push((lint, sp, msg)); return; }
257             None => {}
258         }
259         self.lints.insert(id, ~[(lint, sp, msg)]);
260     }
261     pub fn next_node_id(@self) -> ast::NodeId {
262         return syntax::parse::next_node_id(self.parse_sess);
263     }
264     pub fn diagnostic(@self) -> @diagnostic::span_handler {
265         self.span_diagnostic
266     }
267     pub fn debugging_opt(@self, opt: uint) -> bool {
268         (self.opts.debugging_opts & opt) != 0u
269     }
270     // This exists to help with refactoring to eliminate impossible
271     // cases later on
272     pub fn impossible_case(@self, sp: span, msg: &str) -> ! {
273         self.span_bug(sp, fmt!("Impossible case reached: %s", msg));
274     }
275     pub fn verbose(@self) -> bool { self.debugging_opt(verbose) }
276     pub fn time_passes(@self) -> bool { self.debugging_opt(time_passes) }
277     pub fn count_llvm_insns(@self) -> bool {
278         self.debugging_opt(count_llvm_insns)
279     }
280     pub fn count_type_sizes(@self) -> bool {
281         self.debugging_opt(count_type_sizes)
282     }
283     pub fn time_llvm_passes(@self) -> bool {
284         self.debugging_opt(time_llvm_passes)
285     }
286     pub fn trans_stats(@self) -> bool { self.debugging_opt(trans_stats) }
287     pub fn meta_stats(@self) -> bool { self.debugging_opt(meta_stats) }
288     pub fn asm_comments(@self) -> bool { self.debugging_opt(asm_comments) }
289     pub fn no_verify(@self) -> bool { self.debugging_opt(no_verify) }
290     pub fn lint_llvm(@self) -> bool { self.debugging_opt(lint_llvm) }
291     pub fn trace(@self) -> bool { self.debugging_opt(trace) }
292     pub fn coherence(@self) -> bool { self.debugging_opt(coherence) }
293     pub fn borrowck_stats(@self) -> bool { self.debugging_opt(borrowck_stats) }
294     pub fn borrowck_note_pure(@self) -> bool {
295         self.debugging_opt(borrowck_note_pure)
296     }
297     pub fn borrowck_note_loan(@self) -> bool {
298         self.debugging_opt(borrowck_note_loan)
299     }
300     pub fn no_monomorphic_collapse(@self) -> bool {
301         self.debugging_opt(no_monomorphic_collapse)
302     }
303     pub fn debug_borrows(@self) -> bool {
304         self.opts.optimize == No && !self.debugging_opt(no_debug_borrows)
305     }
306     pub fn once_fns(@self) -> bool { self.debugging_opt(once_fns) }
307
308     // pointless function, now...
309     pub fn str_of(@self, id: ast::ident) -> @str {
310         token::ident_to_str(&id)
311     }
312
313     // pointless function, now...
314     pub fn ident_of(@self, st: &str) -> ast::ident {
315         token::str_to_ident(st)
316     }
317
318     // pointless function, now...
319     pub fn intr(@self) -> @syntax::parse::token::ident_interner {
320         token::get_ident_interner()
321     }
322 }
323
324 /// Some reasonable defaults
325 pub fn basic_options() -> @options {
326     @options {
327         crate_type: session::lib_crate,
328         is_static: false,
329         gc: false,
330         optimize: No,
331         custom_passes: ~[],
332         debuginfo: false,
333         extra_debuginfo: false,
334         lint_opts: ~[],
335         save_temps: false,
336         jit: false,
337         output_type: link::output_type_exe,
338         addl_lib_search_paths: @mut ~[],
339         linker: None,
340         linker_args: ~[],
341         maybe_sysroot: None,
342         target_triple: host_triple(),
343         target_feature: ~"",
344         cfg: ~[],
345         binary: @"rustc",
346         test: false,
347         parse_only: false,
348         no_trans: false,
349         debugging_opts: 0u,
350         android_cross_path: None,
351     }
352 }
353
354 // Seems out of place, but it uses session, so I'm putting it here
355 pub fn expect<T:Clone>(sess: Session, opt: Option<T>, msg: &fn() -> ~str)
356                        -> T {
357     diagnostic::expect(sess.diagnostic(), opt, msg)
358 }
359
360 pub fn building_library(req_crate_type: crate_type,
361                         crate: &ast::Crate,
362                         testing: bool) -> bool {
363     match req_crate_type {
364       bin_crate => false,
365       lib_crate => true,
366       unknown_crate => {
367         if testing {
368             false
369         } else {
370             match syntax::attr::first_attr_value_str_by_name(
371                 crate.attrs,
372                 "crate_type") {
373               Some(s) => "lib" == s,
374               _ => false
375             }
376         }
377       }
378     }
379 }
380
381 pub fn sess_os_to_meta_os(os: os) -> metadata::loader::os {
382     use metadata::loader;
383
384     match os {
385       os_win32 => loader::os_win32,
386       os_linux => loader::os_linux,
387       os_android => loader::os_android,
388       os_macos => loader::os_macos,
389       os_freebsd => loader::os_freebsd
390     }
391 }
392
393 #[cfg(test)]
394 mod test {
395     use driver::session::{bin_crate, building_library, lib_crate};
396     use driver::session::{unknown_crate};
397
398     use syntax::ast;
399     use syntax::attr;
400     use syntax::codemap;
401
402     fn make_crate_type_attr(t: @str) -> ast::Attribute {
403         attr::mk_attr(attr::mk_name_value_item_str(@"crate_type", t))
404     }
405
406     fn make_crate(with_bin: bool, with_lib: bool) -> @ast::Crate {
407         let mut attrs = ~[];
408         if with_bin {
409             attrs.push(make_crate_type_attr(@"bin"));
410         }
411         if with_lib {
412             attrs.push(make_crate_type_attr(@"lib"));
413         }
414         @ast::Crate {
415             module: ast::_mod { view_items: ~[], items: ~[] },
416             attrs: attrs,
417             config: ~[],
418             span: codemap::dummy_sp(),
419         }
420     }
421
422     #[test]
423     fn bin_crate_type_attr_results_in_bin_output() {
424         let crate = make_crate(true, false);
425         assert!(!building_library(unknown_crate, crate, false));
426     }
427
428     #[test]
429     fn lib_crate_type_attr_results_in_lib_output() {
430         let crate = make_crate(false, true);
431         assert!(building_library(unknown_crate, crate, false));
432     }
433
434     #[test]
435     fn bin_option_overrides_lib_crate_type() {
436         let crate = make_crate(false, true);
437         assert!(!building_library(bin_crate, crate, false));
438     }
439
440     #[test]
441     fn lib_option_overrides_bin_crate_type() {
442         let crate = make_crate(true, false);
443         assert!(building_library(lib_crate, crate, false));
444     }
445
446     #[test]
447     fn bin_crate_type_is_default() {
448         let crate = make_crate(false, false);
449         assert!(!building_library(unknown_crate, crate, false));
450     }
451
452     #[test]
453     fn test_option_overrides_lib_crate_type() {
454         let crate = make_crate(false, true);
455         assert!(!building_library(unknown_crate, crate, true));
456     }
457
458     #[test]
459     fn test_option_does_not_override_requested_lib_type() {
460         let crate = make_crate(false, false);
461         assert!(building_library(lib_crate, crate, true));
462     }
463 }