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;
68 pub use metadata::LlvmMetadataLoader;
69 pub use llvm_util::{init, target_features, print_version, print_passes, print, enable_llvm_debug};
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};
82 pub(crate) mod linker;
85 pub(crate) mod symbol_export;
86 pub(crate) mod symbol_names;
97 mod assert_module_sources;
134 mod symbol_names_test;
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.
149 symbol_name_hash: u64,
150 pub source: ModuleSource,
151 pub kind: ModuleKind,
154 #[derive(Copy, Clone, Debug)]
155 pub enum ModuleKind {
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,
169 name: self.name.clone(),
171 symbol_name_hash: self.symbol_name_hash,
179 impl Drop for ModuleTranslation {
182 ModuleSource::Preexisting(_) => {
183 // Nothing to dispose.
185 ModuleSource::Translated(llvm) => {
187 llvm::LLVMDisposeModule(llvm.llmod);
188 llvm::LLVMContextDispose(llvm.llcx);
196 pub struct CompiledModule {
198 pub kind: ModuleKind,
199 pub symbol_name_hash: u64,
200 pub pre_existing: bool,
206 pub enum ModuleSource {
207 /// Copy the `.o` files or whatever from the incr. comp. directory.
208 Preexisting(WorkProduct),
210 /// Rebuild from this LLVM module.
211 Translated(ModuleLlvm),
214 #[derive(Copy, Clone, Debug)]
215 pub struct ModuleLlvm {
216 llcx: llvm::ContextRef,
217 pub llmod: llvm::ModuleRef,
220 unsafe impl Send for ModuleTranslation { }
221 unsafe impl Sync for ModuleTranslation { }
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,
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)>,
250 __build_diagnostic_array! { librustc_trans, DIAGNOSTICS }
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))
261 pub fn provide_extern(providers: &mut Providers) {
262 back::symbol_names::provide(providers);