Skip to content
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
8 changes: 7 additions & 1 deletion src/rgen/Microsoft.Macios.Generator/DataModel/TypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.Macios.Generator.Attributes;
using Microsoft.Macios.Generator.Extensions;

namespace Microsoft.Macios.Generator.DataModel;
Expand Down Expand Up @@ -73,9 +72,15 @@ public string FullyQualifiedName {
/// </summary>
public bool IsSmartEnum { get; }

/// <summary>
/// If the type is an array, it returns the special type of the underlying type.
/// </summary>
public SpecialType? ArrayElementType { get; init; }

/// <summary>
/// Returns if the return type is an array type.
/// </summary>
[MemberNotNullWhen (true, nameof (ArrayElementType))]
public bool IsArray { get; }

/// <summary>
Expand Down Expand Up @@ -209,6 +214,7 @@ symbol is IArrayTypeSymbol arrayTypeSymbol
IsWrapped = symbol.IsWrapped (isNSObject);
if (symbol is IArrayTypeSymbol arraySymbol) {
IsArray = true;
ArrayElementType = arraySymbol.ElementType.SpecialType;
ArrayElementTypeIsWrapped = arraySymbol.ElementType.IsWrapped ();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,14 @@ internal static InvocationExpressionSyntax StringFromHandle (ImmutableArray<Argu
/// <returns>The member access to the correct NSNumber method.</returns>
internal static MemberAccessExpressionSyntax? NSNumberFromHandle (TypeInfo returnType)
{
// create a tuple to store the name and special type depending if it is an array
// or a non array type
var info = returnType.IsArray
? (Name: returnType.Name, SpecialType: returnType.ArrayElementType)
: (Name: returnType.Name, SpecialType: returnType.SpecialType);

#pragma warning disable format
var memberName = returnType switch {
var memberName = info switch {
// name must be before SpecialType or you'll get them wrong values because
// the type we want by name also have a valid special type, the tests should catch
// mistakes here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Macios.Generator.DataModel;
using Xunit;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using static Microsoft.Macios.Generator.Emitters.BindingSyntaxFactory;
using static Microsoft.Macios.Generator.Tests.TestDataFactory;
using TypeInfo = Microsoft.Macios.Generator.DataModel.TypeInfo;

namespace Microsoft.Macios.Generator.Tests.Emitters;

Expand Down Expand Up @@ -316,6 +317,21 @@ public IEnumerator<object []> GetEnumerator ()
ReturnTypeForFloat (),
"NSNumber.ToFloat"
];

yield return [
ReturnTypeForArray ("int", underlyingType: SpecialType.System_Int32),
"NSNumber.ToInt32"
];

yield return [
ReturnTypeForArray ("uint", underlyingType: SpecialType.System_UInt32),
"NSNumber.ToUInt32"
];

yield return [
ReturnTypeForArray ("nint", underlyingType: SpecialType.System_IntPtr),
"NSNumber.ToNInt"
];
}

IEnumerator IEnumerable.GetEnumerator () => GetEnumerator ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,8 @@ public static TypeInfo ReturnTypeForArray (string type,
bool isEnum = false,
bool isSmartEnum = false,
bool isStruct = false,
bool isNSObject = false)
bool isNSObject = false,
SpecialType? underlyingType = null)
=> new (
name: type,
isNullable: isNullable,
Expand All @@ -489,6 +490,7 @@ public static TypeInfo ReturnTypeForArray (string type,
isStruct: isStruct
) {
EnumUnderlyingType = isEnum ? SpecialType.System_Int32 : null,
ArrayElementType = underlyingType,
ArrayElementTypeIsWrapped = isNSObject,
Parents = ["System.Array", "object"],
Interfaces = [
Expand Down