]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/lib.rs
Rollup merge of #44562 - eddyb:ugh-rustdoc, r=nikomatsakis
[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 pub use back::symbol_names::provide;
68
69 pub use metadata::LlvmMetadataLoader;
70 pub use llvm_util::{init, target_features, print_version, print_passes, print, enable_llvm_debug};
71
72 use std::rc::Rc;
73
74 use rustc::hir::def_id::CrateNum;
75 use rustc::util::nodemap::{FxHashSet, FxHashMap};
76 use rustc::middle::cstore::{NativeLibrary, CrateSource, LibSource};
77
78 pub mod back {
79     mod archive;
80     mod command;
81     pub(crate) mod linker;
82     pub mod link;
83     mod lto;
84     pub(crate) mod symbol_export;
85     pub(crate) mod symbol_names;
86     pub mod write;
87     mod rpath;
88 }
89
90 mod diagnostics;
91
92 mod abi;
93 mod adt;
94 mod allocator;
95 mod asm;
96 mod assert_module_sources;
97 mod attributes;
98 mod base;
99 mod builder;
100 mod cabi_aarch64;
101 mod cabi_arm;
102 mod cabi_asmjs;
103 mod cabi_hexagon;
104 mod cabi_mips;
105 mod cabi_mips64;
106 mod cabi_msp430;
107 mod cabi_nvptx;
108 mod cabi_nvptx64;
109 mod cabi_powerpc;
110 mod cabi_powerpc64;
111 mod cabi_s390x;
112 mod cabi_sparc;
113 mod cabi_sparc64;
114 mod cabi_x86;
115 mod cabi_x86_64;
116 mod cabi_x86_win64;
117 mod callee;
118 mod collector;
119 mod common;
120 mod consts;
121 mod context;
122 mod debuginfo;
123 mod declare;
124 mod glue;
125 mod intrinsic;
126 mod llvm_util;
127 mod machine;
128 mod metadata;
129 mod meth;
130 mod mir;
131 mod monomorphize;
132 mod partitioning;
133 mod symbol_names_test;
134 mod time_graph;
135 mod trans_item;
136 mod tvec;
137 mod type_;
138 mod type_of;
139 mod value;
140
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.
147     name: String,
148     symbol_name_hash: u64,
149     pub source: ModuleSource,
150     pub kind: ModuleKind,
151 }
152
153 #[derive(Copy, Clone, Debug)]
154 pub enum ModuleKind {
155     Regular,
156     Metadata,
157     Allocator,
158 }
159
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,
165         };
166
167         CompiledModule {
168             name: self.name.clone(),
169             kind: self.kind,
170             symbol_name_hash: self.symbol_name_hash,
171             pre_existing,
172             emit_obj,
173             emit_bc,
174         }
175     }
176 }
177
178 impl Drop for ModuleTranslation {
179     fn drop(&mut self) {
180         match self.source {
181             ModuleSource::Preexisting(_) => {
182                 // Nothing to dispose.
183             },
184             ModuleSource::Translated(llvm) => {
185                 unsafe {
186                     llvm::LLVMDisposeModule(llvm.llmod);
187                     llvm::LLVMContextDispose(llvm.llcx);
188                 }
189             },
190         }
191     }
192 }
193
194 #[derive(Debug)]
195 pub struct CompiledModule {
196     pub name: String,
197     pub kind: ModuleKind,
198     pub symbol_name_hash: u64,
199     pub pre_existing: bool,
200     pub emit_obj: bool,
201     pub emit_bc: bool,
202 }
203
204 #[derive(Clone)]
205 pub enum ModuleSource {
206     /// Copy the `.o` files or whatever from the incr. comp. directory.
207     Preexisting(WorkProduct),
208
209     /// Rebuild from this LLVM module.
210     Translated(ModuleLlvm),
211 }
212
213 #[derive(Copy, Clone, Debug)]
214 pub struct ModuleLlvm {
215     llcx: llvm::ContextRef,
216     pub llmod: llvm::ModuleRef,
217 }
218
219 unsafe impl Send for ModuleTranslation { }
220 unsafe impl Sync for ModuleTranslation { }
221
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,
231 }
232
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)>,
247 }
248
249 __build_diagnostic_array! { librustc_trans, DIAGNOSTICS }