1 use gccjit::{FunctionType, RValue};
2 use rustc_codegen_ssa::traits::BaseTypeMethods;
3 use rustc_middle::ty::{Instance, TypeFoldable};
4 use rustc_middle::ty::layout::{FnAbiExt, HasTyCtxt};
5 use rustc_target::abi::call::FnAbi;
7 use crate::abi::FnAbiGccExt;
8 use crate::context::CodegenCx;
10 /// Codegens a reference to a fn/method item, monomorphizing and
11 /// inlining as it goes.
15 /// - `cx`: the crate context
16 /// - `instance`: the instance to be instantiated
17 pub fn get_fn<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, instance: Instance<'tcx>) -> RValue<'gcc> {
20 //debug!("get_fn(instance={:?})", instance);
22 assert!(!instance.substs.needs_infer());
23 assert!(!instance.substs.has_escaping_bound_vars());
24 assert!(!instance.substs.has_param_types_or_consts());
26 if let Some(&func) = cx.instances.borrow().get(&instance) {
30 let sym = tcx.symbol_name(instance).name;
31 //debug!("get_fn({:?}: {:?}) => {}", instance, instance.monomorphic_ty(cx.tcx()), sym);
33 let fn_abi = FnAbi::of_instance(cx, instance, &[]);
37 if let Some(func) = cx.get_declared_value(&sym) {
38 // Create a fn pointer with the new signature.
39 let ptrty = fn_abi.ptr_to_gcc_type(cx);
41 // This is subtle and surprising, but sometimes we have to bitcast
42 // the resulting fn pointer. The reason has to do with external
43 // functions. If you have two crates that both bind the same C
44 // library, they may not use precisely the same types: for
45 // example, they will probably each declare their own structs,
46 // which are distinct types from LLVM's point of view (nominal
49 // Now, if those two crates are linked into an application, and
50 // they contain inlined code, you can wind up with a situation
51 // where both of those functions wind up being loaded into this
52 // application simultaneously. In that case, the same function
53 // (from LLVM's point of view) requires two types. But of course
54 // LLVM won't allow one function to have two types.
56 // What we currently do, therefore, is declare the function with
57 // one of the two types (whichever happens to come first) and then
58 // bitcast as needed when the function is referenced to make sure
59 // it has the type we expect.
61 // This can occur on either a crate-local or crate-external
62 // reference. It also occurs when testing libcore and in some
63 // other weird situations. Annoying.
64 if cx.val_ty(func) != ptrty {
65 //debug!("get_fn: casting {:?} to {:?}", func, ptrty);
67 //cx.const_ptrcast(func, ptrty)
71 //debug!("get_fn: not casting pointer!");
76 cx.linkage.set(FunctionType::Extern);
77 let func = cx.declare_fn(&sym, &fn_abi);
78 //cx.linkage.set(FunctionType::Internal);
79 //debug!("get_fn: not casting pointer!");
82 //attributes::from_fn_attrs(cx, func, instance);
84 //let instance_def_id = instance.def_id();
87 /*if cx.use_dll_storage_attrs && tcx.is_dllimport_foreign_item(instance_def_id) {
89 llvm::LLVMSetDLLStorageClass(func, llvm::DLLStorageClass::DllImport);
96 cx.instances.borrow_mut().insert(instance, func);