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] Start implementing the missing parts of the data model for the transformer. #22091

Merged
merged 1 commit into from
Feb 1, 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
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ symbol is IArrayTypeSymbol arrayTypeSymbol
IsArray = true;
ArrayElementTypeIsWrapped = arraySymbol.ElementType.IsWrapped ();
}
IsArray = symbol is IArrayTypeSymbol;

// try to get the named type symbol to have more educated decisions
var namedTypeSymbol = symbol as INamedTypeSymbol;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// Licensed under the MIT License.

using Microsoft.Macios.Transformer.Attributes;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Macios.Generator.Extensions;

namespace Microsoft.Macios.Generator.DataModel;

Expand All @@ -11,4 +15,29 @@ readonly partial struct Parameter {
/// Returns the bind from data if present in the binding.
/// </summary>
public BindAsData? BindAs => BindAsAttribute;

public static bool TryCreate (IParameterSymbol symbol, ParameterSyntax declaration, SemanticModel semanticModel,
[NotNullWhen (true)] out Parameter? parameter)
{
DelegateInfo? delegateInfo = null;
if (symbol.Type is INamedTypeSymbol namedTypeSymbol
&& namedTypeSymbol.DelegateInvokeMethod is not null) {
DelegateInfo.TryCreate (namedTypeSymbol.DelegateInvokeMethod, out delegateInfo);
}

// retrieve the parameter attributes because those might affect the parameter type, for example, the
// NullAllowed attribute can change the parameter type to be nullable.
var parameterAttrs = symbol.GetAttributeData ();
parameter = new (symbol.Ordinal, new (symbol.Type, parameterAttrs), symbol.Name) {
IsOptional = symbol.IsOptional,
IsParams = symbol.IsParams,
IsThis = symbol.IsThis,
DefaultValue = (symbol.HasExplicitDefaultValue) ? symbol.ExplicitDefaultValue?.ToString () : null,
ReferenceKind = symbol.RefKind.ToReferenceKind (),
Delegate = delegateInfo,
Attributes = declaration.GetAttributeCodeChanges (semanticModel),
AttributesDictionary = parameterAttrs,
};
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,46 @@
// Licensed under the MIT License.

using Microsoft.CodeAnalysis;
using Microsoft.Macios.Generator.Extensions;
using Microsoft.Macios.Transformer;
using Microsoft.Macios.Transformer.Extensions;

namespace Microsoft.Macios.Generator.DataModel;

readonly partial struct TypeInfo {

internal TypeInfo (ITypeSymbol symbol) :
internal TypeInfo (ITypeSymbol symbol, Dictionary<string, List<AttributeData>> attributes) :
this (
symbol is IArrayTypeSymbol arrayTypeSymbol
? arrayTypeSymbol.ElementType.ToDisplayString ()
: symbol.ToDisplayString ().Trim ('?', '[', ']'),
symbol.SpecialType)
{
throw new NotImplementedException ();
IsNullable = attributes.HasNullAllowedFlag ();
// special case, the old bindings might not have the ? operator but will have the attr
IsBlittable = symbol.IsBlittable () && !IsNullable;
IsSmartEnum = symbol.IsSmartEnum ();
IsReferenceType = symbol.IsReferenceType;
IsStruct = symbol.TypeKind == TypeKind.Struct;
IsInterface = symbol.TypeKind == TypeKind.Interface;
IsNativeIntegerType = symbol.IsNativeIntegerType;
IsNativeEnum = symbol.HasAttribute (AttributesNames.NativeAttribute);

// data that we can get from the symbol without being INamedType
symbol.GetInheritance (
isNSObject: out isNSObject,
isNativeObject: out isINativeObject,
isDictionaryContainer: out isDictionaryContainer,
parents: out parents,
interfaces: out interfaces);
IsArray = symbol is IArrayTypeSymbol;

// try to get the named type symbol to have more educated decisions
var namedTypeSymbol = symbol as INamedTypeSymbol;

// store the enum special type, useful when generate code that needs to cast
EnumUnderlyingType = namedTypeSymbol?.EnumUnderlyingType?.SpecialType;
MetadataName = SpecialType is SpecialType.None or SpecialType.System_Void
? null : symbol.MetadataName;
}
}
33 changes: 20 additions & 13 deletions tests/rgen/Microsoft.Macios.Generator.Tests/TestDataFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static TypeInfo ReturnTypeForString (bool isNullable = false)
IsINativeObject = false,
};

public static TypeInfo ReturnTypeForInt (bool isNullable = false)
public static TypeInfo ReturnTypeForInt (bool isNullable = false, bool keepInterfaces = false)
=> new (
name: "int",
specialType: SpecialType.System_Int32,
Expand All @@ -44,7 +44,7 @@ public static TypeInfo ReturnTypeForInt (bool isNullable = false)
isStruct: true
) {
Parents = ["System.ValueType", "object"],
Interfaces = isNullable
Interfaces = isNullable && !keepInterfaces
? []
: [
"System.IComparable",
Expand Down Expand Up @@ -175,17 +175,19 @@ public static TypeInfo ReturnTypeForInterface (string interfaceName)
IsInterface = true,
};

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

public static TypeInfo ReturnTypeForEnum (string enumName, bool isSmartEnum = false, bool isNativeEnum = false,
SpecialType underlyingType = SpecialType.System_Int32)
bool isNullable = false, bool isBlittable = true, SpecialType underlyingType = SpecialType.System_Int32)
=> new (
name: enumName,
isBlittable: true,
isNullable: isNullable,
isBlittable: isBlittable,
isSmartEnum: isSmartEnum
) {
Parents = [
Expand Down Expand Up @@ -300,7 +302,7 @@ public static TypeInfo ReturnTypeForDelegate (string delegateName)
]
};

public static TypeInfo ReturnTypeForNSObject (string? nsObjectName = null, bool isNullable = false)
public static TypeInfo ReturnTypeForNSObject (string? nsObjectName = null, bool isNullable = false, bool isApiDefinition = false)
=> new (
name: nsObjectName ?? "Foundation.NSObject",
isNullable: isNullable,
Expand All @@ -310,13 +312,18 @@ public static TypeInfo ReturnTypeForNSObject (string? nsObjectName = null, bool
IsNSObject = true,
IsINativeObject = true,
Parents = nsObjectName is null ? ["object"] : ["Foundation.NSObject", "object"],
Interfaces = [
"ObjCRuntime.INativeObject",
$"System.IEquatable<{nsObjectName ?? "Foundation.NSObject"}>",
"System.IDisposable",
"Foundation.INSObjectFactory",
"Foundation.INSObjectProtocol"
]
Interfaces = isApiDefinition
? [
"ObjCRuntime.INativeObject",
"Foundation.INSObjectFactory",
]
: [
"ObjCRuntime.INativeObject",
$"System.IEquatable<{nsObjectName ?? "Foundation.NSObject"}>",
"System.IDisposable",
"Foundation.INSObjectFactory",
"Foundation.INSObjectProtocol"
]
};

public static TypeInfo ReturnTypeForINativeObject (string nativeObjectName, bool isNullable = false)
Expand Down
Loading
Loading