]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans/monomorphize.rs
Use ast attributes every where (remove HIR attributes).
[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 session;
13 use llvm::ValueRef;
14 use llvm;
15 use middle::def_id::DefId;
16 use middle::infer::normalize_associated_type;
17 use middle::subst;
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;
23 use trans::base;
24 use trans::common::*;
25 use trans::declare;
26 use trans::foreign;
27 use middle::ty::{self, HasTypeFlags, Ty};
28 use rustc::front::map as hir_map;
29
30 use rustc_front::hir;
31
32 use syntax::abi;
33 use syntax::ast;
34 use syntax::attr;
35 use std::hash::{Hasher, Hash, SipHasher};
36
37 pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
38                                 fn_id: DefId,
39                                 psubsts: &'tcx subst::Substs<'tcx>,
40                                 ref_id: Option<ast::NodeId>)
41     -> (ValueRef, Ty<'tcx>, bool) {
42     debug!("monomorphic_fn(\
43             fn_id={:?}, \
44             real_substs={:?}, \
45             ref_id={:?})",
46            fn_id,
47            psubsts,
48            ref_id);
49
50     assert!(!psubsts.types.needs_infer() && !psubsts.types.has_param_types());
51
52     let _icx = push_ctxt("monomorphic_fn");
53
54     let hash_id = MonoId {
55         def: fn_id,
56         params: &psubsts.types
57     };
58
59     let item_ty = ccx.tcx().lookup_item_type(fn_id).ty;
60
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);
64
65     match ccx.monomorphized().borrow().get(&hash_id) {
66         Some(&val) => {
67             debug!("leaving monomorphic fn {}",
68             ccx.tcx().item_path_str(fn_id));
69             return (val, mono_ty, false);
70         }
71         None => ()
72     }
73
74     debug!("monomorphic_fn(\
75             fn_id={:?}, \
76             psubsts={:?}, \
77             hash_id={:?})",
78            fn_id,
79            psubsts,
80            hash_id);
81
82
83     let map_node = session::expect(
84         ccx.sess(),
85         ccx.tcx().map.find(fn_id.node),
86         || {
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?)",
90                     fn_id)
91         });
92
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);
98         }
99     }
100
101     ccx.stats().n_monos.set(ccx.stats().n_monos.get() + 1);
102
103     let depth;
104     {
105         let mut monomorphizing = ccx.monomorphizing().borrow_mut();
106         depth = match monomorphizing.get(&fn_id) {
107             Some(&d) => d, None => 0
108         };
109
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");
116         }
117
118         monomorphizing.insert(fn_id, depth + 1);
119     }
120
121     let hash;
122     let s = {
123         let mut state = SipHasher::new();
124         hash_id.hash(&mut state);
125         mono_ty.hash(&mut state);
126
127         hash = format!("h{}", state.finish());
128         ccx.tcx().map.with_path(fn_id.node, |path| {
129             exported_name(path, &hash[..])
130         })
131     };
132
133     debug!("monomorphize_fn mangled to {}", s);
134
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[..])
140         } else {
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)
144         };
145
146         ccx.monomorphized().borrow_mut().insert(hash_id.take().unwrap(), lldecl);
147         lldecl
148     };
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);
152
153         let is_first = !ccx.available_monomorphizations().borrow().contains(&s);
154         if is_first {
155             ccx.available_monomorphizations().borrow_mut().insert(s.clone());
156         }
157
158         let trans_everywhere = attr::requests_inline(attrs);
159         if trans_everywhere && !is_first {
160             llvm::SetLinkage(lldecl, llvm::AvailableExternallyLinkage);
161         }
162
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
167     };
168
169     let lldecl = match map_node {
170         hir_map::NodeItem(i) => {
171             match *i {
172               hir::Item {
173                   node: hir::ItemFn(ref decl, _, _, abi, _, ref body),
174                   ..
175               } => {
176                   let d = mk_lldecl(abi);
177                   let needs_body = setup_lldecl(d, &i.attrs);
178                   if needs_body {
179                       if abi != abi::Rust {
180                           foreign::trans_rust_fn_with_foreign_abi(
181                               ccx, &**decl, &**body, &[], d, psubsts, fn_id.node,
182                               Some(&hash[..]));
183                       } else {
184                           trans_fn(ccx, &**decl, &**body, d, psubsts, fn_id.node, &[]);
185                       }
186                   }
187
188                   d
189               }
190               _ => {
191                 ccx.sess().bug("Can't monomorphize this kind of item")
192               }
193             }
194         }
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);
201             d
202         }
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);
208                     if needs_body {
209                         trans_fn(ccx,
210                                  &sig.decl,
211                                  body,
212                                  d,
213                                  psubsts,
214                                  impl_item.id,
215                                  &[]);
216                     }
217                     d
218                 }
219                 _ => {
220                     ccx.sess().bug(&format!("can't monomorphize a {:?}",
221                                            map_node))
222                 }
223             }
224         }
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);
230                     if needs_body {
231                         trans_fn(ccx, &sig.decl, body, d,
232                                  psubsts, trait_item.id, &[]);
233                     }
234                     d
235                 }
236                 _ => {
237                     ccx.sess().bug(&format!("can't monomorphize a {:?}",
238                                            map_node))
239                 }
240             }
241         }
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"),
248                                      psubsts,
249                                      d);
250             d
251         }
252
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 {:?}",
264                                    map_node))
265         }
266     };
267
268     ccx.monomorphizing().borrow_mut().insert(fn_id, depth);
269
270     debug!("leaving monomorphic fn {}", ccx.tcx().item_path_str(fn_id));
271     (lldecl, mono_ty, true)
272 }
273
274 #[derive(PartialEq, Eq, Hash, Debug)]
275 pub struct MonoId<'tcx> {
276     pub def: DefId,
277     pub params: &'tcx subst::VecPerParamSpace<Ty<'tcx>>
278 }
279
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>,
284                                   value: &T)
285                                   -> T
286     where T : TypeFoldable<'tcx> + HasTypeFlags
287 {
288     let substituted = value.subst(tcx, param_substs);
289     normalize_associated_type(tcx, &substituted)
290 }
291
292
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>)
297                       -> Ty<'tcx>
298 {
299     normalize_associated_type(tcx, &f.ty(tcx, param_substs))
300 }