Facilitate the merge
[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                 const int buffer_read_length_spans = 3;
87
88                 TextReader reader;
89                 Stream stream;
90
91                 static char[] buffer;
92                 int average_read_length;
93                 int buffer_start;       // in chars
94                 int char_count;         // count buffer[] valid characters
95                 int pos;                // index into buffer[]
96
97                 public SeekableStreamReader (Stream stream, Encoding encoding)
98                 {
99                         this.stream = stream;
100
101                         const int default_average_read_length = 1024;
102                         InitializeStream (default_average_read_length);
103                         reader = new StreamReader (stream, encoding, true);
104                 }
105
106                 public void Dispose ()
107                 {
108                         // Needed to release stream reader buffers
109                         reader.Dispose ();
110                 }
111
112                 void InitializeStream (int read_length_inc)
113                 {
114                         average_read_length += read_length_inc;
115
116                         int required_buffer_size = average_read_length * buffer_read_length_spans;
117                         if (buffer == null || buffer.Length < required_buffer_size)
118                                 buffer = new char [required_buffer_size];
119
120                         stream.Position = 0;                    
121                         buffer_start = char_count = pos = 0;
122                 }
123
124                 /// <remarks>
125                 ///   This value corresponds to the current position in a stream of characters.
126                 ///   The StreamReader hides its manipulation of the underlying byte stream and all
127                 ///   character set/decoding issues.  Thus, we cannot use this position to guess at
128                 ///   the corresponding position in the underlying byte stream even though there is
129                 ///   a correlation between them.
130                 /// </remarks>
131                 public int Position {
132                         get { return buffer_start + pos; }
133
134                         set {
135                                 // If the lookahead was too small, re-read from the beginning.  Increase the buffer size while we're at it
136                                 if (value < buffer_start)
137                                         InitializeStream (average_read_length / 2);
138
139                                 while (value > buffer_start + char_count) {
140                                         pos = char_count;
141                                         if (!ReadBuffer ())
142                                                 throw new InternalErrorException ("Seek beyond end of file: " + (buffer_start + char_count - value));
143                                 }
144
145                                 pos = value - buffer_start;
146                         }
147                 }
148
149                 private bool ReadBuffer ()
150                 {
151                         int slack = buffer.Length - char_count;
152                         if (slack <= average_read_length / 2) {
153                                 // shift the buffer to make room for average_read_length number of characters
154                                 int shift = average_read_length - slack;
155                                 Array.Copy (buffer, shift, buffer, 0, char_count - shift);
156                                 pos -= shift;
157                                 char_count -= shift;
158                                 buffer_start += shift;
159                                 slack += shift;         // slack == average_read_length
160                         }
161
162                         char_count += reader.Read (buffer, char_count, slack);
163
164                         return pos < char_count;
165                 }
166
167                 public int Peek ()
168                 {
169                         if ((pos >= char_count) && !ReadBuffer ())
170                                 return -1;
171
172                         return buffer [pos];
173                 }
174
175                 public int Read ()
176                 {
177                         if ((pos >= char_count) && !ReadBuffer ())
178                                 return -1;
179
180                         return buffer [pos++];
181                 }
182         }
183
184         public class UnixUtils {
185                 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
186                 extern static int _isatty (int fd);
187                         
188                 public static bool isatty (int fd)
189                 {
190                         try {
191                                 return _isatty (fd) == 1;
192                         } catch {
193                                 return false;
194                         }
195                 }
196         }
197
198         /// <summary>
199         ///   An exception used to terminate the compiler resolution phase and provide completions
200         /// </summary>
201         /// <remarks>
202         ///   This is thrown when we want to return the completions or
203         ///   terminate the completion process by AST nodes used in
204         ///   the completion process.
205         /// </remarks>
206         public class CompletionResult : Exception {
207                 string [] result;
208                 string base_text;
209                 
210                 public CompletionResult (string base_text, string [] res)
211                 {
212                         if (base_text == null)
213                                 throw new ArgumentNullException ("base_text");
214                         this.base_text = base_text;
215
216                         result = res;
217                         Array.Sort (result);
218                 }
219
220                 public string [] Result {
221                         get {
222                                 return result;
223                         }
224                 }
225
226                 public string BaseText {
227                         get {
228                                 return base_text;
229                         }
230                 }
231         }
232 }