1 // Copyright 2016 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.
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.
13 use context::CodegenCx;
15 use monomorphize::Instance;
16 use type_of::LayoutLlvmExt;
17 use rustc::hir::def_id::{DefId, LOCAL_CRATE};
18 use rustc::mir::mono::{Linkage, Visibility};
19 use rustc::ty::TypeFoldable;
20 use rustc::ty::layout::{LayoutOf, HasTyCtxt};
21 use rustc_codegen_ssa::traits::*;
23 pub use rustc::mir::mono::MonoItem;
25 impl PreDefineMethods<'tcx> for CodegenCx<'ll, 'tcx> {
26 fn predefine_static(&self,
29 visibility: Visibility,
31 let instance = Instance::mono(self.tcx, def_id);
32 let ty = instance.ty(self.tcx);
33 let llty = self.layout_of(ty).llvm_type(self);
35 let g = self.define_global(symbol_name, llty).unwrap_or_else(|| {
36 self.sess().span_fatal(self.tcx.def_span(def_id),
37 &format!("symbol `{}` is already defined", symbol_name))
41 llvm::LLVMRustSetLinkage(g, base::linkage_to_llvm(linkage));
42 llvm::LLVMRustSetVisibility(g, base::visibility_to_llvm(visibility));
45 self.instances.borrow_mut().insert(instance, g);
48 fn predefine_fn(&self,
49 instance: Instance<'tcx>,
51 visibility: Visibility,
53 assert!(!instance.substs.needs_infer() &&
54 !instance.substs.has_param_types());
56 let mono_sig = instance.fn_sig(self.tcx());
57 let attrs = self.tcx.codegen_fn_attrs(instance.def_id());
58 let lldecl = self.declare_fn(symbol_name, mono_sig);
59 unsafe { llvm::LLVMRustSetLinkage(lldecl, base::linkage_to_llvm(linkage)) };
60 base::set_link_section(lldecl, &attrs);
61 if linkage == Linkage::LinkOnceODR ||
62 linkage == Linkage::WeakODR {
63 llvm::SetUniqueComdat(self.llmod, lldecl);
66 // If we're compiling the compiler-builtins crate, e.g. the equivalent of
67 // compiler-rt, then we want to implicitly compile everything with hidden
68 // visibility as we're going to link this object all over the place but
69 // don't want the symbols to get exported.
70 if linkage != Linkage::Internal && linkage != Linkage::Private &&
71 self.tcx.is_compiler_builtins(LOCAL_CRATE) {
73 llvm::LLVMRustSetVisibility(lldecl, llvm::Visibility::Hidden);
77 llvm::LLVMRustSetVisibility(lldecl, base::visibility_to_llvm(visibility));
81 debug!("predefine_fn: mono_sig = {:?} instance = {:?}", mono_sig, instance);
82 if instance.def.is_inline(self.tcx) {
83 attributes::inline(self, lldecl, attributes::InlineAttr::Hint);
85 attributes::from_fn_attrs(self, lldecl, Some(instance.def.def_id()));
87 self.instances.borrow_mut().insert(instance, lldecl);