manually synchronized with 56802
[mono.git] / mcs / gmcs / decl.cs
index 41d5c4f4c6e365042e00b12c5a85e57aea39f405..dcd451b96f94b5aaef3ca8b55d97bafd624e29c4 100644 (file)
@@ -73,7 +73,7 @@ namespace Mono.CSharp {
                }
 
                public MemberName (MemberName left, MemberName right)
-                       : this (left, right, left != null ? left.Location : right != null ? right.Location : Location.Null)
+                       : this (left, right, right.Location)
                {
                }
 
@@ -298,7 +298,7 @@ namespace Mono.CSharp {
        ///   Base representation for members.  This is used to keep track
        ///   of Name, Location and Modifier flags, and handling Attributes.
        /// </summary>
-       public abstract class MemberCore : Attributable {
+       public abstract class MemberCore : Attributable, IResolveContext {
                /// <summary>
                ///   Public name
                /// </summary>
@@ -323,7 +323,7 @@ namespace Mono.CSharp {
                /// </summary>
                public int ModFlags;
 
-               public /*readonly*/ TypeContainer Parent;
+               public /*readonly*/ DeclSpace Parent;
 
                /// <summary>
                ///   Location where this declaration happens
@@ -359,13 +359,13 @@ namespace Mono.CSharp {
                        IsUsed = 1 << 11,
                        IsAssigned = 1 << 12                            // Field is assigned
                }
-  
+
                /// <summary>
                ///   MemberCore flags at first detected then cached
-               /// </summary>
+               /// </summary>
                internal Flags caching_flags;
 
-               public MemberCore (TypeContainer parent, MemberName name, Attributes attrs)
+               public MemberCore (DeclSpace parent, MemberName name, Attributes attrs)
                        : base (attrs)
                {
                        if (parent is PartialContainer && !(this is PartialContainer))
@@ -406,23 +406,12 @@ namespace Mono.CSharp {
                        VerifyClsCompliance (Parent);
                }
 
-               public virtual EmitContext EmitContext
-               {
-                       get {
-                               return Parent.EmitContext;
-                       }
-               }
-
-               public bool InUnsafe {
-                       get {
-                               return ((ModFlags & Modifiers.UNSAFE) != 0) || Parent.UnsafeContext;
-                       }
+               public virtual EmitContext EmitContext {
+                       get { return Parent.EmitContext; }
                }
 
                public virtual bool IsUsed {
-                       get {
-                               return (caching_flags & Flags.IsUsed) != 0;
-                       }
+                       get { return (caching_flags & Flags.IsUsed) != 0; }
                }
 
                public void SetMemberIsUsed ()
@@ -430,24 +419,6 @@ namespace Mono.CSharp {
                        caching_flags |= Flags.IsUsed;
                }
 
-               // 
-               // Whehter is it ok to use an unsafe pointer in this type container
-               //
-               public bool UnsafeOK (DeclSpace parent)
-               {
-                       //
-                       // First check if this MemberCore modifier flags has unsafe set
-                       //
-                       if ((ModFlags & Modifiers.UNSAFE) != 0)
-                               return true;
-
-                       if (parent.UnsafeContext)
-                               return true;
-
-                       Expression.UnsafeError (Location);
-                       return false;
-               }
-
                /// <summary>
                /// Returns instance of ObsoleteAttribute for this MemberCore
                /// </summary>
@@ -464,11 +435,11 @@ namespace Mono.CSharp {
                                return null;
 
                        Attribute obsolete_attr = OptAttributes.Search (
-                               TypeManager.obsolete_attribute_type, EmitContext);
+                               TypeManager.obsolete_attribute_type);
                        if (obsolete_attr == null)
                                return null;
 
-                       ObsoleteAttribute obsolete = obsolete_attr.GetObsoleteAttribute (EmitContext);
+                       ObsoleteAttribute obsolete = obsolete_attr.GetObsoleteAttribute ();
                        if (obsolete == null)
                                return null;
 
@@ -507,12 +478,12 @@ namespace Mono.CSharp {
                /// <summary>
                /// Analyze whether CLS-Compliant verification must be execute for this MemberCore.
                /// </summary>
-               public override bool IsClsComplianceRequired (DeclSpace container)
+               public override bool IsClsComplianceRequired ()
                {
                        if ((caching_flags & Flags.ClsCompliance_Undetected) == 0)
                                return (caching_flags & Flags.ClsCompliant) != 0;
 
-                       if (GetClsCompliantAttributeValue (container) && IsExposedFromAssembly (container)) {
+                       if (GetClsCompliantAttributeValue (Parent) && IsExposedFromAssembly (Parent)) {
                                caching_flags &= ~Flags.ClsCompliance_Undetected;
                                caching_flags |= Flags.ClsCompliant;
                                return true;
@@ -546,10 +517,10 @@ namespace Mono.CSharp {
                {
                        if (OptAttributes != null) {
                                Attribute cls_attribute = OptAttributes.Search (
-                                       TypeManager.cls_compliant_attribute_type, ds.EmitContext);
+                                       TypeManager.cls_compliant_attribute_type);
                                if (cls_attribute != null) {
                                        caching_flags |= Flags.HasClsCompliantAttribute;
-                                       return cls_attribute.GetClsCompliantAttributeValue (ds.EmitContext);
+                                       return cls_attribute.GetClsCompliantAttributeValue ();
                                }
                        }
                        return ds.GetClsCompliantAttributeValue ();
@@ -580,12 +551,12 @@ namespace Mono.CSharp {
                /// </summary>
                protected virtual bool VerifyClsCompliance (DeclSpace ds)
                {
-                       if (!IsClsComplianceRequired (ds)) {
+                       if (!IsClsComplianceRequired ()) {
                                if (HasClsCompliantAttribute && RootContext.WarningLevel >= 2) {
                                        if (!IsExposedFromAssembly (ds))
-                                               Report.Warning (3019, Location, "CLS compliance checking will not be performed on `{0}' because it is not visible from outside this assembly", GetSignatureForError ());
+                                               Report.Warning (3019, 2, Location, "CLS compliance checking will not be performed on `{0}' because it is not visible from outside this assembly", GetSignatureForError ());
                                        if (!CodeGen.Assembly.IsClsCompliant)
-                                               Report.Warning (3021, Location, "`{0}' does not need a CLSCompliant attribute because the assembly is not marked as CLS-compliant", GetSignatureForError ());
+                                               Report.Warning (3021, 2, Location, "`{0}' does not need a CLSCompliant attribute because the assembly is not marked as CLS-compliant", GetSignatureForError ());
                                }
                                return false;
                        }
@@ -633,6 +604,40 @@ namespace Mono.CSharp {
                {
                        DocUtil.GenerateDocComment (this, ds);
                }
+
+               public override IResolveContext ResolveContext {
+                       get {
+                               return this;
+                       }
+               }
+
+               #region IResolveContext Members
+
+               public virtual DeclSpace DeclContainer {
+                       get {
+                               return Parent;
+                       }
+               }
+
+               public bool IsInObsoleteScope {
+                       get {
+                               if (GetObsoleteAttribute () != null)
+                                       return true;
+
+                               return Parent == null ? false : Parent.IsInObsoleteScope;
+                       }
+               }
+
+               public bool IsInUnsafeScope {
+                       get {
+                               if ((ModFlags & Modifiers.UNSAFE) != 0)
+                                       return true;
+
+                               return Parent == null ? false : Parent.IsInUnsafeScope;
+                       }
+               }
+
+               #endregion
        }
 
        /// <summary>
@@ -677,9 +682,7 @@ namespace Mono.CSharp {
                protected EmitContext ec;
                
                public override EmitContext EmitContext {
-                       get {
-                               return ec;
-                       }
+                       get { return ec; }
                }
 
                //
@@ -698,7 +701,7 @@ namespace Mono.CSharp {
 
                static string[] attribute_targets = new string [] { "type" };
 
-               public DeclSpace (NamespaceEntry ns, TypeContainer parent, MemberName name,
+               public DeclSpace (NamespaceEntry ns, DeclSpace parent, MemberName name,
                                  Attributes attrs)
                        : base (parent, name, attrs)
                {
@@ -713,6 +716,12 @@ namespace Mono.CSharp {
                                count_type_params += parent.count_type_params;
                }
 
+               public override DeclSpace DeclContainer {
+                       get {
+                               return this;
+                       }
+               }
+
                /// <summary>
                /// Adds the member to defined_names table. It tests for duplications and enclosing name conflicts
                /// </summary>
@@ -722,7 +731,7 @@ namespace Mono.CSharp {
                                if (symbol is TypeParameter)
                                        Report.Error (694, symbol.Location,
                                                      "Type parameter `{0}' has same name as " +
-                                                     "containing type or method", name);
+                                                     "containing type, or method", name);
                                else {
                                        Report.SymbolRelatedToPreviousError (this);
                                        Report.Error (542,  symbol.Location,
@@ -820,7 +829,7 @@ namespace Mono.CSharp {
                ///   Define all members, but don't apply any attributes or do anything which may
                ///   access not-yet-defined classes.  This method also creates the MemberCache.
                /// </summary>
-               public virtual bool DefineMembers (TypeContainer parent)
+               public virtual bool DefineMembers ()
                {
                        if (((ModFlags & Modifiers.NEW) != 0) && IsTopLevel) {
                                Report.Error (1530, Location, "Keyword `new' is not allowed on namespace elements");
@@ -830,24 +839,14 @@ namespace Mono.CSharp {
                }
 
                public override string GetSignatureForError ()
-               {       
+               {
+                       if (IsGeneric) {
+                               return SimpleName.RemoveGenericArity (Name) + TypeParameter.GetSignatureForError (CurrentTypeParameters);
+                       }
                        // Parent.GetSignatureForError
                        return Name;
                }
 
-               //
-               // Whether this is an `unsafe context'
-               //
-               public bool UnsafeContext {
-                       get {
-                               if ((ModFlags & Modifiers.UNSAFE) != 0)
-                                       return true;
-                               if (Parent != null)
-                                       return Parent.UnsafeContext;
-                               return false;
-                       }
-               }
-
                EmitContext type_resolve_ec;
                protected EmitContext TypeResolveEmitContext {
                        get {
@@ -859,7 +858,7 @@ namespace Mono.CSharp {
                                        //
                                        // However, if Parent == RootContext.Tree.Types, its NamespaceEntry will be null.
                                        //
-                                       type_resolve_ec = new EmitContext (Parent, this, Location.Null, null, null, ModFlags, false);
+                                       type_resolve_ec = new EmitContext (this, Parent, this, Location.Null, null, null, ModFlags, false);
                                }
                                return type_resolve_ec;
                        }
@@ -869,17 +868,11 @@ namespace Mono.CSharp {
                //    Resolves the expression `e' for a type, and will recursively define
                //    types.  This should only be used for resolving base types.
                // </summary>
-               protected TypeExpr ResolveBaseTypeExpr (Expression e, bool silent, Location loc)
+               protected TypeExpr ResolveBaseTypeExpr (Expression e)
                {
-                       TypeResolveEmitContext.loc = loc;
-                       TypeResolveEmitContext.ContainerType = TypeBuilder;
-                       TypeResolveEmitContext.ResolvingTypeTree = true;
                        if (this is GenericMethod)
                                TypeResolveEmitContext.ContainerType = Parent.TypeBuilder;
-                       else
-                               TypeResolveEmitContext.ContainerType = TypeBuilder;
-
-                       return e.ResolveAsTypeTerminal (TypeResolveEmitContext);
+                       return e.ResolveAsTypeTerminal (TypeResolveEmitContext, false);
                }
                
                public bool CheckAccessLevel (Type check_type) 
@@ -890,9 +883,7 @@ namespace Mono.CSharp {
                        else
                                tb = TypeBuilder;
 
-                       if (check_type.IsGenericInstance)
-                               check_type = check_type.GetGenericTypeDefinition ();
-
+                       check_type = TypeManager.DropGenericTypeArguments (check_type);
                        if (check_type == tb)
                                return true;
 
@@ -929,7 +920,8 @@ namespace Mono.CSharp {
                                //
                                // This test should probably use the declaringtype.
                                //
-                               return check_type.Assembly == TypeBuilder.Assembly;
+                               return check_type.Assembly == TypeBuilder.Assembly ||
+                                       TypeManager.IsFriendAssembly (check_type.Assembly);
 
                        case TypeAttributes.NestedPublic:
                                return true;
@@ -944,15 +936,18 @@ namespace Mono.CSharp {
                                return FamilyAccessible (tb, check_type);
 
                        case TypeAttributes.NestedFamANDAssem:
-                               return (check_type.Assembly == tb.Assembly) &&
+                               return ((check_type.Assembly == tb.Assembly) || 
+                                               TypeManager.IsFriendAssembly (check_type.Assembly)) && 
                                        FamilyAccessible (tb, check_type);
 
                        case TypeAttributes.NestedFamORAssem:
                                return (check_type.Assembly == tb.Assembly) ||
-                                       FamilyAccessible (tb, check_type);
+                                       FamilyAccessible (tb, check_type) ||
+                                       TypeManager.IsFriendAssembly (check_type.Assembly);
 
                        case TypeAttributes.NestedAssembly:
-                               return check_type.Assembly == tb.Assembly;
+                               return check_type.Assembly == tb.Assembly ||
+                                       TypeManager.IsFriendAssembly (check_type.Assembly);
                        }
 
                        Console.WriteLine ("HERE: " + check_attr);
@@ -1095,8 +1090,7 @@ namespace Mono.CSharp {
                        for (Type current_type = TypeBuilder;
                             current_type != null && current_type != TypeManager.object_type;
                             current_type = current_type.BaseType) {
-                               if (current_type.IsGenericInstance)
-                                       current_type = current_type.GetGenericTypeDefinition ();
+                               current_type = TypeManager.DropGenericTypeArguments (current_type);
                                if (current_type is TypeBuilder) {
                                        DeclSpace decl = this;
                                        if (current_type != TypeBuilder)
@@ -1178,10 +1172,10 @@ namespace Mono.CSharp {
                        caching_flags &= ~Flags.HasCompliantAttribute_Undetected;
 
                        if (OptAttributes != null) {
-                               Attribute cls_attribute = OptAttributes.Search (TypeManager.cls_compliant_attribute_type, ec);
+                               Attribute cls_attribute = OptAttributes.Search (TypeManager.cls_compliant_attribute_type);
                                if (cls_attribute != null) {
                                        caching_flags |= Flags.HasClsCompliantAttribute;
-                                       if (cls_attribute.GetClsCompliantAttributeValue (ec)) {
+                                       if (cls_attribute.GetClsCompliantAttributeValue ()) {
                                                caching_flags |= Flags.ClsCompliantAttributeTrue;
                                                return true;
                                        }
@@ -1231,12 +1225,9 @@ namespace Mono.CSharp {
                                if (param.Name != name)
                                        continue;
 
-                               if (RootContext.WarningLevel >= 3)
-                                       Report.Warning (
-                                               693, Location,
-                                               "Type parameter `{0}' has same name " +
-                                               "as type parameter from outer type `{1}'",
-                                               name, Parent.GetInstantiationName ());
+                               Report.Warning (693, 3, Location,
+                                       "Type parameter `{0}' has same name as type parameter from outer type `{1}'",
+                                       name, Parent.GetInstantiationName ());
 
                                return false;
                        }
@@ -1281,37 +1272,39 @@ namespace Mono.CSharp {
                        if (!is_generic) {
                                if (constraints_list != null) {
                                        Report.Error (
-                                               80, Location, "Contraints are not allowed " +
+                                               80, Location, "Constraints are not allowed " +
                                                "on non-generic declarations");
                                }
 
                                return;
                        }
 
-                       string[] names = MemberName.TypeArguments.GetDeclarations ();
+                       TypeParameterName[] names = MemberName.TypeArguments.GetDeclarations ();
                        type_params = new TypeParameter [names.Length];
 
                        //
                        // Register all the names
                        //
                        for (int i = 0; i < type_params.Length; i++) {
-                               string name = names [i];
+                               TypeParameterName name = names [i];
 
                                Constraints constraints = null;
                                if (constraints_list != null) {
                                        foreach (Constraints constraint in constraints_list) {
                                                if (constraint == null)
                                                        continue;
-                                               if (constraint.TypeParameter == name) {
+                                               if (constraint.TypeParameter == name.Name) {
                                                        constraints = constraint;
                                                        break;
                                                }
                                        }
                                }
 
-                               type_params [i] = new TypeParameter (Parent, name, constraints, Location);
+                               type_params [i] = new TypeParameter (
+                                       Parent, this, name.Name, constraints, name.OptAttributes,
+                                       Location);
 
-                               AddToContainer (type_params [i], name);
+                               AddToContainer (type_params [i], name.Name);
                        }
                }
 
@@ -1326,7 +1319,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               protected TypeParameter[] CurrentTypeParameters {
+               public TypeParameter[] CurrentTypeParameters {
                        get {
                                if (!IsGeneric)
                                        throw new InvalidOperationException ();
@@ -1397,7 +1390,7 @@ namespace Mono.CSharp {
 
                                Report.SymbolRelatedToPreviousError ((DeclSpace)val);
                        }
-                       Report.Warning (3005, Location, "Identifier `{0}' differing only in case is not CLS-compliant", GetSignatureForError ());
+                       Report.Warning (3005, 1, Location, "Identifier `{0}' differing only in case is not CLS-compliant", GetSignatureForError ());
                        return true;
                }
        }
@@ -1650,8 +1643,8 @@ namespace Mono.CSharp {
                        // method cache with all declared and inherited methods.
                        Type type = container.Type;
                        if (!(type is TypeBuilder) && !type.IsInterface &&
-                           // !(type.IsGenericInstance && (type.GetGenericTypeDefinition () is TypeBuilder)) &&
-                           !type.IsGenericInstance &&
+                           // !(type.IsGenericType && (type.GetGenericTypeDefinition () is TypeBuilder)) &&
+                           !type.IsGenericType &&
                            (Container.BaseCache == null || Container.BaseCache.method_hash != null)) {
                                method_hash = new Hashtable ();
                                AddMethods (type);
@@ -1727,8 +1720,8 @@ namespace Mono.CSharp {
                        // We need to call AddMembers() with a single member type at a time
                        // to get the member type part of CacheEntry.EntryType right.
                        if (!container.IsInterface) {
-                       AddMembers (MemberTypes.Constructor, container);
-                       AddMembers (MemberTypes.Field, container);
+                               AddMembers (MemberTypes.Constructor, container);
+                               AddMembers (MemberTypes.Field, container);
                        }
                        AddMembers (MemberTypes.Method, container);
                        AddMembers (MemberTypes.Property, container);
@@ -1927,8 +1920,8 @@ namespace Mono.CSharp {
 
                protected class CacheEntry {
                        public readonly IMemberContainer Container;
-                       public EntryType EntryType;
-                       public MemberInfo Member;
+                       public readonly EntryType EntryType;
+                       public readonly MemberInfo Member;
 
                        public CacheEntry (IMemberContainer container, MemberInfo member,
                                           MemberTypes mt, BindingFlags bf)
@@ -1993,7 +1986,7 @@ namespace Mono.CSharp {
                static MemberInfo [] emptyMemberInfo = new MemberInfo [0];
                
                public MemberInfo [] FindMembers (MemberTypes mt, BindingFlags bf, string name,
-                                              MemberFilter filter, object criteria)
+                                                 MemberFilter filter, object criteria)
                {
                        if (using_global)
                                throw new Exception ();
@@ -2113,7 +2106,7 @@ namespace Mono.CSharp {
                // Because the MemberCache holds members from this class and all the base classes,
                // we can avoid tons of reflection stuff.
                //
-               public MemberInfo FindMemberToOverride (Type invocationType, string name, Type [] paramTypes, bool is_property)
+               public MemberInfo FindMemberToOverride (Type invocationType, string name, Type [] paramTypes, GenericMethod genericMethod, bool is_property)
                {
                        ArrayList applicable;
                        if (method_hash != null && !is_property)
@@ -2150,7 +2143,7 @@ namespace Mono.CSharp {
                                        }
                                } else {
                                        mi = (MethodInfo) entry.Member;
-                                       cmpAttrs = TypeManager.GetArgumentTypes (mi);
+                                       cmpAttrs = TypeManager.GetParameterData (mi).Types;
                                }
 
                                if (fi != null) {
@@ -2189,7 +2182,20 @@ namespace Mono.CSharp {
                                        if (!TypeManager.IsEqual (paramTypes [j], cmpAttrs [j]))
                                                goto next;
                                }
-                               
+
+                               //
+                               // check generic arguments for methods
+                               //
+                               if (mi != null) {
+                                       Type [] cmpGenArgs = mi.GetGenericArguments ();
+                                       if (genericMethod != null && cmpGenArgs.Length > 0) {
+                                               if (genericMethod.TypeParameters.Length != cmpGenArgs.Length)
+                                                       goto next;
+                                       }
+                                       else if (! (genericMethod == null && cmpGenArgs.Length == 0))
+                                               goto next;
+                               }
+
                                //
                                // get one of the methods because this has the visibility info.
                                //
@@ -2363,7 +2369,7 @@ namespace Mono.CSharp {
                
                                MethodBase method_to_compare = (MethodBase)entry.Member;
                                AttributeTester.Result result = AttributeTester.AreOverloadedMethodParamsClsCompliant (
-                                       method.ParameterTypes, TypeManager.GetArgumentTypes (method_to_compare));
+                                       method.ParameterTypes, TypeManager.GetParameterData (method_to_compare).Types);
 
                                if (result == AttributeTester.Result.Ok)
                                        continue;
@@ -2372,7 +2378,7 @@ namespace Mono.CSharp {
 
                                // TODO: now we are ignoring CLSCompliance(false) on method from other assembly which is buggy.
                                // However it is exactly what csc does.
-                               if (md != null && !md.IsClsComplianceRequired (method.Parent))
+                               if (md != null && !md.IsClsComplianceRequired ())
                                        continue;
                
                                Report.SymbolRelatedToPreviousError (entry.Member);