lib.rs 8.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

S
Steve Klabnik 已提交
11 12 13 14 15
//! The Rust compiler.
//!
//! # Note
//!
//! 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",
A
Alex Crichton 已提交
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)]
A
Alex Crichton 已提交
23
#![feature(box_syntax)]
N
Nick Cameron 已提交
24 25
#![feature(custom_attribute)]
#![allow(unused_attributes)]
A
Alex Crichton 已提交
26
#![feature(i128_type)]
27
#![feature(libc)]
A
Alex Crichton 已提交
28 29
#![feature(quote)]
#![feature(rustc_diagnostic_macros)]
N
Niko Matsakis 已提交
30
#![feature(slice_patterns)]
M
Mark-Simulacrum 已提交
31
#![feature(conservative_impl_trait)]
32

33 34 35 36
#![cfg_attr(stage0, feature(const_fn))]
#![cfg_attr(not(stage0), feature(const_atomic_bool_new))]
#![cfg_attr(not(stage0), feature(const_once_new))]

37
use rustc::dep_graph::WorkProduct;
38
use syntax_pos::symbol::Symbol;
39

40 41
#[macro_use]
extern crate bitflags;
A
Alex Crichton 已提交
42
extern crate flate2;
43
extern crate libc;
44
extern crate owning_ref;
45
#[macro_use] extern crate rustc;
46
extern crate rustc_allocator;
47
extern crate rustc_back;
48
extern crate rustc_data_structures;
N
Niko Matsakis 已提交
49
extern crate rustc_incremental;
50
extern crate rustc_llvm as llvm;
51
extern crate rustc_platform_intrinsics as intrinsics;
52
extern crate rustc_const_math;
B
bjorn3 已提交
53
extern crate rustc_trans_traits;
54
extern crate rustc_trans_utils;
55
extern crate rustc_demangle;
56
extern crate jobserver;
57
extern crate num_cpus;
58

A
Alex Crichton 已提交
59 60
#[macro_use] extern crate log;
#[macro_use] extern crate syntax;
61 62
extern crate syntax_pos;
extern crate rustc_errors as errors;
63
extern crate serialize;
B
Brian Anderson 已提交
64 65
#[cfg(windows)]
extern crate gcc; // Used to locate MSVC, not gcc :)
66

67 68
pub use base::trans_crate;

69
pub use metadata::LlvmMetadataLoader;
70
pub use llvm_util::{init, target_features, print_version, print_passes, print, enable_llvm_debug};
71

72 73
use std::rc::Rc;

74
use rustc::hir::def_id::CrateNum;
75
use rustc::middle::cstore::{NativeLibrary, CrateSource, LibSource};
76 77
use rustc::ty::maps::Providers;
use rustc::util::nodemap::{FxHashSet, FxHashMap};
78

79 80
mod diagnostics;

81
pub mod back {
R
Robin Kruppe 已提交
82
    mod archive;
83
    mod command;
R
Robin Kruppe 已提交
84
    pub(crate) mod linker;
85
    pub mod link;
R
Robin Kruppe 已提交
86 87 88
    mod lto;
    pub(crate) mod symbol_export;
    pub(crate) mod symbol_names;
89
    pub mod write;
90
    mod rpath;
91 92
}

93 94
mod abi;
mod adt;
95
mod allocator;
96
mod asm;
97
mod assert_module_sources;
98 99 100 101 102 103
mod attributes;
mod base;
mod builder;
mod cabi_aarch64;
mod cabi_arm;
mod cabi_asmjs;
M
Michael Wu 已提交
104
mod cabi_hexagon;
105
mod cabi_mips;
106
mod cabi_mips64;
J
Jorge Aparicio 已提交
107
mod cabi_msp430;
J
Jorge Aparicio 已提交
108 109
mod cabi_nvptx;
mod cabi_nvptx64;
110 111
mod cabi_powerpc;
mod cabi_powerpc64;
U
Ulrich Weigand 已提交
112
mod cabi_s390x;
J
Jorge Aparicio 已提交
113
mod cabi_sparc;
J
Jonathan A. Kollasch 已提交
114
mod cabi_sparc64;
115 116 117 118
mod cabi_x86;
mod cabi_x86_64;
mod cabi_x86_win64;
mod callee;
119
mod collector;
120 121 122 123 124 125 126
mod common;
mod consts;
mod context;
mod debuginfo;
mod declare;
mod glue;
mod intrinsic;
127
mod llvm_util;
128
mod machine;
129
mod metadata;
130 131 132
mod meth;
mod mir;
mod monomorphize;
133
mod partitioning;
134
mod symbol_names_test;
135
mod time_graph;
136
mod trans_item;
137 138 139 140 141
mod tvec;
mod type_;
mod type_of;
mod value;

B
bjorn3 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
use rustc::ty::{self, TyCtxt, CrateAnalysis};
use rustc::session::Session;
use rustc::session::config::OutputFilenames;
use rustc::middle::cstore::MetadataLoader;
use rustc::dep_graph::DepGraph;
use rustc_incremental::IncrementalHashesMap;

pub struct LlvmTransCrate(());

impl LlvmTransCrate {
    pub fn new() -> Self {
        LlvmTransCrate(())
    }
}

impl rustc_trans_traits::TransCrate for LlvmTransCrate {
    type MetadataLoader = metadata::LlvmMetadataLoader;
    type OngoingCrateTranslation = back::write::OngoingCrateTranslation;
    type TranslatedCrate = CrateTranslation;

