Merge pull request #1624 from esdrubal/getprocesstimes
[mono.git] / mcs / tools / linker / Mono.Linker.Steps / SweepStep.cs
1 //
2 // SweepStep.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@gmail.com)
6 //
7 // (C) 2006 Jb Evain
8 // (C) 2007 Novell, Inc.
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Collections;
31 using System.Collections.Generic;
32 using Mono.Cecil;
33 using Mono.Collections.Generic;
34
35 namespace Mono.Linker.Steps {
36
37         public class SweepStep : BaseStep {
38
39                 AssemblyDefinition [] assemblies;
40                 HashSet<AssemblyDefinition> resolvedTypeReferences;
41
42                 protected override void Process ()
43                 {
44                         assemblies = Context.GetAssemblies ();
45                         foreach (var assembly in assemblies)
46                                 SweepAssembly (assembly);
47                 }
48
49                 void SweepAssembly (AssemblyDefinition assembly)
50                 {
51                         if (Annotations.GetAction (assembly) != AssemblyAction.Link)
52                                 return;
53
54                         if (!IsMarkedAssembly (assembly)) {
55                                 RemoveAssembly (assembly);
56                                 return;
57                         }
58
59                         var types = new List<TypeDefinition> ();
60
61                         foreach (TypeDefinition type in assembly.MainModule.Types) {
62                                 if (Annotations.IsMarked (type)) {
63                                         SweepType (type);
64                                         types.Add (type);
65                                         continue;
66                                 }
67
68                                 if (type.Name == "<Module>")
69                                         types.Add (type);
70                         }
71
72                         assembly.MainModule.Types.Clear ();
73                         foreach (TypeDefinition type in types)
74                                 assembly.MainModule.Types.Add (type);
75                 }
76
77                 bool IsMarkedAssembly (AssemblyDefinition assembly)
78                 {
79                         return Annotations.IsMarked (assembly.MainModule);
80                 }
81
82                 void RemoveAssembly (AssemblyDefinition assembly)
83                 {
84                         Annotations.SetAction (assembly, AssemblyAction.Delete);
85
86                         SweepReferences (assembly);
87                 }
88
89                 void SweepReferences (AssemblyDefinition target)
90                 {
91                         foreach (var assembly in assemblies)
92                                 SweepReferences (assembly, target);
93                 }
94
95                 void SweepReferences (AssemblyDefinition assembly, AssemblyDefinition target)
96                 {
97                         if (assembly == target)
98                                 return;
99
100                         var references = assembly.MainModule.AssemblyReferences;
101                         for (int i = 0; i < references.Count; i++) {
102                                 var reference = references [i];
103                                 if (!AreSameReference (reference, target.Name))
104                                         continue;
105
106                                 references.RemoveAt (i);
107                                 // Removing the reference does not mean it will be saved back to disk!
108                                 // That depends on the AssemblyAction set for the `assembly`
109                                 switch (Annotations.GetAction (assembly)) {
110                                 case AssemblyAction.Copy:
111                                         // Copy means even if "unlinked" we still want that assembly to be saved back 
112                                         // to disk (OutputStep) without the (removed) reference
113                                         Annotations.SetAction (assembly, AssemblyAction.Save);
114                                         ResolveAllTypeReferences (assembly);
115                                         break;
116
117                                 case AssemblyAction.Save:
118                                 case AssemblyAction.Link:
119                                         ResolveAllTypeReferences (assembly);
120                                         break;
121                                 }
122                                 return;
123                         }
124                 }
125
126                 void ResolveAllTypeReferences (AssemblyDefinition assembly)
127                 {
128                         if (resolvedTypeReferences == null)
129                                 resolvedTypeReferences = new HashSet<AssemblyDefinition> ();
130                         if (resolvedTypeReferences.Contains (assembly))
131                                 return;
132                         resolvedTypeReferences.Add (assembly);
133
134                         var hash = new Dictionary<TypeReference,IMetadataScope> ();
135
136                         foreach (TypeReference tr in assembly.MainModule.GetTypeReferences ()) {
137                                 if (hash.ContainsKey (tr))
138                                         continue;
139                                 var td = tr.Resolve ();
140                                 IMetadataScope scope = tr.Scope;
141                                 // at this stage reference might include things that can't be resolved
142                                 // and if it is (resolved) it needs to be kept only if marked (#16213)
143                                 if ((td != null) && Annotations.IsMarked (td))
144                                         scope = assembly.MainModule.Import (td).Scope;
145                                 hash.Add (tr, scope);
146                         }
147                         if (assembly.MainModule.HasExportedTypes) {
148                                 foreach (var et in assembly.MainModule.ExportedTypes) {
149                                         var td = et.Resolve ();
150                                         IMetadataScope scope = et.Scope;
151                                         if ((td != null) && Annotations.IsMarked (td)) {
152                                                 scope = assembly.MainModule.Import (td).Scope;
153                                                 hash.Add (td, scope);
154                                         }
155                                 }
156                         }
157
158                         // Resolve everything first before updating scopes.
159                         // If we set the scope to null, then calling Resolve() on any of its
160                         // nested types would crash.
161
162                         foreach (var e in hash) {
163                                 e.Key.Scope = e.Value;
164                         }
165                 }
166
167                 void SweepType (TypeDefinition type)
168                 {
169                         if (type.HasFields)
170                                 SweepCollection (type.Fields);
171
172                         if (type.HasMethods)
173                                 SweepCollection (type.Methods);
174
175                         if (type.HasNestedTypes)
176                                 SweepNestedTypes (type);
177                 }
178
179                 void SweepNestedTypes (TypeDefinition type)
180                 {
181                         for (int i = 0; i < type.NestedTypes.Count; i++) {
182                                 var nested = type.NestedTypes [i];
183                                 if (Annotations.IsMarked (nested)) {
184                                         SweepType (nested);
185                                 } else {
186                                         type.NestedTypes.RemoveAt (i--);
187                                 }
188                         }
189                 }
190
191                 void SweepCollection (IList list)
192                 {
193                         for (int i = 0; i < list.Count; i++)
194                                 if (!Annotations.IsMarked ((IMetadataTokenProvider) list [i]))
195                                         list.RemoveAt (i--);
196                 }
197
198                 static bool AreSameReference (AssemblyNameReference a, AssemblyNameReference b)
199                 {
200                         if (a == b)
201                                 return true;
202
203                         if (a.Name != b.Name)
204                                 return false;
205
206                         if (a.Version > b.Version)
207                                 return false;
208
209                         return true;
210                 }
211         }
212 }