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;
148 return gpd.ParameterModifier (pos);
150 Type t = pi [pos].ParameterType;
152 if ((pi [pos].Attributes & ParameterAttributes.Out) != 0)
153 return Parameter.Modifier.ISBYREF | Parameter.Modifier.OUT;
155 return Parameter.Modifier.ISBYREF | Parameter.Modifier.REF;
158 return Parameter.Modifier.NONE;
163 return is_varargs ? pi.Length + 1 : pi.Length;
168 public class InternalParameters : ParameterData {
173 public readonly Parameters Parameters;
174 public readonly TypeParameter[] TypeParameters;
176 public InternalParameters (Type [] param_types, Parameters parameters)
178 this.param_types = param_types;
179 this.Parameters = parameters;
181 has_varargs = parameters.HasArglist;
183 if (param_types == null)
186 count = param_types.Length;
189 public InternalParameters (Type [] param_types, Parameters parameters,
190 TypeParameter [] type_params)
191 : this (param_types, parameters)
193 this.TypeParameters = type_params;
198 return has_varargs ? count + 1 : count;
202 public bool HasArrayParameter {
203 get { return Parameters.ArrayParameter != null; }
206 Parameter GetParameter (int pos)
208 Parameter [] fixed_pars = Parameters.FixedParameters;
209 if (fixed_pars != null){
210 int len = fixed_pars.Length;
212 return Parameters.FixedParameters [pos];
215 return Parameters.ArrayParameter;
218 public Type ParameterType (int pos)
220 if (has_varargs && pos >= count)
221 return TypeManager.runtime_argument_handle_type;
223 if (param_types == null)
226 return GetParameter (pos).ExternalType ();
229 public GenericConstraints GenericConstraints (int pos)
231 if (TypeParameters == null)
234 return TypeParameters [pos].Constraints;
237 public string ParameterName (int pos)
239 if (has_varargs && pos >= count)
242 return GetParameter (pos).Name;
245 public string ParameterDesc (int pos)
247 if (has_varargs && pos >= count)
250 string tmp = String.Empty;
251 Parameter p = GetParameter (pos);
254 // We need to and for REF/OUT, because if either is set the
255 // extra flag ISBYREF will be set as well
257 if ((p.ModFlags & Parameter.Modifier.REF) != 0)
259 else if ((p.ModFlags & Parameter.Modifier.OUT) != 0)
261 else if (p.ModFlags == Parameter.Modifier.PARAMS)
264 Type t = ParameterType (pos);
266 return tmp + TypeManager.CSharpName (t);
269 public Parameter.Modifier ParameterModifier (int pos)
271 if (has_varargs && pos >= count)
272 return Parameter.Modifier.ARGLIST;
274 Parameter.Modifier mod = GetParameter (pos).ModFlags;
276 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
277 mod |= Parameter.Modifier.ISBYREF;
284 public class ReflectionConstraints : GenericConstraints
286 GenericParameterAttributes attrs;
288 Type class_constraint;
289 Type[] iface_constraints;
291 public ReflectionConstraints (Type t)
293 Type[] constraints = t.GetGenericParameterConstraints ();
294 if ((constraints.Length > 0) && !constraints [0].IsInterface) {
295 class_constraint = constraints [0];
296 iface_constraints = new Type [constraints.Length - 1];
297 Array.Copy (constraints, 1, iface_constraints, 0, constraints.Length - 1);
299 iface_constraints = constraints;
300 attrs = t.GenericParameterAttributes;
302 if (HasValueTypeConstraint)
303 base_type = TypeManager.value_type;
304 else if (class_constraint != null)
305 base_type = class_constraint;
307 base_type = TypeManager.object_type;
310 public override GenericParameterAttributes Attributes {
311 get { return attrs; }
314 public override Type ClassConstraint {
315 get { return class_constraint; }
318 public override Type EffectiveBaseClass {
319 get { return base_type; }
322 public override Type[] InterfaceConstraints {
323 get { return iface_constraints; }
327 class PtrHashtable : Hashtable {
328 sealed class PtrComparer : IComparer {
329 private PtrComparer () {}
331 public static PtrComparer Instance = new PtrComparer ();
333 public int Compare (object x, object y)
342 public PtrHashtable ()
344 comparer = PtrComparer.Instance;
349 * Hashtable whose keys are character arrays with the same length
351 class CharArrayHashtable : Hashtable {
352 sealed class ArrComparer : IComparer {
355 public ArrComparer (int len) {
359 public int Compare (object x, object y)
361 char[] a = (char[])x;
362 char[] b = (char[])y;
364 for (int i = 0; i < len; ++i)
373 protected override int GetHash (Object key)
375 char[] arr = (char[])key;
378 for (int i = 0; i < len; ++i)
379 h = (h << 5) - h + arr [i];
384 public CharArrayHashtable (int len)
387 comparer = new ArrComparer (len);
393 public object Second;
395 public Pair (object f, object s)
403 /// This is a wrapper around StreamReader which is seekable.
405 public class SeekableStreamReader
407 public SeekableStreamReader (StreamReader reader)
409 this.reader = reader;
410 this.buffer = new char [DefaultCacheSize];
412 // Compute the preamble size
414 // Let the StreamWriter autodetect the encoder
417 reader.BaseStream.Position = 0;
418 Encoding enc = reader.CurrentEncoding;
419 // First of all, get at least a char
421 byte[] auxb = new byte [50];
426 br = reader.BaseStream.Read (auxb, num_bytes, auxb.Length - num_bytes);
428 num_chars = enc.GetCharCount (auxb, 0, num_bytes);
430 while (num_chars == 0 && br > 0);
434 // Now, check which bytes at the beginning have no effect in the
438 while (enc.GetCharCount (auxb, p, num_bytes-p) >= num_chars)
441 preamble_size = p - 1;
442 reader.BaseStream.Position = 0;
443 reader.DiscardBufferedData ();
445 buffer_start = preamble_size;
449 public SeekableStreamReader (Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks)
450 : this (new StreamReader (stream, encoding, detect_encoding_from_bytemarks))
455 private const int DefaultCacheSize = 1024;
458 int buffer_start; // in bytes
459 int buffer_size; // in bytes
460 int char_count; // count buffer[] valid characters
461 int pos; // index into buffer[]
465 /// The difference to the StreamReader's BaseStream.Position is that this one is reliable; ie. it
466 // always reports the correct position and if it's modified, it also takes care of the buffered data.
468 public int Position {
470 return buffer_start + reader.CurrentEncoding.GetByteCount (buffer, 0, pos);
474 // This one is easy: we're modifying the position within our current
476 if ((value >= buffer_start) && (value < buffer_start + buffer_size)) {
477 int byte_offset = value - buffer_start;
479 // encoded characters can take more than 1 byte length
480 while (reader.CurrentEncoding.GetByteCount (buffer, 0, pos) > byte_offset)
486 if (value == 0) // Skip preamble
487 value = preamble_size;
489 // Ok, now we need to seek.
490 reader.DiscardBufferedData ();
491 reader.BaseStream.Position = buffer_start = value;
492 char_count = buffer_size = pos = 0;
496 private bool ReadBuffer ()
499 buffer_start += buffer_size;
500 char_count = reader.Read (buffer, 0, buffer.Length);
501 buffer_size = reader.CurrentEncoding.GetByteCount (buffer, 0, char_count);
502 return buffer_size > 0;
507 if ((pos >= char_count) && !ReadBuffer ())
515 if ((pos >= char_count) && !ReadBuffer ())
518 return buffer [pos++];
522 public class DoubleHash {
523 const int DEFAULT_INITIAL_BUCKETS = 100;
525 public DoubleHash () : this (DEFAULT_INITIAL_BUCKETS) {}
527 public DoubleHash (int size)
530 buckets = new Entry [size];
544 public Entry (object key1, object key2, int hash, object value, Entry next)
554 public bool Lookup (object a, object b, out object res)
556 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
558 for (Entry e = buckets [h % count]; e != null; e = e.next) {
559 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b)) {
568 public void Insert (object a, object b, object value)
570 // Is it an existing one?
572 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
574 for (Entry e = buckets [h % count]; e != null; e = e.next) {
575 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b))
579 int bucket = h % count;
580 buckets [bucket] = new Entry (a, b, h, value, buckets [bucket]);
582 // Grow whenever we double in size
583 if (size++ == count) {
587 Entry [] newBuckets = new Entry [count];
588 foreach (Entry root in buckets) {
591 int newLoc = e.hash % count;
593 e.next = newBuckets [newLoc];
594 newBuckets [newLoc] = e;
599 buckets = newBuckets;