summary refs log tree commit diff
path: root/src/rustc/middle/trans/monomorphize.rs
blob: 17eaf591c9f3469aca2c614e27260b07b50e784d (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
use common::*;
use syntax::ast;
use syntax::ast_util::local_def;
use syntax::ast_map::{path, path_mod, path_name};
use base::{trans_item, get_item_val, no_self, self_arg, trans_fn,
              impl_self, decl_internal_cdecl_fn,
              set_inline_hint_if_appr, set_inline_hint,
              trans_enum_variant, trans_class_ctor, trans_class_dtor,
              get_insn_ctxt};
use syntax::parse::token::special_idents;
use type_of::type_of_fn_from_ty;
use back::link::mangle_exported_name;
use middle::ty::{FnTyBase, FnMeta, FnSig};

fn monomorphic_fn(ccx: @crate_ctxt,
                  fn_id: ast::def_id,
                  real_substs: ~[ty::t],
                  vtables: Option<typeck::vtable_res>,
                  ref_id: Option<ast::node_id>)
    -> {val: ValueRef, must_cast: bool}
{
    let _icx = ccx.insn_ctxt("monomorphic_fn");
    let mut must_cast = false;
    let substs = vec::map(real_substs, |t| {
        match normalize_for_monomorphization(ccx.tcx, *t) {
          Some(t) => { must_cast = true; t }
          None => *t
        }
    });

    for real_substs.each() |s| { assert !ty::type_has_params(*s); }
    for substs.each() |s| { assert !ty::type_has_params(*s); }
    let param_uses = type_use::type_uses_for(ccx, fn_id, substs.len());
    let hash_id = make_mono_id(ccx, fn_id, substs, vtables, Some(param_uses));
    if vec::any(hash_id.params,
                |p| match *p { mono_precise(_, _) => false, _ => true }) {
        must_cast = true;
    }

    #debug["monomorphic_fn(fn_id=%? (%s), real_substs=%?, substs=%?, \
           hash_id = %?",
           fn_id, ty::item_path_str(ccx.tcx, fn_id),
           real_substs.map(|s| ty_to_str(ccx.tcx, *s)),
           substs.map(|s| ty_to_str(ccx.tcx, *s)), hash_id];

    match ccx.monomorphized.find(hash_id) {
      Some(val) => {
        debug!("leaving monomorphic fn %s",
               ty::item_path_str(ccx.tcx, fn_id));
        return {val: val, must_cast: must_cast};
      }
      None => ()
    }

    let tpt = ty::lookup_item_type(ccx.tcx, fn_id);
    let mut llitem_ty = tpt.ty;

    let map_node = session::expect(ccx.sess, ccx.tcx.items.find(fn_id.node),
     || fmt!("While monomorphizing %?, couldn't find it in the item map \
        (may have attempted to monomorphize an item defined in a different \
        crate?)", fn_id));
    // Get the path so that we can create a symbol
    let (pt, name, span) = match map_node {
      ast_map::node_item(i, pt) => (pt, i.ident, i.span),
      ast_map::node_variant(v, enm, pt) => (pt, v.node.name, enm.span),
      ast_map::node_method(m, _, pt) => (pt, m.ident, m.span),
      ast_map::node_foreign_item(i, ast::foreign_abi_rust_intrinsic, pt)
      => (pt, i.ident, i.span),
      ast_map::node_foreign_item(*) => {
        // Foreign externs don't have to be monomorphized.
        return {val: get_item_val(ccx, fn_id.node),
                must_cast: true};
      }
      ast_map::node_ctor(nm, _, ct, _, pt) => (pt, nm, ct.span),
      ast_map::node_dtor(_, dtor, _, pt) =>
          (pt, special_idents::dtor, dtor.span),
      ast_map::node_trait_method(*) => {
        ccx.tcx.sess.bug(~"Can't monomorphize a trait method")
      }
      ast_map::node_expr(*) => {
        ccx.tcx.sess.bug(~"Can't monomorphize an expr")
      }
      ast_map::node_stmt(*) => {
        ccx.tcx.sess.bug(~"Can't monomorphize a stmt")
      }
      ast_map::node_export(*) => {
          ccx.tcx.sess.bug(~"Can't monomorphize an export")
      }
      ast_map::node_arg(*) => ccx.tcx.sess.bug(~"Can't monomorphize an arg"),
      ast_map::node_block(*) => {
          ccx.tcx.sess.bug(~"Can't monomorphize a block")
      }
      ast_map::node_local(*) => {
          ccx.tcx.sess.bug(~"Can't monomorphize a local")
      }
    };
    let mono_ty = ty::subst_tps(ccx.tcx, substs, llitem_ty);
    let llfty = type_of_fn_from_ty(ccx, mono_ty);

    ccx.stats.n_monos += 1;

    let depth = option::get_default(&ccx.monomorphizing.find(fn_id), 0u);
    // Random cut-off -- code that needs to instantiate the same function
    // recursively more than ten times can probably safely be assumed to be
    // causing an infinite expansion.
    if depth > 10 {
        ccx.sess.span_fatal(
            span, ~"overly deep expansion of inlined function");
    }
    ccx.monomorphizing.insert(fn_id, depth + 1);

    let pt = vec::append(*pt,
                         ~[path_name(ccx.names(ccx.sess.str_of(name)))]);
    let s = mangle_exported_name(ccx, pt, mono_ty);

    let mk_lldecl = || {
        let lldecl = decl_internal_cdecl_fn(ccx.llmod, s, llfty);
        ccx.monomorphized.insert(hash_id, lldecl);
        lldecl
    };

    let psubsts = Some({tys: substs, vtables: vtables, bounds: tpt.bounds});
    let lldecl = match map_node {
      ast_map::node_item(i@@{node: ast::item_fn(decl, _, _, body), _}, _) => {
        let d = mk_lldecl();
        set_inline_hint_if_appr(i.attrs, d);
        trans_fn(ccx, pt, decl, body, d, no_self, psubsts, fn_id.node);
        d
      }
      ast_map::node_item(*) => {
          ccx.tcx.sess.bug(~"Can't monomorphize this kind of item")
      }
      ast_map::node_foreign_item(i, _, _) => {
          let d = mk_lldecl();
          foreign::trans_intrinsic(ccx, d, i, pt, psubsts.get(),
                                ref_id);
          d
      }
      ast_map::node_variant(v, enum_item, _) => {
        let tvs = ty::enum_variants(ccx.tcx, local_def(enum_item.id));
        let this_tv = option::get(&vec::find(*tvs, |tv| {
            tv.id.node == fn_id.node}));
        let d = mk_lldecl();
        set_inline_hint(d);
        match v.node.kind {
            ast::tuple_variant_kind(args) => {
                trans_enum_variant(ccx, enum_item.id, v, args,
                                   this_tv.disr_val, (*tvs).len() == 1u,
                                   psubsts, d);
            }
            ast::struct_variant_kind(_) =>
                ccx.tcx.sess.bug(~"can't monomorphize struct variants"),
            ast::enum_variant_kind(_) =>
                ccx.tcx.sess.bug(~"can't monomorphize enum variants")
        }
        d
      }
      ast_map::node_method(mth, _, _) => {
        // XXX: What should the self type be here?
        let d = mk_lldecl();
        set_inline_hint_if_appr(mth.attrs, d);
        meth::trans_method(ccx, pt, mth, psubsts, None, d);
        d
      }
      ast_map::node_ctor(_, tps, ctor, parent_id, _) => {
        // ctors don't have attrs, at least not right now
        let d = mk_lldecl();
        let tp_tys = ty::ty_params_to_tys(ccx.tcx, tps);
        trans_class_ctor(ccx, pt, ctor.node.dec, ctor.node.body, d,
               option::get_default(&psubsts,
                        {tys:tp_tys, vtables: None, bounds: @~[]}),
                         fn_id.node, parent_id, ctor.span);
        d
      }
      ast_map::node_dtor(_, dtor, _, pt) => {
        let parent_id = match ty::ty_to_def_id(ty::node_id_to_type(ccx.tcx,
                                              dtor.node.self_id)) {
                Some(did) => did,
                None      => ccx.sess.span_bug(dtor.span, ~"Bad self ty in \
                                                            dtor")
        };
        trans_class_dtor(ccx, *pt, dtor.node.body,
          dtor.node.id, psubsts, Some(hash_id), parent_id)
      }
      // Ugh -- but this ensures any new variants won't be forgotten
      ast_map::node_expr(*) |
      ast_map::node_stmt(*) |
      ast_map::node_trait_method(*) |
      ast_map::node_export(*) |
      ast_map::node_arg(*) |
      ast_map::node_block(*) |
      ast_map::node_local(*) => {
        ccx.tcx.sess.bug(fmt!("Can't monomorphize a %?", map_node))
      }
    };
    ccx.monomorphizing.insert(fn_id, depth);

    debug!("leaving monomorphic fn %s", ty::item_path_str(ccx.tcx, fn_id));
    {val: lldecl, must_cast: must_cast}
}

