5 // Jb Evain (jbevain@gmail.com)
8 // (C) 2007 Novell, Inc.
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections;
31 using System.Collections.Generic;
34 namespace Mono.Linker.Steps {
36 public class SweepStep : BaseStep {
38 AssemblyDefinition [] assemblies;
40 protected override void Process ()
42 assemblies = Context.GetAssemblies ();
43 foreach (var assembly in assemblies)
44 SweepAssembly (assembly);
47 void SweepAssembly (AssemblyDefinition assembly)
49 if (Annotations.GetAction (assembly) != AssemblyAction.Link)
52 if (!IsMarkedAssembly (assembly)) {
53 RemoveAssembly (assembly);
57 var types = assembly.MainModule.Types;
58 var cloned_types = new List<TypeDefinition> (types);
62 foreach (TypeDefinition type in cloned_types) {
63 if (Annotations.IsMarked (type)) {
69 if (type.Name == "<Module>")
74 bool IsMarkedAssembly (AssemblyDefinition assembly)
76 return Annotations.IsMarked (assembly.MainModule);
79 void RemoveAssembly (AssemblyDefinition assembly)
81 Annotations.SetAction (assembly, AssemblyAction.Delete);
83 SweepReferences (assembly);
86 void SweepReferences (AssemblyDefinition target)
88 foreach (var assembly in assemblies)
89 SweepReferences (assembly, target);
92 void SweepReferences (AssemblyDefinition assembly, AssemblyDefinition target)
94 var references = assembly.MainModule.AssemblyReferences;
95 for (int i = 0; i < references.Count; i++) {
96 var reference = references [i];
97 if (!AreSameReference (reference, target.Name))
100 references.RemoveAt (i);
105 static ICollection Clone (ICollection collection)
107 return new ArrayList (collection);
110 void SweepType (TypeDefinition type)
113 SweepCollection (type.Fields);
116 SweepCollection (type.Methods);
118 if (type.HasNestedTypes)
119 SweepNestedTypes (type);
122 void SweepNestedTypes (TypeDefinition type)
124 for (int i = 0; i < type.NestedTypes.Count; i++) {
125 var nested = type.NestedTypes [i];
126 if (Annotations.IsMarked (nested)) {
129 type.NestedTypes.RemoveAt (i--);
134 void SweepCollection (IList list)
136 for (int i = 0; i < list.Count; i++)
137 if (!Annotations.IsMarked ((IMetadataTokenProvider) list [i]))
141 static bool AreSameReference (AssemblyNameReference a, AssemblyNameReference b)
146 if (a.Name != b.Name)
149 if (a.Version != b.Version)