]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/callee.rs
Auto merge of #41433 - estebank:constructor, r=michaelwoerister
[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 trans_item::TransItem;
27 use type_of;
28
29 /// Translates a reference to a fn/method item, monomorphizing and
30 /// inlining as it goes.
31 ///
32 /// # Parameters
33 ///
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>)
38                         -> ValueRef
39 {
40     let tcx = ccx.tcx();
41
42     debug!("get_fn(instance={:?})", instance);
43
44     assert!(!instance.substs.needs_infer());
45     assert!(!instance.substs.has_escaping_regions());
46     assert!(!instance.substs.has_param_types());
47
48     let fn_ty = common::instance_ty(ccx.shared(), &instance);
49     if let Some(&llfn) = ccx.instances().borrow().get(&instance) {
50         return llfn;
51     }
52
53     let sym = ccx.symbol_cache().get(TransItem::Fn(instance));
54     debug!("get_fn({:?}: {:?}) => {}", instance, fn_ty, sym);
55
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
62     // types).
63     //
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.
70     //
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.
75     //
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.
79
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);
83
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)
88         } else {
89             debug!("get_fn: not casting pointer!");
90             llfn
91         }
92     } else {
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!");
96
97         if common::is_inline_instance(tcx, &instance) {
98             attributes::inline(llfn, attributes::InlineAttr::Hint);
99         }
100         let attrs = instance.def.attrs(ccx.tcx());
101         attributes::from_fn_attrs(ccx, &attrs, llfn);
102
103         // Perhaps questionable, but we assume that anything defined
104         // *in Rust code* may unwind. Foreign items like `extern "C" {
105         // fn foo(); }` are assumed not to unwind **unless** they have
106         // a `#[unwind]` attribute.
107         if !tcx.is_foreign_item(instance.def_id()) {
108             attributes::unwind(llfn, true);
109             unsafe {
110                 llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage);
111             }
112         }
113
114         if ccx.use_dll_storage_attrs() &&
115             ccx.sess().cstore.is_dllimport_foreign_item(instance.def_id())
116         {
117             unsafe {
118                 llvm::LLVMSetDLLStorageClass(llfn, llvm::DLLStorageClass::DllImport);
119             }
120         }
121         llfn
122     };
123
124     ccx.instances().borrow_mut().insert(instance, llfn);
125
126     llfn
127 }
128
129 pub fn resolve_and_get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
130                                     def_id: DefId,
131                                     substs: &'tcx Substs<'tcx>)
132                                     -> ValueRef
133 {
134     get_fn(ccx, monomorphize::resolve(ccx.shared(), def_id, substs))
135 }