2004-08-07 Anirban Bhattacharjee <banirban@novell.com>
[mono.git] / mcs / mbas / delegate.cs
index a53c81a84947247fb36431c987e738d62c8ffa7a..aebe2a697d1142755130fc83421c5ad723cde55f 100644 (file)
@@ -15,13 +15,13 @@ using System.Reflection;
 using System.Reflection.Emit;
 using System.Text;
 
-namespace Mono.CSharp {
+namespace Mono.MonoBASIC {
 
        /// <summary>
        ///   Holds Delegates
        /// </summary>
        public class Delegate : DeclSpace {
-               public readonly string ReturnType;
+               public Expression ReturnType;
                public Parameters      Parameters;
                public Attributes      OptAttributes;
 
@@ -37,20 +37,23 @@ namespace Mono.CSharp {
                MethodBase delegate_method;
        
                const int AllowedModifiers =
-                       Modifiers.NEW |
+                       Modifiers.SHADOWS |
                        Modifiers.PUBLIC |
                        Modifiers.PROTECTED |
                        Modifiers.INTERNAL |
-                       Modifiers.UNSAFE |
+                   Modifiers.UNSAFE |
                        Modifiers.PRIVATE;
 
-               public Delegate (TypeContainer parent, string type, int mod_flags,
+               public Delegate (TypeContainer parent, Expression type, int mod_flags,
                                 string name, Parameters param_list,
                                 Attributes attrs, Location l)
                        : base (parent, name, l)
                {
                        this.ReturnType = type;
-                       ModFlags        = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PUBLIC, l);
+
+                       ModFlags        = Modifiers.Check (AllowedModifiers, mod_flags,
+                                                          IsTopLevel ? Modifiers.INTERNAL :
+                                                          Modifiers.PUBLIC, l);
                        Parameters      = param_list;
                        OptAttributes   = attrs;
                }
@@ -83,7 +86,12 @@ namespace Mono.CSharp {
                        return TypeBuilder;
                }
 
-               public override bool Define (TypeContainer parent)
+               public override bool DefineMembers (TypeContainer container)
+               {
+                       return true;
+               }
+
+               public override bool Define (TypeContainer container)
                {
                        MethodAttributes mattr;
                        int i;
@@ -137,18 +145,29 @@ namespace Mono.CSharp {
                        //
                        // Invoke method
                        //
-                       
+
                        // Check accessibility
                        foreach (Type partype in param_types)
-                               if (!TypeContainer.AsAccessible (partype, ModFlags))
+                               if (!container.AsAccessible (partype, ModFlags)) {
+                                       Report.Error (59, Location,
+                                                     "Inconsistent accessibility: parameter type `" +
+                                                     TypeManager.MonoBASIC_Name (partype) + "` is less " +
+                                                     "accessible than delegate `" + Name + "'");
                                        return false;
+                               }
                        
-                       ret_type = FindType (ReturnType);
+                       ReturnType = ResolveTypeExpr (ReturnType, false, Location);
+                       ret_type = ReturnType.Type;
                        if (ret_type == null)
                                return false;
 
-                       if (!TypeContainer.AsAccessible (ret_type, ModFlags))
+                       if (!container.AsAccessible (ret_type, ModFlags)) {
+                               Report.Error (58, Location,
+                                             "Inconsistent accessibility: return type `" +
+                                             TypeManager.MonoBASIC_Name (ret_type) + "` is less " +
+                                             "accessible than delegate `" + Name + "'");
                                return false;
+                       }
 
                        //
                        // We don't have to check any others because they are all
@@ -187,7 +206,7 @@ namespace Mono.CSharp {
                        InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
 
                        TypeManager.RegisterMethod (InvokeBuilder,
-                                                   new InternalParameters (parent, Parameters),
+                                                   new InternalParameters (container, Parameters),
                                                    param_types);
 
                        //
@@ -244,16 +263,18 @@ namespace Mono.CSharp {
                        if (Parameters.ArrayParameter != null)
                                async_params [n] = Parameters.ArrayParameter;
                        
-                       async_params [params_num] = new Parameter ("System.AsyncCallback", "callback",
+                       async_params [params_num] = new Parameter (
+                               TypeManager.system_asynccallback_expr, "callback",
                                                                   Parameter.Modifier.NONE, null);
-                       async_params [params_num + 1] = new Parameter ("System.Object", "object",
+                       async_params [params_num + 1] = new Parameter (
+                               TypeManager.system_object_expr, "object",
                                                                   Parameter.Modifier.NONE, null);
 
                        Parameters async_parameters = new Parameters (async_params, null, Location);
                        
                        async_parameters.ComputeAndDefineParameterTypes (this);
                        TypeManager.RegisterMethod (BeginInvokeBuilder,
-                                                   new InternalParameters (parent, async_parameters),
+                                                   new InternalParameters (container, async_parameters),
                                                    async_param_types);
 
                        //
@@ -272,13 +293,15 @@ namespace Mono.CSharp {
                        EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
 
                        Parameter [] end_params = new Parameter [1];
-                       end_params [0] = new Parameter ("System.IAsyncResult", "result",
+                       end_params [0] = new Parameter (
+                               TypeManager.system_iasyncresult_expr, "result",
                                                        Parameter.Modifier.NONE, null);
 
-                       TypeManager.RegisterMethod (EndInvokeBuilder,
-                                                   new InternalParameters (
-                                                           parent,
-                                                           new Parameters (end_params, null, Location)),
+                       TypeManager.RegisterMethod (
+                               EndInvokeBuilder, new InternalParameters (
+                                       container,
+                                       new Parameters (
+                                               end_params, null, Location)),
                                                    end_param_types);
 
                        return true;
@@ -299,7 +322,7 @@ namespace Mono.CSharp {
                                ec, delegate_type, "Invoke", loc);
 
                        if (!(ml is MethodGroupExpr)) {
-                               Report.Error (-100, loc, "Internal error : could not find Invoke method!");
+                               Report.Error (-100, loc, "Internal error: could not find Invoke method!");
                                return null;
                        }
 
@@ -310,39 +333,19 @@ namespace Mono.CSharp {
                        if (invoke_pd.Count != pd_count)
                                return null;
 
-                       bool mismatch = false;
                        for (int i = pd_count; i > 0; ) {
                                i--;
 
                                if (invoke_pd.ParameterType (i) == pd.ParameterType (i))
                                        continue;
-                               else {
-                                       mismatch = true;
-                                       break;
-                               }
-                       }
-
-                       if (mismatch) {
-                               Report.Error (
-                                       123, loc, "Method '" + Invocation.FullMethodDesc (mb) +
-                                       "' does not match delegate '" +
-                                       FullDelegateDesc (delegate_type, invoke_mb, invoke_pd) + "'");
-                               return null;
+                               else
+                                       return null;
                        }
 
                        if (((MethodInfo) invoke_mb).ReturnType == ((MethodInfo) mb).ReturnType)
                                return mb;
                        else
-                               mismatch = true;
-
-                       if (mismatch) {
-                               Report.Error (123, loc, "Method '" + Invocation.FullMethodDesc (mb) +
-                                             "' does not match delegate '" +
-                                             FullDelegateDesc (delegate_type, invoke_mb, invoke_pd) + "'");
                                return null;
-                       }
-
-                       return null;
                }
 
                // <summary>
@@ -363,9 +366,9 @@ namespace Mono.CSharp {
 
                        Expression ml = Expression.MemberLookup (
                                ec, delegate_type, "Invoke", loc);
-                       
+
                        if (!(ml is MethodGroupExpr)) {
-                               Report.Error (-100, loc, "Internal error : could not find Invoke method!");
+                               Report.Error (-100, loc, "Internal error: could not find Invoke method!" + delegate_type);
                                return false;
                        }
                        
@@ -374,7 +377,8 @@ namespace Mono.CSharp {
 
                        int pd_count = pd.Count;
 
-                       bool not_params_method = (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS);
+                       bool not_params_method = (pd_count == 0) ||
+                               (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS);
 
                        if (not_params_method && pd_count != arg_count) {
                                Report.Error (1593, loc,
@@ -397,7 +401,7 @@ namespace Mono.CSharp {
                                ec, delegate_type, "Invoke", loc);
                        
                        if (!(ml is MethodGroupExpr)) {
-                               Report.Error (-100, loc, "Internal error : could not find Invoke method!");
+                               Report.Error (-100, loc, "Internal error: could not find Invoke method!");
                                return false;
                        }
                        
@@ -408,7 +412,7 @@ namespace Mono.CSharp {
                                ec, delegate_type, "Invoke", loc);
                        
                        if (!(probe_ml is MethodGroupExpr)) {
-                               Report.Error (-100, loc, "Internal error : could not find Invoke method!");
+                               Report.Error (-100, loc, "Internal error: could not find Invoke method!");
                                return false;
                        }
                        
@@ -434,7 +438,7 @@ namespace Mono.CSharp {
                
                public static string FullDelegateDesc (Type del_type, MethodBase mb, ParameterData pd)
                {
-                       StringBuilder sb = new StringBuilder (TypeManager.CSharpName (((MethodInfo) mb).ReturnType));
+                       StringBuilder sb = new StringBuilder (TypeManager.MonoBASIC_Name (((MethodInfo) mb).ReturnType));
                        
                        sb.Append (" " + del_type.ToString ());
                        sb.Append (" (");
@@ -444,7 +448,7 @@ namespace Mono.CSharp {
                        for (int i = length; i > 0; ) {
                                i--;
                                
-                               sb.Append (TypeManager.CSharpName (pd.ParameterType (length - i - 1)));
+                               sb.Append (TypeManager.MonoBASIC_Name (pd.ParameterType (length - i - 1)));
                                if (i != 0)
                                        sb.Append (", ");
                        }
@@ -455,40 +459,38 @@ namespace Mono.CSharp {
                }
                
                // Hack around System.Reflection as found everywhere else
-               public MemberInfo [] FindMembers (MemberTypes mt, BindingFlags bf, MemberFilter filter, object criteria)
+               public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
+                                                       MemberFilter filter, object criteria)
                {
                        ArrayList members = new ArrayList ();
 
                        if ((mt & MemberTypes.Method) != 0) {
+                               if (ConstructorBuilder != null)
                                if (filter (ConstructorBuilder, criteria))
                                        members.Add (ConstructorBuilder);
 
+                               if (InvokeBuilder != null)
                                if (filter (InvokeBuilder, criteria))
                                        members.Add (InvokeBuilder);
 
+                               if (BeginInvokeBuilder != null)
                                if (filter (BeginInvokeBuilder, criteria))
                                        members.Add (BeginInvokeBuilder);
 
+                               if (EndInvokeBuilder != null)
                                if (filter (EndInvokeBuilder, criteria))
                                        members.Add (EndInvokeBuilder);
                        }
 
-                       int count = members.Count;
+                       return new MemberList (members);
+               }
 
-                       if (count > 0) {
-                               MemberInfo [] mi = new MemberInfo [count];
-                               members.CopyTo (mi, 0);
-                               return mi;
+               public override MemberCache MemberCache {
+                       get {
+                               return null;
                        }
-
-                       return null;
                }
-               
-               public void CloseDelegate ()
-               {
-                       TypeBuilder.CreateType ();
-               }
-               
+
                public Expression InstanceExpression {
                        get {
                                return instance_expr;
@@ -529,59 +531,93 @@ namespace Mono.CSharp {
                MethodBase delegate_method;
                Expression delegate_instance_expr;
 
-               Location Location;
-               
                public NewDelegate (Type type, ArrayList Arguments, Location loc)
                {
                        this.type = type;
                        this.Arguments = Arguments;
-                       this.Location  = loc; 
+                       this.loc  = loc; 
                }
 
                public override Expression DoResolve (EmitContext ec)
                {
                        if (Arguments == null) {
-                               Report.Error (-11, Location,
+                               Report.Error (-11, loc,
                                              "Delegate creation expression takes only one argument");
                                return null;
                        }
 
                        if (Arguments.Count != 1) {
-                               Report.Error (-11, Location,
+                               Report.Error (-11, loc,
                                              "Delegate creation expression takes only one argument");
                                return null;
                        }
 
                        Expression ml = Expression.MemberLookup (
-                               ec, type, ".ctor", Location);
+                               ec, type, ".ctor", loc);
 
                        if (!(ml is MethodGroupExpr)) {
-                               Report.Error (-100, Location, "Internal error : Could not find delegate constructor!");
+                               Report.Error (-100, loc, "Internal error: Could not find delegate constructor!");
                                return null;
                        }
 
                        constructor_method = ((MethodGroupExpr) ml).Methods [0];
                        Argument a = (Argument) Arguments [0];
                        
-                       if (!a.Resolve (ec, Location))
+                       if (!a.ResolveMethodGroup (ec, Location))
                                return null;
                        
                        Expression e = a.Expr;
 
+                       Expression invoke_method = Expression.MemberLookup (
+                               ec, type, "Invoke", MemberTypes.Method,
+                               Expression.AllBindingFlags, loc);
+
+                       if (invoke_method == null) {
+                               Report.Error (-200, loc, "Internal error ! Could not find Invoke method!");
+                               return null;
+                       }
+
                        if (e is MethodGroupExpr) {
                                MethodGroupExpr mg = (MethodGroupExpr) e;
 
                                foreach (MethodInfo mi in mg.Methods){
-                                       delegate_method  = Delegate.VerifyMethod (ec, type, mi, Location);
+                                       delegate_method  = Delegate.VerifyMethod (ec, type, mi, loc);
 
                                        if (delegate_method != null)
                                                break;
                                }
                                        
                                if (delegate_method == null) {
-                                       Report.Error (-14, Location, "Ambiguous method reference in delegate creation");
+                                       string method_desc;
+                                       if (mg.Methods.Length > 1)
+                                               method_desc = mg.Methods [0].Name;
+                                       else
+                                               method_desc = Invocation.FullMethodDesc (mg.Methods [0]);
+
+                                       MethodBase dm = ((MethodGroupExpr) invoke_method).Methods [0];
+                                       ParameterData param = Invocation.GetParameterData (dm);
+                                       string delegate_desc = Delegate.FullDelegateDesc (type, dm, param);
+
+                                       Report.Error (30408, loc, "Method '" + method_desc + "' does not " +
+                                                     "match delegate '" + delegate_desc + "'");
+
                                        return null;
                                }
+
+                               //
+                               // Check safe/unsafe of the delegate
+                               //
+                               if (!ec.InUnsafe){
+                                       ParameterData param = Invocation.GetParameterData (delegate_method);
+                                       int count = param.Count;
+                                       
+                                       for (int i = 0; i < count; i++){
+                                               if (param.ParameterType (i).IsPointer){
+                                                       Expression.UnsafeError (loc);
+                                                       return null;
+                                               }
+                                       }
+                               }
                                                
                                if (mg.InstanceExpression != null)
                                        delegate_instance_expr = mg.InstanceExpression.Resolve (ec);
@@ -603,7 +639,7 @@ namespace Mono.CSharp {
                        Type e_type = e.Type;
 
                        if (!TypeManager.IsDelegateType (e_type)) {
-                               Report.Error (-12, Location, "Cannot create a delegate from something " +
+                               Report.Error (-12, loc, "Cannot create a delegate from something " +
                                              "not a delegate or a method.");
                                return null;
                        }
@@ -611,23 +647,14 @@ namespace Mono.CSharp {
                        // This is what MS' compiler reports. We could always choose
                        // to be more verbose and actually give delegate-level specifics
                        
-                       if (!Delegate.VerifyDelegate (ec, type, e_type, Location)) {
-                               Report.Error (29, Location, "Cannot implicitly convert type '" + e_type + "' " +
+                       if (!Delegate.VerifyDelegate (ec, type, e_type, loc)) {
+                               Report.Error (29, loc, "Cannot implicitly convert type '" + e_type + "' " +
                                              "to type '" + type + "'");
                                return null;
                        }
-
-                       Expression invoke_method = Expression.MemberLookup (
-                               ec, e_type, "Invoke", MemberTypes.Method,
-                               Expression.AllBindingFlags, Location);
-
-                       if (invoke_method == null) {
-                               Report.Error (-200, Location, "Internal error ! COuld not find Invoke method!");
-                               return null;
-                       }
                                
                        delegate_instance_expr = e;
-                       delegate_method        = ((MethodGroupExpr) invoke_method).Methods [0];
+                       delegate_method = ((MethodGroupExpr) invoke_method).Methods [0];
                        
                        eclass = ExprClass.Value;
                        return this;
@@ -641,7 +668,11 @@ namespace Mono.CSharp {
                        else
                                delegate_instance_expr.Emit (ec);
                        
-                       ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
+                       if (delegate_method.IsVirtual) {
+                               ec.ig.Emit (OpCodes.Dup);
+                               ec.ig.Emit (OpCodes.Ldvirtftn, (MethodInfo) delegate_method);
+                       } else
+                               ec.ig.Emit (OpCodes.Ldftn, (MethodInfo) delegate_method);
                        ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) constructor_method);
                }
        }
@@ -650,7 +681,6 @@ namespace Mono.CSharp {
 
                public Expression InstanceExpr;
                public ArrayList  Arguments;
-               public Location   Location;
 
                MethodBase method;
                
@@ -658,32 +688,57 @@ namespace Mono.CSharp {
                {
                        this.InstanceExpr = instance_expr;
                        this.Arguments = args;
-                       this.Location = loc;
+                       this.loc = loc;
                }
 
                public override Expression DoResolve (EmitContext ec)
                {
+                       if (InstanceExpr is EventExpr) {
+                               
+                               EventInfo ei = ((EventExpr) InstanceExpr).EventInfo;
+                               
+                               Expression ml = MemberLookup (
+                                       ec, ec.ContainerType, ei.Name,
+                                       MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
+
+                               if (ml == null) {
+                                       //
+                                       // If this is the case, then the Event does not belong 
+                                       // to this Type and so, according to the spec
+                                       // cannot be accessed directly
+                                       //
+                                       // Note that target will not appear as an EventExpr
+                                       // in the case it is being referenced within the same type container;
+                                       // it will appear as a FieldExpr in that case.
+                                       //
+                                       
+                                       Assign.error70 (ei, loc);
+                                       return null;
+                               }
+                       }
+                       
+                       
                        Type del_type = InstanceExpr.Type;
                        if (del_type == null)
                                return null;
                        
                        if (Arguments != null){
                                foreach (Argument a in Arguments){
-                                       if (!a.Resolve (ec, Location))
+                                       if (!a.Resolve (ec, loc))
                                                return null;
                                }
                        }
                        
-                       if (!Delegate.VerifyApplicability (ec, del_type, Arguments, Location))
+                       if (!Delegate.VerifyApplicability (ec, del_type, Arguments, loc))
                                return null;
 
-                       Expression ml = Expression.MemberLookup (ec, del_type, "Invoke", Location);
-                       if (!(ml is MethodGroupExpr)) {
-                               Report.Error (-100, Location, "Internal error : could not find Invoke method!");
+                       Expression lookup = Expression.MemberLookup (ec, del_type, "Invoke", loc);
+                       if (!(lookup is MethodGroupExpr)) {
+                               Report.Error (-100, loc, "Internal error: could not find Invoke method!");
                                return null;
                        }
                        
-                       method = ((MethodGroupExpr) ml).Methods [0];
+                       method = ((MethodGroupExpr) lookup).Methods [0];
                        type = ((MethodInfo) method).ReturnType;
                        eclass = ExprClass.Value;
                        
@@ -698,7 +753,7 @@ namespace Mono.CSharp {
                        // Invocation on delegates call the virtual Invoke member
                        // so we are always `instance' calls
                        //
-                       Invocation.EmitCall (ec, false, false, InstanceExpr, method, Arguments, Location);
+                       Invocation.EmitCall (ec, false, false, InstanceExpr, method, Arguments, loc);
                }
 
                public override void EmitStatement (EmitContext ec)