]>
Commit | Line | Data |
---|---|---|
94b46f34 | 1 | //! Handles codegen of callees as well as other call-related |
9fa01778 XL |
2 | //! things. Callees are a superset of normal rust values and sometimes |
3 | //! have different representations. In particular, top-level fn items | |
54a0048b SL |
4 | //! and methods are represented as just a fn ptr and not a full |
5 | //! closure. | |
6 | ||
c295e0f8 | 7 | use crate::abi::FnAbiLlvmExt; |
9fa01778 | 8 | use crate::attributes; |
f2b60f7d | 9 | use crate::common; |
9fa01778 | 10 | use crate::context::CodegenCx; |
dfeec247 | 11 | use crate::llvm; |
9fa01778 | 12 | use crate::value::Value; |
dfeec247 | 13 | use rustc_codegen_ssa::traits::*; |
ff7c6d11 | 14 | |
c295e0f8 | 15 | use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt}; |
064997fb | 16 | use rustc_middle::ty::{self, Instance, TypeVisitable}; |
54a0048b | 17 | |
94b46f34 | 18 | /// Codegens a reference to a fn/method item, monomorphizing and |
54a0048b SL |
19 | /// inlining as it goes. |
20 | /// | |
21 | /// # Parameters | |
22 | /// | |
2c00a5a8 | 23 | /// - `cx`: the crate context |
cc61c64b | 24 | /// - `instance`: the instance to be instantiated |
a2a8927a | 25 | pub fn get_fn<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'tcx>) -> &'ll Value { |
a1dfa0c6 | 26 | let tcx = cx.tcx(); |
54a0048b | 27 | |
cc61c64b | 28 | debug!("get_fn(instance={:?})", instance); |
54a0048b | 29 | |
cc61c64b | 30 | assert!(!instance.substs.needs_infer()); |
a1dfa0c6 | 31 | assert!(!instance.substs.has_escaping_bound_vars()); |
54a0048b | 32 | |
e74abb32 | 33 | if let Some(&llfn) = cx.instances.borrow().get(&instance) { |
cc61c64b | 34 | return llfn; |
54a0048b SL |
35 | } |
36 | ||
3dfed10e XL |
37 | let sym = tcx.symbol_name(instance).name; |
38 | debug!( | |
39 | "get_fn({:?}: {:?}) => {}", | |
40 | instance, | |
41 | instance.ty(cx.tcx(), ty::ParamEnv::reveal_all()), | |
42 | sym | |
43 | ); | |
54a0048b | 44 | |
c295e0f8 | 45 | let fn_abi = cx.fn_abi_of_instance(instance, ty::List::empty()); |
3157f602 | 46 | |
c295e0f8 | 47 | let llfn = if let Some(llfn) = cx.get_declared_value(sym) { |
60c5eb7d XL |
48 | // Create a fn pointer with the new signature. |
49 | let llptrty = fn_abi.ptr_to_llvm_type(cx); | |
50 | ||
ea8adc8c XL |
51 | // This is subtle and surprising, but sometimes we have to bitcast |
52 | // the resulting fn pointer. The reason has to do with external | |
53 | // functions. If you have two crates that both bind the same C | |
54 | // library, they may not use precisely the same types: for | |
55 | // example, they will probably each declare their own structs, | |
56 | // which are distinct types from LLVM's point of view (nominal | |
57 | // types). | |
58 | // | |
59 | // Now, if those two crates are linked into an application, and | |
60 | // they contain inlined code, you can wind up with a situation | |
61 | // where both of those functions wind up being loaded into this | |
62 | // application simultaneously. In that case, the same function | |
63 | // (from LLVM's point of view) requires two types. But of course | |
64 | // LLVM won't allow one function to have two types. | |
65 | // | |
66 | // What we currently do, therefore, is declare the function with | |
67 | // one of the two types (whichever happens to come first) and then | |
68 | // bitcast as needed when the function is referenced to make sure | |
69 | // it has the type we expect. | |
70 | // | |
71 | // This can occur on either a crate-local or crate-external | |
72 | // reference. It also occurs when testing libcore and in some | |
73 | // other weird situations. Annoying. | |
a1dfa0c6 | 74 | if cx.val_ty(llfn) != llptrty { |
54a0048b | 75 | debug!("get_fn: casting {:?} to {:?}", llfn, llptrty); |
a1dfa0c6 | 76 | cx.const_ptrcast(llfn, llptrty) |
54a0048b SL |
77 | } else { |
78 | debug!("get_fn: not casting pointer!"); | |
79 | llfn | |
80 | } | |
81 | } else { | |
f2b60f7d FG |
82 | let instance_def_id = instance.def_id(); |
83 | let llfn = if tcx.sess.target.arch == "x86" && | |
84 | let Some(dllimport) = common::get_dllimport(tcx, instance_def_id, sym) | |
85 | { | |
86 | cx.declare_fn(&common::i686_decorated_name(&dllimport, common::is_mingw_gnu_toolchain(&tcx.sess.target), true), fn_abi) | |
87 | } else { | |
88 | cx.declare_fn(sym, fn_abi) | |
89 | }; | |
54a0048b SL |
90 | debug!("get_fn: not casting pointer!"); |
91 | ||
ba9703b0 | 92 | attributes::from_fn_attrs(cx, llfn, instance); |
9e0c209e | 93 | |
ea8adc8c XL |
94 | // Apply an appropriate linkage/visibility value to our item that we |
95 | // just declared. | |
96 | // | |
97 | // This is sort of subtle. Inside our codegen unit we started off | |
94b46f34 XL |
98 | // compilation by predefining all our own `MonoItem` instances. That |
99 | // is, everything we're codegenning ourselves is already defined. That | |
100 | // means that anything we're actually codegenning in this codegen unit | |
83c7162d XL |
101 | // will have hit the above branch in `get_declared_value`. As a result, |
102 | // we're guaranteed here that we're declaring a symbol that won't get | |
103 | // defined, or in other words we're referencing a value from another | |
104 | // codegen unit or even another crate. | |
ea8adc8c XL |
105 | // |
106 | // So because this is a foreign value we blanket apply an external | |
107 | // linkage directive because it's coming from a different object file. | |
108 | // The visibility here is where it gets tricky. This symbol could be | |
109 | // referencing some foreign crate or foreign library (an `extern` | |
110 | // block) in which case we want to leave the default visibility. We may | |
83c7162d XL |
111 | // also, though, have multiple codegen units. It could be a |
112 | // monomorphization, in which case its expected visibility depends on | |
113 | // whether we are sharing generics or not. The important thing here is | |
114 | // that the visibility we apply to the declaration is the same one that | |
115 | // has been applied to the definition (wherever that definition may be). | |
3b2f2976 XL |
116 | unsafe { |
117 | llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage); | |
118 | ||
532ac7d7 | 119 | let is_generic = instance.substs.non_erasable_generics().next().is_some(); |
83c7162d XL |
120 | |
121 | if is_generic { | |
122 | // This is a monomorphization. Its expected visibility depends | |
123 | // on whether we are in share-generics mode. | |
124 | ||
b7449926 | 125 | if cx.tcx.sess.opts.share_generics() { |
83c7162d XL |
126 | // We are in share_generics mode. |
127 | ||
f9f354fc | 128 | if let Some(instance_def_id) = instance_def_id.as_local() { |
83c7162d XL |
129 | // This is a definition from the current crate. If the |
130 | // definition is unreachable for downstream crates or | |
131 | // the current crate does not re-export generics, the | |
132 | // definition of the instance will have been declared | |
133 | // as `hidden`. | |
dfeec247 XL |
134 | if cx.tcx.is_unreachable_local_definition(instance_def_id) |
135 | || !cx.tcx.local_crate_exports_generics() | |
136 | { | |
83c7162d XL |
137 | llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden); |
138 | } | |
139 | } else { | |
140 | // This is a monomorphization of a generic function | |
141 | // defined in an upstream crate. | |
dfeec247 | 142 | if instance.upstream_monomorphization(tcx).is_some() { |
83c7162d XL |
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 | } | |
3b2f2976 XL |
154 | } |
155 | } else { | |
83c7162d XL |
156 | // When not sharing generics, all instances are in the same |
157 | // crate and have hidden visibility | |
3b2f2976 XL |
158 | llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden); |
159 | } | |
83c7162d XL |
160 | } else { |
161 | // This is a non-generic function | |
94b46f34 | 162 | if cx.tcx.is_codegened_item(instance_def_id) { |
83c7162d XL |
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 | } | |
9e0c209e | 177 | } |
cc61c64b | 178 | |
cdc7bbd5 XL |
179 | // MinGW: For backward compatibility we rely on the linker to decide whether it |
180 | // should use dllimport for functions. | |
181 | if cx.use_dll_storage_attrs | |
182 | && tcx.is_dllimport_foreign_item(instance_def_id) | |
c295e0f8 | 183 | && !matches!(tcx.sess.target.env.as_ref(), "gnu" | "uclibc") |
cdc7bbd5 | 184 | { |
476ff2be SL |
185 | llvm::LLVMSetDLLStorageClass(llfn, llvm::DLLStorageClass::DllImport); |
186 | } | |
cdc7bbd5 | 187 | |
17df50a5 | 188 | if cx.should_assume_dso_local(llfn, true) { |
cdc7bbd5 XL |
189 | llvm::LLVMRustSetDSOLocal(llfn, true); |
190 | } | |
476ff2be | 191 | } |
ea8adc8c | 192 | |
54a0048b SL |
193 | llfn |
194 | }; | |
195 | ||
2c00a5a8 | 196 | cx.instances.borrow_mut().insert(instance, llfn); |
54a0048b | 197 | |
cc61c64b XL |
198 | llfn |
199 | } |