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;
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;
53 extern crate rustc_bitflags;
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 gcc; // Used to locate MSVC, not gcc :)
66 pub use base::trans_crate;
67 pub use back::symbol_names::provide;
69 pub use metadata::LlvmMetadataLoader;
70 pub use llvm_util::{init, target_features, print_version, print_passes, print, enable_llvm_debug};
74 use rustc::hir::def_id::CrateNum;
75 use rustc::util::nodemap::{FxHashSet, FxHashMap};
76 use rustc::middle::cstore::{NativeLibrary, CrateSource, LibSource};
81 pub(crate) mod linker;
84 pub(crate) mod symbol_export;
85 pub(crate) mod symbol_names;
96 mod assert_module_sources;
133 mod symbol_names_test;
141 pub struct ModuleTranslation {
142 /// The name of the module. When the crate may be saved between
143 /// compilations, incremental compilation requires that name be
144 /// unique amongst **all** crates. Therefore, it should contain
145 /// something unique to this crate (e.g., a module path) as well
146 /// as the crate name and disambiguator.
148 symbol_name_hash: u64,
149 pub source: ModuleSource,
150 pub kind: ModuleKind,
153 #[derive(Copy, Clone, Debug)]
154 pub enum ModuleKind {
160 impl ModuleTranslation {
161 pub fn into_compiled_module(self, emit_obj: bool, emit_bc: bool) -> CompiledModule {
162 let pre_existing = match self.source {
163 ModuleSource::Preexisting(_) => true,
164 ModuleSource::Translated(_) => false,
168 name: self.name.clone(),
170 symbol_name_hash: self.symbol_name_hash,
178 impl Drop for ModuleTranslation {
181 ModuleSource::Preexisting(_) => {
182 // Nothing to dispose.
184 ModuleSource::Translated(llvm) => {
186 llvm::LLVMDisposeModule(llvm.llmod);
187 llvm::LLVMContextDispose(llvm.llcx);
195 pub struct CompiledModule {
197 pub kind: ModuleKind,
198 pub symbol_name_hash: u64,
199 pub pre_existing: bool,
205 pub enum ModuleSource {
206 /// Copy the `.o` files or whatever from the incr. comp. directory.
207 Preexisting(WorkProduct),
209 /// Rebuild from this LLVM module.
210 Translated(ModuleLlvm),
213 #[derive(Copy, Clone, Debug)]
214 pub struct ModuleLlvm {
215 llcx: llvm::ContextRef,
216 pub llmod: llvm::ModuleRef,
219 unsafe impl Send for ModuleTranslation { }
220 unsafe impl Sync for ModuleTranslation { }
222 pub struct CrateTranslation {
223 pub crate_name: Symbol,
224 pub modules: Vec<CompiledModule>,
225 allocator_module: Option<CompiledModule>,
226 pub link: rustc::middle::cstore::LinkMeta,
227 pub metadata: rustc::middle::cstore::EncodedMetadata,
228 windows_subsystem: Option<String>,
229 linker_info: back::linker::LinkerInfo,
230 crate_info: CrateInfo,
233 // Misc info we load from metadata to persist beyond the tcx
234 pub struct CrateInfo {
235 panic_runtime: Option<CrateNum>,
236 compiler_builtins: Option<CrateNum>,
237 profiler_runtime: Option<CrateNum>,
238 sanitizer_runtime: Option<CrateNum>,
239 is_no_builtins: FxHashSet<CrateNum>,
240 native_libraries: FxHashMap<CrateNum, Rc<Vec<NativeLibrary>>>,
241 crate_name: FxHashMap<CrateNum, String>,
242 used_libraries: Rc<Vec<NativeLibrary>>,
243 link_args: Rc<Vec<String>>,
244 used_crate_source: FxHashMap<CrateNum, Rc<CrateSource>>,
245 used_crates_static: Vec<(CrateNum, LibSource)>,
246 used_crates_dynamic: Vec<(CrateNum, LibSource)>,
249 __build_diagnostic_array! { librustc_trans, DIAGNOSTICS }