Refactor MonoMac's MarkNSObjects substep so it can be reused in other products
authorSebastien Pouliot <sebastien@xamarin.com>
Thu, 9 Jun 2011 14:30:07 +0000 (10:30 -0400)
committerSebastien Pouliot <sebastien@xamarin.com>
Thu, 9 Jun 2011 14:30:07 +0000 (10:30 -0400)
mcs/tools/tuner/Mono.Tuner/MarkNSObjectsBase.cs [new file with mode: 0644]
mcs/tools/tuner/MonoMac.Tuner/MarkNSObjects.cs

diff --git a/mcs/tools/tuner/Mono.Tuner/MarkNSObjectsBase.cs b/mcs/tools/tuner/Mono.Tuner/MarkNSObjectsBase.cs
new file mode 100644 (file)
index 0000000..ae8ce8c
--- /dev/null
@@ -0,0 +1,148 @@
+//
+// MarkNSObjectsBase.cs
+//
+// Authors:
+//     Jb Evain (jbevain@novell.com)
+//     Sebastien Pouliot  <sebastien@xamarin.com>
+//
+// (C) 2009 Novell, Inc.
+// Copyright (C) 2011 Xamarin, 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 System.Collections.Generic;
+using System.Linq;
+
+using Mono.Linker;
+using Mono.Linker.Steps;
+
+using Mono.Tuner;
+
+using Mono.Cecil;
+
+namespace MonoMac.Tuner {
+
+       public abstract class MarkNSObjectsBase : BaseSubStep {
+
+               protected abstract string ExportAttribute { get; }
+
+               public override SubStepTargets Targets {
+                       get { return SubStepTargets.Type; }
+               }
+
+               public override void ProcessType (TypeDefinition type)
+               {
+                       if (!IsProductType (type)) {
+                               Annotations.Mark (type);
+                               Annotations.SetPreserve (type, TypePreserve.All);
+                       } else
+                               PreserveProductType (type);
+               }
+
+               void PreserveProductType (TypeDefinition type)
+               {
+                       PreserveIntPtrConstructor (type);
+                       PreserveExportedMethods (type);
+               }
+
+               void PreserveExportedMethods (TypeDefinition type)
+               {
+                       if (!type.HasMethods)
+                               return;
+
+                       foreach (var method in type.GetMethods ()) {
+                               if (!IsExportedMethod (method))
+                                       continue;
+
+                               if (!IsOverridenInUserCode (method))
+                                       continue;
+
+                               PreserveMethod (type, method);
+                       }
+               }
+
+               bool IsOverridenInUserCode (MethodDefinition method)
+               {
+                       if (!method.IsVirtual)
+                               return false;
+
+                       var overrides = Annotations.GetOverrides (method);
+                       if (overrides == null || overrides.Count == 0)
+                               return false;
+
+                       foreach (MethodDefinition @override in overrides)
+                               if (!IsProductMethod (@override))
+                                       return true;
+
+                       return false;
+               }
+
+               bool IsExportedMethod (MethodDefinition method)
+               {
+                       return HasExportAttribute (method);
+               }
+
+               bool HasExportAttribute (ICustomAttributeProvider provider)
+               {
+                       if (!provider.HasCustomAttributes)
+                               return false;
+
+                       foreach (CustomAttribute attribute in provider.CustomAttributes)
+                               if (attribute.AttributeType.FullName == ExportAttribute)
+                                       return true;
+
+                       return false;
+               }
+
+               void PreserveIntPtrConstructor (TypeDefinition type)
+               {
+                       if (!type.HasMethods)
+                               return;
+
+                       foreach (MethodDefinition constructor in type.GetConstructors ()) {
+                               if (!constructor.HasParameters)
+                                       continue;
+
+                               if (constructor.Parameters.Count != 1 || constructor.Parameters [0].ParameterType.FullName != "System.IntPtr")
+                                       continue;
+
+                               PreserveMethod (type, constructor);
+                       }
+               }
+
+               void PreserveMethod (TypeDefinition type, MethodDefinition method)
+               {
+                       Annotations.AddPreservedMethod (type, method);
+               }
+
+               static bool IsProductMethod (MethodDefinition method)
+               {
+                       return IsProductType (method.DeclaringType);
+               }
+
+               static bool IsProductType (TypeDefinition type)
+               {
+                       return Profile.IsProductAssembly (type.Module.Assembly);
+               }
+       }
+}
index 4b4eed4e773c5581e9bfef78f75ce852a923f64d..163deccbbbc012ec8d2a60a57fe915fe013a1249 100644 (file)
@@ -1,10 +1,12 @@
 //
