Merge pull request #5428 from kumpera/wasm-support-p2
[mono.git] / mcs / class / PEAPI / Metadata.cs
index 1e08ff38590bddf4950a8e3c9b03d0d8f2586051..eca297380bcdc92386e6067a43e5b686447f67dd 100644 (file)
@@ -92,7 +92,14 @@ namespace PEAPI {
        /// </summary>
        public enum ImplAttr { IL, Native, Runtime = 0x03, Unmanaged = 0x04,
                ForwardRef = 0x10, PreserveSig = 0x0080, InternalCall = 0x1000, 
-               Synchronised = 0x0020, Synchronized = 0x0020, NoInLining = 0x0008, Optil = 0x0002}
+               Synchronised = 0x0020, Synchronized = 0x0020, NoInLining = 0x0008, NoOptimization = 0x0040, Optil = 0x0002,
+               AggressiveInlining = 0x0100
+       }
+
+       /// <summary>
+       /// Storage location for initial field data
+       /// </summary>
+       public enum DataSegment { Data, TLS, CIL }
 
        /// <summary>
        /// Modes for a parameter
@@ -443,6 +450,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,
@@ -1580,6 +1641,7 @@ namespace PEAPI {
        public class ClassDef : Class {
 
                Class superType;
+               bool setSuperType;
                ArrayList fields = new ArrayList();
                ArrayList methods = new ArrayList();
                ArrayList events;
@@ -1595,9 +1657,6 @@ namespace PEAPI {
                                MetaData md) : base(nsName, name, md) 
                {
                        metaData = md;
-                       if (! ((nsName == "" && name == "<Module>") || (nsName == "System" && name == "Object")) ) {
-                               superType = metaData.mscorlib.GetSpecialSystemClass(PrimitiveType.Object);
-                       }
                        flags = (uint)attrSet;
                        tabIx = MDTable.TypeDef;
                }
@@ -1605,6 +1664,7 @@ namespace PEAPI {
                internal void SetSuper(Class sClass) 
                {
                        superType = sClass;
+                       setSuperType = true;
                        if (! (sClass is GenericTypeInst))
                                typeIndexChecked = false;
                }
@@ -1616,12 +1676,13 @@ namespace PEAPI {
                        else  
                                superType = metaData.mscorlib.ValueType();
 
+                       setSuperType = true;
                        typeIndex = PrimitiveType.ValueType.GetTypeIndex ();
                }
 
                public void SpecialNoSuper() 
                {
-                       superType = null;
+                       setSuperType = true;
                }
 
                /// <summary>
@@ -1863,8 +1924,13 @@ namespace PEAPI {
 
                internal sealed override void BuildTables(MetaData md) 
                {
-                       if (done) return;
-                       if ((flags & (uint)TypeAttr.Interface) != 0) { superType = null; }
+                       if (done) 
+                               return;
+                       
+                       if ((flags & (uint)TypeAttr.Interface) != 0) {
+                               superType = null;
+                               setSuperType = true;
+                       }
                        // Console.WriteLine("Building tables for " + name);
                        if (layout != null) md.AddToTable(MDTable.ClassLayout,layout);
                        // Console.WriteLine("adding methods " + methods.Count);
@@ -1897,6 +1963,10 @@ namespace PEAPI {
                                                        ((Property)properties[0]).Row,MDTable.Property));
                        }
                        // Console.WriteLine("End of building tables");
+
+                       if (!setSuperType)
+                               superType = metaData.mscorlib.GetSpecialSystemClass(PrimitiveType.Object);
+
                        done = true;
                }
 
@@ -2912,7 +2982,7 @@ namespace PEAPI {
        /// <summary>
        /// Boolean constant
        /// </summary>
-       public class BoolConst : Constant {
+       public class BoolConst : DataConstant {
                bool val;
 
                /// <summary>
@@ -2945,7 +3015,7 @@ namespace PEAPI {
        }
 
        public class ByteArrConst : DataConstant {
-               byte[] val;
+               internal byte[] val;
 
                public ByteArrConst(byte[] val) 
                {
@@ -3110,7 +3180,7 @@ namespace PEAPI {
 
        }
 
-       public class UIntConst : Constant {
+       public class UIntConst : DataConstant {
                ulong val;
 
                public UIntConst(byte val) 
@@ -3165,7 +3235,7 @@ namespace PEAPI {
        }
 
        public class StringConst : DataConstant {
-               string val;
+               internal string val;
 
                public StringConst(string val) 
                {
@@ -3266,6 +3336,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 +3382,7 @@ namespace PEAPI {
 
                Type type;
                Class cmodType;
+               PrimitiveTypeRef cmodPrimType;
 
                /// <summary>
                /// Create a new custom modifier for a type
@@ -3324,10 +3397,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 +4552,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 &)
@@ -4955,7 +5061,7 @@ namespace PEAPI {
                private byte heapSizes = 0;
                MetaDataElement entryPoint;
                BinaryWriter output;
-               public MSCorLib mscorlib;
+               MSCorLib _mscorlib;
                private TypeSpec[] systemTypeSpecs = new TypeSpec[PrimitiveType.NumSystemTypes];
                long mdStart;
                private ArrayList cattr_list;
@@ -4980,9 +5086,15 @@ namespace PEAPI {
                        for (int i=0; i < lgeCIx.Length; i++) {
                                lgeCIx[i] = false;
                        }
-                       mscorlib = new MSCorLib(this);
                }
 
+               public MSCorLib mscorlib {
+                       get {
+                               return _mscorlib ?? (_mscorlib = new MSCorLib (this));
+                       }
+               }
+
+
                internal TypeSpec GetPrimitiveTypeSpec(int ix) 
                {
                        return systemTypeSpecs[ix];
@@ -5256,12 +5368,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) {