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-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
2013-01-07 16:16:52 -06: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;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::base::{get_insn_ctxt};
|
|
|
|
use middle::trans::base::{set_inline_hint_if_appr, set_inline_hint};
|
|
|
|
use middle::trans::base::{trans_enum_variant, trans_struct_dtor};
|
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::foreign;
|
|
|
|
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-02-25 13:11:21 -06:00
|
|
|
use middle::ty;
|
2013-01-31 19:12:29 -06:00
|
|
|
use middle::ty::{FnSig};
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::typeck;
|
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
|
|
|
use util::ppaux::Repr;
|
2012-12-13 15:05:22 -06:00
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::vec;
|
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;
|
2013-03-27 11:55:18 -05:00
|
|
|
use syntax::opt_vec;
|
2012-08-28 17:54:45 -05:00
|
|
|
use syntax::parse::token::special_idents;
|
2013-03-13 21:25:28 -05:00
|
|
|
use syntax::abi::AbiSet;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-02-19 01:40:42 -06:00
|
|
|
pub fn monomorphic_fn(ccx: @CrateContext,
|
2013-01-29 19:57:02 -06:00
|
|
|
fn_id: ast::def_id,
|
2013-03-05 19:36:39 -06:00
|
|
|
real_substs: &[ty::t],
|
2013-01-29 19:57:02 -06:00
|
|
|
vtables: Option<typeck::vtable_res>,
|
|
|
|
impl_did_opt: Option<ast::def_id>,
|
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: Option<ast::node_id>)
|
|
|
|
-> (ValueRef, bool)
|
|
|
|
{
|
|
|
|
debug!("monomorphic_fn(\
|
|
|
|
fn_id=%s, \
|
|
|
|
real_substs=%s, \
|
|
|
|
vtables=%s, \
|
|
|
|
impl_did_opt=%s, \
|
|
|
|
ref_id=%?)",
|
|
|
|
fn_id.repr(ccx.tcx),
|
|
|
|
real_substs.repr(ccx.tcx),
|
|
|
|
vtables.repr(ccx.tcx),
|
|
|
|
impl_did_opt.repr(ccx.tcx),
|
|
|
|
ref_id);
|
|
|
|
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(real_substs.all(|t| !ty::type_needs_infer(*t)));
|
2012-08-28 17:54:45 -05:00
|
|
|
let _icx = ccx.insn_ctxt("monomorphic_fn");
|
|
|
|
let mut must_cast = false;
|
|
|
|
let substs = vec::map(real_substs, |t| {
|
2012-09-21 20:43:30 -05:00
|
|
|
match normalize_for_monomorphization(ccx.tcx, *t) {
|
2012-08-28 17:54:45 -05:00
|
|
|
Some(t) => { must_cast = true; t }
|
2012-09-21 20:43:30 -05:00
|
|
|
None => *t
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-03-28 20:39:09 -05:00
|
|
|
for real_substs.each() |s| { assert!(!ty::type_has_params(*s)); }
|
|
|
|
for substs.each() |s| { assert!(!ty::type_has_params(*s)); }
|
2012-08-28 17:54:45 -05:00
|
|
|
let param_uses = type_use::type_uses_for(ccx, fn_id, substs.len());
|
2013-01-07 16:16:52 -06:00
|
|
|
// XXX: Bad copy.
|
|
|
|
let hash_id = make_mono_id(ccx, fn_id, copy substs, vtables, impl_did_opt,
|
2012-10-08 14:39:30 -05:00
|
|
|
Some(param_uses));
|
2012-08-28 17:54:45 -05:00
|
|
|
if vec::any(hash_id.params,
|
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, \
|
|
|
|
vtables=%s, \
|
|
|
|
substs=%s, \
|
|
|
|
hash_id=%?)",
|
|
|
|
fn_id.repr(ccx.tcx),
|
|
|
|
vtables.repr(ccx.tcx),
|
|
|
|
substs.repr(ccx.tcx),
|
|
|
|
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);
|
|
|
|
let mut llitem_ty = tpt.ty;
|
|
|
|
|
2013-02-05 21:41:45 -06:00
|
|
|
let map_node = session::expect(ccx.sess, ccx.tcx.items.find(&fn_id.node),
|
2012-08-28 17:54:45 -05:00
|
|
|
|| 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));
|
|
|
|
// Get the path so that we can create a symbol
|
2013-03-23 20:45:27 -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
|
|
|
}
|
|
|
|
ast_map::node_dtor(_, dtor, _, pt) =>
|
|
|
|
(pt, special_idents::dtor, dtor.span),
|
2012-10-08 14:39:30 -05:00
|
|
|
ast_map::node_trait_method(@ast::provided(m), _, pt) => {
|
|
|
|
(pt, m.ident, m.span)
|
|
|
|
}
|
|
|
|
ast_map::node_trait_method(@ast::required(_), _, _) => {
|
|
|
|
ccx.tcx.sess.bug(~"Can't monomorphize a required trait method")
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast_map::node_expr(*) => {
|
|
|
|
ccx.tcx.sess.bug(~"Can't monomorphize an expr")
|
|
|
|
}
|
|
|
|
ast_map::node_stmt(*) => {
|
|
|
|
ccx.tcx.sess.bug(~"Can't monomorphize a stmt")
|
|
|
|
}
|
|
|
|
ast_map::node_arg(*) => ccx.tcx.sess.bug(~"Can't monomorphize an arg"),
|
|
|
|
ast_map::node_block(*) => {
|
|
|
|
ccx.tcx.sess.bug(~"Can't monomorphize a block")
|
|
|
|
}
|
|
|
|
ast_map::node_local(*) => {
|
|
|
|
ccx.tcx.sess.bug(~"Can't monomorphize a local")
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// Look up the impl type if we're translating a default method.
|
|
|
|
// XXX: Generics.
|
|
|
|
let impl_ty_opt;
|
|
|
|
match impl_did_opt {
|
|
|
|
None => impl_ty_opt = None,
|
|
|
|
Some(impl_did) => {
|
|
|
|
impl_ty_opt = Some(ty::lookup_item_type(ccx.tcx, impl_did).ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mono_ty = ty::subst_tps(ccx.tcx, substs, impl_ty_opt, 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(
|
|
|
|
span, ~"overly deep expansion of inlined function");
|
|
|
|
}
|
2012-09-26 21:40:05 -05:00
|
|
|
ccx.monomorphizing.insert(fn_id, depth + 1);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-01-07 16:16:52 -06:00
|
|
|
let pt = vec::append(/*bad*/copy *pt,
|
2013-03-05 19:36:39 -06:00
|
|
|
~[path_name((ccx.names)(
|
|
|
|
copy *ccx.sess.str_of(name)))]);
|
2013-01-07 16:16:52 -06:00
|
|
|
let s = mangle_exported_name(ccx, /*bad*/copy pt, mono_ty);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-01-15 18:33:20 -06:00
|
|
|
let mk_lldecl = || {
|
2013-01-07 16:16:52 -06:00
|
|
|
let lldecl = decl_internal_cdecl_fn(ccx.llmod, /*bad*/copy s, llfty);
|
2012-08-28 17:54:45 -05:00
|
|
|
ccx.monomorphized.insert(hash_id, lldecl);
|
|
|
|
lldecl
|
|
|
|
};
|
|
|
|
|
2013-02-18 14:36:30 -06:00
|
|
|
let psubsts = Some(@param_substs {
|
2012-10-08 14:39:30 -05:00
|
|
|
tys: substs,
|
|
|
|
vtables: vtables,
|
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
|
|
|
type_param_defs: tpt.generics.type_param_defs,
|
2012-10-08 14:39:30 -05:00
|
|
|
self_ty: impl_ty_opt
|
|
|
|
});
|
|
|
|
|
2013-03-23 20:45:27 -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-01-07 16:16:52 -06:00
|
|
|
set_inline_hint_if_appr(/*bad*/copy i.attrs, d);
|
2013-03-29 18:55:04 -05:00
|
|
|
trans_fn(ccx,
|
|
|
|
pt,
|
|
|
|
decl,
|
|
|
|
body,
|
|
|
|
d,
|
|
|
|
no_self,
|
|
|
|
psubsts,
|
|
|
|
fn_id.node,
|
|
|
|
None,
|
|
|
|
[]);
|
2012-08-28 17:54:45 -05:00
|
|
|
d
|
|
|
|
}
|
|
|
|
ast_map::node_item(*) => {
|
|
|
|
ccx.tcx.sess.bug(~"Can't monomorphize this kind of item")
|
|
|
|
}
|
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-04-20 06:58:56 -05:00
|
|
|
foreign::trans_intrinsic(ccx, d, i, pt, psubsts.get(), i.attrs,
|
2012-08-28 17:54:45 -05:00
|
|
|
ref_id);
|
|
|
|
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-03-16 14:49:12 -05:00
|
|
|
let this_tv = vec::find(*tvs, |tv| { tv.id.node == fn_id.node}).get();
|
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-04-17 11:15:37 -05:00
|
|
|
trans_enum_variant(ccx, enum_item.id, v, /*bad*/copy *args,
|
2013-02-24 19:19:16 -06:00
|
|
|
this_tv.disr_val, psubsts, d);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast::struct_variant_kind(_) =>
|
|
|
|
ccx.tcx.sess.bug(~"can't monomorphize struct variants"),
|
|
|
|
}
|
|
|
|
d
|
|
|
|
}
|
2012-10-08 14:39:30 -05:00
|
|
|
ast_map::node_method(mth, supplied_impl_did, _) => {
|
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-01-07 16:16:52 -06:00
|
|
|
set_inline_hint_if_appr(/*bad*/copy mth.attrs, d);
|
2012-10-08 14:39:30 -05:00
|
|
|
|
|
|
|
// Override the impl def ID if necessary.
|
|
|
|
let impl_did;
|
|
|
|
match impl_did_opt {
|
|
|
|
None => impl_did = supplied_impl_did,
|
|
|
|
Some(override_impl_did) => impl_did = override_impl_did
|
|
|
|
}
|
|
|
|
|
|
|
|
meth::trans_method(ccx, pt, mth, psubsts, None, d, impl_did);
|
2012-08-28 17:54:45 -05:00
|
|
|
d
|
|
|
|
}
|
|
|
|
ast_map::node_dtor(_, dtor, _, pt) => {
|
|
|
|
let parent_id = match ty::ty_to_def_id(ty::node_id_to_type(ccx.tcx,
|
|
|
|
dtor.node.self_id)) {
|
|
|
|
Some(did) => did,
|
|
|
|
None => ccx.sess.span_bug(dtor.span, ~"Bad self ty in \
|
|
|
|
dtor")
|
|
|
|
};
|
2013-01-31 19:12:29 -06:00
|
|
|
trans_struct_dtor(ccx, /*bad*/copy *pt, &dtor.node.body,
|
2012-08-28 17:54:45 -05:00
|
|
|
dtor.node.id, psubsts, Some(hash_id), parent_id)
|
|
|
|
}
|
2012-10-08 14:39:30 -05:00
|
|
|
ast_map::node_trait_method(@ast::provided(mth), _, pt) => {
|
|
|
|
let d = mk_lldecl();
|
2013-01-07 16:16:52 -06:00
|
|
|
set_inline_hint_if_appr(/*bad*/copy mth.attrs, d);
|
2012-10-22 15:33:12 -05:00
|
|
|
debug!("monomorphic_fn impl_did_opt is %?", impl_did_opt);
|
2013-01-07 16:16:52 -06:00
|
|
|
meth::trans_method(ccx, /*bad*/copy *pt, mth, psubsts, None, d,
|
2012-10-08 14:39:30 -05:00
|
|
|
impl_did_opt.get());
|
|
|
|
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-01-07 16:16:52 -06:00
|
|
|
/*bad*/copy struct_def.fields,
|
2013-03-16 14:49:12 -05:00
|
|
|
struct_def.ctor_id.expect(~"ast-mapped tuple struct \
|
|
|
|
didn't have a ctor id"),
|
2012-10-24 16:36:00 -05:00
|
|
|
psubsts,
|
|
|
|
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(*) |
|
|
|
|
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-01-29 19:57:02 -06:00
|
|
|
pub fn normalize_for_monomorphization(tcx: ty::ctxt,
|
|
|
|
ty: ty::t) -> Option<ty::t> {
|
2012-08-28 17:54:45 -05:00
|
|
|
// FIXME[mono] could do this recursively. is that worthwhile? (#2529)
|
2013-01-31 19:12:29 -06:00
|
|
|
return match ty::get(ty).sty {
|
2012-09-07 09:37:19 -05:00
|
|
|
ty::ty_box(*) => {
|
|
|
|
Some(ty::mk_opaque_box(tcx))
|
|
|
|
}
|
2013-01-31 19:12:29 -06:00
|
|
|
ty::ty_bare_fn(_) => {
|
|
|
|
Some(ty::mk_bare_fn(
|
2012-09-07 09:37:19 -05:00
|
|
|
tcx,
|
2013-01-31 19:12:29 -06:00
|
|
|
ty::BareFnTy {
|
|
|
|
purity: ast::impure_fn,
|
2013-03-13 21:25:28 -05:00
|
|
|
abis: AbiSet::Rust(),
|
2013-03-27 11:55:18 -05:00
|
|
|
sig: FnSig {bound_lifetime_names: opt_vec::Empty,
|
|
|
|
inputs: ~[],
|
2013-01-31 19:12:29 -06:00
|
|
|
output: ty::mk_nil(tcx)}}))
|
2012-09-07 09:37:19 -05:00
|
|
|
}
|
2013-01-31 19:12:29 -06:00
|
|
|
ty::ty_closure(ref fty) => {
|
|
|
|
Some(normalized_closure_ty(tcx, fty.sigil))
|
|
|
|
}
|
2013-04-02 02:40:57 -05:00
|
|
|
ty::ty_trait(_, _, ref store, _) => {
|
2013-03-08 23:16:09 -06:00
|
|
|
let sigil = match *store {
|
|
|
|
ty::UniqTraitStore => ast::OwnedSigil,
|
2013-03-27 05:16:28 -05:00
|
|
|
ty::BoxTraitStore => ast::ManagedSigil,
|
2013-03-08 23:16:09 -06:00
|
|
|
ty::RegionTraitStore(_) => ast::BorrowedSigil,
|
2013-01-31 19:12:29 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// Traits have the same runtime representation as closures.
|
|
|
|
Some(normalized_closure_ty(tcx, sigil))
|
2012-09-07 09:37:19 -05:00
|
|
|
}
|
|
|
|
ty::ty_ptr(_) => {
|
|
|
|
Some(ty::mk_uint(tcx))
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
None
|
|
|
|
}
|
2013-01-31 19:12:29 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
fn normalized_closure_ty(tcx: ty::ctxt,
|
|
|
|
sigil: ast::Sigil) -> ty::t
|
|
|
|
{
|
|
|
|
ty::mk_closure(
|
|
|
|
tcx,
|
|
|
|
ty::ClosureTy {
|
|
|
|
purity: ast::impure_fn,
|
|
|
|
sigil: sigil,
|
|
|
|
onceness: ast::Many,
|
|
|
|
region: ty::re_static,
|
2013-03-27 11:55:18 -05:00
|
|
|
sig: ty::FnSig {bound_lifetime_names: opt_vec::Empty,
|
|
|
|
inputs: ~[],
|
2013-01-31 19:12:29 -06:00
|
|
|
output: ty::mk_nil(tcx)}})
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
pub fn make_mono_id(ccx: @CrateContext,
|
|
|
|
item: ast::def_id,
|
|
|
|
substs: &[ty::t],
|
2013-01-29 19:57:02 -06:00
|
|
|
vtables: Option<typeck::vtable_res>,
|
|
|
|
impl_did_opt: Option<ast::def_id>,
|
2013-04-16 19:30:31 -05:00
|
|
|
param_uses: Option<@~[type_use::type_uses]>) -> mono_id {
|
2012-08-28 17:54:45 -05:00
|
|
|
let precise_param_ids = match vtables {
|
|
|
|
Some(vts) => {
|
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
|
|
|
let item_ty = ty::lookup_item_type(ccx.tcx, item);
|
2013-01-16 20:45:05 -06:00
|
|
|
let mut i = 0;
|
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
|
|
|
vec::map2(*item_ty.generics.type_param_defs, substs, |type_param_def, subst| {
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut v = ~[];
|
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
|
|
|
for type_param_def.bounds.each |bound| {
|
2012-09-18 23:41:37 -05:00
|
|
|
match *bound {
|
2012-08-28 17:54:45 -05:00
|
|
|
ty::bound_trait(_) => {
|
2013-01-07 16:16:52 -06:00
|
|
|
v.push(meth::vtable_id(ccx, /*bad*/copy vts[i]));
|
2013-04-16 19:30:31 -05:00
|
|
|
i += 1;
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
2013-04-16 19:30:31 -05:00
|
|
|
(*subst, if !v.is_empty() { Some(v) } else { None })
|
2012-08-28 17:54:45 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
None => {
|
2012-09-21 20:43:30 -05:00
|
|
|
vec::map(substs, |subst| (*subst, None))
|
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-04-16 19:30:31 -05:00
|
|
|
vec::map2(precise_param_ids, **uses, |id, uses| {
|
2012-12-05 22:45:58 -06:00
|
|
|
if ccx.sess.no_monomorphic_collapse() {
|
2013-01-10 08:29:26 -06:00
|
|
|
match copy *id {
|
2012-12-05 22:45:58 -06:00
|
|
|
(a, b) => mono_precise(a, b)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match *id {
|
2013-01-07 16:16:52 -06:00
|
|
|
// XXX: Bad copy.
|
|
|
|
(a, copy 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-01-30 13:46:19 -06:00
|
|
|
let align = machine::llalign_of_pref(ccx, llty);
|
2012-12-05 22:45:58 -06:00
|
|
|
let mode = datum::appropriate_mode(subst);
|
2012-09-20 14:29:15 -05:00
|
|
|
|
2012-12-05 22:45:58 -06:00
|
|
|
// FIXME(#3547)---scalars and floats are
|
|
|
|
// treated differently in most ABIs. But we
|
|
|
|
// should be doing something more detailed
|
|
|
|
// here.
|
|
|
|
let is_float = match ty::get(subst).sty {
|
|
|
|
ty::ty_float(_) => true,
|
|
|
|
_ => false
|
|
|
|
};
|
2012-09-20 14:29:15 -05:00
|
|
|
|
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) {
|
|
|
|
mono_repr(0u, 0u, is_float, mode)
|
|
|
|
} else {
|
|
|
|
mono_repr(size, align, is_float, mode)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2012-09-21 20:43:30 -05:00
|
|
|
None => {
|
|
|
|
precise_param_ids.map(|x| {
|
2013-01-10 08:29:26 -06:00
|
|
|
let (a, b) = copy *x;
|
2012-09-21 20:43:30 -05:00
|
|
|
mono_precise(a, b)
|
|
|
|
})
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
};
|
2013-01-06 13:16:14 -06:00
|
|
|
@mono_id_ {def: item, params: param_ids, impl_did_opt: impl_did_opt}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|