2 // decl.cs: Declaration base class for structs, classes, enums and interfaces.
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 // Marek Safar (marek.safar@seznam.cz)
7 // Dual licensed under the terms of the MIT X11 or GNU GPL
9 // Copyright 2001 Ximian, Inc (http://www.ximian.com)
10 // Copyright 2004-2008 Novell, Inc
11 // Copyright 2011 Xamarin Inc
16 using System.Collections.Generic;
17 using System.Diagnostics;
19 using Mono.CompilerServices.SymbolWriter;
22 using XmlElement = System.Object;
28 using IKVM.Reflection;
29 using IKVM.Reflection.Emit;
31 using System.Reflection;
32 using System.Reflection.Emit;
35 namespace Mono.CSharp {
38 // Better name would be DottenName
40 [DebuggerDisplay ("{GetSignatureForError()}")]
41 public class MemberName
43 public static readonly MemberName Null = new MemberName ("");
45 public readonly string Name;
46 public TypeParameters TypeParameters;
47 public readonly FullNamedExpression ExplicitInterface;
48 public readonly Location Location;
50 public readonly MemberName Left;
52 public MemberName (string name)
53 : this (name, Location.Null)
56 public MemberName (string name, Location loc)
57 : this (null, name, loc)
60 public MemberName (string name, TypeParameters tparams, Location loc)
65 this.TypeParameters = tparams;
68 public MemberName (string name, TypeParameters tparams, FullNamedExpression explicitInterface, Location loc)
69 : this (name, tparams, loc)
71 this.ExplicitInterface = explicitInterface;
74 public MemberName (MemberName left, string name, Location loc)
81 public MemberName (MemberName left, string name, FullNamedExpression explicitInterface, Location loc)
82 : this (left, name, loc)
84 this.ExplicitInterface = explicitInterface;
87 public MemberName (MemberName left, MemberName right)
89 this.Name = right.Name;
90 this.Location = right.Location;
91 this.TypeParameters = right.TypeParameters;
97 return TypeParameters == null ? 0 : TypeParameters.Count;
101 public bool IsGeneric {
103 return TypeParameters != null;
107 public string Basename {
109 if (TypeParameters != null)
110 return MakeName (Name, TypeParameters);
115 public void CreateMetadataName (StringBuilder sb)
118 Left.CreateMetadataName (sb);
120 if (sb.Length != 0) {
124 sb.Append (Basename);
127 public string GetSignatureForDocumentation ()
131 if (ExplicitInterface != null)
132 s = ExplicitInterface.GetSignatureForError () + "." + s;
137 return Left.GetSignatureForDocumentation () + "." + s;
140 public string GetSignatureForError ()
142 string s = TypeParameters == null ? null : "<" + TypeParameters.GetSignatureForError () + ">";
145 if (ExplicitInterface != null)
146 s = ExplicitInterface.GetSignatureForError () + "." + s;
151 return Left.GetSignatureForError () + "." + s;
154 public override bool Equals (object other)
156 return Equals (other as MemberName);
159 public bool Equals (MemberName other)
163 if (other == null || Name != other.Name)
166 if ((TypeParameters != null) &&
167 (other.TypeParameters == null || TypeParameters.Count != other.TypeParameters.Count))
170 if ((TypeParameters == null) && (other.TypeParameters != null))
174 return other.Left == null;
176 return Left.Equals (other.Left);
179 public override int GetHashCode ()
181 int hash = Name.GetHashCode ();
182 for (MemberName n = Left; n != null; n = n.Left)
183 hash ^= n.Name.GetHashCode ();
185 if (TypeParameters != null)
186 hash ^= TypeParameters.Count << 5;
188 return hash & 0x7FFFFFFF;
191 public static string MakeName (string name, TypeParameters args)
196 return name + "`" + args.Count;
199 public static string MakeName (string name, int count)
201 return name + "`" + count;
205 public class SimpleMemberName
208 public Location Location;
210 public SimpleMemberName (string name, Location loc)
218 /// Base representation for members. This is used to keep track
219 /// of Name, Location and Modifier flags, and handling Attributes.
221 [System.Diagnostics.DebuggerDisplay ("{GetSignatureForError()}")]
222 public abstract class MemberCore : Attributable, IMemberContext, IMemberDefinition
224 string IMemberDefinition.Name {
226 return member_name.Name;
230 // Is not readonly because of IndexerName attribute
231 private MemberName member_name;
232 public MemberName MemberName {
233 get { return member_name; }
237 /// Modifier flags that the user specified in the source code
239 private Modifiers mod_flags;
240 public Modifiers ModFlags {
243 if ((value & Modifiers.COMPILER_GENERATED) != 0)
244 caching_flags = Flags.IsUsed | Flags.IsAssigned;
251 public virtual ModuleContainer Module {
253 return Parent.Module;
257 public /*readonly*/ TypeContainer Parent;
260 /// Location where this declaration happens
262 public Location Location {
263 get { return member_name.Location; }
267 /// XML documentation comment
269 protected string comment;
272 /// Represents header string for documentation comment
273 /// for each member types.
275 public abstract string DocCommentHeader { get; }
279 Obsolete_Undetected = 1, // Obsolete attribute has not been detected yet
280 Obsolete = 1 << 1, // Type has obsolete attribute
281 ClsCompliance_Undetected = 1 << 2, // CLS Compliance has not been detected yet
282 ClsCompliant = 1 << 3, // Type is CLS Compliant
283 CloseTypeCreated = 1 << 4, // Tracks whether we have Closed the type
284 HasCompliantAttribute_Undetected = 1 << 5, // Presence of CLSCompliantAttribute has not been detected
285 HasClsCompliantAttribute = 1 << 6, // Type has CLSCompliantAttribute
286 ClsCompliantAttributeFalse = 1 << 7, // Member has CLSCompliant(false)
287 Excluded_Undetected = 1 << 8, // Conditional attribute has not been detected yet
288 Excluded = 1 << 9, // Method is conditional
289 MethodOverloadsExist = 1 << 10, // Test for duplication must be performed
291 IsAssigned = 1 << 12, // Field is assigned
292 HasExplicitLayout = 1 << 13,
293 PartialDefinitionExists = 1 << 14, // Set when corresponding partial method definition exists
294 HasStructLayout = 1 << 15, // Has StructLayoutAttribute
295 HasInstanceConstructor = 1 << 16,
296 HasUserOperators = 1 << 17,
297 CanBeReused = 1 << 18
301 /// MemberCore flags at first detected then cached
303 internal Flags caching_flags;
305 public MemberCore (TypeContainer parent, MemberName name, Attributes attrs)
307 this.Parent = parent;
309 caching_flags = Flags.Obsolete_Undetected | Flags.ClsCompliance_Undetected | Flags.HasCompliantAttribute_Undetected | Flags.Excluded_Undetected;
310 AddAttributes (attrs, this);
313 protected virtual void SetMemberName (MemberName new_name)
315 member_name = new_name;
318 public virtual void Accept (StructuralVisitor visitor)
320 visitor.Visit (this);
323 protected bool CheckAbstractAndExtern (bool has_block)
325 if (Parent.PartialContainer.Kind == MemberKind.Interface)
329 if ((ModFlags & Modifiers.EXTERN) != 0) {
330 Report.Error (179, Location, "`{0}' cannot declare a body because it is marked extern",
331 GetSignatureForError ());
335 if ((ModFlags & Modifiers.ABSTRACT) != 0) {
336 Report.Error (500, Location, "`{0}' cannot declare a body because it is marked abstract",
337 GetSignatureForError ());
341 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN | Modifiers.PARTIAL)) == 0 && !(Parent is Delegate)) {
342 if (Compiler.Settings.Version >= LanguageVersion.V_3) {
343 Property.PropertyMethod pm = this as Property.PropertyMethod;
344 if (pm is Indexer.GetIndexerMethod || pm is Indexer.SetIndexerMethod)
347 if (pm != null && pm.Property.AccessorSecond == null) {
348 Report.Error (840, Location,
349 "`{0}' must have a body because it is not marked abstract or extern. The property can be automatically implemented when you define both accessors",
350 GetSignatureForError ());
355 Report.Error (501, Location, "`{0}' must have a body because it is not marked abstract, extern, or partial",
356 GetSignatureForError ());
364 protected void CheckProtectedModifier ()
366 if ((ModFlags & Modifiers.PROTECTED) == 0)
369 if (Parent.PartialContainer.Kind == MemberKind.Struct) {
370 Report.Error (666, Location, "`{0}': Structs cannot contain protected members",
371 GetSignatureForError ());
375 if ((Parent.ModFlags & Modifiers.STATIC) != 0) {
376 Report.Error (1057, Location, "`{0}': Static classes cannot contain protected members",
377 GetSignatureForError ());
381 if ((Parent.ModFlags & Modifiers.SEALED) != 0 && (ModFlags & Modifiers.OVERRIDE) == 0 &&
382 !(this is Destructor)) {
383 Report.Warning (628, 4, Location, "`{0}': new protected member declared in sealed class",
384 GetSignatureForError ());
389 public abstract bool Define ();
391 public virtual string DocComment {
401 // Returns full member name for error message
403 public virtual string GetSignatureForError ()
405 var parent = Parent.GetSignatureForError ();
407 return member_name.GetSignatureForError ();
409 return parent + "." + member_name.GetSignatureForError ();
413 /// Base Emit method. This is also entry point for CLS-Compliant verification.
415 public virtual void Emit ()
417 if (!Compiler.Settings.VerifyClsCompliance)
420 VerifyClsCompliance ();
423 public bool IsAvailableForReuse {
425 return (caching_flags & Flags.CanBeReused) != 0;
428 caching_flags = value ? (caching_flags | Flags.CanBeReused) : (caching_flags & ~Flags.CanBeReused);
432 public bool IsCompilerGenerated {
434 if ((mod_flags & Modifiers.COMPILER_GENERATED) != 0)
437 return Parent == null ? false : Parent.IsCompilerGenerated;
441 public bool IsImported {
447 public virtual bool IsUsed {
449 return (caching_flags & Flags.IsUsed) != 0;
453 protected Report Report {
455 return Compiler.Report;
459 public void SetIsUsed ()
461 caching_flags |= Flags.IsUsed;
464 public void SetIsAssigned ()
466 caching_flags |= Flags.IsAssigned;
469 public void SetConstraints (List<Constraints> constraints_list)
471 var tparams = member_name.TypeParameters;
472 if (tparams == null) {
473 Report.Error (80, Location, "Constraints are not allowed on non-generic declarations");
477 foreach (var c in constraints_list) {
478 var tp = tparams.Find (c.TypeParameter.Value);
480 Report.Error (699, c.Location, "`{0}': A constraint references nonexistent type parameter `{1}'",
481 GetSignatureForError (), c.TypeParameter.Value);
490 /// Returns instance of ObsoleteAttribute for this MemberCore
492 public virtual ObsoleteAttribute GetAttributeObsolete ()
494 if ((caching_flags & (Flags.Obsolete_Undetected | Flags.Obsolete)) == 0)
497 caching_flags &= ~Flags.Obsolete_Undetected;
499 if (OptAttributes == null)
502 Attribute obsolete_attr = OptAttributes.Search (Module.PredefinedAttributes.Obsolete);
503 if (obsolete_attr == null)
506 caching_flags |= Flags.Obsolete;
508 ObsoleteAttribute obsolete = obsolete_attr.GetObsoleteAttribute ();
509 if (obsolete == null)
516 /// Checks for ObsoleteAttribute presence. It's used for testing of all non-types elements
518 public virtual void CheckObsoleteness (Location loc)
520 ObsoleteAttribute oa = GetAttributeObsolete ();
522 AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc, Report);
526 // Checks whether the type P is as accessible as this member
528 public bool IsAccessibleAs (TypeSpec p)
531 // if M is private, its accessibility is the same as this declspace.
532 // we already know that P is accessible to T before this method, so we
535 if ((mod_flags & Modifiers.PRIVATE) != 0)
538 while (TypeManager.HasElementType (p))
539 p = TypeManager.GetElementType (p);
541 if (p.IsGenericParameter)
544 for (TypeSpec p_parent; p != null; p = p_parent) {
545 p_parent = p.DeclaringType;
548 foreach (TypeSpec t in p.TypeArguments) {
549 if (!IsAccessibleAs (t))
554 var pAccess = p.Modifiers & Modifiers.AccessibilityMask;
555 if (pAccess == Modifiers.PUBLIC)
558 bool same_access_restrictions = false;
559 for (MemberCore mc = this; !same_access_restrictions && mc != null && mc.Parent != null; mc = mc.Parent) {
560 var al = mc.ModFlags & Modifiers.AccessibilityMask;
562 case Modifiers.INTERNAL:
563 if (al == Modifiers.PRIVATE || al == Modifiers.INTERNAL)
564 same_access_restrictions = p.MemberDefinition.IsInternalAsPublic (mc.Module.DeclaringAssembly);
568 case Modifiers.PROTECTED:
569 if (al == Modifiers.PROTECTED) {
570 same_access_restrictions = mc.Parent.PartialContainer.IsBaseTypeDefinition (p_parent);
574 if (al == Modifiers.PRIVATE) {
576 // When type is private and any of its parents derives from
577 // protected type then the type is accessible
579 while (mc.Parent != null && mc.Parent.PartialContainer != null) {
580 if (mc.Parent.PartialContainer.IsBaseTypeDefinition (p_parent))
581 same_access_restrictions = true;
588 case Modifiers.PROTECTED | Modifiers.INTERNAL:
589 if (al == Modifiers.INTERNAL)
590 same_access_restrictions = p.MemberDefinition.IsInternalAsPublic (mc.Module.DeclaringAssembly);
591 else if (al == (Modifiers.PROTECTED | Modifiers.INTERNAL))
592 same_access_restrictions = mc.Parent.PartialContainer.IsBaseTypeDefinition (p_parent) && p.MemberDefinition.IsInternalAsPublic (mc.Module.DeclaringAssembly);
594 goto case Modifiers.PROTECTED;
598 case Modifiers.PRIVATE:
600 // Both are private and share same parent
602 if (al == Modifiers.PRIVATE) {
603 var decl = mc.Parent;
605 same_access_restrictions = decl.CurrentType == p_parent;
606 } while (!same_access_restrictions && !decl.PartialContainer.IsTopLevel && (decl = decl.Parent) != null);
612 throw new InternalErrorException (al.ToString ());
616 if (!same_access_restrictions)
624 /// Analyze whether CLS-Compliant verification must be execute for this MemberCore.
626 public override bool IsClsComplianceRequired ()
628 if ((caching_flags & Flags.ClsCompliance_Undetected) == 0)
629 return (caching_flags & Flags.ClsCompliant) != 0;
631 caching_flags &= ~Flags.ClsCompliance_Undetected;
633 if (HasClsCompliantAttribute) {
634 if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0)
637 caching_flags |= Flags.ClsCompliant;
641 if (Parent.IsClsComplianceRequired ()) {
642 caching_flags |= Flags.ClsCompliant;
649 public virtual string[] ConditionalConditions ()
655 /// Returns true when MemberCore is exposed from assembly.
657 public bool IsExposedFromAssembly ()
659 if ((ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
660 return this is NamespaceContainer;
662 var parentContainer = Parent.PartialContainer;
663 while (parentContainer != null) {
664 if ((parentContainer.ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
667 parentContainer = parentContainer.Parent.PartialContainer;
674 // Does extension methods look up to find a method which matches name and extensionType.
675 // Search starts from this namespace and continues hierarchically up to top level.
677 public ExtensionMethodCandidates LookupExtensionMethod (TypeSpec extensionType, string name, int arity)
681 var ns = m as NamespaceContainer;
683 return ns.LookupExtensionMethod (this, extensionType, name, arity, 0);
691 public virtual FullNamedExpression LookupNamespaceAlias (string name)
693 return Parent.LookupNamespaceAlias (name);
696 public virtual FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
698 return Parent.LookupNamespaceOrType (name, arity, mode, loc);
702 /// Goes through class hierarchy and gets value of first found CLSCompliantAttribute.
703 /// If no is attribute exists then assembly CLSCompliantAttribute is returned.
705 public bool? CLSAttributeValue {
707 if ((caching_flags & Flags.HasCompliantAttribute_Undetected) == 0) {
708 if ((caching_flags & Flags.HasClsCompliantAttribute) == 0)
711 return (caching_flags & Flags.ClsCompliantAttributeFalse) == 0;
714 caching_flags &= ~Flags.HasCompliantAttribute_Undetected;
716 if (OptAttributes != null) {
717 Attribute cls_attribute = OptAttributes.Search (Module.PredefinedAttributes.CLSCompliant);
718 if (cls_attribute != null) {
719 caching_flags |= Flags.HasClsCompliantAttribute;
720 if (cls_attribute.GetClsCompliantAttributeValue ())
723 caching_flags |= Flags.ClsCompliantAttributeFalse;
733 /// Returns true if MemberCore is explicitly marked with CLSCompliantAttribute
735 protected bool HasClsCompliantAttribute {
737 return CLSAttributeValue.HasValue;
742 /// Returns true when a member supports multiple overloads (methods, indexers, etc)
744 public virtual bool EnableOverloadChecks (MemberCore overload)
750 /// The main virtual method for CLS-Compliant verifications.
751 /// The method returns true if member is CLS-Compliant and false if member is not
752 /// CLS-Compliant which means that CLS-Compliant tests are not necessary. A descendants override it
753 /// and add their extra verifications.
755 protected virtual bool VerifyClsCompliance ()
757 if (HasClsCompliantAttribute) {
758 if (!Module.DeclaringAssembly.HasCLSCompliantAttribute) {
759 Attribute a = OptAttributes.Search (Module.PredefinedAttributes.CLSCompliant);
760 if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0) {
761 Report.Warning (3021, 2, a.Location,
762 "`{0}' does not need a CLSCompliant attribute because the assembly is not marked as CLS-compliant",
763 GetSignatureForError ());
765 Report.Warning (3014, 1, a.Location,
766 "`{0}' cannot be marked as CLS-compliant because the assembly is not marked as CLS-compliant",
767 GetSignatureForError ());
772 if (!IsExposedFromAssembly ()) {
773 Attribute a = OptAttributes.Search (Module.PredefinedAttributes.CLSCompliant);
774 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 ());
778 if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0) {
779 if (Parent is Interface && Parent.IsClsComplianceRequired ()) {
780 Report.Warning (3010, 1, Location, "`{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
781 } else if (Parent.Kind == MemberKind.Class && (ModFlags & Modifiers.ABSTRACT) != 0 && Parent.IsClsComplianceRequired ()) {
782 Report.Warning (3011, 1, Location, "`{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
788 if (Parent.Kind != MemberKind.Namespace && Parent.Kind != 0 && !Parent.IsClsComplianceRequired ()) {
789 Attribute a = OptAttributes.Search (Module.PredefinedAttributes.CLSCompliant);
790 Report.Warning (3018, 1, a.Location, "`{0}' cannot be marked as CLS-compliant because it is a member of non CLS-compliant type `{1}'",
791 GetSignatureForError (), Parent.GetSignatureForError ());
795 if (!IsExposedFromAssembly ())
798 if (!Parent.IsClsComplianceRequired ())
802 if (member_name.Name [0] == '_') {
803 Warning_IdentifierNotCompliant ();
806 if (member_name.TypeParameters != null)
807 member_name.TypeParameters.VerifyClsCompliance ();
812 protected void Warning_IdentifierNotCompliant ()
814 Report.Warning (3008, 1, MemberName.Location, "Identifier `{0}' is not CLS-compliant", GetSignatureForError ());
817 public virtual string GetCallerMemberName ()
819 return MemberName.Name;
823 // Returns a string that represents the signature for this
824 // member which should be used in XML documentation.
826 public abstract string GetSignatureForDocumentation ();
828 public virtual void GetCompletionStartingWith (string prefix, List<string> results)
830 Parent.GetCompletionStartingWith (prefix, results);
834 // Generates xml doc comments (if any), and if required,
835 // handle warning report.
837 internal virtual void GenerateDocComment (DocumentationBuilder builder)
839 if (DocComment == null) {
840 if (IsExposedFromAssembly ()) {
841 Constructor c = this as Constructor;
842 if (c == null || !c.IsDefault ())
843 Report.Warning (1591, 4, Location,
844 "Missing XML comment for publicly visible type or member `{0}'", GetSignatureForError ());
851 builder.GenerateDocumentationForMember (this);
852 } catch (Exception e) {
853 throw new InternalErrorException (this, e);
857 public virtual void WriteDebugSymbol (MonoSymbolFile file)
861 #region IMemberContext Members
863 public virtual CompilerContext Compiler {
865 return Module.Compiler;
869 public virtual TypeSpec CurrentType {
870 get { return Parent.CurrentType; }
873 public MemberCore CurrentMemberDefinition {
877 public virtual TypeParameters CurrentTypeParameters {
881 public bool IsObsolete {
883 if (GetAttributeObsolete () != null)
886 return Parent == null ? false : Parent.IsObsolete;
890 public bool IsUnsafe {
892 if ((ModFlags & Modifiers.UNSAFE) != 0)
895 return Parent == null ? false : Parent.IsUnsafe;
899 public bool IsStatic {
901 return (ModFlags & Modifiers.STATIC) != 0;
909 // Base member specification. A member specification contains
910 // member details which can alter in the context (e.g. generic instances)
912 public abstract class MemberSpec
915 public enum StateFlags
917 Obsolete_Undetected = 1, // Obsolete attribute has not been detected yet
918 Obsolete = 1 << 1, // Member has obsolete attribute
919 CLSCompliant_Undetected = 1 << 2, // CLSCompliant attribute has not been detected yet
920 CLSCompliant = 1 << 3, // Member is CLS Compliant
921 MissingDependency_Undetected = 1 << 4,
922 MissingDependency = 1 << 5,
923 HasDynamicElement = 1 << 6,
924 ConstraintsChecked = 1 << 7,
926 IsAccessor = 1 << 9, // Method is an accessor
927 IsGeneric = 1 << 10, // Member contains type arguments
929 PendingMetaInflate = 1 << 12,
930 PendingMakeMethod = 1 << 13,
931 PendingMemberCacheMembers = 1 << 14,
932 PendingBaseTypeInflate = 1 << 15,
933 InterfacesExpanded = 1 << 16,
934 IsNotCSharpCompatible = 1 << 17,
935 SpecialRuntimeType = 1 << 18,
936 InflatedExpressionType = 1 << 19,
937 InflatedNullableType = 1 << 20,
938 GenericIterateInterface = 1 << 21,
939 GenericTask = 1 << 22
943 // Some flags can be copied directly from other member
945 protected const StateFlags SharedStateFlags =
946 StateFlags.CLSCompliant | StateFlags.CLSCompliant_Undetected |
947 StateFlags.Obsolete | StateFlags.Obsolete_Undetected |
948 StateFlags.MissingDependency | StateFlags.MissingDependency_Undetected |
949 StateFlags.HasDynamicElement;
951 protected Modifiers modifiers;
952 public StateFlags state;
953 protected IMemberDefinition definition;
954 public readonly MemberKind Kind;
955 protected TypeSpec declaringType;
959 public int ID = counter++;
962 protected MemberSpec (MemberKind kind, TypeSpec declaringType, IMemberDefinition definition, Modifiers modifiers)
965 this.declaringType = declaringType;
966 this.definition = definition;
967 this.modifiers = modifiers;
969 state = StateFlags.Obsolete_Undetected | StateFlags.CLSCompliant_Undetected | StateFlags.MissingDependency_Undetected;
974 public virtual int Arity {
980 public TypeSpec DeclaringType {
982 return declaringType;
985 declaringType = value;
989 public IMemberDefinition MemberDefinition {
995 public Modifiers Modifiers {
1004 public virtual string Name {
1006 return definition.Name;
1010 public bool IsAbstract {
1011 get { return (modifiers & Modifiers.ABSTRACT) != 0; }
1014 public bool IsAccessor {
1016 return (state & StateFlags.IsAccessor) != 0;
1019 state = value ? state | StateFlags.IsAccessor : state & ~StateFlags.IsAccessor;
1024 // Return true when this member is a generic in C# terms
1025 // A nested non-generic type of generic type will return false
1027 public bool IsGeneric {
1029 return (state & StateFlags.IsGeneric) != 0;
1032 state = value ? state | StateFlags.IsGeneric : state & ~StateFlags.IsGeneric;
1037 // Returns true for imported members which are not compatible with C# language
1039 public bool IsNotCSharpCompatible {
1041 return (state & StateFlags.IsNotCSharpCompatible) != 0;
1044 state = value ? state | StateFlags.IsNotCSharpCompatible : state & ~StateFlags.IsNotCSharpCompatible;
1048 public bool IsPrivate {
1049 get { return (modifiers & Modifiers.PRIVATE) != 0; }
1052 public bool IsPublic {
1053 get { return (modifiers & Modifiers.PUBLIC) != 0; }
1056 public bool IsStatic {
1058 return (modifiers & Modifiers.STATIC) != 0;
1064 public virtual ObsoleteAttribute GetAttributeObsolete ()
1066 if ((state & (StateFlags.Obsolete | StateFlags.Obsolete_Undetected)) == 0)
1069 state &= ~StateFlags.Obsolete_Undetected;
1071 var oa = definition.GetAttributeObsolete ();
1073 state |= StateFlags.Obsolete;
1079 // Returns a list of missing dependencies of this member. The list
1080 // will contain types only but it can have numerous values for members
1081 // like methods where both return type and all parameters are checked
1083 public List<TypeSpec> GetMissingDependencies ()
1085 if ((state & (StateFlags.MissingDependency | StateFlags.MissingDependency_Undetected)) == 0)
1088 state &= ~StateFlags.MissingDependency_Undetected;
1090 var imported = definition as ImportedDefinition;
1091 List<TypeSpec> missing;
1092 if (imported != null) {
1093 missing = ResolveMissingDependencies ();
1094 } else if (this is ElementTypeSpec) {
1095 missing = ((ElementTypeSpec) this).Element.GetMissingDependencies ();
1100 if (missing != null) {
1101 state |= StateFlags.MissingDependency;
1107 public abstract List<TypeSpec> ResolveMissingDependencies ();
1109 protected virtual bool IsNotCLSCompliant (out bool attrValue)
1111 var cls = MemberDefinition.CLSAttributeValue;
1112 attrValue = cls ?? false;
1113 return cls == false;
1116 public virtual string GetSignatureForDocumentation ()
1118 return DeclaringType.GetSignatureForDocumentation () + "." + Name;
1121 public virtual string GetSignatureForError ()
1123 var bf = MemberDefinition as Property.BackingField;
1128 name = bf.OriginalProperty.MemberName.Name;
1131 return DeclaringType.GetSignatureForError () + "." + name;
1134 public virtual MemberSpec InflateMember (TypeParameterInflator inflator)
1136 var inflated = (MemberSpec) MemberwiseClone ();
1137 inflated.declaringType = inflator.TypeInstance;
1138 if (DeclaringType.IsGenericOrParentIsGeneric)
1139 inflated.state |= StateFlags.PendingMetaInflate;
1141 inflated.ID += 1000000;
1147 // Is this member accessible from invocation context
1149 public bool IsAccessible (IMemberContext ctx)
1151 var ma = Modifiers & Modifiers.AccessibilityMask;
1152 if (ma == Modifiers.PUBLIC)
1155 var parentType = /* this as TypeSpec ?? */ DeclaringType;
1156 var ctype = ctx.CurrentType;
1158 if (ma == Modifiers.PRIVATE) {
1162 // It's only accessible to the current class or children
1164 if (parentType.MemberDefinition == ctype.MemberDefinition)
1167 return TypeManager.IsNestedChildOf (ctype, parentType.MemberDefinition);
1170 if ((ma & Modifiers.INTERNAL) != 0) {
1172 var assembly = ctype == null ? ctx.Module.DeclaringAssembly : ctype.MemberDefinition.DeclaringAssembly;
1174 if (parentType == null) {
1175 b = ((ITypeDefinition) MemberDefinition).IsInternalAsPublic (assembly);
1177 b = DeclaringType.MemberDefinition.IsInternalAsPublic (assembly);
1180 if (b || ma == Modifiers.INTERNAL)
1185 // Checks whether `ctype' is a subclass or nested child of `parentType'.
1187 while (ctype != null) {
1188 if (TypeManager.IsFamilyAccessible (ctype, parentType))
1191 // Handle nested types.
1192 ctype = ctype.DeclaringType; // TODO: Untested ???
1199 // Returns member CLS compliance based on full member hierarchy
1201 public bool IsCLSCompliant ()
1203 if ((state & StateFlags.CLSCompliant_Undetected) != 0) {
1204 state &= ~StateFlags.CLSCompliant_Undetected;
1207 if (IsNotCLSCompliant (out compliant))
1211 if (DeclaringType != null) {
1212 compliant = DeclaringType.IsCLSCompliant ();
1214 compliant = ((ITypeDefinition) MemberDefinition).DeclaringAssembly.IsCLSCompliant;
1219 state |= StateFlags.CLSCompliant;
1222 return (state & StateFlags.CLSCompliant) != 0;
1225 public bool IsConditionallyExcluded (IMemberContext ctx, Location loc)
1227 if ((Kind & (MemberKind.Class | MemberKind.Method)) == 0)
1230 var conditions = MemberDefinition.ConditionalConditions ();
1231 if (conditions == null)
1234 var m = ctx.CurrentMemberDefinition;
1235 CompilationSourceFile unit = null;
1236 while (m != null && unit == null) {
1237 unit = m as CompilationSourceFile;
1242 foreach (var condition in conditions) {
1243 if (unit.IsConditionalDefined (condition))
1251 public override string ToString ()
1253 return GetSignatureForError ();
1258 // Member details which are same between all member
1261 public interface IMemberDefinition
1263 bool? CLSAttributeValue { get; }
1264 string Name { get; }
1265 bool IsImported { get; }
1267 string[] ConditionalConditions ();
1268 ObsoleteAttribute GetAttributeObsolete ();
1269 void SetIsAssigned ();
1273 public interface IParametersMember : IInterfaceMemberSpec
1275 AParametersCollection Parameters { get; }
1278 public interface IInterfaceMemberSpec
1280 TypeSpec MemberType { get; }