2002-08-16 Martin Baulig <martin@gnome.org>
[mono.git] / mcs / mcs / class.cs
index f8777ebd5a77ae8d8bb3d67dfaf32a020fb9cb5b..11d1b9be24a0a75746342c0c201dfe0186bebf01 100755 (executable)
@@ -1,19 +1,22 @@
+
 //
 // class.cs: Class and Struct handlers
 //
-// Author: Miguel de Icaza (miguel@gnu.org)
+// Authors: Miguel de Icaza (miguel@gnu.org)
+//          Martin Baulig (martin@gnome.org)
 //
 // Licensed under the terms of the GNU GPL
 //
-// (C) 2001 Ximian, Inc (http://www.ximian.com)
+// (C) 2001, 2002 Ximian, Inc (http://www.ximian.com)
 //
 //
 
+using System;
 using System.Collections;
 using System.Reflection;
 using System.Reflection.Emit;
-using System;
 using System.Runtime.CompilerServices;
+using System.Diagnostics.SymbolStore;
 
 namespace Mono.CSharp {
 
@@ -51,6 +54,9 @@ namespace Mono.CSharp {
                // Holds the list of
                ArrayList interfaces;
 
+               // Holds order in which interfaces must be closed
+               ArrayList interface_order;
+               
                // Holds the methods.
                ArrayList methods;
 
@@ -63,6 +69,9 @@ namespace Mono.CSharp {
                // Holds the operators
                ArrayList operators;
 
+               // The emit context for toplevel objects.
+               EmitContext ec;
+               
                //
                // Pointers to the default constructor and the default static constructor
                //
@@ -73,6 +82,11 @@ namespace Mono.CSharp {
                // Whether we have seen a static constructor for this class or not
                //
                bool have_static_constructor = false;
+
+               //
+               // Whether we have at least one non-static field
+               //
+               bool have_nonstatic_fields = false;
                
                //
                // This one is computed after we can distinguish interfaces
@@ -87,10 +101,17 @@ namespace Mono.CSharp {
 
                // Information in the case we are an attribute type
 
-               public AttributeTargets Targets;
-               public bool AllowMultiple;
+               public AttributeTargets Targets = AttributeTargets.All;
+               public bool AllowMultiple = false;
                public bool Inherited;
+
+               // The interfaces we implement.
+               Type [] ifaces;
                
+               //
+               // The indexer name for this class
+               //
+               public string IndexerName;
 
                public TypeContainer (TypeContainer parent, string name, Location l)
                        : base (parent, name, l)
@@ -196,10 +217,17 @@ namespace Mono.CSharp {
                        if (value != null && (!(value is Method)))
                                return AdditionResult.NameExists;
 
+                       if (name == Basename)
+                               return AdditionResult.EnclosingClash;
+
                        if (methods == null)
                                methods = new ArrayList ();
 
-                       methods.Add (method);
+                       if (method.Name.IndexOf (".") != -1)
+                               methods.Insert (0, method);
+                       else 
+                               methods.Add (method);
+                       
                        if (value != null)
                                DefineName (name, method);
 
@@ -258,16 +286,16 @@ namespace Mono.CSharp {
                {
                        AdditionResult res;
                        string name = field.Name;
-                       
+
                        if ((res = IsValid (name)) != AdditionResult.Success)
                                return res;
-
+                       
                        if (fields == null)
                                fields = new ArrayList ();
-
+                       
                        fields.Add (field);
                        
-                       if (field.Initializer != null){
+                       if (field.HasInitializer){
                                if ((field.ModFlags & Modifiers.STATIC) != 0){
                                        if (initialized_static_fields == null)
                                                initialized_static_fields = new ArrayList ();
@@ -286,7 +314,10 @@ namespace Mono.CSharp {
                                        initialized_fields.Add (field);
                                }
                        }
-                       
+
+                       if ((field.ModFlags & Modifiers.STATIC) == 0)
+                               have_nonstatic_fields = true;
+
                        DefineName (name, field);
                        return AdditionResult.Success;
                }
@@ -302,7 +333,10 @@ namespace Mono.CSharp {
                        if (properties == null)
                                properties = new ArrayList ();
 
-                       properties.Add (prop);
+                       if (prop.Name.IndexOf (".") != -1)
+                               properties.Insert (0, prop);
+                       else
+                               properties.Add (prop);
                        DefineName (name, prop);
 
                        return AdditionResult.Success;
@@ -330,7 +364,10 @@ namespace Mono.CSharp {
                        if (indexers == null)
                                indexers = new ArrayList ();
 
-                       indexers.Add (i);
+                       if (i.InterfaceType != null)
+                               indexers.Insert (0, i);
+                       else
+                               indexers.Add (i);
 
                        return AdditionResult.Success;
                }
@@ -344,6 +381,14 @@ namespace Mono.CSharp {
 
                        return AdditionResult.Success;
                }
+
+               public void RegisterOrder (Interface iface)
+               {
+                       if (interface_order == null)
+                               interface_order = new ArrayList ();
+
+                       interface_order.Add (iface);
+               }
                
                public ArrayList Types {
                        get {
@@ -389,6 +434,10 @@ namespace Mono.CSharp {
                        get {
                                return fields;
                        }
+
+                       set {
+                               fields = value;
+                       }
                }
 
                public ArrayList InstanceConstructors {
@@ -439,21 +488,6 @@ namespace Mono.CSharp {
                        }
                }
                
-               // 
-               // root_types contains all the types.  All TopLevel types
-               // hence have a parent that points to `root_types', that is
-               // why there is a non-obvious test down here.
-               //
-               public bool IsTopLevel {
-                       get {
-                               if (Parent != null){
-                                       if (Parent.Parent == null)
-                                               return true;
-                               }
-                               return false;
-                       }
-               }
-                       
                public bool HaveStaticConstructor {
                        get {
                                return have_static_constructor;
@@ -469,47 +503,44 @@ namespace Mono.CSharp {
                //
                // Emits the instance field initializers
                //
-               public bool EmitFieldInitializers (EmitContext ec, bool is_static)
+               public bool EmitFieldInitializers (EmitContext ec)
                {
                        ArrayList fields;
                        ILGenerator ig = ec.ig;
-
-                       if (is_static)
+                       Expression instance_expr;
+                       
+                       if (ec.IsStatic){
                                fields = initialized_static_fields;
-                       else
+                               instance_expr = null;
+                       } else {
                                fields = initialized_fields;
+                               instance_expr = new This (Location.Null).Resolve (ec);
+                       }
 
                        if (fields == null)
                                return true;
-                       
+
                        foreach (Field f in fields){
-                               Object init = f.Initializer;
+                               Expression e = f.GetInitializerExpression (ec);
+                               if (e == null)
+                                       return false;
+
+                               Location l = f.Location;
+                               FieldExpr fe = new FieldExpr (f.FieldBuilder, l);
+                               fe.InstanceExpression = instance_expr;
+                               Expression a = new Assign (fe, e, l);
 
-                               Expression e;
-                               if (init is Expression)
-                                       e = (Expression) init;
+                               a = a.Resolve (ec);
+                               if (a == null)
+                                       return false;
+
+                               if (a is ExpressionStatement)
+                                       ((ExpressionStatement) a).EmitStatement (ec);
                                else {
-                                       string base_type = f.Type.Substring (0, f.Type.IndexOf ("["));
-                                       string rank = f.Type.Substring (f.Type.IndexOf ("["));
-                                       e = new ArrayCreation (base_type, rank, (ArrayList) init, f.Location); 
+                                       throw new Exception ("Assign.Resolve returned a non ExpressionStatement");
                                }
-                               
-                               e = e.Resolve (ec);
-                               if (e == null)
-                                       return false;
-                               
-                               if (!is_static)
-                                       ig.Emit (OpCodes.Ldarg_0);
-                               
-                               e.Emit (ec);
-                               
-                               if (is_static)
-                                       ig.Emit (OpCodes.Stsfld, f.FieldBuilder);
-                               else
-                                       ig.Emit (OpCodes.Stfld, f.FieldBuilder);
-                               
                        }
-                       
+
                        return true;
                }
                
@@ -521,9 +552,11 @@ namespace Mono.CSharp {
                        Constructor c;
                        int mods = 0;
 
-                       c = new Constructor (Basename, Parameters.GetEmptyReadOnlyParameters (),
-                                            new ConstructorBaseInitializer (null, new Location (-1)),
-                                            new Location (-1));
+                       c = new Constructor (Basename, Parameters.EmptyReadOnlyParameters,
+                                            new ConstructorBaseInitializer (
+                                                    null, Parameters.EmptyReadOnlyParameters,
+                                                    Location.Null),
+                                            Location.Null);
                        
                        if (is_static)
                                mods = Modifiers.STATIC;
@@ -548,306 +581,10 @@ namespace Mono.CSharp {
                        }
                }
 
-               struct TypeAndMethods {
-                       public Type          type;
-                       public MethodInfo [] methods;
-
-                       // Far from ideal, but we want to avoid creating a copy
-                       // of methods above.
-                       public Type [][]     args;
-
-                       //
-                       // This flag on the method says `We found a match, but
-                       // because it was private, we could not use the match
-                       //
-                       public bool []       found;
-               }
-
-               //
-               // This array keeps track of the pending implementations
-               // 
-               TypeAndMethods [] pending_implementations;
-
-               //
-               // Returns a list of the abstract methods that are exposed by all of our
-               // parents that we must implement.  Notice that this `flattens' the
-               // method search space, and takes into account overrides.  
-               //
-               ArrayList GetAbstractMethods (Type t)
-               {
-                       ArrayList list = null;
-                       bool searching = true;
-                       Type current_type = t;
-                       
-                       do {
-                               MemberInfo [] mi;
-                               
-                               mi = FindMembers (
-                                       current_type, MemberTypes.Method,
-                                       BindingFlags.Public | BindingFlags.Instance |
-                                       BindingFlags.DeclaredOnly,
-                                       virtual_method_filter, null);
-
-                               if (current_type == TypeManager.object_type)
-                                       searching = false;
-                               else {
-                                       current_type = current_type.BaseType;
-                                       if (!current_type.IsAbstract)
-                                               searching = false;
-                               }
-
-                               if (mi == null)
-                                       continue;
-
-                               int count = mi.Length;
-                               if (count == 0)
-                                       continue;
-
-                               if (count == 1 && !(mi [0] is MethodBase))
-                                       searching = false;
-                               else 
-                                       list = Expression.CopyNewMethods (list, mi);
-                       } while (searching);
-
-                       if (list == null)
-                               return null;
-                       
-                       for (int i = 0; i < list.Count; i++){
-                               while (list.Count > i && !((MethodInfo) list [i]).IsAbstract)
-                                       list.RemoveAt (i);
-                       }
-
-                       if (list.Count == 0)
-                               return null;
-
-                       return list;
-               }
-               
-               //
-               // Registers the required method implementations for this class
-               //
-               // Register method implementations are either abstract methods
-               // flagged as such on the base class or interface methods
-               //
-               public void RegisterRequiredImplementations ()
-               {
-                       Type [] ifaces = TypeBuilder.GetInterfaces ();
-                       Type b = TypeBuilder.BaseType;
-                       int icount = 0;
-
-                       if (false)
-                       {
-                               Type x = TypeBuilder;
-
-                               while (x != null){
-                                       Type [] iff = x.GetInterfaces ();
-                                       Console.WriteLine ("Type: " + x.Name);
-                                       
-                                       foreach (Type tt in iff){
-                                               Console.WriteLine ("  Iface: " + tt.Name);
-                                       }
-                                       x = x.BaseType;
-                               }
-                       }
-                                       
-                       icount = ifaces.Length;
-
-                       //
-                       // If we are implementing an abstract class, and we are not
-                       // ourselves abstract, and there are abstract methods (C# allows
-                       // abstract classes that have no abstract methods), then allocate
-                       // one slot.
-                       //
-                       // We also pre-compute the methods.
-                       //
-                       bool implementing_abstract = (b.IsAbstract && !TypeBuilder.IsAbstract);
-                       ArrayList abstract_methods = null;
-
-                       if (implementing_abstract){
-                               abstract_methods = GetAbstractMethods (b);
-                               
-                               if (abstract_methods == null)
-                                       implementing_abstract = false;
-                       }
-                       
-                       int total = icount +  (implementing_abstract ? 1 : 0);
-                       if (total == 0)
-                               return;
-
-                       pending_implementations = new TypeAndMethods [total];
-                       
-                       int i = 0;
-                       if (ifaces != null){
-                               foreach (Type t in ifaces){
-                                       MethodInfo [] mi;
-
-                                       if (t is TypeBuilder){
-                                               Interface iface;
-
-                                               iface = RootContext.TypeManager.LookupInterface (t);
-                                               
-                                               mi = iface.GetMethods ();
-                                       } else
-                                               mi = t.GetMethods ();
-
-                                       int count = mi.Length;
-                                       pending_implementations [i].type = t;
-                                       pending_implementations [i].methods = mi;
-                                       pending_implementations [i].args = new Type [count][];
-                                       pending_implementations [i].found = new bool [count];
-
-                                       int j = 0;
-                                       foreach (MethodInfo m in mi){
-                                               Type [] types = TypeManager.GetArgumentTypes (m);
-
-                                               pending_implementations [i].args [j] = types;
-                                               j++;
-                                       }
-                                       i++;
-                               }
-                       }
-
-                       if (abstract_methods != null){
-                               int count = abstract_methods.Count;
-                               pending_implementations [i].methods = new MethodInfo [count];
-                               
-                               abstract_methods.CopyTo (pending_implementations [i].methods, 0);
-                               pending_implementations [i].found = new bool [count];
-                               pending_implementations [i].args = new Type [count][];
-                               pending_implementations [i].type = TypeBuilder;
-                               
-                               int j = 0;
-                               foreach (MemberInfo m in abstract_methods){
-                                       MethodInfo mi = (MethodInfo) m;
-                                       
-                                       Type [] types = TypeManager.GetArgumentTypes (mi);
-                                       
-                                       pending_implementations [i].args [j] = types;
-                                       j++;
-                               }
-                       }
-               }
-
-               public static string MakeFQN (string nsn, string name)
-               {
-                       string prefix = (nsn == "" ? "" : nsn + ".");
-
-                       return prefix + name;
-               }
-                      
-               Type LookupInterfaceOrClass (object builder, string ns, string name, bool is_class, out bool error)
-               {
-                       TypeContainer parent;
-                       Type t;
-
-                       error = false;
-                       name = MakeFQN (ns, name);
-                       
-                       t  = RootContext.TypeManager.LookupType (name);
-                       if (t != null)
-                               return t;
-
-                       if (is_class)
-                               parent = (Class) RootContext.Tree.Classes [name];
-                       else 
-                               parent = (Struct) RootContext.Tree.Structs [name];
-
-                       if (parent == null) {
-                               Enum en = null;
-                               
-                               if (RootContext.Tree.Enums != null)
-                                       en = (Enum) RootContext.Tree.Enums [name];
-                               
-                               if (en != null) {
-                                       t = en.DefineEnum (builder);
-                                       
-                                       if (t != null)
-                                               return t;
-                               }
-                       }
-                       
-                       if (parent != null){
-                               t = parent.DefineType (builder);
-                               if (t == null){
-                                       Report.Error (146, "Class definition is circular: `"+name+"'");
-                                       error = true;
-                                       return null;
-                               }
-
-                               return t;
-                       }
-
-                       return null;
-               }
-               
-               //
-               // returns the type for an interface or a class, this will recursively
-               // try to define the types that it depends on.
-               //
-               Type GetInterfaceOrClass (object builder, string name, bool is_class)
-               {
-                       Type t;
-                       bool error;
-
-                       //
-                       // Attempt to lookup the class on our namespace and all it's implicit parents
-                       //
-                       for (string ns = Namespace.Name; ns != null; ns = RootContext.ImplicitParent (ns)) {
-
-                               t = LookupInterfaceOrClass (builder, ns, name, is_class, out error);
-                               if (error)
-                                       return null;
-                               
-                               if (t != null) 
-                                       return t;
-                       }
-                       
-                       //
-                       // Attempt to do a direct unqualified lookup
-                       //
-                       t = LookupInterfaceOrClass (builder, "", name, is_class, out error);
-                       if (error)
-                               return null;
-                       
-                       if (t != null)
-                               return t;
-                       
-                       //
-                       // Attempt to lookup the class on any of the `using'
-                       // namespaces
-                       //
-
-                       for (Namespace ns = Namespace; ns != null; ns = ns.Parent){
-
-                               t = LookupInterfaceOrClass (builder, ns.Name, name, is_class, out error);
-                               if (error)
-                                       return null;
-
-                               if (t != null)
-                                       return t;
-
-                               //
-                               // Now check the using clause list
-                               //
-                               ArrayList using_list = ns.UsingTable;
-                               
-                               if (using_list == null)
-                                       continue;
-
-                               foreach (string n in using_list){
-                                       t = LookupInterfaceOrClass (builder, n, name, is_class, out error);
-                                       if (error)
-                                               return null;
-
-                                       if (t != null)
-                                               return t;
-                               }
-                               
-                       }
-
-                       Report.Error (246, Location, "Can not find type `"+name+"'");
-                       return null;
-               }
+               /// <remarks>
+               ///  The pending methods that need to be implemented (interfaces or abstract methods)
+               /// </remarks>
+               public PendingImplementation Pending;
 
                /// <summary>
                ///   This function computes the Base class and also the
@@ -859,7 +596,7 @@ namespace Mono.CSharp {
                ///   The @parent argument is set to the parent object or null
                ///   if this is `System.Object'. 
                /// </summary>
-               Type [] GetClassBases (object builder, bool is_class, out Type parent, out bool error)
+               Type [] GetClassBases (bool is_class, out Type parent, out bool error)
                {
                        ArrayList bases = Bases;
                        int count;
@@ -897,14 +634,16 @@ namespace Mono.CSharp {
                        count = bases.Count;
 
                        if (is_class){
-                               string name = (string) bases [0];
-                               Type first = GetInterfaceOrClass (builder, name, is_class);
+                               Expression name = (Expression) bases [0];
+                               name = ResolveTypeExpr (name, false, Location);
 
-                               if (first == null){
+                               if (name == null){
                                        error = true;
                                        return null;
                                }
-                               
+
+                               Type first = name.Type;
+
                                if (first.IsClass){
                                        parent = first;
                                        start = 1;
@@ -912,6 +651,14 @@ namespace Mono.CSharp {
                                        parent = TypeManager.object_type;
                                        start = 0;
                                }
+
+                               if (!AsAccessible (parent, ModFlags))
+                                       Report.Error (60, Location,
+                                                     "Inconsistent accessibility: base class `" +
+                                                     TypeManager.CSharpName (parent) + "' is less " +
+                                                     "accessible than class `" +
+                                                     Name + "'");
+
                        } else {
                                start = 0;
                        }
@@ -919,9 +666,11 @@ namespace Mono.CSharp {
                        Type [] ifaces = new Type [count-start];
                        
                        for (i = start, j = 0; i < count; i++, j++){
-                               string name = (string) bases [i];
-                               Type t = GetInterfaceOrClass (builder, name, is_class);
-                               
+                               Expression name = (Expression) bases [i];
+                               Expression resolved = ResolveTypeExpr (name, false, Location);
+                               bases [i] = resolved;
+                               Type t = resolved.Type;
+
                                if (t == null){
                                        error = true;
                                        return null;
@@ -955,20 +704,27 @@ namespace Mono.CSharp {
                                                return null;
                                        }
                                }
-                               
+
+                               for (int x = 0; x < j; x++) {
+                                       if (t == ifaces [x]) {
+                                               Report.Error (528, "`" + name + "' is already listed in interface list");
+                                               error = true;
+                                               return null;
+                                       }
+                               }
+
                                ifaces [j] = t;
                        }
 
-                       return ifaces;
+                       return TypeManager.ExpandInterfaces (ifaces);
                }
                
                //
                // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
                //
-               public TypeBuilder DefineType (object parent_builder)
+               public override TypeBuilder DefineType ()
                {
                        Type parent;
-                       Type [] ifaces;
                        bool error;
                        bool is_class;
 
@@ -979,18 +735,20 @@ namespace Mono.CSharp {
                                return null;
                        
                        InTransit = true;
-                       
+
                        if (this is Class)
                                is_class = true;
                        else
                                is_class = false;
 
-                       ifaces = GetClassBases (parent_builder, is_class, out parent, out error); 
+                       ec = new EmitContext (this, Mono.CSharp.Location.Null, null, null, ModFlags);
+
+                       ifaces = GetClassBases (is_class, out parent, out error); 
                        
                        if (error)
                                return null;
 
-                       if (this is Class){
+                       if (is_class && parent != null){
                                if (parent == TypeManager.enum_type ||
                                    (parent == TypeManager.value_type && RootContext.StdLib) ||
                                    parent == TypeManager.delegate_type ||
@@ -1001,17 +759,24 @@ namespace Mono.CSharp {
                                        return null;
                                }
                        }
-                       
-                       if (parent_builder is ModuleBuilder) {
-                               ModuleBuilder builder = (ModuleBuilder) parent_builder;
+
+                       if (!is_class && TypeManager.value_type == null)
+                               throw new Exception ();
+
+                       TypeAttributes type_attributes = TypeAttr;
+
+                       // if (parent_builder is ModuleBuilder) {
+                       if (IsTopLevel){
+                               ModuleBuilder builder = CodeGen.ModuleBuilder;
+                               
                                //
                                // Structs with no fields need to have a ".size 1"
                                // appended
                                //
 
-                               if (!is_class && Fields == null)
+                               if (!is_class && !have_nonstatic_fields)
                                        TypeBuilder = builder.DefineType (Name,
-                                                                         TypeAttr,
+                                                                         type_attributes,
                                                                          parent, 
                                                                          PackingSize.Unspecified, 1);
                                else
@@ -1019,49 +784,73 @@ namespace Mono.CSharp {
                                // classes or structs with fields
                                //
                                        TypeBuilder = builder.DefineType (Name,
-                                                                         TypeAttr,
+                                                                         type_attributes,
                                                                          parent,
                                                                          ifaces);
                        } else {
-                               TypeBuilder builder = (System.Reflection.Emit.TypeBuilder) parent_builder;
+                               TypeBuilder builder = Parent.TypeBuilder;
                                
                                //
                                // Structs with no fields need to have a ".size 1"
                                // appended
                                //
-                               if (!is_class && Fields == null)
+                               if (!is_class && !have_nonstatic_fields)
                                        TypeBuilder = builder.DefineNestedType (Basename,
-                                                                               TypeAttr,
+                                                                               type_attributes,
                                                                                parent, 
                                                                                PackingSize.Unspecified);
-                               else
-                               //
-                               // classes or structs with fields
-                               //
+                               else {
+                                       //
+                                       // classes or structs with fields
+                                       //
                                        TypeBuilder = builder.DefineNestedType (Basename,
-                                                                               TypeAttr,
+                                                                               type_attributes,
                                                                                parent,
                                                                                ifaces);
+                               }
+                       }
+
+                       // add interfaces that were not added at type creation (weird API issue)
+                       if (!is_class && !have_nonstatic_fields && (ifaces != null)) {
+                               foreach (Type i in ifaces)
+                                       TypeBuilder.AddInterfaceImplementation (i);
                        }
+                       
+                       //
+                       // Finish the setup for the EmitContext
+                       //
+                       ec.ContainerType = TypeBuilder;
 
-                       RootContext.TypeManager.AddUserType (Name, TypeBuilder, this);
-                       RootContext.RegisterOrder (this);
+                       TypeManager.AddUserType (Name, TypeBuilder, this, ifaces);
+
+                       if ((parent != null) &&
+                           (parent == TypeManager.attribute_type ||
+                            parent.IsSubclassOf (TypeManager.attribute_type))) {
+                               RootContext.RegisterAttribute (this);
+                               TypeManager.RegisterAttrType (TypeBuilder, this);
+                       } else
+                               RootContext.RegisterOrder (this); 
+                               
+                       if (Interfaces != null) {
+                               foreach (Interface iface in Interfaces)
+                                       iface.DefineType ();
+                       }
                        
                        if (Types != null) {
                                foreach (TypeContainer tc in Types)
-                                       tc.DefineType (TypeBuilder);
+                                       tc.DefineType ();
                        }
 
                        if (Delegates != null) {
                                foreach (Delegate d in Delegates)
-                                       d.DefineDelegate (TypeBuilder);
+                                       d.DefineType ();
                        }
 
                        if (Enums != null) {
                                foreach (Enum en in Enums)
-                                       en.DefineEnum (TypeBuilder);
+                                       en.DefineType ();
                        }
-                       
+
                        InTransit = false;
                        return TypeBuilder;
                }
@@ -1088,37 +877,29 @@ namespace Mono.CSharp {
                                                
                                if (defined_names == null)
                                        continue;
-                               
+
                                idx = Array.BinarySearch (defined_names, mc.Name, mif_compare);
-                               
                                if (idx < 0){
                                        if (RootContext.WarningLevel >= 4){
                                                if ((mc.ModFlags & Modifiers.NEW) != 0)
-                                                       Report109 (mc.Location, mc);
+                                                       Warning_KewywordNewNotRequired (mc.Location, mc);
                                        }
                                        continue;
                                }
 
-                               if (defined_names [idx] is PropertyInfo &&
-                                   ((mc.ModFlags & Modifiers.OVERRIDE) != 0)){
+                               MemberInfo match = defined_names [idx];
+
+                               if (match is PropertyInfo && ((mc.ModFlags & Modifiers.OVERRIDE) != 0))
                                        continue;
-                               }
-                                   
-#if WANT_TO_VERIFY_SIGNATURES_HERE
-                               if (defined_names [idx] is MethodBase && mc is MethodCore){
-                                       MethodBase mb = (MethodBase) defined_names [idx];
-                                       MethodCore met = (MethodCore) mc;
-                                       
-                                       if ((mb.IsVirtual || mb.IsAbstract) &&
-                                           (mc.ModFlags & Modifiers.OVERRIDE) != 0)
-                                               continue;
 
-                                       //
-                                       // FIXME: Compare the signatures here.  If they differ,
-                                       // then: `continue;' 
-                               }
-#endif
-                               Report108 (mc.Location, defined_names [idx]);
+                               //
+                               // 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)
+                                       Warning_KeywordNewRequired (mc.Location, defined_names [idx]);
                        }
                        
                        foreach (object o in remove_list)
@@ -1126,14 +907,67 @@ namespace Mono.CSharp {
                        
                        remove_list.Clear ();
                }
-               
+
+               //
+               // Defines the indexers, and also verifies that the IndexerNameAttribute in the
+               // class is consisten.  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 DefaultName attribute is attached
+               // to the class.
+               //
+               void DefineIndexers ()
+               {
+                       string class_indexer_name = null;
+                       
+                       foreach (Indexer i in Indexers){
+                               string name;
+                               
+                               i.Define (this);
+
+                               name = i.IndexerName;
+
+                               if (i.InterfaceType != null)
+                                       continue;
+
+                               if (class_indexer_name == null){
+                                       class_indexer_name = name;
+                                       continue;
+                               }
+                               
+                               if (name == class_indexer_name)
+                                       continue;
+                               
+                               Report.Error (
+                                       668, "Two indexers have different names, " +
+                                       " you should use the same name for all your indexers");
+                       }
+                       if (class_indexer_name == null)
+                               class_indexer_name = "Item";
+                       IndexerName = class_indexer_name;
+               }
+
+               static void Report1530 (Location loc)
+               {
+                       Report.Error (1530, loc, "Keyword new not allowed for namespace elements");
+               }
+
                /// <summary>
                ///   Populates our TypeBuilder with fields and methods
                /// </summary>
-               public override bool Define (TypeContainer parent)
+               public override bool DefineMembers (TypeContainer parent)
                {
                        MemberInfo [] defined_names = null;
 
+                       if (interface_order != null){
+                               foreach (Interface iface in interface_order)
+                                       if ((iface.ModFlags & Modifiers.NEW) == 0)
+                                               iface.DefineMembers (this);
+                                       else
+                                               Report1530 (iface.Location);
+                       }
+
                        if (RootContext.WarningLevel > 1){
                                Type ptype;
 
@@ -1143,7 +977,7 @@ namespace Mono.CSharp {
                                //
                                ptype = TypeBuilder.BaseType;
                                if (ptype != null){
-                                       defined_names = FindMembers (
+                                       defined_names = (MemberInfo []) FindMembers (
                                                ptype, MemberTypes.All & ~MemberTypes.Constructor,
                                                BindingFlags.Public | BindingFlags.Instance |
                                                BindingFlags.Static, null, null);
@@ -1160,7 +994,7 @@ namespace Mono.CSharp {
 
                        if (this is Class){
                                if (instance_constructors == null){
-                                       if (default_constructor == null) 
+                                       if (default_constructor == null)
                                                DefineDefaultConstructor (false);
                                }
 
@@ -1182,8 +1016,8 @@ namespace Mono.CSharp {
                                        ReportStructInitializedInstanceError ();
                        }
 
-                       RegisterRequiredImplementations ();
-
+                       Pending = PendingImplementation.GetPendingImplementations (this);
+                       
                        //
                        // Constructors are not in the defined_names array
                        //
@@ -1194,7 +1028,7 @@ namespace Mono.CSharp {
                                default_static_constructor.Define (this);
                        
                        if (methods != null)
-                               DefineMembers (methods, null);
+                               DefineMembers (methods, defined_names);
 
                        if (properties != null)
                                DefineMembers (properties, defined_names);
@@ -1203,9 +1037,9 @@ namespace Mono.CSharp {
                                DefineMembers (events, defined_names);
 
                        if (indexers != null) {
-                               foreach (Indexer i in Indexers)
-                                       i.Define (this);
-                       }
+                               DefineIndexers ();
+                       } else
+                               IndexerName = "Item";
 
                        if (operators != null)
                                DefineMembers (operators, null);
@@ -1219,17 +1053,17 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               /// <summary>
-               ///   Looks up the alias for the name
-               /// </summary>
-               public string LookupAlias (string name)
+               public override bool Define (TypeContainer parent)
                {
-                       if (Namespace != null)
-                               return Namespace.LookupAlias (name);
-                       else
-                               return null;
+                       if (interface_order != null){
+                               foreach (Interface iface in interface_order)
+                                       if ((iface.ModFlags & Modifiers.NEW) == 0)
+                                               iface.Define (this);
+                       }
+
+                       return true;
                }
-               
+
                /// <summary>
                ///   This function is based by a delegate to the FindMembers routine
                /// </summary>
@@ -1244,19 +1078,6 @@ namespace Mono.CSharp {
                /// </summary>
                static MemberFilter accepting_filter;
 
-               static bool IsVirtualFilter (MemberInfo m, object filterCriteria)
-               {
-                       if (!(m is MethodInfo))
-                               return false;
-
-                       return ((MethodInfo) m).IsVirtual;
-               }
-               
-               /// <summary>
-               ///   This filter is used by FindMembers, and it is used to
-               ///   extract only virtual/abstract fields
-               /// </summary>
-               static MemberFilter virtual_method_filter;
                
                /// <summary>
                ///   A member comparission method based on name only
@@ -1266,7 +1087,6 @@ namespace Mono.CSharp {
                static TypeContainer ()
                {
                        accepting_filter = new MemberFilter (AlwaysAccept);
-                       virtual_method_filter = new MemberFilter (IsVirtualFilter);
                        mif_compare = new MemberInfoCompare ();
                }
                
@@ -1279,27 +1099,46 @@ namespace Mono.CSharp {
                // FIXME: return an empty static array instead of null, that cleans up
                // some code and is consistent with some coding conventions I just found
                // out existed ;-)
-               public MemberInfo [] FindMembers (MemberTypes mt, BindingFlags bf,
-                                                 MemberFilter filter, object criteria)
+               //
+               //
+               // Notice that in various cases we check if our field is non-null,
+               // something that would normally mean that there was a bug elsewhere.
+               //
+               // The problem happens while we are defining p-invoke methods, as those
+               // will trigger a FindMembers, but this happens before things are defined
+               //
+               // Since the whole process is a no-op, it is fine to check for null here.
+               //
+               internal override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
+                                                         MemberFilter filter, object criteria)
                {
                        ArrayList members = new ArrayList ();
+                       bool priv = (bf & BindingFlags.NonPublic) != 0;
 
                        if (filter == null)
                                filter = accepting_filter; 
-                       
+
                        if ((mt & MemberTypes.Field) != 0) {
                                if (fields != null) {
                                        foreach (Field f in fields) {
+                                               if ((f.ModFlags & Modifiers.PRIVATE) != 0)
+                                                       if (!priv)
+                                                               continue;
+
                                                FieldBuilder fb = f.FieldBuilder;
-                                               if (filter (fb, criteria) == true)
+                                               if (fb != null && filter (fb, criteria) == true)
                                                        members.Add (fb);
                                        }
                                }
 
                                if (constants != null) {
                                        foreach (Const con in constants) {
+                                               if ((con.ModFlags & Modifiers.PRIVATE) != 0)
+                                                       if (!priv)
+                                                               continue;
+                                               
                                                FieldBuilder fb = con.FieldBuilder;
-                                               if (filter (fb, criteria) == true)
+                                               if (fb != null && filter (fb, criteria) == true)
                                                        members.Add (fb);
                                        }
                                }
@@ -1308,31 +1147,35 @@ namespace Mono.CSharp {
                        if ((mt & MemberTypes.Method) != 0) {
                                if (methods != null) {
                                        foreach (Method m in methods) {
+                                               if ((m.ModFlags & Modifiers.PRIVATE) != 0)
+                                                       if (!priv)
+                                                               continue;
+                                               
                                                MethodBuilder mb = m.MethodBuilder;
 
-                                               // If we are in transit, ignore
-                                               // This case arises when we are still defining a PInvoke method
-                                               // and we hit FindMembers because of the need to resolve named
-                                               // arguments inside of Attribute.DefinePInvokeMethod
-                                               if (mb == null)
-                                                       continue;
-
-                                               if (filter (mb, criteria) == true)
+                                               if (mb != null && filter (mb, criteria) == true)
                                                        members.Add (mb);
                                        }
                                }
 
                                if (operators != null){
                                        foreach (Operator o in operators) {
+                                               if ((o.ModFlags & Modifiers.PRIVATE) != 0)
+                                                       if (!priv)
+                                                               continue;
+                                               
                                                MethodBuilder ob = o.OperatorMethodBuilder;
-
-                                               if (filter (ob, criteria) == true)
+                                               if (ob != null && filter (ob, criteria) == true)
                                                        members.Add (ob);
                                        }
                                }
 
                                if (properties != null){
                                        foreach (Property p in properties){
+                                               if ((p.ModFlags & Modifiers.PRIVATE) != 0)
+                                                       if (!priv)
+                                                               continue;
+                                               
                                                MethodBuilder b;
 
                                                b = p.GetBuilder;
@@ -1349,7 +1192,12 @@ namespace Mono.CSharp {
                        if ((mt & MemberTypes.Event) != 0) {
                                if (events != null)
                                        foreach (Event e in events) {
-                                               if (filter (e.EventBuilder, criteria) == true)
+                                               if ((e.ModFlags & Modifiers.PRIVATE) != 0)
+                                                       if (!priv)
+                                                               continue;
+
+                                               MemberInfo eb = e.EventBuilder;
+                                               if (eb != null && filter (eb, criteria) == true)
                                                        members.Add (e.EventBuilder);
                                        }
                        }
@@ -1357,31 +1205,56 @@ namespace Mono.CSharp {
                        if ((mt & MemberTypes.Property) != 0){
                                if (properties != null)
                                        foreach (Property p in properties) {
-                                               if (filter (p.PropertyBuilder, criteria) == true) {
+                                               if ((p.ModFlags & Modifiers.PRIVATE) != 0)
+                                                       if (!priv)
+                                                               continue;
+
+                                               MemberInfo pb = p.PropertyBuilder;
+                                               if (pb != null && filter (pb, criteria) == true) {
                                                        members.Add (p.PropertyBuilder);
                                                }
                                        }
 
                                if (indexers != null)
                                        foreach (Indexer ix in indexers) {
-                                               if (filter (ix.PropertyBuilder, criteria) == true) {
+                                               if ((ix.ModFlags & Modifiers.PRIVATE) != 0)
+                                                       if (!priv)
+                                                               continue;
+
+                                               MemberInfo ib = ix.PropertyBuilder;
+                                               if (ib != null && filter (ib, criteria) == true) {
                                                        members.Add (ix.PropertyBuilder);
                                                }
                                        }
                        }
                        
                        if ((mt & MemberTypes.NestedType) != 0) {
+                               if (types != null){
+                                       foreach (TypeContainer t in types) {
+                                               TypeBuilder tb = t.TypeBuilder;
+
+                                               if (tb != null && (filter (tb, criteria) == true))
+                                                               members.Add (tb);
+                                       }
+                               }
 
-                               if (Types != null)
-                                       foreach (TypeContainer t in Types)  
-                                               if (filter (t.TypeBuilder, criteria) == true)
-                                                       members.Add (t.TypeBuilder);
+                               if (enums != null){
+                                       foreach (Enum en in enums){
+                                               TypeBuilder tb = en.TypeBuilder;
 
-                               if (Enums != null)
-                                       foreach (Enum en in Enums)
-                                               if (filter (en.TypeBuilder, criteria) == true)
-                                                       members.Add (en.TypeBuilder);
+                                               if (tb != null && (filter (tb, criteria) == true))
+                                                       members.Add (tb);
+                                       }
+                               }
                                
+                               if (delegates != null){
+                                       foreach (Delegate d in delegates){
+                                               TypeBuilder tb = d.TypeBuilder;
+                                               
+                                               if (tb != null && (filter (tb, criteria) == true))
+                                                       members.Add (tb);
+                                       }
+                               }
                        }
 
                        if ((mt & MemberTypes.Constructor) != 0){
@@ -1389,8 +1262,9 @@ namespace Mono.CSharp {
                                        foreach (Constructor c in instance_constructors){
                                                ConstructorBuilder cb = c.ConstructorBuilder;
 
-                                               if (filter (cb, criteria) == true)
-                                                       members.Add (cb);
+                                               if (cb != null)
+                                                       if (filter (cb, criteria) == true)
+                                                               members.Add (cb);
                                        }
                                }
 
@@ -1398,6 +1272,7 @@ namespace Mono.CSharp {
                                        ConstructorBuilder cb =
                                                default_static_constructor.ConstructorBuilder;
                                        
+                                       if (cb != null)
                                        if (filter (cb, criteria) == true)
                                                members.Add (cb);
                                }
@@ -1406,49 +1281,25 @@ namespace Mono.CSharp {
                        //
                        // Lookup members in parent if requested.
                        //
-                       if ((bf & BindingFlags.DeclaredOnly) == 0){
-                               MemberInfo [] mi;
-
-                               mi = FindMembers (TypeBuilder.BaseType, mt, bf, filter, criteria);
-                               if (mi != null)
-                                       members.AddRange (mi);
-                       }
-                       
-                       int count = members.Count;
-                       if (count > 0){
-                               MemberInfo [] mi = new MemberInfo [count];
-                               members.CopyTo (mi);
-                               return mi;
+                       if (((bf & BindingFlags.DeclaredOnly) == 0) && (TypeBuilder.BaseType != null)) {
+                               MemberList list = FindMembers (TypeBuilder.BaseType, mt, bf, filter, criteria);
+                               members.AddRange (list);
                        }
 
-                       return null;
+                       return new MemberList (members);
                }
 
-               public MemberInfo GetFieldFromEvent (EventExpr event_expr)
-               {
-                       EventInfo ei = event_expr.EventInfo;
-
-                       foreach (Event e in events) { 
-
-                               if (e.FieldBuilder == null)
-                                       continue;
-                               
-                               if (Type.FilterName (e.FieldBuilder, ei.Name))
-                                       return e.FieldBuilder;
-                       }
-
-                       return null;
-               }
+               
 
-               public static MemberInfo [] FindMembers (Type t, MemberTypes mt, BindingFlags bf,
-                                                        MemberFilter filter, object criteria)
+               public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
+                                                     MemberFilter filter, object criteria)
                {
                        TypeContainer tc = TypeManager.LookupTypeContainer (t);
 
                        if (tc != null)
                                return tc.FindMembers (mt, bf, filter, criteria);
                        else
-                               return t.FindMembers (mt, bf, filter, criteria);
+                               return new MemberList (t.FindMembers (mt, bf, filter, criteria));
                }
 
                //
@@ -1461,202 +1312,6 @@ namespace Mono.CSharp {
                        return null;
                }
 
-               /// <summary>
-               ///   Whether the specified method is an interface method implementation
-               /// </summary>
-               ///
-               /// <remarks>
-               ///   If a method in Type `t' (or null to look in all interfaces
-               ///   and the base abstract class) with name `Name', return type `ret_type' and
-               ///   arguments `args' implements an interface, this method will
-               ///   return the MethodInfo that this method implements.
-               ///
-               ///   This will remove the method from the list of "pending" methods
-               ///   that are required to be implemented for this class as a side effect.
-               /// 
-               /// </remarks>
-               public MethodInfo IsInterfaceMethod (Type t, string Name, Type ret_type, Type [] args,
-                                                    bool clear)
-               {
-                       int arg_len = args.Length;
-
-                       if (pending_implementations == null)
-                               return null;
-
-                       foreach (TypeAndMethods tm in pending_implementations){
-                               if (!(t == null || tm.type == t))
-                                       continue;
-
-                               int i = 0;
-                               foreach (MethodInfo m in tm.methods){
-                                       if (m == null){
-                                               i++;
-                                               continue;
-                                       }
-
-                                       if (Name != m.Name){
-                                               i++;
-                                               continue;
-                                       }
-
-                                       if (ret_type != m.ReturnType){
-                                               if (!((ret_type == null && m.ReturnType == TypeManager.void_type) ||
-                                                     (m.ReturnType == null && ret_type == TypeManager.void_type)))
-                                               {
-                                                       i++;
-                                                       continue;
-                                               }
-                                       }
-
-                                       //
-                                       // Check if we have the same parameters
-                                       //
-                                       if (tm.args [i].Length != arg_len){
-                                               i++;
-                                               continue;
-                                       }
-
-                                       int j, top = args.Length;
-                                       bool fail = false;
-                                       
-                                       for (j = 0; j < top; j++){
-                                               if (tm.args [i][j] != args[j]){
-                                                       fail = true;
-                                                       break;
-                                               }
-                                       }
-                                       if (fail){
-                                               i++;
-                                               continue;
-                                       }
-
-                                       if (clear)
-                                               tm.methods [i] = null;
-                                       tm.found [i] = true;
-                                       return m;
-                               }
-
-                               // If a specific type was requested, we can stop now.
-                               if (tm.type == t)
-                                       return null;
-                       }
-                       return null;
-               }
-
-               /// <summary>
-               ///   C# allows this kind of scenarios:
-               ///   interface I { void M (); }
-               ///   class X { public void M (); }
-               ///   class Y : X, I { }
-               ///
-               ///   For that case, we create an explicit implementation function
-               ///   I.M in Y.
-               /// </summary>
-               void DefineProxy (Type iface, MethodInfo parent_method, MethodInfo iface_method,
-                                 Type [] args)
-               {
-                       MethodBuilder proxy;
-
-                       string proxy_name = iface.Name + "." + iface_method.Name;
-
-                       proxy = TypeBuilder.DefineMethod (
-                               proxy_name,
-                               MethodAttributes.HideBySig |
-                               MethodAttributes.NewSlot |
-                               MethodAttributes.Virtual,
-                               CallingConventions.Standard | CallingConventions.HasThis,
-                               parent_method.ReturnType, args);
-
-                       int top = args.Length;
-                       ILGenerator ig = proxy.GetILGenerator ();
-
-                       ig.Emit (OpCodes.Ldarg_0);
-                       for (int i = 0; i < top; i++){
-                               switch (i){
-                               case 0:
-                                       ig.Emit (OpCodes.Ldarg_1); break;
-                               case 1:
-                                       ig.Emit (OpCodes.Ldarg_2); break;
-                               case 2:
-                                       ig.Emit (OpCodes.Ldarg_3); break;
-                               default:
-                                       ig.Emit (OpCodes.Ldarg, i - 1); break;
-                               }
-                       }
-                       ig.Emit (OpCodes.Call, parent_method);
-                       ig.Emit (OpCodes.Ret);
-
-                       TypeBuilder.DefineMethodOverride (proxy, iface_method);
-               }
-               
-               /// <summary>
-               ///   This function tells whether one of our parent classes implements
-               ///   the given method (which turns out, it is valid to have an interface
-               ///   implementation in a parent
-               /// </summary>
-               bool ParentImplements (Type iface_type, MethodInfo mi)
-               {
-                       MethodSignature ms;
-                       
-                       Type [] args = TypeManager.GetArgumentTypes (mi);
-                       ms = new MethodSignature (mi.Name, mi.ReturnType, args);
-                       MemberInfo [] list = FindMembers (
-                               TypeBuilder.BaseType, MemberTypes.Method | MemberTypes.Property,
-                               BindingFlags.Public | BindingFlags.Instance,
-                               MethodSignature.method_signature_filter, ms);
-
-                       if (list == null || list.Length == 0)
-                               return false;
-                       
-                       DefineProxy (iface_type, (MethodInfo) list [0], mi, args);
-                       return true;
-               }
-               
-               /// <summary>
-               ///   Verifies that any pending abstract methods or interface methods
-               ///   were implemented.
-               /// </summary>
-               bool VerifyPendingMethods ()
-               {
-                       int top = pending_implementations.Length;
-                       bool errors = false;
-                       int i;
-                       
-                       for (i = 0; i < top; i++){
-                               Type type = pending_implementations [i].type;
-                               int j = 0;
-                               
-                               foreach (MethodInfo mi in pending_implementations [i].methods){
-                                       if (mi == null)
-                                               continue;
-
-                                       if (type.IsInterface){
-                                               if (ParentImplements (type, mi))
-                                                       continue;
-                                               string extra = "";
-                                               
-                                               if (pending_implementations [i].found [j])
-                                                       extra = ".  (method might be private or static)";
-                                               Report.Error (
-                                                       536, Location,
-                                                       "`" + Name + "' does not implement " +
-                                                       "interface member `" +
-                                                       type.FullName + "." + mi.Name + "'" + extra);
-                                       } else {
-                                               Report.Error (
-                                                       534, Location,
-                                                       "`" + Name + "' does not implement " +
-                                                       "inherited abstract member `" +
-                                                       type.FullName + "." + mi.Name + "'");
-                                       }
-                                       errors = true;
-                                       j++;
-                               }
-                       }
-                       return errors;
-               }
-
                /// <summary>
                ///   Emits the values for the constants
                /// </summary>
@@ -1667,7 +1322,7 @@ namespace Mono.CSharp {
                                        con.EmitConstant (this);
                        return;
                }
-               
+
                /// <summary>
                ///   Emits the code, this step is performed after all
                ///   the types, enumerations, constructors
@@ -1693,12 +1348,12 @@ namespace Mono.CSharp {
                                foreach (Property p in properties)
                                        p.Emit (this);
 
-                       if (indexers != null) {
+                       if (indexers != null){
                                foreach (Indexer ix in indexers)
                                        ix.Emit (this);
-
-                               CustomAttributeBuilder cb = Interface.EmitDefaultMemberAttr (this, ModFlags, Location);
-
+                               
+                               CustomAttributeBuilder cb = Interface.EmitDefaultMemberAttr (
+                                       this, IndexerName, ModFlags, Location);
                                TypeBuilder.SetCustomAttribute (cb);
                        }
                        
@@ -1711,21 +1366,17 @@ namespace Mono.CSharp {
                                        e.Emit (this);
                        }
 
-                       if (pending_implementations != null)
-                               if (!VerifyPendingMethods ())
+                       if (Pending != null)
+                               if (Pending.VerifyPendingMethods ())
                                        return;
-
-                       EmitContext ec = new EmitContext (
-                                                 this, Mono.CSharp.Location.Null, null, null,
-                                                 ModFlags);
-
+                       
                        Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes, Location);
 
                        //
                        // Check for internal or private fields that were never assigned
                        //
-                       if (RootContext.WarningLevel >= 3){
-                               foreach (Field f in fields){
+                       if (fields != null && RootContext.WarningLevel >= 3) {
+                               foreach (Field f in fields) {
                                        if ((f.ModFlags & Modifiers.PUBLIC) != 0)
                                                continue;
 
@@ -1741,7 +1392,7 @@ namespace Mono.CSharp {
                                        //
                                        if (RootContext.WarningLevel < 4)
                                                continue;
-                                       
+
                                        if ((f.status & Field.Status.ASSIGNED) != 0)
                                                continue;
 
@@ -1770,11 +1421,19 @@ namespace Mono.CSharp {
                                //
 //                             Report.Warning (-20, "Exception while creating class: " + TypeBuilder.Name);
 //                             Console.WriteLine (e.Message);
+                       } catch {
+                               Console.WriteLine ("In type: " + Name);
+                               throw;
                        }
                        
                        if (Enums != null)
                                foreach (Enum en in Enums)
                                        en.CloseType ();
+
+                       if (interface_order != null){
+                               foreach (Interface iface in interface_order)
+                                       iface.CloseType ();
+                       }
                        
                        if (Types != null){
                                foreach (TypeContainer tc in Types)
@@ -1796,7 +1455,7 @@ namespace Mono.CSharp {
                        return "`" + Name + "." + n + "'";
                }
 
-               public void Report108 (Location l, MemberInfo mi)
+               public void Warning_KeywordNewRequired (Location l, MemberInfo mi)
                {
                        Report.Warning (
                                108, l, "The keyword new is required on " + 
@@ -1804,7 +1463,7 @@ namespace Mono.CSharp {
                                mi.ReflectedType.Name + "." + mi.Name + "'");
                }
 
-               public void Report109 (Location l, MemberCore mc)
+               public void Warning_KewywordNewNotRequired (Location l, MemberCore mc)
                {
                        Report.Warning (
                                109, l, "The member " + MakeName (mc.Name) + " does not hide an " +
@@ -1823,6 +1482,7 @@ namespace Mono.CSharp {
                public bool MethodModifiersValid (int flags, string n, Location loc)
                {
                        const int vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE);
+                       const int va = (Modifiers.VIRTUAL | Modifiers.ABSTRACT);
                        const int nv = (Modifiers.NEW | Modifiers.VIRTUAL);
                        bool ok = true;
                        string name = MakeName (n);
@@ -1839,6 +1499,13 @@ namespace Mono.CSharp {
                                }
                        }
 
+                       if (this is Struct){
+                               if ((flags & va) != 0){
+                                       Modifiers.Error_InvalidModifier (loc, "virtual or abstract");
+                                       ok = false;
+                               }
+                       }
+
                        if ((flags & Modifiers.OVERRIDE) != 0 && (flags & nv) != 0){
                                Report.Error (
                                        113, loc, name +
@@ -1893,13 +1560,103 @@ namespace Mono.CSharp {
                        return ok;
                }
 
+               // Access level of a type.
+               enum AccessLevel {
+                       Public                  = 0,
+                       ProtectedInternal       = 1,
+                       Internal                = 2,
+                       Protected               = 3,
+                       Private                 = 4
+               }
+
+               // Check whether `flags' denotes a more restricted access than `level'
+               // and return the new level.
+               static AccessLevel CheckAccessLevel (AccessLevel level, int flags)
+               {
+                       AccessLevel old_level = level;
+
+                       if ((flags & Modifiers.INTERNAL) != 0) {
+                               if ((flags & Modifiers.PROTECTED) != 0) {
+                                       if ((int) level < (int) AccessLevel.ProtectedInternal)
+                                               level = AccessLevel.ProtectedInternal;
+                               } else {
+                                       if ((int) level < (int) AccessLevel.Internal)
+                                               level = AccessLevel.Internal;
+                               }
+                       } else if ((flags & Modifiers.PROTECTED) != 0) {
+                               if ((int) level < (int) AccessLevel.Protected)
+                                       level = AccessLevel.Protected;
+                       } else if ((flags & Modifiers.PRIVATE) != 0)
+                               level = AccessLevel.Private;
+
+                       return level;
+               }
+
+               // Return the access level for a new member which is defined in the current
+               // TypeContainer with access modifiers `flags'.
+               AccessLevel GetAccessLevel (int flags)
+               {
+                       if ((flags & Modifiers.PRIVATE) != 0)
+                               return AccessLevel.Private;
+
+                       AccessLevel level;
+                       if (!IsTopLevel && (Parent != null))
+                               level = Parent.GetAccessLevel (flags);
+                       else
+                               level = AccessLevel.Public;
+
+                       return CheckAccessLevel (CheckAccessLevel (level, flags), ModFlags);
+               }
+
+               // Return the access level for type `t', but don't give more access than `flags'.
+               static AccessLevel GetAccessLevel (Type t, int flags)
+               {
+                       if (((flags & Modifiers.PRIVATE) != 0) || t.IsNestedPrivate)
+                               return AccessLevel.Private;
+
+                       AccessLevel level;
+                       if (TypeManager.IsBuiltinType (t))
+                               return AccessLevel.Public;
+                       else if ((t.DeclaringType != null) && (t != t.DeclaringType))
+                               level = GetAccessLevel (t.DeclaringType, flags);
+                       else {
+                               level = CheckAccessLevel (AccessLevel.Public, flags);
+                       }
+
+                       if (t.IsNestedPublic)
+                               return level;
+
+                       if (t.IsNestedAssembly || t.IsNotPublic) {
+                               if ((int) level < (int) AccessLevel.Internal)
+                                       level = AccessLevel.Internal;
+                       }
+
+                       if (t.IsNestedFamily) {
+                               if ((int) level < (int) AccessLevel.Protected)
+                                       level = AccessLevel.Protected;
+                       }
+
+                       if (t.IsNestedFamORAssem) {
+                               if ((int) level < (int) AccessLevel.ProtectedInternal)
+                                       level = AccessLevel.ProtectedInternal;
+                       }
+
+                       return level;
+               }
+
                //
-               // Returns true if `type' is as accessible as the flags `flags'
-               // given for this member
+               // Returns true if `parent' is as accessible as the flags `flags'
+               // given for this member.
                //
-               static public bool AsAccessible (Type type, int flags)
+               public bool AsAccessible (Type parent, int flags)
                {
-                       return true;
+                       while (parent.IsArray || parent.IsPointer || parent.IsByRef)
+                               parent = parent.GetElementType ();
+
+                       AccessLevel level = GetAccessLevel (flags);
+                       AccessLevel level2 = GetAccessLevel (parent, flags);
+
+                       return (int) level >= (int) level2;
                }
 
                Hashtable builder_and_args;
@@ -1910,6 +1667,43 @@ namespace Mono.CSharp {
                                builder_and_args = new Hashtable ();
                        return true;
                }
+
+               /// <summary>
+               ///   Performs checks for an explicit interface implementation.  First it
+               ///   checks whether the `interface_type' is a base inteface implementation.
+               ///   Then it checks whether `name' exists in the interface type.
+               /// </summary>
+               public bool VerifyImplements (Type interface_type, string full, string name, Location loc)
+               {
+                       bool found = false;
+
+                       if (ifaces != null){
+                               foreach (Type t in ifaces){
+                                       if (t == interface_type){
+                                               found = true;
+                                               break;
+                                       }
+                               }
+                       }
+                       
+                       if (!found){
+                               Report.Error (540, "`" + full + "': containing class does not implement interface `" + interface_type.FullName + "'");
+                               return false;
+                       }
+
+                       return true;
+               }
+
+               public static void Error_ExplicitInterfaceNotMemberInterface (Location loc, string name)
+               {
+                       Report.Error (539, loc, "Explicit implementation: `" + name + "' is not a member of the interface");
+               }
+
+               public override MemberCache MemberCache {
+                       get {
+                               return null;
+                       }
+               }
        }
 
        public class Class : TypeContainer {
@@ -1995,35 +1789,30 @@ namespace Mono.CSharp {
                }
        }
 
-       public abstract class MethodCore : MemberCore {
+       public abstract class MethodCore : MemberBase {
                public readonly Parameters Parameters;
                Block block;
                
                //
                // Parameters, cached for semantic analysis.
                //
-               InternalParameters parameter_info;
-               
-               public MethodCore (string name, Parameters parameters, Location l)
-                       : base (name, l)
+               protected InternalParameters parameter_info;
+               protected Type [] parameter_types;
+
+               public MethodCore (Expression type, int mod, int allowed_mod, string name,
+                                  Attributes attrs, Parameters parameters, Location loc)
+                       : base (type, mod, allowed_mod, name, attrs, loc)
                {
-                       Name = name;
                        Parameters = parameters;
                }
                
                //
                //  Returns the System.Type array for the parameters of this method
                //
-               Type [] parameter_types;
-               public Type [] ParameterTypes (TypeContainer parent)
-               {
-                       if (Parameters == null)
-                               return TypeManager.NoTypes;
-                       
-                       if (parameter_types == null)
-                               parameter_types = Parameters.GetParameterInfo (parent);
-
-                       return parameter_types;
+               public Type [] ParameterTypes {
+                       get {
+                               return parameter_types;
+                       }
                }
 
                public InternalParameters ParameterInfo
@@ -2031,10 +1820,6 @@ namespace Mono.CSharp {
                        get {
                                return parameter_info;
                        }
-
-                       set {
-                               parameter_info = value;
-                       }
                }
                
                public Block Block {
@@ -2047,10 +1832,22 @@ namespace Mono.CSharp {
                        }
                }
 
-               public CallingConventions GetCallingConvention (bool is_class)
+               protected virtual bool DoDefineParameters (TypeContainer parent)
                {
-                       CallingConventions cc = 0;
-                       
+                       // Check if arguments were correct
+                       parameter_types = Parameters.GetParameterInfo (parent);
+                       if ((parameter_types == null) || !CheckParameters (parent, parameter_types))
+                               return false;
+
+                       parameter_info = new InternalParameters (parent, Parameters);
+
+                       return true;
+               }
+
+               public CallingConventions GetCallingConvention (bool is_class)
+               {
+                       CallingConventions cc = 0;
+                       
                        cc = Parameters.GetCallingConvention ();
 
                        if (is_class)
@@ -2069,9 +1866,8 @@ namespace Mono.CSharp {
                        // the argument names.
                        //
                        Parameter [] p = Parameters.FixedParameters;
-                       if (p == null)
-                               return;
-
+                       int i = 0;
+                       
                        MethodBuilder mb = null;
                        ConstructorBuilder cb = null;
 
@@ -2079,28 +1875,28 @@ namespace Mono.CSharp {
                                mb = (MethodBuilder) builder;
                        else
                                cb = (ConstructorBuilder) builder;
-                       
-                       int i;
-                               
-                       for (i = 0; i < p.Length; i++) {
-                               ParameterBuilder pb;
 
-                               if (mb == null)
-                                       pb = cb.DefineParameter (
-                                               i + 1, p [i].Attributes, p [i].Name);
-                               else 
-                                       pb = mb.DefineParameter (
-                                               i + 1, p [i].Attributes, p [i].Name);
-                               
-                               Attributes attr = p [i].OptAttributes;
-                               if (attr != null)
-                                       Attribute.ApplyAttributes (ec, pb, pb, attr, Location);
+                       if (p != null){
+                               for (i = 0; i < p.Length; i++) {
+                                       ParameterBuilder pb;
+                                       
+                                       if (mb == null)
+                                               pb = cb.DefineParameter (
+                                                       i + 1, p [i].Attributes, p [i].Name);
+                                       else 
+                                               pb = mb.DefineParameter (
+                                                       i + 1, p [i].Attributes, p [i].Name);
+                                       
+                                       Attributes attr = p [i].OptAttributes;
+                                       if (attr != null)
+                                               Attribute.ApplyAttributes (ec, pb, pb, attr, Location);
+                               }
                        }
-                       
-                       if (i != parameters.Length) {
+
+                       if (Parameters.ArrayParameter != null){
                                ParameterBuilder pb;
-                               
                                Parameter array_param = Parameters.ArrayParameter;
+                               
                                if (mb == null)
                                        pb = cb.DefineParameter (
                                                i + 1, array_param.Attributes,
@@ -2119,11 +1915,8 @@ namespace Mono.CSharp {
        }
        
        public class Method : MethodCore {
-               public readonly string ReturnType;
                public MethodBuilder MethodBuilder;
-               public readonly Attributes OptAttributes;
-
-               MethodAttributes flags;
+               public MethodData MethodData;
 
                /// <summary>
                ///   Modifiers allowed in a class declaration
@@ -2145,81 +1938,94 @@ namespace Mono.CSharp {
                //
                // return_type can be "null" for VOID values.
                //
-               public Method (string return_type, int mod, string name, Parameters parameters,
+               public Method (Expression return_type, int mod, string name, Parameters parameters,
                               Attributes attrs, Location l)
-                       : base (name, parameters, l)
-               {
-                       ReturnType = return_type;
-                       ModFlags = Modifiers.Check (AllowedModifiers, mod, Modifiers.PRIVATE, l);
-                       OptAttributes = attrs;
-               }
+                       : base (return_type, mod, AllowedModifiers, name, attrs, parameters, l)
+               { }
 
                //
                // Returns the `System.Type' for the ReturnType of this
                // function.  Provides a nice cache.  (used between semantic analysis
                // and actual code generation
                //
-               Type type_return_type;
-               
                public Type GetReturnType (TypeContainer parent)
                {
-                       if (type_return_type == null)
-                               type_return_type = RootContext.LookupType (
-                                       parent, ReturnType, false, Location);
-                       
-                       return type_return_type;
+                       return MemberType;
                }
 
-               void DuplicatEntryPoint (MethodInfo b)
-               {
-                       Report.Error (
-                               17, Location,
-                               "Program `" + RootContext.CodeGen.FileName +
-                               "'  has more than one entry point defined: `" +
-                               b.DeclaringType.Name + "." + b.Name + "'");
-               }
-               
+               // Whether this is an operator method.
+               public bool IsOperator;
+
+                void DuplicateEntryPoint (MethodInfo b, Location location)
+                {
+                        Report.Error (
+                                17, location,
+                                "Program `" + CodeGen.FileName +
+                                "'  has more than one entry point defined: `" +
+                                TypeManager.CSharpSignature(b) + "'");
+                }
+
+                void Report28 (MethodInfo b)
+                {
+                       if (RootContext.WarningLevel < 4) 
+                               return;
+                               
+                        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 &&
+                            b.ReturnType != TypeManager.int32_type)
+                                return false;
+
+                        if (pinfo.Count == 0)
+                                return true;
+
+                        if (pinfo.Count > 1)
+                                return false;
+
+                        Type t = pinfo.ParameterType(0);
+                        if (t.IsArray &&
+                            (t.GetArrayRank() == 1) &&
+                            (t.GetElementType() == TypeManager.string_type) &&
+                            (pinfo.ParameterModifier(0) == Parameter.Modifier.NONE))
+                                return true;
+                        else
+                                return false;
+                }
+
                //
-               // Creates the type
+               // Checks our base implementation if any
                //
-               public override bool Define (TypeContainer parent)
+               protected override bool CheckBase (TypeContainer parent)
                {
-                       Type ret_type = GetReturnType (parent);
-                       Type [] parameters = ParameterTypes (parent);
-                       bool error = false;
-                       MethodInfo implementing;
-                       Type iface_type = null;
-                       string iface = "", short_name;
-                       bool explicit_impl = false;
-
-                       // Check if the return type and arguments were correct
-                       if (ret_type == null || parameters == null)
-                               return false;
-                       
-                       if (!parent.MethodModifiersValid (ModFlags, Name, Location))
+                       // Check whether arguments were correct.
+                       if (!DoDefineParameters (parent))
                                return false;
 
-                       flags = Modifiers.MethodAttr (ModFlags);
+                       MethodSignature ms = new MethodSignature (Name, null, ParameterTypes);
+                       if (!IsOperator) {
+                               MemberList mi_this;
 
-                       //
-                       // verify accessibility
-                       //
-                       if (!TypeContainer.AsAccessible (ret_type, ModFlags))
-                               return false;
+                               mi_this = TypeContainer.FindMembers (
+                                       parent.TypeBuilder, MemberTypes.Method,
+                                       BindingFlags.NonPublic | BindingFlags.Public |
+                                       BindingFlags.Static | BindingFlags.Instance |
+                                       BindingFlags.DeclaredOnly,
+                                       MethodSignature.method_signature_filter, ms);
 
-                       if (ret_type.IsPointer && !UnsafeOK (parent))
-                               return false;
-                       
-                       foreach (Type partype in parameters){
-                               if (!TypeContainer.AsAccessible (partype, ModFlags))
-                                       error = true;
-                               if (partype.IsPointer && !UnsafeOK (parent))
-                                       error = true;
+                               if (mi_this.Count > 0) {
+                                       Report.Error (111, Location, "Class `" + parent.Name + "' " +
+                                                     "already defines a member called `" + Name + "' " +
+                                                     "with the same parameter types");
+                                       return false;
+                               }
                        }
 
-                       if (error)
-                               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.
@@ -2228,8 +2034,7 @@ namespace Mono.CSharp {
 
                        // ptype is only null for System.Object while compiling corlib.
                        if (ptype != null){
-                               MethodSignature ms = new MethodSignature (Name, ret_type, parameters);
-                               MemberInfo [] mi, mi_static, mi_instance;
+                               MemberList mi, mi_static, mi_instance;
 
                                mi_static = TypeContainer.FindMembers (
                                        ptype, MemberTypes.Method,
@@ -2242,180 +2047,88 @@ namespace Mono.CSharp {
                                        MethodSignature.inheritable_method_signature_filter,
                                        ms);
 
-                               if (mi_instance != null && mi_instance.Length > 0){
+                               if (mi_instance.Count > 0){
                                        mi = mi_instance;
-                               } else if (mi_static != null && mi_static.Length > 0)
+                               } else if (mi_static.Count > 0)
                                        mi = mi_static;
                                else
                                        mi = null;
-                               
-                               if (mi != null && mi.Length > 0){
-                                       if (!CheckMethodAgainstBase (parent, (MethodInfo) mi [0])){
+
+                               if (mi != null && mi.Count > 0){
+                                       parent_method = (MethodInfo) mi [0];
+                                       string name = parent_method.DeclaringType.Name + "." +
+                                               parent_method.Name;
+
+                                       if (!CheckMethodAgainstBase (parent, flags, parent_method, name))
                                                return false;
+
+                                       if ((ModFlags & Modifiers.NEW) == 0) {
+                                               Type parent_ret = TypeManager.TypeToCoreType (
+                                                       parent_method.ReturnType);
+
+                                               if (parent_ret != MemberType) {
+                                                       Report.Error (
+                                                               508, parent.MakeName (Name) + ": cannot " +
+                                                               "change return type when overriding " +
+                                                               "inherited member " + name);
+                                                       return false;
+                                               }
                                        }
                                } else {
                                        if ((ModFlags & Modifiers.NEW) != 0)
                                                WarningNotHiding (parent);
-                                       
-                                       if ((ModFlags & Modifiers.OVERRIDE) != 0)
+
+                                       if ((ModFlags & Modifiers.OVERRIDE) != 0){
                                                Report.Error (115, Location,
                                                              parent.MakeName (Name) +
                                                              " no suitable methods found to override");
+                                       }
                                }
                        } else if ((ModFlags & Modifiers.NEW) != 0)
                                WarningNotHiding (parent);
 
-                       //
-                       // If we implement an interface, extract the interface name.
-                       //
-
-                       if (Name.IndexOf (".") != -1){
-                               int pos = Name.LastIndexOf (".");
-                               iface = Name.Substring (0, pos);
-
-                               iface_type = RootContext.LookupType (parent, iface, false, Location);
-                               short_name = Name.Substring (pos + 1);
-
-                               if (iface_type == null)
-                                       return false;
-
-                               // Compute the full name that we need to export
-                               Name = iface_type.FullName + "." + short_name;
-                               explicit_impl = true;
-                       } else
-                               short_name = Name;
-
-                       //
-                       // Check if we are an implementation of an interface method or
-                       // a method
-                       //
-                       implementing = parent.IsInterfaceMethod (
-                               iface_type, short_name, ret_type, parameters, false);
-                               
-                       //
-                       // For implicit implementations, make sure we are public, for
-                       // explicit implementations, make sure we are private.
-                       //
-                       if (implementing != null){
-                               //
-                               // Setting null inside this block will trigger a more
-                               // verbose error reporting for missing interface implementations
-                               //
-                               // The "candidate" function has been flagged already
-                               // but it wont get cleared
-                               //
-                               if (iface_type == null){
-                                       if ((ModFlags & Modifiers.PUBLIC) == 0)
-                                               implementing = null;
-
-                                       if ((ModFlags & Modifiers.STATIC) != 0)
-                                               implementing = null;
-                               } else {
-                                       if ((ModFlags & (Modifiers.PUBLIC | Modifiers.ABSTRACT)) != 0){
-                                               Report.Error (
-                                                       106, Location, "`public' or `abstract' modifiers "+
-                                                       "are not allowed in explicit interface declarations"
-                                                       );
-                                               implementing = null;
-                                       }
-                               }
-                       }
-                       
-                       //
-                       // If implementing is still valid, set flags
-                       //
-                       if (implementing != null){
-                               flags |=
-                                       MethodAttributes.Virtual |
-                                       MethodAttributes.HideBySig;
+                       return true;
+               }
 
-                               // If not abstract, then we can set Final.
-                               if (((flags & MethodAttributes.Abstract) == 0) &&
-                                   implementing.DeclaringType.IsInterface)
-                                       flags |= MethodAttributes.Final;
+               //
+               // Creates the type
+               //
+               public override bool Define (TypeContainer parent)
+               {
+                       if (!DoDefine (parent))
+                               return false;
 
-                               //
-                               // clear the flag
-                               //
-                               parent.IsInterfaceMethod (
-                                       iface_type, short_name, ret_type, parameters, true);
-                       } 
-
-                       Attribute dllimport_attr = null;
-                       if (OptAttributes != null && OptAttributes.AttributeSections != null) {
-                               foreach (AttributeSection asec in OptAttributes.AttributeSections) {
-                                       if (asec.Attributes == null)
-                                               continue;
-                                       
-                                       foreach (Attribute a in asec.Attributes)
-                                               if (a.Name.IndexOf ("DllImport") != -1) {
-                                                       flags |= MethodAttributes.PinvokeImpl;
-                                                       dllimport_attr = a;
-                                               }
-                               }
-                       }
+                       if (!CheckBase (parent))
+                               return false;
 
-                       //
-                       // Finally, define the method
-                       //
+                       CallingConventions cc = GetCallingConvention (parent is Class);
 
-                       if ((flags & MethodAttributes.PinvokeImpl) != 0) {
-                               EmitContext ec = new EmitContext (
-                                       parent, Location, null, GetReturnType (parent), ModFlags);
-                               
-                               MethodBuilder = dllimport_attr.DefinePInvokeMethod (
-                                       ec, parent.TypeBuilder,
-                                       Name, flags, ret_type, parameters);
-                       } else {
-                               
-                               MethodBuilder = parent.TypeBuilder.DefineMethod (
-                                       Name, flags,
-                                       GetCallingConvention (parent is Class),
-                                       ret_type, parameters);
-                               
-                               if (implementing != null && explicit_impl)
-                                       parent.TypeBuilder.DefineMethodOverride (
-                                               MethodBuilder, implementing);
-                       }
+                       MethodData = new MethodData (this, null, MemberType, ParameterTypes,
+                                                    ParameterInfo, cc, OptAttributes,
+                                                    ModFlags, flags, true);
 
-                       if (MethodBuilder == null)
+                       if (!MethodData.Define (parent))
                                return false;
 
-                       //
-                       // HACK because System.Reflection.Emit is lame
-                       //
-                       ParameterInfo = new InternalParameters (parent, Parameters);
-
-                       if (!TypeManager.RegisterMethod (MethodBuilder, ParameterInfo,
-                                                        parameters)) {
-                               Report.Error (
-                                       111, Location,
-                                       "Class `" + parent.Name + "' already contains a definition with " +
-                                       " the same return value and parameter types for method `" +
-                                       Name + "'");
-                               return false;
-                       }
+                       MethodBuilder = MethodData.MethodBuilder;
                        
                        //
                        // This is used to track the Entry Point,
                        //
-                       // FIXME: Allow pluggable entry point, check arguments, etc.
-                       //
                        if (Name == "Main" &&
                            ((ModFlags & Modifiers.STATIC) != 0) && 
                            (RootContext.MainClass == null ||
                             RootContext.MainClass == parent.TypeBuilder.FullName)){
-                               if (RootContext.EntryPoint != null){
-                                       DuplicatEntryPoint (MethodBuilder);
-                                       DuplicatEntryPoint (RootContext.EntryPoint);
-                               } else 
-                                       RootContext.EntryPoint = MethodBuilder;
-                               
-                               //
-                               // FIXME: Verify that the method signature
-                               // is valid for an entry point, and report
-                               // error 28 if not.
-                               //
+                                if (IsEntryPoint (MethodBuilder, ParameterInfo)) {
+                                        if (RootContext.EntryPoint == null) {
+                                                RootContext.EntryPoint = MethodBuilder;
+                                                RootContext.EntryPointLocation = Location;
+                                        } else {
+                                                DuplicateEntryPoint (RootContext.EntryPoint, RootContext.EntryPointLocation);
+                                                DuplicateEntryPoint (MethodBuilder, Location);
+                                        }
+                                } else                                         
+                                               Report28(MethodBuilder);
                        }
 
                        return true;
@@ -2426,85 +2139,22 @@ namespace Mono.CSharp {
                // 
                public void Emit (TypeContainer parent)
                {
-                       ILGenerator ig;
-                       EmitContext ec;
-
-                       if ((flags & MethodAttributes.PinvokeImpl) == 0)
-                               ig = MethodBuilder.GetILGenerator ();
-                       else
-                               ig = null;
-                       
-                       ec = new EmitContext (parent, Location, ig, GetReturnType (parent), ModFlags);
-
-                       if (OptAttributes != null)
-                               Attribute.ApplyAttributes (ec, MethodBuilder, this, OptAttributes, Location);
-                       
-
-                       LabelParameters (ec, ParameterTypes (parent), MethodBuilder);
-                       
-                       //
-                       // abstract or extern methods have no bodies
-                       //
-                       if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
-                               return;
-
-                       //
-                       // Handle destructors specially
-                       //
-                       // FIXME: This code generates buggy code
-                       //
-                       if (Name == "Finalize" && type_return_type == TypeManager.void_type)
-                               EmitDestructor (ec);
-                       else
-                               ec.EmitTopBlock (Block, Location);
-               }
-
-               void EmitDestructor (EmitContext ec)
-               {
-                       ILGenerator ig = ec.ig;
-                       
-                       Label finish = ig.DefineLabel ();
-                       bool old_in_try = ec.InTry;
-                       Expression member_lookup;
-                       
-                       ig.BeginExceptionBlock ();
-                       ec.InTry = true;
-                       ec.ReturnLabel = finish;
-                       ec.EmitTopBlock (Block, Location);
-                       ec.InTry = old_in_try;
-                       
-                       ig.MarkLabel (finish);
-                       bool old_in_finally = ec.InFinally;
-                       ec.InFinally = true;
-                       ig.BeginFinallyBlock ();
-                       
-                       member_lookup = Expression.MemberLookup (
-                               ec, ec.TypeContainer.TypeBuilder.BaseType, "Finalize",
-                               MemberTypes.Method, Expression.AllBindingFlags, Location);
-
-                       if (member_lookup != null){
-                               MethodGroupExpr parent_destructor = ((MethodGroupExpr) member_lookup);
-                               
-                               ig.Emit (OpCodes.Ldarg_0);
-                               ig.Emit (OpCodes.Call, (MethodInfo) parent_destructor.Methods [0]);
-                       }
-                       ec.InFinally = old_in_finally;
-                       
-                       ig.EndExceptionBlock ();
-                       //ig.MarkLabel (ec.ReturnLabel);
-                       ig.Emit (OpCodes.Ret);
+                       MethodData.Emit (parent, Block, this);
                }
        }
 
        public abstract class ConstructorInitializer {
                ArrayList argument_list;
                ConstructorInfo parent_constructor;
-               Location location;
+               Parameters parameters;
+               Location loc;
                
-               public ConstructorInitializer (ArrayList argument_list, Location location)
+               public ConstructorInitializer (ArrayList argument_list, Parameters parameters,
+                                              Location loc)
                {
                        this.argument_list = argument_list;
-                       this.location = location;
+                       this.parameters = parameters;
+                       this.loc = loc;
                }
 
                public ArrayList Arguments {
@@ -2517,37 +2167,49 @@ namespace Mono.CSharp {
                {
                        Expression parent_constructor_group;
                        Type t;
-                       
-                       if (argument_list != null){
-                               for (int i = argument_list.Count; i > 0; ){
-                                       --i;
 
-                                       Argument a = (Argument) argument_list [i];
-                                       if (!a.Resolve (ec, location))
+                       ec.CurrentBlock = new Block (null, true, parameters);
+
+                       if (argument_list != null){
+                               foreach (Argument a in argument_list){
+                                       if (!a.Resolve (ec, loc))
                                                return false;
                                }
                        }
 
-                       if (this is ConstructorBaseInitializer)
-                               t = ec.TypeContainer.TypeBuilder.BaseType;
-                       else
-                               t = ec.TypeContainer.TypeBuilder;
-                       
+                       ec.CurrentBlock = null;
+
+                       if (this is ConstructorBaseInitializer) {
+                               if (ec.ContainerType.BaseType == null)
+                                       return true;
+
+                               t = ec.ContainerType.BaseType;
+                               if (ec.ContainerType.IsValueType) {
+                                       Report.Error (522, loc,
+                                               "structs cannot call base class constructors");
+                                       return false;
+                               }
+                       } else
+                               t = ec.ContainerType;
+
                        parent_constructor_group = Expression.MemberLookup (
-                               ec, t, ".ctor", 
+                               ec, t, t, ".ctor", 
                                MemberTypes.Constructor,
-                               BindingFlags.Public | BindingFlags.Instance, location);
+                               BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
+                               loc);
                        
                        if (parent_constructor_group == null){
-                               Console.WriteLine ("Could not find a constructor in our parent");
+                               Report.Error (1501, loc,
+                                      "Can not find a constructor for this argument list");
                                return false;
                        }
                        
                        parent_constructor = (ConstructorInfo) Invocation.OverloadResolve (ec, 
-                               (MethodGroupExpr) parent_constructor_group, argument_list, location);
+                               (MethodGroupExpr) parent_constructor_group, argument_list, loc);
                        
                        if (parent_constructor == null){
-                               Console.WriteLine ("Could not locate a proper overload function");
+                               Report.Error (1501, loc,
+                                      "Can not find a constructor for this argument list");
                                return false;
                        }
                        
@@ -2556,21 +2218,25 @@ namespace Mono.CSharp {
 
                public void Emit (EmitContext ec)
                {
-                       ec.ig.Emit (OpCodes.Ldarg_0);
+                       if (parent_constructor != null)
+                               ec.ig.Emit (OpCodes.Ldarg_0);
                        if (argument_list != null)
                                Invocation.EmitArguments (ec, null, argument_list);
-                       ec.ig.Emit (OpCodes.Call, parent_constructor);
+                       if (parent_constructor != null)
+                               ec.ig.Emit (OpCodes.Call, parent_constructor);
                }
        }
 
        public class ConstructorBaseInitializer : ConstructorInitializer {
-               public ConstructorBaseInitializer (ArrayList argument_list, Location l) : base (argument_list, l)
+               public ConstructorBaseInitializer (ArrayList argument_list, Parameters pars, Location l) :
+                       base (argument_list, pars, l)
                {
                }
        }
 
        public class ConstructorThisInitializer : ConstructorInitializer {
-               public ConstructorThisInitializer (ArrayList argument_list, Location l) : base (argument_list, l)
+               public ConstructorThisInitializer (ArrayList argument_list, Parameters pars, Location l) :
+                       base (argument_list, pars, l)
                {
                }
        }
@@ -2578,7 +2244,7 @@ namespace Mono.CSharp {
        public class Constructor : MethodCore {
                public ConstructorBuilder ConstructorBuilder;
                public ConstructorInitializer Initializer;
-               public Attributes OptAttributes;
+               new public Attributes OptAttributes;
 
                // <summary>
                //   Modifiers allowed for a constructor.
@@ -2596,7 +2262,7 @@ namespace Mono.CSharp {
                // my very own code has static constructors.
                //
                public Constructor (string name, Parameters args, ConstructorInitializer init, Location l)
-                       : base (name, args, l)
+                       : base (null, 0, AllowedModifiers, name, null, args, l)
                {
                        Initializer = init;
                }
@@ -2625,12 +2291,14 @@ namespace Mono.CSharp {
                        MethodAttributes ca = (MethodAttributes.RTSpecialName |
                                               MethodAttributes.SpecialName);
 
-                       Type [] parameters = ParameterTypes (parent);
+                       // Check if arguments were correct.
+                       if (!DoDefineParameters (parent))
+                               return false;
 
                        if ((ModFlags & Modifiers.STATIC) != 0)
                                ca |= MethodAttributes.Static;
                        else {
-                               if (parent is Struct && parameters.Length == 0){
+                               if (parent is Struct && ParameterTypes.Length == 0){
                                        Report.Error (
                                                568, Location, 
                                                "Structs can not contain explicit parameterless " +
@@ -2640,19 +2308,13 @@ namespace Mono.CSharp {
                                ca |= MethodAttributes.Public | MethodAttributes.HideBySig;
                        }
 
-                       foreach (Type partype in parameters)
-                               if (!TypeContainer.AsAccessible (partype, ModFlags))
-                                       return false;
-
                        ConstructorBuilder = parent.TypeBuilder.DefineConstructor (
-                               ca, GetCallingConvention (parent is Class), parameters);
+                               ca, GetCallingConvention (parent is Class), ParameterTypes);
 
                        //
                        // HACK because System.Reflection.Emit is lame
                        //
-                       ParameterInfo = new InternalParameters (parent, Parameters);
-
-                       if (!TypeManager.RegisterMethod (ConstructorBuilder, ParameterInfo, parameters)) {
+                       if (!TypeManager.RegisterMethod (ConstructorBuilder, ParameterInfo, ParameterTypes)) {
                                Report.Error (
                                        111, Location,
                                        "Class `" +parent.Name+ "' already contains a definition with the " +
@@ -2672,54 +2334,773 @@ namespace Mono.CSharp {
                        ILGenerator ig = ConstructorBuilder.GetILGenerator ();
                        EmitContext ec = new EmitContext (parent, Location, ig, null, ModFlags, true);
 
-                       if (parent is Class && ((ModFlags & Modifiers.STATIC) == 0)){
-                               if (Initializer == null)
-                                       Initializer = new ConstructorBaseInitializer (null, parent.Location);
-                               
-                               if (!Initializer.Resolve (ec)){
-                                       Console.WriteLine ("Could not resolve initializer: " + parent.Name);
-                               }
+                       if ((ModFlags & Modifiers.STATIC) == 0){
+                               if (parent is Class && Initializer == null)
+                                       Initializer = new ConstructorBaseInitializer (
+                                               null, Parameters.EmptyReadOnlyParameters, parent.Location);
+
+
+                               //
+                               // Spec mandates that Initializers will not have
+                               // `this' access
+                               //
+                               ec.IsStatic = true;
+                               if (Initializer != null && !Initializer.Resolve (ec))
+                                       return;
+                               ec.IsStatic = false;
                        }
 
-                       LabelParameters (ec, ParameterTypes (parent), ConstructorBuilder);
+                       LabelParameters (ec, ParameterTypes, ConstructorBuilder);
                        
                        //
                        // Classes can have base initializers and instance field initializers.
                        //
                        if (parent is Class){
-                               if ((ModFlags & Modifiers.STATIC) == 0){
-                                       Initializer.Emit (ec);
-                                       parent.EmitFieldInitializers (ec, false);
-                               }
+                               if ((ModFlags & Modifiers.STATIC) == 0)
+                                       parent.EmitFieldInitializers (ec);
                        }
+                       if (Initializer != null)
+                               Initializer.Emit (ec);
                        
                        if ((ModFlags & Modifiers.STATIC) != 0)
-                               parent.EmitFieldInitializers (ec, true);
+                               parent.EmitFieldInitializers (ec);
 
                        Attribute.ApplyAttributes (ec, ConstructorBuilder, this, OptAttributes, Location);
 
-                       ec.EmitTopBlock (Block, Location);
+                       // If this is a non-static `struct' constructor and doesn't have any
+                       // initializer, it must initialize all of the struct's fields.
+                       if ((parent is Struct) && ((ModFlags & Modifiers.STATIC) == 0) &&
+                           (Initializer == null))
+                               Block.AddThisVariable (parent, Location);
+
+                       ec.EmitTopBlock (Block, ParameterInfo, Location);
                }
        }
-       
-       public class Field : MemberCore {
-               public readonly string Type;
-               public readonly Object Initializer;
+
+       public class MethodData {
+               //
+               // The return type of this method
+               //
+               public readonly Type ReturnType;
+               public readonly Type[] ParameterTypes;
+               public readonly InternalParameters ParameterInfo;
+               public readonly CallingConventions CallingConventions;
                public readonly Attributes OptAttributes;
-               public FieldBuilder  FieldBuilder;
-               public Status status;
+               public readonly Location Location;
 
-               [Flags]
-               public enum Status : byte { ASSIGNED = 1, USED = 2 }
+               //
+               // Are we implementing an interface ?
+               //
+               public bool IsImplementing = false;
 
-               
-               // <summary>
-               //   Modifiers allowed in a class declaration
-               // </summary>
-               const int AllowedModifiers =
-                       Modifiers.NEW |
-                       Modifiers.PUBLIC |
-                       Modifiers.PROTECTED |
+               //
+               // Protected data.
+               //
+               protected MemberBase member;
+               protected int modifiers;
+               protected MethodAttributes flags;
+               protected bool is_method;
+               protected string accessor_name;
+               ArrayList conditionals;
+
+               MethodBuilder builder = null;
+               public MethodBuilder MethodBuilder {
+                       get {
+                               return builder;
+                       }
+               }
+
+               public MethodData (MemberBase member, string name, Type return_type,
+                                  Type [] parameter_types, InternalParameters parameters,
+                                  CallingConventions cc, Attributes opt_attrs,
+                                  int modifiers, MethodAttributes flags, bool is_method)
+               {
+                       this.member = member;
+                       this.accessor_name = name;
+                       this.ReturnType = return_type;
+                       this.ParameterTypes = parameter_types;
+                       this.ParameterInfo = parameters;
+                       this.CallingConventions = cc;
+                       this.OptAttributes = opt_attrs;
+                       this.modifiers = modifiers;
+                       this.flags = flags;
+                       this.is_method = is_method;
+                       this.Location = member.Location;
+                       this.conditionals = new ArrayList ();
+               }
+
+               //
+               // Attributes.
+               //
+               Attribute dllimport_attribute = null;
+               string obsolete = null;
+               bool obsolete_error = false;
+
+               public virtual bool ApplyAttributes (Attributes opt_attrs, bool is_method)
+               {
+                       if ((opt_attrs == null) || (opt_attrs.AttributeSections == null))
+                               return true;
+
+                       foreach (AttributeSection asec in opt_attrs.AttributeSections) {
+                               if (asec.Attributes == null)
+                                       continue;
+                                       
+                               foreach (Attribute a in asec.Attributes) {
+                                       if (a.Name == "Conditional") {
+                                               if (!ApplyConditionalAttribute (a))
+                                                       return false;
+                                       } else if (a.Name == "Obsolete") {
+                                               if (!ApplyObsoleteAttribute (a))
+                                                       return false;
+                                       } else if (a.Name.IndexOf ("DllImport") != -1) {
+                                               if (!is_method) {
+                                                       a.Type = TypeManager.dllimport_type;
+                                                       Attribute.Error_AttributeNotValidForElement (a, Location);
+                                                       return false;
+                                               }
+                                               if (!ApplyDllImportAttribute (a))
+                                                       return false;
+                                       }
+                               }
+                       }
+
+                       return true;
+               }
+
+               //
+               // Applies the `DllImport' attribute to the method.
+               //
+               protected virtual bool ApplyDllImportAttribute (Attribute a)
+               {
+                       const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
+                       if ((modifiers & extern_static) != extern_static) {
+                               Report.Error (601, Location,
+                                             "The DllImport attribute must be specified on a method " +
+                                             "marked `static' and `extern'.");
+                               return false;
+                       }
+
+                       flags |= MethodAttributes.PinvokeImpl;
+                       dllimport_attribute = a;
+                       return true;
+               }
+
+               //
+               // Applies the `Obsolete' attribute to the method.
+               //
+               protected virtual bool ApplyObsoleteAttribute (Attribute a)
+               {
+                       if (obsolete != null) {
+                               Report.Error (579, Location, "Duplicate `Obsolete' attribute");
+                               return false;
+                       }
+
+                       obsolete = a.Obsolete_GetObsoleteMessage (out obsolete_error);
+                       return obsolete != null;
+               }
+
+               //
+               // Applies the `Conditional' attribute to the method.
+               //
+               protected virtual bool ApplyConditionalAttribute (Attribute a)
+               {
+                       // The Conditional attribute is only valid on methods.
+                       if (!is_method) {
+                               Attribute.Error_AttributeNotValidForElement (a, Location);
+                               return false;
+                       }
+
+                       string condition = a.Conditional_GetConditionName ();
+
+                       if (condition == null)
+                               return false;
+
+                       if (ReturnType != TypeManager.void_type) {
+                               Report.Error (578, Location,
+                                             "Conditional not valid on `" + member.Name + "' " +
+                                             "because its return type is not void");
+                               return false;
+                       }
+
+                       if ((modifiers & Modifiers.OVERRIDE) != 0) {
+                               Report.Error (243, Location,
+                                             "Conditional not valid on `" + member.Name + "' " +
+                                             "because it is an override method");
+                               return false;
+                       }
+
+                       if (member.IsExplicitImpl) {
+                               Report.Error (577, Location,
+                                             "Conditional not valid on `" + member.Name + "' " +
+                                             "because it is an explicit interface implementation");
+                               return false;
+                       }
+
+                       if (IsImplementing) {
+                               Report.Error (623, Location,
+                                             "Conditional not valid on `" + member.Name + "' " +
+                                             "because it is an interface method");
+                               return false;
+                       }
+
+                       conditionals.Add (condition);
+
+                       return true;
+               }
+
+               //
+               // Checks whether this method should be ignored due to its Conditional attributes.
+               //
+               bool ShouldIgnore (Location loc)
+               {
+                       // When we're overriding a virtual method, we implicitly inherit the
+                       // Conditional attributes from our parent.
+                       if (member.ParentMethod != null) {
+                               TypeManager.MethodFlags flags = TypeManager.GetMethodFlags (
+                                       member.ParentMethod, loc);
+
+                               if ((flags & TypeManager.MethodFlags.ShouldIgnore) != 0)
+                                       return true;
+                       }
+
+                       foreach (string condition in conditionals)
+                               if (RootContext.AllDefines [condition] == null)
+                                       return true;
+
+                       return false;
+               }
+
+               //
+               // Returns the TypeManager.MethodFlags for this method.
+               // This emits an error 619 / warning 618 if the method is obsolete.
+               // In the former case, TypeManager.MethodFlags.IsObsoleteError is returned.
+               //
+               public virtual TypeManager.MethodFlags GetMethodFlags (Location loc)
+               {
+                       TypeManager.MethodFlags flags = 0;
+
+                       if (obsolete != null) {
+                               if (obsolete_error) {
+                                       Report.Error (619, loc, "Method `" + member.Name +
+                                                     "' is obsolete: `" + obsolete + "'");
+                                       return TypeManager.MethodFlags.IsObsoleteError;
+                               } else
+                                       Report.Warning (618, loc, "Method `" + member.Name +
+                                                       "' is obsolete: `" + obsolete + "'");
+
+                               flags |= TypeManager.MethodFlags.IsObsolete;
+                       }
+
+                       if (ShouldIgnore (loc))
+                           flags |= TypeManager.MethodFlags.ShouldIgnore;
+
+                       return flags;
+               }
+
+               public virtual bool Define (TypeContainer parent)
+               {
+                       MethodInfo implementing = null;
+                       string method_name, name, prefix;
+
+                       if (OptAttributes != null)
+                               if (!ApplyAttributes (OptAttributes, is_method))
+                                       return false;
+
+                       if (member.IsExplicitImpl)
+                               prefix = member.InterfaceType.FullName + ".";
+                       else
+                               prefix = "";
+
+                       if (accessor_name != null)
+                               name = accessor_name + "_" + member.ShortName;
+                       else
+                               name = member.ShortName;
+                       method_name = prefix + name;
+                               
+                       if (parent.Pending != null){
+                               if (member is Indexer)
+                                       implementing = parent.Pending.IsInterfaceIndexer (
+                                               member.InterfaceType, ReturnType, ParameterTypes);
+                               else
+                                       implementing = parent.Pending.IsInterfaceMethod (
+                                               member.InterfaceType, name, ReturnType, ParameterTypes);
+
+                               if (member.InterfaceType != null && implementing == null){
+                                       TypeContainer.Error_ExplicitInterfaceNotMemberInterface (
+                                               Location, name);
+                                       return false;
+                               }
+                       }
+
+                       //
+                       // For implicit implementations, make sure we are public, for
+                       // explicit implementations, make sure we are private.
+                       //
+                       if (implementing != null){
+                               //
+                               // Setting null inside this block will trigger a more
+                               // verbose error reporting for missing interface implementations
+                               //
+                               // The "candidate" function has been flagged already
+                               // but it wont get cleared
+                               //
+                               if (!member.IsExplicitImpl){
+                                       //
+                                       // We already catch different accessibility settings
+                                       // so we just need to check that we are not private
+                                       //
+                                       if ((modifiers & Modifiers.PRIVATE) != 0)
+                                               implementing = null;
+                                       
+                                       //
+                                       // Static is not allowed
+                                       //
+                                       if ((modifiers & Modifiers.STATIC) != 0)
+                                               implementing = null;
+                               } else {
+                                       if ((modifiers & (Modifiers.PUBLIC | Modifiers.ABSTRACT | Modifiers.VIRTUAL)) != 0){
+                                               Modifiers.Error_InvalidModifier (Location, "public, virtual or abstract");
+                                               implementing = null;
+                                       }
+                               }
+                       }
+                       
+                       //
+                       // If implementing is still valid, set flags
+                       //
+                       if (implementing != null){
+                               //
+                               // When implementing interface methods, set NewSlot.
+                               //
+                               if (implementing.DeclaringType.IsInterface)
+                                       flags |= MethodAttributes.NewSlot;
+                               
+                               flags |=
+                                       MethodAttributes.Virtual |
+                                       MethodAttributes.HideBySig;
+
+                               // Get the method name from the explicit interface.
+                               if (member.InterfaceType != null) {
+                                       name = implementing.Name;
+                                       method_name = prefix + name;
+                               }
+
+                               IsImplementing = true;
+                       }
+
+                       //
+                       // Create the MethodBuilder for the method
+                       //
+                       if ((flags & MethodAttributes.PinvokeImpl) != 0) {
+                               if ((modifiers & Modifiers.STATIC) == 0) {
+                                       Report.Error (601, Location,
+                                                     "The DllImport attribute must be specified on " +
+                                                     "a method marked 'static' and 'extern'.");
+                                       return false;
+                               }
+                               
+                               EmitContext ec = new EmitContext (
+                                       parent, Location, null, ReturnType, modifiers);
+                               
+                               builder = dllimport_attribute.DefinePInvokeMethod (
+                                       ec, parent.TypeBuilder, method_name, flags,
+                                       ReturnType, ParameterTypes);
+                       } else
+                               builder = parent.TypeBuilder.DefineMethod (
+                                       method_name, flags, CallingConventions,
+                                       ReturnType, ParameterTypes);
+
+                       if (builder == null)
+                               return false;
+
+                       if (IsImplementing) {
+                               //
+                               // clear the pending implemntation flag
+                               //
+                               if (member is Indexer) {
+                                       parent.Pending.ImplementIndexer (
+                                               member.InterfaceType, builder, ReturnType,
+                                               ParameterTypes, true);
+                               } else
+                                       parent.Pending.ImplementMethod (
+                                               member.InterfaceType, name, ReturnType,
+                                               ParameterTypes, member.IsExplicitImpl);
+
+                               if (member.IsExplicitImpl)
+                                       parent.TypeBuilder.DefineMethodOverride (
+                                               builder, implementing);
+                       }
+
+                       if (!TypeManager.RegisterMethod (builder, ParameterInfo, ParameterTypes)) {
+                               Report.Error (111, Location,
+                                             "Class `" + parent.Name +
+                                             "' already contains a definition with the " +
+                                             "same return value and parameter types as the " +
+                                             "'get' method of property `" + member.Name + "'");
+                               return false;
+                       }
+
+                       TypeManager.AddMethod (builder, this);
+
+                       return true;
+               }
+
+               //
+               // Emits the code
+               // 
+               public virtual void Emit (TypeContainer parent, Block block, object kind)
+               {
+                       ILGenerator ig;
+                       EmitContext ec;
+
+                       if ((flags & MethodAttributes.PinvokeImpl) == 0)
+                               ig = builder.GetILGenerator ();
+                       else
+                               ig = null;
+
+                       ec = new EmitContext (parent, Location, ig, ReturnType, modifiers);
+
+                       if (OptAttributes != null)
+                               Attribute.ApplyAttributes (ec, builder, kind, OptAttributes, Location);
+
+                       if (member is MethodCore)
+                               ((MethodCore) member).LabelParameters (ec, ParameterTypes, MethodBuilder);
+
+                       //
+                       // abstract or extern methods have no bodies
+                       //
+                       if ((modifiers & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0){
+                               if (block == null)
+                                       return;
+
+                               //
+                               // abstract or extern methods have no bodies.
+                               //
+                               if ((modifiers & Modifiers.ABSTRACT) != 0)
+                                       Report.Error (
+                                               500, Location, "Abstract method `" +
+                                               TypeManager.CSharpSignature (builder) +
+                                               "' can not have a body");
+
+                               if ((modifiers & Modifiers.EXTERN) != 0)
+                                       Report.Error (
+                                               179, Location, "External method `" +
+                                               TypeManager.CSharpSignature (builder) +
+                                               "' can not have a body");
+
+                               return;
+                       }
+
+                       //
+                       // Methods must have a body unless they're extern or abstract
+                       //
+                       if (block == null) {
+                               Report.Error (
+                                       501, Location, "Method `" +
+                                       TypeManager.CSharpSignature (builder) +
+                                       "' must declare a body since it is not marked " +
+                                       "abstract or extern");
+                               return;
+                       }
+
+                       //
+                       // Handle destructors specially
+                       //
+                       // FIXME: This code generates buggy code
+                       //
+                       if (member.Name == "Finalize" && ReturnType == TypeManager.void_type)
+                               EmitDestructor (ec, block);
+                       else {
+                               ISymbolWriter sw = CodeGen.SymbolWriter;
+
+                               if ((sw != null) && !Location.IsNull (Location) &&
+                                   !Location.IsNull (block.EndLocation)) {
+                                       Location end = block.EndLocation;
+                                       MethodToken token = MethodBuilder.GetToken ();
+                                       sw.OpenMethod (new SymbolToken (token.Token));
+                                       sw.SetMethodSourceRange (Location.SymbolDocument,
+                                                                Location.Row, 0,
+                                                                end.SymbolDocument,
+                                                                end.Row, 0);
+
+                                       ec.EmitTopBlock (block, ParameterInfo, Location);
+
+                                       sw.CloseMethod ();
+                               } else
+                                       ec.EmitTopBlock (block, ParameterInfo, Location);
+                       }
+               }
+
+               void EmitDestructor (EmitContext ec, Block block)
+               {
+                       ILGenerator ig = ec.ig;
+                       
+                       Label finish = ig.DefineLabel ();
+                       bool old_in_try = ec.InTry;
+                       
+                       ig.BeginExceptionBlock ();
+                       ec.InTry = true;
+                       ec.ReturnLabel = finish;
+                       ec.HasReturnLabel = true;
+                       ec.EmitTopBlock (block, null, Location);
+                       ec.InTry = old_in_try;
+                       
+                       // ig.MarkLabel (finish);
+                       bool old_in_finally = ec.InFinally;
+                       ec.InFinally = true;
+                       ig.BeginFinallyBlock ();
+                       
+                       if (ec.ContainerType.BaseType != null) {
+                               Expression member_lookup = Expression.MemberLookup (
+                                       ec, ec.ContainerType.BaseType, "Finalize",
+                                       MemberTypes.Method, Expression.AllBindingFlags, Location);
+
+                               if (member_lookup != null){
+                                       MethodGroupExpr parent_destructor = ((MethodGroupExpr) member_lookup);
+                               
+                                       ig.Emit (OpCodes.Ldarg_0);
+                                       ig.Emit (OpCodes.Call, (MethodInfo) parent_destructor.Methods [0]);
+                               }
+                       }
+                       ec.InFinally = old_in_finally;
+                       
+                       ig.EndExceptionBlock ();
+                       //ig.MarkLabel (ec.ReturnLabel);
+                       ig.Emit (OpCodes.Ret);
+               }
+       }
+
+       abstract public class MemberBase : MemberCore {
+               public Expression Type;
+               public readonly Attributes OptAttributes;
+
+               protected MethodAttributes flags;
+
+               //
+               // The "short" name of this property / indexer / event.  This is the
+               // name without the explicit interface.
+               //
+               public string ShortName;
+
+               //
+               // The type of this property / indexer / event
+               //
+               public Type MemberType;
+
+               //
+               // If true, this is an explicit interface implementation
+               //
+               public bool IsExplicitImpl = false;
+
+               //
+               // The name of the interface we are explicitly implementing
+               //
+               public string ExplicitInterfaceName = null;
+
+               //
+               // If true, the interface type we are explicitly implementing
+               //
+               public Type InterfaceType = null;
+
+               //
+               // The method we're overriding if this is an override method.
+               //
+               protected MethodInfo parent_method = null;
+               public MethodInfo ParentMethod {
+                       get {
+                               return parent_method;
+                       }
+               }
+
+               //
+               // The constructor is only exposed to our children
+               //
+               protected MemberBase (Expression type, int mod, int allowed_mod, string name,
+                                     Attributes attrs, Location loc)
+                       : base (name, loc)
+               {
+                       Type = type;
+                       ModFlags = Modifiers.Check (allowed_mod, mod, Modifiers.PRIVATE, loc);
+                       OptAttributes = attrs;
+               }
+
+               protected virtual bool CheckBase (TypeContainer parent)
+               {
+                       return true;
+               }
+
+               protected virtual bool CheckParameters (TypeContainer parent, Type [] parameters)
+               {
+                       bool error = false;
+
+                       foreach (Type partype in parameters){
+                               if (partype.IsPointer && !UnsafeOK (parent))
+                                       error = true;
+
+                               if (parent.AsAccessible (partype, ModFlags))
+                                       continue;
+
+                               if (this is Indexer)
+                                       Report.Error (55, Location,
+                                                     "Inconsistent accessibility: parameter type `" +
+                                                     TypeManager.CSharpName (partype) + "' is less " +
+                                                     "accessible than indexer `" + Name + "'");
+                               else
+                                       Report.Error (51, Location,
+                                                     "Inconsistent accessibility: parameter type `" +
+                                                     TypeManager.CSharpName (partype) + "' is less " +
+                                                     "accessible than method `" + Name + "'");
+                               error = true;
+                       }
+
+                       return !error;
+               }
+
+               protected virtual bool DoDefine (TypeContainer parent)
+               {
+                       if (Name == null)
+                               Name = "this";
+
+                       if (!parent.MethodModifiersValid (ModFlags, Name, Location))
+                               return false;
+
+                       flags = Modifiers.MethodAttr (ModFlags);
+
+                       // Lookup Type, verify validity
+                       MemberType = parent.ResolveType (Type, false, Location);
+                       if (MemberType == null)
+                               return false;
+
+                       // verify accessibility
+                       if (!parent.AsAccessible (MemberType, ModFlags)) {
+                               if (this is Property)
+                                       Report.Error (53, Location,
+                                                     "Inconsistent accessibility: property type `" +
+                                                     TypeManager.CSharpName (MemberType) + "' is less " +
+                                                     "accessible than property `" + Name + "'");
+                               else if (this is Indexer)
+                                       Report.Error (54, Location,
+                                                     "Inconsistent accessibility: indexer return type `" +
+                                                     TypeManager.CSharpName (MemberType) + "' is less " +
+                                                     "accessible than indexer `" + Name + "'");
+                               else if (this is Method)
+                                       Report.Error (50, Location,
+                                                     "Inconsistent accessibility: return type `" +
+                                                     TypeManager.CSharpName (MemberType) + "' is less " +
+                                                     "accessible than method `" + Name + "'");
+                               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 ((ExplicitInterfaceName == null) && (Name.IndexOf (".") != -1)){
+                               int pos = Name.LastIndexOf (".");
+
+                               ExplicitInterfaceName = Name.Substring (0, pos);
+                               ShortName = Name.Substring (pos + 1);
+                       } else
+                               ShortName = Name;
+
+                       if (ExplicitInterfaceName != null) {
+                               InterfaceType  = RootContext.LookupType (
+                                       parent, ExplicitInterfaceName, false, Location);
+                               if (InterfaceType == null)
+                                       return false;
+
+                               // Compute the full name that we need to export.
+                               Name = InterfaceType.FullName + "." + ShortName;
+                               
+                               if (!parent.VerifyImplements (InterfaceType, ShortName, Name, Location))
+                                       return false;
+                               
+                               IsExplicitImpl = true;
+                       } else
+                               IsExplicitImpl = false;
+
+                       return true;
+               }
+       }
+
+       //
+       // Fields and Events both generate FieldBuilders, we use this to share 
+       // their common bits.  This is also used to flag usage of the field
+       //
+       abstract public class FieldBase : MemberBase {
+               public FieldBuilder  FieldBuilder;
+               public Status status;
+
+               [Flags]
+               public enum Status : byte { ASSIGNED = 1, USED = 2 }
+
+               //
+               // The constructor is only exposed to our children
+               //
+               protected FieldBase (Expression type, int mod, int allowed_mod, string name,
+                                    object init, Attributes attrs, Location loc)
+                       : base (type, mod, allowed_mod, name, attrs, loc)
+               {
+                       this.init = init;
+               }
+
+               //
+               // Whether this field has an initializer.
+               //
+               public bool HasInitializer {
+                       get {
+                               return init != null;
+                       }
+               }
+
+               // Private.
+               readonly Object init;
+               Expression init_expr;
+               bool init_expr_initialized = false;
+
+               //
+               // Resolves and returns the field initializer.
+               //
+               public Expression GetInitializerExpression (EmitContext ec)
+               {
+                       if (init_expr_initialized)
+                               return init_expr;
+
+                       Expression e;
+                       if (init is Expression)
+                               e = (Expression) init;
+                       else
+                               e = new ArrayCreation (Type, "", (ArrayList)init, Location);
+
+                       ec.IsFieldInitializer = true;
+                       e = e.DoResolve (ec);
+                       ec.IsFieldInitializer = false;
+
+                       init_expr = e;
+                       init_expr_initialized = true;
+
+                       return init_expr;
+               }
+       }
+
+       //
+       // The Field class is used to represents class/struct fields during parsing.
+       //
+       public class Field : FieldBase {
+               // <summary>
+               //   Modifiers allowed in a class declaration
+               // </summary>
+               const int AllowedModifiers =
+                       Modifiers.NEW |
+                       Modifiers.PUBLIC |
+                       Modifiers.PROTECTED |
                        Modifiers.INTERNAL |
                        Modifiers.PRIVATE |
                        Modifiers.STATIC |
@@ -2727,25 +3108,26 @@ namespace Mono.CSharp {
                        Modifiers.UNSAFE |
                        Modifiers.READONLY;
 
-               public Field (string type, int mod, string name, Object expr_or_array_init,
+               public Field (Expression type, int mod, string name, Object expr_or_array_init,
                              Attributes attrs, Location loc)
-                       : base (name, loc)
+                       : base (type, mod, AllowedModifiers, name, expr_or_array_init, attrs, loc)
                {
-                       Type = type;
-                       ModFlags = Modifiers.Check (AllowedModifiers, mod, Modifiers.PRIVATE, loc);
-                       Initializer = expr_or_array_init;
-                       OptAttributes = attrs;
                }
 
                public override bool Define (TypeContainer parent)
                {
-                       Type t = RootContext.LookupType (parent, Type, false, Location);
+                       Type t = parent.ResolveType (Type, false, Location);
                        
                        if (t == null)
                                return false;
 
-                       if (!TypeContainer.AsAccessible (t, ModFlags))
+                       if (!parent.AsAccessible (t, ModFlags)) {
+                               Report.Error (52, Location,
+                                             "Inconsistent accessibility: field type `" +
+                                             TypeManager.CSharpName (t) + "' is less " +
+                                             "accessible than field `" + Name + "'");
                                return false;
+                       }
 
                        if (t.IsPointer && !UnsafeOK (parent))
                                return false;
@@ -2755,9 +3137,7 @@ namespace Mono.CSharp {
 
                                // ptype is only null for System.Object while compiling corlib.
                                if (ptype != null){
-                                       MemberInfo [] mi;
-                                       
-                                       mi = TypeContainer.FindMembers (
+                                       TypeContainer.FindMembers (
                                                ptype, MemberTypes.Method,
                                                BindingFlags.Public |
                                                BindingFlags.Static | BindingFlags.Instance,
@@ -2791,7 +3171,7 @@ namespace Mono.CSharp {
                        FieldBuilder = parent.TypeBuilder.DefineField (
                                Name, t, Modifiers.FieldAttr (ModFlags));
 
-                       TypeManager.RegisterField (FieldBuilder, this);
+                       TypeManager.RegisterFieldBase (FieldBuilder, this);
                        return true;
                }
 
@@ -2804,83 +3184,108 @@ namespace Mono.CSharp {
                }
        }
 
-       public class Property : MemberCore {
-               public readonly string Type;
-               public Block           Get, Set;
-               public PropertyBuilder PropertyBuilder;
-               public Attributes OptAttributes;
-               public MethodBuilder GetBuilder, SetBuilder;
-
+       //
+       // `set' and `get' accessors are represented with an Accessor.
+       // 
+       public class Accessor {
+               //
+               // Null if the accessor is empty, or a Block if not
                //
-               // The type, once we compute it.
+               public Block Block;
+               public Attributes OptAttributes;
                
-               Type PropertyType;
+               public Accessor (Block b, Attributes attrs)
+               {
+                       Block = b;
+                       OptAttributes = attrs;
+               }
+       }
 
-               const int AllowedModifiers =
-                       Modifiers.NEW |
-                       Modifiers.PUBLIC |
-                       Modifiers.PROTECTED |
-                       Modifiers.INTERNAL |
-                       Modifiers.PRIVATE |
-                       Modifiers.STATIC |
-                       Modifiers.SEALED |
-                       Modifiers.OVERRIDE |
-                       Modifiers.ABSTRACT |
-                       Modifiers.UNSAFE |
-                       Modifiers.VIRTUAL;
+       //
+       // Properties and Indexers both generate PropertyBuilders, we use this to share 
+       // their common bits.
+       //
+       abstract public class PropertyBase : MethodCore {
+               public Accessor Get, Set;
+               public PropertyBuilder PropertyBuilder;
+               public MethodBuilder GetBuilder, SetBuilder;
+               public MethodData GetData, SetData;
 
-               public Property (string type, string name, int mod_flags, Block get_block, Block set_block,
-                                Attributes attrs, Location loc)
-                       : base (name, loc)
+               protected EmitContext ec;
+
+               public PropertyBase (Expression type, string name, int mod_flags, int allowed_mod,
+                                    Parameters parameters, Accessor get_block, Accessor set_block,
+                                    Attributes attrs, Location loc)
+                       : base (type, mod_flags, allowed_mod, name, attrs, parameters, loc)
                {
-                       Type = type;
-                       ModFlags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PRIVATE, loc);
                        Get = get_block;
                        Set = set_block;
-                       OptAttributes = attrs;
+               }
+
+               protected override bool DoDefine (TypeContainer parent)
+               {
+                       if (!base.DoDefine (parent))
+                               return false;
+
+                       ec = new EmitContext (parent, Location, null, MemberType, ModFlags);
+
+                       return true;
                }
 
                //
                // Checks our base implementation if any
                //
-               bool CheckBase (TypeContainer parent)
+               protected override bool CheckBase (TypeContainer parent)
                {
+                       // Check whether arguments were correct.
+                       if (!DoDefineParameters (parent))
+                               return false;
+
+                       MethodSignature ms = new MethodSignature (Name, null, ParameterTypes);
+                       MemberList props_this;
+
+                       props_this = TypeContainer.FindMembers (
+                               parent.TypeBuilder, MemberTypes.Property,
+                               BindingFlags.NonPublic | BindingFlags.Public |
+                               BindingFlags.Static | BindingFlags.Instance |
+                               BindingFlags.DeclaredOnly,
+                               MethodSignature.method_signature_filter, ms);
+
+                       if (props_this.Count > 0) {
+                               Report.Error (111, Location, "Class `" + parent.Name + "' " +
+                                             "already defines a member called `" + Name + "' " +
+                                             "with the same parameter types");
+                               return false;
+                       }
+
                        //
                        // Find properties with the same name on the base class
                        //
-                       MemberInfo [] props;
-                       MemberInfo [] props_static = TypeContainer.FindMembers (
-                               parent.TypeBuilder.BaseType,
-                               MemberTypes.All, BindingFlags.Public | BindingFlags.Static,
-                               System.Type.FilterName, Name);
-
-                       MemberInfo [] props_instance = TypeContainer.FindMembers (
-                               parent.TypeBuilder.BaseType,
-                               MemberTypes.All, BindingFlags.Public | BindingFlags.Instance,
-                               System.Type.FilterName, Name);
+                       MemberList props;
+                       MemberList props_static = TypeContainer.FindMembers (
+                               parent.TypeBuilder.BaseType, MemberTypes.Property,
+                               BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static,
+                               MethodSignature.inheritable_property_signature_filter, ms);
+
+                       MemberList props_instance = TypeContainer.FindMembers (
+                               parent.TypeBuilder.BaseType, MemberTypes.Property,
+                               BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
+                               MethodSignature.inheritable_property_signature_filter,
+                               ms);
 
                        //
                        // Find if we have anything
                        //
-                       if (props_static != null && props_static.Length > 0)
+                       if (props_static.Count > 0)
                                props = props_static;
-                       else if (props_instance != null && props_instance.Length > 0)
+                       else if (props_instance.Count > 0)
                                props = props_instance;
                        else
                                props = null;
 
                        //
                        // If we have something on the base.
-                       if (props != null && props.Length > 0){
-                               //
-                               // FIXME:
-                               // Currently we expect only to get 1 match at most from our
-                               // base class, maybe we can get more than one, investigate
-                               // whether this is possible
-                               //
-                               if (props.Length > 1)
-                                       throw new Exception ("How do we handle this?");
-                               
+                       if (props != null && props.Count > 0){
                                PropertyInfo pi = (PropertyInfo) props [0];
 
                                MethodInfo inherited_get = TypeManager.GetPropertyGetter (pi);
@@ -2889,266 +3294,161 @@ namespace Mono.CSharp {
                                MethodInfo reference = inherited_get == null ?
                                        inherited_set : inherited_get;
                                
-                               if (!CheckMethodAgainstBase (parent, reference))
+                               if (reference != null) {
+                                       string name = reference.DeclaringType.Name + "." + Name;
+
+                                       if (!CheckMethodAgainstBase (parent, flags, reference, name))
+                                               return false;
+                               }
+
+                               if (((ModFlags & Modifiers.NEW) == 0) && (pi.PropertyType != MemberType)) {
+                                       Report.Error (508, parent.MakeName (Name) + ": cannot " +
+                                                     "change return type when overriding inherited " +
+                                                     "member `" + pi.DeclaringType + "." + pi.Name + "'");
                                        return false;
+                               }
                        } else {
                                if ((ModFlags & Modifiers.NEW) != 0)
                                        WarningNotHiding (parent);
                                
                                if ((ModFlags & Modifiers.OVERRIDE) != 0){
-                                       Report.Error (115, Location,
-                                                     parent.MakeName (Name) +
-                                                     " no suitable methods found to override");
+                                       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;
                                }
                        }
                        return true;
                }
 
-               bool DefineMethod (TypeContainer parent, Type iface_type, string short_name, bool is_get)
+               public void Emit (TypeContainer tc)
                {
-                       MethodAttributes flags = Modifiers.MethodAttr (ModFlags);
-                       Type [] parameters = TypeManager.NoTypes;
-                       MethodInfo implementing;
-                       Type fn_type;
-                       string name;
-
-                       flags |= MethodAttributes.HideBySig |
-                               MethodAttributes.SpecialName;
-
-                       if (is_get){
-                               fn_type = PropertyType;
-                               name = "get_" + short_name;
-                       } else {
-                               name = "set_" + short_name;
-                               parameters = new Type [1];
-                               parameters [0] = PropertyType;
-                               fn_type = TypeManager.void_type;
-                       }
-
-                       implementing = parent.IsInterfaceMethod (
-                               iface_type, name, fn_type, parameters, false);
-
-                       //
-                       // For implicit implementations, make sure we are public, for
-                       // explicit implementations, make sure we are private.
-                       //
-                       if (implementing != null){
-                               //
-                               // Setting null inside this block will trigger a more
-                               // verbose error reporting for missing interface implementations
-                               //
-                               // The "candidate" function has been flagged already
-                               // but it wont get cleared
-                               //
-                               if (iface_type == null){
-                                       if ((ModFlags & Modifiers.PUBLIC) == 0)
-                                               implementing = null;
-                                       if ((ModFlags & Modifiers.STATIC) != 0)
-                                               implementing = null;
-                               } else {
-                                       if ((ModFlags & (Modifiers.PUBLIC | Modifiers.ABSTRACT)) != 0){
-                                               Report.Error (
-                                                       106, Location, "`public' or `abstract' modifiers "+
-                                                       "are not allowed in explicit interface declarations"
-                                                       );
-                                               implementing = null;
-                                       }
-                               }
-                       }
-                       
                        //
-                       // If implementing is still valid, set flags
+                       // The PropertyBuilder can be null for explicit implementations, in that
+                       // case, we do not actually emit the ".property", so there is nowhere to
+                       // put the attribute
                        //
-                       if (implementing != null){
-                               flags |=
-                                       MethodAttributes.Virtual |
-                                       // removed newslot
-                                       MethodAttributes.HideBySig;
+                       if (PropertyBuilder != null)
+                               Attribute.ApplyAttributes (ec, PropertyBuilder, this, OptAttributes, Location);
 
-                               // If an interface implementation, then we can set Final.
-                               if (((flags & MethodAttributes.Abstract) == 0) &&
-                                   implementing.DeclaringType.IsInterface)
-                                       flags |= MethodAttributes.Final;
-                               
-                               //
-                               // clear the pending flag
-                               //
-                               parent.IsInterfaceMethod (
-                                       iface_type, name, fn_type, parameters, true);
-                       }
+                       if (GetData != null)
+                               GetData.Emit (tc, Get.Block, Get);
 
-                       //
-                       // If this is not an explicit interface implementation,
-                       // clear implementing, as it is only used for explicit
-                       // interface implementation
-                       //
-                       if (Name.IndexOf (".") == -1)
-                               implementing = null;
-                       
-                       if (is_get){
-                               GetBuilder = parent.TypeBuilder.DefineMethod (
-                                       name, flags, PropertyType, null);
-                               PropertyBuilder.SetGetMethod (GetBuilder);
+                       if (SetData != null)
+                               SetData.Emit (tc, Set.Block, Set);
+               }
+       }
                        
-                               if (implementing != null)
-                                       parent.TypeBuilder.DefineMethodOverride (
-                                               GetBuilder, implementing);
-                               
-                               //
-                               // HACK because System.Reflection.Emit is lame
-                               //
-                               InternalParameters ip = new InternalParameters (
-                                       parent, Parameters.GetEmptyReadOnlyParameters ());
-                               
-                               if (!TypeManager.RegisterMethod (GetBuilder, ip, null)) {
-                                       Report.Error (111, Location,
-                                                     "Class `" + parent.Name +
-                                                     "' already contains a definition with the " +
-                                                     "same return value and parameter types as the " +
-                                                     "'get' method of property `" + Name + "'");
-                                       return false;
-                               }
-                       } else {
-                               SetBuilder = parent.TypeBuilder.DefineMethod (
-                                       name, flags, null, parameters);
-                               
-                               if (implementing != null)
-                                       parent.TypeBuilder.DefineMethodOverride (
-                                               SetBuilder, implementing);
-                               
-                               SetBuilder.DefineParameter (1, ParameterAttributes.None, "value"); 
-                               PropertyBuilder.SetSetMethod (SetBuilder);
-
-                               //
-                               // HACK because System.Reflection.Emit is lame
-                               //
-                               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));
-
-                               if (!TypeManager.RegisterMethod (SetBuilder, ip, parameters)) {
-                                       Report.Error (
-                                               111, Location,
-                                               "Class `" + parent.Name +
-                                               "' already contains a definition with the " +
-                                               "same return value and parameter types as the " +
-                                               "'set' method of property `" + Name + "'");
-                                       return false;
-                               }
-                       }
+       public class Property : PropertyBase {
+               const int AllowedModifiers =
+                       Modifiers.NEW |
+                       Modifiers.PUBLIC |
+                       Modifiers.PROTECTED |
+                       Modifiers.INTERNAL |
+                       Modifiers.PRIVATE |
+                       Modifiers.STATIC |
+                       Modifiers.SEALED |
+                       Modifiers.OVERRIDE |
+                       Modifiers.ABSTRACT |
+                       Modifiers.UNSAFE |
+                       Modifiers.EXTERN |
+                       Modifiers.VIRTUAL;
 
-                       return true;
+               public Property (Expression type, string name, int mod_flags,
+                                Accessor get_block, Accessor set_block,
+                                Attributes attrs, Location loc)
+                       : base (type, name, mod_flags, AllowedModifiers,
+                               Parameters.EmptyReadOnlyParameters,
+                               get_block, set_block, attrs, loc)
+               {
                }
 
                public override bool Define (TypeContainer parent)
                {
-                       Type iface_type = null;
-                       string short_name;
-                       
-                       if (!parent.MethodModifiersValid (ModFlags, Name, Location))
-                               return false;
-
-                       // Lookup Type, verify validity
-                       PropertyType = RootContext.LookupType (parent, Type, false, Location);
-                       if (PropertyType == null)
-                               return false;
-
-                       // verify accessibility
-                       if (!TypeContainer.AsAccessible (PropertyType, ModFlags))
+                       if (!DoDefine (parent))
                                return false;
 
-                       if (PropertyType.IsPointer && !UnsafeOK (parent))
-                               return false;
-                       
                        if (!CheckBase (parent))
                                return false;
 
-                       //
-                       // Check for explicit interface implementation
-                       //
-                       if (Name.IndexOf (".") != -1){
-                               int pos = Name.LastIndexOf (".");
-                               string iface = Name.Substring (0, pos);
-
-                               iface_type = RootContext.LookupType (parent, iface, false, Location);
-                               if (iface_type == null)
-                                       return false;
-
-                               short_name = Name.Substring (pos + 1);
+                       flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
 
-                               // Compute the full name that we need to export.
-                               Name = iface_type.FullName + "." + short_name;
-                       } else
-                               short_name = Name;
+                       if (Get != null) {
+                               Type [] parameters = TypeManager.NoTypes;
 
-                       // FIXME - PropertyAttributes.HasDefault ?
+                               InternalParameters ip = new InternalParameters (
+                                       parent, Parameters.EmptyReadOnlyParameters);
 
-                       PropertyAttributes prop_attr = PropertyAttributes.RTSpecialName |
-                                                      PropertyAttributes.SpecialName;
-               
-                       PropertyBuilder = parent.TypeBuilder.DefineProperty (
-                               Name, prop_attr, PropertyType, null);
+                               GetData = new MethodData (this, "get", MemberType,
+                                                         parameters, ip, CallingConventions.Standard,
+                                                         Get.OptAttributes, ModFlags, flags, false);
 
-                       if (Get != null)
-                               if (!DefineMethod (parent, iface_type, short_name, true))
-                                       return false;
-                       
-                       if (Set != null)
-                               if (!DefineMethod (parent, iface_type, short_name, false))
+                               if (!GetData.Define (parent))
                                        return false;
-                       
-                       //
-                       // 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;
+
+                               GetBuilder = GetData.MethodBuilder;
                        }
 
-                       return true;
-               }
-               
-               public void Emit (TypeContainer tc)
-               {
-                       ILGenerator ig;
-                       EmitContext ec;
+                       if (Set != null) {
+                               Type [] parameters = new Type [1];
+                               parameters [0] = MemberType;
 
-                       ec = new EmitContext (tc, Location, null, PropertyType, ModFlags);
-                       Attribute.ApplyAttributes (ec, PropertyBuilder, this, OptAttributes, Location);
-                       
+                               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));
+
+                               SetData = new MethodData (this, "set", TypeManager.void_type,
+                                                         parameters, ip, CallingConventions.Standard,
+                                                         Set.OptAttributes, ModFlags, flags, false);
 
-                       //
-                       // abstract or extern properties have no bodies
-                       //
-                       if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
-                               return;
+                               if (!SetData.Define (parent))
+                                       return false;
 
-                       if (Get != null){
-                               ig = GetBuilder.GetILGenerator ();
-                               ec = new EmitContext (tc, Location, ig, PropertyType, ModFlags);
-                               
-                               ec.EmitTopBlock (Get, Location);
+                               SetBuilder = SetData.MethodBuilder;
+                               SetBuilder.DefineParameter (1, ParameterAttributes.None, "value"); 
                        }
 
-                       if (Set != null){
-                               ig = SetBuilder.GetILGenerator ();
-                               ec = new EmitContext (tc, Location, ig, null, ModFlags);
+                       // FIXME - PropertyAttributes.HasDefault ?
+                       
+                       PropertyAttributes prop_attr =
+                       PropertyAttributes.RTSpecialName |
+                       PropertyAttributes.SpecialName;
+
+                       if (!IsExplicitImpl){
+                               PropertyBuilder = parent.TypeBuilder.DefineProperty (
+                                       Name, prop_attr, MemberType, null);
+                               
+                               if (Get != null)
+                                       PropertyBuilder.SetGetMethod (GetBuilder);
                                
-                               ec.EmitTopBlock (Set, Location);
+                               if (Set != null)
+                                       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;
+                               }
                        }
+                       return true;
                }
        }
 
-
        /// </summary>
        ///  Gigantic workaround  for lameness in SRE follows :
-       ///  This class derived from EventInfo and attempts to basically
+       ///  This class derives from EventInfo and attempts to basically
        ///  wrap around the EventBuilder so that FindMembers can quickly
        ///  return this in it search for members
        /// </summary>
@@ -3177,9 +3477,7 @@ namespace Mono.CSharp {
                        
                        declaring_type = type_builder;
 
-                       // FIXME : This is supposed to be MyBuilder but since that doesn't
-                       // derive from Type, I have no clue what to do with this.
-                       reflected_type = null;
+                       reflected_type = type_builder;
                        
                        attributes = event_attr;
                        this.name = name;
@@ -3283,8 +3581,7 @@ namespace Mono.CSharp {
                }
        }
        
-       public class Event : MemberCore {
-               
+       public class Event : FieldBase {
                const int AllowedModifiers =
                        Modifiers.NEW |
                        Modifiers.PUBLIC |
@@ -3298,110 +3595,92 @@ namespace Mono.CSharp {
                        Modifiers.UNSAFE |
                        Modifiers.ABSTRACT;
 
-               public readonly string    Type;
-               public readonly Object    Initializer;
-               public readonly Block     Add;
-               public readonly Block     Remove;
+               public readonly Accessor  Add;
+               public readonly Accessor  Remove;
                public MyEventBuilder     EventBuilder;
-               public FieldBuilder       FieldBuilder;
-               public Attributes         OptAttributes;
 
-               Type EventType;
                MethodBuilder AddBuilder, RemoveBuilder;
+               MethodData AddData, RemoveData;
                
-
-               public Event (string type, string name, Object init, int flags, Block add_block,
-                             Block rem_block, Attributes attrs, Location loc)
-                       : base (name, loc)
+               public Event (Expression type, string name, Object init, int mod, Accessor add,
+                             Accessor remove, Attributes attrs, Location loc)
+                       : base (type, mod, AllowedModifiers, name, init, attrs, loc)
                {
-                       Type = type;
-                       Initializer = init;
-                       ModFlags = Modifiers.Check (AllowedModifiers, flags, Modifiers.PRIVATE, loc);  
-                       Add = add_block;
-                       Remove = rem_block;
-                       OptAttributes = attrs;
+                       Add = add;
+                       Remove = remove;
                }
 
                public override bool Define (TypeContainer parent)
                {
-                       if (!parent.MethodModifiersValid (ModFlags, Name, Location))
-                               return false;
-
-                       MethodAttributes m_attr = Modifiers.MethodAttr (ModFlags);
                        EventAttributes e_attr = EventAttributes.RTSpecialName | EventAttributes.SpecialName;
 
-                       EventType = RootContext.LookupType (parent, Type, false, Location);
-                       if (EventType == null)
-                               return false;
-
-                       if (!TypeContainer.AsAccessible (EventType, ModFlags))
+                       if (!DoDefine (parent))
                                return false;
 
-                       if (EventType.IsPointer && !UnsafeOK (parent))
-                               return false;
-
-                       if (!EventType.IsSubclassOf (TypeManager.delegate_type)) {
+                       if (!MemberType.IsSubclassOf (TypeManager.delegate_type)) {
                                Report.Error (66, Location, "'" + parent.Name + "." + Name +
                                              "' : event must be of a delegate type");
                                return false;
                        }
-                       
-                       Type [] parameters = new Type [1];
-                       parameters [0] = EventType;
-
-                       EventBuilder = new MyEventBuilder (parent.TypeBuilder, Name, e_attr, EventType);
 
-                       if (Add == null && Remove == null)
-                               FieldBuilder = parent.TypeBuilder.DefineField (Name, EventType, FieldAttributes.Private);
-                       
-                       //
-                       // Now define the accessors
-                       //
-                       
-                       AddBuilder = parent.TypeBuilder.DefineMethod (
-                                                        "add_" + Name, m_attr, null, parameters);
-                       AddBuilder.DefineParameter (1, ParameterAttributes.None, "value");
-                       EventBuilder.SetAddOnMethod (AddBuilder);
+                       Type [] parameter_types = new Type [1];
+                       parameter_types [0] = MemberType;
 
-                       //
-                       // HACK because System.Reflection.Emit is lame
-                       //
                        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)); 
-                       
-                       if (!TypeManager.RegisterMethod (AddBuilder, ip, parameters)) {
-                               Report.Error (111, Location,
-                                             "Class `" + parent.Name + "' already contains a definition with the " +
-                                             "same return value and parameter types for the " +
-                                             "'add' method of event `" + Name + "'");
+                               parent, new Parameters (parms, null, Location)); 
+
+                       if (!CheckBase (parent))
                                return false;
-                       }
-               
-                       RemoveBuilder = parent.TypeBuilder.DefineMethod (
-                                                           "remove_" + Name, m_attr, null, parameters);
-                       RemoveBuilder.DefineParameter (1, ParameterAttributes.None, "value");
-                       EventBuilder.SetRemoveOnMethod (RemoveBuilder);
 
                        //
-                       // HACK because System.Reflection.Emit is lame
+                       // Now define the accessors
                        //
+                       AddData = new MethodData (this, "add", TypeManager.void_type,
+                                                 parameter_types, ip, CallingConventions.Standard,
+                                                 (Add != null) ? Add.OptAttributes : null,
+                                                 ModFlags, flags, false);
 
-                       if (!TypeManager.RegisterMethod (RemoveBuilder, ip, parameters)) {
-                               Report.Error (111, Location,    
-                                             "Class `" + parent.Name + "' already contains a definition with the " +
-                                             "same return value and parameter types for the " +
-                                             "'remove' method of event `" + Name + "'");
+                       if (!AddData.Define (parent))
                                return false;
-                       }
-                       
-                       if (!TypeManager.RegisterEvent (EventBuilder, AddBuilder, RemoveBuilder)) {
-                               Report.Error (111, Location,
-                                       "Class `" + parent.Name +
-                                       "' already contains a definition for the event `" +
-                                       Name + "'");
+
+                       AddBuilder = AddData.MethodBuilder;
+                       AddBuilder.DefineParameter (1, ParameterAttributes.None, "value");
+
+                       RemoveData = new MethodData (this, "remove", TypeManager.void_type,
+                                                    parameter_types, ip, CallingConventions.Standard,
+                                                    (Remove != null) ? Remove.OptAttributes : null,
+                                                    ModFlags, flags, false);
+
+                       if (!RemoveData.Define (parent))
                                return false;
+
+                       RemoveBuilder = RemoveData.MethodBuilder;
+                       RemoveBuilder.DefineParameter (1, ParameterAttributes.None, "value");
+
+                       if (!IsExplicitImpl){
+                               EventBuilder = new MyEventBuilder (
+                                       parent.TypeBuilder, Name, e_attr, MemberType);
+                                       
+                               if (Add == null && Remove == null) {
+                                       FieldBuilder = parent.TypeBuilder.DefineField (
+                                               Name, MemberType, FieldAttributes.FamANDAssem);
+                                       TypeManager.RegisterPrivateFieldOfEvent (
+                                               (EventInfo) EventBuilder, FieldBuilder);
+                                       TypeManager.RegisterFieldBase (FieldBuilder, this);
+                               }
+                       
+                               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;
+                               }
                        }
                        
                        return true;
@@ -3416,41 +3695,47 @@ namespace Mono.CSharp {
                                method = TypeManager.delegate_combine_delegate_delegate;
                        else
                                method = TypeManager.delegate_remove_delegate_delegate;
-                       
-                       ig.Emit (OpCodes.Ldarg_0);
-                       ig.Emit (OpCodes.Ldarg_0);
-                       ig.Emit (OpCodes.Ldfld, (FieldInfo) FieldBuilder);
-                       ig.Emit (OpCodes.Ldarg_1);
-                       ig.Emit (OpCodes.Call, method);
-                       ig.Emit (OpCodes.Castclass, EventType);
-                       ig.Emit (OpCodes.Stfld, (FieldInfo) FieldBuilder);
+
+                       if ((ModFlags & Modifiers.STATIC) != 0) {
+                               ig.Emit (OpCodes.Ldsfld, (FieldInfo) FieldBuilder);
+                               ig.Emit (OpCodes.Ldarg_0);
+                               ig.Emit (OpCodes.Call, method);
+                               ig.Emit (OpCodes.Castclass, MemberType);
+                               ig.Emit (OpCodes.Stsfld, (FieldInfo) FieldBuilder);
+                       } else {
+                               ig.Emit (OpCodes.Ldarg_0);
+                               ig.Emit (OpCodes.Ldarg_0);
+                               ig.Emit (OpCodes.Ldfld, (FieldInfo) FieldBuilder);
+                               ig.Emit (OpCodes.Ldarg_1);
+                               ig.Emit (OpCodes.Call, method);
+                               ig.Emit (OpCodes.Castclass, MemberType);
+                               ig.Emit (OpCodes.Stfld, (FieldInfo) FieldBuilder);
+                       }
                        ig.Emit (OpCodes.Ret);
                }
 
                public void Emit (TypeContainer tc)
                {
                        EmitContext ec;
-                       ILGenerator ig;
 
-                       ig = AddBuilder.GetILGenerator ();
-                       ec = new EmitContext (tc, Location, ig, TypeManager.void_type, ModFlags);
+                       ec = new EmitContext (tc, Location, null, MemberType, ModFlags);
+                       Attribute.ApplyAttributes (ec, EventBuilder, this, OptAttributes, Location);
 
                        if (Add != null)
-                               ec.EmitTopBlock (Add, Location);
-                       else
+                               AddData.Emit (tc, Add.Block, Add);
+                       else {
+                               ILGenerator ig = AddData.MethodBuilder.GetILGenerator ();
+                               ec = new EmitContext (tc, Location, ig, TypeManager.void_type, ModFlags);
                                EmitDefaultMethod (ec, true);
+                       }
 
-                       ig = RemoveBuilder.GetILGenerator ();
-                       ec = new EmitContext (tc, Location, ig, TypeManager.void_type, ModFlags);
-                       
                        if (Remove != null)
-                               ec.EmitTopBlock (Remove, Location);
-                       else
+                               RemoveData.Emit (tc, Remove.Block, Remove);
+                       else {
+                               ILGenerator ig = RemoveData.MethodBuilder.GetILGenerator ();
+                               ec = new EmitContext (tc, Location, ig, TypeManager.void_type, ModFlags);
                                EmitDefaultMethod (ec, false);
-
-                       ec = new EmitContext (tc, Location, null, EventType, ModFlags);
-                       Attribute.ApplyAttributes (ec, EventBuilder, this, OptAttributes, Location);
-                       
+                       }
                }
                
        }
@@ -3459,12 +3744,13 @@ namespace Mono.CSharp {
        // FIXME: This does not handle:
        //
        //   int INTERFACENAME [ args ]
+       //   Does not 
        //
        // Only:
        // 
        // int this [ args ]
  
-       public class Indexer : MemberCore {
+       public class Indexer : PropertyBase {
 
                const int AllowedModifiers =
                        Modifiers.NEW |
@@ -3476,225 +3762,116 @@ namespace Mono.CSharp {
                        Modifiers.SEALED |
                        Modifiers.OVERRIDE |
                        Modifiers.UNSAFE |
+                       Modifiers.EXTERN |
                        Modifiers.ABSTRACT;
 
-               public readonly string     Type;
-               public readonly string     InterfaceType;
-               public readonly Parameters FormalParameters;
-               public readonly Block      Get;
-               public readonly Block      Set;
-               public Attributes          OptAttributes;
-               public MethodBuilder       GetBuilder;
-               public MethodBuilder       SetBuilder;
-               public PropertyBuilder PropertyBuilder;
-               public Type IndexerType;
+               public string IndexerName;
+               public string InterfaceIndexerName;
 
-               public Indexer (string type, string int_type, int flags, Parameters parms,
-                               Block get_block, Block set_block, Attributes attrs, Location loc)
-                       : base ("", loc)
+               //
+               // Are we implementing an interface ?
+               //
+               bool IsImplementing = false;
+               
+               public Indexer (Expression type, string int_type, int flags, Parameters parameters,
+                               Accessor get_block, Accessor set_block, Attributes attrs, Location loc)
+                       : base (type, "", flags, AllowedModifiers, parameters, get_block, set_block,
+                               attrs, loc)
                {
-
-                       Type = type;
-                       InterfaceType = int_type;
-                       ModFlags = Modifiers.Check (AllowedModifiers, flags, Modifiers.PRIVATE, loc);
-                       FormalParameters = parms;
-                       Get = get_block;
-                       Set = set_block;
-                       OptAttributes = attrs;
+                       ExplicitInterfaceName = int_type;
                }
 
-               void DefineMethod (TypeContainer parent, Type iface_type,
-                                  Type ret_type, string name,
-                                  Type [] parameters, bool is_get)
-               {
-                       MethodAttributes attr = Modifiers.MethodAttr (ModFlags);
-                       MethodInfo implementing;
-
-                       implementing = parent.IsInterfaceMethod (
-                               iface_type, name, ret_type, parameters, false);
-
-                       //
-                       // Setting null inside this block will trigger a more
-                       // verbose error reporting for missing interface implementations
-                       //
-                       // The "candidate" function has been flagged already
-                       // but it wont get cleared
-                       //
-                       if (implementing != null){
-                               if (iface_type == null){
-                                       if ((ModFlags & Modifiers.PUBLIC) == 0)
-                                               implementing = null;
-                                       if ((ModFlags & Modifiers.STATIC) != 0)
-                                               implementing = null;
-                               } else {
-                                       if((ModFlags&(Modifiers.PUBLIC | Modifiers.ABSTRACT)) != 0){
-                                               Report.Error (
-                                                       106, Location,
-                                                       "`public' or `abstract' modifiers are not "+
-                                                       "allowed in explicit interface declarations"
-                                                       );
-                                               implementing = null;
-                                       }
-                               }
-                       }
-                       if (implementing != null){
-                               attr |=
-                                       MethodAttributes.Virtual |
-                                       // removed newslot
-                                       MethodAttributes.HideBySig;
-
-                               // If an interface implementing, then we can set final.
-                               if (((attr & MethodAttributes.Abstract) == 0) &&
-                                   implementing.DeclaringType.IsInterface)
-                                       attr |= MethodAttributes.Final;
-                               
-                               //
-                               // clear the pending flag
-                               //
-                               parent.IsInterfaceMethod (
-                                       iface_type, name, ret_type, parameters, true);
-                       }
-
-                       //
-                       // If this is not an explicit interface implementation,
-                       // clear implementing, as it is only used for explicit
-                       // interface implementation
-                       //
-                       if (Name.IndexOf (".") == -1)
-                               implementing = null;
-
-                       if (is_get){
-
-                               string meth_name = "get_Item";
-                               if (iface_type != null)
-                                       meth_name = iface_type + ".get_Item";
-                               
-                               GetBuilder = parent.TypeBuilder.DefineMethod (
-                                       meth_name, attr, IndexerType, parameters);
-
-                               if (implementing != null) 
-                                       parent.TypeBuilder.DefineMethodOverride (
-                                               GetBuilder, implementing);
-                               
-                               
-                               PropertyBuilder.SetGetMethod (GetBuilder);
-                       } else {
-
-                               string meth_name = "set_Item";
-
-                               if (iface_type != null)
-                                       meth_name = iface_type + ".set_Item";
-                               
-                               SetBuilder = parent.TypeBuilder.DefineMethod (
-                                       meth_name, attr, null, parameters);
-                               if (implementing != null)
-                                       parent.TypeBuilder.DefineMethodOverride (
-                                               SetBuilder, implementing);
-                                       
-                               PropertyBuilder.SetSetMethod (SetBuilder);
-                       }
-               }
-                       
                public override bool Define (TypeContainer parent)
                {
                        PropertyAttributes prop_attr =
                                PropertyAttributes.RTSpecialName |
                                PropertyAttributes.SpecialName;
-                       bool error = false;
                        
-                       IndexerType = RootContext.LookupType (parent, Type, false, Location);
-                       Type [] parameters = FormalParameters.GetParameterInfo (parent);
-
-                       // Check if the return type and arguments were correct
-                       if (IndexerType == null || parameters == null)
-                               return false;
-
-                       if (!parent.MethodModifiersValid (ModFlags, InterfaceType == null ?
-                                                         "this" : InterfaceType, Location))
+                       if (!DoDefine (parent))
                                return false;
 
-                       //
-                       // verify accessibility and unsafe pointers
-                       //
-                       if (!TypeContainer.AsAccessible (IndexerType, ModFlags))
-                               return false;
+                       IndexerName = Attribute.ScanForIndexerName (ec, OptAttributes);
+                       if (IndexerName == null)
+                               IndexerName = "Item";
+                       else if (IsExplicitImpl)
+                               Report.Error (592, Location,
+                                             "Attribute 'IndexerName' is not valid on this declaration " +
+                                             "type. It is valid on `property' declarations only.");
+
+                       ShortName = IndexerName;
+                       if (IsExplicitImpl) {
+                               InterfaceIndexerName = TypeManager.IndexerPropertyName (InterfaceType);
+                               Name = InterfaceType.FullName + "." + IndexerName;
+                       } else {
+                               InterfaceIndexerName = IndexerName;
+                               Name = ShortName;
+                       }
 
-                       if (IndexerType.IsPointer && !UnsafeOK (parent))
+                       if (!CheckBase (parent))
                                return false;
 
-                       foreach (Type partype in parameters){
-                               if (!TypeContainer.AsAccessible (partype, ModFlags))
-                                       error = true;
-                               if (partype.IsPointer && !UnsafeOK (parent))
-                                       error = true;
-                       }
+                       if (Get != null){
+                                InternalParameters ip = new InternalParameters (parent, Parameters);
 
-                       if (error)
-                               return false;
-                       
-                       Type iface_type = null;
+                               GetData = new MethodData (this, "get", MemberType,
+                                                         ParameterTypes, ip, CallingConventions.Standard,
+                                                         Get.OptAttributes, ModFlags, flags, false);
 
-                       if (InterfaceType != null){
-                               iface_type = RootContext.LookupType (parent, InterfaceType, false, Location);
-                               if (iface_type == null)
+                               if (!GetData.Define (parent))
                                        return false;
-                       } 
-                               
-                       
-                       PropertyBuilder = parent.TypeBuilder.DefineProperty (
-                               TypeManager.IndexerPropertyName (parent.TypeBuilder),
-                               prop_attr, IndexerType, parameters);
 
-                       if (Get != null){
-                               DefineMethod (parent, iface_type, IndexerType, "get_Item", parameters, true);
-                                InternalParameters pi = new InternalParameters (parent, FormalParameters);
-                               if (!TypeManager.RegisterMethod (GetBuilder, pi, parameters)) {
-                                       Report.Error (111, Location,
-                                                     "Class `" + parent.Name +
-                                                     "' already contains a definition with the " +
-                                                     "same return value and parameter types for the " +
-                                                     "'get' indexer");
-                                       return false;
-                               }
+                               GetBuilder = GetData.MethodBuilder;
                        }
                        
                        if (Set != null){
-                               int top = parameters.Length;
+                               int top = ParameterTypes.Length;
                                Type [] set_pars = new Type [top + 1];
-                               parameters.CopyTo (set_pars, 0);
-                               set_pars [top] = IndexerType;
+                               ParameterTypes.CopyTo (set_pars, 0);
+                               set_pars [top] = MemberType;
 
-                               Parameter [] fixed_parms = FormalParameters.FixedParameters;
+                               Parameter [] fixed_parms = Parameters.FixedParameters;
 
+                               if (fixed_parms == null){
+                                       throw new Exception ("We currently do not support only array arguments in an indexer");
+                                       // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
+                                       // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
+                                       //
+                                       // Here is the problem: the `value' parameter has
+                                       // to come *after* the array parameter in the declaration
+                                       // like this:
+                                       // X (object [] x, Type value)
+                                       // .param [0]
+                                       //
+                                       // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
+                                       // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
+                                       
+                               }
+                               
                                Parameter [] tmp = new Parameter [fixed_parms.Length + 1];
 
+
                                fixed_parms.CopyTo (tmp, 0);
                                tmp [fixed_parms.Length] = new Parameter (
                                        Type, "value", Parameter.Modifier.NONE, null);
 
                                Parameters set_formal_params = new Parameters (tmp, null, Location);
                                
-                               DefineMethod (
-                                       parent, iface_type, TypeManager.void_type,
-                                       "set_Item", set_pars, false);
-
                                InternalParameters ip = new InternalParameters (parent, set_formal_params);
-                               
-                               if (!TypeManager.RegisterMethod (SetBuilder, ip, set_pars)) {
-                                       Report.Error (
-                                               111, Location,
-                                               "Class `" + parent.Name + "' already contains a " +
-                                               "definition with the " +
-                                               "same return value and parameter types for the " +
-                                               "'set' indexer");
+
+                               SetData = new MethodData (this, "set", TypeManager.void_type,
+                                                         set_pars, ip, CallingConventions.Standard,
+                                                         Set.OptAttributes, ModFlags, flags, false);
+
+                               if (!SetData.Define (parent))
                                        return false;
-                               }
+
+                               SetBuilder = SetData.MethodBuilder;
                        }
 
                        //
                        // Now name the parameters
                        //
-                       Parameter [] p = FormalParameters.FixedParameters;
+                       Parameter [] p = Parameters.FixedParameters;
                        if (p != null) {
                                int i;
                                
@@ -3712,39 +3889,39 @@ namespace Mono.CSharp {
                                        SetBuilder.DefineParameter (
                                                i + 1, ParameterAttributes.None, "value");
                                        
-                               if (i != parameters.Length) {
-                                       Parameter array_param = FormalParameters.ArrayParameter;
-                                       SetBuilder.DefineParameter (i + 1, array_param.Attributes,
-                                                                   array_param.Name);
+                               if (i != ParameterTypes.Length) {
+                                       Parameter array_param = Parameters.ArrayParameter;
+                                       SetBuilder.DefineParameter (
+                                               i + 1, array_param.Attributes, array_param.Name);
                                }
                        }
 
-                       TypeManager.RegisterProperty (PropertyBuilder, GetBuilder, SetBuilder);
-
-                       return true;
-               }
+                       if (GetData != null)
+                               IsImplementing = GetData.IsImplementing;
+                       else if (SetData != null)
+                               IsImplementing = SetData.IsImplementing;
 
-               public void Emit (TypeContainer tc)
-               {
-                       ILGenerator ig;
-                       EmitContext ec;
+                       //
+                       // 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);
 
-                       ec = new EmitContext (tc, Location, null, IndexerType, ModFlags);
-                       Attribute.ApplyAttributes (ec, PropertyBuilder, this, OptAttributes, Location);
+                               if (GetData != null)
+                                       PropertyBuilder.SetGetMethod (GetBuilder);
 
-                       if (Get != null){
-                               ig = GetBuilder.GetILGenerator ();
-                               ec = new EmitContext (tc, Location, ig, IndexerType, ModFlags);
+                               if (SetData != null)
+                                       PropertyBuilder.SetSetMethod (SetBuilder);
                                
-                               ec.EmitTopBlock (Get, Location);
+                               TypeManager.RegisterIndexer (PropertyBuilder, GetBuilder, SetBuilder,
+                                                            ParameterTypes);
                        }
 
-                       if (Set != null){
-                               ig = SetBuilder.GetILGenerator ();
-                               ec = new EmitContext (tc, Location, ig, null, ModFlags);
-                               
-                               ec.EmitTopBlock (Set, Location);
-                       }
+                       return true;
                }
        }
 
@@ -3799,11 +3976,9 @@ namespace Mono.CSharp {
                };
 
                public readonly OpType OperatorType;
-               public readonly string ReturnType;
-               public readonly string FirstArgType;
-               public readonly string FirstArgName;
-               public readonly string SecondArgType;
-               public readonly string SecondArgName;
+               public readonly Expression ReturnType;
+               public readonly Expression FirstArgType, SecondArgType;
+               public readonly string FirstArgName, SecondArgName;
                public readonly Block  Block;
                public Attributes      OptAttributes;
                public MethodBuilder   OperatorMethodBuilder;
@@ -3811,8 +3986,10 @@ namespace Mono.CSharp {
                public string MethodName;
                public Method OperatorMethod;
 
-               public Operator (OpType type, string ret_type, int flags, string arg1type, string arg1name,
-                                string arg2type, string arg2name, Block block, Attributes attrs, Location loc)
+               public Operator (OpType type, Expression ret_type, int flags,
+                                Expression arg1type, string arg1name,
+                                Expression arg2type, string arg2name,
+                                Block block, Attributes attrs, Location loc)
                        : base ("", loc)
                {
                        OperatorType = type;
@@ -3860,7 +4037,8 @@ namespace Mono.CSharp {
                        OperatorMethod = new Method (ReturnType, ModFlags, MethodName,
                                                     new Parameters (param_list, null, Location),
                                                     OptAttributes, Mono.CSharp.Location.Null);
-                       
+
+                       OperatorMethod.IsOperator = true;                       
                        OperatorMethod.Define (parent);
 
                        if (OperatorMethod.MethodBuilder == null)
@@ -3868,7 +4046,7 @@ namespace Mono.CSharp {
                        
                        OperatorMethodBuilder = OperatorMethod.MethodBuilder;
 
-                       Type [] param_types = OperatorMethod.ParameterTypes (parent);
+                       Type [] param_types = OperatorMethod.ParameterTypes;
                        Type declaring_type = OperatorMethodBuilder.DeclaringType;
                        Type return_type = OperatorMethod.GetReturnType (parent);
                        Type first_arg_type = param_types [0];
@@ -4003,12 +4181,22 @@ namespace Mono.CSharp {
                ///   from the current assembly and class
                /// </summary>
                public static MemberFilter inheritable_method_signature_filter;
+
+               /// <summary>
+               ///   This delegate is used to extract inheritable methods which
+               ///   have the same signature as the argument.  By inheritable,
+               ///   this means that we have permissions to override the method
+               ///   from the current assembly and class
+               /// </summary>
+               public static MemberFilter inheritable_property_signature_filter;
                
                static MethodSignature ()
                {
                        method_signature_filter = new MemberFilter (MemberSignatureCompare);
                        inheritable_method_signature_filter = new MemberFilter (
                                InheritableMemberSignatureCompare);
+                       inheritable_property_signature_filter = new MemberFilter (
+                               InheritablePropertySignatureCompare);
                }
                
                public MethodSignature (string name, Type ret_type, Type [] parameters)
@@ -4059,22 +4247,35 @@ namespace Mono.CSharp {
 
                static bool MemberSignatureCompare (MemberInfo m, object filter_criteria)
                {
-                       MethodInfo mi;
-
-                       if (! (m is MethodInfo))
-                               return false;
-
                        MethodSignature sig = (MethodSignature) filter_criteria;
 
                        if (m.Name != sig.Name)
                                return false;
-                       
-                       mi = (MethodInfo) m;
 
-                       if (mi.ReturnType != sig.RetType)
+                       Type ReturnType;
+                       MethodInfo mi = m as MethodInfo;
+                       PropertyInfo pi = m as PropertyInfo;
+
+                       if (mi != null)
+                               ReturnType = mi.ReturnType;
+                       else if (pi != null)
+                               ReturnType = pi.PropertyType;
+                       else
                                return false;
+                       
+                       //
+                       // we use sig.RetType == null to mean `do not check the
+                       // method return value.  
+                       //
+                       if (sig.RetType != null)
+                               if (ReturnType != sig.RetType)
+                                       return false;
 
-                       Type [] args = TypeManager.GetArgumentTypes (mi);
+                       Type [] args;
+                       if (mi != null)
+                               args = TypeManager.GetArgumentTypes (mi);
+                       else
+                               args = TypeManager.GetArgumentTypes (pi);
                        Type [] sigp = sig.Parameters;
 
                        if (args.Length != sigp.Length)
@@ -4090,7 +4291,13 @@ namespace Mono.CSharp {
 
                //
                // This filter should be used when we are requesting methods that
-               // we want to override.  
+               // we want to override.
+               //
+               // This makes a number of assumptions, for example
+               // that the methods being extracted are of a parent
+               // class (this means we know implicitly that we are
+               // being called to find out about members by a derived
+               // class).
                // 
                static bool InheritableMemberSignatureCompare (MemberInfo m, object filter_criteria)
                {
@@ -4105,7 +4312,48 @@ namespace Mono.CSharp {
                                // If only accessible to the defining assembly or 
                                if (prot == MethodAttributes.FamANDAssem ||
                                    prot == MethodAttributes.Assembly){
-                                       if (m is MethodBuilder)
+                                       if (m.DeclaringType.Assembly == CodeGen.AssemblyBuilder)
+                                               return true;
+                                       else
+                                               return false;
+                               }
+
+                               // Anything else (FamOrAssembly and Public) is fine
+                               return true;
+                       }
+                       return false;
+               }
+
+               //
+               // This filter should be used when we are requesting properties that
+               // we want to override.
+               //
+               // This makes a number of assumptions, for example
+               // that the methods being extracted are of a parent
+               // class (this means we know implicitly that we are
+               // being called to find out about members by a derived
+               // class).
+               // 
+               static bool InheritablePropertySignatureCompare (MemberInfo m, object filter_criteria)
+               {
+                       if (MemberSignatureCompare (m, filter_criteria)){
+                               PropertyInfo pi = (PropertyInfo) m;
+
+                               MethodInfo inherited_get = TypeManager.GetPropertyGetter (pi);
+                               MethodInfo inherited_set = TypeManager.GetPropertySetter (pi);
+
+                               MethodInfo mi = inherited_get == null ? inherited_set : inherited_get;
+
+                               MethodAttributes prot = mi.Attributes & MethodAttributes.MemberAccessMask;
+
+                               // If only accessible to the current class.
+                               if (prot == MethodAttributes.Private)
+                                       return false;
+
+                               // If only accessible to the defining assembly or 
+                               if (prot == MethodAttributes.FamANDAssem ||
+                                   prot == MethodAttributes.Assembly){
+                                       if (m.DeclaringType.Assembly == CodeGen.AssemblyBuilder)
                                                return true;
                                        else
                                                return false;
@@ -4116,5 +4364,5 @@ namespace Mono.CSharp {
                        }
                        return false;
                }
-       }               
+       }
 }