[w32handle] Stop returning 0 in every cases for locking/unlocking (#3926)
[mono.git] / mcs / tools / tuner / Mono.Tuner / FilterAttributes.cs
1 //
2 // FilterAttributes.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@novell.com)
6 //
7 // (C) 2009 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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27 //
28
29 using System;
30 using System.Collections;
31
32 using Mono.Linker;
33 using Mono.Linker.Steps;
34
35 using Mono.Cecil;
36
37 namespace Mono.Tuner {
38
39         public class FilterAttributes : BaseStep {
40
41                 static Hashtable attributes = new Hashtable ();
42
43                 static FilterAttributes ()
44                 {
45                         FilterAttribute ("System.Runtime.InteropServices.ComVisibleAttribute");
46                 }
47
48                 static void FilterAttribute (string fullname)
49                 {
50                         attributes.Add (fullname, null);
51                 }
52
53                 protected override void ProcessAssembly (AssemblyDefinition assembly)
54                 {
55                         if (Annotations.GetAction (assembly) != AssemblyAction.Link)
56                                 return;
57
58                         Filter (assembly);
59
60                         foreach (ModuleDefinition module in assembly.Modules)
61                                 ProcessModule (module);
62                 }
63
64                 static void ProcessModule (ModuleDefinition module)
65                 {
66                         Filter (module);
67
68                         foreach (TypeDefinition type in module.Types)
69                                 ProcessType (type);
70                 }
71
72                 static void ProcessType (TypeDefinition type)
73                 {
74                         if (type.HasFields)
75                                 ProcessFields (type.Fields);
76
77                         if (type.HasMethods)
78                                 ProcessMethods (type.Methods);
79
80                         if (type.HasEvents)
81                                 ProcessEvents (type.Events);
82
83                         if (type.HasProperties)
84                                 ProcessProperties (type.Properties);
85
86                         ProcessGenericParameters (type);
87                 }
88
89                 static void ProcessFields (ICollection fields)
90                 {
91                         foreach (FieldDefinition field in fields)
92                                 Filter (field);
93                 }
94
95                 static void ProcessMethods (ICollection methods)
96                 {
97                         foreach (MethodDefinition method in methods)
98                                 ProcessMethod (method);
99                 }
100
101                 static void ProcessMethod (MethodDefinition method)
102                 {
103                         ProcessGenericParameters (method);
104
105                         Filter (method.MethodReturnType);
106
107                         if (method.HasParameters)
108                                 ProcessParameters (method.Parameters);
109                 }
110
111                 static void ProcessParameters (ICollection parameters)
112                 {
113                         foreach (ParameterDefinition parameter in parameters)
114                                 Filter (parameter);
115                 }
116
117                 static void ProcessGenericParameters (IGenericParameterProvider provider)
118                 {
119                         if (!provider.HasGenericParameters)
120                                 return;
121
122                         foreach (GenericParameter parameter in provider.GenericParameters)
123                                 Filter (parameter);
124                 }
125
126                 static void ProcessEvents (ICollection events)
127                 {
128                         foreach (EventDefinition @event in events)
129                                 Filter (@event);
130                 }
131
132                 static void ProcessProperties (ICollection properties)
133                 {
134                         foreach (PropertyDefinition property in properties)
135                                 Filter (property);
136                 }
137
138                 static void Filter (ICustomAttributeProvider provider)
139                 {
140                         if (!provider.HasCustomAttributes)
141                                 return;
142
143                         for (int i = 0; i < provider.CustomAttributes.Count; i++) {
144                                 CustomAttribute attribute = provider.CustomAttributes [i];
145                                 if (!IsFilteredAttribute (attribute))
146                                         continue;
147
148                                 provider.CustomAttributes.RemoveAt (i--);
149                         }
150                 }
151
152                 static bool IsFilteredAttribute (CustomAttribute attribute)
153                 {
154                         return attributes.Contains (attribute.Constructor.DeclaringType.FullName);
155                 }
156         }
157 }