-// MarkNSObbjects.cs
+// MarkNSObjects.cs
 //
-// Author:
-//   Jb Evain (jbevain@novell.com)
+// Authors:
+//     Jb Evain (jbevain@novell.com)
+//     Sebastien Pouliot  <sebastien@xamarin.com>
 //
 // (C) 2009 Novell, Inc.
+// Copyright (C) 2011 Xamarin, Inc.
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 //
 
 using System;
-using System.Collections;
-using System.Collections.Generic;
-using System.Linq;
-
-using Mono.Linker;
-using Mono.Linker.Steps;
 
 using Mono.Tuner;
 
@@ -40,13 +36,10 @@ using Mono.Cecil;
 
 namespace MonoMac.Tuner {
 
-       public class MarkNSObjects : BaseSubStep {
-
-               const string ExportAttribute = "MonoMac.Foundation.ExportAttribute";
-               const string MonoMacAssembly = "MonoMac";
+       public class MarkNSObjects : MarkNSObjectsBase {
 
-               public override SubStepTargets Targets {
-                       get { return SubStepTargets.Type; }
+               protected override string ExportAttribute {
+                       get { return "MonoMac.Foundation.ExportAttribute"; }
                }
 
                public override void ProcessType (TypeDefinition type)
@@ -54,97 +47,7 @@ namespace MonoMac.Tuner {
                        if (!type.IsNSObject () || !type.IsNativeObject ())
                                return;
 
-                       if (!IsMonoMacType (type)) {
-                               Annotations.Mark (type);
-                               Annotations.SetPreserve (type, TypePreserve.All);
-                       } else
-                               PreserveMonoMacType (type);
-               }
-
-               void PreserveMonoMacType (TypeDefinition type)
-               {
-                       PreserveIntPtrConstructor (type);
-                       PreserveExportedMethods (type);
-               }
-
-               void PreserveExportedMethods (TypeDefinition type)
-               {
-                       if (!type.HasMethods)
-                               return;
-
-                       foreach (var method in type.GetMethods ()) {
-                               if (!IsExportedMethod (method))
-                                       continue;
-
-                               if (!IsOverridenInUserCode (method))
-                                       continue;
-
-                               PreserveMethod (type, method);
-                       }
-               }
-
-               bool IsOverridenInUserCode (MethodDefinition method)
-               {
-                       if (!method.IsVirtual)
-                               return false;
-
-                       var overrides = Annotations.GetOverrides (method);
-                       if (overrides == null || overrides.Count == 0)
-                               return false;
-
-                       foreach (MethodDefinition @override in overrides)
-                               if (!IsMonoMacMethod (@override))
-                                       return true;
-
-                       return false;
-               }
-
-               static bool IsExportedMethod (MethodDefinition method)
-               {
-                       return HasExportAttribute (method);
-               }
-
-               static bool HasExportAttribute (ICustomAttributeProvider provider)
-               {
-                       if (!provider.HasCustomAttributes)
-                               return false;
-
-                       foreach (CustomAttribute attribute in provider.CustomAttributes)
-                               if (attribute.AttributeType.FullName == ExportAttribute)
-                                       return true;
-
-                       return false;
-               }
-
-               void PreserveIntPtrConstructor (TypeDefinition type)
-               {
-                       if (!type.HasMethods)
-                               return;
-
-                       foreach (MethodDefinition constructor in type.GetConstructors ()) {
-                               if (!constructor.HasParameters)
-                                       continue;
-
-                               if (constructor.Parameters.Count != 1 || constructor.Parameters [0].ParameterType.FullName != "System.IntPtr")
-                                       continue;
-
-                               PreserveMethod (type, constructor);
-                       }
-               }
-
-               void PreserveMethod (TypeDefinition type, MethodDefinition method)
-               {
-                       Annotations.AddPreservedMethod (type, method);
-               }
-
-               static bool IsMonoMacMethod (MethodDefinition method)
-               {
-                       return IsMonoMacType (method.DeclaringType);
-               }
-
-               static bool IsMonoMacType (TypeDefinition type)
-               {
-                       return type.Module.Assembly.Name.Name == MonoMacAssembly;
+                       base.ProcessType (type);
                }
        }
 }