]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/callee.rs
trans: Collect drop-glue translation item for closure env in fn-once-adapters.
[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 pub use self::CalleeData::*;
18
19 use llvm::{self, ValueRef, get_params};
20 use rustc::hir::def_id::DefId;
21 use rustc::ty::subst::Substs;
22 use rustc::traits;
23 use abi::{Abi, FnType};
24 use attributes;
25 use base;
26 use base::*;
27 use common::{
28     self, CrateContext, FunctionContext, SharedCrateContext
29 };
30 use adt::MaybeSizedValue;
31 use consts;
32 use declare;
33 use value::Value;
34 use meth;
35 use monomorphize::{self, Instance};
36 use trans_item::TransItem;
37 use type_of;
38 use Disr;
39 use rustc::ty::{self, Ty, TypeFoldable};
40 use rustc::hir;
41 use std::iter;
42
43 use syntax_pos::DUMMY_SP;
44
45 #[derive(Debug)]
46 pub enum CalleeData {
47     /// Constructor for enum variant/tuple-like-struct.
48     NamedTupleConstructor(Disr),
49
50     /// Function pointer.
51     Fn(ValueRef),
52
53     Intrinsic,
54
55     /// Trait object found in the vtable at that index.
56     Virtual(usize)
57 }
58
59 #[derive(Debug)]
60 pub struct Callee<'tcx> {
61     pub data: CalleeData,
62     pub ty: Ty<'tcx>
63 }
64
65 impl<'tcx> Callee<'tcx> {
66     /// Function pointer.
67     pub fn ptr(llfn: ValueRef, ty: Ty<'tcx>) -> Callee<'tcx> {
68         Callee {
69             data: Fn(llfn),
70             ty: ty
71         }
72     }
73
74     /// Function or method definition.
75     pub fn def<'a>(ccx: &CrateContext<'a, 'tcx>, def_id: DefId, substs: &'tcx Substs<'tcx>)
76                    -> Callee<'tcx> {
77         let tcx = ccx.tcx();
78
79         if let Some(trait_id) = tcx.trait_of_item(def_id) {
80             return Callee::trait_method(ccx, trait_id, def_id, substs);
81         }
82
83         let fn_ty = def_ty(ccx.shared(), def_id, substs);
84         if let ty::TyFnDef(.., f) = fn_ty.sty {
85             if f.abi == Abi::RustIntrinsic || f.abi == Abi::PlatformIntrinsic {
86                 return Callee {
87                     data: Intrinsic,
88                     ty: fn_ty
89                 };
90             }
91         }
92
93         // FIXME(eddyb) Detect ADT constructors more efficiently.
94         if let Some(adt_def) = fn_ty.fn_ret().skip_binder().ty_adt_def() {
95             if let Some(v) = adt_def.variants.iter().find(|v| def_id == v.did) {
96                 return Callee {
97                     data: NamedTupleConstructor(Disr::from(v.disr_val)),
98                     ty: fn_ty
99                 };
100             }
101         }
102
103         let (llfn, ty) = get_fn(ccx, def_id, substs);
104         Callee::ptr(llfn, ty)
105     }
106
107     /// Trait method, which has to be resolved to an impl method.
108     pub fn trait_method<'a>(ccx: &CrateContext<'a, 'tcx>,
109                             trait_id: DefId,
110                             def_id: DefId,
111                             substs: &'tcx Substs<'tcx>)
112                             -> Callee<'tcx> {
113         let tcx = ccx.tcx();
114
115         let trait_ref = ty::TraitRef::from_method(tcx, trait_id, substs);
116         let trait_ref = tcx.normalize_associated_type(&ty::Binder(trait_ref));
117         match common::fulfill_obligation(ccx.shared(), DUMMY_SP, trait_ref) {
118             traits::VtableImpl(vtable_impl) => {
119                 let name = tcx.item_name(def_id);
120                 let (def_id, substs) = traits::find_method(tcx, name, substs, &vtable_impl);
121
122                 // Translate the function, bypassing Callee::def.
123                 // That is because default methods have the same ID as the
124                 // trait method used to look up the impl method that ended
125                 // up here, so calling Callee::def would infinitely recurse.
126                 let (llfn, ty) = get_fn(ccx, def_id, substs);
127                 Callee::ptr(llfn, ty)
128             }
129             traits::VtableClosure(vtable_closure) => {
130                 // The substitutions should have no type parameters remaining
131                 // after passing through fulfill_obligation
132                 let trait_closure_kind = tcx.lang_items.fn_trait_kind(trait_id).unwrap();
133                 let instance = Instance::new(def_id, substs);
134                 let llfn = trans_closure_method(
135                     ccx,
136                     vtable_closure.closure_def_id,
137                     vtable_closure.substs,
138                     instance,
139                     trait_closure_kind);
140
141                 let method_ty = def_ty(ccx.shared(), def_id, substs);
142                 Callee::ptr(llfn, method_ty)
143             }
144             traits::VtableFnPointer(vtable_fn_pointer) => {
145                 let trait_closure_kind = tcx.lang_items.fn_trait_kind(trait_id).unwrap();
146                 let instance = Instance::new(def_id, substs);
147                 let llfn = trans_fn_pointer_shim(ccx, instance,
148                                                  trait_closure_kind,
149                                                  vtable_fn_pointer.fn_ty);
150
151                 let method_ty = def_ty(ccx.shared(), def_id, substs);
152                 Callee::ptr(llfn, method_ty)
153             }
154             traits::VtableObject(ref data) => {
155                 Callee {
156                     data: Virtual(tcx.get_vtable_index_of_object_method(data, def_id)),
157                     ty: def_ty(ccx.shared(), def_id, substs)
158                 }
159             }
160             vtable => {
161                 bug!("resolved vtable bad vtable {:?} in trans", vtable);
162             }
163         }
164     }
165
166     /// Get the abi::FnType for a direct call. Mainly deals with the fact
167     /// that a Virtual call doesn't take the vtable, like its shim does.
168     /// The extra argument types are for variadic (extern "C") functions.
169     pub fn direct_fn_type<'a>(&self, ccx: &CrateContext<'a, 'tcx>,
170                               extra_args: &[Ty<'tcx>]) -> FnType {
171         let abi = self.ty.fn_abi();
172         let sig = ccx.tcx().erase_late_bound_regions_and_normalize(self.ty.fn_sig());
173         let mut fn_ty = FnType::unadjusted(ccx, abi, &sig, extra_args);
174         if let Virtual(_) = self.data {
175             // Don't pass the vtable, it's not an argument of the virtual fn.
176             fn_ty.args[1].ignore();
177         }
178         fn_ty.adjust_for_abi(ccx, abi, &sig);
179         fn_ty
180     }
181
182     /// Turn the callee into a function pointer.
183     pub fn reify<'a>(self, ccx: &CrateContext<'a, 'tcx>) -> ValueRef {
184         match self.data {
185             Fn(llfn) => llfn,
186             Virtual(_) => meth::trans_object_shim(ccx, self),
187             NamedTupleConstructor(disr) => match self.ty.sty {
188                 ty::TyFnDef(def_id, substs, _) => {
189                     let instance = Instance::new(def_id, substs);
190                     if let Some(&llfn) = ccx.instances().borrow().get(&instance) {
191                         return llfn;
192                     }
193
194                     let sym = ccx.symbol_map().get_or_compute(ccx.shared(),
195                                                               TransItem::Fn(instance));
196                     assert!(!ccx.codegen_unit().contains_item(&TransItem::Fn(instance)));
197                     let lldecl = declare::define_internal_fn(ccx, &sym, self.ty);
198                     base::trans_ctor_shim(ccx, def_id, substs, disr, lldecl);
199                     ccx.instances().borrow_mut().insert(instance, lldecl);
200
201                     lldecl
202                 }
203                 _ => bug!("expected fn item type, found {}", self.ty)
204             },
205             Intrinsic => bug!("intrinsic {} getting reified", self.ty)
206         }
207     }
208 }
209
210 /// Given a DefId and some Substs, produces the monomorphic item type.
211 fn def_ty<'a, 'tcx>(shared: &SharedCrateContext<'a, 'tcx>,
212                     def_id: DefId,
213                     substs: &'tcx Substs<'tcx>)
214                     -> Ty<'tcx> {
215     let ty = shared.tcx().item_type(def_id);
216     monomorphize::apply_param_substs(shared, substs, &ty)
217 }
218
219
220 fn trans_closure_method<'a, 'tcx>(ccx: &'a CrateContext<'a, 'tcx>,
221                                   def_id: DefId,
222                                   substs: ty::ClosureSubsts<'tcx>,
223                                   method_instance: Instance<'tcx>,
224                                   trait_closure_kind: ty::ClosureKind)
225                                   -> ValueRef
226 {
227     // If this is a closure, redirect to it.
228     let (llfn, _) = get_fn(ccx, def_id, substs.substs);
229
230     // If the closure is a Fn closure, but a FnOnce is needed (etc),
231     // then adapt the self type
232     let llfn_closure_kind = ccx.tcx().closure_kind(def_id);
233
234     debug!("trans_closure_adapter_shim(llfn_closure_kind={:?}, \
235            trait_closure_kind={:?}, llfn={:?})",
236            llfn_closure_kind, trait_closure_kind, Value(llfn));
237
238     match needs_fn_once_adapter_shim(llfn_closure_kind, trait_closure_kind) {
239         Ok(true) => trans_fn_once_adapter_shim(ccx,
240                                                def_id,
241                                                substs,
242                                                method_instance,
243                                                llfn),
244         Ok(false) => llfn,
245         Err(()) => {
246             bug!("trans_closure_adapter_shim: cannot convert {:?} to {:?}",
247                  llfn_closure_kind,
248                  trait_closure_kind);
249         }
250     }
251 }
252
253 pub fn needs_fn_once_adapter_shim(actual_closure_kind: ty::ClosureKind,
254                                   trait_closure_kind: ty::ClosureKind)
255                                   -> Result<bool, ()>
256 {
257     match (actual_closure_kind, trait_closure_kind) {
258         (ty::ClosureKind::Fn, ty::ClosureKind::Fn) |
259         (ty::ClosureKind::FnMut, ty::ClosureKind::FnMut) |
260         (ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => {
261             // No adapter needed.
262            Ok(false)
263         }
264         (ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => {
265             // The closure fn `llfn` is a `fn(&self, ...)`.  We want a
266             // `fn(&mut self, ...)`. In fact, at trans time, these are
267             // basically the same thing, so we can just return llfn.
268             Ok(false)
269         }
270         (ty::ClosureKind::Fn, ty::ClosureKind::FnOnce) |
271         (ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
272             // The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut
273             // self, ...)`.  We want a `fn(self, ...)`. We can produce
274             // this by doing something like:
275             //
276             //     fn call_once(self, ...) { call_mut(&self, ...) }
277             //     fn call_once(mut self, ...) { call_mut(&mut self, ...) }
278             //
279             // These are both the same at trans time.
280             Ok(true)
281         }
282         _ => Err(()),
283     }
284 }
285
286 fn trans_fn_once_adapter_shim<'a, 'tcx>(
287     ccx: &'a CrateContext<'a, 'tcx>,
288     def_id: DefId,
289     substs: ty::ClosureSubsts<'tcx>,
290     method_instance: Instance<'tcx>,
291     llreffn: ValueRef)
292     -> ValueRef
293 {
294     if let Some(&llfn) = ccx.instances().borrow().get(&method_instance) {
295         return llfn;
296     }
297
298     debug!("trans_fn_once_adapter_shim(def_id={:?}, substs={:?}, llreffn={:?})",
299            def_id, substs, Value(llreffn));
300
301     let tcx = ccx.tcx();
302
303     // Find a version of the closure type. Substitute static for the
304     // region since it doesn't really matter.
305     let closure_ty = tcx.mk_closure_from_closure_substs(def_id, substs);
306     let ref_closure_ty = tcx.mk_imm_ref(tcx.mk_region(ty::ReErased), closure_ty);
307
308     // Make a version with the type of by-ref closure.
309     let ty::ClosureTy { unsafety, abi, mut sig } = tcx.closure_type(def_id, substs);
310     sig.0 = tcx.mk_fn_sig(
311         iter::once(ref_closure_ty).chain(sig.0.inputs().iter().cloned()),
312         sig.0.output(),
313         sig.0.variadic
314     );
315     let llref_fn_ty = tcx.mk_fn_ptr(tcx.mk_bare_fn(ty::BareFnTy {
316         unsafety: unsafety,
317         abi: abi,
318         sig: sig.clone()
319     }));
320     debug!("trans_fn_once_adapter_shim: llref_fn_ty={:?}",
321            llref_fn_ty);
322
323
324     // Make a version of the closure type with the same arguments, but
325     // with argument #0 being by value.
326     assert_eq!(abi, Abi::RustCall);
327     sig.0 = tcx.mk_fn_sig(
328         iter::once(closure_ty).chain(sig.0.inputs().iter().skip(1).cloned()),
329         sig.0.output(),
330         sig.0.variadic
331     );
332
333     let sig = tcx.erase_late_bound_regions_and_normalize(&sig);
334     let fn_ty = FnType::new(ccx, abi, &sig, &[]);
335
336     let llonce_fn_ty = tcx.mk_fn_ptr(tcx.mk_bare_fn(ty::BareFnTy {
337         unsafety: unsafety,
338         abi: abi,
339         sig: ty::Binder(sig)
340     }));
341
342     // Create the by-value helper.
343     let function_name = method_instance.symbol_name(ccx.shared());
344     let lloncefn = declare::define_internal_fn(ccx, &function_name, llonce_fn_ty);
345     attributes::set_frame_pointer_elimination(ccx, lloncefn);
346
347     let orig_fn_ty = fn_ty;
348     let fcx = FunctionContext::new(ccx, lloncefn);
349     let mut bcx = fcx.get_entry_block();
350
351     let callee = Callee {
352         data: Fn(llreffn),
353         ty: llref_fn_ty
354     };
355
356     // the first argument (`self`) will be the (by value) closure env.
357
358     let mut llargs = get_params(fcx.llfn);
359     let fn_ret = callee.ty.fn_ret();
360     let fn_ty = callee.direct_fn_type(bcx.ccx, &[]);
361     let self_idx = fn_ty.ret.is_indirect() as usize;
362     let env_arg = &orig_fn_ty.args[0];
363     let llenv = if env_arg.is_indirect() {
364         llargs[self_idx]
365     } else {
366         let scratch = alloc_ty(&bcx, closure_ty, "self");
367         let mut llarg_idx = self_idx;
368         env_arg.store_fn_arg(&bcx, &mut llarg_idx, scratch);
369         scratch
370     };
371
372     debug!("trans_fn_once_adapter_shim: env={:?}", Value(llenv));
373     // Adjust llargs such that llargs[self_idx..] has the call arguments.
374     // For zero-sized closures that means sneaking in a new argument.
375     if env_arg.is_ignore() {
376         llargs.insert(self_idx, llenv);
377     } else {
378         llargs[self_idx] = llenv;
379     }
380
381     // Call the by-ref closure body with `self` in a cleanup scope,
382     // to drop `self` when the body returns, or in case it unwinds.
383     let self_scope = fcx.schedule_drop_mem(MaybeSizedValue::sized(llenv), closure_ty);
384
385     let llfn = callee.reify(bcx.ccx);
386     let llret;
387     if let Some(landing_pad) = self_scope.landing_pad {
388         let normal_bcx = bcx.fcx().build_new_block("normal-return");
389         llret = bcx.invoke(llfn, &llargs[..], normal_bcx.llbb(), landing_pad, None);
390         bcx = normal_bcx;
391     } else {
392         llret = bcx.call(llfn, &llargs[..], None);
393     }
394     fn_ty.apply_attrs_callsite(llret);
395
396     if fn_ret.0.is_never() {
397         bcx.unreachable();
398     } else {
399         self_scope.trans(&bcx);
400
401         if fn_ty.ret.is_indirect() || fn_ty.ret.is_ignore() {
402             bcx.ret_void();
403         } else {
404             bcx.ret(llret);
405         }
406     }
407
408     ccx.instances().borrow_mut().insert(method_instance, lloncefn);
409
410     lloncefn
411 }
412
413 /// Translates an adapter that implements the `Fn` trait for a fn
414 /// pointer. This is basically the equivalent of something like:
415 ///
416 /// ```
417 /// impl<'a> Fn(&'a int) -> &'a int for fn(&int) -> &int {
418 ///     extern "rust-abi" fn call(&self, args: (&'a int,)) -> &'a int {
419 ///         (*self)(args.0)
420 ///     }
421 /// }
422 /// ```
423 ///
424 /// but for the bare function type given.
425 fn trans_fn_pointer_shim<'a, 'tcx>(
426     ccx: &'a CrateContext<'a, 'tcx>,
427     method_instance: Instance<'tcx>,
428     closure_kind: ty::ClosureKind,
429     bare_fn_ty: Ty<'tcx>)
430     -> ValueRef
431 {
432     let tcx = ccx.tcx();
433
434     // Normalize the type for better caching.
435     let bare_fn_ty = tcx.normalize_associated_type(&bare_fn_ty);
436
437     // If this is an impl of `Fn` or `FnMut` trait, the receiver is `&self`.
438     let is_by_ref = match closure_kind {
439         ty::ClosureKind::Fn | ty::ClosureKind::FnMut => true,
440         ty::ClosureKind::FnOnce => false,
441     };
442
443     let llfnpointer = match bare_fn_ty.sty {
444         ty::TyFnDef(def_id, substs, _) => {
445             // Function definitions have to be turned into a pointer.
446             let llfn = Callee::def(ccx, def_id, substs).reify(ccx);
447             if !is_by_ref {
448                 // A by-value fn item is ignored, so the shim has
449                 // the same signature as the original function.
450                 return llfn;
451             }
452             Some(llfn)
453         }
454         _ => None
455     };
456
457     let bare_fn_ty_maybe_ref = if is_by_ref {
458         tcx.mk_imm_ref(tcx.mk_region(ty::ReErased), bare_fn_ty)
459     } else {
460         bare_fn_ty
461     };
462
463     // Check if we already trans'd this shim.
464     if let Some(&llval) = ccx.fn_pointer_shims().borrow().get(&bare_fn_ty_maybe_ref) {
465         return llval;
466     }
467
468     debug!("trans_fn_pointer_shim(bare_fn_ty={:?})",
469            bare_fn_ty);
470
471     // Construct the "tuply" version of `bare_fn_ty`. It takes two arguments: `self`,
472     // which is the fn pointer, and `args`, which is the arguments tuple.
473     let sig = match bare_fn_ty.sty {
474         ty::TyFnDef(..,
475                     &ty::BareFnTy { unsafety: hir::Unsafety::Normal,
476                                     abi: Abi::Rust,
477                                     ref sig }) |
478         ty::TyFnPtr(&ty::BareFnTy { unsafety: hir::Unsafety::Normal,
479                                     abi: Abi::Rust,
480                                     ref sig }) => sig,
481
482         _ => {
483             bug!("trans_fn_pointer_shim invoked on invalid type: {}",
484                  bare_fn_ty);
485         }
486     };
487     let sig = tcx.erase_late_bound_regions_and_normalize(sig);
488     let tuple_input_ty = tcx.intern_tup(sig.inputs());
489     let sig = tcx.mk_fn_sig(
490         [bare_fn_ty_maybe_ref, tuple_input_ty].iter().cloned(),
491         sig.output(),
492         false
493     );
494     let fn_ty = FnType::new(ccx, Abi::RustCall, &sig, &[]);
495     let tuple_fn_ty = tcx.mk_fn_ptr(tcx.mk_bare_fn(ty::BareFnTy {
496         unsafety: hir::Unsafety::Normal,
497         abi: Abi::RustCall,
498         sig: ty::Binder(sig)
499     }));
500     debug!("tuple_fn_ty: {:?}", tuple_fn_ty);
501
502     //
503     let function_name = method_instance.symbol_name(ccx.shared());
504     let llfn = declare::define_internal_fn(ccx, &function_name, tuple_fn_ty);
505     attributes::set_frame_pointer_elimination(ccx, llfn);
506     //
507     let fcx = FunctionContext::new(ccx, llfn);
508     let bcx = fcx.get_entry_block();
509
510     let mut llargs = get_params(fcx.llfn);
511
512     let self_arg = llargs.remove(fn_ty.ret.is_indirect() as usize);
513     let llfnpointer = llfnpointer.unwrap_or_else(|| {
514         // the first argument (`self`) will be ptr to the fn pointer
515         if is_by_ref {
516             bcx.load(self_arg)
517         } else {
518             self_arg
519         }
520     });
521
522     let callee = Callee {
523         data: Fn(llfnpointer),
524         ty: bare_fn_ty
525     };
526     let fn_ret = callee.ty.fn_ret();
527     let fn_ty = callee.direct_fn_type(ccx, &[]);
528     let llret = bcx.call(llfnpointer, &llargs, None);
529     fn_ty.apply_attrs_callsite(llret);
530
531     if fn_ret.0.is_never() {
532         bcx.unreachable();
533     } else {
534         if fn_ty.ret.is_indirect() || fn_ty.ret.is_ignore() {
535             bcx.ret_void();
536         } else {
537             bcx.ret(llret);
538         }
539     }
540
541     ccx.fn_pointer_shims().borrow_mut().insert(bare_fn_ty_maybe_ref, llfn);
542
543     llfn
544 }
545
546 /// Translates a reference to a fn/method item, monomorphizing and
547 /// inlining as it goes.
548 ///
549 /// # Parameters
550 ///
551 /// - `ccx`: the crate context
552 /// - `def_id`: def id of the fn or method item being referenced
553 /// - `substs`: values for each of the fn/method's parameters
554 fn get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
555                     def_id: DefId,
556                     substs: &'tcx Substs<'tcx>)
557                     -> (ValueRef, Ty<'tcx>) {
558     let tcx = ccx.tcx();
559
560     debug!("get_fn(def_id={:?}, substs={:?})", def_id, substs);
561
562     assert!(!substs.needs_infer());
563     assert!(!substs.has_escaping_regions());
564     assert!(!substs.has_param_types());
565
566     let substs = tcx.normalize_associated_type(&substs);
567     let instance = Instance::new(def_id, substs);
568     let item_ty = ccx.tcx().item_type(def_id);
569     let fn_ty = monomorphize::apply_param_substs(ccx.shared(), substs, &item_ty);
570
571     if let Some(&llfn) = ccx.instances().borrow().get(&instance) {
572         return (llfn, fn_ty);
573     }
574
575     let sym = ccx.symbol_map().get_or_compute(ccx.shared(),
576                                               TransItem::Fn(instance));
577     debug!("get_fn({:?}: {:?}) => {}", instance, fn_ty, sym);
578
579     // This is subtle and surprising, but sometimes we have to bitcast
580     // the resulting fn pointer.  The reason has to do with external
581     // functions.  If you have two crates that both bind the same C
582     // library, they may not use precisely the same types: for
583     // example, they will probably each declare their own structs,
584     // which are distinct types from LLVM's point of view (nominal
585     // types).
586     //
587     // Now, if those two crates are linked into an application, and
588     // they contain inlined code, you can wind up with a situation
589     // where both of those functions wind up being loaded into this
590     // application simultaneously. In that case, the same function
591     // (from LLVM's point of view) requires two types. But of course
592     // LLVM won't allow one function to have two types.
593     //
594     // What we currently do, therefore, is declare the function with
595     // one of the two types (whichever happens to come first) and then
596     // bitcast as needed when the function is referenced to make sure
597     // it has the type we expect.
598     //
599     // This can occur on either a crate-local or crate-external
600     // reference. It also occurs when testing libcore and in some
601     // other weird situations. Annoying.
602
603     // Create a fn pointer with the substituted signature.
604     let fn_ptr_ty = tcx.mk_fn_ptr(tcx.mk_bare_fn(common::ty_fn_ty(ccx, fn_ty).into_owned()));
605     let llptrty = type_of::type_of(ccx, fn_ptr_ty);
606
607     let llfn = if let Some(llfn) = declare::get_declared_value(ccx, &sym) {
608         if common::val_ty(llfn) != llptrty {
609             debug!("get_fn: casting {:?} to {:?}", llfn, llptrty);
610             consts::ptrcast(llfn, llptrty)
611         } else {
612             debug!("get_fn: not casting pointer!");
613             llfn
614         }
615     } else {
616         let llfn = declare::declare_fn(ccx, &sym, fn_ty);
617         assert_eq!(common::val_ty(llfn), llptrty);
618         debug!("get_fn: not casting pointer!");
619
620         let attrs = ccx.tcx().get_attrs(def_id);
621         attributes::from_fn_attrs(ccx, &attrs, llfn);
622
623         let is_local_def = ccx.shared().translation_items().borrow()
624                               .contains(&TransItem::Fn(instance));
625         if is_local_def {
626             // FIXME(eddyb) Doubt all extern fn should allow unwinding.
627             attributes::unwind(llfn, true);
628             unsafe {
629                 llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage);
630             }
631         }
632         if ccx.use_dll_storage_attrs() && ccx.sess().cstore.is_dllimport_foreign_item(def_id) {
633             unsafe {
634                 llvm::LLVMSetDLLStorageClass(llfn, llvm::DLLStorageClass::DllImport);
635             }
636         }
637         llfn
638     };
639
640     ccx.instances().borrow_mut().insert(instance, llfn);
641
642     (llfn, fn_ty)
643 }