2004-01-19 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / gmcs / class.cs
old mode 100755 (executable)
new mode 100644 (file)
index e814e9a..e9b82d9
@@ -2,12 +2,13 @@
 // class.cs: Class and Struct handlers
 //
 // Authors: Miguel de Icaza (miguel@gnu.org)
-//          Martin Baulig (martin@gnome.org)
+//          Martin Baulig (martin@ximian.com)
 //          Marek Safar (marek.safar@seznam.cz)
 //
 // Licensed under the terms of the GNU GPL
 //
 // (C) 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
+// (C) 2004 Novell, Inc
 //
 //
 //  2002-10-11  Miguel de Icaza  <miguel@ximian.com>
 using System;
 using System.Text;
 using System.Collections;
+using System.Collections.Specialized;
 using System.Reflection;
 using System.Reflection.Emit;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
+using System.Security;
+using System.Security.Permissions;
+using System.Xml;
 
 using Mono.CompilerServices.SymbolWriter;
 
@@ -54,6 +59,319 @@ namespace Mono.CSharp {
        /// </summary>
        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;
 
@@ -61,43 +379,43 @@ namespace Mono.CSharp {
                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 the methods.
-               ArrayList methods;
+               MethodArrayList methods;
 
                // Holds the events
-               ArrayList events;
+               protected MemberCoreArrayList events;
 
                // Holds the indexers
-               ArrayList indexers;
+               IndexerArrayList indexers;
 
                // Holds the operators
-               ArrayList operators;
+               MemberCoreArrayList operators;
 
                // Holds the iterators
                ArrayList iterators;
@@ -105,19 +423,11 @@ namespace Mono.CSharp {
                // Holds the parts of a partial class;
                ArrayList parts;
 
-               // The emit context for toplevel objects.
-               EmitContext ec;
-               
                //
                // 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
@@ -128,8 +438,8 @@ namespace Mono.CSharp {
                // This one is computed after we can distinguish interfaces
                // from classes from the arraylist `type_bases' 
                //
-               string     base_class_name;
-               TypeExpr   parent_type;
+               string base_class_name;
+               TypeExpr parent_type;
 
                ArrayList type_bases;
 
@@ -137,21 +447,19 @@ namespace Mono.CSharp {
                bool members_defined_ok;
 
                // The interfaces we implement.
-               Type[] ifaces;
+               protected Type[] ifaces;
+               protected Type ptype;
 
-               // The parent member container and our member cache
-               IMemberContainer parent_container;
+               // The parent member cache and our member cache
+               MemberCache parent_cache;
                MemberCache member_cache;
 
-               //
-               // The indexer name for this class
-               //
-               public string IndexerName;
+               public const string DefaultIndexerName = "Item";
 
                Type GenericType;
 
-               public TypeContainer (NamespaceEntry ns, TypeContainer parent,
-                                     MemberName name, Attributes attrs, Kind kind, Location l)
+               public TypeContainer (NamespaceEntry ns, TypeContainer parent, MemberName name,
+                                     Attributes attrs, Kind kind, Location l)
                        : base (ns, parent, name, attrs, l)
                {
                        this.Kind = kind;
@@ -161,211 +469,125 @@ namespace Mono.CSharp {
                        base_class_name = null;
                }
 
-               // <summary>
-               //   Used to report back to the user the result of a declaration
-               //   in the current declaration space
-               // </summary>
-               public void CheckDef (AdditionResult result, string name, Location loc)
+               public bool AddToMemberContainer (MemberCore symbol, bool is_method)
                {
-                       if (result == AdditionResult.Success)
-                               return;
-
-                       switch (result){
-                       case AdditionResult.NameExists:
-                               Report.Error (102, loc, "The container `{0}' already " +
-                                             "contains a definition for `{1}'",
-                                             Name, name);
-                               break;
-
-                               //
-                               // This is handled only for static Constructors, because
-                               // in reality we handle these by the semantic analysis later
-                               //
-                       case AdditionResult.MethodExists:
-                               Report.Error (111, loc, "Class `{0}' already defines a " +
-                                             "member called '{1}' with the same parameter " +
-                                             "types (more than one default constructor)",
-                                             Name, name);
-                               break;
-
-                       case AdditionResult.EnclosingClash:
-                               Report.Error (542, loc, "Member names cannot be the same " +
-                                             "as their enclosing type");
-                               break;
-               
-                       case AdditionResult.NotAConstructor:
-                               Report.Error (1520, loc, "Class, struct, or interface method " +
-                                             "must have a return type");
-                               break;
+                       return AddToContainer (symbol, is_method, String.Concat (Name, '.', symbol.Name), symbol.Name);
+               }
 
-                       case AdditionResult.Error:
-                               // Error has already been reported.
-                               break;
-                       }
+               bool AddToTypeContainer (DeclSpace ds)
+               {
+                       return AddToContainer (ds, false, ds.Name, ds.Basename);
                }
 
-               public AdditionResult AddConstant (Const constant)
+               public void AddConstant (Const constant)
                {
-                       AdditionResult res;
-                       string basename = constant.Name;
-                       string fullname = Name + "." + basename;
+                       if (!AddToMemberContainer (constant, false))
+                               return;
 
-                       if ((res = IsValid (basename, fullname)) != AdditionResult.Success)
-                               return res;
-                       
                        if (constants == null)
-                               constants = new ArrayList ();
+                               constants = new MemberCoreArrayList ();
 
                        constants.Add (constant);
-                       DefineName (fullname, constant);
-
-                       return AdditionResult.Success;
                }
 
-               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 (TypeContainer 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 (TypeContainer 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, c.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, "Type '{0}' already defines a member " +
+                                                     "called '{1}' with the same parameter types", 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, "Type '{0}' already defines a member " +
+                                                     "called '{1}' with the same parameter types", 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;
                }
                
-               public AdditionResult AddInterface (TypeContainer 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);
                        
@@ -386,95 +608,65 @@ namespace Mono.CSharp {
 
                        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.MemberName.Left != 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)
@@ -524,7 +716,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               public ArrayList Methods {
+               public MethodArrayList Methods {
                        get {
                                return methods;
                        }
@@ -568,10 +760,6 @@ namespace Mono.CSharp {
                        get {
                                return fields;
                        }
-
-                       set {
-                               fields = value;
-                       }
                }
 
                public ArrayList InstanceConstructors {
@@ -628,6 +816,12 @@ namespace Mono.CSharp {
                        }
                }
 
+               public string IndexerName {
+                       get {
+                               return indexers == null ? DefaultIndexerName : indexers.IndexerName;
+                       }
+               }
+
                //
                // Emits the instance field initializers
                //
@@ -672,7 +866,7 @@ namespace Mono.CSharp {
                //
                // Defines the default constructors
                //
-               void DefineDefaultConstructor (bool is_static)
+               protected void DefineDefaultConstructor (bool is_static)
                {
                        Constructor c;
 
@@ -698,18 +892,6 @@ 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)
@@ -786,8 +968,7 @@ namespace Mono.CSharp {
                        int start, i, j;
 
                        if (Kind == Kind.Class){
-                               TypeExpr name = ResolveTypeExpr (
-                                       (Expression) Bases [0], false, Location);
+                               TypeExpr name = ResolveTypeExpr ((Expression) Bases [0], Location);
 
                                if (name == null){
                                        error = true;
@@ -808,7 +989,7 @@ namespace Mono.CSharp {
                        
                        for (i = start, j = 0; i < count; i++, j++){
                                Expression name = (Expression) Bases [i];
-                               TypeExpr resolved = ResolveTypeExpr (name, false, Location);
+                               TypeExpr resolved = ResolveTypeExpr (name, Location);
                                if (resolved == null) {
                                        error = true;
                                        return null;
@@ -833,8 +1014,7 @@ namespace Mono.CSharp {
                /// </summary>
                TypeExpr [] GetClassBases (out TypeExpr parent, out bool error)
                {
-                       ArrayList bases = Bases;
-                       int start, j, i;
+                       int i;
 
                        error = false;
 
@@ -872,15 +1052,13 @@ namespace Mono.CSharp {
                                }
 
                                if (parent.IsSealed){
-                                       string detail = "";
-                                       
-                                       if (parent.IsValueType)
-                                               detail = " (a class can not inherit from a struct/enum)";
-                                       
-                                       Report.Error (509, "class `"+ Name +
-                                                     "': Cannot inherit from sealed class `"+
-                                                     parent.Name + "'" + detail);
                                        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;
                                }
 
@@ -892,11 +1070,11 @@ namespace Mono.CSharp {
                                        return null;
                                }
 
-                               if (!parent.AsAccessible (this, ModFlags))
-                                       Report.Error (60, Location,
-                                                     "Inconsistent accessibility: base class `" +
-                                                     parent.Name + "' is less accessible than class `" +
-                                                     Name + "'");
+                               if (!parent.AsAccessible (this, ModFlags)) {
+                                       Report.SymbolRelatedToPreviousError (parent.Type);
+                                       Report.Error (60, Location, "Inconsistent accessibility: base class '{0}' is less accessible than class '{1}'", 
+                                               TypeManager.CSharpName (parent.Type), GetSignatureForError ());
+                               }
                        }
 
                        if (parent != null)
@@ -922,13 +1100,17 @@ namespace Mono.CSharp {
                                }
 
                                if (iface.IsClass) {
-                                       if (parent != null){
-                                               Report.Error (527, Location,
+                                       if (parent != null)
+                                               Report.Error (1721, Location,
+                                                             "In Class `{0}', `{1}' is not an interface, and a base class has already been defined",
+                                                             Name, iface.Name);
+                                       else {
+                                               Report.Error (1722, Location,
                                                              "In Class `{0}', `{1}' is not " +
-                                                             "an interface", Name, iface.Name);
-                                               error = true;
-                                               return null;
+                                                             "an interface, a base class must be listed first", Name, iface.Name);
                                        }
+                                       error = true;
+                                       return null;
                                }
   
                                for (int x = 0; x < i; x++) {
@@ -963,7 +1145,8 @@ namespace Mono.CSharp {
                                        if (iface == t)
                                                continue;
 
-                                       if (!TypeManager.MayBecomeEqualGenericInstances (iface, t))
+                                       Type[] infered = new Type [CountTypeParameters];
+                                       if (!TypeManager.MayBecomeEqualGenericInstances (iface, t, infered, null))
                                                continue;
 
                                        Report.Error (
@@ -991,67 +1174,75 @@ namespace Mono.CSharp {
                {
                        TypeExpr parent;
 
-                       if (TypeBuilder != null)
-                               return TypeBuilder;
-
                        if (error)
                                return null;
-                       
-                       if (InTransit) {
-                               Report.Error (146, Location, "Class definition is circular: `{0}'", Name);
-                               error = true;
-                               return null;
-                       }
-                       
-                       InTransit = true;
+
+                       if (TypeBuilder != null)
+                               return TypeBuilder;
 
                        ec = new EmitContext (this, Mono.CSharp.Location.Null, null, null, ModFlags);
 
                        TypeAttributes type_attributes = TypeAttr;
 
-                       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, null, null);
-                       } else {
-                               TypeBuilder builder = Parent.DefineType ();
-                               if (builder == null) {
-                                       error = true;
-                                       return null;
-                               }
-                               
-                               TypeBuilder = builder.DefineNestedType (
-                                       MemberName.Basename, type_attributes, null, null);
-                       }
-
-                       TypeManager.AddUserType (Name, TypeBuilder, this);
+                                       ModuleBuilder builder = CodeGen.Module.Builder;
+                                       TypeBuilder = builder.DefineType (
+                                               Name, type_attributes, null, null);
+                               } else {
+                                       TypeBuilder builder;
+                                       if (Parent.TypeBuilder != null)
+                                               builder = Parent.TypeBuilder;
+                                       else
+                                               builder = Parent.DefineType ();
 
-                       if (IsGeneric) {
-                               foreach (TypeParameter type_param in TypeParameters) {
-                                       if (!type_param.Resolve (this)) {
+                                       if (builder == null) {
                                                error = true;
                                                return null;
                                        }
+                               
+                                       TypeBuilder = builder.DefineNestedType (
+                                               MemberName.Basename, type_attributes,
+                                               null, null);
                                }
+                       }
+                       catch (ArgumentException) {
+                               Report.RuntimeMissingSupport (Location, "static classes");
+                               return null;
+                       }
+
+                       TypeManager.AddUserType (Name, TypeBuilder, this);
 
-                               CurrentType = new ConstructedType (
-                                       Name, TypeParameters, Location);
+                       TypeExpr current_type = null;
 
+                       if (IsGeneric) {
                                string[] param_names = new string [TypeParameters.Length];
                                for (int i = 0; i < TypeParameters.Length; i++)
                                        param_names [i] = TypeParameters [i].Name;
 
                                GenericTypeParameterBuilder[] gen_params;
-                               
                                gen_params = TypeBuilder.DefineGenericParameters (param_names);
 
-                               for (int i = 0; i < gen_params.Length; i++)
-                                       TypeParameters [i].Define (gen_params [i]);
+                               int offset = CountTypeParameters - CurrentTypeParameters.Length;
+                               for (int i = offset; i < gen_params.Length; i++)
+                                       CurrentTypeParameters [i - offset].Define (gen_params [i]);
+
+                               foreach (TypeParameter type_param in CurrentTypeParameters) {
+                                       if (!type_param.Resolve (this)) {
+                                               error = true;
+                                               return null;
+                                       }
+                               }
+
+                               for (int i = offset; i < gen_params.Length; i++)
+                                       CurrentTypeParameters [i - offset].DefineConstraints ();
+
+                               current_type = new ConstructedType (Name, TypeParameters, Location);
                        }
 
                        if (IsGeneric) {
@@ -1088,19 +1279,19 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       Type ptype;
-                       ConstructedType constructed = parent_type as ConstructedType;
-                       if ((constructed == null) && (parent_type != null))
-                               ptype = parent_type.ResolveType (ec);
-                       else
-                               ptype = null;
-
-                       if (constructed != null) {
-                               ptype = constructed.ResolveType (ec);
-                               if (ptype == null) {
+                       if (parent_type != null) {
+                               parent_type = parent_type.ResolveAsTypeTerminal (ec);
+                               if (parent_type == null) {
                                        error = true;
                                        return null;
                                }
+
+                               ptype = parent_type.Type;
+                       }
+
+                       if (!CheckRecursiveDefinition ()) {
+                               error = true;
+                               return null;
                        }
 
                        if (ptype != null)
@@ -1145,6 +1336,16 @@ namespace Mono.CSharp {
                                        }
                        }
 
+                       if (current_type != null) {
+                               current_type = current_type.ResolveAsTypeTerminal (ec);
+                               if (current_type == null) {
+                                       error = true;
+                                       return null;
+                               }
+
+                               CurrentType = current_type.Type;
+                       }
+
                        //
                        // Finish the setup for the EmitContext
                        //
@@ -1160,9 +1361,8 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       InTransit = false;
                        return TypeBuilder;
-                                       }
+               }
 
                protected virtual bool DefineNestedTypes ()
                {
@@ -1194,127 +1394,37 @@ namespace Mono.CSharp {
                                foreach (ClassPart part in Parts) {
                                        part.TypeBuilder = TypeBuilder;
                                        part.parent_type = parent_type;
+                                       part.ec = new EmitContext (part, Mono.CSharp.Location.Null, null, null, ModFlags);
                                }
-               }
+                       }
 
                        return true;
                }
 
-
-               /// <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)
+               protected bool CheckRecursiveDefinition ()
                {
-                       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 ()){
-                                       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]);
-                               }
+                       if (InTransit) {
+                               Report.Error (146, Location,
+                                             "Class definition is circular: `{0}'",
+                                             GetSignatureForError ());
+                               error = true;
+                               return false;
                        }
-                       
-                       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) {
-                               string name;
-
-                               i.Define ();
 
-                               name = i.IndexerName;
-
-                               if (i.InterfaceType != null) {
-                                       if (seen_normal_indexers)
-                                               throw new Exception ("Internal Error: 'Indexers' array not sorted properly.");
-                                       continue;
-                               }
+                       InTransit = true;
 
-                               seen_normal_indexers = true;
+                       Type parent = ptype;
+                       if (parent != null) {
+                               if (parent.IsGenericInstance)
+                                       parent = parent.GetGenericTypeDefinition ();
 
-                               if (class_indexer_name == null)
-                                       class_indexer_name = name;
-                               else if (name != class_indexer_name)
-                                       Report.Error (668, i.Location, "Two indexers have different names, " +
-                                                     " you should use the same name for all your indexers");
+                               TypeContainer ptc = TypeManager.LookupTypeContainer (parent);
+                               if ((ptc != null) && !ptc.CheckRecursiveDefinition ())
+                                       return false;
                        }
 
-                       if (seen_normal_indexers && class_indexer_name == null)
-                               class_indexer_name = "Item";
-                       IndexerName = class_indexer_name;
+                       InTransit = false;
+                       return true;
                }
 
                static void Error_KeywordNotAllowed (Location loc)
@@ -1336,67 +1446,47 @@ namespace Mono.CSharp {
                        return members_defined_ok;
                }
 
-               bool DoDefineMembers ()
+               protected virtual 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 (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);
+                               parent_cache = TypeManager.LookupMemberCache (TypeBuilder.BaseType);
+
+                       if (TypeBuilder.IsInterface)
+                               parent_cache = TypeManager.LookupParentInterfacesCache (TypeBuilder);
+
+                       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 ());
+                                               }
+                                       }
                                }
                        }
 
-                       Class pclass = Parent as Class;
-                       if (pclass != null) {
-                               string pname = null;
-                               TypeExpr 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);
+                       DefineContainerMembers (constants);
+                       DefineContainerMembers (fields);
 
-                       if ((Kind == Kind.Class) && !(this is ClassPart)){
-                               if (instance_constructors == null){
+                       if ((Kind == Kind.Class) && !(this is ClassPart) && !(this is StaticClass)){
+                               if ((instance_constructors == null) &&
+                                   !(this is StaticClass)) {
                                        if (default_constructor == null)
                                                DefineDefaultConstructor (false);
                                }
@@ -1431,38 +1521,21 @@ namespace Mono.CSharp {
                        //
                        // 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 ();
 
-                       if (methods != null)
-                               DefineMembers (methods, defined_names);
-
-                       if (properties != null)
-                               DefineMembers (properties, defined_names);
-
-                       if (events != null)
-                               DefineMembers (events, defined_names);
-
-                       if (indexers != null)
-                               DefineIndexers ();
-
-                       if (operators != null){
-                               DefineMembers (operators, null);
-
-                               CheckPairedOperators ();
-                       }
-
-                       if (enums != null)
-                               DefineMembers (enums, defined_names);
-                       
-                       if (delegates != null)
-                               DefineMembers (delegates, defined_names);
+                       DefineContainerMembers (properties);
+                       DefineContainerMembers (events);
+                       DefineContainerMembers (indexers);
+                       DefineContainerMembers (methods);
+                       DefineContainerMembers (operators);
+                       DefineContainerMembers (enums);
+                       DefineContainerMembers (delegates);
 
                        if (CurrentType != null) {
-                               GenericType = CurrentType.ResolveType (ec);
+                               GenericType = CurrentType;
 
                                ec.ContainerType = GenericType;
                        }
@@ -1470,7 +1543,7 @@ namespace Mono.CSharp {
 
 #if CACHE
                        if (!(this is ClassPart))
-                       member_cache = new MemberCache (this);
+                               member_cache = new MemberCache (this);
 #endif
 
                        if (parts != null) {
@@ -1493,6 +1566,24 @@ namespace Mono.CSharp {
                        return true;
                }
 
+               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");
+                       }
+               }
+
+               protected virtual void DefineContainerMembers (MemberCoreArrayList mcal)
+               {
+                       if (mcal != null)
+                               mcal.DefineContainerMembers ();
+               }
+
                public override bool Define ()
                {
                        if (parts != null) {
@@ -1505,6 +1596,11 @@ namespace Mono.CSharp {
                        return true;
                }
 
+               public MemberInfo FindMemberWithSameName (string name, bool ignore_methods)
+               {
+                       return ParentCache.FindMemberWithSameName (name, ignore_methods, null);
+               }
+
                /// <summary>
                ///   This function is based by a delegate to the FindMembers routine
                /// </summary>
@@ -1520,15 +1616,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 ()
@@ -1596,6 +1686,13 @@ namespace Mono.CSharp {
                        return retMethods;
                }
                
+               // Indicated whether container has StructLayout attribute set Explicit
+               public virtual bool HasExplicitLayout {
+                       get {
+                               return false;
+                       }
+               }
+               
                /// <summary>
                ///   This method returns the members of this type just like Type.FindMembers would
                ///   Only, we need to use this for types which are _being_ defined because MS' 
@@ -2034,9 +2131,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;
                                                }
                                                
@@ -2049,17 +2144,7 @@ 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");
-                                       }
-                               }
-
-                               if (events != null){
-                                       foreach (Event e in events){
-                                               if (e.status == 0)
-                                                       Report.Warning (67, "The event " + MakeName (e.Name) + " is never used");
+                                               Report.Warning (649, f.Location, "Field '{0}' is never assigned to, and will always have its default value '{1}'", f.GetSignatureForError (), "");
                                        }
                                }
                        }
@@ -2077,7 +2162,7 @@ namespace Mono.CSharp {
                        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) {
@@ -2089,13 +2174,15 @@ 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 ();
                                }
                        }
 
+                       EmitConstants ();
+
                        if (default_static_constructor != null)
                                default_static_constructor.Emit ();
                        
@@ -2112,12 +2199,7 @@ namespace Mono.CSharp {
                                        p.Emit ();
 
                        if (indexers != null){
-                               foreach (Indexer ix in indexers)
-                                       ix.Emit ();
-                               if (IndexerName != null) {
-                                       CustomAttributeBuilder cb = EmitDefaultMemberAttr ();
-                                       TypeBuilder.SetCustomAttribute (cb);
-                               }
+                               indexers.Emit ();
                        }
                        
                        if (fields != null)
@@ -2161,31 +2243,6 @@ namespace Mono.CSharp {
 //                                     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)
@@ -2247,42 +2304,16 @@ namespace Mono.CSharp {
                        type_bases = null;
                        OptAttributes = null;
                        ifaces = null;
-                       parent_container = null;
+                       parent_cache = null;
                        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).
@@ -2367,13 +2398,14 @@ 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;
+                       }
+               }
+
+               public Constructor DefaultStaticConstructor {
+                       get { return default_static_constructor; }
                }
 
                protected override bool VerifyClsCompliance (DeclSpace ds)
@@ -2381,14 +2413,65 @@ namespace Mono.CSharp {
                        if (!base.VerifyClsCompliance (ds))
                                return false;
 
-                       // 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));
+                       VerifyClsName ();
+
+                       if (IsGeneric) {
+                               Report.Error (3024, Location, "'{0}': type parameters are not CLS-compliant",
+                                             GetSignatureForError ());
+                               return false;
+                       }
+
+                       Type base_type = TypeBuilder.BaseType;
+                       if (base_type != null && !AttributeTester.IsClsCompliant (base_type)) {
+                               Report.Error (3009, Location, "'{0}': base type '{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (base_type));
                        }
                        return true;
                }
 
 
+               /// <summary>
+               /// Checks whether container name is CLS Compliant
+               /// </summary>
+               void VerifyClsName ()
+               {
+                       Hashtable parent_members = parent_cache == null ? 
+                               new Hashtable () :
+                               parent_cache.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.
@@ -2444,12 +2527,6 @@ namespace Mono.CSharp {
                        }
                }
 
-               IMemberContainer IMemberContainer.Parent {
-                       get {
-                               return parent_container;
-                       }
-               }
-
                MemberCache IMemberContainer.MemberCache {
                        get {
                                return member_cache;
@@ -2474,202 +2551,49 @@ namespace Mono.CSharp {
                }
 
                //
-               // Operator pair checking
+               // Generates xml doc comments (if any), and if required,
+               // handle warning report.
                //
+               internal override void GenerateDocComment (DeclSpace ds)
+               {
+                       DocUtil.GenerateTypeDocComment (this, ds);
+               }
 
-               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 override string DocCommentHeader {
+                       get { return "T:"; }
+               }
 
-                               ret_type = o.OperatorMethod.GetReturnType ();
-                               Type [] pt = o.OperatorMethod.ParameterTypes;
-                               type1 = pt [0];
-                               type2 = pt [1];
-                               op = o;
-                               ot = o.OperatorType;
+               public virtual MemberCache ParentCache {
+                       get {
+                               return parent_cache;
                        }
+               }
+               
+       }
 
-                       public override int GetHashCode ()
-                       {       
-                               return ret_type.GetHashCode ();
-                       }
+       public class PartialContainer : TypeContainer {
 
-                       public override bool Equals (object o)
-                       {
-                               OperatorEntry other = (OperatorEntry) o;
+               public readonly Namespace Namespace;
+               public readonly int OriginalModFlags;
+               public readonly int AllowedModifiers;
+               public readonly TypeAttributes DefaultTypeAttributes;
 
-                               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 ()
+               static PartialContainer Create (NamespaceEntry ns, TypeContainer parent,
+                                               MemberName member_name, int mod_flags, Kind kind,
+                                               Location loc)
                {
-                       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;
-
-                               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)) {
-                               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 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,
-                                               MemberName name, int mod_flags, Kind kind,
-                                               Location loc)
-               {
-                       PartialContainer pc;
-                       string full_name = name.GetName (true);
-                       DeclSpace ds = (DeclSpace) RootContext.Tree.Decls [full_name];
-                       if (ds != null) {
-                               pc = ds as PartialContainer;
+                       PartialContainer pc;
+                       string full_name = member_name.GetName (true);
+                       DeclSpace ds = (DeclSpace) RootContext.Tree.Decls [full_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);
+                                               member_name.GetTypeName());
 
                                        Report.LocationOfPreviousError (loc);
                                        return null;
@@ -2679,7 +2603,7 @@ namespace Mono.CSharp {
                                        Report.Error (
                                                261, loc, "Partial declarations of `{0}' " +
                                                "must be all classes, all structs or " +
-                                               "all interfaces", name);
+                                               "all interfaces", member_name.GetTypeName ());
                                        return null;
                                }
 
@@ -2687,14 +2611,14 @@ namespace Mono.CSharp {
                                        Report.Error (
                                                262, loc, "Partial declarations of `{0}' " +
                                                "have conflicting accessibility modifiers",
-                                               name);
+                                               member_name.GetTypeName ());
                                        return null;
                                }
 
                                return pc;
                        }
 
-                       pc = new PartialContainer (ns, parent, name, mod_flags, kind, loc);
+                       pc = new PartialContainer (ns, parent, member_name, mod_flags, kind, loc);
                        RootContext.Tree.RecordDecl (full_name, pc);
                        parent.AddType (pc);
                        pc.Register ();
@@ -2757,10 +2681,8 @@ namespace Mono.CSharp {
                {
                        if (Kind == Kind.Interface)
                                Parent.AddInterface (this);
-                       else if (Kind == Kind.Class)
-                               Parent.AddClass (this);
-                       else if (Kind == Kind.Struct)
-                               Parent.AddStruct (this);
+                       else if (Kind == Kind.Class || Kind == Kind.Struct)
+                               Parent.AddClassOrStruct (this);
                        else
                                throw new InvalidOperationException ();
                }
@@ -2785,7 +2707,7 @@ namespace Mono.CSharp {
                }
        }
 
-       public class ClassPart : TypeContainer {
+       public class ClassPart : TypeContainer, IMemberContainer {
                public readonly PartialContainer PartialContainer;
                public readonly bool IsPartial;
 
@@ -2821,10 +2743,17 @@ namespace Mono.CSharp {
                        return PartialContainer.VerifyImplements (
                                interface_type, full, name, loc);
                }
+
+               public override MemberCache ParentCache {
+                       get {
+                               return PartialContainer.ParentCache;
+                       }
+               }
        }
 
        public abstract class ClassOrStruct : TypeContainer {
                bool hasExplicitLayout = false;
+               ListDictionary declarative_security;
 
                public ClassOrStruct (NamespaceEntry ns, TypeContainer parent,
                                      MemberName name, Attributes attrs, Kind kind,
@@ -2838,42 +2767,132 @@ namespace Mono.CSharp {
                        return PendingImplementation.GetPendingImplementations (this);
                }
 
-               protected override void VerifyMembers (EmitContext ec) 
-               {
-                       if (Fields != null) {
-                               foreach (Field f in Fields) {
-                                       if ((f.ModFlags & Modifiers.STATIC) != 0)
-                                               continue;
-                                       if (hasExplicitLayout) {
-                                               if (f.OptAttributes == null 
-                                                   || !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"
-                                                                     + " FieldOffset attribute.");
-                                               }
-                                       }
-                                       else {
-                                               if (f.OptAttributes != null 
-                                                   && 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)");
-                                               }
-                                       }
+               public override bool HasExplicitLayout {
+                       get {
+                               return hasExplicitLayout;
                                }
                        }
+
+               protected override void VerifyMembers (EmitContext ec)
+               {
                        base.VerifyMembers (ec);
+
+                       if ((events != null) && (RootContext.WarningLevel >= 3)) {
+                               foreach (Event e in events){
+                                       if (e.status == 0)
+                                               Report.Warning (67, e.Location, "The event '{0}' is never used", e.GetSignatureForError ());
+                               }
+                       }
                }
 
                public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
                {
+                       if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
+                               if (declarative_security == null)
+                                       declarative_security = new ListDictionary ();
+
+                               a.ExtractSecurityPermissionSet (declarative_security);
+                               return;
+                       }
+
                        if (a.Type == TypeManager.struct_layout_attribute_type
                            && (LayoutKind) a.GetPositionalValue (0) == LayoutKind.Explicit)
                                hasExplicitLayout = true;
 
                        base.ApplyAttributeBuilder (a, cb);
                }
+
+               public override void Emit()
+               {
+                       base.Emit ();
+
+                       if (declarative_security != null) {
+                               foreach (DictionaryEntry de in declarative_security) {
+                                       TypeBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
+                               }
+                       }
+               }
+
+               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, MemberName 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 ((ptype != null) && (ptype != TypeManager.object_type)) {
+                               Report.Error (
+                                       713, Location,
+                                       "Static class '{0}' cannot derive from type '{1}'. " +
+                                       "Static classes must derive from object",
+                                       GetSignatureForError (), ptype);
+                               return null;
+                       }
+
+                       if (ifaces != null) {
+                               foreach (Type t in ifaces)
+                                       Report.SymbolRelatedToPreviousError (t);
+                               Report.Error (
+                                       714, Location,
+                                       "'{0}': static classes cannot implement interfaces",
+                                       GetSignatureForError ());
+                       }
+                       return tb;
+               }
+
+               public override TypeAttributes TypeAttr {
+                       get {
+                               return base.TypeAttr | TypeAttributes.Abstract | TypeAttributes.Sealed;
+                       }
+               }
        }
 
        public class Class : ClassOrStruct {
@@ -2893,8 +2912,8 @@ namespace Mono.CSharp {
                // Information in the case we are an attribute type
                AttributeUsageAttribute attribute_usage;
 
-               public Class (NamespaceEntry ns, TypeContainer parent, MemberName name,
-                             int mod, Attributes attrs, Location l)
+               public Class (NamespaceEntry ns, TypeContainer parent, MemberName name, int mod,
+                             Attributes attrs, Location l)
                        : base (ns, parent, name, attrs, Kind.Class, l)
                {
                        int accmods;
@@ -2905,6 +2924,10 @@ 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);
                }
 
@@ -2916,8 +2939,14 @@ namespace Mono.CSharp {
 
                public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
                {
-                       if (a.UsageAttribute != null)
+                       if (a.UsageAttribute != null) {
+                               if (ptype != TypeManager.attribute_type &&
+                                   !ptype.IsSubclassOf (TypeManager.attribute_type) &&
+                                   TypeBuilder.FullName != "System.Attribute") {
+                                       Report.Error (641, a.Location, "Attribute '{0}' is only valid on classes derived from System.Attribute", a.Name);
+                               }
                                attribute_usage = a.UsageAttribute;
+                       }
 
                        base.ApplyAttributeBuilder (a, cb);
                }
@@ -2928,11 +2957,6 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override void Register ()
-               {
-                       CheckDef (Parent.AddClass (this), Name, Location);
-               }
-
                public const TypeAttributes DefaultTypeAttributes =
                        TypeAttributes.AutoLayout | TypeAttributes.Class;
 
@@ -2981,11 +3005,6 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override void Register ()
-               {
-                       CheckDef (Parent.AddStruct (this), Name, Location);
-               }
-
                public const TypeAttributes DefaultTypeAttributes =
                        TypeAttributes.SequentialLayout |
                        TypeAttributes.Sealed |
@@ -3034,7 +3053,7 @@ namespace Mono.CSharp {
 
                public override void Register ()
                {
-                       CheckDef (Parent.AddInterface (this), Name, Location);
+                       Parent.AddInterface (this);
                }
 
                public override PendingImplementation GetPendingImplementations ()
@@ -3064,7 +3083,7 @@ namespace Mono.CSharp {
                public readonly Parameters Parameters;
                public readonly GenericMethod GenericMethod;
                public readonly DeclSpace ds;
-               protected Block block;
+               protected ToplevelBlock block;
                
                //
                // Parameters, cached for semantic analysis.
@@ -3072,14 +3091,14 @@ namespace Mono.CSharp {
                protected InternalParameters parameter_info;
                protected Type [] parameter_types;
 
-               // <summary>
-               //   This is set from TypeContainer.DefineMembers if this method overrides something.
-               // </summary>
-               public bool OverridesSomething;
-
                // 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, GenericMethod generic,
@@ -3115,7 +3134,7 @@ namespace Mono.CSharp {
                        }
                }
                
-               public Block Block {
+               public ToplevelBlock Block {
                        get {
                                return block;
                        }
@@ -3125,15 +3144,264 @@ namespace Mono.CSharp {
                        }
                }
 
+               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.ParentCache == 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.Equals (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;
+                                       }
+                               } else {
+                                       if (parent_method.IsAbstract && !IsInterface) {
+                                               Report.SymbolRelatedToPreviousError (parent_method);
+                                               Report.Error (533, Location, "'{0}' hides inherited abstract member", GetSignatureForError (Parent));
+                                               return false;
+                                       }
+                               }
+
+                               if (parent_method.IsSpecialName && !(this is PropertyBase)) {
+                                       Report.Error (561, Location, "'{0}': cannot override '{1}' because it is a special compiler-generated method", GetSignatureForError (Parent), TypeManager.GetFullNameSignature (parent_method));
+                                       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) {
+                                       EmitContext ec = new EmitContext (this.Parent, this.Parent, Location, null, null, ModFlags, false);
+                                       if (OptAttributes == null || !OptAttributes.Contains (TypeManager.obsolete_attribute_type, ec)) {
+                                               Report.SymbolRelatedToPreviousError (parent_method);
+                                               Report.Warning (672, 1, Location, "Member '{0}' overrides obsolete member. Add the Obsolete attribute to '{0}'", GetSignatureForError (Parent));
+                                       }
+                               }
+                               return true;
+                       }
+
+                       MemberInfo conflict_symbol = Parent.FindMemberWithSameName (Name, !(this is Property));
+                       if ((ModFlags & Modifiers.OVERRIDE) != 0) {
+                               if (conflict_symbol != null) {
+                                       Report.SymbolRelatedToPreviousError (conflict_symbol);
+                                       if (this is PropertyBase)
+                                               Report.Error (544, Location, "'{0}': cannot override because '{1}' is not a property", GetSignatureForError (Parent), TypeManager.GetFullNameSignature (conflict_symbol));
+                                       else
+                                               Report.Error (505, Location, "'{0}': cannot override because '{1}' is not a method", GetSignatureForError (Parent), TypeManager.GetFullNameSignature (conflict_symbol));
+                               } else
+                               Report.Error (115, Location, "'{0}': no suitable methods found to override", GetSignatureForError (Parent));
+                               return 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) == 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;
+
+                               if (!CheckAccessModifiers (thisp, parentp, parent_method)) {
+                                       Error_CannotChangeAccessModifiers (Parent, parent_method, name);
+                                       ok = false;
+                               }
+                       }
+
+                       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), TypeManager.CSharpSignature (parent_method));
+                               } else
+                                       Report.Warning (108, Location, "The keyword new is required on '{0}' because it hides inherited member", GetSignatureForError (Parent));
+                       }
+
+                       return ok;
+               }
+               
+               protected bool CheckAccessModifiers (MethodAttributes thisp, MethodAttributes parentp, MethodInfo base_method)
+               {
+                       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 != base_method.DeclaringType.Assembly){
+                                               //
+                                               // assemblies differ - report an error
+                                               //
+                                               
+                                               return false;
+                                       } else if (thisp != parentp) {
+                                               //
+                                               // same assembly, but other attributes differ - report an error
+                                               //
+                                               
+                                               return false;
+                                       };
+                               } else if ((thisp & MethodAttributes.Family) != MethodAttributes.Family) {
+                                       //
+                                       // if it's not "protected internal", it must be "protected"
+                                       //
+
+                                       return false;
+                               } else if (Parent.TypeBuilder.Assembly == base_method.DeclaringType.Assembly) {
+                                       //
+                                       // protected within the same assembly - an error
+                                       //
+                                       return false;
+                               } else if ((thisp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem)) != 
+                                          (parentp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem))) {
+                                       //
+                                       // protected ok, but other attributes differ - report an error
+                                       //
+                                       return false;
+                               }
+                               return true;
+                       } else {
+                               return (thisp == parentp);
+                       }
+               }
+               
+               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 + "'");
+               }
+
+               protected static string Error722 {
+                       get {
+                               return "'{0}': static types cannot be used as return types";
+                       }
+               }
+
+               /// <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 ()
                {
+                       EmitContext ec = ds.EmitContext;
+                       if (ec == null)
+                               throw new InternalErrorException ("DoDefineParameters invoked too early");
+
+                       bool old_unsafe = ec.InUnsafe;
+                       ec.InUnsafe = InUnsafe;
                        // Check if arguments were correct
-                       parameter_types = Parameters.GetParameterInfo (ds);
+                       parameter_types = Parameters.GetParameterInfo (ec);
+                       ec.InUnsafe = old_unsafe;
+
                        if ((parameter_types == null) ||
                            !CheckParameters (ds, parameter_types))
                                return false;
 
-                       parameter_info = new InternalParameters (ds, Parameters);
+                       TypeParameter[] tparam = ds.IsGeneric ? ds.TypeParameters : null;
+                       parameter_info = new InternalParameters (parameter_types, Parameters, tparam);
 
                        Parameter array_param = Parameters.ArrayParameter;
                        if ((array_param != null) &&
@@ -3175,12 +3443,8 @@ namespace Mono.CSharp {
                                        return false;
                                }
 
-                               if (gc.HasConstructor != ogc.HasConstructor) {
-                                       error_425 (ot, t, name);
-                                       return false;
-                               }
-
-                               if (ogc.HasClassConstraint != gc.HasClassConstraint) {
+                               if ((gc.Attributes != ogc.Attributes) ||
+                                   (gc.HasClassConstraint != ogc.HasClassConstraint)) {
                                        error_425 (ot, t, name);
                                        return false;
                                }
@@ -3209,7 +3473,7 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               protected override string[] ValidAttributeTargets {
+               public override string[] ValidAttributeTargets {
                        get {
                                return attribute_targets;
                        }
@@ -3219,78 +3483,84 @@ namespace Mono.CSharp {
                {
                        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;
                        }
 
                        if (Parameters.HasArglist) {
-                               // "Methods with variable arguments are not CLS-compliant"
-                               Report.Error_T (3000, Location);
+                               Report.Error (3000, Location, "Methods with variable arguments are not CLS-compliant");
                        }
 
-                       AttributeTester.AreParametersCompliant (Parameters.FixedParameters, Location);
-
                        if (!AttributeTester.IsClsCompliant (MemberType)) {
-                               Report.Error_T (3002, Location, GetSignatureForError ());
+                               if ((this is Property) || (this is Indexer))
+                                       Report.Error (3003, Location, "Type of `{0}' is not CLS-compliant",
+                                                     GetSignatureForError ());
+                               else
+                                       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)
+               bool MayUnify (MethodCore first, MethodCore second)
                {
-                       if ((method == this) || (method.Name != Name))
-                               return false;
+                       int a_type_params = 0;
+                       if (first.GenericMethod != null)
+                               a_type_params = first.GenericMethod.CountTypeParameters;
 
-                       Type[] param_types = method.ParameterTypes;
-                       if (param_types == null)
-                               return false;
+                       int b_type_params = 0;
+                       if (second.GenericMethod != null)
+                               b_type_params = second.GenericMethod.CountTypeParameters;
 
-                       if (param_types.Length != ParameterTypes.Length)
+                       if (a_type_params != b_type_params)
                                return false;
 
-                       int type_params = 0;
-                       if (GenericMethod != null)
-                               type_params = GenericMethod.CountTypeParameters;
-
-                       int m_type_params = 0;
-                       if (method.GenericMethod != null)
-                               m_type_params = method.GenericMethod.CountTypeParameters;
+                       Type[] class_infered, method_infered;
+                       if (Parent.CountTypeParameters > 0)
+                               class_infered = new Type [Parent.CountTypeParameters];
+                       else
+                               class_infered = null;
 
-                       if (type_params != m_type_params)
-                               return false;
+                       if (a_type_params > 0)
+                               method_infered = new Type [a_type_params];
+                       else
+                               method_infered = null;
 
-                       bool equal = true;
-                       bool may_unify;
+                       return TypeManager.MayBecomeEqualGenericInstances (
+                               first.ParameterTypes, second.ParameterTypes, class_infered, method_infered);
+               }
 
-                       Type[] infered_types;
-                       if (type_params > 0)
-                               infered_types = new Type [type_params];
-                       else
-                               infered_types = null;
+               protected bool IsDuplicateImplementation (MethodCore method)
+               {
+                       if ((method == this) ||
+                           (method.MemberName.GetTypeName () != MemberName.GetTypeName ()))
+                               return false;
 
-                       may_unify = Invocation.InferTypeArguments (
-                               param_types, ParameterTypes, ref infered_types);
+                       Type[] param_types = method.ParameterTypes;
+                       if (param_types == null)
+                               return false;
 
-                       if (!may_unify) {
-                               if (type_params > 0)
-                                       infered_types = new Type [type_params];
-                               else
-                                       infered_types = null;
+                       if (param_types.Length != ParameterTypes.Length)
+                               return false;
 
-                               may_unify = Invocation.InferTypeArguments (
-                                       ParameterTypes, param_types, ref infered_types);
-                       }
+                       bool equal = true;
+                       bool may_unify = MayUnify (this, method);
 
                        for (int i = 0; i < param_types.Length; i++) {
-                               Type a = param_types [i];
-                               Type b = ParameterTypes [i];
-
-                               if (a != b)
+                               if (param_types [i] != ParameterTypes [i])
                                        equal = false;
                        }
 
+                       // TODO: make operator compatible with MethodCore to avoid this
+                       if (this is Operator && method is Operator) {
+                               if (MemberType != method.MemberType)
+                                       equal = may_unify = false;
+                       }
+
                        if (equal) {
                                //
                                // Try to report 663: method only differs on out/ref
@@ -3306,35 +3576,46 @@ 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, "Type '{0}' already defines a member called '{1}' with the same parameter types", Parent.Name, Name);
                                return true;
                        } else if (may_unify) {
                                Report.Error (408, Location,
                                              "`{0}' cannot define overload members that " +
                                              "may unify for some type parameter substitutions",
-                                             tc.Name);
+                                             Parent.MemberName);
                                return true;
                        }
 
                        return false;
                }
 
-               public CallingConventions GetCallingConvention (bool is_class)
+               //
+               // Returns a string that represents the signature for this 
+               // member which should be used in XML documentation.
+               //
+               public override string GetDocCommentName (DeclSpace ds)
                {
-                       CallingConventions cc = 0;
-                       
-                       cc = Parameters.GetCallingConvention ();
+                       return DocUtil.GetMethodDocCommentName (this, ds);
+               }
 
-                       if (is_class)
-                               if ((ModFlags & Modifiers.STATIC) == 0)
-                                       cc |= CallingConventions.HasThis;
+               //
+               // Raised (and passed an XmlElement that contains the comment)
+               // when GenerateDocComment is writing documentation expectedly.
+               //
+               // FIXME: with a few effort, it could be done with XmlReader,
+               // that means removal of DOM use.
+               //
+               internal override void OnGenerateDocComment (DeclSpace ds, XmlElement el)
+               {
+                       DocUtil.OnMethodGenerateDocComment (this, ds, el);
+               }
 
-                       // FIXME: How is `ExplicitThis' used in C#?
-                       
-                       return cc;
+               //
+               //   Represents header string for documentation comment.
+               //
+               public override string DocCommentHeader {
+                       get { return "M:"; }
                }
 
                protected override void VerifyObsoleteAttribute()
@@ -3419,6 +3700,7 @@ namespace Mono.CSharp {
                public MethodBuilder MethodBuilder;
                public MethodData MethodData;
                ReturnParameter return_attributes;
+               ListDictionary declarative_security;
 
                /// <summary>
                ///   Modifiers allowed in a class declaration
@@ -3456,22 +3738,15 @@ namespace Mono.CSharp {
 
                public override AttributeTargets AttributeTargets {
                        get {
-                               return AttributeTargets.Method | AttributeTargets.ReturnValue;
+                               return AttributeTargets.Method;
                        }
                }
                
-               //
-               // 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 string GetSignatureForError()
                {
+                       if (MethodBuilder == null) {
+                               return GetSignatureForError (Parent);
+                       }
                        return TypeManager.CSharpSignature (MethodBuilder);
                }
 
@@ -3480,6 +3755,10 @@ namespace Mono.CSharp {
                /// </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) {
@@ -3504,14 +3783,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 &&
@@ -3536,7 +3807,7 @@ namespace Mono.CSharp {
 
                public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
                {
-                       if (a.Target == "return") {
+                       if (a.Target == AttributeTargets.ReturnValue) {
                                if (return_attributes == null)
                                        return_attributes = new ReturnParameter (MethodBuilder, Location);
 
@@ -3551,135 +3822,95 @@ namespace Mono.CSharp {
                        if (a.Type == TypeManager.dllimport_type) {
                                const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
                                if ((ModFlags & extern_static) != extern_static) {
-                                       //"The DllImport attribute must be specified on a method marked `static' and `extern'"
-                                       Report.Error_T (601, a.Location);
+                                       Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
                                }
 
                                return;
                        }
 
+                       if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
+                               if (declarative_security == null)
+                                       declarative_security = new ListDictionary ();
+                               a.ExtractSecurityPermissionSet (declarative_security);
+                               return;
+                       }
+
                        if (a.Type == TypeManager.conditional_attribute_type) {
                                if (IsOperator || IsExplicitImpl) {
-                                       // Conditional not valid on '{0}' because it is a destructor, operator, or explicit interface implementation
-                                       Report.Error_T (577, Location, GetSignatureForError ());
+                                       Report.Error (577, Location, "Conditional not valid on '{0}' because it is a destructor, operator, or explicit interface implementation", GetSignatureForError ());
                                        return;
                                }
 
                                if (ReturnType != TypeManager.void_type) {
-                                       // Conditional not valid on '{0}' because its return type is not void
-                                       Report.Error_T (578, Location, GetSignatureForError ());
+                                       Report.Error (578, Location, "Conditional not valid on '{0}' because its return new ErrorData ( type is not void", GetSignatureForError ());
                                        return;
                                }
 
                                if ((ModFlags & Modifiers.OVERRIDE) != 0) {
-                                       // Conditional not valid on '{0}' because it is an override method
-                                       Report.Error_T (243, Location, GetSignatureForError ());
+                                       Report.Error (243, Location, "Conditional not valid on '{0}' because it is an override method", GetSignatureForError ());
                                        return;
                                }
 
                                if (IsInterface) {
-                                       // Conditional not valid on interface members
-                                       Report.Error_T (582, Location);
+                                       Report.Error (582, Location, "Conditional not valid on interface members");
                                        return;
                                }
 
                                if (MethodData.IsImplementing) {
-                                       // Conditional member '{0}' cannot implement interface member
-                                       Report.Error_T (629, Location, GetSignatureForError ());
+                                       Report.Error (629, Location, "Conditional member '{0}' cannot implement interface member", GetSignatureForError ());
                                        return;
                                }
+
+                               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;
+                                       }
+                               }
                        }
 
                        MethodBuilder.SetCustomAttribute (cb);
                }
 
-               //
-               // Checks our base implementation if any
-               //
-               protected override bool CheckBase ()
+               protected override bool CheckForDuplications ()
                {
-                       base.CheckBase ();
-                       
-                       // Check whether arguments were correct.
-                       if (!DoDefineParameters ())
-                               return false;
+                       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;
+                               }
+                       }
 
-                       MethodSignature ms = new MethodSignature (Name, null, ParameterTypes);
-                       if (IsOperator) {
-                               flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
-                       } else {
-                               //
-                               // Check in our class for dups
-                               //
-                               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 (Parent, 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;
                                }
                        }
 
-
-                       //
-                       // 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 = Parent.TypeBuilder.BaseType;
-
-                       // 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)Parent).Parent.MemberCache.FindMemberToOverride (
-                                               Parent.TypeBuilder, Name, ParameterTypes, false);
-                               }
-                               
-                               if (parent_method != null) {
-                                       string name = parent_method.DeclaringType.Name + "." +
-                                               parent_method.Name;
-
-                                       if (!CheckMethodAgainstBase (Parent, flags, parent_method, name))
+                       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;
+                               }
+                       }
 
-                                       if ((ModFlags & Modifiers.NEW) == 0) {
-                                               Type parent_ret = TypeManager.TypeToCoreType (
-                                                       parent_method.ReturnType);
-
-                                               if (!parent_ret.Equals (MemberType)) {
-                                                       Report.Error (
-                                                               508, Location, Parent.MakeName (Name) + ": cannot " +
-                                                               "change return type when overriding " +
-                                                               "inherited member " + name);
-                                                       return false;
-                                               }
-                                       }
-
-                                       ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (parent_method);
-                                       if (oa != null) {
-                                               Report.SymbolRelatedToPreviousError (parent_method);
-                                               Report.Warning_T (672, Location, GetSignatureForError (Parent));
-                                       }
-                               } else {
-                                       if (!OverridesSomething && ((ModFlags & Modifiers.NEW) != 0))
-                                               WarningNotHiding (Parent);
-
-                                       if ((ModFlags & Modifiers.OVERRIDE) != 0){
-                                               Report.Error (115, Location,
-                                                             Parent.MakeName (Name) +
-                                                             " no suitable methods found to override");
-                                       }
+                       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 (Parent);
+                       }
 
                        return true;
                }
@@ -3694,8 +3925,9 @@ namespace Mono.CSharp {
 
                        MethodBuilder mb = null;
                        if (GenericMethod != null) {
-                               mb = Parent.TypeBuilder.DefineGenericMethod (Name, flags);
-                               if (!GenericMethod.Define (mb))
+                               string mname = MemberName.GetMethodName ();
+                               mb = Parent.TypeBuilder.DefineGenericMethod (mname, flags);
+                               if (!GenericMethod.Define (mb, ReturnType))
                                        return false;
                        }
 
@@ -3705,8 +3937,11 @@ namespace Mono.CSharp {
                        if (!CheckBase ())
                                return false;
 
+                       if (IsOperator)
+                               flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
+
                        MethodData = new MethodData (this, ParameterInfo, ModFlags, flags,
-                                                    this, mb, GenericMethod);
+                                                    this, mb, GenericMethod, parent_method);
 
                        if (!MethodData.Define (Parent))
                                return false;
@@ -3747,8 +3982,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;
@@ -3761,18 +4003,47 @@ namespace Mono.CSharp {
                {
                        MethodData.Emit (Parent, this);
                        base.Emit ();
+
+                       if (declarative_security != null) {
+                               foreach (DictionaryEntry de in declarative_security) {
+                                       MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
+                               }
+                       }
+
                        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.ParentCache.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)
+               {
+                       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 ()
                {
-                       return IsIdentifierAndParamClsCompliant (ds, Name, MethodBuilder, parameter_types);
+                       ModFlags |= Modifiers.METHOD_YIELDS;
                }
 
                #region IMethodData Members
@@ -3799,9 +4070,9 @@ namespace Mono.CSharp {
                        }
                }
 
-               public string MethodName {
+               public MemberName MethodName {
                        get {
-                               return ShortName;
+                               return MemberName;
                        }
                }
 
@@ -3872,6 +4143,7 @@ namespace Mono.CSharp {
                                return GenericMethod;
                        }
                }
+
                #endregion
        }
 
@@ -3956,7 +4228,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;
                        }
                        
@@ -4067,9 +4339,10 @@ namespace Mono.CSharp {
                }
        }
        
-       public class Constructor : MethodCore {
+       public class Constructor : MethodCore, IMethodData {
                public ConstructorBuilder ConstructorBuilder;
                public ConstructorInitializer Initializer;
+               ListDictionary declarative_security;
 
                // <summary>
                //   Modifiers allowed for a constructor.
@@ -4098,6 +4371,9 @@ namespace Mono.CSharp {
 
                public override string GetSignatureForError()
                {
+                       if (ConstructorBuilder == null)
+                               return GetSignatureForError (Parent);
+
                        return TypeManager.CSharpSignature (ConstructorBuilder);
                }
 
@@ -4132,41 +4408,63 @@ namespace Mono.CSharp {
 
                public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
                {
+                       if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
+                               if (declarative_security == null) {
+                                       declarative_security = new ListDictionary ();
+                               }
+                               a.ExtractSecurityPermissionSet (declarative_security);
+                               return;
+                       }
+
                        ConstructorBuilder.SetCustomAttribute (cb);
                }
 
-               protected override bool CheckBase ()
+               protected override bool CheckForDuplications ()
                {
-                       base.CheckBase ();
+                       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 ()
+               {
                        // Check whether arguments were correct.
                        if (!DoDefineParameters ())
                                return false;
                        
+                       // TODO: skip the rest for generated ctor
                        if ((ModFlags & Modifiers.STATIC) != 0)
                                return true;
                        
-                       if (Parent.Kind == Kind.Struct && ParameterTypes.Length == 0) {
+                       if (!CheckForDuplications ())
+                               return false;
+
+                       if (Parent.Kind == Kind.Struct) {
+                               if (ParameterTypes.Length == 0) {
                                Report.Error (568, Location, 
                                        "Structs can not contain explicit parameterless " +
                                        "constructors");
                                return false;
                        }
                                
-                       //
-                       // Check in our class for dups
-                       //
-                       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 (Parent, m))
+                               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;
                }
                
@@ -4203,12 +4501,14 @@ namespace Mono.CSharp {
                                return false;
 
                        ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
-                               ca, GetCallingConvention (Parent.Kind == Kind.Class),
+                               ca, CallingConventions,
                                ParameterTypes);
 
                        if ((ModFlags & Modifiers.UNSAFE) != 0)
                                ConstructorBuilder.InitLocals = false;
                        
+                       TypeManager.AddMethod (ConstructorBuilder, this);
+
                        //
                        // HACK because System.Reflection.Emit is lame
                        //
@@ -4222,8 +4522,7 @@ namespace Mono.CSharp {
                //
                public override void Emit ()
                {
-                       ILGenerator ig = ConstructorBuilder.GetILGenerator ();
-                       EmitContext ec = new EmitContext (Parent, Location, ig, null, ModFlags, true);
+                       EmitContext ec = CreateEmitContext (null, null);
 
                        //
                        // extern methods have no bodies
@@ -4280,7 +4579,10 @@ namespace Mono.CSharp {
                                }
                        }
                        if (Initializer != null) {
-                               Initializer.CheckObsoleteAttribute (Parent, Location);
+                               if (GetObsoleteAttribute (Parent) == null && Parent.GetObsoleteAttribute (Parent.Parent) == null)
+                                       Initializer.CheckObsoleteAttribute (Parent, Location);
+                               else
+                                       ec.TestObsoleteMethodUsage = false;
                                Initializer.Emit (ec);
                        }
                        
@@ -4303,75 +4605,101 @@ namespace Mono.CSharp {
 
                        base.Emit ();
 
+                       if (declarative_security != null) {
+                               foreach (DictionaryEntry de in declarative_security) {
+                                       ConstructorBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
+                               }
+                       }
+
                        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];
+                       return null;
+               }
                                                
-                               if (c == this || c.ParameterTypes.Length == 0)
-                                       continue;
+               protected override bool VerifyClsCompliance (DeclSpace ds)
+               {
+                       if (!base.VerifyClsCompliance (ds) || !IsExposedFromAssembly (ds)) {
+                               return false;
+                       }
 
-                               if (!c.IsClsCompliaceRequired (ds))
-                                       continue;
+                       if (parameter_types.Length > 0) {
+                               ArrayList al = (ArrayList)ds.MemberCache.Members [".ctor"];
+                               if (al.Count > 3)
+                                       ds.MemberCache.VerifyClsParameterConflict (al, this, ConstructorBuilder);
                                
-                               if (!AttributeTester.AreOverloadedMethodParamsClsCompliant (parameter_types, c.ParameterTypes)) {
-                                       Report.Error_T (3006, Location, GetSignatureForError ());
-                                       return false;
+                               if (ds.TypeBuilder.IsSubclassOf (TypeManager.attribute_type)) {
+                                       foreach (Type param in parameter_types) {
+                                               if (param.IsArray) {
+                                                       return true;
                                }
                        }
-
-                       if (tc.TypeBuilder.BaseType == null)
+                               }
+                       }
+                       has_compliant_args = true;
                                return true;
+               }
 
-                       DeclSpace temp_ds = TypeManager.LookupDeclSpace (tc.TypeBuilder.BaseType);
-                       if (temp_ds != null)
-                               return IsIdentifierClsCompliant (temp_ds);
+               #region IMethodData Members
 
-                       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;
+               public System.Reflection.CallingConventions CallingConventions {
+                       get {
+                               CallingConventions cc = Parameters.GetCallingConvention ();
 
-                       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 (Parent.Kind == Kind.Class)
+                                       if ((ModFlags & Modifiers.STATIC) == 0)
+                                               cc |= CallingConventions.HasThis;
 
-                               if (!AttributeTester.AreOverloadedMethodParamsClsCompliant (parameter_types, TypeManager.GetArgumentTypes (ci))) {
-                                       Report.Error_T (3006, Location, GetSignatureForError ());
-                                       return false;
+                               // FIXME: How is `ExplicitThis' used in C#?
+                       
+                               return cc;
                                }
                        }
                        
-                       return true;
+               public new Location Location {
+                       get {
+                               return base.Location;
+                       }
                }
 
-               protected override bool VerifyClsCompliance (DeclSpace ds)
-               {
-                       if (!base.VerifyClsCompliance (ds) || !IsExposedFromAssembly (ds)) {
-                               return false;
+               public MemberName MethodName {
+                       get {
+                               return MemberName;
                        }
+               }
                        
-                       if (ds.TypeBuilder.IsSubclassOf (TypeManager.attribute_type)) {
-                               foreach (Type param in parameter_types) {
-                                       if (param.IsArray) {
-                                               return false;
-                                       }
-                               }
+               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;
+               }
+
+               GenericMethod IMethodData.GenericMethod {
+                       get {
+                               return null;
                        }
-                       has_compliant_args = true;
-                       return true;
                }
 
+               #endregion
        }
 
        /// <summary>
@@ -4381,18 +4709,19 @@ namespace Mono.CSharp {
        {
                CallingConventions CallingConventions { get; }
                Location Location { get; }
-               string MethodName { get; }
+               MemberName MethodName { get; }
                Type[] ParameterTypes { get; }
                Type ReturnType { get; }
                GenericMethod GenericMethod { get; }
 
                Attributes OptAttributes { get; }
-               Block Block { get; }
+               ToplevelBlock Block { get; }
 
                EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig);
                ObsoleteAttribute GetObsoleteAttribute ();
                string GetSignatureForError (TypeContainer tc);
                bool IsExcluded (EmitContext ec);
+               bool IsClsCompliaceRequired (DeclSpace ds);
        }
 
        //
@@ -4420,6 +4749,7 @@ namespace Mono.CSharp {
                protected int modifiers;
                protected MethodAttributes flags;
                protected Type declaring_type;
+               protected MethodInfo parent_method;
 
                EmitContext ec;
 
@@ -4450,11 +4780,12 @@ namespace Mono.CSharp {
                public MethodData (MemberBase member, InternalParameters parameters,
                                   int modifiers, MethodAttributes flags, 
                                   IMethodData method, MethodBuilder builder,
-                                  GenericMethod generic)
+                                  GenericMethod generic, MethodInfo parent_method)
                        : this (member, parameters, modifiers, flags, method)
                {
                        this.builder = builder;
                        this.GenericMethod = generic;
+                       this.parent_method = parent_method;
                }
 
                static string RemoveArity (string name)
@@ -4469,8 +4800,10 @@ namespace Mono.CSharp {
                                }
 
                                sb.Append (name.Substring (start, pos-start));
-                               while (Char.IsNumber (name [++pos]))
-                                       ;
+
+                               pos++;
+                               while ((pos < name.Length) && Char.IsNumber (name [pos]))
+                                       pos++;
 
                                start = pos;
                        }
@@ -4481,15 +4814,16 @@ namespace Mono.CSharp {
                public bool Define (TypeContainer container)
                {
                        MethodInfo implementing = null;
-                       string prefix;
 
+                       string prefix;
                        if (member.IsExplicitImpl)
-                               prefix = RemoveArity (member.InterfaceType.FullName) + ".";
+                               prefix = member.InterfaceType.FullName + ".";
                        else
                                prefix = "";
 
-                       string name = method.MethodName;
+                       string name = method.MethodName.Basename;
                        string method_name = prefix + name;
+
                        Type[] ParameterTypes = method.ParameterTypes;
 
                        if (container.Pending != null){
@@ -4500,9 +4834,12 @@ namespace Mono.CSharp {
                                        implementing = container.Pending.IsInterfaceMethod (
                                                member.InterfaceType, name, method.ReturnType, ParameterTypes);
 
-                               if (member.InterfaceType != null && implementing == null){
-                                       Report.Error (539, method.Location, "'{0}' in explicit interface declaration is not an interface", method_name);
-                                       return false;
+                               if (member.InterfaceType != null){
+                                       if (implementing == null){
+                                               Report.Error (539, method.Location,
+                                                             "'{0}' in explicit interface declaration is not an interface", method_name);
+                                               return false;
+                                       }
                                }
                        }
 
@@ -4570,12 +4907,6 @@ 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;
                        }
 
@@ -4586,13 +4917,8 @@ namespace Mono.CSharp {
                        if (builder == null)
                                return false;
 
-                       if (GenericMethod != null) {
-                               if (!GenericMethod.DefineType (ec, builder))
-                                       return false;
-                       }
-
                        if (container.CurrentType != null)
-                               declaring_type = container.CurrentType.ResolveType (ec);
+                               declaring_type = container.CurrentType;
                        else
                                declaring_type = container.TypeBuilder;
 
@@ -4606,7 +4932,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,
@@ -4618,17 +4944,20 @@ 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.RegisterMethod (builder, ParameterInfo, ParameterTypes);
                        TypeManager.AddMethod (builder, method);
 
+                       if (GenericMethod != null) {
+                               bool is_override = member.IsExplicitImpl |
+                                       ((modifiers & Modifiers.OVERRIDE) != 0);
+
+                               if (implementing != null)
+                                       parent_method = implementing;
+
+                               if (!GenericMethod.DefineType (ec, builder, parent_method, is_override))
+                                       return false;
+                       }
+
                        return true;
                }
 
@@ -4657,8 +4986,7 @@ namespace Mono.CSharp {
                                // We are more strict than Microsoft and report CS0626 like error
                                if (method.OptAttributes == null ||
                                        !method.OptAttributes.Contains (TypeManager.methodimpl_attr_type, ec)) {
-                                       //"Method, operator, or accessor '{0}' is marked external and has no attributes on it. Consider adding a DllImport attribute to specify the external implementation"
-                                       Report.Error_T (626, method.Location, method.GetSignatureForError (container));
+                                       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;
                                }
                        }
@@ -4684,6 +5012,9 @@ namespace Mono.CSharp {
                        else
                                ec = method.CreateEmitContext (container, null);
 
+                       if (method.GetObsoleteAttribute () != null || container.GetObsoleteAttribute (container.Parent) != null)
+                               ec.TestObsoleteMethodUsage = false;
+
                        Location loc = method.Location;
                        Attributes OptAttributes = method.OptAttributes;
 
@@ -4693,7 +5024,8 @@ namespace Mono.CSharp {
                        if (member is MethodCore)
                                ((MethodCore) member).Parameters.LabelParameters (ec, MethodBuilder, loc);
                         
-                       Block block = method.Block;
+                       SymbolWriter sw = CodeGen.SymbolWriter;
+                       ToplevelBlock block = method.Block;
                        
                        //
                        // abstract or extern methods have no bodies
@@ -4749,7 +5081,7 @@ namespace Mono.CSharp {
                                source.CloseMethod ();
                }
 
-               void EmitDestructor (EmitContext ec, Block block)
+               void EmitDestructor (EmitContext ec, ToplevelBlock block)
                {
                        ILGenerator ig = ec.ig;
                        
@@ -4795,8 +5127,7 @@ namespace Mono.CSharp {
                public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
                {
                        if (a.Type == TypeManager.conditional_attribute_type) {
-                               // Conditional not valid on '{0}' because it is a destructor, operator, or explicit interface implementation
-                               Report.Error_T (577, Location, GetSignatureForError ());
+                               Report.Error (577, Location, "Conditional not valid on '{0}' because it is a destructor, operator, or explicit interface implementation", GetSignatureForError ());
                                return;
                        }
 
@@ -4830,7 +5161,7 @@ namespace Mono.CSharp {
                //
                // The name of the interface we are explicitly implementing
                //
-               public Expression ExplicitInterfaceName = null;
+               public MemberName ExplicitInterfaceName = null;
 
                //
                // Whether this is an interface member.
@@ -4842,16 +5173,6 @@ 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
                //
@@ -4863,180 +5184,35 @@ namespace Mono.CSharp {
                        explicit_mod_flags = mod;
                        Type = type;
                        ModFlags = Modifiers.Check (allowed_mod, mod, def_mod, loc);
-               }
-
-               protected virtual bool CheckBase ()
-               {
-                       if ((Parent.Kind == Kind.Struct) || (RootContext.WarningLevel > 3)){
-                               if ((ModFlags & Modifiers.PROTECTED) != 0 && (Parent.ModFlags & Modifiers.SEALED) != 0){
-                                       if (Parent.Kind == Kind.Struct){
-                                               Report.Error (666, Location, "Protected member in struct declaration");
-                                               return false;
-                                       } else
-                                               Report.Warning (628, Location, "Member " + Parent.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");
-                                                          
-               }
-
-               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 + "'");
-               }
-
-               protected abstract bool CheckGenericOverride (MethodInfo method, string 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)
-               {
-                       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 constraints match when overriding a
-                               // generic method.
-                               //
-
-                               if (!CheckGenericOverride (mb, name))
-                                       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;
-                                       }
-                               }
+                       // Check for explicit interface implementation
+                       if (MemberName.Left != null) {
+                               ExplicitInterfaceName = MemberName.Left;
+                               ShortName = MemberName.Name;
+                               IsExplicitImpl = true;
+                       } else
+                               ShortName = Name;
+               }
+
+               protected virtual bool CheckBase ()
+               {
+                       if ((ModFlags & Modifiers.PROTECTED) != 0 && Parent.Kind == Kind.Struct) {
+                               Report.Error (666, Location, "Protected member in struct declaration");
+                               return 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;
-                                       }
-                               }
+                       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 ok;
+                       return true;
                }
 
+               protected abstract bool CheckGenericOverride (MethodInfo method, string name);
+
                protected virtual bool CheckParameters (DeclSpace ds, Type [] parameters)
                {
                        bool error = false;
@@ -5082,13 +5258,17 @@ namespace Mono.CSharp {
 
                protected virtual bool DoDefineBase ()
                {
+                       EmitContext ec = Parent.EmitContext;
+                       if (ec == null)
+                               throw new InternalErrorException ("MemberBase.DoDefine called too early");
+
                        if (Name == null)
-                               Name = "this";
+                               throw new InternalErrorException ();
 
                        if (IsInterface) {
                                ModFlags = Modifiers.PUBLIC |
                                        Modifiers.ABSTRACT |
-                                       Modifiers.VIRTUAL | (ModFlags & Modifiers.UNSAFE);
+                                       Modifiers.VIRTUAL | (ModFlags & Modifiers.UNSAFE) | (ModFlags & Modifiers.NEW);
 
                                flags = MethodAttributes.Public |
                                        MethodAttributes.Abstract |
@@ -5107,11 +5287,23 @@ namespace Mono.CSharp {
 
                protected virtual bool DoDefine (DeclSpace decl)
                {
+                       EmitContext ec = decl.EmitContext;
+                       if (ec == null)
+                               throw new InternalErrorException ("MemberBase.DoDefine called too early");
+
+                       ec.InUnsafe = InUnsafe;
+
                        // Lookup Type, verify validity
-                       MemberType = decl.ResolveType (Type, false, Location);
-                       if (MemberType == null)
+                       bool old_unsafe = ec.InUnsafe;
+                       ec.InUnsafe = InUnsafe;
+                       TypeExpr texpr = Type.ResolveAsTypeTerminal (ec);
+                       ec.InUnsafe = old_unsafe;
+
+                       if (texpr == null)
                                return false;
 
+                       MemberType = texpr.Type;
+
                        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");
@@ -5131,8 +5323,8 @@ namespace Mono.CSharp {
                                                      "Inconsistent accessibility: indexer return type `" +
                                                      TypeManager.CSharpName (MemberType) + "' is less " +
                                                      "accessible than indexer `" + Name + "'");
-                               else if (this is Method) {
-                                       if (((Method) this).IsOperator)
+                               else if (this is MethodCore) {
+                                       if (this is Operator)
                                                Report.Error (56, Location,
                                                              "Inconsistent accessibility: return type `" +
                                                              TypeManager.CSharpName (MemberType) + "' is less " +
@@ -5142,63 +5334,60 @@ namespace Mono.CSharp {
                                                              "Inconsistent accessibility: return type `" +
                                                              TypeManager.CSharpName (MemberType) + "' is less " +
                                                              "accessible than method `" + Name + "'");
-                               } else
+                               } else {
                                        Report.Error (52, Location,
                                                      "Inconsistent accessibility: field type `" +
                                                      TypeManager.CSharpName (MemberType) + "' is less " +
                                                      "accessible than field `" + Name + "'");
+                               }
                                return false;
                        }
 
                        if (MemberType.IsPointer && !UnsafeOK (Parent))
                                return false;
 
-                       //
-                       // Check for explicit interface implementation
-                       //
-                       if (MemberName.Left != null) {
-                               ExplicitInterfaceName = MemberName.Left.GetTypeExpression (Location);
-                               ShortName = MemberName.Name;
-                       } else
-                               ShortName = Name;
-
-                       if (ExplicitInterfaceName != null) {
-                               InterfaceType = Parent.ResolveType (
-                                       ExplicitInterfaceName, false, Location);
-                               if (InterfaceType == null)
+                       if (IsExplicitImpl) {
+                               Expression expr = ExplicitInterfaceName.GetTypeExpression (Location);
+                               TypeExpr iface_texpr = expr.ResolveAsTypeTerminal (ec);
+                               if (iface_texpr == null)
                                        return false;
 
-                               if (InterfaceType.IsClass) {
-                                       Report.Error (538, Location, "'{0}' in explicit interface declaration is not an interface", ExplicitInterfaceName);
+                               InterfaceType = iface_texpr.Type;
+
+                               if (!InterfaceType.IsInterface) {
+                                       Report.Error (538, Location, "'{0}' in explicit interface declaration is not an interface", TypeManager.CSharpName (InterfaceType));
                                        return false;
                                }
 
-                               // Compute the full name that we need to export.
-                               Name = InterfaceType.FullName + "." + ShortName;
-                               
                                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;
                }
 
                /// <summary>
-               /// Use this method when MethodBuilder is null
+               /// The name of the member can be changed during definition (see IndexerName attribute)
                /// </summary>
-               public virtual string GetSignatureForError (TypeContainer tc)
+               protected virtual void UpdateMemberName ()
+               {
+                       MemberName.Name = ShortName;
+               }
+
+               public override string GetSignatureForError (TypeContainer tc)
                {
-                       return String.Concat (tc.Name, '.', Name);
+                       return String.Concat (tc.Name, '.', base.GetSignatureForError (tc));
                }
 
-               protected override bool IsIdentifierClsCompliant (DeclSpace ds)
+               protected bool IsTypePermitted ()
                {
-                       return IsIdentifierAndParamClsCompliant (ds, Name, 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;
+                       }
+                       return true;
                }
 
                protected override bool VerifyClsCompliance(DeclSpace ds)
@@ -5208,7 +5397,7 @@ 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;
                }
@@ -5228,10 +5417,19 @@ namespace Mono.CSharp {
                public Status status;
 
                [Flags]
-               public enum Status : byte { ASSIGNED = 1, USED = 2 }
+               public enum Status : byte {
+                       ASSIGNED = 1,
+                       USED = 2,
+                       HAS_OFFSET = 4          // Used by FieldMember.
+               }
 
                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
                //
@@ -5253,16 +5451,18 @@ namespace Mono.CSharp {
                public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
                {
                        if (a.Type == TypeManager.marshal_as_attr_type) {
-                               UnmanagedMarshal marshal = a.GetMarshal ();
+                               UnmanagedMarshal marshal = a.GetMarshal (this);
                                if (marshal != null) {
                                        FieldBuilder.SetMarshal (marshal);
+                               }
                                        return;
                                }
-                               Report.Warning_T (-24, a.Location);
+
+                       if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
+                               a.Error_InvalidSecurityParent ();
                                return;
                        }
 
-                       
                        FieldBuilder.SetCustomAttribute (cb);
                }
 
@@ -5309,6 +5509,31 @@ namespace Mono.CSharp {
                        return init_expr;
                }
 
+               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 (DeclSpace ds)
                {
                        if (!base.DoDefine (ds))
@@ -5319,22 +5544,18 @@ namespace Mono.CSharp {
                                              "Keyword 'void' cannot be used in this context");
                                return false;
                        }
-
-                       if (MemberType == TypeManager.arg_iterator_type || MemberType == TypeManager.typed_reference_type) {
-                               // "Field or property cannot be of type '{0}'";
-                               Report.Error_T (610, Location, TypeManager.CSharpName (MemberType));
-                               return false;
-                       }
-
                        return true;
                }
 
                public override string GetSignatureForError ()
                {
+                       if (FieldBuilder == null) {
+                               return base.GetSignatureForError (Parent);
+                       }
                        return TypeManager.GetFullNameSignature (FieldBuilder);
                }
 
-               protected override string[] ValidAttributeTargets {
+               public override string[] ValidAttributeTargets {
                        get {
                                return attribute_targets;
                        }
@@ -5350,7 +5571,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;
                }
@@ -5362,10 +5583,92 @@ namespace Mono.CSharp {
                }
        }
 
+       public abstract class FieldMember: FieldBase
+       {
+               
+
+               protected FieldMember (TypeContainer parent, Expression type, int mod,
+                       int allowed_mod, MemberName name, object init, Attributes attrs, Location loc)
+                       : base (parent, type, mod, allowed_mod, name, init, attrs, loc)
+               {
+               }
+
+               public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
+               {
+                       if (a.Type == TypeManager.field_offset_attribute_type)
+                       {
+                               status |= Status.HAS_OFFSET;
+
+                               if (!Parent.HasExplicitLayout) {
+                                       Report.Error (636, Location, "The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)");
+                                       return;
+                               }
+
+                               if ((ModFlags & Modifiers.STATIC) != 0 || this is Const) {
+                                       Report.Error (637, Location, "The FieldOffset attribute is not allowed on static or const fields");
+                                       return;
+                               }
+                       }
+                       base.ApplyAttributeBuilder (a, cb);
+               }
+
+
+               public override bool Define()
+               {
+                       EmitContext ec = Parent.EmitContext;
+                       if (ec == null)
+                               throw new InternalErrorException ("FieldMember.Define called too early");
+
+                       bool old_unsafe = ec.InUnsafe;
+                       ec.InUnsafe = InUnsafe;
+                       TypeExpr texpr = Type.ResolveAsTypeTerminal (ec);
+                       ec.InUnsafe = old_unsafe;
+                       if (texpr == null)
+                               return false;
+                       
+                       MemberType = texpr.Type;
+
+                       if (!CheckBase ())
+                               return false;
+                       
+                       if (!Parent.AsAccessible (MemberType, ModFlags)) {
+                               Report.Error (52, Location,
+                                       "Inconsistent accessibility: field type `" +
+                                       TypeManager.CSharpName (MemberType) + "' is less " +
+                                       "accessible than field `" + Name + "'");
+                               return false;
+                       }
+
+                       if (!IsTypePermitted ())
+                               return false;
+
+                       if (MemberType.IsPointer && !UnsafeOK (Parent))
+                               return false;
+
+                       return true;
+               }
+
+               public override void Emit ()
+               {
+                       if (Parent.HasExplicitLayout && ((status & Status.HAS_OFFSET) == 0) && (ModFlags & Modifiers.STATIC) == 0) {
+                               Report.Error (625, Location, "'{0}': Instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute.", GetSignatureForError ());
+                       }
+
+                       base.Emit ();
+               }
+
+               //
+               //   Represents header string for documentation comment.
+               //
+               public override string DocCommentHeader {
+                       get { return "F:"; }
+               }
+       }
+
        //
        // The Field class is used to represents class/struct fields during parsing.
        //
-       public class Field : FieldBase {
+       public class Field : FieldMember {
                // <summary>
                //   Modifiers allowed in a class declaration
                // </summary>
@@ -5389,24 +5692,9 @@ namespace Mono.CSharp {
 
                public override bool Define ()
                {
-                       MemberType = Parent.ResolveType (Type, false, Location);
-
-                       if (MemberType == null)
-                               return false;
-
-                       CheckBase ();
-                       
-                       if (!Parent.AsAccessible (MemberType, ModFlags)) {
-                               Report.Error (52, Location,
-                                             "Inconsistent accessibility: field type `" +
-                                             TypeManager.CSharpName (MemberType) + "' is less " +
-                                             "accessible than field `" + Name + "'");
+                       if (!base.Define ())
                                return false;
-                       }
 
-                       if (MemberType.IsPointer && !UnsafeOK (Parent))
-                               return false;
-                       
                        if (RootContext.WarningLevel > 1){
                                Type ptype = Parent.TypeBuilder.BaseType;
 
@@ -5498,40 +5786,74 @@ namespace Mono.CSharp {
                //
                // Null if the accessor is empty, or a Block if not
                //
-               public Block Block;
+               public const int AllowedModifiers = 
+                       Modifiers.PUBLIC |
+                       Modifiers.PROTECTED |
+                       Modifiers.INTERNAL |
+                       Modifiers.PRIVATE;
+               
+               public ToplevelBlock Block;
                public Attributes Attributes;
+               public Location Location;
+               public int ModFlags;
                
-               public Accessor (Block b, Attributes attrs)
+               public Accessor (ToplevelBlock b, int mod, Attributes attrs, Location loc)
                {
                        Block = b;
                        Attributes = attrs;
+                       Location = loc;
+                       ModFlags = Modifiers.Check (AllowedModifiers, mod, 0, 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: Attributable, IMethodData
-       {
+       public abstract class AbstractPropertyEventMethod: MemberCore, IMethodData {
                protected MethodData method_data;
-               protected Block block;
+               protected ToplevelBlock block;
+               protected ListDictionary declarative_security;
+
+               // 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;
+
+               protected readonly string prefix;
 
                ReturnParameter return_attributes;
 
-               public AbstractPropertyEventMethod ():
-                       base (null)
+               public AbstractPropertyEventMethod (MemberBase member, string prefix)
+                       : base (null, SetupName (prefix, member), null, member.Location)
                {
+                       this.prefix = prefix;
+                       IsDummy = true;
                }
 
-               public AbstractPropertyEventMethod (Accessor accessor):
-                       base (accessor.Attributes)
+               public AbstractPropertyEventMethod (MemberBase member, Accessor accessor,
+                                                   string prefix)
+                       : base (null, SetupName (prefix, member),
+                               accessor.Attributes, accessor.Location)
                {
+                       this.prefix = prefix;
                        this.block = accessor.Block;
                }
 
+               static MemberName SetupName (string prefix, MemberBase member)
+               {
+                       MemberName name = member.MemberName.Clone ();
+                       name.Name = prefix + member.ShortName;
+                       return name;
+               }
+
+               public void UpdateName (MemberBase member)
+               {
+                       MemberName.Name = prefix + member.ShortName;
+               }
+
                #region IMethodData Members
 
-               public Block Block {
+               public ToplevelBlock Block {
                        get {
                                return block;
                        }
@@ -5558,10 +5880,13 @@ namespace Mono.CSharp {
                        }
                }
 
+               public MemberName MethodName {
+                       get {
+                               return MemberName;
+                       }
+               }
+
                public abstract ObsoleteAttribute GetObsoleteAttribute ();
-               public abstract string GetSignatureForError (TypeContainer tc);
-               public abstract Location Location { get; }
-               public abstract string MethodName { get; }
                public abstract Type[] ParameterTypes { get; }
                public abstract Type ReturnType { get; }
                public abstract EmitContext CreateEmitContext(TypeContainer tc, ILGenerator ig);
@@ -5572,17 +5897,23 @@ namespace Mono.CSharp {
                {
                        if (a.Type == TypeManager.cls_compliant_attribute_type || a.Type == TypeManager.obsolete_attribute_type ||
                                        a.Type == TypeManager.conditional_attribute_type) {
-                               //"'{0}' is not valid on property or event accessors. It is valid on '{1}' declarations only"
-                               Report.Error_T (1667, a.Location, TypeManager.CSharpName (a.Type), a.GetValidTargets ());
+                               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.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
+                               if (declarative_security == null)
+                                       declarative_security = new ListDictionary ();
+                               a.ExtractSecurityPermissionSet (declarative_security);
                                return;
                        }
 
-                       if (a.Target == "method") {
+                       if (a.Target == AttributeTargets.Method) {
                                method_data.MethodBuilder.SetCustomAttribute (cb);
                                return;
                        }
 
-                       if (a.Target == "return") {
+                       if (a.Target == AttributeTargets.ReturnValue) {
                                if (return_attributes == null)
                                        return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
 
@@ -5598,11 +5929,66 @@ namespace Mono.CSharp {
                        System.Diagnostics.Debug.Fail ("You forgot to define special attribute target handling");
                }
 
+               public override bool Define()
+               {
+                       throw new NotSupportedException ();
+               }
+
                public virtual void Emit (TypeContainer container)
                {
                        method_data.Emit (container, this);
+
+                       if (declarative_security != null) {
+                               foreach (DictionaryEntry de in declarative_security) {
+                                       method_data.MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
+                               }
+                       }
+
                        block = null;
                }
+
+               public override bool IsClsCompliaceRequired(DeclSpace ds)
+               {
+                       return false;
+               }
+
+               public bool IsDuplicateImplementation (MethodCore method)
+               {
+                       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;
+
+                       Report.SymbolRelatedToPreviousError (method);
+                       Report.Error (111, Location, "Type '{0}' already defines a member called '{1}' with " +
+                                     "the same parameter types", Parent.Name, Name);
+                       return true;
+               }
+
+               public new Location Location { 
+                       get {
+                               return base.Location;
+                       }
+               }
+
+               //
+               //   Represents header string for documentation comment.
+               //
+               public override string DocCommentHeader {
+                       get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
+               }
+
+               protected override void VerifyObsoleteAttribute()
+               {
+               }
+
        }
 
        //
@@ -5615,14 +6001,21 @@ namespace Mono.CSharp {
                {
                        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, this);
+                               base.Define (container);
+                               
+                               method_data = new MethodData (method, method.ParameterInfo, ModFlags, flags, this);
 
                                if (!method_data.Define (container))
                                        return null;
@@ -5635,20 +6028,13 @@ namespace Mono.CSharp {
                                return String.Concat (base.GetSignatureForError (tc), ".get");
                        }
 
-                       public override string MethodName 
-                       {
-                               get {
-                                       return "get_" + method.ShortName;
-                               }
-                       }
-
                        public override Type ReturnType {
                                get {
                                        return method.MemberType;
                                }
                        }
 
-                       protected override string[] ValidAttributeTargets {
+                       public override string[] ValidAttributeTargets {
                                get {
                                        return attribute_targets;
                                }
@@ -5660,14 +6046,19 @@ namespace Mono.CSharp {
                        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 == "param") {
+                               if (a.Target == AttributeTargets.Parameter) {
                                        if (param_attr == null)
                                                param_attr = new ImplicitParameter (method_data.MethodBuilder);
 
@@ -5678,17 +6069,23 @@ namespace Mono.CSharp {
                                base.ApplyAttributeBuilder (a, cb);
                        }
 
-                       protected virtual InternalParameters GetParameterInfo (TypeContainer container)
+                       protected virtual InternalParameters GetParameterInfo (EmitContext ec)
                        {
                                Parameter [] parms = new Parameter [1];
                                parms [0] = new Parameter (method.Type, "value", Parameter.Modifier.NONE, null);
-                               return new InternalParameters (
-                                       container, new Parameters (parms, null, method.Location));
+                               Parameters parameters = new Parameters (parms, null, method.Location);
+                               Type [] types = parameters.GetParameterInfo (ec);
+                               return new InternalParameters (types, parameters);
                        }
 
                        public override MethodBuilder Define(TypeContainer container)
                        {
-                               method_data = new MethodData (method, GetParameterInfo (container), method.ModFlags, method.flags, this);
+                               if (container.EmitContext == null)
+                                       throw new InternalErrorException ("SetMethod.Define called too early");
+                                       
+                               base.Define (container);
+                               
+                               method_data = new MethodData (method, GetParameterInfo (container.EmitContext), ModFlags, flags, this);
 
                                if (!method_data.Define (container))
                                        return null;
@@ -5701,12 +6098,6 @@ namespace Mono.CSharp {
                                return String.Concat (base.GetSignatureForError (tc), ".set");
                        }
 
-                       public override string MethodName {
-                               get {
-                                       return "set_" + method.ShortName;
-                               }
-                       }
-
                        public override Type[] ParameterTypes {
                                get {
                                        return new Type[] { method.MemberType };
@@ -5719,7 +6110,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       protected override string[] ValidAttributeTargets {
+                       public override string[] ValidAttributeTargets {
                                get {
                                        return attribute_targets;
                                }
@@ -5730,16 +6121,29 @@ namespace Mono.CSharp {
 
                public abstract class PropertyMethod: AbstractPropertyEventMethod {
                        protected readonly MethodCore method;
+                       protected MethodAttributes flags;
+
+                       public PropertyMethod (MethodCore method, string prefix)
+                               : base (method, prefix)
+                       {
+                               this.method = method;
+                       }
 
-                       public PropertyMethod (MethodCore method, Accessor accessor):
-                               base (accessor)
+                       public PropertyMethod (MethodCore method, Accessor accessor, string prefix)
+                               : base (method, accessor, prefix)
                        {
                                this.method = method;
+                               this.ModFlags = accessor.ModFlags;
+
+                               if (accessor.ModFlags != 0 && RootContext.Version == LanguageVersion.ISO_1) {
+                                       Report.FeatureIsNotStandardized (Location, "accessor modifiers");
+                                       Environment.Exit (1);
+                               }
                        }
 
                        public override AttributeTargets AttributeTargets {
                                get {
-                                       return AttributeTargets.Method | AttributeTargets.ReturnValue;
+                                       return AttributeTargets.Method;
                                }
                        }
 
@@ -5755,17 +6159,28 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       public abstract MethodBuilder Define (TypeContainer container);
-
-                       public override Type[] ParameterTypes {
-                               get {
-                                       return TypeManager.NoTypes;
+                       public virtual MethodBuilder Define (TypeContainer container)
+                       {
+                               //
+                               // Check for custom access modifier
+                               //
+                                if (ModFlags == 0) {
+                                        ModFlags = method.ModFlags;
+                                        flags = method.flags;
+                                } else {
+                                       CheckModifiers (container, ModFlags);
+                                       ModFlags |= (method.ModFlags & (~Modifiers.Accessibility));
+                                       flags = Modifiers.MethodAttr (ModFlags);
+                                       flags |= (method.flags & (~MethodAttributes.MemberAccessMask));
                                }
+
+                               return null;
+
                        }
 
-                       public override Location Location {
+                       public override Type[] ParameterTypes {
                                get {
-                                       return method.Location;
+                                       return TypeManager.NoTypes;
                                }
                        }
 
@@ -5786,6 +6201,28 @@ namespace Mono.CSharp {
                        {
                                return String.Concat (tc.Name, '.', method.Name);
                        }
+
+                       void CheckModifiers (TypeContainer container, int modflags)
+                        {
+                                int flags = 0;
+                                int mflags = method.ModFlags & Modifiers.Accessibility;
+
+                                if ((mflags & Modifiers.PUBLIC) != 0) {
+                                        flags |= Modifiers.PROTECTED | Modifiers.INTERNAL | Modifiers.PRIVATE;
+                                }
+                                else if ((mflags & Modifiers.PROTECTED) != 0) {
+                                        if ((mflags & Modifiers.INTERNAL) != 0)
+                                                flags |= Modifiers.PROTECTED | Modifiers.INTERNAL;
+
+                                        flags |= Modifiers.PRIVATE;
+                                }
+                                else if ((mflags & Modifiers.INTERNAL) != 0)
+                                        flags |= Modifiers.PRIVATE;
+
+                                if ((mflags == modflags) || (modflags & (~flags)) != 0)
+                                        Report.Error (273, Location, "{0}: accessibility modifier must be more restrictive than the property or indexer",
+                                                       GetSignatureForError (container));
+                        }
                }
 
                public PropertyMethod Get, Set;
@@ -5805,6 +6242,11 @@ namespace Mono.CSharp {
 
                public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
                {
+                       if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
+                               a.Error_InvalidSecurityParent ();
+                               return;
+                       }
+
                        PropertyBuilder.SetCustomAttribute (cb);
                }
 
@@ -5814,169 +6256,138 @@ namespace Mono.CSharp {
                        }
                }
 
-               protected override bool DoDefine (DeclSpace decl)
+               public override bool Define ()
                {
-                       if (!base.DoDefine (decl))
+                       if (!DoDefine (Parent))
                                return false;
 
-                       if (MemberType == TypeManager.arg_iterator_type || MemberType == TypeManager.typed_reference_type) {
-                               // "Field or property cannot be of type '{0}'";
-                               Report.Error_T (610, Location, TypeManager.CSharpName (MemberType));
+                       if (!IsTypePermitted ())
                                return false;
-                       }
-
-                       ec = new EmitContext (Parent, Location, null, MemberType, ModFlags);
 
                        return true;
                }
 
-               public override string GetSignatureForError()
-               {
-                       return TypeManager.CSharpSignature (PropertyBuilder, false);
-               }
-
-               protected virtual string RealMethodName {
-                       get {
-                               return Name;
-                       }
-               }
-
-               protected override bool IsIdentifierClsCompliant (DeclSpace ds)
+               protected override bool DoDefine (DeclSpace ds)
                {
-                       if (!IsIdentifierAndParamClsCompliant (ds, RealMethodName, null, null))
+                       if (!base.DoDefine (ds))
+                               return false;
+
+                       //
+                       // Accessors modifiers check
+                       //
+                       if (Get.ModFlags != 0 && Set.ModFlags != 0) {
+                               Report.Error (274, Location, "'{0}': cannot specify accessibility modifiers for both accessors of the property or indexer.",
+                                               GetSignatureForError ());
                                return false;
+                       }
 
-                       if (Get != null && !IsIdentifierAndParamClsCompliant (ds, "get_" + RealMethodName, null, null))
+                       if ((Get.IsDummy || Set.IsDummy)
+                                       && (Get.ModFlags != 0 || Set.ModFlags != 0) && (ModFlags & Modifiers.OVERRIDE) == 0) {
+                               Report.Error (276, Location, 
+                                       "'{0}': accessibility modifiers on accessors may only be used if the property or indexer has both a get and a set accessor.",
+                                       GetSignatureForError ());
                                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 ()
+               public override string GetSignatureForError()
                {
-                       base.CheckBase ();
-                       
-                       // 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 = Parent.Properties;
+
+               protected override bool CheckForDuplications ()
+               {
+                       ArrayList ar = Parent.Indexers;
+                       if (ar != null) {
+                               int arLen = ar.Count;
+                                       
+                               for (int i = 0; i < arLen; i++) {
+                                       Indexer m = (Indexer) ar [i];
+                                       if (IsDuplicateImplementation (m))
+                                               return false;
+                               }
+                       }
+
+                       ar = Parent.Properties;
                        if (ar != null) {
                                int arLen = ar.Count;
                                        
                                for (int i = 0; i < arLen; i++) {
                                        Property m = (Property) ar [i];
-                                       if (IsDuplicateImplementation (Parent, m))
+                                       if (IsDuplicateImplementation (m))
                                                return false;
                                }
                        }
 
-                       if (IsInterface)
-                               return true;
+                       return true;
+               }
 
-                       string report_name;
-                       MethodSignature ms, base_ms;
-                       if (this is Indexer) {
-                               string name, base_name;
+               // TODO: rename to Resolve......
+               protected override MethodInfo FindOutParentMethod (TypeContainer container, ref Type parent_ret_type)
+               {
+                       PropertyInfo parent_property = container.ParentCache.FindMemberToOverride (
+                               container.TypeBuilder, Name, ParameterTypes, true) as PropertyInfo;
 
-                               report_name = "this";
-                               name = TypeManager.IndexerPropertyName (Parent.TypeBuilder);
-                               ms = new MethodSignature (name, null, ParameterTypes);
-                               base_name = TypeManager.IndexerPropertyName (Parent.TypeBuilder.BaseType);
-                               base_ms = new MethodSignature (base_name, null, ParameterTypes);
-                       } else {
-                               report_name = Name;
-                               ms = base_ms = new MethodSignature (Name, null, ParameterTypes);
-                       }
+                       if (parent_property == null)
+                               return null;
 
-                       //
-                       // 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 = Parent.TypeBuilder.BaseType;
+                       parent_ret_type = parent_property.PropertyType;
+                       MethodInfo get_accessor = parent_property.GetGetMethod (true);
+                       MethodInfo set_accessor = parent_property.GetSetMethod (true);
+                       MethodAttributes get_accessor_access, set_accessor_access;
 
-                       // ptype is only null for System.Object while compiling corlib.
-                       if (ptype == null) {
-                               if ((ModFlags & Modifiers.NEW) != 0)
-                                       WarningNotHiding (Parent);
+                       if ((ModFlags & Modifiers.OVERRIDE) != 0) {
+                               if (Get != null && !Get.IsDummy && get_accessor == null) {
+                                       Report.SymbolRelatedToPreviousError (parent_property);
+                                       Report.Error (545, Location, "'{0}': cannot override because '{1}' does not have an overridable get accessor", GetSignatureForError (), TypeManager.GetFullNameSignature (parent_property));
+                               }
 
-                               return true;
+                               if (Set != null && !Set.IsDummy && set_accessor == null) {
+                                       Report.SymbolRelatedToPreviousError (parent_property);
+                                       Report.Error (546, Location, "'{0}': cannot override because '{1}' does not have an overridable set accessor", GetSignatureForError (), TypeManager.GetFullNameSignature (parent_property));
+                               }
                        }
-
-                       MemberInfo parent_member = 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.
+                       // Check parent accessors access
                        //
-                       if (! IsExplicitImpl) {
-                               parent_member = ((IMemberContainer)Parent).Parent.MemberCache.FindMemberToOverride (
-                                       Parent.TypeBuilder, Name, ParameterTypes, true);
-                       }
-
-                       if (parent_member is PropertyInfo) {
-                               PropertyInfo parent_property = (PropertyInfo)parent_member;
-
-                               string name = parent_property.DeclaringType.Name + "." +
-                                       parent_property.Name;
-
-                               MethodInfo get, set, parent_method;
-                               get = parent_property.GetGetMethod (true);
-                               set = parent_property.GetSetMethod (true);
-
-                               if (get != null)
-                                       parent_method = get;
-                               else if (set != null)
-                                       parent_method = set;
-                               else
-                                       throw new Exception ("Internal error!");
-
-                               if (!CheckMethodAgainstBase (Parent, flags, parent_method, name))
-                                       return false;
+                       get_accessor_access = set_accessor_access = 0;
+                       if ((ModFlags & Modifiers.NEW) == 0) {
+                               if (get_accessor != null) {
+                                       MethodAttributes get_flags = Modifiers.MethodAttr (Get.ModFlags != 0 ? Get.ModFlags : ModFlags);
+                                       get_accessor_access = (get_accessor.Attributes & MethodAttributes.MemberAccessMask);
 
-                               if ((ModFlags & Modifiers.NEW) == 0) {
-                                       Type parent_type = TypeManager.TypeToCoreType (
-                                               parent_property.PropertyType);
-
-                                       if (parent_type != MemberType) {
-                                               Report.Error (
-                                                       508, Location, Parent.MakeName (Name) + ": cannot " +
-                                                       "change return type when overriding " +
-                                                       "inherited member " + name);
-                                               return false;
-                                       }
+                                       if (!Get.IsDummy && !CheckAccessModifiers (get_flags & MethodAttributes.MemberAccessMask, get_accessor_access, get_accessor))
+                                               Report.Error (507, Location, "'{0}' can't change the access modifiers when overriding inherited member '{1}'",
+                                                               GetSignatureForError (), TypeManager.GetFullNameSignature (parent_property));
                                }
-                       } else if (parent_member == null){
-                               if ((ModFlags & Modifiers.NEW) != 0)
-                                       WarningNotHiding (Parent);
 
-                               if ((ModFlags & Modifiers.OVERRIDE) != 0){
-                                       if (this is Indexer)
-                                               Report.Error (115, Location,
-                                                             Parent.MakeName (Name) +
-                                                             " no suitable indexers found to override");
-                                       else
-                                               Report.Error (115, Location,
-                                                             Parent.MakeName (Name) +
-                                                             " no suitable properties found to override");
-                                       return false;
+                               if (set_accessor != null)  {
+                                       MethodAttributes set_flags = Modifiers.MethodAttr (Set.ModFlags != 0 ? Set.ModFlags : ModFlags);
+                                       set_accessor_access = (set_accessor.Attributes & MethodAttributes.MemberAccessMask);
+
+                                       if (!Set.IsDummy && !CheckAccessModifiers (set_flags & MethodAttributes.MemberAccessMask, set_accessor_access, set_accessor))
+                                               Report.Error (507, Location, "'{0}' can't change the access modifiers when overriding inherited member '{1}'",
+                                                               GetSignatureForError (container), TypeManager.GetFullNameSignature (parent_property));
                                }
                        }
-                       return true;
+
+                       //
+                       // Get the less restrictive access
+                       //
+                       return get_accessor_access > set_accessor_access ? get_accessor : set_accessor;
                }
 
                public override void Emit ()
@@ -5989,20 +6400,44 @@ namespace Mono.CSharp {
                        if (PropertyBuilder != null && OptAttributes != null)
                                OptAttributes.Emit (ec, this);
 
-                       if (Get != null)
+                       if (!Get.IsDummy)
                                Get.Emit (Parent);
 
-                       if (Set != null)
+                       if (!Set.IsDummy)
                                Set.Emit (Parent);
 
                        base.Emit ();
                }
 
-               protected override string[] ValidAttributeTargets {
+               /// <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);
+               }
+
+               protected override void UpdateMemberName ()
+               {
+                       base.UpdateMemberName ();
+
+                       Get.UpdateName (this);
+                       Set.UpdateName (this);
+               }
+
+
+               public override string[] ValidAttributeTargets {
                        get {
                                return attribute_targets;
                        }
                }
+
+               //
+               //   Represents header string for documentation comment.
+               //
+               public override string DocCommentHeader {
+                       get { return "P:"; }
+               }
        }
                        
        public class Property : PropertyBase, IIteratorContainer {
@@ -6032,10 +6467,14 @@ namespace Mono.CSharp {
                                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);
                }
 
@@ -6044,7 +6483,7 @@ namespace Mono.CSharp {
                        if (!DoDefineBase ())
                                return false;
 
-                       if (!DoDefine (Parent))
+                       if (!base.Define ())
                                return false;
 
                        if (!CheckBase ())
@@ -6052,7 +6491,7 @@ namespace Mono.CSharp {
 
                        flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
 
-                       if (Get != null) {
+                       if (!Get.IsDummy) {
 
                                GetBuilder = Get.Define (Parent);
                                if (GetBuilder == null)
@@ -6073,7 +6512,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       if (Set != null) {
+                       if (!Set.IsDummy) {
                                SetBuilder = Set.Define (Parent);
                                if (SetBuilder == null)
                                        return false;
@@ -6088,28 +6527,16 @@ namespace Mono.CSharp {
                                prop_attr |= PropertyAttributes.RTSpecialName |
                        PropertyAttributes.SpecialName;
 
-                       if (!IsExplicitImpl){
                                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 `" + Parent.Name +
-                                               "' already contains a definition for the property `" +
-                                               Name + "'");
-                                       return false;
-                               }
-                       }
+                               TypeManager.RegisterProperty (PropertyBuilder, GetBuilder, SetBuilder);
                        return true;
                }
 
@@ -6278,9 +6705,13 @@ namespace Mono.CSharp {
                {
                        Add = new AddDelegateMethod (this, add);
                        Remove = new RemoveDelegateMethod (this, remove);
+
+                       // For this event syntax we don't report error CS0067
+                       // because it is hard to do it.
+                       SetAssigned ();
                }
 
-               protected override string[] ValidAttributeTargets {
+               public override string[] ValidAttributeTargets {
                        get {
                                return attribute_targets;
                        }
@@ -6293,6 +6724,7 @@ namespace Mono.CSharp {
        public class EventField: Event {
 
                static string[] attribute_targets = new string [] { "event", "field", "method" };
+               static string[] attribute_targets_interface = new string[] { "event", "method" };
 
                public EventField (TypeContainer parent, Expression type, int mod_flags,
                                   bool is_iface, MemberName name, Object init,
@@ -6305,23 +6737,23 @@ namespace Mono.CSharp {
 
                public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
                {
-                       if (a.Target == "field") {
+                       if (a.Target == AttributeTargets.Field) {
                                FieldBuilder.SetCustomAttribute (cb);
                                return;
                        }
 
-                       if (a.Target == "method") {
-                               AddBuilder.SetCustomAttribute (cb);
-                               RemoveBuilder.SetCustomAttribute (cb);
+                       if (a.Target == AttributeTargets.Method) {
+                               Add.ApplyAttributeBuilder (a, cb);
+                               Remove.ApplyAttributeBuilder (a, cb);
                                return;
                        }
 
                        base.ApplyAttributeBuilder (a, cb);
                }
 
-               protected override string[] ValidAttributeTargets {
+               public override string[] ValidAttributeTargets {
                        get {
-                               return attribute_targets;
+                               return IsInterface ? attribute_targets_interface : attribute_targets;
                        }
                }
        }
@@ -6330,22 +6762,17 @@ namespace Mono.CSharp {
 
                protected sealed class AddDelegateMethod: DelegateMethod
                {
+
                        public AddDelegateMethod (Event method):
-                               base (method)
+                               base (method, "add_")
                        {
                        }
 
                        public AddDelegateMethod (Event method, Accessor accessor):
-                               base (method, accessor)
+                               base (method, accessor, "add_")
                        {
                        }
 
-                       public override string MethodName {
-                               get {
-                                       return "add_" + method.ShortName;
-                               }
-                       }
-
                        protected override MethodInfo DelegateMethodInfo {
                                get {
                                        return TypeManager.delegate_combine_delegate_delegate;
@@ -6357,21 +6784,15 @@ namespace Mono.CSharp {
                protected sealed class RemoveDelegateMethod: DelegateMethod
                {
                        public RemoveDelegateMethod (Event method):
-                               base (method)
+                               base (method, "remove_")
                        {
                        }
 
                        public RemoveDelegateMethod (Event method, Accessor accessor):
-                               base (method, accessor)
+                               base (method, accessor, "remove_")
                        {
                        }
 
-                       public override string MethodName {
-                               get {
-                                       return "remove_" + method.ShortName;
-                               }
-                       }
-
                        protected override MethodInfo DelegateMethodInfo {
                                get {
                                        return TypeManager.delegate_remove_delegate_delegate;
@@ -6387,20 +6808,21 @@ namespace Mono.CSharp {
 
                        static string[] attribute_targets = new string [] { "method", "param", "return" };
 
-                       public DelegateMethod (Event method)
+                       public DelegateMethod (Event method, string prefix)
+                               : base (method, prefix)
                        {
                                this.method = method;
                        }
 
-                       public DelegateMethod (Event method, Accessor accessor):
-                               base (accessor)
+                       public DelegateMethod (Event method, Accessor accessor, string prefix)
+                               : base (method, accessor, prefix)
                        {
                                this.method = method;
                        }
 
                        protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
                        {
-                               if (a.Target == "param") {
+                               if (a.Target == AttributeTargets.Parameter) {
                                        if (param_attr == null)
                                                param_attr = new ImplicitParameter (method_data.MethodBuilder);
 
@@ -6480,12 +6902,6 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       public override Location Location {
-                               get {
-                                       return method.Location;
-                               }
-                       }
-
                        public override EmitContext CreateEmitContext (TypeContainer tc,
                                                                       ILGenerator ig)
                        {
@@ -6504,7 +6920,7 @@ namespace Mono.CSharp {
                                return method.GetObsoleteAttribute (method.Parent);
                        }
 
-                       protected override string[] ValidAttributeTargets {
+                       public override string[] ValidAttributeTargets {
                                get {
                                        return attribute_targets;
                                }
@@ -6528,7 +6944,7 @@ namespace Mono.CSharp {
                const int AllowedInterfaceModifiers =
                        Modifiers.NEW;
 
-               protected DelegateMethod Add, Remove;
+               public DelegateMethod Add, Remove;
                public MyEventBuilder     EventBuilder;
                public MethodBuilder AddBuilder, RemoveBuilder;
 
@@ -6546,9 +6962,19 @@ namespace Mono.CSharp {
 
                public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
                {
+                       if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
+                               a.Error_InvalidSecurityParent ();
+                               return;
+                       }
+                       
                        EventBuilder.SetCustomAttribute (cb);
                }
 
+               public bool AreAccessorsDuplicateImplementation (MethodCore mc)
+               {
+                       return Add.IsDuplicateImplementation (mc) || Remove.IsDuplicateImplementation (mc);
+               }
+
                public override AttributeTargets AttributeTargets {
                        get {
                                return AttributeTargets.Event;
@@ -6559,7 +6985,7 @@ namespace Mono.CSharp {
                {
                        EventAttributes e_attr;
                        e_attr = EventAttributes.None;
-;
+
                        if (!DoDefineBase ())
                                return false;
 
@@ -6578,10 +7004,19 @@ namespace Mono.CSharp {
                                return false;
                        }
 
+                       EmitContext ec = Parent.EmitContext;
+                       if (ec == null)
+                               throw new InternalErrorException ("Event.Define called too early?");
+                       bool old_unsafe = ec.InUnsafe;
+                       ec.InUnsafe = InUnsafe;
+
                        Parameter [] parms = new Parameter [1];
                        parms [0] = new Parameter (Type, "value", Parameter.Modifier.NONE, null);
-                       InternalParameters ip = new InternalParameters (
-                               Parent, new Parameters (parms, null, Location)); 
+                       Parameters parameters = new Parameters (parms, null, Location);
+                       Type [] types = parameters.GetParameterInfo (ec);
+                       InternalParameters ip = new InternalParameters (types, parameters);
+
+                       ec.InUnsafe = old_unsafe;
 
                        if (!CheckBase ())
                                return false;
@@ -6598,12 +7033,9 @@ namespace Mono.CSharp {
                        if (RemoveBuilder == null)
                                return false;
 
-                       if (!IsExplicitImpl){
-                               EventBuilder = new MyEventBuilder (this,
-                                       Parent.TypeBuilder, Name, e_attr, MemberType);
+                       EventBuilder = new MyEventBuilder (this, Parent.TypeBuilder, Name, e_attr, MemberType);
                                        
-                               if (Add.Block == null && Remove.Block == null &&
-                                   !IsInterface) {
+                       if (Add.Block == null && Remove.Block == null && !IsInterface) {
                                        FieldBuilder = Parent.TypeBuilder.DefineField (
                                                Name, MemberType,
                                                FieldAttributes.Private | ((ModFlags & Modifiers.STATIC) != 0 ? FieldAttributes.Static : 0));
@@ -6615,18 +7047,26 @@ namespace Mono.CSharp {
                                EventBuilder.SetAddOnMethod (AddBuilder);
                                EventBuilder.SetRemoveOnMethod (RemoveBuilder);
 
-                               if (!TypeManager.RegisterEvent (EventBuilder, AddBuilder, RemoveBuilder)) {
-                                       Report.Error (111, Location,
-                                                     "Class `" + Parent.Name +
-                                                     "' already contains a definition for the event `" +
-                                                     Name + "'");
-                                       return false;
-                               }
-                       }
-                       
+                               TypeManager.RegisterEvent (EventBuilder, AddBuilder, RemoveBuilder);
                        return true;
                }
 
+               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) {
@@ -6645,24 +7085,30 @@ namespace Mono.CSharp {
 
                public override string GetSignatureForError ()
                {
+                       if (EventBuilder == null)
+                               return base.GetSignatureForError (Parent);
+
                        return TypeManager.GetFullNameSignature (EventBuilder);
                }
+
+               //
+               //   Represents header string for documentation comment.
+               //
+               public override string DocCommentHeader {
+                       get { return "E:"; }
+               }
        }
 
-       //
-       // 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)
                        {
@@ -6679,6 +7125,11 @@ namespace Mono.CSharp {
                {
                        readonly Parameters parameters;
 
+                       public SetIndexerMethod (MethodCore method):
+                               base (method)
+                       {
+                       }
+
                        public SetIndexerMethod (MethodCore method, Parameters parameters, Accessor accessor):
                                base (method, accessor)
                        {
@@ -6695,7 +7146,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       protected override InternalParameters GetParameterInfo (TypeContainer container)
+                       protected override InternalParameters GetParameterInfo (EmitContext ec)
                        {
                                Parameter [] fixed_parms = parameters.FixedParameters;
 
@@ -6722,13 +7173,12 @@ namespace Mono.CSharp {
                                        method.Type, "value", Parameter.Modifier.NONE, null);
 
                                Parameters set_formal_params = new Parameters (tmp, null, method.Location);
+                               Type [] types = set_formal_params.GetParameterInfo (ec);
                                
-                               return new InternalParameters (container, set_formal_params);
+                               return new InternalParameters (types, set_formal_params);
                        }
-
                }
 
-
                const int AllowedModifiers =
                        Modifiers.NEW |
                        Modifiers.PUBLIC |
@@ -6745,24 +7195,24 @@ namespace Mono.CSharp {
                const int AllowedInterfaceModifiers =
                        Modifiers.NEW;
 
-               public string IndexerName = "Item";
-               public string InterfaceIndexerName;
-
                //
                // Are we implementing an interface ?
                //
-               public Indexer (TypeContainer parent, Expression type, int mod_flags,
-                               bool is_iface, MemberName name, Parameters parameters,
-                               Attributes attrs, Accessor get_block, Accessor set_block,
-                               Location loc)
-                       : base (parent, type, mod_flags,
+               public Indexer (TypeContainer parent, Expression type, MemberName name, int mod,
+                               bool is_iface, Parameters parameters, Attributes attrs,
+                               Accessor get_block, Accessor set_block, Location loc)
+                       : base (parent, type, mod,
                                is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
                                is_iface, name, parameters, attrs, loc)
                {
-                       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);
                }
 
@@ -6775,56 +7225,62 @@ namespace Mono.CSharp {
                        if (!DoDefineBase ())
                                return false;
 
-                       if (!DoDefine (Parent))
+                       if (!base.Define ())
                                return false;
 
                        if (OptAttributes != null) {
-                               Attribute indexer_attr = OptAttributes.GetIndexerNameAttribute (ec);
+                               Attribute indexer_attr = OptAttributes.Search (TypeManager.indexer_name_type, ec);
                                if (indexer_attr != null) {
-                                       IndexerName = indexer_attr.GetIndexerAttributeValue (ec);
+                                       // Remove the attribute from the list because it is not emitted
+                                       OptAttributes.Attrs.Remove (indexer_attr);
+
+                                       ShortName = indexer_attr.GetIndexerAttributeValue (ec);
+
                                        if (IsExplicitImpl) {
-                                               // The 'IndexerName' attribute is valid only on an indexer that is not an explicit interface member declaration
-                                               Report.Error_T (415, indexer_attr.Location);
+                                               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 (IsExplicitImpl) {
-                                               // The 'IndexerName' attribute is valid only on an indexer that is not an explicit interface member declaration
-                                               Report.Error_T (415, indexer_attr.Location);
+                                       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 (IndexerName)) {
-                                               // The argument to the 'IndexerName' attribute must be a valid identifier
-                                               Report.Error_T (633, indexer_attr.Location);
+                                       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 (Parent))
+                       if (!Parent.AddToMemberContainer (this, true) ||
+                           !Parent.AddToMemberContainer (Get, true) || !Parent.AddToMemberContainer (Set, true))
                                return false;
 
                        if (!CheckBase ())
                                return false;
 
                        flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
-                       if (Get != null){
+                       if (!Get.IsDummy){
                                GetBuilder = Get.Define (Parent);
                                if (GetBuilder == null)
                                        return false;
                        }
                        
-                       if (Set != null){
+                       if (!Set.IsDummy){
                                SetBuilder = Set.Define (Parent);
                                if (SetBuilder == null)
                                        return false;
@@ -6835,19 +7291,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");
                                        
@@ -6859,82 +7320,35 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       //
-                       // Define the PropertyBuilder if one of the following conditions are met:
-                       // a) we're not implementing an interface indexer.
-                       // b) the indexer has a different IndexerName and this is no
-                       //    explicit interface implementation.
-                       //
-                       if (!IsExplicitImpl) {
                                PropertyBuilder = Parent.TypeBuilder.DefineProperty (
-                                       IndexerName, prop_attr, MemberType, ParameterTypes);
+                               Name, 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,
-                                                            ParameterTypes);
-                       }
-
-                       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);
+                       TypeManager.RegisterIndexer (PropertyBuilder, GetBuilder, SetBuilder, ParameterTypes);
 
-                       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 |
@@ -6942,10 +7356,6 @@ namespace Mono.CSharp {
                        Modifiers.EXTERN |
                        Modifiers.STATIC;
 
-               const int RequiredModifiers =
-                       Modifiers.PUBLIC |
-                       Modifiers.STATIC;
-
                public enum OpType : byte {
 
                        // Unary operators
@@ -6985,40 +7395,22 @@ 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;
 
                static string[] attribute_targets = new string [] { "method", "return" };
 
                public Operator (TypeContainer parent, OpType type, Expression ret_type,
-                                int mod_flags, Expression arg1type, string arg1name,
-                                Expression arg2type, string arg2name,
-                                Block block, Attributes attrs, Location loc)
-                       : base (parent, ret_type, mod_flags, AllowedModifiers,
-                               Modifiers.PUBLIC, MemberName.Null, attrs, loc)
+                                int mod_flags, Parameters parameters,
+                                ToplevelBlock block, Attributes attrs, Location loc)
+                       : base (parent, null, ret_type, mod_flags, AllowedModifiers, false,
+                               new MemberName ("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)
-               {
-                       return container.Name + ".operator " + OperatorType + " (" + FirstArgType + "," +
-                               SecondArgType + ")";
-               }
-
                public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb) 
                {
                        OperatorMethod.ApplyAttributeBuilder (a, cb);
@@ -7035,39 +7427,51 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               public override bool Define ()
+               protected override bool CheckForDuplications()
                {
-                       int length = 1;
-                       MethodName = "op_" + OperatorType;
-                       
-                       if (SecondArgType != null)
-                               length = 2;
-                       
-                       Parameter [] param_list = new Parameter [length];
+                       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 ()
+               {
+                       const int RequiredModifiers = Modifiers.PUBLIC | Modifiers.STATIC;
                        if ((ModFlags & RequiredModifiers) != RequiredModifiers){
-                               Report.Error (
-                                       558, Location, 
-                                       "User defined operators `" +
-                                       Prototype (Parent) +
-                                       "' must be declared static and public");
+                               Report.Error (558, Location, "User defined operators '{0}' must be declared static and public", GetSignatureForError (Parent));
                                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 (ds))
+                               return false;
+
                        OperatorMethod = new Method (
-                               Parent, null, ReturnType, ModFlags, false,
-                               new MemberName (MethodName),
-                               new Parameters (param_list, null, Location),
-                               OptAttributes, Location);
+                               Parent, null, Type, ModFlags, false, MemberName,
+                               Parameters, OptAttributes, Location);
 
                        OperatorMethod.Block = Block;
                        OperatorMethod.IsOperator = true;                       
+                       OperatorMethod.flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
                        OperatorMethod.Define ();
 
                        if (OperatorMethod.MethodBuilder == null)
@@ -7075,10 +7479,13 @@ namespace Mono.CSharp {
                        
                        OperatorMethodBuilder = OperatorMethod.MethodBuilder;
 
-                       Type [] param_types = OperatorMethod.ParameterTypes;
+                       parameter_types = OperatorMethod.ParameterTypes;
                        Type declaring_type = OperatorMethod.MethodData.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
                        
@@ -7120,34 +7527,39 @@ namespace Mono.CSharp {
                                if (first_arg_type.IsSubclassOf (return_type)
                                        || return_type.IsSubclassOf (first_arg_type)){
                                        if (declaring_type.IsSubclassOf (return_type)) {
-                                               // '{0}' : user defined conversion to/from base class
-                                               Report.Error_T (553, Location, GetSignatureForError ());
+                                               Report.Error (553, Location, "'{0}' : user defined conversion to/from base class", GetSignatureForError ());
                                                return false;
                                        }
-                                       // '{0}' : user defined conversion to/from derived class
-                                       Report.Error_T (554, Location, GetSignatureForError ());
+                                       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 (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
+                                       if (return_type != declaring_type && !return_type.IsSubclassOf (declaring_type)) {
+                                               Report.Error (448, Location,
+                                                       "The return type for ++ or -- operator must be the containing type or derived from the containing type");
+                                               return false;
+                                       }
                                if (first_arg_type != declaring_type){
                                        Report.Error (
-                                               562, Location,
-                                               "The parameter of a unary operator must be the " +
-                                               "containing type");
+                                                       559, Location, "The parameter type for ++ or -- operator must be the containing type");
                                        return false;
                                }
+                               }
                                
-                               if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
-                                       if (return_type != declaring_type){
+                               if (first_arg_type != declaring_type){
                                                Report.Error (
-                                                       559, Location,
-                                                       "The parameter and return type for ++ and -- " +
-                                                       "must be the containing type");
+                                               562, Location,
+                                               "The parameter of a unary operator must be the " +
+                                               "containing type");
                                                return false;
-                                       }
-                                       
                                }
                                
                                if (OperatorType == OpType.True || OperatorType == OpType.False) {
@@ -7164,7 +7576,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 " +
@@ -7188,6 +7600,12 @@ namespace Mono.CSharp {
                        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){
@@ -7247,22 +7665,34 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override string GetSignatureForError(TypeContainer tc)
+               public override string GetSignatureForError (TypeContainer tc)
                {
-                       return ToString ();
+                       StringBuilder sb = new StringBuilder ();
+                       sb.AppendFormat ("{0}.operator {1} {2}({3}", tc.Name, GetName (OperatorType), Type.Type == null ? Type.ToString () : TypeManager.CSharpName (Type.Type),
+                               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()
+               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),
@@ -7276,7 +7706,7 @@ namespace Mono.CSharp {
                                        param_types [0], param_types [1]);
                }
 
-               protected override string[] ValidAttributeTargets {
+               public override string[] ValidAttributeTargets {
                        get {
                                return attribute_targets;
                        }