New tests.
[mono.git] / mcs / mcs / support.cs
1 //
2 // support.cs: Support routines to work around the fact that System.Reflection.Emit
3 // can not introspect types that are being constructed
4 //
5 // Author:
6 //   Miguel de Icaza (miguel@ximian.com)
7 //   Marek Safar (marek.safar@gmail.com)
8 //
9 // Copyright 2001 Ximian, Inc (http://www.ximian.com)
10 // Copyright 2003-2009 Novell, Inc
11 //
12
13 using System;
14 using System.IO;
15 using System.Text;
16 using System.Reflection;
17 using System.Reflection.Emit;
18 using System.Globalization;
19 using System.Collections.Generic;
20
21 namespace Mono.CSharp {
22
23         sealed class ReferenceEquality<T> : IEqualityComparer<T> where T : class
24         {
25                 public static readonly IEqualityComparer<T> Default = new ReferenceEquality<T> ();
26
27                 private ReferenceEquality ()
28                 {
29                 }
30
31                 public bool Equals (T x, T y)
32                 {
33                         return ReferenceEquals (x, y);
34                 }
35
36                 public int GetHashCode (T obj)
37                 {
38                         return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode (obj);
39                 }
40         }
41
42         class Tuple<T1, T2> : IEquatable<Tuple<T1, T2>>
43         {
44                 public Tuple (T1 item1, T2 item2)
45                 {
46                         Item1 = item1;
47                         Item2 = item2;
48                 }
49
50                 public T1 Item1 { get; private set; }
51                 public T2 Item2 { get; private set; }
52
53                 public override int GetHashCode ()
54                 {
55                         return Item1.GetHashCode () ^ Item2.GetHashCode ();
56                 }
57
58                 #region IEquatable<Tuple<T1,T2>> Members
59
60                 public bool Equals (Tuple<T1, T2> other)
61                 {
62                         return EqualityComparer<T1>.Default.Equals (Item1, other.Item1) &&
63                                 EqualityComparer<T2>.Default.Equals (Item2, other.Item2);
64                 }
65
66                 #endregion
67         }
68
69         static class Tuple
70         {
71                 public static Tuple<T1, T2> Create<T1, T2> (T1 item1, T2 item2)
72                 {
73                         return new Tuple<T1, T2> (item1, item2);
74                 }
75         }
76
77         public class Accessors {
78                 public Accessor get_or_add;
79                 public Accessor set_or_remove;
80
81                 // was 'set' declared before 'get'?  was 'remove' declared before 'add'?
82                 public bool declared_in_reverse;
83
84                 public Accessors (Accessor get_or_add, Accessor set_or_remove)
85                 {
86                         this.get_or_add = get_or_add;
87                         this.set_or_remove = set_or_remove;
88                 }
89         }
90
91         /// <summary>
92         ///   This is an arbitrarily seekable StreamReader wrapper.
93         ///
94         ///   It uses a self-tuning buffer to cache the seekable data,
95         ///   but if the seek is too far, it may read the underly
96         ///   stream all over from the beginning.
97         /// </summary>
98         public class SeekableStreamReader : IDisposable
99         {
100                 const int buffer_read_length_spans = 3;
101
102                 TextReader reader;
103                 Stream stream;
104
105                 static char[] buffer;
106                 int average_read_length;
107                 int buffer_start;       // in chars
108                 int char_count;         // count buffer[] valid characters
109                 int pos;                // index into buffer[]
110
111                 public SeekableStreamReader (Stream stream, Encoding encoding)
112                 {
113                         this.stream = stream;
114
115                         const int default_average_read_length = 1024;
116                         InitializeStream (default_average_read_length);
117                         reader = new StreamReader (stream, encoding, true);
118                 }
119
120                 public void Dispose ()
121                 {
122                         // Needed to release stream reader buffers
123                         reader.Dispose ();
124                 }
125
126                 void InitializeStream (int read_length_inc)
127                 {
128                         average_read_length += read_length_inc;
129
130                         int required_buffer_size = average_read_length * buffer_read_length_spans;
131                         if (buffer == null || buffer.Length < required_buffer_size)
132                                 buffer = new char [required_buffer_size];
133
134                         stream.Position = 0;                    
135                         buffer_start = char_count = pos = 0;
136                 }
137
138                 /// <remarks>
139                 ///   This value corresponds to the current position in a stream of characters.
140                 ///   The StreamReader hides its manipulation of the underlying byte stream and all
141                 ///   character set/decoding issues.  Thus, we cannot use this position to guess at
142                 ///   the corresponding position in the underlying byte stream even though there is
143                 ///   a correlation between them.
144                 /// </remarks>
145                 public int Position {
146                         get { return buffer_start + pos; }
147
148                         set {
149                                 // If the lookahead was too small, re-read from the beginning.  Increase the buffer size while we're at it
150                                 if (value < buffer_start)
151                                         InitializeStream (average_read_length / 2);
152
153                                 while (value > buffer_start + char_count) {
154                                         pos = char_count;
155                                         if (!ReadBuffer ())
156                                                 throw new InternalErrorException ("Seek beyond end of file: " + (buffer_start + char_count - value));
157                                 }
158
159                                 pos = value - buffer_start;
160                         }
161                 }
162
163                 private bool ReadBuffer ()
164                 {
165                         int slack = buffer.Length - char_count;
166                         if (slack <= average_read_length / 2) {
167                                 // shift the buffer to make room for average_read_length number of characters
168                                 int shift = average_read_length - slack;
169                                 Array.Copy (buffer, shift, buffer, 0, char_count - shift);
170                                 pos -= shift;
171                                 char_count -= shift;
172                                 buffer_start += shift;
173                                 slack += shift;         // slack == average_read_length
174                         }
175
176                         char_count += reader.Read (buffer, char_count, slack);
177
178                         return pos < char_count;
179                 }
180
181                 public int Peek ()
182                 {
183                         if ((pos >= char_count) && !ReadBuffer ())
184                                 return -1;
185
186                         return buffer [pos];
187                 }
188
189                 public int Read ()
190                 {
191                         if ((pos >= char_count) && !ReadBuffer ())
192                                 return -1;
193
194                         return buffer [pos++];
195                 }
196         }
197
198         public class DoubleHash {
199                 const int DEFAULT_INITIAL_BUCKETS = 100;
200
201                 public DoubleHash () : this (DEFAULT_INITIAL_BUCKETS) {}
202
203                 public DoubleHash (int size)
204                 {
205                         count = size;
206                         buckets = new Entry [size];
207                 }
208
209                 int count;
210                 Entry [] buckets;
211                 int size = 0;
212
213                 class Entry {
214                         public object key1;
215                         public object key2;
216                         public int hash;
217                         public object value;
218                         public Entry next;
219
220                         public Entry (object key1, object key2, int hash, object value, Entry next)
221                         {
222                                 this.key1 = key1;
223                                 this.key2 = key2;
224                                 this.hash = hash;
225                                 this.next = next;
226                                 this.value = value;
227                         }
228                 }
229
230                 public bool Lookup (object a, object b, out object res)
231                 {
232                         int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
233
234                         for (Entry e = buckets [h % count]; e != null; e = e.next) {
235                                 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b)) {
236                                         res = e.value;
237                                         return true;
238                                 }
239                         }
240                         res = null;
241                         return false;
242                 }
243
244                 public void Insert (object a, object b, object value)
245                 {
246                         // Is it an existing one?
247
248                         int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
249
250                         for (Entry e = buckets [h % count]; e != null; e = e.next) {
251                                 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b))
252                                         e.value = value;
253                         }
254
255                         int bucket = h % count;
256                         buckets [bucket] = new Entry (a, b, h, value, buckets [bucket]);
257
258                         // Grow whenever we double in size
259                         if (size++ == count) {
260                                 count <<= 1;
261                                 count ++;
262
263                                 Entry [] newBuckets = new Entry [count];
264                                 foreach (Entry root in buckets) {
265                                         Entry e = root;
266                                         while (e != null) {
267                                                 int newLoc = e.hash % count;
268                                                 Entry n = e.next;
269                                                 e.next = newBuckets [newLoc];
270                                                 newBuckets [newLoc] = e;
271                                                 e = n;
272                                         }
273                                 }
274
275                                 buckets = newBuckets;
276                         }
277                 }
278         }
279
280         public class UnixUtils {
281                 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
282                 extern static int _isatty (int fd);
283                         
284                 public static bool isatty (int fd)
285                 {
286                         try {
287                                 return _isatty (fd) == 1;
288                         } catch {
289                                 return false;
290                         }
291                 }
292         }
293
294         /// <summary>
295         ///   An exception used to terminate the compiler resolution phase and provide completions
296         /// </summary>
297         /// <remarks>
298         ///   This is thrown when we want to return the completions or
299         ///   terminate the completion process by AST nodes used in
300         ///   the completion process.
301         /// </remarks>
302         public class CompletionResult : Exception {
303                 string [] result;
304                 string base_text;
305                 
306                 public CompletionResult (string base_text, string [] res)
307                 {
308                         if (base_text == null)
309                                 throw new ArgumentNullException ("base_text");
310                         this.base_text = base_text;
311
312                         result = res;
313                         Array.Sort (result);
314                 }
315
316                 public string [] Result {
317                         get {
318                                 return result;
319                         }
320                 }
321
322                 public string BaseText {
323                         get {
324                                 return base_text;
325                         }
326                 }
327         }
328 }