ChangeLog: Updated ChangeLog.
[mono.git] / mcs / mbas / delegate.cs
index 68ca9ec9f5d2a103f66ab63e435d6d2c7bc7ca54..aebe2a697d1142755130fc83421c5ad723cde55f 100644 (file)
@@ -15,7 +15,7 @@ using System.Reflection;
 using System.Reflection.Emit;
 using System.Text;
 
-namespace Mono.CSharp {
+namespace Mono.MonoBASIC {
 
        /// <summary>
        ///   Holds Delegates
@@ -37,11 +37,11 @@ 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, Expression type, int mod_flags,
@@ -50,9 +50,10 @@ namespace Mono.CSharp {
                        : base (parent, name, l)
                {
                        this.ReturnType = type;
+
                        ModFlags        = Modifiers.Check (AllowedModifiers, mod_flags,
                                                           IsTopLevel ? Modifiers.INTERNAL :
-                                                          Modifiers.PRIVATE, l);
+                                                          Modifiers.PUBLIC, l);
                        Parameters      = param_list;
                        OptAttributes   = attrs;
                }
@@ -85,6 +86,11 @@ namespace Mono.CSharp {
                        return TypeBuilder;
                }
 
+               public override bool DefineMembers (TypeContainer container)
+               {
+                       return true;
+               }
+
                public override bool Define (TypeContainer container)
                {
                        MethodAttributes mattr;
@@ -145,7 +151,7 @@ namespace Mono.CSharp {
                                if (!container.AsAccessible (partype, ModFlags)) {
                                        Report.Error (59, Location,
                                                      "Inconsistent accessibility: parameter type `" +
-                                                     TypeManager.CSharpName (partype) + "` is less " +
+                                                     TypeManager.MonoBASIC_Name (partype) + "` is less " +
                                                      "accessible than delegate `" + Name + "'");
                                        return false;
                                }
@@ -158,7 +164,7 @@ namespace Mono.CSharp {
                        if (!container.AsAccessible (ret_type, ModFlags)) {
                                Report.Error (58, Location,
                                              "Inconsistent accessibility: return type `" +
-                                             TypeManager.CSharpName (ret_type) + "` is less " +
+                                             TypeManager.MonoBASIC_Name (ret_type) + "` is less " +
                                              "accessible than delegate `" + Name + "'");
                                return false;
                        }
@@ -316,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;
                        }
 
@@ -360,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;
                        }
                        
@@ -371,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,
@@ -394,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;
                        }
                        
@@ -405,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;
                        }
                        
@@ -431,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 (" (");
@@ -441,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 (", ");
                        }
@@ -452,7 +459,8 @@ 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 ();
 
@@ -474,22 +482,15 @@ namespace Mono.CSharp {
                                        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;
@@ -555,14 +556,14 @@ namespace Mono.CSharp {
                                ec, type, ".ctor", loc);
 
                        if (!(ml is MethodGroupExpr)) {
-                               Report.Error (-100, loc, "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, loc))
+                       if (!a.ResolveMethodGroup (ec, Location))
                                return null;
                        
                        Expression e = a.Expr;
@@ -572,7 +573,7 @@ namespace Mono.CSharp {
                                Expression.AllBindingFlags, loc);
 
                        if (invoke_method == null) {
-                               Report.Error (-200, loc, "Internal error ! COuld not find Invoke method!");
+                               Report.Error (-200, loc, "Internal error ! Could not find Invoke method!");
                                return null;
                        }
 
@@ -597,11 +598,26 @@ namespace Mono.CSharp {
                                        ParameterData param = Invocation.GetParameterData (dm);
                                        string delegate_desc = Delegate.FullDelegateDesc (type, dm, param);
 
-                                       Report.Error (123, loc, "Method '" + method_desc + "' does not " +
+                                       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);
@@ -638,7 +654,7 @@ namespace Mono.CSharp {
                        }
                                
                        delegate_instance_expr = e;
-                       delegate_method        = ((MethodGroupExpr) invoke_method).Methods [0];
+                       delegate_method = ((MethodGroupExpr) invoke_method).Methods [0];
                        
                        eclass = ExprClass.Value;
                        return this;
@@ -652,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);
                }
        }
@@ -673,6 +693,31 @@ namespace Mono.CSharp {
 
                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;
@@ -687,13 +732,13 @@ namespace Mono.CSharp {
                        if (!Delegate.VerifyApplicability (ec, del_type, Arguments, loc))
                                return null;
 
-                       Expression ml = Expression.MemberLookup (ec, del_type, "Invoke", loc);
-                       if (!(ml is MethodGroupExpr)) {
-                               Report.Error (-100, loc, "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;