2004-03-25 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / MethodBuilder.cs
index 783343cdf0adf7e2273630be7c57732193ac0433..63b9477decb8630a3472a6c90135cdd30b5d86be 100755 (executable)
@@ -12,6 +12,8 @@ using System;
 using System.Reflection;
 using System.Reflection.Emit;
 using System.Globalization;
+using System.Security;
+using System.Security.Permissions;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 
@@ -37,13 +39,22 @@ namespace System.Reflection.Emit {
                private CallingConvention native_cc;
                private CallingConventions call_conv;
                private bool init_locals = true;
-               private TypeBuilder.MonoGenericParam[] generic_params;
-
-               internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
+               private MonoGenericParam[] generic_params;
+               private Type[] returnModReq;
+               private Type[] returnModOpt;
+               private Type[][] paramModReq;
+               private Type[][] paramModOpt;
+               private RefEmitPermissionSet[] permissions;
+
+               internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnModReq, Type[] returnModOpt, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt) {
                        this.name = name;
                        this.attrs = attributes;
                        this.call_conv = callingConvention;
                        this.rtype = returnType;
+                       this.returnModReq = returnModReq;
+                       this.returnModOpt = returnModOpt;
+                       this.paramModReq = paramModReq;
+                       this.paramModOpt = paramModOpt;
                        // The MSDN docs does not specify this, but the MS MethodBuilder
                        // appends a HasThis flag if the method is not static
                        if ((attributes & MethodAttributes.Static) == 0)
@@ -58,9 +69,9 @@ namespace System.Reflection.Emit {
                }
 
                internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes, 
-                       CallingConventions callingConvention, Type returnType, Type[] parameterTypes
+                                                               CallingConventions callingConvention, Type returnType, Type[] returnModReq, Type[] returnModOpt, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt
                        String dllName, String entryName, CallingConvention nativeCConv, CharSet nativeCharset) 
-                       : this (tb, name, attributes, callingConvention, returnType, parameterTypes) {
+                       : this (tb, name, attributes, callingConvention, returnType, returnModReq, returnModOpt, parameterTypes, paramModReq, paramModOpt) {
                        pi_dll = dllName;
                        pi_entry = entryName;
                        native_cc = nativeCConv;
@@ -95,6 +106,13 @@ namespace System.Reflection.Emit {
                        get { return call_conv; }
                }
 
+               [MonoTODO]
+               public string Signature {
+                       get {
+                               throw new NotImplementedException ();
+                       }
+               }
+
                public MethodToken GetToken() {
                        return new MethodToken(0x06000000 | table_idx);
                }
@@ -115,6 +133,14 @@ namespace System.Reflection.Emit {
                        }
                        return retval;
                }
+               
+               internal override int GetParameterCount ()
+               {
+                       if (parameters == null)
+                               return 0;
+                       
+                       return parameters.Length;
+               }
 
                public Module GetModule () {
                        return type.Module;
@@ -159,7 +185,7 @@ namespace System.Reflection.Emit {
                                throw new InvalidOperationException ("Method body should not exist.");
                        if (ilgen != null)
                                return ilgen;
-                       ilgen = new ILGenerator (this, size);
+                       ilgen = new ILGenerator (type.Module, ((ModuleBuilder)type.Module).GetTokenGenerator (), size);
                        return ilgen;
                }
                
@@ -218,6 +244,48 @@ namespace System.Reflection.Emit {
                        RejectIfCreated ();
                        iattrs = attributes;
                }
+
+               public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
+                       if (pset == null)
+                               throw new ArgumentNullException ("pset");
+                       if ((action == SecurityAction.RequestMinimum) ||
+                               (action == SecurityAction.RequestOptional) ||
+                               (action == SecurityAction.RequestRefuse))
+                               throw new ArgumentException ("Request* values are not permitted", "action");
+
+                       RejectIfCreated ();
+
+                       if (permissions != null) {
+                               /* Check duplicate actions */
+                               foreach (RefEmitPermissionSet set in permissions)
+                                       if (set.action == action)
+                                               throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
+
+                               RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
+                               permissions.CopyTo (new_array, 0);
+                               permissions = new_array;
+                       }
+                       else
+                               permissions = new RefEmitPermissionSet [1];
+
+                       permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
+                       attrs |= MethodAttributes.HasSecurity;
+               }
+
+               [MonoTODO]
+               public void SetMarshal (UnmanagedMarshal unmanagedMarshal)
+               {
+                       RejectIfCreated ();
+                       throw new NotImplementedException ();
+               }
+
+               [MonoTODO]
+               public void SetSymCustomAttribute (string name, byte[] data)
+               {
+                       RejectIfCreated ();
+                       throw new NotImplementedException ();
+               }
+
                internal override int get_next_table_index (object obj, int table, bool inc) {
                    return type.get_next_table_index (obj, table, inc);
                }
@@ -235,25 +303,30 @@ namespace System.Reflection.Emit {
                        return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
                }
 
-#if GENERICS
+#if NET_1_2
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               private extern Type define_generic_parameter (TypeBuilder.MonoGenericParam param);
+               private extern MonoGenericParam define_generic_parameter (string name, int index);
                
-               public Type DefineGenericParameter (string name, Type[] constraints)
+               public Type DefineGenericParameter (string name)
                {
-                       TypeBuilder.MonoGenericParam gparam = new TypeBuilder.MonoGenericParam (name, constraints);
-
+                       int index;
                        if (generic_params != null) {
-                               TypeBuilder.MonoGenericParam[] new_generic_params = new TypeBuilder.MonoGenericParam [generic_params.Length+1];
+                               MonoGenericParam[] new_generic_params = new MonoGenericParam [generic_params.Length+1];
                                System.Array.Copy (generic_params, new_generic_params, generic_params.Length);
-                               new_generic_params [generic_params.Length] = gparam;
+                               index = generic_params.Length;
                                generic_params = new_generic_params;
                        } else {
-                               generic_params = new TypeBuilder.MonoGenericParam [1];
-                               generic_params [0] = gparam;
+                               generic_params = new MonoGenericParam [1];
+                               index = 0;
                        }
 
-                       return define_generic_parameter (gparam);
+                       generic_params [index] = define_generic_parameter (name, index);
+                       return generic_params [index];
+               }
+
+               public void SetGenericParameterConstraints (int index, Type[] constraints)
+               {
+                       generic_params [index].SetConstraints (constraints);
                }
 
                public override Type[] GetGenericArguments ()
@@ -263,15 +336,20 @@ namespace System.Reflection.Emit {
 
                        Type[] result = new Type [generic_params.Length];
                        for (int i = 0; i < generic_params.Length; i++)
-                               result [i] = generic_params [i].Type;
+                               result [i] = generic_params [i];
 
                        return result;
                }
 
-               public void SetGenericMethodSignature (Type return_type, Type[] parameter_types)
+               public void SetGenericMethodSignature (MethodAttributes attributes, CallingConventions callingConvention, Type return_type, Type[] parameter_types)
                {
                        RejectIfCreated ();
 
+                       this.attrs = attributes;
+                       this.call_conv = callingConvention;
+                       if ((attributes & MethodAttributes.Static) == 0)
+                               this.call_conv |= CallingConventions.HasThis;
+
                        this.rtype = return_type;
                        this.parameters = new Type [parameter_types.Length];
                        System.Array.Copy (parameter_types, this.parameters, parameter_types.Length);