2009-06-22 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / class.cs
index bee17eeb1b50fa1e9025cd933c769c82184f143f..1f84bc436316f89c571a9e33ff6a2018871363a5 100644 (file)
@@ -229,6 +229,7 @@ namespace Mono.CSharp {
                TypeExpr base_type;
                TypeExpr[] iface_exprs;
                Type GenericType;
+               GenericTypeParameterBuilder[] nested_gen_params;
 
                protected ArrayList type_bases;
 
@@ -406,7 +407,7 @@ namespace Mono.CSharp {
 
                }
                
-               public void AddMethod (Method method)
+               public void AddMethod (MethodOrOperator method)
                {
                        if (!AddToContainer (method, method.MemberName.Basename))
                                return;
@@ -544,16 +545,16 @@ namespace Mono.CSharp {
                        compiler_generated.Add (c);
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
-                       if (a.Type == TypeManager.default_member_type) {
+                       if (a.Type == pa.DefaultMember) {
                                if (Indexers != null) {
                                        Report.Error (646, a.Location, "Cannot specify the `DefaultMember' attribute on type containing an indexer");
                                        return;
                                }
                        }
                        
-                       base.ApplyAttributeBuilder (a, cb);
+                       base.ApplyAttributeBuilder (a, cb, pa);
                } 
 
                public override AttributeTargets AttributeTargets {
@@ -642,10 +643,10 @@ namespace Mono.CSharp {
 
                public bool IsComImport {
                        get {
-                               if (OptAttributes == null || TypeManager.comimport_attr_type == null)
+                               if (OptAttributes == null)
                                        return false;
 
-                               return OptAttributes.Contains (TypeManager.comimport_attr_type);
+                               return OptAttributes.Contains (PredefinedAttributes.Get.ComImport);
                        }
                }
 
@@ -837,7 +838,7 @@ namespace Mono.CSharp {
                                                }
                                        }
 
-                                       if (Kind == Kind.Interface && !fne_resolved.AsAccessible (this)) {
+                                       if (Kind == Kind.Interface && !IsAccessibleAs (fne_resolved.Type)) {
                                                Report.Error (61, fne.Location,
                                                        "Inconsistent accessibility: base interface `{0}' is less accessible than interface `{1}'",
                                                        fne_resolved.GetSignatureForError (), GetSignatureForError ());
@@ -953,7 +954,7 @@ namespace Mono.CSharp {
                                                return false;
                                        }
 
-                                       ModuleBuilder builder = CodeGen.Module.Builder;
+                                       ModuleBuilder builder = Module.Builder;
                                        TypeBuilder = builder.DefineType (
                                                Name, TypeAttr, default_parent, type_size);
                                } else {
@@ -969,19 +970,27 @@ namespace Mono.CSharp {
 
                        TypeManager.AddUserType (this);
 
-#if GMCS_SOURCE
                        if (IsGeneric) {
                                string[] param_names = new string [TypeParameters.Length];
                                for (int i = 0; i < TypeParameters.Length; i++)
                                        param_names [i] = TypeParameters [i].Name;
 
+#if GMCS_SOURCE
                                GenericTypeParameterBuilder[] gen_params = TypeBuilder.DefineGenericParameters (param_names);
 
                                int offset = CountTypeParameters - CurrentTypeParameters.Length;
+                               if (offset > 0) {
+                                       nested_gen_params = new GenericTypeParameterBuilder [offset];
+                                       Array.Copy (gen_params, nested_gen_params, offset);
+                               }
+
                                for (int i = offset; i < gen_params.Length; i++)
                                        CurrentTypeParameters [i - offset].Define (gen_params [i]);
-                       }
+#else
+                               nested_gen_params = null;
+                               throw new NotSupportedException ();
 #endif
+                       }
 
                        return true;
                }
@@ -1145,12 +1154,6 @@ namespace Mono.CSharp {
 
                protected virtual bool DoResolveType ()
                {
-                       if ((base_type != null) &&
-                           (base_type.ResolveAsTypeTerminal (this, false) == null)) {
-                               error = true;
-                               return false;
-                       }
-
                        if (!IsGeneric)
                                return true;
 
@@ -1171,10 +1174,19 @@ namespace Mono.CSharp {
                                        UpdateTypeParameterConstraints (part);
                        }
 
-                       foreach (TypeParameter type_param in TypeParameters) {
-                               if (!type_param.DefineType (this)) {
-                                       error = true;
-                                       return false;
+                       for (int i = 0; i < TypeParameters.Length; ++i) {
+                               //
+                               // FIXME: Same should be done for delegates
+                               // TODO: Quite ugly way how to propagate constraints to
+                               // nested types
+                               //
+                               if (nested_gen_params != null && i < nested_gen_params.Length) {
+                                       TypeParameters [i].SetConstraints (nested_gen_params [i]);
+                               } else {
+                                       if (!TypeParameters [i].DefineType (this)) {
+                                               error = true;
+                                               return false;
+                                       }
                                }
                        }
 
@@ -1276,8 +1288,13 @@ namespace Mono.CSharp {
                                                        oa, iface.GetSignatureForError (), Location);
 
                                        GenericTypeExpr ct = iface as GenericTypeExpr;
-                                       if ((ct != null) && !ct.CheckConstraints (this))
-                                               return false;
+                                       if (ct != null) {
+                                               // TODO: passing `this' is wrong, should be base type iface instead
+                                               TypeManager.CheckTypeVariance (ct.Type, Variance.Covariant, this);
+
+                                               if (!ct.CheckConstraints (this))
+                                                       return false;
+                                       }
                                }
                        }
 
@@ -1290,6 +1307,10 @@ namespace Mono.CSharp {
                                if ((ct != null) && !ct.CheckConstraints (this))
                                        return false;
                                
+                               TypeContainer baseContainer = TypeManager.LookupTypeContainer(base_type.Type);
+                               if (baseContainer != null)
+                                       baseContainer.Define();                         
+                               
                                member_cache = new MemberCache (base_type.Type, this);
                        } else if (Kind == Kind.Interface) {
                                member_cache = new MemberCache (null, this);
@@ -1335,7 +1356,7 @@ namespace Mono.CSharp {
 
                                if (requires_delayed_unmanagedtype_check) {
                                        requires_delayed_unmanagedtype_check = false;
-                                       foreach (Field f in fields) {
+                                       foreach (FieldBase f in fields) {
                                                if (f.MemberType != null && f.MemberType.IsPointer)
                                                        TypeManager.VerifyUnManaged (f.MemberType, f.Location);
                                        }
@@ -1361,15 +1382,13 @@ namespace Mono.CSharp {
                                GenericType = CurrentType;
                        }
 
-#if GMCS_SOURCE
                        //
                        // FIXME: This hack is needed because member cache does not work
                        // with generic types, we rely on runtime to inflate dynamic types.
                        // TODO: This hack requires member cache refactoring to be removed
                        //
-                       if (TypeBuilder.IsGenericType)
+                       if (TypeManager.IsGenericType (TypeBuilder))
                                member_cache = new MemberCache (this);
-#endif                 
 
                        return true;
                }
@@ -1421,23 +1440,12 @@ namespace Mono.CSharp {
                        if (!seen_normal_indexers)
                                return;
 
-                       if (TypeManager.default_member_ctor == null) {
-                               if (TypeManager.default_member_type == null) {
-                                       TypeManager.default_member_type = TypeManager.CoreLookupType (
-                                               "System.Reflection", "DefaultMemberAttribute", Kind.Class, true);
-
-                                       if (TypeManager.default_member_type == null)
-                                               return;
-                               }
-
-                               TypeManager.default_member_ctor = TypeManager.GetPredefinedConstructor (
-                                       TypeManager.default_member_type, Location, TypeManager.string_type);
-
-                               if (TypeManager.default_member_ctor == null)
-                                       return;
-                       }
+                       PredefinedAttribute pa = PredefinedAttributes.Get.DefaultMember;
+                       if (pa.Constructor == null &&
+                               !pa.ResolveConstructor (Location, TypeManager.string_type))
+                               return;
 
-                       CustomAttributeBuilder cb = new CustomAttributeBuilder (TypeManager.default_member_ctor, new string [] { IndexerName });
+                       CustomAttributeBuilder cb = new CustomAttributeBuilder (pa.Constructor, new string [] { IndexerName });
                        TypeBuilder.SetCustomAttribute (cb);
                }
 
@@ -1720,7 +1728,7 @@ namespace Mono.CSharp {
                                if (methods != null) {
                                        int len = methods.Count;
                                        for (int i = 0; i < len; i++) {
-                                               Method m = (Method) methods [i];
+                                               MethodOrOperator m = (MethodOrOperator) methods [i];
                                                
                                                if ((m.ModFlags & modflags) == 0)
                                                        continue;
@@ -2142,7 +2150,7 @@ namespace Mono.CSharp {
 
                        if (methods != null) {
                                for (int i = 0; i < methods.Count; ++i)
-                                       ((Method) methods [i]).Emit ();
+                                       ((MethodOrOperator) methods [i]).Emit ();
                        }
                        
                        if (fields != null)
@@ -2203,6 +2211,7 @@ namespace Mono.CSharp {
                                foreach (CompilerGeneratedClass c in compiler_generated)
                                        c.CloseType ();
                        
+                       PartialContainer = null;
                        types = null;
                        properties = null;
                        delegates = null;
@@ -2517,7 +2526,7 @@ namespace Mono.CSharp {
 
                protected override bool AddToContainer (MemberCore symbol, string name)
                {
-                       if (name == MemberName.Name && symbol.MemberName.Left == null) {
+                       if (name == MemberName.Name) {
                                if (symbol is TypeParameter) {
                                        Report.Error (694, symbol.Location,
                                                "Type parameter `{0}' has same name as containing type, or method",
@@ -2525,10 +2534,13 @@ namespace Mono.CSharp {
                                        return false;
                                }
 
-                               Report.SymbolRelatedToPreviousError (this);
-                               Report.Error (542, symbol.Location, "`{0}': member names cannot be the same as their enclosing type",
-                                       symbol.GetSignatureForError ());
-                               return false;
+                               InterfaceMemberBase imb = symbol as InterfaceMemberBase;
+                               if (imb == null || !imb.IsExplicitImpl) {
+                                       Report.SymbolRelatedToPreviousError (this);
+                                       Report.Error (542, symbol.Location, "`{0}': member names cannot be the same as their enclosing type",
+                                               symbol.GetSignatureForError ());
+                                       return false;
+                               }
                        }
 
                        return base.AddToContainer (symbol, name);
@@ -2548,7 +2560,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
                        if (a.IsValidSecurityAttribute ()) {
                                if (declarative_security == null)
@@ -2558,14 +2570,14 @@ namespace Mono.CSharp {
                                return;
                        }
 
-                       if (a.Type == TypeManager.struct_layout_attribute_type) {
+                       if (a.Type == pa.StructLayout) {
                                PartialContainer.HasStructLayout = true;
 
                                if (a.GetLayoutKindValue () == LayoutKind.Explicit)
                                        PartialContainer.HasExplicitLayout = true;
                        }
 
-                       base.ApplyAttributeBuilder (a, cb);
+                       base.ApplyAttributeBuilder (a, cb, pa);
                }
 
                /// <summary>
@@ -2585,12 +2597,12 @@ namespace Mono.CSharp {
                        }
 
                        Constructor c = new Constructor (this, MemberName.Name, mods,
-                               null, Parameters.EmptyReadOnlyParameters,
+                               null, ParametersCompiled.EmptyReadOnlyParameters,
                                new GeneratedBaseInitializer (Location),
                                Location);
                        
                        AddConstructor (c);
-                       c.Block = new ToplevelBlock (Parameters.EmptyReadOnlyParameters, Location);
+                       c.Block = new ToplevelBlock (ParametersCompiled.EmptyReadOnlyParameters, Location);
                }
 
                public override bool Define ()
@@ -2672,27 +2684,26 @@ namespace Mono.CSharp {
                        base.AddBasesForPart (part, bases);
                }
 
-               public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
-                       if (a.Type == TypeManager.attribute_usage_type) {
+                       if (a.Type == pa.AttributeUsage) {
                                if (!TypeManager.IsAttributeType (BaseType) &&
                                        TypeBuilder.FullName != "System.Attribute") {
                                        Report.Error (641, a.Location, "Attribute `{0}' is only valid on classes derived from System.Attribute", a.GetSignatureForError ());
                                }
                        }
 
-                       if (a.Type == TypeManager.conditional_attribute_type && !TypeManager.IsAttributeType (BaseType)) {
+                       if (a.Type == pa.Conditional && !TypeManager.IsAttributeType (BaseType)) {
                                Report.Error (1689, a.Location, "Attribute `System.Diagnostics.ConditionalAttribute' is only valid on methods or attribute classes");
                                return;
                        }
 
-                       if (a.Type == TypeManager.comimport_attr_type && TypeManager.guid_attr_type != null &&
-                               !attributes.Contains (TypeManager.guid_attr_type)) {
-                                       a.Error_MissingGuidAttribute ();
-                                       return;
+                       if (a.Type == pa.ComImport && !attributes.Contains (pa.Guid)) {
+                               a.Error_MissingGuidAttribute ();
+                               return;
                        }
 
-                       if (a.Type == TypeManager.extension_attribute_type) {
+                       if (a.Type == pa.Extension) {
                                a.Error_MisusedExtensionAttribute ();
                                return;
                        }
@@ -2700,7 +2711,7 @@ namespace Mono.CSharp {
                        if (AttributeTester.IsAttributeExcluded (a.Type, Location))
                                return;
 
-                       base.ApplyAttributeBuilder (a, cb);
+                       base.ApplyAttributeBuilder (a, cb, pa);
                }
 
                public override AttributeTargets AttributeTargets {
@@ -2780,10 +2791,8 @@ namespace Mono.CSharp {
                {
                        base.Emit ();
 
-#if GMCS_SOURCE
                        if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
-                               TypeBuilder.SetCustomAttribute (TypeManager.extension_attribute_attr);
-#endif                 
+                               PredefinedAttributes.Get.Extension.EmitAttribute (TypeBuilder);
                }
 
                protected override TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
@@ -2816,7 +2825,7 @@ namespace Mono.CSharp {
                                                Report.Error (709, Location, "`{0}': Cannot derive from static class `{1}'",
                                                        GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
                                        } else {
-                                               Report.Error (509, Location, "`{0}': cannot derive from sealed class `{1}'",
+                                               Report.Error (509, Location, "`{0}': cannot derive from sealed type `{1}'",
                                                        GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
                                        }
                                        return ifaces;
@@ -2828,7 +2837,7 @@ namespace Mono.CSharp {
                                        return ifaces;
                                }
 
-                               if (!base_class.AsAccessible (this)) {
+                               if (!IsAccessibleAs (base_class.Type)) {
                                        Report.SymbolRelatedToPreviousError (base_class.Type);
                                        Report.Error (60, Location, "Inconsistent accessibility: base class `{0}' is less accessible than class `{1}'", 
                                                TypeManager.CSharpName (base_class.Type), GetSignatureForError ());
@@ -2864,11 +2873,7 @@ namespace Mono.CSharp {
                        if (OptAttributes == null)
                                return false;
 
-                       if (TypeManager.conditional_attribute_type == null)
-                               return false;
-
-                       Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type);
-
+                       Attribute[] attrs = OptAttributes.SearchMulti (PredefinedAttributes.Get.Conditional);
                        if (attrs == null)
                                return false;
 
@@ -2903,6 +2908,9 @@ namespace Mono.CSharp {
        }
 
        public sealed class Struct : ClassOrStruct {
+
+               bool is_unmanaged, has_unmanaged_check_done;
+
                // <summary>
                //   Modifiers allowed in a struct declaration
                // </summary>
@@ -2930,16 +2938,16 @@ namespace Mono.CSharp {
                        this.ModFlags |= Modifiers.SEALED;
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
-                       base.ApplyAttributeBuilder (a, cb);
+                       base.ApplyAttributeBuilder (a, cb, pa);
 
                        //
                        // When struct constains fixed fixed and struct layout has explicitly
                        // set CharSet, its value has to be propagated to compiler generated
                        // fixed field types
                        //
-                       if (a.Type == TypeManager.struct_layout_attribute_type && Fields != null && a.HasField ("CharSet")) {
+                       if (a.Type == pa.StructLayout && Fields != null && a.HasField ("CharSet")) {
                                for (int i = 0; i < Fields.Count; ++i) {
                                        FixedField ff = Fields [i] as FixedField;
                                        if (ff != null)
@@ -2962,6 +2970,11 @@ namespace Mono.CSharp {
                        if (requires_delayed_unmanagedtype_check)
                                return true;
 
+                       if (has_unmanaged_check_done)
+                               return is_unmanaged;
+
+                       has_unmanaged_check_done = true;
+
                        foreach (FieldBase f in fields) {
                                if ((f.ModFlags & Modifiers.STATIC) != 0)
                                        continue;
@@ -2970,6 +2983,7 @@ namespace Mono.CSharp {
                                // struct S { S* s; }
                                Type mt = f.MemberType;
                                if (mt == null) {
+                                       has_unmanaged_check_done = false;
                                        requires_delayed_unmanagedtype_check = true;
                                        return true;
                                }
@@ -2986,6 +3000,7 @@ namespace Mono.CSharp {
                                return false;
                        }
 
+                       is_unmanaged = true;
                        return true;
                }
 
@@ -3016,8 +3031,7 @@ namespace Mono.CSharp {
                        get {
                                const TypeAttributes DefaultTypeAttributes =
                                        TypeAttributes.SequentialLayout |
-                                       TypeAttributes.Sealed |
-                                       TypeAttributes.BeforeFieldInit;
+                                       TypeAttributes.Sealed;
 
                                return base.TypeAttr | DefaultTypeAttributes;
                        }
@@ -3065,14 +3079,14 @@ namespace Mono.CSharp {
                        this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, name.Location);
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
-                       if (a.Type == TypeManager.comimport_attr_type && TypeManager.guid_attr_type != null &&
-                               !attributes.Contains (TypeManager.guid_attr_type)) {
-                                       a.Error_MissingGuidAttribute ();
-                                       return;
+                       if (a.Type == pa.ComImport && !attributes.Contains (pa.Guid)) {
+                               a.Error_MissingGuidAttribute ();
+                               return;
                        }
-                       base.ApplyAttributeBuilder (a, cb);
+
+                       base.ApplyAttributeBuilder (a, cb, pa);
                }
 
 
@@ -3141,12 +3155,12 @@ namespace Mono.CSharp {
 
        public abstract class MethodCore : InterfaceMemberBase
        {
-               public readonly Parameters Parameters;
+               public readonly ParametersCompiled Parameters;
                protected ToplevelBlock block;
 
                public MethodCore (DeclSpace parent, GenericMethod generic,
                        FullNamedExpression type, int mod, int allowed_mod,
-                       MemberName name, Attributes attrs, Parameters parameters)
+                       MemberName name, Attributes attrs, ParametersCompiled parameters)
                        : base (parent, generic, type, mod, allowed_mod, name, attrs)
                {
                        Parameters = parameters;
@@ -3161,8 +3175,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               public Parameters ParameterInfo
-               {
+               public ParametersCompiled ParameterInfo {
                        get {
                                return Parameters;
                        }
@@ -3178,6 +3191,19 @@ namespace Mono.CSharp {
                        }
                }
 
+               public CallingConventions CallingConventions {
+                       get {
+                               CallingConventions cc = Parameters.CallingConvention;
+                               if (!IsInterface)
+                                       if ((ModFlags & Modifiers.STATIC) == 0)
+                                               cc |= CallingConventions.HasThis;
+
+                               // FIXME: How is `ExplicitThis' used in C#?
+                       
+                               return cc;
+                       }
+               }
+
                protected override bool CheckBase ()
                {
                        // Check whether arguments were correct.
@@ -3313,13 +3339,13 @@ namespace Mono.CSharp {
                                if ((ModFlags & Modifiers.OVERRIDE) != 0) {
                                        ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (base_method);
                                        if (oa != null) {
-                                               if (OptAttributes == null || TypeManager.obsolete_attribute_type == null || !OptAttributes.Contains (TypeManager.obsolete_attribute_type)) {
+                                               if (OptAttributes == null || !OptAttributes.Contains (PredefinedAttributes.Get.Obsolete)) {
                                                        Report.SymbolRelatedToPreviousError (base_method);
                                                                Report.Warning (672, 1, Location, "Member `{0}' overrides obsolete member `{1}'. Add the Obsolete attribute to `{0}'",
                                                                        GetSignatureForError (), TypeManager.CSharpSignature (base_method));
                                                }
                                        } else {
-                                               if (OptAttributes != null && TypeManager.obsolete_attribute_type != null && OptAttributes.Contains (TypeManager.obsolete_attribute_type)) {
+                                               if (OptAttributes != null && OptAttributes.Contains (PredefinedAttributes.Get.Obsolete)) {
                                                        Report.SymbolRelatedToPreviousError (base_method);
                                                        Report.Warning (809, 1, Location, "Obsolete member `{0}' overrides non-obsolete member `{1}'",
                                                                GetSignatureForError (), TypeManager.CSharpSignature (base_method));
@@ -3368,7 +3394,7 @@ namespace Mono.CSharp {
                protected virtual bool CheckForDuplications ()
                {
                        return Parent.MemberCache.CheckExistingMembersOverloads (
-                               this, GetFullName (MemberName), Parameters.EmptyReadOnlyParameters);
+                               this, GetFullName (MemberName), ParametersCompiled.EmptyReadOnlyParameters);
                }
 
                //
@@ -3425,7 +3451,7 @@ namespace Mono.CSharp {
                        }
 
                        if ((ModFlags & Modifiers.NEW) == 0) {
-                               if ((ModFlags & Modifiers.OVERRIDE) == 0 && Name != "Finalize") {
+                               if ((ModFlags & Modifiers.OVERRIDE) == 0) {
                                        ModFlags |= Modifiers.NEW;
                                        Report.SymbolRelatedToPreviousError (base_method);
                                        if (!IsInterface && (base_method.IsVirtual || base_method.IsAbstract)) {
@@ -3525,7 +3551,7 @@ namespace Mono.CSharp {
                        return base.Define ();
                }
 
-               protected bool DefineParameters (Parameters parameters)
+               protected bool DefineParameters (ParametersCompiled parameters)
                {
                        IResolveContext rc = GenericMethod == null ? this : (IResolveContext)ds;
 
@@ -3560,11 +3586,16 @@ namespace Mono.CSharp {
                public override void Emit()
                {
                        // for extern static method must be specified either DllImport attribute or MethodImplAttribute.
-                       // We are more strict than Microsoft and report CS0626 as error
+                       // We are more strict than csc and report this as an error because SRE does not allow emit that
                        if ((ModFlags & Modifiers.EXTERN) != 0 && !is_external_implementation) {
-                               Report.Error (626, Location,
-                                       "`{0}' is marked as an external but has no DllImport attribute. Consider adding a DllImport attribute to specify the external implementation",
-                                       GetSignatureForError ());
+                               if (this is Constructor) {
+                                       Report.Error (824, Location,
+                                               "Constructor `{0}' is marked `external' but has no external implementation specified", GetSignatureForError ());
+                               } else {
+                                       Report.Error (626, Location,
+                                               "`{0}' is marked as an external but has no DllImport attribute. Consider adding a DllImport attribute to specify the external implementation",
+                                               GetSignatureForError ());
+                               }
                        }
 
                        base.Emit ();
@@ -3678,19 +3709,19 @@ namespace Mono.CSharp {
 
                protected MethodOrOperator (DeclSpace parent, GenericMethod generic, FullNamedExpression type, int mod,
                                int allowed_mod, MemberName name,
-                               Attributes attrs, Parameters parameters)
+                               Attributes attrs, ParametersCompiled parameters)
                        : base (parent, generic, type, mod, allowed_mod, name,
                                        attrs, parameters)
                {
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
                        if (a.Target == AttributeTargets.ReturnValue) {
                                if (return_attributes == null)
                                        return_attributes = new ReturnParameter (MethodBuilder, Location);
 
-                               return_attributes.ApplyAttributeBuilder (a, cb);
+                               return_attributes.ApplyAttributeBuilder (a, cb, pa);
                                return;
                        }
 
@@ -3698,7 +3729,7 @@ namespace Mono.CSharp {
                                is_external_implementation = true;
                        }
 
-                       if (a.Type == TypeManager.dllimport_type) {
+                       if (a.Type == pa.DllImport) {
                                const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
                                if ((ModFlags & extern_static) != extern_static) {
                                        Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
@@ -3789,10 +3820,8 @@ namespace Mono.CSharp {
                                        
                        MethodBuilder = MethodData.MethodBuilder;
 
-#if GMCS_SOURCE                                                
-                       if (MethodBuilder.IsGenericMethod)
+                       if (TypeManager.IsGenericMethod (MethodBuilder))
                                Parent.MemberCache.AddGenericMember (MethodBuilder, this);
-#endif                 
                        
                        Parent.MemberCache.AddMember (MethodBuilder, this);
 
@@ -3807,11 +3836,14 @@ namespace Mono.CSharp {
 
                        if ((ModFlags & Modifiers.PARTIAL) != 0) {
                                for (int i = 0; i < Parameters.Count; ++i) {
-                                       if (Parameters.FixedParameters[i].ModFlags == Parameter.Modifier.OUT) {
+                                       IParameterData p = Parameters.FixedParameters [i];
+                                       if (p.ModFlags == Parameter.Modifier.OUT) {
                                                Report.Error (752, Location, "`{0}': A partial method parameters cannot use `out' modifier",
                                                        GetSignatureForError ());
-                                               break;
                                        }
+
+                                       if (p.HasDefaultValue && IsPartialImplementation)
+                                               ((Parameter) p).Warning_UselessOptionalParameter ();
                                }
                        }
                }
@@ -3829,12 +3861,11 @@ namespace Mono.CSharp {
 
                public override void Emit ()
                {
-#if GMCS_SOURCE                        
                        if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
-                               MethodBuilder.SetCustomAttribute (TypeManager.GetCompilerGeneratedAttribute (Location));
+                               PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (MethodBuilder);
                        if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
-                               MethodBuilder.SetCustomAttribute (TypeManager.GetDebuggerHiddenAttribute (Location));
-#endif
+                               PredefinedAttributes.Get.DebuggerHidden.EmitAttribute (MethodBuilder);
+
                        if (OptAttributes != null)
                                OptAttributes.Emit ();
 
@@ -3844,7 +3875,13 @@ namespace Mono.CSharp {
                                }
                        }
 
+                       if (MethodData != null)
+                               MethodData.Emit (Parent);
+
                        base.Emit ();
+
+                       Block = null;
+                       MethodData = null;
                }
 
                protected void Error_ConditionalAttributeIsNotValid ()
@@ -3874,19 +3911,6 @@ namespace Mono.CSharp {
 
                #region IMethodData Members
 
-               public CallingConventions CallingConventions {
-                       get {
-                               CallingConventions cc = Parameters.CallingConvention;
-                               if (!IsInterface)
-                                       if ((ModFlags & Modifiers.STATIC) == 0)
-                                               cc |= CallingConventions.HasThis;
-
-                               // FIXME: How is `ExplicitThis' used in C#?
-                       
-                               return cc;
-                       }
-               }
-
                public Type ReturnType {
                        get {
                                return MemberType;
@@ -3899,21 +3923,6 @@ namespace Mono.CSharp {
                        }
                }
 
-               protected override bool CheckBase ()
-               {
-                       if (!base.CheckBase ())
-                               return false;
-
-                       // TODO: Destructor should derive from MethodCore
-                       if (base_method != null && (ModFlags & Modifiers.OVERRIDE) != 0 && Name == "Finalize" &&
-                               base_method.DeclaringType == TypeManager.object_type && !(this is Destructor)) {
-                               Report.Error (249, Location, "Do not override object.Finalize. Instead, provide a destructor");
-                               return false;
-                       }
-
-                       return true;
-               }
-
                /// <summary>
                /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
                /// </summary>
@@ -3927,10 +3936,7 @@ namespace Mono.CSharp {
                                if (OptAttributes == null)
                                        return false;
 
-                               if (TypeManager.conditional_attribute_type == null)
-                                       return false;
-
-                               Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type);
+                               Attribute[] attrs = OptAttributes.SearchMulti (PredefinedAttributes.Get.Conditional);
 
                                if (attrs == null)
                                        return false;
@@ -4058,7 +4064,7 @@ namespace Mono.CSharp {
 
                public Method (DeclSpace parent, GenericMethod generic,
                               FullNamedExpression return_type, int mod,
-                              MemberName name, Parameters parameters, Attributes attrs)
+                              MemberName name, ParametersCompiled parameters, Attributes attrs)
                        : base (parent, generic, return_type, mod,
                                parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
                                name, attrs, parameters)
@@ -4066,7 +4072,7 @@ namespace Mono.CSharp {
                }
 
                protected Method (DeclSpace parent, FullNamedExpression return_type, int mod, int amod,
-                                       MemberName name, Parameters parameters, Attributes attrs)
+                                       MemberName name, ParametersCompiled parameters, Attributes attrs)
                        : base (parent, null, return_type, mod, amod, name, attrs, parameters)
                {
                }
@@ -4076,11 +4082,11 @@ namespace Mono.CSharp {
                        return base.GetSignatureForError () + Parameters.GetSignatureForError ();
                }
 
-               void Error_DuplicateEntryPoint (MethodInfo b, Location location)
+               static void Error_DuplicateEntryPoint (Method b)
                {
-                       Report.Error (17, location,
+                       Report.Error (17, b.Location,
                                "Program `{0}' has more than one entry point defined: `{1}'",
-                               CodeGen.FileName, TypeManager.CSharpSignature(b));
+                               CodeGen.FileName, b.GetSignatureForError ());
                }
 
                bool IsEntryPoint ()
@@ -4101,9 +4107,9 @@ namespace Mono.CSharp {
                                        (Parameters[0].ModFlags & ~Parameter.Modifier.PARAMS) == Parameter.Modifier.NONE;
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
-                       if (a.Type == TypeManager.conditional_attribute_type) {
+                       if (a.Type == pa.Conditional) {
                                if (IsExplicitImpl) {
                                        Error_ConditionalAttributeIsNotValid ();
                                        return;
@@ -4139,12 +4145,12 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       if (a.Type == TypeManager.extension_attribute_type) {
+                       if (a.Type == pa.Extension) {
                                a.Error_MisusedExtensionAttribute ();
                                return;
                        }
 
-                       base.ApplyAttributeBuilder (a, cb);
+                       base.ApplyAttributeBuilder (a, cb, pa);
                }
 
                protected override bool CheckForDuplications ()
@@ -4173,11 +4179,28 @@ namespace Mono.CSharp {
                        return true;
                }
 
+               protected override bool CheckBase ()
+               {
+                       if (!base.CheckBase ())
+                               return false;
+
+                       if (base_method != null && (ModFlags & Modifiers.OVERRIDE) != 0 && Name == Destructor.MetadataName) {
+                               Report.Error (249, Location, "Do not override `{0}'. Use destructor syntax instead",
+                                       TypeManager.CSharpSignature (base_method));
+                       }
+
+                       return true;
+               }
+
                //
                // Creates the type
                //
                public override bool Define ()
                {
+                       if (type_name == TypeManager.system_void_expr && Parameters.IsEmpty && Name == Destructor.MetadataName) {
+                               Report.Warning (465, 1, Location, "Introducing `Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?");
+                       }
+
                        if (!base.Define ())
                                return false;
 
@@ -4186,11 +4209,6 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       if (ReturnType == TypeManager.void_type && Parameters.Count == 0 && 
-                               Name == "Finalize" && !(this is Destructor)) {
-                               Report.Warning (465, 1, Location, "Introducing a 'Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?");
-                       }
-
                        if (base_method != null && (ModFlags & Modifiers.NEW) == 0) {
                                if (Parameters.Count == 1 && ParameterTypes [0] == TypeManager.object_type && Name == "Equals")
                                        Parent.PartialContainer.Mark_HasEquals ();
@@ -4202,25 +4220,20 @@ namespace Mono.CSharp {
                                return true;
 
                        if (Parameters.HasExtensionMethodType) {
-                               if (Parent.IsStaticClass && !Parent.IsGeneric) {
+                               if (Parent.PartialContainer.IsStaticClass && !Parent.IsGeneric) {
                                        if (!Parent.IsTopLevel)
                                                Report.Error (1109, Location, "`{0}': Extension methods cannot be defined in a nested class",
                                                        GetSignatureForError ());
 
-                                       if (TypeManager.extension_attribute_type == null) {
+                                       PredefinedAttribute pa = PredefinedAttributes.Get.Extension;
+                                       if (!pa.IsDefined) {
                                                Report.Error (1110, Location,
                                                        "`{0}': Extension methods cannot be declared without a reference to System.Core.dll assembly. Add the assembly reference or remove `this' modifer from the first parameter",
                                                        GetSignatureForError ());
-                                       } else if (TypeManager.extension_attribute_attr == null) {
-                                               ConstructorInfo ci = TypeManager.GetPredefinedConstructor (
-                                                       TypeManager.extension_attribute_type, Location, System.Type.EmptyTypes);
-
-                                               if (ci != null)
-                                                       TypeManager.extension_attribute_attr = new CustomAttributeBuilder (ci, new object [0]);
                                        }
 
                                        ModFlags |= Modifiers.METHOD_EXTENSION;
-                                       Parent.ModFlags |= Modifiers.METHOD_EXTENSION;
+                                       Parent.PartialContainer.ModFlags |= Modifiers.METHOD_EXTENSION;
                                        CodeGen.Assembly.HasExtensionMethods = true;
                                } else {
                                        Report.Error (1106, Location, "`{0}': Extension methods must be defined in a non-generic static class",
@@ -4242,15 +4255,12 @@ namespace Mono.CSharp {
                                                        Report.Warning (402, 4, Location, "`{0}': an entry point cannot be generic or in a generic type",
                                                                GetSignatureForError ());
                                                } else {
-                                                       IMethodData md = TypeManager.GetMethod (MethodBuilder);
-                                                       md.SetMemberIsUsed ();
-
-                                                       RootContext.EntryPoint = MethodBuilder;
-                                                       RootContext.EntryPointLocation = Location;
+                                                       SetMemberIsUsed ();
+                                                       RootContext.EntryPoint = this;
                                                }
                                        } else {
-                                               Error_DuplicateEntryPoint (RootContext.EntryPoint, RootContext.EntryPointLocation);
-                                               Error_DuplicateEntryPoint (MethodBuilder, Location);
+                                               Error_DuplicateEntryPoint (RootContext.EntryPoint);
+                                               Error_DuplicateEntryPoint (this);
                                        }
                                } else {
                                        Report.Warning (28, 4, Location, "`{0}' has the wrong signature to be an entry point",
@@ -4282,16 +4292,10 @@ namespace Mono.CSharp {
                                        Report.Error (759, Location, "A partial method `{0}' implementation is missing a partial method declaration",
                                                GetSignatureForError ());
 
-                               MethodData.Emit (Parent);
                                base.Emit ();
                                
-#if GMCS_SOURCE                                
                                if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
-                                       MethodBuilder.SetCustomAttribute (TypeManager.extension_attribute_attr);
-#endif
-
-                               Block = null;
-                               MethodData = null;
+                                       PredefinedAttributes.Get.Extension.EmitAttribute (MethodBuilder);
                        } catch {
                                Console.WriteLine ("Internal compiler error at {0}: exception caught while emitting {1}",
                                                   Location, MethodBuilder);
@@ -4335,6 +4339,11 @@ namespace Mono.CSharp {
                {
                        caching_flags |= Flags.PartialDefinitionExists;
                        methodDefinition.MethodBuilder = MethodBuilder;
+
+                       for (int i = 0; i < methodDefinition.Parameters.Count; ++i ) {
+                               Parameters [i].DefaultValue = methodDefinition.Parameters [i].DefaultValue;
+                       }
+
                        if (methodDefinition.attributes == null)
                                return;
 
@@ -4350,7 +4359,7 @@ namespace Mono.CSharp {
                        if (!base.VerifyClsCompliance ())
                                return false;
 
-                       if (ParameterInfo.Count > 0) {
+                       if (!Parameters.IsEmpty) {
                                ArrayList al = (ArrayList)Parent.PartialContainer.MemberCache.Members [Name];
                                if (al.Count > 1)
                                        MemberCache.VerifyClsParameterConflict (al, this, MethodBuilder);
@@ -4396,7 +4405,7 @@ namespace Mono.CSharp {
                                        return true;
 
                                type = ec.ContainerType.BaseType;
-                               if (ec.ContainerType.IsValueType) {
+                               if (TypeManager.IsStruct (ec.ContainerType)) {
                                        Report.Error (522, loc,
                                                "`{0}': Struct constructors cannot call base constructors", TypeManager.CSharpSignature (caller_builder));
                                        return false;
@@ -4408,7 +4417,7 @@ namespace Mono.CSharp {
                                //
                                // struct D { public D (int a) : this () {}
                                //
-                               if (ec.ContainerType.IsValueType && argument_list == null)
+                               if (TypeManager.IsStruct (ec.ContainerType) && argument_list == null)
                                        return true;
                                
                                type = ec.ContainerType;
@@ -4506,7 +4515,7 @@ namespace Mono.CSharp {
                // The spec claims that static is not permitted, but
                // my very own code has static constructors.
                //
-               public Constructor (DeclSpace parent, string name, int mod, Attributes attrs, Parameters args,
+               public Constructor (DeclSpace parent, string name, int mod, Attributes attrs, ParametersCompiled args,
                                    ConstructorInitializer init, Location loc)
                        : base (parent, null, null, mod, AllowedModifiers,
                                new MemberName (name, loc), attrs, args)
@@ -4535,7 +4544,7 @@ namespace Mono.CSharp {
                                        (Initializer.Arguments == null);
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
                        if (a.IsValidSecurityAttribute ()) {
                                if (declarative_security == null) {
@@ -4652,7 +4661,7 @@ namespace Mono.CSharp {
                public override void Emit ()
                {
                        if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
-                               ConstructorBuilder.SetCustomAttribute (TypeManager.GetDebuggerHiddenAttribute (Location));
+                               PredefinedAttributes.Get.DebuggerHidden.EmitAttribute (ConstructorBuilder);
 
                        if (OptAttributes != null)
                                OptAttributes.Emit ();
@@ -4678,7 +4687,7 @@ namespace Mono.CSharp {
                                        ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
                                        block.AddThisVariable (Parent, Location);
 
-                               if (!block.ResolveMeta (ec, ParameterInfo))
+                               if (!block.ResolveMeta (ec, Parameters))
                                        block = null;
 
                                if (block != null && (ModFlags & Modifiers.STATIC) == 0){
@@ -4707,7 +4716,7 @@ namespace Mono.CSharp {
 
                        bool unreachable = false;
                        if (block != null) {
-                               if (!ec.ResolveTopBlock (null, block, ParameterInfo, this, out unreachable))
+                               if (!ec.ResolveTopBlock (null, block, Parameters, this, out unreachable))
                                        return;
 
                                ec.EmitMeta (block);
@@ -4753,8 +4762,8 @@ namespace Mono.CSharp {
                                return false;
                        }
                        
-                       if (ParameterInfo.Count > 0) {
-                               ArrayList al = (ArrayList)Parent.MemberCache.Members [".ctor"];
+                       if (!Parameters.IsEmpty) {
+                               ArrayList al = (ArrayList)Parent.MemberCache.Members [ConstructorInfo.ConstructorName];
                                if (al.Count > 2)
                                        MemberCache.VerifyClsParameterConflict (al, this, ConstructorBuilder);
  
@@ -4772,20 +4781,6 @@ namespace Mono.CSharp {
 
                #region IMethodData Members
 
-               public System.Reflection.CallingConventions CallingConventions {
-                       get {
-                               CallingConventions cc = Parameters.CallingConvention;
-
-                               if (Parent.PartialContainer.Kind == Kind.Class)
-                                       if ((ModFlags & Modifiers.STATIC) == 0)
-                                               cc |= CallingConventions.HasThis;
-
-                               // FIXME: How is `ExplicitThis' used in C#?
-                       
-                               return cc;
-                       }
-               }
-
                public MemberName MethodName {
                        get {
                                return MemberName;
@@ -4833,7 +4828,7 @@ namespace Mono.CSharp {
                MemberName MethodName { get; }
                Type ReturnType { get; }
                GenericMethod GenericMethod { get; }
-               Parameters ParameterInfo { get; }
+               ParametersCompiled ParameterInfo { get; }
 
                Attributes OptAttributes { get; }
                ToplevelBlock Block { get; set; }
@@ -4851,9 +4846,7 @@ namespace Mono.CSharp {
        // Encapsulates most of the Method's state
        //
        public class MethodData {
-#if GMCS_SOURCE
                static FieldInfo methodbuilder_attrs_field;
-#endif
                public readonly IMethodData method;
 
                public readonly GenericMethod GenericMethod;
@@ -5070,7 +5063,7 @@ namespace Mono.CSharp {
                /// <summary>
                /// Create the MethodBuilder for the method 
                /// </summary>
-               void DefineMethodBuilder (TypeContainer container, string method_name, Parameters param)
+               void DefineMethodBuilder (TypeContainer container, string method_name, ParametersCompiled param)
                {
                        if (builder == null) {
                                builder = container.TypeBuilder.DefineMethod (
@@ -5086,7 +5079,7 @@ namespace Mono.CSharp {
                        //
                        builder.SetParameters (param.GetEmitTypes ());
                        builder.SetReturnType (method.ReturnType);
-
+#endif
                        if (builder.Attributes != flags) {
                                try {
                                        if (methodbuilder_attrs_field == null)
@@ -5096,9 +5089,6 @@ namespace Mono.CSharp {
                                        Report.RuntimeMissingSupport (method.Location, "Generic method MethodAttributes");
                                }
                        }
-#else
-                       throw new InternalErrorException ();
-#endif
                }
 
                //
@@ -5128,87 +5118,80 @@ namespace Mono.CSharp {
 
                        SourceMethod source = SourceMethod.Create (parent, MethodBuilder, method.Block);
 
-                       //
-                       // Handle destructors specially
-                       //
-                       // FIXME: This code generates buggy code
-                       //
-                       if (member is Destructor)
-                               EmitDestructor (ec, block);
-                       else
-                               ec.EmitTopBlock (method, block);
+                       ec.EmitTopBlock (method, block);
 
                        if (source != null) {
                                method.EmitExtraSymbolInfo (source);
                                source.CloseMethod ();
                        }
                }
-
-               void EmitDestructor (EmitContext ec, ToplevelBlock block)
-               {
-                       ILGenerator ig = ec.ig;
-                       
-                       Label finish = ig.DefineLabel ();
-
-                       block.SetDestructor ();
-                       
-                       ig.BeginExceptionBlock ();
-                       ec.ReturnLabel = finish;
-                       ec.HasReturnLabel = true;
-                       ec.EmitTopBlock (method, block);
-                       
-                       // ig.MarkLabel (finish);
-                       ig.BeginFinallyBlock ();
-                       
-                       if (ec.ContainerType.BaseType != null) {
-                               Expression member_lookup = Expression.MemberLookup (
-                                       ec.ContainerType.BaseType, null, ec.ContainerType.BaseType,
-                                       "Finalize", MemberTypes.Method, Expression.AllBindingFlags, method.Location);
-
-                               if (member_lookup != null){
-                                       MethodGroupExpr base_destructor = ((MethodGroupExpr) member_lookup);
-                               
-                                       ig.Emit (OpCodes.Ldarg_0);
-                                       ig.Emit (OpCodes.Call, (MethodInfo) base_destructor.Methods [0]);
-                               }
-                       }
-                       
-                       ig.EndExceptionBlock ();
-                       //ig.MarkLabel (ec.ReturnLabel);
-                       ig.Emit (OpCodes.Ret);
-               }
        }
 
-       // TODO: Should derive from MethodCore
-       public class Destructor : Method
+       public class Destructor : MethodOrOperator
        {
                const int AllowedModifiers =
                        Modifiers.UNSAFE |
                        Modifiers.EXTERN;
 
-               static string[] attribute_targets = new string [] { "method" };
+               static readonly string[] attribute_targets = new string [] { "method" };
+
+               public static readonly string MetadataName = "Finalize";
 
-               public Destructor (DeclSpace parent, FullNamedExpression return_type, int mod,
-                                  string name, Parameters parameters, Attributes attrs,
-                                  Location l)
-                       : base (parent, return_type, mod, AllowedModifiers, new MemberName (name, l),
-                               parameters, attrs)
+               public Destructor (DeclSpace parent, int mod, ParametersCompiled parameters, Attributes attrs, Location l)
+                       : base (parent, null, TypeManager.system_void_expr, mod, AllowedModifiers,
+                               new MemberName (MetadataName, l), attrs, parameters)
                {
                        ModFlags &= ~Modifiers.PRIVATE;
-                       if (!RootContext.StdLib && parent.Name == "System.Object")
-                               ModFlags |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
-                       else
-                               ModFlags |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
+                       ModFlags |= Modifiers.PROTECTED;
                }
 
-               public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
-                       if (a.Type == TypeManager.conditional_attribute_type) {
+                       if (a.Type == pa.Conditional) {
                                Error_ConditionalAttributeIsNotValid ();
                                return;
                        }
 
-                       base.ApplyAttributeBuilder (a, cb);
+                       base.ApplyAttributeBuilder (a, cb, pa);
+               }
+
+               protected override bool CheckBase ()
+               {
+                       flags |= MethodAttributes.Virtual;
+
+                       if (!base.CheckBase ())
+                               return false;
+
+                       if (Parent.PartialContainer.BaseCache == null)
+                               return true;
+
+                       Type base_type = Parent.PartialContainer.BaseCache.Container.Type;
+                       if (base_type != null && Block != null) {
+                               MethodGroupExpr method_expr = Expression.MethodLookup (Parent.TypeBuilder, base_type, MetadataName, Location);
+                               if (method_expr == null)
+                                       throw new NotImplementedException ();
+
+                               method_expr.IsBase = true;
+                               method_expr.InstanceExpression = new CompilerGeneratedThis (Parent.TypeBuilder, Location);
+
+                               ToplevelBlock new_block = new ToplevelBlock (Block.StartLocation);
+                               new_block.EndLocation = Block.EndLocation;
+
+                               Block finaly_block = new ExplicitBlock (new_block, Location, Location);
+                               Block try_block = new Block (new_block, block);
+
+                               //
+                               // 0-size arguments to avoid CS0250 error
+                               // TODO: Should use AddScopeStatement or something else which emits correct
+                               // debugger scope
+                               //
+                               finaly_block.AddStatement (new StatementExpression (new Invocation (method_expr, new ArrayList (0))));
+                               new_block.AddStatement (new TryFinally (try_block, finaly_block, Location));
+
+                               block = new_block;
+                       }
+
+                       return true;
                }
 
                public override string GetSignatureForError ()
@@ -5216,6 +5199,11 @@ namespace Mono.CSharp {
                        return Parent.GetSignatureForError () + ".~" + Parent.MemberName.Name + "()";
                }
 
+               protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
+               {
+                       return null;
+               }
+
                public override string[] ValidAttributeTargets {
                        get {
                                return attribute_targets;
@@ -5309,6 +5297,8 @@ namespace Mono.CSharp {
                                                      "accessible than field `" + GetSignatureForError () + "'");
                                }
                        }
+
+                       TypeManager.CheckTypeVariance (MemberType, Variance.Covariant, this);
                }
 
                protected bool IsTypePermitted ()
@@ -5340,7 +5330,7 @@ namespace Mono.CSharp {
                        TypeExpr te = type_name.ResolveAsTypeTerminal (rc, false);
                        if (te == null)
                                return false;
-
+                       
                        //
                        // Replace original type name, error reporting can use fully resolved name
                        //
@@ -5381,9 +5371,9 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
-                       if (a.Type == TypeManager.field_offset_attribute_type) {
+                       if (a.Type == pa.FieldOffset) {
                                status |= Status.HAS_OFFSET;
 
                                if (!Parent.PartialContainer.HasExplicitLayout) {
@@ -5398,14 +5388,14 @@ namespace Mono.CSharp {
                        }
 
 #if NET_2_0
-                       if (a.Type == TypeManager.fixed_buffer_attr_type) {
+                       if (a.Type == pa.FixedBuffer) {
                                Report.Error (1716, Location, "Do not use 'System.Runtime.CompilerServices.FixedBuffer' attribute. Use the 'fixed' field modifier instead");
                                return;
                        }
 #endif
 
 #if !NET_2_0
-                       if (a.Type == TypeManager.marshal_as_attr_type) {
+                       if (a.Type == pa.MarshalAs) {
                                UnmanagedMarshal marshal = a.GetMarshal (this);
                                if (marshal != null) {
                                        FieldBuilder.SetMarshal (marshal);
@@ -5434,7 +5424,7 @@ namespace Mono.CSharp {
                                return true;
                        }
  
-                       if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0) {
+                       if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.BACKING_FIELD)) == 0) {
                                Report.SymbolRelatedToPreviousError (conflict_symbol);
                                Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
                                        GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
@@ -5467,10 +5457,8 @@ namespace Mono.CSharp {
 
                public override void Emit ()
                {
-#if GMCS_SOURCE
                        if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
-                               FieldBuilder.SetCustomAttribute (TypeManager.GetCompilerGeneratedAttribute (Location));
-#endif
+                               PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (FieldBuilder);
 
                        if (OptAttributes != null) {
                                OptAttributes.Emit ();
@@ -5607,7 +5595,7 @@ namespace Mono.CSharp {
                        
                        // Create nested fixed buffer container
                        string name = String.Format ("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);
-                       fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name, CodeGen.Module.DefaultCharSetType |
+                       fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name, Parent.Module.DefaultCharSetType |
                                TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, TypeManager.value_type);
                        
                        element = fixed_buffer_type.DefineField (FixedElementName, MemberType, FieldAttributes.Public);
@@ -5662,53 +5650,27 @@ namespace Mono.CSharp {
                        buffer_size *= type_size;
                        EmitFieldSize (buffer_size);
 
-#if GMCS_SOURCE
-                       //
-                       // Emit compiler generated fixed buffer type attribute
-                       //
-                       CustomAttributeBuilder cab = TypeManager.unsafe_value_type_attr;
-                       if (cab == null) {
-                               Type attr_type = TypeManager.CoreLookupType (
-                                       "System.Runtime.CompilerServices", "UnsafeValueTypeAttribute", Kind.Class, true);
-
-                               if (attr_type != null) {
-                                       ConstructorInfo ci = TypeManager.GetPredefinedConstructor (attr_type, Location);
-                                       if (ci != null) {
-                                               cab = new CustomAttributeBuilder (ci, new object [0]);
-                                               TypeManager.unsafe_value_type_attr = cab;
-                                       }
-                               }
-                       }
+                       PredefinedAttributes.Get.UnsafeValueType.EmitAttribute (fixed_buffer_type);
 
-                       if (cab != null)
-                               fixed_buffer_type.SetCustomAttribute (cab);
-#endif
                        base.Emit ();
                }
 
                void EmitFieldSize (int buffer_size)
                {
-                       if (TypeManager.struct_layout_attribute_type == null) {
-                               TypeManager.struct_layout_attribute_type = TypeManager.CoreLookupType (
-                                       "System.Runtime.InteropServices", "StructLayoutAttribute", Kind.Class, true);
+                       CustomAttributeBuilder cab;
+                       PredefinedAttribute pa;
 
-                               if (TypeManager.struct_layout_attribute_type == null)
+                       pa = PredefinedAttributes.Get.StructLayout;
+                       if (pa.Constructor == null &&
+                               !pa.ResolveConstructor (Location, TypeManager.short_type))
                                        return;
-                       }
 
+                       // TODO: It's not cleared
                        if (fi == null)
-                               fi = new FieldInfo [] { TypeManager.struct_layout_attribute_type.GetField ("Size") };
-
-                       object [] fi_val = new object [] { buffer_size };
-
-                       if (TypeManager.struct_layout_attribute_ctor == null) {
-                               TypeManager.struct_layout_attribute_ctor = TypeManager.GetPredefinedConstructor (
-                                       TypeManager.struct_layout_attribute_type, Location, TypeManager.short_type);
-                               if (TypeManager.struct_layout_attribute_ctor == null)
-                                       return;
-                       }
+                               fi = new FieldInfo[] { pa.Type.GetField ("Size") };
 
-                       CustomAttributeBuilder cab = new CustomAttributeBuilder (TypeManager.struct_layout_attribute_ctor,
+                       object[] fi_val = new object[] { buffer_size };
+                       cab = new CustomAttributeBuilder (pa.Constructor,
                                ctor_args, fi, fi_val);
                        fixed_buffer_type.SetCustomAttribute (cab);
                        
@@ -5716,25 +5678,14 @@ namespace Mono.CSharp {
                        // Don't emit FixedBufferAttribute attribute for private types
                        //
                        if ((ModFlags & Modifiers.PRIVATE) != 0)
-                               return; 
-
-                       if (TypeManager.fixed_buffer_attr_ctor == null) {
-                               if (TypeManager.fixed_buffer_attr_type == null) {
-                                       TypeManager.fixed_buffer_attr_type = TypeManager.CoreLookupType (
-                                               "System.Runtime.CompilerServices", "FixedBufferAttribute", Kind.Class, true);
-
-                                       if (TypeManager.fixed_buffer_attr_type == null)
-                                               return;
-                               }
+                               return;
 
-                               TypeManager.fixed_buffer_attr_ctor = TypeManager.GetPredefinedConstructor (TypeManager.fixed_buffer_attr_type,
-                                       Location, TypeManager.type_type, TypeManager.int32_type);
-                               
-                               if (TypeManager.fixed_buffer_attr_ctor == null)
-                                       return;
-                       }
+                       pa = PredefinedAttributes.Get.FixedBuffer;
+                       if (pa.Constructor == null &&
+                               !pa.ResolveConstructor (Location, TypeManager.type_type, TypeManager.int32_type))
+                               return;
 
-                       cab = new CustomAttributeBuilder (TypeManager.fixed_buffer_attr_ctor, new object [] { MemberType, buffer_size });
+                       cab = new CustomAttributeBuilder (pa.Constructor, new object[] { MemberType, buffer_size });
                        FieldBuilder.SetCustomAttribute (cab);
                }
 
@@ -5811,14 +5762,15 @@ namespace Mono.CSharp {
                        if (TypeManager.IsReferenceType (MemberType))
                                return true;
 
-                       if (MemberType.IsEnum)
-                               return true;
-
                        if (MemberType == TypeManager.bool_type || MemberType == TypeManager.char_type ||
                                MemberType == TypeManager.sbyte_type || MemberType == TypeManager.byte_type ||
                                MemberType == TypeManager.short_type || MemberType == TypeManager.ushort_type ||
                                MemberType == TypeManager.int32_type || MemberType == TypeManager.uint32_type ||
-                               MemberType == TypeManager.float_type)
+                               MemberType == TypeManager.float_type ||
+                               MemberType == TypeManager.intptr_type || MemberType == TypeManager.uintptr_type)
+                               return true;
+
+                       if (TypeManager.IsEnumType (MemberType))
                                return true;
 
                        return false;
@@ -5874,7 +5826,11 @@ namespace Mono.CSharp {
                                FieldBuilder = Parent.TypeBuilder.DefineField (
                                        Name, MemberType, Modifiers.FieldAttr (ModFlags));
 #endif
-                               Parent.MemberCache.AddMember (FieldBuilder, this);
+                               // Don't cache inaccessible fields
+                               if ((ModFlags & Modifiers.BACKING_FIELD) == 0) {
+                                       Parent.MemberCache.AddMember (FieldBuilder, this);
+                               }
+
                                TypeManager.RegisterFieldBase (FieldBuilder, this);
                        }
                        catch (ArgumentException) {
@@ -5910,17 +5866,6 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override string GetSignatureForError ()
-               {
-                       string s = base.GetSignatureForError ();
-                       if ((ModFlags & Modifiers.BACKING_FIELD) == 0)
-                               return s;
-
-                       // Undecorate name mangling
-                       int l = s.LastIndexOf ('>');
-                       return s.Substring (0, l).Remove (s.LastIndexOf ('<'), 1);
-               }
-
                protected override bool VerifyClsCompliance ()
                {
                        if (!base.VerifyClsCompliance ())
@@ -5951,9 +5896,9 @@ namespace Mono.CSharp {
                public Attributes Attributes;
                public Location Location;
                public int ModFlags;
-               public Parameters Parameters;
+               public ParametersCompiled Parameters;
                
-               public Accessor (ToplevelBlock b, int mod, Attributes attrs, Parameters p, Location loc)
+               public Accessor (ToplevelBlock b, int mod, Attributes attrs, ParametersCompiled p, Location loc)
                {
                        Block = b;
                        Attributes = attrs;
@@ -5970,7 +5915,7 @@ namespace Mono.CSharp {
                protected ToplevelBlock block;
                protected ListDictionary declarative_security;
 
-               // The accessor are created event if they are not wanted.
+               // The accessor are created even if they are not wanted.
                // But we need them because their names are reserved.
                // Field says whether accessor will be emited or not
                public readonly bool IsDummy;
@@ -6046,16 +5991,15 @@ namespace Mono.CSharp {
                        }
                }
 
-               public abstract Parameters ParameterInfo { get ; }
+               public abstract ParametersCompiled ParameterInfo { get ; }
                public abstract Type ReturnType { get; }
                public abstract EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig);
 
                #endregion
 
-               public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
-                       if (a.Type == TypeManager.cls_compliant_attribute_type || a.Type == TypeManager.obsolete_attribute_type ||
-                                       a.Type == TypeManager.conditional_attribute_type) {
+                       if (a.Type == pa.CLSCompliant || a.Type == pa.Obsolete || a.Type == pa.Conditional) {
                                Report.Error (1667, a.Location,
                                        "Attribute `{0}' is not valid on property or event accessors. It is valid on `{1}' declarations only",
                                        TypeManager.CSharpName (a.Type), a.GetValidTargets ());
@@ -6078,14 +6022,14 @@ namespace Mono.CSharp {
                                if (return_attributes == null)
                                        return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
 
-                               return_attributes.ApplyAttributeBuilder (a, cb);
+                               return_attributes.ApplyAttributeBuilder (a, cb, pa);
                                return;
                        }
 
-                       ApplyToExtraTarget (a, cb);
+                       ApplyToExtraTarget (a, cb, pa);
                }
 
-               virtual protected void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb)
+               protected virtual void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
                        throw new NotSupportedException ("You forgot to define special attribute target handling");
                }
@@ -6096,16 +6040,15 @@ namespace Mono.CSharp {
                        throw new NotSupportedException ();
                }
 
-               public void Emit (DeclSpace parent)
+               public virtual void Emit (DeclSpace parent)
                {
-                       EmitMethod (parent);
+                       method_data.Emit (parent);
 
-#if GMCS_SOURCE                        
                        if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
-                               method_data.MethodBuilder.SetCustomAttribute (TypeManager.GetCompilerGeneratedAttribute (Location));
+                               PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (method_data.MethodBuilder);
                        if (((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0))
-                               method_data.MethodBuilder.SetCustomAttribute (TypeManager.GetDebuggerHiddenAttribute (Location));
-#endif                 
+                               PredefinedAttributes.Get.DebuggerHidden.EmitAttribute (method_data.MethodBuilder);
+
                        if (OptAttributes != null)
                                OptAttributes.Emit ();
 
@@ -6118,11 +6061,6 @@ namespace Mono.CSharp {
                        block = null;
                }
 
-               protected virtual void EmitMethod (DeclSpace parent)
-               {
-                       method_data.Emit (parent);
-               }
-
                public override bool EnableOverloadChecks (MemberCore overload)
                {
                        // This can only happen with indexers and it will
@@ -6224,9 +6162,9 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       public override Parameters ParameterInfo {
+                       public override ParametersCompiled ParameterInfo {
                                get {
-                                       return Parameters.EmptyReadOnlyParameters;
+                                       return ParametersCompiled.EmptyReadOnlyParameters;
                                }
                        }
 
@@ -6241,12 +6179,12 @@ namespace Mono.CSharp {
 
                        static string[] attribute_targets = new string [] { "method", "param", "return" };
                        ImplicitParameter param_attr;
-                       protected Parameters parameters;
+                       protected ParametersCompiled parameters;
 
                        public SetMethod (PropertyBase method) :
                                base (method, "set_")
                        {
-                               parameters = new Parameters (
+                               parameters = new ParametersCompiled (
                                        new Parameter (method.type_name, "value", Parameter.Modifier.NONE, null, Location));
                        }
 
@@ -6256,20 +6194,20 @@ namespace Mono.CSharp {
                                this.parameters = accessor.Parameters;
                        }
 
-                       protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
+                       protected override void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                        {
                                if (a.Target == AttributeTargets.Parameter) {
                                        if (param_attr == null)
                                                param_attr = new ImplicitParameter (method_data.MethodBuilder);
 
-                                       param_attr.ApplyAttributeBuilder (a, cb);
+                                       param_attr.ApplyAttributeBuilder (a, cb, pa);
                                        return;
                                }
 
-                               base.ApplyAttributeBuilder (a, cb);
+                               base.ApplyAttributeBuilder (a, cb, pa);
                        }
 
-                       public override Parameters ParameterInfo {
+                       public override ParametersCompiled ParameterInfo {
                            get {
                                return parameters;
                            }
@@ -6278,6 +6216,7 @@ namespace Mono.CSharp {
                        public override MethodBuilder Define (DeclSpace parent)
                        {
                                parameters.Resolve (ResolveContext);
+                               
                                base.Define (parent);
 
                                if (IsDummy)
@@ -6330,13 +6269,13 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+                       public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                        {
                                if (a.IsInternalMethodImplAttribute) {
                                        method.is_external_implementation = true;
                                }
 
-                               base.ApplyAttributeBuilder (a, cb);
+                               base.ApplyAttributeBuilder (a, cb, pa);
                        }
 
                        public override AttributeTargets AttributeTargets {
@@ -6476,7 +6415,7 @@ namespace Mono.CSharp {
                         this.define_set_first = define_set_first;
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
                        if (a.HasSecurityAttribute) {
                                a.Error_InvalidSecurityParent ();
@@ -6670,7 +6609,26 @@ namespace Mono.CSharp {
                }
        }
                        
-       public class Property : PropertyBase {
+       public class Property : PropertyBase
+       {
+               public sealed class BackingField : Field
+               {
+                       readonly Property property;
+
+                       public BackingField (Property p)
+                               : base (p.Parent, p.type_name,
+                               Modifiers.BACKING_FIELD | Modifiers.COMPILER_GENERATED | Modifiers.PRIVATE | (p.ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE)),
+                               new MemberName ("<" + p.GetFullName (p.MemberName) + ">k__BackingField", p.Location), null)
+                       {
+                               this.property = p;
+                       }
+
+                       public override string GetSignatureForError ()
+                       {
+                               return property.GetSignatureForError ();
+                       }
+               }
+
                const int AllowedModifiers =
                        Modifiers.NEW |
                        Modifiers.PUBLIC |
@@ -6727,23 +6685,24 @@ namespace Mono.CSharp {
                void CreateAutomaticProperty ()
                {
                        // Create backing field
-                       Field field = new Field (
-                               Parent, type_name,
-                               Modifiers.BACKING_FIELD | Modifiers.PRIVATE | (ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE)),
-                               new MemberName ("<" + GetFullName (MemberName) + ">k__BackingField", Location), null);
+                       Field field = new BackingField (this);
                        if (!field.Define ())
                                return;
 
                        Parent.PartialContainer.AddField (field);
 
+                       FieldExpr fe = new FieldExpr (field.FieldBuilder, Location);
+                       if ((field.ModFlags & Modifiers.STATIC) == 0)
+                               fe.InstanceExpression = new CompilerGeneratedThis (fe.Type, Location);
+
                        // Create get block
-                       Get.Block = new ToplevelBlock (Parameters.EmptyReadOnlyParameters, Location);
-                       Return r = new Return (new SimpleName (field.Name, Location), Location);
+                       Get.Block = new ToplevelBlock (ParametersCompiled.EmptyReadOnlyParameters, Location);
+                       Return r = new Return (fe, Location);
                        Get.Block.AddStatement (r);
 
                        // Create set block
                        Set.Block = new ToplevelBlock (Set.ParameterInfo, Location);
-                       Assign a = new SimpleAssign (new SimpleName (field.Name, Location), new SimpleName ("value", Location));
+                       Assign a = new SimpleAssign (fe, new SimpleName ("value", Location));
                        Set.Block.AddStatement (new StatementExpression (a));
                }
 
@@ -6797,7 +6756,7 @@ namespace Mono.CSharp {
                protected override PropertyInfo ResolveBaseProperty ()
                {
                        return Parent.PartialContainer.BaseCache.FindMemberToOverride (
-                               Parent.TypeBuilder, Name, Parameters.EmptyReadOnlyParameters, null, true) as PropertyInfo;
+                               Parent.TypeBuilder, Name, ParametersCompiled.EmptyReadOnlyParameters, null, true) as PropertyInfo;
                }
        }
 
@@ -6976,12 +6935,6 @@ namespace Mono.CSharp {
                                base (method, accessor, "add_")
                        {
                        }
-
-                       protected override MethodInfo DelegateMethodInfo {
-                               get {
-                                       return TypeManager.delegate_combine_delegate_delegate;
-                               }
-                       }
                }
 
                sealed class RemoveDelegateMethod: AEventPropertyAccessor
@@ -6990,12 +6943,6 @@ namespace Mono.CSharp {
                                base (method, accessor, "remove_")
                        {
                        }
-
-                       protected override MethodInfo DelegateMethodInfo {
-                               get {
-                                       return TypeManager.delegate_remove_delegate_delegate;
-                               }
-                       }
                }
 
 
@@ -7037,42 +6984,31 @@ namespace Mono.CSharp {
                        {
                        }
 
-                       protected override void EmitMethod(DeclSpace parent)
+                       public override void Emit (DeclSpace parent)
                        {
-                               if (method_data.implementing != null)
-                                       parent.PartialContainer.PendingImplementations.ImplementMethod (
-                                               Name, method.InterfaceType, method_data, method.IsExplicitImpl);
-                               
-                               if ((method.ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
-                                       return;
-
-                               MethodBuilder mb = method_data.MethodBuilder;
-                               ILGenerator ig = mb.GetILGenerator ();
+                               if ((method.ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) == 0) {
+                                       if (parent is Class) {
+                                               MethodBuilder mb = method_data.MethodBuilder;
+                                               mb.SetImplementationFlags (mb.GetMethodImplementationFlags () | MethodImplAttributes.Synchronized);
+                                       }
 
-                               // TODO: because we cannot use generics yet
-                               FieldInfo field_info = ((EventField)method).FieldBuilder;
+                                       // TODO: because we cannot use generics yet
+                                       FieldInfo field_info = ((EventField) method).BackingField.FieldBuilder;
+                                       FieldExpr f_expr = new FieldExpr (field_info, Location);
+                                       if ((method.ModFlags & Modifiers.STATIC) == 0)
+                                               f_expr.InstanceExpression = new CompilerGeneratedThis (field_info.FieldType, Location);
 
-                               if (parent is Class) {
-                                       mb.SetImplementationFlags (mb.GetMethodImplementationFlags () | MethodImplAttributes.Synchronized);
+                                       block = new ToplevelBlock (ParameterInfo, Location);
+                                       block.AddStatement (new StatementExpression (
+                                               new CompoundAssign (Operation,
+                                                       f_expr,
+                                                       block.GetParameterReference (ParameterInfo[0].Name, Location))));
                                }
-                               
-                               if ((method.ModFlags & Modifiers.STATIC) != 0) {
-                                       ig.Emit (OpCodes.Ldsfld, field_info);
-                                       ig.Emit (OpCodes.Ldarg_0);
-                                       ig.Emit (OpCodes.Call, DelegateMethodInfo);
-                                       ig.Emit (OpCodes.Castclass, method.MemberType);
-                                       ig.Emit (OpCodes.Stsfld, field_info);
-                               } else {
-                                       ig.Emit (OpCodes.Ldarg_0);
-                                       ig.Emit (OpCodes.Ldarg_0);
-                                       ig.Emit (OpCodes.Ldfld, field_info);
-                                       ig.Emit (OpCodes.Ldarg_1);
-                                       ig.Emit (OpCodes.Call, DelegateMethodInfo);
-                                       ig.Emit (OpCodes.Castclass, method.MemberType);
-                                       ig.Emit (OpCodes.Stfld, field_info);
-                               }
-                               ig.Emit (OpCodes.Ret);
+
+                               base.Emit (parent);
                        }
+
+                       protected abstract Binary.Operator Operation { get; }
                }
 
                sealed class AddDelegateMethod: EventFieldAccessor
@@ -7082,10 +7018,8 @@ namespace Mono.CSharp {
                        {
                        }
 
-                       protected override MethodInfo DelegateMethodInfo {
-                               get {
-                                       return TypeManager.delegate_combine_delegate_delegate;
-                               }
+                       protected override Binary.Operator Operation {
+                               get { return Binary.Operator.Addition; }
                        }
                }
 
@@ -7096,10 +7030,8 @@ namespace Mono.CSharp {
                        {
                        }
 
-                       protected override MethodInfo DelegateMethodInfo {
-                               get {
-                                       return TypeManager.delegate_remove_delegate_delegate;
-                               }
+                       protected override Binary.Operator Operation {
+                               get { return Binary.Operator.Subtraction; }
                        }
                }
 
@@ -7107,7 +7039,7 @@ namespace Mono.CSharp {
                static readonly string[] attribute_targets = new string [] { "event", "field", "method" };
                static readonly string[] attribute_targets_interface = new string[] { "event", "method" };
 
-               public FieldBuilder FieldBuilder;
+               public Field BackingField;
                public Expression Initializer;
 
                public EventField (DeclSpace parent, FullNamedExpression type, int mod_flags, MemberName name, Attributes attrs)
@@ -7117,22 +7049,22 @@ namespace Mono.CSharp {
                        Remove = new RemoveDelegateMethod (this);
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
                        if (a.Target == AttributeTargets.Field) {
-                               FieldBuilder.SetCustomAttribute (cb);
+                               BackingField.ApplyAttributeBuilder (a, cb, pa);
                                return;
                        }
 
                        if (a.Target == AttributeTargets.Method) {
                                int errors = Report.Errors;
-                               Add.ApplyAttributeBuilder (a, cb);
+                               Add.ApplyAttributeBuilder (a, cb, pa);
                                if (errors == Report.Errors)
-                                       Remove.ApplyAttributeBuilder (a, cb);
+                                       Remove.ApplyAttributeBuilder (a, cb, pa);
                                return;
                        }
 
-                       base.ApplyAttributeBuilder (a, cb);
+                       base.ApplyAttributeBuilder (a, cb, pa);
                }
 
                public override bool Define()
@@ -7140,41 +7072,50 @@ namespace Mono.CSharp {
                        if (!base.Define ())
                                return false;
 
-                       if (IsInterface)
+                       if (Initializer != null && (ModFlags & Modifiers.ABSTRACT) != 0) {
+                               Report.Error (74, Location, "`{0}': abstract event cannot have an initializer",
+                                       GetSignatureForError ());
+                       }
+
+                       if (!HasBackingField) {
+                               SetMemberIsUsed ();
                                return true;
+                       }
 
                        // FIXME: We are unable to detect whether generic event is used because
                        // we are using FieldExpr instead of EventExpr for event access in that
                        // case.  When this issue will be fixed this hack can be removed.
                        if (TypeManager.IsGenericType (MemberType))
-                               SetMemberIsUsed();
+                               SetMemberIsUsed ();
 
                        if (Add.IsInterfaceImplementation)
                                SetMemberIsUsed ();
 
-                       FieldBuilder = Parent.TypeBuilder.DefineField (
-                               Name, MemberType,
-                               FieldAttributes.Private | ((ModFlags & Modifiers.STATIC) != 0 ? FieldAttributes.Static : 0));
                        TypeManager.RegisterEventField (EventBuilder, this);
 
-                       if (Initializer != null) {
-                               if (((ModFlags & Modifiers.ABSTRACT) != 0)) {
-                                       Report.Error (74, Location, "`{0}': abstract event cannot have an initializer",
-                                               GetSignatureForError ());
-                                       return false;
-                               }
+                       BackingField = new Field (Parent,
+                               new TypeExpression (MemberType, Location),
+                               Modifiers.BACKING_FIELD | Modifiers.COMPILER_GENERATED | Modifiers.PRIVATE | (ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE)),
+                               MemberName, null);
 
-                               ((TypeContainer) Parent).RegisterFieldForInitialization (this,
-                                       new FieldInitializer (FieldBuilder, Initializer, this));
-                       }
+                       Parent.PartialContainer.AddField (BackingField);
+                       BackingField.Initializer = Initializer;
+                       BackingField.ModFlags &= ~Modifiers.COMPILER_GENERATED;
 
-                       return true;
+                       // Call define because we passed fields definition
+                       return BackingField.Define ();
+               }
+
+               bool HasBackingField {
+                       get {
+                               return !IsInterface && (ModFlags & Modifiers.ABSTRACT) == 0;
+                       }
                }
 
                public override string[] ValidAttributeTargets 
                {
                        get {
-                               return IsInterface ? attribute_targets_interface : attribute_targets;
+                               return HasBackingField ? attribute_targets : attribute_targets_interface;
                        }
                }
        }
@@ -7205,26 +7146,26 @@ namespace Mono.CSharp {
                                get { return method_data.implementing != null; }
                        }
 
-                       public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+                       public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                        {
                                if (a.IsInternalMethodImplAttribute) {
                                        method.is_external_implementation = true;
                                }
 
-                               base.ApplyAttributeBuilder (a, cb);
+                               base.ApplyAttributeBuilder (a, cb, pa);
                        }
 
-                       protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
+                       protected override void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                        {
                                if (a.Target == AttributeTargets.Parameter) {
                                        if (param_attr == null)
                                                param_attr = new ImplicitParameter (method_data.MethodBuilder);
 
-                                       param_attr.ApplyAttributeBuilder (a, cb);
+                                       param_attr.ApplyAttributeBuilder (a, cb, pa);
                                        return;
                                }
 
-                               base.ApplyAttributeBuilder (a, cb);
+                               base.ApplyAttributeBuilder (a, cb, pa);
                        }
 
                        public override AttributeTargets AttributeTargets {
@@ -7251,8 +7192,6 @@ namespace Mono.CSharp {
                                return mb;
                        }
 
-                       protected abstract MethodInfo DelegateMethodInfo { get; }
-
                        public override Type ReturnType {
                                get {
                                        return TypeManager.void_type;
@@ -7277,7 +7216,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       public override Parameters ParameterInfo {
+                       public override ParametersCompiled ParameterInfo {
                                get {
                                        return method.parameters;
                                }
@@ -7306,7 +7245,7 @@ namespace Mono.CSharp {
                public MyEventBuilder     EventBuilder;
                public MethodBuilder AddBuilder, RemoveBuilder;
 
-               Parameters parameters;
+               ParametersCompiled parameters;
 
                protected Event (DeclSpace parent, FullNamedExpression type, int mod_flags, MemberName name, Attributes attrs)
                        : base (parent, null, type, mod_flags,
@@ -7315,7 +7254,7 @@ namespace Mono.CSharp {
                {
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
                        if ((a.HasSecurityAttribute)) {
                                a.Error_InvalidSecurityParent ();
@@ -7345,7 +7284,7 @@ namespace Mono.CSharp {
                                Report.Error (66, Location, "`{0}': event must be of a delegate type", GetSignatureForError ());
                        }
 
-                       parameters = Parameters.CreateFullyResolved (
+                       parameters = ParametersCompiled.CreateFullyResolved (
                                new Parameter (null, "value", Parameter.Modifier.NONE, null, Location), MemberType);
 
                        if (!CheckBase ())
@@ -7423,7 +7362,7 @@ namespace Mono.CSharp {
        {
                public class GetIndexerMethod : GetMethod
                {
-                       Parameters parameters;
+                       ParametersCompiled parameters;
 
                        public GetIndexerMethod (Indexer method):
                                base (method)
@@ -7453,7 +7392,7 @@ namespace Mono.CSharp {
                                return false;
                        }                       
 
-                       public override Parameters ParameterInfo {
+                       public override ParametersCompiled ParameterInfo {
                                get {
                                        return parameters;
                                }
@@ -7465,7 +7404,7 @@ namespace Mono.CSharp {
                        public SetIndexerMethod (Indexer method):
                                base (method)
                        {
-                               parameters = Parameters.MergeGenerated (method.parameters, false, parameters [0], null);
+                               parameters = ParametersCompiled.MergeGenerated (method.parameters, false, parameters [0], null);
                        }
 
                        public SetIndexerMethod (PropertyBase method, Accessor accessor):
@@ -7501,10 +7440,10 @@ namespace Mono.CSharp {
                const int AllowedInterfaceModifiers =
                        Modifiers.NEW;
 
-               public readonly Parameters parameters;
+               public readonly ParametersCompiled parameters;
 
                public Indexer (DeclSpace parent, FullNamedExpression type, MemberName name, int mod,
-                               Parameters parameters, Attributes attrs,
+                               ParametersCompiled parameters, Attributes attrs,
                                Accessor get_block, Accessor set_block, bool define_set_first)
                        : base (parent, type, mod,
                                parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
@@ -7536,8 +7475,8 @@ namespace Mono.CSharp {
                        if (!DefineParameters (parameters))
                                return false;
 
-                       if (OptAttributes != null && TypeManager.indexer_name_type != null) {
-                               Attribute indexer_attr = OptAttributes.Search (TypeManager.indexer_name_type);
+                       if (OptAttributes != null) {
+                               Attribute indexer_attr = OptAttributes.Search (PredefinedAttributes.Get.IndexerName);
                                if (indexer_attr != null) {
                                        // Remove the attribute from the list because it is not emitted
                                        OptAttributes.Attrs.Remove (indexer_attr);
@@ -7731,7 +7670,7 @@ namespace Mono.CSharp {
                }
                
                public Operator (DeclSpace parent, OpType type, FullNamedExpression ret_type,
-                                int mod_flags, Parameters parameters,
+                                int mod_flags, ParametersCompiled parameters,
                                 ToplevelBlock block, Attributes attrs, Location loc)
                        : base (parent, null, ret_type, mod_flags, AllowedModifiers,
                                new MemberName (GetMetadataName (type), loc), attrs, parameters)
@@ -7740,14 +7679,14 @@ namespace Mono.CSharp {
                        Block = block;
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
                {
-                       if (a.Type == TypeManager.conditional_attribute_type) {
+                       if (a.Type == pa.Conditional) {
                                Error_ConditionalAttributeIsNotValid ();
                                return;
                        }
 
-                       base.ApplyAttributeBuilder (a, cb);
+                       base.ApplyAttributeBuilder (a, cb, pa);
                }
                
                public override bool Define ()
@@ -7884,33 +7823,6 @@ namespace Mono.CSharp {
                        flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
                        return true;
                }
-               
-               public override void Emit ()
-               {
-                       base.Emit ();
-
-                       Parameters.ApplyAttributes (MethodBuilder);
-
-                       //
-                       // abstract or extern methods have no bodies
-                       //
-                       if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
-                               return;
-                       
-                       EmitContext ec;
-                       if ((flags & MethodAttributes.PinvokeImpl) == 0)
-                               ec = CreateEmitContext (Parent, MethodBuilder.GetILGenerator ());
-                       else
-                               ec = CreateEmitContext (Parent, null);
-                       
-                       SourceMethod source = SourceMethod.Create (Parent, MethodBuilder, Block);
-                       ec.EmitTopBlock (this, Block);
-
-                       if (source != null)
-                               source.CloseMethod ();
-
-                       Block = null;
-               }
 
                // Operator cannot be override
                protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)