]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/callee.rs
Rollup merge of #67254 - RalfJung:vtable-ice, r=oli-obk
[rust.git] / src / librustc_codegen_llvm / callee.rs
1 //! Handles codegen of callees as well as other call-related
2 //! things. Callees are a superset of normal rust values and sometimes
3 //! have different representations. In particular, top-level fn items
4 //! and methods are represented as just a fn ptr and not a full
5 //! closure.
6
7 use crate::abi::{FnAbi, FnAbiLlvmExt};
8 use crate::attributes;
9 use crate::llvm;
10 use crate::context::CodegenCx;
11 use crate::value::Value;
12 use rustc_codegen_ssa::traits::*;
13 use log::debug;
14
15 use rustc::ty::{TypeFoldable, Instance};
16 use rustc::ty::layout::{FnAbiExt, HasTyCtxt};
17
18 /// Codegens a reference to a fn/method item, monomorphizing and
19 /// inlining as it goes.
20 ///
21 /// # Parameters
22 ///
23 /// - `cx`: the crate context
24 /// - `instance`: the instance to be instantiated
25 pub fn get_fn(
26     cx: &CodegenCx<'ll, 'tcx>,
27     instance: Instance<'tcx>,
28 ) -> &'ll Value {
29     let tcx = cx.tcx();
30
31     debug!("get_fn(instance={:?})", instance);
32
33     assert!(!instance.substs.needs_infer());
34     assert!(!instance.substs.has_escaping_bound_vars());
35     assert!(!instance.substs.has_param_types());
36
37     if let Some(&llfn) = cx.instances.borrow().get(&instance) {
38         return llfn;
39     }
40
41     let sym = tcx.symbol_name(instance).name.as_str();
42     debug!("get_fn({:?}: {:?}) => {}", instance, instance.ty(cx.tcx()), sym);
43
44     let fn_abi = FnAbi::of_instance(cx, instance, &[]);
45
46     let llfn = if let Some(llfn) = cx.get_declared_value(&sym) {
47         // Create a fn pointer with the new signature.
48         let llptrty = fn_abi.ptr_to_llvm_type(cx);
49
50         // This is subtle and surprising, but sometimes we have to bitcast
51         // the resulting fn pointer.  The reason has to do with external
52         // functions.  If you have two crates that both bind the same C
53         // library, they may not use precisely the same types: for
54         // example, they will probably each declare their own structs,
55         // which are distinct types from LLVM's point of view (nominal
56         // types).
57         //
58         // Now, if those two crates are linked into an application, and
59         // they contain inlined code, you can wind up with a situation
60         // where both of those functions wind up being loaded into this
61         // application simultaneously. In that case, the same function
62         // (from LLVM's point of view) requires two types. But of course
63         // LLVM won't allow one function to have two types.
64         //
65         // What we currently do, therefore, is declare the function with
66         // one of the two types (whichever happens to come first) and then
67         // bitcast as needed when the function is referenced to make sure
68         // it has the type we expect.
69         //
70         // This can occur on either a crate-local or crate-external
71         // reference. It also occurs when testing libcore and in some
72         // other weird situations. Annoying.
73         if cx.val_ty(llfn) != llptrty {
74             debug!("get_fn: casting {:?} to {:?}", llfn, llptrty);
75             cx.const_ptrcast(llfn, llptrty)
76         } else {
77             debug!("get_fn: not casting pointer!");
78             llfn
79         }
80     } else {
81         let llfn = cx.declare_fn(&sym, &fn_abi);
82         debug!("get_fn: not casting pointer!");
83
84         attributes::from_fn_attrs(cx, llfn, instance, &fn_abi);
85
86         let instance_def_id = instance.def_id();
87
88         // Apply an appropriate linkage/visibility value to our item that we
89         // just declared.
90         //
91         // This is sort of subtle. Inside our codegen unit we started off
92         // compilation by predefining all our own `MonoItem` instances. That
93         // is, everything we're codegenning ourselves is already defined. That
94         // means that anything we're actually codegenning in this codegen unit
95         // will have hit the above branch in `get_declared_value`. As a result,
96         // we're guaranteed here that we're declaring a symbol that won't get
97         // defined, or in other words we're referencing a value from another
98         // codegen unit or even another crate.
99         //
100         // So because this is a foreign value we blanket apply an external
101         // linkage directive because it's coming from a different object file.
102         // The visibility here is where it gets tricky. This symbol could be
103         // referencing some foreign crate or foreign library (an `extern`
104         // block) in which case we want to leave the default visibility. We may
105         // also, though, have multiple codegen units. It could be a
106         // monomorphization, in which case its expected visibility depends on
107         // whether we are sharing generics or not. The important thing here is
108         // that the visibility we apply to the declaration is the same one that
109         // has been applied to the definition (wherever that definition may be).
110         unsafe {
111             llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage);
112
113             let is_generic = instance.substs.non_erasable_generics().next().is_some();
114
115             if is_generic {
116                 // This is a monomorphization. Its expected visibility depends
117                 // on whether we are in share-generics mode.
118
119                 if cx.tcx.sess.opts.share_generics() {
120                     // We are in share_generics mode.
121
122                     if instance_def_id.is_local() {
123                         // This is a definition from the current crate. If the
124                         // definition is unreachable for downstream crates or
125                         // the current crate does not re-export generics, the
126                         // definition of the instance will have been declared
127                         // as `hidden`.
128                         if cx.tcx.is_unreachable_local_definition(instance_def_id) ||
129                            !cx.tcx.local_crate_exports_generics() {
130                             llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
131                         }
132                     } else {
133                         // This is a monomorphization of a generic function
134                         // defined in an upstream crate.
135                         if cx.tcx.upstream_monomorphizations_for(instance_def_id)
136                                  .map(|set| set.contains_key(instance.substs))
137                                  .unwrap_or(false) {
138                             // This is instantiated in another crate. It cannot
139                             // be `hidden`.
140                         } else {
141                             // This is a local instantiation of an upstream definition.
142                             // If the current crate does not re-export it
143                             // (because it is a C library or an executable), it
144                             // will have been declared `hidden`.
145                             if !cx.tcx.local_crate_exports_generics() {
146                                 llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
147                             }
148                         }
149                     }
150                 } else {
151                     // When not sharing generics, all instances are in the same
152                     // crate and have hidden visibility
153                     llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
154                 }
155             } else {
156                 // This is a non-generic function
157                 if cx.tcx.is_codegened_item(instance_def_id) {
158                     // This is a function that is instantiated in the local crate
159
160                     if instance_def_id.is_local() {
161                         // This is function that is defined in the local crate.
162                         // If it is not reachable, it is hidden.
163                         if !cx.tcx.is_reachable_non_generic(instance_def_id) {
164                             llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
165                         }
166                     } else {
167                         // This is a function from an upstream crate that has
168                         // been instantiated here. These are always hidden.
169                         llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
170                     }
171                 }
172             }
173         }
174
175         if cx.use_dll_storage_attrs &&
176             tcx.is_dllimport_foreign_item(instance_def_id)
177         {
178             unsafe {
179                 llvm::LLVMSetDLLStorageClass(llfn, llvm::DLLStorageClass::DllImport);
180             }
181         }
182
183         llfn
184     };
185
186     cx.instances.borrow_mut().insert(instance, llfn);
187
188     llfn
189 }