Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rgen] Add more information about the type in the TypeInfo struct. #22005

Merged
merged 3 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/rgen/Microsoft.Macios.Generator/AttributesNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ static class AttributesNames {
public const string SupportedOSPlatformAttribute = "System.Runtime.Versioning.SupportedOSPlatformAttribute";
public const string UnsupportedOSPlatformAttribute = "System.Runtime.Versioning.UnsupportedOSPlatformAttribute";
public const string ObsoletedOSPlatformAttribute = "System.Runtime.Versioning.ObsoletedOSPlatformAttribute";
public const string NativeEnumAttribute = "ObjCRuntime.NativeAttribute";

public static readonly string [] BindingTypes = [
BindingAttribute,
Expand Down
29 changes: 29 additions & 0 deletions src/rgen/Microsoft.Macios.Generator/DataModel/TypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ namespace Microsoft.Macios.Generator.DataModel;
/// </summary>
public bool IsVoid => SpecialType == SpecialType.System_Void;

/// <summary>
/// True if the type is for an interface.
/// </summary>
public bool IsInterface { get; init; }

/// <summary>
/// True if the type represents an integer that was built using one of the keywords, like byte, int, nint etc.
///
/// This can be used to decide if we should use the name of the metadata name to cast the value.
/// </summary>
public bool IsNativeIntegerType { get; init; }

/// <summary>
/// True if an enumerator was marked with the NativeAttribute.
/// </summary>
public bool IsNativeEnum { get; init; }

readonly bool isNSObject = false;

public bool IsNSObject {
Expand Down Expand Up @@ -131,6 +148,9 @@ symbol is IArrayTypeSymbol arrayTypeSymbol
IsSmartEnum = symbol.IsSmartEnum ();
IsArray = symbol is IArrayTypeSymbol;
IsReferenceType = symbol.IsReferenceType;
IsInterface = symbol.TypeKind == TypeKind.Interface;
IsNativeIntegerType = symbol.IsNativeIntegerType;
IsNativeEnum = symbol.HasAttribute (AttributesNames.NativeEnumAttribute);

// data that we can get from the symbol without being INamedType
symbol.GetInheritance (
Expand Down Expand Up @@ -182,6 +202,12 @@ public bool Equals (TypeInfo other)
return false;
if (EnumUnderlyingType != other.EnumUnderlyingType)
return false;
if (IsInterface != other.IsInterface)
return false;
if (IsNativeIntegerType != other.IsNativeIntegerType)
return false;
if (IsNativeEnum != other.IsNativeEnum)
return false;

// compare base classes and interfaces, order does not matter at all
var listComparer = new CollectionComparer<string> ();
Expand Down Expand Up @@ -230,6 +256,9 @@ public override string ToString ()
sb.Append ($"IsVoid : {IsVoid}, ");
sb.Append ($"IsNSObject : {IsNSObject}, ");
sb.Append ($"IsNativeObject: {IsINativeObject}, ");
sb.Append ($"IsInterface: {IsInterface}, ");
sb.Append ($"IsNativeIntegerType: {IsNativeIntegerType}, ");
sb.Append ($"IsNativeEnum: {IsNativeEnum}, ");
sb.Append ($"EnumUnderlyingType: '{EnumUnderlyingType?.ToString () ?? "null"}', ");
sb.Append ("Parents: [");
sb.AppendJoin (", ", parents);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,67 @@ public class MyClass {
parameters: []
)
];

const string interfaceMethod = @"
using System;
using System.Collections.Generic;

namespace NS {
public interface IInterface {}

public class MyClass {
public IInterface MyMethod () {}
}
}
";
yield return [
interfaceMethod,
new Method (
type: "NS.MyClass",
name: "MyMethod",
returnType: ReturnTypeForInterface ("NS.IInterface"),
symbolAvailability: new (),
exportMethodData: new (),
attributes: [],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: []
)
];

const string nativeEnumMethod = @"
using System;
using ObjCRuntime;
using System.Collections.Generic;

namespace NS {
[Native]
public enum MyEnum : int {
One,
Two
}

public class MyClass {
public MyEnum MyMethod () {}
}
}
";
yield return [
nativeEnumMethod,
new Method (
type: "NS.MyClass",
name: "MyMethod",
returnType: ReturnTypeForEnum ("NS.MyEnum", isNativeEnum: true),
symbolAvailability: new (),
exportMethodData: new (),
attributes: [],
modifiers: [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
],
parameters: []
)
];
}

IEnumerator IEnumerable.GetEnumerator () => GetEnumerator ();
Expand Down
13 changes: 12 additions & 1 deletion tests/rgen/Microsoft.Macios.Generator.Tests/TestDataFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public static TypeInfo ReturnTypeForIntPtr (bool isNullable = false)
"System.Runtime.Serialization.ISerializable"
],
MetadataName = "IntPtr",
IsNativeIntegerType = !isNullable,
};

public static TypeInfo ReturnTypeForBool ()
Expand Down Expand Up @@ -158,14 +159,23 @@ public static TypeInfo ReturnTypeForClass (string className)
Parents = ["object"]
};

public static TypeInfo ReturnTypeForInterface (string interfaceName)
=> new (
name: interfaceName,
isReferenceType: true
) {
Parents = [],
IsInterface = true,
};

public static TypeInfo ReturnTypeForStruct (string structName)
=> new (
name: structName
) {
Parents = ["System.ValueType", "object"]
};

public static TypeInfo ReturnTypeForEnum (string enumName, bool isSmartEnum = false)
public static TypeInfo ReturnTypeForEnum (string enumName, bool isSmartEnum = false, bool isNativeEnum = false)
=> new (
name: enumName,
isBlittable: true,
Expand All @@ -182,6 +192,7 @@ public static TypeInfo ReturnTypeForEnum (string enumName, bool isSmartEnum = fa
"System.IFormattable",
"System.ISpanFormattable"
],
IsNativeEnum = isNativeEnum,
EnumUnderlyingType = SpecialType.System_Int32,
};

Expand Down
Loading