Skip to content

Commit

Permalink
[RGen] Add the named type symbol platform availability to the code ch…
Browse files Browse the repository at this point in the history
…ange struct. (#21858)

Reduce the amount of work we need to do in the code generation by
precalculating the availability of the class. This way we can later use
the code change struct to generate the code.

---------

Co-authored-by: GitHub Actions Autoformatter <[email protected]>
  • Loading branch information
mandel-macaque and GitHub Actions Autoformatter authored Dec 27, 2024
1 parent 6286034 commit 2b49db1
Show file tree
Hide file tree
Showing 11 changed files with 634 additions and 100 deletions.
21 changes: 15 additions & 6 deletions src/rgen/Microsoft.Macios.Generator/DataModel/CodeChanges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Macios.Generator.Availability;
using Microsoft.Macios.Generator.Extensions;

namespace Microsoft.Macios.Generator.DataModel;
Expand Down Expand Up @@ -34,6 +35,11 @@ readonly struct CodeChanges {
/// </summary>
public string FullyQualifiedSymbol { get; }

/// <summary>
/// The platform availability of the named type.
/// </summary>
public SymbolAvailability SymbolAvailability { get; }

/// <summary>
/// Changes to the attributes of the symbol.
/// </summary>
Expand Down Expand Up @@ -177,12 +183,15 @@ static void GetMembers<T, TR> (TypeDeclarationSyntax baseDeclarationSyntax, Sema
/// <param name="name">The name of the named type that created the code change.</param>
/// <param name="namespace">The namespace that contains the named type.</param>
/// <param name="fullyQualifiedSymbol">The fully qualified name of the symbol.</param>
internal CodeChanges (BindingType bindingType, string name, ImmutableArray<string> @namespace, string fullyQualifiedSymbol)
/// <param name="symbolAvailability">The platform availability of the named symbol.</param>
internal CodeChanges (BindingType bindingType, string name, ImmutableArray<string> @namespace,
string fullyQualifiedSymbol, SymbolAvailability symbolAvailability)
{
BindingType = bindingType;
Name = name;
Namespace = @namespace;
FullyQualifiedSymbol = fullyQualifiedSymbol;
SymbolAvailability = symbolAvailability;
}

/// <summary>
Expand All @@ -192,7 +201,7 @@ internal CodeChanges (BindingType bindingType, string name, ImmutableArray<strin
/// <param name="semanticModel">The semantic model of the compilation.</param>
CodeChanges (EnumDeclarationSyntax enumDeclaration, SemanticModel semanticModel)
{
(Name, Namespace) = semanticModel.GetNameAndNamespace (enumDeclaration);
(Name, Namespace, SymbolAvailability) = semanticModel.GetSymbolData (enumDeclaration);
BindingType = BindingType.SmartEnum;
FullyQualifiedSymbol = enumDeclaration.GetFullyQualifiedIdentifier ();
Attributes = enumDeclaration.GetAttributeCodeChanges (semanticModel);
Expand Down Expand Up @@ -224,7 +233,7 @@ internal CodeChanges (BindingType bindingType, string name, ImmutableArray<strin
/// <param name="semanticModel">The semantic model of the compilation.</param>
CodeChanges (ClassDeclarationSyntax classDeclaration, SemanticModel semanticModel)
{
(Name, Namespace) = semanticModel.GetNameAndNamespace (classDeclaration);
(Name, Namespace, SymbolAvailability) = semanticModel.GetSymbolData (classDeclaration);
BindingType = BindingType.Class;
FullyQualifiedSymbol = classDeclaration.GetFullyQualifiedIdentifier ();
Attributes = classDeclaration.GetAttributeCodeChanges (semanticModel);
Expand All @@ -247,7 +256,7 @@ internal CodeChanges (BindingType bindingType, string name, ImmutableArray<strin
/// <param name="semanticModel">The semantic model of the compilation.</param>
CodeChanges (InterfaceDeclarationSyntax interfaceDeclaration, SemanticModel semanticModel)
{
(Name, Namespace) = semanticModel.GetNameAndNamespace (interfaceDeclaration);
(Name, Namespace, SymbolAvailability) = semanticModel.GetSymbolData (interfaceDeclaration);
BindingType = BindingType.Protocol;
FullyQualifiedSymbol = interfaceDeclaration.GetFullyQualifiedIdentifier ();
Attributes = interfaceDeclaration.GetAttributeCodeChanges (semanticModel);
Expand Down Expand Up @@ -282,9 +291,9 @@ internal CodeChanges (BindingType bindingType, string name, ImmutableArray<strin
public override string ToString ()
{
var sb = new StringBuilder ("Changes: {");
sb.Append ($"BindingType: {BindingType}, Name: {Name}, Namespace: [");
sb.Append ($"BindingType: '{BindingType}', Name: '{Name}', Namespace: [");
sb.AppendJoin (", ", Namespace);
sb.Append ($"], FullyQualifiedSymbol: {FullyQualifiedSymbol}, ");
sb.Append ($"], FullyQualifiedSymbol: '{FullyQualifiedSymbol}', SymbolAvailability: {SymbolAvailability}, ");
sb.Append ("Attributes: [");
sb.AppendJoin (", ", Attributes);
sb.Append ("], EnumMembers: [");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public override bool Equals (CodeChanges x, CodeChanges y)
return false;
if (x.FullyQualifiedSymbol != y.FullyQualifiedSymbol)
return false;
if (x.SymbolAvailability != y.SymbolAvailability)
return false;
if (x.BindingType != y.BindingType)
return false;
if (x.Attributes.Length != y.Attributes.Length)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Microsoft.Macios.Generator.DataModel;
public string Type { get; }

/// <summary>
/// The platform availability of the enum value.
/// The platform availability of the constructor.
/// </summary>
public SymbolAvailability SymbolAvailability { get; }

Expand Down
2 changes: 1 addition & 1 deletion src/rgen/Microsoft.Macios.Generator/DataModel/Method.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Microsoft.Macios.Generator.DataModel;
public string ReturnType { get; }

/// <summary>
/// The platform availability of the eum value.
/// The platform availability of the method.
/// </summary>
public SymbolAvailability SymbolAvailability { get; }

Expand Down
2 changes: 1 addition & 1 deletion src/rgen/Microsoft.Macios.Generator/DataModel/Property.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Microsoft.Macios.Generator.DataModel;
public string Type { get; } = string.Empty;

/// <summary>
/// The platform availability of the enum value.
/// The platform availability of the property.
/// </summary>
public SymbolAvailability SymbolAvailability { get; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Macios.Generator.Availability;

namespace Microsoft.Macios.Generator.Extensions;

public static class SemanticModelExtensions {
static class SemanticModelExtensions {

/// <summary>
/// Returns the name and namespace of the symbol that has been declared in the passed base type declaration
/// syntax node.
/// </summary>
/// <param name="self">The current semantic model.</param>
/// <param name="declaration">The named type declaration syntaxt.</param>
/// <returns>A tuple containing the name and namespace of the type. If they could not be calculated they will
/// <returns>A tuple containing the name and namespace of the type. If they could not be calculated, they will
/// be set to be string.Empty.</returns>
public static (string Name, ImmutableArray<string> Namespace) GetNameAndNamespace (this SemanticModel self,
public static (string Name, ImmutableArray<string> Namespace, SymbolAvailability SymbolAvailability) GetSymbolData (this SemanticModel self,
BaseTypeDeclarationSyntax declaration)
{
var symbol = self.GetDeclaredSymbol (declaration);
Expand All @@ -26,6 +28,7 @@ public static (string Name, ImmutableArray<string> Namespace) GetNameAndNamespac
bucket.Insert (0, ns.Name);
ns = ns.ContainingNamespace;
}
return (name, bucket.ToImmutableArray ());
var availability = symbol?.GetSupportedPlatforms () ?? new SymbolAvailability ();
return (name, bucket.ToImmutableArray (), availability);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Macios.Generator.Attributes;
using Microsoft.Macios.Generator.Availability;
using Microsoft.Macios.Generator.DataModel;
using Xamarin.Tests;
using Xamarin.Utils;
Expand All @@ -16,6 +18,11 @@ public class ClassCodeChangesTests : BaseGeneratorTestClass {
class TestDataCodeChangesFromClassDeclaration : IEnumerable<object []> {
public IEnumerator<object []> GetEnumerator ()
{
var builder = SymbolAvailability.CreateBuilder ();
builder.Add (new SupportedOSPlatformData ("ios17.0"));
builder.Add (new SupportedOSPlatformData ("tvos17.0"));
builder.Add (new UnsupportedOSPlatformData ("macos"));

const string emptyClass = @"
using Foundation;
using ObjCRuntime;
Expand All @@ -34,14 +41,49 @@ public partial class MyClass {
bindingType: BindingType.Class,
name: "MyClass",
@namespace: ["NS"],
fullyQualifiedSymbol: "NS.MyClass"
fullyQualifiedSymbol: "NS.MyClass",
symbolAvailability: new ()
) {
Attributes = [
new ("ObjCBindings.BindingTypeAttribute")
]
}
];

const string emptyClassAvailability = @"
using System.Runtime.Versioning;
using Foundation;
using ObjCRuntime;
using ObjCBindings;
namespace NS;
[BindingType]
[SupportedOSPlatform (""ios17.0"")]
[SupportedOSPlatform (""tvos17.0"")]
[UnsupportedOSPlatform (""macos"")]
public partial class MyClass {
}
";

yield return [
emptyClassAvailability,
new CodeChanges (
bindingType: BindingType.Class,
name: "MyClass",
@namespace: ["NS"],
fullyQualifiedSymbol: "NS.MyClass",
symbolAvailability: builder.ToImmutable ()
) {
Attributes = [
new ("ObjCBindings.BindingTypeAttribute"),
new ("System.Runtime.Versioning.UnsupportedOSPlatformAttribute", ["macos"]),
new ("System.Runtime.Versioning.SupportedOSPlatformAttribute", ["tvos17.0"]),
new ("System.Runtime.Versioning.SupportedOSPlatformAttribute", ["ios17.0"]),
]
}
];

const string singleConstructorClass = @"
using ObjCBindings;
Expand All @@ -61,7 +103,8 @@ public MyClass () {}
bindingType: BindingType.Class,
name: "MyClass",
@namespace: ["NS"],
fullyQualifiedSymbol: "NS.MyClass"
fullyQualifiedSymbol: "NS.MyClass",
symbolAvailability: new ()
) {
Attributes = [
new ("ObjCBindings.BindingTypeAttribute")
Expand Down Expand Up @@ -103,7 +146,8 @@ public MyClass(string inName) {
bindingType: BindingType.Class,
name: "MyClass",
@namespace: ["NS"],
fullyQualifiedSymbol: "NS.MyClass"
fullyQualifiedSymbol: "NS.MyClass",
symbolAvailability: new ()
) {
Attributes = [
new ("ObjCBindings.BindingTypeAttribute")
Expand Down Expand Up @@ -155,7 +199,8 @@ public partial class MyClass {
bindingType: BindingType.Class,
name: "MyClass",
@namespace: ["NS"],
fullyQualifiedSymbol: "NS.MyClass"
fullyQualifiedSymbol: "NS.MyClass",
symbolAvailability: new ()
) {
Attributes = [
new ("ObjCBindings.BindingTypeAttribute")
Expand Down Expand Up @@ -201,7 +246,8 @@ public partial class MyClass {
bindingType: BindingType.Class,
name: "MyClass",
@namespace: ["NS"],
fullyQualifiedSymbol: "NS.MyClass"
fullyQualifiedSymbol: "NS.MyClass",
symbolAvailability: new ()
) {
Attributes = [
new ("ObjCBindings.BindingTypeAttribute")
Expand Down Expand Up @@ -248,7 +294,8 @@ public partial class MyClass {
bindingType: BindingType.Class,
name: "MyClass",
@namespace: ["NS"],
fullyQualifiedSymbol: "NS.MyClass"
fullyQualifiedSymbol: "NS.MyClass",
symbolAvailability: new ()
) {
Attributes = [
new ("ObjCBindings.BindingTypeAttribute")
Expand Down Expand Up @@ -308,7 +355,8 @@ public partial class MyClass {
bindingType: BindingType.Class,
name: "MyClass",
@namespace: ["NS"],
fullyQualifiedSymbol: "NS.MyClass"
fullyQualifiedSymbol: "NS.MyClass",
symbolAvailability: new ()
) {
Attributes = [
new ("ObjCBindings.BindingTypeAttribute")
Expand Down Expand Up @@ -354,7 +402,8 @@ public void SetSurname (string inSurname) {}
bindingType: BindingType.Class,
name: "MyClass",
@namespace: ["NS"],
fullyQualifiedSymbol: "NS.MyClass"
fullyQualifiedSymbol: "NS.MyClass",
symbolAvailability: new ()
) {
Attributes = [
new ("ObjCBindings.BindingTypeAttribute")
Expand Down Expand Up @@ -401,7 +450,8 @@ public partial class MyClass {
bindingType: BindingType.Class,
name: "MyClass",
@namespace: ["NS"],
fullyQualifiedSymbol: "NS.MyClass"
fullyQualifiedSymbol: "NS.MyClass",
symbolAvailability: new ()
) {
Attributes = [
new ("ObjCBindings.BindingTypeAttribute")
Expand Down Expand Up @@ -462,7 +512,8 @@ public partial class MyClass {
bindingType: BindingType.Class,
name: "MyClass",
@namespace: ["NS"],
fullyQualifiedSymbol: "NS.MyClass"
fullyQualifiedSymbol: "NS.MyClass",
symbolAvailability: new ()
) {
Attributes = [
new ("ObjCBindings.BindingTypeAttribute")
Expand Down Expand Up @@ -505,7 +556,8 @@ public partial class MyClass {
bindingType: BindingType.Class,
name: "MyClass",
@namespace: ["NS"],
fullyQualifiedSymbol: "NS.MyClass"
fullyQualifiedSymbol: "NS.MyClass",
symbolAvailability: new ()
) {
Attributes = [
new ("ObjCBindings.BindingTypeAttribute")
Expand Down
Loading

9 comments on commit 2b49db1

@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.