Skip to content

Commit

Permalink
[Rgen] Generate the property signatures. (#22172)
Browse files Browse the repository at this point in the history
  • Loading branch information
mandel-macaque authored Feb 13, 2025
1 parent f5fab03 commit 6e37e60
Show file tree
Hide file tree
Showing 7 changed files with 610 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/ObjCBindings/ExportTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public enum Property : Int64 {
/// <summary>
/// The backing field for a property to be annotated with the .NET [ThreadStatic] attribute.
/// </summary>
IsThreadStaticAttribute = 1 << 2,
IsThreadStatic = 1 << 2,

/// <summary>
/// Generate a notification for the property.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static bool TryParse (AttributeData attributeData,
// there are two possible cases in this situation.
// 1. The second argument is an ArgumentSemantic
// 2. The second argument is a T
if (attributeData.ConstructorArguments [1].Value is ArgumentSemantic) {
if (attributeData.ConstructorArguments [1].Type?.Name == nameof (ObjCRuntime.ArgumentSemantic)) {
selector = (string?) attributeData.ConstructorArguments [0].Value!;
argumentSemantic = (ArgumentSemantic) attributeData.ConstructorArguments [1].Value!;
} else {
Expand Down
39 changes: 39 additions & 0 deletions src/rgen/Microsoft.Macios.Generator/Emitters/ClassEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,44 @@ void EmitFields (string className, in ImmutableArray<Property> properties, Tabbe
notificationProperties = notificationsBuilder.ToImmutable ();
}

void EmitProperties (in ImmutableArray<Property> properties, TabbedWriter<StringWriter> classBlock)
{

foreach (var property in properties.OrderBy (p => p.Name)) {
if (property.IsField)
// ignore fields
continue;

// we expect to always at least have a getter
var getter = property.GetAccessor (AccessorKind.Getter);
if (getter is null)
continue;

classBlock.WriteLine ();
classBlock.AppendMemberAvailability (property.SymbolAvailability);
classBlock.AppendGeneratedCodeAttribute (optimizable: true);

using (var propertyBlock = classBlock.CreateBlock (property.ToDeclaration ().ToString (), block: true)) {
// be very verbose with the availability, makes the life easier to the dotnet analyzer
propertyBlock.AppendMemberAvailability (getter.Value.SymbolAvailability);
using (var getterBlock = propertyBlock.CreateBlock ("get", block: true)) {
getterBlock.WriteLine ("throw new NotImplementedException();");
}

var setter = property.GetAccessor (AccessorKind.Setter);
if (setter is null)
// we are done with the current property
continue;

propertyBlock.WriteLine (); // add space between getter and setter since we have the attrs
propertyBlock.AppendMemberAvailability (setter.Value.SymbolAvailability);
using (var setterBlock = propertyBlock.CreateBlock ("set", block: true)) {
setterBlock.WriteLine ("throw new NotImplementedException();");
}
}
}
}

void EmitNotifications (in ImmutableArray<Property> properties, TabbedWriter<StringWriter> classBlock)
{
// to be implemented, do not throw or tests will fail.
Expand Down Expand Up @@ -189,6 +227,7 @@ public bool TryEmit (in BindingContext bindingContext, [NotNullWhen (false)] out

EmitFields (bindingContext.Changes.Name, bindingContext.Changes.Properties, classBlock,
out var notificationProperties);
EmitProperties (bindingContext.Changes.Properties, classBlock);

// emit the notification helper classes, leave this for the very bottom of the class
EmitNotifications (notificationProperties, classBlock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public static bool IsWrapped (this ITypeSymbol symbol, bool isNSObject)
/// itself.
/// </summary>
/// <param name="symbol">The symbol to check if it is wrapped.</param>
/// <returns>True if the ymbol is considered to be wrapped.</returns>
/// <returns>True if the symbol is considered to be wrapped.</returns>
public static bool IsWrapped (this ITypeSymbol symbol)
{
symbol.GetInheritance (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public class TestDataGenerator : BaseTestDataGenerator, IEnumerable<object []> {
(ApplePlatform.iOS, "CIImage", "CIImage.cs", "ExpectedCIImage.cs", null),
(ApplePlatform.TVOS, "CIImage", "CIImage.cs", "ExpectedCIImage.cs", null),
(ApplePlatform.MacCatalyst, "CIImage", "CIImage.cs", "ExpectedCIImage.cs", null),
(ApplePlatform.iOS, "PropertyTests", "PropertyTests.cs", "ExpectedPropertyTests.cs", null),
(ApplePlatform.TVOS, "PropertyTests", "PropertyTests.cs", "ExpectedPropertyTests.cs", null),
(ApplePlatform.MacCatalyst, "PropertyTests", "PropertyTests.cs", "ExpectedPropertyTests.cs", null),
(ApplePlatform.MacOSX, "PropertyTests", "PropertyTests.cs", "ExpectedPropertyTests.cs", null),

};

Expand Down
Loading

10 comments on commit 6e37e60

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.