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)
98 if (last_arg_is_params && pos >= pi.Length - 1)
99 return pi [pi.Length - 1].Name;
100 else if (is_varargs && pos >= pi.Length)
103 return pi [pos].Name;
106 public string ParameterDesc (int pos)
108 if (is_varargs && pos >= pi.Length)
111 StringBuilder sb = new StringBuilder ();
116 Type partype = ParameterType (pos);
117 if (partype.IsByRef){
118 partype = TypeManager.GetElementType (partype);
125 if (pos >= pi.Length - 1 && last_arg_is_params)
126 sb.Append ("params ");
128 sb.Append (TypeManager.CSharpName (partype));
130 return sb.ToString ();
134 public Parameter.Modifier ParameterModifier (int pos)
138 if (last_arg_is_params && pos >= pi.Length - 1)
139 return Parameter.Modifier.PARAMS;
140 else if (is_varargs && pos >= pi.Length)
141 return Parameter.Modifier.ARGLIST;
144 return gpd.ParameterModifier (pos);
146 Type t = pi [pos].ParameterType;
148 if ((pi [pos].Attributes & ParameterAttributes.Out) != 0)
149 return Parameter.Modifier.ISBYREF | Parameter.Modifier.OUT;
151 return Parameter.Modifier.ISBYREF | Parameter.Modifier.REF;
154 return Parameter.Modifier.NONE;
159 return is_varargs ? pi.Length + 1 : pi.Length;
163 public bool HasParams {
165 return this.last_arg_is_params;
170 public class InternalParameters : ParameterData {
175 public readonly Parameters Parameters;
176 public readonly TypeParameter[] TypeParameters;
178 public InternalParameters (Type [] param_types, Parameters parameters)
180 this.param_types = param_types;
181 this.Parameters = parameters;
183 has_varargs = parameters.HasArglist;
185 if (param_types == null)
188 count = param_types.Length;
191 public InternalParameters (Type [] param_types, Parameters parameters,
192 TypeParameter [] type_params)
193 : this (param_types, parameters)
195 this.TypeParameters = type_params;
200 return has_varargs ? count + 1 : count;
204 public bool HasParams {
206 return Parameters.ArrayParameter != null;
210 Parameter GetParameter (int pos)
212 Parameter [] fixed_pars = Parameters.FixedParameters;
213 if (fixed_pars != null){
214 int len = fixed_pars.Length;
216 return Parameters.FixedParameters [pos];
219 return Parameters.ArrayParameter;
222 public Type ParameterType (int pos)
224 if (has_varargs && pos >= count)
225 return TypeManager.runtime_argument_handle_type;
227 if (param_types == null)
230 return GetParameter (pos).ExternalType ();
233 public GenericConstraints GenericConstraints (int pos)
235 if (TypeParameters == null)
238 return TypeParameters [pos].Constraints;
241 public string ParameterName (int pos)
243 if (has_varargs && pos >= count)
246 return GetParameter (pos).Name;
249 public string ParameterDesc (int pos)
251 if (has_varargs && pos >= count)
254 string tmp = String.Empty;
255 Parameter p = GetParameter (pos);
258 // We need to and for REF/OUT, because if either is set the
259 // extra flag ISBYREF will be set as well
261 if ((p.ModFlags & Parameter.Modifier.REF) != 0)
263 else if ((p.ModFlags & Parameter.Modifier.OUT) != 0)
265 else if (p.ModFlags == Parameter.Modifier.PARAMS)
268 Type t = ParameterType (pos);
270 return tmp + TypeManager.CSharpName (t);
273 public Parameter.Modifier ParameterModifier (int pos)
275 if (has_varargs && pos >= count)
276 return Parameter.Modifier.ARGLIST;
278 Parameter.Modifier mod = GetParameter (pos).ModFlags;
280 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
281 mod |= Parameter.Modifier.ISBYREF;
288 public class ReflectionConstraints : GenericConstraints
290 GenericParameterAttributes attrs;
292 Type class_constraint;
293 Type[] iface_constraints;
295 public ReflectionConstraints (Type t)
297 Type[] constraints = t.GetGenericParameterConstraints ();
298 if ((constraints.Length > 0) && !constraints [0].IsInterface) {
299 class_constraint = constraints [0];
300 iface_constraints = new Type [constraints.Length - 1];
301 Array.Copy (constraints, 1, iface_constraints, 0, constraints.Length - 1);
303 iface_constraints = constraints;
304 attrs = t.GenericParameterAttributes;
306 if (HasValueTypeConstraint)
307 base_type = TypeManager.value_type;
308 else if (class_constraint != null)
309 base_type = class_constraint;
311 base_type = TypeManager.object_type;
314 public override GenericParameterAttributes Attributes {
315 get { return attrs; }
318 public override Type ClassConstraint {
319 get { return class_constraint; }
322 public override Type EffectiveBaseClass {
323 get { return base_type; }
326 public override Type[] InterfaceConstraints {
327 get { return iface_constraints; }
331 class PtrHashtable : Hashtable {
332 sealed class PtrComparer : IComparer {
333 private PtrComparer () {}
335 public static PtrComparer Instance = new PtrComparer ();
337 public int Compare (object x, object y)
346 public PtrHashtable ()
348 comparer = PtrComparer.Instance;
353 * Hashtable whose keys are character arrays with the same length
355 class CharArrayHashtable : Hashtable {
356 sealed class ArrComparer : IComparer {
359 public ArrComparer (int len) {
363 public int Compare (object x, object y)
365 char[] a = (char[])x;
366 char[] b = (char[])y;
368 for (int i = 0; i < len; ++i)
377 protected override int GetHash (Object key)
379 char[] arr = (char[])key;
382 for (int i = 0; i < len; ++i)
383 h = (h << 5) - h + arr [i];
388 public CharArrayHashtable (int len)
391 comparer = new ArrComparer (len);
397 public object Second;
399 public Pair (object f, object s)
407 /// This is a wrapper around StreamReader which is seekable.
409 public class SeekableStreamReader
411 public SeekableStreamReader (StreamReader reader)
413 this.reader = reader;
414 this.buffer = new char [DefaultCacheSize];
416 // Compute the preamble size
418 // Let the StreamWriter autodetect the encoder
421 reader.BaseStream.Position = 0;
422 Encoding enc = reader.CurrentEncoding;
423 // First of all, get at least a char
425 byte[] auxb = new byte [50];
430 br = reader.BaseStream.Read (auxb, num_bytes, auxb.Length - num_bytes);
432 num_chars = enc.GetCharCount (auxb, 0, num_bytes);
434 while (num_chars == 0 && br > 0);
438 // Now, check which bytes at the beginning have no effect in the
442 while (enc.GetCharCount (auxb, p, num_bytes-p) >= num_chars)
445 preamble_size = p - 1;
446 reader.BaseStream.Position = 0;
447 reader.DiscardBufferedData ();
449 buffer_start = preamble_size;
453 public SeekableStreamReader (Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks)
454 : this (new StreamReader (stream, encoding, detect_encoding_from_bytemarks))
459 private const int DefaultCacheSize = 1024;
462 int buffer_start; // in bytes
463 int buffer_size; // in bytes
464 int char_count; // count buffer[] valid characters
465 int pos; // index into buffer[]
469 /// The difference to the StreamReader's BaseStream.Position is that this one is reliable; ie. it
470 // always reports the correct position and if it's modified, it also takes care of the buffered data.
472 public int Position {
474 return buffer_start + reader.CurrentEncoding.GetByteCount (buffer, 0, pos);
478 // This one is easy: we're modifying the position within our current
480 if ((value >= buffer_start) && (value < buffer_start + buffer_size)) {
481 int byte_offset = value - buffer_start;
483 // encoded characters can take more than 1 byte length
484 while (reader.CurrentEncoding.GetByteCount (buffer, 0, pos) > byte_offset)
490 if (value == 0) // Skip preamble
491 value = preamble_size;
493 // Ok, now we need to seek.
494 reader.DiscardBufferedData ();
495 reader.BaseStream.Position = buffer_start = value;
496 char_count = buffer_size = pos = 0;
500 private bool ReadBuffer ()
503 buffer_start += buffer_size;
504 char_count = reader.Read (buffer, 0, buffer.Length);
505 buffer_size = reader.CurrentEncoding.GetByteCount (buffer, 0, char_count);
506 return buffer_size > 0;
511 if ((pos >= char_count) && !ReadBuffer ())
519 if ((pos >= char_count) && !ReadBuffer ())
522 return buffer [pos++];
526 public class DoubleHash {
527 const int DEFAULT_INITIAL_BUCKETS = 100;
529 public DoubleHash () : this (DEFAULT_INITIAL_BUCKETS) {}
531 public DoubleHash (int size)
534 buckets = new Entry [size];
548 public Entry (object key1, object key2, int hash, object value, Entry next)
558 public bool Lookup (object a, object b, out object res)
560 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
562 for (Entry e = buckets [h % count]; e != null; e = e.next) {
563 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b)) {
572 public void Insert (object a, object b, object value)
574 // Is it an existing one?
576 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
578 for (Entry e = buckets [h % count]; e != null; e = e.next) {
579 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b))
583 int bucket = h % count;
584 buckets [bucket] = new Entry (a, b, h, value, buckets [bucket]);
586 // Grow whenever we double in size
587 if (size++ == count) {
591 Entry [] newBuckets = new Entry [count];
592 foreach (Entry root in buckets) {
595 int newLoc = e.hash % count;
597 e.next = newBuckets [newLoc];
598 newBuckets [newLoc] = e;
603 buckets = newBuckets;