summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorPatrick Walton <pcwalton@mimiga.net>2012-08-07 16:08:09 -0700
committerPatrick Walton <pcwalton@mimiga.net>2012-08-07 16:09:08 -0700
commit0f711e72f72cf69166902fe9df9c5063e5aa14ff (patch)
tree6025c53bc18b4c615caf042dbc2d9fe68b7f1c03 /src/libsyntax
parent1f0574e8f0d304afa6931f4fa1019c80a708b94d (diff)
downloadrust-0f711e72f72cf69166902fe9df9c5063e5aa14ff.tar.gz
rust-0f711e72f72cf69166902fe9df9c5063e5aa14ff.zip
libsyntax: Break struct definitions out of classes internally in a few more places
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/ast_map.rs32
-rw-r--r--src/libsyntax/print/pprust.rs145
2 files changed, 94 insertions, 83 deletions
diff --git a/src/libsyntax/ast_map.rs b/src/libsyntax/ast_map.rs
index 0c3e99a3794..5c270c968f3 100644
--- a/src/libsyntax/ast_map.rs
+++ b/src/libsyntax/ast_map.rs
@@ -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| {
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs
index c4bb680c220..db62637a673 100644
--- a/src/libsyntax/print/pprust.rs
+++ b/src/libsyntax/print/pprust.rs
@@ -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.