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.
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 metadata::cstore::CStore;
14 use metadata::filesearch;
15 use session::search_paths::PathKind;
16 use util::nodemap::NodeMap;
18 use syntax::ast::NodeId;
19 use syntax::codemap::Span;
20 use syntax::diagnostic::{mod, Emitter};
21 use syntax::diagnostics;
22 use syntax::feature_gate;
24 use syntax::parse::token;
25 use syntax::parse::ParseSess;
26 use syntax::{ast, codemap};
29 use std::cell::{Cell, RefCell};
34 // Represents the data associated with a compilation
35 // session for a single crate.
37 pub target: config::Config,
38 pub opts: config::Options,
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>,
56 /// The maximum recursion limit for potentially infinitely recursive
57 /// operations such as auto-dereference and monomorphization.
58 pub recursion_limit: Cell<uint>,
60 pub can_print_warnings: bool
64 pub fn span_fatal(&self, sp: Span, msg: &str) -> ! {
65 self.diagnostic().span_fatal(sp, msg)
67 pub fn fatal(&self, msg: &str) -> ! {
68 self.diagnostic().handler().fatal(msg)
70 pub fn span_err(&self, sp: Span, msg: &str) {
71 self.diagnostic().span_err(sp, msg)
73 pub fn span_err_with_code(&self, sp: Span, msg: &str, code: &str) {
74 self.diagnostic().span_err_with_code(sp, msg, code)
76 pub fn err(&self, msg: &str) {
77 self.diagnostic().handler().err(msg)
79 pub fn err_count(&self) -> uint {
80 self.diagnostic().handler().err_count()
82 pub fn has_errors(&self) -> bool {
83 self.diagnostic().handler().has_errors()
85 pub fn abort_if_errors(&self) {
86 self.diagnostic().handler().abort_if_errors()
88 pub fn span_warn(&self, sp: Span, msg: &str) {
89 if self.can_print_warnings {
90 self.diagnostic().span_warn(sp, msg)
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)
98 pub fn warn(&self, msg: &str) {
99 if self.can_print_warnings {
100 self.diagnostic().handler().warn(msg)
103 pub fn opt_span_warn(&self, opt_sp: Option<Span>, msg: &str) {
105 Some(sp) => self.span_warn(sp, msg),
106 None => self.warn(msg),
109 pub fn span_note(&self, sp: Span, msg: &str) {
110 self.diagnostic().span_note(sp, msg)
112 pub fn span_end_note(&self, sp: Span, msg: &str) {
113 self.diagnostic().span_end_note(sp, msg)
115 pub fn span_help(&self, sp: Span, msg: &str) {
116 self.diagnostic().span_help(sp, msg)
118 pub fn fileline_note(&self, sp: Span, msg: &str) {
119 self.diagnostic().fileline_note(sp, msg)
121 pub fn note(&self, msg: &str) {
122 self.diagnostic().handler().note(msg)
124 pub fn help(&self, msg: &str) {
125 self.diagnostic().handler().note(msg)
127 pub fn opt_span_bug(&self, opt_sp: Option<Span>, msg: &str) -> ! {
129 Some(sp) => self.span_bug(sp, msg),
130 None => self.bug(msg),
133 pub fn span_bug(&self, sp: Span, msg: &str) -> ! {
134 self.diagnostic().span_bug(sp, msg)
136 pub fn bug(&self, msg: &str) -> ! {
137 self.diagnostic().handler().bug(msg)
139 pub fn span_unimpl(&self, sp: Span, msg: &str) -> ! {
140 self.diagnostic().span_unimpl(sp, msg)
142 pub fn unimpl(&self, msg: &str) -> ! {
143 self.diagnostic().handler().unimpl(msg)
145 pub fn add_lint(&self,
146 lint: &'static lint::Lint,
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; }
156 lints.insert(id, vec!((lint_id, sp, msg)));
158 pub fn next_node_id(&self) -> ast::NodeId {
159 self.parse_sess.next_node_id()
161 pub fn reserve_node_ids(&self, count: ast::NodeId) -> ast::NodeId {
162 self.parse_sess.reserve_node_ids(count)
164 pub fn diagnostic<'a>(&'a self) -> &'a diagnostic::SpanHandler {
165 &self.parse_sess.span_diagnostic
167 pub fn debugging_opt(&self, opt: u64) -> bool {
168 (self.opts.debugging_opts & opt) != 0
170 pub fn codemap<'a>(&'a self) -> &'a codemap::CodeMap {
171 &self.parse_sess.span_diagnostic.cm
173 // This exists to help with refactoring to eliminate impossible
175 pub fn impossible_case(&self, sp: Span, msg: &str) -> ! {
177 format!("impossible case reached: {}", msg)[]);
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)
184 pub fn count_type_sizes(&self) -> bool {
185 self.debugging_opt(config::COUNT_TYPE_SIZES)
187 pub fn time_llvm_passes(&self) -> bool {
188 self.debugging_opt(config::TIME_LLVM_PASSES)
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)
198 pub fn lto(&self) -> bool {
201 pub fn no_landing_pads(&self) -> bool {
202 self.debugging_opt(config::NO_LANDING_PADS)
204 pub fn show_span(&self) -> bool {
205 self.debugging_opt(config::SHOW_SPAN)
207 pub fn print_enum_sizes(&self) -> bool {
208 self.debugging_opt(config::PRINT_ENUM_SIZES)
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")
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,
223 pub fn host_filesearch(&self, kind: PathKind) -> filesearch::FileSearch {
224 filesearch::FileSearch::new(
226 config::host_triple(),
227 &self.opts.search_paths,
232 pub fn build_session(sopts: config::Options,
233 local_crate_source_file: Option<Path>,
234 registry: diagnostics::registry::Registry)
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);
242 build_session_(sopts, local_crate_source_file, span_diagnostic_handler)
245 pub fn build_session_(sopts: config::Options,
246 local_crate_source_file: Option<Path>,
247 span_diagnostic: diagnostic::SpanHandler)
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 {
253 None => Some(filesearch::get_or_default_sysroot())
256 // Make the path absolute, if necessary
257 let local_crate_source_file = local_crate_source_file.map(|path|
258 if path.is_absolute() {
261 os::getcwd().unwrap().join(&path)
265 let can_print_warnings = sopts.lint_opts
267 .filter(|&&(ref key, _)| *key == "warnings")
268 .map(|&(_, ref level)| *level != lint::Allow)
275 cstore: CStore::new(token::get_ident_interner()),
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
293 sess.lint_store.borrow_mut().register_builtin(Some(&sess));
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,
301 diagnostic::expect(sess.diagnostic(), opt, msg)
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);
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);