2008-11-06 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / mcs / delegate.cs
index 1262fb2aad6b9b4a642c80fba1f9babc4bcb80e1..7fa7f6da67398743b1ef1f724128c6aef50818fb 100644 (file)
@@ -147,11 +147,6 @@ namespace Mono.CSharp {
                                        if (!type_param.DefineType (this))
                                                return false;
                                }
-
-                               foreach (TypeParameter type_param in TypeParameters) {
-                                       if (!type_param.CheckDependencies ())
-                                               return false;
-                               }
                        }
 #endif
                        member_cache = new MemberCache (TypeManager.multicast_delegate_type, this);
@@ -174,13 +169,14 @@ namespace Mono.CSharp {
                        //
                        // HACK because System.Reflection.Emit is lame
                        //
-                       Parameter [] fixed_pars = new Parameter [2];
-                       fixed_pars [0] = new Parameter (TypeManager.object_type, "object",
-                                                       Parameter.Modifier.NONE, null, Location);
-                       fixed_pars [1] = new Parameter (TypeManager.intptr_type, "method", 
-                                                       Parameter.Modifier.NONE, null, Location);
-                       Parameters const_parameters = Parameters.CreateFullyResolved (fixed_pars,
-                               new Type[] { fixed_pars [0].ParameterType, fixed_pars [1].ParameterType});
+                       IParameterData [] fixed_pars = new IParameterData [] {
+                               new ParameterData ("object", Parameter.Modifier.NONE),
+                               new ParameterData ("method", Parameter.Modifier.NONE)
+                       };
+
+                       AParametersCollection const_parameters = new ParametersCollection (
+                               fixed_pars,
+                               new Type[] { TypeManager.object_type, TypeManager.intptr_type });
                        
                        TypeManager.RegisterMethod (ConstructorBuilder, const_parameters);
                        member_cache.AddMember (ConstructorBuilder, this);
@@ -205,9 +201,9 @@ namespace Mono.CSharp {
                                if (!IsAccessibleAs (partype)) {
                                        Report.SymbolRelatedToPreviousError (partype);
                                        Report.Error (59, Location,
-                                                     "Inconsistent accessibility: parameter type `" +
-                                                     TypeManager.CSharpName (partype) + "' is less " +
-                                                         "accessible than delegate `" + GetSignatureForError () + "'");
+                                                     "Inconsistent accessibility: parameter type `{0}' is less accessible than delegate `{1}'",
+                                                     TypeManager.CSharpName (partype),
+                                                     GetSignatureForError ());
                                        return false;
                                }
                        }
@@ -229,7 +225,7 @@ namespace Mono.CSharp {
 
                        CheckProtectedModifier ();
 
-                       if (RootContext.StdLib && (ret_type == TypeManager.arg_iterator_type || ret_type == TypeManager.typed_reference_type)) {
+                       if (RootContext.StdLib && TypeManager.IsSpecialType (ret_type)) {
                                Method.Error1599 (Location, ret_type);
                                return false;
                        }
@@ -245,7 +241,7 @@ namespace Mono.CSharp {
                                                                  mattr,                     
                                                                  cc,
                                                                  ret_type,                  
-                                                                 Parameters.Types);
+                                                                 Parameters.GetEmitTypes ());
                        
                        InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
 
@@ -265,11 +261,18 @@ namespace Mono.CSharp {
                        // BeginInvoke
                        //
                        Parameters async_parameters = Parameters.MergeGenerated (Parameters, false,
-                               new Parameter (TypeManager.asynccallback_type, "callback", Parameter.Modifier.NONE, null, Location),
-                               new Parameter (TypeManager.object_type, "object", Parameter.Modifier.NONE, null, Location));
+                               new Parameter [] {
+                                       new Parameter (null, "callback", Parameter.Modifier.NONE, null, Location),
+                                       new Parameter (null, "object", Parameter.Modifier.NONE, null, Location)
+                               },
+                               new Type [] {
+                                       TypeManager.asynccallback_type,
+                                       TypeManager.object_type
+                               }
+                       );
 
                        BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",
-                               mattr, cc, TypeManager.iasyncresult_type, async_parameters.Types);
+                               mattr, cc, TypeManager.iasyncresult_type, async_parameters.GetEmitTypes ());
 
                        BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
                        TypeManager.RegisterMethod (BeginInvokeBuilder, async_parameters);
