Merge pull request #495 from nicolas-raoul/fix-for-issue2907-with-no-formatting-changes
[mono.git] / mcs / class / corlib / System.Reflection.Emit / ParameterBuilder.cs
old mode 100755 (executable)
new mode 100644 (file)
index b776548..97f9980
@@ -32,6 +32,7 @@
 // (C) 2001 Ximian, Inc.  http://www.ximian.com
 //
 
+#if !FULL_AOT_RUNTIME
 using System;
 using System.Reflection;
 using System.Reflection.Emit;
@@ -40,8 +41,14 @@ using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 
 namespace System.Reflection.Emit {
-       public class ParameterBuilder {
-               private MethodBase methodb; /* MethodBuilder or ConstructorBuilder */
+       [ComVisible (true)]
+       [ComDefaultInterface (typeof (_ParameterBuilder))]
+       [ClassInterface (ClassInterfaceType.None)]
+       [StructLayout (LayoutKind.Sequential)]
+       public class ParameterBuilder : _ParameterBuilder {
+
+#pragma warning disable 169, 414
+               private MethodBase methodb; /* MethodBuilder, ConstructorBuilder or DynamicMethod */
                private string name;
                private CustomAttributeBuilder[] cattrs;
                private UnmanagedMarshal marshal_info;
@@ -49,13 +56,17 @@ namespace System.Reflection.Emit {
                private int position;
                private int table_idx;
                object def_value;
+#pragma warning restore 169, 414
                
                internal ParameterBuilder (MethodBase mb, int pos, ParameterAttributes attributes, string strParamName) {
                        name = strParamName;
                        position = pos;
                        attrs = attributes;
                        methodb = mb;
-                       table_idx = mb.get_next_table_index (this, 0x08, true);
+                       if (mb is DynamicMethod)
+                               table_idx = 0;
+                       else
+                               table_idx = mb.get_next_table_index (this, 0x08, true);
                }
 
                public virtual int Attributes {
@@ -83,6 +94,16 @@ namespace System.Reflection.Emit {
 
                public virtual void SetConstant (object defaultValue)
                {
+                       if (position > 0) {
+                               Type t = methodb.GetParameterType (position - 1);
+                               if (defaultValue != null && t != defaultValue.GetType ()) {
+                                       if(!t.IsEnum || t.UnderlyingSystemType != defaultValue.GetType ())
+                                               throw new ArgumentException ("Constant does not match the defined type.");
+                               }
+                               if (t.IsValueType && !t.IsPrimitive && !t.IsEnum && t != typeof (DateTime))
+                                       throw new ArgumentException ("" + t + " is not a supported constant type.");
+                       }
+
                        def_value = defaultValue;
                        attrs |= ParameterAttributes.HasDefault;
                }
@@ -99,10 +120,18 @@ namespace System.Reflection.Emit {
                                attrs |= ParameterAttributes.Optional;
                                return;
                        } else if (attrname == "System.Runtime.InteropServices.MarshalAsAttribute") {
-                               marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, true);
+                               attrs |= ParameterAttributes.HasFieldMarshal;
+                               marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, false);
                                /* FIXME: check for errors */
                                return;
+                       } else if (attrname == "System.Runtime.InteropServices.DefaultParameterValueAttribute") {
+                               /* MS.NET doesn't handle this attribute but we handle it for consistency */
+                               CustomAttributeBuilder.CustomAttributeInfo cinfo = CustomAttributeBuilder.decode_cattr (customBuilder);
+                               /* FIXME: check for type compatibility */
+                               SetConstant (cinfo.ctorArgs [0]);
+                               return;
                        }
+
                        if (cattrs != null) {
                                CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
                                cattrs.CopyTo (new_array, 0);
@@ -113,15 +142,38 @@ namespace System.Reflection.Emit {
                                cattrs [0] = customBuilder;
                        }
                }
+
+               [ComVisible (true)]
                public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
                        SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
                }
 
+               [Obsolete ("An alternate API is available: Emit the MarshalAs custom attribute instead.")]
                public virtual void SetMarshal( UnmanagedMarshal unmanagedMarshal) {
                        marshal_info = unmanagedMarshal;
                        attrs |= ParameterAttributes.HasFieldMarshal;
                }
 
+                void _ParameterBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
+                {
+                        throw new NotImplementedException ();
+                }
+
+                void _ParameterBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
+                {
+                        throw new NotImplementedException ();
+                }
+
+                void _ParameterBuilder.GetTypeInfoCount (out uint pcTInfo)
+                {
+                        throw new NotImplementedException ();
+                }
+
+                void _ParameterBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
+                {
+                        throw new NotImplementedException ();
+                }
        }
 }
 
+#endif