Merge pull request #4033 from ntherning/no-stdcall-for-icalls-on-windows-32-bit
[mono.git] / mcs / class / PEAPI / Metadata.cs
index fa2c360863ae12151c3c0219173796f17c214da4..d9b17da6a81524be9855bd2ec80234b626f3bc3e 100644 (file)
@@ -55,6 +55,7 @@ namespace PEAPI {
                PublicSealed = 0x101, SpecialName = 0x400, RTSpecialName = 0x800, 
                Import = 0x1000, Serializable = 0x2000, UnicodeClass = 0x10000,
                AutoClass = 0x20000, HasSecurity = 0x40000, BeforeFieldInit = 0x100000,
+               Forwarder = 0x200000,
                VisibilityMask = 0x07 }
 
        /// <summary>
@@ -93,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>
@@ -442,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,
@@ -2090,9 +2150,9 @@ namespace PEAPI {
                ExternClass externClass;
 
                internal ExternClassRef(TypeAttr attrs, string nsName, string name,
-                               FileRef declFile, MetaData md) : base(nsName,name,md) 
+                               MetaDataElement declRef, MetaData md) : base(nsName,name,md) 
                {
-                       externClass = new ExternClass(attrs,nameSpaceIx,nameIx,declFile);
+                       externClass = new ExternClass(attrs,nameSpaceIx,nameIx,declRef);
                        metaData.AddToTable(MDTable.ExportedType,externClass);
                }
 
@@ -2911,7 +2971,7 @@ namespace PEAPI {
        /// <summary>
        /// Boolean constant
        /// </summary>
-       public class BoolConst : Constant {
+       public class BoolConst : DataConstant {
                bool val;
 
                /// <summary>
@@ -2944,7 +3004,7 @@ namespace PEAPI {
        }
 
        public class ByteArrConst : DataConstant {
-               byte[] val;
+               internal byte[] val;
 
                public ByteArrConst(byte[] val) 
                {
@@ -3109,7 +3169,7 @@ namespace PEAPI {
 
        }
 
-       public class UIntConst : Constant {
+       public class UIntConst : DataConstant {
                ulong val;
 
                public UIntConst(byte val) 
@@ -3164,7 +3224,7 @@ namespace PEAPI {
        }
 
        public class StringConst : DataConstant {
-               string val;
+               internal string val;
 
                public StringConst(string val) 
                {
@@ -3265,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++) {
@@ -3309,6 +3371,7 @@ namespace PEAPI {
 
                Type type;
                Class cmodType;
+               PrimitiveTypeRef cmodPrimType;
 
                /// <summary>
                /// Create a new custom modifier for a type
@@ -3323,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);
                }
 
@@ -4465,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 &)
@@ -4926,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'};
@@ -5261,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) {
@@ -5336,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);
@@ -5393,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) {
@@ -5442,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
@@ -5451,7 +5541,6 @@ namespace PEAPI {
                                  }*/
                        }
                        SortTable(metaDataTables[(int)MDTable.GenericParamConstraint]);
-#endif 
                        SortTable(metaDataTables[(int)MDTable.InterfaceImpl]);
                        SortTable(metaDataTables[(int)MDTable.CustomAttribute]);