* TabControl.cs: Fix typo, emilinates an unneeded expose event.
[mono.git] / mcs / mcs / anonymous.cs
index cddac60fe2d3371139dfb2c6f4ae4d31a6175627..dfadc80316048ef5aa71905306ab23f224369c68 100644 (file)
@@ -52,7 +52,7 @@ namespace Mono.CSharp {
                //
                // The modifiers applied to the method, we aggregate them
                //
-               int method_modifiers = Modifiers.INTERNAL;
+               int method_modifiers = Modifiers.PRIVATE;
                
                //
                // During the resolve stage of the anonymous method body,
@@ -113,7 +113,8 @@ namespace Mono.CSharp {
                        //
                        
                        TypeBuilder current_type = ec.TypeContainer.TypeBuilder;
-                       TypeBuilder type_host = Scope == null ? current_type : Scope.ScopeTypeBuilder;
+                       TypeBuilder type_host = (Scope == null ) // || Scope.ScopeTypeBuilder == null)
+                               ? current_type : Scope.ScopeTypeBuilder;
 
                        if (current_type == null)
                                throw new Exception ("The current_type is null");
@@ -122,8 +123,9 @@ namespace Mono.CSharp {
                                throw new Exception ("Type host is null");
                        
                        if (current_type == type_host && ec.IsStatic){
-                               if (ec.IsStatic)
+                               if (ec.IsStatic){
                                        method_modifiers |= Modifiers.STATIC;
+                               }
                                current_type = null;
                        } 
 
@@ -133,7 +135,6 @@ namespace Mono.CSharp {
                                method_modifiers, false, new MemberName ("<#AnonymousMethod>" + anonymous_method_count++),
                                Parameters, null, loc);
                        method.Block = Block;
-
                        
                        //
                        // Swap the TypeBuilder while we define the method, then restore
@@ -164,17 +165,10 @@ namespace Mono.CSharp {
                        //
 
                        invoke_mb = (MethodInfo) Delegate.GetInvokeMethod (ec, delegate_type, loc);
-                       ParameterData invoke_pd = Invocation.GetParameterData (invoke_mb);
+                       ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
 
-                       //
-                       // If implicit parameters are set, then we must check for out in the parameters
-                       // and flag it accordingly.
-                       //
-                       bool out_invalid_check = false;
-                       
                        if (Parameters == null){
                                int i, j;
-                               out_invalid_check = true;
                                
                                //
                                // We provide a set of inaccessible parameters
@@ -224,6 +218,16 @@ namespace Mono.CSharp {
                        
                        for (int i = 0; i < amp.Count; i++){
                                Parameter.Modifier amp_mod = amp.ParameterModifier (i);
+
+                               if ((amp_mod & (Parameter.Modifier.OUT | Parameter.Modifier.REF)) != 0){
+                                       if (!probe){
+                                               Error_ParameterMismatch (delegate_type);
+                                               Report.Error (1677, loc, "Parameter '{0}' should not be declared with the '{1}' keyword", 
+                                                       i+1, amp.ModifierDesc (i));
+                                       }
+                                       return null;
+                               }
+
                                if (amp_mod != invoke_pd.ParameterModifier (i)){
                                        if (!probe){
                                                Report.Error (1676, loc, 
@@ -243,14 +247,6 @@ namespace Mono.CSharp {
                                        }
                                        return null;
                                }
-                               
-                               if (out_invalid_check && (invoke_pd.ParameterModifier (i) & Parameter.Modifier.OUT) != 0){
-                                       if (!probe){
-                                               Report.Error (1676, loc,"Parameter {0} must include the `out' modifier ", i+1);
-                                               Error_ParameterMismatch (delegate_type);
-                                       }
-                                       return null;
-                               }
                        }
 
                        //
@@ -274,13 +270,14 @@ namespace Mono.CSharp {
                                ec.TypeContainer, ec.DeclSpace, loc, null,
                                invoke_mb.ReturnType,
                                /* REVIEW */ (ec.InIterator ? Modifiers.METHOD_YIELDS : 0) |
-                               (ec.InUnsafe ? Modifiers.UNSAFE : 0),
+                               (ec.InUnsafe ? Modifiers.UNSAFE : 0) |
+                               (ec.IsStatic ? Modifiers.STATIC : 0),
                                /* No constructor */ false);
 
                        aec.CurrentAnonymousMethod = this;
                        ContainerAnonymousMethod = ec.CurrentAnonymousMethod;
                        ContainingBlock = ec.CurrentBlock;
-               
+
                        if (aec.ResolveTopBlock (ec, Block, amp, loc, out unreachable))
                                return new AnonymousDelegate (this, delegate_type, loc).Resolve (ec);
 
@@ -292,10 +289,10 @@ namespace Mono.CSharp {
                        return method.MethodData.MethodBuilder;
                }
                
-               public void EmitMethod (EmitContext ec)
+               public bool EmitMethod (EmitContext ec)
                {
                        if (!CreateMethodHost (ec, invoke_mb.ReturnType))
-                               return;
+                               return false;
 
                        MethodBuilder builder = GetMethodBuilder ();
                        ILGenerator ig = builder.GetILGenerator ();
@@ -307,11 +304,11 @@ namespace Mono.CSharp {
                        // Adjust based on the computed state of the
                        // method from CreateMethodHost
                        
-                       if ((method_modifiers & Modifiers.STATIC) != 0)
-                               aec.IsStatic = true;
+                       aec.MethodIsStatic = (method_modifiers & Modifiers.STATIC) != 0;
                        
                        aec.EmitMeta (Block, amp);
                        aec.EmitResolvedTopBlock (Block, unreachable);
+                       return true;
                }
 
                public static void Error_AddressOfCapturedVar (string name, Location loc)
@@ -337,12 +334,14 @@ namespace Mono.CSharp {
                public override Expression DoResolve (EmitContext ec)
                {
                        eclass = ExprClass.Value;
+
                        return this;
                }
                
                public override void Emit (EmitContext ec)
                {
-                       am.EmitMethod (ec);
+                       if (!am.EmitMethod (ec))
+                               return;
 
                        //
                        // Now emit the delegate creation.
@@ -556,7 +555,7 @@ namespace Mono.CSharp {
 
                public void CloseTypes ()
                {
-                       RootContext.RegisterHelperClass (ScopeTypeBuilder);
+                       RootContext.RegisterCompilerGeneratedType (ScopeTypeBuilder);
                        foreach (ScopeInfo si in children)
                                si.CloseTypes ();
                }
@@ -589,7 +588,6 @@ namespace Mono.CSharp {
                                Hashtable captured_parameters = CaptureContext.captured_parameters;
                                
                                foreach (DictionaryEntry de in captured_parameters){
-                                       string name = (string) de.Key;
                                        CapturedParameter cp = (CapturedParameter) de.Value;
 
                                        ig.Emit (OpCodes.Ldloc, ScopeInstance);
@@ -599,6 +597,9 @@ namespace Mono.CSharp {
                        }
                        
                        if (ParentScope != null){
+                               if (!ParentScope.inited)
+                                       ParentScope.EmitInitScope (ec);
+                               
                                //
                                // Only emit initialization in our capturecontext world
                                //
@@ -651,7 +652,7 @@ namespace Mono.CSharp {
        public class CaptureContext {
                public static int count;
                public int cc_id;
-               Location loc;
+               public Location loc;
                
                //
                // Points to the toplevel block that owns this CaptureContext
@@ -659,6 +660,7 @@ namespace Mono.CSharp {
                ToplevelBlock toplevel_owner;
                Hashtable scopes = new Hashtable ();
                bool have_captured_vars = false;
+               bool referenced_this = false;
                ScopeInfo topmost = null;
 
                //
@@ -903,7 +905,14 @@ namespace Mono.CSharp {
                        else
                                captured_fields [fe] = fe;
                }
-               
+
+               public void CaptureThis ()
+               {
+                       CaptureContext parent = ParentCaptureContext;
+                       if (parent != null)
+                               parent.CaptureThis ();
+                       referenced_this = true;
+               }
 
                public bool HaveCapturedVariables {
                        get {
@@ -942,15 +951,15 @@ namespace Mono.CSharp {
                        return false;
                }
 
-               public void EmitHelperClasses (EmitContext ec)
+               public void EmitAnonymousHelperClasses (EmitContext ec)
                {
                        if (topmost != null){
-                               topmost.NeedThis = HaveCapturedFields;
+                               topmost.NeedThis = HaveCapturedFields || referenced_this;
                                topmost.EmitScopeType (ec);
                        } 
                }
 
-               public void CloseHelperClasses ()
+               public void CloseAnonymousHelperClasses ()
                {
                        if (topmost != null)
                                topmost.CloseTypes ();
@@ -1008,8 +1017,13 @@ namespace Mono.CSharp {
                                cc.EmitParameterInstance (ec, name);
                                return;
                        }
-                       Block invocation_block = ec.CurrentBlock;
+                       
                        CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
+                       if (par_info != null){
+                               // 
+                               // FIXME: implementing this.
+                               //
+                       }
                        ILGenerator ig = ec.ig;
 
                        ScopeInfo si;
@@ -1042,6 +1056,11 @@ namespace Mono.CSharp {
                        }
                        EmitParameterInstance (ec, name);
                        CapturedParameter par_info = (CapturedParameter) captured_parameters [name];
+                       if (par_info != null){
+                               // 
+                               // FIXME: implementing this.
+                               //
+                       }
                        ec.ig.Emit (OpCodes.Ldfld, par_info.FieldBuilder);
                }