2003-03-17 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / mcs / ecore.cs
index 30330750e4f4404fe4c44a23bcbc2127eb9d6fbe..8a3819a3ca7a6b9b4b40350f2dc64ddc252bc69e 100755 (executable)
@@ -36,23 +36,159 @@ namespace Mono.CSharp {
                Nothing, 
        }
 
+       /// <remarks>
+       ///   This is used to tell Resolve in which types of expressions we're
+       ///   interested.
+       /// </remarks>
+       [Flags]
+       public enum ResolveFlags {
+               // Returns Value, Variable, PropertyAccess, EventAccess or IndexerAccess.
+               VariableOrValue         = 1,
+
+               // Returns a type expression.
+               Type                    = 2,
+
+               // Returns a method group.
+               MethodGroup             = 4,
+
+               // Allows SimpleNames to be returned.
+               // This is used by MemberAccess to construct long names that can not be
+               // partially resolved (namespace-qualified names for example).
+               SimpleName              = 8,
+
+               // Mask of all the expression class flags.
+               MaskExprClass           = 15,
+
+               // Disable control flow analysis while resolving the expression.
+               // This is used when resolving the instance expression of a field expression.
+               DisableFlowAnalysis     = 16
+       }
+
+       //
+       // This is just as a hint to AddressOf of what will be done with the
+       // address.
+       [Flags]
+       public enum AddressOp {
+               Store = 1,
+               Load  = 2,
+               LoadStore = 3
+       };
+       
        /// <summary>
        ///   This interface is implemented by variables
        /// </summary>
        public interface IMemoryLocation {
                /// <summary>
                ///   The AddressOf method should generate code that loads
-               ///   the address of the object and leaves it on the stack
+               ///   the address of the object and leaves it on the stack.
+               ///
+               ///   The `mode' argument is used to notify the expression
+               ///   of whether this will be used to read from the address or
+               ///   write to the address.
+               ///
+               ///   This is just a hint that can be used to provide good error
+               ///   reporting, and should have no other side effects. 
+               /// </summary>
+               void AddressOf (EmitContext ec, AddressOp mode);
+       }
+
+       /// <summary>
+       ///   This interface is implemented by variables
+       /// </summary>
+       public interface IVariable {
+               /// <summary>
+               ///   Checks whether the variable has already been assigned at
+               ///   the current position of the method's control flow and
+               ///   reports an appropriate error message if not.
+               ///
+               ///   If the variable is a struct, then this call checks whether
+               ///   all of its fields (including all private ones) have been
+               ///   assigned.
+               /// </summary>
+               bool IsAssigned (EmitContext ec, Location loc);
+
+               /// <summary>
+               ///   Checks whether field `name' in this struct has been assigned.
+               /// </summary>
+               bool IsFieldAssigned (EmitContext ec, string name, Location loc);
+
+               /// <summary>
+               ///   Tells the flow analysis code that the variable has already
+               ///   been assigned at the current code position.
+               ///
+               ///   If the variable is a struct, this call marks all its fields
+               ///   (including private fields) as being assigned.
+               /// </summary>
+               void SetAssigned (EmitContext ec);
+
+               /// <summary>
+               ///   Tells the flow analysis code that field `name' in this struct
+               ///   has already been assigned atthe current code position.
+               /// </summary>
+               void SetFieldAssigned (EmitContext ec, string name);
+       }
+
+       /// <summary>
+       ///   This interface denotes an expression which evaluates to a member
+       ///   of a struct or a class.
+       /// </summary>
+       public interface IMemberExpr
+       {
+               /// <summary>
+               ///   The name of this member.
+               /// </summary>
+               string Name {
+                       get;
+               }
+
+               /// <summary>
+               ///   Whether this is an instance member.
+               /// </summary>
+               bool IsInstance {
+                       get;
+               }
+
+               /// <summary>
+               ///   Whether this is a static member.
+               /// </summary>
+               bool IsStatic {
+                       get;
+               }
+
+               /// <summary>
+               ///   The type which declares this member.
                /// </summary>
-               void AddressOf (EmitContext ec);
+               Type DeclaringType {
+                       get;
+               }
+
+               /// <summary>
+               ///   The instance expression associated with this member, if it's a
+               ///   non-static member.
+               /// </summary>
+               Expression InstanceExpression {
+                       get; set;
+               }
+       }
+
+       /// <summary>
+       ///   Expression which resolves to a type.
+       /// </summary>
+       public interface ITypeExpression
+       {
+               /// <summary>
+               ///   Resolve the expression, but only lookup types.
+               /// </summary>
+               Expression DoResolveType (EmitContext ec);
        }
 
        /// <remarks>
        ///   Base class for expressions
        /// </remarks>
        public abstract class Expression {
-               protected ExprClass eclass;
-               protected Type      type;
+               public ExprClass eclass;
+               protected Type type;
+               protected Location loc;
                
                public Type Type {
                        get {
@@ -64,38 +200,45 @@ namespace Mono.CSharp {
                        }
                }
 
-               public ExprClass ExprClass {
+               public Location Location {
                        get {
-                               return eclass;
-                       }
-
-                       set {
-                               eclass = value;
+                               return loc;
                        }
                }
 
                /// <summary>
                ///   Utility wrapper routine for Error, just to beautify the code
                /// </summary>
-               static protected void Error (int error, string s)
+               public void Error (int error, string s)
                {
-                       Report.Error (error, s);
+                       if (!Location.IsNull (loc))
+                               Report.Error (error, loc, s);
+                       else
+                               Report.Error (error, s);
                }
 
-               static protected void Error (int error, Location loc, string s)
+               /// <summary>
+               ///   Utility wrapper routine for Warning, just to beautify the code
+               /// </summary>
+               public void Warning (int warning, string s)
                {
-                       Report.Error (error, loc, s);
+                       if (!Location.IsNull (loc))
+                               Report.Warning (warning, loc, s);
+                       else
+                               Report.Warning (warning, s);
                }
-               
+
                /// <summary>
-               ///   Utility wrapper routine for Warning, just to beautify the code
+               ///   Utility wrapper routine for Warning, only prints the warning if
+               ///   warnings of level `level' are enabled.
                /// </summary>
-               static protected void Warning (int warning, string s)
+               public void Warning (int warning, int level, string s)
                {
-                       Report.Warning (warning, s);
+                       if (level <= RootContext.WarningLevel)
+                               Warning (warning, s);
                }
 
-               static public void error30 (Location loc, Type source, Type target)
+               static public void Error_CannotConvertType (Location loc, Type source, Type target)
                {
                        Report.Error (30, loc, "Cannot convert type '" +
                                      TypeManager.CSharpName (source) + "' to '" +
@@ -144,64 +287,102 @@ namespace Mono.CSharp {
                ///   Currently Resolve wraps DoResolve to perform sanity
                ///   checking and assertion checking on what we expect from Resolve.
                /// </remarks>
-               public Expression Resolve (EmitContext ec)
+               public Expression Resolve (EmitContext ec, ResolveFlags flags)
                {
-                       Expression e = DoResolve (ec);
+                       // Are we doing a types-only search ?
+                       if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type) {
+                               ITypeExpression type_expr = this as ITypeExpression;
 
-                       if (e != null){
-                               if (e is SimpleName){
-                                       SimpleName s = (SimpleName) e;
-                                       
-                                       Report.Error (
-                                               103, s.Location,
-                                               "The name `" + s.Name + "' could not be found in `" +
-                                               ec.TypeContainer.Name + "'");
+                               if (type_expr == null)
+                                       return null;
+
+                               return type_expr.DoResolveType (ec);
+                       }
+
+                       bool old_do_flow_analysis = ec.DoFlowAnalysis;
+                       if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
+                               ec.DoFlowAnalysis = false;
+
+                       Expression e;
+                       if (this is SimpleName)
+                               e = ((SimpleName) this).DoResolveAllowStatic (ec);
+                       else 
+                               e = DoResolve (ec);
+
+                       ec.DoFlowAnalysis = old_do_flow_analysis;
+
+                       if (e == null)
+                               return null;
+
+                       if (e is SimpleName){
+                               SimpleName s = (SimpleName) e;
+
+                               if ((flags & ResolveFlags.SimpleName) == 0) {
+                                       MemberLookupFailed (ec, null, ec.ContainerType, s.Name,
+                                                           ec.DeclSpace.Name, loc);
                                        return null;
                                }
-                               
-                               if (e.ExprClass == ExprClass.Invalid)
-                                       throw new Exception ("Expression " + e +
-                                                            " ExprClass is Invalid after resolve");
 
-                               if (e.ExprClass != ExprClass.MethodGroup)
-                                       if (e.type == null)
-                                               throw new Exception ("Expression " + e +
-                                                                    " did not set its type after Resolve");
+                               return s;
                        }
 
-                       return e;
-               }
+                       if ((e is TypeExpr) || (e is ComposedCast)) {
+                               if ((flags & ResolveFlags.Type) == 0) {
+                                       e.Error118 (flags);
+                                       return null;
+                               }
 
-               /// <summary>
-               ///   Performs expression resolution and semantic analysis, but
-               ///   allows SimpleNames to be returned.
-               /// </summary>
-               ///
-               /// <remarks>
-               ///   This is used by MemberAccess to construct long names that can not be
-               ///   partially resolved (namespace-qualified names for example).
-               /// </remarks>
-               public Expression ResolveWithSimpleName (EmitContext ec)
-               {
-                       Expression e = DoResolve (ec);
+                               return e;
+                       }
 
-                       if (e != null){
-                               if (e is SimpleName)
-                                       return e;
+                       switch (e.eclass) {
+                       case ExprClass.Type:
+                               if ((flags & ResolveFlags.VariableOrValue) == 0) {
+                                       e.Error118 (flags);
+                                       return null;
+                               }
+                               break;
 
-                               if (e.ExprClass == ExprClass.Invalid)
-                                       throw new Exception ("Expression " + e +
-                                                            " ExprClass is Invalid after resolve");
+                       case ExprClass.MethodGroup:
+                               if ((flags & ResolveFlags.MethodGroup) == 0) {
+                                       ((MethodGroupExpr) e).ReportUsageError ();
+                                       return null;
+                               }
+                               break;
+
+                       case ExprClass.Value:
+                       case ExprClass.Variable:
+                       case ExprClass.PropertyAccess:
+                       case ExprClass.EventAccess:
+                       case ExprClass.IndexerAccess:
+                               if ((flags & ResolveFlags.VariableOrValue) == 0) {
+                                       e.Error118 (flags);
+                                       return null;
+                               }
+                               break;
 
-                               if (e.ExprClass != ExprClass.MethodGroup)
-                                       if (e.type == null)
-                                               throw new Exception ("Expression " + e +
-                                                                    " did not set its type after Resolve");
+                       default:
+                               throw new Exception ("Expression " + e.GetType () +
+                                                    " ExprClass is Invalid after resolve");
                        }
 
+                       if (e.type == null)
+                               throw new Exception (
+                                       "Expression " + e.GetType () +
+                                       " did not set its type after Resolve\n" +
+                                       "called from: " + this.GetType ());
+
                        return e;
                }
-               
+
+               /// <summary>
+               ///   Resolves an expression and performs semantic analysis on it.
+               /// </summary>
+               public Expression Resolve (EmitContext ec)
+               {
+                       return Resolve (ec, ResolveFlags.VariableOrValue);
+               }
+
                /// <summary>
                ///   Resolves an expression for LValue assignment
                /// </summary>
@@ -217,22 +398,23 @@ namespace Mono.CSharp {
                        if (e != null){
                                if (e is SimpleName){
                                        SimpleName s = (SimpleName) e;
-                                       
-                                       Report.Error (
-                                               103, s.Location,
-                                               "The name `" + s.Name + "' could not be found in `" +
-                                               ec.TypeContainer.Name + "'");
+                                       MemberLookupFailed (ec, null, ec.ContainerType, s.Name,
+                                                           ec.DeclSpace.Name, loc);
                                        return null;
                                }
 
-                               if (e.ExprClass == ExprClass.Invalid)
+                               if (e.eclass == ExprClass.Invalid)
                                        throw new Exception ("Expression " + e +
                                                             " ExprClass is Invalid after resolve");
 
-                               if (e.ExprClass != ExprClass.MethodGroup)
-                                       if (e.type == null)
-                                               throw new Exception ("Expression " + e +
-                                                                    " did not set its type after Resolve");
+                               if (e.eclass == ExprClass.MethodGroup) {
+                                       ((MethodGroupExpr) e).ReportUsageError ();
+                                       return null;
+                               }
+
+                               if (e.type == null)
+                                       throw new Exception ("Expression " + e +
+                                                            " did not set its type after Resolve");
                        }
 
                        return e;
@@ -248,15 +430,6 @@ namespace Mono.CSharp {
                /// </remarks>
                public abstract void Emit (EmitContext ec);
 
-               /// <summary>
-               ///   This method should perform a reduction of the expression.  This should
-               ///   never return null.
-               /// </summary>
-               public virtual Expression Reduce (EmitContext ec)
-               {
-                       return this;
-               }
-
                /// <summary>
                ///   Protected constructor.  Only derivate types should
                ///   be able to be created
@@ -271,50 +444,67 @@ namespace Mono.CSharp {
                /// <summary>
                ///   Returns a literalized version of a literal FieldInfo
                /// </summary>
-               public static Expression Literalize (object v, Type t)
+               ///
+               /// <remarks>
+               ///   The possible return values are:
+               ///      IntConstant, UIntConstant
+               ///      LongLiteral, ULongConstant
+               ///      FloatConstant, DoubleConstant
+               ///      StringConstant
+               ///
+               ///   The value returned is already resolved.
+               /// </remarks>
+               public static Constant Constantify (object v, Type t)
                {
                        if (t == TypeManager.int32_type)
-                               return new IntLiteral ((int) v);
+                               return new IntConstant ((int) v);
                        else if (t == TypeManager.uint32_type)
-                               return new UIntLiteral ((uint) v);
+                               return new UIntConstant ((uint) v);
                        else if (t == TypeManager.int64_type)
-                               return new LongLiteral ((long) v);
+                               return new LongConstant ((long) v);
                        else if (t == TypeManager.uint64_type)
-                               return new ULongLiteral ((ulong) v);
+                               return new ULongConstant ((ulong) v);
                        else if (t == TypeManager.float_type)
-                               return new FloatLiteral ((float) v);
+                               return new FloatConstant ((float) v);
                        else if (t == TypeManager.double_type)
-                               return new DoubleLiteral ((double) v);
+                               return new DoubleConstant ((double) v);
                        else if (t == TypeManager.string_type)
-                               return new StringLiteral ((string) v);
+                               return new StringConstant ((string) v);
                        else if (t == TypeManager.short_type)
-                               return new IntLiteral ((int) ((short)v));
+                               return new ShortConstant ((short)v);
                        else if (t == TypeManager.ushort_type)
-                               return new IntLiteral ((int) ((ushort)v));
+                               return new UShortConstant ((ushort)v);
                        else if (t == TypeManager.sbyte_type)
-                               return new IntLiteral ((int) ((sbyte)v));
+                               return new SByteConstant (((sbyte)v));
                        else if (t == TypeManager.byte_type)
-                               return new IntLiteral ((int) ((byte)v));
+                               return new ByteConstant ((byte)v);
                        else if (t == TypeManager.char_type)
-                               return new IntLiteral ((int) ((char)v));
-                       else
-                               throw new Exception ("Unknown type for literal (" + t +
+                               return new CharConstant ((char)v);
+                       else if (t == TypeManager.bool_type)
+                               return new BoolConstant ((bool) v);
+                       else if (TypeManager.IsEnumType (t)){
+                               Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
+
+                               return new EnumConstant (e, t);
+                       } else
+                               throw new Exception ("Unknown type for constant (" + t +
                                                     "), details: " + v);
                }
 
                /// <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);
                        else if (mi is FieldInfo)
                                return new FieldExpr ((FieldInfo) mi, loc);
                        else if (mi is PropertyInfo)
-                               return new PropertyExpr ((PropertyInfo) mi, loc);
-                       else if (mi is Type)
-                               return new TypeExpr ((Type) mi);
+                               return new PropertyExpr (ec, (PropertyInfo) mi, loc);
+                       else if (mi is Type){
+                               return new TypeExpr ((System.Type) mi, loc);
+                       }
 
                        return null;
                }
@@ -322,9 +512,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
@@ -346,45 +533,41 @@ namespace Mono.CSharp {
                // This is so we can catch correctly attempts to invoke instance methods
                // from a static body (scan for error 120 in ResolveSimpleName).
                //
-               public static Expression MemberLookup (EmitContext ec, Type t, string name,
-                                                      bool same_type, MemberTypes mt,
-                                                      BindingFlags bf, Location loc)
+               //
+               // FIXME: Potential optimization, have a static ArrayList
+               //
+
+               public static Expression MemberLookup (EmitContext ec, Type queried_type, string name,
+                                                      MemberTypes mt, BindingFlags bf, Location loc)
                {
-                       if (same_type)
-                               bf |= BindingFlags.NonPublic;
+                       return MemberLookup (ec, ec.ContainerType, null, queried_type, name, mt, bf, loc);
+               }
+
+               //
+               // Lookup type `queried_type' for code in class `container_type' with a qualifier of
+               // `qualifier_type' or null to lookup members in the current class.
+               //
 
-                       MemberInfo [] mi = ec.TypeContainer.RootContext.TypeManager.FindMembers (
-                               t, mt, bf, Type.FilterName, name);
+               public static Expression MemberLookup (EmitContext ec, Type container_type,
+                                                      Type qualifier_type, Type queried_type,
+                                                      string name, MemberTypes mt,
+                                                      BindingFlags bf, Location loc)
+               {
+                       MemberInfo [] mi = TypeManager.MemberLookup (container_type, qualifier_type,
+                                                                    queried_type, mt, bf, name);
 
                        if (mi == null)
                                return null;
 
-                       // Empty array ...
-                       if (mi.Length == 0) 
-                               return null;
+                       int count = mi.Length;
 
-                       
-                       if (mi.Length == 1 && !(mi [0] is MethodBase))
-                               return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
-                       
-                       for (int i = 0; i < mi.Length; i++)
-                               if (!(mi [i] is MethodBase)){
-                                       Error (-5, "Do not know how to reproduce this case: " + 
-                                              "Methods and non-Method with the same name, " +
-                                              "report this please");
-
-                                       for (i = 0; i < mi.Length; i++){
-                                               Type tt = mi [i].GetType ();
-
-                                               Console.WriteLine (i + ": " + mi [i]);
-                                               while (tt != TypeManager.object_type){
-                                                       Console.WriteLine (tt);
-                                                       tt = tt.BaseType;
-                                               }
-                                       }
-                               }
+                       if (mi [0] is MethodBase)
+                               return new MethodGroupExpr (mi, loc);
+
+                       if (count > 1)
+                               return null;
 
-                       return new MethodGroupExpr (mi);
+                       return ExprClassFromMemberInfo (ec, mi [0], loc);
                }
 
                public const MemberTypes AllMemberTypes =
@@ -395,41 +578,188 @@ 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 queried_type,
+                                                      string name, Location loc)
+               {
+                       return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
+                                            AllMemberTypes, AllBindingFlags, loc);
+               }
+
+               public static Expression MemberLookup (EmitContext ec, Type qualifier_type,
+                                                      Type queried_type, string name, Location loc)
+               {
+                       return MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
+                                            name, AllMemberTypes, AllBindingFlags, loc);
+               }
+
+               public static Expression MethodLookup (EmitContext ec, Type queried_type,
+                                                      string name, Location loc)
+               {
+                       return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
+                                            MemberTypes.Method, 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 qualifier_type,
+                                                           Type queried_type, string name, Location loc)
+               {
+                       return MemberLookupFinal (ec, qualifier_type, queried_type, name,
+                                                 AllMemberTypes, AllBindingFlags, loc);
+               }
+
+               public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
+                                                           Type queried_type, string name,
+                                                           MemberTypes mt, BindingFlags bf,
+                                                           Location loc)
+               {
+                       Expression e;
+
+                       int errors = Report.Errors;
+
+                       e = MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
+                                         name, mt, bf, loc);
+
+                       if (e != null)
+                               return e;
+
+                       // Error has already been reported.
+                       if (errors < Report.Errors)
+                               return null;
+
+                       MemberLookupFailed (ec, qualifier_type, queried_type, name, null, loc);
+                       return null;
+               }
+
+               public static void MemberLookupFailed (EmitContext ec, Type qualifier_type,
+                                                      Type queried_type, string name,
+                                                      string class_name, Location loc)
                {
-                       return MemberLookup (ec, t, name, same_type, AllMemberTypes, AllBindingsFlags, loc);
+                       object lookup = TypeManager.MemberLookup (queried_type, null, queried_type,
+                                                                 AllMemberTypes, AllBindingFlags |
+                                                                 BindingFlags.NonPublic, name);
+
+                       if (lookup == null) {
+                               if (class_name != null)
+                                       Report.Error (103, loc, "The name `" + name + "' could not be " +
+                                                     "found in `" + class_name + "'");
+                               else
+                                       Report.Error (
+                                               117, loc, "`" + queried_type + "' does not contain a " +
+                                               "definition for `" + name + "'");
+                               return;
+                       }
+
+                       if ((qualifier_type != null) && (qualifier_type != ec.ContainerType) &&
+                           ec.ContainerType.IsSubclassOf (qualifier_type)) {
+                               // Although a derived class can access protected members of
+                               // its base class it cannot do so through an instance of the
+                               // base class (CS1540).  If the qualifier_type is a parent of the
+                               // ec.ContainerType and the lookup succeeds with the latter one,
+                               // then we are in this situation.
+
+                               lookup = TypeManager.MemberLookup (
+                                       ec.ContainerType, ec.ContainerType, ec.ContainerType,
+                                       AllMemberTypes, AllBindingFlags, name);
+
+                               if (lookup != null) {
+                                       Report.Error (
+                                               1540, loc, "Cannot access protected member `" +
+                                               TypeManager.CSharpName (qualifier_type) + "." +
+                                               name + "' " + "via a qualifier of type `" +
+                                               TypeManager.CSharpName (qualifier_type) + "'; the " +
+                                               "qualifier must be of type `" +
+                                               TypeManager.CSharpName (ec.ContainerType) + "' " +
+                                               "(or derived from it)");
+                                       return;
+                               }
+                       }
+
+                       if (qualifier_type != null)
+                               Report.Error (
+                                       122, loc, "`" + TypeManager.CSharpName (qualifier_type) + "." +
+                                       name + "' is inaccessible due to its protection level");
+                       else
+                               Report.Error (
+                                       122, loc, "`" + name + "' is inaccessible due to its " +
+                                       "protection level");
                }
 
+               static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
+               {
+                       EventInfo ei = event_expr.EventInfo;
+
+                       return TypeManager.GetPrivateFieldOfEvent (ei);
+               }
+               
+               static EmptyExpression MyEmptyExpr;
                static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
                {
                        Type expr_type = expr.Type;
 
-                       if (target_type == TypeManager.object_type) {
-                               if (expr_type.IsClass)
-                                       return new EmptyCast (expr, target_type);
+                       if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
+                               // if we are a method group, emit a warning
+
+                               expr.Emit (null);
+                       }
+
+                       //
+                       // notice that it is possible to write "ValueType v = 1", the ValueType here
+                       // is an abstract class, and not really a value type, so we apply the same rules.
+                       //
+                       if (target_type == TypeManager.object_type || target_type == TypeManager.value_type) {
+                               //
+                               // A pointer type cannot be converted to object
+                               // 
+                               if (expr_type.IsPointer)
+                                       return null;
+
                                if (expr_type.IsValueType)
                                        return new BoxedCast (expr);
+                               if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type)
+                                       return new EmptyCast (expr, target_type);
                        } else if (expr_type.IsSubclassOf (target_type)) {
+                               //
+                               // Special case: enumeration to System.Enum.
+                               // System.Enum is not a value type, it is a class, so we need
+                               // a boxing conversion
+                               //
+                               if (expr_type.IsEnum)
+                                       return new BoxedCast (expr);
+
                                return new EmptyCast (expr, target_type);
                        } else {
-                               // from any class-type S to any interface-type T.
-                               if (expr_type.IsClass && target_type.IsInterface) {
 
-                                       if (TypeManager.ImplementsInterface (expr_type, target_type))
-                                               return new EmptyCast (expr, target_type);
-                                       else
-                                               return null;
+                               // This code is kind of mirrored inside StandardConversionExists
+                               // with the small distinction that we only probe there
+                               //
+                               // Always ensure that the code here and there is in sync
+                               
+                               // from the null type to any reference-type.
+                               if (expr is NullLiteral && !target_type.IsValueType)
+                                       return new NullLiteralTyped (target_type);
+
+                               // from any class-type S to any interface-type T.
+                               if (target_type.IsInterface) {
+                                       if (TypeManager.ImplementsInterface (expr_type, target_type)){
+                                               if (expr_type.IsClass)
+                                                       return new EmptyCast (expr, target_type);
+                                               else if (expr_type.IsValueType)
+                                                       return new BoxedCast (expr);
+                                       }
                                }
 
                                // from any interface type S to interface-type T.
                                if (expr_type.IsInterface && target_type.IsInterface) {
-
                                        if (TypeManager.ImplementsInterface (expr_type, target_type))
                                                return new EmptyCast (expr, target_type);
                                        else
@@ -441,10 +771,15 @@ namespace Mono.CSharp {
                                        if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
 
                                                Type expr_element_type = expr_type.GetElementType ();
+
+                                               if (MyEmptyExpr == null)
+                                                       MyEmptyExpr = new EmptyExpression ();
+                                               
+                                               MyEmptyExpr.SetType (expr_element_type);
                                                Type target_element_type = target_type.GetElementType ();
 
                                                if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
-                                                       if (StandardConversionExists (expr_element_type,
+                                                       if (StandardConversionExists (MyEmptyExpr,
                                                                                      target_element_type))
                                                                return new EmptyCast (expr, target_type);
                                        }
@@ -456,19 +791,18 @@ namespace Mono.CSharp {
                                        return new EmptyCast (expr, target_type);
                                
                                // from any delegate type to System.Delegate
-                               if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
+                               if ((expr_type == TypeManager.delegate_type || 
+                                    expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
                                    target_type == TypeManager.delegate_type)
                                        return new EmptyCast (expr, target_type);
                                        
                                // from any array-type or delegate type into System.ICloneable.
-                               if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
+                               if (expr_type.IsArray ||
+                                   expr_type == TypeManager.delegate_type ||
+                                   expr_type.IsSubclassOf (TypeManager.delegate_type))
                                        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;
 
                        }
@@ -476,22 +810,6 @@ namespace Mono.CSharp {
                        return null;
                }
 
-               /// <summary>
-               ///   Handles expressions like this: decimal d; d = 1;
-               ///   and changes them into: decimal d; d = new System.Decimal (1);
-               /// </summary>
-               static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
-               {
-                       ArrayList args = new ArrayList ();
-
-                       args.Add (new Argument (expr, Argument.AType.Expression));
-
-                       Expression ne = new New (target.FullName, args,
-                                                new Location (-1));
-
-                       return ne.Resolve (ec);
-               }
-
                /// <summary>
                ///   Implicit Numeric Conversions.
                ///
@@ -506,174 +824,278 @@ namespace Mono.CSharp {
                        //
                        // Attempt to do the implicit constant expression conversions
 
-                       if (expr is IntLiteral){
-                               Expression e;
+                       if (expr is Constant){
                                
-                               e = TryImplicitIntConversion (target_type, (IntLiteral) expr);
-
-                               if (e != null)
-                                       return e;
-                       } else if (expr is LongLiteral && target_type == TypeManager.uint64_type){
-                               //
-                               // Try the implicit constant expression conversion
-                               // from long to ulong, instead of a nice routine,
-                               // we just inline it
-                               //
-                               if (((LongLiteral) expr).Value > 0)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
+                               if (expr is IntConstant){
+                                       Expression e;
+                                       
+                                       e = TryImplicitIntConversion (target_type, (IntConstant) expr);
+                                       
+                                       if (e != null)
+                                               return e;
+                               } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
+                                       //
+                                       // Try the implicit constant expression conversion
+                                       // from long to ulong, instead of a nice routine,
+                                       // we just inline it
+                                       //
+                                       long v = ((LongConstant) expr).Value;
+                                       if (v > 0)
+                                               return new ULongConstant ((ulong) v);
+                               } 
                        }
                        
+                       Type real_target_type = target_type;
+
                        if (expr_type == TypeManager.sbyte_type){
                                //
                                // From sbyte to short, int, long, float, double.
                                //
-                               if (target_type == TypeManager.int32_type)
+                               if (real_target_type == TypeManager.int32_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
-                               if (target_type == TypeManager.int64_type)
+                               if (real_target_type == TypeManager.int64_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
-                               if (target_type == TypeManager.double_type)
+                               if (real_target_type == TypeManager.double_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
-                               if (target_type == TypeManager.float_type)
+                               if (real_target_type == TypeManager.float_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
-                               if (target_type == TypeManager.short_type)
+                               if (real_target_type == TypeManager.short_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
-                               if (target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.byte_type){
                                //
                                // From byte to short, ushort, int, uint, long, ulong, float, double
                                // 
-                               if ((target_type == TypeManager.short_type) ||
-                                   (target_type == TypeManager.ushort_type) ||
-                                   (target_type == TypeManager.int32_type) ||
-                                   (target_type == TypeManager.uint32_type))
+                               if ((real_target_type == TypeManager.short_type) ||
+                                   (real_target_type == TypeManager.ushort_type) ||
+                                   (real_target_type == TypeManager.int32_type) ||
+                                   (real_target_type == TypeManager.uint32_type))
                                        return new EmptyCast (expr, target_type);
 
-                               if (target_type == TypeManager.uint64_type)
+                               if (real_target_type == TypeManager.uint64_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
-                               if (target_type == TypeManager.int64_type)
+                               if (real_target_type == TypeManager.int64_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
-                               
-                               if (target_type == TypeManager.float_type)
+                               if (real_target_type == TypeManager.float_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
-                               if (target_type == TypeManager.double_type)
+                               if (real_target_type == TypeManager.double_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
-                               if (target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.short_type){
                                //
                                // From short to int, long, float, double
                                // 
-                               if (target_type == TypeManager.int32_type)
+                               if (real_target_type == TypeManager.int32_type)
                                        return new EmptyCast (expr, target_type);
-                               if (target_type == TypeManager.int64_type)
+                               if (real_target_type == TypeManager.int64_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
-                               if (target_type == TypeManager.double_type)
+                               if (real_target_type == TypeManager.double_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
-                               if (target_type == TypeManager.float_type)
+                               if (real_target_type == TypeManager.float_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
-                               if (target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.ushort_type){
                                //
                                // From ushort to int, uint, long, ulong, float, double
                                //
-                               if (target_type == TypeManager.uint32_type)
+                               if (real_target_type == TypeManager.uint32_type)
                                        return new EmptyCast (expr, target_type);
 
-                               if (target_type == TypeManager.uint64_type)
+                               if (real_target_type == TypeManager.uint64_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
-                               if (target_type == TypeManager.int32_type)
+                               if (real_target_type == TypeManager.int32_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
-                               if (target_type == TypeManager.int64_type)
+                               if (real_target_type == TypeManager.int64_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
-                               if (target_type == TypeManager.double_type)
+                               if (real_target_type == TypeManager.double_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
-                               if (target_type == TypeManager.float_type)
+                               if (real_target_type == TypeManager.float_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
-                               if (target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.int32_type){
                                //
                                // From int to long, float, double
                                //
-                               if (target_type == TypeManager.int64_type)
+                               if (real_target_type == TypeManager.int64_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
-                               if (target_type == TypeManager.double_type)
+                               if (real_target_type == TypeManager.double_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
-                               if (target_type == TypeManager.float_type)
+                               if (real_target_type == TypeManager.float_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
-                               if (target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.uint32_type){
                                //
                                // From uint to long, ulong, float, double
                                //
-                               if (target_type == TypeManager.int64_type)
+                               if (real_target_type == TypeManager.int64_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
-                               if (target_type == TypeManager.uint64_type)
+                               if (real_target_type == TypeManager.uint64_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
-                               if (target_type == TypeManager.double_type)
+                               if (real_target_type == TypeManager.double_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
                                                               OpCodes.Conv_R8);
-                               if (target_type == TypeManager.float_type)
+                               if (real_target_type == TypeManager.float_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
                                                               OpCodes.Conv_R4);
-                               if (target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
-                       } else if ((expr_type == TypeManager.uint64_type) ||
-                                  (expr_type == TypeManager.int64_type)){
+                       } else if (expr_type == TypeManager.int64_type){
                                //
                                // From long/ulong to float, double
                                //
-                               if (target_type == TypeManager.double_type)
+                               if (real_target_type == TypeManager.double_type)
+                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
+                               if (real_target_type == TypeManager.float_type)
+                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);     
+                       } else if (expr_type == TypeManager.uint64_type){
+                               //
+                               // From ulong to float, double
+                               //
+                               if (real_target_type == TypeManager.double_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
                                                               OpCodes.Conv_R8);
-                               if (target_type == TypeManager.float_type)
+                               if (real_target_type == TypeManager.float_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
                                                               OpCodes.Conv_R4);        
-                               if (target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.char_type){
                                //
                                // From char to ushort, int, uint, long, ulong, float, double
                                // 
-                               if ((target_type == TypeManager.ushort_type) ||
-                                   (target_type == TypeManager.int32_type) ||
-                                   (target_type == TypeManager.uint32_type))
+                               if ((real_target_type == TypeManager.ushort_type) ||
+                                   (real_target_type == TypeManager.int32_type) ||
+                                   (real_target_type == TypeManager.uint32_type))
                                        return new EmptyCast (expr, target_type);
-                               if (target_type == TypeManager.uint64_type)
+                               if (real_target_type == TypeManager.uint64_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
-                               if (target_type == TypeManager.int64_type)
+                               if (real_target_type == TypeManager.int64_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
-                               if (target_type == TypeManager.float_type)
+                               if (real_target_type == TypeManager.float_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
-                               if (target_type == TypeManager.double_type)
+                               if (real_target_type == TypeManager.double_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
-                               if (target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.float_type){
                                //
                                // float to double
                                //
-                               if (target_type == TypeManager.double_type)
+                               if (real_target_type == TypeManager.double_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
                        }
 
                        return null;
                }
 
+               //
+               // Tests whether an implicit reference conversion exists between expr_type
+               // and target_type
+               //
+               public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
+               {
+                       Type expr_type = expr.Type;
+                       
+                       //
+                       // This is the boxed case.
+                       //
+                       if (target_type == TypeManager.object_type) {
+                               if (expr_type.IsClass || expr_type.IsValueType ||
+                                   expr_type.IsInterface || expr_type == TypeManager.enum_type)
+                                       return true;
+                       } else if (expr_type.IsSubclassOf (target_type)) {
+                               return true;
+                       } else {
+                               // Please remember that all code below actually comes
+                               // from ImplicitReferenceConversion so make sure code remains in sync
+                               
+                               // from any class-type S to any interface-type T.
+                               if (target_type.IsInterface) {
+                                       if (TypeManager.ImplementsInterface (expr_type, target_type))
+                                               return true;
+                               }
+                               
+                               // from any interface type S to interface-type T.
+                               if (expr_type.IsInterface && target_type.IsInterface)
+                                       if (TypeManager.ImplementsInterface (expr_type, target_type))
+                                               return true;
+                               
+                               // from an array-type S to an array-type of type T
+                               if (expr_type.IsArray && target_type.IsArray) {
+                                       if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
+                                               
+                                               Type expr_element_type = expr_type.GetElementType ();
+
+                                               if (MyEmptyExpr == null)
+                                                       MyEmptyExpr = new EmptyExpression ();
+                                               
+                                               MyEmptyExpr.SetType (expr_element_type);
+                                               Type target_element_type = target_type.GetElementType ();
+                                               
+                                               if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
+                                                       if (StandardConversionExists (MyEmptyExpr,
+                                                                                     target_element_type))
+                                                               return true;
+                                       }
+                               }
+                               
+                               // from an array-type to System.Array
+                               if (expr_type.IsArray && (target_type == TypeManager.array_type))
+                                       return true;
+                               
+                               // from any delegate type to System.Delegate
+                               if ((expr_type == TypeManager.delegate_type ||
+                                    expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
+                                   target_type == TypeManager.delegate_type)
+                                       if (target_type.IsAssignableFrom (expr_type))
+                                               return true;
+                                       
+                               // from any array-type or delegate type into System.ICloneable.
+                               if (expr_type.IsArray ||
+                                   expr_type == TypeManager.delegate_type ||
+                                   expr_type.IsSubclassOf (TypeManager.delegate_type))
+                                       if (target_type == TypeManager.icloneable_type)
+                                               return true;
+                               
+                               // from the null type to any reference-type.
+                               if (expr is NullLiteral && !target_type.IsValueType &&
+                                   !TypeManager.IsEnumType (target_type))
+                                       return true;
+                               
+                       }
+
+                       return false;
+               }
+
+               /// <summary>
+               ///  Same as StandardConversionExists except that it also looks at
+               ///  implicit user defined conversions - needed for overload resolution
+               /// </summary>
+               public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
+               {
+                       if (StandardConversionExists (expr, target_type) == true)
+                               return true;
+
+                       Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
+
+                       if (dummy != null)
+                               return true;
+
+                       return false;
+               }
+
+               public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
+               {
+                       Expression dummy = ImplicitUserConversion (
+                               ec, new EmptyExpression (source), target, Location.Null);
+                       return dummy != null;
+               }
+
                /// <summary>
                ///  Determines if a standard implicit conversion exists from
                ///  expr_type to target_type
                /// </summary>
-               public static bool StandardConversionExists (Type expr_type, Type target_type)
+               public static bool StandardConversionExists (Expression expr, Type target_type)
                {
+                       Type expr_type = expr.Type;
+
+                       if (expr_type == TypeManager.void_type)
+                               return false;
+                       
                        if (expr_type == target_type)
                                return true;
 
                        // First numeric conversions 
-                       
+
                        if (expr_type == TypeManager.sbyte_type){
                                //
                                // From sbyte to short, int, long, float, double.
@@ -778,122 +1200,292 @@ namespace Mono.CSharp {
                                        return true;
                        }       
                        
-                       // Next reference conversions
-
-                       if (target_type == TypeManager.object_type) {
-                               if ((expr_type.IsClass) ||
-                                   (expr_type.IsValueType))
-                                       return true;
-                               
-                       } else if (expr_type.IsSubclassOf (target_type)) {
+                       if (ImplicitReferenceConversionExists (expr, target_type))
                                return true;
-                               
-                       } else {
-                               // from any class-type S to any interface-type T.
-                               if (expr_type.IsClass && target_type.IsInterface)
-                                       return true;
-                               
-                               // from any interface type S to interface-type T.
-                               // FIXME : Is it right to use IsAssignableFrom ?
-                               if (expr_type.IsInterface && target_type.IsInterface)
-                                       if (target_type.IsAssignableFrom (expr_type))
+                       
+                       if (expr is IntConstant){
+                               int value = ((IntConstant) expr).Value;
+
+                               if (target_type == TypeManager.sbyte_type){
+                                       if (value >= SByte.MinValue && value <= SByte.MaxValue)
                                                return true;
-                               
-                               // from an array-type S to an array-type of type T
-                               if (expr_type.IsArray && target_type.IsArray) {
-                                       if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
-                                               
-                                               Type expr_element_type = expr_type.GetElementType ();
-                                               Type target_element_type = target_type.GetElementType ();
-                                               
-                                               if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
-                                                       if (StandardConversionExists (expr_element_type,
-                                                                                     target_element_type))
-                                                               return true;
-                                       }
-                               }
-                               
-                               // from an array-type to System.Array
-                               if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
-                                       return true;
-                               
-                               // from any delegate type to System.Delegate
-                               if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
-                                   target_type == TypeManager.delegate_type)
-                                       if (target_type.IsAssignableFrom (expr_type))
+                               } else if (target_type == TypeManager.byte_type){
+                                       if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
                                                return true;
-                                       
-                               // from any array-type or delegate type into System.ICloneable.
-                               if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
-                                       if (target_type == TypeManager.icloneable_type)
+                               } else if (target_type == TypeManager.short_type){
+                                       if (value >= Int16.MinValue && value <= Int16.MaxValue)
+                                               return true;
+                               } else if (target_type == TypeManager.ushort_type){
+                                       if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
                                                return true;
+                               } else if (target_type == TypeManager.uint32_type){
+                                       if (value >= 0)
+                                               return true;
+                               } else if (target_type == TypeManager.uint64_type){
+                                        //
+                                        // we can optimize this case: a positive int32
+                                        // always fits on a uint64.  But we need an opcode
+                                        // to do it.
+                                        //
+                                       if (value >= 0)
+                                               return true;
+                               }
                                
-                               // from the null type to any reference-type.
-                               // FIXME : How do we do this ?
+                               if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
+                                       return true;
+                       }
+
+                       if (expr is LongConstant && target_type == TypeManager.uint64_type){
+                               //
+                               // Try the implicit constant expression conversion
+                               // from long to ulong, instead of a nice routine,
+                               // we just inline it
+                               //
+                               long v = ((LongConstant) expr).Value;
+                               if (v > 0)
+                                       return true;
+                       }
+                       
+                       if ((target_type == TypeManager.enum_type ||
+                            target_type.IsSubclassOf (TypeManager.enum_type)) &&
+                            expr is IntLiteral){
+                               IntLiteral i = (IntLiteral) expr;
 
+                               if (i.Value == 0)
+                                       return true;
                        }
 
+                       if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
+                               return true;
+
                        return false;
                }
+
+               //
+               // Used internally by FindMostEncompassedType, this is used
+               // to avoid creating lots of objects in the tight loop inside
+               // FindMostEncompassedType
+               //
+               static EmptyExpression priv_fmet_param;
                
                /// <summary>
                ///  Finds "most encompassed type" according to the spec (13.4.2)
-               ///  amongst the methods in the MethodGroupExpr which convert from a
-               ///  type encompassing source_type
+               ///  amongst the methods in the MethodGroupExpr
                /// </summary>
-               static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
+               static Type FindMostEncompassedType (ArrayList types)
                {
                        Type best = null;
-                       
-                       for (int i = me.Methods.Length; i > 0; ) {
-                               i--;
 
-                               MethodBase mb = me.Methods [i];
-                               ParameterData pd = Invocation.GetParameterData (mb);
-                               Type param_type = pd.ParameterType (0);
+                       if (priv_fmet_param == null)
+                               priv_fmet_param = new EmptyExpression ();
 
-                               if (StandardConversionExists (source_type, param_type)) {
-                                       if (best == null)
-                                               best = param_type;
-                                       
-                                       if (StandardConversionExists (param_type, best))
-                                               best = param_type;
+                       foreach (Type t in types){
+                               priv_fmet_param.SetType (t);
+                               
+                               if (best == null) {
+                                       best = t;
+                                       continue;
                                }
+                               
+                               if (StandardConversionExists (priv_fmet_param, best))
+                                       best = t;
                        }
 
                        return best;
                }
+
+               //
+               // Used internally by FindMostEncompassingType, this is used
+               // to avoid creating lots of objects in the tight loop inside
+               // FindMostEncompassingType
+               //
+               static EmptyExpression priv_fmee_ret;
                
                /// <summary>
                ///  Finds "most encompassing type" according to the spec (13.4.2)
-               ///  amongst the methods in the MethodGroupExpr which convert to a
-               ///  type encompassed by target_type
+               ///  amongst the types in the given set
                /// </summary>
-               static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
+               static Type FindMostEncompassingType (ArrayList types)
                {
                        Type best = null;
-                       
-                       for (int i = me.Methods.Length; i > 0; ) {
-                               i--;
-                               
-                               MethodInfo mi = (MethodInfo) me.Methods [i];
-                               Type ret_type = mi.ReturnType;
-                               
-                               if (StandardConversionExists (ret_type, target)) {
-                                       if (best == null)
-                                               best = ret_type;
 
-                                       if (!StandardConversionExists (ret_type, best))
-                                               best = ret_type;
+                       if (priv_fmee_ret == null)
+                               priv_fmee_ret = new EmptyExpression ();
+
+                       foreach (Type t in types){
+                               priv_fmee_ret.SetType (best);
+
+                               if (best == null) {
+                                       best = t;
+                                       continue;
                                }
-                               
+
+                               if (StandardConversionExists (priv_fmee_ret, t))
+                                       best = t;
                        }
                        
                        return best;
+               }
+
+               //
+               // Used to avoid creating too many objects
+               //
+               static EmptyExpression priv_fms_expr;
+               
+               /// <summary>
+               ///   Finds the most specific source Sx according to the rules of the spec (13.4.4)
+               ///   by making use of FindMostEncomp* methods. Applies the correct rules separately
+               ///   for explicit and implicit conversion operators.
+               /// </summary>
+               static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
+                                                          bool apply_explicit_conv_rules,
+                                                          Location loc)
+               {
+                       ArrayList src_types_set = new ArrayList ();
+                       
+                       if (priv_fms_expr == null)
+                               priv_fms_expr = new EmptyExpression ();
+
+                       //
+                       // If any operator converts from S then Sx = S
+                       //
+                       Type source_type = source.Type;
+                       foreach (MethodBase mb in me.Methods){
+                               ParameterData pd = Invocation.GetParameterData (mb);
+                               Type param_type = pd.ParameterType (0);
+
+                               if (param_type == source_type)
+                                       return param_type;
+
+                               if (apply_explicit_conv_rules) {
+                                       //
+                                       // From the spec :
+                                       // Find the set of applicable user-defined conversion operators, U.  This set
+                                       // consists of the
+                                       // user-defined implicit or explicit conversion operators declared by
+                                       // the classes or structs in D that convert from a type encompassing
+                                       // or encompassed by S to a type encompassing or encompassed by T
+                                       //
+                                       priv_fms_expr.SetType (param_type);
+                                       if (StandardConversionExists (priv_fms_expr, source_type))
+                                               src_types_set.Add (param_type);
+                                       else {
+                                               if (StandardConversionExists (source, param_type))
+                                                       src_types_set.Add (param_type);
+                                       }
+                               } else {
+                                       //
+                                       // Only if S is encompassed by param_type
+                                       //
+                                       if (StandardConversionExists (source, param_type))
+                                               src_types_set.Add (param_type);
+                               }
+                       }
+                       
+                       //
+                       // Explicit Conv rules
+                       //
+                       if (apply_explicit_conv_rules) {
+                               ArrayList candidate_set = new ArrayList ();
+
+                               foreach (Type param_type in src_types_set){
+                                       if (StandardConversionExists (source, param_type))
+                                               candidate_set.Add (param_type);
+                               }
 
+                               if (candidate_set.Count != 0)
+                                       return FindMostEncompassedType (candidate_set);
+                       }
+
+                       //
+                       // Final case
+                       //
+                       if (apply_explicit_conv_rules)
+                               return FindMostEncompassingType (src_types_set);
+                       else
+                               return FindMostEncompassedType (src_types_set);
                }
+
+               //
+               // Useful in avoiding proliferation of objects
+               //
+               static EmptyExpression priv_fmt_expr;
                
+               /// <summary>
+               ///  Finds the most specific target Tx according to section 13.4.4
+               /// </summary>
+               static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
+                                                          bool apply_explicit_conv_rules,
+                                                          Location loc)
+               {
+                       ArrayList tgt_types_set = new ArrayList ();
+                       
+                       if (priv_fmt_expr == null)
+                               priv_fmt_expr = new EmptyExpression ();
+                       
+                       //
+                       // If any operator converts to T then Tx = T
+                       //
+                       foreach (MethodInfo mi in me.Methods){
+                               Type ret_type = mi.ReturnType;
+
+                               if (ret_type == target)
+                                       return ret_type;
+
+                               if (apply_explicit_conv_rules) {
+                                       //
+                                       // From the spec :
+                                       // Find the set of applicable user-defined conversion operators, U.
+                                       //
+                                       // This set consists of the
+                                       // user-defined implicit or explicit conversion operators declared by
+                                       // the classes or structs in D that convert from a type encompassing
+                                       // or encompassed by S to a type encompassing or encompassed by T
+                                       //
+                                       priv_fms_expr.SetType (ret_type);
+                                       if (StandardConversionExists (priv_fms_expr, target))
+                                               tgt_types_set.Add (ret_type);
+                                       else {
+                                               priv_fms_expr.SetType (target);
+                                               if (StandardConversionExists (priv_fms_expr, ret_type))
+                                                       tgt_types_set.Add (ret_type);
+                                       }
+                               } else {
+                                       //
+                                       // Only if T is encompassed by param_type
+                                       //
+                                       priv_fms_expr.SetType (ret_type);
+                                       if (StandardConversionExists (priv_fms_expr, target))
+                                               tgt_types_set.Add (ret_type);
+                               }
+                       }
+
+                       //
+                       // Explicit conv rules
+                       //
+                       if (apply_explicit_conv_rules) {
+                               ArrayList candidate_set = new ArrayList ();
+
+                               foreach (Type ret_type in tgt_types_set){
+                                       priv_fmt_expr.SetType (ret_type);
+                                       
+                                       if (StandardConversionExists (priv_fmt_expr, target))
+                                               candidate_set.Add (ret_type);
+                               }
 
+                               if (candidate_set.Count != 0)
+                                       return FindMostEncompassingType (candidate_set);
+                       }
+                       
+                       //
+                       // Okay, final case !
+                       //
+                       if (apply_explicit_conv_rules)
+                               return FindMostEncompassedType (tgt_types_set);
+                       else 
+                               return FindMostEncompassingType (tgt_types_set);
+               }
+               
                /// <summary>
                ///  User-defined Implicit conversions
                /// </summary>
@@ -911,132 +1503,157 @@ namespace Mono.CSharp {
                {
                        return UserDefinedConversion (ec, source, target, loc, true);
                }
-               
+
                /// <summary>
-               ///   User-defined conversions
+               ///   Computes the MethodGroup for the user-defined conversion
+               ///   operators from source_type to target_type.  `look_for_explicit'
+               ///   controls whether we should also include the list of explicit
+               ///   operators
                /// </summary>
-               static public Expression UserDefinedConversion (EmitContext ec, Expression source,
-                                                               Type target, Location loc,
-                                                               bool look_for_explicit)
+               static MethodGroupExpr GetConversionOperators (EmitContext ec,
+                                                              Type source_type, Type target_type,
+                                                              Location loc, bool look_for_explicit)
                {
-                       Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
+                       Expression mg1 = null, mg2 = null;
                        Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
-                       Expression e;
-                       MethodBase method = null;
-                       Type source_type = source.Type;
-
                        string op_name;
-                       
-                       // If we have a boolean type, we need to check for the True operator
 
+                       //
                        // FIXME : How does the False operator come into the picture ?
-                       // FIXME : This doesn't look complete and very correct !
-                       if (target == TypeManager.bool_type)
+                       // This doesn't look complete and very correct !
+                       //
+                       if (target_type == TypeManager.bool_type && !look_for_explicit)
                                op_name = "op_True";
                        else
                                op_name = "op_Implicit";
-                       
-                       mg1 = MemberLookup (ec, source_type, op_name, false, loc);
 
-                       if (source_type.BaseType != null)
-                               mg2 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
+                       MethodGroupExpr union3;
                        
-                       mg3 = MemberLookup (ec, target, op_name, false, loc);
+                       mg1 = MethodLookup (ec, source_type, op_name, loc);
+                       if (source_type.BaseType != null)
+                               mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
 
-                       if (target.BaseType != null)
-                               mg4 = MemberLookup (ec, target.BaseType, op_name, false, loc);
+                       if (mg1 == null)
+                               union3 = (MethodGroupExpr) mg2;
+                       else if (mg2 == null)
+                               union3 = (MethodGroupExpr) mg1;
+                       else
+                               union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
 
-                       MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
-                       MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
+                       mg1 = MethodLookup (ec, target_type, op_name, loc);
+                       if (mg1 != null){
+                               if (union3 != null)
+                                       union3 = Invocation.MakeUnionSet (union3, mg1, loc);
+                               else
+                                       union3 = (MethodGroupExpr) mg1;
+                       }
 
-                       MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
+                       if (target_type.BaseType != null)
+                               mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
+                       
+                       if (mg1 != null){
+                               if (union3 != null)
+                                       union3 = Invocation.MakeUnionSet (union3, mg1, loc);
+                               else
+                                       union3 = (MethodGroupExpr) mg1;
+                       }
 
                        MethodGroupExpr union4 = null;
 
                        if (look_for_explicit) {
-
                                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);
-                               
-                               mg7 = MemberLookup (ec, target, op_name, false, loc);
+                                       mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
                                
-                               if (target.BaseType != null)
-                                       mg8 = MemberLookup (ec, target.BaseType, op_name, false, loc);
+                               mg7 = MemberLookup (ec, target_type, op_name, loc);
+                               if (target_type.BaseType != null)
+                                       mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
                                
-                               MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
-                               MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
+                               MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
+                               MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
 
-                               union4 = Invocation.MakeUnionSet (union5, union6);
+                               union4 = Invocation.MakeUnionSet (union5, union6, loc);
                        }
                        
-                       MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
-
-                       if (union != null) {
-
-                               Type most_specific_source, most_specific_target;
+                       return Invocation.MakeUnionSet (union3, union4, loc);
+               }
+               
+               /// <summary>
+               ///   User-defined conversions
+               /// </summary>
+               static public Expression UserDefinedConversion (EmitContext ec, Expression source,
+                                                               Type target, Location loc,
+                                                               bool look_for_explicit)
+               {
+                       MethodGroupExpr union;
+                       Type source_type = source.Type;
+                       MethodBase method = null;
 
-                               most_specific_source = FindMostEncompassedType (union, source_type);
-                               if (most_specific_source == null)
-                                       return null;
+                       union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
+                       if (union == null)
+                               return null;
+                       
+                       Type most_specific_source, most_specific_target;
 
-                               most_specific_target = FindMostEncompassingType (union, target);
-                               if (most_specific_target == null) 
-                                       return null;
-                               
-                               int count = 0;
-                               
-                               for (int i = union.Methods.Length; i > 0;) {
-                                       i--;
+#if BLAH
+                       foreach (MethodBase m in union.Methods){
+                               Console.WriteLine ("Name: " + m.Name);
+                               Console.WriteLine ("    : " + ((MethodInfo)m).ReturnType);
+                       }
+#endif
+                       
+                       most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
+                       if (most_specific_source == null)
+                               return null;
 
-                                       MethodBase mb = union.Methods [i];
-                                       ParameterData pd = Invocation.GetParameterData (mb);
-                                       MethodInfo mi = (MethodInfo) union.Methods [i];
+                       most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
+                       if (most_specific_target == null) 
+                               return null;
 
-                                       if (pd.ParameterType (0) == most_specific_source &&
-                                           mi.ReturnType == most_specific_target) {
-                                               method = mb;
-                                               count++;
-                                       }
-                               }
+                       int count = 0;
 
-                               if (method == null || count > 1) {
-                                       Report.Error (-11, loc, "Ambiguous user defined conversion");
-                                       return null;
-                               }
+                       
+                       foreach (MethodBase mb in union.Methods){
+                               ParameterData pd = Invocation.GetParameterData (mb);
+                               MethodInfo mi = (MethodInfo) mb;
                                
-                               //
-                               // This will do the conversion to the best match that we
-                               // found.  Now we need to perform an implict standard conversion
-                               // if the best match was not the type that we were requested
-                               // by target.
-                               //
-                               if (look_for_explicit)
-                                       source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
-                               else
-                                       source = ConvertImplicitStandard (ec, source,
-                                                                         most_specific_source, loc);
+                               if (pd.ParameterType (0) == most_specific_source &&
+                                   mi.ReturnType == most_specific_target) {
+                                       method = mb;
+                                       count++;
+                               }
+                       }
+                       
+                       if (method == null || count > 1)
+                               return null;
+                       
+                       
+                       //
+                       // This will do the conversion to the best match that we
+                       // found.  Now we need to perform an implict standard conversion
+                       // if the best match was not the type that we were requested
+                       // by target.
+                       //
+                       if (look_for_explicit)
+                               source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
+                       else
+                               source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
 
-                               if (source == null)
-                                       return null;
-                               
-                               e =  new UserCast ((MethodInfo) method, source);
-                               
-                               if (e.Type != target){
-                                       if (!look_for_explicit)
-                                               e = ConvertImplicitStandard (ec, e, target, loc);
-                                       else
-                                               e = ConvertExplicitStandard (ec, e, target, loc);
+                       if (source == null)
+                               return null;
 
-                                       return e;
-                               } else
-                                       return e;
+                       Expression e;
+                       e =  new UserCast ((MethodInfo) method, source, loc);
+                       if (e.Type != target){
+                               if (!look_for_explicit)
+                                       e = ConvertImplicitStandard (ec, e, target, loc);
+                               else
+                                       e = ConvertExplicitStandard (ec, e, target, loc);
                        }
-                       
-                       return null;
+
+                       return e;
                }
                
                /// <summary>
@@ -1056,11 +1673,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;
 
@@ -1068,13 +1681,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;
                }
 
@@ -1105,44 +1711,67 @@ namespace Mono.CSharp {
                        e = ImplicitReferenceConversion (expr, target_type);
                        if (e != null)
                                return e;
-
-                       if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
+                       
+                       if ((target_type == TypeManager.enum_type ||
+                            target_type.IsSubclassOf (TypeManager.enum_type)) &&
+                           expr is IntLiteral){
                                IntLiteral i = (IntLiteral) expr;
 
                                if (i.Value == 0)
-                                       return new EmptyCast (expr, target_type);
+                                       return new EnumConstant ((Constant) expr, target_type);
+                       }
+
+                       if (ec.InUnsafe) {
+                               if (expr_type.IsPointer){
+                                       if (target_type == TypeManager.void_ptr_type)
+                                               return new EmptyCast (expr, target_type);
+
+                                       //
+                                       // yep, comparing pointer types cant be done with
+                                       // t1 == t2, we have to compare their element types.
+                                       //
+                                       if (target_type.IsPointer){
+                                               if (target_type.GetElementType() == expr_type.GetElementType())
+                                                       return expr;
+                                       }
+                               }
+                               
+                               if (target_type.IsPointer) {
+                                       if (expr is NullLiteral)
+                                               return new EmptyCast (expr, target_type);
+
+                                       if (expr_type == TypeManager.void_ptr_type)
+                                               return new EmptyCast (expr, target_type);
+                               }
                        }
+
                        return null;
                }
 
                /// <summary>
-               ///   Attemps to perform an implict constant conversion of the IntLiteral
+               ///   Attemps to perform an implict constant conversion of the IntConstant
                ///   into a different data type using casts (See Implicit Constant
                ///   Expression Conversions)
                /// </summary>
-               static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
+               static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
                {
-                       int value = il.Value;
-                       
+                       int value = ic.Value;
+
                        if (target_type == TypeManager.sbyte_type){
                                if (value >= SByte.MinValue && value <= SByte.MaxValue)
-                                       return new EmptyCast (il, target_type);
+                                       return new SByteConstant ((sbyte) value);
                        } else if (target_type == TypeManager.byte_type){
                                if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
-                                       return new EmptyCast (il, target_type);
+                                       return new ByteConstant ((byte) value);
                        } else if (target_type == TypeManager.short_type){
                                if (value >= Int16.MinValue && value <= Int16.MaxValue)
-                                       return new EmptyCast (il, target_type);
+                                       return new ShortConstant ((short) value);
                        } else if (target_type == TypeManager.ushort_type){
                                if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
-                                       return new EmptyCast (il, target_type);
+                                       return new UShortConstant ((ushort) value);
                        } else if (target_type == TypeManager.uint32_type){
-                               //
-                               // we can optimize this case: a positive int32
-                               // always fits on a uint32
-                               //
                                if (value >= 0)
-                                       return new EmptyCast (il, target_type);
+                                       return new UIntConstant ((uint) value);
                        } else if (target_type == TypeManager.uint64_type){
                                //
                                // we can optimize this case: a positive int32
@@ -1150,12 +1779,39 @@ namespace Mono.CSharp {
                                // to do it.
                                //
                                if (value >= 0)
-                                       return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
+                                       return new ULongConstant ((ulong) value);
+                       } else if (target_type == TypeManager.double_type)
+                               return new DoubleConstant ((double) value);
+                       else if (target_type == TypeManager.float_type)
+                               return new FloatConstant ((float) value);
+                       
+                       if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
+                               Type underlying = TypeManager.EnumToUnderlying (target_type);
+                               Constant e = (Constant) ic;
+                               
+                               //
+                               // Possibly, we need to create a different 0 literal before passing
+                               // to EnumConstant
+                               //n
+                               if (underlying == TypeManager.int64_type)
+                                       e = new LongLiteral (0);
+                               else if (underlying == TypeManager.uint64_type)
+                                       e = new ULongLiteral (0);
+
+                               return new EnumConstant (e, target_type);
                        }
-
                        return null;
                }
 
+               static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
+               {
+                       string msg = "Cannot convert implicitly from `"+
+                               TypeManager.CSharpName (source) + "' to `" +
+                               TypeManager.CSharpName (target) + "'";
+
+                       Report.Error (29, loc, msg);
+               }
+
                /// <summary>
                ///   Attemptes to implicityly convert `target' into `type', using
                ///   ConvertImplicit.  If there is no implicit conversion, then
@@ -1169,12 +1825,14 @@ namespace Mono.CSharp {
                        e = ConvertImplicit (ec, source, target_type, loc);
                        if (e != null)
                                return e;
-                       
-                       string msg = "Cannot convert implicitly from `"+
-                               TypeManager.CSharpName (source.Type) + "' to `" +
-                               TypeManager.CSharpName (target_type) + "'";
 
-                       Error (29, loc, msg);
+                       if (source is DoubleLiteral && target_type == TypeManager.float_type){
+                               Report.Error (664, loc,
+                                             "Double literal cannot be implicitly converted to " +
+                                             "float type, use F suffix to create a float literal");
+                       }
+
+                       Error_CannotConvertImplicit (loc, source.Type, target_type);
 
                        return null;
                }
@@ -1182,199 +1840,212 @@ namespace Mono.CSharp {
                /// <summary>
                ///   Performs the explicit numeric conversions
                /// </summary>
-               static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
-                                                         Type target_type)
+               static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type, Location loc)
                {
                        Type expr_type = expr.Type;
+
+                       //
+                       // If we have an enumeration, extract the underlying type,
+                       // use this during the comparison, but wrap around the original
+                       // target_type
+                       //
+                       Type real_target_type = target_type;
+
+                       if (TypeManager.IsEnumType (real_target_type))
+                               real_target_type = TypeManager.EnumToUnderlying (real_target_type);
+
+                       if (StandardConversionExists (expr, real_target_type)){
+                               Expression ce = ConvertImplicitStandard (ec, expr, real_target_type, loc);
+
+                               if (real_target_type != target_type)
+                                       return new EmptyCast (ce, target_type);
+                               return ce;
+                       }
                        
                        if (expr_type == TypeManager.sbyte_type){
                                //
                                // From sbyte to byte, ushort, uint, ulong, char
                                //
-                               if (target_type == TypeManager.byte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
-                               if (target_type == TypeManager.ushort_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
-                               if (target_type == TypeManager.uint32_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
-                               if (target_type == TypeManager.uint64_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
-                               if (target_type == TypeManager.char_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                               if (real_target_type == TypeManager.byte_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
+                               if (real_target_type == TypeManager.ushort_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
+                               if (real_target_type == TypeManager.uint32_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
+                               if (real_target_type == TypeManager.uint64_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
+                               if (real_target_type == TypeManager.char_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
                        } else if (expr_type == TypeManager.byte_type){
                                //
                                // From byte to sbyte and char
                                //
-                               if (target_type == TypeManager.sbyte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
-                               if (target_type == TypeManager.char_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                               if (real_target_type == TypeManager.sbyte_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
+                               if (real_target_type == TypeManager.char_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
                        } else if (expr_type == TypeManager.short_type){
                                //
                                // From short to sbyte, byte, ushort, uint, ulong, char
                                //
-                               if (target_type == TypeManager.sbyte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
-                               if (target_type == TypeManager.byte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
-                               if (target_type == TypeManager.ushort_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
-                               if (target_type == TypeManager.uint32_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
-                               if (target_type == TypeManager.uint64_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
-                               if (target_type == TypeManager.char_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                               if (real_target_type == TypeManager.sbyte_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
+                               if (real_target_type == TypeManager.byte_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
+                               if (real_target_type == TypeManager.ushort_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
+                               if (real_target_type == TypeManager.uint32_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
+                               if (real_target_type == TypeManager.uint64_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
+                               if (real_target_type == TypeManager.char_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
                        } else if (expr_type == TypeManager.ushort_type){
                                //
                                // From ushort to sbyte, byte, short, char
                                //
-                               if (target_type == TypeManager.sbyte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
-                               if (target_type == TypeManager.byte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
-                               if (target_type == TypeManager.short_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
-                               if (target_type == TypeManager.char_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                               if (real_target_type == TypeManager.sbyte_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
+                               if (real_target_type == TypeManager.byte_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
+                               if (real_target_type == TypeManager.short_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
+                               if (real_target_type == TypeManager.char_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
                        } else if (expr_type == TypeManager.int32_type){
                                //
                                // From int to sbyte, byte, short, ushort, uint, ulong, char
                                //
-                               if (target_type == TypeManager.sbyte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
-                               if (target_type == TypeManager.byte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
-                               if (target_type == TypeManager.short_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
-                               if (target_type == TypeManager.ushort_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
-                               if (target_type == TypeManager.uint32_type)
-                                       return new EmptyCast (expr, target_type);
-                               if (target_type == TypeManager.uint64_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
-                               if (target_type == TypeManager.char_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                               if (real_target_type == TypeManager.sbyte_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
+                               if (real_target_type == TypeManager.byte_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
+                               if (real_target_type == TypeManager.short_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
+                               if (real_target_type == TypeManager.ushort_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
+                               if (real_target_type == TypeManager.uint32_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
+                               if (real_target_type == TypeManager.uint64_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
+                               if (real_target_type == TypeManager.char_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
                        } else if (expr_type == TypeManager.uint32_type){
                                //
                                // From uint to sbyte, byte, short, ushort, int, char
                                //
-                               if (target_type == TypeManager.sbyte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
-                               if (target_type == TypeManager.byte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
-                               if (target_type == TypeManager.short_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
-                               if (target_type == TypeManager.ushort_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
-                               if (target_type == TypeManager.int32_type)
-                                       return new EmptyCast (expr, target_type);
-                               if (target_type == TypeManager.char_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                               if (real_target_type == TypeManager.sbyte_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
+                               if (real_target_type == TypeManager.byte_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
+                               if (real_target_type == TypeManager.short_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
+                               if (real_target_type == TypeManager.ushort_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
+                               if (real_target_type == TypeManager.int32_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
+                               if (real_target_type == TypeManager.char_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
                        } else if (expr_type == TypeManager.int64_type){
                                //
                                // From long to sbyte, byte, short, ushort, int, uint, ulong, char
                                //
-                               if (target_type == TypeManager.sbyte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
-                               if (target_type == TypeManager.byte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
-                               if (target_type == TypeManager.short_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
-                               if (target_type == TypeManager.ushort_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
-                               if (target_type == TypeManager.int32_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
-                               if (target_type == TypeManager.uint32_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
-                               if (target_type == TypeManager.uint64_type)
-                                       return new EmptyCast (expr, target_type);
-                               if (target_type == TypeManager.char_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                               if (real_target_type == TypeManager.sbyte_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
+                               if (real_target_type == TypeManager.byte_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
+                               if (real_target_type == TypeManager.short_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
+                               if (real_target_type == TypeManager.ushort_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
+                               if (real_target_type == TypeManager.int32_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
+                               if (real_target_type == TypeManager.uint32_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
+                               if (real_target_type == TypeManager.uint64_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
+                               if (real_target_type == TypeManager.char_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
                        } else if (expr_type == TypeManager.uint64_type){
                                //
                                // From ulong to sbyte, byte, short, ushort, int, uint, long, char
                                //
-                               if (target_type == TypeManager.sbyte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
-                               if (target_type == TypeManager.byte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
-                               if (target_type == TypeManager.short_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
-                               if (target_type == TypeManager.ushort_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
-                               if (target_type == TypeManager.int32_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
-                               if (target_type == TypeManager.uint32_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
-                               if (target_type == TypeManager.int64_type)
-                                       return new EmptyCast (expr, target_type);
-                               if (target_type == TypeManager.char_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                               if (real_target_type == TypeManager.sbyte_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
+                               if (real_target_type == TypeManager.byte_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
+                               if (real_target_type == TypeManager.short_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
+                               if (real_target_type == TypeManager.ushort_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
+                               if (real_target_type == TypeManager.int32_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
+                               if (real_target_type == TypeManager.uint32_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
+                               if (real_target_type == TypeManager.int64_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
+                               if (real_target_type == TypeManager.char_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
                        } else if (expr_type == TypeManager.char_type){
                                //
                                // From char to sbyte, byte, short
                                //
-                               if (target_type == TypeManager.sbyte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
-                               if (target_type == TypeManager.byte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
-                               if (target_type == TypeManager.short_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
+                               if (real_target_type == TypeManager.sbyte_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
+                               if (real_target_type == TypeManager.byte_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
+                               if (real_target_type == TypeManager.short_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
                        } else if (expr_type == TypeManager.float_type){
                                //
                                // From float to sbyte, byte, short,
                                // ushort, int, uint, long, ulong, char
                                // or decimal
                                //
-                               if (target_type == TypeManager.sbyte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
-                               if (target_type == TypeManager.byte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
-                               if (target_type == TypeManager.short_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
-                               if (target_type == TypeManager.ushort_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
-                               if (target_type == TypeManager.int32_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
-                               if (target_type == TypeManager.uint32_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
-                               if (target_type == TypeManager.int64_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
-                               if (target_type == TypeManager.uint64_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
-                               if (target_type == TypeManager.char_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
-                               if (target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
+                               if (real_target_type == TypeManager.sbyte_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
+                               if (real_target_type == TypeManager.byte_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
+                               if (real_target_type == TypeManager.short_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
+                               if (real_target_type == TypeManager.ushort_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
+                               if (real_target_type == TypeManager.int32_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
+                               if (real_target_type == TypeManager.uint32_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
+                               if (real_target_type == TypeManager.int64_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
+                               if (real_target_type == TypeManager.uint64_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
+                               if (real_target_type == TypeManager.char_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
                        } else if (expr_type == TypeManager.double_type){
                                //
                                // From double to byte, byte, short,
                                // ushort, int, uint, long, ulong,
                                // char, float or decimal
                                //
-                               if (target_type == TypeManager.sbyte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
-                               if (target_type == TypeManager.byte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
-                               if (target_type == TypeManager.short_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
-                               if (target_type == TypeManager.ushort_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
-                               if (target_type == TypeManager.int32_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
-                               if (target_type == TypeManager.uint32_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
-                               if (target_type == TypeManager.int64_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
-                               if (target_type == TypeManager.uint64_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
-                               if (target_type == TypeManager.char_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
-                               if (target_type == TypeManager.float_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
-                               if (target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
+                               if (real_target_type == TypeManager.sbyte_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
+                               if (real_target_type == TypeManager.byte_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
+                               if (real_target_type == TypeManager.short_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
+                               if (real_target_type == TypeManager.ushort_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
+                               if (real_target_type == TypeManager.int32_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
+                               if (real_target_type == TypeManager.uint32_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
+                               if (real_target_type == TypeManager.int64_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
+                               if (real_target_type == TypeManager.uint64_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
+                               if (real_target_type == TypeManager.char_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
+                               if (real_target_type == TypeManager.float_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
                        } 
 
                        // decimal is taken care of by the op_Explicit methods.
@@ -1386,7 +2057,7 @@ namespace Mono.CSharp {
                ///  Returns whether an explicit reference conversion can be performed
                ///  from source_type to target_type
                /// </summary>
-               static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
+               public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
                {
                        bool target_is_value_type = target_type.IsValueType;
                        
@@ -1414,11 +2085,11 @@ namespace Mono.CSharp {
                        }
                            
                        //
-                       // From any class type S to any interface T, provides S is not sealed
+                       // From any class type S to any interface T, provided S is not sealed
                        // and provided S does not implement T.
                        //
                        if (target_type.IsInterface && !source_type.IsSealed &&
-                           !target_type.IsAssignableFrom (source_type))
+                           !TypeManager.ImplementsInterface (source_type, target_type))
                                return true;
 
                        //
@@ -1426,9 +2097,10 @@ namespace Mono.CSharp {
                        // sealed, or provided T implements S.
                        //
                        if (source_type.IsInterface &&
-                           (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
+                           (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
                                return true;
-
+                       
+                       
                        // From an array type S with an element type Se to an array type T with an 
                        // element type Te provided all the following are true:
                        //     * S and T differe only in element type, in other words, S and T
@@ -1452,7 +2124,7 @@ namespace Mono.CSharp {
 
                        // From System.Array to any array-type
                        if (source_type == TypeManager.array_type &&
-                           target_type.IsSubclassOf (TypeManager.array_type)){
+                           target_type.IsArray){
                                return true;
                        }
 
@@ -1481,7 +2153,7 @@ namespace Mono.CSharp {
                {
                        Type source_type = source.Type;
                        bool target_is_value_type = target_type.IsValueType;
-                       
+
                        //
                        // From object to any reference type
                        //
@@ -1499,9 +2171,6 @@ namespace Mono.CSharp {
                        // From any interface type S to any interface T provided S is not derived from T
                        //
                        if (source_type.IsInterface && target_type.IsInterface){
-
-                               Type [] ifaces = source_type.GetInterfaces ();
-
                                if (TypeManager.ImplementsInterface (source_type, target_type))
                                        return null;
                                else
@@ -1513,7 +2182,6 @@ namespace Mono.CSharp {
                        // and provided S does not implement T.
                        //
                        if (target_type.IsInterface && !source_type.IsSealed) {
-                               
                                if (TypeManager.ImplementsInterface (source_type, target_type))
                                        return null;
                                else
@@ -1526,11 +2194,7 @@ namespace Mono.CSharp {
                        // sealed, or provided T implements S.
                        //
                        if (source_type.IsInterface) {
-
-                               if (target_type.IsSealed)
-                                       return null;
-                               
-                               if (TypeManager.ImplementsInterface (target_type, source_type))
+                               if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
                                        return new ClassCast (source, target_type);
                                else
                                        return null;
@@ -1559,7 +2223,7 @@ namespace Mono.CSharp {
 
                        // From System.Array to any array-type
                        if (source_type == TypeManager.array_type &&
-                           target_type.IsSubclassOf (TypeManager.array_type)){
+                           target_type.IsArray) {
                                return new ClassCast (source, target_type);
                        }
 
@@ -1589,44 +2253,110 @@ namespace Mono.CSharp {
                                                          Type target_type, Location loc)
                {
                        Type expr_type = expr.Type;
+                       Type original_expr_type = expr_type;
+
+                       if (expr_type.IsSubclassOf (TypeManager.enum_type)){
+                               if (target_type == TypeManager.enum_type ||
+                                   target_type == TypeManager.object_type) {
+                                       if (expr is EnumConstant)
+                                               expr = ((EnumConstant) expr).Child;
+                                       // We really need all these casts here .... :-(
+                                       expr = new BoxedCast (new EmptyCast (expr, expr_type));
+                                       return new EmptyCast (expr, target_type);
+                               } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
+                                          target_type.IsSubclassOf (TypeManager.enum_type))
+                                       return new UnboxCast (expr, target_type);
+
+                               //
+                               // Notice that we have kept the expr_type unmodified, which is only
+                               // used later on to 
+                               if (expr is EnumConstant)
+                                       expr = ((EnumConstant) expr).Child;
+                               else
+                                       expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
+                               expr_type = expr.Type;
+                       }
+
                        Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
 
                        if (ne != null)
                                return ne;
 
-                       ne = ConvertNumericExplicit (ec, expr, target_type);
+                       ne = ConvertNumericExplicit (ec, expr, target_type, loc);
                        if (ne != null)
                                return ne;
 
                        //
                        // Unboxing conversion.
                        //
-                       if (expr_type == TypeManager.object_type && target_type.IsValueType)
+                       if (expr_type == TypeManager.object_type && target_type.IsValueType){
+                               if (expr is NullLiteral){
+                                       Report.Error (37, "Cannot convert null to value type `" + TypeManager.CSharpName (expr_type) + "'");
+                                       return null;
+                               }
                                return new UnboxCast (expr, target_type);
-
-                       //
-                       // Enum types
-                       //
-                       if (expr is EnumLiteral) {
-                               Expression e = ((EnumLiteral) expr).Child;
-                               
-                               return ConvertImplicit (ec, e, target_type, loc);
                        }
+
                        
                        ne = ConvertReferenceExplicit (expr, target_type);
                        if (ne != null)
                                return ne;
 
+                       if (ec.InUnsafe){
+                               if (target_type.IsPointer){
+                                       if (expr_type.IsPointer)
+                                               return new EmptyCast (expr, target_type);
+                                       
+                                       if (expr_type == TypeManager.sbyte_type ||
+                                           expr_type == TypeManager.byte_type ||
+                                           expr_type == TypeManager.short_type ||
+                                           expr_type == TypeManager.ushort_type ||
+                                           expr_type == TypeManager.int32_type ||
+                                           expr_type == TypeManager.uint32_type ||
+                                           expr_type == TypeManager.uint64_type ||
+                                           expr_type == TypeManager.int64_type)
+                                               return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
+                               }
+                               if (expr_type.IsPointer){
+                                       if (target_type == TypeManager.sbyte_type ||
+                                           target_type == TypeManager.byte_type ||
+                                           target_type == TypeManager.short_type ||
+                                           target_type == TypeManager.ushort_type ||
+                                           target_type == TypeManager.int32_type ||
+                                           target_type == TypeManager.uint32_type ||
+                                           target_type == TypeManager.uint64_type ||
+                                           target_type == TypeManager.int64_type){
+                                               Expression e = new EmptyCast (expr, TypeManager.uint32_type);
+                                               Expression ci, ce;
+
+                                               ci = ConvertImplicitStandard (ec, e, target_type, loc);
+
+                                               if (ci != null)
+                                                       return ci;
+
+                                               ce = ConvertNumericExplicit (ec, e, target_type, loc);
+                                               if (ce != null)
+                                                       return ce;
+                                               //
+                                               // We should always be able to go from an uint32
+                                               // implicitly or explicitly to the other integral
+                                               // types
+                                               //
+                                               throw new Exception ("Internal compiler error");
+                                       }
+                               }
+                       }
+                       
                        ne = ExplicitUserConversion (ec, expr, target_type, loc);
                        if (ne != null)
                                return ne;
 
-                       error30 (loc, expr_type, target_type);
+                       Error_CannotConvertType (loc, original_expr_type, target_type);
                        return null;
                }
 
                /// <summary>
-               ///   Same as ConverExplicit, only it doesn't include user defined conversions
+               ///   Same as ConvertExplicit, only it doesn't include user defined conversions
                /// </summary>
                static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
                                                                  Type target_type, Location l)
@@ -1636,7 +2366,7 @@ namespace Mono.CSharp {
                        if (ne != null)
                                return ne;
 
-                       ne = ConvertNumericExplicit (ec, expr, target_type);
+                       ne = ConvertNumericExplicit (ec, expr, target_type, l);
                        if (ne != null)
                                return ne;
 
@@ -1644,7 +2374,7 @@ namespace Mono.CSharp {
                        if (ne != null)
                                return ne;
 
-                       error30 (l, expr.Type, target_type);
+                       Error_CannotConvertType (l, expr.Type, target_type);
                        return null;
                }
 
@@ -1678,117 +2408,668 @@ namespace Mono.CSharp {
                /// <summary>
                ///   Reports that we were expecting `expr' to be of class `expected'
                /// </summary>
-               protected void report118 (Location loc, Expression expr, string expected)
+               public void Error118 (string expected)
                {
                        string kind = "Unknown";
                        
-                       if (expr != null)
-                               kind = ExprClassName (expr.ExprClass);
+                       kind = ExprClassName (eclass);
 
-                       Error (118, loc, "Expression denotes a `" + kind +
+                       Error (118, "Expression denotes a `" + kind +
                               "' where a `" + expected + "' was expected");
                }
 
-               /// <summary>
-               ///   This function tries to reduce the expression performing
-               ///   constant folding and common subexpression elimination
-               /// </summary>
-               static public Expression Reduce (EmitContext ec, Expression e)
+               public void Error118 (ResolveFlags flags)
                {
-                       //Console.WriteLine ("Calling reduce");
-                       return e.Reduce (ec);
-               }
-       }
+                       ArrayList valid = new ArrayList (10);
 
-       /// <summary>
-       ///   This is just a base class for expressions that can
-       ///   appear on statements (invocations, object creation,
-       ///   assignments, post/pre increment and decrement).  The idea
-       ///   being that they would support an extra Emition interface that
-       ///   does not leave a result on the stack.
-       /// </summary>
-       public abstract class ExpressionStatement : Expression {
+                       if ((flags & ResolveFlags.VariableOrValue) != 0) {
+                               valid.Add ("variable");
+                               valid.Add ("value");
+                       }
 
-               /// <summary>
-               ///   Requests the expression to be emitted in a `statement'
-               ///   context.  This means that no new value is left on the
-               ///   stack after invoking this method (constrasted with
-               ///   Emit that will always leave a value on the stack).
-               /// </summary>
-               public abstract void EmitStatement (EmitContext ec);
-       }
+                       if ((flags & ResolveFlags.Type) != 0)
+                               valid.Add ("type");
 
-       /// <summary>
-       ///   This kind of cast is used to encapsulate the child
-       ///   whose type is child.Type into an expression that is
-       ///   reported to return "return_type".  This is used to encapsulate
-       ///   expressions which have compatible types, but need to be dealt
-       ///   at higher levels with.
-       ///
-       ///   For example, a "byte" expression could be encapsulated in one
-       ///   of these as an "unsigned int".  The type for the expression
-       ///   would be "unsigned int".
-       ///
-       /// </summary>
-       public class EmptyCast : Expression {
-               protected Expression child;
+                       if ((flags & ResolveFlags.MethodGroup) != 0)
+                               valid.Add ("method group");
 
-               public EmptyCast (Expression child, Type return_type)
-               {
-                       ExprClass = child.ExprClass;
-                       type = return_type;
-                       this.child = child;
-               }
+                       if ((flags & ResolveFlags.SimpleName) != 0)
+                               valid.Add ("simple name");
 
-               public override Expression DoResolve (EmitContext ec)
-               {
-                       // This should never be invoked, we are born in fully
-                       // initialized state.
+                       if (valid.Count == 0)
+                               valid.Add ("unknown");
 
-                       return this;
-               }
+                       StringBuilder sb = new StringBuilder ();
+                       for (int i = 0; i < valid.Count; i++) {
+                               if (i > 0)
+                                       sb.Append (", ");
+                               else if (i == valid.Count)
+                                       sb.Append (" or ");
+                               sb.Append (valid [i]);
+                       }
 
-               public override void Emit (EmitContext ec)
+                       string kind = ExprClassName (eclass);
+
+                       Error (119, "Expression denotes a `" + kind + "' where " +
+                              "a `" + sb.ToString () + "' was expected");
+               }
+               
+               static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
                {
-                       child.Emit (ec);
+                       Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
+                                     TypeManager.CSharpName (t));
                }
 
-       }
-
-       /// <summary>
-       ///  This class is used to wrap literals which belong inside Enums
-       /// </summary>
-       public class EnumLiteral : Literal {
-               public Expression Child;
-
-               public EnumLiteral (Expression child, Type enum_type)
+               public static void UnsafeError (Location loc)
                {
-                       ExprClass = child.ExprClass;
-                       this.Child = child;
-                       type = enum_type;
+                       Report.Error (214, loc, "Pointers may only be used in an unsafe context");
                }
                
-               public override Expression DoResolve (EmitContext ec)
+               /// <summary>
+               ///   Converts the IntConstant, UIntConstant, LongConstant or
+               ///   ULongConstant into the integral target_type.   Notice
+               ///   that we do not return an `Expression' we do return
+               ///   a boxed integral type.
+               ///
+               ///   FIXME: Since I added the new constants, we need to
+               ///   also support conversions from CharConstant, ByteConstant,
+               ///   SByteConstant, UShortConstant, ShortConstant
+               ///
+               ///   This is used by the switch statement, so the domain
+               ///   of work is restricted to the literals above, and the
+               ///   targets are int32, uint32, char, byte, sbyte, ushort,
+               ///   short, uint64 and int64
+               /// </summary>
+               public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
                {
-                       // This should never be invoked, we are born in fully
-                       // initialized state.
+                       string s = "";
 
-                       return this;
-               }
+                       if (c.Type == target_type)
+                               return ((Constant) c).GetValue ();
 
-               public override void Emit (EmitContext ec)
-               {
+                       //
+                       // Make into one of the literals we handle, we dont really care
+                       // about this value as we will just return a few limited types
+                       // 
+                       if (c is EnumConstant)
+                               c = ((EnumConstant)c).WidenToCompilerConstant ();
+
+                       if (c is IntConstant){
+                               int v = ((IntConstant) c).Value;
+                               
+                               if (target_type == TypeManager.uint32_type){
+                                       if (v >= 0)
+                                               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.MinValue && v <= SByte.MaxValue)
+                                               return (sbyte) v;
+                               } else if (target_type == TypeManager.short_type){
+                                       if (v >= Int16.MinValue && v <= UInt16.MaxValue)
+                                               return (short) v;
+                               } else if (target_type == TypeManager.ushort_type){
+                                       if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
+                                               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 UIntConstant){
+                               uint v = ((UIntConstant) c).Value;
+
+                               if (target_type == TypeManager.int32_type){
+                                       if (v <= Int32.MaxValue)
+                                               return (int) 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.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 <= UInt16.MaxValue)
+                                               return (short) v;
+                               } else if (target_type == TypeManager.ushort_type){
+                                       if (v <= UInt16.MaxValue)
+                                               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 LongConstant){ 
+                               long v = ((LongConstant) c).Value;
+
+                               if (target_type == TypeManager.int32_type){
+                                       if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
+                                               return (int) v;
+                               } else if (target_type == TypeManager.uint32_type){
+                                       if (v >= 0 && v <= UInt32.MaxValue)
+                                               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.MinValue && v <= SByte.MaxValue)
+                                               return (sbyte) v;
+                               } else if (target_type == TypeManager.short_type){
+                                       if (v >= Int16.MinValue && v <= UInt16.MaxValue)
+                                               return (short) v;
+                               } else if (target_type == TypeManager.ushort_type){
+                                       if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
+                                               return (ushort) v;
+                               } else if (target_type == TypeManager.uint64_type){
+                                       if (v > 0)
+                                               return (ulong) v;
+                               }
+                               s = v.ToString ();
+                       } else if (c is ULongConstant){
+                               ulong v = ((ULongConstant) c).Value;
+
+                               if (target_type == TypeManager.int32_type){
+                                       if (v <= Int32.MaxValue)
+                                               return (int) v;
+                               } else if (target_type == TypeManager.uint32_type){
+                                       if (v <= UInt32.MaxValue)
+                                               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 <= (int) SByte.MaxValue)
+                                               return (sbyte) v;
+                               } else if (target_type == TypeManager.short_type){
+                                       if (v <= UInt16.MaxValue)
+                                               return (short) v;
+                               } else if (target_type == TypeManager.ushort_type){
+                                       if (v <= UInt16.MaxValue)
+                                               return (ushort) v;
+                               } else if (target_type == TypeManager.int64_type){
+                                       if (v <= Int64.MaxValue)
+                                               return (long) v;
+                               }
+                               s = v.ToString ();
+                       } else if (c is ByteConstant){
+                               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){
+                               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){
+                               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){
+                               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 ();
+                       }
+                       Error_ConstantValueCannotBeConverted (loc, s, target_type);
+                       return null;
+               }
+
+               //
+               // Load the object from the pointer.  
+               //
+               public static void LoadFromPtr (ILGenerator ig, Type t)
+               {
+                       if (t == TypeManager.int32_type)
+                               ig.Emit (OpCodes.Ldind_I4);
+                       else if (t == TypeManager.uint32_type)
+                               ig.Emit (OpCodes.Ldind_U4);
+                       else if (t == TypeManager.short_type)
+                               ig.Emit (OpCodes.Ldind_I2);
+                       else if (t == TypeManager.ushort_type)
+                               ig.Emit (OpCodes.Ldind_U2);
+                       else if (t == TypeManager.char_type)
+                               ig.Emit (OpCodes.Ldind_U2);
+                       else if (t == TypeManager.byte_type)
+                               ig.Emit (OpCodes.Ldind_U1);
+                       else if (t == TypeManager.sbyte_type)
+                               ig.Emit (OpCodes.Ldind_I1);
+                       else if (t == TypeManager.uint64_type)
+                               ig.Emit (OpCodes.Ldind_I8);
+                       else if (t == TypeManager.int64_type)
+                               ig.Emit (OpCodes.Ldind_I8);
+                       else if (t == TypeManager.float_type)
+                               ig.Emit (OpCodes.Ldind_R4);
+                       else if (t == TypeManager.double_type)
+                               ig.Emit (OpCodes.Ldind_R8);
+                       else if (t == TypeManager.bool_type)
+                               ig.Emit (OpCodes.Ldind_I1);
+                       else if (t == TypeManager.intptr_type)
+                               ig.Emit (OpCodes.Ldind_I);
+                       else if (TypeManager.IsEnumType (t)) {
+                               if (t == TypeManager.enum_type)
+                                       ig.Emit (OpCodes.Ldind_Ref);
+                               else
+                                       LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
+                       } else if (t.IsValueType)
+                               ig.Emit (OpCodes.Ldobj, t);
+                       else
+                               ig.Emit (OpCodes.Ldind_Ref);
+               }
+
+               //
+               // The stack contains the pointer and the value of type `type'
+               //
+               public static void StoreFromPtr (ILGenerator ig, Type type)
+               {
+                       if (TypeManager.IsEnumType (type))
+                               type = TypeManager.EnumToUnderlying (type);
+                       if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
+                               ig.Emit (OpCodes.Stind_I4);
+                       else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
+                               ig.Emit (OpCodes.Stind_I8);
+                       else if (type == TypeManager.char_type || type == TypeManager.short_type ||
+                                type == TypeManager.ushort_type)
+                               ig.Emit (OpCodes.Stind_I2);
+                       else if (type == TypeManager.float_type)
+                               ig.Emit (OpCodes.Stind_R4);
+                       else if (type == TypeManager.double_type)
+                               ig.Emit (OpCodes.Stind_R8);
+                       else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
+                                type == TypeManager.bool_type)
+                               ig.Emit (OpCodes.Stind_I1);
+                       else if (type == TypeManager.intptr_type)
+                               ig.Emit (OpCodes.Stind_I);
+                       else if (type.IsValueType)
+                               ig.Emit (OpCodes.Stobj, type);
+                       else
+                               ig.Emit (OpCodes.Stind_Ref);
+               }
+               
+               //
+               // Returns the size of type `t' if known, otherwise, 0
+               //
+               public static int GetTypeSize (Type t)
+               {
+                       t = TypeManager.TypeToCoreType (t);
+                       if (t == TypeManager.int32_type ||
+                           t == TypeManager.uint32_type ||
+                           t == TypeManager.float_type)
+                               return 4;
+                       else if (t == TypeManager.int64_type ||
+                                t == TypeManager.uint64_type ||
+                                t == TypeManager.double_type)
+                               return 8;
+                       else if (t == TypeManager.byte_type ||
+                                t == TypeManager.sbyte_type ||
+                                t == TypeManager.bool_type)    
+                               return 1;
+                       else if (t == TypeManager.short_type ||
+                                t == TypeManager.char_type ||
+                                t == TypeManager.ushort_type)
+                               return 2;
+                       else if (t == TypeManager.decimal_type)
+                               return 16;
+                       else
+                               return 0;
+               }
+
+               //
+               // Default implementation of IAssignMethod.CacheTemporaries
+               //
+               public void CacheTemporaries (EmitContext ec)
+               {
+               }
+
+               static void Error_NegativeArrayIndex (Location loc)
+               {
+                       Report.Error (284, loc, "Can not create array with a negative size");
+               }
+               
+               //
+               // Converts `source' to an int, uint, long or ulong.
+               //
+               public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
+               {
+                       Expression target;
+                       
+                       bool old_checked = ec.CheckState;
+                       ec.CheckState = true;
+                       
+                       target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
+                       if (target == null){
+                               target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
+                               if (target == null){
+                                       target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
+                                       if (target == null){
+                                               target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
+                                               if (target == null)
+                                                       Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
+                                       }
+                               }
+                       } 
+                       ec.CheckState = old_checked;
+
+                       //
+                       // Only positive constants are allowed at compile time
+                       //
+                       if (target is Constant){
+                               if (target is IntConstant){
+                                       if (((IntConstant) target).Value < 0){
+                                               Error_NegativeArrayIndex (loc);
+                                               return null;
+                                       }
+                               }
+
+                               if (target is LongConstant){
+                                       if (((LongConstant) target).Value < 0){
+                                               Error_NegativeArrayIndex (loc);
+                                               return null;
+                                       }
+                               }
+                               
+                       }
+
+                       return target;
+               }
+               
+       }
+
+       /// <summary>
+       ///   This is just a base class for expressions that can
+       ///   appear on statements (invocations, object creation,
+       ///   assignments, post/pre increment and decrement).  The idea
+       ///   being that they would support an extra Emition interface that
+       ///   does not leave a result on the stack.
+       /// </summary>
+       public abstract class ExpressionStatement : Expression {
+
+               /// <summary>
+               ///   Requests the expression to be emitted in a `statement'
+               ///   context.  This means that no new value is left on the
+               ///   stack after invoking this method (constrasted with
+               ///   Emit that will always leave a value on the stack).
+               /// </summary>
+               public abstract void EmitStatement (EmitContext ec);
+       }
+
+       /// <summary>
+       ///   This kind of cast is used to encapsulate the child
+       ///   whose type is child.Type into an expression that is
+       ///   reported to return "return_type".  This is used to encapsulate
+       ///   expressions which have compatible types, but need to be dealt
+       ///   at higher levels with.
+       ///
+       ///   For example, a "byte" expression could be encapsulated in one
+       ///   of these as an "unsigned int".  The type for the expression
+       ///   would be "unsigned int".
+       ///
+       /// </summary>
+       public class EmptyCast : Expression {
+               protected Expression child;
+               
+               public EmptyCast (Expression child, Type return_type)
+               {
+                       eclass = child.eclass;
+                       type = return_type;
+                       this.child = child;
+               }
+
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       // This should never be invoked, we are born in fully
+                       // initialized state.
+
+                       return this;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       child.Emit (ec);
+               }
+       }
+
+       /// <summary>
+       ///  This class is used to wrap literals which belong inside Enums
+       /// </summary>
+       public class EnumConstant : Constant {
+               public Constant Child;
+
+               public EnumConstant (Constant child, Type enum_type)
+               {
+                       eclass = child.eclass;
+                       this.Child = child;
+                       type = enum_type;
+               }
+               
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       // This should never be invoked, we are born in fully
+                       // initialized state.
+
+                       return this;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
                        Child.Emit (ec);
                }
 
                public override object GetValue ()
                {
-                       return ((Literal) Child).GetValue ();
+                       return Child.GetValue ();
                }
 
+               //
+               // Converts from one of the valid underlying types for an enumeration
+               // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
+               // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
+               //
+               public Constant WidenToCompilerConstant ()
+               {
+                       Type t = TypeManager.EnumToUnderlying (Child.Type);
+                       object v = ((Constant) Child).GetValue ();;
+                       
+                       if (t == TypeManager.int32_type)
+                               return new IntConstant ((int) v);
+                       if (t == TypeManager.uint32_type)
+                               return new UIntConstant ((uint) v);
+                       if (t == TypeManager.int64_type)
+                               return new LongConstant ((long) v);
+                       if (t == TypeManager.uint64_type)
+                               return new ULongConstant ((ulong) v);
+                       if (t == TypeManager.short_type)
+                               return new ShortConstant ((short) v);
+                       if (t == TypeManager.ushort_type)
+                               return new UShortConstant ((ushort) v);
+                       if (t == TypeManager.byte_type)
+                               return new ByteConstant ((byte) v);
+                       if (t == TypeManager.sbyte_type)
+                               return new SByteConstant ((sbyte) v);
+
+                       throw new Exception ("Invalid enumeration underlying type: " + t);
+               }
+
+               //
+               // Extracts the value in the enumeration on its native representation
+               //
+               public object GetPlainValue ()
+               {
+                       Type t = TypeManager.EnumToUnderlying (Child.Type);
+                       object v = ((Constant) Child).GetValue ();;
+                       
+                       if (t == TypeManager.int32_type)
+                               return (int) v;
+                       if (t == TypeManager.uint32_type)
+                               return (uint) v;
+                       if (t == TypeManager.int64_type)
+                               return (long) v;
+                       if (t == TypeManager.uint64_type)
+                               return (ulong) v;
+                       if (t == TypeManager.short_type)
+                               return (short) v;
+                       if (t == TypeManager.ushort_type)
+                               return (ushort) v;
+                       if (t == TypeManager.byte_type)
+                               return (byte) v;
+                       if (t == TypeManager.sbyte_type)
+                               return (sbyte) v;
+
+                       return null;
+               }
+               
                public override string AsString ()
                {
-                       return ((Literal) Child).AsString ();
+                       return Child.AsString ();
+               }
+
+               public override DoubleConstant ConvertToDouble ()
+               {
+                       return Child.ConvertToDouble ();
+               }
+
+               public override FloatConstant ConvertToFloat ()
+               {
+                       return Child.ConvertToFloat ();
+               }
+
+               public override ULongConstant ConvertToULong ()
+               {
+                       return Child.ConvertToULong ();
+               }
+
+               public override LongConstant ConvertToLong ()
+               {
+                       return Child.ConvertToLong ();
+               }
+
+               public override UIntConstant ConvertToUInt ()
+               {
+                       return Child.ConvertToUInt ();
+               }
+
+               public override IntConstant ConvertToInt ()
+               {
+                       return Child.ConvertToInt ();
                }
        }
 
@@ -1801,10 +3082,10 @@ namespace Mono.CSharp {
        public class BoxedCast : EmptyCast {
 
                public BoxedCast (Expression expr)
-                       : base (expr, TypeManager.object_type)
+                       : base (expr, TypeManager.object_type) 
                {
                }
-
+               
                public override Expression DoResolve (EmitContext ec)
                {
                        // This should never be invoked, we are born in fully
@@ -1816,6 +3097,7 @@ namespace Mono.CSharp {
                public override void Emit (EmitContext ec)
                {
                        base.Emit (ec);
+                       
                        ec.ig.Emit (OpCodes.Box, child.Type);
                }
        }
@@ -1842,45 +3124,222 @@ namespace Mono.CSharp {
                        base.Emit (ec);
                        ig.Emit (OpCodes.Unbox, t);
 
-                       //
-                       // Load the object from the pointer
-                       //
-                       if (t == TypeManager.int32_type)
-                               ig.Emit (OpCodes.Ldind_I4);
-                       else if (t == TypeManager.uint32_type)
-                               ig.Emit (OpCodes.Ldind_U4);
-                       else if (t == TypeManager.short_type)
-                               ig.Emit (OpCodes.Ldind_I2);
-                       else if (t == TypeManager.ushort_type)
-                               ig.Emit (OpCodes.Ldind_U2);
-                       else if (t == TypeManager.char_type)
-                               ig.Emit (OpCodes.Ldind_U2);
-                       else if (t == TypeManager.byte_type)
-                               ig.Emit (OpCodes.Ldind_U1);
-                       else if (t == TypeManager.sbyte_type)
-                               ig.Emit (OpCodes.Ldind_I1);
-                       else if (t == TypeManager.uint64_type)
-                               ig.Emit (OpCodes.Ldind_I8);
-                       else if (t == TypeManager.int64_type)
-                               ig.Emit (OpCodes.Ldind_I8);
-                       else if (t == TypeManager.float_type)
-                               ig.Emit (OpCodes.Ldind_R4);
-                       else if (t == TypeManager.double_type)
-                               ig.Emit (OpCodes.Ldind_R8);
-                       else if (t == TypeManager.bool_type)
-                               ig.Emit (OpCodes.Ldind_I1);
-                       else if (t == TypeManager.intptr_type)
-                               ig.Emit (OpCodes.Ldind_I);
-                       else 
-                               ig.Emit (OpCodes.Ldobj, t);
+                       LoadFromPtr (ig, t);
                }
        }
        
        /// <summary>
-       ///   This kind of cast is used to encapsulate a child expression
-       ///   that can be trivially converted to a target type using one or 
-       ///   two opcodes.  The opcodes are passed as arguments.
+       ///   This is used to perform explicit numeric conversions.
+       ///
+       ///   Explicit numeric conversions might trigger exceptions in a checked
+       ///   context, so they should generate the conv.ovf opcodes instead of
+       ///   conv opcodes.
        /// </summary>
+       public class ConvCast : EmptyCast {
+               public enum Mode : byte {
+                       I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
+                       U1_I1, U1_CH,
+                       I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
+                       U2_I1, U2_U1, U2_I2, U2_CH,
+                       I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
+                       U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
+                       I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
+                       U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
+                       CH_I1, CH_U1, CH_I2,
+                       R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
+                       R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
+               }
+
+               Mode mode;
+               bool checked_state;
+               
+               public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
+                       : base (child, return_type)
+               {
+                       checked_state = ec.CheckState;
+                       mode = m;
+               }
+
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       // This should never be invoked, we are born in fully
+                       // initialized state.
+
+                       return this;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       ILGenerator ig = ec.ig;
+                       
+                       base.Emit (ec);
+
+                       if (checked_state){
+                               switch (mode){
+                               case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
+                               case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
+                               case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
+                               case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
+                               case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
+
+                               case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
+                               case Mode.U1_CH: /* nothing */ break;
+
+                               case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
+                               case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
+                               case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
+                               case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
+                               case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
+                               case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
+
+                               case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
+                               case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
+                               case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
+                               case Mode.U2_CH: /* nothing */ break;
+
+                               case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
+                               case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
+                               case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
+                               case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
+                               case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
+                               case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
+                               case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
+
+                               case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
+                               case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
+                               case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
+                               case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
+                               case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
+                               case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
+
+                               case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
+                               case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
+                               case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
+                               case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
+                               case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
+                               case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
+                               case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
+                               case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
+
+                               case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
+                               case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
+                               case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
+                               case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
+                               case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
+                               case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
+                               case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
+                               case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
+
+                               case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
+                               case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
+                               case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
+
+                               case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
+                               case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
+                               case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
+                               case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
+                               case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
+                               case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
+                               case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
+                               case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
+                               case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
+
+                               case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
+                               case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
+                               case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
+                               case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
+                               case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
+                               case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
+                               case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
+                               case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
+                               case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
+                               case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
+                               }
+                       } else {
+                               switch (mode){
+                               case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
+                               case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
+                               case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
+                               case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
+                               case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
+
+                               case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
+                               case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
+
+                               case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
+                               case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
+                               case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
+                               case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
+                               case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
+                               case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
+
+                               case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
+                               case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
+                               case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
+                               case Mode.U2_CH: /* nothing */ break;
+
+                               case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
+                               case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
+                               case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
+                               case Mode.I4_U4: /* nothing */ break;
+                               case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
+                               case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
+                               case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
+
+                               case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
+                               case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
+                               case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
+                               case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
+                               case Mode.U4_I4: /* nothing */ break;
+                               case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
+
+                               case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
+                               case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
+                               case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
+                               case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
+                               case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
+                               case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
+                               case Mode.I8_U8: /* nothing */ break;
+                               case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
+
+                               case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
+                               case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
+                               case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
+                               case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
+                               case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
+                               case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
+                               case Mode.U8_I8: /* nothing */ break;
+                               case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
+
+                               case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
+                               case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
+                               case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
+
+                               case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
+                               case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
+                               case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
+                               case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
+                               case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
+                               case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
+                               case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
+                               case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
+                               case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
+
+                               case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
+                               case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
+                               case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
+                               case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
+                               case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
+                               case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
+                               case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
+                               case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
+                               case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
+                               case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
+                               }
+                       }
+               }
+       }
+       
        public class OpcodeCast : EmptyCast {
                OpCode op, op2;
                bool second_valid;
@@ -1918,7 +3377,6 @@ namespace Mono.CSharp {
                        if (second_valid)
                                ec.ig.Emit (op2);
                }                       
-               
        }
 
        /// <summary>
@@ -1977,48 +3435,53 @@ namespace Mono.CSharp {
        ///   The downside of this is that we might be hitting `LookupType' too many
        ///   times with this scheme.
        /// </remarks>
-       public class SimpleName : Expression {
+       public class SimpleName : Expression, ITypeExpression {
                public readonly string Name;
-               public readonly Location Location;
+
+               //
+               // If true, then we are a simple name, not composed with a ".
+               //
+               bool is_base;
+
+               public SimpleName (string a, string b, Location l)
+               {
+                       Name = String.Concat (a, ".", b);
+                       loc = l;
+                       is_base = false;
+               }
                
                public SimpleName (string name, Location l)
                {
                        Name = name;
-                       Location = l;
+                       loc = l;
+                       is_base = true;
                }
 
-               public static void Error120 (Location l, string name)
+               public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
                {
-                       Report.Error (
-                               120, l,
-                               "An object reference is required " +
-                               "for the non-static field `"+name+"'");
+                       if (ec.IsFieldInitializer)
+                               Report.Error (
+                                       236, l,
+                                       "A field initializer cannot reference the non-static field, " +
+                                       "method or property `"+name+"'");
+                       else
+                               Report.Error (
+                                       120, l,
+                                       "An object reference is required " +
+                                       "for the non-static field `"+name+"'");
                }
                
                //
                // Checks whether we are trying to access an instance
                // property, method or field from a static body.
                //
-               Expression MemberStaticCheck (Expression e)
+               Expression MemberStaticCheck (EmitContext ec, Expression e)
                {
-                       if (e is FieldExpr){
-                               FieldInfo fi = ((FieldExpr) e).FieldInfo;
+                       if (e is IMemberExpr){
+                               IMemberExpr member = (IMemberExpr) e;
                                
-                               if (!fi.IsStatic){
-                                       Error120 (Location, Name);
-                                       return null;
-                               }
-                       } else if (e is MethodGroupExpr){
-                               MethodGroupExpr mg = (MethodGroupExpr) e;
-
-                               if (!mg.RemoveInstanceMethods ()){
-                                       Error120 (Location, mg.Methods [0].Name);
-                                       return null;
-                               }
-                               return e;
-                       } else if (e is PropertyExpr){
-                               if (!((PropertyExpr) e).IsStatic){
-                                       Error120 (Location, Name);
+                               if (!member.IsStatic){
+                                       Error_ObjectRefRequired (ec, loc, Name);
                                        return null;
                                }
                        }
@@ -2026,68 +3489,196 @@ namespace Mono.CSharp {
                        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)
                {
-                       Expression e;
+                       return SimpleNameResolve (ec, null, false);
+               }
+
+               public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
+               {
+                       return SimpleNameResolve (ec, right_side, false);
+               }
+               
+
+               public Expression DoResolveAllowStatic (EmitContext ec)
+               {
+                       return SimpleNameResolve (ec, null, true);
+               }
+
+               public Expression DoResolveType (EmitContext ec)
+               {
+                       DeclSpace ds = ec.DeclSpace;
+                       Namespace ns = ds.Namespace;
+                       Type t;
+                       string alias_value;
 
                        //
-                       // Stage 1: Performed by the parser (binding to local or parameters).
+                       // Since we are cheating: we only do the Alias lookup for
+                       // namespaces if the name does not include any dots in it
                        //
+                       if (ns != null && is_base)
+                               alias_value = ns.LookupAlias (Name);
+                       else
+                               alias_value = null;
+                               
+                       if (ec.ResolvingTypeTree){
+                               if (alias_value != null){
+                                       if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
+                                               return new TypeExpr (t, loc);
+                               }
+                               
+                               int errors = Report.Errors;
+                               Type dt = ec.DeclSpace.FindType (loc, Name);
+                               if (Report.Errors != errors)
+                                       return null;
+                               
+                               if (dt != null)
+                                       return new TypeExpr (dt, loc);
+                       }
 
                        //
-                       // Stage 2: Lookup members
+                       // First, the using aliases
                        //
-                       e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, true, Location);
-                       if (e == null){
-                               //
-                               // Stage 3: Lookup symbol in the various namespaces. 
-                               // 
-                               Type t;
+                       if (alias_value != null){
+                               if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
+                                       return new TypeExpr (t, loc);
                                
-                               if ((t = ec.TypeContainer.LookupType (Name, true)) != null)
-                                       return new TypeExpr (t);
-
-                               //
-                               // Stage 3 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
-                               //
+                               // we have alias value, but it isn't Type, so try if it's namespace
+                               return new SimpleName (alias_value, loc);
+                       }
+                       
+                       //
+                       // Stage 2: Lookup up if we are an alias to a type
+                       // or a namespace.
+                       //
                                
-                               // IMPLEMENT ME.  Read mcs/mcs/TODO for ideas, or rewrite
-                               // using NamespaceExprs (dunno how that fixes the alias
-                               // per-file though).
+                       if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
+                               return new TypeExpr (t, loc);
                                
-                               // No match, maybe our parent can compose us
-                               // into something meaningful.
-                               //
-                               return this;
+                       // No match, maybe our parent can compose us
+                       // into something meaningful.
+                       return this;
+               }
+
+               /// <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, Expression right_side, bool allow_static)
+               {
+                       Expression e = null;
+
+                       //
+                       // Stage 1: Performed by the parser (binding to locals or parameters).
+                       //
+                       Block current_block = ec.CurrentBlock;
+                       if (current_block != null && current_block.GetVariableInfo (Name) != null){
+                               LocalVariableReference var;
+
+                               var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
+
+                               if (right_side != null)
+                                       return var.ResolveLValue (ec, right_side);
+                               else
+                                       return var.Resolve (ec);
+                       }
+
+                       if (current_block != null){
+                               int idx = -1;
+                               Parameter par = null;
+                               Parameters pars = current_block.Parameters;
+                               if (pars != null)
+                                       par = pars.GetParameterByName (Name, out idx);
+
+                               if (par != null) {
+                                       ParameterReference param;
+                                       
+                                       param = new ParameterReference (pars, idx, Name, loc);
+
+                                       if (right_side != null)
+                                               return param.ResolveLValue (ec, right_side);
+                                       else
+                                               return param.Resolve (ec);
+                               }
                        }
 
-                       // Step 2, continues here.
+                       //
+                       // Stage 2: Lookup members 
+                       //
+
+                       DeclSpace lookup_ds = ec.DeclSpace;
+                       do {
+                               if (lookup_ds.TypeBuilder == null)
+                                       break;
+
+                               e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
+                               if (e != null)
+                                       break;
+
+                               lookup_ds =lookup_ds.Parent;
+                       } while (lookup_ds != null);
+                               
+                       if (e == null && ec.ContainerType != null)
+                               e = MemberLookup (ec, ec.ContainerType, Name, loc);
+
+                       if (e == null)
+                               return DoResolveType (ec);
+
                        if (e is TypeExpr)
                                return e;
 
-                       if (e is FieldExpr){
-                               FieldExpr fe = (FieldExpr) e;
-                               
-                               if (!fe.FieldInfo.IsStatic)
-                                       fe.InstanceExpression = new This (Location.Null);
-                       }                               
+                       if (e is IMemberExpr) {
+                               e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
+                               if (e == null)
+                                       return null;
 
-                       if (ec.IsStatic)
-                               return MemberStaticCheck (e);
-                       else
+                               IMemberExpr me = e as IMemberExpr;
+                               if (me == null)
+                                       return e;
+
+                               // This fails if ResolveMemberAccess() was unable to decide whether
+                               // it's a field or a type of the same name.
+                               if (!me.IsStatic && (me.InstanceExpression == null))
+                                       return e;
+
+                               if (!me.IsStatic &&
+                                   TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType)) {
+                                       Error (38, "Cannot access nonstatic member `" + me.Name + "' of " +
+                                              "outer type `" + me.DeclaringType + "' via nested type `" +
+                                              me.InstanceExpression.Type + "'");
+                                       return null;
+                               }
+
+                               if (right_side != null)
+                                       e = e.DoResolveLValue (ec, right_side);
+                               else
+                                       e = e.DoResolve (ec);
+
+                               return e;                               
+                       }
+
+                       if (ec.IsStatic || ec.IsFieldInitializer){
+                               if (allow_static)
+                                       return e;
+
+                               return MemberStaticCheck (ec, e);
+                       } else
                                return e;
                }
-
+               
                public override void Emit (EmitContext ec)
                {
                        //
@@ -2095,20 +3686,31 @@ namespace Mono.CSharp {
                        // find the name as a namespace
                        //
 
-                       Error (103, Location, "The name `" + Name +
+                       Error (103, "The name `" + Name +
                               "' does not exist in the class `" +
-                              ec.TypeContainer.Name + "'");
+                              ec.DeclSpace.Name + "'");
+               }
+
+               public override string ToString ()
+               {
+                       return Name;
                }
        }
        
        /// <summary>
        ///   Fully resolved expression that evaluates to a type
        /// </summary>
-       public class TypeExpr : Expression {
-               public TypeExpr (Type t)
+       public class TypeExpr : Expression, ITypeExpression {
+               public TypeExpr (Type t, Location l)
                {
                        Type = t;
                        eclass = ExprClass.Type;
+                       loc = l;
+               }
+
+               public virtual Expression DoResolveType (EmitContext ec)
+               {
+                       return this;
                }
 
                override public Expression DoResolve (EmitContext ec)
@@ -2118,7 +3720,48 @@ namespace Mono.CSharp {
 
                override public void Emit (EmitContext ec)
                {
-                       throw new Exception ("Implement me");
+                       throw new Exception ("Should never be called");
+               }
+
+               public override string ToString ()
+               {
+                       return Type.ToString ();
+               }
+       }
+
+       /// <summary>
+       ///   Used to create types from a fully qualified name.  These are just used
+       ///   by the parser to setup the core types.  A TypeLookupExpression is always
+       ///   classified as a type.
+       /// </summary>
+       public class TypeLookupExpression : TypeExpr {
+               string name;
+               
+               public TypeLookupExpression (string name) : base (null, Location.Null)
+               {
+                       this.name = name;
+               }
+
+               public override Expression DoResolveType (EmitContext ec)
+               {
+                       if (type == null)
+                               type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
+                       return this;
+               }
+
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       return DoResolveType (ec);
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       throw new Exception ("Should never be called");
+               }
+
+               public override string ToString ()
+               {
+                       return name;
                }
        }
 
@@ -2127,23 +3770,44 @@ namespace Mono.CSharp {
        ///  
        ///   This is a fully resolved expression that evaluates to a type
        /// </summary>
-       public class MethodGroupExpr : Expression {
+       public class MethodGroupExpr : Expression, IMemberExpr {
                public MethodBase [] Methods;
                Expression instance_expression = null;
+               bool is_explicit_impl = false;
                
-               public MethodGroupExpr (MemberInfo [] mi)
+               public MethodGroupExpr (MemberInfo [] mi, Location l)
                {
                        Methods = new MethodBase [mi.Length];
                        mi.CopyTo (Methods, 0);
                        eclass = ExprClass.MethodGroup;
+                       type = TypeManager.object_type;
+                       loc = l;
                }
 
-               public MethodGroupExpr (ArrayList l)
+               public MethodGroupExpr (ArrayList list, Location l)
                {
-                       Methods = new MethodBase [l.Count];
-
-                       l.CopyTo (Methods, 0);
+                       Methods = new MethodBase [list.Count];
+
+                       try {
+                               list.CopyTo (Methods, 0);
+                       } catch {
+                               foreach (MemberInfo m in list){
+                                       if (!(m is MethodBase)){
+                                               Console.WriteLine ("Name " + m.Name);
+                                               Console.WriteLine ("Found a: " + m.GetType ().FullName);
+                                       }
+                               }
+                               throw;
+                       }
+                       loc = l;
                        eclass = ExprClass.MethodGroup;
+                       type = TypeManager.object_type;
+               }
+
+               public Type DeclaringType {
+                       get {
+                               return Methods [0].DeclaringType;
+                       }
                }
                
                //
@@ -2154,30 +3818,74 @@ namespace Mono.CSharp {
                                return instance_expression;
                        }
 
-                       set {
-                               instance_expression = value;
+                       set {
+                               instance_expression = value;
+                       }
+               }
+
+               public bool IsExplicitImpl {
+                       get {
+                               return is_explicit_impl;
+                       }
+
+                       set {
+                               is_explicit_impl = value;
+                       }
+               }
+
+               public string Name {
+                       get {
+                               return Methods [0].Name;
+                       }
+               }
+
+               public bool IsInstance {
+                       get {
+                               foreach (MethodBase mb in Methods)
+                                       if (!mb.IsStatic)
+                                               return true;
+
+                               return false;
+                       }
+               }
+
+               public bool IsStatic {
+                       get {
+                               foreach (MethodBase mb in Methods)
+                                       if (mb.IsStatic)
+                                               return true;
+
+                               return false;
                        }
                }
                
                override public Expression DoResolve (EmitContext ec)
                {
+                       if (instance_expression != null) {
+                               instance_expression = instance_expression.DoResolve (ec);
+                               if (instance_expression == null)
+                                       return null;
+                       }
+
                        return this;
                }
 
+               public void ReportUsageError ()
+               {
+                       Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
+                                     Methods [0].Name + "()' is referenced without parentheses");
+               }
+
                override public void Emit (EmitContext ec)
                {
-                       throw new Exception ("This should never be reached");
+                       ReportUsageError ();
                }
 
                bool RemoveMethods (bool keep_static)
                {
                        ArrayList smethods = new ArrayList ();
-                       int top = Methods.Length;
-                       int i;
-                       
-                       for (i = 0; i < top; i++){
-                               MethodBase mb = Methods [i];
 
+                       foreach (MethodBase mb in Methods){
                                if (mb.IsStatic == keep_static)
                                        smethods.Add (mb);
                        }
@@ -2213,10 +3921,9 @@ namespace Mono.CSharp {
        /// <summary>
        ///   Fully resolved expression that evaluates to a Field
        /// </summary>
-       public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
+       public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
                public readonly FieldInfo FieldInfo;
-               public Expression InstanceExpression;
-               Location loc;
+               Expression instance_expr;
                
                public FieldExpr (FieldInfo fi, Location l)
                {
@@ -2226,25 +3933,94 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
+               public string Name {
+                       get {
+                               return FieldInfo.Name;
+                       }
+               }
+
+               public bool IsInstance {
+                       get {
+                               return !FieldInfo.IsStatic;
+                       }
+               }
+
+               public bool IsStatic {
+                       get {
+                               return FieldInfo.IsStatic;
+                       }
+               }
+
+               public Type DeclaringType {
+                       get {
+                               return FieldInfo.DeclaringType;
+                       }
+               }
+
+               public Expression InstanceExpression {
+                       get {
+                               return instance_expr;
+                       }
+
+                       set {
+                               instance_expr = value;
+                       }
+               }
+
                override public Expression DoResolve (EmitContext ec)
                {
                        if (!FieldInfo.IsStatic){
-                               if (InstanceExpression == null){
-                                       throw new Exception ("non-static FieldExpr without instance var\n" +
-                                                            "You have to assign the Instance variable\n" +
-                                                            "Of the FieldExpr to set this\n");
+                               if (instance_expr == null){
+                                       //
+                                       // This can happen when referencing an instance field using
+                                       // a fully qualified type expression: TypeName.InstanceField = xxx
+                                       // 
+                                       SimpleName.Error_ObjectRefRequired (ec, loc, FieldInfo.Name);
+                                       return null;
                                }
 
-                               InstanceExpression = InstanceExpression.Resolve (ec);
-                               if (InstanceExpression == null)
+                               // Resolve the field's instance expression while flow analysis is turned
+                               // off: when accessing a field "a.b", we must check whether the field
+                               // "a.b" is initialized, not whether the whole struct "a" is initialized.
+                               instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
+                                                                      ResolveFlags.DisableFlowAnalysis);
+                               if (instance_expr == null)
                                        return null;
-                               
                        }
+
+                       // If the instance expression is a local variable or parameter.
+                       IVariable var = instance_expr as IVariable;
+                       if ((var != null) && !var.IsFieldAssigned (ec, FieldInfo.Name, loc))
+                               return null;
+
                        return this;
                }
 
-               public Expression DoResolveLValue (EmitContext ec)
+               void Report_AssignToReadonly (bool is_instance)
+               {
+                       string msg;
+                       
+                       if (is_instance)
+                               msg = "Readonly field can not be assigned outside " +
+                               "of constructor or variable initializer";
+                       else
+                               msg = "A static readonly field can only be assigned in " +
+                               "a static constructor";
+
+                       Report.Error (is_instance ? 191 : 198, loc, msg);
+               }
+               
+               override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
                {
+                       IVariable var = instance_expr as IVariable;
+                       if (var != null)
+                               var.SetFieldAssigned (ec, FieldInfo.Name);
+
+                       Expression e = DoResolve (ec);
+
+                       if (e == null)
+                               return null;
+                       
                        if (!FieldInfo.IsInitOnly)
                                return this;
 
@@ -2252,12 +4028,12 @@ namespace Mono.CSharp {
                        // InitOnly fields can only be assigned in constructors
                        //
 
-                       if (ec.IsConstructor)
-                               return this;
+                       if (ec.IsConstructor){
+                               if (ec.ContainerType == FieldInfo.DeclaringType)
+                                       return this;
+                       }
 
-                       Report.Error (191, loc,
-                                     "Readonly field can not be assigned outside " +
-                                     "of constructor or variable initializer");
+                       Report_AssignToReadonly (true);
                        
                        return null;
                }
@@ -2265,11 +4041,43 @@ namespace Mono.CSharp {
                override public void Emit (EmitContext ec)
                {
                        ILGenerator ig = ec.ig;
+                       bool is_volatile = false;
 
-                       if (FieldInfo.IsStatic)
+                       if (FieldInfo is FieldBuilder){
+                               FieldBase f = TypeManager.GetField (FieldInfo);
+
+                               if ((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 (instance_expr.Type.IsValueType){
+                                       IMemoryLocation ml;
+                                       LocalTemporary tempo = null;
+                                       
+                                       if (!(instance_expr is IMemoryLocation)){
+                                               tempo = new LocalTemporary (
+                                                       ec, instance_expr.Type);
+
+                                               InstanceExpression.Emit (ec);
+                                               tempo.Store (ec);
+                                               ml = tempo;
+                                       } else
+                                               ml = (IMemoryLocation) instance_expr;
+
+                                       ml.AddressOf (ec, AddressOp.Load);
+                               } else 
+                                       instance_expr.Emit (ec);
+
+                               if (is_volatile)
+                                       ig.Emit (OpCodes.Volatile);
                                
                                ig.Emit (OpCodes.Ldfld, FieldInfo);
                        }
@@ -2277,26 +4085,99 @@ namespace Mono.CSharp {
 
                public void EmitAssign (EmitContext ec, Expression source)
                {
-                       bool is_static = FieldInfo.IsStatic;
+                       FieldAttributes fa = FieldInfo.Attributes;
+                       bool is_static = (fa & FieldAttributes.Static) != 0;
+                       bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
+                       ILGenerator ig = ec.ig;
 
-                       if (!is_static)
-                               InstanceExpression.Emit (ec);
+                       if (is_readonly && !ec.IsConstructor){
+                               Report_AssignToReadonly (!is_static);
+                               return;
+                       }
+                       
+                       if (!is_static){
+                               Expression instance = instance_expr;
+
+                               if (instance.Type.IsValueType){
+                                       if (instance is IMemoryLocation){
+                                               IMemoryLocation ml = (IMemoryLocation) instance;
+
+                                               ml.AddressOf (ec, AddressOp.Store);
+                                       } else
+                                               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){
+                               FieldBase f = TypeManager.GetField (FieldInfo);
+                               
+                               if ((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){
+                               FieldBase f = TypeManager.GetField (FieldInfo);
+
+                               f.status |= Field.Status.ASSIGNED;
+                       }
                }
                
-               public void AddressOf (EmitContext ec)
+               public void AddressOf (EmitContext ec, AddressOp mode)
                {
+                       ILGenerator ig = ec.ig;
+                       
+                       if (FieldInfo is FieldBuilder){
+                               FieldBase f = TypeManager.GetField (FieldInfo);
+                               if ((f.ModFlags & Modifiers.VOLATILE) != 0)
+                                       ig.Emit (OpCodes.Volatile);
+                       }
+
+                       if (FieldInfo is FieldBuilder){
+                               FieldBase f = TypeManager.GetField (FieldInfo);
+
+                               if ((mode & AddressOp.Store) != 0)
+                                       f.status |= Field.Status.ASSIGNED;
+                               if ((mode & AddressOp.Load) != 0)
+                                       f.status |= Field.Status.USED;
+                       }
+
+                       //
+                       // Handle initonly fields specially: make a copy and then
+                       // get the address of the copy.
+                       //
+                       if (FieldInfo.IsInitOnly && !ec.IsConstructor){
+                               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);
+                               //
+                               // In the case of `This', we call the AddressOf method, which will
+                               // only load the pointer, and not perform an Ldobj immediately after
+                               // the value has been loaded into the stack.
+                               //
+                               if (instance_expr is This)
+                                       ((This)instance_expr).AddressOf (ec, AddressOp.LoadStore);
+                               else
+                                       instance_expr.Emit (ec);
+                               ig.Emit (OpCodes.Ldflda, FieldInfo);
                        }
                }
        }
@@ -2308,32 +4189,53 @@ namespace Mono.CSharp {
        ///   This is not an LValue because we need to re-write the expression, we
        ///   can not take data from the stack and store it.  
        /// </summary>
-       public class PropertyExpr : ExpressionStatement, IAssignMethod {
+       public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
                public readonly PropertyInfo PropertyInfo;
-               public readonly bool IsStatic;
-               MethodInfo [] Accessors;
-               Location loc;
+
+               //
+               // This is set externally by the  `BaseAccess' class
+               //
+               public bool IsBase;
+               MethodInfo getter, setter;
+               bool is_static;
+               bool must_do_cs1540_check;
                
                Expression instance_expr;
-               
-               public PropertyExpr (PropertyInfo pi, Location l)
+
+               public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
                {
                        PropertyInfo = pi;
                        eclass = ExprClass.PropertyAccess;
-                       IsStatic = false;
+                       is_static = false;
                        loc = l;
-                       Accessors = TypeManager.GetAccessors (pi);
 
-                       if (Accessors != null)
-                               for (int i = 0; i < Accessors.Length; i++){
-                                       if (Accessors [i] != null)
-                                               if (Accessors [i].IsStatic)
-                                                       IsStatic = true;
-                               }
-                       else
-                               Accessors = new MethodInfo [2];
-                       
-                       type = pi.PropertyType;
+                       type = TypeManager.TypeToCoreType (pi.PropertyType);
+
+                       ResolveAccessors (ec);
+               }
+
+               public string Name {
+                       get {
+                               return PropertyInfo.Name;
+                       }
+               }
+
+               public bool IsInstance {
+                       get {
+                               return !is_static;
+                       }
+               }
+
+               public bool IsStatic {
+                       get {
+                               return is_static;
+                       }
+               }
+               
+               public Type DeclaringType {
+                       get {
+                               return PropertyInfo.DeclaringType;
+                       }
                }
 
                //
@@ -2351,7 +4253,7 @@ namespace Mono.CSharp {
 
                public bool VerifyAssignable ()
                {
-                       if (!PropertyInfo.CanWrite){
+                       if (setter == null) {
                                Report.Error (200, loc, 
                                              "The property `" + PropertyInfo.Name +
                                              "' can not be assigned to, as it has not set accessor");
@@ -2361,22 +4263,218 @@ namespace Mono.CSharp {
                        return true;
                }
 
+               MethodInfo GetAccessor (Type invocation_type, string accessor_name)
+               {
+                       BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
+                               BindingFlags.Static | BindingFlags.Instance;
+                       MemberInfo[] group;
+
+                       group = TypeManager.MemberLookup (
+                               invocation_type, invocation_type, PropertyInfo.DeclaringType,
+                               MemberTypes.Method, flags, accessor_name + "_" + PropertyInfo.Name);
+
+                       //
+                       // The first method is the closest to us
+                       //
+                       if (group == null)
+                               return null;
+
+                       foreach (MethodInfo mi in group) {
+                               MethodAttributes ma = mi.Attributes & MethodAttributes.MemberAccessMask;
+
+                               //
+                               // If only accessible to the current class or children
+                               //
+                               if (ma == MethodAttributes.Private) {
+                                       Type declaring_type = mi.DeclaringType;
+                                       
+                                       if (invocation_type != declaring_type){
+                                               if (TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
+                                                       return mi;
+                                               else
+                                                       continue;
+                                       } else
+                                               return mi;
+                               }
+                               //
+                               // FamAndAssem requires that we not only derivate, but we are on the
+                               // same assembly.  
+                               //
+                               if (ma == MethodAttributes.FamANDAssem){
+                                       if (mi.DeclaringType.Assembly != invocation_type.Assembly)
+                                               continue;
+                                       else
+                                               return mi;
+                               }
+
+                               // Assembly and FamORAssem succeed if we're in the same assembly.
+                               if ((ma == MethodAttributes.Assembly) || (ma == MethodAttributes.FamORAssem)){
+                                       if (mi.DeclaringType.Assembly != invocation_type.Assembly)
+                                               continue;
+                                       else
+                                               return mi;
+                               }
+
+                               // We already know that we aren't in the same assembly.
+                               if (ma == MethodAttributes.Assembly)
+                                       continue;
+
+                               // Family and FamANDAssem require that we derive.
+                               if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem)){
+                                       if (!TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
+                                               continue;
+                                       else {
+                                               must_do_cs1540_check = true;
+
+                                               return mi;
+                                       }
+                               }
+
+                               return mi;
+                       }
+
+                       return null;
+               }
+
+               //
+               // We also perform the permission checking here, as the PropertyInfo does not
+               // hold the information for the accessibility of its setter/getter
+               //
+               void ResolveAccessors (EmitContext ec)
+               {
+                       getter = GetAccessor (ec.ContainerType, "get");
+                       if ((getter != null) && getter.IsStatic)
+                               is_static = true;
+
+                       setter = GetAccessor (ec.ContainerType, "set");
+                       if ((setter != null) && setter.IsStatic)
+                               is_static = true;
+
+                       if (setter == null && getter == null){
+                               Error (122, "`" + PropertyInfo.Name + "' " +
+                                      "is inaccessible because of its protection level");
+                               
+                       }
+               }
+
+               bool InstanceResolve (EmitContext ec)
+               {
+                       if ((instance_expr == null) && ec.IsStatic && !is_static) {
+                               SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
+                               return false;
+                       }
+
+                       if (instance_expr != null) {
+                               instance_expr = instance_expr.DoResolve (ec);
+                               if (instance_expr == null)
+                                       return false;
+                       }
+
+                       if (must_do_cs1540_check && (instance_expr != null)) {
+                               if ((instance_expr.Type != ec.ContainerType) &&
+                                   ec.ContainerType.IsSubclassOf (instance_expr.Type)) {
+                                       Report.Error (1540, loc, "Cannot access protected member `" +
+                                                     PropertyInfo.DeclaringType + "." + PropertyInfo.Name + 
+                                                     "' via a qualifier of type `" +
+                                                     TypeManager.CSharpName (instance_expr.Type) +
+                                                     "'; the qualifier must be of type `" +
+                                                     TypeManager.CSharpName (ec.ContainerType) +
+                                                     "' (or derived from it)");
+                                       return false;
+                               }
+                       }
+
+                       return true;
+               }
+               
                override public Expression DoResolve (EmitContext ec)
                {
-                       if (!PropertyInfo.CanRead){
+                       if (getter == null){
+                               //
+                               // The following condition happens if the PropertyExpr was
+                               // created, but is invalid (ie, the property is inaccessible),
+                               // and we did not want to embed the knowledge about this in
+                               // the caller routine.  This only avoids double error reporting.
+                               //
+                               if (setter == null)
+                                       return null;
+                               
                                Report.Error (154, loc, 
                                              "The property `" + PropertyInfo.Name +
                                              "' can not be used in " +
                                              "this context because it lacks a get accessor");
                                return null;
+                       } 
+
+                       if (!InstanceResolve (ec))
+                               return null;
+
+                       //
+                       // Only base will allow this invocation to happen.
+                       //
+                       if (IsBase && getter.IsAbstract){
+                               Report.Error (205, loc, "Cannot call an abstract base property: " +
+                                             PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
+                               return null;
+                       }
+
+                       return this;
+               }
+
+               override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
+               {
+                       if (setter == null){
+                               //
+                               // The following condition happens if the PropertyExpr was
+                               // created, but is invalid (ie, the property is inaccessible),
+                               // and we did not want to embed the knowledge about this in
+                               // the caller routine.  This only avoids double error reporting.
+                               //
+                               if (getter == null)
+                                       return null;
+                               
+                               Report.Error (154, loc, 
+                                             "The property `" + PropertyInfo.Name +
+                                             "' can not be used in " +
+                                             "this context because it lacks a set accessor");
+                               return null;
                        }
 
+                       if (!InstanceResolve (ec))
+                               return null;
+                       
+                       //
+                       // Only base will allow this invocation to happen.
+                       //
+                       if (IsBase && setter.IsAbstract){
+                               Report.Error (205, loc, "Cannot call an abstract base property: " +
+                                             PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
+                               return null;
+                       }
                        return this;
                }
 
                override public void Emit (EmitContext ec)
                {
-                       Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [0], null);
+                       //
+                       // Special case: length of single dimension array property is turned into ldlen
+                       //
+                       if ((getter == TypeManager.system_int_array_get_length) ||
+                           (getter == TypeManager.int_array_get_length)){
+                               Type iet = instance_expr.Type;
+
+                               //
+                               // System.Array.Length can be called, but the Type does not
+                               // support invoking GetArrayRank, so test for that case first
+                               //
+                               if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
+                                       instance_expr.Emit (ec);
+                                       ec.ig.Emit (OpCodes.Ldlen);
+                                       return;
+                               }
+                       }
+
+                       Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, loc);
                        
                }
 
@@ -2389,7 +4487,7 @@ namespace Mono.CSharp {
                        ArrayList args = new ArrayList ();
 
                        args.Add (arg);
-                       Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [1], args);
+                       Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, loc);
                }
 
                override public void EmitStatement (EmitContext ec)
@@ -2400,30 +4498,101 @@ 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 class EventExpr : Expression, IMemberExpr {
                public readonly EventInfo EventInfo;
-               Location loc;
+               public Expression instance_expr;
+
+               bool is_static;
+               MethodInfo add_accessor, remove_accessor;
                
                public EventExpr (EventInfo ei, Location loc)
                {
                        EventInfo = ei;
                        this.loc = loc;
                        eclass = ExprClass.EventAccess;
+
+                       add_accessor = TypeManager.GetAddMethod (ei);
+                       remove_accessor = TypeManager.GetRemoveMethod (ei);
+                       
+                       if (add_accessor.IsStatic || remove_accessor.IsStatic)
+                               is_static = true;
+
+                       if (EventInfo is MyEventBuilder){
+                               MyEventBuilder eb = (MyEventBuilder) EventInfo;
+                               type = eb.EventType;
+                               eb.SetUsed ();
+                       } else
+                               type = EventInfo.EventHandlerType;
                }
 
-               override public Expression DoResolve (EmitContext ec)
+               public string Name {
+                       get {
+                               return EventInfo.Name;
+                       }
+               }
+
+               public bool IsInstance {
+                       get {
+                               return !is_static;
+                       }
+               }
+
+               public bool IsStatic {
+                       get {
+                               return is_static;
+                       }
+               }
+
+               public Type DeclaringType {
+                       get {
+                               return EventInfo.DeclaringType;
+                       }
+               }
+
+               public Expression InstanceExpression {
+                       get {
+                               return instance_expr;
+                       }
+
+                       set {
+                               instance_expr = value;
+                       }
+               }
+
+               public override Expression DoResolve (EmitContext ec)
                {
-                       // We are born in resolved state. 
+                       if (instance_expr != null) {
+                               instance_expr = instance_expr.DoResolve (ec);
+                               if (instance_expr == null)
+                                       return null;
+                       }
+
+                       
                        return this;
                }
 
-               override public void Emit (EmitContext ec)
+               public override void Emit (EmitContext ec)
+               {
+                       Report.Error (70, loc, "The event `" + Name + "' can only appear on the left hand side of += or -= (except on the defining type)");
+               }
+
+               public void EmitAddOrRemove (EmitContext ec, Expression source)
                {
-                       throw new Exception ("Implement me");
-                       // FIXME: Implement.
+                       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, instance_expr, add_accessor, args, loc);
+                       else
+                               Invocation.EmitCall (
+                                       ec, false, IsStatic, instance_expr, remove_accessor, args, loc);
                }
        }
-       
 }