2006-04-28 Marek Safar <marek.safar@seznam.cz>
[mono.git] / mcs / gmcs / class.cs
index 6279f73f2236a05a641ca8d066e4b3d7c853a645..4f307da7aeac102ec0abb2acf2f8328bbe31938e 100644 (file)
@@ -32,7 +32,6 @@
 //
 #define CACHE
 using System;
-using System.Text;
 using System.Collections;
 using System.Collections.Specialized;
 using System.Reflection;
@@ -41,6 +40,7 @@ using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 using System.Security;
 using System.Security.Permissions;
+using System.Text;
 using System.Xml;
 
 using Mono.CompilerServices.SymbolWriter;
@@ -67,7 +67,12 @@ namespace Mono.CSharp {
                        public virtual void DefineContainerMembers ()
                        {
                                foreach (MemberCore mc in this) {
-                                       mc.Define ();
+                                       try {
+                                               mc.Define ();
+                                       }
+                                       catch (Exception e) {
+                                               throw new InternalErrorException (mc.Location, mc.GetSignatureForError (), e);
+                                       }
                                }
                        }
 
@@ -227,8 +232,8 @@ namespace Mono.CSharp {
                                {
                                        flags = f;
 
-                                       ret_type = o.OperatorMethod.ReturnType;
-                                       Type [] pt = o.OperatorMethod.ParameterTypes;
+                                       ret_type = o.MemberType;
+                                       Type [] pt = o.ParameterTypes;
                                        type1 = pt [0];
                                        type2 = pt [1];
                                        op = o;
@@ -275,7 +280,7 @@ namespace Mono.CSharp {
                                        int reg = 0;
 
                                        // Skip erroneous code.
-                                       if (op.OperatorMethod == null)
+                                       if (op.MethodBuilder == null)
                                                continue;
 
                                        switch (op.OperatorType){
@@ -383,7 +388,7 @@ namespace Mono.CSharp {
                public readonly Kind Kind;
 
                // Holds a list of classes and structures
-               ArrayList types;
+               protected ArrayList types;
 
                // Holds the list of properties
                MemberCoreArrayList properties;
@@ -427,9 +432,6 @@ namespace Mono.CSharp {
                // Holds the iterators
                ArrayList iterators;
 
-               // Holds the parts of a partial class;
-               ArrayList parts;
-
                //
                // Pointers to the default constructor and the default static constructor
                //
@@ -448,7 +450,6 @@ namespace Mono.CSharp {
                // This one is computed after we can distinguish interfaces
                // from classes from the arraylist `type_bases' 
                //
-               string base_class_name;
                TypeExpr base_type;
                TypeExpr[] iface_exprs;
 
@@ -459,7 +460,6 @@ namespace Mono.CSharp {
 
                // The interfaces we implement.
                protected Type[] ifaces;
-               protected Type ptype;
 
                // The base member cache and our member cache
                MemberCache base_cache;
@@ -470,7 +470,16 @@ namespace Mono.CSharp {
                Type GenericType;
                GenericTypeParameterBuilder[] gen_params;
 
-               public TypeContainer (NamespaceEntry ns, TypeContainer parent, MemberName name,
+               public TypeContainer PartialContainer;
+               ArrayList partial_parts;
+
+               /// <remarks>
+               ///  The pending methods that need to be implemented
+               //   (interfaces or abstract methods)
+               /// </remarks>
+               PendingImplementation pending;
+
+               public TypeContainer (NamespaceEntry ns, DeclSpace parent, MemberName name,
                                      Attributes attrs, Kind kind)
                        : base (ns, parent, name, attrs)
                {
@@ -478,10 +487,7 @@ namespace Mono.CSharp {
                                throw new InternalErrorException ("A nested type should be in the same NamespaceEntry as its enclosing class");
 
                        this.Kind = kind;
-
-                       types = new ArrayList ();
-
-                       base_class_name = null;
+                       this.PartialContainer = this;
                }
 
                public bool AddToMemberContainer (MemberCore symbol)
@@ -521,10 +527,72 @@ namespace Mono.CSharp {
                        if (!AddToTypeContainer (c))
                                return false;
 
+                       if (types == null)
+                               types = new ArrayList (2);
+
+                       RootContext.Tree.RecordDecl (c.NamespaceEntry.NS, c.MemberName, c);
                        types.Add (c);
                        return true;
                }
 
+               public virtual TypeContainer AddPartial (TypeContainer nextPart)
+               {
+                       return AddPartial (nextPart, nextPart.Basename);
+               }
+
+               protected TypeContainer AddPartial (TypeContainer nextPart, string name)
+               {
+                       nextPart.ModFlags |= Modifiers.PARTIAL;
+                       TypeContainer tc = defined_names [name] as TypeContainer;
+
+                       if (tc == null) {
+                               if (nextPart is Interface)
+                                       AddInterface (nextPart);
+                               else
+                                       AddClassOrStruct (nextPart);
+                               return nextPart;
+                       }
+
+                       if ((tc.ModFlags & Modifiers.PARTIAL) == 0) {
+                               Report.SymbolRelatedToPreviousError (tc);
+                               Error_MissingPartialModifier (nextPart);
+                               return tc;
+                       }
+
+                       if (tc.Kind != nextPart.Kind) {
+                               Report.SymbolRelatedToPreviousError (tc);
+                               Report.Error (261, nextPart.Location,
+                                       "Partial declarations of `{0}' must be all classes, all structs or all interfaces",
+                                       nextPart.GetSignatureForError ());
+                               return tc;
+                       }
+
+                       if ((tc.ModFlags & Modifiers.Accessibility) != (nextPart.ModFlags & Modifiers.Accessibility) &&
+                               ((tc.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0 &&
+                                (nextPart.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0)) {
+                               Report.SymbolRelatedToPreviousError (tc);
+                               Report.Error (262, nextPart.Location,
+                                       "Partial declarations of `{0}' have conflicting accessibility modifiers",
+                                       nextPart.GetSignatureForError ());
+                               return tc;
+                       }
+
+                       if (tc.partial_parts == null)
+                               tc.partial_parts = new ArrayList (1);
+
+                       tc.ModFlags |= nextPart.ModFlags;
+                       if (nextPart.attributes != null) {
+                               if (tc.attributes == null)
+                                       tc.attributes = nextPart.attributes;
+                               else
+                                       tc.attributes.AddAttributes (nextPart.attributes.Attrs);
+                       }
+
+                       nextPart.PartialContainer = tc;
+                       tc.partial_parts.Add (nextPart);
+                       return tc;
+               }
+
                public void AddDelegate (Delegate d)
                {
                        if (!AddToTypeContainer (d))
@@ -583,7 +651,7 @@ namespace Mono.CSharp {
 
                                default_static_constructor = c;
                        } else {
-                               if (c.IsDefault ()){
+                               if (c.Parameters.Empty){
                                        if (default_constructor != null) {
                                                Report.SymbolRelatedToPreviousError (default_constructor);
                                                Report.Error (111, c.Location, Error111, c.GetSignatureForError ());
@@ -614,6 +682,7 @@ namespace Mono.CSharp {
                                interfaces = new MemberCoreArrayList ();
                        }
 
+                       RootContext.Tree.RecordDecl (iface.NamespaceEntry.NS, iface.MemberName, iface);
                        interfaces.Add (iface);
                        return true;
                }
@@ -713,19 +782,6 @@ namespace Mono.CSharp {
                        iterators.Add (i);
                }
 
-               public void AddType (TypeContainer tc)
-               {
-                       types.Add (tc);
-               }
-
-               public void AddPart (ClassPart part)
-               {
-                       if (parts == null)
-                               parts = new ArrayList ();
-
-                       parts.Add (part);
-               }
-
                public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
                {
                        if (a.Type == TypeManager.default_member_type) {
@@ -736,20 +792,11 @@ namespace Mono.CSharp {
                        }
                        
                        base.ApplyAttributeBuilder (a, cb);
-               } 
+               } 
 
                public override AttributeTargets AttributeTargets {
                        get {
-                               switch (Kind) {
-                               case Kind.Class:
-                                       return AttributeTargets.Class;
-                               case Kind.Struct:
-                                       return AttributeTargets.Struct;
-                               case Kind.Interface:
-                                       return AttributeTargets.Interface;
-                               default:
-                                       throw new NotSupportedException ();
-                               }
+                               throw new NotSupportedException ();
                        }
                }
 
@@ -782,10 +829,10 @@ namespace Mono.CSharp {
                                return iterators;
                        }
                }
-               
-               public string Base {
+
+               protected Type BaseType {
                        get {
-                               return base_class_name;
+                               return TypeBuilder.BaseType;
                        }
                }
                
@@ -847,15 +894,9 @@ namespace Mono.CSharp {
                        }
                }
                
-               public ArrayList Parts {
-                       get {
-                               return parts;
-                       }
-               }
-
                protected override TypeAttributes TypeAttr {
                        get {
-                               return Modifiers.TypeAttr (ModFlags, this) | base.TypeAttr;
+                               return Modifiers.TypeAttr (ModFlags, IsTopLevel) | base.TypeAttr;
                        }
                }
 
@@ -870,7 +911,7 @@ namespace Mono.CSharp {
                                if (OptAttributes == null)
                                        return false;
 
-                               return OptAttributes.Contains (TypeManager.comimport_attr_type, EmitContext);
+                               return OptAttributes.Contains (TypeManager.comimport_attr_type);
                        }
                }
 
@@ -892,7 +933,7 @@ namespace Mono.CSharp {
                //
                // Emits the instance field initializers
                //
-               public virtual bool EmitFieldInitializers (EmitContext ec)
+               public bool EmitFieldInitializers (EmitContext ec)
                {
                        ArrayList fields;
                        
@@ -911,107 +952,41 @@ namespace Mono.CSharp {
                        return true;
                }
                
-               //
-               // Defines the default constructors
-               //
-               protected void DefineDefaultConstructor (bool is_static)
-               {
-                       Constructor c;
-
-                       // The default constructor is public
-                       // If the class is abstract, the default constructor is protected
-                       // The default static constructor is private
-
-                       int mods = Modifiers.PUBLIC;
-                       if (is_static)
-                               mods = Modifiers.STATIC | Modifiers.PRIVATE;
-                       else if ((ModFlags & Modifiers.ABSTRACT) != 0)
-                               mods = Modifiers.PROTECTED;
-
-                       TypeContainer constructor_parent = this;
-                       if (Parts != null)
-                               constructor_parent = (TypeContainer) Parts [0];
+               public override string DocComment {
+                       get {
+                               return comment;
+                       }
+                       set {
+                               if (value == null)
+                                       return;
 
-                       c = new Constructor (constructor_parent, MemberName.Name, mods,
-                                            Parameters.EmptyReadOnlyParameters,
-                                            new GeneratedBaseInitializer (Location),
-                                            Location);
-                       
-                       AddConstructor (c);
-                       
-                       c.Block = new ToplevelBlock (null, Location);
-                       
+                               comment += value;
+                       }
                }
 
-               /// <remarks>
-               ///  The pending methods that need to be implemented
-               //   (interfaces or abstract methods)
-               /// </remarks>
-               public PendingImplementation Pending;
-
-               public abstract PendingImplementation GetPendingImplementations ();
+               public PendingImplementation PendingImplementations {
+                       get { return pending; }
+               }
 
-               TypeExpr[] GetPartialBases (out TypeExpr base_class)
+               public override bool GetClsCompliantAttributeValue ()
                {
-                       ArrayList ifaces = new ArrayList ();
-
-                       base_class = null;
-
-                       foreach (ClassPart part in parts) {
-                               TypeExpr new_base_class;
-                               TypeExpr[] new_ifaces;
-
-                               new_ifaces = part.GetClassBases (out new_base_class);
-                               if (new_ifaces == null && new_base_class != null)
-                                       return null;
-
-                               if ((base_class != null) && (new_base_class != null) &&
-                                   !base_class.Equals (new_base_class)) {
-                                       Report.SymbolRelatedToPreviousError (base_class.Location, "");
-                                       Report.Error (263, part.Location,
-                                                     "Partial declarations of `{0}' must " +
-                                                     "not specify different base classes",
-                                                     Name);
-
-                                       return null;
-                               }
-
-                               if ((base_class == null) && (new_base_class != null)) {
-                                       base_class = new_base_class;
-                               }
-
-                               if (new_ifaces == null)
-                                       continue;
-
-                               foreach (TypeExpr iface in new_ifaces) {
-                                       bool found = false;
-                                       foreach (TypeExpr old_iface in ifaces) {
-                                               if (old_iface.Equals (iface)) {
-                                                       found = true;
-                                                       break;
-                                               }
-                                       }
+                       if (PartialContainer != this)
+                               return PartialContainer.GetClsCompliantAttributeValue ();
 
-                                       if (!found)
-                                               ifaces.Add (iface);
-                               }
-                       }
-
-                       TypeExpr[] retval = new TypeExpr [ifaces.Count];
-                       ifaces.CopyTo (retval, 0);
-                       return retval;
+                       return base.GetClsCompliantAttributeValue ();
                }
 
                TypeExpr[] GetNormalBases (out TypeExpr base_class)
                {
                        base_class = null;
+                       if (Bases == null)
+                               return null;
 
                        int count = Bases.Count;
                        int start = 0, i, j;
 
                        if (Kind == Kind.Class){
-                               TypeExpr name = ResolveBaseTypeExpr (
-                                       (Expression) Bases [0], false, Location);
+                               TypeExpr name = ((Expression) Bases [0]).ResolveAsBaseTerminal (this, false);
 
                                if (name == null){
                                        return null;
@@ -1028,7 +1003,7 @@ namespace Mono.CSharp {
                        TypeExpr [] ifaces = new TypeExpr [count-start];
                        
                        for (i = start, j = 0; i < count; i++, j++){
-                               TypeExpr resolved = ResolveBaseTypeExpr ((Expression) Bases [i], false, Location);
+                               TypeExpr resolved = ((Expression) Bases [i]).ResolveAsTypeTerminal (this, false);
                                if (resolved == null) {
                                        return null;
                                }
@@ -1036,7 +1011,49 @@ namespace Mono.CSharp {
                                ifaces [j] = resolved;
                        }
 
-                       return ifaces;
+                       return ifaces.Length == 0 ? null : ifaces;
+               }
+
+
+               TypeExpr[] GetNormalPartialBases (ref TypeExpr base_class)
+               {
+                       ArrayList ifaces = new ArrayList (0);
+                       if (iface_exprs != null)
+                               ifaces.AddRange (iface_exprs);
+
+                       foreach (TypeContainer part in partial_parts) {
+                               TypeExpr new_base_class;
+                               TypeExpr[] new_ifaces = part.GetClassBases (out new_base_class);
+                               if (new_base_class != TypeManager.system_object_expr) {
+                                       if (base_class == TypeManager.system_object_expr)
+                                               base_class = new_base_class;
+                                       else {
+                                               if (new_base_class != null && !new_base_class.Equals (base_class)) {
+                                                       Report.SymbolRelatedToPreviousError (base_class.Location, "");
+                                                       Report.Error (263, part.Location,
+                                                               "Partial declarations of `{0}' must not specify different base classes",
+                                                               part.GetSignatureForError ());
+
+                                                       return null;
+                                               }
+                                       }
+                               }
+
+                               if (new_ifaces == null)
+                                       continue;
+
+                               foreach (TypeExpr iface in new_ifaces) {
+                                       if (ifaces.Contains (iface))
+                                               continue;
+
+                                       ifaces.Add (iface);
+                               }
+                       }
+
+                       if (ifaces.Count == 0)
+                               return null;
+
+                       return (TypeExpr[])ifaces.ToArray (typeof (TypeExpr));
                }
 
                /// <summary>
@@ -1049,72 +1066,16 @@ namespace Mono.CSharp {
                ///   The @base_class argument is set to the base object or null
                ///   if this is `System.Object'. 
                /// </summary>
-               protected virtual TypeExpr [] GetClassBases (out TypeExpr base_class)
+               public virtual TypeExpr [] GetClassBases (out TypeExpr base_class)
                {
-                       int i;
-
-                       TypeExpr[] ifaces;
-
-                       if (parts != null)
-                               ifaces = GetPartialBases (out base_class);
-                       else if (Bases == null){
-                               base_class = null;
-                               return null;
-                       } else
-                               ifaces = GetNormalBases (out base_class);
+                       TypeExpr[] ifaces = GetNormalBases (out base_class);
 
                        if (ifaces == null)
                                return null;
 
-                       if ((base_class != null) && (Kind == Kind.Class)){
-                               if (base_class is TypeParameterExpr){
-                                       Report.Error (
-                                               689, base_class.Location,
-                                               "Cannot derive from `{0}' because it is a type parameter",
-                                               base_class.GetSignatureForError ());
-                                       error = true;
-                                       return null;
-                               }
-
-                               if (base_class.Type.IsArray || base_class.Type.IsPointer) {
-                                       Report.Error (1521, base_class.Location, "Invalid base type");
-                                       return null;
-                               }
-
-                               if (base_class.IsSealed){
-                                       Report.SymbolRelatedToPreviousError (base_class.Type);
-                                       if (base_class.Type.IsAbstract) {
-                                               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}'",
-                                                       GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
-                                       }
-                                       return null;
-                               }
-
-                               if (!base_class.CanInheritFrom ()){
-                                       Report.Error (644, Location, "`{0}' cannot derive from special class `{1}'",
-                                                     GetSignatureForError (), base_class.GetSignatureForError ());
-                                       return null;
-                               }
-
-                               if (!base_class.AsAccessible (this, ModFlags)) {
-                                       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 ());
-                               }
-                       }
-
-                       if (base_class != null)
-                               base_class_name = base_class.Name;
-
-                       if (ifaces == null)
-                               return null;
+                       int count = ifaces.Length;
 
-                       int count = ifaces != null ? ifaces.Length : 0;
-
-                       for (i = 0; i < count; i++) {
+                       for (int i = 0; i < count; i++) {
                                TypeExpr iface = (TypeExpr) ifaces [i];
 
                                if (!iface.IsInterface) {
@@ -1136,7 +1097,7 @@ namespace Mono.CSharp {
                                        if (iface.Equals (ifaces [x])) {
                                                Report.Error (528, Location,
                                                              "`{0}' is already listed in " +
-                                                             "interface list", iface.Name);
+                                                             "interface list", iface.GetSignatureForError ());
                                                return null;
                                        }
                                }
@@ -1189,24 +1150,12 @@ namespace Mono.CSharp {
                        Report.Error (527, loc, "Type `{0}' in interface list is not an interface", type);
                }
 
-               //
-               // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
-               //
-               public override TypeBuilder DefineType ()
+               bool DefineTypeBuilder ()
                {
-                       if (error)
-                               return null;
-
-                       if (TypeBuilder != null)
-                               return TypeBuilder;
-
-                       TypeAttributes type_attributes = TypeAttr;
-
                        try {
                                if (IsTopLevel){
                                        if (TypeManager.NamespaceClash (Name, Location)) {
-                                               error = true;
-                                               return null;
+                                               return false;
                                        }
 
                                        ModuleBuilder builder = CodeGen.Module.Builder;
@@ -1214,43 +1163,20 @@ namespace Mono.CSharp {
                                        if (Kind == Kind.Struct)
                                                default_parent = TypeManager.value_type;
                                        TypeBuilder = builder.DefineType (
-                                               Name, type_attributes, default_parent, null);
+                                               Name, TypeAttr, default_parent, null);
                                } else {
                                        TypeBuilder builder = Parent.TypeBuilder;
-                                       if (builder == null) {
-                                               error = true;
-                                               return null;
-                                       }
 
                                        TypeBuilder = builder.DefineNestedType (
-                                               Basename, type_attributes, ptype, null);
+                                               Basename, TypeAttr, null, null);
                                }
                        } catch (ArgumentException) {
                                Report.RuntimeMissingSupport (Location, "static classes");
-                               error = true;
-                               return null;
+                               return false;
                        }
 
                        TypeManager.AddUserType (this);
 
-                       if (Parts != null) {
-                               ec = null;
-                               foreach (ClassPart part in Parts) {
-                                       part.TypeBuilder = TypeBuilder;
-                                       part.ec = new EmitContext (part, Mono.CSharp.Location.Null, null, null, ModFlags);
-                                       part.ec.ContainerType = TypeBuilder;
-                               }
-                       } else {
-                               //
-                               // Normally, we create the EmitContext here.
-                               // The only exception is if we're an Iterator - in this case,
-                               // we already have the `ec', so we don't want to create a new one.
-                               //
-                               if (ec == null)
-                                       ec = new EmitContext (this, Mono.CSharp.Location.Null, null, null, ModFlags);
-                               ec.ContainerType = TypeBuilder;
-                       }
-
                        if (IsGeneric) {
                                string[] param_names = new string [TypeParameters.Length];
                                for (int i = 0; i < TypeParameters.Length; i++)
@@ -1264,9 +1190,8 @@ namespace Mono.CSharp {
                        }
 
                        iface_exprs = GetClassBases (out base_type);
-                       if (iface_exprs == null && base_type != null) {
-                               error = true;
-                               return null;
+                       if (partial_parts != null) {
+                               iface_exprs = GetNormalPartialBases (ref base_type);
                        }
 
                        //
@@ -1282,153 +1207,176 @@ namespace Mono.CSharp {
                        if (!(this is Iterator))
                                RootContext.RegisterOrder (this); 
 
-                       if (base_type == null) {
-                               if (Kind == Kind.Class){
-                                       if (RootContext.StdLib)
-                                               base_type = TypeManager.system_object_expr;
-                                       else if (Name != "System.Object")
-                                               base_type = TypeManager.system_object_expr;
-                               } else if (Kind == Kind.Struct){
-                                       //
-                                       // If we are compiling our runtime,
-                                       // and we are defining ValueType, then our
-                                       // base is `System.Object'.
-                                       //
-                                       if (!RootContext.StdLib && Name == "System.ValueType")
-                                               base_type = TypeManager.system_object_expr;
-                                       else if (Kind == Kind.Struct)
-                                               base_type = TypeManager.system_valuetype_expr;
-                               }
-                       }
-
-                       // Avoid attributes check when parent is not set
-                       TypeResolveEmitContext.TestObsoleteMethodUsage = false;
-
                        if (base_type != null) {
-                               // FIXME: I think this should be ...ResolveType (Parent.EmitContext).
-                               //        However, if Parent == RootContext.Tree.Types, its NamespaceEntry will be null.
-                               FullNamedExpression fne = base_type.ResolveAsTypeStep (TypeResolveEmitContext, false);
-                               if ((fne == null) || (fne.Type == null)) {
-                                       error = true;
-                                       return null;
-                               }
-
-                               ptype = fne.Type;
-
-                               if (IsGeneric && TypeManager.IsAttributeType (ptype)) {
+                               if (IsGeneric && TypeManager.IsAttributeType (base_type.Type)) {
                                        Report.Error (698, base_type.Location,
                                                      "A generic type cannot derive from `{0}' " +
                                                      "because it is an attribute class",
                                                      base_type.Name);
-                                       error = true;
-                                       return null;
+                                       return false;
                                }
-                       }
 
-                       if (!CheckRecursiveDefinition (this)) {
-                               error = true;
-                               return null;
-                       }
+                               TypeBuilder.SetParent (base_type.Type);
 
-                       if (ptype != null) {
-                               TypeBuilder.SetParent (ptype);
+                               ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (base_type.Type);
+                               if (obsolete_attr != null && !IsInObsoleteScope) {
+                                       AttributeTester.Report_ObsoleteMessage (obsolete_attr, base_type.GetSignatureForError (), Location);
+                               }
                        }
 
-                       // Attribute is undefined at the begining of corlib compilation
-                       if (TypeManager.obsolete_attribute_type != null) {
-                               TypeResolveEmitContext.TestObsoleteMethodUsage = GetObsoleteAttribute () == null;
-                               if (ptype != null && TypeResolveEmitContext.TestObsoleteMethodUsage) {
-                                       CheckObsoleteType (base_type);
-                               }
+                       if (!CheckRecursiveDefinition (this)) {
+                               return false;
                        }
 
                        // add interfaces that were not added at type creation
                        if (iface_exprs != null) {
-                               // FIXME: I think this should be ...ExpandInterfaces (Parent.EmitContext, ...).
-                               //        However, if Parent == RootContext.Tree.Types, its NamespaceEntry will be null.
-                               TypeResolveEmitContext.ContainerType = TypeBuilder;
-                               ifaces = TypeManager.ExpandInterfaces (TypeResolveEmitContext, iface_exprs);
+                               ifaces = TypeManager.ExpandInterfaces (iface_exprs);
                                if (ifaces == null) {
-                                       error = true;
-                                       return null;
+                                       return false;
                                }
 
                                foreach (Type itype in ifaces)
                                        TypeBuilder.AddInterfaceImplementation (itype);
 
                                if (!CheckGenericInterfaces (ifaces)) {
-                                       error = true;
-                                       return null;
+                                       return false;
                                }
 
                                TypeManager.RegisterBuilder (TypeBuilder, ifaces);
                        }
 
                        if (this is Iterator && !ResolveType ()) {
-                               error = true;
-                               return null;
+                               return false;
                        }
 
-                       if (!DefineNestedTypes ()) {
+                       return true;
+               }
+
+               //
+               // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
+               //
+               public override TypeBuilder DefineType ()
+               {
+                       if (TypeBuilder != null)
+                               return TypeBuilder;
+
+                       if (error)
+                               return null;
+                       
+                       if (!DefineTypeBuilder ()) {
                                error = true;
                                return null;
                        }
 
+                       if (partial_parts != null) {
+                               foreach (TypeContainer part in partial_parts)
+                                       part.TypeBuilder = TypeBuilder;
+                       }
+
+                       DefineNestedTypes ();
+
                        return TypeBuilder;
                }
 
-               public bool ResolveType ()
+               Constraints [] constraints;
+               public override void SetParameterInfo (ArrayList constraints_list)
                {
-                       if ((base_type != null) &&
-                           (base_type.ResolveType (TypeResolveEmitContext) == null)) {
-                               error = true;
-                               return false;
+                       if (PartialContainer == this) {
+                               base.SetParameterInfo (constraints_list);
+                               return;
                        }
 
-                       if (!IsGeneric)
-                               return true;
+                       if (constraints_list == null)
+                               return;
 
-                       TypeExpr current_type = null;
-                       if (Parts != null) {
-                               foreach (ClassPart part in Parts) {
-                                       if (!part.DefineTypeParameters ()) {
-                                               error = true;
-                                               return false;
+                       constraints = new Constraints [PartialContainer.CountCurrentTypeParameters];
+
+                       TypeParameter[] current_params = PartialContainer.CurrentTypeParameters;
+                       for (int i = 0; i < constraints.Length; i++) {
+                               foreach (Constraints constraint in constraints_list) {
+                                       if (constraint.TypeParameter == current_params [i].Name) {
+                                               constraints [i] = constraint;
+                                               break;
                                        }
                                }
-                       } else {
-                               foreach (TypeParameter type_param in CurrentTypeParameters) {
-                                       if (!type_param.Resolve (this)) {
-                                               error = true;
-                                               return false;
-                                       }
+                       }
+               }
+
+               bool UpdateTypeParameterConstraints ()
+               {
+                       bool ok = true;
+                       TypeParameter[] current_params = PartialContainer.CurrentTypeParameters;
+
+                       if (constraints == null)
+                               return true;
+
+                       for (int i = 0; i < current_params.Length; i++) {
+                               if (!current_params [i].UpdateConstraints (this, constraints [i])) {
+                                       Report.Error (265, Location, "Partial declarations of `{0}' have " +
+                                                     "inconsistent constraints for type parameter `{1}'.",
+                                                     MemberName.GetTypeName (), current_params [i].Name);
+                                       ok = false;
+                               }
+                       }
+
+                       return ok;
+               }
+
+               public bool ResolveType ()
+               {
+                       if ((base_type != null) &&
+                           (base_type.ResolveAsTypeTerminal (this, false) == null)) {
+                               error = true;
+                               return false;
+                       }
+
+                       if (!IsGeneric)
+                               return true;
+
+                       if (PartialContainer != this)
+                               throw new InternalErrorException ();
+
+                       TypeExpr current_type = null;
+
+                       foreach (TypeParameter type_param in CurrentTypeParameters) {
+                               if (!type_param.Resolve (this)) {
+                                       error = true;
+                                       return false;
                                }
+                       }
 
-                               foreach (TypeParameter type_param in TypeParameters) {
-                                       if (!type_param.DefineType (ec)) {
+                       if (partial_parts != null) {
+                               foreach (TypeContainer part in partial_parts) {
+                                       if (!part.UpdateTypeParameterConstraints ()) {
                                                error = true;
                                                return false;
                                        }
                                }
+                       }
 
-                               current_type = new ConstructedType (
-                                       TypeBuilder, TypeParameters, Location);
+                       foreach (TypeParameter type_param in TypeParameters) {
+                               if (!type_param.DefineType (this)) {
+                                       error = true;
+                                       return false;
+                               }
                        }
 
+                       current_type = new ConstructedType (TypeBuilder, TypeParameters, Location);
+
                        foreach (TypeParameter type_param in TypeParameters)
-                               if (!type_param.CheckDependencies (ec)) {
+                               if (!type_param.CheckDependencies ()) {
                                        error = true;
                                        return false;
                                }
 
                        if (current_type != null) {
-                               current_type = current_type.ResolveAsTypeTerminal (ec, false);
+                               current_type = current_type.ResolveAsTypeTerminal (this, false);
                                if (current_type == null) {
                                        error = true;
                                        return false;
                                }
 
-                               CurrentType = current_type.ResolveType (ec);
+                               CurrentType = current_type.Type;
                        }
 
                        return true;
@@ -1484,10 +1432,9 @@ namespace Mono.CSharp {
 
                        InTransit = tc;
 
-                       Type parent = ptype;
-                       if (parent != null) {
-                               parent = TypeManager.DropGenericTypeArguments (parent);
-                               TypeContainer ptc = TypeManager.LookupTypeContainer (parent);
+                       if (BaseType != null) {
+                               Type t = TypeManager.DropGenericTypeArguments (BaseType);
+                               TypeContainer ptc = TypeManager.LookupTypeContainer (t);
                                if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
                                        return false;
                        }
@@ -1532,14 +1479,14 @@ namespace Mono.CSharp {
                        if (iface_exprs != null) {
                                foreach (TypeExpr iface in iface_exprs) {
                                        ConstructedType ct = iface as ConstructedType;
-                                       if ((ct != null) && !ct.CheckConstraints (ec))
+                                       if ((ct != null) && !ct.CheckConstraints (this))
                                                return false;
                                }
                        }
 
                        if (base_type != null) {
                                ConstructedType ct = base_type as ConstructedType;
-                               if ((ct != null) && !ct.CheckConstraints (ec))
+                               if ((ct != null) && !ct.CheckConstraints (this))
                                        return false;
                        }
 
@@ -1560,38 +1507,8 @@ namespace Mono.CSharp {
                        DefineContainerMembers (constants);
                        DefineContainerMembers (fields);
 
-                       if ((Kind == Kind.Class) && !(this is ClassPart)){
-                               if ((instance_constructors == null) &&
-                                   !(this is StaticClass)) {
-                                       if (default_constructor == null)
-                                               DefineDefaultConstructor (false);
-                               }
-
-                               if (initialized_static_fields != null &&
-                                   default_static_constructor == null)
-                                       DefineDefaultConstructor (true);
-                       }
-
-                       if (Kind == Kind.Struct){
-                               //
-                               // Structs can not have initialized instance
-                               // fields
-                               //
-                               if (initialized_static_fields != null &&
-                                   default_static_constructor == null)
-                                       DefineDefaultConstructor (true);
-
-                               if (initialized_fields != null)
-                                       ReportStructInitializedInstanceError ();
-                       }
-
-                       Pending = GetPendingImplementations ();
-
-                       if (parts != null) {
-                               foreach (ClassPart part in parts) {
-                                       if (!part.DefineMembers ())
-                                               return false;
-                               }
+                       if (Kind == Kind.Struct || Kind == Kind.Class) {
+                               pending = PendingImplementation.GetPendingImplementations (this);
                        }
                        
                        //
@@ -1599,9 +1516,6 @@ namespace Mono.CSharp {
                        //
                        DefineContainerMembers (instance_constructors);
 
-                       if (default_static_constructor != null)
-                               default_static_constructor.Define ();
-
                        DefineContainerMembers (properties);
                        DefineContainerMembers (events);
                        DefineContainerMembers (indexers);
@@ -1612,21 +1526,16 @@ namespace Mono.CSharp {
 
                        if (CurrentType != null) {
                                GenericType = CurrentType;
-
-                               ec.ContainerType = GenericType;
                        }
 
 
 #if CACHE
-                       if (!(this is ClassPart))
-                               member_cache = new MemberCache (this);
-#endif
-
-                       if (parts != null) {
-                               foreach (ClassPart part in parts)
+                       member_cache = new MemberCache (this);
+                       if (partial_parts != null) {
+                               foreach (TypeContainer part in partial_parts)
                                        part.member_cache = member_cache;
                        }
-
+#endif
                        if (iterators != null) {
                                foreach (Iterator iterator in iterators) {
                                        if (iterator.DefineType () == null)
@@ -1642,15 +1551,6 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               void ReportStructInitializedInstanceError ()
-               {
-                       foreach (Field f in initialized_fields){
-                               Report.Error (573, Location,
-                                       "`{0}': Structs cannot have instance field initializers",
-                                       f.GetSignatureForError ());
-                       }
-               }
-
                protected virtual void DefineContainerMembers (MemberCoreArrayList mcal)
                {
                        if (mcal != null)
@@ -1659,13 +1559,6 @@ namespace Mono.CSharp {
 
                public override bool Define ()
                {
-                       if (parts != null) {
-                               foreach (ClassPart part in parts) {
-                                       if (!part.Define ())
-                                               return false;
-                               }
-                       }
-
                        if (iterators != null) {
                                foreach (Iterator iterator in iterators) {
                                        if (!iterator.Define ())
@@ -1721,7 +1614,7 @@ namespace Mono.CSharp {
                                for (int i = 0; i < len; i++) {
                                        Operator o = (Operator) operators [i];
 
-                                       members.Add (o.OperatorMethodBuilder);
+                                       members.Add (o.MethodBuilder);
                                }
                        }
 
@@ -1767,14 +1660,20 @@ namespace Mono.CSharp {
                }
                
                // Indicated whether container has StructLayout attribute set Explicit
-               public virtual bool HasExplicitLayout {
+               public bool HasExplicitLayout {
                        get {
-                               return false;
+                               return (caching_flags & Flags.HasExplicitLayout) != 0;
+                       }
+                       set {
+                               caching_flags |= Flags.HasExplicitLayout;
                        }
                }
 
                public override Type FindNestedType (string name)
                {
+                       if (PartialContainer != this)
+                               return PartialContainer.FindNestedType (name);
+
                        ArrayList [] lists = { types, enums, delegates, interfaces };
 
                        for (int j = 0; j < lists.Length; ++j) {
@@ -1786,8 +1685,7 @@ namespace Mono.CSharp {
                                for (int i = 0; i < len; ++i) {
                                        DeclSpace ds = (DeclSpace) list [i];
                                        if (ds.Basename == name) {
-                                               ds.DefineType ();
-                                               return ds.TypeBuilder;
+                                               return ds.DefineType ();
                                        }
                                }
                        }
@@ -1959,7 +1857,7 @@ namespace Mono.CSharp {
                                                if ((o.ModFlags & static_mask) != static_flags)
                                                        continue;
                                                
-                                               MethodBuilder ob = o.OperatorMethodBuilder;
+                                               MethodBuilder ob = o.MethodBuilder;
                                                if (ob != null && filter (ob, criteria) == true) {
                                                        if (members == null)
                                                                members = new ArrayList ();
@@ -2167,16 +2065,6 @@ namespace Mono.CSharp {
                                return new MemberList (t.FindMembers (mt, bf, filter, criteria));
                }
 
-               //
-               // FindMethods will look for methods not only in the type `t', but in
-               // any interfaces implemented by the type.
-               //
-               public static MethodInfo [] FindMethods (Type t, BindingFlags bf,
-                                                        MemberFilter filter, object criteria)
-               {
-                       return null;
-               }
-
                /// <summary>
                ///   Emits the values for the constants
                /// </summary>
@@ -2188,7 +2076,7 @@ namespace Mono.CSharp {
                        return;
                }
 
-               void CheckMemberUsage (MemberCoreArrayList al, string member_type)
+               static void CheckMemberUsage (MemberCoreArrayList al, string member_type)
                {
                        if (al == null)
                                return;
@@ -2249,6 +2137,31 @@ namespace Mono.CSharp {
                        }
                }
 
+               // TODO: move to ClassOrStruct
+               void EmitConstructors ()
+               {
+                       if (instance_constructors == null)
+                               return;
+
+                       if (TypeBuilder.IsSubclassOf (TypeManager.attribute_type) && RootContext.VerifyClsCompliance && IsClsComplianceRequired ()) {
+                               bool has_compliant_args = false;
+
+                               foreach (Constructor c in instance_constructors) {
+                                       c.Emit ();
+
+                                       if (has_compliant_args)
+                                               continue;
+
+                                       has_compliant_args = c.HasCompliantArgs;
+                               }
+                               if (!has_compliant_args)
+                                       Report.Error (3015, Location, "`{0}' has no accessible constructors which use only CLS-compliant types", GetSignatureForError ());
+                       } else {
+                               foreach (Constructor c in instance_constructors)
+                                       c.Emit ();
+                       }
+               }
+
                /// <summary>
                ///   Emits the code, this step is performed after all
                ///   the types, enumerations, constructors
@@ -2256,12 +2169,12 @@ namespace Mono.CSharp {
                public virtual void EmitType ()
                {
                        if (OptAttributes != null)
-                               OptAttributes.Emit (ec, this);
+                               OptAttributes.Emit ();
 
-                       if (IsGeneric && !(this is ClassPart)) {
+                       if (IsGeneric) {
                                int offset = CountTypeParameters - CurrentTypeParameters.Length;
                                for (int i = offset; i < gen_params.Length; i++)
-                                       CurrentTypeParameters [i - offset].EmitAttributes (ec);
+                                       CurrentTypeParameters [i - offset].EmitAttributes ();
                        }
 
                        //
@@ -2287,25 +2200,7 @@ namespace Mono.CSharp {
 
                        Emit ();
 
-                       if (instance_constructors != null) {
-                               if (TypeBuilder.IsSubclassOf (TypeManager.attribute_type) && RootContext.VerifyClsCompliance && IsClsComplianceRequired (this)) {
-                                       bool has_compliant_args = false;
-
-                                       foreach (Constructor c in instance_constructors) {
-                                               c.Emit ();
-
-                                               if (has_compliant_args)
-                                                       continue;
-
-                                               has_compliant_args = c.HasCompliantArgs;
-                                       }
-                                       if (!has_compliant_args)
-                                               Report.Error (3015, Location, "`{0}' has no accessible constructors which use only CLS-compliant types", GetSignatureForError ());
-                               } else {
-                               foreach (Constructor c in instance_constructors)
-                                               c.Emit ();
-                               }
-                       }
+                       EmitConstructors ();
 
                        // Can not continue if constants are broken
                        EmitConstants ();
@@ -2353,22 +2248,13 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       if (parts != null) {
-                               foreach (ClassPart part in parts)
-                                       part.EmitType ();
-                       }
-
-                       if ((Pending != null) && !(this is ClassPart))
-                               if (Pending.VerifyPendingMethods ())
+                       if (pending != null)
+                               if (pending.VerifyPendingMethods ())
                                        return;
 
                        if (iterators != null)
                                foreach (Iterator iterator in iterators)
                                        iterator.EmitType ();
-                       
-//                     if (types != null)
-//                             foreach (TypeContainer tc in types)
-//                                     tc.Emit ();
                }
                
                public override void CloseType ()
@@ -2426,7 +2312,6 @@ namespace Mono.CSharp {
                        indexers = null;
                        operators = null;
                        iterators = null;
-                       ec = null;
                        default_constructor = null;
                        default_static_constructor = null;
                        type_bases = null;
@@ -2516,19 +2401,13 @@ namespace Mono.CSharp {
                        return ok;
                }
 
-               public bool UserDefinedStaticConstructor {
-                       get {
-                               return default_static_constructor != null;
-                       }
-               }
-
                public Constructor DefaultStaticConstructor {
                        get { return default_static_constructor; }
                }
 
-               protected override bool VerifyClsCompliance (DeclSpace ds)
+               protected override bool VerifyClsCompliance ()
                {
-                       if (!base.VerifyClsCompliance (ds))
+                       if (!base.VerifyClsCompliance ())
                                return false;
 
                        VerifyClsName ();
@@ -2538,7 +2417,7 @@ namespace Mono.CSharp {
                                Report.Error (3009, Location, "`{0}': base type `{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (base_type));
                        }
 
-                       if (!Parent.IsClsComplianceRequired (ds)) {
+                       if (!Parent.IsClsComplianceRequired ()) {
                                Report.Error (3018, Location, "`{0}' cannot be marked as CLS-compliant because it is a member of non CLS-compliant type `{1}'", 
                                        GetSignatureForError (), Parent.GetSignatureForError ());
                        }
@@ -2558,7 +2437,7 @@ namespace Mono.CSharp {
 
                        foreach (DictionaryEntry entry in defined_names) {
                                MemberCore mc = (MemberCore)entry.Value;
-                               if (!mc.IsClsComplianceRequired (mc.Parent))
+                               if (!mc.IsClsComplianceRequired ())
                                        continue;
 
                                string name = (string) entry.Key;
@@ -2603,17 +2482,18 @@ namespace Mono.CSharp {
                                }
                        }
                        
+                       Report.SymbolRelatedToPreviousError (mb.InterfaceType);
                        Report.Error (540, mb.Location, "`{0}': containing type does not implement interface `{1}'",
                                mb.GetSignatureForError (), TypeManager.CSharpName (mb.InterfaceType));
                        return false;
                }
 
-               public virtual void Mark_HasEquals ()
+               public void Mark_HasEquals ()
                {
                        Methods.HasEquals = true;
                }
 
-               public virtual void Mark_HasGetHashCode ()
+               public void Mark_HasGetHashCode ()
                {
                        Methods.HasGetHashCode = true;
                }
@@ -2684,411 +2564,219 @@ namespace Mono.CSharp {
                
        }
 
-       public class PartialContainer : TypeContainer {
-
-               public readonly Namespace Namespace;
-               public readonly int OriginalModFlags;
-               public readonly int AllowedModifiers;
-               public readonly TypeAttributes DefaultTypeAttributes;
-               public ListDictionary DeclarativeSecurity;
+       public abstract class ClassOrStruct : TypeContainer {
+               ListDictionary declarative_security;
 
-               static PartialContainer Create (NamespaceEntry ns, TypeContainer parent,
-                                               MemberName member_name, int mod_flags, Kind kind)
+               public ClassOrStruct (NamespaceEntry ns, DeclSpace parent,
+                                     MemberName name, Attributes attrs, Kind kind)
+                       : base (ns, parent, name, attrs, kind)
                {
+               }
 
-                       if (!CheckModFlags (0, mod_flags, member_name))
-                               return null;
-
-                       PartialContainer pc = RootContext.Tree.GetDecl (member_name) as PartialContainer;
-                       if (pc != null) {
-                               if (pc.Kind != kind) {
-                                       Report.Error (
-                                               261, member_name.Location,
-                                               "Partial declarations of `{0}' must be all classes, " +
-                                               "all structs or all interfaces",
-                                               member_name.GetTypeName ());
-                                       return null;
-                               }
-
-                               if (!CheckModFlags (pc.OriginalModFlags, mod_flags, member_name))
-                                       return null;
-                               pc.ModFlags |= (mod_flags & pc.AllowedModifiers);
-
-                               if (pc.IsGeneric) {
-                                       if (pc.CountTypeParameters != member_name.CountTypeArguments) {
-                                               Report.Error (
-                                                       264, member_name.Location,
-                                                       "Partial declarations of `{0}' must have the " +
-                                                       "same type parameter names in the same order",
-                                                       member_name.GetTypeName ());
-                                               return null;
-                                       }
-
-                                       TypeParameterName[] pc_names = pc.MemberName.TypeArguments.GetDeclarations ();
-                                       TypeParameterName[] names = member_name.TypeArguments.GetDeclarations ();
-
-                                       for (int i = 0; i < pc.CountTypeParameters; i++) {
-                                               if (pc_names [i].Name == names [i].Name)
-                                                       continue;
-
-                                               Report.Error (
-                                                       264, member_name.Location,
-                                                       "Partial declarations of `{0}' must have the " +
-                                                       "same type parameter names in the same order",
-                                                       member_name.GetTypeName ());
-                                               return null;
-                                       }
+               protected override bool AddToContainer (MemberCore symbol, string name)
+               {
+                       if (name == MemberName.Name) {
+                               if (symbol is TypeParameter) {
+                                       Report.Error (694, symbol.Location,
+                                                     "Type parameter `{0}' has same name as " +
+                                                     "containing type, or method", name);
+                                       return false;
                                }
 
-                               return pc;
+                               Report.SymbolRelatedToPreviousError (this);
+                               Report.Error (542,  symbol.Location, "`{0}': member names cannot be the same as their enclosing type",
+                                       symbol.GetSignatureForError ());
+                               return false;
                        }
 
-                       if (parent is ClassPart)
-                               parent = ((ClassPart) parent).PartialContainer;
+                       return base.AddToContainer (symbol, name);
+               }
 
-                       pc = new PartialContainer (ns.NS, parent, member_name, mod_flags, kind);
+               public override void VerifyMembers ()
+               {
+                       base.VerifyMembers ();
 
-                       if (kind == Kind.Interface) {
-                               if (!parent.AddInterface (pc))
-                                       return null;
-                       } else if (kind == Kind.Class || kind == Kind.Struct) {
-                               if (!parent.AddClassOrStruct (pc))
-                                       return null;
-                       } else {
-                               throw new InvalidOperationException ();
+                       if ((events != null) && (RootContext.WarningLevel >= 3)) {
+                               foreach (Event e in events){
+                                       if ((e.caching_flags & Flags.IsAssigned) == 0)
+                                               Report.Warning (67, 3, e.Location, "The event `{0}' is never used", e.GetSignatureForError ());
+                               }
                        }
-                       RootContext.Tree.RecordDecl (ns.NS, member_name, pc);
-                       // This is needed to define our type parameters; we define the constraints later.
-                       pc.SetParameterInfo (null);
-                       return pc;
                }
 
-               static bool CheckModFlags (int flags_org, int flags, MemberName member_name)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
                {
-                       // Check (abstract|static|sealed) sanity.
-                       int tmp = (flags_org | flags) & (Modifiers.ABSTRACT | Modifiers.SEALED | Modifiers.STATIC);
-                       if ((tmp & Modifiers.ABSTRACT) != 0) {
-                               if ((tmp & (Modifiers.STATIC | Modifiers.SEALED)) != 0) {
-                                       Report.Error (
-                                               418, member_name.Location, 
-                                               "`{0}': an abstract class cannot be sealed or static", member_name.ToString ());
-                                       return false;
-                               }
-                       } else if (tmp == (Modifiers.SEALED | Modifiers.STATIC)) {
-                               Report.Error (441, member_name.Location, "`{0}': a class cannot be both static and sealed", member_name.ToString ());
-                               return false;
-                       }
+                       if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
+                               if (declarative_security == null)
+                                       declarative_security = new ListDictionary ();
 
-                       if (flags_org == 0)
-                               return true;
+                               a.ExtractSecurityPermissionSet (declarative_security);
+                               return;
+                       }
 
-                       // Check conflicts.
-                       if (0 != ((flags_org ^ flags) & (0xFFFFFFFF ^ (Modifiers.SEALED | Modifiers.ABSTRACT)))) {
-                               Report.Error (
-                                       262, member_name.Location, "Partial declarations of `{0}' " +
-                                       "have conflicting accessibility modifiers",
-                                       member_name.GetName ());
-                               return false;
+                       if (a.Type == TypeManager.struct_layout_attribute_type && a.GetLayoutKindValue () == LayoutKind.Explicit) {
+                               HasExplicitLayout = true;
                        }
-                       return true;
+
+                       base.ApplyAttributeBuilder (a, cb);
                }
 
-               public static ClassPart CreatePart (NamespaceEntry ns, TypeContainer parent,
-                                                   MemberName name, int mod, Attributes attrs,
-                                                   Kind kind, Location loc)
+               /// <summary>
+               /// Defines the default constructors 
+               /// </summary>
+               protected void DefineDefaultConstructor (bool is_static)
                {
-                       PartialContainer pc = Create (ns, parent, name, mod, kind);
-                       if (pc == null) {
-                               // An error occured; create a dummy container, but don't
-                               // register it.
-                               pc = new PartialContainer (ns.NS, parent, name, mod, kind);
+                       // The default instance constructor is public
+                       // If the class is abstract, the default constructor is protected
+                       // The default static constructor is private
+
+                       int mods;
+                       if (is_static) {
+                               mods = Modifiers.STATIC | Modifiers.PRIVATE;
+                       } else {
+                               mods = ((ModFlags & Modifiers.ABSTRACT) != 0) ? Modifiers.PROTECTED : Modifiers.PUBLIC;
                        }
 
-                       ClassPart part = new ClassPart (ns, pc, parent, mod, attrs, kind);
-                       pc.AddPart (part);
-                       return part;
+                       Constructor c = new Constructor (this, MemberName.Name, mods,
+                               Parameters.EmptyReadOnlyParameters,
+                               new GeneratedBaseInitializer (Location),
+                               Location);
+                       
+                       AddConstructor (c);
+                       c.Block = new ToplevelBlock (null, Location);
                }
 
-               protected PartialContainer (Namespace ns, TypeContainer parent,
-                                           MemberName name, int mod, Kind kind)
-                       : base (null, parent, name, null, kind)
+               void DefineFieldInitializers ()
                {
-                       this.Namespace = ns;
-
-                       switch (kind) {
-                       case Kind.Class:
-                               AllowedModifiers = Class.AllowedModifiers;
-                               DefaultTypeAttributes = Class.DefaultTypeAttributes;
-                               break;
+                       if (initialized_fields != null) {
+                               for (int i = 0; i < initialized_fields.Count; ++i) {
+                                       FieldBase fb = (FieldBase)initialized_fields[i];
+                                       fb.ResolveInitializer ();
+                                       if (fb.HasDefaultInitializer && RootContext.Optimize) {
+                                               // Field is re-initialized to its default value => removed
+                                               initialized_fields.RemoveAt (i);
+                                               --i;
+                                       }
+                               }
+                       }
 
-                       case Kind.Struct:
-                               AllowedModifiers = Struct.AllowedModifiers;
-                               DefaultTypeAttributes = Struct.DefaultTypeAttributes;
-                               break;
+                       if (initialized_static_fields != null) {
+                               bool has_complex_initializer = false;
 
-                       case Kind.Interface:
-                               AllowedModifiers = Interface.AllowedModifiers;
-                               DefaultTypeAttributes = Interface.DefaultTypeAttributes;
-                               break;
+                               foreach (FieldBase fb in initialized_static_fields) {
+                                       if (fb.ResolveInitializer () is Constant)
+                                               continue;
 
-                       default:
-                               throw new InvalidOperationException ();
-                       }
+                                       has_complex_initializer = true;
+                               }
 
-                       int accmods;
-                       if (parent.Parent == null)
-                               accmods = Modifiers.INTERNAL;
-                       else
-                               accmods = Modifiers.PRIVATE;
+                               // Need special check to not optimize code like this
+                               // static int a = b = 5;
+                               // static int b = 0;
+                               if (!has_complex_initializer && RootContext.Optimize) {
+                                       for (int i = 0; i < initialized_static_fields.Count; ++i) {
+                                               FieldBase fb = (FieldBase)initialized_static_fields[i];
+                                               if (fb.HasDefaultInitializer) {
+                                                       initialized_static_fields.RemoveAt (i);
+                                                       --i;
+                                               }
+                                       }
+                               }
 
-                       // FIXME: remove this nasty fix for bug #77370 when
-                       // we get good AllowModifiersProp implementation.
-                       if ((mod & Modifiers.STATIC) != 0) {
-                               AllowedModifiers |= Modifiers.STATIC;
-                               AllowedModifiers &= ~ (Modifiers.ABSTRACT | Modifiers.SEALED);
+                               if (default_static_constructor == null && initialized_static_fields.Count > 0) {
+                                       DefineDefaultConstructor (true);
+                               }
                        }
 
-                       this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
-                       this.OriginalModFlags = mod;
                }
 
-               public override void EmitType ()
+               public override bool Define ()
                {
-                       base.EmitType ();
+                       DefineFieldInitializers ();
 
-                       if (DeclarativeSecurity != null) {
-                               foreach (DictionaryEntry de in DeclarativeSecurity) {
-                                       TypeBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
-                               }
-                       }
-               }
+                       if (default_static_constructor != null)
+                               default_static_constructor.Define ();
 
-               public override PendingImplementation GetPendingImplementations ()
-               {
-                       return PendingImplementation.GetPendingImplementations (this);
+                       return base.Define ();
                }
 
-               public override bool MarkForDuplicationCheck ()
+               public override void Emit ()
                {
-                       return true;
+                       base.Emit ();
+
+                       if (declarative_security != null) {
+                               foreach (DictionaryEntry de in declarative_security) {
+                                       TypeBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
+                               }
+                       }
                }
 
                protected override TypeAttributes TypeAttr {
                        get {
-                               return base.TypeAttr | DefaultTypeAttributes;
+                               if (default_static_constructor == null)
+                                       return base.TypeAttr | TypeAttributes.BeforeFieldInit;
+
+                               return base.TypeAttr;
                        }
                }
        }
 
-       public class ClassPart : TypeContainer, IMemberContainer {
-               public readonly PartialContainer PartialContainer;
-               public readonly bool IsPartial;
 
-               Constraints[] constraints;
+       // TODO: should be sealed
+       public class Class : ClassOrStruct {
+               const int AllowedModifiers =
+                       Modifiers.NEW |
+                       Modifiers.PUBLIC |
+                       Modifiers.PROTECTED |
+                       Modifiers.INTERNAL |
+                       Modifiers.PRIVATE |
+                       Modifiers.ABSTRACT |
+                       Modifiers.SEALED |
+                       Modifiers.STATIC |
+                       Modifiers.UNSAFE;
 
-               public ClassPart (NamespaceEntry ns, PartialContainer pc, TypeContainer parent,
-                                 int mod, Attributes attrs, Kind kind)
-                       : base (ns, parent, pc.MemberName, attrs, kind)
+               public Class (NamespaceEntry ns, DeclSpace parent, MemberName name, int mod,
+                             Attributes attrs)
+                       : base (ns, parent, name, attrs, Kind.Class)
                {
-                       this.PartialContainer = pc;
-                       this.IsPartial = true;
+                       int accmods = Parent.Parent == null ? Modifiers.INTERNAL : Modifiers.PRIVATE;
+                       this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
 
-                       int accmods;
-                       if (parent == null || parent == RootContext.Tree.Types)
-                               accmods = Modifiers.INTERNAL;
-                       else
-                               accmods = Modifiers.PRIVATE;
-
-                       this.ModFlags = Modifiers.Check (pc.AllowedModifiers, mod, accmods, pc.MemberName.Location);
-
-                       if (pc.IsGeneric)
-                               constraints = new Constraints [pc.CountCurrentTypeParameters];
-               }
-
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
-               {
-                       if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
-                               if (PartialContainer.DeclarativeSecurity == null)
-                                       PartialContainer.DeclarativeSecurity = new ListDictionary ();
-
-                               a.ExtractSecurityPermissionSet (PartialContainer.DeclarativeSecurity);
-                               return;
-                       }
-
-                       base.ApplyAttributeBuilder (a, cb);
-               }
-
-               public override PendingImplementation GetPendingImplementations ()
-               {
-                       return PartialContainer.Pending;
-               }
-
-               public override bool VerifyImplements (MemberBase mb)
-               {
-                       return PartialContainer.VerifyImplements (mb);
-               }
-
-               public override void SetParameterInfo (ArrayList constraints_list)
-               {
-                       if (constraints_list == null)
-                               return;
-
-                       TypeParameter[] current_params = PartialContainer.CurrentTypeParameters;
-                       for (int i = 0; i < constraints.Length; i++) {
-                               foreach (Constraints constraint in constraints_list) {
-                                       if (constraint.TypeParameter == current_params [i].Name) {
-                                               constraints [i] = constraint;
-                                               break;
-                                       }
-                               }
+                       if (IsStatic && RootContext.Version == LanguageVersion.ISO_1) {
+                               Report.FeatureIsNotStandardized (Location, "static classes");
                        }
                }
 
-               public bool DefineTypeParameters ()
+               public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
                {
-                       TypeParameter[] current_params = PartialContainer.CurrentTypeParameters;
-
-                       for (int i = 0; i < current_params.Length; i++) {
-                               Constraints new_constraints = constraints [i];
-                               if (new_constraints == null)
-                                       continue;
-
-                               if (!current_params [i].UpdateConstraints (ec, new_constraints)) {
-                                       Report.Error (265, Location, "Partial declarations of `{0}' have " +
-                                                     "inconsistent constraints for type parameter `{1}'.",
-                                                     MemberName.GetTypeName (), current_params [i].Name);
-                                       return false;
+                       if (a.Type == TypeManager.attribute_usage_type) {
+                               if (BaseType != TypeManager.attribute_type && !BaseType.IsSubclassOf (TypeManager.attribute_type) &&
+                                       TypeBuilder.FullName != "System.Attribute") {
+                                       Report.Error (641, a.Location, "Attribute `{0}' is only valid on classes derived from System.Attribute", a.GetSignatureForError ());
                                }
                        }
 
-                       for (int i = 0; i < current_params.Length; i++) {
-                               if (!current_params [i].Resolve (this))
-                                       return false;
-                       }
-
-                       foreach (TypeParameter type_param in PartialContainer.TypeParameters) {
-                               if (!type_param.DefineType (ec))
-                                       return false;
-                       }
-
-                       return true;
-               }
-
-               public override void RegisterFieldForInitialization (FieldBase field)
-               {
-                       PartialContainer.RegisterFieldForInitialization (field);
-               }
-
-               public override bool EmitFieldInitializers (EmitContext ec)
-               {
-                       return PartialContainer.EmitFieldInitializers (ec);
-               }
-
-               public override Type FindNestedType (string name)
-               {
-                       return PartialContainer.FindNestedType (name);
-               }
-
-               public override MemberCache BaseCache {
-                       get {
-                               return PartialContainer.BaseCache;
-                       }
-               }
-
-               public override TypeBuilder DefineType ()
-               {
-                       throw new InternalErrorException ("Should not get here");
-               }
-
-               public override void Mark_HasEquals ()
-               {
-                       PartialContainer.Mark_HasEquals ();
-               }
-
-               public override void Mark_HasGetHashCode ()
-               {
-                       PartialContainer.Mark_HasGetHashCode ();
-               }
-       }
-
-       public abstract class ClassOrStruct : TypeContainer {
-               bool has_explicit_layout = false;
-               ListDictionary declarative_security;
-
-               public ClassOrStruct (NamespaceEntry ns, TypeContainer parent,
-                                     MemberName name, Attributes attrs, Kind kind)
-                       : base (ns, parent, name, attrs, kind)
-               {
-               }
-
-               public override PendingImplementation GetPendingImplementations ()
-               {
-                       return PendingImplementation.GetPendingImplementations (this);
-               }
-
-               public override bool HasExplicitLayout {
-                       get {
-                               return has_explicit_layout;
-                               }
+                       if (a.Type == TypeManager.conditional_attribute_type &&
+                               !(BaseType == TypeManager.attribute_type || BaseType.IsSubclassOf (TypeManager.attribute_type))) {
+                               Report.Error (1689, a.Location, "Attribute `System.Diagnostics.ConditionalAttribute' is only valid on methods or attribute classes");
+                               return;
                        }
 
-               public override void VerifyMembers ()
-               {
-                       base.VerifyMembers ();
-
-                       if ((events != null) && (RootContext.WarningLevel >= 3)) {
-                               foreach (Event e in events){
-                                       if ((e.caching_flags & Flags.IsAssigned) == 0)
-                                               Report.Warning (67, 3, e.Location, "The event `{0}' is never used", e.GetSignatureForError ());
-                               }
+                       if (a.Type == TypeManager.comimport_attr_type &&
+                               !attributes.Contains (TypeManager.guid_attr_type)) {
+                                       a.Error_MissingGuidAttribute ();
+                                       return;
                        }
-               }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
-               {
-                       if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
-                               if (declarative_security == null)
-                                       declarative_security = new ListDictionary ();
-
-                               a.ExtractSecurityPermissionSet (declarative_security);
+                       if (AttributeTester.IsAttributeExcluded (a.Type))
                                return;
-                       }
-
-                       if (a.Type == TypeManager.struct_layout_attribute_type && a.GetLayoutKindValue () == LayoutKind.Explicit)
-                               has_explicit_layout = true;
 
                        base.ApplyAttributeBuilder (a, cb);
                }
 
-               public override void Emit()
-               {
-                       base.Emit ();
-
-                       if (declarative_security != null) {
-                               foreach (DictionaryEntry de in declarative_security) {
-                                       TypeBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
-                               }
-                       }
-               }
-       }
-
-       /// <summary>
-       /// Class handles static classes declaration
-       /// </summary>
-       public sealed class StaticClass: Class {
-               public StaticClass (NamespaceEntry ns, TypeContainer parent, MemberName name, int mod,
-                                   Attributes attrs)
-                       : base (ns, parent, name, mod, attrs)
-               {
-                       if (RootContext.Version == LanguageVersion.ISO_1) {
-                               Report.FeatureIsNotStandardized (Location, "static classes");
-                       }
-               }
-
-               protected override int AllowedModifiersProp {
+               public override AttributeTargets AttributeTargets {
                        get {
-                               return Modifiers.NEW | Modifiers.PUBLIC | Modifiers.PROTECTED | Modifiers.INTERNAL | Modifiers.PRIVATE |
-                                       Modifiers.STATIC | Modifiers.UNSAFE;
+                               return AttributeTargets.Class;
                        }
                }
 
@@ -3097,6 +2785,11 @@ namespace Mono.CSharp {
                        if (list == null)
                                return;
 
+                       if (!IsStatic) {
+                               base.DefineContainerMembers (list);
+                               return;
+                       }
+
                        foreach (MemberCore m in list) {
                                if (m is Operator) {
                                        Report.Error (715, m.Location, "`{0}': Static classes cannot contain user-defined operators", m.GetSignatureForError ());
@@ -3132,98 +2825,92 @@ namespace Mono.CSharp {
                        base.DefineContainerMembers (list);
                }
 
-               public override TypeBuilder DefineType()
+               public override TypeBuilder DefineType ()
                {
-                       if ((ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) == (Modifiers.SEALED | Modifiers.STATIC)) {
-                               Report.Error (441, Location, "`{0}': a class cannot be both static and sealed", GetSignatureForError ());
+                       if ((ModFlags & Modifiers.ABSTRACT) == Modifiers.ABSTRACT && (ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) != 0) {
+                               Report.Error (418, Location, "`{0}': an abstract class cannot be sealed or static", GetSignatureForError ());
                                return null;
                        }
 
-                       TypeBuilder tb = base.DefineType ();
-                       if (tb == null)
-                               return null;
-
-                       if (ptype != TypeManager.object_type) {
-                               Report.Error (713, Location, "Static class `{0}' cannot derive from type `{1}'. Static classes must derive from object", GetSignatureForError (), TypeManager.CSharpName (ptype));
+                       if ((ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) == (Modifiers.SEALED | Modifiers.STATIC)) {
+                               Report.Error (441, Location, "`{0}': a class cannot be both static and sealed", GetSignatureForError ());
                                return null;
                        }
 
-                       if (ifaces != null) {
-                               foreach (Type t in ifaces)
-                                       Report.SymbolRelatedToPreviousError (t);
-                               Report.Error (714, Location, "`{0}': static classes cannot implement interfaces", GetSignatureForError ());
-                       }
-                       return tb;
+                       return base.DefineType ();
                }
 
-               protected override TypeAttributes TypeAttr {
-                       get {
-                               return base.TypeAttr | TypeAttributes.Abstract | TypeAttributes.Sealed;
-                       }
-               }
-       }
-
-       public class Class : ClassOrStruct {
-               // TODO: remove this and use only AllowedModifiersProp to fix partial classes bugs
-               public const int AllowedModifiers =
-                       Modifiers.NEW |
-                       Modifiers.PUBLIC |
-                       Modifiers.PROTECTED |
-                       Modifiers.INTERNAL |
-                       Modifiers.PRIVATE |
-                       Modifiers.ABSTRACT |
-                       Modifiers.SEALED |
-                       Modifiers.UNSAFE;
-
-               public Class (NamespaceEntry ns, TypeContainer parent, MemberName name, int mod,
-                             Attributes attrs)
-                       : base (ns, parent, name, attrs, Kind.Class)
+               protected override bool DoDefineMembers ()
                {
-                       this.ModFlags = mod;
-               }
+                       if (InstanceConstructors == null && !IsStatic)
+                               DefineDefaultConstructor (false);
 
-               virtual protected int AllowedModifiersProp {
-                       get {
-                               return AllowedModifiers;
-                       }
+                       return base.DoDefineMembers ();
                }
 
-               public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
+               public override TypeExpr[] GetClassBases (out TypeExpr base_class)
                {
-                       if (a.Type == TypeManager.attribute_usage_type) {
-                               if (ptype != TypeManager.attribute_type &&
-                                   !ptype.IsSubclassOf (TypeManager.attribute_type) &&
-                                   TypeBuilder.FullName != "System.Attribute") {
-                                       Report.Error (641, a.Location, "Attribute `{0}' is only valid on classes derived from System.Attribute", a.GetSignatureForError ());
-                               }
-                       }
+                       TypeExpr[] ifaces = base.GetClassBases (out base_class);
 
-                       if (a.Type == TypeManager.conditional_attribute_type &&
-                               !(ptype == TypeManager.attribute_type || ptype.IsSubclassOf (TypeManager.attribute_type))) {
-                               Report.Error (1689, a.Location, "Attribute 'System.Diagnostics.ConditionalAttribute' is only valid on methods or attribute classes");
-                               return;
-                       }
+                       if (base_class == null) {
+                               if (RootContext.StdLib)
+                                       base_class = TypeManager.system_object_expr;
+                               else if (Name != "System.Object")
+                                       base_class = TypeManager.system_object_expr;
+                       } else {
+                               if (Kind == Kind.Class && base_class is TypeParameterExpr){
+                                       Report.Error (
+                                               689, base_class.Location,
+                                               "Cannot derive from `{0}' because it is a type parameter",
+                                               base_class.GetSignatureForError ());
+                                       return ifaces;
+                               }
 
-                       if (AttributeTester.IsAttributeExcluded (a.Type))
-                               return;
+                               if (base_class.Type.IsArray || base_class.Type.IsPointer) {
+                                       Report.Error (1521, base_class.Location, "Invalid base type");
+                                       return ifaces;
+                               }
 
-                       base.ApplyAttributeBuilder (a, cb);
-               }
+                               if (base_class.IsSealed){
+                                       Report.SymbolRelatedToPreviousError (base_class.Type);
+                                       if (base_class.Type.IsAbstract) {
+                                               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}'",
+                                                       GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
+                                       }
+                                       return ifaces;
+                               }
 
-               public const TypeAttributes DefaultTypeAttributes =
-                       TypeAttributes.AutoLayout | TypeAttributes.Class;
+                               if (!base_class.CanInheritFrom ()){
+                                       Report.Error (644, Location, "`{0}' cannot derive from special class `{1}'",
+                                               GetSignatureForError (), base_class.GetSignatureForError ());
+                                       return ifaces;
+                               }
 
-               public override TypeBuilder DefineType()
-               {
-                       if ((ModFlags & Modifiers.ABSTRACT) == Modifiers.ABSTRACT && (ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) != 0) {
-                               Report.Error (418, Location, "`{0}': an abstract class cannot be sealed or static", GetSignatureForError ());
-                               return null;
+                               if (!base_class.AsAccessible (this, ModFlags)) {
+                                       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 ());
+                               }
                        }
 
-                       int accmods = Parent.Parent == null ? Modifiers.INTERNAL : Modifiers.PRIVATE;
-                       ModFlags = Modifiers.Check (AllowedModifiersProp, ModFlags, accmods, Location);
+                       if (IsStatic) {
+                               if (base_class != TypeManager.system_object_expr) {
+                                       Report.Error (713, Location, "Static class `{0}' cannot derive from type `{1}'. Static classes must derive from object",
+                                               GetSignatureForError (), base_class.GetSignatureForError ());
+                                       return ifaces;
+                               }
 
-                       return base.DefineType ();
+                               if (ifaces != null) {
+                                       foreach (TypeExpr t in ifaces)
+                                               Report.SymbolRelatedToPreviousError (t.Type);
+                                       Report.Error (714, Location, "`{0}': static classes cannot implement interfaces", GetSignatureForError ());
+                               }
+                       }
+
+                       return ifaces;
                }
 
                /// Search for at least one defined condition in ConditionalAttribute of attribute class
@@ -3238,13 +2925,13 @@ namespace Mono.CSharp {
                        if (OptAttributes == null)
                                return false;
 
-                       Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type, ec);
+                       Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type);
 
                        if (attrs == null)
                                return false;
 
                        foreach (Attribute a in attrs) {
-                               string condition = a.GetConditionalAttributeValue (Parent.EmitContext);
+                               string condition = a.GetConditionalAttributeValue ();
                                if (RootContext.AllDefines.Contains (condition))
                                        return false;
                        }
@@ -3253,22 +2940,31 @@ namespace Mono.CSharp {
                        return true;
                }
 
+               bool IsStatic {
+                       get {
+                               return (ModFlags & Modifiers.STATIC) != 0;
+                       }
+               }
+
                //
                // FIXME: How do we deal with the user specifying a different
                // layout?
                //
                protected override TypeAttributes TypeAttr {
                        get {
-                               return base.TypeAttr | DefaultTypeAttributes;
+                               TypeAttributes ta = base.TypeAttr | TypeAttributes.AutoLayout | TypeAttributes.Class;
+                               if (IsStatic)
+                                       ta |= TypeAttributes.Abstract | TypeAttributes.Sealed;
+                               return ta;
                        }
                }
        }
 
-       public class Struct : ClassOrStruct {
+       public sealed class Struct : ClassOrStruct {
                // <summary>
                //   Modifiers allowed in a struct declaration
                // </summary>
-               public const int AllowedModifiers =
+               const int AllowedModifiers =
                        Modifiers.NEW       |
                        Modifiers.PUBLIC    |
                        Modifiers.PROTECTED |
@@ -3276,7 +2972,7 @@ namespace Mono.CSharp {
                        Modifiers.UNSAFE    |
                        Modifiers.PRIVATE;
 
-               public Struct (NamespaceEntry ns, TypeContainer parent, MemberName name,
+               public Struct (NamespaceEntry ns, DeclSpace parent, MemberName name,
                               int mod, Attributes attrs)
                        : base (ns, parent, name, attrs, Kind.Struct)
                {
@@ -3292,11 +2988,36 @@ namespace Mono.CSharp {
                        this.ModFlags |= Modifiers.SEALED;
                }
 
-               public const TypeAttributes DefaultTypeAttributes =
+               public override AttributeTargets AttributeTargets {
+                       get {
+                               return AttributeTargets.Struct;
+                       }
+               }
+
+               const TypeAttributes DefaultTypeAttributes =
                        TypeAttributes.SequentialLayout |
                        TypeAttributes.Sealed |
                        TypeAttributes.BeforeFieldInit;
 
+
+               public override TypeExpr[] GetClassBases (out TypeExpr base_class)
+               {
+                       TypeExpr[] ifaces = base.GetClassBases (out base_class);
+                       //
+                       // If we are compiling our runtime,
+                       // and we are defining ValueType, then our
+                       // base is `System.Object'.
+                       //
+                       if (base_class == null) {
+                               if (!RootContext.StdLib && Name == "System.ValueType")
+                                       base_class = TypeManager.system_object_expr;
+                               else
+                                       base_class = TypeManager.system_valuetype_expr;
+                       }
+
+                       return ifaces;
+               }
+
                //
                // FIXME: Allow the user to specify a different set of attributes
                // in some cases (Sealed for example is mandatory for a class,
@@ -3307,12 +3028,23 @@ namespace Mono.CSharp {
                                return base.TypeAttr | DefaultTypeAttributes;
                        }
                }
+
+               public override void RegisterFieldForInitialization (FieldBase field)
+               {
+                       if ((field.ModFlags & Modifiers.STATIC) == 0) {
+                               Report.Error (573, field.Location, "`{0}': Structs cannot have instance field initializers",
+                                       field.GetSignatureForError ());
+                               return;
+                       }
+                       base.RegisterFieldForInitialization (field);
+               }
+
        }
 
        /// <summary>
        ///   Interfaces
        /// </summary>
-       public class Interface : TypeContainer, IMemberContainer {
+       public sealed class Interface : TypeContainer, IMemberContainer {
 
                /// <summary>
                ///   Modifiers allowed in a class declaration
@@ -3325,7 +3057,7 @@ namespace Mono.CSharp {
                        Modifiers.UNSAFE    |
                        Modifiers.PRIVATE;
 
-               public Interface (NamespaceEntry ns, TypeContainer parent, MemberName name, int mod,
+               public Interface (NamespaceEntry ns, DeclSpace parent, MemberName name, int mod,
                                  Attributes attrs)
                        : base (ns, parent, name, attrs, Kind.Interface)
                {
@@ -3339,12 +3071,24 @@ namespace Mono.CSharp {
                        this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, name.Location);
                }
 
-               public override PendingImplementation GetPendingImplementations ()
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
                {
-                       return null;
+                       if (a.Type == TypeManager.comimport_attr_type &&
+                               !attributes.Contains (TypeManager.guid_attr_type)) {
+                                       a.Error_MissingGuidAttribute ();
+                                       return;
+                       }
+                       base.ApplyAttributeBuilder (a, cb);
+               }
+
+
+               public override AttributeTargets AttributeTargets {
+                       get {
+                               return AttributeTargets.Interface;
+                       }
                }
 
-               public const TypeAttributes DefaultTypeAttributes =
+               const TypeAttributes DefaultTypeAttributes =
                                        TypeAttributes.AutoLayout |
                                        TypeAttributes.Abstract |
                                        TypeAttributes.Interface;
@@ -3355,9 +3099,9 @@ namespace Mono.CSharp {
                        }
                }
 
-               protected override bool VerifyClsCompliance (DeclSpace ds)
+               protected override bool VerifyClsCompliance ()
                {
-                       if (!base.VerifyClsCompliance (ds))
+                       if (!base.VerifyClsCompliance ())
                                return false;
 
                        if (ifaces != null) {
@@ -3378,18 +3122,13 @@ namespace Mono.CSharp {
        public abstract class MethodCore : MemberBase {
                public readonly Parameters Parameters;
                protected ToplevelBlock block;
-               
-               // Whether this is an operator method.
-               public Operator IsOperator;
 
                //
                // The method we're overriding if this is an override method.
                //
                protected MethodInfo base_method = null;
 
-               static string[] attribute_targets = new string [] { "method", "return" };
-
-               public MethodCore (TypeContainer parent, GenericMethod generic,
+               public MethodCore (DeclSpace parent, GenericMethod generic,
                                   Expression type, int mod, int allowed_mod, bool is_iface,
                                   MemberName name, Attributes attrs, Parameters parameters)
                        : base (parent, generic, type, mod, allowed_mod, Modifiers.PRIVATE,
@@ -3415,10 +3154,6 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override EmitContext EmitContext {
-                       get { return ds.EmitContext; }
-               }
-               
                public ToplevelBlock Block {
                        get {
                                return block;
@@ -3450,7 +3185,7 @@ namespace Mono.CSharp {
                                return true;
 
                        // Is null for System.Object while compiling corlib and base interfaces
-                       if (Parent.BaseCache == null) {
+                       if (ParentContainer.BaseCache == null) {
                                if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
                                        Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
                                }
@@ -3458,7 +3193,7 @@ namespace Mono.CSharp {
                        }
 
                        Type base_ret_type = null;
-                       base_method = FindOutBaseMethod (Parent, ref base_ret_type);
+                       base_method = FindOutBaseMethod (ref base_ret_type);
 
                        // method is override
                        if (base_method != null) {
@@ -3494,15 +3229,14 @@ namespace Mono.CSharp {
                                }
 
                                if (Name == "Equals" && Parameters.Count == 1 && ParameterTypes [0] == TypeManager.object_type)
-                                       Parent.Mark_HasEquals ();
+                                       ParentContainer.Mark_HasEquals ();
                                else if (Name == "GetHashCode" && Parameters.Empty)
-                                       Parent.Mark_HasGetHashCode ();
+                                       ParentContainer.Mark_HasGetHashCode ();
 
                                if ((ModFlags & Modifiers.OVERRIDE) != 0) {
                                        ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (base_method);
                                        if (oa != null) {
-                                               EmitContext ec = new EmitContext (this.Parent, this.Parent, Location, null, null, ModFlags, false);
-                                               if (OptAttributes == null || !OptAttributes.Contains (TypeManager.obsolete_attribute_type, ec)) {
+                                               if (OptAttributes == null || !OptAttributes.Contains (TypeManager.obsolete_attribute_type)) {
                                                        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) );
@@ -3512,7 +3246,7 @@ namespace Mono.CSharp {
                                return true;
                        }
 
-                       MemberInfo conflict_symbol = Parent.FindBaseMemberWithSameName (Name, !(this is Property));
+                       MemberInfo conflict_symbol = ParentContainer.FindBaseMemberWithSameName (Name, !(this is Property));
                        if ((ModFlags & Modifiers.OVERRIDE) != 0) {
                                if (conflict_symbol != null) {
                                        Report.SymbolRelatedToPreviousError (conflict_symbol);
@@ -3646,7 +3380,7 @@ namespace Mono.CSharp {
 
                public bool CheckAbstractAndExtern (bool has_block)
                {
-                       if (Parent.Kind == Kind.Interface)
+                       if (ParentContainer.Kind == Kind.Interface)
                                return true;
 
                        if (has_block) {
@@ -3700,30 +3434,16 @@ namespace Mono.CSharp {
                /// <summary>
                /// Gets base method and its return type
                /// </summary>
-               protected abstract MethodInfo FindOutBaseMethod (TypeContainer container, ref Type base_ret_type);
+               protected abstract MethodInfo FindOutBaseMethod (ref Type base_ret_type);
 
                protected bool DoDefineParameters ()
                {
-                       EmitContext ec = ds.EmitContext;
-                       if (ec == null)
-                               throw new InternalErrorException ("DoDefineParameters invoked too early");
-
-                       bool old_unsafe = ec.InUnsafe;
-                       ec.InUnsafe = InUnsafe;
-                       ec.ResolvingGenericMethod = GenericMethod != null;
-
-                       bool old_obsolete = ec.TestObsoleteMethodUsage;
-                       if (GetObsoleteAttribute () != null || Parent.GetObsoleteAttribute () != null)
-                               ec.TestObsoleteMethodUsage = false;
+                       IResolveContext rc = GenericMethod == null ? this : (IResolveContext)ds;
 
                        // Check if arguments were correct
-                       if (!Parameters.Resolve (ec))
+                       if (!Parameters.Resolve (rc))
                                return false;
 
-                       ec.ResolvingGenericMethod = false;
-                       ec.InUnsafe = old_unsafe;
-                       ec.TestObsoleteMethodUsage = old_obsolete;
-
                        return CheckParameters (ParameterTypes);
                }
 
@@ -3740,8 +3460,6 @@ namespace Mono.CSharp {
                                }
 
                                if (partype.IsPointer){
-                                       if (!UnsafeOK (ds))
-                                               error = true;
                                        if (!TypeManager.VerifyUnManaged (TypeManager.GetElementType (partype), Location))
                                                error = true;
                                }
@@ -3749,12 +3467,13 @@ namespace Mono.CSharp {
                                if (ds.AsAccessible (partype, ModFlags))
                                        continue;
 
+                               Report.SymbolRelatedToPreviousError (partype);
                                if (this is Indexer)
                                        Report.Error (55, Location,
                                                "Inconsistent accessibility: parameter type `" +
                                                TypeManager.CSharpName (partype) + "' is less " +
                                                "accessible than indexer `" + GetSignatureForError () + "'");
-                               else if ((this is Method) && ((Method) this).IsOperator != null)
+                               else if (this is Operator)
                                        Report.Error (57, Location,
                                                "Inconsistent accessibility: parameter type `" +
                                                TypeManager.CSharpName (partype) + "' is less " +
@@ -3770,16 +3489,10 @@ namespace Mono.CSharp {
                        return !error;
                }
 
-               public override string[] ValidAttributeTargets {
-                       get {
-                               return attribute_targets;
-                       }
-               }
-
-               protected override bool VerifyClsCompliance (DeclSpace ds)
+               protected override bool VerifyClsCompliance ()
                {
-                       if (!base.VerifyClsCompliance (ds)) {
-                               if ((ModFlags & Modifiers.ABSTRACT) != 0 && IsExposedFromAssembly (ds) && ds.IsClsComplianceRequired (ds)) {
+                       if (!base.VerifyClsCompliance ()) {
+                               if ((ModFlags & Modifiers.ABSTRACT) != 0 && IsExposedFromAssembly () && Parent.IsClsComplianceRequired ()) {
                                        Report.Error (3011, Location, "`{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
                                }
                                return false;
@@ -3850,71 +3563,323 @@ namespace Mono.CSharp {
                                                Report.Error (663, Location, "`{0}': Methods cannot differ only on their use of ref and out on a parameters",
                                                              GetSignatureForError ());
                                                return false;
-                                       }} catch {
-                                               Console.WriteLine ("Method is: {0} {1}", method.Location, method);
-                                               Console.WriteLine ("this is: {0} {1}", Location, this);
-                                       }
-                               }
+                                       }} catch {
+                                               Console.WriteLine ("Method is: {0} {1}", method.Location, method);
+                                               Console.WriteLine ("this is: {0} {1}", Location, this);
+                                       }
+                               }
+
+                               Report.SymbolRelatedToPreviousError (method);
+                               if (this is Operator && method is Operator)
+                                       Report.Error (557, Location, "Duplicate user-defined conversion in type `{0}'", Parent.Name);
+                               else
+                                       Report.Error (111, Location, TypeContainer.Error111, GetSignatureForError ());
+
+                               return true;
+                       }
+
+                       return false;
+               }
+
+               public override bool IsUsed {
+                       get { return IsExplicitImpl || base.IsUsed; }
+               }
+
+               //
+               // Returns a string that represents the signature for this 
+               // member which should be used in XML documentation.
+               //
+               public override string GetDocCommentName (DeclSpace ds)
+               {
+                       return DocUtil.GetMethodDocCommentName (this, ds);
+               }
+
+               //
+               // Raised (and passed an XmlElement that contains the comment)
+               // when GenerateDocComment is writing documentation expectedly.
+               //
+               // FIXME: with a few effort, it could be done with XmlReader,
+               // that means removal of DOM use.
+               //
+               internal override void OnGenerateDocComment (XmlElement el)
+               {
+                       DocUtil.OnMethodGenerateDocComment (this, el);
+               }
+
+               //
+               //   Represents header string for documentation comment.
+               //
+               public override string DocCommentHeader {
+                       get { return "M:"; }
+               }
+
+       }
+
+       public abstract class MethodOrOperator : MethodCore, IMethodData
+       {
+               public MethodBuilder MethodBuilder;
+               ReturnParameter return_attributes;
+               ListDictionary declarative_security;
+               protected MethodData MethodData;
+
+               static string[] attribute_targets = new string [] { "method", "return" };
+
+               protected MethodOrOperator (DeclSpace parent, GenericMethod generic, Expression type, int mod,
+                               int allowed_mod, bool is_interface, MemberName name,
+                               Attributes attrs, Parameters parameters)
+                       : base (parent, generic, type, mod, allowed_mod, is_interface, name,
+                                       attrs, parameters)
+               {
+               }
+
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               {
+                       if (a.Target == AttributeTargets.ReturnValue) {
+                               if (return_attributes == null)
+                                       return_attributes = new ReturnParameter (MethodBuilder, Location);
+
+                               return_attributes.ApplyAttributeBuilder (a, cb);
+                               return;
+                       }
+
+                       if (a.Type == TypeManager.methodimpl_attr_type &&
+                               (a.GetMethodImplOptions () & MethodImplOptions.InternalCall) != 0) {
+                               MethodBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
+                       }
+
+                       if (a.Type == TypeManager.dllimport_type) {
+                               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'");
+                               }
+
+                               return;
+                       }
+
+                       if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
+                               if (declarative_security == null)
+                                       declarative_security = new ListDictionary ();
+                               a.ExtractSecurityPermissionSet (declarative_security);
+                               return;
+                       }
+
+                       MethodBuilder.SetCustomAttribute (cb);
+               }
+
+               public override AttributeTargets AttributeTargets {
+                       get {
+                               return AttributeTargets.Method; 
+                       }
+               }
+
+               public EmitContext CreateEmitContext (DeclSpace tc, ILGenerator ig)
+               {
+                       EmitContext ec = new EmitContext (this,
+                               tc, this.ds, Location, ig, MemberType, ModFlags, false);
+
+                       Iterator iterator = tc as Iterator;
+                       if (iterator != null)
+                               ec.CurrentAnonymousMethod = iterator.Host;
+
+                       return ec;
+               }
+
+               public override bool Define ()
+               {
+                       if (!DoDefineBase ())
+                               return false;
+
+                       MethodBuilder mb = null;
+                       if (GenericMethod != null) {
+                               string method_name = MemberName.Name;
+
+                               if (IsExplicitImpl) {
+                                       method_name = TypeManager.CSharpName (InterfaceType) +
+                                               '.' + method_name;
+                               }
+
+                               mb = Parent.TypeBuilder.DefineMethod (method_name, flags);
+
+                               if (!GenericMethod.Define (mb))
+                                       return false;
+                       }
+
+                       if (!DoDefine ())
+                               return false;
+
+                       if (!CheckAbstractAndExtern (block != null))
+                               return false;
+
+                       if (!CheckBase ())
+                               return false;
+
+                       MethodData = new MethodData (this, ModFlags, flags, this, mb, GenericMethod, base_method);
+
+                       if (!MethodData.Define (ParentContainer))
+                               return false;
+
+                       MethodBuilder = MethodData.MethodBuilder;
+
+                       if (MemberType.IsAbstract && MemberType.IsSealed) {
+                               Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
+                               return false;
+                       }
+
+                       return true;
+               }
+
+               public override void Emit ()
+               {
+                       if (OptAttributes != null)
+                               OptAttributes.Emit ();
+
+                       if (declarative_security != null) {
+                               foreach (DictionaryEntry de in declarative_security) {
+                                       MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
+                               }
+                       }
+
+                       base.Emit ();
+               }
+
+               protected void Error_ConditionalAttributeIsNotValid ()
+               {
+                       Report.Error (577, Location,
+                               "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
+                               GetSignatureForError ());
+               }
+
+               public override bool MarkForDuplicationCheck ()
+               {
+                       caching_flags |= Flags.TestMethodDuplication;
+                       return true;
+               }
+
+               public override string[] ValidAttributeTargets {
+                       get {
+                               return attribute_targets;
+                       }
+               }
+
+               #region IMethodData Members
+
+               public CallingConventions CallingConventions {
+                       get {
+                               CallingConventions cc = Parameters.CallingConvention;
+                               if (Parameters.HasArglist)
+                                       block.HasVarargs = true;
+
+                               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;
+                       }
+               }
+
+               public MemberName MethodName {
+                       get {
+                               return MemberName;
+                       }
+               }
+
+               public new Location Location {
+                       get {
+                               return base.Location;
+                       }
+               }
+
+               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>
+               public bool IsExcluded () {
+                       if ((caching_flags & Flags.Excluded_Undetected) == 0)
+                               return (caching_flags & Flags.Excluded) != 0;
+
+                       caching_flags &= ~Flags.Excluded_Undetected;
+
+                       if (base_method == null) {
+                               if (OptAttributes == null)
+                                       return false;
+
+                               Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type);
+
+                               if (attrs == null)
+                                       return false;
+
+                               foreach (Attribute a in attrs) {
+                                       string condition = a.GetConditionalAttributeValue ();
+                                       if (condition == null)
+                                               return false;
 
-                               Report.SymbolRelatedToPreviousError (method);
-                               if (this is Operator && method is Operator)
-                                       Report.Error (557, Location, "Duplicate user-defined conversion in type `{0}'", Parent.Name);
-                               else
-                                       Report.Error (111, Location, TypeContainer.Error111, GetSignatureForError ());
+                                       if (RootContext.AllDefines.Contains (condition))
+                                               return false;
+                               }
 
+                               caching_flags |= Flags.Excluded;
                                return true;
                        }
 
-                       return false;
-               }
-
-               public override bool IsUsed {
-                       get { return IsExplicitImpl || base.IsUsed; }
-               }
+                       IMethodData md = TypeManager.GetMethod (base_method);
+                       if (md == null) {
+                               if (AttributeTester.IsConditionalMethodExcluded (base_method)) {
+                                       caching_flags |= Flags.Excluded;
+                                       return true;
+                               }
+                               return false;
+                       }
 
-               //
-               // Returns a string that represents the signature for this 
-               // member which should be used in XML documentation.
-               //
-               public override string GetDocCommentName (DeclSpace ds)
-               {
-                       return DocUtil.GetMethodDocCommentName (this, ds);
+                       if (md.IsExcluded ()) {
+                               caching_flags |= Flags.Excluded;
+                               return true;
+                       }
+                       return false;
                }
 
-               //
-               // Raised (and passed an XmlElement that contains the comment)
-               // when GenerateDocComment is writing documentation expectedly.
-               //
-               // FIXME: with a few effort, it could be done with XmlReader,
-               // that means removal of DOM use.
-               //
-               internal override void OnGenerateDocComment (DeclSpace ds, XmlElement el)
-               {
-                       DocUtil.OnMethodGenerateDocComment (this, ds, el);
+               GenericMethod IMethodData.GenericMethod {
+                       get {
+                               return GenericMethod;
+                       }
                }
 
-               //
-               //   Represents header string for documentation comment.
-               //
-               public override string DocCommentHeader {
-                       get { return "M:"; }
-               }
+               #endregion
 
        }
 
        public class SourceMethod : ISourceMethod
        {
-               TypeContainer container;
+               DeclSpace parent;
                MethodBase builder;
 
-               protected SourceMethod (TypeContainer container, MethodBase builder,
+               protected SourceMethod (DeclSpace parent, MethodBase builder,
                                        ISourceFile file, Location start, Location end)
                {
-                       this.container = container;
+                       this.parent = parent;
                        this.builder = builder;
                        
-                       CodeGen.SymbolWriter.OpenMethod (
-                               file, this, start.Row, start.Column, end.Row, start.Column);
+                       CodeGen.SymbolWriter.OpenMethod (file, this, start.Row, start.Column, end.Row, start.Column);
                }
 
                public string Name {
@@ -3922,7 +3887,7 @@ namespace Mono.CSharp {
                }
 
                public int NamespaceID {
-                       get { return container.NamespaceEntry.SymbolFileID; }
+                       get { return parent.NamespaceEntry.SymbolFileID; }
                }
 
                public int Token {
@@ -3942,8 +3907,7 @@ namespace Mono.CSharp {
                                CodeGen.SymbolWriter.CloseMethod ();
                }
 
-               public static SourceMethod Create (TypeContainer parent,
-                                                  MethodBase builder, Block block)
+               public static SourceMethod Create (DeclSpace parent, MethodBase builder, Block block)
                {
                        if (CodeGen.SymbolWriter == null)
                                return null;
@@ -3967,11 +3931,7 @@ namespace Mono.CSharp {
                }
        }
 
-       public class Method : MethodCore, IIteratorContainer, IMethodData {
-               public MethodBuilder MethodBuilder;
-               public MethodData MethodData;
-               ReturnParameter return_attributes;
-               ListDictionary declarative_security;
+       public class Method : MethodOrOperator, IIteratorContainer {
 
                /// <summary>
                ///   Modifiers allowed in a class declaration
@@ -3997,7 +3957,7 @@ namespace Mono.CSharp {
                //
                // return_type can be "null" for VOID values.
                //
-               public Method (TypeContainer parent, GenericMethod generic,
+               public Method (DeclSpace parent, GenericMethod generic,
                               Expression return_type, int mod, bool is_iface,
                               MemberName name, Parameters parameters, Attributes attrs)
                        : base (parent, generic, return_type, mod,
@@ -4005,29 +3965,18 @@ namespace Mono.CSharp {
                                is_iface, name, attrs, parameters)
                {
                }
-
-               public override AttributeTargets AttributeTargets {
-                       get {
-                               return AttributeTargets.Method;
-                       }
-               }
                
                public override string GetSignatureForError()
                {
-                       if (IsOperator != null)
-                               return IsOperator.GetSignatureForError ();
-
                        return base.GetSignatureForError () + Parameters.GetSignatureForError ();
                }
 
-                void DuplicateEntryPoint (MethodInfo b, Location location)
-                {
-                        Report.Error (
-                                17, location,
-                                "Program `" + CodeGen.FileName +
-                                "' has more than one entry point defined: `" +
-                                TypeManager.CSharpSignature(b) + "'");
-                }
+               void Error_DuplicateEntryPoint (MethodInfo b, Location location)
+               {
+                       Report.Error (17, location,
+                               "Program `{0}' has more than one entry point defined: `{1}'",
+                               CodeGen.FileName, TypeManager.CSharpSignature(b));
+               }
 
                 bool IsEntryPoint (MethodBuilder b, Parameters pinfo)
                 {
@@ -4053,39 +4002,9 @@ namespace Mono.CSharp {
 
                public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
                {
-                       if (a.Target == AttributeTargets.ReturnValue) {
-                               if (return_attributes == null)
-                                       return_attributes = new ReturnParameter (MethodBuilder, Location);
-
-                               return_attributes.ApplyAttributeBuilder (a, cb);
-                               return;
-                       }
-
-                       if (a.Type == TypeManager.methodimpl_attr_type &&
-                               (a.GetMethodImplOptions () & MethodImplOptions.InternalCall) != 0) {
-                               MethodBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
-                       }
-
-                       if (a.Type == TypeManager.dllimport_type) {
-                               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'");
-                               }
-
-                               return;
-                       }
-
-                       if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
-                               if (declarative_security == null)
-                                       declarative_security = new ListDictionary ();
-                               a.ExtractSecurityPermissionSet (declarative_security);
-                               return;
-                       }
-
                        if (a.Type == TypeManager.conditional_attribute_type) {
-                               if (IsOperator != null || IsExplicitImpl) {
-                                       Report.Error (577, Location, "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
-                                               GetSignatureForError ());
+                               if (IsExplicitImpl) {
+                                       Error_ConditionalAttributeIsNotValid ();
                                        return;
                                }
 
@@ -4118,13 +4037,13 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       MethodBuilder.SetCustomAttribute (cb);
+                       base.ApplyAttributeBuilder (a, cb);
                }
 
                protected override bool CheckForDuplications ()
                {
-                       ArrayList ar = Parent.Methods;
-                       if (ar != null) {
+                       ArrayList ar = ParentContainer.Methods;
+                       if (ar != null) {
                                int arLen = ar.Count;
                                        
                                for (int i = 0; i < arLen; i++) {
@@ -4134,7 +4053,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       ar = Parent.Properties;
+                       ar = ParentContainer.Properties;
                        if (ar != null) {
                                for (int i = 0; i < ar.Count; ++i) {
                                        PropertyBase pb = (PropertyBase) ar [i];
@@ -4143,7 +4062,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       ar = Parent.Indexers;
+                       ar = ParentContainer.Indexers;
                        if (ar != null) {
                                for (int i = 0; i < ar.Count; ++i) {
                                        PropertyBase pb = (PropertyBase) ar [i];
@@ -4152,7 +4071,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       ar = Parent.Events;
+                       ar = ParentContainer.Events;
                        if (ar != null) {
                                for (int i = 0; i < ar.Count; ++i) {
                                        Event ev = (Event) ar [i];
@@ -4169,28 +4088,7 @@ namespace Mono.CSharp {
                //
                public override bool Define ()
                {
-                       if (!DoDefineBase ())
-                               return false;
-
-                       MethodBuilder mb = null;
-                       if (GenericMethod != null) {
-                               string method_name = MemberName.Name;
-
-                               if (IsExplicitImpl) {
-                                       method_name = TypeManager.CSharpName (InterfaceType) +
-                                               '.' + method_name;
-                               }
-
-                               mb = Parent.TypeBuilder.DefineMethod (method_name, flags);
-
-                               if (!GenericMethod.Define (mb))
-                                       return false;
-                       }
-
-                       if (!DoDefine ())
-                               return false;
-
-                       if (!CheckAbstractAndExtern (block != null))
+                       if (!base.Define ())
                                return false;
 
                        if (RootContext.StdLib && (ReturnType == TypeManager.arg_iterator_type || ReturnType == TypeManager.typed_reference_type)) {
@@ -4198,17 +4096,6 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       if (!CheckBase ())
-                               return false;
-
-                       if (IsOperator != null)
-                               flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
-
-                       MethodData = new MethodData (this, ModFlags, flags, this, mb, GenericMethod, base_method);
-
-                       if (!MethodData.Define (Parent))
-                               return false;
-
                        if (ReturnType == TypeManager.void_type && ParameterTypes.Length == 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?");
@@ -4225,42 +4112,35 @@ namespace Mono.CSharp {
                                        return false;
                        }
 
-                       MethodBuilder = MethodData.MethodBuilder;
-
                        //
                        // This is used to track the Entry Point,
                        //
                        if (Name == "Main" &&
-                           ((ModFlags & Modifiers.STATIC) != 0) && RootContext.NeedsEntryPoint && 
-                           (RootContext.MainClass == null ||
-                            RootContext.MainClass == Parent.TypeBuilder.FullName)){
-                                if (IsEntryPoint (MethodBuilder, ParameterInfo)) {
-                                        IMethodData md = TypeManager.GetMethod (MethodBuilder);
-                                        md.SetMemberIsUsed ();
-
-                                        if (RootContext.EntryPoint == null) {
+                               ((ModFlags & Modifiers.STATIC) != 0) && RootContext.NeedsEntryPoint && 
+                               (RootContext.MainClass == null ||
+                               RootContext.MainClass == Parent.TypeBuilder.FullName)){
+                               if (IsEntryPoint (MethodBuilder, ParameterInfo)) {
+                                       IMethodData md = TypeManager.GetMethod (MethodBuilder);
+                                       md.SetMemberIsUsed ();
+
+                                       if (RootContext.EntryPoint == null) {
                                                if (Parent.IsGeneric){
                                                        Report.Error (-201, Location,
                                                                      "Entry point can not be defined in a generic class");
                                                }
-                                               
-                                                RootContext.EntryPoint = MethodBuilder;
-                                                RootContext.EntryPointLocation = Location;
-                                        } else {
-                                                DuplicateEntryPoint (RootContext.EntryPoint, RootContext.EntryPointLocation);
-                                                DuplicateEntryPoint (MethodBuilder, Location);
-                                        }
-                                } else {
+
+                                               RootContext.EntryPoint = MethodBuilder;
+                                               RootContext.EntryPointLocation = Location;
+                                       } else {
+                                               Error_DuplicateEntryPoint (RootContext.EntryPoint, RootContext.EntryPointLocation);
+                                               Error_DuplicateEntryPoint (MethodBuilder, Location);
+                                       }
+                               } else {
                                        if (RootContext.WarningLevel >= 4)
                                                Report.Warning (28, 4, Location, "`{0}' has the wrong signature to be an entry point", TypeManager.CSharpSignature(MethodBuilder));
                                }
                        }
 
-                       if (MemberType.IsAbstract && MemberType.IsSealed) {
-                               Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
-                               return false;
-                       }
-
                        return true;
                }
 
@@ -4269,15 +4149,9 @@ namespace Mono.CSharp {
                // 
                public override void Emit ()
                {
-                       MethodData.Emit (Parent, this);
+                       MethodData.Emit (Parent);
                        base.Emit ();
 
-                       if (declarative_security != null) {
-                               foreach (DictionaryEntry de in declarative_security) {
-                                       MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
-                               }
-                       }
-
                        Block = null;
                        MethodData = null;
                }
@@ -4287,152 +4161,34 @@ namespace Mono.CSharp {
                        Report.Error (1599, loc, "Method or delegate cannot return type `{0}'", TypeManager.CSharpName (t));
                }
 
-               protected override MethodInfo FindOutBaseMethod (TypeContainer container, ref Type base_ret_type)
+               protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
                {
-                       MethodInfo mi = (MethodInfo) container.BaseCache.FindMemberToOverride (
-                               container.TypeBuilder, Name, ParameterTypes, GenericMethod, false);
+                       MethodInfo mi = (MethodInfo) ParentContainer.BaseCache.FindMemberToOverride (
+                               Parent.TypeBuilder, Name, ParameterTypes, GenericMethod, false);
 
                        if (mi == null)
                                return null;
 
-                       base_ret_type = mi.ReturnType;
-                       return mi;
-               }
-       
-               public override bool MarkForDuplicationCheck ()
-               {
-                       caching_flags |= Flags.TestMethodDuplication;
-                       return true;
-               }
-
-               protected override bool VerifyClsCompliance(DeclSpace ds)
-               {
-                       if (!base.VerifyClsCompliance (ds))
-                               return false;
-
-                       if (ParameterInfo.Count > 0) {
-                               ArrayList al = (ArrayList)ds.MemberCache.Members [Name];
-                               if (al.Count > 1)
-                                       ds.MemberCache.VerifyClsParameterConflict (al, this, MethodBuilder);
-                       }
-
-                       return true;
-               }
-
-               #region IMethodData Members
-
-               public CallingConventions CallingConventions {
-                       get {
-                               CallingConventions cc = Parameters.CallingConvention;
-                               if (Parameters.HasArglist)
-                                       block.HasVarargs = true;
-
-                               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;
-                       }
-               }
-
-               public MemberName MethodName {
-                       get {
-                               return MemberName;
-                       }
-               }
-
-               public new Location Location {
-                       get {
-                               return base.Location;
-                       }
-               }
-
-               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;
-               }
-
-               public EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
-               {
-                       EmitContext ec = new EmitContext (
-                               tc, ds, Location, ig, ReturnType, ModFlags, false);
-
-                       Iterator iterator = tc as Iterator;
-                       if (iterator != null)
-                               ec.CurrentAnonymousMethod = iterator.Host;
-
-                       return ec;
-               }
-
-               /// <summary>
-               /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
-               /// </summary>
-               public bool IsExcluded (EmitContext ec)
-               {
-                       if ((caching_flags & Flags.Excluded_Undetected) == 0)
-                               return (caching_flags & Flags.Excluded) != 0;
-
-                       caching_flags &= ~Flags.Excluded_Undetected;
-
-                       if (base_method == null) {
-                               if (OptAttributes == null)
-                                       return false;
-
-                               Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type, ec);
-
-                               if (attrs == null)
-                                       return false;
-
-                               foreach (Attribute a in attrs) {
-                                       string condition = a.GetConditionalAttributeValue (Parent.EmitContext);
-                                       if (RootContext.AllDefines.Contains (condition))
-                                               return false;
-                               }
+                       if (mi.IsSpecialName)
+                               return null;
 
-                               caching_flags |= Flags.Excluded;
-                               return true;
-                       }
+                       base_ret_type = mi.ReturnType;
+                       return mi;
+               }
 
-                       IMethodData md = TypeManager.GetMethod (base_method);
-                       if (md == null) {
-                               if (AttributeTester.IsConditionalMethodExcluded (base_method)) {
-                                       caching_flags |= Flags.Excluded;
-                                       return true;
-                               }
+               protected override bool VerifyClsCompliance ()
+               {
+                       if (!base.VerifyClsCompliance ())
                                return false;
-                       }
 
-                       if (md.IsExcluded (ec)) {
-                               caching_flags |= Flags.Excluded;
-                               return true;
+                       if (ParameterInfo.Count > 0) {
+                               ArrayList al = (ArrayList)ParentContainer.MemberCache.Members [Name];
+                               if (al.Count > 1)
+                                       MemberCache.VerifyClsParameterConflict (al, this, MethodBuilder);
                        }
-                       return false;
-               }
 
-               GenericMethod IMethodData.GenericMethod {
-                       get {
-                               return GenericMethod;
-                       }
+                       return true;
                }
-
-               #endregion
        }
 
        public abstract class ConstructorInitializer {
@@ -4503,7 +4259,7 @@ namespace Mono.CSharp {
                        if (base_constructor == null) {
                                if (errors == Report.Errors)
                                        Invocation.Error_WrongNumArguments (loc, TypeManager.CSharpSignature (caller_builder),
-                                               argument_list.Count);
+                                               argument_list == null ? 0 : argument_list.Count);
                                return false;
                        }
 
@@ -4545,14 +4301,6 @@ namespace Mono.CSharp {
                        base (null, loc)
                {
                }
-
-               public override void Emit(EmitContext ec)
-               {
-                       bool old = ec.TestObsoleteMethodUsage;
-                       ec.TestObsoleteMethodUsage = false;
-                       base.Emit (ec);
-                       ec.TestObsoleteMethodUsage = old;
-               }
        }
 
        public class ConstructorThisInitializer : ConstructorInitializer {
@@ -4579,29 +4327,27 @@ namespace Mono.CSharp {
                        Modifiers.EXTERN |              
                        Modifiers.PRIVATE;
 
+               static string[] attribute_targets = new string [] { "method" };
+
                bool has_compliant_args = false;
                //
                // The spec claims that static is not permitted, but
                // my very own code has static constructors.
                //
-               public Constructor (TypeContainer ds, string name, int mod, Parameters args,
+               public Constructor (DeclSpace parent, string name, int mod, Parameters args,
                                    ConstructorInitializer init, Location loc)
-                       : base (ds, null, null, mod, AllowedModifiers, false,
+                       : base (parent, null, null, mod, AllowedModifiers, false,
                                new MemberName (name, loc), null, args)
                {
                        Initializer = init;
                }
 
                public bool HasCompliantArgs {
-                       get {
-                               return has_compliant_args;
-                       }
+                       get { return has_compliant_args; }
                }
 
                public override AttributeTargets AttributeTargets {
-                       get {
-                               return AttributeTargets.Constructor;
-                       }
+                       get { return AttributeTargets.Constructor; }
                }
 
 
@@ -4628,12 +4374,17 @@ namespace Mono.CSharp {
                                return;
                        }
 
+                       if (a.Type == TypeManager.methodimpl_attr_type &&
+                               (a.GetMethodImplOptions () & MethodImplOptions.InternalCall) != 0) {
+                               ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
+                       }
+
                        ConstructorBuilder.SetCustomAttribute (cb);
                }
 
                protected override bool CheckForDuplications ()
                {
-                       ArrayList ar = Parent.InstanceConstructors;
+                       ArrayList ar = ParentContainer.InstanceConstructors;
                        if (ar != null) {
                                int arLen = ar.Count;
                                        
@@ -4648,18 +4399,25 @@ namespace Mono.CSharp {
                        
                protected override bool CheckBase ()
                {
+                       if ((ModFlags & Modifiers.STATIC) != 0) {
+                               if (!Parameters.Empty) {
+                                       Report.Error (132, Location, "`{0}': The static constructor must be parameterless",
+                                               GetSignatureForError ());
+                                       return false;
+                               }
+
+                               // the rest can be ignored
+                               return true;
+                       }
+
                        // Check whether arguments were correct.
                        if (!DoDefineParameters ())
                                return false;
                        
-                       // TODO: skip the rest for generated ctor
-                       if ((ModFlags & Modifiers.STATIC) != 0)
-                               return true;
-                       
                        if (!CheckForDuplications ())
                                return false;
 
-                       if (Parent.Kind == Kind.Struct) {
+                       if (ParentContainer.Kind == Kind.Struct) {
                                if (ParameterTypes.Length == 0) {
                                        Report.Error (568, Location, 
                                                "Structs cannot contain explicit parameterless constructors");
@@ -4675,7 +4433,7 @@ namespace Mono.CSharp {
                        if ((RootContext.WarningLevel >= 4) && ((Parent.ModFlags & Modifiers.SEALED) != 0 && (ModFlags & Modifiers.PROTECTED) != 0)) {
                                Report.Warning (628, 4, Location, "`{0}': new protected member declared in sealed class", GetSignatureForError ());
                        }
-                       
+
                        return true;
                }
                
@@ -4704,8 +4462,6 @@ namespace Mono.CSharp {
                                                ca |= MethodAttributes.Family;
                                } else if ((ModFlags & Modifiers.INTERNAL) != 0)
                                        ca |= MethodAttributes.Assembly;
-                               else if (IsDefault ())
-                                       ca |= MethodAttributes.Public;
                                else
                                        ca |= MethodAttributes.Private;
                        }
@@ -4724,7 +4480,7 @@ namespace Mono.CSharp {
                        if ((ModFlags & Modifiers.UNSAFE) != 0)
                                ConstructorBuilder.InitLocals = false;
 
-                       if (Parent.IsComImport) {
+                       if (ParentContainer.IsComImport) {
                                if (!IsDefault ()) {
                                        Report.Error (669, Location, "`{0}': A class with the ComImport attribute cannot have a user-defined constructor",
                                                Parent.GetSignatureForError ());
@@ -4732,7 +4488,7 @@ namespace Mono.CSharp {
                                }
                                ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall);
                        }
-                       
+
                        TypeManager.AddMethod (ConstructorBuilder, this);
 
                        return true;
@@ -4743,23 +4499,24 @@ namespace Mono.CSharp {
                //
                public override void Emit ()
                {
-                       EmitContext ec = CreateEmitContext (null, null);
-                       if (GetObsoleteAttribute () != null || Parent.GetObsoleteAttribute () != null)
-                               ec.TestObsoleteMethodUsage = false;
+                       if (OptAttributes != null)
+                               OptAttributes.Emit ();
 
-                       // If this is a non-static `struct' constructor and doesn't have any
-                       // initializer, it must initialize all of the struct's fields.
-                       if ((Parent.Kind == Kind.Struct) &&
-                           ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
-                               Block.AddThisVariable (Parent, Location);
+                       EmitContext ec = CreateEmitContext (null, null);
 
                        if (block != null) {
+                               // If this is a non-static `struct' constructor and doesn't have any
+                               // initializer, it must initialize all of the struct's fields.
+                               if ((ParentContainer.Kind == Kind.Struct) &&
+                                       ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
+                                       block.AddThisVariable (Parent, Location);
+
                                if (!block.ResolveMeta (ec, ParameterInfo))
                                        block = null;
                        }
 
                        if ((ModFlags & Modifiers.STATIC) == 0){
-                               if (Parent.Kind == Kind.Class && Initializer == null)
+                               if (ParentContainer.Kind == Kind.Class && Initializer == null)
                                        Initializer = new GeneratedBaseInitializer (Location);
 
 
@@ -4774,7 +4531,7 @@ namespace Mono.CSharp {
                                ec.IsStatic = false;
                        }
 
-                       Parameters.ApplyAttributes (ec, ConstructorBuilder);
+                       Parameters.ApplyAttributes (ConstructorBuilder);
                        
                        SourceMethod source = SourceMethod.Create (
                                Parent, ConstructorBuilder, block);
@@ -4782,7 +4539,7 @@ namespace Mono.CSharp {
                        //
                        // Classes can have base initializers and instance field initializers.
                        //
-                       if (Parent.Kind == Kind.Class){
+                       if (ParentContainer.Kind == Kind.Class){
                                if ((ModFlags & Modifiers.STATIC) == 0){
 
                                        //
@@ -4790,7 +4547,7 @@ namespace Mono.CSharp {
                                        // do not emit field initializers, they are initialized in the other constructor
                                        //
                                        if (!(Initializer != null && Initializer is ConstructorThisInitializer))
-                                               Parent.EmitFieldInitializers (ec);
+                                               ParentContainer.EmitFieldInitializers (ec);
                                }
                        }
                        if (Initializer != null) {
@@ -4798,10 +4555,7 @@ namespace Mono.CSharp {
                        }
                        
                        if ((ModFlags & Modifiers.STATIC) != 0)
-                               Parent.EmitFieldInitializers (ec);
-
-                       if (OptAttributes != null) 
-                               OptAttributes.Emit (ec, this);
+                               ParentContainer.EmitFieldInitializers (ec);
 
                        ec.EmitTopBlock (this, block);
 
@@ -4820,37 +4574,43 @@ namespace Mono.CSharp {
                }
 
                // Is never override
-               protected override MethodInfo FindOutBaseMethod (TypeContainer container, ref Type base_ret_type)
+               protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
                {
                        return null;
                }
-                                               
+
                public override string GetSignatureForError()
                {
                        return base.GetSignatureForError () + Parameters.GetSignatureForError ();
                }
 
-               protected override bool VerifyClsCompliance (DeclSpace ds)
+               public override string[] ValidAttributeTargets {
+                       get {
+                               return attribute_targets;
+                       }
+               }
+
+               protected override bool VerifyClsCompliance ()
                {
-                       if (!base.VerifyClsCompliance (ds) || !IsExposedFromAssembly (ds)) {
+                       if (!base.VerifyClsCompliance () || !IsExposedFromAssembly ()) {
                                return false;
                        }
-
+                       
                        if (ParameterInfo.Count > 0) {
-                               ArrayList al = (ArrayList)ds.MemberCache.Members [".ctor"];
+                               ArrayList al = (ArrayList)Parent.MemberCache.Members [".ctor"];
                                if (al.Count > 3)
-                                       ds.MemberCache.VerifyClsParameterConflict (al, this, ConstructorBuilder);
-                               
-                               if (ds.TypeBuilder.IsSubclassOf (TypeManager.attribute_type)) {
+                                       MemberCache.VerifyClsParameterConflict (al, this, ConstructorBuilder);
+                               if (Parent.TypeBuilder.IsSubclassOf (TypeManager.attribute_type)) {
                                        foreach (Type param in ParameterTypes) {
                                                if (param.IsArray) {
                                                        return true;
-                               }
-                       }
+                                               }
+                                       }
                                }
                        }
                        has_compliant_args = true;
-                               return true;
+                       return true;
                }
 
                #region IMethodData Members
@@ -4859,7 +4619,7 @@ namespace Mono.CSharp {
                        get {
                                CallingConventions cc = Parameters.CallingConvention;
 
-                               if (Parent.Kind == Kind.Class)
+                               if (ParentContainer.Kind == Kind.Class)
                                        if ((ModFlags & Modifiers.STATIC) == 0)
                                                cc |= CallingConventions.HasThis;
 
@@ -4887,13 +4647,13 @@ namespace Mono.CSharp {
                        }
                }
 
-               public EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
+               public EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
                {
                        ILGenerator ig_ = ConstructorBuilder.GetILGenerator ();
-                       return new EmitContext (Parent, Location, ig_, null, ModFlags, true);
+                       return new EmitContext (this, Parent, Location, ig_, null, ModFlags, true);
                }
 
-               public bool IsExcluded(EmitContext ec)
+               public bool IsExcluded()
                {
                        return false;
                }
@@ -4922,11 +4682,11 @@ namespace Mono.CSharp {
                Attributes OptAttributes { get; }
                ToplevelBlock Block { get; set; }
 
-               EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig);
+               EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig);
                ObsoleteAttribute GetObsoleteAttribute ();
                string GetSignatureForError ();
-               bool IsExcluded (EmitContext ec);
-               bool IsClsComplianceRequired (DeclSpace ds);
+               bool IsExcluded ();
+               bool IsClsComplianceRequired ();
                void SetMemberIsUsed ();
        }
 
@@ -4987,17 +4747,20 @@ namespace Mono.CSharp {
                        this.parent_method = parent_method;
                }
 
-               public bool Define (TypeContainer container)
+               public bool Define (DeclSpace parent)
                {
                        string name = method.MethodName.Basename;
                        string method_name = method.MethodName.FullName;
 
-                       if (container.Pending != null){
+                       TypeContainer container = ((TypeContainer) parent).PartialContainer;
+
+                       PendingImplementation pending = container.PendingImplementations;
+                       if (pending != null){
                                if (member is Indexer) // TODO: test it, but it should work without this IF
-                                       implementing = container.Pending.IsInterfaceIndexer (
+                                       implementing = pending.IsInterfaceIndexer (
                                                member.InterfaceType, method.ReturnType, method.ParameterInfo);
                                else
-                                       implementing = container.Pending.IsInterfaceMethod (
+                                       implementing = pending.IsInterfaceMethod (
                                                member.InterfaceType, name, method.ReturnType, method.ParameterInfo);
 
                                if (member.InterfaceType != null){
@@ -5110,10 +4873,8 @@ namespace Mono.CSharp {
                        }
 
                        EmitContext ec = method.CreateEmitContext (container, null);
-                       if (method.GetObsoleteAttribute () != null || container.GetObsoleteAttribute () != null)
-                               ec.TestObsoleteMethodUsage = false;
 
-                       DefineMethodBuilder (ec, container, method_name, method.ParameterInfo.Types);
+                       DefineMethodBuilder (container, method_name, method.ParameterInfo.Types);
 
                        if (builder == null)
                                return false;
@@ -5131,17 +4892,18 @@ namespace Mono.CSharp {
                                // clear the pending implemntation flag
                                //
                                if (member is Indexer) {
-                                       container.Pending.ImplementIndexer (
+                                       pending.ImplementIndexer (
                                                member.InterfaceType, builder, method.ReturnType,
                                                method.ParameterInfo, member.IsExplicitImpl);
                                } else
-                                       container.Pending.ImplementMethod (
+                                       pending.ImplementMethod (
                                                member.InterfaceType, name, method.ReturnType,
                                                method.ParameterInfo, member.IsExplicitImpl);
 
                                if (member.IsExplicitImpl)
                                        container.TypeBuilder.DefineMethodOverride (
                                                builder, implementing);
+
                        }
 
                        TypeManager.AddMethod (builder, method);
@@ -5160,21 +4922,22 @@ namespace Mono.CSharp {
                        return true;
                }
 
+
                /// <summary>
                /// Create the MethodBuilder for the method 
                /// </summary>
-               void DefineMethodBuilder (EmitContext ec, TypeContainer container, string method_name, Type[] ParameterTypes)
+               void DefineMethodBuilder (TypeContainer container, string method_name, Type[] ParameterTypes)
                {
                        const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
 
                        if ((modifiers & extern_static) == extern_static) {
 
                                if (method.OptAttributes != null) {
-                                       Attribute dllimport_attribute = method.OptAttributes.Search (TypeManager.dllimport_type, ec);
+                                       Attribute dllimport_attribute = method.OptAttributes.Search (TypeManager.dllimport_type);
                                        if (dllimport_attribute != null) {
                                                flags |= MethodAttributes.PinvokeImpl;
                                                builder = dllimport_attribute.DefinePInvokeMethod (
-                                                       ec, container.TypeBuilder, method_name, flags,
+                                                       container.TypeBuilder, method_name, flags,
                                                        method.ReturnType, ParameterTypes);
 
                                                return;
@@ -5184,7 +4947,7 @@ namespace Mono.CSharp {
                                // for extern static method must be specified either DllImport attribute or MethodImplAttribute.
                                // We are more strict than Microsoft and report CS0626 like error
                                if (method.OptAttributes == null ||
-                                       !method.OptAttributes.Contains (TypeManager.methodimpl_attr_type, ec)) {
+                                       !method.OptAttributes.Contains (TypeManager.methodimpl_attr_type)) {
                                        Report.Error (626, method.Location, "Method, operator, or accessor `{0}' is marked external and has no attributes on it. Consider adding a DllImport attribute to specify the external implementation",
                                                method.GetSignatureForError ());
                                        return;
@@ -5207,31 +4970,22 @@ namespace Mono.CSharp {
                //
                // Emits the code
                // 
-               public void Emit (TypeContainer container, Attributable kind)
+               public void Emit (DeclSpace parent)
                {
                        EmitContext ec;
                        if ((flags & MethodAttributes.PinvokeImpl) == 0)
-                               ec = method.CreateEmitContext (container, builder.GetILGenerator ());
+                               ec = method.CreateEmitContext (parent, builder.GetILGenerator ());
                        else
-                               ec = method.CreateEmitContext (container, null);
-
-                       if (method.GetObsoleteAttribute () != null || container.GetObsoleteAttribute () != null)
-                               ec.TestObsoleteMethodUsage = false;
-
-                       method.ParameterInfo.ApplyAttributes (ec, MethodBuilder);
-
-                       Attributes OptAttributes = method.OptAttributes;
+                               ec = method.CreateEmitContext (parent, null);
 
-                       if (OptAttributes != null)
-                               OptAttributes.Emit (ec, kind);
+                       method.ParameterInfo.ApplyAttributes (MethodBuilder);
 
                        if (GenericMethod != null)
-                               GenericMethod.EmitAttributes (ec);
+                               GenericMethod.EmitAttributes ();
 
                        ToplevelBlock block = method.Block;
                        
-                       SourceMethod source = SourceMethod.Create (
-                               container, MethodBuilder, method.Block);
+                       SourceMethod source = SourceMethod.Create (parent, MethodBuilder, method.Block);
 
                        //
                        // Handle destructors specially
@@ -5285,18 +5039,19 @@ namespace Mono.CSharp {
        // TODO: Should derive from MethodCore
        public class Destructor : Method {
 
-               public Destructor (TypeContainer ds, Expression return_type, int mod,
+               static string[] attribute_targets = new string [] { "method" };
+
+               public Destructor (DeclSpace parent, Expression return_type, int mod,
                                   string name, Parameters parameters, Attributes attrs,
                                   Location l)
-                       : base (ds, null, return_type, mod, false, new MemberName (name, l),
+                       : base (parent, null, return_type, mod, false, new MemberName (name, l),
                                parameters, attrs)
                { }
 
                public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
                {
                        if (a.Type == TypeManager.conditional_attribute_type) {
-                               Report.Error (577, Location, "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
-                                       GetSignatureForError ());
+                               Error_ConditionalAttributeIsNotValid ();
                                return;
                        }
 
@@ -5308,6 +5063,11 @@ namespace Mono.CSharp {
                        return Parent.GetSignatureForError () + ".~" + Parent.MemberName.Name + "()";
                }
 
+               public override string[] ValidAttributeTargets {
+                       get {
+                               return attribute_targets;
+                       }
+               }
        }
        
        abstract public class MemberBase : MemberCore {
@@ -5328,8 +5088,8 @@ namespace Mono.CSharp {
                        set { SetMemberName (new MemberName (MemberName.Left, value, Location)); }
                }
 
-               public new TypeContainer Parent {
-                       get { return (TypeContainer) base.Parent; }
+               public TypeContainer ParentContainer {
+                       get { return ((TypeContainer) Parent).PartialContainer; }
                }
 
                //
@@ -5339,13 +5099,8 @@ namespace Mono.CSharp {
                public Type MemberType {
                        get {
                                if (member_type == null && Type != null) {
-                                       EmitContext ec = ds.EmitContext;
-                                       bool old_unsafe = ec.InUnsafe;
-                                       ec.InUnsafe = InUnsafe;
-                                       ec.ResolvingGenericMethod = GenericMethod != null;
-                                       Type = Type.ResolveAsTypeTerminal (ec, false);
-                                       ec.ResolvingGenericMethod = false;
-                                       ec.InUnsafe = old_unsafe;
+                                       IResolveContext rc = GenericMethod == null ? this : (IResolveContext)ds;
+                                       Type = Type.ResolveAsTypeTerminal (rc, false);
                                        if (Type != null) {
                                                member_type = Type.Type;
                                        }
@@ -5372,7 +5127,7 @@ namespace Mono.CSharp {
                //
                // The constructor is only exposed to our children
                //
-               protected MemberBase (TypeContainer parent, GenericMethod generic,
+               protected MemberBase (DeclSpace parent, GenericMethod generic,
                                      Expression type, int mod, int allowed_mod, int def_mod,
                                      MemberName name, Attributes attrs)
                        : base (parent, name, attrs)
@@ -5383,11 +5138,13 @@ namespace Mono.CSharp {
                        ModFlags = Modifiers.Check (allowed_mod, mod, def_mod, Location);
                        IsExplicitImpl = (MemberName.Left != null);
                        GenericMethod = generic;
+                       if (GenericMethod != null)
+                               GenericMethod.ModFlags = ModFlags;
                }
 
                protected virtual bool CheckBase ()
                {
-                       if ((ModFlags & Modifiers.PROTECTED) != 0 && Parent.Kind == Kind.Struct) {
+                       if ((ModFlags & Modifiers.PROTECTED) != 0 && ParentContainer.Kind == Kind.Struct) {
                                Report.Error (666, Location, "`{0}': new protected member declared in struct", GetSignatureForError ());
                                return false;
                        }
@@ -5403,10 +5160,6 @@ namespace Mono.CSharp {
 
                protected virtual bool DoDefineBase ()
                {
-                       EmitContext ec = Parent.EmitContext;
-                       if (ec == null)
-                               throw new InternalErrorException ("MemberBase.DoDefine called too early");
-
                        if (Name == null)
                                throw new InternalErrorException ();
 
@@ -5421,7 +5174,7 @@ namespace Mono.CSharp {
                                        MethodAttributes.NewSlot |
                                        MethodAttributes.Virtual;
                        } else {
-                               if (!Parent.MethodModifiersValid (this))
+                               if (!ParentContainer.MethodModifiersValid (this))
                                        return false;
 
                                flags = Modifiers.MethodAttr (ModFlags);
@@ -5429,18 +5182,18 @@ namespace Mono.CSharp {
 
                        if (IsExplicitImpl) {
                                Expression expr = MemberName.Left.GetTypeExpression ();
-                               TypeExpr iface_texpr = expr.ResolveAsTypeTerminal (ec, false);
+                               TypeExpr iface_texpr = expr.ResolveAsTypeTerminal (this, false);
                                if (iface_texpr == null)
                                        return false;
 
-                               InterfaceType = iface_texpr.ResolveType (ec);
+                               InterfaceType = iface_texpr.Type;
 
                                if (!InterfaceType.IsInterface) {
                                        Report.Error (538, Location, "'{0}' in explicit interface declaration is not an interface", TypeManager.CSharpName (InterfaceType));
                                        return false;
                                }
 
-                               if (!Parent.VerifyImplements (this))
+                               if (!ParentContainer.VerifyImplements (this))
                                        return false;
                                
                                Modifiers.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location);
@@ -5451,17 +5204,9 @@ namespace Mono.CSharp {
 
                protected virtual bool DoDefine ()
                {
-                       EmitContext ec = ds.EmitContext;
-                       if (ec == null)
-                               throw new InternalErrorException ("MemberBase.DoDefine called too early");
-
-                       ec.InUnsafe = InUnsafe;
-
                        if (MemberType == null)
                                return false;
 
-                       CheckObsoleteType (Type);
-
                        if ((Parent.ModFlags & Modifiers.SEALED) != 0 && 
                                (ModFlags & (Modifiers.VIRTUAL|Modifiers.ABSTRACT)) != 0) {
                                        Report.Error (549, Location, "New virtual member `{0}' is declared in a sealed class `{1}'",
@@ -5502,23 +5247,20 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       if (MemberType.IsPointer && !UnsafeOK (Parent))
-                               return false;
-
                        if (IsExplicitImpl) {
                                Expression expr = MemberName.Left.GetTypeExpression ();
-                               TypeExpr texpr = expr.ResolveAsTypeTerminal (ec, false);
+                               TypeExpr texpr = expr.ResolveAsTypeTerminal (this, false);
                                if (texpr == null)
                                        return false;
 
-                               InterfaceType = texpr.ResolveType (ec);
+                               InterfaceType = texpr.Type;
 
                                if (!InterfaceType.IsInterface) {
                                        Report.Error (538, Location, "`{0}' in explicit interface declaration is not an interface", TypeManager.CSharpName (InterfaceType));
                                        return false;
                                }
                                
-                               if (!Parent.VerifyImplements (this))
+                               if (!ParentContainer.VerifyImplements (this))
                                        return false;
                                
                                Modifiers.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location);
@@ -5536,13 +5278,13 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               protected override bool VerifyClsCompliance(DeclSpace ds)
+               protected override bool VerifyClsCompliance()
                {
-                       if (base.VerifyClsCompliance (ds)) {
+                       if (base.VerifyClsCompliance ()) {
                                return true;
                        }
 
-                       if (IsInterface && HasClsCompliantAttribute && ds.IsClsComplianceRequired (ds)) {
+                       if (IsInterface && HasClsCompliantAttribute && Parent.IsClsComplianceRequired ()) {
                                Report.Error (3010, Location, "`{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
                        }
                        return false;
@@ -5558,6 +5300,7 @@ namespace Mono.CSharp {
                public FieldBuilder  FieldBuilder;
                public Status status;
                protected Expression initializer;
+               ExpressionStatement initializerStatement;
 
                [Flags]
                public enum Status : byte {
@@ -5571,7 +5314,7 @@ namespace Mono.CSharp {
                /// </summary>
                public MemberInfo conflict_symbol;
 
-               protected FieldBase (TypeContainer parent, Expression type, int mod,
+               protected FieldBase (DeclSpace parent, Expression type, int mod,
                                     int allowed_mod, MemberName name, Attributes attrs)
                        : base (parent, null, type, mod, allowed_mod, Modifiers.PRIVATE,
                                name, attrs)
@@ -5591,8 +5334,8 @@ namespace Mono.CSharp {
                                if (marshal != null) {
                                        FieldBuilder.SetMarshal (marshal);
                                }
-                                       return;
-                               }
+                               return;
+                       }
 
                        if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
                                a.Error_InvalidSecurityParent ();
@@ -5604,40 +5347,7 @@ namespace Mono.CSharp {
 
                public void EmitInitializer (EmitContext ec)
                {
-                       // Replace DeclSpace because of partial classes
-                       ec.DeclContainer = EmitContext.DeclContainer;
-
-                       ec.IsFieldInitializer = true;
-                       initializer = initializer.Resolve (ec);
-                       ec.IsFieldInitializer = false;
-                       if (initializer == null)
-                               return;
-                       FieldExpr fe = new FieldExpr (FieldBuilder, Location, true);
-                       if ((ModFlags & Modifiers.STATIC) == 0)
-                               fe.InstanceExpression = new This (Location).Resolve (ec);
-
-                       ExpressionStatement a = new Assign (fe, initializer, Location);
-
-                       a = a.ResolveStatement (ec);
-                       if (a == null)
-                               return;
-
-                       Constant c = initializer as Constant;
-                       if (c != null && CanElideInitializer (c))
-                               return;
-
-                       a.EmitStatement (ec);
-               }
-
-               bool CanElideInitializer (Constant c)
-               {
-                       if (MemberType == c.Type)
-                               return c.IsDefaultValue;
-
-                       if (c.Type == TypeManager.null_type)
-                               return true;
-
-                       return false;
+                       initializerStatement.EmitStatement (ec);
                }
 
                protected override bool CheckBase ()
@@ -5649,7 +5359,7 @@ namespace Mono.CSharp {
                        if (IsInterface)
                                return true;
 
-                       conflict_symbol = Parent.FindBaseMemberWithSameName (Name, false);
+                       conflict_symbol = ParentContainer.FindBaseMemberWithSameName (Name, false);
                        if (conflict_symbol == null) {
                                if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
                                        Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
@@ -5670,7 +5380,7 @@ namespace Mono.CSharp {
                        set {
                                if (value != null) {
                                        this.initializer = value;
-                                       Parent.RegisterFieldForInitialization (this);
+                                       ParentContainer.RegisterFieldForInitialization (this);
                                }
                        }
                }
@@ -5684,15 +5394,47 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override string[] ValidAttributeTargets {
+               public Expression ResolveInitializer ()
+               {
+                       // TODO: again it's too heavy-weight
+                       EmitContext ec = new EmitContext (this, Parent, Location, null, null, ModFlags);
+                       ec.IsFieldInitializer = true;
+                       initializer = initializer.Resolve (ec);
+                       if (initializer == null)
+                               return null;
+
+                       FieldExpr fe = new FieldExpr (FieldBuilder, Location, true);
+                       if ((ModFlags & Modifiers.STATIC) == 0) 
+                       {
+                               fe.InstanceExpression = CompilerGeneratedThis.Instance;
+                       }
+
+                       initializerStatement = new Assign (fe, initializer, Location).ResolveStatement (ec);
+                       return initializer;
+               }
+
+               public bool HasDefaultInitializer
+               {
+                       get
+                       {
+                               Constant c = initializer as Constant;
+                               if (c == null)
+                                       return false;
+
+                               return c.IsDefaultInitializer (MemberType);
+                       }
+               }
+
+               public override string[] ValidAttributeTargets 
+               {
                        get {
                                return attribute_targets;
                        }
                }
 
-               protected override bool VerifyClsCompliance (DeclSpace ds)
+               protected override bool VerifyClsCompliance ()
                {
-                       if (!base.VerifyClsCompliance (ds))
+                       if (!base.VerifyClsCompliance ())
                                return false;
 
                        if (!IsFieldClsCompliant) {
@@ -5710,7 +5452,7 @@ namespace Mono.CSharp {
 
        public abstract class FieldMember : FieldBase
        {
-               protected FieldMember (TypeContainer parent, Expression type, int mod,
+               protected FieldMember (DeclSpace parent, Expression type, int mod,
                        int allowed_mod, MemberName name, Attributes attrs)
                        : base (parent, type, mod, allowed_mod | Modifiers.ABSTRACT, name, attrs)
                {
@@ -5724,7 +5466,7 @@ namespace Mono.CSharp {
                        {
                                status |= Status.HAS_OFFSET;
 
-                               if (!Parent.HasExplicitLayout) {
+                               if (!ParentContainer.HasExplicitLayout) {
                                        Report.Error (636, Location, "The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)");
                                        return;
                                }
@@ -5743,18 +5485,11 @@ namespace Mono.CSharp {
                        base.ApplyAttributeBuilder (a, cb);
                }
 
-
                public override bool Define()
                {
-                       EmitContext ec = Parent.EmitContext;
-                       if (ec == null)
-                               throw new InternalErrorException ("FieldMember.Define called too early");
-
                        if (MemberType == null || Type == null)
                                return false;
                        
-                       CheckObsoleteType (Type);
-
                        if (MemberType == TypeManager.void_type) {
                                Report.Error (1547, Location, "Keyword 'void' cannot be used in this context");
                                return false;
@@ -5774,20 +5509,16 @@ namespace Mono.CSharp {
                        if (!IsTypePermitted ())
                                return false;
 
-                       if (MemberType.IsPointer && !UnsafeOK (Parent))
-                               return false;
-
                        return true;
                }
 
                public override void Emit ()
                {
                        if (OptAttributes != null) {
-                               EmitContext ec = new EmitContext (Parent, Location, null, FieldBuilder.FieldType, ModFlags);
-                               OptAttributes.Emit (ec, this);
+                               OptAttributes.Emit ();
                        }
 
-                       if (Parent.HasExplicitLayout && ((status & Status.HAS_OFFSET) == 0) && (ModFlags & Modifiers.STATIC) == 0) {
+                       if (((status & Status.HAS_OFFSET) == 0) && (ModFlags & Modifiers.STATIC) == 0 && ParentContainer.HasExplicitLayout) {
                                Report.Error (625, Location, "`{0}': Instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute.", GetSignatureForError ());
                        }
 
@@ -5856,7 +5587,7 @@ namespace Mono.CSharp {
                        Modifiers.INTERNAL |
                        Modifiers.PRIVATE;
 
-               public FixedField (TypeContainer parent, Expression type, int mod, string name,
+               public FixedField (DeclSpace parent, Expression type, int mod, string name,
                        Expression size_expr, Attributes attrs, Location loc):
                        base (parent, type, mod, AllowedModifiers, new MemberName (name, loc), attrs)
                {
@@ -5873,7 +5604,7 @@ namespace Mono.CSharp {
                                Report.Warning (-23, 1, Location, "Only private or internal fixed sized buffers are supported by .NET 1.x");
 #endif
 
-                       if (Parent.Kind != Kind.Struct) {
+                       if (ParentContainer.Kind != Kind.Struct) {
                                Report.Error (1642, Location, "`{0}': Fixed size buffer fields may only be members of structs",
                                        GetSignatureForError ());
                                return false;
@@ -5888,7 +5619,8 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       Constant c = size_expr.ResolveAsConstant (Parent.EmitContext, this);
+                       EmitContext ec = new EmitContext (this, Parent, Location, null, null, ModFlags);
+                       Constant c = size_expr.ResolveAsConstant (ec, this);
                        if (c == null)
                                return false;
 
@@ -5985,7 +5717,7 @@ namespace Mono.CSharp {
                        Modifiers.UNSAFE |
                        Modifiers.READONLY;
 
-               public Field (TypeContainer parent, Expression type, int mod, string name,
+               public Field (DeclSpace parent, Expression type, int mod, string name,
                              Attributes attrs, Location loc)
                        : base (parent, type, mod, AllowedModifiers, new MemberName (name, loc),
                                attrs)
@@ -6042,7 +5774,7 @@ namespace Mono.CSharp {
 
                        FieldAttributes fa = Modifiers.FieldAttr (ModFlags);
 
-                       if (Parent.Kind == Kind.Struct && 
+                       if (ParentContainer.Kind == Kind.Struct && 
                            ((fa & FieldAttributes.Static) == 0) &&
                            MemberType == Parent.TypeBuilder &&
                            !TypeManager.IsBuiltinType (MemberType)){
@@ -6065,9 +5797,9 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               protected override bool VerifyClsCompliance (DeclSpace ds)
+               protected override bool VerifyClsCompliance ()
                {
-                       if (!base.VerifyClsCompliance (ds))
+                       if (!base.VerifyClsCompliance ())
                                return false;
 
                        if ((ModFlags & Modifiers.VOLATILE) != 0) {
@@ -6171,7 +5903,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               public bool IsExcluded (EmitContext ec)
+               public bool IsExcluded ()
                {
                        return false;
                }
@@ -6196,7 +5928,7 @@ namespace Mono.CSharp {
 
                public abstract Parameters ParameterInfo { get ; }
                public abstract Type ReturnType { get; }
-               public abstract EmitContext CreateEmitContext(TypeContainer tc, ILGenerator ig);
+               public abstract EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig);
 
                #endregion
 
@@ -6243,9 +5975,12 @@ namespace Mono.CSharp {
                        throw new NotSupportedException ();
                }
 
-               public virtual void Emit (TypeContainer container)
+               public void Emit (DeclSpace parent)
                {
-                       EmitMethod (container);
+                       EmitMethod (parent);
+
+                       if (OptAttributes != null)
+                               OptAttributes.Emit ();
 
                        if (declarative_security != null) {
                                foreach (DictionaryEntry de in declarative_security) {
@@ -6256,12 +5991,12 @@ namespace Mono.CSharp {
                        block = null;
                }
 
-               protected virtual void EmitMethod (TypeContainer container)
+               protected virtual void EmitMethod (DeclSpace parent)
                {
-                       method_data.Emit (container, this);
+                       method_data.Emit (parent);
                }
 
-               public override bool IsClsComplianceRequired(DeclSpace ds)
+               public override bool IsClsComplianceRequired()
                {
                        return false;
                }
@@ -6330,13 +6065,13 @@ namespace Mono.CSharp {
                        {
                        }
 
-                       public override MethodBuilder Define(TypeContainer container)
+                       public override MethodBuilder Define (DeclSpace parent)
                        {
-                               base.Define (container);
+                               base.Define (parent);
                                
                                method_data = new MethodData (method, ModFlags, flags, this);
 
-                               if (!method_data.Define (container))
+                               if (!method_data.Define (parent))
                                        return null;
 
                                return method_data.MethodBuilder;
@@ -6364,7 +6099,7 @@ namespace Mono.CSharp {
                public class SetMethod : PropertyMethod {
 
                        static string[] attribute_targets = new string [] { "method", "param", "return" };
-                       ImplicitParameter param_attr;
+                       ImplicitParameter param_attr;
                        protected Parameters parameters;
 
                        public SetMethod (MethodCore method):
@@ -6381,7 +6116,7 @@ namespace Mono.CSharp {
                        {
                                if (a.Target == AttributeTargets.Parameter) {
                                        if (param_attr == null)
-                                               param_attr = new ImplicitParameter (method_data.MethodBuilder, method.Location);
+                                               param_attr = new ImplicitParameter (method_data.MethodBuilder);
 
                                        param_attr.ApplyAttributeBuilder (a, cb);
                                        return;
@@ -6398,26 +6133,22 @@ namespace Mono.CSharp {
 
                        protected virtual void DefineParameters ()
                        {
-                               Parameter [] parms = new Parameter [1];
-                               parms [0] = new Parameter (method.MemberType, "value", Parameter.Modifier.NONE, null, Location);
-                               parameters = new Parameters (parms);
-                               parameters.Resolve (null);
+                               parameters = new Parameters (
+                                       new Parameter[] { new Parameter (method.MemberType, "value", Parameter.Modifier.NONE, null, Location) },
+                                       new Type[] { method.MemberType });
                        }
 
-                       public override MethodBuilder Define (TypeContainer container)
+                       public override MethodBuilder Define (DeclSpace parent)
                        {
-                               if (container.EmitContext == null)
-                                       throw new InternalErrorException ("SetMethod.Define called too early");
-
                                DefineParameters ();
                                if (IsDummy)
                                        return null;
 
-                               base.Define (container);
+                               base.Define (parent);
 
                                method_data = new MethodData (method, ModFlags, flags, this);
 
-                               if (!method_data.Define (container))
+                               if (!method_data.Define (parent))
                                        return null;
 
                                return method_data.MethodBuilder;
@@ -6438,7 +6169,8 @@ namespace Mono.CSharp {
 
                static string[] attribute_targets = new string [] { "property" };
 
-               public abstract class PropertyMethod : AbstractPropertyEventMethod {
+               public abstract class PropertyMethod : AbstractPropertyEventMethod
+               {
                        protected readonly MethodCore method;
                        protected MethodAttributes flags;
                        bool yields;
@@ -6449,7 +6181,8 @@ namespace Mono.CSharp {
                                this.method = method;
                        }
 
-                       public PropertyMethod (MethodCore method, Accessor accessor, string prefix)
+                       public PropertyMethod (MethodCore method, Accessor accessor,
+                                              string prefix)
                                : base (method, accessor, prefix)
                        {
                                this.method = method;
@@ -6467,16 +6200,18 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       public override bool IsClsComplianceRequired(DeclSpace ds)
+                       public override bool IsClsComplianceRequired ()
                        {
-                               return method.IsClsComplianceRequired (ds);
+                               return method.IsClsComplianceRequired ();
                        }
 
-                       public virtual MethodBuilder Define (TypeContainer container)
+                       public virtual MethodBuilder Define (DeclSpace parent)
                        {
                                if (!method.CheckAbstractAndExtern (block != null))
                                        return null;
 
+                               TypeContainer container = ((TypeContainer) parent).PartialContainer;
+
                                //
                                // Check for custom access modifier
                                //
@@ -6492,7 +6227,7 @@ namespace Mono.CSharp {
                                                Report.Error (442, Location, "`{0}': abstract properties cannot have private accessors", GetSignatureForError ());
                                        }
 
-                                       CheckModifiers (container, ModFlags);
+                                       CheckModifiers (ModFlags);
                                        ModFlags |= (method.ModFlags & (~Modifiers.Accessibility));
                                        ModFlags |= Modifiers.PROPERTY_CUSTOM;
                                        flags = Modifiers.MethodAttr (ModFlags);
@@ -6519,11 +6254,10 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       public override EmitContext CreateEmitContext (TypeContainer tc,
-                                                                      ILGenerator ig)
+                       public override EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
                        {
-                               return new EmitContext (
-                                       tc, method.ds, method.Location, ig, ReturnType,
+                               return new EmitContext (method,
+                                       ds, method.ds, method.Location, ig, ReturnType,
                                        method.ModFlags, false);
                        }
 
@@ -6537,7 +6271,7 @@ namespace Mono.CSharp {
                                return method.GetSignatureForError () + '.' + prefix.Substring (0, 3);
                        }
 
-                       void CheckModifiers (TypeContainer container, int modflags)
+                       void CheckModifiers (int modflags)
                        {
                                int flags = 0;
                                int mflags = method.ModFlags & Modifiers.Accessibility;
@@ -6574,7 +6308,7 @@ namespace Mono.CSharp {
 
                protected EmitContext ec;
 
-               public PropertyBase (TypeContainer parent, Expression type, int mod_flags,
+               public PropertyBase (DeclSpace parent, Expression type, int mod_flags,
                                     int allowed_mod, bool is_iface, MemberName name,
                                     Parameters parameters, Attributes attrs)
                        : base (parent, null, type, mod_flags, allowed_mod, is_iface, name,
@@ -6636,13 +6370,13 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       ec = new EmitContext (Parent, Location, null, MemberType, ModFlags);
+                       ec = new EmitContext (this, Parent, Location, null, MemberType, ModFlags);
                        return true;
                }
 
                protected override bool CheckForDuplications ()
                {
-                       ArrayList ar = Parent.Indexers;
+                       ArrayList ar = ParentContainer.Indexers;
                        if (ar != null) {
                                int arLen = ar.Count;
                                        
@@ -6653,7 +6387,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       ar = Parent.Properties;
+                       ar = ParentContainer.Properties;
                        if (ar != null) {
                                int arLen = ar.Count;
                                        
@@ -6668,10 +6402,10 @@ namespace Mono.CSharp {
                }
 
                // TODO: rename to Resolve......
-               protected override MethodInfo FindOutBaseMethod (TypeContainer container, ref Type base_ret_type)
+               protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
                {
-                       PropertyInfo base_property = container.BaseCache.FindMemberToOverride (
-                               container.TypeBuilder, Name, ParameterTypes, null, true) as PropertyInfo;
+                       PropertyInfo base_property = ParentContainer.BaseCache.FindMemberToOverride (
+                               Parent.TypeBuilder, Name, ParameterTypes, null, true) as PropertyInfo;
 
                        if (base_property == null)
                                return null;
@@ -6731,7 +6465,7 @@ namespace Mono.CSharp {
                        // put the attribute
                        //
                        if (PropertyBuilder != null && OptAttributes != null)
-                               OptAttributes.Emit (ec, this);
+                               OptAttributes.Emit ();
 
                        if (!Get.IsDummy)
                                Get.Emit (Parent);
@@ -6801,10 +6535,10 @@ namespace Mono.CSharp {
                const int AllowedInterfaceModifiers =
                        Modifiers.NEW;
 
-               public Property (TypeContainer ds, Expression type, int mod, bool is_iface,
+               public Property (DeclSpace parent, Expression type, int mod, bool is_iface,
                                 MemberName name, Attributes attrs, Accessor get_block,
                                 Accessor set_block)
-                       : base (ds, type, mod,
+                       : base (parent, type, mod,
                                is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
                                is_iface, name, Parameters.EmptyReadOnlyParameters, attrs)
                {
@@ -6855,6 +6589,7 @@ namespace Mono.CSharp {
                        if (!Set.IsDummy)
                                PropertyBuilder.SetSetMethod (SetBuilder);
 
+                       TypeManager.RegisterProperty (PropertyBuilder, this);
                        return true;
                }
        }
@@ -7012,7 +6747,7 @@ namespace Mono.CSharp {
 
                static string[] attribute_targets = new string [] { "event" }; // "property" target was disabled for 2.0 version
 
-               public EventProperty (TypeContainer parent, Expression type, int mod_flags,
+               public EventProperty (DeclSpace parent, Expression type, int mod_flags,
                                      bool is_iface, MemberName name,
                                      Attributes attrs, Accessor add, Accessor remove)
                        : base (parent, type, mod_flags, is_iface, name, attrs)
@@ -7040,7 +6775,7 @@ namespace Mono.CSharp {
                static string[] attribute_targets = new string [] { "event", "field", "method" };
                static string[] attribute_targets_interface = new string[] { "event", "method" };
 
-               public EventField (TypeContainer parent, Expression type, int mod_flags,
+               public EventField (DeclSpace parent, Expression type, int mod_flags,
                                   bool is_iface, MemberName name,
                                   Attributes attrs)
                        : base (parent, type, mod_flags, is_iface, name, attrs)
@@ -7131,10 +6866,10 @@ namespace Mono.CSharp {
 
                }
 
-               public abstract class DelegateMethod: AbstractPropertyEventMethod
+               public abstract class DelegateMethod : AbstractPropertyEventMethod
                {
                        protected readonly Event method;
-                       ImplicitParameter param_attr;
+                       ImplicitParameter param_attr;
 
                        static string[] attribute_targets = new string [] { "method", "param", "return" };
 
@@ -7154,7 +6889,7 @@ namespace Mono.CSharp {
                        {
                                if (a.Target == AttributeTargets.Parameter) {
                                        if (param_attr == null)
-                                               param_attr = new ImplicitParameter (method_data.MethodBuilder, method.Location);
+                                               param_attr = new ImplicitParameter (method_data.MethodBuilder);
 
                                        param_attr.ApplyAttributeBuilder (a, cb);
                                        return;
@@ -7169,29 +6904,29 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       public override bool IsClsComplianceRequired(DeclSpace ds)
+                       public override bool IsClsComplianceRequired ()
                        {
-                               return method.IsClsComplianceRequired (ds);
+                               return method.IsClsComplianceRequired ();
                        }
 
-                       public MethodBuilder Define (TypeContainer container)
+                       public MethodBuilder Define (DeclSpace parent)
                        {
                                method_data = new MethodData (method, method.ModFlags,
                                        method.flags | MethodAttributes.HideBySig | MethodAttributes.SpecialName, this);
 
-                               if (!method_data.Define (container))
+                               if (!method_data.Define (parent))
                                        return null;
 
                                MethodBuilder mb = method_data.MethodBuilder;
-                               ParameterInfo.ApplyAttributes (Parent.EmitContext, mb);
+                               ParameterInfo.ApplyAttributes (mb);
                                return mb;
                        }
 
 
-                       protected override void EmitMethod (TypeContainer tc)
+                       protected override void EmitMethod (DeclSpace parent)
                        {
                                if (block != null) {
-                                       base.EmitMethod (tc);
+                                       base.EmitMethod (parent);
                                        return;
                                }
 
@@ -7228,11 +6963,10 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       public override EmitContext CreateEmitContext (TypeContainer tc,
-                                                                      ILGenerator ig)
+                       public override EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
                        {
                                return new EmitContext (
-                                       tc, method.Parent, Location, ig, ReturnType,
+                                       ds, method.Parent, Location, ig, ReturnType,
                                        method.ModFlags, false);
                        }
 
@@ -7277,7 +7011,7 @@ namespace Mono.CSharp {
                public MethodBuilder AddBuilder, RemoveBuilder;
                Parameters parameters;
 
-               protected Event (TypeContainer parent, Expression type, int mod_flags,
+               protected Event (DeclSpace parent, Expression type, int mod_flags,
                              bool is_iface, MemberName name, Attributes attrs)
                        : base (parent, type, mod_flags,
                                is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
@@ -7323,18 +7057,9 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       EmitContext ec = Parent.EmitContext;
-                       if (ec == null)
-                               throw new InternalErrorException ("Event.Define called too early?");
-                       bool old_unsafe = ec.InUnsafe;
-                       ec.InUnsafe = InUnsafe;
-
-                       Parameter [] parms = new Parameter [1];
-                       parms [0] = new Parameter (MemberType, "value", Parameter.Modifier.NONE, null, Location);
-                       parameters = new Parameters (parms);
-                       parameters.Resolve (null);
-
-                       ec.InUnsafe = old_unsafe;
+                       parameters = new Parameters (
+                               new Parameter[] { new Parameter (MemberType, "value", Parameter.Modifier.NONE, null, Location) },
+                               new Type[] { MemberType } );
 
                        if (!CheckBase ())
                                return false;
@@ -7352,20 +7077,20 @@ namespace Mono.CSharp {
                                return false;
 
                        EventBuilder = new MyEventBuilder (this, Parent.TypeBuilder, Name, e_attr, MemberType);
-                                       
+                       
                        if (Add.Block == null && Remove.Block == null && !IsInterface) {
-                                       FieldBuilder = Parent.TypeBuilder.DefineField (
-                                               Name, MemberType,
-                                               FieldAttributes.Private | ((ModFlags & Modifiers.STATIC) != 0 ? FieldAttributes.Static : 0));
-                                       TypeManager.RegisterPrivateFieldOfEvent (
-                                               (EventInfo) EventBuilder, FieldBuilder);
-                                       TypeManager.RegisterFieldBase (FieldBuilder, this);
-                               }
+                               FieldBuilder = Parent.TypeBuilder.DefineField (
+                                       Name, MemberType,
+                                       FieldAttributes.Private | ((ModFlags & Modifiers.STATIC) != 0 ? FieldAttributes.Static : 0));
+                               TypeManager.RegisterPrivateFieldOfEvent (
+                                       (EventInfo) EventBuilder, FieldBuilder);
+                               TypeManager.RegisterFieldBase (FieldBuilder, this);
+                       }
                        
-                               EventBuilder.SetAddOnMethod (AddBuilder);
-                               EventBuilder.SetRemoveOnMethod (RemoveBuilder);
+                       EventBuilder.SetAddOnMethod (AddBuilder);
+                       EventBuilder.SetRemoveOnMethod (RemoveBuilder);
 
-                               TypeManager.RegisterEvent (EventBuilder, AddBuilder, RemoveBuilder);
+                       TypeManager.RegisterEvent (EventBuilder, AddBuilder, RemoveBuilder);
                        return true;
                }
 
@@ -7388,9 +7113,7 @@ namespace Mono.CSharp {
                public override void Emit ()
                {
                        if (OptAttributes != null) {
-                               EmitContext ec = new EmitContext (
-                                       Parent, Location, null, MemberType, ModFlags);
-                               OptAttributes.Emit (ec, this);
+                               OptAttributes.Emit ();
                        }
 
                        Add.Emit (Parent);
@@ -7448,15 +7171,8 @@ namespace Mono.CSharp {
 
                        protected override void DefineParameters ()
                        {
-                               Parameter [] fixed_parms = method.Parameters.FixedParameters;
-                               Parameter [] tmp = new Parameter [fixed_parms.Length + 1];
-
-                               fixed_parms.CopyTo (tmp, 0);
-                               tmp [fixed_parms.Length] = new Parameter (
-                                       method.MemberType, "value", Parameter.Modifier.NONE, null, method.Location);
-
-                               parameters = new Parameters (tmp);
-                               parameters.Resolve (null);
+                               parameters = Parameters.MergeGenerated (method.Parameters,
+                                       new Parameter (method.MemberType, "value", Parameter.Modifier.NONE, null, method.Location));
                        }
                }
 
@@ -7479,7 +7195,7 @@ namespace Mono.CSharp {
                //
                // Are we implementing an interface ?
                //
-               public Indexer (TypeContainer parent, Expression type, MemberName name, int mod,
+               public Indexer (DeclSpace parent, Expression type, MemberName name, int mod,
                                bool is_iface, Parameters parameters, Attributes attrs,
                                Accessor get_block, Accessor set_block)
                        : base (parent, type, mod,
@@ -7496,7 +7212,7 @@ namespace Mono.CSharp {
                        else
                                Set = new SetIndexerMethod (this, set_block);
                }
-
+                      
                public override bool Define ()
                {
                        if (!DoDefineBase ())
@@ -7511,12 +7227,16 @@ namespace Mono.CSharp {
                        }
 
                        if (OptAttributes != null) {
-                               Attribute indexer_attr = OptAttributes.Search (TypeManager.indexer_name_type, ec);
+                               Attribute indexer_attr = OptAttributes.Search (TypeManager.indexer_name_type);
                                if (indexer_attr != null) {
                                        // Remove the attribute from the list because it is not emitted
                                        OptAttributes.Attrs.Remove (indexer_attr);
 
-                                       ShortName = indexer_attr.GetIndexerAttributeValue (ec);
+                                       string name = indexer_attr.GetIndexerAttributeValue ();
+                                       if (name == null)
+                                               return false;
+
+                                       ShortName = name;
 
                                        if (IsExplicitImpl) {
                                                Report.Error (415, indexer_attr.Location,
@@ -7524,18 +7244,12 @@ namespace Mono.CSharp {
                                                              "indexer that is not an explicit interface member declaration");
                                                return false;
                                        }
-                               
+
                                        if ((ModFlags & Modifiers.OVERRIDE) != 0) {
                                                Report.Error (609, indexer_attr.Location,
                                                              "Cannot set the `IndexerName' attribute on an indexer marked override");
                                                return false;
                                        }
-
-                                       if (!Tokenizer.IsValidIdentifier (ShortName)) {
-                                               Report.Error (633, indexer_attr.Location,
-                                                             "The argument to the `IndexerName' attribute must be a valid identifier");
-                                               return false;
-                                       }
                                }
                        }
 
@@ -7545,8 +7259,8 @@ namespace Mono.CSharp {
                                        ShortName = base_IndexerName;
                        }
 
-                       if (!Parent.AddToMemberContainer (this) ||
-                               !Parent.AddToMemberContainer (Get) || !Parent.AddToMemberContainer (Set))
+                       if (!ParentContainer.AddToMemberContainer (this) ||
+                               !ParentContainer.AddToMemberContainer (Get) || !ParentContainer.AddToMemberContainer (Set))
                                return false;
 
                        if (!CheckBase ())
@@ -7588,14 +7302,14 @@ namespace Mono.CSharp {
                                }
                        }
 
-                               PropertyBuilder = Parent.TypeBuilder.DefineProperty (
+                       PropertyBuilder = Parent.TypeBuilder.DefineProperty (
                                Name, PropertyAttributes.None, MemberType, ParameterTypes);
+                       
+                       if (!Get.IsDummy)
+                               PropertyBuilder.SetGetMethod (GetBuilder);
 
-                               if (!Get.IsDummy)
-                                       PropertyBuilder.SetGetMethod (GetBuilder);
-
-                               if (!Set.IsDummy)
-                                       PropertyBuilder.SetSetMethod (SetBuilder);
+                       if (!Set.IsDummy)
+                               PropertyBuilder.SetSetMethod (SetBuilder);
                                
                        TypeManager.RegisterIndexer (PropertyBuilder, GetBuilder, SetBuilder, ParameterTypes);
 
@@ -7622,7 +7336,7 @@ namespace Mono.CSharp {
                }
        }
 
-       public class Operator : MethodCore, IIteratorContainer {
+       public class Operator : MethodOrOperator, IIteratorContainer {
 
                const int AllowedModifiers =
                        Modifiers.PUBLIC |
@@ -7672,13 +7386,8 @@ namespace Mono.CSharp {
                };
 
                public readonly OpType OperatorType;
-               public MethodBuilder   OperatorMethodBuilder;
                
-               public Method OperatorMethod;
-
-               static string[] attribute_targets = new string [] { "method", "return" };
-
-               public Operator (TypeContainer parent, OpType type, Expression ret_type,
+               public Operator (DeclSpace parent, OpType type, Expression ret_type,
                                 int mod_flags, Parameters parameters,
                                 ToplevelBlock block, Attributes attrs, Location loc)
                        : base (parent, null, ret_type, mod_flags, AllowedModifiers, false,
@@ -7690,18 +7399,17 @@ namespace Mono.CSharp {
 
                public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb) 
                {
-                       OperatorMethod.ApplyAttributeBuilder (a, cb);
-               }
-
-               public override AttributeTargets AttributeTargets {
-                       get {
-                               return AttributeTargets.Method; 
+                       if (a.Type == TypeManager.conditional_attribute_type) {
+                               Error_ConditionalAttributeIsNotValid ();
+                               return;
                        }
+
+                       base.ApplyAttributeBuilder (a, cb);
                }
                
-               protected override bool CheckForDuplications()
+               protected override bool CheckForDuplications ()
                {
-                       ArrayList ar = Parent.Operators;
+                       ArrayList ar = ParentContainer.Operators;
                        if (ar != null) {
                                int arLen = ar.Count;
 
@@ -7712,7 +7420,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       ar = Parent.Methods;
+                       ar = ParentContainer.Methods;
                        if (ar != null) {
                                int arLen = ar.Count;
 
@@ -7734,7 +7442,7 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       if (!DoDefine ())
+                       if (!base.Define ())
                                return false;
 
                        if (MemberType == TypeManager.void_type) {
@@ -7742,27 +7450,9 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       OperatorMethod = new Method (
-                               Parent, null, Type, ModFlags, false, MemberName,
-                               Parameters, OptAttributes);
-
-                       OperatorMethod.Block = Block;
-                       OperatorMethod.IsOperator = this;                       
-                       OperatorMethod.flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
-                       OperatorMethod.Define ();
-
-                       if (OperatorMethod.MethodBuilder == null)
-                               return false;
-                       
-                       OperatorMethodBuilder = OperatorMethod.MethodBuilder;
-
-                       Type[] parameter_types = OperatorMethod.ParameterTypes;
-                       Type declaring_type = OperatorMethod.MethodData.DeclaringType;
-                       Type return_type = OperatorMethod.ReturnType;
-                       Type first_arg_type = parameter_types [0];
-
-                       if (!CheckBase ())
-                               return false;
+                       Type declaring_type = MethodData.DeclaringType;
+                       Type return_type = MemberType;
+                       Type first_arg_type = ParameterTypes [0];
 
                        // Rules for conversion operators
                        
@@ -7780,15 +7470,6 @@ namespace Mono.CSharp {
                                                "enclosing type");
                                        return false;
                                }
-                               
-                               if (first_arg_type == TypeManager.object_type ||
-                                   return_type == TypeManager.object_type){
-                                       Report.Error (
-                                               -8, Location,
-                                               "User-defined conversion cannot convert to or from " +
-                                               "object type");
-                                       return false;
-                               }
 
                                if (first_arg_type.IsInterface || return_type.IsInterface){
                                        Report.Error (552, Location, "User-defined conversion `{0}' cannot convert to or from an interface type",
@@ -7796,9 +7477,8 @@ namespace Mono.CSharp {
                                        return false;
                                }
                                
-                               if (first_arg_type.IsSubclassOf (return_type)
-                                       || return_type.IsSubclassOf (first_arg_type)){
-                                       if (declaring_type.IsSubclassOf (return_type)) {
+                               if (first_arg_type.IsSubclassOf (return_type) || return_type.IsSubclassOf (first_arg_type)) {
+                                       if (declaring_type.IsSubclassOf (return_type) || declaring_type.IsSubclassOf (first_arg_type)) {
                                                Report.Error (553, Location, "User-defined conversion `{0}' cannot convert to or from base class",
                                                        GetSignatureForError ());
                                                return false;
@@ -7808,32 +7488,32 @@ namespace Mono.CSharp {
                                        return false;
                                }
                        } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
-                               if (first_arg_type != declaring_type || parameter_types [1] != TypeManager.int32_type) {
+                               if (first_arg_type != declaring_type || ParameterTypes [1] != TypeManager.int32_type) {
                                        Report.Error (564, Location, "Overloaded shift operator must have the type of the first operand be the containing type, and the type of the second operand must be int");
                                        return false;
                                }
                        } else if (Parameters.Count == 1) {
                                // Checks for Unary operators
-                               
+
                                if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
-                                       if (return_type != declaring_type && !return_type.IsSubclassOf (declaring_type)) {
+                                       if (return_type != declaring_type && !TypeManager.IsSubclassOf (return_type, declaring_type)) {
                                                Report.Error (448, Location,
                                                        "The return type for ++ or -- operator must be the containing type or derived from the containing type");
                                                return false;
                                        }
-                               if (first_arg_type != declaring_type){
-                                       Report.Error (
+                                       if (first_arg_type != declaring_type) {
+                                               Report.Error (
                                                        559, Location, "The parameter type for ++ or -- operator must be the containing type");
-                                       return false;
-                               }
+                                               return false;
+                                       }
                                }
                                
                                if (first_arg_type != declaring_type){
-                                               Report.Error (
+                                       Report.Error (
                                                562, Location,
                                                "The parameter of a unary operator must be the " +
                                                "containing type");
-                                               return false;
+                                       return false;
                                }
                                
                                if (OperatorType == OpType.True || OperatorType == OpType.False) {
@@ -7850,7 +7530,7 @@ namespace Mono.CSharp {
                                // Checks for Binary operators
                                
                                if (first_arg_type != declaring_type &&
-                                   parameter_types [1] != declaring_type){
+                                   ParameterTypes [1] != declaring_type){
                                        Report.Error (
                                                563, Location,
                                                "One of the parameters of a binary operator must " +
@@ -7861,21 +7541,45 @@ namespace Mono.CSharp {
 
                        return true;
                }
+
+               protected override bool DoDefine ()
+               {
+                       if (!base.DoDefine ())
+                               return false;
+
+                       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;
                        
-                       OperatorMethod.Emit ();
+                       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 (TypeContainer container, ref Type base_ret_type)
+               protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
                {
                        return null;
                }
@@ -7968,18 +7672,6 @@ namespace Mono.CSharp {
                        sb.Append (Parameters.GetSignatureForError ());
                        return sb.ToString ();
                }
-               
-               public override bool MarkForDuplicationCheck ()
-               {
-                       caching_flags |= Flags.TestMethodDuplication;
-                       return true;
-               }
-
-               public override string[] ValidAttributeTargets {
-                       get {
-                               return attribute_targets;
-                       }
-               }
        }
 
        //