Skip to content

Commit

Permalink
[Rgen] Add extra data in the parameter struct to know if a parameter …
Browse files Browse the repository at this point in the history
…is an array. (#21884)
  • Loading branch information
mandel-macaque authored Jan 2, 2025
1 parent 90a0a60 commit b98b822
Show file tree
Hide file tree
Showing 5 changed files with 374 additions and 34 deletions.
14 changes: 3 additions & 11 deletions src/rgen/Microsoft.Macios.Generator/DataModel/Constructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,9 @@ public static bool TryCreate (ConstructorDeclarationSyntax declaration, Semantic
// loop over the parameters of the construct since changes on those implies a change in the generated code
foreach (var parameter in constructor.Parameters) {
var parameterDeclaration = declaration.ParameterList.Parameters [parameter.Ordinal];
parametersBucket.Add (new (parameter.Ordinal, parameter.Type.ToDisplayString ().Trim (),
parameter.Name) {
IsOptional = parameter.IsOptional,
IsParams = parameter.IsParams,
IsThis = parameter.IsThis,
IsNullable = parameter.NullableAnnotation == NullableAnnotation.Annotated,
IsSmartEnum = parameter.Type.IsSmartEnum (),
DefaultValue = (parameter.HasExplicitDefaultValue) ? parameter.ExplicitDefaultValue?.ToString () : null,
ReferenceKind = parameter.RefKind.ToReferenceKind (),
Attributes = parameterDeclaration.GetAttributeCodeChanges (semanticModel),
});
if (!Parameter.TryCreate (parameter, parameterDeclaration, semanticModel, out var parameterChange))
continue;
parametersBucket.Add (parameterChange.Value);
}

change = new (
Expand Down
14 changes: 3 additions & 11 deletions src/rgen/Microsoft.Macios.Generator/DataModel/Method.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,9 @@ public static bool TryCreate (MethodDeclarationSyntax declaration, SemanticModel
// loop over the parameters of the construct since changes on those implies a change in the generated code
foreach (var parameter in method.Parameters) {
var parameterDeclaration = declaration.ParameterList.Parameters [parameter.Ordinal];
parametersBucket.Add (new (parameter.Ordinal, parameter.Type.ToDisplayString ().Trim (),
parameter.Name) {
IsOptional = parameter.IsOptional,
IsParams = parameter.IsParams,
IsThis = parameter.IsThis,
IsNullable = parameter.NullableAnnotation == NullableAnnotation.Annotated,
IsSmartEnum = parameter.Type.IsSmartEnum (),
DefaultValue = (parameter.HasExplicitDefaultValue) ? parameter.ExplicitDefaultValue?.ToString () : null,
ReferenceKind = parameter.RefKind.ToReferenceKind (),
Attributes = parameterDeclaration.GetAttributeCodeChanges (semanticModel),
});
if (!Parameter.TryCreate (parameter, parameterDeclaration, semanticModel, out var parameterChange))
continue;
parametersBucket.Add (parameterChange.Value);
}

change = new (
Expand Down
37 changes: 35 additions & 2 deletions src/rgen/Microsoft.Macios.Generator/DataModel/Parameter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Macios.Generator.Extensions;

namespace Microsoft.Macios.Generator.DataModel;

Expand Down Expand Up @@ -48,6 +52,11 @@ namespace Microsoft.Macios.Generator.DataModel;
/// </summary>
public bool IsSmartEnum { get; init; }

/// <summary>
/// Returns if the parameter is an array type.
/// </summary>
public bool IsArray { get; init; }

/// <summary>
/// Optional default value.
/// </summary>
Expand All @@ -63,13 +72,33 @@ namespace Microsoft.Macios.Generator.DataModel;
/// </summary>
public ImmutableArray<AttributeCodeChange> Attributes { get; init; } = [];

public Parameter (int position, string type, string name)
internal Parameter (int position, string type, string name)
{
Position = position;
Type = type;
Name = name;
}

public static bool TryCreate (IParameterSymbol symbol, ParameterSyntax declaration, SemanticModel semanticModel,
[NotNullWhen (true)] out Parameter? parameter)
{
var type = symbol.Type is IArrayTypeSymbol arrayTypeSymbol
? arrayTypeSymbol.ElementType.ToDisplayString ()
: symbol.Type.ToDisplayString ().Trim ('?', '[', ']');
parameter = new (symbol.Ordinal, type, symbol.Name) {
IsOptional = symbol.IsOptional,
IsParams = symbol.IsParams,
IsThis = symbol.IsThis,
IsNullable = symbol.NullableAnnotation == NullableAnnotation.Annotated,
IsSmartEnum = symbol.Type.IsSmartEnum (),
IsArray = symbol.Type is IArrayTypeSymbol,
DefaultValue = (symbol.HasExplicitDefaultValue) ? symbol.ExplicitDefaultValue?.ToString () : null,
ReferenceKind = symbol.RefKind.ToReferenceKind (),
Attributes = declaration.GetAttributeCodeChanges (semanticModel),
};
return true;
}

/// <inheritdoc/>
public bool Equals (Parameter other)
{
Expand All @@ -89,6 +118,8 @@ public bool Equals (Parameter other)
return false;
if (IsSmartEnum != other.IsSmartEnum)
return false;
if (IsArray != other.IsArray)
return false;
if (DefaultValue != other.DefaultValue)
return false;
if (ReferenceKind != other.ReferenceKind)
Expand All @@ -115,6 +146,7 @@ public override int GetHashCode ()
hashCode.Add (IsThis);
hashCode.Add (IsNullable);
hashCode.Add (IsSmartEnum);
hashCode.Add (IsArray);
hashCode.Add (DefaultValue);
hashCode.Add ((int) ReferenceKind);
return hashCode.ToHashCode ();
Expand All @@ -140,10 +172,11 @@ public override string ToString ()
sb.Append ("Attributes: ");
sb.AppendJoin (", ", Attributes);
sb.Append ($" IsOptional: {IsOptional}, ");
sb.Append ($"IsParams {IsParams}, ");
sb.Append ($"IsParams: {IsParams}, ");
sb.Append ($"IsThis: {IsThis}, ");
sb.Append ($"IsNullable: {IsNullable}, ");
sb.Append ($"IsSmartEnum: {IsSmartEnum}, ");
sb.Append ($"IsArray: {IsArray}, ");
sb.Append ($"DefaultValue: {DefaultValue}, ");
sb.Append ($"ReferenceKind: {ReferenceKind} }}");
return sb.ToString ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public TestClass (string? inName, int inAge) {
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (0, "string?", "inName") { IsNullable = true, },
new (0, "string", "inName") { IsNullable = true, },
new (1, "int", "inAge"),
]
)
Expand Down Expand Up @@ -159,9 +159,184 @@ public TestClass (string? inName, int inAge, params string[] inSurnames) {
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (0, "string?", "inName") { IsNullable = true, },
new (0, "string", "inName") { IsNullable = true, },
new (1, "int", "inAge"),
new (2, "string[]", "inSurnames") { IsParams = true, },
new (2, "string", "inSurnames") { IsParams = true, IsArray = true },
]
)
];

const string arrayParameter = @"
using System;
namespace NS {
public class TestClass {
string name;
int age;
string [] surnames;
public TestClass (string? inName, int inAge, string[] inSurnames) {
name = inName ?? string.Empty;
age = inAge;
surnames = inSurnames;
}
}
}
";

yield return [
arrayParameter,
new Constructor (
type: "NS.TestClass",
symbolAvailability: new (),
attributes: [],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (0, "string", "inName") { IsNullable = true, },
new (1, "int", "inAge"),
new (2, "string", "inSurnames") { IsParams = false, IsArray = true },
]
)
];

const string nullableArrayParameter = @"
using System;
namespace NS {
public class TestClass {
string name;
int age;
string [] surnames;
public TestClass (string? inName, int inAge, string[]? inSurnames) {
name = inName ?? string.Empty;
age = inAge;
surnames = inSurnames;
}
}
}
";

yield return [
nullableArrayParameter,
new Constructor (
type: "NS.TestClass",
symbolAvailability: new (),
attributes: [],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (0, "string", "inName") { IsNullable = true, },
new (1, "int", "inAge"),
new (2, "string", "inSurnames") { IsNullable = true, IsArray = true },
]
)
];

const string arrayOfNullableParameter = @"
using System;
namespace NS {
public class TestClass {
string name;
int age;
string [] surnames;
public TestClass (string? inName, int inAge, string?[] inSurnames) {
name = inName ?? string.Empty;
age = inAge;
surnames = inSurnames;
}
}
}
";

yield return [
arrayOfNullableParameter,
new Constructor (
type: "NS.TestClass",
symbolAvailability: new (),
attributes: [],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (0, "string", "inName") { IsNullable = true, },
new (1, "int", "inAge"),
new (2, "string?", "inSurnames") { IsNullable = false, IsArray = true },
]
)
];

