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.
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.
13 use back::target_strs;
15 use driver::driver::host_triple;
17 use metadata::filesearch;
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};
28 use syntax::parse::token;
31 use std::hashmap::HashMap;
34 pub enum os { os_win32, os_macos, os_linux, os_android, os_freebsd, }
45 arch: abi::Architecture,
46 target_strs: target_strs::t,
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;
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",
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",
95 (~"borrowck-note-loan", ~"note where loans are req'd",
97 (~"no-landing-pads", ~"omit landing pads for unwinding",
99 (~"debug-llvm", ~"enable debug output from LLVM", debug_llvm),
100 (~"count-type-sizes", ~"count the sizes of aggregate types",
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)",
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",
118 ~"Run the LLVM lint pass on the pre-optimization IR",
121 ~"Allow 'once fn' closures to deinitialize captured variables",
126 #[deriving(Clone, Eq)]
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,
142 custom_passes: ~[~str],
144 extra_debuginfo: bool,
145 lint_opts: ~[(lint::lint, lint::level)],
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
152 linker: Option<~str>,
153 linker_args: ~[~str],
154 maybe_sysroot: Option<@Path>,
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,
166 debugging_opts: uint,
167 android_cross_path: Option<~str>,
170 pub struct crate_metadata {
175 // The type of entry function, so
176 // users can have their own entry
177 // functions that don't start a
180 pub enum EntryFnType {
186 pub struct Session_ {
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,
199 lints: @mut HashMap<ast::NodeId, ~[(lint::lint, codemap::span, ~str)]>,
202 pub type Session = @Session_;
205 pub fn span_fatal(@self, sp: span, msg: &str) -> ! {
206 self.span_diagnostic.span_fatal(sp, msg)
208 pub fn fatal(@self, msg: &str) -> ! {
209 self.span_diagnostic.handler().fatal(msg)
211 pub fn span_err(@self, sp: span, msg: &str) {
212 self.span_diagnostic.span_err(sp, msg)
214 pub fn err(@self, msg: &str) {
215 self.span_diagnostic.handler().err(msg)
217 pub fn err_count(@self) -> uint {
218 self.span_diagnostic.handler().err_count()
220 pub fn has_errors(@self) -> bool {
221 self.span_diagnostic.handler().has_errors()
223 pub fn abort_if_errors(@self) {
224 self.span_diagnostic.handler().abort_if_errors()
226 pub fn span_warn(@self, sp: span, msg: &str) {
227 self.span_diagnostic.span_warn(sp, msg)
229 pub fn warn(@self, msg: &str) {
230 self.span_diagnostic.handler().warn(msg)
232 pub fn span_note(@self, sp: span, msg: &str) {
233 self.span_diagnostic.span_note(sp, msg)
235 pub fn note(@self, msg: &str) {
236 self.span_diagnostic.handler().note(msg)
238 pub fn span_bug(@self, sp: span, msg: &str) -> ! {
239 self.span_diagnostic.span_bug(sp, msg)
241 pub fn bug(@self, msg: &str) -> ! {
242 self.span_diagnostic.handler().bug(msg)
244 pub fn span_unimpl(@self, sp: span, msg: &str) -> ! {
245 self.span_diagnostic.span_unimpl(sp, msg)
247 pub fn unimpl(@self, msg: &str) -> ! {
248 self.span_diagnostic.handler().unimpl(msg)
250 pub fn add_lint(@self,
255 match self.lints.find_mut(&id) {
256 Some(arr) => { arr.push((lint, sp, msg)); return; }
259 self.lints.insert(id, ~[(lint, sp, msg)]);
261 pub fn next_node_id(@self) -> ast::NodeId {
262 return syntax::parse::next_node_id(self.parse_sess);
264 pub fn diagnostic(@self) -> @diagnostic::span_handler {
267 pub fn debugging_opt(@self, opt: uint) -> bool {
268 (self.opts.debugging_opts & opt) != 0u
270 // This exists to help with refactoring to eliminate impossible
272 pub fn impossible_case(@self, sp: span, msg: &str) -> ! {
273 self.span_bug(sp, fmt!("Impossible case reached: %s", msg));
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)
280 pub fn count_type_sizes(@self) -> bool {
281 self.debugging_opt(count_type_sizes)
283 pub fn time_llvm_passes(@self) -> bool {
284 self.debugging_opt(time_llvm_passes)
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)
297 pub fn borrowck_note_loan(@self) -> bool {
298 self.debugging_opt(borrowck_note_loan)
300 pub fn no_monomorphic_collapse(@self) -> bool {
301 self.debugging_opt(no_monomorphic_collapse)
303 pub fn debug_borrows(@self) -> bool {
304 self.opts.optimize == No && !self.debugging_opt(no_debug_borrows)
306 pub fn once_fns(@self) -> bool { self.debugging_opt(once_fns) }
308 // pointless function, now...
309 pub fn str_of(@self, id: ast::ident) -> @str {
310 token::ident_to_str(&id)
313 // pointless function, now...
314 pub fn ident_of(@self, st: &str) -> ast::ident {
315 token::str_to_ident(st)
318 // pointless function, now...
319 pub fn intr(@self) -> @syntax::parse::token::ident_interner {
320 token::get_ident_interner()
324 /// Some reasonable defaults
325 pub fn basic_options() -> @options {
327 crate_type: session::lib_crate,
333 extra_debuginfo: false,
337 output_type: link::output_type_exe,
338 addl_lib_search_paths: @mut ~[],
342 target_triple: host_triple(),
350 android_cross_path: None,
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)
357 diagnostic::expect(sess.diagnostic(), opt, msg)
360 pub fn building_library(req_crate_type: crate_type,
362 testing: bool) -> bool {
363 match req_crate_type {
370 match syntax::attr::first_attr_value_str_by_name(
373 Some(s) => "lib" == s,
381 pub fn sess_os_to_meta_os(os: os) -> metadata::loader::os {
382 use metadata::loader;
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
395 use driver::session::{bin_crate, building_library, lib_crate};
396 use driver::session::{unknown_crate};
402 fn make_crate_type_attr(t: @str) -> ast::Attribute {
403 attr::mk_attr(attr::mk_name_value_item_str(@"crate_type", t))
406 fn make_crate(with_bin: bool, with_lib: bool) -> @ast::Crate {
409 attrs.push(make_crate_type_attr(@"bin"));
412 attrs.push(make_crate_type_attr(@"lib"));
415 module: ast::_mod { view_items: ~[], items: ~[] },
418 span: codemap::dummy_sp(),
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));
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));
435 fn bin_option_overrides_lib_crate_type() {
436 let crate = make_crate(false, true);
437 assert!(!building_library(bin_crate, crate, false));
441 fn lib_option_overrides_bin_crate_type() {
442 let crate = make_crate(true, false);
443 assert!(building_library(lib_crate, crate, false));
447 fn bin_crate_type_is_default() {
448 let crate = make_crate(false, false);
449 assert!(!building_library(unknown_crate, crate, false));
453 fn test_option_overrides_lib_crate_type() {
454 let crate = make_crate(false, true);
455 assert!(!building_library(unknown_crate, crate, true));
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));