In mcs:
[mono.git] / mcs / mcs / decl.cs
1 //
2 // decl.cs: Declaration base class for structs, classes, enums and interfaces.
3 //
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 //         Marek Safar (marek.safar@seznam.cz)
6 //
7 // Licensed under the terms of the GNU GPL
8 //
9 // (C) 2001 Ximian, Inc (http://www.ximian.com)
10 // (C) 2004 Novell, Inc
11 //
12 // TODO: Move the method verification stuff from the class.cs and interface.cs here
13 //
14
15 using System;
16 using System.Collections;
17 using System.Globalization;
18 using System.Reflection.Emit;
19 using System.Reflection;
20
21 #if BOOTSTRAP_WITH_OLDLIB
22 using XmlElement = System.Object;
23 #else
24 using System.Xml;
25 #endif
26
27 namespace Mono.CSharp {
28
29         public class MemberName {
30                 public string Name;
31                 public readonly MemberName Left;
32
33                 public static readonly MemberName Null = new MemberName ("");
34
35                 public MemberName (string name)
36                 {
37                         this.Name = name;
38                 }
39
40                 public MemberName (MemberName left, string name)
41                         : this (name)
42                 {
43                         this.Left = left;
44                 }
45
46                 public MemberName (MemberName left, MemberName right)
47                         : this (left, right.Name)
48                 {
49                 }
50
51                 public string GetName ()
52                 {
53                         return GetName (false);
54                 }
55
56                 public string GetName (bool is_generic)
57                 {
58                         string name = is_generic ? Basename : Name;
59                         if (Left != null)
60                                 return Left.GetName (is_generic) + "." + name;
61                         else
62                                 return name;
63                 }
64
65                 ///
66                 /// This returns exclusively the name as seen on the source code
67                 /// it is not the fully qualifed type after resolution
68                 ///
69                 public string GetPartialName ()
70                 {
71                         if (Left != null)
72                                 return Left.GetPartialName () + "." + Name;
73                         else
74                                 return Name;
75                 }
76
77                 public string GetTypeName ()
78                 {
79                         if (Left != null)
80                                 return Left.GetTypeName () + "." + Name;
81                         else
82                                 return Name;
83                 }
84
85                 public Expression GetTypeExpression (Location loc)
86                 {
87                         if (Left != null) {
88                                 Expression lexpr = Left.GetTypeExpression (loc);
89
90                                 return new MemberAccess (lexpr, Name, loc);
91                         } else {
92                                 return new SimpleName (Name, loc);
93                         }
94                 }
95
96                 public MemberName Clone ()
97                 {
98                         if (Left != null)
99                                 return new MemberName (Left.Clone (), Name);
100                         else
101                                 return new MemberName (Name);
102                 }
103
104                 public string Basename {
105                         get {
106                                 return Name;
107                         }
108                 }
109
110                 public override string ToString ()
111                 {
112                         if (Left != null)
113                                 return Left + "." + Name;
114                         else
115                                 return Name;
116                 }
117         }
118
119         /// <summary>
120         ///   Base representation for members.  This is used to keep track
121         ///   of Name, Location and Modifier flags, and handling Attributes.
122         /// </summary>
123         public abstract class MemberCore : Attributable {
124                 /// <summary>
125                 ///   Public name
126                 /// </summary>
127                 public string Name {
128                         get {
129                                 // !(this is GenericMethod) && !(this is Method)
130                                 return MemberName.GetName (false);
131                         }
132                 }
133
134                 // Is not readonly because of IndexerName attribute
135                 public MemberName MemberName;
136
137                 /// <summary>
138                 ///   Modifier flags that the user specified in the source code
139                 /// </summary>
140                 public int ModFlags;
141
142                 public /*readonly*/ TypeContainer Parent;
143
144                 /// <summary>
145                 ///   Location where this declaration happens
146                 /// </summary>
147                 public readonly Location Location;
148
149                 /// <summary>
150                 ///   XML documentation comment
151                 /// </summary>
152                 public string DocComment;
153
154                 /// <summary>
155                 ///   Represents header string for documentation comment 
156                 ///   for each member types.
157                 /// </summary>
158                 public abstract string DocCommentHeader { get; }
159
160                 [Flags]
161                 public enum Flags {
162                         Obsolete_Undetected = 1,                // Obsolete attribute has not been detected yet
163                         Obsolete = 1 << 1,                      // Type has obsolete attribute
164                         ClsCompliance_Undetected = 1 << 2,      // CLS Compliance has not been detected yet
165                         ClsCompliant = 1 << 3,                  // Type is CLS Compliant
166                         CloseTypeCreated = 1 << 4,              // Tracks whether we have Closed the type
167                         HasCompliantAttribute_Undetected = 1 << 5,      // Presence of CLSCompliantAttribute has not been detected
168                         HasClsCompliantAttribute = 1 << 6,                      // Type has CLSCompliantAttribute
169                         ClsCompliantAttributeTrue = 1 << 7,                     // Type has CLSCompliant (true)
170                         Excluded_Undetected = 1 << 8,           // Conditional attribute has not been detected yet
171                         Excluded = 1 << 9,                                      // Method is conditional
172                         TestMethodDuplication = 1 << 10         // Test for duplication must be performed
173                 }
174
175                 /// <summary>
176                 ///   MemberCore flags at first detected then cached
177                 /// </summary>
178                 internal Flags caching_flags;
179
180                 public MemberCore (TypeContainer parent, MemberName name, Attributes attrs,
181                                    Location loc)
182                         : base (attrs)
183                 {
184                         Parent = parent;
185                         MemberName = name;
186                         Location = loc;
187                         caching_flags = Flags.Obsolete_Undetected | Flags.ClsCompliance_Undetected | Flags.HasCompliantAttribute_Undetected | Flags.Excluded_Undetected;
188                 }
189
190                 /// <summary>
191                 /// Tests presence of ObsoleteAttribute and report proper error
192                 /// </summary>
193                 protected void CheckUsageOfObsoleteAttribute (Type type)
194                 {
195                         if (type == null)
196                                 return;
197
198                         ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (type);
199                         if (obsolete_attr == null)
200                                 return;
201
202                         AttributeTester.Report_ObsoleteMessage (obsolete_attr, type.FullName, Location);
203                 }
204
205                 public abstract bool Define ();
206
207                 // 
208                 // Returns full member name for error message
209                 //
210                 public virtual string GetSignatureForError ()
211                 {
212                         return Name;
213                 }
214
215                 /// <summary>
216                 /// Use this method when MethodBuilder is null
217                 /// </summary>
218                 public virtual string GetSignatureForError (TypeContainer tc)
219                 {
220                         return Name;
221                 }
222
223                 /// <summary>
224                 /// Base Emit method. This is also entry point for CLS-Compliant verification.
225                 /// </summary>
226                 public virtual void Emit ()
227                 {
228                         // Hack with Parent == null is for EnumMember
229                         if (Parent == null || (GetObsoleteAttribute (Parent) == null && Parent.GetObsoleteAttribute (Parent) == null))
230                                 VerifyObsoleteAttribute ();
231
232                         if (!RootContext.VerifyClsCompliance)
233                                 return;
234
235                         VerifyClsCompliance (Parent);
236                 }
237
238                 public bool InUnsafe {
239                         get {
240                                 return ((ModFlags & Modifiers.UNSAFE) != 0) || Parent.UnsafeContext;
241                         }
242                 }
243
244                 // 
245                 // Whehter is it ok to use an unsafe pointer in this type container
246                 //
247                 public bool UnsafeOK (DeclSpace parent)
248                 {
249                         //
250                         // First check if this MemberCore modifier flags has unsafe set
251                         //
252                         if ((ModFlags & Modifiers.UNSAFE) != 0)
253                                 return true;
254
255                         if (parent.UnsafeContext)
256                                 return true;
257
258                         Expression.UnsafeError (Location);
259                         return false;
260                 }
261
262                 /// <summary>
263                 /// Returns instance of ObsoleteAttribute for this MemberCore
264                 /// </summary>
265                 public ObsoleteAttribute GetObsoleteAttribute (DeclSpace ds)
266                 {
267                         // ((flags & (Flags.Obsolete_Undetected | Flags.Obsolete)) == 0) is slower, but why ?
268                         if ((caching_flags & Flags.Obsolete_Undetected) == 0 && (caching_flags & Flags.Obsolete) == 0) {
269                                 return null;
270                         }
271
272                         caching_flags &= ~Flags.Obsolete_Undetected;
273
274                         if (OptAttributes == null)
275                                 return null;
276
277                         Attribute obsolete_attr = OptAttributes.Search (
278                                 TypeManager.obsolete_attribute_type, ds.EmitContext);
279                         if (obsolete_attr == null)
280                                 return null;
281
282                         ObsoleteAttribute obsolete = obsolete_attr.GetObsoleteAttribute (ds.EmitContext);
283                         if (obsolete == null)
284                                 return null;
285
286                         caching_flags |= Flags.Obsolete;
287                         return obsolete;
288                 }
289
290                 /// <summary>
291                 /// Analyze whether CLS-Compliant verification must be execute for this MemberCore.
292                 /// </summary>
293                 public override bool IsClsCompliaceRequired (DeclSpace container)
294                 {
295                         if ((caching_flags & Flags.ClsCompliance_Undetected) == 0)
296                                 return (caching_flags & Flags.ClsCompliant) != 0;
297
298                         if (GetClsCompliantAttributeValue (container) && IsExposedFromAssembly (container)) {
299                                 caching_flags &= ~Flags.ClsCompliance_Undetected;
300                                 caching_flags |= Flags.ClsCompliant;
301                                 return true;
302                         }
303
304                         caching_flags &= ~Flags.ClsCompliance_Undetected;
305                         return false;
306                 }
307
308                 /// <summary>
309                 /// Returns true when MemberCore is exposed from assembly.
310                 /// </summary>
311                 public bool IsExposedFromAssembly (DeclSpace ds)
312                 {
313                         if ((ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
314                                 return false;
315                         
316                         DeclSpace parentContainer = ds;
317                         while (parentContainer != null && parentContainer.ModFlags != 0) {
318                                 if ((parentContainer.ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
319                                         return false;
320                                 parentContainer = parentContainer.Parent;
321                         }
322                         return true;
323                 }
324
325                 /// <summary>
326                 /// Resolve CLSCompliantAttribute value or gets cached value.
327                 /// </summary>
328                 bool GetClsCompliantAttributeValue (DeclSpace ds)
329                 {
330                         if (OptAttributes != null) {
331                                 Attribute cls_attribute = OptAttributes.Search (
332                                         TypeManager.cls_compliant_attribute_type, ds.EmitContext);
333                                 if (cls_attribute != null) {
334                                         caching_flags |= Flags.HasClsCompliantAttribute;
335                                         return cls_attribute.GetClsCompliantAttributeValue (ds.EmitContext);
336                                 }
337                         }
338                         return ds.GetClsCompliantAttributeValue ();
339                 }
340
341                 /// <summary>
342                 /// Returns true if MemberCore is explicitly marked with CLSCompliantAttribute
343                 /// </summary>
344                 protected bool HasClsCompliantAttribute {
345                         get {
346                                 return (caching_flags & Flags.HasClsCompliantAttribute) != 0;
347                         }
348                 }
349
350                 /// <summary>
351                 /// It helps to handle error 102 & 111 detection
352                 /// </summary>
353                 public virtual bool MarkForDuplicationCheck ()
354                 {
355                         return false;
356                 }
357
358                 /// <summary>
359                 /// The main virtual method for CLS-Compliant verifications.
360                 /// The method returns true if member is CLS-Compliant and false if member is not
361                 /// CLS-Compliant which means that CLS-Compliant tests are not necessary. A descendants override it
362                 /// and add their extra verifications.
363                 /// </summary>
364                 protected virtual bool VerifyClsCompliance (DeclSpace ds)
365                 {
366                         if (!IsClsCompliaceRequired (ds)) {
367                                 if (HasClsCompliantAttribute && RootContext.WarningLevel >= 2) {
368                                         if (!IsExposedFromAssembly (ds))
369                                                 Report.Warning (3019, Location, "CLS compliance checking will not be performed on '{0}' because it is private or internal", GetSignatureForError ());
370                                         if (!CodeGen.Assembly.IsClsCompliant)
371                                                 Report.Warning (3021, Location, "'{0}' does not need a CLSCompliant attribute because the assembly does not have a CLSCompliant attribute", GetSignatureForError ());
372                                 }
373                                 return false;
374                         }
375
376                         if (!CodeGen.Assembly.IsClsCompliant) {
377                                 if (HasClsCompliantAttribute) {
378                                         Report.Error (3014, Location, "'{0}' cannot be marked as CLS-compliant because the assembly does not have a CLSCompliant attribute", GetSignatureForError ());
379                                 }
380                                 return false;
381                         }
382
383                         int index = Name.LastIndexOf ('.');
384                         if (Name [index > 0 ? index + 1 : 0] == '_') {
385                                 Report.Error (3008, Location, "Identifier '{0}' is not CLS-compliant", GetSignatureForError () );
386                         }
387                         return true;
388                 }
389
390                 protected abstract void VerifyObsoleteAttribute ();
391
392                 //
393                 // Raised (and passed an XmlElement that contains the comment)
394                 // when GenerateDocComment is writing documentation expectedly.
395                 //
396                 internal virtual void OnGenerateDocComment (DeclSpace ds, XmlElement intermediateNode)
397                 {
398                 }
399
400                 //
401                 // Returns a string that represents the signature for this 
402                 // member which should be used in XML documentation.
403                 //
404                 public virtual string GetDocCommentName (DeclSpace ds)
405                 {
406                         if (ds == null || this is DeclSpace)
407                                 return DocCommentHeader + Name;
408                         else
409                                 return String.Concat (DocCommentHeader, ds.Name, ".", Name);
410                 }
411
412                 //
413                 // Generates xml doc comments (if any), and if required,
414                 // handle warning report.
415                 //
416                 internal virtual void GenerateDocComment (DeclSpace ds)
417                 {
418                         DocUtil.GenerateDocComment (this, ds);
419                 }
420         }
421
422         /// <summary>
423         ///   Base class for structs, classes, enumerations and interfaces.  
424         /// </summary>
425         /// <remarks>
426         ///   They all create new declaration spaces.  This
427         ///   provides the common foundation for managing those name
428         ///   spaces.
429         /// </remarks>
430         public abstract class DeclSpace : MemberCore, IAlias {
431                 /// <summary>
432                 ///   This points to the actual definition that is being
433                 ///   created with System.Reflection.Emit
434                 /// </summary>
435                 public TypeBuilder TypeBuilder;
436
437                 //
438                 // This is the namespace in which this typecontainer
439                 // was declared.  We use this to resolve names.
440                 //
441                 public NamespaceEntry NamespaceEntry;
442
443                 private Hashtable Cache = new Hashtable ();
444                 
445                 public string Basename;
446                 
447                 protected Hashtable defined_names;
448
449                 // The emit context for toplevel objects.
450                 protected EmitContext ec;
451                 
452                 public EmitContext EmitContext {
453                         get { return ec; }
454                 }
455
456                 static string[] attribute_targets = new string [] { "type" };
457
458                 public DeclSpace (NamespaceEntry ns, TypeContainer parent, MemberName name,
459                                   Attributes attrs, Location l)
460                         : base (parent, name, attrs, l)
461                 {
462                         NamespaceEntry = ns;
463                         Basename = name.Name;
464                         defined_names = new Hashtable ();
465                 }
466
467                 /// <summary>
468                 /// Adds the member to defined_names table. It tests for duplications and enclosing name conflicts
469                 /// </summary>
470                 protected bool AddToContainer (MemberCore symbol, string fullname, string basename)
471                 {
472                         if (basename == Basename && !(this is Interface)) {
473                                 Report.SymbolRelatedToPreviousError (this);
474                                 Report.Error (542,  symbol.Location, "'{0}': member names cannot be the same as their enclosing type", symbol.GetSignatureForError ());
475                                 return false;
476                         }
477
478                         MemberCore mc = (MemberCore)defined_names [fullname];
479
480                         if (mc == null) {
481                                 defined_names.Add (fullname, symbol);
482                                 return true;
483                         }
484
485                         if (symbol.MarkForDuplicationCheck () && mc.MarkForDuplicationCheck ())
486                                 return true;
487
488                         Report.SymbolRelatedToPreviousError (mc);
489                         Report.Error (102, symbol.Location, "The type '{0}' already contains a definition for '{1}'", GetSignatureForError (), basename);
490                         return false;
491                 }
492
493                 public void RecordDecl ()
494                 {
495                         if ((NamespaceEntry != null) && (Parent == RootContext.Tree.Types))
496                                 NamespaceEntry.DefineName (MemberName.Basename, this);
497                 }
498
499                 /// <summary>
500                 ///   Returns the MemberCore associated with a given name in the declaration
501                 ///   space. It doesn't return method based symbols !!
502                 /// </summary>
503                 /// 
504                 public MemberCore GetDefinition (string name)
505                 {
506                         return (MemberCore)defined_names [name];
507                 }
508                 
509                 bool in_transit = false;
510                 
511                 /// <summary>
512                 ///   This function is used to catch recursive definitions
513                 ///   in declarations.
514                 /// </summary>
515                 public bool InTransit {
516                         get {
517                                 return in_transit;
518                         }
519
520                         set {
521                                 in_transit = value;
522                         }
523                 }
524                 
525                 // 
526                 // root_types contains all the types.  All TopLevel types
527                 // hence have a parent that points to `root_types', that is
528                 // why there is a non-obvious test down here.
529                 //
530                 public bool IsTopLevel {
531                         get {
532                                 if (Parent != null){
533                                         if (Parent.Parent == null)
534                                                 return true;
535                                 }
536                                 return false;
537                         }
538                 }
539
540                 public virtual void CloseType ()
541                 {
542                         if ((caching_flags & Flags.CloseTypeCreated) == 0){
543                                 try {
544                                         TypeBuilder.CreateType ();
545                                 } catch {
546                                         //
547                                         // The try/catch is needed because
548                                         // nested enumerations fail to load when they
549                                         // are defined.
550                                         //
551                                         // Even if this is the right order (enumerations
552                                         // declared after types).
553                                         //
554                                         // Note that this still creates the type and
555                                         // it is possible to save it
556                                 }
557                                 caching_flags |= Flags.CloseTypeCreated;
558                         }
559                 }
560
561                 /// <remarks>
562                 ///  Should be overriten by the appropriate declaration space
563                 /// </remarks>
564                 public abstract TypeBuilder DefineType ();
565                 
566                 /// <summary>
567                 ///   Define all members, but don't apply any attributes or do anything which may
568                 ///   access not-yet-defined classes.  This method also creates the MemberCache.
569                 /// </summary>
570                 public abstract bool DefineMembers (TypeContainer parent);
571
572                 //
573                 // Whether this is an `unsafe context'
574                 //
575                 public bool UnsafeContext {
576                         get {
577                                 if ((ModFlags & Modifiers.UNSAFE) != 0)
578                                         return true;
579                                 if (Parent != null)
580                                         return Parent.UnsafeContext;
581                                 return false;
582                         }
583                 }
584
585                 public static string MakeFQN (string nsn, string name)
586                 {
587                         if (nsn == "")
588                                 return name;
589                         return String.Concat (nsn, ".", name);
590                 }
591
592                 EmitContext type_resolve_ec;
593
594                 // <summary>
595                 //    Resolves the expression `e' for a type, and will recursively define
596                 //    types.  This should only be used for resolving base types.
597                 // </summary>
598                 public TypeExpr ResolveBaseTypeExpr (Expression e, bool silent, Location loc)
599                 {
600                         if (type_resolve_ec == null) {
601                                 // FIXME: I think this should really be one of:
602                                 //
603                                 // a. type_resolve_ec = Parent.EmitContext;
604                                 // b. type_resolve_ec = new EmitContext (Parent, Parent, loc, null, null, ModFlags, false);
605                                 //
606                                 // However, if Parent == RootContext.Tree.Types, its NamespaceEntry will be null.
607                                 //
608                                 type_resolve_ec = new EmitContext (Parent, this, loc, null, null, ModFlags, false);
609                                 type_resolve_ec.ResolvingTypeTree = true;
610                         }
611                         type_resolve_ec.loc = loc;
612                         type_resolve_ec.ContainerType = TypeBuilder;
613
614                         return e.ResolveAsTypeTerminal (type_resolve_ec, silent);
615                 }
616                 
617                 public bool CheckAccessLevel (Type check_type)
618                 {
619                         if (check_type == TypeBuilder)
620                                 return true;
621                         
622                         TypeAttributes check_attr = check_type.Attributes & TypeAttributes.VisibilityMask;
623
624                         //
625                         // Broken Microsoft runtime, return public for arrays, no matter what 
626                         // the accessibility is for their underlying class, and they return 
627                         // NonPublic visibility for pointers
628                         //
629                         if (check_type.IsArray || check_type.IsPointer)
630                                 return CheckAccessLevel (TypeManager.GetElementType (check_type));
631
632                         if (TypeBuilder == null)
633                                 // FIXME: TypeBuilder will be null when invoked by Class.GetNormalBases().
634                                 //        However, this is invoked again later -- so safe to return true.
635                                 //        May also be null when resolving top-level attributes.
636                                 return true;
637
638                         switch (check_attr){
639                         case TypeAttributes.Public:
640                                 return true;
641
642                         case TypeAttributes.NotPublic:
643                                 //
644                                 // This test should probably use the declaringtype.
645                                 //
646                                 return check_type.Assembly == TypeBuilder.Assembly;
647                                 
648                         case TypeAttributes.NestedPublic:
649                                 return true;
650
651                         case TypeAttributes.NestedPrivate:
652                                 return NestedAccessible (check_type);
653
654                         case TypeAttributes.NestedFamily:
655                                 return FamilyAccessible (check_type);
656
657                         case TypeAttributes.NestedFamANDAssem:
658                                 return (check_type.Assembly == TypeBuilder.Assembly) &&
659                                         FamilyAccessible (check_type);
660
661                         case TypeAttributes.NestedFamORAssem:
662                                 return (check_type.Assembly == TypeBuilder.Assembly) ||
663                                         FamilyAccessible (check_type);
664
665                         case TypeAttributes.NestedAssembly:
666                                 return check_type.Assembly == TypeBuilder.Assembly;
667                         }
668
669                         Console.WriteLine ("HERE: " + check_attr);
670                         return false;
671
672                 }
673
674                 protected bool NestedAccessible (Type check_type)
675                 {
676                         string check_type_name = check_type.FullName;
677
678                         // At this point, we already know check_type is a nested class.
679                         int cio = check_type_name.LastIndexOf ('+');
680
681                         // Ensure that the string 'container' has a '+' in it to avoid false matches
682                         string container = check_type_name.Substring (0, cio + 1);
683
684                         // Ensure that type_name ends with a '+' so that it can match 'container', if necessary
685                         string type_name = TypeBuilder.FullName + "+";
686
687                         // If the current class is nested inside the container of check_type,
688                         // we can access check_type even if it is private or protected.
689                         return type_name.StartsWith (container);
690                 }
691
692                 protected bool FamilyAccessible (Type check_type)
693                 {
694                         Type declaring = check_type.DeclaringType;
695                         if (TypeBuilder == declaring ||
696                             TypeBuilder.IsSubclassOf (declaring))
697                                 return true;
698
699                         return NestedAccessible (check_type);
700                 }
701
702                 // Access level of a type.
703                 const int X = 1;
704                 enum AccessLevel { // Each column represents `is this scope larger or equal to Blah scope'
705                                             // Public    Assembly   Protected
706                         Protected           = (0 << 0) | (0 << 1) | (X << 2),
707                         Public              = (X << 0) | (X << 1) | (X << 2),
708                         Private             = (0 << 0) | (0 << 1) | (0 << 2),
709                         Internal            = (0 << 0) | (X << 1) | (0 << 2),
710                         ProtectedOrInternal = (0 << 0) | (X << 1) | (X << 2),
711                 }
712                 
713                 static AccessLevel GetAccessLevelFromModifiers (int flags)
714                 {
715                         if ((flags & Modifiers.INTERNAL) != 0) {
716                                 
717                                 if ((flags & Modifiers.PROTECTED) != 0)
718                                         return AccessLevel.ProtectedOrInternal;
719                                 else
720                                         return AccessLevel.Internal;
721                                 
722                         } else if ((flags & Modifiers.PROTECTED) != 0)
723                                 return AccessLevel.Protected;
724                         
725                         else if ((flags & Modifiers.PRIVATE) != 0)
726                                 return AccessLevel.Private;
727                         
728                         else
729                                 return AccessLevel.Public;
730                 }
731
732                 // What is the effective access level of this?
733                 // TODO: Cache this?
734                 AccessLevel EffectiveAccessLevel {
735                         get {
736                                 AccessLevel myAccess = GetAccessLevelFromModifiers (ModFlags);
737                                 if (!IsTopLevel && (Parent != null))
738                                         return myAccess & Parent.EffectiveAccessLevel;
739                                 else
740                                         return myAccess;
741                         }
742                 }
743
744                 // Return the access level for type `t'
745                 static AccessLevel TypeEffectiveAccessLevel (Type t)
746                 {
747                         if (t.IsPublic)
748                                 return AccessLevel.Public;              
749                         if (t.IsNestedPrivate)
750                                 return AccessLevel.Private;
751                         if (t.IsNotPublic)
752                                 return AccessLevel.Internal;
753                         
754                         // By now, it must be nested
755                         AccessLevel parentLevel = TypeEffectiveAccessLevel (t.DeclaringType);
756                         
757                         if (t.IsNestedPublic)
758                                 return parentLevel;
759                         if (t.IsNestedAssembly)
760                                 return parentLevel & AccessLevel.Internal;
761                         if (t.IsNestedFamily)
762                                 return parentLevel & AccessLevel.Protected;
763                         if (t.IsNestedFamORAssem)
764                                 return parentLevel & AccessLevel.ProtectedOrInternal;
765                         if (t.IsNestedFamANDAssem)
766                                 throw new NotImplementedException ("NestedFamANDAssem not implemented, cant make this kind of type from c# anyways");
767                         
768                         // nested private is taken care of
769                         
770                         throw new Exception ("I give up, what are you?");
771                 }
772
773                 //
774                 // This answers `is the type P, as accessible as a member M which has the
775                 // accessability @flags which is declared as a nested member of the type T, this declspace'
776                 //
777                 public bool AsAccessible (Type p, int flags)
778                 {
779                         //
780                         // 1) if M is private, its accessability is the same as this declspace.
781                         // we already know that P is accessible to T before this method, so we
782                         // may return true.
783                         //
784                         
785                         if ((flags & Modifiers.PRIVATE) != 0)
786                                 return true;
787                         
788                         while (p.IsArray || p.IsPointer || p.IsByRef)
789                                 p = TypeManager.GetElementType (p);
790                         
791                         AccessLevel pAccess = TypeEffectiveAccessLevel (p);
792                         AccessLevel mAccess = this.EffectiveAccessLevel &
793                                 GetAccessLevelFromModifiers (flags);
794                         
795                         // for every place from which we can access M, we must
796                         // be able to access P as well. So, we want
797                         // For every bit in M and P, M_i -> P_1 == true
798                         // or, ~ (M -> P) == 0 <-> ~ ( ~M | P) == 0
799                         
800                         return ~ (~ mAccess | pAccess) == 0;
801                 }
802                 
803                 static DoubleHash dh = new DoubleHash (1000);
804
805                 Type DefineTypeAndParents (DeclSpace tc)
806                 {
807                         DeclSpace container = tc.Parent;
808
809                         if (container.TypeBuilder == null && container.Name != "")
810                                 DefineTypeAndParents (container);
811
812                         return tc.DefineType ();
813                 }
814                 
815                 FullNamedExpression LookupInterfaceOrClass (string ns, string name, out bool error)
816                 {
817                         DeclSpace parent;
818                         FullNamedExpression result;
819                         Type t;
820                         object r;
821                         
822                         error = false;
823                         int p = name.LastIndexOf ('.');
824
825                         if (dh.Lookup (ns, name, out r))
826                                 return (FullNamedExpression) r;
827                         else {
828                                 //
829                                 // If the type is not a nested type, we do not need `LookupType's processing.
830                                 // If the @name does not have a `.' in it, this cant be a nested type.
831                                 //
832                                 if (ns != ""){
833                                         if (Namespace.IsNamespace (ns)) {
834                                                 if (p != -1)
835                                                         t = TypeManager.LookupType (ns + "." + name);
836                                                 else
837                                                         t = TypeManager.LookupTypeDirect (ns + "." + name);
838                                         } else
839                                                 t = null;
840                                 } else if (p != -1)
841                                         t = TypeManager.LookupType (name);
842                                 else
843                                         t = TypeManager.LookupTypeDirect (name);
844                         }
845                         
846                         if (t != null) {
847                                 result = new TypeExpression (t, Location.Null);
848                                 dh.Insert (ns, name, result);
849                                 return result;
850                         }
851
852                         if (ns != "" && Namespace.IsNamespace (ns)) {
853                                 result = Namespace.LookupNamespace (ns, false).Lookup (this, name, Location.Null);
854                                 if (result != null) {
855                                         dh.Insert (ns, name, result);
856                                         return result;
857                                 }
858                         }
859
860                         if (ns == "" && Namespace.IsNamespace (name)) {
861                                 result = Namespace.LookupNamespace (name, false);
862                                 dh.Insert (ns, name, result);
863                                 return result;
864                         }
865
866                         //
867                         // In case we are fed a composite name, normalize it.
868                         //
869                         
870                         if (p != -1){
871                                 ns = MakeFQN (ns, name.Substring (0, p));
872                                 name = name.Substring (p+1);
873                         }
874
875                         if (ns.IndexOf ('+') != -1)
876                                 ns = ns.Replace ('+', '.');
877
878                         parent = RootContext.Tree.LookupByNamespace (ns, name);
879                         if (parent == null) {
880                                 dh.Insert (ns, name, null);
881                                 return null;
882                         }
883
884                         t = DefineTypeAndParents (parent);
885                         if (t == null){
886                                 error = true;
887                                 return null;
888                         }
889                         
890                         result = new TypeExpression (t, Location.Null);
891                         dh.Insert (ns, name, result);
892                         return result;
893                 }
894
895                 public static void Error_AmbiguousTypeReference (Location loc, string name, string t1, string t2)
896                 {
897                         Report.Error (104, loc,
898                                       "`{0}' is an ambiguous reference ({1} or {2})",
899                                       name, t1, t2);
900                 }
901
902                 /// <summary>
903                 ///   GetType is used to resolve type names at the DeclSpace level.
904                 ///   Use this to lookup class/struct bases, interface bases or 
905                 ///   delegate type references
906                 /// </summary>
907                 ///
908                 /// <remarks>
909                 ///   Contrast this to LookupType which is used inside method bodies to 
910                 ///   lookup types that have already been defined.  GetType is used
911                 ///   during the tree resolution process and potentially define
912                 ///   recursively the type
913                 /// </remarks>
914                 public FullNamedExpression FindType (Location loc, string name)
915                 {
916                         FullNamedExpression t;
917                         bool error;
918
919                         //
920                         // For the case the type we are looking for is nested within this one
921                         // or is in any base class
922                         //
923
924                         DeclSpace containing_ds = this;
925
926                         while (containing_ds != null){
927                                 Type container_type = containing_ds.TypeBuilder;
928                                 Type current_type = container_type;
929
930                                 while (current_type != null && current_type != TypeManager.object_type) {
931                                         string pre = current_type.FullName;
932
933                                         t = LookupInterfaceOrClass (pre, name, out error);
934                                         if (error)
935                                                 return null;
936                                 
937                                         if ((t != null) && containing_ds.CheckAccessLevel (t.Type))
938                                                 return t;
939
940                                         current_type = current_type.BaseType;
941                                 }
942                                 containing_ds = containing_ds.Parent;
943                         }
944
945                         //
946                         // Attempt to lookup the class on our namespace and all it's implicit parents
947                         //
948                         for (NamespaceEntry ns = NamespaceEntry; ns != null; ns = ns.ImplicitParent) {
949                                 t = LookupInterfaceOrClass (ns.FullName, name, out error);
950                                 if (error)
951                                         return null;
952                                 
953                                 if (t != null) 
954                                         return t;
955                         }
956                         
957                         //
958                         // Attempt to do a direct unqualified lookup
959                         //
960                         t = LookupInterfaceOrClass ("", name, out error);
961                         if (error)
962                                 return null;
963                         
964                         if (t != null)
965                                 return t;
966                         
967                         //
968                         // Attempt to lookup the class on any of the `using'
969                         // namespaces
970                         //
971
972                         for (NamespaceEntry ns = NamespaceEntry; ns != null; ns = ns.Parent){
973
974                                 t = LookupInterfaceOrClass (ns.FullName, name, out error);
975                                 if (error)
976                                         return null;
977
978                                 if (t != null)
979                                         return t;
980
981                                 if (name.IndexOf ('.') > 0)
982                                         continue;
983
984                                 t = ns.LookupAlias (name);
985                                 if (t != null)
986                                         return t;
987
988                                 //
989                                 // Now check the using clause list
990                                 //
991                                 FullNamedExpression match = null;
992                                 foreach (Namespace using_ns in ns.GetUsingTable ()) {
993                                         match = LookupInterfaceOrClass (using_ns.Name, name, out error);
994                                         if (error)
995                                                 return null;
996
997                                         if ((match != null) && (match is TypeExpr)) {
998                                                 Type matched = ((TypeExpr) match).Type;
999                                                 if (!CheckAccessLevel (matched))
1000                                                         continue;
1001                                                 if (t != null){
1002                                                         Error_AmbiguousTypeReference (loc, name, t.FullName, match.FullName);
1003                                                         return null;
1004                                                 }
1005                                                 t = match;
1006                                         }
1007                                 }
1008                                 if (t != null)
1009                                         return t;
1010                         }
1011
1012                         //Report.Error (246, Location, "Can not find type `"+name+"'");
1013                         return null;
1014                 }
1015
1016                 //
1017                 // Public function used to locate types, this can only
1018                 // be used after the ResolveTree function has been invoked.
1019                 //
1020                 // Set 'silent' to true if you want to suppress "type not found" errors.
1021                 // Set 'ignore_cs0104' to true if you want to ignore cs0104 errors.
1022                 //
1023                 // Returns: Type or null if they type can not be found.
1024                 //
1025                 public FullNamedExpression LookupType (string name, Location loc, bool silent, bool ignore_cs0104)
1026                 {
1027                         FullNamedExpression e;
1028
1029                         if (Cache.Contains (name)) {
1030                                 e = (FullNamedExpression) Cache [name];
1031                         } else {
1032                                 //
1033                                 // For the case the type we are looking for is nested within this one
1034                                 // or is in any base class
1035                                 //
1036                                 DeclSpace containing_ds = this;
1037                                 while (containing_ds != null){
1038                                         
1039                                         // if the member cache has been created, lets use it.
1040                                         // the member cache is MUCH faster.
1041                                         if (containing_ds.MemberCache != null) {
1042                                                 Type t = containing_ds.MemberCache.FindNestedType (name);
1043                                                 if (t == null) {
1044                                                         containing_ds = containing_ds.Parent;
1045                                                         continue;
1046                                                 }
1047
1048                                                 e = new TypeExpression (t, Location.Null);
1049                                                 Cache [name] = e;
1050                                                 return e;
1051                                         }
1052                                         
1053                                         // no member cache. Do it the hard way -- reflection
1054                                         Type current_type = containing_ds.TypeBuilder;
1055                                         
1056                                         while (current_type != null &&
1057                                                current_type != TypeManager.object_type) {
1058                                                 //
1059                                                 // nested class
1060                                                 //
1061                                                 Type t = TypeManager.LookupType (current_type.FullName + "." + name);
1062                                                 if (t != null){
1063                                                         e = new TypeExpression (t, Location.Null);
1064                                                         Cache [name] = e;
1065                                                         return e;
1066                                                 }
1067                                                 
1068                                                 current_type = current_type.BaseType;
1069                                         }
1070                                         
1071                                         containing_ds = containing_ds.Parent;
1072                                 }
1073                                 
1074                                 e = NamespaceEntry.LookupNamespaceOrType (this, name, loc, ignore_cs0104);
1075                                 if (!silent || e != null)
1076                                         Cache [name] = e;
1077                         }
1078
1079                         if (e == null && !silent)
1080                                 Report.Error (246, loc, "Cannot find type `"+name+"'");
1081                         
1082                         return e;
1083                 }
1084
1085                 /// <remarks>
1086                 ///   This function is broken and not what you're looking for.  It should only
1087                 ///   be used while the type is still being created since it doesn't use the cache
1088                 ///   and relies on the filter doing the member name check.
1089                 /// </remarks>
1090                 public abstract MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1091                                                         MemberFilter filter, object criteria);
1092
1093                 /// <remarks>
1094                 ///   If we have a MemberCache, return it.  This property may return null if the
1095                 ///   class doesn't have a member cache or while it's still being created.
1096                 /// </remarks>
1097                 public abstract MemberCache MemberCache {
1098                         get;
1099                 }
1100
1101                 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
1102                 {
1103                         try {
1104                                 TypeBuilder.SetCustomAttribute (cb);
1105                         } catch (System.ArgumentException e) {
1106                                 Report.Warning (-21, a.Location,
1107                                                 "The CharSet named property on StructLayout\n"+
1108                                                 "\tdoes not work correctly on Microsoft.NET\n"+
1109                                                 "\tYou might want to remove the CharSet declaration\n"+
1110                                                 "\tor compile using the Mono runtime instead of the\n"+
1111                                                 "\tMicrosoft .NET runtime\n"+
1112                                                 "\tThe runtime gave the error: " + e);
1113                         }
1114                 }
1115
1116                 /// <summary>
1117                 /// Goes through class hierarchy and get value of first CLSCompliantAttribute that found.
1118                 /// If no is attribute exists then return assembly CLSCompliantAttribute.
1119                 /// </summary>
1120                 public bool GetClsCompliantAttributeValue ()
1121                 {
1122                         if ((caching_flags & Flags.HasCompliantAttribute_Undetected) == 0)
1123                                 return (caching_flags & Flags.ClsCompliantAttributeTrue) != 0;
1124
1125                         caching_flags &= ~Flags.HasCompliantAttribute_Undetected;
1126
1127                         if (OptAttributes != null) {
1128                                 Attribute cls_attribute = OptAttributes.Search (TypeManager.cls_compliant_attribute_type, ec);
1129                                 if (cls_attribute != null) {
1130                                         caching_flags |= Flags.HasClsCompliantAttribute;
1131                                         if (cls_attribute.GetClsCompliantAttributeValue (ec)) {
1132                                                 caching_flags |= Flags.ClsCompliantAttributeTrue;
1133                                                 return true;
1134                                         }
1135                                         return false;
1136                                 }
1137                         }
1138
1139                         if (Parent == null) {
1140                                 if (CodeGen.Assembly.IsClsCompliant) {
1141                                         caching_flags |= Flags.ClsCompliantAttributeTrue;
1142                                         return true;
1143                                 }
1144                                 return false;
1145                         }
1146
1147                         if (Parent.GetClsCompliantAttributeValue ()) {
1148                                 caching_flags |= Flags.ClsCompliantAttributeTrue;
1149                                 return true;
1150                         }
1151                         return false;
1152                 }
1153
1154                 public override string[] ValidAttributeTargets {
1155                         get {
1156                                 return attribute_targets;
1157                         }
1158                 }
1159
1160                 bool IAlias.IsType {
1161                         get { return true; }
1162                 }
1163
1164                 string IAlias.Name {
1165                         get { return Name; }
1166                 }
1167
1168                 TypeExpr IAlias.ResolveAsType (EmitContext ec)
1169                 {
1170                         if (TypeBuilder == null)
1171                                 throw new InvalidOperationException ();
1172
1173                         return new TypeExpression (TypeBuilder, Location);
1174                 }
1175         }
1176
1177         /// <summary>
1178         ///   This is a readonly list of MemberInfo's.      
1179         /// </summary>
1180         public class MemberList : IList {
1181                 public readonly IList List;
1182                 int count;
1183
1184                 /// <summary>
1185                 ///   Create a new MemberList from the given IList.
1186                 /// </summary>
1187                 public MemberList (IList list)
1188                 {
1189                         if (list != null)
1190                                 this.List = list;
1191                         else
1192                                 this.List = new ArrayList ();
1193                         count = List.Count;
1194                 }
1195
1196                 /// <summary>
1197                 ///   Concatenate the ILists `first' and `second' to a new MemberList.
1198                 /// </summary>
1199                 public MemberList (IList first, IList second)
1200                 {
1201                         ArrayList list = new ArrayList ();
1202                         list.AddRange (first);
1203                         list.AddRange (second);
1204                         count = list.Count;
1205                         List = list;
1206                 }
1207
1208                 public static readonly MemberList Empty = new MemberList (new ArrayList ());
1209
1210                 /// <summary>
1211                 ///   Cast the MemberList into a MemberInfo[] array.
1212                 /// </summary>
1213                 /// <remarks>
1214                 ///   This is an expensive operation, only use it if it's really necessary.
1215                 /// </remarks>
1216                 public static explicit operator MemberInfo [] (MemberList list)
1217                 {
1218                         Timer.StartTimer (TimerType.MiscTimer);
1219                         MemberInfo [] result = new MemberInfo [list.Count];
1220                         list.CopyTo (result, 0);
1221                         Timer.StopTimer (TimerType.MiscTimer);
1222                         return result;
1223                 }
1224
1225                 // ICollection
1226
1227                 public int Count {
1228                         get {
1229                                 return count;
1230                         }
1231                 }
1232
1233                 public bool IsSynchronized {
1234                         get {
1235                                 return List.IsSynchronized;
1236                         }
1237                 }
1238
1239                 public object SyncRoot {
1240                         get {
1241                                 return List.SyncRoot;
1242                         }
1243                 }
1244
1245                 public void CopyTo (Array array, int index)
1246                 {
1247                         List.CopyTo (array, index);
1248                 }
1249
1250                 // IEnumerable
1251
1252                 public IEnumerator GetEnumerator ()
1253                 {
1254                         return List.GetEnumerator ();
1255                 }
1256
1257                 // IList
1258
1259                 public bool IsFixedSize {
1260                         get {
1261                                 return true;
1262                         }
1263                 }
1264
1265                 public bool IsReadOnly {
1266                         get {
1267                                 return true;
1268                         }
1269                 }
1270
1271                 object IList.this [int index] {
1272                         get {
1273                                 return List [index];
1274                         }
1275
1276                         set {
1277                                 throw new NotSupportedException ();
1278                         }
1279                 }
1280
1281                 // FIXME: try to find out whether we can avoid the cast in this indexer.
1282                 public MemberInfo this [int index] {
1283                         get {
1284                                 return (MemberInfo) List [index];
1285                         }
1286                 }
1287
1288                 public int Add (object value)
1289                 {
1290                         throw new NotSupportedException ();
1291                 }
1292
1293                 public void Clear ()
1294                 {
1295                         throw new NotSupportedException ();
1296                 }
1297
1298                 public bool Contains (object value)
1299                 {
1300                         return List.Contains (value);
1301                 }
1302
1303                 public int IndexOf (object value)
1304                 {
1305                         return List.IndexOf (value);
1306                 }
1307
1308                 public void Insert (int index, object value)
1309                 {
1310                         throw new NotSupportedException ();
1311                 }
1312
1313                 public void Remove (object value)
1314                 {
1315                         throw new NotSupportedException ();
1316                 }
1317
1318                 public void RemoveAt (int index)
1319                 {
1320                         throw new NotSupportedException ();
1321                 }
1322         }
1323
1324         /// <summary>
1325         ///   This interface is used to get all members of a class when creating the
1326         ///   member cache.  It must be implemented by all DeclSpace derivatives which
1327         ///   want to support the member cache and by TypeHandle to get caching of
1328         ///   non-dynamic types.
1329         /// </summary>
1330         public interface IMemberContainer {
1331                 /// <summary>
1332                 ///   The name of the IMemberContainer.  This is only used for
1333                 ///   debugging purposes.
1334                 /// </summary>
1335                 string Name {
1336                         get;
1337                 }
1338
1339                 /// <summary>
1340                 ///   The type of this IMemberContainer.
1341                 /// </summary>
1342                 Type Type {
1343                         get;
1344                 }
1345
1346                 /// <summary>
1347                 ///   Returns the IMemberContainer of the base class or null if this
1348                 ///   is an interface or TypeManger.object_type.
1349                 ///   This is used when creating the member cache for a class to get all
1350                 ///   members from the base class.
1351                 /// </summary>
1352                 MemberCache BaseCache {
1353                         get;
1354                 }
1355
1356                 /// <summary>
1357                 ///   Whether this is an interface.
1358                 /// </summary>
1359                 bool IsInterface {
1360                         get;
1361                 }
1362
1363                 /// <summary>
1364                 ///   Returns all members of this class with the corresponding MemberTypes
1365                 ///   and BindingFlags.
1366                 /// </summary>
1367                 /// <remarks>
1368                 ///   When implementing this method, make sure not to return any inherited
1369                 ///   members and check the MemberTypes and BindingFlags properly.
1370                 ///   Unfortunately, System.Reflection is lame and doesn't provide a way to
1371                 ///   get the BindingFlags (static/non-static,public/non-public) in the
1372                 ///   MemberInfo class, but the cache needs this information.  That's why
1373                 ///   this method is called multiple times with different BindingFlags.
1374                 /// </remarks>
1375                 MemberList GetMembers (MemberTypes mt, BindingFlags bf);
1376
1377                 /// <summary>
1378                 ///   Return the container's member cache.
1379                 /// </summary>
1380                 MemberCache MemberCache {
1381                         get;
1382                 }
1383         }
1384
1385         /// <summary>
1386         ///   The MemberCache is used by dynamic and non-dynamic types to speed up
1387         ///   member lookups.  It has a member name based hash table; it maps each member
1388         ///   name to a list of CacheEntry objects.  Each CacheEntry contains a MemberInfo
1389         ///   and the BindingFlags that were initially used to get it.  The cache contains
1390         ///   all members of the current class and all inherited members.  If this cache is
1391         ///   for an interface types, it also contains all inherited members.
1392         ///
1393         ///   There are two ways to get a MemberCache:
1394         ///   * if this is a dynamic type, lookup the corresponding DeclSpace and then
1395         ///     use the DeclSpace.MemberCache property.
1396         ///   * if this not a dynamic type, call TypeHandle.GetTypeHandle() to get a
1397         ///     TypeHandle instance for the type and then use TypeHandle.MemberCache.
1398         /// </summary>
1399         public class MemberCache {
1400                 public readonly IMemberContainer Container;
1401                 protected Hashtable member_hash;
1402                 protected Hashtable method_hash;
1403
1404                 /// <summary>
1405                 ///   Create a new MemberCache for the given IMemberContainer `container'.
1406                 /// </summary>
1407                 public MemberCache (IMemberContainer container)
1408                 {
1409                         this.Container = container;
1410
1411                         Timer.IncrementCounter (CounterType.MemberCache);
1412                         Timer.StartTimer (TimerType.CacheInit);
1413
1414                         // If we have a base class (we have a base class unless we're
1415                         // TypeManager.object_type), we deep-copy its MemberCache here.
1416                         if (Container.BaseCache != null)
1417                                 member_hash = DeepCopy (Container.BaseCache.member_hash);
1418                         else
1419                                 member_hash = new Hashtable ();
1420
1421                         // If this is neither a dynamic type nor an interface, create a special
1422                         // method cache with all declared and inherited methods.
1423                         Type type = container.Type;
1424                         if (!(type is TypeBuilder) && !type.IsInterface) {
1425                                 if (Container.BaseCache != null)
1426                                         method_hash = DeepCopy (Container.BaseCache.method_hash);
1427                                 else
1428                                         method_hash = new Hashtable ();
1429                                 AddMethods (type);
1430                         }
1431
1432                         // Add all members from the current class.
1433                         AddMembers (Container);
1434
1435                         Timer.StopTimer (TimerType.CacheInit);
1436                 }
1437
1438                 public MemberCache (Type[] ifaces)
1439                 {
1440                         //
1441                         // The members of this cache all belong to other caches.  
1442                         // So, 'Container' will not be used.
1443                         //
1444                         this.Container = null;
1445
1446                         member_hash = new Hashtable ();
1447                         if (ifaces == null)
1448                                 return;
1449
1450                         foreach (Type itype in ifaces)
1451                                 AddCacheContents (TypeManager.LookupMemberCache (itype));
1452                 }
1453
1454                 /// <summary>
1455                 ///   Return a a deep-copy of the hashtable @other.
1456                 /// </summary>
1457                 Hashtable DeepCopy (Hashtable other)
1458                 {
1459                         Hashtable hash = new Hashtable ();
1460
1461                         if (other == null)
1462                                 return hash;
1463
1464                         IDictionaryEnumerator it = other.GetEnumerator ();
1465                         while (it.MoveNext ()) {
1466                                 hash [it.Key] = ((ArrayList) it.Value).Clone ();
1467                          }
1468                                 
1469                         return hash;
1470                 }
1471
1472                 /// <summary>
1473                 ///   Add the contents of `cache' to the member_hash.
1474                 /// </summary>
1475                 void AddCacheContents (MemberCache cache)
1476                 {
1477                         IDictionaryEnumerator it = cache.member_hash.GetEnumerator ();
1478                         while (it.MoveNext ()) {
1479                                 ArrayList list = (ArrayList) member_hash [it.Key];
1480                                 if (list == null)
1481                                         member_hash [it.Key] = list = new ArrayList ();
1482
1483                                 ArrayList entries = (ArrayList) it.Value;
1484                                 for (int i = entries.Count-1; i >= 0; i--) {
1485                                         CacheEntry entry = (CacheEntry) entries [i];
1486
1487                                         if (entry.Container != cache.Container)
1488                                                 break;
1489                                         list.Add (entry);
1490                                 }
1491                         }
1492                 }
1493
1494                 /// <summary>
1495                 ///   Add all members from class `container' to the cache.
1496                 /// </summary>
1497                 void AddMembers (IMemberContainer container)
1498                 {
1499                         // We need to call AddMembers() with a single member type at a time
1500                         // to get the member type part of CacheEntry.EntryType right.
1501                         if (!container.IsInterface) {
1502                                 AddMembers (MemberTypes.Constructor, container);
1503                                 AddMembers (MemberTypes.Field, container);
1504                         }
1505                         AddMembers (MemberTypes.Method, container);
1506                         AddMembers (MemberTypes.Property, container);
1507                         AddMembers (MemberTypes.Event, container);
1508                         // Nested types are returned by both Static and Instance searches.
1509                         AddMembers (MemberTypes.NestedType,
1510                                     BindingFlags.Static | BindingFlags.Public, container);
1511                         AddMembers (MemberTypes.NestedType,
1512                                     BindingFlags.Static | BindingFlags.NonPublic, container);
1513                 }
1514
1515                 void AddMembers (MemberTypes mt, IMemberContainer container)
1516                 {
1517                         AddMembers (mt, BindingFlags.Static | BindingFlags.Public, container);
1518                         AddMembers (mt, BindingFlags.Static | BindingFlags.NonPublic, container);
1519                         AddMembers (mt, BindingFlags.Instance | BindingFlags.Public, container);
1520                         AddMembers (mt, BindingFlags.Instance | BindingFlags.NonPublic, container);
1521                 }
1522
1523                 /// <summary>
1524                 ///   Add all members from class `container' with the requested MemberTypes and
1525                 ///   BindingFlags to the cache.  This method is called multiple times with different
1526                 ///   MemberTypes and BindingFlags.
1527                 /// </summary>
1528                 void AddMembers (MemberTypes mt, BindingFlags bf, IMemberContainer container)
1529                 {
1530                         MemberList members = container.GetMembers (mt, bf);
1531
1532                         foreach (MemberInfo member in members) {
1533                                 string name = member.Name;
1534
1535                                 // We use a name-based hash table of ArrayList's.
1536                                 ArrayList list = (ArrayList) member_hash [name];
1537                                 if (list == null) {
1538                                         list = new ArrayList ();
1539                                         member_hash.Add (name, list);
1540                                 }
1541
1542                                 // When this method is called for the current class, the list will
1543                                 // already contain all inherited members from our base classes.
1544                                 // We cannot add new members in front of the list since this'd be an
1545                                 // expensive operation, that's why the list is sorted in reverse order
1546                                 // (ie. members from the current class are coming last).
1547                                 list.Add (new CacheEntry (container, member, mt, bf));
1548                         }
1549                 }
1550
1551                 /// <summary>
1552                 ///   Add all declared and inherited methods from class `type' to the method cache.
1553                 /// </summary>
1554                 void AddMethods (Type type)
1555                 {
1556                         AddMethods (BindingFlags.Static | BindingFlags.Public |
1557                                     BindingFlags.FlattenHierarchy, type);
1558                         AddMethods (BindingFlags.Static | BindingFlags.NonPublic |
1559                                     BindingFlags.FlattenHierarchy, type);
1560                         AddMethods (BindingFlags.Instance | BindingFlags.Public, type);
1561                         AddMethods (BindingFlags.Instance | BindingFlags.NonPublic, type);
1562                 }
1563
1564                 void AddMethods (BindingFlags bf, Type type)
1565                 {
1566                         //
1567                         // Consider the case:
1568                         //
1569                         //   class X { public virtual int f() {} }
1570                         //   class Y : X {}
1571                         // 
1572                         // When processing 'Y', the method_cache will already have a copy of 'f', 
1573                         // with ReflectedType == X.  However, we want to ensure that its ReflectedType == Y
1574                         // 
1575                         MethodBase [] members = type.GetMethods (bf);
1576
1577                         Array.Reverse (members);
1578
1579                         foreach (MethodBase member in members) {
1580                                 string name = member.Name;
1581
1582                                 // We use a name-based hash table of ArrayList's.
1583                                 ArrayList list = (ArrayList) method_hash [name];
1584                                 if (list == null) {
1585                                         list = new ArrayList ();
1586                                         method_hash.Add (name, list);
1587                                 }
1588
1589                                 Type declaring_type = member.DeclaringType;
1590                                 if (declaring_type == type) {
1591                                         list.Add (new CacheEntry (Container, member, MemberTypes.Method, bf | BindingFlags.DeclaredOnly));
1592                                         continue;
1593                                 }
1594
1595                                 int n = list.Count;
1596                                 while (n-- > 0) {
1597                                         CacheEntry entry = (CacheEntry) list [n];
1598                                         MethodBase old = entry.Member as MethodBase;
1599
1600                                         if (member.MethodHandle.Value == old.MethodHandle.Value && 
1601                                             declaring_type == old.DeclaringType) {
1602                                                 list [n] = new CacheEntry (entry, member);
1603                                                 break;
1604                                         }
1605                                 }
1606
1607                                 if (n < 0)
1608                                         throw new InternalErrorException ("cannot find inherited member " + member + " in base classes of " + type);
1609                         }
1610                 }
1611
1612                 /// <summary>
1613                 ///   Compute and return a appropriate `EntryType' magic number for the given
1614                 ///   MemberTypes and BindingFlags.
1615                 /// </summary>
1616                 protected static EntryType GetEntryType (MemberTypes mt, BindingFlags bf)
1617                 {
1618                         EntryType type = EntryType.None;
1619
1620                         if ((mt & MemberTypes.Constructor) != 0)
1621                                 type |= EntryType.Constructor;
1622                         if ((mt & MemberTypes.Event) != 0)
1623                                 type |= EntryType.Event;
1624                         if ((mt & MemberTypes.Field) != 0)
1625                                 type |= EntryType.Field;
1626                         if ((mt & MemberTypes.Method) != 0)
1627                                 type |= EntryType.Method;
1628                         if ((mt & MemberTypes.Property) != 0)
1629                                 type |= EntryType.Property;
1630                         // Nested types are returned by static and instance searches.
1631                         if ((mt & MemberTypes.NestedType) != 0)
1632                                 type |= EntryType.NestedType | EntryType.Static | EntryType.Instance;
1633
1634                         if ((bf & BindingFlags.Instance) != 0)
1635                                 type |= EntryType.Instance;
1636                         if ((bf & BindingFlags.Static) != 0)
1637                                 type |= EntryType.Static;
1638                         if ((bf & BindingFlags.Public) != 0)
1639                                 type |= EntryType.Public;
1640                         if ((bf & BindingFlags.NonPublic) != 0)
1641                                 type |= EntryType.NonPublic;
1642                         if ((bf & BindingFlags.DeclaredOnly) != 0)
1643                                 type |= EntryType.Declared;
1644
1645                         return type;
1646                 }
1647
1648                 /// <summary>
1649                 ///   The `MemberTypes' enumeration type is a [Flags] type which means that it may
1650                 ///   denote multiple member types.  Returns true if the given flags value denotes a
1651                 ///   single member types.
1652                 /// </summary>
1653                 public static bool IsSingleMemberType (MemberTypes mt)
1654                 {
1655                         switch (mt) {
1656                         case MemberTypes.Constructor:
1657                         case MemberTypes.Event:
1658                         case MemberTypes.Field:
1659                         case MemberTypes.Method:
1660                         case MemberTypes.Property:
1661                         case MemberTypes.NestedType:
1662                                 return true;
1663
1664                         default:
1665                                 return false;
1666                         }
1667                 }
1668
1669                 /// <summary>
1670                 ///   We encode the MemberTypes and BindingFlags of each members in a "magic"
1671                 ///   number to speed up the searching process.
1672                 /// </summary>
1673                 [Flags]
1674                 protected enum EntryType {
1675                         None            = 0x000,
1676
1677                         Instance        = 0x001,
1678                         Static          = 0x002,
1679                         MaskStatic      = Instance|Static,
1680
1681                         Public          = 0x004,
1682                         NonPublic       = 0x008,
1683                         MaskProtection  = Public|NonPublic,
1684
1685                         Declared        = 0x010,
1686
1687                         Constructor     = 0x020,
1688                         Event           = 0x040,
1689                         Field           = 0x080,
1690                         Method          = 0x100,
1691                         Property        = 0x200,
1692                         NestedType      = 0x400,
1693
1694                         MaskType        = Constructor|Event|Field|Method|Property|NestedType
1695                 }
1696
1697                 protected class CacheEntry {
1698                         public readonly IMemberContainer Container;
1699                         public readonly EntryType EntryType;
1700                         public readonly MemberInfo Member;
1701
1702                         public CacheEntry (IMemberContainer container, MemberInfo member,
1703                                            MemberTypes mt, BindingFlags bf)
1704                         {
1705                                 this.Container = container;
1706                                 this.Member = member;
1707                                 this.EntryType = GetEntryType (mt, bf);
1708                         }
1709
1710                         public CacheEntry (CacheEntry other, MemberInfo update)
1711                         {
1712                                 this.Container = other.Container;
1713                                 this.EntryType = other.EntryType & ~EntryType.Declared;
1714                                 this.Member = update;
1715                         }
1716
1717                         public override string ToString ()
1718                         {
1719                                 return String.Format ("CacheEntry ({0}:{1}:{2})", Container.Name,
1720                                                       EntryType, Member);
1721                         }
1722                 }
1723
1724                 /// <summary>
1725                 ///   This is called each time we're walking up one level in the class hierarchy
1726                 ///   and checks whether we can abort the search since we've already found what
1727                 ///   we were looking for.
1728                 /// </summary>
1729                 protected bool DoneSearching (ArrayList list)
1730                 {
1731                         //
1732                         // We've found exactly one member in the current class and it's not
1733                         // a method or constructor.
1734                         //
1735                         if (list.Count == 1 && !(list [0] is MethodBase))
1736                                 return true;
1737
1738                         //
1739                         // Multiple properties: we query those just to find out the indexer
1740                         // name
1741                         //
1742                         if ((list.Count > 0) && (list [0] is PropertyInfo))
1743                                 return true;
1744
1745                         return false;
1746                 }
1747
1748                 /// <summary>
1749                 ///   Looks up members with name `name'.  If you provide an optional
1750                 ///   filter function, it'll only be called with members matching the
1751                 ///   requested member name.
1752                 ///
1753                 ///   This method will try to use the cache to do the lookup if possible.
1754                 ///
1755                 ///   Unlike other FindMembers implementations, this method will always
1756                 ///   check all inherited members - even when called on an interface type.
1757                 ///
1758                 ///   If you know that you're only looking for methods, you should use
1759                 ///   MemberTypes.Method alone since this speeds up the lookup a bit.
1760                 ///   When doing a method-only search, it'll try to use a special method
1761                 ///   cache (unless it's a dynamic type or an interface) and the returned
1762                 ///   MemberInfo's will have the correct ReflectedType for inherited methods.
1763                 ///   The lookup process will automatically restart itself in method-only
1764                 ///   search mode if it discovers that it's about to return methods.
1765                 /// </summary>
1766                 ArrayList global = new ArrayList ();
1767                 bool using_global = false;
1768                 
1769                 static MemberInfo [] emptyMemberInfo = new MemberInfo [0];
1770                 
1771                 public MemberInfo [] FindMembers (MemberTypes mt, BindingFlags bf, string name,
1772                                                   MemberFilter filter, object criteria)
1773                 {
1774                         if (using_global)
1775                                 throw new Exception ();
1776                         
1777                         bool declared_only = (bf & BindingFlags.DeclaredOnly) != 0;
1778                         bool method_search = mt == MemberTypes.Method;
1779                         // If we have a method cache and we aren't already doing a method-only search,
1780                         // then we restart a method search if the first match is a method.
1781                         bool do_method_search = !method_search && (method_hash != null);
1782
1783                         ArrayList applicable;
1784
1785                         // If this is a method-only search, we try to use the method cache if
1786                         // possible; a lookup in the method cache will return a MemberInfo with
1787                         // the correct ReflectedType for inherited methods.
1788                         
1789                         if (method_search && (method_hash != null))
1790                                 applicable = (ArrayList) method_hash [name];
1791                         else
1792                                 applicable = (ArrayList) member_hash [name];
1793
1794                         if (applicable == null)
1795                                 return emptyMemberInfo;
1796
1797                         //
1798                         // 32  slots gives 53 rss/54 size
1799                         // 2/4 slots gives 55 rss
1800                         //
1801                         // Strange: from 25,000 calls, only 1,800
1802                         // are above 2.  Why does this impact it?
1803                         //
1804                         global.Clear ();
1805                         using_global = true;
1806
1807                         Timer.StartTimer (TimerType.CachedLookup);
1808
1809                         EntryType type = GetEntryType (mt, bf);
1810
1811                         IMemberContainer current = Container;
1812
1813
1814                         // `applicable' is a list of all members with the given member name `name'
1815                         // in the current class and all its base classes.  The list is sorted in
1816                         // reverse order due to the way how the cache is initialy created (to speed
1817                         // things up, we're doing a deep-copy of our base).
1818
1819                         for (int i = applicable.Count-1; i >= 0; i--) {
1820                                 CacheEntry entry = (CacheEntry) applicable [i];
1821
1822                                 // This happens each time we're walking one level up in the class
1823                                 // hierarchy.  If we're doing a DeclaredOnly search, we must abort
1824                                 // the first time this happens (this may already happen in the first
1825                                 // iteration of this loop if there are no members with the name we're
1826                                 // looking for in the current class).
1827                                 if (entry.Container != current) {
1828                                         if (declared_only || DoneSearching (global))
1829                                                 break;
1830
1831                                         current = entry.Container;
1832                                 }
1833
1834                                 // Is the member of the correct type ?
1835                                 if ((entry.EntryType & type & EntryType.MaskType) == 0)
1836                                         continue;
1837
1838                                 // Is the member static/non-static ?
1839                                 if ((entry.EntryType & type & EntryType.MaskStatic) == 0)
1840                                         continue;
1841
1842                                 // Apply the filter to it.
1843                                 if (filter (entry.Member, criteria)) {
1844                                         if ((entry.EntryType & EntryType.MaskType) != EntryType.Method)
1845                                                 do_method_search = false;
1846                                         global.Add (entry.Member);
1847                                 }
1848                         }
1849
1850                         Timer.StopTimer (TimerType.CachedLookup);
1851
1852                         // If we have a method cache and we aren't already doing a method-only
1853                         // search, we restart in method-only search mode if the first match is
1854                         // a method.  This ensures that we return a MemberInfo with the correct
1855                         // ReflectedType for inherited methods.
1856                         if (do_method_search && (global.Count > 0)){
1857                                 using_global = false;
1858
1859                                 return FindMembers (MemberTypes.Method, bf, name, filter, criteria);
1860                         }
1861
1862                         using_global = false;
1863                         MemberInfo [] copy = new MemberInfo [global.Count];
1864                         global.CopyTo (copy);
1865                         return copy;
1866                 }
1867                 
1868                 // find the nested type @name in @this.
1869                 public Type FindNestedType (string name)
1870                 {
1871                         ArrayList applicable = (ArrayList) member_hash [name];
1872                         if (applicable == null)
1873                                 return null;
1874                         
1875                         for (int i = applicable.Count-1; i >= 0; i--) {
1876                                 CacheEntry entry = (CacheEntry) applicable [i];
1877                                 if ((entry.EntryType & EntryType.NestedType & EntryType.MaskType) != 0)
1878                                         return (Type) entry.Member;
1879                         }
1880                         
1881                         return null;
1882                 }
1883                 
1884                 //
1885                 // This finds the method or property for us to override. invocationType is the type where
1886                 // the override is going to be declared, name is the name of the method/property, and
1887                 // paramTypes is the parameters, if any to the method or property
1888                 //
1889                 // Because the MemberCache holds members from this class and all the base classes,
1890                 // we can avoid tons of reflection stuff.
1891                 //
1892                 public MemberInfo FindMemberToOverride (Type invocationType, string name, Type [] paramTypes, bool is_property)
1893                 {
1894                         ArrayList applicable;
1895                         if (method_hash != null && !is_property)
1896                                 applicable = (ArrayList) method_hash [name];
1897                         else
1898                                 applicable = (ArrayList) member_hash [name];
1899                         
1900                         if (applicable == null)
1901                                 return null;
1902                         //
1903                         // Walk the chain of methods, starting from the top.
1904                         //
1905                         for (int i = applicable.Count - 1; i >= 0; i--) {
1906                                 CacheEntry entry = (CacheEntry) applicable [i];
1907                                 
1908                                 if ((entry.EntryType & (is_property ? (EntryType.Property | EntryType.Field) : EntryType.Method)) == 0)
1909                                         continue;
1910
1911                                 PropertyInfo pi = null;
1912                                 MethodInfo mi = null;
1913                                 FieldInfo fi = null;
1914                                 Type [] cmpAttrs = null;
1915                                 
1916                                 if (is_property) {
1917                                         if ((entry.EntryType & EntryType.Field) != 0) {
1918                                                 fi = (FieldInfo)entry.Member;
1919
1920                                                 // TODO: For this case we ignore member type
1921                                                 //fb = TypeManager.GetField (fi);
1922                                                 //cmpAttrs = new Type[] { fb.MemberType };
1923                                         } else {
1924                                                 pi = (PropertyInfo) entry.Member;
1925                                                 cmpAttrs = TypeManager.GetArgumentTypes (pi);
1926                                         }
1927                                 } else {
1928                                         mi = (MethodInfo) entry.Member;
1929                                         cmpAttrs = TypeManager.GetArgumentTypes (mi);
1930                                 }
1931
1932                                 if (fi != null) {
1933                                         // TODO: Almost duplicate !
1934                                         // Check visibility
1935                                         switch (fi.Attributes & FieldAttributes.FieldAccessMask) {
1936                                                 case FieldAttributes.Private:
1937                                                         //
1938                                                         // A private method is Ok if we are a nested subtype.
1939                                                         // The spec actually is not very clear about this, see bug 52458.
1940                                                         //
1941                                                         if (invocationType != entry.Container.Type &
1942                                                                 TypeManager.IsNestedChildOf (invocationType, entry.Container.Type))
1943                                                                 continue;
1944
1945                                                         break;
1946                                                 case FieldAttributes.FamANDAssem:
1947                                                 case FieldAttributes.Assembly:
1948                                                         //
1949                                                         // Check for assembly methods
1950                                                         //
1951                                                         if (mi.DeclaringType.Assembly != CodeGen.Assembly.Builder)
1952                                                                 continue;
1953                                                         break;
1954                                         }
1955                                         return entry.Member;
1956                                 }
1957
1958                                 //
1959                                 // Check the arguments
1960                                 //
1961                                 if (cmpAttrs.Length != paramTypes.Length)
1962                                         continue;
1963         
1964                                 for (int j = cmpAttrs.Length - 1; j >= 0; j --)
1965                                         if (paramTypes [j] != cmpAttrs [j])
1966                                                 goto next;
1967                                 
1968                                 //
1969                                 // get one of the methods because this has the visibility info.
1970                                 //
1971                                 if (is_property) {
1972                                         mi = pi.GetGetMethod (true);
1973                                         if (mi == null)
1974                                                 mi = pi.GetSetMethod (true);
1975                                 }
1976                                 
1977                                 //
1978                                 // Check visibility
1979                                 //
1980                                 switch (mi.Attributes & MethodAttributes.MemberAccessMask) {
1981                                 case MethodAttributes.Private:
1982                                         //
1983                                         // A private method is Ok if we are a nested subtype.
1984                                         // The spec actually is not very clear about this, see bug 52458.
1985                                         //
1986                                         if (invocationType == entry.Container.Type ||
1987                                             TypeManager.IsNestedChildOf (invocationType, entry.Container.Type))
1988                                                 return entry.Member;
1989                                         
1990                                         break;
1991                                 case MethodAttributes.FamANDAssem:
1992                                 case MethodAttributes.Assembly:
1993                                         //
1994                                         // Check for assembly methods
1995                                         //
1996                                         if (mi.DeclaringType.Assembly == CodeGen.Assembly.Builder)
1997                                                 return entry.Member;
1998                                         
1999                                         break;
2000                                 default:
2001                                         //
2002                                         // A protected method is ok, because we are overriding.
2003                                         // public is always ok.
2004                                         //
2005                                         return entry.Member;
2006                                 }
2007                         next:
2008                                 ;
2009                         }
2010                         
2011                         return null;
2012                 }
2013
2014                 /// <summary>
2015                 /// The method is looking for conflict with inherited symbols (errors CS0108, CS0109).
2016                 /// We handle two cases. The first is for types without parameters (events, field, properties).
2017                 /// The second are methods, indexers and this is why ignore_complex_types is here.
2018                 /// The latest param is temporary hack. See DoDefineMembers method for more info.
2019                 /// </summary>
2020                 public MemberInfo FindMemberWithSameName (string name, bool ignore_complex_types, MemberInfo ignore_member)
2021                 {
2022                         ArrayList applicable = null;
2023  
2024                         if (method_hash != null)
2025                                 applicable = (ArrayList) method_hash [name];
2026  
2027                         if (applicable != null) {
2028                                 for (int i = applicable.Count - 1; i >= 0; i--) {
2029                                         CacheEntry entry = (CacheEntry) applicable [i];
2030                                         if ((entry.EntryType & EntryType.Public) != 0)
2031                                                 return entry.Member;
2032                                 }
2033                         }
2034  
2035                         if (member_hash == null)
2036                                 return null;
2037                         applicable = (ArrayList) member_hash [name];
2038                         
2039                         if (applicable != null) {
2040                                 for (int i = applicable.Count - 1; i >= 0; i--) {
2041                                         CacheEntry entry = (CacheEntry) applicable [i];
2042                                         if ((entry.EntryType & EntryType.Public) != 0 & entry.Member != ignore_member) {
2043                                                 if (ignore_complex_types) {
2044                                                         if ((entry.EntryType & EntryType.Method) != 0)
2045                                                                 continue;
2046  
2047                                                         // Does exist easier way how to detect indexer ?
2048                                                         if ((entry.EntryType & EntryType.Property) != 0) {
2049                                                                 Type[] arg_types = TypeManager.GetArgumentTypes ((PropertyInfo)entry.Member);
2050                                                                 if (arg_types.Length > 0)
2051                                                                         continue;
2052                                                         }
2053                                                 }
2054                                                 return entry.Member;
2055                                         }
2056                                 }
2057                         }
2058                         return null;
2059                 }
2060
2061                 Hashtable locase_table;
2062  
2063                 /// <summary>
2064                 /// Builds low-case table for CLS Compliance test
2065                 /// </summary>
2066                 public Hashtable GetPublicMembers ()
2067                 {
2068                         if (locase_table != null)
2069                                 return locase_table;
2070  
2071                         locase_table = new Hashtable ();
2072                         foreach (DictionaryEntry entry in member_hash) {
2073                                 ArrayList members = (ArrayList)entry.Value;
2074                                 for (int ii = 0; ii < members.Count; ++ii) {
2075                                         CacheEntry member_entry = (CacheEntry) members [ii];
2076  
2077                                         if ((member_entry.EntryType & EntryType.Public) == 0)
2078                                                 continue;
2079  
2080                                         // TODO: Does anyone know easier way how to detect that member is internal ?
2081                                         switch (member_entry.EntryType & EntryType.MaskType) {
2082                                                 case EntryType.Constructor:
2083                                                         continue;
2084  
2085                                                 case EntryType.Field:
2086                                                         if ((((FieldInfo)member_entry.Member).Attributes & (FieldAttributes.Assembly | FieldAttributes.Public)) == FieldAttributes.Assembly)
2087                                                                 continue;
2088                                                         break;
2089  
2090                                                 case EntryType.Method:
2091                                                         if ((((MethodInfo)member_entry.Member).Attributes & (MethodAttributes.Assembly | MethodAttributes.Public)) == MethodAttributes.Assembly)
2092                                                                 continue;
2093                                                         break;
2094  
2095                                                 case EntryType.Property:
2096                                                         PropertyInfo pi = (PropertyInfo)member_entry.Member;
2097                                                         if (pi.GetSetMethod () == null && pi.GetGetMethod () == null)
2098                                                                 continue;
2099                                                         break;
2100  
2101                                                 case EntryType.Event:
2102                                                         EventInfo ei = (EventInfo)member_entry.Member;
2103                                                         MethodInfo mi = ei.GetAddMethod ();
2104                                                         if ((mi.Attributes & (MethodAttributes.Assembly | MethodAttributes.Public)) == MethodAttributes.Assembly)
2105                                                                 continue;
2106                                                         break;
2107                                         }
2108                                         string lcase = ((string)entry.Key).ToLower (System.Globalization.CultureInfo.InvariantCulture);
2109                                         locase_table [lcase] = member_entry.Member;
2110                                         break;
2111                                 }
2112                         }
2113                         return locase_table;
2114                 }
2115  
2116                 public Hashtable Members {
2117                         get {
2118                                 return member_hash;
2119                         }
2120                 }
2121  
2122                 /// <summary>
2123                 /// Cls compliance check whether methods or constructors parameters differing only in ref or out, or in array rank
2124                 /// </summary>
2125                 public void VerifyClsParameterConflict (ArrayList al, MethodCore method, MemberInfo this_builder)
2126                 {
2127                         EntryType tested_type = (method is Constructor ? EntryType.Constructor : EntryType.Method) | EntryType.Public;
2128  
2129                         for (int i = 0; i < al.Count; ++i) {
2130                                 MemberCache.CacheEntry entry = (MemberCache.CacheEntry) al [i];
2131                 
2132                                 // skip itself
2133                                 if (entry.Member == this_builder)
2134                                         continue;
2135                 
2136                                 if ((entry.EntryType & tested_type) != tested_type)
2137                                         continue;
2138                 
2139                                 MethodBase method_to_compare = (MethodBase)entry.Member;
2140                                 if (AttributeTester.AreOverloadedMethodParamsClsCompliant (method.ParameterTypes, TypeManager.GetArgumentTypes (method_to_compare)))
2141                                         continue;
2142
2143                                 IMethodData md = TypeManager.GetMethod (method_to_compare);
2144
2145                                 // TODO: now we are ignoring CLSCompliance(false) on method from other assembly which is buggy.
2146                                 // However it is exactly what csc does.
2147                                 if (md != null && !md.IsClsCompliaceRequired (method.Parent))
2148                                         continue;
2149                 
2150                                 Report.SymbolRelatedToPreviousError (entry.Member);
2151                                 Report.Error (3006, method.Location, "Overloaded method '{0}' differing only in ref or out, or in array rank, is not CLS-compliant", method.GetSignatureForError ());
2152                         }
2153                 }
2154         }
2155 }