fn normalize_for_monomorphization(tcx: ty::ctxt, ty: ty::t) -> Option<ty::t> {
    // FIXME[mono] could do this recursively. is that worthwhile? (#2529)
    match ty::get(ty).sty {
        ty::ty_box(*) => {
            Some(ty::mk_opaque_box(tcx))
        }
        ty::ty_fn(ref fty) => {
            Some(ty::mk_fn(
                tcx,
                FnTyBase {meta: FnMeta {purity: ast::impure_fn,
                                        proto: fty.meta.proto,
                                        bounds: @~[],
                                        ret_style: ast::return_val},
                          sig: FnSig {inputs: ~[],
                                      output: ty::mk_nil(tcx)}}))
        }
        ty::ty_trait(_, _, _) => {
            let box_proto = ty::proto_vstore(ty::vstore_box);
            Some(ty::mk_fn(
                tcx,
                FnTyBase {meta: FnMeta {purity: ast::impure_fn,
                                        proto: box_proto,
                                        bounds: @~[],
                                        ret_style: ast::return_val},
                          sig: FnSig {inputs: ~[],
                                      output: ty::mk_nil(tcx)}}))
        }
        ty::ty_ptr(_) => {
            Some(ty::mk_uint(tcx))
        }
        _ => {
            None
        }
    }
}

