[svn] r6201: nemerle/trunk: lib/internal-array.n macros/core.n ncc/Makefile ncc/external/InternalTypes.n n...

malekith svnadmin at nemerle.org
Wed Apr 26 19:31:22 CEST 2006


Log:
Allow extension methods on arrays.

Author: malekith
Date: Wed Apr 26 19:31:20 2006
New Revision: 6201

Added:
   nemerle/trunk/lib/internal-array.n
Modified:
   nemerle/trunk/macros/core.n
   nemerle/trunk/ncc/Makefile
   nemerle/trunk/ncc/external/InternalTypes.n
   nemerle/trunk/ncc/external/LibrariesLoader.n
   nemerle/trunk/ncc/hierarchy/ExtensionMethod.n
   nemerle/trunk/ncc/testsuite/positive/extension-methods-lib.n
   nemerle/trunk/ncc/testsuite/positive/extension-methods.n
   nemerle/trunk/ncc/typing/MType.n
   nemerle/trunk/ncc/typing/Typer.n

Added: nemerle/trunk/lib/internal-array.n
==============================================================================
--- (empty file)
+++ nemerle/trunk/lib/internal-array.n	Wed Apr 26 19:31:20 2006
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2006 The University of Wroclaw.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *    1. Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *    2. Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in the
+ *       documentation and/or other materials provided with the distribution.
+ *    3. The name of the University may not be used to endorse or promote
+ *       products derived from this software without specific prior
+ *       written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
+ * NO EVENT SHALL THE UNIVERSITY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+// fake type used for extension methods on arrays
+
+namespace Nemerle.Builtins {
+
+[Nemerle.Extensions.DisableImplicitConstructor]
+public abstract class Array1 [T] : System.Array { }
+
+[Nemerle.Extensions.DisableImplicitConstructor]
+public abstract class Array2 [T] : System.Array { }
+
+[Nemerle.Extensions.DisableImplicitConstructor]
+public abstract class Array3 [T] : System.Array { }
+
+[Nemerle.Extensions.DisableImplicitConstructor]
+public abstract class Array4 [T] : System.Array { }
+
+[Nemerle.Extensions.DisableImplicitConstructor]
+public abstract class Array5 [T] : System.Array { }
+
+[Nemerle.Extensions.DisableImplicitConstructor]
+public abstract class Array6 [T] : System.Array { }
+
+[Nemerle.Extensions.DisableImplicitConstructor]
+public abstract class Array7 [T] : System.Array { }
+
+[Nemerle.Extensions.DisableImplicitConstructor]
+public abstract class Array8 [T] : System.Array { }
+
+[Nemerle.Extensions.DisableImplicitConstructor]
+public abstract class Array9 [T] : System.Array { }
+
+[Nemerle.Extensions.DisableImplicitConstructor]
+public abstract class Array10 [T] : System.Array { }
+
+}

Modified: nemerle/trunk/macros/core.n
==============================================================================
--- nemerle/trunk/macros/core.n	(original)
+++ nemerle/trunk/macros/core.n	Wed Apr 26 19:31:20 2006
@@ -707,7 +707,8 @@
        Message.Error ("extension methods need to be marked public static")
      else
        match (p.ty.Fix ()) {
-         | Class (tc, _) =>
+         | Class (tc, _) 
+         | Array (_, n) with tc = InternalType.GetArrayType (n) =>
            add_attr (tb.GetModifiers ());
            add_attr (mb.GetModifiers ());
            tc.AddExtensionMethod (mb);

Modified: nemerle/trunk/ncc/Makefile
==============================================================================
--- nemerle/trunk/ncc/Makefile	(original)
+++ nemerle/trunk/ncc/Makefile	Wed Apr 26 19:31:20 2006
@@ -44,6 +44,7 @@
 
 NEMERLE_DLL_SRC = \
 	../lib/internal-numbered.n	\
+	../lib/internal-array.n		\
 	../lib/internal.n		\
 	../lib/core.n			\
 	../lib/macros.n			\

Modified: nemerle/trunk/ncc/external/InternalTypes.n
==============================================================================
--- nemerle/trunk/ncc/external/InternalTypes.n	(original)
+++ nemerle/trunk/ncc/external/InternalTypes.n	Wed Apr 26 19:31:20 2006
@@ -409,6 +409,7 @@
 
   mutable function_types : array [FunctionType];
   mutable tuple_types : array [TupleType];
+  mutable array_types : array [TypeInfo];
 
   public mutable Void : MType.Void;
   public mutable Array : MType.Class;
@@ -503,6 +504,13 @@
     tuple_types [len]
   }
 
