Add testcase for multiple missing IDs
[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                                 //
137                                 // If the lookahead was too small, re-read from the beginning. Increase the buffer size while we're at it
138                                 // This should never happen until we are parsing some weird source code
139                                 //
140                                 if (value < buffer_start) {
141                                         InitializeStream (read_ahead_length);
142
143                                         //
144                                         // Discard buffer data after underlying stream changed position
145                                         // Cannot use handy reader.DiscardBufferedData () because it for
146                                         // some strange reason resets encoding as well
147                                         //
148                                         reader = new StreamReader (stream, reader.CurrentEncoding, true);
149                                 }
150
151                                 while (value > buffer_start + char_count) {
152                                         pos = char_count;
153                                         if (!ReadBuffer ())
154                                                 throw new InternalErrorException ("Seek beyond end of file: " + (buffer_start + char_count - value));
155                                 }
156
157                                 pos = value - buffer_start;
158                         }
159                 }
160
161                 bool ReadBuffer ()
162                 {
163                         int slack = buffer.Length - char_count;
164
165                         //
166                         // read_ahead_length is only half of the buffer to deal with
167                         // reads ahead and moves back without re-reading whole buffer
168                         //
169                         if (slack <= read_ahead_length) {
170                                 //
171                                 // shift the buffer to make room for read_ahead_length number of characters
172                                 //
173                                 int shift = read_ahead_length - slack;
174                                 Array.Copy (buffer, shift, buffer, 0, char_count - shift);
175
176                                 // Update all counters
177                                 pos -= shift;
178                                 char_count -= shift;
179                                 buffer_start += shift;
180                                 slack += shift;
181                         }
182
183                         char_count += reader.Read (buffer, char_count, slack);
184
185                         return pos < char_count;
186                 }
187
188                 public int Peek ()
189                 {
190                         if ((pos >= char_count) && !ReadBuffer ())
191                                 return -1;
192
193                         return buffer [pos];
194                 }
195
196                 public int Read ()
197                 {
198                         if ((pos >= char_count) && !ReadBuffer ())
199                                 return -1;
200
201                         return buffer [pos++];
202                 }
203         }
204
205         public class UnixUtils {
206                 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
207                 extern static int _isatty (int fd);
208                         
209                 public static bool isatty (int fd)
210                 {
211                         try {
212                                 return _isatty (fd) == 1;
213                         } catch {
214                                 return false;
215                         }
216                 }
217         }
218
219         /// <summary>
220         ///   An exception used to terminate the compiler resolution phase and provide completions
221         /// </summary>
222         /// <remarks>
223         ///   This is thrown when we want to return the completions or
224         ///   terminate the completion process by AST nodes used in
225         ///   the completion process.
226         /// </remarks>
227         public class CompletionResult : Exception {
228                 string [] result;
229                 string base_text;
230                 
231                 public CompletionResult (string base_text, string [] res)
232                 {
233                         if (base_text == null)
234                                 throw new ArgumentNullException ("base_text");
235                         this.base_text = base_text;
236
237                         result = res;
238                         Array.Sort (result);
239                 }
240
241                 public string [] Result {
242                         get {
243                                 return result;
244                         }
245                 }
246
247                 public string BaseText {
248                         get {
249                                 return base_text;
250                         }
251                 }
252         }
253 }