2 // support.cs: Support routines to work around the fact that System.Reflection.Emit
3 // can not introspect types that are being constructed
6 // Miguel de Icaza (miguel@ximian.com)
7 // Marek Safar (marek.safar@gmail.com)
9 // Copyright 2001 Ximian, Inc (http://www.ximian.com)
10 // Copyright 2003-2009 Novell, Inc
11 // Copyright 2011 Xamarin Inc
17 using System.Collections.Generic;
19 namespace Mono.CSharp {
21 sealed class ReferenceEquality<T> : IEqualityComparer<T> where T : class
23 public static readonly IEqualityComparer<T> Default = new ReferenceEquality<T> ();
25 private ReferenceEquality ()
29 public bool Equals (T x, T y)
31 return ReferenceEquals (x, y);
34 public int GetHashCode (T obj)
36 return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode (obj);
39 #if !NET_4_0 && !MONODROID
40 public class Tuple<T1, T2> : IEquatable<Tuple<T1, T2>>
42 public Tuple (T1 item1, T2 item2)
48 public T1 Item1 { get; private set; }
49 public T2 Item2 { get; private set; }
51 public override int GetHashCode ()
53 return Item1.GetHashCode () ^ Item2.GetHashCode ();
56 #region IEquatable<Tuple<T1,T2>> Members
58 public bool Equals (Tuple<T1, T2> other)
60 return EqualityComparer<T1>.Default.Equals (Item1, other.Item1) &&
61 EqualityComparer<T2>.Default.Equals (Item2, other.Item2);
67 public class Tuple<T1, T2, T3> : IEquatable<Tuple<T1, T2, T3>>
69 public Tuple (T1 item1, T2 item2, T3 item3)
76 public T1 Item1 { get; private set; }
77 public T2 Item2 { get; private set; }
78 public T3 Item3 { get; private set; }
80 public override int GetHashCode ()
82 return Item1.GetHashCode () ^ Item2.GetHashCode () ^ Item3.GetHashCode ();
85 #region IEquatable<Tuple<T1,T2>> Members
87 public bool Equals (Tuple<T1, T2, T3> other)
89 return EqualityComparer<T1>.Default.Equals (Item1, other.Item1) &&
90 EqualityComparer<T2>.Default.Equals (Item2, other.Item2) &&
91 EqualityComparer<T3>.Default.Equals (Item3, other.Item3);
99 public static Tuple<T1, T2> Create<T1, T2> (T1 item1, T2 item2)
101 return new Tuple<T1, T2> (item1, item2);
104 public static Tuple<T1, T2, T3> Create<T1, T2, T3> (T1 item1, T2 item2, T3 item3)
106 return new Tuple<T1, T2, T3> (item1, item2, item3);
111 static class ArrayComparer
113 public static bool IsEqual<T> (T[] array1, T[] array2)
115 if (array1 == null || array2 == null)
116 return array1 == array2;
118 var eq = EqualityComparer<T>.Default;
120 for (int i = 0; i < array1.Length; ++i) {
121 if (!eq.Equals (array1[i], array2[i])) {
131 /// This is an arbitrarily seekable StreamReader wrapper.
133 /// It uses a self-tuning buffer to cache the seekable data,
134 /// but if the seek is too far, it may read the underly
135 /// stream all over from the beginning.
137 public class SeekableStreamReader : IDisposable
139 public const int DefaultReadAheadSize = 2048;
145 int read_ahead_length; // the length of read buffer
146 int buffer_start; // in chars
147 int char_count; // count of filled characters in buffer[]
148 int pos; // index into buffer[]
150 public SeekableStreamReader (Stream stream, Encoding encoding, char[] sharedBuffer = null)
152 this.stream = stream;
153 this.buffer = sharedBuffer;
155 InitializeStream (DefaultReadAheadSize);
156 reader = new StreamReader (stream, encoding, true);
159 public void Dispose ()
161 // Needed to release stream reader buffers
165 void InitializeStream (int read_length_inc)
167 read_ahead_length += read_length_inc;
169 int required_buffer_size = read_ahead_length * 2;
171 if (buffer == null || buffer.Length < required_buffer_size)
172 buffer = new char [required_buffer_size];
175 buffer_start = char_count = pos = 0;
179 /// This value corresponds to the current position in a stream of characters.
180 /// The StreamReader hides its manipulation of the underlying byte stream and all
181 /// character set/decoding issues. Thus, we cannot use this position to guess at
182 /// the corresponding position in the underlying byte stream even though there is
183 /// a correlation between them.
185 public int Position {
187 return buffer_start + pos;
192 // If the lookahead was too small, re-read from the beginning. Increase the buffer size while we're at it
193 // This should never happen until we are parsing some weird source code
195 if (value < buffer_start) {
196 InitializeStream (read_ahead_length);
199 // Discard buffer data after underlying stream changed position
200 // Cannot use handy reader.DiscardBufferedData () because it for
201 // some strange reason resets encoding as well
203 reader = new StreamReader (stream, reader.CurrentEncoding, true);
206 while (value > buffer_start + char_count) {
209 throw new InternalErrorException ("Seek beyond end of file: " + (buffer_start + char_count - value));
212 pos = value - buffer_start;
218 int slack = buffer.Length - char_count;
221 // read_ahead_length is only half of the buffer to deal with
222 // reads ahead and moves back without re-reading whole buffer
224 if (slack <= read_ahead_length) {
226 // shift the buffer to make room for read_ahead_length number of characters
228 int shift = read_ahead_length - slack;
229 Array.Copy (buffer, shift, buffer, 0, char_count - shift);
231 // Update all counters
234 buffer_start += shift;
238 char_count += reader.Read (buffer, char_count, slack);
240 return pos < char_count;
243 public char[] ReadChars (int fromPosition, int toPosition)
245 char[] chars = new char[toPosition - fromPosition];
246 if (buffer_start <= fromPosition && toPosition <= buffer_start + buffer.Length) {
247 Array.Copy (buffer, fromPosition - buffer_start, chars, 0, chars.Length);
249 throw new NotImplementedException ();
257 if ((pos >= char_count) && !ReadBuffer ())
265 if ((pos >= char_count) && !ReadBuffer ())
268 return buffer [pos++];
272 public class UnixUtils {
273 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
274 extern static int _isatty (int fd);
276 public static bool isatty (int fd)
279 return _isatty (fd) == 1;
287 /// An exception used to terminate the compiler resolution phase and provide completions
290 /// This is thrown when we want to return the completions or
291 /// terminate the completion process by AST nodes used in
292 /// the completion process.
294 public class CompletionResult : Exception {
298 public CompletionResult (string base_text, string [] res)
300 if (base_text == null)
301 throw new ArgumentNullException ("base_text");
302 this.base_text = base_text;
308 public string [] Result {
314 public string BaseText {