2014-08-01 22:25:41 -06:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08:00
|
|
|
// 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.
|
|
|
|
|
2016-02-14 13:01:44 -05:00
|
|
|
use back::symbol_names;
|
2014-07-07 17:58:01 -07:00
|
|
|
use llvm::ValueRef;
|
2014-08-01 12:27:12 -07:00
|
|
|
use llvm;
|
2015-08-16 06:32:28 -04:00
|
|
|
use middle::def_id::DefId;
|
2015-09-15 00:47:14 +03:00
|
|
|
use middle::infer::normalize_associated_type;
|
2016-03-22 16:21:46 +02:00
|
|
|
use middle::ty::subst;
|
|
|
|
use middle::ty::subst::{Subst, Substs};
|
2016-02-14 13:01:44 -05:00
|
|
|
use middle::ty::{self, Ty, TyCtxt};
|
2015-09-06 18:32:34 +03:00
|
|
|
use middle::ty::fold::{TypeFolder, TypeFoldable};
|
2015-02-28 23:53:12 +02:00
|
|
|
use trans::attributes;
|
2016-02-23 22:04:51 +02:00
|
|
|
use trans::base::{push_ctxt};
|
2015-03-04 01:08:06 +02:00
|
|
|
use trans::base::trans_fn;
|
2014-11-15 20:30:33 -05:00
|
|
|
use trans::base;
|
|
|
|
use trans::common::*;
|
2015-03-04 01:08:06 +02:00
|
|
|
use trans::declare;
|
2016-01-16 16:03:09 +01:00
|
|
|
use trans::Disr;
|
2015-07-31 00:04:06 -07:00
|
|
|
use rustc::front::map as hir_map;
|
2016-02-23 22:04:27 +02:00
|
|
|
use rustc::util::ppaux;
|
2015-07-31 00:04:06 -07:00
|
|
|
|
|
|
|
use rustc_front::hir;
|
2012-12-13 13:05:22 -08:00
|
|
|
|
2015-09-14 21:58:20 +12:00
|
|
|
use syntax::attr;
|
2015-12-14 18:15:39 +13:00
|
|
|
use syntax::errors;
|
2016-02-23 22:04:27 +02:00
|
|
|
|
|
|
|
use std::fmt;
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
2015-08-16 06:32:28 -04:00
|
|
|
fn_id: DefId,
|
2016-02-17 09:11:46 +02:00
|
|
|
psubsts: &'tcx subst::Substs<'tcx>)
|
2016-02-23 22:04:51 +02:00
|
|
|
-> (ValueRef, Ty<'tcx>) {
|
2016-02-17 09:11:46 +02:00
|
|
|
debug!("monomorphic_fn(fn_id={:?}, real_substs={:?})", fn_id, psubsts);
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-08 22:54:49 -07:00
|
|
|
|
2015-06-24 02:54:32 +03:00
|
|
|
assert!(!psubsts.types.needs_infer() && !psubsts.types.has_param_types());
|
2014-04-20 19:09:11 +03:00
|
|
|
|
2013-06-17 16:23:24 +12:00
|
|
|
let _icx = push_ctxt("monomorphic_fn");
|
2013-07-08 17:28:36 -07:00
|
|
|
|
2016-03-09 18:22:05 -05:00
|
|
|
let instance = Instance::new(fn_id, psubsts);
|
2014-04-20 19:09:11 +03:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
let item_ty = ccx.tcx().lookup_item_type(fn_id).ty;
|
2015-06-03 11:31:23 +12:00
|
|
|
|
2015-06-18 20:25:05 +03:00
|
|
|
debug!("monomorphic_fn about to subst into {:?}", item_ty);
|
2015-07-03 05:22:54 +03:00
|
|
|
let mono_ty = apply_param_substs(ccx.tcx(), psubsts, &item_ty);
|
|
|
|
debug!("mono_ty = {:?} (post-substitution)", mono_ty);
|
2015-01-04 18:47:58 +02:00
|
|
|
|
2016-02-23 22:04:27 +02:00
|
|
|
match ccx.instances().borrow().get(&instance) {
|
2014-04-20 19:09:11 +03:00
|
|
|
Some(&val) => {
|
2016-02-23 22:04:27 +02:00
|
|
|
debug!("leaving monomorphic fn {:?}", instance);
|
|
|
|
return (val, mono_ty);
|
2014-04-20 19:09:11 +03:00
|
|
|
}
|
|
|
|
None => ()
|
|
|
|
}
|
|
|
|
|
2016-02-23 22:04:27 +02:00
|
|
|
debug!("monomorphic_fn({:?})", instance);
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2014-09-05 09:18:53 -07:00
|
|
|
ccx.stats().n_monos.set(ccx.stats().n_monos.get() + 1);
|
2012-09-12 14:48:13 -07:00
|
|
|
|
2013-12-18 16:38:25 -08:00
|
|
|
let depth;
|
|
|
|
{
|
2014-09-05 09:18:53 -07:00
|
|
|
let mut monomorphizing = ccx.monomorphizing().borrow_mut();
|
2014-11-06 12:25:16 -05:00
|
|
|
depth = match monomorphizing.get(&fn_id) {
|
2013-12-18 16:38:25 -08:00
|
|
|
Some(&d) => d, None => 0
|
|
|
|
};
|
|
|
|
|
2015-09-17 14:29:59 -04:00
|
|
|
debug!("monomorphic_fn: depth for fn_id={:?} is {:?}", fn_id, depth+1);
|
|
|
|
|
2013-12-18 16:38:25 -08:00
|
|
|
// Random cut-off -- code that needs to instantiate the same function
|
|
|
|
// recursively more than thirty times can probably safely be assumed
|
|
|
|
// to be causing an infinite expansion.
|
2014-03-05 16:36:01 +02:00
|
|
|
if depth > ccx.sess().recursion_limit.get() {
|
2016-02-23 22:04:27 +02:00
|
|
|
let error = format!("reached the recursion limit while instantiating `{}`",
|
|
|
|
instance);
|
|
|
|
if let Some(id) = ccx.tcx().map.as_local_node_id(fn_id) {
|
|
|
|
ccx.sess().span_fatal(ccx.tcx().map.span(id), &error);
|
|
|
|
} else {
|
|
|
|
ccx.sess().fatal(&error);
|
|
|
|
}
|
2013-12-18 16:38:25 -08:00
|
|
|
}
|
2014-03-06 20:37:24 +02:00
|
|
|
|
2014-03-20 19:49:20 -07:00
|
|
|
monomorphizing.insert(fn_id, depth + 1);
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
|
2016-02-14 13:01:44 -05:00
|
|
|
let symbol = symbol_names::exported_name(ccx, &instance);
|
2014-08-01 22:25:41 -06:00
|
|
|
|
2016-02-14 13:01:44 -05:00
|
|
|
debug!("monomorphize_fn mangled to {}", symbol);
|
|
|
|
assert!(declare::get_defined_value(ccx, &symbol).is_none());
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2016-02-23 22:04:51 +02:00
|
|
|
// FIXME(nagisa): perhaps needs a more fine grained selection?
|
2016-02-14 13:01:44 -05:00
|
|
|
let lldecl = declare::define_internal_fn(ccx, &symbol, mono_ty);
|
2016-02-23 22:04:51 +02:00
|
|
|
// FIXME(eddyb) Doubt all extern fn should allow unwinding.
|
|
|
|
attributes::unwind(lldecl, true);
|
2014-08-01 22:25:41 -06:00
|
|
|
|
2016-02-23 22:04:51 +02:00
|
|
|
ccx.instances().borrow_mut().insert(instance, lldecl);
|
2014-08-01 12:27:12 -07:00
|
|
|
|
2016-02-23 22:04:51 +02:00
|
|
|
// we can only monomorphize things in this crate (or inlined into it)
|
|
|
|
let fn_node_id = ccx.tcx().map.as_local_node_id(fn_id).unwrap();
|
|
|
|
let map_node = errors::expect(
|
|
|
|
ccx.sess().diagnostic(),
|
|
|
|
ccx.tcx().map.find(fn_node_id),
|
|
|
|
|| {
|
|
|
|
format!("while instantiating `{}`, couldn't find it in \
|
|
|
|
the item map (may have attempted to monomorphize \
|
|
|
|
an item defined in a different crate?)",
|
|
|
|
instance)
|
|
|
|
});
|
|
|
|
match map_node {
|
|
|
|
hir_map::NodeItem(&hir::Item {
|
2016-03-06 16:30:21 +02:00
|
|
|
ref attrs, node: hir::ItemFn(ref decl, _, _, _, _, ref body), ..
|
2016-02-23 22:04:51 +02:00
|
|
|
}) |
|
|
|
|
hir_map::NodeTraitItem(&hir::TraitItem {
|
|
|
|
ref attrs, node: hir::MethodTraitItem(
|
2016-03-06 16:30:21 +02:00
|
|
|
hir::MethodSig { ref decl, .. }, Some(ref body)), ..
|
2016-02-23 22:04:51 +02:00
|
|
|
}) |
|
|
|
|
hir_map::NodeImplItem(&hir::ImplItem {
|
|
|
|
ref attrs, node: hir::ImplItemKind::Method(
|
2016-03-06 16:30:21 +02:00
|
|
|
hir::MethodSig { ref decl, .. }, ref body), ..
|
2016-02-23 22:04:51 +02:00
|
|
|
}) => {
|
|
|
|
base::update_linkage(ccx, lldecl, None, base::OriginalTranslation);
|
|
|
|
attributes::from_fn_attrs(ccx, attrs, lldecl);
|
|
|
|
|
2016-02-14 13:01:44 -05:00
|
|
|
let is_first = !ccx.available_monomorphizations().borrow()
|
|
|
|
.contains(&symbol);
|
2016-02-23 22:04:51 +02:00
|
|
|
if is_first {
|
2016-02-14 13:01:44 -05:00
|
|
|
ccx.available_monomorphizations().borrow_mut().insert(symbol.clone());
|
2016-02-23 22:04:51 +02:00
|
|
|
}
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2016-02-23 22:04:51 +02:00
|
|
|
let trans_everywhere = attr::requests_inline(attrs);
|
|
|
|
if trans_everywhere && !is_first {
|
|
|
|
llvm::SetLinkage(lldecl, llvm::AvailableExternallyLinkage);
|
2014-01-29 18:44:14 -05:00
|
|
|
}
|
2016-02-23 22:04:51 +02:00
|
|
|
|
|
|
|
if trans_everywhere || is_first {
|
2016-03-08 14:38:13 +02:00
|
|
|
trans_fn(ccx, decl, body, lldecl, psubsts, fn_node_id);
|
2014-08-04 13:56:56 -07:00
|
|
|
}
|
2014-01-29 18:44:14 -05:00
|
|
|
}
|
2016-02-23 22:04:51 +02:00
|
|
|
|
|
|
|
hir_map::NodeVariant(_) | hir_map::NodeStructCtor(_) => {
|
|
|
|
let disr = match map_node {
|
|
|
|
hir_map::NodeVariant(_) => {
|
|
|
|
Disr::from(inlined_variant_def(ccx, fn_node_id).disr_val)
|
2014-01-29 18:44:14 -05:00
|
|
|
}
|
2016-02-23 22:04:51 +02:00
|
|
|
hir_map::NodeStructCtor(_) => Disr(0),
|
|
|
|
_ => unreachable!()
|
|
|
|
};
|
|
|
|
attributes::inline(lldecl, attributes::InlineAttr::Hint);
|
|
|
|
base::trans_ctor_shim(ccx, fn_node_id, disr, psubsts, lldecl);
|
2014-01-29 18:44:14 -05:00
|
|
|
}
|
2012-10-08 12:39:30 -07:00
|
|
|
|
2016-02-23 22:04:51 +02:00
|
|
|
_ => unreachable!("can't monomorphize a {:?}", map_node)
|
2012-08-28 15:54:45 -07:00
|
|
|
};
|
2013-12-18 16:38:25 -08:00
|
|
|
|
2014-09-05 09:18:53 -07:00
|
|
|
ccx.monomorphizing().borrow_mut().insert(fn_id, depth);
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2015-06-25 23:42:17 +03:00
|
|
|
debug!("leaving monomorphic fn {}", ccx.tcx().item_path_str(fn_id));
|
2016-02-23 22:04:51 +02:00
|
|
|
(lldecl, mono_ty)
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
|
2016-02-23 22:04:27 +02:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
|
|
|
pub struct Instance<'tcx> {
|
2015-08-16 06:32:28 -04:00
|
|
|
pub def: DefId,
|
2016-03-09 18:22:05 -05:00
|
|
|
pub substs: &'tcx Substs<'tcx>,
|
2014-04-20 19:29:56 +03:00
|
|
|
}
|
2014-12-17 14:16:28 -05:00
|
|
|
|
2016-02-23 22:04:27 +02:00
|
|
|
impl<'tcx> fmt::Display for Instance<'tcx> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2016-03-09 18:22:05 -05:00
|
|
|
ppaux::parameterized(f, &self.substs, self.def, ppaux::Ns::Value, &[],
|
2016-02-23 22:04:27 +02:00
|
|
|
|tcx| tcx.lookup_item_type(self.def).generics)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Instance<'tcx> {
|
2016-03-09 18:22:05 -05:00
|
|
|
pub fn new(def_id: DefId, substs: &'tcx Substs<'tcx>)
|
|
|
|
-> Instance<'tcx> {
|
|
|
|
assert!(substs.regions.iter().all(|&r| r == ty::ReStatic));
|
|
|
|
Instance { def: def_id, substs: substs }
|
|
|
|
}
|
2016-02-23 22:04:27 +02:00
|
|
|
pub fn mono(tcx: &TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> {
|
2016-03-09 18:22:05 -05:00
|
|
|
Instance::new(def_id, &tcx.mk_substs(Substs::empty()))
|
2016-02-23 22:04:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-17 14:16:28 -05:00
|
|
|
/// Monomorphizes a type from the AST by first applying the in-scope
|
|
|
|
/// substitutions and then normalizing any associated types.
|
2016-02-29 23:36:51 +00:00
|
|
|
pub fn apply_param_substs<'tcx,T>(tcx: &TyCtxt<'tcx>,
|
2014-12-17 14:16:28 -05:00
|
|
|
param_substs: &Substs<'tcx>,
|
|
|
|
value: &T)
|
|
|
|
-> T
|
2015-11-18 09:38:57 +00:00
|
|
|
where T : TypeFoldable<'tcx>
|
2014-12-17 14:16:28 -05:00
|
|
|
{
|
|
|
|
let substituted = value.subst(tcx, param_substs);
|
|
|
|
normalize_associated_type(tcx, &substituted)
|
|
|
|
}
|
|
|
|
|
2015-08-02 22:52:50 +03:00
|
|
|
|
|
|
|
/// Returns the normalized type of a struct field
|
2016-02-29 23:36:51 +00:00
|
|
|
pub fn field_ty<'tcx>(tcx: &TyCtxt<'tcx>,
|
2015-08-02 22:52:50 +03:00
|
|
|
param_substs: &Substs<'tcx>,
|
2015-08-07 14:41:33 +03:00
|
|
|
f: ty::FieldDef<'tcx>)
|
2015-08-02 22:52:50 +03:00
|
|
|
-> Ty<'tcx>
|
|
|
|
{
|
|
|
|
normalize_associated_type(tcx, &f.ty(tcx, param_substs))
|
|
|
|
}
|