Committing Miguel's type lookup patch.
[mono.git] / mcs / mcs / class.cs
index 3cf54210d277311052daa30f8baf8349f0cb4ade..584826ba5b49a3117db292b1ff6cee00f969bb5a 100755 (executable)
@@ -103,6 +103,9 @@ namespace Mono.CSharp {
                public bool AllowMultiple = false;
                public bool Inherited;
 
+               // The interfaces we implement.
+               Type [] ifaces;
+               
                //
                // The indexer name for this class
                //
@@ -215,7 +218,11 @@ namespace Mono.CSharp {
                        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);
 
@@ -274,13 +281,13 @@ 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){
@@ -305,7 +312,7 @@ namespace Mono.CSharp {
 
                        if ((field.ModFlags & Modifiers.STATIC) == 0)
                                have_nonstatic_fields = true;
-                       
+
                        DefineName (name, field);
                        return AdditionResult.Success;
                }
@@ -321,7 +328,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;
@@ -349,7 +359,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;
                }
@@ -416,6 +429,10 @@ namespace Mono.CSharp {
                        get {
                                return fields;
                        }
+
+                       set {
+                               fields = value;
+                       }
                }
 
                public ArrayList InstanceConstructors {
@@ -505,9 +522,7 @@ namespace Mono.CSharp {
                                if (init is Expression)
                                        e = (Expression) init;
                                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);
+                                       e = new ArrayCreation (f.Type, "", (ArrayList)init, f.Location);
                                }
 
                                Location l = f.Location;
