Skip to content
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

[Rgen] Generalize backing field method. #22274

Merged
merged 2 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/rgen/Microsoft.Macios.Generator/DataModel/Property.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ namespace Microsoft.Macios.Generator.DataModel;
/// </summary>
public string Name { get; } = string.Empty;

/// <summary>
/// Name of the backing field.
/// </summary>
public string BackingField { get; private init; }

readonly TypeInfo returnType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,33 +84,43 @@ static StatementSyntax ThrowNotSupportedException (string message)
static StatementSyntax ThrowNotImplementedException ()
=> ThrowException (type: "NotImplementedException");

/// <summary>
/// Generate a field declaration.
/// </summary>
/// <param name="variableName">The variable name to be used with the field.</param>
/// <param name="variableType">The variable type.</param>
/// <param name="nullable">If the variable type should be made nullable.. </param>
/// <returns>The syntax for the field declaration.</returns>
internal static MemberDeclarationSyntax StaticVariable (string variableName, string variableType, bool nullable)
{
return FieldDeclaration (
VariableDeclaration (
nullable
? NullableType (IdentifierName (variableType))
: IdentifierName (variableType)
)
.WithVariables (
SingletonSeparatedList (
VariableDeclarator (
Identifier (variableName)))))
.WithModifiers (TokenList (Token (SyntaxKind.StaticKeyword)))
.NormalizeWhitespace ();
}

/// <summary>
/// Generates the syntax to declare the variable used by a field property.
/// </summary>
/// <param name="property">The field property whose backing variable we want to generate.</param>
/// <returns>The variable declaration syntax.</returns>
internal static CompilationUnitSyntax FieldPropertyBackingVariable (in Property property)
internal static MemberDeclarationSyntax FieldPropertyBackingVariable (in Property property)
{
var variableType = property.ReturnType.FullyQualifiedName;
if (property.ReturnType.SpecialType is SpecialType.System_IntPtr or SpecialType.System_UIntPtr
&& property.ReturnType.MetadataName is not null) {
variableType = property.ReturnType.MetadataName;
}
var compilationUnit = CompilationUnit ().WithMembers (
SingletonList<MemberDeclarationSyntax> (
FieldDeclaration (
VariableDeclaration (
property.IsReferenceType // nullable only for reference types
? NullableType (IdentifierName (variableType))
: IdentifierName (variableType)
)
.WithVariables (
SingletonSeparatedList (
VariableDeclarator (
Identifier (property.BackingField)))))
.WithModifiers (TokenList (Token (SyntaxKind.StaticKeyword))))) // fields are static variables
.NormalizeWhitespace ();
return compilationUnit;

return StaticVariable (property.BackingField, variableType, property.IsReferenceType);
}

/// <summary>
Expand Down