]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/callee.rs
Fix checking for missing stability annotations
[rust.git] / src / librustc_trans / callee.rs
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.
4 //
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.
10
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
15 //! closure.
16
17 use attributes;
18 use common::{self, CrateContext};
19 use consts;
20 use declare;
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 type_of;
27
28 /// Translates a reference to a fn/method item, monomorphizing and
29 /// inlining as it goes.
30 ///
31 /// # Parameters
32 ///
33 /// - `ccx`: the crate context
34 /// - `instance`: the instance to be instantiated
35 pub fn get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
36                         instance: Instance<'tcx>)
37                         -> ValueRef
38 {
39     let tcx = ccx.tcx();
40
41     debug!("get_fn(instance={:?})", instance);
42
43     assert!(!instance.substs.needs_infer());
44     assert!(!instance.substs.has_escaping_regions());
45     assert!(!instance.substs.has_param_types());
46
47     let fn_ty = common::instance_ty(ccx.shared(), &instance);
48     if let Some(&llfn) = ccx.instances().borrow().get(&instance) {
49         return llfn;
50     }
51
52     let sym = tcx.symbol_name(instance);
53     debug!("get_fn({:?}: {:?}) => {}", instance, fn_ty, sym);
54
55     // This is subtle and surprising, but sometimes we have to bitcast
56     // the resulting fn pointer.  The reason has to do with external
57     // functions.  If you have two crates that both bind the same C
58     // library, they may not use precisely the same types: for
59     // example, they will probably each declare their own structs,
60     // which are distinct types from LLVM's point of view (nominal
61     // types).
62     //
63     // Now, if those two crates are linked into an application, and
64     // they contain inlined code, you can wind up with a situation
65     // where both of those functions wind up being loaded into this
66     // application simultaneously. In that case, the same function
67     // (from LLVM's point of view) requires two types. But of course
68     // LLVM won't allow one function to have two types.
69     //
70     // What we currently do, therefore, is declare the function with
71     // one of the two types (whichever happens to come first) and then
72     // bitcast as needed when the function is referenced to make sure
73     // it has the type we expect.
74     //
75     // This can occur on either a crate-local or crate-external
76     // reference. It also occurs when testing libcore and in some
77     // other weird situations. Annoying.
78
79     // Create a fn pointer with the substituted signature.
80     let fn_ptr_ty = tcx.mk_fn_ptr(common::ty_fn_sig(ccx, fn_ty));
81     let llptrty = type_of::type_of(ccx, fn_ptr_ty);
82
83     let llfn = if let Some(llfn) = declare::get_declared_value(ccx, &sym) {
84         if common::val_ty(llfn) != llptrty {
85             debug!("get_fn: casting {:?} to {:?}", llfn, llptrty);
86             consts::ptrcast(llfn, llptrty)
87         } else {
88             debug!("get_fn: not casting pointer!");
89             llfn
90         }
91     } else {
92         let llfn = declare::declare_fn(ccx, &sym, fn_ty);
93         assert_eq!(common::val_ty(llfn), llptrty);
94         debug!("get_fn: not casting pointer!");
95
96         if common::is_inline_instance(tcx, &instance) {
97             attributes::inline(llfn, attributes::InlineAttr::Hint);
98         }
99         let attrs = instance.def.attrs(ccx.tcx());
100         attributes::from_fn_attrs(ccx, &attrs, llfn);
101
102         // Perhaps questionable, but we assume that anything defined
103         // *in Rust code* may unwind. Foreign items like `extern "C" {
104         // fn foo(); }` are assumed not to unwind **unless** they have
105         // a `#[unwind]` attribute.
106         if !tcx.is_foreign_item(instance.def_id()) {
107             attributes::unwind(llfn, true);
108             unsafe {
109                 llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage);
110             }
111         }
112
113         if ccx.use_dll_storage_attrs() &&
114             ccx.sess().cstore.is_dllimport_foreign_item(instance.def_id())
115         {
116             unsafe {
117                 llvm::LLVMSetDLLStorageClass(llfn, llvm::DLLStorageClass::DllImport);
118             }
119         }
120         llfn
121     };
122
123     ccx.instances().borrow_mut().insert(instance, llfn);
124
125     llfn
126 }
127
128 pub fn resolve_and_get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
129                                     def_id: DefId,
130                                     substs: &'tcx Substs<'tcx>)
131                                     -> ValueRef
132 {
133     get_fn(ccx, monomorphize::resolve(ccx.shared(), def_id, substs))
134 }