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 // (C) 2001 Ximian, Inc (http://www.ximian.com)
14 using System.Reflection;
15 using System.Collections;
16 using System.Reflection.Emit;
17 using System.Globalization;
19 namespace Mono.CSharp {
21 public interface ParameterData {
22 Type ParameterType (int pos);
23 Type [] Types { get; }
25 bool HasParams { get; }
26 string ParameterName (int pos);
27 string ParameterDesc (int pos);
28 Parameter.Modifier ParameterModifier (int pos);
29 string GetSignatureForError ();
32 public class ReflectionParameters : ParameterData {
35 bool last_arg_is_params = false;
36 bool is_varargs = false;
39 public ReflectionParameters (MethodBase mb)
43 ParameterInfo [] pi = mb.GetParameters ();
44 is_varargs = (mb.CallingConvention & CallingConventions.VarArgs) != 0;
47 int count = pi.Length-1;
50 types = TypeManager.NoTypes;
52 types = new Type [pi.Length];
53 for (int i = 0; i < pi.Length; i++)
54 types [i] = pi [i].ParameterType;
60 if (mb.Mono_IsInflatedMethod) {
61 MethodInfo generic = mb.GetGenericMethodDefinition ();
62 gpd = TypeManager.GetParameterData (generic);
64 last_arg_is_params = gpd.HasParams;
68 attrs = pi [count].GetCustomAttributes (TypeManager.param_array_type, true);
72 if (attrs.Length == 0)
75 last_arg_is_params = true;
78 public string GetSignatureForError ()
80 StringBuilder sb = new StringBuilder ("(");
81 for (int i = 0; i < pi.Length; ++i) {
84 sb.Append (ParameterDesc (i));
89 sb.Append ("__arglist");
92 return sb.ToString ();
95 public Type ParameterType (int pos)
97 if (last_arg_is_params && pos >= pi.Length - 1)
98 return pi [pi.Length - 1].ParameterType;
99 else if (is_varargs && pos >= pi.Length)
100 return TypeManager.runtime_argument_handle_type;
102 Type t = pi [pos].ParameterType;
108 public string ParameterName (int pos)
111 return gpd.ParameterName (pos);
113 if (last_arg_is_params && pos >= pi.Length - 1)
114 return pi [pi.Length - 1].Name;
115 else if (is_varargs && pos >= pi.Length)
118 return pi [pos].Name;
121 public string ParameterDesc (int pos)
123 if (is_varargs && pos >= pi.Length)
126 StringBuilder sb = new StringBuilder ();
131 Type partype = ParameterType (pos);
132 if (partype.IsByRef){
133 partype = TypeManager.GetElementType (partype);
140 if (pos >= pi.Length - 1 && last_arg_is_params)
141 sb.Append ("params ");
143 sb.Append (TypeManager.CSharpName (partype).Replace ("&", ""));
145 return sb.ToString ();
149 public Parameter.Modifier ParameterModifier (int pos)
151 if (last_arg_is_params && pos >= pi.Length - 1)
152 return Parameter.Modifier.PARAMS;
153 else if (is_varargs && pos >= pi.Length)
154 return Parameter.Modifier.ARGLIST;
157 return gpd.ParameterModifier (pos);
159 Type t = pi [pos].ParameterType;
161 if ((pi [pos].Attributes & (ParameterAttributes.Out|ParameterAttributes.In)) == ParameterAttributes.Out)
162 return Parameter.Modifier.OUT;
164 return Parameter.Modifier.REF;
167 return Parameter.Modifier.NONE;
172 return is_varargs ? pi.Length + 1 : pi.Length;
176 public bool HasParams {
178 return this.last_arg_is_params;
182 public Type[] Types {
189 public class ReflectionConstraints : GenericConstraints
191 GenericParameterAttributes attrs;
193 Type class_constraint;
194 Type[] iface_constraints;
197 public static GenericConstraints GetConstraints (Type t)
199 Type [] constraints = t.GetGenericParameterConstraints ();
200 GenericParameterAttributes attrs = t.GenericParameterAttributes;
201 if (constraints.Length == 0 && attrs == GenericParameterAttributes.None)
203 return new ReflectionConstraints (t.Name, constraints, attrs);
206 private ReflectionConstraints (string name, Type [] constraints, GenericParameterAttributes attrs)
211 if ((constraints.Length > 0) && !constraints [0].IsInterface) {
212 class_constraint = constraints [0];
213 iface_constraints = new Type [constraints.Length - 1];
214 Array.Copy (constraints, 1, iface_constraints, 0, constraints.Length - 1);
216 iface_constraints = constraints;
218 if (HasValueTypeConstraint)
219 base_type = TypeManager.value_type;
220 else if (class_constraint != null)
221 base_type = class_constraint;
223 base_type = TypeManager.object_type;
226 public override string TypeParameter {
230 public override GenericParameterAttributes Attributes {
231 get { return attrs; }
234 public override Type ClassConstraint {
235 get { return class_constraint; }
238 public override Type EffectiveBaseClass {
239 get { return base_type; }
242 public override Type[] InterfaceConstraints {
243 get { return iface_constraints; }
247 class PtrHashtable : Hashtable {
248 sealed class PtrComparer : IComparer {
249 private PtrComparer () {}
251 public static PtrComparer Instance = new PtrComparer ();
253 public int Compare (object x, object y)
262 public PtrHashtable ()
264 comparer = PtrComparer.Instance;
269 * Hashtable whose keys are character arrays with the same length
271 class CharArrayHashtable : Hashtable {
272 sealed class ArrComparer : IComparer {
275 public ArrComparer (int len) {
279 public int Compare (object x, object y)
281 char[] a = (char[])x;
282 char[] b = (char[])y;
284 for (int i = 0; i < len; ++i)
293 protected override int GetHash (Object key)
295 char[] arr = (char[])key;
298 for (int i = 0; i < len; ++i)
299 h = (h << 5) - h + arr [i];
304 public CharArrayHashtable (int len)
307 comparer = new ArrComparer (len);
313 public object Second;
315 public Pair (object f, object s)
323 /// This is a wrapper around StreamReader which is seekable backwards
324 /// within a window of around 2048 chars.
326 public class SeekableStreamReader
328 public SeekableStreamReader (StreamReader reader)
330 this.reader = reader;
331 this.buffer = new char [AverageReadLength * 3];
333 // Let the StreamWriter autodetect the encoder
337 public SeekableStreamReader (Stream stream, Encoding encoding)
338 : this (new StreamReader (stream, encoding, true))
343 private const int AverageReadLength = 1024;
346 int buffer_start; // in chars
347 int char_count; // count buffer[] valid characters
348 int pos; // index into buffer[]
351 /// This value corresponds to the current position in a stream of characters.
352 /// The StreamReader hides its manipulation of the underlying byte stream and all
353 /// character set/decoding issues. Thus, we cannot use this position to guess at
354 /// the corresponding position in the underlying byte stream even though there is
355 /// a correlation between them.
357 public int Position {
358 get { return buffer_start + pos; }
361 if (value < buffer_start || value > buffer_start + char_count)
362 throw new InternalErrorException ("can't seek that far back: " + (pos - value));
363 pos = value - buffer_start;
367 private bool ReadBuffer ()
369 int slack = buffer.Length - char_count;
370 if (slack <= AverageReadLength / 2) {
371 // shift the buffer to make room for AverageReadLength number of characters
372 int shift = AverageReadLength - slack;
373 Array.Copy (buffer, shift, buffer, 0, char_count - shift);
376 buffer_start += shift;
377 slack += shift; // slack == AverageReadLength
380 int chars_read = reader.Read (buffer, char_count, slack);
381 char_count += chars_read;
383 return pos < char_count;
388 if ((pos >= char_count) && !ReadBuffer ())
396 if ((pos >= char_count) && !ReadBuffer ())
399 return buffer [pos++];
403 public class DoubleHash {
404 const int DEFAULT_INITIAL_BUCKETS = 100;
406 public DoubleHash () : this (DEFAULT_INITIAL_BUCKETS) {}
408 public DoubleHash (int size)
411 buckets = new Entry [size];
425 public Entry (object key1, object key2, int hash, object value, Entry next)
435 public bool Lookup (object a, object b, out object res)
437 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
439 for (Entry e = buckets [h % count]; e != null; e = e.next) {
440 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b)) {
449 public void Insert (object a, object b, object value)
451 // Is it an existing one?
453 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
455 for (Entry e = buckets [h % count]; e != null; e = e.next) {
456 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b))
460 int bucket = h % count;
461 buckets [bucket] = new Entry (a, b, h, value, buckets [bucket]);
463 // Grow whenever we double in size
464 if (size++ == count) {
468 Entry [] newBuckets = new Entry [count];
469 foreach (Entry root in buckets) {
472 int newLoc = e.hash % count;
474 e.next = newBuckets [newLoc];
475 newBuckets [newLoc] = e;
480 buckets = newBuckets;