2002-10-19 Ravi Pratap <ravi@ximian.com>
[mono.git] / mcs / mcs / class.cs
index 2ede8c38e6b24c13b85f1a55896f08c54e475776..f7c8810c31a43eec822c010dc8605250c4a71d14 100755 (executable)
@@ -1,3 +1,4 @@
+
 //
 // class.cs: Class and Struct handlers
 //
@@ -9,7 +10,26 @@
 // (C) 2001, 2002 Ximian, Inc (http://www.ximian.com)
 //
 //
-
+//  2002-10-11  Miguel de Icaza  <miguel@ximian.com>
+//
+//     * class.cs: Following the comment from 2002-09-26 to AddMethod, I
+//     have fixed a remaining problem: not every AddXXXX was adding a
+//     fully qualified name.  
+//
+//     Now everyone registers a fully qualified name in the DeclSpace as
+//     being defined instead of the partial name.  
+//
+//     Downsides: we are slower than we need to be due to the excess
+//     copies and the names being registered this way.  
+//
+//     The reason for this is that we currently depend (on the corlib
+//     bootstrap for instance) that types are fully qualified, because
+//     we dump all the types in the namespace, and we should really have
+//     types inserted into the proper namespace, so we can only store the
+//     basenames in the defined_names array.
+//
+//
+#define CACHE
 using System;
 using System.Collections;
 using System.Reflection;
