[svn] r7390: nemerle/trunk/ncc/hierarchy/XmlDump.n

pbludov svnadmin at nemerle.org
Tue Feb 6 08:29:28 CET 2007


Log:
XmlDoc.GetKey optimization.

Author: pbludov
Date: Tue Feb  6 08:29:26 2007
New Revision: 7390

Modified:
   nemerle/trunk/ncc/hierarchy/XmlDump.n

Modified: nemerle/trunk/ncc/hierarchy/XmlDump.n
==============================================================================
--- nemerle/trunk/ncc/hierarchy/XmlDump.n	(original)
+++ nemerle/trunk/ncc/hierarchy/XmlDump.n	Tue Feb  6 08:29:26 2007
@@ -32,6 +32,7 @@
 using Nemerle.Compiler;
 using Nemerle.Compiler.Parsetree;
 using Nemerle.Utility;
+using Nemerle.Utility.Text;
 
 using System;
 using System.Xml;
@@ -384,120 +385,94 @@
       List.Iter (mems, DumpMember);
     }
 
-    public static GetKey (m : IMember) : string {
-
-      // Since typical member key has at least four parts
-      // (prefix, declaring type, dot, name) it is good
-      // to use a string builder here.
-      //
-      def sb = StringBuilder ();
-
-      // Append a list of something.
-      // The comma symbol will be inserted between elements.
-      //
-      def appendList['a] (lst : list['a], action : 'a -> void) : void {
-        match (lst) {
-          | Nil      => ()
-          | x :: Nil => action (x);
-          | x :: xs  =>
-            action (x);
-            ignore (sb.Append (","));
-            appendList (xs, action);
-          }
-      }
-
       // Append a method parameter type name.
       //
