rust/src/librustc/middle/trans/monomorphize.rs

261 lines
8.5 KiB
Rust
Raw Normal View History

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