bdb04a94db239e4935bd20139ca2fc0392e443f6
[mono.git] / mcs / tools / tuner / Mono.Tuner / PreserveSoapHttpClients.cs
1 using System;
2
3 using Mono.Linker;
4
5 using Mono.Cecil;
6
7 namespace Mono.Tuner {
8
9         public class PreserveSoapHttpClients : BaseSubStep {
10
11                 public override SubStepTargets Targets {
12                         get { return SubStepTargets.Type; }
13                 }
14
15                 public override bool IsActiveFor (AssemblyDefinition assembly)
16                 {
17                         return Annotations.GetAction (assembly) == AssemblyAction.Link && !Profile.IsSdkAssembly (assembly);
18                 }
19
20                 public override void ProcessType (TypeDefinition type)
21                 {
22                         if (IsWebServiceClient (type))
23                                 PreserveClient (type);
24                 }
25
26                 void PreserveClient (TypeDefinition type)
27                 {
28                         if (!type.HasMethods)
29                                 return;
30
31                         foreach (MethodDefinition method in type.Methods) {
32                                 string sync_method;
33                                 if (!TryExtractSyncMethod (method, out sync_method))
34                                         continue;
35
36                                 AddPreservedMethod (method, sync_method);
37                         }
38                 }
39
40                 void AddPreservedMethod (MethodDefinition target, string methodName)
41                 {
42                         foreach (MethodDefinition method in target.DeclaringType.Methods)
43                                 if (method.Name == methodName)
44                                         Annotations.AddPreservedMethod (target, method);
45                 }
46
47                 static bool TryExtractSyncMethod (MethodDefinition method, out string sync_method)
48                 {
49                         if (TryExtractPrefixedMethodName ("Begin", method.Name, out sync_method))
50                                 return true;
51
52                         if (TryExtractPrefixedMethodName ("End", method.Name, out sync_method))
53                                 return true;
54
55                         if (TryExtractSuffixedMethodName ("Async", method.Name, out sync_method))
56                                 return true;
57
58                         return false;
59                 }
60
61                 static bool TryExtractPrefixedMethodName (string prefix, string fullName, out string methodName)
62                 {
63                         methodName = null;
64
65                         int pos = fullName.IndexOf (prefix, StringComparison.Ordinal);
66                         if (pos == -1)
67                                 return false;
68
69                         methodName = fullName.Substring (prefix.Length);
70                         return true;
71                 }
72
73                 static bool TryExtractSuffixedMethodName (string suffix, string fullName, out string methodName)
74                 {
75                         methodName = null;
76
77                         int pos = fullName.LastIndexOf (suffix, StringComparison.Ordinal);
78                         if (pos == -1)
79                                 return false;
80
81                         methodName = fullName.Substring (0, pos);
82                         return true;
83                 }
84
85                 static bool IsWebServiceClient (TypeDefinition type)
86                 {
87                         return type.Inherits ("System.Web.Services.Protocols", "SoapHttpClientProtocol");
88                 }
89         }
90 }