svn path=/branches/mono-1-1-9/mcs/; revision=51212
[mono.git] / mcs / mbas / ecore.cs
index 74b0b012b35a2e8ba6b0596b41c970eed97a00af..7e5b20829d86bce2439fdcd595cd5bf05eb3b950 100644 (file)
@@ -3,12 +3,13 @@
 //
 // Author:
 //   Miguel de Icaza (miguel@ximian.com)
+//   Manjula GHM (mmanjula@novell.com)
 //
 // (C) 2001 Ximian, Inc.
 //
 //
 
-namespace Mono.CSharp {
+namespace Mono.MonoBASIC {
        using System;
        using System.Collections;
        using System.Diagnostics;
@@ -36,6 +37,34 @@ namespace Mono.CSharp {
                Nothing, 
        }
 
+       /// <remarks>
+       ///   This is used to tell Resolve in which types of expressions we're
+       ///   interested.
+       /// </remarks>
+       [Flags]
+       public enum ResolveFlags {
+               // Returns Value, Variable, PropertyAccess, EventAccess or IndexerAccess.
+               VariableOrValue         = 1,
+
+               // Returns a type expression.
+               Type                    = 2,
+
+               // Returns a method group.
+               MethodGroup             = 4,
+
+               // Allows SimpleNames to be returned.
+               // This is used by MemberAccess to construct long names that can not be
+               // partially resolved (namespace-qualified names for example).
+               SimpleName              = 8,
+
+               // Mask of all the expression class flags.
+               MaskExprClass           = 15,
+
+               // Disable control flow analysis while resolving the expression.
+               // This is used when resolving the instance expression of a field expression.
+               DisableFlowAnalysis     = 16
+       }
+
        //
        // This is just as a hint to AddressOf of what will be done with the
        // address.
@@ -54,7 +83,7 @@ namespace Mono.CSharp {
                ///   The AddressOf method should generate code that loads
                ///   the address of the object and leaves it on the stack.
                ///
-               ///   The `mode' argument is used to notify the expression
+               ///   The 'mode' argument is used to notify the expression
                ///   of whether this will be used to read from the address or
                ///   write to the address.
                ///
@@ -64,6 +93,96 @@ namespace Mono.CSharp {
                void AddressOf (EmitContext ec, AddressOp mode);
        }
 
+       /// <summary>
+       ///   This interface is implemented by variables
+       /// </summary>
+       public interface IVariable {
+               /// <summary>
+               ///   Checks whether the variable has already been assigned at
+               ///   the current position of the method's control flow and
+               ///   reports an appropriate error message if not.
+               ///
+               ///   If the variable is a struct, then this call checks whether
+               ///   all of its fields (including all private ones) have been
+               ///   assigned.
+               /// </summary>
+               bool IsAssigned (EmitContext ec, Location loc);
+
+               /// <summary>
+               ///   Checks whether field 'name' in this struct has been assigned.
+               /// </summary>
+               bool IsFieldAssigned (EmitContext ec, string name, Location loc);
+
+               /// <summary>
+               ///   Tells the flow analysis code that the variable has already
+               ///   been assigned at the current code position.
+               ///
+               ///   If the variable is a struct, this call marks all its fields
+               ///   (including private fields) as being assigned.
+               /// </summary>
+               void SetAssigned (EmitContext ec);
+
+               /// <summary>
+               ///   Tells the flow analysis code that field 'name' in this struct
+               ///   has already been assigned atthe current code position.
+               /// </summary>
+               void SetFieldAssigned (EmitContext ec, string name);
+       }
+
+       /// <summary>
+       ///   This interface denotes an expression which evaluates to a member
+       ///   of a struct or a class.
+       /// </summary>
+       public interface IMemberExpr
+       {
+               /// <summary>
+               ///   The name of this member.
+               /// </summary>
+               string Name {
+                       get;
+               }
+
+               /// <summary>
+               ///   Whether this is an instance member.
+               /// </summary>
+               bool IsInstance {
+                       get;
+               }
+
+               /// <summary>
+               ///   Whether this is a static member.
+               /// </summary>
+               bool IsStatic {
+                       get;
+               }
+
+               /// <summary>
+               ///   The type which declares this member.
+               /// </summary>
+               Type DeclaringType {
+                       get;
+               }
+
+               /// <summary>
+               ///   The instance expression associated with this member, if it's a
+               ///   non-static member.
+               /// </summary>
+               Expression InstanceExpression {
+                       get; set;
+               }
+       }
+
+       /// <summary>
+       ///   Expression which resolves to a type.
+       /// </summary>
+       public interface ITypeExpression
+       {
+               /// <summary>
+               ///   Resolve the expression, but only lookup types.
+               /// </summary>
+               Expression DoResolveType (EmitContext ec);
+       }
+
        /// <remarks>
        ///   Base class for expressions
        /// </remarks>
@@ -93,7 +212,7 @@ namespace Mono.CSharp {
                /// </summary>
                public void Error (int error, string s)
                {
-                       if (Location.IsNull (loc))
+                       if (!Location.IsNull (loc))
                                Report.Error (error, loc, s);
                        else
                                Report.Error (error, s);
@@ -102,15 +221,19 @@ namespace Mono.CSharp {
                /// <summary>
                ///   Utility wrapper routine for Warning, just to beautify the code
                /// </summary>
-               protected void Warning (int warning, string s)
+               public void Warning (int warning, string s)
                {
-                       if (Location.IsNull (loc))
+                       if (!Location.IsNull (loc))
                                Report.Warning (warning, loc, s);
                        else
                                Report.Warning (warning, s);
                }
 
-               protected void Warning (int warning, int level, string s)
+               /// <summary>
+               ///   Utility wrapper routine for Warning, only prints the warning if
+               ///   warnings of level 'level' are enabled.
+               /// </summary>
+               public void Warning (int warning, int level, string s)
                {
                        if (level <= RootContext.WarningLevel)
                                Warning (warning, s);
@@ -119,8 +242,8 @@ namespace Mono.CSharp {
                static public void Error_CannotConvertType (Location loc, Type source, Type target)
                {
                        Report.Error (30, loc, "Cannot convert type '" +
-                                     TypeManager.CSharpName (source) + "' to '" +
-                                     TypeManager.CSharpName (target) + "'");
+                                     TypeManager.MonoBASIC_Name (source) + "' to '" +
+                                     TypeManager.MonoBASIC_Name (target) + "'");
                }
 
                /// <summary>
@@ -146,7 +269,7 @@ namespace Mono.CSharp {
                ///   There are two side effects expected from calling
                ///   Resolve(): the the field variable "eclass" should
                ///   be set to any value of the enumeration
-               ///   `ExprClass' and the type variable should be set
+               ///   'ExprClass' and the type variable should be set
                ///   to a valid type (this is the type of the
                ///   expression).
                /// </remarks>
@@ -165,72 +288,116 @@ namespace Mono.CSharp {
                ///   Currently Resolve wraps DoResolve to perform sanity
                ///   checking and assertion checking on what we expect from Resolve.
                /// </remarks>
-               public Expression Resolve (EmitContext ec)
+               public Expression Resolve (EmitContext ec, ResolveFlags flags)
                {
-                       Expression e = DoResolve (ec);
+                       // Are we doing a types-only search ?
+                       if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type) {
+                               ITypeExpression type_expr = this as ITypeExpression;
 
-                       if (e != null){
+                               if (type_expr == null)
+                                       return null;
 
-                               if (e is SimpleName){
-                                       SimpleName s = (SimpleName) e;
+                               return type_expr.DoResolveType (ec);
+                       }
 
-                                       Report.Error (
-                                                     103, loc,
-                                                     "The name `" + s.Name + "' could not be found in `" +
-                                                     ec.DeclSpace.Name + "'");
+                       bool old_do_flow_analysis = ec.DoFlowAnalysis;
+                       if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
+                               ec.DoFlowAnalysis = false;
+                       
+                       Expression e;
+                       try {
+                               if (this is SimpleName)
+                                       e = ((SimpleName) this).DoResolveAllowStatic (ec);
+                               else 
+                                       e = DoResolve (ec);
+                       } finally {
+                               ec.DoFlowAnalysis = old_do_flow_analysis;
+                       }
+
+                       if (e == null)
+                               return null;
+
+                       if (e is SimpleName){
+                               SimpleName s = (SimpleName) e;
+
+                               if ((flags & ResolveFlags.SimpleName) == 0) {
+
+                                       object lookup = TypeManager.MemberLookup (
+                                               ec.ContainerType, ec.ContainerType, AllMemberTypes,
+                                               AllBindingFlags | BindingFlags.NonPublic, s.Name);
+                                       if (lookup != null)
+                                               Error (30390, "'" + s.Name + "' " +
+                                                      "is inaccessible because of its protection level");
+                                       else
+                                               Error (30451, "The name '" + s.Name + "' could not be " +
+                                                      "found in '" + ec.DeclSpace.Name + "'");
                                        return null;
                                }
-                               
-                               if (e.eclass == ExprClass.Invalid)
-                                       throw new Exception ("Expression " + e.GetType () +
-                                                            " ExprClass is Invalid after resolve");
 
-                               if (e.eclass != ExprClass.MethodGroup)
-                                       if (e.type == null)
-                                               throw new Exception (
-                                                       "Expression " + e.GetType () +
-                                                       " did not set its type after Resolve\n" +
-                                                       "called from: " + this.GetType ());
+                               return s;
                        }
+                       
+                       if ((e is TypeExpr) || (e is ComposedCast)) {
+                               if ((flags & ResolveFlags.Type) == 0) {
+                                       e.Error118 (flags);
+                                       return null;
+                               }
 
-                       return e;
-               }
-
-               /// <summary>
-               ///   Performs expression resolution and semantic analysis, but
-               ///   allows SimpleNames to be returned.
-               /// </summary>
-               ///
-               /// <remarks>
-               ///   This is used by MemberAccess to construct long names that can not be
-               ///   partially resolved (namespace-qualified names for example).
-               /// </remarks>
-               public Expression ResolveWithSimpleName (EmitContext ec)
-               {
-                       Expression e;
+                               return e;
+                       }
 
-                       if (this is SimpleName)
-                               e = ((SimpleName) this).DoResolveAllowStatic (ec);
-                       else 
-                               e = DoResolve (ec);
+                       switch (e.eclass) {
+                       case ExprClass.Type:
+                               if ((flags & ResolveFlags.VariableOrValue) == 0) {
+                                       e.Error118 (flags);
+                                       return null;
+                               }
+                               break;
 
-                       if (e != null){
-                               if (e is SimpleName)
-                                       return e;
+                       case ExprClass.MethodGroup:
+                               if ((flags & ResolveFlags.MethodGroup) == 0) {
+                                       MethodGroupExpr mg = (MethodGroupExpr) e;
+                                       Invocation i = new Invocation (mg, new ArrayList(), Location.Null);
+                                       Expression te = i.Resolve(ec);
+                                       //((MethodGroupExpr) e).ReportUsageError ();
+                                       //return null;
+                                       return te;
+                               }
+                               break;
 
-                               if (e.eclass == ExprClass.Invalid)
-                                       throw new Exception ("Expression " + e +
-                                                            " ExprClass is Invalid after resolve");
+                       case ExprClass.Value:
+                       case ExprClass.Variable:
+                       case ExprClass.PropertyAccess:
+                       case ExprClass.EventAccess:
+                       case ExprClass.IndexerAccess:
+                               if ((flags & ResolveFlags.VariableOrValue) == 0) {
+                                       e.Error118 (flags);
+                                       return null;
+                               }
+                               break;
 
-                               if (e.eclass != ExprClass.MethodGroup)
-                                       if (e.type == null)
-                                               throw new Exception ("Expression " + e +
-                                                                    " did not set its type after Resolve");
+                       default:
+                               throw new Exception ("Expression " + e.GetType () +
+                                                    " ExprClass is Invalid after resolve");
                        }
 
+                       if (e.type == null)
+                               throw new Exception (
+                                       "Expression " + e.GetType () +
+                                       " did not set its type after Resolve\n" +
+                                       "called from: " + this.GetType ());
+
                        return e;
                }
-               
+
+               /// <summary>
+               ///   Resolves an expression and performs semantic analysis on it.
+               /// </summary>
+               public Expression Resolve (EmitContext ec)
+               {
+                       return Resolve (ec, ResolveFlags.VariableOrValue);
+               }
+
                /// <summary>
                ///   Resolves an expression for LValue assignment
                /// </summary>
@@ -248,8 +415,8 @@ namespace Mono.CSharp {
                                        SimpleName s = (SimpleName) e;
 
                                        Report.Error (
-                                               103, loc,
-                                               "The name `" + s.Name + "' could not be found in `" +
+                                               30451, loc,
+                                               "The name '" + s.Name + "' could not be found in '" +
                                                ec.DeclSpace.Name + "'");
                                        return null;
                                }
@@ -258,10 +425,18 @@ namespace Mono.CSharp {
                                        throw new Exception ("Expression " + e +
                                                             " ExprClass is Invalid after resolve");
 
-                               if (e.eclass != ExprClass.MethodGroup)
-                                       if (e.type == null)
-                                               throw new Exception ("Expression " + e +
-                                                                    " did not set its type after Resolve");
+                               if (e.eclass == ExprClass.MethodGroup) {
+                                       MethodGroupExpr mg = (MethodGroupExpr) e;
+                                       Invocation i = new Invocation (mg, new ArrayList(), Location.Null);
+                                       Expression te = i.Resolve(ec);
+                                       return te;
+                                       //((MethodGroupExpr) e).ReportUsageError ();
+                                       //return null;
+                               }
+
+                               if (e.type == null)
+                                       throw new Exception ("Expression " + e +
+                                                            " did not set its type after Resolve");
                        }
 
                        return e;
@@ -329,8 +504,10 @@ namespace Mono.CSharp {
                                return new CharConstant ((char)v);
                        else if (t == TypeManager.bool_type)
                                return new BoolConstant ((bool) v);
+                       else if (t == TypeManager.decimal_type)
+                               return new DecimalConstant ((decimal)v);
                        else if (TypeManager.IsEnumType (t)){
-                               Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
+                               Constant e = Constantify (v, TypeManager.EnumToUnderlying (v.GetType ()));
 
                                return new EnumConstant (e, t);
                        } else
@@ -348,7 +525,7 @@ namespace Mono.CSharp {
                        else if (mi is FieldInfo)
                                return new FieldExpr ((FieldInfo) mi, loc);
                        else if (mi is PropertyInfo)
-                               return new PropertyExpr ((PropertyInfo) mi, loc);
+                               return new PropertyExpr (ec, (PropertyInfo) mi, loc);
                        else if (mi is Type){
                                return new TypeExpr ((System.Type) mi, loc);
                        }
@@ -360,7 +537,7 @@ namespace Mono.CSharp {
                // 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
+               // measurements.  For example, we could use a delegate to 'flag' when
                // something can not any longer be a method-group (because it is something
                // else).
                //
@@ -372,7 +549,7 @@ namespace Mono.CSharp {
                //
                //     null on error.
                //
-               // FIXME: When calling MemberLookup inside an `Invocation', we should pass
+               // FIXME: When calling MemberLookup inside an 'Invocation', we should pass
                // the arguments here and have MemberLookup return only the methods that
                // match the argument count/type, unlike we are doing now (we delay this
                // decision).
@@ -387,19 +564,38 @@ namespace Mono.CSharp {
                public static Expression MemberLookup (EmitContext ec, Type t, string name,
                                                       MemberTypes mt, BindingFlags bf, Location loc)
                {
-                       MemberInfo [] mi = TypeManager.MemberLookup (ec.ContainerType, t, mt, bf, name);
+                       return MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
+               }
+
+               //
+               // Lookup type 't' for code in class 'invocation_type'.  Note that it's important
+               // to set 'invocation_type' correctly since this method also checks whether the
+               // invoking class is allowed to access the member in class 't'.  When you want to
+               // explicitly do a lookup in the base class, you must set both 't' and 'invocation_type'
+               // to the base class (although a derived class can access protected members of its base
+               // class it cannot do so through an instance of the base class (error CS1540)).
+               // 
+
+               public static Expression MemberLookup (EmitContext ec, Type invocation_type, Type t,
+                                                      string name, MemberTypes mt, BindingFlags bf,
+                                                      Location loc)
+               {
+                       MemberInfo [] mi = TypeManager.MemberLookup (invocation_type, t, mt, bf, name);
 
                        if (mi == null)
                                return null;
 
                        int count = mi.Length;
 
-                       if (count > 1)
-                               return new MethodGroupExpr (mi, loc);
-
                        if (mi [0] is MethodBase)
                                return new MethodGroupExpr (mi, loc);
 
+                       if (mi [0] is PropertyInfo)
+                               return new PropertyGroupExpr (mi, loc);
+
+                       if (count > 1)
+                               return null;
+
                        return ExprClassFromMemberInfo (ec, mi [0], loc);
                }
 
@@ -414,16 +610,18 @@ namespace Mono.CSharp {
                public const BindingFlags AllBindingFlags =
                        BindingFlags.Public |
                        BindingFlags.Static |
-                       BindingFlags.Instance;
+                       BindingFlags.Instance |
+                       BindingFlags.IgnoreCase;
 
                public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
                {
-                       return MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
+                       return MemberLookup (ec, ec.ContainerType, t, name, AllMemberTypes, AllBindingFlags, loc);
                }
 
                public static Expression MethodLookup (EmitContext ec, Type t, string name, Location loc)
                {
-                       return MemberLookup (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
+                       return MemberLookup (ec, ec.ContainerType, t, name,
+                                            MemberTypes.Method, AllBindingFlags, loc);
                }
 
                /// <summary>
@@ -445,7 +643,7 @@ namespace Mono.CSharp {
 
                        int errors = Report.Errors;
 
-                       e = MemberLookup (ec, t, name, mt, bf, loc);
+                       e = MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
 
                        if (e != null)
                                return e;
@@ -458,12 +656,12 @@ namespace Mono.CSharp {
                                          AllBindingFlags | BindingFlags.NonPublic, loc);
                        if (e == null){
                                Report.Error (
-                                       117, loc, "`" + t + "' does not contain a definition " +
-                                       "for `" + name + "'");
+                                       30456, loc, "'" + t + "' does not contain a definition " +
+                                       "for '" + name + "'");
                        } else {
-                               Report.Error (
-                                       122, loc, "`" + t + "." + name +
-                                       "' is inaccessible due to its protection level");
+                                       Report.Error (
+                                               30390, loc, "'" + t + "." + name +
+                                               "' is inaccessible due to its protection level");
                        }
                        
                        return null;
@@ -486,8 +684,12 @@ namespace Mono.CSharp {
 
                                expr.Emit (null);
                        }
-                       
-                       if (target_type == TypeManager.object_type) {
+
+                       //
+                       // notice that it is possible to write "ValueType v = 1", the ValueType here
+                       // is an abstract class, and not really a value type, so we apply the same rules.
+                       //
+                       if (target_type == TypeManager.object_type || target_type == TypeManager.value_type) {
                                //
                                // A pointer type cannot be converted to object
                                // 
@@ -499,6 +701,14 @@ namespace Mono.CSharp {
                                if (expr_type.IsClass || expr_type.IsInterface)
                                        return new EmptyCast (expr, target_type);
                        } else if (expr_type.IsSubclassOf (target_type)) {
+                               //
+                               // Special case: enumeration to System.Enum.
+                               // System.Enum is not a value type, it is a class, so we need
+                               // a boxing conversion
+                               //
+                               if (expr_type.IsEnum)
+                                       return new BoxedCast (expr);
+                       
                                return new EmptyCast (expr, target_type);
                        } else {
 
@@ -512,16 +722,17 @@ namespace Mono.CSharp {
                                        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
-                                               return null;
+                               if (target_type.IsInterface) {
+                                       if (TypeManager.ImplementsInterface (expr_type, target_type)){
+                                               if (expr_type.IsClass)
+                                                       return new EmptyCast (expr, target_type);
+                                               else if (expr_type.IsValueType)
+                                                       return new BoxedCast (expr);
+                                       }
                                }
 
                                // from any interface type S to interface-type T.
                                if (expr_type.IsInterface && target_type.IsInterface) {
-
                                        if (TypeManager.ImplementsInterface (expr_type, target_type))
                                                return new EmptyCast (expr, target_type);
                                        else
@@ -569,21 +780,6 @@ namespace Mono.CSharp {
                        return null;
                }
 
-               /// <summary>
-               ///   Handles expressions like this: decimal d; d = 1;
-               ///   and changes them into: decimal d; d = new System.Decimal (1);
-               /// </summary>
-               static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
-               {
-                       ArrayList args = new ArrayList ();
-
-                       args.Add (new Argument (expr, Argument.AType.Expression));
-
-                       Expression ne = new New (new TypeExpr (target, Location.Null), args, Location.Null);
-
-                       return ne.Resolve (ec);
-               }
-
                /// <summary>
                ///   Implicit Numeric Conversions.
                ///
@@ -598,14 +794,32 @@ namespace Mono.CSharp {
                        //
                        // Attempt to do the implicit constant expression conversions
 
-                       if (expr is IntConstant){
+                       if (expr is BoolConstant || expr is IntConstant || expr is LongConstant || expr is DoubleConstant || expr is FloatConstant){
                                Expression e;
                                
-                               e = TryImplicitIntConversion (target_type, (IntConstant) expr);
+                               e = TryImplicitNumericConversion (target_type, (Constant) expr);
 
                                if (e != null)
                                        return e;
-                       } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
+                               if (target_type == TypeManager.byte_type || 
+                                   target_type == TypeManager.short_type ||
+                                   target_type == TypeManager.int32_type ||
+                                   target_type == TypeManager.int64_type ||
+                                   target_type == TypeManager.float_type) {
+                                       
+                                       string val = null;
+                                       if (expr is IntConstant)
+                                               val = ((IntConstant) expr).Value.ToString();
+                                       if (expr is LongConstant)
+                                               val = ((LongConstant) expr).Value.ToString();
+                                       if (expr is FloatConstant)
+                                               val = ((FloatConstant) expr).Value.ToString();
+                                       if (expr is DoubleConstant)
+                                               val = ((DoubleConstant) expr).Value.ToString();
+                                       Error_ConstantValueCannotBeConverted(loc, val, target_type);
+                                       return null;
+                               }
+                       } 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,
@@ -616,20 +830,39 @@ namespace Mono.CSharp {
                                        return new ULongConstant ((ulong) v);
                        }
 
-                       //
-                       // If we have an enumeration, extract the underlying type,
-                       // use this during the comparission, but wrap around the original
-                       // target_type
-                       //
-                       Type real_target_type = target_type;
+                       Type real_target_type = target_type;
 
-                       if (TypeManager.IsEnumType (real_target_type))
-                               real_target_type = TypeManager.EnumToUnderlying (real_target_type);
+                       if (target_type == TypeManager.bool_type) {
 
-                       if (expr_type == real_target_type)
-                               return new EmptyCast (expr, target_type);
-                       
-                       if (expr_type == TypeManager.sbyte_type){
+                               if (expr_type == TypeManager.decimal_type) {
+                                       return RTConversionExpression (ec, "System.Convert",".ToBoolean" , expr, loc);
+                               }
+
+                               if ((expr_type != TypeManager.char_type) && 
+                                   (expr_type != TypeManager.string_type) &&
+                                   (expr_type != TypeManager.object_type))
+                                       return new NumericToBoolCast (expr, expr.Type);
+                       }
+
+                       if (expr_type == TypeManager.bool_type){
+                               //
+                               if (real_target_type == TypeManager.sbyte_type)
+                                       return new BoolToNumericCast (expr, target_type);
+                               if (real_target_type == TypeManager.byte_type) 
+                                       return new BoolToNumericCast (expr, target_type);
+                               if (real_target_type == TypeManager.int32_type)
+                                       return new BoolToNumericCast (expr, target_type);
+                               if (real_target_type == TypeManager.int64_type)
+                                       return new BoolToNumericCast (expr, target_type);
+                               if (real_target_type == TypeManager.double_type)
+                                       return new BoolToNumericCast (expr, target_type);
+                               if (real_target_type == TypeManager.float_type)
+                                       return new BoolToNumericCast (expr, target_type);
+                               if (real_target_type == TypeManager.short_type)
+                                       return new BoolToNumericCast (expr, target_type);
+                               if (real_target_type == TypeManager.decimal_type)
+                                       return RTConversionExpression(ec, "DecimalType.FromBoolean", expr, loc);
+                       } else if (expr_type == TypeManager.sbyte_type){
                                //
                                // From sbyte to short, int, long, float, double.
                                //
@@ -643,8 +876,6 @@ namespace Mono.CSharp {
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
                                if (real_target_type == TypeManager.short_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
-                               if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.byte_type){
                                //
                                // From byte to short, ushort, int, uint, long, ulong, float, double
@@ -663,8 +894,6 @@ namespace Mono.CSharp {
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
                                if (real_target_type == TypeManager.double_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
-                               if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.short_type){
                                //
                                // From short to int, long, float, double
@@ -677,8 +906,6 @@ namespace Mono.CSharp {
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
                                if (real_target_type == TypeManager.float_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
-                               if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.ushort_type){
                                //
                                // From ushort to int, uint, long, ulong, float, double
@@ -696,8 +923,6 @@ namespace Mono.CSharp {
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
                                if (real_target_type == TypeManager.float_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
-                               if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.int32_type){
                                //
                                // From int to long, float, double
@@ -708,8 +933,6 @@ namespace Mono.CSharp {
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
                                if (real_target_type == TypeManager.float_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
-                               if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.uint32_type){
                                //
                                // From uint to long, ulong, float, double
@@ -724,8 +947,6 @@ namespace Mono.CSharp {
                                if (real_target_type == TypeManager.float_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
                                                               OpCodes.Conv_R4);
-                               if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.int64_type){
                                //
                                // From long/ulong to float, double
@@ -734,8 +955,6 @@ namespace Mono.CSharp {
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
                                if (real_target_type == TypeManager.float_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);     
-                               if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.uint64_type){
                                //
                                // From ulong to float, double
@@ -746,33 +965,54 @@ namespace Mono.CSharp {
                                if (real_target_type == TypeManager.float_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
                                                               OpCodes.Conv_R4);        
+                       } else if (expr_type == TypeManager.string_type){
+
+                               if (real_target_type == TypeManager.bool_type)
+                                       return RTConversionExpression (ec, "BooleanType.FromString" , expr, loc);
                                if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
-                       } else if (expr_type == TypeManager.char_type){
-                               //
-                               // From char to ushort, int, uint, long, ulong, float, double
-                               // 
-                               if ((real_target_type == TypeManager.ushort_type) ||
-                                   (real_target_type == TypeManager.int32_type) ||
-                                   (real_target_type == TypeManager.uint32_type))
-                                       return new EmptyCast (expr, target_type);
-                               if (real_target_type == TypeManager.uint64_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
-                               if (real_target_type == TypeManager.int64_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
+                                       return RTConversionExpression (ec, "DecimalType.FromString" , expr, loc);
                                if (real_target_type == TypeManager.float_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
+                                       return RTConversionExpression (ec, "SingleType.FromString" , expr, loc);
+                               if (real_target_type == TypeManager.short_type)
+                                       return RTConversionExpression (ec, "ShortType.FromString" , expr, loc);
+                               if (real_target_type == TypeManager.int64_type)
+                                       return RTConversionExpression (ec, "LongType.FromString" , expr, loc);
+                               if (real_target_type == TypeManager.int32_type)
+                                       return RTConversionExpression (ec, "IntegerType.FromString" , expr, loc);
                                if (real_target_type == TypeManager.double_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
-                               if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
+                                       return RTConversionExpression (ec, "DoubleType.FromString" , expr, loc);
+                               if (real_target_type == TypeManager.byte_type)
+                                       return RTConversionExpression (ec, "ByteType.FromString" , expr, loc);
                        } else if (expr_type == TypeManager.float_type){
                                //
                                // float to double
                                //
+                               if (real_target_type == TypeManager.decimal_type)
+                                       return RTConversionExpression (ec, "System.Convert", ".ToDecimal" , expr, loc);
                                if (real_target_type == TypeManager.double_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
-                       }
+
+                       } else if (expr_type == TypeManager.double_type){
+
+                               if (real_target_type == TypeManager.decimal_type)
+                                       return RTConversionExpression (ec, "System.Convert", ".ToDecimal" , expr, loc);
+                       } else if (expr_type == TypeManager.decimal_type){
+
+                               if (real_target_type == TypeManager.bool_type)
+                                       return RTConversionExpression (ec, "BooleanType.FromDecimal" , expr, loc);
+                               if (real_target_type == TypeManager.short_type)
+                                       return RTConversionExpression(ec, "System.Convert", ".ToInt16", expr, loc);
+                               if (real_target_type == TypeManager.byte_type)
+                                       return RTConversionExpression(ec, "System.Convert", ".ToByte", expr, loc);
+                               if (real_target_type == TypeManager.int32_type)
+                                       return RTConversionExpression(ec, "System.Convert", ".ToInt32", expr, loc);
+                               if (real_target_type == TypeManager.int64_type)
+                                       return RTConversionExpression(ec, "System.Convert", ".ToInt64", expr, loc);
+                               if (real_target_type == TypeManager.float_type)
+                                       return RTConversionExpression(ec, "System.Convert", ".ToSingle", expr, loc);
+                               if (real_target_type == TypeManager.double_type)
+                                       return RTConversionExpression(ec, "System.Convert", ".ToDouble", expr, loc);
+                       }
 
                        return null;
                }
@@ -781,10 +1021,8 @@ namespace Mono.CSharp {
                // Tests whether an implicit reference conversion exists between expr_type
                // and target_type
                //
-               public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
+               public static bool ImplicitReferenceConversionExists (Expression expr, Type expr_type, Type target_type)
                {
-                       Type expr_type = expr.Type;
-                       
                        //
                        // This is the boxed case.
                        //
@@ -796,13 +1034,12 @@ namespace Mono.CSharp {
                                
                        } else if (expr_type.IsSubclassOf (target_type)) {
                                return true;
-                               
                        } else {
                                // Please remember that all code below actually comes
                                // from ImplicitReferenceConversion so make sure code remains in sync
                                
                                // from any class-type S to any interface-type T.
-                               if (expr_type.IsClass && target_type.IsInterface) {
+                               if (target_type.IsInterface) {
                                        if (TypeManager.ImplementsInterface (expr_type, target_type))
                                                return true;
                                }
@@ -865,27 +1102,53 @@ namespace Mono.CSharp {
                        if (StandardConversionExists (expr, target_type) == true)
                                return true;
 
+#if false
                        Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
 
                        if (dummy != null)
                                return true;
+#endif
 
                        return false;
                }
-               
+
                /// <summary>
                ///  Determines if a standard implicit conversion exists from
                ///  expr_type to target_type
                /// </summary>
                public static bool StandardConversionExists (Expression expr, Type target_type)
                {
-                       Type expr_type = expr.Type;
+                       return WideningConversionExists (expr, expr.type, target_type);
+               }
+
+               public static bool WideningConversionExists (Type expr_type, Type target_type)
+               {
+                       return WideningConversionExists (null, expr_type, target_type);
+               }
+
+               public static bool WideningConversionExists (Expression expr, Type target_type)
+               {
+                       return WideningConversionExists (expr, expr.Type, target_type);
+               }
+
+               public static bool WideningConversionExists (Expression expr, Type expr_type, Type target_type)
+               {
+
+                       if (expr_type == null || expr_type == TypeManager.void_type)
+                               return false;
                        
                        if (expr_type == target_type)
                                return true;
 
-                       // First numeric conversions 
+                       // Conversions from enum to underlying type are widening.
+                       if (expr_type.IsSubclassOf (TypeManager.enum_type))
+                               expr_type = TypeManager.EnumToUnderlying (expr_type);
 
+                       if (expr_type == target_type)
+                               return true;
+
+                       // First numeric conversions 
+                       
                        if (expr_type == TypeManager.sbyte_type){
                                //
                                // From sbyte to short, int, long, float, double.
@@ -903,6 +1166,7 @@ namespace Mono.CSharp {
                                // From byte to short, ushort, int, uint, long, ulong, float, double
                                // 
                                if ((target_type == TypeManager.short_type) ||
+                                   (target_type == TypeManager.bool_type) ||
                                    (target_type == TypeManager.ushort_type) ||
                                    (target_type == TypeManager.int32_type) ||
                                    (target_type == TypeManager.uint32_type) ||
@@ -918,6 +1182,7 @@ namespace Mono.CSharp {
                                // From short to int, long, float, double
                                // 
                                if ((target_type == TypeManager.int32_type) ||
+                                   (target_type == TypeManager.bool_type) ||
                                    (target_type == TypeManager.int64_type) ||
                                    (target_type == TypeManager.double_type) ||
                                    (target_type == TypeManager.float_type) ||
@@ -942,6 +1207,7 @@ namespace Mono.CSharp {
                                // From int to long, float, double
                                //
                                if ((target_type == TypeManager.int64_type) ||
+                                   (target_type == TypeManager.bool_type) ||
                                    (target_type == TypeManager.double_type) ||
                                    (target_type == TypeManager.float_type) ||
                                    (target_type == TypeManager.decimal_type))
@@ -952,6 +1218,7 @@ namespace Mono.CSharp {
                                // From uint to long, ulong, float, double
                                //
                                if ((target_type == TypeManager.int64_type) ||
+                                   (target_type == TypeManager.bool_type) ||
                                    (target_type == TypeManager.uint64_type) ||
                                    (target_type == TypeManager.double_type) ||
                                    (target_type == TypeManager.float_type) ||
@@ -964,35 +1231,31 @@ namespace Mono.CSharp {
                                // From long/ulong to float, double
                                //
                                if ((target_type == TypeManager.double_type) ||
+                                   (target_type == TypeManager.bool_type) ||
                                    (target_type == TypeManager.float_type) ||
                                    (target_type == TypeManager.decimal_type))
                                        return true;
                                    
-                       } else if (expr_type == TypeManager.char_type){
+                       } else if (expr_type == TypeManager.decimal_type) {
+                               if (target_type == TypeManager.float_type ||
+                                   target_type == TypeManager.double_type)
+                                       return true;
+                       } else if (expr_type == TypeManager.float_type){
                                //
-                               // From char to ushort, int, uint, long, ulong, float, double
-                               // 
-                               if ((target_type == TypeManager.ushort_type) ||
-                                   (target_type == TypeManager.int32_type) ||
-                                   (target_type == TypeManager.uint32_type) ||
-                                   (target_type == TypeManager.uint64_type) ||
-                                   (target_type == TypeManager.int64_type) ||
-                                   (target_type == TypeManager.float_type) ||
-                                   (target_type == TypeManager.double_type) ||
-                                   (target_type == TypeManager.decimal_type))
-                                       return true;
-
-                       } else if (expr_type == TypeManager.float_type){
-                               //
-                               // float to double
+                               // float to double, decimal
                                //
                                if (target_type == TypeManager.double_type)
                                        return true;
-                       }       
+                       } else if (expr_type == TypeManager.double_type){
+
+                               if ((target_type == TypeManager.bool_type))
+                                       return true;
+                       }               
                        
-                       if (ImplicitReferenceConversionExists (expr, target_type))
+                       if (ImplicitReferenceConversionExists (expr, expr_type, target_type))
                                return true;
                        
+/*
                        if (expr is IntConstant){
                                int value = ((IntConstant) expr).Value;
 
@@ -1035,6 +1298,7 @@ namespace Mono.CSharp {
                                if (v > 0)
                                        return true;
                        }
+*/
                        
                        if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
                                IntLiteral i = (IntLiteral) expr;
@@ -1042,6 +1306,10 @@ namespace Mono.CSharp {
                                if (i.Value == 0)
                                        return true;
                        }
+
+                       if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
+                               return true;
+
                        return false;
                }
 
@@ -1121,7 +1389,7 @@ namespace Mono.CSharp {
                ///   by making use of FindMostEncomp* methods. Applies the correct rules separately
                ///   for explicit and implicit conversion operators.
                /// </summary>
-               static public Type FindMostSpecificSource (MethodGroupExpr me, Type source_type,
+               static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
                                                           bool apply_explicit_conv_rules,
                                                           Location loc)
                {
@@ -1129,10 +1397,11 @@ namespace Mono.CSharp {
                        
                        if (priv_fms_expr == null)
                                priv_fms_expr = new EmptyExpression ();
-                       
+
                        //
                        // If any operator converts from S then Sx = S
                        //
+                       Type source_type= source.Type;
                        foreach (MethodBase mb in me.Methods){
                                ParameterData pd = Invocation.GetParameterData (mb);
                                Type param_type = pd.ParameterType (0);
@@ -1153,16 +1422,14 @@ namespace Mono.CSharp {
                                        if (StandardConversionExists (priv_fms_expr, source_type))
                                                src_types_set.Add (param_type);
                                        else {
-                                               priv_fms_expr.SetType (source_type);
-                                               if (StandardConversionExists (priv_fms_expr, param_type))
+                                               if (StandardConversionExists (source, param_type))
                                                        src_types_set.Add (param_type);
                                        }
                                } else {
                                        //
                                        // Only if S is encompassed by param_type
                                        //
-                                       priv_fms_expr.SetType (source_type);
-                                       if (StandardConversionExists (priv_fms_expr, param_type))
+                                       if (StandardConversionExists (source, param_type))
                                                src_types_set.Add (param_type);
                                }
                        }
@@ -1174,9 +1441,7 @@ namespace Mono.CSharp {
                                ArrayList candidate_set = new ArrayList ();
 
                                foreach (Type param_type in src_types_set){
-                                       priv_fms_expr.SetType (source_type);
-                                       
-                                       if (StandardConversionExists (priv_fms_expr, param_type))
+                                       if (StandardConversionExists (source, param_type))
                                                candidate_set.Add (param_type);
                                }
 
@@ -1269,7 +1534,7 @@ namespace Mono.CSharp {
                        //
                        if (apply_explicit_conv_rules)
                                return FindMostEncompassedType (tgt_types_set);
-                       else
+                       else 
                                return FindMostEncompassingType (tgt_types_set);
                }
                
@@ -1293,7 +1558,7 @@ namespace Mono.CSharp {
 
                /// <summary>
                ///   Computes the MethodGroup for the user-defined conversion
-               ///   operators from source_type to target_type.  `look_for_explicit'
+               ///   operators from source_type to target_type.  'look_for_explicit'
                ///   controls whether we should also include the list of explicit
                ///   operators
                /// </summary>
@@ -1391,14 +1656,14 @@ namespace Mono.CSharp {
                        }
 #endif
                        
-                       most_specific_source = FindMostSpecificSource (union, source_type, look_for_explicit, loc);
+                       most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
                        if (most_specific_source == null)
                                return null;
 
                        most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
                        if (most_specific_target == null) 
                                return null;
-                       
+
                        int count = 0;
 
                        foreach (MethodBase mb in union.Methods){
@@ -1412,10 +1677,9 @@ namespace Mono.CSharp {
                                }
                        }
                        
-                       if (method == null || count > 1) {
-                               Report.Error (-11, loc, "Ambiguous user defined conversion");
+                       if (method == null || count > 1)
                                return null;
-                       }
+                       
                        
                        //
                        // This will do the conversion to the best match that we
@@ -1443,9 +1707,9 @@ namespace Mono.CSharp {
                }
                
                /// <summary>
-               ///   Converts implicitly the resolved expression `expr' into the
-               ///   `target_type'.  It returns a new expression that can be used
-               ///   in a context that expects a `target_type'. 
+               ///   Converts implicitly the resolved expression 'expr' into the
+               ///   'target_type'.  It returns a new expression that can be used
+               ///   in a context that expects a 'target_type'. 
                /// </summary>
                static public Expression ConvertImplicit (EmitContext ec, Expression expr,
                                                          Type target_type, Location loc)
@@ -1453,6 +1717,7 @@ namespace Mono.CSharp {
                        Type expr_type = expr.Type;
                        Expression e;
 
+
                        if (expr_type == target_type)
                                return expr;
 
@@ -1462,47 +1727,592 @@ namespace Mono.CSharp {
                        e = ConvertImplicitStandard (ec, expr, target_type, loc);
                        if (e != null)
                                return e;
-
+                                       
                        e = ImplicitUserConversion (ec, expr, target_type, loc);
+                       
                        if (e != null)
                                return e;
 
+                       e = NarrowingConversion (ec, expr, target_type, loc);
+                       if (e != null)
+                               return e;                               
+
                        return null;
                }
 
+               /// <summary>
+               ///   Converts the resolved expression 'expr' into the
+               ///   'target_type' using the Microsoft.VisualBasic runtime.
+               ///   It returns a new expression that can be used
+               ///   in a context that expects a 'target_type'. 
+               /// </summary>
+               static private Expression RTConversionExpression (EmitContext ec, string s, Expression expr, Location loc)
+               {
+                       Expression etmp, e;
+                       ArrayList args;
+                       Argument arg;
+                       
+                       etmp = Mono.MonoBASIC.Parser.DecomposeQI("Microsoft.VisualBasic.CompilerServices." + s, loc);
+                       args = new ArrayList();
+                       arg = new Argument (expr, Argument.AType.Expression);
+                       args.Add (arg);
+                       e = (Expression) new Invocation (etmp, args, loc);
+                       e = e.Resolve(ec);      
+                       return (e);             
+               }
+
+               static private Expression RTConversionExpression (EmitContext ec, string ns, string method, Expression expr, Location loc)
+               {
+                       Expression etmp, e;
+                       ArrayList args;
+                       Argument arg;
+                       
+                       etmp = Mono.MonoBASIC.Parser.DecomposeQI(ns+method, loc);
+                       args = new ArrayList();
+                       arg = new Argument (expr, Argument.AType.Expression);
+                       args.Add (arg);
+                       e = (Expression) new Invocation (etmp, args, loc);
+                       e = e.Resolve(ec);      
+                       return (e);             
+               }
+
+               
+               static public bool NarrowingConversionExists (EmitContext ec, Expression expr, Type target_type)
+               {
+                       Type expr_type = expr.Type;
+                       if (expr_type.IsSubclassOf (TypeManager.enum_type))
+                               expr_type = TypeManager.EnumToUnderlying (expr_type);
+       
+                       if (target_type.IsSubclassOf (TypeManager.enum_type))
+                               target_type = TypeManager.EnumToUnderlying (target_type);
+
+
+                       if (expr_type == target_type)
+                               return true;
+
+                       if (target_type == TypeManager.sbyte_type){
+                               //
+                               // To sbyte from short, int, long, float, double.
+                               //
+                               if ((expr_type == TypeManager.int32_type) || 
+                                   (expr_type == TypeManager.int64_type) ||
+                                   (expr_type == TypeManager.double_type) ||
+                                   (expr_type == TypeManager.float_type)  ||
+                                   (expr_type == TypeManager.short_type) ||
+                                   (expr_type == TypeManager.decimal_type))
+                                       return true;
+                               
+                       } else if (target_type == TypeManager.byte_type){
+                               //
+                               // To byte from short, ushort, int, uint, long, ulong, float, double
+                               // 
+                               if ((expr_type == TypeManager.short_type) ||
+                                   (expr_type == TypeManager.ushort_type) ||
+                                   (expr_type == TypeManager.int32_type) ||
+                                   (expr_type == TypeManager.uint32_type) ||
+                                   (expr_type == TypeManager.uint64_type) ||
+                                   (expr_type == TypeManager.int64_type) ||
+                                   (expr_type == TypeManager.float_type) ||
+                                   (expr_type == TypeManager.double_type) ||
+                                   (expr_type == TypeManager.decimal_type))
+                                       return true;
+       
+                       } else if (target_type == TypeManager.short_type){
+                               //
+                               // To short from int, long, float, double
+                               // 
+                               if ((expr_type == TypeManager.int32_type) ||
+                                   (expr_type == TypeManager.int64_type) ||
+                                   (expr_type == TypeManager.double_type) ||
+                                   (expr_type == TypeManager.float_type) ||
+                                   (expr_type == TypeManager.decimal_type))
+                                       return true;
+                                       
+                       } else if (target_type == TypeManager.ushort_type){
+                               //
+                               // To ushort from int, uint, long, ulong, float, double
+                               //
+                               if ((expr_type == TypeManager.uint32_type) ||
+                                   (expr_type == TypeManager.uint64_type) ||
+                                   (expr_type == TypeManager.int32_type) ||
+                                   (expr_type == TypeManager.int64_type) ||
+                                   (expr_type == TypeManager.double_type) ||
+                                   (expr_type == TypeManager.float_type) ||
+                                   (expr_type == TypeManager.decimal_type))
+                                       return true;
+                                   
+                       } else if (target_type == TypeManager.int32_type){
+                               //
+                               // To int from long, float, double
+                               //
+                               if ((expr_type == TypeManager.int64_type) ||
+                                   (expr_type == TypeManager.double_type) ||
+                                   (expr_type == TypeManager.float_type) ||
+                                   (expr_type == TypeManager.decimal_type))
+                                       return true;
+                                       
+                       } else if (target_type == TypeManager.uint32_type){
+                               //
+                               // To uint from long, ulong, float, double
+                               //
+                               if ((expr_type == TypeManager.int64_type) ||
+                                   (expr_type == TypeManager.uint64_type) ||
+                                   (expr_type == TypeManager.double_type) ||
+                                   (expr_type == TypeManager.float_type) ||
+                                   (expr_type == TypeManager.decimal_type))
+                                       return true;
+                                       
+                       } else if ((target_type == TypeManager.uint64_type) ||
+                                  (target_type == TypeManager.int64_type)) {
+                               //
+                               // To long/ulong from float, double
+                               //
+                               if ((expr_type == TypeManager.double_type) ||
+                                   (expr_type == TypeManager.float_type) ||
+                                   (expr_type == TypeManager.decimal_type))
+                                       return true;
+                                   
+                       } else if (target_type == TypeManager.decimal_type){
+                               if (expr_type == TypeManager.float_type ||
+                                   expr_type == TypeManager.double_type)
+                                       return true;
+                       } else if (target_type == TypeManager.float_type){
+                               //
+                               // To float from double
+                               //
+                               if (expr_type == TypeManager.double_type)
+                                       return true;
+                       }       
+
+                       return (NarrowingConversion (ec, expr, target_type,Location.Null)) != null;     
+               }
                
+               static public Expression NarrowingConversion (EmitContext ec, Expression expr,
+                                                               Type target_type, Location loc)
+               {
+                       Type expr_type = expr.Type;
+
+                       if (expr_type.IsSubclassOf (TypeManager.enum_type))
+                               expr_type = TypeManager.EnumToUnderlying (expr_type);
+       
+                       if (target_type.IsSubclassOf (TypeManager.enum_type))
+                               target_type = TypeManager.EnumToUnderlying (target_type);
+
+                       if (expr_type == target_type)
+                               return expr;
+
+                       if (target_type == TypeManager.sbyte_type){
+                               //
+                               // To sbyte from short, int, long, float, double.
+                               //
+                               if (expr_type == TypeManager.int32_type) 
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
+                               if (expr_type == TypeManager.int64_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
+                               if (expr_type == TypeManager.short_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
+
+                               if (expr_type == TypeManager.float_type) {
+                                       Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_I1);
+                               }
+                               if (expr_type == TypeManager.double_type) {
+                                       Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_I1);
+                               }
+                               
+                       } else if (target_type == TypeManager.byte_type){
+                               //
+                               // To byte from short, ushort, int, uint, long, ulong, float, double
+                               // 
+                               if (expr_type == TypeManager.short_type) 
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
+                               if (expr_type == TypeManager.ushort_type) 
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
+                               if (expr_type == TypeManager.int32_type) 
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
+                               if (expr_type == TypeManager.uint32_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
+                               if (expr_type == TypeManager.uint64_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
+                               if (expr_type == TypeManager.int64_type) 
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
+
+                               if (expr_type == TypeManager.float_type) {
+                                       Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_U1);
+                               }
+                               if (expr_type == TypeManager.double_type) {
+                                       Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_U1);
+                               }
+       
+                       } else if (target_type == TypeManager.short_type) {
+                               //
+                               // To short from int, long, float, double
+                               // 
+                               if (expr_type == TypeManager.int32_type) 
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
+                               if (expr_type == TypeManager.int64_type) 
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
+
+                               if (expr_type == TypeManager.float_type) {
+                                       Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_I2);
+                               }
+                               if (expr_type == TypeManager.double_type) {
+                                       Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_I2);
+                               }
+                                       
+                       } else if (target_type == TypeManager.ushort_type) {
+                               //
+                               // To ushort from int, uint, long, ulong, float, double
+                               //
+                               if (expr_type == TypeManager.uint32_type) 
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
+                               if (expr_type == TypeManager.uint64_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
+                               if (expr_type == TypeManager.int32_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
+                               if (expr_type == TypeManager.int64_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
+
+                               if (expr_type == TypeManager.float_type) {
+                                       Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_U2);
+                               }
+
+                               if (expr_type == TypeManager.double_type) {
+                                       Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_U2);
+                               }
+                                   
+                       } else if (target_type == TypeManager.int32_type){
+                               //
+                               // To int from long, float, double
+                               //
+                               if (expr_type == TypeManager.int64_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
+
+                               if (expr_type == TypeManager.float_type) {
+                                       Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_I4);
+                               }
+                               if (expr_type == TypeManager.double_type) {
+                                       Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_I4);
+                               }
+                                       
+                       } else if (target_type == TypeManager.uint32_type){
+                               //
+                               // To uint from long, ulong, float, double
+                               //
+                               if (expr_type == TypeManager.int64_type) 
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
+                               if (expr_type == TypeManager.uint64_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
+                               if (expr_type == TypeManager.float_type) {
+                                       Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_U4);
+                               }
+                               if (expr_type == TypeManager.double_type) {
+                                       Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_U4);
+                               }
+                                       
+                       } else if (target_type == TypeManager.uint64_type) {
+                               //
+                               // To long/ulong from float, double
+                               //
+                               if (expr_type == TypeManager.float_type) {
+                                       Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_U8);
+                               }
+                               if (expr_type == TypeManager.double_type) {
+                                       Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_U8);
+                               }
+                                   
+                       } else if (target_type == TypeManager.int64_type) {
+                               //
+                               // To long/ulong from float, double
+                               //
+                               if (expr_type == TypeManager.float_type) {
+                                       Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_I8);
+                               }
+                               if (expr_type == TypeManager.double_type) {
+                                       Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_I8);
+                               }
+
+                       } else if (target_type == TypeManager.float_type){
+                               //
+                               // To float from double
+                               //
+                               if (expr_type == TypeManager.double_type)
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
+                       }       
+
+                       TypeCode dest_type = Type.GetTypeCode (target_type);
+                       TypeCode src_type = Type.GetTypeCode (expr_type);
+                       Expression e = null;
+
+                       switch (dest_type) {
+                               case TypeCode.String:
+                                       switch (src_type) {
+                                               case TypeCode.SByte:                                            
+                                               case TypeCode.Byte:
+                                                       e = RTConversionExpression(ec, "StringType.FromByte", expr, loc);
+                                                       break;  
+                                               case TypeCode.UInt16:
+                                               case TypeCode.Int16:
+                                                       e = RTConversionExpression(ec, "StringType.FromShort", expr, loc);
+                                                       break;          
+                                               case TypeCode.UInt32:                                   
+                                               case TypeCode.Int32:
+                                                       e = RTConversionExpression(ec, "StringType.FromInteger", expr, loc);
+                                                       break;                                                  
+                                               case TypeCode.UInt64:   
+                                               case TypeCode.Int64:
+                                                       e = RTConversionExpression(ec, "StringType.FromLong", expr, loc);
+                                                       break;                                                  
+                                               case TypeCode.Char:
+                                                       e = RTConversionExpression(ec, "StringType.FromChar", expr, loc);
+                                                       break;                                                          
+                                               case TypeCode.Single:
+                                                       e = RTConversionExpression(ec, "StringType.FromSingle", expr, loc);
+                                                       break;          
+                                               case TypeCode.Double:
+                                                       e = RTConversionExpression(ec, "StringType.FromDouble", expr, loc);
+                                                       break;                                                                                                                                                  
+                                               case TypeCode.Boolean:
+                                                       e = RTConversionExpression(ec, "StringType.FromBoolean", expr, loc);
+                                                       break;  
+                                               case TypeCode.DateTime:
+                                                       e = RTConversionExpression(ec, "StringType.FromDate", expr, loc);
+                                                       break;          
+                                               case TypeCode.Decimal:
+                                                       e = RTConversionExpression(ec, "StringType.FromDecimal", expr, loc);
+                                                       break;          
+                                               case TypeCode.Object:
+                                                       e = RTConversionExpression(ec, "StringType.FromObject", expr, loc);
+                                                       break;                                                                                                                                                                                                                          
+                                       }
+                                       break;
+                                       
+                               case TypeCode.Double:
+                                       switch (src_type) {                                             
+                                               case TypeCode.String:                           
+                                                       e = RTConversionExpression(ec, "DoubleType.FromString", expr, loc);
+                                                       break;          
+                                               case TypeCode.Object:                           
+                                                       e = RTConversionExpression(ec, "DoubleType.FromObject", expr, loc);
+                                                       break;                                                                                  
+                                       }
+                                       break;  
+                                       
+                               case TypeCode.Single:
+                                       switch (src_type) {                                             
+                                               case TypeCode.String:                           
+                                                       e = RTConversionExpression(ec, "SingleType.FromString", expr, loc);
+                                                       break;          
+                                               case TypeCode.Object:                           
+                                                       e = RTConversionExpression(ec, "SingleType.FromObject", expr, loc);
+                                                       break;                                                                                  
+                                       }
+                                       break;  
+                                       
+                               case TypeCode.Decimal:
+                                       switch (src_type) {                                             
+                                               case TypeCode.String:                           
+                                                       e = RTConversionExpression(ec, "DecimalType.FromString", expr, loc);
+                                                       break;          
+                                               case TypeCode.Object:                           
+                                                       e = RTConversionExpression(ec, "DecimalType.FromObject", expr, loc);
+                                                       break;                                                                                  
+                                       }
+                                       break;  
+                                       
+                               case TypeCode.Int64:
+                               case TypeCode.UInt64:   
+                                       switch (src_type) {                                             
+                                               case TypeCode.String:                           
+                                                       e = RTConversionExpression(ec, "LongType.FromString", expr, loc);
+                                                       break;          
+                                               case TypeCode.Object:                           
+                                                       e = RTConversionExpression(ec, "LongType.FromObject", expr, loc);
+                                                       break;                                                                                  
+                                       }
+                                       break;  
+                               case TypeCode.Int32:
+                               case TypeCode.UInt32:   
+                                       switch (src_type) {                                             
+                                               case TypeCode.String:                           
+                                                       e = RTConversionExpression(ec, "IntegerType.FromString", expr, loc);
+                                                       break;          
+                                               case TypeCode.Object:                           
+                                                       e = RTConversionExpression(ec, "IntegerType.FromObject", expr, loc);
+                                                       break;                                                                                  
+                                       }
+                                       break;  
+
+                               case TypeCode.Int16:
+                               case TypeCode.UInt16:   
+                                       switch (src_type) {                                             
+                                               case TypeCode.String:                           
+                                                       e = RTConversionExpression(ec, "ShortType.FromString", expr, loc);
+                                                       break;          
+                                               case TypeCode.Object:                           
+                                                       e = RTConversionExpression(ec, "ShortType.FromObject", expr, loc);
+                                                       break;                                                                                  
+                                       }
+                                       break;  
+                               case TypeCode.Byte:
+                                       
+                                       switch (src_type) {                                             
+                                               case TypeCode.String:                           
+                                                       e = RTConversionExpression(ec, "BooleanType.FromString", expr, loc);
+                                                       break;          
+                                               case TypeCode.Object:                           
+                                                       e = RTConversionExpression(ec, "ByteType.FromObject", expr, loc);
+                                                       break;  
+                                       }
+                                       break;
+                               case TypeCode.Boolean:  
+                                       switch (src_type) {                                             
+                                               case TypeCode.String:                           
+                                                       e = RTConversionExpression(ec, "BooleanType.FromString", expr, loc);
+                                                       break;          
+                                               case TypeCode.Object:                           
+                                                       e = RTConversionExpression(ec, "BooleanType.FromObject", expr, loc);
+                                                       break;                                                                                  
+                                       }
+                                       break;
+                               case TypeCode.DateTime: 
+                                       switch (src_type) {                                             
+                                               case TypeCode.String:                           
+                                                       e = RTConversionExpression(ec, "DateType.FromString", expr, loc);
+                                                       break;          
+                                               case TypeCode.Object:                           
+                                                       e = RTConversionExpression(ec, "DateType.FromObject", expr, loc);
+                                                       break;                                                                                  
+                                       }
+                                       break;
+                               case TypeCode.Char:
+                                       switch (src_type) {
+
+                                               case TypeCode.String:
+                                                       e = RTConversionExpression(ec, "CharType.FromString", expr, loc);
+                                                       break;                  
+                                       
+                               }       
+                               break;                                                                                                          
+                       }
+                       
+                       // We must examine separately some types that
+                       // don't have a TypeCode but are supported 
+                       // in the runtime
+                       if (expr_type == typeof(System.String) && target_type == typeof (System.Char[])) {
+                               e = RTConversionExpression(ec, "CharArrayType.FromString", expr, loc);
+                       }
+                       if (e != null)  
+                               return e;
+                       // VB.NET Objects can be converted to anything by default
+                       // unless, that is, an exception at runtime blows it all
+                       if (src_type == TypeCode.Object) {
+                               Expression cast_type = Mono.MonoBASIC.Parser.DecomposeQI(target_type.ToString(), loc);
+                               Cast ce = new Cast (cast_type, expr, loc);
+                               ce.IsRuntimeCast = true;
+                               return ce.Resolve (ec);
+                       }
+                     return null;
+               }
+
+               static public Expression ConvertNothingToDefaultValues (EmitContext ec, Expression expr,
+                                                                       Type target_type, Location loc)
+               {
+                       switch (Type.GetTypeCode (target_type)) {
+                       case TypeCode.Boolean  :
+                               return new BoolConstant (false);
+                       case TypeCode.Byte  :
+                               return new ByteConstant (0);
+                       case TypeCode.Char  :
+                               return new CharConstant ((char)0);
+                       case TypeCode.SByte :
+                               return new SByteConstant (0);
+                       case TypeCode.Int16 :
+                               return new ShortConstant (0);
+                       case TypeCode.Int32 :
+                               return new IntConstant (0);
+                       case TypeCode.Int64 :
+                               return new LongConstant (0);
+                       case TypeCode.Decimal :
+                               return new DecimalConstant (System.Decimal.Zero);
+                       case TypeCode.Single :
+                               return new FloatConstant (0.0F);
+                       case TypeCode.Double :
+                               return new DoubleConstant (0.0);
+                       }
+
+                       return null;
+               }
+                                                                                               
                /// <summary>
-               ///   Attempts to apply the `Standard Implicit
-               ///   Conversion' rules to the expression `expr' into
-               ///   the `target_type'.  It returns a new expression
+               ///   Attempts to apply the 'Standard Implicit
+               ///   Conversion' rules to the expression 'expr' into
+               ///   the 'target_type'.  It returns a new expression
                ///   that can be used in a context that expects a
-               ///   `target_type'.
+               ///   'target_type'.
                ///
-               ///   This is different from `ConvertImplicit' in that the
+               ///   This is different from 'ConvertImplicit' in that the
                ///   user defined implicit conversions are excluded. 
                /// </summary>
                static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
                                                                  Type target_type, Location loc)
                {
                        Type expr_type = expr.Type;
+
+                       if (expr_type.IsSubclassOf (TypeManager.enum_type))
+                               expr_type = TypeManager.EnumToUnderlying (expr_type);
+
                        Expression e;
 
+                       if (expr is NullLiteral) {
+                               if (target_type == TypeManager.string_type)
+                                       return expr;
+                               e = ConvertNothingToDefaultValues (ec, expr, target_type, loc);
+                               if (e != null)
+                                       return e;
+                       }
+
                        if (expr_type == target_type)
                                return expr;
 
                        e = ImplicitNumericConversion (ec, expr, target_type, loc);
+
                        if (e != null)
                                return e;
 
+                       if (expr is StringConstant && target_type == TypeManager.char_type)
+                               return new CharConstant (((StringConstant) expr).Value [0]);
+
+                       if (expr is CharConstant && target_type == TypeManager.string_type)
+                               return new StringConstant (((CharConstant) expr).Value.ToString ());
+
                        e = ImplicitReferenceConversion (expr, target_type);
                        if (e != null)
                                return e;
 
-                       if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
-                               IntLiteral i = (IntLiteral) expr;
-
-                               if (i.Value == 0)
-                                       return new EmptyCast (expr, target_type);
+                       if (expr.Type.IsSubclassOf (TypeManager.enum_type)) {
+                               expr_type = TypeManager.EnumToUnderlying (expr.Type);
+                               expr = new EmptyCast (expr, expr_type);
+                               if (expr_type == target_type)
+                                       return expr;
+                               e = ImplicitNumericConversion (ec, expr, target_type, loc);
+                               if (e != null)
+                                       return e;
                        }
 
                        if (ec.InUnsafe) {
@@ -1530,32 +2340,63 @@ namespace Mono.CSharp {
                }
 
                /// <summary>
-               ///   Attemps to perform an implict constant conversion of the IntConstant
+               ///   Attemps to perform an implict constant conversion of the any Numeric Constant
                ///   into a different data type using casts (See Implicit Constant
                ///   Expression Conversions)
                /// </summary>
-               static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
+               static protected Expression TryImplicitNumericConversion (Type target_type, Constant ic)
                {
-                       int value = ic.Value;
+                       double value = 0;
+                       if (ic is BoolConstant) {
+                               bool val = (bool) ((BoolConstant)ic).Value;
+                               if (val) {
+                                       if (target_type == TypeManager.byte_type)
+                                               value = Byte.MaxValue;
+                                       else 
+                                               value = -1;
+                               }
+                       }
+                       if (ic is IntConstant) 
+                               value = (double)((IntConstant)ic).Value;
+                       
+                       if (ic is LongConstant) 
+                               value = (double) ((LongConstant)ic).Value;
+
+                       if (ic is FloatConstant) {
+                               value = (double) ((FloatConstant)ic).Value;
+                       }
+
+                       if (ic is DoubleConstant) {
+                               value = ((DoubleConstant)ic).Value;
+                       }
 
                        //
                        // FIXME: This could return constants instead of EmptyCasts
                        //
-                       if (target_type == TypeManager.sbyte_type){
+                       if (target_type == TypeManager.bool_type){
+                               if (value != 0)
+                                       return new BoolConstant (true);
+                               return new BoolConstant (false);
+                       } else if (target_type == TypeManager.sbyte_type){
                                if (value >= SByte.MinValue && value <= SByte.MaxValue)
-                                       return new SByteConstant ((sbyte) value);
+                                       return new SByteConstant ((sbyte) System.Math.Round (value));
                        } else if (target_type == TypeManager.byte_type){
-                               if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
-                                       return new ByteConstant ((byte) value);
+                               if (value >= Byte.MinValue && value <= Byte.MaxValue)
+                                       return new ByteConstant ((byte) System.Math.Round (value));
                        } else if (target_type == TypeManager.short_type){
                                if (value >= Int16.MinValue && value <= Int16.MaxValue)
-                                       return new ShortConstant ((short) value);
+                                       return new ShortConstant ((short) System.Math.Round (value));
                        } else if (target_type == TypeManager.ushort_type){
                                if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
-                                       return new UShortConstant ((ushort) value);
+                                       return new UShortConstant ((ushort) System.Math.Round (value));
+                       } else if (target_type == TypeManager.int32_type){
+                               if (value >= Int32.MinValue && value <= Int32.MaxValue)
+                                       return new IntConstant ((int) System.Math.Round (value));
                        } else if (target_type == TypeManager.uint32_type){
                                if (value >= 0)
-                                       return new UIntConstant ((uint) value);
+                                       return new UIntConstant ((uint) System.Math.Round (value));
+                       } else if (target_type == TypeManager.int64_type){
+                                       return new LongConstant ((long) System.Math.Round (value));
                        } else if (target_type == TypeManager.uint64_type){
                                //
                                // we can optimize this case: a positive int32
@@ -1563,26 +2404,44 @@ namespace Mono.CSharp {
                                // to do it.
                                //
                                if (value >= 0)
-                                       return new ULongConstant ((ulong) value);
+                                       return new ULongConstant ((ulong)System.Math.Round ( value));
+                       } else if (target_type == TypeManager.float_type){
+                                       return new FloatConstant ((float) value);
+                       } else if (target_type == TypeManager.double_type){
+                                       return new DoubleConstant ((double) value);
                        }
                        
-                       if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
-                               return new EnumConstant (ic, target_type);
-
+                       if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
+                               Type underlying = TypeManager.EnumToUnderlying (target_type);
+                               Constant e = (Constant) ic;
+                               
+                               //
+                               // Possibly, we need to create a different 0 literal before passing
+                               // to EnumConstant
+                               //n
+                               if (underlying == TypeManager.int64_type)
+                                       e = new LongLiteral (0);
+                               else if (underlying == TypeManager.uint64_type)
+                                       e = new ULongLiteral (0);
+
+                               return new EnumConstant (e, target_type);
+                       }
                        return null;
                }
 
                static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
                {
-                       string msg = "Cannot convert implicitly from `"+
-                               TypeManager.CSharpName (source) + "' to `" +
-                               TypeManager.CSharpName (target) + "'";
+                       string msg = "Cannot convert implicitly from '"+
+                               TypeManager.MonoBASIC_Name (source) + "' to '" +
+                               TypeManager.MonoBASIC_Name (target) + "'";
 
-                       Report.Error (29, loc, msg);
+                       throw new Exception (msg);
+
+                       // Report.Error (30512, loc, msg);
                }
 
                /// <summary>
-               ///   Attemptes to implicityly convert `target' into `type', using
+               ///   Attemptes to implicityly convert 'target' into 'type', using
                ///   ConvertImplicit.  If there is no implicit conversion, then
                ///   an error is signaled
                /// </summary>
@@ -1592,15 +2451,17 @@ namespace Mono.CSharp {
                        Expression e;
                        
                        e = ConvertImplicit (ec, source, target_type, loc);
+
                        if (e != null)
                                return e;
 
+
                        if (source is DoubleLiteral && target_type == TypeManager.float_type){
                                Report.Error (664, loc,
                                              "Double literal cannot be implicitly converted to " +
                                              "float type, use F suffix to create a float literal");
                        }
-                       
+
                        Error_CannotConvertImplicit (loc, source.Type, target_type);
 
                        return null;
@@ -1609,13 +2470,13 @@ namespace Mono.CSharp {
                /// <summary>
                ///   Performs the explicit numeric conversions
                /// </summary>
-               static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type)
+               static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type, Location loc)
                {
                        Type expr_type = expr.Type;
 
                        //
                        // If we have an enumeration, extract the underlying type,
-                       // use this during the comparission, but wrap around the original
+                       // use this during the comparison, but wrap around the original
                        // target_type
                        //
                        Type real_target_type = target_type;
@@ -1623,6 +2484,14 @@ namespace Mono.CSharp {
                        if (TypeManager.IsEnumType (real_target_type))
                                real_target_type = TypeManager.EnumToUnderlying (real_target_type);
 
+                       if (StandardConversionExists (expr, real_target_type)){
+                               Expression ce = ConvertImplicitStandard (ec, expr, real_target_type, loc);
+
+                               if (real_target_type != target_type)
+                                       return new EmptyCast (ce, target_type);
+                               return ce;
+                       }
+                       
                        if (expr_type == TypeManager.sbyte_type){
                                //
                                // From sbyte to byte, ushort, uint, ulong, char
@@ -1635,16 +2504,12 @@ namespace Mono.CSharp {
                                        return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
                                if (real_target_type == TypeManager.uint64_type)
                                        return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
-                               if (real_target_type == TypeManager.char_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
                        } else if (expr_type == TypeManager.byte_type){
                                //
                                // From byte to sbyte and char
                                //
                                if (real_target_type == TypeManager.sbyte_type)
                                        return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
-                               if (real_target_type == TypeManager.char_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
                        } else if (expr_type == TypeManager.short_type){
                                //
                                // From short to sbyte, byte, ushort, uint, ulong, char
@@ -1659,8 +2524,6 @@ namespace Mono.CSharp {
                                        return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
                                if (real_target_type == TypeManager.uint64_type)
                                        return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
-                               if (real_target_type == TypeManager.char_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
                        } else if (expr_type == TypeManager.ushort_type){
                                //
                                // From ushort to sbyte, byte, short, char
@@ -1671,8 +2534,6 @@ namespace Mono.CSharp {
                                        return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
                                if (real_target_type == TypeManager.short_type)
                                        return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
-                               if (real_target_type == TypeManager.char_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
                        } else if (expr_type == TypeManager.int32_type){
                                //
                                // From int to sbyte, byte, short, ushort, uint, ulong, char
@@ -1689,8 +2550,6 @@ namespace Mono.CSharp {
                                        return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
                                if (real_target_type == TypeManager.uint64_type)
                                        return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
-                               if (real_target_type == TypeManager.char_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
                        } else if (expr_type == TypeManager.uint32_type){
                                //
                                // From uint to sbyte, byte, short, ushort, int, char
@@ -1705,8 +2564,6 @@ namespace Mono.CSharp {
                                        return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
                                if (real_target_type == TypeManager.int32_type)
                                        return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
-                               if (real_target_type == TypeManager.char_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
                        } else if (expr_type == TypeManager.int64_type){
                                //
                                // From long to sbyte, byte, short, ushort, int, uint, ulong, char
@@ -1725,8 +2582,6 @@ namespace Mono.CSharp {
                                        return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
                                if (real_target_type == TypeManager.uint64_type)
                                        return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
-                               if (real_target_type == TypeManager.char_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
                        } else if (expr_type == TypeManager.uint64_type){
                                //
                                // From ulong to sbyte, byte, short, ushort, int, uint, long, char
@@ -1745,72 +2600,54 @@ namespace Mono.CSharp {
                                        return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
                                if (real_target_type == TypeManager.int64_type)
                                        return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
-                               if (real_target_type == TypeManager.char_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
-                       } else if (expr_type == TypeManager.char_type){
-                               //
-                               // From char to sbyte, byte, short
-                               //
-                               if (real_target_type == TypeManager.sbyte_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
-                               if (real_target_type == TypeManager.byte_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
-                               if (real_target_type == TypeManager.short_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
                        } else if (expr_type == TypeManager.float_type){
                                //
                                // From float to sbyte, byte, short,
                                // ushort, int, uint, long, ulong, char
                                // or decimal
                                //
+                               Expression rounded_expr = RTConversionExpression(ec, "System.Math",".Round" , expr, loc);
                                if (real_target_type == TypeManager.sbyte_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_I1);
                                if (real_target_type == TypeManager.byte_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_U1);
                                if (real_target_type == TypeManager.short_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_I2);
                                if (real_target_type == TypeManager.ushort_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_U2);
                                if (real_target_type == TypeManager.int32_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_I4);
                                if (real_target_type == TypeManager.uint32_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_U4);
                                if (real_target_type == TypeManager.int64_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_I8);
                                if (real_target_type == TypeManager.uint64_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
-                               if (real_target_type == TypeManager.char_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
-                               if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_U8);
                        } else if (expr_type == TypeManager.double_type){
                                //
                                // From double to byte, byte, short,
                                // ushort, int, uint, long, ulong,
                                // char, float or decimal
                                //
+                               Expression rounded_expr = RTConversionExpression(ec, "System.Math",".Round" , expr, loc);
                                if (real_target_type == TypeManager.sbyte_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_I1);
                                if (real_target_type == TypeManager.byte_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_U1);
                                if (real_target_type == TypeManager.short_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_I2);
                                if (real_target_type == TypeManager.ushort_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_U2);
                                if (real_target_type == TypeManager.int32_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_I4);
                                if (real_target_type == TypeManager.uint32_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_U4);
                                if (real_target_type == TypeManager.int64_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_I8);
                                if (real_target_type == TypeManager.uint64_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
-                               if (real_target_type == TypeManager.char_type)
-                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
+                                       return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_U8);
                                if (real_target_type == TypeManager.float_type)
                                        return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
-                               if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } 
 
                        // decimal is taken care of by the op_Explicit methods.
@@ -2011,11 +2848,11 @@ namespace Mono.CSharp {
                }
                
                /// <summary>
-               ///   Performs an explicit conversion of the expression `expr' whose
-               ///   type is expr.Type to `target_type'.
+               ///   Performs an explicit conversion of the expression 'expr' whose
+               ///   type is expr.Type to 'target_type'.
                /// </summary>
                static public Expression ConvertExplicit (EmitContext ec, Expression expr,
-                                                         Type target_type, Location loc)
+                                                         Type target_type, bool runtimeconv, Location loc)
                {
                        Type expr_type = expr.Type;
                        Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
@@ -2023,7 +2860,7 @@ namespace Mono.CSharp {
                        if (ne != null)
                                return ne;
 
-                       ne = ConvertNumericExplicit (ec, expr, target_type);
+                       ne = ConvertNumericExplicit (ec, expr, target_type, loc);
                        if (ne != null)
                                return ne;
 
@@ -2054,7 +2891,16 @@ namespace Mono.CSharp {
                                if (t != null)
                                        return t;
                                
-                               return ConvertNumericExplicit (ec, e, target_type);
+                               t = ConvertNumericExplicit (ec, e, target_type, loc);
+                               if (t != null)
+                                       return t;
+                               
+                               t = NarrowingConversion (ec, e, target_type, loc);
+                               if (t != null)
+                                       return t;       
+                                                               
+                               Error_CannotConvertType (loc, expr_type, target_type);
+                               return null;
                        }
                        
                        ne = ConvertReferenceExplicit (expr, target_type);
@@ -2093,7 +2939,7 @@ namespace Mono.CSharp {
                                                if (ci != null)
                                                        return ci;
 
-                                               ce = ConvertNumericExplicit (ec, e, target_type);
+                                               ce = ConvertNumericExplicit (ec, e, target_type, loc);
                                                if (ce != null)
                                                        return ce;
                                                //
@@ -2110,7 +2956,13 @@ namespace Mono.CSharp {
                        if (ne != null)
                                return ne;
 
-                       Error_CannotConvertType (loc, expr_type, target_type);
+                       if (!(runtimeconv))     {
+                               ne = NarrowingConversion (ec, expr, target_type, loc);
+                               if (ne != null)
+                                       return ne;
+                               
+                               Error_CannotConvertType (loc, expr_type, target_type);
+                       }
                        return null;
                }
 
@@ -2125,7 +2977,7 @@ namespace Mono.CSharp {
                        if (ne != null)
                                return ne;
 
-                       ne = ConvertNumericExplicit (ec, expr, target_type);
+                       ne = ConvertNumericExplicit (ec, expr, target_type, l);
                        if (ne != null)
                                return ne;
 
@@ -2133,7 +2985,11 @@ namespace Mono.CSharp {
                        if (ne != null)
                                return ne;
 
-                       Error_CannotConvertType (l, expr.Type, target_type);
+                       ne = NarrowingConversion (ec, expr, target_type, l);
+                       if (ne != null)
+                               return ne;                              
+
+                       Error_CannotConvertType (l, expr.Type, target_type);
                        return null;
                }
 
@@ -2165,7 +3021,7 @@ namespace Mono.CSharp {
                }
                
                /// <summary>
-               ///   Reports that we were expecting `expr' to be of class `expected'
+               ///   Reports that we were expecting 'expr' to be of class 'expected'
                /// </summary>
                public void Error118 (string expected)
                {
@@ -2173,14 +3029,50 @@ namespace Mono.CSharp {
                        
                        kind = ExprClassName (eclass);
 
-                       Error (118, "Expression denotes a `" + kind +
-                              "' where a `" + expected + "' was expected");
+                       Error (118, "Expression denotes a '" + kind +
+                              "' where a '" + expected + "' was expected");
                }
 
+               public void Error118 (ResolveFlags flags)
+               {
+                       ArrayList valid = new ArrayList (10);
+
+                       if ((flags & ResolveFlags.VariableOrValue) != 0) {
+                               valid.Add ("variable");
+                               valid.Add ("value");
+                       }
+
+                       if ((flags & ResolveFlags.Type) != 0)
+                               valid.Add ("type");
+
+                       if ((flags & ResolveFlags.MethodGroup) != 0)
+                               valid.Add ("method group");
+
+                       if ((flags & ResolveFlags.SimpleName) != 0)
+                               valid.Add ("simple name");
+
+                       if (valid.Count == 0)
+                               valid.Add ("unknown");
+
+                       StringBuilder sb = new StringBuilder ();
+                       for (int i = 0; i < valid.Count; i++) {
+                               if (i > 0)
+                                       sb.Append (", ");
+                               else if (i == valid.Count)
+                                       sb.Append (" or ");
+                               sb.Append (valid [i]);
+                       }
+
+                       string kind = ExprClassName (eclass);
+
+                       Error (119, "Expression denotes a '" + kind + "' where " +
+                              "a '" + sb.ToString () + "' was expected");
+               }
+               
                static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
                {
-                       Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
-                                     TypeManager.CSharpName (t));
+                       Report.Error (30439, l, "Constant value '" + val + "' not representable in type " +
+                                     TypeManager.MonoBASIC_Name (t));
                }
 
                public static void UnsafeError (Location loc)
@@ -2190,8 +3082,8 @@ namespace Mono.CSharp {
                
                /// <summary>
                ///   Converts the IntConstant, UIntConstant, LongConstant or
-               ///   ULongConstant into the integral target_type.   Notice
-               ///   that we do not return an `Expression' we do return
+               ///   ULongConstant,Double 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
@@ -2223,9 +3115,6 @@ namespace Mono.CSharp {
                                if (target_type == TypeManager.uint32_type){
                                        if (v >= 0)
                                                return (uint) v;
-                               } else if (target_type == TypeManager.char_type){
-                                       if (v >= Char.MinValue && v <= Char.MaxValue)
-                                               return (char) v;
                                } else if (target_type == TypeManager.byte_type){
                                        if (v >= Byte.MinValue && v <= Byte.MaxValue)
                                                return (byte) v;
@@ -2252,9 +3141,6 @@ namespace Mono.CSharp {
                                if (target_type == TypeManager.int32_type){
                                        if (v <= Int32.MaxValue)
                                                return (int) v;
-                               } else if (target_type == TypeManager.char_type){
-                                       if (v >= Char.MinValue && v <= Char.MaxValue)
-                                               return (char) v;
                                } else if (target_type == TypeManager.byte_type){
                                        if (v <= Byte.MaxValue)
                                                return (byte) v;
@@ -2281,9 +3167,6 @@ namespace Mono.CSharp {
                                } else if (target_type == TypeManager.uint32_type){
                                        if (v >= 0 && v <= UInt32.MaxValue)
                                                return (uint) v;
-                               } else if (target_type == TypeManager.char_type){
-                                       if (v >= Char.MinValue && v <= Char.MaxValue)
-                                               return (char) v;
                                } else if (target_type == TypeManager.byte_type){
                                        if (v >= Byte.MinValue && v <= Byte.MaxValue)
                                                return (byte) v;
@@ -2310,9 +3193,6 @@ namespace Mono.CSharp {
                                } else if (target_type == TypeManager.uint32_type){
                                        if (v <= UInt32.MaxValue)
                                                return (uint) v;
-                               } else if (target_type == TypeManager.char_type){
-                                       if (v >= Char.MinValue && v <= Char.MaxValue)
-                                               return (char) v;
                                } else if (target_type == TypeManager.byte_type){
                                        if (v >= Byte.MinValue && v <= Byte.MaxValue)
                                                return (byte) v;
@@ -2337,8 +3217,6 @@ namespace Mono.CSharp {
                                        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;
@@ -2359,9 +3237,6 @@ namespace Mono.CSharp {
                                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;
@@ -2385,9 +3260,6 @@ namespace Mono.CSharp {
                                } 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;
@@ -2410,10 +3282,7 @@ namespace Mono.CSharp {
                                        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){
+                               else if (target_type == TypeManager.byte_type){
                                        if (v >= Byte.MinValue && v <= Byte.MaxValue)
                                                return (byte) v;
                                } else if (target_type == TypeManager.sbyte_type){
@@ -2452,7 +3321,35 @@ namespace Mono.CSharp {
                                        return (ulong) v;
 
                                s = v.ToString ();
+
+                        } else if (c is DoubleConstant){
+                                double v = ((DoubleConstant) c).Value;
+
+                                if (target_type == TypeManager.sbyte_type){
+                                        if (v >= SByte.MinValue && v <= SByte.MaxValue)
+                                               return new SByteConstant ((sbyte) System.Math.Round (v));
+                                } else if (target_type == TypeManager.byte_type){
+                                        if (v >= Byte.MinValue && v <= Byte.MaxValue)
+                                               return new ByteConstant ((byte) System.Math.Round (v));
+                                } else if (target_type == TypeManager.short_type){
+                                        if (v >= Int16.MinValue && v <= Int16.MaxValue)
+                                               return new ShortConstant ((short) System.Math.Round (v));
+                                } else if (target_type == TypeManager.ushort_type){
+                                        if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
+                                                return new UShortConstant ((ushort) System.Math.Round (v));
+                               } else if (target_type == TypeManager.int32_type){
+                                        if (v >= Int32.MinValue && v <= Int32.MaxValue)
+                                               return new IntConstant ((int) System.Math.Round (v));
+                                } else if (target_type == TypeManager.uint32_type){
+                                        if (v >= 0 && v <= UInt32.MaxValue)
+                                               return new UIntConstant ((uint) System.Math.Round (v));
+                                } else if (target_type == TypeManager.uint64_type){
+                                        if (v > 0)
+                                               return new ULongConstant ((ulong) System.Math.Round (v));
+                                }
+                                s = v.ToString ();
                        }
+
                        Error_ConstantValueCannotBeConverted (loc, s, target_type);
                        return null;
                }
@@ -2500,11 +3397,11 @@ namespace Mono.CSharp {
                }
 
                //
-               // The stack contains the pointer and the value of type `type'
+               // The stack contains the pointer and the value of type 'type'
                //
                public static void StoreFromPtr (ILGenerator ig, Type type)
                {
-                       if (type.IsEnum)
+                       if (TypeManager.IsEnumType (type))
                                type = TypeManager.EnumToUnderlying (type);
                        if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
                                ig.Emit (OpCodes.Stind_I4);
@@ -2529,7 +3426,7 @@ namespace Mono.CSharp {
                }
                
                //
-               // Returns the size of type `t' if known, otherwise, 0
+               // Returns the size of type 't' if known, otherwise, 0
                //
                public static int GetTypeSize (Type t)
                {
@@ -2550,6 +3447,8 @@ namespace Mono.CSharp {
                                 t == TypeManager.char_type ||
                                 t == TypeManager.ushort_type)
                                return 2;
+                       else if (t == TypeManager.decimal_type)
+                               return 16;
                        else
                                return 0;
                }
@@ -2567,7 +3466,7 @@ namespace Mono.CSharp {
                }
                
                //
-               // Converts `source' to an int, uint, long or ulong.
+               // Converts 'source' to an int, uint, long or ulong.
                //
                public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
                {
@@ -2625,7 +3524,7 @@ namespace Mono.CSharp {
        public abstract class ExpressionStatement : Expression {
 
                /// <summary>
-               ///   Requests the expression to be emitted in a `statement'
+               ///   Requests the expression to be emitted in a 'statement'
                ///   context.  This means that no new value is left on the
                ///   stack after invoking this method (constrasted with
                ///   Emit that will always leave a value on the stack).
@@ -3100,6 +3999,103 @@ namespace Mono.CSharp {
                }                       
        }
 
+
+       public class NumericToBoolCast : EmptyCast 
+       {
+               Type src_type;
+               
+               public NumericToBoolCast (Expression src, Type src_type)
+                       : base (src, TypeManager.bool_type)
+                       
+               {
+                       this.src_type = src_type;
+               }
+
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       return this;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       base.Emit (ec);
+
+                       if (src_type == TypeManager.byte_type ||
+                               src_type == TypeManager.short_type ||
+                               src_type == TypeManager.int32_type) {
+                               
+                               ec.ig.Emit (OpCodes.Ldc_I4_0);
+                               ec.ig.Emit (OpCodes.Cgt_Un);
+                               return;
+                       } 
+
+                       if (src_type == TypeManager.int64_type) {
+                               ec.ig.Emit (OpCodes.Ldc_I8, (long) 0);
+                               ec.ig.Emit (OpCodes.Cgt_Un);
+                               return;
+                       } 
+
+                       if (src_type == TypeManager.float_type) {
+                               ec.ig.Emit (OpCodes.Ldc_R4, (float) 0);
+                               ec.ig.Emit (OpCodes.Ceq);
+                               ec.ig.Emit (OpCodes.Ldc_I4_0);
+                               ec.ig.Emit (OpCodes.Ceq);
+                               return;
+                       } 
+
+                       if (src_type == TypeManager.double_type) {
+                               ec.ig.Emit (OpCodes.Ldc_R8, (double) 0);
+                               ec.ig.Emit (OpCodes.Ceq);
+                               ec.ig.Emit (OpCodes.Ldc_I4_0);
+                               ec.ig.Emit (OpCodes.Ceq);
+                               return;
+                       }
+               }                       
+       }
+
+       public class BoolToNumericCast : EmptyCast 
+       {
+               Type target_type;
+               OpCode conv;
+               
+               public BoolToNumericCast (Expression src, Type target_type)
+                       : base (src, target_type)
+                       
+               {
+                       this.target_type = target_type;
+               }
+
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       return this;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       base.Emit (ec);
+
+                       if (target_type == TypeManager.byte_type) {
+                               conv = OpCodes.Conv_U1;
+                       } else if (target_type == TypeManager.short_type) {
+                               conv = OpCodes.Conv_I2;
+                       } else if (target_type == TypeManager.int32_type) {
+                               conv = OpCodes.Conv_I4;
+                       } else if (target_type == TypeManager.int64_type) {
+                               conv = OpCodes.Conv_I8;
+                       } else if (target_type == TypeManager.float_type) {
+                               conv = OpCodes.Conv_R4;
+                       } else if (target_type == TypeManager.double_type) {
+                               conv = OpCodes.Conv_R8;
+                       }
+
+                       ec.ig.Emit (OpCodes.Ldc_I4_0);
+                       ec.ig.Emit (OpCodes.Cgt_Un);
+                       ec.ig.Emit (OpCodes.Neg);
+                       ec.ig.Emit (conv);
+                       return;
+               }                       
+       }
+
        /// <summary>
        ///   This kind of cast is used to encapsulate a child and cast it
        ///   to the class requested
@@ -3141,10 +4137,10 @@ namespace Mono.CSharp {
        ///   creating a namespace map from the assemblies, as that requires
        ///   the GetExportedTypes function to be called and a hashtable to
        ///   be constructed which reduces startup time.  If later we find
-       ///   that this is slower, we should create a `NamespaceExpr' expression
+       ///   that this is slower, we should create a 'NamespaceExpr' expression
        ///   that fully participates in the resolution process. 
        ///   
-       ///   For example `System.Console.WriteLine' is decomposed into
+       ///   For example 'System.Console.WriteLine' is decomposed into
        ///   MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
        ///   
        ///   The first SimpleName wont produce a match on its own, so it will
@@ -3153,11 +4149,25 @@ namespace Mono.CSharp {
        ///   
        ///   System.Console will produce a TypeExpr match.
        ///   
-       ///   The downside of this is that we might be hitting `LookupType' too many
+       ///   The downside of this is that we might be hitting 'LookupType' too many
        ///   times with this scheme.
        /// </remarks>
-       public class SimpleName : Expression {
+       public class SimpleName : Expression, ITypeExpression {
                public readonly string Name;
+               bool is_invocation = false;
+               bool is_addressof = false;
+
+               public bool IsInvocation {
+                       set {
+                               is_invocation = value;
+                       }
+               }
+
+               public bool IsAddressOf {
+                       set {
+                               is_addressof = value;
+                       }
+               }
                
                public SimpleName (string name, Location l)
                {
@@ -3165,43 +4175,31 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               public static void Error_ObjectRefRequired (Location l, string name)
+               public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
                {
-                       Report.Error (
-                               120, l,
-                               "An object reference is required " +
-                               "for the non-static field `"+name+"'");
+                       if (ec.IsFieldInitializer)
+                               Report.Error (
+                                       236, l,
+                                       "A field initializer cannot reference the non-static field, " +
+                                       "method or property '"+name+"'");
+                       else
+                               Report.Error (
+                                       120, l,
+                                       "An object reference is required " +
+                                       "for the non-static field '"+name+"'");
                }
                
                //
                // Checks whether we are trying to access an instance
                // property, method or field from a static body.
                //
-               Expression MemberStaticCheck (Expression e)
+               Expression MemberStaticCheck (EmitContext ec, Expression e)
                {
-                       if (e is FieldExpr){
-                               FieldInfo fi = ((FieldExpr) e).FieldInfo;
+                       if (e is IMemberExpr){
+                               IMemberExpr member = (IMemberExpr) e;
                                
-                               if (!fi.IsStatic){
-                                       Error_ObjectRefRequired (loc, Name);
-                                       return null;
-                               }
-                       } else if (e is MethodGroupExpr){
-                               MethodGroupExpr mg = (MethodGroupExpr) e;
-
-                               if (!mg.RemoveInstanceMethods ()){
-                                       Error_ObjectRefRequired (loc, mg.Methods [0].Name);
-                                       return null;
-                               }
-                               return e;
-                       } else if (e is PropertyExpr){
-                               if (!((PropertyExpr) e).IsStatic){
-                                       Error_ObjectRefRequired (loc, Name);
-                                       return null;
-                               }
-                       } else if (e is EventExpr) {
-                               if (!((EventExpr) e).IsStatic) {
-                                       Error_ObjectRefRequired (loc, Name);
+                               if (!member.IsStatic){
+                                       Error_ObjectRefRequired (ec, loc, Name);
                                        return null;
                                }
                        }
@@ -3225,13 +4223,59 @@ namespace Mono.CSharp {
                        return SimpleNameResolve (ec, null, true);
                }
 
+               public Expression DoResolveType (EmitContext ec)
+               {
+                       //
+                       // Stage 3: Lookup symbol in the various namespaces. 
+                       //
+                       DeclSpace ds = ec.DeclSpace;
+                       Type t;
+                       string alias_value;
+
+                       if (ec.ResolvingTypeTree){
+                               int errors = Report.Errors;
+                               Type dt = ec.DeclSpace.FindType (loc, Name);
+                               if (Report.Errors != errors)
+                                       return null;
+                               
+                               if (dt != null)
+                                       return new TypeExpr (dt, loc);
+                       }
+
+                       if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
+                               return new TypeExpr (t, loc);
+                               
+
+                       //
+                       // 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
+                       //
+                               
+                       alias_value = ec.DeclSpace.LookupAlias (Name);
+                               
+                       if (Name.IndexOf ('.') == -1 && alias_value != null) {
+                               if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
+                                       return new TypeExpr (t, loc);
+                                       
+                               // we have alias value, but it isn't Type, so try if it's namespace
+                               return new SimpleName (alias_value, loc);
+                       }
+                               
+                       // No match, maybe our parent can compose us
+                       // into something meaningful.
+                       return this;
+               }
+
                /// <remarks>
                ///   7.5.2: Simple Names. 
                ///
                ///   Local Variables and Parameters are handled at
                ///   parse time, so they never occur as SimpleNames.
                ///
-               ///   The `allow_static' flag is used by MemberAccess only
+               ///   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
@@ -3245,247 +4289,198 @@ namespace Mono.CSharp {
                Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
                {
                        Expression e = null;
-
+                       
                        //
                        // Stage 1: Performed by the parser (binding to locals or parameters).
                        //
-                       if (!ec.OnlyLookupTypes){
-                               Block current_block = ec.CurrentBlock;
-                               if (current_block != null && current_block.IsVariableDefined (Name)){
-                                       LocalVariableReference var;
+                       Block current_block = ec.CurrentBlock;
+                       if (ec.InvokingOwnOverload == false && current_block != null && current_block.IsVariableDefined (Name)){
+                               LocalVariableReference var;
+
+                               var = new LocalVariableReference (current_block, Name, loc);
+
+                               if (right_side != null)
+                                       return var.ResolveLValue (ec, right_side);
+                               else
+                                       return var.Resolve (ec);
+                       }
+
+                       if (current_block != null){
+                               int idx = -1;
+                               Parameter par = null;
+                               Parameters pars = current_block.Parameters;
+                               if (pars != null)
+                                       par = pars.GetParameterByName (Name, out idx);
+
+                               if (par != null) {
+                                       ParameterReference param;
                                        
-                                       var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
+                                       param = new ParameterReference (pars, idx, Name, loc);
 
                                        if (right_side != null)
-                                               return var.ResolveLValue (ec, right_side);
+                                               return param.ResolveLValue (ec, right_side);
                                        else
-                                               return var.Resolve (ec);
+                                               return param.Resolve (ec);
                                }
-                       
-                               //
-                               // Stage 2: Lookup members 
-                               //
-
-                               //
-                               // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
-                               // Hence we have two different cases
-                               //
+                       }
 
-                               DeclSpace lookup_ds = ec.DeclSpace;
-                               do {
-                                       if (lookup_ds.TypeBuilder == null)
-                                               break;
+                       //
+                       // Stage 2: Lookup members 
+                       //
 
-                                       e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
-                                       if (e != null)
-                                               break;
+                       //
+                       // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
+                       // Hence we have two different cases
+                       //
 
-                                       //
-                                       // Classes/structs keep looking, enums break
-                                       //
-                                       if (lookup_ds is TypeContainer)
-                                               lookup_ds = ((TypeContainer) lookup_ds).Parent;
-                                       else
-                                               break;
-                               } while (lookup_ds != null);
-                               
-                               if (e == null && ec.ContainerType != null)
-                                       e = MemberLookup (ec, ec.ContainerType, Name, loc);
-                       }
+                       DeclSpace lookup_ds = ec.DeclSpace;
+                       do {
+                               if (lookup_ds.TypeBuilder == null)
+                                       break;
 
-                       // Continuation of stage 2
-                       if (e == null){
-                               //
-                               // Stage 3: Lookup symbol in the various namespaces. 
-                               //
-                               DeclSpace ds = ec.DeclSpace;
-                               Type t;
-                               string alias_value;
+                               e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
+                               if (e != null)
+                                       break;
 
-                               if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
-                                       return new TypeExpr (t, loc);
-                               
                                //
-                               // Stage 2 part b: Lookup up if we are an alias to a type
-                               // or a namespace.
+                               // Classes/structs keep looking, enums break
                                //
-                               // Since we are cheating: we only do the Alias lookup for
-                               // namespaces if the name does not include any dots in it
-                               //
-                               
-                               alias_value = ec.DeclSpace.LookupAlias (Name);
-                               
-                               if (Name.IndexOf ('.') == -1 && alias_value != null) {
-                                       if ((t = RootContext.LookupType (ds, alias_value, true, loc))
-                                           != null)
-                                               return new TypeExpr (t, loc);
-                                       
-                               // we have alias value, but it isn't Type, so try if it's namespace
-                                       return new SimpleName (alias_value, loc);
-                               }
-                               
-                               if (ec.ResolvingTypeTree){
-                                       Type dt = ec.DeclSpace.FindType (Name);
-                                       if (dt != null)
-                                               return new TypeExpr (dt, loc);
-                               }
+                               if (lookup_ds is TypeContainer)
+                                       lookup_ds = ((TypeContainer) lookup_ds).Parent;
+                               else
+                                       break;
+                       } while (lookup_ds != null);
                                
-                               // No match, maybe our parent can compose us
-                               // into something meaningful.
-                               return this;
-                       }
-                       
-                       //
-                       // Stage 2 continues here. 
-                       // 
-                       if (e is TypeExpr)
-                               return e;
+                       if (e == null && ec.ContainerType != null)
+                               e = MemberLookup (ec, ec.ContainerType, Name, loc);
 
-                       if (ec.OnlyLookupTypes)
-                               return null;
-                       
-                       if (e is FieldExpr){
-                               FieldExpr fe = (FieldExpr) e;
-                               FieldInfo fi = fe.FieldInfo;
+// #52067 - Start - Trying to solve
 
-                               if (fi.FieldType.IsPointer && !ec.InUnsafe){
-                                       UnsafeError (loc);
-                               }
+                       if (e == null) {
+                               ArrayList lookups = new ArrayList();
+                               ArrayList typelookups = new ArrayList();
                                
-                               if (ec.IsStatic){
-                                       if (!allow_static && !fi.IsStatic){
-                                               Error_ObjectRefRequired (loc, Name);
-                                               return null;
+                               int split = Name.LastIndexOf('.');
+                               if (split != -1) {
+                                       String nameSpacePart = Name.Substring(0, split);
+                                       String memberNamePart = Name.Substring(split + 1);
+                                       foreach(Type type in TypeManager.GetPertinentStandardModules(nameSpacePart)) {
+                                               e = MemberLookup(ec, type, memberNamePart, loc);
+                                               if (e != null) {
+                                                       lookups.Add(e);
+                                                       typelookups.Add(type);
+                                               }
                                        }
-                               } 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);
-                                               if (o == null)
-                                                       return null;
-                                               object real_value = ((Constant)c.Expr).GetValue ();
-                                               return Constantify (real_value, fi.FieldType);
+                               string[] NamespacesInScope = RootContext.SourceBeingCompiled.GetNamespacesInScope(ec.DeclSpace.Namespace.Name);
+                               foreach(Type type in TypeManager.GetPertinentStandardModules(NamespacesInScope)) {
+                                       e = MemberLookup(ec, type, Name, loc);
+                                       if (e != null) {
+                                               lookups.Add(e);
+                                               typelookups.Add(type);
                                        }
                                }
+                               if (lookups.Count == 1) { 
+                                       e = (Expression)lookups[0];
+                               } else {
+                                       if (lookups.Count > 1) {
+                                               StringBuilder sb = new StringBuilder();
+                                               foreach(Type type in typelookups)
+                                                       sb.Append("'" + type.FullName + "'");                                           
+                                               Error (-1, "The name '" + Name + "' can be resolved to a member of more than one standard module: " + sb.ToString() + ". Please fully qualify it.");
+                                               return null;
+                                       }
+                               }
+                       }
 
-                               if (fi.IsLiteral) {
-                                       Type t = fi.FieldType;
-                                       Type decl_type = fi.DeclaringType;
-                                       object o;
-
-                                       if (fi is FieldBuilder)
-                                               o = TypeManager.GetValue ((FieldBuilder) fi);
-                                       else
-                                               o = fi.GetValue (fi);
-                                       
-                                       if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
-                                               Expression enum_member = MemberLookup (
-                                                       ec, decl_type, "value__", MemberTypes.Field,
-                                                       AllBindingFlags, loc); 
-
-                                               Enum en = TypeManager.LookupEnum (decl_type);
+// #52067 - End
 
-                                               Constant c;
-                                               if (en != null)
-                                                       c = Constantify (o, en.UnderlyingType);
-                                               else 
-                                                       c = Constantify (o, enum_member.Type);
+                       if (e == null) {
+                       
+               /* preparing to support automatic definition of variables on first usage with Option Explicit Off
+                       
+                       Isn't good enough to enable just now (tries to define some internal links and breaks on emit)
+                       
+                               if (Name.IndexOf ('.') == -1 && current_block != null && !Mono.MonoBASIC.Parser.OptionExplicit) {
+                               
+                                       // while looking for a real solution
+                                       if (Name != "anything")
+                                               return DoResolveType (ec);
                                                
-                                               return new EnumConstant (c, decl_type);
-                                       }
+                                       Console.WriteLine("Implicitly adding a variable named '{0}'", Name);
                                        
-                                       Expression exp = Constantify (o, t);
-                               }
+                                       // TODO: look at type-suffixes to correct name and type
+                                       Expression type = Mono.MonoBASIC.Parser.DecomposeQI("System.Object", loc);
                                        
-                               return e;
+                                       current_block.AddVariable(ec, type, Name, loc);
+                               
+                                       LocalVariableReference var = new LocalVariableReference (current_block, Name, loc);
+                                       if (right_side != null)
+                                               return var.ResolveLValue (ec, right_side);
+                                       else
+                                               return var.Resolve (ec);
+                               } else
+               */
+                                       return DoResolveType (ec);
                        }
-
-                       if (e is PropertyExpr) {
-                               PropertyExpr pe = (PropertyExpr) e;
-
-                               if (ec.IsStatic){
-                                       if (allow_static)
-                                               return e;
-
-                                       return MemberStaticCheck (e);
-                               } else {
-                                       // If we are not in static code and this
-                                       // field is not static, set the instance to `this'.
-
-                                       if (!pe.IsStatic)
-                                               pe.InstanceExpression = ec.This;
-                               }
-
+                       
+                       if (e is TypeExpr)
                                return e;
-                       }
-
-                       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.ContainerType, ee.EventInfo.Name,
-                                       MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
+                       if (e is IMemberExpr) {
+                               if ((e is MethodGroupExpr) && !is_invocation && !is_addressof) {
+                                       Expression inv = new Invocation (this, new ArrayList (), loc);
+                                       return inv.Resolve (ec);
+                               }
+                               e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
+                               if (e == null)
+                                       return null;
 
-                               if (ml != null) {
-                                       MemberInfo mi = GetFieldFromEvent ((EventExpr) ml);
+                               if (e is PropertyGroupExpr && is_invocation) // We dont know the arguments yet
+                                       return e;
 
-                                       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, loc);
-                                               return null;
-                                       }
+                               IMemberExpr me = e as IMemberExpr;
+                               if (me == null)
+                                       return e;
 
-                                       ml = ExprClassFromMemberInfo (ec, mi, loc);
-                                       
-                                       if (ml == null) {
-                                               Report.Error (-200, loc, "Internal error!!");
-                                               return null;
-                                       }
+                               // This fails if ResolveMemberAccess() was unable to decide whether
+                               // it's a field or a type of the same name.
+                               if (!me.IsStatic && (me.InstanceExpression == null))
+                                       return e;
 
-                                       Expression instance_expr;
-                                       
-                                       FieldInfo fi = ((FieldExpr) ml).FieldInfo;
+/* FIXME    If this is not commented out, it seems that it's not possible to reach class members in mBas.
+            Maybe a grammar-related problem?
 
-                                       if (fi.IsStatic)
-                                               instance_expr = null;
-                                       else {
-                                               instance_expr = ec.This;
-                                               instance_expr = instance_expr.Resolve (ec);
-                                       } 
-                                       
-                                       return MemberAccess.ResolveMemberAccess (ec, ml, instance_expr, loc, null);
+                               if (!me.IsStatic &&
+                                   TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType)) {
+                                       Error (38, "Cannot access nonstatic member '" + me.Name + "' of " +
+                                              "outer type '" + me.DeclaringType + "' via nested type '" +
+                                              me.InstanceExpression.Type + "'");
+                                       return null;
                                }
+*/
+                               bool isPropertyGroup = (e is PropertyGroupExpr);
+                               if (right_side != null)
+                                       e = e.DoResolveLValue (ec, right_side);
+                               else
+                                       e = e.DoResolve (ec);
+
+                               if (e == null && isPropertyGroup && !is_invocation)
+                                        Error (30057, "Property '" + Name + "' cannot be invoked with given arguments");
+                                        
+                               return e;
                        }
-                               
-                       
-                       if (ec.IsStatic){
+
+                       if (ec.IsStatic || ec.IsFieldInitializer) {
                                if (allow_static)
                                        return e;
 
-                               return MemberStaticCheck (e);
-                       } else
-                               return e;
-               }
+                               return MemberStaticCheck (ec, e);
+                       }
 
-               
+                       return e;
+               }
                
                public override void Emit (EmitContext ec)
                {
@@ -3494,8 +4489,8 @@ namespace Mono.CSharp {
                        // find the name as a namespace
                        //
 
-                       Error (103, "The name `" + Name +
-                              "' does not exist in the class `" +
+                       Error (30451, "The name '" + Name +
+                              "' does not exist in the class '" +
                               ec.DeclSpace.Name + "'");
                }
 
@@ -3504,11 +4499,41 @@ namespace Mono.CSharp {
                        return Name;
                }
        }
+
+       public class DecoratedIdentifier : Expression {
+               Expression id;
+               Type decoration;
+
+               public DecoratedIdentifier (Expression id, Type decoration)
+               {
+                       this.id = id;
+                       this.decoration = decoration;
+               }
+
+               override public Expression DoResolve (EmitContext ec)
+               {
+                       if (id == null || decoration == null)
+                               return null;
+                       
+                       Expression ret = id.DoResolve (ec);
+                       if (ret.Type != TypeManager.TypeToCoreType (decoration)) {
+                               Report.Error (30277, id.Location, "Type character '" + decoration + "' does not match declared type '" + ret.Type  + "'."); 
+                               return null;
+                       }
+
+                       return ret;
+               }
+               
+               override public void Emit (EmitContext ec)
+               {
+                       throw new InternalErrorException ("Should never be called");
+               }
+       }
        
        /// <summary>
        ///   Fully resolved expression that evaluates to a type
        /// </summary>
-       public class TypeExpr : Expression {
+       public class TypeExpr : Expression, ITypeExpression {
                public TypeExpr (Type t, Location l)
                {
                        Type = t;
@@ -3516,6 +4541,11 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
+               public virtual Expression DoResolveType (EmitContext ec)
+               {
+                       return this;
+               }
+
                override public Expression DoResolve (EmitContext ec)
                {
                        return this;
@@ -3525,31 +4555,41 @@ namespace Mono.CSharp {
                {
                        throw new Exception ("Should never be called");
                }
+
+               public override string ToString ()
+               {
+                       return Type.ToString ();
+               }
        }
 
        /// <summary>
        ///   Used to create types from a fully qualified name.  These are just used
-       ///   by the parser to setup the core types.  A TypeExpression is always
+       ///   by the parser to setup the core types.  A TypeLookupExpression is always
        ///   classified as a type.
        /// </summary>
-       public class TypeExpression : TypeExpr {
+       public class TypeLookupExpression : TypeExpr {
                string name;
                
-               public TypeExpression (string name) : base (null, Location.Null)
+               public TypeLookupExpression (string name) : base (null, Location.Null)
                {
                        this.name = name;
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               public override Expression DoResolveType (EmitContext ec)
                {
                        if (type == null)
                                type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
                        return this;
                }
 
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       return DoResolveType (ec);
+               }
+
                public override void Emit (EmitContext ec)
                {
-                       throw new Exception ("Should never be called");                 
+                       throw new Exception ("Should never be called");
                }
 
                public override string ToString ()
@@ -3563,9 +4603,10 @@ namespace Mono.CSharp {
        ///  
        ///   This is a fully resolved expression that evaluates to a type
        /// </summary>
-       public class MethodGroupExpr : Expression {
+       public class MethodGroupExpr : Expression, IMemberExpr {
                public MethodBase [] Methods;
                Expression instance_expression = null;
+               bool is_explicit_impl = false;
                
                public MethodGroupExpr (MemberInfo [] mi, Location l)
                {
@@ -3595,9 +4636,15 @@ namespace Mono.CSharp {
                        eclass = ExprClass.MethodGroup;
                        type = TypeManager.object_type;
                }
+
+               public Type DeclaringType {
+                       get {
+                               return Methods [0].DeclaringType;
+                       }
+               }
                
                //
-               // `A method group may have associated an instance expression' 
+               // 'A method group may have associated an instance expression' 
                // 
                public Expression InstanceExpression {
                        get {
@@ -3608,15 +4655,57 @@ namespace Mono.CSharp {
                                instance_expression = value;
                        }
                }
+
+               public bool IsExplicitImpl {
+                       get {
+                               return is_explicit_impl;
+                       }
+
+                       set {
+                               is_explicit_impl = value;
+                       }
+               }
+
+               public string Name {
+                       get {
+                               return Methods [0].Name;
+                       }
+               }
+
+               public bool IsInstance {
+                       get {
+                               foreach (MethodBase mb in Methods)
+                                       if (!mb.IsStatic)
+                                               return true;
+
+                               return false;
+                       }
+               }
+
+               public bool IsStatic {
+                       get {
+                               foreach (MethodBase mb in Methods)
+                                       if (mb.IsStatic)
+                                               return true;
+
+                               return false;
+                       }
+               }
                
                override public Expression DoResolve (EmitContext ec)
                {
+                       if (instance_expression != null) {
+                               instance_expression = instance_expression.DoResolve (ec);
+                               if (instance_expression == null)
+                                       return null;
+                       }
+
                        return this;
                }
 
                public void ReportUsageError ()
                {
-                       Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
+                       Report.Error (654, loc, "Method '" + Methods [0].DeclaringType + "." +
                                      Methods [0].Name + "()' is referenced without parentheses");
                }
 
@@ -3662,12 +4751,270 @@ namespace Mono.CSharp {
                }
        }
 
+       /// <summary>
+       ///   Property Group Expression.
+       ///  
+       /// </summary>
+       public class PropertyGroupExpr : ExpressionStatement, IMemberExpr {
+               public PropertyInfo [] Properties;
+               Expression instance_expression = null;
+               bool is_explicit_impl = false;
+               MethodBase method = null;
+               bool indexer_access_req = false;
+               ArrayList arguments = null;
+               
+               public PropertyGroupExpr (MemberInfo [] mi, Location l)
+               {
+                       Properties = new PropertyInfo [mi.Length];
+                       mi.CopyTo (Properties, 0);
+                       eclass = ExprClass.PropertyAccess;
+                       type = TypeManager.object_type;
+                       loc = l;
+               }
+
+               public PropertyGroupExpr (MemberInfo [] mi, ArrayList args, Expression expr, Location l)
+                       : this (mi, l)
+               {
+                       arguments = args;
+                       instance_expression = expr;
+               }
+
+               public PropertyGroupExpr (ArrayList list, Location l)
+               {
+                       Properties = new PropertyInfo [list.Count];
+
+                       try {
+                               list.CopyTo (Properties, 0);
+                       } catch {
+                               foreach (MemberInfo m in list){
+                                       if (!(m is PropertyInfo)){
+                                               Console.WriteLine ("Name " + m.Name);
+                                               Console.WriteLine ("Found a: " + m.GetType ().FullName);
+                                       }
+                               }
+                               throw;
+                       }
+                       loc = l;
+                       eclass = ExprClass.PropertyAccess;
+                       type = TypeManager.object_type;
+               }
+
+               public ArrayList Arguments {
+                       get {
+                               return arguments;
+                       }
+                       set {
+                               arguments = value;
+                       }
+               }
+
+               public Type DeclaringType {
+                       get {
+                               return Properties [0].DeclaringType;
+                       }
+               }
+
+               public bool IndexerAccessRequired {
+                       get {
+                               return indexer_access_req;
+                       }
+               }
+
+               //
+               // 'A method group may have associated an instance expression' 
+               // 
+               public Expression InstanceExpression {
+                       get {
+                               return instance_expression;
+                       }
+
+                       set {
+                               instance_expression = value;
+                       }
+               }
+
+               public bool IsExplicitImpl {
+                       get {
+                               return is_explicit_impl;
+                       }
+
+                       set {
+                               is_explicit_impl = value;
+                       }
+               }
+
+               public string Name {
+                       get {
+                               return Properties [0].Name;
+                       }
+               }
+
+               public bool IsInstance {
+                       get {
+                               foreach (PropertyInfo pi in Properties) {
+                                       MethodInfo mi = pi.GetGetMethod ();
+                                       if (mi != null && !mi.IsStatic)
+                                               return true;
+                                       mi = pi.GetSetMethod ();
+                                       if (mi != null && !mi.IsStatic)
+                                               return true;
+                               }               
+                               return false;
+                       }
+               }
+
+               public bool IsStatic {
+                       get {
+                               return (!IsInstance);
+                       }
+               }
+               
+               public ArrayList GetAccessors () {
+                       ArrayList GetAccessors = new ArrayList ();
+                       foreach (PropertyInfo pi in Properties) {
+                               if (pi.GetGetMethod () != null)
+                                       GetAccessors.Add (pi.GetGetMethod ());
+/*
+                               else if (pi.GetGetMethod (true) != null)
+                                       GetAccessors.Add (pi.GetGetMethod (true));
+*/
+                                       
+                       }
+                       return GetAccessors;
+               }
+               
+               public ArrayList SetAccessors () {
+                       ArrayList SetAccessors = new ArrayList ();
+                       foreach (PropertyInfo pi in Properties) {
+                               if (pi.GetSetMethod () != null)
+                                       SetAccessors.Add (pi.GetSetMethod ());
+/*
+                               else if (pi.GetSetMethod (true) != null)
+                                       SetAccessors.Add (pi.GetSetMethod (true));
+*/
+                       }
+                       return SetAccessors;
+               }
+
+               override public Expression DoResolve (EmitContext ec)
+               {
+                       if (instance_expression != null) {
+                               instance_expression = instance_expression.DoResolve (ec);
+                               if (instance_expression == null)
+                                       return null;
+                       }
+
+                       ArrayList members = GetAccessors ();
+                       if (members == null || members.Count == 0) {
+                               Report.Error (30524, loc, "Property '" + Name + "' lacks a 'get' accesor");
+                               return null;
+                       }
+
+                       MethodGroupExpr m_expr = new MethodGroupExpr (members, loc);
+                       method = Invocation.OverloadResolve (ec, m_expr, ref arguments, loc);
+                       if ((method as MethodInfo) != null) {
+                               MethodInfo mi = method as MethodInfo;
+                               type = TypeManager.TypeToCoreType (mi.ReturnType);
+                               indexer_access_req = false;
+                               eclass = ExprClass.Value;
+                               return this;
+                       } else {
+                               // find a get method that doesnt take any arguments. Check the return type of
+                               // that method to find out if indexer access is required. Leave the rest to
+                               // 'Invocation's Resolve'
+                               method = Invocation.OverloadResolve (ec, m_expr, null, loc);
+                               if (method != null) {
+                                       MethodInfo mi = method as MethodInfo;
+                                       Type ret_type = mi.ReturnType;
+                                       if (ret_type.IsArray)
+                                               indexer_access_req = true;
+                                       else {
+                                               Indexers list = Indexers.GetIndexersForType (ec.ContainerType, ret_type, loc);
+                                               if (list != null && list.getters.Count > 0)
+                                                       indexer_access_req = true;
+                                               else 
+                                                       return null;
+                                       }
+                                       Arguments = null;
+                                       type = mi.ReturnType;
+                                       return this;
+                               }
+                       }
+
+                       return null;
+               }
+
+               override public Expression DoResolveLValue (EmitContext ec, Expression right_side) {
+                       if (instance_expression != null) {
+                               instance_expression = instance_expression.DoResolve (ec);
+                               if (instance_expression == null)
+                                       return null;
+                       }
+
+                       ArrayList members = SetAccessors ();
+                       if (members == null || members.Count == 0) {
+                               Report.Error (30524, loc, "Property '" + Name + "' lacks a 'set' accesor");
+                               return null;
+                       }
+
+                       MethodGroupExpr m_expr = new MethodGroupExpr (members, loc);
+                       if (arguments == null)
+                               arguments = new ArrayList ();
+                       arguments.Add (new Argument (right_side, Argument.AType.Expression));
+                       method = Invocation.OverloadResolve (ec, m_expr, ref arguments, loc);
+                       if (method != null) {
+                               //MethodInfo mi = method as MethodInfo;
+                               type = TypeManager.void_type; //TypeManager.TypeToCoreType (mi.ReturnType);
+                               eclass = ExprClass.Value;
+                               indexer_access_req = false;
+                               return this;
+                       } else {
+                               // Look for properties that do not take any arguments.
+                               // Check if the return type has any indexers
+                               arguments = null;
+                               DoResolve (ec);
+                               if (method != null) {
+                                       MethodInfo mi = method as MethodInfo;
+                                       Type ret_type = mi.ReturnType;
+                                       if (ret_type.IsArray)
+                                               indexer_access_req = true;
+                                       else {
+
+                                               Indexers list = Indexers.GetIndexersForType (ec.ContainerType, 
+                                                                       ret_type, loc);
+                                               if (list != null && list.setters.Count > 0)
+                                                       indexer_access_req = true;
+                                               else 
+                                                       return null;
+                                       }
+                                       type = mi.ReturnType;
+                                       return this;
+                               }
+                       }
+
+                       return null;
+               }
+
+               override public void Emit (EmitContext ec)
+               {
+                       if (Arguments == null)
+                               Arguments = new ArrayList ();
+                       Invocation.EmitCall (ec, false, IsStatic, instance_expression, method, null, Arguments, loc);
+               }
+
+               override public void EmitStatement (EmitContext ec) 
+               {
+                       Emit (ec);
+               }
+
+       }
+
        /// <summary>
        ///   Fully resolved expression that evaluates to a Field
        /// </summary>
-       public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
+       public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
                public readonly FieldInfo FieldInfo;
-               public Expression InstanceExpression;
+               Expression instance_expr;
                
                public FieldExpr (FieldInfo fi, Location l)
                {
@@ -3677,20 +5024,63 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
+               public string Name {
+                       get {
+                               return FieldInfo.Name;
+                       }
+               }
+
+               public bool IsInstance {
+                       get {
+                               return !FieldInfo.IsStatic;
+                       }
+               }
+
+               public bool IsStatic {
+                       get {
+                               return FieldInfo.IsStatic;
+                       }
+               }
+
+               public Type DeclaringType {
+                       get {
+                               return FieldInfo.DeclaringType;
+                       }
+               }
+
+               public Expression InstanceExpression {
+                       get {
+                               return instance_expr;
+                       }
+
+                       set {
+                               instance_expr = value;
+                       }
+               }
+
                override public Expression DoResolve (EmitContext ec)
                {
                        if (!FieldInfo.IsStatic){
-                               if (InstanceExpression == null){
+                               if (instance_expr == null){
                                        throw new Exception ("non-static FieldExpr without instance var\n" +
                                                             "You have to assign the Instance variable\n" +
                                                             "Of the FieldExpr to set this\n");
                                }
 
-                               InstanceExpression = InstanceExpression.Resolve (ec);
-                               if (InstanceExpression == null)
+                               // Resolve the field's instance expression while flow analysis is turned
+                               // off: when accessing a field "a.b", we must check whether the field
+                               // "a.b" is initialized, not whether the whole struct "a" is initialized.
+                               instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
+                                                                      ResolveFlags.DisableFlowAnalysis);
+                               if (instance_expr == null)
                                        return null;
                        }
 
+                       // If the instance expression is a local variable or parameter.
+                       IVariable var = instance_expr as IVariable;
+                       if ((var != null) && !var.IsFieldAssigned (ec, FieldInfo.Name, loc))
+                               return null;
+
                        return this;
                }
 
@@ -3710,6 +5100,10 @@ namespace Mono.CSharp {
                
                override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
                {
+                       IVariable var = instance_expr as IVariable;
+                       if (var != null)
+                               var.SetFieldAssigned (ec, FieldInfo.Name);
+
                        Expression e = DoResolve (ec);
 
                        if (e == null)
@@ -3734,7 +5128,7 @@ namespace Mono.CSharp {
                {
                        ILGenerator ig = ec.ig;
                        bool is_volatile = false;
-                               
+
                        if (FieldInfo is FieldBuilder){
                                FieldBase f = TypeManager.GetField (FieldInfo);
 
@@ -3750,23 +5144,23 @@ namespace Mono.CSharp {
                                
                                ig.Emit (OpCodes.Ldsfld, FieldInfo);
                        } else {
-                               if (InstanceExpression.Type.IsValueType){
+                               if (instance_expr.Type.IsValueType){
                                        IMemoryLocation ml;
                                        LocalTemporary tempo = null;
                                        
-                                       if (!(InstanceExpression is IMemoryLocation)){
+                                       if (!(instance_expr is IMemoryLocation)){
                                                tempo = new LocalTemporary (
-                                                       ec, InstanceExpression.Type);
+                                                       ec, instance_expr.Type);
 
                                                InstanceExpression.Emit (ec);
                                                tempo.Store (ec);
                                                ml = tempo;
                                        } else
-                                               ml = (IMemoryLocation) InstanceExpression;
+                                               ml = (IMemoryLocation) instance_expr;
 
                                        ml.AddressOf (ec, AddressOp.Load);
                                } else 
-                                       InstanceExpression.Emit (ec);
+                                       instance_expr.Emit (ec);
 
                                if (is_volatile)
                                        ig.Emit (OpCodes.Volatile);
@@ -3788,7 +5182,7 @@ namespace Mono.CSharp {
                        }
                        
                        if (!is_static){
-                               Expression instance = InstanceExpression;
+                               Expression instance = instance_expr;
 
                                if (instance.Type.IsValueType){
                                        if (instance is IMemoryLocation){
@@ -3847,27 +5241,23 @@ namespace Mono.CSharp {
                        // Handle initonly fields specially: make a copy and then
                        // get the address of the copy.
                        //
-                       if (FieldInfo.IsInitOnly){
-                               if (ec.IsConstructor) {
-                                       ig.Emit (OpCodes.Ldsflda, FieldInfo);
-                               } else {
-                                       LocalBuilder local;
+                       if (FieldInfo.IsInitOnly && !ec.IsConstructor){
+                               LocalBuilder local;
                                
-                                       Emit (ec);
-                                       local = ig.DeclareLocal (type);
-                                       ig.Emit (OpCodes.Stloc, local);
-                                       ig.Emit (OpCodes.Ldloca, local);
-                               }
+                               Emit (ec);
+                               local = ig.DeclareLocal (type);
+                               ig.Emit (OpCodes.Stloc, local);
+                               ig.Emit (OpCodes.Ldloca, local);
                                return;
                        }
 
                        if (FieldInfo.IsStatic)
                                ig.Emit (OpCodes.Ldsflda, FieldInfo);
                        else {
-                               if (InstanceExpression is IMemoryLocation)
-                                       ((IMemoryLocation)InstanceExpression).AddressOf (ec, AddressOp.LoadStore);
+                               if (instance_expr is IMemoryLocation)
+                                       ((IMemoryLocation)instance_expr).AddressOf (ec, AddressOp.LoadStore);
                                else
-                                       InstanceExpression.Emit (ec);
+                                       instance_expr.Emit (ec);
                                ig.Emit (OpCodes.Ldflda, FieldInfo);
                        }
                }
@@ -3875,37 +5265,55 @@ namespace Mono.CSharp {
        
        /// <summary>
        ///   Expression that evaluates to a Property.  The Assign class
-       ///   might set the `Value' expression if we are in an assignment.
+       ///   might set the 'Value' expression if we are in an assignment.
        ///
        ///   This is not an LValue because we need to re-write the expression, we
        ///   can not take data from the stack and store it.  
        /// </summary>
-       public class PropertyExpr : ExpressionStatement, IAssignMethod {
+       public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
                public readonly PropertyInfo PropertyInfo;
-               public readonly bool IsStatic;
                public bool IsBase;
-               MethodInfo [] Accessors;
-               
+               MethodInfo getter, setter;
+               bool is_static;
+               public ArrayList PropertyArgs;
+
                Expression instance_expr;
-               
-               public PropertyExpr (PropertyInfo pi, Location l)
+
+               public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
                {
                        PropertyInfo = pi;
                        eclass = ExprClass.PropertyAccess;
-                       IsStatic = false;
+                       PropertyArgs = null;
+                       is_static = false;
                        loc = l;
-                       Accessors = TypeManager.GetAccessors (pi);
 
-                       if (Accessors != null)
-                               foreach (MethodInfo mi in Accessors){
-                                       if (mi != null)
-                                               if (mi.IsStatic)
-                                                       IsStatic = true;
-                               }
-                       else
-                               Accessors = new MethodInfo [2];
-                       
                        type = TypeManager.TypeToCoreType (pi.PropertyType);
+
+                       ResolveAccessors (ec);
+               }
+
+               public string Name {
+                       get {
+                               return PropertyInfo.Name;
+                       }
+               }
+
+               public bool IsInstance {
+                       get {
+                               return !is_static;
+                       }
+               }
+
+               public bool IsStatic {
+                       get {
+                               return is_static;
+                       }
+               }
+               
+               public Type DeclaringType {
+                       get {
+                               return PropertyInfo.DeclaringType;
+                       }
                }
 
                //
@@ -3925,7 +5333,7 @@ namespace Mono.CSharp {
                {
                        if (!PropertyInfo.CanWrite){
                                Report.Error (200, loc, 
-                                             "The property `" + PropertyInfo.Name +
+                                             "The property '" + PropertyInfo.Name +
                                              "' can not be assigned to, as it has not set accessor");
                                return false;
                        }
@@ -3933,29 +5341,86 @@ namespace Mono.CSharp {
                        return true;
                }
 
+               void ResolveAccessors (EmitContext ec)
+               {
+                       BindingFlags flags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
+                       MemberInfo [] group;
+                       
+                       group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
+                                                             MemberTypes.Method, flags, "get_" + PropertyInfo.Name);
+
+                       //
+                       // The first method is the closest to us
+                       //
+                       if (group != null && group.Length > 0){
+                               getter = (MethodInfo) group [0];
+
+                               if (getter.IsStatic)
+                                       is_static = true;
+                       }                       
+
+                       //
+                       // The first method is the closest to us
+                       //
+                       group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
+                                                         MemberTypes.Method, flags, "set_" + PropertyInfo.Name);
+                       if (group != null && group.Length > 0){
+                               setter = (MethodInfo) group [0];
+                               if (setter.IsStatic)
+                                       is_static = true;
+                       }
+               }
+
                override public Expression DoResolve (EmitContext ec)
                {
-                       if (!PropertyInfo.CanRead){
-                               Report.Error (154, loc, 
-                                             "The property `" + PropertyInfo.Name +
+                       if (getter == null){
+                               Report.Error (30524, loc, 
+                                             "The property '" + PropertyInfo.Name +
                                              "' can not be used in " +
                                              "this context because it lacks a get accessor");
                                return null;
                        }
 
-                       type = PropertyInfo.PropertyType;
+                       if ((instance_expr == null) && ec.IsStatic && !is_static) {
+                               SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
+                               return null;
+                       }
+
+                       if (instance_expr != null) {
+                               instance_expr = instance_expr.DoResolve (ec);
+                               if (instance_expr == null)
+                                       return null;
+                       }
 
                        return this;
                }
 
-               override public void Emit (EmitContext ec)
+               override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
                {
-                       MethodInfo method = Accessors [0];
+                       if (setter == null){
+                               Report.Error (30526, loc, 
+                                             "The property '" + PropertyInfo.Name +
+                                             "' can not be used in " +
+                                             "this context because it lacks a set accessor");
+                               return null;
+                       }
 
+                       if (instance_expr != null) {
+                               instance_expr = instance_expr.DoResolve (ec);
+                               if (instance_expr == null)
+                                       return null;
+                       }
+
+                       return this;
+               }
+
+               override public void Emit (EmitContext ec)
+               {
                        //
-                       // Special case: length of single dimension array is turned into ldlen
+                       // Special case: length of single dimension array property is turned into ldlen
                        //
-                       if (method == TypeManager.int_array_get_length){
+                       if ((getter == TypeManager.system_int_array_get_length) ||
+                           (getter == TypeManager.int_array_get_length)){
                                Type iet = instance_expr.Type;
 
                                //
@@ -3968,9 +5433,9 @@ namespace Mono.CSharp {
                                        return;
                                }
                        }
-
-                       Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, method, null, loc);
-                       
+                       if (PropertyArgs == null)
+                               PropertyArgs = new ArrayList ();
+                       Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, PropertyArgs, loc);
                }
 
                //
@@ -3980,9 +5445,9 @@ namespace Mono.CSharp {
                {
                        Argument arg = new Argument (source, Argument.AType.Expression);
                        ArrayList args = new ArrayList ();
-
+//HERE
                        args.Add (arg);
-                       Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args, loc);
+                       Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, PropertyArgs,loc);
                }
 
                override public void EmitStatement (EmitContext ec)
@@ -3995,12 +5460,11 @@ namespace Mono.CSharp {
        /// <summary>
        ///   Fully resolved expression that evaluates to an Event
        /// </summary>
-       public class EventExpr : Expression {
+       public class EventExpr : Expression, IMemberExpr {
                public readonly EventInfo EventInfo;
-               public Expression InstanceExpression;
-
-               public readonly bool IsStatic;
+               public Expression instance_expr;
 
+               bool is_static;
                MethodInfo add_accessor, remove_accessor;
                
                public EventExpr (EventInfo ei, Location loc)
@@ -4013,7 +5477,7 @@ namespace Mono.CSharp {
                        remove_accessor = TypeManager.GetRemoveMethod (ei);
                        
                        if (add_accessor.IsStatic || remove_accessor.IsStatic)
-                                       IsStatic = true;
+                               is_static = true;
 
                        if (EventInfo is MyEventBuilder)
                                type = ((MyEventBuilder) EventInfo).EventType;
@@ -4021,15 +5485,68 @@ namespace Mono.CSharp {
                                type = EventInfo.EventHandlerType;
                }
 
+               public string Name {
+                       get {
+                               return EventInfo.Name;
+                       }
+               }
+
+               public bool IsInstance {
+                       get {
+                               return !is_static;
+                       }
+               }
+
+               public bool IsStatic {
+                       get {
+                               return is_static;
+                       }
+               }
+
+               public Type DeclaringType {
+                       get {
+                               return EventInfo.DeclaringType;
+                       }
+               }
+
+               public Expression InstanceExpression {
+                       get {
+                               return instance_expr;
+                       }
+
+                       set {
+                               instance_expr = value;
+                       }
+               }
+
+               Expression field_expr = null;
+
                public override Expression DoResolve (EmitContext ec)
                {
-                       // We are born fully resolved
+                       if (instance_expr != null) {
+                               instance_expr = instance_expr.DoResolve (ec);
+                               if (instance_expr == null)
+                                       return null;
+                       }
+
+                       if (this.DeclaringType == ec.ContainerType)     {
+                               MemberInfo mi = GetFieldFromEvent (this);
+                               if (mi == null)
+                                       return null;
+                               field_expr = ExprClassFromMemberInfo (ec, mi, loc);
+                               ((FieldExpr) field_expr).InstanceExpression = instance_expr;
+                               field_expr = field_expr.DoResolve (ec);
+                               if (field_expr == null)
+                                       return null;
+                       }
+
                        return this;
                }
 
                public override void Emit (EmitContext ec)
                {
-                       throw new Exception ("Should not happen I think");
+                       if (field_expr != null)
+                               field_expr.Emit (ec);
                }
 
                public void EmitAddOrRemove (EmitContext ec, Expression source)
@@ -4043,10 +5560,10 @@ namespace Mono.CSharp {
                        
                        if (((Binary) source).Oper == Binary.Operator.Addition)
                                Invocation.EmitCall (
-                                       ec, false, IsStatic, InstanceExpression, add_accessor, args, loc);
+                                       ec, false, IsStatic, instance_expr, add_accessor, args, loc);
                        else
                                Invocation.EmitCall (
-                                       ec, false, IsStatic, InstanceExpression, remove_accessor, args, loc);
+                                       ec, false, IsStatic, instance_expr, remove_accessor, args, loc);
                }
        }
-}      
+}