2004-08-03 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / mbas / expression.cs
index 91f86df21701986df13d7d955c0087ef1d061d9f..ceed21db3885a84b2039a265a8b3ad9f82db3eef 100644 (file)
@@ -1130,7 +1130,7 @@ namespace Mono.MonoBASIC {
 
                        if (TypeManager.IsValueType (probe_type)){
                                Report.Error (77, loc, "The as operator should be used with a reference type only (" +
-                                             TypeManager.MonoBASIC_Name (probe_type) + " is a value type");
+                                             TypeManager.MonoBASIC_Name (probe_type) + " is a value type)");
                                return null;
                        
                        }
@@ -1161,12 +1161,14 @@ namespace Mono.MonoBASIC {
        public class Cast : Expression {
                Expression target_type;
                Expression expr;
+               bool runtime_cast;
                        
                public Cast (Expression cast_type, Expression expr, Location loc)
                {
                        this.target_type = cast_type;
                        this.expr = expr;
                        this.loc = loc;
+                       runtime_cast = false;
                }
 
                public Expression TargetType {
@@ -1184,6 +1186,16 @@ namespace Mono.MonoBASIC {
                        }
                }
 
+               public bool IsRuntimeCast\r
+               {
+                       get {
+                               return runtime_cast;
+                       }
+                       set{
+                               runtime_cast = value;
+                       }
+               }
+
                /// <summary>
                ///   Attempts to do a compile-time folding of a constant cast.
                /// </summary>
@@ -1462,7 +1474,7 @@ namespace Mono.MonoBASIC {
                        int errors = Report.Errors;
 
                        type = ec.DeclSpace.ResolveType (target_type, false, Location);
-                       
+
                        if (type == null)
                                return null;
 
@@ -1475,7 +1487,7 @@ namespace Mono.MonoBASIC {
                                        return e;
                        }
                        
-                       expr = ConvertExplicit (ec, expr, type, loc);
+                       expr = ConvertExplicit (ec, expr, type, runtime_cast, loc);
                        return expr;
                }
 
@@ -3573,7 +3585,7 @@ namespace Mono.MonoBASIC {
                                ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
 
                        if (a_mod == p_mod || (a_mod == Parameter.Modifier.NONE && p_mod == Parameter.Modifier.PARAMS)) {
-                               if (a_mod == Parameter.Modifier.NONE)                                   
+                               if (a_mod == Parameter.Modifier.NONE)   
                                        if (! (ImplicitConversionExists (ec, a.Expr, ptype) || RuntimeConversionExists (ec, a.Expr, ptype)) )
                                                return false;
                                
@@ -3607,13 +3619,11 @@ namespace Mono.MonoBASIC {
 
                        ParameterData pd = GetParameterData (candidate);
                        Parameters ps = GetFullParameters (candidate);
-                       
                        if (ps == null) {
                                ps_count = 0;
                                po_count = 0;
                        }
-                       else
-                       {
+                       else {
                                ps_count = ps.CountStandardParams();                    
                                po_count = ps.CountOptionalParams();
                        }
@@ -3624,8 +3634,7 @@ namespace Mono.MonoBASIC {
                                if (arg_count != pd.Count)
                                        return false;
                        }
-                       else
-                       {
+                       else {
                                if ((arg_count < ps_count) || (arg_count > pd_count))
                                        return false;   
                        }       
@@ -3635,33 +3644,55 @@ namespace Mono.MonoBASIC {
                                        i--;
 
                                        Argument a = (Argument) arguments [i];
-                                       if (a.ArgType == Argument.AType.NoArg)
-                                       {
+                                       if (a.ArgType == Argument.AType.NoArg) {
                                                Parameter p = (Parameter) ps.FixedParameters[i];
                                                a = new Argument (p.ParameterInitializer, Argument.AType.Expression);
                                                param_type = p.ParameterInitializer.Type;
                                        }
-                                       else 
-                                       {
+                                       else {
                                                param_type = pd.ParameterType (i);
                                                if (ps != null) {
                                                        Parameter p = (Parameter) ps.FixedParameters[i];
-                                                       
-                                                       if ((p.ModFlags & Parameter.Modifier.REF) != 0) 
-                                                       {
+                                                       bool IsDelegate = TypeManager.IsDelegateType (param_type);
+
+                                                       if (IsDelegate) {       
+                                                               if (a.ArgType == Argument.AType.AddressOf) {
+                                                                       a = new Argument ((Expression) a.Expr, Argument.AType.Expression);
+                                                                       ArrayList args = new ArrayList();
+                                                                       args.Add (a);
+                                                                       string param_name = pd.ParameterDesc(i).Replace('+', '.');
+                                                                       Expression pname = MonoBASIC.Parser.DecomposeQI (param_name, Location.Null);
+
+
+                                                                       New temp_new = new New ((Expression)pname, args, Location.Null);
+                                                                       Expression del_temp = temp_new.DoResolve(ec);
+
+                                                                       if (del_temp == null)
+                                                                               return false;
+
+                                                                       a = new Argument (del_temp, Argument.AType.Expression);
+                                                                       if (!a.Resolve(ec, Location.Null))\r
+                                                                               return false;
+                                                               }
+                                                       }
+                                                       else {
+                                                               if (a.ArgType == Argument.AType.AddressOf)
+                                                                       return false;
+                                                       }
+
+                                                       if ((p.ModFlags & Parameter.Modifier.REF) != 0) {
                                                                a = new Argument (a.Expr, Argument.AType.Ref);
                                                                if (!a.Resolve(ec,Location.Null))
                                                                        return false;
                                                        }
                                                }
                                        }       
-       
+\r
                                        if (!CheckParameterAgainstArgument (ec, pd, i, a, param_type))
                                                return (false);
                                }
                        }
-                       else
-                       {
+                       else {
                                // If we have no arguments AND the first parameter is optional
                                // we must check for a candidate (the loop above wouldn't)      
                                if (po_count > 0) {
@@ -3684,6 +3715,7 @@ namespace Mono.MonoBASIC {
                        }
                        // We've found a candidate, so we exchange the dummy NoArg arguments
                        // with new arguments containing the default value for that parameter
+
                        ArrayList newarglist = new ArrayList();
                        for (int i = 0; i < arg_count; i++) {
                                Argument a = (Argument) arguments [i];
@@ -3695,19 +3727,41 @@ namespace Mono.MonoBASIC {
                                if (a.ArgType == Argument.AType.NoArg){
                                        a = new Argument (p.ParameterInitializer, Argument.AType.Expression);
                                        a.Resolve(ec, Location.Null);
-                               }               
-                               
-                               if ((p != null) && ((p.ModFlags & Parameter.Modifier.REF) != 0))
-                               {
+                               }
+
+                               // ToDo - This part is getting resolved second time within this function
+                               // This is a costly operation
+                               // The earlier resoved result should be used here.
+                               // Has to be done during compiler optimization.
+                               if (a.ArgType == Argument.AType.AddressOf) {
+                                       param_type = pd.ParameterType (i);
+                                       bool IsDelegate = TypeManager.IsDelegateType (param_type);
+
+                                       a = new Argument ((Expression) a.Expr, Argument.AType.Expression);
+                                       ArrayList args = new ArrayList();
+                                       args.Add (a);
+                                       string param_name = pd.ParameterDesc(i).Replace('+', '.');
+                                       Expression pname = MonoBASIC.Parser.DecomposeQI (param_name, Location.Null);
+                                                               
+                                       New temp_new = new New ((Expression)pname, args, Location.Null);
+                                       Expression del_temp = temp_new.DoResolve(ec);
+
+                                       if (del_temp == null)
+                                               return false;
+
+                                       a = new Argument (del_temp, Argument.AType.Expression);
+                                       if (!a.Resolve(ec, Location.Null))\r
+                                               return false;
+                               }
+
+                               if ((p != null) && ((p.ModFlags & Parameter.Modifier.REF) != 0)) {
                                        a.ArgType = Argument.AType.Ref;
                                        a.Resolve(ec, Location.Null);
                                }       
                                newarglist.Add(a);
                                int n = pd_count - arg_count;
-                               if (n > 0) 
-                               {
-                                       for (int x = 0; x < n; x++) 
-                                       {
+                               if (n > 0) {
+                                       for (int x = 0; x < n; x++) {
                                                Parameter op = (Parameter) ps.FixedParameters[x + arg_count];
                                                Argument b = new Argument (op.ParameterInitializer, Argument.AType.Expression);
                                                b.Resolve(ec, Location.Null);
@@ -4039,7 +4093,8 @@ namespace Mono.MonoBASIC {
                                {
                                        if ((a.ArgType == Argument.AType.NoArg) && (!(expr is MethodGroupExpr)))
                                                Report.Error (999, "This item cannot have empty arguments");
-                                       
+
+                       
                                        if (!a.Resolve (ec, loc))
                                                return null;                            
                                }
@@ -4052,8 +4107,8 @@ namespace Mono.MonoBASIC {
 
                                if (method == null)
                                {
-                                       Error (-6,
-                                               "Could not find any applicable function for this argument list");
+                                       Error (30455,
+                                               "Could not find any applicable function to invoke for this argument list");
                                        return null;
                                }
 
@@ -4465,6 +4520,7 @@ namespace Mono.MonoBASIC {
                //
                Expression value_target;
                bool value_target_set = false;
+               public bool isDelegate = false;
                
                public New (Expression requested_type, ArrayList arguments, Location l)
                {
@@ -4513,7 +4569,16 @@ namespace Mono.MonoBASIC {
                
                public override Expression DoResolve (EmitContext ec)
                {
-                       type = ec.DeclSpace.ResolveType (RequestedType, false, loc);
+                       if (this.isDelegate) {
+                               // if its a delegate resolve the type of RequestedType first
+                               Expression dtype = RequestedType.Resolve(ec);
+                               string ts = (dtype.Type.ToString()).Replace ('+','.');
+                               dtype = Mono.MonoBASIC.Parser.DecomposeQI (ts, Location.Null);
+
+                               type = ec.DeclSpace.ResolveType (dtype, false, loc);
+                       }
+                       else
+                               type = ec.DeclSpace.ResolveType (RequestedType, false, loc);
                        
                        if (type == null)
                                return null;
@@ -4525,8 +4590,8 @@ namespace Mono.MonoBASIC {
 
                        if (type.IsInterface || type.IsAbstract){
                                Error (
-                                       144, "It is not possible to create instances of interfaces " +
-                                       "or abstract classes");
+                                       30376, "It is not possible to create instances of Interfaces " +
+                                       "or classes marked as MustInherit");
                                return null;
                        }
                        
@@ -4742,7 +4807,7 @@ namespace Mono.MonoBASIC {
 
                void Error_IncorrectArrayInitializer ()
                {
-                       Error (178, "Incorrectly structured array initializer");
+                       Error (30567, "Incorrectly structured array initializer");
                }
                
                public bool CheckIndices (EmitContext ec, ArrayList probe, int idx, bool specified_dims)
@@ -5692,12 +5757,16 @@ namespace Mono.MonoBASIC {
                                                Enum en = TypeManager.LookupEnum (decl_type);
 
                                                Constant c;
-                                               if (en != null)
+                                               if (en != null) {
                                                        c = Constantify (o, en.UnderlyingType);
-                                               else 
+                                                       return new EnumConstant (c, en.UnderlyingType);
+                                               }
+                                               else {
                                                        c = Constantify (o, enum_member.Type);
+                                                       return new EnumConstant (c, enum_member.Type);
+                                               }
+                                               
                                                
-                                               return new EnumConstant (c, decl_type);
                                        }
                                        
                                        Expression exp = Constantify (o, t);
@@ -5770,10 +5839,10 @@ namespace Mono.MonoBASIC {
                                                if (IdenticalNameAndTypeName (ec, left_original, loc))
                                                        return member_lookup;
 
-                                               if (left_is_explicit) {
+                                               /*if (left_is_explicit) {
                                                        error176 (loc, me.Name);
                                                        return null;
-                                               }
+                                               }*/
                                        }
 
                                        //
@@ -5833,37 +5902,18 @@ namespace Mono.MonoBASIC {
 
                        if (expr is SimpleName){
                                SimpleName child_expr = (SimpleName) expr;
-                               
+
                                Expression new_expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
 
-                               return new_expr.Resolve (ec, flags);
+                               if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type)
+                                       return new_expr.Resolve (ec, flags);
+                               else
+                                       return new_expr.Resolve (ec, flags | ResolveFlags.MethodGroup | ResolveFlags.VariableOrValue);
                        }
                                        
-                       //
-                       // TODO: I mailed Ravi about this, and apparently we can get rid
-                       // of this and put it in the right place.
-                       // 
-                       // Handle enums here when they are in transit.
-                       // Note that we cannot afford to hit MemberLookup in this case because
-                       // it will fail to find any members at all
-                       //
-
                        int errors = Report.Errors;
                        
                        Type expr_type = expr.Type;
-                       if ((expr is TypeExpr) && (expr_type.IsSubclassOf (TypeManager.enum_type))){
-                               
-                               Enum en = TypeManager.LookupEnum (expr_type);
-                               
-                               if (en != null) {
-                                       object value = en.LookupEnumValue (ec, Identifier, loc);
-
-                                       if (value != null){
-                                               Constant c = Constantify (value, en.UnderlyingType);
-                                               return new EnumConstant (c, expr_type);
-                                       }
-                               }
-                       }
 
                        if (expr_type.IsPointer){
                                Error (23, "The '.' operator can not be applied to pointer operands (" +
@@ -5888,7 +5938,7 @@ namespace Mono.MonoBASIC {
                                        BindingFlags.NonPublic, Identifier);
                                        
                                if (lookup == null)
-                                       Error (117, "'" + expr_type + "' does not contain a definition for '" + Identifier + "'");
+                                       Error (30456, "'" + expr_type + "' does not contain a definition for '" + Identifier + "'");
                                else
                                {
                                        if ((expr_type != ec.ContainerType) &&
@@ -5912,17 +5962,31 @@ namespace Mono.MonoBASIC {
                                                       "qualifier must be of type '" + TypeManager.MonoBASIC_Name (ec.ContainerType) + "' " +
                                                       "(or derived from it)");
                                                else
-                                                       Error (122, "'" + expr_type + "." + Identifier + "' " +
+                                                       Error (30390, "'" + expr_type + "." + Identifier + "' " +
                                                       "is inaccessible because of its protection level");
                                        } else
-                                               Error (122, "'" + expr_type + "." + Identifier + "' " +
+                                               Error (30390, "'" + expr_type + "." + Identifier + "' " +
                                               "is inaccessible because of its protection level");
                                }  
                                return null;
                        }
 
+                       if ((expr is TypeExpr) && (expr_type.IsSubclassOf (TypeManager.enum_type)))     {
+                               Enum en = TypeManager.LookupEnum (expr_type);
+                               
+                               if (en != null) {
+                                       object value = en.LookupEnumValue (ec, Identifier, loc);
+                                       expr_type = TypeManager.int32_type;
+                                       if (value != null) {
+                                               Constant c = Constantify (value, en.UnderlyingType);
+                                               return new EnumConstant (c, en.UnderlyingType);
+                                       }
+                               }
+                       }
+
                        if (member_lookup is TypeExpr){
                                member_lookup.Resolve (ec, ResolveFlags.Type);
+
                                return member_lookup;
                        } else if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type)
                                return null;
@@ -5933,7 +5997,6 @@ namespace Mono.MonoBASIC {
 
                        // The following DoResolve/DoResolveLValue will do the definite assignment
                        // check.
-
                        if (right_side != null)
                                member_lookup = member_lookup.DoResolveLValue (ec, right_side);
                        else
@@ -6762,7 +6825,7 @@ namespace Mono.MonoBASIC {
                                                      AllMemberTypes, AllBindingFlags, loc);
 
                        if (member_lookup == null) {
-                               Error (117,
+                               Error (30456,
                                              TypeManager.MonoBASIC_Name (base_type) + " does not " +
                                              "contain a definition for '" + member + "'");
                                return null;