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;
33 using Mono.Collections.Generic;
35 namespace Mono.Linker.Steps {
37 public class SweepStep : BaseStep {
39 AssemblyDefinition [] assemblies;
40 HashSet<AssemblyDefinition> resolvedTypeReferences;
42 protected override void Process ()
44 assemblies = Context.GetAssemblies ();
45 foreach (var assembly in assemblies)
46 SweepAssembly (assembly);
49 void SweepAssembly (AssemblyDefinition assembly)
51 if (Annotations.GetAction (assembly) != AssemblyAction.Link)
54 if (!IsMarkedAssembly (assembly)) {
55 RemoveAssembly (assembly);
59 var types = new List<TypeDefinition> ();
61 foreach (TypeDefinition type in assembly.MainModule.Types) {
62 if (Annotations.IsMarked (type)) {
68 if (type.Name == "<Module>")
72 assembly.MainModule.Types.Clear ();
73 foreach (TypeDefinition type in types)
74 assembly.MainModule.Types.Add (type);
77 bool IsMarkedAssembly (AssemblyDefinition assembly)
79 return Annotations.IsMarked (assembly.MainModule);
82 void RemoveAssembly (AssemblyDefinition assembly)
84 Annotations.SetAction (assembly, AssemblyAction.Delete);
86 SweepReferences (assembly);
89 void SweepReferences (AssemblyDefinition target)
91 foreach (var assembly in assemblies)
92 SweepReferences (assembly, target);
95 void SweepReferences (AssemblyDefinition assembly, AssemblyDefinition target)
97 if (assembly == target)
100 var references = assembly.MainModule.AssemblyReferences;
101 for (int i = 0; i < references.Count; i++) {
102 var reference = references [i];
103 var r = Context.Resolver.Resolve (reference);
104 if (!AreSameReference (r.Name, target.Name))
107 references.RemoveAt (i);
108 // Removing the reference does not mean it will be saved back to disk!
109 // That depends on the AssemblyAction set for the `assembly`
110 switch (Annotations.GetAction (assembly)) {
111 case AssemblyAction.Copy:
112 // Copy means even if "unlinked" we still want that assembly to be saved back
113 // to disk (OutputStep) without the (removed) reference
114 Annotations.SetAction (assembly, AssemblyAction.Save);
115 ResolveAllTypeReferences (assembly);
118 case AssemblyAction.Save:
119 case AssemblyAction.Link:
120 ResolveAllTypeReferences (assembly);
127 void ResolveAllTypeReferences (AssemblyDefinition assembly)
129 if (resolvedTypeReferences == null)
130 resolvedTypeReferences = new HashSet<AssemblyDefinition> ();
131 if (resolvedTypeReferences.Contains (assembly))
133 resolvedTypeReferences.Add (assembly);
135 var hash = new Dictionary<TypeReference,IMetadataScope> ();
137 foreach (TypeReference tr in assembly.MainModule.GetTypeReferences ()) {
138 if (hash.ContainsKey (tr))
140 var td = tr.Resolve ();
141 IMetadataScope scope = tr.Scope;
142 // at this stage reference might include things that can't be resolved
143 // and if it is (resolved) it needs to be kept only if marked (#16213)
144 if ((td != null) && Annotations.IsMarked (td))
145 scope = assembly.MainModule.Import (td).Scope;
146 hash.Add (tr, scope);
148 if (assembly.MainModule.HasExportedTypes) {
149 foreach (var et in assembly.MainModule.ExportedTypes) {
150 var td = et.Resolve ();
151 IMetadataScope scope = et.Scope;
152 if ((td != null) && Annotations.IsMarked (td)) {
153 scope = assembly.MainModule.Import (td).Scope;
154 hash.Add (td, scope);
159 // Resolve everything first before updating scopes.
160 // If we set the scope to null, then calling Resolve() on any of its
161 // nested types would crash.
163 foreach (var e in hash) {
164 e.Key.Scope = e.Value;
168 void SweepType (TypeDefinition type)
171 SweepCollection (type.Fields);
174 SweepCollection (type.Methods);
176 if (type.HasNestedTypes)
177 SweepNestedTypes (type);
180 void SweepNestedTypes (TypeDefinition type)
182 for (int i = 0; i < type.NestedTypes.Count; i++) {
183 var nested = type.NestedTypes [i];
184 if (Annotations.IsMarked (nested)) {
187 type.NestedTypes.RemoveAt (i--);
192 void SweepCollection (IList list)
194 for (int i = 0; i < list.Count; i++)
195 if (!Annotations.IsMarked ((IMetadataTokenProvider) list [i]))
199 static bool AreSameReference (AssemblyNameReference a, AssemblyNameReference b)
204 if (a.Name != b.Name)
207 if (a.Version > b.Version)