svn path=/trunk/mcs/; revision=38482
[mono.git] / mcs / mbas / ecore.cs
index 9fabfa082a00f11fd37d5bc1165fc041765841a1..9b3b61a0b38de3d3c33ef349c20d2570018bf769 100644 (file)
@@ -325,10 +325,10 @@ namespace Mono.MonoBASIC {
                                                ec.ContainerType, ec.ContainerType, AllMemberTypes,
                                                AllBindingFlags | BindingFlags.NonPublic, s.Name);
                                        if (lookup != null)
-                                               Error (122, "'" + s.Name + "' " +
+                                               Error (30390, "'" + s.Name + "' " +
                                                       "is inaccessible because of its protection level");
                                        else
-                                               Error (103, "The name '" + s.Name + "' could not be " +
+                                               Error (30451, "The name '" + s.Name + "' could not be " +
                                                       "found in '" + ec.DeclSpace.Name + "'");
                                        return null;
                                }
@@ -414,7 +414,7 @@ namespace Mono.MonoBASIC {
                                        SimpleName s = (SimpleName) e;
 
                                        Report.Error (
-                                               103, loc,
+                                               30451, loc,
                                                "The name '" + s.Name + "' could not be found in '" +
                                                ec.DeclSpace.Name + "'");
                                        return null;
@@ -650,11 +650,11 @@ namespace Mono.MonoBASIC {
                                          AllBindingFlags | BindingFlags.NonPublic, loc);
                        if (e == null){
                                Report.Error (
-                                       117, loc, "'" + t + "' does not contain a definition " +
+                                       30456, loc, "'" + t + "' does not contain a definition " +
                                        "for '" + name + "'");
                        } else {
                                        Report.Error (
-                                               122, loc, "'" + t + "." + name +
+                                               30390, loc, "'" + t + "." + name +
                                                "' is inaccessible due to its protection level");
                        }
                        
@@ -942,10 +942,8 @@ namespace Mono.MonoBASIC {
                // 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.
                        //
@@ -1025,10 +1023,12 @@ namespace Mono.MonoBASIC {
                        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;
                }
@@ -1039,14 +1039,34 @@ namespace Mono.MonoBASIC {
                /// </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;
 
+                       // 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){
@@ -1153,7 +1173,7 @@ namespace Mono.MonoBASIC {
                                        return true;
                        }       
                        
-                       if (ImplicitReferenceConversionExists (expr, target_type))
+                       if (ImplicitReferenceConversionExists (expr, expr_type, target_type))
                                return true;
                        
                        if (expr is IntConstant){
@@ -1630,7 +1650,7 @@ namespace Mono.MonoBASIC {
                        if (e != null)
                                return e;
                                
-                       e = RuntimeConversion (ec, expr, target_type, loc);
+                       e = NarrowingConversion (ec, expr, target_type, loc);
                        if (e != null)
                                return e;                               
 
@@ -1658,15 +1678,226 @@ namespace Mono.MonoBASIC {
                        return (e);             
                }
                
-               static public bool RuntimeConversionExists (EmitContext ec, Expression expr, Type target_type)
+               static public bool NarrowingConversionExists (EmitContext ec, Expression expr, Type target_type)
                {
-                       return (RuntimeConversion (ec, expr, target_type,Location.Null)) != null;       
+                       Type expr_type = expr.Type;
+
+                       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.char_type){
+                               //
+                               // To char from ushort, int, uint, long, ulong, float, double
+                               // 
+                               if ((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.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 RuntimeConversion (EmitContext ec, Expression expr,
+               static public Expression NarrowingConversion (EmitContext ec, Expression expr,
                                                                Type target_type, Location loc)
                {
                        Type expr_type = expr.Type;
+
+                       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 new OpcodeCast (expr, target_type, OpCodes.Conv_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) ||
+                                   (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 new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
+       
+                       } 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 new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
+                                       
+                       } 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 new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+                                   
+                       } 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 new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
+                                       
+                       } 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 new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
+                                       
+                       } 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 new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
+                                   
+                       } else if (target_type == TypeManager.char_type){
+                               //
+                               // To char from ushort, int, uint, long, ulong, float, double
+                               // 
+                               if ((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 new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
+
+                       } else if (target_type == TypeManager.float_type){
+                               //
+                               // To float from double
+                               //
+                               if (expr_type == TypeManager.double_type)
+                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
+                       }       
+
                        TypeCode dest_type = Type.GetTypeCode (target_type);
                        TypeCode src_type = Type.GetTypeCode (expr_type);
                        Expression e = null;
@@ -1676,9 +1907,10 @@ namespace Mono.MonoBASIC {
                        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);
                        }
-                               
+
                        switch (dest_type) {
                                case TypeCode.String:
                                        switch (src_type) {
@@ -1748,7 +1980,17 @@ namespace Mono.MonoBASIC {
                                case TypeCode.Byte:
                                        // Ok, this *is* broken
                                        e = RTConversionExpression(ec, "ByteType.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;                                                                                                          
                        }
                        
                        // We must examine separately some types that
@@ -1788,11 +2030,14 @@ namespace Mono.MonoBASIC {
                        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) {
@@ -2321,7 +2566,7 @@ namespace Mono.MonoBASIC {
                ///   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);
@@ -2364,7 +2609,7 @@ namespace Mono.MonoBASIC {
                                if (t != null)
                                        return t;
                                
-                               t = RuntimeConversion (ec, e, target_type, loc);
+                               t = NarrowingConversion (ec, e, target_type, loc);
                                if (t != null)
                                        return t;       
                                                                
@@ -2424,12 +2669,14 @@ namespace Mono.MonoBASIC {
                        ne = ExplicitUserConversion (ec, expr, target_type, loc);
                        if (ne != null)
                                return ne;
+
+                       if (!(runtimeconv))     {
+                               ne = NarrowingConversion (ec, expr, target_type, loc);
+                               if (ne != null)
+                                       return ne;
                                
-                       ne = RuntimeConversion (ec, expr, target_type, loc);
-                       if (ne != null)
-                               return ne;
-                                       
-                       Error_CannotConvertType (loc, expr_type, target_type);
+                               Error_CannotConvertType (loc, expr_type, target_type);
+                       }
                        return null;
                }
 
@@ -2452,7 +2699,7 @@ namespace Mono.MonoBASIC {
                        if (ne != null)
                                return ne;
 
-                       ne = RuntimeConversion (ec, expr, target_type, l);
+                       ne = NarrowingConversion (ec, expr, target_type, l);
                        if (ne != null)
                                return ne;                              
 
@@ -3640,7 +3887,7 @@ namespace Mono.MonoBASIC {
                Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
                {
                        Expression e = null;
-
+                       
                        //
                        // Stage 1: Performed by the parser (binding to locals or parameters).
                        //
@@ -3708,15 +3955,30 @@ namespace Mono.MonoBASIC {
 // #52067 - Start - Trying to solve
 
                        if (e == null) {
-                               string[] NamespacesInScope = RootContext.SourceBeingCompiled.GetNamespacesInScope(ec.DeclSpace.Namespace.Name);
+                               
                                ArrayList lookups = new ArrayList();
                                ArrayList typelookups = new ArrayList();
                                
+                               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);
+                                               }
+                                       }
+                               }
+                               
+                               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)  
+                                       if (e != null) {
                                                lookups.Add(e);
                                                typelookups.Add(type);
+                                       }
                                }
                                if (lookups.Count == 1) { 
                                        e = (Expression)lookups[0];
@@ -3769,7 +4031,7 @@ namespace Mono.MonoBASIC {
                                else
                                        e = e.DoResolve (ec);
 
-                               return e;                               
+                               return e;
                        }
 
                        if (ec.IsStatic || ec.IsFieldInitializer){
@@ -3788,7 +4050,7 @@ namespace Mono.MonoBASIC {
                        // find the name as a namespace
                        //
 
-                       Error (103, "The name '" + Name +
+                       Error (30451, "The name '" + Name +
                               "' does not exist in the class '" +
                               ec.DeclSpace.Name + "'");
                }
@@ -4528,6 +4790,8 @@ namespace Mono.MonoBASIC {
                        }
                }
 
+               Expression field_expr = null;
+
                public override Expression DoResolve (EmitContext ec)
                {
                        if (instance_expr != null) {
@@ -4536,12 +4800,24 @@ namespace Mono.MonoBASIC {
                                        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)
                {
-                       Report.Error (70, loc, "The event '" + Name + "' can only appear on the left hand side of += or -= (except on the defining type)");
+                       if (field_expr != null)
+                               field_expr.Emit (ec);
                }
 
                public void EmitAddOrRemove (EmitContext ec, Expression source)