2002-01-24 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / mcs / ecore.cs
index da7a9c90e5251b6bbcbab63ae76a1b63672736c7..51232241232242ab852511e7727722375d795a52 100755 (executable)
@@ -175,7 +175,12 @@ namespace Mono.CSharp {
                /// </remarks>
                public Expression ResolveWithSimpleName (EmitContext ec)
                {
-                       Expression e = DoResolve (ec);
+                       Expression e;
+
+                       if (this is SimpleName)
+                               e = ((SimpleName) this).DoResolveAllowStatic (ec);
+                       else 
+                               e = DoResolve (ec);
 
                        if (e != null){
                                if (e is SimpleName)
@@ -302,7 +307,7 @@ namespace Mono.CSharp {
                /// <summary>
                ///   Returns a fully formed expression after a MemberLookup
                /// </summary>
-               static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
+               public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
                {
                        if (mi is EventInfo)
                                return new EventExpr ((EventInfo) mi, loc);
@@ -317,13 +322,47 @@ namespace Mono.CSharp {
                        return null;
                }
 
+               //
+               // Returns whether the array of memberinfos contains the given method
+               //
+               static bool ArrayContainsMethod (MemberInfo [] array, MethodBase new_method)
+               {
+                       Type [] new_args = TypeManager.GetArgumentTypes (new_method);
+
+                       foreach (MethodBase method in array){
+                               if (method.Name != new_method.Name)
+                                       continue;
+                               
+                               Type [] old_args = TypeManager.GetArgumentTypes (method);
+                               int old_count = old_args.Length;
+                               int i;
+
+                               if (new_args.Length != old_count)
+                                       continue;
+                               
+                               for (i = 0; i < old_count; i++){
+                                       if (old_args [i] != new_args [i])
+                                               break;
+                               }
+                               if (i != old_count)
+                                       continue;
+
+                               if (!(method is MethodInfo && new_method is MethodInfo))
+                                       return true;
+                               
+                               if (((MethodInfo) method).ReturnType == ((MethodInfo) new_method).ReturnType)
+                                       return true;
+                       }
+                       return false;
+               }
+               
                //
                // We copy methods from `new_members' into `target_list' if the signature
                // for the method from in the new list does not exist in the target_list
                //
                // The name is assumed to be the same.
                //
-               static ArrayList CopyNewMethods (ArrayList target_list, MemberInfo [] new_members)
+               public static ArrayList CopyNewMethods (ArrayList target_list, MemberInfo [] new_members)
                {
                        if (target_list == null){
                                target_list = new ArrayList ();
@@ -337,25 +376,9 @@ namespace Mono.CSharp {
                        
                        foreach (MemberInfo mi in new_members){
                                MethodBase new_method = (MethodBase) mi;
-                               Type [] new_args = TypeManager.GetArgumentTypes (new_method);
-                                       
-                               foreach (MethodBase method in target_array){
-                                       Type [] old_args = TypeManager.GetArgumentTypes (method);
-                                       int new_count = new_args.Length;
-                                       int old_count = old_args.Length;
-                                       
-                                       if (new_count != old_count){
-                                               target_list.Add (method);
-                                               continue;
-                                       }
 
-                                       for (int i = 0; i < old_count; i++){
-                                               if (old_args [i] == new_args [i])
-                                                       continue;
-                                               target_list.Add (method);
-                                               break;
-                                       }
-                               }
+                               if (!ArrayContainsMethod (target_array, new_method))
+                                       target_list.Add (new_method);
                        }
                        return target_list;
                }
@@ -363,9 +386,6 @@ namespace Mono.CSharp {
                //
                // FIXME: Probably implement a cache for (t,name,current_access_set)?
                //
-               // FIXME: We need to cope with access permissions here, or this wont
-               // work!
-               //
                // This code could use some optimizations, but we need to do some
                // measurements.  For example, we could use a delegate to `flag' when
                // something can not any longer be a method-group (because it is something
@@ -390,12 +410,16 @@ namespace Mono.CSharp {
                //
                // FIXME: Potential optimization, have a static ArrayList
                //
+
                public static Expression MemberLookup (EmitContext ec, Type t, string name,
-                                                      bool same_type, MemberTypes mt,
-                                                      BindingFlags bf, Location loc)
+                                                      MemberTypes mt, BindingFlags bf, Location loc)
                {
-                       if (same_type)
-                               bf |= BindingFlags.NonPublic;
+                       Type source_type = ec.ContainerType;
+
+                       if (source_type != null){
+                               if (source_type == t || source_type.IsSubclassOf (t))
+                                       bf |= BindingFlags.NonPublic;
+                       }
 
                        //
                        // Lookup for members starting in the type requested and going
@@ -463,22 +487,58 @@ namespace Mono.CSharp {
                        MemberTypes.NestedType  |
                        MemberTypes.Property;
                
-               public const BindingFlags AllBindingsFlags =
+               public const BindingFlags AllBindingFlags =
                        BindingFlags.Public |
                        BindingFlags.Static |
                        BindingFlags.Instance;
 
-               public static Expression MemberLookup (EmitContext ec, Type t, string name,
-                                                      bool same_type, Location loc)
+               public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
                {
-                       return MemberLookup (ec, t, name, same_type, AllMemberTypes, AllBindingsFlags, loc);
+                       return MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
                }
 
+               /// <summary>
+               ///   This is a wrapper for MemberLookup that is not used to "probe", but
+               ///   to find a final definition.  If the final definition is not found, we
+               ///   look for private members and display a useful debugging message if we
+               ///   find it.
+               /// </summary>
+               public static Expression MemberLookupFinal (EmitContext ec, Type t, string name, 
+                                                           Location loc)
+               {
+                       Expression e;
+
+                       e = MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
+
+                       if (e != null)
+                               return e;
+                       
+                       e = MemberLookup (ec, t, name, AllMemberTypes,
+                                         AllBindingFlags | BindingFlags.NonPublic, loc);
+                       if (e == null){
+                               Report.Error (
+                                       117, loc, "`" + t + "' does not contain a definition " +
+                                       "for `" + name + "'");
+                       } else {
+                               Report.Error (
+                                       122, loc, "`" + t + "." + name +
+                                       "' is inaccessible due to its protection level");
+                       }
+                       
+                       return null;
+               }
+               
                static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
                {
                        Type expr_type = expr.Type;
 
                        if (target_type == TypeManager.object_type) {
+                               //
+                               // A pointer type cannot be converted to object
+                               // 
+                               if (expr_type.IsPointer)
+                                       return null;
+                               
                                if (expr_type.IsClass)
                                        return new EmptyCast (expr, target_type);
                                if (expr_type.IsValueType)
@@ -486,6 +546,10 @@ namespace Mono.CSharp {
                        } else if (expr_type.IsSubclassOf (target_type)) {
                                return new EmptyCast (expr, target_type);
                        } else {
+                               // from the null type to any reference-type.
+                               if (expr is NullLiteral && !target_type.IsValueType)
+                                       return new EmptyCast (expr, target_type);
+
                                // from any class-type S to any interface-type T.
                                if (expr_type.IsClass && target_type.IsInterface) {
                                        if (TypeManager.ImplementsInterface (expr_type, target_type))
@@ -532,10 +596,6 @@ namespace Mono.CSharp {
                                        if (target_type == TypeManager.icloneable_type)
                                                return new EmptyCast (expr, target_type);
                                
-                               // from the null type to any reference-type.
-                               if (expr is NullLiteral)
-                                       return new EmptyCast (expr, target_type);
-
                                return null;
 
                        }
@@ -1019,15 +1079,15 @@ namespace Mono.CSharp {
                        else
                                op_name = "op_Implicit";
                        
-                       mg1 = MemberLookup (ec, source_type, op_name, false, loc);
+                       mg1 = MemberLookup (ec, source_type, op_name, loc);
 
                        if (source_type.BaseType != null)
-                               mg2 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
+                               mg2 = MemberLookup (ec, source_type.BaseType, op_name, loc);
                        
-                       mg3 = MemberLookup (ec, target, op_name, false, loc);
+                       mg3 = MemberLookup (ec, target, op_name, loc);
 
                        if (target.BaseType != null)
-                               mg4 = MemberLookup (ec, target.BaseType, op_name, false, loc);
+                               mg4 = MemberLookup (ec, target.BaseType, op_name, loc);
 
                        MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
                        MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
@@ -1040,15 +1100,15 @@ namespace Mono.CSharp {
 
                                op_name = "op_Explicit";
                                
-                               mg5 = MemberLookup (ec, source_type, op_name, false, loc);
+                               mg5 = MemberLookup (ec, source_type, op_name, loc);
 
                                if (source_type.BaseType != null)
-                                       mg6 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
+                                       mg6 = MemberLookup (ec, source_type.BaseType, op_name, loc);
                                
-                               mg7 = MemberLookup (ec, target, op_name, false, loc);
+                               mg7 = MemberLookup (ec, target, op_name, loc);
                                
                                if (target.BaseType != null)
-                                       mg8 = MemberLookup (ec, target.BaseType, op_name, false, loc);
+                                       mg8 = MemberLookup (ec, target.BaseType, op_name, loc);
                                
                                MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
                                MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
@@ -1139,11 +1199,7 @@ namespace Mono.CSharp {
                        if (target_type == null)
                                throw new Exception ("Target type is null");
 
-                       e = ImplicitNumericConversion (ec, expr, target_type, loc);
-                       if (e != null)
-                               return e;
-
-                       e = ImplicitReferenceConversion (expr, target_type);
+                       e = ConvertImplicitStandard (ec, expr, target_type, loc);
                        if (e != null)
                                return e;
 
@@ -1151,13 +1207,6 @@ namespace Mono.CSharp {
                        if (e != null)
                                return e;
 
-                       if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
-                               IntLiteral i = (IntLiteral) expr;
-
-                               if (i.Value == 0)
-                                       return new EmptyCast (expr, target_type);
-                       }
-
                        return null;
                }
 
@@ -1195,6 +1244,19 @@ namespace Mono.CSharp {
                                if (i.Value == 0)
                                        return new EmptyCast (expr, target_type);
                        }
+
+                       if (ec.InUnsafe) {
+                               if (expr_type.IsPointer){
+                                       if (target_type == TypeManager.void_ptr_type)
+                                               return new EmptyCast (expr, target_type);
+                               }
+                               
+                               if (target_type.IsPointer){
+                                       if (expr is NullLiteral)
+                                               return new EmptyCast (expr, target_type);
+                               }
+                       }
+
                        return null;
                }
 
@@ -1234,6 +1296,9 @@ namespace Mono.CSharp {
                                if (value >= 0)
                                        return new ULongConstant ((ulong) value);
                        }
+                       
+                       if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
+                               return new EnumConstant (ic, target_type);
 
                        return null;
                }
@@ -1695,10 +1760,25 @@ namespace Mono.CSharp {
                        //
                        // Enum types
                        //
-                       if (expr is EnumConstant) {
-                               Expression e = ((EnumConstant) expr).Child;
+                       if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
+                               Expression e;
+
+                               //
+                               // FIXME: Is there any reason we should have EnumConstant
+                               // dealt with here instead of just using always the
+                               // UnderlyingSystemType to wrap the type?
+                               //
+                               if (expr is EnumConstant)
+                                       e = ((EnumConstant) expr).Child;
+                               else {
+                                       e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
+                               }
+                               
+                               e = ConvertImplicit (ec, e, target_type, loc);
+                               if (e != null)
+                                       return e;
                                
-                               return ConvertImplicit (ec, e, target_type, loc);
+                               return ConvertNumericExplicit (ec, e, target_type);
                        }
                        
                        ne = ConvertReferenceExplicit (expr, target_type);
@@ -1803,7 +1883,7 @@ namespace Mono.CSharp {
                        string s = "";
 
                        if (c.Type == target_type)
-                               return c;
+                               return ((Constant) c).GetValue ();
 
                        //
                        // Make into one of the literals we handle, we dont really care
@@ -1817,27 +1897,27 @@ namespace Mono.CSharp {
                                
                                if (target_type == TypeManager.uint32_type){
                                        if (v >= 0)
-                                               return (object) ((uint) v);
+                                               return (uint) v;
                                } else if (target_type == TypeManager.char_type){
                                        if (v >= Char.MinValue && v <= Char.MaxValue)
-                                               return (object) ((char) v);
+                                               return (char) v;
                                } else if (target_type == TypeManager.byte_type){
                                        if (v >= Byte.MinValue && v <= Byte.MaxValue)
-                                               return (object) ((byte) v);
+                                               return (byte) v;
                                } else if (target_type == TypeManager.sbyte_type){
                                        if (v >= SByte.MinValue && v <= SByte.MaxValue)
-                                               return (object) ((sbyte) v);
+                                               return (sbyte) v;
                                } else if (target_type == TypeManager.short_type){
                                        if (v >= Int16.MinValue && v <= UInt16.MaxValue)
-                                               return (object) ((short) v);
+                                               return (short) v;
                                } else if (target_type == TypeManager.ushort_type){
                                        if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
-                                               return (object) ((ushort) v);
+                                               return (ushort) v;
                                } else if (target_type == TypeManager.int64_type)
-                                       return (object) ((long) v);
+                                       return (long) v;
                                else if (target_type == TypeManager.uint64_type){
                                        if (v > 0)
-                                               return (object) ((ulong) v);
+                                               return (ulong) v;
                                }
 
                                s = v.ToString ();
@@ -1846,95 +1926,208 @@ namespace Mono.CSharp {
 
                                if (target_type == TypeManager.int32_type){
                                        if (v <= Int32.MaxValue)
-                                               return (object) ((int) v);
+                                               return (int) v;
                                } else if (target_type == TypeManager.char_type){
                                        if (v >= Char.MinValue && v <= Char.MaxValue)
-                                               return (object) ((char) v);
+                                               return (char) v;
                                } else if (target_type == TypeManager.byte_type){
                                        if (v <= Byte.MaxValue)
-                                               return (object) ((byte) v);
+                                               return (byte) v;
                                } else if (target_type == TypeManager.sbyte_type){
                                        if (v <= SByte.MaxValue)
-                                               return (object) ((sbyte) v);
+                                               return (sbyte) v;
                                } else if (target_type == TypeManager.short_type){
                                        if (v <= UInt16.MaxValue)
-                                               return (object) ((short) v);
+                                               return (short) v;
                                } else if (target_type == TypeManager.ushort_type){
                                        if (v <= UInt16.MaxValue)
-                                               return (object) ((ushort) v);
+                                               return (ushort) v;
                                } else if (target_type == TypeManager.int64_type)
-                                       return (object) ((long) v);
+                                       return (long) v;
                                else if (target_type == TypeManager.uint64_type)
-                                       return (object) ((ulong) v);
+                                       return (ulong) v;
                                s = v.ToString ();
-                       } else if (c is LongLiteral){ 
-                               long v = ((LongLiteral) c).Value;
+                       } else if (c is LongConstant){ 
+                               long v = ((LongConstant) c).Value;
 
                                if (target_type == TypeManager.int32_type){
                                        if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
-                                               return (object) ((int) v);
+                                               return (int) v;
                                } else if (target_type == TypeManager.uint32_type){
                                        if (v >= 0 && v <= UInt32.MaxValue)
-                                               return (object) ((uint) v);
+                                               return (uint) v;
                                } else if (target_type == TypeManager.char_type){
                                        if (v >= Char.MinValue && v <= Char.MaxValue)
-                                               return (object) ((char) v);
+                                               return (char) v;
                                } else if (target_type == TypeManager.byte_type){
                                        if (v >= Byte.MinValue && v <= Byte.MaxValue)
-                                               return (object) ((byte) v);
+                                               return (byte) v;
                                } else if (target_type == TypeManager.sbyte_type){
                                        if (v >= SByte.MinValue && v <= SByte.MaxValue)
-                                               return (object) ((sbyte) v);
+                                               return (sbyte) v;
                                } else if (target_type == TypeManager.short_type){
                                        if (v >= Int16.MinValue && v <= UInt16.MaxValue)
-                                               return (object) ((short) v);
+                                               return (short) v;
                                } else if (target_type == TypeManager.ushort_type){
                                        if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
-                                               return (object) ((ushort) v);
+                                               return (ushort) v;
                                } else if (target_type == TypeManager.uint64_type){
                                        if (v > 0)
-                                               return (object) ((ulong) v);
+                                               return (ulong) v;
                                }
                                s = v.ToString ();
-                       } else if (c is ULongLiteral){
-                               ulong v = ((ULongLiteral) c).Value;
+                       } else if (c is ULongConstant){
+                               ulong v = ((ULongConstant) c).Value;
 
                                if (target_type == TypeManager.int32_type){
                                        if (v <= Int32.MaxValue)
-                                               return (object) ((int) v);
+                                               return (int) v;
                                } else if (target_type == TypeManager.uint32_type){
                                        if (v <= UInt32.MaxValue)
-                                               return (object) ((uint) v);
+                                               return (uint) v;
                                } else if (target_type == TypeManager.char_type){
                                        if (v >= Char.MinValue && v <= Char.MaxValue)
-                                               return (object) ((char) v);
+                                               return (char) v;
                                } else if (target_type == TypeManager.byte_type){
                                        if (v >= Byte.MinValue && v <= Byte.MaxValue)
-                                               return (object) ((byte) v);
+                                               return (byte) v;
                                } else if (target_type == TypeManager.sbyte_type){
                                        if (v <= (int) SByte.MaxValue)
-                                               return (object) ((sbyte) v);
+                                               return (sbyte) v;
                                } else if (target_type == TypeManager.short_type){
                                        if (v <= UInt16.MaxValue)
-                                               return (object) ((short) v);
+                                               return (short) v;
                                } else if (target_type == TypeManager.ushort_type){
                                        if (v <= UInt16.MaxValue)
-                                               return (object) ((ushort) v);
+                                               return (ushort) v;
                                } else if (target_type == TypeManager.int64_type){
                                        if (v <= Int64.MaxValue)
-                                               return (object) ((long) v);
+                                               return (long) v;
                                }
                                s = v.ToString ();
                        } else if (c is ByteConstant){
-                               throw new Exception ("Implement me");
+                               byte v = ((ByteConstant) c).Value;
+                               
+                               if (target_type == TypeManager.int32_type)
+                                       return (int) v;
+                               else if (target_type == TypeManager.uint32_type)
+                                       return (uint) v;
+                               else if (target_type == TypeManager.char_type)
+                                       return (char) v;
+                               else if (target_type == TypeManager.sbyte_type){
+                                       if (v <= SByte.MaxValue)
+                                               return (sbyte) v;
+                               } else if (target_type == TypeManager.short_type)
+                                       return (short) v;
+                               else if (target_type == TypeManager.ushort_type)
+                                       return (ushort) v;
+                               else if (target_type == TypeManager.int64_type)
+                                       return (long) v;
+                               else if (target_type == TypeManager.uint64_type)
+                                       return (ulong) v;
+                               s = v.ToString ();
                        } else if (c is SByteConstant){
-                               throw new Exception ("Implement me");
+                               sbyte v = ((SByteConstant) c).Value;
+                               
+                               if (target_type == TypeManager.int32_type)
+                                       return (int) v;
+                               else if (target_type == TypeManager.uint32_type){
+                                       if (v >= 0)
+                                               return (uint) v;
+                               } else if (target_type == TypeManager.char_type){
+                                       if (v >= 0)
+                                               return (char) v;
+                               } else if (target_type == TypeManager.byte_type){
+                                       if (v >= 0)
+                                               return (byte) v;
+                               } else if (target_type == TypeManager.short_type)
+                                       return (short) v;
+                               else if (target_type == TypeManager.ushort_type){
+                                       if (v >= 0)
+                                               return (ushort) v;
+                               } else if (target_type == TypeManager.int64_type)
+                                       return (long) v;
+                               else if (target_type == TypeManager.uint64_type){
+                                       if (v >= 0)
+                                               return (ulong) v;
+                               }
+                               s = v.ToString ();
                        } else if (c is ShortConstant){
-                               throw new Exception ("Implement me");
+                               short v = ((ShortConstant) c).Value;
+                               
+                               if (target_type == TypeManager.int32_type){
+                                       return (int) v;
+                               } else if (target_type == TypeManager.uint32_type){
+                                       if (v >= 0)
+                                               return (uint) v;
+                               } else if (target_type == TypeManager.char_type){
+                                       if (v >= 0)
+                                               return (char) v;
+                               } else if (target_type == TypeManager.byte_type){
+                                       if (v >= Byte.MinValue && v <= Byte.MaxValue)
+                                               return (byte) v;
+                               } else if (target_type == TypeManager.sbyte_type){
+                                       if (v >= SByte.MinValue && v <= SByte.MaxValue)
+                                               return (sbyte) v;
+                               } else if (target_type == TypeManager.ushort_type){
+                                       if (v >= 0)
+                                               return (ushort) v;
+                               } else if (target_type == TypeManager.int64_type)
+                                       return (long) v;
+                               else if (target_type == TypeManager.uint64_type)
+                                       return (ulong) v;
+
+                               s = v.ToString ();
                        } else if (c is UShortConstant){
-                               throw new Exception ("Implement me");
+                               ushort v = ((UShortConstant) c).Value;
+                               
+                               if (target_type == TypeManager.int32_type)
+                                       return (int) v;
+                               else if (target_type == TypeManager.uint32_type)
+                                       return (uint) v;
+                               else if (target_type == TypeManager.char_type){
+                                       if (v >= Char.MinValue && v <= Char.MaxValue)
+                                               return (char) v;
+                               } else if (target_type == TypeManager.byte_type){
+                                       if (v >= Byte.MinValue && v <= Byte.MaxValue)
+                                               return (byte) v;
+                               } else if (target_type == TypeManager.sbyte_type){
+                                       if (v <= SByte.MaxValue)
+                                               return (byte) v;
+                               } else if (target_type == TypeManager.short_type){
+                                       if (v <= Int16.MaxValue)
+                                               return (short) v;
+                               } else if (target_type == TypeManager.int64_type)
+                                       return (long) v;
+                               else if (target_type == TypeManager.uint64_type)
+                                       return (ulong) v;
+
+                               s = v.ToString ();
+                       } else if (c is CharConstant){
+                               char v = ((CharConstant) c).Value;
+                               
+                               if (target_type == TypeManager.int32_type)
+                                       return (int) v;
+                               else if (target_type == TypeManager.uint32_type)
+                                       return (uint) v;
+                               else if (target_type == TypeManager.byte_type){
+                                       if (v >= Byte.MinValue && v <= Byte.MaxValue)
+                                               return (byte) v;
+                               } else if (target_type == TypeManager.sbyte_type){
+                                       if (v <= SByte.MaxValue)
+                                               return (sbyte) v;
+                               } else if (target_type == TypeManager.short_type){
+                                       if (v <= Int16.MaxValue)
+                                               return (short) v;
+                               } else if (target_type == TypeManager.ushort_type)
+                                       return (short) v;
+                               else if (target_type == TypeManager.int64_type)
+                                       return (long) v;
+                               else if (target_type == TypeManager.uint64_type)
+                                       return (ulong) v;
+
+                               s = v.ToString ();
                        }
-                       
                        error31 (loc, s, target_type);
                        return null;
                }               
@@ -1993,7 +2186,6 @@ namespace Mono.CSharp {
                {
                        child.Emit (ec);
                }
-
        }
 
        /// <summary>
@@ -2034,7 +2226,7 @@ namespace Mono.CSharp {
                //
                public Constant WidenToCompilerConstant ()
                {
-                       Type t = Child.Type.UnderlyingSystemType;
+                       Type t = TypeManager.EnumToUnderlying (Child.Type);
                        object v = ((Constant) Child).GetValue ();;
                        
                        if (t == TypeManager.int32_type)
@@ -2062,7 +2254,7 @@ namespace Mono.CSharp {
                //
                public object GetPlainValue ()
                {
-                       Type t = Child.Type.UnderlyingSystemType;
+                       Type t = TypeManager.EnumToUnderlying (Child.Type);
                        object v = ((Constant) Child).GetValue ();;
                        
                        if (t == TypeManager.int32_type)
@@ -2144,6 +2336,8 @@ namespace Mono.CSharp {
                        //
                        // Load the object from the pointer
                        //
+               basic_type:
+                       
                        if (t == TypeManager.int32_type)
                                ig.Emit (OpCodes.Ldind_I4);
                        else if (t == TypeManager.uint32_type)
@@ -2170,7 +2364,10 @@ namespace Mono.CSharp {
                                ig.Emit (OpCodes.Ldind_I1);
                        else if (t == TypeManager.intptr_type)
                                ig.Emit (OpCodes.Ldind_I);
-                       else 
+                       else if (TypeManager.IsEnumType (t)){
+                               t = TypeManager.EnumToUnderlying (t);
+                               goto basic_type;
+                       } else
                                ig.Emit (OpCodes.Ldobj, t);
                }
        }
@@ -2524,18 +2721,44 @@ namespace Mono.CSharp {
                                        Error120 (Location, Name);
                                        return null;
                                }
+                       } else if (e is EventExpr) {
+                               if (!((EventExpr) e).IsStatic) {
+                                       Error120 (Location, Name);
+                                       return null;
+                               }
                        }
 
                        return e;
                }
                
-               // <remarks>
-               //   7.5.2: Simple Names. 
-               //
-               //   Local Variables and Parameters are handled at
-               //   parse time, so they never occur as SimpleNames.
-               // </remarks>
                public override Expression DoResolve (EmitContext ec)
+               {
+                       return SimpleNameResolve (ec, false);
+               }
+
+               public Expression DoResolveAllowStatic (EmitContext ec)
+               {
+                       return SimpleNameResolve (ec, true);
+               }
+
+               /// <remarks>
+               ///   7.5.2: Simple Names. 
+               ///
+               ///   Local Variables and Parameters are handled at
+               ///   parse time, so they never occur as SimpleNames.
+               ///
+               ///   The `allow_static' flag is used by MemberAccess only
+               ///   and it is used to inform us that it is ok for us to 
+               ///   avoid the static check, because MemberAccess might end
+               ///   up resolving the Name as a Type name and the access as
+               ///   a static type access.
+               ///
+               ///   ie: Type Type; .... { Type.GetType (""); }
+               ///
+               ///   Type is both an instance variable and a Type;  Type.GetType
+               ///   is the static method not an instance method of type.
+               /// </remarks>
+               Expression SimpleNameResolve (EmitContext ec, bool allow_static)
                {
                        Expression e;
 
@@ -2544,58 +2767,66 @@ namespace Mono.CSharp {
                        //
 
                        //
-                       // Stage 2: Lookup members
+                       // Stage 2: Lookup members 
                        //
-                       e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, true, Location);
-                       if (e == null) {
+                       e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, Location);
+                       if (e == null){
                                //
                                // Stage 3: Lookup symbol in the various namespaces. 
-                               // 
+                               //
                                DeclSpace ds = ec.TypeContainer;
                                Type t;
                                string alias_value;
-
+                               
                                if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
                                        return new TypeExpr (t);
-
+                               
                                //
-                               // Stage 3 part b: Lookup up if we are an alias to a type
+                               // Stage 2 part b: Lookup up if we are an alias to a type
                                // or a namespace.
                                //
                                // Since we are cheating: we only do the Alias lookup for
                                // namespaces if the name does not include any dots in it
                                //
-
+                               
                                if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
-                                       // System.Console.WriteLine (Name + " --> " + alias_value);
+                               // System.Console.WriteLine (Name + " --> " + alias_value);
                                        if ((t = RootContext.LookupType (ds, alias_value, true, Location))
                                            != null)
                                                return new TypeExpr (t);
-
-                                       // we have alias value, but it isn't Type, so try if it's namespace
+                                       
+                               // we have alias value, but it isn't Type, so try if it's namespace
                                        return new SimpleName (alias_value, Location);
                                }
-
+                               
                                // No match, maybe our parent can compose us
                                // into something meaningful.
-                               //
                                return this;
                        }
-
-                       // Step 2, continues here.
+                       
+                       //
+                       // Stage 2 continues here. 
+                       // 
                        if (e is TypeExpr)
                                return e;
 
                        if (e is FieldExpr){
                                FieldExpr fe = (FieldExpr) e;
-                               
-                               if (!fe.FieldInfo.IsStatic){
-                                       This t = new This (Location.Null);
+                               FieldInfo fi = fe.FieldInfo;
+
+                               if (ec.IsStatic){
+                                       if (!allow_static && !fi.IsStatic){
+                                               Error120 (Location, Name);
+                                               return null;
+                                       }
+                               } else {
+                                       // If we are not in static code and this
+                                       // field is not static, set the instance to `this'.
 
-                                       fe.InstanceExpression = t.DoResolve (ec);
+                                       if (!fi.IsStatic)
+                                               fe.InstanceExpression = ec.This;
                                }
 
-                               FieldInfo fi = fe.FieldInfo;
                                
                                if (fi is FieldBuilder) {
                                        Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
@@ -2606,11 +2837,66 @@ namespace Mono.CSharp {
                                                return Constantify (real_value, fi.FieldType);
                                        }
                                }
+
+                               return e;
                        }                               
 
-                       if (ec.IsStatic)
+                       if (e is EventExpr) {
+                               //
+                               // If the event is local to this class, we transform ourselves into
+                               // a FieldExpr
+                               //
+                               EventExpr ee = (EventExpr) e;
+
+                               Expression ml = MemberLookup (
+                                       ec, ec.TypeContainer.TypeBuilder, ee.EventInfo.Name,
+                                       MemberTypes.Event, AllBindingFlags, Location);
+
+                               if (ml != null) {
+                                       MemberInfo mi = ec.TypeContainer.GetFieldFromEvent ((EventExpr) ml);
+
+                                       if (mi == null) {
+                                               //
+                                               // If this happens, then we have an event with its own
+                                               // accessors and private field etc so there's no need
+                                               // to transform ourselves : we should instead flag an error
+                                               //
+                                               Assign.error70 (ee.EventInfo, Location);
+                                               return null;
+                                       }
+
+                                       ml = ExprClassFromMemberInfo (ec, mi, Location);
+                                       
+                                       if (ml == null) {
+                                               Report.Error (-200, Location, "Internal error!!");
+                                               return null;
+                                       }
+
+                                       Expression instance_expr;
+                                       
+                                       FieldInfo fi = ((FieldExpr) ml).FieldInfo;
+
+                                       if (fi.IsStatic)
+                                               instance_expr = null;
+                                       else
+                                               instance_expr = ec.This;
+
+                                       instance_expr = instance_expr.Resolve (ec);
+
+                                       if (instance_expr != null)
+                                               instance_expr = instance_expr.Resolve (ec);
+                                       
+                                       return MemberAccess.ResolveMemberAccess (ec, ml, instance_expr, Location, null);
+                               }
+                       }
+                               
+                       
+                       if (ec.IsStatic){
+                               if (allow_static)
+                                       return e;
+
                                return MemberStaticCheck (e);
-                       else
+                       else
                                return e;
                }
 
@@ -2764,7 +3050,6 @@ namespace Mono.CSharp {
                                InstanceExpression = InstanceExpression.Resolve (ec);
                                if (InstanceExpression == null)
                                        return null;
-                               
                        }
 
                        return this;
@@ -2797,11 +3082,42 @@ namespace Mono.CSharp {
                override public void Emit (EmitContext ec)
                {
                        ILGenerator ig = ec.ig;
-
-                       if (FieldInfo.IsStatic)
+                       bool is_volatile = false;
+                               
+                       if (FieldInfo is FieldBuilder){
+                               Field f = TypeManager.GetField (FieldInfo);
+                               if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
+                                       is_volatile = true;
+                               
+                               f.status |= Field.Status.USED;
+                       }
+                       
+                       if (FieldInfo.IsStatic){
+                               if (is_volatile)
+                                       ig.Emit (OpCodes.Volatile);
+                               
                                ig.Emit (OpCodes.Ldsfld, FieldInfo);
-                       else {
-                               InstanceExpression.Emit (ec);
+                       } else {
+                               if (InstanceExpression.Type.IsValueType){
+                                       IMemoryLocation ml;
+                                       LocalTemporary tempo = null;
+                                       
+                                       if (!(InstanceExpression is IMemoryLocation)){
+                                               tempo = new LocalTemporary (
+                                                       ec, InstanceExpression.Type);
+
+                                               InstanceExpression.Emit (ec);
+                                               tempo.Store (ec);
+                                               ml = tempo;
+                                       } else
+                                               ml = (IMemoryLocation) InstanceExpression;
+
+                                       ml.AddressOf (ec);
+                               } else 
+                                       InstanceExpression.Emit (ec);
+
+                               if (is_volatile)
+                                       ig.Emit (OpCodes.Volatile);
                                
                                ig.Emit (OpCodes.Ldfld, FieldInfo);
                        }
@@ -2810,7 +3126,8 @@ namespace Mono.CSharp {
                public void EmitAssign (EmitContext ec, Expression source)
                {
                        bool is_static = FieldInfo.IsStatic;
-
+                       ILGenerator ig = ec.ig;
+                       
                        if (!is_static){
                                Expression instance = InstanceExpression;
 
@@ -2820,28 +3137,79 @@ namespace Mono.CSharp {
 
                                                ml.AddressOf (ec);
                                        } else
-                                               throw new Exception ("The " + instance + " of type " + Type+
-                                                                    "represents a ValueType and does not " +
-                                                                    "implement IMemoryLocation");
+                                               throw new Exception ("The " + instance + " of type " +
+                                                                    instance.Type +
+                                                                    " represents a ValueType and does " +
+                                                                    "not implement IMemoryLocation");
                                } else
                                        instance.Emit (ec);
                        }
                        source.Emit (ec);
+
+                       if (FieldInfo is FieldBuilder){
+                               Field f = TypeManager.GetField (FieldInfo);
+                               if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
+                                       ig.Emit (OpCodes.Volatile);
+                       }
                        
                        if (is_static)
-                               ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
-                       else {
-                               ec.ig.Emit (OpCodes.Stfld, FieldInfo);
+                               ig.Emit (OpCodes.Stsfld, FieldInfo);
+                       else 
+                               ig.Emit (OpCodes.Stfld, FieldInfo);
+
+                       if (FieldInfo is FieldBuilder){
+                               Field f = TypeManager.GetField (FieldInfo);
+
+                               f.status |= Field.Status.ASSIGNED;
                        }
                }
                
                public void AddressOf (EmitContext ec)
                {
+                       ILGenerator ig = ec.ig;
+                       
+                       if (FieldInfo is FieldBuilder){
+                               Field f = TypeManager.GetField (FieldInfo);
+                               if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
+                                       ig.Emit (OpCodes.Volatile);
+                       }
+
+                       //
+                       // FIXME:
+                       //
+                       // Mhm.  We do not know what we are being used for:
+                       // READING or WRITING the field.
+                       //
+                       // I think we want an extra argument to AddressOf to pass
+                       // this semantic information.
+                       //
+                       // For now: just flag both assigned and used.
+                       //
+                       if (FieldInfo is FieldBuilder){
+                               Field f = TypeManager.GetField (FieldInfo);
+
+                               f.status |= Field.Status.ASSIGNED | Field.Status.USED;
+                       }
+
+                       //
+                       // Handle initonly fields specially: make a copy and then
+                       // get the address of the copy.
+                       //
+                       if (FieldInfo.IsInitOnly){
+                               LocalBuilder local;
+                               
+                               Emit (ec);
+                               local = ig.DeclareLocal (type);
+                               ig.Emit (OpCodes.Stloc, local);
+                               ig.Emit (OpCodes.Ldloca, local);
+                               return;
+                       } 
+
                        if (FieldInfo.IsStatic)
-                               ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
+                               ig.Emit (OpCodes.Ldsflda, FieldInfo);
                        else {
                                InstanceExpression.Emit (ec);
-                               ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
+                               ig.Emit (OpCodes.Ldflda, FieldInfo);
                        }
                }
        }
@@ -2856,6 +3224,7 @@ namespace Mono.CSharp {
        public class PropertyExpr : ExpressionStatement, IAssignMethod {
                public readonly PropertyInfo PropertyInfo;
                public readonly bool IsStatic;
+               public bool IsBase;
                MethodInfo [] Accessors;
                Location loc;
                
@@ -2923,7 +3292,7 @@ namespace Mono.CSharp {
 
                override public void Emit (EmitContext ec)
                {
-                       Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [0], null);
+                       Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, Accessors [0], null);
                        
                }
 
@@ -2936,7 +3305,7 @@ namespace Mono.CSharp {
                        ArrayList args = new ArrayList ();
 
                        args.Add (arg);
-                       Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [1], args);
+                       Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args);
                }
 
                override public void EmitStatement (EmitContext ec)
@@ -2947,7 +3316,7 @@ namespace Mono.CSharp {
        }
 
        /// <summary>
-       ///   Fully resolved expression that evaluates to a Expression
+       ///   Fully resolved expression that evaluates to an Event
        /// </summary>
        public class EventExpr : Expression {
                public readonly EventInfo EventInfo;
@@ -2955,6 +3324,8 @@ namespace Mono.CSharp {
                public Expression InstanceExpression;
 
                public readonly bool IsStatic;
+
+               MethodInfo add_accessor, remove_accessor;
                
                public EventExpr (EventInfo ei, Location loc)
                {
@@ -2962,31 +3333,44 @@ namespace Mono.CSharp {
                        this.loc = loc;
                        eclass = ExprClass.EventAccess;
 
-                       MethodInfo add_accessor = TypeManager.GetAddMethod (ei);
-                       MethodInfo remove_accessor = TypeManager.GetRemoveMethod (ei);
+                       add_accessor = TypeManager.GetAddMethod (ei);
+                       remove_accessor = TypeManager.GetRemoveMethod (ei);
                        
-                       if (add_accessor != null)
-                               if (add_accessor.IsStatic)
+                       if (add_accessor.IsStatic || remove_accessor.IsStatic)
                                        IsStatic = true;
 
-                       if (remove_accessor != null)
-                               if (remove_accessor.IsStatic)
-                                       IsStatic = true;
+                       if (EventInfo is MyEventBuilder)
+                               type = ((MyEventBuilder) EventInfo).EventType;
+                       else
+                               type = EventInfo.EventHandlerType;
                }
 
                override public Expression DoResolve (EmitContext ec)
                {
-                       // We are born in resolved state.
-
-                       Console.WriteLine ("Came here");
-                       type = EventInfo.EventHandlerType;
+                       // We are born fully resolved
                        return this;
                }
 
                override public void Emit (EmitContext ec)
                {
-                       throw new Exception ("Implement me");
-                       // FIXME: Implement.
+                       throw new Exception ("Should not happen I think");
+               }
+
+               public void EmitAddOrRemove (EmitContext ec, Expression source)
+               {
+                       Expression handler = ((Binary) source).Right;
+                       
+                       Argument arg = new Argument (handler, Argument.AType.Expression);
+                       ArrayList args = new ArrayList ();
+                               
+                       args.Add (arg);
+                       
+                       if (((Binary) source).Oper == Binary.Operator.Addition)
+                               Invocation.EmitCall (
+                                       ec, false, IsStatic, InstanceExpression, add_accessor, args);
+                       else
+                               Invocation.EmitCall (
+                                       ec, false, IsStatic, InstanceExpression, remove_accessor, args);
                }
        }
 }