]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/callee.rs
Auto merge of #44350 - GuillaumeGomez:id-false-positive, r=QuietMisdreavus
[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.tcx(), &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     // Create a fn pointer with the substituted signature.
56     let fn_ptr_ty = tcx.mk_fn_ptr(common::ty_fn_sig(ccx, fn_ty));
57     let llptrty = type_of::type_of(ccx, fn_ptr_ty);
58
59     let llfn = if let Some(llfn) = declare::get_declared_value(ccx, &sym) {
60         // This is subtle and surprising, but sometimes we have to bitcast
61         // the resulting fn pointer.  The reason has to do with external
62         // functions.  If you have two crates that both bind the same C
63         // library, they may not use precisely the same types: for
64         // example, they will probably each declare their own structs,
65         // which are distinct types from LLVM's point of view (nominal
66         // types).
67         //
68         // Now, if those two crates are linked into an application, and
69         // they contain inlined code, you can wind up with a situation
70         // where both of those functions wind up being loaded into this
71         // application simultaneously. In that case, the same function
72         // (from LLVM's point of view) requires two types. But of course
73         // LLVM won't allow one function to have two types.
74         //
75         // What we currently do, therefore, is declare the function with
76         // one of the two types (whichever happens to come first) and then
77         // bitcast as needed when the function is referenced to make sure
78         // it has the type we expect.
79         //
80         // This can occur on either a crate-local or crate-external
81         // reference. It also occurs when testing libcore and in some
82         // other weird situations. Annoying.
83         if common::val_ty(llfn) != llptrty {
84             debug!("get_fn: casting {:?} to {:?}", llfn, llptrty);
85             consts::ptrcast(llfn, llptrty)
86         } else {
87             debug!("get_fn: not casting pointer!");
88             llfn
89         }
90     } else {
91         let llfn = declare::declare_fn(ccx, &sym, fn_ty);
92         assert_eq!(common::val_ty(llfn), llptrty);
93         debug!("get_fn: not casting pointer!");
94
95         if common::is_inline_instance(tcx, &instance) {
96             attributes::inline(llfn, attributes::InlineAttr::Hint);
97         }
98         let attrs = instance.def.attrs(ccx.tcx());
99         attributes::from_fn_attrs(ccx, &attrs, llfn);
100
101         let instance_def_id = instance.def_id();
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         }
110
111         // Apply an appropriate linkage/visibility value to our item that we
112         // just declared.
113         //
114         // This is sort of subtle. Inside our codegen unit we started off
115         // compilation by predefining all our own `TransItem` instances. That
116         // is, everything we're translating ourselves is already defined. That
117         // means that anything we're actually translating ourselves will have
118         // hit the above branch in `get_declared_value`. As a result, we're
119         // guaranteed here that we're declaring a symbol that won't get defined,
120         // or in other words we're referencing a foreign value.
121         //
122         // So because this is a foreign value we blanket apply an external
123         // linkage directive because it's coming from a different object file.
124         // The visibility here is where it gets tricky. This symbol could be
125         // referencing some foreign crate or foreign library (an `extern`
126         // block) in which case we want to leave the default visibility. We may
127         // also, though, have multiple codegen units.
128         //
129         // In the situation of multiple codegen units this function may be
130         // referencing a function from another codegen unit. If we're
131         // indeed referencing a symbol in another codegen unit then we're in one
132         // of two cases:
133         //
134         //  * This is a symbol defined in a foreign crate and we're just
135         //    monomorphizing in another codegen unit. In this case this symbols
136         //    is for sure not exported, so both codegen units will be using
137         //    hidden visibility. Hence, we apply a hidden visibility here.
138         //
139         //  * This is a symbol defined in our local crate. If the symbol in the
140         //    other codegen unit is also not exported then like with the foreign
141         //    case we apply a hidden visibility. If the symbol is exported from
142         //    the foreign object file, however, then we leave this at the
143         //    default visibility as we'll just import it naturally.
144         unsafe {
145             llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage);
146
147             if ccx.tcx().is_translated_function(instance_def_id) {
148                 if instance_def_id.is_local() {
149                     if !ccx.tcx().is_exported_symbol(instance_def_id) {
150                         llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
151                     }
152                 } else {
153                     llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
154                 }
155             }
156         }
157
158         // FIXME(#42293) we should actually track this, but fails too many tests
159         // today.
160         tcx.dep_graph.with_ignore(|| {
161             if ccx.use_dll_storage_attrs() &&
162                 tcx.is_dllimport_foreign_item(instance_def_id)
163             {
164                 unsafe {
165                     llvm::LLVMSetDLLStorageClass(llfn, llvm::DLLStorageClass::DllImport);
166                 }
167             }
168         });
169         llfn
170     };
171
172     ccx.instances().borrow_mut().insert(instance, llfn);
173
174     llfn
175 }
176
177 pub fn resolve_and_get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
178                                     def_id: DefId,
179                                     substs: &'tcx Substs<'tcx>)
180                                     -> ValueRef
181 {
182     get_fn(ccx, monomorphize::resolve(ccx.tcx(), def_id, substs))
183 }