2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
//! 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.
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2016-03-22 12:23:36 -05:00
|
|
|
use attributes;
|
2018-01-04 23:01:54 -06:00
|
|
|
use common::{self, CodegenCx};
|
2016-03-22 12:23:36 -05:00
|
|
|
use consts;
|
|
|
|
use declare;
|
2017-04-14 09:51:22 -05:00
|
|
|
use llvm::{self, ValueRef};
|
2017-09-27 22:23:16 -05:00
|
|
|
use monomorphize::Instance;
|
2017-09-20 15:07:47 -05:00
|
|
|
use type_of::LayoutLlvmExt;
|
|
|
|
|
2017-04-14 09:51:22 -05:00
|
|
|
use rustc::hir::def_id::DefId;
|
2017-09-27 22:23:16 -05:00
|
|
|
use rustc::ty::{self, TypeFoldable};
|
2017-09-20 15:07:47 -05:00
|
|
|
use rustc::ty::layout::LayoutOf;
|
2017-04-14 09:51:22 -05:00
|
|
|
use rustc::ty::subst::Substs;
|
2017-10-04 16:54:28 -05:00
|
|
|
use rustc_back::PanicStrategy;
|
2014-12-01 08:23:40 -06:00
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
/// Translates a reference to a fn/method item, monomorphizing and
|
|
|
|
/// inlining as it goes.
|
|
|
|
///
|
|
|
|
/// # Parameters
|
|
|
|
///
|
2018-01-04 23:04:08 -06:00
|
|
|
/// - `cx`: the crate context
|
2017-03-08 15:19:09 -06:00
|
|
|
/// - `instance`: the instance to be instantiated
|
2018-01-04 23:04:08 -06:00
|
|
|
pub fn get_fn<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
|
2017-03-08 15:19:09 -06:00
|
|
|
instance: Instance<'tcx>)
|
|
|
|
-> ValueRef
|
2017-03-07 17:41:26 -06:00
|
|
|
{
|
2018-01-04 23:04:08 -06:00
|
|
|
let tcx = cx.tcx;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2017-02-08 11:31:03 -06:00
|
|
|
debug!("get_fn(instance={:?})", instance);
|
2012-09-10 14:25:45 -05:00
|
|
|
|
2017-02-08 11:31:03 -06:00
|
|
|
assert!(!instance.substs.needs_infer());
|
|
|
|
assert!(!instance.substs.has_escaping_regions());
|
|
|
|
assert!(!instance.substs.has_param_types());
|
2016-02-23 14:04:51 -06:00
|
|
|
|
2018-01-04 23:04:08 -06:00
|
|
|
let fn_ty = instance.ty(cx.tcx);
|
|
|
|
if let Some(&llfn) = cx.instances.borrow().get(&instance) {
|
2017-03-07 17:41:26 -06:00
|
|
|
return llfn;
|
2016-02-23 14:04:51 -06:00
|
|
|
}
|
|
|
|
|
2017-04-24 11:35:47 -05:00
|
|
|
let sym = tcx.symbol_name(instance);
|
2016-08-17 14:50:55 -05:00
|
|
|
debug!("get_fn({:?}: {:?}) => {}", instance, fn_ty, sym);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2016-11-14 17:23:07 -06:00
|
|
|
// Create a fn pointer with the substituted signature.
|
2018-01-04 23:04:08 -06:00
|
|
|
let fn_ptr_ty = tcx.mk_fn_ptr(common::ty_fn_sig(cx, fn_ty));
|
|
|
|
let llptrty = cx.layout_of(fn_ptr_ty).llvm_type(cx);
|
2016-05-12 11:52:38 -05:00
|
|
|
|
2018-01-04 23:04:08 -06:00
|
|
|
let llfn = if let Some(llfn) = declare::get_declared_value(cx, &sym) {
|
2017-09-13 17:04:51 -05:00
|
|
|
// 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.
|
2016-04-05 05:01:00 -05:00
|
|
|
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
|
|
|
|
}
|
2014-05-29 00:26:56 -05:00
|
|
|
} else {
|
2018-01-04 23:04:08 -06:00
|
|
|
let llfn = declare::declare_fn(cx, &sym, fn_ty);
|
2016-04-05 05:01:00 -05:00
|
|
|
assert_eq!(common::val_ty(llfn), llptrty);
|
2016-02-23 14:04:51 -06:00
|
|
|
debug!("get_fn: not casting pointer!");
|
2016-04-05 05:01:00 -05:00
|
|
|
|
2017-10-30 02:36:03 -05:00
|
|
|
if instance.def.is_inline(tcx) {
|
2017-03-13 18:08:21 -05:00
|
|
|
attributes::inline(llfn, attributes::InlineAttr::Hint);
|
|
|
|
}
|
2018-01-04 23:04:08 -06:00
|
|
|
attributes::from_fn_attrs(cx, llfn, instance.def.def_id());
|
2016-08-17 14:50:55 -05:00
|
|
|
|
2017-07-12 10:37:58 -05:00
|
|
|
let instance_def_id = instance.def_id();
|
|
|
|
|
2017-04-14 09:51:22 -05:00
|
|
|
// 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.
|
2017-10-04 16:54:28 -05:00
|
|
|
if tcx.sess.panic_strategy() == PanicStrategy::Unwind {
|
|
|
|
if !tcx.is_foreign_item(instance_def_id) {
|
|
|
|
attributes::unwind(llfn, true);
|
|
|
|
}
|
2017-07-12 10:37:58 -05:00
|
|
|
}
|
|
|
|
|
2017-09-13 17:24:13 -05:00
|
|
|
// Apply an appropriate linkage/visibility value to our item that we
|
|
|
|
// just declared.
|
|
|
|
//
|
|
|
|
// This is sort of subtle. Inside our codegen unit we started off
|
|
|
|
// compilation by predefining all our own `TransItem` instances. That
|
|
|
|
// is, everything we're translating ourselves is already defined. That
|
|
|
|
// means that anything we're actually translating ourselves will have
|
|
|
|
// hit the above branch in `get_declared_value`. As a result, we're
|
|
|
|
// guaranteed here that we're declaring a symbol that won't get defined,
|
|
|
|
// or in other words we're referencing a foreign value.
|
|
|
|
//
|
|
|
|
// So because this is a foreign value we blanket apply an external
|
|
|
|
// linkage directive because it's coming from a different object file.
|
|
|
|
// The visibility here is where it gets tricky. This symbol could be
|
|
|
|
// referencing some foreign crate or foreign library (an `extern`
|
|
|
|
// block) in which case we want to leave the default visibility. We may
|
|
|
|
// also, though, have multiple codegen units.
|
|
|
|
//
|
|
|
|
// In the situation of multiple codegen units this function may be
|
|
|
|
// referencing a function from another codegen unit. If we're
|
|
|
|
// indeed referencing a symbol in another codegen unit then we're in one
|
|
|
|
// of two cases:
|
|
|
|
//
|
|
|
|
// * This is a symbol defined in a foreign crate and we're just
|
|
|
|
// monomorphizing in another codegen unit. In this case this symbols
|
|
|
|
// is for sure not exported, so both codegen units will be using
|
|
|
|
// hidden visibility. Hence, we apply a hidden visibility here.
|
|
|
|
//
|
|
|
|
// * This is a symbol defined in our local crate. If the symbol in the
|
|
|
|
// other codegen unit is also not exported then like with the foreign
|
|
|
|
// case we apply a hidden visibility. If the symbol is exported from
|
|
|
|
// the foreign object file, however, then we leave this at the
|
|
|
|
// default visibility as we'll just import it naturally.
|
2017-07-12 10:37:58 -05:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage);
|
|
|
|
|
2018-02-19 09:47:59 -06:00
|
|
|
if cx.tcx.is_translated_item(instance_def_id) {
|
2017-09-13 15:22:20 -05:00
|
|
|
if instance_def_id.is_local() {
|
2018-02-22 05:18:16 -06:00
|
|
|
if !cx.tcx.is_reachable_non_generic(instance_def_id) {
|
2017-07-12 10:37:58 -05:00
|
|
|
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
|
|
|
|
}
|
2016-08-17 14:50:55 -05:00
|
|
|
}
|
2016-04-05 05:01:00 -05:00
|
|
|
}
|
2017-04-14 09:51:22 -05:00
|
|
|
|
2018-01-04 23:04:08 -06:00
|
|
|
if cx.use_dll_storage_attrs &&
|
2017-09-28 04:58:45 -05:00
|
|
|
tcx.is_dllimport_foreign_item(instance_def_id)
|
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMSetDLLStorageClass(llfn, llvm::DLLStorageClass::DllImport);
|
2016-11-23 18:09:51 -06:00
|
|
|
}
|
2017-09-28 04:58:45 -05:00
|
|
|
}
|
|
|
|
|
2016-02-23 14:04:51 -06:00
|
|
|
llfn
|
|
|
|
};
|
|
|
|
|
2018-01-04 23:04:08 -06:00
|
|
|
cx.instances.borrow_mut().insert(instance, llfn);
|
2013-05-21 14:25:44 -05:00
|
|
|
|
2017-03-07 17:41:26 -06:00
|
|
|
llfn
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2017-03-08 10:33:21 -06:00
|
|
|
|
2018-01-04 23:04:08 -06:00
|
|
|
pub fn resolve_and_get_fn<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
|
2017-03-08 10:33:21 -06:00
|
|
|
def_id: DefId,
|
|
|
|
substs: &'tcx Substs<'tcx>)
|
|
|
|
-> ValueRef
|
|
|
|
{
|
2017-09-29 20:51:20 -05:00
|
|
|
get_fn(
|
2018-01-04 23:04:08 -06:00
|
|
|
cx,
|
2017-09-29 20:51:20 -05:00
|
|
|
ty::Instance::resolve(
|
2018-01-04 23:04:08 -06:00
|
|
|
cx.tcx,
|
2018-02-10 12:18:02 -06:00
|
|
|
ty::ParamEnv::reveal_all(),
|
2017-09-29 20:51:20 -05:00
|
|
|
def_id,
|
2017-10-01 10:14:47 -05:00
|
|
|
substs
|
2017-09-30 10:08:08 -05:00
|
|
|
).unwrap()
|
2017-09-29 20:51:20 -05:00
|
|
|
)
|
2017-03-08 10:33:21 -06:00
|
|
|
}
|