]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans_item.rs
Rollup merge of #47277 - tspiteri:log-correctness, r=frewsxcv
[rust.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::CrateContext;
22 use declare;
23 use llvm;
24 use monomorphize::Instance;
25 use type_of::LayoutLlvmExt;
26 use rustc::hir;
27 use rustc::mir::mono::{Linkage, Visibility};
28 use rustc::ty::{TyCtxt, TypeFoldable};
29 use rustc::ty::layout::LayoutOf;
30 use syntax::ast;
31 use syntax::attr;
32 use syntax_pos::Span;
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, ccx: &CrateContext<'a, 'tcx>) {
42         debug!("BEGIN IMPLEMENTING '{} ({})' in cgu {}",
43                self.to_string(ccx.tcx()),
44                self.to_raw_string(),
45                ccx.codegen_unit().name());
46
47         match *self.as_mono_item() {
48             MonoItem::Static(node_id) => {
49                 let tcx = ccx.tcx();
50                 let item = tcx.hir.expect_item(node_id);
51                 if let hir::ItemStatic(_, m, _) = item.node {
52                     match consts::trans_static(&ccx, m, item.id, &item.attrs) {
53                         Ok(_) => { /* Cool, everything's alright. */ },
54                         Err(err) => {
55                             err.report(tcx, item.span, "static");
56                         }
57                     };
58                 } else {
59                     span_bug!(item.span, "Mismatch between hir::Item type and TransItem type")
60                 }
61             }
62             MonoItem::GlobalAsm(node_id) => {
63                 let item = ccx.tcx().hir.expect_item(node_id);
64                 if let hir::ItemGlobalAsm(ref ga) = item.node {
65                     asm::trans_global_asm(ccx, ga);
66                 } else {
67                     span_bug!(item.span, "Mismatch between hir::Item type and TransItem type")
68                 }
69             }
70             MonoItem::Fn(instance) => {
71                 base::trans_instance(&ccx, instance);
72             }
73         }
74
75         debug!("END IMPLEMENTING '{} ({})' in cgu {}",
76                self.to_string(ccx.tcx()),
77                self.to_raw_string(),
78                ccx.codegen_unit().name());
79     }
80
81     fn predefine(&self,
82                  ccx: &CrateContext<'a, 'tcx>,
83                  linkage: Linkage,
84                  visibility: Visibility) {
85         debug!("BEGIN PREDEFINING '{} ({})' in cgu {}",
86                self.to_string(ccx.tcx()),
87                self.to_raw_string(),
88                ccx.codegen_unit().name());
89
90         let symbol_name = self.symbol_name(ccx.tcx());
91
92         debug!("symbol {}", &symbol_name);
93
94         match *self.as_mono_item() {
95             MonoItem::Static(node_id) => {
96                 predefine_static(ccx, node_id, linkage, visibility, &symbol_name);
97             }
98             MonoItem::Fn(instance) => {
99                 predefine_fn(ccx, instance, linkage, visibility, &symbol_name);
100             }
101             MonoItem::GlobalAsm(..) => {}
102         }
103
104         debug!("END PREDEFINING '{} ({})' in cgu {}",
105                self.to_string(ccx.tcx()),
106                self.to_raw_string(),
107                ccx.codegen_unit().name());
108     }
109
110     fn local_span(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Option<Span> {
111         match *self.as_mono_item() {
112             MonoItem::Fn(Instance { def, .. }) => {
113                 tcx.hir.as_local_node_id(def.def_id())
114             }
115             MonoItem::Static(node_id) |
116             MonoItem::GlobalAsm(node_id) => {
117                 Some(node_id)
118             }
119         }.map(|node_id| tcx.hir.span(node_id))
120     }
121
122     fn to_raw_string(&self) -> String {
123         match *self.as_mono_item() {
124             MonoItem::Fn(instance) => {
125                 format!("Fn({:?}, {})",
126                          instance.def,
127                          instance.substs.as_ptr() as usize)
128             }
129             MonoItem::Static(id) => {
130                 format!("Static({:?})", id)
131             }
132             MonoItem::GlobalAsm(id) => {
133                 format!("GlobalAsm({:?})", id)
134             }
135         }
136     }
137 }
138
139 impl<'a, 'tcx> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {}
140
141 fn predefine_static<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
142                               node_id: ast::NodeId,
143                               linkage: Linkage,
144                               visibility: Visibility,
145                               symbol_name: &str) {
146     let def_id = ccx.tcx().hir.local_def_id(node_id);
147     let instance = Instance::mono(ccx.tcx(), def_id);
148     let ty = instance.ty(ccx.tcx());
149     let llty = ccx.layout_of(ty).llvm_type(ccx);
150
151     let g = declare::define_global(ccx, symbol_name, llty).unwrap_or_else(|| {
152         ccx.sess().span_fatal(ccx.tcx().hir.span(node_id),
153             &format!("symbol `{}` is already defined", symbol_name))
154     });
155
156     unsafe {
157         llvm::LLVMRustSetLinkage(g, base::linkage_to_llvm(linkage));
158         llvm::LLVMRustSetVisibility(g, base::visibility_to_llvm(visibility));
159     }
160
161     ccx.instances().borrow_mut().insert(instance, g);
162     ccx.statics().borrow_mut().insert(g, def_id);
163 }
164
165 fn predefine_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
166                           instance: Instance<'tcx>,
167                           linkage: Linkage,
168                           visibility: Visibility,
169                           symbol_name: &str) {
170     assert!(!instance.substs.needs_infer() &&
171             !instance.substs.has_param_types());
172
173     let mono_ty = instance.ty(ccx.tcx());
174     let attrs = instance.def.attrs(ccx.tcx());
175     let lldecl = declare::declare_fn(ccx, symbol_name, mono_ty);
176     unsafe { llvm::LLVMRustSetLinkage(lldecl, base::linkage_to_llvm(linkage)) };
177     base::set_link_section(ccx, lldecl, &attrs);
178     if linkage == Linkage::LinkOnceODR ||
179         linkage == Linkage::WeakODR {
180         llvm::SetUniqueComdat(ccx.llmod(), lldecl);
181     }
182
183     // If we're compiling the compiler-builtins crate, e.g. the equivalent of
184     // compiler-rt, then we want to implicitly compile everything with hidden
185     // visibility as we're going to link this object all over the place but
186     // don't want the symbols to get exported.
187     if linkage != Linkage::Internal && linkage != Linkage::Private &&
188        attr::contains_name(ccx.tcx().hir.krate_attrs(), "compiler_builtins") {
189         unsafe {
190             llvm::LLVMRustSetVisibility(lldecl, llvm::Visibility::Hidden);
191         }
192     } else {
193         unsafe {
194             llvm::LLVMRustSetVisibility(lldecl, base::visibility_to_llvm(visibility));
195         }
196     }
197
198     debug!("predefine_fn: mono_ty = {:?} instance = {:?}", mono_ty, instance);
199     if instance.def.is_inline(ccx.tcx()) {
200         attributes::inline(lldecl, attributes::InlineAttr::Hint);
201     }
202     attributes::from_fn_attrs(ccx, lldecl, instance.def.def_id());
203
204     ccx.instances().borrow_mut().insert(instance, lldecl);
205 }