]> git.lizzy.rs Git - rust.git/blob - src/comp/middle/trans_impl.rs
Add some basic comments to trans_imp.rs
[rust.git] / src / comp / middle / trans_impl.rs
1 import trans::*;
2 import trans_common::*;
3 import trans_build::*;
4 import option::{some, none};
5 import syntax::{ast, ast_util};
6 import metadata::csearch;
7 import back::link;
8 import lib::llvm::llvm;
9 import llvm::{ValueRef, TypeRef, LLVMGetParam};
10
11 // Translation functionality related to impls and ifaces
12 //
13 // Terminology:
14 //  vtable:  a table of function pointers pointing to method wrappers
15 //           of an impl that implements an iface
16 //  dict:    a record containing a vtable pointer along with pointers to
17 //           all tydescs and other dicts needed to run methods in this vtable
18 //           (i.e. corresponding to the type parameters of the impl)
19 //  wrapper: a function that takes a dict as first argument, along
20 //           with the method-specific tydescs for a method (and all
21 //           other args the method expects), which fetches the extra
22 //           tydescs and dicts from the dict, splices them into the
23 //           arglist, and calls through to the actual method
24 //
25 // Generic functions take, along with their normal arguments, a number
26 // of extra tydesc and dict arguments -- one tydesc for each type
27 // parameter, one dict (following the tydesc in the arg order) for
28 // each interface bound on a type parameter.
29 //
30 // Most dicts are completely static, and are allocated and filled at
31 // compile time. Dicts that depend on run-time values (tydescs or
32 // dicts for type parameter types) are built at run-time, and interned
33 // through upcall_intern_dict in the runtime. This means that dict
34 // pointers are self-contained things that do not need to be cleaned
35 // up.
36 //
37 // The trans_constants pass in trans.rs outputs the vtables. Typeck
38 // annotates notes with information about the methods and dicts that
39 // are referenced (ccx.method_map and ccx.dict_map).
40
41 fn trans_impl(cx: @local_ctxt, name: ast::ident, methods: [@ast::method],
42               id: ast::node_id, tps: [ast::ty_param]) {
43     let sub_cx = extend_path(cx, name);
44     for m in methods {
45         alt cx.ccx.item_ids.find(m.id) {
46           some(llfn) {
47             trans_fn(extend_path(sub_cx, m.ident), m.span, m.decl, m.body,
48                      llfn, impl_self(ty::node_id_to_monotype(cx.ccx.tcx, id)),
49                      tps + m.tps, m.id);
50           }
51         }
52     }
53 }
54
55 fn trans_self_arg(bcx: @block_ctxt, base: @ast::expr) -> result {
56     let tz = [], tr = [];
57     let basety = ty::expr_ty(bcx_tcx(bcx), base);
58     let {bcx, val} = trans_arg_expr(bcx, {mode: ast::by_ref, ty: basety},
59                                     T_ptr(type_of_or_i8(bcx, basety)), tz,
60                                     tr, base);
61     rslt(bcx, PointerCast(bcx, val, T_opaque_boxed_closure_ptr(bcx_ccx(bcx))))
62 }
63
64 fn trans_static_callee(bcx: @block_ctxt, e: @ast::expr, base: @ast::expr,
65                        did: ast::def_id) -> lval_maybe_callee {
66     let {bcx, val} = trans_self_arg(bcx, base);
67     {env: obj_env(val) with lval_static_fn(bcx, did, e.id)}
68 }
69
70 fn trans_dict_callee(bcx: @block_ctxt, e: @ast::expr, base: @ast::expr,
71                      iface_id: ast::def_id, n_method: uint,
72                      n_param: uint, n_bound: uint) -> lval_maybe_callee {
73     let tcx = bcx_tcx(bcx);
74     let {bcx, val} = trans_self_arg(bcx, base);
75     let dict = option::get(bcx.fcx.lltyparams[n_param].dicts)[n_bound];
76     let method = ty::iface_methods(tcx, iface_id)[n_method];
77     let fty = ty::expr_ty(tcx, e);
78     let bare_fn_ty = type_of_fn_from_ty(bcx_ccx(bcx), ast_util::dummy_sp(),
79                                         fty, *method.tps);
80     let {inputs: bare_inputs, output} = llfn_arg_tys(bare_fn_ty);
81     let fn_ty = T_fn([val_ty(dict)] + bare_inputs, output);
82     let vtable = PointerCast(bcx, Load(bcx, GEPi(bcx, dict, [0, 0])),
83                              T_ptr(T_array(T_ptr(fn_ty), n_method + 1u)));
84     let mptr = Load(bcx, GEPi(bcx, vtable, [0, n_method as int]));
85     let generic = none;
86     if vec::len(*method.tps) > 0u {
87         let tydescs = [], tis = [];
88         let tptys = ty::node_id_to_type_params(tcx, e.id);
89         for t in vec::tail_n(tptys, vec::len(tptys) - vec::len(*method.tps)) {
90             // TODO: Doesn't always escape.
91             let ti = none;
92             let td = get_tydesc(bcx, t, true, tps_normal, ti).result;
93             tis += [ti];
94             tydescs += [td.val];
95             bcx = td.bcx;
96         }
97         generic = some({item_type: fty,
98                         static_tis: tis,
99                         tydescs: tydescs,
100                         param_bounds: method.tps,
101                         origins: bcx_ccx(bcx).dict_map.find(e.id)});
102     }
103     {bcx: bcx, val: mptr, kind: owned,
104      env: dict_env(dict, val),
105      generic: generic}
106 }
107
108 fn llfn_arg_tys(ft: TypeRef) -> {inputs: [TypeRef], output: TypeRef} {
109     let out_ty = llvm::LLVMGetReturnType(ft);
110     let n_args = llvm::LLVMCountParamTypes(ft);
111     let args = vec::init_elt(0 as TypeRef, n_args);
112     unsafe { llvm::LLVMGetParamTypes(ft, vec::to_ptr(args)); }
113     {inputs: args, output: out_ty}
114 }
115
116 fn trans_wrapper(ccx: @crate_ctxt, pt: [ast::ident],
117                  extra_tps: [ty::param_bounds], m: @ast::method) -> ValueRef {
118     let real_fn = ccx.item_ids.get(m.id);
119     let {inputs: real_args, output: real_ret} =
120         llfn_arg_tys(llvm::LLVMGetElementType(val_ty(real_fn)));
121     let extra_ptrs = [];
122     for tp in extra_tps {
123         extra_ptrs += [T_ptr(ccx.tydesc_type)];
124         for bound in *tp {
125             alt bound {
126               ty::bound_iface(_) { extra_ptrs += [T_ptr(T_dict())]; }
127               _ {}
128             }
129         }
130     }
131     let env_ty = T_ptr(T_struct([T_ptr(T_i8())] + extra_ptrs));
132     let n_extra_ptrs = vec::len(extra_ptrs);
133
134     let wrap_args = [T_ptr(T_dict())] + vec::slice(real_args, 0u, 2u) +
135         vec::slice(real_args, 2u + vec::len(extra_ptrs), vec::len(real_args));
136     let llfn_ty = T_fn(wrap_args, real_ret);
137
138     let lcx = @{path: pt + ["wrapper", m.ident], module_path: [],
139                 obj_typarams: [], obj_fields: [], ccx: ccx};
140     let name = link::mangle_internal_name_by_path_and_seq(ccx, pt, m.ident);
141     let llfn = decl_internal_cdecl_fn(ccx.llmod, name, llfn_ty);
142     let fcx = new_fn_ctxt(lcx, ast_util::dummy_sp(), llfn);
143     let bcx = new_top_block_ctxt(fcx), lltop = bcx.llbb;
144
145     let dict = PointerCast(bcx, LLVMGetParam(llfn, 0u), env_ty);
146     // retptr, self
147     let args = [LLVMGetParam(llfn, 1u), LLVMGetParam(llfn, 2u)], i = 0u;
148     // saved tydescs/dicts
149     while i < n_extra_ptrs {
150         i += 1u;
151         args += [load_inbounds(bcx, dict, [0, i as int])];
152     }
153     // the rest of the parameters
154     let i = 3u, params_total = llvm::LLVMCountParamTypes(llfn_ty);
155     while i < params_total {
156         args += [LLVMGetParam(llfn, i)];
157         i += 1u;
158     }
159     Call(bcx, ccx.item_ids.get(m.id), args);
160     build_return(bcx);
161     finish_fn(fcx, lltop);
162     ret llfn;
163 }
164
165 fn dict_is_static(tcx: ty::ctxt, origin: typeck::dict_origin) -> bool {
166     alt origin {
167       typeck::dict_static(_, ts, origs) {
168         vec::all(ts, {|t| !ty::type_contains_params(tcx, t)}) &&
169         vec::all(*origs, {|o| dict_is_static(tcx, o)})
170       }
171       typeck::dict_param(_, _) { false }
172     }
173 }
174
175 fn get_dict(bcx: @block_ctxt, origin: typeck::dict_origin) -> result {
176     let ccx = bcx_ccx(bcx);
177     alt origin {
178       typeck::dict_static(impl_did, tys, sub_origins) {
179         if dict_is_static(ccx.tcx, origin) {
180             ret rslt(bcx, get_static_dict(bcx, origin));
181         }
182         let {bcx, ptrs} = get_dict_ptrs(bcx, origin);
183         let pty = T_ptr(T_i8()), dict_ty = T_array(pty, vec::len(ptrs));
184         let dict = alloca(bcx, dict_ty), i = 0;
185         for ptr in ptrs {
186             Store(bcx, PointerCast(bcx, ptr, pty), GEPi(bcx, dict, [0, i]));
187             i += 1;
188         }
189         dict = Call(bcx, ccx.upcalls.intern_dict,
190                     [C_uint(ccx, vec::len(ptrs)),
191                      PointerCast(bcx, dict, T_ptr(T_dict()))]);
192         rslt(bcx, dict)
193       }
194       typeck::dict_param(n_param, n_bound) {
195         rslt(bcx, option::get(bcx.fcx.lltyparams[n_param].dicts)[n_bound])
196       }
197     }
198 }
199
200 fn dict_id(tcx: ty::ctxt, origin: typeck::dict_origin) -> dict_id {
201     alt origin {
202       typeck::dict_static(did, ts, origs) {
203         let d_params = [], orig = 0u;
204         if vec::len(ts) == 0u { ret @{impl_def: did, params: d_params}; }
205         let impl_params = ty::lookup_item_type(tcx, did).bounds;
206         vec::iter2(ts, *impl_params) {|t, bounds|
207             d_params += [dict_param_ty(t)];
208             for bound in *bounds {
209                 alt bound {
210                   ty::bound_iface(_) {
211                     d_params += [dict_param_dict(dict_id(tcx, origs[orig]))];
212                     orig += 1u;
213                   }
214                 }
215             }
216         }
217         @{impl_def: did, params: d_params}
218       }
219     }
220 }
221
222 fn get_static_dict(bcx: @block_ctxt, origin: typeck::dict_origin)
223     -> ValueRef {
224     let ccx = bcx_ccx(bcx);
225     let id = dict_id(ccx.tcx, origin);
226     alt ccx.dicts.find(id) {
227       some(d) { ret d; }
228       none. {}
229     }
230     let ptrs = C_struct(get_dict_ptrs(bcx, origin).ptrs);
231     let name = ccx.names.next("dict");
232     let gvar = str::as_buf(name, {|buf|
233         llvm::LLVMAddGlobal(ccx.llmod, val_ty(ptrs), buf)
234     });
235     llvm::LLVMSetGlobalConstant(gvar, lib::llvm::True);
236     llvm::LLVMSetInitializer(gvar, ptrs);
237     llvm::LLVMSetLinkage(gvar,
238                          lib::llvm::LLVMInternalLinkage as llvm::Linkage);
239     let cast = llvm::LLVMConstPointerCast(gvar, T_ptr(T_dict()));
240     ccx.dicts.insert(id, cast);
241     cast
242 }
243
244 fn get_dict_ptrs(bcx: @block_ctxt, origin: typeck::dict_origin)
245     -> {bcx: @block_ctxt, ptrs: [ValueRef]} {
246     let ccx = bcx_ccx(bcx);
247     alt origin {
248       typeck::dict_static(impl_did, tys, sub_origins) {
249         let vtable = if impl_did.crate == ast::local_crate {
250             ccx.item_ids.get(impl_did.node)
251         } else {
252             let name = csearch::get_symbol(ccx.sess.get_cstore(), impl_did);
253             get_extern_const(ccx.externs, ccx.llmod, name, T_ptr(T_i8()))
254         };
255         let impl_params = ty::lookup_item_type(ccx.tcx, impl_did).bounds;
256         let ptrs = [vtable], origin = 0u, ti = none, bcx = bcx;
257         vec::iter2(*impl_params, tys) {|param, ty|
258             let rslt = get_tydesc(bcx, ty, true, tps_normal, ti).result;
259             ptrs += [rslt.val];
260             bcx = rslt.bcx;
261             for bound in *param {
262                 alt bound {
263                   ty::bound_iface(_) {
264                     let res = get_dict(bcx, sub_origins[origin]);
265                     ptrs += [res.val];
266                     bcx = res.bcx;
267                     origin += 1u;
268                   }
269                   _ {}
270                 }
271             }
272         }
273         {bcx: bcx, ptrs: ptrs}
274       }
275     }
276 }