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
16 using System.Collections.Generic;
18 namespace Mono.CSharp {
20 sealed class ReferenceEquality<T> : IEqualityComparer<T> where T : class
22 public static readonly IEqualityComparer<T> Default = new ReferenceEquality<T> ();
24 private ReferenceEquality ()
28 public bool Equals (T x, T y)
30 return ReferenceEquals (x, y);
33 public int GetHashCode (T obj)
35 return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode (obj);
39 public class Tuple<T1, T2> : IEquatable<Tuple<T1, T2>>
41 public Tuple (T1 item1, T2 item2)
47 public T1 Item1 { get; private set; }
48 public T2 Item2 { get; private set; }
50 public override int GetHashCode ()
52 return Item1.GetHashCode () ^ Item2.GetHashCode ();
55 #region IEquatable<Tuple<T1,T2>> Members
57 public bool Equals (Tuple<T1, T2> other)
59 return EqualityComparer<T1>.Default.Equals (Item1, other.Item1) &&
60 EqualityComparer<T2>.Default.Equals (Item2, other.Item2);
66 public class Tuple<T1, T2, T3> : IEquatable<Tuple<T1, T2, T3>>
68 public Tuple (T1 item1, T2 item2, T3 item3)
75 public T1 Item1 { get; private set; }
76 public T2 Item2 { get; private set; }
77 public T3 Item3 { get; private set; }
79 public override int GetHashCode ()
81 return Item1.GetHashCode () ^ Item2.GetHashCode () ^ Item3.GetHashCode ();
84 #region IEquatable<Tuple<T1,T2>> Members
86 public bool Equals (Tuple<T1, T2, T3> other)
88 return EqualityComparer<T1>.Default.Equals (Item1, other.Item1) &&
89 EqualityComparer<T2>.Default.Equals (Item2, other.Item2) &&
90 EqualityComparer<T3>.Default.Equals (Item3, other.Item3);
98 public static Tuple<T1, T2> Create<T1, T2> (T1 item1, T2 item2)
100 return new Tuple<T1, T2> (item1, item2);
103 public static Tuple<T1, T2, T3> Create<T1, T2, T3> (T1 item1, T2 item2, T3 item3)
105 return new Tuple<T1, T2, T3> (item1, item2, item3);
109 static class ArrayComparer
111 public static bool IsEqual<T> (T[] array1, T[] array2)
113 if (array1 == null || array2 == null)
114 return array1 == array2;
116 var eq = EqualityComparer<T>.Default;
118 for (int i = 0; i < array1.Length; ++i) {
119 if (!eq.Equals (array1[i], array2[i])) {
129 /// This is an arbitrarily seekable StreamReader wrapper.
131 /// It uses a self-tuning buffer to cache the seekable data,
132 /// but if the seek is too far, it may read the underly
133 /// stream all over from the beginning.
135 public class SeekableStreamReader : IDisposable
140 static char[] buffer;
141 int read_ahead_length; // the length of read buffer
142 int buffer_start; // in chars
143 int char_count; // count of filled characters in buffer[]
144 int pos; // index into buffer[]
146 public SeekableStreamReader (Stream stream, Encoding encoding)
148 this.stream = stream;
150 const int default_read_ahead = 2048;
151 InitializeStream (default_read_ahead);
152 reader = new StreamReader (stream, encoding, true);
155 public void Dispose ()
157 // Needed to release stream reader buffers
161 void InitializeStream (int read_length_inc)
163 read_ahead_length += read_length_inc;
165 int required_buffer_size = read_ahead_length * 2;
167 if (buffer == null || buffer.Length < required_buffer_size)
168 buffer = new char [required_buffer_size];
171 buffer_start = char_count = pos = 0;
175 /// This value corresponds to the current position in a stream of characters.
176 /// The StreamReader hides its manipulation of the underlying byte stream and all
177 /// character set/decoding issues. Thus, we cannot use this position to guess at
178 /// the corresponding position in the underlying byte stream even though there is
179 /// a correlation between them.
181 public int Position {
183 return buffer_start + pos;
188 // If the lookahead was too small, re-read from the beginning. Increase the buffer size while we're at it
189 // This should never happen until we are parsing some weird source code
191 if (value < buffer_start) {
192 InitializeStream (read_ahead_length);
195 // Discard buffer data after underlying stream changed position
196 // Cannot use handy reader.DiscardBufferedData () because it for
197 // some strange reason resets encoding as well
199 reader = new StreamReader (stream, reader.CurrentEncoding, true);
202 while (value > buffer_start + char_count) {
205 throw new InternalErrorException ("Seek beyond end of file: " + (buffer_start + char_count - value));
208 pos = value - buffer_start;
214 int slack = buffer.Length - char_count;
217 // read_ahead_length is only half of the buffer to deal with
218 // reads ahead and moves back without re-reading whole buffer
220 if (slack <= read_ahead_length) {
222 // shift the buffer to make room for read_ahead_length number of characters
224 int shift = read_ahead_length - slack;
225 Array.Copy (buffer, shift, buffer, 0, char_count - shift);
227 // Update all counters
230 buffer_start += shift;
234 char_count += reader.Read (buffer, char_count, slack);
236 return pos < char_count;
241 if ((pos >= char_count) && !ReadBuffer ())
249 if ((pos >= char_count) && !ReadBuffer ())
252 return buffer [pos++];
256 public class UnixUtils {
257 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
258 extern static int _isatty (int fd);
260 public static bool isatty (int fd)
263 return _isatty (fd) == 1;
271 /// An exception used to terminate the compiler resolution phase and provide completions
274 /// This is thrown when we want to return the completions or
275 /// terminate the completion process by AST nodes used in
276 /// the completion process.
278 public class CompletionResult : Exception {
282 public CompletionResult (string base_text, string [] res)
284 if (base_text == null)
285 throw new ArgumentNullException ("base_text");
286 this.base_text = base_text;
292 public string [] Result {
298 public string BaseText {