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
18 use common::{self, CrateContext};
21 use llvm::{self, ValueRef};
22 use monomorphize::{self, Instance};
23 use rustc::hir::def_id::DefId;
24 use rustc::ty::TypeFoldable;
25 use rustc::ty::subst::Substs;
26 use trans_item::TransItem;
29 /// Translates a reference to a fn/method item, monomorphizing and
30 /// inlining as it goes.
34 /// - `ccx`: the crate context
35 /// - `instance`: the instance to be instantiated
36 pub fn get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
37 instance: Instance<'tcx>)
42 debug!("get_fn(instance={:?})", instance);
44 assert!(!instance.substs.needs_infer());
45 assert!(!instance.substs.has_escaping_regions());
46 assert!(!instance.substs.has_param_types());
48 let fn_ty = common::instance_ty(ccx.shared(), &instance);
49 if let Some(&llfn) = ccx.instances().borrow().get(&instance) {
53 let sym = tcx.symbol_name(instance);
54 debug!("get_fn({:?}: {:?}) => {}", instance, fn_ty, sym);
56 // This is subtle and surprising, but sometimes we have to bitcast
57 // the resulting fn pointer. The reason has to do with external
58 // functions. If you have two crates that both bind the same C
59 // library, they may not use precisely the same types: for
60 // example, they will probably each declare their own structs,
61 // which are distinct types from LLVM's point of view (nominal
64 // Now, if those two crates are linked into an application, and
65 // they contain inlined code, you can wind up with a situation
66 // where both of those functions wind up being loaded into this
67 // application simultaneously. In that case, the same function
68 // (from LLVM's point of view) requires two types. But of course
69 // LLVM won't allow one function to have two types.
71 // What we currently do, therefore, is declare the function with
72 // one of the two types (whichever happens to come first) and then
73 // bitcast as needed when the function is referenced to make sure
74 // it has the type we expect.
76 // This can occur on either a crate-local or crate-external
77 // reference. It also occurs when testing libcore and in some
78 // other weird situations. Annoying.
80 // Create a fn pointer with the substituted signature.
81 let fn_ptr_ty = tcx.mk_fn_ptr(common::ty_fn_sig(ccx, fn_ty));
82 let llptrty = type_of::type_of(ccx, fn_ptr_ty);
84 let llfn = if let Some(llfn) = declare::get_declared_value(ccx, &sym) {
85 if common::val_ty(llfn) != llptrty {
86 debug!("get_fn: casting {:?} to {:?}", llfn, llptrty);
87 consts::ptrcast(llfn, llptrty)
89 debug!("get_fn: not casting pointer!");
93 let llfn = declare::declare_fn(ccx, &sym, fn_ty);
94 assert_eq!(common::val_ty(llfn), llptrty);
95 debug!("get_fn: not casting pointer!");
97 if common::is_inline_instance(tcx, &instance) {
98 attributes::inline(llfn, attributes::InlineAttr::Hint);
100 let attrs = instance.def.attrs(ccx.tcx());
101 attributes::from_fn_attrs(ccx, &attrs, llfn);
103 let instance_def_id = instance.def_id();
105 // Perhaps questionable, but we assume that anything defined
106 // *in Rust code* may unwind. Foreign items like `extern "C" {
107 // fn foo(); }` are assumed not to unwind **unless** they have
108 // a `#[unwind]` attribute.
109 if !tcx.is_foreign_item(instance_def_id) {
110 attributes::unwind(llfn, true);
114 llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage);
116 if ccx.crate_trans_items().contains(&TransItem::Fn(instance)) {
117 if let Some(node_id) = tcx.hir.as_local_node_id(instance_def_id) {
118 if !ccx.exported_symbols().local_exports().contains(&node_id) {
119 llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
122 llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
127 if ccx.use_dll_storage_attrs() &&
128 tcx.is_dllimport_foreign_item(instance_def_id)
131 llvm::LLVMSetDLLStorageClass(llfn, llvm::DLLStorageClass::DllImport);
137 ccx.instances().borrow_mut().insert(instance, llfn);
142 pub fn resolve_and_get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
144 substs: &'tcx Substs<'tcx>)
147 get_fn(ccx, monomorphize::resolve(ccx.shared(), def_id, substs))