    fn metadata_loader() -> Box<MetadataLoader> {
        box metadata::LlvmMetadataLoader
    }

    fn provide(providers: &mut ty::maps::Providers) {
        back::symbol_names::provide(providers);
    }

    fn trans_crate<'a, 'tcx>(
        tcx: TyCtxt<'a, 'tcx, 'tcx>,
        analysis: CrateAnalysis,
        incr_hashes_map: IncrementalHashesMap,
        output_filenames: &OutputFilenames
    ) -> Self::OngoingCrateTranslation {
        base::trans_crate(tcx, analysis, incr_hashes_map, output_filenames)
    }

    fn join_trans(
        trans: Self::OngoingCrateTranslation,
        sess: &Session,
        dep_graph: &DepGraph
    ) -> Self::TranslatedCrate {
        trans.join(sess, dep_graph)
    }

    fn link_binary(sess: &Session, trans: &Self::TranslatedCrate, outputs: &OutputFilenames) {
        back::link::link_binary(sess, trans, outputs, &trans.crate_name.as_str());
    }

    fn dump_incremental_data(trans: &Self::TranslatedCrate) {
        back::write::dump_incremental_data(trans);
    }
}

196
pub struct ModuleTranslation {
197 198 199 200 201
    /// The name of the module. When the crate may be saved between
    /// compilations, incremental compilation requires that name be
    /// unique amongst **all** crates.  Therefore, it should contain
    /// something unique to this crate (e.g., a module path) as well
    /// as the crate name and disambiguator.
202 203
    name: String,
    symbol_name_hash: u64,
204
    pub source: ModuleSource,
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    pub kind: ModuleKind,
}

#[derive(Copy, Clone, Debug)]
pub enum ModuleKind {
    Regular,
    Metadata,
    Allocator,
}

impl ModuleTranslation {
    pub fn into_compiled_module(self, emit_obj: bool, emit_bc: bool) -> CompiledModule {
        let pre_existing = match self.source {
            ModuleSource::Preexisting(_) => true,
            ModuleSource::Translated(_) => false,
        };

        CompiledModule {
            name: self.name.clone(),
            kind: self.kind,
            symbol_name_hash: self.symbol_name_hash,
            pre_existing,
            emit_obj,
            emit_bc,
        }
    }
}

impl Drop for ModuleTranslation {
    fn drop(&mut self) {
        match self.source {
            ModuleSource::Preexisting(_) => {
                // Nothing to dispose.
            },
            ModuleSource::Translated(llvm) => {
                unsafe {
                    llvm::LLVMDisposeModule(llvm.llmod);
                    llvm::LLVMContextDispose(llvm.llcx);
                }
            },
        }
    }
}

#[derive(Debug)]
pub struct CompiledModule {
    pub name: String,
    pub kind: ModuleKind,
    pub symbol_name_hash: u64,
    pub pre_existing: bool,
    pub emit_obj: bool,
    pub emit_bc: bool,
257 258 259 260
}

#[derive(Clone)]
pub enum ModuleSource {
261 262 263 264
    /// Copy the `.o` files or whatever from the incr. comp. directory.
    Preexisting(WorkProduct),

    /// Rebuild from this LLVM module.
265 266 267
    Translated(ModuleLlvm),
}

268
#[derive(Copy, Clone, Debug)]
269
pub struct ModuleLlvm {
270
    llcx: llvm::ContextRef,
271 272 273 274 275
    pub llmod: llvm::ModuleRef,
}

unsafe impl Send for ModuleTranslation { }
unsafe impl Sync for ModuleTranslation { }
276

277
pub struct CrateTranslation {
278
    pub crate_name: Symbol,
279
    pub modules: Vec<CompiledModule>,
280
    allocator_module: Option<CompiledModule>,
281 282
    pub link: rustc::middle::cstore::LinkMeta,
    pub metadata: rustc::middle::cstore::EncodedMetadata,
283
    windows_subsystem: Option<String>,
284 285 286 287 288 289 290 291 292 293 294
    linker_info: back::linker::LinkerInfo,
    crate_info: CrateInfo,
}

// Misc info we load from metadata to persist beyond the tcx
pub struct CrateInfo {
    panic_runtime: Option<CrateNum>,
    compiler_builtins: Option<CrateNum>,
    profiler_runtime: Option<CrateNum>,
    sanitizer_runtime: Option<CrateNum>,
    is_no_builtins: FxHashSet<CrateNum>,
295
    native_libraries: FxHashMap<CrateNum, Rc<Vec<NativeLibrary>>>,
296
    crate_name: FxHashMap<CrateNum, String>,
297 298
    used_libraries: Rc<Vec<NativeLibrary>>,
    link_args: Rc<Vec<String>>,
299 300 301
    used_crate_source: FxHashMap<CrateNum, Rc<CrateSource>>,
    used_crates_static: Vec<(CrateNum, LibSource)>,
    used_crates_dynamic: Vec<(CrateNum, LibSource)>,
302
}
303 304

__build_diagnostic_array! { librustc_trans, DIAGNOSTICS }
305 306 307

pub fn provide_local(providers: &mut Providers) {
    back::symbol_names::provide(providers);
308
    back::symbol_export::provide_local(providers);
309
    base::provide_local(providers);
310 311 312 313
}

pub fn provide_extern(providers: &mut Providers) {
    back::symbol_names::provide(providers);
314
    back::symbol_export::provide_extern(providers);
315
    base::provide_extern(providers);
316
}