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 GenericConstraints {
22 bool HasConstructor { get; }
23 bool IsReferenceType { get; }
24 bool IsValueType { get; }
25 bool HasClassConstraint { get; }
26 Type ClassConstraint { get; }
27 Type[] InterfaceConstraints { get; }
30 public interface ParameterData {
31 Type ParameterType (int pos);
32 GenericConstraints GenericConstraints (int pos);
33 bool HasArrayParameter { get; }
35 string ParameterName (int pos);
36 string ParameterDesc (int pos);
37 Parameter.Modifier ParameterModifier (int pos);
40 public class ReflectionParameters : ParameterData {
42 bool last_arg_is_params = false;
43 bool is_varargs = false;
46 public ReflectionParameters (MethodBase mb)
50 ParameterInfo [] pi = mb.GetParameters ();
51 is_varargs = (mb.CallingConvention & CallingConventions.VarArgs) != 0;
54 int count = pi.Length-1;
59 if (mb.Mono_IsInflatedMethod) {
60 MethodInfo generic = mb.GetGenericMethodDefinition ();
61 gpd = Invocation.GetParameterData (generic);
63 last_arg_is_params = gpd.HasArrayParameter;
67 attrs = pi [count].GetCustomAttributes (TypeManager.param_array_type, true);
71 if (attrs.Length == 0)
74 last_arg_is_params = true;
77 public bool HasArrayParameter {
78 get { return last_arg_is_params; }
81 public Type ParameterType (int pos)
83 if (last_arg_is_params && pos >= pi.Length - 1)
84 return pi [pi.Length - 1].ParameterType;
85 else if (is_varargs && pos >= pi.Length)
86 return TypeManager.runtime_argument_handle_type;
88 Type t = pi [pos].ParameterType;
94 public GenericConstraints GenericConstraints (int pos)
97 return gpd.GenericConstraints (pos);
99 Type t = ParameterType (pos);
100 if (!t.IsGenericParameter)
103 return ReflectionConstraints.Create (t);
106 public string ParameterName (int pos)
108 if (last_arg_is_params && pos >= pi.Length - 1)
109 return pi [pi.Length - 1].Name;
110 else if (is_varargs && pos >= pi.Length)
113 return pi [pos].Name;
116 public string ParameterDesc (int pos)
118 if (is_varargs && pos >= pi.Length)
121 StringBuilder sb = new StringBuilder ();
126 Type partype = ParameterType (pos);
127 if (partype.IsByRef){
128 partype = TypeManager.GetElementType (partype);
135 if (pos >= pi.Length - 1 && last_arg_is_params)
136 sb.Append ("params ");
138 sb.Append (TypeManager.CSharpName (partype));
140 return sb.ToString ();
144 public Parameter.Modifier ParameterModifier (int pos)
148 if (last_arg_is_params && pos >= pi.Length - 1)
149 return Parameter.Modifier.PARAMS;
150 else if (is_varargs && pos >= pi.Length)
151 return Parameter.Modifier.ARGLIST;
153 Type t = pi [pos].ParameterType;
155 if ((pi [pos].Attributes & ParameterAttributes.Out) != 0)
156 return Parameter.Modifier.ISBYREF | Parameter.Modifier.OUT;
158 return Parameter.Modifier.ISBYREF | Parameter.Modifier.REF;
161 return Parameter.Modifier.NONE;
166 return is_varargs ? pi.Length + 1 : pi.Length;
170 protected class ReflectionConstraints : GenericConstraints
173 bool is_reference_type;
175 Type class_constraint;
176 Type[] iface_constraints;
178 protected ReflectionConstraints (bool has_ctor, Type class_constr,
179 Type[] iface_constrs)
181 this.has_ctor = has_ctor;
182 this.class_constraint = class_constr;
183 this.iface_constraints = iface_constrs;
185 if (class_constraint != null) {
186 if (class_constraint == TypeManager.object_type)
187 is_reference_type = true;
188 else if (class_constraint == TypeManager.value_type)
189 is_value_type = true;
193 public static GenericConstraints Create (Type t)
195 Type class_constr = null;
196 Type[] iface_constrs = t.GetInterfaces ();
197 if (iface_constrs == null)
198 iface_constrs = Type.EmptyTypes;
199 if (t.BaseType != TypeManager.object_type)
200 class_constr = t.BaseType;
202 return new ReflectionConstraints (
203 false, class_constr, iface_constrs);
206 public bool HasConstructor {
207 get { return has_ctor; }
210 public bool HasClassConstraint {
211 get { return class_constraint != null; }
214 public bool IsReferenceType {
215 get { return is_reference_type; }
218 public bool IsValueType {
219 get { return is_value_type; }
222 public Type ClassConstraint {
223 get { return class_constraint; }
226 public Type[] InterfaceConstraints {
227 get { return iface_constraints; }
232 public class InternalParameters : ParameterData {
237 public readonly Parameters Parameters;
239 public InternalParameters (Type [] param_types, Parameters parameters)
241 this.param_types = param_types;
242 this.Parameters = parameters;
245 public InternalParameters (DeclSpace ds, Parameters parameters)
246 : this (parameters.GetParameterInfo (ds), parameters)
248 has_varargs = parameters.HasArglist;
250 if (param_types == null)
253 count = param_types.Length;
258 return has_varargs ? count + 1 : count;
262 public bool HasArrayParameter {
263 get { return Parameters.ArrayParameter != null; }
266 Parameter GetParameter (int pos)
268 Parameter [] fixed_pars = Parameters.FixedParameters;
269 if (fixed_pars != null){
270 int len = fixed_pars.Length;
272 return Parameters.FixedParameters [pos];
275 return Parameters.ArrayParameter;
278 public Type ParameterType (int pos)
280 if (has_varargs && pos >= count)
281 return TypeManager.runtime_argument_handle_type;
283 if (param_types == null)
286 return GetParameter (pos).ExternalType ();
289 public GenericConstraints GenericConstraints (int pos)
291 if (param_types == null)
294 return GetParameter (pos).GenericConstraints;
297 public string ParameterName (int pos)
299 if (has_varargs && pos >= count)
302 return GetParameter (pos).Name;
305 public string ParameterDesc (int pos)
307 if (has_varargs && pos >= count)
310 string tmp = String.Empty;
311 Parameter p = GetParameter (pos);
314 // We need to and for REF/OUT, because if either is set the
315 // extra flag ISBYREF will be set as well
317 if ((p.ModFlags & Parameter.Modifier.REF) != 0)
319 else if ((p.ModFlags & Parameter.Modifier.OUT) != 0)
321 else if (p.ModFlags == Parameter.Modifier.PARAMS)
324 Type t = ParameterType (pos);
326 return tmp + TypeManager.CSharpName (t);
329 public Parameter.Modifier ParameterModifier (int pos)
331 if (has_varargs && pos >= count)
332 return Parameter.Modifier.ARGLIST;
334 Parameter.Modifier mod = GetParameter (pos).ModFlags;
336 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
337 mod |= Parameter.Modifier.ISBYREF;
344 class PtrHashtable : Hashtable {
345 sealed class PtrComparer : IComparer {
346 private PtrComparer () {}
348 public static PtrComparer Instance = new PtrComparer ();
350 public int Compare (object x, object y)
359 public PtrHashtable ()
361 comparer = PtrComparer.Instance;
366 * Hashtable whose keys are character arrays with the same length
368 class CharArrayHashtable : Hashtable {
369 sealed class ArrComparer : IComparer {
372 public ArrComparer (int len) {
376 public int Compare (object x, object y)
378 char[] a = (char[])x;
379 char[] b = (char[])y;
381 for (int i = 0; i < len; ++i)
390 protected override int GetHash (Object key)
392 char[] arr = (char[])key;
395 for (int i = 0; i < len; ++i)
396 h = (h << 5) - h + arr [i];
401 public CharArrayHashtable (int len)
404 comparer = new ArrComparer (len);
410 public object Second;
412 public Pair (object f, object s)
420 /// This is a wrapper around StreamReader which is seekable.
422 public class SeekableStreamReader
424 public SeekableStreamReader (StreamReader reader)
426 this.reader = reader;
427 this.buffer = new char [DefaultCacheSize];
429 // Compute the preamble size
431 // Let the StreamWriter autodetect the encoder
434 reader.BaseStream.Position = 0;
435 Encoding enc = reader.CurrentEncoding;
436 // First of all, get at least a char
438 byte[] auxb = new byte [50];
443 br = reader.BaseStream.Read (auxb, num_bytes, auxb.Length - num_bytes);
445 num_chars = enc.GetCharCount (auxb, 0, num_bytes);
447 while (num_chars == 0 && br > 0);
451 // Now, check which bytes at the beginning have no effect in the
455 while (enc.GetCharCount (auxb, p, num_bytes-p) >= num_chars)
458 preamble_size = p - 1;
459 reader.BaseStream.Position = 0;
460 reader.DiscardBufferedData ();
462 buffer_start = preamble_size;
466 public SeekableStreamReader (Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks)
467 : this (new StreamReader (stream, encoding, detect_encoding_from_bytemarks))
472 private const int DefaultCacheSize = 1024;
475 int buffer_start; // in bytes
476 int buffer_size; // in bytes
477 int char_count; // count buffer[] valid characters
478 int pos; // index into buffer[]
482 /// The difference to the StreamReader's BaseStream.Position is that this one is reliable; ie. it
483 // always reports the correct position and if it's modified, it also takes care of the buffered data.
485 public int Position {
487 return buffer_start + reader.CurrentEncoding.GetByteCount (buffer, 0, pos);
491 // This one is easy: we're modifying the position within our current
493 if ((value >= buffer_start) && (value < buffer_start + buffer_size)) {
494 int byte_offset = value - buffer_start;
496 // encoded characters can take more than 1 byte length
497 while (reader.CurrentEncoding.GetByteCount (buffer, 0, pos) > byte_offset)
503 if (value == 0) // Skip preamble
504 value = preamble_size;
506 // Ok, now we need to seek.
507 reader.DiscardBufferedData ();
508 reader.BaseStream.Position = buffer_start = value;
509 char_count = buffer_size = pos = 0;
513 private bool ReadBuffer ()
516 buffer_start += buffer_size;
517 char_count = reader.Read (buffer, 0, buffer.Length);
518 buffer_size = reader.CurrentEncoding.GetByteCount (buffer, 0, char_count);
519 return buffer_size > 0;
524 if ((pos >= char_count) && !ReadBuffer ())
532 if ((pos >= char_count) && !ReadBuffer ())
535 return buffer [pos++];
539 public class DoubleHash {
540 const int DEFAULT_INITIAL_BUCKETS = 100;
542 public DoubleHash () : this (DEFAULT_INITIAL_BUCKETS) {}
544 public DoubleHash (int size)
547 buckets = new Entry [size];
561 public Entry (object key1, object key2, int hash, object value, Entry next)
571 public bool Lookup (object a, object b, out object res)
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)) {
585 public void Insert (object a, object b, object value)
587 // Is it an existing one?
589 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
591 for (Entry e = buckets [h % count]; e != null; e = e.next) {
592 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b))
596 int bucket = h % count;
597 buckets [bucket] = new Entry (a, b, h, value, buckets [bucket]);
599 // Grow whenever we double in size
600 if (size++ == count) {
604 Entry [] newBuckets = new Entry [count];
605 foreach (Entry root in buckets) {
608 int newLoc = e.hash % count;
610 e.next = newBuckets [newLoc];
611 newBuckets [newLoc] = e;
616 buckets = newBuckets;