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
15 using System.Collections.Generic;
16 using System.Reflection.Emit;
17 using System.Reflection;
20 using XmlElement = System.Object;
25 namespace Mono.CSharp {
28 // Better name would be DottenName
30 public class MemberName {
31 public readonly string Name;
32 public TypeArguments TypeArguments;
34 public readonly MemberName Left;
35 public readonly Location Location;
37 public static readonly MemberName Null = new MemberName ("");
41 private MemberName (MemberName left, string name, bool is_double_colon,
46 this.is_double_colon = is_double_colon;
50 private MemberName (MemberName left, string name, bool is_double_colon,
51 TypeArguments args, Location loc)
52 : this (left, name, is_double_colon, loc)
54 if (args != null && args.Count > 0)
55 this.TypeArguments = args;
58 public MemberName (string name)
59 : this (name, Location.Null)
62 public MemberName (string name, Location loc)
63 : this (null, name, false, loc)
66 public MemberName (string name, TypeArguments args, Location loc)
67 : this (null, name, false, args, loc)
70 public MemberName (MemberName left, string name)
71 : this (left, name, left != null ? left.Location : Location.Null)
74 public MemberName (MemberName left, string name, Location loc)
75 : this (left, name, false, loc)
78 public MemberName (MemberName left, string name, TypeArguments args, Location loc)
79 : this (left, name, false, args, loc)
82 public MemberName (string alias, string name, TypeArguments args, Location loc)
83 : this (new MemberName (alias, loc), name, true, args, loc)
86 public MemberName (MemberName left, MemberName right)
87 : this (left, right, right.Location)
90 public MemberName (MemberName left, MemberName right, Location loc)
91 : this (null, right.Name, false, right.TypeArguments, loc)
93 if (right.is_double_colon)
94 throw new InternalErrorException ("Cannot append double_colon member name");
95 this.Left = (right.Left == null) ? left : new MemberName (left, right.Left);
99 public string GetName ()
101 return GetName (false);
106 return TypeArguments == null ? 0 : TypeArguments.Count;
110 public bool IsGeneric {
112 if (TypeArguments != null)
114 else if (Left != null)
115 return Left.IsGeneric;
121 public string GetName (bool is_generic)
123 string name = is_generic ? Basename : Name;
125 return Left.GetName (is_generic) + (is_double_colon ? "::" : ".") + name;
130 public ATypeNameExpression GetTypeExpression ()
133 if (TypeArguments != null)
134 return new SimpleName (Name, TypeArguments, Location);
136 return new SimpleName (Name, Location);
139 if (is_double_colon) {
140 if (Left.Left != null)
141 throw new InternalErrorException ("The left side of a :: should be an identifier");
142 return new QualifiedAliasMember (Left.Name, Name, TypeArguments, Location);
145 Expression lexpr = Left.GetTypeExpression ();
146 return new MemberAccess (lexpr, Name, TypeArguments, Location);
149 public MemberName Clone ()
151 MemberName left_clone = Left == null ? null : Left.Clone ();
152 return new MemberName (left_clone, Name, is_double_colon, TypeArguments, Location);
155 public string Basename {
157 if (TypeArguments != null)
158 return MakeName (Name, TypeArguments);
163 public string GetSignatureForError ()
165 string append = TypeArguments == null ? "" : "<" + TypeArguments.GetSignatureForError () + ">";
167 return Name + append;
168 string connect = is_double_colon ? "::" : ".";
169 return Left.GetSignatureForError () + connect + Name + append;
172 public override bool Equals (object other)
174 return Equals (other as MemberName);
177 public bool Equals (MemberName other)
181 if (other == null || Name != other.Name)
183 if (is_double_colon != other.is_double_colon)
186 if ((TypeArguments != null) &&
187 (other.TypeArguments == null || TypeArguments.Count != other.TypeArguments.Count))
190 if ((TypeArguments == null) && (other.TypeArguments != null))
194 return other.Left == null;
196 return Left.Equals (other.Left);
199 public override int GetHashCode ()
201 int hash = Name.GetHashCode ();
202 for (MemberName n = Left; n != null; n = n.Left)
203 hash ^= n.Name.GetHashCode ();
207 if (TypeArguments != null)
208 hash ^= TypeArguments.Count << 5;
210 return hash & 0x7FFFFFFF;
213 public int CountTypeArguments {
215 if (TypeArguments != null)
216 return TypeArguments.Count;
217 else if (Left != null)
218 return Left.CountTypeArguments;
224 public static string MakeName (string name, TypeArguments args)
229 return name + "`" + args.Count;
232 public static string MakeName (string name, int count)
234 return name + "`" + count;
238 public class SimpleMemberName
241 public Location Location;
243 public SimpleMemberName (string name, Location loc)
251 /// Base representation for members. This is used to keep track
252 /// of Name, Location and Modifier flags, and handling Attributes.
254 [System.Diagnostics.DebuggerDisplay ("{GetSignatureForError()}")]
255 public abstract class MemberCore : Attributable, IMemberContext, IMemberDefinition
261 protected string cached_name;
262 // TODO: Remove in favor of MemberName
265 if (cached_name == null)
266 cached_name = MemberName.GetName (!(this is GenericMethod) && !(this is Method));
271 string IMemberDefinition.Name {
273 return member_name.Name;
277 // Is not readonly because of IndexerName attribute
278 private MemberName member_name;
279 public MemberName MemberName {
280 get { return member_name; }
284 /// Modifier flags that the user specified in the source code
286 private Modifiers mod_flags;
287 public Modifiers ModFlags {
290 if ((value & Modifiers.COMPILER_GENERATED) != 0)
291 caching_flags = Flags.IsUsed | Flags.IsAssigned;
298 public virtual ModuleContainer Module {
300 return Parent.Module;
304 public /*readonly*/ TypeContainer Parent;
307 /// Location where this declaration happens
309 public Location Location {
310 get { return member_name.Location; }
314 /// XML documentation comment
316 protected string comment;
319 /// Represents header string for documentation comment
320 /// for each member types.
322 public abstract string DocCommentHeader { get; }
326 Obsolete_Undetected = 1, // Obsolete attribute has not been detected yet
327 Obsolete = 1 << 1, // Type has obsolete attribute
328 ClsCompliance_Undetected = 1 << 2, // CLS Compliance has not been detected yet
329 ClsCompliant = 1 << 3, // Type is CLS Compliant
330 CloseTypeCreated = 1 << 4, // Tracks whether we have Closed the type
331 HasCompliantAttribute_Undetected = 1 << 5, // Presence of CLSCompliantAttribute has not been detected
332 HasClsCompliantAttribute = 1 << 6, // Type has CLSCompliantAttribute
333 ClsCompliantAttributeFalse = 1 << 7, // Member has CLSCompliant(false)
334 Excluded_Undetected = 1 << 8, // Conditional attribute has not been detected yet
335 Excluded = 1 << 9, // Method is conditional
336 MethodOverloadsExist = 1 << 10, // Test for duplication must be performed
338 IsAssigned = 1 << 12, // Field is assigned
339 HasExplicitLayout = 1 << 13,
340 PartialDefinitionExists = 1 << 14, // Set when corresponding partial method definition exists
341 HasStructLayout = 1 << 15 // Has StructLayoutAttribute
345 /// MemberCore flags at first detected then cached
347 internal Flags caching_flags;
349 public MemberCore (DeclSpace parent, MemberName name, Attributes attrs)
351 this.Parent = parent as TypeContainer;
353 caching_flags = Flags.Obsolete_Undetected | Flags.ClsCompliance_Undetected | Flags.HasCompliantAttribute_Undetected | Flags.Excluded_Undetected;
354 AddAttributes (attrs, this);
357 protected virtual void SetMemberName (MemberName new_name)
359 member_name = new_name;
363 protected bool CheckAbstractAndExtern (bool has_block)
365 if (Parent.PartialContainer.Kind == MemberKind.Interface)
369 if ((ModFlags & Modifiers.EXTERN) != 0) {
370 Report.Error (179, Location, "`{0}' cannot declare a body because it is marked extern",
371 GetSignatureForError ());
375 if ((ModFlags & Modifiers.ABSTRACT) != 0) {
376 Report.Error (500, Location, "`{0}' cannot declare a body because it is marked abstract",
377 GetSignatureForError ());
381 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN | Modifiers.PARTIAL)) == 0 && !(Parent is Delegate)) {
382 if (RootContext.Version >= LanguageVersion.V_3) {
383 Property.PropertyMethod pm = this as Property.PropertyMethod;
384 if (pm is Indexer.GetIndexerMethod || pm is Indexer.SetIndexerMethod)
387 if (pm != null && pm.Property.AccessorSecond == null) {
388 Report.Error (840, Location,
389 "`{0}' must have a body because it is not marked abstract or extern. The property can be automatically implemented when you define both accessors",
390 GetSignatureForError ());
395 Report.Error (501, Location, "`{0}' must have a body because it is not marked abstract, extern, or partial",
396 GetSignatureForError ());
404 protected void CheckProtectedModifier ()
406 if ((ModFlags & Modifiers.PROTECTED) == 0)
409 if (Parent.PartialContainer.Kind == MemberKind.Struct) {
410 Report.Error (666, Location, "`{0}': Structs cannot contain protected members",
411 GetSignatureForError ());
415 if ((Parent.ModFlags & Modifiers.STATIC) != 0) {
416 Report.Error (1057, Location, "`{0}': Static classes cannot contain protected members",
417 GetSignatureForError ());
421 if ((Parent.ModFlags & Modifiers.SEALED) != 0 && (ModFlags & Modifiers.OVERRIDE) == 0 &&
422 !(this is Destructor)) {
423 Report.Warning (628, 4, Location, "`{0}': new protected member declared in sealed class",
424 GetSignatureForError ());
429 public abstract bool Define ();
431 public virtual string DocComment {
441 // Returns full member name for error message
443 public virtual string GetSignatureForError ()
445 if (Parent == null || Parent.Parent == null)
446 return member_name.GetSignatureForError ();
448 return Parent.GetSignatureForError () + "." + member_name.GetSignatureForError ();
452 /// Base Emit method. This is also entry point for CLS-Compliant verification.
454 public virtual void Emit ()
456 if (!RootContext.VerifyClsCompliance)
459 VerifyClsCompliance ();
462 public bool IsCompilerGenerated {
464 if ((mod_flags & Modifiers.COMPILER_GENERATED) != 0)
467 return Parent == null ? false : Parent.IsCompilerGenerated;
471 public bool IsImported {
477 public virtual bool IsUsed {
479 return (caching_flags & Flags.IsUsed) != 0;
483 protected Report Report {
485 return Compiler.Report;
489 public void SetIsUsed ()
491 caching_flags |= Flags.IsUsed;
494 public void SetIsAssigned ()
496 caching_flags |= Flags.IsAssigned;
500 /// Returns instance of ObsoleteAttribute for this MemberCore
502 public virtual ObsoleteAttribute GetAttributeObsolete ()
504 if ((caching_flags & (Flags.Obsolete_Undetected | Flags.Obsolete)) == 0)
507 caching_flags &= ~Flags.Obsolete_Undetected;
509 if (OptAttributes == null)
512 Attribute obsolete_attr = OptAttributes.Search (Compiler.PredefinedAttributes.Obsolete);
513 if (obsolete_attr == null)
516 caching_flags |= Flags.Obsolete;
518 ObsoleteAttribute obsolete = obsolete_attr.GetObsoleteAttribute ();
519 if (obsolete == null)
526 /// Checks for ObsoleteAttribute presence. It's used for testing of all non-types elements
528 public virtual void CheckObsoleteness (Location loc)
530 ObsoleteAttribute oa = GetAttributeObsolete ();
532 AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc, Report);
536 // Checks whether the type P is as accessible as this member
538 public bool IsAccessibleAs (TypeSpec p)
541 // if M is private, its accessibility is the same as this declspace.
542 // we already know that P is accessible to T before this method, so we
545 if ((mod_flags & Modifiers.PRIVATE) != 0)
548 while (TypeManager.HasElementType (p))
549 p = TypeManager.GetElementType (p);
551 if (p.IsGenericParameter)
554 for (TypeSpec p_parent; p != null; p = p_parent) {
555 p_parent = p.DeclaringType;
558 foreach (TypeSpec t in p.TypeArguments) {
559 if (!IsAccessibleAs (t))
564 var pAccess = p.Modifiers & Modifiers.AccessibilityMask;
565 if (pAccess == Modifiers.PUBLIC)
568 bool same_access_restrictions = false;
569 for (MemberCore mc = this; !same_access_restrictions && mc != null && mc.Parent != null; mc = mc.Parent) {
570 var al = mc.ModFlags & Modifiers.AccessibilityMask;
572 case Modifiers.INTERNAL:
573 if (al == Modifiers.PRIVATE || al == Modifiers.INTERNAL)
574 same_access_restrictions = p.MemberDefinition.IsInternalAsPublic (mc.Module.DeclaringAssembly);
578 case Modifiers.PROTECTED:
579 if (al == Modifiers.PROTECTED) {
580 same_access_restrictions = mc.Parent.IsBaseTypeDefinition (p_parent);
584 if (al == Modifiers.PRIVATE) {
586 // When type is private and any of its parents derives from
587 // protected type then the type is accessible
589 while (mc.Parent != null) {
590 if (mc.Parent.IsBaseTypeDefinition (p_parent))
591 same_access_restrictions = true;
598 case Modifiers.PROTECTED | Modifiers.INTERNAL:
599 if (al == Modifiers.INTERNAL)
600 same_access_restrictions = p.MemberDefinition.IsInternalAsPublic (mc.Module.DeclaringAssembly);
601 else if (al == (Modifiers.PROTECTED | Modifiers.INTERNAL))
602 same_access_restrictions = mc.Parent.IsBaseTypeDefinition (p_parent) && p.MemberDefinition.IsInternalAsPublic (mc.Module.DeclaringAssembly);
604 goto case Modifiers.PROTECTED;
608 case Modifiers.PRIVATE:
610 // Both are private and share same parent
612 if (al == Modifiers.PRIVATE) {
613 var decl = mc.Parent;
615 same_access_restrictions = decl.CurrentType == p_parent;
616 } while (!same_access_restrictions && !decl.IsTopLevel && (decl = decl.Parent) != null);
622 throw new InternalErrorException (al.ToString ());
626 if (!same_access_restrictions)
634 /// Analyze whether CLS-Compliant verification must be execute for this MemberCore.
636 public override bool IsClsComplianceRequired ()
638 if ((caching_flags & Flags.ClsCompliance_Undetected) == 0)
639 return (caching_flags & Flags.ClsCompliant) != 0;
641 caching_flags &= ~Flags.ClsCompliance_Undetected;
643 if (HasClsCompliantAttribute) {
644 if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0)
647 caching_flags |= Flags.ClsCompliant;
651 if (Parent.PartialContainer.IsClsComplianceRequired ()) {
652 caching_flags |= Flags.ClsCompliant;
659 public virtual string[] ConditionalConditions ()
665 /// Returns true when MemberCore is exposed from assembly.
667 public bool IsExposedFromAssembly ()
669 if ((ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
672 DeclSpace parentContainer = Parent.PartialContainer;
673 while (parentContainer != null && parentContainer.ModFlags != 0) {
674 if ((parentContainer.ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
676 parentContainer = parentContainer.Parent;
681 public virtual IList<MethodSpec> LookupExtensionMethod (TypeSpec extensionType, string name, int arity, ref NamespaceEntry scope)
683 return Parent.LookupExtensionMethod (extensionType, name, arity, ref scope);
686 public virtual FullNamedExpression LookupNamespaceAlias (string name)
688 return Parent.NamespaceEntry.LookupNamespaceAlias (name);
691 public virtual FullNamedExpression LookupNamespaceOrType (string name, int arity, Location loc, bool ignore_cs0104)
693 return Parent.LookupNamespaceOrType (name, arity, loc, ignore_cs0104);
697 /// Goes through class hierarchy and gets value of first found CLSCompliantAttribute.
698 /// If no is attribute exists then assembly CLSCompliantAttribute is returned.
700 public bool IsNotCLSCompliant ()
702 if ((caching_flags & Flags.HasCompliantAttribute_Undetected) == 0)
703 return (caching_flags & Flags.ClsCompliantAttributeFalse) != 0;
705 caching_flags &= ~Flags.HasCompliantAttribute_Undetected;
707 if (OptAttributes != null) {
708 Attribute cls_attribute = OptAttributes.Search (Compiler.PredefinedAttributes.CLSCompliant);
709 if (cls_attribute != null) {
710 caching_flags |= Flags.HasClsCompliantAttribute;
711 if (cls_attribute.GetClsCompliantAttributeValue ())
714 caching_flags |= Flags.ClsCompliantAttributeFalse;
723 /// Returns true if MemberCore is explicitly marked with CLSCompliantAttribute
725 protected bool HasClsCompliantAttribute {
727 if ((caching_flags & Flags.HasCompliantAttribute_Undetected) != 0)
728 IsNotCLSCompliant ();
730 return (caching_flags & Flags.HasClsCompliantAttribute) != 0;
735 /// Returns true when a member supports multiple overloads (methods, indexers, etc)
737 public virtual bool EnableOverloadChecks (MemberCore overload)
743 /// The main virtual method for CLS-Compliant verifications.
744 /// The method returns true if member is CLS-Compliant and false if member is not
745 /// CLS-Compliant which means that CLS-Compliant tests are not necessary. A descendants override it
746 /// and add their extra verifications.
748 protected virtual bool VerifyClsCompliance ()
750 if (HasClsCompliantAttribute) {
751 if (!Module.DeclaringAssembly.HasCLSCompliantAttribute) {
752 Attribute a = OptAttributes.Search (Compiler.PredefinedAttributes.CLSCompliant);
753 if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0) {
754 Report.Warning (3021, 2, a.Location,
755 "`{0}' does not need a CLSCompliant attribute because the assembly is not marked as CLS-compliant",
756 GetSignatureForError ());
758 Report.Warning (3014, 1, a.Location,
759 "`{0}' cannot be marked as CLS-compliant because the assembly is not marked as CLS-compliant",
760 GetSignatureForError ());
765 if (!IsExposedFromAssembly ()) {
766 Attribute a = OptAttributes.Search (Compiler.PredefinedAttributes.CLSCompliant);
767 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 if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0) {
772 if (Parent.Kind == MemberKind.Interface && Parent.IsClsComplianceRequired ()) {
773 Report.Warning (3010, 1, Location, "`{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
774 } else if (Parent.Kind == MemberKind.Class && (ModFlags & Modifiers.ABSTRACT) != 0 && Parent.IsClsComplianceRequired ()) {
775 Report.Warning (3011, 1, Location, "`{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
781 if (Parent.Parent != null && !Parent.IsClsComplianceRequired ()) {
782 Attribute a = OptAttributes.Search (Compiler.PredefinedAttributes.CLSCompliant);
783 Report.Warning (3018, 1, a.Location, "`{0}' cannot be marked as CLS-compliant because it is a member of non CLS-compliant type `{1}'",
784 GetSignatureForError (), Parent.GetSignatureForError ());
788 if (!IsExposedFromAssembly ())
791 if (!Parent.PartialContainer.IsClsComplianceRequired ())
795 if (member_name.Name [0] == '_') {
796 Report.Warning (3008, 1, Location, "Identifier `{0}' is not CLS-compliant", GetSignatureForError () );
803 // Raised (and passed an XmlElement that contains the comment)
804 // when GenerateDocComment is writing documentation expectedly.
806 internal virtual void OnGenerateDocComment (XmlElement intermediateNode)
811 // Returns a string that represents the signature for this
812 // member which should be used in XML documentation.
814 public virtual string GetDocCommentName (DeclSpace ds)
816 if (ds == null || this is DeclSpace)
817 return DocCommentHeader + Name;
819 return String.Concat (DocCommentHeader, ds.Name, ".", Name);
823 // Generates xml doc comments (if any), and if required,
824 // handle warning report.
826 internal virtual void GenerateDocComment (DeclSpace ds)
829 DocUtil.GenerateDocComment (this, ds, Report);
830 } catch (Exception e) {
831 throw new InternalErrorException (this, e);
835 #region IMemberContext Members
837 public virtual CompilerContext Compiler {
838 get { return Parent.Compiler; }
841 public virtual TypeSpec CurrentType {
842 get { return Parent.CurrentType; }
845 public MemberCore CurrentMemberDefinition {
849 public virtual TypeParameter[] CurrentTypeParameters {
853 public virtual bool HasUnresolvedConstraints {
854 get { return false; }
857 public bool IsObsolete {
859 if (GetAttributeObsolete () != null)
862 return Parent == null ? false : Parent.IsObsolete;
866 public bool IsUnsafe {
868 if ((ModFlags & Modifiers.UNSAFE) != 0)
871 return Parent == null ? false : Parent.IsUnsafe;
875 public bool IsStatic {
877 return (ModFlags & Modifiers.STATIC) != 0;
885 // Base member specification. A member specification contains
886 // member details which can alter in the context (e.g. generic instances)
888 public abstract class MemberSpec
891 public enum StateFlags
893 Obsolete_Undetected = 1, // Obsolete attribute has not been detected yet
894 Obsolete = 1 << 1, // Member has obsolete attribute
895 CLSCompliant_Undetected = 1 << 3, // CLSCompliant attribute has not been detected yet
896 CLSCompliant = 1 << 4, // Member is CLS Compliant
897 HasDynamicElement = 1 << 5,
899 IsAccessor = 1 << 9, // Method is an accessor
900 IsGeneric = 1 << 10, // Member contains type arguments
902 PendingMetaInflate = 1 << 12,
903 PendingMakeMethod = 1 << 13,
904 PendingMemberCacheMembers = 1 << 14,
905 PendingBaseTypeInflate = 1 << 15,
906 InterfacesExpanded = 1 << 16,
907 IsNotRealProperty = 1 << 17,
910 protected Modifiers modifiers;
911 public StateFlags state;
912 protected IMemberDefinition definition;
913 public readonly MemberKind Kind;
914 protected TypeSpec declaringType;
918 public int ID = counter++;
921 protected MemberSpec (MemberKind kind, TypeSpec declaringType, IMemberDefinition definition, Modifiers modifiers)
924 this.declaringType = declaringType;
925 this.definition = definition;
926 this.modifiers = modifiers;
928 state = StateFlags.Obsolete_Undetected | StateFlags.CLSCompliant_Undetected;
933 public virtual int Arity {
939 public TypeSpec DeclaringType {
941 return declaringType;
944 declaringType = value;
948 public IMemberDefinition MemberDefinition {
954 public Modifiers Modifiers {
963 public virtual string Name {
965 return definition.Name;
969 public bool IsAbstract {
970 get { return (modifiers & Modifiers.ABSTRACT) != 0; }
973 public bool IsAccessor {
975 return (state & StateFlags.IsAccessor) != 0;
978 state = value ? state | StateFlags.IsAccessor : state & ~StateFlags.IsAccessor;
983 // Return true when this member is a generic in C# terms
984 // A nested non-generic type of generic type will return false
986 public bool IsGeneric {
988 return (state & StateFlags.IsGeneric) != 0;
991 state = value ? state | StateFlags.IsGeneric : state & ~StateFlags.IsGeneric;
995 public bool IsPrivate {
996 get { return (modifiers & Modifiers.PRIVATE) != 0; }
999 public bool IsPublic {
1000 get { return (modifiers & Modifiers.PUBLIC) != 0; }
1003 public bool IsStatic {
1005 return (modifiers & Modifiers.STATIC) != 0;
1011 public virtual ObsoleteAttribute GetAttributeObsolete ()
1013 if ((state & (StateFlags.Obsolete | StateFlags.Obsolete_Undetected)) == 0)
1016 state &= ~StateFlags.Obsolete_Undetected;
1018 var oa = definition.GetAttributeObsolete ();
1020 state |= StateFlags.Obsolete;
1025 protected virtual bool IsNotCLSCompliant ()
1027 return MemberDefinition.IsNotCLSCompliant ();
1030 public virtual string GetSignatureForError ()
1032 var bf = MemberDefinition as Property.BackingField;
1033 var name = bf == null ? Name : bf.OriginalName;
1034 return DeclaringType.GetSignatureForError () + "." + name;
1037 public virtual MemberSpec InflateMember (TypeParameterInflator inflator)
1039 var inflated = (MemberSpec) MemberwiseClone ();
1040 inflated.declaringType = inflator.TypeInstance;
1041 inflated.state |= StateFlags.PendingMetaInflate;
1043 if (inflated.ID > 0)
1044 inflated.ID = -inflated.ID;
1050 // Is this member accessible from invocationType
1052 public bool IsAccessible (TypeSpec invocationType)
1054 var ma = Modifiers & Modifiers.AccessibilityMask;
1055 if (ma == Modifiers.PUBLIC)
1058 var parentType = /* this as TypeSpec ?? */ DeclaringType;
1060 // It's null for module context
1061 if (invocationType == null)
1062 invocationType = InternalType.FakeInternalType;
1065 // If only accessible to the current class or children
1067 if (ma == Modifiers.PRIVATE)
1068 return invocationType.MemberDefinition == parentType.MemberDefinition ||
1069 TypeManager.IsNestedChildOf (invocationType, parentType);
1071 if ((ma & Modifiers.INTERNAL) != 0) {
1073 var assembly = invocationType == InternalType.FakeInternalType ?
1074 RootContext.ToplevelTypes.DeclaringAssembly :
1075 invocationType.MemberDefinition.DeclaringAssembly;
1077 if (parentType == null) {
1078 b = ((ITypeDefinition) MemberDefinition).IsInternalAsPublic (assembly);
1080 b = DeclaringType.MemberDefinition.IsInternalAsPublic (assembly);
1083 if (b || ma == Modifiers.INTERNAL)
1088 if (!TypeManager.IsNestedFamilyAccessible (invocationType, parentType))
1095 // Returns member CLS compliance based on full member hierarchy
1097 public bool IsCLSCompliant ()
1099 if ((state & StateFlags.CLSCompliant_Undetected) != 0) {
1100 state &= ~StateFlags.CLSCompliant_Undetected;
1102 if (IsNotCLSCompliant ())
1106 if (DeclaringType != null) {
1107 compliant = DeclaringType.IsCLSCompliant ();
1109 compliant = ((ITypeDefinition) MemberDefinition).DeclaringAssembly.IsCLSCompliant;
1113 state |= StateFlags.CLSCompliant;
1116 return (state & StateFlags.CLSCompliant) != 0;
1119 public bool IsConditionallyExcluded (Location loc)
1121 if ((Kind & (MemberKind.Class | MemberKind.Method)) == 0)
1124 var conditions = MemberDefinition.ConditionalConditions ();
1125 if (conditions == null)
1128 foreach (var condition in conditions) {
1129 if (loc.CompilationUnit.IsConditionalDefined (condition))
1136 public override string ToString ()
1138 return GetSignatureForError ();
1143 // Member details which are same between all member
1146 public interface IMemberDefinition
1148 string Name { get; }
1149 bool IsImported { get; }
1151 string[] ConditionalConditions ();
1152 ObsoleteAttribute GetAttributeObsolete ();
1153 bool IsNotCLSCompliant ();
1154 void SetIsAssigned ();
1158 public interface IParametersMember : IInterfaceMemberSpec
1160 AParametersCollection Parameters { get; }
1163 public interface IInterfaceMemberSpec
1165 TypeSpec MemberType { get; }
1169 // Base type container declaration. It exists to handle partial types
1170 // which share same definition (PartialContainer) but have different
1173 public abstract class DeclSpace : MemberCore {
1175 /// This points to the actual definition that is being
1176 /// created with System.Reflection.Emit
1178 public TypeBuilder TypeBuilder;
1181 // This is the namespace in which this typecontainer
1182 // was declared. We use this to resolve names.
1184 public NamespaceEntry NamespaceEntry;
1186 public readonly string Basename;
1188 protected Dictionary<string, MemberCore> defined_names;
1190 public TypeContainer PartialContainer;
1192 protected readonly bool is_generic;
1193 readonly int count_type_params;
1194 protected TypeParameter[] type_params;
1195 TypeParameter[] type_param_list;
1198 // Whether we are Generic
1200 public bool IsGeneric {
1204 else if (Parent != null)
1205 return Parent.IsGeneric;
1211 static string[] attribute_targets = new string [] { "type" };
1213 public DeclSpace (NamespaceEntry ns, DeclSpace parent, MemberName name,
1215 : base (parent, name, attrs)
1217 NamespaceEntry = ns;
1218 Basename = name.Basename;
1219 defined_names = new Dictionary<string, MemberCore> ();
1220 PartialContainer = null;
1221 if (name.TypeArguments != null) {
1223 count_type_params = name.TypeArguments.Count;
1226 count_type_params += parent.count_type_params;
1230 /// Adds the member to defined_names table. It tests for duplications and enclosing name conflicts
1232 protected virtual bool AddToContainer (MemberCore symbol, string name)
1235 if (!defined_names.TryGetValue (name, out mc)) {
1236 defined_names.Add (name, symbol);
1240 if (((mc.ModFlags | symbol.ModFlags) & Modifiers.COMPILER_GENERATED) != 0)
1243 if (symbol.EnableOverloadChecks (mc))
1246 InterfaceMemberBase im = mc as InterfaceMemberBase;
1247 if (im != null && im.IsExplicitImpl)
1250 Report.SymbolRelatedToPreviousError (mc);
1251 if ((mc.ModFlags & Modifiers.PARTIAL) != 0 && (symbol is ClassOrStruct || symbol is Interface)) {
1252 Error_MissingPartialModifier (symbol);
1256 if (this is ModuleContainer) {
1257 Report.Error (101, symbol.Location,
1258 "The namespace `{0}' already contains a definition for `{1}'",
1259 ((DeclSpace)symbol).NamespaceEntry.GetSignatureForError (), symbol.MemberName.Name);
1260 } else if (symbol is TypeParameter) {
1261 Report.Error (692, symbol.Location,
1262 "Duplicate type parameter `{0}'", symbol.GetSignatureForError ());
1264 Report.Error (102, symbol.Location,
1265 "The type `{0}' already contains a definition for `{1}'",
1266 GetSignatureForError (), symbol.MemberName.Name);
1272 protected void RemoveFromContainer (string name)
1274 defined_names.Remove (name);
1278 /// Returns the MemberCore associated with a given name in the declaration
1279 /// space. It doesn't return method based symbols !!
1282 public MemberCore GetDefinition (string name)
1284 MemberCore mc = null;
1285 defined_names.TryGetValue (name, out mc);
1290 // root_types contains all the types. All TopLevel types
1291 // hence have a parent that points to `root_types', that is
1292 // why there is a non-obvious test down here.
1294 public bool IsTopLevel {
1295 get { return (Parent != null && Parent.Parent == null); }
1298 public virtual bool IsUnmanagedType ()
1303 protected virtual TypeAttributes TypeAttr {
1304 get { return Module.DefaultCharSetType; }
1308 /// Should be overriten by the appropriate declaration space
1310 public abstract void DefineType ();
1312 protected void Error_MissingPartialModifier (MemberCore type)
1314 Report.Error (260, type.Location,
1315 "Missing partial modifier on declaration of type `{0}'. Another partial declaration of this type exists",
1316 type.GetSignatureForError ());
1319 public override string GetSignatureForError ()
1321 return MemberName.GetSignatureForError ();
1324 TypeParameter[] initialize_type_params ()
1326 if (type_param_list != null)
1327 return type_param_list;
1329 DeclSpace the_parent = Parent;
1330 if (this is GenericMethod)
1333 var list = new List<TypeParameter> ();
1334 if (the_parent != null && the_parent.IsGeneric) {
1335 // FIXME: move generics info out of DeclSpace
1336 TypeParameter[] parent_params = the_parent.TypeParameters;
1337 list.AddRange (parent_params);
1340 int count = type_params != null ? type_params.Length : 0;
1341 for (int i = 0; i < count; i++) {
1342 TypeParameter param = type_params [i];
1344 if (Parent.CurrentTypeParameters != null) {
1345 foreach (TypeParameter tp in Parent.CurrentTypeParameters) {
1346 if (tp.Name != param.Name)
1349 Report.SymbolRelatedToPreviousError (tp.Location, null);
1350 Report.Warning (693, 3, param.Location,
1351 "Type parameter `{0}' has the same name as the type parameter from outer type `{1}'",
1352 param.Name, Parent.GetSignatureForError ());
1357 type_param_list = new TypeParameter [list.Count];
1358 list.CopyTo (type_param_list, 0);
1359 return type_param_list;
1362 public virtual void SetParameterInfo (List<Constraints> constraints_list)
1365 if (constraints_list != null) {
1367 80, Location, "Constraints are not allowed " +
1368 "on non-generic declarations");
1374 TypeParameterName[] names = MemberName.TypeArguments.GetDeclarations ();
1375 type_params = new TypeParameter [names.Length];
1378 // Register all the names
1380 for (int i = 0; i < type_params.Length; i++) {
1381 TypeParameterName name = names [i];
1383 Constraints constraints = null;
1384 if (constraints_list != null) {
1385 int total = constraints_list.Count;
1386 for (int ii = 0; ii < total; ++ii) {
1387 Constraints constraints_at = (Constraints)constraints_list[ii];
1388 // TODO: it is used by iterators only
1389 if (constraints_at == null) {
1390 constraints_list.RemoveAt (ii);
1394 if (constraints_at.TypeParameter.Value == name.Name) {
1395 constraints = constraints_at;
1396 constraints_list.RemoveAt(ii);
1402 Variance variance = name.Variance;
1403 if (name.Variance != Variance.None && !(this is Delegate || this is Interface)) {
1404 Report.Error (1960, name.Location, "Variant type parameters can only be used with interfaces and delegates");
1405 variance = Variance.None;
1408 type_params [i] = new TypeParameter (
1409 Parent, i, new MemberName (name.Name, Location), constraints, name.OptAttributes, variance);
1411 AddToContainer (type_params [i], name.Name);
1414 if (constraints_list != null && constraints_list.Count > 0) {
1415 foreach (Constraints constraint in constraints_list) {
1416 Report.Error(699, constraint.Location, "`{0}': A constraint references nonexistent type parameter `{1}'",
1417 GetSignatureForError (), constraint.TypeParameter.Value);
1422 protected TypeParameter[] TypeParameters {
1425 throw new InvalidOperationException ();
1426 if ((PartialContainer != null) && (PartialContainer != this))
1427 return PartialContainer.TypeParameters;
1428 if (type_param_list == null)
1429 initialize_type_params ();
1431 return type_param_list;
1435 public int CountTypeParameters {
1437 return count_type_params;
1441 public override string[] ValidAttributeTargets {
1442 get { return attribute_targets; }
1445 protected override bool VerifyClsCompliance ()
1447 if (!base.VerifyClsCompliance ()) {
1451 if (type_params != null) {
1452 foreach (TypeParameter tp in type_params) {
1453 tp.VerifyClsCompliance ();