-      def appendParmTypeName (p : TyVar, m : IMethod) : void {
-
-        // Append a generic parameter type name,
-        // which is an index actually.
+    static AppendParmTypeName (sb : StringBuilder, p : TyVar, m : IMethod) : StringBuilder
+    {
+      // Append a generic parameter type name, which is an index actually.
         //
-        def appendStaticTyVarName (tyvar : StaticTyVar) : void {
-          def indexOf ['a] (l : list ['a], a : 'a) : int {
-            def loop (l, a, idx) {
-              match (l) {
-                | h :: t  =>
-                  if (h.Equals (a))
-                    idx
-                  else
-                    loop (t, a, idx + 1)
-                | [] => -1
-                }
+      def appendStaticTyVarName (sb, tyvar) {
+
+        def indexOf (mutable lst, elem) {
+          mutable i = 0;
+          while (!lst.IsEmpty && !lst.Head.Equals (elem)) {
+            ++i;
+            lst = lst.Tail;
             }
 
-            loop (l, a, 0)
+          if (lst.IsEmpty)
+            -1
+          else
+            i
           }
 
           match (indexOf (m.GetHeader ().typarms, tyvar)) {
             | -1 =>
               match (indexOf (m.DeclaringType.Typarms, tyvar)) {
-                | -1 => throw ArgumentException ("Unknown type parameter"); // Should never be happen.
-                | x  => ignore (sb.Append ("`").Append (x)); // The declaring type generic parameter index.
+              | -1 =>
+                Message.Warning ($"Unknown type parameter $tyvar"); // Should never be happen.
+                sb.Append ("???");
+              | x  => sb.Append ("`").Append (x); // The declaring type generic parameter index.
                 }
-            | x  => ignore (sb.Append ("``").Append (x)); // The method generic parameter index.
+          | x  => sb.Append ("``").Append (x); // The method generic parameter index.
           }
         }
 
         // Append type name and, optional, generic argument names.
         //
-        def appendTypeNameAndArgs (typeName : string, args : list[TyVar]) : void {
-          ignore (sb.Append (typeName));
-          unless (args.IsEmpty) {
-            ignore (sb.Append ("{"));
-            appendList (args, e => appendParmTypeName (e, m));
-            ignore (sb.Append ("}"));
-          }
+      def appendTypeNameAndArgs (sb, typeName, args) {
+         sb.Append (typeName)
+           .AppendUnless (args.IsEmpty, sb => sb.Append ("{").AppendList (args, (sb, e) => AppendParmTypeName (sb, e, m), ",").Append ("}"));
         }
 
-        match (p.Fix ()) {
-          | Class       (tycon, args) => appendTypeNameAndArgs (tycon.FullName, args);
-          | TyVarRef    (tyvar)       => appendStaticTyVarName (tyvar);
-          | Array       (t, rank)     =>
-            appendParmTypeName (t, m);
-            if (rank > 1) {
-              ignore (sb.Append ("[0:"));
-              repeat (rank - 1)
-                ignore (sb.Append (",0:"));
-              ignore (sb.Append ("]"));
-            }
-            else
-              ignore (sb.Append ("[]"));
-
-          | Ref         (t)
-          | Out         (t)           => appendParmTypeName (t, m);
-          | Tuple       (args)        => appendTypeNameAndArgs ("Nemerle.Builtins.Tuple", args);
-          | Fun         (from, to)    =>
+      // Append function argument types & return type.
+      //
+      def appendFunctionTypeArgs (sb, from, to) {
             def args = match (from.Fix ()) {
-              | Void         => [];
-              | Tuple (args) => args;
+          | MType.Void         => [];
+          | MType.Tuple (args) => args;
               | _            => [from];
               }
             match (to.Fix ()) {
-              | Void => appendTypeNameAndArgs ("Nemerle.Builtins.FunctionVoid", args);
-              | _    => appendTypeNameAndArgs ("Nemerle.Builtins.Function", args.Append ([to]));
+          | MType.Void => appendTypeNameAndArgs (sb, "Nemerle.Builtins.FunctionVoid", args);
+          | _          => appendTypeNameAndArgs (sb, "Nemerle.Builtins.Function", args.Append ([to]));
+          }
               }
 
+      match (p.Fix ()) {
+        | Class       (tycon, args) => appendTypeNameAndArgs  (sb, tycon.FullName, args);
+        | TyVarRef    (tyvar)       => appendStaticTyVarName  (sb, tyvar);
+        | Array       (t, rank)     => AppendParmTypeName     (sb, t, m).Append ("[").AppendNTimes (rank, "0:", ",").Append ("]");
+        | Tuple       (args)        => appendTypeNameAndArgs  (sb, "Nemerle.Builtins.Tuple", args);
+        | Fun         (from, to)    => appendFunctionTypeArgs (sb, from, to);
+        | Ref         (t)
+        | Out         (t)           => AppendParmTypeName     (sb, t, m);
           | Void
-          | Intersection              => throw InvalidOperationException ("Invalid MType for doc comment");
+        | Intersection              => Util.ice ($"Invalid MType for doc comment: `$p'");
           }
       }
 
-      def appendTypeName (typeInfo : TypeInfo) : void {
+    public static GetKey (m : IMember) : string
+    {
+      // Since typical member key has at least four parts
+      // (prefix, declaring type, dot, name) it is good
+      // to use a string builder here.
+      //
+      def sb = StringBuilder ();
+
+      def appendTypeName (sb, typeInfo) {
         def tyParmsCount = typeInfo.Typarms.Length;
 
-        ignore (sb.Append (typeInfo.FullName));
-        unless (tyParmsCount == 0)
-          ignore (sb.Append ("`").Append (tyParmsCount));
+        sb.Append (typeInfo.FullName)
+          .AppendUnless (tyParmsCount == 0, sb => sb.Append ("`").Append (tyParmsCount));
       }
 
-      match (m.MemberKind) {
+      (match (m.MemberKind) {
         | MemberKinds.Field    with prefix = "F:"
         | MemberKinds.Event    with prefix = "E:"
         | MemberKinds.Property with prefix = "P:" =>
-          ignore (sb.Append (prefix));
-          appendTypeName (m.DeclaringType);
-          ignore (sb.Append (".").Append (m.Name.Replace ('.', '#')));
+          appendTypeName (sb.Append (prefix), m.DeclaringType)
+            .Append (".").Append (m.Name.Replace ('.', '#'));
 
         | MemberKinds.Constructor
         | MemberKinds.Method =>
@@ -505,27 +480,17 @@
           def parms        = method.GetParameters ();
           def tyParmsCount = method.GetHeader ().typarms.Length;
 
-          ignore (sb.Append ("M:"));
-          appendTypeName (method.DeclaringType);
-          ignore (sb.Append (".").Append (m.Name.Replace ('.', '#')));
-
-          unless (tyParmsCount == 0)
-            ignore (sb.Append ("``").Append (tyParmsCount));
-          unless (parms.IsEmpty) {
-            ignore (sb.Append ("("));
-            appendList (parms, p => appendParmTypeName (p.ty, method));
-            ignore (sb.Append (")"));
-          }
+          appendTypeName (sb.Append ("M:"), method.DeclaringType)
+            .Append (".").Append (m.Name.Replace ('.', '#'))
+            .AppendUnless (tyParmsCount == 0, sb => sb.Append ("``").Append (tyParmsCount))
+            .AppendUnless (parms.IsEmpty, sb => sb.Append ("(").AppendList (parms, (sb, p) => AppendParmTypeName (sb, p.ty, method), ",").Append (")"));
 
         | MemberKinds.TypeInfo
         | MemberKinds.NestedType =>
-          ignore (sb.Append ("T:"));
-          appendTypeName(m :> TypeInfo);
-
-        | other => throw InvalidOperationException ($"Invalid MemberKinds for doc comment: `$other'");
-        }
+          appendTypeName(sb.Append ("T:"), m :> TypeInfo);
 
-      sb.ToString ();
+        | other => Util.ice ($"Invalid MemberKinds for doc comment: `$other'");
+        }).ToString ();
     }
   }
 } // end ns



More information about the svn mailing list