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