]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/lib.rs
Auto merge of #43931 - eddyb:const-local-key, r=alexcrichton
[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 pub mod back {
70     mod archive;
71     mod command;
72     pub(crate) mod linker;
73     pub mod link;
74     mod lto;
75     pub(crate) mod symbol_export;
76     pub(crate) mod symbol_names;
77     pub mod write;
78     mod rpath;
79 }
80
81 mod diagnostics;
82
83 mod abi;
84 mod adt;
85 mod allocator;
86 mod asm;
87 mod assert_module_sources;
88 mod attributes;
89 mod base;
90 mod builder;
91 mod cabi_aarch64;
92 mod cabi_arm;
93 mod cabi_asmjs;
94 mod cabi_hexagon;
95 mod cabi_mips;
96 mod cabi_mips64;
97 mod cabi_msp430;
98 mod cabi_nvptx;
99 mod cabi_nvptx64;
100 mod cabi_powerpc;
101 mod cabi_powerpc64;
102 mod cabi_s390x;
103 mod cabi_sparc;
104 mod cabi_sparc64;
105 mod cabi_x86;
106 mod cabi_x86_64;
107 mod cabi_x86_win64;
108 mod callee;
109 mod collector;
110 mod common;
111 mod consts;
112 mod context;
113 mod debuginfo;
114 mod declare;
115 mod glue;
116 mod intrinsic;
117 mod llvm_util;
118 mod machine;
119 mod metadata;
120 mod meth;
121 mod mir;
122 mod monomorphize;
123 mod partitioning;
124 mod symbol_names_test;
125 mod time_graph;
126 mod trans_item;
127 mod tvec;
128 mod type_;
129 mod type_of;
130 mod value;
131
132 pub struct ModuleTranslation {
133     /// The name of the module. When the crate may be saved between
134     /// compilations, incremental compilation requires that name be
135     /// unique amongst **all** crates.  Therefore, it should contain
136     /// something unique to this crate (e.g., a module path) as well
137     /// as the crate name and disambiguator.
138     name: String,
139     symbol_name_hash: u64,
140     pub source: ModuleSource,
141     pub kind: ModuleKind,
142 }
143
144 #[derive(Copy, Clone, Debug)]
145 pub enum ModuleKind {
146     Regular,
147     Metadata,
148     Allocator,
149 }
150
151 impl ModuleTranslation {
152     pub fn into_compiled_module(self, emit_obj: bool, emit_bc: bool) -> CompiledModule {
153         let pre_existing = match self.source {
154             ModuleSource::Preexisting(_) => true,
155             ModuleSource::Translated(_) => false,
156         };
157
158         CompiledModule {
159             name: self.name.clone(),
160             kind: self.kind,
161             symbol_name_hash: self.symbol_name_hash,
162             pre_existing,
163             emit_obj,
164             emit_bc,
165         }
166     }
167 }
168
169 impl Drop for ModuleTranslation {
170     fn drop(&mut self) {
171         match self.source {
172             ModuleSource::Preexisting(_) => {
173                 // Nothing to dispose.
174             },
175             ModuleSource::Translated(llvm) => {
176                 unsafe {
177                     llvm::LLVMDisposeModule(llvm.llmod);
178                     llvm::LLVMContextDispose(llvm.llcx);
179                 }
180             },
181         }
182     }
183 }
184
185 #[derive(Debug)]
186 pub struct CompiledModule {
187     pub name: String,
188     pub kind: ModuleKind,
189     pub symbol_name_hash: u64,
190     pub pre_existing: bool,
191     pub emit_obj: bool,
192     pub emit_bc: bool,
193 }
194
195 #[derive(Clone)]
196 pub enum ModuleSource {
197     /// Copy the `.o` files or whatever from the incr. comp. directory.
198     Preexisting(WorkProduct),
199
200     /// Rebuild from this LLVM module.
201     Translated(ModuleLlvm),
202 }
203
204 #[derive(Copy, Clone, Debug)]
205 pub struct ModuleLlvm {
206     llcx: llvm::ContextRef,
207     pub llmod: llvm::ModuleRef,
208 }
209
210 unsafe impl Send for ModuleTranslation { }
211 unsafe impl Sync for ModuleTranslation { }
212
213 pub struct CrateTranslation {
214     pub crate_name: Symbol,
215     pub modules: Vec<CompiledModule>,
216     allocator_module: Option<CompiledModule>,
217     pub link: rustc::middle::cstore::LinkMeta,
218     pub metadata: rustc::middle::cstore::EncodedMetadata,
219     windows_subsystem: Option<String>,
220     linker_info: back::linker::LinkerInfo
221 }
222
223 __build_diagnostic_array! { librustc_trans, DIAGNOSTICS }