2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / tools / tuner / Mono.Tuner / RemoveSerialization.cs
blob13fcc45484369d7a0085032a4024762509199203
1 //
2 // RemoveSerialization.cs
3 //
4 // Author:
5 // Jb Evain (jbevain@novell.com)
6 //
7 // (C) 2007 Novell, Inc.
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System;
30 using System.Collections;
32 using Mono.Linker;
33 using Mono.Linker.Steps;
35 using Mono.Cecil;
37 namespace Mono.Tuner {
39 public class RemoveSerialization : BaseStep {
41 static readonly string _Serialization = "System.Runtime.Serialization";
42 static readonly string _ISerializable = Concat (_Serialization, "ISerializable");
43 static readonly string _IDeserializationCallback = Concat (_Serialization, "IDeserializationCallback");
44 static readonly string _SerializationInfo = Concat (_Serialization, "SerializationInfo");
45 static readonly string _StreamingContext = Concat (_Serialization, "StreamingContext");
47 static readonly string _GetObjectData = "GetObjectData";
48 static readonly string _OnDeserialization = "OnDeserialization";
50 static string Concat (string lhs, string rhs)
52 return string.Concat (lhs, ".", rhs);
55 protected override void ProcessAssembly (AssemblyDefinition assembly)
57 if (assembly.Name.Name == Constants.Corlib)
58 return;
60 if (Annotations.GetAction (assembly) != AssemblyAction.Link)
61 return;
63 foreach (ModuleDefinition module in assembly.Modules)
64 foreach (TypeDefinition type in module.Types)
65 ProcessType (type);
68 static void RemoveInterface (TypeDefinition type, string name)
70 for (int i = 0; i < type.Interfaces.Count; i++) {
71 TypeReference iface = type.Interfaces [i];
72 if (iface.FullName == name) {
73 type.Interfaces.RemoveAt (i);
74 return;
79 static void RemoveSerializableFlag (TypeDefinition type)
81 type.Attributes &= ~TypeAttributes.Serializable;
84 static void ProcessType (TypeDefinition type)
86 RemoveSerializableFlag (type);
88 RemoveInterface (type, _ISerializable);
89 RemoveConstructor (type, _SerializationInfo, _StreamingContext);
90 RemoveInterfaceMethod (type, _ISerializable, _GetObjectData, _SerializationInfo, _StreamingContext);
92 RemoveInterface (type, _IDeserializationCallback);
93 RemoveInterfaceMethod (type, _IDeserializationCallback, _OnDeserialization, Constants.Object);
95 RemoveField (type);
98 static void RemoveField (TypeDefinition type)
100 for (int i = 0; i < type.Fields.Count; i++) {
101 FieldDefinition field = type.Fields [i];
102 if (field.FieldType.FullName == _SerializationInfo) {
103 type.Fields.RemoveAt (i);
104 break;
109 static bool ParametersMatch (IMethodSignature meth, string [] parameters)
111 for (int i = 0; i < parameters.Length; i++) {
112 ParameterDefinition param = meth.Parameters [i];
113 if (param.ParameterType.FullName != parameters [i])
114 return false;
117 return true;
120 static void RemoveInterfaceMethod (TypeDefinition type, string iface, string method, params string [] parameters)
122 RemoveMethod (type, method, parameters);
123 RemoveMethod (type, Concat (iface, method), parameters);
126 static void RemoveMethod (TypeDefinition type, string name, params string [] parameters)
128 RemoveMethod (type.Methods, name, parameters);
131 static void RemoveConstructor (TypeDefinition type, params string [] parameters)
133 RemoveMethod (type.Constructors, MethodDefinition.Ctor, parameters);
136 static void RemoveMethod (IList container, string name, params string [] parameters)
138 for (int i = 0; i < container.Count; i++) {
139 MethodDefinition method = (MethodDefinition) container [i];
140 if (method.Name != name)
141 continue;
143 if (method.Parameters.Count != parameters.Length)
144 continue;
146 if (!ParametersMatch (method, parameters))
147 continue;
149 container.RemoveAt (i);
150 return;