]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_ssa/src/traits/backend.rs
use iter:: before free functions
[rust.git] / compiler / rustc_codegen_ssa / src / traits / backend.rs
1 use super::write::WriteBackendMethods;
2 use super::CodegenObject;
3 use crate::ModuleCodegen;
4
5 use rustc_ast::expand::allocator::AllocatorKind;
6 use rustc_errors::ErrorReported;
7 use rustc_middle::dep_graph::DepGraph;
8 use rustc_middle::middle::cstore::{EncodedMetadata, MetadataLoaderDyn};
9 use rustc_middle::ty::layout::{HasTyCtxt, TyAndLayout};
10 use rustc_middle::ty::query::Providers;
11 use rustc_middle::ty::{Ty, TyCtxt};
12 use rustc_session::{
13     config::{self, OutputFilenames, PrintRequest},
14     Session,
15 };
16 use rustc_span::symbol::Symbol;
17 use rustc_target::abi::LayoutOf;
18
19 pub use rustc_data_structures::sync::MetadataRef;
20
21 use std::any::Any;
22 use std::sync::Arc;
23
24 pub trait BackendTypes {
25     type Value: CodegenObject;
26     type Function: CodegenObject;
27
28     type BasicBlock: Copy;
29     type Type: CodegenObject;
30     type Funclet;
31
32     // FIXME(eddyb) find a common convention for all of the debuginfo-related
33     // names (choose between `Dbg`, `Debug`, `DebugInfo`, `DI` etc.).
34     type DIScope: Copy;
35     type DIVariable: Copy;
36 }
37
38 pub trait Backend<'tcx>:
39     Sized + BackendTypes + HasTyCtxt<'tcx> + LayoutOf<Ty = Ty<'tcx>, TyAndLayout = TyAndLayout<'tcx>>
40 {
41 }
42
43 impl<'tcx, T> Backend<'tcx> for T where
44     Self: BackendTypes + HasTyCtxt<'tcx> + LayoutOf<Ty = Ty<'tcx>, TyAndLayout = TyAndLayout<'tcx>>
45 {
46 }
47
48 pub trait CodegenBackend {
49     fn init(&self, _sess: &Session) {}
50     fn print(&self, _req: PrintRequest, _sess: &Session) {}
51     fn target_features(&self, _sess: &Session) -> Vec<Symbol> {
52         vec![]
53     }
54     fn print_passes(&self) {}
55     fn print_version(&self) {}
56
57     fn metadata_loader(&self) -> Box<MetadataLoaderDyn>;
58     fn provide(&self, _providers: &mut Providers);
59     fn provide_extern(&self, _providers: &mut Providers);
60     fn codegen_crate<'tcx>(
61         &self,
62         tcx: TyCtxt<'tcx>,
63         metadata: EncodedMetadata,
64         need_metadata_module: bool,
65     ) -> Box<dyn Any>;
66
67     /// This is called on the returned `Box<dyn Any>` from `codegen_backend`
68     ///
69     /// # Panics
70     ///
71     /// Panics when the passed `Box<dyn Any>` was not returned by `codegen_backend`.
72     fn join_codegen(
73         &self,
74         ongoing_codegen: Box<dyn Any>,
75         sess: &Session,
76         dep_graph: &DepGraph,
77     ) -> Result<Box<dyn Any>, ErrorReported>;
78
79     /// This is called on the returned `Box<dyn Any>` from `join_codegen`
80     ///
81     /// # Panics
82     ///
83     /// Panics when the passed `Box<dyn Any>` was not returned by `join_codegen`.
84     fn link(
85         &self,
86         sess: &Session,
87         codegen_results: Box<dyn Any>,
88         outputs: &OutputFilenames,
89     ) -> Result<(), ErrorReported>;
90 }
91
92 pub trait ExtraBackendMethods: CodegenBackend + WriteBackendMethods + Sized + Send + Sync {
93     fn new_metadata(&self, sess: TyCtxt<'_>, mod_name: &str) -> Self::Module;
94     fn write_compressed_metadata<'tcx>(
95         &self,
96         tcx: TyCtxt<'tcx>,
97         metadata: &EncodedMetadata,
98         llvm_module: &mut Self::Module,
99     );
100     fn codegen_allocator<'tcx>(
101         &self,
102         tcx: TyCtxt<'tcx>,
103         mods: &mut Self::Module,
104         kind: AllocatorKind,
105     );
106     /// This generates the codegen unit and returns it along with
107     /// a `u64` giving an estimate of the unit's processing cost.
108     fn compile_codegen_unit(
109         &self,
110         tcx: TyCtxt<'_>,
111         cgu_name: Symbol,
112     ) -> (ModuleCodegen<Self::Module>, u64);
113     fn target_machine_factory(
114         &self,
115         sess: &Session,
116         opt_level: config::OptLevel,
117     ) -> Arc<dyn Fn() -> Result<Self::TargetMachine, String> + Send + Sync>;
118     fn target_cpu<'b>(&self, sess: &'b Session) -> &'b str;
119 }