]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans/monomorphize.rs
63fb8c5fb5e1c4326dbbf5d550fc1f7bf2518963
[rust.git] / src / librustc_trans / trans / monomorphize.rs
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.
4 //
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.
10
11 use back::link::exported_name;
12 use llvm::ValueRef;
13 use llvm;
14 use middle::def_id::DefId;
15 use middle::infer::normalize_associated_type;
16 use middle::subst;
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;
22 use trans::base;
23 use trans::common::*;
24 use trans::declare;
25 use middle::ty::{self, Ty, TyCtxt};
26 use trans::Disr;
27 use rustc::front::map as hir_map;
28 use rustc::util::ppaux;
29
30 use rustc_front::hir;
31
32 use syntax::attr;
33 use syntax::errors;
34
35 use std::fmt;
36 use std::hash::{Hasher, Hash, SipHasher};
37
38 pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
39                                 fn_id: DefId,
40                                 psubsts: &'tcx subst::Substs<'tcx>)
41                                 -> (ValueRef, Ty<'tcx>) {
42     debug!("monomorphic_fn(fn_id={:?}, real_substs={:?})", fn_id, psubsts);
43
44     assert!(!psubsts.types.needs_infer() && !psubsts.types.has_param_types());
45
46     let _icx = push_ctxt("monomorphic_fn");
47
48     let instance = Instance::new(fn_id, psubsts);
49
50     let item_ty = ccx.tcx().lookup_item_type(fn_id).ty;
51
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);
55
56     match ccx.instances().borrow().get(&instance) {
57         Some(&val) => {
58             debug!("leaving monomorphic fn {:?}", instance);
59             return (val, mono_ty);
60         }
61         None => ()
62     }
63
64     debug!("monomorphic_fn({:?})", instance);
65
66     ccx.stats().n_monos.set(ccx.stats().n_monos.get() + 1);
67
68     let depth;
69     {
70         let mut monomorphizing = ccx.monomorphizing().borrow_mut();
71         depth = match monomorphizing.get(&fn_id) {
72             Some(&d) => d, None => 0
73         };
74
75         debug!("monomorphic_fn: depth for fn_id={:?} is {:?}", fn_id, depth+1);
76
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 `{}`",
82                                 instance);
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);
85             } else {
86                 ccx.sess().fatal(&error);
87             }
88         }
89
90         monomorphizing.insert(fn_id, depth + 1);
91     }
92
93     let hash;
94     let s = {
95         let mut state = SipHasher::new();
96         instance.hash(&mut state);
97         mono_ty.hash(&mut state);
98
99         hash = format!("h{}", state.finish());
100         let path = ccx.tcx().map.def_path(fn_id);
101         exported_name(path, &hash[..])
102     };
103
104     debug!("monomorphize_fn mangled to {}", s);
105     assert!(declare::get_defined_value(ccx, &s).is_none());
106
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);
111
112     ccx.instances().borrow_mut().insert(instance, lldecl);
113
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),
119         || {
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?)",
123                     instance)
124         });
125     match map_node {
126         hir_map::NodeItem(&hir::Item {
127             ref attrs, node: hir::ItemFn(ref decl, _, _, _, _, ref body), ..
128         }) |
129         hir_map::NodeTraitItem(&hir::TraitItem {
130             ref attrs, node: hir::MethodTraitItem(
131                 hir::MethodSig { ref decl, .. }, Some(ref body)), ..
132         }) |
133         hir_map::NodeImplItem(&hir::ImplItem {
134             ref attrs, node: hir::ImplItemKind::Method(
135                 hir::MethodSig { ref decl, .. }, ref body), ..
136         }) => {
137             base::update_linkage(ccx, lldecl, None, base::OriginalTranslation);
138             attributes::from_fn_attrs(ccx, attrs, lldecl);
139
140             let is_first = !ccx.available_monomorphizations().borrow().contains(&s);
141             if is_first {
142                 ccx.available_monomorphizations().borrow_mut().insert(s.clone());
143             }
144
145             let trans_everywhere = attr::requests_inline(attrs);
146             if trans_everywhere && !is_first {
147                 llvm::SetLinkage(lldecl, llvm::AvailableExternallyLinkage);
148             }
149
150             if trans_everywhere || is_first {
151                 trans_fn(ccx, decl, body, lldecl, psubsts, fn_node_id);
152             }
153         }
154
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)
159                 }
160                 hir_map::NodeStructCtor(_) => Disr(0),
161                 _ => unreachable!()
162             };
163             attributes::inline(lldecl, attributes::InlineAttr::Hint);
164             base::trans_ctor_shim(ccx, fn_node_id, disr, psubsts, lldecl);
165         }
166
167         _ => unreachable!("can't monomorphize a {:?}", map_node)
168     };
169
170     ccx.monomorphizing().borrow_mut().insert(fn_id, depth);
171
172     debug!("leaving monomorphic fn {}", ccx.tcx().item_path_str(fn_id));
173     (lldecl, mono_ty)
174 }
175
176 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
177 pub struct Instance<'tcx> {
178     pub def: DefId,
179     pub substs: &'tcx Substs<'tcx>,
180 }
181
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)
186     }
187 }
188
189 impl<'tcx> Instance<'tcx> {
190     pub fn new(def_id: DefId, substs: &'tcx Substs<'tcx>)
191                -> Instance<'tcx> {
192         assert!(substs.regions.iter().all(|&r| r == ty::ReStatic));
193         Instance { def: def_id, substs: substs }
194     }
195     pub fn mono(tcx: &TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> {
196         Instance::new(def_id, &tcx.mk_substs(Substs::empty()))
197     }
198 }
199
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>,
204                                   value: &T)
205                                   -> T
206     where T : TypeFoldable<'tcx>
207 {
208     let substituted = value.subst(tcx, param_substs);
209     normalize_associated_type(tcx, &substituted)
210 }
211
212
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>)
217                       -> Ty<'tcx>
218 {
219     normalize_associated_type(tcx, &f.ty(tcx, param_substs))
220 }