File: support.cs

package info (click to toggle)
nrefactory 5.3.0%2B20130718.73b6d0f-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 15,684 kB
  • ctags: 37,257
  • sloc: cs: 296,018; makefile: 24; ansic: 7; sh: 2
file content (364 lines) | stat: -rw-r--r-- 8,617 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//
// support.cs: Support routines to work around the fact that System.Reflection.Emit
// can not introspect types that are being constructed
//
// Author:
//   Miguel de Icaza (miguel@ximian.com)
//   Marek Safar (marek.safar@gmail.com)
//
// Copyright 2001 Ximian, Inc (http://www.ximian.com)
// Copyright 2003-2009 Novell, Inc
// Copyright 2011 Xamarin Inc
//

using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections.Generic;

namespace Mono.CSharp {

	sealed class ReferenceEquality<T> : IEqualityComparer<T> where T : class
	{
		public static readonly IEqualityComparer<T> Default = new ReferenceEquality<T> ();

		private ReferenceEquality ()
		{
		}

		public bool Equals (T x, T y)
		{
			return ReferenceEquals (x, y);
		}

		public int GetHashCode (T obj)
		{
			return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode (obj);
		}
	}
#if !NET_4_0 && !MONODROID
	public class Tuple<T1, T2> : IEquatable<Tuple<T1, T2>>
	{
		public Tuple (T1 item1, T2 item2)
		{
			Item1 = item1;
			Item2 = item2;
		}

		public T1 Item1 { get; private set; }
		public T2 Item2 { get; private set; }

		public override int GetHashCode ()
		{
			return ((object)Item1 ?? 0) .GetHashCode () ^ ((object)Item2 ?? 0).GetHashCode ();
		}

		#region IEquatable<Tuple<T1,T2>> Members

		public bool Equals (Tuple<T1, T2> other)
		{
			return EqualityComparer<T1>.Default.Equals (Item1, other.Item1) &&
				EqualityComparer<T2>.Default.Equals (Item2, other.Item2);
		}

		#endregion
	}

	public class Tuple<T1, T2, T3> : IEquatable<Tuple<T1, T2, T3>>
	{
		public Tuple (T1 item1, T2 item2, T3 item3)
		{
			Item1 = item1;
			Item2 = item2;
			Item3 = item3;
		}

		public T1 Item1 { get; private set; }
		public T2 Item2 { get; private set; }
		public T3 Item3 { get; private set; }

		public override int GetHashCode ()
		{
			return Item1.GetHashCode () ^ Item2.GetHashCode () ^ Item3.GetHashCode ();
		}

		#region IEquatable<Tuple<T1,T2>> Members

		public bool Equals (Tuple<T1, T2, T3> other)
		{
			return EqualityComparer<T1>.Default.Equals (Item1, other.Item1) &&
				EqualityComparer<T2>.Default.Equals (Item2, other.Item2) &&
				EqualityComparer<T3>.Default.Equals (Item3, other.Item3);
		}

		#endregion
	}

	static class Tuple
	{
		public static Tuple<T1, T2> Create<T1, T2> (T1 item1, T2 item2)
		{
			return new Tuple<T1, T2> (item1, item2);
		}

		public static Tuple<T1, T2, T3> Create<T1, T2, T3> (T1 item1, T2 item2, T3 item3)
		{
			return new Tuple<T1, T2, T3> (item1, item2, item3);
		}
	}
#endif

	static class ArrayComparer
	{
		public static bool IsEqual<T> (T[] array1, T[] array2)
		{
			if (array1 == null || array2 == null)
				return array1 == array2;

			var eq = EqualityComparer<T>.Default;

			for (int i = 0; i < array1.Length; ++i) {
				if (!eq.Equals (array1[i], array2[i])) {
					return false;
				}
			}

			return true;
		}
	}
#if !FULL_AST
	/// <summary>
	///   This is an arbitrarily seekable StreamReader wrapper.
	///
	///   It uses a self-tuning buffer to cache the seekable data,
	///   but if the seek is too far, it may read the underly
	///   stream all over from the beginning.
	/// </summary>
	public class SeekableStreamReader : IDisposable
	{
		public const int DefaultReadAheadSize =
			4096 / 2;

		StreamReader reader;
		Stream stream;

		char[] buffer;
		int read_ahead_length;	// the length of read buffer
		int buffer_start;       // in chars
		int char_count;         // count of filled characters in buffer[]
		int pos;                // index into buffer[]

		public SeekableStreamReader (Stream stream, Encoding encoding, char[] sharedBuffer = null)
		{
			this.stream = stream;
			this.buffer = sharedBuffer;

			InitializeStream (DefaultReadAheadSize);
			reader = new StreamReader (stream, encoding, true);
		}

