2004-10-14 Umadevi S <sumadevi@novell.com>
[mono.git] / mcs / gmcs / delegate.cs
index 26a2a73a8ef8087dfe5316d168c212834dd04b14..617f4bc15610148790e2b615ab3890db215640bc 100644 (file)
@@ -63,7 +63,7 @@ namespace Mono.CSharp {
 
                public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
                {
-                       if (a.Target == "return") {
+                       if (a.Target == AttributeTargets.ReturnValue) {
                                if (return_attributes == null)
                                        return_attributes = new ReturnParameter (InvokeBuilder, Location);
 
@@ -138,7 +138,7 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               public override bool Define (TypeContainer container)
+               public override bool Define ()
                {
                        MethodAttributes mattr;
                        int i;
@@ -202,14 +202,14 @@ namespace Mono.CSharp {
 
                        // Check accessibility
                        foreach (Type partype in param_types){
-                               if (!container.AsAccessible (partype, ModFlags)) {
+                               if (!Parent.AsAccessible (partype, ModFlags)) {
                                        Report.Error (59, Location,
                                                      "Inconsistent accessibility: parameter type `" +
                                                      TypeManager.CSharpName (partype) + "` is less " +
                                                      "accessible than delegate `" + Name + "'");
                                        return false;
                                }
-                               if (partype.IsPointer && !UnsafeOK (container))
+                               if (partype.IsPointer && !UnsafeOK (Parent))
                                        return false;
                        }
                        
@@ -221,7 +221,7 @@ namespace Mono.CSharp {
                        if (ret_type == null)
                                return false;
 
-                       if (!container.AsAccessible (ret_type, ModFlags)) {
+                       if (!Parent.AsAccessible (ret_type, ModFlags)) {
                                Report.Error (58, Location,
                                              "Inconsistent accessibility: return type `" +
                                              TypeManager.CSharpName (ret_type) + "` is less " +
@@ -229,7 +229,7 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       if (ret_type.IsPointer && !UnsafeOK (container))
+                       if (ret_type.IsPointer && !UnsafeOK (Parent))
                                return false;
 
                        //
@@ -272,7 +272,7 @@ namespace Mono.CSharp {
                        InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
 
                        TypeManager.RegisterMethod (InvokeBuilder,
-                                                   new InternalParameters (container, Parameters),
+                                                   new InternalParameters (Parent, Parameters),
                                                    param_types);
 
                        //
@@ -339,7 +339,7 @@ namespace Mono.CSharp {
                        
                        async_parameters.ComputeAndDefineParameterTypes (this);
                        TypeManager.RegisterMethod (BeginInvokeBuilder,
-                                                   new InternalParameters (container, async_parameters),
+                                                   new InternalParameters (Parent, async_parameters),
                                                    async_param_types);
 
                        //
@@ -384,24 +384,25 @@ namespace Mono.CSharp {
 
                        TypeManager.RegisterMethod (
                                EndInvokeBuilder,
-                               new InternalParameters (container, end_parameters),
+                               new InternalParameters (Parent, end_parameters),
                                end_param_types);
 
                        return true;
                }
 
-               public override void Emit (TypeContainer tc)
+               public override void Emit ()
                {
                        if (OptAttributes != null) {
-                               EmitContext ec = new EmitContext (tc, this, Location, null, null, ModFlags, false);
+                               EmitContext ec = new EmitContext (
+                                       Parent, this, Location, null, null, ModFlags, false);
                                Parameters.LabelParameters (ec, InvokeBuilder, Location);
                                OptAttributes.Emit (ec, this);
                        }
 
-                       base.Emit (tc);
+                       base.Emit ();
                }
 
-               protected override string[] ValidAttributeTargets {
+               public override string[] ValidAttributeTargets {
                        get {
                                return attribute_targets;
                        }
@@ -417,7 +418,7 @@ namespace Mono.CSharp {
                        AttributeTester.AreParametersCompliant (Parameters.FixedParameters, Location);
 
                        if (!AttributeTester.IsClsCompliant (ReturnType.Type)) {
-                               Report.Error_T (3002, Location, GetSignatureForError ());
+                               Report.Error (3002, Location, "Return type of '{0}' is not CLS-compliant", GetSignatureForError ());
                        }
                        return true;
                }
@@ -472,10 +473,8 @@ namespace Mono.CSharp {
                //  Verifies whether the invocation arguments are compatible with the
                //  delegate's target method
                // </summary>
-               public static bool VerifyApplicability (EmitContext ec,
-                                                       Type delegate_type,
-                                                       ArrayList args,
-                                                       Location loc)
+               public static bool VerifyApplicability (EmitContext ec, Type delegate_type,
+                                                       ArrayList args, Location loc)
                {
                        int arg_count;
 
@@ -524,9 +523,13 @@ namespace Mono.CSharp {
 
                        bool ans = false;
                        if (arg_count == pd_count)
-                               ans = Invocation.VerifyArgumentsCompat (ec, args, arg_count, mb, false, delegate_type, loc);
+                               ans = Invocation.VerifyArgumentsCompat (
+                                       ec, args, arg_count, mb, false,
+                                       delegate_type, false, loc);
                        if (!ans && params_method)
-                               ans = Invocation.VerifyArgumentsCompat (ec, args, arg_count, mb, true,  delegate_type, loc);
+                               ans = Invocation.VerifyArgumentsCompat (
+                                       ec, args, arg_count, mb, true,
+                                       delegate_type, false, loc);
                        return ans;
                }
                
@@ -662,7 +665,7 @@ namespace Mono.CSharp {
 
                public override AttributeTargets AttributeTargets {
                        get {
-                               return AttributeTargets.Delegate | AttributeTargets.ReturnValue;
+                               return AttributeTargets.Delegate;
                        }
                }
 
@@ -682,7 +685,8 @@ namespace Mono.CSharp {
        public abstract class DelegateCreation : Expression {
                protected MethodBase constructor_method;
                protected MethodBase delegate_method;
-               protected Expression delegate_instance_expr;
+               protected MethodGroupExpr method_group;
+               protected Expression delegate_instance_expression;
 
                public DelegateCreation () {}
 
@@ -717,13 +721,12 @@ namespace Mono.CSharp {
                
                public override void Emit (EmitContext ec)
                {
-                       if (delegate_instance_expr == null ||
-                           delegate_method.IsStatic)
+                       if (delegate_instance_expression == null || delegate_method.IsStatic)
                                ec.ig.Emit (OpCodes.Ldnull);
                        else
-                               delegate_instance_expr.Emit (ec);
+                               delegate_instance_expression.Emit (ec);
                        
-                       if (delegate_method.IsVirtual) {
+                       if (delegate_method.IsVirtual && !method_group.IsBase) {
                                ec.ig.Emit (OpCodes.Dup);
                                ec.ig.Emit (OpCodes.Ldvirtftn, (MethodInfo) delegate_method);
                        } else
@@ -775,28 +778,38 @@ namespace Mono.CSharp {
                                        }
                                }
                                                
-                               if (mg.InstanceExpression != null)
-                                       delegate_instance_expr = mg.InstanceExpression.Resolve (ec);
-                               else {
-                                       if (ec.IsStatic){
-                                               if (!delegate_method.IsStatic){
-                                                       Report.Error (120, loc,
-                                                                     "An object reference is required for the non-static method " +
-                                                                     delegate_method.Name);
-                                                       return null;
-                                               }
-                                               delegate_instance_expr = null;
-                                       } else
-                                               delegate_instance_expr = ec.GetThis (loc);
+                       //TODO: implement caching when performance will be low
+                       IMethodData md = TypeManager.GetMethod (delegate_method);
+                       if (md == null) {
+                               if (System.Attribute.GetCustomAttribute (delegate_method, TypeManager.conditional_attribute_type) != null) {
+                                       Report.Error (1618, loc, "Cannot create delegate with '{0}' because it has a Conditional attribute", TypeManager.CSharpSignature (delegate_method));
+                               }
+                       } else {
+                               if (md.OptAttributes != null && md.OptAttributes.Search (TypeManager.conditional_attribute_type, ec) != null) {
+                                       Report.Error (1618, loc, "Cannot create delegate with '{0}' because it has a Conditional attribute", TypeManager.CSharpSignature (delegate_method));
                                }
-
-                               if (delegate_instance_expr != null)
-                                       if (delegate_instance_expr.Type.IsValueType)
-                                               delegate_instance_expr = new BoxedCast (delegate_instance_expr);
-                               
-                               eclass = ExprClass.Value;
-                               return this;
                        }
+                       
+                       if (mg.InstanceExpression != null)
+                               delegate_instance_expression = mg.InstanceExpression.Resolve (ec);
+                       else if (ec.IsStatic) {
+                               if (!delegate_method.IsStatic) {
+                                       Report.Error (120, loc,
+                                                     "An object reference is required for the non-static method " +
+                                                     delegate_method.Name);
+                                       return null;
+                               }
+                               delegate_instance_expression = null;
+                       } else
+                               delegate_instance_expression = ec.GetThis (loc);
+
+                       if (delegate_instance_expression != null && delegate_instance_expression.Type.IsValueType)
+                               delegate_instance_expression = new BoxedCast (mg.InstanceExpression);
+
+                       method_group = mg;
+                       eclass = ExprClass.Value;
+                       return this;
+               }
        }
 
        //
@@ -854,15 +867,6 @@ namespace Mono.CSharp {
 
                        Argument a = (Argument) Arguments [0];
                        
-                       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 (!a.ResolveMethodGroup (ec, loc))
                                return null;
                        
@@ -875,40 +879,33 @@ namespace Mono.CSharp {
                        Type e_type = e.Type;
 
                        if (!TypeManager.IsDelegateType (e_type)) {
-                               e.Error_UnexpectedKind ("method");
+                               e.Error_UnexpectedKind ("method", loc);
+                               return null;
+                       }
+
+                       method_group = Expression.MemberLookup (
+                               ec, type, "Invoke", MemberTypes.Method,
+                               Expression.AllBindingFlags, loc) as MethodGroupExpr;
+
+                       if (method_group == null) {
+                               Report.Error (-200, loc, "Internal error ! Could not find Invoke method!");
                                return null;
                        }
 
                        // 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, loc)) {
                                Report.Error (29, loc, "Cannot implicitly convert type '" + e_type + "' " +
                                              "to type '" + type + "'");
                                return null;
                        }
                                
-                       delegate_instance_expr = e;
-                       delegate_method = ((MethodGroupExpr) invoke_method).Methods [0];
+                       delegate_instance_expression = e;
+                       delegate_method = method_group.Methods [0];
                        
                        eclass = ExprClass.Value;
                        return this;
                }
-               
-               public override void Emit (EmitContext ec)
-               {
-                       if (delegate_instance_expr == null || delegate_method.IsStatic)
-                               ec.ig.Emit (OpCodes.Ldnull);
-                       else
-                               delegate_instance_expr.Emit (ec);
-                       
-                       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);
-               }
        }
 
        public class DelegateInvocation : ExpressionStatement {