Skip to content

Commit 5f30c04

Browse files
[Rgen] Allow the transformer to keep track of nornt smart enums.
The transformer needs to be able to parse non smart enums to be able to copy them over to the new location and to update the availability attributes to use SupportedOS and UnsupportedOS. This changes nothing in the code generator.
1 parent fe5c724 commit 5f30c04

File tree

4 files changed

+60
-14
lines changed

4 files changed

+60
-14
lines changed

src/rgen/Microsoft.Macios.Generator/DataModel/BindingType.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ enum BindingType : ulong {
2424
/// </summary>
2525
Protocol,
2626
/// <summary>
27+
/// Binding type for a traditional enum.
28+
/// </summary>
29+
Enum,
30+
/// <summary>
2731
/// Binding type for a enum with backing fields.
2832
/// </summary>
2933
SmartEnum,

src/rgen/Microsoft.Macios.Transformer/DataModel/Binding.Transformer.cs

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ public ImmutableArray<string> Protocols {
4040
init => protocols = value;
4141
}
4242

43+
public BaseTypeDeclarationSyntax? BaseDeclarationSyntax { get; init; }
44+
4345
/// <summary>
4446
/// Internal constructor added for testing purposes.
4547
/// </summary>
48+
/// <param name="declarationSyntax">The syntax used to declare the binding.</param>
4649
/// <param name="name">The name of the named type that created the code change.</param>
4750
/// <param name="namespace">The namespace that contains the named type.</param>
4851
/// <param name="fullyQualifiedSymbol">The fully qualified name of the symbol.</param>
@@ -74,6 +77,7 @@ internal Binding (EnumDeclarationSyntax enumDeclaration, INamedTypeSymbol symbol
7477
outerClasses: out outerClasses,
7578
namespaces: out namespaces,
7679
symbolAvailability: out availability);
80+
BaseDeclarationSyntax = enumDeclaration;
7781
BindingBindingInfo = new BindingInfo (null, BindingType.SmartEnum);
7882
FullyQualifiedSymbol = enumDeclaration.GetFullyQualifiedIdentifier (context.SemanticModel);
7983
UsingDirectives = enumDeclaration.SyntaxTree.CollectUsingStatements ();
@@ -91,24 +95,55 @@ internal Binding (EnumDeclarationSyntax enumDeclaration, INamedTypeSymbol symbol
9195
continue;
9296
}
9397
var fieldData = enumValueSymbol.GetFieldData ();
98+
EnumMember? enumMember;
9499
// try and compute the library for this enum member
95100
if (fieldData is null || !context.TryComputeLibraryName (fieldData.Value.LibraryName, Namespace [^1],
96-
out string? libraryName, out string? libraryPath))
97-
// could not calculate the library for the enum, do not add it
98-
continue;
99-
// we do know we have a backing field, so we can set the binding type to smart enum
100-
bindingType = BindingType.SmartEnum;
101-
var enumMember = new EnumMember (
102-
name: enumValueDeclaration.Identifier.ToFullString ().Trim (),
103-
libraryName: libraryName,
104-
libraryPath: libraryPath,
105-
fieldData: enumValueSymbol.GetFieldData (),
106-
symbolAvailability: enumValueSymbol.GetAvailabilityForSymbol () // no parent availability, just the symbol
107-
);
108-
bucket.Add (enumMember);
101+
out string? libraryName, out string? libraryPath)) {
102+
// create a member for those enum values that do not have a backing field, we will
103+
// set the binding type to regular enum if we do not know it yet
104+
if (bindingType == BindingType.Unknown) {
105+
bindingType = BindingType.Enum;
106+
}
107+
enumMember = new EnumMember (
108+
name: enumValueDeclaration.Identifier.ToFullString ().Trim (),
109+
libraryName: string.Empty,
110+
libraryPath: null,
111+
fieldData: enumValueSymbol.GetFieldData (),
112+
symbolAvailability: enumValueSymbol
113+
.GetAvailabilityForSymbol () // no parent availability, just the symbol
114+
) {
115+
DeclarationSyntax = enumValueDeclaration,
116+
IsSmartMember = false
117+
};
118+
} else {
119+
// we do know we have a backing field, so we can set the binding type to smart enum
120+
bindingType = BindingType.SmartEnum;
121+
enumMember = new EnumMember (
122+
name: enumValueDeclaration.Identifier.ToFullString ().Trim (),
123+
libraryName: libraryName,
124+
libraryPath: libraryPath,
125+
fieldData: enumValueSymbol.GetFieldData (),
126+
symbolAvailability: enumValueSymbol
127+
.GetAvailabilityForSymbol () // no parent availability, just the symbol
128+
) {
129+
DeclarationSyntax = enumValueDeclaration,
130+
IsSmartMember = true,
131+
};
132+
}
133+
bucket.Add (enumMember.Value);
109134
}
110135

136+
if (bindingType == BindingType.Unknown) {
137+
// we could not find any enum member with a backing field this means that we are dealing with a regular enum
138+
// we will set it so that we can convert the availability information
139+
bindingType = BindingType.Enum;
140+
}
111141
BindingBindingInfo = new (null, bindingType);
142+
// based on the type of enum we might need to remove enums that are not valid, this are enums without
143+
// a backing field in the case of smart enums
144+
if (BindingBindingInfo.BindingType == BindingType.SmartEnum) {
145+
bucket.RemoveAll (em => !em.IsSmartMember);
146+
}
112147
EnumMembers = bucket.ToImmutable ();
113148
}
114149

@@ -236,6 +271,7 @@ internal static bool Skip (MethodDeclarationSyntax methodDeclarationSyntax, Sema
236271
/// <param name="context">The current compilation context.</param>
237272
internal Binding (InterfaceDeclarationSyntax interfaceDeclarationSyntax, INamedTypeSymbol symbol, in RootContext context)
238273
{
274+
BaseDeclarationSyntax = interfaceDeclarationSyntax;
239275
// basic properties of the binding
240276
FullyQualifiedSymbol = interfaceDeclarationSyntax.GetFullyQualifiedIdentifier (context.SemanticModel);
241277
UsingDirectives = interfaceDeclarationSyntax.SyntaxTree.CollectUsingStatements ();

src/rgen/Microsoft.Macios.Transformer/DataModel/EnumMember.Transformer.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using Microsoft.CodeAnalysis;
5+
using Microsoft.CodeAnalysis.CSharp.Syntax;
56
using Microsoft.Macios.Generator.Availability;
67
using Microsoft.Macios.Transformer.Attributes;
78

@@ -14,6 +15,12 @@ readonly partial struct EnumMember {
1415
/// </summary>
1516
public FieldInfo? FieldInfo { get; init; }
1617

18+
/// <summary>
19+
/// The enum member declaration.
20+
/// </summary>
21+
public EnumMemberDeclarationSyntax? DeclarationSyntax { get; init; }
22+
23+
public bool IsSmartMember { get; init; }
1724

1825
/// <summary>
1926
/// Return the native selector that references the enum value.

tests/rgen/Microsoft.Macios.Transformer.Tests/DataModel/SmartEnumTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ public enum AVCaptureDeviceType {
9797
[Field (""AVCaptureDeviceTypeBuiltInWideAngleCamera"")]
9898
BuiltInWideAngleCamera,
9999
100-
// missing attr, this should be ignored
101100
BuiltInTelephotoCamera,
102101
}
103102
";

0 commit comments

Comments
 (0)