Allow imported members cache to be setup in 2 phases as compiled one is to deal with...
[mono.git] / mcs / mcs / method.cs
index 6bf279f613309b23fee1835ea6ba7fc6997d6c0b..16eb2881b47c4a7e7fe6644a7960caa3c1ba11e1 100644 (file)
@@ -307,7 +307,11 @@ namespace Mono.CSharp {
                        }
 
                        if ((state & StateFlags.PendingMakeMethod) != 0) {
-                               metaInfo = ((MethodInfo) metaInfo).MakeGenericMethod (targs.Select (l => l.GetMetaInfo ()).ToArray ());
+                               var sre_targs = new Type[targs.Length];
+                               for (int i = 0; i < sre_targs.Length; ++i)
+                                       sre_targs[i] = targs[i].GetMetaInfo ();
+
+                               metaInfo = ((MethodInfo) metaInfo).MakeGenericMethod (sre_targs);
                                state &= ~StateFlags.PendingMakeMethod;
                        }
 
@@ -357,7 +361,7 @@ namespace Mono.CSharp {
                        ms.returnType = inflator.Inflate (returnType);
                        ms.parameters = parameters.Inflate (inflator);
                        if (IsGeneric)
-                               ms.constraints = TypeParameterSpec.InflateConstraints (inflator, GenericDefinition.TypeParameters);
+                               ms.constraints = TypeParameterSpec.InflateConstraints (inflator, Constraints);
 
                        return ms;
                }
