2009-08-03 Marek Safar <marek.safar@gmail.com>
[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         class PtrHashtable : Hashtable {
23                 sealed class PtrComparer : IComparer
24 #if NET_2_0
25                         , IEqualityComparer
26 #endif
27                 {
28                         private PtrComparer () {}
29
30                         public static PtrComparer Instance = new PtrComparer ();
31
32                         public int Compare (object x, object y)
33                         {
34                                 if (x == y)
35                                         return 0;
36                                 else
37                                         return 1;
38                         }
39 #if NET_2_0
40                         bool IEqualityComparer.Equals (object x, object y)
41                         {
42                                 return x == y;
43                         }
44                         
45                         int IEqualityComparer.GetHashCode (object obj)
46                         {
47                                 return obj.GetHashCode ();
48                         }
49 #endif
50                         
51                 }
52
53 #if NET_2_0
54                 public PtrHashtable () : base (PtrComparer.Instance) {}
55 #else
56                 public PtrHashtable () 
57                 {
58                         comparer = PtrComparer.Instance;
59                 }
60 #endif
61
62 #if MS_COMPATIBLE
63                 //
64                 // Workaround System.InvalidOperationException for enums
65                 //
66                 protected override int GetHash (object key)
67                 {
68                         TypeBuilder tb = key as TypeBuilder;
69                         if (tb != null && tb.BaseType == TypeManager.enum_type && tb.BaseType != null)
70                                 key = tb.BaseType;
71
72                         return base.GetHash (key);
73                 }
74 #endif
75         }
76
77         /*
78          * Hashtable whose keys are character arrays with the same length
79          */
80         class CharArrayHashtable : Hashtable {
81                 sealed class ArrComparer : IComparer {
82                         private int len;
83
84                         public ArrComparer (int len) {
85                                 this.len = len;
86                         }
87
88                         public int Compare (object x, object y)
89                         {
90                                 char[] a = (char[])x;
91                                 char[] b = (char[])y;
92
93                                 for (int i = 0; i < len; ++i)
94                                         if (a [i] != b [i])
95                                                 return 1;
96                                 return 0;
97                         }
98                 }
99
100                 private int len;
101
102                 protected override int GetHash (Object key)
103                 {
104                         char[] arr = (char[])key;
105                         int h = 0;
106
107                         for (int i = 0; i < len; ++i)
108                                 h = (h << 5) - h + arr [i];
109
110                         return h;
111                 }
112
113                 public CharArrayHashtable (int len)
114                 {
115                         this.len = len;
116                         comparer = new ArrComparer (len);
117                 }
118         }
119
120         struct Pair {
121                 public object First;
122                 public object Second;
123
124                 public Pair (object f, object s)
125                 {
126                         First = f;
127                         Second = s;
128                 }
129         }
130
131         public class Accessors {
132                 public Accessor get_or_add;
133                 public Accessor set_or_remove;
134
135                 // was 'set' declared before 'get'?  was 'remove' declared before 'add'?
136                 public bool declared_in_reverse;
137
138                 public Accessors (Accessor get_or_add, Accessor set_or_remove)
139                 {
140                         this.get_or_add = get_or_add;
141                         this.set_or_remove = set_or_remove;
142                 }
143         }
144
145         /// <summary>
146         ///   This is an arbitrarily seekable StreamReader wrapper.
147         ///
148         ///   It uses a self-tuning buffer to cache the seekable data,
149         ///   but if the seek is too far, it may read the underly
150         ///   stream all over from the beginning.
151         /// </summary>
152         public class SeekableStreamReader
153         {
154                 const int default_average_read_length = 1024;
155                 const int buffer_read_length_spans = 3;
156
157                 TextReader reader;
158                 Stream stream;
159                 Encoding encoding;
160
161                 char[] buffer;
162                 int average_read_length;
163                 int buffer_start;       // in chars
164                 int char_count;         // count buffer[] valid characters
165                 int pos;                // index into buffer[]
166
167                 void ResetStream (int read_length_inc)
168                 {
169                         average_read_length += read_length_inc;
170                         stream.Position = 0;
171                         reader = new StreamReader (stream, encoding, true);
172                         buffer = new char [average_read_length * buffer_read_length_spans];
173                         buffer_start = char_count = pos = 0;
174                 }
175
176                 public SeekableStreamReader (Stream stream, Encoding encoding)
177                 {
178                         this.stream = stream;
179                         this.encoding = encoding;
180
181                         ResetStream (default_average_read_length);
182                 }
183
184                 /// <remarks>
185                 ///   This value corresponds to the current position in a stream of characters.
186                 ///   The StreamReader hides its manipulation of the underlying byte stream and all
187                 ///   character set/decoding issues.  Thus, we cannot use this position to guess at
188                 ///   the corresponding position in the underlying byte stream even though there is
189                 ///   a correlation between them.
190                 /// </remarks>
191                 public int Position {
192                         get { return buffer_start + pos; }
193
194                         set {
195                                 // If the lookahead was too small, re-read from the beginning.  Increase the buffer size while we're at it
196                                 if (value < buffer_start)
197                                         ResetStream (average_read_length / 2);
198
199                                 while (value > buffer_start + char_count) {
200                                         pos = char_count;
201                                         if (!ReadBuffer ())
202                                                 throw new InternalErrorException ("Seek beyond end of file: " + (buffer_start + char_count - value));
203                                 }
204
205                                 pos = value - buffer_start;
206                         }
207                 }
208
209                 private bool ReadBuffer ()
210                 {
211                         int slack = buffer.Length - char_count;
212                         if (slack <= average_read_length / 2) {
213                                 // shift the buffer to make room for average_read_length number of characters
214                                 int shift = average_read_length - slack;
215                                 Array.Copy (buffer, shift, buffer, 0, char_count - shift);
216                                 pos -= shift;
217                                 char_count -= shift;
218                                 buffer_start += shift;
219                                 slack += shift;         // slack == average_read_length
220                         }
221
222                         char_count += reader.Read (buffer, char_count, slack);
223
224                         return pos < char_count;
225                 }
226
227                 public int Peek ()
228                 {
229                         if ((pos >= char_count) && !ReadBuffer ())
230                                 return -1;
231
232                         return buffer [pos];
233                 }
234
235                 public int Read ()
236                 {
237                         if ((pos >= char_count) && !ReadBuffer ())
238                                 return -1;
239
240                         return buffer [pos++];
241                 }
242         }
243
244         public class DoubleHash {
245                 const int DEFAULT_INITIAL_BUCKETS = 100;
246
247                 public DoubleHash () : this (DEFAULT_INITIAL_BUCKETS) {}
248
249                 public DoubleHash (int size)
250                 {
251                         count = size;
252                         buckets = new Entry [size];
253                 }
254
255                 int count;
256                 Entry [] buckets;
257                 int size = 0;
258
259                 class Entry {
260                         public object key1;
261                         public object key2;
262                         public int hash;
263                         public object value;
264                         public Entry next;
265
266                         public Entry (object key1, object key2, int hash, object value, Entry next)
267                         {
268                                 this.key1 = key1;
269                                 this.key2 = key2;
270                                 this.hash = hash;
271                                 this.next = next;
272                                 this.value = value;
273                         }
274                 }
275
276                 public bool Lookup (object a, object b, out object res)
277                 {
278                         int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
279
280                         for (Entry e = buckets [h % count]; e != null; e = e.next) {
281                                 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b)) {
282                                         res = e.value;
283                                         return true;
284                                 }
285                         }
286                         res = null;
287                         return false;
288                 }
289
290                 public void Insert (object a, object b, object value)
291                 {
292                         // Is it an existing one?
293
294                         int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
295
296                         for (Entry e = buckets [h % count]; e != null; e = e.next) {
297                                 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b))
298                                         e.value = value;
299                         }
300
301                         int bucket = h % count;
302                         buckets [bucket] = new Entry (a, b, h, value, buckets [bucket]);
303
304                         // Grow whenever we double in size
305                         if (size++ == count) {
306                                 count <<= 1;
307                                 count ++;
308
309                                 Entry [] newBuckets = new Entry [count];
310                                 foreach (Entry root in buckets) {
311                                         Entry e = root;
312                                         while (e != null) {
313                                                 int newLoc = e.hash % count;
314                                                 Entry n = e.next;
315                                                 e.next = newBuckets [newLoc];
316                                                 newBuckets [newLoc] = e;
317                                                 e = n;
318                                         }
319                                 }
320
321                                 buckets = newBuckets;
322                         }
323                 }
324         }
325
326         class PartialMethodDefinitionInfo : MethodInfo
327         {
328                 MethodOrOperator mc;
329                 MethodAttributes attrs;
330
331                 public PartialMethodDefinitionInfo (MethodOrOperator mc)
332                 {
333                         this.mc = mc;
334                         if ((mc.ModFlags & Modifiers.STATIC) != 0)
335                                 attrs = MethodAttributes.Static;
336                 }
337
338                 public override MethodInfo GetBaseDefinition ()
339                 {
340                         throw new NotImplementedException ();
341                 }
342
343                 public override ICustomAttributeProvider ReturnTypeCustomAttributes
344                 {
345                         get { throw new NotImplementedException (); }
346                 }
347
348                 public override MethodAttributes Attributes
349                 {
350                         get { return attrs; }
351                 }
352
353                 public override MethodImplAttributes GetMethodImplementationFlags ()
354                 {
355                         throw new NotImplementedException ();
356                 }
357
358                 public override ParameterInfo [] GetParameters ()
359                 {
360                         throw new NotImplementedException ();
361                 }
362
363                 public override object Invoke (object obj, BindingFlags invokeAttr, Binder binder, object [] parameters, CultureInfo culture)
364                 {
365                         throw new NotImplementedException ();
366                 }
367
368                 public override RuntimeMethodHandle MethodHandle
369                 {
370                         get { throw new NotImplementedException (); }
371                 }
372
373                 public override Type DeclaringType
374                 {
375                         get { return mc.Parent.TypeBuilder; }
376                 }
377
378                 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
379                 {
380                         throw new NotImplementedException ();
381                 }
382
383                 public override object [] GetCustomAttributes (bool inherit)
384                 {
385                         throw new NotImplementedException ();
386                 }
387
388                 public override Type ReturnType {
389                         get {
390                                 return mc.MemberType;
391                         }
392                 }
393
394                 public override bool IsDefined (Type attributeType, bool inherit)
395                 {
396                         throw new NotImplementedException ();
397                 }
398
399                 public override string Name
400                 {
401                         get { return mc.Name; }
402                 }
403
404                 public override Type ReflectedType
405                 {
406                         get { throw new NotImplementedException (); }
407                 }
408         }
409
410 #if NET_4_0
411         [System.Diagnostics.DebuggerDisplay ("Dynamic type")]
412 #endif
413         class DynamicType : Type
414         {
415                 public override Assembly Assembly {
416                         get { return UnderlyingSystemType.Assembly; }
417                 }
418
419                 public override string AssemblyQualifiedName {
420                         get { throw new NotImplementedException (); }
421                 }
422
423                 public override Type BaseType {
424                         get { return null; }
425                 }
426
427                 public override string FullName {
428                         get { return UnderlyingSystemType.FullName; }
429                 }
430
431                 public override Guid GUID {
432                         get { throw new NotImplementedException (); }
433                 }
434
435                 protected override TypeAttributes GetAttributeFlagsImpl ()
436                 {
437                         return UnderlyingSystemType.Attributes;
438                 }
439
440                 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
441                 {
442                         throw new NotImplementedException ();
443                 }
444
445                 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
446                 {
447                         throw new NotImplementedException ();
448                 }
449
450                 public override Type GetElementType ()
451                 {
452                         throw new NotImplementedException ();
453                 }
454
455                 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
456                 {
457                         throw new NotImplementedException ();
458                 }
459
460                 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
461                 {
462                         throw new NotImplementedException ();
463                 }
464
465                 public override FieldInfo GetField (string name, BindingFlags bindingAttr)
466                 {
467                         throw new NotImplementedException ();
468                 }
469
470                 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
471                 {
472                         throw new NotImplementedException ();
473                 }
474
475                 public override Type GetInterface (string name, bool ignoreCase)
476                 {
477                         throw new NotImplementedException ();
478                 }
479
480                 public override Type[] GetInterfaces ()
481                 {
482                         throw new NotImplementedException ();
483                 }
484
485                 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
486                 {
487                         throw new NotImplementedException ();
488                 }
489
490                 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
491                 {
492                         throw new NotImplementedException ();
493                 }
494
495                 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
496                 {
497                         throw new NotImplementedException ();
498                 }
499
500                 public override Type GetNestedType (string name, BindingFlags bindingAttr)
501                 {
502                         throw new NotImplementedException ();
503                 }
504
505                 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
506                 {
507                         throw new NotImplementedException ();
508                 }
509
510                 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
511                 {
512                         throw new NotImplementedException ();
513                 }
514
515                 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
516                 {
517                         throw new NotImplementedException ();
518                 }
519
520                 protected override bool HasElementTypeImpl ()
521                 {
522                         throw new NotImplementedException ();
523                 }
524
525                 public override object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, System.Globalization.CultureInfo culture, string[] namedParameters)
526                 {
527                         throw new NotImplementedException ();
528                 }
529
530                 protected override bool IsArrayImpl ()
531                 {
532                         return false;
533                 }
534
535                 protected override bool IsByRefImpl ()
536                 {
537                         return false;
538                 }
539
540                 protected override bool IsCOMObjectImpl ()
541                 {
542                         return false;
543                 }
544
545                 protected override bool IsPointerImpl ()
546                 {
547                         return false;
548                 }
549
550                 protected override bool IsPrimitiveImpl ()
551                 {
552                         return false;
553                 }
554
555                 public override Module Module {
556                         get { return UnderlyingSystemType.Module; }
557                 }
558
559                 public override string Namespace {
560                         get { throw new NotImplementedException (); }
561                 }
562
563                 public override Type UnderlyingSystemType {
564                         get { return TypeManager.object_type; }
565                 }
566
567                 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
568                 {
569                         return new object [0];
570                 }
571
572                 public override object[] GetCustomAttributes (bool inherit)
573                 {
574                         return new object [0];
575                 }
576
577                 public override bool IsDefined (Type attributeType, bool inherit)
578                 {
579                         throw new NotImplementedException ();
580                 }
581
582                 public override string Name {
583                         get { return UnderlyingSystemType.Name; }
584                 }
585
586                 public override string ToString ()
587                 {
588                         return UnderlyingSystemType.ToString ();
589                 }
590
591                 public override RuntimeTypeHandle TypeHandle {
592                         get { return UnderlyingSystemType.TypeHandle; }
593                 }
594         }
595
596         public class UnixUtils {
597                 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
598                 extern static int _isatty (int fd);
599                         
600                 public static bool isatty (int fd)
601                 {
602                         try {
603                                 return _isatty (fd) == 1;
604                         } catch {
605                                 return false;
606                         }
607                 }
608         }
609
610         /// <summary>
611         ///   An exception used to terminate the compiler resolution phase and provide completions
612         /// </summary>
613         /// <remarks>
614         ///   This is thrown when we want to return the completions or
615         ///   terminate the completion process by AST nodes used in
616         ///   the completion process.
617         /// </remarks>
618         public class CompletionResult : Exception {
619                 string [] result;
620                 string base_text;
621                 
622                 public CompletionResult (string base_text, string [] res)
623                 {
624                         if (base_text == null)
625                                 throw new ArgumentNullException ("base_text");
626                         this.base_text = base_text;
627
628                         result = res;
629                         Array.Sort (result);
630                 }
631
632                 public string [] Result {
633                         get {
634                                 return result;
635                         }
636                 }
637
638                 public string BaseText {
639                         get {
640                                 return base_text;
641                         }
642                 }
643         }
644 }