//
// 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
+ sealed class ReferenceEquality<T> : IEqualityComparer<T> where T : class
+ {
+ public static readonly IEqualityComparer<T> Default = new ReferenceEquality<T> ();
+
+ private ReferenceEquality ()
{
- private PtrComparer () {}
+ }
- public static PtrComparer Instance = new PtrComparer ();
+ public bool Equals (T x, T y)
+ {
+ return ReferenceEquals (x, y);
+ }
- public int Compare (object x, object y)
- {
- if (x == y)
- return 0;
- else
- return 1;
- }
+ public int GetHashCode (T obj)
+ {
+ return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode (obj);
+ }
+ }
- bool IEqualityComparer.Equals (object x, object y)
- {
- return x == y;
- }
-
- int IEqualityComparer.GetHashCode (object obj)
- {
- return obj.GetHashCode ();
- }
+ class Tuple<T1, T2> : IEquatable<Tuple<T1, T2>>
+ {
+ public Tuple (T1 item1, T2 item2)
+ {
+ Item1 = item1;
+ Item2 = item2;
}
- public PtrHashtable () : base (PtrComparer.Instance) {}
+ public T1 Item1 { get; private set; }
+ public T2 Item2 { get; private set; }
-#if MS_COMPATIBLE
- //
- // Workaround System.InvalidOperationException for enums
- //
- protected override int GetHash (object key)
+ public override int GetHashCode ()
{
- TypeBuilder tb = key as TypeBuilder;
- if (tb != null && tb.BaseType == TypeManager.enum_type && tb.BaseType != null)
- key = tb.BaseType;
+ return Item1.GetHashCode () ^ Item2.GetHashCode ();
+ }
+
+ #region IEquatable<Tuple<T1,T2>> Members
- return base.GetHash (key);
+ public bool Equals (Tuple<T1, T2> other)
+ {
+ return EqualityComparer<T1>.Default.Equals (Item1, other.Item1) &&
+ EqualityComparer<T2>.Default.Equals (Item2, other.Item2);
}
-#endif
- }
- struct Pair {
- public object First;
- public object Second;
+ #endregion
+ }
- public Pair (object f, object s)
+ static class Tuple
+ {
+ public static Tuple<T1, T2> Create<T1, T2> (T1 item1, T2 item2)
{
- First = f;
- Second = s;
+ return new Tuple<T1, T2> (item1, item2);
}
}
/// 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 ();
+ }
- ResetStream (default_average_read_length);
+ void InitializeStream (int read_length_inc)
+ {
+ average_read_length += read_length_inc;
+
+ 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>
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;
}
}
- class PartialMethodDefinitionInfo : MethodInfo
- {
- MethodOrOperator mc;
- MethodAttributes attrs;
-
- public PartialMethodDefinitionInfo (MethodOrOperator mc)
- {
- this.mc = mc;
- if ((mc.ModFlags & Modifiers.STATIC) != 0)
- attrs = MethodAttributes.Static;
- }
-
- public override MethodInfo GetBaseDefinition ()
- {
- throw new NotImplementedException ();
- }
-
- public override ICustomAttributeProvider ReturnTypeCustomAttributes
- {
- get { throw new NotImplementedException (); }
- }
-
- public override MethodAttributes Attributes
- {
- get { return attrs; }
- }
-
- public override MethodImplAttributes GetMethodImplementationFlags ()
- {
- throw new NotImplementedException ();
- }
-
- public override ParameterInfo [] GetParameters ()
- {
- throw new NotImplementedException ();
- }
-
- public override object Invoke (object obj, BindingFlags invokeAttr, Binder binder, object [] parameters, CultureInfo culture)
- {
- throw new NotImplementedException ();
- }
-
- public override RuntimeMethodHandle MethodHandle
- {
- get { throw new NotImplementedException (); }
- }
-
- public override Type DeclaringType
- {
- get { return mc.Parent.TypeBuilder; }
- }
-
- public override object [] GetCustomAttributes (Type attributeType, bool inherit)
- {
- throw new NotImplementedException ();
- }
-
- public override object [] GetCustomAttributes (bool inherit)
- {
- throw new NotImplementedException ();
- }
-
- public override Type ReturnType {
- get {
- return mc.MemberType;
- }
- }
-
- public override bool IsDefined (Type attributeType, bool inherit)
- {
- throw new NotImplementedException ();
- }
-
- public override string Name
- {
- get { return mc.Name; }
- }
-
- public override Type ReflectedType
- {
- get { throw new NotImplementedException (); }
- }
- }
-
-#if NET_4_0 || MS_COMPATIBLE
- [System.Diagnostics.DebuggerDisplay ("Dynamic type")]
-#endif
- class DynamicType : Type
- {
- public override Assembly Assembly {
- get { return UnderlyingSystemType.Assembly; }
- }
-
- public override string AssemblyQualifiedName {
- get { throw new NotImplementedException (); }
- }
-
- public override Type BaseType {
- get { return null; }
- }
-
- public override string FullName {
- get { return UnderlyingSystemType.FullName; }
- }
-
- public override Guid GUID {
- get { throw new NotImplementedException (); }
- }
-
- protected override TypeAttributes GetAttributeFlagsImpl ()
- {
- return UnderlyingSystemType.Attributes;
- }
-
- 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 ()
- {
- throw new NotImplementedException ();
- }
-
- 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 false;
- }
-
- 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 false;
- }
-
- 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 { return TypeManager.object_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 override Type MakeByRefType ()
- {
- // TODO: Wrong, hides dynamic type
- return UnderlyingSystemType.MakeByRefType ();
- }
- }
-
-#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);