1 use crate::queries
::Queries
;
4 pub use crate::passes
::BoxedResolver
;
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
;
16 use std
::path
::PathBuf
;
18 use std
::sync
::{Arc, Mutex}
;
20 use syntax
::source_map
::{FileLoader, SourceMap}
;
22 pub type Result
<T
> = result
::Result
<T
, ErrorReported
>;
24 /// Represents a compiler session.
25 /// Can be used run `rustc_interface` queries.
26 /// Created by passing `Config` to `run_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
>,
41 pub fn session(&self) -> &Lrc
<Session
> {
44 pub fn codegen_backend(&self) -> &Lrc
<Box
<dyn CodegenBackend
>> {
47 pub fn cstore(&self) -> &Lrc
<CStore
> {
50 pub fn source_map(&self) -> &Lrc
<SourceMap
> {
53 pub fn input(&self) -> &Input
{
56 pub fn output_dir(&self) -> &Option
<PathBuf
> {
59 pub fn output_file(&self) -> &Option
<PathBuf
> {
64 /// The compiler configuration
66 /// Command line options
67 pub opts
: config
::Options
,
69 /// cfg! configuration in addition to the default ones
70 pub crate_cfg
: FxHashSet
<(String
, Option
<String
>)>,
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
,
79 /// Set to capture stderr output during compiler execution
80 pub stderr
: Option
<Arc
<Mutex
<Vec
<u8>>>>,
82 pub crate_name
: Option
<String
>,
83 pub lint_caps
: FxHashMap
<lint
::LintId
, lint
::Level
>,
86 pub fn run_compiler_in_existing_thread_pool
<F
, R
>(config
: Config
, f
: F
) -> R
88 F
: FnOnce(&Compiler
) -> R
,
90 let (sess
, codegen_backend
, source_map
) = util
::create_session(
93 config
.diagnostic_output
,
95 config
.input_path
.clone(),
99 let cstore
= Lrc
::new(CStore
::new(codegen_backend
.metadata_loader()));
101 let compiler
= Compiler
{
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
,
114 let _sess_abort_error
= OnDrop(|| compiler
.sess
.diagnostic().print_error_count());
116 if compiler
.sess
.profile_queries() {
117 profile
::begin(&compiler
.sess
);
120 let r
= f(&compiler
);
122 if compiler
.sess
.profile_queries() {
123 profile
::dump(&compiler
.sess
, "profile_queries".to_string())
126 if compiler
.sess
.opts
.debugging_opts
.self_profile
{
127 compiler
.sess
.profiler(|p
| p
.dump_raw_events(&compiler
.sess
.opts
));
133 pub fn run_compiler
<F
, R
>(mut config
: Config
, f
: F
) -> R
135 F
: FnOnce(&Compiler
) -> R
+ Send
,
138 let stderr
= config
.stderr
.take();
139 util
::spawn_thread_pool(
140 config
.opts
.debugging_opts
.threads
,
142 || run_compiler_in_existing_thread_pool(config
, f
),
146 pub fn default_thread_pool
<F
, R
>(f
: F
) -> R
148 F
: FnOnce() -> R
+ Send
,
151 util
::spawn_thread_pool(None
, &None
, f
)