Merge pull request #794 from aakashapoorv/patch-1
[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                         var references = assembly.MainModule.AssemblyReferences;
98                         for (int i = 0; i < references.Count; i++) {
99                                 var reference = references [i];
100                                 if (!AreSameReference (reference, target.Name))
101                                         continue;
102
103                                 references.RemoveAt (i);
104                                 // Removing the reference does not mean it will be saved back to disk!
105                                 // That depends on the AssemblyAction set for the `assembly`
106                                 switch (Annotations.GetAction (assembly)) {
107                                 case AssemblyAction.Copy:
108                                         // Copy means even if "unlinked" we still want that assembly to be saved back 
109                                         // to disk (OutputStep) without the (removed) reference
110                                         Annotations.SetAction (assembly, AssemblyAction.Save);
111                                         ResolveAllTypeReferences (assembly);
112                                         break;
113
114                                 case AssemblyAction.Save:
115                                 case AssemblyAction.Link:
116                                         ResolveAllTypeReferences (assembly);
117                                         break;
118                                 }
119                                 return;
120                         }
121                 }
122
123                 void ResolveAllTypeReferences (AssemblyDefinition assembly)
124                 {
125                         if (resolvedTypeReferences == null)
126                                 resolvedTypeReferences = new HashSet<AssemblyDefinition> ();
127                         if (resolvedTypeReferences.Contains (assembly))
128                                 return;
129                         resolvedTypeReferences.Add (assembly);
130
131                         foreach (TypeReference tr in assembly.MainModule.GetTypeReferences ()) {
132                                 var td = tr.Resolve ();
133                                 // at this stage reference might include things that can't be resolved
134                                 tr.Scope = td == null ? null : assembly.MainModule.Import (td).Scope;
135                         }
136                 }
137
138                 void SweepType (TypeDefinition type)
139                 {
140                         if (type.HasFields)
141                                 SweepCollection (type.Fields);
142
143                         if (type.HasMethods)
144                                 SweepCollection (type.Methods);
145
146                         if (type.HasNestedTypes)
147                                 SweepNestedTypes (type);
148                 }
149
150                 void SweepNestedTypes (TypeDefinition type)
151                 {
152                         for (int i = 0; i < type.NestedTypes.Count; i++) {
153                                 var nested = type.NestedTypes [i];
154                                 if (Annotations.IsMarked (nested)) {
155                                         SweepType (nested);
156                                 } else {
157                                         type.NestedTypes.RemoveAt (i--);
158                                 }
159                         }
160                 }
161
162                 void SweepCollection (IList list)
163                 {
164                         for (int i = 0; i < list.Count; i++)
165                                 if (!Annotations.IsMarked ((IMetadataTokenProvider) list [i]))
166                                         list.RemoveAt (i--);
167                 }
168
169                 static bool AreSameReference (AssemblyNameReference a, AssemblyNameReference b)
170                 {
171                         if (a == b)
172                                 return true;
173
174                         if (a.Name != b.Name)
175                                 return false;
176
177                         if (a.Version > b.Version)
178                                 return false;
179
180                         return true;
181                 }
182         }
183 }