Skip to content

Commit ad3fd8e

Browse files
committed
Change to a registration-based model
1 parent 0e5bf59 commit ad3fd8e

File tree

16 files changed

+172
-127
lines changed

16 files changed

+172
-127
lines changed

src/linker/Linker.Steps/IMarkAssemblyStep.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
namespace Mono.Linker.Steps
5+
{
6+
7+
/// <summary>
8+
/// Extensibility point for custom logic that run during MarkStep,
9+
/// for marked members.
10+
/// </summary>
11+
public interface IMarkHandler
12+
{
13+
/// <summary>
14+
/// Initialize is called at the beginning of MarkStep. This should be
15+
/// used to perform global setup, and register callbacks through the
16+
/// MarkContext.Register* methods) to be called when pieces of IL are marked.
17+
/// </summary>
18+
void Initialize (LinkContext context, MarkContext markContext);
19+
}
20+
}

src/linker/Linker.Steps/MarkAssemblySubStepsDispatcher.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ namespace Mono.Linker.Steps
1212
//
1313
// Dispatcher for SubSteps which only need to run on marked assemblies.
1414
// This simplifies the implementation of linker custom steps, in the same
15-
// way that SubStepsDispatcher does, but it implements IMarkAssemblyStep
16-
// and is invoked during MarkStep when an assembly gets marked.
15+
// way that SubStepsDispatcher does, but it implements IMarkHandler
16+
// and registers a callback that gets invoked during MarkStep when an
17+
// assembly gets marked.
1718
//
18-
public abstract class MarkAssemblySubStepsDispatcher : IMarkAssemblyStep
19+
public abstract class MarkHandlerDispatcher : IMarkHandler
1920
{
2021
readonly List<ISubStep> substeps;
2122

@@ -26,12 +27,12 @@ public abstract class MarkAssemblySubStepsDispatcher : IMarkAssemblyStep
2627
List<ISubStep> on_properties;
2728
List<ISubStep> on_events;
2829

29-
protected MarkAssemblySubStepsDispatcher ()
30+
protected MarkHandlerDispatcher ()
3031
{
3132
substeps = new List<ISubStep> ();
3233
}
3334

34-
protected MarkAssemblySubStepsDispatcher (IEnumerable<ISubStep> subSteps)
35+
protected MarkHandlerDispatcher (IEnumerable<ISubStep> subSteps)
3536
{
3637
substeps = new List<ISubStep> (subSteps);
3738
}
@@ -41,14 +42,10 @@ public void Add (ISubStep substep)
4142
substeps.Add (substep);
4243
}
4344

44-
void IMarkAssemblyStep.Initialize (LinkContext context)
45+
void IMarkHandler.Initialize (LinkContext context, MarkContext markContext)
4546
{
4647
InitializeSubSteps (context);
47-
}
48-
49-
void IMarkAssemblyStep.ProcessAssembly (AssemblyDefinition assembly)
50-
{
51-
BrowseAssembly (assembly);
48+
markContext.RegisterMarkAssemblyAction (assembly => BrowseAssembly (assembly));
5249
}
5350

5451
static bool HasSubSteps (List<ISubStep> substeps) => substeps?.Count > 0;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using Mono.Cecil;
3+
4+
namespace Mono.Linker.Steps
5+
{
6+
/// <summary>
7+
/// Context which can be used to register actions to call during MarkStep
8+
/// when various members are marked.
9+
/// </summary>
10+
public abstract class MarkContext
11+
{
12+
/// <summary>
13+
/// Register a callback that will be invoked once for each marked assembly.
14+
/// </summary>
15+
public abstract void RegisterMarkAssemblyAction (Action<AssemblyDefinition> action);
16+
17+
/// <summary>
18+
/// Register a callback that will be invoked once for each marked type.
19+
/// </summary>
20+
public abstract void RegisterMarkTypeAction (Action<TypeDefinition> action);
21+
}
22+
}

src/linker/Linker.Steps/MarkStep.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public partial class MarkStep : IStep
5757

