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);
24 bool HasArrayParameter { 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.HasArrayParameter;
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 bool HasArrayParameter {
73 get { return last_arg_is_params; }
76 public Type ParameterType (int pos)
78 if (last_arg_is_params && pos >= pi.Length - 1)
79 return pi [pi.Length - 1].ParameterType;
80 else if (is_varargs && pos >= pi.Length)
81 return TypeManager.runtime_argument_handle_type;
83 Type t = pi [pos].ParameterType;
89 public GenericConstraints GenericConstraints (int pos)
92 return gpd.GenericConstraints (pos);
94 if (type_params == null)
97 return new ReflectionConstraints (type_params [pos]);
100 public string ParameterName (int pos)
102 if (last_arg_is_params && pos >= pi.Length - 1)
103 return pi [pi.Length - 1].Name;
104 else if (is_varargs && pos >= pi.Length)
107 return pi [pos].Name;
110 public string ParameterDesc (int pos)
112 if (is_varargs && pos >= pi.Length)
115 StringBuilder sb = new StringBuilder ();
120 Type partype = ParameterType (pos);
121 if (partype.IsByRef){
122 partype = TypeManager.GetElementType (partype);
129 if (pos >= pi.Length - 1 && last_arg_is_params)
130 sb.Append ("params ");
132 sb.Append (TypeManager.CSharpName (partype));
134 return sb.ToString ();
138 public Parameter.Modifier ParameterModifier (int pos)
142 if (last_arg_is_params && pos >= pi.Length - 1)
143 return Parameter.Modifier.PARAMS;
144 else if (is_varargs && pos >= pi.Length)
145 return Parameter.Modifier.ARGLIST;
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;
165 public class InternalParameters : ParameterData {
170 public readonly Parameters Parameters;
171 public readonly TypeParameter[] TypeParameters;
173 public InternalParameters (Type [] param_types, Parameters parameters)
175 this.param_types = param_types;
176 this.Parameters = parameters;
179 public InternalParameters (DeclSpace ds, Parameters parameters)
180 : this (parameters.GetParameterInfo (ds), parameters)
182 has_varargs = parameters.HasArglist;
184 if (param_types == null)
187 count = param_types.Length;
190 public InternalParameters (DeclSpace ds, Parameters parameters,
191 TypeParameter [] type_params)
192 : this (ds, parameters)
194 this.TypeParameters = type_params;
199 return has_varargs ? count + 1 : count;
203 public bool HasArrayParameter {
204 get { return Parameters.ArrayParameter != null; }
207 Parameter GetParameter (int pos)
209 Parameter [] fixed_pars = Parameters.FixedParameters;
210 if (fixed_pars != null){
211 int len = fixed_pars.Length;
213 return Parameters.FixedParameters [pos];
216 return Parameters.ArrayParameter;
219 public Type ParameterType (int pos)
221 if (has_varargs && pos >= count)
222 return TypeManager.runtime_argument_handle_type;
224 if (param_types == null)
227 return GetParameter (pos).ExternalType ();
230 public GenericConstraints GenericConstraints (int pos)
232 if (TypeParameters == null)
235 return TypeParameters [pos].Constraints;
238 public string ParameterName (int pos)
240 if (has_varargs && pos >= count)
243 return GetParameter (pos).Name;
246 public string ParameterDesc (int pos)
248 if (has_varargs && pos >= count)
251 string tmp = String.Empty;
252 Parameter p = GetParameter (pos);
255 // We need to and for REF/OUT, because if either is set the
256 // extra flag ISBYREF will be set as well
258 if ((p.ModFlags & Parameter.Modifier.REF) != 0)
260 else if ((p.ModFlags & Parameter.Modifier.OUT) != 0)
262 else if (p.ModFlags == Parameter.Modifier.PARAMS)
265 Type t = ParameterType (pos);
267 return tmp + TypeManager.CSharpName (t);
270 public Parameter.Modifier ParameterModifier (int pos)
272 if (has_varargs && pos >= count)
273 return Parameter.Modifier.ARGLIST;
275 Parameter.Modifier mod = GetParameter (pos).ModFlags;
277 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
278 mod |= Parameter.Modifier.ISBYREF;
285 public class ReflectionConstraints : GenericConstraints
287 GenericParameterAttributes attrs;
289 Type class_constraint;
290 Type[] iface_constraints;
292 public ReflectionConstraints (Type t)
294 Type[] constraints = t.GetGenericParameterConstraints ();
295 if ((constraints.Length > 0) && !constraints [0].IsInterface) {
296 class_constraint = constraints [0];
297 iface_constraints = new Type [constraints.Length - 1];
298 Array.Copy (constraints, 1, iface_constraints, 0, constraints.Length - 1);
300 iface_constraints = constraints;
301 attrs = t.GenericParameterAttributes;
303 if (HasValueTypeConstraint)
304 base_type = TypeManager.value_type;
305 else if (class_constraint != null)
306 base_type = class_constraint;
308 base_type = TypeManager.object_type;
311 public override GenericParameterAttributes Attributes {
312 get { return attrs; }
315 public override Type ClassConstraint {
316 get { return class_constraint; }
319 public override Type EffectiveBaseClass {
320 get { return base_type; }
323 public override Type[] InterfaceConstraints {
324 get { return iface_constraints; }
328 class PtrHashtable : Hashtable {
329 sealed class PtrComparer : IComparer {
330 private PtrComparer () {}
332 public static PtrComparer Instance = new PtrComparer ();
334 public int Compare (object x, object y)
343 public PtrHashtable ()
345 comparer = PtrComparer.Instance;
350 * Hashtable whose keys are character arrays with the same length
352 class CharArrayHashtable : Hashtable {
353 sealed class ArrComparer : IComparer {
356 public ArrComparer (int len) {
360 public int Compare (object x, object y)
362 char[] a = (char[])x;
363 char[] b = (char[])y;
365 for (int i = 0; i < len; ++i)
374 protected override int GetHash (Object key)
376 char[] arr = (char[])key;
379 for (int i = 0; i < len; ++i)
380 h = (h << 5) - h + arr [i];
385 public CharArrayHashtable (int len)
388 comparer = new ArrComparer (len);
394 public object Second;
396 public Pair (object f, object s)
404 /// This is a wrapper around StreamReader which is seekable.
406 public class SeekableStreamReader
408 public SeekableStreamReader (StreamReader reader)
410 this.reader = reader;
411 this.buffer = new char [DefaultCacheSize];
413 // Compute the preamble size
415 // Let the StreamWriter autodetect the encoder
418 reader.BaseStream.Position = 0;
419 Encoding enc = reader.CurrentEncoding;
420 // First of all, get at least a char
422 byte[] auxb = new byte [50];
427 br = reader.BaseStream.Read (auxb, num_bytes, auxb.Length - num_bytes);
429 num_chars = enc.GetCharCount (auxb, 0, num_bytes);
431 while (num_chars == 0 && br > 0);
435 // Now, check which bytes at the beginning have no effect in the
439 while (enc.GetCharCount (auxb, p, num_bytes-p) >= num_chars)
442 preamble_size = p - 1;
443 reader.BaseStream.Position = 0;
444 reader.DiscardBufferedData ();
446 buffer_start = preamble_size;
450 public SeekableStreamReader (Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks)
451 : this (new StreamReader (stream, encoding, detect_encoding_from_bytemarks))
456 private const int DefaultCacheSize = 1024;
459 int buffer_start; // in bytes
460 int buffer_size; // in bytes
461 int char_count; // count buffer[] valid characters
462 int pos; // index into buffer[]
466 /// The difference to the StreamReader's BaseStream.Position is that this one is reliable; ie. it
467 // always reports the correct position and if it's modified, it also takes care of the buffered data.
469 public int Position {
471 return buffer_start + reader.CurrentEncoding.GetByteCount (buffer, 0, pos);
475 // This one is easy: we're modifying the position within our current
477 if ((value >= buffer_start) && (value < buffer_start + buffer_size)) {
478 int byte_offset = value - buffer_start;
480 // encoded characters can take more than 1 byte length
481 while (reader.CurrentEncoding.GetByteCount (buffer, 0, pos) > byte_offset)
487 if (value == 0) // Skip preamble
488 value = preamble_size;
490 // Ok, now we need to seek.
491 reader.DiscardBufferedData ();
492 reader.BaseStream.Position = buffer_start = value;
493 char_count = buffer_size = pos = 0;
497 private bool ReadBuffer ()
500 buffer_start += buffer_size;
501 char_count = reader.Read (buffer, 0, buffer.Length);
502 buffer_size = reader.CurrentEncoding.GetByteCount (buffer, 0, char_count);
503 return buffer_size > 0;
508 if ((pos >= char_count) && !ReadBuffer ())
516 if ((pos >= char_count) && !ReadBuffer ())
519 return buffer [pos++];
523 public class DoubleHash {
524 const int DEFAULT_INITIAL_BUCKETS = 100;
526 public DoubleHash () : this (DEFAULT_INITIAL_BUCKETS) {}
528 public DoubleHash (int size)
531 buckets = new Entry [size];
545 public Entry (object key1, object key2, int hash, object value, Entry next)
555 public bool Lookup (object a, object b, out object res)
557 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
559 for (Entry e = buckets [h % count]; e != null; e = e.next) {
560 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b)) {
569 public void Insert (object a, object b, object value)
571 // Is it an existing one?
573 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
575 for (Entry e = buckets [h % count]; e != null; e = e.next) {
576 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b))
580 int bucket = h % count;
581 buckets [bucket] = new Entry (a, b, h, value, buckets [bucket]);
583 // Grow whenever we double in size
584 if (size++ == count) {
588 Entry [] newBuckets = new Entry [count];
589 foreach (Entry root in buckets) {
592 int newLoc = e.hash % count;
594 e.next = newBuckets [newLoc];
595 newBuckets [newLoc] = e;
600 buckets = newBuckets;