]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/mono_item.rs
Auto merge of #67077 - Aaron1011:build-llvm-in-binary, r=alexcrichton
[rust.git] / src / librustc_codegen_llvm / mono_item.rs
1 use crate::abi::FnAbi;
2 use crate::attributes;
3 use crate::base;
4 use crate::context::CodegenCx;
5 use crate::llvm;
6 use crate::type_of::LayoutLlvmExt;
7 use rustc::hir::def_id::{DefId, LOCAL_CRATE};
8 use rustc::mir::mono::{Linkage, Visibility};
9 use rustc::ty::{TypeFoldable, Instance};
10 use rustc::ty::layout::{FnAbiExt, LayoutOf};
11 use rustc_codegen_ssa::traits::*;
12 use log::debug;
13
14 pub use rustc::mir::mono::MonoItem;
15
16 impl PreDefineMethods<'tcx> for CodegenCx<'ll, 'tcx> {
17     fn predefine_static(&self,
18                                   def_id: DefId,
19                                   linkage: Linkage,
20                                   visibility: Visibility,
21                                   symbol_name: &str) {
22         let instance = Instance::mono(self.tcx, def_id);
23         let ty = instance.ty(self.tcx);
24         let llty = self.layout_of(ty).llvm_type(self);
25
26         let g = self.define_global(symbol_name, llty).unwrap_or_else(|| {
27             self.sess().span_fatal(self.tcx.def_span(def_id),
28                 &format!("symbol `{}` is already defined", symbol_name))
29         });
30
31         unsafe {
32             llvm::LLVMRustSetLinkage(g, base::linkage_to_llvm(linkage));
33             llvm::LLVMRustSetVisibility(g, base::visibility_to_llvm(visibility));
34         }
35
36         self.instances.borrow_mut().insert(instance, g);
37     }
38
39     fn predefine_fn(&self,
40                     instance: Instance<'tcx>,
41                     linkage: Linkage,
42                     visibility: Visibility,
43                     symbol_name: &str) {
44         assert!(!instance.substs.needs_infer() &&
45                 !instance.substs.has_param_types());
46
47         let fn_abi = FnAbi::of_instance(self, instance, &[]);
48         let lldecl = self.declare_fn(symbol_name, &fn_abi);
49         unsafe { llvm::LLVMRustSetLinkage(lldecl, base::linkage_to_llvm(linkage)) };
50         let attrs = self.tcx.codegen_fn_attrs(instance.def_id());
51         base::set_link_section(lldecl, &attrs);
52         if linkage == Linkage::LinkOnceODR ||
53             linkage == Linkage::WeakODR {
54             llvm::SetUniqueComdat(self.llmod, lldecl);
55         }
56
57         // If we're compiling the compiler-builtins crate, e.g., the equivalent of
58         // compiler-rt, then we want to implicitly compile everything with hidden
59         // visibility as we're going to link this object all over the place but
60         // don't want the symbols to get exported.
61         if linkage != Linkage::Internal && linkage != Linkage::Private &&
62            self.tcx.is_compiler_builtins(LOCAL_CRATE) {
63             unsafe {
64                 llvm::LLVMRustSetVisibility(lldecl, llvm::Visibility::Hidden);
65             }
66         } else {
67             unsafe {
68                 llvm::LLVMRustSetVisibility(lldecl, base::visibility_to_llvm(visibility));
69             }
70         }
71
72         debug!("predefine_fn: instance = {:?}", instance);
73
74         attributes::from_fn_attrs(self, lldecl, instance, &fn_abi);
75
76         self.instances.borrow_mut().insert(instance, lldecl);
77     }
78 }