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 = Type.EmptyTypes;
52 types = new Type [pi.Length];
53 for (int i = 0; i < pi.Length; i++)
54 types [i] = pi [i].ParameterType;
60 MethodBase generic = TypeManager.DropGenericMethodArguments (mb);
62 gpd = TypeManager.GetParameterData (generic);
63 last_arg_is_params = gpd.HasParams;
67 attrs = pi [count].GetCustomAttributes (TypeManager.param_array_type, true);
71 if (attrs.Length == 0)
74 last_arg_is_params = true;
77 public string GetSignatureForError ()
79 StringBuilder sb = new StringBuilder ("(");
80 for (int i = 0; i < pi.Length; ++i) {
83 sb.Append (ParameterDesc (i));
88 sb.Append ("__arglist");
91 return sb.ToString ();
94 public Type ParameterType (int pos)
96 if (last_arg_is_params && pos >= pi.Length - 1)
97 return pi [pi.Length - 1].ParameterType;
98 else if (is_varargs && pos >= pi.Length)
99 return TypeManager.runtime_argument_handle_type;
101 Type t = pi [pos].ParameterType;
107 public string ParameterName (int pos)
110 return gpd.ParameterName (pos);
112 if (last_arg_is_params && pos >= pi.Length - 1)
113 return pi [pi.Length - 1].Name;
114 else if (is_varargs && pos >= pi.Length)
117 return pi [pos].Name;
120 public string ParameterDesc (int pos)
122 if (is_varargs && pos >= pi.Length)
125 StringBuilder sb = new StringBuilder ();
130 Type partype = ParameterType (pos);
131 if (partype.IsByRef){
132 partype = TypeManager.GetElementType (partype);
139 if (pos >= pi.Length - 1 && last_arg_is_params)
140 sb.Append ("params ");
142 sb.Append (TypeManager.CSharpName (partype).Replace ("&", ""));
144 return sb.ToString ();
148 public Parameter.Modifier ParameterModifier (int pos)
150 if (last_arg_is_params && pos >= pi.Length - 1)
151 return Parameter.Modifier.PARAMS;
152 else if (is_varargs && pos >= pi.Length)
153 return Parameter.Modifier.ARGLIST;
156 return gpd.ParameterModifier (pos);
158 Type t = pi [pos].ParameterType;
160 if ((pi [pos].Attributes & (ParameterAttributes.Out|ParameterAttributes.In)) == ParameterAttributes.Out)
161 return Parameter.Modifier.OUT;
163 return Parameter.Modifier.REF;
166 return Parameter.Modifier.NONE;
171 return is_varargs ? pi.Length + 1 : pi.Length;
175 public bool HasParams {
177 return this.last_arg_is_params;
181 public Type[] Types {
188 public class ReflectionConstraints : GenericConstraints
190 GenericParameterAttributes attrs;
192 Type class_constraint;
193 Type[] iface_constraints;
196 public static GenericConstraints GetConstraints (Type t)
198 Type [] constraints = t.GetGenericParameterConstraints ();
199 GenericParameterAttributes attrs = t.GenericParameterAttributes;
200 if (constraints.Length == 0 && attrs == GenericParameterAttributes.None)
202 return new ReflectionConstraints (t.Name, constraints, attrs);
205 private ReflectionConstraints (string name, Type [] constraints, GenericParameterAttributes attrs)
210 if ((constraints.Length > 0) && !constraints [0].IsInterface) {
211 class_constraint = constraints [0];
212 iface_constraints = new Type [constraints.Length - 1];
213 Array.Copy (constraints, 1, iface_constraints, 0, constraints.Length - 1);
215 iface_constraints = constraints;
217 if (HasValueTypeConstraint)
218 base_type = TypeManager.value_type;
219 else if (class_constraint != null)
220 base_type = class_constraint;
222 base_type = TypeManager.object_type;
225 public override string TypeParameter {
229 public override GenericParameterAttributes Attributes {
230 get { return attrs; }
233 public override Type ClassConstraint {
234 get { return class_constraint; }
237 public override Type EffectiveBaseClass {
238 get { return base_type; }
241 public override Type[] InterfaceConstraints {
242 get { return iface_constraints; }
246 class PtrHashtable : Hashtable {
247 sealed class PtrComparer : IComparer {
248 private PtrComparer () {}
250 public static PtrComparer Instance = new PtrComparer ();
252 public int Compare (object x, object y)
261 public PtrHashtable ()
263 comparer = PtrComparer.Instance;
268 * Hashtable whose keys are character arrays with the same length
270 class CharArrayHashtable : Hashtable {
271 sealed class ArrComparer : IComparer {
274 public ArrComparer (int len) {
278 public int Compare (object x, object y)
280 char[] a = (char[])x;
281 char[] b = (char[])y;
283 for (int i = 0; i < len; ++i)
292 protected override int GetHash (Object key)
294 char[] arr = (char[])key;
297 for (int i = 0; i < len; ++i)
298 h = (h << 5) - h + arr [i];
303 public CharArrayHashtable (int len)
306 comparer = new ArrComparer (len);
312 public object Second;
314 public Pair (object f, object s)
322 /// This is a wrapper around StreamReader which is seekable backwards
323 /// within a window of around 2048 chars.
325 public class SeekableStreamReader
327 public SeekableStreamReader (StreamReader reader)
329 this.reader = reader;
330 this.buffer = new char [AverageReadLength * 3];
332 // Let the StreamWriter autodetect the encoder
336 public SeekableStreamReader (Stream stream, Encoding encoding)
337 : this (new StreamReader (stream, encoding, true))
342 private const int AverageReadLength = 1024;
345 int buffer_start; // in chars
346 int char_count; // count buffer[] valid characters
347 int pos; // index into buffer[]
350 /// This value corresponds to the current position in a stream of characters.
351 /// The StreamReader hides its manipulation of the underlying byte stream and all
352 /// character set/decoding issues. Thus, we cannot use this position to guess at
353 /// the corresponding position in the underlying byte stream even though there is
354 /// a correlation between them.
356 public int Position {
357 get { return buffer_start + pos; }
360 if (value < buffer_start || value > buffer_start + char_count)
361 throw new InternalErrorException ("can't seek that far back: " + (pos - value));
362 pos = value - buffer_start;
366 private bool ReadBuffer ()
368 int slack = buffer.Length - char_count;
369 if (slack <= AverageReadLength / 2) {
370 // shift the buffer to make room for AverageReadLength number of characters
371 int shift = AverageReadLength - slack;
372 Array.Copy (buffer, shift, buffer, 0, char_count - shift);
375 buffer_start += shift;
376 slack += shift; // slack == AverageReadLength
379 int chars_read = reader.Read (buffer, char_count, slack);
380 char_count += chars_read;
382 return pos < char_count;
387 if ((pos >= char_count) && !ReadBuffer ())
395 if ((pos >= char_count) && !ReadBuffer ())
398 return buffer [pos++];
402 public class DoubleHash {
403 const int DEFAULT_INITIAL_BUCKETS = 100;
405 public DoubleHash () : this (DEFAULT_INITIAL_BUCKETS) {}
407 public DoubleHash (int size)
410 buckets = new Entry [size];
424 public Entry (object key1, object key2, int hash, object value, Entry next)
434 public bool Lookup (object a, object b, out object res)
436 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
438 for (Entry e = buckets [h % count]; e != null; e = e.next) {
439 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b)) {
448 public void Insert (object a, object b, object value)
450 // Is it an existing one?
452 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
454 for (Entry e = buckets [h % count]; e != null; e = e.next) {
455 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b))
459 int bucket = h % count;
460 buckets [bucket] = new Entry (a, b, h, value, buckets [bucket]);
462 // Grow whenever we double in size
463 if (size++ == count) {
467 Entry [] newBuckets = new Entry [count];
468 foreach (Entry root in buckets) {
471 int newLoc = e.hash % count;
473 e.next = newBuckets [newLoc];
474 newBuckets [newLoc] = e;
479 buckets = newBuckets;