callee.rs 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

S
Steve Klabnik 已提交
11 12 13 14 15
//! Handles translation of callees as well as other call-related
//! things.  Callees are a superset of normal rust values and sometimes
//! have different representations.  In particular, top-level fn items
//! and methods are represented as just a fn ptr and not a full
//! closure.
16

17
use attributes;
18
use common::{self, CrateContext};
19 20
use consts;
use declare;
21 22 23
use llvm::{self, ValueRef};
use monomorphize::{self, Instance};
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

S
Steve Klabnik 已提交
29 30 31 32 33
/// Translates a reference to a fn/method item, monomorphizing and
/// inlining as it goes.
///
/// # Parameters
///
34
/// - `ccx`: the crate context
35 36 37 38
/// - `instance`: the instance to be instantiated
pub fn get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
                        instance: Instance<'tcx>)
                        -> ValueRef
39
{
40
    let tcx = ccx.tcx();
41

42
    debug!("get_fn(instance={:?})", instance);
43

44 45 46
    assert!(!instance.substs.needs_infer());
    assert!(!instance.substs.has_escaping_regions());
    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 = tcx.symbol_name(instance);
54
    debug!("get_fn({:?}: {:?}) => {}", instance, fn_ty, sym);
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    // This is subtle and surprising, but sometimes we have to bitcast
    // the resulting fn pointer.  The reason has to do with external
    // functions.  If you have two crates that both bind the same C
    // library, they may not use precisely the same types: for
    // example, they will probably each declare their own structs,
    // which are distinct types from LLVM's point of view (nominal
    // types).
    //
    // Now, if those two crates are linked into an application, and
    // they contain inlined code, you can wind up with a situation
    // where both of those functions wind up being loaded into this
    // application simultaneously. In that case, the same function
    // (from LLVM's point of view) requires two types. But of course
    // LLVM won't allow one function to have two types.
    //
    // What we currently do, therefore, is declare the function with
    // one of the two types (whichever happens to come first) and then
    // bitcast as needed when the function is referenced to make sure
    // it has the type we expect.
    //
    // This can occur on either a crate-local or crate-external
    // reference. It also occurs when testing libcore and in some
    // 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 86 87 88 89 90 91
        if common::val_ty(llfn) != llptrty {
            debug!("get_fn: casting {:?} to {:?}", llfn, llptrty);
            consts::ptrcast(llfn, llptrty)
        } else {
            debug!("get_fn: not casting pointer!");
            llfn
        }
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

A
Ariel Ben-Yehuda 已提交
97 98 99
        if common::is_inline_instance(tcx, &instance) {
            attributes::inline(llfn, attributes::InlineAttr::Hint);
        }
100
        let attrs = instance.def.attrs(ccx.tcx());
101
        attributes::from_fn_attrs(ccx, &attrs, llfn);
102

103 104
        let instance_def_id = instance.def_id();

105 106 107 108
        // Perhaps questionable, but we assume that anything defined
        // *in Rust code* may unwind. Foreign items like `extern "C" {
        // fn foo(); }` are assumed not to unwind **unless** they have
        // a `#[unwind]` attribute.
109
        if !tcx.is_foreign_item(instance_def_id) {
110
            attributes::unwind(llfn, true);
111 112 113 114 115 116 117 118 119 120 121 122 123
        }

        unsafe {
            llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage);

            if ccx.crate_trans_items().contains(&TransItem::Fn(instance)) {
                if let Some(node_id) = tcx.hir.as_local_node_id(instance_def_id) {
                    if !ccx.exported_symbols().local_exports().contains(&node_id) {
                        llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
                    }
                } else {
                    llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
                }
124
            }
125
        }
126

127
        if ccx.use_dll_storage_attrs() &&
128
            ccx.sess().cstore.is_dllimport_foreign_item(instance_def_id)
129
        {
130 131 132 133
            unsafe {
                llvm::LLVMSetDLLStorageClass(llfn, llvm::DLLStorageClass::DllImport);
            }
        }
134 135 136 137
        llfn
    };

    ccx.instances().borrow_mut().insert(instance, llfn);
138

139
    llfn
140
}
141 142 143 144 145 146 147 148

pub fn resolve_and_get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
                                    def_id: DefId,
                                    substs: &'tcx Substs<'tcx>)
                                    -> ValueRef
{
    get_fn(ccx, monomorphize::resolve(ccx.shared(), def_id, substs))
}