]> git.lizzy.rs Git - rust.git/commitdiff
Tweak ItemDecorator API
authorSteven Fackler <sfackler@gmail.com>
Thu, 13 Feb 2014 07:53:52 +0000 (23:53 -0800)
committerSteven Fackler <sfackler@gmail.com>
Fri, 14 Feb 2014 05:53:06 +0000 (21:53 -0800)
The old method of building up a list of items and threading it through
all of the decorators was unwieldy and not really scalable as
non-deriving ItemDecorators become possible. The API is now that the
decorator gets an immutable reference to the item it's attached to, and
a callback that it can pass new items to. If we want to add syntax
extensions that can modify the item they're attached to, we can add that
later, but I think it'll have to be separate from ItemDecorator to avoid
strange ordering issues.

18 files changed:
src/libsyntax/ext/base.rs
src/libsyntax/ext/deriving/clone.rs
src/libsyntax/ext/deriving/cmp/eq.rs
src/libsyntax/ext/deriving/cmp/ord.rs
src/libsyntax/ext/deriving/cmp/totaleq.rs
src/libsyntax/ext/deriving/cmp/totalord.rs
src/libsyntax/ext/deriving/decodable.rs
src/libsyntax/ext/deriving/default.rs
src/libsyntax/ext/deriving/encodable.rs
src/libsyntax/ext/deriving/generic.rs
src/libsyntax/ext/deriving/iter_bytes.rs
src/libsyntax/ext/deriving/mod.rs
src/libsyntax/ext/deriving/primitive.rs
src/libsyntax/ext/deriving/rand.rs
src/libsyntax/ext/deriving/show.rs
src/libsyntax/ext/deriving/to_str.rs
src/libsyntax/ext/deriving/zero.rs
src/libsyntax/ext/expand.rs

index 7ac66ecaa3713c7cb4eae5ec0500c75418ef27c6..bb5245c240f4fff651b9c32c3941a317b2fb33d4 100644 (file)
@@ -35,7 +35,7 @@ pub struct MacroDef {
 }
 
 pub type ItemDecorator =
