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.
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.
11 //! The Rust compiler.
15 //! This API is completely unstable and subject to change.
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/")]
22 #![feature(box_patterns)]
23 #![feature(box_syntax)]
24 #![feature(custom_attribute)]
25 #![allow(unused_attributes)]
26 #![feature(i128_type)]
29 #![feature(rustc_diagnostic_macros)]
30 #![feature(slice_patterns)]
31 #![feature(conservative_impl_trait)]
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))]
37 use rustc::dep_graph::WorkProduct;
38 use syntax_pos::symbol::Symbol;
41 extern crate bitflags;
44 extern crate owning_ref;
45 #[macro_use] extern crate rustc;
46 extern crate rustc_allocator;
47 extern crate rustc_back;
48 extern crate rustc_data_structures;
49 extern crate rustc_incremental;
50 extern crate rustc_llvm as llvm;
51 extern crate rustc_platform_intrinsics as intrinsics;
52 extern crate rustc_const_math;
53 extern crate rustc_trans_utils;
54 extern crate rustc_demangle;
55 extern crate jobserver;
56 extern crate num_cpus;
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;
64 extern crate cc; // Used to locate MSVC
66 pub use base::trans_crate;
68 pub use metadata::LlvmMetadataLoader;
69 pub use llvm_util::{init, target_features, print_version, print_passes, print, enable_llvm_debug};
72 use std::path::PathBuf;
76 use rustc::dep_graph::DepGraph;
77 use rustc::hir::def_id::CrateNum;
78 use rustc::middle::cstore::MetadataLoader;
79 use rustc::middle::cstore::{NativeLibrary, CrateSource, LibSource};
80 use rustc::session::Session;
81 use rustc::session::config::{OutputFilenames, OutputType};
82 use rustc::ty::maps::Providers;
83 use rustc::ty::{self, TyCtxt};
84 use rustc::util::nodemap::{FxHashSet, FxHashMap};
92 pub(crate) mod linker;
95 pub(crate) mod symbol_export;
96 pub(crate) mod symbol_names;
105 mod assert_module_sources;
142 mod symbol_names_test;
150 pub struct LlvmTransCrate(());
152 impl LlvmTransCrate {
153 pub fn new() -> Self {
158 impl rustc_trans_utils::trans_crate::TransCrate for LlvmTransCrate {
159 type MetadataLoader = metadata::LlvmMetadataLoader;
160 type OngoingCrateTranslation = back::write::OngoingCrateTranslation;
161 type TranslatedCrate = CrateTranslation;
163 fn metadata_loader() -> Box<MetadataLoader> {
164 box metadata::LlvmMetadataLoader
167 fn provide_local(providers: &mut ty::maps::Providers) {
168 provide_local(providers);
171 fn provide_extern(providers: &mut ty::maps::Providers) {
172 provide_extern(providers);
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)
183 trans: Self::OngoingCrateTranslation,
186 ) -> Self::TranslatedCrate {
187 trans.join(sess, dep_graph)
190 fn link_binary(sess: &Session, trans: &Self::TranslatedCrate, outputs: &OutputFilenames) {
191 back::link::link_binary(sess, trans, outputs, &trans.crate_name.as_str());
194 fn dump_incremental_data(trans: &Self::TranslatedCrate) {
195 back::write::dump_incremental_data(trans);
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.
207 pub source: ModuleSource,
208 pub kind: ModuleKind,
211 #[derive(Copy, Clone, Debug, PartialEq)]
212 pub enum ModuleKind {
218 impl ModuleTranslation {
219 pub fn llvm(&self) -> Option<&ModuleLlvm> {
221 ModuleSource::Translated(ref llvm) => Some(llvm),
222 ModuleSource::Preexisting(_) => None,
226 pub fn into_compiled_module(self,
229 outputs: &OutputFilenames) -> CompiledModule {
230 let pre_existing = match self.source {
231 ModuleSource::Preexisting(_) => true,
232 ModuleSource::Translated(_) => false,
234 let object = outputs.temp_path(OutputType::Object, Some(&self.name));
237 llmod_id: self.llmod_id,
238 name: self.name.clone(),
249 pub struct CompiledModule {
251 pub llmod_id: String,
253 pub kind: ModuleKind,
254 pub pre_existing: bool,
259 pub enum ModuleSource {
260 /// Copy the `.o` files or whatever from the incr. comp. directory.
261 Preexisting(WorkProduct),
263 /// Rebuild from this LLVM module.
264 Translated(ModuleLlvm),
268 pub struct ModuleLlvm {
269 llcx: llvm::ContextRef,
270 pub llmod: llvm::ModuleRef,
271 tm: llvm::TargetMachineRef,
274 unsafe impl Send for ModuleLlvm { }
275 unsafe impl Sync for ModuleLlvm { }
277 impl Drop for ModuleLlvm {
280 llvm::LLVMDisposeModule(self.llmod);
281 llvm::LLVMContextDispose(self.llcx);
282 llvm::LLVMRustDisposeTargetMachine(self.tm);
287 pub struct CrateTranslation {
288 pub crate_name: Symbol,
289 pub modules: Vec<CompiledModule>,
290 allocator_module: Option<CompiledModule>,
291 pub link: rustc::middle::cstore::LinkMeta,
292 pub metadata: rustc::middle::cstore::EncodedMetadata,
293 windows_subsystem: Option<String>,
294 linker_info: back::linker::LinkerInfo,
295 crate_info: CrateInfo,
298 // Misc info we load from metadata to persist beyond the tcx
299 pub struct CrateInfo {
300 panic_runtime: Option<CrateNum>,
301 compiler_builtins: Option<CrateNum>,
302 profiler_runtime: Option<CrateNum>,
303 sanitizer_runtime: Option<CrateNum>,
304 is_no_builtins: FxHashSet<CrateNum>,
305 native_libraries: FxHashMap<CrateNum, Rc<Vec<NativeLibrary>>>,
306 crate_name: FxHashMap<CrateNum, String>,
307 used_libraries: Rc<Vec<NativeLibrary>>,
308 link_args: Rc<Vec<String>>,
309 used_crate_source: FxHashMap<CrateNum, Rc<CrateSource>>,
310 used_crates_static: Vec<(CrateNum, LibSource)>,
311 used_crates_dynamic: Vec<(CrateNum, LibSource)>,
314 __build_diagnostic_array! { librustc_trans, DIAGNOSTICS }
316 pub fn provide_local(providers: &mut Providers) {
317 back::symbol_names::provide(providers);
318 back::symbol_export::provide_local(providers);
319 base::provide_local(providers);
322 pub fn provide_extern(providers: &mut Providers) {
323 back::symbol_names::provide(providers);
324 back::symbol_export::provide_extern(providers);
325 base::provide_extern(providers);