]> git.lizzy.rs Git - rust.git/blob - src/librustc_interface/interface.rs
Rollup merge of #60766 - vorner:weak-into-raw, r=sfackler
[rust.git] / src / librustc_interface / interface.rs
1 use crate::queries::Queries;
2 use crate::util;
3 use crate::profile;
4 pub use crate::passes::BoxedResolver;
5
6 use rustc::lint;
7 use rustc::session::config::{self, Input};
8 use rustc::session::{DiagnosticOutput, Session};
9 use rustc::util::common::ErrorReported;
10 use rustc_codegen_utils::codegen_backend::CodegenBackend;
11 use rustc_data_structures::OnDrop;
12 use rustc_data_structures::sync::Lrc;
13 use rustc_data_structures::fx::{FxHashSet, FxHashMap};
14 use rustc_metadata::cstore::CStore;
15 use std::io::Write;
16 use std::path::PathBuf;
17 use std::result;
18 use std::sync::{Arc, Mutex};
19 use syntax;
20 use syntax::source_map::{FileLoader, SourceMap};
21 use syntax_pos::edition;
22
23 pub type Result<T> = result::Result<T, ErrorReported>;
24
25 /// Represents a compiler session.
26 /// Can be used run `rustc_interface` queries.
27 /// Created by passing `Config` to `run_compiler`.
28 pub struct Compiler {
29     pub(crate) sess: Lrc<Session>,
30     codegen_backend: Lrc<Box<dyn CodegenBackend>>,
31     source_map: Lrc<SourceMap>,
32     pub(crate) input: Input,
33     pub(crate) input_path: Option<PathBuf>,
34     pub(crate) output_dir: Option<PathBuf>,
35     pub(crate) output_file: Option<PathBuf>,
36     pub(crate) queries: Queries,
37     pub(crate) cstore: Lrc<CStore>,
38     pub(crate) crate_name: Option<String>,
39 }
40
41 impl Compiler {
42     pub fn session(&self) -> &Lrc<Session> {
43         &self.sess
44     }
45     pub fn codegen_backend(&self) -> &Lrc<Box<dyn CodegenBackend>> {
46         &self.codegen_backend
47     }
48     pub fn cstore(&self) -> &Lrc<CStore> {
49         &self.cstore
50     }
51     pub fn source_map(&self) -> &Lrc<SourceMap> {
52         &self.source_map
53     }
54     pub fn input(&self) -> &Input {
55         &self.input
56     }
57     pub fn output_dir(&self) -> &Option<PathBuf> {
58         &self.output_dir
59     }
60     pub fn output_file(&self) -> &Option<PathBuf> {
61         &self.output_file
62     }
63 }
64
65 /// The compiler configuration
66 pub struct Config {
67     /// Command line options
68     pub opts: config::Options,
69
70     /// cfg! configuration in addition to the default ones
71     pub crate_cfg: FxHashSet<(String, Option<String>)>,
72
73     pub input: Input,
74     pub input_path: Option<PathBuf>,
75     pub output_dir: Option<PathBuf>,
76     pub output_file: Option<PathBuf>,
77     pub file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
78     pub diagnostic_output: DiagnosticOutput,
79
80     /// Set to capture stderr output during compiler execution
81     pub stderr: Option<Arc<Mutex<Vec<u8>>>>,
82
83     pub crate_name: Option<String>,
84     pub lint_caps: FxHashMap<lint::LintId, lint::Level>,
85 }
86
87 pub fn run_compiler_in_existing_thread_pool<F, R>(config: Config, f: F) -> R
88 where
89     F: FnOnce(&Compiler) -> R,
90 {
91     let (sess, codegen_backend, source_map) = util::create_session(
92         config.opts,
93         config.crate_cfg,
94         config.diagnostic_output,
95         config.file_loader,
96         config.input_path.clone(),
97         config.lint_caps,
98     );
99
100     let cstore = Lrc::new(CStore::new(codegen_backend.metadata_loader()));
101
102     let compiler = Compiler {
103         sess,
104         codegen_backend,
105         source_map,
106         cstore,
107         input: config.input,
108         input_path: config.input_path,
109         output_dir: config.output_dir,
110         output_file: config.output_file,
111         queries: Default::default(),
112         crate_name: config.crate_name,
113     };
114
115     let _sess_abort_error = OnDrop(|| {
116         compiler.sess.diagnostic().print_error_count(&util::diagnostics_registry());
117     });
118
119     if compiler.sess.profile_queries() {
120         profile::begin(&compiler.sess);
121     }
122
123     let r = f(&compiler);
124
125     if compiler.sess.profile_queries() {
126         profile::dump(&compiler.sess, "profile_queries".to_string())
127     }
128
129     r
130 }
131
132 pub fn run_compiler<F, R>(mut config: Config, f: F) -> R
133 where
134     F: FnOnce(&Compiler) -> R + Send,
135     R: Send,
136 {
137     let stderr = config.stderr.take();
138     util::spawn_thread_pool(
139         config.opts.edition,
140         config.opts.debugging_opts.threads,
141         &stderr,
142         || run_compiler_in_existing_thread_pool(config, f),
143     )
144 }
145
146 pub fn default_thread_pool<F, R>(edition: edition::Edition, f: F) -> R
147 where
148     F: FnOnce() -> R + Send,
149     R: Send,
150 {
151     util::spawn_thread_pool(edition, None, &None, f)
152 }