summaryrefslogtreecommitdiff
path: root/src/librustc_trans/trans/monomorphize.rs
blob: c6119416e47eda2fffee413336498b212bc2d42d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use back::link::exported_name;
use llvm::ValueRef;
use llvm;
use middle::def_id::DefId;
use middle::infer::normalize_associated_type;
use middle::subst;
use middle::subst::{Subst, Substs};
use middle::ty::fold::{TypeFolder, TypeFoldable};
use trans::attributes;
use trans::base::{trans_enum_variant, push_ctxt, get_item_val};
use trans::base::trans_fn;
use trans::base;
use trans::common::*;
use trans::declare;
use trans::foreign;
use middle::ty::{self, Ty, TyCtxt};
use trans::Disr;
use rustc::front::map as hir_map;

use rustc_front::hir;

use syntax::abi::Abi;
use syntax::ast;
use syntax::attr;
use syntax::errors;
use std::hash::{Hasher, Hash, SipHasher};

pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
                                fn_id: DefId,
                                psubsts: &'tcx subst::Substs<'tcx>)
                                -> (ValueRef, Ty<'tcx>, bool) {
    debug!("monomorphic_fn(fn_id={:?}, real_substs={:?})", fn_id, psubsts);

    assert!(!psubsts.types.needs_infer() && !psubsts.types.has_param_types());

    // we can only monomorphize things in this crate (or inlined into it)
    let fn_node_id = ccx.tcx().map.as_local_node_id(fn_id).unwrap();

    let _icx = push_ctxt("monomorphic_fn");

    let hash_id = MonoId {
        def: fn_id,
        params: &psubsts.types
    };

    let item_ty = ccx.tcx().lookup_item_type(fn_id).ty;

    debug!("monomorphic_fn about to subst into {:?}", item_ty);
    let mono_ty = apply_param_substs(ccx.tcx(), psubsts, &item_ty);
    debug!("mono_ty = {:?} (post-substitution)", mono_ty);

    match ccx.monomorphized().borrow().get(&hash_id) {
        Some(&val) => {
            debug!("leaving monomorphic fn {}",
            ccx.tcx().item_path_str(fn_id));
            return (val, mono_ty, false);
        }
        None => ()
    }

    debug!("monomorphic_fn(\
            fn_id={:?}, \
            psubsts={:?}, \
            hash_id={:?})",
           fn_id,
           psubsts,
           hash_id);


    let map_node = errors::expect(
        ccx.sess().diagnostic(),
        ccx.tcx().map.find(fn_node_id),
        || {
            format!("while monomorphizing {:?}, couldn't find it in \
                     the item map (may have attempted to monomorphize \
                     an item defined in a different crate?)",
                    fn_id)
        });

    if let hir_map::NodeForeignItem(_) = map_node {
        let abi = ccx.tcx().map.get_foreign_abi(fn_node_id);
        if abi != Abi::RustIntrinsic && abi != Abi::PlatformIntrinsic {
            // Foreign externs don't have to be monomorphized.
            return (get_item_val(ccx, fn_node_id), mono_ty, true);
        }
    }

    ccx.stats().n_monos.set(ccx.stats().n_monos.get() + 1);

    let depth;
    {
        let mut monomorphizing = ccx.monomorphizing().borrow_mut();
        depth = match monomorphizing.get(&fn_id) {
            Some(&d) => d, None => 0
        };

        debug!("monomorphic_fn: depth for fn_id={:?} is {:?}", fn_id, depth+1);

        // Random cut-off -- code that needs to instantiate the same function
        // recursively more than thirty times can probably safely be assumed
        // to be causing an infinite expansion.
        if depth > ccx.sess().recursion_limit.get() {
            ccx.sess().span_fatal(ccx.tcx().map.span(fn_node_id),
                "reached the recursion limit during monomorphization");
        }

        monomorphizing.insert(fn_id, depth + 1);
    }

    let hash;
    let s = {
        let mut state = SipHasher::new();
        hash_id.hash(&mut state);
        mono_ty.hash(&mut state);

        hash = format!("h{}", state.finish());
        let path = ccx.tcx().map.def_path_from_id(fn_node_id);
        exported_name(path, &hash[..])
    };

    debug!("monomorphize_fn mangled to {}", s);

    // This shouldn't need to option dance.
    let mut hash_id = Some(hash_id);
    let mut mk_lldecl = |abi: Abi| {
        let lldecl = if abi != Abi::Rust {
            foreign::decl_rust_fn_with_foreign_abi(ccx, mono_ty, &s)
        } else {
            // FIXME(nagisa): perhaps needs a more fine grained selection? See
            // setup_lldecl below.
            declare::define_internal_rust_fn(ccx, &s, mono_ty)
        };

        ccx.monomorphized().borrow_mut().insert(hash_id.take().unwrap(), lldecl);
        lldecl
    };
    let setup_lldecl = |lldecl, attrs: &[ast::Attribute]| {
        base::update_linkage(ccx, lldecl, None, base::OriginalTranslation);
        attributes::from_fn_attrs(ccx, attrs, lldecl);

        let is_first = !ccx.available_monomorphizations().borrow().contains(&s);
        if is_first {
            ccx.available_monomorphizations().borrow_mut().insert(s.clone());
        }

        let trans_everywhere = attr::requests_inline(attrs);
        if trans_everywhere && !is_first {
            llvm::SetLinkage(lldecl, llvm::AvailableExternallyLinkage);
        }

        // If `true`, then `lldecl` should be given a function body.
        // Otherwise, it should be left as a declaration of an external
        // function, with no definition in the current compilation unit.
        trans_everywhere || is_first
    };

    let lldecl = match map_node {
        hir_map::NodeItem(i) => {
            match *i {
              hir::Item {
                  node: hir::ItemFn(ref decl, _, _, abi, _, ref body),
                  ..
              } => {
                  let d = mk_lldecl(abi);
                  let needs_body = setup_lldecl(d, &i.attrs);
                  if needs_body {
                      if abi != Abi::Rust {
                          foreign::trans_rust_fn_with_foreign_abi(
                              ccx, &decl, &body, &[], d, psubsts, fn_node_id,
                              Some(&hash[..]));
                      } else {
                          trans_fn(ccx,
                                   &decl,
                                   &body,
                                   d,
                                   psubsts,
                                   fn_node_id,
                                   &i.attrs);
                      }
                  }

                  d
              }
              _ => {
                ccx.sess().bug("Can't monomorphize this kind of item")
              }
            }
        }
        hir_map::NodeVariant(v) => {
            let variant = inlined_variant_def(ccx, fn_node_id);
            assert_eq!(v.node.name, variant.name);
            let d = mk_lldecl(Abi::Rust);
            attributes::inline(d, attributes::InlineAttr::Hint);
            trans_enum_variant(ccx, fn_node_id, Disr::from(variant.disr_val), psubsts, d);
            d
        }
        hir_map::NodeImplItem(impl_item) => {
            match impl_item.node {
                hir::ImplItemKind::Method(ref sig, ref body) => {
                    let d = mk_lldecl(Abi::Rust);
                    let needs_body = setup_lldecl(d, &impl_item.attrs);
                    if needs_body {
                        trans_fn(ccx,
                                 &sig.decl,
                                 body,
                                 d,
                                 psubsts,
                                 impl_item.id,
                                 &impl_item.attrs);
                    }
                    d
                }
                _ => {
                    ccx.sess().bug(&format!("can't monomorphize a {:?}",
                                           map_node))
                }
            }
        }
        hir_map::NodeTraitItem(trait_item) => {
            match trait_item.node {
                hir::MethodTraitItem(ref sig, Some(ref body)) => {
                    let d = mk_lldecl(Abi::Rust);
                    let needs_body = setup_lldecl(d, &trait_item.attrs);
                    if needs_body {
                        trans_fn(ccx,
                                 &sig.decl,
                                 body,
                                 d,
                                 psubsts,
                                 trait_item.id,
                                 &trait_item.attrs);
                    }
                    d
                }
                _ => {
                    ccx.sess().bug(&format!("can't monomorphize a {:?}",
                                           map_node))
                }
            }
        }
        hir_map::NodeStructCtor(struct_def) => {
            let d = mk_lldecl(Abi::Rust);
            attributes::inline(d, attributes::InlineAttr::Hint);
            if struct_def.is_struct() {
                panic!("ast-mapped struct didn't have a ctor id")
            }
            base::trans_tuple_struct(ccx,
                                     struct_def.id(),
                                     psubsts,
                                     d);
            d
        }

        // Ugh -- but this ensures any new variants won't be forgotten
        hir_map::NodeForeignItem(..) |
        hir_map::NodeLifetime(..) |
        hir_map::NodeTyParam(..) |
        hir_map::NodeExpr(..) |
        hir_map::NodeStmt(..) |
        hir_map::NodeBlock(..) |
        hir_map::NodePat(..) |
        hir_map::NodeLocal(..) => {
            ccx.sess().bug(&format!("can't monomorphize a {:?}",
                                   map_node))
        }
    };

    ccx.monomorphizing().borrow_mut().insert(fn_id, depth);

    debug!("leaving monomorphic fn {}", ccx.tcx().item_path_str(fn_id));
    (lldecl, mono_ty, true)
}

#[derive(PartialEq, Eq, Hash, Debug)]
pub struct MonoId<'tcx> {
    pub def: DefId,
    pub params: &'tcx subst::VecPerParamSpace<Ty<'tcx>>
}

/// Monomorphizes a type from the AST by first applying the in-scope
/// substitutions and then normalizing any associated types.
pub fn apply_param_substs<'tcx,T>(tcx: &TyCtxt<'tcx>,
                                  param_substs: &Substs<'tcx>,
                                  value: &T)
                                  -> T
    where T : TypeFoldable<'tcx>
{
    let substituted = value.subst(tcx, param_substs);
    normalize_associated_type(tcx, &substituted)
}


/// Returns the normalized type of a struct field
pub fn field_ty<'tcx>(tcx: &TyCtxt<'tcx>,
                      param_substs: &Substs<'tcx>,
                      f: ty::FieldDef<'tcx>)
                      -> Ty<'tcx>
{
    normalize_associated_type(tcx, &f.ty(tcx, param_substs))
}