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.
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.
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.
21 use context::CrateContext;
24 use monomorphize::Instance;
25 use type_of::LayoutLlvmExt;
27 use rustc::mir::mono::{Linkage, Visibility};
28 use rustc::ty::{TyCtxt, TypeFoldable};
29 use rustc::ty::layout::LayoutOf;
35 pub use rustc::mir::mono::MonoItem;
37 pub use rustc_mir::monomorphize::item::*;
38 pub use rustc_mir::monomorphize::item::MonoItemExt as BaseMonoItemExt;
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()),
45 ccx.codegen_unit().name());
47 match *self.as_mono_item() {
48 MonoItem::Static(node_id) => {
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. */ },
55 err.report(tcx, item.span, "static");
59 span_bug!(item.span, "Mismatch between hir::Item type and TransItem type")
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);
67 span_bug!(item.span, "Mismatch between hir::Item type and TransItem type")
70 MonoItem::Fn(instance) => {
71 base::trans_instance(&ccx, instance);
75 debug!("END IMPLEMENTING '{} ({})' in cgu {}",
76 self.to_string(ccx.tcx()),
78 ccx.codegen_unit().name());
82 ccx: &CrateContext<'a, 'tcx>,
84 visibility: Visibility) {
85 debug!("BEGIN PREDEFINING '{} ({})' in cgu {}",
86 self.to_string(ccx.tcx()),
88 ccx.codegen_unit().name());
90 let symbol_name = self.symbol_name(ccx.tcx());
92 debug!("symbol {}", &symbol_name);
94 match *self.as_mono_item() {
95 MonoItem::Static(node_id) => {
96 predefine_static(ccx, node_id, linkage, visibility, &symbol_name);
98 MonoItem::Fn(instance) => {
99 predefine_fn(ccx, instance, linkage, visibility, &symbol_name);
101 MonoItem::GlobalAsm(..) => {}
104 debug!("END PREDEFINING '{} ({})' in cgu {}",
105 self.to_string(ccx.tcx()),
106 self.to_raw_string(),
107 ccx.codegen_unit().name());
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())
115 MonoItem::Static(node_id) |
116 MonoItem::GlobalAsm(node_id) => {
119 }.map(|node_id| tcx.hir.span(node_id))
122 fn to_raw_string(&self) -> String {
123 match *self.as_mono_item() {
124 MonoItem::Fn(instance) => {
125 format!("Fn({:?}, {})",
127 instance.substs.as_ptr() as usize)
129 MonoItem::Static(id) => {
130 format!("Static({:?})", id)
132 MonoItem::GlobalAsm(id) => {
133 format!("GlobalAsm({:?})", id)
139 impl<'a, 'tcx> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {}
141 fn predefine_static<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
142 node_id: ast::NodeId,
144 visibility: Visibility,
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);
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))
157 llvm::LLVMRustSetLinkage(g, base::linkage_to_llvm(linkage));
158 llvm::LLVMRustSetVisibility(g, base::visibility_to_llvm(visibility));
161 ccx.instances().borrow_mut().insert(instance, g);
162 ccx.statics().borrow_mut().insert(g, def_id);
165 fn predefine_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
166 instance: Instance<'tcx>,
168 visibility: Visibility,
170 assert!(!instance.substs.needs_infer() &&
171 !instance.substs.has_param_types());
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);
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") {
190 llvm::LLVMRustSetVisibility(lldecl, llvm::Visibility::Hidden);
194 llvm::LLVMRustSetVisibility(lldecl, base::visibility_to_llvm(visibility));
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);
202 attributes::from_fn_attrs(ccx, lldecl, instance.def.def_id());
204 ccx.instances().borrow_mut().insert(instance, lldecl);