Merge pull request #1225 from strawd/bug22307
[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 // Copyright 2011 Xamarin Inc
12 //
13 //
14
15 using System;
16 using System.Collections.Generic;
17 using System.Diagnostics;
18 using System.Text;
19 using Mono.CompilerServices.SymbolWriter;
20
21 #if NET_2_1
22 using XmlElement = System.Object;
23 #else
24 using System.Xml;
25 #endif
26
27 #if STATIC
28 using IKVM.Reflection;
29 using IKVM.Reflection.Emit;
30 #else
31 using System.Reflection;
32 using System.Reflection.Emit;
33 #endif
34
35 namespace Mono.CSharp {
36
37         //
38         // Better name would be DottenName
39         //
40         [DebuggerDisplay ("{GetSignatureForError()}")]
41         public class MemberName
42         {
43                 public static readonly MemberName Null = new MemberName ("");
44
45                 public readonly string Name;
46                 public TypeParameters TypeParameters;
47                 public readonly FullNamedExpression ExplicitInterface;
48                 public readonly Location Location;
49
50                 public readonly MemberName Left;
51
52                 public MemberName (string name)
53                         : this (name, Location.Null)
54                 { }
55
56                 public MemberName (string name, Location loc)
57                         : this (null, name, loc)
58                 { }
59
60                 public MemberName (string name, TypeParameters tparams, Location loc)
61                 {
62                         this.Name = name;
63                         this.Location = loc;
64
65                         this.TypeParameters = tparams;
66                 }
67
68                 public MemberName (string name, TypeParameters tparams, FullNamedExpression explicitInterface, Location loc)
69                         : this (name, tparams, loc)
70                 {
71                         this.ExplicitInterface = explicitInterface;
72                 }
73
74                 public MemberName (MemberName left, string name, Location loc)
75                 {
76                         this.Name = name;
77                         this.Location = loc;
78                         this.Left = left;
79                 }
80
81                 public MemberName (MemberName left, string name, FullNamedExpression explicitInterface, Location loc)
82                         : this (left, name, loc)
83                 {
84                         this.ExplicitInterface = explicitInterface;
85                 }
86
87                 public MemberName (MemberName left, MemberName right)
88                 {
89                         this.Name = right.Name;
90                         this.Location = right.Location;
91                         this.TypeParameters = right.TypeParameters;
92                         this.Left = left;
93                 }
94
95                 public int Arity {
96                         get {
97                                 return TypeParameters == null ? 0 : TypeParameters.Count;
98                         }
99                 }
100
101                 public bool IsGeneric {
102                         get {
103                                 return TypeParameters != null;
104                         }
105                 }
106
107                 public string Basename {
108                         get {
109                                 if (TypeParameters != null)
110                                         return MakeName (Name, TypeParameters);
111                                 return Name;
112                         }
113                 }
114
115                 public void CreateMetadataName (StringBuilder sb)
116                 {
117                         if (Left != null)
118                                 Left.CreateMetadataName (sb);
119
120                         if (sb.Length != 0) {
121                                 sb.Append (".");
122                         }
123
124                         sb.Append (Basename);
125                 }
126
127                 public string GetSignatureForDocumentation ()
128                 {
129                         var s = Basename;
130
131                         if (ExplicitInterface != null)
132                                 s = ExplicitInterface.GetSignatureForError () + "." + s;
133
134                         if (Left == null)
135                                 return s;
136
137                         return Left.GetSignatureForDocumentation () + "." + s;
138                 }
139
140                 public string GetSignatureForError ()
141                 {
142                         string s = TypeParameters == null ? null : "<" + TypeParameters.GetSignatureForError () + ">";
143                         s = Name + s;
144
145                         if (ExplicitInterface != null)
146                                 s = ExplicitInterface.GetSignatureForError () + "." + s;
147
148                         if (Left == null)
149                                 return s;
150
151                         return Left.GetSignatureForError () + "." + s;
152                 }
153
154                 public override bool Equals (object other)
155                 {
156                         return Equals (other as MemberName);
157                 }
158
159                 public bool Equals (MemberName other)
160                 {
161                         if (this == other)
162                                 return true;
163                         if (other == null || Name != other.Name)
164                                 return false;
165
166                         if ((TypeParameters != null) &&
167                             (other.TypeParameters == null || TypeParameters.Count != other.TypeParameters.Count))
168                                 return false;
169
170                         if ((TypeParameters == null) && (other.TypeParameters != null))
171                                 return false;
172
173                         if (Left == null)
174                                 return other.Left == null;
175
176                         return Left.Equals (other.Left);
177                 }
178
179                 public override int GetHashCode ()
180                 {
181                         int hash = Name.GetHashCode ();
182                         for (MemberName n = Left; n != null; n = n.Left)
183                                 hash ^= n.Name.GetHashCode ();
184
185                         if (TypeParameters != null)
186                                 hash ^= TypeParameters.Count << 5;
187
188                         return hash & 0x7FFFFFFF;
189                 }
190
191                 public static string MakeName (string name, TypeParameters args)
192                 {
193                         if (args == null)
194                                 return name;
195
196                         return name + "`" + args.Count;
197                 }
198         }
199
200         public class SimpleMemberName
201         {
202                 public string Value;
203                 public Location Location;
204
205                 public SimpleMemberName (string name, Location loc)
206                 {
207                         this.Value = name;
208                         this.Location = loc;
209                 }
210         }
211
212         /// <summary>
213         ///   Base representation for members.  This is used to keep track
214         ///   of Name, Location and Modifier flags, and handling Attributes.
215         /// </summary>
216         [System.Diagnostics.DebuggerDisplay ("{GetSignatureForError()}")]
217         public abstract class MemberCore : Attributable, IMemberContext, IMemberDefinition
218         {
219                 string IMemberDefinition.Name {
220                         get {
221                                 return member_name.Name;
222                         }
223                 }
224
225                 // Is not readonly because of IndexerName attribute
226                 private MemberName member_name;
227                 public MemberName MemberName {
228                         get { return member_name; }
229                 }
230
231                 /// <summary>
232                 ///   Modifier flags that the user specified in the source code
233                 /// </summary>
234                 private Modifiers mod_flags;
235                 public Modifiers ModFlags {
236                         set {
237                                 mod_flags = value;
238                                 if ((value & Modifiers.COMPILER_GENERATED) != 0)
239                                         caching_flags = Flags.IsUsed | Flags.IsAssigned;
240                         }
241                         get {
242                                 return mod_flags;
243                         }
244                 }
245
246                 public virtual ModuleContainer Module {
247                         get {
248                                 return Parent.Module;
249                         }
250                 }
251
252                 public /*readonly*/ TypeContainer Parent;
253
254                 /// <summary>
255                 ///   Location where this declaration happens
256                 /// </summary>
257                 public Location Location {
258                         get { return member_name.Location; }
259                 }
260
261                 /// <summary>
262                 ///   XML documentation comment
263                 /// </summary>
264                 protected string comment;
265
266                 /// <summary>
267                 ///   Represents header string for documentation comment 
268                 ///   for each member types.
269                 /// </summary>
270                 public abstract string DocCommentHeader { get; }
271
272                 [Flags]
273                 public enum Flags {
274                         Obsolete_Undetected = 1,                // Obsolete attribute has not been detected yet
275                         Obsolete = 1 << 1,                      // Type has obsolete attribute
276                         ClsCompliance_Undetected = 1 << 2,      // CLS Compliance has not been detected yet
277                         ClsCompliant = 1 << 3,                  // Type is CLS Compliant
278                         CloseTypeCreated = 1 << 4,              // Tracks whether we have Closed the type
279                         HasCompliantAttribute_Undetected = 1 << 5,      // Presence of CLSCompliantAttribute has not been detected
280                         HasClsCompliantAttribute = 1 << 6,                      // Type has CLSCompliantAttribute
281                         ClsCompliantAttributeFalse = 1 << 7,                    // Member has CLSCompliant(false)
282                         Excluded_Undetected = 1 << 8,           // Conditional attribute has not been detected yet
283                         Excluded = 1 << 9,                                      // Method is conditional
284                         MethodOverloadsExist = 1 << 10,         // Test for duplication must be performed
285                         IsUsed = 1 << 11,
286                         IsAssigned = 1 << 12,                           // Field is assigned
287                         HasExplicitLayout       = 1 << 13,
288                         PartialDefinitionExists = 1 << 14,      // Set when corresponding partial method definition exists
289                         HasStructLayout = 1 << 15,                      // Has StructLayoutAttribute
290                         HasInstanceConstructor = 1 << 16,
291                         HasUserOperators = 1 << 17,
292                         CanBeReused = 1 << 18,
293                         InterfacesExpanded = 1 << 19
294                 }
295
296                 /// <summary>
297                 ///   MemberCore flags at first detected then cached
298                 /// </summary>
299                 internal Flags caching_flags;
300
301                 protected MemberCore (TypeContainer parent, MemberName name, Attributes attrs)
302                 {
303                         this.Parent = parent;
304                         member_name = name;
305                         caching_flags = Flags.Obsolete_Undetected | Flags.ClsCompliance_Undetected | Flags.HasCompliantAttribute_Undetected | Flags.Excluded_Undetected;
306                         AddAttributes (attrs, this);
307                 }
308
309                 protected virtual void SetMemberName (MemberName new_name)
310                 {
311                         member_name = new_name;
312                 }
313
314                 public virtual void Accept (StructuralVisitor visitor)
315                 {
316                         visitor.Visit (this);
317                 }
318
319                 protected bool CheckAbstractAndExtern (bool has_block)
320                 {
321                         if (Parent.PartialContainer.Kind == MemberKind.Interface)
322                                 return true;
323
324                         if (has_block) {
325                                 if ((ModFlags & Modifiers.EXTERN) != 0) {
326                                         Report.Error (179, Location, "`{0}' cannot declare a body because it is marked extern",
327                                                 GetSignatureForError ());
328                                         return false;
329                                 }
330
331                                 if ((ModFlags & Modifiers.ABSTRACT) != 0) {
332                                         Report.Error (500, Location, "`{0}' cannot declare a body because it is marked abstract",
333                                                 GetSignatureForError ());
334                                         return false;
335                                 }
336                         } else {
337                                 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN | Modifiers.PARTIAL)) == 0 && !(Parent is Delegate)) {
338                                         if (Compiler.Settings.Version >= LanguageVersion.V_3) {
339                                                 Property.PropertyMethod pm = this as Property.PropertyMethod;
340                                                 if (pm is Indexer.GetIndexerMethod || pm is Indexer.SetIndexerMethod)
341                                                         pm = null;
342
343                                                 if (pm != null && pm.Property.AccessorSecond == null) {
344                                                         Report.Error (840, Location,
345                                                                 "`{0}' must have a body because it is not marked abstract or extern. The property can be automatically implemented when you define both accessors",
346                                                                 GetSignatureForError ());
347                                                         return false;
348                                                 }
349                                         }
350
351                                         Report.Error (501, Location, "`{0}' must have a body because it is not marked abstract, extern, or partial",
352                                                       GetSignatureForError ());
353                                         return false;
354                                 }
355                         }
356
357                         return true;
358                 }
359
360                 protected void CheckProtectedModifier ()
361                 {
362                         if ((ModFlags & Modifiers.PROTECTED) == 0)
363                                 return;
364
365                         if (Parent.PartialContainer.Kind == MemberKind.Struct) {
366                                 Report.Error (666, Location, "`{0}': Structs cannot contain protected members",
367                                         GetSignatureForError ());
368                                 return;
369                         }
370
371                         if ((Parent.ModFlags & Modifiers.STATIC) != 0) {
372                                 Report.Error (1057, Location, "`{0}': Static classes cannot contain protected members",
373                                         GetSignatureForError ());
374                                 return;
375                         }
376
377                         if ((Parent.ModFlags & Modifiers.SEALED) != 0 && (ModFlags & Modifiers.OVERRIDE) == 0 &&
378                                 !(this is Destructor)) {
379                                 Report.Warning (628, 4, Location, "`{0}': new protected member declared in sealed class",
380                                         GetSignatureForError ());
381                                 return;
382                         }
383                 }
384
385                 public abstract bool Define ();
386
387                 public virtual string DocComment {
388                         get {
389                                 return comment;
390                         }
391                         set {
392                                 comment = value;
393                         }
394                 }
395
396                 // 
397                 // Returns full member name for error message
398                 //
399                 public virtual string GetSignatureForError ()
400                 {
401                         var parent = Parent.GetSignatureForError ();
402                         if (parent == null)
403                                 return member_name.GetSignatureForError ();
404
405                         return parent + "." + member_name.GetSignatureForError ();
406                 }
407
408                 /// <summary>
409                 /// Base Emit method. This is also entry point for CLS-Compliant verification.
410                 /// </summary>
411                 public virtual void Emit ()
412                 {
413                         if (!Compiler.Settings.VerifyClsCompliance)
414                                 return;
415
416                         VerifyClsCompliance ();
417                 }
418
419                 public bool IsAvailableForReuse {
420                         get {
421                                 return (caching_flags & Flags.CanBeReused) != 0;
422                         }
423                         set {
424                                 caching_flags = value ? (caching_flags | Flags.CanBeReused) : (caching_flags & ~Flags.CanBeReused);
425                         }
426                 }
427
428                 public bool IsCompilerGenerated {
429                         get     {
430                                 if ((mod_flags & Modifiers.COMPILER_GENERATED) != 0)
431                                         return true;
432
433                                 return Parent != null && Parent.IsCompilerGenerated;
434                         }
435                 }
436
437                 public bool IsImported {
438                         get {
439                                 return false;
440                         }
441                 }
442
443                 public virtual bool IsUsed {
444                         get {
445                                 return (caching_flags & Flags.IsUsed) != 0;
446                         }
447                 }
448
449                 protected Report Report {
450                         get {
451                                 return Compiler.Report;
452                         }
453                 }
454
455                 public void SetIsUsed ()
456                 {
457                         caching_flags |= Flags.IsUsed;
458                 }
459
460                 public void SetIsAssigned ()
461                 {
462                         caching_flags |= Flags.IsAssigned;
463                 }
464
465                 public virtual void SetConstraints (List<Constraints> constraints_list)
466                 {
467                         var tparams = member_name.TypeParameters;
468                         if (tparams == null) {
469                                 Report.Error (80, Location, "Constraints are not allowed on non-generic declarations");
470                                 return;
471                         }
472
473                         foreach (var c in constraints_list) {
474                                 var tp = tparams.Find (c.TypeParameter.Value);
475                                 if (tp == null) {
476                                         Report.Error (699, c.Location, "`{0}': A constraint references nonexistent type parameter `{1}'",
477                                                 GetSignatureForError (), c.TypeParameter.Value);
478                                         continue;
479                                 }
480
481                                 tp.Constraints = c;
482                         }
483                 }
484
485                 /// <summary>
486                 /// Returns instance of ObsoleteAttribute for this MemberCore
487                 /// </summary>
488                 public virtual ObsoleteAttribute GetAttributeObsolete ()
489                 {
490                         if ((caching_flags & (Flags.Obsolete_Undetected | Flags.Obsolete)) == 0)
491                                 return null;
492
493                         caching_flags &= ~Flags.Obsolete_Undetected;
494
495                         if (OptAttributes == null)
496                                 return null;
497
498                         Attribute obsolete_attr = OptAttributes.Search (Module.PredefinedAttributes.Obsolete);
499                         if (obsolete_attr == null)
500                                 return null;
501
502                         caching_flags |= Flags.Obsolete;
503
504                         ObsoleteAttribute obsolete = obsolete_attr.GetObsoleteAttribute ();
505                         if (obsolete == null)
506                                 return null;
507
508                         return obsolete;
509                 }
510
511                 //
512                 // Checks whether the type P is as accessible as this member
513                 //
514                 public bool IsAccessibleAs (TypeSpec p)
515                 {
516                         //
517                         // if M is private, its accessibility is the same as this declspace.
518                         // we already know that P is accessible to T before this method, so we
519                         // may return true.
520                         //
521                         if ((mod_flags & Modifiers.PRIVATE) != 0)
522                                 return true;
523
524                         while (TypeManager.HasElementType (p))
525                                 p = TypeManager.GetElementType (p);
526
527                         if (p.IsGenericParameter)
528                                 return true;
529
530                         for (TypeSpec p_parent; p != null; p = p_parent) {
531                                 p_parent = p.DeclaringType;
532
533                                 if (p.IsGeneric) {
534                                         foreach (TypeSpec t in p.TypeArguments) {
535                                                 if (!IsAccessibleAs (t))
536                                                         return false;
537                                         }
538                                 }
539
540                                 var pAccess = p.Modifiers & Modifiers.AccessibilityMask;
541                                 if (pAccess == Modifiers.PUBLIC)
542                                         continue;
543
544                                 bool same_access_restrictions = false;
545                                 for (MemberCore mc = this; !same_access_restrictions && mc != null && mc.Parent != null; mc = mc.Parent) {
546                                         var al = mc.ModFlags & Modifiers.AccessibilityMask;
547                                         switch (pAccess) {
548                                         case Modifiers.INTERNAL:
549                                                 if (al == Modifiers.PRIVATE || al == Modifiers.INTERNAL)
550                                                         same_access_restrictions = p.MemberDefinition.IsInternalAsPublic (mc.Module.DeclaringAssembly);
551                                                 
552                                                 break;
553
554                                         case Modifiers.PROTECTED:
555                                                 if (al == Modifiers.PROTECTED) {
556                                                         same_access_restrictions = mc.Parent.PartialContainer.IsBaseTypeDefinition (p_parent);
557                                                         break;
558                                                 }
559
560                                                 if (al == Modifiers.PRIVATE) {
561                                                         //
562                                                         // When type is private and any of its parents derives from
563                                                         // protected type then the type is accessible
564                                                         //
565                                                         while (mc.Parent != null && mc.Parent.PartialContainer != null) {
566                                                                 if (mc.Parent.PartialContainer.IsBaseTypeDefinition (p_parent)) {
567                                                                         same_access_restrictions = true;
568                                                                         break;
569                                                                 }
570
571                                                                 mc = mc.Parent; 
572                                                         }
573                                                 }
574                                                 
575                                                 break;
576
577                                         case Modifiers.PROTECTED | Modifiers.INTERNAL:
578                                                 if (al == Modifiers.INTERNAL)
579                                                         same_access_restrictions = p.MemberDefinition.IsInternalAsPublic (mc.Module.DeclaringAssembly);
580                                                 else if (al == (Modifiers.PROTECTED | Modifiers.INTERNAL))
581                                                         same_access_restrictions = mc.Parent.PartialContainer.IsBaseTypeDefinition (p_parent) && p.MemberDefinition.IsInternalAsPublic (mc.Module.DeclaringAssembly);
582                                                 else if (al == Modifiers.PROTECTED)
583                                                         goto case Modifiers.PROTECTED;
584                                                 else if (al == Modifiers.PRIVATE) {
585                                                         if (p.MemberDefinition.IsInternalAsPublic (mc.Module.DeclaringAssembly)) {
586                                                                 same_access_restrictions = true;
587                                                         } else {
588                                                                 goto case Modifiers.PROTECTED;
589                                                         }
590                                                 }
591
592                                                 break;
593
594                                         case Modifiers.PRIVATE:
595                                                 //
596                                                 // Both are private and share same parent
597                                                 //
598                                                 if (al == Modifiers.PRIVATE) {
599                                                         var decl = mc.Parent;
600                                                         do {
601                                                                 same_access_restrictions = decl.CurrentType.MemberDefinition == p_parent.MemberDefinition;
602                                                         } while (!same_access_restrictions && !decl.PartialContainer.IsTopLevel && (decl = decl.Parent) != null);
603                                                 }
604                                                 
605                                                 break;
606                                                 
607                                         default:
608                                                 throw new InternalErrorException (al.ToString ());
609                                         }
610                                 }
611                                 
612                                 if (!same_access_restrictions)
613                                         return false;
614                         }
615
616                         return true;
617                 }
618
619                 /// <summary>
620                 /// Analyze whether CLS-Compliant verification must be execute for this MemberCore.
621                 /// </summary>
622                 public override bool IsClsComplianceRequired ()
623                 {
624                         if ((caching_flags & Flags.ClsCompliance_Undetected) == 0)
625                                 return (caching_flags & Flags.ClsCompliant) != 0;
626
627                         caching_flags &= ~Flags.ClsCompliance_Undetected;
628
629                         if (HasClsCompliantAttribute) {
630                                 if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0)
631                                         return false;
632
633                                 caching_flags |= Flags.ClsCompliant;
634                                 return true;
635                         }
636
637                         if (Parent.IsClsComplianceRequired ()) {
638                                 caching_flags |= Flags.ClsCompliant;
639                                 return true;
640                         }
641
642                         return false;
643                 }
644
645                 public virtual string[] ConditionalConditions ()
646                 {
647                         return null;
648                 }
649
650                 /// <summary>
651                 /// Returns true when MemberCore is exposed from assembly.
652                 /// </summary>
653                 public bool IsExposedFromAssembly ()
654                 {
655                         if ((ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
656                                 return this is NamespaceContainer;
657                         
658                         var parentContainer = Parent.PartialContainer;
659                         while (parentContainer != null) {
660                                 if ((parentContainer.ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
661                                         return false;
662
663                                 parentContainer = parentContainer.Parent.PartialContainer;
664                         }
665
666                         return true;
667                 }
668
669                 //
670                 // Does extension methods look up to find a method which matches name and extensionType.
671                 // Search starts from this namespace and continues hierarchically up to top level.
672                 //
673                 public ExtensionMethodCandidates LookupExtensionMethod (string name, int arity)
674                 {
675                         var m = Parent;
676                         do {
677                                 var ns = m as NamespaceContainer;
678                                 if (ns != null)
679                                         return ns.LookupExtensionMethod (this, name, arity, 0);
680
681                                 m = m.Parent;
682                         } while (m != null);
683
684                         return null;
685                 }
686
687                 public virtual FullNamedExpression LookupNamespaceAlias (string name)
688                 {
689                         return Parent.LookupNamespaceAlias (name);
690                 }
691
692                 public virtual FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
693                 {
694                         return Parent.LookupNamespaceOrType (name, arity, mode, loc);
695                 }
696
697                 /// <summary>
698                 /// Goes through class hierarchy and gets value of first found CLSCompliantAttribute.
699                 /// If no is attribute exists then assembly CLSCompliantAttribute is returned.
700                 /// </summary>
701                 public bool? CLSAttributeValue {
702                         get {
703                                 if ((caching_flags & Flags.HasCompliantAttribute_Undetected) == 0) {
704                                         if ((caching_flags & Flags.HasClsCompliantAttribute) == 0)
705                                                 return null;
706
707                                         return (caching_flags & Flags.ClsCompliantAttributeFalse) == 0;
708                                 }
709
710                                 caching_flags &= ~Flags.HasCompliantAttribute_Undetected;
711
712                                 if (OptAttributes != null) {
713                                         Attribute cls_attribute = OptAttributes.Search (Module.PredefinedAttributes.CLSCompliant);
714                                         if (cls_attribute != null) {
715                                                 caching_flags |= Flags.HasClsCompliantAttribute;
716                                                 if (cls_attribute.GetClsCompliantAttributeValue ())
717                                                         return true;
718
719                                                 caching_flags |= Flags.ClsCompliantAttributeFalse;
720                                                 return false;
721                                         }
722                                 }
723
724                                 return null;
725                         }
726                 }
727
728                 /// <summary>
729                 /// Returns true if MemberCore is explicitly marked with CLSCompliantAttribute
730                 /// </summary>
731                 protected bool HasClsCompliantAttribute {
732                         get {
733                                 return CLSAttributeValue.HasValue;
734                         }
735                 }
736
737                 /// <summary>
738                 /// Returns true when a member supports multiple overloads (methods, indexers, etc)
739                 /// </summary>
740                 public virtual bool EnableOverloadChecks (MemberCore overload)
741                 {
742                         return false;
743                 }
744
745                 /// <summary>
746                 /// The main virtual method for CLS-Compliant verifications.
747                 /// The method returns true if member is CLS-Compliant and false if member is not
748                 /// CLS-Compliant which means that CLS-Compliant tests are not necessary. A descendants override it
749                 /// and add their extra verifications.
750                 /// </summary>
751                 protected virtual bool VerifyClsCompliance ()
752                 {
753                         if (HasClsCompliantAttribute) {
754                                 if (!Module.DeclaringAssembly.HasCLSCompliantAttribute) {
755                                         Attribute a = OptAttributes.Search (Module.PredefinedAttributes.CLSCompliant);
756                                         if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0) {
757                                                 Report.Warning (3021, 2, a.Location,
758                                                         "`{0}' does not need a CLSCompliant attribute because the assembly is not marked as CLS-compliant",
759                                                         GetSignatureForError ());
760                                         } else {
761                                                 Report.Warning (3014, 1, a.Location,
762                                                         "`{0}' cannot be marked as CLS-compliant because the assembly is not marked as CLS-compliant",
763                                                         GetSignatureForError ());
764                                         }
765                                         return false;
766                                 }
767
768                                 if (!IsExposedFromAssembly ()) {
769                                         Attribute a = OptAttributes.Search (Module.PredefinedAttributes.CLSCompliant);
770                                         Report.Warning (3019, 2, a.Location, "CLS compliance checking will not be performed on `{0}' because it is not visible from outside this assembly", GetSignatureForError ());
771                                         return false;
772                                 }
773
774                                 if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0) {
775                                         if (Parent is Interface && Parent.IsClsComplianceRequired ()) {
776                                                 Report.Warning (3010, 1, Location, "`{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
777                                         } else if (Parent.Kind == MemberKind.Class && (ModFlags & Modifiers.ABSTRACT) != 0 && Parent.IsClsComplianceRequired ()) {
778                                                 Report.Warning (3011, 1, Location, "`{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
779                                         }
780
781                                         return false;
782                                 }
783
784                                 if (Parent.Kind != MemberKind.Namespace && Parent.Kind != 0 && !Parent.IsClsComplianceRequired ()) {
785                                         Attribute a = OptAttributes.Search (Module.PredefinedAttributes.CLSCompliant);
786                                         Report.Warning (3018, 1, a.Location, "`{0}' cannot be marked as CLS-compliant because it is a member of non CLS-compliant type `{1}'",
787                                                 GetSignatureForError (), Parent.GetSignatureForError ());
788                                         return false;
789                                 }
790                         } else {
791                                 if (!IsExposedFromAssembly ())
792                                         return false;
793
794                                 if (!Parent.IsClsComplianceRequired ())
795                                         return false;
796                         }
797
798                         if (member_name.Name [0] == '_') {
799                                 Warning_IdentifierNotCompliant ();
800                         }
801
802                         if (member_name.TypeParameters != null)
803                                 member_name.TypeParameters.VerifyClsCompliance ();
804
805                         return true;
806                 }
807
808                 protected void Warning_IdentifierNotCompliant ()
809                 {
810                         Report.Warning (3008, 1, MemberName.Location, "Identifier `{0}' is not CLS-compliant", GetSignatureForError ());
811                 }
812
813                 public virtual string GetCallerMemberName ()
814                 {
815                         return MemberName.Name;
816                 }
817
818                 //
819                 // Returns a string that represents the signature for this 
820                 // member which should be used in XML documentation.
821                 //
822                 public abstract string GetSignatureForDocumentation ();
823
824                 public virtual void GetCompletionStartingWith (string prefix, List<string> results)
825                 {
826                         Parent.GetCompletionStartingWith (prefix, results);
827                 }
828
829                 //
830                 // Generates xml doc comments (if any), and if required,
831                 // handle warning report.
832                 //
833                 internal virtual void GenerateDocComment (DocumentationBuilder builder)
834                 {
835                         if (DocComment == null) {
836                                 if (IsExposedFromAssembly ()) {
837                                         Constructor c = this as Constructor;
838                                         if (c == null || !c.IsDefault ())
839                                                 Report.Warning (1591, 4, Location,
840                                                         "Missing XML comment for publicly visible type or member `{0}'", GetSignatureForError ());
841                                 }
842
843                                 return;
844                         }
845
846                         try {
847                                 builder.GenerateDocumentationForMember (this);
848                         } catch (Exception e) {
849                                 throw new InternalErrorException (this, e);
850                         }
851                 }
852
853                 public virtual void WriteDebugSymbol (MonoSymbolFile file)
854                 {
855                 }
856
857                 #region IMemberContext Members
858
859                 public virtual CompilerContext Compiler {
860                         get {
861                                 return Module.Compiler;
862                         }
863                 }
864
865                 public virtual TypeSpec CurrentType {
866                         get { return Parent.CurrentType; }
867                 }
868
869                 public MemberCore CurrentMemberDefinition {
870                         get { return this; }
871                 }
872
873                 public virtual TypeParameters CurrentTypeParameters {
874                         get { return null; }
875                 }
876
877                 public bool IsObsolete {
878                         get {
879                                 if (GetAttributeObsolete () != null)
880                                         return true;
881
882                                 return Parent != null && Parent.IsObsolete;
883                         }
884                 }
885
886                 public bool IsUnsafe {
887                         get {
888                                 if ((ModFlags & Modifiers.UNSAFE) != 0)
889                                         return true;
890
891                                 return Parent != null && Parent.IsUnsafe;
892                         }
893                 }
894
895                 public bool IsStatic {
896                         get {
897                                 return (ModFlags & Modifiers.STATIC) != 0;
898                         }
899                 }
900
901                 #endregion
902         }
903
904         //
905         // Base member specification. A member specification contains
906         // member details which can alter in the context (e.g. generic instances)
907         //
908         public abstract class MemberSpec
909         {
910                 [Flags]
911                 public enum StateFlags
912                 {
913                         Obsolete_Undetected = 1,        // Obsolete attribute has not been detected yet
914                         Obsolete = 1 << 1,                      // Member has obsolete attribute
915                         CLSCompliant_Undetected = 1 << 2,       // CLSCompliant attribute has not been detected yet
916                         CLSCompliant = 1 << 3,          // Member is CLS Compliant
917                         MissingDependency_Undetected = 1 << 4,
918                         MissingDependency = 1 << 5,
919                         HasDynamicElement = 1 << 6,
920                         ConstraintsChecked = 1 << 7,
921
922                         IsAccessor = 1 << 9,            // Method is an accessor
923                         IsGeneric = 1 << 10,            // Member contains type arguments
924
925                         PendingMetaInflate = 1 << 12,
926                         PendingMakeMethod = 1 << 13,
927                         PendingMemberCacheMembers = 1 << 14,
928                         PendingBaseTypeInflate = 1 << 15,
929                         InterfacesExpanded = 1 << 16,
930                         IsNotCSharpCompatible = 1 << 17,
931                         SpecialRuntimeType = 1 << 18,
932                         InflatedExpressionType = 1 << 19,
933                         InflatedNullableType = 1 << 20,
934                         GenericIterateInterface = 1 << 21,
935                         GenericTask = 1 << 22,
936                         InterfacesImported = 1 << 23,
937                 }
938
939                 //
940                 // Some flags can be copied directly from other member
941                 //
942                 protected const StateFlags SharedStateFlags =
943                         StateFlags.CLSCompliant | StateFlags.CLSCompliant_Undetected |
944                         StateFlags.Obsolete | StateFlags.Obsolete_Undetected |
945                         StateFlags.MissingDependency | StateFlags.MissingDependency_Undetected |
946                         StateFlags.HasDynamicElement;
947
948                 protected Modifiers modifiers;
949                 public StateFlags state;
950                 protected IMemberDefinition definition;
951                 public readonly MemberKind Kind;
952                 protected TypeSpec declaringType;
953
954 #if DEBUG
955                 static int counter;
956                 public int ID = counter++;
957 #endif
958
959                 protected MemberSpec (MemberKind kind, TypeSpec declaringType, IMemberDefinition definition, Modifiers modifiers)
960                 {
961                         this.Kind = kind;
962                         this.declaringType = declaringType;
963                         this.definition = definition;
964                         this.modifiers = modifiers;
965
966                         if (kind == MemberKind.MissingType)
967                                 state = StateFlags.MissingDependency;
968                         else
969                                 state = StateFlags.Obsolete_Undetected | StateFlags.CLSCompliant_Undetected | StateFlags.MissingDependency_Undetected;
970                 }
971
972                 #region Properties
973
974                 public virtual int Arity {
975                         get {
976                                 return 0;
977                         }
978                 }
979
980                 public TypeSpec DeclaringType {
981                         get {
982                                 return declaringType;
983                         }
984                         set {
985                                 declaringType = value;
986                         }
987                 }
988
989                 public IMemberDefinition MemberDefinition {
990                         get {
991                                 return definition;
992                         }
993                 }
994
995                 public Modifiers Modifiers {
996                         get {
997                                 return modifiers;
998                         }
999                         set {
1000                                 modifiers = value;
1001                         }
1002                 }
1003                 
1004                 public virtual string Name {
1005                         get {
1006                                 return definition.Name;
1007                         }
1008                 }
1009
1010                 public bool IsAbstract {
1011                         get { return (modifiers & Modifiers.ABSTRACT) != 0; }
1012                 }
1013
1014                 public bool IsAccessor {
1015                         get {
1016                                 return (state & StateFlags.IsAccessor) != 0;
1017                         }
1018                         set {
1019                                 state = value ? state | StateFlags.IsAccessor : state & ~StateFlags.IsAccessor;
1020                         }
1021                 }
1022
1023                 //
1024                 // Return true when this member is a generic in C# terms
1025                 // A nested non-generic type of generic type will return false
1026                 //
1027                 public bool IsGeneric {
1028                         get {
1029                                 return (state & StateFlags.IsGeneric) != 0;
1030                         }
1031                         set {
1032                                 state = value ? state | StateFlags.IsGeneric : state & ~StateFlags.IsGeneric;
1033                         }
1034                 }
1035
1036                 //
1037                 // Returns true for imported members which are not compatible with C# language
1038                 //
1039                 public bool IsNotCSharpCompatible {
1040                         get {
1041                                 return (state & StateFlags.IsNotCSharpCompatible) != 0;
1042                         }
1043                         set {
1044                                 state = value ? state | StateFlags.IsNotCSharpCompatible : state & ~StateFlags.IsNotCSharpCompatible;
1045                         }
1046                 }
1047
1048                 public bool IsPrivate {
1049                         get { return (modifiers & Modifiers.PRIVATE) != 0; }
1050                 }
1051
1052                 public bool IsPublic {
1053                         get { return (modifiers & Modifiers.PUBLIC) != 0; }
1054                 }
1055
1056                 public bool IsStatic {
1057                         get { 
1058                                 return (modifiers & Modifiers.STATIC) != 0;
1059                         }
1060                 }
1061
1062                 #endregion
1063
1064                 public virtual void CheckObsoleteness (IMemberContext mc, Location loc)
1065                 {
1066                         var oa = GetAttributeObsolete ();
1067                         if (oa == null)
1068                                 return;
1069
1070                         if (!mc.IsObsolete)
1071                                 AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc, mc.Module.Compiler.Report);
1072                 }
1073
1074                 public virtual ObsoleteAttribute GetAttributeObsolete ()
1075                 {
1076                         if ((state & (StateFlags.Obsolete | StateFlags.Obsolete_Undetected)) == 0)
1077                                 return null;
1078
1079                         state &= ~StateFlags.Obsolete_Undetected;
1080
1081                         var oa = definition.GetAttributeObsolete ();
1082                         if (oa != null)
1083                                 state |= StateFlags.Obsolete;
1084
1085                         return oa;
1086                 }
1087
1088                 //
1089                 // Returns a list of missing dependencies of this member. The list
1090                 // will contain types only but it can have numerous values for members
1091                 // like methods where both return type and all parameters are checked
1092                 //
1093                 public List<MissingTypeSpecReference> GetMissingDependencies ()
1094                 {
1095                         return GetMissingDependencies (this);
1096                 }
1097
1098                 public List<MissingTypeSpecReference> GetMissingDependencies (MemberSpec caller)
1099                 {
1100                         if ((state & (StateFlags.MissingDependency | StateFlags.MissingDependency_Undetected)) == 0)
1101                                 return null;
1102
1103                         state &= ~StateFlags.MissingDependency_Undetected;
1104
1105                         var imported = definition as ImportedDefinition;
1106                         List<MissingTypeSpecReference> missing;
1107                         if (imported != null) {
1108                                 missing = ResolveMissingDependencies (caller);
1109                         } else if (this is ElementTypeSpec) {
1110                                 missing = ((ElementTypeSpec) this).Element.GetMissingDependencies (caller);
1111                         } else {
1112                                 missing = null;
1113                         }
1114
1115                         if (missing != null) {
1116                                 state |= StateFlags.MissingDependency;
1117                         }
1118
1119                         return missing;
1120                 }
1121
1122                 public abstract List<MissingTypeSpecReference> ResolveMissingDependencies (MemberSpec caller);
1123
1124                 protected virtual bool IsNotCLSCompliant (out bool attrValue)
1125                 {
1126                         var cls = MemberDefinition.CLSAttributeValue;
1127                         attrValue = cls ?? false;
1128                         return cls == false;
1129                 }
1130
1131                 public virtual string GetSignatureForDocumentation ()
1132                 {
1133                         return DeclaringType.GetSignatureForDocumentation () + "." + Name;
1134                 }
1135
1136                 public virtual string GetSignatureForError ()
1137                 {
1138                         var bf = MemberDefinition as Property.BackingFieldDeclaration;
1139                         string name;
1140                         if (bf == null) {
1141                                 name = Name;
1142                         } else {
1143                                 name = bf.OriginalProperty.MemberName.Name;
1144                         }
1145
1146                         return DeclaringType.GetSignatureForError () + "." + name;
1147                 }
1148
1149                 public virtual MemberSpec InflateMember (TypeParameterInflator inflator)
1150                 {
1151                         var inflated = (MemberSpec) MemberwiseClone ();
1152                         inflated.declaringType = inflator.TypeInstance;
1153                         if (DeclaringType.IsGenericOrParentIsGeneric)
1154                                 inflated.state |= StateFlags.PendingMetaInflate;
1155 #if DEBUG
1156                         inflated.ID += 1000000;
1157 #endif
1158                         return inflated;
1159                 }
1160
1161                 //
1162                 // Is this member accessible from invocation context
1163                 //
1164                 public bool IsAccessible (IMemberContext ctx)
1165                 {
1166                         var ma = Modifiers & Modifiers.AccessibilityMask;
1167                         if (ma == Modifiers.PUBLIC)
1168                                 return true;
1169
1170                         var parentType = /* this as TypeSpec ?? */ DeclaringType;
1171                         var ctype = ctx.CurrentType;
1172
1173                         if (ma == Modifiers.PRIVATE) {
1174                                 if (ctype == null || parentType == null)
1175                                         return false;
1176                                 //
1177                                 // It's only accessible to the current class or children
1178                                 //
1179                                 if (parentType.MemberDefinition == ctype.MemberDefinition)
1180                                         return true;
1181
1182                                 return TypeManager.IsNestedChildOf (ctype, parentType.MemberDefinition);
1183                         }
1184
1185                         if ((ma & Modifiers.INTERNAL) != 0) {
1186                                 bool b;
1187                                 var assembly = ctype == null ? ctx.Module.DeclaringAssembly : ctype.MemberDefinition.DeclaringAssembly;
1188
1189                                 if (parentType == null) {
1190                                         b = ((ITypeDefinition) MemberDefinition).IsInternalAsPublic (assembly);
1191                                 } else {
1192                                         b = DeclaringType.MemberDefinition.IsInternalAsPublic (assembly);
1193                                 }
1194
1195                                 if (b || ma == Modifiers.INTERNAL)
1196                                         return b;
1197                         }
1198
1199                         //
1200                         // Checks whether `ctype' is a subclass or nested child of `parentType'.
1201                         //
1202                         while (ctype != null) {
1203                                 if (TypeManager.IsFamilyAccessible (ctype, parentType))
1204                                         return true;
1205
1206                                 // Handle nested types.
1207                                 ctype = ctype.DeclaringType;    // TODO: Untested ???
1208                         }
1209
1210                         return false;
1211                 }
1212
1213                 //
1214                 // Returns member CLS compliance based on full member hierarchy
1215                 //
1216                 public bool IsCLSCompliant ()
1217                 {
1218                         if ((state & StateFlags.CLSCompliant_Undetected) != 0) {
1219                                 state &= ~StateFlags.CLSCompliant_Undetected;
1220
1221                                 bool compliant;
1222                                 if (IsNotCLSCompliant (out compliant))
1223                                         return false;
1224
1225                                 if (!compliant) {
1226                                         if (DeclaringType != null) {
1227                                                 compliant = DeclaringType.IsCLSCompliant ();
1228                                         } else {
1229                                                 compliant = ((ITypeDefinition) MemberDefinition).DeclaringAssembly.IsCLSCompliant;
1230                                         }
1231                                 }
1232
1233                                 if (compliant)
1234                                         state |= StateFlags.CLSCompliant;
1235                         }
1236
1237                         return (state & StateFlags.CLSCompliant) != 0;
1238                 }
1239
1240                 public bool IsConditionallyExcluded (IMemberContext ctx)
1241                 {
1242                         if ((Kind & (MemberKind.Class | MemberKind.Method)) == 0)
1243                                 return false;
1244
1245                         var conditions = MemberDefinition.ConditionalConditions ();
1246                         if (conditions == null)
1247                                 return false;
1248
1249                         var m = ctx.CurrentMemberDefinition;
1250                         CompilationSourceFile unit = null;
1251                         while (m != null && unit == null) {
1252                                 unit = m as CompilationSourceFile;
1253                                 m = m.Parent;
1254                         }
1255
1256                         if (unit != null) {
1257                                 foreach (var condition in conditions) {
1258                                         if (unit.IsConditionalDefined (condition))
1259                                                 return false;
1260                                 }
1261                         }
1262
1263                         return true;
1264                 }
1265
1266                 public override string ToString ()
1267                 {
1268                         return GetSignatureForError ();
1269                 }
1270         }
1271
1272         //
1273         // Member details which are same between all member
1274         // specifications
1275         //
1276         public interface IMemberDefinition
1277         {
1278                 bool? CLSAttributeValue { get; }
1279                 string Name { get; }
1280                 bool IsImported { get; }
1281
1282                 string[] ConditionalConditions ();
1283                 ObsoleteAttribute GetAttributeObsolete ();
1284                 void SetIsAssigned ();
1285                 void SetIsUsed ();
1286         }
1287
1288         public interface IMethodDefinition : IMemberDefinition
1289         {
1290                 MethodBase Metadata { get; }
1291         }
1292
1293         public interface IParametersMember : IInterfaceMemberSpec
1294         {
1295                 AParametersCollection Parameters { get; }
1296         }
1297
1298         public interface IInterfaceMemberSpec
1299         {
1300                 TypeSpec MemberType { get; }
1301         }
1302 }