-
Notifications
You must be signed in to change notification settings - Fork 128
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
Create shared WellKnownTypes enum #2692
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
26201ca
wip
jtschuster f41212d
Use WellKnownType enum in more places
jtschuster c435c69
Use DisplayName for Namespace comparisons and remove unnecessary using
jtschuster 19c6183
formatting
jtschuster 65cd97a
Use switch expressions instead of statements
jtschuster e027128
PR comments
jtschuster 14eff4d
Merge branch 'main' into RemoveRoslynNullRefs
jtschuster 5e16d02
Remove leftover merge issue
jtschuster 90345ac
Delete duplicated method from merge
jtschuster 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 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
23 changes: 23 additions & 0 deletions
23
src/ILLink.RoslynAnalyzer/TrimAnalysis/WellKnownTypeExtensions.cs
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,23 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace ILLink.Shared.TypeSystemProxy | ||
{ | ||
public static partial class WellKnownTypeExtensions | ||
{ | ||
public static bool TryGetSpecialType (this WellKnownType wellKnownType, [NotNullWhen (true)] out SpecialType? specialType) | ||
{ | ||
specialType = wellKnownType switch { | ||
WellKnownType.System_String => SpecialType.System_String, | ||
WellKnownType.System_Nullable_T => SpecialType.System_Nullable_T, | ||
WellKnownType.System_Array => SpecialType.System_Array, | ||
WellKnownType.System_Object => SpecialType.System_Object, | ||
_ => null, | ||
}; | ||
return specialType is not null; | ||
} | ||
} | ||
} |
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,37 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
|
||
namespace ILLink.Shared.TypeSystemProxy | ||
{ | ||
public enum WellKnownType | ||
{ | ||
System_String, | ||
System_Nullable_T, | ||
System_Type, | ||
System_Reflection_IReflect, | ||
System_Array, | ||
System_Object, | ||
System_Attribute | ||
} | ||
|
||
public static partial class WellKnownTypeExtensions | ||
{ | ||
public static (string Namespace, string Name) GetNamespaceAndName (this WellKnownType type) | ||
{ | ||
return type switch { | ||
WellKnownType.System_String => ("System", "String"), | ||
WellKnownType.System_Nullable_T => ("System", "Nullable`1"), | ||
WellKnownType.System_Type => ("System", "Type"), | ||
WellKnownType.System_Reflection_IReflect => ("System.Reflection", "IReflect"), | ||
WellKnownType.System_Array => ("System", "Array"), | ||
WellKnownType.System_Object => ("System", "Object"), | ||
WellKnownType.System_Attribute => ("System", "Attribute"), | ||
_ => throw new ArgumentException ($"{nameof (type)} is not a well-known type."), | ||
}; | ||
} | ||
public static string GetNamespace (this WellKnownType type) => GetNamespaceAndName (type).Namespace; | ||
public static string GetName (this WellKnownType type) => GetNamespaceAndName (type).Name; | ||
} | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cecil has a similar concept to SpecialType - it's called MetadataType and through it one can detect primitive types (and couple of other cases) faster than string compares.
For example
linker/src/linker/Linker.Dataflow/ReflectionMethodBodyScanner.cs
Line 505 in ae1f280
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to know, I'll add that in a new PR. Thanks!