5e9f681fd1784d25dfd201c93fc913c409c6fa9f
[mono.git] / mcs / tools / linker / Mono.Linker.Steps / ResolveFromXmlStep.cs
1 //
2 // ResolveFromXmlStep.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@gmail.com)
6 //
7 // (C) 2006 Jb Evain
8 // (C) 2007 Novell, Inc.
9 // Copyright 2013 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 SR = System.Reflection;
33 using System.Text;
34 using System.Text.RegularExpressions;
35 using System.Xml.XPath;
36
37 using Mono.Cecil;
38
39 namespace Mono.Linker.Steps {
40
41         public class XmlResolutionException : Exception {
42                 public XmlResolutionException (string message, Exception innerException)
43                         : base (message, innerException)
44                 {
45                 }
46         }
47
48         public class ResolveFromXmlStep : ResolveStep {
49
50                 static readonly string _signature = "signature";
51                 static readonly string _fullname = "fullname";
52                 static readonly string _required = "required";
53                 static readonly string _preserve = "preserve";
54                 static readonly string _ns = string.Empty;
55
56                 XPathDocument _document;
57                 string _xmlDocumentLocation;
58
59                 public ResolveFromXmlStep (XPathDocument document, string xmlDocumentLocation = "<unspecified>")
60                 {
61                         _document = document;
62                         _xmlDocumentLocation = xmlDocumentLocation;
63                 }
64
65                 protected override void Process ()
66                 {
67                         XPathNavigator nav = _document.CreateNavigator ();
68                         nav.MoveToFirstChild ();
69
70                         // This step can be created with XML files that aren't necessarily
71                         // linker descriptor files. So bail if we don't have a <linker> element.
72                         if (nav.LocalName != "linker")
73                                 return;
74
75                         try {
76                                 ProcessAssemblies (Context, nav.SelectChildren ("assembly", _ns));
77                         } catch (Exception ex) {
78                                 throw new XmlResolutionException (string.Format ("Failed to process XML description: {0}", _xmlDocumentLocation), ex);
79                         }
80                 }
81
82                 void ProcessAssemblies (LinkContext context, XPathNodeIterator iterator)
83                 {
84                         while (iterator.MoveNext ()) {
85                                 AssemblyDefinition assembly = GetAssembly (context, GetFullName (iterator.Current));
86                                 ProcessTypes (assembly, iterator.Current.SelectChildren ("type", _ns));
87                                 ProcessNamespaces (assembly, iterator.Current.SelectChildren ("namespace", _ns));
88                         }
89                 }
90
91                 void ProcessNamespaces (AssemblyDefinition assembly, XPathNodeIterator iterator)
92                 {
93                         while (iterator.MoveNext ()) {
94                                 string fullname = GetFullName (iterator.Current);
95                                 foreach (TypeDefinition type in assembly.MainModule.Types) {
96                                         if (type.Namespace != fullname)
97                                                 continue;
98
99                                         MarkAndPreserveAll (type);
100                                 }
101                         }
102                 }
103
104                 void MarkAndPreserveAll (TypeDefinition type)
105                 {
106                         Annotations.Mark (type);
107                         Annotations.SetPreserve (type, TypePreserve.All);
108
109                         if (!type.HasNestedTypes)
110                                 return;
111
112                         foreach (TypeDefinition nested in type.NestedTypes)
113                                 MarkAndPreserveAll (nested);
114                 }
115
116                 void ProcessTypes (AssemblyDefinition assembly, XPathNodeIterator iterator)
117                 {
118                         while (iterator.MoveNext ()) {
119                                 XPathNavigator nav = iterator.Current;
120                                 string fullname = GetFullName (nav);
121
122                                 if (IsTypePattern (fullname)) {
123                                         ProcessTypePattern (fullname, assembly, nav);
124                                         continue;
125                                 }
126
127                                 TypeDefinition type = assembly.MainModule.GetType (fullname);
128                                 if (type == null)
129                                         continue;
130
131                                 ProcessType (type, nav);
132                         }
133                 }
134
135                 static bool IsTypePattern (string fullname)
136                 {
137                         return fullname.IndexOf ("*") != -1;
138                 }
139
140                 static Regex CreateRegexFromPattern (string pattern)
141                 {
142                         return new Regex (pattern.Replace(".", @"\.").Replace("*", "(.*)"));
143                 }
144
145                 void MatchType (TypeDefinition type, Regex regex, XPathNavigator nav)
146                 {
147                         if (regex.Match (type.FullName).Success)
148                                 ProcessType (type, nav);
149
150                         if (!type.HasNestedTypes)
151                                 return;
152
153                         foreach (var nt in type.NestedTypes)
154                                 MatchType (nt, regex, nav);
155                 }
156
157                 void ProcessTypePattern (string fullname, AssemblyDefinition assembly, XPathNavigator nav)
158                 {
159                         Regex regex = CreateRegexFromPattern (fullname);
160
161                         foreach (TypeDefinition type in assembly.MainModule.Types) {
162                                 MatchType (type, regex, nav);
163                         }
164                 }
165
166                 void ProcessType (TypeDefinition type, XPathNavigator nav)
167                 {
168                         TypePreserve preserve = GetTypePreserve (nav);
169
170                         if (!IsRequired (nav)) {
171                                 Annotations.SetPreserve (type, preserve);
172                                 return;
173                         }
174
175                         Annotations.Mark (type);
176
177                         switch (preserve) {
178                         case TypePreserve.Nothing:
179                                 if (!nav.HasChildren)
180                                         Annotations.SetPreserve (type, TypePreserve.All);
181                                 break;
182                         default:
183                                 Annotations.SetPreserve (type, preserve);
184                                 break;
185                         }
186
187                         if (nav.HasChildren) {
188                                 MarkSelectedFields (nav, type);
189                                 MarkSelectedMethods (nav, type);
190                         }
191                 }
192
193                 void MarkSelectedFields (XPathNavigator nav, TypeDefinition type)
194                 {
195                         XPathNodeIterator fields = nav.SelectChildren ("field", _ns);
196                         if (fields.Count == 0)
197                                 return;
198
199                         ProcessFields (type, fields);
200                 }
201
202                 void MarkSelectedMethods (XPathNavigator nav, TypeDefinition type)
203                 {
204                         XPathNodeIterator methods = nav.SelectChildren ("method", _ns);
205                         if (methods.Count == 0)
206                                 return;
207
208                         ProcessMethods (type, methods);
209                 }
210
211                 static TypePreserve GetTypePreserve (XPathNavigator nav)
212                 {
213                         string attribute = GetAttribute (nav, _preserve);
214                         if (attribute == null || attribute.Length == 0)
215                                 return TypePreserve.Nothing;
216
217                         try {
218                                 return (TypePreserve) Enum.Parse (typeof (TypePreserve), attribute, true);
219                         } catch {
220                                 return TypePreserve.Nothing;
221                         }
222                 }
223
224                 void ProcessFields (TypeDefinition type, XPathNodeIterator iterator)
225                 {
226                         while (iterator.MoveNext ()) {
227                                 string value = GetSignature (iterator.Current);
228                                 if (!String.IsNullOrEmpty (value))
229                                         ProcessFieldSignature (type, value);
230
231                                 value = GetAttribute (iterator.Current, "name");
232                                 if (!String.IsNullOrEmpty (value))
233                                         ProcessFieldName (type, value);
234                         }
235                 }
236
237                 void ProcessFieldSignature (TypeDefinition type, string signature)
238                 {
239                         FieldDefinition field = GetField (type, signature);
240                         MarkField (type, field, signature);
241                 }
242
243                 void MarkField (TypeDefinition type, FieldDefinition field, string signature)
244                 {
245                         if (field != null)
246                                 Annotations.Mark (field);
247                         else
248                                 AddUnresolveMarker (string.Format ("T: {0}; F: {1}", type, signature));
249                 }
250
251                 void ProcessFieldName (TypeDefinition type, string name)
252                 {
253                         if (!type.HasFields)
254                                 return;
255
256                         foreach (FieldDefinition field in type.Fields)
257                                 if (field.Name == name)
258                                         MarkField (type, field, name);
259                 }
260
261                 static FieldDefinition GetField (TypeDefinition type, string signature)
262                 {
263                         if (!type.HasFields)
264                                 return null;
265
266                         foreach (FieldDefinition field in type.Fields)
267                                 if (signature == GetFieldSignature (field))
268                                         return field;
269
270                         return null;
271                 }
272
273                 static string GetFieldSignature (FieldDefinition field)
274                 {
275                         return field.FieldType.FullName + " " + field.Name;
276                 }
277
278                 void ProcessMethods (TypeDefinition type, XPathNodeIterator iterator)
279                 {
280                         while (iterator.MoveNext()) {
281                                 string value = GetSignature (iterator.Current);
282                                 if (!String.IsNullOrEmpty (value))
283                                         ProcessMethodSignature (type, value);
284
285                                 value = GetAttribute (iterator.Current, "name");
286                                 if (!String.IsNullOrEmpty (value))
287                                         ProcessMethodName (type, value);
288                         }
289                 }
290
291                 void ProcessMethodSignature (TypeDefinition type, string signature)
292                 {
293                         MethodDefinition meth = GetMethod (type, signature);
294                         MarkMethod (type, meth, signature);
295                 }
296
297                 void MarkMethod (TypeDefinition type, MethodDefinition method, string signature)
298                 {
299                         if (method != null) {
300                                 Annotations.Mark (method);
301                                 Annotations.SetAction (method, MethodAction.Parse);
302                         } else
303                                 AddUnresolveMarker (string.Format ("T: {0}; M: {1}", type, signature));
304                 }
305
306                 void ProcessMethodName (TypeDefinition type, string name)
307                 {
308                         if (!type.HasMethods)
309                                 return;
310
311                         foreach (MethodDefinition method in type.Methods)
312                                 if (name == method.Name)
313                                         MarkMethod (type, method, name);
314                 }
315
316                 static MethodDefinition GetMethod (TypeDefinition type, string signature)
317                 {
318                         if (type.HasMethods)
319                                 foreach (MethodDefinition meth in type.Methods)
320                                         if (signature == GetMethodSignature (meth))
321                                                 return meth;
322
323                         return null;
324                 }
325
326                 static string GetMethodSignature (MethodDefinition meth)
327                 {
328                         StringBuilder sb = new StringBuilder ();
329                         sb.Append (meth.ReturnType.FullName);
330                         sb.Append (" ");
331                         sb.Append (meth.Name);
332                         sb.Append ("(");
333                         if (meth.HasParameters) {
334                                 for (int i = 0; i < meth.Parameters.Count; i++) {
335                                         if (i > 0)
336                                                 sb.Append (",");
337
338                                         sb.Append (meth.Parameters [i].ParameterType.FullName);
339                                 }
340                         }
341                         sb.Append (")");
342                         return sb.ToString ();
343                 }
344
345                 static AssemblyDefinition GetAssembly (LinkContext context, string assemblyName)
346                 {
347                         AssemblyNameReference reference = AssemblyNameReference.Parse (assemblyName);
348                         AssemblyDefinition assembly;
349
350                         assembly = context.Resolve (reference);
351
352                         ProcessReferences (assembly, context);
353                         return assembly;
354                 }
355
356                 static void ProcessReferences (AssemblyDefinition assembly, LinkContext context)
357                 {
358                         foreach (AssemblyNameReference name in assembly.MainModule.AssemblyReferences)
359                                 context.Resolve (name);
360                 }
361
362                 static bool IsRequired (XPathNavigator nav)
363                 {
364                         string attribute = GetAttribute (nav, _required);
365                         if (attribute == null || attribute.Length == 0)
366                                 return true;
367
368                         return TryParseBool (attribute);
369                 }
370
371                 static bool TryParseBool (string s)
372                 {
373                         try {
374                                 return bool.Parse (s);
375                         } catch {
376                                 return false;
377                         }
378                 }
379
380                 static string GetSignature (XPathNavigator nav)
381                 {
382                         return GetAttribute (nav, _signature);
383                 }
384
385                 static string GetFullName (XPathNavigator nav)
386                 {
387                         return GetAttribute (nav, _fullname);
388                 }
389
390                 static string GetAttribute (XPathNavigator nav, string attribute)
391                 {
392                         return nav.GetAttribute (attribute, _ns);
393                 }
394         }
395 }