		public void Dispose ()
		{
			// Needed to release stream reader buffers
			reader.Dispose ();
		}

		void InitializeStream (int read_length_inc)
		{
			read_ahead_length += read_length_inc;

			int required_buffer_size = read_ahead_length * 2;

			if (buffer == null || buffer.Length < required_buffer_size)
				buffer = new char [required_buffer_size];

			stream.Position = 0;
			buffer_start = char_count = pos = 0;
		}

		/// <remarks>
		///   This value corresponds to the current position in a stream of characters.
		///   The StreamReader hides its manipulation of the underlying byte stream and all
		///   character set/decoding issues.  Thus, we cannot use this position to guess at
		///   the corresponding position in the underlying byte stream even though there is
		///   a correlation between them.
		/// </remarks>
		public int Position {
			get {
				return buffer_start + pos;
			}

			set {
				//
				// If the lookahead was too small, re-read from the beginning. Increase the buffer size while we're at it
				// This should never happen until we are parsing some weird source code
				//
				if (value < buffer_start) {
					InitializeStream (read_ahead_length);

					//
					// Discard buffer data after underlying stream changed position
					// Cannot use handy reader.DiscardBufferedData () because it for
					// some strange reason resets encoding as well
					//
					reader = new StreamReader (stream, reader.CurrentEncoding, true);
				}

				while (value > buffer_start + char_count) {
					pos = char_count;
					if (!ReadBuffer ())
						throw new InternalErrorException ("Seek beyond end of file: " + (buffer_start + char_count - value));
				}

				pos = value - buffer_start;
			}
		}

		bool ReadBuffer ()
		{
			int slack = buffer.Length - char_count;

			//
			// read_ahead_length is only half of the buffer to deal with
			// reads ahead and moves back without re-reading whole buffer
			//
			if (slack <= read_ahead_length) {
				//
				// shift the buffer to make room for read_ahead_length number of characters
				//
				int shift = read_ahead_length - slack;
				Array.Copy (buffer, shift, buffer, 0, char_count - shift);

				// Update all counters
				pos -= shift;
				char_count -= shift;
				buffer_start += shift;
				slack += shift;
			}

			char_count += reader.Read (buffer, char_count, slack);

			return pos < char_count;
		}
		
		public char GetChar (int position)
		{
			if (buffer_start <= position && position < buffer.Length)
				return buffer[position];
			return '\0';
		}
		
		public char[] ReadChars (int fromPosition, int toPosition)
		{
			char[] chars = new char[toPosition - fromPosition];
			if (buffer_start <= fromPosition && toPosition <= buffer_start + buffer.Length) {
				Array.Copy (buffer, fromPosition - buffer_start, chars, 0, chars.Length);
			} else {
				throw new NotImplementedException ();
			}

			return chars;
		}

		public int Peek ()
		{
			if ((pos >= char_count) && !ReadBuffer ())
				return -1;

			return buffer [pos];
		}

		public int Read ()
		{
			if ((pos >= char_count) && !ReadBuffer ())
				return -1;

			return buffer [pos++];
		}
	}
#endif

	public class UnixUtils {
		[System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
		extern static int _isatty (int fd);
			
		public static bool isatty (int fd)
		{
			try {
				return _isatty (fd) == 1;
			} catch {
				return false;
			}
		}
	}

	/// <summary>
	///   An exception used to terminate the compiler resolution phase and provide completions
	/// </summary>
	/// <remarks>
	///   This is thrown when we want to return the completions or
	///   terminate the completion process by AST nodes used in
	///   the completion process.
	/// </remarks>
	public class CompletionResult : Exception {
		string [] result;
		string base_text;
		
		public CompletionResult (string base_text, string [] res)
		{
			if (base_text == null)
				throw new ArgumentNullException ("base_text");
			this.base_text = base_text;

			result = res;
			Array.Sort (result);
		}

		public string [] Result {
			get {
				return result;
			}
		}

		public string BaseText {
			get {
				return base_text;
			}
		}
	}

	struct TypeNameParser
	{
		internal static string Escape(string name)
		{
			if (name == null) {
				return null;
			}
			StringBuilder sb = null;
			for (int pos = 0; pos < name.Length; pos++) {
				char c = name[pos];
				switch (c) {
					case '\\':
					case '+':
					case ',':
					case '[':
					case ']':
					case '*':
					case '&':
						if (sb == null) {
							sb = new StringBuilder(name, 0, pos, name.Length + 3);
						}
						sb.Append("\\").Append(c);
						break;
					default:
						if (sb != null) {
							sb.Append(c);
						}
						break;
				}
			}
			return sb != null ? sb.ToString() : name;
		}
	}
}