2 // MarkNSObjectsBase.cs
5 // Jb Evain (jbevain@novell.com)
6 // Sebastien Pouliot <sebastien@xamarin.com>
8 // (C) 2009 Novell, Inc.
9 // Copyright (C) 2011 Xamarin, Inc.
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.Collections.Generic;
37 using Mono.Linker.Steps;
43 namespace Mono.Tuner {
45 public abstract class MarkNSObjectsBase : BaseSubStep {
47 protected abstract string ExportAttribute { get; }
49 public override SubStepTargets Targets {
50 get { return SubStepTargets.Type; }
53 public override void ProcessType (TypeDefinition type)
55 if (!IsProductType (type)) {
56 Annotations.Mark (type);
57 Annotations.SetPreserve (type, TypePreserve.All);
59 PreserveProductType (type);
62 void PreserveProductType (TypeDefinition type)
64 PreserveIntPtrConstructor (type);
65 PreserveExportedMethods (type);
68 void PreserveExportedMethods (TypeDefinition type)
73 foreach (var method in type.GetMethods ()) {
74 if (!IsExportedMethod (method))
77 if (!IsOverridenInUserCode (method))
80 PreserveMethod (type, method);
84 bool IsOverridenInUserCode (MethodDefinition method)
86 if (!method.IsVirtual)
89 var overrides = Annotations.GetOverrides (method);
90 if (overrides == null || overrides.Count == 0)
93 foreach (MethodDefinition @override in overrides)
94 if (!IsProductMethod (@override))
100 bool IsExportedMethod (MethodDefinition method)
102 return HasExportAttribute (method);
105 bool HasExportAttribute (ICustomAttributeProvider provider)
107 if (!provider.HasCustomAttributes)
110 foreach (CustomAttribute attribute in provider.CustomAttributes)
111 if (attribute.AttributeType.FullName == ExportAttribute)
117 void PreserveIntPtrConstructor (TypeDefinition type)
119 if (!type.HasMethods)
122 foreach (MethodDefinition constructor in type.GetConstructors ()) {
123 if (!constructor.HasParameters)
126 if (constructor.Parameters.Count != 1 || constructor.Parameters [0].ParameterType.FullName != "System.IntPtr")
129 PreserveMethod (type, constructor);
130 break; // only one .ctor can match this
134 void PreserveMethod (TypeDefinition type, MethodDefinition method)
136 Annotations.AddPreservedMethod (type, method);
139 static bool IsProductMethod (MethodDefinition method)
141 return IsProductType (method.DeclaringType);
144 static bool IsProductType (TypeDefinition type)
146 return Profile.IsProductAssembly (type.Module.Assembly);