]> git.lizzy.rs Git - rust.git/blob - src/librustc/session/mod.rs
Merge pull request #20510 from tshepang/patch-6
[rust.git] / src / librustc / session / mod.rs
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
12 use lint;
13 use metadata::cstore::CStore;
14 use metadata::filesearch;
15 use session::search_paths::PathKind;
16 use util::nodemap::NodeMap;
17
18 use syntax::ast::NodeId;
19 use syntax::codemap::Span;
20 use syntax::diagnostic::{self, Emitter};
21 use syntax::diagnostics;
22 use syntax::feature_gate;
23 use syntax::parse;
24 use syntax::parse::token;
25 use syntax::parse::ParseSess;
26 use syntax::{ast, codemap};
27
28 use std::os;
29 use std::cell::{Cell, RefCell};
30
31 pub mod config;
32 pub mod search_paths;
33
34 // Represents the data associated with a compilation
35 // session for a single crate.
36 pub struct Session {
37     pub target: config::Config,
38     pub opts: config::Options,
39     pub cstore: CStore,
40     pub parse_sess: ParseSess,
41     // For a library crate, this is always none
42     pub entry_fn: RefCell<Option<(NodeId, codemap::Span)>>,
43     pub entry_type: Cell<Option<config::EntryFnType>>,
44     pub plugin_registrar_fn: Cell<Option<ast::NodeId>>,
45     pub default_sysroot: Option<Path>,
46     // The name of the root source file of the crate, in the local file system. The path is always
47     // expected to be absolute. `None` means that there is no source file.
48     pub local_crate_source_file: Option<Path>,
49     pub working_dir: Path,
50     pub lint_store: RefCell<lint::LintStore>,
51     pub lints: RefCell<NodeMap<Vec<(lint::LintId, codemap::Span, String)>>>,
52     pub crate_types: RefCell<Vec<config::CrateType>>,
53     pub crate_metadata: RefCell<Vec<String>>,
54     pub features: RefCell<feature_gate::Features>,
55
56     /// The maximum recursion limit for potentially infinitely recursive
57     /// operations such as auto-dereference and monomorphization.
58     pub recursion_limit: Cell<uint>,
59
60     pub can_print_warnings: bool
61 }
62
63 impl Session {
64     pub fn span_fatal(&self, sp: Span, msg: &str) -> ! {
65         self.diagnostic().span_fatal(sp, msg)
66     }
67     pub fn fatal(&self, msg: &str) -> ! {
68         self.diagnostic().handler().fatal(msg)
69     }
70     pub fn span_err(&self, sp: Span, msg: &str) {
71         self.diagnostic().span_err(sp, msg)
72     }
73     pub fn span_err_with_code(&self, sp: Span, msg: &str, code: &str) {
74         self.diagnostic().span_err_with_code(sp, msg, code)
75     }
76     pub fn err(&self, msg: &str) {
77         self.diagnostic().handler().err(msg)
78     }
79     pub fn err_count(&self) -> uint {
80         self.diagnostic().handler().err_count()
81     }
82     pub fn has_errors(&self) -> bool {
83         self.diagnostic().handler().has_errors()
84     }
85     pub fn abort_if_errors(&self) {
86         self.diagnostic().handler().abort_if_errors()
87     }
88     pub fn span_warn(&self, sp: Span, msg: &str) {
89         if self.can_print_warnings {
90             self.diagnostic().span_warn(sp, msg)
91         }
92     }
93     pub fn span_warn_with_code(&self, sp: Span, msg: &str, code: &str) {
94         if self.can_print_warnings {
95             self.diagnostic().span_warn_with_code(sp, msg, code)
96         }
97     }
98     pub fn warn(&self, msg: &str) {
99         if self.can_print_warnings {
100             self.diagnostic().handler().warn(msg)
101         }
102     }
103     pub fn opt_span_warn(&self, opt_sp: Option<Span>, msg: &str) {
104         match opt_sp {
105             Some(sp) => self.span_warn(sp, msg),
106             None => self.warn(msg),
107         }
108     }
109     pub fn span_note(&self, sp: Span, msg: &str) {
110         self.diagnostic().span_note(sp, msg)
111     }
112     pub fn span_end_note(&self, sp: Span, msg: &str) {
113         self.diagnostic().span_end_note(sp, msg)
114     }
115     pub fn span_help(&self, sp: Span, msg: &str) {
116         self.diagnostic().span_help(sp, msg)
117     }
118     pub fn fileline_note(&self, sp: Span, msg: &str) {
119         self.diagnostic().fileline_note(sp, msg)
120     }
121     pub fn note(&self, msg: &str) {
122         self.diagnostic().handler().note(msg)
123     }
124     pub fn help(&self, msg: &str) {
125         self.diagnostic().handler().note(msg)
126     }
127     pub fn opt_span_bug(&self, opt_sp: Option<Span>, msg: &str) -> ! {
128         match opt_sp {
129             Some(sp) => self.span_bug(sp, msg),
130             None => self.bug(msg),
131         }
132     }
133     pub fn span_bug(&self, sp: Span, msg: &str) -> ! {
134         self.diagnostic().span_bug(sp, msg)
135     }
136     pub fn bug(&self, msg: &str) -> ! {
137         self.diagnostic().handler().bug(msg)
138     }
139     pub fn span_unimpl(&self, sp: Span, msg: &str) -> ! {
140         self.diagnostic().span_unimpl(sp, msg)
141     }
142     pub fn unimpl(&self, msg: &str) -> ! {
143         self.diagnostic().handler().unimpl(msg)
144     }
145     pub fn add_lint(&self,
146                     lint: &'static lint::Lint,
147                     id: ast::NodeId,
148                     sp: Span,
149                     msg: String) {
150         let lint_id = lint::LintId::of(lint);
151         let mut lints = self.lints.borrow_mut();
152         match lints.get_mut(&id) {
153             Some(arr) => { arr.push((lint_id, sp, msg)); return; }
154             None => {}
155         }
156         lints.insert(id, vec!((lint_id, sp, msg)));
157     }
158     pub fn next_node_id(&self) -> ast::NodeId {
159         self.parse_sess.next_node_id()
160     }
161     pub fn reserve_node_ids(&self, count: ast::NodeId) -> ast::NodeId {
162         self.parse_sess.reserve_node_ids(count)
163     }
164     pub fn diagnostic<'a>(&'a self) -> &'a diagnostic::SpanHandler {
165         &self.parse_sess.span_diagnostic
166     }
167     pub fn debugging_opt(&self, opt: u64) -> bool {
168         (self.opts.debugging_opts & opt) != 0
169     }
170     pub fn codemap<'a>(&'a self) -> &'a codemap::CodeMap {
171         &self.parse_sess.span_diagnostic.cm
172     }
173     // This exists to help with refactoring to eliminate impossible
174     // cases later on
175     pub fn impossible_case(&self, sp: Span, msg: &str) -> ! {
176         self.span_bug(sp,
177                       format!("impossible case reached: {}", msg)[]);
178     }
179     pub fn verbose(&self) -> bool { self.debugging_opt(config::VERBOSE) }
180     pub fn time_passes(&self) -> bool { self.debugging_opt(config::TIME_PASSES) }
181     pub fn count_llvm_insns(&self) -> bool {
182         self.debugging_opt(config::COUNT_LLVM_INSNS)
183     }
184     pub fn count_type_sizes(&self) -> bool {
185         self.debugging_opt(config::COUNT_TYPE_SIZES)
186     }
187     pub fn time_llvm_passes(&self) -> bool {
188         self.debugging_opt(config::TIME_LLVM_PASSES)
189     }
190     pub fn trans_stats(&self) -> bool { self.debugging_opt(config::TRANS_STATS) }
191     pub fn meta_stats(&self) -> bool { self.debugging_opt(config::META_STATS) }
192     pub fn asm_comments(&self) -> bool { self.debugging_opt(config::ASM_COMMENTS) }
193     pub fn no_verify(&self) -> bool { self.debugging_opt(config::NO_VERIFY) }
194     pub fn borrowck_stats(&self) -> bool { self.debugging_opt(config::BORROWCK_STATS) }
195     pub fn print_llvm_passes(&self) -> bool {
196         self.debugging_opt(config::PRINT_LLVM_PASSES)
197     }
198     pub fn lto(&self) -> bool {
199         self.opts.cg.lto
200     }
201     pub fn no_landing_pads(&self) -> bool {
202         self.debugging_opt(config::NO_LANDING_PADS)
203     }
204     pub fn show_span(&self) -> bool {
205         self.debugging_opt(config::SHOW_SPAN)
206     }
207     pub fn print_enum_sizes(&self) -> bool {
208         self.debugging_opt(config::PRINT_ENUM_SIZES)
209     }
210     pub fn sysroot<'a>(&'a self) -> &'a Path {
211         match self.opts.maybe_sysroot {
212             Some (ref sysroot) => sysroot,
213             None => self.default_sysroot.as_ref()
214                         .expect("missing sysroot and default_sysroot in Session")
215         }
216     }
217     pub fn target_filesearch(&self, kind: PathKind) -> filesearch::FileSearch {
218         filesearch::FileSearch::new(self.sysroot(),
219                                     self.opts.target_triple[],
220                                     &self.opts.search_paths,
221                                     kind)
222     }
223     pub fn host_filesearch(&self, kind: PathKind) -> filesearch::FileSearch {
224         filesearch::FileSearch::new(
225             self.sysroot(),
226             config::host_triple(),
227             &self.opts.search_paths,
228             kind)
229     }
230 }
231
232 pub fn build_session(sopts: config::Options,
233                      local_crate_source_file: Option<Path>,
234                      registry: diagnostics::registry::Registry)
235                      -> Session {
236     let codemap = codemap::CodeMap::new();
237     let diagnostic_handler =
238         diagnostic::default_handler(sopts.color, Some(registry));
239     let span_diagnostic_handler =
240         diagnostic::mk_span_handler(diagnostic_handler, codemap);
241
242     build_session_(sopts, local_crate_source_file, span_diagnostic_handler)
243 }
244
245 pub fn build_session_(sopts: config::Options,
246                       local_crate_source_file: Option<Path>,
247                       span_diagnostic: diagnostic::SpanHandler)
248                       -> Session {
249     let target_cfg = config::build_target_config(&sopts, &span_diagnostic);
250     let p_s = parse::new_parse_sess_special_handler(span_diagnostic);
251     let default_sysroot = match sopts.maybe_sysroot {
252         Some(_) => None,
253         None => Some(filesearch::get_or_default_sysroot())
254     };
255
256     // Make the path absolute, if necessary
257     let local_crate_source_file = local_crate_source_file.map(|path|
258         if path.is_absolute() {
259             path.clone()
260         } else {
261             os::getcwd().unwrap().join(&path)
262         }
263     );
264
265     let can_print_warnings = sopts.lint_opts
266         .iter()
267         .filter(|&&(ref key, _)| *key == "warnings")
268         .map(|&(_, ref level)| *level != lint::Allow)
269         .last()
270         .unwrap_or(true);
271
272     let sess = Session {
273         target: target_cfg,
274         opts: sopts,
275         cstore: CStore::new(token::get_ident_interner()),
276         parse_sess: p_s,
277         // For a library crate, this is always none
278         entry_fn: RefCell::new(None),
279         entry_type: Cell::new(None),
280         plugin_registrar_fn: Cell::new(None),
281         default_sysroot: default_sysroot,
282         local_crate_source_file: local_crate_source_file,
283         working_dir: os::getcwd().unwrap(),
284         lint_store: RefCell::new(lint::LintStore::new()),
285         lints: RefCell::new(NodeMap::new()),
286         crate_types: RefCell::new(Vec::new()),
287         crate_metadata: RefCell::new(Vec::new()),
288         features: RefCell::new(feature_gate::Features::new()),
289         recursion_limit: Cell::new(64),
290         can_print_warnings: can_print_warnings
291     };
292
293     sess.lint_store.borrow_mut().register_builtin(Some(&sess));
294     sess
295 }
296
297 // Seems out of place, but it uses session, so I'm putting it here
298 pub fn expect<T, M>(sess: &Session, opt: Option<T>, msg: M) -> T where
299     M: FnOnce() -> String,
300 {
301     diagnostic::expect(sess.diagnostic(), opt, msg)
302 }
303
304 pub fn early_error(msg: &str) -> ! {
305     let mut emitter = diagnostic::EmitterWriter::stderr(diagnostic::Auto, None);
306     emitter.emit(None, msg, None, diagnostic::Fatal);
307     panic!(diagnostic::FatalError);
308 }
309
310 pub fn early_warn(msg: &str) {
311     let mut emitter = diagnostic::EmitterWriter::stderr(diagnostic::Auto, None);
312     emitter.emit(None, msg, None, diagnostic::Warning);
313 }