[w32handle] Stop returning 0 in every cases for locking/unlocking (#3926)
[mono.git] / mcs / tools / linker / Mono.Linker / XApiReader.cs
1 //
2 // XApiReader.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@novell.com)
6 //
7 // (C) 2007 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 using System.Linq;
32 using System.Text;
33 using System.Xml.XPath;
34
35 using Mono.Cecil;
36
37 namespace Mono.Linker {
38
39         public class XApiReader {
40
41                 static readonly string _name = "name";
42                 static readonly string _ns = string.Empty;
43
44                 LinkContext _context;
45                 XPathDocument _document;
46                 IXApiVisitor _visitor;
47
48                 AssemblyDefinition _assembly;
49                 string _namespace;
50                 Stack _types = new Stack ();
51                 StringBuilder _signature;
52
53                 public XApiReader (XPathDocument document, IXApiVisitor visitor)
54                 {
55                         _document = document;
56                         _visitor = visitor;
57                 }
58
59                 public void Process (LinkContext context)
60                 {
61                         _context = context;
62                         ProcessAssemblies (_document.CreateNavigator ());
63                 }
64
65                 void OnAssembly (XPathNavigator nav)
66                 {
67                         _assembly = GetAssembly (nav);
68
69                         _visitor.OnAssembly (nav, _assembly);
70
71                         ProcessAttributes (nav);
72                         ProcessNamespaces (nav);
73                 }
74
75                 AssemblyDefinition GetAssembly (XPathNavigator nav)
76                 {
77                         AssemblyNameReference name = new AssemblyNameReference (
78                                 GetName (nav),
79                                 new Version (GetAttribute (nav, "version")));
80
81                         AssemblyDefinition assembly = _context.Resolve (name);
82                         ProcessReferences (assembly);
83                         return assembly;
84                 }
85
86                 void ProcessReferences (AssemblyDefinition assembly)
87                 {
88                         foreach (AssemblyNameReference name in assembly.MainModule.AssemblyReferences)
89                                 _context.Resolve (name);
90                 }
91
92                 void OnAttribute (XPathNavigator nav)
93                 {
94                         _visitor.OnAttribute (nav);
95                 }
96
97                 void PushType (TypeDefinition type)
98                 {
99                         _types.Push (type);
100                 }
101
102                 TypeDefinition PeekType ()
103                 {
104                         return (TypeDefinition) _types.Peek ();
105                 }
106
107                 TypeDefinition PopType ()
108                 {
109                         return (TypeDefinition) _types.Pop ();
110                 }
111
112                 void OnNamespace (XPathNavigator nav)
113                 {
114                         _namespace = GetName (nav);
115
116                         ProcessClasses (nav);
117                 }
118
119                 void OnClass (XPathNavigator nav)
120                 {
121                         string name = GetClassName (nav);
122
123                         TypeDefinition type = _assembly.MainModule.GetType (name);
124                         if (type == null)
125                                 return;
126
127                         _visitor.OnClass (nav, type);
128
129                         PushType (type);
130
131                         ProcessAttributes (nav);
132                         ProcessInterfaces (nav);
133                         ProcessFields (nav);
134                         ProcessMethods (nav);
135                         ProcessConstructors (nav);
136                         ProcessProperties (nav);
137                         ProcessEvents (nav);
138                         ProcessClasses (nav);
139
140                         PopType ();
141                 }
142
143                 string GetClassName (XPathNavigator nav)
144                 {
145                         if (IsNestedClass ())
146                                 return PeekType ().FullName + "/" + GetName (nav);
147
148                         return _namespace + "." + GetName (nav);
149                 }
150
151                 bool IsNestedClass ()
152                 {
153                         return _types.Count > 0;
154                 }
155
156                 void OnField (XPathNavigator nav)
157                 {
158                         TypeDefinition declaring = PeekType ();
159
160                         FieldDefinition field = declaring.Fields.FirstOrDefault (f => f.Name == GetName (nav));
161                         if (field != null)
162                                 _visitor.OnField (nav, field);
163
164                         ProcessAttributes (nav);
165                 }
166
167                 void OnInterface (XPathNavigator nav)
168                 {
169                         string name = GetName (nav);
170
171                         TypeDefinition type = _context.GetType (GetTypeName (name));
172                         if (type != null)
173                                 _visitor.OnInterface (nav, type);
174                 }
175
176                 void OnMethod (XPathNavigator nav)
177                 {
178                         InitMethodSignature (nav);
179
180                         ProcessParameters (nav);
181
182                         string signature = GetMethodSignature ();
183
184                         MethodDefinition method = GetMethod (signature);
185                         if (method != null)
186                                 _visitor.OnMethod (nav, method);
187
188                         ProcessAttributes (nav);
189                 }
190
191                 MethodDefinition GetMethod (string signature)
192                 {
193                         return GetMethod (PeekType ().Methods, signature);
194                 }
195
196                 static MethodDefinition GetMethod (ICollection methods, string signature)
197                 {
198                         foreach (MethodDefinition method in methods)
199                                 if (signature == GetSignature (method))
200                                         return method;
201
202                         return null;
203                 }
204
205                 static string GetSignature (MethodDefinition method)
206                 {
207                         return method.ToString ().Replace ("<", "[").Replace (">", "]");
208                 }
209
210                 string GetMethodSignature ()
211                 {
212                         _signature.Append (")");
213                         return _signature.ToString ();
214                 }
215
216                 void InitMethodSignature (XPathNavigator nav)
217                 {
218                         _signature = new StringBuilder ();
219
220                         string returntype = GetAttribute (nav, "returntype");
221                         if (returntype == null || returntype.Length == 0)
222                                 returntype = "System.Void";
223
224                         _signature.Append (NormalizeTypeName (returntype));
225                         _signature.Append (" ");
226                         _signature.Append (PeekType ().FullName);
227                         _signature.Append ("::");
228
229                         string name = GetName (nav);
230                         _signature.Append (GetMethodName (name));
231
232                         _signature.Append ("(");
233                 }
234
235                 static string GetMethodName (string name)
236                 {
237                         return GetStringBefore (name, "(");
238                 }
239
240                 static string NormalizeTypeName (string name)
241                 {
242                         return name.Replace ("+", "/").Replace ("<", "[").Replace (">", "]");
243                 }
244
245                 static string GetTypeName (string name)
246                 {
247                         return GetStringBefore (NormalizeTypeName (name), "[");
248                 }
249
250                 static string GetStringBefore (string str, string marker)
251                 {
252                         int pos = str.IndexOf (marker);
253                         if (pos == -1)
254                                 return str;
255
256                         return str.Substring (0, pos);
257                 }
258
259                 void OnParameter (XPathNavigator nav)
260                 {
261                         string type = GetAttribute (nav, "type");
262                         int pos = int.Parse (GetAttribute (nav, "position"));
263
264                         if (pos > 0)
265                                 _signature.Append (",");
266                         _signature.Append (NormalizeTypeName (type));
267                 }
268
269                 void OnConstructor (XPathNavigator nav)
270                 {
271                         InitMethodSignature (nav);
272
273                         ProcessParameters (nav);
274
275                         string signature = GetMethodSignature ();
276
277                         MethodDefinition ctor = GetMethod (signature);
278                         if (ctor != null)
279                                 _visitor.OnConstructor (nav, ctor);
280
281                         ProcessAttributes (nav);
282                 }
283
284                 void OnProperty (XPathNavigator nav)
285                 {
286                         string name = GetName (nav);
287                         TypeDefinition type = PeekType ();
288
289                         var property = type.Properties.FirstOrDefault (p => p.Name == name);
290                         if (property != null)
291                                 _visitor.OnProperty (nav, property);
292
293                         ProcessAttributes (nav);
294                         ProcessMethods (nav);
295                 }
296
297                 void OnEvent (XPathNavigator nav)
298                 {
299                         string name = GetName (nav);
300                         TypeDefinition type = PeekType ();
301
302                         EventDefinition evt = type.Events.FirstOrDefault (e => e.Name == name);
303                         if (evt != null)
304                                 _visitor.OnEvent (nav, evt);
305
306                         ProcessAttributes (nav);
307                 }
308
309                 void ProcessAssemblies (XPathNavigator nav)
310                 {
311                         ProcessChildren (nav, "assemblies//assembly", new OnChildren (OnAssembly));
312                 }
313
314                 void ProcessAttributes (XPathNavigator nav)
315                 {
316                         ProcessChildren (nav, "attributes//attribute", new OnChildren (OnAttribute));
317                 }
318
319                 void ProcessNamespaces (XPathNavigator nav)
320                 {
321                         ProcessChildren (nav, "namespaces//namespace", new OnChildren (OnNamespace));
322                 }
323
324                 void ProcessClasses (XPathNavigator nav)
325                 {
326                         ProcessChildren (nav, "classes//class", new OnChildren (OnClass));
327                 }
328
329                 void ProcessInterfaces (XPathNavigator nav)
330                 {
331                         ProcessChildren (nav, "intefaces//interface", new OnChildren (OnInterface));
332                 }
333
334                 void ProcessFields (XPathNavigator nav)
335                 {
336                         ProcessChildren (nav, "fields//field", new OnChildren (OnField));
337                 }
338
339                 void ProcessMethods (XPathNavigator nav)
340                 {
341                         ProcessChildren (nav, "methods//method", new OnChildren (OnMethod));
342                 }
343
344                 void ProcessConstructors (XPathNavigator nav)
345                 {
346                         ProcessChildren (nav, "constructors//constructor", new OnChildren (OnConstructor));
347                 }
348
349                 void ProcessParameters (XPathNavigator nav)
350                 {
351                         ProcessChildren (nav, "parameters//parameter", new OnChildren (OnParameter));
352                 }
353
354                 void ProcessProperties (XPathNavigator nav)
355                 {
356                         ProcessChildren (nav, "properties//property", new OnChildren (OnProperty));
357                 }
358
359                 void ProcessEvents (XPathNavigator nav)
360                 {
361                         ProcessChildren (nav, "events//event", new OnChildren (OnEvent));
362                 }
363
364                 static void ProcessChildren (XPathNavigator nav, string children, OnChildren action)
365                 {
366                         XPathNodeIterator iterator = nav.Select (children);
367                         while (iterator.MoveNext ())
368                                 action (iterator.Current);
369                 }
370
371                 delegate void OnChildren (XPathNavigator nav);
372
373                 static string GetName (XPathNavigator nav)
374                 {
375                         return GetAttribute (nav, _name);
376                 }
377
378                 static string GetAttribute (XPathNavigator nav, string attribute)
379                 {
380                         return nav.GetAttribute (attribute, _ns);
381                 }
382         }
383 }