@@ -22,7 +42,7 @@ namespace Mono.CSharp {
        /// <summary>
        ///   This is the base class for structs and classes.  
        /// </summary>
-       public class TypeContainer : DeclSpace, IMemberFinder {
+       public class TypeContainer : DeclSpace, IMemberContainer {
                // Holds a list of classes and structures
                ArrayList types;
 
@@ -106,6 +126,10 @@ namespace Mono.CSharp {
 
                // The interfaces we implement.
                Type [] ifaces;
+
+               // The parent member container and our member cache
+               IMemberContainer parent_container;
+               MemberCache member_cache;
                
                //
                // The indexer name for this class
@@ -131,16 +155,16 @@ namespace Mono.CSharp {
                public AdditionResult AddConstant (Const constant)
                {
                        AdditionResult res;
-                       string name = constant.Name;
+                       string basename = constant.Name;
 
-                       if ((res = IsValid (name)) != AdditionResult.Success)
+                       if ((res = IsValid (basename)) != AdditionResult.Success)
                                return res;
                        
                        if (constants == null)
                                constants = new ArrayList ();
 
                        constants.Add (constant);
-                       DefineName (name, constant);
+                       DefineName (Name + "." + basename, constant);
 
                        return AdditionResult.Success;
                }
@@ -148,16 +172,15 @@ namespace Mono.CSharp {
                public AdditionResult AddEnum (Mono.CSharp.Enum e)
                {
                        AdditionResult res;
-                       string name = e.Name;
 
-                       if ((res = IsValid (name)) != AdditionResult.Success)
+                       if ((res = IsValid (e.Basename)) != AdditionResult.Success)
                                return res;
 
                        if (enums == null)
                                enums = new ArrayList ();
 
                        enums.Add (e);
-                       DefineName (name, e);
+                       DefineName (e.Name, e);
 
                        return AdditionResult.Success;
                }
@@ -165,13 +188,11 @@ namespace Mono.CSharp {
                public AdditionResult AddClass (Class c)
                {
                        AdditionResult res;
-                       string name = c.Name;
 
-
-                       if ((res = IsValid (name)) != AdditionResult.Success)
+                       if ((res = IsValid (c.Basename)) != AdditionResult.Success)
                                return res;
 
-                       DefineName (name, c);
+                       DefineName (c.Name, c);
                        types.Add (c);
 
                        return AdditionResult.Success;
@@ -180,12 +201,11 @@ namespace Mono.CSharp {
                public AdditionResult AddStruct (Struct s)
                {
                        AdditionResult res;
-                       string name = s.Name;
                        
-                       if ((res = IsValid (name)) != AdditionResult.Success)
+                       if ((res = IsValid (s.Basename)) != AdditionResult.Success)
                                return res;
 
-                       DefineName (name, s);
+                       DefineName (s.Name, s);
                        types.Add (s);
 
                        return AdditionResult.Success;
@@ -194,15 +214,14 @@ namespace Mono.CSharp {
                public AdditionResult AddDelegate (Delegate d)
                {
                        AdditionResult res;
-                       string name = d.Name;
 
-                       if ((res = IsValid (name)) != AdditionResult.Success)
+                       if ((res = IsValid (d.Basename)) != AdditionResult.Success)
                                return res;
 
                        if (delegates == null)
                                delegates = new ArrayList ();
                        
-                       DefineName (name, d);
+                       DefineName (d.Name, d);
                        delegates.Add (d);
 
                        return AdditionResult.Success;
@@ -210,13 +229,15 @@ namespace Mono.CSharp {
 
                public AdditionResult AddMethod (Method method)
                {
-                       string name = method.Name;
-                       Object value = defined_names [name];
-                       
+                       string basename = method.Name;
+                       string fullname = Name + "." + basename;
+
+                       Object value = defined_names [fullname];
+
                        if (value != null && (!(value is Method)))
                                return AdditionResult.NameExists;
 
-                       if (name == Basename)
+                       if (basename == Basename)
                                return AdditionResult.EnclosingClash;
 
                        if (methods == null)
@@ -227,8 +248,8 @@ namespace Mono.CSharp {
                        else 
                                methods.Add (method);
                        
-                       if (value != null)
-                               DefineName (name, method);
+                       if (value == null)
+                               DefineName (fullname, method);
 
                        return AdditionResult.Success;
                }
@@ -268,15 +289,14 @@ namespace Mono.CSharp {
                public AdditionResult AddInterface (Interface iface)
                {
                        AdditionResult res;
-                       string name = iface.Name;
 
-                       if ((res = IsValid (name)) != AdditionResult.Success)
+                       if ((res = IsValid (iface.Basename)) != AdditionResult.Success)
                                return res;
                        
                        if (interfaces == null)
                                interfaces = new ArrayList ();
                        interfaces.Add (iface);
-                       DefineName (name, iface);
+                       DefineName (iface.Name, iface);
                        
                        return AdditionResult.Success;
                }
@@ -284,9 +304,9 @@ namespace Mono.CSharp {
                public AdditionResult AddField (Field field)
                {
                        AdditionResult res;
-                       string name = field.Name;
+                       string basename = field.Name;
 
-                       if ((res = IsValid (name)) != AdditionResult.Success)
+                       if ((res = IsValid (basename)) != AdditionResult.Success)
                                return res;
                        
                        if (fields == null)
@@ -317,16 +337,16 @@ namespace Mono.CSharp {
                        if ((field.ModFlags & Modifiers.STATIC) == 0)
                                have_nonstatic_fields = true;
 
-                       DefineName (name, field);
+                       DefineName (Name + "." + basename, field);
                        return AdditionResult.Success;
                }
 
                public AdditionResult AddProperty (Property prop)
                {
                        AdditionResult res;
-                       string name = prop.Name;
+                       string basename = prop.Name;
 
-                       if ((res = IsValid (name)) != AdditionResult.Success)
+                       if ((res = IsValid (basename)) != AdditionResult.Success)
                                return res;
 
                        if (properties == null)
@@ -336,7 +356,7 @@ namespace Mono.CSharp {
                                properties.Insert (0, prop);
                        else
                                properties.Add (prop);
-                       DefineName (name, prop);
+                       DefineName (Name + "." + basename, prop);
 
                        return AdditionResult.Success;
                }
@@ -344,16 +364,16 @@ namespace Mono.CSharp {
                public AdditionResult AddEvent (Event e)
                {
                        AdditionResult res;
-                       string name = e.Name;
+                       string basename = e.Name;
 
-                       if ((res = IsValid (name)) != AdditionResult.Success)
+                       if ((res = IsValid (basename)) != AdditionResult.Success)
                                return res;
 
                        if (events == null)
                                events = new ArrayList ();
                        
                        events.Add (e);
-                       DefineName (name, e);
+                       DefineName (Name + "." + basename, e);
 
                        return AdditionResult.Success;
                }
@@ -767,46 +787,25 @@ namespace Mono.CSharp {
                        // if (parent_builder is ModuleBuilder) {
                        if (IsTopLevel){
                                ModuleBuilder builder = CodeGen.ModuleBuilder;
+                               TypeBuilder = builder.DefineType (
+                                       Name, type_attributes, parent, ifaces);
                                
-                               //
-                               // Structs with no fields need to have a ".size 1"
-                               // appended
-                               //
-
-                               if (!is_class && !have_nonstatic_fields)
-                                       TypeBuilder = builder.DefineType (Name,
-                                                                         type_attributes,
-                                                                         parent, 
-                                                                         PackingSize.Unspecified, 1);
-                               else
-                               //
-                               // classes or structs with fields
-                               //
-                                       TypeBuilder = builder.DefineType (Name,
-                                                                         type_attributes,
-                                                                         parent,
-                                                                         ifaces);
                        } else {
                                TypeBuilder builder = Parent.TypeBuilder;
+                               TypeBuilder = builder.DefineNestedType (
+                                       Basename, type_attributes, parent, ifaces);
+                       }
                                
-                               //
-                               // Structs with no fields need to have a ".size 1"
-                               // appended
-                               //
-                               if (!is_class && !have_nonstatic_fields)
-                                       TypeBuilder = builder.DefineNestedType (Basename,
-                                                                               type_attributes,
-                                                                               parent, 
-                                                                               PackingSize.Unspecified);
-                               else {
-                                       //
-                                       // classes or structs with fields
-                                       //
-                                       TypeBuilder = builder.DefineNestedType (Basename,
-                                                                               type_attributes,
-                                                                               parent,
-                                                                               ifaces);
-                               }
+                       //
+                       // Structs with no fields need to have at least one byte.
+                       // The right thing would be to set the PackingSize in a DefineType
+                       // but there are no functions that allow interfaces *and* the size to
+                       // be specified.
+                       //
+
+                       if (!is_class && !have_nonstatic_fields){
+                               TypeBuilder.DefineField ("$PRIVATE$", TypeManager.byte_type,
+                                                        FieldAttributes.Private);
                        }
 
                        // add interfaces that were not added at type creation (weird API issue)
@@ -947,7 +946,7 @@ namespace Mono.CSharp {
                        IndexerName = class_indexer_name;
                }
 
-               static void Report1530 (Location loc)
+               static void Error_KeywordNotAllowed (Location loc)
                {
                        Report.Error (1530, loc, "Keyword new not allowed for namespace elements");
                }
@@ -955,16 +954,16 @@ namespace Mono.CSharp {
                /// <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.Define (this);
+                                               iface.DefineMembers (this);
                                        else
-                                               Report1530 (iface.Location);
+                                               Error_KeywordNotAllowed (iface.Location);
                        }
 
                        if (RootContext.WarningLevel > 1){
@@ -1040,15 +1039,36 @@ namespace Mono.CSharp {
                        } else
                                IndexerName = "Item";
 
-                       if (operators != null)
+                       if (operators != null){
                                DefineMembers (operators, null);
 
+                               CheckPairedOperators ();
+                       }
+
                        if (enums != null)
                                DefineMembers (enums, defined_names);
                        
                        if (delegates != null)
                                DefineMembers (delegates, defined_names);
 
+#if CACHE
+                       if (TypeBuilder.BaseType != null)
+                               parent_container = TypeManager.LookupMemberContainer (TypeBuilder.BaseType);
+
+                       member_cache = new MemberCache (this);
+#endif
+
+                       return true;
+               }
+
+               public override bool Define (TypeContainer parent)
+               {
+                       if (interface_order != null){
+                               foreach (Interface iface in interface_order)
+                                       if ((iface.ModFlags & Modifiers.NEW) == 0)
+                                               iface.Define (this);
+                       }
+
                        return true;
                }
 
@@ -1097,11 +1117,35 @@ namespace Mono.CSharp {
                //
                // Since the whole process is a no-op, it is fine to check for null here.
                //
-               public MemberList FindMembers (MemberTypes mt, BindingFlags bf,
-                                              MemberFilter filter, object criteria)
+               public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
+                                                       MemberFilter filter, object criteria)
                {
                        ArrayList members = new ArrayList ();
-                       bool priv = (bf & BindingFlags.NonPublic) != 0;
+
+                       int modflags = 0;
+                       if ((bf & BindingFlags.Public) != 0)
+                               modflags |= Modifiers.PUBLIC | Modifiers.PROTECTED |
+                                       Modifiers.INTERNAL;
+                       if ((bf & BindingFlags.NonPublic) != 0)
+                               modflags |= Modifiers.PRIVATE;
+
+                       int static_mask = 0, static_flags = 0;
+                       switch (bf & (BindingFlags.Static | BindingFlags.Instance)) {
+                       case BindingFlags.Static:
+                               static_mask = static_flags = Modifiers.STATIC;
+                               break;
+
+                       case BindingFlags.Instance:
+                               static_mask = Modifiers.STATIC;
+                               static_flags = 0;
+                               break;
+
+                       default:
+                               static_mask = static_flags = 0;
+                               break;
+                       }
+
+                       Timer.StartTimer (TimerType.TcFindMembers);
 
                        if (filter == null)
                                filter = accepting_filter; 
@@ -1109,9 +1153,10 @@ namespace Mono.CSharp {
                        if ((mt & MemberTypes.Field) != 0) {
                                if (fields != null) {
                                        foreach (Field f in fields) {
-                                               if ((f.ModFlags & Modifiers.PRIVATE) != 0)
-                                                       if (!priv)
-                                                               continue;
+                                               if ((f.ModFlags & modflags) == 0)
+                                                       continue;
+                                               if ((f.ModFlags & static_mask) != static_flags)
+                                                       continue;
 
                                                FieldBuilder fb = f.FieldBuilder;
                                                if (fb != null && filter (fb, criteria) == true)
@@ -1121,10 +1166,11 @@ namespace Mono.CSharp {
 
                                if (constants != null) {
                                        foreach (Const con in constants) {
-                                               if ((con.ModFlags & Modifiers.PRIVATE) != 0)
-                                                       if (!priv)
-                                                               continue;
-                                               
+                                               if ((con.ModFlags & modflags) == 0)
+                                                       continue;
+                                               if ((con.ModFlags & static_mask) != static_flags)
+                                                       continue;
+
                                                FieldBuilder fb = con.FieldBuilder;
                                                if (fb != null && filter (fb, criteria) == true)
                                                        members.Add (fb);
@@ -1135,9 +1181,10 @@ 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;
+                                               if ((m.ModFlags & modflags) == 0)
+                                                       continue;
+                                               if ((m.ModFlags & static_mask) != static_flags)
+                                                       continue;
                                                
                                                MethodBuilder mb = m.MethodBuilder;
 
@@ -1148,9 +1195,10 @@ namespace Mono.CSharp {
 
                                if (operators != null){
                                        foreach (Operator o in operators) {
-                                               if ((o.ModFlags & Modifiers.PRIVATE) != 0)
-                                                       if (!priv)
-                                                               continue;
+                                               if ((o.ModFlags & modflags) == 0)
+                                                       continue;
+                                               if ((o.ModFlags & static_mask) != static_flags)
+                                                       continue;
                                                
                                                MethodBuilder ob = o.OperatorMethodBuilder;
                                                if (ob != null && filter (ob, criteria) == true)
@@ -1160,9 +1208,10 @@ namespace Mono.CSharp {
 
                                if (properties != null){
                                        foreach (Property p in properties){
-                                               if ((p.ModFlags & Modifiers.PRIVATE) != 0)
-                                                       if (!priv)
-                                                               continue;
+                                               if ((p.ModFlags & modflags) == 0)
+                                                       continue;
+                                               if ((p.ModFlags & static_mask) != static_flags)
+                                                       continue;
                                                
                                                MethodBuilder b;
 
@@ -1175,14 +1224,34 @@ namespace Mono.CSharp {
                                                        members.Add (b);
                                        }
                                }
+
+                               if (indexers != null){
+                                       foreach (Indexer ix in indexers){
+                                               if ((ix.ModFlags & modflags) == 0)
+                                                       continue;
+                                               if ((ix.ModFlags & static_mask) != static_flags)
+                                                       continue;
+                                               
+                                               MethodBuilder b;
+
+                                               b = ix.GetBuilder;
+                                               if (b != null && filter (b, criteria) == true)
+                                                       members.Add (b);
+
+                                               b = ix.SetBuilder;
+                                               if (b != null && filter (b, criteria) == true)
+                                                       members.Add (b);
+                                       }
+                               }
                        }
 
                        if ((mt & MemberTypes.Event) != 0) {
                                if (events != null)
                                        foreach (Event e in events) {
-                                               if ((e.ModFlags & Modifiers.PRIVATE) != 0)
-                                                       if (!priv)
-                                                               continue;
+                                               if ((e.ModFlags & modflags) == 0)
+                                                       continue;
+                                               if ((e.ModFlags & static_mask) != static_flags)
+                                                       continue;
 
                                                MemberInfo eb = e.EventBuilder;
                                                if (eb != null && filter (eb, criteria) == true)
@@ -1193,9 +1262,10 @@ namespace Mono.CSharp {
                        if ((mt & MemberTypes.Property) != 0){
                                if (properties != null)
                                        foreach (Property p in properties) {
-                                               if ((p.ModFlags & Modifiers.PRIVATE) != 0)
-                                                       if (!priv)
-                                                               continue;
+                                               if ((p.ModFlags & modflags) == 0)
+                                                       continue;
+                                               if ((p.ModFlags & static_mask) != static_flags)
+                                                       continue;
 
                                                MemberInfo pb = p.PropertyBuilder;
                                                if (pb != null && filter (pb, criteria) == true) {
@@ -1205,9 +1275,10 @@ namespace Mono.CSharp {
 
                                if (indexers != null)
                                        foreach (Indexer ix in indexers) {
-                                               if ((ix.ModFlags & Modifiers.PRIVATE) != 0)
-                                                       if (!priv)
-                                                               continue;
+                                               if ((ix.ModFlags & modflags) == 0)
+                                                       continue;
+                                               if ((ix.ModFlags & static_mask) != static_flags)
+                                                       continue;
 
                                                MemberInfo ib = ix.PropertyBuilder;
                                                if (ib != null && filter (ib, criteria) == true) {
@@ -1219,8 +1290,10 @@ namespace Mono.CSharp {
                        if ((mt & MemberTypes.NestedType) != 0) {
                                if (types != null){
                                        foreach (TypeContainer t in types) {
-                                               TypeBuilder tb = t.TypeBuilder;
+                                               if ((t.ModFlags & modflags) == 0)
+                                                       continue;
 
+                                               TypeBuilder tb = t.TypeBuilder;
                                                if (tb != null && (filter (tb, criteria) == true))
                                                                members.Add (tb);
                                        }
@@ -1228,8 +1301,10 @@ namespace Mono.CSharp {
 
                                if (enums != null){
                                        foreach (Enum en in enums){
-                                               TypeBuilder tb = en.TypeBuilder;
+                                               if ((en.ModFlags & modflags) == 0)
+                                                       continue;
 
+                                               TypeBuilder tb = en.TypeBuilder;
                                                if (tb != null && (filter (tb, criteria) == true))
                                                        members.Add (tb);
                                        }
@@ -1237,8 +1312,21 @@ namespace Mono.CSharp {
                                
                                if (delegates != null){
                                        foreach (Delegate d in delegates){
+                                               if ((d.ModFlags & modflags) == 0)
+                                                       continue;
+
                                                TypeBuilder tb = d.TypeBuilder;
-                                               
+                                               if (tb != null && (filter (tb, criteria) == true))
+                                                       members.Add (tb);
+                                       }
+                               }
+
+                               if (interfaces != null){
+                                       foreach (Interface iface in interfaces){
+                                               if ((iface.ModFlags & modflags) == 0)
+                                                       continue;
+
+                                               TypeBuilder tb = iface.TypeBuilder;
                                                if (tb != null && (filter (tb, criteria) == true))
                                                        members.Add (tb);
                                        }
@@ -1246,17 +1334,16 @@ namespace Mono.CSharp {
                        }
 
                        if ((mt & MemberTypes.Constructor) != 0){
-                               if (instance_constructors != null){
+                               if (((bf & BindingFlags.Instance) != 0) && (instance_constructors != null)){
                                        foreach (Constructor c in instance_constructors){
                                                ConstructorBuilder cb = c.ConstructorBuilder;
-
                                                if (cb != null)
                                                        if (filter (cb, criteria) == true)
                                                                members.Add (cb);
                                        }
                                }
 
-                               if (default_static_constructor != null){
+                               if (((bf & BindingFlags.Static) != 0) && (default_static_constructor != null)){
                                        ConstructorBuilder cb =
                                                default_static_constructor.ConstructorBuilder;
                                        
@@ -1274,10 +1361,16 @@ namespace Mono.CSharp {
                                members.AddRange (list);
                        }
 
+                       Timer.StopTimer (TimerType.TcFindMembers);
+
                        return new MemberList (members);
                }
 
-               
+               public override MemberCache MemberCache {
+                       get {
+                               return member_cache;
+                       }
+               }
 
                public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
                                                      MemberFilter filter, object criteria)
@@ -1435,7 +1528,7 @@ namespace Mono.CSharp {
 
                        if (Delegates != null)
                                foreach (Delegate d in Delegates)
-                                       d.CloseDelegate ();
+                                       d.CloseType ();
                }
 
                public string MakeName (string n)
@@ -1686,6 +1779,170 @@ namespace Mono.CSharp {
                {
                        Report.Error (539, loc, "Explicit implementation: `" + name + "' is not a member of the interface");
                }
+
+               //
+               // IMemberContainer
+               //
+
+               string IMemberContainer.Name {
+                       get {
+                               return Name;
+                       }
+               }
+
+               Type IMemberContainer.Type {
+                       get {
+                               return TypeBuilder;
+                       }
+               }
+
+               IMemberContainer IMemberContainer.Parent {
+                       get {
+                               return parent_container;
+                       }
+               }
+
+               MemberCache IMemberContainer.MemberCache {
+                       get {
+                               return member_cache;
+                       }
+               }
+
+               bool IMemberContainer.IsInterface {
+                       get {
+                               return false;
+                       }
+               }
+
+               MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
+               {
+                       return FindMembers (mt, bf | BindingFlags.DeclaredOnly, null, null);
+               }
+
+               //
+               // Operator pair checking
+               //
+
+               class OperatorEntry {
+                       public int flags;
+                       public Type ret_type;
+                       public Type type1, type2;
+                       public Operator op;
+                       public Operator.OpType ot;
+                       
+                       public OperatorEntry (int f, Operator o)
+                       {
+                               flags = f;
+
+                               ret_type = o.OperatorMethod.GetReturnType ();
+                               Type [] pt = o.OperatorMethod.ParameterTypes;
+                               type1 = pt [0];
+                               type2 = pt [1];
+                               op = o;
+                               ot = o.OperatorType;
+                       }
+
+                       public override int GetHashCode ()
+                       {       
+                               return ret_type.GetHashCode ();
+                       }
+
+                       public override bool Equals (object o)
+                       {
+                               OperatorEntry other = (OperatorEntry) o;
+
+                               if (other.ret_type != ret_type)
+                                       return false;
+                               if (other.type1 != type1)
+                                       return false;
+                               if (other.type2 != type2)
+                                       return false;
+                               return true;
+                       }
+               }
+                               
+               //
+               // Checks that some operators come in pairs:
+               //  == and !=
+               // > and <
+               // >= and <=
+               //
+               // They are matched based on the return type and the argument types
+               //
+               void CheckPairedOperators ()
+               {
+                       Hashtable pairs = new Hashtable (null, null);
+
+                       // Register all the operators we care about.
+                       foreach (Operator op in operators){
+                               int reg = 0;
+                               
+                               switch (op.OperatorType){
+                               case Operator.OpType.Equality:
+                                       reg = 1; break;
+                               case Operator.OpType.Inequality:
+                                       reg = 2; break;
+                                       
+                               case Operator.OpType.GreaterThan:
+                                       reg = 1; break;
+                               case Operator.OpType.LessThan:
+                                       reg = 2; break;
+                                       
+                               case Operator.OpType.GreaterThanOrEqual:
+                                       reg = 1; break;
+                               case Operator.OpType.LessThanOrEqual:
+                                       reg = 2; break;
+                               }
+                               if (reg == 0)
+                                       continue;
+
+                               OperatorEntry oe = new OperatorEntry (reg, op);
+
+                               object o = pairs [oe];
+                               if (o == null)
+                                       pairs [oe] = oe;
+                               else {
+                                       oe = (OperatorEntry) o;
+                                       oe.flags |= reg;
+                               }
+                       }
+
+                       //
+                       // Look for the mistakes.
+                       //
+                       foreach (DictionaryEntry de in pairs){
+                               OperatorEntry oe = (OperatorEntry) de.Key;
+
+                               if (oe.flags == 3)
+                                       continue;
+
+                               string s = "";
+                               switch (oe.ot){
+                               case Operator.OpType.Equality:
+                                       s = "!=";
+                                       break;
+                               case Operator.OpType.Inequality: 
+                                       s = "==";
+                                       break;
+                               case Operator.OpType.GreaterThan: 
+                                       s = "<";
+                                       break;
+                               case Operator.OpType.LessThan:
+                                       s = ">";
+                                       break;
+                               case Operator.OpType.GreaterThanOrEqual:
+                                       s = "<=";
+                                       break;
+                               case Operator.OpType.LessThanOrEqual:
+                                       s = ">=";
+                                       break;
+                               }
+                               Report.Error (216, oe.op.Location,
+                                             "The operator `" + oe.op + "' requires a matching operator `" + s + "' to also be defined");
+                       }
+               }
+               
+               
        }
 
        public class Class : TypeContainer {
@@ -1930,7 +2187,7 @@ namespace Mono.CSharp {
                // function.  Provides a nice cache.  (used between semantic analysis
                // and actual code generation
                //
-               public Type GetReturnType (TypeContainer parent)
+               public Type GetReturnType ()
                {
                        return MemberType;
                }
@@ -2173,9 +2430,9 @@ namespace Mono.CSharp {
                                }
                        } else
                                t = ec.ContainerType;
-                       
+
                        parent_constructor_group = Expression.MemberLookup (
-                               ec, t, ".ctor", 
+                               ec, t, t, ".ctor", 
                                MemberTypes.Constructor,
                                BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
                                loc);
@@ -2200,12 +2457,12 @@ namespace Mono.CSharp {
 
                public void Emit (EmitContext ec)
                {
-                       if (parent_constructor != null)
-                               ec.ig.Emit (OpCodes.Ldarg_0);
-                       if (argument_list != null)
-                               Invocation.EmitArguments (ec, null, argument_list);
-                       if (parent_constructor != null)
-                               ec.ig.Emit (OpCodes.Call, parent_constructor);
+                       if (parent_constructor != null){
+                               if (ec.IsStatic)
+                                       Invocation.EmitCall (ec, true, true, null, parent_constructor, argument_list, loc);
+                               else
+                                       Invocation.EmitCall (ec, true, false, ec.This, parent_constructor, argument_list, loc);
+                       }
                }
        }
 
@@ -2231,12 +2488,13 @@ namespace Mono.CSharp {
                // <summary>
                //   Modifiers allowed for a constructor.
                // </summary>
-               const int AllowedModifiers =
+               public const int AllowedModifiers =
                        Modifiers.PUBLIC |
                        Modifiers.PROTECTED |
                        Modifiers.INTERNAL |
                        Modifiers.STATIC |
                        Modifiers.UNSAFE |
+                       Modifiers.EXTERN |              
                        Modifiers.PRIVATE;
 
                //
@@ -2287,7 +2545,21 @@ namespace Mono.CSharp {
                                                "constructors");
                                        return false;
                                }
-                               ca |= MethodAttributes.Public | MethodAttributes.HideBySig;
+                               ca |= MethodAttributes.HideBySig;
+
+                               if ((ModFlags & Modifiers.PUBLIC) != 0)
+                                       ca |= MethodAttributes.Public;
+                               else if ((ModFlags & Modifiers.PROTECTED) != 0){
+                                       if ((ModFlags & Modifiers.INTERNAL) != 0)
+                                               ca |= MethodAttributes.FamORAssem;
+                                       else 
+                                               ca |= MethodAttributes.Family;
+                               } else if ((ModFlags & Modifiers.INTERNAL) != 0)
+                                       ca |= MethodAttributes.Assembly;
+                               else if (IsDefault ())
+                                       ca |= MethodAttributes.Public;
+                               else
+                                       ca |= MethodAttributes.Private;
                        }
 
                        ConstructorBuilder = parent.TypeBuilder.DefineConstructor (
@@ -2598,7 +2870,7 @@ namespace Mono.CSharp {
                        else
                                name = member.ShortName;
                        method_name = prefix + name;
-                               
+
                        if (parent.Pending != null){
                                if (member is Indexer)
                                        implementing = parent.Pending.IsInterfaceIndexer (
@@ -2656,7 +2928,7 @@ namespace Mono.CSharp {
                                //
                                if (implementing.DeclaringType.IsInterface)
                                        flags |= MethodAttributes.NewSlot;
-                               
+
                                flags |=
                                        MethodAttributes.Virtual |
                                        MethodAttributes.HideBySig;
@@ -2834,7 +3106,7 @@ namespace Mono.CSharp {
                        
                        if (ec.ContainerType.BaseType != null) {
                                Expression member_lookup = Expression.MemberLookup (
-                                       ec, ec.ContainerType.BaseType, "Finalize",
+                                       ec, ec.ContainerType.BaseType, ec.ContainerType.BaseType, "Finalize",
                                        MemberTypes.Method, Expression.AllBindingFlags, Location);
 
                                if (member_lookup != null){
@@ -3149,7 +3421,17 @@ namespace Mono.CSharp {
                                        }
                                }
                        }
-                       
+
+                       FieldAttributes fa = Modifiers.FieldAttr (ModFlags);
+
+                       if (parent is Struct && 
+                           ((fa & FieldAttributes.Static) == 0) &&
+                           t == parent.TypeBuilder &&
+                           !TypeManager.IsBuiltinType (t)){
+                               Report.Error (523, Location, "Struct member `" + parent.Name + "." + Name + 
+                                             "' causes a cycle in the structure layout");
+                               return false;
+                       }
                        FieldBuilder = parent.TypeBuilder.DefineField (
                                Name, t, Modifiers.FieldAttr (ModFlags));
 
@@ -3223,7 +3505,24 @@ namespace Mono.CSharp {
                        if (!DoDefineParameters (parent))
                                return false;
 
-                       MethodSignature ms = new MethodSignature (Name, null, ParameterTypes);
+                       if (IsExplicitImpl)
+                               return true;
+
+                       string report_name;
+                       MethodSignature ms, base_ms;
+                       if (this is Indexer) {
+                               string name, base_name;
+
+                               report_name = "this";
+                               name = TypeManager.IndexerPropertyName (parent.TypeBuilder);
+                               ms = new MethodSignature (name, null, ParameterTypes);
+                               base_name = TypeManager.IndexerPropertyName (parent.TypeBuilder.BaseType);
+                               base_ms = new MethodSignature (base_name, null, ParameterTypes);
+                       } else {
+                               report_name = Name;
+                               ms = base_ms = new MethodSignature (Name, null, ParameterTypes);
+                       }
+
                        MemberList props_this;
 
                        props_this = TypeContainer.FindMembers (
@@ -3235,7 +3534,7 @@ namespace Mono.CSharp {
 
                        if (props_this.Count > 0) {
                                Report.Error (111, Location, "Class `" + parent.Name + "' " +
-                                             "already defines a member called `" + Name + "' " +
+                                             "already defines a member called `" + report_name + "' " +
                                              "with the same parameter types");
                                return false;
                        }
@@ -3247,13 +3546,13 @@ namespace Mono.CSharp {
                        MemberList props_static = TypeContainer.FindMembers (
                                parent.TypeBuilder.BaseType, MemberTypes.Property,
                                BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static,
-                               MethodSignature.inheritable_property_signature_filter, ms);
+                               MethodSignature.inheritable_property_signature_filter, base_ms);
 
                        MemberList props_instance = TypeContainer.FindMembers (
                                parent.TypeBuilder.BaseType, MemberTypes.Property,
                                BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
                                MethodSignature.inheritable_property_signature_filter,
-                               ms);
+                               base_ms);
 
                        //
                        // Find if we have anything
@@ -3277,7 +3576,7 @@ namespace Mono.CSharp {
                                        inherited_set : inherited_get;
                                
                                if (reference != null) {
-                                       string name = reference.DeclaringType.Name + "." + Name;
+                                       string name = reference.DeclaringType.Name + "." + report_name;
 
                                        if (!CheckMethodAgainstBase (parent, flags, reference, name))
                                                return false;
@@ -3647,7 +3946,8 @@ namespace Mono.CSharp {
                                        
                                if (Add == null && Remove == null) {
                                        FieldBuilder = parent.TypeBuilder.DefineField (
-                                               Name, MemberType, FieldAttributes.FamANDAssem);
+                                               Name, MemberType,
+                                               FieldAttributes.FamANDAssem | ((ModFlags & Modifiers.STATIC) != 0 ? FieldAttributes.Static : 0));
                                        TypeManager.RegisterPrivateFieldOfEvent (
                                                (EventInfo) EventBuilder, FieldBuilder);
                                        TypeManager.RegisterFieldBase (FieldBuilder, this);
@@ -4030,7 +4330,7 @@ namespace Mono.CSharp {
 
                        Type [] param_types = OperatorMethod.ParameterTypes;
                        Type declaring_type = OperatorMethodBuilder.DeclaringType;
-                       Type return_type = OperatorMethod.GetReturnType (parent);
+                       Type return_type = OperatorMethod.GetReturnType ();
                        Type first_arg_type = param_types [0];
 
                        // Rules for conversion operators
@@ -4140,6 +4440,84 @@ namespace Mono.CSharp {
                        OperatorMethod.Block = Block;
                        OperatorMethod.Emit (parent);
                }
+
+               public static string GetName (OpType ot)
+               {
+                       switch (ot){
+                       case OpType.LogicalNot:
+                               return "!";
+                       case OpType.OnesComplement:
+                               return "~";
+                       case OpType.Increment:
+                               return "++";
+                       case OpType.Decrement:
+                               return "--";
+                       case OpType.True:
+                               return "true";
+                       case OpType.False:
+                               return "false";
+                       case OpType.Addition:
+                               return "+";
+                       case OpType.Subtraction:
+                               return "-";
+                       case OpType.UnaryPlus:
+                               return "+";
+                       case OpType.UnaryNegation:
+                               return "-";
+                       case OpType.Multiply:
+                               return "*";
+                       case OpType.Division:
+                               return "/";
+                       case OpType.Modulus:
+                               return "%";
+                       case OpType.BitwiseAnd:
+                               return "&";
+                       case OpType.BitwiseOr:
+                               return "|";
+                       case OpType.ExclusiveOr:
+                               return "^";
+                       case OpType.LeftShift:
+                               return "<<";
+                       case OpType.RightShift:
+                               return ">>";
+                       case OpType.Equality:
+                               return "==";
+                       case OpType.Inequality:
+                               return "!=";
+                       case OpType.GreaterThan:
+                               return ">";
+                       case OpType.LessThan:
+                               return "<";
+                       case OpType.GreaterThanOrEqual:
+                               return ">=";
+                       case OpType.LessThanOrEqual:
+                               return "<=";
+                       case OpType.Implicit:
+                               return "implicit";
+                       case OpType.Explicit:
+                               return "explicit";
+                       default: return "";
+                       }
+               }
+               
+               public override string ToString ()
+               {
+                       Type return_type = OperatorMethod.GetReturnType();
+                       Type [] param_types = OperatorMethod.ParameterTypes;
+                       
+                       if (SecondArgType == null)
+                               return String.Format (
+                                       "{0} operator {1}({2})",
+                                       TypeManager.CSharpName (return_type),
+                                       GetName (OperatorType),
+                                       param_types [0]);
+                       else
+                               return String.Format (
+                                       "{0} operator {1}({2}, {3})",
+                                       TypeManager.CSharpName (return_type),
+                                       GetName (OperatorType),
+                                       param_types [0], param_types [1]);
+               }
        }
 
        //