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);
25 bool HasParams { get; }
26 string ParameterName (int pos);
27 string ParameterDesc (int pos);
28 Parameter.Modifier ParameterModifier (int pos);
29 string GetSignatureForError ();
32 public class ReflectionParameters : ParameterData {
35 bool last_arg_is_params = false;
36 bool is_varargs = false;
39 public ReflectionParameters (MethodBase mb)
43 ParameterInfo [] pi = mb.GetParameters ();
44 is_varargs = (mb.CallingConvention & CallingConventions.VarArgs) != 0;
47 int count = pi.Length-1;
52 if (mb.Mono_IsInflatedMethod) {
53 MethodInfo generic = mb.GetGenericMethodDefinition ();
54 gpd = TypeManager.GetParameterData (generic);
56 last_arg_is_params = gpd.HasParams;
60 if (mb.IsGenericMethodDefinition)
61 type_params = mb.GetGenericArguments ();
63 attrs = pi [count].GetCustomAttributes (TypeManager.param_array_type, true);
67 if (attrs.Length == 0)
70 last_arg_is_params = true;
73 public string GetSignatureForError ()
75 StringBuilder sb = new StringBuilder ("(");
76 for (int i = 0; i < pi.Length; ++i) {
79 sb.Append (ParameterDesc (i));
82 return sb.ToString ();
85 public Type ParameterType (int pos)
87 if (last_arg_is_params && pos >= pi.Length - 1)
88 return pi [pi.Length - 1].ParameterType;
89 else if (is_varargs && pos >= pi.Length)
90 return TypeManager.runtime_argument_handle_type;
92 Type t = pi [pos].ParameterType;
98 public GenericConstraints GenericConstraints (int pos)
101 return gpd.GenericConstraints (pos);
103 if (type_params == null)
106 return new ReflectionConstraints (type_params [pos]);
109 public string ParameterName (int pos)
112 return gpd.ParameterName (pos);
114 if (last_arg_is_params && pos >= pi.Length - 1)
115 return pi [pi.Length - 1].Name;
116 else if (is_varargs && pos >= pi.Length)
119 return pi [pos].Name;
122 public string ParameterDesc (int pos)
124 if (is_varargs && pos >= pi.Length)
127 StringBuilder sb = new StringBuilder ();
132 Type partype = ParameterType (pos);
133 if (partype.IsByRef){
134 partype = TypeManager.GetElementType (partype);
141 if (pos >= pi.Length - 1 && last_arg_is_params)
142 sb.Append ("params ");
144 sb.Append (TypeManager.CSharpName (partype).Replace ("&", ""));
146 return sb.ToString ();
150 public Parameter.Modifier ParameterModifier (int pos)
152 if (last_arg_is_params && pos >= pi.Length - 1)
153 return Parameter.Modifier.PARAMS;
154 else if (is_varargs && pos >= pi.Length)
155 return Parameter.Modifier.ARGLIST;
158 return gpd.ParameterModifier (pos);
160 Type t = pi [pos].ParameterType;
162 if ((pi [pos].Attributes & (ParameterAttributes.Out|ParameterAttributes.In)) == ParameterAttributes.Out)
163 return Parameter.Modifier.ISBYREF | Parameter.Modifier.OUT;
165 return Parameter.Modifier.ISBYREF | Parameter.Modifier.REF;
168 return Parameter.Modifier.NONE;
173 return is_varargs ? pi.Length + 1 : pi.Length;
177 public bool HasParams {
179 return this.last_arg_is_params;
184 public class InternalParameters : ParameterData {
189 public readonly Parameters Parameters;
190 public readonly TypeParameter[] TypeParameters;
192 public InternalParameters (Type [] param_types, Parameters parameters)
194 this.param_types = param_types;
195 this.Parameters = parameters;
197 has_varargs = parameters.HasArglist;
199 if (param_types == null)
202 count = param_types.Length;
205 public InternalParameters (Type [] param_types, Parameters parameters,
206 TypeParameter [] type_params)
207 : this (param_types, parameters)
209 this.TypeParameters = type_params;
214 return has_varargs ? count + 1 : count;
218 public bool HasParams {
220 return Parameters.ArrayParameter != null;
224 Parameter GetParameter (int pos)
226 Parameter [] fixed_pars = Parameters.FixedParameters;
227 if (fixed_pars != null){
228 int len = fixed_pars.Length;
230 return Parameters.FixedParameters [pos];
233 return Parameters.ArrayParameter;
236 public string GetSignatureForError ()
238 StringBuilder sb = new StringBuilder ("(");
239 for (int i = 0; i < count; ++i) {
242 sb.Append (ParameterDesc (i));
245 return sb.ToString ();
248 public Type ParameterType (int pos)
250 if (has_varargs && pos >= count)
251 return TypeManager.runtime_argument_handle_type;
253 if (param_types == null)
256 return GetParameter (pos).ExternalType ();
259 public GenericConstraints GenericConstraints (int pos)
261 if (TypeParameters == null)
264 return TypeParameters [pos].Constraints;
267 public string ParameterName (int pos)
269 if (has_varargs && pos >= count)
272 return GetParameter (pos).Name;
275 public string ParameterDesc (int pos)
277 if (has_varargs && pos >= count)
280 Type t = ParameterType (pos);
281 return (ModifierDesc (pos) + " " + TypeManager.CSharpName (t).Replace ("&", "")).TrimStart ();
284 public string ModifierDesc (int pos)
286 Parameter p = GetParameter (pos);
289 // We need to and for REF/OUT, because if either is set the
290 // extra flag ISBYREF will be set as well
292 if ((p.ModFlags & Parameter.Modifier.REF) != 0)
294 if ((p.ModFlags & Parameter.Modifier.OUT) != 0)
296 if (p.ModFlags == Parameter.Modifier.PARAMS)
302 public Parameter.Modifier ParameterModifier (int pos)
304 if (has_varargs && pos >= count)
305 return Parameter.Modifier.ARGLIST;
307 Parameter.Modifier mod = GetParameter (pos).ModFlags;
309 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
310 mod |= Parameter.Modifier.ISBYREF;
317 public class ReflectionConstraints : GenericConstraints
319 GenericParameterAttributes attrs;
321 Type class_constraint;
322 Type[] iface_constraints;
325 public ReflectionConstraints (Type t)
328 Type[] constraints = t.GetGenericParameterConstraints ();
329 if ((constraints.Length > 0) && !constraints [0].IsInterface) {
330 class_constraint = constraints [0];
331 iface_constraints = new Type [constraints.Length - 1];
332 Array.Copy (constraints, 1, iface_constraints, 0, constraints.Length - 1);
334 iface_constraints = constraints;
335 attrs = t.GenericParameterAttributes;
337 if (HasValueTypeConstraint)
338 base_type = TypeManager.value_type;
339 else if (class_constraint != null)
340 base_type = class_constraint;
342 base_type = TypeManager.object_type;
345 public override string TypeParameter {
349 public override GenericParameterAttributes Attributes {
350 get { return attrs; }
353 public override Type ClassConstraint {
354 get { return class_constraint; }
357 public override Type EffectiveBaseClass {
358 get { return base_type; }
361 public override Type[] InterfaceConstraints {
362 get { return iface_constraints; }
366 class PtrHashtable : Hashtable {
367 sealed class PtrComparer : IComparer {
368 private PtrComparer () {}
370 public static PtrComparer Instance = new PtrComparer ();
372 public int Compare (object x, object y)
381 public PtrHashtable ()
383 comparer = PtrComparer.Instance;
388 * Hashtable whose keys are character arrays with the same length
390 class CharArrayHashtable : Hashtable {
391 sealed class ArrComparer : IComparer {
394 public ArrComparer (int len) {
398 public int Compare (object x, object y)
400 char[] a = (char[])x;
401 char[] b = (char[])y;
403 for (int i = 0; i < len; ++i)
412 protected override int GetHash (Object key)
414 char[] arr = (char[])key;
417 for (int i = 0; i < len; ++i)
418 h = (h << 5) - h + arr [i];
423 public CharArrayHashtable (int len)
426 comparer = new ArrComparer (len);
432 public object Second;
434 public Pair (object f, object s)
442 /// This is a wrapper around StreamReader which is seekable backwards
443 /// within a window of around 2048 chars.
445 public class SeekableStreamReader
447 public SeekableStreamReader (StreamReader reader)
449 this.reader = reader;
450 this.buffer = new char [AverageReadLength * 3];
452 // Let the StreamWriter autodetect the encoder
456 public SeekableStreamReader (Stream stream, Encoding encoding)
457 : this (new StreamReader (stream, encoding, true))
462 private const int AverageReadLength = 1024;
465 int buffer_start; // in chars
466 int char_count; // count buffer[] valid characters
467 int pos; // index into buffer[]
470 /// This value corresponds to the current position in a stream of characters.
471 /// The StreamReader hides its manipulation of the underlying byte stream and all
472 /// character set/decoding issues. Thus, we cannot use this position to guess at
473 /// the corresponding position in the underlying byte stream even though there is
474 /// a correlation between them.
476 public int Position {
477 get { return buffer_start + pos; }
480 if (value < buffer_start || value > buffer_start + char_count)
481 throw new InternalErrorException ("can't seek that far back: " + (pos - value));
482 pos = value - buffer_start;
486 private bool ReadBuffer ()
488 int slack = buffer.Length - char_count;
489 if (slack <= AverageReadLength / 2) {
490 // shift the buffer to make room for AverageReadLength number of characters
491 int shift = AverageReadLength - slack;
492 Array.Copy (buffer, shift, buffer, 0, char_count - shift);
495 buffer_start += shift;
496 slack += shift; // slack == AverageReadLength
499 int chars_read = reader.Read (buffer, char_count, slack);
500 char_count += chars_read;
502 return pos < char_count;
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;