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;
14 use middle::def_id::DefId;
15 use middle::infer::normalize_associated_type;
17 use middle::subst::{Subst, Substs};
18 use middle::ty::fold::{TypeFolder, TypeFoldable};
19 use trans::attributes;
20 use trans::base::{push_ctxt};
21 use trans::base::trans_fn;
25 use middle::ty::{self, Ty, TyCtxt};
27 use rustc::front::map as hir_map;
28 use rustc::util::ppaux;
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 -> (ValueRef, Ty<'tcx>) {
42 debug!("monomorphic_fn(fn_id={:?}, real_substs={:?})", fn_id, psubsts);
44 assert!(!psubsts.types.needs_infer() && !psubsts.types.has_param_types());
46 let _icx = push_ctxt("monomorphic_fn");
48 let instance = Instance::new(fn_id, psubsts);
50 let item_ty = ccx.tcx().lookup_item_type(fn_id).ty;
52 debug!("monomorphic_fn about to subst into {:?}", item_ty);
53 let mono_ty = apply_param_substs(ccx.tcx(), psubsts, &item_ty);
54 debug!("mono_ty = {:?} (post-substitution)", mono_ty);
56 match ccx.instances().borrow().get(&instance) {
58 debug!("leaving monomorphic fn {:?}", instance);
59 return (val, mono_ty);
64 debug!("monomorphic_fn({:?})", instance);
66 ccx.stats().n_monos.set(ccx.stats().n_monos.get() + 1);
70 let mut monomorphizing = ccx.monomorphizing().borrow_mut();
71 depth = match monomorphizing.get(&fn_id) {
72 Some(&d) => d, None => 0
75 debug!("monomorphic_fn: depth for fn_id={:?} is {:?}", fn_id, depth+1);
77 // Random cut-off -- code that needs to instantiate the same function
78 // recursively more than thirty times can probably safely be assumed
79 // to be causing an infinite expansion.
80 if depth > ccx.sess().recursion_limit.get() {
81 let error = format!("reached the recursion limit while instantiating `{}`",
83 if let Some(id) = ccx.tcx().map.as_local_node_id(fn_id) {
84 ccx.sess().span_fatal(ccx.tcx().map.span(id), &error);
86 ccx.sess().fatal(&error);
90 monomorphizing.insert(fn_id, depth + 1);
95 let mut state = SipHasher::new();
96 instance.hash(&mut state);
97 mono_ty.hash(&mut state);
99 hash = format!("h{}", state.finish());
100 let path = ccx.tcx().map.def_path(fn_id);
101 exported_name(path, &hash[..])
104 debug!("monomorphize_fn mangled to {}", s);
105 assert!(declare::get_defined_value(ccx, &s).is_none());
107 // FIXME(nagisa): perhaps needs a more fine grained selection?
108 let lldecl = declare::define_internal_fn(ccx, &s, mono_ty);
109 // FIXME(eddyb) Doubt all extern fn should allow unwinding.
110 attributes::unwind(lldecl, true);
112 ccx.instances().borrow_mut().insert(instance, lldecl);
114 // we can only monomorphize things in this crate (or inlined into it)
115 let fn_node_id = ccx.tcx().map.as_local_node_id(fn_id).unwrap();
116 let map_node = errors::expect(
117 ccx.sess().diagnostic(),
118 ccx.tcx().map.find(fn_node_id),
120 format!("while instantiating `{}`, couldn't find it in \
121 the item map (may have attempted to monomorphize \
122 an item defined in a different crate?)",
126 hir_map::NodeItem(&hir::Item {
127 ref attrs, node: hir::ItemFn(ref decl, _, _, _, _, ref body), ..
129 hir_map::NodeTraitItem(&hir::TraitItem {
130 ref attrs, node: hir::MethodTraitItem(
131 hir::MethodSig { ref decl, .. }, Some(ref body)), ..
133 hir_map::NodeImplItem(&hir::ImplItem {
134 ref attrs, node: hir::ImplItemKind::Method(
135 hir::MethodSig { ref decl, .. }, ref body), ..
137 base::update_linkage(ccx, lldecl, None, base::OriginalTranslation);
138 attributes::from_fn_attrs(ccx, attrs, lldecl);
140 let is_first = !ccx.available_monomorphizations().borrow().contains(&s);
142 ccx.available_monomorphizations().borrow_mut().insert(s.clone());
145 let trans_everywhere = attr::requests_inline(attrs);
146 if trans_everywhere && !is_first {
147 llvm::SetLinkage(lldecl, llvm::AvailableExternallyLinkage);
150 if trans_everywhere || is_first {
151 trans_fn(ccx, decl, body, lldecl, psubsts, fn_node_id);
155 hir_map::NodeVariant(_) | hir_map::NodeStructCtor(_) => {
156 let disr = match map_node {
157 hir_map::NodeVariant(_) => {
158 Disr::from(inlined_variant_def(ccx, fn_node_id).disr_val)
160 hir_map::NodeStructCtor(_) => Disr(0),
163 attributes::inline(lldecl, attributes::InlineAttr::Hint);
164 base::trans_ctor_shim(ccx, fn_node_id, disr, psubsts, lldecl);
167 _ => unreachable!("can't monomorphize a {:?}", map_node)
170 ccx.monomorphizing().borrow_mut().insert(fn_id, depth);
172 debug!("leaving monomorphic fn {}", ccx.tcx().item_path_str(fn_id));
176 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
177 pub struct Instance<'tcx> {
179 pub substs: &'tcx Substs<'tcx>,
182 impl<'tcx> fmt::Display for Instance<'tcx> {
183 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
184 ppaux::parameterized(f, &self.substs, self.def, ppaux::Ns::Value, &[],
185 |tcx| tcx.lookup_item_type(self.def).generics)
189 impl<'tcx> Instance<'tcx> {
190 pub fn new(def_id: DefId, substs: &'tcx Substs<'tcx>)
192 assert!(substs.regions.iter().all(|&r| r == ty::ReStatic));
193 Instance { def: def_id, substs: substs }
195 pub fn mono(tcx: &TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> {
196 Instance::new(def_id, &tcx.mk_substs(Substs::empty()))
200 /// Monomorphizes a type from the AST by first applying the in-scope
201 /// substitutions and then normalizing any associated types.
202 pub fn apply_param_substs<'tcx,T>(tcx: &TyCtxt<'tcx>,
203 param_substs: &Substs<'tcx>,
206 where T : TypeFoldable<'tcx>
208 let substituted = value.subst(tcx, param_substs);
209 normalize_associated_type(tcx, &substituted)
213 /// Returns the normalized type of a struct field
214 pub fn field_ty<'tcx>(tcx: &TyCtxt<'tcx>,
215 param_substs: &Substs<'tcx>,
216 f: ty::FieldDef<'tcx>)
219 normalize_associated_type(tcx, &f.ty(tcx, param_substs))