encode variant names and have log print them out.

This commit is contained in:
Niko Matsakis 2012-01-15 21:42:10 -08:00
parent c3bc8fada8
commit a83ad1b9e6
8 changed files with 75 additions and 13 deletions

View File

@ -246,6 +246,7 @@ fn get_tag_variants(cdata: cmd, id: ast::node_id, tcx: ty::ctxt)
for did: ast::def_id in variant_ids {
let item = find_item(did.node, items);
let ctor_ty = item_type(item, tcx, cdata);
let name = item_name(item);
let arg_tys: [ty::t] = [];
alt ty::struct(tcx, ctor_ty) {
ty::ty_fn(f) {
@ -257,8 +258,8 @@ fn get_tag_variants(cdata: cmd, id: ast::node_id, tcx: ty::ctxt)
some(val) { disr_val = val; }
_ { /* empty */ }
}
infos += [@{args: arg_tys, ctor_ty: ctor_ty, id: did,
disr_val: disr_val}];
infos += [@{args: arg_tys, ctor_ty: ctor_ty, name: name,
id: did, disr_val: disr_val}];
disr_val += 1;
}
ret infos;

View File

@ -239,6 +239,7 @@ fn encode_tag_variant_info(ecx: @encode_ctxt, ebml_w: ebml::writer,
ebml::start_tag(ebml_w, tag_items_data_item);
encode_def_id(ebml_w, local_def(variant.node.id));
encode_family(ebml_w, 'v' as u8);
encode_name(ebml_w, variant.node.name);
encode_tag_id(ebml_w, local_def(id));
encode_type(ecx, ebml_w,
node_id_to_monotype(ecx.ccx.tcx, variant.node.id));

View File

@ -458,6 +458,15 @@ fn shape_of_variant(ccx: @crate_ctxt, v: ty::variant_info,
ret s;
}
//fn variant_names(ccx: @crate_ctxt, tag_id: ast::def_id) -> [str] {
// assert ast::local_crate == tag_id.crate;
// alt ccx.tcx.items.get(tag_id.node) {
// ast_map::node_item(@{node: ast::item_tag(variants, _), _}) {
// vec::map(variants) {|variant| variant.node.name}
// }
// }
//}
fn gen_tag_shapes(ccx: @crate_ctxt) -> ValueRef {
// Loop over all the tag variants and write their shapes into a data
// buffer. As we do this, it's possible for us to discover new tags, so we
@ -471,11 +480,14 @@ fn gen_tag_shapes(ccx: @crate_ctxt) -> ValueRef {
let item_tyt = ty::lookup_item_type(ccx.tcx, did);
let ty_param_count = vec::len(*item_tyt.bounds);
for v: ty::variant_info in *variants {
vec::iter(*variants) {|v|
offsets += [vec::len(data) as u16];
let variant_shape = shape_of_variant(ccx, v, ty_param_count);
add_substr(data, variant_shape);
let zname = str::bytes(v.name) + [0u8];
add_substr(data, zname);
}
i += 1u;

View File

@ -2633,8 +2633,8 @@ fn impl_iface(cx: ctxt, id: ast::def_id) -> option::t<t> {
}
// Tag information
type variant_info = @{args: [ty::t], ctor_ty: ty::t, id: ast::def_id,
disr_val: int};
type variant_info = @{args: [ty::t], ctor_ty: ty::t, name: str,
id: ast::def_id, disr_val: int};
fn tag_variants(cx: ctxt, id: ast::def_id) -> @[variant_info] {
alt cx.tag_var_cache.find(id) {
@ -2666,6 +2666,7 @@ fn tag_variants(cx: ctxt, id: ast::def_id) -> @[variant_info] {
}
@{args: arg_tys,
ctor_ty: ctor_ty,
name: variant.node.name,
id: ast_util::local_def(variant.node.id),
disr_val: disr_val
}

View File

@ -358,7 +358,8 @@ public:
void walk_res2(const rust_fn *dtor, uint16_t n_ty_params,
const type_param *ty_params_sp, const uint8_t *end_sp,
const data_pair<uintptr_t> &live);
void walk_variant2(tag_info &tinfo, tag_variant_t variant_id,
void walk_variant2(tag_info &tinfo,
tag_variant_t variant_id,
const std::pair<const uint8_t *,const uint8_t *>
variant_ptr_and_end);
@ -413,7 +414,8 @@ cmp::walk_res2(const rust_fn *dtor, uint16_t n_ty_params,
}
void
cmp::walk_variant2(tag_info &tinfo, tag_variant_t variant_id,
cmp::walk_variant2(tag_info &tinfo,
tag_variant_t variant_id,
const std::pair<const uint8_t *,const uint8_t *>
variant_ptr_and_end) {
cmp sub(*this, variant_ptr_and_end.first, tinfo.params);
@ -485,7 +487,8 @@ log::walk_vec2(bool is_pod, const std::pair<ptr,ptr> &data) {
}
void
log::walk_variant2(tag_info &tinfo, tag_variant_t variant_id,
log::walk_variant2(tag_info &tinfo,
tag_variant_t variant_id,
const std::pair<const uint8_t *,const uint8_t *>
variant_ptr_and_end) {
log sub(*this, variant_ptr_and_end.first, tinfo.params);

View File

@ -246,6 +246,9 @@ public:
std::pair<const uint8_t *,const uint8_t *>
get_variant_sp(tag_info &info, tag_variant_t variant_id);
const char *
get_variant_name(tag_info &info, tag_variant_t variant_id);
protected:
inline uint8_t peek() { return *sp; }
@ -415,9 +418,19 @@ ctxt<T>::get_variant_sp(tag_info &tinfo, tag_variant_t variant_id) {
const uint8_t *variant_ptr = tables->tags + variant_offset;
uint16_t variant_len = get_u16_bump(variant_ptr);
const uint8_t *variant_end = variant_ptr + variant_len;
return std::make_pair(variant_ptr, variant_end);
}
template<typename T>
const char *
ctxt<T>::get_variant_name(tag_info &tinfo, tag_variant_t variant_id) {
std::pair<const uint8_t *,const uint8_t *> variant_ptr_and_end =
this->get_variant_sp(tinfo, variant_id);
// skip over the length to get the null-terminated string:
return (const char*)(variant_ptr_and_end.second + 2);
}
template<typename T>
void
ctxt<T>::walk_vec0() {
@ -951,7 +964,7 @@ template<typename T,typename U>
void
data<T,U>::walk_variant1(tag_info &tinfo, tag_variant_t variant_id) {
std::pair<const uint8_t *,const uint8_t *> variant_ptr_and_end =
this->get_variant_sp(tinfo, variant_id);
this->get_variant_sp(tinfo, variant_id);
static_cast<T *>(this)->walk_variant2(tinfo, variant_id,
variant_ptr_and_end);
}
@ -1128,7 +1141,8 @@ private:
}
void walk_tag2(tag_info &tinfo, tag_variant_t tag_variant) {
out << prefix << "tag" << tag_variant;
// out << prefix << "tag" << tag_variant;
out << prefix << get_variant_name(tinfo, tag_variant);
data<log,ptr>::walk_variant1(tinfo, tag_variant);
}
@ -1187,9 +1201,10 @@ private:
void walk_struct2(const uint8_t *end_sp);
void walk_vec2(bool is_pod, const std::pair<ptr,ptr> &data);
void walk_variant2(tag_info &tinfo, tag_variant_t variant_id,
const std::pair<const uint8_t *,const uint8_t *>
variant_ptr_and_end);
void walk_variant2(tag_info &tinfo,
tag_variant_t variant_id,
const std::pair<const uint8_t *,const uint8_t *>
variant_ptr_and_end);
void walk_string2(const std::pair<ptr,ptr> &data);
void walk_res2(const rust_fn *dtor, unsigned n_params,
const type_param *params, const uint8_t *end_sp, bool live);

View File

@ -0,0 +1,18 @@
use std;
import std::list;
tag foo {
a(uint);
b(str);
}
fn check_log<T>(exp: str, v: T) {
assert exp == #fmt["%?", v];
}
fn main() {
let x = list::from_vec([a(22u), b("hi")]);
let exp = "cons(a(22), @cons(b(\"hi\"), @nil))";
assert #fmt["%?", x] == exp;
check_log(exp, x);
}

View File

@ -0,0 +1,11 @@
tag foo {
a(uint);
b(str);
c;
}
fn main() {
assert "a(22)" == #fmt["%?", a(22u)];
assert "b(\"hi\")" == #fmt["%?", b("hi")];
assert "c" == #fmt["%?", c];
}