fn make_mono_id(ccx: @crate_ctxt, item: ast::def_id, substs: ~[ty::t],
                vtables: Option<typeck::vtable_res>,
                param_uses: Option<~[type_use::type_uses]>) -> mono_id {
    let precise_param_ids = match vtables {
      Some(vts) => {
        let bounds = ty::lookup_item_type(ccx.tcx, item).bounds;
        let mut i = 0u;
        vec::map2(*bounds, substs, |bounds, subst| {
            let mut v = ~[];
            for bounds.each |bound| {
                match *bound {
                  ty::bound_trait(_) => {
                    v.push(meth::vtable_id(ccx, vts[i]));
                    i += 1u;
                  }
                  _ => ()
                }
            }
            (*subst, if v.len() > 0u { Some(v) } else { None })
        })
      }
      None => {
        vec::map(substs, |subst| (*subst, None))
      }
    };
    let param_ids = match param_uses {
      Some(uses) => {
        vec::map2(precise_param_ids, uses, |id, uses| {
            match *id {
                (a, b@Some(_)) => mono_precise(a, b),
                (subst, None) => {
                    if *uses == 0u {
                        mono_any
                    } else if *uses == type_use::use_repr &&
                        !ty::type_needs_drop(ccx.tcx, subst)
                    {
                        let llty = type_of::type_of(ccx, subst);
                        let size = shape::llsize_of_real(ccx, llty);
                        let align = shape::llalign_of_pref(ccx, llty);
                        let mode = datum::appropriate_mode(subst);

                        // FIXME(#3547)---scalars and floats are
                        // treated differently in most ABIs.  But we
                        // should be doing something more detailed
                        // here.
                        let is_float = match ty::get(subst).sty {
                            ty::ty_float(_) => true,
                            _ => false
                        };

                        // Special value for nil to prevent problems
                        // with undef return pointers.
                        if size == 1u && ty::type_is_nil(subst) {
                            mono_repr(0u, 0u, is_float, mode)
                        } else {
                            mono_repr(size, align, is_float, mode)
                        }
                    } else {
                        mono_precise(subst, None)
                    }
                }
            }
        })
      }
      None => {
          precise_param_ids.map(|x| {
              let (a, b) = *x;
              mono_precise(a, b)
          })
      }
    };
    @{def: item, params: param_ids}
}