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;
200 public class SimpleMemberName
203 public Location Location;
205 public SimpleMemberName (string name, Location loc)
213 /// Base representation for members. This is used to keep track
214 /// of Name, Location and Modifier flags, and handling Attributes.
216 [System.Diagnostics.DebuggerDisplay ("{GetSignatureForError()}")]
217 public abstract class MemberCore : Attributable, IMemberContext, IMemberDefinition
219 string IMemberDefinition.Name {
221 return member_name.Name;
225 // Is not readonly because of IndexerName attribute
226 private MemberName member_name;
227 public MemberName MemberName {
228 get { return member_name; }
232 /// Modifier flags that the user specified in the source code
234 private Modifiers mod_flags;
235 public Modifiers ModFlags {
238 if ((value & Modifiers.COMPILER_GENERATED) != 0)
239 caching_flags = Flags.IsUsed | Flags.IsAssigned;
246 public virtual ModuleContainer Module {
248 return Parent.Module;
252 public /*readonly*/ TypeContainer Parent;
255 /// Location where this declaration happens
257 public Location Location {
258 get { return member_name.Location; }
262 /// XML documentation comment
264 protected string comment;
267 /// Represents header string for documentation comment
268 /// for each member types.
270 public abstract string DocCommentHeader { get; }
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
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
297 /// MemberCore flags at first detected then cached
299 internal Flags caching_flags;
301 protected MemberCore (TypeContainer parent, MemberName name, Attributes attrs)
303 this.Parent = parent;
305 caching_flags = Flags.Obsolete_Undetected | Flags.ClsCompliance_Undetected | Flags.HasCompliantAttribute_Undetected | Flags.Excluded_Undetected;
306 AddAttributes (attrs, this);
309 protected virtual void SetMemberName (MemberName new_name)
311 member_name = new_name;
314 public virtual void Accept (StructuralVisitor visitor)
316 visitor.Visit (this);
319 protected bool CheckAbstractAndExtern (bool has_block)
321 if (Parent.PartialContainer.Kind == MemberKind.Interface)
325 if ((ModFlags & Modifiers.EXTERN) != 0) {
326 Report.Error (179, Location, "`{0}' cannot declare a body because it is marked extern",
327 GetSignatureForError ());
331 if ((ModFlags & Modifiers.ABSTRACT) != 0) {
332 Report.Error (500, Location, "`{0}' cannot declare a body because it is marked abstract",
333 GetSignatureForError ());
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)
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 ());
351 Report.Error (501, Location, "`{0}' must have a body because it is not marked abstract, extern, or partial",
352 GetSignatureForError ());
360 protected void CheckProtectedModifier ()
362 if ((ModFlags & Modifiers.PROTECTED) == 0)
365 if (Parent.PartialContainer.Kind == MemberKind.Struct) {
366 Report.Error (666, Location, "`{0}': Structs cannot contain protected members",
367 GetSignatureForError ());
371 if ((Parent.ModFlags & Modifiers.STATIC) != 0) {
372 Report.Error (1057, Location, "`{0}': Static classes cannot contain protected members",
373 GetSignatureForError ());
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 ());
385 public abstract bool Define ();
387 public virtual string DocComment {
397 // Returns full member name for error message
399 public virtual string GetSignatureForError ()
401 var parent = Parent.GetSignatureForError ();
403 return member_name.GetSignatureForError ();
405 return parent + "." + member_name.GetSignatureForError ();
409 /// Base Emit method. This is also entry point for CLS-Compliant verification.
411 public virtual void Emit ()
413 if (!Compiler.Settings.VerifyClsCompliance)
416 VerifyClsCompliance ();
419 public bool IsAvailableForReuse {
421 return (caching_flags & Flags.CanBeReused) != 0;
424 caching_flags = value ? (caching_flags | Flags.CanBeReused) : (caching_flags & ~Flags.CanBeReused);
428 public bool IsCompilerGenerated {
430 if ((mod_flags & Modifiers.COMPILER_GENERATED) != 0)
433 return Parent != null && Parent.IsCompilerGenerated;
437 public bool IsImported {
443 public virtual bool IsUsed {
445 return (caching_flags & Flags.IsUsed) != 0;
449 protected Report Report {
451 return Compiler.Report;
455 public void SetIsUsed ()
457 caching_flags |= Flags.IsUsed;
460 public void SetIsAssigned ()
462 caching_flags |= Flags.IsAssigned;
465 public virtual void SetConstraints (List<Constraints> constraints_list)
467 var tparams = member_name.TypeParameters;
468 if (tparams == null) {
469 Report.Error (80, Location, "Constraints are not allowed on non-generic declarations");
473 foreach (var c in constraints_list) {
474 var tp = tparams.Find (c.TypeParameter.Value);
476 Report.Error (699, c.Location, "`{0}': A constraint references nonexistent type parameter `{1}'",
477 GetSignatureForError (), c.TypeParameter.Value);
486 /// Returns instance of ObsoleteAttribute for this MemberCore
488 public virtual ObsoleteAttribute GetAttributeObsolete ()
490 if ((caching_flags & (Flags.Obsolete_Undetected | Flags.Obsolete)) == 0)
493 caching_flags &= ~Flags.Obsolete_Undetected;
495 if (OptAttributes == null)
498 Attribute obsolete_attr = OptAttributes.Search (Module.PredefinedAttributes.Obsolete);
499 if (obsolete_attr == null)
502 caching_flags |= Flags.Obsolete;
504 ObsoleteAttribute obsolete = obsolete_attr.GetObsoleteAttribute ();
505 if (obsolete == null)
512 // Checks whether the type P is as accessible as this member
514 public bool IsAccessibleAs (TypeSpec p)
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
521 if ((mod_flags & Modifiers.PRIVATE) != 0)
524 while (TypeManager.HasElementType (p))
525 p = TypeManager.GetElementType (p);
527 if (p.BuiltinType != BuiltinTypeSpec.Type.None)
530 if (p.IsGenericParameter)
533 for (TypeSpec p_parent; p != null; p = p_parent) {
534 p_parent = p.DeclaringType;
537 foreach (TypeSpec t in p.TypeArguments) {
538 if (!IsAccessibleAs (t))
543 var pAccess = p.Modifiers & Modifiers.AccessibilityMask;
544 if (pAccess == Modifiers.PUBLIC)
547 bool same_access_restrictions = false;
548 for (MemberCore mc = this; !same_access_restrictions && mc != null && mc.Parent != null; mc = mc.Parent) {
549 var tc = mc as TypeContainer;
550 if (tc != null && tc.PartialContainer != null)
551 mc = tc.PartialContainer;
553 var al = mc.ModFlags & Modifiers.AccessibilityMask;
555 case Modifiers.INTERNAL:
556 if (al == Modifiers.PRIVATE || al == Modifiers.INTERNAL)
557 same_access_restrictions = p.MemberDefinition.IsInternalAsPublic (mc.Module.DeclaringAssembly);
561 case Modifiers.PROTECTED:
562 if (al == Modifiers.PROTECTED) {
563 same_access_restrictions = mc.Parent.PartialContainer.IsBaseTypeDefinition (p_parent);
567 if (al == Modifiers.PRIVATE) {
569 // When type is private and any of its parents derives from
570 // protected type then the type is accessible
572 while (mc.Parent != null && mc.Parent.PartialContainer != null) {
573 if (mc.Parent.PartialContainer.IsBaseTypeDefinition (p_parent)) {
574 same_access_restrictions = true;
584 case Modifiers.PROTECTED | Modifiers.INTERNAL:
585 if (al == Modifiers.INTERNAL)
586 same_access_restrictions = p.MemberDefinition.IsInternalAsPublic (mc.Module.DeclaringAssembly);
587 else if (al == (Modifiers.PROTECTED | Modifiers.INTERNAL))
588 same_access_restrictions = mc.Parent.PartialContainer.IsBaseTypeDefinition (p_parent) && p.MemberDefinition.IsInternalAsPublic (mc.Module.DeclaringAssembly);
589 else if (al == Modifiers.PROTECTED)
590 goto case Modifiers.PROTECTED;
591 else if (al == Modifiers.PRIVATE) {
592 if (p.MemberDefinition.IsInternalAsPublic (mc.Module.DeclaringAssembly)) {
593 same_access_restrictions = true;
595 goto case Modifiers.PROTECTED;
601 case Modifiers.PRIVATE:
603 // Both are private and share same parent
605 if (al == Modifiers.PRIVATE) {
606 var decl = mc.Parent;
608 same_access_restrictions = decl.CurrentType.MemberDefinition == p_parent.MemberDefinition;
609 } while (!same_access_restrictions && !decl.PartialContainer.IsTopLevel && (decl = decl.Parent) != null);
615 throw new InternalErrorException (al.ToString ());
619 if (!same_access_restrictions)
627 /// Analyze whether CLS-Compliant verification must be execute for this MemberCore.
629 public override bool IsClsComplianceRequired ()
631 if ((caching_flags & Flags.ClsCompliance_Undetected) == 0)
632 return (caching_flags & Flags.ClsCompliant) != 0;
634 caching_flags &= ~Flags.ClsCompliance_Undetected;
636 if (HasClsCompliantAttribute) {
637 if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0)
640 caching_flags |= Flags.ClsCompliant;
644 if (Parent.IsClsComplianceRequired ()) {
645 caching_flags |= Flags.ClsCompliant;
652 public virtual string[] ConditionalConditions ()
658 /// Returns true when MemberCore is exposed from assembly.
660 public bool IsExposedFromAssembly ()
662 if ((ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
663 return this is NamespaceContainer;
665 var parentContainer = Parent.PartialContainer;
666 while (parentContainer != null) {
667 if ((parentContainer.ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
670 parentContainer = parentContainer.Parent.PartialContainer;
677 // Does extension methods look up to find a method which matches name and extensionType.
678 // Search starts from this namespace and continues hierarchically up to top level.
680 public ExtensionMethodCandidates LookupExtensionMethod (string name, int arity)
684 var ns = m as NamespaceContainer;
686 return ns.LookupExtensionMethod (this, name, arity, 0);
694 public virtual FullNamedExpression LookupNamespaceAlias (string name)
696 return Parent.LookupNamespaceAlias (name);
699 public virtual FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
701 return Parent.LookupNamespaceOrType (name, arity, mode, loc);
705 /// Goes through class hierarchy and gets value of first found CLSCompliantAttribute.
706 /// If no is attribute exists then assembly CLSCompliantAttribute is returned.
708 public bool? CLSAttributeValue {
710 if ((caching_flags & Flags.HasCompliantAttribute_Undetected) == 0) {
711 if ((caching_flags & Flags.HasClsCompliantAttribute) == 0)
714 return (caching_flags & Flags.ClsCompliantAttributeFalse) == 0;
717 caching_flags &= ~Flags.HasCompliantAttribute_Undetected;
719 if (OptAttributes != null) {
720 Attribute cls_attribute = OptAttributes.Search (Module.PredefinedAttributes.CLSCompliant);
721 if (cls_attribute != null) {
722 caching_flags |= Flags.HasClsCompliantAttribute;
723 if (cls_attribute.GetClsCompliantAttributeValue ())
726 caching_flags |= Flags.ClsCompliantAttributeFalse;
736 /// Returns true if MemberCore is explicitly marked with CLSCompliantAttribute
738 protected bool HasClsCompliantAttribute {
740 return CLSAttributeValue.HasValue;
745 /// Returns true when a member supports multiple overloads (methods, indexers, etc)
747 public virtual bool EnableOverloadChecks (MemberCore overload)
753 /// The main virtual method for CLS-Compliant verifications.
754 /// The method returns true if member is CLS-Compliant and false if member is not
755 /// CLS-Compliant which means that CLS-Compliant tests are not necessary. A descendants override it
756 /// and add their extra verifications.
758 protected virtual bool VerifyClsCompliance ()
760 if (HasClsCompliantAttribute) {
761 if (!Module.DeclaringAssembly.HasCLSCompliantAttribute) {
762 Attribute a = OptAttributes.Search (Module.PredefinedAttributes.CLSCompliant);
763 if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0) {
764 Report.Warning (3021, 2, a.Location,
765 "`{0}' does not need a CLSCompliant attribute because the assembly is not marked as CLS-compliant",
766 GetSignatureForError ());
768 Report.Warning (3014, 1, a.Location,
769 "`{0}' cannot be marked as CLS-compliant because the assembly is not marked as CLS-compliant",
770 GetSignatureForError ());
775 if (!IsExposedFromAssembly ()) {
776 Attribute a = OptAttributes.Search (Module.PredefinedAttributes.CLSCompliant);
777 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 ());
781 if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0) {
782 if (Parent is Interface && Parent.IsClsComplianceRequired ()) {
783 Report.Warning (3010, 1, Location, "`{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
784 } else if (Parent.Kind == MemberKind.Class && (ModFlags & Modifiers.ABSTRACT) != 0 && Parent.IsClsComplianceRequired ()) {
785 Report.Warning (3011, 1, Location, "`{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
791 if (Parent.Kind != MemberKind.Namespace && Parent.Kind != 0 && !Parent.IsClsComplianceRequired ()) {
792 Attribute a = OptAttributes.Search (Module.PredefinedAttributes.CLSCompliant);
793 Report.Warning (3018, 1, a.Location, "`{0}' cannot be marked as CLS-compliant because it is a member of non CLS-compliant type `{1}'",
794 GetSignatureForError (), Parent.GetSignatureForError ());
798 if (!IsExposedFromAssembly ())
801 if (!Parent.IsClsComplianceRequired ())
805 if (member_name.Name [0] == '_') {
806 Warning_IdentifierNotCompliant ();
809 if (member_name.TypeParameters != null)
810 member_name.TypeParameters.VerifyClsCompliance ();
815 protected void Warning_IdentifierNotCompliant ()
817 Report.Warning (3008, 1, MemberName.Location, "Identifier `{0}' is not CLS-compliant", GetSignatureForError ());
820 public virtual string GetCallerMemberName ()
822 return MemberName.Name;
826 // Returns a string that represents the signature for this
827 // member which should be used in XML documentation.
829 public abstract string GetSignatureForDocumentation ();
831 public virtual void GetCompletionStartingWith (string prefix, List<string> results)
833 Parent.GetCompletionStartingWith (prefix, results);
837 // Generates xml doc comments (if any), and if required,
838 // handle warning report.
840 internal virtual void GenerateDocComment (DocumentationBuilder builder)
842 if (DocComment == null) {
843 if (IsExposedFromAssembly ()) {
844 Constructor c = this as Constructor;
845 if (c == null || !c.IsDefault ())
846 Report.Warning (1591, 4, Location,
847 "Missing XML comment for publicly visible type or member `{0}'", GetSignatureForError ());
854 builder.GenerateDocumentationForMember (this);
855 } catch (Exception e) {
856 throw new InternalErrorException (this, e);
860 public virtual void WriteDebugSymbol (MonoSymbolFile file)
864 #region IMemberContext Members
866 public virtual CompilerContext Compiler {
868 return Module.Compiler;
872 public virtual TypeSpec CurrentType {
873 get { return Parent.CurrentType; }
876 public MemberCore CurrentMemberDefinition {
880 public virtual TypeParameters CurrentTypeParameters {
884 public bool IsObsolete {
886 if (GetAttributeObsolete () != null)
889 return Parent != null && Parent.IsObsolete;
893 public bool IsUnsafe {
895 if ((ModFlags & Modifiers.UNSAFE) != 0)
898 return Parent != null && Parent.IsUnsafe;
902 public bool IsStatic {
904 return (ModFlags & Modifiers.STATIC) != 0;
912 // Base member specification. A member specification contains
913 // member details which can alter in the context (e.g. generic instances)
915 public abstract class MemberSpec
918 public enum StateFlags
920 Obsolete_Undetected = 1, // Obsolete attribute has not been detected yet
921 Obsolete = 1 << 1, // Member has obsolete attribute
922 CLSCompliant_Undetected = 1 << 2, // CLSCompliant attribute has not been detected yet
923 CLSCompliant = 1 << 3, // Member is CLS Compliant
924 MissingDependency_Undetected = 1 << 4,
925 MissingDependency = 1 << 5,
926 HasDynamicElement = 1 << 6,
927 ConstraintsChecked = 1 << 7,
929 IsAccessor = 1 << 9, // Method is an accessor
930 IsGeneric = 1 << 10, // Member contains type arguments
932 PendingMetaInflate = 1 << 12,
933 PendingMakeMethod = 1 << 13,
934 PendingMemberCacheMembers = 1 << 14,
935 PendingBaseTypeInflate = 1 << 15,
936 InterfacesExpanded = 1 << 16,
937 IsNotCSharpCompatible = 1 << 17,
938 SpecialRuntimeType = 1 << 18,
939 InflatedExpressionType = 1 << 19,
940 InflatedNullableType = 1 << 20,
941 GenericIterateInterface = 1 << 21,
942 GenericTask = 1 << 22,
943 InterfacesImported = 1 << 23,
947 // Some flags can be copied directly from other member
949 protected const StateFlags SharedStateFlags =
950 StateFlags.CLSCompliant | StateFlags.CLSCompliant_Undetected |
951 StateFlags.Obsolete | StateFlags.Obsolete_Undetected |
952 StateFlags.MissingDependency | StateFlags.MissingDependency_Undetected |
953 StateFlags.HasDynamicElement;
955 protected Modifiers modifiers;
956 public StateFlags state;
957 protected IMemberDefinition definition;
958 public readonly MemberKind Kind;
959 protected TypeSpec declaringType;
963 public int ID = counter++;
966 protected MemberSpec (MemberKind kind, TypeSpec declaringType, IMemberDefinition definition, Modifiers modifiers)
969 this.declaringType = declaringType;
970 this.definition = definition;
971 this.modifiers = modifiers;
973 if (kind == MemberKind.MissingType)
974 state = StateFlags.MissingDependency;
976 state = StateFlags.Obsolete_Undetected | StateFlags.CLSCompliant_Undetected | StateFlags.MissingDependency_Undetected;
981 public virtual int Arity {
987 public TypeSpec DeclaringType {
989 return declaringType;
992 declaringType = value;
996 public IMemberDefinition MemberDefinition {
1002 public Modifiers Modifiers {
1011 public virtual string Name {
1013 return definition.Name;
1017 public bool IsAbstract {
1018 get { return (modifiers & Modifiers.ABSTRACT) != 0; }
1021 public bool IsAccessor {
1023 return (state & StateFlags.IsAccessor) != 0;
1026 state = value ? state | StateFlags.IsAccessor : state & ~StateFlags.IsAccessor;
1031 // Return true when this member is a generic in C# terms
1032 // A nested non-generic type of generic type will return false
1034 public bool IsGeneric {
1036 return (state & StateFlags.IsGeneric) != 0;
1039 state = value ? state | StateFlags.IsGeneric : state & ~StateFlags.IsGeneric;
1044 // Returns true for imported members which are not compatible with C# language
1046 public bool IsNotCSharpCompatible {
1048 return (state & StateFlags.IsNotCSharpCompatible) != 0;
1051 state = value ? state | StateFlags.IsNotCSharpCompatible : state & ~StateFlags.IsNotCSharpCompatible;
1055 public bool IsPrivate {
1056 get { return (modifiers & Modifiers.PRIVATE) != 0; }
1059 public bool IsPublic {
1060 get { return (modifiers & Modifiers.PUBLIC) != 0; }
1063 public bool IsStatic {
1065 return (modifiers & Modifiers.STATIC) != 0;
1071 public virtual void CheckObsoleteness (IMemberContext mc, Location loc)
1073 var oa = GetAttributeObsolete ();
1078 AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc, mc.Module.Compiler.Report);
1081 public virtual ObsoleteAttribute GetAttributeObsolete ()
1083 if ((state & (StateFlags.Obsolete | StateFlags.Obsolete_Undetected)) == 0)
1086 state &= ~StateFlags.Obsolete_Undetected;
1088 var oa = definition.GetAttributeObsolete ();
1090 state |= StateFlags.Obsolete;
1096 // Returns a list of missing dependencies of this member. The list
1097 // will contain types only but it can have numerous values for members
1098 // like methods where both return type and all parameters are checked
1100 public List<MissingTypeSpecReference> GetMissingDependencies ()
1102 return GetMissingDependencies (this);
1105 public List<MissingTypeSpecReference> GetMissingDependencies (MemberSpec caller)
1107 if ((state & (StateFlags.MissingDependency | StateFlags.MissingDependency_Undetected)) == 0)
1110 state &= ~StateFlags.MissingDependency_Undetected;
1112 var imported = definition as ImportedDefinition;
1113 List<MissingTypeSpecReference> missing;
1114 if (imported != null) {
1115 missing = ResolveMissingDependencies (caller);
1116 } else if (this is ElementTypeSpec) {
1117 missing = ((ElementTypeSpec) this).Element.GetMissingDependencies (caller);
1122 if (missing != null) {
1123 state |= StateFlags.MissingDependency;
1129 public abstract List<MissingTypeSpecReference> ResolveMissingDependencies (MemberSpec caller);
1131 protected virtual bool IsNotCLSCompliant (out bool attrValue)
1133 var cls = MemberDefinition.CLSAttributeValue;
1134 attrValue = cls ?? false;
1135 return cls == false;
1138 public virtual string GetSignatureForDocumentation ()
1140 return DeclaringType.GetSignatureForDocumentation () + "." + Name;
1143 public virtual string GetSignatureForError ()
1145 var bf = MemberDefinition as Property.BackingFieldDeclaration;
1150 name = bf.OriginalProperty.MemberName.Name;
1153 return DeclaringType.GetSignatureForError () + "." + name;
1156 public virtual MemberSpec InflateMember (TypeParameterInflator inflator)
1158 var inflated = (MemberSpec) MemberwiseClone ();
1159 inflated.declaringType = inflator.TypeInstance;
1160 if (DeclaringType.IsGenericOrParentIsGeneric)
1161 inflated.state |= StateFlags.PendingMetaInflate;
1163 inflated.ID += 1000000;
1169 // Is this member accessible from invocation context
1171 public bool IsAccessible (IMemberContext ctx)
1173 var ma = Modifiers & Modifiers.AccessibilityMask;
1174 if (ma == Modifiers.PUBLIC)
1177 var parentType = /* this as TypeSpec ?? */ DeclaringType;
1178 var ctype = ctx.CurrentType;
1180 if (ma == Modifiers.PRIVATE) {
1181 if (ctype == null || parentType == null)
1184 // It's only accessible to the current class or children
1186 if (parentType.MemberDefinition == ctype.MemberDefinition)
1189 return TypeManager.IsNestedChildOf (ctype, parentType.MemberDefinition);
1192 if ((ma & Modifiers.INTERNAL) != 0) {
1194 var assembly = ctype == null ? ctx.Module.DeclaringAssembly : ctype.MemberDefinition.DeclaringAssembly;
1196 if (parentType == null) {
1197 b = ((ITypeDefinition) MemberDefinition).IsInternalAsPublic (assembly);
1199 b = DeclaringType.MemberDefinition.IsInternalAsPublic (assembly);
1202 if (b || ma == Modifiers.INTERNAL)
1207 // Checks whether `ctype' is a subclass or nested child of `parentType'.
1209 while (ctype != null) {
1210 if (TypeManager.IsFamilyAccessible (ctype, parentType))
1213 // Handle nested types.
1214 ctype = ctype.DeclaringType; // TODO: Untested ???
1221 // Returns member CLS compliance based on full member hierarchy
1223 public bool IsCLSCompliant ()
1225 if ((state & StateFlags.CLSCompliant_Undetected) != 0) {
1226 state &= ~StateFlags.CLSCompliant_Undetected;
1229 if (IsNotCLSCompliant (out compliant))
1233 if (DeclaringType != null) {
1234 compliant = DeclaringType.IsCLSCompliant ();
1236 compliant = ((ITypeDefinition) MemberDefinition).DeclaringAssembly.IsCLSCompliant;
1241 state |= StateFlags.CLSCompliant;
1244 return (state & StateFlags.CLSCompliant) != 0;
1247 public bool IsConditionallyExcluded (IMemberContext ctx)
1249 if ((Kind & (MemberKind.Class | MemberKind.Method)) == 0)
1252 var conditions = MemberDefinition.ConditionalConditions ();
1253 if (conditions == null)
1256 var m = ctx.CurrentMemberDefinition;
1257 CompilationSourceFile unit = null;
1258 while (m != null && unit == null) {
1259 unit = m as CompilationSourceFile;
1264 foreach (var condition in conditions) {
1265 if (unit.IsConditionalDefined (condition))
1273 public override string ToString ()
1275 return GetSignatureForError ();
1280 // Member details which are same between all member
1283 public interface IMemberDefinition
1285 bool? CLSAttributeValue { get; }
1286 string Name { get; }
1287 bool IsImported { get; }
1289 string[] ConditionalConditions ();
1290 ObsoleteAttribute GetAttributeObsolete ();
1291 void SetIsAssigned ();
1295 public interface IMethodDefinition : IMemberDefinition
1297 MethodBase Metadata { get; }
1300 public interface IParametersMember : IInterfaceMemberSpec
1302 AParametersCollection Parameters { get; }
1305 public interface IInterfaceMemberSpec
1307 TypeSpec MemberType { get; }