Skip to content

Commit

Permalink
[Rgen] Add more information about the type in the TypeInfo struct. (#…
Browse files Browse the repository at this point in the history
…22005)

We want to be able to know if a symbol is:

* An Interface
* An enumerator with a native attribute.
* If the interger used a C# keyword to be created.

---------

Co-authored-by: GitHub Actions Autoformatter <[email protected]>
Co-authored-by: Copilot <[email protected]>
  • Loading branch information
3 people authored Jan 18, 2025
1 parent 8ce51e8 commit d69bb0c
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 1 deletion.
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 @@ -162,12 +163,21 @@ public static TypeInfo ReturnTypeForGeneric (string genericName, bool isNullable
isNullable: isNullable
);

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 @@ -184,6 +194,7 @@ public static TypeInfo ReturnTypeForEnum (string enumName, bool isSmartEnum = fa
"System.IFormattable",
"System.ISpanFormattable"
],
IsNativeEnum = isNativeEnum,
EnumUnderlyingType = SpecialType.System_Int32,
};

Expand Down

0 comments on commit d69bb0c

Please sign in to comment.