5858
readonly List<(TypeDefinition Type, MethodBody Body, Instruction Instr)> _pending_isinst_instr;
5959
UnreachableBlocksOptimizer _unreachableBlocksOptimizer;
60+
MarkStepContext _markContext;
6061

6162
#if DEBUG
6263
static readonly DependencyKind[] _entireTypeReasons = new DependencyKind[] {
@@ -183,6 +184,7 @@ public virtual void Process (LinkContext context)
183184
{
184185
_context = context;
185186
_unreachableBlocksOptimizer = new UnreachableBlocksOptimizer (_context);
187+
_markContext = new MarkStepContext ();
186188

187189
Initialize ();
188190
Process ();
@@ -191,6 +193,8 @@ public virtual void Process (LinkContext context)
191193

192194
void Initialize ()
193195
{
196+
_context.Pipeline.InitializeMarkHandlers (_context, _markContext);
197+
194198
foreach (AssemblyDefinition assembly in _context.GetAssemblies ())
195199
InitializeAssembly (assembly);
196200

@@ -1236,7 +1240,8 @@ protected void MarkAssembly (AssemblyDefinition assembly, DependencyInfo reason)
12361240
if (CheckProcessed (assembly))
12371241
return;
12381242

1239-
_context.Pipeline.ProcessAssembly (assembly);
1243+
foreach (Action<AssemblyDefinition> handleMarkAssembly in _markContext.MarkAssemblyActions)
1244+
handleMarkAssembly (assembly);
12401245

12411246
var action = _context.Annotations.GetAction (assembly);
12421247
if (action == AssemblyAction.Copy || action == AssemblyAction.Save) {
@@ -1558,6 +1563,10 @@ protected internal virtual TypeDefinition MarkType (TypeReference reference, Dep
15581563
return null;
15591564

15601565
MarkModule (type.Scope as ModuleDefinition, new DependencyInfo (DependencyKind.ScopeOfType, type));
1566+
1567+
foreach (Action<TypeDefinition> handleMarkType in _markContext.MarkTypeActions)
1568+
handleMarkType (type);
1569+
15611570
MarkType (type.BaseType, new DependencyInfo (DependencyKind.BaseType, type), type);
15621571
MarkType (type.DeclaringType, new DependencyInfo (DependencyKind.DeclaringType, type), type);
15631572
MarkCustomAttributes (type, new DependencyInfo (DependencyKind.CustomAttribute, type), type);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Mono.Cecil;
4+
5+
namespace Mono.Linker.Steps
6+
{
7+
public class MarkStepContext : MarkContext
8+
{
9+
10+
public readonly List<Action<AssemblyDefinition>> MarkAssemblyActions;
11+
public readonly List<Action<TypeDefinition>> MarkTypeActions;
12+
13+
public MarkStepContext ()
14+
{
15+
MarkAssemblyActions = new List<Action<AssemblyDefinition>> ();
16+
MarkTypeActions = new List<Action<TypeDefinition>> ();
17+
}
18+
19+
public override void RegisterMarkAssemblyAction (Action<AssemblyDefinition> action)
20+
{
21+
MarkAssemblyActions.Add (action);
22+
}
23+
24+
public override void RegisterMarkTypeAction (Action<TypeDefinition> action)
25+
{
26+
MarkTypeActions.Add (action);
27+
}
28+
}
29+
}

src/linker/Linker/Driver.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -788,16 +788,16 @@ protected virtual void AddXmlDependencyRecorder (LinkContext context, string fil
788788
context.Tracer.AddRecorder (new XmlDependencyRecorder (context, fileName));
789789
}
790790

791-
protected bool AddMarkAssemblyStep (Pipeline pipeline, string arg)
791+
protected bool AddMarkHandler (Pipeline pipeline, string arg)
792792
{
793793
if (!TryGetCustomAssembly (ref arg, out Assembly custom_assembly))
794794
return false;
795795

796-
var step = ResolveStep<IMarkAssemblyStep> (arg, custom_assembly);
796+
var step = ResolveStep<IMarkHandler> (arg, custom_assembly);
797797
if (step == null)
798798
return false;
799799

800-
pipeline.AppendMarkAssemblyStep (step);
800+
pipeline.AppendMarkHandler (step);
801801
return true;
802802
}
803803

@@ -855,7 +855,7 @@ protected bool AddCustomStep (Pipeline pipeline, string arg)
855855
pipeline.AppendStep (customStep);
856856
return true;
857857
}
858-
858+
859859
IStep target = FindStep (pipeline, targetName);
860860
if (target == null) {
861861
context.LogError ($"Pipeline step '{targetName}' could not be found", 1026);
@@ -867,24 +867,24 @@ protected bool AddCustomStep (Pipeline pipeline, string arg)
867867
else
868868
pipeline.AddStepAfter (target, customStep);
869869

870-
} else if (typeof (IMarkAssemblyStep).IsAssignableFrom (stepType)) {
870+
} else if (typeof (IMarkHandler).IsAssignableFrom (stepType)) {
871871

872-
var customStep = (IMarkAssemblyStep) Activator.CreateInstance (stepType);
872+
var customStep = (IMarkHandler) Activator.CreateInstance (stepType);
873873
if (targetName == null) {
874-
pipeline.AppendMarkAssemblyStep (customStep);
874+
pipeline.AppendMarkHandler (customStep);
875875
return true;
876876
}
877877

878-
IMarkAssemblyStep target = FindMarkAssemblyStep (pipeline, targetName);
878+
IMarkHandler target = FindMarkHandler (pipeline, targetName);
879879
if (target == null) {
880880
context.LogError ($"Pipeline step '{targetName}' could not be found", 1026);
881881
return false;
882882
}
883883

884884
if (before)
885-
pipeline.AddMarkAssemblyStepBefore (target, customStep);
885+
pipeline.AddMarkHandlerBefore (target, customStep);
886886
else
887-
pipeline.AddMarkAssemblyStepAfter (target, customStep);
887+
pipeline.AddMarkHandlerAfter (target, customStep);
888888

889889
} else {
890890
context.LogError ($"Custom step '{stepType}' is incompatible with this linker version", 1028);
@@ -905,9 +905,9 @@ static IStep FindStep (Pipeline pipeline, string name)
905905
return null;
906906
}
907907

908-
static IMarkAssemblyStep FindMarkAssemblyStep (Pipeline pipeline, string name)
908+
static IMarkHandler FindMarkHandler (Pipeline pipeline, string name)
909909
{
910-
foreach (IMarkAssemblyStep step in pipeline.GetMarkAssemblySteps ()) {
910+
foreach (IMarkHandler step in pipeline.MarkHandlers) {
911911
Type t = step.GetType ();
912912
if (t.Name == name)
913913
return step;

src/linker/Linker/LinkContext.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
using System.IO;
3333
using Mono.Cecil;
3434
using Mono.Cecil.Cil;
35-
35+
using Mono.Linker.Steps;
3636
namespace Mono.Linker
3737
{
3838

@@ -200,6 +200,8 @@ public ISymbolWriterProvider SymbolWriterProvider {
200200

201201
public string AssemblyListFile { get; set; }
202202

203+
public List<IMarkHandler> MarkHandlers { get; }
204+
203205
public LinkContext (Pipeline pipeline, ILogger logger)
204206
{
205207
_pipeline = pipeline;
@@ -234,6 +236,7 @@ public LinkContext (Pipeline pipeline, ILogger logger)
234236
GeneralWarnAsError = false;
235237
WarnAsError = new Dictionary<int, bool> ();
236238
WarnVersion = WarnVersion.Latest;
239+
MarkHandlers = new List<IMarkHandler> ();
237240

238241
// See https://github.com/mono/linker/issues/612
239242
const CodeOptimizations defaultOptimizations =

0 commit comments

Comments
 (0)