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 {
23 public class ReflectionConstraints : GenericConstraints
25 GenericParameterAttributes attrs;
27 Type class_constraint;
28 Type[] iface_constraints;
31 public static GenericConstraints GetConstraints (Type t)
33 Type [] constraints = t.GetGenericParameterConstraints ();
34 GenericParameterAttributes attrs = t.GenericParameterAttributes;
35 if (constraints.Length == 0 && attrs == GenericParameterAttributes.None)
37 return new ReflectionConstraints (t.Name, constraints, attrs);
40 private ReflectionConstraints (string name, Type [] constraints, GenericParameterAttributes attrs)
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);
50 iface_constraints = constraints;
52 if (HasValueTypeConstraint)
53 base_type = TypeManager.value_type;
54 else if (class_constraint != null)
55 base_type = class_constraint;
57 base_type = TypeManager.object_type;
60 public override string TypeParameter {
64 public override GenericParameterAttributes Attributes {
68 public override Type ClassConstraint {
69 get { return class_constraint; }
72 public override Type EffectiveBaseClass {
73 get { return base_type; }
76 public override Type[] InterfaceConstraints {
77 get { return iface_constraints; }
82 class PtrHashtable : Hashtable {
83 sealed class PtrComparer : IComparer {
84 private PtrComparer () {}
86 public static PtrComparer Instance = new PtrComparer ();
88 public int Compare (object x, object y)
97 public PtrHashtable ()
99 comparer = PtrComparer.Instance;
104 // Workaround System.InvalidOperationException for enums
106 protected override int GetHash (object key)
108 TypeBuilder tb = key as TypeBuilder;
109 if (tb != null && tb.BaseType == TypeManager.enum_type)
112 return base.GetHash (key);
118 * Hashtable whose keys are character arrays with the same length
120 class CharArrayHashtable : Hashtable {
121 sealed class ArrComparer : IComparer {
124 public ArrComparer (int len) {
128 public int Compare (object x, object y)
130 char[] a = (char[])x;
131 char[] b = (char[])y;
133 for (int i = 0; i < len; ++i)
142 protected override int GetHash (Object key)
144 char[] arr = (char[])key;
147 for (int i = 0; i < len; ++i)
148 h = (h << 5) - h + arr [i];
153 public CharArrayHashtable (int len)
156 comparer = new ArrComparer (len);
162 public object Second;
164 public Pair (object f, object s)
171 public class Accessors {
172 public Accessor get_or_add;
173 public Accessor set_or_remove;
175 // was 'set' declared before 'get'? was 'remove' declared before 'add'?
176 public bool declared_in_reverse;
178 public Accessors (Accessor get_or_add, Accessor set_or_remove)
180 this.get_or_add = get_or_add;
181 this.set_or_remove = set_or_remove;
186 /// This is a wrapper around StreamReader which is seekable backwards
187 /// within a window of around 2048 chars.
189 public class SeekableStreamReader
191 const int AverageReadLength = 1024;
197 int buffer_start; // in chars
198 int char_count; // count buffer[] valid characters
199 int pos; // index into buffer[]
201 public SeekableStreamReader (Stream stream, Encoding encoding)
203 this.stream = stream;
204 this.encoding = encoding;
206 this.reader = new StreamReader (stream, encoding, true);
207 this.buffer = new char [AverageReadLength * 3];
209 // Let the StreamWriter autodetect the encoder
214 /// This value corresponds to the current position in a stream of characters.
215 /// The StreamReader hides its manipulation of the underlying byte stream and all
216 /// character set/decoding issues. Thus, we cannot use this position to guess at
217 /// the corresponding position in the underlying byte stream even though there is
218 /// a correlation between them.
220 public int Position {
221 get { return buffer_start + pos; }
224 if (value > buffer_start + char_count)
225 throw new InternalErrorException ("can't seek that far forward: " + (pos - value));
227 if (value < buffer_start){
230 reader = new StreamReader (stream, encoding, true);
236 while (value > buffer_start + char_count){
240 pos = value - buffer_start;
243 pos = value - buffer_start;
247 private bool ReadBuffer ()
249 int slack = buffer.Length - char_count;
250 if (slack <= AverageReadLength / 2) {
251 // shift the buffer to make room for AverageReadLength number of characters
252 int shift = AverageReadLength - slack;
253 Array.Copy (buffer, shift, buffer, 0, char_count - shift);
256 buffer_start += shift;
257 slack += shift; // slack == AverageReadLength
260 int chars_read = reader.Read (buffer, char_count, slack);
261 char_count += chars_read;
263 return pos < char_count;
268 if ((pos >= char_count) && !ReadBuffer ())
276 if ((pos >= char_count) && !ReadBuffer ())
279 return buffer [pos++];
283 public class DoubleHash {
284 const int DEFAULT_INITIAL_BUCKETS = 100;
286 public DoubleHash () : this (DEFAULT_INITIAL_BUCKETS) {}
288 public DoubleHash (int size)
291 buckets = new Entry [size];
305 public Entry (object key1, object key2, int hash, object value, Entry next)
315 public bool Lookup (object a, object b, out object res)
317 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
319 for (Entry e = buckets [h % count]; e != null; e = e.next) {
320 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b)) {
329 public void Insert (object a, object b, object value)
331 // Is it an existing one?
333 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
335 for (Entry e = buckets [h % count]; e != null; e = e.next) {
336 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b))
340 int bucket = h % count;
341 buckets [bucket] = new Entry (a, b, h, value, buckets [bucket]);
343 // Grow whenever we double in size
344 if (size++ == count) {
348 Entry [] newBuckets = new Entry [count];
349 foreach (Entry root in buckets) {
352 int newLoc = e.hash % count;
354 e.next = newBuckets [newLoc];
355 newBuckets [newLoc] = e;
360 buckets = newBuckets;
365 class PartialMethodDefinitionInfo : MethodInfo
368 MethodAttributes attrs;
370 public PartialMethodDefinitionInfo (MethodOrOperator mc)
373 if ((mc.ModFlags & Modifiers.STATIC) != 0)
374 attrs = MethodAttributes.Static;
377 public override MethodInfo GetBaseDefinition ()
379 throw new NotImplementedException ();
382 public override ICustomAttributeProvider ReturnTypeCustomAttributes
384 get { throw new NotImplementedException (); }
387 public override MethodAttributes Attributes
389 get { return attrs; }
392 public override MethodImplAttributes GetMethodImplementationFlags ()
394 throw new NotImplementedException ();
397 public override ParameterInfo [] GetParameters ()
399 throw new NotImplementedException ();
402 public override object Invoke (object obj, BindingFlags invokeAttr, Binder binder, object [] parameters, CultureInfo culture)
404 throw new NotImplementedException ();
407 public override RuntimeMethodHandle MethodHandle
409 get { throw new NotImplementedException (); }
412 public override Type DeclaringType
414 get { return mc.Parent.TypeBuilder; }
417 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
419 throw new NotImplementedException ();
422 public override object [] GetCustomAttributes (bool inherit)
424 throw new NotImplementedException ();
427 public override Type ReturnType {
429 return mc.MemberType;
433 public override bool IsDefined (Type attributeType, bool inherit)
435 throw new NotImplementedException ();
438 public override string Name
440 get { return mc.Name; }
443 public override Type ReflectedType
445 get { throw new NotImplementedException (); }