]> git.proxmox.com Git - rustc.git/blob - src/librustc_interface/interface.rs
New upstream version 1.35.0+dfsg1
[rustc.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
22 pub type Result<T> = result::Result<T, ErrorReported>;
23
24 /// Represents a compiler session.
25 /// Can be used run `rustc_interface` queries.
26 /// Created by passing `Config` to `run_compiler`.
27 pub struct Compiler {
28 pub(crate) sess: Lrc<Session>,
29 codegen_backend: Lrc<Box<dyn CodegenBackend>>,
30 source_map: Lrc<SourceMap>,
31 pub(crate) input: Input,
32 pub(crate) input_path: Option<PathBuf>,
33 pub(crate) output_dir: Option<PathBuf>,
34 pub(crate) output_file: Option<PathBuf>,
35 pub(crate) queries: Queries,
36 pub(crate) cstore: Lrc<CStore>,
37 pub(crate) crate_name: Option<String>,
38 }
39
40 impl Compiler {
41 pub fn session(&self) -> &Lrc<Session> {
42 &self.sess
43 }
44 pub fn codegen_backend(&self) -> &Lrc<Box<dyn CodegenBackend>> {
45 &self.codegen_backend
46 }
47 pub fn cstore(&self) -> &Lrc<CStore> {
48 &self.cstore
49 }
50 pub fn source_map(&self) -> &Lrc<SourceMap> {
51 &self.source_map
52 }
53 pub fn input(&self) -> &Input {
54 &self.input
55 }
56 pub fn output_dir(&self) -> &Option<PathBuf> {
57 &self.output_dir
58 }
59 pub fn output_file(&self) -> &Option<PathBuf> {
60 &self.output_file
61 }
62 }
63
64 /// The compiler configuration
65 pub struct Config {
66 /// Command line options
67 pub opts: config::Options,
68
69 /// cfg! configuration in addition to the default ones
70 pub crate_cfg: FxHashSet<(String, Option<String>)>,
71
72 pub input: Input,
73 pub input_path: Option<PathBuf>,
74 pub output_dir: Option<PathBuf>,
75 pub output_file: Option<PathBuf>,
76 pub file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
77 pub diagnostic_output: DiagnosticOutput,
78
79 /// Set to capture stderr output during compiler execution
80 pub stderr: Option<Arc<Mutex<Vec<u8>>>>,
81
82 pub crate_name: Option<String>,
83 pub lint_caps: FxHashMap<lint::LintId, lint::Level>,
84 }
85
86 pub fn run_compiler_in_existing_thread_pool<F, R>(config: Config, f: F) -> R
87 where
88 F: FnOnce(&Compiler) -> R,
89 {
90 let (sess, codegen_backend, source_map) = util::create_session(
91 config.opts,
92 config.crate_cfg,
93 config.diagnostic_output,
94 config.file_loader,
95 config.input_path.clone(),
96 config.lint_caps,
97 );
98
99 let cstore = Lrc::new(CStore::new(codegen_backend.metadata_loader()));
100
101 let compiler = Compiler {
102 sess,
103 codegen_backend,
104 source_map,
105 cstore,
106 input: config.input,
107 input_path: config.input_path,
108 output_dir: config.output_dir,
109 output_file: config.output_file,
110 queries: Default::default(),
111 crate_name: config.crate_name,
112 };
113
114 let _sess_abort_error = OnDrop(|| compiler.sess.diagnostic().print_error_count());
115
116 if compiler.sess.profile_queries() {
117 profile::begin(&compiler.sess);
118 }
119
120 let r = f(&compiler);
121
122 if compiler.sess.profile_queries() {
123 profile::dump(&compiler.sess, "profile_queries".to_string())
124 }
125
126 if compiler.sess.opts.debugging_opts.self_profile {
127 compiler.sess.profiler(|p| p.dump_raw_events(&compiler.sess.opts));
128 }
129
130 r
131 }
132
133 pub fn run_compiler<F, R>(mut config: Config, f: F) -> R
134 where
135 F: FnOnce(&Compiler) -> R + Send,
136 R: Send,
137 {
138 let stderr = config.stderr.take();
139 util::spawn_thread_pool(
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>(f: F) -> R
147 where
148 F: FnOnce() -> R + Send,
149 R: Send,
150 {
151 util::spawn_thread_pool(None, &None, f)
152 }