2005-05-31 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / mcs / delegate.cs
index 08d2fac38991a04ee199d39d46bc4fb39acdda8d..1a14bd913658f378fb7e60ea954d8aed8a47ac00 100644 (file)
@@ -81,11 +81,9 @@ namespace Mono.CSharp {
 
                        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");
+                               Namespace system = Namespace.LookupNamespace ("System", true);
+                               TypeExpr expr = system.Lookup (this, "MulticastDelegate", Location) as TypeExpr;
                                TypeManager.multicast_delegate_type = expr.ResolveType (ec);
                        }
 
@@ -99,13 +97,13 @@ namespace Mono.CSharp {
                                ModuleBuilder builder = CodeGen.Module.Builder;
 
                                TypeBuilder = builder.DefineType (
-                                       Name, attr, TypeManager.multicast_delegate_type);
+                                       Name, TypeAttr, TypeManager.multicast_delegate_type);
                        } else {
                                TypeBuilder builder = Parent.TypeBuilder;
 
                                string name = Name.Substring (1 + Name.LastIndexOf ('.'));
                                TypeBuilder = builder.DefineNestedType (
-                                       name, attr, TypeManager.multicast_delegate_type);
+                                       name, TypeAttr, TypeManager.multicast_delegate_type);
                        }
 
                        TypeManager.AddDelegateType (Name, TypeBuilder, this);
@@ -144,14 +142,12 @@ namespace Mono.CSharp {
                        //
                        // HACK because System.Reflection.Emit is lame
                        //
-                       //
-                       // FIXME: POSSIBLY make these static, as they are always the same
                        Parameter [] fixed_pars = new Parameter [2];
                        fixed_pars [0] = new Parameter (TypeManager.system_object_expr, "object",
-                                                       Parameter.Modifier.NONE, null);
+                                                       Parameter.Modifier.NONE, null, Location);
                        fixed_pars [1] = new Parameter (TypeManager.system_intptr_expr, "method", 
-                                                       Parameter.Modifier.NONE, null);
-                       Parameters const_parameters = new Parameters (fixed_pars, null, Location);
+                                                       Parameter.Modifier.NONE, null, Location);
+                       Parameters const_parameters = new Parameters (fixed_pars, null);
                        
                        TypeManager.RegisterMethod (
                                ConstructorBuilder,
@@ -167,9 +163,6 @@ namespace Mono.CSharp {
                        // First, call the `out of band' special method for
                        // defining recursively any types we need:
                        
-                       if (!Parameters.ComputeAndDefineParameterTypes (ec))
-                               return false;
-                       
                        param_types = Parameters.GetParameterInfo (ec);
                        if (param_types == null)
                                return false;
@@ -210,6 +203,11 @@ namespace Mono.CSharp {
                        if (ret_type.IsPointer && !UnsafeOK (Parent))
                                return false;
 
+                       if (RootContext.StdLib && (ret_type == TypeManager.arg_iterator_type || ret_type == TypeManager.typed_reference_type)) {
+                               Method.Error1599 (Location, ret_type);
+                               return false;
+                       }
+
                        //
                        // We don't have to check any others because they are all
                        // guaranteed to be accessible - they are standard types.
@@ -236,7 +234,7 @@ namespace Mono.CSharp {
                                
                                for (; i < top; i++) {
                                        p = Parameters.FixedParameters [i];
-                                       p.DefineParameter (ec, InvokeBuilder, null, i + 1, Location);
+                                       p.DefineParameter (ec, InvokeBuilder, null, i + 1);
 
                                        if ((p.ModFlags & Parameter.Modifier.ISBYREF) != 0)
                                                out_params++;
@@ -244,7 +242,24 @@ namespace Mono.CSharp {
                        }
                        if (Parameters.ArrayParameter != null){
                                Parameter p = Parameters.ArrayParameter;
-                               p.DefineParameter (ec, InvokeBuilder, null, i + 1, Location);
+
+                               if (TypeManager.param_array_type == null && !RootContext.StdLib) {
+                                       Namespace system = Namespace.LookupNamespace ("System", true);
+                                       TypeExpr expr = system.Lookup (this, "ParamArrayAttribute", Location) as TypeExpr;
+                                       TypeManager.param_array_type = expr.ResolveType (ec);
+                               }
+
+                               if (TypeManager.cons_param_array_attribute == null) {
+                                       Type [] void_arg = { };
+                                       TypeManager.cons_param_array_attribute = TypeManager.GetConstructor (
+                                               TypeManager.param_array_type, void_arg);
+                               }
+
+                               ParameterBuilder pb = InvokeBuilder.DefineParameter (
+                                       i + 1, Parameters.ArrayParameter.Attributes,Parameters.ArrayParameter.Name);
+                               
+                               pb.SetCustomAttribute (
+                                       new CustomAttributeBuilder (TypeManager.cons_param_array_attribute, new object [0]));
                        }
                        
                        InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
@@ -281,12 +296,12 @@ namespace Mono.CSharp {
                                for (i = 0 ; i < top; i++) {
                                        p = Parameters.FixedParameters [i];
 
-                                       p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1, Location);
+                                       p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1);
                                }
                        }
                        if (Parameters.ArrayParameter != null){
                                Parameter p = Parameters.ArrayParameter;
-                               p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1, Location);
+                               p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1);
 
                                i++;
                        }
@@ -307,16 +322,15 @@ namespace Mono.CSharp {
                        
                        async_params [params_num] = new Parameter (
                                TypeManager.system_asynccallback_expr, "callback",
-                                                                  Parameter.Modifier.NONE, null);
+                                                                  Parameter.Modifier.NONE, null, Location);
                        async_params [params_num + 1] = new Parameter (
                                TypeManager.system_object_expr, "object",
-                                                                  Parameter.Modifier.NONE, null);
+                                                                  Parameter.Modifier.NONE, null, Location);
 
