[w32handle] Stop returning 0 in every cases for locking/unlocking (#3926)
[mono.git] / mcs / tools / linker / Mono.Linker.Steps / SweepStep.cs
index 552693546beebd33a082fc9ed2384ec68b349d4a..73c89cc7991f9c770da19dce0a6fa69deb7474e4 100644 (file)
@@ -42,8 +42,17 @@ namespace Mono.Linker.Steps {
                protected override void Process ()
                {
                        assemblies = Context.GetAssemblies ();
-                       foreach (var assembly in assemblies)
+                       foreach (var assembly in assemblies) {
                                SweepAssembly (assembly);
+                               if (Annotations.GetAction (assembly) == AssemblyAction.Copy) {
+                                       // Copy assemblies can still contain Type references with
+                                       // type forwarders from Delete assemblies
+                                       // thus try to resolve all the type references and see
+                                       // if some changed the scope. if yes change the action to Save
+                                       if (ResolveAllTypeReferences (assembly))
+                                               Annotations.SetAction (assembly, AssemblyAction.Save);
+                               }
+                       }
                }
 
                void SweepAssembly (AssemblyDefinition assembly)
@@ -94,10 +103,14 @@ namespace Mono.Linker.Steps {
 
                void SweepReferences (AssemblyDefinition assembly, AssemblyDefinition target)
                {
+                       if (assembly == target)
+                               return;
+
                        var references = assembly.MainModule.AssemblyReferences;
                        for (int i = 0; i < references.Count; i++) {
                                var reference = references [i];
-                               if (!AreSameReference (reference, target.Name))
+                               var r = Context.Resolver.Resolve (reference);
+                               if (!AreSameReference (r.Name, target.Name))
                                        continue;
 
                                references.RemoveAt (i);
@@ -120,23 +133,40 @@ namespace Mono.Linker.Steps {
                        }
                }
 
-               void ResolveAllTypeReferences (AssemblyDefinition assembly)
+               bool ResolveAllTypeReferences (AssemblyDefinition assembly)
                {
                        if (resolvedTypeReferences == null)
                                resolvedTypeReferences = new HashSet<AssemblyDefinition> ();
                        if (resolvedTypeReferences.Contains (assembly))
-                               return;
+                               return false;
                        resolvedTypeReferences.Add (assembly);
 
                        var hash = new Dictionary<TypeReference,IMetadataScope> ();
+                       bool changes = false;
 
                        foreach (TypeReference tr in assembly.MainModule.GetTypeReferences ()) {
                                if (hash.ContainsKey (tr))
                                        continue;
                                var td = tr.Resolve ();
+                               IMetadataScope scope = tr.Scope;
                                // at this stage reference might include things that can't be resolved
-                               var scope = td == null ? null : assembly.MainModule.Import (td).Scope;
-                               hash.Add (tr, scope);
+                               // and if it is (resolved) it needs to be kept only if marked (#16213)
+                               if ((td != null) && Annotations.IsMarked (td)) {
+                                       scope = assembly.MainModule.ImportReference (td).Scope;
+                                       if (tr.Scope != scope)
+                                               changes = true;
+                                       hash.Add (tr, scope);
+                               }
+                       }
+                       if (assembly.MainModule.HasExportedTypes) {
+                               foreach (var et in assembly.MainModule.ExportedTypes) {
+                                       var td = et.Resolve ();
+                                       IMetadataScope scope = et.Scope;
+                                       if ((td != null) && Annotations.IsMarked (td)) {
+                                               scope = assembly.MainModule.ImportReference (td).Scope;
+                                               hash.Add (td, scope);
+                                       }
+                               }
                        }
 
                        // Resolve everything first before updating scopes.
@@ -146,6 +176,8 @@ namespace Mono.Linker.Steps {
                        foreach (var e in hash) {
                                e.Key.Scope = e.Value;
                        }
+
+                       return changes;
                }
 
                void SweepType (TypeDefinition type)