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
7 use crate::abi::FnAbiLlvmExt;
10 use crate::context::CodegenCx;
12 use crate::value::Value;
13 use rustc_codegen_ssa::traits::*;
16 use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt};
17 use rustc_middle::ty::{self, Instance, TypeVisitable};
19 /// Codegens a reference to a fn/method item, monomorphizing and
20 /// inlining as it goes.
24 /// - `cx`: the crate context
25 /// - `instance`: the instance to be instantiated
26 pub fn get_fn<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'tcx>) -> &'ll Value {
29 debug!("get_fn(instance={:?})", instance);
31 assert!(!instance.substs.needs_infer());
32 assert!(!instance.substs.has_escaping_bound_vars());
34 if let Some(&llfn) = cx.instances.borrow().get(&instance) {
38 let sym = tcx.symbol_name(instance).name;
40 "get_fn({:?}: {:?}) => {}",
42 instance.ty(cx.tcx(), ty::ParamEnv::reveal_all()),
46 let fn_abi = cx.fn_abi_of_instance(instance, ty::List::empty());
48 let llfn = if let Some(llfn) = cx.get_declared_value(sym) {
49 // Create a fn pointer with the new signature.
50 let llptrty = fn_abi.ptr_to_llvm_type(cx);
52 // This is subtle and surprising, but sometimes we have to bitcast
53 // the resulting fn pointer. The reason has to do with external
54 // functions. If you have two crates that both bind the same C
55 // library, they may not use precisely the same types: for
56 // example, they will probably each declare their own structs,
57 // which are distinct types from LLVM's point of view (nominal
60 // Now, if those two crates are linked into an application, and
61 // they contain inlined code, you can wind up with a situation
62 // where both of those functions wind up being loaded into this
63 // application simultaneously. In that case, the same function
64 // (from LLVM's point of view) requires two types. But of course
65 // LLVM won't allow one function to have two types.
67 // What we currently do, therefore, is declare the function with
68 // one of the two types (whichever happens to come first) and then
69 // bitcast as needed when the function is referenced to make sure
70 // it has the type we expect.
72 // This can occur on either a crate-local or crate-external
73 // reference. It also occurs when testing libcore and in some
74 // other weird situations. Annoying.
75 if cx.val_ty(llfn) != llptrty {
76 debug!("get_fn: casting {:?} to {:?}", llfn, llptrty);
77 cx.const_ptrcast(llfn, llptrty)
79 debug!("get_fn: not casting pointer!");
83 let instance_def_id = instance.def_id();
84 let llfn = if tcx.sess.target.arch == "x86" &&
85 let Some(dllimport) = common::get_dllimport(tcx, instance_def_id, sym)
87 cx.declare_fn(&common::i686_decorated_name(&dllimport, common::is_mingw_gnu_toolchain(&tcx.sess.target), true), fn_abi)
89 cx.declare_fn(sym, fn_abi)
91 debug!("get_fn: not casting pointer!");
93 attributes::from_fn_attrs(cx, llfn, instance);
95 // Apply an appropriate linkage/visibility value to our item that we
98 // This is sort of subtle. Inside our codegen unit we started off
99 // compilation by predefining all our own `MonoItem` instances. That
100 // is, everything we're codegenning ourselves is already defined. That
101 // means that anything we're actually codegenning in this codegen unit
102 // will have hit the above branch in `get_declared_value`. As a result,
103 // we're guaranteed here that we're declaring a symbol that won't get
104 // defined, or in other words we're referencing a value from another
105 // codegen unit or even another crate.
107 // So because this is a foreign value we blanket apply an external
108 // linkage directive because it's coming from a different object file.
109 // The visibility here is where it gets tricky. This symbol could be
110 // referencing some foreign crate or foreign library (an `extern`
111 // block) in which case we want to leave the default visibility. We may
112 // also, though, have multiple codegen units. It could be a
113 // monomorphization, in which case its expected visibility depends on
114 // whether we are sharing generics or not. The important thing here is
115 // that the visibility we apply to the declaration is the same one that
116 // has been applied to the definition (wherever that definition may be).
118 llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage);
120 let is_generic = instance.substs.non_erasable_generics().next().is_some();
123 // This is a monomorphization. Its expected visibility depends
124 // on whether we are in share-generics mode.
126 if cx.tcx.sess.opts.share_generics() {
127 // We are in share_generics mode.
129 if let Some(instance_def_id) = instance_def_id.as_local() {
130 // This is a definition from the current crate. If the
131 // definition is unreachable for downstream crates or
132 // the current crate does not re-export generics, the
133 // definition of the instance will have been declared
135 if cx.tcx.is_unreachable_local_definition(instance_def_id)
136 || !cx.tcx.local_crate_exports_generics()
138 llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
141 // This is a monomorphization of a generic function
142 // defined in an upstream crate.
143 if instance.upstream_monomorphization(tcx).is_some() {
144 // This is instantiated in another crate. It cannot
147 // This is a local instantiation of an upstream definition.
148 // If the current crate does not re-export it
149 // (because it is a C library or an executable), it
150 // will have been declared `hidden`.
151 if !cx.tcx.local_crate_exports_generics() {
152 llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
157 // When not sharing generics, all instances are in the same
158 // crate and have hidden visibility
159 llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
162 // This is a non-generic function
163 if cx.tcx.is_codegened_item(instance_def_id) {
164 // This is a function that is instantiated in the local crate
166 if instance_def_id.is_local() {
167 // This is function that is defined in the local crate.
168 // If it is not reachable, it is hidden.
169 if !cx.tcx.is_reachable_non_generic(instance_def_id) {
170 llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
173 // This is a function from an upstream crate that has
174 // been instantiated here. These are always hidden.
175 llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
180 // MinGW: For backward compatibility we rely on the linker to decide whether it
181 // should use dllimport for functions.
182 if cx.use_dll_storage_attrs
183 && tcx.is_dllimport_foreign_item(instance_def_id)
184 && !matches!(tcx.sess.target.env.as_ref(), "gnu" | "uclibc")
186 llvm::LLVMSetDLLStorageClass(llfn, llvm::DLLStorageClass::DllImport);
189 if cx.should_assume_dso_local(llfn, true) {
190 llvm::LLVMRustSetDSOLocal(llfn, true);
197 cx.instances.borrow_mut().insert(instance, llfn);