-    fn(&mut ExtCtxt, Span, @ast::MetaItem, ~[@ast::Item]) -> ~[@ast::Item];
+    fn(&mut ExtCtxt, Span, @ast::MetaItem, @ast::Item, |@ast::Item|);
 
 pub struct BasicMacroExpander {
     expander: MacroExpanderFn,
index 76cf5997aee75851e586ac63c8b7ed4c1819c069..bd961002f5311b7f669a615298e46107571ab728 100644 (file)
@@ -17,8 +17,8 @@
 pub fn expand_deriving_clone(cx: &mut ExtCtxt,
                              span: Span,
                              mitem: @MetaItem,
-                             in_items: ~[@Item])
-                          -> ~[@Item] {
+                             item: @Item,
+                             push: |@Item|) {
     let trait_def = TraitDef {
         span: span,
         path: Path::new(~["std", "clone", "Clone"]),
@@ -38,14 +38,14 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt,
         ]
     };
 
-    trait_def.expand(cx, mitem, in_items)
+    trait_def.expand(cx, mitem, item, push)
 }
 
 pub fn expand_deriving_deep_clone(cx: &mut ExtCtxt,
                                   span: Span,
                                   mitem: @MetaItem,
-                                  in_items: ~[@Item])
-    -> ~[@Item] {
+                                  item: @Item,
+                                  push: |@Item|) {
     let trait_def = TraitDef {
         span: span,
         path: Path::new(~["std", "clone", "DeepClone"]),
@@ -67,7 +67,7 @@ pub fn expand_deriving_deep_clone(cx: &mut ExtCtxt,
         ]
     };
 
-    trait_def.expand(cx, mitem, in_items)
+    trait_def.expand(cx, mitem, item, push)
 }
 
 fn cs_clone(
index 4672f1a8db689131de597d841b04f13c03ff9e70..6592dc97f348eb3dbf6e5daaa8805cf77ab64f9c 100644 (file)
@@ -17,7 +17,8 @@
 pub fn expand_deriving_eq(cx: &mut ExtCtxt,
                           span: Span,
                           mitem: @MetaItem,
-                          in_items: ~[@Item]) -> ~[@Item] {
+                          item: @Item,
+                          push: |@Item|) {
     // structures are equal if all fields are equal, and non equal, if
     // any fields are not equal or if the enum variants are different
     fn cs_eq(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> @Expr {
@@ -54,5 +55,5 @@ macro_rules! md (
             md!("ne", cs_ne)
         ]
     };
-    trait_def.expand(cx, mitem, in_items)
+    trait_def.expand(cx, mitem, item, push)
 }
index 1a8ace69307a7313b17c6fecd2dfe2580cfab3de..c41986fceef64ad546b3a72e8cda6ebc4c27f6fa 100644 (file)
@@ -18,7 +18,8 @@
 pub fn expand_deriving_ord(cx: &mut ExtCtxt,
                            span: Span,
                            mitem: @MetaItem,
-                           in_items: ~[@Item]) -> ~[@Item] {
+                           item: @Item,
+                           push: |@Item|) {
     macro_rules! md (
         ($name:expr, $op:expr, $equal:expr) => {
             MethodDef {
@@ -46,7 +47,7 @@ macro_rules! md (
             md!("ge", false, true)
         ]
     };
-    trait_def.expand(cx, mitem, in_items)
+    trait_def.expand(cx, mitem, item, push)
 }
 
 /// Strict inequality.
index 7b7c1afa4d5cb990928adb196e025c1d03ca701d..63e13b492020dc32506a994e62ee133257a3dd6f 100644 (file)
@@ -17,7 +17,8 @@
 pub fn expand_deriving_totaleq(cx: &mut ExtCtxt,
                                span: Span,
                                mitem: @MetaItem,
-                               in_items: ~[@Item]) -> ~[@Item] {
+                               item: @Item,
+                               push: |@Item|) {
     fn cs_equals(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> @Expr {
         cs_and(|cx, span, _, _| cx.expr_bool(span, false),
                cx, span, substr)
@@ -41,5 +42,5 @@ fn cs_equals(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> @Expr {
             }
         ]
     };
-    trait_def.expand(cx, mitem, in_items)
+    trait_def.expand(cx, mitem, item, push)
 }
index 157b6dc7521b7a737dcd82bf56ac9ed9ae49dc00..3e58d2edb29603e5763c53f3d02897e1627908b2 100644 (file)
@@ -19,7 +19,8 @@
 pub fn expand_deriving_totalord(cx: &mut ExtCtxt,
                                 span: Span,
                                 mitem: @MetaItem,
-                                in_items: ~[@Item]) -> ~[@Item] {
+                                item: @Item,
+                                push: |@Item|) {
     let trait_def = TraitDef {
         span: span,
         path: Path::new(~["std", "cmp", "TotalOrd"]),
@@ -39,7 +40,7 @@ pub fn expand_deriving_totalord(cx: &mut ExtCtxt,
         ]
     };
 
-    trait_def.expand(cx, mitem, in_items)
+    trait_def.expand(cx, mitem, item, push)
 }
 
 
index fdf1ef17d538bf084884a8091526811bb39502a3..c5c9a43fa5f584d3842ef08d1f05c7681de64e72 100644 (file)
@@ -24,7 +24,8 @@
 pub fn expand_deriving_decodable(cx: &mut ExtCtxt,
                                  span: Span,
                                  mitem: @MetaItem,
-                                 in_items: ~[@Item]) -> ~[@Item] {
+                                 item: @Item,
+                                 push: |@Item|) {
     let trait_def = TraitDef {
         span: span,
         path: Path::new_(~["serialize", "Decodable"], None,
@@ -49,7 +50,7 @@ pub fn expand_deriving_decodable(cx: &mut ExtCtxt,
         ]
     };
 
-    trait_def.expand(cx, mitem, in_items)
+    trait_def.expand(cx, mitem, item, push)
 }
 
 fn decodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
index 7b87152b7115a1c122786310fe005c23cd0a2b5a..7ed27168bb60612632e65ebc25e0c510265d3f81 100644 (file)
@@ -17,8 +17,8 @@
 pub fn expand_deriving_default(cx: &mut ExtCtxt,
                             span: Span,
                             mitem: @MetaItem,
-                            in_items: ~[@Item])
-                               -> ~[@Item] {
+                            item: @Item,
+                            push: |@Item|) {
     let trait_def = TraitDef {
         span: span,
         path: Path::new(~["std", "default", "Default"]),
@@ -37,7 +37,7 @@ pub fn expand_deriving_default(cx: &mut ExtCtxt,
             },
         ]
     };
-    trait_def.expand(cx, mitem, in_items)
+    trait_def.expand(cx, mitem, item, push)
 }
 
 fn default_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> @Expr {
index 43edf6006b414bfd83e6f07f6a34db6a9ee5ed4f..99c712caafef8c482051b5f68b806be9077e486e 100644 (file)
@@ -85,7 +85,8 @@ fn decode(d: &D) -> spanned<T> {
 pub fn expand_deriving_encodable(cx: &mut ExtCtxt,
                                  span: Span,
                                  mitem: @MetaItem,
-                                 in_items: ~[@Item]) -> ~[@Item] {
+                                 item: @Item,
+                                 push: |@Item|) {
     let trait_def = TraitDef {
         span: span,
         path: Path::new_(~["serialize", "Encodable"], None,
@@ -110,7 +111,7 @@ pub fn expand_deriving_encodable(cx: &mut ExtCtxt,
         ]
     };
 
-    trait_def.expand(cx, mitem, in_items)
+    trait_def.expand(cx, mitem, item, push)
 }
 
 fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
index 9d290c93c6494f1edca3268fe0cb548d0101fdcf..effcccdbaf831cbbcc6629d6ee3ee76454283aa2 100644 (file)
@@ -322,27 +322,23 @@ impl<'a> TraitDef<'a> {
     pub fn expand(&self,
                   cx: &mut ExtCtxt,
                   _mitem: @ast::MetaItem,
-                  in_items: ~[@ast::Item]) -> ~[@ast::Item] {
-        let mut result = ~[];
-        for item in in_items.iter() {
-            result.push(*item);
-            match item.node {
-                ast::ItemStruct(struct_def, ref generics) => {
-                    result.push(self.expand_struct_def(cx,
-                                                       struct_def,
-                                                       item.ident,
-                                                       generics));
-                }
-                ast::ItemEnum(ref enum_def, ref generics) => {
-                    result.push(self.expand_enum_def(cx,
-                                                     enum_def,
-                                                     item.ident,
-                                                     generics));
-                }
-                _ => ()
+                  item: @ast::Item,
+                  push: |@ast::Item|) {
+        match item.node {
+            ast::ItemStruct(struct_def, ref generics) => {
+                push(self.expand_struct_def(cx,
+                                            struct_def,
+                                            item.ident,
+                                            generics));
+            }
+            ast::ItemEnum(ref enum_def, ref generics) => {
+                push(self.expand_enum_def(cx,
+                                          enum_def,
+                                          item.ident,
+                                          generics));
             }
+            _ => ()
         }
-        result
     }
 
     /**
index 19e81b81df6a650434d04bb28d05ef63791050e4..b1adf96b90af8957a4aa5ae41f86e59d0ce36a5d 100644 (file)
@@ -18,7 +18,8 @@
 pub fn expand_deriving_iter_bytes(cx: &mut ExtCtxt,
                                   span: Span,
                                   mitem: @MetaItem,
-                                  in_items: ~[@Item]) -> ~[@Item] {
+                                  item: @Item,
+                                  push: |@Item|) {
     let trait_def = TraitDef {
         span: span,
         path: Path::new(~["std", "to_bytes", "IterBytes"]),
@@ -41,7 +42,7 @@ pub fn expand_deriving_iter_bytes(cx: &mut ExtCtxt,
         ]
     };
 
-    trait_def.expand(cx, mitem, in_items)
+    trait_def.expand(cx, mitem, item, push)
 }
 
 fn iter_bytes_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> @Expr {
index 01e31fc5724d20b1200353c200a1c8ff322f5f66..62408d79ee31e077d2cbca3793fa5e0558b86950 100644 (file)
 pub fn expand_meta_deriving(cx: &mut ExtCtxt,
                             _span: Span,
                             mitem: @MetaItem,
-                            in_items: ~[@Item])
-                         -> ~[@Item] {
+                            item: @Item,
+                            push: |@Item|) {
     match mitem.node {
         MetaNameValue(_, ref l) => {
             cx.span_err(l.span, "unexpected value in `deriving`");
-            in_items
         }
         MetaWord(_) | MetaList(_, []) => {
             cx.span_warn(mitem.span, "empty trait list in `deriving`");
-            in_items
         }
         MetaList(_, ref titems) => {
-            titems.rev_iter().fold(in_items, |in_items, &titem| {
+            for &titem in titems.rev_iter() {
                 match titem.node {
                     MetaNameValue(ref tname, _) |
                     MetaList(ref tname, _) |
                     MetaWord(ref tname) => {
                         macro_rules! expand(($func:path) => ($func(cx, titem.span,
-                                                                   titem, in_items)));
+                                                                   titem, item,
+                                                                   |i| push(i))));
                         match tname.get() {
                             "Clone" => expand!(clone::expand_deriving_clone),
                             "DeepClone" => expand!(clone::expand_deriving_deep_clone),
@@ -94,12 +93,11 @@ macro_rules! expand(($func:path) => ($func(cx, titem.span,
                             ref tname => {
                                 cx.span_err(titem.span, format!("unknown \
                                     `deriving` trait: `{}`", *tname));
-                                in_items
                             }
-                        }
+                        };
                     }
                 }
-            })
+            }
         }
     }
 }
index 1b356667b6b313da7e7cc7137e9223e486773fe6..9391c9cd23c7400e912eacdef606baff32a4fa71 100644 (file)
@@ -19,7 +19,8 @@
 pub fn expand_deriving_from_primitive(cx: &mut ExtCtxt,
                                       span: Span,
                                       mitem: @MetaItem,
-                                      in_items: ~[@Item]) -> ~[@Item] {
+                                      item: @Item,
+                                      push: |@Item|) {
     let trait_def = TraitDef {
         span: span,
         path: Path::new(~["std", "num", "FromPrimitive"]),
@@ -61,7 +62,7 @@ pub fn expand_deriving_from_primitive(cx: &mut ExtCtxt,
         ]
     };
 
-    trait_def.expand(cx, mitem, in_items)
+    trait_def.expand(cx, mitem, item, push)
 }
 
 fn cs_from(name: &str, cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> @Expr {
index ef7bd7c2bcdec2ffae56a6dd9d544d3146e4afcc..351264fed72af051efd51dafe1c7624715323a7c 100644 (file)
@@ -19,8 +19,8 @@
 pub fn expand_deriving_rand(cx: &mut ExtCtxt,
                             span: Span,
                             mitem: @MetaItem,
-                            in_items: ~[@Item])
-                            -> ~[@Item] {
+                            item: @Item,
+                            push: |@Item|) {
     let trait_def = TraitDef {
         span: span,
         path: Path::new(~["std", "rand", "Rand"]),
@@ -46,7 +46,7 @@ pub fn expand_deriving_rand(cx: &mut ExtCtxt,
             }
         ]
     };
-    trait_def.expand(cx, mitem, in_items)
+    trait_def.expand(cx, mitem, item, push)
 }
 
 fn rand_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> @Expr {
index e2d507f30351054190ddd95baa8de70a65d42737..984be122fe8a44f906cf2906ca2f342cf02c6a92 100644 (file)
@@ -23,8 +23,8 @@
 pub fn expand_deriving_show(cx: &mut ExtCtxt,
                             span: Span,
                             mitem: @MetaItem,
-                            in_items: ~[@Item])
-                            -> ~[@Item] {
+                            item: @Item,
+                            push: |@Item|) {
     // &mut ::std::fmt::Formatter
     let fmtr = Ptr(~Literal(Path::new(~["std", "fmt", "Formatter"])),
                    Borrowed(None, ast::MutMutable));
@@ -47,7 +47,7 @@ pub fn expand_deriving_show(cx: &mut ExtCtxt,
             }
         ]
     };
-    trait_def.expand(cx, mitem, in_items)
+    trait_def.expand(cx, mitem, item, push)
 }
 
 // we construct a format string and then defer to std::fmt, since that
index e5145fb15f7a1393568c228c38ac5cec833f5c16..1efcf61013f6af2ce1ce99122908b1c54713eea8 100644 (file)
@@ -20,8 +20,8 @@
 pub fn expand_deriving_to_str(cx: &mut ExtCtxt,
                               span: Span,
                               mitem: @MetaItem,
-                              in_items: ~[@Item])
-                              -> ~[@Item] {
+                              item: @Item,
+                              push: |@Item|) {
     let trait_def = TraitDef {
         span: span,
         path: Path::new(~["std", "to_str", "ToStr"]),
@@ -40,7 +40,7 @@ pub fn expand_deriving_to_str(cx: &mut ExtCtxt,
             }
         ]
     };
-    trait_def.expand(cx, mitem, in_items)
+    trait_def.expand(cx, mitem, item, push)
 }
 
 // It used to be the case that this deriving implementation invoked
index ca5c1543d88dc66f5e30772e8199e7beccbdd1c0..924ab3c9e0020c4f2df93be6ab90ddc6c79061e1 100644 (file)
@@ -17,8 +17,8 @@
 pub fn expand_deriving_zero(cx: &mut ExtCtxt,
                             span: Span,
                             mitem: @MetaItem,
-                            in_items: ~[@Item])
-                            -> ~[@Item] {
+                            item: @Item,
+                            push: |@Item|) {
     let trait_def = TraitDef {
         span: span,
         path: Path::new(~["std", "num", "Zero"]),
@@ -53,7 +53,7 @@ pub fn expand_deriving_zero(cx: &mut ExtCtxt,
             }
         ]
     };
-    trait_def.expand(cx, mitem, in_items)
+    trait_def.expand(cx, mitem, item, push)
 }
 
 fn zero_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> @Expr {
index d96fc27f3ddfe3bd1b3dd55a28ba701c93b6a8c6..fc13207563c9526ad5331f67181c4f3cb387cccf 100644 (file)
@@ -29,7 +29,6 @@
 use util::small_vector::SmallVector;
 
 use std::cast;
-use std::vec;
 use std::unstable::dynamic_lib::DynamicLibrary;
 use std::os;
 
@@ -220,8 +219,9 @@ pub fn expand_mod_items(module_: &ast::Mod, fld: &mut MacroExpander) -> ast::Mod
     // For each item, look through the attributes.  If any of them are
     // decorated with "item decorators", then use that function to transform
     // the item into a new set of items.
-    let new_items = vec::flat_map(module_.items, |item| {
-        item.attrs.rev_iter().fold(~[*item], |items, attr| {
+    let mut new_items = module_.items.clone();
+    for item in module_.items.iter() {
+        for attr in item.attrs.rev_iter() {
             let mname = attr.name();
 
             match fld.extsbox.find(&intern(mname.get())) {
@@ -234,14 +234,14 @@ pub fn expand_mod_items(module_: &ast::Mod, fld: &mut MacroExpander) -> ast::Mod
                           span: None
                       }
                   });
-                  let r = dec_fn(fld.cx, attr.span, attr.node.value, items);
+                  dec_fn(fld.cx, attr.span, attr.node.value, *item,
+                         |item| new_items.push(item));
                   fld.cx.bt_pop();
-                  r
               },
-              _ => items,
+              _ => {},
             }
-        })
-    });
+        }
+    }
 
     ast::Mod {
         items: new_items,