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)
111 if (last_arg_is_params && pos >= pi.Length - 1)
112 return Parameter.Modifier.PARAMS;
113 else if (is_varargs && pos >= pi.Length)
114 return Parameter.Modifier.ARGLIST;
116 Type t = pi [pos].ParameterType;
118 if ((pi [pos].Attributes & ParameterAttributes.Out) != 0)
119 return Parameter.Modifier.ISBYREF | Parameter.Modifier.OUT;
121 return Parameter.Modifier.ISBYREF | Parameter.Modifier.REF;
124 return Parameter.Modifier.NONE;
129 return is_varargs ? pi.Length + 1 : pi.Length;
133 public bool HasParams {
135 return this.last_arg_is_params;
141 public class InternalParameters : ParameterData {
146 public readonly Parameters Parameters;
148 public InternalParameters (Type [] param_types, Parameters parameters)
150 this.param_types = param_types;
151 this.Parameters = parameters;
153 has_varargs = parameters.HasArglist;
155 if (param_types == null)
158 count = param_types.Length;
163 return has_varargs ? count + 1 : count;
167 public bool HasParams {
169 return Parameters.ArrayParameter != null;
173 Parameter GetParameter (int pos)
175 Parameter [] fixed_pars = Parameters.FixedParameters;
176 if (fixed_pars != null){
177 int len = fixed_pars.Length;
179 return Parameters.FixedParameters [pos];
182 return Parameters.ArrayParameter;
185 public Type ParameterType (int pos)
187 if (has_varargs && pos >= count)
188 return TypeManager.runtime_argument_handle_type;
190 if (param_types == null)
193 return GetParameter (pos).ExternalType ();
197 public string ParameterName (int pos)
199 if (has_varargs && pos >= count)
202 return GetParameter (pos).Name;
205 public string ParameterDesc (int pos)
207 if (has_varargs && pos >= count)
210 string tmp = String.Empty;
211 Parameter p = GetParameter (pos);
214 // We need to and for REF/OUT, because if either is set the
215 // extra flag ISBYREF will be set as well
217 if ((p.ModFlags & Parameter.Modifier.REF) != 0)
219 else if ((p.ModFlags & Parameter.Modifier.OUT) != 0)
221 else if (p.ModFlags == Parameter.Modifier.PARAMS)
224 Type t = ParameterType (pos);
226 return tmp + TypeManager.CSharpName (t);
229 public Parameter.Modifier ParameterModifier (int pos)
231 if (has_varargs && pos >= count)
232 return Parameter.Modifier.ARGLIST;
234 Parameter.Modifier mod = GetParameter (pos).ModFlags;
236 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
237 mod |= Parameter.Modifier.ISBYREF;
244 class PtrHashtable : Hashtable {
245 sealed class PtrComparer : IComparer {
246 private PtrComparer () {}
248 public static PtrComparer Instance = new PtrComparer ();
250 public int Compare (object x, object y)
259 public PtrHashtable ()
261 comparer = PtrComparer.Instance;
266 * Hashtable whose keys are character arrays with the same length
268 class CharArrayHashtable : Hashtable {
269 sealed class ArrComparer : IComparer {
272 public ArrComparer (int len) {
276 public int Compare (object x, object y)
278 char[] a = (char[])x;
279 char[] b = (char[])y;
281 for (int i = 0; i < len; ++i)
290 protected override int GetHash (Object key)
292 char[] arr = (char[])key;
295 for (int i = 0; i < len; ++i)
296 h = (h << 5) - h + arr [i];
301 public CharArrayHashtable (int len)
304 comparer = new ArrComparer (len);
310 public object Second;
312 public Pair (object f, object s)
320 /// This is a wrapper around StreamReader which is seekable.
322 public class SeekableStreamReader
324 public SeekableStreamReader (StreamReader reader)
326 this.reader = reader;
327 this.buffer = new char [DefaultCacheSize];
329 // Compute the preamble size
331 // Let the StreamWriter autodetect the encoder
334 reader.BaseStream.Position = 0;
335 Encoding enc = reader.CurrentEncoding;
336 // First of all, get at least a char
338 byte[] auxb = new byte [50];
343 br = reader.BaseStream.Read (auxb, num_bytes, auxb.Length - num_bytes);
345 num_chars = enc.GetCharCount (auxb, 0, num_bytes);
347 while (num_chars == 0 && br > 0);
351 // Now, check which bytes at the beginning have no effect in the
355 while (enc.GetCharCount (auxb, p, num_bytes-p) >= num_chars)
358 preamble_size = p - 1;
359 reader.BaseStream.Position = 0;
360 reader.DiscardBufferedData ();
362 buffer_start = preamble_size;
366 public SeekableStreamReader (Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks)
367 : this (new StreamReader (stream, encoding, detect_encoding_from_bytemarks))
372 private const int DefaultCacheSize = 1024;
375 int buffer_start; // in bytes
376 int buffer_size; // in bytes
377 int char_count; // count buffer[] valid characters
378 int pos; // index into buffer[]
382 /// The difference to the StreamReader's BaseStream.Position is that this one is reliable; ie. it
383 // always reports the correct position and if it's modified, it also takes care of the buffered data.
385 public int Position {
387 return buffer_start + reader.CurrentEncoding.GetByteCount (buffer, 0, pos);
391 // This one is easy: we're modifying the position within our current
393 if ((value >= buffer_start) && (value < buffer_start + buffer_size)) {
394 int byte_offset = value - buffer_start;
396 // pos is an index into a char
397 // buffer so it might be
398 // greater than the buffer
399 // length now, if the buffer
400 // contains multibyte chars
403 // encoded characters can take
404 // more than 1 byte length.
405 while ((pos > buffer.Length) ||
406 reader.CurrentEncoding.GetByteCount (buffer, 0, pos) > byte_offset) {
413 if (value == 0) // Skip preamble
414 value = preamble_size;
416 // Ok, now we need to seek.
417 reader.DiscardBufferedData ();
418 reader.BaseStream.Position = buffer_start = value;
419 char_count = buffer_size = pos = 0;
423 private bool ReadBuffer ()
426 buffer_start += buffer_size;
427 char_count = reader.Read (buffer, 0, buffer.Length);
428 buffer_size = reader.CurrentEncoding.GetByteCount (buffer, 0, char_count);
429 return buffer_size > 0;
434 if ((pos >= char_count) && !ReadBuffer ())
442 if ((pos >= char_count) && !ReadBuffer ())
445 return buffer [pos++];
449 public class DoubleHash {
450 const int DEFAULT_INITIAL_BUCKETS = 100;
452 public DoubleHash () : this (DEFAULT_INITIAL_BUCKETS) {}
454 public DoubleHash (int size)
457 buckets = new Entry [size];
471 public Entry (object key1, object key2, int hash, object value, Entry next)
481 public bool Lookup (object a, object b, out object res)
483 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
485 for (Entry e = buckets [h % count]; e != null; e = e.next) {
486 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b)) {
495 public void Insert (object a, object b, object value)
497 // Is it an existing one?
499 int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
501 for (Entry e = buckets [h % count]; e != null; e = e.next) {
502 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b))
506 int bucket = h % count;
507 buckets [bucket] = new Entry (a, b, h, value, buckets [bucket]);
509 // Grow whenever we double in size
510 if (size++ == count) {
514 Entry [] newBuckets = new Entry [count];
515 foreach (Entry root in buckets) {
518 int newLoc = e.hash % count;
520 e.next = newBuckets [newLoc];
521 newBuckets [newLoc] = e;
526 buckets = newBuckets;