const string nullableArrayOfNullableParameter = @"
using System;
namespace NS {
public class TestClass {
string name;
int age;
string [] surnames;
public TestClass (string? inName, int inAge, string?[]? inSurnames) {
name = inName ?? string.Empty;
age = inAge;
surnames = inSurnames;
}
}
}
";

yield return [
nullableArrayOfNullableParameter,
new Constructor (
type: "NS.TestClass",
symbolAvailability: new (),
attributes: [],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (0, "string", "inName") { IsNullable = true, },
new (1, "int", "inAge"),
new (2, "string?", "inSurnames") { IsNullable = true, IsArray = true },
]
)
];

const string twoDimensionalArrayParameter = @"
using System;
namespace NS {
public class TestClass {
string name;
int age;
string [] surnames;
public TestClass (string? inName, int inAge, string[][] inSurnames) {
name = inName ?? string.Empty;
age = inAge;
surnames = inSurnames;
}
}
}
";

yield return [
twoDimensionalArrayParameter,
new Constructor (
type: "NS.TestClass",
symbolAvailability: new (),
attributes: [],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (0, "string", "inName") { IsNullable = true, },
new (1, "int", "inAge"),
new (2, "string[]", "inSurnames") { IsParams = false, IsArray = true },
]
)
];
Expand Down Expand Up @@ -189,7 +364,7 @@ public TestClass (string? inName = null) {
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (0, "string?", "inName") { IsNullable = true, IsOptional = true, },
new (0, "string", "inName") { IsNullable = true, IsOptional = true, },
]
)
];
Expand Down Expand Up @@ -217,7 +392,7 @@ public TestClass (T? inName = null) {
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (0, "T?", "inName") { IsOptional = true, IsNullable = true, },
new (0, "T", "inName") { IsOptional = true, IsNullable = true, },
]
)
];
Expand Down Expand Up @@ -251,7 +426,7 @@ public TestClass (string? inName = null) {
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (0, "string?", "inName") { IsNullable = true, IsOptional = true, },
new (0, "string", "inName") { IsNullable = true, IsOptional = true, },
]
)
];
Expand Down
Loading

