2009-11-23 Marek Safar <marek.safar@gmail.com>
authorMarek Safar <marek.safar@gmail.com>
Mon, 23 Nov 2009 09:05:36 +0000 (09:05 -0000)
committerMarek Safar <marek.safar@gmail.com>
Mon, 23 Nov 2009 09:05:36 +0000 (09:05 -0000)
* cs-tokenizer.cs, support.cs: Use Dictionary instead of Hashtable,
cleanup some obsolete code.

svn path=/trunk/mcs/; revision=146712

mcs/mcs/ChangeLog
mcs/mcs/cs-tokenizer.cs
mcs/mcs/support.cs

index 634992aeef3a92546a25f1d3b6f5a1ab540601dc..a168bd35f5bc09a8eab6dcdd7043594aeff179d2 100644 (file)
@@ -1,3 +1,8 @@
+2009-11-23  Marek Safar  <marek.safar@gmail.com>
+
+       * cs-tokenizer.cs, support.cs: Use Dictionary instead of Hashtable,
+       cleanup some obsolete code.
+
 2009-11-20  Marek Safar  <marek.safar@gmail.com>
 
        * context.cs, expression.cs, ecore.cs, complete.cs: Cleaned up
index feff8405e1833bd4d48f2906238a6ebd026e1eb0..ec1e3933e64df94fb4217a9c627a89a3f06e7378 100644 (file)
@@ -15,6 +15,7 @@
 using System;
 using System.Text;
 using System.Collections;
+using System.Collections.Generic;
 using System.IO;
 using System.Globalization;
 using System.Reflection;
@@ -40,6 +41,34 @@ namespace Mono.CSharp
                        }
                }
 
+               sealed class IdentifiersComparer : IEqualityComparer<char[]>
+               {
+                       readonly int length;
+
+                       public IdentifiersComparer (int length)
+                       {
+                               this.length = length;
+                       }
+
+                       public bool Equals (char[] x, char[] y)
+                       {
+                               for (int i = 0; i < length; ++i)
+                                       if (x [i] != y [i])
+                                               return false;
+
+                               return true;
+                       }
+
+                       public int GetHashCode (char[] obj)
+                       {
+                               int h = 0;
+                               for (int i = 0; i < length; ++i)
+                                       h = (h << 5) - h + obj [i];
+
+                               return h;
+                       }
+               }
+
                SeekableStreamReader reader;
                SourceFile ref_name;
                CompilationUnit file_name;
@@ -188,7 +217,7 @@ namespace Mono.CSharp
                // Class variables
                // 
                static KeywordEntry[][] keywords;
-               static Hashtable keyword_strings;
+               static Dictionary<string, object> keyword_strings;              // TODO: HashSet
                static NumberStyles styles;
                static NumberFormatInfo csharp_format_info;
                
@@ -216,11 +245,13 @@ namespace Mono.CSharp
                const int max_id_size = 512;
                static char [] id_builder = new char [max_id_size];
 
-               static CharArrayHashtable [] identifiers = new CharArrayHashtable [max_id_size + 1];
+               public static Dictionary<char[], string>[] identifiers = new Dictionary<char[], string>[max_id_size + 1];
 
                const int max_number_size = 512;
                static char [] number_builder = new char [max_number_size];
                static int number_pos;
+
+               static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
                
                //
                // Details about the error encoutered by the tokenizer
