Merge pull request #4033 from ntherning/no-stdcall-for-icalls-on-windows-32-bit
[mono.git] / mcs / class / PEAPI / Metadata.cs
index b2bc01dbd606b888ae23d0e394284717b93d2193..d9b17da6a81524be9855bd2ec80234b626f3bc3e 100644 (file)
@@ -94,6 +94,11 @@ namespace PEAPI {
                ForwardRef = 0x10, PreserveSig = 0x0080, InternalCall = 0x1000, 
                Synchronised = 0x0020, Synchronized = 0x0020, NoInLining = 0x0008, Optil = 0x0002}
 
+       /// <summary>
+       /// Storage location for initial field data
+       /// </summary>
+       public enum DataSegment { Data, TLS, CIL }
+
        /// <summary>
        /// Modes for a parameter
        /// </summary>
@@ -443,6 +448,60 @@ namespace PEAPI {
                        type = constrType;
                        cVal = val;
                        tabIx = MDTable.CustomAttribute;
+
+                       byteVal = ConstantToByteArray (val);
+               }
+
+               static byte[] ConstantToByteArray (Constant c)
+               {
+                       var bac = c as ByteArrConst;
+                       if (bac != null)
+                               return bac.val;
+
+                       var ms = new MemoryStream ();
+                       // Version info
+                       ms.WriteByte (1);
+                       ms.WriteByte (0);
+
+                       if (c == null) {
+                               ms.WriteByte (0);
+                               ms.WriteByte (0);
+                               return ms.ToArray ();
+                       }
+
+                       var sc = c as StringConst;
+                       if (sc != null) {
+                               string value = sc.val;
+                               if (value == null)
+                                       throw new NotImplementedException ();
+
+                               var buf = Encoding.UTF8.GetBytes (value);
+                               MetaData.CompressNum ((uint) buf.Length, ms);
+                               var byteVal = ms.ToArray ();
+                               System.Array.Resize (ref byteVal, (int) ms.Length + buf.Length + 2);
+                               System.Array.Copy (buf, 0, byteVal, ms.Length, buf.Length);
+                               return byteVal;
+                       }
+
+                       var ac = c as ArrayConstant;
+                       if (ac != null) {
+                               var bw = new BinaryWriter (ms);
+                               if (ac.ExplicitSize != null)
+                                       bw.Write (ac.ExplicitSize.Value);
+                               ac.Write (bw);
+                               bw.Write ((short)0);
+                               return ms.ToArray ();
+                       }
+
+                       var bc = c as DataConstant;
+                       if (bc != null) {
+                               var bw = new BinaryWriter (ms);
+                               bc.Write (bw);
+                               bw.Write ((short)0);
+                               return ms.ToArray ();
+                       }
+
+                       throw new NotImplementedException (c.GetType ().ToString ());
                }
 
                internal CustomAttribute(MetaDataElement paren, Method constrType,
@@ -2912,7 +2971,7 @@ namespace PEAPI {
        /// <summary>
        /// Boolean constant
        /// </summary>
-       public class BoolConst : Constant {
+       public class BoolConst : DataConstant {
                bool val;
 
                /// <summary>
@@ -2945,7 +3004,7 @@ namespace PEAPI {
        }
 
        public class ByteArrConst : DataConstant {
-               byte[] val;
+               internal byte[] val;
 
                public ByteArrConst(byte[] val) 
                {
@@ -3110,7 +3169,7 @@ namespace PEAPI {
 
        }
 
-       public class UIntConst : Constant {
+       public class UIntConst : DataConstant {
                ulong val;
 
                public UIntConst(byte val) 
@@ -3165,7 +3224,7 @@ namespace PEAPI {
        }
 
        public class StringConst : DataConstant {
-               string val;
+               internal string val;
 
                public StringConst(string val) 
                {
@@ -3266,6 +3325,8 @@ namespace PEAPI {
                        }
                }
 
+               public int? ExplicitSize { get; set; }
+
                internal sealed override void Write(BinaryWriter bw) 
                {
                        for (int i=0; i < dataVals.Length; i++) {
@@ -3310,6 +3371,7 @@ namespace PEAPI {
 
                Type type;
                Class cmodType;
+               PrimitiveTypeRef cmodPrimType;
 
                /// <summary>
                /// Create a new custom modifier for a type
@@ -3324,10 +3386,23 @@ namespace PEAPI {
                        this.cmodType = cmodType;
                }
 
+               public CustomModifiedType(Type type, CustomModifier cmod, PrimitiveTypeRef cmodType)
+                       : base((byte)cmod)
+               {
+                       this.type = type;
+                       this.cmodPrimType = cmodType;
+               }
+
                internal sealed override void TypeSig(MemoryStream str) 
                {
                        str.WriteByte(typeIndex);
-                       MetaData.CompressNum(cmodType.TypeDefOrRefToken(),str);
+
+                       if (cmodType != null) {
+                               MetaData.CompressNum(cmodType.TypeDefOrRefToken(),str);
+                       } else {
+                               MetaData.CompressNum(cmodPrimType.TypeDefOrRefToken(),str);
+                       }
+
                        type.TypeSig(str);
                }
 
@@ -4466,6 +4541,26 @@ namespace PEAPI {
 
        }
 
+       public class PrimitiveTypeRef : Type
+       {
+               PrimitiveType type;
+               MetaData metaData;
+
+               internal PrimitiveTypeRef(PrimitiveType type, MetaData md)
+                       : base (0)
+               {
+                       this.type = type;
+                       this.metaData = md;
+               }
+
+               internal uint TypeDefOrRefToken()
+               {
+                       uint cIx = type.GetTypeSpec (metaData).Row;
+                       cIx = (cIx << 2) | 0x2;
+                       return cIx;
+               }
+       }
+
        /**************************************************************************/  
        /// <summary>
        /// Descriptor for an pointer (type * or type &)
@@ -4927,13 +5022,7 @@ namespace PEAPI {
                private static readonly uint max3BitSmlIx = 0x1FFF;
                private static readonly uint max5BitSmlIx = 0x7FF;
                // NOTE: version and stream name strings MUST always be quad padded
-#if NET_4_0
                private static readonly string version = "v4.0.30319\0\0";
-#elif NET_2_0
-               private static readonly string version = "v2.0.50727\0\0";
-#else
-               private static readonly string version = "v1.1.4322\0\0\0";
-#endif
                private static readonly char[] tildeName = {'#','~','\0','\0'};
                private static readonly char[] stringsName = {'#','S','t','r','i','n','g','s','\0','\0','\0','\0'};
                private static readonly char[] usName = {'#','U','S','\0'};
@@ -5262,12 +5351,20 @@ namespace PEAPI {
 
                                MDTable tabIx = (MDTable)i;
                                if (count > max5BitSmlIx) {
+                                       if (tabIx == MDTable.Method || tabIx == MDTable.Field || tabIx == MDTable.TypeRef ||
+                                               tabIx == MDTable.TypeDef || tabIx == MDTable.Param || tabIx == MDTable.InterfaceImpl ||
+                                               tabIx == MDTable.MemberRef || tabIx == MDTable.Module || tabIx == MDTable.DeclSecurity ||
+                                               tabIx == MDTable.Property || tabIx == MDTable.Event || tabIx == MDTable.StandAloneSig ||
+                                               tabIx == MDTable.ModuleRef || tabIx == MDTable.TypeSpec || tabIx == MDTable.Assembly ||
+                                               tabIx == MDTable.AssemblyRef || tabIx == MDTable.File || tabIx == MDTable.ExportedType ||
+                                               tabIx == MDTable.ManifestResource || tabIx == MDTable.GenericParam)
                                        lgeCIx[(int)CIx.HasCustomAttr] = true;
                                }
                                if (count > max3BitSmlIx) {
-                                       if ((tabIx == MDTable.TypeRef) || (tabIx == MDTable.ModuleRef) || (tabIx == MDTable.Method) || (tabIx == MDTable.TypeSpec) || (tabIx == MDTable.Field)) 
+                                       if (tabIx == MDTable.Method || tabIx == MDTable.MemberRef)
                                                lgeCIx[(int)CIx.CustomAttributeType] = true;
-                                       if ((tabIx == MDTable.Method) || (tabIx == MDTable.MemberRef)) 
+                                       if (tabIx == MDTable.TypeDef || tabIx == MDTable.TypeRef || tabIx == MDTable.ModuleRef ||
+                                               tabIx == MDTable.Method || tabIx == MDTable.TypeSpec)
                                                lgeCIx[(int)CIx.MemberRefParent] = true;
                                }
                                if (count > max2BitSmlIx) {
@@ -5337,13 +5434,8 @@ namespace PEAPI {
                {
                        output.Seek(0,SeekOrigin.Current);
                        output.Write((uint)0); // Reserved
-#if NET_2_0
                        output.Write((byte)2); // MajorVersion
                        output.Write((byte)0); // MinorVersion
-#else
-                       output.Write((byte)1); // MajorVersion
-                       output.Write((byte)0); // MinorVersion
-#endif
                        output.Write(heapSizes);
                        output.Write((byte)1); // Reserved
                        output.Write(valid);
@@ -5394,11 +5486,9 @@ namespace PEAPI {
                        BuildTable(metaDataTables[(int)MDTable.TypeDef]);
                        BuildTable(metaDataTables[(int)MDTable.TypeSpec]);
                        BuildTable(metaDataTables[(int)MDTable.MemberRef]);
-#if NET_2_0
                        BuildTable(metaDataTables[(int)MDTable.GenericParam]);
                        BuildTable(metaDataTables[(int)MDTable.MethodSpec]);
                        BuildTable(metaDataTables[(int)MDTable.GenericParamConstraint]);
-#endif
                        BuildTable(metaDataTables[(int)MDTable.ManifestResource]);
 
                        if (cattr_list != null) {
@@ -5443,7 +5533,6 @@ namespace PEAPI {
                        SortTable(metaDataTables[(int)MDTable.DeclSecurity]);
                        SortTable(metaDataTables[(int)MDTable.MethodSemantics]);
                        SortTable(metaDataTables[(int)MDTable.ImplMap]);
-#if NET_2_0
                        if (metaDataTables[(int)MDTable.GenericParam] != null) {
                                SortTable(metaDataTables[(int)MDTable.GenericParam]);
                                // Now add GenericParamConstraints
@@ -5452,7 +5541,6 @@ namespace PEAPI {
                                  }*/
                        }
                        SortTable(metaDataTables[(int)MDTable.GenericParamConstraint]);
-#endif 
                        SortTable(metaDataTables[(int)MDTable.InterfaceImpl]);
                        SortTable(metaDataTables[(int)MDTable.CustomAttribute]);