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