@@ -303,7 +334,7 @@ namespace Mono.CSharp
                
                static void AddKeyword (string kw, int token)
                {
-                       keyword_strings.Add (kw, kw);
+                       keyword_strings.Add (kw, null);
 
                        int length = kw.Length;
                        if (keywords [length] == null) {
@@ -326,7 +357,7 @@ namespace Mono.CSharp
 
                static void InitTokens ()
                {
-                       keyword_strings = new Hashtable ();
+                       keyword_strings = new Dictionary<string, object> ();
 
                        // 11 is the length of the longest keyword for now
                        keywords = new KeywordEntry [11] [];
@@ -661,7 +692,7 @@ namespace Mono.CSharp
 
                public static bool IsKeyword (string s)
                {
-                       return keyword_strings [s] != null;
+                       return keyword_strings.ContainsKey (s);
                }
 
                //
@@ -1125,16 +1156,8 @@ namespace Mono.CSharp
                                                        //
                                                        Report.Warning (78, 4, Location, "The 'l' suffix is easily confused with the digit '1' (use 'L' for clarity)");
                                                }
-                                               //
-                                               // This goto statement causes the MS CLR 2.0 beta 1 csc to report an error, so
-                                               // work around that.
-                                               //
-                                               //goto case 'L';
-                                               if (is_long)
-                                                       scanning = false;
-                                               is_long = true;
-                                               get_char ();
-                                               break;
+
+                                               goto case 'L';
 
                                        case 'L': 
                                                if (is_long)
@@ -1576,8 +1599,6 @@ namespace Mono.CSharp
                        return current_token;
                }
 
-               static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
-
                void get_cmd_arg (out string cmd, out string arg)
                {
                        int c;
@@ -2433,36 +2454,30 @@ namespace Mono.CSharp
                        // Keep identifiers in an array of hashtables to avoid needless
                        // allocations
                        //
-                       CharArrayHashtable identifiers_group = identifiers [pos];
+                       var identifiers_group = identifiers [pos];
+                       string s;
                        if (identifiers_group != null) {
-                               val = identifiers_group [id_builder];
-                               if (val != null) {
-                                       val = new LocatedToken (loc, (string) val);
+                               if (identifiers_group.TryGetValue (id_builder, out s)) {
+                                       val = new LocatedToken (loc, s);
                                        if (quoted)
                                                AddEscapedIdentifier ((LocatedToken) val);
                                        return Token.IDENTIFIER;
                                }
                        } else {
-                               identifiers_group = new CharArrayHashtable (pos);
+                               // TODO: this should be number of files dependant
+                               // corlib compilation peaks at 1000 and System.Core at 150
+                               int capacity = pos > 20 ? 10 : 100;
+                               identifiers_group = new Dictionary<char[],string> (capacity, new IdentifiersComparer (pos));
                                identifiers [pos] = identifiers_group;
                        }
 
                        char [] chars = new char [pos];
                        Array.Copy (id_builder, chars, pos);
 
-                       val = new String (id_builder, 0, pos);
-                       identifiers_group.Add (chars, val);
-
-                       if (RootContext.Version == LanguageVersion.ISO_1) {
-                               for (int i = 1; i < chars.Length; i += 3) {
-                                       if (chars [i] == '_' && (chars [i - 1] == '_' || chars [i + 1] == '_')) {
-                                               Report.Error (1638, loc,
-                                                       "`{0}': Any identifier with double underscores cannot be used when ISO language version mode is specified", val.ToString ());
-                                       }
-                               }
-                       }
+                       s = new string (id_builder, 0, pos);
+                       identifiers_group.Add (chars, s);
 
-                       val = new LocatedToken (loc, (string) val);
+                       val = new LocatedToken (loc, s);
                        if (quoted)
                                AddEscapedIdentifier ((LocatedToken) val);
                        return Token.IDENTIFIER;
index 8c92396bce23d52752738101d22e1b078c4556f8..cdc7e8e9876aa6b58339a8ea9e4a6a8e8dc7b3cc 100644 (file)
@@ -62,49 +62,6 @@ namespace Mono.CSharp {
 #endif
        }
 
-       /*
-        * Hashtable whose keys are character arrays with the same length
-        */
-       class CharArrayHashtable : Hashtable {
-               sealed class ArrComparer : IComparer {
-                       private int len;
-
-                       public ArrComparer (int len) {
-                               this.len = len;
-                       }
-
-                       public int Compare (object x, object y)
-                       {
-                               char[] a = (char[])x;
-                               char[] b = (char[])y;
-
-                               for (int i = 0; i < len; ++i)
-                                       if (a [i] != b [i])
-                                               return 1;
-                               return 0;
-                       }
-               }
-
-               private int len;
-
-               protected override int GetHash (Object key)
-               {
-                       char[] arr = (char[])key;
-                       int h = 0;
-
-                       for (int i = 0; i < len; ++i)
-                               h = (h << 5) - h + arr [i];
-
-                       return h;
-               }
-
-               public CharArrayHashtable (int len)
-               {
-                       this.len = len;
-                       comparer = new ArrComparer (len);
-               }
-       }
-
        struct Pair {
                public object First;
                public object Second;