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-03-31 16:43:19 -05:00
|
|
|
use back::link::exported_name;
|
2013-02-25 13:11:21 -06:00
|
|
|
use driver::session;
|
2014-07-07 19:58:01 -05:00
|
|
|
use llvm::ValueRef;
|
2014-05-13 10:35:42 -05:00
|
|
|
use middle::subst;
|
|
|
|
use middle::subst::Subst;
|
2013-08-05 23:21:37 -05:00
|
|
|
use middle::trans::base::{set_llvm_fn_attrs, set_inline_hint};
|
2014-01-11 08:39:32 -06:00
|
|
|
use middle::trans::base::{trans_enum_variant, push_ctxt, get_item_val};
|
2013-09-10 17:42:01 -05:00
|
|
|
use middle::trans::base::{trans_fn, decl_internal_rust_fn};
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::base;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::common::*;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::ty;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::typeck;
|
2013-09-26 15:54:15 -05:00
|
|
|
use util::ppaux::Repr;
|
2012-12-13 15:05:22 -06:00
|
|
|
|
2014-04-02 03:19:41 -05:00
|
|
|
use syntax::abi;
|
2012-08-28 17:54:45 -05:00
|
|
|
use syntax::ast;
|
2013-02-25 13:11:21 -06:00
|
|
|
use syntax::ast_map;
|
2014-07-14 18:31:52 -05:00
|
|
|
use syntax::ast_util::{local_def, PostExpansionMethod};
|
2014-04-20 11:29:56 -05:00
|
|
|
use std::hash::{sip, Hash};
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-03-06 10:47:24 -06:00
|
|
|
pub fn monomorphic_fn(ccx: &CrateContext,
|
2013-09-01 20:45:37 -05:00
|
|
|
fn_id: ast::DefId,
|
2014-05-13 10:35:42 -05:00
|
|
|
real_substs: &subst::Substs,
|
2014-05-14 19:53:48 -05:00
|
|
|
vtables: typeck::vtable_res,
|
2013-07-27 03:25:59 -05:00
|
|
|
ref_id: Option<ast::NodeId>)
|
2014-02-26 08:06:45 -06:00
|
|
|
-> (ValueRef, bool) {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("monomorphic_fn(\
|
2013-09-28 00:38:08 -05:00
|
|
|
fn_id={}, \
|
|
|
|
real_substs={}, \
|
|
|
|
vtables={}, \
|
|
|
|
ref_id={:?})",
|
2014-03-15 15:29:34 -05:00
|
|
|
fn_id.repr(ccx.tcx()),
|
|
|
|
real_substs.repr(ccx.tcx()),
|
|
|
|
vtables.repr(ccx.tcx()),
|
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-09 00:54:49 -05:00
|
|
|
ref_id);
|
|
|
|
|
2014-05-31 17:53:13 -05:00
|
|
|
assert!(real_substs.types.all(|t| {
|
2014-04-20 11:09:11 -05:00
|
|
|
!ty::type_needs_infer(*t) && !ty::type_has_params(*t)
|
|
|
|
}));
|
|
|
|
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("monomorphic_fn");
|
2013-07-08 19:28:36 -05:00
|
|
|
|
2014-04-20 11:29:56 -05:00
|
|
|
let hash_id = MonoId {
|
2014-04-20 11:09:11 -05:00
|
|
|
def: fn_id,
|
2014-05-31 17:53:13 -05:00
|
|
|
params: real_substs.types.clone()
|
2014-04-20 11:09:11 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
match ccx.monomorphized.borrow().find(&hash_id) {
|
|
|
|
Some(&val) => {
|
|
|
|
debug!("leaving monomorphic fn {}",
|
|
|
|
ty::item_path_str(ccx.tcx(), fn_id));
|
|
|
|
return (val, false);
|
|
|
|
}
|
|
|
|
None => ()
|
|
|
|
}
|
|
|
|
|
2014-04-20 12:04:57 -05:00
|
|
|
let psubsts = param_substs {
|
2014-05-07 06:20:15 -05:00
|
|
|
substs: (*real_substs).clone(),
|
2013-07-01 20:38:41 -05:00
|
|
|
vtables: vtables,
|
|
|
|
};
|
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("monomorphic_fn(\
|
2013-09-28 00:38:08 -05:00
|
|
|
fn_id={}, \
|
|
|
|
psubsts={}, \
|
|
|
|
hash_id={:?})",
|
2014-03-15 15:29:34 -05:00
|
|
|
fn_id.repr(ccx.tcx()),
|
|
|
|
psubsts.repr(ccx.tcx()),
|
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-09 00:54:49 -05:00
|
|
|
hash_id);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-03-15 15:29:34 -05:00
|
|
|
let tpt = ty::lookup_item_type(ccx.tcx(), fn_id);
|
2013-04-12 00:15:30 -05:00
|
|
|
let llitem_ty = tpt.ty;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-02-13 23:07:09 -06:00
|
|
|
let map_node = session::expect(
|
2014-03-05 08:36:01 -06:00
|
|
|
ccx.sess(),
|
2014-02-13 23:07:09 -06:00
|
|
|
ccx.tcx.map.find(fn_id.node),
|
2014-05-07 18:33:43 -05:00
|
|
|
|| {
|
2014-05-27 22:44:58 -05:00
|
|
|
format!("while monomorphizing {:?}, couldn't find it in \
|
|
|
|
the item map (may have attempted to monomorphize \
|
|
|
|
an item defined in a different crate?)",
|
|
|
|
fn_id)
|
2014-05-07 18:33:43 -05:00
|
|
|
});
|
2013-12-27 18:09:29 -06:00
|
|
|
|
2014-02-13 23:07:09 -06:00
|
|
|
match map_node {
|
|
|
|
ast_map::NodeForeignItem(_) => {
|
2014-04-02 03:19:41 -05:00
|
|
|
if ccx.tcx.map.get_foreign_abi(fn_id.node) != abi::RustIntrinsic {
|
2014-02-13 23:07:09 -06:00
|
|
|
// Foreign externs don't have to be monomorphized.
|
|
|
|
return (get_item_val(ccx, fn_id.node), true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2012-10-08 14:39:30 -05:00
|
|
|
|
2014-03-15 15:29:34 -05:00
|
|
|
debug!("monomorphic_fn about to subst into {}", llitem_ty.repr(ccx.tcx()));
|
2014-05-31 17:53:13 -05:00
|
|
|
let mono_ty = llitem_ty.subst(ccx.tcx(), real_substs);
|
2013-09-10 17:42:01 -05:00
|
|
|
|
2013-12-22 15:46:59 -06:00
|
|
|
ccx.stats.n_monos.set(ccx.stats.n_monos.get() + 1);
|
2012-09-12 16:48:13 -05:00
|
|
|
|
2013-12-18 18:38:25 -06:00
|
|
|
let depth;
|
|
|
|
{
|
|
|
|
let mut monomorphizing = ccx.monomorphizing.borrow_mut();
|
2014-03-20 21:49:20 -05:00
|
|
|
depth = match monomorphizing.find(&fn_id) {
|
2013-12-18 18:38:25 -06:00
|
|
|
Some(&d) => d, None => 0
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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 08:36:01 -06:00
|
|
|
if depth > ccx.sess().recursion_limit.get() {
|
|
|
|
ccx.sess().span_fatal(ccx.tcx.map.span(fn_id.node),
|
2014-03-06 12:37:24 -06:00
|
|
|
"reached the recursion limit during monomorphization");
|
2013-12-18 18:38:25 -06:00
|
|
|
}
|
2014-03-06 12:37:24 -06:00
|
|
|
|
2014-03-20 21:49:20 -05:00
|
|
|
monomorphizing.insert(fn_id, depth + 1);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-02-13 23:07:09 -06:00
|
|
|
let s = ccx.tcx.map.with_path(fn_id.node, |path| {
|
2014-04-20 11:29:56 -05:00
|
|
|
let mut state = sip::SipState::new();
|
|
|
|
hash_id.hash(&mut state);
|
|
|
|
mono_ty.hash(&mut state);
|
|
|
|
|
2014-06-06 15:21:18 -05:00
|
|
|
exported_name(path, format!("h{}", state.result()).as_slice())
|
2014-02-13 23:07:09 -06:00
|
|
|
});
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("monomorphize_fn mangled to {}", s);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-04-20 11:29:56 -05:00
|
|
|
// This shouldn't need to option dance.
|
|
|
|
let mut hash_id = Some(hash_id);
|
2014-01-27 06:18:36 -06:00
|
|
|
let mk_lldecl = || {
|
2014-05-21 14:07:48 -05:00
|
|
|
let lldecl = decl_internal_rust_fn(ccx, mono_ty, s.as_slice());
|
2014-04-20 11:29:56 -05:00
|
|
|
ccx.monomorphized.borrow_mut().insert(hash_id.take_unwrap(), lldecl);
|
2012-08-28 17:54:45 -05:00
|
|
|
lldecl
|
|
|
|
};
|
|
|
|
|
2013-05-05 11:17:59 -05:00
|
|
|
let lldecl = match map_node {
|
2014-02-13 23:07:09 -06:00
|
|
|
ast_map::NodeItem(i) => {
|
2014-01-29 17:44:14 -06:00
|
|
|
match *i {
|
|
|
|
ast::Item {
|
2014-05-16 12:15:33 -05:00
|
|
|
node: ast::ItemFn(ref decl, _, _, _, ref body),
|
2014-01-29 17:44:14 -06:00
|
|
|
..
|
|
|
|
} => {
|
2014-01-27 06:18:36 -06:00
|
|
|
let d = mk_lldecl();
|
2014-02-28 17:25:15 -06:00
|
|
|
set_llvm_fn_attrs(i.attrs.as_slice(), d);
|
2014-05-16 12:15:33 -05:00
|
|
|
trans_fn(ccx, &**decl, &**body, d, &psubsts, fn_id.node, []);
|
2014-01-27 06:18:36 -06:00
|
|
|
d
|
2014-01-03 17:08:48 -06:00
|
|
|
}
|
|
|
|
_ => {
|
2014-03-05 08:36:01 -06:00
|
|
|
ccx.sess().bug("Can't monomorphize this kind of item")
|
2014-01-03 17:08:48 -06:00
|
|
|
}
|
2014-01-29 17:44:14 -06:00
|
|
|
}
|
|
|
|
}
|
2014-02-13 23:07:09 -06:00
|
|
|
ast_map::NodeVariant(v) => {
|
|
|
|
let parent = ccx.tcx.map.get_parent(fn_id.node);
|
2014-03-15 15:29:34 -05:00
|
|
|
let tvs = ty::enum_variants(ccx.tcx(), local_def(parent));
|
2014-04-21 18:21:52 -05:00
|
|
|
let this_tv = tvs.iter().find(|tv| { tv.id.node == fn_id.node}).unwrap();
|
2014-01-29 17:44:14 -06:00
|
|
|
let d = mk_lldecl();
|
|
|
|
set_inline_hint(d);
|
|
|
|
match v.node.kind {
|
|
|
|
ast::TupleVariantKind(ref args) => {
|
|
|
|
trans_enum_variant(ccx,
|
2014-02-13 23:07:09 -06:00
|
|
|
parent,
|
2014-05-16 12:15:33 -05:00
|
|
|
&*v,
|
2014-02-28 17:25:15 -06:00
|
|
|
args.as_slice(),
|
2014-01-29 17:44:14 -06:00
|
|
|
this_tv.disr_val,
|
2014-05-14 19:53:48 -05:00
|
|
|
&psubsts,
|
2014-01-29 17:44:14 -06:00
|
|
|
d);
|
|
|
|
}
|
|
|
|
ast::StructVariantKind(_) =>
|
2014-03-05 08:36:01 -06:00
|
|
|
ccx.sess().bug("can't monomorphize struct variants"),
|
2014-01-29 17:44:14 -06:00
|
|
|
}
|
|
|
|
d
|
|
|
|
}
|
2014-02-13 23:07:09 -06:00
|
|
|
ast_map::NodeMethod(mth) => {
|
2014-01-29 17:44:14 -06:00
|
|
|
let d = mk_lldecl();
|
2014-02-28 17:25:15 -06:00
|
|
|
set_llvm_fn_attrs(mth.attrs.as_slice(), d);
|
2014-07-14 18:31:52 -05:00
|
|
|
trans_fn(ccx, &*mth.pe_fn_decl(), &*mth.pe_body(), d, &psubsts, mth.id, []);
|
2014-01-29 17:44:14 -06:00
|
|
|
d
|
|
|
|
}
|
2014-02-13 23:07:09 -06:00
|
|
|
ast_map::NodeTraitMethod(method) => {
|
2014-01-29 17:44:14 -06:00
|
|
|
match *method {
|
|
|
|
ast::Provided(mth) => {
|
|
|
|
let d = mk_lldecl();
|
2014-02-28 17:25:15 -06:00
|
|
|
set_llvm_fn_attrs(mth.attrs.as_slice(), d);
|
2014-07-14 18:31:52 -05:00
|
|
|
trans_fn(ccx, &*mth.pe_fn_decl(), &*mth.pe_body(), d,
|
|
|
|
&psubsts, mth.id, []);
|
2014-01-29 17:44:14 -06:00
|
|
|
d
|
|
|
|
}
|
|
|
|
_ => {
|
2014-03-05 08:36:01 -06:00
|
|
|
ccx.sess().bug(format!("can't monomorphize a {:?}",
|
2014-05-16 12:45:16 -05:00
|
|
|
map_node).as_slice())
|
2014-01-29 17:44:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-13 23:07:09 -06:00
|
|
|
ast_map::NodeStructCtor(struct_def) => {
|
2014-01-29 17:44:14 -06:00
|
|
|
let d = mk_lldecl();
|
|
|
|
set_inline_hint(d);
|
|
|
|
base::trans_tuple_struct(ccx,
|
2014-02-28 17:25:15 -06:00
|
|
|
struct_def.fields.as_slice(),
|
2014-01-29 17:44:14 -06:00
|
|
|
struct_def.ctor_id.expect("ast-mapped tuple struct \
|
|
|
|
didn't have a ctor id"),
|
2014-05-14 19:53:48 -05:00
|
|
|
&psubsts,
|
2014-01-29 17:44:14 -06:00
|
|
|
d);
|
|
|
|
d
|
|
|
|
}
|
2012-10-08 14:39:30 -05:00
|
|
|
|
2014-01-29 17:44:14 -06:00
|
|
|
// Ugh -- but this ensures any new variants won't be forgotten
|
2014-07-09 19:25:52 -05:00
|
|
|
ast_map::NodeForeignItem(..) |
|
2014-04-11 04:28:43 -05:00
|
|
|
ast_map::NodeLifetime(..) |
|
2014-01-29 17:44:14 -06:00
|
|
|
ast_map::NodeExpr(..) |
|
|
|
|
ast_map::NodeStmt(..) |
|
|
|
|
ast_map::NodeArg(..) |
|
|
|
|
ast_map::NodeBlock(..) |
|
2014-05-08 08:50:54 -05:00
|
|
|
ast_map::NodePat(..) |
|
2014-01-29 17:44:14 -06:00
|
|
|
ast_map::NodeLocal(..) => {
|
2014-05-16 12:45:16 -05:00
|
|
|
ccx.sess().bug(format!("can't monomorphize a {:?}",
|
|
|
|
map_node).as_slice())
|
2014-01-29 17:44:14 -06:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
};
|
2013-12-18 18:38:25 -06:00
|
|
|
|
2014-03-20 21:49:20 -05:00
|
|
|
ccx.monomorphizing.borrow_mut().insert(fn_id, depth);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-03-15 15:29:34 -05:00
|
|
|
debug!("leaving monomorphic fn {}", ty::item_path_str(ccx.tcx(), fn_id));
|
2014-04-20 07:43:37 -05:00
|
|
|
(lldecl, false)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-04-20 11:29:56 -05:00
|
|
|
// Used to identify cached monomorphized functions and vtables
|
2014-05-31 12:43:52 -05:00
|
|
|
#[deriving(PartialEq, Eq, Hash)]
|
2014-04-20 11:29:56 -05:00
|
|
|
pub struct MonoParamId {
|
|
|
|
pub subst: ty::t,
|
|
|
|
}
|
|
|
|
|
2014-05-31 12:43:52 -05:00
|
|
|
#[deriving(PartialEq, Eq, Hash)]
|
2014-04-20 11:29:56 -05:00
|
|
|
pub struct MonoId {
|
|
|
|
pub def: ast::DefId,
|
2014-05-31 17:53:13 -05:00
|
|
|
pub params: subst::VecPerParamSpace<ty::t>
|
2014-04-20 11:29:56 -05:00
|
|
|
}
|
|
|
|
|
2014-05-13 10:49:06 -05:00
|
|
|
pub fn make_vtable_id(_ccx: &CrateContext,
|
2014-04-20 11:09:11 -05:00
|
|
|
origin: &typeck::vtable_origin)
|
2014-04-20 11:29:56 -05:00
|
|
|
-> MonoId {
|
2014-04-20 11:09:11 -05:00
|
|
|
match origin {
|
2014-05-13 10:49:06 -05:00
|
|
|
&typeck::vtable_static(impl_id, ref substs, _) => {
|
2014-04-20 11:29:56 -05:00
|
|
|
MonoId {
|
2014-04-20 11:09:11 -05:00
|
|
|
def: impl_id,
|
2014-05-31 17:53:13 -05:00
|
|
|
params: substs.types.clone()
|
2014-04-20 11:09:11 -05:00
|
|
|
}
|
2014-04-20 07:43:37 -05:00
|
|
|
}
|
2013-07-01 20:38:41 -05:00
|
|
|
|
2014-04-20 11:09:11 -05:00
|
|
|
// can't this be checked at the callee?
|
|
|
|
_ => fail!("make_vtable_id needs vtable_static")
|
2014-04-20 07:43:37 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|