]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/lib.rs
Add transpose conversions for Option and Result
[rust.git] / src / librustc_trans / lib.rs
1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! The Rust compiler.
12 //!
13 //! # Note
14 //!
15 //! This API is completely unstable and subject to change.
16
17 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
18       html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
19       html_root_url = "https://doc.rust-lang.org/nightly/")]
20 #![deny(warnings)]
21
22 #![feature(box_patterns)]
23 #![feature(box_syntax)]
24 #![feature(custom_attribute)]
25 #![allow(unused_attributes)]
26 #![feature(i128_type)]
27 #![feature(i128)]
28 #![feature(inclusive_range)]
29 #![feature(inclusive_range_syntax)]
30 #![feature(libc)]
31 #![feature(quote)]
32 #![feature(rustc_diagnostic_macros)]
33 #![feature(slice_patterns)]
34 #![feature(conservative_impl_trait)]
35
36 use rustc::dep_graph::WorkProduct;
37 use syntax_pos::symbol::Symbol;
38
39 #[macro_use]
40 extern crate bitflags;
41 extern crate flate2;
42 extern crate libc;
43 #[macro_use] extern crate rustc;
44 extern crate jobserver;
45 extern crate num_cpus;
46 extern crate rustc_mir;
47 extern crate rustc_allocator;
48 extern crate rustc_apfloat;
49 extern crate rustc_back;
50 extern crate rustc_binaryen;
51 extern crate rustc_const_math;
52 extern crate rustc_data_structures;
53 extern crate rustc_demangle;
54 extern crate rustc_incremental;
55 extern crate rustc_llvm as llvm;
56 extern crate rustc_platform_intrinsics as intrinsics;
57 extern crate rustc_trans_utils;
58
59 #[macro_use] extern crate log;
60 #[macro_use] extern crate syntax;
61 extern crate syntax_pos;
62 extern crate rustc_errors as errors;
63 extern crate serialize;
64 #[cfg(windows)]
65 extern crate cc; // Used to locate MSVC
66 extern crate tempdir;
67
68 pub use base::trans_crate;
69 use back::bytecode::RLIB_BYTECODE_EXTENSION;
70
71 pub use metadata::LlvmMetadataLoader;
72 pub use llvm_util::{init, target_features, print_version, print_passes, print, enable_llvm_debug};
73
74 use std::any::Any;
75 use std::path::PathBuf;
76 use std::rc::Rc;
77 use std::sync::mpsc;
78
79 use rustc::dep_graph::DepGraph;
80 use rustc::hir::def_id::CrateNum;
81 use rustc::middle::cstore::MetadataLoader;
82 use rustc::middle::cstore::{NativeLibrary, CrateSource, LibSource};
83 use rustc::session::Session;
84 use rustc::session::config::{OutputFilenames, OutputType};
85 use rustc::ty::{self, TyCtxt};
86 use rustc::util::nodemap::{FxHashSet, FxHashMap};
87
88 use rustc_mir::monomorphize;
89
90 mod diagnostics;
91
92 pub mod back {
93     mod archive;
94     pub mod bytecode;
95     mod command;
96     pub(crate) mod linker;
97     pub mod link;
98     mod lto;
99     pub(crate) mod symbol_export;
100     pub(crate) mod symbol_names;
101     pub mod write;
102     mod rpath;
103 }
104
105 mod abi;
106 mod allocator;
107 mod asm;
108 mod assert_module_sources;
109 mod attributes;
110 mod base;
111 mod builder;
112 mod cabi_aarch64;
113 mod cabi_arm;
114 mod cabi_asmjs;
115 mod cabi_hexagon;
116 mod cabi_mips;
117 mod cabi_mips64;
118 mod cabi_msp430;
119 mod cabi_nvptx;
120 mod cabi_nvptx64;
121 mod cabi_powerpc;
122 mod cabi_powerpc64;
123 mod cabi_s390x;
124 mod cabi_sparc;
125 mod cabi_sparc64;
126 mod cabi_x86;
127 mod cabi_x86_64;
128 mod cabi_x86_win64;
129 mod callee;
130 mod common;
131 mod consts;
132 mod context;
133 mod debuginfo;
134 mod declare;
135 mod glue;
136 mod intrinsic;
137 mod llvm_util;
138 mod metadata;
139 mod meth;
140 mod mir;
141 mod symbol_names_test;
142 mod time_graph;
143 mod trans_item;
144 mod type_;
145 mod type_of;
146 mod value;
147
148 pub struct LlvmTransCrate(());
149
150 impl LlvmTransCrate {
151     pub fn new() -> Self {
152         LlvmTransCrate(())
153     }
154 }
155
156 impl rustc_trans_utils::trans_crate::TransCrate for LlvmTransCrate {
157     type MetadataLoader = metadata::LlvmMetadataLoader;
158     type OngoingCrateTranslation = back::write::OngoingCrateTranslation;
159     type TranslatedCrate = CrateTranslation;
160
161     fn metadata_loader() -> Box<MetadataLoader> {
162         box metadata::LlvmMetadataLoader
163     }
164
165     fn provide(providers: &mut ty::maps::Providers) {
166         back::symbol_names::provide(providers);
167         back::symbol_export::provide(providers);
168         base::provide(providers);
169     }
170
171     fn provide_extern(providers: &mut ty::maps::Providers) {
172         back::symbol_export::provide_extern(providers);
173     }
174
175     fn trans_crate<'a, 'tcx>(
176         tcx: TyCtxt<'a, 'tcx, 'tcx>,
177         rx: mpsc::Receiver<Box<Any + Send>>
178     ) -> Self::OngoingCrateTranslation {
179         base::trans_crate(tcx, rx)
180     }
181
182     fn join_trans(
183         trans: Self::OngoingCrateTranslation,
184         sess: &Session,
185         dep_graph: &DepGraph
186     ) -> Self::TranslatedCrate {
187         trans.join(sess, dep_graph)
188     }
189
190     fn link_binary(sess: &Session, trans: &Self::TranslatedCrate, outputs: &OutputFilenames) {
191         back::link::link_binary(sess, trans, outputs, &trans.crate_name.as_str());
192     }
193
194     fn dump_incremental_data(trans: &Self::TranslatedCrate) {
195         back::write::dump_incremental_data(trans);
196     }
197 }
198
199 pub struct ModuleTranslation {
200     /// The name of the module. When the crate may be saved between
201     /// compilations, incremental compilation requires that name be
202     /// unique amongst **all** crates.  Therefore, it should contain
203     /// something unique to this crate (e.g., a module path) as well
204     /// as the crate name and disambiguator.
205     name: String,
206     llmod_id: String,
207     pub source: ModuleSource,
208     pub kind: ModuleKind,
209 }
210
211 #[derive(Copy, Clone, Debug, PartialEq)]
212 pub enum ModuleKind {
213     Regular,
214     Metadata,
215     Allocator,
216 }
217
218 impl ModuleTranslation {
219     pub fn llvm(&self) -> Option<&ModuleLlvm> {
220         match self.source {
221             ModuleSource::Translated(ref llvm) => Some(llvm),
222             ModuleSource::Preexisting(_) => None,
223         }
224     }
225
226     pub fn into_compiled_module(self,
227                                 emit_obj: bool,
228                                 emit_bc: bool,
229                                 emit_bc_compressed: bool,
230                                 outputs: &OutputFilenames) -> CompiledModule {
231         let pre_existing = match self.source {
232             ModuleSource::Preexisting(_) => true,
233             ModuleSource::Translated(_) => false,
234         };
235         let object = if emit_obj {
236             Some(outputs.temp_path(OutputType::Object, Some(&self.name)))
237         } else {
238             None
239         };
240         let bytecode = if emit_bc {
241             Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name)))
242         } else {
243             None
244         };
245         let bytecode_compressed = if emit_bc_compressed {
246             Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name))
247                     .with_extension(RLIB_BYTECODE_EXTENSION))
248         } else {
249             None
250         };
251
252         CompiledModule {
253             llmod_id: self.llmod_id,
254             name: self.name.clone(),
255             kind: self.kind,
256             pre_existing,
257             object,
258             bytecode,
259             bytecode_compressed,
260         }
261     }
262 }
263
264 #[derive(Debug)]
265 pub struct CompiledModule {
266     pub name: String,
267     pub llmod_id: String,
268     pub kind: ModuleKind,
269     pub pre_existing: bool,
270     pub object: Option<PathBuf>,
271     pub bytecode: Option<PathBuf>,
272     pub bytecode_compressed: Option<PathBuf>,
273 }
274
275 pub enum ModuleSource {
276     /// Copy the `.o` files or whatever from the incr. comp. directory.
277     Preexisting(WorkProduct),
278
279     /// Rebuild from this LLVM module.
280     Translated(ModuleLlvm),
281 }
282
283 #[derive(Debug)]
284 pub struct ModuleLlvm {
285     llcx: llvm::ContextRef,
286     pub llmod: llvm::ModuleRef,
287     tm: llvm::TargetMachineRef,
288 }
289
290 unsafe impl Send for ModuleLlvm { }
291 unsafe impl Sync for ModuleLlvm { }
292
293 impl Drop for ModuleLlvm {
294     fn drop(&mut self) {
295         unsafe {
296             llvm::LLVMDisposeModule(self.llmod);
297             llvm::LLVMContextDispose(self.llcx);
298             llvm::LLVMRustDisposeTargetMachine(self.tm);
299         }
300     }
301 }
302
303 pub struct CrateTranslation {
304     pub crate_name: Symbol,
305     pub modules: Vec<CompiledModule>,
306     allocator_module: Option<CompiledModule>,
307     metadata_module: CompiledModule,
308     pub link: rustc::middle::cstore::LinkMeta,
309     pub metadata: rustc::middle::cstore::EncodedMetadata,
310     windows_subsystem: Option<String>,
311     linker_info: back::linker::LinkerInfo,
312     crate_info: CrateInfo,
313 }
314
315 // Misc info we load from metadata to persist beyond the tcx
316 pub struct CrateInfo {
317     panic_runtime: Option<CrateNum>,
318     compiler_builtins: Option<CrateNum>,
319     profiler_runtime: Option<CrateNum>,
320     sanitizer_runtime: Option<CrateNum>,
321     is_no_builtins: FxHashSet<CrateNum>,
322     native_libraries: FxHashMap<CrateNum, Rc<Vec<NativeLibrary>>>,
323     crate_name: FxHashMap<CrateNum, String>,
324     used_libraries: Rc<Vec<NativeLibrary>>,
325     link_args: Rc<Vec<String>>,
326     used_crate_source: FxHashMap<CrateNum, Rc<CrateSource>>,
327     used_crates_static: Vec<(CrateNum, LibSource)>,
328     used_crates_dynamic: Vec<(CrateNum, LibSource)>,
329 }
330
331 __build_diagnostic_array! { librustc_trans, DIAGNOSTICS }