]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/mono_item.rs
041cfbf00c6fe5648997a7d63788fc35b7e39036
[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 //! Walks the crate looking for items/impl-items/trait-items that have
12 //! either a `rustc_symbol_name` or `rustc_item_path` attribute and
13 //! generates an error giving, respectively, the symbol name or
14 //! item-path. This is used for unit testing the code that generates
15 //! paths etc in all kinds of annoying scenarios.
16
17 use attributes;
18 use base;
19 use context::CodegenCx;
20 use llvm;
21 use monomorphize::Instance;
22 use type_of::LayoutLlvmExt;
23 use rustc::hir;
24 use rustc::hir::def::Def;
25 use rustc::hir::def_id::{DefId, LOCAL_CRATE};
26 use rustc::mir::mono::{Linkage, Visibility};
27 use rustc::ty::TypeFoldable;
28 use rustc::ty::layout::{LayoutOf, HasTyCtxt};
29 use std::fmt;
30 use builder::Builder;
31 use interfaces::*;
32
33 pub use rustc::mir::mono::MonoItem;
34
35 pub use rustc_mir::monomorphize::item::MonoItemExt as BaseMonoItemExt;
36
37 pub trait MonoItemExt<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> :
38     fmt::Debug + BaseMonoItemExt<'a, 'tcx>
39 {
40     fn define(&self, cx: &'a Bx::CodegenCx) {
41         debug!("BEGIN IMPLEMENTING '{} ({})' in cgu {}",
42                self.to_string(cx.tcx()),
43                self.to_raw_string(),
44                cx.codegen_unit().name());
45
46         match *self.as_mono_item() {
47             MonoItem::Static(def_id) => {
48                 let tcx = cx.tcx();
49                 let is_mutable = match tcx.describe_def(def_id) {
50                     Some(Def::Static(_, is_mutable)) => is_mutable,
51                     Some(other) => {
52                         bug!("Expected Def::Static, found {:?}", other)
53                     }
54                     None => {
55                         bug!("Expected Def::Static for {:?}, found nothing", def_id)
56                     }
57                 };
58                 cx.codegen_static(def_id, is_mutable);
59             }
60             MonoItem::GlobalAsm(node_id) => {
61                 let item = cx.tcx().hir.expect_item(node_id);
62                 if let hir::ItemKind::GlobalAsm(ref ga) = item.node {
63                     cx.codegen_global_asm(ga);
64                 } else {
65                     span_bug!(item.span, "Mismatch between hir::Item type and MonoItem type")
66                 }
67             }
68             MonoItem::Fn(instance) => {
69                 base::codegen_instance::<Bx>(&cx, instance);
70             }
71         }
72
73         debug!("END IMPLEMENTING '{} ({})' in cgu {}",
74                self.to_string(cx.tcx()),
75                self.to_raw_string(),
76                cx.codegen_unit().name());
77     }
78
79     fn predefine(&self,
80                  cx: &'a Bx::CodegenCx,
81                  linkage: Linkage,
82                  visibility: Visibility) {
83         debug!("BEGIN PREDEFINING '{} ({})' in cgu {}",
84                self.to_string(cx.tcx()),
85                self.to_raw_string(),
86                cx.codegen_unit().name());
87
88         let symbol_name = self.symbol_name(cx.tcx()).as_str();
89
90         debug!("symbol {}", &symbol_name);
91
92         match *self.as_mono_item() {
93             MonoItem::Static(def_id) => {
94                 cx.predefine_static(def_id, linkage, visibility, &symbol_name);
95             }
96             MonoItem::Fn(instance) => {
97                 cx.predefine_fn(instance, linkage, visibility, &symbol_name);
98             }
99             MonoItem::GlobalAsm(..) => {}
100         }
101
102         debug!("END PREDEFINING '{} ({})' in cgu {}",
103                self.to_string(cx.tcx()),
104                self.to_raw_string(),
105                cx.codegen_unit().name());
106     }
107
108     fn to_raw_string(&self) -> String {
109         match *self.as_mono_item() {
110             MonoItem::Fn(instance) => {
111                 format!("Fn({:?}, {})",
112                         instance.def,
113                         instance.substs.as_ptr() as usize)
114             }
115             MonoItem::Static(id) => {
116                 format!("Static({:?})", id)
117             }
118             MonoItem::GlobalAsm(id) => {
119                 format!("GlobalAsm({:?})", id)
120             }
121         }
122     }
123 }
124
125 impl MonoItemExt<'a, 'tcx, Builder<'a, 'll, 'tcx>> for MonoItem<'tcx> {}
126
127 impl PreDefineMethods<'tcx> for CodegenCx<'ll, 'tcx> {
128     fn predefine_static(&self,
129                                   def_id: DefId,
130                                   linkage: Linkage,
131                                   visibility: Visibility,
132                                   symbol_name: &str) {
133         let instance = Instance::mono(self.tcx, def_id);
134         let ty = instance.ty(self.tcx);
135         let llty = self.layout_of(ty).llvm_type(self);
136
137         let g = self.define_global(symbol_name, llty).unwrap_or_else(|| {
138             self.sess().span_fatal(self.tcx.def_span(def_id),
139                 &format!("symbol `{}` is already defined", symbol_name))
140         });
141
142         unsafe {
143             llvm::LLVMRustSetLinkage(g, base::linkage_to_llvm(linkage));
144             llvm::LLVMRustSetVisibility(g, base::visibility_to_llvm(visibility));
145         }
146
147         self.instances.borrow_mut().insert(instance, g);
148     }
149
150     fn predefine_fn(&self,
151                               instance: Instance<'tcx>,
152                               linkage: Linkage,
153                               visibility: Visibility,
154                               symbol_name: &str) {
155         assert!(!instance.substs.needs_infer() &&
156                 !instance.substs.has_param_types());
157
158         let mono_sig = instance.fn_sig(self.tcx());
159         let attrs = self.tcx.codegen_fn_attrs(instance.def_id());
160         let lldecl = self.declare_fn(symbol_name, mono_sig);
161         unsafe { llvm::LLVMRustSetLinkage(lldecl, base::linkage_to_llvm(linkage)) };
162         base::set_link_section(lldecl, &attrs);
163         if linkage == Linkage::LinkOnceODR ||
164             linkage == Linkage::WeakODR {
165             llvm::SetUniqueComdat(self.llmod, lldecl);
166         }
167
168         // If we're compiling the compiler-builtins crate, e.g. the equivalent of
169         // compiler-rt, then we want to implicitly compile everything with hidden
170         // visibility as we're going to link this object all over the place but
171         // don't want the symbols to get exported.
172         if linkage != Linkage::Internal && linkage != Linkage::Private &&
173            self.tcx.is_compiler_builtins(LOCAL_CRATE) {
174             unsafe {
175                 llvm::LLVMRustSetVisibility(lldecl, llvm::Visibility::Hidden);
176             }
177         } else {
178             unsafe {
179                 llvm::LLVMRustSetVisibility(lldecl, base::visibility_to_llvm(visibility));
180             }
181         }
182
183         debug!("predefine_fn: mono_sig = {:?} instance = {:?}", mono_sig, instance);
184         if instance.def.is_inline(self.tcx) {
185             attributes::inline(self, lldecl, attributes::InlineAttr::Hint);
186         }
187         attributes::from_fn_attrs(self, lldecl, Some(instance.def.def_id()));
188
189         self.instances.borrow_mut().insert(instance, lldecl);
190     }
191 }