]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/mono_item.rs
9b2d17d65caa30fe5c20c8b26c79ea2ea18d72cb
[rust.git] / src / librustc_codegen_llvm / mono_item.rs
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.
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 use attributes;
12 use base;
13 use context::CodegenCx;
14 use llvm;
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::*;
22
23 pub use rustc::mir::mono::MonoItem;
24
25 impl PreDefineMethods<'tcx> for CodegenCx<'ll, 'tcx> {
26     fn predefine_static(&self,
27                                   def_id: DefId,
28                                   linkage: Linkage,
29                                   visibility: Visibility,
30                                   symbol_name: &str) {
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);
34
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))
38         });
39
40         unsafe {
41             llvm::LLVMRustSetLinkage(g, base::linkage_to_llvm(linkage));
42             llvm::LLVMRustSetVisibility(g, base::visibility_to_llvm(visibility));
43         }
44
45         self.instances.borrow_mut().insert(instance, g);
46     }
47
48     fn predefine_fn(&self,
49                               instance: Instance<'tcx>,
50                               linkage: Linkage,
51                               visibility: Visibility,
52                               symbol_name: &str) {
53         assert!(!instance.substs.needs_infer() &&
54                 !instance.substs.has_param_types());
55
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);
64         }
65
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) {
72             unsafe {
73                 llvm::LLVMRustSetVisibility(lldecl, llvm::Visibility::Hidden);
74             }
75         } else {
76             unsafe {
77                 llvm::LLVMRustSetVisibility(lldecl, base::visibility_to_llvm(visibility));
78             }
79         }
80
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);
84         }
85         attributes::from_fn_attrs(self, lldecl, Some(instance.def.def_id()));
86
87         self.instances.borrow_mut().insert(instance, lldecl);
88     }
89 }