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