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
143
static char[] buffer;
144
int read_ahead_length; // the length of read buffer
145
int buffer_start; // in chars
146
int char_count; // count of filled characters in buffer[]
147
int pos; // index into buffer[]
149
public SeekableStreamReader (Stream stream, Encoding encoding)
151
this.stream = stream;
153
const int default_read_ahead = 2048;
154
InitializeStream (default_read_ahead);
155
reader = new StreamReader (stream, encoding, true);
158
public void Dispose ()
160
// Needed to release stream reader buffers
164
void InitializeStream (int read_length_inc)
166
read_ahead_length += read_length_inc;
168
int required_buffer_size = read_ahead_length * 2;
170
if (buffer == null || buffer.Length < required_buffer_size)
171
buffer = new char [required_buffer_size];
174
buffer_start = char_count = pos = 0;
178
/// This value corresponds to the current position in a stream of characters.
179
/// The StreamReader hides its manipulation of the underlying byte stream and all
180
/// character set/decoding issues. Thus, we cannot use this position to guess at
181
/// the corresponding position in the underlying byte stream even though there is
182
/// a correlation between them.
184
public int Position {
186
return buffer_start + pos;
191
// If the lookahead was too small, re-read from the beginning. Increase the buffer size while we're at it
192
// This should never happen until we are parsing some weird source code
194
if (value < buffer_start) {
195
InitializeStream (read_ahead_length);
198
// Discard buffer data after underlying stream changed position
199
// Cannot use handy reader.DiscardBufferedData () because it for
200
// some strange reason resets encoding as well
202
reader = new StreamReader (stream, reader.CurrentEncoding, true);
205
while (value > buffer_start + char_count) {
208
throw new InternalErrorException ("Seek beyond end of file: " + (buffer_start + char_count - value));
211
pos = value - buffer_start;
217
int slack = buffer.Length - char_count;
220
// read_ahead_length is only half of the buffer to deal with
221
// reads ahead and moves back without re-reading whole buffer
223
if (slack <= read_ahead_length) {
225
// shift the buffer to make room for read_ahead_length number of characters
227
int shift = read_ahead_length - slack;
228
Array.Copy (buffer, shift, buffer, 0, char_count - shift);
230
// Update all counters
233
buffer_start += shift;
237
char_count += reader.Read (buffer, char_count, slack);
239
return pos < char_count;
242
public char GetChar (int position)
244
if (buffer_start <= position && position < buffer.Length)
245
return buffer[position];
249
public char[] ReadChars (int fromPosition, int toPosition)
251
char[] chars = new char[toPosition - fromPosition];
252
if (buffer_start <= fromPosition && toPosition <= buffer_start + buffer.Length) {
253
Array.Copy (buffer, fromPosition - buffer_start, chars, 0, chars.Length);
255
throw new NotImplementedException ();
263
if ((pos >= char_count) && !ReadBuffer ())
271
if ((pos >= char_count) && !ReadBuffer ())
274
return buffer [pos++];
278
public class UnixUtils {
279
[System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
280
extern static int _isatty (int fd);
282
public static bool isatty (int fd)
285
return _isatty (fd) == 1;
293
/// An exception used to terminate the compiler resolution phase and provide completions
296
/// This is thrown when we want to return the completions or
297
/// terminate the completion process by AST nodes used in
298
/// the completion process.
300
public class CompletionResult : Exception {
304
public CompletionResult (string base_text, string [] res)
306
if (base_text == null)
307
throw new ArgumentNullException ("base_text");
308
this.base_text = base_text;
314
public string [] Result {
320
public string BaseText {