2012-05-14 19:57:39 -05:00
|
|
|
import std::map::{hashmap,str_hash};
|
|
|
|
import driver::session::session;
|
|
|
|
import lib::llvm::{TypeRef, ValueRef};
|
|
|
|
import syntax::ast;
|
|
|
|
import back::abi;
|
|
|
|
import common::*;
|
|
|
|
import build::*;
|
|
|
|
import base::*;
|
|
|
|
import type_of::*;
|
|
|
|
import ast::def_id;
|
|
|
|
import util::ppaux::ty_to_str;
|
|
|
|
|
2012-06-07 15:51:35 -05:00
|
|
|
enum reflector = {
|
|
|
|
visitor_val: ValueRef,
|
2012-06-29 18:26:56 -05:00
|
|
|
visitor_methods: @~[ty::method],
|
2012-06-07 15:51:35 -05:00
|
|
|
mut bcx: block
|
|
|
|
};
|
2012-05-16 20:24:00 -05:00
|
|
|
|
2012-06-07 15:51:35 -05:00
|
|
|
impl methods for reflector {
|
2012-05-16 20:24:00 -05:00
|
|
|
|
2012-06-07 15:51:35 -05:00
|
|
|
fn c_uint(u: uint) -> ValueRef {
|
2012-06-07 21:42:22 -05:00
|
|
|
C_uint(self.bcx.ccx(), u)
|
2012-06-07 15:51:35 -05:00
|
|
|
}
|
|
|
|
|
2012-06-08 17:35:55 -05:00
|
|
|
fn c_int(i: int) -> ValueRef {
|
|
|
|
C_int(self.bcx.ccx(), i)
|
|
|
|
}
|
|
|
|
|
2012-06-08 18:41:38 -05:00
|
|
|
fn c_slice(s: str) -> ValueRef {
|
|
|
|
let ss = C_estr_slice(self.bcx.ccx(), s);
|
|
|
|
do_spill_noroot(self.bcx, ss)
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn c_size_and_align(t: ty::t) -> ~[ValueRef] {
|
2012-06-11 20:33:58 -05:00
|
|
|
let tr = type_of::type_of(self.bcx.ccx(), t);
|
|
|
|
let s = shape::llsize_of_real(self.bcx.ccx(), tr);
|
|
|
|
let a = shape::llalign_of_min(self.bcx.ccx(), tr);
|
2012-06-29 18:26:56 -05:00
|
|
|
ret ~[self.c_uint(s),
|
|
|
|
self.c_uint(a)];
|
2012-06-11 20:33:58 -05:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn visit(ty_name: str, args: ~[ValueRef]) {
|
2012-06-07 21:42:22 -05:00
|
|
|
let tcx = self.bcx.tcx();
|
2012-06-10 02:49:59 -05:00
|
|
|
let mth_idx = option::get(ty::method_idx(@("visit_" + ty_name),
|
2012-06-07 15:51:35 -05:00
|
|
|
*self.visitor_methods));
|
|
|
|
let mth_ty = ty::mk_fn(tcx, self.visitor_methods[mth_idx].fty);
|
|
|
|
let v = self.visitor_val;
|
2012-06-30 18:19:07 -05:00
|
|
|
let get_lval = |bcx| {
|
2012-06-08 18:41:38 -05:00
|
|
|
let callee =
|
|
|
|
impl::trans_iface_callee(bcx, v, mth_ty, mth_idx);
|
|
|
|
#debug("calling mth ty %s, lltype %s",
|
|
|
|
ty_to_str(bcx.ccx().tcx, mth_ty),
|
|
|
|
val_str(bcx.ccx().tn, callee.val));
|
|
|
|
callee
|
2012-06-07 15:51:35 -05:00
|
|
|
};
|
2012-06-08 18:41:38 -05:00
|
|
|
#debug("passing %u args:", vec::len(args));
|
|
|
|
let bcx = self.bcx;
|
2012-06-30 18:19:07 -05:00
|
|
|
for args.eachi |i, a| {
|
2012-06-08 18:41:38 -05:00
|
|
|
#debug("arg %u: %s", i, val_str(bcx.ccx().tn, a));
|
|
|
|
}
|
2012-06-07 15:51:35 -05:00
|
|
|
self.bcx =
|
|
|
|
trans_call_inner(self.bcx, none, mth_ty, ty::mk_bool(tcx),
|
|
|
|
get_lval, arg_vals(args), ignore);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_tydesc(t: ty::t) {
|
|
|
|
self.bcx =
|
|
|
|
call_tydesc_glue(self.bcx, self.visitor_val, t,
|
|
|
|
abi::tydesc_field_visit_glue);
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn bracketed_t(bracket_name: str, t: ty::t, extra: ~[ValueRef]) {
|
2012-06-08 15:26:06 -05:00
|
|
|
self.visit("enter_" + bracket_name, extra);
|
|
|
|
self.visit_tydesc(t);
|
|
|
|
self.visit("leave_" + bracket_name, extra);
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn bracketed_mt(bracket_name: str, mt: ty::mt, extra: ~[ValueRef]) {
|
2012-06-08 15:26:06 -05:00
|
|
|
self.bracketed_t(bracket_name, mt.ty,
|
2012-06-29 18:26:56 -05:00
|
|
|
vec::append(~[self.c_uint(mt.mutbl as uint)],
|
2012-06-28 17:00:03 -05:00
|
|
|
extra));
|
2012-06-07 15:51:35 -05:00
|
|
|
}
|
2012-05-16 20:24:00 -05:00
|
|
|
|
2012-06-14 21:53:56 -05:00
|
|
|
fn vstore_name_and_extra(t: ty::t,
|
|
|
|
vstore: ty::vstore,
|
2012-06-29 18:26:56 -05:00
|
|
|
f: fn(str,~[ValueRef])) {
|
2012-06-07 15:51:35 -05:00
|
|
|
alt vstore {
|
2012-06-14 21:53:56 -05:00
|
|
|
ty::vstore_fixed(n) {
|
2012-06-29 18:26:56 -05:00
|
|
|
let extra = vec::append(~[self.c_uint(n)],
|
2012-06-28 17:00:03 -05:00
|
|
|
self.c_size_and_align(t));
|
2012-06-14 21:53:56 -05:00
|
|
|
f("fixed", extra)
|
|
|
|
}
|
2012-06-29 18:26:56 -05:00
|
|
|
ty::vstore_slice(_) { f("slice", ~[]) }
|
|
|
|
ty::vstore_uniq { f("uniq", ~[]);}
|
|
|
|
ty::vstore_box { f("box", ~[]); }
|
2012-06-07 15:51:35 -05:00
|
|
|
}
|
|
|
|
}
|
2012-05-16 20:24:00 -05:00
|
|
|
|
2012-06-07 15:51:35 -05:00
|
|
|
fn leaf(name: str) {
|
2012-06-29 18:26:56 -05:00
|
|
|
self.visit(name, ~[]);
|
2012-06-07 15:51:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Entrypoint
|
|
|
|
fn visit_ty(t: ty::t) {
|
|
|
|
|
2012-06-08 15:26:06 -05:00
|
|
|
let bcx = self.bcx;
|
|
|
|
#debug("reflect::visit_ty %s",
|
|
|
|
ty_to_str(bcx.ccx().tcx, t));
|
|
|
|
|
2012-06-07 15:51:35 -05:00
|
|
|
alt ty::get(t).struct {
|
|
|
|
ty::ty_bot { self.leaf("bot") }
|
|
|
|
ty::ty_nil { self.leaf("nil") }
|
|
|
|
ty::ty_bool { self.leaf("bool") }
|
|
|
|
ty::ty_int(ast::ty_i) { self.leaf("int") }
|
|
|
|
ty::ty_int(ast::ty_char) { self.leaf("char") }
|
|
|
|
ty::ty_int(ast::ty_i8) { self.leaf("i8") }
|
|
|
|
ty::ty_int(ast::ty_i16) { self.leaf("i16") }
|
|
|
|
ty::ty_int(ast::ty_i32) { self.leaf("i32") }
|
|
|
|
ty::ty_int(ast::ty_i64) { self.leaf("i64") }
|
|
|
|
ty::ty_uint(ast::ty_u) { self.leaf("uint") }
|
|
|
|
ty::ty_uint(ast::ty_u8) { self.leaf("u8") }
|
|
|
|
ty::ty_uint(ast::ty_u16) { self.leaf("u16") }
|
|
|
|
ty::ty_uint(ast::ty_u32) { self.leaf("u32") }
|
|
|
|
ty::ty_uint(ast::ty_u64) { self.leaf("u64") }
|
|
|
|
ty::ty_float(ast::ty_f) { self.leaf("float") }
|
|
|
|
ty::ty_float(ast::ty_f32) { self.leaf("f32") }
|
|
|
|
ty::ty_float(ast::ty_f64) { self.leaf("f64") }
|
|
|
|
ty::ty_str { self.leaf("str") }
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
ty::ty_vec(mt) { self.bracketed_mt("vec", mt, ~[]) }
|
2012-06-07 15:51:35 -05:00
|
|
|
ty::ty_estr(vst) {
|
2012-06-30 18:19:07 -05:00
|
|
|
do self.vstore_name_and_extra(t, vst) |name, extra| {
|
2012-06-07 15:51:35 -05:00
|
|
|
self.visit("estr_" + name, extra)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::ty_evec(mt, vst) {
|
2012-06-30 18:19:07 -05:00
|
|
|
do self.vstore_name_and_extra(t, vst) |name, extra| {
|
2012-06-07 15:51:35 -05:00
|
|
|
self.bracketed_mt("evec_" + name, mt, extra)
|
|
|
|
}
|
|
|
|
}
|
2012-06-29 18:26:56 -05:00
|
|
|
ty::ty_box(mt) { self.bracketed_mt("box", mt, ~[]) }
|
|
|
|
ty::ty_uniq(mt) { self.bracketed_mt("uniq", mt, ~[]) }
|
|
|
|
ty::ty_ptr(mt) { self.bracketed_mt("ptr", mt, ~[]) }
|
|
|
|
ty::ty_rptr(_, mt) { self.bracketed_mt("rptr", mt, ~[]) }
|
2012-06-07 15:51:35 -05:00
|
|
|
|
2012-06-08 15:26:06 -05:00
|
|
|
ty::ty_rec(fields) {
|
2012-06-29 18:26:56 -05:00
|
|
|
let extra = (vec::append(~[self.c_uint(vec::len(fields))],
|
2012-06-28 17:00:03 -05:00
|
|
|
self.c_size_and_align(t)));
|
2012-06-14 21:53:56 -05:00
|
|
|
self.visit("enter_rec", extra);
|
2012-06-30 18:19:07 -05:00
|
|
|
for fields.eachi |i, field| {
|
2012-06-08 15:26:06 -05:00
|
|
|
self.bracketed_mt("rec_field", field.mt,
|
2012-06-29 18:26:56 -05:00
|
|
|
~[self.c_uint(i),
|
|
|
|
self.c_slice(*field.ident)]);
|
2012-06-08 15:26:06 -05:00
|
|
|
}
|
2012-06-14 21:53:56 -05:00
|
|
|
self.visit("leave_rec", extra);
|
2012-06-08 15:26:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::ty_tup(tys) {
|
2012-06-29 18:26:56 -05:00
|
|
|
let extra = (vec::append(~[self.c_uint(vec::len(tys))],
|
2012-06-28 17:00:03 -05:00
|
|
|
self.c_size_and_align(t)));
|
2012-06-14 21:53:56 -05:00
|
|
|
self.visit("enter_tup", extra);
|
2012-06-30 18:19:07 -05:00
|
|
|
for tys.eachi |i, t| {
|
2012-06-29 18:26:56 -05:00
|
|
|
self.bracketed_t("tup_field", t, ~[self.c_uint(i)]);
|
2012-06-08 15:26:06 -05:00
|
|
|
}
|
2012-06-14 21:53:56 -05:00
|
|
|
self.visit("leave_tup", extra);
|
2012-06-08 15:26:06 -05:00
|
|
|
}
|
|
|
|
|
2012-06-21 18:44:10 -05:00
|
|
|
// FIXME (#2594): fetch constants out of intrinsic:: for the
|
|
|
|
// numbers.
|
2012-06-08 15:26:06 -05:00
|
|
|
ty::ty_fn(fty) {
|
|
|
|
let pureval = alt fty.purity {
|
|
|
|
ast::pure_fn { 0u }
|
|
|
|
ast::unsafe_fn { 1u }
|
|
|
|
ast::impure_fn { 2u }
|
2012-06-26 18:18:37 -05:00
|
|
|
ast::extern_fn { 3u }
|
2012-06-08 15:26:06 -05:00
|
|
|
};
|
|
|
|
let protoval = alt fty.proto {
|
|
|
|
ast::proto_bare { 0u }
|
|
|
|
ast::proto_any { 1u }
|
|
|
|
ast::proto_uniq { 2u }
|
|
|
|
ast::proto_box { 3u }
|
|
|
|
ast::proto_block { 4u }
|
|
|
|
};
|
|
|
|
let retval = alt fty.ret_style {
|
|
|
|
ast::noreturn { 0u }
|
|
|
|
ast::return_val { 1u }
|
|
|
|
};
|
2012-06-29 18:26:56 -05:00
|
|
|
let extra = ~[self.c_uint(pureval),
|
2012-06-08 15:26:06 -05:00
|
|
|
self.c_uint(protoval),
|
|
|
|
self.c_uint(vec::len(fty.inputs)),
|
2012-06-29 18:26:56 -05:00
|
|
|
self.c_uint(retval)];
|
2012-06-08 15:26:06 -05:00
|
|
|
self.visit("enter_fn", extra);
|
2012-06-30 18:19:07 -05:00
|
|
|
for fty.inputs.eachi |i, arg| {
|
2012-06-08 15:26:06 -05:00
|
|
|
let modeval = alt arg.mode {
|
|
|
|
ast::infer(_) { 0u }
|
|
|
|
ast::expl(e) {
|
|
|
|
alt e {
|
|
|
|
ast::by_ref { 1u }
|
|
|
|
ast::by_val { 2u }
|
|
|
|
ast::by_mutbl_ref { 3u }
|
|
|
|
ast::by_move { 4u }
|
|
|
|
ast::by_copy { 5u }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
self.bracketed_t("fn_input", arg.ty,
|
2012-06-29 18:26:56 -05:00
|
|
|
~[self.c_uint(i),
|
|
|
|
self.c_uint(modeval)]);
|
2012-06-08 15:26:06 -05:00
|
|
|
}
|
|
|
|
self.bracketed_t("fn_output", fty.output,
|
2012-06-29 18:26:56 -05:00
|
|
|
~[self.c_uint(retval)]);
|
2012-06-08 15:26:06 -05:00
|
|
|
self.visit("leave_fn", extra);
|
|
|
|
}
|
|
|
|
|
2012-06-08 17:35:55 -05:00
|
|
|
ty::ty_class(did, substs) {
|
|
|
|
let bcx = self.bcx;
|
|
|
|
let tcx = bcx.ccx().tcx;
|
|
|
|
let fields = ty::class_items_as_fields(tcx, did, substs);
|
2012-06-29 18:26:56 -05:00
|
|
|
let extra = vec::append(~[self.c_uint(vec::len(fields))],
|
2012-06-28 17:00:03 -05:00
|
|
|
self.c_size_and_align(t));
|
2012-06-14 21:53:56 -05:00
|
|
|
|
|
|
|
self.visit("enter_class", extra);
|
2012-06-30 18:19:07 -05:00
|
|
|
for fields.eachi |i, field| {
|
2012-06-08 17:35:55 -05:00
|
|
|
self.bracketed_mt("class_field", field.mt,
|
2012-06-29 18:26:56 -05:00
|
|
|
~[self.c_uint(i),
|
|
|
|
self.c_slice(*field.ident)]);
|
2012-06-08 17:35:55 -05:00
|
|
|
}
|
2012-06-14 21:53:56 -05:00
|
|
|
self.visit("leave_class", extra);
|
2012-06-08 17:35:55 -05:00
|
|
|
}
|
|
|
|
|
2012-06-21 18:44:10 -05:00
|
|
|
// FIXME (#2595): visiting all the variants in turn is probably
|
|
|
|
// not ideal. It'll work but will get costly on big enums. Maybe
|
|
|
|
// let the visitor tell us if it wants to visit only a particular
|
|
|
|
// variant?
|
2012-06-08 17:35:55 -05:00
|
|
|
ty::ty_enum(did, substs) {
|
|
|
|
let bcx = self.bcx;
|
|
|
|
let tcx = bcx.ccx().tcx;
|
|
|
|
let variants = ty::substd_enum_variants(tcx, did, substs);
|
2012-06-29 18:26:56 -05:00
|
|
|
let extra = vec::append(~[self.c_uint(vec::len(variants))],
|
2012-06-28 17:00:03 -05:00
|
|
|
self.c_size_and_align(t));
|
2012-06-08 17:35:55 -05:00
|
|
|
|
2012-06-14 21:53:56 -05:00
|
|
|
self.visit("enter_enum", extra);
|
2012-06-30 18:19:07 -05:00
|
|
|
for variants.eachi |i, v| {
|
2012-06-29 18:26:56 -05:00
|
|
|
let extra = ~[self.c_uint(i),
|
2012-06-08 17:35:55 -05:00
|
|
|
self.c_int(v.disr_val),
|
2012-06-08 18:41:38 -05:00
|
|
|
self.c_uint(vec::len(v.args)),
|
2012-06-29 18:26:56 -05:00
|
|
|
self.c_slice(*v.name)];
|
2012-06-08 17:35:55 -05:00
|
|
|
self.visit("enter_enum_variant", extra);
|
2012-06-30 18:19:07 -05:00
|
|
|
for v.args.eachi |j, a| {
|
2012-06-08 17:35:55 -05:00
|
|
|
self.bracketed_t("enum_variant_field", a,
|
2012-06-29 18:26:56 -05:00
|
|
|
~[self.c_uint(j)]);
|
2012-06-08 17:35:55 -05:00
|
|
|
}
|
|
|
|
self.visit("leave_enum_variant", extra);
|
|
|
|
}
|
2012-06-14 21:53:56 -05:00
|
|
|
self.visit("leave_enum", extra);
|
2012-06-08 17:35:55 -05:00
|
|
|
}
|
2012-06-08 15:26:06 -05:00
|
|
|
|
|
|
|
// Miscallaneous extra types
|
|
|
|
ty::ty_iface(_, _) { self.leaf("iface") }
|
|
|
|
ty::ty_var(_) { self.leaf("var") }
|
|
|
|
ty::ty_var_integral(_) { self.leaf("var_integral") }
|
2012-06-29 18:26:56 -05:00
|
|
|
ty::ty_param(n, _) { self.visit("param", ~[self.c_uint(n)]) }
|
2012-06-08 15:26:06 -05:00
|
|
|
ty::ty_self { self.leaf("self") }
|
|
|
|
ty::ty_type { self.leaf("type") }
|
|
|
|
ty::ty_opaque_box { self.leaf("opaque_box") }
|
2012-06-29 18:26:56 -05:00
|
|
|
ty::ty_constr(t, _) { self.bracketed_t("constr", t, ~[]) }
|
2012-06-08 15:26:06 -05:00
|
|
|
ty::ty_opaque_closure_ptr(ck) {
|
|
|
|
let ckval = alt ck {
|
|
|
|
ty::ck_block { 0u }
|
|
|
|
ty::ck_box { 1u }
|
|
|
|
ty::ck_uniq { 2u }
|
|
|
|
};
|
2012-06-29 18:26:56 -05:00
|
|
|
self.visit("closure_ptr", ~[self.c_uint(ckval)])
|
2012-06-08 15:26:06 -05:00
|
|
|
}
|
2012-06-29 18:26:56 -05:00
|
|
|
ty::ty_unboxed_vec(mt) { self.bracketed_mt("vec", mt, ~[]) }
|
2012-06-07 15:51:35 -05:00
|
|
|
}
|
2012-05-14 19:57:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit a sequence of calls to visit_ty::visit_foo
|
|
|
|
fn emit_calls_to_iface_visit_ty(bcx: block, t: ty::t,
|
|
|
|
visitor_val: ValueRef,
|
|
|
|
visitor_iid: def_id) -> block {
|
2012-06-07 15:51:35 -05:00
|
|
|
|
|
|
|
let r = reflector({
|
|
|
|
visitor_val: visitor_val,
|
|
|
|
visitor_methods: ty::iface_methods(bcx.tcx(), visitor_iid),
|
|
|
|
mut bcx: bcx
|
|
|
|
});
|
|
|
|
|
|
|
|
r.visit_ty(t);
|
|
|
|
ret r.bcx;
|
2012-05-14 19:57:39 -05:00
|
|
|
}
|