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