-
Notifications
You must be signed in to change notification settings - Fork 514
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NativeAOT] Add support for [Preserve] attributes (#18666)
Add partial support for the `[Preserve]` attribute for NativeAOT. This is done by injecting an equivalent `[DynamicDependency]` attribute. The partial support comes from the fact that there's no way to map a conditional preserve attribute (`[Preserve (Conditional = true)]`) to a `[DynamicDependency]` attribute, so we report a warning instead. For non-conditional `[Preserve]` attributes, we'll now add a `[DynamicDependency]` attribute to the assembly's module constructor for the type/member in question, effectively rooting it. This makes it possible to fully link all our test suites when NativeAOT (otherwise NativeAOT would just link out all the tests, leaving the test suites empty - and unfortunately green, so this was a rather accidental discovery).
- Loading branch information
1 parent
90cc165
commit 19b2b37
Showing
9 changed files
with
281 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,80 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
using Mono.Cecil; | ||
|
||
using Xamarin.Bundler; | ||
|
||
#nullable enable | ||
|
||
namespace Xamarin.Utils { | ||
// signature format: https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/documentation-comments.md#d42-id-string-format | ||
public static class DocumentationComments { | ||
public static string GetSignature (IMetadataTokenProvider member) | ||
{ | ||
if (member is FieldDefinition fd) | ||
return GetSignature (fd); | ||
|
||
if (member is MethodDefinition md) | ||
return GetSignature (md); | ||
|
||
if (member is TypeDefinition td) | ||
return GetSignature (td); | ||
|
||
throw ErrorHelper.CreateError (99, $"Unable to get the doc signature for {member.GetType ().FullName}"); | ||
} | ||
|
||
public static string GetSignature (TypeDefinition type) | ||
{ | ||
if (type.IsNested) | ||
return type.Name; | ||
return type.FullName; | ||
} | ||
|
||
public static string GetSignature (FieldDefinition field) | ||
{ | ||
return field.Name.Replace ('.', '#'); | ||
} | ||
|
||
public static string GetSignature (MethodDefinition method) | ||
{ | ||
var sb = new StringBuilder (); | ||
sb.Append (method.Name.Replace ('.', '#')); | ||
sb.Append ('('); | ||
for (var i = 0; i < method.Parameters.Count; i++) { | ||
if (i > 0) | ||
sb.Append (','); | ||
|
||
var parameterType = method.Parameters [i].ParameterType; | ||
WriteTypeSignature (sb, parameterType); | ||
} | ||
sb.Append (')'); | ||
|
||
return sb.ToString (); | ||
} | ||
|
||
static void WriteTypeSignature (StringBuilder sb, TypeReference type) | ||
{ | ||
if (type is ByReferenceType brt) { | ||
WriteTypeSignature (sb, brt.GetElementType ()); | ||
sb.Append ('@'); | ||
return; | ||
} | ||
|
||
if (type is ArrayType at) { | ||
WriteTypeSignature (sb, at.GetElementType ()); | ||
sb.Append ("[]"); | ||
return; | ||
} | ||
|
||
if (type is PointerType pt) { | ||
WriteTypeSignature (sb, pt.GetElementType ()); | ||
sb.Append ('*'); | ||
return; | ||
} | ||
|
||
sb.Append (type.FullName.Replace ('/', '.')); | ||
} | ||
} | ||
} |
Oops, something went wrong.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
Sorry, something went wrong.