@@ -293,15 +296,15 @@ namespace Mono.CSharp {
 
                        if (out_params > 0) {
                                Type [] end_param_types = new Type [out_params];
-                               Parameter [] end_params = new Parameter [out_params];
+                               Parameter[] end_params = new Parameter [out_params];
 
                                int param = 0;
                                for (int i = 0; i < Parameters.FixedParameters.Length; ++i) {
-                                       Parameter p = Parameters.FixedParameters [i];
+                                       Parameter p = Parameters [i];
                                        if ((p.ModFlags & Parameter.Modifier.ISBYREF) == 0)
                                                continue;
 
-                                       end_param_types [param] = p.ExternalType ();
+                                       end_param_types [param] = Parameters.Types [i];
                                        end_params [param] = p;
                                        ++param;
                                }
@@ -311,12 +314,12 @@ namespace Mono.CSharp {
                        }
 
                        end_parameters = Parameters.MergeGenerated (end_parameters, false,
-                               new Parameter (TypeManager.iasyncresult_type, "result", Parameter.Modifier.NONE, null, Location));
+                               new Parameter (null, "result", Parameter.Modifier.NONE, null, Location), TypeManager.iasyncresult_type);
 
                        //
                        // Create method, define parameters, register parameters with type system
                        //
-                       EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke", mattr, cc, ret_type, end_parameters.Types);
+                       EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke", mattr, cc, ret_type, end_parameters.GetEmitTypes ());
                        EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
 
                        end_parameters.ApplyAttributes (EndInvokeBuilder);
@@ -477,7 +480,7 @@ namespace Mono.CSharp {
                        if (invoke_mb == null)
                                return null;
 
-                       ParameterData invoke_pd = TypeManager.GetParameterData (invoke_mb);
+                       AParametersCollection invoke_pd = TypeManager.GetParameterData (invoke_mb);
 
 #if GMCS_SOURCE
                        if (old_mg.type_arguments == null &&
@@ -485,7 +488,7 @@ namespace Mono.CSharp {
                                return null;
 #endif
 
-                       ParameterData pd = TypeManager.GetParameterData (mb);
+                       AParametersCollection pd = TypeManager.GetParameterData (mb);
 
                        if (invoke_pd.Count != pd.Count)
                                return null;
@@ -493,10 +496,10 @@ namespace Mono.CSharp {
                        for (int i = pd.Count; i > 0; ) {
                                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);
+                               Type invoke_pd_type = invoke_pd.Types [i];
+                               Type pd_type = pd.Types [i];
+                               Parameter.Modifier invoke_pd_type_mod = invoke_pd.FixedParameters [i].ModFlags;
+                               Parameter.Modifier pd_type_mod = pd.FixedParameters [i].ModFlags;
 
                                invoke_pd_type_mod &= ~Parameter.Modifier.PARAMS;
                                pd_type_mod &= ~Parameter.Modifier.PARAMS;
@@ -552,7 +555,7 @@ namespace Mono.CSharp {
                        }
                        
                        MethodBase mb = GetInvokeMethod (ec.ContainerType, delegate_type);
-                       ParameterData pd = TypeManager.GetParameterData (mb);
+                       AParametersCollection pd = TypeManager.GetParameterData (mb);
 
                        int pd_count = pd.Count;
 
@@ -665,26 +668,26 @@ namespace Mono.CSharp {
 
                public static ArrayList CreateDelegateMethodArguments (MethodInfo invoke_method, Location loc)
                {
-                       ParameterData pd = TypeManager.GetParameterData (invoke_method);
+                       AParametersCollection pd = TypeManager.GetParameterData (invoke_method);
                        ArrayList delegate_arguments = new ArrayList (pd.Count);
                        for (int i = 0; i < pd.Count; ++i) {
                                Argument.AType atype_modifier;
                                Type atype = pd.Types [i];
-                               switch (pd.ParameterModifier (i)) {
-                                       case Parameter.Modifier.REF:
-                                               atype_modifier = Argument.AType.Ref;
-                                               atype = atype.GetElementType ();
-                                               break;
-                                       case Parameter.Modifier.OUT:
-                                               atype_modifier = Argument.AType.Out;
-                                               atype = atype.GetElementType ();
-                                               break;
-                                       case Parameter.Modifier.ARGLIST:
-                                               // __arglist is not valid
-                                               throw new InternalErrorException ("__arglist modifier");
-                                       default:
-                                               atype_modifier = Argument.AType.Expression;
-                                               break;
+                               switch (pd.FixedParameters [i].ModFlags) {
+                               case Parameter.Modifier.REF:
+                                       atype_modifier = Argument.AType.Ref;
+                                       //atype = atype.GetElementType ();
+                                       break;
+                               case Parameter.Modifier.OUT:
+                                       atype_modifier = Argument.AType.Out;
+                                       //atype = atype.GetElementType ();
+                                       break;
+                               case Parameter.Modifier.ARGLIST:
+                                       // __arglist is not valid
+                                       throw new InternalErrorException ("__arglist modifier");
+                               default:
+                                       atype_modifier = Argument.AType.Expression;
+                                       break;
                                }
                                delegate_arguments.Add (new Argument (new TypeExpression (atype, loc), atype_modifier));
                        }
@@ -724,6 +727,13 @@ namespace Mono.CSharp {
                                return null;
 
                        delegate_method = (MethodInfo) method_group;
+                       
+                       if (TypeManager.IsNullableType (delegate_method.DeclaringType)) {
+                               Report.Error (1728, loc, "Cannot create delegate from method `{0}' because it is a member of System.Nullable<T> type",
+                                       TypeManager.GetFullNameSignature (delegate_method));
+                               return null;
+                       }               
+                       
                        Invocation.IsSpecialMethodInvocation (delegate_method, loc);
 
                        ExtensionMethodGroupExpr emg = method_group as ExtensionMethodGroupExpr;
@@ -754,11 +764,6 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       if (TypeManager.IsNullableType (delegate_method.DeclaringType)) {
-                               Report.Error (1728, loc, "Cannot create delegate from method `{0}' because it is a member of System.Nullable<T> type",
-                                       TypeManager.GetFullNameSignature (delegate_method));
-                       }
-
                        DoResolveInstanceExpression (ec);
                        eclass = ExprClass.Value;
                        return this;
@@ -772,8 +777,9 @@ namespace Mono.CSharp {
                        if (delegate_instance_expression != null)
                                return;
 
-                       if (method_group.InstanceExpression != null) {
-                               delegate_instance_expression = method_group.InstanceExpression;
+                       Expression instance = method_group.InstanceExpression;
+                       if (instance != null && instance != EmptyExpression.Null) {
+                               delegate_instance_expression = instance;
                                Type instance_type = delegate_instance_expression.Type;
                                if (TypeManager.IsValueType (instance_type) || TypeManager.IsGenericParameter (instance_type)) {
                                        delegate_instance_expression = new BoxedCast (
@@ -793,9 +799,11 @@ namespace Mono.CSharp {
 
                        if (!delegate_method.DeclaringType.IsSealed && delegate_method.IsVirtual && !method_group.IsBase) {
                                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.Ldvirtftn, delegate_method);
+                       } else {
+                               ec.ig.Emit (OpCodes.Ldftn, delegate_method);
+                       }
+
                        ec.ig.Emit (OpCodes.Newobj, constructor_method);
                }
 
@@ -900,7 +908,7 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       if (Arguments == null) {
+                       if (Arguments == null || Arguments.Count != 1) {
                                Error_InvalidDelegateArgument ();
                                return null;
                        }
@@ -923,7 +931,7 @@ namespace Mono.CSharp {
                        method_group = e as MethodGroupExpr;
                        if (method_group == null) {
                                if (!TypeManager.IsDelegateType (e.Type)) {
-                                       Report.Error (149, loc, "Method name expected");
+                                       e.Error_UnexpectedKind (ResolveFlags.MethodGroup | ResolveFlags.Type, loc);
                                        return null;
                                }