2007-07-27 Jb Evain <jbevain@novell.com>
authorJb Evain <jbevain@gmail.com>
Fri, 27 Jul 2007 15:07:53 +0000 (15:07 -0000)
committerJb Evain <jbevain@gmail.com>
Fri, 27 Jul 2007 15:07:53 +0000 (15:07 -0000)
* Mono.Tuner/RemoveSerialization.cs:
Add a new linker step to remove automatically everything
serialization related.

svn path=/trunk/mcs/; revision=82861

mcs/tools/tuner/ChangeLog
mcs/tools/tuner/Mono.Tuner/PrintStatus.cs
mcs/tools/tuner/Mono.Tuner/RemoveSerialization.cs [new file with mode: 0644]

index 0fa3fd71a0a227aab36d7e4869f4d3ed417cc68a..c445a8a69f8678272d5cfacd973b022ef048dd23 100644 (file)
@@ -1,3 +1,9 @@
+2007-07-27  Jb Evain  <jbevain@novell.com>
+
+       * Mono.Tuner/RemoveSerialization.cs:
+               Add a new linker step to remove automatically everything
+               serialization related.
+
 2007-07-18  Jb Evain  <jbevain@novell.com>
 
        * tuner: new tool to tune the Mono assemblies from a full 2.1
index 9a39b968bd717a8d7f23ae9ae25e3359a9281c30..00ed5e5117d6f199e49aaf7bf496fa1fe38269fc 100644 (file)
@@ -1,3 +1,31 @@
+//
+// PrintStatus.cs
+//
+// Author:
+//   Jb Evain (jbevain@novell.com)
+//
+// (C) 2007 Novell, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System;
 
 using Mono.Linker;
diff --git a/mcs/tools/tuner/Mono.Tuner/RemoveSerialization.cs b/mcs/tools/tuner/Mono.Tuner/RemoveSerialization.cs
new file mode 100644 (file)
index 0000000..edd002b
--- /dev/null
@@ -0,0 +1,141 @@
+//
+// RemoveSerialization.cs
+//
+// Author:
+//   Jb Evain (jbevain@novell.com)
+//
+// (C) 2007 Novell, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Collections;
+
+using Mono.Linker;
+using Mono.Linker.Steps;
+
+using Mono.Cecil;
+
+namespace Mono.Tuner {
+       
+       public class RemoveSerialization : BaseStep {
+
+               static readonly string _Serialization = "System.Runtime.Serialization";
+               static readonly string _ISerializable = Concat (_Serialization, "ISerializable");
+               static readonly string _IDeserializationCallback = Concat (_Serialization, "IDeserializationCallback");
+               static readonly string _SerializationInfo = Concat (_Serialization, "SerializationInfo");
+               static readonly string _StreamingContext = Concat (_Serialization, "StreamingContext");
+
+               static readonly string _GetObjectData = "GetObjectData";
+               static readonly string _OnDeserialization = "OnDeserialization";
+
+               static string Concat (string lhs, string rhs)
+               {
+                       return string.Concat (lhs, ".", rhs);
+               }
+
+               protected override void ProcessAssembly (AssemblyDefinition assembly)
+               {
+                       if (assembly.Name.Name == Constants.Corlib)
+                               return;
+
+                       foreach (ModuleDefinition module in assembly.Modules)
+                               foreach (TypeDefinition type in module.Types)
+                                       ProcessType (type);
+               }
+
+               static bool RemoveIfImplements (TypeDefinition type, string name)
+               {
+                       for (int i = 0; i < type.Interfaces.Count; i++) {
+                               TypeReference iface = type.Interfaces [i];
+                               if (iface.FullName == name) {
+                                       type.Interfaces.RemoveAt (i);
+                                       return true;
+                               }
+                       }
+
+                       return false;
+               }
+
+               static void RemoveSerializableFlag (TypeDefinition type)
+               {
+                       type.Attributes &= ~TypeAttributes.Serializable; 
+               }
+
+               static void ProcessType (TypeDefinition type)
+               {
+                       RemoveSerializableFlag (type);
+
+                       if (RemoveIfImplements (type, _ISerializable)) {
+                               RemoveConstructor (type, _SerializationInfo, _StreamingContext);
+                               RemoveInterfaceMethod (type, _ISerializable, _GetObjectData, _SerializationInfo, _StreamingContext);
+                       }
+
+                       if (RemoveIfImplements (type, _IDeserializationCallback))
+                               RemoveInterfaceMethod (type, _IDeserializationCallback, _OnDeserialization, Constants.Object);
+               }
+
+               static bool ParametersMatch (IMethodSignature meth, string [] parameters)
+               {
+                       for (int i = 0; i < parameters.Length; i++) {
+                               ParameterDefinition param = meth.Parameters [i];
+                               if (param.ParameterType.FullName != parameters [i])
+                                       return false;
+                       }
+
+                       return true;
+               }
+
+               static void RemoveInterfaceMethod (TypeDefinition type, string iface, string method, params string [] parameters)
+               {
+                       RemoveMethod (type, method, parameters);
+                       RemoveMethod (type, Concat (iface, method), parameters);
+               }
+
+               static void RemoveMethod (TypeDefinition type, string name, params string [] parameters)
+               {
+                       RemoveMethod (type.Methods, name, parameters);
+               }
+
+               static void RemoveConstructor (TypeDefinition type, params string [] parameters)
+               {
+                       RemoveMethod (type.Constructors, MethodDefinition.Ctor, parameters);
+               }
+
+               static void RemoveMethod (IList container, string name, params string [] parameters)
+               {
+                       for (int i = 0; i < container.Count; i++) {
+                               MethodDefinition method = (MethodDefinition) container [i];
+                               if (method.Name != name)
+                                       continue;
+
+                               if (method.Parameters.Count != parameters.Length)
+                                       continue;
+
+                               if (!ParametersMatch (method, parameters))
+                                       continue;
+
+                               container.RemoveAt (i);
+                               return;
+                       }
+               }
+       }
+}