2009-11-24 Jb Evain <jbevain@novell.com>
[mcs.git] / mcs / support.cs
blob736d01cbd1741889f4aed2d5e02e486b904b95e6
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
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;
20 namespace Mono.CSharp {
22 class PtrHashtable : Hashtable {
23 sealed class PtrComparer : IComparer, IEqualityComparer
25 private PtrComparer () {}
27 public static PtrComparer Instance = new PtrComparer ();
29 public int Compare (object x, object y)
31 if (x == y)
32 return 0;
33 else
34 return 1;
37 bool IEqualityComparer.Equals (object x, object y)
39 return x == y;
42 int IEqualityComparer.GetHashCode (object obj)
44 return obj.GetHashCode ();
48 public PtrHashtable () : base (PtrComparer.Instance) {}
50 #if MS_COMPATIBLE
52 // Workaround System.InvalidOperationException for enums
54 protected override int GetHash (object key)
56 TypeBuilder tb = key as TypeBuilder;
57 if (tb != null && tb.BaseType == TypeManager.enum_type && tb.BaseType != null)
58 key = tb.BaseType;
60 return base.GetHash (key);
62 #endif
65 struct Pair {
66 public object First;
67 public object Second;
69 public Pair (object f, object s)
71 First = f;
72 Second = s;
76 public class Accessors {
77 public Accessor get_or_add;
78 public Accessor set_or_remove;
80 // was 'set' declared before 'get'? was 'remove' declared before 'add'?
81 public bool declared_in_reverse;
83 public Accessors (Accessor get_or_add, Accessor set_or_remove)
85 this.get_or_add = get_or_add;
86 this.set_or_remove = set_or_remove;
90 /// <summary>
91 /// This is an arbitrarily seekable StreamReader wrapper.
92 ///
93 /// It uses a self-tuning buffer to cache the seekable data,
94 /// but if the seek is too far, it may read the underly
95 /// stream all over from the beginning.
96 /// </summary>
97 public class SeekableStreamReader
99 const int buffer_read_length_spans = 3;
101 TextReader reader;
102 Stream stream;
103 Encoding encoding;
105 char[] buffer;
106 int average_read_length;
107 int buffer_start; // in chars
108 int char_count; // count buffer[] valid characters
109 int pos; // index into buffer[]
111 void ResetStream (int read_length_inc)
113 average_read_length += read_length_inc;
114 stream.Position = 0;
115 reader = new StreamReader (stream, encoding, true, average_read_length);
116 buffer = new char [average_read_length * buffer_read_length_spans];
117 buffer_start = char_count = pos = 0;
120 public SeekableStreamReader (Stream stream, Encoding encoding)
122 this.stream = stream;
123 this.encoding = encoding;
125 const int default_average_read_length = 1024;
126 ResetStream (default_average_read_length);
129 /// <remarks>
130 /// This value corresponds to the current position in a stream of characters.
131 /// The StreamReader hides its manipulation of the underlying byte stream and all
132 /// character set/decoding issues. Thus, we cannot use this position to guess at
133 /// the corresponding position in the underlying byte stream even though there is
134 /// a correlation between them.
135 /// </remarks>
136 public int Position {
137 get { return buffer_start + pos; }
139 set {
140 // If the lookahead was too small, re-read from the beginning. Increase the buffer size while we're at it
141 if (value < buffer_start)
142 ResetStream (average_read_length / 2);
144 while (value > buffer_start + char_count) {
145 pos = char_count;
146 if (!ReadBuffer ())
147 throw new InternalErrorException ("Seek beyond end of file: " + (buffer_start + char_count - value));
150 pos = value - buffer_start;
154 private bool ReadBuffer ()
156 int slack = buffer.Length - char_count;
157 if (slack <= average_read_length / 2) {
158 // shift the buffer to make room for average_read_length number of characters
159 int shift = average_read_length - slack;
160 Array.Copy (buffer, shift, buffer, 0, char_count - shift);
161 pos -= shift;
162 char_count -= shift;
163 buffer_start += shift;
164 slack += shift; // slack == average_read_length
167 char_count += reader.Read (buffer, char_count, slack);
169 return pos < char_count;
172 public int Peek ()
174 if ((pos >= char_count) && !ReadBuffer ())
175 return -1;
177 return buffer [pos];
180 public int Read ()
182 if ((pos >= char_count) && !ReadBuffer ())
183 return -1;
185 return buffer [pos++];
189 public class DoubleHash {
190 const int DEFAULT_INITIAL_BUCKETS = 100;
192 public DoubleHash () : this (DEFAULT_INITIAL_BUCKETS) {}
194 public DoubleHash (int size)
196 count = size;
197 buckets = new Entry [size];
200 int count;
201 Entry [] buckets;
202 int size = 0;
204 class Entry {
205 public object key1;
206 public object key2;
207 public int hash;
208 public object value;
209 public Entry next;
211 public Entry (object key1, object key2, int hash, object value, Entry next)
213 this.key1 = key1;
214 this.key2 = key2;
215 this.hash = hash;
216 this.next = next;
217 this.value = value;
221 public bool Lookup (object a, object b, out object res)
223 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
225 for (Entry e = buckets [h % count]; e != null; e = e.next) {
226 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b)) {
227 res = e.value;
228 return true;
231 res = null;
232 return false;
235 public void Insert (object a, object b, object value)
237 // Is it an existing one?
239 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
241 for (Entry e = buckets [h % count]; e != null; e = e.next) {
242 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b))
243 e.value = value;
246 int bucket = h % count;
247 buckets [bucket] = new Entry (a, b, h, value, buckets [bucket]);
249 // Grow whenever we double in size
250 if (size++ == count) {
251 count <<= 1;
252 count ++;
254 Entry [] newBuckets = new Entry [count];
255 foreach (Entry root in buckets) {
256 Entry e = root;
257 while (e != null) {
258 int newLoc = e.hash % count;
259 Entry n = e.next;
260 e.next = newBuckets [newLoc];
261 newBuckets [newLoc] = e;
262 e = n;
266 buckets = newBuckets;
271 class PartialMethodDefinitionInfo : MethodInfo
273 MethodOrOperator mc;
274 MethodAttributes attrs;
276 public PartialMethodDefinitionInfo (MethodOrOperator mc)
278 this.mc = mc;
279 if ((mc.ModFlags & Modifiers.STATIC) != 0)
280 attrs = MethodAttributes.Static;
283 public override MethodInfo GetBaseDefinition ()
285 throw new NotImplementedException ();
288 public override ICustomAttributeProvider ReturnTypeCustomAttributes
290 get { throw new NotImplementedException (); }
293 public override MethodAttributes Attributes
295 get { return attrs; }
298 public override MethodImplAttributes GetMethodImplementationFlags ()
300 throw new NotImplementedException ();
303 public override ParameterInfo [] GetParameters ()
305 throw new NotImplementedException ();
308 public override object Invoke (object obj, BindingFlags invokeAttr, Binder binder, object [] parameters, CultureInfo culture)
310 throw new NotImplementedException ();
313 public override RuntimeMethodHandle MethodHandle
315 get { throw new NotImplementedException (); }
318 public override Type DeclaringType
320 get { return mc.Parent.TypeBuilder; }
323 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
325 throw new NotImplementedException ();
328 public override object [] GetCustomAttributes (bool inherit)
330 throw new NotImplementedException ();
333 public override Type ReturnType {
334 get {
335 return mc.MemberType;
339 public override bool IsDefined (Type attributeType, bool inherit)
341 throw new NotImplementedException ();
344 public override string Name
346 get { return mc.Name; }
349 public override Type ReflectedType
351 get { throw new NotImplementedException (); }
355 #if NET_4_0 || MS_COMPATIBLE
356 [System.Diagnostics.DebuggerDisplay ("Dynamic type")]
357 #endif
358 class DynamicType : Type
360 public override Assembly Assembly {
361 get { return UnderlyingSystemType.Assembly; }
364 public override string AssemblyQualifiedName {
365 get { throw new NotImplementedException (); }
368 public override Type BaseType {
369 get { return null; }
372 public override string FullName {
373 get { return UnderlyingSystemType.FullName; }
376 public override Guid GUID {
377 get { throw new NotImplementedException (); }
380 protected override TypeAttributes GetAttributeFlagsImpl ()
382 return UnderlyingSystemType.Attributes;
385 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
387 throw new NotImplementedException ();
390 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
392 throw new NotImplementedException ();
395 public override Type GetElementType ()
397 throw new NotImplementedException ();
400 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
402 throw new NotImplementedException ();
405 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
407 throw new NotImplementedException ();
410 public override FieldInfo GetField (string name, BindingFlags bindingAttr)
412 throw new NotImplementedException ();
415 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
417 throw new NotImplementedException ();
420 public override Type GetInterface (string name, bool ignoreCase)
422 throw new NotImplementedException ();
425 public override Type[] GetInterfaces ()
427 return Type.EmptyTypes;
430 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
432 throw new NotImplementedException ();
435 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
437 throw new NotImplementedException ();
440 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
442 throw new NotImplementedException ();
445 public override Type GetNestedType (string name, BindingFlags bindingAttr)
447 throw new NotImplementedException ();
450 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
452 throw new NotImplementedException ();
455 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
457 throw new NotImplementedException ();
460 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
462 throw new NotImplementedException ();
465 protected override bool HasElementTypeImpl ()
467 return false;
470 public override object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, System.Globalization.CultureInfo culture, string[] namedParameters)
472 throw new NotImplementedException ();
475 protected override bool IsArrayImpl ()
477 return false;
480 protected override bool IsByRefImpl ()
482 return false;
485 protected override bool IsCOMObjectImpl ()
487 return false;
490 protected override bool IsPointerImpl ()
492 return false;
495 protected override bool IsPrimitiveImpl ()
497 return false;
500 public override Module Module {
501 get { return UnderlyingSystemType.Module; }
504 public override string Namespace {
505 get { return UnderlyingSystemType.Namespace; }
508 public override Type UnderlyingSystemType {
509 get { return TypeManager.object_type; }
512 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
514 return new object [0];
517 public override object[] GetCustomAttributes (bool inherit)
519 return new object [0];
522 public override bool IsDefined (Type attributeType, bool inherit)
524 throw new NotImplementedException ();
527 public override string Name {
528 get { return UnderlyingSystemType.Name; }
531 public override string ToString ()
533 return UnderlyingSystemType.ToString ();
536 public override RuntimeTypeHandle TypeHandle {
537 get { return UnderlyingSystemType.TypeHandle; }
540 public override Type MakeByRefType ()
542 // TODO: Wrong, hides dynamic type
543 return UnderlyingSystemType.MakeByRefType ();
547 #if NET_4_0 || MS_COMPATIBLE
548 [System.Diagnostics.DebuggerDisplay ("Dynamic array type")]
549 #endif
550 class DynamicArrayType : Type
552 readonly int rank;
553 Type reflection_type;
555 public DynamicArrayType (int rank)
557 this.rank = rank;
560 public override Assembly Assembly {
561 get { return UnderlyingSystemType.Assembly; }
564 public override string AssemblyQualifiedName {
565 get { throw new NotImplementedException (); }
568 public override Type BaseType {
569 get { return TypeManager.array_type; }
572 public override string FullName {
573 get { return UnderlyingSystemType.FullName; }
576 public override Guid GUID {
577 get { throw new NotImplementedException (); }
580 protected override TypeAttributes GetAttributeFlagsImpl ()
582 return UnderlyingSystemType.Attributes;
585 public override int GetArrayRank ()
587 return rank;
590 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
592 throw new NotImplementedException ();
595 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
597 throw new NotImplementedException ();
600 public override Type GetElementType ()
602 return InternalType.Dynamic;
605 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
607 throw new NotImplementedException ();
610 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
612 throw new NotImplementedException ();
615 public override FieldInfo GetField (string name, BindingFlags bindingAttr)
617 throw new NotImplementedException ();
620 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
622 throw new NotImplementedException ();
625 public override Type GetInterface (string name, bool ignoreCase)
627 throw new NotImplementedException ();
630 public override Type[] GetInterfaces ()
632 return Type.EmptyTypes;
635 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
637 throw new NotImplementedException ();
640 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
642 throw new NotImplementedException ();
645 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
647 throw new NotImplementedException ();
650 public override Type GetNestedType (string name, BindingFlags bindingAttr)
652 throw new NotImplementedException ();
655 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
657 throw new NotImplementedException ();
660 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
662 throw new NotImplementedException ();
665 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
667 throw new NotImplementedException ();
670 protected override bool HasElementTypeImpl ()
672 return true;
675 public override object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, System.Globalization.CultureInfo culture, string[] namedParameters)
677 throw new NotImplementedException ();
680 protected override bool IsArrayImpl ()
682 return true;
685 protected override bool IsByRefImpl ()
687 return false;
690 protected override bool IsCOMObjectImpl ()
692 return false;
695 protected override bool IsPointerImpl ()
697 return false;
700 protected override bool IsPrimitiveImpl ()
702 return false;
705 public override Module Module {
706 get { return UnderlyingSystemType.Module; }
709 public override string Namespace {
710 get { return UnderlyingSystemType.Namespace; }
713 public override Type UnderlyingSystemType {
714 get {
715 if (reflection_type == null) {
716 reflection_type = rank == 1 ?
717 TypeManager.object_type.MakeArrayType () :
718 TypeManager.object_type.MakeArrayType (rank);
721 return reflection_type;
725 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
727 return new object [0];
730 public override object[] GetCustomAttributes (bool inherit)
732 return new object [0];
735 public override bool IsDefined (Type attributeType, bool inherit)
737 throw new NotImplementedException ();
740 public override string Name {
741 get { return UnderlyingSystemType.Name; }
744 public override string ToString ()
746 return UnderlyingSystemType.ToString ();
749 public override RuntimeTypeHandle TypeHandle {
750 get { return UnderlyingSystemType.TypeHandle; }
754 public class UnixUtils {
755 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
756 extern static int _isatty (int fd);
758 public static bool isatty (int fd)
760 try {
761 return _isatty (fd) == 1;
762 } catch {
763 return false;
768 /// <summary>
769 /// An exception used to terminate the compiler resolution phase and provide completions
770 /// </summary>
771 /// <remarks>
772 /// This is thrown when we want to return the completions or
773 /// terminate the completion process by AST nodes used in
774 /// the completion process.
775 /// </remarks>
776 public class CompletionResult : Exception {
777 string [] result;
778 string base_text;
780 public CompletionResult (string base_text, string [] res)
782 if (base_text == null)
783 throw new ArgumentNullException ("base_text");
784 this.base_text = base_text;
786 result = res;
787 Array.Sort (result);
790 public string [] Result {
791 get {
792 return result;
796 public string BaseText {
797 get {
798 return base_text;