Update
[mono.git] / mcs / mcs / delegate.cs
index f71a8c7fa7b5ec55f9527809c0416367e66c2db3..00858b8ceb38ba1183cb024f4f42dcd31d36b84c 100644 (file)
@@ -49,7 +49,7 @@ namespace Mono.CSharp {
                        Modifiers.PRIVATE;
 
                public Delegate (NamespaceEntry ns, TypeContainer parent, Expression type,
-                                int mod_flags, string name, Parameters param_list,
+                                int mod_flags, MemberName name, Parameters param_list,
                                 Attributes attrs, Location l)
                        : base (ns, parent, name, attrs, l)
 
@@ -79,9 +79,19 @@ namespace Mono.CSharp {
                        if (TypeBuilder != null)
                                return TypeBuilder;
 
+                       ec = new EmitContext (this, this, Location, null, null, ModFlags, false);
+
                        TypeAttributes attr = Modifiers.TypeAttr (ModFlags, IsTopLevel) |
                                TypeAttributes.Class | TypeAttributes.Sealed;
 
+                       if (TypeManager.multicast_delegate_type == null && !RootContext.StdLib) {
+                               TypeExpr expr = new TypeLookupExpression ("System.MulticastDelegate");
+                               TypeManager.multicast_delegate_type = expr.ResolveType (ec);
+                       }
+
+                       if (TypeManager.multicast_delegate_type == null)
+                               throw new InternalErrorException ("System.MulticastDelegate unresolved");
+
                        if (IsTopLevel) {
                                if (TypeManager.NamespaceClash (Name, Location))
                                        return null;
@@ -112,8 +122,9 @@ namespace Mono.CSharp {
                {
                        MethodAttributes mattr;
                        int i;
-                       EmitContext ec = new EmitContext (this, this, Location, null,
-                                                         null, ModFlags, false);
+
+                       if (ec == null)
+                               throw new InternalErrorException ("Define called before DefineType?");
 
                        // FIXME: POSSIBLY make this static, as it is always constant
                        //
@@ -136,8 +147,10 @@ namespace Mono.CSharp {
                        //
                        // FIXME: POSSIBLY make these static, as they are always the same
                        Parameter [] fixed_pars = new Parameter [2];
-                       fixed_pars [0] = new Parameter (null, null, Parameter.Modifier.NONE, null);
-                       fixed_pars [1] = new Parameter (null, null, Parameter.Modifier.NONE, null);
+                       fixed_pars [0] = new Parameter (TypeManager.system_object_expr, "object",
+                                                       Parameter.Modifier.NONE, null);
+                       fixed_pars [1] = new Parameter (TypeManager.system_intptr_expr, "method", 
+                                                       Parameter.Modifier.NONE, null);
                        Parameters const_parameters = new Parameters (fixed_pars, null, Location);
                        
                        TypeManager.RegisterMethod (
@@ -154,10 +167,10 @@ namespace Mono.CSharp {
                        // First, call the `out of band' special method for
                        // defining recursively any types we need:
                        
-                       if (!Parameters.ComputeAndDefineParameterTypes (this))
+                       if (!Parameters.ComputeAndDefineParameterTypes (ec))
                                return false;
                        
-                       param_types = Parameters.GetParameterInfo (this);
+                       param_types = Parameters.GetParameterInfo (ec);
                        if (param_types == null)
                                return false;
 
@@ -178,7 +191,7 @@ namespace Mono.CSharp {
                                        return false;
                        }
                        
-                       ReturnType = ResolveTypeExpr (ReturnType, false, Location);
+                       ReturnType = ReturnType.ResolveAsTypeTerminal (ec, false);
                         if (ReturnType == null)
                             return false;
                         
@@ -237,7 +250,7 @@ namespace Mono.CSharp {
                        InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
 
                        TypeManager.RegisterMethod (InvokeBuilder,
-                                                   new InternalParameters (Parent, Parameters),
+                                                   new InternalParameters (param_types, Parameters),
                                                    param_types);
 
                        //
@@ -300,11 +313,10 @@ namespace Mono.CSharp {
                                                                   Parameter.Modifier.NONE, null);
 
                        Parameters async_parameters = new Parameters (async_params, null, Location);
-                       async_parameters.ComputeAndDefineParameterTypes (this);
-                       
-                       async_parameters.ComputeAndDefineParameterTypes (this);
+                       async_parameters.ComputeAndDefineParameterTypes (ec);
+
                        TypeManager.RegisterMethod (BeginInvokeBuilder,
-                                                   new InternalParameters (Parent, async_parameters),
+                                                   new InternalParameters (async_param_types, async_parameters),
                                                    async_param_types);
 
                        //
@@ -345,11 +357,11 @@ namespace Mono.CSharp {
                        }
 
                        Parameters end_parameters = new Parameters (end_params, null, Location);
-                       end_parameters.ComputeAndDefineParameterTypes (this);
+                       end_parameters.ComputeAndDefineParameterTypes (ec);
 
                        TypeManager.RegisterMethod (
                                EndInvokeBuilder,
-                               new InternalParameters (Parent, end_parameters),
+                               new InternalParameters (end_param_types, end_parameters),
                                end_param_types);
 
                        return true;
@@ -358,8 +370,6 @@ namespace Mono.CSharp {
                public override void Emit ()
                {
                        if (OptAttributes != null) {
-                               EmitContext ec = new EmitContext (
-                                       Parent, this, Location, null, null, ModFlags, false);
                                Parameters.LabelParameters (ec, InvokeBuilder, Location);
                                OptAttributes.Emit (ec, this);
                        }
@@ -388,6 +398,23 @@ namespace Mono.CSharp {
                        return true;
                }
 
+               //
+               // Returns the MethodBase for "Invoke" from a delegate type, this is used
+               // to extract the signature of a delegate.
+               //
+               public static MethodInfo GetInvokeMethod (EmitContext ec, Type delegate_type, Location loc)
+               {
+                       Expression ml = Expression.MemberLookup (
+                               ec, delegate_type, "Invoke", loc);
+
+                       if (!(ml is MethodGroupExpr)) {
+                               Report.Error (-100, loc, "Internal error: could not find Invoke method!");
+                               return null;
+                       }
+
+                       return (MethodInfo) (((MethodGroupExpr) ml).Methods [0]);
+               }
+               
                /// <summary>
                ///  Verifies whether the method in question is compatible with the delegate
                ///  Returns the method itself if okay and null if not.
@@ -395,21 +422,15 @@ namespace Mono.CSharp {
                public static MethodBase VerifyMethod (EmitContext ec, Type delegate_type, MethodBase mb,
                                                       Location loc)
                {
-                       ParameterData pd = Invocation.GetParameterData (mb);
+                       ParameterData pd = TypeManager.GetParameterData (mb);
 
                        int pd_count = pd.Count;
 
-                       Expression ml = Expression.MemberLookup (
-                               ec, delegate_type, "Invoke", loc);
-
-                       if (!(ml is MethodGroupExpr)) {
-                               Report.Error (-100, loc, "Internal error: could not find Invoke method!");
+                       MethodBase invoke_mb = GetInvokeMethod (ec, delegate_type, loc);
+                       if (invoke_mb == null)
                                return null;
-                       }
-
-                       MethodBase invoke_mb = ((MethodGroupExpr) ml).Methods [0];
 
-                       ParameterData invoke_pd = Invocation.GetParameterData (invoke_mb);
+                       ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
 
                        if (invoke_pd.Count != pd_count)
                                return null;
@@ -417,18 +438,40 @@ namespace Mono.CSharp {
                        for (int i = pd_count; i > 0; ) {
                                i--;
 
-                               if (invoke_pd.ParameterType (i) == pd.ParameterType (i) &&
-                                   invoke_pd.ParameterModifier (i) == pd.ParameterModifier (i))
+                               Type invoke_pd_type = invoke_pd.ParameterType (i);
+                               Type pd_type = pd.ParameterType (i);
+                               Parameter.Modifier invoke_pd_type_mod = invoke_pd.ParameterModifier (i);
+                               Parameter.Modifier pd_type_mod = pd.ParameterModifier (i);
+
+                               if (invoke_pd_type == pd_type &&
+                                   invoke_pd_type_mod == pd_type_mod)
                                        continue;
-                               else {
-                                       return null;
-                               }
+                               
+                               if (invoke_pd_type.IsSubclassOf (pd_type) && 
+                                               invoke_pd_type_mod == pd_type_mod)
+                                       if (RootContext.Version == LanguageVersion.ISO_1) {
+                                               Report.FeatureIsNotStandardized (loc, "contravariance");
+                                               return null;
+                                       } else
+                                               continue;
+                                       
+                               return null;
                        }
 
-                       if (((MethodInfo) invoke_mb).ReturnType == ((MethodInfo) mb).ReturnType)
+                       Type invoke_mb_retval = ((MethodInfo) invoke_mb).ReturnType;
+                       Type mb_retval = ((MethodInfo) mb).ReturnType;
+                       if (invoke_mb_retval == mb_retval)
                                return mb;
-                       else
-                               return null;
+                       
+                       if (mb_retval.IsSubclassOf (invoke_mb_retval))
+                               if (RootContext.Version == LanguageVersion.ISO_1) {
+                                       Report.FeatureIsNotStandardized (loc, "covariance");
+                                       return null;
+                               }
+                               else
+                                       return mb;
+                       
+                       return null;
                }
 
                // <summary>
@@ -454,7 +497,7 @@ namespace Mono.CSharp {
                        }
                        
                        MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
-                       ParameterData pd = Invocation.GetParameterData (mb);
+                       ParameterData pd = TypeManager.GetParameterData (mb);
 
                        int pd_count = pd.Count;
 
@@ -463,8 +506,8 @@ namespace Mono.CSharp {
 
                        if (!params_method && pd_count != arg_count) {
                                Report.Error (1593, loc,
-                                             "Delegate '" + delegate_type.ToString ()
-                                             + "' does not take '" + arg_count + "' arguments");
+                                             "Delegate '{0}' does not take {1} arguments",
+                                             delegate_type.ToString (), arg_count);
                                return false;
                        }
 
@@ -510,7 +553,7 @@ namespace Mono.CSharp {
                        }
                        
                        MethodBase mb = ((MethodGroupExpr) ml).Methods [0];
-                       ParameterData pd = Invocation.GetParameterData (mb);
+                       ParameterData pd = TypeManager.GetParameterData (mb);
 
                        Expression probe_ml = Expression.MemberLookup (
                                ec, delegate_type, "Invoke", loc);
@@ -521,7 +564,7 @@ namespace Mono.CSharp {
                        }
                        
                        MethodBase probe_mb = ((MethodGroupExpr) probe_ml).Methods [0];
-                       ParameterData probe_pd = Invocation.GetParameterData (probe_mb);
+                       ParameterData probe_pd = TypeManager.GetParameterData (probe_mb);
                        
                        if (((MethodInfo) mb).ReturnType != ((MethodInfo) probe_mb).ReturnType)
                                return false;
@@ -631,6 +674,13 @@ namespace Mono.CSharp {
                        }
                }
 
+               //
+               //   Represents header string for documentation comment.
+               //
+               public override string DocCommentHeader {
+                       get { return "T:"; }
+               }
+
                protected override void VerifyObsoleteAttribute()
                {
                        CheckUsageOfObsoleteAttribute (ret_type);
@@ -665,7 +715,7 @@ namespace Mono.CSharp {
                                ec, type, "Invoke", MemberTypes.Method,
                                Expression.AllBindingFlags, loc);
                        MethodBase method = ((MethodGroupExpr) invoke_method).Methods [0];
-                       ParameterData param = Invocation.GetParameterData (method);
+                       ParameterData param = TypeManager.GetParameterData (method);
                        string delegate_desc = Delegate.FullDelegateDesc (type, method, param);
                        
                        Report.Error (123, loc, "Method '" + method_desc + "' does not " +
@@ -719,7 +769,7 @@ namespace Mono.CSharp {
                        // Check safe/unsafe of the delegate
                        //
                        if (!ec.InUnsafe){
-                               ParameterData param = Invocation.GetParameterData (delegate_method);
+                               ParameterData param = TypeManager.GetParameterData (delegate_method);
                                int count = param.Count;
                                
                                for (int i = 0; i < count; i++){
@@ -756,7 +806,7 @@ namespace Mono.CSharp {
                                delegate_instance_expression = ec.GetThis (loc);
                        
                        if (delegate_instance_expression != null && delegate_instance_expression.Type.IsValueType)
-                               delegate_instance_expression = new BoxedCast (mg.InstanceExpression);
+                               delegate_instance_expression = new BoxedCast (delegate_instance_expression);
                        
                        method_group = mg;
                        eclass = ExprClass.Value;
@@ -789,7 +839,7 @@ namespace Mono.CSharp {
                                return null;
                }
        }
-       
+
        //
        // A delegate-creation-expression, invoked from the `New' class 
        //
@@ -824,6 +874,9 @@ namespace Mono.CSharp {
                        
                        Expression e = a.Expr;
 
+                       if (e is AnonymousMethod && RootContext.Version != LanguageVersion.ISO_1)
+                               return ((AnonymousMethod) e).Compatible (ec, type, false);
+
                        MethodGroupExpr mg = e as MethodGroupExpr;
                        if (mg != null)
                                return ResolveMethodGroupExpr (ec, mg);
@@ -831,7 +884,7 @@ namespace Mono.CSharp {
                        Type e_type = e.Type;
 
                        if (!TypeManager.IsDelegateType (e_type)) {
-                               e.Error_UnexpectedKind ("method");
+                               Report.Error (149, loc, "Method name expected");
                                return null;
                        }