@@ -617,14 +632,16 @@ namespace Mono.CSharp {
                        count = bases.Count;
 
                        if (is_class){
-                               string name = (string) bases [0];
-                               Type first = FindType (name);
+                               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;
@@ -640,8 +657,10 @@ 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 = FindType (name);
+                               Expression name = (Expression) bases [i];
+                               Expression resolved = ResolveTypeExpr (name, false, Location);
+                               bases [i] = resolved;
+                               Type t = resolved.Type;
 
                                if (t == null){
                                        error = true;
@@ -697,7 +716,6 @@ namespace Mono.CSharp {
                public override TypeBuilder DefineType ()
                {
                        Type parent;
-                       Type [] ifaces;
                        bool error;
                        bool is_class;
 
@@ -783,6 +801,12 @@ namespace Mono.CSharp {
                                }
                        }
 
+                       // 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
                        //
@@ -790,8 +814,9 @@ namespace Mono.CSharp {
 
                        TypeManager.AddUserType (Name, TypeBuilder, this, ifaces);
 
-                       if (parent == TypeManager.attribute_type ||
-                           parent.IsSubclassOf (TypeManager.attribute_type)) {
+                       if ((parent != null) &&
+                           (parent == TypeManager.attribute_type ||
+                            parent.IsSubclassOf (TypeManager.attribute_type))) {
                                RootContext.RegisterAttribute (this);
                                TypeManager.RegisterAttrType (TypeBuilder, this);
                        } else
@@ -1002,17 +1027,6 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               /// <summary>
-               ///   Looks up the alias for the name
-               /// </summary>
-               public string LookupAlias (string name)
-               {
-                       if (Namespace != null)
-                               return Namespace.LookupAlias (name);
-                       else
-                               return null;
-               }
-               
                /// <summary>
                ///   This function is based by a delegate to the FindMembers routine
                /// </summary>
@@ -1066,7 +1080,7 @@ namespace Mono.CSharp {
 
                        if (filter == null)
                                filter = accepting_filter; 
-                       
+
                        if ((mt & MemberTypes.Field) != 0) {
                                if (fields != null) {
                                        foreach (Field f in fields) {
@@ -1178,17 +1192,32 @@ namespace Mono.CSharp {
                        }
                        
                        if ((mt & MemberTypes.NestedType) != 0) {
+                               if (types != null){
+                                       foreach (TypeContainer t in types) {
+                                               TypeBuilder tb = t.TypeBuilder;
 
-                               if (Types != null)
-                                       foreach (TypeContainer t in Types)  
-                                               if (filter (t.TypeBuilder, criteria) == true)
-                                                       members.Add (t.TypeBuilder);
+                                               if (tb != null && (filter (tb, criteria) == true))
+                                                               members.Add (tb);
+                                       }
+                               }
+
+                               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){
@@ -1206,6 +1235,7 @@ namespace Mono.CSharp {
                                        ConstructorBuilder cb =
                                                default_static_constructor.ConstructorBuilder;
                                        
+                                       if (cb != null)
                                        if (filter (cb, criteria) == true)
                                                members.Add (cb);
                                }
@@ -1232,24 +1262,7 @@ namespace Mono.CSharp {
                        return null;
                }
 
-               public MemberInfo GetFieldFromEvent (EventExpr event_expr)
-               {
-                       if (events == null)
-                               return null;
-                       
-                       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)
@@ -1442,6 +1455,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);
@@ -1458,6 +1472,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 +
@@ -1529,6 +1550,37 @@ 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 class Class : TypeContainer {
@@ -1737,7 +1789,7 @@ namespace Mono.CSharp {
        }
        
        public class Method : MethodCore {
-               public readonly string ReturnType;
+               public Expression ReturnType;
                public MethodBuilder MethodBuilder;
                public readonly Attributes OptAttributes;
 
@@ -1763,7 +1815,7 @@ 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)
                {
@@ -1782,8 +1834,7 @@ namespace Mono.CSharp {
                public Type GetReturnType (TypeContainer parent)
                {
                        if (type_return_type == null)
-                               type_return_type = RootContext.LookupType (
-                                       parent, ReturnType, false, Location);
+                               type_return_type = parent.ResolveType (ReturnType, false, Location);
                        
                        return type_return_type;
                }
@@ -1920,7 +1971,6 @@ namespace Mono.CSharp {
                        //
                        // If we implement an interface, extract the interface name.
                        //
-
                        if (Name.IndexOf (".") != -1){
                                int pos = Name.LastIndexOf (".");
                                iface = Name.Substring (0, pos);
@@ -1933,6 +1983,10 @@ namespace Mono.CSharp {
 
                                // Compute the full name that we need to export
                                Name = iface_type.FullName + "." + short_name;
+
+                               if (!parent.VerifyImplements (iface_type, short_name, Name, Location))
+                                       return false;
+                               
                                explicit_impl = true;
                        } else
                                short_name = Name;
@@ -1941,10 +1995,16 @@ namespace Mono.CSharp {
                        // Check if we are an implementation of an interface method or
                        // a method
                        //
-                       if (parent.Pending != null)
+                       if (parent.Pending != null){
                                implementing = parent.Pending.IsInterfaceMethod (
                                        iface_type, short_name, ret_type, parameters);
-                               
+
+                               if (iface_type != null && implementing == null){
+                                       TypeContainer.Error_ExplicitInterfaceNotMemberInterface (Location, short_name);
+                                       return false;
+                               }
+                       }
+
                        //
                        // For implicit implementations, make sure we are public, for
                        // explicit implementations, make sure we are private.
@@ -1971,11 +2031,8 @@ namespace Mono.CSharp {
                                        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"
-                                                       );
+                                       if ((ModFlags & (Modifiers.PUBLIC | Modifiers.ABSTRACT | Modifiers.VIRTUAL)) != 0){
+                                               Modifiers.Error_InvalidModifier (Location, "public, virtual or abstract");
                                                implementing = null;
                                        }
                                }
@@ -1985,6 +2042,8 @@ namespace Mono.CSharp {
                        // If implementing is still valid, set flags
                        //
                        if (implementing != null){
+                               // Console.WriteLine ("Implementing for:" + (iface_type != null ? iface_type.FullName : "<null>") + " " + short_name);
+                               
                                if (implementing.DeclaringType.IsInterface)
                                        flags |= MethodAttributes.NewSlot;
                                
@@ -2216,6 +2275,11 @@ namespace Mono.CSharp {
                                        return true;
 
                                t = ec.ContainerType.BaseType;
+                               if (ec.ContainerType.IsValueType) {
+                                       Report.Error (522, location,
+                                               "structs cannot call base class constructors");
+                                       return false;
+                               }
                        } else
                                t = ec.ContainerType;
                        
@@ -2245,7 +2309,8 @@ 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);
                        if (parent_constructor != null)
@@ -2365,8 +2430,8 @@ 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)
+                       if ((ModFlags & Modifiers.STATIC) == 0){
+                               if (parent is Class && Initializer == null)
                                        Initializer = new ConstructorBaseInitializer (null, parent.Location);
 
 
@@ -2375,7 +2440,7 @@ namespace Mono.CSharp {
                                // `this' access
                                //
                                ec.IsStatic = true;
-                               if (!Initializer.Resolve (ec))
+                               if (Initializer != null && !Initializer.Resolve (ec))
                                        return;
                                ec.IsStatic = false;
                        }
@@ -2386,12 +2451,11 @@ namespace Mono.CSharp {
                        // Classes can have base initializers and instance field initializers.
                        //
                        if (parent is Class){
-                               if ((ModFlags & Modifiers.STATIC) == 0){
+                               if ((ModFlags & Modifiers.STATIC) == 0)
                                        parent.EmitFieldInitializers (ec);
-
-                                       Initializer.Emit (ec);
-                               }
                        }
+                       if (Initializer != null)
+                               Initializer.Emit (ec);
                        
                        if ((ModFlags & Modifiers.STATIC) != 0)
                                parent.EmitFieldInitializers (ec);
@@ -2407,7 +2471,7 @@ namespace Mono.CSharp {
        // their common bits.  This is also used to flag usage of the field
        //
        abstract public class FieldBase : MemberCore {
-               public readonly string Type;
+               public Expression Type;
                public readonly Object Initializer;
                public readonly Attributes OptAttributes;
                public FieldBuilder  FieldBuilder;
@@ -2419,7 +2483,7 @@ namespace Mono.CSharp {
                //
                // The constructor is only exposed to our children
                //
-               protected FieldBase (string type, int mod, int allowed_mod, string name,
+               protected FieldBase (Expression type, int mod, int allowed_mod, string name,
                                     object init, Attributes attrs, Location loc)
                        : base (name, loc)
                {
@@ -2448,7 +2512,7 @@ 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 (type, mod, AllowedModifiers, name, expr_or_array_init, attrs, loc)
                {
@@ -2456,7 +2520,7 @@ namespace Mono.CSharp {
 
                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;
@@ -2539,7 +2603,7 @@ namespace Mono.CSharp {
        }
                        
        public class Property : MemberCore {
-               public readonly string Type;
+               public Expression Type;
                public Accessor Get, Set;
                public PropertyBuilder PropertyBuilder;
                public Attributes OptAttributes;
@@ -2551,6 +2615,11 @@ namespace Mono.CSharp {
 
                bool explicit_impl;
 
+               //
+               // If true, the interface type we are explicitly implementing
+               //
+               Type explicit_iface_type = null;
+
                const int AllowedModifiers =
                        Modifiers.NEW |
                        Modifiers.PUBLIC |
@@ -2565,7 +2634,7 @@ namespace Mono.CSharp {
                        Modifiers.EXTERN |
                        Modifiers.VIRTUAL;
 
-               public Property (string type, string name, int mod_flags,
+               public Property (Expression type, string name, int mod_flags,
                                 Accessor get_block, Accessor set_block,
                                 Attributes attrs, Location loc)
                        : base (name, loc)
@@ -2641,14 +2710,19 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               bool DefineMethod (TypeContainer parent, Type iface_type, string short_name,
-                                  MethodAttributes flags, bool is_get)
+               bool DefineMethod (TypeContainer parent, string short_name,
+                                  MethodAttributes flags, bool is_get, ref bool is_implementing)
                {
                        Type [] parameters = TypeManager.NoTypes;
                        MethodInfo implementing = null;
                        Type fn_type;
-                       string name;
+                       string name, prefix;
 
+                       if (explicit_impl)
+                               prefix = explicit_iface_type.FullName + ".";
+                       else
+                               prefix = "";
+                               
                        if (is_get){
                                fn_type = PropertyType;
                                name = "get_" + short_name;
@@ -2659,10 +2733,16 @@ namespace Mono.CSharp {
                                fn_type = TypeManager.void_type;
                        }
 
-                       if (parent.Pending != null)
+                       if (parent.Pending != null){
                                implementing = parent.Pending.IsInterfaceMethod (
-                                       iface_type, name, fn_type, parameters);
+                                       explicit_iface_type, name, fn_type, parameters);
 
+                               if (explicit_iface_type != 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.
@@ -2675,7 +2755,7 @@ namespace Mono.CSharp {
                                // The "candidate" function has been flagged already
                                // but it wont get cleared
                                //
-                               if (iface_type == null){
+                               if (explicit_iface_type == null){
                                        //
                                        // We already catch different accessibility settings
                                        // so we just need to check that we are not private
@@ -2689,11 +2769,8 @@ namespace Mono.CSharp {
                                        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"
-                                                       );
+                                       if ((ModFlags & (Modifiers.PUBLIC | Modifiers.ABSTRACT | Modifiers.VIRTUAL)) != 0){
+                                               Modifiers.Error_InvalidModifier (Location, "public, virtual or abstract");
                                                implementing = null;
                                        }
                                }
@@ -2717,7 +2794,9 @@ namespace Mono.CSharp {
                                // clear the pending implemntation flag
                                //
                                parent.Pending.ImplementMethod (
-                                       iface_type, name, fn_type, parameters, explicit_impl);
+                                       explicit_iface_type, name, fn_type, parameters, explicit_impl);
+
+                               is_implementing = true;
                        }
 
                        //
@@ -2730,8 +2809,7 @@ namespace Mono.CSharp {
                        
                        if (is_get){
                                GetBuilder = parent.TypeBuilder.DefineMethod (
-                                       name, flags, PropertyType, null);
-                               PropertyBuilder.SetGetMethod (GetBuilder);
+                                       prefix + name, flags, PropertyType, null);
                        
                                if (implementing != null)
                                        parent.TypeBuilder.DefineMethodOverride (
@@ -2753,14 +2831,13 @@ namespace Mono.CSharp {
                                }
                        } else {
                                SetBuilder = parent.TypeBuilder.DefineMethod (
-                                       name, flags, null, parameters);
+                                       prefix + 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
@@ -2786,7 +2863,6 @@ namespace Mono.CSharp {
 
                public override bool Define (TypeContainer parent)
                {
-                       Type iface_type = null;
                        string short_name;
                        
                        if (!parent.MethodModifiersValid (ModFlags, Name, Location))
@@ -2797,7 +2873,7 @@ namespace Mono.CSharp {
                        flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
 
                        // Lookup Type, verify validity
-                       PropertyType = RootContext.LookupType (parent, Type, false, Location);
+                       PropertyType = parent.ResolveType (Type, false, Location);
                        if (PropertyType == null)
                                return false;
 
@@ -2818,48 +2894,61 @@ namespace Mono.CSharp {
                                int pos = Name.LastIndexOf (".");
                                string iface = Name.Substring (0, pos);
 
-                               iface_type = RootContext.LookupType (parent, iface, false, Location);
-                               if (iface_type == null)
+                               explicit_iface_type = RootContext.LookupType (parent, iface, false, Location);
+                               if (explicit_iface_type == null)
                                        return false;
 
                                short_name = Name.Substring (pos + 1);
 
                                // Compute the full name that we need to export.
-                               Name = iface_type.FullName + "." + short_name;
+                               Name = explicit_iface_type.FullName + "." + short_name;
+                               
+                               if (!parent.VerifyImplements (explicit_iface_type, short_name, Name, Location))
+                                       return false;
+                               
                                explicit_impl = true;
                        } else {
                                explicit_impl = false;
                                short_name = Name;
                        }
 
-                       // FIXME - PropertyAttributes.HasDefault ?
-
-                       PropertyAttributes prop_attr = PropertyAttributes.RTSpecialName |
-                                                      PropertyAttributes.SpecialName;
-               
-                       PropertyBuilder = parent.TypeBuilder.DefineProperty (
-                               Name, prop_attr, PropertyType, null);
-
+                       bool is_implementing = false;
                        if (Get != null)
-                               if (!DefineMethod (parent, iface_type, short_name, flags, true))
+                               if (!DefineMethod (parent, short_name, flags, true, ref is_implementing))
                                        return false;
                        
                        if (Set != null)
-                               if (!DefineMethod (parent, iface_type, short_name, flags, false))
+                               if (!DefineMethod (parent, short_name, flags, false, ref is_implementing))
                                        return false;
+
+                       // FIXME - PropertyAttributes.HasDefault ?
                        
-                       //
-                       // 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;
-                       }
+                       PropertyAttributes prop_attr =
+                       PropertyAttributes.RTSpecialName |
+                       PropertyAttributes.SpecialName;
 
+                       if (!explicit_impl){
+                               PropertyBuilder = parent.TypeBuilder.DefineProperty (
+                                       Name, prop_attr, PropertyType, null);
+                               
+                               if (Get != null)
+                                       PropertyBuilder.SetGetMethod (GetBuilder);
+                               
+                               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;
                }
                
@@ -2869,8 +2958,18 @@ namespace Mono.CSharp {
                        EmitContext ec;
 
                        ec = new EmitContext (tc, Location, null, PropertyType, ModFlags);
-                       Attribute.ApplyAttributes (ec, PropertyBuilder, this, OptAttributes, Location);
-                       
+
+                       //
+                       // The PropertyBuilder can be null for explicit implementations, in that
+                       // case, we do not actually emit the ".property", so there is nowhere to
+                       // put the attribute
+                       //
+                       if (PropertyBuilder != null)
+                               Attribute.ApplyAttributes (ec, PropertyBuilder, this, OptAttributes, Location);
+                       if (Get != null)
+                               Attribute.ApplyAttributes (ec, GetBuilder, Get, Get.OptAttributes, Location);
+                       if (Set != null)
+                               Attribute.ApplyAttributes (ec, SetBuilder, Set, Set.OptAttributes, Location);
 
                        //
                        // abstract or extern properties have no bodies
@@ -2882,7 +2981,6 @@ namespace Mono.CSharp {
                                ig = GetBuilder.GetILGenerator ();
                                ec = new EmitContext (tc, Location, ig, PropertyType, ModFlags);
 
-                               Attribute.ApplyAttributes (ec, GetBuilder, Get, Get.OptAttributes, Location);
                                ec.EmitTopBlock (Get.Block, Location);
                        }
 
@@ -2890,7 +2988,6 @@ namespace Mono.CSharp {
                                ig = SetBuilder.GetILGenerator ();
                                ec = new EmitContext (tc, Location, ig, null, ModFlags);
 
-                               Attribute.ApplyAttributes (ec, SetBuilder, Set, Set.OptAttributes, Location);
                                ec.EmitTopBlock (Set.Block, Location);
                        }
                }
@@ -3053,7 +3150,7 @@ namespace Mono.CSharp {
                Type EventType;
                MethodBuilder AddBuilder, RemoveBuilder;
                
-               public Event (string type, string name, Object init, int mod, Accessor add,
+               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)
                {
@@ -3069,7 +3166,7 @@ namespace Mono.CSharp {
                        MethodAttributes m_attr = Modifiers.MethodAttr (ModFlags);
                        EventAttributes e_attr = EventAttributes.RTSpecialName | EventAttributes.SpecialName;
 
-                       EventType = RootContext.LookupType (parent, Type, false, Location);
+                       EventType = parent.ResolveType (Type, false, Location);
                        if (EventType == null)
                                return false;
 
@@ -3090,9 +3187,10 @@ namespace Mono.CSharp {
 
                        EventBuilder = new MyEventBuilder (parent.TypeBuilder, Name, e_attr, EventType);
 
-                       if (Add == null && Remove == null){
-                               FieldBuilder = parent.TypeBuilder.DefineField (
-                                       Name, EventType, FieldAttributes.Private);
+                       if (Add == null && Remove == null) {
+                               FieldBuilder = parent.TypeBuilder.DefineField (
+                                       Name, EventType, FieldAttributes.FamANDAssem);
+                               TypeManager.RegisterPrivateFieldOfEvent ((EventInfo) EventBuilder, FieldBuilder);
                                TypeManager.RegisterFieldBase (FieldBuilder, this);
                        }
                        
@@ -3129,6 +3227,8 @@ namespace Mono.CSharp {
                                remove_name, m_attr, null, parameters);
                        RemoveBuilder.DefineParameter (1, ParameterAttributes.None, "value");
                        EventBuilder.SetRemoveOnMethod (RemoveBuilder);
+                       if (parent.Pending != null)
+                               parent.Pending.ImplementMethod (null, remove_name, null, parameters, false);
 
                        //
                        // This looks like dead code
@@ -3234,7 +3334,7 @@ namespace Mono.CSharp {
                        Modifiers.EXTERN |
                        Modifiers.ABSTRACT;
 
-               public readonly string     Type;
+               public readonly Expression Type;
                public readonly string     InterfaceType;
                public readonly Parameters FormalParameters;
                public readonly Accessor   Get, Set;
@@ -3247,7 +3347,7 @@ namespace Mono.CSharp {
                
                EmitContext ec;
                
-               public Indexer (string type, string int_type, int flags, Parameters parms,
+               public Indexer (Expression type, string int_type, int flags, Parameters parms,
                                Accessor get_block, Accessor set_block, Attributes attrs, Location loc)
                        : base ("", loc)
                {
@@ -3261,16 +3361,22 @@ namespace Mono.CSharp {
                        OptAttributes = attrs;
                }
 
-               bool DefineMethod (TypeContainer parent, Type iface_type, 
+               bool DefineMethod (TypeContainer parent, Type explicit_iface_type, 
                                   Type ret_type, string name,
                                   Type [] parameters, MethodAttributes attr, bool is_get)
                {
                        MethodInfo implementing = null;
                        bool is_implementation;
 
-                       if (parent.Pending != null)
+                       if (parent.Pending != null){
                                implementing = parent.Pending.IsInterfaceMethod (
-                                       iface_type, name, ret_type, parameters);
+                                       explicit_iface_type, name, ret_type, parameters);
+
+                               if (explicit_iface_type != null && implementing == null){
+                                       TypeContainer.Error_ExplicitInterfaceNotMemberInterface (Location, "this");
+                                       return false;
+                               }
+                       }
 
                        is_implementation = implementing != null;
                        
@@ -3282,7 +3388,7 @@ namespace Mono.CSharp {
                        // but it wont get cleared
                        //
                        if (implementing != null){
-                               if (iface_type == null){
+                               if (explicit_iface_type == null){
                                        //
                                        // We already catch different accessibility settings
                                        // so we just need to check that we are not private
@@ -3296,12 +3402,8 @@ namespace Mono.CSharp {
                                        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"
-                                                       );
+                                       if((ModFlags&(Modifiers.PUBLIC | Modifiers.ABSTRACT | Modifiers.VIRTUAL)) != 0){
+                                               Modifiers.Error_InvalidModifier (Location, "public, virtual or abstract");
                                                implementing = null;
                                        }
                                }
@@ -3321,7 +3423,7 @@ namespace Mono.CSharp {
                                // clear the pending implementing flag
                                //
                                parent.Pending.ImplementMethod (
-                                       iface_type, name, ret_type, parameters, true);
+                                       explicit_iface_type, name, ret_type, parameters, true);
                        }
 
                        //
@@ -3332,10 +3434,14 @@ namespace Mono.CSharp {
                        if (InterfaceType == null)
                                implementing = null;
 
+                       string prefix;
+                       if (explicit_iface_type == null)
+                               prefix = "";
+                       else
+                               prefix = explicit_iface_type.FullName + ".";
+                       
                        if (is_get){
-                               string meth_name = "get_" + IndexerName;
-                               if (iface_type != null)
-                                       meth_name = iface_type + ".get_" + IndexerName;
+                               string meth_name = prefix + "get_" + IndexerName;
                                
                                GetBuilder = parent.TypeBuilder.DefineMethod (
                                        meth_name, attr, IndexerType, parameters);
@@ -3344,11 +3450,8 @@ namespace Mono.CSharp {
                                        parent.TypeBuilder.DefineMethodOverride (
                                                GetBuilder, implementing);
                        } else {
-                               string meth_name = "set_" + IndexerName;
+                               string meth_name = prefix + "set_" + IndexerName;
 
-                               if (iface_type != null)
-                                       meth_name = iface_type + ".set_" + IndexerName;
-                               
                                SetBuilder = parent.TypeBuilder.DefineMethod (
                                        meth_name, attr, null, parameters);
                                if (implementing != null)
@@ -3366,7 +3469,7 @@ namespace Mono.CSharp {
                                PropertyAttributes.SpecialName;
                        bool error = false;
                        
-                       IndexerType = RootContext.LookupType (parent, Type, false, Location);
+                       IndexerType = parent.ResolveType (Type, false, Location);
                        Type [] parameters = FormalParameters.GetParameterInfo (parent);
 
                        // Check if the return type and arguments were correct
@@ -3396,11 +3499,14 @@ namespace Mono.CSharp {
                        if (error)
                                return false;
                        
-                       Type iface_type = null;
+                       Type explicit_iface_type = null;
 
                        if (InterfaceType != null){
-                               iface_type = RootContext.LookupType (parent, InterfaceType, false, Location);
-                               if (iface_type == null)
+                               explicit_iface_type = RootContext.LookupType (parent, InterfaceType, false, Location);
+                               if (explicit_iface_type == null)
+                                       return false;
+
+                               if (!parent.VerifyImplements (explicit_iface_type, "this", "this", Location))
                                        return false;
                        } 
 
@@ -3416,7 +3522,7 @@ namespace Mono.CSharp {
                        
                        if (Get != null){
                                is_implementing = DefineMethod (
-                                       parent, iface_type, IndexerType, "get_" + IndexerName,
+                                       parent, explicit_iface_type, IndexerType, "get_" + IndexerName,
                                        parameters, attr, true);
                                 InternalParameters pi = new InternalParameters (parent, FormalParameters);
                                if (!TypeManager.RegisterMethod (GetBuilder, pi, parameters)) {
@@ -3463,7 +3569,7 @@ namespace Mono.CSharp {
                                Parameters set_formal_params = new Parameters (tmp, null, Location);
                                
                                is_implementing = DefineMethod (
-                                       parent, iface_type, TypeManager.void_type,
+                                       parent, explicit_iface_type, TypeManager.void_type,
                                        "set_" + IndexerName, set_pars, attr, false);
 
                                InternalParameters ip = new InternalParameters (parent, set_formal_params);
@@ -3515,13 +3621,14 @@ namespace Mono.CSharp {
                        if (!is_implementing){
                                PropertyBuilder = parent.TypeBuilder.DefineProperty (
                                        IndexerName, prop_attr, IndexerType, parameters);
-                               TypeManager.RegisterProperty (PropertyBuilder, GetBuilder, SetBuilder);
 
                                if (GetBuilder != null)
                                        PropertyBuilder.SetGetMethod (GetBuilder);
 
                                if (SetBuilder != null)
                                        PropertyBuilder.SetSetMethod (SetBuilder);
+                               
+                               TypeManager.RegisterProperty (PropertyBuilder, GetBuilder, SetBuilder);
                        }
 
                        return true;
@@ -3531,8 +3638,18 @@ namespace Mono.CSharp {
                {
                        ILGenerator ig;
 
-                       Attribute.ApplyAttributes (
-                               ec, PropertyBuilder, this, OptAttributes, Location);
+                       //
+                       // The PropertyBuilder can be null for explicit implementations, in that
+                       // case, we do not actually emit the ".property", so there is nowhere to
+                       // put the attribute
+                       //
+                       if (PropertyBuilder != null)
+                               Attribute.ApplyAttributes (
+                                       ec, PropertyBuilder, this, OptAttributes, Location);
+                       if (Get != null)
+                               Attribute.ApplyAttributes (ec, GetBuilder, Get, Get.OptAttributes, Location);
+                       if (Set != null)
+                               Attribute.ApplyAttributes (ec, SetBuilder, Set, Set.OptAttributes, Location);
 
                        if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
                                return;
@@ -3604,11 +3721,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;
@@ -3616,8 +3731,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;