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
9 use crate::context::CodegenCx;
10 use crate::value::Value;
11 use rustc_codegen_ssa::traits::*;
13 use rustc::ty::{TypeFoldable, Instance};
14 use rustc::ty::layout::{LayoutOf, HasTyCtxt};
16 /// Codegens a reference to a fn/method item, monomorphizing and
17 /// inlining as it goes.
21 /// - `cx`: the crate context
22 /// - `instance`: the instance to be instantiated
24 cx: &CodegenCx<'ll, 'tcx>,
25 instance: Instance<'tcx>,
29 debug!("get_fn(instance={:?})", instance);
31 assert!(!instance.substs.needs_infer());
32 assert!(!instance.substs.has_escaping_bound_vars());
33 assert!(!instance.substs.has_param_types());
35 let sig = instance.fn_sig(cx.tcx());
36 if let Some(&llfn) = cx.instances.borrow().get(&instance) {
40 let sym = tcx.symbol_name(instance).name.as_str();
41 debug!("get_fn({:?}: {:?}) => {}", instance, sig, sym);
43 // Create a fn pointer with the substituted signature.
44 let fn_ptr_ty = tcx.mk_fn_ptr(sig);
45 let llptrty = cx.backend_type(cx.layout_of(fn_ptr_ty));
47 let llfn = if let Some(llfn) = cx.get_declared_value(&sym) {
48 // This is subtle and surprising, but sometimes we have to bitcast
49 // the resulting fn pointer. The reason has to do with external
50 // functions. If you have two crates that both bind the same C
51 // library, they may not use precisely the same types: for
52 // example, they will probably each declare their own structs,
53 // which are distinct types from LLVM's point of view (nominal
56 // Now, if those two crates are linked into an application, and
57 // they contain inlined code, you can wind up with a situation
58 // where both of those functions wind up being loaded into this
59 // application simultaneously. In that case, the same function
60 // (from LLVM's point of view) requires two types. But of course
61 // LLVM won't allow one function to have two types.
63 // What we currently do, therefore, is declare the function with
64 // one of the two types (whichever happens to come first) and then
65 // bitcast as needed when the function is referenced to make sure
66 // it has the type we expect.
68 // This can occur on either a crate-local or crate-external
69 // reference. It also occurs when testing libcore and in some
70 // other weird situations. Annoying.
71 if cx.val_ty(llfn) != llptrty {
72 debug!("get_fn: casting {:?} to {:?}", llfn, llptrty);
73 cx.const_ptrcast(llfn, llptrty)
75 debug!("get_fn: not casting pointer!");
79 let llfn = cx.declare_fn(&sym, sig);
80 assert_eq!(cx.val_ty(llfn), llptrty);
81 debug!("get_fn: not casting pointer!");
83 if instance.def.is_inline(tcx) {
84 attributes::inline(cx, llfn, attributes::InlineAttr::Hint);
86 attributes::from_fn_attrs(cx, llfn, Some(instance.def.def_id()), sig);
88 let instance_def_id = instance.def_id();
90 // Apply an appropriate linkage/visibility value to our item that we
93 // This is sort of subtle. Inside our codegen unit we started off
94 // compilation by predefining all our own `MonoItem` instances. That
95 // is, everything we're codegenning ourselves is already defined. That
96 // means that anything we're actually codegenning in this codegen unit
97 // will have hit the above branch in `get_declared_value`. As a result,
98 // we're guaranteed here that we're declaring a symbol that won't get
99 // defined, or in other words we're referencing a value from another
100 // codegen unit or even another crate.
102 // So because this is a foreign value we blanket apply an external
103 // linkage directive because it's coming from a different object file.
104 // The visibility here is where it gets tricky. This symbol could be
105 // referencing some foreign crate or foreign library (an `extern`
106 // block) in which case we want to leave the default visibility. We may
107 // also, though, have multiple codegen units. It could be a
108 // monomorphization, in which case its expected visibility depends on
109 // whether we are sharing generics or not. The important thing here is
110 // that the visibility we apply to the declaration is the same one that
111 // has been applied to the definition (wherever that definition may be).
113 llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage);
115 let is_generic = instance.substs.non_erasable_generics().next().is_some();
118 // This is a monomorphization. Its expected visibility depends
119 // on whether we are in share-generics mode.
121 if cx.tcx.sess.opts.share_generics() {
122 // We are in share_generics mode.
124 if instance_def_id.is_local() {
125 // This is a definition from the current crate. If the
126 // definition is unreachable for downstream crates or
127 // the current crate does not re-export generics, the
128 // definition of the instance will have been declared
130 if cx.tcx.is_unreachable_local_definition(instance_def_id) ||
131 !cx.tcx.local_crate_exports_generics() {
132 llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
135 // This is a monomorphization of a generic function
136 // defined in an upstream crate.
137 if cx.tcx.upstream_monomorphizations_for(instance_def_id)
138 .map(|set| set.contains_key(instance.substs))
140 // This is instantiated in another crate. It cannot
143 // This is a local instantiation of an upstream definition.
144 // If the current crate does not re-export it
145 // (because it is a C library or an executable), it
146 // will have been declared `hidden`.
147 if !cx.tcx.local_crate_exports_generics() {
148 llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
153 // When not sharing generics, all instances are in the same
154 // crate and have hidden visibility
155 llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
158 // This is a non-generic function
159 if cx.tcx.is_codegened_item(instance_def_id) {
160 // This is a function that is instantiated in the local crate
162 if instance_def_id.is_local() {
163 // This is function that is defined in the local crate.
164 // If it is not reachable, it is hidden.
165 if !cx.tcx.is_reachable_non_generic(instance_def_id) {
166 llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
169 // This is a function from an upstream crate that has
170 // been instantiated here. These are always hidden.
171 llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
177 if cx.use_dll_storage_attrs &&
178 tcx.is_dllimport_foreign_item(instance_def_id)
181 llvm::LLVMSetDLLStorageClass(llfn, llvm::DLLStorageClass::DllImport);
188 cx.instances.borrow_mut().insert(instance, llfn);