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.Diagnostics;
19 using XmlElement = System.Object;
25 using IKVM.Reflection;
26 using IKVM.Reflection.Emit;
28 using System.Reflection;
29 using System.Reflection.Emit;
32 namespace Mono.CSharp {
35 // Better name would be DottenName
37 [DebuggerDisplay ("{GetSignatureForError()}")]
38 public class MemberName {
39 public readonly string Name;
40 public TypeArguments TypeArguments;
42 public readonly MemberName Left;
43 public readonly Location Location;
45 public static readonly MemberName Null = new MemberName ("");
49 private MemberName (MemberName left, string name, bool is_double_colon,
54 this.is_double_colon = is_double_colon;
58 private MemberName (MemberName left, string name, bool is_double_colon,
59 TypeArguments args, Location loc)
60 : this (left, name, is_double_colon, loc)
62 if (args != null && args.Count > 0)
63 this.TypeArguments = args;
66 public MemberName (string name)
67 : this (name, Location.Null)
70 public MemberName (string name, Location loc)
71 : this (null, name, false, loc)
74 public MemberName (string name, TypeArguments args, Location loc)
75 : this (null, name, false, args, loc)
78 public MemberName (MemberName left, string name)
79 : this (left, name, left != null ? left.Location : Location.Null)
82 public MemberName (MemberName left, string name, Location loc)
83 : this (left, name, false, loc)
86 public MemberName (MemberName left, string name, TypeArguments args, Location loc)
87 : this (left, name, false, args, loc)
90 public MemberName (string alias, string name, TypeArguments args, Location loc)
91 : this (new MemberName (alias, loc), name, true, args, loc)
94 public MemberName (MemberName left, MemberName right)
95 : this (left, right, right.Location)
98 public MemberName (MemberName left, MemberName right, Location loc)
99 : this (null, right.Name, false, right.TypeArguments, loc)
101 if (right.is_double_colon)
102 throw new InternalErrorException ("Cannot append double_colon member name");
103 this.Left = (right.Left == null) ? left : new MemberName (left, right.Left);
107 public string GetName ()
109 return GetName (false);
114 return TypeArguments == null ? 0 : TypeArguments.Count;
118 public bool IsGeneric {
120 if (TypeArguments != null)
122 else if (Left != null)
123 return Left.IsGeneric;
129 public string GetName (bool is_generic)
131 string name = is_generic ? Basename : Name;
133 return Left.GetName (is_generic) + (is_double_colon ? "::" : ".") + name;
138 public ATypeNameExpression GetTypeExpression ()
141 if (TypeArguments != null)
142 return new SimpleName (Name, TypeArguments, Location);
144 return new SimpleName (Name, Location);
147 if (is_double_colon) {
148 if (Left.Left != null)
149 throw new InternalErrorException ("The left side of a :: should be an identifier");
150 return new QualifiedAliasMember (Left.Name, Name, TypeArguments, Location);
153 Expression lexpr = Left.GetTypeExpression ();
154 return new MemberAccess (lexpr, Name, TypeArguments, Location);
157 public MemberName Clone ()
159 MemberName left_clone = Left == null ? null : Left.Clone ();
160 return new MemberName (left_clone, Name, is_double_colon, TypeArguments, Location);
163 public string Basename {
165 if (TypeArguments != null)
166 return MakeName (Name, TypeArguments);
171 public string GetSignatureForError ()
173 string append = TypeArguments == null ? "" : "<" + TypeArguments.GetSignatureForError () + ">";
175 return Name + append;
176 string connect = is_double_colon ? "::" : ".";
177 return Left.GetSignatureForError () + connect + Name + append;
180 public override bool Equals (object other)
182 return Equals (other as MemberName);
185 public bool Equals (MemberName other)
189 if (other == null || Name != other.Name)
191 if (is_double_colon != other.is_double_colon)
194 if ((TypeArguments != null) &&
195 (other.TypeArguments == null || TypeArguments.Count != other.TypeArguments.Count))
198 if ((TypeArguments == null) && (other.TypeArguments != null))
202 return other.Left == null;
204 return Left.Equals (other.Left);
207 public override int GetHashCode ()
209 int hash = Name.GetHashCode ();
210 for (MemberName n = Left; n != null; n = n.Left)
211 hash ^= n.Name.GetHashCode ();
215 if (TypeArguments != null)
216 hash ^= TypeArguments.Count << 5;
218 return hash & 0x7FFFFFFF;
221 public int CountTypeArguments {
223 if (TypeArguments != null)
224 return TypeArguments.Count;
225 else if (Left != null)
226 return Left.CountTypeArguments;
232 public static string MakeName (string name, TypeArguments args)
237 return name + "`" + args.Count;
240 public static string MakeName (string name, int count)
242 return name + "`" + count;
246 public class SimpleMemberName
249 public Location Location;
251 public SimpleMemberName (string name, Location loc)
259 /// Base representation for members. This is used to keep track
260 /// of Name, Location and Modifier flags, and handling Attributes.
262 [System.Diagnostics.DebuggerDisplay ("{GetSignatureForError()}")]
263 public abstract class MemberCore : Attributable, IMemberContext, IMemberDefinition
269 protected string cached_name;
270 // TODO: Remove in favor of MemberName
273 if (cached_name == null)
274 cached_name = MemberName.GetName (!(this is GenericMethod) && !(this is Method));
279 string IMemberDefinition.Name {
281 return member_name.Name;
285 // Is not readonly because of IndexerName attribute
286 private MemberName member_name;
287 public MemberName MemberName {
288 get { return member_name; }
292 /// Modifier flags that the user specified in the source code
294 private Modifiers mod_flags;
295 public Modifiers ModFlags {
298 if ((value & Modifiers.COMPILER_GENERATED) != 0)
299 caching_flags = Flags.IsUsed | Flags.IsAssigned;
306 public virtual ModuleContainer Module {
308 return Parent.Module;
312 public /*readonly*/ TypeContainer Parent;
315 /// Location where this declaration happens
317 public Location Location {
318 get { return member_name.Location; }
322 /// XML documentation comment
324 protected string comment;
327 /// Represents header string for documentation comment
328 /// for each member types.
330 public abstract string DocCommentHeader { get; }
334 Obsolete_Undetected = 1, // Obsolete attribute has not been detected yet
335 Obsolete = 1 << 1, // Type has obsolete attribute
336 ClsCompliance_Undetected = 1 << 2, // CLS Compliance has not been detected yet
337 ClsCompliant = 1 << 3, // Type is CLS Compliant
338 CloseTypeCreated = 1 << 4, // Tracks whether we have Closed the type
339 HasCompliantAttribute_Undetected = 1 << 5, // Presence of CLSCompliantAttribute has not been detected
340 HasClsCompliantAttribute = 1 << 6, // Type has CLSCompliantAttribute
341 ClsCompliantAttributeFalse = 1 << 7, // Member has CLSCompliant(false)
342 Excluded_Undetected = 1 << 8, // Conditional attribute has not been detected yet
343 Excluded = 1 << 9, // Method is conditional
344 MethodOverloadsExist = 1 << 10, // Test for duplication must be performed
346 IsAssigned = 1 << 12, // Field is assigned
347 HasExplicitLayout = 1 << 13,
348 PartialDefinitionExists = 1 << 14, // Set when corresponding partial method definition exists
349 HasStructLayout = 1 << 15 // Has StructLayoutAttribute
353 /// MemberCore flags at first detected then cached
355 internal Flags caching_flags;
357 public MemberCore (DeclSpace parent, MemberName name, Attributes attrs)
359 this.Parent = parent as TypeContainer;
361 caching_flags = Flags.Obsolete_Undetected | Flags.ClsCompliance_Undetected | Flags.HasCompliantAttribute_Undetected | Flags.Excluded_Undetected;
362 AddAttributes (attrs, this);
365 protected virtual void SetMemberName (MemberName new_name)
367 member_name = new_name;
371 protected bool CheckAbstractAndExtern (bool has_block)
373 if (Parent.PartialContainer.Kind == MemberKind.Interface)
377 if ((ModFlags & Modifiers.EXTERN) != 0) {
378 Report.Error (179, Location, "`{0}' cannot declare a body because it is marked extern",
379 GetSignatureForError ());
383 if ((ModFlags & Modifiers.ABSTRACT) != 0) {
384 Report.Error (500, Location, "`{0}' cannot declare a body because it is marked abstract",
385 GetSignatureForError ());
389 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN | Modifiers.PARTIAL)) == 0 && !(Parent is Delegate)) {
390 if (Compiler.Settings.Version >= LanguageVersion.V_3) {
391 Property.PropertyMethod pm = this as Property.PropertyMethod;
392 if (pm is Indexer.GetIndexerMethod || pm is Indexer.SetIndexerMethod)
395 if (pm != null && pm.Property.AccessorSecond == null) {
396 Report.Error (840, Location,
397 "`{0}' must have a body because it is not marked abstract or extern. The property can be automatically implemented when you define both accessors",
398 GetSignatureForError ());
403 Report.Error (501, Location, "`{0}' must have a body because it is not marked abstract, extern, or partial",
404 GetSignatureForError ());
412 protected void CheckProtectedModifier ()
414 if ((ModFlags & Modifiers.PROTECTED) == 0)
417 if (Parent.PartialContainer.Kind == MemberKind.Struct) {
418 Report.Error (666, Location, "`{0}': Structs cannot contain protected members",
419 GetSignatureForError ());
423 if ((Parent.ModFlags & Modifiers.STATIC) != 0) {
424 Report.Error (1057, Location, "`{0}': Static classes cannot contain protected members",
425 GetSignatureForError ());
429 if ((Parent.ModFlags & Modifiers.SEALED) != 0 && (ModFlags & Modifiers.OVERRIDE) == 0 &&
430 !(this is Destructor)) {
431 Report.Warning (628, 4, Location, "`{0}': new protected member declared in sealed class",
432 GetSignatureForError ());
437 public abstract bool Define ();
439 public virtual string DocComment {
449 // Returns full member name for error message
451 public virtual string GetSignatureForError ()
453 if (Parent == null || Parent.Parent == null)
454 return member_name.GetSignatureForError ();
456 return Parent.GetSignatureForError () + "." + member_name.GetSignatureForError ();
460 /// Base Emit method. This is also entry point for CLS-Compliant verification.
462 public virtual void Emit ()
464 if (!Compiler.Settings.VerifyClsCompliance)
467 VerifyClsCompliance ();
470 public bool IsCompilerGenerated {
472 if ((mod_flags & Modifiers.COMPILER_GENERATED) != 0)
475 return Parent == null ? false : Parent.IsCompilerGenerated;
479 public bool IsImported {
485 public virtual bool IsUsed {
487 return (caching_flags & Flags.IsUsed) != 0;
491 protected Report Report {
493 return Compiler.Report;
497 public void SetIsUsed ()
499 caching_flags |= Flags.IsUsed;
502 public void SetIsAssigned ()
504 caching_flags |= Flags.IsAssigned;
508 /// Returns instance of ObsoleteAttribute for this MemberCore
510 public virtual ObsoleteAttribute GetAttributeObsolete ()
512 if ((caching_flags & (Flags.Obsolete_Undetected | Flags.Obsolete)) == 0)
515 caching_flags &= ~Flags.Obsolete_Undetected;
517 if (OptAttributes == null)
520 Attribute obsolete_attr = OptAttributes.Search (Module.PredefinedAttributes.Obsolete);
521 if (obsolete_attr == null)
524 caching_flags |= Flags.Obsolete;
526 ObsoleteAttribute obsolete = obsolete_attr.GetObsoleteAttribute ();
527 if (obsolete == null)
534 /// Checks for ObsoleteAttribute presence. It's used for testing of all non-types elements
536 public virtual void CheckObsoleteness (Location loc)
538 ObsoleteAttribute oa = GetAttributeObsolete ();
540 AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc, Report);
544 // Checks whether the type P is as accessible as this member
546 public bool IsAccessibleAs (TypeSpec p)
549 // if M is private, its accessibility is the same as this declspace.
550 // we already know that P is accessible to T before this method, so we
553 if ((mod_flags & Modifiers.PRIVATE) != 0)
556 while (TypeManager.HasElementType (p))
557 p = TypeManager.GetElementType (p);
559 if (p.IsGenericParameter)
562 for (TypeSpec p_parent; p != null; p = p_parent) {
563 p_parent = p.DeclaringType;
566 foreach (TypeSpec t in p.TypeArguments) {
567 if (!IsAccessibleAs (t))
572 var pAccess = p.Modifiers & Modifiers.AccessibilityMask;
573 if (pAccess == Modifiers.PUBLIC)
576 bool same_access_restrictions = false;
577 for (MemberCore mc = this; !same_access_restrictions && mc != null && mc.Parent != null; mc = mc.Parent) {
578 var al = mc.ModFlags & Modifiers.AccessibilityMask;
580 case Modifiers.INTERNAL:
581 if (al == Modifiers.PRIVATE || al == Modifiers.INTERNAL)
582 same_access_restrictions = p.MemberDefinition.IsInternalAsPublic (mc.Module.DeclaringAssembly);
586 case Modifiers.PROTECTED:
587 if (al == Modifiers.PROTECTED) {
588 same_access_restrictions = mc.Parent.IsBaseTypeDefinition (p_parent);
592 if (al == Modifiers.PRIVATE) {
594 // When type is private and any of its parents derives from
595 // protected type then the type is accessible
597 while (mc.Parent != null) {
598 if (mc.Parent.IsBaseTypeDefinition (p_parent))
599 same_access_restrictions = true;
606 case Modifiers.PROTECTED | Modifiers.INTERNAL:
607 if (al == Modifiers.INTERNAL)
608 same_access_restrictions = p.MemberDefinition.IsInternalAsPublic (mc.Module.DeclaringAssembly);
609 else if (al == (Modifiers.PROTECTED | Modifiers.INTERNAL))
610 same_access_restrictions = mc.Parent.IsBaseTypeDefinition (p_parent) && p.MemberDefinition.IsInternalAsPublic (mc.Module.DeclaringAssembly);
612 goto case Modifiers.PROTECTED;
616 case Modifiers.PRIVATE:
618 // Both are private and share same parent
620 if (al == Modifiers.PRIVATE) {
621 var decl = mc.Parent;
623 same_access_restrictions = decl.CurrentType == p_parent;
624 } while (!same_access_restrictions && !decl.IsTopLevel && (decl = decl.Parent) != null);
630 throw new InternalErrorException (al.ToString ());
634 if (!same_access_restrictions)
642 /// Analyze whether CLS-Compliant verification must be execute for this MemberCore.
644 public override bool IsClsComplianceRequired ()
646 if ((caching_flags & Flags.ClsCompliance_Undetected) == 0)
647 return (caching_flags & Flags.ClsCompliant) != 0;
649 caching_flags &= ~Flags.ClsCompliance_Undetected;
651 if (HasClsCompliantAttribute) {
652 if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0)
655 caching_flags |= Flags.ClsCompliant;
659 if (Parent.PartialContainer.IsClsComplianceRequired ()) {
660 caching_flags |= Flags.ClsCompliant;
667 public virtual string[] ConditionalConditions ()
673 /// Returns true when MemberCore is exposed from assembly.
675 public bool IsExposedFromAssembly ()
677 if ((ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
680 DeclSpace parentContainer = Parent.PartialContainer;
681 while (parentContainer != null && parentContainer.ModFlags != 0) {
682 if ((parentContainer.ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
684 parentContainer = parentContainer.Parent;
689 public virtual IList<MethodSpec> LookupExtensionMethod (TypeSpec extensionType, string name, int arity, ref NamespaceContainer scope)
691 return Parent.LookupExtensionMethod (extensionType, name, arity, ref scope);
694 public virtual FullNamedExpression LookupNamespaceAlias (string name)
696 return Parent.NamespaceEntry.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.Kind == MemberKind.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.Parent != null && !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.PartialContainer.IsClsComplianceRequired ())
805 if (member_name.Name [0] == '_') {
806 Report.Warning (3008, 1, Location, "Identifier `{0}' is not CLS-compliant", GetSignatureForError () );
813 // Returns a string that represents the signature for this
814 // member which should be used in XML documentation.
816 public abstract string GetSignatureForDocumentation ();
819 // Generates xml doc comments (if any), and if required,
820 // handle warning report.
822 internal virtual void GenerateDocComment (DocumentationBuilder builder)
824 if (DocComment == null) {
825 if (IsExposedFromAssembly ()) {
826 Constructor c = this as Constructor;
827 if (c == null || !c.IsDefault ())
828 Report.Warning (1591, 4, Location,
829 "Missing XML comment for publicly visible type or member `{0}'", GetSignatureForError ());
836 builder.GenerateDocumentationForMember (this);
837 } catch (Exception e) {
838 throw new InternalErrorException (this, e);
842 #region IMemberContext Members
844 public virtual CompilerContext Compiler {
845 get { return Parent.Compiler; }
848 public virtual TypeSpec CurrentType {
849 get { return Parent.CurrentType; }
852 public MemberCore CurrentMemberDefinition {
856 public virtual TypeParameter[] CurrentTypeParameters {
860 public bool IsObsolete {
862 if (GetAttributeObsolete () != null)
865 return Parent == null ? false : Parent.IsObsolete;
869 public bool IsUnsafe {
871 if ((ModFlags & Modifiers.UNSAFE) != 0)
874 return Parent == null ? false : Parent.IsUnsafe;
878 public bool IsStatic {
880 return (ModFlags & Modifiers.STATIC) != 0;
888 // Base member specification. A member specification contains
889 // member details which can alter in the context (e.g. generic instances)
891 public abstract class MemberSpec
894 public enum StateFlags
896 Obsolete_Undetected = 1, // Obsolete attribute has not been detected yet
897 Obsolete = 1 << 1, // Member has obsolete attribute
898 CLSCompliant_Undetected = 1 << 2, // CLSCompliant attribute has not been detected yet
899 CLSCompliant = 1 << 3, // Member is CLS Compliant
900 MissingDependency_Undetected = 1 << 4,
901 MissingDependency = 1 << 5,
902 HasDynamicElement = 1 << 6,
903 ConstraintsChecked = 1 << 7,
905 IsAccessor = 1 << 9, // Method is an accessor
906 IsGeneric = 1 << 10, // Member contains type arguments
908 PendingMetaInflate = 1 << 12,
909 PendingMakeMethod = 1 << 13,
910 PendingMemberCacheMembers = 1 << 14,
911 PendingBaseTypeInflate = 1 << 15,
912 InterfacesExpanded = 1 << 16,
913 IsNotCSharpCompatible = 1 << 17,
914 SpecialRuntimeType = 1 << 18,
915 InflatedExpressionType = 1 << 19,
916 InflatedNullableType = 1 << 20,
917 GenericIterateInterface = 1 << 21,
918 GenericTask = 1 << 22
921 protected Modifiers modifiers;
922 public StateFlags state;
923 protected IMemberDefinition definition;
924 public readonly MemberKind Kind;
925 protected TypeSpec declaringType;
929 public int ID = counter++;
932 protected MemberSpec (MemberKind kind, TypeSpec declaringType, IMemberDefinition definition, Modifiers modifiers)
935 this.declaringType = declaringType;
936 this.definition = definition;
937 this.modifiers = modifiers;
939 state = StateFlags.Obsolete_Undetected | StateFlags.CLSCompliant_Undetected | StateFlags.MissingDependency_Undetected;
944 public virtual int Arity {
950 public TypeSpec DeclaringType {
952 return declaringType;
955 declaringType = value;
959 public IMemberDefinition MemberDefinition {
965 public Modifiers Modifiers {
974 public virtual string Name {
976 return definition.Name;
980 public bool IsAbstract {
981 get { return (modifiers & Modifiers.ABSTRACT) != 0; }
984 public bool IsAccessor {
986 return (state & StateFlags.IsAccessor) != 0;
989 state = value ? state | StateFlags.IsAccessor : state & ~StateFlags.IsAccessor;
994 // Return true when this member is a generic in C# terms
995 // A nested non-generic type of generic type will return false
997 public bool IsGeneric {
999 return (state & StateFlags.IsGeneric) != 0;
1002 state = value ? state | StateFlags.IsGeneric : state & ~StateFlags.IsGeneric;
1007 // Returns true for imported members which are not compatible with C# language
1009 public bool IsNotCSharpCompatible {
1011 return (state & StateFlags.IsNotCSharpCompatible) != 0;
1014 state = value ? state | StateFlags.IsNotCSharpCompatible : state & ~StateFlags.IsNotCSharpCompatible;
1018 public bool IsPrivate {
1019 get { return (modifiers & Modifiers.PRIVATE) != 0; }
1022 public bool IsPublic {
1023 get { return (modifiers & Modifiers.PUBLIC) != 0; }
1026 public bool IsStatic {
1028 return (modifiers & Modifiers.STATIC) != 0;
1034 public virtual ObsoleteAttribute GetAttributeObsolete ()
1036 if ((state & (StateFlags.Obsolete | StateFlags.Obsolete_Undetected)) == 0)
1039 state &= ~StateFlags.Obsolete_Undetected;
1041 var oa = definition.GetAttributeObsolete ();
1043 state |= StateFlags.Obsolete;
1049 // Returns a list of missing dependencies of this member. The list
1050 // will contain types only but it can have numerous values for members
1051 // like methods where both return type and all parameters are checked
1053 public List<TypeSpec> GetMissingDependencies ()
1055 if ((state & (StateFlags.MissingDependency | StateFlags.MissingDependency_Undetected)) == 0)
1058 state &= ~StateFlags.MissingDependency_Undetected;
1060 var imported = definition as ImportedDefinition;
1061 List<TypeSpec> missing;
1062 if (imported != null) {
1063 missing = ResolveMissingDependencies ();
1064 } else if (this is ElementTypeSpec) {
1065 missing = ((ElementTypeSpec) this).Element.GetMissingDependencies ();
1070 if (missing != null) {
1071 state |= StateFlags.MissingDependency;
1077 public abstract List<TypeSpec> ResolveMissingDependencies ();
1079 protected virtual bool IsNotCLSCompliant (out bool attrValue)
1081 var cls = MemberDefinition.CLSAttributeValue;
1082 attrValue = cls ?? false;
1083 return cls == false;
1086 public virtual string GetSignatureForDocumentation ()
1088 return DeclaringType.GetSignatureForDocumentation () + "." + Name;
1091 public virtual string GetSignatureForError ()
1093 var bf = MemberDefinition as Property.BackingField;
1094 var name = bf == null ? Name : bf.OriginalName;
1095 return DeclaringType.GetSignatureForError () + "." + name;
1098 public virtual MemberSpec InflateMember (TypeParameterInflator inflator)
1100 var inflated = (MemberSpec) MemberwiseClone ();
1101 inflated.declaringType = inflator.TypeInstance;
1102 if (DeclaringType.IsGenericOrParentIsGeneric)
1103 inflated.state |= StateFlags.PendingMetaInflate;
1105 inflated.ID += 1000000;
1111 // Is this member accessible from invocation context
1113 public bool IsAccessible (IMemberContext ctx)
1115 var ma = Modifiers & Modifiers.AccessibilityMask;
1116 if (ma == Modifiers.PUBLIC)
1119 var parentType = /* this as TypeSpec ?? */ DeclaringType;
1120 var ctype = ctx.CurrentType;
1122 if (ma == Modifiers.PRIVATE) {
1126 // It's only accessible to the current class or children
1128 if (parentType.MemberDefinition == ctype.MemberDefinition)
1131 return TypeManager.IsNestedChildOf (ctype, parentType.MemberDefinition);
1134 if ((ma & Modifiers.INTERNAL) != 0) {
1136 var assembly = ctype == null ? ctx.Module.DeclaringAssembly : ctype.MemberDefinition.DeclaringAssembly;
1138 if (parentType == null) {
1139 b = ((ITypeDefinition) MemberDefinition).IsInternalAsPublic (assembly);
1141 b = DeclaringType.MemberDefinition.IsInternalAsPublic (assembly);
1144 if (b || ma == Modifiers.INTERNAL)
1149 // Checks whether `ctype' is a subclass or nested child of `parentType'.
1151 while (ctype != null) {
1152 if (TypeManager.IsFamilyAccessible (ctype, parentType))
1155 // Handle nested types.
1156 ctype = ctype.DeclaringType; // TODO: Untested ???
1163 // Returns member CLS compliance based on full member hierarchy
1165 public bool IsCLSCompliant ()
1167 if ((state & StateFlags.CLSCompliant_Undetected) != 0) {
1168 state &= ~StateFlags.CLSCompliant_Undetected;
1171 if (IsNotCLSCompliant (out compliant))
1175 if (DeclaringType != null) {
1176 compliant = DeclaringType.IsCLSCompliant ();
1178 compliant = ((ITypeDefinition) MemberDefinition).DeclaringAssembly.IsCLSCompliant;
1183 state |= StateFlags.CLSCompliant;
1186 return (state & StateFlags.CLSCompliant) != 0;
1189 public bool IsConditionallyExcluded (CompilerContext ctx, Location loc)
1191 if ((Kind & (MemberKind.Class | MemberKind.Method)) == 0)
1194 var conditions = MemberDefinition.ConditionalConditions ();
1195 if (conditions == null)
1198 foreach (var condition in conditions) {
1199 if (loc.CompilationUnit.IsConditionalDefined (ctx, condition))
1206 public override string ToString ()
1208 return GetSignatureForError ();
1213 // Member details which are same between all member
1216 public interface IMemberDefinition
1218 bool? CLSAttributeValue { get; }
1219 string Name { get; }
1220 bool IsImported { get; }
1222 string[] ConditionalConditions ();
1223 ObsoleteAttribute GetAttributeObsolete ();
1224 void SetIsAssigned ();
1228 public interface IParametersMember : IInterfaceMemberSpec
1230 AParametersCollection Parameters { get; }
1233 public interface IInterfaceMemberSpec
1235 TypeSpec MemberType { get; }
1239 // Base type container declaration. It exists to handle partial types
1240 // which share same definition (PartialContainer) but have different
1243 public abstract class DeclSpace : MemberCore {
1245 /// This points to the actual definition that is being
1246 /// created with System.Reflection.Emit
1248 public TypeBuilder TypeBuilder;
1251 // This is the namespace in which this typecontainer
1252 // was declared. We use this to resolve names.
1254 public NamespaceContainer NamespaceEntry;
1256 public readonly string Basename;
1258 protected Dictionary<string, MemberCore> defined_names;
1260 public TypeContainer PartialContainer;
1262 protected readonly bool is_generic;
1263 readonly int count_type_params;
1264 protected TypeParameter[] type_params;
1265 TypeParameter[] type_param_list;
1268 // Whether we are Generic
1270 public bool IsGeneric {
1274 else if (Parent != null)
1275 return Parent.IsGeneric;
1281 static readonly string[] attribute_targets = new string [] { "type" };
1283 public DeclSpace (NamespaceContainer ns, DeclSpace parent, MemberName name,
1285 : base (parent, name, attrs)
1287 NamespaceEntry = ns;
1288 Basename = name.Basename;
1289 defined_names = new Dictionary<string, MemberCore> ();
1290 PartialContainer = null;
1291 if (name.TypeArguments != null) {
1293 count_type_params = name.TypeArguments.Count;
1296 count_type_params += parent.count_type_params;
1300 /// Adds the member to defined_names table. It tests for duplications and enclosing name conflicts
1302 protected virtual bool AddToContainer (MemberCore symbol, string name)
1305 if (!defined_names.TryGetValue (name, out mc)) {
1306 defined_names.Add (name, symbol);
1310 if (((mc.ModFlags | symbol.ModFlags) & Modifiers.COMPILER_GENERATED) != 0)
1313 if (symbol.EnableOverloadChecks (mc))
1316 InterfaceMemberBase im = mc as InterfaceMemberBase;
1317 if (im != null && im.IsExplicitImpl)
1320 Report.SymbolRelatedToPreviousError (mc);
1321 if ((mc.ModFlags & Modifiers.PARTIAL) != 0 && (symbol is ClassOrStruct || symbol is Interface)) {
1322 Error_MissingPartialModifier (symbol);
1326 if (symbol is TypeParameter) {
1327 Report.Error (692, symbol.Location,
1328 "Duplicate type parameter `{0}'", symbol.GetSignatureForError ());
1330 Report.Error (102, symbol.Location,
1331 "The type `{0}' already contains a definition for `{1}'",
1332 GetSignatureForError (), symbol.MemberName.Name);
1338 protected void RemoveFromContainer (string name)
1340 defined_names.Remove (name);
1344 /// Returns the MemberCore associated with a given name in the declaration
1345 /// space. It doesn't return method based symbols !!
1348 public MemberCore GetDefinition (string name)
1350 MemberCore mc = null;
1351 defined_names.TryGetValue (name, out mc);
1356 // root_types contains all the types. All TopLevel types
1357 // hence have a parent that points to `root_types', that is
1358 // why there is a non-obvious test down here.
1360 public bool IsTopLevel {
1361 get { return (Parent != null && Parent.Parent == null); }
1364 public virtual bool IsUnmanagedType ()
1369 protected abstract TypeAttributes TypeAttr { get; }
1372 /// Should be overriten by the appropriate declaration space
1374 public abstract void DefineType ();
1376 protected void Error_MissingPartialModifier (MemberCore type)
1378 Report.Error (260, type.Location,
1379 "Missing partial modifier on declaration of type `{0}'. Another partial declaration of this type exists",
1380 type.GetSignatureForError ());
1383 public override string GetSignatureForDocumentation ()
1388 public override string GetSignatureForError ()
1390 return MemberName.GetSignatureForError ();
1393 TypeParameter[] initialize_type_params ()
1395 if (type_param_list != null)
1396 return type_param_list;
1398 DeclSpace the_parent = Parent;
1399 if (this is GenericMethod)
1402 var list = new List<TypeParameter> ();
1403 if (the_parent != null && the_parent.IsGeneric) {
1404 // FIXME: move generics info out of DeclSpace
1405 TypeParameter[] parent_params = the_parent.TypeParameters;
1406 list.AddRange (parent_params);
1409 int count = type_params != null ? type_params.Length : 0;
1410 for (int i = 0; i < count; i++) {
1411 TypeParameter param = type_params [i];
1413 if (Parent.CurrentTypeParameters != null) {
1414 foreach (TypeParameter tp in Parent.CurrentTypeParameters) {
1415 if (tp.Name != param.Name)
1418 Report.SymbolRelatedToPreviousError (tp.Location, null);
1419 Report.Warning (693, 3, param.Location,
1420 "Type parameter `{0}' has the same name as the type parameter from outer type `{1}'",
1421 param.Name, Parent.GetSignatureForError ());
1426 type_param_list = new TypeParameter [list.Count];
1427 list.CopyTo (type_param_list, 0);
1428 return type_param_list;
1431 public virtual void SetParameterInfo (List<Constraints> constraints_list)
1434 if (constraints_list != null) {
1436 80, Location, "Constraints are not allowed " +
1437 "on non-generic declarations");
1443 TypeParameterName[] names = MemberName.TypeArguments.GetDeclarations ();
1444 type_params = new TypeParameter [names.Length];
1447 // Register all the names
1449 for (int i = 0; i < type_params.Length; i++) {
1450 TypeParameterName name = names [i];
1452 Constraints constraints = null;
1453 if (constraints_list != null) {
1454 int total = constraints_list.Count;
1455 for (int ii = 0; ii < total; ++ii) {
1456 Constraints constraints_at = (Constraints)constraints_list[ii];
1457 // TODO: it is used by iterators only
1458 if (constraints_at == null) {
1459 constraints_list.RemoveAt (ii);
1463 if (constraints_at.TypeParameter.Value == name.Name) {
1464 constraints = constraints_at;
1465 constraints_list.RemoveAt(ii);
1471 Variance variance = name.Variance;
1472 if (name.Variance != Variance.None && !(this is Delegate || this is Interface)) {
1473 Report.Error (1960, name.Location, "Variant type parameters can only be used with interfaces and delegates");
1474 variance = Variance.None;
1477 type_params [i] = new TypeParameter (
1478 Parent, i, new MemberName (name.Name, Location), constraints, name.OptAttributes, variance);
1480 AddToContainer (type_params [i], name.Name);
1483 if (constraints_list != null && constraints_list.Count > 0) {
1484 foreach (Constraints constraint in constraints_list) {
1485 Report.Error(699, constraint.Location, "`{0}': A constraint references nonexistent type parameter `{1}'",
1486 GetSignatureForError (), constraint.TypeParameter.Value);
1491 protected TypeParameter[] TypeParameters {
1494 throw new InvalidOperationException ();
1495 if ((PartialContainer != null) && (PartialContainer != this))
1496 return PartialContainer.TypeParameters;
1497 if (type_param_list == null)
1498 initialize_type_params ();
1500 return type_param_list;
1504 public int CountTypeParameters {
1506 return count_type_params;
1510 public override string[] ValidAttributeTargets {
1511 get { return attribute_targets; }
1514 protected override bool VerifyClsCompliance ()
1516 if (!base.VerifyClsCompliance ()) {
1520 if (type_params != null) {
1521 foreach (TypeParameter tp in type_params) {
1522 tp.VerifyClsCompliance ();