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;
15 use middle::def_id::DefId;
16 use middle::infer::normalize_associated_type;
18 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, HasTypeFlags, Ty};
28 use rustc::front::map as hir_map;
35 use std::hash::{Hasher, Hash, SipHasher};
37 pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
39 psubsts: &'tcx subst::Substs<'tcx>,
40 ref_id: Option<ast::NodeId>)
41 -> (ValueRef, Ty<'tcx>, bool) {
42 debug!("monomorphic_fn(\
50 assert!(!psubsts.types.needs_infer() && !psubsts.types.has_param_types());
52 let _icx = push_ctxt("monomorphic_fn");
54 let hash_id = MonoId {
56 params: &psubsts.types
59 let item_ty = ccx.tcx().lookup_item_type(fn_id).ty;
61 debug!("monomorphic_fn about to subst into {:?}", item_ty);
62 let mono_ty = apply_param_substs(ccx.tcx(), psubsts, &item_ty);
63 debug!("mono_ty = {:?} (post-substitution)", mono_ty);
65 match ccx.monomorphized().borrow().get(&hash_id) {
67 debug!("leaving monomorphic fn {}",
68 ccx.tcx().item_path_str(fn_id));
69 return (val, mono_ty, false);
74 debug!("monomorphic_fn(\
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 hir_map::NodeForeignItem(_) = map_node {
94 let abi = ccx.tcx().map.get_foreign_abi(fn_id.node);
95 if abi != abi::RustIntrinsic && abi != abi::PlatformIntrinsic {
96 // Foreign externs don't have to be monomorphized.
97 return (get_item_val(ccx, fn_id.node), mono_ty, true);
101 ccx.stats().n_monos.set(ccx.stats().n_monos.get() + 1);
105 let mut monomorphizing = ccx.monomorphizing().borrow_mut();
106 depth = match monomorphizing.get(&fn_id) {
107 Some(&d) => d, None => 0
110 // Random cut-off -- code that needs to instantiate the same function
111 // recursively more than thirty times can probably safely be assumed
112 // to be causing an infinite expansion.
113 if depth > ccx.sess().recursion_limit.get() {
114 ccx.sess().span_fatal(ccx.tcx().map.span(fn_id.node),
115 "reached the recursion limit during monomorphization");
118 monomorphizing.insert(fn_id, depth + 1);
123 let mut state = SipHasher::new();
124 hash_id.hash(&mut state);
125 mono_ty.hash(&mut state);
127 hash = format!("h{}", state.finish());
128 ccx.tcx().map.with_path(fn_id.node, |path| {
129 exported_name(path, &hash[..])
133 debug!("monomorphize_fn mangled to {}", s);
135 // This shouldn't need to option dance.
136 let mut hash_id = Some(hash_id);
137 let mut mk_lldecl = |abi: abi::Abi| {
138 let lldecl = if abi != abi::Rust {
139 foreign::decl_rust_fn_with_foreign_abi(ccx, mono_ty, &s[..])
141 // FIXME(nagisa): perhaps needs a more fine grained selection? See
142 // setup_lldecl below.
143 declare::define_internal_rust_fn(ccx, &s, mono_ty)
146 ccx.monomorphized().borrow_mut().insert(hash_id.take().unwrap(), lldecl);
149 let setup_lldecl = |lldecl, attrs: &[ast::Attribute]| {
150 base::update_linkage(ccx, lldecl, None, base::OriginalTranslation);
151 attributes::from_fn_attrs(ccx, attrs, lldecl);
153 let is_first = !ccx.available_monomorphizations().borrow().contains(&s);
155 ccx.available_monomorphizations().borrow_mut().insert(s.clone());
158 let trans_everywhere = attr::requests_inline(attrs);
159 if trans_everywhere && !is_first {
160 llvm::SetLinkage(lldecl, llvm::AvailableExternallyLinkage);
163 // If `true`, then `lldecl` should be given a function body.
164 // Otherwise, it should be left as a declaration of an external
165 // function, with no definition in the current compilation unit.
166 trans_everywhere || is_first
169 let lldecl = match map_node {
170 hir_map::NodeItem(i) => {
173 node: hir::ItemFn(ref decl, _, _, abi, _, ref body),
176 let d = mk_lldecl(abi);
177 let needs_body = setup_lldecl(d, &i.attrs);
179 if abi != abi::Rust {
180 foreign::trans_rust_fn_with_foreign_abi(
181 ccx, &**decl, &**body, &[], d, psubsts, fn_id.node,
184 trans_fn(ccx, &**decl, &**body, d, psubsts, fn_id.node, &[]);
191 ccx.sess().bug("Can't monomorphize this kind of item")
195 hir_map::NodeVariant(v) => {
196 let variant = inlined_variant_def(ccx, fn_id.node);
197 assert_eq!(v.node.name.name, variant.name);
198 let d = mk_lldecl(abi::Rust);
199 attributes::inline(d, attributes::InlineAttr::Hint);
200 trans_enum_variant(ccx, fn_id.node, variant.disr_val, psubsts, d);
203 hir_map::NodeImplItem(impl_item) => {
204 match impl_item.node {
205 hir::MethodImplItem(ref sig, ref body) => {
206 let d = mk_lldecl(abi::Rust);
207 let needs_body = setup_lldecl(d, &impl_item.attrs);
220 ccx.sess().bug(&format!("can't monomorphize a {:?}",
225 hir_map::NodeTraitItem(trait_item) => {
226 match trait_item.node {
227 hir::MethodTraitItem(ref sig, Some(ref body)) => {
228 let d = mk_lldecl(abi::Rust);
229 let needs_body = setup_lldecl(d, &trait_item.attrs);
231 trans_fn(ccx, &sig.decl, body, d,
232 psubsts, trait_item.id, &[]);
237 ccx.sess().bug(&format!("can't monomorphize a {:?}",
242 hir_map::NodeStructCtor(struct_def) => {
243 let d = mk_lldecl(abi::Rust);
244 attributes::inline(d, attributes::InlineAttr::Hint);
245 base::trans_tuple_struct(ccx,
246 struct_def.ctor_id.expect("ast-mapped tuple struct \
247 didn't have a ctor id"),
253 // Ugh -- but this ensures any new variants won't be forgotten
254 hir_map::NodeForeignItem(..) |
255 hir_map::NodeLifetime(..) |
256 hir_map::NodeTyParam(..) |
257 hir_map::NodeExpr(..) |
258 hir_map::NodeStmt(..) |
259 hir_map::NodeArg(..) |
260 hir_map::NodeBlock(..) |
261 hir_map::NodePat(..) |
262 hir_map::NodeLocal(..) => {
263 ccx.sess().bug(&format!("can't monomorphize a {:?}",
268 ccx.monomorphizing().borrow_mut().insert(fn_id, depth);
270 debug!("leaving monomorphic fn {}", ccx.tcx().item_path_str(fn_id));
271 (lldecl, mono_ty, true)
274 #[derive(PartialEq, Eq, Hash, Debug)]
275 pub struct MonoId<'tcx> {
277 pub params: &'tcx subst::VecPerParamSpace<Ty<'tcx>>
280 /// Monomorphizes a type from the AST by first applying the in-scope
281 /// substitutions and then normalizing any associated types.
282 pub fn apply_param_substs<'tcx,T>(tcx: &ty::ctxt<'tcx>,
283 param_substs: &Substs<'tcx>,
286 where T : TypeFoldable<'tcx> + HasTypeFlags
288 let substituted = value.subst(tcx, param_substs);
289 normalize_associated_type(tcx, &substituted)
293 /// Returns the normalized type of a struct field
294 pub fn field_ty<'tcx>(tcx: &ty::ctxt<'tcx>,
295 param_substs: &Substs<'tcx>,
296 f: ty::FieldDef<'tcx>)
299 normalize_associated_type(tcx, &f.ty(tcx, param_substs))