2008-08-20 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / support.cs
1 //
2 // support.cs: Support routines to work around the fact that System.Reflection.Emit
3 // can not introspect types that are being constructed
4 //
5 // Author:
6 //   Miguel de Icaza (miguel@ximian.com)
7 //
8 // Copyright 2001 Ximian, Inc (http://www.ximian.com)
9 // Copyright 2003-2008 Novell, Inc
10 //
11
12 using System;
13 using System.IO;
14 using System.Text;
15 using System.Reflection;
16 using System.Collections;
17 using System.Reflection.Emit;
18 using System.Globalization;
19
20 namespace Mono.CSharp {
21
22 #if GMCS_SOURCE
23         public class ReflectionConstraints : GenericConstraints
24         {
25                 GenericParameterAttributes attrs;
26                 Type base_type;
27                 Type class_constraint;
28                 Type[] iface_constraints;
29                 string name;
30
31                 public static GenericConstraints GetConstraints (Type t)
32                 {
33                         Type [] constraints = t.GetGenericParameterConstraints ();
34                         GenericParameterAttributes attrs = t.GenericParameterAttributes;
35                         if (constraints.Length == 0 && attrs == GenericParameterAttributes.None)
36                                 return null;
37                         return new ReflectionConstraints (t.Name, constraints, attrs);
38                 }
39
40                 private ReflectionConstraints (string name, Type [] constraints, GenericParameterAttributes attrs)
41                 {
42                         this.name = name;
43                         this.attrs = attrs;
44
45                         if ((constraints.Length > 0) && !constraints [0].IsInterface) {
46                                 class_constraint = constraints [0];
47                                 iface_constraints = new Type [constraints.Length - 1];
48                                 Array.Copy (constraints, 1, iface_constraints, 0, constraints.Length - 1);
49                         } else
50                                 iface_constraints = constraints;
51
52                         if (HasValueTypeConstraint)
53                                 base_type = TypeManager.value_type;
54                         else if (class_constraint != null)
55                                 base_type = class_constraint;
56                         else
57                                 base_type = TypeManager.object_type;
58                 }
59
60                 public override string TypeParameter {
61                         get { return name; }
62                 }
63
64                 public override GenericParameterAttributes Attributes {
65                         get { return attrs; }
66                 }
67
68                 public override Type ClassConstraint {
69                         get { return class_constraint; }
70                 }
71
72                 public override Type EffectiveBaseClass {
73                         get { return base_type; }
74                 }
75
76                 public override Type[] InterfaceConstraints {
77                         get { return iface_constraints; }
78                 }
79         }
80 #endif
81
82         class PtrHashtable : Hashtable {
83                 sealed class PtrComparer : IComparer {
84                         private PtrComparer () {}
85
86                         public static PtrComparer Instance = new PtrComparer ();
87
88                         public int Compare (object x, object y)
89                         {
90                                 if (x == y)
91                                         return 0;
92                                 else
93                                         return 1;
94                         }
95                 }
96
97                 public PtrHashtable ()
98                 {
99                         comparer = PtrComparer.Instance;
100                 }
101
102 #if MS_COMPATIBLE
103                 //
104                 // Workaround System.InvalidOperationException for enums
105                 //
106                 protected override int GetHash (object key)
107                 {
108                         TypeBuilder tb = key as TypeBuilder;
109                         if (tb != null && tb.BaseType == TypeManager.enum_type)
110                                 key = tb.BaseType;
111
112                         return base.GetHash (key);
113                 }
114 #endif
115         }
116
117         /*
118          * Hashtable whose keys are character arrays with the same length
119          */
120         class CharArrayHashtable : Hashtable {
121                 sealed class ArrComparer : IComparer {
122                         private int len;
123
124                         public ArrComparer (int len) {
125                                 this.len = len;
126                         }
127
128                         public int Compare (object x, object y)
129                         {
130                                 char[] a = (char[])x;
131                                 char[] b = (char[])y;
132
133                                 for (int i = 0; i < len; ++i)
134                                         if (a [i] != b [i])
135                                                 return 1;
136                                 return 0;
137                         }
138                 }
139
140                 private int len;
141
142                 protected override int GetHash (Object key)
143                 {
144                         char[] arr = (char[])key;
145                         int h = 0;
146
147                         for (int i = 0; i < len; ++i)
148                                 h = (h << 5) - h + arr [i];
149
150                         return h;
151                 }
152
153                 public CharArrayHashtable (int len)
154                 {
155                         this.len = len;
156                         comparer = new ArrComparer (len);
157                 }
158         }
159
160         struct Pair {
161                 public object First;
162                 public object Second;
163
164                 public Pair (object f, object s)
165                 {
166                         First = f;
167                         Second = s;
168                 }
169         }
170
171         public class Accessors {
172                 public Accessor get_or_add;
173                 public Accessor set_or_remove;
174
175                 // was 'set' declared before 'get'?  was 'remove' declared before 'add'?
176                 public bool declared_in_reverse;
177
178                 public Accessors (Accessor get_or_add, Accessor set_or_remove)
179                 {
180                         this.get_or_add = get_or_add;
181                         this.set_or_remove = set_or_remove;
182                 }
183         }
184
185         /// <summary>
186         ///   This is a wrapper around StreamReader which is seekable backwards
187         ///   within a window of around 2048 chars.
188         /// </summary>
189         public class SeekableStreamReader
190         {
191                 const int AverageReadLength = 1024;
192                 TextReader reader;
193                 Stream stream;
194                 Encoding encoding;
195
196                 char[] buffer;
197                 int buffer_start;       // in chars
198                 int char_count;         // count buffer[] valid characters
199                 int pos;                // index into buffer[]
200
201                 public SeekableStreamReader (Stream stream, Encoding encoding)
202                 {
203                         this.stream = stream;
204                         this.encoding = encoding;
205                         
206                         this.reader = new StreamReader (stream, encoding, true);
207                         this.buffer = new char [AverageReadLength * 3];
208
209                         // Let the StreamWriter autodetect the encoder
210                         reader.Peek ();
211                 }
212
213                 /// <remarks>
214                 ///   This value corresponds to the current position in a stream of characters.
215                 ///   The StreamReader hides its manipulation of the underlying byte stream and all
216                 ///   character set/decoding issues.  Thus, we cannot use this position to guess at
217                 ///   the corresponding position in the underlying byte stream even though there is
218                 ///   a correlation between them.
219                 /// </remarks>
220                 public int Position {
221                         get { return buffer_start + pos; }
222
223                         set {
224                                 if (value > buffer_start + char_count)
225                                         throw new InternalErrorException ("can't seek that far forward: " + (pos - value));
226                                 
227                                 if (value < buffer_start){
228                                         // Reinitialize.
229                                         stream.Position = 0;
230                                         reader = new StreamReader (stream, encoding, true);
231                                         buffer_start = 0;
232                                         char_count = 0;
233                                         pos = 0;
234                                         Peek ();
235
236                                         while (value > buffer_start + char_count){
237                                                 pos = char_count+1;
238                                                 Peek ();
239                                         }
240                                         pos = value - buffer_start;
241                                 }
242
243                                 pos = value - buffer_start;
244                         }
245                 }
246
247                 private bool ReadBuffer ()
248                 {
249                         int slack = buffer.Length - char_count;
250                         if (slack <= AverageReadLength / 2) {
251                                 // shift the buffer to make room for AverageReadLength number of characters
252                                 int shift = AverageReadLength - slack;
253                                 Array.Copy (buffer, shift, buffer, 0, char_count - shift);
254                                 pos -= shift;
255                                 char_count -= shift;
256                                 buffer_start += shift;
257                                 slack += shift;         // slack == AverageReadLength
258                         }
259
260                         int chars_read = reader.Read (buffer, char_count, slack);
261                         char_count += chars_read;
262
263                         return pos < char_count;
264                 }
265
266                 public int Peek ()
267                 {
268                         if ((pos >= char_count) && !ReadBuffer ())
269                                 return -1;
270
271                         return buffer [pos];
272                 }
273
274                 public int Read ()
275                 {
276                         if ((pos >= char_count) && !ReadBuffer ())
277                                 return -1;
278
279                         return buffer [pos++];
280                 }
281         }
282
283         public class DoubleHash {
284                 const int DEFAULT_INITIAL_BUCKETS = 100;
285
286                 public DoubleHash () : this (DEFAULT_INITIAL_BUCKETS) {}
287
288                 public DoubleHash (int size)
289                 {
290                         count = size;
291                         buckets = new Entry [size];
292                 }
293
294                 int count;
295                 Entry [] buckets;
296                 int size = 0;
297
298                 class Entry {
299                         public object key1;
300                         public object key2;
301                         public int hash;
302                         public object value;
303                         public Entry next;
304
305                         public Entry (object key1, object key2, int hash, object value, Entry next)
306                         {
307                                 this.key1 = key1;
308                                 this.key2 = key2;
309                                 this.hash = hash;
310                                 this.next = next;
311                                 this.value = value;
312                         }
313                 }
314
315                 public bool Lookup (object a, object b, out object res)
316                 {
317                         int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
318
319                         for (Entry e = buckets [h % count]; e != null; e = e.next) {
320                                 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b)) {
321                                         res = e.value;
322                                         return true;
323                                 }
324                         }
325                         res = null;
326                         return false;
327                 }
328
329                 public void Insert (object a, object b, object value)
330                 {
331                         // Is it an existing one?
332
333                         int h = (a.GetHashCode () ^ b.GetHashCode ()) & 0x7FFFFFFF;
334
335                         for (Entry e = buckets [h % count]; e != null; e = e.next) {
336                                 if (e.hash == h && e.key1.Equals (a) && e.key2.Equals (b))
337                                         e.value = value;
338                         }
339
340                         int bucket = h % count;
341                         buckets [bucket] = new Entry (a, b, h, value, buckets [bucket]);
342
343                         // Grow whenever we double in size
344                         if (size++ == count) {
345                                 count <<= 1;
346                                 count ++;
347
348                                 Entry [] newBuckets = new Entry [count];
349                                 foreach (Entry root in buckets) {
350                                         Entry e = root;
351                                         while (e != null) {
352                                                 int newLoc = e.hash % count;
353                                                 Entry n = e.next;
354                                                 e.next = newBuckets [newLoc];
355                                                 newBuckets [newLoc] = e;
356                                                 e = n;
357                                         }
358                                 }
359
360                                 buckets = newBuckets;
361                         }
362                 }
363         }
364
365         class PartialMethodDefinitionInfo : MethodInfo
366         {
367                 MethodOrOperator mc;
368                 MethodAttributes attrs;
369
370                 public PartialMethodDefinitionInfo (MethodOrOperator mc)
371                 {
372                         this.mc = mc;
373                         if ((mc.ModFlags & Modifiers.STATIC) != 0)
374                                 attrs = MethodAttributes.Static;
375                 }
376
377                 public override MethodInfo GetBaseDefinition ()
378                 {
379                         throw new NotImplementedException ();
380                 }
381
382                 public override ICustomAttributeProvider ReturnTypeCustomAttributes
383                 {
384                         get { throw new NotImplementedException (); }
385                 }
386
387                 public override MethodAttributes Attributes
388                 {
389                         get { return attrs; }
390                 }
391
392                 public override MethodImplAttributes GetMethodImplementationFlags ()
393                 {
394                         throw new NotImplementedException ();
395                 }
396
397                 public override ParameterInfo [] GetParameters ()
398                 {
399                         throw new NotImplementedException ();
400                 }
401
402                 public override object Invoke (object obj, BindingFlags invokeAttr, Binder binder, object [] parameters, CultureInfo culture)
403                 {
404                         throw new NotImplementedException ();
405                 }
406
407                 public override RuntimeMethodHandle MethodHandle
408                 {
409                         get { throw new NotImplementedException (); }
410                 }
411
412                 public override Type DeclaringType
413                 {
414                         get { return mc.Parent.TypeBuilder; }
415                 }
416
417                 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
418                 {
419                         throw new NotImplementedException ();
420                 }
421
422                 public override object [] GetCustomAttributes (bool inherit)
423                 {
424                         throw new NotImplementedException ();
425                 }
426
427                 public override Type ReturnType {
428                         get {
429                                 return mc.MemberType;
430                         }
431                 }
432
433                 public override bool IsDefined (Type attributeType, bool inherit)
434                 {
435                         throw new NotImplementedException ();
436                 }
437
438                 public override string Name
439                 {
440                         get { return mc.Name; }
441                 }
442
443                 public override Type ReflectedType
444                 {
445                         get { throw new NotImplementedException (); }
446                 }
447         }
448
449 }