]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/callee.rs
Do not show `::constructor` on tuple struct diagnostics
[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 llvm::{self, ValueRef};
18 use rustc::hir::def_id::DefId;
19 use rustc::ty::subst::Substs;
20 use attributes;
21 use common::{self, CrateContext};
22 use monomorphize;
23 use consts;
24 use declare;
25 use monomorphize::Instance;
26 use trans_item::TransItem;
27 use type_of;
28 use rustc::ty::TypeFoldable;
29
30 /// Translates a reference to a fn/method item, monomorphizing and
31 /// inlining as it goes.
32 ///
33 /// # Parameters
34 ///
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>)
39                         -> ValueRef
40 {
41     let tcx = ccx.tcx();
42
43     debug!("get_fn(instance={:?})", instance);
44
45     assert!(!instance.substs.needs_infer());
46     assert!(!instance.substs.has_escaping_regions());
47     assert!(!instance.substs.has_param_types());
48
49     let fn_ty = common::instance_ty(ccx.shared(), &instance);
50     if let Some(&llfn) = ccx.instances().borrow().get(&instance) {
51         return llfn;
52     }
53
54     let sym = ccx.symbol_map().get_or_compute(ccx.shared(),
55                                               TransItem::Fn(instance));
56     debug!("get_fn({:?}: {:?}) => {}", instance, fn_ty, sym);
57
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
64     // types).
65     //
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.
72     //
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.
77     //
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.
81
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);
85
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)
90         } else {
91             debug!("get_fn: not casting pointer!");
92             llfn
93         }
94     } else {
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!");
98
99         if common::is_inline_instance(tcx, &instance) {
100             attributes::inline(llfn, attributes::InlineAttr::Hint);
101         }
102         let attrs = instance.def.attrs(ccx.tcx());
103         attributes::from_fn_attrs(ccx, &attrs, llfn);
104
105         let is_local_def = ccx.shared().translation_items().borrow()
106                               .contains(&TransItem::Fn(instance));
107         if is_local_def {
108             // FIXME(eddyb) Doubt all extern fn should allow unwinding.
109             attributes::unwind(llfn, true);
110             unsafe {
111                 llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage);
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 }