5 // Jb Evain (jbevain@novell.com)
7 // (C) 2009 Novell, Inc.
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.
30 using System.Collections;
31 using System.Collections.Generic;
35 using Mono.Linker.Steps;
41 namespace MonoMac.Tuner {
43 public class MarkNSObjects : BaseSubStep {
45 const string ExportAttribute = "MonoMac.Foundation.ExportAttribute";
46 const string MonoMacAssembly = "MonoMac";
48 public override SubStepTargets Targets {
49 get { return SubStepTargets.Type; }
52 public override void ProcessType (TypeDefinition type)
54 if (!type.IsNSObject () || !type.IsNativeObject ())
57 if (!IsMonoMacType (type)) {
58 Annotations.Mark (type);
59 Annotations.SetPreserve (type, TypePreserve.All);
61 PreserveMonoMacType (type);
64 void PreserveMonoMacType (TypeDefinition type)
66 PreserveIntPtrConstructor (type);
67 PreserveExportedMethods (type);
70 void PreserveExportedMethods (TypeDefinition type)
75 foreach (var method in type.GetMethods ()) {
76 if (!IsExportedMethod (method))
79 if (!IsOverridenInUserCode (method))
82 PreserveMethod (type, method);
86 bool IsOverridenInUserCode (MethodDefinition method)
88 if (!method.IsVirtual)
91 var overrides = Annotations.GetOverrides (method);
92 if (overrides == null || overrides.Count == 0)
95 foreach (MethodDefinition @override in overrides)
96 if (!IsMonoMacMethod (@override))
102 static bool IsExportedMethod (MethodDefinition method)
104 return HasExportAttribute (method);
107 static bool HasExportAttribute (ICustomAttributeProvider provider)
109 if (!provider.HasCustomAttributes)
112 foreach (CustomAttribute attribute in provider.CustomAttributes)
113 if (attribute.AttributeType.FullName == ExportAttribute)
119 void PreserveIntPtrConstructor (TypeDefinition type)
121 if (!type.HasMethods)
124 foreach (MethodDefinition constructor in type.GetConstructors ()) {
125 if (!constructor.HasParameters)
128 if (constructor.Parameters.Count != 1 || constructor.Parameters [0].ParameterType.FullName != "System.IntPtr")
131 PreserveMethod (type, constructor);
135 void PreserveMethod (TypeDefinition type, MethodDefinition method)
137 Annotations.AddPreservedMethod (type, method);
140 static bool IsMonoMacMethod (MethodDefinition method)
142 return IsMonoMacType (method.DeclaringType);
145 static bool IsMonoMacType (TypeDefinition type)
147 return type.Module.Assembly.Name.Name == MonoMacAssembly;