Skip to content

Commit 0f711e7

Browse files
committed
libsyntax: Break struct definitions out of classes internally in a few more places
1 parent 1f0574e commit 0f711e7

File tree

2 files changed

+94
-83
lines changed

2 files changed

+94
-83
lines changed

src/libsyntax/ast_map.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -217,19 +217,8 @@ fn map_item(i: @item, cx: ctx, v: vt) {
217217
}
218218
}
219219
item_class(struct_def, _) => {
220-
let (_, ms) = ast_util::split_class_items(struct_def.members);
221-
// Map trait refs to their parent classes. This is
222-
// so we can find the self_ty
223-
for struct_def.traits.each |p| {
224-
cx.map.insert(p.ref_id, node_item(i, item_path));
225-
// This is so we can look up the right things when
226-
// encoding/decoding
227-
cx.map.insert(p.impl_id, node_item(i, item_path));
228-
}
229-
let d_id = ast_util::local_def(i.id);
230-
let p = extend(cx, i.ident);
231-
// only need to handle methods
232-
do vec::iter(ms) |m| { map_method(d_id, p, m, cx); }
220+
map_struct_def(struct_def, node_item(i, item_path), i.ident, i.id, cx,
221+
v);
233222
}
234223
item_trait(tps, traits, methods) => {
235224
// Map trait refs to their parent classes. This is
@@ -258,6 +247,23 @@ fn map_item(i: @item, cx: ctx, v: vt) {
258247
vec::pop(cx.path);
259248
}
260249

250+
fn map_struct_def(struct_def: ast::struct_def, parent_node: ast_node,
251+
ident: ast::ident, id: ast::node_id, cx: ctx, _v: vt) {
252+
let (_, ms) = ast_util::split_class_items(struct_def.members);
253+
// Map trait refs to their parent classes. This is
254+
// so we can find the self_ty
255+
for struct_def.traits.each |p| {
256+
cx.map.insert(p.ref_id, parent_node);
257+
// This is so we can look up the right things when
258+
// encoding/decoding
259+
cx.map.insert(p.impl_id, parent_node);
260+
}
261+
let d_id = ast_util::local_def(id);
262+
let p = extend(cx, ident);
263+
// only need to handle methods
264+
do vec::iter(ms) |m| { map_method(d_id, p, m, cx); }
265+
}
266+
261267
fn map_view_item(vi: @view_item, cx: ctx, _v: vt) {
262268
match vi.node {
263269
view_item_export(vps) => for vps.each |vp| {

src/libsyntax/print/pprust.rs

Lines changed: 75 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -532,76 +532,8 @@ fn print_item(s: ps, &&item: @ast::item) {
532532
}
533533
ast::item_class(struct_def, tps) => {
534534
head(s, ~"class");
535-
word_nbsp(s, *item.ident);
536-
print_type_params(s, tps);
537-
if vec::len(struct_def.traits) != 0u {
538-
word_space(s, ~":");
539-
commasep(s, inconsistent, struct_def.traits, |s, p|
540-
print_path(s, p.path, false));
541-
}
542-
bopen(s);
543-
hardbreak_if_not_bol(s);
544-
do option::iter(struct_def.ctor) |ctor| {
545-
maybe_print_comment(s, ctor.span.lo);
546-
print_outer_attributes(s, ctor.node.attrs);
547-
// Doesn't call head because there shouldn't be a space after new.
548-
cbox(s, indent_unit);
549-
ibox(s, 4);
550-
word(s.s, ~"new(");
551-
print_fn_args(s, ctor.node.dec, ~[]);
552-
word(s.s, ~")");
553-
space(s.s);
554-
print_block(s, ctor.node.body);
555-
}
556-
do option::iter(struct_def.dtor) |dtor| {
557-
hardbreak_if_not_bol(s);
558-
maybe_print_comment(s, dtor.span.lo);
559-
print_outer_attributes(s, dtor.node.attrs);
560-
head(s, ~"drop");
561-
print_block(s, dtor.node.body);
562-
}
563-
for struct_def.members.each |ci| {
564-
/*
565-
FIXME (#1893): collect all private items and print
566-
them in a single "priv" section
567-
568-
tjc: I'm not going to fix this yet b/c we might
569-
change how exports work, including for class items
570-
*/
571-
hardbreak_if_not_bol(s);
572-
maybe_print_comment(s, ci.span.lo);
573-
let pr = ast_util::class_member_visibility(ci);
574-
match pr {
575-
ast::private => {
576-
head(s, ~"priv");
577-
bopen(s);
578-
hardbreak_if_not_bol(s);
579-
}
580-
_ => ()
581-
}
582-
match ci.node {
583-
ast::instance_var(nm, t, mt, _,_) => {
584-
word_nbsp(s, ~"let");
585-
match mt {
586-
ast::class_mutable => word_nbsp(s, ~"mut"),
587-
_ => ()
588-
}
589-
word(s.s, *nm);
590-
word_nbsp(s, ~":");
591-
print_type(s, t);
592-
word(s.s, ~";");
593-
}
594-
ast::class_method(m) => {
595-
print_method(s, m);
596-
}
597-
}
598-
match pr {
599-
ast::private => bclose(s, ci.span),
600-
_ => ()
601-
}
602-
}
603-
bclose(s, item.span);
604-
}
535+
print_struct(s, struct_def, tps, item.ident, item.span);
536+
}
605537
ast::item_impl(tps, traits, ty, methods) => {
606538
head(s, ~"impl");
607539
word(s.s, *item.ident);
@@ -650,6 +582,79 @@ fn print_item(s: ps, &&item: @ast::item) {
650582
s.ann.post(ann_node);
651583
}
652584

585+
fn print_struct(s: ps, struct_def: ast::struct_def, tps: ~[ast::ty_param],
586+
ident: ast::ident, span: ast::span) {
587+
word_nbsp(s, *ident);
588+
print_type_params(s, tps);
589+
if vec::len(struct_def.traits) != 0u {
590+
word_space(s, ~":");
591+
commasep(s, inconsistent, struct_def.traits, |s, p|
592+
print_path(s, p.path, false));
593+
}
594+
bopen(s);
595+
hardbreak_if_not_bol(s);
596+
do option::iter(struct_def.ctor) |ctor| {
597+
maybe_print_comment(s, ctor.span.lo);
598+
print_outer_attributes(s, ctor.node.attrs);
599+
// Doesn't call head because there shouldn't be a space after new.
600+
cbox(s, indent_unit);
601+
ibox(s, 4);
602+
word(s.s, ~"new(");
603+
print_fn_args(s, ctor.node.dec, ~[]);
604+
word(s.s, ~")");
605+
space(s.s);
606+
print_block(s, ctor.node.body);
607+
}
608+
do option::iter(struct_def.dtor) |dtor| {
609+
hardbreak_if_not_bol(s);
610+
maybe_print_comment(s, dtor.span.lo);
611+
print_outer_attributes(s, dtor.node.attrs);
612+
head(s, ~"drop");
613+
print_block(s, dtor.node.body);
614+
}
615+
for struct_def.members.each |ci| {
616+
/*
617+
FIXME (#1893): collect all private items and print
618+
them in a single "priv" section
619+
620+
tjc: I'm not going to fix this yet b/c we might
621+
change how exports work, including for class items
622+
*/
623+
hardbreak_if_not_bol(s);
624+
maybe_print_comment(s, ci.span.lo);
625+
let pr = ast_util::class_member_visibility(ci);
626+
match pr {
627+
ast::private => {
628+
head(s, ~"priv");
629+
bopen(s);
630+
hardbreak_if_not_bol(s);
631+
}
632+
_ => ()
633+
}
634+
match ci.node {
635+
ast::instance_var(nm, t, mt, _,_) => {
636+
word_nbsp(s, ~"let");
637+
match mt {
638+
ast::class_mutable => word_nbsp(s, ~"mut"),
639+
_ => ()
640+
}
641+
word(s.s, *nm);
642+
word_nbsp(s, ~":");
643+
print_type(s, t);
644+
word(s.s, ~";");
645+
}
646+
ast::class_method(m) => {
647+
print_method(s, m);
648+
}
649+
}
650+
match pr {
651+
ast::private => bclose(s, ci.span),
652+
_ => ()
653+
}
654+
}
655+
bclose(s, span);
656+
}
657+
653658
/// This doesn't deserve to be called "pretty" printing, but it should be
654659
/// meaning-preserving. A quick hack that might help would be to look at the
655660
/// spans embedded in the TTs to decide where to put spaces and newlines.

0 commit comments

Comments
 (0)