2004-09-12 Marek Safar <marek.safar@seznam.cz>
[mono.git] / mcs / mcs / class.cs
index c02d1c0cf6dd9669067353aa8fb3c4754f7c9251..b845cf022b74815b951c9bf32432b9d50ceb51f8 100755 (executable)
@@ -3,6 +3,7 @@
 //
 // Authors: Miguel de Icaza (miguel@gnu.org)
 //          Martin Baulig (martin@gnome.org)
+//          Marek Safar (marek.safar@seznam.cz)
 //
 // Licensed under the terms of the GNU GPL
 //
@@ -35,57 +36,387 @@ using System.Reflection;
 using System.Reflection.Emit;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
+using System.Text;
+
+using Mono.CompilerServices.SymbolWriter;
 
 namespace Mono.CSharp {
 
+       public enum Kind {
+               Root,
+               Struct,
+               Class,
+               Interface
+       }
+
        /// <summary>
        ///   This is the base class for structs and classes.  
        /// </summary>
-       public class TypeContainer : DeclSpace, IMemberContainer {
+       public abstract class TypeContainer : DeclSpace, IMemberContainer {
+
+               public class MemberCoreArrayList: ArrayList
+               {
+                       /// <summary>
+                       ///   Defines the MemberCore objects that are in this array
+                       /// </summary>
+                       public virtual void DefineContainerMembers ()
+                       {
+                               foreach (MemberCore mc in this) {
+                                       mc.Define ();
+                               }
+                       }
+
+                       public virtual void Emit ()
+                       {
+                               foreach (MemberCore mc in this)
+                                       mc.Emit ();
+                       }
+               }
+
+               public class MethodArrayList: MemberCoreArrayList
+               {
+                       [Flags]
+                       enum CachedMethods {
+                               Equals                  = 1,
+                               GetHashCode             = 1 << 1
+                       }
+                       CachedMethods cached_method;
+                       TypeContainer container;
+
+                       public MethodArrayList (TypeContainer container)
+                       {
+                               this.container = container;
+                       }
+                       /// <summary>
+                       /// Method container contains Equals method
+                       /// </summary>
+                       public bool HasEquals {
+                               set {
+                                       cached_method |= CachedMethods.Equals;
+                               }
+                               get {
+                                       return (cached_method & CachedMethods.Equals) != 0;
+                               }
+                       }
+                       /// <summary>
+                       /// Method container contains GetHashCode method
+                       /// </summary>
+                       public bool HasGetHashCode {
+                               set {
+                                       cached_method |= CachedMethods.GetHashCode;
+                               }
+                               get {
+                                       return (cached_method & CachedMethods.GetHashCode) != 0;
+                               }
+                       }
+                       public override void DefineContainerMembers ()
+                       {
+                               base.DefineContainerMembers ();
+                               if ((RootContext.WarningLevel >= 3) && HasEquals && !HasGetHashCode) {
+                                       Report.Warning (659, container.Location, "'{0}' overrides Object.Equals(object) but does not override Object.GetHashCode()", container.GetSignatureForError ());
+                               }
+                       }
+               }
+
+               public sealed class IndexerArrayList: MemberCoreArrayList
+               {
+                       /// <summary>
+                       /// The indexer name for this container
+                       /// </summary>
+                       public string IndexerName = DefaultIndexerName;
+
+                       bool seen_normal_indexers = false;
+
+                       TypeContainer container;
+
+                       public IndexerArrayList (TypeContainer container)
+                       {
+                               this.container = container;
+                       }
+
+                       /// <summary>
+                       /// Defines the indexers, and also verifies that the IndexerNameAttribute in the
+                       /// class is consistent.  Either it is `Item' or it is the name defined by all the
+                       /// indexers with the `IndexerName' attribute.
+                       ///
+                       /// Turns out that the IndexerNameAttribute is applied to each indexer,
+                       /// but it is never emitted, instead a DefaultMember attribute is attached
+                       /// to the class.
+                       /// </summary>
+                       public override void DefineContainerMembers()
+                       {
+                               base.DefineContainerMembers ();
+
+                               string class_indexer_name = null;
+
+                               //
+                               // If there's both an explicit and an implicit interface implementation, the
+                               // explicit one actually implements the interface while the other one is just
+                               // a normal indexer.  See bug #37714.
+                               //
+
+                               // Invariant maintained by AddIndexer(): All explicit interface indexers precede normal indexers
+                               foreach (Indexer i in this) {
+                                       if (i.InterfaceType != null) {
+                                               if (seen_normal_indexers)
+                                                       throw new Exception ("Internal Error: 'Indexers' array not sorted properly.");
+                                               continue;
+                                       }
+
+                                       seen_normal_indexers = true;
+
+                                       if (class_indexer_name == null) {
+                                               class_indexer_name = i.ShortName;
+                                               continue;
+                                       }
+
+                                       if (i.ShortName != class_indexer_name)
+                                               Report.Error (668, i.Location, "Two indexers have different names; the IndexerName attribute must be used with the same name on every indexer within a type");
+                               }
+
+                               if (class_indexer_name != null)
+                                       IndexerName = class_indexer_name;
+                       }
+
+                       public override void Emit ()
+                       {
+                               base.Emit ();
+
+                               if (!seen_normal_indexers)
+                                       return;
+
+                               CustomAttributeBuilder cb = new CustomAttributeBuilder (TypeManager.default_member_ctor, new string [] { IndexerName });
+                               container.TypeBuilder.SetCustomAttribute (cb);
+                       }
+               }
+
+               public class OperatorArrayList: MemberCoreArrayList
+               {
+                       TypeContainer container;
+
+                       public OperatorArrayList (TypeContainer container)
+                       {
+                               this.container = container;
+                       }
+
+                       //
+                       // Operator pair checking
+                       //
+                       class OperatorEntry
+                       {
+                               public int flags;
+                               public Type ret_type;
+                               public Type type1, type2;
+                               public Operator op;
+                               public Operator.OpType ot;
+                               
+                               public OperatorEntry (int f, Operator o)
+                               {
+                                       flags = f;
+
+                                       ret_type = o.OperatorMethod.ReturnType;
+                                       Type [] pt = o.OperatorMethod.ParameterTypes;
+                                       type1 = pt [0];
+                                       type2 = pt [1];
+                                       op = o;
+                                       ot = o.OperatorType;
+                               }
+
+                               public override int GetHashCode ()
+                               {       
+                                       return ret_type.GetHashCode ();
+                               }
+
+                               public override bool Equals (object o)
+                               {
+                                       OperatorEntry other = (OperatorEntry) o;
+
+                                       if (other.ret_type != ret_type)
+                                               return false;
+                                       if (other.type1 != type1)
+                                               return false;
+                                       if (other.type2 != type2)
+                                               return false;
+                                       return true;
+                               }
+                       }
+                               
+                       //
+                       // Checks that some operators come in pairs:
+                       //  == and !=
+                       // > and <
+                       // >= and <=
+                       // true and false
+                       //
+                       // They are matched based on the return type and the argument types
+                       //
+                       void CheckPairedOperators ()
+                       {
+                               Hashtable pairs = new Hashtable (null, null);
+                               Operator true_op = null;
+                               Operator false_op = null;
+                               bool has_equality_or_inequality = false;
+                               
+                               // Register all the operators we care about.
+                               foreach (Operator op in this){
+                                       int reg = 0;
+                                       
+                                       switch (op.OperatorType){
+                                       case Operator.OpType.Equality:
+                                               reg = 1;
+                                               has_equality_or_inequality = true;
+                                               break;
+                                       case Operator.OpType.Inequality:
+                                               reg = 2;
+                                               has_equality_or_inequality = true;
+                                               break;
+
+                                       case Operator.OpType.True:
+                                               true_op = op;
+                                               break;
+                                       case Operator.OpType.False:
+                                               false_op = op;
+                                               break;
+                                               
+                                       case Operator.OpType.GreaterThan:
+                                               reg = 1; break;
+                                       case Operator.OpType.LessThan:
+                                               reg = 2; break;
+                                               
+                                       case Operator.OpType.GreaterThanOrEqual:
+                                               reg = 1; break;
+                                       case Operator.OpType.LessThanOrEqual:
+                                               reg = 2; break;
+                                       }
+                                       if (reg == 0)
+                                               continue;
+
+                                       OperatorEntry oe = new OperatorEntry (reg, op);
+
+                                       object o = pairs [oe];
+                                       if (o == null)
+                                               pairs [oe] = oe;
+                                       else {
+                                               oe = (OperatorEntry) o;
+                                               oe.flags |= reg;
+                                       }
+                               }
+
+                               if (true_op != null){
+                                       if (false_op == null)
+                                               Report.Error (216, true_op.Location, "operator true requires a matching operator false");
+                               } else if (false_op != null)
+                                       Report.Error (216, false_op.Location, "operator false requires a matching operator true");
+                               
+                               //
+                               // Look for the mistakes.
+                               //
+                               foreach (DictionaryEntry de in pairs){
+                                       OperatorEntry oe = (OperatorEntry) de.Key;
+
+                                       if (oe.flags == 3)
+                                               continue;
+
+                                       string s = "";
+                                       switch (oe.ot){
+                                       case Operator.OpType.Equality:
+                                               s = "!=";
+                                               break;
+                                       case Operator.OpType.Inequality: 
+                                               s = "==";
+                                               break;
+                                       case Operator.OpType.GreaterThan: 
+                                               s = "<";
+                                               break;
+                                       case Operator.OpType.LessThan:
+                                               s = ">";
+                                               break;
+                                       case Operator.OpType.GreaterThanOrEqual:
+                                               s = "<=";
+                                               break;
+                                       case Operator.OpType.LessThanOrEqual:
+                                               s = ">=";
+                                               break;
+                                       }
+                                       Report.Error (216, oe.op.Location,
+                                                       "The operator `" + oe.op + "' requires a matching operator `" + s + "' to also be defined");
+                               }
+
+                               if (has_equality_or_inequality && (RootContext.WarningLevel > 2)) {
+                                       if (container.Methods == null || !container.Methods.HasEquals)
+                                               Report.Warning (660, container.Location, "'{0}' defines operator == or operator != but does not override Object.Equals(object o)", container.GetSignatureForError ());
+                                       if (container.Methods == null || !container.Methods.HasGetHashCode)
+                                               Report.Warning (661, container.Location, "'{0}' defines operator == or operator != but does not override Object.GetHashCode()", container.GetSignatureForError ());
+                               }
+                       }
+
+                       public override void DefineContainerMembers ()
+                       {
+                               base.DefineContainerMembers ();
+                               CheckPairedOperators ();
+                       }
+               }
+
+
+               // Whether this is a struct, class or interface
+               public readonly Kind Kind;
+
                // Holds a list of classes and structures
                ArrayList types;
 
                // Holds the list of properties
-               ArrayList properties;
+               MemberCoreArrayList properties;
 
                // Holds the list of enumerations
-               ArrayList enums;
+               MemberCoreArrayList enums;
 
                // Holds the list of delegates
-               ArrayList delegates;
+               MemberCoreArrayList delegates;
                
                // Holds the list of constructors
-               ArrayList instance_constructors;
+               protected MemberCoreArrayList instance_constructors;
 
                // Holds the list of fields
-               ArrayList fields;
+               MemberCoreArrayList fields;
 
                // Holds a list of fields that have initializers
-               ArrayList initialized_fields;
+               protected ArrayList initialized_fields;
 
                // Holds a list of static fields that have initializers
-               ArrayList initialized_static_fields;
+               protected ArrayList initialized_static_fields;
 
                // Holds the list of constants
-               ArrayList constants;
+               MemberCoreArrayList constants;
 
                // Holds the list of
-               ArrayList interfaces;
+               MemberCoreArrayList interfaces;
 
-               // Holds order in which interfaces must be closed
-               ArrayList interface_order;
-               
                // Holds the methods.
-               ArrayList methods;
+               MethodArrayList methods;
 
                // Holds the events
-               ArrayList events;
+               MemberCoreArrayList events;
 
                // Holds the indexers
-               ArrayList indexers;
+               IndexerArrayList indexers;
 
                // Holds the operators
-               ArrayList operators;
+               MemberCoreArrayList operators;
+
+               // Holds the iterators
+               ArrayList iterators;
+
+               // Holds the parts of a partial class;
+               ArrayList parts;
 
                // The emit context for toplevel objects.
                EmitContext ec;
@@ -93,13 +424,8 @@ namespace Mono.CSharp {
                //
                // Pointers to the default constructor and the default static constructor
                //
-               Constructor default_constructor;
-               Constructor default_static_constructor;
-
-               //
-               // Whether we have seen a static constructor for this class or not
-               //
-               public bool UserDefinedStaticConstructor = false;
+               protected Constructor default_constructor;
+               protected Constructor default_static_constructor;
 
                //
                // Whether we have at least one non-static field
@@ -111,328 +437,261 @@ namespace Mono.CSharp {
                // from classes from the arraylist `type_bases' 
                //
                string     base_class_name;
+               public Type base_class_type;
 
                ArrayList type_bases;
 
                bool members_defined;
                bool members_defined_ok;
 
-               // Information in the case we are an attribute type
-
-               public AttributeTargets Targets = AttributeTargets.All;
-               public bool AllowMultiple = false;
-               public bool Inherited;
-
                // The interfaces we implement.
                TypeExpr [] ifaces;
-               Type[] base_inteface_types;
+               protected Type[] base_inteface_types;
 
                // The parent member container and our member cache
                IMemberContainer parent_container;
                MemberCache member_cache;
-               
-               //
-               // The indexer name for this class
-               //
-               public string IndexerName;
-
-               public TypeContainer ():
-                       this (null, null, "", null, new Location (-1)) {
-               }
 
-               public TypeContainer (NamespaceEntry ns, TypeContainer parent, string name, Attributes attrs, Location l)
+               public const string DefaultIndexerName = "Item";
+               
+               public TypeContainer (NamespaceEntry ns, TypeContainer parent, string name,
+                                     Attributes attrs, Kind kind, Location l)
                        : base (ns, parent, name, attrs, l)
                {
+                       this.Kind = kind;
+
                        types = new ArrayList ();
 
                        base_class_name = null;
                }
 
-               public AdditionResult AddConstant (Const constant)
+               public bool AddToMemberContainer (MemberCore symbol, bool is_method)
                {
-                       AdditionResult res;
-                       string basename = constant.Name;
-                       string fullname = Name + "." + basename;
+                       return AddToContainer (symbol, is_method, String.Concat (Name, '.', symbol.Name), symbol.Name);
+               }
 
-                       if ((res = IsValid (basename, fullname)) != AdditionResult.Success)
-                               return res;
-                       
-                       if (constants == null)
-                               constants = new ArrayList ();
+               bool AddToTypeContainer (DeclSpace ds)
+               {
+                       return AddToContainer (ds, false, ds.Name, ds.Basename);
+               }
 
-                       constants.Add (constant);
-                       DefineName (fullname, constant);
+               public void AddConstant (Const constant)
+               {
+                       if (!AddToMemberContainer (constant, false))
+                               return;
 
-                       return AdditionResult.Success;
+                       if (constants == null)
+                               constants = new MemberCoreArrayList ();
+                       
+                       constants.Add (constant);
                }
 
-               public AdditionResult AddEnum (Mono.CSharp.Enum e)
+               public void AddEnum (Mono.CSharp.Enum e)
                {
-                       AdditionResult res;
-
-                       if ((res = IsValid (e.Basename, e.Name)) != AdditionResult.Success)
-                               return res;
+                       if (!AddToTypeContainer (e))
+                               return;
 
                        if (enums == null)
-                               enums = new ArrayList ();
+                               enums = new MemberCoreArrayList ();
 
                        enums.Add (e);
-                       DefineName (e.Name, e);
-
-                       return AdditionResult.Success;
                }
                
-               public AdditionResult AddClass (Class c)
+               public void AddClassOrStruct (TypeContainer c)
                {
-                       AdditionResult res;
-                       string name = c.Basename;
-                       
-                       if ((res = IsValid (name, c.Name)) != AdditionResult.Success)
-                               return res;
+                       if (!AddToTypeContainer (c))
+                               return;
 
-                       DefineName (c.Name, c);
                        types.Add (c);
-
-                       return AdditionResult.Success;
-               }
-
-               public AdditionResult AddStruct (Struct s)
-               {
-                       AdditionResult res;
-                       string name = s.Basename;
-                       
-                       if ((res = IsValid (name, s.Name)) != AdditionResult.Success)
-                               return res;
-
-                       DefineName (s.Name, s);
-                       types.Add (s);
-
-                       return AdditionResult.Success;
                }
 
-               public AdditionResult AddDelegate (Delegate d)
+               public void AddDelegate (Delegate d)
                {
-                       AdditionResult res;
-                       string name = d.Basename;
-                       
-                       if ((res = IsValid (name, d.Name)) != AdditionResult.Success)
-                               return res;
+                       if (!AddToTypeContainer (d))
+                               return;
 
                        if (delegates == null)
-                               delegates = new ArrayList ();
+                               delegates = new MemberCoreArrayList ();
                        
-                       DefineName (d.Name, d);
                        delegates.Add (d);
-
-                       return AdditionResult.Success;
                }
 
-               public AdditionResult AddMethod (Method method)
+               public void AddMethod (Method method)
                {
-                       string basename = method.Name;
-                       string fullname = Name + "." + basename;
-
-                       Object value = defined_names [fullname];
-
-                       if (value != null && (!(value is Method)))
-                               return AdditionResult.NameExists;
-
-                       if (basename == Basename)
-                               return AdditionResult.EnclosingClash;
+                       if (!AddToMemberContainer (method, true))
+                               return;
 
                        if (methods == null)
-                               methods = new ArrayList ();
-
+                               methods = new MethodArrayList (this);
+                       
                        if (method.Name.IndexOf ('.') != -1)
                                methods.Insert (0, method);
                        else 
                                methods.Add (method);
-                       
-                       if (value == null)
-                               DefineName (fullname, method);
-
-                       return AdditionResult.Success;
                }
 
-               public AdditionResult AddConstructor (Constructor c)
+               public void AddConstructor (Constructor c)
                {
-                       if (c.Name != Basename) 
-                               return AdditionResult.NotAConstructor;
+                       if (c.Name != Basename)  {
+                               Report.Error (1520, Location, "Class, struct, or interface method must have a return type");
+                       }
 
                        bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
                        
                        if (is_static){
-                               UserDefinedStaticConstructor = true;
-                               if (default_static_constructor != null)
-                                       return AdditionResult.MethodExists;
+                               if (default_static_constructor != null) {
+                                       Report.SymbolRelatedToPreviousError (default_static_constructor);
+                                       Report.Error (111, c.Location, Error111, Name, c.Name);
+                                       return;
+                               }
 
                                default_static_constructor = c;
                        } else {
                                if (c.IsDefault ()){
-                                       if (default_constructor != null)
-                                               return AdditionResult.MethodExists;
+                                       if (default_constructor != null) {
+                                               Report.SymbolRelatedToPreviousError (default_constructor);
+                                               Report.Error (111, c.Location, Error111, c.Location, Name, c.Name);
+                                               return;
+                                       }
                                        default_constructor = c;
                                }
                                
                                if (instance_constructors == null)
-                                       instance_constructors = new ArrayList ();
+                                       instance_constructors = new MemberCoreArrayList ();
                                
                                instance_constructors.Add (c);
                        }
-                       
-                       return AdditionResult.Success;
+               }
+
+               internal static string Error111 {
+                       get {
+                               return "Type '{0}' already defines a member called '{1}' with the same parameter types";
+                       }
                }
                
-               public AdditionResult AddInterface (Interface iface)
+               public void AddInterface (TypeContainer iface)
                {
-                       AdditionResult res;
-                       string name = iface.Basename;
-                       
-                       if ((res = IsValid (name, iface.Name)) != AdditionResult.Success)
-                               return res;
-                       
-                       if (interfaces == null)
-                               interfaces = new ArrayList ();
+                       if (!AddToTypeContainer (iface))
+                               return;
+
+                       if (interfaces == null) {
+                               interfaces = new MemberCoreArrayList ();
+                       }
+
                        interfaces.Add (iface);
-                       DefineName (iface.Name, iface);
-                       
-                       return AdditionResult.Success;
                }
 
-               public AdditionResult AddField (Field field)
+               public void AddField (Field field)
                {
-                       AdditionResult res;
-                       string basename = field.Name;
-                       string fullname = Name + "." + basename;
+                       if (!AddToMemberContainer (field, false))
+                               return;
 
-                       if ((res = IsValid (basename, fullname)) != AdditionResult.Success)
-                               return res;
-                       
                        if (fields == null)
-                               fields = new ArrayList ();
+                               fields = new MemberCoreArrayList ();
                        
                        fields.Add (field);
-                       
+
                        if (field.HasInitializer){
                                if ((field.ModFlags & Modifiers.STATIC) != 0){
                                        if (initialized_static_fields == null)
                                                initialized_static_fields = new ArrayList ();
-
+                       
                                        initialized_static_fields.Add (field);
-
                                } else {
                                        if (initialized_fields == null)
                                                initialized_fields = new ArrayList ();
-                               
+                                                       
                                        initialized_fields.Add (field);
                                }
                        }
-
+                       
                        if ((field.ModFlags & Modifiers.STATIC) == 0)
                                have_nonstatic_fields = true;
-
-                       DefineName (fullname, field);
-                       return AdditionResult.Success;
                }
 
-               public AdditionResult AddProperty (Property prop)
+               public void AddProperty (Property prop)
                {
-                       AdditionResult res;
-
-                       if ((res = AddProperty (prop, prop.Name)) != AdditionResult.Success)
-                               return res;
-
-                       if (prop.Get != null) {
-                               if ((res = AddProperty (prop, "get_" + prop.Name)) != AdditionResult.Success)
-                                       return res;
-                       }
-
-                       if (prop.Set != null) {
-                               if ((res = AddProperty (prop, "set_" + prop.Name)) != AdditionResult.Success)
-                               return res;
-                       }
+                       if (!AddToMemberContainer (prop, false) || 
+                               !AddToMemberContainer (prop.Get, true) || !AddToMemberContainer (prop.Set, true))
+                               return;
 
                        if (properties == null)
-                               properties = new ArrayList ();
+                               properties = new MemberCoreArrayList ();
 
                        if (prop.Name.IndexOf ('.') != -1)
                                properties.Insert (0, prop);
                        else
                                properties.Add (prop);
-
-                       return AdditionResult.Success;
                }
 
-               AdditionResult AddProperty (Property prop, string basename)
+               public void AddEvent (Event e)
                {
-                       AdditionResult res;
-                       string fullname = Name + "." + basename;
-
-                       if ((res = IsValid (basename, fullname)) != AdditionResult.Success)
-                               return res;
-
-                       DefineName (fullname, prop);
-
-                       return AdditionResult.Success;
-               }
+                       if (!AddToMemberContainer (e, false))
+                               return;
 
-               public AdditionResult AddEvent (Event e)
-               {
-                       AdditionResult res;
-                       string basename = e.Name;
-                       string fullname = Name + "." + basename;
+                       if (e is EventProperty) {
+                               if (!AddToMemberContainer (e.Add, true))
+                                       return;
 
-                       if ((res = IsValid (basename, fullname)) != AdditionResult.Success)
-                               return res;
+                               if (!AddToMemberContainer (e.Remove, true))
+                                       return;
+                       }
 
                        if (events == null)
-                               events = new ArrayList ();
-                       
-                       events.Add (e);
-                       DefineName (fullname, e);
+                               events = new MemberCoreArrayList ();
 
-                       return AdditionResult.Success;
+                       events.Add (e);
                }
 
+
+               /// <summary>
+               /// Indexer has special handling in constrast to other AddXXX because the name can be driven by IndexerNameAttribute
+               /// </summary>
                public void AddIndexer (Indexer i)
                {
                        if (indexers == null)
-                               indexers = new ArrayList ();
+                               indexers = new IndexerArrayList (this);
 
-                       if (i.ExplicitInterfaceName != null)
+                       if (i.IsExplicitImpl)
                                indexers.Insert (0, i);
                        else
                                indexers.Add (i);
                }
 
-               public AdditionResult AddOperator (Operator op)
+               public void AddOperator (Operator op)
                {
+                       if (!AddToMemberContainer (op, true))
+                               return;
+
                        if (operators == null)
-                               operators = new ArrayList ();
+                               operators = new OperatorArrayList (this);
 
                        operators.Add (op);
+               }
 
-                       string basename = op.Name;
-                       string fullname = Name + "." + basename;
-                       if (!defined_names.Contains (fullname))
-                       {
-                               DefineName (fullname, op);
-                       }
-                       return AdditionResult.Success;
+               public void AddIterator (Iterator i)
+               {
+                       if (iterators == null)
+                               iterators = new ArrayList ();
+
+                       iterators.Add (i);
                }
 
-               public override void ApplyAttributeBuilder (object builder, Attribute a, CustomAttributeBuilder cb)
+               public void AddType (TypeContainer tc)
                {
-                       TypeBuilder tb = builder as TypeBuilder;
-                                       
-                       if (a.UsageAttr) {
-                               Targets = a.Targets;
-                               AllowMultiple = a.AllowMultiple;
-                               Inherited = a.Inherited;
-                               
-                               TypeManager.RegisterAttributeAllowMultiple (tb, AllowMultiple);
-                       }
+                       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) {
                                if (Indexers != null) {
                                        Report.Error (646, a.Location,
@@ -442,35 +701,22 @@ namespace Mono.CSharp {
                                }
                        }
                        
-                       try {
-                               tb.SetCustomAttribute (cb);
-                       } 
-                       catch (System.ArgumentException e) {
-                               Report.Warning (-21, a.Location,
-                                               "The CharSet named property on StructLayout\n"+
-                                               "\tdoes not work correctly on Microsoft.NET\n"+
-                                               "\tYou might want to remove the CharSet declaration\n"+
-                                               "\tor compile using the Mono runtime instead of the\n"+
-                                               "\tMicrosoft .NET runtime.\n"+
-                                               "\tThe runtime reported the error: " + e);
+                       base.ApplyAttributeBuilder (a, cb);
+               } 
+
+               public override AttributeTargets AttributeTargets {
+                       get {
+                               throw new NotSupportedException ();
                        }
                }
 
-               public void RegisterOrder (Interface iface)
-               {
-                       if (interface_order == null)
-                               interface_order = new ArrayList ();
-
-                       interface_order.Add (iface);
-               }
-               
                public ArrayList Types {
                        get {
                                return types;
                        }
                }
 
-               public ArrayList Methods {
+               public MethodArrayList Methods {
                        get {
                                return methods;
                        }
@@ -487,6 +733,12 @@ namespace Mono.CSharp {
                                return interfaces;
                        }
                }
+
+               public ArrayList Iterators {
+                       get {
+                               return iterators;
+                       }
+               }
                
                public string Base {
                        get {
@@ -508,10 +760,6 @@ namespace Mono.CSharp {
                        get {
                                return fields;
                        }
-
-                       set {
-                               fields = value;
-                       }
                }
 
                public ArrayList InstanceConstructors {
@@ -556,12 +804,24 @@ namespace Mono.CSharp {
                        }
                }
 
+               public ArrayList Parts {
+                       get {
+                               return parts;
+                       }
+               }
+
                public virtual TypeAttributes TypeAttr {
                        get {
                                return Modifiers.TypeAttr (ModFlags, this);
                        }
                }
 
+               public string IndexerName {
+                       get {
+                               return indexers == null ? DefaultIndexerName : indexers.IndexerName;
+                       }
+               }
+
                //
                // Emits the instance field initializers
                //
@@ -604,7 +864,7 @@ namespace Mono.CSharp {
                //
                // Defines the default constructors
                //
-               void DefineDefaultConstructor (bool is_static)
+               protected void DefineDefaultConstructor (bool is_static)
                {
                        Constructor c;
 
@@ -630,73 +890,84 @@ namespace Mono.CSharp {
                        
                }
 
-               public void ReportStructInitializedInstanceError ()
-               {
-                       string n = TypeBuilder.FullName;
-                       
-                       foreach (Field f in initialized_fields){
-                               Report.Error (
-                                       573, Location,
-                                       "`" + n + "." + f.Name + "': can not have " +
-                                       "instance field initializers in structs");
-                       }
-               }
-
                /// <remarks>
-               ///  The pending methods that need to be implemented (interfaces or abstract methods)
+               ///  The pending methods that need to be implemented
+               //   (interfaces or abstract methods)
                /// </remarks>
                public PendingImplementation Pending;
 
-               /// <summary>
-               ///   This function computes the Base class and also the
-               ///   list of interfaces that the class or struct @c implements.
-               ///   
-               ///   The return value is an array (might be null) of
-               ///   interfaces implemented (as Types).
-               ///   
-               ///   The @parent argument is set to the parent object or null
-               ///   if this is `System.Object'. 
-               /// </summary>
-               TypeExpr [] GetClassBases (bool is_class, bool is_iface,
-                                          out TypeExpr parent, out bool error)
+               public abstract void Register ();
+
+               public abstract PendingImplementation GetPendingImplementations ();
+
+               TypeExpr[] GetPartialBases (out TypeExpr parent, out bool error)
                {
-                       ArrayList bases = Bases;
-                       int count;
-                       int start, j, i;
+                       ArrayList ifaces = new ArrayList ();
 
-                       error = false;
+                       parent = null;
+                       Location parent_loc = Location.Null;
 
-                       if (is_class || is_iface)
-                               parent = null;
-                       else
-                               parent = TypeManager.system_valuetype_expr;
+                       foreach (ClassPart part in parts) {
+                               TypeExpr new_parent;
+                               TypeExpr[] new_ifaces;
 
-                       if (bases == null){
-                               if (is_class){
-                                       if (RootContext.StdLib)
-                                               parent = TypeManager.system_object_expr;
-                                       else if (Name != "System.Object")
-                                               parent = TypeManager.system_object_expr;
-                               } else {
-                                       //
-                                       // If we are compiling our runtime,
-                                       // and we are defining ValueType, then our
-                                       // parent is `System.Object'.
-                                       //
-                                       if (!RootContext.StdLib && Name == "System.ValueType")
-                                               parent = TypeManager.system_object_expr;
+                               new_ifaces = part.GetClassBases (out new_parent, out error);
+                               if (error)
+                                       return null;
+
+                               if ((parent != null) && (new_parent != null) &&
+                                   !parent.Equals (new_parent)) {
+                                       Report.Error (263, part.Location,
+                                                     "Partial declarations of `{0}' must " +
+                                                     "not specify different base classes",
+                                                     Name);
+
+                                       if (!Location.IsNull (parent_loc))
+                                               Report.LocationOfPreviousError (parent_loc);
+
+                                       error = true;
+                                       return null;
                                }
 
-                               return null;
+                               if ((parent == null) && (new_parent != null)) {
+                                       parent = new_parent;
+                                       parent_loc = part.Location;
+                               }
+
+                               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 (!found)
+                                               ifaces.Add (iface);
+                               }
                        }
 
-                       //
-                       // Bases should be null if there are no bases at all
-                       //
-                       count = bases.Count;
+                       error = false;
+
+                       TypeExpr[] retval = new TypeExpr [ifaces.Count];
+                       ifaces.CopyTo (retval, 0);
+                       return retval;
+               }
+
+               TypeExpr[] GetNormalBases (out TypeExpr parent, out bool error)
+               {
+                       parent = null;
+
+                       int count = Bases.Count;
+                       int start, i, j;
 
-                       if (is_class){
-                               TypeExpr name = ResolveTypeExpr ((Expression) bases [0], false, Location);
+                       if (Kind == Kind.Class){
+                               TypeExpr name = ResolveTypeExpr (
+                                       (Expression) Bases [0], false, Location);
 
                                if (name == null){
                                        error = true;
@@ -707,19 +978,68 @@ namespace Mono.CSharp {
                                        parent = name;
                                        start = 1;
                                } else {
-                                       parent = TypeManager.system_object_expr;
                                        start = 0;
                                }
-                               if (name.IsSealed){
-                                       string detail = "";
-                                       
-                                       if (name.IsValueType)
-                                               detail = " (a class can not inherit from a struct/enum)";
-                                       
-                                       Report.Error (509, "class `"+ Name +
-                                                     "': Cannot inherit from sealed class `"+
-                                                     name.Name + "'" + detail);
+                       } else {
+                               start = 0;
+                       }
+
+                       TypeExpr [] ifaces = new TypeExpr [count-start];
+                       
+                       for (i = start, j = 0; i < count; i++, j++){
+                               Expression name = (Expression) Bases [i];
+                               TypeExpr resolved = ResolveTypeExpr (name, false, Location);
+                               if (resolved == null) {
+                                       error = true;
+                                       return null;
+                               }
+                               
+                               ifaces [j] = resolved;
+                       }
+
+                       error = false;
+                       return ifaces;
+               }
+
+               /// <summary>
+               ///   This function computes the Base class and also the
+               ///   list of interfaces that the class or struct @c implements.
+               ///   
+               ///   The return value is an array (might be null) of
+               ///   interfaces implemented (as Types).
+               ///   
+               ///   The @parent argument is set to the parent object or null
+               ///   if this is `System.Object'. 
+               /// </summary>
+               TypeExpr [] GetClassBases (out TypeExpr parent, out bool error)
+               {
+                       ArrayList bases = Bases;
+                       int start, j, i;
+
+                       error = false;
+
+                       TypeExpr[] ifaces;
+
+                       if (parts != null)
+                               ifaces = GetPartialBases (out parent, out error);
+                       else if (Bases == null){
+                               parent = null;
+                               return null;
+                       } else
+                               ifaces = GetNormalBases (out parent, out error);
+
+                       if (error)
+                               return null;
+
+                       if ((parent != null) && (Kind == Kind.Class)){
+                               if (parent.IsSealed){
                                        error = true;
+                                       Report.SymbolRelatedToPreviousError (parent.Type);
+                                       if (parent.Type.IsAbstract) {
+                                               Report.Error (709, Location, "'{0}': Cannot derive from static class", GetSignatureForError ());
+                                       } else {
+                                               Report.Error (509, Location, "'{0}': Cannot derive from sealed class", GetSignatureForError ());
+                                       }
                                        return null;
                                }
 
@@ -734,58 +1054,59 @@ namespace Mono.CSharp {
                                if (!parent.AsAccessible (this, ModFlags))
                                        Report.Error (60, Location,
                                                      "Inconsistent accessibility: base class `" +
-                                                     name.Name + "' is less accessible than class `" +
+                                                     parent.Name + "' is less accessible than class `" +
                                                      Name + "'");
-
-                       } else {
-                               start = 0;
                        }
 
                        if (parent != null)
                                base_class_name = parent.Name;
 
-                       TypeExpr [] ifaces = new TypeExpr [count-start];
-                       
-                       for (i = start, j = 0; i < count; i++, j++){
-                               Expression name = (Expression) bases [i];
-                               TypeExpr resolved = ResolveTypeExpr (name, false, Location);
-                               if (resolved == null)
-                                       return null;
-                               
-                               bases [i] = resolved;
+                       if (ifaces == null)
+                               return null;
+
+                       int count = ifaces != null ? ifaces.Length : 0;
+
+                       for (i = 0; i < count; i++) {
+                               TypeExpr iface = (TypeExpr) ifaces [i];
+
+                               if ((Kind != Kind.Class) && !iface.IsInterface){
+                                       string what = Kind == Kind.Struct ?
+                                               "Struct" : "Interface";
 
-                               if (is_class == false && !resolved.IsInterface){
-                                       Report.Error (527, "In Struct `" + Name + "', type `"+
-                                                     name +"' is not an interface");
+                                       Report.Error (527, Location,
+                                                     "In {0} `{1}', type `{2}' is not "+
+                                                     "an interface", what, Name, iface.Name);
                                        error = true;
                                        return null;
                                }
                                
-                               if (resolved.IsClass) {
+                               if (iface.IsClass) {
                                        if (parent != null){
-                                               Report.Error (527, "In Class `" + Name + "', type `"+
-                                                             name+"' is not an interface");
+                                               Report.Error (527, Location,
+                                                             "In Class `{0}', `{1}' is not " +
+                                                             "an interface", Name, iface.Name);
                                                error = true;
                                                return null;
                                        }
                                }
 
-                               for (int x = 0; x < j; x++) {
-                                       if (resolved.Equals (ifaces [x])) {
-                                               Report.Error (528, "`" + name + "' is already listed in interface list");
+                               for (int x = 0; x < i; x++) {
+                                       if (iface.Equals (ifaces [x])) {
+                                               Report.Error (528, Location,
+                                                             "`{0}' is already listed in " +
+                                                             "interface list", iface.Name);
                                                error = true;
                                                return null;
                                        }
                                }
 
-                               if (is_iface &&
-                                   !resolved.AsAccessible (Parent, ModFlags))
+                               if ((Kind == Kind.Interface) &&
+                                   !iface.AsAccessible (Parent, ModFlags))
                                        Report.Error (61, Location,
-                                                     "Inconsistent accessibility: base interface `" +
-                                                     name + "' is less accessible than interface `" +
-                                                     Name + "'");
-
-                               ifaces [j] = resolved;
+                                                     "Inconsistent accessibility: base " +
+                                                     "interface `{0}' is less accessible " +
+                                                     "than interface `{1}'", iface.Name,
+                                                     Name);
                        }
 
                        return TypeManager.ExpandInterfaces (ifaces);
@@ -799,7 +1120,6 @@ namespace Mono.CSharp {
                public override TypeBuilder DefineType ()
                {
                        TypeExpr parent;
-                       bool is_class, is_iface;
 
                        if (TypeBuilder != null)
                                return TypeBuilder;
@@ -815,51 +1135,62 @@ namespace Mono.CSharp {
                        
                        InTransit = true;
 
-                       if (this is Interface) {
-                               is_iface = true;
-                               is_class = false;
-                       } else {
-                               is_iface = false;
-                               if (this is Class)
-                                       is_class = true;
-                               else
-                                       is_class = false;
-                       }
-
                        ec = new EmitContext (this, Mono.CSharp.Location.Null, null, null, ModFlags);
 
-                       ifaces = GetClassBases (is_class, is_iface, out parent, out error); 
-
+                       ifaces = GetClassBases (out parent, out error); 
                        if (error)
                                return null;
 
-                       if (!is_class && TypeManager.value_type == null)
+                       if (parent == null) {
+                               if (Kind == Kind.Class){
+                                       if (RootContext.StdLib)
+                                               parent = TypeManager.system_object_expr;
+                                       else if (Name != "System.Object")
+                                               parent = TypeManager.system_object_expr;
+                               } else if (Kind == Kind.Struct) {
+                                       //
+                                       // If we are compiling our runtime,
+                                       // and we are defining ValueType, then our
+                                       // parent is `System.Object'.
+                                       //
+                                       if (!RootContext.StdLib && Name == "System.ValueType")
+                                               parent = TypeManager.system_object_expr;
+                                       else
+                                               parent = TypeManager.system_valuetype_expr;
+                               }
+                       }
+
+                       if ((Kind == Kind.Struct) && TypeManager.value_type == null)
                                throw new Exception ();
 
                        TypeAttributes type_attributes = TypeAttr;
 
-                       Type ptype;
                        if (parent != null)
-                               ptype = parent.ResolveType (ec);
-                       else
-                               ptype = null;
+                               base_class_type = parent.ResolveType (ec);
 
-                       if (IsTopLevel){
-                               if (TypeManager.NamespaceClash (Name, Location)) {
-                                       error = true;
-                                       return null;
-                               }
+                       try {
+                               if (IsTopLevel){
+                                       if (TypeManager.NamespaceClash (Name, Location)) {
+                                               error = true;
+                                               return null;
+                                       }
                                
-                               ModuleBuilder builder = CodeGen.Module.Builder;
-                               TypeBuilder = builder.DefineType (
-                                       Name, type_attributes, ptype, null);
-                       } else {
-                               TypeBuilder builder = Parent.DefineType ();
-                               if (builder == null)
-                                       return null;
+                                       ModuleBuilder builder = CodeGen.Module.Builder;
+                                       TypeBuilder = builder.DefineType (
+                                               Name, type_attributes, base_class_type, null);
+                               
+                               } else {
+                                       TypeBuilder builder = Parent.DefineType ();
+                                       if (builder == null)
+                                               return null;
                                
-                               TypeBuilder = builder.DefineNestedType (
-                                       Basename, type_attributes, ptype, null);
+                                       TypeBuilder = builder.DefineNestedType (
+                                               Basename, type_attributes, base_class_type, null);
+                               }
+                       }
+                       catch (ArgumentException) {
+                               Report.RuntimeMissingSupport ("static classes");
+                               return null;
                        }
                                
                        //
@@ -869,7 +1200,7 @@ namespace Mono.CSharp {
                        // be specified.
                        //
 
-                       if (!is_class && !is_iface && !have_nonstatic_fields){
+                       if ((Kind == Kind.Struct) && !have_nonstatic_fields){
                                TypeBuilder.DefineField ("$PRIVATE$", TypeManager.byte_type,
                                                         FieldAttributes.Private);
                        }
@@ -893,169 +1224,52 @@ namespace Mono.CSharp {
 
                        if ((parent != null) && parent.IsAttribute) {
                                RootContext.RegisterAttribute (this);
-                               TypeManager.RegisterAttrType (TypeBuilder, this);
-                       } else
+                       } else if (!(this is Iterator))
                                RootContext.RegisterOrder (this); 
-                               
+
+                       if (!DefineNestedTypes ()) {
+                               error = true;
+                               return null;
+                       }
+
+                       InTransit = false;
+                       return TypeBuilder;
+               }
+
+               protected virtual bool DefineNestedTypes ()
+               {
                        if (Interfaces != null) {
-                               foreach (Interface iface in Interfaces)
-                                       if (iface.DefineType () == null) {
-                                               error = true;
-                                               return null;
-                                       }
+                               foreach (TypeContainer iface in Interfaces)
+                                       if (iface.DefineType () == null)
+                                               return false;
                        }
                        
                        if (Types != null) {
                                foreach (TypeContainer tc in Types)
-                                       if (tc.DefineType () == null) {
-                                               error = true;
-                                               return null;
-                                       }
+                                       if (tc.DefineType () == null)
+                                               return false;
                        }
 
                        if (Delegates != null) {
                                foreach (Delegate d in Delegates)
-                                       if (d.DefineType () == null) {
-                                               error = true;
-                                               return null;
-                                       }
+                                       if (d.DefineType () == null)
+                                               return false;
                        }
 
                        if (Enums != null) {
                                foreach (Enum en in Enums)
-                                       if (en.DefineType () == null) {
-                                               error = true;
-                                               return null;
-                                       }
-                       }
-
-                       InTransit = false;
-                       return TypeBuilder;
-               }
-
-
-               /// <summary>
-               ///   Defines the MemberCore objects that are in the `list' Arraylist
-               ///
-               ///   The `defined_names' array contains a list of members defined in
-               ///   a base class
-               /// </summary>
-               static ArrayList remove_list = new ArrayList ();
-               void DefineMembers (ArrayList list, MemberInfo [] defined_names)
-               {
-                       int idx;
-                       
-                       remove_list.Clear ();
-
-                       foreach (MemberCore mc in list){
-
-                               if (defined_names != null)
-                                       idx = Array.BinarySearch (defined_names, mc.Name, mif_compare);
-                               else
-                                       idx = -1;
-
-                               if (idx < 0){
-                                       if (RootContext.WarningLevel >= 4){
-                                               if ((mc.ModFlags & Modifiers.NEW) != 0)
-                                                       Warning_KeywordNewNotRequired (mc.Location, mc);
-                                       }
-                               } else if (mc is MethodCore)
-                                       ((MethodCore) mc).OverridesSomething = true;
-
-                               if (!mc.Define (this)){
-                                       remove_list.Add (mc);
-                                       continue;
-                               }
-                                               
-                               if (idx < 0)
-                                       continue;
-
-                               MemberInfo match = defined_names [idx];
-
-                               if (match is PropertyInfo && ((mc.ModFlags & Modifiers.OVERRIDE) != 0))
-                                       continue;
-
-                               //
-                               // If we are both methods, let the method resolution emit warnings
-                               //
-                               if (match is MethodBase && mc is MethodCore)
-                                       continue; 
-
-                               if ((mc.ModFlags & Modifiers.NEW) == 0) {
-                                       if (mc is Event) {
-                                               if (!(match is EventInfo)) {
-                                                       Error_EventCanOnlyOverrideEvent (mc.Location, defined_names [idx]);
-                                                       return;
-                                               }
-
-                                               if ((mc.ModFlags & Modifiers.OVERRIDE) != 0)
-                                                       continue;
-                                       }
-
-                                       Warning_KeywordNewRequired (mc.Location, defined_names [idx]);
-                               }
-                       }
-                       
-                       foreach (object o in remove_list)
-                               list.Remove (o);
-                       
-                       remove_list.Clear ();
-               }
-
-               //
-               // Defines the indexers, and also verifies that the IndexerNameAttribute in the
-               // class is consistent.  Either it is `Item' or it is the name defined by all the
-               // indexers with the `IndexerName' attribute.
-               //
-               // Turns out that the IndexerNameAttribute is applied to each indexer,
-               // but it is never emitted, instead a DefaultMember attribute is attached
-               // to the class.
-               //
-               void DefineIndexers ()
-               {
-                       string class_indexer_name = null;
-
-                       //
-                       // If there's both an explicit and an implicit interface implementation, the
-                       // explicit one actually implements the interface while the other one is just
-                       // a normal indexer.  See bug #37714.
-                       //
-
-                       // Invariant maintained by AddIndexer(): All explicit interface indexers precede normal indexers
-                       bool seen_normal_indexers = false;
-                       foreach (Indexer i in Indexers) {
-                               if (i.ExplicitInterfaceName == null)
-                                       seen_normal_indexers = true;
-                               else if (seen_normal_indexers)
-                                       throw new Exception ("Internal Error: 'Indexers' array not sorted properly.");
+                                       if (en.DefineType () == null)
+                                               return false;
                        }
 
-                       foreach (Indexer i in Indexers) {
-                               string name;
-
-                               i.Define (this);
-
-                               name = i.IndexerName;
-
-                               if (i.InterfaceType != null)
-                                       continue;
-
-                               if (class_indexer_name == null){
-                                       class_indexer_name = name;
-                                       continue;
+                       if (Parts != null) {
+                               foreach (ClassPart part in Parts) {
+                                       part.TypeBuilder = TypeBuilder;
+                                       part.base_class_type = base_class_type;
                                }
-                               
-                               if (name == class_indexer_name)
-                                       continue;
-                               
-                               Report.Error (
-                                       668, "Two indexers have different names, " +
-                                       " you should use the same name for all your indexers");
                        }
 
-                       if (seen_normal_indexers && class_indexer_name == null)
-                               class_indexer_name = "Item";
-                       IndexerName = class_indexer_name;
+                       return true;
                }
 
                static void Error_KeywordNotAllowed (Location loc)
@@ -1079,83 +1293,57 @@ namespace Mono.CSharp {
 
                bool DoDefineMembers ()
                {
-                       MemberInfo [] defined_names = null;
-
                        //
                        // We need to be able to use the member cache while we are checking/defining
                        //
-#if CACHE
                        if (TypeBuilder.BaseType != null)
                                parent_container = TypeManager.LookupMemberContainer (TypeBuilder.BaseType);
-#endif
 
-                       if (interface_order != null){
-                               foreach (Interface iface in interface_order)
-                                       if ((iface.ModFlags & Modifiers.NEW) == 0)
-                                               iface.DefineMembers (this);
-                                       else
-                                               Error_KeywordNotAllowed (iface.Location);
-                       }
-
-                       if (RootContext.WarningLevel > 1){
-                               Type ptype;
-
-                               //
-                               // This code throws an exception in the comparer
-                               // I guess the string is not an object?
-                               //
-                               ptype = TypeBuilder.BaseType;
-                               if (ptype != null){
-                                       defined_names = (MemberInfo []) FindMembers (
-                                               ptype, MemberTypes.All & ~MemberTypes.Constructor,
-                                               BindingFlags.Public | BindingFlags.Instance |
-                                               BindingFlags.Static, null, null);
-
-                                       Array.Sort (defined_names, mif_compare);
-                               }
-                       }
-
-                       Class pclass = Parent as Class;
-                       if (pclass != null) {
-                               string pname = null;
-                               Type ptype = null;
-                               Type t = pclass.TypeBuilder.BaseType;
-                               while ((t != null) && (ptype == null)) {
-                                       pname = t.FullName + "." + Basename;
-                                       ptype = RootContext.LookupType (this, pname, true, Location.Null);
-                                       t = t.BaseType;
-                               }
-
-                               if ((ModFlags & Modifiers.NEW) != 0) {
-                                       if (ptype == null)
-                                               Report.Warning (109, Location, "The member '" + Name + "' does not hide an " +
-                                                               "inherited member. The keyword new is not required.");
-                               } else if (ptype != null) {
-                                       Report.Warning (108, Location, "The keyword new is required on `" +
-                                                       Name + "' because it hides inherited member '" +
-                                                       pname + "'.");
-                               }
-                       } else if ((ModFlags & Modifiers.NEW) != 0)
-                               Error_KeywordNotAllowed (Location);
-
-                       if (constants != null)
-                               DefineMembers (constants, defined_names);
-
-                       if (fields != null)
-                               DefineMembers (fields, defined_names);
-
-                       if (this is Class){
-                               if (instance_constructors == null){
-                                       if (default_constructor == null)
-                                               DefineDefaultConstructor (false);
-                               }
+                       // TODO:
+                       //if (TypeBuilder.IsInterface) {
+                       //      parent_container = TypeManager.LookupInterfaceContainer (base_inteface_types);
+                       //}
+
+                       if (IsTopLevel) {
+                               if ((ModFlags & Modifiers.NEW) != 0)
+                                       Error_KeywordNotAllowed (Location);
+                       } else {
+                               // HACK: missing implemenation
+                               // This is not fully functional. Better way how to handle this is to have recursive definition of containers
+                               // instead of flat as we have now.
+                               // Now we are not able to check inner attribute class because its parent had not been defined.
+                               // TODO: remove this if
+                               if (Parent.MemberCache != null) {
+                                       MemberInfo conflict_symbol = Parent.MemberCache.FindMemberWithSameName (Basename, false, TypeBuilder);
+                                       if (conflict_symbol == null) {
+                                               if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0))
+                                                       Report.Warning (109, Location, "The member '{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
+                                       } else {
+                                               if ((ModFlags & Modifiers.NEW) == 0) {
+                                                       Report.SymbolRelatedToPreviousError (conflict_symbol);
+                                                       Report.Warning (108, Location, "The keyword new is required on '{0}' because it hides inherited member", GetSignatureForError ());
+                                               }
+                                       }
+                               }
+                       }
+
+                       DefineContainerMembers (constants);
+                       DefineContainerMembers (fields);
+
+                       if ((Kind == Kind.Class) && !(this is ClassPart) && !(this is StaticClass)){
+                               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 (this is Struct){
+                       if (Kind == Kind.Struct){
                                //
                                // Structs can not have initialized instance
                                // fields
@@ -1168,61 +1356,91 @@ namespace Mono.CSharp {
                                        ReportStructInitializedInstanceError ();
                        }
 
-                       if (!(this is Interface))
-                               Pending = PendingImplementation.GetPendingImplementations (this);
-                       
+                       Pending = GetPendingImplementations ();
+
+                       if (parts != null) {
+                               foreach (ClassPart part in parts) {
+                                       if (!part.DefineMembers (this))
+                                               return false;
+                               }
+                       }
+               
                        //
                        // Constructors are not in the defined_names array
                        //
-                       if (instance_constructors != null)
-                               DefineMembers (instance_constructors, null);
+                       DefineContainerMembers (instance_constructors);
                
                        if (default_static_constructor != null)
-                               default_static_constructor.Define (this);
+                               default_static_constructor.Define ();
                        
-                       if (methods != null)
-                               DefineMembers (methods, defined_names);
+                       DefineContainerMembers (properties);
+                       DefineContainerMembers (events);
+                       DefineContainerMembers (indexers);
+                       DefineContainerMembers (methods);
+                       DefineContainerMembers (operators);
+                       DefineContainerMembers (enums);
+                       DefineContainerMembers (delegates);                     
 
-                       if (properties != null)
-                               DefineMembers (properties, defined_names);
-
-                       if (events != null)
-                               DefineMembers (events, defined_names);
+#if CACHE
+                       if (!(this is ClassPart))
+                               member_cache = new MemberCache (this);
+#endif
 
-                       if (indexers != null)
-                               DefineIndexers ();
+                       if (parts != null) {
+                               foreach (ClassPart part in parts)
+                                       part.member_cache = member_cache;
+                       }
 
-                       if (operators != null){
-                               DefineMembers (operators, null);
+                       if (iterators != null) {
+                               foreach (Iterator iterator in iterators) {
+                                       if (iterator.DefineType () == null)
+                                               return false;
+                               }
 
-                               CheckPairedOperators ();
+                               foreach (Iterator iterator in iterators) {
+                                       if (!iterator.DefineMembers (this))
+                                               return false;
+                               }
                        }
 
-                       if (enums != null)
-                               DefineMembers (enums, defined_names);
-                       
-                       if (delegates != null)
-                               DefineMembers (delegates, defined_names);
-
-#if CACHE
-                       member_cache = new MemberCache (this);
-#endif
+                       return true;
+               }
 
+               void ReportStructInitializedInstanceError ()
+               {
+                       string n = TypeBuilder.FullName;
                        
-                       return true;
+                       foreach (Field f in initialized_fields){
+                               Report.Error (
+                                       573, Location,
+                                       "`" + n + "." + f.Name + "': can not have " +
+                                       "instance field initializers in structs");
+                       }
                }
 
-               public override bool Define (TypeContainer container)
+               protected virtual void DefineContainerMembers (MemberCoreArrayList mcal)
                {
-                       if (interface_order != null){
-                               foreach (Interface iface in interface_order)
-                                       if ((iface.ModFlags & Modifiers.NEW) == 0)
-                                               iface.Define (this);
+                       if (mcal != null)
+                               mcal.DefineContainerMembers ();
+               }
+
+               public override bool Define ()
+               {
+                       if (parts != null) {
+                               foreach (ClassPart part in parts) {
+                                       if (!part.Define ())
+                                               return false;
+                               }
                        }
 
                        return true;
                }
 
+               public MemberInfo FindMemberWithSameName (string name, bool ignore_methods)
+               {
+                       return ParentContainer.MemberCache.FindMemberWithSameName (name, ignore_methods, null);
+               }
+
                /// <summary>
                ///   This function is based by a delegate to the FindMembers routine
                /// </summary>
@@ -1238,15 +1456,9 @@ namespace Mono.CSharp {
                static MemberFilter accepting_filter;
 
                
-               /// <summary>
-               ///   A member comparission method based on name only
-               /// </summary>
-               static IComparer mif_compare;
-
                static TypeContainer ()
                {
                        accepting_filter = new MemberFilter (AlwaysAccept);
-                       mif_compare = new MemberInfoCompare ();
                }
 
                public MethodInfo[] GetMethods ()
@@ -1633,7 +1845,7 @@ namespace Mono.CSharp {
                                if (interfaces != null) {
                                        int len = interfaces.Count;
                                        for (int i = 0; i < len; i++) {
-                                               Interface iface = (Interface) interfaces [i];
+                                               TypeContainer iface = (TypeContainer) interfaces [i];
                                                
                                                if ((iface.ModFlags & modflags) == 0)
                                                        continue;
@@ -1747,11 +1959,11 @@ namespace Mono.CSharp {
                {
                        if (constants != null)
                                foreach (Const con in constants)
-                                       con.Emit (this);
+                                       con.Emit ();
                        return;
                }
 
-               protected virtual void VerifyMembers ()
+               protected virtual void VerifyMembers (EmitContext ec)
                {
                        //
                        // Check for internal or private fields that were never assigned
@@ -1763,9 +1975,7 @@ namespace Mono.CSharp {
                                                        continue;
                                                
                                                if ((f.status & Field.Status.USED) == 0){
-                                                       Report.Warning (
-                                                               169, f.Location, "Private field " +
-                                                               MakeName (f.Name) + " is never used");
+                                                       Report.Warning (169, f.Location, "The private field '{0}' is never used", f.GetSignatureForError ());
                                                        continue;
                                                }
                                                
@@ -1778,17 +1988,14 @@ namespace Mono.CSharp {
                                                if ((f.status & Field.Status.ASSIGNED) != 0)
                                                        continue;
                                                
-                                               Report.Warning (
-                                                       649, f.Location,
-                                                       "Field " + MakeName (f.Name) + " is never assigned " +
-                                                       " to and will always have its default value");
+                                               Report.Warning (649, f.Location, "Field '{0}' is never assigned to, and will always have its default value '{1}'", f.GetSignatureForError (), "");
                                        }
                                }
 
-                               if (events != null){
+                               if ((events != null) && (RootContext.WarningLevel >= 3)) {
                                        foreach (Event e in events){
                                                if (e.status == 0)
-                                                       Report.Warning (67, "The event " + MakeName (e.Name) + " is never used");
+                                                       Report.Warning (67, e.Location, "The event '{0}' is never used", e.GetSignatureForError ());
                                        }
                                }
                        }
@@ -1798,18 +2005,19 @@ namespace Mono.CSharp {
                ///   Emits the code, this step is performed after all
                ///   the types, enumerations, constructors
                /// </summary>
-               public void Emit ()
+               public void EmitType ()
                {
-                       Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes);
-
-                       Emit (this);
+                       if (OptAttributes != null)
+                               OptAttributes.Emit (ec, this);
+                               
+                       Emit ();
 
                        if (instance_constructors != null) {
-                               if (TypeBuilder.IsSubclassOf (TypeManager.attribute_type) && IsClsCompliaceRequired (this)) {
+                               if (TypeBuilder.IsSubclassOf (TypeManager.attribute_type) && RootContext.VerifyClsCompliance && IsClsCompliaceRequired (this)) {
                                        bool has_compliant_args = false;
 
                                        foreach (Constructor c in instance_constructors) {
-                                               c.Emit (this);
+                                               c.Emit ();
 
                                                if (has_compliant_args)
                                                        continue;
@@ -1817,93 +2025,78 @@ namespace Mono.CSharp {
                                                has_compliant_args = c.HasCompliantArgs;
                                        }
                                        if (!has_compliant_args)
-                                               Report.Error_T (3015, Location, GetSignatureForError ());
+                                               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 (this);
+                                               c.Emit ();
                                }
                        }
 
                        if (default_static_constructor != null)
-                               default_static_constructor.Emit (this);
+                               default_static_constructor.Emit ();
                        
                        if (methods != null)
                                foreach (Method m in methods)
-                                       m.Emit (this);
+                                       m.Emit ();
 
                        if (operators != null)
                                foreach (Operator o in operators)
-                                       o.Emit (this);
+                                       o.Emit ();
 
                        if (properties != null)
                                foreach (Property p in properties)
-                                       p.Emit (this);
+                                       p.Emit ();
 
                        if (indexers != null){
-                               foreach (Indexer ix in indexers)
-                                       ix.Emit (this);
-                               if (IndexerName != null) {
-                                       CustomAttributeBuilder cb = EmitDefaultMemberAttr ();
-                                       TypeBuilder.SetCustomAttribute (cb);
-                               }
+                               indexers.Emit ();
                        }
                        
                        if (fields != null)
                                foreach (Field f in fields)
-                                       f.Emit (this);
+                                       f.Emit ();
 
                        if (events != null){
                                foreach (Event e in Events)
-                                       e.Emit (this);
+                                       e.Emit ();
                        }
 
                        if (delegates != null) {
                                foreach (Delegate d in Delegates) {
-                                       d.Emit (this);
+                                       d.Emit ();
                                }
                        }
 
-                       if (Pending != null)
+                       if (enums != null) {
+                               foreach (Enum e in enums) {
+                                       e.Emit ();
+                               }
+                       }
+
+                       if (parts != null) {
+                               foreach (ClassPart part in parts)
+                                       part.EmitType ();
+                       }
+
+                       if ((Pending != null) && !(this is ClassPart))
                                if (Pending.VerifyPendingMethods ())
                                        return;
 
-                       VerifyMembers ();
+                       VerifyMembers (ec);
+
+                       if (iterators != null)
+                               foreach (Iterator iterator in iterators)
+                                       iterator.EmitType ();
                        
 //                     if (types != null)
 //                             foreach (TypeContainer tc in types)
 //                                     tc.Emit ();
                }
 
-               CustomAttributeBuilder EmitDefaultMemberAttr ()
-               {
-                       EmitContext ec = new EmitContext (this, Location, null, null, ModFlags);
-
-                       Expression ml = Expression.MemberLookup (ec, TypeManager.default_member_type,
-                                                                ".ctor", MemberTypes.Constructor,
-                                                                BindingFlags.Public | BindingFlags.Instance,
-                                                                Location.Null);
-                       
-                       MethodGroupExpr mg = (MethodGroupExpr) ml;
-
-                       MethodBase constructor = mg.Methods [0];
-
-                       string [] vals = { IndexerName };
-
-                       CustomAttributeBuilder cb = null;
-                       try {
-                               cb = new CustomAttributeBuilder ((ConstructorInfo) constructor, vals);
-                       } catch {
-                               Report.Warning (-100, "Can not set the indexer default member attribute");
-                       }
-
-                       return cb;
-               }
-
                public override void CloseType ()
                {
                        if ((caching_flags & Flags.CloseTypeCreated) != 0)
                                return;
-                       
+
                        try {
                                caching_flags |= Flags.CloseTypeCreated;
                                TypeBuilder.CreateType ();
@@ -1922,25 +2115,24 @@ namespace Mono.CSharp {
                                foreach (Enum en in Enums)
                                        en.CloseType ();
 
-                       if (interface_order != null){
-                               foreach (Interface iface in interface_order)
-                                       iface.CloseType ();
-                       }
-                       
                        if (Types != null){
                                foreach (TypeContainer tc in Types)
-                                       if (tc is Struct)
+                                       if (tc.Kind == Kind.Struct)
                                                tc.CloseType ();
 
                                foreach (TypeContainer tc in Types)
-                                       if (!(tc is Struct))
+                                       if (tc.Kind != Kind.Struct)
                                                tc.CloseType ();
                        }
 
                        if (Delegates != null)
                                foreach (Delegate d in Delegates)
                                        d.CloseType ();
-                       
+
+                       if (Iterators != null)
+                               foreach (Iterator i in Iterators)
+                                       i.CloseType ();
+
                        types = null;
                        properties = null;
                        enums = null;
@@ -1950,11 +2142,11 @@ namespace Mono.CSharp {
                        initialized_static_fields = null;
                        constants = null;
                        interfaces = null;
-                       interface_order = null;
                        methods = null;
                        events = null;
                        indexers = null;
                        operators = null;
+                       iterators = null;
                        ec = null;
                        default_constructor = null;
                        default_static_constructor = null;
@@ -1965,38 +2157,12 @@ namespace Mono.CSharp {
                        member_cache = null;
                }
 
+               // TODO: make it obsolete and use GetSignatureForError
                public string MakeName (string n)
                {
                        return "`" + Name + "." + n + "'";
                }
 
-               public void Warning_KeywordNewRequired (Location l, MemberInfo mi)
-               {
-                       Report.Warning (
-                               108, l, "The keyword new is required on " + 
-                               MakeName (mi.Name) + " because it hides `" +
-                               mi.ReflectedType.Name + "." + mi.Name + "'");
-               }
-
-               public void Warning_KeywordNewNotRequired (Location l, MemberCore mc)
-               {
-                       Report.Warning (
-                               109, l, "The member " + MakeName (mc.Name) + " does not hide an " +
-                               "inherited member, the keyword new is not required");
-               }
-
-               public void Error_EventCanOnlyOverrideEvent (Location l, MemberInfo mi)
-               {
-                       Report.Error (
-                               72, l, MakeName (mi.Name) + " : cannot override; `" +
-                               mi.ReflectedType.Name + "." + mi.Name + "' is not an event");
-               }
-               
-               public static int CheckMember (string name, MemberInfo mi, int ModFlags)
-               {
-                       return 0;
-               }
-
                //
                // Performs the validation on a Method's modifiers (properties have
                // the same properties).
@@ -2020,7 +2186,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       if (this is Struct){
+                       if (Kind == Kind.Struct){
                                if ((flags & va) != 0){
                                        Modifiers.Error_InvalidModifier (loc, "virtual or abstract");
                                        ok = false;
@@ -2081,13 +2247,10 @@ namespace Mono.CSharp {
                        return ok;
                }
 
-               Hashtable builder_and_args;
-               
-               public bool RegisterMethod (MethodBuilder mb, InternalParameters ip, Type [] args)
-               {
-                       if (builder_and_args == null)
-                               builder_and_args = new Hashtable ();
-                       return true;
+               public bool UserDefinedStaticConstructor {
+                       get {
+                               return default_static_constructor != null;
+                       }
                }
 
                protected override bool VerifyClsCompliance (DeclSpace ds)
@@ -2095,20 +2258,66 @@ namespace Mono.CSharp {
                        if (!base.VerifyClsCompliance (ds))
                                return false;
 
+                       VerifyClsName ();
+
                        // parent_container is null for System.Object
                        if (parent_container != null && !AttributeTester.IsClsCompliant (parent_container.Type)) {
-                               Report.Error_T (3009, Location, GetSignatureForError (),  TypeManager.CSharpName (parent_container.Type));
+                               Report.Error (3009, Location, "'{0}': base type '{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (parent_container.Type));
                        }
                        return true;
                }
 
 
+               /// <summary>
+               /// Checks whether container name is CLS Compliant
+               /// </summary>
+               void VerifyClsName ()
+               {
+                       Hashtable parent_members = parent_container == null ? 
+                               new Hashtable () :
+                               parent_container.MemberCache.GetPublicMembers ();
+                       Hashtable this_members = new Hashtable ();
+
+                       foreach (DictionaryEntry entry in defined_names) {
+                               MemberCore mc = (MemberCore)entry.Value;
+                               if (!mc.IsClsCompliaceRequired (this))
+                                       continue;
+
+                               string name = (string)entry.Key;
+                               string basename = name.Substring (name.LastIndexOf ('.') + 1);
+
+                               string lcase = basename.ToLower (System.Globalization.CultureInfo.InvariantCulture);
+                               object found = parent_members [lcase];
+                               if (found == null) {
+                                       found = this_members [lcase];
+                                       if (found == null) {
+                                               this_members.Add (lcase, mc);
+                                               continue;
+                                       }
+                               }
+
+                               if ((mc.ModFlags & Modifiers.OVERRIDE) != 0)
+                                       continue;                                       
+
+                               if (found is MemberInfo) {
+                                       if (basename == ((MemberInfo)found).Name)
+                                               continue;
+                                       Report.SymbolRelatedToPreviousError ((MemberInfo)found);
+                               } else {
+                                       Report.SymbolRelatedToPreviousError ((MemberCore) found);
+                               }
+                               Report.Error (3005, mc.Location, "Identifier '{0}' differing only in case is not CLS-compliant", mc.GetSignatureForError ());
+                       }
+               }
+
+
                /// <summary>
                ///   Performs checks for an explicit interface implementation.  First it
                ///   checks whether the `interface_type' is a base inteface implementation.
                ///   Then it checks whether `name' exists in the interface type.
                /// </summary>
-               public bool VerifyImplements (Type interface_type, string full, string name, Location loc)
+               public virtual bool VerifyImplements (Type interface_type, string full,
+                                                     string name, Location loc)
                {
                        bool found = false;
 
@@ -2129,6 +2338,19 @@ namespace Mono.CSharp {
                        return true;
                }
 
+               protected override void VerifyObsoleteAttribute()
+               {
+                       CheckUsageOfObsoleteAttribute (base_class_type);
+
+                       if (ifaces == null)
+                               return;
+
+                       foreach (TypeExpr expr in ifaces) {
+                               CheckUsageOfObsoleteAttribute (expr.Type);
+                       }
+               }
+
+
                //
                // IMemberContainer
                //
@@ -2145,12 +2367,6 @@ namespace Mono.CSharp {
                        }
                }
 
-               IMemberContainer IMemberContainer.Parent {
-                       get {
-                               return parent_container;
-                       }
-               }
-
                MemberCache IMemberContainer.MemberCache {
                        get {
                                return member_cache;
@@ -2159,7 +2375,7 @@ namespace Mono.CSharp {
 
                bool IMemberContainer.IsInterface {
                        get {
-                               return this is Interface;
+                               return Kind == Kind.Interface;
                        }
                }
 
@@ -2168,189 +2384,208 @@ namespace Mono.CSharp {
                        return FindMembers (mt, bf | BindingFlags.DeclaredOnly, null, null);
                }
 
-               //
-               // Operator pair checking
-               //
+               public virtual IMemberContainer ParentContainer {
+                       get {
+                               return parent_container;
+                       }
+               }
+       }
 
-               class OperatorEntry {
-                       public int flags;
-                       public Type ret_type;
-                       public Type type1, type2;
-                       public Operator op;
-                       public Operator.OpType ot;
-                       
-                       public OperatorEntry (int f, Operator o)
-                       {
-                               flags = f;
+       public class PartialContainer : TypeContainer {
+
+               public readonly Namespace Namespace;
+               public readonly int OriginalModFlags;
+               public readonly int AllowedModifiers;
+               public readonly TypeAttributes DefaultTypeAttributes;
+
+               static PartialContainer Create (NamespaceEntry ns, TypeContainer parent,
+                                               string name, int mod_flags, Kind kind,
+                                               Location loc)
+               {
+                       PartialContainer pc;
+                       DeclSpace ds = (DeclSpace) RootContext.Tree.Decls [name];
+                       if (ds != null) {
+                               pc = ds as PartialContainer;
+
+                               if (pc == null) {
+                                       Report.Error (
+                                               260, ds.Location, "Missing partial modifier " +
+                                               "on declaration of type `{0}'; another " +
+                                               "partial implementation of this type exists",
+                                               name);
+
+                                       Report.LocationOfPreviousError (loc);
+                                       return null;
+                               }
+
+                               if (pc.Kind != kind) {
+                                       Report.Error (
+                                               261, loc, "Partial declarations of `{0}' " +
+                                               "must be all classes, all structs or " +
+                                               "all interfaces", name);
+                                       return null;
+                               }
 
-                               ret_type = o.OperatorMethod.GetReturnType ();
-                               Type [] pt = o.OperatorMethod.ParameterTypes;
-                               type1 = pt [0];
-                               type2 = pt [1];
-                               op = o;
-                               ot = o.OperatorType;
+                               if (pc.OriginalModFlags != mod_flags) {
+                                       Report.Error (
+                                               262, loc, "Partial declarations of `{0}' " +
+                                               "have conflicting accessibility modifiers",
+                                               name);
+                                       return null;
+                               }
+
+                               return pc;
                        }
 
-                       public override int GetHashCode ()
-                       {       
-                               return ret_type.GetHashCode ();
+                       pc = new PartialContainer (ns, parent, name, mod_flags, kind, loc);
+                       RootContext.Tree.RecordDecl (name, pc);
+                       parent.AddType (pc);
+                       pc.Register ();
+                       return pc;
+               }
+
+               public static ClassPart CreatePart (NamespaceEntry ns, TypeContainer parent,
+                                                   string name, int mod, Attributes attrs,
+                                                   Kind kind, Location loc)
+               {
+                       PartialContainer pc = Create (ns, parent, name, mod, kind, loc);
+                       if (pc == null) {
+                               // An error occured; create a dummy container, but don't
+                               // register it.
+                               pc = new PartialContainer (ns, parent, name, mod, kind, loc);
                        }
 
-                       public override bool Equals (object o)
-                       {
-                               OperatorEntry other = (OperatorEntry) o;
+                       ClassPart part = new ClassPart (ns, pc, mod, attrs, kind, loc);
+                       pc.AddPart (part);
+                       return part;
+               }
 
-                               if (other.ret_type != ret_type)
-                                       return false;
-                               if (other.type1 != type1)
-                                       return false;
-                               if (other.type2 != type2)
-                                       return false;
-                               return true;
+               protected PartialContainer (NamespaceEntry ns, TypeContainer parent,
+                                           string name, int mod, Kind kind, Location l)
+                       : base (ns, parent, name, null, kind, l)
+               {
+                       this.Namespace = ns.NS;
+
+                       switch (kind) {
+                       case Kind.Class:
+                               AllowedModifiers = Class.AllowedModifiers;
+                               DefaultTypeAttributes = Class.DefaultTypeAttributes;
+                               break;
+
+                       case Kind.Struct:
+                               AllowedModifiers = Struct.AllowedModifiers;
+                               DefaultTypeAttributes = Struct.DefaultTypeAttributes;
+                               break;
+
+                       case Kind.Interface:
+                               AllowedModifiers = Interface.AllowedModifiers;
+                               DefaultTypeAttributes = Interface.DefaultTypeAttributes;
+                               break;
+
+                       default:
+                               throw new InvalidOperationException ();
                        }
+
+                       int accmods;
+                       if (parent.Parent == null)
+                               accmods = Modifiers.INTERNAL;
+                       else
+                               accmods = Modifiers.PRIVATE;
+
+                       this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, l);
+                       this.OriginalModFlags = mod;
                }
-                               
-               //
-               // Checks that some operators come in pairs:
-               //  == and !=
-               // > and <
-               // >= and <=
-               // true and false
-               //
-               // They are matched based on the return type and the argument types
-               //
-               void CheckPairedOperators ()
+
+               public override void Register ()
                {
-                       Hashtable pairs = new Hashtable (null, null);
-                       Operator true_op = null;
-                       Operator false_op = null;
-                       bool has_equality_or_inequality = false;
-                       
-                       // Register all the operators we care about.
-                       foreach (Operator op in operators){
-                               int reg = 0;
-                               
-                               switch (op.OperatorType){
-                               case Operator.OpType.Equality:
-                                       reg = 1;
-                                       has_equality_or_inequality = true;
-                                       break;
-                               case Operator.OpType.Inequality:
-                                       reg = 2;
-                                       has_equality_or_inequality = true;
-                                       break;
-
-                               case Operator.OpType.True:
-                                       true_op = op;
-                                       break;
-                               case Operator.OpType.False:
-                                       false_op = op;
-                                       break;
-                                       
-                               case Operator.OpType.GreaterThan:
-                                       reg = 1; break;
-                               case Operator.OpType.LessThan:
-                                       reg = 2; break;
-                                       
-                               case Operator.OpType.GreaterThanOrEqual:
-                                       reg = 1; break;
-                               case Operator.OpType.LessThanOrEqual:
-                                       reg = 2; break;
-                               }
-                               if (reg == 0)
-                                       continue;
+                       if (Kind == Kind.Interface)
+                               Parent.AddInterface (this);
+                       else if (Kind == Kind.Class || Kind == Kind.Struct)
+                               Parent.AddClassOrStruct (this);
+                       else
+                               throw new InvalidOperationException ();
+               }
 
-                               OperatorEntry oe = new OperatorEntry (reg, op);
+               public override PendingImplementation GetPendingImplementations ()
+               {
+                       return PendingImplementation.GetPendingImplementations (this);
+               }
 
-                               object o = pairs [oe];
-                               if (o == null)
-                                       pairs [oe] = oe;
-                               else {
-                                       oe = (OperatorEntry) o;
-                                       oe.flags |= reg;
-                               }
+               public ClassPart AddPart (NamespaceEntry ns, int mod, Attributes attrs,
+                                         Location l)
+               {
+                       ClassPart part = new ClassPart (ns, this, mod, attrs, Kind, l);
+                       AddPart (part);
+                       return part;
+               }
+
+               public override TypeAttributes TypeAttr {
+                       get {
+                               return base.TypeAttr | DefaultTypeAttributes;
                        }
+               }
+       }
 
-                       if (true_op != null){
-                               if (false_op == null)
-                                       Report.Error (216, true_op.Location, "operator true requires a matching operator false");
-                       } else if (false_op != null)
-                               Report.Error (216, false_op.Location, "operator false requires a matching operator true");
-                       
-                       //
-                       // Look for the mistakes.
-                       //
-                       foreach (DictionaryEntry de in pairs){
-                               OperatorEntry oe = (OperatorEntry) de.Key;
+       public class ClassPart : TypeContainer, IMemberContainer {
+               public readonly PartialContainer PartialContainer;
+               public readonly bool IsPartial;
 
-                               if (oe.flags == 3)
-                                       continue;
+               public ClassPart (NamespaceEntry ns, PartialContainer parent,
+                                 int mod, Attributes attrs, Kind kind, Location l)
+                       : base (ns, parent.Parent, parent.Name, attrs, kind, l)
+               {
+                       this.PartialContainer = parent;
+                       this.IsPartial = true;
+
+                       int accmods;
+                       if (parent.Parent == null)
+                               accmods = Modifiers.INTERNAL;
+                       else
+                               accmods = Modifiers.PRIVATE;
+
+                       this.ModFlags = Modifiers.Check (
+                               parent.AllowedModifiers, mod, accmods, l);
+               }
 
-                               string s = "";
-                               switch (oe.ot){
-                               case Operator.OpType.Equality:
-                                       s = "!=";
-                                       break;
-                               case Operator.OpType.Inequality: 
-                                       s = "==";
-                                       break;
-                               case Operator.OpType.GreaterThan: 
-                                       s = "<";
-                                       break;
-                               case Operator.OpType.LessThan:
-                                       s = ">";
-                                       break;
-                               case Operator.OpType.GreaterThanOrEqual:
-                                       s = "<=";
-                                       break;
-                               case Operator.OpType.LessThanOrEqual:
-                                       s = ">=";
-                                       break;
-                               }
-                               Report.Error (216, oe.op.Location,
-                                             "The operator `" + oe.op + "' requires a matching operator `" + s + "' to also be defined");
-                       }
-
-                       if ((has_equality_or_inequality) && (RootContext.WarningLevel >= 2)) {
-                               MethodSignature equals_ms = new MethodSignature (
-                                       "Equals", TypeManager.bool_type, new Type [] { TypeManager.object_type });
-                               MethodSignature hash_ms = new MethodSignature (
-                                       "GetHashCode", TypeManager.int32_type, new Type [0]);
-
-                               MemberList equals_ml = FindMembers (MemberTypes.Method, BindingFlags.Public | BindingFlags.Instance |
-                                                                   BindingFlags.DeclaredOnly, MethodSignature.method_signature_filter,
-                                                                   equals_ms);
-                               MemberList hash_ml = FindMembers (MemberTypes.Method, BindingFlags.Public | BindingFlags.Instance |
-                                                                 BindingFlags.DeclaredOnly, MethodSignature.method_signature_filter,
-                                                                 hash_ms);
-
-                               bool equals_ok = false;
-                               if ((equals_ml != null) && (equals_ml.Count == 1))
-                                       equals_ok = equals_ml [0].DeclaringType == TypeBuilder;
-                               bool hash_ok = false;
-                               if ((hash_ml != null) && (hash_ml.Count == 1))
-                                       hash_ok = hash_ml [0].DeclaringType == TypeBuilder;
-
-                               if (!equals_ok)
-                                       Report.Warning (660, Location, "`" + Name + "' defines operator == or operator != but does " +
-                                                       "not override Object.Equals (object o)");
-                               if (!hash_ok)
-                                       Report.Warning (661, Location, "`" + Name + "' defines operator == or operator != but does " +
-                                                       "not override Object.GetHashCode ()");
+               public override void Register ()
+               {
+               }
+
+               public override PendingImplementation GetPendingImplementations ()
+               {
+                       return PartialContainer.Pending;
+               }
+
+               public override bool VerifyImplements (Type interface_type, string full,
+                                                      string name, Location loc)
+               {
+                       return PartialContainer.VerifyImplements (
+                               interface_type, full, name, loc);
+               }
+
+               public override IMemberContainer ParentContainer {
+                       get {
+                               return PartialContainer.ParentContainer;
                        }
                }
-               
-               
        }
 
-       public class ClassOrStruct : TypeContainer {
+       public abstract class ClassOrStruct : TypeContainer {
                bool hasExplicitLayout = false;
-               public ClassOrStruct (NamespaceEntry ns, TypeContainer parent, string name, Attributes attrs, Location l)
-                       : base (ns, parent, name, attrs, l)
+
+               public ClassOrStruct (NamespaceEntry ns, TypeContainer parent,
+                                     string name, Attributes attrs, Kind kind,
+                                     Location l)
+                       : base (ns, parent, name, attrs, kind, l)
                {
                }
 
-               protected override void VerifyMembers () 
+               public override PendingImplementation GetPendingImplementations ()
+               {
+                       return PendingImplementation.GetPendingImplementations (this);
+               }
+
+               protected override void VerifyMembers (EmitContext ec) 
                {
                        if (Fields != null) {
                                foreach (Field f in Fields) {
@@ -2358,7 +2593,7 @@ namespace Mono.CSharp {
                                                continue;
                                        if (hasExplicitLayout) {
                                                if (f.OptAttributes == null 
-                                                   || !f.OptAttributes.Contains (TypeManager.field_offset_attribute_type, this)) {
+                                                   || !f.OptAttributes.Contains (TypeManager.field_offset_attribute_type, ec)) {
                                                        Report.Error (625, f.Location,
                                                                      "Instance field of type marked with" 
                                                                      + " StructLayout(LayoutKind.Explicit) must have a"
@@ -2367,7 +2602,7 @@ namespace Mono.CSharp {
                                        }
                                        else {
                                                if (f.OptAttributes != null 
-                                                   && f.OptAttributes.Contains (TypeManager.field_offset_attribute_type, this)) {
+                                                   && f.OptAttributes.Contains (TypeManager.field_offset_attribute_type, ec)) {
                                                        Report.Error (636, f.Location,
                                                                      "The FieldOffset attribute can only be placed on members of "
                                                                      + "types marked with the StructLayout(LayoutKind.Explicit)");
@@ -2375,16 +2610,90 @@ namespace Mono.CSharp {
                                        }
                                }
                        }
-                       base.VerifyMembers ();
+                       base.VerifyMembers (ec);
                }
 
-               public override void ApplyAttributeBuilder (object builder, Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
                {
                        if (a.Type == TypeManager.struct_layout_attribute_type
                            && (LayoutKind) a.GetPositionalValue (0) == LayoutKind.Explicit)
                                hasExplicitLayout = true;
 
-                       base.ApplyAttributeBuilder (builder, a, cb);
+                       base.ApplyAttributeBuilder (a, cb);
+               }
+
+               public override void Register ()
+               {
+                       Parent.AddClassOrStruct (this);
+               }
+       }
+
+       /// <summary>
+       /// Class handles static classes declaration
+       /// </summary>
+       public sealed class StaticClass: Class {
+               public StaticClass (NamespaceEntry ns, TypeContainer parent, string name, int mod,
+                       Attributes attrs, Location l)
+                       : base (ns, parent, name, mod & ~Modifiers.STATIC, attrs, l)
+               {
+                       if (RootContext.Version == LanguageVersion.ISO_1) {
+                               Report.FeatureIsNotStandardized (l, "static classes");
+                               Environment.Exit (1);
+                       }
+               }
+
+               protected override void DefineContainerMembers (MemberCoreArrayList list)
+               {
+                       if (list == null)
+                               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 (this));
+                                       continue;
+                               }
+
+                               if ((m.ModFlags & Modifiers.STATIC) != 0)
+                                       continue;
+
+                               if (m is Constructor) {
+                                       Report.Error (710, m.Location, "'{0}': Static classes cannot have instance constructors", GetSignatureForError ());
+                                       continue;
+                               }
+
+                               if (m is Destructor) {
+                                       Report.Error (711, m.Location, "'{0}': Static class cannot contain destructor", GetSignatureForError ());
+                                       continue;
+                               }
+                               Report.Error (708, m.Location, "'{0}': cannot declare instance members in a static class", m.GetSignatureForError (this));
+                       }
+
+                       base.DefineContainerMembers (list);
+               }
+
+               public override TypeBuilder DefineType()
+               {
+                       TypeBuilder tb = base.DefineType ();
+                       if (tb == null)
+                               return null;
+
+                       if (base_class_type != TypeManager.object_type) {
+                               Report.Error (713, Location, "Static class '{0}' cannot derive from type '{1}'. Static classes must derive from object", GetSignatureForError (), TypeManager.CSharpName (base_class_type));
+                               return null;
+                       }
+
+                       if (base_inteface_types != null) {
+                               foreach (Type t in base_inteface_types)
+                                       Report.SymbolRelatedToPreviousError (t);
+                               Report.Error (714, Location, "'{0}': static classes cannot implement interfaces", GetSignatureForError ());
+                       }
+                       return tb;
+               }
+
+               public override TypeAttributes TypeAttr {
+                       get {
+                               return base.TypeAttr | TypeAttributes.Abstract | TypeAttributes.Sealed;
+                       }
                }
        }
 
@@ -2402,9 +2711,12 @@ namespace Mono.CSharp {
                        Modifiers.SEALED |
                        Modifiers.UNSAFE;
 
+               // Information in the case we are an attribute type
+               AttributeUsageAttribute attribute_usage;
+
                public Class (NamespaceEntry ns, TypeContainer parent, string name, int mod,
                              Attributes attrs, Location l)
-                       : base (ns, parent, name, attrs, l)
+                       : base (ns, parent, name, attrs, Kind.Class, l)
                {
                        int accmods;
 
@@ -2414,15 +2726,48 @@ namespace Mono.CSharp {
                                accmods = Modifiers.PRIVATE;
 
                        this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, l);
+                       if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.SEALED)) == (Modifiers.ABSTRACT | Modifiers.SEALED)) {
+                               Report.Error (502, Location, "'{0}' cannot be both abstract and sealed", GetSignatureForError ());
+                       }
+
+                       attribute_usage = new AttributeUsageAttribute (AttributeTargets.All);
+               }
+
+               public override AttributeTargets AttributeTargets {
+                       get {
+                               return AttributeTargets.Class;
+                       }
                }
 
+               public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
+               {
+                       if (a.UsageAttribute != null) {
+                               if (base_class_type != TypeManager.attribute_type && !base_class_type.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.Name);
+                               }
+                               attribute_usage = a.UsageAttribute;
+                       }
+
+                       base.ApplyAttributeBuilder (a, cb);
+               }
+
+               public AttributeUsageAttribute AttributeUsage {
+                       get {
+                               return attribute_usage;
+                       }
+               }
+
+               public const TypeAttributes DefaultTypeAttributes =
+                       TypeAttributes.AutoLayout | TypeAttributes.Class;
+
                //
                // FIXME: How do we deal with the user specifying a different
                // layout?
                //
                public override TypeAttributes TypeAttr {
                        get {
-                               return base.TypeAttr | TypeAttributes.AutoLayout | TypeAttributes.Class;
+                               return base.TypeAttr | DefaultTypeAttributes;
                        }
                }
        }
@@ -2439,8 +2784,9 @@ namespace Mono.CSharp {
                        Modifiers.UNSAFE    |
                        Modifiers.PRIVATE;
 
-               public Struct (NamespaceEntry ns, TypeContainer parent, string name, int mod, Attributes attrs, Location l)
-                       : base (ns, parent, name, attrs, l)
+               public Struct (NamespaceEntry ns, TypeContainer parent, string name,
+                              int mod, Attributes attrs, Location l)
+                       : base (ns, parent, name, attrs, Kind.Struct, l)
                {
                        int accmods;
                        
@@ -2454,6 +2800,17 @@ namespace Mono.CSharp {
                        this.ModFlags |= Modifiers.SEALED;
                }
 
+               public override AttributeTargets AttributeTargets {
+                       get {
+                               return AttributeTargets.Struct;
+                       }
+               }
+
+               public const TypeAttributes DefaultTypeAttributes =
+                       TypeAttributes.SequentialLayout |
+                       TypeAttributes.Sealed |
+                       TypeAttributes.BeforeFieldInit;
+
                //
                // FIXME: Allow the user to specify a different set of attributes
                // in some cases (Sealed for example is mandatory for a class,
@@ -2461,10 +2818,7 @@ namespace Mono.CSharp {
                //
                public override TypeAttributes TypeAttr {
                        get {
-                               return base.TypeAttr |
-                                       TypeAttributes.SequentialLayout |
-                                       TypeAttributes.Sealed |
-                                       TypeAttributes.BeforeFieldInit;
+                               return base.TypeAttr | DefaultTypeAttributes;
                        }
                }
        }
@@ -2486,7 +2840,7 @@ namespace Mono.CSharp {
 
                public Interface (NamespaceEntry ns, TypeContainer parent, string name, int mod,
                                  Attributes attrs, Location l)
-                       : base (ns, parent, name, attrs, l)
+                       : base (ns, parent, name, attrs, Kind.Interface, l)
                {
                        int accmods;
 
@@ -2498,79 +2852,321 @@ namespace Mono.CSharp {
                        this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, l);
                }
 
+               public override void Register ()
+               {
+                       Parent.AddInterface (this);
+               }
+
+               public override PendingImplementation GetPendingImplementations ()
+               {
+                       return null;
+               }
+
+               public override AttributeTargets AttributeTargets {
+                       get {
+                               return AttributeTargets.Interface;
+                       }
+               }
+
+               public const TypeAttributes DefaultTypeAttributes =
+                       TypeAttributes.AutoLayout |
+                       TypeAttributes.Abstract |
+                       TypeAttributes.Interface;
+
                public override TypeAttributes TypeAttr {
                        get {
-                               return base.TypeAttr |
-                                       TypeAttributes.AutoLayout |
-                                       TypeAttributes.Abstract |
-                                       TypeAttributes.Interface;
+                               return base.TypeAttr | DefaultTypeAttributes;
+                       }
+               }
+       }
+
+       public abstract class MethodCore : MemberBase {
+               public readonly Parameters Parameters;
+               protected Block block;
+               
+               //
+               // Parameters, cached for semantic analysis.
+               //
+               protected InternalParameters parameter_info;
+               protected Type [] parameter_types;
+
+               // Whether this is an operator method.
+               public bool IsOperator;
+
+               //
+               // The method we're overriding if this is an override method.
+               //
+               protected MethodInfo parent_method = null;
+
+               static string[] attribute_targets = new string [] { "method", "return" };
+
+               public MethodCore (TypeContainer parent, Expression type, int mod,
+                                  int allowed_mod, bool is_interface, string name,
+                                  Attributes attrs, Parameters parameters, Location loc)
+                       : base (parent, type, mod, allowed_mod, Modifiers.PRIVATE, name,
+                               attrs, loc)
+               {
+                       Parameters = parameters;
+                       IsInterface = is_interface;
+               }
+               
+               //
+               //  Returns the System.Type array for the parameters of this method
+               //
+               public Type [] ParameterTypes {
+                       get {
+                               return parameter_types;
+                       }
+               }
+
+               public InternalParameters ParameterInfo
+               {
+                       get {
+                               return parameter_info;
+                       }
+               }
+               
+               public Block Block {
+                       get {
+                               return block;
+                       }
+
+                       set {
+                               block = value;
+                       }
+               }
+
+               protected override bool CheckBase ()
+               {
+                       if (!base.CheckBase ())
+                               return false;
+                       
+                       // Check whether arguments were correct.
+                       if (!DoDefineParameters ())
+                               return false;
+
+                       if ((caching_flags & Flags.TestMethodDuplication) != 0 && !CheckForDuplications ())
+                               return false;
+
+                       if (IsExplicitImpl)
+                               return true;
+
+                       // Is null for System.Object while compiling corlib and base interfaces
+                       if (Parent.ParentContainer == null) {
+                               if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
+                                       Report.Warning (109, Location, "The member '{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError (Parent));
+                               }
+                               return true;
+                       }
+
+                       Type parent_ret_type = null;
+                       parent_method = FindOutParentMethod (Parent, ref parent_ret_type);
+
+                       // method is override
+                       if (parent_method != null) {
+
+                               if (!CheckMethodAgainstBase ())
+                                       return false;
+
+                               if ((ModFlags & Modifiers.NEW) == 0) {
+                                       if (MemberType != TypeManager.TypeToCoreType (parent_ret_type)) {
+                                               Report.SymbolRelatedToPreviousError (parent_method);
+                                               Report.Error (508, Location, GetSignatureForError (Parent) + ": cannot " +
+                                                       "change return type when overriding inherited member");
+                                               return false;
+                                       }
+                               }
+
+                               if (RootContext.WarningLevel > 2) {
+                                       if (Name == "Equals" && parameter_types.Length == 1 && parameter_types [0] == TypeManager.object_type)
+                                               Parent.Methods.HasEquals = true;
+                                       else if (Name == "GetHashCode" && parameter_types.Length == 0)
+                                               Parent.Methods.HasGetHashCode = true;
+                               }
+
+                               ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (parent_method);
+                               if (oa != null) {
+                                       Report.SymbolRelatedToPreviousError (parent_method);
+                                       Report.Warning (672, Location, "Member '{0}' overrides obsolete member. Add the Obsolete attribute to '{0}'", GetSignatureForError (Parent));
+                               }
+                               return true;
+                       }
+
+                       if ((ModFlags & Modifiers.OVERRIDE) != 0) {
+                               Report.Error (115, Location, "'{0}': no suitable methods found to override", GetSignatureForError (Parent));
+                               return false;
+                       }
+
+                       MemberInfo conflict_symbol = Parent.FindMemberWithSameName (Name, !(this is Property));
+                       if (conflict_symbol == null) {
+                               if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
+                                       Report.Warning (109, Location, "The member '{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError (Parent));
+                               }
+                               return true;
+                       }
+
+                       if ((ModFlags & Modifiers.NEW) == 0) {
+                               if (this is Method && conflict_symbol is MethodBase)
+                                       return true;
+
+                               Report.SymbolRelatedToPreviousError (conflict_symbol);
+                               Report.Warning (108, Location, "The keyword new is required on '{0}' because it hides inherited member", GetSignatureForError (Parent));
+                       }
+
+                       return true;
+               }
+
+
+               //
+               // Performs various checks on the MethodInfo `mb' regarding the modifier flags
+               // that have been defined.
+               //
+               // `name' is the user visible name for reporting errors (this is used to
+               // provide the right name regarding method names and properties)
+               //
+               bool CheckMethodAgainstBase ()
+               {
+                       bool ok = true;
+
+                       // TODO: replace with GetSignatureForError 
+                       string name = parent_method.DeclaringType.Name + "." + parent_method.Name;
+
+                       if ((ModFlags & Modifiers.OVERRIDE) != 0){
+                               if (!(parent_method.IsAbstract || parent_method.IsVirtual)){
+                                       Report.Error (
+                                               506, Location, Parent.MakeName (Name) +
+                                               ": cannot override inherited member `" +
+                                               name + "' because it is not " +
+                                               "virtual, abstract or override");
+                                       ok = false;
+                               }
+                               
+                               // Now we check that the overriden method is not final
+                               
+                               if (parent_method.IsFinal) {
+                                       // This happens when implementing interface methods.
+                                       if (parent_method.IsHideBySig && parent_method.IsVirtual) {
+                                               Report.Error (
+                                                       506, Location, Parent.MakeName (Name) +
+                                                       ": cannot override inherited member `" +
+                                                       name + "' because it is not " +
+                                                       "virtual, abstract or override");
+                                       } else
+                                               Report.Error (239, Location, Parent.MakeName (Name) + " : cannot " +
+                                                             "override inherited member `" + name +
+                                                             "' because it is sealed.");
+                                       ok = false;
+                               }
+                               //
+                               // Check that the permissions are not being changed
+                               //
+                               MethodAttributes thisp = flags & MethodAttributes.MemberAccessMask;
+                               MethodAttributes parentp = parent_method.Attributes & MethodAttributes.MemberAccessMask;
+
+                               //
+                               // special case for "protected internal"
+                               //
+
+                               if ((parentp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
+                                       //
+                                       // when overriding protected internal, the method can be declared
+                                       // protected internal only within the same assembly
+                                       //
+
+                                       if ((thisp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
+                                               if (Parent.TypeBuilder.Assembly != parent_method.DeclaringType.Assembly){
+                                                       //
+                                                       // assemblies differ - report an error
+                                                       //
+                                                       
+                                                       Error_CannotChangeAccessModifiers (Parent, parent_method, name);
+                                                   ok = false;
+                                               } else if (thisp != parentp) {
+                                                       //
+                                                       // same assembly, but other attributes differ - report an error
+                                                       //
+                                                       
+                                                       Error_CannotChangeAccessModifiers (Parent, parent_method, name);
+                                                       ok = false;
+                                               };
+                                       } else if ((thisp & MethodAttributes.Family) != MethodAttributes.Family) {
+                                               //
+                                               // if it's not "protected internal", it must be "protected"
+                                               //
+
+                                               Error_CannotChangeAccessModifiers (Parent, parent_method, name);
+                                               ok = false;
+                                       } else if (Parent.TypeBuilder.Assembly == parent_method.DeclaringType.Assembly) {
+                                               //
+                                               // protected within the same assembly - an error
+                                               //
+                                               Error_CannotChangeAccessModifiers (Parent, parent_method, name);
+                                               ok = false;
+                                       } else if ((thisp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem)) != 
+                                                  (parentp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem))) {
+                                               //
+                                               // protected ok, but other attributes differ - report an error
+                                               //
+                                               Error_CannotChangeAccessModifiers (Parent, parent_method, name);
+                                               ok = false;
+                                       }
+                               } else {
+                                       if (thisp != parentp){
+                                               Error_CannotChangeAccessModifiers (Parent, parent_method, name);
+                                               ok = false;
+                                       }
+                               }
                        }
-               }
-       }
-
-       public abstract class MethodCore : MemberBase {
-               public readonly Parameters Parameters;
-               protected Block block;
-               public DeclSpace ds;
-               
-               //
-               // Parameters, cached for semantic analysis.
-               //
-               protected InternalParameters parameter_info;
-               protected Type [] parameter_types;
 
-               // <summary>
-               //   This is set from TypeContainer.DefineMembers if this method overrides something.
-               // </summary>
-               public bool OverridesSomething;
+                       if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0 && Name != "Finalize") {
+                               ModFlags |= Modifiers.NEW;
+                               Report.SymbolRelatedToPreviousError (parent_method);
+                               if (!IsInterface && (parent_method.IsVirtual || parent_method.IsAbstract)) {
+                                       if (RootContext.WarningLevel >= 2)
+                                               Report.Warning (114, Location, "'{0}' hides inherited member '{1}'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword", GetSignatureForError (Parent), parent_method);
+                               } else
+                                       Report.Warning (108, Location, "The keyword new is required on '{0}' because it hides inherited member", GetSignatureForError (Parent));
+                       }
 
-               // Whether this is an operator method.
-               public bool IsOperator;
+                       return ok;
+               }
 
-               public MethodCore (DeclSpace ds, Expression type, int mod, int allowed_mod,
-                                  bool is_interface, string name, Attributes attrs,
-                                  Parameters parameters, Location loc)
-                       : base (type, mod, allowed_mod, Modifiers.PRIVATE, name, attrs, loc)
+               void Error_CannotChangeAccessModifiers (TypeContainer parent, MethodInfo parent_method, string name)
                {
-                       Parameters = parameters;
-                       IsInterface = is_interface;
-                       this.ds = ds;
-               }
-               
-               //
-               //  Returns the System.Type array for the parameters of this method
-               //
-               public Type [] ParameterTypes {
-                       get {
-                               return parameter_types;
-                       }
+                       //
+                       // FIXME: report the old/new permissions?
+                       //
+                       Report.Error (
+                               507, Location, parent.MakeName (Name) +
+                               ": can't change the access modifiers when overriding inherited " +
+                               "member `" + name + "'");
                }
 
-               public InternalParameters ParameterInfo
-               {
+               protected static string Error722 {
                        get {
-                               return parameter_info;
+                               return "'{0}': static types cannot be used as return types";
                        }
                }
-               
-               public Block Block {
-                       get {
-                               return block;
-                       }
 
-                       set {
-                               block = value;
-                       }
-               }
+               /// <summary>
+               /// For custom member duplication search in a container
+               /// </summary>
+               protected abstract bool CheckForDuplications ();
+
+               /// <summary>
+               /// Gets parent method and its return type
+               /// </summary>
+               protected abstract MethodInfo FindOutParentMethod (TypeContainer container, ref Type parent_ret_type);
 
                protected virtual bool DoDefineParameters ()
                {
                        // Check if arguments were correct
-                       parameter_types = Parameters.GetParameterInfo (ds);
-                       if ((parameter_types == null) || !CheckParameters (ds, parameter_types))
+                       parameter_types = Parameters.GetParameterInfo (Parent);
+                       if ((parameter_types == null) ||
+                           !CheckParameters (Parent, parameter_types))
                                return false;
 
-                       parameter_info = new InternalParameters (ds, Parameters);
+                       parameter_info = new InternalParameters (Parent, Parameters);
 
                        Parameter array_param = Parameters.ArrayParameter;
                        if ((array_param != null) &&
@@ -2583,25 +3179,35 @@ namespace Mono.CSharp {
                        return true;
                }
 
+               public override string[] ValidAttributeTargets {
+                       get {
+                               return attribute_targets;
+                       }
+               }
+
                protected override bool VerifyClsCompliance (DeclSpace ds)
                {
                        if (!base.VerifyClsCompliance (ds)) {
                                if ((ModFlags & Modifiers.ABSTRACT) != 0 && IsExposedFromAssembly (ds) && ds.IsClsCompliaceRequired (ds)) {
-                                       Report.Error_T (3011, Location, GetSignatureForError ());
+                                       Report.Error (3011, Location, "'{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
                                }
                                return false;
                        }
 
-                       AttributeTester.AreParametersCompliant (Parameters.FixedParameters, Location);
+                       if (Parameters.HasArglist) {
+                               Report.Error (3000, Location, "Methods with variable arguments are not CLS-compliant");
+                       }
 
                        if (!AttributeTester.IsClsCompliant (MemberType)) {
-                               Report.Error_T (3002, Location, GetSignatureForError ());
+                               Report.Error (3002, Location, "Return type of '{0}' is not CLS-compliant", GetSignatureForError ());
                        }
 
+                       AttributeTester.AreParametersCompliant (Parameters.FixedParameters, Location);
+
                        return true;
                }
 
-               protected bool IsDuplicateImplementation (TypeContainer tc, MethodCore method)
+               protected bool IsDuplicateImplementation (MethodCore method)
                {
                        if ((method == this) || (method.Name != Name))
                                return false;
@@ -2617,6 +3223,12 @@ namespace Mono.CSharp {
                                if (param_types [i] != ParameterTypes [i])
                                        return false;
 
+                       // TODO: make operator compatible with MethodCore to avoid this
+                       if (this is Operator && method is Operator) {
+                               if (MemberType != method.MemberType)
+                                       return false;
+                       }
+
                        //
                        // Try to report 663: method only differs on out/ref
                        //
@@ -2629,136 +3241,93 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       Report.Error (111, Location, "Class `{0}' already defines a " +
-                                     "member called `{1}' with the same parameter types",
-                                     tc.Name, Name);
+                       Report.SymbolRelatedToPreviousError (method);
+                       Report.Error (111, Location, TypeContainer.Error111, Parent.Name, Name);
                        return true;
                }
 
-               public CallingConventions GetCallingConvention (bool is_class)
+               protected override void VerifyObsoleteAttribute()
                {
-                       CallingConventions cc = 0;
-                       
-                       cc = Parameters.GetCallingConvention ();
+                       base.VerifyObsoleteAttribute ();
 
-                       if (is_class)
-                               if ((ModFlags & Modifiers.STATIC) == 0)
-                                       cc |= CallingConventions.HasThis;
+                       if (parameter_types == null)
+                               return;
 
-                       // FIXME: How is `ExplicitThis' used in C#?
-                       
-                       return cc;
+                       foreach (Type type in parameter_types) {
+                               CheckUsageOfObsoleteAttribute (type);
+                       }
                }
+       }
 
-               //
-               // The method's attributes are passed in because we need to extract
-               // the "return:" attribute from there to apply on the return type
-               //
-               static public void LabelParameters (EmitContext ec,
-                                                    MethodBase builder,
-                                                    Parameters parameters,
-                                                    Attributes method_attrs,
-                                                    Location loc)
+       public class SourceMethod : ISourceMethod
+       {
+               TypeContainer container;
+               MethodBase builder;
+
+               protected SourceMethod (TypeContainer container, MethodBase builder,
+                                       ISourceFile file, Location start, Location end)
                {
-                       //
-                       // Define each type attribute (in/out/ref) and
-                       // the argument names.
-                       //
-                       Parameter [] p = parameters.FixedParameters;
-                       int i = 0;
+                       this.container = container;
+                       this.builder = builder;
                        
-                       MethodBuilder mb = null;
-                       ConstructorBuilder cb = null;
-
-                       if (builder is MethodBuilder)
-                               mb = (MethodBuilder) builder;
-                       else
-                               cb = (ConstructorBuilder) builder;
+                       CodeGen.SymbolWriter.OpenMethod (
+                               file, this, start.Row, 0, end.Row, 0);
+               }
 
-                       if (p != null){
-                               for (i = 0; i < p.Length; i++) {
-                                       ParameterBuilder pb;
-                                       ParameterAttributes par_attr = p [i].Attributes;
-                                       
-                                       if (mb == null)
-                                               pb = cb.DefineParameter (
-                                                       i + 1, par_attr, p [i].Name);
-                                       else 
-                                               pb = mb.DefineParameter (
-                                                       i + 1, par_attr, p [i].Name);
-                                       
-                                       Attributes attr = p [i].OptAttributes;
-                                       if (attr != null){
-                                               Attribute.ApplyAttributes (ec, pb, p[i], attr);
-
-                                               if (par_attr == ParameterAttributes.Out){
-                                                       if (attr.Contains (TypeManager.in_attribute_type, ec.DeclSpace))
-                                                               Report.Error (36, loc,
-                                                                    "Can not use [In] attribute on out parameter");
-                                               }
-                                       }
-                               }
-                       }
+               public string Name {
+                       get { return builder.Name; }
+               }
 
-                       if (parameters.ArrayParameter != null){
-                               ParameterBuilder pb;
-                               Parameter array_param = parameters.ArrayParameter;
+               public int NamespaceID {
+                       get { return container.NamespaceEntry.SymbolFileID; }
+               }
 
-                               if (mb == null)
-                                       pb = cb.DefineParameter (
-                                               i + 1, array_param.Attributes,
-                                               array_param.Name);
+               public int Token {
+                       get {
+                               if (builder is MethodBuilder)
+                                       return ((MethodBuilder) builder).GetToken ().Token;
+                               else if (builder is ConstructorBuilder)
+                                       return ((ConstructorBuilder) builder).GetToken ().Token;
                                else
-                                       pb = mb.DefineParameter (
-                                               i + 1, array_param.Attributes,
-                                               array_param.Name);
-                                       
-                               CustomAttributeBuilder a = new CustomAttributeBuilder (
-                                       TypeManager.cons_param_array_attribute, new object [0]);
-                               
-                               pb.SetCustomAttribute (a);
+                                       throw new NotSupportedException ();
                        }
+               }
 
-                       //
-                       // And now for the return type attribute decoration
-                       //
-                       ParameterBuilder ret_pb;
-                       Attributes ret_attrs = null;
-                               
-                       if (mb == null || method_attrs == null)
-                               return;
-
-                       foreach (AttributeSection asec in method_attrs.AttributeSections) {
+               public void CloseMethod ()
+               {
+                       if (CodeGen.SymbolWriter != null)
+                               CodeGen.SymbolWriter.CloseMethod ();
+               }
 
-                               if (asec.Target != "return")
-                                       continue;
+               public static SourceMethod Create (TypeContainer parent,
+                                                  MethodBase builder, Block block)
+               {
+                       if (CodeGen.SymbolWriter == null)
+                               return null;
+                       if (block == null)
+                               return null;
 
-                               if (ret_attrs == null)
-                                       ret_attrs = new Attributes (asec);
-                               else
-                                       ret_attrs.AddAttributeSection (asec);
-                       }
+                       Location start_loc = block.StartLocation;
+                       if (Location.IsNull (start_loc))
+                               return null;
 
-                       if (ret_attrs != null) {
-                               ParameterBase ret = new ParameterBase (ret_attrs);
+                       Location end_loc = block.EndLocation;
+                       if (Location.IsNull (end_loc))
+                               return null;
 
-                               try {
-                                       ret_pb = mb.DefineParameter (0, ParameterAttributes.None, "");                          
-                                       Attribute.ApplyAttributes (ec, ret_pb, ret, ret_attrs);
+                       ISourceFile file = start_loc.SourceFile;
+                       if (file == null)
+                               return null;
 
-                                } catch (ArgumentOutOfRangeException) {
-                                       Report.Warning (
-                                               -24, loc,
-                                               ".NET SDK 1.0 does not permit setting custom attributes" +
-                                                " on the return type of a method");
-                               }
-                       }
+                       return new SourceMethod (
+                               parent, builder, file, start_loc, end_loc);
                }
        }
 
        public class Method : MethodCore, IIteratorContainer, IMethodData {
                public MethodBuilder MethodBuilder;
                public MethodData MethodData;
+               ReturnParameter return_attributes;
 
                /// <summary>
                ///   Modifiers allowed in a class declaration
@@ -2779,12 +3348,12 @@ namespace Mono.CSharp {
                        Modifiers.EXTERN;
 
                const int AllowedInterfaceModifiers =
-                       Modifiers.NEW;
+                       Modifiers.NEW | Modifiers.UNSAFE;
 
                //
                // return_type can be "null" for VOID values.
                //
-               public Method (DeclSpace ds, Expression return_type, int mod, bool is_iface,
+               public Method (TypeContainer ds, Expression return_type, int mod, bool is_iface,
                               string name, Parameters parameters, Attributes attrs, Location l)
                        : base (ds, return_type, mod,
                                is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
@@ -2792,21 +3361,44 @@ namespace Mono.CSharp {
                {
                }
 
-               //
-               // Returns the `System.Type' for the ReturnType of this
-               // function.  Provides a nice cache.  (used between semantic analysis
-               // and actual code generation
-               //
-               public Type GetReturnType ()
-               {
-                       return MemberType;
+               public override AttributeTargets AttributeTargets {
+                       get {
+                               return AttributeTargets.Method;
+                       }
                }
-
+               
                public override string GetSignatureForError()
                {
+                       if (MethodBuilder == null) {
+                               return GetSignatureForError (Parent);
+                       }
                        return TypeManager.CSharpSignature (MethodBuilder);
                }
 
+               /// <summary>
+               /// Use this method when MethodBuilder is null
+               /// </summary>
+               public override string GetSignatureForError (TypeContainer tc)
+               {
+                       // TODO: get params from somewhere
+                       if (parameter_info == null)
+                               return base.GetSignatureForError (tc);
+
+                       // TODO: move to parameters
+                       System.Text.StringBuilder args = new System.Text.StringBuilder ();
+                       if (parameter_info.Parameters.FixedParameters != null) {
+                               for (int i = 0; i < parameter_info.Parameters.FixedParameters.Length; ++i) {
+                                       Parameter p = parameter_info.Parameters.FixedParameters [i];
+                                       args.Append (p.GetSignatureForError ());
+
+                                       if (i < parameter_info.Parameters.FixedParameters.Length - 1)
+                                               args.Append (',');
+                               }
+                       }
+
+                       return String.Concat (base.GetSignatureForError (tc), "(", args.ToString (), ")");
+               }
+
                 void DuplicateEntryPoint (MethodInfo b, Location location)
                 {
                         Report.Error (
@@ -2816,14 +3408,6 @@ namespace Mono.CSharp {
                                 TypeManager.CSharpSignature(b) + "'");
                 }
 
-                void Report28 (MethodInfo b)
-                {
-                        Report.Warning (
-                                28, Location,
-                                "`" + TypeManager.CSharpSignature(b) +
-                                "' has the wrong signature to be an entry point");
-                }
-
                 public bool IsEntryPoint (MethodBuilder b, InternalParameters pinfo)
                 {
                         if (b.ReturnType != TypeManager.void_type &&
@@ -2846,97 +3430,105 @@ namespace Mono.CSharp {
                                 return false;
                 }
 
-               public override void ApplyAttributeBuilder (object builder, Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
                {
-                       MethodBuilder mb = builder as MethodBuilder;
+                       if (a.Target == AttributeTargets.ReturnValue) {
+                               if (return_attributes == null)
+                                       return_attributes = new ReturnParameter (MethodBuilder, Location);
 
-                       if (a.Type == TypeManager.methodimpl_attr_type && a.IsInternalCall)
-                               mb.SetImplementationFlags (MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
-                       else if (a.Type != TypeManager.dllimport_type)
-                               mb.SetCustomAttribute (cb);
-               }
+                               return_attributes.ApplyAttributeBuilder (a, cb);
+                               return;
+                       }
 
-               //
-               // Checks our base implementation if any
-               //
-               protected override bool CheckBase (TypeContainer container)
-               {
-                       base.CheckBase (container);
-                       
-                       // Check whether arguments were correct.
-                       if (!DoDefineParameters ())
-                               return false;
+                       if (a.Type == TypeManager.methodimpl_attr_type && a.IsInternalCall) {
+                               MethodBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
+                       }
 
-                       MethodSignature ms = new MethodSignature (Name, null, ParameterTypes);
-                       if (IsOperator) {
-                               flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
-                       } else {
-                               //
-                               // Check in our class for dups
-                               //
-                               ArrayList ar = container.Methods;
-                               if (ar != null) {
-                                       int arLen = ar.Count;
-                                       
-                                       for (int i = 0; i < arLen; i++) {
-                                               Method m = (Method) ar [i];
-                                               if (IsDuplicateImplementation (container, m))
-                                                       return false;
-                                       }
+                       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;
                        }
-                       
 
-                       //
-                       // Verify if the parent has a type with the same name, and then
-                       // check whether we have to create a new slot for it or not.
-                       //
-                       Type ptype = container.TypeBuilder.BaseType;
+                       if (a.Type == TypeManager.conditional_attribute_type) {
+                               if (IsOperator || IsExplicitImpl) {
+                                       Report.Error (577, Location, "Conditional not valid on '{0}' because it is a destructor, operator, or explicit interface implementation", GetSignatureForError ());
+                                       return;
+                               }
 
-                       // ptype is only null for System.Object while compiling corlib.
-                       if (ptype != null) {
-                               
-                               //
-                               // Explicit implementations do not have `parent' methods, however,
-                               // the member cache stores them there. Without this check, we get
-                               // an incorrect warning in corlib.
-                               //
-                               if (! IsExplicitImpl) {
-                                       parent_method = (MethodInfo)((IMemberContainer)container).Parent.MemberCache.FindMemberToOverride (
-                                               container.TypeBuilder, Name, ParameterTypes, false);
+                               if (ReturnType != TypeManager.void_type) {
+                                       Report.Error (578, Location, "Conditional not valid on '{0}' because its return new ErrorData ( type is not void", GetSignatureForError ());
+                                       return;
                                }
-                               
-                               if (parent_method != null) {
-                                       string name = parent_method.DeclaringType.Name + "." +
-                                               parent_method.Name;
 
-                                       if (!CheckMethodAgainstBase (container, flags, parent_method, name))
-                                               return false;
+                               if ((ModFlags & Modifiers.OVERRIDE) != 0) {
+                                       Report.Error (243, Location, "Conditional not valid on '{0}' because it is an override method", GetSignatureForError ());
+                                       return;
+                               }
+
+                               if (IsInterface) {
+                                       Report.Error (582, Location, "Conditional not valid on interface members");
+                                       return;
+                               }
 
-                                       if ((ModFlags & Modifiers.NEW) == 0) {
-                                               Type parent_ret = TypeManager.TypeToCoreType (
-                                                       parent_method.ReturnType);
+                               if (MethodData.IsImplementing) {
+                                       Report.Error (629, Location, "Conditional member '{0}' cannot implement interface member", GetSignatureForError ());
+                                       return;
+                               }
 
-                                               if (parent_ret != MemberType) {
-                                                       Report.Error (
-                                                               508, Location, container.MakeName (Name) + ": cannot " +
-                                                               "change return type when overriding " +
-                                                               "inherited member " + name);
-                                                       return false;
-                                               }
+                               for (int i = 0; i < parameter_info.Count; ++i) {
+                                       if ((parameter_info.ParameterModifier (i) & Parameter.Modifier.OUT) != 0) {
+                                               Report.Error (685, Location, "Conditional method '{0}' cannot have an out parameter", GetSignatureForError ());
+                                               return;
                                        }
-                               } else {
-                                       if (!OverridesSomething && ((ModFlags & Modifiers.NEW) != 0))
-                                               WarningNotHiding (container);
+                               }
+                       }
 
-                                       if ((ModFlags & Modifiers.OVERRIDE) != 0){
-                                               Report.Error (115, Location,
-                                                             container.MakeName (Name) +
-                                                             " no suitable methods found to override");
-                                       }
+                       MethodBuilder.SetCustomAttribute (cb);
+               }
+
+               protected override bool CheckForDuplications ()
+               {
+                       ArrayList ar = Parent.Methods;
+                       if (ar != null) {
+                               int arLen = ar.Count;
+                                       
+                               for (int i = 0; i < arLen; i++) {
+                                       Method m = (Method) ar [i];
+                                       if (IsDuplicateImplementation (m))
+                                               return false;
+                               }
+                       }
+
+                       ar = Parent.Properties;
+                       if (ar != null) {
+                               for (int i = 0; i < ar.Count; ++i) {
+                                       PropertyBase pb = (PropertyBase) ar [i];
+                                       if (pb.AreAccessorsDuplicateImplementation (this))
+                                               return false;
+                               }
+                       }
+
+                       ar = Parent.Indexers;
+                       if (ar != null) {
+                               for (int i = 0; i < ar.Count; ++i) {
+                                       PropertyBase pb = (PropertyBase) ar [i];
+                                       if (pb.AreAccessorsDuplicateImplementation (this))
+                                               return false;
+                               }
+                       }
+
+                       ar = Parent.Events;
+                       if (ar != null) {
+                               for (int i = 0; i < ar.Count; ++i) {
+                                       Event ev = (Event) ar [i];
+                                       if (ev.AreAccessorsDuplicateImplementation (this))
+                                               return false;
                                }
-                       } else if ((ModFlags & Modifiers.NEW) != 0)
-                               WarningNotHiding (container);
+                       }
 
                        return true;
                }
@@ -2944,32 +3536,34 @@ namespace Mono.CSharp {
                //
                // Creates the type
                //
-               public override bool Define (TypeContainer container)
+               public override bool Define ()
                {
-                       if (!DoDefine (container))
+                       if (!DoDefine ())
                                return false;
 
-                       if (!CheckBase (container))
+                       if (!CheckBase ())
                                return false;
 
-                       MethodData = new MethodData (this, ParameterInfo, ModFlags, flags, true, this);
+                       if (IsOperator)
+                               flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
+
+                       MethodData = new MethodData (this, ParameterInfo, ModFlags, flags, this);
 
-                       if (!MethodData.Define (container))
+                       if (!MethodData.Define (Parent))
                                return false;
 
                        //
                        // Setup iterator if we are one
                        //
                        if ((ModFlags & Modifiers.METHOD_YIELDS) != 0){
-                               IteratorHandler ih = new  IteratorHandler (
-                                           Name, container, MemberType,
-                                           ParameterTypes, ParameterInfo,
-                                           ModFlags, Location);
+                               Iterator iterator = new Iterator (
+                                       Parent, Name, MemberType, ParameterTypes,
+                                       ParameterInfo, ModFlags, block, Location);
 
-                               Block new_block = ih.Setup (block);
-                               if (new_block == null)
+                               if (!iterator.DefineIterator ())
                                        return false;
-                               block = new_block;
+
+                               block = iterator.Block;
                        }
 
                        MethodBuilder = MethodData.MethodBuilder;
@@ -2980,7 +3574,7 @@ namespace Mono.CSharp {
                        if (Name == "Main" &&
                            ((ModFlags & Modifiers.STATIC) != 0) && RootContext.NeedsEntryPoint && 
                            (RootContext.MainClass == null ||
-                            RootContext.MainClass == container.TypeBuilder.FullName)){
+                            RootContext.MainClass == Parent.TypeBuilder.FullName)){
                                 if (IsEntryPoint (MethodBuilder, ParameterInfo)) {
                                         if (RootContext.EntryPoint == null) {
                                                 RootContext.EntryPoint = MethodBuilder;
@@ -2989,8 +3583,15 @@ namespace Mono.CSharp {
                                                 DuplicateEntryPoint (RootContext.EntryPoint, RootContext.EntryPointLocation);
                                                 DuplicateEntryPoint (MethodBuilder, Location);
                                         }
-                                } else                                         
-                                               Report28(MethodBuilder);
+                                } else {
+                                       if (RootContext.WarningLevel >= 4)
+                                               Report.Warning (28, 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;
@@ -2999,29 +3600,53 @@ namespace Mono.CSharp {
                //
                // Emits the code
                // 
-               public override void Emit (TypeContainer container)
+               public override void Emit ()
                {
-                       MethodData.Emit (container, this);
-                       base.Emit (container);
+                       MethodData.Emit (Parent, this);
+                       base.Emit ();
                        Block = null;
                        MethodData = null;
                }
 
-               void IIteratorContainer.SetYields ()
+               protected override MethodInfo FindOutParentMethod (TypeContainer container, ref Type parent_ret_type)
                {
-                       ModFlags |= Modifiers.METHOD_YIELDS;
+                       MethodInfo mi = (MethodInfo) container.ParentContainer.MemberCache.FindMemberToOverride (
+                               container.TypeBuilder, Name, ParameterTypes, false);
+
+                       if (mi == null)
+                               return null;
+
+                       parent_ret_type = mi.ReturnType;
+                       return mi;
                }
-       
-               protected override bool IsIdentifierClsCompliant (DeclSpace ds)
+
+               protected override bool VerifyClsCompliance(DeclSpace ds)
                {
-                       return IsIdentifierAndParamClsCompliant (ds, Name, MethodBuilder, parameter_types);
+                       if (!base.VerifyClsCompliance (ds))
+                               return false;
+
+                       if (parameter_types.Length > 0) {
+                               ArrayList al = (ArrayList)ds.MemberCache.Members [Name];
+                               if (al.Count > 1)
+                                       ds.MemberCache.VerifyClsParameterConflict (al, this, MethodBuilder);
+                       }
+
+                       return true;
                }
 
+
+               void IIteratorContainer.SetYields ()
+               {
+                       ModFlags |= Modifiers.METHOD_YIELDS;
+               }
+       
                #region IMethodData Members
 
                public CallingConventions CallingConventions {
                        get {
                                CallingConventions cc = Parameters.GetCallingConvention ();
+                               if (Parameters.HasArglist)
+                                       block.HasVarargs = true;
 
                                if (!IsInterface)
                                        if ((ModFlags & Modifiers.STATIC) == 0)
@@ -3041,7 +3666,7 @@ namespace Mono.CSharp {
 
                public string MethodName {
                        get {
-                               return ShortName;
+                               return Name;
                        }
                }
 
@@ -3051,9 +3676,61 @@ namespace Mono.CSharp {
                        }
                }
 
-               public EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
-               {
-                       return new EmitContext (tc, ds, Location, ig, ReturnType, ModFlags, false);
+               public EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
+               {
+                       return new EmitContext (
+                               tc, Parent, Location, ig, ReturnType, ModFlags, false);
+               }
+
+               public ObsoleteAttribute GetObsoleteAttribute ()
+               {
+                       return GetObsoleteAttribute (Parent);
+               }
+
+               /// <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 (parent_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);
+                                       if (RootContext.AllDefines.Contains (condition))
+                                               return false;
+                               }
+
+                               caching_flags |= Flags.Excluded;
+                               return true;
+                       }
+
+                       IMethodData md = TypeManager.GetMethod (parent_method);
+                       if (md == null) {
+                               if (AttributeTester.IsConditionalMethodExcluded (parent_method)) {
+                                       caching_flags |= Flags.Excluded;
+                                       return true;
+                               }
+                               return false;
+                       }
+
+                       if (md.IsExcluded (ec)) {
+                               caching_flags |= Flags.Excluded;
+                               return true;
+                       }
+                       return false;
                }
 
                #endregion
@@ -3061,7 +3738,7 @@ namespace Mono.CSharp {
 
        public abstract class ConstructorInitializer {
                ArrayList argument_list;
-               ConstructorInfo parent_constructor;
+               protected ConstructorInfo parent_constructor;
                Parameters parameters;
                Location loc;
                
@@ -3129,8 +3806,9 @@ namespace Mono.CSharp {
                                return false;
                        }
                        
-                       parent_constructor = (ConstructorInfo) Invocation.OverloadResolve (ec, 
-                               (MethodGroupExpr) parent_constructor_group, argument_list, loc);
+                       parent_constructor = (ConstructorInfo) Invocation.OverloadResolve (
+                               ec, (MethodGroupExpr) parent_constructor_group, argument_list,
+                               false, loc);
                        
                        if (parent_constructor == null){
                                Report.Error (1501, loc,
@@ -3139,7 +3817,7 @@ namespace Mono.CSharp {
                        }
 
                        if (parent_constructor == caller_builder){
-                               Report.Error (515, String.Format ("Constructor `{0}' can not call itself", TypeManager.CSharpSignature (caller_builder)));
+                               Report.Error (516, String.Format ("Constructor `{0}' can not call itself", TypeManager.CSharpSignature (caller_builder)));
                                return false;
                        }
                        
@@ -3156,6 +3834,66 @@ namespace Mono.CSharp {
                                        Invocation.EmitCall (ec, true, false, ec.GetThis (loc), parent_constructor, argument_list, loc);
                        }
                }
+
+               /// <summary>
+               /// Method search for base ctor. (We do not cache it).
+               /// </summary>
+               Constructor GetOverloadedConstructor (TypeContainer tc)
+               {
+                       if (tc.InstanceConstructors == null)
+                               return null;
+
+                       foreach (Constructor c in tc.InstanceConstructors) {
+                               if (Arguments == null) {
+                                       if (c.ParameterTypes.Length == 0)
+                                               return c;
+
+                                       continue;
+                               }
+
+                               bool ok = true;
+
+                               int count = c.ParameterInfo.Count;
+                               if ((count > 0) &&
+                                   c.ParameterInfo.ParameterModifier (count - 1) == Parameter.Modifier.PARAMS) {
+                                       for (int i = 0; i < count-1; i++)
+                                               if (c.ParameterTypes [i] != ((Argument)Arguments [i]).Type) {
+                                                       ok = false;
+                                                       break;
+                                               }
+                               } else {
+                                       if (c.ParameterTypes.Length != Arguments.Count)
+                                               continue;
+
+                                       for (int i = 0; i < Arguments.Count; ++i)
+                                               if (c.ParameterTypes [i] != ((Argument)Arguments [i]).Type) {
+                                                       ok = false;
+                                                       break;
+                                               }
+                               }
+
+                               if (!ok)
+                                       continue;
+
+                               return c;
+                       }
+
+                       return null;
+               }
+
+               //TODO: implement caching when it will be necessary
+               public virtual void CheckObsoleteAttribute (TypeContainer tc, Location loc)
+               {
+                       Constructor ctor = GetOverloadedConstructor (tc);
+                       if (ctor == null)
+                               return;
+
+                       ObsoleteAttribute oa = ctor.GetObsoleteAttribute (tc);
+                       if (oa == null)
+                               return;
+
+                       AttributeTester.Report_ObsoleteMessage (oa, ctor.GetSignatureForError (), loc);
+               }
        }
 
        public class ConstructorBaseInitializer : ConstructorInitializer {
@@ -3163,6 +3901,24 @@ namespace Mono.CSharp {
                        base (argument_list, pars, l)
                {
                }
+
+               public override void CheckObsoleteAttribute(TypeContainer tc, Location loc) {
+                       if (parent_constructor == null)
+                               return;
+
+                       TypeContainer type_ds = TypeManager.LookupTypeContainer (tc.base_class_type);
+                       if (type_ds == null) {
+                               ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (parent_constructor);
+
+                               if (oa != null)
+                                       AttributeTester.Report_ObsoleteMessage (oa, TypeManager.CSharpSignature (parent_constructor), loc);
+
+                               return;
+                       }
+
+                       base.CheckObsoleteAttribute (type_ds, loc);
+               }
+
        }
 
        public class ConstructorThisInitializer : ConstructorInitializer {
@@ -3172,7 +3928,7 @@ namespace Mono.CSharp {
                }
        }
        
-       public class Constructor : MethodCore {
+       public class Constructor : MethodCore, IMethodData {
                public ConstructorBuilder ConstructorBuilder;
                public ConstructorInitializer Initializer;
 
@@ -3193,7 +3949,7 @@ namespace Mono.CSharp {
                // The spec claims that static is not permitted, but
                // my very own code has static constructors.
                //
-               public Constructor (DeclSpace ds, string name, int mod, Parameters args,
+               public Constructor (TypeContainer ds, string name, int mod, Parameters args,
                                    ConstructorInitializer init, Location l)
                        : base (ds, null, mod, AllowedModifiers, false, name, null, args, l)
                {
@@ -3202,6 +3958,9 @@ namespace Mono.CSharp {
 
                public override string GetSignatureForError()
                {
+                       if (ConstructorBuilder == null)
+                               return GetSignatureForError (Parent);
+
                        return TypeManager.CSharpSignature (ConstructorBuilder);
                }
 
@@ -3211,6 +3970,13 @@ namespace Mono.CSharp {
                        }
                }
 
+               public override AttributeTargets AttributeTargets {
+                       get {
+                               return AttributeTargets.Constructor;
+                       }
+               }
+
+
                //
                // Returns true if this is a default constructor
                //
@@ -3227,42 +3993,56 @@ namespace Mono.CSharp {
                                        (Initializer.Arguments == null);
                }
 
-               public override void ApplyAttributeBuilder (object builder, Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
                {
-                       ((ConstructorBuilder) builder).SetCustomAttribute (cb);
+                       ConstructorBuilder.SetCustomAttribute (cb);
                }
                
-               protected override bool CheckBase (TypeContainer container)
+               protected override bool CheckForDuplications ()
+               {
+                       ArrayList ar = Parent.InstanceConstructors;
+                       if (ar != null) {
+                               int arLen = ar.Count;
+                                       
+                               for (int i = 0; i < arLen; i++) {
+                                       Constructor m = (Constructor) ar [i];
+                                       if (IsDuplicateImplementation (m))
+                                               return false;
+                               }
+                       }
+                       return true;
+               }
+
+               protected override bool CheckBase ()
                {
-                       base.CheckBase (container);
-                       
                        // Check whether arguments were correct.
                        if (!DoDefineParameters ())
                                return false;
-                       
+
+                       // TODO: skip the rest for generated ctor
                        if ((ModFlags & Modifiers.STATIC) != 0)
                                return true;
-                       
-                       if (container is Struct && ParameterTypes.Length == 0) {
-                               Report.Error (568, Location, 
-                                       "Structs can not contain explicit parameterless " +
-                                       "constructors");
+
+                       if (!CheckForDuplications ())
                                return false;
-                       }
-                               
-                       //
-                       // Check in our class for dups
-                       //
-                       ArrayList ar = container.InstanceConstructors;
-                       if (ar != null) {
-                               int arLen = ar.Count;
-                                       
-                               for (int i = 0; i < arLen; i++) {
-                                       Constructor m = (Constructor) ar [i];
-                                       if (IsDuplicateImplementation (container, m))
-                                               return false;
+
+                       if (Parent.Kind == Kind.Struct) {
+                               if (ParameterTypes.Length == 0) {
+                                       Report.Error (568, Location, 
+                                               "Structs can not contain explicit parameterless " +
+                                               "constructors");
+                                       return false;
+                               }
+
+                               if ((ModFlags & Modifiers.PROTECTED) != 0) {
+                                       Report.Error (666, Location, "Protected member in struct declaration");
+                                       return false;
                                }
                        }
+
+                       if ((RootContext.WarningLevel >= 4) && ((Parent.ModFlags & Modifiers.SEALED) != 0 && (ModFlags & Modifiers.PROTECTED) != 0)) {
+                               Report.Warning (628, Location, "'{0}': new protected member declared in sealed class", GetSignatureForError (Parent));
+                       }
                        
                        return true;
                }
@@ -3270,7 +4050,7 @@ namespace Mono.CSharp {
                //
                // Creates the ConstructorBuilder
                //
-               public override bool Define (TypeContainer container)
+               public override bool Define ()
                {
                        MethodAttributes ca = (MethodAttributes.RTSpecialName |
                                               MethodAttributes.SpecialName);
@@ -3296,15 +4076,18 @@ namespace Mono.CSharp {
                        }
                        
                        // Check if arguments were correct.
-                       if (!CheckBase (container))
+                       if (!CheckBase ())
                                return false;
 
-                       ConstructorBuilder = container.TypeBuilder.DefineConstructor (
-                               ca, GetCallingConvention (container is Class), ParameterTypes);
+                       ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
+                               ca, CallingConventions,
+                               ParameterTypes);
 
                        if ((ModFlags & Modifiers.UNSAFE) != 0)
                                ConstructorBuilder.InitLocals = false;
                        
+                       TypeManager.AddMethod (ConstructorBuilder, this);
+
                        //
                        // HACK because System.Reflection.Emit is lame
                        //
@@ -3316,10 +4099,9 @@ namespace Mono.CSharp {
                //
                // Emits the code
                //
-               public override void Emit (TypeContainer container)
+               public override void Emit ()
                {
-                       ILGenerator ig = ConstructorBuilder.GetILGenerator ();
-                       EmitContext ec = new EmitContext (container, Location, ig, null, ModFlags, true);
+                       EmitContext ec = CreateEmitContext (null, null);
 
                        //
                        // extern methods have no bodies
@@ -3341,7 +4123,7 @@ namespace Mono.CSharp {
                        }
 
                        if ((ModFlags & Modifiers.STATIC) == 0){
-                               if (container is Class && Initializer == null)
+                               if (Parent.Kind == Kind.Class && Initializer == null)
                                        Initializer = new ConstructorBaseInitializer (
                                                null, Parameters.EmptyReadOnlyParameters, Location);
 
@@ -3356,25 +4138,15 @@ namespace Mono.CSharp {
                                ec.IsStatic = false;
                        }
 
-                       MethodCore.LabelParameters (ec, ConstructorBuilder,
-                                                    Parameters, OptAttributes, Location);
+                       Parameters.LabelParameters (ec, ConstructorBuilder, Location);
                        
-                       SymbolWriter sw = CodeGen.SymbolWriter;
-                       bool generate_debugging = false;
-
-                       if ((sw != null) && (block != null) &&
-                               !Location.IsNull (Location) &&
-                               !Location.IsNull (block.EndLocation) &&
-                               (Location.SymbolDocument != null)) {
-                               sw.OpenMethod (container, ConstructorBuilder, Location, block.EndLocation);
-
-                               generate_debugging = true;
-                       }
+                       SourceMethod source = SourceMethod.Create (
+                               Parent, ConstructorBuilder, block);
 
                        //
                        // Classes can have base initializers and instance field initializers.
                        //
-                       if (container is Class){
+                       if (Parent.Kind == Kind.Class){
                                if ((ModFlags & Modifiers.STATIC) == 0){
 
                                        //
@@ -3382,79 +4154,40 @@ namespace Mono.CSharp {
                                        // do not emit field initializers, they are initialized in the other constructor
                                        //
                                        if (!(Initializer != null && Initializer is ConstructorThisInitializer))
-                                               container.EmitFieldInitializers (ec);
+                                               Parent.EmitFieldInitializers (ec);
                                }
                        }
-                       if (Initializer != null)
+                       if (Initializer != null) {
+                               Initializer.CheckObsoleteAttribute (Parent, Location);
                                Initializer.Emit (ec);
+                       }
                        
                        if ((ModFlags & Modifiers.STATIC) != 0)
-                               container.EmitFieldInitializers (ec);
+                               Parent.EmitFieldInitializers (ec);
 
-                       Attribute.ApplyAttributes (ec, ConstructorBuilder, this, OptAttributes);
+                       if (OptAttributes != null) 
+                               OptAttributes.Emit (ec, this);
 
                        // If this is a non-static `struct' constructor and doesn't have any
                        // initializer, it must initialize all of the struct's fields.
-                       if ((container is Struct) && ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
-                               Block.AddThisVariable (container, Location);
+                       if ((Parent.Kind == Kind.Struct) &&
+                           ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
+                               Block.AddThisVariable (Parent, Location);
 
                        ec.EmitTopBlock (block, ParameterInfo, Location);
 
-                       if (generate_debugging)
-                               sw.CloseMethod ();
+                       if (source != null)
+                               source.CloseMethod ();
 
-                       base.Emit (container);
+                       base.Emit ();
 
                        block = null;
                }
 
-               // For constructors is needed to test only parameters
-               protected override bool IsIdentifierClsCompliant (DeclSpace ds)
+               // Is never override
+               protected override MethodInfo FindOutParentMethod (TypeContainer container, ref Type parent_ret_type)
                {
-                       if (parameter_types == null || parameter_types.Length == 0)
-                               return true;
-
-                       TypeContainer tc = ds as TypeContainer;
-
-                       for (int i = 0; i < tc.InstanceConstructors.Count; i++) {
-                               Constructor c = (Constructor) tc.InstanceConstructors [i];
-                                               
-                               if (c == this || c.ParameterTypes.Length == 0)
-                                       continue;
-
-                               if (!c.IsClsCompliaceRequired (ds))
-                                       continue;
-                               
-                               if (!AttributeTester.AreOverloadedMethodParamsClsCompliant (parameter_types, c.ParameterTypes)) {
-                                       Report.Error_T (3006, Location, GetSignatureForError ());
-                                       return false;
-                               }
-                       }
-
-                       if (tc.TypeBuilder.BaseType == null)
-                               return true;
-
-                       DeclSpace temp_ds = TypeManager.LookupDeclSpace (tc.TypeBuilder.BaseType);
-                       if (temp_ds != null)
-                               return IsIdentifierClsCompliant (temp_ds);
-
-                       MemberInfo[] ml = tc.TypeBuilder.BaseType.FindMembers (MemberTypes.Constructor, BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance, null, null);
-                       // Skip parameter-less ctor
-                       if (ml.Length < 2)
-                               return true;
-
-                       foreach (ConstructorInfo ci in ml) {
-                               object[] cls_attribute = ci.GetCustomAttributes (TypeManager.cls_compliant_attribute_type, false);
-                               if (cls_attribute.Length == 1 && (!((CLSCompliantAttribute)cls_attribute[0]).IsCompliant))
-                                       continue;
-
-                               if (!AttributeTester.AreOverloadedMethodParamsClsCompliant (parameter_types, TypeManager.GetArgumentTypes (ci))) {
-                                       Report.Error_T (3006, Location, GetSignatureForError ());
-                                       return false;
-                               }
-                       }
-                       
-                       return true;
+                       return null;
                }
 
                protected override bool VerifyClsCompliance (DeclSpace ds)
@@ -3463,10 +4196,16 @@ namespace Mono.CSharp {
                                return false;
                        }
                        
-                       if (ds.TypeBuilder.IsSubclassOf (TypeManager.attribute_type)) {
-                               foreach (Type param in parameter_types) {
-                                       if (param.IsArray) {
-                                               return false;
+                       if (parameter_types.Length > 0) {
+                               ArrayList al = (ArrayList)ds.MemberCache.Members [".ctor"];
+                               if (al.Count > 3)
+                                       ds.MemberCache.VerifyClsParameterConflict (al, this, ConstructorBuilder);
+                               if (ds.TypeBuilder.IsSubclassOf (TypeManager.attribute_type)) {
+                                       foreach (Type param in parameter_types) {
+                                               if (param.IsArray) {
+                                                       return true;
+                                               }
                                        }
                                }
                        }
@@ -3474,6 +4213,57 @@ namespace Mono.CSharp {
                        return true;
                }
 
+               #region IMethodData Members
+
+               public System.Reflection.CallingConventions CallingConventions {
+                       get {
+                               CallingConventions cc = Parameters.GetCallingConvention ();
+
+                               if (Parent.Kind == Kind.Class)
+                                       if ((ModFlags & Modifiers.STATIC) == 0)
+                                               cc |= CallingConventions.HasThis;
+
+                               // FIXME: How is `ExplicitThis' used in C#?
+                       
+                               return cc;
+                       }
+               }
+
+               public new Location Location {
+                       get {
+                               return base.Location;
+                       }
+               }
+
+               public string MethodName {
+                       get {
+                               return Name;
+                       }
+               }
+
+               public Type ReturnType {
+                       get {
+                               return MemberType;
+                       }
+               }
+
+               public EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
+               {
+                       ILGenerator ig_ = ConstructorBuilder.GetILGenerator ();
+                       return new EmitContext (Parent, Location, ig_, null, ModFlags, true);
+               }
+
+               public ObsoleteAttribute GetObsoleteAttribute ()
+               {
+                       return null;
+               }
+
+               public bool IsExcluded(EmitContext ec)
+               {
+                       return false;
+               }
+
+               #endregion
        }
 
        /// <summary>
@@ -3491,6 +4281,10 @@ namespace Mono.CSharp {
                Block Block { get; }
 
                EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig);
+               ObsoleteAttribute GetObsoleteAttribute ();
+               string GetSignatureForError (TypeContainer tc);
+               bool IsExcluded (EmitContext ec);
+               bool IsClsCompliaceRequired (DeclSpace ds);
        }
 
        //
@@ -3507,241 +4301,46 @@ namespace Mono.CSharp {
 
                //
                // Are we implementing an interface ?
-               //
-               public bool IsImplementing = false;
-
-               //
-               // Protected data.
-               //
-               protected MemberBase member;
-               protected int modifiers;
-               protected MethodAttributes flags;
-               protected bool is_method;
-
-               //
-               // It can either hold a string with the condition, or an arraylist of conditions.
-               object conditionals;
-
-               MethodBuilder builder = null;
-               public MethodBuilder MethodBuilder {
-                       get {
-                               return builder;
-                       }
-               }
-
-               public MethodData (MemberBase member, InternalParameters parameters,
-                                  int modifiers, MethodAttributes flags, bool is_method, IMethodData method)
-               {
-                       this.member = member;
-                       this.ParameterInfo = parameters;
-                       this.modifiers = modifiers;
-                       this.flags = flags;
-                       this.is_method = is_method;
-                       this.conditionals = null;
-
-                       this.method = method;
-               }
-
-               //
-               // Attributes.
-               //
-               Attribute dllimport_attribute = null;
-               string obsolete = null;
-               bool obsolete_error = false;
-
-               public virtual bool ApplyAttributes (Attributes opt_attrs, bool is_method, DeclSpace ds)
-               {
-                       if ((opt_attrs == null) || (opt_attrs.AttributeSections == null))
-                               return true;
-
-                       foreach (AttributeSection asec in opt_attrs.AttributeSections) {
-                               if (asec.Attributes == null)
-                                       continue;
-                                       
-                               foreach (Attribute a in asec.Attributes) {
-                                       Type attr_type = a.ResolveType (ds, true);
-                                       if (attr_type == TypeManager.conditional_attribute_type) {
-                                               if (!ApplyConditionalAttribute (a))
-                                                       return false;
-                                       } else if (attr_type == TypeManager.obsolete_attribute_type) {
-                                               if (!ApplyObsoleteAttribute (a))
-                                                       return false;
-                                       } else if (attr_type == TypeManager.dllimport_type) {
-                                               if (!is_method) {
-                                                       Attribute.Error_AttributeNotValidForElement (a, method.Location);
-                                                       return false;
-                                               }
-                                               if (!ApplyDllImportAttribute (a))
-                                                       return false;
-                                       }
-                               }
-                       }
-
-                       return true;
-               }
-
-               //
-               // Applies the `DllImport' attribute to the method.
-               //
-               protected virtual bool ApplyDllImportAttribute (Attribute a)
-               {
-                       const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
-                       if ((modifiers & extern_static) != extern_static) {
-                               Report.Error (601, method.Location,
-                                             "The DllImport attribute must be specified on a method " +
-                                             "marked `static' and `extern'.");
-                               return false;
-                       }
-
-                       flags |= MethodAttributes.PinvokeImpl;
-                       dllimport_attribute = a;
-                       return true;
-               }
-
-               //
-               // Applies the `Obsolete' attribute to the method.
-               //
-               protected virtual bool ApplyObsoleteAttribute (Attribute a)
-               {
-                       if (obsolete != null) {
-                               Report.Error (579, method.Location, "Duplicate `Obsolete' attribute");
-                               return false;
-                       }
-
-                       obsolete = a.Obsolete_GetObsoleteMessage (out obsolete_error);
-                       return obsolete != null;
-               }
-
-               //
-               // Applies the `Conditional' attribute to the method.
-               //
-               protected virtual bool ApplyConditionalAttribute (Attribute a)
-               {
-                       // The Conditional attribute is only valid on methods.
-                       if (!is_method) {
-                               Attribute.Error_AttributeNotValidForElement (a, method.Location);
-                               return false;
-                       }
-
-                       string condition = a.Conditional_GetConditionName ();
-
-                       if (condition == null)
-                               return false;
-
-                       if (method.ReturnType != TypeManager.void_type) {
-                               Report.Error (578, method.Location,
-                                             "Conditional not valid on `" + member.Name + "' " +
-                                             "because its return type is not void");
-                               return false;
-                       }
-
-                       if ((modifiers & Modifiers.OVERRIDE) != 0) {
-                               Report.Error (243, method.Location,
-                                             "Conditional not valid on `" + member.Name + "' " +
-                                             "because it is an override method");
-                               return false;
-                       }
-
-                       if (member.IsExplicitImpl) {
-                               Report.Error (577, method.Location,
-                                             "Conditional not valid on `" + member.Name + "' " +
-                                             "because it is an explicit interface implementation");
-                               return false;
-                       }
-
-                       if (IsImplementing) {
-                               Report.Error (623, method.Location,
-                                             "Conditional not valid on `" + member.Name + "' " +
-                                             "because it is an interface method");
-                               return false;
-                       }
-
-                       //
-                       // The likelyhood that the conditional will be more than 1 is very slim
-                       //
-                       if (conditionals == null)
-                               conditionals = condition;
-                       else if (conditionals is string){
-                               string s = (string) conditionals;
-                               conditionals = new ArrayList ();
-                               ((ArrayList)conditionals).Add (s);
-                       } else
-                               ((ArrayList)conditionals).Add (condition);
-
-                       return true;
-               }
+               //
+               public bool IsImplementing = false;
 
                //
-               // Checks whether this method should be ignored due to its Conditional attributes.
+               // Protected data.
                //
-               bool ShouldIgnore (Location loc)
-               {
-                       // When we're overriding a virtual method, we implicitly inherit the
-                       // Conditional attributes from our parent.
-                       if (member.ParentMethod != null) {
-                               TypeManager.MethodFlags flags = TypeManager.GetMethodFlags (
-                                       member.ParentMethod, loc);
-
-                               if ((flags & TypeManager.MethodFlags.ShouldIgnore) != 0)
-                                       return true;
-                       }
+               protected MemberBase member;
+               protected int modifiers;
+               protected MethodAttributes flags;
 
-                       if (conditionals != null){
-                               if (conditionals is string){
-                                       if (RootContext.AllDefines [conditionals] == null)
-                                               return true;
-                               } else {
-                                       foreach (string condition in (ArrayList) conditionals)
-                                       if (RootContext.AllDefines [condition] == null)
-                                               return true;
-                               }
+               MethodBuilder builder = null;
+               public MethodBuilder MethodBuilder {
+                       get {
+                               return builder;
                        }
-                       return false;
                }
 
-               //
-               // Returns the TypeManager.MethodFlags for this method.
-               // This emits an error 619 / warning 618 if the method is obsolete.
-               // In the former case, TypeManager.MethodFlags.IsObsoleteError is returned.
-               //
-               public virtual TypeManager.MethodFlags GetMethodFlags (Location loc)
+               public MethodData (MemberBase member, InternalParameters parameters,
+                                  int modifiers, MethodAttributes flags, IMethodData method)
                {
-                       TypeManager.MethodFlags flags = 0;
-
-                       if (obsolete != null) {
-                               if (obsolete_error) {
-                                       Report.Error (619, loc, "Method `" + member.Name +
-                                                     "' is obsolete: `" + obsolete + "'");
-                                       return TypeManager.MethodFlags.IsObsoleteError;
-                               } else
-                                       Report.Warning (618, loc, "Method `" + member.Name +
-                                                       "' is obsolete: `" + obsolete + "'");
-
-                               flags |= TypeManager.MethodFlags.IsObsolete;
-                       }
-
-                       if (ShouldIgnore (loc))
-                               flags |= TypeManager.MethodFlags.ShouldIgnore;
+                       this.member = member;
+                       this.ParameterInfo = parameters;
+                       this.modifiers = modifiers;
+                       this.flags = flags;
 
-                       return flags;
+                       this.method = method;
                }
 
                public bool Define (TypeContainer container)
                {
                        MethodInfo implementing = null;
-                       string prefix;
-
-                       if (method.OptAttributes != null)
-                               if (!ApplyAttributes (method.OptAttributes, is_method, container))
-                                       return false;
 
-                       if (member.IsExplicitImpl)
-                               prefix = member.InterfaceType.FullName + ".";
-                       else
-                               prefix = "";
+                       string method_name = method.MethodName;
+                       string name;
+                       if (member.IsExplicitImpl) {
+                               name = method_name.Remove (0, member.InterfaceType.FullName.Length + 1);
+                       } else {
+                               name = method_name;
+                       }
 
-                       string name = method.MethodName;
-                       string method_name = prefix + name;
                        Type[] ParameterTypes = method.ParameterTypes;
 
                        if (container.Pending != null){
@@ -3822,35 +4421,12 @@ namespace Mono.CSharp {
                                if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
                                        flags |= MethodAttributes.Final;
 
-                               // Get the method name from the explicit interface.
-                               if (member.InterfaceType != null) {
-                                       name = implementing.Name;
-                                       method_name = prefix + name;
-                               }
-
                                IsImplementing = true;
                        }
 
-                       //
-                       // Create the MethodBuilder for the method
-                       //
-                       if ((flags & MethodAttributes.PinvokeImpl) != 0) {
-                               if ((modifiers & Modifiers.STATIC) == 0) {
-                                       Report.Error (601, method.Location,
-                                                     "The DllImport attribute must be specified on " +
-                                                     "a method marked 'static' and 'extern'.");
-                                       return false;
-                               }
-                               
-                               EmitContext ec = method.CreateEmitContext (container, null);
-                               
-                               builder = dllimport_attribute.DefinePInvokeMethod (
-                                       ec, container.TypeBuilder, method_name, flags,
-                                       method.ReturnType, ParameterTypes);
-                       } else
-                               builder = container.TypeBuilder.DefineMethod (
-                                       method_name, flags, method.CallingConventions,
-                                       method.ReturnType, ParameterTypes);
+                       EmitContext ec = method.CreateEmitContext (container, null);
+
+                       DefineMethodBuilder (ec, container, method_name, ParameterTypes);
 
                        if (builder == null)
                                return false;
@@ -3865,7 +4441,7 @@ namespace Mono.CSharp {
                                if (member is Indexer) {
                                        container.Pending.ImplementIndexer (
                                                member.InterfaceType, builder, method.ReturnType,
-                                               ParameterTypes, true);
+                                               ParameterTypes, member.IsExplicitImpl);
                                } else
                                        container.Pending.ImplementMethod (
                                                member.InterfaceType, name, method.ReturnType,
@@ -3877,27 +4453,54 @@ namespace Mono.CSharp {
 
                        }
 
-                       if (!TypeManager.RegisterMethod (builder, ParameterInfo, ParameterTypes)) {
-                               Report.Error (111, method.Location,
-                                             "Class `" + container.Name +
-                                             "' already contains a definition with the " +
-                                             "same return value and parameter types as the " +
-                                             "'get' method of property `" + member.Name + "'");
-                               return false;
-                       }
-
-                       TypeManager.AddMethod (builder, this);
+                       TypeManager.RegisterMethod (builder, ParameterInfo, ParameterTypes);
+                       TypeManager.AddMethod (builder, method);
 
                        return true;
                }
 
+
+               /// <summary>
+               /// Create the MethodBuilder for the method 
+               /// </summary>
+               void DefineMethodBuilder (EmitContext ec, 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);
+                                       if (dllimport_attribute != null) {
+                                               flags |= MethodAttributes.PinvokeImpl;
+                                               builder = dllimport_attribute.DefinePInvokeMethod (
+                                                       ec, container.TypeBuilder, method_name, flags,
+                                                       method.ReturnType, ParameterTypes);
+
+                                               return;
+                                       }
+                               }
+
+                               // 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)) {
+                                       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 (container));
+                                       return;
+                               }
+                       }
+
+                       builder = container.TypeBuilder.DefineMethod (
+                               method_name, flags, method.CallingConventions,
+                               method.ReturnType, ParameterTypes);
+               }
+
                //
                // Emits the code
                // 
-               public void Emit (TypeContainer container, object kind)
+               public void Emit (TypeContainer container, Attributable kind)
                {
                        EmitContext ec;
-
                        if ((flags & MethodAttributes.PinvokeImpl) == 0)
                                ec = method.CreateEmitContext (container, builder.GetILGenerator ());
                        else
@@ -3907,31 +4510,19 @@ namespace Mono.CSharp {
                        Attributes OptAttributes = method.OptAttributes;
 
                        if (OptAttributes != null)
-                               Attribute.ApplyAttributes (ec, builder, kind, OptAttributes);
+                               OptAttributes.Emit (ec, kind);
 
                        if (member is MethodCore)
-                               MethodCore.LabelParameters (ec, MethodBuilder,
-                                                            ((MethodCore) member).Parameters,
-                                                            OptAttributes,
-                                                            loc);
+                               ((MethodCore) member).Parameters.LabelParameters (ec, MethodBuilder, loc);
 
-                       SymbolWriter sw = CodeGen.SymbolWriter;
                        Block block = method.Block;
                        
                        //
                        // abstract or extern methods have no bodies
                        //
                        if ((modifiers & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0){
-                               if (block == null) {
-                                       if ((sw != null) && ((modifiers & Modifiers.EXTERN) != 0) &&
-                                           !Location.IsNull (loc) &&
-                                           (method.Location.SymbolDocument != null)) {
-                                               sw.OpenMethod (container, MethodBuilder, loc, loc);
-                                               sw.CloseMethod ();
-                                       }
-
+                               if (block == null)
                                        return;
-                               }
 
                                //
                                // abstract or extern methods have no bodies.
@@ -3963,28 +4554,21 @@ namespace Mono.CSharp {
                                return;
                        }
 
+                       SourceMethod source = SourceMethod.Create (
+                               container, MethodBuilder, method.Block);
+
                        //
                        // Handle destructors specially
                        //
                        // FIXME: This code generates buggy code
                        //
-                       if ((sw != null) && !Location.IsNull (loc) &&
-                           !Location.IsNull (block.EndLocation) &&
-                           (loc.SymbolDocument != null)) {
-                               sw.OpenMethod (container, MethodBuilder, loc, block.EndLocation);
-
-                               if (member is Destructor)
-                                       EmitDestructor (ec, block);
-                               else
-                                       ec.EmitTopBlock (block, ParameterInfo, loc);
+                       if (member is Destructor)
+                               EmitDestructor (ec, block);
+                       else
+                               ec.EmitTopBlock (block, ParameterInfo, loc);
 
-                               sw.CloseMethod ();
-                       } else {
-                               if (member is Destructor)
-                                       EmitDestructor (ec, block);
-                               else
-                                       ec.EmitTopBlock (block, ParameterInfo, loc);
-                       }
+                       if (source != null)
+                               source.CloseMethod ();
                }
 
                void EmitDestructor (EmitContext ec, Block block)
@@ -4024,11 +4608,20 @@ namespace Mono.CSharp {
 
        public class Destructor : Method {
 
-               public Destructor (DeclSpace ds, Expression return_type, int mod, string name,
+               public Destructor (TypeContainer ds, Expression return_type, int mod, string name,
                                   Parameters parameters, Attributes attrs, Location l)
                        : base (ds, return_type, mod, false, name, parameters, attrs, l)
                { }
 
+               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 destructor, operator, or explicit interface implementation", GetSignatureForError ());
+                               return;
+                       }
+
+                       base.ApplyAttributeBuilder (a, cb);
+               }
        }
        
        abstract public class MemberBase : MemberCore {
@@ -4069,187 +4662,42 @@ namespace Mono.CSharp {
                //
                public Type InterfaceType = null;
 
-               //
-               // The method we're overriding if this is an override method.
-               //
-               protected MethodInfo parent_method = null;
-               public MethodInfo ParentMethod {
-                       get {
-                               return parent_method;
-                       }
-               }
-
                //
                // The constructor is only exposed to our children
                //
-               protected MemberBase (Expression type, int mod, int allowed_mod, int def_mod, string name,
+               protected MemberBase (TypeContainer parent, Expression type, int mod,
+                                     int allowed_mod, int def_mod, string name,
                                      Attributes attrs, Location loc)
-                       : base (name, attrs, loc)
+                       : base (parent, name, attrs, loc)
                {
                        explicit_mod_flags = mod;
                        Type = type;
                        ModFlags = Modifiers.Check (allowed_mod, mod, def_mod, loc);
-               }
-
-               protected virtual bool CheckBase (TypeContainer container)
-               {
-                       if ((container is Struct) || (RootContext.WarningLevel > 3)){
-                               if ((ModFlags & Modifiers.PROTECTED) != 0 && (container.ModFlags & Modifiers.SEALED) != 0){
-                                       if (container is Struct){
-                                               Report.Error (666, Location, "Protected member in struct declaration");
-                                               return false;
-                                       } else
-                                               Report.Warning (628, Location, "Member " + container.MakeName (Name) + " protected in sealed class");
-                               }
-                       }
-                       return true;
-               }
 
-               protected void WarningNotHiding (TypeContainer parent)
-               {
-                       Report.Warning (
-                               109, Location,
-                               "The member " + parent.MakeName (Name) + " does not hide an " +
-                               "inherited member.  The keyword new is not required");
-                                                          
+                       // Check for explicit interface implementation
+                       int pos = Name.LastIndexOf ('.');
+                       if (pos != -1) {
+                               ExplicitInterfaceName = Name.Substring (0, pos);
+                               ShortName = Name.Substring (pos + 1);
+                               IsExplicitImpl = true;
+                       } else
+                               ShortName = Name;
                }
 
-               void Error_CannotChangeAccessModifiers (TypeContainer parent, MethodInfo parent_method,
-                                                       string name)
-               {
-                       //
-                       // FIXME: report the old/new permissions?
-                       //
-                       Report.Error (
-                               507, Location, parent.MakeName (Name) +
-                               ": can't change the access modifiers when overriding inherited " +
-                               "member `" + name + "'");
-               }
-               
-               //
-               // Performs various checks on the MethodInfo `mb' regarding the modifier flags
-               // that have been defined.
-               //
-               // `name' is the user visible name for reporting errors (this is used to
-               // provide the right name regarding method names and properties)
-               //
-               protected bool CheckMethodAgainstBase (TypeContainer parent, MethodAttributes my_attrs,
-                                                      MethodInfo mb, string name)
+               protected virtual bool CheckBase ()
                {
-                       bool ok = true;
-                       
-                       if ((ModFlags & Modifiers.OVERRIDE) != 0){
-                               if (!(mb.IsAbstract || mb.IsVirtual)){
-                                       Report.Error (
-                                               506, Location, parent.MakeName (Name) +
-                                               ": cannot override inherited member `" +
-                                               name + "' because it is not " +
-                                               "virtual, abstract or override");
-                                       ok = false;
-                               }
-                               
-                               // Now we check that the overriden method is not final
-                               
-                               if (mb.IsFinal) {
-                                       // This happens when implementing interface methods.
-                                       if (mb.IsHideBySig && mb.IsVirtual) {
-                                               Report.Error (
-                                                       506, Location, parent.MakeName (Name) +
-                                                       ": cannot override inherited member `" +
-                                                       name + "' because it is not " +
-                                                       "virtual, abstract or override");
-                                       } else
-                                               Report.Error (239, Location, parent.MakeName (Name) + " : cannot " +
-                                                             "override inherited member `" + name +
-                                                             "' because it is sealed.");
-                                       ok = false;
-                               }
-                               //
-                               // Check that the permissions are not being changed
-                               //
-                               MethodAttributes thisp = my_attrs & MethodAttributes.MemberAccessMask;
-                               MethodAttributes parentp = mb.Attributes & MethodAttributes.MemberAccessMask;
-
-                               //
-                               // special case for "protected internal"
-                               //
-
-                               if ((parentp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
-                                       //
-                                       // when overriding protected internal, the method can be declared
-                                       // protected internal only within the same assembly
-                                       //
-
-                                       if ((thisp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
-                                               if (parent.TypeBuilder.Assembly != mb.DeclaringType.Assembly){
-                                                       //
-                                                       // assemblies differ - report an error
-                                                       //
-                                                       
-                                                       Error_CannotChangeAccessModifiers (parent, mb, name);
-                                                   ok = false;
-                                               } else if (thisp != parentp) {
-                                                       //
-                                                       // same assembly, but other attributes differ - report an error
-                                                       //
-                                                       
-                                                       Error_CannotChangeAccessModifiers (parent, mb, name);
-                                                       ok = false;
-                                               };
-                                       } else if ((thisp & MethodAttributes.Family) != MethodAttributes.Family) {
-                                               //
-                                               // if it's not "protected internal", it must be "protected"
-                                               //
-
-                                               Error_CannotChangeAccessModifiers (parent, mb, name);
-                                               ok = false;
-                                       } else if (parent.TypeBuilder.Assembly == mb.DeclaringType.Assembly) {
-                                               //
-                                               // protected within the same assembly - an error
-                                               //
-                                               Error_CannotChangeAccessModifiers (parent, mb, name);
-                                               ok = false;
-                                       } else if ((thisp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem)) != 
-                                                  (parentp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem))) {
-                                               //
-                                               // protected ok, but other attributes differ - report an error
-                                               //
-                                               Error_CannotChangeAccessModifiers (parent, mb, name);
-                                               ok = false;
-                                       }
-                               } else {
-                                       if (thisp != parentp){
-                                               Error_CannotChangeAccessModifiers (parent, mb, name);
-                                               ok = false;
-                                       }
-                               }
-                       }
-
-                       if (mb.IsVirtual || mb.IsAbstract){
-                               if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0){
-                                       if (Name != "Finalize"){
-                                               Report.Warning (
-                                                       114, 2, Location, parent.MakeName (Name) + 
-                                                       " hides inherited member `" + name +
-                                                       "'.  To make the current member override that " +
-                                                       "implementation, add the override keyword, " +
-                                                       "otherwise use the new keyword");
-                                               ModFlags |= Modifiers.NEW;
-                                       }
-                               }
-                       } else {
-                               if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0){
-                                       if (Name != "Finalize"){
-                                               Report.Warning (
-                                                       108, 1, Location, "The keyword new is required on " +
-                                                       parent.MakeName (Name) + " because it hides " +
-                                                       "inherited member `" + name + "'");
-                                               ModFlags |= Modifiers.NEW;
-                                       }
-                               }
-                       }
-
-                       return ok;
+                       if ((ModFlags & Modifiers.PROTECTED) != 0 && Parent.Kind == Kind.Struct) {
+                               Report.Error (666, Location, "Protected member in struct declaration");
+                               return false;
+                       }
+   
+                       if ((RootContext.WarningLevel >= 4) &&
+                           ((Parent.ModFlags & Modifiers.SEALED) != 0) &&
+                           ((ModFlags & Modifiers.PROTECTED) != 0) &&
+                           ((ModFlags & Modifiers.OVERRIDE) == 0) && (Name != "Finalize")) {
+                               Report.Warning (628, Location, "'{0}': new protected member declared in sealed class", GetSignatureForError (Parent));
+                       }
+                       return true;
                }
 
                protected virtual bool CheckParameters (DeclSpace ds, Type [] parameters)
@@ -4295,7 +4743,7 @@ namespace Mono.CSharp {
                        return !error;
                }
 
-               protected virtual bool DoDefine (TypeContainer container)
+               protected virtual bool DoDefine ()
                {
                        if (Name == null)
                                Name = "this";
@@ -4303,7 +4751,7 @@ namespace Mono.CSharp {
                        if (IsInterface) {
                                ModFlags = Modifiers.PUBLIC |
                                        Modifiers.ABSTRACT |
-                                       Modifiers.VIRTUAL;
+                                       Modifiers.VIRTUAL | (ModFlags & Modifiers.UNSAFE) | (ModFlags & Modifiers.NEW);
 
                                flags = MethodAttributes.Public |
                                        MethodAttributes.Abstract |
@@ -4311,18 +4759,18 @@ namespace Mono.CSharp {
                                        MethodAttributes.NewSlot |
                                        MethodAttributes.Virtual;
                        } else {
-                               if (!container.MethodModifiersValid (ModFlags, Name, Location))
+                               if (!Parent.MethodModifiersValid (ModFlags, Name, Location))
                                        return false;
 
                                flags = Modifiers.MethodAttr (ModFlags);
                        }
 
                        // Lookup Type, verify validity
-                       MemberType = container.ResolveType (Type, false, Location);
+                       MemberType = Parent.ResolveType (Type, false, Location);
                        if (MemberType == null)
                                return false;
 
-                       if ((container.ModFlags & Modifiers.SEALED) != 0){
+                       if ((Parent.ModFlags & Modifiers.SEALED) != 0){
                                if ((ModFlags & (Modifiers.VIRTUAL|Modifiers.ABSTRACT)) != 0){
                                        Report.Error (549, Location, "Virtual method can not be contained in sealed class");
                                        return false;
@@ -4330,7 +4778,7 @@ namespace Mono.CSharp {
                        }
                        
                        // verify accessibility
-                       if (!container.AsAccessible (MemberType, ModFlags)) {
+                       if (!Parent.AsAccessible (MemberType, ModFlags)) {
                                if (this is Property)
                                        Report.Error (53, Location,
                                                      "Inconsistent accessibility: property type `" +
@@ -4360,23 +4808,23 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       if (MemberType.IsPointer && !UnsafeOK (container))
+                       if (MemberType.IsPointer && !UnsafeOK (Parent))
                                return false;
-                       
-                       //
-                       // Check for explicit interface implementation
-                       //
-                       if ((ExplicitInterfaceName == null) && (Name.IndexOf ('.') != -1)){
-                               int pos = Name.LastIndexOf ('.');
 
-                               ExplicitInterfaceName = Name.Substring (0, pos);
-                               ShortName = Name.Substring (pos + 1);
-                       } else
-                               ShortName = Name;
+                       if (IsExplicitImpl) {
+                               int errors = Report.Errors;
+
+                               InterfaceType  = Parent.FindType (
+                                       Location, ExplicitInterfaceName);
+
+                               if (Report.Errors != errors)
+                                       return false;
+                               else if (InterfaceType == null) {
+                                       Report.Error (246, Location, "Can not find type `{0}'",
+                                               ExplicitInterfaceName);
+                                       return false;
+                               }
 
-                       if (ExplicitInterfaceName != null) {
-                               InterfaceType  = RootContext.LookupType (
-                                       container, ExplicitInterfaceName, false, Location);
                                if (InterfaceType == null)
                                        return false;
 
@@ -4386,23 +4834,34 @@ namespace Mono.CSharp {
                                }
 
                                // Compute the full name that we need to export.
-                               Name = InterfaceType.FullName + "." + ShortName;
+                               if (InterfaceType.FullName != ExplicitInterfaceName) {
+                                       ExplicitInterfaceName = InterfaceType.FullName;
+                                       UpdateMemberName ();
+                               }
                                
-                               if (!container.VerifyImplements (InterfaceType, ShortName, Name, Location))
+                               if (!Parent.VerifyImplements (InterfaceType, ShortName, Name, Location))
                                        return false;
                                
                                Modifiers.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location);
                                
-                               IsExplicitImpl = true;
-                       } else
-                               IsExplicitImpl = false;
-
+                       }
                        return true;
                }
 
-               protected override bool IsIdentifierClsCompliant (DeclSpace ds)
+               /// <summary>
+               /// The name of the member can be changed during definition (see IndexerName attribute)
+               /// </summary>
+               protected virtual void UpdateMemberName ()
+               {
+                       if (IsExplicitImpl)
+                               Name = String.Concat (ExplicitInterfaceName, ".", ShortName);
+                       else
+                               Name = ShortName;
+               }
+
+               public override string GetSignatureForError (TypeContainer tc)
                {
-                       return IsIdentifierAndParamClsCompliant (ds, Name, null, null);
+                       return String.Concat (tc.Name, '.', base.GetSignatureForError (tc));
                }
 
                protected override bool VerifyClsCompliance(DeclSpace ds)
@@ -4412,12 +4871,15 @@ namespace Mono.CSharp {
                        }
 
                        if (IsInterface && HasClsCompliantAttribute && ds.IsClsCompliaceRequired (ds)) {
-                               Report.Error_T (3010, Location, GetSignatureForError ());
+                               Report.Error (3010, Location, "'{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
                        }
                        return false;
                }
 
-
+               protected override void VerifyObsoleteAttribute()
+               {
+                       CheckUsageOfObsoleteAttribute (MemberType);
+               }
        }
 
        //
@@ -4431,16 +4893,47 @@ namespace Mono.CSharp {
                [Flags]
                public enum Status : byte { ASSIGNED = 1, USED = 2 }
 
+               static string[] attribute_targets = new string [] { "field" };
+
+               /// <summary>
+               ///  Symbol with same name in parent class/struct
+               /// </summary>
+               public MemberInfo conflict_symbol;
+
                //
                // The constructor is only exposed to our children
                //
-               protected FieldBase (Expression type, int mod, int allowed_mod, string name,
-                                    object init, Attributes attrs, Location loc)
-                       : base (type, mod, allowed_mod, Modifiers.PRIVATE, name, attrs, loc)
+               protected FieldBase (TypeContainer parent, Expression type, int mod,
+                                    int allowed_mod, string name, object init,
+                                    Attributes attrs, Location loc)
+                       : base (parent, type, mod, allowed_mod, Modifiers.PRIVATE,
+                               name, attrs, loc)
                {
                        this.init = init;
                }
 
+               public override AttributeTargets AttributeTargets {
+                       get {
+                               return AttributeTargets.Field;
+                       }
+               }
+
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               {
+                       if (a.Type == TypeManager.marshal_as_attr_type) {
+                               UnmanagedMarshal marshal = a.GetMarshal ();
+                               if (marshal != null) {
+                                       FieldBuilder.SetMarshal (marshal);
+                                       return;
+                               }
+                               Report.Warning (-24, a.Location, "The Microsoft Runtime cannot set this marshal info. Please use the Mono runtime instead.");
+                               return;
+                       }
+
+                       
+                       FieldBuilder.SetCustomAttribute (cb);
+               }
+
                //
                // Whether this field has an initializer.
                //
@@ -4479,9 +4972,34 @@ namespace Mono.CSharp {
                        return init_expr;
                }
 
-               protected override bool DoDefine (TypeContainer container)
+               protected override bool CheckBase ()
+               {
+                       if (!base.CheckBase ())
+                               return false;
+                       // TODO: Implement
+                       if (IsInterface)
+                               return true;
+                       conflict_symbol = Parent.FindMemberWithSameName (Name, false);
+                       if (conflict_symbol == null) {
+                               if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
+                                       Report.Warning (109, Location, "The member '{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError (Parent));
+                               }
+                               return true;
+                       }
+                       if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0) {
+                               Report.SymbolRelatedToPreviousError (conflict_symbol);
+                               Report.Warning (108, Location, "The keyword new is required on '{0}' because it hides inherited member", GetSignatureForError (Parent));
+                       }
+                       return true;
+               }
+
+               protected override bool DoDefine ()
                {
-                       if (!base.DoDefine (container))
+                       if (!base.DoDefine ())
                                return false;
 
                        if (MemberType == TypeManager.void_type) {
@@ -4490,14 +5008,28 @@ namespace Mono.CSharp {
                                return false;
                        }
 
+                       if (MemberType == TypeManager.arg_iterator_type || MemberType == TypeManager.typed_reference_type) {
+                               Report.Error (610, Location, "Field or property cannot be of type '{0}'", TypeManager.CSharpName (MemberType));
+                               return false;
+                       }
+
                        return true;
                }
 
                public override string GetSignatureForError ()
                {
+                       if (FieldBuilder == null) {
+                               return base.GetSignatureForError (Parent);
+                       }
                        return TypeManager.GetFullNameSignature (FieldBuilder);
                }
 
+               public override string[] ValidAttributeTargets {
+                       get {
+                               return attribute_targets;
+                       }
+               }
+
                protected override bool VerifyClsCompliance (DeclSpace ds)
                {
                        if (!base.VerifyClsCompliance (ds))
@@ -4508,7 +5040,7 @@ namespace Mono.CSharp {
                        }
 
                        if (!AttributeTester.IsClsCompliant (FieldBuilder.FieldType)) {
-                               Report.Error_T (3003, Location, GetSignatureForError ());
+                               Report.Error (3003, Location, "Type of '{0}' is not CLS-compliant", GetSignatureForError ());
                        }
                        return true;
                }
@@ -4538,51 +5070,35 @@ namespace Mono.CSharp {
                        Modifiers.UNSAFE |
                        Modifiers.READONLY;
 
-               public Field (Expression type, int mod, string name, Object expr_or_array_init,
-                             Attributes attrs, Location loc)
-                       : base (type, mod, AllowedModifiers, name, expr_or_array_init, attrs, loc)
-               {
-               }
-
-               public override void ApplyAttributeBuilder (object builder, Attribute a, CustomAttributeBuilder cb)
+               public Field (TypeContainer parent, Expression type, int mod, string name,
+                             Object expr_or_array_init, Attributes attrs, Location loc)
+                       : base (parent, type, mod, AllowedModifiers, name, expr_or_array_init,
+                               attrs, loc)
                {
-                       FieldBuilder fb = builder as FieldBuilder;
-
-                       if (a.Type == TypeManager.marshal_as_attr_type) {
-                               UnmanagedMarshal marshal = a.GetMarshal ();
-                               if (marshal == null)
-                                       Report.Warning (-24, a.Location,
-                                                       "The Microsoft Runtime cannot set this marshal info. " +
-                                                       "Please use the Mono runtime instead.");
-                               else
-                                       fb.SetMarshal (marshal);
-                       }
-                       else
-                               fb.SetCustomAttribute (cb);
                }
 
-               public override bool Define (TypeContainer container)
+               public override bool Define ()
                {
-                       Type t = container.ResolveType (Type, false, Location);
+                       MemberType = Parent.ResolveType (Type, false, Location);
                        
-                       if (t == null)
+                       if (MemberType == null)
                                return false;
 
-                       CheckBase (container);
+                       CheckBase ();
                        
-                       if (!container.AsAccessible (t, ModFlags)) {
+                       if (!Parent.AsAccessible (MemberType, ModFlags)) {
                                Report.Error (52, Location,
                                              "Inconsistent accessibility: field type `" +
-                                             TypeManager.CSharpName (t) + "' is less " +
+                                             TypeManager.CSharpName (MemberType) + "' is less " +
                                              "accessible than field `" + Name + "'");
                                return false;
                        }
 
-                       if (t.IsPointer && !UnsafeOK (container))
+                       if (MemberType.IsPointer && !UnsafeOK (Parent))
                                return false;
                        
                        if (RootContext.WarningLevel > 1){
-                               Type ptype = container.TypeBuilder.BaseType;
+                               Type ptype = Parent.TypeBuilder.BaseType;
 
                                // ptype is only null for System.Object while compiling corlib.
                                if (ptype != null){
@@ -4595,11 +5111,11 @@ namespace Mono.CSharp {
                        }
 
                        if ((ModFlags & Modifiers.VOLATILE) != 0){
-                               if (!t.IsClass){
-                                       Type vt = t;
+                               if (!MemberType.IsClass){
+                                       Type vt = MemberType;
                                        
                                        if (TypeManager.IsEnumType (vt))
-                                               vt = TypeManager.EnumToUnderlying (t);
+                                               vt = TypeManager.EnumToUnderlying (MemberType);
 
                                        if (!((vt == TypeManager.bool_type) ||
                                              (vt == TypeManager.sbyte_type) ||
@@ -4612,7 +5128,7 @@ namespace Mono.CSharp {
                                              (vt == TypeManager.float_type) ||
                                              (!vt.IsValueType))){
                                                Report.Error (
-                                                       677, Location, container.MakeName (Name) +
+                                                       677, Location, Parent.MakeName (Name) +
                                                        " A volatile field can not be of type `" +
                                                        TypeManager.CSharpName (vt) + "'");
                                                return false;
@@ -4629,18 +5145,18 @@ namespace Mono.CSharp {
 
                        FieldAttributes fa = Modifiers.FieldAttr (ModFlags);
 
-                       if (container is Struct && 
+                       if (Parent.Kind == Kind.Struct && 
                            ((fa & FieldAttributes.Static) == 0) &&
-                           t == container.TypeBuilder &&
-                           !TypeManager.IsBuiltinType (t)){
-                               Report.Error (523, Location, "Struct member `" + container.Name + "." + Name + 
+                           MemberType == Parent.TypeBuilder &&
+                           !TypeManager.IsBuiltinType (MemberType)){
+                               Report.Error (523, Location, "Struct member `" + Parent.Name + "." + Name + 
                                              "' causes a cycle in the structure layout");
                                return false;
                        }
 
                        try {
-                               FieldBuilder = container.TypeBuilder.DefineField (
-                                       Name, t, Modifiers.FieldAttr (ModFlags));
+                               FieldBuilder = Parent.TypeBuilder.DefineField (
+                                       Name, MemberType, Modifiers.FieldAttr (ModFlags));
 
                                TypeManager.RegisterFieldBase (FieldBuilder, this);
                        }
@@ -4652,41 +5168,191 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               public override void Emit (TypeContainer tc)
+               public override void Emit ()
                {
                        if (OptAttributes != null) {
-                               EmitContext ec = new EmitContext (tc, Location, null, FieldBuilder.FieldType, ModFlags);
-                               Attribute.ApplyAttributes (ec, FieldBuilder, this, OptAttributes);
+                               EmitContext ec = new EmitContext (
+                                       Parent, Location, null, FieldBuilder.FieldType,
+                                       ModFlags);
+                               OptAttributes.Emit (ec, this);
                        }
 
-                       base.Emit (tc);
+                       base.Emit ();
                }
        }
 
        //
        // `set' and `get' accessors are represented with an Accessor.
        // 
-       public class Accessor : Attributable {
+       public class Accessor {
                //
                // Null if the accessor is empty, or a Block if not
                //
                public Block Block;
+               public Attributes Attributes;
+               public Location Location;
                
-               public Accessor (Block b, Attributes attrs)
-                       : base (attrs)
+               public Accessor (Block b, Attributes attrs, Location loc)
                {
                        Block = b;
+                       Attributes = attrs;
+                       Location = loc;
+               }
+       }
+
+
+       // Ooouh Martin, templates are missing here.
+       // When it will be possible move here a lot of child code and template method type.
+       public abstract class AbstractPropertyEventMethod: MemberCore, IMethodData {
+               protected MethodData method_data;
+               protected Block block;
+
+               // The accessor are created event if they are not wanted.
+               // But we need them because their names are reserved.
+               // Field says whether accessor will be emited or not
+               public readonly bool IsDummy;
+
+               ReturnParameter return_attributes;
+
+               public AbstractPropertyEventMethod (string name, Location loc):
+                       base (null, name, null, loc)
+               {
+                       IsDummy = true;
+               }
+
+               public AbstractPropertyEventMethod (Accessor accessor, string name):
+                       base (null, name, accessor.Attributes, accessor.Location)
+               {
+                       this.block = accessor.Block;
+               }
+
+               protected static string SetupName (string prefix, MemberBase member)
+               {
+                       if (member.IsExplicitImpl) {
+                               return String.Concat (member.ExplicitInterfaceName, '.', prefix, member.ShortName);
+                       }
+
+                       return String.Concat (prefix, member.Name);
+               }
+
+               public void UpdateName (MemberBase member)
+               {
+                       int start = Name.LastIndexOf ('.');
+                       start++;
+                       string prefix = Name.Substring (start, (Name.LastIndexOf ('_') + 1) - start);
+                       base.Name = SetupName (prefix, member);
+               }
+
+               #region IMethodData Members
+
+               public Block Block {
+                       get {
+                               return block;
+                       }
+
+                       set {
+                               block = value;
+                       }
+               }
+
+               public CallingConventions CallingConventions {
+                       get {
+                               return CallingConventions.Standard;
+                       }
+               }
+
+               public bool IsExcluded (EmitContext ec)
+               {
+                       return false;
+               }
+
+               public string MethodName {
+                       get {
+                               return Name;
+                       }
+               }
+
+               public abstract ObsoleteAttribute GetObsoleteAttribute ();
+               public abstract Type[] ParameterTypes { get; }
+               public abstract Type ReturnType { get; }
+               public abstract EmitContext CreateEmitContext(TypeContainer tc, ILGenerator ig);
+
+               #endregion
+
+               public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
+               {
+                       if (a.Type == TypeManager.cls_compliant_attribute_type || a.Type == TypeManager.obsolete_attribute_type ||
+                                       a.Type == TypeManager.conditional_attribute_type) {
+                               Report.Error (1667, a.Location, "'{0}' is not valid on property or event accessors. It is valid on '{1}' declarations only", TypeManager.CSharpName (a.Type), a.GetValidTargets ());
+                               return;
+                       }
+
+                       if (a.Target == AttributeTargets.Method) {
+                               method_data.MethodBuilder.SetCustomAttribute (cb);
+                               return;
+                       }
+
+                       if (a.Target == AttributeTargets.ReturnValue) {
+                               if (return_attributes == null)
+                                       return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
+
+                               return_attributes.ApplyAttributeBuilder (a, cb);
+                               return;
+                       }
+
+                       ApplyToExtraTarget (a, cb);
+               }
+
+               virtual protected void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb)
+               {
+                       System.Diagnostics.Debug.Fail ("You forgot to define special attribute target handling");
+               }
+
+               public override bool Define()
+               {
+                       return false;
+               }
+
+               public virtual void Emit (TypeContainer container)
+               {
+                       method_data.Emit (container, this);
+                       block = null;
+               }
+
+               public override bool IsClsCompliaceRequired(DeclSpace ds)
+               {
+                       return false;
                }
 
-               public override void ApplyAttributeBuilder (object builder, Attribute a, CustomAttributeBuilder cb)
+               public bool IsDuplicateImplementation (MethodCore method)
                {
-                       MethodBuilder mb = builder as MethodBuilder;
+                       if (Name != method.Name)
+                               return false;
+
+                       Type[] param_types = method.ParameterTypes;
+
+                       if (param_types.Length != ParameterTypes.Length)
+                               return false;
+
+                       for (int i = 0; i < param_types.Length; i++)
+                               if (param_types [i] != ParameterTypes [i])
+                                       return false;
 
-                       if (a.Type == TypeManager.methodimpl_attr_type && a.IsInternalCall)
-                               mb.SetImplementationFlags (MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
-                       else if (a.Type != TypeManager.dllimport_type)
-                               mb.SetCustomAttribute (cb);
+                       Report.SymbolRelatedToPreviousError (method);
+                       Report.Error (111, Location, TypeContainer.Error111, method.Parent.GetSignatureForError (), Name);
+                       return true;
+               }
+
+               public new Location Location { 
+                       get {
+                               return base.Location;
+                       }
+               }
+
+               protected override void VerifyObsoleteAttribute()
+               {
                }
+
        }
 
        //
@@ -4697,14 +5363,21 @@ namespace Mono.CSharp {
 
                public class GetMethod: PropertyMethod
                {
+                       static string[] attribute_targets = new string [] { "method", "return" };
+
+                       public GetMethod (MethodCore method):
+                               base (method, "get_")
+                       {
+                       }
+
                        public GetMethod (MethodCore method, Accessor accessor):
-                               base (method, accessor)
+                               base (method, accessor, "get_")
                        {
                        }
 
                        public override MethodBuilder Define(TypeContainer container)
                        {
-                               method_data = new MethodData (method, method.ParameterInfo, method.ModFlags, method.flags, false, this);
+                               method_data = new MethodData (method, method.ParameterInfo, method.ModFlags, method.flags, this);
 
                                if (!method_data.Define (container))
                                        return null;
@@ -4712,10 +5385,9 @@ namespace Mono.CSharp {
                                return method_data.MethodBuilder;
                        }
 
-                       public override string MethodName {
-                               get {
-                                       return "get_" + method.ShortName;
-                               }
+                       public override string GetSignatureForError (TypeContainer tc)
+                       {
+                               return String.Concat (base.GetSignatureForError (tc), ".get");
                        }
 
                        public override Type ReturnType {
@@ -4723,14 +5395,42 @@ namespace Mono.CSharp {
                                        return method.MemberType;
                                }
                        }
+
+                       public override string[] ValidAttributeTargets {
+                               get {
+                                       return attribute_targets;
+                               }
+                       }
                }
 
                public class SetMethod: PropertyMethod {
+
+                       static string[] attribute_targets = new string [] { "method", "param", "return" };
+                       ImplicitParameter param_attr;
+
+                       public SetMethod (MethodCore method):
+                               base (method, "set_")
+                       {
+                       }
+
                        public SetMethod (MethodCore method, Accessor accessor):
-                               base (method, accessor)
+                               base (method, accessor, "set_")
                        {
                        }
 
+                       protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
+                       {
+                               if (a.Target == AttributeTargets.Parameter) {
+                                       if (param_attr == null)
+                                               param_attr = new ImplicitParameter (method_data.MethodBuilder);
+
+                                       param_attr.ApplyAttributeBuilder (a, cb);
+                                       return;
+                               }
+
+                               base.ApplyAttributeBuilder (a, cb);
+                       }
+
                        protected virtual InternalParameters GetParameterInfo (TypeContainer container)
                        {
                                Parameter [] parms = new Parameter [1];
@@ -4741,7 +5441,7 @@ namespace Mono.CSharp {
 
                        public override MethodBuilder Define(TypeContainer container)
                        {
-                               method_data = new MethodData (method, GetParameterInfo (container), method.ModFlags, method.flags, false, this);
+                               method_data = new MethodData (method, GetParameterInfo (container), method.ModFlags, method.flags, this);
 
                                if (!method_data.Define (container))
                                        return null;
@@ -4749,10 +5449,9 @@ namespace Mono.CSharp {
                                return method_data.MethodBuilder;
                        }
 
-                       public override string MethodName {
-                               get {
-                                       return "set_" + method.ShortName;
-                               }
+                       public override string GetSignatureForError (TypeContainer tc)
+                       {
+                               return String.Concat (base.GetSignatureForError (tc), ".set");
                        }
 
                        public override Type[] ParameterTypes {
@@ -4766,76 +5465,75 @@ namespace Mono.CSharp {
                                        return TypeManager.void_type;
                                }
                        }
+
+                       public override string[] ValidAttributeTargets {
+                               get {
+                                       return attribute_targets;
+                               }
+                       }
                }
 
+               static string[] attribute_targets = new string [] { "property" };
 
-               public abstract class PropertyMethod: IMethodData {
+               public abstract class PropertyMethod: AbstractPropertyEventMethod
+               {
                        protected readonly MethodCore method;
-                       public readonly Accessor Accessor;
-                       protected MethodData method_data;
 
-                       public PropertyMethod (MethodCore method, Accessor accessor)
+                       public PropertyMethod (MethodCore method, string prefix):
+                               base (SetupName (prefix, method), method.Location)
                        {
                                this.method = method;
-                               this.Accessor = accessor;
-                       }
-
-                       public InternalParameters ParameterInfo {
-                               get {
-                                       return method_data.ParameterInfo;
-                               }
                        }
 
-                       #region IMethodData Members
-
-                       public Block Block {
-                               get {
-                                       return Accessor.Block;
-                               }
+                       public PropertyMethod (MethodCore method, Accessor accessor, string prefix):
+                               base (accessor, SetupName (prefix, method))
+                       {
+                               this.method = method;
                        }
 
-                       public CallingConventions CallingConventions {
+                       public override AttributeTargets AttributeTargets {
                                get {
-                                       return CallingConventions.Standard;
+                                       return AttributeTargets.Method;
                                }
                        }
 
-                       public abstract MethodBuilder Define (TypeContainer container);
-
-                       public void Emit (TypeContainer container)
+                       public override bool IsClsCompliaceRequired(DeclSpace ds)
                        {
-                               method_data.Emit (container, Accessor);
-                               Accessor.Block = null;
+                               return method.IsClsCompliaceRequired (ds);
                        }
 
-                       public Attributes OptAttributes {
+                       public InternalParameters ParameterInfo 
+                       {
                                get {
-                                       return Accessor.OptAttributes;
+                                       return method_data.ParameterInfo;
                                }
                        }
 
-                       public virtual Type[] ParameterTypes {
+                       public abstract MethodBuilder Define (TypeContainer container);
+
+                       public override Type[] ParameterTypes {
                                get {
                                        return TypeManager.NoTypes;
                                }
                        }
 
-                       public abstract Type ReturnType { get; }
-
-                       public Location Location {
-                               get {
-                                       return method.Location;
-                               }
+                       public override EmitContext CreateEmitContext (TypeContainer tc,
+                                                                      ILGenerator ig)
+                       {
+                               return new EmitContext (
+                                       tc, method.Parent, method.Location, ig, ReturnType,
+                                       method.ModFlags, false);
                        }
 
-                       public abstract string MethodName { get; }
-
-                       public EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
+                       public override ObsoleteAttribute GetObsoleteAttribute ()
                        {
-                               return new EmitContext (tc, method.ds, method.Location, ig, ReturnType, method.ModFlags, false);
+                               return method.GetObsoleteAttribute (method.Parent);
                        }
 
-                       #endregion
+                       public override string GetSignatureForError (TypeContainer tc)
+                       {
+                               return String.Concat (tc.Name, '.', method.Name);
+                       }
                }
 
 
@@ -4845,7 +5543,7 @@ namespace Mono.CSharp {
 
                protected EmitContext ec;
 
-               public PropertyBase (DeclSpace ds, Expression type, int mod_flags,
+               public PropertyBase (TypeContainer ds, Expression type, int mod_flags,
                                     int allowed_mod, bool is_iface, string name,
                                     Parameters parameters, Attributes attrs,
                                     Location loc)
@@ -4854,194 +5552,131 @@ namespace Mono.CSharp {
                {
                }
 
-               public override void ApplyAttributeBuilder (object builder, Attribute a, CustomAttributeBuilder cb)
-               {
-                       if (builder is PropertyBuilder) 
-                               ((PropertyBuilder) builder).SetCustomAttribute (cb);
-                       //
-                       // This is for the case we are setting attributes on
-                       // the get and set accessors
-                       //
-                       else if (builder is MethodBuilder)
-                               ((MethodBuilder) builder).SetCustomAttribute (cb);
-               }
-
-               protected override bool DoDefine (TypeContainer container)
-               {
-                       if (!base.DoDefine (container))
-                               return false;
-
-                       ec = new EmitContext (container, Location, null, MemberType, ModFlags);
-
-                       return true;
-               }
-
-               public override string GetSignatureForError()
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
                {
-                       return TypeManager.CSharpSignature (PropertyBuilder, false);
+                       PropertyBuilder.SetCustomAttribute (cb);
                }
 
-               protected virtual string RealMethodName {
+               public override AttributeTargets AttributeTargets {
                        get {
-                               return Name;
+                               return AttributeTargets.Property;
                        }
                }
 
-               protected override bool IsIdentifierClsCompliant (DeclSpace ds)
+               protected override bool DoDefine ()
                {
-                       if (!IsIdentifierAndParamClsCompliant (ds, RealMethodName, null, null))
+                       if (!base.DoDefine ())
                                return false;
 
-                       if (Get != null && !IsIdentifierAndParamClsCompliant (ds, "get_" + RealMethodName, null, null))
+                       if (MemberType == TypeManager.arg_iterator_type || MemberType == TypeManager.typed_reference_type) {
+                               Report.Error (610, Location, "Field or property cannot be of type '{0}'", TypeManager.CSharpName (MemberType));
                                return false;
+                       }
 
-                       if (Set != null && !IsIdentifierAndParamClsCompliant (ds, "set_" + RealMethodName, null, null))
+                       if (MemberType.IsAbstract && MemberType.IsSealed) {
+                               Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
                                return false;
+                       }
 
+                       ec = new EmitContext (Parent, Location, null, MemberType, ModFlags);
                        return true;
                }
 
-
-               //
-               // Checks our base implementation if any
-               //
-               protected override bool CheckBase (TypeContainer container)
+               public override string GetSignatureForError()
                {
-                       base.CheckBase (container);
-                       
-                       // Check whether arguments were correct.
-                       if (!DoDefineParameters ())
-                               return false;
+                       if (PropertyBuilder == null)
+                               return GetSignatureForError (Parent);
 
-                       if (IsExplicitImpl)
-                               return true;
+                       return TypeManager.CSharpSignature (PropertyBuilder, false);
+               }
 
-                       //
-                       // Check in our class for dups
-                       //
-                       ArrayList ar = container.Properties;
+
+               protected override bool CheckForDuplications ()
+               {
+                       ArrayList ar = Parent.Indexers;
                        if (ar != null) {
                                int arLen = ar.Count;
                                        
                                for (int i = 0; i < arLen; i++) {
-                                       Property m = (Property) ar [i];
-                                       if (IsDuplicateImplementation (container, m))
+                                       Indexer m = (Indexer) ar [i];
+                                       if (IsDuplicateImplementation (m))
                                                return false;
                                }
                        }
 
-                       if (IsInterface)
-                               return true;
-
-                       string report_name;
-                       MethodSignature ms, base_ms;
-                       if (this is Indexer) {
-                               string name, base_name;
-
-                               report_name = "this";
-                               name = TypeManager.IndexerPropertyName (container.TypeBuilder);
-                               ms = new MethodSignature (name, null, ParameterTypes);
-                               base_name = TypeManager.IndexerPropertyName (container.TypeBuilder.BaseType);
-                               base_ms = new MethodSignature (base_name, null, ParameterTypes);
-                       } else {
-                               report_name = Name;
-                               ms = base_ms = new MethodSignature (Name, null, ParameterTypes);
+                       ar = Parent.Properties;
+                       if (ar != null) {
+                               int arLen = ar.Count;
+                                       
+                               for (int i = 0; i < arLen; i++) {
+                                       Property m = (Property) ar [i];
+                                       if (IsDuplicateImplementation (m))
+                                               return false;
+                               }
                        }
 
-                       //
-                       // Verify if the parent has a type with the same name, and then
-                       // check whether we have to create a new slot for it or not.
-                       //
-                       Type ptype = container.TypeBuilder.BaseType;
-
-                       // ptype is only null for System.Object while compiling corlib.
-                       if (ptype == null) {
-                               if ((ModFlags & Modifiers.NEW) != 0)
-                                       WarningNotHiding (container);
-
-                               return true;
-                       }
+                       return true;
+               }
 
-                       MemberInfo parent_member = null;
-                       
+               protected override MethodInfo FindOutParentMethod (TypeContainer container, ref Type parent_ret_type)
+               {
+                       PropertyInfo parent_property = container.ParentContainer.MemberCache.FindMemberToOverride (
+                               container.TypeBuilder, Name, ParameterTypes, true) as PropertyInfo;
+  
+                       if (parent_property == null)
+                               return null;
+  
+                       parent_ret_type = parent_property.PropertyType;
+  
+                       MethodInfo temp_m;
+                       temp_m = parent_property.GetGetMethod (true);
+                       if (temp_m != null)
+                               return temp_m;
+  
+                       System.Diagnostics.Debug.Assert (parent_property.GetSetMethod (true) != null, "Internal error property without get/set");
+                       return parent_property.GetSetMethod (true);
+               }
+
+               public override void Emit ()
+               {
                        //
-                       // Explicit implementations do not have `parent' methods, however,
-                       // the member cache stores them there. Without this check, we get
-                       // an incorrect warning in corlib.
+                       // The PropertyBuilder can be null for explicit implementations, in that
+                       // case, we do not actually emit the ".property", so there is nowhere to
+                       // put the attribute
                        //
-                       if (! IsExplicitImpl) {
-                               parent_member = ((IMemberContainer)container).Parent.MemberCache.FindMemberToOverride (
-                                       container.TypeBuilder, Name, ParameterTypes, true);
-                       }
-
-                       if (parent_member is PropertyInfo) {
-                               PropertyInfo parent_property = (PropertyInfo)parent_member;
-
-                               string name = parent_property.DeclaringType.Name + "." +
-                                       parent_property.Name;
+                       if (PropertyBuilder != null && OptAttributes != null)
+                               OptAttributes.Emit (ec, this);
 
-                               MethodInfo get, set, parent_method;
-                               get = parent_property.GetGetMethod (true);
-                               set = parent_property.GetSetMethod (true);
+                       if (!Get.IsDummy)
+                               Get.Emit (Parent);
 
-                               if (get != null)
-                                       parent_method = get;
-                               else if (set != null)
-                                       parent_method = set;
-                               else
-                                       throw new Exception ("Internal error!");
-
-                               if (!CheckMethodAgainstBase (container, flags, parent_method, name))
-                                       return false;
-
-                               if ((ModFlags & Modifiers.NEW) == 0) {
-                                       Type parent_type = TypeManager.TypeToCoreType (
-                                               parent_property.PropertyType);
-
-                                       if (parent_type != MemberType) {
-                                               Report.Error (
-                                                       508, Location, container.MakeName (Name) + ": cannot " +
-                                                       "change return type when overriding " +
-                                                       "inherited member " + name);
-                                               return false;
-                                       }
-                               }
-                       } else if (parent_member == null){
-                               if ((ModFlags & Modifiers.NEW) != 0)
-                                       WarningNotHiding (container);
+                       if (!Set.IsDummy)
+                               Set.Emit (Parent);
 
-                               if ((ModFlags & Modifiers.OVERRIDE) != 0){
-                                       if (this is Indexer)
-                                               Report.Error (115, Location,
-                                                             container.MakeName (Name) +
-                                                             " no suitable indexers found to override");
-                                       else
-                                               Report.Error (115, Location,
-                                                             container.MakeName (Name) +
-                                                             " no suitable properties found to override");
-                                       return false;
-                               }
-                       }
-                       return true;
+                       base.Emit ();
+               }
+
+               /// <summary>
+               /// Tests whether accessors are not in collision with some method (CS0111)
+               /// </summary>
+               public bool AreAccessorsDuplicateImplementation (MethodCore mc)
+               {
+                       return Get.IsDuplicateImplementation (mc) || Set.IsDuplicateImplementation (mc);
                }
 
-               public override void Emit (TypeContainer tc)
+               protected override void UpdateMemberName ()
                {
-                       //
-                       // The PropertyBuilder can be null for explicit implementations, in that
-                       // case, we do not actually emit the ".property", so there is nowhere to
-                       // put the attribute
-                       //
-                       if (PropertyBuilder != null)
-                               Attribute.ApplyAttributes (ec, PropertyBuilder, this, OptAttributes);
+                       base.UpdateMemberName ();
 
-                       if (Get != null)
-                               Get.Emit (tc);
+                       Get.UpdateName (this);
+                       Set.UpdateName (this);
+               }
 
-                       if (Set != null)
-                               Set.Emit (tc);
 
-                       base.Emit (tc);
+               public override string[] ValidAttributeTargets {
+                       get {
+                               return attribute_targets;
+                       }
                }
        }
                        
@@ -5064,34 +5699,38 @@ namespace Mono.CSharp {
                const int AllowedInterfaceModifiers =
                        Modifiers.NEW;
 
-               public Property (DeclSpace ds, Expression type, int mod_flags, bool is_iface,
+               public Property (TypeContainer ds, Expression type, int mod, bool is_iface,
                                 string name, Attributes attrs, Accessor get_block,
                                 Accessor set_block, Location loc)
-                       : base (ds, type, mod_flags,
+                       : base (ds, type, mod,
                                is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
                                is_iface, name, Parameters.EmptyReadOnlyParameters, attrs,
                                loc)
                {
-                       if (get_block != null)
+                       if (get_block == null)
+                               Get = new GetMethod (this);
+                       else
                                Get = new GetMethod (this, get_block);
 
-                       if (set_block != null)
+                       if (set_block == null)
+                               Set = new SetMethod (this);
+                       else
                                Set = new SetMethod (this, set_block);
                }
 
-               public override bool Define (TypeContainer container)
+               public override bool Define ()
                {
-                       if (!DoDefine (container))
+                       if (!DoDefine ())
                                return false;
 
-                       if (!CheckBase (container))
+                       if (!CheckBase ())
                                return false;
 
                        flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
 
-                       if (Get != null) {
+                       if (!Get.IsDummy) {
 
-                               GetBuilder = Get.Define (container);
+                               GetBuilder = Get.Define (Parent);
                                if (GetBuilder == null)
                                        return false;
 
@@ -5099,19 +5738,19 @@ namespace Mono.CSharp {
                                // Setup iterator if we are one
                                //
                                if ((ModFlags & Modifiers.METHOD_YIELDS) != 0){
-                                       IteratorHandler ih = new  IteratorHandler (
-                                                                                  "get", container, MemberType,
-                                                                                  TypeManager.NoTypes, Get.ParameterInfo, ModFlags, Location);
+                                       Iterator iterator = new Iterator (
+                                               Parent, "get", MemberType,
+                                               TypeManager.NoTypes, Get.ParameterInfo,
+                                               ModFlags, Get.Block, Location);
                                        
-                                       Block new_block = ih.Setup (block);
-                                       if (new_block == null)
+                                       if (!iterator.DefineIterator ())
                                                return false;
-                                       block = new_block;
+                                       Get.Block = iterator.Block;
                                }
                        }
 
-                       if (Set != null) {
-                               SetBuilder = Set.Define (container);
+                       if (!Set.IsDummy) {
+                               SetBuilder = Set.Define (Parent);
                                if (SetBuilder == null)
                                        return false;
 
@@ -5126,26 +5765,16 @@ namespace Mono.CSharp {
                                        PropertyAttributes.SpecialName;
 
                        if (!IsExplicitImpl){
-                               PropertyBuilder = container.TypeBuilder.DefineProperty (
+                               PropertyBuilder = Parent.TypeBuilder.DefineProperty (
                                        Name, prop_attr, MemberType, null);
                                
-                               if (Get != null)
+                               if (!Get.IsDummy)
                                        PropertyBuilder.SetGetMethod (GetBuilder);
                                
-                               if (Set != null)
+                               if (!Set.IsDummy)
                                        PropertyBuilder.SetSetMethod (SetBuilder);
 
-                               //
-                               // HACK for the reasons exposed above
-                               //
-                               if (!TypeManager.RegisterProperty (PropertyBuilder, GetBuilder, SetBuilder)) {
-                                       Report.Error (
-                                               111, Location,
-                                               "Class `" + container.Name +
-                                               "' already contains a definition for the property `" +
-                                               Name + "'");
-                                       return false;
-                               }
+                               TypeManager.RegisterProperty (PropertyBuilder, GetBuilder, SetBuilder);
                        }
                        return true;
                }
@@ -5300,19 +5929,82 @@ namespace Mono.CSharp {
                }
        }
        
-       public class Event : FieldBase {
+       /// <summary>
+       /// For case when event is declared like property (with add and remove accessors).
+       /// </summary>
+       public class EventProperty: Event {
+
+               static string[] attribute_targets = new string [] { "event", "property" };
 
-               sealed class AddDelegateMethod: DelegateMethod
+               public EventProperty (TypeContainer parent, Expression type, int mod_flags,
+                                     bool is_iface, string name, Object init,
+                                     Attributes attrs, Accessor add, Accessor remove,
+                                     Location loc)
+                       : base (parent, type, mod_flags, is_iface, name, init, attrs, loc)
                {
-                       public AddDelegateMethod (Event method, Accessor accessor):
-                               base (method, accessor)
+                       Add = new AddDelegateMethod (this, add);
+                       Remove = new RemoveDelegateMethod (this, remove);
+               }
+
+               public override string[] ValidAttributeTargets {
+                       get {
+                               return attribute_targets;
+                       }
+               }
+       }
+
+       /// <summary>
+       /// Event is declared like field.
+       /// </summary>
+       public class EventField: Event {
+
+               static string[] attribute_targets = new string [] { "event", "field", "method" };
+
+               public EventField (TypeContainer parent, Expression type, int mod_flags,
+                                  bool is_iface, string name, Object init,
+                                  Attributes attrs, Location loc)
+                       : base (parent, type, mod_flags, is_iface, name, init, attrs, loc)
+               {
+                       Add = new AddDelegateMethod (this);
+                       Remove = new RemoveDelegateMethod (this);
+               }
+
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               {
+                       if (a.Target == AttributeTargets.Field) {
+                               FieldBuilder.SetCustomAttribute (cb);
+                               return;
+                       }
+
+                       if (a.Target == AttributeTargets.Method) {
+                               AddBuilder.SetCustomAttribute (cb);
+                               RemoveBuilder.SetCustomAttribute (cb);
+                               return;
+                       }
+
+                       base.ApplyAttributeBuilder (a, cb);
+               }
+
+               public override string[] ValidAttributeTargets {
+                       get {
+                               return attribute_targets;
+                       }
+               }
+       }
+
+       public abstract class Event : FieldBase {
+
+               protected sealed class AddDelegateMethod: DelegateMethod
+               {
+
+                       public AddDelegateMethod (Event method):
+                               base (method, "add_")
                        {
                        }
 
-                       public override string MethodName {
-                               get {
-                                       return "add_" + method.ShortName;
-                               }
+                       public AddDelegateMethod (Event method, Accessor accessor):
+                               base (method, accessor, "add_")
+                       {
                        }
 
                        protected override MethodInfo DelegateMethodInfo {
@@ -5323,17 +6015,16 @@ namespace Mono.CSharp {
 
                }
 
-               sealed class RemoveDelegateMethod: DelegateMethod
+               protected sealed class RemoveDelegateMethod: DelegateMethod
                {
-                       public RemoveDelegateMethod (Event method, Accessor accessor):
-                               base (method, accessor)
+                       public RemoveDelegateMethod (Event method):
+                               base (method, "remove_")
                        {
                        }
 
-                       public override string MethodName {
-                               get {
-                                       return "remove_" + method.ShortName;
-                               }
+                       public RemoveDelegateMethod (Event method, Accessor accessor):
+                               base (method, accessor, "remove_")
+                       {
                        }
 
                        protected override MethodInfo DelegateMethodInfo {
@@ -5344,22 +6035,53 @@ namespace Mono.CSharp {
 
                }
 
-               abstract class DelegateMethod: IMethodData
+               public abstract class DelegateMethod: AbstractPropertyEventMethod
                {
                        protected readonly Event method;
-                       public readonly Accessor Accessor;
-                       protected MethodData method_data;
+                       ImplicitParameter param_attr;
+
+                       static string[] attribute_targets = new string [] { "method", "param", "return" };
+
+                       public DelegateMethod (Event method, string prefix):
+                               base (SetupName (prefix, method), method.Location)
+                       {
+                               this.method = method;
+                       }
 
-                       public DelegateMethod (Event method, Accessor accessor)
+                       public DelegateMethod (Event method, Accessor accessor, string prefix):
+                               base (accessor, SetupName (prefix, method))
                        {
                                this.method = method;
-                               this.Accessor = accessor;
+                       }
+
+                       protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
+                       {
+                               if (a.Target == AttributeTargets.Parameter) {
+                                       if (param_attr == null)
+                                               param_attr = new ImplicitParameter (method_data.MethodBuilder);
+
+                                       param_attr.ApplyAttributeBuilder (a, cb);
+                                       return;
+                               }
+
+                               base.ApplyAttributeBuilder (a, cb);
+                       }
+
+                       public override AttributeTargets AttributeTargets {
+                               get {
+                                       return AttributeTargets.Method;
+                               }
+                       }
+
+                       public override bool IsClsCompliaceRequired(DeclSpace ds)
+                       {
+                               return method.IsClsCompliaceRequired (ds);
                        }
 
                        public MethodBuilder Define (TypeContainer container, InternalParameters ip)
                        {
                                method_data = new MethodData (method, ip, method.ModFlags,
-                                       method.flags | MethodAttributes.HideBySig | MethodAttributes.SpecialName, false, this);
+                                       method.flags | MethodAttributes.HideBySig | MethodAttributes.SpecialName, this);
 
                                if (!method_data.Define (container))
                                        return null;
@@ -5369,25 +6091,11 @@ namespace Mono.CSharp {
                                return mb;
                        }
 
-                       #region IMethodData Members
-
-                       public Block Block {
-                               get {
-                                       return Accessor.Block;
-                               }
-                       }
-
-                       public CallingConventions CallingConventions {
-                               get {
-                                       return CallingConventions.Standard;
-                               }
-                       }
 
-                       public void Emit (TypeContainer tc)
+                       public override void Emit (TypeContainer tc)
                        {
-                               if (Accessor != null) {
-                                       method_data.Emit (tc, Accessor);
-                                       Accessor.Block = null;
+                               if (block != null) {
+                                       base.Emit (tc);
                                        return;
                                }
 
@@ -5395,6 +6103,7 @@ namespace Mono.CSharp {
                                EmitContext ec = CreateEmitContext (tc, ig);
                                FieldInfo field_info = (FieldInfo)method.FieldBuilder;
 
+                               method_data.MethodBuilder.SetImplementationFlags (MethodImplAttributes.Synchronized);
                                if ((method.ModFlags & Modifiers.STATIC) != 0) {
                                        ig.Emit (OpCodes.Ldsfld, field_info);
                                        ig.Emit (OpCodes.Ldarg_0);
@@ -5415,39 +6124,41 @@ namespace Mono.CSharp {
 
                        protected abstract MethodInfo DelegateMethodInfo { get; }
 
-                       public Attributes OptAttributes {
-                               get {
-                                       return Accessor == null ? null : Accessor.OptAttributes;
-                               }
-                       }
-
-                       public Type[] ParameterTypes {
+                       public override Type[] ParameterTypes {
                                get {
                                        return new Type[] { method.MemberType };
                                }
                        }
 
-                       public Type ReturnType {
+                       public override Type ReturnType {
                                get {
                                        return TypeManager.void_type;
                                }
                        }
 
-                       public Location Location {
-                               get {
-                                       return method.Location;
-                               }
+                       public override EmitContext CreateEmitContext (TypeContainer tc,
+                                                                      ILGenerator ig)
+                       {
+                               return new EmitContext (
+                                       tc, method.Parent, Location, ig, ReturnType,
+                                       method.ModFlags, false);
                        }
 
-                       public EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
+                       public override string GetSignatureForError (TypeContainer tc)
                        {
-                               return new EmitContext (tc, method.ds, method.Location, ig, ReturnType, method.ModFlags, false);
+                               return String.Concat (tc.Name, '.', method.Name);
                        }
 
-                       public abstract string MethodName { get; }
-
-                       #endregion
+                       public override ObsoleteAttribute GetObsoleteAttribute ()
+                       {
+                               return method.GetObsoleteAttribute (method.Parent);
+                       }
 
+                       public override string[] ValidAttributeTargets {
+                               get {
+                                       return attribute_targets;
+                               }
+                       }
                }
 
 
@@ -5467,49 +6178,52 @@ namespace Mono.CSharp {
                const int AllowedInterfaceModifiers =
                        Modifiers.NEW;
 
-               readonly DelegateMethod Add, Remove;
+               public DelegateMethod Add, Remove;
                public MyEventBuilder     EventBuilder;
                public MethodBuilder AddBuilder, RemoveBuilder;
-               public DeclSpace ds;
-               
-               public Event (DeclSpace ds, Expression type, int mod_flags, bool is_iface, string name,
-                             Object init, Attributes attrs, Accessor add, Accessor remove,
+
+               public Event (TypeContainer parent, Expression type, int mod_flags,
+                             bool is_iface, string name, Object init, Attributes attrs,
                              Location loc)
-                       : base (type, mod_flags,
+                       : base (parent, type, mod_flags,
                                is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
                                name, init, attrs, loc)
                {
                        IsInterface = is_iface;
-                       Add = new AddDelegateMethod (this, add);
-                       Remove = new RemoveDelegateMethod (this, remove);
-                       this.ds = ds;
                }
 
-               public override void ApplyAttributeBuilder (object builder, Attribute a, CustomAttributeBuilder cb)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               {
+                       EventBuilder.SetCustomAttribute (cb);
+               }
+
+               public bool AreAccessorsDuplicateImplementation (MethodCore mc)
                {
-                       ((MyEventBuilder) builder).SetCustomAttribute (cb);
+                       return Add.IsDuplicateImplementation (mc) || Remove.IsDuplicateImplementation (mc);
+               }
+
+               public override AttributeTargets AttributeTargets {
+                       get {
+                               return AttributeTargets.Event;
+                       }
                }
 
-               public override bool Define (TypeContainer container)
+               public override bool Define ()
                {
                        EventAttributes e_attr;
-                       if (IsInterface)
-                               e_attr = EventAttributes.None;
-                       else
-                               e_attr = EventAttributes.RTSpecialName |
-                                       EventAttributes.SpecialName;
+                       e_attr = EventAttributes.None;
 
-                       if (!DoDefine (container))
+                       if (!DoDefine ())
                                return false;
 
                        if (init != null && ((ModFlags & Modifiers.ABSTRACT) != 0)){
-                               Report.Error (74, Location, "'" + container.Name + "." + Name +
+                               Report.Error (74, Location, "'" + Parent.Name + "." + Name +
                                              "': abstract event can not have an initializer");
                                return false;
                        }
                        
                        if (!MemberType.IsSubclassOf (TypeManager.delegate_type)) {
-                               Report.Error (66, Location, "'" + container.Name + "." + Name +
+                               Report.Error (66, Location, "'" + Parent.Name + "." + Name +
                                              "' : event must be of a delegate type");
                                return false;
                        }
@@ -5517,30 +6231,30 @@ namespace Mono.CSharp {
                        Parameter [] parms = new Parameter [1];
                        parms [0] = new Parameter (Type, "value", Parameter.Modifier.NONE, null);
                        InternalParameters ip = new InternalParameters (
-                               container, new Parameters (parms, null, Location)); 
+                               Parent, new Parameters (parms, null, Location)); 
 
-                       if (!CheckBase (container))
+                       if (!CheckBase ())
                                return false;
 
                        //
                        // Now define the accessors
                        //
 
-                       AddBuilder = Add.Define (container, ip);
+                       AddBuilder = Add.Define (Parent, ip);
                        if (AddBuilder == null)
                                return false;
 
-                       RemoveBuilder = Remove.Define (container, ip);
+                       RemoveBuilder = Remove.Define (Parent, ip);
                        if (RemoveBuilder == null)
                                return false;
 
                        if (!IsExplicitImpl){
                                EventBuilder = new MyEventBuilder (this,
-                                       container.TypeBuilder, Name, e_attr, MemberType);
+                                       Parent.TypeBuilder, Name, e_attr, MemberType);
                                        
-                               if (Add.Accessor == null && Remove.Accessor == null &&
+                               if (Add.Block == null && Remove.Block == null &&
                                    !IsInterface) {
-                                       FieldBuilder = container.TypeBuilder.DefineField (
+                                       FieldBuilder = Parent.TypeBuilder.DefineField (
                                                Name, MemberType,
                                                FieldAttributes.Private | ((ModFlags & Modifiers.STATIC) != 0 ? FieldAttributes.Static : 0));
                                        TypeManager.RegisterPrivateFieldOfEvent (
@@ -5551,49 +6265,63 @@ namespace Mono.CSharp {
                                EventBuilder.SetAddOnMethod (AddBuilder);
                                EventBuilder.SetRemoveOnMethod (RemoveBuilder);
 
-                               if (!TypeManager.RegisterEvent (EventBuilder, AddBuilder, RemoveBuilder)) {
-                                       Report.Error (111, Location,
-                                                     "Class `" + container.Name +
-                                                     "' already contains a definition for the event `" +
-                                                     Name + "'");
-                                       return false;
-                               }
+                               TypeManager.RegisterEvent (EventBuilder, AddBuilder, RemoveBuilder);
                        }
                        
                        return true;
                }
 
-               public override void Emit (TypeContainer tc)
+               protected override bool CheckBase ()
+               {
+                       if (!base.CheckBase ())
+                               return false;
+                       if (conflict_symbol != null && (ModFlags & Modifiers.NEW) == 0) {
+                               if (!(conflict_symbol is EventInfo)) {
+                                       Report.SymbolRelatedToPreviousError (conflict_symbol);
+                                       Report.Error (72, Location, "Event '{0}' can override only event", GetSignatureForError (Parent));
+                                       return false;
+                               }
+                       }
+                       return true;
+               }
+
+               public override void Emit ()
                {
                        if (OptAttributes != null) {
-                               EmitContext ec = new EmitContext (tc, Location, null, MemberType, ModFlags);
-                               Attribute.ApplyAttributes (ec, EventBuilder, this, OptAttributes);
+                               EmitContext ec = new EmitContext (
+                                       Parent, Location, null, MemberType, ModFlags);
+                               OptAttributes.Emit (ec, this);
                        }
 
                        if (!IsInterface) {
-                               Add.Emit (tc);
-                               Remove.Emit (tc);
+                               Add.Emit (Parent);
+                               Remove.Emit (Parent);
                        }
 
-                       base.Emit (tc);
+                       base.Emit ();
+               }
+
+               public override string GetSignatureForError ()
+               {
+                       if (EventBuilder == null)
+                               return base.GetSignatureForError (Parent);
+
+                       return TypeManager.GetFullNameSignature (EventBuilder);
                }
-               
        }
 
-       //
-       // FIXME: This does not handle:
-       //
-       //   int INTERFACENAME [ args ]
-       //   Does not 
-       //
-       // Only:
-       // 
-       // int this [ args ]
+
        public class Indexer : PropertyBase {
 
                class GetIndexerMethod: GetMethod
                {
+                       public GetIndexerMethod (MethodCore method):
+                               base (method)
+                       {
+                       }
+
                        public GetIndexerMethod (MethodCore method, Accessor accessor):
                                base (method, accessor)
                        {
@@ -5610,6 +6338,11 @@ namespace Mono.CSharp {
                {
                        readonly Parameters parameters;
 
+                       public SetIndexerMethod (MethodCore method):
+                               base (method)
+                       {
+                       }
+
                        public SetIndexerMethod (MethodCore method, Parameters parameters, Accessor accessor):
                                base (method, accessor)
                        {
@@ -5656,7 +6389,6 @@ namespace Mono.CSharp {
                                
                                return new InternalParameters (container, set_formal_params);
                        }
-
                }
 
 
@@ -5676,79 +6408,81 @@ namespace Mono.CSharp {
                const int AllowedInterfaceModifiers =
                        Modifiers.NEW;
 
-               public string IndexerName;
-               public string InterfaceIndexerName;
 
-               //
-               // Are we implementing an interface ?
-               //
-               public Indexer (DeclSpace ds, Expression type, string int_type, int mod_flags,
+               public Indexer (TypeContainer ds, Expression type, string int_type, int mod,
                                bool is_iface, Parameters parameters, Attributes attrs,
                                Accessor get_block, Accessor set_block, Location loc)
-                       : base (ds, type, mod_flags,
+                       : base (ds, type, mod,
                                is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
-                               is_iface, "", parameters, attrs, loc)
+                               is_iface, int_type, parameters, attrs, loc)
                {
-                       ExplicitInterfaceName = int_type;
-
-                       if (get_block != null)
+                       if (get_block == null)
+                               Get = new GetIndexerMethod (this);
+                       else
                                Get = new GetIndexerMethod (this, get_block);
 
-                       if (set_block != null)
+                       if (set_block == null)
+                               Set = new SetIndexerMethod (this);
+                       else
                                Set = new SetIndexerMethod (this, parameters, set_block);
                }
                       
-               public override bool Define (TypeContainer container)
+               public override bool Define ()
                {
                        PropertyAttributes prop_attr =
                                PropertyAttributes.RTSpecialName |
                                PropertyAttributes.SpecialName;
                        
-                       if (!DoDefine (container))
+                       if (!DoDefine ())
                                return false;
 
-                       IndexerName = Attribute.ScanForIndexerName (ec, OptAttributes);
-                       if (IndexerName == null)
-                               IndexerName = "Item";
-                       else {
-                               if (! Tokenizer.IsValidIdentifier (IndexerName)) {
-                                       Report.Error (633, Location, "The IndexerName specified is an invalid identifier");
-                                       return false;
-                               }
-                               
-                               if (IsExplicitImpl) {
-                                       Report.Error (592, Location,
-                                                     "Attribute 'IndexerName' is not valid on explicit " +
-                                                     "implementations.");
-                                       
-                                       return false;
+                       if (OptAttributes != null) {
+                               Attribute indexer_attr = OptAttributes.GetIndexerNameAttribute (ec);
+                               if (indexer_attr != null) {
+                                       ShortName = indexer_attr.GetIndexerAttributeValue (ec);
+
+                                       if (IsExplicitImpl) {
+                                               Report.Error (415, indexer_attr.Location, "The 'IndexerName' attribute is valid only on an 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;
+                                       }
+
+                                       UpdateMemberName ();
                                }
                        }
 
-                       ShortName = IndexerName;
-                       if (IsExplicitImpl) {
-                               InterfaceIndexerName = TypeManager.IndexerPropertyName (InterfaceType);
-                               Name = InterfaceType.FullName + "." + IndexerName;
-                       } else {
-                               InterfaceIndexerName = IndexerName;
-                               Name = ShortName;
+                       if (InterfaceType != null) {
+                               string parent_IndexerName = TypeManager.IndexerPropertyName (InterfaceType);
+                               if (parent_IndexerName != Name)
+                                       ShortName = parent_IndexerName;
+                                       UpdateMemberName ();
                        }
 
-                       if (!CheckNameCollision (container))
+                       if (!Parent.AddToMemberContainer (this, true) ||
+                               !Parent.AddToMemberContainer (Get, true) || !Parent.AddToMemberContainer (Set, true))
                                return false;
 
-                       if (!CheckBase (container))
+                       if (!CheckBase ())
                                return false;
 
                        flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
-                       if (Get != null){
-                               GetBuilder = Get.Define (container);
+                       if (!Get.IsDummy){
+                               GetBuilder = Get.Define (Parent);
                                if (GetBuilder == null)
                                        return false;
                        }
                        
-                       if (Set != null){
-                               SetBuilder = Set.Define (container);
+                       if (!Set.IsDummy){
+                               SetBuilder = Set.Define (Parent);
                                if (SetBuilder == null)
                                        return false;
                        }
@@ -5758,19 +6492,24 @@ namespace Mono.CSharp {
                        //
                        Parameter [] p = Parameters.FixedParameters;
                        if (p != null) {
+                               if ((p [0].ModFlags & Parameter.Modifier.ISBYREF) != 0) {
+                                       Report.Error (631, Location, "ref and out are not valid in this context");
+                                       return false;
+                               }
+
                                int i;
                                
                                for (i = 0; i < p.Length; ++i) {
-                                       if (Get != null)
+                                       if (!Get.IsDummy)
                                                GetBuilder.DefineParameter (
                                                        i + 1, p [i].Attributes, p [i].Name);
 
-                                       if (Set != null)
+                                       if (!Set.IsDummy)
                                                SetBuilder.DefineParameter (
                                                        i + 1, p [i].Attributes, p [i].Name);
                                }
 
-                               if (Set != null)
+                               if (!Set.IsDummy)
                                        SetBuilder.DefineParameter (
                                                i + 1, ParameterAttributes.None, "value");
                                        
@@ -5789,13 +6528,13 @@ namespace Mono.CSharp {
                        //    explicit interface implementation.
                        //
                        if (!IsExplicitImpl) {
-                               PropertyBuilder = container.TypeBuilder.DefineProperty (
-                                       IndexerName, prop_attr, MemberType, ParameterTypes);
+                               PropertyBuilder = Parent.TypeBuilder.DefineProperty (
+                                       ShortName, prop_attr, MemberType, ParameterTypes);
 
-                               if (Get != null)
+                               if (!Get.IsDummy)
                                        PropertyBuilder.SetGetMethod (GetBuilder);
 
-                               if (Set != null)
+                               if (!Set.IsDummy)
                                        PropertyBuilder.SetSetMethod (SetBuilder);
                                
                                TypeManager.RegisterIndexer (PropertyBuilder, GetBuilder, SetBuilder,
@@ -5805,59 +6544,21 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               bool CheckNameCollision (TypeContainer container) {
-                       switch (VerifyName (container)){
-                               case DeclSpace.AdditionResult.NameExists:
-                                       Report.Error (102, Location, "The container '{0}' already contains a definition for '{1}'", container.GetSignatureForError (), Name);
-                                       return false;
-
-                               case DeclSpace.AdditionResult.Success:
-                                       return true;
-                       }
-                       throw new NotImplementedException ();
-               }
-
-               DeclSpace.AdditionResult VerifyName (TypeContainer container) {
-                       if (!AddIndexer (container, container.Name + "." + Name))
-                               return DeclSpace.AdditionResult.NameExists;
-
-                       if (Get != null) {
-                               if (!AddIndexer (container, container.Name + ".get_" + Name))
-                                       return DeclSpace.AdditionResult.NameExists;
-                       }
-
-                       if (Set != null) {
-                               if (!AddIndexer (container, container.Name + ".set_" + Name))
-                                       return DeclSpace.AdditionResult.NameExists;
-                       }
-                       return DeclSpace.AdditionResult.Success;
-               }
-
-               bool AddIndexer (TypeContainer container, string fullname)
-               {
-                       object value = container.GetDefinition (fullname);
-
-                       if (value != null) {
-                               return value.GetType () != GetType () ? false : true;
-                       }
-
-                       container.DefineName (fullname, this);
-                       return true;
-               }
-
                public override string GetSignatureForError ()
                {
+                       if (PropertyBuilder == null)
+                               return GetSignatureForError (Parent);
+
                        return TypeManager.CSharpSignature (PropertyBuilder, true);
                }
 
-               protected override string RealMethodName {
-                       get {
-                               return IndexerName;
-                       }
+               public override string GetSignatureForError(TypeContainer tc)
+               {
+                       return String.Concat (tc.Name, ".this[", Parameters.FixedParameters [0].TypeName.ToString (), ']');
                }
        }
 
-       public class Operator : MemberBase, IIteratorContainer {
+       public class Operator : MethodCore, IIteratorContainer {
 
                const int AllowedModifiers =
                        Modifiers.PUBLIC |
@@ -5908,85 +6609,97 @@ namespace Mono.CSharp {
                };
 
                public readonly OpType OperatorType;
-               public readonly Expression ReturnType;
-               public readonly Expression FirstArgType, SecondArgType;
-               public readonly string FirstArgName, SecondArgName;
-               public Block           Block;
                public MethodBuilder   OperatorMethodBuilder;
                
-               public string MethodName;
                public Method OperatorMethod;
 
-               public Operator (OpType type, Expression ret_type, int mod_flags,
-                                Expression arg1type, string arg1name,
-                                Expression arg2type, string arg2name,
+               static string[] attribute_targets = new string [] { "method", "return" };
+
+               public Operator (TypeContainer parent, OpType type, Expression ret_type,
+                                int mod_flags, Parameters parameters,
                                 Block block, Attributes attrs, Location loc)
-                       : base (ret_type, mod_flags, AllowedModifiers, Modifiers.PUBLIC, "", attrs, loc)
+                       : base (parent, ret_type, mod_flags, AllowedModifiers, false, "op_" + type,
+                               attrs, parameters, loc)
                {
                        OperatorType = type;
-                       Name = "op_" + OperatorType;
-                       ReturnType = ret_type;
-                       FirstArgType = arg1type;
-                       FirstArgName = arg1name;
-                       SecondArgType = arg2type;
-                       SecondArgName = arg2name;
                        Block = block;
                }
 
-               string Prototype (TypeContainer container)
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb) 
                {
-                       return container.Name + ".operator " + OperatorType + " (" + FirstArgType + "," +
-                               SecondArgType + ")";
+                       OperatorMethod.ApplyAttributeBuilder (a, cb);
                }
 
-               public override void ApplyAttributeBuilder (object builder, Attribute a, CustomAttributeBuilder cb) 
-               {
-                       OperatorMethod.ApplyAttributeBuilder (builder, a, cb);
+               public override AttributeTargets AttributeTargets {
+                       get {
+                               return AttributeTargets.Method; 
+                       }
                }
                
-               public override bool Define (TypeContainer container)
+               protected override bool CheckForDuplications()
+               {
+                       ArrayList ar = Parent.Operators;
+                       if (ar != null) {
+                               int arLen = ar.Count;
+                                       
+                               for (int i = 0; i < arLen; i++) {
+                                       Operator o = (Operator) ar [i];
+                                       if (IsDuplicateImplementation (o))
+                                               return false;
+                               }
+                       }
+
+                       ar = Parent.Methods;
+                       if (ar != null) {
+                               int arLen = ar.Count;
+                                       
+                               for (int i = 0; i < arLen; i++) {
+                                       Method m = (Method) ar [i];
+                                       if (IsDuplicateImplementation (m))
+                                               return false;
+                               }
+                       }
+
+                       return true;
+               }
+
+               public override bool Define ()
                {
-                       int length = 1;
-                       MethodName = "op_" + OperatorType;
-                       
-                       if (SecondArgType != null)
-                               length = 2;
-                       
-                       Parameter [] param_list = new Parameter [length];
 
                        if ((ModFlags & RequiredModifiers) != RequiredModifiers){
                                Report.Error (
                                        558, Location, 
                                        "User defined operators `" +
-                                       Prototype (container) +
+                                       GetSignatureForError (Parent) +
                                        "' must be declared static and public");
                                return false;
                        }
 
-                       param_list[0] = new Parameter (FirstArgType, FirstArgName,
-                                                      Parameter.Modifier.NONE, null);
-                       if (SecondArgType != null)
-                               param_list[1] = new Parameter (SecondArgType, SecondArgName,
-                                                              Parameter.Modifier.NONE, null);
-                       
+                       if (!DoDefine ())
+                               return false;
+
                        OperatorMethod = new Method (
-                               container, ReturnType, ModFlags, false, MethodName,
-                               new Parameters (param_list, null, Location),
+                               Parent, Type, ModFlags, false, Name,
+                               Parameters,
                                OptAttributes, Location);
 
                        OperatorMethod.Block = Block;
                        OperatorMethod.IsOperator = true;                       
-                       OperatorMethod.Define (container);
+                       OperatorMethod.flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
+                       OperatorMethod.Define ();
 
                        if (OperatorMethod.MethodBuilder == null)
                                return false;
-                       
+
                        OperatorMethodBuilder = OperatorMethod.MethodBuilder;
 
-                       Type [] param_types = OperatorMethod.ParameterTypes;
+                       parameter_types = OperatorMethod.ParameterTypes;
                        Type declaring_type = OperatorMethodBuilder.DeclaringType;
-                       Type return_type = OperatorMethod.GetReturnType ();
-                       Type first_arg_type = param_types [0];
+                       Type return_type = OperatorMethod.ReturnType;
+                       Type first_arg_type = parameter_types [0];
+
+                       if (!CheckBase ())
+                               return false;
 
                        // Rules for conversion operators
                        
@@ -6009,7 +6722,7 @@ namespace Mono.CSharp {
                                }
                                
                                if (first_arg_type == TypeManager.object_type ||
-                                   return_type == TypeManager.object_type){
+                                       return_type == TypeManager.object_type){
                                        Report.Error (
                                                -8, Location,
                                                "User-defined conversion cannot convert to or from " +
@@ -6025,15 +6738,21 @@ namespace Mono.CSharp {
                                        return false;
                                }
                                
-                               if (first_arg_type.IsSubclassOf (return_type) ||
-                                   return_type.IsSubclassOf (first_arg_type)){
-                                       Report.Error (
-                                               -10, Location,
-                                               "User-defined conversion cannot convert between types " +
-                                               "that derive from each other");
+                               if (first_arg_type.IsSubclassOf (return_type)
+                                       || return_type.IsSubclassOf (first_arg_type)){
+                                       if (declaring_type.IsSubclassOf (return_type)) {
+                                               Report.Error (553, Location, "'{0}' : user defined conversion to/from base class", GetSignatureForError ());
+                                               return false;
+                                       }
+                                       Report.Error (554, Location, "'{0}' : user defined conversion to/from derived class", GetSignatureForError ());
+                                       return false;
+                               }
+                       } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
+                               if (first_arg_type != declaring_type || parameter_types [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 (SecondArgType == null) {
+                       } else if (Parameters.FixedParameters.Length == 1) {
                                // Checks for Unary operators
                                
                                if (first_arg_type != declaring_type){
@@ -6069,7 +6788,7 @@ namespace Mono.CSharp {
                                // Checks for Binary operators
                                
                                if (first_arg_type != declaring_type &&
-                                   param_types [1] != declaring_type){
+                                   parameter_types [1] != declaring_type){
                                        Report.Error (
                                                563, Location,
                                                "One of the parameters of a binary operator must " +
@@ -6081,7 +6800,7 @@ namespace Mono.CSharp {
                        return true;
                }
                
-               public override void Emit (TypeContainer container)
+               public override void Emit ()
                {
                        //
                        // abstract or extern methods have no bodies
@@ -6089,10 +6808,16 @@ namespace Mono.CSharp {
                        if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
                                return;
                        
-                       OperatorMethod.Emit (container);
+                       OperatorMethod.Emit ();
                        Block = null;
                }
 
+               // Operator cannot be override
+               protected override MethodInfo FindOutParentMethod (TypeContainer container, ref Type parent_ret_type)
+               {
+                       return null;
+               }
+
                public static string GetName (OpType ot)
                {
                        switch (ot){
@@ -6151,13 +6876,34 @@ namespace Mono.CSharp {
                        default: return "";
                        }
                }
-               
+
+               public override string GetSignatureForError (TypeContainer tc)
+               {
+                       StringBuilder sb = new StringBuilder ();
+                       sb.AppendFormat ("{0}.operator {1} {2}({3}", tc.Name, GetName (OperatorType), Type.ToString (), Parameters.FixedParameters [0].GetSignatureForError ());
+                       
+                       if (Parameters.FixedParameters.Length > 1) {
+                               sb.Append (",");
+                               sb.Append (Parameters.FixedParameters [1].GetSignatureForError ());
+                       }
+                       sb.Append (")");
+                       return sb.ToString ();
+               }
+
+               public override string GetSignatureForError ()
+               {
+                       return ToString ();
+               }
+
                public override string ToString ()
                {
-                       Type return_type = OperatorMethod.GetReturnType();
+                       if (OperatorMethod == null)
+                               return Name;
+
+                       Type return_type = OperatorMethod.ReturnType;
                        Type [] param_types = OperatorMethod.ParameterTypes;
                        
-                       if (SecondArgType == null)
+                       if (Parameters.FixedParameters.Length == 1)
                                return String.Format (
                                        "{0} operator {1}({2})",
                                        TypeManager.CSharpName (return_type),
@@ -6171,6 +6917,12 @@ namespace Mono.CSharp {
                                        param_types [0], param_types [1]);
                }
 
+               public override string[] ValidAttributeTargets {
+                       get {
+                               return attribute_targets;
+                       }
+               }
+
                public void SetYields ()
                {
                        ModFlags |= Modifiers.METHOD_YIELDS;