-                       Parameters async_parameters = new Parameters (async_params, null, Location);
-                       async_parameters.ComputeAndDefineParameterTypes (ec);
+                       Parameters async_parameters = new Parameters (async_params, null);
 
                        TypeManager.RegisterMethod (BeginInvokeBuilder,
-                                                   new InternalParameters (async_param_types, async_parameters),
+                                                   new InternalParameters (async_parameters.GetParameterInfo (ec), async_parameters),
                                                    async_param_types);
 
                        //
@@ -340,7 +354,7 @@ namespace Mono.CSharp {
                                }
                        }
                        end_param_types [out_params] = TypeManager.iasyncresult_type;
-                       end_params [out_params] = new Parameter (TypeManager.system_iasyncresult_expr, "result", Parameter.Modifier.NONE, null);
+                       end_params [out_params] = new Parameter (TypeManager.system_iasyncresult_expr, "result", Parameter.Modifier.NONE, null, Location);
 
                        //
                        // Create method, define parameters, register parameters with type system
@@ -356,12 +370,11 @@ namespace Mono.CSharp {
                                EndInvokeBuilder.DefineParameter (i + 1, end_params [i].Attributes, end_params [i].Name);
                        }
 
-                       Parameters end_parameters = new Parameters (end_params, null, Location);
-                       end_parameters.ComputeAndDefineParameterTypes (ec);
+                       Parameters end_parameters = new Parameters (end_params, null);
 
                        TypeManager.RegisterMethod (
                                EndInvokeBuilder,
-                               new InternalParameters (end_param_types, end_parameters),
+                               new InternalParameters (end_parameters.GetParameterInfo (ec), end_parameters),
                                end_param_types);
 
                        return true;
