Merge pull request #730 from LogosBible/locale-changes
[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
41                 protected override void Process ()
42                 {
43                         assemblies = Context.GetAssemblies ();
44                         foreach (var assembly in assemblies)
45                                 SweepAssembly (assembly);
46                 }
47
48                 void SweepAssembly (AssemblyDefinition assembly)
49                 {
50                         if (Annotations.GetAction (assembly) != AssemblyAction.Link)
51                                 return;
52
53                         if (!IsMarkedAssembly (assembly)) {
54                                 RemoveAssembly (assembly);
55                                 return;
56                         }
57
58                         var types = new List<TypeDefinition> ();
59
60                         foreach (TypeDefinition type in assembly.MainModule.Types) {
61                                 if (Annotations.IsMarked (type)) {
62                                         SweepType (type);
63                                         types.Add (type);
64                                         continue;
65                                 }
66
67                                 if (type.Name == "<Module>")
68                                         types.Add (type);
69                         }
70
71                         assembly.MainModule.Types.Clear ();
72                         foreach (TypeDefinition type in types)
73                                 assembly.MainModule.Types.Add (type);
74                 }
75
76                 bool IsMarkedAssembly (AssemblyDefinition assembly)
77                 {
78                         return Annotations.IsMarked (assembly.MainModule);
79                 }
80
81                 void RemoveAssembly (AssemblyDefinition assembly)
82                 {
83                         Annotations.SetAction (assembly, AssemblyAction.Delete);
84
85                         SweepReferences (assembly);
86                 }
87
88                 void SweepReferences (AssemblyDefinition target)
89                 {
90                         foreach (var assembly in assemblies)
91                                 SweepReferences (assembly, target);
92                 }
93
94                 void SweepReferences (AssemblyDefinition assembly, AssemblyDefinition target)
95                 {
96                         var references = assembly.MainModule.AssemblyReferences;
97                         for (int i = 0; i < references.Count; i++) {
98                                 var reference = references [i];
99                                 if (!AreSameReference (reference, target.Name))
100                                         continue;
101
102                                 references.RemoveAt (i);
103                                 // Removing the reference does not mean it will be saved back to disk!
104                                 // That depends on the AssemblyAction set for the `assembly`
105                                 if (Annotations.GetAction (assembly) == AssemblyAction.Copy) {
106                                         // Copy means even if "unlinked" we still want that assembly to be saved back 
107                                         // to disk (OutputStep) without the (removed) reference
108                                         Annotations.SetAction (assembly, AssemblyAction.Save);
109                                         // note: we only enter here (Copy->Save once) so we nned to do the complete job
110                                         foreach (TypeReference tr in assembly.MainModule.GetTypeReferences ()) {
111                                                 var td = tr.Resolve ();
112                                                 // at this stage reference might include things that can't be resolved
113                                                 tr.Scope = td == null ? null : assembly.MainModule.Import (td).Scope;
114                                         }
115                                 }
116                                 return;
117                         }
118                 }
119
120                 void SweepType (TypeDefinition type)
121                 {
122                         if (type.HasFields)
123                                 SweepCollection (type.Fields);
124
125                         if (type.HasMethods)
126                                 SweepCollection (type.Methods);
127
128                         if (type.HasNestedTypes)
129                                 SweepNestedTypes (type);
130                 }
131
132                 void SweepNestedTypes (TypeDefinition type)
133                 {
134                         for (int i = 0; i < type.NestedTypes.Count; i++) {
135                                 var nested = type.NestedTypes [i];
136                                 if (Annotations.IsMarked (nested)) {
137                                         SweepType (nested);
138                                 } else {
139                                         type.NestedTypes.RemoveAt (i--);
140                                 }
141                         }
142                 }
143
144                 void SweepCollection (IList list)
145                 {
146                         for (int i = 0; i < list.Count; i++)
147                                 if (!Annotations.IsMarked ((IMetadataTokenProvider) list [i]))
148                                         list.RemoveAt (i--);
149                 }
150
151                 static bool AreSameReference (AssemblyNameReference a, AssemblyNameReference b)
152                 {
153                         if (a == b)
154                                 return true;
155
156                         if (a.Name != b.Name)
157                                 return false;
158
159                         if (a.Version > b.Version)
160                                 return false;
161
162                         return true;
163                 }
164         }
165 }