Skip to content

Commit

Permalink
[Rgen] Move to use TypeInfo as the Type property in Paramters.
Browse files Browse the repository at this point in the history
This will later be used to be ablet to correctly calculate the signature
of the ObjCRuntime messaging method to perform a native call to a
property or method.
  • Loading branch information
mandel-macaque committed Jan 17, 2025
1 parent 02ad356 commit adf2ef2
Show file tree
Hide file tree
Showing 15 changed files with 272 additions and 304 deletions.
48 changes: 3 additions & 45 deletions src/rgen/Microsoft.Macios.Generator/DataModel/Parameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Microsoft.Macios.Generator.DataModel;
/// <summary>
/// Type of the parameter.
/// </summary>
public string Type { get; }
public TypeInfo Type { get; }

/// <summary>
/// Parameter name
Expand All @@ -44,26 +44,6 @@ namespace Microsoft.Macios.Generator.DataModel;
/// </summary>
public bool IsThis { get; init; }

/// <summary>
/// True if the parameter is nullable.:w
/// </summary>
public bool IsNullable { get; init; }

/// <summary>
/// True if the parameter type is blittable.
/// </summary>
public bool IsBlittable { get; }

/// <summary>
/// Returns if the parameter type is a smart enum.
/// </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 @@ -90,33 +70,26 @@ namespace Microsoft.Macios.Generator.DataModel;
/// </summary>
public ImmutableArray<AttributeCodeChange> Attributes { get; init; } = [];

public Parameter (int position, string type, string name, bool isBlittable)
public Parameter (int position, TypeInfo type, string name)
{
Position = position;
Type = type;
Name = name;
IsBlittable = isBlittable;
}

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 ('?', '[', ']');
DelegateInfo? delegateInfo = null;
if (symbol.Type is INamedTypeSymbol namedTypeSymbol
&& namedTypeSymbol.DelegateInvokeMethod is not null) {
DelegateInfo.TryCreate (namedTypeSymbol.DelegateInvokeMethod, out delegateInfo);
}

