Make the static field readonly
[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         public 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         /// <summary>
78         ///   This is an arbitrarily seekable StreamReader wrapper.
79         ///
80         ///   It uses a self-tuning buffer to cache the seekable data,
81         ///   but if the seek is too far, it may read the underly
82         ///   stream all over from the beginning.
83         /// </summary>
84         public class SeekableStreamReader : IDisposable
85         {
86                 StreamReader reader;
87                 Stream stream;
88
89                 static char[] buffer;
90                 int read_ahead_length;  // the length of read buffer
91                 int buffer_start;       // in chars
92                 int char_count;         // count of filled characters in buffer[]
93                 int pos;                // index into buffer[]
94
95                 public SeekableStreamReader (Stream stream, Encoding encoding)
96                 {
97                         this.stream = stream;
98
99                         const int default_read_ahead = 2048;
100                         InitializeStream (default_read_ahead);
101                         reader = new StreamReader (stream, encoding, true);
102                 }
103
104                 public void Dispose ()
105                 {
106                         // Needed to release stream reader buffers
107                         reader.Dispose ();
108                 }
109
110                 void InitializeStream (int read_length_inc)
111                 {
112                         read_ahead_length += read_length_inc;
113
114                         int required_buffer_size = read_ahead_length * 2;
115
116                         if (buffer == null || buffer.Length < required_buffer_size)
117                                 buffer = new char [required_buffer_size];
118
119                         stream.Position = 0;
120                         buffer_start = char_count = pos = 0;
121                 }
122
123                 /// <remarks>
124                 ///   This value corresponds to the current position in a stream of characters.
125                 ///   The StreamReader hides its manipulation of the underlying byte stream and all
126                 ///   character set/decoding issues.  Thus, we cannot use this position to guess at
127                 ///   the corresponding position in the underlying byte stream even though there is
128                 ///   a correlation between them.
129                 /// </remarks>
130                 public int Position {
131                         get {
132                                 return buffer_start + pos;
133                         }
134
135                         set {
136                                 // If the lookahead was too small, re-read from the beginning. Increase the buffer size while we're at it
137                                 // This should never happen until we are parsing some weird source code
138                                 if (value < buffer_start) {
139                                         InitializeStream (read_ahead_length);
140
141                                         //
142                                         // Discard buffer data after underlying stream changed position
143                                         // Cannot use handy reader.DiscardBufferedData () because it for
144                                         // some strange reason resets encoding as well
145                                         //
146                                         reader = new StreamReader (stream, reader.CurrentEncoding, true);
147                                 }
148
149                                 while (value > buffer_start + char_count) {
150                                         pos = char_count;
151                                         if (!ReadBuffer ())
152                                                 throw new InternalErrorException ("Seek beyond end of file: " + (buffer_start + char_count - value));
153                                 }
154
155                                 pos = value - buffer_start;
156                         }
157                 }
158
159                 bool ReadBuffer ()
160                 {
161                         int slack = buffer.Length - char_count;
162
163                         //
164                         // read_ahead_length is only half of the buffer to deal with
165                         // reads ahead and moves back without re-reading whole buffer
166                         //
167                         if (slack <= read_ahead_length) {
168                                 //
169                                 // shift the buffer to make room for read_ahead_length number of characters
170                                 //
171                                 int shift = read_ahead_length - slack;
172                                 Array.Copy (buffer, shift, buffer, 0, char_count - shift);
173
174                                 // Update all counters
175                                 pos -= shift;
176                                 char_count -= shift;
177                                 buffer_start += shift;
178                                 slack += shift;
179                         }
180
181                         char_count += reader.Read (buffer, char_count, slack);
182
183                         return pos < char_count;
184                 }
185
186                 public int Peek ()
187                 {
188                         if ((pos >= char_count) && !ReadBuffer ())
189                                 return -1;
190
191                         return buffer [pos];
192                 }
193
194                 public int Read ()
195                 {
196                         if ((pos >= char_count) && !ReadBuffer ())
197                                 return -1;
198
199                         return buffer [pos++];
200                 }
201         }
202
203         public class UnixUtils {
204                 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
205                 extern static int _isatty (int fd);
206                         
207                 public static bool isatty (int fd)
208                 {
209                         try {
210                                 return _isatty (fd) == 1;
211                         } catch {
212                                 return false;
213                         }
214                 }
215         }
216
217         /// <summary>
218         ///   An exception used to terminate the compiler resolution phase and provide completions
219         /// </summary>
220         /// <remarks>
221         ///   This is thrown when we want to return the completions or
222         ///   terminate the completion process by AST nodes used in
223         ///   the completion process.
224         /// </remarks>
225         public class CompletionResult : Exception {
226                 string [] result;
227                 string base_text;
228                 
229                 public CompletionResult (string base_text, string [] res)
230                 {
231                         if (base_text == null)
232                                 throw new ArgumentNullException ("base_text");
233                         this.base_text = base_text;
234
235                         result = res;
236                         Array.Sort (result);
237                 }
238
239                 public string [] Result {
240                         get {
241                                 return result;
242                         }
243                 }
244
245                 public string BaseText {
246                         get {
247                                 return base_text;
248                         }
249                 }
250         }
251 }