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::CodegenCx;
24 use monomorphize::Instance;
25 use type_of::LayoutLlvmExt;
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;
34 pub use rustc::mir::mono::MonoItem;
36 pub use rustc_mir::monomorphize::item::*;
37 pub use rustc_mir::monomorphize::item::MonoItemExt as BaseMonoItemExt;
39 pub trait MonoItemExt<'a, 'tcx>: fmt::Debug + BaseMonoItemExt<'a, 'tcx> {
40 fn define(&self, cx: &CodegenCx<'a, 'tcx>) {
41 debug!("BEGIN IMPLEMENTING '{} ({})' in cgu {}",
42 self.to_string(cx.tcx),
44 cx.codegen_unit.name());
46 match *self.as_mono_item() {
47 MonoItem::Static(def_id) => {
49 let is_mutable = match tcx.describe_def(def_id) {
50 Some(Def::Static(_, is_mutable)) => is_mutable,
52 bug!("Expected Def::Static, found {:?}", other)
55 bug!("Expected Def::Static for {:?}, found nothing", def_id)
58 let attrs = tcx.get_attrs(def_id);
60 consts::codegen_static(&cx, def_id, is_mutable, &attrs);
62 MonoItem::GlobalAsm(node_id) => {
63 let item = cx.tcx.hir.expect_item(node_id);
64 if let hir::ItemGlobalAsm(ref ga) = item.node {
65 asm::codegen_global_asm(cx, ga);
67 span_bug!(item.span, "Mismatch between hir::Item type and MonoItem type")
70 MonoItem::Fn(instance) => {
71 base::codegen_instance(&cx, instance);
75 debug!("END IMPLEMENTING '{} ({})' in cgu {}",
76 self.to_string(cx.tcx),
78 cx.codegen_unit.name());
82 cx: &CodegenCx<'a, 'tcx>,
84 visibility: Visibility) {
85 debug!("BEGIN PREDEFINING '{} ({})' in cgu {}",
86 self.to_string(cx.tcx),
88 cx.codegen_unit.name());
90 let symbol_name = self.symbol_name(cx.tcx).as_str();
92 debug!("symbol {}", &symbol_name);
94 match *self.as_mono_item() {
95 MonoItem::Static(def_id) => {
96 predefine_static(cx, def_id, linkage, visibility, &symbol_name);
98 MonoItem::Fn(instance) => {
99 predefine_fn(cx, instance, linkage, visibility, &symbol_name);
101 MonoItem::GlobalAsm(..) => {}
104 debug!("END PREDEFINING '{} ({})' in cgu {}",
105 self.to_string(cx.tcx),
106 self.to_raw_string(),
107 cx.codegen_unit.name());
110 fn to_raw_string(&self) -> String {
111 match *self.as_mono_item() {
112 MonoItem::Fn(instance) => {
113 format!("Fn({:?}, {})",
115 instance.substs.as_ptr() as usize)
117 MonoItem::Static(id) => {
118 format!("Static({:?})", id)
120 MonoItem::GlobalAsm(id) => {
121 format!("GlobalAsm({:?})", id)
127 impl<'a, 'tcx> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {}
129 fn predefine_static<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
132 visibility: Visibility,
134 let instance = Instance::mono(cx.tcx, def_id);
135 let ty = instance.ty(cx.tcx);
136 let llty = cx.layout_of(ty).llvm_type(cx);
138 let g = declare::define_global(cx, symbol_name, llty).unwrap_or_else(|| {
139 cx.sess().span_fatal(cx.tcx.def_span(def_id),
140 &format!("symbol `{}` is already defined", symbol_name))
144 llvm::LLVMRustSetLinkage(g, base::linkage_to_llvm(linkage));
145 llvm::LLVMRustSetVisibility(g, base::visibility_to_llvm(visibility));
148 cx.instances.borrow_mut().insert(instance, g);
149 cx.statics.borrow_mut().insert(g, def_id);
152 fn predefine_fn<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
153 instance: Instance<'tcx>,
155 visibility: Visibility,
157 assert!(!instance.substs.needs_infer() &&
158 !instance.substs.has_param_types());
160 let mono_ty = instance.ty(cx.tcx);
161 let attrs = instance.def.attrs(cx.tcx);
162 let lldecl = declare::declare_fn(cx, symbol_name, mono_ty);
163 unsafe { llvm::LLVMRustSetLinkage(lldecl, base::linkage_to_llvm(linkage)) };
164 base::set_link_section(cx, lldecl, &attrs);
165 if linkage == Linkage::LinkOnceODR ||
166 linkage == Linkage::WeakODR {
167 llvm::SetUniqueComdat(cx.llmod, lldecl);
170 // If we're compiling the compiler-builtins crate, e.g. the equivalent of
171 // compiler-rt, then we want to implicitly compile everything with hidden
172 // visibility as we're going to link this object all over the place but
173 // don't want the symbols to get exported.
174 if linkage != Linkage::Internal && linkage != Linkage::Private &&
175 cx.tcx.is_compiler_builtins(LOCAL_CRATE) {
177 llvm::LLVMRustSetVisibility(lldecl, llvm::Visibility::Hidden);
181 llvm::LLVMRustSetVisibility(lldecl, base::visibility_to_llvm(visibility));
185 debug!("predefine_fn: mono_ty = {:?} instance = {:?}", mono_ty, instance);
186 if instance.def.is_inline(cx.tcx) {
187 attributes::inline(lldecl, attributes::InlineAttr::Hint);
189 attributes::from_fn_attrs(cx, lldecl, instance.def.def_id());
191 cx.instances.borrow_mut().insert(instance, lldecl);