parameter = new (symbol.Ordinal, type, symbol.Name, symbol.Type.IsBlittable ()) {
parameter = new (symbol.Ordinal, new (symbol.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 (),
Delegate = delegateInfo,
Expand All @@ -140,14 +113,6 @@ public bool Equals (Parameter other)
return false;
if (IsThis != other.IsThis)
return false;
if (IsNullable != other.IsNullable)
return false;
if (IsBlittable != other.IsBlittable)
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 Down Expand Up @@ -175,9 +140,6 @@ public override int GetHashCode ()
hashCode.Add (IsOptional);
hashCode.Add (IsParams);
hashCode.Add (IsThis);
hashCode.Add (IsNullable);
hashCode.Add (IsSmartEnum);
hashCode.Add (IsArray);
hashCode.Add (DefaultValue);
hashCode.Add ((int) ReferenceKind);
hashCode.Add (Delegate);
Expand Down Expand Up @@ -206,10 +168,6 @@ public override string ToString ()
sb.Append ($" IsOptional: {IsOptional}, ");
sb.Append ($"IsParams: {IsParams}, ");
sb.Append ($"IsThis: {IsThis}, ");
sb.Append ($"IsNullable: {IsNullable}, ");
sb.Append ($"IsBlittable: {IsBlittable}, ");
sb.Append ($"IsSmartEnum: {IsSmartEnum}, ");
sb.Append ($"IsArray: {IsArray}, ");
sb.Append ($"DefaultValue: {DefaultValue}, ");
sb.Append ($"ReferenceKind: {ReferenceKind}, ");
sb.Append ($"Delegate: {Delegate?.ToString () ?? "null"} }}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ public int Compare (Parameter x, Parameter y)
var value = x.Position.CompareTo (y.Position);
if (value != 0)
return value;
value = String.Compare (x.Type, y.Type, StringComparison.Ordinal);
var comparer = new TypeInfoComparer ();
value = comparer.Compare (x.Type, y.Type);
if (value != 0)
return value;
value = String.Compare (x.Name, y.Name, StringComparison.Ordinal);
if (value != 0)
return value;
var xValues = new [] { x.IsOptional, x.IsParams, x.IsThis, x.IsNullable };
var yValues = new [] { y.IsOptional, y.IsParams, y.IsThis, y.IsNullable };
var xValues = new [] { x.IsOptional, x.IsParams, x.IsThis };
var yValues = new [] { y.IsOptional, y.IsParams, y.IsThis };
for (int i = 0; i < xValues.Length; ++i) {
value = xValues [i].CompareTo (yValues [i]);
if (value != 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ public static ParameterListSyntax GetParameterList (this in ImmutableArray<Param
}
static TypeSyntax GetIdentifierSyntax (this in ParameterDataModel parameter)
{
if (parameter.IsArray) {
if (parameter.Type.IsArray) {
// could be a params array or simply an array
var arrayType = ArrayType (IdentifierName (parameter.Type))
var arrayType = ArrayType (IdentifierName (parameter.Type.Name))
.WithRankSpecifiers (SingletonList (
ArrayRankSpecifier (
SingletonSeparatedList<ExpressionSyntax> (OmittedArraySizeExpression ()))));
return parameter.IsNullable
return parameter.Type.IsNullable
? NullableType (arrayType)
: arrayType;
}

// dealing with a non-array type
return parameter.IsNullable
? NullableType (IdentifierName (parameter.Type))
: IdentifierName (parameter.Type);
return parameter.Type.IsNullable
? NullableType (IdentifierName (parameter.Type.Name))
: IdentifierName (parameter.Type.Name);
}

public static ParameterSyntax ToDeclaration (this in ParameterDataModel parameter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,8 @@ public MyClass(string name) {
parameters: [
new (
position: 0,
type: "string",
name: "name",
isBlittable: false
type: ReturnTypeForString (),
name: "name"
)
]
),
Expand Down Expand Up @@ -881,7 +880,7 @@ public partial class MyClass {
SyntaxFactory.Token (SyntaxKind.PartialKeyword),
],
parameters: [
new (position: 0, type: "string", name: "name", isBlittable: false)
new (position: 0, type: ReturnTypeForString (), name: "name")
]
),
]
Expand Down Expand Up @@ -936,7 +935,7 @@ public void SetSurname (string inSurname) {}
SyntaxFactory.Token (SyntaxKind.PartialKeyword),
],
parameters: [
new (position: 0, type: "string", name: "name", isBlittable: false)
new (position: 0, type: ReturnTypeForString (), name: "name")
]
),
]
Expand Down Expand Up @@ -992,7 +991,7 @@ public partial class MyClass {
SyntaxFactory.Token (SyntaxKind.PartialKeyword),
],
parameters: [
new (position: 0, type: "string", name: "name", isBlittable: false)
new (position: 0, type: ReturnTypeForString (), name: "name")
]
),
new (
Expand All @@ -1009,7 +1008,7 @@ public partial class MyClass {
SyntaxFactory.Token (SyntaxKind.PartialKeyword),
],
parameters: [
new (position: 0, type: "string", name: "inSurname", isBlittable: false)
new (position: 0, type: ReturnTypeForString (), name: "inSurname")
]
),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Macios.Generator.Availability;
using Microsoft.Macios.Generator.DataModel;
using Xunit;
using static Microsoft.Macios.Generator.Tests.TestDataFactory;

namespace Microsoft.Macios.Generator.Tests.DataModel;

Expand Down Expand Up @@ -1261,8 +1262,7 @@ public void CompareDifferentMethodsLength ()
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (position: 0, type: "string?", name: "example", isBlittable: false) {
IsNullable = true,
new (position: 0, type: ReturnTypeForString (isNullable: true), name: "example") {
ReferenceKind = ReferenceKind.Out,
},
]
Expand All @@ -1278,7 +1278,7 @@ public void CompareDifferentMethodsLength ()
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (position: 0, type: "NS.CustomType", name: "input", isBlittable: false)
new (position: 0, type: ReturnTypeForClass ("NS.CustomType"), name: "input")
]
)
]
Expand Down Expand Up @@ -1407,9 +1407,7 @@ public void CompareDifferentMethodsLength ()
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (position: 0, type: "string?", name: "example", isBlittable: false) {
IsNullable = true,
ReferenceKind = ReferenceKind.Out,
new (position: 0, type: ReturnTypeForString (isNullable: true), name: "example") {
},
]
),
Expand Down Expand Up @@ -1547,8 +1545,7 @@ public void CompareSameMethodsDiffOrder ()
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (position: 0, type: "string?", name: "example", isBlittable: false) {
IsNullable = true,
new (position: 0, type: ReturnTypeForString (isNullable: true), name: "example") {
ReferenceKind = ReferenceKind.Out,
},
]
Expand All @@ -1564,7 +1561,7 @@ public void CompareSameMethodsDiffOrder ()
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (position: 0, type: "NS.CustomType", name: "input", isBlittable: false)
new (position: 0, type: ReturnTypeForClass ("NS.CustomType"), name: "input")
]
)
]
Expand Down Expand Up @@ -1687,7 +1684,7 @@ public void CompareSameMethodsDiffOrder ()
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (position: 0, type: "NS.CustomType", name: "input", isBlittable: false)
new (position: 0, type: ReturnTypeForClass ("NS.CustomType"), name: "input")
]
),
new (
Expand All @@ -1708,8 +1705,7 @@ public void CompareSameMethodsDiffOrder ()
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (position: 0, type: "string?", name: "example", isBlittable: false) {
IsNullable = true,
new (position: 0, type: ReturnTypeForString (isNullable: true), name: "example") {
ReferenceKind = ReferenceKind.Out,
},
]
Expand Down Expand Up @@ -1841,7 +1837,7 @@ public void CompareDifferentMethods ()
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (position: 0, type: "NS.CustomType", name: "input", isBlittable: false),
new (position: 0, type: ReturnTypeForClass ("NS.CustomType"), name: "input"),
]
),
]
Expand Down Expand Up @@ -1971,8 +1967,7 @@ public void CompareDifferentMethods ()
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (position: 0, type: "string?", name: "example", isBlittable: false) {
IsNullable = true,
new (position: 0, type: ReturnTypeForString (isNullable: true), name: "example") {
ReferenceKind = ReferenceKind.Out,
},
]
Expand Down Expand Up @@ -2114,8 +2109,7 @@ public void CompareSameMethodsDiffAvailability ()
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (position: 0, type: "string?", name: "example", isBlittable: false) {
IsNullable = true,
new (position: 0, type: ReturnTypeForString (isNullable: true), name: "example") {
ReferenceKind = ReferenceKind.Out,
},
]
Expand All @@ -2131,7 +2125,7 @@ public void CompareSameMethodsDiffAvailability ()
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (position: 0, type: "NS.CustomType", name: "input", isBlittable: false)
new (position: 0, type: ReturnTypeForClass ("NS.CustomType"), name: "input")
]
)
]
Expand Down Expand Up @@ -2246,7 +2240,7 @@ public void CompareSameMethodsDiffAvailability ()
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (position: 0, type: "NS.CustomType", name: "input", isBlittable: false)
new (position: 0, type: ReturnTypeForClass ("NS.CustomType"), name: "input")
]
),
new (
Expand All @@ -2267,8 +2261,7 @@ public void CompareSameMethodsDiffAvailability ()
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (position: 0, type: "string?", name: "example", isBlittable: false) {
IsNullable = true,
new (position: 0, type: ReturnTypeForString (isNullable: true), name: "example") {
ReferenceKind = ReferenceKind.Out,
},
]
Expand Down Expand Up @@ -2410,8 +2403,7 @@ public void CompareSameMethodsSameAvailability ()
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (position: 0, type: "string?", name: "example", isBlittable: false) {
IsNullable = true,
new (position: 0, type: ReturnTypeForString (isNullable: true), name: "example") {
ReferenceKind = ReferenceKind.Out,
},
]
Expand All @@ -2427,7 +2419,7 @@ public void CompareSameMethodsSameAvailability ()
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (position: 0, type: "NS.CustomType", name: "input", isBlittable: false)
new (position: 0, type: ReturnTypeForClass ("NS.CustomType"), name: "input")
]
)
]
Expand Down Expand Up @@ -2549,7 +2541,7 @@ public void CompareSameMethodsSameAvailability ()
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (position: 0, type: "NS.CustomType", name: "input", isBlittable: false)
new (position: 0, type: ReturnTypeForClass ("NS.CustomType"), name: "input")
]
),
new (
Expand All @@ -2570,8 +2562,7 @@ public void CompareSameMethodsSameAvailability ()
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: [
new (position: 0, type: "string?", name: "example", isBlittable: false) {
IsNullable = true,
new (position: 0, type: ReturnTypeForString (isNullable: true), name: "example") {
ReferenceKind = ReferenceKind.Out,
},
]
Expand Down
Loading

0 comments on commit adf2ef2

Please sign in to comment.