-
Notifications
You must be signed in to change notification settings - Fork 565
[dotnet-linker] Use [DynamicDependency] attributes instead of manual marking when preserving smart enum methods. #24934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rolfbjarne
merged 8 commits into
main
from
dev/rolf/use-dynamic-dependency-attributes-preservesmartenumconversion
Mar 27, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d4e05fb
[dotnet-linker] Use [DynamicDependency] attributes instead of manual …
rolfbjarne e5fa56d
Remove unnecessary code
rolfbjarne 492061b
Update tools/dotnet-linker/PreserveSmartEnumConversionsStep.cs
rolfbjarne 820329b
Update test
rolfbjarne 08345af
privatize
rolfbjarne a92fbe4
Merge remote-tracking branch 'origin/main' into dev/rolf/use-dynamic-…
rolfbjarne 9cfacc0
Tweak
rolfbjarne 54cf0f6
Build fix
rolfbjarne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 212 additions & 0 deletions
212
tools/dotnet-linker/PreserveSmartEnumConversionsStep.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| // Copyright 2017 Xamarin Inc. | ||
|
|
||
| using System.Linq; | ||
|
|
||
| using Mono.Cecil; | ||
| using Mono.Linker; | ||
| using Mono.Linker.Steps; | ||
| using Mono.Tuner; | ||
|
|
||
| using Xamarin.Bundler; | ||
| using Xamarin.Tuner; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Xamarin.Linker.Steps { | ||
| public class PreserveSmartEnumConversionsStep : AssemblyModifierStep { | ||
| protected override string Name { get; } = "Smart Enum Conversion Preserver"; | ||
| protected override int ErrorCode { get; } = 2200; | ||
|
|
||
| PreserveSmartEnumConversion? preserver; | ||
| PreserveSmartEnumConversion Preserver { | ||
| get { | ||
| if (preserver is null) | ||
| preserver = new PreserveSmartEnumConversion (DerivedLinkContext, Preserve); | ||
| return preserver; | ||
| } | ||
| } | ||
|
|
||
| protected override bool IsActiveFor (AssemblyDefinition assembly) | ||
| { | ||
| // We have to process assemblies that aren't linked, because type A from the unlinked assembly X | ||
| // might reference the smart enum B from the linked assembly Y, | ||
| // and we need to make sure that smart enum B's conversion methods aren't trimmed away - which | ||
| // means adding dynamic dependency attributes to the methods in the unlinked assembly X, | ||
| // which means we need to process the unlinked assembly X. | ||
|
|
||
| // Hot Reload: we can't modify user assemblies when Hot Reload is enabled (otherwise Hot Reload won't work), | ||
| // so we'll have to come up with a different solution (emit xml definition instead maybe?) | ||
|
|
||
| // Unless an assembly is or references our platform assembly, then it won't have anything we need to preserve | ||
| if (!Configuration.Profile.IsOrReferencesProductAssembly (assembly)) | ||
| return false; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool Preserve (Tuple<MethodDefinition, MethodDefinition> pair, bool alreadyProcessed, params MethodDefinition? [] conditions) | ||
| { | ||
| var conds = conditions.Where (v => v is not null).Cast<MethodDefinition> ().ToArray (); | ||
| if (conds.Length == 0) | ||
| return false; | ||
|
|
||
| var modified = false; | ||
| foreach (var condition in conds) { | ||
| modified |= abr.AddDynamicDependencyAttribute (condition, pair.Item1); | ||
| modified |= abr.AddDynamicDependencyAttribute (condition, pair.Item2); | ||
| } | ||
|
|
||
| return modified; | ||
| } | ||
|
|
||
| protected override bool ProcessType (TypeDefinition type) | ||
| { | ||
| var modified = false; | ||
|
|
||
| if (!type.HasMethods) | ||
| return modified; | ||
|
|
||
| foreach (var method in type.Methods) | ||
| modified |= ProcessMethod (method); | ||
|
|
||
| return modified; | ||
| } | ||
|
|
||
| bool ProcessMethod (MethodDefinition method) | ||
| { | ||
| static bool IsPropertyMethod (MethodDefinition method) | ||
| { | ||
| return method.IsGetter || method.IsSetter; | ||
| } | ||
|
|
||
| var modified = false; | ||
| modified |= Preserver.ProcessAttributeProvider (method, method); | ||
| modified |= Preserver.ProcessAttributeProvider (method.MethodReturnType, method); | ||
|
|
||
| if (method.HasParameters) { | ||
| foreach (var p in method.Parameters) | ||
| modified |= Preserver.ProcessAttributeProvider (p, method); | ||
| } | ||
| if (IsPropertyMethod (method)) { | ||
| foreach (var property in method.DeclaringType.Properties) | ||
| if (property.GetMethod == method || property.SetMethod == method) { | ||
| modified |= Preserver.ProcessAttributeProvider (property, property.GetMethod, property.SetMethod); | ||
| break; | ||
| } | ||
| } | ||
| return modified; | ||
| } | ||
| } | ||
|
|
||
| class PreserveSmartEnumConversion { | ||
| Dictionary<TypeDefinition, Tuple<MethodDefinition, MethodDefinition>> cache = new (); | ||
|
|
||
| public DerivedLinkContext LinkContext { get; private set; } | ||
|
|
||
| Func<Tuple<MethodDefinition, MethodDefinition>, bool, MethodDefinition? [], bool> preserve { get; set; } | ||
|
|
||
| public PreserveSmartEnumConversion (DerivedLinkContext linkContext, Func<Tuple<MethodDefinition, MethodDefinition>, bool, MethodDefinition? [], bool> preserve) | ||
| { | ||
| LinkContext = linkContext; | ||
| this.preserve = preserve; | ||
| } | ||
|
|
||
| bool Preserve (Tuple<MethodDefinition, MethodDefinition> pair, bool alreadyProcessed, params MethodDefinition? [] conditions) | ||
| { | ||
| return preserve (pair, alreadyProcessed, conditions); | ||
| } | ||
|
|
||
| public bool ProcessAttributeProvider (ICustomAttributeProvider provider, params MethodDefinition [] conditions) | ||
| { | ||
| var modified = false; | ||
|
|
||
| if (provider?.HasCustomAttributes != true) | ||
| return modified; | ||
|
|
||
| foreach (var ca in provider.CustomAttributes) { | ||
| var tr = ca.Constructor.DeclaringType; | ||
|
|
||
| if (!tr.Is ("ObjCRuntime", "BindAsAttribute")) | ||
| continue; | ||
|
|
||
| if (ca.ConstructorArguments.Count != 1) { | ||
| ErrorHelper.Show (ErrorHelper.CreateWarning (LinkContext.App, 4124, provider, Errors.MT4124_E, provider.AsString (), ca.ConstructorArguments.Count)); | ||
| continue; | ||
| } | ||
|
|
||
| var managedType = ca.ConstructorArguments [0].Value as TypeReference; | ||
| var managedEnumType = managedType?.GetElementType ().Resolve (); | ||
| if (managedEnumType is null) { | ||
| ErrorHelper.Show (ErrorHelper.CreateWarning (LinkContext.App, 4124, provider, Errors.MT4124_H, provider.AsString (), managedType?.FullName ?? "(null)")); | ||
| continue; | ||
| } | ||
|
|
||
| // We only care about enums, BindAs attributes can be used for other types too. | ||
| if (!managedEnumType.IsEnum) | ||
| continue; | ||
|
|
||
| if (cache.TryGetValue (managedEnumType, out var pair)) { | ||
| // The pair was already marked if it was cached. | ||
| modified |= Preserve (pair, true, conditions); | ||
| continue; | ||
| } | ||
|
|
||
| // Find the Extension type | ||
| TypeDefinition? extensionType = null; | ||
| var extensionName = managedEnumType.Name + "Extensions"; | ||
| foreach (var type in managedEnumType.Module.Types) { | ||
| if (type.Namespace != managedEnumType.Namespace) | ||
| continue; | ||
| if (type.Name != extensionName) | ||
| continue; | ||
| extensionType = type; | ||
| break; | ||
| } | ||
| if (extensionType is null) { | ||
| Driver.Log (1, $"Could not find a smart extension type for the enum {managedEnumType.FullName} (due to BindAs attribute on {provider.AsString ()}): most likely this is because the enum isn't a smart enum."); | ||
| continue; | ||
| } | ||
|
|
||
| // Find the GetConstant/GetValue methods | ||
| MethodDefinition? getConstant = null; | ||
| MethodDefinition? getValue = null; | ||
|
|
||
| foreach (var method in extensionType.Methods) { | ||
| if (!method.IsStatic) | ||
| continue; | ||
| if (!method.HasParameters || method.Parameters.Count != 1) | ||
| continue; | ||
| if (method.Name == "GetConstant") { | ||
| if (!method.ReturnType.Is ("Foundation", "NSString")) | ||
| continue; | ||
| if (method.Parameters [0].ParameterType != managedEnumType) | ||
| continue; | ||
| getConstant = method; | ||
| } else if (method.Name == "GetValue") { | ||
| if (!method.Parameters [0].ParameterType.Is ("Foundation", "NSString")) | ||
| continue; | ||
| if (method.ReturnType != managedEnumType) | ||
| continue; | ||
| getValue = method; | ||
| } | ||
| } | ||
|
|
||
| if (getConstant is null) { | ||
| Driver.Log (1, $"Could not find the GetConstant method on the supposedly smart extension type {extensionType.FullName} for the enum {managedEnumType.FullName} (due to BindAs attribute on {provider.AsString ()}): most likely this is because the enum isn't a smart enum."); | ||
| continue; | ||
| } | ||
|
|
||
| if (getValue is null) { | ||
| Driver.Log (1, $"Could not find the GetValue method on the supposedly smart extension type {extensionType.FullName} for the enum {managedEnumType.FullName} (due to BindAs attribute on {provider.AsString ()}): most likely this is because the enum isn't a smart enum."); | ||
| continue; | ||
| } | ||
|
|
||
| pair = new Tuple<MethodDefinition, MethodDefinition> (getConstant, getValue); | ||
| cache.Add (managedEnumType, pair); | ||
| modified |= Preserve (pair, false, conditions); | ||
| } | ||
|
|
||
| return modified; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.