about summary refs log tree commit diff
path: root/src/libsyntax/ext/deriving/cmp
diff options
context:
space:
mode:
authorHuon Wilson <dbau.pp+github@gmail.com>2013-05-07 01:23:51 +1000
committerHuon Wilson <dbau.pp+github@gmail.com>2013-05-07 01:33:54 +1000
commitb20eb970e18377191465cb37f2074f467dfd61b1 (patch)
tree11ac991d67a5bfef9022d90bc2715406e925d8c3 /src/libsyntax/ext/deriving/cmp
parent6e6a4be19d8e6a2cedc66be6cc602db8a1e71acd (diff)
downloadrust-b20eb970e18377191465cb37f2074f467dfd61b1.tar.gz
rust-b20eb970e18377191465cb37f2074f467dfd61b1.zip
libsyntax: extend generic deriving code to handle almost all possible traits.
This adds support for static methods, and arguments of most types, traits with
type parameters, methods with type parameters (and lifetimes for both), as well
as making the code more robust to support deriving on types with lifetimes (i.e.
'self).
Diffstat (limited to 'src/libsyntax/ext/deriving/cmp')
-rw-r--r--src/libsyntax/ext/deriving/cmp/eq.rs19
-rw-r--r--src/libsyntax/ext/deriving/cmp/ord.rs42
-rw-r--r--src/libsyntax/ext/deriving/cmp/totaleq.rs13
-rw-r--r--src/libsyntax/ext/deriving/cmp/totalord.rs12
4 files changed, 47 insertions, 39 deletions
diff --git a/src/libsyntax/ext/deriving/cmp/eq.rs b/src/libsyntax/ext/deriving/cmp/eq.rs
index c0060cc67dc..e431e1f78bf 100644
--- a/src/libsyntax/ext/deriving/cmp/eq.rs
+++ b/src/libsyntax/ext/deriving/cmp/eq.rs
@@ -8,15 +8,12 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-
 use ast::{meta_item, item, expr};
 use codemap::span;
 use ext::base::ext_ctxt;
 use ext::build;
 use ext::deriving::generic::*;
 
-use core::option::Some;
-
 pub fn expand_deriving_eq(cx: @ext_ctxt,
                           span: span,
                           mitem: @meta_item,
@@ -24,28 +21,32 @@ pub fn expand_deriving_eq(cx: @ext_ctxt,
     // 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: @ext_ctxt, span: span, substr: &Substructure) -> @expr {
-        cs_and(|cx, span, _| build::mk_bool(cx, span, false),
+        cs_and(|cx, span, _, _| build::mk_bool(cx, span, false),
                                  cx, span, substr)
     }
     fn cs_ne(cx: @ext_ctxt, span: span, substr: &Substructure) -> @expr {
-        cs_or(|cx, span, _| build::mk_bool(cx, span, true),
+        cs_or(|cx, span, _, _| build::mk_bool(cx, span, true),
               cx, span, substr)
     }
+
     macro_rules! md (
         ($name:expr, $f:ident) => {
             MethodDef {
                 name: $name,
-                output_type: Some(~[~"bool"]),
-                nargs: 1,
+                generics: LifetimeBounds::empty(),
+                self_ty: borrowed_explicit_self(),
+                args: ~[borrowed_self()],
+                ret_ty: Literal(Path::new(~[~"bool"])),
                 const_nonmatching: true,
                 combine_substructure: $f
             },
         }
-    )
+    );
 
     let trait_def = TraitDef {
-        path: ~[~"core", ~"cmp", ~"Eq"],
+        path: Path::new(~[~"core", ~"cmp", ~"Eq"]),
         additional_bounds: ~[],
+        generics: LifetimeBounds::empty(),
         methods: ~[
             md!(~"eq", cs_eq),
             md!(~"ne", cs_ne)
diff --git a/src/libsyntax/ext/deriving/cmp/ord.rs b/src/libsyntax/ext/deriving/cmp/ord.rs
index 398e27eb3e3..5998fc7145d 100644
--- a/src/libsyntax/ext/deriving/cmp/ord.rs
+++ b/src/libsyntax/ext/deriving/cmp/ord.rs
@@ -14,29 +14,33 @@ use codemap::span;
 use ext::base::ext_ctxt;
 use ext::build;
 use ext::deriving::generic::*;
-use core::option::Some;
-
-macro_rules! md {
-    ($name:expr, $less:expr, $equal:expr) => {
-        MethodDef {
-            name: $name,
-            output_type: Some(~[~"bool"]),
-            nargs: 1,
-            const_nonmatching: false,
-            combine_substructure: |cx, span, substr|
-                    cs_ord($less, $equal, cx, span, substr)
-        }
-    }
-}
 
 pub fn expand_deriving_ord(cx: @ext_ctxt,
                            span: span,
                            mitem: @meta_item,
                            in_items: ~[@item]) -> ~[@item] {
+    macro_rules! md (
+        ($name:expr, $less:expr, $equal:expr) => {
+            MethodDef {
+                name: $name,
+                generics: LifetimeBounds::empty(),
+                self_ty: borrowed_explicit_self(),
+                args: ~[borrowed_self()],
+                ret_ty: Literal(Path::new(~[~"bool"])),
+                const_nonmatching: false,
+                combine_substructure: |cx, span, substr|
+                    cs_ord($less, $equal, cx, span, substr)
+            }
+        }
+    );
+
+
+
     let trait_def = TraitDef {
-        path: ~[~"core", ~"cmp", ~"Ord"],
+        path: Path::new(~[~"core", ~"cmp", ~"Ord"]),
         // XXX: Ord doesn't imply Eq yet
-        additional_bounds: ~[~[~"core", ~"cmp", ~"Eq"]],
+        additional_bounds: ~[Literal(Path::new(~[~"core", ~"cmp", ~"Eq"]))],
+        generics: LifetimeBounds::empty(),
         methods: ~[
             md!(~"lt", true,  false),
             md!(~"le", true,  true),
@@ -97,19 +101,19 @@ fn cs_ord(less: bool, equal: bool,
             }
 
             let cmp = build::mk_method_call(cx, span,
-                                            self_f, cx.ident_of(~"eq"), other_fs);
+                                            self_f, cx.ident_of(~"eq"), other_fs.to_owned());
             let subexpr = build::mk_simple_block(cx, span, subexpr);
             let elseif = expr_if(cmp, subexpr, Some(false_blk_expr));
             let elseif = build::mk_expr(cx, span, elseif);
 
             let cmp = build::mk_method_call(cx, span,
-                                            self_f, binop, other_fs);
+                                            self_f, binop, other_fs.to_owned());
             let if_ = expr_if(cmp, true_blk, Some(elseif));
 
             build::mk_expr(cx, span, if_)
         },
         base,
-        |cx, span, args| {
+        |cx, span, args, _| {
             // nonmatching enums, order by the order the variants are
             // written
             match args {
diff --git a/src/libsyntax/ext/deriving/cmp/totaleq.rs b/src/libsyntax/ext/deriving/cmp/totaleq.rs
index fc8ec103a60..068a7bc06b1 100644
--- a/src/libsyntax/ext/deriving/cmp/totaleq.rs
+++ b/src/libsyntax/ext/deriving/cmp/totaleq.rs
@@ -15,26 +15,27 @@ use ext::base::ext_ctxt;
 use ext::build;
 use ext::deriving::generic::*;
 
-use core::option::Some;
-
 pub fn expand_deriving_totaleq(cx: @ext_ctxt,
                           span: span,
                           mitem: @meta_item,
                           in_items: ~[@item]) -> ~[@item] {
 
     fn cs_equals(cx: @ext_ctxt, span: span, substr: &Substructure) -> @expr {
-        cs_and(|cx, span, _| build::mk_bool(cx, span, false),
+        cs_and(|cx, span, _, _| build::mk_bool(cx, span, false),
                cx, span, substr)
     }
 
     let trait_def = TraitDef {
-        path: ~[~"core", ~"cmp", ~"TotalEq"],
+        path: Path::new(~[~"core", ~"cmp", ~"TotalEq"]),
         additional_bounds: ~[],
+        generics: LifetimeBounds::empty(),
         methods: ~[
             MethodDef {
                 name: ~"equals",
-                output_type: Some(~[~"bool"]),
-                nargs: 1,
+                generics: LifetimeBounds::empty(),
+                self_ty: borrowed_explicit_self(),
+                args: ~[borrowed_self()],
+                ret_ty: Literal(Path::new(~[~"bool"])),
                 const_nonmatching: true,
                 combine_substructure: cs_equals
             }
diff --git a/src/libsyntax/ext/deriving/cmp/totalord.rs b/src/libsyntax/ext/deriving/cmp/totalord.rs
index a098a7463d3..ac873c5bd12 100644
--- a/src/libsyntax/ext/deriving/cmp/totalord.rs
+++ b/src/libsyntax/ext/deriving/cmp/totalord.rs
@@ -14,20 +14,22 @@ use ext::base::ext_ctxt;
 use ext::build;
 use ext::deriving::generic::*;
 use core::cmp::{Ordering, Equal, Less, Greater};
-use core::option::Some;
 
 pub fn expand_deriving_totalord(cx: @ext_ctxt,
                                 span: span,
                                 mitem: @meta_item,
                                 in_items: ~[@item]) -> ~[@item] {
     let trait_def = TraitDef {
-        path: ~[~"core", ~"cmp", ~"TotalOrd"],
+        path: Path::new(~[~"core", ~"cmp", ~"TotalOrd"]),
         additional_bounds: ~[],
+        generics: LifetimeBounds::empty(),
         methods: ~[
             MethodDef {
                 name: ~"cmp",
-                output_type: Some(~[~"core", ~"cmp", ~"Ordering"]),
-                nargs: 1,
+                generics: LifetimeBounds::empty(),
+                self_ty: borrowed_explicit_self(),
+                args: ~[borrowed_self()],
+                ret_ty: Literal(Path::new(~[~"core", ~"cmp", ~"Ordering"])),
                 const_nonmatching: false,
                 combine_substructure: cs_cmp
             }
@@ -64,7 +66,7 @@ pub fn cs_cmp(cx: @ext_ctxt, span: span,
             build::mk_call_global(cx, span, lexical_ord, ~[old, new])
         },
         ordering_const(cx, span, Equal),
-        |cx, span, list| {
+        |cx, span, list, _| {
             match list {
                 // an earlier nonmatching variant is Less than a
                 // later one