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 =
141 65536 / 2; // Large buffer because of ReadChars of large literal string
150 int read_ahead_length; // the length of read buffer
151 int buffer_start; // in chars
152 int char_count; // count of filled characters in buffer[]
153 int pos; // index into buffer[]
155 public SeekableStreamReader (Stream stream, Encoding encoding, char[] sharedBuffer = null)
157 this.stream = stream;
158 this.buffer = sharedBuffer;
160 InitializeStream (DefaultReadAheadSize);
161 reader = new StreamReader (stream, encoding, true);
164 public void Dispose ()
166 // Needed to release stream reader buffers
170 void InitializeStream (int read_length_inc)
172 read_ahead_length += read_length_inc;
174 int required_buffer_size = read_ahead_length * 2;
176 if (buffer == null || buffer.Length < required_buffer_size)
177 buffer = new char [required_buffer_size];
180 buffer_start = char_count = pos = 0;
184 /// This value corresponds to the current position in a stream of characters.
185 /// The StreamReader hides its manipulation of the underlying byte stream and all
186 /// character set/decoding issues. Thus, we cannot use this position to guess at
187 /// the corresponding position in the underlying byte stream even though there is
188 /// a correlation between them.
190 public int Position {
192 return buffer_start + pos;
197 // If the lookahead was too small, re-read from the beginning. Increase the buffer size while we're at it
198 // This should never happen until we are parsing some weird source code
200 if (value < buffer_start) {
201 InitializeStream (read_ahead_length);
204 // Discard buffer data after underlying stream changed position
205 // Cannot use handy reader.DiscardBufferedData () because it for
206 // some strange reason resets encoding as well
208 reader = new StreamReader (stream, reader.CurrentEncoding, true);
211 while (value > buffer_start + char_count) {
214 throw new InternalErrorException ("Seek beyond end of file: " + (buffer_start + char_count - value));
217 pos = value - buffer_start;
223 int slack = buffer.Length - char_count;
226 // read_ahead_length is only half of the buffer to deal with
227 // reads ahead and moves back without re-reading whole buffer
229 if (slack <= read_ahead_length) {
231 // shift the buffer to make room for read_ahead_length number of characters
233 int shift = read_ahead_length - slack;
234 Array.Copy (buffer, shift, buffer, 0, char_count - shift);
236 // Update all counters
239 buffer_start += shift;
243 char_count += reader.Read (buffer, char_count, slack);
245 return pos < char_count;
248 public char[] ReadChars (int fromPosition, int toPosition)
250 char[] chars = new char[toPosition - fromPosition];
251 if (buffer_start <= fromPosition && toPosition <= buffer_start + buffer.Length) {
252 Array.Copy (buffer, fromPosition - buffer_start, chars, 0, chars.Length);
254 throw new NotImplementedException ();
262 if ((pos >= char_count) && !ReadBuffer ())
270 if ((pos >= char_count) && !ReadBuffer ())
273 return buffer [pos++];
277 public class UnixUtils {
278 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
279 extern static int _isatty (int fd);
281 public static bool isatty (int fd)
284 return _isatty (fd) == 1;
292 /// An exception used to terminate the compiler resolution phase and provide completions
295 /// This is thrown when we want to return the completions or
296 /// terminate the completion process by AST nodes used in
297 /// the completion process.
299 public class CompletionResult : Exception {
303 public CompletionResult (string base_text, string [] res)
305 if (base_text == null)
306 throw new ArgumentNullException ("base_text");
307 this.base_text = base_text;
313 public string [] Result {
319 public string BaseText {
326 struct TypeNameParser
328 internal static string Escape(string name)
333 StringBuilder sb = null;
334 for (int pos = 0; pos < name.Length; pos++) {
345 sb = new StringBuilder(name, 0, pos, name.Length + 3);
347 sb.Append("\\").Append(c);
356 return sb != null ? sb.ToString() : name;