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
18
using System.Collections.Generic;
20
namespace Mono.CSharp {
22
sealed class ReferenceEquality<T> : IEqualityComparer<T> where T : class
24
public static readonly IEqualityComparer<T> Default = new ReferenceEquality<T> ();
26
private ReferenceEquality ()
30
public bool Equals (T x, T y)
32
return ReferenceEquals (x, y);
35
public int GetHashCode (T obj)
37
return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode (obj);
40
#if !NET_4_0 && !MONODROID
41
public class Tuple<T1, T2> : IEquatable<Tuple<T1, T2>>
43
public Tuple (T1 item1, T2 item2)
49
public T1 Item1 { get; private set; }
50
public T2 Item2 { get; private set; }
52
public override int GetHashCode ()
54
return ((object)Item1 ?? 0) .GetHashCode () ^ ((object)Item2 ?? 0).GetHashCode ();
57
#region IEquatable<Tuple<T1,T2>> Members
59
public bool Equals (Tuple<T1, T2> other)
61
return EqualityComparer<T1>.Default.Equals (Item1, other.Item1) &&
62
EqualityComparer<T2>.Default.Equals (Item2, other.Item2);
68
public class Tuple<T1, T2, T3> : IEquatable<Tuple<T1, T2, T3>>
70
public Tuple (T1 item1, T2 item2, T3 item3)
77
public T1 Item1 { get; private set; }
78
public T2 Item2 { get; private set; }
79
public T3 Item3 { get; private set; }
81
public override int GetHashCode ()
83
return Item1.GetHashCode () ^ Item2.GetHashCode () ^ Item3.GetHashCode ();
86
#region IEquatable<Tuple<T1,T2>> Members
88
public bool Equals (Tuple<T1, T2, T3> other)
90
return EqualityComparer<T1>.Default.Equals (Item1, other.Item1) &&
91
EqualityComparer<T2>.Default.Equals (Item2, other.Item2) &&
92
EqualityComparer<T3>.Default.Equals (Item3, other.Item3);
100
public static Tuple<T1, T2> Create<T1, T2> (T1 item1, T2 item2)
102
return new Tuple<T1, T2> (item1, item2);
105
public static Tuple<T1, T2, T3> Create<T1, T2, T3> (T1 item1, T2 item2, T3 item3)
107
return new Tuple<T1, T2, T3> (item1, item2, item3);
112
static class ArrayComparer
114
public static bool IsEqual<T> (T[] array1, T[] array2)
116
if (array1 == null || array2 == null)
117
return array1 == array2;
119
var eq = EqualityComparer<T>.Default;
121
for (int i = 0; i < array1.Length; ++i) {
122
if (!eq.Equals (array1[i], array2[i])) {
132
/// This is an arbitrarily seekable StreamReader wrapper.
134
/// It uses a self-tuning buffer to cache the seekable data,
135
/// but if the seek is too far, it may read the underly
136
/// stream all over from the beginning.
138
public class SeekableStreamReader : IDisposable
140
public const int DefaultReadAheadSize =
147
int read_ahead_length; // the length of read buffer
148
int buffer_start; // in chars
149
int char_count; // count of filled characters in buffer[]
150
int pos; // index into buffer[]
152
public SeekableStreamReader (Stream stream, Encoding encoding, char[] sharedBuffer = null)
154
this.stream = stream;
155
this.buffer = sharedBuffer;
157
InitializeStream (DefaultReadAheadSize);
158
reader = new StreamReader (stream, encoding, true);
161
public void Dispose ()
163
// Needed to release stream reader buffers
167
void InitializeStream (int read_length_inc)
169
read_ahead_length += read_length_inc;
171
int required_buffer_size = read_ahead_length * 2;
173
if (buffer == null || buffer.Length < required_buffer_size)
174
buffer = new char [required_buffer_size];
177
buffer_start = char_count = pos = 0;
181
/// This value corresponds to the current position in a stream of characters.
182
/// The StreamReader hides its manipulation of the underlying byte stream and all
183
/// character set/decoding issues. Thus, we cannot use this position to guess at
184
/// the corresponding position in the underlying byte stream even though there is
185
/// a correlation between them.
187
public int Position {
189
return buffer_start + pos;
194
// If the lookahead was too small, re-read from the beginning. Increase the buffer size while we're at it
195
// This should never happen until we are parsing some weird source code
197
if (value < buffer_start) {
198
InitializeStream (read_ahead_length);
201
// Discard buffer data after underlying stream changed position
202
// Cannot use handy reader.DiscardBufferedData () because it for
203
// some strange reason resets encoding as well
205
reader = new StreamReader (stream, reader.CurrentEncoding, true);
208
while (value > buffer_start + char_count) {
211
throw new InternalErrorException ("Seek beyond end of file: " + (buffer_start + char_count - value));
214
pos = value - buffer_start;
220
int slack = buffer.Length - char_count;
223
// read_ahead_length is only half of the buffer to deal with
224
// reads ahead and moves back without re-reading whole buffer
226
if (slack <= read_ahead_length) {
228
// shift the buffer to make room for read_ahead_length number of characters
230
int shift = read_ahead_length - slack;
231
Array.Copy (buffer, shift, buffer, 0, char_count - shift);
233
// Update all counters
236
buffer_start += shift;
240
char_count += reader.Read (buffer, char_count, slack);
242
return pos < char_count;
245
public char GetChar (int position)
247
if (buffer_start <= position && position < buffer.Length)
248
return buffer[position];
252
public char[] ReadChars (int fromPosition, int toPosition)
254
char[] chars = new char[toPosition - fromPosition];
255
if (buffer_start <= fromPosition && toPosition <= buffer_start + buffer.Length) {
256
Array.Copy (buffer, fromPosition - buffer_start, chars, 0, chars.Length);
258
throw new NotImplementedException ();
266
if ((pos >= char_count) && !ReadBuffer ())
274
if ((pos >= char_count) && !ReadBuffer ())
277
return buffer [pos++];
282
public class UnixUtils {
283
[System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
284
extern static int _isatty (int fd);
286
public static bool isatty (int fd)
289
return _isatty (fd) == 1;
297
/// An exception used to terminate the compiler resolution phase and provide completions
300
/// This is thrown when we want to return the completions or
301
/// terminate the completion process by AST nodes used in
302
/// the completion process.
304
public class CompletionResult : Exception {
308
public CompletionResult (string base_text, string [] res)
310
if (base_text == null)
311
throw new ArgumentNullException ("base_text");
312
this.base_text = base_text;
318
public string [] Result {
324
public string BaseText {