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