10 comments on commit b98b822

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ [CI Build] Build passed (Build packages) ✅

Pipeline on Agent
Hash: b98b822f861657a49972da6cd49a168d40a3dcf9 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ [CI Build] Build passed (Detect API changes) ✅

Pipeline on Agent
Hash: b98b822f861657a49972da6cd49a168d40a3dcf9 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ [CI Build] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: b98b822f861657a49972da6cd49a168d40a3dcf9 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💻 [CI Build] Windows Integration Tests passed 💻

All Windows Integration Tests passed.

Pipeline on Agent
Hash: b98b822f861657a49972da6cd49a168d40a3dcf9 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💻 [CI Build] Tests on macOS M1 - Mac Monterey (12) passed 💻

All tests on macOS M1 - Mac Monterey (12) passed.

Pipeline on Agent
Hash: b98b822f861657a49972da6cd49a168d40a3dcf9 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💻 [CI Build] Tests on macOS X64 - Mac Sonoma (14) passed 💻

All tests on macOS X64 - Mac Sonoma (14) passed.

Pipeline on Agent
Hash: b98b822f861657a49972da6cd49a168d40a3dcf9 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💻 [CI Build] Tests on macOS arm64 - Mac Sequoia (15) passed 💻

All tests on macOS arm64 - Mac Sequoia (15) passed.

Pipeline on Agent
Hash: b98b822f861657a49972da6cd49a168d40a3dcf9 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💻 [CI Build] Tests on macOS M1 - Mac Ventura (13) passed 💻

All tests on macOS M1 - Mac Ventura (13) passed.

Pipeline on Agent
Hash: b98b822f861657a49972da6cd49a168d40a3dcf9 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ API diff for current PR / commit

.NET (No breaking changes)

❗ API diff vs stable (Breaking changes)

.NET (:heavy_exclamation_mark: Breaking changes :heavy_exclamation_mark:)
  • iOS: vsdrops gist (:heavy_exclamation_mark: Breaking changes :heavy_exclamation_mark:)
  • tvOS: vsdrops gist (:heavy_exclamation_mark: Breaking changes :heavy_exclamation_mark:)
  • MacCatalyst: vsdrops gist (:heavy_exclamation_mark: Breaking changes :heavy_exclamation_mark:)
  • macOS: vsdrops gist (:heavy_exclamation_mark: Breaking changes :heavy_exclamation_mark:)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: b98b822f861657a49972da6cd49a168d40a3dcf9 [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 [CI Build] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

2 tests crashed, 0 tests failed, 98 tests passed.

Failures

❌ dotnettests tests (MacCatalyst)

🔥 Failed catastrophically on VSTS: test results - dotnettests_maccatalyst (no summary found).

Html Report (VSDrops) Download

❌ monotouch tests (iOS)

🔥 Failed catastrophically on VSTS: test results - monotouch_ios (no summary found).

Html Report (VSDrops) Download

Successes

✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 4 tests passed. Html Report (VSDrops) Download
✅ linker: All 40 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 10 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 9 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 8 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: b98b822f861657a49972da6cd49a168d40a3dcf9 [CI build]

Please sign in to comment.