2010-02-14 Miguel de Icaza <miguel@novell.com>
[mono.git] / mcs / mcs / support.cs
index 76564fe1031b4ebcd4df51a37f2ac5276366c976..bde81d0f9dc44bc48404553faff5e7fb3d1a44a8 100644 (file)
 //
 // Author:
 //   Miguel de Icaza (miguel@ximian.com)
+//   Marek Safar (marek.safar@gmail.com)
 //
 // Copyright 2001 Ximian, Inc (http://www.ximian.com)
-// Copyright 2003-2008 Novell, Inc
+// Copyright 2003-2009 Novell, Inc
 //
 
 using System;
 using System.IO;
 using System.Text;
 using System.Reflection;
-using System.Collections;
 using System.Reflection.Emit;
 using System.Globalization;
+using System.Collections.Generic;
 
 namespace Mono.CSharp {
 
-       class PtrHashtable : Hashtable {
-               sealed class PtrComparer : IComparer, IEqualityComparer
-               {
-                       private PtrComparer () {}
-
-                       public static PtrComparer Instance = new PtrComparer ();
-
-                       public int Compare (object x, object y)
-                       {
-                               if (x == y)
-                                       return 0;
-                               else
-                                       return 1;
-                       }
-
-                       bool IEqualityComparer.Equals (object x, object y)
-                       {
-                               return x == y;
-                       }
-                       
-                       int IEqualityComparer.GetHashCode (object obj)
-                       {
-                               return obj.GetHashCode ();
-                       }
-               }
-
-               public PtrHashtable () : base (PtrComparer.Instance) {}
+       class ReferenceEquality<T> : IEqualityComparer<T> where T : class
+       {
+               public static readonly IEqualityComparer<T> Default = new ReferenceEquality<T> ();
 
-#if MS_COMPATIBLE
-               //
-               // Workaround System.InvalidOperationException for enums
-               //
-               protected override int GetHash (object key)
+               private ReferenceEquality ()
                {
-                       TypeBuilder tb = key as TypeBuilder;
-                       if (tb != null && tb.BaseType == TypeManager.enum_type && tb.BaseType != null)
-                               key = tb.BaseType;
-
-                       return base.GetHash (key);
                }
-#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)
+               public bool Equals (T x, T y)
                {
-                       char[] arr = (char[])key;
-                       int h = 0;
-
-                       for (int i = 0; i < len; ++i)
-                               h = (h << 5) - h + arr [i];
-
-                       return h;
+                       return object.ReferenceEquals (x, y);
                }
 
-               public CharArrayHashtable (int len)
+               public int GetHashCode (T obj)
                {
-                       this.len = len;
-                       comparer = new ArrComparer (len);
+                       return obj == null ? 0 : obj.GetHashCode ();
                }
        }
 
-       struct Pair {
-               public object First;
-               public object Second;
-
-               public Pair (object f, object s)
+       class Tuple<T1, T2>
+       {
+               public Tuple (T1 item1, T2 item2)
                {
-                       First = f;
-                       Second = s;
+                       Item1 = item1;
+                       Item2 = item2;
                }
+
+               public T1 Item1 { get; private set; }
+               public T2 Item2 { get; private set; }
        }
 
        public class Accessors {
@@ -137,36 +72,44 @@ namespace Mono.CSharp {
        ///   but if the seek is too far, it may read the underly
        ///   stream all over from the beginning.
        /// </summary>
-       public class SeekableStreamReader
+       public class SeekableStreamReader : IDisposable
        {
-               const int default_average_read_length = 1024;
                const int buffer_read_length_spans = 3;
 
                TextReader reader;
                Stream stream;
-               Encoding encoding;
 
-               char[] buffer;
+               static char[] buffer;
                int average_read_length;
                int buffer_start;       // in chars
                int char_count;         // count buffer[] valid characters
                int pos;                // index into buffer[]
 
-               void ResetStream (int read_length_inc)
+               public SeekableStreamReader (Stream stream, Encoding encoding)
                {
-                       average_read_length += read_length_inc;
-                       stream.Position = 0;
+                       this.stream = stream;
+
+                       const int default_average_read_length = 1024;
+                       InitializeStream (default_average_read_length);
                        reader = new StreamReader (stream, encoding, true);
-                       buffer = new char [average_read_length * buffer_read_length_spans];
-                       buffer_start = char_count = pos = 0;
                }
 
-               public SeekableStreamReader (Stream stream, Encoding encoding)
+               public void Dispose ()
                {
-                       this.stream = stream;
-                       this.encoding = encoding;
+                       // Needed to release stream reader buffers
+                       reader.Dispose ();
+               }
+
+               void InitializeStream (int read_length_inc)
+               {
+                       average_read_length += read_length_inc;
 
-                       ResetStream (default_average_read_length);
+                       int required_buffer_size = average_read_length * buffer_read_length_spans;
+                       if (buffer == null || buffer.Length < required_buffer_size)
+                               buffer = new char [required_buffer_size];
+
+                       stream.Position = 0;                    
+                       buffer_start = char_count = pos = 0;
                }
 
                /// <remarks>
@@ -182,7 +125,7 @@ namespace Mono.CSharp {
                        set {
                                // If the lookahead was too small, re-read from the beginning.  Increase the buffer size while we're at it
                                if (value < buffer_start)
-                                       ResetStream (average_read_length / 2);
+                                       InitializeStream (average_read_length / 2);
 
                                while (value > buffer_start + char_count) {
                                        pos = char_count;
@@ -401,7 +344,7 @@ namespace Mono.CSharp {
        class DynamicType : Type
        {
                public override Assembly Assembly {
-                       get { return UnderlyingSystemType.Assembly; }
+                       get { return CodeGen.Assembly.Builder; }
                }
 
                public override string AssemblyQualifiedName {
@@ -587,6 +530,213 @@ namespace Mono.CSharp {
                }
        }
 
+#if NET_4_0 || MS_COMPATIBLE
+       [System.Diagnostics.DebuggerDisplay ("Dynamic array type")]
+#endif
+       class DynamicArrayType : Type
+       {
+               readonly int rank;
+               Type reflection_type;
+
+               public DynamicArrayType (int rank)
+               {
+                       this.rank = rank;
+               }
+
+               public override Assembly Assembly {
+                       get { return UnderlyingSystemType.Assembly; }
+               }
+
+               public override string AssemblyQualifiedName {
+                       get { throw new NotImplementedException (); }
+               }
+
+               public override Type BaseType {
+                       get { return TypeManager.array_type; }
+               }
+
+               public override string FullName {
+                       get { return UnderlyingSystemType.FullName; }
+               }
+
+               public override Guid GUID {
+                       get { throw new NotImplementedException (); }
+               }
+
+               protected override TypeAttributes GetAttributeFlagsImpl ()
+               {
+                       return UnderlyingSystemType.Attributes;
+               }
+
+               public override int GetArrayRank ()
+               {
+                       return rank;
+               }
+
+               protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override Type GetElementType ()
+               {
+                       return InternalType.Dynamic;
+               }
+
+               public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override EventInfo[] GetEvents (BindingFlags bindingAttr)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override FieldInfo GetField (string name, BindingFlags bindingAttr)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override FieldInfo[] GetFields (BindingFlags bindingAttr)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override Type GetInterface (string name, bool ignoreCase)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override Type[] GetInterfaces ()
+               {
+                       return Type.EmptyTypes;
+               }
+
+               public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override Type GetNestedType (string name, BindingFlags bindingAttr)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override Type[] GetNestedTypes (BindingFlags bindingAttr)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               protected override bool HasElementTypeImpl ()
+               {
+                       return true;
+               }
+
+               public override object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, System.Globalization.CultureInfo culture, string[] namedParameters)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               protected override bool IsArrayImpl ()
+               {
+                       return true;
+               }
+
+               protected override bool IsByRefImpl ()
+               {
+                       return false;
+               }
+
+               protected override bool IsCOMObjectImpl ()
+               {
+                       return false;
+               }
+
+               protected override bool IsPointerImpl ()
+               {
+                       return false;
+               }
+
+               protected override bool IsPrimitiveImpl ()
+               {
+                       return false;
+               }
+
+               public override Module Module {
+                       get { return UnderlyingSystemType.Module; }
+               }
+
+               public override string Namespace {
+                       get { return UnderlyingSystemType.Namespace; }
+               }
+
+               public override Type UnderlyingSystemType {
+                       get {
+                               if (reflection_type == null) {
+                                       reflection_type = rank == 1 ?
+                                               TypeManager.object_type.MakeArrayType () :
+                                               TypeManager.object_type.MakeArrayType (rank);
+                               }
+
+                               return reflection_type;
+                       }
+               }
+
+               public override object[] GetCustomAttributes (Type attributeType, bool inherit)
+               {
+                       return new object [0];
+               }
+
+               public override object[] GetCustomAttributes (bool inherit)
+               {
+                       return new object [0];
+               }
+
+               public override bool IsDefined (Type attributeType, bool inherit)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override string Name {
+                       get { return UnderlyingSystemType.Name; }
+               }
+
+               public override string ToString ()
+               {
+                       return UnderlyingSystemType.ToString ();
+               }
+
+               public override RuntimeTypeHandle TypeHandle {
+                       get { return UnderlyingSystemType.TypeHandle; }
+               }
+       }
+
        public class UnixUtils {
                [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
                extern static int _isatty (int fd);