2009-03-27 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / anonymous.cs
index 73251d087df09dd6cc68e95599ef4e5c10b55b7b..75b4b29457b45cfd23dd342ab4ff51111ffcb31a 100644 (file)
@@ -22,17 +22,16 @@ namespace Mono.CSharp {
        {
                public static string MakeName (string host, string typePrefix, string name, int id)
                {
-                       return "<" + host + ">" + typePrefix + "__" + name + id.ToString ();
+                       return "<" + host + ">" + typePrefix + "__" + name + id.ToString ("X");
                }
                
-               protected CompilerGeneratedClass (DeclSpace parent, MemberName name, int mod, Location loc)
+               protected CompilerGeneratedClass (DeclSpace parent, MemberName name, int mod)
                        : base (parent.NamespaceEntry, parent, name, mod | Modifiers.COMPILER_GENERATED | Modifiers.SEALED, null)
                {
-                       parent.PartialContainer.AddCompilerGeneratedClass (this);
                }
 
-               protected CompilerGeneratedClass (DeclSpace parent, GenericMethod generic, MemberName name, int mod, Location loc)
-                       : this (parent, name, mod, loc)
+               protected CompilerGeneratedClass (DeclSpace parent, GenericMethod generic, MemberName name, int mod)
+                       : this (parent, name, mod)
                {
                        if (generic != null) {
                                ArrayList list = new ArrayList ();
@@ -59,12 +58,13 @@ namespace Mono.CSharp {
        public class AnonymousMethodStorey : CompilerGeneratedClass
        {
                class StoreyFieldPair {
-                       public AnonymousMethodStorey Storey;
-                       public Field Field;
+                       public readonly AnonymousMethodStorey Storey;
+                       public readonly Field Field;
 
-                       public StoreyFieldPair (AnonymousMethodStorey storey)
+                       public StoreyFieldPair (AnonymousMethodStorey storey, Field field)
                        {
                                this.Storey = storey;
+                               this.Field = field;
                        }
 
                        public override int GetHashCode ()
@@ -78,22 +78,59 @@ namespace Mono.CSharp {
                        }
                }
 
-               class HoistedGenericField : Field
+               sealed class HoistedGenericField : Field
                {
                        public HoistedGenericField (DeclSpace parent, FullNamedExpression type, int mod, string name,
                                  Attributes attrs, Location loc)
-                               : base (parent, type, mod, name, attrs, loc)
+                               : base (parent, type, mod, new MemberName (name, loc), attrs)
+                       {
+                       }
+
+                       protected override bool ResolveMemberType ()
+                       {
+                               if (!base.ResolveMemberType ())
+                                       return false;
+
+                               AnonymousMethodStorey parent = ((AnonymousMethodStorey) Parent).GetGenericStorey ();
+                               if (parent != null)
+                                       member_type = parent.MutateType (member_type);
+
+                               return true;
+                       }
+               }
+
+               //
+               // Needed to delay hoisted _this_ initialization. When an anonymous
+               // method is used inside ctor and _this_ is hoisted, base ctor has to
+               // be called first, otherwise _this_ will be initialized with 
+               // uninitialized value.
+               //
+               sealed class ThisInitializer : Statement
+               {
+                       readonly HoistedThis hoisted_this;
+
+                       public ThisInitializer (HoistedThis hoisted_this)
+                       {
+                               this.hoisted_this = hoisted_this;
+                       }
+
+                       protected override void DoEmit (EmitContext ec)
+                       {
+                               hoisted_this.EmitHoistingAssignment (ec);
+                       }
+
+                       protected override void CloneTo (CloneContext clonectx, Statement target)
                        {
+                               // Nothing to clone
                        }
 
-                       public override bool Define ()
+                       public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                        {
-                               type_name.Type = ((AnonymousMethodStorey) Parent).MutateType (type_name.Type);
-                               return base.Define ();
+                               // Nothing to mutate
                        }
                }
 
-               // TODO: Why is it required by debugger ?
+               // Unique storey ID
                public readonly int ID;
                static int unique_id;
 
@@ -101,20 +138,20 @@ namespace Mono.CSharp {
 
                // A list of StoreyFieldPair with local field keeping parent storey instance
                ArrayList used_parent_storeys;
+               ArrayList children_references;
 
                // A list of hoisted parameters
                protected ArrayList hoisted_params;
+               protected ArrayList hoisted_locals;
 
                // Hoisted this
-               HoistedThis hoisted_this;
+               protected HoistedThis hoisted_this;
 
                // Local variable which holds this storey instance
                public LocalTemporary Instance;
 
-               bool references_defined;
-
                public AnonymousMethodStorey (Block block, DeclSpace parent, MemberBase host, GenericMethod generic, string name)
-                       : base (parent, generic, MakeMemberName (host, name, generic, block.StartLocation), Modifiers.PRIVATE, block.StartLocation)
+                       : base (parent, generic, MakeMemberName (host, name, generic, block.StartLocation), Modifiers.PRIVATE)
                {
                        Parent = parent;
                        OriginalSourceBlock = block;
@@ -127,14 +164,22 @@ namespace Mono.CSharp {
                        string tname = MakeName (host_name, "c", name, unique_id);
                        TypeArguments args = null;
                        if (generic != null) {
-                               args = new TypeArguments (loc);
+                               args = new TypeArguments ();
                                foreach (TypeParameter tparam in generic.CurrentTypeParameters)
-                                       args.Add (new SimpleName (tparam.Name, loc));
+                                       args.Add (new TypeParameterName (tparam.Name, null, loc));
                        }
 
                        return new MemberName (tname, args, loc);
                }
 
+               public void AddCapturedThisField (EmitContext ec)
+               {
+                       TypeExpr type_expr = new TypeExpression (ec.ContainerType, Location);
+                       Field f = AddCompilerGeneratedField ("<>f__this", type_expr);
+                       f.Define ();
+                       hoisted_this = new HoistedThis (this, f);
+               }
+
                public Field AddCapturedVariable (string name, Type type)
                {
                        CheckMembersDefined ();
@@ -152,111 +197,117 @@ namespace Mono.CSharp {
                protected Field AddCompilerGeneratedField (string name, FullNamedExpression type)
                {
                        const int mod = Modifiers.INTERNAL | Modifiers.COMPILER_GENERATED;
-                       Field f = new Field (this, type, mod, name, null, Location);
+                       Field f = new Field (this, type, mod, new MemberName (name, Location), null);
                        AddField (f);
                        return f;
                }
 
-               public void AddParentStoreyReference (AnonymousMethodStorey s)
+               //
+               // Creates a link between block and the anonymous method storey
+               //
+               // An anonymous method can reference variables from any outer block, but they are
+               // hoisted in their own ExplicitBlock. When more than one block is referenced we
+               // need to create another link between those variable storeys
+               //
+               public void AddReferenceFromChildrenBlock (ExplicitBlock block)
+               {
+                       if (children_references == null)
+                               children_references = new ArrayList ();
+
+                       if (!children_references.Contains (block))
+                               children_references.Add (block);
+               }
+
+               public void AddParentStoreyReference (AnonymousMethodStorey storey)
                {
                        CheckMembersDefined ();
 
                        if (used_parent_storeys == null)
                                used_parent_storeys = new ArrayList ();
-                       else if (used_parent_storeys.IndexOf (s) != -1)
+                       else if (used_parent_storeys.IndexOf (storey) != -1)
                                return;
 
-                       used_parent_storeys.Add (new StoreyFieldPair (s));
+                       TypeExpr type_expr = new TypeExpression (storey.TypeBuilder, Location);
+                       Field f = AddCompilerGeneratedField ("<>f__ref$" + storey.ID, type_expr);
+                       used_parent_storeys.Add (new StoreyFieldPair (storey, f));
                }
 
                public void CaptureLocalVariable (EmitContext ec, LocalInfo local_info)
                {
+                       ec.CurrentBlock.Explicit.HasCapturedVariable = true;
+                       if (ec.CurrentBlock.Explicit != local_info.Block.Explicit)
+                               AddReferenceFromChildrenBlock (ec.CurrentBlock.Explicit);
+
                        if (local_info.HoistedVariableReference != null)
                                return;
 
                        HoistedVariable var = new HoistedLocalVariable (this, local_info, GetVariableMangledName (local_info));
                        local_info.HoistedVariableReference = var;
+
+                       if (hoisted_locals == null)
+                               hoisted_locals = new ArrayList ();
+
+                       hoisted_locals.Add (var);
                }
 
                public void CaptureParameter (EmitContext ec, ParameterReference param_ref)
                {
-                       if (param_ref.HoistedVariable != null)
+                       ec.CurrentBlock.Explicit.HasCapturedVariable = true;
+                       AddReferenceFromChildrenBlock (ec.CurrentBlock.Explicit);
+
+                       if (param_ref.GetHoistedVariable (ec) != null)
                                return;
 
                        if (hoisted_params == null)
-                               hoisted_params = new ArrayList ();
+                               hoisted_params = new ArrayList (2);
 
                        HoistedVariable expr = new HoistedParameter (this, param_ref);
                        param_ref.Parameter.HoistedVariableReference = expr;
                        hoisted_params.Add (expr);
                }
 
-               public HoistedThis CaptureThis (EmitContext ec, This t)
-               {
-                       hoisted_this = new HoistedThis (this, t);
-                       return hoisted_this;
-               }
-
-               void DefineStoreyReferences ()
+               public void ChangeParentStorey (AnonymousMethodStorey parentStorey)
                {
-                       if (used_parent_storeys == null || references_defined)
-                               return;
-
-                       references_defined = true;
-
-                       //
-                       // For each used variable from parent scope we allocate its local reference point
-                       //
-                       for (int i = 0; i < used_parent_storeys.Count; ++i) {
-                               StoreyFieldPair sf = (StoreyFieldPair) used_parent_storeys [i];
-                               AnonymousMethodStorey p_storey = sf.Storey;
-                               TypeExpr type_expr = new TypeExpression (p_storey.TypeBuilder, Location);
-
-                               sf.Field = AddCompilerGeneratedField ("<>f__ref$" + p_storey.ID, type_expr);
-                               sf.Field.Define ();
-                       }
+                       Parent = parentStorey;
+                       type_params = null;
                }
 
                //
                // Initializes all hoisted variables
                //
-               public void EmitHoistedVariables (EmitContext ec)
+               public void EmitStoreyInstantiation (EmitContext ec)
                {
                        // There can be only one instance variable for each storey type
                        if (Instance != null)
                                throw new InternalErrorException ();
 
-                       //
-                       // A storey with hoisted `this' is an instance method
-                       //
-                       if (!HasHoistedVariables) {
-                               throw new NotImplementedException ();
-                       }
-
-                       DefineStoreyReferences ();
+                       SymbolWriter.OpenCompilerGeneratedBlock (ec.ig);
 
                        //
                        // Create an instance of storey type
                        //
                        Expression storey_type_expr;
-                       if (IsGeneric) {
+                       if (is_generic) {
                                //
                                // Use current method type parameter (MVAR) for top level storey only. All
                                // nested storeys use class type parameter (VAR)
                                //
-                               TypeParameter[] tparams = ec.CurrentAnonymousMethod != null ?
-                                       ec.CurrentAnonymousMethod.Storey.CurrentTypeParameters :
-                                       ec.GenericDeclContainer.CurrentTypeParameters;
-
-                               if (tparams.Length != CountTypeParameters) {
-                                       TypeParameter [] full = new TypeParameter [CountTypeParameters];
-                                       DeclSpace parent = ec.DeclContainer.Parent;
-                                       parent.CurrentTypeParameters.CopyTo (full, 0);
-                                       tparams.CopyTo (full, parent.CountTypeParameters);
-                                       tparams = full;
+                               TypeParameter[] tparams = ec.CurrentAnonymousMethod != null && ec.CurrentAnonymousMethod.Storey != null ?
+                                       ec.CurrentAnonymousMethod.Storey.TypeParameters :
+                                       ec.GenericDeclContainer.TypeParameters;
+
+                               TypeArguments targs = new TypeArguments ();
+
+                               if (tparams.Length < CountTypeParameters) {
+                                       TypeParameter[] parent_tparams = ec.DeclContainer.Parent.PartialContainer.TypeParameters;
+                                       for (int i = 0; i < parent_tparams.Length; ++i)
+                                               targs.Add (new TypeParameterExpr (parent_tparams[i], Location));
                                }
+                               
+                               for (int i = 0; i < tparams.Length; ++i)
+                                       targs.Add (new TypeParameterExpr (tparams[i], Location));
 
-                               storey_type_expr = new ConstructedType (TypeBuilder, tparams, Location);
+                               storey_type_expr = new GenericTypeExpr (TypeBuilder, targs, Location);
                        } else {
                                storey_type_expr = new TypeExpression (TypeBuilder, Location);
                        }
@@ -268,6 +319,9 @@ namespace Mono.CSharp {
                        Instance.Store (ec);
 
                        EmitHoistedFieldsInitialization (ec);
+
+                       SymbolWriter.DefineScopeVariable (ID, Instance.Builder);
+                       SymbolWriter.CloseCompilerGeneratedBlock (ec.ig);
                }
 
                void EmitHoistedFieldsInitialization (EmitContext ec)
@@ -280,8 +334,11 @@ namespace Mono.CSharp {
                                        //
                                        // Setting local field
                                        //
-                                       FieldExpr f_set_expr = new FieldExpr (sf.Field.FieldBuilder, Location);
-                                       f_set_expr.InstanceExpression = GetStoreyInstanceExpression (ec);
+                                       Expression instace_expr = GetStoreyInstanceExpression (ec);
+                                       FieldExpr f_set_expr = TypeManager.IsGenericType (instace_expr.Type) ?
+                                               new FieldExpr (sf.Field.FieldBuilder, instace_expr.Type, Location) :
+                                               new FieldExpr (sf.Field.FieldBuilder, Location);
+                                       f_set_expr.InstanceExpression = instace_expr;
 
                                        SimpleAssign a = new SimpleAssign (f_set_expr, sf.Storey.GetStoreyInstanceExpression (ec));
                                        if (a.Resolve (ec) != null)
@@ -289,6 +346,14 @@ namespace Mono.CSharp {
                                }
                        }
 
+                       //
+                       // Define hoisted `this' in top-level storey only 
+                       //
+                       if (OriginalSourceBlock.Explicit.HasCapturedThis && !(Parent is AnonymousMethodStorey)) {
+                               AddCapturedThisField (ec);
+                               OriginalSourceBlock.AddScopeStatement (new ThisInitializer (hoisted_this));
+                       }
+
                        //
                        // Setting currect anonymous method to null blocks any further variable hoisting
                        //
@@ -296,24 +361,54 @@ namespace Mono.CSharp {
                        ec.CurrentAnonymousMethod = null;
 
                        if (hoisted_params != null) {
-                               foreach (HoistedParameter hp in hoisted_params) {
-                                       hp.EmitHoistingAssignment (ec);
-                               }
-                       }
-
-                       if (hoisted_this != null) {
-                               hoisted_this.EmitHoistingAssignment (ec);
+                               EmitHoistedParameters (ec, hoisted_params);
                        }
 
                        ec.CurrentAnonymousMethod = ae;
                }
 
+               protected virtual void EmitHoistedParameters (EmitContext ec, ArrayList hoisted)
+               {
+                       foreach (HoistedParameter hp in hoisted) {
+                               hp.EmitHoistingAssignment (ec);
+                       }
+               }
+
                public override void EmitType ()
                {
-                       DefineStoreyReferences ();
+                       SymbolWriter.DefineAnonymousScope (ID);
+
+                       if (hoisted_this != null)
+                               hoisted_this.EmitSymbolInfo ();
+
+                       if (hoisted_locals != null) {
+                               foreach (HoistedVariable local in hoisted_locals)
+                                       local.EmitSymbolInfo ();
+                       }
+
+                       if (hoisted_params != null) {
+                               foreach (HoistedParameter param in hoisted_params)
+                                       param.EmitSymbolInfo ();
+                       }
+
+                       if (used_parent_storeys != null) {
+                               foreach (StoreyFieldPair sf in used_parent_storeys) {
+                                       SymbolWriter.DefineCapturedScope (ID, sf.Storey.ID, sf.Field.Name);
+                               }
+                       }
+
                        base.EmitType ();
                }
 
+               public AnonymousMethodStorey GetGenericStorey ()
+               {
+                       DeclSpace storey = this;
+                       while (storey != null && storey.CurrentTypeParameters.Length == 0)
+                               storey = storey.Parent;
+
+                       return storey as AnonymousMethodStorey;
+               }
+
                //
                // Returns a field which holds referenced storey instance
                //
@@ -380,10 +475,8 @@ namespace Mono.CSharp {
                        return local_info.Name;
                }
 
-               public bool HasHoistedVariables {
-                       get {
-                               return true; // TODO: Finish this optimization
-                       }
+               public HoistedThis HoistedThis {
+                       get { return hoisted_this; }
                }
 
                //
@@ -410,13 +503,18 @@ namespace Mono.CSharp {
                public MethodInfo MutateGenericMethod (MethodInfo method)
                {
 #if GMCS_SOURCE
+                       Type [] t_args = TypeManager.GetGenericArguments (method);
                        if (TypeManager.IsGenericType (method.DeclaringType)) {
                                Type t = MutateGenericType (method.DeclaringType);
-                               if (t != method.DeclaringType)
-                                       method = TypeBuilder.GetMethod (t, method);
+                               if (t != method.DeclaringType) {
+                                       method = (MethodInfo) TypeManager.DropGenericMethodArguments (method);
+                                       if (method.Module == Module.Builder)
+                                               method = TypeBuilder.GetMethod (t, method);
+                                       else
+                                               method = (MethodInfo) MethodInfo.GetMethodFromHandle (method.MethodHandle, t.TypeHandle);
+                               }                               
                        }
 
-                       Type [] t_args = TypeManager.GetGenericArguments (method);
                        if (t_args == null || t_args.Length == 0)
                                return method;
 
@@ -435,23 +533,39 @@ namespace Mono.CSharp {
                        if (TypeManager.IsGenericType (ctor.DeclaringType)) {
                                Type t = MutateGenericType (ctor.DeclaringType);
                                if (t != ctor.DeclaringType) {
-                                       // TODO: It should throw on imported types
-                                       return TypeBuilder.GetConstructor (t, ctor);
+                                       ctor = (ConstructorInfo) TypeManager.DropGenericMethodArguments (ctor);
+                                       if (ctor.Module == Module.Builder)
+                                               return TypeBuilder.GetConstructor (t, ctor);
+                                               
+                                       return (ConstructorInfo) ConstructorInfo.GetMethodFromHandle (ctor.MethodHandle, t.TypeHandle);
                                }
                        }
 #endif
                        return ctor;
                }
+               
+               public FieldInfo MutateField (FieldInfo field)
+               {
+#if GMCS_SOURCE
+                       if (TypeManager.IsGenericType (field.DeclaringType)) {
+                               Type t = MutateGenericType (field.DeclaringType);
+                               if (t != field.DeclaringType) {
+                                       // TODO: It should throw on imported types
+                                       return TypeBuilder.GetField (t, field);
+                               }
+                       }
+#endif
+                       return field;
+               }               
 
 #if GMCS_SOURCE
                protected Type MutateArrayType (Type array)
                {
                        int rank = array.GetArrayRank ();
                        Type element = TypeManager.GetElementType (array);
-                       if (element.IsArray)
-                               throw new NotImplementedException ();
-
-                       if (TypeManager.IsGenericParameter (element)) {
+                       if (element.IsArray) {
+                               element = MutateArrayType (element);
+                       } else if (TypeManager.IsGenericParameter (element)) {
                                element = MutateGenericArgument (element);
                        } else if (TypeManager.IsGenericType (element)) {
                                element = MutateGenericType (element);
@@ -471,7 +585,7 @@ namespace Mono.CSharp {
                        for (int i = 0; i < t_args.Length; ++i)
                                t_args [i] = MutateType (t_args [i]);
 
-                       return type.GetGenericTypeDefinition ().MakeGenericType (t_args);
+                       return TypeManager.DropGenericTypeArguments (type).MakeGenericType (t_args);
                }
 #endif
 
@@ -489,23 +603,63 @@ namespace Mono.CSharp {
                        return type;
                }
 
+               public ArrayList ReferencesFromChildrenBlock {
+                       get { return children_references; }
+               }
+
                public static void Reset ()
                {
                        unique_id = 0;
-               }
+               }               
        }
 
        public abstract class HoistedVariable
        {
+               class ExpressionTreeProxy : Expression
+               {
+                       readonly HoistedVariable hv;
+
+                       public ExpressionTreeProxy (HoistedVariable hv)
+                       {
+                               this.hv = hv;
+                       }
+
+                       public override Expression CreateExpressionTree (EmitContext ec)
+                       {
+                               throw new NotSupportedException ("ET");
+                       }
+
+                       public override Expression DoResolve (EmitContext ec)
+                       {
+                               eclass = ExprClass.Value;
+                               type = TypeManager.expression_type_expr.Type;
+                               return this;
+                       }
+
+                       public override void Emit (EmitContext ec)
+                       {
+                               Expression e = hv.GetFieldExpression (ec).CreateExpressionTree (ec);
+                               // This should never fail
+                               e = e.Resolve (ec);
+                               if (e != null)
+                                       e.Emit (ec);
+                       }
+               }
+       
                protected readonly AnonymousMethodStorey storey;
                protected Field field;
                Hashtable cached_inner_access; // TODO: Hashtable is too heavyweight
+               FieldExpr cached_outer_access;
 
                protected HoistedVariable (AnonymousMethodStorey storey, string name, Type type)
+                       : this (storey, storey.AddCapturedVariable (name, type))
                {
-                       this.storey = storey;
+               }
 
-                       this.field = storey.AddCapturedVariable (name, type);
+               protected HoistedVariable (AnonymousMethodStorey storey, Field field)
+               {
+                       this.storey = storey;
+                       this.field = field;
                }
 
                public void AddressOf (EmitContext ec, AddressOp mode)
@@ -513,6 +667,11 @@ namespace Mono.CSharp {
                        GetFieldExpression (ec).AddressOf (ec, mode);
                }
 
+               public Expression CreateExpressionTree (EmitContext ec)
+               {
+                       return new ExpressionTreeProxy (this);
+               }
+
                public void Emit (EmitContext ec)
                {
                        GetFieldExpression (ec).Emit (ec);
@@ -523,18 +682,21 @@ namespace Mono.CSharp {
                //
                protected FieldExpr GetFieldExpression (EmitContext ec)
                {
-                       if (ec.CurrentAnonymousMethod == null) {
+                       if (ec.CurrentAnonymousMethod == null || ec.CurrentAnonymousMethod.Storey == null) {
+                               if (cached_outer_access != null)
+                                       return cached_outer_access;
+
                                //
                                // When setting top-level hoisted variable in generic storey
                                // change storey generic types to method generic types (VAR -> MVAR)
                                //
-                               FieldExpr outer_access = storey.MemberName.IsGeneric ?
+                               cached_outer_access = storey.MemberName.IsGeneric ?
                                        new FieldExpr (field.FieldBuilder, storey.Instance.Type, field.Location) :
                                        new FieldExpr (field.FieldBuilder, field.Location);
 
-                               outer_access.InstanceExpression = storey.GetStoreyInstanceExpression (ec);
-                               outer_access.Resolve (ec);
-                               return outer_access;
+                               cached_outer_access.InstanceExpression = storey.GetStoreyInstanceExpression (ec);
+                               cached_outer_access.Resolve (ec);
+                               return cached_outer_access;
                        }
 
                        FieldExpr inner_access;
@@ -570,7 +732,7 @@ namespace Mono.CSharp {
 
        class HoistedParameter : HoistedVariable
        {
-               class HoistedFieldAssign : Assign
+               sealed class HoistedFieldAssign : Assign
                {
                        public HoistedFieldAssign (Expression target, Expression source)
                                : base (target, source, source.Location)
@@ -595,6 +757,12 @@ namespace Mono.CSharp {
                        this.parameter = par;
                }
 
+               public HoistedParameter (HoistedParameter hp, string name)
+                       : base (hp.storey, name, hp.parameter.Type)
+               {
+                       this.parameter = hp.parameter;
+               }
+
                public void EmitHoistingAssignment (EmitContext ec)
                {
                        //
@@ -622,30 +790,30 @@ namespace Mono.CSharp {
 
        class HoistedLocalVariable : HoistedVariable
        {
+               readonly string name;
+
                public HoistedLocalVariable (AnonymousMethodStorey scope, LocalInfo local, string name)
                        : base (scope, name, local.VariableType)
                {
+                       this.name = local.Name;
                }
 
                public override void EmitSymbolInfo ()
                {
-                       SymbolWriter.DefineCapturedLocal (storey.ID, field.Name, field.Name);
+                       SymbolWriter.DefineCapturedLocal (storey.ID, name, field.Name);
                }
        }
 
        public class HoistedThis : HoistedVariable
        {
-               readonly This this_reference;
-
-               public HoistedThis (AnonymousMethodStorey storey, This this_reference)
-                       : base (storey, "<>f__this", this_reference.Type)
+               public HoistedThis (AnonymousMethodStorey storey, Field field)
+                       : base (storey, field)
                {
-                       this.this_reference = this_reference;
                }
 
                public void EmitHoistingAssignment (EmitContext ec)
                {
-                       SimpleAssign a = new SimpleAssign (GetFieldExpression (ec), this_reference);
+                       SimpleAssign a = new SimpleAssign (GetFieldExpression (ec), ec.GetThis (field.Location));
                        if (a.Resolve (ec) != null)
                                a.EmitStatement (ec);
                }
@@ -654,15 +822,10 @@ namespace Mono.CSharp {
                {
                        SymbolWriter.DefineCapturedThis (storey.ID, field.Name);
                }
-       }
 
-       // TODO: Remove, it should be done in block
-       public interface IAnonymousHost
-       {
-               //
-               // Invoked if a yield statement is found in the body
-               //
-               void SetYields ();
+               public Field Field {
+                       get { return field; }
+               }
        }
 
        //
@@ -670,15 +833,11 @@ namespace Mono.CSharp {
        //
        public class AnonymousMethodExpression : Expression
        {
-               protected readonly TypeContainer Host;
-               public readonly Parameters Parameters;
                ListDictionary compatibles;
                public ToplevelBlock Block;
 
-               public AnonymousMethodExpression (TypeContainer host, Parameters parameters, Location loc)
+               public AnonymousMethodExpression (Location loc)
                {
-                       this.Host = host;
-                       this.Parameters = parameters;
                        this.loc = loc;
                        this.compatibles = new ListDictionary ();
                }
@@ -691,9 +850,13 @@ namespace Mono.CSharp {
 
                public virtual bool HasExplicitParameters {
                        get {
-                               return Parameters != null;
+                               return Parameters != ParametersCompiled.Undefined;
                        }
                }
+               
+               public ParametersCompiled Parameters {
+                       get { return Block.Parameters; }
+               }
 
                //
                // Returns true if the body of lambda expression can be implicitly
@@ -701,8 +864,10 @@ namespace Mono.CSharp {
                //
                public bool ImplicitStandardConversionExists (EmitContext ec, Type delegate_type)
                {
-                       using (ec.Set (EmitContext.Flags.ProbingMode)) {
-                               return Compatible (ec, delegate_type) != null;
+                       using (ec.With (EmitContext.Flags.InferReturnType, false)) {
+                               using (ec.Set (EmitContext.Flags.ProbingMode)) {
+                                       return Compatible (ec, delegate_type) != null;
+                               }
                        }
                }
 
@@ -711,7 +876,6 @@ namespace Mono.CSharp {
                        if (TypeManager.IsDelegateType (delegate_type))
                                return delegate_type;
 
-#if GMCS_SOURCE
                        if (TypeManager.DropGenericTypeArguments (delegate_type) == TypeManager.expression_type) {
                                delegate_type = TypeManager.GetTypeArguments (delegate_type) [0];
                                if (TypeManager.IsDelegateType (delegate_type))
@@ -721,14 +885,13 @@ namespace Mono.CSharp {
                                        GetSignatureForError (), TypeManager.CSharpName (delegate_type));
                                return null;
                        }
-#endif
 
                        Report.Error (1660, loc, "Cannot convert `{0}' to non-delegate type `{1}'",
                                      GetSignatureForError (), TypeManager.CSharpName (delegate_type));
                        return null;
                }
 
-               protected bool VerifyExplicitParameters (Type delegate_type, ParameterData parameters, bool ignore_error)
+               protected bool VerifyExplicitParameters (Type delegate_type, AParametersCollection parameters, bool ignore_error)
                {
                        if (VerifyParameterCompatibility (delegate_type, parameters, ignore_error))
                                return true;
@@ -741,7 +904,7 @@ namespace Mono.CSharp {
                        return false;
                }
 
-               protected bool VerifyParameterCompatibility (Type delegate_type, ParameterData invoke_pd, bool ignore_errors)
+               protected bool VerifyParameterCompatibility (Type delegate_type, AParametersCollection invoke_pd, bool ignore_errors)
                {
                        if (Parameters.Count != invoke_pd.Count) {
                                if (ignore_errors)
@@ -751,27 +914,28 @@ namespace Mono.CSharp {
                                              TypeManager.CSharpName (delegate_type), Parameters.Count.ToString ());
                                return false;
                        }
-                       
-                       if (!HasExplicitParameters)
-                               return true;                    
 
+                       bool has_implicit_parameters = !HasExplicitParameters;
                        bool error = false;
+
                        for (int i = 0; i < Parameters.Count; ++i) {
-                               Parameter.Modifier p_mod = invoke_pd.ParameterModifier (i);
-                               if (Parameters.ParameterModifier (i) != p_mod && p_mod != Parameter.Modifier.PARAMS) {
+                               Parameter.Modifier p_mod = invoke_pd.FixedParameters [i].ModFlags;
+                               if (Parameters.FixedParameters [i].ModFlags != p_mod && p_mod != Parameter.Modifier.PARAMS) {
                                        if (ignore_errors)
                                                return false;
                                        
                                        if (p_mod == Parameter.Modifier.NONE)
                                                Report.Error (1677, loc, "Parameter `{0}' should not be declared with the `{1}' keyword",
-                                                             (i + 1).ToString (), Parameter.GetModifierSignature (Parameters.ParameterModifier (i)));
+                                                             (i + 1).ToString (), Parameter.GetModifierSignature (Parameters.FixedParameters [i].ModFlags));
                                        else
                                                Report.Error (1676, loc, "Parameter `{0}' must be declared with the `{1}' keyword",
                                                              (i+1).ToString (), Parameter.GetModifierSignature (p_mod));
                                        error = true;
-                                       continue;
                                }
 
+                               if (has_implicit_parameters)
+                                       continue;
+
                                Type type = invoke_pd.Types [i];
                                
                                // We assume that generic parameters are always inflated
@@ -781,14 +945,14 @@ namespace Mono.CSharp {
                                if (TypeManager.HasElementType (type) && TypeManager.IsGenericParameter (TypeManager.GetElementType (type)))
                                        continue;
                                
-                               if (invoke_pd.ParameterType (i) != Parameters.ParameterType (i)) {
+                               if (invoke_pd.Types [i] != Parameters.Types [i]) {
                                        if (ignore_errors)
                                                return false;
                                        
                                        Report.Error (1678, loc, "Parameter `{0}' is declared as type `{1}' but should be `{2}'",
                                                      (i+1).ToString (),
-                                                     TypeManager.CSharpName (Parameters.ParameterType (i)),
-                                                     TypeManager.CSharpName (invoke_pd.ParameterType (i)));
+                                                     TypeManager.CSharpName (Parameters.Types [i]),
+                                                     TypeManager.CSharpName (invoke_pd.Types [i]));
                                        error = true;
                                }
                        }
@@ -805,19 +969,15 @@ namespace Mono.CSharp {
                                return false;
 
                        if (!TypeManager.IsDelegateType (delegate_type)) {
-#if GMCS_SOURCE
                                if (TypeManager.DropGenericTypeArguments (delegate_type) != TypeManager.expression_type)
                                        return false;
 
-                               delegate_type = delegate_type.GetGenericArguments () [0];
+                               delegate_type = TypeManager.GetTypeArguments (delegate_type) [0];
                                if (!TypeManager.IsDelegateType (delegate_type))
                                        return false;
-#else
-                               return false;
-#endif
                        }
                        
-                       ParameterData d_params = TypeManager.GetDelegateParameters (delegate_type);
+                       AParametersCollection d_params = TypeManager.GetDelegateParameters (delegate_type);
                        if (d_params.Count != Parameters.Count)
                                return false;
 
@@ -827,10 +987,10 @@ namespace Mono.CSharp {
                                        if (!TypeManager.HasElementType (itype))
                                                continue;
                                        
-                                       if (!TypeManager.IsGenericParameter (itype.GetElementType ()))
+                                       if (!TypeManager.IsGenericParameter (TypeManager.GetElementType (itype)))
                                            continue;
                                }
-                               type_inference.ExactInference (Parameters.FixedParameters[i].ParameterType, itype);
+                               type_inference.ExactInference (Parameters.Types [i], itype);
                        }
                        return true;
                }
@@ -845,6 +1005,7 @@ namespace Mono.CSharp {
                        if (am == null)
                                return null;
 
+                       // Stop referencing gmcs NullLiteral type
                        if (am.ReturnType == TypeManager.null_type)
                                am.ReturnType = null;
 
@@ -894,7 +1055,7 @@ namespace Mono.CSharp {
                                        am = CreateExpressionTree (ec, delegate_type);
 
                                if (!ec.IsInProbingMode)
-                                       compatibles.Add (type, am);
+                                       compatibles.Add (type, am == null ? EmptyExpression.Null : am);
 
                                return am;
                        } catch (Exception e) {
@@ -913,19 +1074,19 @@ namespace Mono.CSharp {
                        return null;
                }
 
-               protected virtual Parameters ResolveParameters (EmitContext ec, TypeInferenceContext tic, Type delegate_type)
+               protected virtual ParametersCompiled ResolveParameters (EmitContext ec, TypeInferenceContext tic, Type delegate_type)
                {
-                       ParameterData delegate_parameters = TypeManager.GetDelegateParameters (delegate_type);
+                       AParametersCollection delegate_parameters = TypeManager.GetDelegateParameters (delegate_type);
 
-                       if (Parameters == null) {
+                       if (Parameters == ParametersCompiled.Undefined) {
                                //
                                // We provide a set of inaccessible parameters
                                //
                                Parameter[] fixedpars = new Parameter[delegate_parameters.Count];
 
                                for (int i = 0; i < delegate_parameters.Count; i++) {
-                                       Parameter.Modifier i_mod = delegate_parameters.ParameterModifier (i);
-                                       if ((i_mod & Parameter.Modifier.OUTMASK) != 0) {
+                                       Parameter.Modifier i_mod = delegate_parameters.FixedParameters [i].ModFlags;
+                                       if (i_mod == Parameter.Modifier.OUT) {
                                                Report.Error (1688, loc, "Cannot convert anonymous " +
                                                                  "method block without a parameter list " +
                                                                  "to delegate type `{0}' because it has " +
@@ -934,11 +1095,11 @@ namespace Mono.CSharp {
                                                return null;
                                        }
                                        fixedpars[i] = new Parameter (
-                                               delegate_parameters.ParameterType (i), null,
-                                               delegate_parameters.ParameterModifier (i), null, loc);
+                                               null, null,
+                                               delegate_parameters.FixedParameters [i].ModFlags, null, loc);
                                }
 
-                               return Parameters.CreateFullyResolved (fixedpars, delegate_parameters.Types);
+                               return ParametersCompiled.CreateFullyResolved (fixedpars, delegate_parameters.Types);
                        }
 
                        if (!VerifyExplicitParameters (delegate_type, delegate_parameters, ec.IsInProbingMode)) {
@@ -985,6 +1146,13 @@ namespace Mono.CSharp {
                        // nothing, as we only exist to not do anything.
                }
 
+               public static void Error_AddressOfCapturedVar (IVariableReference var, Location loc)
+               {
+                       Report.Error (1686, loc,
+                               "Local variable or parameter `{0}' cannot have their address taken and be used inside an anonymous method or lambda expression",
+                               var.Name);
+               }
+
                public override string GetSignatureForError ()
                {
                        return ExprClassName;
@@ -992,7 +1160,7 @@ namespace Mono.CSharp {
 
                protected AnonymousMethodBody CompatibleMethod (EmitContext ec, TypeInferenceContext tic, Type return_type, Type delegate_type)
                {
-                       Parameters p = ResolveParameters (ec, tic, delegate_type);
+                       ParametersCompiled p = ResolveParameters (ec, tic, delegate_type);
                        if (p == null)
                                return null;
 
@@ -1005,11 +1173,9 @@ namespace Mono.CSharp {
                        return anonymous;
                }
 
-               protected virtual AnonymousMethodBody CompatibleMethodFactory (Type return_type, Type delegate_type, Parameters p, ToplevelBlock b)
+               protected virtual AnonymousMethodBody CompatibleMethodFactory (Type return_type, Type delegate_type, ParametersCompiled p, ToplevelBlock b)
                {
-                       return new AnonymousMethodBody (Host,
-                               p, b, return_type,
-                               delegate_type, loc);
+                       return new AnonymousMethodBody (p, b, return_type, delegate_type, loc);
                }
 
                protected override void CloneTo (CloneContext clonectx, Expression t)
@@ -1034,9 +1200,9 @@ namespace Mono.CSharp {
                        public AnonymousMethodMethod (DeclSpace parent, AnonymousExpression am, AnonymousMethodStorey storey,
                                                          GenericMethod generic, TypeExpr return_type,
                                                          int mod, string real_name, MemberName name,
-                                                         Parameters parameters)
+                                                         ParametersCompiled parameters)
                                : base (parent, generic, return_type, mod | Modifiers.COMPILER_GENERATED,
-                                               false, name, parameters, null)
+                                               name, parameters, null)
                        {
                                this.AnonymousMethod = am;
                                this.Storey = storey;
@@ -1054,20 +1220,25 @@ namespace Mono.CSharp {
                                return aec;
                        }
 
-                       public override bool Define ()
+                       protected override bool ResolveMemberType ()
                        {
+                               if (!base.ResolveMemberType ())
+                                       return false;
+
                                if (Storey != null && Storey.IsGeneric) {
-                               
-                                       if (!Parameters.Empty) {
-                                               Type [] ptypes = Parameters.Types;
-                                               for (int i = 0; i < ptypes.Length; ++i)
-                                                       ptypes [i] = Storey.MutateType (ptypes [i]);
+                                       AnonymousMethodStorey gstorey = Storey.GetGenericStorey ();
+                                       if (gstorey != null) {
+                                               if (!Parameters.IsEmpty) {
+                                                       Type [] ptypes = Parameters.Types;
+                                                       for (int i = 0; i < ptypes.Length; ++i)
+                                                               ptypes [i] = gstorey.MutateType (ptypes [i]);
+                                               }
+
+                                               member_type = gstorey.MutateType (member_type);
                                        }
-
-                                       member_type = Storey.MutateType (ReturnType);
                                }
 
-                               return base.Define ();
+                               return true;
                        }
 
                        public override void Emit ()
@@ -1077,8 +1248,15 @@ namespace Mono.CSharp {
                                // when the method is of generic type and has hoisted variables
                                //
                                if (Storey == Parent && Storey.IsGeneric) {
-                                       AnonymousMethod.aec.ReturnType = Storey.MutateType (ReturnType);
-                                       block.MutateHoistedGenericType (Storey);
+                                       AnonymousMethodStorey gstorey = Storey.GetGenericStorey ();
+                                       if (gstorey != null) {
+                                               AnonymousMethod.aec.ReturnType = gstorey.MutateType (ReturnType);
+                                               block.MutateHoistedGenericType (gstorey);
+                                       }
+                               }
+
+                               if (MethodBuilder == null) {
+                                       Define ();
                                }
 
                                base.Emit ();
@@ -1096,24 +1274,15 @@ namespace Mono.CSharp {
                protected readonly ToplevelBlock Block;
 
                public Type ReturnType;
-               public readonly DeclSpace Host;
-
-               //
-               // The implicit method we create
-               //
-               protected AnonymousMethodMethod method;
                protected EmitContext aec;
 
-               protected AnonymousExpression (DeclSpace host, ToplevelBlock block, Type return_type, Location loc)
+               protected AnonymousExpression (ToplevelBlock block, Type return_type, Location loc)
                {
                        this.ReturnType = return_type;
-                       this.Host = host;
-
                        this.Block = block;
                        this.loc = loc;
                }
 
-               public abstract void AddStoreyReference (AnonymousMethodStorey storey);
                public abstract string ContainerType { get; }
                public abstract bool IsIterator { get; }
                public abstract AnonymousMethodStorey Storey { get; }
@@ -1124,7 +1293,6 @@ namespace Mono.CSharp {
                        aec = new EmitContext (
                                ec.ResolveContext, ec.TypeContainer, ec.DeclContainer,
                                Location, null, ReturnType,
-                               /* REVIEW */ (ec.InIterator ? Modifiers.METHOD_YIELDS : 0) |
                                (ec.InUnsafe ? Modifiers.UNSAFE : 0), /* No constructor */ false);
 
                        aec.CurrentAnonymousMethod = this;
@@ -1160,20 +1328,36 @@ namespace Mono.CSharp {
 
                        return res;
                }
+
+               public void SetHasThisAccess ()
+               {
+                       Block.HasCapturedThis = true;
+                       ExplicitBlock b = Block.Parent.Explicit;
+
+                       while (b != null) {
+                               if (b.HasCapturedThis)
+                                       return;
+
+                               b.HasCapturedThis = true;
+                               b = b.Parent == null ? null : b.Parent.Explicit;
+                       }
+               }
        }
 
        public class AnonymousMethodBody : AnonymousExpression
        {
-               ArrayList referenced_storeys;
-               readonly Parameters parameters;
+               protected readonly ParametersCompiled parameters;
+               AnonymousMethodStorey storey;
+
+               AnonymousMethodMethod method;
+               Field am_cache;
+
                static int unique_id;
 
-               public AnonymousMethodBody (
-                                       DeclSpace host,
-                                       Parameters parameters,
+               public AnonymousMethodBody (ParametersCompiled parameters,
                                        ToplevelBlock block, Type return_type, Type delegate_type,
                                        Location loc)
-                       : base (host, block, return_type, loc)
+                       : base (block, return_type, loc)
                {
                        this.type = delegate_type;
                        this.parameters = parameters;
@@ -1184,30 +1368,13 @@ namespace Mono.CSharp {
                }
 
                public override AnonymousMethodStorey Storey {
-                       get { return method.Storey; }
+                       get { return storey; }
                }
 
                public override bool IsIterator {
                        get { return false; }
                }
 
-               //
-               // Adds new storey reference to track out of scope variables
-               //
-               public override void AddStoreyReference (AnonymousMethodStorey storey)
-               {
-                       if (referenced_storeys == null) {
-                               referenced_storeys = new ArrayList ();
-                       } else {
-                               foreach (AnonymousMethodStorey ams in referenced_storeys) {
-                                       if (ams == storey)
-                                               return;
-                               }
-                       }
-
-                       referenced_storeys.Add (storey);
-               }
-
                public override Expression CreateExpressionTree (EmitContext ec)
                {
                        Report.Error (1945, loc, "An expression tree cannot contain an anonymous method expression");
@@ -1219,20 +1386,7 @@ namespace Mono.CSharp {
                        if (aec == null && !Compatible (ec))
                                return false;
 
-                       // Don't define anything when we are in probing scope (nested anonymous methods)
-                       if (ec.IsInProbingMode)
-                               return true;
-
-                       method = DoCreateMethodHost (ec);
-
-                       //
-                       // Define method only when is not inside AnonymousMethodStorey because a container passed its Define
-                       //
-                       if (method.Storey != null && method.Storey.HasHoistedVariables)
-                               return true;
-
-                       method.ResolveMembers ();
-                       return method.Define ();
+                       return true;
                }
 
                //
@@ -1240,29 +1394,6 @@ namespace Mono.CSharp {
                //
                AnonymousMethodMethod DoCreateMethodHost (EmitContext ec)
                {
-                       //
-                       // Searches references and parent blocks for the best store for
-                       // this anynous method
-                       //
-                       AnonymousMethodStorey storey = FindBestMethodStorey ();
-                       
-                       if (referenced_storeys != null && referenced_storeys.Count > 1) {
-                               foreach (AnonymousMethodStorey s in referenced_storeys) {
-                                       if (s == storey)
-                                               continue;
-
-                                       storey.AddParentStoreyReference (s);
-                                       Block.Parent.Explicit.PropagateStoreyReference (s);
-                               }
-                       } else {
-                               //
-                               // Ensure we have a reference between this block and a storey
-                               // where this anymous method is created
-                               //
-                               if (Block.Parent != null)
-                                       Block.Parent.Explicit.PropagateStoreyReference (storey);
-                       }
-
                        //
                        // Anonymous method body can be converted to
                        //
@@ -1272,13 +1403,17 @@ namespace Mono.CSharp {
                        //
 
                        int modifiers;
-                       if (storey != null) {
-                               modifiers = storey.HasHoistedVariables ? Modifiers.INTERNAL : Modifiers.PRIVATE;
+                       if (Block.HasCapturedVariable || Block.HasCapturedThis) {
+                               storey = FindBestMethodStorey ();
+                               modifiers = storey != null ? Modifiers.INTERNAL : Modifiers.PRIVATE;
                        } else {
+                               if (ec.CurrentAnonymousMethod != null)
+                                       storey = ec.CurrentAnonymousMethod.Storey;
+
                                modifiers = Modifiers.STATIC | Modifiers.PRIVATE;
                        }
 
-                       DeclSpace parent = (modifiers & Modifiers.PRIVATE) != 0 ? Host : storey;
+                       DeclSpace parent = storey != null ? storey : ec.TypeContainer;
 
                        MemberCore mc = ec.ResolveContext as MemberCore;
                        string name = CompilerGeneratedClass.MakeName (parent != storey ? mc.Name : null,
@@ -1286,13 +1421,18 @@ namespace Mono.CSharp {
 
                        MemberName member_name;
                        GenericMethod generic_method;
-                       if ((modifiers & Modifiers.PRIVATE) != 0 && mc.MemberName.IsGeneric) {
+                       if (storey == null && mc.MemberName.IsGeneric) {
                                member_name = new MemberName (name, mc.MemberName.TypeArguments.Clone (), Location);
 
-                               generic_method = new GenericMethod (
-                                       Host.NamespaceEntry, storey, member_name,
+                               generic_method = new GenericMethod (parent.NamespaceEntry, parent, member_name,
                                        new TypeExpression (ReturnType, Location), parameters);
-                               generic_method.SetParameterInfo (null);
+
+                               ArrayList list = new ArrayList ();
+                               foreach (TypeParameter tparam in ((IMethodData)mc).GenericMethod.CurrentTypeParameters) {
+                                       if (tparam.Constraints != null)
+                                               list.Add (tparam.Constraints.Clone ());
+                               }
+                               generic_method.SetParameterInfo (list);
                        } else {
                                member_name = new MemberName (name, Location);
                                generic_method = null;
@@ -1320,30 +1460,111 @@ namespace Mono.CSharp {
 
                public override void Emit (EmitContext ec)
                {
+                       //
+                       // Use same anonymous method implementation for scenarios where same
+                       // code is used from multiple blocks, e.g. field initializers
+                       //
+                       if (method == null) {
+                               //
+                               // Delay an anonymous method definition to avoid emitting unused code
+                               // for unreachable blocks or expression trees
+                               //
+                               method = DoCreateMethodHost (ec);
+                               method.Define ();
+                       }
+
+                       bool is_static = (method.ModFlags & Modifiers.STATIC) != 0;
+                       if (is_static && am_cache == null) {
+                               //
+                               // Creates a field cache to store delegate instance if it's not generic
+                               //
+                               if (!method.MemberName.IsGeneric) {
+                                       TypeContainer parent = method.Parent.PartialContainer;
+                                       int id = parent.Fields == null ? 0 : parent.Fields.Count;
+                                       am_cache = new Field (parent, new TypeExpression (type, loc),
+                                               Modifiers.STATIC | Modifiers.PRIVATE | Modifiers.COMPILER_GENERATED,
+                                               new MemberName (CompilerGeneratedClass.MakeName (null, "f", "am$cache", id), loc), null);
+                                       am_cache.Define ();
+                                       parent.AddField (am_cache);
+                               } else {
+                                       // TODO: Implement caching of generated generic static methods
+                                       //
+                                       // Idea:
+                                       //
+                                       // Some extra class is needed to capture variable generic type
+                                       // arguments. Maybe we could re-use anonymous types, with a unique
+                                       // anonymous method id, but they are quite heavy.
+                                       //
+                                       // Consider : "() => typeof(T);"
+                                       //
+                                       // We need something like
+                                       // static class Wrap<Tn, Tm, DelegateType> {
+                                       //              public static DelegateType cache;
+                                       // }
+                                       //
+                                       // We then specialize local variable to capture all generic parameters
+                                       // and delegate type, e.g. "Wrap<Ta, Tb, DelegateTypeInst> cache;"
+                                       //
+                               }
+                       }
+
+                       ILGenerator ig = ec.ig;
+                       Label l_initialized = ig.DefineLabel ();
+
+                       if (am_cache != null) {
+                               ig.Emit (OpCodes.Ldsfld, am_cache.FieldBuilder);
+                               ig.Emit (OpCodes.Brtrue_S, l_initialized);
+                       }
+
                        //
                        // Load method delegate implementation
                        //
-                       if ((method.ModFlags & Modifiers.STATIC) != 0) {
-                               ec.ig.Emit (OpCodes.Ldnull);
-                       } else {
-                               Expression e = Storey.GetStoreyInstanceExpression (ec).Resolve (ec);
+
+                       if (is_static) {
+                               ig.Emit (OpCodes.Ldnull);
+                       } else if (storey != null) {
+                               Expression e = storey.GetStoreyInstanceExpression (ec).Resolve (ec);
                                if (e != null)
                                        e.Emit (ec);
+                       } else {
+                               ig.Emit (OpCodes.Ldarg_0);
                        }
 
                        MethodInfo delegate_method = method.MethodBuilder;
+                       if (storey != null && storey.MemberName.IsGeneric) {
+                               Type t = storey.Instance.Type;
+                               
+                               //
+                               // Mutate anonymous method instance type if we are in nested
+                               // hoisted generic anonymous method storey
+                               //
+                               if (ec.CurrentAnonymousMethod != null &&
+                                       ec.CurrentAnonymousMethod.Storey != null &&
+                                       ec.CurrentAnonymousMethod.Storey.IsGeneric) {
+                                       t = storey.GetGenericStorey ().MutateType (t);
+                               }
+
 #if GMCS_SOURCE
-                       if (Storey != null && Storey.IsGeneric)
-                               delegate_method = TypeBuilder.GetMethod (Storey.Instance.Type, delegate_method);
+                               delegate_method = TypeBuilder.GetMethod (t, delegate_method);
+#else
+                               throw new NotSupportedException ();
 #endif
-                       ec.ig.Emit (OpCodes.Ldftn, delegate_method);
+                       }
+
+                       ig.Emit (OpCodes.Ldftn, delegate_method);
 
                        ConstructorInfo constructor_method = Delegate.GetConstructor (ec.ContainerType, type);
 #if MS_COMPATIBLE
             if (type.IsGenericType && type is TypeBuilder)
                 constructor_method = TypeBuilder.GetConstructor (type, constructor_method);
 #endif
-                       ec.ig.Emit (OpCodes.Newobj, constructor_method);
+                       ig.Emit (OpCodes.Newobj, constructor_method);
+
+                       if (am_cache != null) {
+                               ig.Emit (OpCodes.Stsfld, am_cache.FieldBuilder);
+                               ig.MarkLabel (l_initialized);
+                               ig.Emit (OpCodes.Ldsfld, am_cache.FieldBuilder);
+                       }
                }
 
                //
@@ -1352,34 +1573,14 @@ namespace Mono.CSharp {
                AnonymousMethodStorey FindBestMethodStorey ()
                {
                        //
-                       // When no storey reference exists, use the nearest block which has
-                       // a storey
+                       // Use the nearest parent block which has a storey
                        //
-                       if (referenced_storeys == null) {
-                               for (Block b = Block.Parent; b != null; b = b.Parent) {
-                                       AnonymousMethodStorey s = b.Explicit.AnonymousMethodStorey;
-                                       if (s != null)
-                                               return s;
-                               }
-                                               
-                               return null;
-                       }
-
-                       //
-                       // We have storey reference, emit method in referenced storey
-                       //
-                       if (referenced_storeys.Count == 1) {
-                               // TODO: Remove reference when appropriate
-                               return (AnonymousMethodStorey) referenced_storeys [0];
-                       }
-
                        for (Block b = Block.Parent; b != null; b = b.Parent) {
-                               foreach (AnonymousMethodStorey storey in referenced_storeys) {
-                                       if (storey.OriginalSourceBlock == b)
-                                               return storey;
-                               }
+                               AnonymousMethodStorey s = b.Explicit.AnonymousMethodStorey;
+                               if (s != null)
+                                       return s;
                        }
-
+                                       
                        return null;
                }
 
@@ -1388,12 +1589,9 @@ namespace Mono.CSharp {
                        return TypeManager.CSharpName (type);
                }
 
-               public static void Error_AddressOfCapturedVar (string name, Location loc)
+               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
-                       Report.Error (1686, loc,
-                                     "Local variable `{0}' or its members cannot have their " +
-                                     "address taken and be used inside an anonymous method block",
-                                     name);
+                       type = storey.MutateType (type);
                }
 
                public static void Reset ()
@@ -1407,6 +1605,20 @@ namespace Mono.CSharp {
        //
        public class AnonymousTypeClass : CompilerGeneratedClass
        {
+               sealed class AnonymousParameters : ParametersCompiled
+               {
+                       public AnonymousParameters (params Parameter[] parameters)
+                               : base (parameters)
+                       {
+                       }
+
+                       protected override void ErrorDuplicateName (Parameter p)
+                       {
+                               Report.Error (833, p.Location, "`{0}': An anonymous type cannot have multiple properties with the same name",
+                                       p.Name);
+                       }
+               }
+
                static int types_counter;
                public const string ClassNamePrefix = "<>__AnonType";
                public const string SignatureForError = "anonymous type";
@@ -1414,7 +1626,7 @@ namespace Mono.CSharp {
                readonly ArrayList parameters;
 
                private AnonymousTypeClass (DeclSpace parent, MemberName name, ArrayList parameters, Location loc)
-                       : base (parent, name, Modifiers.SEALED, loc)
+                       : base (parent, name, (RootContext.EvalMode ? Modifiers.PUBLIC : 0) | Modifiers.SEALED)
                {
                        this.parameters = parameters;
                }
@@ -1423,15 +1635,17 @@ namespace Mono.CSharp {
                {
                        if (RootContext.Version <= LanguageVersion.ISO_2)
                                Report.FeatureIsNotAvailable (loc, "anonymous types");
-                       
+
                        string name = ClassNamePrefix + types_counter++;
 
                        SimpleName [] t_args = new SimpleName [parameters.Count];
+                       TypeParameterName [] t_params = new TypeParameterName [parameters.Count];
                        Parameter [] ctor_params = new Parameter [parameters.Count];
                        for (int i = 0; i < parameters.Count; ++i) {
                                AnonymousTypeParameter p = (AnonymousTypeParameter) parameters [i];
 
                                t_args [i] = new SimpleName ("<" + p.Name + ">__T", p.Location);
+                               t_params [i] = new TypeParameterName (t_args [i].Name, null, p.Location);
                                ctor_params [i] = new Parameter (t_args [i], p.Name, 0, null, p.Location);
                        }
 
@@ -1440,14 +1654,13 @@ namespace Mono.CSharp {
                        // named upon properties names
                        //
                        AnonymousTypeClass a_type = new AnonymousTypeClass (parent.NamespaceEntry.SlaveDeclSpace,
-                               new MemberName (name, new TypeArguments (loc, t_args), loc), parameters, loc);
+                               new MemberName (name, new TypeArguments (t_params), loc), parameters, loc);
 
                        if (parameters.Count > 0)
                                a_type.SetParameterInfo (null);
 
-                       Constructor c = new Constructor (a_type, name, Modifiers.PUBLIC,
-                               new Parameters (ctor_params), null, loc);
-                       c.OptAttributes = a_type.GetDebuggerHiddenAttribute ();
+                       Constructor c = new Constructor (a_type, name, Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN,
+                               null, new AnonymousParameters (ctor_params), null, loc);
                        c.Block = new ToplevelBlock (c.Parameters, loc);
 
                        // 
@@ -1458,12 +1671,10 @@ namespace Mono.CSharp {
                                AnonymousTypeParameter p = (AnonymousTypeParameter) parameters [i];
 
                                Field f = new Field (a_type, t_args [i], Modifiers.PRIVATE | Modifiers.READONLY,
-                                       "<" + p.Name + ">", null, p.Location);
+                                       new MemberName ("<" + p.Name + ">", p.Location), null);
 
                                if (!a_type.AddField (f)) {
                                        error = true;
-                                       Report.Error (833, p.Location, "`{0}': An anonymous type cannot have multiple properties with the same name",
-                                               p.Name);
                                        continue;
                                }
 
@@ -1474,8 +1685,8 @@ namespace Mono.CSharp {
                                ToplevelBlock get_block = new ToplevelBlock (p.Location);
                                get_block.AddStatement (new Return (
                                        new MemberAccess (new This (p.Location), f.Name), p.Location));
-                               Accessor get_accessor = new Accessor (get_block, 0, null, p.Location);
-                               Property prop = new Property (a_type, t_args [i], Modifiers.PUBLIC, false,
+                               Accessor get_accessor = new Accessor (get_block, 0, null, null, p.Location);
+                               Property prop = new Property (a_type, t_args [i], Modifiers.PUBLIC,
                                        new MemberName (p.Name, p.Location), null, get_accessor, null, false);
                                a_type.AddProperty (prop);
                        }
@@ -1487,7 +1698,7 @@ namespace Mono.CSharp {
                        return a_type;
                }
                
-               public new static void Reset ()
+               public static void Reset ()
                {
                        types_counter = 0;
                }
@@ -1510,18 +1721,17 @@ namespace Mono.CSharp {
                        Location loc = Location;
 
                        Method equals = new Method (this, null, TypeManager.system_boolean_expr,
-                               Modifiers.PUBLIC | Modifiers.OVERRIDE, false, new MemberName ("Equals", loc),
-                               new Parameters (new Parameter (TypeManager.system_object_expr, "obj", 0, null, loc)),
-                               GetDebuggerHiddenAttribute ());
+                               Modifiers.PUBLIC | Modifiers.OVERRIDE | Modifiers.DEBUGGER_HIDDEN, new MemberName ("Equals", loc),
+                               Mono.CSharp.ParametersCompiled.CreateFullyResolved (new Parameter (null, "obj", 0, null, loc), TypeManager.object_type), null);
 
                        Method tostring = new Method (this, null, TypeManager.system_string_expr,
-                               Modifiers.PUBLIC | Modifiers.OVERRIDE, false, new MemberName ("ToString", loc),
-                               Mono.CSharp.Parameters.EmptyReadOnlyParameters, GetDebuggerHiddenAttribute ());
+                               Modifiers.PUBLIC | Modifiers.OVERRIDE | Modifiers.DEBUGGER_HIDDEN, new MemberName ("ToString", loc),
+                               Mono.CSharp.ParametersCompiled.EmptyReadOnlyParameters, null);
 
                        ToplevelBlock equals_block = new ToplevelBlock (equals.Parameters, loc);
                        TypeExpr current_type;
                        if (IsGeneric)
-                               current_type = new ConstructedType (TypeBuilder, TypeParameters, loc);
+                               current_type = new GenericTypeExpr (this, loc);
                        else
                                current_type = new TypeExpression (TypeBuilder, loc);
 
@@ -1529,10 +1739,10 @@ namespace Mono.CSharp {
                        LocalVariableReference other_variable = new LocalVariableReference (equals_block, "other", loc);
 
                        MemberAccess system_collections_generic = new MemberAccess (new MemberAccess (
-                               new SimpleName ("System", loc), "Collections", loc), "Generic", loc);
+                               new QualifiedAliasMember ("global", "System", loc), "Collections", loc), "Generic", loc);
 
                        Expression rs_equals = null;
-                       Expression string_concat = new StringConstant ("<empty type>", loc);
+                       Expression string_concat = new StringConstant ("{", loc);
                        Expression rs_hashcode = new IntConstant (-2128831035, loc);
                        for (int i = 0; i < parameters.Count; ++i) {
                                AnonymousTypeParameter p = (AnonymousTypeParameter) parameters [i];
@@ -1540,7 +1750,7 @@ namespace Mono.CSharp {
 
                                MemberAccess equality_comparer = new MemberAccess (new MemberAccess (
                                        system_collections_generic, "EqualityComparer",
-                                               new TypeArguments (loc, new SimpleName (TypeParameters [i].Name, loc)), loc),
+                                               new TypeArguments (new SimpleName (TypeParameters [i].Name, loc)), loc),
                                                "Default", loc);
 
                                ArrayList arguments_equal = new ArrayList (2);
@@ -1564,13 +1774,15 @@ namespace Mono.CSharp {
                                        new MemberAccess (new This (f.Location), f.Name), new NullLiteral (loc)),
                                        new Invocation (new MemberAccess (
                                                new MemberAccess (new This (f.Location), f.Name), "ToString"), null),
-                                       new StringConstant ("<null>", loc));
+                                       new StringConstant (string.Empty, loc));
 
                                if (rs_equals == null) {
                                        rs_equals = field_equal;
                                        string_concat = new Binary (Binary.Operator.Addition,
-                                               new StringConstant (p.Name + " = ", loc),
-                                               field_to_string);
+                                               string_concat,
+                                               new Binary (Binary.Operator.Addition,
+                                                       new StringConstant (" " + p.Name + " = ", loc),
+                                                       field_to_string));
                                        continue;
                                }
 
@@ -1586,6 +1798,10 @@ namespace Mono.CSharp {
                                rs_equals = new Binary (Binary.Operator.LogicalAnd, rs_equals, field_equal);
                        }
 
+                       string_concat = new Binary (Binary.Operator.Addition,
+                               string_concat,
+                               new StringConstant (" }", loc));
+
                        //
                        // Equals (object obj) override
                        //
@@ -1600,15 +1816,16 @@ namespace Mono.CSharp {
                        equals_block.AddStatement (new Return (equals_test, loc));
 
                        equals.Block = equals_block;
-                       equals.ResolveMembers ();
+                       equals.Define ();
                        AddMethod (equals);
 
                        //
                        // GetHashCode () override
                        //
                        Method hashcode = new Method (this, null, TypeManager.system_int32_expr,
-                               Modifiers.PUBLIC | Modifiers.OVERRIDE, false, new MemberName ("GetHashCode", loc),
-                               Mono.CSharp.Parameters.EmptyReadOnlyParameters, GetDebuggerHiddenAttribute ());
+                               Modifiers.PUBLIC | Modifiers.OVERRIDE | Modifiers.DEBUGGER_HIDDEN,
+                               new MemberName ("GetHashCode", loc),
+                               Mono.CSharp.ParametersCompiled.EmptyReadOnlyParameters, null);
 
                        //
                        // Modified FNV with good avalanche behavior and uniform
@@ -1624,7 +1841,10 @@ namespace Mono.CSharp {
                        // hash ^= hash >> 17;
                        // hash += hash << 5;
 
-                       ToplevelBlock hashcode_block = new ToplevelBlock (loc);
+                       ToplevelBlock hashcode_top = new ToplevelBlock (loc);
+                       Block hashcode_block = new Block (hashcode_top);
+                       hashcode_top.AddStatement (new Unchecked (hashcode_block));
+
                        hashcode_block.AddVariable (TypeManager.system_int32_expr, "hash", loc);
                        LocalVariableReference hash_variable = new LocalVariableReference (hashcode_block, "hash", loc);
                        hashcode_block.AddStatement (new StatementExpression (
@@ -1647,8 +1867,8 @@ namespace Mono.CSharp {
                                        new Binary (Binary.Operator.LeftShift, hash_variable, new IntConstant (5, loc)))));
 
                        hashcode_block.AddStatement (new Return (hash_variable, loc));
-                       hashcode.Block = hashcode_block;
-                       hashcode.ResolveMembers ();
+                       hashcode.Block = hashcode_top;
+                       hashcode.Define ();
                        AddMethod (hashcode);
 
                        //
@@ -1658,21 +1878,17 @@ namespace Mono.CSharp {
                        ToplevelBlock tostring_block = new ToplevelBlock (loc);
                        tostring_block.AddStatement (new Return (string_concat, loc));
                        tostring.Block = tostring_block;
-                       tostring.ResolveMembers ();
+                       tostring.Define ();
                        AddMethod (tostring);
                }
 
-               public override bool DefineMembers ()
+               public override bool Define ()
                {
-                       DefineOverrides ();
-
-                       return base.DefineMembers ();
-               }
+                       if (!base.Define ())
+                               return false;
 
-               Attributes GetDebuggerHiddenAttribute ()
-               {
-                       return new Attributes (new Attribute (null, null,
-                               "System.Diagnostics.DebuggerHiddenAttribute", null, Location, false));
+                       DefineOverrides ();
+                       return true;
                }
 
                public override string GetSignatureForError ()