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 GenericConstraints GenericConstraints (int pos);
25 bool HasParams { get; }
26 string ParameterName (int pos);
27 string ParameterDesc (int pos);
28 Parameter.Modifier ParameterModifier (int pos);
31 public class ReflectionParameters : ParameterData {
34 bool last_arg_is_params = false;
35 bool is_varargs = false;
38 public ReflectionParameters (MethodBase mb)
42 ParameterInfo [] pi = mb.GetParameters ();
43 is_varargs = (mb.CallingConvention & CallingConventions.VarArgs) != 0;
46 int count = pi.Length-1;
51 if (mb.Mono_IsInflatedMethod) {
52 MethodInfo generic = mb.GetGenericMethodDefinition ();
53 gpd = Invocation.GetParameterData (generic);
55 last_arg_is_params = gpd.HasParams;
59 if (mb.IsGenericMethodDefinition)
60 type_params = mb.GetGenericArguments ();
62 attrs = pi [count].GetCustomAttributes (TypeManager.param_array_type, true);
66 if (attrs.Length == 0)
69 last_arg_is_params = true;
72 public Type ParameterType (int pos)
74 if (last_arg_is_params && pos >= pi.Length - 1)
75 return pi [pi.Length - 1].ParameterType;
76 else if (is_varargs && pos >= pi.Length)
77 return TypeManager.runtime_argument_handle_type;
79 Type t = pi [pos].ParameterType;
85 public GenericConstraints GenericConstraints (int pos)
88 return gpd.GenericConstraints (pos);
90 if (type_params == null)
93 return new ReflectionConstraints (type_params [pos]);
96 public string ParameterName (int pos)
99 return gpd.ParameterName (pos);
101 if (last_arg_is_params && pos >= pi.Length - 1)
102 return pi [pi.Length - 1].Name;
103 else if (is_varargs && pos >= pi.Length)
106 return pi [pos].Name;
109 public string ParameterDesc (int pos)
111 if (is_varargs && pos >= pi.Length)
114 StringBuilder sb = new StringBuilder ();
119 Type partype = ParameterType (pos);
120 if (partype.IsByRef){
121 partype = TypeManager.GetElementType (partype);
128 if (pos >= pi.Length - 1 && last_arg_is_params)
129 sb.Append ("params ");
131 sb.Append (TypeManager.CSharpName (partype));
133 return sb.ToString ();
137 public Parameter.Modifier ParameterModifier (int pos)
141 if (last_arg_is_params && pos >= pi.Length - 1)
142 return Parameter.Modifier.PARAMS;
143 else if (is_varargs && pos >= pi.Length)
144 return Parameter.Modifier.ARGLIST;
147 return gpd.ParameterModifier (pos);
149 Type t = pi [pos].ParameterType;
151 if ((pi [pos].Attributes & ParameterAttributes.Out) != 0)
152 return Parameter.Modifier.ISBYREF | Parameter.Modifier.OUT;
154 return Parameter.Modifier.ISBYREF | Parameter.Modifier.REF;
157 return Parameter.Modifier.NONE;
162 return is_varargs ? pi.Length + 1 : pi.Length;
166 public bool HasParams {
168 return this.last_arg_is_params;
173 public class InternalParameters : ParameterData {
178 public readonly Parameters Parameters;
179 public readonly TypeParameter[] TypeParameters;
181 public InternalParameters (Type [] param_types, Parameters parameters)
183 this.param_types = param_types;
184 this.Parameters = parameters;
186 has_varargs = parameters.HasArglist;
188 if (param_types == null)
191 count = param_types.Length;
194 public InternalParameters (Type [] param_types, Parameters parameters,
195 TypeParameter [] type_params)
196 : this (param_types, parameters)
198 this.TypeParameters = type_params;
203 return has_varargs ? count + 1 : count;
207 public bool HasParams {
209 return Parameters.ArrayParameter != null;
213 Parameter GetParameter (int pos)
215 Parameter [] fixed_pars = Parameters.FixedParameters;
216 if (fixed_pars != null){
217 int len = fixed_pars.Length;
219 return Parameters.FixedParameters [pos];
222 return Parameters.ArrayParameter;
225 public Type ParameterType (int pos)
227 if (has_varargs && pos >= count)
228 return TypeManager.runtime_argument_handle_type;
230 if (param_types == null)
233 return GetParameter (pos).ExternalType ();
236 public GenericConstraints GenericConstraints (int pos)
238 if (TypeParameters == null)
241 return TypeParameters [pos].Constraints;
244 public string ParameterName (int pos)
246 if (has_varargs && pos >= count)
249 return GetParameter (pos).Name;
252 public string ParameterDesc (int pos)
254 if (has_varargs && pos >= count)
257 string tmp = String.Empty;
258 Parameter p = GetParameter (pos);
261 // We need to and for REF/OUT, because if either is set the
262 // extra flag ISBYREF will be set as well
264 if ((p.ModFlags & Parameter.Modifier.REF) != 0)
266 else if ((p.ModFlags & Parameter.Modifier.OUT) != 0)
268 else if (p.ModFlags == Parameter.Modifier.PARAMS)
271 Type t = ParameterType (pos);
273 return tmp + TypeManager.CSharpName (t);
276 public Parameter.Modifier ParameterModifier (int pos)
278 if (has_varargs && pos >= count)
279 return Parameter.Modifier.ARGLIST;
281 Parameter.Modifier mod = GetParameter (pos).ModFlags;
283 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
284 mod |= Parameter.Modifier.ISBYREF;
291 public class ReflectionConstraints : GenericConstraints
293 GenericParameterAttributes attrs;
295 Type class_constraint;
296 Type[] iface_constraints;
298 public ReflectionConstraints (Type t)
300 Type[] constraints = t.GetGenericParameterConstraints ();
301 if ((constraints.Length > 0) && !constraints [0].IsInterface) {
302 class_constraint = constraints [0];
303 iface_constraints = new Type [constraints.Length - 1];
304 Array.Copy (constraints, 1, iface_constraints, 0, constraints.Length - 1);
306 iface_constraints = constraints;
307 attrs = t.GenericParameterAttributes;
309 if (HasValueTypeConstraint)
310 base_type = TypeManager.value_type;
311 else if (class_constraint != null)
312 base_type = class_constraint;
314 base_type = TypeManager.object_type;
317 public override GenericParameterAttributes Attributes {
318 get { return attrs; }
321 public override Type ClassConstraint {
322 get { return class_constraint; }
325 public override Type EffectiveBaseClass {
326 get { return base_type; }
329 public override Type[] InterfaceConstraints {
330 get { return iface_constraints; }
334 class PtrHashtable : Hashtable {
335 sealed class PtrComparer : IComparer {
336 private PtrComparer () {}
338 public static PtrComparer Instance = new PtrComparer ();
340 public int Compare (object x, object y)
349 public PtrHashtable ()
351 comparer = PtrComparer.Instance;
356 * Hashtable whose keys are character arrays with the same length
358 class CharArrayHashtable : Hashtable {
359 sealed class ArrComparer : IComparer {
362 public ArrComparer (int len) {
366 public int Compare (object x, object y)
368 char[] a = (char[])x;
369 char[] b = (char[])y;
371 for (int i = 0; i < len; ++i)
380 protected override int GetHash (Object key)
382 char[] arr = (char[])key;
385 for (int i = 0; i < len; ++i)
386 h = (h << 5) - h + arr [i];
391 public CharArrayHashtable (int len)
394 comparer = new ArrComparer (len);
400 public object Second;
402 public Pair (object f, object s)
410 /// This is a wrapper around StreamReader which is seekable.
412 public class SeekableStreamReader
414 public SeekableStreamReader (StreamReader reader)
416 this.reader = reader;
417 this.buffer = new char [DefaultCacheSize];
419 // Compute the preamble size
421 // Let the StreamWriter autodetect the encoder
424 reader.BaseStream.Position = 0;
425 Encoding enc = reader.CurrentEncoding;
426 // First of all, get at least a char
428 byte[] auxb = new byte [50];
433 br = reader.BaseStream.Read (auxb, num_bytes, auxb.Length - num_bytes);
435 num_chars = enc.GetCharCount (auxb, 0, num_bytes);
437 while (num_chars == 0 && br > 0);
441 // Now, check which bytes at the beginning have no effect in the
445 while (enc.GetCharCount (auxb, p, num_bytes-p) >= num_chars)
448 preamble_size = p - 1;
449 reader.BaseStream.Position = 0;
450 reader.DiscardBufferedData ();
452 buffer_start = preamble_size;
456 public SeekableStreamReader (Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks)
457 : this (new StreamReader (stream, encoding, detect_encoding_from_bytemarks))
462 private const int DefaultCacheSize = 1024;
465 int buffer_start; // in bytes
466 int buffer_size; // in bytes
467 int char_count; // count buffer[] valid characters
468 int pos; // index into buffer[]
472 /// The difference to the StreamReader's BaseStream.Position is that this one is reliable; ie. it
473 // always reports the correct position and if it's modified, it also takes care of the buffered data.
475 public int Position {
477 return buffer_start + reader.CurrentEncoding.GetByteCount (buffer, 0, pos);
481 // This one is easy: we're modifying the position within our current
483 if ((value >= buffer_start) && (value < buffer_start + buffer_size)) {
484 int byte_offset = value - buffer_start;
486 // encoded characters can take more than 1 byte length
487 while (reader.CurrentEncoding.GetByteCount (buffer, 0, pos) > byte_offset)
493 if (value == 0) // Skip preamble
494 value = preamble_size;
496 // Ok, now we need to seek.
497 reader.DiscardBufferedData ();
498 reader.BaseStream.Position = buffer_start = value;
499 char_count = buffer_size = pos = 0;
503 private bool ReadBuffer ()
506 buffer_start += buffer_size;
507 char_count = reader.Read (buffer, 0, buffer.Length);
508 buffer_size = reader.CurrentEncoding.GetByteCount (buffer, 0, char_count);
509 return buffer_size > 0;
514 if ((pos >= char_count) && !ReadBuffer ())
522 if ((pos >= char_count) && !ReadBuffer ())
525 return buffer [pos++];
529 public class DoubleHash {
530 const int DEFAULT_INITIAL_BUCKETS = 100;
532 public DoubleHash () : this (DEFAULT_INITIAL_BUCKETS) {}
534 public DoubleHash (int size)
537 buckets = new Entry [size];
551 public Entry (object key1, object key2, int hash, object value, Entry next)
561 public bool Lookup (object a, object b, out object res)
563 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
565 for (Entry e = buckets [h % count]; e != null; e = e.next) {
566 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b)) {
575 public void Insert (object a, object b, object value)
577 // Is it an existing one?
579 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
581 for (Entry e = buckets [h % count]; e != null; e = e.next) {
582 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b))
586 int bucket = h % count;
587 buckets [bucket] = new Entry (a, b, h, value, buckets [bucket]);
589 // Grow whenever we double in size
590 if (size++ == count) {
594 Entry [] newBuckets = new Entry [count];
595 foreach (Entry root in buckets) {
598 int newLoc = e.hash % count;
600 e.next = newBuckets [newLoc];
601 newBuckets [newLoc] = e;
606 buckets = newBuckets;