1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use back::link::exported_name;
17 use middle::subst::{Subst, Substs};
19 use middle::ty_fold::{TypeFolder, TypeFoldable};
20 use trans::attributes;
21 use trans::base::{trans_enum_variant, push_ctxt, get_item_val};
22 use trans::base::trans_fn;
27 use middle::ty::{self, HasProjectionTypes, Ty};
28 use util::ppaux::Repr;
33 use syntax::ast_util::local_def;
35 use syntax::codemap::DUMMY_SP;
36 use std::hash::{Hasher, Hash, SipHasher};
38 pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
40 psubsts: &'tcx subst::Substs<'tcx>,
41 ref_id: Option<ast::NodeId>)
42 -> (ValueRef, Ty<'tcx>, bool) {
43 debug!("monomorphic_fn(\
47 fn_id.repr(ccx.tcx()),
48 psubsts.repr(ccx.tcx()),
51 assert!(psubsts.types.all(|t| {
52 !ty::type_needs_infer(*t) && !ty::type_has_params(*t)
55 let _icx = push_ctxt("monomorphic_fn");
57 let hash_id = MonoId {
59 params: &psubsts.types
62 let item_ty = ty::lookup_item_type(ccx.tcx(), fn_id).ty;
63 let mono_ty = item_ty.subst(ccx.tcx(), psubsts);
65 match ccx.monomorphized().borrow().get(&hash_id) {
67 debug!("leaving monomorphic fn {}",
68 ty::item_path_str(ccx.tcx(), fn_id));
69 return (val, mono_ty, false);
74 debug!("monomorphic_fn(\
78 fn_id.repr(ccx.tcx()),
79 psubsts.repr(ccx.tcx()),
83 let map_node = session::expect(
85 ccx.tcx().map.find(fn_id.node),
87 format!("while monomorphizing {:?}, couldn't find it in \
88 the item map (may have attempted to monomorphize \
89 an item defined in a different crate?)",
93 if let ast_map::NodeForeignItem(_) = map_node {
94 if ccx.tcx().map.get_foreign_abi(fn_id.node) != abi::RustIntrinsic {
95 // Foreign externs don't have to be monomorphized.
96 return (get_item_val(ccx, fn_id.node), mono_ty, true);
100 debug!("monomorphic_fn about to subst into {}", item_ty.repr(ccx.tcx()));
102 debug!("mono_ty = {} (post-substitution)", mono_ty.repr(ccx.tcx()));
104 let mono_ty = normalize_associated_type(ccx.tcx(), &mono_ty);
105 debug!("mono_ty = {} (post-normalization)", mono_ty.repr(ccx.tcx()));
107 ccx.stats().n_monos.set(ccx.stats().n_monos.get() + 1);
111 let mut monomorphizing = ccx.monomorphizing().borrow_mut();
112 depth = match monomorphizing.get(&fn_id) {
113 Some(&d) => d, None => 0
116 // Random cut-off -- code that needs to instantiate the same function
117 // recursively more than thirty times can probably safely be assumed
118 // to be causing an infinite expansion.
119 if depth > ccx.sess().recursion_limit.get() {
120 ccx.sess().span_fatal(ccx.tcx().map.span(fn_id.node),
121 "reached the recursion limit during monomorphization");
124 monomorphizing.insert(fn_id, depth + 1);
129 let mut state = SipHasher::new();
130 hash_id.hash(&mut state);
131 mono_ty.hash(&mut state);
133 hash = format!("h{}", state.finish());
134 ccx.tcx().map.with_path(fn_id.node, |path| {
135 exported_name(path, &hash[..])
139 debug!("monomorphize_fn mangled to {}", s);
141 // This shouldn't need to option dance.
142 let mut hash_id = Some(hash_id);
143 let mut mk_lldecl = |abi: abi::Abi| {
144 let lldecl = if abi != abi::Rust {
145 foreign::decl_rust_fn_with_foreign_abi(ccx, mono_ty, &s[..])
147 // FIXME(nagisa): perhaps needs a more fine grained selection? See setup_lldecl below.
148 declare::define_internal_rust_fn(ccx, &s[..], mono_ty).unwrap_or_else(||{
149 ccx.sess().bug(&format!("symbol `{}` already defined", s));
153 ccx.monomorphized().borrow_mut().insert(hash_id.take().unwrap(), lldecl);
156 let setup_lldecl = |lldecl, attrs: &[ast::Attribute]| {
157 base::update_linkage(ccx, lldecl, None, base::OriginalTranslation);
158 attributes::from_fn_attrs(ccx, attrs, lldecl);
160 let is_first = !ccx.available_monomorphizations().borrow().contains(&s);
162 ccx.available_monomorphizations().borrow_mut().insert(s.clone());
165 let trans_everywhere = attr::requests_inline(attrs);
166 if trans_everywhere && !is_first {
167 llvm::SetLinkage(lldecl, llvm::AvailableExternallyLinkage);
170 // If `true`, then `lldecl` should be given a function body.
171 // Otherwise, it should be left as a declaration of an external
172 // function, with no definition in the current compilation unit.
173 trans_everywhere || is_first
176 let lldecl = match map_node {
177 ast_map::NodeItem(i) => {
180 node: ast::ItemFn(ref decl, _, abi, _, ref body),
183 let d = mk_lldecl(abi);
184 let needs_body = setup_lldecl(d, &i.attrs);
186 if abi != abi::Rust {
187 foreign::trans_rust_fn_with_foreign_abi(
188 ccx, &**decl, &**body, &[], d, psubsts, fn_id.node,
191 trans_fn(ccx, &**decl, &**body, d, psubsts, fn_id.node, &[]);
198 ccx.sess().bug("Can't monomorphize this kind of item")
202 ast_map::NodeVariant(v) => {
203 let parent = ccx.tcx().map.get_parent(fn_id.node);
204 let tvs = ty::enum_variants(ccx.tcx(), local_def(parent));
205 let this_tv = tvs.iter().find(|tv| { tv.id.node == fn_id.node}).unwrap();
206 let d = mk_lldecl(abi::Rust);
207 attributes::inline(d, attributes::InlineAttr::Hint);
209 ast::TupleVariantKind(ref args) => {
210 trans_enum_variant(ccx,
218 ast::StructVariantKind(_) =>
219 ccx.sess().bug("can't monomorphize struct variants"),
223 ast_map::NodeImplItem(impl_item) => {
224 match impl_item.node {
225 ast::MethodImplItem(ref sig, ref body) => {
226 let d = mk_lldecl(abi::Rust);
227 let needs_body = setup_lldecl(d, &impl_item.attrs);
240 ccx.sess().bug(&format!("can't monomorphize a {:?}",
245 ast_map::NodeTraitItem(trait_item) => {
246 match trait_item.node {
247 ast::MethodTraitItem(ref sig, Some(ref body)) => {
248 let d = mk_lldecl(abi::Rust);
249 let needs_body = setup_lldecl(d, &trait_item.attrs);
251 trans_fn(ccx, &sig.decl, body, d,
252 psubsts, trait_item.id, &[]);
257 ccx.sess().bug(&format!("can't monomorphize a {:?}",
262 ast_map::NodeStructCtor(struct_def) => {
263 let d = mk_lldecl(abi::Rust);
264 attributes::inline(d, attributes::InlineAttr::Hint);
265 base::trans_tuple_struct(ccx,
267 struct_def.ctor_id.expect("ast-mapped tuple struct \
268 didn't have a ctor id"),
274 // Ugh -- but this ensures any new variants won't be forgotten
275 ast_map::NodeForeignItem(..) |
276 ast_map::NodeLifetime(..) |
277 ast_map::NodeExpr(..) |
278 ast_map::NodeStmt(..) |
279 ast_map::NodeArg(..) |
280 ast_map::NodeBlock(..) |
281 ast_map::NodePat(..) |
282 ast_map::NodeLocal(..) => {
283 ccx.sess().bug(&format!("can't monomorphize a {:?}",
288 ccx.monomorphizing().borrow_mut().insert(fn_id, depth);
290 debug!("leaving monomorphic fn {}", ty::item_path_str(ccx.tcx(), fn_id));
291 (lldecl, mono_ty, true)
294 #[derive(PartialEq, Eq, Hash, Debug)]
295 pub struct MonoId<'tcx> {
297 pub params: &'tcx subst::VecPerParamSpace<Ty<'tcx>>
300 /// Monomorphizes a type from the AST by first applying the in-scope
301 /// substitutions and then normalizing any associated types.
302 pub fn apply_param_substs<'tcx,T>(tcx: &ty::ctxt<'tcx>,
303 param_substs: &Substs<'tcx>,
306 where T : TypeFoldable<'tcx> + Repr<'tcx> + HasProjectionTypes + Clone
308 let substituted = value.subst(tcx, param_substs);
309 normalize_associated_type(tcx, &substituted)
312 /// Removes associated types, if any. Since this during
313 /// monomorphization, we know that only concrete types are involved,
314 /// and hence we can be sure that all associated types will be
315 /// completely normalized away.
316 pub fn normalize_associated_type<'tcx,T>(tcx: &ty::ctxt<'tcx>, value: &T) -> T
317 where T : TypeFoldable<'tcx> + Repr<'tcx> + HasProjectionTypes + Clone
319 debug!("normalize_associated_type(t={})", value.repr(tcx));
321 let value = erase_regions(tcx, value);
323 if !value.has_projection_types() {
327 // FIXME(#20304) -- cache
329 let infcx = infer::new_infer_ctxt(tcx);
330 let typer = NormalizingClosureTyper::new(tcx);
331 let mut selcx = traits::SelectionContext::new(&infcx, &typer);
332 let cause = traits::ObligationCause::dummy();
333 let traits::Normalized { value: result, obligations } =
334 traits::normalize(&mut selcx, cause, &value);
336 debug!("normalize_associated_type: result={} obligations={}",
338 obligations.repr(tcx));
340 let mut fulfill_cx = traits::FulfillmentContext::new();
341 for obligation in obligations {
342 fulfill_cx.register_predicate_obligation(&infcx, obligation);
344 let result = drain_fulfillment_cx_or_panic(DUMMY_SP, &infcx, &mut fulfill_cx, &result);