]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/lib.rs
rustc: Make trans collect/partition a query
[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(libc)]
28 #![feature(quote)]
29 #![feature(rustc_diagnostic_macros)]
30 #![feature(slice_patterns)]
31 #![feature(conservative_impl_trait)]
32
33 #![cfg_attr(stage0, feature(const_fn))]
34 #![cfg_attr(not(stage0), feature(const_atomic_bool_new))]
35 #![cfg_attr(not(stage0), feature(const_once_new))]
36
37 use rustc::dep_graph::WorkProduct;
38 use syntax_pos::symbol::Symbol;
39
40 extern crate flate2;
41 extern crate libc;
42 extern crate owning_ref;
43 #[macro_use] extern crate rustc;
44 extern crate rustc_allocator;
45 extern crate rustc_back;
46 extern crate rustc_data_structures;
47 extern crate rustc_incremental;
48 extern crate rustc_llvm as llvm;
49 extern crate rustc_platform_intrinsics as intrinsics;
50 extern crate rustc_const_math;
51 #[macro_use]
52 #[no_link]
53 extern crate rustc_bitflags;
54 extern crate rustc_demangle;
55 extern crate jobserver;
56 extern crate num_cpus;
57
58 #[macro_use] extern crate log;
59 #[macro_use] extern crate syntax;
60 extern crate syntax_pos;
61 extern crate rustc_errors as errors;
62 extern crate serialize;
63 #[cfg(windows)]
64 extern crate gcc; // Used to locate MSVC, not gcc :)
65
66 pub use base::trans_crate;
67
68 pub use metadata::LlvmMetadataLoader;
69 pub use llvm_util::{init, target_features, print_version, print_passes, print, enable_llvm_debug};
70
71 use std::rc::Rc;
72 use std::sync::Arc;
73
74 use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
75 use rustc::middle::cstore::{NativeLibrary, CrateSource, LibSource};
76 use rustc::ty::maps::Providers;
77 use rustc::util::nodemap::{FxHashSet, FxHashMap};
78
79 pub mod back {
80     mod archive;
81     mod command;
82     pub(crate) mod linker;
83     pub mod link;
84     mod lto;
85     pub(crate) mod symbol_export;
86     pub(crate) mod symbol_names;
87     pub mod write;
88     mod rpath;
89 }
90
91 mod diagnostics;
92
93 mod abi;
94 mod adt;
95 mod allocator;
96 mod asm;
97 mod assert_module_sources;
98 mod attributes;
99 mod base;
100 mod builder;
101 mod cabi_aarch64;
102 mod cabi_arm;
103 mod cabi_asmjs;
104 mod cabi_hexagon;
105 mod cabi_mips;
106 mod cabi_mips64;
107 mod cabi_msp430;
108 mod cabi_nvptx;
109 mod cabi_nvptx64;
110 mod cabi_powerpc;
111 mod cabi_powerpc64;
112 mod cabi_s390x;
113 mod cabi_sparc;
114 mod cabi_sparc64;
115 mod cabi_x86;
116 mod cabi_x86_64;
117 mod cabi_x86_win64;
118 mod callee;
119 mod collector;
120 mod common;
121 mod consts;
122 mod context;
123 mod debuginfo;
124 mod declare;
125 mod glue;
126 mod intrinsic;
127 mod llvm_util;
128 mod machine;
129 mod metadata;
130 mod meth;
131 mod mir;
132 mod monomorphize;
133 mod partitioning;
134 mod symbol_names_test;
135 mod time_graph;
136 mod trans_item;
137 mod tvec;
138 mod type_;
139 mod type_of;
140 mod value;
141
142 pub struct ModuleTranslation {
143     /// The name of the module. When the crate may be saved between
144     /// compilations, incremental compilation requires that name be
145     /// unique amongst **all** crates.  Therefore, it should contain
146     /// something unique to this crate (e.g., a module path) as well
147     /// as the crate name and disambiguator.
148     name: String,
149     symbol_name_hash: u64,
150     pub source: ModuleSource,
151     pub kind: ModuleKind,
152 }
153
154 #[derive(Copy, Clone, Debug)]
155 pub enum ModuleKind {
156     Regular,
157     Metadata,
158     Allocator,
159 }
160
161 impl ModuleTranslation {
162     pub fn into_compiled_module(self, emit_obj: bool, emit_bc: bool) -> CompiledModule {
163         let pre_existing = match self.source {
164             ModuleSource::Preexisting(_) => true,
165             ModuleSource::Translated(_) => false,
166         };
167
168         CompiledModule {
169             name: self.name.clone(),
170             kind: self.kind,
171             symbol_name_hash: self.symbol_name_hash,
172             pre_existing,
173             emit_obj,
174             emit_bc,
175         }
176     }
177 }
178
179 impl Drop for ModuleTranslation {
180     fn drop(&mut self) {
181         match self.source {
182             ModuleSource::Preexisting(_) => {
183                 // Nothing to dispose.
184             },
185             ModuleSource::Translated(llvm) => {
186                 unsafe {
187                     llvm::LLVMDisposeModule(llvm.llmod);
188                     llvm::LLVMContextDispose(llvm.llcx);
189                 }
190             },
191         }
192     }
193 }
194
195 #[derive(Debug)]
196 pub struct CompiledModule {
197     pub name: String,
198     pub kind: ModuleKind,
199     pub symbol_name_hash: u64,
200     pub pre_existing: bool,
201     pub emit_obj: bool,
202     pub emit_bc: bool,
203 }
204
205 #[derive(Clone)]
206 pub enum ModuleSource {
207     /// Copy the `.o` files or whatever from the incr. comp. directory.
208     Preexisting(WorkProduct),
209
210     /// Rebuild from this LLVM module.
211     Translated(ModuleLlvm),
212 }
213
214 #[derive(Copy, Clone, Debug)]
215 pub struct ModuleLlvm {
216     llcx: llvm::ContextRef,
217     pub llmod: llvm::ModuleRef,
218 }
219
220 unsafe impl Send for ModuleTranslation { }
221 unsafe impl Sync for ModuleTranslation { }
222
223 pub struct CrateTranslation {
224     pub crate_name: Symbol,
225     pub modules: Vec<CompiledModule>,
226     allocator_module: Option<CompiledModule>,
227     pub link: rustc::middle::cstore::LinkMeta,
228     pub metadata: rustc::middle::cstore::EncodedMetadata,
229     windows_subsystem: Option<String>,
230     linker_info: back::linker::LinkerInfo,
231     crate_info: CrateInfo,
232 }
233
234 // Misc info we load from metadata to persist beyond the tcx
235 pub struct CrateInfo {
236     panic_runtime: Option<CrateNum>,
237     compiler_builtins: Option<CrateNum>,
238     profiler_runtime: Option<CrateNum>,
239     sanitizer_runtime: Option<CrateNum>,
240     is_no_builtins: FxHashSet<CrateNum>,
241     native_libraries: FxHashMap<CrateNum, Rc<Vec<NativeLibrary>>>,
242     crate_name: FxHashMap<CrateNum, String>,
243     used_libraries: Rc<Vec<NativeLibrary>>,
244     link_args: Rc<Vec<String>>,
245     used_crate_source: FxHashMap<CrateNum, Rc<CrateSource>>,
246     used_crates_static: Vec<(CrateNum, LibSource)>,
247     used_crates_dynamic: Vec<(CrateNum, LibSource)>,
248 }
249
250 __build_diagnostic_array! { librustc_trans, DIAGNOSTICS }
251
252 pub fn provide_local(providers: &mut Providers) {
253     back::symbol_names::provide(providers);
254     base::provide(providers);
255     providers.exported_symbol_set = |tcx, cnum| {
256         assert_eq!(cnum, LOCAL_CRATE);
257         Arc::new(back::symbol_export::compute(tcx))
258     };
259 }
260
261 pub fn provide_extern(providers: &mut Providers) {
262     back::symbol_names::provide(providers);
263 }