libsyntax: Break struct definitions out of classes internally in a few more places

This commit is contained in:
Patrick Walton 2012-08-07 16:08:09 -07:00
parent 1f0574e8f0
commit 0f711e72f7
2 changed files with 94 additions and 83 deletions

View file

@ -217,19 +217,8 @@ fn map_item(i: @item, cx: ctx, v: vt) {
}
}
item_class(struct_def, _) => {
let (_, ms) = ast_util::split_class_items(struct_def.members);
// Map trait refs to their parent classes. This is
// so we can find the self_ty
for struct_def.traits.each |p| {
cx.map.insert(p.ref_id, node_item(i, item_path));
// This is so we can look up the right things when
// encoding/decoding
cx.map.insert(p.impl_id, node_item(i, item_path));
}
let d_id = ast_util::local_def(i.id);
let p = extend(cx, i.ident);
// only need to handle methods
do vec::iter(ms) |m| { map_method(d_id, p, m, cx); }
map_struct_def(struct_def, node_item(i, item_path), i.ident, i.id, cx,
v);
}
item_trait(tps, traits, methods) => {
// Map trait refs to their parent classes. This is
@ -258,6 +247,23 @@ fn map_item(i: @item, cx: ctx, v: vt) {
vec::pop(cx.path);
}
fn map_struct_def(struct_def: ast::struct_def, parent_node: ast_node,
ident: ast::ident, id: ast::node_id, cx: ctx, _v: vt) {
let (_, ms) = ast_util::split_class_items(struct_def.members);
// Map trait refs to their parent classes. This is
// so we can find the self_ty
for struct_def.traits.each |p| {
cx.map.insert(p.ref_id, parent_node);
// This is so we can look up the right things when
// encoding/decoding
cx.map.insert(p.impl_id, parent_node);
}
let d_id = ast_util::local_def(id);
let p = extend(cx, ident);
// only need to handle methods
do vec::iter(ms) |m| { map_method(d_id, p, m, cx); }
}
fn map_view_item(vi: @view_item, cx: ctx, _v: vt) {
match vi.node {
view_item_export(vps) => for vps.each |vp| {

View file

@ -532,76 +532,8 @@ fn print_item(s: ps, &&item: @ast::item) {
}
ast::item_class(struct_def, tps) => {
head(s, ~"class");
word_nbsp(s, *item.ident);
print_type_params(s, tps);
if vec::len(struct_def.traits) != 0u {
word_space(s, ~":");
commasep(s, inconsistent, struct_def.traits, |s, p|
print_path(s, p.path, false));
}
bopen(s);
hardbreak_if_not_bol(s);
do option::iter(struct_def.ctor) |ctor| {
maybe_print_comment(s, ctor.span.lo);
print_outer_attributes(s, ctor.node.attrs);
// Doesn't call head because there shouldn't be a space after new.
cbox(s, indent_unit);
ibox(s, 4);
word(s.s, ~"new(");
print_fn_args(s, ctor.node.dec, ~[]);
word(s.s, ~")");
space(s.s);
print_block(s, ctor.node.body);
}
do option::iter(struct_def.dtor) |dtor| {
hardbreak_if_not_bol(s);
maybe_print_comment(s, dtor.span.lo);
print_outer_attributes(s, dtor.node.attrs);
head(s, ~"drop");
print_block(s, dtor.node.body);
}
for struct_def.members.each |ci| {
/*
FIXME (#1893): collect all private items and print
them in a single "priv" section
tjc: I'm not going to fix this yet b/c we might
change how exports work, including for class items
*/
hardbreak_if_not_bol(s);
maybe_print_comment(s, ci.span.lo);
let pr = ast_util::class_member_visibility(ci);
match pr {
ast::private => {
head(s, ~"priv");
bopen(s);
hardbreak_if_not_bol(s);
}
_ => ()
}
match ci.node {
ast::instance_var(nm, t, mt, _,_) => {
word_nbsp(s, ~"let");
match mt {
ast::class_mutable => word_nbsp(s, ~"mut"),
_ => ()
}
word(s.s, *nm);
word_nbsp(s, ~":");
print_type(s, t);
word(s.s, ~";");
}
ast::class_method(m) => {
print_method(s, m);
}
}
match pr {
ast::private => bclose(s, ci.span),
_ => ()
}
}
bclose(s, item.span);
}
print_struct(s, struct_def, tps, item.ident, item.span);
}
ast::item_impl(tps, traits, ty, methods) => {
head(s, ~"impl");
word(s.s, *item.ident);
@ -650,6 +582,79 @@ fn print_item(s: ps, &&item: @ast::item) {
s.ann.post(ann_node);
}
fn print_struct(s: ps, struct_def: ast::struct_def, tps: ~[ast::ty_param],
ident: ast::ident, span: ast::span) {
word_nbsp(s, *ident);
print_type_params(s, tps);
if vec::len(struct_def.traits) != 0u {
word_space(s, ~":");
commasep(s, inconsistent, struct_def.traits, |s, p|
print_path(s, p.path, false));
}
bopen(s);
hardbreak_if_not_bol(s);
do option::iter(struct_def.ctor) |ctor| {
maybe_print_comment(s, ctor.span.lo);
print_outer_attributes(s, ctor.node.attrs);
// Doesn't call head because there shouldn't be a space after new.
cbox(s, indent_unit);
ibox(s, 4);
word(s.s, ~"new(");
print_fn_args(s, ctor.node.dec, ~[]);
word(s.s, ~")");
space(s.s);
print_block(s, ctor.node.body);
}
do option::iter(struct_def.dtor) |dtor| {
hardbreak_if_not_bol(s);
maybe_print_comment(s, dtor.span.lo);
print_outer_attributes(s, dtor.node.attrs);
head(s, ~"drop");
print_block(s, dtor.node.body);
}
for struct_def.members.each |ci| {
/*
FIXME (#1893): collect all private items and print
them in a single "priv" section
tjc: I'm not going to fix this yet b/c we might
change how exports work, including for class items
*/
hardbreak_if_not_bol(s);
maybe_print_comment(s, ci.span.lo);
let pr = ast_util::class_member_visibility(ci);
match pr {
ast::private => {
head(s, ~"priv");
bopen(s);
hardbreak_if_not_bol(s);
}
_ => ()
}
match ci.node {
ast::instance_var(nm, t, mt, _,_) => {
word_nbsp(s, ~"let");
match mt {
ast::class_mutable => word_nbsp(s, ~"mut"),
_ => ()
}
word(s.s, *nm);
word_nbsp(s, ~":");
print_type(s, t);
word(s.s, ~";");
}
ast::class_method(m) => {
print_method(s, m);
}
}
match pr {
ast::private => bclose(s, ci.span),
_ => ()
}
}
bclose(s, span);
}
/// This doesn't deserve to be called "pretty" printing, but it should be
/// meaning-preserving. A quick hack that might help would be to look at the
/// spans embedded in the TTs to decide where to put spaces and newlines.