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);
24 bool HasParams { get; }
25 string ParameterName (int pos);
26 string ParameterDesc (int pos);
27 Parameter.Modifier ParameterModifier (int pos);
30 public class ReflectionParameters : ParameterData {
32 bool last_arg_is_params = false;
33 bool is_varargs = false;
35 public ReflectionParameters (MethodBase mb)
39 ParameterInfo [] pi = mb.GetParameters ();
40 is_varargs = (mb.CallingConvention & CallingConventions.VarArgs) != 0;
43 int count = pi.Length-1;
46 attrs = pi [count].GetCustomAttributes (TypeManager.param_array_type, true);
51 if (attrs.Length == 0)
54 last_arg_is_params = true;
58 public Type ParameterType (int pos)
60 if (last_arg_is_params && pos >= pi.Length - 1)
61 return pi [pi.Length - 1].ParameterType;
62 else if (is_varargs && pos >= pi.Length)
63 return TypeManager.runtime_argument_handle_type;
65 Type t = pi [pos].ParameterType;
71 public string ParameterName (int pos)
73 if (last_arg_is_params && pos >= pi.Length - 1)
74 return pi [pi.Length - 1].Name;
75 else if (is_varargs && pos >= pi.Length)
81 public string ParameterDesc (int pos)
83 if (is_varargs && pos >= pi.Length)
86 StringBuilder sb = new StringBuilder ();
91 Type partype = ParameterType (pos);
93 partype = TypeManager.GetElementType (partype);
100 if (pos >= pi.Length - 1 && last_arg_is_params)
101 sb.Append ("params ");
103 sb.Append (TypeManager.CSharpName (partype));
105 return sb.ToString ();
109 public Parameter.Modifier ParameterModifier (int pos)
113 if (last_arg_is_params && pos >= pi.Length - 1)
114 return Parameter.Modifier.PARAMS;
115 else if (is_varargs && pos >= pi.Length)
116 return Parameter.Modifier.ARGLIST;
118 Type t = pi [pos].ParameterType;
120 if ((pi [pos].Attributes & ParameterAttributes.Out) != 0)
121 return Parameter.Modifier.ISBYREF | Parameter.Modifier.OUT;
123 return Parameter.Modifier.ISBYREF | Parameter.Modifier.REF;
126 return Parameter.Modifier.NONE;
131 return is_varargs ? pi.Length + 1 : pi.Length;
135 public bool HasParams {
137 return this.last_arg_is_params;
143 public class InternalParameters : ParameterData {
148 public readonly Parameters Parameters;
150 public InternalParameters (Type [] param_types, Parameters parameters)
152 this.param_types = param_types;
153 this.Parameters = parameters;
155 has_varargs = parameters.HasArglist;
157 if (param_types == null)
160 count = param_types.Length;
165 return has_varargs ? count + 1 : count;
169 public bool HasParams {
171 return Parameters.ArrayParameter != null;
175 Parameter GetParameter (int pos)
177 Parameter [] fixed_pars = Parameters.FixedParameters;
178 if (fixed_pars != null){
179 int len = fixed_pars.Length;
181 return Parameters.FixedParameters [pos];
184 return Parameters.ArrayParameter;
187 public Type ParameterType (int pos)
189 if (has_varargs && pos >= count)
190 return TypeManager.runtime_argument_handle_type;
192 if (param_types == null)
195 return GetParameter (pos).ExternalType ();
199 public string ParameterName (int pos)
201 if (has_varargs && pos >= count)
204 return GetParameter (pos).Name;
207 public string ParameterDesc (int pos)
209 if (has_varargs && pos >= count)
212 string tmp = String.Empty;
213 Parameter p = GetParameter (pos);
216 // We need to and for REF/OUT, because if either is set the
217 // extra flag ISBYREF will be set as well
219 if ((p.ModFlags & Parameter.Modifier.REF) != 0)
221 else if ((p.ModFlags & Parameter.Modifier.OUT) != 0)
223 else if (p.ModFlags == Parameter.Modifier.PARAMS)
226 Type t = ParameterType (pos);
228 return tmp + TypeManager.CSharpName (t);
231 public Parameter.Modifier ParameterModifier (int pos)
233 if (has_varargs && pos >= count)
234 return Parameter.Modifier.ARGLIST;
236 Parameter.Modifier mod = GetParameter (pos).ModFlags;
238 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
239 mod |= Parameter.Modifier.ISBYREF;
246 class PtrHashtable : Hashtable {
247 sealed class PtrComparer : IComparer {
248 private PtrComparer () {}
250 public static PtrComparer Instance = new PtrComparer ();
252 public int Compare (object x, object y)
261 public PtrHashtable ()
263 comparer = PtrComparer.Instance;
268 * Hashtable whose keys are character arrays with the same length
270 class CharArrayHashtable : Hashtable {
271 sealed class ArrComparer : IComparer {
274 public ArrComparer (int len) {
278 public int Compare (object x, object y)
280 char[] a = (char[])x;
281 char[] b = (char[])y;
283 for (int i = 0; i < len; ++i)
292 protected override int GetHash (Object key)
294 char[] arr = (char[])key;
297 for (int i = 0; i < len; ++i)
298 h = (h << 5) - h + arr [i];
303 public CharArrayHashtable (int len)
306 comparer = new ArrComparer (len);
312 public object Second;
314 public Pair (object f, object s)
322 /// This is a wrapper around StreamReader which is seekable.
324 public class SeekableStreamReader
326 public SeekableStreamReader (StreamReader reader)
328 this.reader = reader;
329 this.buffer = new char [DefaultCacheSize];
331 // Compute the preamble size
333 // Let the StreamWriter autodetect the encoder
336 reader.BaseStream.Position = 0;
337 Encoding enc = reader.CurrentEncoding;
338 // First of all, get at least a char
340 byte[] auxb = new byte [50];
345 br = reader.BaseStream.Read (auxb, num_bytes, auxb.Length - num_bytes);
347 num_chars = enc.GetCharCount (auxb, 0, num_bytes);
349 while (num_chars == 0 && br > 0);
353 // Now, check which bytes at the beginning have no effect in the
357 while (enc.GetCharCount (auxb, p, num_bytes-p) >= num_chars)
360 preamble_size = p - 1;
361 reader.BaseStream.Position = 0;
362 reader.DiscardBufferedData ();
364 buffer_start = preamble_size;
368 public SeekableStreamReader (Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks)
369 : this (new StreamReader (stream, encoding, detect_encoding_from_bytemarks))
374 private const int DefaultCacheSize = 1024;
377 int buffer_start; // in bytes
378 int buffer_size; // in bytes
379 int char_count; // count buffer[] valid characters
380 int pos; // index into buffer[]
384 /// The difference to the StreamReader's BaseStream.Position is that this one is reliable; ie. it
385 // always reports the correct position and if it's modified, it also takes care of the buffered data.
387 public int Position {
389 return buffer_start + reader.CurrentEncoding.GetByteCount (buffer, 0, pos);
393 // This one is easy: we're modifying the position within our current
395 if ((value >= buffer_start) && (value < buffer_start + buffer_size)) {
396 int byte_offset = value - buffer_start;
398 // encoded characters can take more than 1 byte length
399 while (reader.CurrentEncoding.GetByteCount (buffer, 0, pos) > byte_offset)
405 if (value == 0) // Skip preamble
406 value = preamble_size;
408 // Ok, now we need to seek.
409 reader.DiscardBufferedData ();
410 reader.BaseStream.Position = buffer_start = value;
411 char_count = buffer_size = pos = 0;
415 private bool ReadBuffer ()
418 buffer_start += buffer_size;
419 char_count = reader.Read (buffer, 0, buffer.Length);
420 buffer_size = reader.CurrentEncoding.GetByteCount (buffer, 0, char_count);
421 return buffer_size > 0;
426 if ((pos >= char_count) && !ReadBuffer ())
434 if ((pos >= char_count) && !ReadBuffer ())
437 return buffer [pos++];
441 public class DoubleHash {
442 const int DEFAULT_INITIAL_BUCKETS = 100;
444 public DoubleHash () : this (DEFAULT_INITIAL_BUCKETS) {}
446 public DoubleHash (int size)
449 buckets = new Entry [size];
463 public Entry (object key1, object key2, int hash, object value, Entry next)
473 public bool Lookup (object a, object b, out object res)
475 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
477 for (Entry e = buckets [h % count]; e != null; e = e.next) {
478 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b)) {
487 public void Insert (object a, object b, object value)
489 // Is it an existing one?
491 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
493 for (Entry e = buckets [h % count]; e != null; e = e.next) {
494 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b))
498 int bucket = h % count;
499 buckets [bucket] = new Entry (a, b, h, value, buckets [bucket]);
501 // Grow whenever we double in size
502 if (size++ == count) {
506 Entry [] newBuckets = new Entry [count];
507 foreach (Entry root in buckets) {
510 int newLoc = e.hash % count;
512 e.next = newBuckets [newLoc];
513 newBuckets [newLoc] = e;
518 buckets = newBuckets;