2002-01-11 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / mcs / ecore.cs
index 4e7ea98610a845bc49bac2c7c5af8ed992fd6b7a..c0bf5f2dce26a71c230aa99d5320e12f8df05126 100755 (executable)
@@ -51,7 +51,7 @@ namespace Mono.CSharp {
        ///   Base class for expressions
        /// </remarks>
        public abstract class Expression {
-               protected ExprClass eclass;
+               public ExprClass eclass;
                protected Type      type;
                
                public Type Type {
@@ -64,16 +64,6 @@ namespace Mono.CSharp {
                        }
                }
 
-               public ExprClass ExprClass {
-                       get {
-                               return eclass;
-                       }
-
-                       set {
-                               eclass = value;
-                       }
-               }
-
                /// <summary>
                ///   Utility wrapper routine for Error, just to beautify the code
                /// </summary>
@@ -159,14 +149,16 @@ namespace Mono.CSharp {
                                        return null;
                                }
                                
-                               if (e.ExprClass == ExprClass.Invalid)
-                                       throw new Exception ("Expression " + e +
+                               if (e.eclass == ExprClass.Invalid)
+                                       throw new Exception ("Expression " + e.GetType () +
                                                             " ExprClass is Invalid after resolve");
 
-                               if (e.ExprClass != ExprClass.MethodGroup)
+                               if (e.eclass != ExprClass.MethodGroup)
                                        if (e.type == null)
-                                               throw new Exception ("Expression " + e +
-                                                                    " did not set its type after Resolve");
+                                               throw new Exception (
+                                                       "Expression " + e.GetType () +
+                                                       " did not set its type after Resolve\n" +
+                                                       "called from: " + this.GetType ());
                        }
 
                        return e;
@@ -183,17 +175,22 @@ namespace Mono.CSharp {
                /// </remarks>
                public Expression ResolveWithSimpleName (EmitContext ec)
                {
-                       Expression e = DoResolve (ec);
+                       Expression e;
+
+                       if (this is SimpleName)
+                               e = ((SimpleName) this).DoResolveAllowStatic (ec);
+                       else 
+                               e = DoResolve (ec);
 
                        if (e != null){
                                if (e is SimpleName)
                                        return e;
 
-                               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.eclass != ExprClass.MethodGroup)
                                        if (e.type == null)
                                                throw new Exception ("Expression " + e +
                                                                     " did not set its type after Resolve");
@@ -225,11 +222,11 @@ namespace Mono.CSharp {
                                        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.eclass != ExprClass.MethodGroup)
                                        if (e.type == null)
                                                throw new Exception ("Expression " + e +
                                                                     " did not set its type after Resolve");
@@ -248,15 +245,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
@@ -274,46 +262,52 @@ namespace Mono.CSharp {
                ///
                /// <remarks>
                ///   The possible return values are:
-               ///      IntLiteral, UIntLiteral
-               ///      LongLiteral, ULongLiteral
-               ///      FloatLiteral, DoubleLiteral
-               ///      StringLiteral
+               ///      IntConstant, UIntConstant
+               ///      LongLiteral, ULongConstant
+               ///      FloatConstant, DoubleConstant
+               ///      StringConstant
+               ///
+               ///   The value returned is already resolved.
                /// </remarks>
-               public static Expression Literalize (object v, Type t)
+               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 (TypeManager.IsEnumType (t)){
+                               Expression e = Constantify (v, v.GetType ());
+
+                               return new EnumConstant ((Constant) 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);
@@ -321,17 +315,58 @@ namespace Mono.CSharp {
                                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);
+                       else if (mi is Type){
+                               return new TypeExpr ((System.Type) mi);
+                       }
 
                        return null;
                }
 
                //
-               // FIXME: Probably implement a cache for (t,name,current_access_set)?
+               // We copy methods from `new_members' into `target_list' if the signature
+               // for the method from in the new list does not exist in the target_list
                //
-               // FIXME: We need to cope with access permissions here, or this wont
-               // work!
+               // The name is assumed to be the same.
+               //
+               static ArrayList CopyNewMethods (ArrayList target_list, MemberInfo [] new_members)
+               {
+                       if (target_list == null){
+                               target_list = new ArrayList ();
+
+                               target_list.AddRange (new_members);
+                               return target_list;
+                       }
+                       
+                       MemberInfo [] target_array = new MemberInfo [target_list.Count];
+                       target_list.CopyTo (target_array, 0);
+                       
+                       foreach (MemberInfo mi in new_members){
+                               MethodBase new_method = (MethodBase) mi;
+                               Type [] new_args = TypeManager.GetArgumentTypes (new_method);
+                                       
+                               foreach (MethodBase method in target_array){
+                                       Type [] old_args = TypeManager.GetArgumentTypes (method);
+                                       int new_count = new_args.Length;
+                                       int old_count = old_args.Length;
+                                       
+                                       if (new_count != old_count){
+                                               target_list.Add (method);
+                                               continue;
+                                       }
+
+                                       for (int i = 0; i < old_count; i++){
+                                               if (old_args [i] == new_args [i])
+                                                       continue;
+                                               target_list.Add (method);
+                                               break;
+                                       }
+                               }
+                       }
+                       return target_list;
+               }
+               
+               //
+               // FIXME: Probably implement a cache for (t,name,current_access_set)?
                //
                // This code could use some optimizations, but we need to do some
                // measurements.  For example, we could use a delegate to `flag' when
@@ -354,45 +389,74 @@ 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).
                //
+               //
+               // FIXME: Potential optimization, have a static ArrayList
+               //
+
                public static Expression MemberLookup (EmitContext ec, Type t, string name,
-                                                      bool same_type, MemberTypes mt,
-                                                      BindingFlags bf, Location loc)
+                                                      MemberTypes mt, BindingFlags bf, Location loc)
                {
-                       if (same_type)
+                       Type source_type = ec.ContainerType;
+                       
+                       if (source_type == t || source_type.IsSubclassOf (t))
                                bf |= BindingFlags.NonPublic;
 
-                       MemberInfo [] mi = ec.TypeContainer.RootContext.TypeManager.FindMembers (
-                               t, mt, bf, Type.FilterName, name);
+                       //
+                       // Lookup for members starting in the type requested and going
+                       // up the hierarchy until a match is found.
+                       //
+                       // As soon as a non-method match is found, we return.
+                       //
+                       // If methods are found though, then the search proceeds scanning
+                       // for more public methods in the hierarchy with signatures that
+                       // do not match any of the signatures found so far.
+                       //
+                       ArrayList method_list = null;
+                       Type current_type = t;
+                       bool searching = true;
+                       do {
+                               MemberInfo [] mi;
+
+                               mi = RootContext.TypeManager.FindMembers (
+                                       current_type, mt, bf | BindingFlags.DeclaredOnly,
+                                       System.Type.FilterName, name);
+                               
+                               if (current_type == TypeManager.object_type)
+                                       searching = false;
+                               else {
+                                       current_type = current_type.BaseType;
+
+                                       //
+                                       // This happens with interfaces, they have a null
+                                       // basetype
+                                       //
+                                       if (current_type == null)
+                                               searching = false;
+                               }
 
-                       if (mi == null)
-                               return null;
+                               if (mi == null)
+                                       continue;
+                               
+                               int count = mi.Length;
 
-                       // Empty array ...
-                       // This arises when we use FindMembers from SRE. Our code prefers to send a null back
-                       if (mi.Length == 0) 
-                               return null;
-                       
-                       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 (count == 0)
+                                       continue;
+                               
+                               if (count == 1 && !(mi [0] is MethodBase))
+                                       return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
+
+                               //
+                               // We found methods, turn the search into "method scan"
+                               // mode.
+                               //
+                               method_list = CopyNewMethods (method_list, mi);
+                               mt &= (MemberTypes.Method | MemberTypes.Constructor);
+                       } while (searching);
+
+                       if (method_list != null && method_list.Count > 0)
+                               return new MethodGroupExpr (method_list);
 
-                       return new MethodGroupExpr (mi);
+                       return null;
                }
 
                public const MemberTypes AllMemberTypes =
@@ -403,17 +467,47 @@ namespace Mono.CSharp {
                        MemberTypes.NestedType  |
                        MemberTypes.Property;
                
-               public const BindingFlags AllBindingsFlags =
+               public const BindingFlags AllBindingFlags =
                        BindingFlags.Public |
                        BindingFlags.Static |
                        BindingFlags.Instance;
 
-               public static Expression MemberLookup (EmitContext ec, Type t, string name,
-                                                      bool same_type, Location loc)
+               public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
                {
-                       return MemberLookup (ec, t, name, same_type, AllMemberTypes, AllBindingsFlags, loc);
+                       return MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
                }
 
+               /// <summary>
+               ///   This is a wrapper for MemberLookup that is not used to "probe", but
+               ///   to find a final definition.  If the final definition is not found, we
+               ///   look for private members and display a useful debugging message if we
+               ///   find it.
+               /// </summary>
+               public static Expression MemberLookupFinal (EmitContext ec, Type t, string name, 
+                                                           Location loc)
+               {
+                       Expression e;
+
+                       e = MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
+
+                       if (e != null)
+                               return e;
+                       
+                       e = MemberLookup (ec, t, name, AllMemberTypes,
+                                         AllBindingFlags | BindingFlags.NonPublic, loc);
+                       if (e == null){
+                               Report.Error (
+                                       117, loc, "`" + t + "' does not contain a definition " +
+                                       "for `" + name + "'");
+                       } else {
+                               Report.Error (
+                                       122, loc, "`" + t + "." + name +
+                                       "' is inaccessible due to its protection level");
+                       }
+                       
+                       return null;
+               }
+               
                static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
                {
                        Type expr_type = expr.Type;
@@ -426,9 +520,12 @@ namespace Mono.CSharp {
                        } else if (expr_type.IsSubclassOf (target_type)) {
                                return new EmptyCast (expr, target_type);
                        } else {
+                               // from the null type to any reference-type.
+                               if (expr is NullLiteral && !target_type.IsValueType)
+                                       return new EmptyCast (expr, target_type);
+
                                // from any class-type S to any interface-type T.
                                if (expr_type.IsClass && target_type.IsInterface) {
-
                                        if (TypeManager.ImplementsInterface (expr_type, target_type))
                                                return new EmptyCast (expr, target_type);
                                        else
@@ -473,10 +570,6 @@ namespace Mono.CSharp {
                                        if (target_type == TypeManager.icloneable_type)
                                                return new EmptyCast (expr, target_type);
                                
-                               // from the null type to any reference-type.
-                               if (expr is NullLiteral)
-                                       return new EmptyCast (expr, target_type);
-
                                return null;
 
                        }
@@ -514,21 +607,22 @@ namespace Mono.CSharp {
                        //
                        // Attempt to do the implicit constant expression conversions
 
-                       if (expr is IntLiteral){
+                       if (expr is IntConstant){
                                Expression e;
                                
-                               e = TryImplicitIntConversion (target_type, (IntLiteral) expr);
+                               e = TryImplicitIntConversion (target_type, (IntConstant) expr);
 
                                if (e != null)
                                        return e;
-                       } else if (expr is LongLiteral && target_type == TypeManager.uint64_type){
+                       } 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
                                //
-                               if (((LongLiteral) expr).Value > 0)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
+                               long v = ((LongConstant) expr).Value;
+                               if (v > 0)
+                                       return new ULongConstant ((ulong) v);
                        }
                        
                        if (expr_type == TypeManager.sbyte_type){
@@ -561,7 +655,6 @@ namespace Mono.CSharp {
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
                                if (target_type == TypeManager.int64_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
-                               
                                if (target_type == TypeManager.float_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
                                if (target_type == TypeManager.double_type)
@@ -843,6 +936,22 @@ namespace Mono.CSharp {
 
                        return false;
                }
+
+               static EmptyExpression MyEmptyExpr;
+               /// <summary>
+               ///   Tells whether an implicit conversion exists from expr_type to
+               ///   target_type
+               /// </summary>
+               public bool ImplicitConversionExists (EmitContext ec, Type expr_type, Type target_type,
+                                                     Location l)
+               {
+                       if (MyEmptyExpr == null)
+                               MyEmptyExpr = new EmptyExpression (expr_type);
+                       else
+                               MyEmptyExpr.SetType (expr_type);
+
+                       return ConvertImplicit (ec, MyEmptyExpr, target_type, l) != null;
+               }
                
                /// <summary>
                ///  Finds "most encompassed type" according to the spec (13.4.2)
@@ -944,15 +1053,15 @@ namespace Mono.CSharp {
                        else
                                op_name = "op_Implicit";
                        
-                       mg1 = MemberLookup (ec, source_type, op_name, false, loc);
+                       mg1 = MemberLookup (ec, source_type, op_name, loc);
 
                        if (source_type.BaseType != null)
-                               mg2 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
+                               mg2 = MemberLookup (ec, source_type.BaseType, op_name, loc);
                        
-                       mg3 = MemberLookup (ec, target, op_name, false, loc);
+                       mg3 = MemberLookup (ec, target, op_name, loc);
 
                        if (target.BaseType != null)
-                               mg4 = MemberLookup (ec, target.BaseType, op_name, false, loc);
+                               mg4 = MemberLookup (ec, target.BaseType, op_name, loc);
 
                        MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
                        MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
@@ -965,15 +1074,15 @@ namespace Mono.CSharp {
 
                                op_name = "op_Explicit";
                                
-                               mg5 = MemberLookup (ec, source_type, op_name, false, loc);
+                               mg5 = MemberLookup (ec, source_type, op_name, loc);
 
                                if (source_type.BaseType != null)
-                                       mg6 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
+                                       mg6 = MemberLookup (ec, source_type.BaseType, op_name, loc);
                                
-                               mg7 = MemberLookup (ec, target, op_name, false, loc);
+                               mg7 = MemberLookup (ec, target, op_name, loc);
                                
                                if (target.BaseType != null)
-                                       mg8 = MemberLookup (ec, target.BaseType, op_name, false, loc);
+                                       mg8 = MemberLookup (ec, target.BaseType, op_name, loc);
                                
                                MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
                                MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
@@ -1030,7 +1139,7 @@ namespace Mono.CSharp {
 
                                if (source == null)
                                        return null;
-                               
+
                                e =  new UserCast ((MethodInfo) method, source);
                                
                                if (e.Type != target){
@@ -1124,33 +1233,32 @@ namespace Mono.CSharp {
                }
 
                /// <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;
+
+                       //
+                       // FIXME: This should really return constants instead of EmptyCasts
+                       //
                        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
@@ -1158,8 +1266,11 @@ namespace Mono.CSharp {
                                // to do it.
                                //
                                if (value >= 0)
-                                       return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
+                                       return new ULongConstant ((ulong) value);
                        }
+                       
+                       if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
+                               return new EnumConstant (ic, target_type);
 
                        return null;
                }
@@ -1177,6 +1288,12 @@ namespace Mono.CSharp {
                        e = ConvertImplicit (ec, source, target_type, loc);
                        if (e != null)
                                return e;
+
+                       if (source is DoubleLiteral && target_type == TypeManager.float_type){
+                               Error (664, loc,
+                                      "Double literal cannot be implicitly converted to " +
+                                      "float type, use F suffix to create a float literal");
+                       }
                        
                        string msg = "Cannot convert implicitly from `"+
                                TypeManager.CSharpName (source.Type) + "' to `" +
@@ -1200,135 +1317,135 @@ namespace Mono.CSharp {
                                // From sbyte to byte, ushort, uint, ulong, char
                                //
                                if (target_type == TypeManager.byte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
                                if (target_type == TypeManager.ushort_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
                                if (target_type == TypeManager.uint32_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
                                if (target_type == TypeManager.uint64_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
                                if (target_type == TypeManager.char_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                                       return new ConvCast (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);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
                                if (target_type == TypeManager.char_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                                       return new ConvCast (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);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
                                if (target_type == TypeManager.byte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
                                if (target_type == TypeManager.ushort_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
                                if (target_type == TypeManager.uint32_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
                                if (target_type == TypeManager.uint64_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
                                if (target_type == TypeManager.char_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                                       return new ConvCast (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);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
                                if (target_type == TypeManager.byte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
                                if (target_type == TypeManager.short_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
                                if (target_type == TypeManager.char_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                                       return new ConvCast (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);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
                                if (target_type == TypeManager.byte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
                                if (target_type == TypeManager.short_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
                                if (target_type == TypeManager.ushort_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
                                if (target_type == TypeManager.uint32_type)
-                                       return new EmptyCast (expr, target_type);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
                                if (target_type == TypeManager.uint64_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
                                if (target_type == TypeManager.char_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                                       return new ConvCast (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);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
                                if (target_type == TypeManager.byte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
                                if (target_type == TypeManager.short_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
                                if (target_type == TypeManager.ushort_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
                                if (target_type == TypeManager.int32_type)
-                                       return new EmptyCast (expr, target_type);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
                                if (target_type == TypeManager.char_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                                       return new ConvCast (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);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
                                if (target_type == TypeManager.byte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
                                if (target_type == TypeManager.short_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
                                if (target_type == TypeManager.ushort_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
                                if (target_type == TypeManager.int32_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
                                if (target_type == TypeManager.uint32_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
                                if (target_type == TypeManager.uint64_type)
-                                       return new EmptyCast (expr, target_type);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
                                if (target_type == TypeManager.char_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                                       return new ConvCast (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);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
                                if (target_type == TypeManager.byte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
                                if (target_type == TypeManager.short_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
                                if (target_type == TypeManager.ushort_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
                                if (target_type == TypeManager.int32_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
                                if (target_type == TypeManager.uint32_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
                                if (target_type == TypeManager.int64_type)
-                                       return new EmptyCast (expr, target_type);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
                                if (target_type == TypeManager.char_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                                       return new ConvCast (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);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
                                if (target_type == TypeManager.byte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
                                if (target_type == TypeManager.short_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
                        } else if (expr_type == TypeManager.float_type){
                                //
                                // From float to sbyte, byte, short,
@@ -1336,23 +1453,23 @@ namespace Mono.CSharp {
                                // or decimal
                                //
                                if (target_type == TypeManager.sbyte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
                                if (target_type == TypeManager.byte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
                                if (target_type == TypeManager.short_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
                                if (target_type == TypeManager.ushort_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
                                if (target_type == TypeManager.int32_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
                                if (target_type == TypeManager.uint32_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
                                if (target_type == TypeManager.int64_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
                                if (target_type == TypeManager.uint64_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
                                if (target_type == TypeManager.char_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
                                if (target_type == TypeManager.decimal_type)
                                        return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.double_type){
@@ -1362,25 +1479,25 @@ namespace Mono.CSharp {
                                // char, float or decimal
                                //
                                if (target_type == TypeManager.sbyte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
                                if (target_type == TypeManager.byte_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
                                if (target_type == TypeManager.short_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
                                if (target_type == TypeManager.ushort_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
                                if (target_type == TypeManager.int32_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
                                if (target_type == TypeManager.uint32_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
                                if (target_type == TypeManager.int64_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
                                if (target_type == TypeManager.uint64_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
                                if (target_type == TypeManager.char_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
                                if (target_type == TypeManager.float_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
+                                       return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
                                if (target_type == TypeManager.decimal_type)
                                        return InternalTypeConstructor (ec, expr, target_type);
                        } 
@@ -1615,10 +1732,25 @@ namespace Mono.CSharp {
                        //
                        // Enum types
                        //
-                       if (expr is EnumLiteral) {
-                               Expression e = ((EnumLiteral) expr).Child;
+                       if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
+                               Expression e;
+
+                               //
+                               // FIXME: Is there any reason we should have EnumConstant
+                               // dealt with here instead of just using always the
+                               // UnderlyingSystemType to wrap the type?
+                               //
+                               if (expr is EnumConstant)
+                                       e = ((EnumConstant) expr).Child;
+                               else {
+                                       e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
+                               }
                                
-                               return ConvertImplicit (ec, e, target_type, loc);
+                               e = ConvertImplicit (ec, e, target_type, loc);
+                               if (e != null)
+                                       return e;
+                               
+                               return ConvertNumericExplicit (ec, e, target_type);
                        }
                        
                        ne = ConvertReferenceExplicit (expr, target_type);
@@ -1691,22 +1823,12 @@ namespace Mono.CSharp {
                        string kind = "Unknown";
                        
                        if (expr != null)
-                               kind = ExprClassName (expr.ExprClass);
+                               kind = ExprClassName (expr.eclass);
 
                        Error (118, loc, "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)
-               {
-                       //Console.WriteLine ("Calling reduce");
-                       return e.Reduce (ec);
-               }
-
                static void error31 (Location l, string val, Type t)
                {
                        Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
@@ -1714,146 +1836,273 @@ namespace Mono.CSharp {
                }
                
                /// <summary>
-               ///   Converts the IntLiteral, UIntLiteral, LongLiteral or
-               ///   ULongLiteral into the integral target_type.
+               ///   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 Literal ConvertIntLiteral (Literal l, Type target_type, Location loc)
+               public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
                {
                        string s = "";
 
-                       if (l.Type == target_type)
-                               return l;
+                       if (c.Type == target_type)
+                               return ((Constant) c).GetValue ();
 
                        //
                        // 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 (l is EnumLiteral)
-                               l = ((EnumLiteral)l).WidenToCompilerLiteral ();
+                       if (c is EnumConstant)
+                               c = ((EnumConstant)c).WidenToCompilerConstant ();
 
-                       if (l is IntLiteral){
-                               int v = ((IntLiteral) l).Value;
+                       if (c is IntConstant){
+                               int v = ((IntConstant) c).Value;
                                
                                if (target_type == TypeManager.uint32_type){
                                        if (v >= 0)
-                                               return new UIntLiteral ((uint) v);
+                                               return (uint) v;
                                } else if (target_type == TypeManager.char_type){
                                        if (v >= Char.MinValue && v <= Char.MaxValue)
-                                               return l;
+                                               return (char) v;
                                } else if (target_type == TypeManager.byte_type){
                                        if (v >= Byte.MinValue && v <= Byte.MaxValue)
-                                               return l;
+                                               return (byte) v;
                                } else if (target_type == TypeManager.sbyte_type){
                                        if (v >= SByte.MinValue && v <= SByte.MaxValue)
-                                               return l;
+                                               return (sbyte) v;
                                } else if (target_type == TypeManager.short_type){
                                        if (v >= Int16.MinValue && v <= UInt16.MaxValue)
-                                               return l;
+                                               return (short) v;
                                } else if (target_type == TypeManager.ushort_type){
                                        if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
-                                               return l;
+                                               return (ushort) v;
                                } else if (target_type == TypeManager.int64_type)
-                                       return new LongLiteral (v);
+                                       return (long) v;
                                else if (target_type == TypeManager.uint64_type){
                                        if (v > 0)
-                                               return new ULongLiteral ((ulong) v);
+                                               return (ulong) v;
                                }
 
                                s = v.ToString ();
-                       } else if (l is UIntLiteral){
-                               uint v = ((UIntLiteral) l).Value;
+                       } else if (c is UIntConstant){
+                               uint v = ((UIntConstant) c).Value;
 
                                if (target_type == TypeManager.int32_type){
                                        if (v <= Int32.MaxValue)
-                                               return new IntLiteral ((int) v);
+                                               return (int) v;
                                } else if (target_type == TypeManager.char_type){
                                        if (v >= Char.MinValue && v <= Char.MaxValue)
-                                               return l;
+                                               return (char) v;
                                } else if (target_type == TypeManager.byte_type){
                                        if (v <= Byte.MaxValue)
-                                               return l;
+                                               return (byte) v;
                                } else if (target_type == TypeManager.sbyte_type){
                                        if (v <= SByte.MaxValue)
-                                               return l;
+                                               return (sbyte) v;
                                } else if (target_type == TypeManager.short_type){
                                        if (v <= UInt16.MaxValue)
-                                               return l;
+                                               return (short) v;
                                } else if (target_type == TypeManager.ushort_type){
                                        if (v <= UInt16.MaxValue)
-                                               return l;
+                                               return (ushort) v;
                                } else if (target_type == TypeManager.int64_type)
-                                       return new LongLiteral (v);
+                                       return (long) v;
                                else if (target_type == TypeManager.uint64_type)
-                                       return new ULongLiteral (v);
+                                       return (ulong) v;
                                s = v.ToString ();
-                       } else if (l is LongLiteral){ 
-                               long v = ((LongLiteral) l).Value;
+                       } else if (c is LongConstant){ 
+                               long v = ((LongConstant) c).Value;
 
                                if (target_type == TypeManager.int32_type){
                                        if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
-                                               return new IntLiteral ((int) v);
+                                               return (int) v;
                                } else if (target_type == TypeManager.uint32_type){
                                        if (v >= 0 && v <= UInt32.MaxValue)
-                                               return new UIntLiteral ((uint) v);
+                                               return (uint) v;
                                } else if (target_type == TypeManager.char_type){
                                        if (v >= Char.MinValue && v <= Char.MaxValue)
-                                               return new IntLiteral ((int) v);
+                                               return (char) v;
                                } else if (target_type == TypeManager.byte_type){
                                        if (v >= Byte.MinValue && v <= Byte.MaxValue)
-                                               return new IntLiteral ((int) v);
+                                               return (byte) v;
                                } else if (target_type == TypeManager.sbyte_type){
                                        if (v >= SByte.MinValue && v <= SByte.MaxValue)
-                                               return new IntLiteral ((int) v);
+                                               return (sbyte) v;
                                } else if (target_type == TypeManager.short_type){
                                        if (v >= Int16.MinValue && v <= UInt16.MaxValue)
-                                               return new IntLiteral ((int) v);
+                                               return (short) v;
                                } else if (target_type == TypeManager.ushort_type){
                                        if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
-                                               return new IntLiteral ((int) v);
+                                               return (ushort) v;
                                } else if (target_type == TypeManager.uint64_type){
                                        if (v > 0)
-                                               return new ULongLiteral ((ulong) v);
+                                               return (ulong) v;
                                }
                                s = v.ToString ();
-                       } else if (l is ULongLiteral){
-                               ulong v = ((ULongLiteral) l).Value;
+                       } else if (c is ULongConstant){
+                               ulong v = ((ULongConstant) c).Value;
 
                                if (target_type == TypeManager.int32_type){
                                        if (v <= Int32.MaxValue)
-                                               return new IntLiteral ((int) v);
+                                               return (int) v;
                                } else if (target_type == TypeManager.uint32_type){
                                        if (v <= UInt32.MaxValue)
-                                               return new UIntLiteral ((uint) v);
+                                               return (uint) v;
                                } else if (target_type == TypeManager.char_type){
                                        if (v >= Char.MinValue && v <= Char.MaxValue)
-                                               return new IntLiteral ((int) v);
+                                               return (char) v;
                                } else if (target_type == TypeManager.byte_type){
                                        if (v >= Byte.MinValue && v <= Byte.MaxValue)
-                                               return new IntLiteral ((int) v);
+                                               return (byte) v;
                                } else if (target_type == TypeManager.sbyte_type){
                                        if (v <= (int) SByte.MaxValue)
-                                               return new IntLiteral ((int) v);
+                                               return (sbyte) v;
                                } else if (target_type == TypeManager.short_type){
                                        if (v <= UInt16.MaxValue)
-                                               return new IntLiteral ((int) v);
+                                               return (short) v;
                                } else if (target_type == TypeManager.ushort_type){
                                        if (v <= UInt16.MaxValue)
-                                               return new IntLiteral ((int) v);
+                                               return (ushort) v;
                                } else if (target_type == TypeManager.int64_type){
                                        if (v <= Int64.MaxValue)
-                                               return new LongLiteral ((long) v);
+                                               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 ();
+                       }
                        error31 (loc, s, target_type);
                        return null;
-               }
+               }               
                        
        }
 
@@ -1892,7 +2141,7 @@ namespace Mono.CSharp {
 
                public EmptyCast (Expression child, Type return_type)
                {
-                       ExprClass = child.ExprClass;
+                       eclass = child.eclass;
                        type = return_type;
                        this.child = child;
                }
@@ -1909,18 +2158,17 @@ namespace Mono.CSharp {
                {
                        child.Emit (ec);
                }
-
        }
 
        /// <summary>
        ///  This class is used to wrap literals which belong inside Enums
        /// </summary>
-       public class EnumLiteral : Literal {
-               public Expression Child;
+       public class EnumConstant : Constant {
+               public Constant Child;
 
-               public EnumLiteral (Expression child, Type enum_type)
+               public EnumConstant (Constant child, Type enum_type)
                {
-                       ExprClass = child.ExprClass;
+                       eclass = child.eclass;
                        this.Child = child;
                        type = enum_type;
                }
@@ -1940,7 +2188,7 @@ namespace Mono.CSharp {
 
                public override object GetValue ()
                {
-                       return ((Literal) Child).GetValue ();
+                       return Child.GetValue ();
                }
 
                //
@@ -1948,34 +2196,62 @@ namespace Mono.CSharp {
                // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
                // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
                //
-               public Literal WidenToCompilerLiteral ()
+               public Constant WidenToCompilerConstant ()
                {
-                       Type t = Child.Type.UnderlyingSystemType;
-                       object v = ((Literal) Child).GetValue ();;
+                       Type t = TypeManager.EnumToUnderlying (Child.Type);
+                       object v = ((Constant) Child).GetValue ();;
                        
                        if (t == TypeManager.int32_type)
-                               return new IntLiteral ((int) v);
+                               return new IntConstant ((int) v);
                        if (t == TypeManager.uint32_type)
-                               return new UIntLiteral ((uint) v);
+                               return new UIntConstant ((uint) v);
                        if (t == TypeManager.int64_type)
-                               return new LongLiteral ((long) v);
+                               return new LongConstant ((long) v);
                        if (t == TypeManager.uint64_type)
-                               return new ULongLiteral ((ulong) v);
+                               return new ULongConstant ((ulong) v);
                        if (t == TypeManager.short_type)
-                               return new IntLiteral ((short) v);
+                               return new ShortConstant ((short) v);
                        if (t == TypeManager.ushort_type)
-                               return new UIntLiteral ((ushort) v);
+                               return new UShortConstant ((ushort) v);
                        if (t == TypeManager.byte_type)
-                               return new UIntLiteral ((byte) v);
+                               return new ByteConstant ((byte) v);
                        if (t == TypeManager.sbyte_type)
-                               return new IntLiteral ((sbyte) v);
+                               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 ();
                }
        }
 
@@ -2032,6 +2308,8 @@ namespace Mono.CSharp {
                        //
                        // Load the object from the pointer
                        //
+               basic_type:
+                       
                        if (t == TypeManager.int32_type)
                                ig.Emit (OpCodes.Ldind_I4);
                        else if (t == TypeManager.uint32_type)
@@ -2058,16 +2336,224 @@ namespace Mono.CSharp {
                                ig.Emit (OpCodes.Ldind_I1);
                        else if (t == TypeManager.intptr_type)
                                ig.Emit (OpCodes.Ldind_I);
-                       else 
+                       else if (TypeManager.IsEnumType (t)){
+                               t = TypeManager.EnumToUnderlying (t);
+                               goto basic_type;
+                       } else
                                ig.Emit (OpCodes.Ldobj, t);
                }
        }
        
        /// <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;
+               
+               public ConvCast (Expression child, Type return_type, Mode m)
+                       : base (child, return_type)
+               {
+                       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 (ec.CheckState){
+                               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;
@@ -2105,7 +2591,6 @@ namespace Mono.CSharp {
                        if (second_valid)
                                ec.ig.Emit (op2);
                }                       
-               
        }
 
        /// <summary>
@@ -2208,73 +2693,182 @@ namespace Mono.CSharp {
                                        Error120 (Location, Name);
                                        return null;
                                }
+                       } else if (e is EventExpr) {
+                               if (!((EventExpr) e).IsStatic) {
+                                       Error120 (Location, Name);
+                                       return null;
+                               }
                        }
 
                        return e;
                }
                
-               // <remarks>
-               //   7.5.2: Simple Names. 
-               //
-               //   Local Variables and Parameters are handled at
-               //   parse time, so they never occur as SimpleNames.
-               // </remarks>
                public override Expression DoResolve (EmitContext ec)
+               {
+                       return SimpleNameResolve (ec, false);
+               }
+
+               public Expression DoResolveAllowStatic (EmitContext ec)
+               {
+                       return SimpleNameResolve (ec, true);
+               }
+
+               /// <remarks>
+               ///   7.5.2: Simple Names. 
+               ///
+               ///   Local Variables and Parameters are handled at
+               ///   parse time, so they never occur as SimpleNames.
+               ///
+               ///   The `allow_static' flag is used by MemberAccess only
+               ///   and it is used to inform us that it is ok for us to 
+               ///   avoid the static check, because MemberAccess might end
+               ///   up resolving the Name as a Type name and the access as
+               ///   a static type access.
+               ///
+               ///   ie: Type Type; .... { Type.GetType (""); }
+               ///
+               ///   Type is both an instance variable and a Type;  Type.GetType
+               ///   is the static method not an instance method of type.
+               /// </remarks>
+               Expression SimpleNameResolve (EmitContext ec, bool allow_static)
                {
                        Expression e;
 
                        //
-                       // Stage 1: Performed by the parser (binding to local or parameters).
+                       // Stage 1: Performed by the parser (binding to locals or parameters).
                        //
 
                        //
-                       // Stage 2: Lookup members
+                       // Stage 2: Lookup members 
                        //
-                       e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, true, Location);
+                       e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, Location);
                        if (e == null){
                                //
                                // Stage 3: Lookup symbol in the various namespaces. 
-                               // 
+                               //
+                               DeclSpace ds = ec.TypeContainer;
                                Type t;
+                               string alias_value;
                                
-                               if ((t = ec.TypeContainer.LookupType (Name, true)) != null)
+                               if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
                                        return new TypeExpr (t);
-
+                               
                                //
-                               // Stage 3 part b: Lookup up if we are an alias to a type
+                               // Stage 2 part b: Lookup up if we are an alias to a type
                                // or a namespace.
                                //
                                // Since we are cheating: we only do the Alias lookup for
                                // namespaces if the name does not include any dots in it
                                //
                                
-                               // IMPLEMENT ME.  Read mcs/mcs/TODO for ideas, or rewrite
-                               // using NamespaceExprs (dunno how that fixes the alias
-                               // per-file though).
+                               if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
+                               // System.Console.WriteLine (Name + " --> " + alias_value);
+                                       if ((t = RootContext.LookupType (ds, alias_value, true, Location))
+                                           != null)
+                                               return new TypeExpr (t);
+                                       
+                               // we have alias value, but it isn't Type, so try if it's namespace
+                                       return new SimpleName (alias_value, Location);
+                               }
                                
                                // No match, maybe our parent can compose us
                                // into something meaningful.
-                               //
                                return this;
                        }
-
-                       // Step 2, continues here.
+                       
+                       //
+                       // Stage 2 continues here. 
+                       // 
                        if (e is TypeExpr)
                                return e;
 
                        if (e is FieldExpr){
                                FieldExpr fe = (FieldExpr) e;
-                               
-                               if (!fe.FieldInfo.IsStatic){
-                                       This t = new This (Location.Null);
+                               FieldInfo fi = fe.FieldInfo;
 
-                                       fe.InstanceExpression = t.DoResolve (ec);
+                               if (ec.IsStatic){
+                                       if (!allow_static && !fi.IsStatic){
+                                               Error120 (Location, Name);
+                                               return null;
+                                       }
+                               } else {
+                                       // If we are not in static code and this
+                                       // field is not static, set the instance to `this'.
+
+                                       if (!fi.IsStatic)
+                                               fe.InstanceExpression = ec.This;
                                }
+
+                               
+                               if (fi is FieldBuilder) {
+                                       Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
+                                       
+                                       if (c != null) {
+                                               object o = c.LookupConstantValue (ec);
+                                               object real_value = ((Constant)c.Expr).GetValue ();
+                                               return Constantify (real_value, fi.FieldType);
+                                       }
+                               }
+
+                               return e;
                        }                               
 
-                       if (ec.IsStatic)
+                       if (e is EventExpr) {
+                               //
+                               // If the event is local to this class, we transform ourselves into
+                               // a FieldExpr
+                               //
+                               EventExpr ee = (EventExpr) e;
+
+                               Expression ml = MemberLookup (
+                                       ec, ec.TypeContainer.TypeBuilder, ee.EventInfo.Name,
+                                       MemberTypes.Event, AllBindingFlags, Location);
+
+                               if (ml != null) {
+                                       MemberInfo mi = ec.TypeContainer.GetFieldFromEvent ((EventExpr) ml);
+
+                                       if (mi == null) {
+                                               //
+                                               // If this happens, then we have an event with its own
+                                               // accessors and private field etc so there's no need
+                                               // to transform ourselves : we should instead flag an error
+                                               //
+                                               Assign.error70 (ee.EventInfo, Location);
+                                               return null;
+                                       }
+
+                                       ml = ExprClassFromMemberInfo (ec, mi, Location);
+                                       
+                                       if (ml == null) {
+                                               Report.Error (-200, Location, "Internal error!!");
+                                               return null;
+                                       }
+
+                                       Expression instance_expr;
+                                       
+                                       FieldInfo fi = ((FieldExpr) ml).FieldInfo;
+
+                                       if (fi.IsStatic)
+                                               instance_expr = null;
+                                       else
+                                               instance_expr = ec.This;
+
+                                       instance_expr = instance_expr.Resolve (ec);
+
+                                       if (instance_expr != null)
+                                               instance_expr = instance_expr.Resolve (ec);
+                                       
+                                       return MemberAccess.ResolveMemberAccess (ec, ml, instance_expr, Location, null);
+                               }
+                       }
+                               
+                       
+                       if (ec.IsStatic){
+                               if (allow_static)
+                                       return e;
+
                                return MemberStaticCheck (e);
-                       else
+                       else
                                return e;
                }
 
@@ -2430,6 +3024,7 @@ namespace Mono.CSharp {
                                        return null;
                                
                        }
+
                        return this;
                }
 
@@ -2464,8 +3059,23 @@ namespace Mono.CSharp {
                        if (FieldInfo.IsStatic)
                                ig.Emit (OpCodes.Ldsfld, FieldInfo);
                        else {
-                               InstanceExpression.Emit (ec);
-                               
+                               if (InstanceExpression.Type.IsValueType){
+                                       IMemoryLocation ml;
+                                       
+                                       if (!(InstanceExpression is IMemoryLocation)){
+                                               LocalTemporary tempo = new LocalTemporary (
+                                                       ec, InstanceExpression.Type);
+
+                                               InstanceExpression.Emit (ec);
+                                               tempo.Store (ec);
+                                               ml = tempo;
+                                       } else
+                                               ml = (IMemoryLocation) InstanceExpression;
+
+                                       ml.AddressOf (ec);
+                               } else 
+                                       InstanceExpression.Emit (ec);
+
                                ig.Emit (OpCodes.Ldfld, FieldInfo);
                        }
                }
@@ -2483,9 +3093,10 @@ namespace Mono.CSharp {
 
                                                ml.AddressOf (ec);
                                        } else
-                                               throw new Exception ("The " + instance + " of type " + Type+
-                                                                    "represents a ValueType and does not " +
-                                                                    "implement IMemoryLocation");
+                                               throw new Exception ("The " + instance + " of type " +
+                                                                    instance.Type +
+                                                                    " represents a ValueType and does " +
+                                                                    "not implement IMemoryLocation");
                                } else
                                        instance.Emit (ec);
                        }
@@ -2519,6 +3130,7 @@ namespace Mono.CSharp {
        public class PropertyExpr : ExpressionStatement, IAssignMethod {
                public readonly PropertyInfo PropertyInfo;
                public readonly bool IsStatic;
+               public bool IsBase;
                MethodInfo [] Accessors;
                Location loc;
                
@@ -2579,12 +3191,14 @@ namespace Mono.CSharp {
                                return null;
                        }
 
+                       type = PropertyInfo.PropertyType;
+
                        return this;
                }
 
                override public void Emit (EmitContext ec)
                {
-                       Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [0], null);
+                       Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, Accessors [0], null);
                        
                }
 
@@ -2597,7 +3211,7 @@ namespace Mono.CSharp {
                        ArrayList args = new ArrayList ();
 
                        args.Add (arg);
-                       Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [1], args);
+                       Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args);
                }
 
                override public void EmitStatement (EmitContext ec)
@@ -2608,30 +3222,61 @@ namespace Mono.CSharp {
        }
 
        /// <summary>
-       ///   Fully resolved expression that evaluates to a Expression
+       ///   Fully resolved expression that evaluates to an Event
        /// </summary>
        public class EventExpr : Expression {
                public readonly EventInfo EventInfo;
                Location loc;
+               public Expression InstanceExpression;
+
+               public readonly bool IsStatic;
+
+               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)
+                                       IsStatic = true;
+
+                       if (EventInfo is MyEventBuilder)
+                               type = ((MyEventBuilder) EventInfo).EventType;
+                       else
+                               type = EventInfo.EventHandlerType;
                }
 
                override public Expression DoResolve (EmitContext ec)
                {
-                       // We are born in resolved state. 
+                       // We are born fully resolved
                        return this;
                }
 
                override public void Emit (EmitContext ec)
                {
-                       throw new Exception ("Implement me");
-                       // FIXME: Implement.
+                       throw new Exception ("Should not happen I think");
+               }
+
+               public void EmitAddOrRemove (EmitContext ec, Expression source)
+               {
+                       Expression handler = ((Binary) source).Right;
+                       
+                       Argument arg = new Argument (handler, Argument.AType.Expression);
+                       ArrayList args = new ArrayList ();
+                               
+                       args.Add (arg);
+                       
+                       if (((Binary) source).Oper == Binary.Operator.Addition)
+                               Invocation.EmitCall (
+                                       ec, false, IsStatic, InstanceExpression, add_accessor, args);
+                       else
+                               Invocation.EmitCall (
+                                       ec, false, IsStatic, InstanceExpression, remove_accessor, args);
                }
        }
-       
 }