]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/mono_item.rs
Ensure StorageDead is created even if variable initialization fails
[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::*;
37 pub use rustc_mir::monomorphize::item::MonoItemExt as BaseMonoItemExt;
38
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),
43                self.to_raw_string(),
44                cx.codegen_unit.name());
45
46         match *self.as_mono_item() {
47             MonoItem::Static(def_id) => {
48                 let tcx = cx.tcx;
49                 let is_mutable = match tcx.describe_def(def_id) {
50                     Some(Def::Static(_, is_mutable)) => is_mutable,
51                     Some(other) => {
52                         bug!("Expected Def::Static, found {:?}", other)
53                     }
54                     None => {
55                         bug!("Expected Def::Static for {:?}, found nothing", def_id)
56                     }
57                 };
58                 let attrs = tcx.get_attrs(def_id);
59
60                 consts::codegen_static(&cx, def_id, is_mutable, &attrs);
61             }
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);
66                 } else {
67                     span_bug!(item.span, "Mismatch between hir::Item type and MonoItem type")
68                 }
69             }
70             MonoItem::Fn(instance) => {
71                 base::codegen_instance(&cx, instance);
72             }
73         }
74
75         debug!("END IMPLEMENTING '{} ({})' in cgu {}",
76                self.to_string(cx.tcx),
77                self.to_raw_string(),
78                cx.codegen_unit.name());
79     }
80
81     fn predefine(&self,
82                  cx: &CodegenCx<'a, 'tcx>,
83                  linkage: Linkage,
84                  visibility: Visibility) {
85         debug!("BEGIN PREDEFINING '{} ({})' in cgu {}",
86                self.to_string(cx.tcx),
87                self.to_raw_string(),
88                cx.codegen_unit.name());
89
90         let symbol_name = self.symbol_name(cx.tcx).as_str();
91
92         debug!("symbol {}", &symbol_name);
93
94         match *self.as_mono_item() {
95             MonoItem::Static(def_id) => {
96                 predefine_static(cx, def_id, linkage, visibility, &symbol_name);
97             }
98             MonoItem::Fn(instance) => {
99                 predefine_fn(cx, instance, linkage, visibility, &symbol_name);
100             }
101             MonoItem::GlobalAsm(..) => {}
102         }
103
104         debug!("END PREDEFINING '{} ({})' in cgu {}",
105                self.to_string(cx.tcx),
106                self.to_raw_string(),
107                cx.codegen_unit.name());
108     }
109
110     fn to_raw_string(&self) -> String {
111         match *self.as_mono_item() {
112             MonoItem::Fn(instance) => {
113                 format!("Fn({:?}, {})",
114                          instance.def,
115                          instance.substs.as_ptr() as usize)
116             }
117             MonoItem::Static(id) => {
118                 format!("Static({:?})", id)
119             }
120             MonoItem::GlobalAsm(id) => {
121                 format!("GlobalAsm({:?})", id)
122             }
123         }
124     }
125 }
126
127 impl<'a, 'tcx> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {}
128
129 fn predefine_static<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
130                               def_id: DefId,
131                               linkage: Linkage,
132                               visibility: Visibility,
133                               symbol_name: &str) {
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);
137
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))
141     });
142
143     unsafe {
144         llvm::LLVMRustSetLinkage(g, base::linkage_to_llvm(linkage));
145         llvm::LLVMRustSetVisibility(g, base::visibility_to_llvm(visibility));
146     }
147
148     cx.instances.borrow_mut().insert(instance, g);
149     cx.statics.borrow_mut().insert(g, def_id);
150 }
151
152 fn predefine_fn<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
153                           instance: Instance<'tcx>,
154                           linkage: Linkage,
155                           visibility: Visibility,
156                           symbol_name: &str) {
157     assert!(!instance.substs.needs_infer() &&
158             !instance.substs.has_param_types());
159
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);
168     }
169
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) {
176         unsafe {
177             llvm::LLVMRustSetVisibility(lldecl, llvm::Visibility::Hidden);
178         }
179     } else {
180         unsafe {
181             llvm::LLVMRustSetVisibility(lldecl, base::visibility_to_llvm(visibility));
182         }
183     }
184
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);
188     }
189     attributes::from_fn_attrs(cx, lldecl, instance.def.def_id());
190
191     cx.instances.borrow_mut().insert(instance, lldecl);
192 }