1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Handles translation of callees as well as other call-related
12 //! things. Callees are a superset of normal rust values and sometimes
13 //! have different representations. In particular, top-level fn items
14 //! and methods are represented as just a fn ptr and not a full
17 use llvm::{self, ValueRef};
18 use rustc::hir::def_id::DefId;
19 use rustc::ty::subst::Substs;
21 use common::{self, CrateContext};
25 use monomorphize::Instance;
26 use trans_item::TransItem;
28 use rustc::ty::TypeFoldable;
30 /// Translates a reference to a fn/method item, monomorphizing and
31 /// inlining as it goes.
35 /// - `ccx`: the crate context
36 /// - `instance`: the instance to be instantiated
37 pub fn get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
38 instance: Instance<'tcx>)
43 debug!("get_fn(instance={:?})", instance);
45 assert!(!instance.substs.needs_infer());
46 assert!(!instance.substs.has_escaping_regions());
47 assert!(!instance.substs.has_param_types());
49 let fn_ty = common::instance_ty(ccx.shared(), &instance);
50 if let Some(&llfn) = ccx.instances().borrow().get(&instance) {
54 let sym = ccx.symbol_map().get_or_compute(ccx.shared(),
55 TransItem::Fn(instance));
56 debug!("get_fn({:?}: {:?}) => {}", instance, fn_ty, sym);
58 // This is subtle and surprising, but sometimes we have to bitcast
59 // the resulting fn pointer. The reason has to do with external
60 // functions. If you have two crates that both bind the same C
61 // library, they may not use precisely the same types: for
62 // example, they will probably each declare their own structs,
63 // which are distinct types from LLVM's point of view (nominal
66 // Now, if those two crates are linked into an application, and
67 // they contain inlined code, you can wind up with a situation
68 // where both of those functions wind up being loaded into this
69 // application simultaneously. In that case, the same function
70 // (from LLVM's point of view) requires two types. But of course
71 // LLVM won't allow one function to have two types.
73 // What we currently do, therefore, is declare the function with
74 // one of the two types (whichever happens to come first) and then
75 // bitcast as needed when the function is referenced to make sure
76 // it has the type we expect.
78 // This can occur on either a crate-local or crate-external
79 // reference. It also occurs when testing libcore and in some
80 // other weird situations. Annoying.
82 // Create a fn pointer with the substituted signature.
83 let fn_ptr_ty = tcx.mk_fn_ptr(common::ty_fn_sig(ccx, fn_ty));
84 let llptrty = type_of::type_of(ccx, fn_ptr_ty);
86 let llfn = if let Some(llfn) = declare::get_declared_value(ccx, &sym) {
87 if common::val_ty(llfn) != llptrty {
88 debug!("get_fn: casting {:?} to {:?}", llfn, llptrty);
89 consts::ptrcast(llfn, llptrty)
91 debug!("get_fn: not casting pointer!");
95 let llfn = declare::declare_fn(ccx, &sym, fn_ty);
96 assert_eq!(common::val_ty(llfn), llptrty);
97 debug!("get_fn: not casting pointer!");
99 if common::is_inline_instance(tcx, &instance) {
100 attributes::inline(llfn, attributes::InlineAttr::Hint);
102 let attrs = instance.def.attrs(ccx.tcx());
103 attributes::from_fn_attrs(ccx, &attrs, llfn);
105 let is_local_def = ccx.shared().translation_items().borrow()
106 .contains(&TransItem::Fn(instance));
108 // FIXME(eddyb) Doubt all extern fn should allow unwinding.
109 attributes::unwind(llfn, true);
111 llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage);
114 if ccx.use_dll_storage_attrs() &&
115 ccx.sess().cstore.is_dllimport_foreign_item(instance.def_id())
118 llvm::LLVMSetDLLStorageClass(llfn, llvm::DLLStorageClass::DllImport);
124 ccx.instances().borrow_mut().insert(instance, llfn);
129 pub fn resolve_and_get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
131 substs: &'tcx Substs<'tcx>)
134 get_fn(ccx, monomorphize::resolve(ccx.shared(), def_id, substs))