]> git.lizzy.rs Git - rust.git/blob - src/callee.rs
d0ae96adcedaf31a4252885a2460b1a7721796a1
[rust.git] / src / callee.rs
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;
6
7 use crate::abi::FnAbiGccExt;
8 use crate::context::CodegenCx;
9
10 /// Codegens a reference to a fn/method item, monomorphizing and
11 /// inlining as it goes.
12 ///
13 /// # Parameters
14 ///
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> {
18     let tcx = cx.tcx();
19
20     //debug!("get_fn(instance={:?})", instance);
21
22     assert!(!instance.substs.needs_infer());
23     assert!(!instance.substs.has_escaping_bound_vars());
24     assert!(!instance.substs.has_param_types_or_consts());
25
26     if let Some(&func) = cx.instances.borrow().get(&instance) {
27         return func;
28     }
29
30     let sym = tcx.symbol_name(instance).name;
31     //debug!("get_fn({:?}: {:?}) => {}", instance, instance.monomorphic_ty(cx.tcx()), sym);
32
33     let fn_abi = FnAbi::of_instance(cx, instance, &[]);
34
35     // TODO
36     let func =
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);
40
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
47             // types).
48             //
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.
55             //
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.
60             //
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);
66                 // TODO
67                 //cx.const_ptrcast(func, ptrty)
68                 func
69             }
70             else {
71                 //debug!("get_fn: not casting pointer!");
72                 func
73             }
74         }
75         else {
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!");
80
81             // TODO
82             //attributes::from_fn_attrs(cx, func, instance);
83
84             //let instance_def_id = instance.def_id();
85
86             // TODO
87             /*if cx.use_dll_storage_attrs && tcx.is_dllimport_foreign_item(instance_def_id) {
88               unsafe {
89               llvm::LLVMSetDLLStorageClass(func, llvm::DLLStorageClass::DllImport);
90               }
91               }*/
92
93             func
94         };
95
96     cx.instances.borrow_mut().insert(instance, func);
97
98     func
99 }