]> git.proxmox.com Git - rustc.git/blob - src/librustc_interface/queries.rs
New upstream version 1.46.0~beta.2+dfsg1
[rustc.git] / src / librustc_interface / queries.rs
1 use crate::interface::{Compiler, Result};
2 use crate::passes::{self, BoxedResolver, QueryContext};
3
4 use rustc_ast::{self, ast};
5 use rustc_codegen_ssa::traits::CodegenBackend;
6 use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal};
7 use rustc_errors::ErrorReported;
8 use rustc_hir::def_id::LOCAL_CRATE;
9 use rustc_hir::Crate;
10 use rustc_incremental::DepGraphFuture;
11 use rustc_lint::LintStore;
12 use rustc_middle::arena::Arena;
13 use rustc_middle::dep_graph::DepGraph;
14 use rustc_middle::ty::steal::Steal;
15 use rustc_middle::ty::{GlobalCtxt, ResolverOutputs, TyCtxt};
16 use rustc_session::config::{OutputFilenames, OutputType};
17 use rustc_session::{output::find_crate_name, Session};
18 use rustc_span::symbol::sym;
19 use std::any::Any;
20 use std::cell::{Ref, RefCell, RefMut};
21 use std::mem;
22 use std::rc::Rc;
23
24 /// Represent the result of a query.
25 /// This result can be stolen with the `take` method and generated with the `compute` method.
26 pub struct Query<T> {
27 result: RefCell<Option<Result<T>>>,
28 }
29
30 impl<T> Query<T> {
31 fn compute<F: FnOnce() -> Result<T>>(&self, f: F) -> Result<&Query<T>> {
32 let mut result = self.result.borrow_mut();
33 if result.is_none() {
34 *result = Some(f());
35 }
36 result.as_ref().unwrap().as_ref().map(|_| self).map_err(|err| *err)
37 }
38
39 /// Takes ownership of the query result. Further attempts to take or peek the query
40 /// result will panic unless it is generated by calling the `compute` method.
41 pub fn take(&self) -> T {
42 self.result.borrow_mut().take().expect("missing query result").unwrap()
43 }
44
45 /// Borrows the query result using the RefCell. Panics if the result is stolen.
46 pub fn peek(&self) -> Ref<'_, T> {
47 Ref::map(self.result.borrow(), |r| {
48 r.as_ref().unwrap().as_ref().expect("missing query result")
49 })
50 }
51
52 /// Mutably borrows the query result using the RefCell. Panics if the result is stolen.
53 pub fn peek_mut(&self) -> RefMut<'_, T> {
54 RefMut::map(self.result.borrow_mut(), |r| {
55 r.as_mut().unwrap().as_mut().expect("missing query result")
56 })
57 }
58 }
59
60 impl<T> Default for Query<T> {
61 fn default() -> Self {
62 Query { result: RefCell::new(None) }
63 }
64 }
65
66 pub struct Queries<'tcx> {
67 compiler: &'tcx Compiler,
68 gcx: OnceCell<GlobalCtxt<'tcx>>,
69
70 arena: WorkerLocal<Arena<'tcx>>,
71 hir_arena: WorkerLocal<rustc_ast_lowering::Arena<'tcx>>,
72
73 dep_graph_future: Query<Option<DepGraphFuture>>,
74 parse: Query<ast::Crate>,
75 crate_name: Query<String>,
76 register_plugins: Query<(ast::Crate, Lrc<LintStore>)>,
77 expansion: Query<(ast::Crate, Steal<Rc<RefCell<BoxedResolver>>>, Lrc<LintStore>)>,
78 dep_graph: Query<DepGraph>,
79 lower_to_hir: Query<(&'tcx Crate<'tcx>, Steal<ResolverOutputs>)>,
80 prepare_outputs: Query<OutputFilenames>,
81 global_ctxt: Query<QueryContext<'tcx>>,
82 ongoing_codegen: Query<Box<dyn Any>>,
83 }
84
85 impl<'tcx> Queries<'tcx> {
86 pub fn new(compiler: &'tcx Compiler) -> Queries<'tcx> {
87 Queries {
88 compiler,
89 gcx: OnceCell::new(),
90 arena: WorkerLocal::new(|_| Arena::default()),
91 hir_arena: WorkerLocal::new(|_| rustc_ast_lowering::Arena::default()),
92 dep_graph_future: Default::default(),
93 parse: Default::default(),
94 crate_name: Default::default(),
95 register_plugins: Default::default(),
96 expansion: Default::default(),
97 dep_graph: Default::default(),
98 lower_to_hir: Default::default(),
99 prepare_outputs: Default::default(),
100 global_ctxt: Default::default(),
101 ongoing_codegen: Default::default(),
102 }
103 }
104
105 fn session(&self) -> &Lrc<Session> {
106 &self.compiler.sess
107 }
108 fn codegen_backend(&self) -> &Lrc<Box<dyn CodegenBackend>> {
109 &self.compiler.codegen_backend()
110 }
111
112 pub fn dep_graph_future(&self) -> Result<&Query<Option<DepGraphFuture>>> {
113 self.dep_graph_future.compute(|| {
114 Ok(self
115 .session()
116 .opts
117 .build_dep_graph()
118 .then(|| rustc_incremental::load_dep_graph(self.session())))
119 })
120 }
121
122 pub fn parse(&self) -> Result<&Query<ast::Crate>> {
123 self.parse.compute(|| {
124 passes::parse(self.session(), &self.compiler.input).map_err(|mut parse_error| {
125 parse_error.emit();
126 ErrorReported
127 })
128 })
129 }
130
131 pub fn register_plugins(&self) -> Result<&Query<(ast::Crate, Lrc<LintStore>)>> {
132 self.register_plugins.compute(|| {
133 let crate_name = self.crate_name()?.peek().clone();
134 let krate = self.parse()?.take();
135
136 let empty: &(dyn Fn(&Session, &mut LintStore) + Sync + Send) = &|_, _| {};
137 let result = passes::register_plugins(
138 self.session(),
139 &*self.codegen_backend().metadata_loader(),
140 self.compiler.register_lints.as_deref().unwrap_or_else(|| empty),
141 krate,
142 &crate_name,
143 );
144
145 // Compute the dependency graph (in the background). We want to do
146 // this as early as possible, to give the DepGraph maximum time to
147 // load before dep_graph() is called, but it also can't happen
148 // until after rustc_incremental::prepare_session_directory() is
149 // called, which happens within passes::register_plugins().
150 self.dep_graph_future().ok();
151
152 result
153 })
154 }
155
156 pub fn crate_name(&self) -> Result<&Query<String>> {
157 self.crate_name.compute(|| {
158 Ok(match self.compiler.crate_name {
159 Some(ref crate_name) => crate_name.clone(),
160 None => {
161 let parse_result = self.parse()?;
162 let krate = parse_result.peek();
163 find_crate_name(Some(self.session()), &krate.attrs, &self.compiler.input)
164 }
165 })
166 })
167 }
168
169 pub fn expansion(
170 &self,
171 ) -> Result<&Query<(ast::Crate, Steal<Rc<RefCell<BoxedResolver>>>, Lrc<LintStore>)>> {
172 log::trace!("expansion");
173 self.expansion.compute(|| {
174 let crate_name = self.crate_name()?.peek().clone();
175 let (krate, lint_store) = self.register_plugins()?.take();
176 let _timer = self.session().timer("configure_and_expand");
177 passes::configure_and_expand(
178 self.session().clone(),
179 lint_store.clone(),
180 self.codegen_backend().metadata_loader(),
181 krate,
182 &crate_name,
183 )
184 .map(|(krate, resolver)| {
185 (krate, Steal::new(Rc::new(RefCell::new(resolver))), lint_store)
186 })
187 })
188 }
189
190 pub fn dep_graph(&self) -> Result<&Query<DepGraph>> {
191 self.dep_graph.compute(|| {
192 Ok(match self.dep_graph_future()?.take() {
193 None => DepGraph::new_disabled(),
194 Some(future) => {
195 let (prev_graph, prev_work_products) =
196 self.session().time("blocked_on_dep_graph_loading", || {
197 future
198 .open()
199 .unwrap_or_else(|e| rustc_incremental::LoadResult::Error {
200 message: format!("could not decode incremental cache: {:?}", e),
201 })
202 .open(self.session())
203 });
204 DepGraph::new(prev_graph, prev_work_products)
205 }
206 })
207 })
208 }
209
210 pub fn lower_to_hir(&'tcx self) -> Result<&Query<(&'tcx Crate<'tcx>, Steal<ResolverOutputs>)>> {
211 self.lower_to_hir.compute(|| {
212 let expansion_result = self.expansion()?;
213 let peeked = expansion_result.peek();
214 let krate = &peeked.0;
215 let resolver = peeked.1.steal();
216 let lint_store = &peeked.2;
217 let hir = resolver.borrow_mut().access(|resolver| {
218 Ok(passes::lower_to_hir(
219 self.session(),
220 lint_store,
221 resolver,
222 &*self.dep_graph()?.peek(),
223 &krate,
224 &self.hir_arena,
225 ))
226 })?;
227 let hir = self.hir_arena.alloc(hir);
228 Ok((hir, Steal::new(BoxedResolver::to_resolver_outputs(resolver))))
229 })
230 }
231
232 pub fn prepare_outputs(&self) -> Result<&Query<OutputFilenames>> {
233 self.prepare_outputs.compute(|| {
234 let expansion_result = self.expansion()?;
235 let (krate, boxed_resolver, _) = &*expansion_result.peek();
236 let crate_name = self.crate_name()?;
237 let crate_name = crate_name.peek();
238 passes::prepare_outputs(
239 self.session(),
240 self.compiler,
241 &krate,
242 &boxed_resolver,
243 &crate_name,
244 )
245 })
246 }
247
248 pub fn global_ctxt(&'tcx self) -> Result<&Query<QueryContext<'tcx>>> {
249 self.global_ctxt.compute(|| {
250 let crate_name = self.crate_name()?.peek().clone();
251 let outputs = self.prepare_outputs()?.peek().clone();
252 let lint_store = self.expansion()?.peek().2.clone();
253 let hir = self.lower_to_hir()?.peek();
254 let dep_graph = self.dep_graph()?.peek().clone();
255 let (ref krate, ref resolver_outputs) = &*hir;
256 let _timer = self.session().timer("create_global_ctxt");
257 Ok(passes::create_global_ctxt(
258 self.compiler,
259 lint_store,
260 krate,
261 dep_graph,
262 resolver_outputs.steal(),
263 outputs,
264 &crate_name,
265 &self.gcx,
266 &self.arena,
267 ))
268 })
269 }
270
271 pub fn ongoing_codegen(&'tcx self) -> Result<&Query<Box<dyn Any>>> {
272 self.ongoing_codegen.compute(|| {
273 let outputs = self.prepare_outputs()?;
274 self.global_ctxt()?.peek_mut().enter(|tcx| {
275 tcx.analysis(LOCAL_CRATE).ok();
276
277 // Don't do code generation if there were any errors
278 self.session().compile_status()?;
279
280 // Hook for compile-fail tests.
281 Self::check_for_rustc_errors_attr(tcx);
282
283 Ok(passes::start_codegen(&***self.codegen_backend(), tcx, &*outputs.peek()))
284 })
285 })
286 }
287
288 /// Check for the `#[rustc_error]` annotation, which forces an error in codegen. This is used
289 /// to write compile-fail tests that actually test that compilation succeeds without reporting
290 /// an error.
291 fn check_for_rustc_errors_attr(tcx: TyCtxt<'_>) {
292 let def_id = match tcx.entry_fn(LOCAL_CRATE) {
293 Some((def_id, _)) => def_id,
294 _ => return,
295 };
296
297 let attrs = &*tcx.get_attrs(def_id.to_def_id());
298 let attrs = attrs.iter().filter(|attr| attr.check_name(sym::rustc_error));
299 for attr in attrs {
300 match attr.meta_item_list() {
301 // Check if there is a `#[rustc_error(delay_span_bug_from_inside_query)]`.
302 Some(list)
303 if list.iter().any(|list_item| {
304 matches!(
305 list_item.ident().map(|i| i.name),
306 Some(sym::delay_span_bug_from_inside_query)
307 )
308 }) =>
309 {
310 tcx.ensure().trigger_delay_span_bug(def_id);
311 }
312
313 // Bare `#[rustc_error]`.
314 None => {
315 tcx.sess.span_fatal(
316 tcx.def_span(def_id),
317 "fatal error triggered by #[rustc_error]",
318 );
319 }
320
321 // Some other attribute.
322 Some(_) => {
323 tcx.sess.span_warn(
324 tcx.def_span(def_id),
325 "unexpected annotation used with `#[rustc_error(...)]!",
326 );
327 }
328 }
329 }
330 }
331
332 pub fn linker(&'tcx self) -> Result<Linker> {
333 let dep_graph = self.dep_graph()?;
334 let prepare_outputs = self.prepare_outputs()?;
335 let ongoing_codegen = self.ongoing_codegen()?;
336
337 let sess = self.session().clone();
338 let codegen_backend = self.codegen_backend().clone();
339
340 Ok(Linker {
341 sess,
342 dep_graph: dep_graph.peek().clone(),
343 prepare_outputs: prepare_outputs.take(),
344 ongoing_codegen: ongoing_codegen.take(),
345 codegen_backend,
346 })
347 }
348 }
349
350 pub struct Linker {
351 sess: Lrc<Session>,
352 dep_graph: DepGraph,
353 prepare_outputs: OutputFilenames,
354 ongoing_codegen: Box<dyn Any>,
355 codegen_backend: Lrc<Box<dyn CodegenBackend>>,
356 }
357
358 impl Linker {
359 pub fn link(self) -> Result<()> {
360 let codegen_results =
361 self.codegen_backend.join_codegen(self.ongoing_codegen, &self.sess, &self.dep_graph)?;
362 let prof = self.sess.prof.clone();
363 let dep_graph = self.dep_graph;
364 prof.generic_activity("drop_dep_graph").run(move || drop(dep_graph));
365
366 if !self
367 .sess
368 .opts
369 .output_types
370 .keys()
371 .any(|&i| i == OutputType::Exe || i == OutputType::Metadata)
372 {
373 return Ok(());
374 }
375 self.codegen_backend.link(&self.sess, codegen_results, &self.prepare_outputs)
376 }
377 }
378
379 impl Compiler {
380 pub fn enter<F, T>(&self, f: F) -> T
381 where
382 F: for<'tcx> FnOnce(&'tcx Queries<'tcx>) -> T,
383 {
384 let mut _timer = None;
385 let queries = Queries::new(&self);
386 let ret = f(&queries);
387
388 if self.session().opts.debugging_opts.query_stats {
389 if let Ok(gcx) = queries.global_ctxt() {
390 gcx.peek_mut().print_stats();
391 }
392 }
393
394 _timer = Some(self.session().timer("free_global_ctxt"));
395
396 ret
397 }
398
399 // This method is different to all the other methods in `Compiler` because
400 // it lacks a `Queries` entry. It's also not currently used. It does serve
401 // as an example of how `Compiler` can be used, with additional steps added
402 // between some passes. And see `rustc_driver::run_compiler` for a more
403 // complex example.
404 pub fn compile(&self) -> Result<()> {
405 let linker = self.enter(|queries| {
406 queries.prepare_outputs()?;
407
408 if self.session().opts.output_types.contains_key(&OutputType::DepInfo)
409 && self.session().opts.output_types.len() == 1
410 {
411 return Ok(None);
412 }
413
414 queries.global_ctxt()?;
415
416 // Drop AST after creating GlobalCtxt to free memory.
417 mem::drop(queries.expansion()?.take());
418
419 queries.ongoing_codegen()?;
420
421 let linker = queries.linker()?;
422 Ok(Some(linker))
423 })?;
424
425 if let Some(linker) = linker {
426 linker.link()?
427 }
428
429 Ok(())
430 }
431 }