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.
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2012-12-13 15:05:22 -06:00
|
|
|
use back::link::mangle_exported_name;
|
2013-02-25 13:11:21 -06:00
|
|
|
use driver::session;
|
|
|
|
use lib::llvm::ValueRef;
|
2013-08-05 23:21:37 -05:00
|
|
|
use middle::trans::base::{set_llvm_fn_attrs, set_inline_hint};
|
2013-06-16 23:23:24 -05:00
|
|
|
use middle::trans::base::{trans_enum_variant,push_ctxt};
|
2013-03-26 15:38:07 -05:00
|
|
|
use middle::trans::base::{trans_fn, decl_internal_cdecl_fn};
|
|
|
|
use middle::trans::base::{get_item_val, no_self};
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::base;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::common::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::datum;
|
|
|
|
use middle::trans::machine;
|
|
|
|
use middle::trans::meth;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::type_of::type_of_fn_from_ty;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::type_of;
|
|
|
|
use middle::trans::type_use;
|
2013-05-21 14:25:44 -05:00
|
|
|
use middle::trans::intrinsic;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::ty;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::typeck;
|
2013-06-04 00:34:51 -05:00
|
|
|
use util::ppaux::{Repr,ty_to_str};
|
2012-12-13 15:05:22 -06:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
use syntax::ast;
|
2013-02-25 13:11:21 -06:00
|
|
|
use syntax::ast_map;
|
2013-03-26 15:38:07 -05:00
|
|
|
use syntax::ast_map::path_name;
|
2012-12-13 15:05:22 -06:00
|
|
|
use syntax::ast_util::local_def;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-06-13 02:19:50 -05:00
|
|
|
pub fn monomorphic_fn(ccx: @mut CrateContext,
|
2013-01-29 19:57:02 -06:00
|
|
|
fn_id: ast::def_id,
|
2013-06-12 13:05:03 -05:00
|
|
|
real_substs: &ty::substs,
|
2013-01-29 19:57:02 -06:00
|
|
|
vtables: Option<typeck::vtable_res>,
|
2013-07-22 18:40:31 -05:00
|
|
|
self_vtables: Option<typeck::vtable_param_res>,
|
2013-07-27 03:25:59 -05:00
|
|
|
ref_id: Option<ast::NodeId>)
|
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
|
|
|
-> (ValueRef, bool)
|
|
|
|
{
|
|
|
|
debug!("monomorphic_fn(\
|
|
|
|
fn_id=%s, \
|
|
|
|
real_substs=%s, \
|
|
|
|
vtables=%s, \
|
2013-07-01 20:38:41 -05:00
|
|
|
self_vtable=%s, \
|
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=%?)",
|
|
|
|
fn_id.repr(ccx.tcx),
|
|
|
|
real_substs.repr(ccx.tcx),
|
|
|
|
vtables.repr(ccx.tcx),
|
2013-07-22 18:40:31 -05:00
|
|
|
self_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);
|
|
|
|
|
2013-06-17 18:43:22 -05:00
|
|
|
assert!(real_substs.tps.iter().all(|t| !ty::type_needs_infer(*t)));
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("monomorphic_fn");
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut must_cast = false;
|
2013-07-08 19:28:36 -05:00
|
|
|
|
2013-07-01 20:38:41 -05:00
|
|
|
let psubsts = @param_substs {
|
2013-08-23 17:18:11 -05:00
|
|
|
tys: real_substs.tps.to_owned(),
|
2013-07-01 20:38:41 -05:00
|
|
|
vtables: vtables,
|
2013-08-23 17:18:11 -05:00
|
|
|
self_ty: real_substs.self_ty.clone(),
|
2013-07-22 18:40:31 -05:00
|
|
|
self_vtables: self_vtables
|
2013-07-01 20:38:41 -05:00
|
|
|
};
|
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for s in real_substs.tps.iter() { assert!(!ty::type_has_params(*s)); }
|
|
|
|
for s in psubsts.tys.iter() { assert!(!ty::type_has_params(*s)); }
|
2013-07-08 19:28:36 -05:00
|
|
|
let param_uses = type_use::type_uses_for(ccx, fn_id, psubsts.tys.len());
|
|
|
|
|
|
|
|
|
2013-07-15 19:26:56 -05:00
|
|
|
let hash_id = make_mono_id(ccx, fn_id, &*psubsts, Some(param_uses));
|
2013-07-04 21:13:26 -05:00
|
|
|
if hash_id.params.iter().any(
|
2012-09-28 00:20:47 -05:00
|
|
|
|p| match *p { mono_precise(_, _) => false, _ => true }) {
|
2012-08-28 17:54:45 -05:00
|
|
|
must_cast = true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
debug!("monomorphic_fn(\
|
|
|
|
fn_id=%s, \
|
2013-07-01 20:38:41 -05:00
|
|
|
psubsts=%s, \
|
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=%?)",
|
|
|
|
fn_id.repr(ccx.tcx),
|
2013-07-01 20:38:41 -05:00
|
|
|
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
|
|
|
|
2013-02-05 21:41:45 -06:00
|
|
|
match ccx.monomorphized.find(&hash_id) {
|
2013-03-22 21:26:41 -05:00
|
|
|
Some(&val) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
debug!("leaving monomorphic fn %s",
|
|
|
|
ty::item_path_str(ccx.tcx, fn_id));
|
2013-02-19 01:40:42 -06:00
|
|
|
return (val, must_cast);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
None => ()
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-07-08 19:30:36 -05:00
|
|
|
// We need to do special handling of the substitutions if we are
|
|
|
|
// calling a static provided method. This is sort of unfortunate.
|
|
|
|
let mut is_static_provided = None;
|
|
|
|
|
2013-05-05 11:17:59 -05:00
|
|
|
let map_node = session::expect(
|
|
|
|
ccx.sess,
|
|
|
|
ccx.tcx.items.find_copy(&fn_id.node),
|
|
|
|
|| fmt!("While monomorphizing %?, couldn't find it in the item map \
|
|
|
|
(may have attempted to monomorphize an item \
|
|
|
|
defined in a different crate?)", fn_id));
|
2012-08-28 17:54:45 -05:00
|
|
|
// Get the path so that we can create a symbol
|
2013-05-05 11:17:59 -05:00
|
|
|
let (pt, name, span) = match map_node {
|
2012-08-28 17:54:45 -05:00
|
|
|
ast_map::node_item(i, pt) => (pt, i.ident, i.span),
|
2012-12-04 12:50:00 -06:00
|
|
|
ast_map::node_variant(ref v, enm, pt) => (pt, (*v).node.name, enm.span),
|
2012-08-28 17:54:45 -05:00
|
|
|
ast_map::node_method(m, _, pt) => (pt, m.ident, m.span),
|
2013-03-13 21:25:28 -05:00
|
|
|
ast_map::node_foreign_item(i, abis, _, pt) if abis.is_intrinsic()
|
2012-08-28 17:54:45 -05:00
|
|
|
=> (pt, i.ident, i.span),
|
|
|
|
ast_map::node_foreign_item(*) => {
|
|
|
|
// Foreign externs don't have to be monomorphized.
|
2013-02-19 01:40:42 -06:00
|
|
|
return (get_item_val(ccx, fn_id.node), true);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2012-10-08 14:39:30 -05:00
|
|
|
ast_map::node_trait_method(@ast::provided(m), _, pt) => {
|
2013-07-08 19:30:36 -05:00
|
|
|
// If this is a static provided method, indicate that
|
|
|
|
// and stash the number of params on the method.
|
|
|
|
if m.explicit_self.node == ast::sty_static {
|
|
|
|
is_static_provided = Some(m.generics.ty_params.len());
|
|
|
|
}
|
|
|
|
|
2012-10-08 14:39:30 -05:00
|
|
|
(pt, m.ident, m.span)
|
|
|
|
}
|
|
|
|
ast_map::node_trait_method(@ast::required(_), _, _) => {
|
2013-05-19 00:07:44 -05:00
|
|
|
ccx.tcx.sess.bug("Can't monomorphize a required trait method")
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast_map::node_expr(*) => {
|
2013-05-19 00:07:44 -05:00
|
|
|
ccx.tcx.sess.bug("Can't monomorphize an expr")
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast_map::node_stmt(*) => {
|
2013-05-19 00:07:44 -05:00
|
|
|
ccx.tcx.sess.bug("Can't monomorphize a stmt")
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-05-19 00:07:44 -05:00
|
|
|
ast_map::node_arg(*) => ccx.tcx.sess.bug("Can't monomorphize an arg"),
|
2012-08-28 17:54:45 -05:00
|
|
|
ast_map::node_block(*) => {
|
2013-05-19 00:07:44 -05:00
|
|
|
ccx.tcx.sess.bug("Can't monomorphize a block")
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast_map::node_local(*) => {
|
2013-05-19 00:07:44 -05:00
|
|
|
ccx.tcx.sess.bug("Can't monomorphize a local")
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
ast_map::node_callee_scope(*) => {
|
2013-05-19 00:07:44 -05:00
|
|
|
ccx.tcx.sess.bug("Can't monomorphize a callee-scope")
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
2012-10-24 16:36:00 -05:00
|
|
|
ast_map::node_struct_ctor(_, i, pt) => (pt, i.ident, i.span)
|
2012-08-28 17:54:45 -05:00
|
|
|
};
|
2012-10-08 14:39:30 -05:00
|
|
|
|
2013-07-08 19:30:36 -05:00
|
|
|
debug!("monomorphic_fn about to subst into %s", llitem_ty.repr(ccx.tcx));
|
|
|
|
let mono_ty = match is_static_provided {
|
|
|
|
None => ty::subst_tps(ccx.tcx, psubsts.tys,
|
|
|
|
psubsts.self_ty, llitem_ty),
|
|
|
|
Some(num_method_ty_params) => {
|
|
|
|
// Static default methods are a little unfortunate, in
|
|
|
|
// that the "internal" and "external" type of them differ.
|
|
|
|
// Internally, the method body can refer to Self, but the
|
|
|
|
// externally visable type of the method has a type param
|
|
|
|
// inserted in between the trait type params and the
|
|
|
|
// method type params. The substs that we are given are
|
|
|
|
// the proper substs *internally* to the method body, so
|
|
|
|
// we have to use those when compiling it.
|
|
|
|
//
|
|
|
|
// In order to get the proper substitution to use on the
|
|
|
|
// type of the method, we pull apart the substitution and
|
|
|
|
// stick a substitution for the self type in.
|
|
|
|
// This is a bit unfortunate.
|
|
|
|
|
|
|
|
let idx = psubsts.tys.len() - num_method_ty_params;
|
|
|
|
let substs =
|
|
|
|
(psubsts.tys.slice(0, idx) +
|
2013-08-03 18:59:24 -05:00
|
|
|
&[psubsts.self_ty.unwrap()] +
|
2013-07-08 19:30:36 -05:00
|
|
|
psubsts.tys.tailn(idx));
|
|
|
|
debug!("static default: changed substitution to %s",
|
|
|
|
substs.repr(ccx.tcx));
|
|
|
|
|
|
|
|
ty::subst_tps(ccx.tcx, substs, None, llitem_ty)
|
|
|
|
}
|
|
|
|
};
|
2012-08-28 17:54:45 -05:00
|
|
|
let llfty = type_of_fn_from_ty(ccx, mono_ty);
|
|
|
|
|
2012-09-12 16:48:13 -05:00
|
|
|
ccx.stats.n_monos += 1;
|
|
|
|
|
2013-03-22 21:26:41 -05:00
|
|
|
let depth = match ccx.monomorphizing.find(&fn_id) {
|
|
|
|
Some(&d) => d, None => 0
|
|
|
|
};
|
2012-08-28 17:54:45 -05:00
|
|
|
// Random cut-off -- code that needs to instantiate the same function
|
2013-02-14 17:08:25 -06:00
|
|
|
// recursively more than thirty times can probably safely be assumed to be
|
2012-08-28 17:54:45 -05:00
|
|
|
// causing an infinite expansion.
|
2013-02-14 17:08:25 -06:00
|
|
|
if depth > 30 {
|
2012-08-28 17:54:45 -05:00
|
|
|
ccx.sess.span_fatal(
|
2013-05-02 11:28:53 -05:00
|
|
|
span, "overly deep expansion of inlined function");
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2012-09-26 21:40:05 -05:00
|
|
|
ccx.monomorphizing.insert(fn_id, depth + 1);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-06-21 03:28:33 -05:00
|
|
|
let elt = path_name(gensym_name(ccx.sess.str_of(name)));
|
2013-07-02 14:47:32 -05:00
|
|
|
let mut pt = (*pt).clone();
|
2013-06-21 03:28:33 -05:00
|
|
|
pt.push(elt);
|
2013-07-02 14:47:32 -05:00
|
|
|
let s = mangle_exported_name(ccx, pt.clone(), mono_ty);
|
2013-06-20 11:29:24 -05:00
|
|
|
debug!("monomorphize_fn mangled to %s", s);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-01-15 18:33:20 -06:00
|
|
|
let mk_lldecl = || {
|
2013-07-02 14:47:32 -05:00
|
|
|
let lldecl = decl_internal_cdecl_fn(ccx.llmod, s, llfty);
|
2012-08-28 17:54:45 -05:00
|
|
|
ccx.monomorphized.insert(hash_id, lldecl);
|
|
|
|
lldecl
|
|
|
|
};
|
|
|
|
|
2013-05-05 11:17:59 -05:00
|
|
|
let lldecl = match map_node {
|
2013-01-13 15:13:41 -06:00
|
|
|
ast_map::node_item(i@@ast::item {
|
2013-03-13 21:25:28 -05:00
|
|
|
node: ast::item_fn(ref decl, _, _, _, ref body),
|
2012-12-04 23:13:02 -06:00
|
|
|
_
|
|
|
|
}, _) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
let d = mk_lldecl();
|
2013-08-05 23:21:37 -05:00
|
|
|
set_llvm_fn_attrs(i.attrs, d);
|
2013-03-29 18:55:04 -05:00
|
|
|
trans_fn(ccx,
|
|
|
|
pt,
|
|
|
|
decl,
|
|
|
|
body,
|
|
|
|
d,
|
|
|
|
no_self,
|
2013-07-01 20:38:41 -05:00
|
|
|
Some(psubsts),
|
2013-03-29 18:55:04 -05:00
|
|
|
fn_id.node,
|
|
|
|
[]);
|
2012-08-28 17:54:45 -05:00
|
|
|
d
|
|
|
|
}
|
|
|
|
ast_map::node_item(*) => {
|
2013-05-19 00:07:44 -05:00
|
|
|
ccx.tcx.sess.bug("Can't monomorphize this kind of item")
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-03-18 19:20:45 -05:00
|
|
|
ast_map::node_foreign_item(i, _, _, _) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
let d = mk_lldecl();
|
2013-05-21 14:25:44 -05:00
|
|
|
intrinsic::trans_intrinsic(ccx, d, i, pt, psubsts, i.attrs,
|
|
|
|
ref_id);
|
2012-08-28 17:54:45 -05:00
|
|
|
d
|
|
|
|
}
|
2012-12-04 12:50:00 -06:00
|
|
|
ast_map::node_variant(ref v, enum_item, _) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
let tvs = ty::enum_variants(ccx.tcx, local_def(enum_item.id));
|
2013-08-09 22:49:29 -05:00
|
|
|
let this_tv = *tvs.iter().find(|tv| { tv.id.node == fn_id.node}).unwrap();
|
2012-08-28 17:54:45 -05:00
|
|
|
let d = mk_lldecl();
|
|
|
|
set_inline_hint(d);
|
2013-04-17 11:15:37 -05:00
|
|
|
match v.node.kind {
|
2013-01-07 16:16:52 -06:00
|
|
|
ast::tuple_variant_kind(ref args) => {
|
2013-07-02 14:47:32 -05:00
|
|
|
trans_enum_variant(ccx,
|
|
|
|
enum_item.id,
|
|
|
|
v,
|
|
|
|
(*args).clone(),
|
|
|
|
this_tv.disr_val,
|
|
|
|
Some(psubsts),
|
|
|
|
d);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast::struct_variant_kind(_) =>
|
2013-05-19 00:07:44 -05:00
|
|
|
ccx.tcx.sess.bug("can't monomorphize struct variants"),
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
d
|
|
|
|
}
|
2013-06-28 13:11:47 -05:00
|
|
|
ast_map::node_method(mth, _, _) => {
|
2012-10-05 19:49:13 -05:00
|
|
|
// XXX: What should the self type be here?
|
2012-08-28 17:54:45 -05:00
|
|
|
let d = mk_lldecl();
|
2013-08-05 23:21:37 -05:00
|
|
|
set_llvm_fn_attrs(mth.attrs, d);
|
2013-07-01 20:38:41 -05:00
|
|
|
meth::trans_method(ccx, pt, mth, Some(psubsts), d);
|
2012-08-28 17:54:45 -05:00
|
|
|
d
|
|
|
|
}
|
2012-10-08 14:39:30 -05:00
|
|
|
ast_map::node_trait_method(@ast::provided(mth), _, pt) => {
|
|
|
|
let d = mk_lldecl();
|
2013-08-05 23:21:37 -05:00
|
|
|
set_llvm_fn_attrs(mth.attrs, d);
|
2013-07-02 14:47:32 -05:00
|
|
|
meth::trans_method(ccx, (*pt).clone(), mth, Some(psubsts), d);
|
2012-10-08 14:39:30 -05:00
|
|
|
d
|
|
|
|
}
|
2012-10-24 16:36:00 -05:00
|
|
|
ast_map::node_struct_ctor(struct_def, _, _) => {
|
|
|
|
let d = mk_lldecl();
|
|
|
|
set_inline_hint(d);
|
|
|
|
base::trans_tuple_struct(ccx,
|
2013-07-02 14:47:32 -05:00
|
|
|
struct_def.fields,
|
2013-05-19 00:07:44 -05:00
|
|
|
struct_def.ctor_id.expect("ast-mapped tuple struct \
|
|
|
|
didn't have a ctor id"),
|
2013-07-01 20:38:41 -05:00
|
|
|
Some(psubsts),
|
2012-10-24 16:36:00 -05:00
|
|
|
d);
|
|
|
|
d
|
|
|
|
}
|
2012-10-08 14:39:30 -05:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
// Ugh -- but this ensures any new variants won't be forgotten
|
|
|
|
ast_map::node_expr(*) |
|
|
|
|
ast_map::node_stmt(*) |
|
|
|
|
ast_map::node_trait_method(*) |
|
|
|
|
ast_map::node_arg(*) |
|
|
|
|
ast_map::node_block(*) |
|
2013-03-15 14:24:24 -05:00
|
|
|
ast_map::node_callee_scope(*) |
|
2012-08-28 17:54:45 -05:00
|
|
|
ast_map::node_local(*) => {
|
|
|
|
ccx.tcx.sess.bug(fmt!("Can't monomorphize a %?", map_node))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
ccx.monomorphizing.insert(fn_id, depth);
|
|
|
|
|
|
|
|
debug!("leaving monomorphic fn %s", ty::item_path_str(ccx.tcx, fn_id));
|
2013-02-19 01:40:42 -06:00
|
|
|
(lldecl, must_cast)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-06-13 02:19:50 -05:00
|
|
|
pub fn make_mono_id(ccx: @mut CrateContext,
|
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
|
|
|
item: ast::def_id,
|
2013-07-01 20:38:41 -05:00
|
|
|
substs: ¶m_substs,
|
2013-04-16 19:30:31 -05:00
|
|
|
param_uses: Option<@~[type_use::type_uses]>) -> mono_id {
|
2013-06-29 00:05:50 -05:00
|
|
|
// FIXME (possibly #5801): Need a lot of type hints to get
|
|
|
|
// .collect() to work.
|
2013-08-09 22:22:59 -05:00
|
|
|
let substs_iter = substs.self_ty.iter().chain(substs.tys.iter());
|
2013-07-01 20:38:41 -05:00
|
|
|
let precise_param_ids: ~[(ty::t, Option<@~[mono_id]>)] = match substs.vtables {
|
2012-08-28 17:54:45 -05:00
|
|
|
Some(vts) => {
|
2013-06-26 19:20:53 -05:00
|
|
|
debug!("make_mono_id vtables=%s substs=%s",
|
2013-07-01 20:38:41 -05:00
|
|
|
vts.repr(ccx.tcx), substs.tys.repr(ccx.tcx));
|
2013-08-09 22:22:59 -05:00
|
|
|
let vts_iter = substs.self_vtables.iter().chain(vts.iter());
|
2013-08-09 22:09:47 -05:00
|
|
|
vts_iter.zip(substs_iter).map(|(vtable, subst)| {
|
2013-06-26 19:20:53 -05:00
|
|
|
let v = vtable.map(|vt| meth::vtable_id(ccx, vt));
|
2013-05-17 19:27:44 -05:00
|
|
|
(*subst, if !v.is_empty() { Some(@v) } else { None })
|
2013-06-29 00:05:50 -05:00
|
|
|
}).collect()
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-08-09 22:09:47 -05:00
|
|
|
None => substs_iter.map(|subst| (*subst, None::<@~[mono_id]>)).collect()
|
2012-08-28 17:54:45 -05:00
|
|
|
};
|
2013-07-01 20:38:41 -05:00
|
|
|
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
let param_ids = match param_uses {
|
2013-01-07 16:16:52 -06:00
|
|
|
Some(ref uses) => {
|
2013-07-01 20:38:41 -05:00
|
|
|
// param_uses doesn't include a use for the self type.
|
|
|
|
// We just say it is fully used.
|
|
|
|
let self_use =
|
|
|
|
substs.self_ty.map(|_| type_use::use_repr|type_use::use_tydesc);
|
2013-08-09 22:22:59 -05:00
|
|
|
let uses_iter = self_use.iter().chain(uses.iter());
|
2013-07-01 20:38:41 -05:00
|
|
|
|
2013-08-09 22:09:47 -05:00
|
|
|
precise_param_ids.iter().zip(uses_iter).map(|(id, uses)| {
|
2012-12-05 22:45:58 -06:00
|
|
|
if ccx.sess.no_monomorphic_collapse() {
|
2013-06-27 19:41:35 -05:00
|
|
|
match *id {
|
2012-12-05 22:45:58 -06:00
|
|
|
(a, b) => mono_precise(a, b)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match *id {
|
2013-05-17 19:27:44 -05:00
|
|
|
(a, b@Some(_)) => mono_precise(a, b),
|
2012-12-05 22:45:58 -06:00
|
|
|
(subst, None) => {
|
2013-04-16 19:30:31 -05:00
|
|
|
if *uses == 0 {
|
2012-12-05 22:45:58 -06:00
|
|
|
mono_any
|
|
|
|
} else if *uses == type_use::use_repr &&
|
|
|
|
!ty::type_needs_drop(ccx.tcx, subst)
|
|
|
|
{
|
|
|
|
let llty = type_of::type_of(ccx, subst);
|
|
|
|
let size = machine::llbitsize_of_real(ccx, llty);
|
2013-06-04 00:34:51 -05:00
|
|
|
let align = machine::llalign_of_min(ccx, llty);
|
2013-06-30 23:02:14 -05:00
|
|
|
let mode = datum::appropriate_mode(ccx.tcx, subst);
|
2013-04-06 21:52:35 -05:00
|
|
|
let data_class = mono_data_classify(subst);
|
2012-09-20 14:29:15 -05:00
|
|
|
|
2013-06-04 00:34:51 -05:00
|
|
|
debug!("make_mono_id: type %s -> size %u align %u mode %? class %?",
|
|
|
|
ty_to_str(ccx.tcx, subst),
|
|
|
|
size, align, mode, data_class);
|
|
|
|
|
2012-12-05 22:45:58 -06:00
|
|
|
// Special value for nil to prevent problems
|
|
|
|
// with undef return pointers.
|
|
|
|
if size <= 8u && ty::type_is_nil(subst) {
|
2013-04-06 21:52:35 -05:00
|
|
|
mono_repr(0u, 0u, data_class, mode)
|
2012-12-05 22:45:58 -06:00
|
|
|
} else {
|
2013-04-06 21:52:35 -05:00
|
|
|
mono_repr(size, align, data_class, mode)
|
2012-12-05 22:45:58 -06:00
|
|
|
}
|
2012-09-20 14:29:15 -05:00
|
|
|
} else {
|
2012-12-05 22:45:58 -06:00
|
|
|
mono_precise(subst, None)
|
2012-09-20 14:29:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-06-29 00:05:50 -05:00
|
|
|
}).collect()
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2012-09-21 20:43:30 -05:00
|
|
|
None => {
|
2013-08-09 22:09:47 -05:00
|
|
|
precise_param_ids.iter().map(|x| {
|
2013-06-27 19:41:35 -05:00
|
|
|
let (a, b) = *x;
|
2012-09-21 20:43:30 -05:00
|
|
|
mono_precise(a, b)
|
2013-06-29 00:05:50 -05:00
|
|
|
}).collect()
|
2012-09-21 20:43:30 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
};
|
2013-07-15 19:26:56 -05:00
|
|
|
@mono_id_ {def: item, params: param_ids}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|