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)
139 if (last_arg_is_params && pos >= pi.Length - 1)
140 return Parameter.Modifier.PARAMS;
141 else if (is_varargs && pos >= pi.Length)
142 return Parameter.Modifier.ARGLIST;
145 return gpd.ParameterModifier (pos);
147 Type t = pi [pos].ParameterType;
149 if ((pi [pos].Attributes & ParameterAttributes.Out) != 0)
150 return Parameter.Modifier.ISBYREF | Parameter.Modifier.OUT;
152 return Parameter.Modifier.ISBYREF | Parameter.Modifier.REF;
155 return Parameter.Modifier.NONE;
160 return is_varargs ? pi.Length + 1 : pi.Length;
164 public bool HasParams {
166 return this.last_arg_is_params;
171 public class InternalParameters : ParameterData {
176 public readonly Parameters Parameters;
177 public readonly TypeParameter[] TypeParameters;
179 public InternalParameters (Type [] param_types, Parameters parameters)
181 this.param_types = param_types;
182 this.Parameters = parameters;
184 has_varargs = parameters.HasArglist;
186 if (param_types == null)
189 count = param_types.Length;
192 public InternalParameters (Type [] param_types, Parameters parameters,
193 TypeParameter [] type_params)
194 : this (param_types, parameters)
196 this.TypeParameters = type_params;
201 return has_varargs ? count + 1 : count;
205 public bool HasParams {
207 return Parameters.ArrayParameter != null;
211 Parameter GetParameter (int pos)
213 Parameter [] fixed_pars = Parameters.FixedParameters;
214 if (fixed_pars != null){
215 int len = fixed_pars.Length;
217 return Parameters.FixedParameters [pos];
220 return Parameters.ArrayParameter;
223 public Type ParameterType (int pos)
225 if (has_varargs && pos >= count)
226 return TypeManager.runtime_argument_handle_type;
228 if (param_types == null)
231 return GetParameter (pos).ExternalType ();
234 public GenericConstraints GenericConstraints (int pos)
236 if (TypeParameters == null)
239 return TypeParameters [pos].Constraints;
242 public string ParameterName (int pos)
244 if (has_varargs && pos >= count)
247 return GetParameter (pos).Name;
250 public string ParameterDesc (int pos)
252 if (has_varargs && pos >= count)
255 string tmp = String.Empty;
256 Parameter p = GetParameter (pos);
259 // We need to and for REF/OUT, because if either is set the
260 // extra flag ISBYREF will be set as well
262 if ((p.ModFlags & Parameter.Modifier.REF) != 0)
264 else if ((p.ModFlags & Parameter.Modifier.OUT) != 0)
266 else if (p.ModFlags == Parameter.Modifier.PARAMS)
269 Type t = ParameterType (pos);
271 return tmp + TypeManager.CSharpName (t);
274 public Parameter.Modifier ParameterModifier (int pos)
276 if (has_varargs && pos >= count)
277 return Parameter.Modifier.ARGLIST;
279 Parameter.Modifier mod = GetParameter (pos).ModFlags;
281 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
282 mod |= Parameter.Modifier.ISBYREF;
289 public class ReflectionConstraints : GenericConstraints
291 GenericParameterAttributes attrs;
293 Type class_constraint;
294 Type[] iface_constraints;
296 public ReflectionConstraints (Type t)
298 Type[] constraints = t.GetGenericParameterConstraints ();
299 if ((constraints.Length > 0) && !constraints [0].IsInterface) {
300 class_constraint = constraints [0];
301 iface_constraints = new Type [constraints.Length - 1];
302 Array.Copy (constraints, 1, iface_constraints, 0, constraints.Length - 1);
304 iface_constraints = constraints;
305 attrs = t.GenericParameterAttributes;
307 if (HasValueTypeConstraint)
308 base_type = TypeManager.value_type;
309 else if (class_constraint != null)
310 base_type = class_constraint;
312 base_type = TypeManager.object_type;
315 public override GenericParameterAttributes Attributes {
316 get { return attrs; }
319 public override Type ClassConstraint {
320 get { return class_constraint; }
323 public override Type EffectiveBaseClass {
324 get { return base_type; }
327 public override Type[] InterfaceConstraints {
328 get { return iface_constraints; }
332 class PtrHashtable : Hashtable {
333 sealed class PtrComparer : IComparer {
334 private PtrComparer () {}
336 public static PtrComparer Instance = new PtrComparer ();
338 public int Compare (object x, object y)
347 public PtrHashtable ()
349 comparer = PtrComparer.Instance;
354 * Hashtable whose keys are character arrays with the same length
356 class CharArrayHashtable : Hashtable {
357 sealed class ArrComparer : IComparer {
360 public ArrComparer (int len) {
364 public int Compare (object x, object y)
366 char[] a = (char[])x;
367 char[] b = (char[])y;
369 for (int i = 0; i < len; ++i)
378 protected override int GetHash (Object key)
380 char[] arr = (char[])key;
383 for (int i = 0; i < len; ++i)
384 h = (h << 5) - h + arr [i];
389 public CharArrayHashtable (int len)
392 comparer = new ArrComparer (len);
398 public object Second;
400 public Pair (object f, object s)
408 /// This is a wrapper around StreamReader which is seekable.
410 public class SeekableStreamReader
412 public SeekableStreamReader (StreamReader reader)
414 this.reader = reader;
415 this.buffer = new char [DefaultCacheSize];
417 // Compute the preamble size
419 // Let the StreamWriter autodetect the encoder
422 reader.BaseStream.Position = 0;
423 Encoding enc = reader.CurrentEncoding;
424 // First of all, get at least a char
426 byte[] auxb = new byte [50];
431 br = reader.BaseStream.Read (auxb, num_bytes, auxb.Length - num_bytes);
433 num_chars = enc.GetCharCount (auxb, 0, num_bytes);
435 while (num_chars == 0 && br > 0);
439 // Now, check which bytes at the beginning have no effect in the
443 while (enc.GetCharCount (auxb, p, num_bytes-p) >= num_chars)
446 preamble_size = p - 1;
447 reader.BaseStream.Position = 0;
448 reader.DiscardBufferedData ();
450 buffer_start = preamble_size;
454 public SeekableStreamReader (Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks)
455 : this (new StreamReader (stream, encoding, detect_encoding_from_bytemarks))
460 private const int DefaultCacheSize = 1024;
463 int buffer_start; // in bytes
464 int buffer_size; // in bytes
465 int char_count; // count buffer[] valid characters
466 int pos; // index into buffer[]
470 /// The difference to the StreamReader's BaseStream.Position is that this one is reliable; ie. it
471 // always reports the correct position and if it's modified, it also takes care of the buffered data.
473 public int Position {
475 return buffer_start + reader.CurrentEncoding.GetByteCount (buffer, 0, pos);
479 // This one is easy: we're modifying the position within our current
481 if ((value >= buffer_start) && (value < buffer_start + buffer_size)) {
482 int byte_offset = value - buffer_start;
484 // pos is an index into a char
485 // buffer so it might be
486 // greater than the buffer
487 // length now, if the buffer
488 // contains multibyte chars
491 // encoded characters can take
492 // more than 1 byte length.
493 while ((pos > buffer.Length) ||
494 reader.CurrentEncoding.GetByteCount (buffer, 0, pos) > byte_offset) {
501 if (value == 0) // Skip preamble
502 value = preamble_size;
504 // Ok, now we need to seek.
505 reader.DiscardBufferedData ();
506 reader.BaseStream.Position = buffer_start = value;
507 char_count = buffer_size = pos = 0;
511 private bool ReadBuffer ()
514 buffer_start += buffer_size;
515 char_count = reader.Read (buffer, 0, buffer.Length);
516 buffer_size = reader.CurrentEncoding.GetByteCount (buffer, 0, char_count);
517 return buffer_size > 0;
522 if ((pos >= char_count) && !ReadBuffer ())
530 if ((pos >= char_count) && !ReadBuffer ())
533 return buffer [pos++];
537 public class DoubleHash {
538 const int DEFAULT_INITIAL_BUCKETS = 100;
540 public DoubleHash () : this (DEFAULT_INITIAL_BUCKETS) {}
542 public DoubleHash (int size)
545 buckets = new Entry [size];
559 public Entry (object key1, object key2, int hash, object value, Entry next)
569 public bool Lookup (object a, object b, out object res)
571 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
573 for (Entry e = buckets [h % count]; e != null; e = e.next) {
574 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b)) {
583 public void Insert (object a, object b, object value)
585 // Is it an existing one?
587 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
589 for (Entry e = buckets [h % count]; e != null; e = e.next) {
590 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b))
594 int bucket = h % count;
595 buckets [bucket] = new Entry (a, b, h, value, buckets [bucket]);
597 // Grow whenever we double in size
598 if (size++ == count) {
602 Entry [] newBuckets = new Entry [count];
603 foreach (Entry root in buckets) {
606 int newLoc = e.hash % count;
608 e.next = newBuckets [newLoc];
609 newBuckets [newLoc] = e;
614 buckets = newBuckets;