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);
409 // Compares member infos based on their name and
410 // also allows one argument to be a string
412 class MemberInfoCompare : IComparer {
414 public int Compare (object a, object b)
416 if (a == null || b == null){
417 Console.WriteLine ("Invalid information passed");
418 throw new Exception ();
422 return String.Compare ((string) a, ((MemberInfo)b).Name, false, CultureInfo.InvariantCulture);
425 return String.Compare (((MemberInfo)a).Name, (string) b, false, CultureInfo.InvariantCulture);
427 return String.Compare (((MemberInfo)a).Name, ((MemberInfo)b).Name, false, CultureInfo.InvariantCulture);
433 public object Second;
435 public Pair (object f, object s)
443 /// This is a wrapper around StreamReader which is seekable.
445 public class SeekableStreamReader
447 public SeekableStreamReader (StreamReader reader)
449 this.reader = reader;
450 this.buffer = new char [DefaultCacheSize];
452 // Compute the preamble size
454 // Let the StreamWriter autodetect the encoder
457 reader.BaseStream.Position = 0;
458 Encoding enc = reader.CurrentEncoding;
459 // First of all, get at least a char
461 byte[] auxb = new byte [50];
466 br = reader.BaseStream.Read (auxb, num_bytes, auxb.Length - num_bytes);
468 num_chars = enc.GetCharCount (auxb, 0, num_bytes);
470 while (num_chars == 0 && br > 0);
474 // Now, check which bytes at the beginning have no effect in the
478 while (enc.GetCharCount (auxb, p, num_bytes-p) >= num_chars)
481 preamble_size = p - 1;
482 reader.BaseStream.Position = 0;
483 reader.DiscardBufferedData ();
485 buffer_start = preamble_size;
489 public SeekableStreamReader (Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks)
490 : this (new StreamReader (stream, encoding, detect_encoding_from_bytemarks))
495 private const int DefaultCacheSize = 1024;
498 int buffer_start; // in bytes
499 int buffer_size; // in bytes
500 int char_count; // count buffer[] valid characters
501 int pos; // index into buffer[]
505 /// The difference to the StreamReader's BaseStream.Position is that this one is reliable; ie. it
506 // always reports the correct position and if it's modified, it also takes care of the buffered data.
508 public int Position {
510 return buffer_start + reader.CurrentEncoding.GetByteCount (buffer, 0, pos);
514 // This one is easy: we're modifying the position within our current
516 if ((value >= buffer_start) && (value < buffer_start + buffer_size)) {
517 int byte_offset = value - buffer_start;
519 // encoded characters can take more than 1 byte length
520 while (reader.CurrentEncoding.GetByteCount (buffer, 0, pos) > byte_offset)
526 if (value == 0) // Skip preamble
527 value = preamble_size;
529 // Ok, now we need to seek.
530 reader.DiscardBufferedData ();
531 reader.BaseStream.Position = buffer_start = value;
532 char_count = buffer_size = pos = 0;
536 private bool ReadBuffer ()
539 buffer_start += buffer_size;
540 char_count = reader.Read (buffer, 0, buffer.Length);
541 buffer_size = reader.CurrentEncoding.GetByteCount (buffer, 0, char_count);
542 return buffer_size > 0;
547 if ((pos >= char_count) && !ReadBuffer ())
555 if ((pos >= char_count) && !ReadBuffer ())
558 return buffer [pos++];
562 public class DoubleHash {
563 const int DEFAULT_INITIAL_BUCKETS = 100;
565 public DoubleHash () : this (DEFAULT_INITIAL_BUCKETS) {}
567 public DoubleHash (int size)
570 buckets = new Entry [size];
584 public Entry (object key1, object key2, int hash, object value, Entry next)
594 public bool Lookup (object a, object b, out object res)
596 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
598 for (Entry e = buckets [h % count]; e != null; e = e.next) {
599 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b)) {
608 public void Insert (object a, object b, object value)
610 // Is it an existing one?
612 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
614 for (Entry e = buckets [h % count]; e != null; e = e.next) {
615 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b))
619 int bucket = h % count;
620 buckets [bucket] = new Entry (a, b, h, value, buckets [bucket]);
622 // Grow whenever we double in size
623 if (size++ == count) {
627 Entry [] newBuckets = new Entry [count];
628 foreach (Entry root in buckets) {
631 int newLoc = e.hash % count;
633 e.next = newBuckets [newLoc];
634 newBuckets [newLoc] = e;
639 buckets = newBuckets;