Fix another mcs warning
[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 //
8 // Copyright 2001 Ximian, Inc (http://www.ximian.com)
9 // Copyright 2003-2008 Novell, Inc
10 //
11
12 using System;
13 using System.IO;
14 using System.Text;
15 using System.Reflection;
16 using System.Collections;
17 using System.Reflection.Emit;
18 using System.Globalization;
19
20 namespace Mono.CSharp {
21
22 #if GMCS_SOURCE
23         public class ReflectionConstraints : GenericConstraints
24         {
25                 GenericParameterAttributes attrs;
26                 Type base_type;
27                 Type class_constraint;
28                 Type[] iface_constraints;
29                 string name;
30
31                 public static GenericConstraints GetConstraints (Type t)
32                 {
33                         Type [] constraints = t.GetGenericParameterConstraints ();
34                         GenericParameterAttributes attrs = t.GenericParameterAttributes;
35                         if (constraints.Length == 0 && attrs == GenericParameterAttributes.None)
36                                 return null;
37                         return new ReflectionConstraints (t.Name, constraints, attrs);
38                 }
39
40                 private ReflectionConstraints (string name, Type [] constraints, GenericParameterAttributes attrs)
41                 {
42                         this.name = name;
43                         this.attrs = attrs;
44
45                         if ((constraints.Length > 0) && !constraints [0].IsInterface) {
46                                 class_constraint = constraints [0];
47                                 iface_constraints = new Type [constraints.Length - 1];
48                                 Array.Copy (constraints, 1, iface_constraints, 0, constraints.Length - 1);
49                         } else
50                                 iface_constraints = constraints;
51
52                         if (HasValueTypeConstraint)
53                                 base_type = TypeManager.value_type;
54                         else if (class_constraint != null)
55                                 base_type = class_constraint;
56                         else
57                                 base_type = TypeManager.object_type;
58                 }
59
60                 public override string TypeParameter {
61                         get { return name; }
62                 }
63
64                 public override GenericParameterAttributes Attributes {
65                         get { return attrs; }
66                 }
67
68                 public override Type ClassConstraint {
69                         get { return class_constraint; }
70                 }
71
72                 public override Type EffectiveBaseClass {
73                         get { return base_type; }
74                 }
75
76                 public override Type[] InterfaceConstraints {
77                         get { return iface_constraints; }
78                 }
79         }
80 #endif
81
82         class PtrHashtable : Hashtable {
83                 sealed class PtrComparer : IComparer
84 #if NET_2_0
85                         , IEqualityComparer
86 #endif
87                 {
88                         private PtrComparer () {}
89
90                         public static PtrComparer Instance = new PtrComparer ();
91
92                         public int Compare (object x, object y)
93                         {
94                                 if (x == y)
95                                         return 0;
96                                 else
97                                         return 1;
98                         }
99 #if NET_2_0
100                         bool IEqualityComparer.Equals (object x, object y)
101                         {
102                                 return x == y;
103                         }
104                         
105                         int IEqualityComparer.GetHashCode (object obj)
106                         {
107                                 return obj.GetHashCode ();
108                         }
109 #endif
110                         
111                 }
112
113 #if NET_2_0
114                 public PtrHashtable () : base (PtrComparer.Instance) {}
115 #else
116                 public PtrHashtable () 
117                 {
118                         comparer = PtrComparer.Instance;
119                 }
120 #endif
121
122 #if MS_COMPATIBLE
123                 //
124                 // Workaround System.InvalidOperationException for enums
125                 //
126                 protected override int GetHash (object key)
127                 {
128                         TypeBuilder tb = key as TypeBuilder;
129                         if (tb != null && tb.BaseType == TypeManager.enum_type)
130                                 key = tb.BaseType;
131
132                         return base.GetHash (key);
133                 }
134 #endif
135         }
136
137         /*
138          * Hashtable whose keys are character arrays with the same length
139          */
140         class CharArrayHashtable : Hashtable {
141                 sealed class ArrComparer : IComparer {
142                         private int len;
143
144                         public ArrComparer (int len) {
145                                 this.len = len;
146                         }
147
148                         public int Compare (object x, object y)
149                         {
150                                 char[] a = (char[])x;
151                                 char[] b = (char[])y;
152
153                                 for (int i = 0; i < len; ++i)
154                                         if (a [i] != b [i])
155                                                 return 1;
156                                 return 0;
157                         }
158                 }
159
160                 private int len;
161
162                 protected override int GetHash (Object key)
163                 {
164                         char[] arr = (char[])key;
165                         int h = 0;
166
167                         for (int i = 0; i < len; ++i)
168                                 h = (h << 5) - h + arr [i];
169
170                         return h;
171                 }
172
173                 public CharArrayHashtable (int len)
174                 {
175                         this.len = len;
176                         comparer = new ArrComparer (len);
177                 }
178         }
179
180         struct Pair {
181                 public object First;
182                 public object Second;
183
184                 public Pair (object f, object s)
185                 {
186                         First = f;
187                         Second = s;
188                 }
189         }
190
191         public class Accessors {
192                 public Accessor get_or_add;
193                 public Accessor set_or_remove;
194
195                 // was 'set' declared before 'get'?  was 'remove' declared before 'add'?
196                 public bool declared_in_reverse;
197
198                 public Accessors (Accessor get_or_add, Accessor set_or_remove)
199                 {
200                         this.get_or_add = get_or_add;
201                         this.set_or_remove = set_or_remove;
202                 }
203         }
204
205         /// <summary>
206         ///   This is a wrapper around StreamReader which is seekable backwards
207         ///   within a window of around 2048 chars.
208         /// </summary>
209         public class SeekableStreamReader
210         {
211                 const int AverageReadLength = 1024;
212                 TextReader reader;
213                 Stream stream;
214                 Encoding encoding;
215
216                 char[] buffer;
217                 int buffer_start;       // in chars
218                 int char_count;         // count buffer[] valid characters
219                 int pos;                // index into buffer[]
220
221                 public SeekableStreamReader (Stream stream, Encoding encoding)
222                 {
223                         this.stream = stream;
224                         this.encoding = encoding;
225                         
226                         this.reader = new StreamReader (stream, encoding, true);
227                         this.buffer = new char [AverageReadLength * 3];
228
229                         // Let the StreamWriter autodetect the encoder
230                         reader.Peek ();
231                 }
232
233                 /// <remarks>
234                 ///   This value corresponds to the current position in a stream of characters.
235                 ///   The StreamReader hides its manipulation of the underlying byte stream and all
236                 ///   character set/decoding issues.  Thus, we cannot use this position to guess at
237                 ///   the corresponding position in the underlying byte stream even though there is
238                 ///   a correlation between them.
239                 /// </remarks>
240                 public int Position {
241                         get { return buffer_start + pos; }
242
243                         set {
244                                 if (value > buffer_start + char_count)
245                                         throw new InternalErrorException ("can't seek that far forward: " + (pos - value));
246                                 
247                                 if (value < buffer_start){
248                                         // Reinitialize.
249                                         stream.Position = 0;
250                                         reader = new StreamReader (stream, encoding, true);
251                                         buffer_start = 0;
252                                         char_count = 0;
253                                         pos = 0;
254                                         Peek ();
255
256                                         while (value > buffer_start + char_count){
257                                                 pos = char_count+1;
258                                                 Peek ();
259                                         }
260                                         pos = value - buffer_start;
261                                 }
262
263                                 pos = value - buffer_start;
264                         }
265                 }
266
267                 private bool ReadBuffer ()
268                 {
269                         int slack = buffer.Length - char_count;
270                         if (slack <= AverageReadLength / 2) {
271                                 // shift the buffer to make room for AverageReadLength number of characters
272                                 int shift = AverageReadLength - slack;
273                                 Array.Copy (buffer, shift, buffer, 0, char_count - shift);
274                                 pos -= shift;
275                                 char_count -= shift;
276                                 buffer_start += shift;
277                                 slack += shift;         // slack == AverageReadLength
278                         }
279
280                         int chars_read = reader.Read (buffer, char_count, slack);
281                         char_count += chars_read;
282
283                         return pos < char_count;
284                 }
285
286                 public int Peek ()
287                 {
288                         if ((pos >= char_count) && !ReadBuffer ())
289                                 return -1;
290
291                         return buffer [pos];
292                 }
293
294                 public int Read ()
295                 {
296                         if ((pos >= char_count) && !ReadBuffer ())
297                                 return -1;
298
299                         return buffer [pos++];
300                 }
301         }
302
303         public class DoubleHash {
304                 const int DEFAULT_INITIAL_BUCKETS = 100;
305
306                 public DoubleHash () : this (DEFAULT_INITIAL_BUCKETS) {}
307
308                 public DoubleHash (int size)
309                 {
310                         count = size;
311                         buckets = new Entry [size];
312                 }
313
314                 int count;
315                 Entry [] buckets;
316                 int size = 0;
317
318                 class Entry {
319                         public object key1;
320                         public object key2;
321                         public int hash;
322                         public object value;
323                         public Entry next;
324
325                         public Entry (object key1, object key2, int hash, object value, Entry next)
326                         {
327                                 this.key1 = key1;
328                                 this.key2 = key2;
329                                 this.hash = hash;
330                                 this.next = next;
331                                 this.value = value;
332                         }
333                 }
334
335                 public bool Lookup (object a, object b, out object res)
336                 {
337                         int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
338
339                         for (Entry e = buckets [h % count]; e != null; e = e.next) {
340                                 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b)) {
341                                         res = e.value;
342                                         return true;
343                                 }
344                         }
345                         res = null;
346                         return false;
347                 }
348
349                 public void Insert (object a, object b, object value)
350                 {
351                         // Is it an existing one?
352
353                         int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
354
355                         for (Entry e = buckets [h % count]; e != null; e = e.next) {
356                                 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b))
357                                         e.value = value;
358                         }
359
360                         int bucket = h % count;
361                         buckets [bucket] = new Entry (a, b, h, value, buckets [bucket]);
362
363                         // Grow whenever we double in size
364                         if (size++ == count) {
365                                 count <<= 1;
366                                 count ++;
367
368                                 Entry [] newBuckets = new Entry [count];
369                                 foreach (Entry root in buckets) {
370                                         Entry e = root;
371                                         while (e != null) {
372                                                 int newLoc = e.hash % count;
373                                                 Entry n = e.next;
374                                                 e.next = newBuckets [newLoc];
375                                                 newBuckets [newLoc] = e;
376                                                 e = n;
377                                         }
378                                 }
379
380                                 buckets = newBuckets;
381                         }
382                 }
383         }
384
385         class PartialMethodDefinitionInfo : MethodInfo
386         {
387                 MethodOrOperator mc;
388                 MethodAttributes attrs;
389
390                 public PartialMethodDefinitionInfo (MethodOrOperator mc)
391                 {
392                         this.mc = mc;
393                         if ((mc.ModFlags & Modifiers.STATIC) != 0)
394                                 attrs = MethodAttributes.Static;
395                 }
396
397                 public override MethodInfo GetBaseDefinition ()
398                 {
399                         throw new NotImplementedException ();
400                 }
401
402                 public override ICustomAttributeProvider ReturnTypeCustomAttributes
403                 {
404                         get { throw new NotImplementedException (); }
405                 }
406
407                 public override MethodAttributes Attributes
408                 {
409                         get { return attrs; }
410                 }
411
412                 public override MethodImplAttributes GetMethodImplementationFlags ()
413                 {
414                         throw new NotImplementedException ();
415                 }
416
417                 public override ParameterInfo [] GetParameters ()
418                 {
419                         throw new NotImplementedException ();
420                 }
421
422                 public override object Invoke (object obj, BindingFlags invokeAttr, Binder binder, object [] parameters, CultureInfo culture)
423                 {
424                         throw new NotImplementedException ();
425                 }
426
427                 public override RuntimeMethodHandle MethodHandle
428                 {
429                         get { throw new NotImplementedException (); }
430                 }
431
432                 public override Type DeclaringType
433                 {
434                         get { return mc.Parent.TypeBuilder; }
435                 }
436
437                 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
438                 {
439                         throw new NotImplementedException ();
440                 }
441
442                 public override object [] GetCustomAttributes (bool inherit)
443                 {
444                         throw new NotImplementedException ();
445                 }
446
447                 public override Type ReturnType {
448                         get {
449                                 return mc.MemberType;
450                         }
451                 }
452
453                 public override bool IsDefined (Type attributeType, bool inherit)
454                 {
455                         throw new NotImplementedException ();
456                 }
457
458                 public override string Name
459                 {
460                         get { return mc.Name; }
461                 }
462
463                 public override Type ReflectedType
464                 {
465                         get { throw new NotImplementedException (); }
466                 }
467         }
468
469         public class UnixUtils {
470                 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
471                 extern static int _isatty (int fd);
472                         
473                 public static bool isatty (int fd)
474                 {
475                         try {
476                                 return _isatty (fd) == 1;
477                         } catch {
478                                 return false;
479                         }
480                 }
481         }
482 }