863f2c91c91a3e340757fc4315ae563b84a03aa3
[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 // Dual licensed under the terms of the MIT X11 or GNU GPL
8 //
9 // Copyright 2001 Ximian, Inc (http://www.ximian.com)
10 // Copyright 2004-2008 Novell, Inc
11 //
12 //
13
14 using System;
15 using System.Text;
16 using System.Collections;
17 using System.Globalization;
18 using System.Reflection.Emit;
19 using System.Reflection;
20
21 #if BOOTSTRAP_WITH_OLDLIB || NET_2_1
22 using XmlElement = System.Object;
23 #else
24 using System.Xml;
25 #endif
26
27 namespace Mono.CSharp {
28
29         //
30         // Better name would be DottenName
31         //
32         public class MemberName {
33                 public readonly string Name;
34                 public readonly TypeArguments TypeArguments;
35
36                 public readonly MemberName Left;
37                 public readonly Location Location;
38
39                 public static readonly MemberName Null = new MemberName ("");
40
41                 bool is_double_colon;
42
43                 private MemberName (MemberName left, string name, bool is_double_colon,
44                                     Location loc)
45                 {
46                         this.Name = name;
47                         this.Location = loc;
48                         this.is_double_colon = is_double_colon;
49                         this.Left = left;
50                 }
51
52                 private MemberName (MemberName left, string name, bool is_double_colon,
53                                     TypeArguments args, Location loc)
54                         : this (left, name, is_double_colon, loc)
55                 {
56                         if (args != null && args.Count > 0)
57                                 this.TypeArguments = args;
58                 }
59
60                 public MemberName (string name)
61                         : this (name, Location.Null)
62                 { }
63
64                 public MemberName (string name, Location loc)
65                         : this (null, name, false, loc)
66                 { }
67
68                 public MemberName (string name, TypeArguments args, Location loc)
69                         : this (null, name, false, args, loc)
70                 { }
71
72                 public MemberName (MemberName left, string name)
73                         : this (left, name, left != null ? left.Location : Location.Null)
74                 { }
75
76                 public MemberName (MemberName left, string name, Location loc)
77                         : this (left, name, false, loc)
78                 { }
79
80                 public MemberName (MemberName left, string name, TypeArguments args, Location loc)
81                         : this (left, name, false, args, loc)
82                 { }
83
84                 public MemberName (string alias, string name, TypeArguments args, Location loc)
85                         : this (new MemberName (alias, loc), name, true, args, loc)
86                 { }
87
88                 public MemberName (MemberName left, MemberName right)
89                         : this (left, right, right.Location)
90                 { }
91
92                 public MemberName (MemberName left, MemberName right, Location loc)
93                         : this (null, right.Name, false, right.TypeArguments, loc)
94                 {
95                         if (right.is_double_colon)
96                                 throw new InternalErrorException ("Cannot append double_colon member name");
97                         this.Left = (right.Left == null) ? left : new MemberName (left, right.Left);
98                 }
99
100                 // TODO: Remove
101                 public string GetName ()
102                 {
103                         return GetName (false);
104                 }
105
106                 public bool IsGeneric {
107                         get {
108                                 if (TypeArguments != null)
109                                         return true;
110                                 else if (Left != null)
111                                         return Left.IsGeneric;
112                                 else
113                                         return false;
114                         }
115                 }
116
117                 public string GetName (bool is_generic)
118                 {
119                         string name = is_generic ? Basename : Name;
120                         if (Left != null)
121                                 return Left.GetName (is_generic) + (is_double_colon ? "::" : ".") + name;
122
123                         return name;
124                 }
125
126                 public ATypeNameExpression GetTypeExpression ()
127                 {
128                         if (Left == null) {
129                                 if (TypeArguments != null)
130                                         return new SimpleName (Basename, TypeArguments, Location);
131                                 
132                                 return new SimpleName (Name, Location);
133                         }
134
135                         if (is_double_colon) {
136                                 if (Left.Left != null)
137                                         throw new InternalErrorException ("The left side of a :: should be an identifier");
138                                 return new QualifiedAliasMember (Left.Name, Name, TypeArguments, Location);
139                         }
140
141                         Expression lexpr = Left.GetTypeExpression ();
142                         return new MemberAccess (lexpr, Name, TypeArguments, Location);
143                 }
144
145                 public MemberName Clone ()
146                 {
147                         MemberName left_clone = Left == null ? null : Left.Clone ();
148                         return new MemberName (left_clone, Name, is_double_colon, TypeArguments, Location);
149                 }
150
151                 public string Basename {
152                         get {
153                                 if (TypeArguments != null)
154                                         return MakeName (Name, TypeArguments);
155                                 return Name;
156                         }
157                 }
158
159                 public string GetSignatureForError ()
160                 {
161                         string append = TypeArguments == null ? "" : "<" + TypeArguments.GetSignatureForError () + ">";
162                         if (Left == null)
163                                 return Name + append;
164                         string connect = is_double_colon ? "::" : ".";
165                         return Left.GetSignatureForError () + connect + Name + append;
166                 }
167
168                 public override bool Equals (object other)
169                 {
170                         return Equals (other as MemberName);
171                 }
172
173                 public bool Equals (MemberName other)
174                 {
175                         if (this == other)
176                                 return true;
177                         if (other == null || Name != other.Name)
178                                 return false;
179                         if (is_double_colon != other.is_double_colon)
180                                 return false;
181
182                         if ((TypeArguments != null) &&
183                             (other.TypeArguments == null || TypeArguments.Count != other.TypeArguments.Count))
184                                 return false;
185
186                         if ((TypeArguments == null) && (other.TypeArguments != null))
187                                 return false;
188
189                         if (Left == null)
190                                 return other.Left == null;
191
192                         return Left.Equals (other.Left);
193                 }
194
195                 public override int GetHashCode ()
196                 {
197                         int hash = Name.GetHashCode ();
198                         for (MemberName n = Left; n != null; n = n.Left)
199                                 hash ^= n.Name.GetHashCode ();
200                         if (is_double_colon)
201                                 hash ^= 0xbadc01d;
202
203                         if (TypeArguments != null)
204                                 hash ^= TypeArguments.Count << 5;
205
206                         return hash & 0x7FFFFFFF;
207                 }
208
209                 public int CountTypeArguments {
210                         get {
211                                 if (TypeArguments != null)
212                                         return TypeArguments.Count;
213                                 else if (Left != null)
214                                         return Left.CountTypeArguments; 
215                                 else
216                                         return 0;
217                         }
218                 }
219
220                 public static string MakeName (string name, TypeArguments args)
221                 {
222                         if (args == null)
223                                 return name;
224
225                         return name + "`" + args.Count;
226                 }
227
228                 public static string MakeName (string name, int count)
229                 {
230                         return name + "`" + count;
231                 }
232         }
233
234         /// <summary>
235         ///   Base representation for members.  This is used to keep track
236         ///   of Name, Location and Modifier flags, and handling Attributes.
237         /// </summary>
238         public abstract class MemberCore : Attributable, IResolveContext {
239                 /// <summary>
240                 ///   Public name
241                 /// </summary>
242
243                 protected string cached_name;
244                 // TODO: Remove in favor of MemberName
245                 public string Name {
246                         get {
247                                 if (cached_name == null)
248                                         cached_name = MemberName.GetName (!(this is GenericMethod) && !(this is Method));
249                                 return cached_name;
250                         }
251                 }
252
253                 // Is not readonly because of IndexerName attribute
254                 private MemberName member_name;
255                 public MemberName MemberName {
256                         get { return member_name; }
257                 }
258
259                 /// <summary>
260                 ///   Modifier flags that the user specified in the source code
261                 /// </summary>
262                 private int mod_flags;
263                 public int ModFlags {
264                         set {
265                                 mod_flags = value;
266                                 if ((value & Modifiers.COMPILER_GENERATED) != 0)
267                                         caching_flags = Flags.IsUsed | Flags.IsAssigned;
268                         }
269                         get {
270                                 return mod_flags;
271                         }
272                 }
273
274                 public /*readonly*/ DeclSpace Parent;
275
276                 /// <summary>
277                 ///   Location where this declaration happens
278                 /// </summary>
279                 public Location Location {
280                         get { return member_name.Location; }
281                 }
282
283                 /// <summary>
284                 ///   XML documentation comment
285                 /// </summary>
286                 protected string comment;
287
288                 /// <summary>
289                 ///   Represents header string for documentation comment 
290                 ///   for each member types.
291                 /// </summary>
292                 public abstract string DocCommentHeader { get; }
293
294                 [Flags]
295                 public enum Flags {
296                         Obsolete_Undetected = 1,                // Obsolete attribute has not been detected yet
297                         Obsolete = 1 << 1,                      // Type has obsolete attribute
298                         ClsCompliance_Undetected = 1 << 2,      // CLS Compliance has not been detected yet
299                         ClsCompliant = 1 << 3,                  // Type is CLS Compliant
300                         CloseTypeCreated = 1 << 4,              // Tracks whether we have Closed the type
301                         HasCompliantAttribute_Undetected = 1 << 5,      // Presence of CLSCompliantAttribute has not been detected
302                         HasClsCompliantAttribute = 1 << 6,                      // Type has CLSCompliantAttribute
303                         ClsCompliantAttributeTrue = 1 << 7,                     // Type has CLSCompliant (true)
304                         Excluded_Undetected = 1 << 8,           // Conditional attribute has not been detected yet
305                         Excluded = 1 << 9,                                      // Method is conditional
306                         MethodOverloadsExist = 1 << 10,         // Test for duplication must be performed
307                         IsUsed = 1 << 11,
308                         IsAssigned = 1 << 12,                           // Field is assigned
309                         HasExplicitLayout       = 1 << 13,
310                         PartialDefinitionExists = 1 << 14,      // Set when corresponding partial method definition exists
311                         HasStructLayout         = 1 << 15                       // Has StructLayoutAttribute
312                 }
313
314                 /// <summary>
315                 ///   MemberCore flags at first detected then cached
316                 /// </summary>
317                 internal Flags caching_flags;
318
319                 public MemberCore (DeclSpace parent, MemberName name, Attributes attrs)
320                         : base (attrs)
321                 {
322                         this.Parent = parent;
323                         member_name = name;
324                         caching_flags = Flags.Obsolete_Undetected | Flags.ClsCompliance_Undetected | Flags.HasCompliantAttribute_Undetected | Flags.Excluded_Undetected;
325                 }
326
327                 protected virtual void SetMemberName (MemberName new_name)
328                 {
329                         member_name = new_name;
330                         cached_name = null;
331                 }
332
333                 protected bool CheckAbstractAndExtern (bool has_block)
334                 {
335                         if (Parent.PartialContainer.Kind == Kind.Interface)
336                                 return true;
337
338                         if (has_block) {
339                                 if ((ModFlags & Modifiers.EXTERN) != 0) {
340                                         Report.Error (179, Location, "`{0}' cannot declare a body because it is marked extern",
341                                                 GetSignatureForError ());
342                                         return false;
343                                 }
344
345                                 if ((ModFlags & Modifiers.ABSTRACT) != 0) {
346                                         Report.Error (500, Location, "`{0}' cannot declare a body because it is marked abstract",
347                                                 GetSignatureForError ());
348                                         return false;
349                                 }
350                         } else {
351                                 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN | Modifiers.PARTIAL)) == 0) {
352                                         if (RootContext.Version >= LanguageVersion.LINQ && this is Property.PropertyMethod &&
353                                                 !(this is Indexer.GetIndexerMethod || this is Indexer.SetIndexerMethod)) {
354                                                 Report.Error (840, Location, "`{0}' must have a body because it is not marked abstract or extern. The property can be automatically implemented when you define both accessors",
355                                                               GetSignatureForError ());
356                                         } else {
357                                                 Report.Error (501, Location, "`{0}' must have a body because it is not marked abstract, extern, or partial",
358                                                               GetSignatureForError ());
359                                         }
360                                         return false;
361                                 }
362                         }
363
364                         return true;
365                 }
366
367                 public void CheckProtectedModifier ()
368                 {
369                         if ((ModFlags & Modifiers.PROTECTED) == 0)
370                                 return;
371
372                         if (Parent.PartialContainer.Kind == Kind.Struct) {
373                                 Report.Error (666, Location, "`{0}': Structs cannot contain protected members",
374                                         GetSignatureForError ());
375                                 return;
376                         }
377
378                         if ((Parent.ModFlags & Modifiers.STATIC) != 0) {
379                                 Report.Error (1057, Location, "`{0}': Static classes cannot contain protected members",
380                                         GetSignatureForError ());
381                                 return;
382                         }
383
384                         if (((Parent.ModFlags & Modifiers.SEALED) != 0) &&
385                                 ((ModFlags & Modifiers.OVERRIDE) == 0) && (Name != "Finalize")) {
386                                 Report.Warning (628, 4, Location, "`{0}': new protected member declared in sealed class",
387                                         GetSignatureForError ());
388                                 return;
389                         }
390                 }
391
392                 public abstract bool Define ();
393
394                 public virtual string DocComment {
395                         get {
396                                 return comment;
397                         }
398                         set {
399                                 comment = value;
400                         }
401                 }
402
403                 // 
404                 // Returns full member name for error message
405                 //
406                 public virtual string GetSignatureForError ()
407                 {
408                         if (Parent == null || Parent.Parent == null)
409                                 return member_name.GetSignatureForError ();
410
411                         return Parent.GetSignatureForError () + "." + member_name.GetSignatureForError ();
412                 }
413
414                 /// <summary>
415                 /// Base Emit method. This is also entry point for CLS-Compliant verification.
416                 /// </summary>
417                 public virtual void Emit ()
418                 {
419                         if (!RootContext.VerifyClsCompliance)
420                                 return;
421
422                         if (Report.WarningLevel > 0)
423                                 VerifyClsCompliance ();
424                 }
425
426                 public bool IsCompilerGenerated {
427                         get     {
428                                 if ((mod_flags & Modifiers.COMPILER_GENERATED) != 0)
429                                         return true;
430
431                                 return Parent == null ? false : Parent.IsCompilerGenerated;
432                         }
433                 }
434
435                 public virtual bool IsUsed {
436                         get { return (caching_flags & Flags.IsUsed) != 0; }
437                 }
438
439                 public void SetMemberIsUsed ()
440                 {
441                         caching_flags |= Flags.IsUsed;
442                 }
443
444                 /// <summary>
445                 /// Returns instance of ObsoleteAttribute for this MemberCore
446                 /// </summary>
447                 public virtual ObsoleteAttribute GetObsoleteAttribute ()
448                 {
449                         if ((caching_flags & (Flags.Obsolete_Undetected | Flags.Obsolete)) == 0)
450                                 return null;
451
452                         caching_flags &= ~Flags.Obsolete_Undetected;
453
454                         if (OptAttributes == null || TypeManager.obsolete_attribute_type == null)
455                                 return null;
456
457                         Attribute obsolete_attr = OptAttributes.Search (TypeManager.obsolete_attribute_type);
458                         if (obsolete_attr == null)
459                                 return null;
460
461                         caching_flags |= Flags.Obsolete;
462
463                         ObsoleteAttribute obsolete = obsolete_attr.GetObsoleteAttribute ();
464                         if (obsolete == null)
465                                 return null;
466
467                         return obsolete;
468                 }
469
470                 /// <summary>
471                 /// Checks for ObsoleteAttribute presence. It's used for testing of all non-types elements
472                 /// </summary>
473                 public virtual void CheckObsoleteness (Location loc)
474                 {
475                         ObsoleteAttribute oa = GetObsoleteAttribute ();
476                         if (oa != null)
477                                 AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc);
478                 }
479
480                 // Access level of a type.
481                 const int X = 1;
482                 enum AccessLevel
483                 { // Each column represents `is this scope larger or equal to Blah scope'
484                         // Public    Assembly   Protected
485                         Protected = (0 << 0) | (0 << 1) | (X << 2),
486                         Public = (X << 0) | (X << 1) | (X << 2),
487                         Private = (0 << 0) | (0 << 1) | (0 << 2),
488                         Internal = (0 << 0) | (X << 1) | (0 << 2),
489                         ProtectedOrInternal = (0 << 0) | (X << 1) | (X << 2),
490                 }
491
492                 static AccessLevel GetAccessLevelFromModifiers (int flags)
493                 {
494                         if ((flags & Modifiers.INTERNAL) != 0) {
495
496                                 if ((flags & Modifiers.PROTECTED) != 0)
497                                         return AccessLevel.ProtectedOrInternal;
498                                 else
499                                         return AccessLevel.Internal;
500
501                         } else if ((flags & Modifiers.PROTECTED) != 0)
502                                 return AccessLevel.Protected;
503                         else if ((flags & Modifiers.PRIVATE) != 0)
504                                 return AccessLevel.Private;
505                         else
506                                 return AccessLevel.Public;
507                 }
508
509                 //
510                 // Returns the access level for type `t'
511                 //
512                 static AccessLevel GetAccessLevelFromType (Type t)
513                 {
514                         if (t.IsPublic)
515                                 return AccessLevel.Public;
516                         if (t.IsNestedPrivate)
517                                 return AccessLevel.Private;
518                         if (t.IsNotPublic)
519                                 return AccessLevel.Internal;
520
521                         if (t.IsNestedPublic)
522                                 return AccessLevel.Public;
523                         if (t.IsNestedAssembly)
524                                 return AccessLevel.Internal;
525                         if (t.IsNestedFamily)
526                                 return AccessLevel.Protected;
527                         if (t.IsNestedFamORAssem)
528                                 return AccessLevel.ProtectedOrInternal;
529                         if (t.IsNestedFamANDAssem)
530                                 throw new NotImplementedException ("NestedFamANDAssem not implemented, cant make this kind of type from c# anyways");
531
532                         // nested private is taken care of
533
534                         throw new Exception ("I give up, what are you?");
535                 }
536
537                 //
538                 // Checks whether the type P is as accessible as this member
539                 //
540                 public bool IsAccessibleAs (Type p)
541                 {
542                         //
543                         // if M is private, its accessibility is the same as this declspace.
544                         // we already know that P is accessible to T before this method, so we
545                         // may return true.
546                         //
547                         if ((mod_flags & Modifiers.PRIVATE) != 0)
548                                 return true;
549
550                         while (TypeManager.HasElementType (p))
551                                 p = TypeManager.GetElementType (p);
552
553 #if GMCS_SOURCE
554                         if (p.IsGenericParameter)
555                                 return true;
556
557                         if (TypeManager.IsGenericType (p)) {
558                                 foreach (Type t in p.GetGenericArguments ()) {
559                                         if (!IsAccessibleAs (t))
560                                                 return false;
561                                 }
562                         }
563 #endif
564
565                         for (Type p_parent = null; p != null; p = p_parent) {
566                                 p_parent = p.DeclaringType;
567                                 AccessLevel pAccess = GetAccessLevelFromType (p);
568                                 if (pAccess == AccessLevel.Public)
569                                         continue;
570
571                                 bool same_access_restrictions = false;
572                                 for (MemberCore mc = this; !same_access_restrictions && mc != null && mc.Parent != null; mc = mc.Parent) {
573                                         AccessLevel al = GetAccessLevelFromModifiers (mc.ModFlags);
574                                         switch (pAccess) {
575                                         case AccessLevel.Internal:
576                                                 if (al == AccessLevel.Private || al == AccessLevel.Internal)
577                                                         same_access_restrictions = TypeManager.IsThisOrFriendAssembly (p.Assembly);
578                                                 
579                                                 break;
580                                                 
581                                         case AccessLevel.Protected:
582                                                 if (al == AccessLevel.Protected) {
583                                                         same_access_restrictions = mc.Parent.IsBaseType (p_parent);
584                                                         break;
585                                                 }
586                                                 
587                                                 if (al == AccessLevel.Private) {
588                                                         //
589                                                         // When type is private and any of its parents derives from
590                                                         // protected type then the type is accessible
591                                                         //
592                                                         while (mc.Parent != null) {
593                                                                 if (mc.Parent.IsBaseType (p_parent))
594                                                                         same_access_restrictions = true;
595                                                                 mc = mc.Parent; 
596                                                         }
597                                                 }
598                                                 
599                                                 break;
600                                                 
601                                         case AccessLevel.ProtectedOrInternal:
602                                                 if (al == AccessLevel.Protected)
603                                                         same_access_restrictions = mc.Parent.IsBaseType (p_parent);
604                                                 else if (al == AccessLevel.Internal)
605                                                         same_access_restrictions = TypeManager.IsThisOrFriendAssembly (p.Assembly);
606                                                 else if (al == AccessLevel.ProtectedOrInternal)
607                                                         same_access_restrictions = mc.Parent.IsBaseType (p_parent) &&
608                                                                 TypeManager.IsThisOrFriendAssembly (p.Assembly);
609                                                 
610                                                 break;
611                                                 
612                                         case AccessLevel.Private:
613                                                 //
614                                                 // Both are private and share same parent
615                                                 //
616                                                 if (al == AccessLevel.Private)
617                                                         same_access_restrictions = TypeManager.IsEqual (mc.Parent.TypeBuilder, p_parent);
618                                                 
619                                                 break;
620                                                 
621                                         default:
622                                                 throw new InternalErrorException (al.ToString ());
623                                         }
624                                 }
625                                 
626                                 if (!same_access_restrictions)
627                                         return false;
628                         }
629
630                         return true;
631                 }
632
633                 /// <summary>
634                 /// Analyze whether CLS-Compliant verification must be execute for this MemberCore.
635                 /// </summary>
636                 public override bool IsClsComplianceRequired ()
637                 {
638                         if ((caching_flags & Flags.ClsCompliance_Undetected) == 0)
639                                 return (caching_flags & Flags.ClsCompliant) != 0;
640
641                         if (GetClsCompliantAttributeValue () && IsExposedFromAssembly ()) {
642                                 caching_flags &= ~Flags.ClsCompliance_Undetected;
643                                 caching_flags |= Flags.ClsCompliant;
644                                 return true;
645                         }
646
647                         caching_flags &= ~Flags.ClsCompliance_Undetected;
648                         return false;
649                 }
650
651                 /// <summary>
652                 /// Returns true when MemberCore is exposed from assembly.
653                 /// </summary>
654                 public bool IsExposedFromAssembly ()
655                 {
656                         if ((ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
657                                 return false;
658                         
659                         DeclSpace parentContainer = Parent;
660                         while (parentContainer != null && parentContainer.ModFlags != 0) {
661                                 if ((parentContainer.ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
662                                         return false;
663                                 parentContainer = parentContainer.Parent;
664                         }
665                         return true;
666                 }
667
668                 /// <summary>
669                 /// Goes through class hierarchy and gets value of first found CLSCompliantAttribute.
670                 /// If no is attribute exists then assembly CLSCompliantAttribute is returned.
671                 /// </summary>
672                 public virtual bool GetClsCompliantAttributeValue ()
673                 {
674                         if ((caching_flags & Flags.HasCompliantAttribute_Undetected) == 0)
675                                 return (caching_flags & Flags.ClsCompliantAttributeTrue) != 0;
676
677                         caching_flags &= ~Flags.HasCompliantAttribute_Undetected;
678
679                         if (OptAttributes != null && TypeManager.cls_compliant_attribute_type != null) {
680                                 Attribute cls_attribute = OptAttributes.Search (
681                                         TypeManager.cls_compliant_attribute_type);
682                                 if (cls_attribute != null) {
683                                         caching_flags |= Flags.HasClsCompliantAttribute;
684                                         bool value = cls_attribute.GetClsCompliantAttributeValue ();
685                                         if (value)
686                                                 caching_flags |= Flags.ClsCompliantAttributeTrue;
687                                         return value;
688                                 }
689                         }
690                         
691                         // It's null for TypeParameter
692                         if (Parent == null)
693                                 return false;                   
694
695                         if (Parent.GetClsCompliantAttributeValue ()) {
696                                 caching_flags |= Flags.ClsCompliantAttributeTrue;
697                                 return true;
698                         }
699                         return false;
700                 }
701
702                 /// <summary>
703                 /// Returns true if MemberCore is explicitly marked with CLSCompliantAttribute
704                 /// </summary>
705                 protected bool HasClsCompliantAttribute {
706                         get {
707                                 if ((caching_flags & Flags.HasCompliantAttribute_Undetected) != 0)
708                                         GetClsCompliantAttributeValue ();
709                                 
710                                 return (caching_flags & Flags.HasClsCompliantAttribute) != 0;
711                         }
712                 }
713
714                 /// <summary>
715                 /// Returns true when a member supports multiple overloads (methods, indexers, etc)
716                 /// </summary>
717                 public virtual bool EnableOverloadChecks (MemberCore overload)
718                 {
719                         return false;
720                 }
721
722                 /// <summary>
723                 /// The main virtual method for CLS-Compliant verifications.
724                 /// The method returns true if member is CLS-Compliant and false if member is not
725                 /// CLS-Compliant which means that CLS-Compliant tests are not necessary. A descendants override it
726                 /// and add their extra verifications.
727                 /// </summary>
728                 protected virtual bool VerifyClsCompliance ()
729                 {
730                         if (!IsClsComplianceRequired ()) {
731                                 if (HasClsCompliantAttribute && Report.WarningLevel >= 2) {
732                                         if (!IsExposedFromAssembly ())
733                                                 Report.Warning (3019, 2, Location, "CLS compliance checking will not be performed on `{0}' because it is not visible from outside this assembly", GetSignatureForError ());
734                                         if (!CodeGen.Assembly.IsClsCompliant)
735                                                 Report.Warning (3021, 2, Location, "`{0}' does not need a CLSCompliant attribute because the assembly is not marked as CLS-compliant", GetSignatureForError ());
736                                 }
737                                 return false;
738                         }
739
740                         if (HasClsCompliantAttribute) {
741                                 if (CodeGen.Assembly.ClsCompliantAttribute == null && !CodeGen.Assembly.IsClsCompliant) {
742                                         Report.Warning (3014, 1, Location,
743                                                 "`{0}' cannot be marked as CLS-compliant because the assembly is not marked as CLS-compliant",
744                                                 GetSignatureForError ());
745                                         return false;
746                                 }
747
748                                 if (!Parent.IsClsComplianceRequired ()) {
749                                         Report.Warning (3018, 1, Location, "`{0}' cannot be marked as CLS-compliant because it is a member of non CLS-compliant type `{1}'", 
750                                                 GetSignatureForError (), Parent.GetSignatureForError ());
751                                         return false;
752                                 }
753                         }
754
755                         if (member_name.Name [0] == '_') {
756                                 Report.Warning (3008, 1, Location, "Identifier `{0}' is not CLS-compliant", GetSignatureForError () );
757                         }
758                         return true;
759                 }
760
761                 //
762                 // Raised (and passed an XmlElement that contains the comment)
763                 // when GenerateDocComment is writing documentation expectedly.
764                 //
765                 internal virtual void OnGenerateDocComment (XmlElement intermediateNode)
766                 {
767                 }
768
769                 //
770                 // Returns a string that represents the signature for this 
771                 // member which should be used in XML documentation.
772                 //
773                 public virtual string GetDocCommentName (DeclSpace ds)
774                 {
775                         if (ds == null || this is DeclSpace)
776                                 return DocCommentHeader + Name;
777                         else
778                                 return String.Concat (DocCommentHeader, ds.Name, ".", Name);
779                 }
780
781                 //
782                 // Generates xml doc comments (if any), and if required,
783                 // handle warning report.
784                 //
785                 internal virtual void GenerateDocComment (DeclSpace ds)
786                 {
787                         try {
788                                 DocUtil.GenerateDocComment (this, ds);
789                         } catch (Exception e) {
790                                 throw new InternalErrorException (this, e);
791                         }
792                 }
793
794                 public override IResolveContext ResolveContext {
795                         get { return this; }
796                 }
797
798                 #region IResolveContext Members
799
800                 public DeclSpace DeclContainer {
801                         get { return Parent; }
802                 }
803
804                 public virtual DeclSpace GenericDeclContainer {
805                         get { return DeclContainer; }
806                 }
807
808                 public bool IsInObsoleteScope {
809                         get {
810                                 if (GetObsoleteAttribute () != null)
811                                         return true;
812
813                                 return Parent == null ? false : Parent.IsInObsoleteScope;
814                         }
815                 }
816
817                 public bool IsInUnsafeScope {
818                         get {
819                                 if ((ModFlags & Modifiers.UNSAFE) != 0)
820                                         return true;
821
822                                 return Parent == null ? false : Parent.IsInUnsafeScope;
823                         }
824                 }
825
826                 #endregion
827         }
828
829         /// <summary>
830         ///   Base class for structs, classes, enumerations and interfaces.  
831         /// </summary>
832         /// <remarks>
833         ///   They all create new declaration spaces.  This
834         ///   provides the common foundation for managing those name
835         ///   spaces.
836         /// </remarks>
837         public abstract class DeclSpace : MemberCore {
838                 /// <summary>
839                 ///   This points to the actual definition that is being
840                 ///   created with System.Reflection.Emit
841                 /// </summary>
842                 public TypeBuilder TypeBuilder;
843
844                 /// <summary>
845                 ///   If we are a generic type, this is the type we are
846                 ///   currently defining.  We need to lookup members on this
847                 ///   instead of the TypeBuilder.
848                 /// </summary>
849                 public Type CurrentType;
850
851                 //
852                 // This is the namespace in which this typecontainer
853                 // was declared.  We use this to resolve names.
854                 //
855                 public NamespaceEntry NamespaceEntry;
856
857                 private Hashtable Cache = new Hashtable ();
858                 
859                 public readonly string Basename;
860                 
861                 protected Hashtable defined_names;
862
863                 public TypeContainer PartialContainer;          
864
865                 protected readonly bool is_generic;
866                 readonly int count_type_params;
867                 protected TypeParameter[] type_params;
868                 TypeParameter[] type_param_list;
869
870                 //
871                 // Whether we are Generic
872                 //
873                 public bool IsGeneric {
874                         get {
875                                 if (is_generic)
876                                         return true;
877                                 else if (Parent != null)
878                                         return Parent.IsGeneric;
879                                 else
880                                         return false;
881                         }
882                 }
883
884                 static string[] attribute_targets = new string [] { "type" };
885
886                 public DeclSpace (NamespaceEntry ns, DeclSpace parent, MemberName name,
887                                   Attributes attrs)
888                         : base (parent, name, attrs)
889                 {
890                         NamespaceEntry = ns;
891                         Basename = name.Basename;
892                         defined_names = new Hashtable ();
893                         PartialContainer = null;
894                         if (name.TypeArguments != null) {
895                                 is_generic = true;
896                                 count_type_params = name.TypeArguments.Count;
897                         }
898                         if (parent != null)
899                                 count_type_params += parent.count_type_params;
900                 }
901
902                 public override DeclSpace GenericDeclContainer {
903                         get { return this; }
904                 }
905
906                 /// <summary>
907                 /// Adds the member to defined_names table. It tests for duplications and enclosing name conflicts
908                 /// </summary>
909                 protected virtual bool AddToContainer (MemberCore symbol, string name)
910                 {
911                         MemberCore mc = (MemberCore) defined_names [name];
912
913                         if (mc == null) {
914                                 defined_names.Add (name, symbol);
915                                 return true;
916                         }
917
918                         if (symbol.EnableOverloadChecks (mc))
919                                 return true;
920
921                         Report.SymbolRelatedToPreviousError (mc);
922                         if ((mc.ModFlags & Modifiers.PARTIAL) != 0 && (symbol is ClassOrStruct || symbol is Interface)) {
923                                 Error_MissingPartialModifier (symbol);
924                                 return false;
925                         }
926
927                         if (this is RootTypes) {
928                                 Report.Error (101, symbol.Location, 
929                                         "The namespace `{0}' already contains a definition for `{1}'",
930                                         ((DeclSpace)symbol).NamespaceEntry.GetSignatureForError (), symbol.MemberName.Name);
931                         } else if (symbol is TypeParameter) {
932                                 Report.Error (692, symbol.Location,
933                                         "Duplicate type parameter `{0}'", symbol.GetSignatureForError ());
934                         } else {
935                                 Report.Error (102, symbol.Location,
936                                               "The type `{0}' already contains a definition for `{1}'",
937                                               GetSignatureForError (), symbol.MemberName.Name);
938                         }
939
940                         return false;
941                 }
942
943                 protected void RemoveFromContainer (string name)
944                 {
945                         defined_names.Remove (name);
946                 }
947                 
948                 /// <summary>
949                 ///   Returns the MemberCore associated with a given name in the declaration
950                 ///   space. It doesn't return method based symbols !!
951                 /// </summary>
952                 /// 
953                 public MemberCore GetDefinition (string name)
954                 {
955                         return (MemberCore)defined_names [name];
956                 }
957
958                 public bool IsStaticClass {
959                         get { return (ModFlags & Modifiers.STATIC) != 0; }
960                 }
961                 
962                 // 
963                 // root_types contains all the types.  All TopLevel types
964                 // hence have a parent that points to `root_types', that is
965                 // why there is a non-obvious test down here.
966                 //
967                 public bool IsTopLevel {
968                         get { return (Parent != null && Parent.Parent == null); }
969                 }
970
971                 public virtual bool IsUnmanagedType ()
972                 {
973                         return false;
974                 }
975
976                 public virtual void CloseType ()
977                 {
978                         if ((caching_flags & Flags.CloseTypeCreated) == 0){
979                                 try {
980                                         TypeBuilder.CreateType ();
981                                 } catch {
982                                         //
983                                         // The try/catch is needed because
984                                         // nested enumerations fail to load when they
985                                         // are defined.
986                                         //
987                                         // Even if this is the right order (enumerations
988                                         // declared after types).
989                                         //
990                                         // Note that this still creates the type and
991                                         // it is possible to save it
992                                 }
993                                 caching_flags |= Flags.CloseTypeCreated;
994                         }
995                 }
996
997                 protected virtual TypeAttributes TypeAttr {
998                         get { return CodeGen.Module.DefaultCharSetType; }
999                 }
1000
1001                 /// <remarks>
1002                 ///  Should be overriten by the appropriate declaration space
1003                 /// </remarks>
1004                 public abstract TypeBuilder DefineType ();
1005
1006                 protected void Error_MissingPartialModifier (MemberCore type)
1007                 {
1008                         Report.Error (260, type.Location,
1009                                 "Missing partial modifier on declaration of type `{0}'. Another partial declaration of this type exists",
1010                                 type.GetSignatureForError ());
1011                 }
1012
1013                 public override void Emit ()
1014                 {
1015 #if GMCS_SOURCE
1016                         if (type_params != null) {
1017                                 int offset = count_type_params - type_params.Length;
1018                                 for (int i = offset; i < type_params.Length; i++)
1019                                         CurrentTypeParameters [i - offset].Emit ();
1020                         }
1021
1022                         if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
1023                                 TypeBuilder.SetCustomAttribute (TypeManager.GetCompilerGeneratedAttribute (Location));
1024 #endif
1025
1026                         base.Emit ();
1027                 }
1028
1029                 public override string GetSignatureForError ()
1030                 {       
1031                         return MemberName.GetSignatureForError ();
1032                 }
1033                 
1034                 public bool CheckAccessLevel (Type check_type)
1035                 {
1036                         TypeBuilder tb = TypeBuilder;
1037 #if GMCS_SOURCE
1038                         if (this is GenericMethod) {
1039                                 tb = Parent.TypeBuilder;
1040
1041                                 // FIXME: Generic container does not work with nested generic
1042                                 // anonymous method stories
1043                                 if (TypeBuilder == null)
1044                                         return true;
1045                         }
1046 #endif
1047
1048                         check_type = TypeManager.DropGenericTypeArguments (check_type);
1049                         if (check_type == tb)
1050                                 return true;
1051
1052                         //
1053                         // Broken Microsoft runtime, return public for arrays, no matter what 
1054                         // the accessibility is for their underlying class, and they return 
1055                         // NonPublic visibility for pointers
1056                         //
1057                         if (check_type.IsArray || check_type.IsPointer)
1058                                 return CheckAccessLevel (TypeManager.GetElementType (check_type));
1059
1060                         TypeAttributes check_attr = check_type.Attributes & TypeAttributes.VisibilityMask;
1061
1062                         switch (check_attr){
1063                         case TypeAttributes.Public:
1064                                 return true;
1065
1066                         case TypeAttributes.NotPublic:
1067                                 return TypeManager.IsThisOrFriendAssembly (check_type.Assembly);
1068                                 
1069                         case TypeAttributes.NestedPublic:
1070                                 return CheckAccessLevel (check_type.DeclaringType);
1071
1072                         case TypeAttributes.NestedPrivate:
1073                                 Type declaring = check_type.DeclaringType;
1074                                 return tb == declaring || TypeManager.IsNestedChildOf (tb, declaring);  
1075
1076                         case TypeAttributes.NestedFamily:
1077                                 //
1078                                 // Only accessible to methods in current type or any subtypes
1079                                 //
1080                                 return FamilyAccessible (tb, check_type);
1081
1082                         case TypeAttributes.NestedFamANDAssem:
1083                                 return TypeManager.IsThisOrFriendAssembly (check_type.Assembly) && 
1084                                         FamilyAccessible (tb, check_type);
1085
1086                         case TypeAttributes.NestedFamORAssem:
1087                                 return FamilyAccessible (tb, check_type) ||
1088                                         TypeManager.IsThisOrFriendAssembly (check_type.Assembly);
1089
1090                         case TypeAttributes.NestedAssembly:
1091                                 return TypeManager.IsThisOrFriendAssembly (check_type.Assembly);
1092                         }
1093
1094                         throw new NotImplementedException (check_attr.ToString ());
1095                 }
1096
1097                 static bool FamilyAccessible (Type tb, Type check_type)
1098                 {
1099                         Type declaring = check_type.DeclaringType;
1100                         return TypeManager.IsNestedFamilyAccessible (tb, declaring);
1101                 }
1102
1103                 public bool IsBaseType (Type baseType)
1104                 {
1105                         if (TypeManager.IsInterfaceType (baseType))
1106                                 throw new NotImplementedException ();
1107
1108                         Type type = TypeBuilder;
1109                         while (type != null) {
1110                                 if (TypeManager.IsEqual (type, baseType))
1111                                         return true;
1112
1113                                 type = type.BaseType;
1114                         }
1115
1116                         return false;
1117                 }
1118
1119                 private Type LookupNestedTypeInHierarchy (string name)
1120                 {
1121                         Type t = null;
1122                         // if the member cache has been created, lets use it.
1123                         // the member cache is MUCH faster.
1124                         if (MemberCache != null) {
1125                                 t = MemberCache.FindNestedType (name);
1126                                 if (t == null)
1127                                         return null;
1128                                 
1129                         //
1130                         // FIXME: This hack is needed because member cache does not work
1131                         // with nested base generic types, it does only type name copy and
1132                         // not type construction
1133                         //
1134 #if !GMCS_SOURCE
1135                                 return t;
1136 #endif                          
1137                         }
1138
1139                         // no member cache. Do it the hard way -- reflection
1140                         for (Type current_type = TypeBuilder;
1141                              current_type != null && current_type != TypeManager.object_type;
1142                              current_type = current_type.BaseType) {
1143
1144                                 Type ct = TypeManager.DropGenericTypeArguments (current_type);
1145                                 if (ct is TypeBuilder) {
1146                                         TypeContainer tc = ct == TypeBuilder
1147                                                 ? PartialContainer : TypeManager.LookupTypeContainer (ct);
1148                                         if (tc != null)
1149                                                 t = tc.FindNestedType (name);
1150                                 } else {
1151                                         t = TypeManager.GetNestedType (ct, name);
1152                                 }
1153
1154                                 if ((t == null) || !CheckAccessLevel (t))
1155                                         continue;
1156
1157 #if GMCS_SOURCE
1158                                 if (!TypeManager.IsGenericType (current_type))
1159                                         return t;
1160
1161                                 Type[] args = TypeManager.GetTypeArguments (current_type);
1162                                 Type[] targs = TypeManager.GetTypeArguments (t);
1163                                 for (int i = 0; i < args.Length; i++)
1164                                         targs [i] = args [i];
1165
1166                                 t = t.MakeGenericType (targs);
1167 #endif
1168
1169                                 return t;
1170                         }
1171
1172                         return null;
1173                 }
1174
1175                 public virtual ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, string name, Location loc)
1176                 {
1177                         return null;
1178                 }
1179
1180                 //
1181                 // Public function used to locate types.
1182                 //
1183                 // Set 'ignore_cs0104' to true if you want to ignore cs0104 errors.
1184                 //
1185                 // Returns: Type or null if they type can not be found.
1186                 //
1187                 public FullNamedExpression LookupNamespaceOrType (string name, Location loc, bool ignore_cs0104)
1188                 {
1189                         if (Cache.Contains (name))
1190                                 return (FullNamedExpression) Cache [name];
1191
1192                         FullNamedExpression e;
1193                         int errors = Report.Errors;
1194                         Type t = LookupNestedTypeInHierarchy (name);
1195                         if (t != null)
1196                                 e = new TypeExpression (t, Location.Null);
1197                         else if (Parent != null)
1198                                 e = Parent.LookupNamespaceOrType (name, loc, ignore_cs0104);
1199                         else
1200                                 e = NamespaceEntry.LookupNamespaceOrType (this, name, loc, ignore_cs0104);
1201
1202                         if (errors == Report.Errors)
1203                                 Cache [name] = e;
1204                         
1205                         return e;
1206                 }
1207
1208                 /// <remarks>
1209                 ///   This function is broken and not what you're looking for.  It should only
1210                 ///   be used while the type is still being created since it doesn't use the cache
1211                 ///   and relies on the filter doing the member name check.
1212                 /// </remarks>
1213                 ///
1214                 // [Obsolete ("Only MemberCache approach should be used")]
1215                 public virtual MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1216                                                         MemberFilter filter, object criteria)
1217                 {
1218                         throw new NotSupportedException ();
1219                 }
1220
1221                 /// <remarks>
1222                 ///   If we have a MemberCache, return it.  This property may return null if the
1223                 ///   class doesn't have a member cache or while it's still being created.
1224                 /// </remarks>
1225                 public abstract MemberCache MemberCache {
1226                         get;
1227                 }
1228
1229                 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
1230                 {
1231                         if (a.Type == TypeManager.required_attr_type) {
1232                                 Report.Error (1608, a.Location, "The RequiredAttribute attribute is not permitted on C# types");
1233                                 return;
1234                         }
1235                         TypeBuilder.SetCustomAttribute (cb);
1236                 }
1237
1238                 TypeParameter[] initialize_type_params ()
1239                 {
1240                         if (type_param_list != null)
1241                                 return type_param_list;
1242
1243                         DeclSpace the_parent = Parent;
1244                         if (this is GenericMethod)
1245                                 the_parent = null;
1246
1247                         ArrayList list = new ArrayList ();
1248                         if (the_parent != null && the_parent.IsGeneric) {
1249                                 // FIXME: move generics info out of DeclSpace
1250                                 TypeParameter[] parent_params = the_parent.PartialContainer.TypeParameters;
1251                                 list.AddRange (parent_params);
1252                         }
1253  
1254                         int count = type_params != null ? type_params.Length : 0;
1255                         for (int i = 0; i < count; i++) {
1256                                 TypeParameter param = type_params [i];
1257                                 list.Add (param);
1258                                 if (Parent.IsGeneric) {
1259                                         foreach (TypeParameter tp in Parent.PartialContainer.CurrentTypeParameters) {
1260                                                 if (tp.Name != param.Name)                              
1261                                                         continue;
1262
1263                                                 Report.SymbolRelatedToPreviousError (tp.Location, null);
1264                                                 Report.Warning (693, 3, param.Location,
1265                                                         "Type parameter `{0}' has the same name as the type parameter from outer type `{1}'",
1266                                                         param.Name, Parent.GetSignatureForError ());
1267                                         }
1268                                 }
1269                         }
1270
1271                         type_param_list = new TypeParameter [list.Count];
1272                         list.CopyTo (type_param_list, 0);
1273                         return type_param_list;
1274                 }
1275
1276                 public virtual void SetParameterInfo (ArrayList constraints_list)
1277                 {
1278                         if (!is_generic) {
1279                                 if (constraints_list != null) {
1280                                         Report.Error (
1281                                                 80, Location, "Constraints are not allowed " +
1282                                                 "on non-generic declarations");
1283                                 }
1284
1285                                 return;
1286                         }
1287
1288                         TypeParameterName[] names = MemberName.TypeArguments.GetDeclarations ();
1289                         type_params = new TypeParameter [names.Length];
1290
1291                         //
1292                         // Register all the names
1293                         //
1294                         for (int i = 0; i < type_params.Length; i++) {
1295                                 TypeParameterName name = names [i];
1296
1297                                 Constraints constraints = null;
1298                                 if (constraints_list != null) {
1299                                         int total = constraints_list.Count;
1300                                         for (int ii = 0; ii < total; ++ii) {
1301                                                 Constraints constraints_at = (Constraints)constraints_list[ii];
1302                                                 // TODO: it is used by iterators only
1303                                                 if (constraints_at == null) {
1304                                                         constraints_list.RemoveAt (ii);
1305                                                         --total;
1306                                                         continue;
1307                                                 }
1308                                                 if (constraints_at.TypeParameter == name.Name) {
1309                                                         constraints = constraints_at;
1310                                                         constraints_list.RemoveAt(ii);
1311                                                         break;
1312                                                 }
1313                                         }
1314                                 }
1315
1316                                 type_params [i] = new TypeParameter (
1317                                         Parent, this, name.Name, constraints, name.OptAttributes,
1318                                         Location);
1319
1320                                 AddToContainer (type_params [i], name.Name);
1321                         }
1322
1323                         if (constraints_list != null && constraints_list.Count > 0) {
1324                                 foreach (Constraints constraint in constraints_list) {
1325                                         Report.Error(699, constraint.Location, "`{0}': A constraint references nonexistent type parameter `{1}'", 
1326                                                 GetSignatureForError (), constraint.TypeParameter);
1327                                 }
1328                         }
1329                 }
1330
1331                 public TypeParameter[] TypeParameters {
1332                         get {
1333                                 if (!IsGeneric)
1334                                         throw new InvalidOperationException ();
1335                                 if ((PartialContainer != null) && (PartialContainer != this))
1336                                         return PartialContainer.TypeParameters;
1337                                 if (type_param_list == null)
1338                                         initialize_type_params ();
1339
1340                                 return type_param_list;
1341                         }
1342                 }
1343
1344                 public TypeParameter[] CurrentTypeParameters {
1345                         get {
1346                                 if (!IsGeneric)
1347                                         throw new InvalidOperationException ();
1348
1349                                 // TODO: Something is seriously broken here
1350                                 if (type_params == null)
1351                                         return new TypeParameter [0];
1352
1353                                 return type_params;
1354                         }
1355                 }
1356
1357                 public int CountTypeParameters {
1358                         get {
1359                                 return count_type_params;
1360                         }
1361                 }
1362
1363                 public TypeParameterExpr LookupGeneric (string name, Location loc)
1364                 {
1365                         if (!IsGeneric)
1366                                 return null;
1367
1368                         TypeParameter [] current_params;
1369                         if (this is TypeContainer)
1370                                 current_params = PartialContainer.CurrentTypeParameters;
1371                         else
1372                                 current_params = CurrentTypeParameters;
1373
1374                         foreach (TypeParameter type_param in current_params) {
1375                                 if (type_param.Name == name)
1376                                         return new TypeParameterExpr (type_param, loc);
1377                         }
1378
1379                         if (Parent != null)
1380                                 return Parent.LookupGeneric (name, loc);
1381
1382                         return null;
1383                 }
1384
1385                 // Used for error reporting only
1386                 public virtual Type LookupAnyGeneric (string typeName)
1387                 {
1388                         return NamespaceEntry.NS.LookForAnyGenericType (typeName);
1389                 }
1390
1391                 public override string[] ValidAttributeTargets {
1392                         get { return attribute_targets; }
1393                 }
1394
1395                 protected override bool VerifyClsCompliance ()
1396                 {
1397                         if (!base.VerifyClsCompliance ()) {
1398                                 return false;
1399                         }
1400
1401                         if (type_params != null) {
1402                                 foreach (TypeParameter tp in type_params) {
1403                                         if (tp.Constraints == null)
1404                                                 continue;
1405
1406                                         tp.Constraints.VerifyClsCompliance ();
1407                                 }
1408                         }
1409
1410                         IDictionary cache = TypeManager.AllClsTopLevelTypes;
1411                         if (cache == null)
1412                                 return true;
1413
1414                         string lcase = Name.ToLower (System.Globalization.CultureInfo.InvariantCulture);
1415                         if (!cache.Contains (lcase)) {
1416                                 cache.Add (lcase, this);
1417                                 return true;
1418                         }
1419
1420                         object val = cache [lcase];
1421                         if (val == null) {
1422                                 Type t = AttributeTester.GetImportedIgnoreCaseClsType (lcase);
1423                                 if (t == null)
1424                                         return true;
1425                                 Report.SymbolRelatedToPreviousError (t);
1426                         }
1427                         else {
1428                                 Report.SymbolRelatedToPreviousError ((DeclSpace)val);
1429                         }
1430
1431                         Report.Warning (3005, 1, Location, "Identifier `{0}' differing only in case is not CLS-compliant", GetSignatureForError ());
1432                         return true;
1433                 }
1434         }
1435
1436         /// <summary>
1437         ///   This is a readonly list of MemberInfo's.      
1438         /// </summary>
1439         public class MemberList : IList {
1440                 public readonly IList List;
1441                 int count;
1442
1443                 /// <summary>
1444                 ///   Create a new MemberList from the given IList.
1445                 /// </summary>
1446                 public MemberList (IList list)
1447                 {
1448                         if (list != null)
1449                                 this.List = list;
1450                         else
1451                                 this.List = new ArrayList ();
1452                         count = List.Count;
1453                 }
1454
1455                 /// <summary>
1456                 ///   Concatenate the ILists `first' and `second' to a new MemberList.
1457                 /// </summary>
1458                 public MemberList (IList first, IList second)
1459                 {
1460                         ArrayList list = new ArrayList ();
1461                         list.AddRange (first);
1462                         list.AddRange (second);
1463                         count = list.Count;
1464                         List = list;
1465                 }
1466
1467                 public static readonly MemberList Empty = new MemberList (new ArrayList (0));
1468
1469                 /// <summary>
1470                 ///   Cast the MemberList into a MemberInfo[] array.
1471                 /// </summary>
1472                 /// <remarks>
1473                 ///   This is an expensive operation, only use it if it's really necessary.
1474                 /// </remarks>
1475                 public static explicit operator MemberInfo [] (MemberList list)
1476                 {
1477                         Timer.StartTimer (TimerType.MiscTimer);
1478                         MemberInfo [] result = new MemberInfo [list.Count];
1479                         list.CopyTo (result, 0);
1480                         Timer.StopTimer (TimerType.MiscTimer);
1481                         return result;
1482                 }
1483
1484                 // ICollection
1485
1486                 public int Count {
1487                         get {
1488                                 return count;
1489                         }
1490                 }
1491
1492                 public bool IsSynchronized {
1493                         get {
1494                                 return List.IsSynchronized;
1495                         }
1496                 }
1497
1498                 public object SyncRoot {
1499                         get {
1500                                 return List.SyncRoot;
1501                         }
1502                 }
1503
1504                 public void CopyTo (Array array, int index)
1505                 {
1506                         List.CopyTo (array, index);
1507                 }
1508
1509                 // IEnumerable
1510
1511                 public IEnumerator GetEnumerator ()
1512                 {
1513                         return List.GetEnumerator ();
1514                 }
1515
1516                 // IList
1517
1518                 public bool IsFixedSize {
1519                         get {
1520                                 return true;
1521                         }
1522                 }
1523
1524                 public bool IsReadOnly {
1525                         get {
1526                                 return true;
1527                         }
1528                 }
1529
1530                 object IList.this [int index] {
1531                         get {
1532                                 return List [index];
1533                         }
1534
1535                         set {
1536                                 throw new NotSupportedException ();
1537                         }
1538                 }
1539
1540                 // FIXME: try to find out whether we can avoid the cast in this indexer.
1541                 public MemberInfo this [int index] {
1542                         get {
1543                                 return (MemberInfo) List [index];
1544                         }
1545                 }
1546
1547                 public int Add (object value)
1548                 {
1549                         throw new NotSupportedException ();
1550                 }
1551
1552                 public void Clear ()
1553                 {
1554                         throw new NotSupportedException ();
1555                 }
1556
1557                 public bool Contains (object value)
1558                 {
1559                         return List.Contains (value);
1560                 }
1561
1562                 public int IndexOf (object value)
1563                 {
1564                         return List.IndexOf (value);
1565                 }
1566
1567                 public void Insert (int index, object value)
1568                 {
1569                         throw new NotSupportedException ();
1570                 }
1571
1572                 public void Remove (object value)
1573                 {
1574                         throw new NotSupportedException ();
1575                 }
1576
1577                 public void RemoveAt (int index)
1578                 {
1579                         throw new NotSupportedException ();
1580                 }
1581         }
1582
1583         /// <summary>
1584         ///   This interface is used to get all members of a class when creating the
1585         ///   member cache.  It must be implemented by all DeclSpace derivatives which
1586         ///   want to support the member cache and by TypeHandle to get caching of
1587         ///   non-dynamic types.
1588         /// </summary>
1589         public interface IMemberContainer {
1590                 /// <summary>
1591                 ///   The name of the IMemberContainer.  This is only used for
1592                 ///   debugging purposes.
1593                 /// </summary>
1594                 string Name {
1595                         get;
1596                 }
1597
1598                 /// <summary>
1599                 ///   The type of this IMemberContainer.
1600                 /// </summary>
1601                 Type Type {
1602                         get;
1603                 }
1604
1605                 /// <summary>
1606                 ///   Returns the IMemberContainer of the base class or null if this
1607                 ///   is an interface or TypeManger.object_type.
1608                 ///   This is used when creating the member cache for a class to get all
1609                 ///   members from the base class.
1610                 /// </summary>
1611                 MemberCache BaseCache {
1612                         get;
1613                 }
1614
1615                 /// <summary>
1616                 ///   Whether this is an interface.
1617                 /// </summary>
1618                 bool IsInterface {
1619                         get;
1620                 }
1621
1622                 /// <summary>
1623                 ///   Returns all members of this class with the corresponding MemberTypes
1624                 ///   and BindingFlags.
1625                 /// </summary>
1626                 /// <remarks>
1627                 ///   When implementing this method, make sure not to return any inherited
1628                 ///   members and check the MemberTypes and BindingFlags properly.
1629                 ///   Unfortunately, System.Reflection is lame and doesn't provide a way to
1630                 ///   get the BindingFlags (static/non-static,public/non-public) in the
1631                 ///   MemberInfo class, but the cache needs this information.  That's why
1632                 ///   this method is called multiple times with different BindingFlags.
1633                 /// </remarks>
1634                 MemberList GetMembers (MemberTypes mt, BindingFlags bf);
1635         }
1636
1637         /// <summary>
1638         ///   The MemberCache is used by dynamic and non-dynamic types to speed up
1639         ///   member lookups.  It has a member name based hash table; it maps each member
1640         ///   name to a list of CacheEntry objects.  Each CacheEntry contains a MemberInfo
1641         ///   and the BindingFlags that were initially used to get it.  The cache contains
1642         ///   all members of the current class and all inherited members.  If this cache is
1643         ///   for an interface types, it also contains all inherited members.
1644         ///
1645         ///   There are two ways to get a MemberCache:
1646         ///   * if this is a dynamic type, lookup the corresponding DeclSpace and then
1647         ///     use the DeclSpace.MemberCache property.
1648         ///   * if this not a dynamic type, call TypeHandle.GetTypeHandle() to get a
1649         ///     TypeHandle instance for the type and then use TypeHandle.MemberCache.
1650         /// </summary>
1651         public class MemberCache {
1652                 public readonly IMemberContainer Container;
1653                 protected Hashtable member_hash;
1654                 protected Hashtable method_hash;
1655
1656                 /// <summary>
1657                 ///   Create a new MemberCache for the given IMemberContainer `container'.
1658                 /// </summary>
1659                 public MemberCache (IMemberContainer container)
1660                 {
1661                         this.Container = container;
1662
1663                         Timer.IncrementCounter (CounterType.MemberCache);
1664                         Timer.StartTimer (TimerType.CacheInit);
1665
1666                         // If we have a base class (we have a base class unless we're
1667                         // TypeManager.object_type), we deep-copy its MemberCache here.
1668                         if (Container.BaseCache != null)
1669                                 member_hash = SetupCache (Container.BaseCache);
1670                         else
1671                                 member_hash = new Hashtable ();
1672
1673                         // If this is neither a dynamic type nor an interface, create a special
1674                         // method cache with all declared and inherited methods.
1675                         Type type = container.Type;
1676                         if (!(type is TypeBuilder) && !type.IsInterface &&
1677                             // !(type.IsGenericType && (type.GetGenericTypeDefinition () is TypeBuilder)) &&
1678                             !TypeManager.IsGenericType (type) && !TypeManager.IsGenericParameter (type) &&
1679                             (Container.BaseCache == null || Container.BaseCache.method_hash != null)) {
1680                                 method_hash = new Hashtable ();
1681                                 AddMethods (type);
1682                         }
1683
1684                         // Add all members from the current class.
1685                         AddMembers (Container);
1686
1687                         Timer.StopTimer (TimerType.CacheInit);
1688                 }
1689
1690                 public MemberCache (Type baseType, IMemberContainer container)
1691                 {
1692                         this.Container = container;
1693                         if (baseType == null)
1694                                 this.member_hash = new Hashtable ();
1695                         else
1696                                 this.member_hash = SetupCache (TypeManager.LookupMemberCache (baseType));
1697                 }
1698
1699                 public MemberCache (Type[] ifaces)
1700                 {
1701                         //
1702                         // The members of this cache all belong to other caches.  
1703                         // So, 'Container' will not be used.
1704                         //
1705                         this.Container = null;
1706
1707                         member_hash = new Hashtable ();
1708                         if (ifaces == null)
1709                                 return;
1710
1711                         foreach (Type itype in ifaces)
1712                                 AddCacheContents (TypeManager.LookupMemberCache (itype));
1713                 }
1714
1715                 public MemberCache (IMemberContainer container, Type base_class, Type[] ifaces)
1716                 {
1717                         this.Container = container;
1718
1719                         // If we have a base class (we have a base class unless we're
1720                         // TypeManager.object_type), we deep-copy its MemberCache here.
1721                         if (Container.BaseCache != null)
1722                                 member_hash = SetupCache (Container.BaseCache);
1723                         else
1724                                 member_hash = new Hashtable ();
1725
1726                         if (base_class != null)
1727                                 AddCacheContents (TypeManager.LookupMemberCache (base_class));
1728                         if (ifaces != null) {
1729                                 foreach (Type itype in ifaces) {
1730                                         MemberCache cache = TypeManager.LookupMemberCache (itype);
1731                                         if (cache != null)
1732                                                 AddCacheContents (cache);
1733                                 }
1734                         }
1735                 }
1736
1737                 /// <summary>
1738                 ///   Bootstrap this member cache by doing a deep-copy of our base.
1739                 /// </summary>
1740                 static Hashtable SetupCache (MemberCache base_class)
1741                 {
1742                         if (base_class == null)
1743                                 return new Hashtable ();
1744
1745                         Hashtable hash = new Hashtable (base_class.member_hash.Count);
1746                         IDictionaryEnumerator it = base_class.member_hash.GetEnumerator ();
1747                         while (it.MoveNext ()) {
1748                                 hash.Add (it.Key, ((ArrayList) it.Value).Clone ());
1749                          }
1750                                 
1751                         return hash;
1752                 }
1753                 
1754                 //
1755                 // Converts ModFlags to BindingFlags
1756                 //
1757                 static BindingFlags GetBindingFlags (int modifiers)
1758                 {
1759                         BindingFlags bf;
1760                         if ((modifiers & Modifiers.STATIC) != 0)
1761                                 bf = BindingFlags.Static;
1762                         else
1763                                 bf = BindingFlags.Instance;
1764
1765                         if ((modifiers & Modifiers.PRIVATE) != 0)
1766                                 bf |= BindingFlags.NonPublic;
1767                         else
1768                                 bf |= BindingFlags.Public;
1769
1770                         return bf;
1771                 }               
1772
1773                 /// <summary>
1774                 ///   Add the contents of `cache' to the member_hash.
1775                 /// </summary>
1776                 void AddCacheContents (MemberCache cache)
1777                 {
1778                         IDictionaryEnumerator it = cache.member_hash.GetEnumerator ();
1779                         while (it.MoveNext ()) {
1780                                 ArrayList list = (ArrayList) member_hash [it.Key];
1781                                 if (list == null)
1782                                         member_hash [it.Key] = list = new ArrayList ();
1783
1784                                 ArrayList entries = (ArrayList) it.Value;
1785                                 for (int i = entries.Count-1; i >= 0; i--) {
1786                                         CacheEntry entry = (CacheEntry) entries [i];
1787
1788                                         if (entry.Container != cache.Container)
1789                                                 break;
1790                                         list.Add (entry);
1791                                 }
1792                         }
1793                 }
1794
1795                 /// <summary>
1796                 ///   Add all members from class `container' to the cache.
1797                 /// </summary>
1798                 void AddMembers (IMemberContainer container)
1799                 {
1800                         // We need to call AddMembers() with a single member type at a time
1801                         // to get the member type part of CacheEntry.EntryType right.
1802                         if (!container.IsInterface) {
1803                                 AddMembers (MemberTypes.Constructor, container);
1804                                 AddMembers (MemberTypes.Field, container);
1805                         }
1806                         AddMembers (MemberTypes.Method, container);
1807                         AddMembers (MemberTypes.Property, container);
1808                         AddMembers (MemberTypes.Event, container);
1809                         // Nested types are returned by both Static and Instance searches.
1810                         AddMembers (MemberTypes.NestedType,
1811                                     BindingFlags.Static | BindingFlags.Public, container);
1812                         AddMembers (MemberTypes.NestedType,
1813                                     BindingFlags.Static | BindingFlags.NonPublic, container);
1814                 }
1815
1816                 void AddMembers (MemberTypes mt, IMemberContainer container)
1817                 {
1818                         AddMembers (mt, BindingFlags.Static | BindingFlags.Public, container);
1819                         AddMembers (mt, BindingFlags.Static | BindingFlags.NonPublic, container);
1820                         AddMembers (mt, BindingFlags.Instance | BindingFlags.Public, container);
1821                         AddMembers (mt, BindingFlags.Instance | BindingFlags.NonPublic, container);
1822                 }
1823
1824                 public void AddMember (MemberInfo mi, MemberCore mc)
1825                 {
1826                         AddMember (mi.MemberType, GetBindingFlags (mc.ModFlags), Container, mi.Name, mi);
1827                 }
1828
1829                 public void AddGenericMember (MemberInfo mi, InterfaceMemberBase mc)
1830                 {
1831                         AddMember (mi.MemberType, GetBindingFlags (mc.ModFlags), Container,
1832                                 MemberName.MakeName (mc.GetFullName (mc.MemberName), mc.MemberName.TypeArguments), mi);
1833                 }
1834
1835                 public void AddNestedType (DeclSpace type)
1836                 {
1837                         AddMember (MemberTypes.NestedType, GetBindingFlags (type.ModFlags), (IMemberContainer) type.Parent,
1838                                 type.TypeBuilder.Name, type.TypeBuilder);
1839                 }
1840
1841                 public void AddInterface (MemberCache baseCache)
1842                 {
1843                         if (baseCache.member_hash.Count > 0)
1844                                 AddCacheContents (baseCache);
1845                 }
1846
1847                 void AddMember (MemberTypes mt, BindingFlags bf, IMemberContainer container,
1848                                 string name, MemberInfo member)
1849                 {
1850                         // We use a name-based hash table of ArrayList's.
1851                         ArrayList list = (ArrayList) member_hash [name];
1852                         if (list == null) {
1853                                 list = new ArrayList (1);
1854                                 member_hash.Add (name, list);
1855                         }
1856
1857                         // When this method is called for the current class, the list will
1858                         // already contain all inherited members from our base classes.
1859                         // We cannot add new members in front of the list since this'd be an
1860                         // expensive operation, that's why the list is sorted in reverse order
1861                         // (ie. members from the current class are coming last).
1862                         list.Add (new CacheEntry (container, member, mt, bf));
1863                 }
1864
1865                 /// <summary>
1866                 ///   Add all members from class `container' with the requested MemberTypes and
1867                 ///   BindingFlags to the cache.  This method is called multiple times with different
1868                 ///   MemberTypes and BindingFlags.
1869                 /// </summary>
1870                 void AddMembers (MemberTypes mt, BindingFlags bf, IMemberContainer container)
1871                 {
1872                         MemberList members = container.GetMembers (mt, bf);
1873
1874                         foreach (MemberInfo member in members) {
1875                                 string name = member.Name;
1876
1877                                 AddMember (mt, bf, container, name, member);
1878
1879                                 if (member is MethodInfo) {
1880                                         string gname = TypeManager.GetMethodName ((MethodInfo) member);
1881                                         if (gname != name)
1882                                                 AddMember (mt, bf, container, gname, member);
1883                                 }
1884                         }
1885                 }
1886
1887                 /// <summary>
1888                 ///   Add all declared and inherited methods from class `type' to the method cache.
1889                 /// </summary>
1890                 void AddMethods (Type type)
1891                 {
1892                         AddMethods (BindingFlags.Static | BindingFlags.Public |
1893                                     BindingFlags.FlattenHierarchy, type);
1894                         AddMethods (BindingFlags.Static | BindingFlags.NonPublic |
1895                                     BindingFlags.FlattenHierarchy, type);
1896                         AddMethods (BindingFlags.Instance | BindingFlags.Public, type);
1897                         AddMethods (BindingFlags.Instance | BindingFlags.NonPublic, type);
1898                 }
1899
1900                 static ArrayList overrides = new ArrayList ();
1901
1902                 void AddMethods (BindingFlags bf, Type type)
1903                 {
1904                         MethodBase [] members = type.GetMethods (bf);
1905
1906                         Array.Reverse (members);
1907
1908                         foreach (MethodBase member in members) {
1909                                 string name = member.Name;
1910
1911                                 // We use a name-based hash table of ArrayList's.
1912                                 ArrayList list = (ArrayList) method_hash [name];
1913                                 if (list == null) {
1914                                         list = new ArrayList (1);
1915                                         method_hash.Add (name, list);
1916                                 }
1917
1918                                 MethodInfo curr = (MethodInfo) member;
1919                                 while (curr.IsVirtual && (curr.Attributes & MethodAttributes.NewSlot) == 0) {
1920                                         MethodInfo base_method = curr.GetBaseDefinition ();
1921
1922                                         if (base_method == curr)
1923                                                 // Not every virtual function needs to have a NewSlot flag.
1924                                                 break;
1925
1926                                         overrides.Add (curr);
1927                                         list.Add (new CacheEntry (null, base_method, MemberTypes.Method, bf));
1928                                         curr = base_method;
1929                                 }
1930
1931                                 if (overrides.Count > 0) {
1932                                         for (int i = 0; i < overrides.Count; ++i)
1933                                                 TypeManager.RegisterOverride ((MethodBase) overrides [i], curr);
1934                                         overrides.Clear ();
1935                                 }
1936
1937                                 // Unfortunately, the elements returned by Type.GetMethods() aren't
1938                                 // sorted so we need to do this check for every member.
1939                                 BindingFlags new_bf = bf;
1940                                 if (member.DeclaringType == type)
1941                                         new_bf |= BindingFlags.DeclaredOnly;
1942
1943                                 list.Add (new CacheEntry (Container, member, MemberTypes.Method, new_bf));
1944                         }
1945                 }
1946
1947                 /// <summary>
1948                 ///   Compute and return a appropriate `EntryType' magic number for the given
1949                 ///   MemberTypes and BindingFlags.
1950                 /// </summary>
1951                 protected static EntryType GetEntryType (MemberTypes mt, BindingFlags bf)
1952                 {
1953                         EntryType type = EntryType.None;
1954
1955                         if ((mt & MemberTypes.Constructor) != 0)
1956                                 type |= EntryType.Constructor;
1957                         if ((mt & MemberTypes.Event) != 0)
1958                                 type |= EntryType.Event;
1959                         if ((mt & MemberTypes.Field) != 0)
1960                                 type |= EntryType.Field;
1961                         if ((mt & MemberTypes.Method) != 0)
1962                                 type |= EntryType.Method;
1963                         if ((mt & MemberTypes.Property) != 0)
1964                                 type |= EntryType.Property;
1965                         // Nested types are returned by static and instance searches.
1966                         if ((mt & MemberTypes.NestedType) != 0)
1967                                 type |= EntryType.NestedType | EntryType.Static | EntryType.Instance;
1968
1969                         if ((bf & BindingFlags.Instance) != 0)
1970                                 type |= EntryType.Instance;
1971                         if ((bf & BindingFlags.Static) != 0)
1972                                 type |= EntryType.Static;
1973                         if ((bf & BindingFlags.Public) != 0)
1974                                 type |= EntryType.Public;
1975                         if ((bf & BindingFlags.NonPublic) != 0)
1976                                 type |= EntryType.NonPublic;
1977                         if ((bf & BindingFlags.DeclaredOnly) != 0)
1978                                 type |= EntryType.Declared;
1979
1980                         return type;
1981                 }
1982
1983                 /// <summary>
1984                 ///   The `MemberTypes' enumeration type is a [Flags] type which means that it may
1985                 ///   denote multiple member types.  Returns true if the given flags value denotes a
1986                 ///   single member types.
1987                 /// </summary>
1988                 public static bool IsSingleMemberType (MemberTypes mt)
1989                 {
1990                         switch (mt) {
1991                         case MemberTypes.Constructor:
1992                         case MemberTypes.Event:
1993                         case MemberTypes.Field:
1994                         case MemberTypes.Method:
1995                         case MemberTypes.Property:
1996                         case MemberTypes.NestedType:
1997                                 return true;
1998
1999                         default:
2000                                 return false;
2001                         }
2002                 }
2003
2004                 /// <summary>
2005                 ///   We encode the MemberTypes and BindingFlags of each members in a "magic"
2006                 ///   number to speed up the searching process.
2007                 /// </summary>
2008                 [Flags]
2009                 protected enum EntryType {
2010                         None            = 0x000,
2011
2012                         Instance        = 0x001,
2013                         Static          = 0x002,
2014                         MaskStatic      = Instance|Static,
2015
2016                         Public          = 0x004,
2017                         NonPublic       = 0x008,
2018                         MaskProtection  = Public|NonPublic,
2019
2020                         Declared        = 0x010,
2021
2022                         Constructor     = 0x020,
2023                         Event           = 0x040,
2024                         Field           = 0x080,
2025                         Method          = 0x100,
2026                         Property        = 0x200,
2027                         NestedType      = 0x400,
2028
2029                         NotExtensionMethod      = 0x800,
2030
2031                         MaskType        = Constructor|Event|Field|Method|Property|NestedType
2032                 }
2033
2034                 protected class CacheEntry {
2035                         public readonly IMemberContainer Container;
2036                         public EntryType EntryType;
2037                         public readonly MemberInfo Member;
2038
2039                         public CacheEntry (IMemberContainer container, MemberInfo member,
2040                                            MemberTypes mt, BindingFlags bf)
2041                         {
2042                                 this.Container = container;
2043                                 this.Member = member;
2044                                 this.EntryType = GetEntryType (mt, bf);
2045                         }
2046
2047                         public override string ToString ()
2048                         {
2049                                 return String.Format ("CacheEntry ({0}:{1}:{2})", Container.Name,
2050                                                       EntryType, Member);
2051                         }
2052                 }
2053
2054                 /// <summary>
2055                 ///   This is called each time we're walking up one level in the class hierarchy
2056                 ///   and checks whether we can abort the search since we've already found what
2057                 ///   we were looking for.
2058                 /// </summary>
2059                 protected bool DoneSearching (ArrayList list)
2060                 {
2061                         //
2062                         // We've found exactly one member in the current class and it's not
2063                         // a method or constructor.
2064                         //
2065                         if (list.Count == 1 && !(list [0] is MethodBase))
2066                                 return true;
2067
2068                         //
2069                         // Multiple properties: we query those just to find out the indexer
2070                         // name
2071                         //
2072                         if ((list.Count > 0) && (list [0] is PropertyInfo))
2073                                 return true;
2074
2075                         return false;
2076                 }
2077
2078                 /// <summary>
2079                 ///   Looks up members with name `name'.  If you provide an optional
2080                 ///   filter function, it'll only be called with members matching the
2081                 ///   requested member name.
2082                 ///
2083                 ///   This method will try to use the cache to do the lookup if possible.
2084                 ///
2085                 ///   Unlike other FindMembers implementations, this method will always
2086                 ///   check all inherited members - even when called on an interface type.
2087                 ///
2088                 ///   If you know that you're only looking for methods, you should use
2089                 ///   MemberTypes.Method alone since this speeds up the lookup a bit.
2090                 ///   When doing a method-only search, it'll try to use a special method
2091                 ///   cache (unless it's a dynamic type or an interface) and the returned
2092                 ///   MemberInfo's will have the correct ReflectedType for inherited methods.
2093                 ///   The lookup process will automatically restart itself in method-only
2094                 ///   search mode if it discovers that it's about to return methods.
2095                 /// </summary>
2096                 ArrayList global = new ArrayList ();
2097                 bool using_global = false;
2098                 
2099                 static MemberInfo [] emptyMemberInfo = new MemberInfo [0];
2100                 
2101                 public MemberInfo [] FindMembers (MemberTypes mt, BindingFlags bf, string name,
2102                                                   MemberFilter filter, object criteria)
2103                 {
2104                         if (using_global)
2105                                 throw new Exception ();
2106
2107                         bool declared_only = (bf & BindingFlags.DeclaredOnly) != 0;
2108                         bool method_search = mt == MemberTypes.Method;
2109                         // If we have a method cache and we aren't already doing a method-only search,
2110                         // then we restart a method search if the first match is a method.
2111                         bool do_method_search = !method_search && (method_hash != null);
2112
2113                         ArrayList applicable;
2114
2115                         // If this is a method-only search, we try to use the method cache if
2116                         // possible; a lookup in the method cache will return a MemberInfo with
2117                         // the correct ReflectedType for inherited methods.
2118                         
2119                         if (method_search && (method_hash != null))
2120                                 applicable = (ArrayList) method_hash [name];
2121                         else
2122                                 applicable = (ArrayList) member_hash [name];
2123
2124                         if (applicable == null)
2125                                 return emptyMemberInfo;
2126
2127                         //
2128                         // 32  slots gives 53 rss/54 size
2129                         // 2/4 slots gives 55 rss
2130                         //
2131                         // Strange: from 25,000 calls, only 1,800
2132                         // are above 2.  Why does this impact it?
2133                         //
2134                         global.Clear ();
2135                         using_global = true;
2136
2137                         Timer.StartTimer (TimerType.CachedLookup);
2138
2139                         EntryType type = GetEntryType (mt, bf);
2140
2141                         IMemberContainer current = Container;
2142
2143                         bool do_interface_search = current.IsInterface;
2144
2145                         // `applicable' is a list of all members with the given member name `name'
2146                         // in the current class and all its base classes.  The list is sorted in
2147                         // reverse order due to the way how the cache is initialy created (to speed
2148                         // things up, we're doing a deep-copy of our base).
2149
2150                         for (int i = applicable.Count-1; i >= 0; i--) {
2151                                 CacheEntry entry = (CacheEntry) applicable [i];
2152
2153                                 // This happens each time we're walking one level up in the class
2154                                 // hierarchy.  If we're doing a DeclaredOnly search, we must abort
2155                                 // the first time this happens (this may already happen in the first
2156                                 // iteration of this loop if there are no members with the name we're
2157                                 // looking for in the current class).
2158                                 if (entry.Container != current) {
2159                                         if (declared_only)
2160                                                 break;
2161
2162                                         if (!do_interface_search && DoneSearching (global))
2163                                                 break;
2164
2165                                         current = entry.Container;
2166                                 }
2167
2168                                 // Is the member of the correct type ?
2169                                 if ((entry.EntryType & type & EntryType.MaskType) == 0)
2170                                         continue;
2171
2172                                 // Is the member static/non-static ?
2173                                 if ((entry.EntryType & type & EntryType.MaskStatic) == 0)
2174                                         continue;
2175
2176                                 // Apply the filter to it.
2177                                 if (filter (entry.Member, criteria)) {
2178                                         if ((entry.EntryType & EntryType.MaskType) != EntryType.Method) {
2179                                                 do_method_search = false;
2180                                         }
2181                                         
2182                                         // Because interfaces support multiple inheritance we have to be sure that
2183                                         // base member is from same interface, so only top level member will be returned
2184                                         if (do_interface_search && global.Count > 0) {
2185                                                 bool member_already_exists = false;
2186
2187                                                 foreach (MemberInfo mi in global) {
2188                                                         if (mi is MethodBase)
2189                                                                 continue;
2190
2191                                                         if (IsInterfaceBaseInterface (TypeManager.GetInterfaces (mi.DeclaringType), entry.Member.DeclaringType)) {
2192                                                                 member_already_exists = true;
2193                                                                 break;
2194                                                         }
2195                                                 }
2196                                                 if (member_already_exists)
2197                                                         continue;
2198                                         }
2199
2200                                         global.Add (entry.Member);
2201                                 }
2202                         }
2203
2204                         Timer.StopTimer (TimerType.CachedLookup);
2205
2206                         // If we have a method cache and we aren't already doing a method-only
2207                         // search, we restart in method-only search mode if the first match is
2208                         // a method.  This ensures that we return a MemberInfo with the correct
2209                         // ReflectedType for inherited methods.
2210                         if (do_method_search && (global.Count > 0)){
2211                                 using_global = false;
2212
2213                                 return FindMembers (MemberTypes.Method, bf, name, filter, criteria);
2214                         }
2215
2216                         using_global = false;
2217                         MemberInfo [] copy = new MemberInfo [global.Count];
2218                         global.CopyTo (copy);
2219                         return copy;
2220                 }
2221
2222                 /// <summary>
2223                 /// Returns true if iterface exists in any base interfaces (ifaces)
2224                 /// </summary>
2225                 static bool IsInterfaceBaseInterface (Type[] ifaces, Type ifaceToFind)
2226                 {
2227                         foreach (Type iface in ifaces) {
2228                                 if (iface == ifaceToFind)
2229                                         return true;
2230
2231                                 Type[] base_ifaces = TypeManager.GetInterfaces (iface);
2232                                 if (base_ifaces.Length > 0 && IsInterfaceBaseInterface (base_ifaces, ifaceToFind))
2233                                         return true;
2234                         }
2235                         return false;
2236                 }
2237                 
2238                 // find the nested type @name in @this.
2239                 public Type FindNestedType (string name)
2240                 {
2241                         ArrayList applicable = (ArrayList) member_hash [name];
2242                         if (applicable == null)
2243                                 return null;
2244                         
2245                         for (int i = applicable.Count-1; i >= 0; i--) {
2246                                 CacheEntry entry = (CacheEntry) applicable [i];
2247                                 if ((entry.EntryType & EntryType.NestedType & EntryType.MaskType) != 0)
2248                                         return (Type) entry.Member;
2249                         }
2250                         
2251                         return null;
2252                 }
2253
2254                 public MemberInfo FindBaseEvent (Type invocation_type, string name)
2255                 {
2256                         ArrayList applicable = (ArrayList) member_hash [name];
2257                         if (applicable == null)
2258                                 return null;
2259
2260                         //
2261                         // Walk the chain of events, starting from the top.
2262                         //
2263                         for (int i = applicable.Count - 1; i >= 0; i--) 
2264                         {
2265                                 CacheEntry entry = (CacheEntry) applicable [i];
2266                                 if ((entry.EntryType & EntryType.Event) == 0)
2267                                         continue;
2268                                 
2269                                 EventInfo ei = (EventInfo)entry.Member;
2270                                 return ei.GetAddMethod (true);
2271                         }
2272
2273                         return null;
2274                 }
2275
2276                 //
2277                 // Looks for extension methods with defined name and extension type
2278                 //
2279                 public ArrayList FindExtensionMethods (Type extensionType, string name, bool publicOnly)
2280                 {
2281                         ArrayList entries;
2282                         if (method_hash != null)
2283                                 entries = (ArrayList)method_hash [name];
2284                         else
2285                                 entries = (ArrayList)member_hash [name];
2286
2287                         if (entries == null)
2288                                 return null;
2289
2290                         EntryType entry_type = EntryType.Static | EntryType.Method | EntryType.NotExtensionMethod;
2291                         if (publicOnly) {
2292                                 entry_type |= EntryType.Public;
2293                         }
2294                         EntryType found_entry_type = entry_type & ~EntryType.NotExtensionMethod;
2295
2296                         ArrayList candidates = null;
2297                         foreach (CacheEntry entry in entries) {
2298                                 if ((entry.EntryType & entry_type) == found_entry_type) {
2299                                         MethodBase mb = (MethodBase)entry.Member;
2300
2301                                         IMethodData md = TypeManager.GetMethod (mb);
2302                                         AParametersCollection pd = md == null ?
2303                                                 TypeManager.GetParameterData (mb) : md.ParameterInfo;
2304
2305                                         Type ex_type = pd.ExtensionMethodType;
2306                                         if (ex_type == null) {
2307                                                 entry.EntryType |= EntryType.NotExtensionMethod;
2308                                                 continue;
2309                                         }
2310
2311                                         //if (implicit conversion between ex_type and extensionType exist) {
2312                                                 if (candidates == null)
2313                                                         candidates = new ArrayList (2);
2314                                                 candidates.Add (mb);
2315                                         //}
2316                                 }
2317                         }
2318
2319                         return candidates;
2320                 }
2321                 
2322                 //
2323                 // This finds the method or property for us to override. invocation_type is the type where
2324                 // the override is going to be declared, name is the name of the method/property, and
2325                 // param_types is the parameters, if any to the method or property
2326                 //
2327                 // Because the MemberCache holds members from this class and all the base classes,
2328                 // we can avoid tons of reflection stuff.
2329                 //
2330                 public MemberInfo FindMemberToOverride (Type invocation_type, string name, AParametersCollection parameters, GenericMethod generic_method, bool is_property)
2331                 {
2332                         ArrayList applicable;
2333                         if (method_hash != null && !is_property)
2334                                 applicable = (ArrayList) method_hash [name];
2335                         else
2336                                 applicable = (ArrayList) member_hash [name];
2337                         
2338                         if (applicable == null)
2339                                 return null;
2340                         //
2341                         // Walk the chain of methods, starting from the top.
2342                         //
2343                         for (int i = applicable.Count - 1; i >= 0; i--) {
2344                                 CacheEntry entry = (CacheEntry) applicable [i];
2345                                 
2346                                 if ((entry.EntryType & (is_property ? (EntryType.Property | EntryType.Field) : EntryType.Method)) == 0)
2347                                         continue;
2348
2349                                 PropertyInfo pi = null;
2350                                 MethodInfo mi = null;
2351                                 FieldInfo fi = null;
2352                                 AParametersCollection cmp_attrs;
2353                                 
2354                                 if (is_property) {
2355                                         if ((entry.EntryType & EntryType.Field) != 0) {
2356                                                 fi = (FieldInfo)entry.Member;
2357                                                 cmp_attrs = Parameters.EmptyReadOnlyParameters;
2358                                         } else {
2359                                                 pi = (PropertyInfo) entry.Member;
2360                                                 cmp_attrs = TypeManager.GetParameterData (pi);
2361                                         }
2362                                 } else {
2363                                         mi = (MethodInfo) entry.Member;
2364                                         cmp_attrs = TypeManager.GetParameterData (mi);
2365                                 }
2366
2367                                 if (fi != null) {
2368                                         // TODO: Almost duplicate !
2369                                         // Check visibility
2370                                         switch (fi.Attributes & FieldAttributes.FieldAccessMask) {
2371                                         case FieldAttributes.PrivateScope:
2372                                                 continue;
2373                                         case FieldAttributes.Private:
2374                                                 //
2375                                                 // A private method is Ok if we are a nested subtype.
2376                                                 // The spec actually is not very clear about this, see bug 52458.
2377                                                 //
2378                                                 if (!invocation_type.Equals (entry.Container.Type) &&
2379                                                     !TypeManager.IsNestedChildOf (invocation_type, entry.Container.Type))
2380                                                         continue;
2381                                                 break;
2382                                         case FieldAttributes.FamANDAssem:
2383                                         case FieldAttributes.Assembly:
2384                                                 //
2385                                                 // Check for assembly methods
2386                                                 //
2387                                                 if (mi.DeclaringType.Assembly != CodeGen.Assembly.Builder)
2388                                                         continue;
2389                                                 break;
2390                                         }
2391                                         return entry.Member;
2392                                 }
2393
2394                                 //
2395                                 // Check the arguments
2396                                 //
2397                                 if (cmp_attrs.Count != parameters.Count)
2398                                         continue;
2399         
2400                                 int j;
2401                                 for (j = 0; j < cmp_attrs.Count; ++j) {
2402                                         //
2403                                         // LAMESPEC: No idea why `params' modifier is ignored
2404                                         //
2405                                         if ((parameters.FixedParameters [j].ModFlags & ~Parameter.Modifier.PARAMS) != 
2406                                                 (cmp_attrs.FixedParameters [j].ModFlags & ~Parameter.Modifier.PARAMS))
2407                                                 break;
2408
2409                                         if (!TypeManager.IsEqual (parameters.Types [j], cmp_attrs.Types [j]))
2410                                                 break;
2411                                 }
2412
2413                                 if (j < cmp_attrs.Count)
2414                                         continue;
2415
2416                                 //
2417                                 // check generic arguments for methods
2418                                 //
2419                                 if (mi != null) {
2420                                         Type [] cmpGenArgs = TypeManager.GetGenericArguments (mi);
2421                                         if (generic_method == null && cmpGenArgs != null && cmpGenArgs.Length != 0)
2422                                                 continue;
2423                                         if (generic_method != null && cmpGenArgs != null && cmpGenArgs.Length != generic_method.TypeParameters.Length)
2424                                                 continue;
2425                                 }
2426
2427                                 //
2428                                 // get one of the methods because this has the visibility info.
2429                                 //
2430                                 if (is_property) {
2431                                         mi = pi.GetGetMethod (true);
2432                                         if (mi == null)
2433                                                 mi = pi.GetSetMethod (true);
2434                                 }
2435                                 
2436                                 //
2437                                 // Check visibility
2438                                 //
2439                                 switch (mi.Attributes & MethodAttributes.MemberAccessMask) {
2440                                 case MethodAttributes.PrivateScope:
2441                                         continue;
2442                                 case MethodAttributes.Private:
2443                                         //
2444                                         // A private method is Ok if we are a nested subtype.
2445                                         // The spec actually is not very clear about this, see bug 52458.
2446                                         //
2447                                         if (!invocation_type.Equals (entry.Container.Type) &&
2448                                             !TypeManager.IsNestedChildOf (invocation_type, entry.Container.Type))
2449                                                 continue;
2450                                         break;
2451                                 case MethodAttributes.FamANDAssem:
2452                                 case MethodAttributes.Assembly:
2453                                         //
2454                                         // Check for assembly methods
2455                                         //
2456                                         if (!TypeManager.IsThisOrFriendAssembly (mi.DeclaringType.Assembly))
2457                                                 continue;
2458                                         break;
2459                                 }
2460                                 return entry.Member;
2461                         }
2462                         
2463                         return null;
2464                 }
2465
2466                 /// <summary>
2467                 /// The method is looking for conflict with inherited symbols (errors CS0108, CS0109).
2468                 /// We handle two cases. The first is for types without parameters (events, field, properties).
2469                 /// The second are methods, indexers and this is why ignore_complex_types is here.
2470                 /// The latest param is temporary hack. See DoDefineMembers method for more info.
2471                 /// </summary>
2472                 public MemberInfo FindMemberWithSameName (string name, bool ignore_complex_types, MemberInfo ignore_member)
2473                 {
2474                         ArrayList applicable = null;
2475  
2476                         if (method_hash != null)
2477                                 applicable = (ArrayList) method_hash [name];
2478  
2479                         if (applicable != null) {
2480                                 for (int i = applicable.Count - 1; i >= 0; i--) {
2481                                         CacheEntry entry = (CacheEntry) applicable [i];
2482                                         if ((entry.EntryType & EntryType.Public) != 0)
2483                                                 return entry.Member;
2484                                 }
2485                         }
2486  
2487                         if (member_hash == null)
2488                                 return null;
2489                         applicable = (ArrayList) member_hash [name];
2490                         
2491                         if (applicable != null) {
2492                                 for (int i = applicable.Count - 1; i >= 0; i--) {
2493                                         CacheEntry entry = (CacheEntry) applicable [i];
2494                                         if ((entry.EntryType & EntryType.Public) != 0 & entry.Member != ignore_member) {
2495                                                 if (ignore_complex_types) {
2496                                                         if ((entry.EntryType & EntryType.Method) != 0)
2497                                                                 continue;
2498  
2499                                                         // Does exist easier way how to detect indexer ?
2500                                                         if ((entry.EntryType & EntryType.Property) != 0) {
2501                                                                 AParametersCollection arg_types = TypeManager.GetParameterData ((PropertyInfo)entry.Member);
2502                                                                 if (arg_types.Count > 0)
2503                                                                         continue;
2504                                                         }
2505                                                 }
2506                                                 return entry.Member;
2507                                         }
2508                                 }
2509                         }
2510                         return null;
2511                 }
2512
2513                 Hashtable locase_table;
2514  
2515                 /// <summary>
2516                 /// Builds low-case table for CLS Compliance test
2517                 /// </summary>
2518                 public Hashtable GetPublicMembers ()
2519                 {
2520                         if (locase_table != null)
2521                                 return locase_table;
2522  
2523                         locase_table = new Hashtable ();
2524                         foreach (DictionaryEntry entry in member_hash) {
2525                                 ArrayList members = (ArrayList)entry.Value;
2526                                 for (int ii = 0; ii < members.Count; ++ii) {
2527                                         CacheEntry member_entry = (CacheEntry) members [ii];
2528  
2529                                         if ((member_entry.EntryType & EntryType.Public) == 0)
2530                                                 continue;
2531  
2532                                         // TODO: Does anyone know easier way how to detect that member is internal ?
2533                                         switch (member_entry.EntryType & EntryType.MaskType) {
2534                                         case EntryType.Constructor:
2535                                                 continue;
2536                                                 
2537                                         case EntryType.Field:
2538                                                 if ((((FieldInfo)member_entry.Member).Attributes & (FieldAttributes.Assembly | FieldAttributes.Public)) == FieldAttributes.Assembly)
2539                                                         continue;
2540                                                 break;
2541                                                 
2542                                         case EntryType.Method:
2543                                                 if ((((MethodInfo)member_entry.Member).Attributes & (MethodAttributes.Assembly | MethodAttributes.Public)) == MethodAttributes.Assembly)
2544                                                         continue;
2545                                                 break;
2546                                                 
2547                                         case EntryType.Property:
2548                                                 PropertyInfo pi = (PropertyInfo)member_entry.Member;
2549                                                 if (pi.GetSetMethod () == null && pi.GetGetMethod () == null)
2550                                                         continue;
2551                                                 break;
2552                                                 
2553                                         case EntryType.Event:
2554                                                 EventInfo ei = (EventInfo)member_entry.Member;
2555                                                 MethodInfo mi = ei.GetAddMethod ();
2556                                                 if ((mi.Attributes & (MethodAttributes.Assembly | MethodAttributes.Public)) == MethodAttributes.Assembly)
2557                                                         continue;
2558                                                 break;
2559                                         }
2560                                         string lcase = ((string)entry.Key).ToLower (System.Globalization.CultureInfo.InvariantCulture);
2561                                         locase_table [lcase] = member_entry.Member;
2562                                         break;
2563                                 }
2564                         }
2565                         return locase_table;
2566                 }
2567  
2568                 public Hashtable Members {
2569                         get {
2570                                 return member_hash;
2571                         }
2572                 }
2573  
2574                 /// <summary>
2575                 /// Cls compliance check whether methods or constructors parameters differing only in ref or out, or in array rank
2576                 /// </summary>
2577                 /// 
2578                 // TODO: refactor as method is always 'this'
2579                 public static void VerifyClsParameterConflict (ArrayList al, MethodCore method, MemberInfo this_builder)
2580                 {
2581                         EntryType tested_type = (method is Constructor ? EntryType.Constructor : EntryType.Method) | EntryType.Public;
2582  
2583                         for (int i = 0; i < al.Count; ++i) {
2584                                 MemberCache.CacheEntry entry = (MemberCache.CacheEntry) al [i];
2585                 
2586                                 // skip itself
2587                                 if (entry.Member == this_builder)
2588                                         continue;
2589                 
2590                                 if ((entry.EntryType & tested_type) != tested_type)
2591                                         continue;
2592                 
2593                                 MethodBase method_to_compare = (MethodBase)entry.Member;
2594                                 AttributeTester.Result result = AttributeTester.AreOverloadedMethodParamsClsCompliant (
2595                                         method.Parameters, TypeManager.GetParameterData (method_to_compare));
2596
2597                                 if (result == AttributeTester.Result.Ok)
2598                                         continue;
2599
2600                                 IMethodData md = TypeManager.GetMethod (method_to_compare);
2601
2602                                 // TODO: now we are ignoring CLSCompliance(false) on method from other assembly which is buggy.
2603                                 // However it is exactly what csc does.
2604                                 if (md != null && !md.IsClsComplianceRequired ())
2605                                         continue;
2606                 
2607                                 Report.SymbolRelatedToPreviousError (entry.Member);
2608                                 switch (result) {
2609                                 case AttributeTester.Result.RefOutArrayError:
2610                                         Report.Warning (3006, 1, method.Location,
2611                                                         "Overloaded method `{0}' differing only in ref or out, or in array rank, is not CLS-compliant",
2612                                                         method.GetSignatureForError ());
2613                                         continue;
2614                                 case AttributeTester.Result.ArrayArrayError:
2615                                         Report.Warning (3007, 1, method.Location,
2616                                                         "Overloaded method `{0}' differing only by unnamed array types is not CLS-compliant",
2617                                                         method.GetSignatureForError ());
2618                                         continue;
2619                                 }
2620
2621                                 throw new NotImplementedException (result.ToString ());
2622                         }
2623                 }
2624
2625                 public bool CheckExistingMembersOverloads (MemberCore member, string name, Parameters parameters)
2626                 {
2627                         ArrayList entries = (ArrayList)member_hash [name];
2628                         if (entries == null)
2629                                 return true;
2630
2631                         int method_param_count = parameters.Count;
2632                         for (int i = entries.Count - 1; i >= 0; --i) {
2633                                 CacheEntry ce = (CacheEntry) entries [i];
2634
2635                                 if (ce.Container != member.Parent.PartialContainer)
2636                                         return true;
2637
2638                                 Type [] p_types;
2639                                 AParametersCollection pd;
2640                                 if ((ce.EntryType & EntryType.Property) != 0) {
2641                                         pd = TypeManager.GetParameterData ((PropertyInfo) ce.Member);
2642                                         p_types = pd.Types;
2643                                 } else {
2644                                         MethodBase mb = (MethodBase) ce.Member;
2645 #if GMCS_SOURCE                                 
2646                                         // TODO: This is more like a hack, because we are adding generic methods
2647                                         // twice with and without arity name
2648                                         if (mb.IsGenericMethod && !member.MemberName.IsGeneric)
2649                                                 continue;
2650 #endif                  
2651                                         pd = TypeManager.GetParameterData (mb);
2652                                         p_types = pd.Types;
2653                                 }
2654
2655                                 if (p_types.Length != method_param_count)
2656                                         continue;
2657
2658                                 if (method_param_count > 0) {
2659                                         int ii = method_param_count - 1;
2660                                         Type type_a, type_b;
2661                                         do {
2662                                                 type_a = parameters.Types [ii];
2663                                                 type_b = p_types [ii];
2664
2665 #if GMCS_SOURCE
2666                                                 if (type_a.IsGenericParameter && type_a.DeclaringMethod != null)
2667                                                         type_a = null;
2668
2669                                                 if (type_b.IsGenericParameter && type_b.DeclaringMethod != null)
2670                                                         type_b = null;
2671 #endif
2672                                                 if ((pd.FixedParameters [ii].ModFlags & Parameter.Modifier.ISBYREF) !=
2673                                                         (parameters.FixedParameters [ii].ModFlags & Parameter.Modifier.ISBYREF))
2674                                                         type_a = null;
2675
2676                                         } while (type_a == type_b && ii-- != 0);
2677
2678                                         if (ii >= 0)
2679                                                 continue;
2680
2681                                         //
2682                                         // Operators can differ in return type only
2683                                         //
2684                                         if (member is Operator) {
2685                                                 Operator op = TypeManager.GetMethod ((MethodBase) ce.Member) as Operator;
2686                                                 if (op != null && op.ReturnType != ((Operator) member).ReturnType)
2687                                                         continue;
2688                                         }
2689
2690                                         //
2691                                         // Report difference in parameter modifiers only
2692                                         //
2693                                         if (pd != null && member is MethodCore) {
2694                                                 ii = method_param_count;
2695                                                 while (ii-- != 0 && parameters.FixedParameters [ii].ModFlags == pd.FixedParameters [ii].ModFlags &&
2696                                                         parameters.ExtensionMethodType == pd.ExtensionMethodType);
2697
2698                                                 if (ii >= 0) {
2699                                                         MethodCore mc = TypeManager.GetMethod ((MethodBase) ce.Member) as MethodCore;
2700                                                         Report.SymbolRelatedToPreviousError (ce.Member);
2701                                                         if ((member.ModFlags & Modifiers.PARTIAL) != 0 && (mc.ModFlags & Modifiers.PARTIAL) != 0) {
2702                                                                 if (parameters.HasParams || pd.HasParams) {
2703                                                                         Report.Error (758, member.Location,
2704                                                                                 "A partial method declaration and partial method implementation cannot differ on use of `params' modifier");
2705                                                                 } else {
2706                                                                         Report.Error (755, member.Location,
2707                                                                                 "A partial method declaration and partial method implementation must be both an extension method or neither");
2708                                                                 }
2709                                                         } else {
2710                                                                 Report.Error (663, member.Location,
2711                                                                         "An overloaded method `{0}' cannot differ on use of parameter modifiers only",
2712                                                                         member.GetSignatureForError ());
2713                                                         }
2714                                                         return false;
2715                                                 }
2716                                         }
2717                                 }
2718
2719                                 if ((ce.EntryType & EntryType.Method) != 0) {
2720                                         Method method_a = member as Method;
2721                                         Method method_b = TypeManager.GetMethod ((MethodBase) ce.Member) as Method;
2722                                         if (method_a != null && method_b != null && (method_a.ModFlags & method_b.ModFlags & Modifiers.PARTIAL) != 0) {
2723                                                 const int partial_modifiers = Modifiers.STATIC | Modifiers.UNSAFE;
2724                                                 if (method_a.IsPartialDefinition == method_b.IsPartialImplementation) {
2725                                                         if ((method_a.ModFlags & partial_modifiers) == (method_b.ModFlags & partial_modifiers) ||
2726                                                                 method_a.Parent.IsInUnsafeScope && method_b.Parent.IsInUnsafeScope) {
2727                                                                 if (method_a.IsPartialImplementation) {
2728                                                                         method_a.SetPartialDefinition (method_b);
2729                                                                         entries.RemoveAt (i);
2730                                                                 } else {
2731                                                                         method_b.SetPartialDefinition (method_a);
2732                                                                 }
2733                                                                 continue;
2734                                                         }
2735
2736                                                         if ((method_a.ModFlags & Modifiers.STATIC) != (method_b.ModFlags & Modifiers.STATIC)) {
2737                                                                 Report.SymbolRelatedToPreviousError (ce.Member);
2738                                                                 Report.Error (763, member.Location,
2739                                                                         "A partial method declaration and partial method implementation must be both `static' or neither");
2740                                                         }
2741
2742                                                         Report.SymbolRelatedToPreviousError (ce.Member);
2743                                                         Report.Error (764, member.Location,
2744                                                                 "A partial method declaration and partial method implementation must be both `unsafe' or neither");
2745                                                         return false;
2746                                                 }
2747
2748                                                 Report.SymbolRelatedToPreviousError (ce.Member);
2749                                                 if (method_a.IsPartialDefinition) {
2750                                                         Report.Error (756, member.Location, "A partial method `{0}' declaration is already defined",
2751                                                                 member.GetSignatureForError ());
2752                                                 }
2753
2754                                                 Report.Error (757, member.Location, "A partial method `{0}' implementation is already defined",
2755                                                         member.GetSignatureForError ());
2756                                                 return false;
2757                                         }
2758
2759                                         Report.SymbolRelatedToPreviousError (ce.Member);
2760                                         IMethodData duplicate_member = TypeManager.GetMethod ((MethodBase) ce.Member);
2761                                         if (member is Operator && duplicate_member is Operator) {
2762                                                 Report.Error (557, member.Location, "Duplicate user-defined conversion in type `{0}'",
2763                                                         member.Parent.GetSignatureForError ());
2764                                                 return false;
2765                                         }
2766
2767                                         bool is_reserved_a = member is AbstractPropertyEventMethod || member is Operator;
2768                                         bool is_reserved_b = duplicate_member is AbstractPropertyEventMethod || duplicate_member is Operator;
2769
2770                                         if (is_reserved_a || is_reserved_b) {
2771                                                 Report.Error (82, member.Location, "A member `{0}' is already reserved",
2772                                                         is_reserved_a ?
2773                                                         TypeManager.GetFullNameSignature (ce.Member) :
2774                                                         member.GetSignatureForError ());
2775                                                 return false;
2776                                         }
2777                                 } else {
2778                                         Report.SymbolRelatedToPreviousError (ce.Member);
2779                                 }
2780                                 
2781                                 Report.Error (111, member.Location,
2782                                         "A member `{0}' is already defined. Rename this member or use different parameter types",
2783                                         member.GetSignatureForError ());
2784                                 return false;
2785                         }
2786
2787                         return true;
2788                 }
2789         }
2790 }