2 // support.cs: Support routines to work around the fact that System.Reflection.Emit
3 // can not introspect types that are being constructed
6 // Miguel de Icaza (miguel@ximian.com)
8 // Copyright 2001 Ximian, Inc (http://www.ximian.com)
9 // Copyright 2003-2008 Novell, Inc
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)
37 bool IEqualityComparer.Equals (object x, object y)
42 int IEqualityComparer.GetHashCode (object obj)
44 return obj.GetHashCode ();
48 public PtrHashtable () : base (PtrComparer.Instance) {}
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)
60 return base.GetHash (key);
69 public Pair (object f, object 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;
91 /// This is an arbitrarily seekable StreamReader wrapper.
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.
97 public class SeekableStreamReader
99 const int default_average_read_length = 1024;
100 const int buffer_read_length_spans = 3;
107 int average_read_length;
108 int buffer_start; // in chars
109 int char_count; // count buffer[] valid characters
110 int pos; // index into buffer[]
112 void ResetStream (int read_length_inc)
114 average_read_length += read_length_inc;
116 reader = new StreamReader (stream, encoding, true);
117 buffer = new char [average_read_length * buffer_read_length_spans];
118 buffer_start = char_count = pos = 0;
121 public SeekableStreamReader (Stream stream, Encoding encoding)
123 this.stream = stream;
124 this.encoding = encoding;
126 ResetStream (default_average_read_length);
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.
136 public int Position {
137 get { return buffer_start + pos; }
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) {
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);
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;
174 if ((pos >= char_count) && !ReadBuffer ())
182 if ((pos >= char_count) && !ReadBuffer ())
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)
197 buckets = new Entry [size];
211 public Entry (object key1, object key2, int hash, object value, Entry next)
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)) {
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))
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) {
254 Entry [] newBuckets = new Entry [count];
255 foreach (Entry root in buckets) {
258 int newLoc = e.hash % count;
260 e.next = newBuckets [newLoc];
261 newBuckets [newLoc] = e;
266 buckets = newBuckets;
271 class PartialMethodDefinitionInfo : MethodInfo
274 MethodAttributes attrs;
276 public PartialMethodDefinitionInfo (MethodOrOperator 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 {
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")]
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 {
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 ()
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 ()
480 protected override bool IsByRefImpl ()
485 protected override bool IsCOMObjectImpl ()
490 protected override bool IsPointerImpl ()
495 protected override bool IsPrimitiveImpl ()
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")]
550 class DynamicArrayType : Type
553 Type reflection_type;
555 public DynamicArrayType (int 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 ()
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 ()
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 ()
685 protected override bool IsByRefImpl ()
690 protected override bool IsCOMObjectImpl ()
695 protected override bool IsPointerImpl ()
700 protected override bool IsPrimitiveImpl ()
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 {
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)
761 return _isatty (fd) == 1;
769 /// An exception used to terminate the compiler resolution phase and provide completions
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.
776 public class CompletionResult : Exception {
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;
790 public string [] Result {
796 public string BaseText {