* PEAPI.cs: Don't build custom attributes twice.
[mono.git] / mcs / class / PEAPI / PEAPI.cs
index b6e6c644e82e009c1b4484bc7a92357a5fa5fdcd..9c28a49496c3d4c6d529453bd9b02420060fbe7d 100644 (file)
@@ -523,7 +523,7 @@ namespace PEAPI
       }\r
       return 0;\r
     }\r
\r
+
         }\r
   /**************************************************************************/  \r
 \r
@@ -1821,7 +1821,12 @@ namespace PEAPI
       fields.Add(field);\r
       return field;\r
     }\r
-\r
+
+    public void SetFieldOrder (ArrayList fields)
+    {
+            this.fields = fields;
+    }
+
     /// <summary>\r
     /// Add a method to this class\r
     /// </summary>\r
@@ -1988,7 +1993,6 @@ namespace PEAPI
         md.AddToTable(MDTable.PropertyMap,new MapElem(this,\r
                           ((Property)properties[0]).Row,MDTable.Property));\r
       }\r
-      DoCustomAttributes (md);\r
       // Console.WriteLine("End of building tables");\r
       done = true;\r
     }\r
@@ -2646,7 +2650,7 @@ namespace PEAPI
 \r
     internal sealed override void BuildTables(MetaData md) {\r
       BinaryWriter bw = new BinaryWriter(new MemoryStream());\r
-      bw.Write((ushort)1);\r
+      bw.Write(byteVal);\r
       md.AddToTable(MDTable.CustomAttribute, this);\r
       MemoryStream str = (MemoryStream)bw.BaseStream;\r
       valIx = md.AddToBlobHeap(str.ToArray());\r
@@ -3482,9 +3486,13 @@ if (rsrc != null)
      }\r
 \r
     private void WriteSDataSection() {\r
+      long size = sdata.Size ();
+      long start = BaseStream.Position;
       for (int i=0; i < data.Count; i++) {\r
         ((DataConstant)data[i]).Write(this);\r
-      }\r
+      }
+      while (BaseStream.Position < (start + size))
+              Write ((byte) 0);
     }\r
 \r
                 private void WriteRsrcSection() {\r
@@ -4416,7 +4424,8 @@ if (rsrc != null)
     public MSCorLib mscorlib;\r
     private TypeSpec[] systemTypeSpecs = new TypeSpec[PrimitiveType.NumSystemTypes];\r
     long mdStart;\r
-\r
+                private ArrayList cattr_list;
+                
     internal MetaData(FileImage file) {\r
       // tilde = new MetaDataStream(tildeName,false,0);\r
       this.file = file;\r
@@ -4521,7 +4530,13 @@ if (rsrc != null)
       return blob.Add(val,true);\r
     }\r
 \r
-\r
+                internal void AddCustomAttribute (CustomAttribute cattr)
+                {
+                        if (cattr_list == null)
+                                cattr_list = new ArrayList ();
+                        cattr_list.Add (cattr);
+                }
+
     private ArrayList GetTable(MDTable tableIx) {\r
       int tabIx = (int)tableIx;\r
       if (metaDataTables[tabIx] == null) {\r
@@ -4752,7 +4767,12 @@ if (rsrc != null)
       BuildTable(metaDataTables[(int)MDTable.GenericParam]);\r
       BuildTable(metaDataTables[(int)MDTable.MethodSpec]);\r
       BuildTable(metaDataTables[(int)MDTable.GenericParamConstraint]);\r
-      BuildTable(metaDataTables[(int)MDTable.CustomAttribute]);\r
+
+      if (cattr_list != null) {
+              foreach (CustomAttribute cattr in cattr_list)
+                      cattr.BuildTables (this);
+      }
+
 /*      for (int i=0; i < metaDataTables.Length; i++) {\r
         ArrayList table = metaDataTables[i];\r
         if (table != null) {\r
@@ -4762,6 +4782,7 @@ if (rsrc != null)
         }\r
       }\r
       */\r
+
                         SetIndexSizes();\r
                         for (int i=1; i < numStreams; i++) {\r
                                 streams[i].EndStream();\r
@@ -4881,16 +4902,7 @@ if (rsrc != null)
       } \r
 //      customAttributes.Add(new CustomAttribute(this,ctorMeth,cVals));\r
     }\r
-\r
-    internal void DoCustomAttributes(MetaData md) {\r
-      if (customAttributes != null) {\r
-        for (int i=0; i < customAttributes.Count; i++) {\r
-          CustomAttribute ca = (CustomAttribute)customAttributes[i];\r
-          ca.BuildTables(md);\r
-        }\r
-      }\r
-    }\r
-\r
+
     internal uint Token() {\r
       return (((uint)tabIx << 24) | row);\r
     }\r
@@ -5354,7 +5366,6 @@ if (rsrc != null)
         varArgSig.BuildTables(md);\r
       }\r
       }\r
-      DoCustomAttributes (md);\r
       // Console.WriteLine("method has " + numPars + " parameters");\r
       done = true;\r
     }\r
@@ -5521,7 +5532,7 @@ if (rsrc != null)
         /// \r
         public class MethPtrType : Type\r
         {\r
-
+                bool varArgMeth;
                 Type retType;
                 Type [] parList;
                 Type [] optParList;
@@ -5539,6 +5550,7 @@ if (rsrc != null)
       this.retType = retType;
       callConv = callconv;
       parList = pars;
+      this.varArgMeth = varArgMeth;
       if (parList != null) numPars = (uint)parList.Length;
       if (varArgMeth) {
         optParList = optPars;
@@ -5557,7 +5569,7 @@ if (rsrc != null)
       for (int i=0; i < numPars; i++) {
               parList[i].TypeSig (sig);
       }
-      if (numOptPars > 0) {
+      if (varArgMeth) {
               sig.WriteByte (0x41); // Write the sentinel
         for (int i=0; i < numOptPars; i++) {
           optParList[i].TypeSig (sig);
@@ -6257,7 +6269,12 @@ if (rsrc != null)
       metaData.AddToTable(MDTable.ManifestResource,mr);\r
       //mr.FixName(metaData);\r
     }\r
-\r
+
+    public void AddCustomAttribute (Method meth, byte [] data, MetaDataElement element)
+    {
+            metaData.AddCustomAttribute (new CustomAttribute (element, meth, data));
+    }
+
     /// <summary>\r
     /// Write out the PEFile (the "bake" function)\r
     /// </summary>\r