-
Notifications
You must be signed in to change notification settings - Fork 517
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Rgen] Implement the generation of the smart enums. (#21562)
Add the code generation for smart enumerators. This means the following changes: 1. Refactor the way we register the diff pipelines. 2. Add code that generates the Extension class for the enums. 3. Add code that will generate the Library.g.cs partial class. --------- Co-authored-by: Rolf Bjarne Kvinge <[email protected]>
- Loading branch information
1 parent
fa9294b
commit 6bfecec
Showing
74 changed files
with
5,435 additions
and
301 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
3 changes: 2 additions & 1 deletion
3
src/rgen/Microsoft.Macios.Generator.Sample/Microsoft.Macios.Generator.Sample.csproj
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
51 changes: 41 additions & 10 deletions
51
src/rgen/Microsoft.Macios.Generator/Attributes/FieldData.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 |
---|---|---|
@@ -1,51 +1,82 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.Macios.Generator.Extensions; | ||
|
||
namespace Microsoft.Macios.Generator.Attributes; | ||
|
||
record FieldData { | ||
readonly struct FieldData<T> where T : Enum { | ||
public string SymbolName { get; } | ||
public string? LibraryName { get; private set; } | ||
public string? LibraryName { get; } | ||
|
||
FieldData (string symbolName, string? libraryName = null) | ||
public T? Flags { get; } = default; | ||
|
||
FieldData (string symbolName, string? libraryName, T? flags) | ||
{ | ||
SymbolName = symbolName; | ||
LibraryName = libraryName; | ||
Flags = flags; | ||
} | ||
|
||
public static bool TryParse (SyntaxNode attributeSyntax, AttributeData attributeData, | ||
[NotNullWhen (true)] out FieldData? data) | ||
public static bool TryParse (AttributeData attributeData, | ||
[NotNullWhen (true)] out FieldData<T>? data) | ||
{ | ||
data = default; | ||
|
||
var count = attributeData.ConstructorArguments.Length; | ||
string? symbolName; | ||
string? libraryName = null; | ||
T? flags = default; | ||
switch (count) { | ||
case 1: | ||
data = new ((string) attributeData.ConstructorArguments [0].Value!); | ||
if (!attributeData.ConstructorArguments [0].TryGetIdentifier (out symbolName)) { | ||
return false; | ||
} | ||
break; | ||
case 2: | ||
data = new ((string) attributeData.ConstructorArguments [0].Value!, | ||
(string) attributeData.ConstructorArguments [1].Value!); | ||
if (!attributeData.ConstructorArguments [0].TryGetIdentifier (out symbolName)) { | ||
return false; | ||
} | ||
switch (attributeData.ConstructorArguments [1].Value) { | ||
// there are two possible cases here: | ||
// 1. The second argument is a string | ||
// 2. The second argument is an enum | ||
case T enumValue: | ||
flags = enumValue; | ||
break; | ||
case string lib: | ||
libraryName = lib; | ||
break; | ||
default: | ||
// unexpected value :/ | ||
return false; | ||
} | ||
break; | ||
default: | ||
// 0 should not be an option.. | ||
return false; | ||
} | ||
|
||
if (attributeData.NamedArguments.Length == 0) | ||
if (attributeData.NamedArguments.Length == 0) { | ||
data = new (symbolName, libraryName, flags); | ||
return true; | ||
} | ||
|
||
// LibraryName can be a param value | ||
foreach (var (name, value) in attributeData.NamedArguments) { | ||
switch (name) { | ||
case "LibraryName": | ||
data.LibraryName = (string?) value.Value!; | ||
libraryName = (string?) value.Value!; | ||
break; | ||
case "Flags": | ||
flags = (T) value.Value!; | ||
break; | ||
default: | ||
data = null; | ||
return false; | ||
} | ||
} | ||
data = new (symbolName, libraryName, flags); | ||
return true; | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
src/rgen/Microsoft.Macios.Generator/Attributes/ObsoletedOSPlatformData.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,93 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.Macios.Generator.Extensions; | ||
using Xamarin.Utils; | ||
|
||
namespace Microsoft.Macios.Generator.Attributes; | ||
|
||
/// <summary> | ||
/// Represents the data found in a ObsoletedOSPlatformAttribute. | ||
/// </summary> | ||
readonly struct ObsoletedOSPlatformData { | ||
/// <summary> | ||
/// Obsoleted platform. | ||
/// </summary> | ||
public ApplePlatform Platform { get; } | ||
|
||
/// <summary> | ||
/// Version in which the symbol was obsoleted. The default new Version () value will be | ||
/// used when the symbol has been obsoleted in all version. | ||
/// </summary> | ||
public Version Version { get; } | ||
|
||
/// <summary> | ||
/// Optional obsoleted message to report to the user. | ||
/// </summary> | ||
public string? Message { get; } = default; | ||
|
||
/// <summary> | ||
/// Optional url that points to the documentation. | ||
/// </summary> | ||
public string? Url { get; } | ||
|
||
internal ObsoletedOSPlatformData (string platformName) | ||
{ | ||
(Platform, Version) = platformName.GetPlatformAndVersion (); | ||
} | ||
|
||
internal ObsoletedOSPlatformData (string platformName, string? message = null, string? url = null) : this (platformName) | ||
{ | ||
Message = message; | ||
Url = url; | ||
} | ||
|
||
/// <summary> | ||
/// Try to parse the attribute data to retrieve the information of an ObsoletedOSPlatformAttribute. | ||
/// </summary> | ||
/// <param name="attributeData">The attribute data to be parsed.</param> | ||
/// <param name="data">The parsed data. Null if we could not parse the attribute data.</param> | ||
/// <returns>True if the data was parsed.</returns> | ||
public static bool TryParse (AttributeData attributeData, | ||
[NotNullWhen (true)] out ObsoletedOSPlatformData? data) | ||
{ | ||
data = default; | ||
string platformName; | ||
string? message = null; | ||
string? url = null; | ||
|
||
var count = attributeData.ConstructorArguments.Length; | ||
switch (count) { | ||
case 1: | ||
platformName = (string) attributeData.ConstructorArguments [0].Value!; | ||
break; | ||
case 2: | ||
platformName = (string) attributeData.ConstructorArguments [0].Value!; | ||
message = (string) attributeData.ConstructorArguments [1].Value!; | ||
break; | ||
default: // there is not 3 args constructor with a url | ||
return false; | ||
} | ||
|
||
if (attributeData.NamedArguments.Length == 0) { | ||
data = new (platformName, message, url); | ||
return true; | ||
} | ||
|
||
foreach (var (name, value) in attributeData.NamedArguments) { | ||
switch (name) { | ||
case "Url": | ||
url = (string?) value.Value!; | ||
break; | ||
case "TypeId": // ignore the type id | ||
break; | ||
default: | ||
data = null; | ||
return false; | ||
} | ||
} | ||
|
||
data = new (platformName, message, url); | ||
return true; | ||
} | ||
} |
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.