Skip to content

Commit

Permalink
[RGen] Add the platform availabiltiy to the method data model. (#21851)
Browse files Browse the repository at this point in the history
This way we have all the needed data when generated the generated code
taking into account the availability of the parent objects.

---------

Co-authored-by: GitHub Actions Autoformatter <[email protected]>
  • Loading branch information
mandel-macaque and GitHub Actions Autoformatter authored Dec 23, 2024
1 parent 5c76b9a commit 84de7cd
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 2 deletions.
35 changes: 34 additions & 1 deletion src/rgen/Microsoft.Macios.Generator/DataModel/Method.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,58 @@
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Macios.Generator.Availability;
using Microsoft.Macios.Generator.Extensions;

namespace Microsoft.Macios.Generator.DataModel;

readonly struct Method : IEquatable<Method> {

/// <summary>
/// Type name that owns the constructor.
/// </summary>
public string Type { get; }

/// <summary>
/// Method name.
/// </summary>
public string Name { get; }

/// <summary>
/// Method return type.
/// </summary>
public string ReturnType { get; }

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

/// <summary>
/// Get the attributes added to the constructor.
/// </summary>
public ImmutableArray<AttributeCodeChange> Attributes { get; } = [];

/// <summary>
/// Modifiers list.
/// </summary>
public ImmutableArray<SyntaxToken> Modifiers { get; } = [];

/// <summary>
/// Parameters list.
/// </summary>
public ImmutableArray<Parameter> Parameters { get; } = [];

public Method (string type, string name, string returnType, ImmutableArray<AttributeCodeChange> attributes,
public Method (string type, string name, string returnType,
SymbolAvailability symbolAvailability,
ImmutableArray<AttributeCodeChange> attributes,
ImmutableArray<SyntaxToken> modifiers,
ImmutableArray<Parameter> parameters)
{
Type = type;
Name = name;
ReturnType = returnType;
SymbolAvailability = symbolAvailability;
Attributes = attributes;
Modifiers = modifiers;
Parameters = parameters;
Expand Down Expand Up @@ -62,6 +91,7 @@ public static bool TryCreate (MethodDeclarationSyntax declaration, SemanticModel
type: method.ContainingSymbol.ToDisplayString ().Trim (), // we want the full name
name: method.Name,
returnType: method.ReturnType.ToDisplayString ().Trim (),
symbolAvailability: method.GetSupportedPlatforms (),
attributes: attributes,
modifiers: [.. declaration.Modifiers],
parameters: parametersBucket.ToImmutableArray ());
Expand All @@ -77,6 +107,9 @@ public bool Equals (Method other)
return false;
if (ReturnType != other.ReturnType)
return false;
if (SymbolAvailability != other.SymbolAvailability)
return false;

var attrsComparer = new AttributesEqualityComparer ();
if (!attrsComparer.Equals (Attributes, other.Attributes))
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public int Compare (Method x, Method y)
if (attributesLengthCompare != 0)
return attributesLengthCompare;

// sort by the attributes
// sort by the attributes, this allows us not to do a compare against the platform availability
// since that is generated via the attrs
var attributeComparer = new AttributeComparer ();
var xAttributes = x.Attributes.Order (attributeComparer).ToArray ();
var yAttributes = y.Attributes.Order (attributeComparer).ToArray ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ public partial class MyClass {
type: "NS.MyClass",
name: "SetName",
returnType: "void",
symbolAvailability: new (),
attributes: [
new ("ObjCBindings.ExportAttribute<ObjCBindings.Method>", ["withName:"])
],
Expand Down Expand Up @@ -343,6 +344,7 @@ public void SetSurname (string inSurname) {}
type: "NS.MyClass",
name: "SetName",
returnType: "void",
symbolAvailability: new (),
attributes: [
new ("ObjCBindings.ExportAttribute<ObjCBindings.Method>", ["withName:"])
],
Expand Down Expand Up @@ -387,6 +389,7 @@ public partial class MyClass {
type: "NS.MyClass",
name: "SetName",
returnType: "void",
symbolAvailability: new (),
attributes: [
new ("ObjCBindings.ExportAttribute<ObjCBindings.Method>", ["withName:"])
],
Expand All @@ -402,6 +405,7 @@ public partial class MyClass {
type: "NS.MyClass",
name: "SetSurname",
returnType: "void",
symbolAvailability: new (),
attributes: [
new ("ObjCBindings.ExportAttribute<ObjCBindings.Method>", ["withSurname:"])
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ public void CompareDifferentMethodsLength ()
type: "NS.MyClass",
name: "TryGetString",
returnType: "bool",
symbolAvailability: new (),
attributes: [],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
Expand All @@ -665,6 +666,7 @@ public void CompareDifferentMethodsLength ()
type: "NS.MyClass",
name: "MyMethod",
returnType: "void",
symbolAvailability: new (),
attributes: [],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
Expand Down Expand Up @@ -735,6 +737,7 @@ public void CompareDifferentMethodsLength ()
type: "NS.MyClass",
name: "TryGetString",
returnType: "bool",
symbolAvailability: new (),
attributes: [],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
Expand Down Expand Up @@ -815,6 +818,7 @@ public void CompareSameMethodsDiffOrder ()
type: "NS.MyClass",
name: "TryGetString",
returnType: "bool",
symbolAvailability: new (),
attributes: [],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
Expand All @@ -830,6 +834,7 @@ public void CompareSameMethodsDiffOrder ()
type: "NS.MyClass",
name: "MyMethod",
returnType: "void",
symbolAvailability: new (),
attributes: [],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
Expand Down Expand Up @@ -900,6 +905,7 @@ public void CompareSameMethodsDiffOrder ()
type: "NS.MyClass",
name: "MyMethod",
returnType: "void",
symbolAvailability: new (),
attributes: [],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
Expand All @@ -912,6 +918,7 @@ public void CompareSameMethodsDiffOrder ()
type: "NS.MyClass",
name: "TryGetString",
returnType: "bool",
symbolAvailability: new (),
attributes: [],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
Expand Down Expand Up @@ -992,6 +999,7 @@ public void CompareDifferentMethods ()
type: "NS.MyClass",
name: "MyMethod",
returnType: "void",
symbolAvailability: new (),
attributes: [],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
Expand Down Expand Up @@ -1062,6 +1070,7 @@ public void CompareDifferentMethods ()
type: "NS.MyClass",
name: "TryGetString",
returnType: "bool",
symbolAvailability: new (),
attributes: [],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ public partial interface IProtocol {
type: "NS.IProtocol",
name: "SetName",
returnType: "void",
symbolAvailability: new (),
attributes: [
new ("ObjCBindings.ExportAttribute<ObjCBindings.Method>", ["withName:"])
],
Expand Down Expand Up @@ -252,6 +253,7 @@ public void SetSurname (string inSurname) {}
type: "NS.IProtocol",
name: "SetName",
returnType: "void",
symbolAvailability: new (),
attributes: [
new ("ObjCBindings.ExportAttribute<ObjCBindings.Method>", ["withName:"])
],
Expand Down Expand Up @@ -296,6 +298,7 @@ public partial interface IProtocol {
type: "NS.IProtocol",
name: "SetName",
returnType: "void",
symbolAvailability: new (),
attributes: [
new ("ObjCBindings.ExportAttribute<ObjCBindings.Method>", ["withName:"])
],
Expand All @@ -311,6 +314,7 @@ public partial interface IProtocol {
type: "NS.IProtocol",
name: "SetSurname",
returnType: "void",
symbolAvailability: new (),
attributes: [
new ("ObjCBindings.ExportAttribute<ObjCBindings.Method>", ["withSurname:"])
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public void CompareDiffType ()
type: "MyType",
name: "MyMethod",
returnType: "void",
symbolAvailability: new (),
attributes: [],
modifiers: [],
parameters: []
Expand All @@ -24,6 +25,7 @@ public void CompareDiffType ()
type: "MyOtherType",
name: "MyMethod",
returnType: "void",
symbolAvailability: new (),
attributes: [],
modifiers: [],
parameters: []
Expand All @@ -39,6 +41,7 @@ public void CompareDiffName ()
type: "MyType",
name: "MyMethod",
returnType: "void",
symbolAvailability: new (),
attributes: [],
modifiers: [],
parameters: []
Expand All @@ -48,6 +51,7 @@ public void CompareDiffName ()
type: "MyType",
name: "MyOtherMethod",
returnType: "void",
symbolAvailability: new (),
attributes: [],
modifiers: [],
parameters: []
Expand All @@ -62,6 +66,7 @@ public void CompareDiffReturnType ()
type: "MyType",
name: "MyMethod",
returnType: "void",
symbolAvailability: new (),
attributes: [],
modifiers: [],
parameters: []
Expand All @@ -71,6 +76,7 @@ public void CompareDiffReturnType ()
type: "MyType",
name: "MyMethod",
returnType: "int",
symbolAvailability: new (),
attributes: [],
modifiers: [],
parameters: []
Expand All @@ -85,6 +91,7 @@ public void CompareModifierDiffLength ()
type: "MyType",
name: "MyMethod",
returnType: "void",
symbolAvailability: new (),
attributes: [],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
Expand All @@ -97,6 +104,7 @@ public void CompareModifierDiffLength ()
type: "MyType",
name: "MyMethod",
returnType: "void",
symbolAvailability: new (),
attributes: [],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
Expand All @@ -113,6 +121,7 @@ public void CompareDiffModifier ()
type: "MyType",
name: "MyMethod",
returnType: "void",
symbolAvailability: new (),
attributes: [],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PartialKeyword),
Expand All @@ -124,6 +133,7 @@ public void CompareDiffModifier ()
type: "MyType",
name: "MyMethod",
returnType: "void",
symbolAvailability: new (),
attributes: [],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
Expand All @@ -142,6 +152,7 @@ public void CompareAttrsDiffLength ()
type: "MyType",
name: "MyMethod",
returnType: "void",
symbolAvailability: new (),
attributes: [
new ("FirstAttr"),
new ("SecondAttr", ["first"]),
Expand All @@ -156,6 +167,7 @@ public void CompareAttrsDiffLength ()
type: "MyType",
name: "MyMethod",
returnType: "void",
symbolAvailability: new (),
attributes: [
new ("FirstAttr"),
],
Expand All @@ -174,6 +186,7 @@ public void CompareDiffAttrs ()
type: "MyType",
name: "MyMethod",
returnType: "void",
symbolAvailability: new (),
attributes: [
new ("FirstAttr"),
],
Expand All @@ -187,6 +200,7 @@ public void CompareDiffAttrs ()
type: "MyType",
name: "MyMethod",
returnType: "void",
symbolAvailability: new (),
attributes: [
new ("SecondAttr", ["first"]),
],
Expand All @@ -206,6 +220,7 @@ public void CompareParameterDiffLength ()
type: "MyType",
name: "MyMethod",
returnType: "void",
symbolAvailability: new (),
attributes: [
new ("FirstAttr"),
],
Expand All @@ -221,6 +236,7 @@ public void CompareParameterDiffLength ()
type: "MyType",
name: "MyMethod",
returnType: "void",
symbolAvailability: new (),
attributes: [
new ("FirstAttr"),
],
Expand All @@ -242,6 +258,7 @@ public void CompareDiffParameters ()
type: "MyType",
name: "MyMethod",
returnType: "void",
symbolAvailability: new (),
attributes: [
new ("FirstAttr"),
],
Expand All @@ -257,6 +274,7 @@ public void CompareDiffParameters ()
type: "MyType",
name: "MyMethod",
returnType: "void",
symbolAvailability: new (),
attributes: [
new ("FirstAttr"),
],
Expand Down
Loading

9 comments on commit 84de7cd

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