[w32handle] Stop returning 0 in every cases for locking/unlocking (#3926)
[mono.git] / mcs / tools / tuner / Mono.Tuner / MarkNSObjectsBase.cs
1 //
2 // MarkNSObjectsBase.cs
3 //
4 // Authors:
5 //      Jb Evain (jbevain@novell.com)
6 //      Sebastien Pouliot  <sebastien@xamarin.com>
7 //
8 // (C) 2009 Novell, Inc.
9 // Copyright (C) 2011 Xamarin, Inc.
10 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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.
29 //
30
31 using System;
32 using System.Collections;
33 using System.Collections.Generic;
34 using System.Linq;
35
36 using Mono.Linker;
37 using Mono.Linker.Steps;
38
39 using Mono.Tuner;
40
41 using Mono.Cecil;
42
43 namespace Mono.Tuner {
44
45         public abstract class MarkNSObjectsBase : BaseSubStep {
46
47                 protected abstract string ExportAttribute { get; }
48
49                 public override SubStepTargets Targets {
50                         get { return SubStepTargets.Type; }
51                 }
52
53                 public override void ProcessType (TypeDefinition type)
54                 {
55                         if (!IsProductType (type)) {
56                                 Annotations.Mark (type);
57                                 Annotations.SetPreserve (type, TypePreserve.All);
58                         } else
59                                 PreserveProductType (type);
60                 }
61
62                 void PreserveProductType (TypeDefinition type)
63                 {
64                         PreserveIntPtrConstructor (type);
65                         PreserveExportedMethods (type);
66                 }
67
68                 void PreserveExportedMethods (TypeDefinition type)
69                 {
70                         if (!type.HasMethods)
71                                 return;
72
73                         foreach (var method in type.GetMethods ()) {
74                                 if (!IsExportedMethod (method))
75                                         continue;
76
77                                 if (!IsOverridenInUserCode (method))
78                                         continue;
79
80                                 PreserveMethod (type, method);
81                         }
82                 }
83
84                 bool IsOverridenInUserCode (MethodDefinition method)
85                 {
86                         if (!method.IsVirtual)
87                                 return false;
88
89                         var overrides = Annotations.GetOverrides (method);
90                         if (overrides == null || overrides.Count == 0)
91                                 return false;
92
93                         foreach (MethodDefinition @override in overrides)
94                                 if (!IsProductMethod (@override))
95                                         return true;
96
97                         return false;
98                 }
99
100                 bool IsExportedMethod (MethodDefinition method)
101                 {
102                         return HasExportAttribute (method);
103                 }
104
105                 bool HasExportAttribute (ICustomAttributeProvider provider)
106                 {
107                         if (!provider.HasCustomAttributes)
108                                 return false;
109
110                         foreach (CustomAttribute attribute in provider.CustomAttributes)
111                                 if (attribute.AttributeType.FullName == ExportAttribute)
112                                         return true;
113
114                         return false;
115                 }
116
117                 void PreserveIntPtrConstructor (TypeDefinition type)
118                 {
119                         if (!type.HasMethods)
120                                 return;
121
122                         foreach (MethodDefinition constructor in type.GetConstructors ()) {
123                                 if (!constructor.HasParameters)
124                                         continue;
125
126                                 if (constructor.Parameters.Count != 1 || constructor.Parameters [0].ParameterType.FullName != "System.IntPtr")
127                                         continue;
128
129                                 PreserveMethod (type, constructor);
130                                 break; // only one .ctor can match this
131                         }
132                 }
133
134                 void PreserveMethod (TypeDefinition type, MethodDefinition method)
135                 {
136                         Annotations.AddPreservedMethod (type, method);
137                 }
138
139                 static bool IsProductMethod (MethodDefinition method)
140                 {
141                         return IsProductType (method.DeclaringType);
142                 }
143
144                 static bool IsProductType (TypeDefinition type)
145                 {
146                         return Profile.IsProductAssembly (type.Module.Assembly);
147                 }
148         }
149 }