2006-03-04 Marek Safar <marek.safar@seznam.cz>
[mono.git] / mcs / mcs / delegate.cs
index a869c6380c9fde7a403fe5b1563f00438561daed..e242626a16fa729a046668e1968dbbea52a1f9bc 100644 (file)
@@ -31,7 +31,6 @@ namespace Mono.CSharp {
                public MethodBuilder      BeginInvokeBuilder;
                public MethodBuilder      EndInvokeBuilder;
                
-               Type [] param_types;
                Type ret_type;
 
                static string[] attribute_targets = new string [] { "type", "return" };
@@ -79,12 +78,10 @@ namespace Mono.CSharp {
                        if (TypeBuilder != null)
                                return TypeBuilder;
 
-                       ec = new EmitContext (this, this, Location, null, null, ModFlags, false);
-
                        if (TypeManager.multicast_delegate_type == null && !RootContext.StdLib) {
-                               Namespace system = Namespace.LookupNamespace ("System", true);
+                               Namespace system = RootNamespace.Global.GetNamespace ("System", true);
                                TypeExpr expr = system.Lookup (this, "MulticastDelegate", Location) as TypeExpr;
-                               TypeManager.multicast_delegate_type = expr.ResolveType (ec);
+                               TypeManager.multicast_delegate_type = expr.Type;
                        }
 
                        if (TypeManager.multicast_delegate_type == null)
@@ -106,29 +103,23 @@ namespace Mono.CSharp {
                                        name, TypeAttr, TypeManager.multicast_delegate_type);
                        }
 
-                       TypeManager.AddUserType (Name, this);
+                       TypeManager.AddUserType (this);
 
                        return TypeBuilder;
                }
 
                public override bool Define ()
                {
-                       MethodAttributes mattr;
-                       int i;
-
-                       if (ec == null)
-                               throw new InternalErrorException ("Define called before DefineType?");
-
                        // FIXME: POSSIBLY make this static, as it is always constant
                        //
                        Type [] const_arg_types = new Type [2];
                        const_arg_types [0] = TypeManager.object_type;
                        const_arg_types [1] = TypeManager.intptr_type;
 
-                       mattr = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName |
+                       const MethodAttributes ctor_mattr = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName |
                                MethodAttributes.HideBySig | MethodAttributes.Public;
 
-                       ConstructorBuilder = TypeBuilder.DefineConstructor (mattr,
+                       ConstructorBuilder = TypeBuilder.DefineConstructor (ctor_mattr,
                                                                            CallingConventions.Standard,
                                                                            const_arg_types);
 
@@ -138,16 +129,14 @@ namespace Mono.CSharp {
                        // HACK because System.Reflection.Emit is lame
                        //
                        Parameter [] fixed_pars = new Parameter [2];
-                       fixed_pars [0] = new Parameter (TypeManager.system_object_expr, "object",
+                       fixed_pars [0] = new Parameter (TypeManager.object_type, "object",
                                                        Parameter.Modifier.NONE, null, Location);
-                       fixed_pars [1] = new Parameter (TypeManager.system_intptr_expr, "method", 
+                       fixed_pars [1] = new Parameter (TypeManager.intptr_type, "method", 
                                                        Parameter.Modifier.NONE, null, Location);
-                       Parameters const_parameters = new Parameters (fixed_pars, null);
+                       Parameters const_parameters = new Parameters (fixed_pars);
+                       const_parameters.Resolve (null);
                        
-                       TypeManager.RegisterMethod (
-                               ConstructorBuilder,
-                               new InternalParameters (const_arg_types, const_parameters),
-                               const_arg_types);
+                       TypeManager.RegisterMethod (ConstructorBuilder, const_parameters);
                                
                        
                        ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
@@ -158,8 +147,7 @@ namespace Mono.CSharp {
                        // First, call the `out of band' special method for
                        // defining recursively any types we need:
                        
-                       param_types = Parameters.GetParameterInfo (ec);
-                       if (param_types == null)
+                       if (!Parameters.Resolve (this))
                                return false;
 
                        //
@@ -167,7 +155,7 @@ namespace Mono.CSharp {
                        //
 
                        // Check accessibility
-                       foreach (Type partype in param_types){
+                       foreach (Type partype in Parameters.Types){
                                if (!Parent.AsAccessible (partype, ModFlags)) {
                                        Report.Error (59, Location,
                                                      "Inconsistent accessibility: parameter type `" +
@@ -175,18 +163,14 @@ namespace Mono.CSharp {
                                                      "accessible than delegate `" + Name + "'");
                                        return false;
                                }
-                               if (partype.IsPointer && !UnsafeOK (Parent))
-                                       return false;
                        }
                        
-                       ReturnType = ReturnType.ResolveAsTypeTerminal (ec, false);
-                        if (ReturnType == null)
-                            return false;
-                        
-                       ret_type = ReturnType.Type;
-                       if (ret_type == null)
+                       ReturnType = ReturnType.ResolveAsTypeTerminal (this, false);
+                       if (ReturnType == null)
                                return false;
 
+                       ret_type = ReturnType.Type;
+            
                        if (!Parent.AsAccessible (ret_type, ModFlags)) {
                                Report.Error (58, Location,
                                              "Inconsistent accessibility: return type `" +
@@ -195,9 +179,6 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       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;
@@ -208,176 +189,92 @@ namespace Mono.CSharp {
                        // guaranteed to be accessible - they are standard types.
                        //
                        
-                       CallingConventions cc = Parameters.GetCallingConvention ();
+                       CallingConventions cc = Parameters.CallingConvention;
 
-                       mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual;
+                       const MethodAttributes mattr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.NewSlot;
 
                        InvokeBuilder = TypeBuilder.DefineMethod ("Invoke", 
                                                                  mattr,                     
                                                                  cc,
                                                                  ret_type,                  
-                                                                 param_types);
-
-                       //
-                       // Define parameters, and count out/ref parameters
-                       //
-                       int out_params = 0;
-                       i = 0;
-                       if (Parameters.FixedParameters != null){
-                               int top = Parameters.FixedParameters.Length;
-                               Parameter p;
-                               
-                               for (; i < top; i++) {
-                                       p = Parameters.FixedParameters [i];
-                                       p.DefineParameter (ec, InvokeBuilder, null, i + 1);
-
-                                       if ((p.ModFlags & Parameter.Modifier.ISBYREF) != 0)
-                                               out_params++;
-                               }
-                       }
-                       if (Parameters.ArrayParameter != null){
-                               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]));
-                       }
+                                                                 Parameters.Types);
                        
                        InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
 
-                       TypeManager.RegisterMethod (InvokeBuilder,
-                                                   new InternalParameters (param_types, Parameters),
-                                                   param_types);
+                       TypeManager.RegisterMethod (InvokeBuilder, Parameters);
 
                        //
                        // BeginInvoke
                        //
-                       int params_num = param_types.Length;
-                       Type [] async_param_types = new Type [params_num + 2];
-
-                       param_types.CopyTo (async_param_types, 0);
-
-                       async_param_types [params_num] = TypeManager.asynccallback_type;
-                       async_param_types [params_num + 1] = TypeManager.object_type;
-
-                       mattr = MethodAttributes.Public | MethodAttributes.HideBySig |
-                               MethodAttributes.Virtual | MethodAttributes.NewSlot;
+                       
+                       Parameters async_parameters = Parameters.MergeGenerated (Parameters, 
+                               new Parameter (TypeManager.asynccallback_type, "callback", Parameter.Modifier.NONE, null, Location),
+                               new Parameter (TypeManager.object_type, "object", Parameter.Modifier.NONE, null, Location));
                        
                        BeginInvokeBuilder = TypeBuilder.DefineMethod ("BeginInvoke",
-                                                                      mattr,
-                                                                      cc,
-                                                                      TypeManager.iasyncresult_type,
-                                                                      async_param_types);
-
-                       i = 0;
-                       if (Parameters.FixedParameters != null){
-                               int top = Parameters.FixedParameters.Length;
-                               Parameter p;
-                               
-                               for (i = 0 ; i < top; i++) {
-                                       p = Parameters.FixedParameters [i];
-
-                                       p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1);
-                               }
-                       }
-                       if (Parameters.ArrayParameter != null){
-                               Parameter p = Parameters.ArrayParameter;
-                               p.DefineParameter (ec, BeginInvokeBuilder, null, i + 1);
+                               mattr, cc, TypeManager.iasyncresult_type, async_parameters.Types);
 
-                               i++;
-                       }
-
-                       BeginInvokeBuilder.DefineParameter (i + 1, ParameterAttributes.None, "callback");
-                       BeginInvokeBuilder.DefineParameter (i + 2, ParameterAttributes.None, "object");
-                       
                        BeginInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
-
-                       Parameter [] async_params = new Parameter [params_num + 2];
-                       int n = 0;
-                       if (Parameters.FixedParameters != null){
-                               Parameters.FixedParameters.CopyTo (async_params, 0);
-                               n = Parameters.FixedParameters.Length;
-                       }
-                       if (Parameters.ArrayParameter != null)
-                               async_params [n] = Parameters.ArrayParameter;
-                       
-                       async_params [params_num] = new Parameter (
-                               TypeManager.system_asynccallback_expr, "callback",
-                                                                  Parameter.Modifier.NONE, null, Location);
-                       async_params [params_num + 1] = new Parameter (
-                               TypeManager.system_object_expr, "object",
-                                                                  Parameter.Modifier.NONE, null, Location);
-
-                       Parameters async_parameters = new Parameters (async_params, null);
-
-                       TypeManager.RegisterMethod (BeginInvokeBuilder,
-                                                   new InternalParameters (async_parameters.GetParameterInfo (ec), async_parameters),
-                                                   async_param_types);
+                       async_parameters.ApplyAttributes (BeginInvokeBuilder);
+                       TypeManager.RegisterMethod (BeginInvokeBuilder, async_parameters);
 
                        //
                        // EndInvoke is a bit more interesting, all the parameters labeled as
                        // out or ref have to be duplicated here.
                        //
-                       
-                       Type [] end_param_types = new Type [out_params + 1];
-                       Parameter [] end_params = new Parameter [out_params + 1];
-                       int param = 0; 
-                       if (out_params > 0){
-                               int top = Parameters.FixedParameters.Length;
-                               for (i = 0; i < top; i++){
+
+                       //
+                       // Define parameters, and count out/ref parameters
+                       //
+                       Parameters end_parameters;
+                       int out_params = 0;
+
+                       foreach (Parameter p in Parameters.FixedParameters) {
+                               if ((p.ModFlags & Parameter.Modifier.ISBYREF) != 0)
+                                       ++out_params;
+                       }
+
+                       if (out_params > 0) {
+                               Type [] end_param_types = new Type [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];
                                        if ((p.ModFlags & Parameter.Modifier.ISBYREF) == 0)
                                                continue;
 
-                                       end_param_types [param] = param_types [i];
+                                       end_param_types [param] = p.ExternalType();
                                        end_params [param] = p;
-                                       param++;
+                                       ++param;
                                }
+                               end_parameters = new Parameters (end_params, end_param_types);
+                       }
+                       else {
+                               end_parameters = Parameters.EmptyReadOnlyParameters;
                        }
-                       end_param_types [out_params] = TypeManager.iasyncresult_type;
-                       end_params [out_params] = new Parameter (TypeManager.system_iasyncresult_expr, "result", Parameter.Modifier.NONE, null, Location);
 
+                       end_parameters = Parameters.MergeGenerated (end_parameters,
+                               new Parameter (TypeManager.iasyncresult_type, "result", Parameter.Modifier.NONE, null, Location));
+                       
                        //
                        // Create method, define parameters, register parameters with type system
                        //
-                       EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke", mattr, cc, ret_type, end_param_types);
+                       EndInvokeBuilder = TypeBuilder.DefineMethod ("EndInvoke", mattr, cc, ret_type, end_parameters.Types);
                        EndInvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);
 
-                       //
-                       // EndInvoke: Label the parameters
-                       //
-                       EndInvokeBuilder.DefineParameter (out_params + 1, ParameterAttributes.None, "result");
-                       for (i = 0; i < end_params.Length-1; i++){
-                               EndInvokeBuilder.DefineParameter (i + 1, end_params [i].Attributes, end_params [i].Name);
-                       }
-
-                       Parameters end_parameters = new Parameters (end_params, null);
-
-                       TypeManager.RegisterMethod (
-                               EndInvokeBuilder,
-                               new InternalParameters (end_parameters.GetParameterInfo (ec), end_parameters),
-                               end_param_types);
+                       end_parameters.ApplyAttributes (EndInvokeBuilder);
+                       TypeManager.RegisterMethod (EndInvokeBuilder, end_parameters);
 
                        return true;
                }
 
                public override void Emit ()
                {
+                       Parameters.ApplyAttributes (InvokeBuilder);
+
                        if (OptAttributes != null) {
-                               Parameters.LabelParameters (ec, InvokeBuilder);
-                               OptAttributes.Emit (ec, this);
+                               OptAttributes.Emit ();
                        }
 
                        base.Emit ();
@@ -398,13 +295,13 @@ namespace Mono.CSharp {
                }
 
                //TODO: duplicate
-               protected override bool VerifyClsCompliance (DeclSpace ds)
+               protected override bool VerifyClsCompliance ()
                {
-                       if (!base.VerifyClsCompliance (ds)) {
+                       if (!base.VerifyClsCompliance ()) {
                                return false;
                        }
 
-                       AttributeTester.AreParametersCompliant (Parameters.FixedParameters, Location);
+                       Parameters.VerifyClsCompliance ();
 
                        if (!AttributeTester.IsClsCompliant (ReturnType.Type)) {
                                Report.Error (3002, Location, "Return type of `{0}' is not CLS-compliant", GetSignatureForError ());
@@ -516,19 +413,17 @@ namespace Mono.CSharp {
 
                        int pd_count = pd.Count;
 
-                       bool params_method = (pd_count != 0) &&
-                               (pd.ParameterModifier (pd_count - 1) == Parameter.Modifier.PARAMS);
-
+                       bool params_method = pd.HasParams;
                        bool is_params_applicable = false;
-                       bool is_applicable = Invocation.IsApplicable (ec, me, args, arg_count, ref mb);
+                       bool is_applicable = Invocation.IsApplicable (ec, args, arg_count, mb);
 
                        if (!is_applicable && params_method &&
-                           Invocation.IsParamsMethodApplicable (ec, me, args, arg_count, ref mb))
+                           Invocation.IsParamsMethodApplicable (ec, args, arg_count, mb))
                                is_applicable = is_params_applicable = true;
 
                        if (!is_applicable && !params_method && arg_count != pd_count) {
                                Report.Error (1593, loc, "Delegate `{0}' does not take `{1}' arguments",
-                                       TypeManager.CSharpName (delegate_type), arg_count);
+                                       TypeManager.CSharpName (delegate_type), arg_count.ToString ());
                                return false;
                        }
 
@@ -650,12 +545,6 @@ namespace Mono.CSharp {
                        }
                }
 
-               public Type [] ParameterTypes {
-                       get {
-                               return param_types;
-                       }
-               }
-
                public override AttributeTargets AttributeTargets {
                        get {
                                return AttributeTargets.Delegate;
@@ -669,14 +558,6 @@ namespace Mono.CSharp {
                        get { return "T:"; }
                }
 
-               protected override void VerifyObsoleteAttribute()
-               {
-                       CheckUsageOfObsoleteAttribute (ret_type);
-
-                       foreach (Type type in param_types) {
-                               CheckUsageOfObsoleteAttribute (type);
-                       }
-               }
        }
 
        //
@@ -701,7 +582,7 @@ namespace Mono.CSharp {
                                method_desc = Invocation.FullMethodDesc (found_method);
 
                        Expression invoke_method = Expression.MemberLookup (
-                               ec, type, "Invoke", MemberTypes.Method,
+                               ec.ContainerType, type, "Invoke", MemberTypes.Method,
                                Expression.AllBindingFlags, loc);
                        MethodInfo method = ((MethodGroupExpr) invoke_method).Methods [0] as MethodInfo;
 
@@ -779,12 +660,16 @@ namespace Mono.CSharp {
                        IMethodData md = TypeManager.GetMethod (delegate_method);
                        if (md == null) {
                                if (System.Attribute.GetCustomAttribute (delegate_method, TypeManager.conditional_attribute_type) != null) {
+                                       Report.SymbolRelatedToPreviousError (delegate_method);
                                        Report.Error (1618, loc, "Cannot create delegate with `{0}' because it has a Conditional attribute", TypeManager.CSharpSignature (delegate_method));
+                                       return null;
                                }
                        } else {
                                md.SetMemberIsUsed ();
-                               if (md.OptAttributes != null && md.OptAttributes.Search (TypeManager.conditional_attribute_type, ec) != null) {
+                               if (md.OptAttributes != null && md.OptAttributes.Search (TypeManager.conditional_attribute_type) != null) {
+                                       Report.SymbolRelatedToPreviousError (delegate_method);
                                        Report.Error (1618, loc, "Cannot create delegate with `{0}' because it has a Conditional attribute", TypeManager.CSharpSignature (delegate_method));
+                                       return null;
                                }
                        }
                        
@@ -886,7 +771,7 @@ namespace Mono.CSharp {
                        }
 
                        method_group = Expression.MemberLookup (
-                               ec, type, "Invoke", MemberTypes.Method,
+                               ec.ContainerType, type, "Invoke", MemberTypes.Method,
                                Expression.AllBindingFlags, loc) as MethodGroupExpr;
 
                        if (method_group == null) {
@@ -931,7 +816,7 @@ namespace Mono.CSharp {
                                EventInfo ei = ((EventExpr) InstanceExpr).EventInfo;
                                
                                Expression ml = MemberLookup (
-                                       ec, ec.ContainerType, ei.Name,
+                                       ec.ContainerType, ec.ContainerType, ei.Name,
                                        MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
 
                                if (ml == null) {