+  public GetArrayType (dims : int) : TypeInfo
+  {
+    when (array_types [dims] == null)
+      array_types [dims] = lookup ($ "Nemerle.Builtins.Array$dims");
+    array_types [dims]
+  }
+
 
   lookup (type_name : string) : TypeInfo
   {
@@ -586,6 +594,7 @@
     
     function_types = array (20);
     tuple_types = array (20);
+    array_types = array (20);
 
     InternalType.MatchFailureException_tc = null; // cleanup
   }

Modified: nemerle/trunk/ncc/external/LibrariesLoader.n
==============================================================================
--- nemerle/trunk/ncc/external/LibrariesLoader.n	(original)
+++ nemerle/trunk/ncc/external/LibrariesLoader.n	Wed Apr 26 19:31:20 2006
@@ -1284,7 +1284,8 @@
             foreach (m is IMethod in GetMembers ())
               when (m.IsStatic && m.GetHandle ().IsDefined (t, false))
                 match (m.GetParameters ().Head.ty.Fix ()) {
-                  | Class (tc, _) =>
+                  | Class (tc, _)
+                  | Array (_, n) with tc = InternalType.GetArrayType (n) =>
                     tc.AddExtensionMethod (m);
                   | _ => Util.ice ()
                 }

Modified: nemerle/trunk/ncc/hierarchy/ExtensionMethod.n
==============================================================================
--- nemerle/trunk/ncc/hierarchy/ExtensionMethod.n	(original)
+++ nemerle/trunk/ncc/hierarchy/ExtensionMethod.n	Wed Apr 26 19:31:20 2006
@@ -49,7 +49,9 @@
       match (h.parms) {
         | first_parm :: rest_of_parms =>
           match (first_parm.ty.Fix ()) {
-            | MType.Class (tc, tcparms) =>
+            | MType.Class (tc, tcparms)
+            | MType.Array (t, n) with (tc = InternalType.GetArrayType (n),
+                                       tcparms = [t]) =>
               assert (parent.Equals (tc));
               if (tcparms.Length > h.typarms.Length ||
                   List.Exists2 (tcparms, h.typarms.FirstN (tcparms.Length),

Modified: nemerle/trunk/ncc/testsuite/positive/extension-methods-lib.n
==============================================================================
--- nemerle/trunk/ncc/testsuite/positive/extension-methods-lib.n	(original)
+++ nemerle/trunk/ncc/testsuite/positive/extension-methods-lib.n	Wed Apr 26 19:31:20 2006
@@ -1,6 +1,7 @@
 // OPTIONS: -t:library
 #pragma indent
 using System.Console
+using Nemerle.Collections
 
 public class A
   public v : int
@@ -24,6 +25,10 @@
     a.Describe ()
     System.Console.WriteLine ($"$(typeof(C))")
 
+  public static Rev [T] (this a : array [T]) : void
+    foreach (i in [0 .. a.Length / 2])
+      a [i] <-> a [a.Length - i - 1]
+
   public static Run () : void
     def a = A (42)
     a.foo ()
@@ -39,6 +44,12 @@
     g.bar ()
     g.bar (7.0)
 
+    def a = array [3, 14, 15]
+    a.Rev ()
+    System.Console.WriteLine (List.FromArray (a))
+    B.Rev (a)
+    System.Console.WriteLine (List.FromArray (a))
+
 public class G[X, Y]
   public Describe () : void
     System.Console.WriteLine ($"$(typeof(X)) $(typeof(Y))")

Modified: nemerle/trunk/ncc/testsuite/positive/extension-methods.n
==============================================================================
--- nemerle/trunk/ncc/testsuite/positive/extension-methods.n	(original)
+++ nemerle/trunk/ncc/testsuite/positive/extension-methods.n	Wed Apr 26 19:31:20 2006
@@ -1,6 +1,7 @@
 // OPTIONS: -r:extension-methods-lib.dll
 #pragma indent
 using System.Console
+using Nemerle.Collections
 
 B.Run ()
 def a = A (42)
@@ -13,6 +14,12 @@
 g.bar ()
 g.bar (7.0)
 
+def a = array [3, 14, 15]
+a.Rev ()
+System.Console.WriteLine (List.FromArray (a))
+B.Rev (a)
+System.Console.WriteLine (List.FromArray (a))
+
 /*
 BEGIN-OUTPUT
 foo()
@@ -24,6 +31,8 @@
 System.Int32 System.String
 System.Int32 System.String
 System.Double
+[15, 14, 3]
+[3, 14, 15]
 foo()
 foo(42,1)
 foo(42,2)
@@ -31,5 +40,7 @@
 System.Int32 System.String
 System.Int32 System.String
 System.Double
+[15, 14, 3]
+[3, 14, 15]
 END-OUTPUT
 */

Modified: nemerle/trunk/ncc/typing/MType.n
==============================================================================
--- nemerle/trunk/ncc/typing/MType.n	(original)
+++ nemerle/trunk/ncc/typing/MType.n	Wed Apr 26 19:31:20 2006
@@ -299,10 +299,11 @@
         //| (Class (tc, _), Void) when tc.Equals (InternalType.Void_tc)
         | (Void, Void) => true
 
-        | (Array (t, _), Class (tc, [t'])) =>
+        | (Array (t, n), Class (tc, [t'])) =>
          (tc.Equals (InternalType.Generic_IEnumerable_tc) ||
           tc.Equals (InternalType.Generic_IList_tc) ||
-          tc.Equals (InternalType.Generic_ICollection_tc)) && t.Unify (t')
+          tc.Equals (InternalType.Generic_ICollection_tc) ||
+          tc.Equals (InternalType.GetArrayType (n))) && t.Unify (t')
       
         | (Array, Class (_, []) as ct) =>
           InternalType.Array.Require (ct)
@@ -809,8 +810,8 @@
           | Fun =>
             FunctionType.Make (this).GetInstantiatedSuperType (super_type)
 
-          | Array =>
-            InternalType.Array.GetInstantiatedSuperType (super_type)
+          | Array (t, n) =>
+            MType.Class (InternalType.GetArrayType (n), [t]).GetInstantiatedSuperType (super_type)
 
           | _ => Util.ice ($ "GIST: $this for $super_type")
         }

Modified: nemerle/trunk/ncc/typing/Typer.n
==============================================================================
--- nemerle/trunk/ncc/typing/Typer.n	(original)
+++ nemerle/trunk/ncc/typing/Typer.n	Wed Apr 26 19:31:20 2006
@@ -2309,7 +2309,9 @@
             lookup (InternalType.Object);
             foreach (t in tv.Constraints) lookup (t)
             
-          | MType.Array => lookup (InternalType.Array)
+          | MType.Array (t, n) => 
+            lookup (MType.Class (InternalType.GetArrayType (n), [t]))
+
           | MType.Intersection (lst) => foreach (t in lst) lookup (t)
           | MType.Tuple
           | MType.Fun => lookup (InternalType.Object)



More information about the svn mailing list