@@ -370,13 +383,21 @@ namespace Mono.CSharp {
                public override void Emit ()
                {
                        if (OptAttributes != null) {
-                               Parameters.LabelParameters (ec, InvokeBuilder, Location);
+                               Parameters.LabelParameters (ec, InvokeBuilder);
                                OptAttributes.Emit (ec, this);
                        }
 
                        base.Emit ();
                }
 
+               protected override TypeAttributes TypeAttr {
+                       get {
+                               return Modifiers.TypeAttr (ModFlags, IsTopLevel) |
+                                       TypeAttributes.Class | TypeAttributes.Sealed |
+                                       base.TypeAttr;
+                       }
+               }
+
                public override string[] ValidAttributeTargets {
                        get {
                                return attribute_targets;
@@ -692,21 +713,27 @@ namespace Mono.CSharp {
                public static void Error_NoMatchingMethodForDelegate (EmitContext ec, MethodGroupExpr mg, Type type, Location loc)
                {
                        string method_desc;
+                       MethodInfo found_method = (MethodInfo)mg.Methods [0];
                        
                        if (mg.Methods.Length > 1)
-                               method_desc = mg.Methods [0].Name;
+                               method_desc = found_method.Name;
                        else
-                               method_desc = Invocation.FullMethodDesc (mg.Methods [0]);
+                               method_desc = Invocation.FullMethodDesc (found_method);
 
                        Expression invoke_method = Expression.MemberLookup (
                                ec, type, "Invoke", MemberTypes.Method,
                                Expression.AllBindingFlags, loc);
-                       MethodBase method = ((MethodGroupExpr) invoke_method).Methods [0];
+                       MethodInfo method = ((MethodGroupExpr) invoke_method).Methods [0] as MethodInfo;
+
                        ParameterData param = TypeManager.GetParameterData (method);
                        string delegate_desc = Delegate.FullDelegateDesc (type, method, param);
-                       
-                       Report.Error (123, loc, "Method '" + method_desc + "' does not " +
-                                     "match delegate '" + delegate_desc + "'");
+
+                       if (method.ReturnType != found_method.ReturnType) {
+                               Report.Error (407, loc, "'{0}' has the wrong return type to match delegate '{1}'", method_desc, delegate_desc);
+                       } else {
+                               Report.Error (123, loc, "Method '" + method_desc + "' does not " +
+                                       "match delegate '" + delegate_desc + "'");
+                       }
                }
                
                public override void Emit (EmitContext ec)
@@ -738,7 +765,8 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               protected Expression ResolveMethodGroupExpr (EmitContext ec, MethodGroupExpr mg)
+               protected Expression ResolveMethodGroupExpr (EmitContext ec, MethodGroupExpr mg,
+                                                            bool check_only)
                {
                        foreach (MethodInfo mi in mg.Methods){
                                delegate_method  = Delegate.VerifyMethod (ec, type, mi, loc);
@@ -748,7 +776,8 @@ namespace Mono.CSharp {
                        }
                        
                        if (delegate_method == null) {
-                               Error_NoMatchingMethodForDelegate (ec, mg, type, loc);
+                               if (!check_only)
+                                       Error_NoMatchingMethodForDelegate (ec, mg, type, loc);
                                return null;
                        }
                        
@@ -774,6 +803,7 @@ namespace Mono.CSharp {
                                        Report.Error (1618, loc, "Cannot create delegate with '{0}' because it has a Conditional attribute", TypeManager.CSharpSignature (delegate_method));
                                }
                        } else {
+                               md.SetMemberIsUsed ();
                                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));
                                }
@@ -816,12 +846,13 @@ namespace Mono.CSharp {
                {
                        return this;
                }
-               
-               static public Expression Create (EmitContext ec, MethodGroupExpr mge, Type target_type, Location loc)
+
+               static public Expression Create (EmitContext ec, MethodGroupExpr mge,
+                                                Type target_type, bool check_only, Location loc)
                {
                        ImplicitDelegateCreation d = new ImplicitDelegateCreation (target_type, loc);
                        if (d.ResolveConstructorMethod (ec))
-                               return d.ResolveMethodGroupExpr (ec, mge);
+                               return d.ResolveMethodGroupExpr (ec, mge, check_only);
                        else
                                return null;
                }
@@ -866,7 +897,7 @@ namespace Mono.CSharp {
 
                        MethodGroupExpr mg = e as MethodGroupExpr;
                        if (mg != null)
-                               return ResolveMethodGroupExpr (ec, mg);
+                               return ResolveMethodGroupExpr (ec, mg, false);
 
                        Type e_type = e.Type;