@@ -407,7 +411,7 @@ namespace Mono.CSharp {
                        var ms = (MethodSpec) MemberwiseClone ();
                        if (decl != DeclaringType) {
                                // Gets back MethodInfo in case of metaInfo was inflated
-                               ms.metaInfo = MemberCache.GetMember (DeclaringType.GetDefinition (), this).metaInfo;
+                               ms.metaInfo = MemberCache.GetMember (TypeParameterMutator.GetMemberDeclaringType (DeclaringType), this).metaInfo;
 
                                ms.declaringType = decl;
                                ms.state |= StateFlags.PendingMetaInflate;
@@ -576,17 +580,17 @@ namespace Mono.CSharp {
                public override void Emit ()
                {
                        if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
-                               PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (MethodBuilder);
+                               Compiler.PredefinedAttributes.CompilerGenerated.EmitAttribute (MethodBuilder);
                        if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
-                               PredefinedAttributes.Get.DebuggerHidden.EmitAttribute (MethodBuilder);
+                               Compiler.PredefinedAttributes.DebuggerHidden.EmitAttribute (MethodBuilder);
 
                        if (ReturnType == InternalType.Dynamic) {
                                return_attributes = new ReturnParameter (this, MethodBuilder, Location);
-                               PredefinedAttributes.Get.Dynamic.EmitAttribute (return_attributes.Builder);
+                               Compiler.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder);
                        } else {
                                var trans_flags = TypeManager.HasDynamicTypeUsed (ReturnType);
                                if (trans_flags != null) {
-                                       var pa = PredefinedAttributes.Get.DynamicTransform;
+                                       var pa = Compiler.PredefinedAttributes.DynamicTransform;
                                        if (pa.Constructor != null || pa.ResolveConstructor (Location, ArrayContainer.MakeType (TypeManager.bool_type))) {
                                                return_attributes = new ReturnParameter (this, MethodBuilder, Location);
                                                return_attributes.Builder.SetCustomAttribute (
@@ -670,7 +674,7 @@ namespace Mono.CSharp {
                                if (OptAttributes == null)
                                        return null;
 
-                               Attribute[] attrs = OptAttributes.SearchMulti (PredefinedAttributes.Get.Conditional);
+                               Attribute[] attrs = OptAttributes.SearchMulti (Compiler.PredefinedAttributes.Conditional);
                                if (attrs == null)
                                        return null;
 
@@ -759,33 +763,15 @@ namespace Mono.CSharp {
 
        public class Method : MethodOrOperator, IGenericMethodDefinition
        {
-               /// <summary>
-               ///   Modifiers allowed in a class declaration
-               /// </summary>
-               const Modifiers AllowedModifiers =
-                       Modifiers.NEW |
-                       Modifiers.PUBLIC |
-                       Modifiers.PROTECTED |
-                       Modifiers.INTERNAL |
-                       Modifiers.PRIVATE |
-                       Modifiers.STATIC |
-                       Modifiers.VIRTUAL |
-                       Modifiers.SEALED |
-                       Modifiers.OVERRIDE |
-                       Modifiers.ABSTRACT |
-                       Modifiers.UNSAFE |
-                       Modifiers.EXTERN;
-
-               const Modifiers AllowedInterfaceModifiers = 
-                       Modifiers.NEW | Modifiers.UNSAFE;
-
                Method partialMethodImplementation;
 
                public Method (DeclSpace parent, GenericMethod generic,
                               FullNamedExpression return_type, Modifiers mod,
                               MemberName name, ParametersCompiled parameters, Attributes attrs)
                        : base (parent, generic, return_type, mod,
-                               parent.PartialContainer.Kind == MemberKind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
+                               parent.PartialContainer.Kind == MemberKind.Interface ? AllowedModifiersInterface :
+                               parent.PartialContainer.Kind == MemberKind.Struct ? AllowedModifiersStruct :
+                               AllowedModifiersClass,
                                name, attrs, parameters)
                {
                }
@@ -826,6 +812,7 @@ namespace Mono.CSharp {
 
                public TypeParameterSpec[] TypeParameters {
                        get {
+                               // TODO: Cache this
                                return CurrentTypeParameters.Select (l => l.Type).ToArray ();
                        }
                }
@@ -889,13 +876,13 @@ namespace Mono.CSharp {
                                        return;
                                }
 
-                               if (ReturnType != TypeManager.void_type) {
-                                       Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
+                               if ((ModFlags & Modifiers.OVERRIDE) != 0) {
+                                       Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ());
                                        return;
                                }
 
-                               if ((ModFlags & Modifiers.OVERRIDE) != 0) {
-                                       Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ());
+                               if (ReturnType != TypeManager.void_type) {
+                                       Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
                                        return;
                                }
 
@@ -937,10 +924,21 @@ namespace Mono.CSharp {
                        if (((ModFlags & Modifiers.OVERRIDE) != 0 || IsExplicitImpl)) {
                                if (base_method != null) {
                                        base_tparams = base_method.GenericDefinition.TypeParameters;
+                               
                                        if (base_method.DeclaringType.IsGeneric) {
                                                base_decl_tparams = base_method.DeclaringType.MemberDefinition.TypeParameters;
                                                base_targs = Parent.BaseType.TypeArguments;
                                        }
+
+                                       if (base_method.IsGeneric) {
+                                               if (base_decl_tparams.Length != 0) {
+                                                       base_decl_tparams = base_decl_tparams.Concat (base_tparams).ToArray ();
+                                                       base_targs = base_targs.Concat (tparams.Select<TypeParameter, TypeSpec> (l => l.Type)).ToArray ();
+                                               } else {
+                                                       base_decl_tparams = base_tparams;
+                                                       base_targs = tparams.Select (l => l.Type).ToArray ();
+                                               }
+                                       }
                                } else if (MethodData.implementing != null) {
                                        base_tparams = MethodData.implementing.GenericDefinition.TypeParameters;
                                        if (MethodData.implementing.DeclaringType.IsGeneric) {
@@ -1014,7 +1012,7 @@ namespace Mono.CSharp {
                                DefineTypeParameters ();
                        }
 
-                       if (block != null && block.IsIterator && !(Parent is IteratorStorey)) {
+                       if (block != null && block.IsIterator) {
                                //
                                // Current method is turned into automatically generated
                                // wrapper which creates an instance of iterator
@@ -1032,7 +1030,7 @@ namespace Mono.CSharp {
                                                Report.Error (1109, Location, "`{0}': Extension methods cannot be defined in a nested class",
                                                        GetSignatureForError ());
 
-                                       PredefinedAttribute pa = PredefinedAttributes.Get.Extension;
+                                       PredefinedAttribute pa = Compiler.PredefinedAttributes.Extension;
                                        if (!pa.IsDefined) {
                                                Report.Error (1110, Location,
                                                        "`{0}': Extension methods cannot be declared without a reference to System.Core.dll assembly. Add the assembly reference or remove `this' modifer from the first parameter",
@@ -1085,16 +1083,18 @@ namespace Mono.CSharp {
                public override void Emit ()
                {
                        try {
-                               Report.Debug (64, "METHOD EMIT", this, MethodBuilder, Location, Block, MethodData);
                                if (IsPartialDefinition) {
                                        //
                                        // Use partial method implementation builder for partial method declaration attributes
                                        //
                                        if (partialMethodImplementation != null) {
                                                MethodBuilder = partialMethodImplementation.MethodBuilder;
-                                               return;
                                        }
-                               } else if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.PartialDefinitionExists) == 0) {
+
+                                       return;
+                               }
+                               
+                               if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.PartialDefinitionExists) == 0) {
                                        Report.Error (759, Location, "A partial method `{0}' implementation is missing a partial method declaration",
                                                GetSignatureForError ());
                                }
@@ -1120,7 +1120,7 @@ namespace Mono.CSharp {
                                base.Emit ();
                                
                                if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
-                                       PredefinedAttributes.Get.Extension.EmitAttribute (MethodBuilder);
+                                       Compiler.PredefinedAttributes.Extension.EmitAttribute (MethodBuilder);
                        } catch {
                                Console.WriteLine ("Internal compiler error at {0}: exception caught while emitting {1}",
                                                   Location, MethodBuilder);
@@ -1451,7 +1451,7 @@ namespace Mono.CSharp {
                        }
 
                        if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
-                               PredefinedAttributes.Get.DebuggerHidden.EmitAttribute (ConstructorBuilder);
+                               Compiler.PredefinedAttributes.DebuggerHidden.EmitAttribute (ConstructorBuilder);
 
                        if (OptAttributes != null)
                                OptAttributes.Emit ();
@@ -1476,7 +1476,7 @@ namespace Mono.CSharp {
                                // initializer, it must initialize all of the struct's fields.
                                if ((Parent.PartialContainer.Kind == MemberKind.Struct) &&
                                        ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
-                                       block.AddThisVariable (Parent, Location);
+                                       block.AddThisVariable (bc, Parent, Location);
 
                                if (block != null && (ModFlags & Modifiers.STATIC) == 0){
                                        if (Parent.PartialContainer.Kind == MemberKind.Class && Initializer == null)
@@ -1488,12 +1488,12 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       parameters.ApplyAttributes (ConstructorBuilder);
+                       parameters.ApplyAttributes (this, ConstructorBuilder);
 
                        SourceMethod source = SourceMethod.Create (Parent, ConstructorBuilder, block);
 
                        if (block != null) {
-                               if (block.Resolve (null, bc, parameters, this)) {
+                               if (block.Resolve (null, bc, this)) {
                                        EmitContext ec = new EmitContext (this, ConstructorBuilder.GetILGenerator (), bc.ReturnType);
                                        ec.With (EmitContext.Options.ConstructorScope, true);
 
@@ -1861,14 +1861,16 @@ namespace Mono.CSharp {
                        if (GenericMethod != null)
                                GenericMethod.EmitAttributes ();
 
-                       method.ParameterInfo.ApplyAttributes (MethodBuilder);
+                       var mc = (IMemberContext) method;
+
+                       method.ParameterInfo.ApplyAttributes (mc, MethodBuilder);
 
                        SourceMethod source = SourceMethod.Create (parent, MethodBuilder, method.Block);
 
                        ToplevelBlock block = method.Block;
                        if (block != null) {
-                               BlockContext bc = new BlockContext ((IMemberContext) method, block, method.ReturnType);
-                               if (block.Resolve (null, bc, method.ParameterInfo, method)) {
+                               BlockContext bc = new BlockContext (mc, block, method.ReturnType);
+                               if (block.Resolve (null, bc, method)) {
                                        EmitContext ec = method.CreateEmitContext (MethodBuilder.GetILGenerator ());
                                        if (!ec.HasReturnLabel && bc.HasReturnLabel) {
                                                ec.ReturnLabel = bc.ReturnLabel;
@@ -1933,11 +1935,8 @@ namespace Mono.CSharp {
                                MethodGroupExpr method_expr = MethodGroupExpr.CreatePredefined (base_dtor, base_type, Location);
                                method_expr.InstanceExpression = new BaseThis (base_type, Location);
 
-                               ToplevelBlock new_block = new ToplevelBlock (Compiler, Block.StartLocation);
-                               new_block.EndLocation = Block.EndLocation;
-
-                               Block finaly_block = new ExplicitBlock (new_block, Location, Location);
-                               Block try_block = new Block (new_block, block);
+                               var try_block = new ExplicitBlock (block, block.StartLocation, block.EndLocation);
+                               var finaly_block = new ExplicitBlock (block, Location, Location);
 
                                //
                                // 0-size arguments to avoid CS0250 error
@@ -1945,9 +1944,9 @@ namespace Mono.CSharp {
                                // debugger scope
                                //
                                finaly_block.AddStatement (new StatementExpression (new Invocation (method_expr, new Arguments (0))));
-                               new_block.AddStatement (new TryFinally (try_block, finaly_block, Location));
 
-                               block = new_block;
+                               var tf = new TryFinally (try_block, finaly_block, Location);
+                               block.WrapIntoDestructor (tf, try_block);
                        }
 
                        base.Emit ();
@@ -2097,17 +2096,17 @@ namespace Mono.CSharp {
                        method_data.Emit (parent);
 
                        if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
-                               PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (method_data.MethodBuilder);
+                               Compiler.PredefinedAttributes.CompilerGenerated.EmitAttribute (method_data.MethodBuilder);
                        if (((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0))
-                               PredefinedAttributes.Get.DebuggerHidden.EmitAttribute (method_data.MethodBuilder);
+                               Compiler.PredefinedAttributes.DebuggerHidden.EmitAttribute (method_data.MethodBuilder);
 
                        if (ReturnType == InternalType.Dynamic) {
                                return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
-                               PredefinedAttributes.Get.Dynamic.EmitAttribute (return_attributes.Builder);
+                               Compiler.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder);
                        } else {
                                var trans_flags = TypeManager.HasDynamicTypeUsed (ReturnType);
                                if (trans_flags != null) {
-                                       var pa = PredefinedAttributes.Get.DynamicTransform;
+                                       var pa = Compiler.PredefinedAttributes.DynamicTransform;
                                        if (pa.Constructor != null || pa.ResolveConstructor (Location, ArrayContainer.MakeType (TypeManager.bool_type))) {
                                                return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
                                                return_attributes.Builder.SetCustomAttribute (
@@ -2275,7 +2274,7 @@ namespace Mono.CSharp {
                        if (!base.Define ())
                                return false;
 
-                       if (block != null && block.IsIterator && !(Parent is IteratorStorey)) {
+                       if (block != null && block.IsIterator) {
                                //
                                // Current method is turned into automatically generated
                                // wrapper which creates an instance of iterator
@@ -2313,9 +2312,9 @@ namespace Mono.CSharp {
                                }
 
                                TypeSpec conv_type;
-                               if (TypeManager.IsEqual (declaring_type, return_type) || declaring_type == return_type_unwrap) {
+                               if (declaring_type == return_type || declaring_type == return_type_unwrap) {
                                        conv_type = first_arg_type;
-                               } else if (TypeManager.IsEqual (declaring_type, first_arg_type) || declaring_type == first_arg_type_unwrap) {
+                               } else if (declaring_type == first_arg_type || declaring_type == first_arg_type_unwrap) {
                                        conv_type = return_type;
                                } else {
                                        Report.Error (556, Location,
@@ -2338,13 +2337,13 @@ namespace Mono.CSharp {
                                }
 
                                if (conv_type.IsClass) {
-                                       if (TypeManager.IsSubclassOf (declaring_type, conv_type)) {
+                                       if (TypeSpec.IsBaseClass (declaring_type, conv_type, true)) {
                                                Report.Error (553, Location, "User-defined conversion `{0}' cannot convert to or from a base class",
                                                        GetSignatureForError ());
                                                return false;
                                        }
 
-                                       if (TypeManager.IsSubclassOf (conv_type, declaring_type)) {
+                                       if (TypeSpec.IsBaseClass (conv_type, declaring_type, false)) {
                                                Report.Error (554, Location, "User-defined conversion `{0}' cannot convert to or from a derived class",
                                                        GetSignatureForError ());
                                                return false;
@@ -2359,7 +2358,7 @@ namespace Mono.CSharp {
                                // Checks for Unary operators
 
                                if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
-                                       if (return_type != declaring_type && !TypeManager.IsSubclassOf (return_type, declaring_type)) {
+                                       if (return_type != declaring_type && !TypeSpec.IsBaseClass (return_type, declaring_type, false)) {
                                                Report.Error (448, Location,
                                                        "The return type for ++ or -- operator must be the containing type or derived from the containing type");
                                                return false;
@@ -2371,7 +2370,7 @@ namespace Mono.CSharp {
                                        }
                                }
 
-                               if (!TypeManager.IsEqual (first_arg_type_unwrap, declaring_type)) {
+                               if (first_arg_type_unwrap != declaring_type) {
                                        Report.Error (562, Location,
                                                "The parameter type of a unary operator must be the containing type");
                                        return false;
@@ -2387,14 +2386,14 @@ namespace Mono.CSharp {
                                        }
                                }
 
-                       } else if (!TypeManager.IsEqual (first_arg_type_unwrap, declaring_type)) {
+                       } else if (first_arg_type_unwrap != declaring_type) {
                                // Checks for Binary operators
 
                                var second_arg_type = ParameterTypes[1];
                                if (TypeManager.IsNullableType (second_arg_type))
                                        second_arg_type = TypeManager.GetTypeArguments (second_arg_type)[0];
 
-                               if (!TypeManager.IsEqual (second_arg_type, declaring_type)) {
+                               if (second_arg_type != declaring_type) {
                                        Report.Error (563, Location,
                                                "One of the parameters of a binary operator must be the containing type");
                                        return false;