2012-12-03 16:48:01 -08: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.
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
//! Handles codegen of callees as well as other call-related
|
2014-11-25 21:17:11 -05:00
|
|
|
//! 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 15:54:45 -07:00
|
|
|
|
2016-03-22 19:23:36 +02:00
|
|
|
use attributes;
|
2018-01-05 07:01:54 +02:00
|
|
|
use common::{self, CodegenCx};
|
2016-03-22 19:23:36 +02:00
|
|
|
use consts;
|
|
|
|
use declare;
|
2018-07-10 13:28:39 +03:00
|
|
|
use llvm;
|
2017-09-27 23:23:16 -04:00
|
|
|
use monomorphize::Instance;
|
2017-09-20 23:07:47 +03:00
|
|
|
use type_of::LayoutLlvmExt;
|
2018-07-10 13:28:39 +03:00
|
|
|
use value::Value;
|
2017-09-20 23:07:47 +03:00
|
|
|
|
2017-04-14 10:51:22 -04:00
|
|
|
use rustc::hir::def_id::DefId;
|
2017-09-27 23:23:16 -04:00
|
|
|
use rustc::ty::{self, TypeFoldable};
|
2017-09-20 23:07:47 +03:00
|
|
|
use rustc::ty::layout::LayoutOf;
|
2017-04-14 10:51:22 -04:00
|
|
|
use rustc::ty::subst::Substs;
|
2014-12-01 09:23:40 -05:00
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
/// Codegens a reference to a fn/method item, monomorphizing and
|
2014-11-25 21:17:11 -05:00
|
|
|
/// inlining as it goes.
|
|
|
|
///
|
|
|
|
/// # Parameters
|
|
|
|
///
|
2018-01-05 07:04:08 +02:00
|
|
|
/// - `cx`: the crate context
|
2017-03-08 23:19:09 +02:00
|
|
|
/// - `instance`: the instance to be instantiated
|
2018-07-10 13:28:39 +03:00
|
|
|
pub fn get_fn(
|
|
|
|
cx: &CodegenCx<'ll, 'tcx>,
|
|
|
|
instance: Instance<'tcx>,
|
|
|
|
) -> &'ll Value {
|
2018-01-05 07:04:08 +02:00
|
|
|
let tcx = cx.tcx;
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2017-02-08 18:31:03 +01:00
|
|
|
debug!("get_fn(instance={:?})", instance);
|
2012-09-10 12:25:45 -07:00
|
|
|
|
2017-02-08 18:31:03 +01:00
|
|
|
assert!(!instance.substs.needs_infer());
|
|
|
|
assert!(!instance.substs.has_escaping_regions());
|
|
|
|
assert!(!instance.substs.has_param_types());
|
2016-02-23 22:04:51 +02:00
|
|
|
|
2018-01-05 07:04:08 +02:00
|
|
|
let fn_ty = instance.ty(cx.tcx);
|
|
|
|
if let Some(&llfn) = cx.instances.borrow().get(&instance) {
|
2017-03-08 01:41:26 +02:00
|
|
|
return llfn;
|
2016-02-23 22:04:51 +02:00
|
|
|
}
|
|
|
|
|
2018-04-11 23:02:41 +02:00
|
|
|
let sym = tcx.symbol_name(instance).as_str();
|
2016-08-17 22:50:55 +03:00
|
|
|
debug!("get_fn({:?}: {:?}) => {}", instance, fn_ty, sym);
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2016-11-14 18:23:07 -05:00
|
|
|
// Create a fn pointer with the substituted signature.
|
2018-01-05 07:04:08 +02: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 19:52:38 +03:00
|
|
|
|
2018-01-05 07:04:08 +02:00
|
|
|
let llfn = if let Some(llfn) = declare::get_declared_value(cx, &sym) {
|
2017-09-13 15:04:51 -07: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 13:01:00 +03: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-28 22:26:56 -07:00
|
|
|
} else {
|
2018-01-05 07:04:08 +02:00
|
|
|
let llfn = declare::declare_fn(cx, &sym, fn_ty);
|
2016-04-05 13:01:00 +03:00
|
|
|
assert_eq!(common::val_ty(llfn), llptrty);
|
2016-02-23 22:04:51 +02:00
|
|
|
debug!("get_fn: not casting pointer!");
|
2016-04-05 13:01:00 +03:00
|
|
|
|
2017-10-30 08:36:03 +01:00
|
|
|
if instance.def.is_inline(tcx) {
|
2018-07-18 22:04:27 -05:00
|
|
|
attributes::inline(cx, llfn, attributes::InlineAttr::Hint);
|
2017-03-14 01:08:21 +02:00
|
|
|
}
|
2018-08-16 13:19:04 -07:00
|
|
|
attributes::from_fn_attrs(cx, llfn, Some(instance.def.def_id()));
|
2016-08-17 22:50:55 +03:00
|
|
|
|
2017-07-12 17:37:58 +02:00
|
|
|
let instance_def_id = instance.def_id();
|
|
|
|
|
2017-09-13 15:24:13 -07: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
|
2018-05-08 16:10:16 +03:00
|
|
|
// compilation by predefining all our own `MonoItem` instances. That
|
|
|
|
// is, everything we're codegenning ourselves is already defined. That
|
|
|
|
// means that anything we're actually codegenning in this codegen unit
|
2018-03-14 16:45:06 +01:00
|
|
|
// 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 value from another
|
|
|
|
// codegen unit or even another crate.
|
2017-09-13 15:24:13 -07:00
|
|
|
//
|
|
|
|
// 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
|
2018-03-14 16:45:06 +01:00
|
|
|
// also, though, have multiple codegen units. It could be a
|
|
|
|
// monomorphization, in which case its expected visibility depends on
|
|
|
|
// whether we are sharing generics or not. The important thing here is
|
|
|
|
// that the visibility we apply to the declaration is the same one that
|
|
|
|
// has been applied to the definition (wherever that definition may be).
|
2017-07-12 17:37:58 +02:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage);
|
|
|
|
|
2018-03-06 14:44:14 +01:00
|
|
|
let is_generic = instance.substs.types().next().is_some();
|
|
|
|
|
|
|
|
if is_generic {
|
2018-03-14 16:45:06 +01:00
|
|
|
// This is a monomorphization. Its expected visibility depends
|
|
|
|
// on whether we are in share-generics mode.
|
|
|
|
|
2018-07-26 13:20:47 -06:00
|
|
|
if cx.tcx.sess.opts.share_generics() {
|
2018-03-06 14:44:14 +01:00
|
|
|
// We are in share_generics mode.
|
|
|
|
|
|
|
|
if instance_def_id.is_local() {
|
|
|
|
// This is a definition from the current crate. If the
|
|
|
|
// definition is unreachable for downstream crates or
|
|
|
|
// the current crate does not re-export generics, the
|
2018-03-14 16:45:06 +01:00
|
|
|
// definition of the instance will have been declared
|
|
|
|
// as `hidden`.
|
2018-03-06 14:44:14 +01:00
|
|
|
if cx.tcx.is_unreachable_local_definition(instance_def_id) ||
|
|
|
|
!cx.tcx.local_crate_exports_generics() {
|
|
|
|
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
|
|
|
|
}
|
|
|
|
} else {
|
2018-03-14 16:45:06 +01:00
|
|
|
// This is a monomorphization of a generic function
|
|
|
|
// defined in an upstream crate.
|
2018-03-06 14:44:14 +01:00
|
|
|
if cx.tcx.upstream_monomorphizations_for(instance_def_id)
|
|
|
|
.map(|set| set.contains_key(instance.substs))
|
|
|
|
.unwrap_or(false) {
|
2018-03-14 16:45:06 +01:00
|
|
|
// This is instantiated in another crate. It cannot
|
|
|
|
// be `hidden`.
|
2018-03-06 14:44:14 +01:00
|
|
|
} else {
|
|
|
|
// This is a local instantiation of an upstream definition.
|
2018-03-14 16:45:06 +01:00
|
|
|
// If the current crate does not re-export it
|
|
|
|
// (because it is a C library or an executable), it
|
|
|
|
// will have been declared `hidden`.
|
2018-03-06 14:44:14 +01:00
|
|
|
if !cx.tcx.local_crate_exports_generics() {
|
|
|
|
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// When not sharing generics, all instances are in the same
|
|
|
|
// crate and have hidden visibility
|
|
|
|
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
|
|
|
|
}
|
2018-03-06 10:33:42 +01:00
|
|
|
} else {
|
2018-03-06 14:44:14 +01:00
|
|
|
// This is a non-generic function
|
2018-05-08 16:10:16 +03:00
|
|
|
if cx.tcx.is_codegened_item(instance_def_id) {
|
2018-03-06 14:44:14 +01:00
|
|
|
// This is a function that is instantiated in the local crate
|
|
|
|
|
2018-03-06 10:33:42 +01:00
|
|
|
if instance_def_id.is_local() {
|
2018-03-06 14:44:14 +01:00
|
|
|
// This is function that is defined in the local crate.
|
|
|
|
// If it is not reachable, it is hidden.
|
2018-03-06 10:33:42 +01:00
|
|
|
if !cx.tcx.is_reachable_non_generic(instance_def_id) {
|
|
|
|
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
|
|
|
|
}
|
|
|
|
} else {
|
2018-03-06 14:44:14 +01:00
|
|
|
// This is a function from an upstream crate that has
|
|
|
|
// been instantiated here. These are always hidden.
|
2017-07-12 17:37:58 +02:00
|
|
|
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
|
|
|
|
}
|
|
|
|
}
|
2016-08-17 22:50:55 +03:00
|
|
|
}
|
2016-04-05 13:01:00 +03:00
|
|
|
}
|
2017-04-14 10:51:22 -04:00
|
|
|
|
2018-01-05 07:04:08 +02:00
|
|
|
if cx.use_dll_storage_attrs &&
|
2017-09-28 11:58:45 +02:00
|
|
|
tcx.is_dllimport_foreign_item(instance_def_id)
|
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMSetDLLStorageClass(llfn, llvm::DLLStorageClass::DllImport);
|
2016-11-23 16:09:51 -08:00
|
|
|
}
|
2017-09-28 11:58:45 +02:00
|
|
|
}
|
|
|
|
|
2016-02-23 22:04:51 +02:00
|
|
|
llfn
|
|
|
|
};
|
|
|
|
|
2018-01-05 07:04:08 +02:00
|
|
|
cx.instances.borrow_mut().insert(instance, llfn);
|
2013-05-21 15:25:44 -04:00
|
|
|
|
2017-03-08 01:41:26 +02:00
|
|
|
llfn
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
2017-03-08 18:33:21 +02:00
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
pub fn resolve_and_get_fn(
|
|
|
|
cx: &CodegenCx<'ll, 'tcx>,
|
|
|
|
def_id: DefId,
|
|
|
|
substs: &'tcx Substs<'tcx>,
|
|
|
|
) -> &'ll Value {
|
2017-09-29 21:51:20 -04:00
|
|
|
get_fn(
|
2018-01-05 07:04:08 +02:00
|
|
|
cx,
|
2017-09-29 21:51:20 -04:00
|
|
|
ty::Instance::resolve(
|
2018-01-05 07:04:08 +02:00
|
|
|
cx.tcx,
|
2018-02-10 13:18:02 -05:00
|
|
|
ty::ParamEnv::reveal_all(),
|
2017-09-29 21:51:20 -04:00
|
|
|
def_id,
|
2017-10-01 11:14:47 -04:00
|
|
|
substs
|
2017-09-30 11:08:08 -04:00
|
|
|
).unwrap()
|
2017-09-29 21:51:20 -04:00
|
|
|
)
|
2017-03-08 18:33:21 +02:00
|
|
|
}
|