C#: Cleanup Variant marshaling code in source/bindings generators

This change aims to reduce the number of places that need to be changed
when adding or editing a Godot type to the bindings.

Since the addition of `Variant.From<T>/As<T>` and
`VariantUtils.CreateFrom<T>/ConvertTo<T>`, we can now replace a lot of
the previous code in the bindings generator and the source generators
that specify these conversions for each type manually.

The only exceptions are the generic Godot collections (`Array<T>` and
`Dictionary<TKey, TValue>`) which still use the old version, as that
one cannot be matched by our new conversion methods (limitation in the
language with generics, forcing us to use delegate pointers).

The cleanup applies to:

- Bindings generator:
  - `TypeInterface.cs_variant_to_managed`
  - `TypeInterface.cs_managed_to_variant`
- Source generators:
  - `MarshalUtils.AppendNativeVariantToManagedExpr`
  - `MarshalUtils.AppendManagedToNativeVariantExpr`
  - `MarshalUtils.AppendVariantToManagedExpr`
  - `MarshalUtils.AppendManagedToVariantExpr`
This commit is contained in:
Ignacio Roldán Etcheverry 2022-12-01 01:45:11 +01:00
parent f86c6b6ac4
commit 17b2838f39
10 changed files with 83 additions and 415 deletions

View file

@ -268,8 +268,9 @@ namespace Godot.SourceGenerators
if (parameters.Length > paramTypes.Length)
return null; // Ignore incompatible method
return new GodotMethodData(method, paramTypes, parameters
.Select(p => p.Type).ToImmutableArray(), retType, retSymbol);
return new GodotMethodData(method, paramTypes,
parameters.Select(p => p.Type).ToImmutableArray(),
retType != null ? (retType.Value, retSymbol) : null);
}
public static IEnumerable<GodotMethodData> WhereHasGodotCompatibleSignature(
@ -330,10 +331,10 @@ namespace Godot.SourceGenerators
public static string Path(this Location location)
=> location.SourceTree?.GetLineSpan(location.SourceSpan).Path
?? location.GetLineSpan().Path;
?? location.GetLineSpan().Path;
public static int StartLine(this Location location)
=> location.SourceTree?.GetLineSpan(location.SourceSpan).StartLinePosition.Line
?? location.GetLineSpan().StartLinePosition.Line;
?? location.GetLineSpan().StartLinePosition.Line;
}
}

View file

@ -6,20 +6,18 @@ namespace Godot.SourceGenerators
public readonly struct GodotMethodData
{
public GodotMethodData(IMethodSymbol method, ImmutableArray<MarshalType> paramTypes,
ImmutableArray<ITypeSymbol> paramTypeSymbols, MarshalType? retType, ITypeSymbol? retSymbol)
ImmutableArray<ITypeSymbol> paramTypeSymbols, (MarshalType MarshalType, ITypeSymbol TypeSymbol)? retType)
{
Method = method;
ParamTypes = paramTypes;
ParamTypeSymbols = paramTypeSymbols;
RetType = retType;
RetSymbol = retSymbol;
}
public IMethodSymbol Method { get; }
public ImmutableArray<MarshalType> ParamTypes { get; }
public ImmutableArray<ITypeSymbol> ParamTypeSymbols { get; }
public MarshalType? RetType { get; }
public ITypeSymbol? RetSymbol { get; }
public (MarshalType MarshalType, ITypeSymbol TypeSymbol)? RetType { get; }
}
public readonly struct GodotSignalDelegateData

View file

@ -304,240 +304,33 @@ namespace Godot.SourceGenerators
{
return marshalType switch
{
MarshalType.Boolean =>
source.Append(VariantUtils, ".ConvertToBool(", inputExpr, ")"),
MarshalType.Char =>
source.Append("(char)", VariantUtils, ".ConvertToUInt16(", inputExpr, ")"),
MarshalType.SByte =>
source.Append(VariantUtils, ".ConvertToInt8(", inputExpr, ")"),
MarshalType.Int16 =>
source.Append(VariantUtils, ".ConvertToInt16(", inputExpr, ")"),
MarshalType.Int32 =>
source.Append(VariantUtils, ".ConvertToInt32(", inputExpr, ")"),
MarshalType.Int64 =>
source.Append(VariantUtils, ".ConvertToInt64(", inputExpr, ")"),
MarshalType.Byte =>
source.Append(VariantUtils, ".ConvertToUInt8(", inputExpr, ")"),
MarshalType.UInt16 =>
source.Append(VariantUtils, ".ConvertToUInt16(", inputExpr, ")"),
MarshalType.UInt32 =>
source.Append(VariantUtils, ".ConvertToUInt32(", inputExpr, ")"),
MarshalType.UInt64 =>
source.Append(VariantUtils, ".ConvertToUInt64(", inputExpr, ")"),
MarshalType.Single =>
source.Append(VariantUtils, ".ConvertToFloat32(", inputExpr, ")"),
MarshalType.Double =>
source.Append(VariantUtils, ".ConvertToFloat64(", inputExpr, ")"),
MarshalType.String =>
source.Append(VariantUtils, ".ConvertToStringObject(", inputExpr, ")"),
MarshalType.Vector2 =>
source.Append(VariantUtils, ".ConvertToVector2(", inputExpr, ")"),
MarshalType.Vector2i =>
source.Append(VariantUtils, ".ConvertToVector2i(", inputExpr, ")"),
MarshalType.Rect2 =>
source.Append(VariantUtils, ".ConvertToRect2(", inputExpr, ")"),
MarshalType.Rect2i =>
source.Append(VariantUtils, ".ConvertToRect2i(", inputExpr, ")"),
MarshalType.Transform2D =>
source.Append(VariantUtils, ".ConvertToTransform2D(", inputExpr, ")"),
MarshalType.Vector3 =>
source.Append(VariantUtils, ".ConvertToVector3(", inputExpr, ")"),
MarshalType.Vector3i =>
source.Append(VariantUtils, ".ConvertToVector3i(", inputExpr, ")"),
MarshalType.Basis =>
source.Append(VariantUtils, ".ConvertToBasis(", inputExpr, ")"),
MarshalType.Quaternion =>
source.Append(VariantUtils, ".ConvertToQuaternion(", inputExpr, ")"),
MarshalType.Transform3D =>
source.Append(VariantUtils, ".ConvertToTransform3D(", inputExpr, ")"),
MarshalType.Vector4 =>
source.Append(VariantUtils, ".ConvertToVector4(", inputExpr, ")"),
MarshalType.Vector4i =>
source.Append(VariantUtils, ".ConvertToVector4i(", inputExpr, ")"),
MarshalType.Projection =>
source.Append(VariantUtils, ".ConvertToProjection(", inputExpr, ")"),
MarshalType.AABB =>
source.Append(VariantUtils, ".ConvertToAABB(", inputExpr, ")"),
MarshalType.Color =>
source.Append(VariantUtils, ".ConvertToColor(", inputExpr, ")"),
MarshalType.Plane =>
source.Append(VariantUtils, ".ConvertToPlane(", inputExpr, ")"),
MarshalType.Callable =>
source.Append(VariantUtils, ".ConvertToCallableManaged(", inputExpr, ")"),
MarshalType.SignalInfo =>
source.Append(VariantUtils, ".ConvertToSignalInfo(", inputExpr, ")"),
MarshalType.Enum =>
source.Append("(", typeSymbol.FullQualifiedNameIncludeGlobal(),
")", VariantUtils, ".ConvertToInt64(", inputExpr, ")"),
MarshalType.ByteArray =>
source.Append(VariantUtils, ".ConvertAsPackedByteArrayToSystemArray(", inputExpr, ")"),
MarshalType.Int32Array =>
source.Append(VariantUtils, ".ConvertAsPackedInt32ArrayToSystemArray(", inputExpr, ")"),
MarshalType.Int64Array =>
source.Append(VariantUtils, ".ConvertAsPackedInt64ArrayToSystemArray(", inputExpr, ")"),
MarshalType.Float32Array =>
source.Append(VariantUtils, ".ConvertAsPackedFloat32ArrayToSystemArray(", inputExpr, ")"),
MarshalType.Float64Array =>
source.Append(VariantUtils, ".ConvertAsPackedFloat64ArrayToSystemArray(", inputExpr, ")"),
MarshalType.StringArray =>
source.Append(VariantUtils, ".ConvertAsPackedStringArrayToSystemArray(", inputExpr, ")"),
MarshalType.Vector2Array =>
source.Append(VariantUtils, ".ConvertAsPackedVector2ArrayToSystemArray(", inputExpr, ")"),
MarshalType.Vector3Array =>
source.Append(VariantUtils, ".ConvertAsPackedVector3ArrayToSystemArray(", inputExpr, ")"),
MarshalType.ColorArray =>
source.Append(VariantUtils, ".ConvertAsPackedColorArrayToSystemArray(", inputExpr, ")"),
MarshalType.GodotObjectOrDerivedArray =>
source.Append(VariantUtils, ".ConvertToSystemArrayOfGodotObject<",
((IArrayTypeSymbol)typeSymbol).ElementType.FullQualifiedNameIncludeGlobal(), ">(", inputExpr, ")"),
MarshalType.SystemArrayOfStringName =>
source.Append(VariantUtils, ".ConvertToSystemArrayOfStringName(", inputExpr, ")"),
MarshalType.SystemArrayOfNodePath =>
source.Append(VariantUtils, ".ConvertToSystemArrayOfNodePath(", inputExpr, ")"),
MarshalType.SystemArrayOfRID =>
source.Append(VariantUtils, ".ConvertToSystemArrayOfRID(", inputExpr, ")"),
MarshalType.Variant =>
source.Append("global::Godot.Variant.CreateCopyingBorrowed(", inputExpr, ")"),
MarshalType.GodotObjectOrDerived =>
source.Append("(", typeSymbol.FullQualifiedNameIncludeGlobal(),
")", VariantUtils, ".ConvertToGodotObject(", inputExpr, ")"),
MarshalType.StringName =>
source.Append(VariantUtils, ".ConvertToStringNameObject(", inputExpr, ")"),
MarshalType.NodePath =>
source.Append(VariantUtils, ".ConvertToNodePathObject(", inputExpr, ")"),
MarshalType.RID =>
source.Append(VariantUtils, ".ConvertToRID(", inputExpr, ")"),
MarshalType.GodotDictionary =>
source.Append(VariantUtils, ".ConvertToDictionaryObject(", inputExpr, ")"),
MarshalType.GodotArray =>
source.Append(VariantUtils, ".ConvertToArrayObject(", inputExpr, ")"),
// For generic Godot collections, VariantUtils.ConvertTo<T> is slower, so we need this special case
MarshalType.GodotGenericDictionary =>
source.Append(VariantUtils, ".ConvertToDictionaryObject<",
((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ", ",
((INamedTypeSymbol)typeSymbol).TypeArguments[1].FullQualifiedNameIncludeGlobal(), ">(", inputExpr, ")"),
((INamedTypeSymbol)typeSymbol).TypeArguments[1].FullQualifiedNameIncludeGlobal(), ">(",
inputExpr, ")"),
MarshalType.GodotGenericArray =>
source.Append(VariantUtils, ".ConvertToArrayObject<",
((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ">(", inputExpr, ")"),
_ => throw new ArgumentOutOfRangeException(nameof(marshalType), marshalType,
"Received unexpected marshal type")
((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ">(",
inputExpr, ")"),
_ => source.Append(VariantUtils, ".ConvertTo<",
typeSymbol.FullQualifiedNameIncludeGlobal(), ">(", inputExpr, ")"),
};
}
public static StringBuilder AppendManagedToNativeVariantExpr(
this StringBuilder source, string inputExpr, MarshalType marshalType)
public static StringBuilder AppendManagedToNativeVariantExpr(this StringBuilder source,
string inputExpr, ITypeSymbol typeSymbol, MarshalType marshalType)
{
return marshalType switch
{
MarshalType.Boolean =>
source.Append(VariantUtils, ".CreateFromBool(", inputExpr, ")"),
MarshalType.Char =>
source.Append(VariantUtils, ".CreateFromInt((ushort)", inputExpr, ")"),
MarshalType.SByte =>
source.Append(VariantUtils, ".CreateFromInt(", inputExpr, ")"),
MarshalType.Int16 =>
source.Append(VariantUtils, ".CreateFromInt(", inputExpr, ")"),
MarshalType.Int32 =>
source.Append(VariantUtils, ".CreateFromInt(", inputExpr, ")"),
MarshalType.Int64 =>
source.Append(VariantUtils, ".CreateFromInt(", inputExpr, ")"),
MarshalType.Byte =>
source.Append(VariantUtils, ".CreateFromInt(", inputExpr, ")"),
MarshalType.UInt16 =>
source.Append(VariantUtils, ".CreateFromInt(", inputExpr, ")"),
MarshalType.UInt32 =>
source.Append(VariantUtils, ".CreateFromInt(", inputExpr, ")"),
MarshalType.UInt64 =>
source.Append(VariantUtils, ".CreateFromInt(", inputExpr, ")"),
MarshalType.Single =>
source.Append(VariantUtils, ".CreateFromFloat(", inputExpr, ")"),
MarshalType.Double =>
source.Append(VariantUtils, ".CreateFromFloat(", inputExpr, ")"),
MarshalType.String =>
source.Append(VariantUtils, ".CreateFromString(", inputExpr, ")"),
MarshalType.Vector2 =>
source.Append(VariantUtils, ".CreateFromVector2(", inputExpr, ")"),
MarshalType.Vector2i =>
source.Append(VariantUtils, ".CreateFromVector2i(", inputExpr, ")"),
MarshalType.Rect2 =>
source.Append(VariantUtils, ".CreateFromRect2(", inputExpr, ")"),
MarshalType.Rect2i =>
source.Append(VariantUtils, ".CreateFromRect2i(", inputExpr, ")"),
MarshalType.Transform2D =>
source.Append(VariantUtils, ".CreateFromTransform2D(", inputExpr, ")"),
MarshalType.Vector3 =>
source.Append(VariantUtils, ".CreateFromVector3(", inputExpr, ")"),
MarshalType.Vector3i =>
source.Append(VariantUtils, ".CreateFromVector3i(", inputExpr, ")"),
MarshalType.Basis =>
source.Append(VariantUtils, ".CreateFromBasis(", inputExpr, ")"),
MarshalType.Quaternion =>
source.Append(VariantUtils, ".CreateFromQuaternion(", inputExpr, ")"),
MarshalType.Transform3D =>
source.Append(VariantUtils, ".CreateFromTransform3D(", inputExpr, ")"),
MarshalType.Vector4 =>
source.Append(VariantUtils, ".CreateFromVector4(", inputExpr, ")"),
MarshalType.Vector4i =>
source.Append(VariantUtils, ".CreateFromVector4i(", inputExpr, ")"),
MarshalType.Projection =>
source.Append(VariantUtils, ".CreateFromProjection(", inputExpr, ")"),
MarshalType.AABB =>
source.Append(VariantUtils, ".CreateFromAABB(", inputExpr, ")"),
MarshalType.Color =>
source.Append(VariantUtils, ".CreateFromColor(", inputExpr, ")"),
MarshalType.Plane =>
source.Append(VariantUtils, ".CreateFromPlane(", inputExpr, ")"),
MarshalType.Callable =>
source.Append(VariantUtils, ".CreateFromCallable(", inputExpr, ")"),
MarshalType.SignalInfo =>
source.Append(VariantUtils, ".CreateFromSignalInfo(", inputExpr, ")"),
MarshalType.Enum =>
source.Append(VariantUtils, ".CreateFromInt((long)", inputExpr, ")"),
MarshalType.ByteArray =>
source.Append(VariantUtils, ".CreateFromPackedByteArray(", inputExpr, ")"),
MarshalType.Int32Array =>
source.Append(VariantUtils, ".CreateFromPackedInt32Array(", inputExpr, ")"),
MarshalType.Int64Array =>
source.Append(VariantUtils, ".CreateFromPackedInt64Array(", inputExpr, ")"),
MarshalType.Float32Array =>
source.Append(VariantUtils, ".CreateFromPackedFloat32Array(", inputExpr, ")"),
MarshalType.Float64Array =>
source.Append(VariantUtils, ".CreateFromPackedFloat64Array(", inputExpr, ")"),
MarshalType.StringArray =>
source.Append(VariantUtils, ".CreateFromPackedStringArray(", inputExpr, ")"),
MarshalType.Vector2Array =>
source.Append(VariantUtils, ".CreateFromPackedVector2Array(", inputExpr, ")"),
MarshalType.Vector3Array =>
source.Append(VariantUtils, ".CreateFromPackedVector3Array(", inputExpr, ")"),
MarshalType.ColorArray =>
source.Append(VariantUtils, ".CreateFromPackedColorArray(", inputExpr, ")"),
MarshalType.GodotObjectOrDerivedArray =>
source.Append(VariantUtils, ".CreateFromSystemArrayOfGodotObject(", inputExpr, ")"),
MarshalType.SystemArrayOfStringName =>
source.Append(VariantUtils, ".CreateFromSystemArrayOfStringName(", inputExpr, ")"),
MarshalType.SystemArrayOfNodePath =>
source.Append(VariantUtils, ".CreateFromSystemArrayOfNodePath(", inputExpr, ")"),
MarshalType.SystemArrayOfRID =>
source.Append(VariantUtils, ".CreateFromSystemArrayOfRID(", inputExpr, ")"),
MarshalType.Variant =>
source.Append(inputExpr, ".CopyNativeVariant()"),
MarshalType.GodotObjectOrDerived =>
source.Append(VariantUtils, ".CreateFromGodotObject(", inputExpr, ")"),
MarshalType.StringName =>
source.Append(VariantUtils, ".CreateFromStringName(", inputExpr, ")"),
MarshalType.NodePath =>
source.Append(VariantUtils, ".CreateFromNodePath(", inputExpr, ")"),
MarshalType.RID =>
source.Append(VariantUtils, ".CreateFromRID(", inputExpr, ")"),
MarshalType.GodotDictionary =>
source.Append(VariantUtils, ".CreateFromDictionary(", inputExpr, ")"),
MarshalType.GodotArray =>
source.Append(VariantUtils, ".CreateFromArray(", inputExpr, ")"),
// For generic Godot collections, VariantUtils.CreateFrom<T> is slower, so we need this special case
MarshalType.GodotGenericDictionary =>
source.Append(VariantUtils, ".CreateFromDictionary(", inputExpr, ")"),
MarshalType.GodotGenericArray =>
source.Append(VariantUtils, ".CreateFromArray(", inputExpr, ")"),
_ => throw new ArgumentOutOfRangeException(nameof(marshalType), marshalType,
"Received unexpected marshal type")
_ => source.Append(VariantUtils, ".CreateFrom<",
typeSymbol.FullQualifiedNameIncludeGlobal(), ">(", inputExpr, ")"),
};
}
@ -546,137 +339,30 @@ namespace Godot.SourceGenerators
{
return marshalType switch
{
MarshalType.Boolean => source.Append(inputExpr, ".AsBool()"),
MarshalType.Char => source.Append(inputExpr, ".AsChar()"),
MarshalType.SByte => source.Append(inputExpr, ".AsSByte()"),
MarshalType.Int16 => source.Append(inputExpr, ".AsInt16()"),
MarshalType.Int32 => source.Append(inputExpr, ".AsInt32()"),
MarshalType.Int64 => source.Append(inputExpr, ".AsInt64()"),
MarshalType.Byte => source.Append(inputExpr, ".AsByte()"),
MarshalType.UInt16 => source.Append(inputExpr, ".AsUInt16()"),
MarshalType.UInt32 => source.Append(inputExpr, ".AsUInt32()"),
MarshalType.UInt64 => source.Append(inputExpr, ".AsUInt64()"),
MarshalType.Single => source.Append(inputExpr, ".AsSingle()"),
MarshalType.Double => source.Append(inputExpr, ".AsDouble()"),
MarshalType.String => source.Append(inputExpr, ".AsString()"),
MarshalType.Vector2 => source.Append(inputExpr, ".AsVector2()"),
MarshalType.Vector2i => source.Append(inputExpr, ".AsVector2i()"),
MarshalType.Rect2 => source.Append(inputExpr, ".AsRect2()"),
MarshalType.Rect2i => source.Append(inputExpr, ".AsRect2i()"),
MarshalType.Transform2D => source.Append(inputExpr, ".AsTransform2D()"),
MarshalType.Vector3 => source.Append(inputExpr, ".AsVector3()"),
MarshalType.Vector3i => source.Append(inputExpr, ".AsVector3i()"),
MarshalType.Basis => source.Append(inputExpr, ".AsBasis()"),
MarshalType.Quaternion => source.Append(inputExpr, ".AsQuaternion()"),
MarshalType.Transform3D => source.Append(inputExpr, ".AsTransform3D()"),
MarshalType.Vector4 => source.Append(inputExpr, ".AsVector4()"),
MarshalType.Vector4i => source.Append(inputExpr, ".AsVector4i()"),
MarshalType.Projection => source.Append(inputExpr, ".AsProjection()"),
MarshalType.AABB => source.Append(inputExpr, ".AsAABB()"),
MarshalType.Color => source.Append(inputExpr, ".AsColor()"),
MarshalType.Plane => source.Append(inputExpr, ".AsPlane()"),
MarshalType.Callable => source.Append(inputExpr, ".AsCallable()"),
MarshalType.SignalInfo => source.Append(inputExpr, ".AsSignalInfo()"),
MarshalType.Enum =>
source.Append("(", typeSymbol.FullQualifiedNameIncludeGlobal(), ")", inputExpr, ".AsInt64()"),
MarshalType.ByteArray => source.Append(inputExpr, ".AsByteArray()"),
MarshalType.Int32Array => source.Append(inputExpr, ".AsInt32Array()"),
MarshalType.Int64Array => source.Append(inputExpr, ".AsInt64Array()"),
MarshalType.Float32Array => source.Append(inputExpr, ".AsFloat32Array()"),
MarshalType.Float64Array => source.Append(inputExpr, ".AsFloat64Array()"),
MarshalType.StringArray => source.Append(inputExpr, ".AsStringArray()"),
MarshalType.Vector2Array => source.Append(inputExpr, ".AsVector2Array()"),
MarshalType.Vector3Array => source.Append(inputExpr, ".AsVector3Array()"),
MarshalType.ColorArray => source.Append(inputExpr, ".AsColorArray()"),
MarshalType.GodotObjectOrDerivedArray => source.Append(inputExpr, ".AsGodotObjectArray<",
((IArrayTypeSymbol)typeSymbol).ElementType.FullQualifiedNameIncludeGlobal(), ">()"),
MarshalType.SystemArrayOfStringName => source.Append(inputExpr, ".AsSystemArrayOfStringName()"),
MarshalType.SystemArrayOfNodePath => source.Append(inputExpr, ".AsSystemArrayOfNodePath()"),
MarshalType.SystemArrayOfRID => source.Append(inputExpr, ".AsSystemArrayOfRID()"),
MarshalType.Variant => source.Append(inputExpr),
MarshalType.GodotObjectOrDerived => source.Append("(",
typeSymbol.FullQualifiedNameIncludeGlobal(), ")", inputExpr, ".AsGodotObject()"),
MarshalType.StringName => source.Append(inputExpr, ".AsStringName()"),
MarshalType.NodePath => source.Append(inputExpr, ".AsNodePath()"),
MarshalType.RID => source.Append(inputExpr, ".AsRID()"),
MarshalType.GodotDictionary => source.Append(inputExpr, ".AsGodotDictionary()"),
MarshalType.GodotArray => source.Append(inputExpr, ".AsGodotArray()"),
MarshalType.GodotGenericDictionary => source.Append(inputExpr, ".AsGodotDictionary<",
((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ", ",
((INamedTypeSymbol)typeSymbol).TypeArguments[1].FullQualifiedNameIncludeGlobal(), ">()"),
MarshalType.GodotGenericArray => source.Append(inputExpr, ".AsGodotArray<",
((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ">()"),
_ => throw new ArgumentOutOfRangeException(nameof(marshalType), marshalType,
"Received unexpected marshal type")
// For generic Godot collections, Variant.As<T> is slower, so we need this special case
MarshalType.GodotGenericDictionary =>
source.Append(inputExpr, ".AsGodotDictionary<",
((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ", ",
((INamedTypeSymbol)typeSymbol).TypeArguments[1].FullQualifiedNameIncludeGlobal(), ">()"),
MarshalType.GodotGenericArray =>
source.Append(inputExpr, ".AsGodotArray<",
((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ">()"),
_ => source.Append(inputExpr, ".As<",
typeSymbol.FullQualifiedNameIncludeGlobal(), ">()")
};
}
public static StringBuilder AppendManagedToVariantExpr(this StringBuilder source,
string inputExpr, MarshalType marshalType)
string inputExpr, ITypeSymbol typeSymbol, MarshalType marshalType)
{
switch (marshalType)
return marshalType switch
{
case MarshalType.Boolean:
case MarshalType.Char:
case MarshalType.SByte:
case MarshalType.Int16:
case MarshalType.Int32:
case MarshalType.Int64:
case MarshalType.Byte:
case MarshalType.UInt16:
case MarshalType.UInt32:
case MarshalType.UInt64:
case MarshalType.Single:
case MarshalType.Double:
case MarshalType.String:
case MarshalType.Vector2:
case MarshalType.Vector2i:
case MarshalType.Rect2:
case MarshalType.Rect2i:
case MarshalType.Transform2D:
case MarshalType.Vector3:
case MarshalType.Vector3i:
case MarshalType.Basis:
case MarshalType.Quaternion:
case MarshalType.Transform3D:
case MarshalType.Vector4:
case MarshalType.Vector4i:
case MarshalType.Projection:
case MarshalType.AABB:
case MarshalType.Color:
case MarshalType.Plane:
case MarshalType.Callable:
case MarshalType.SignalInfo:
case MarshalType.ByteArray:
case MarshalType.Int32Array:
case MarshalType.Int64Array:
case MarshalType.Float32Array:
case MarshalType.Float64Array:
case MarshalType.StringArray:
case MarshalType.Vector2Array:
case MarshalType.Vector3Array:
case MarshalType.ColorArray:
case MarshalType.GodotObjectOrDerivedArray:
case MarshalType.SystemArrayOfStringName:
case MarshalType.SystemArrayOfNodePath:
case MarshalType.SystemArrayOfRID:
case MarshalType.GodotObjectOrDerived:
case MarshalType.StringName:
case MarshalType.NodePath:
case MarshalType.RID:
case MarshalType.GodotDictionary:
case MarshalType.GodotArray:
case MarshalType.GodotGenericDictionary:
case MarshalType.GodotGenericArray:
return source.Append("Variant.CreateFrom(", inputExpr, ")");
case MarshalType.Enum:
return source.Append("Variant.CreateFrom((long)", inputExpr, ")");
case MarshalType.Variant:
return source.Append(inputExpr);
default:
throw new ArgumentOutOfRangeException(nameof(marshalType), marshalType,
"Received unexpected marshal type");
}
// For generic Godot collections, Variant.From<T> is slower, so we need this special case
MarshalType.GodotGenericDictionary or MarshalType.GodotGenericArray =>
source.Append("global::Godot.Variant.CreateFrom(", inputExpr, ")"),
_ => source.Append("global::Godot.Variant.From<",
typeSymbol.FullQualifiedNameIncludeGlobal(), ">(", inputExpr, ")")
};
}
}
}

View file

@ -135,7 +135,8 @@ namespace Godot.SourceGenerators
source.Append("#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword\n");
source.Append($" public new class MethodName : {symbol.BaseType.FullQualifiedNameIncludeGlobal()}.MethodName {{\n");
source.Append(
$" public new class MethodName : {symbol.BaseType.FullQualifiedNameIncludeGlobal()}.MethodName {{\n");
// Generate cached StringNames for methods and properties, for fast lookup
@ -297,7 +298,7 @@ namespace Godot.SourceGenerators
if (method.RetType != null)
{
returnVal = DeterminePropertyInfo(method.RetType.Value, name: string.Empty);
returnVal = DeterminePropertyInfo(method.RetType.Value.MarshalType, name: string.Empty);
}
else
{
@ -391,7 +392,8 @@ namespace Godot.SourceGenerators
{
source.Append(" ret = ");
source.AppendManagedToNativeVariantExpr("callRet", method.RetType.Value);
source.AppendManagedToNativeVariantExpr("callRet",
method.RetType.Value.TypeSymbol, method.RetType.Value.MarshalType);
source.Append(";\n");
source.Append(" return true;\n");

View file

@ -124,7 +124,8 @@ namespace Godot.SourceGenerators
source.Append("#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword\n");
source.Append($" public new class PropertyName : {symbol.BaseType.FullQualifiedNameIncludeGlobal()}.PropertyName {{\n");
source.Append(
$" public new class PropertyName : {symbol.BaseType.FullQualifiedNameIncludeGlobal()}.PropertyName {{\n");
// Generate cached StringNames for methods and properties, for fast lookup
@ -199,14 +200,14 @@ namespace Godot.SourceGenerators
foreach (var property in godotClassProperties)
{
GeneratePropertyGetter(property.PropertySymbol.Name,
property.Type, source, isFirstEntry);
property.PropertySymbol.Type, property.Type, source, isFirstEntry);
isFirstEntry = false;
}
foreach (var field in godotClassFields)
{
GeneratePropertyGetter(field.FieldSymbol.Name,
field.Type, source, isFirstEntry);
field.FieldSymbol.Type, field.Type, source, isFirstEntry);
isFirstEntry = false;
}
@ -303,6 +304,7 @@ namespace Godot.SourceGenerators
private static void GeneratePropertyGetter(
string propertyMemberName,
ITypeSymbol propertyTypeSymbol,
MarshalType propertyMarshalType,
StringBuilder source,
bool isFirstEntry
@ -317,7 +319,8 @@ namespace Godot.SourceGenerators
.Append(propertyMemberName)
.Append(") {\n")
.Append(" value = ")
.AppendManagedToNativeVariantExpr("this." + propertyMemberName, propertyMarshalType)
.AppendManagedToNativeVariantExpr("this." + propertyMemberName,
propertyTypeSymbol, propertyMarshalType)
.Append(";\n")
.Append(" return true;\n")
.Append(" }\n");
@ -376,7 +379,8 @@ namespace Godot.SourceGenerators
if (propertyUsage != PropertyUsageFlags.Category && attr.ConstructorArguments.Length > 1)
hintString = attr.ConstructorArguments[1].Value?.ToString();
yield return new PropertyInfo(VariantType.Nil, name, PropertyHint.None, hintString, propertyUsage.Value, true);
yield return new PropertyInfo(VariantType.Nil, name, PropertyHint.None, hintString,
propertyUsage.Value, true);
}
}
}

View file

@ -307,7 +307,8 @@ namespace Godot.SourceGenerators
source.Append(" values.Add(PropertyName.");
source.Append(exportedMember.Name);
source.Append(", ");
source.AppendManagedToVariantExpr(defaultValueLocalName, exportedMember.Type);
source.AppendManagedToVariantExpr(defaultValueLocalName,
exportedMember.TypeSymbol, exportedMember.Type);
source.Append(");\n");
}

View file

@ -162,7 +162,8 @@ namespace Godot.SourceGenerators
source.Append(" info.AddProperty(PropertyName.")
.Append(propertyName)
.Append(", ")
.AppendManagedToVariantExpr(string.Concat("this.", propertyName), property.Type)
.AppendManagedToVariantExpr(string.Concat("this.", propertyName),
property.PropertySymbol.Type, property.Type)
.Append(");\n");
}
@ -175,7 +176,8 @@ namespace Godot.SourceGenerators
source.Append(" info.AddProperty(PropertyName.")
.Append(fieldName)
.Append(", ")
.AppendManagedToVariantExpr(string.Concat("this.", fieldName), field.Type)
.AppendManagedToVariantExpr(string.Concat("this.", fieldName),
field.FieldSymbol.Type, field.Type)
.Append(");\n");
}

View file

@ -176,7 +176,8 @@ namespace Godot.SourceGenerators
source.Append("#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword\n");
source.Append($" public new class SignalName : {symbol.BaseType.FullQualifiedNameIncludeGlobal()}.SignalName {{\n");
source.Append(
$" public new class SignalName : {symbol.BaseType.FullQualifiedNameIncludeGlobal()}.SignalName {{\n");
// Generate cached StringNames for methods and properties, for fast lookup
@ -236,7 +237,8 @@ namespace Godot.SourceGenerators
.Append(signalName)
.Append(";\n");
source.Append($" /// <inheritdoc cref=\"{signalDelegate.DelegateSymbol.FullQualifiedNameIncludeGlobal()}\"/>\n");
source.Append(
$" /// <inheritdoc cref=\"{signalDelegate.DelegateSymbol.FullQualifiedNameIncludeGlobal()}\"/>\n");
source.Append(" public event ")
.Append(signalDelegate.DelegateSymbol.FullQualifiedNameIncludeGlobal())
@ -351,7 +353,7 @@ namespace Godot.SourceGenerators
if (invokeMethodData.RetType != null)
{
returnVal = DeterminePropertyInfo(invokeMethodData.RetType.Value, name: string.Empty);
returnVal = DeterminePropertyInfo(invokeMethodData.RetType.Value.MarshalType, name: string.Empty);
}
else
{

View file

@ -2837,9 +2837,6 @@ bool BindingsGenerator::_populate_object_type_interfaces() {
itype.is_ref_counted = ClassDB::is_parent_class(type_cname, name_cache.type_RefCounted);
itype.memory_own = itype.is_ref_counted;
itype.cs_variant_to_managed = "(%1)VariantUtils.ConvertToGodotObject(%0)";
itype.cs_managed_to_variant = "VariantUtils.CreateFromGodotObject(%0)";
itype.c_out = "%5return ";
itype.c_out += C_METHOD_UNMANAGED_GET_MANAGED;
itype.c_out += itype.is_ref_counted ? "(%1.Reference);\n" : "(%1);\n";
@ -3218,8 +3215,6 @@ bool BindingsGenerator::_populate_object_type_interfaces() {
enum_itype.cname = StringName(enum_itype.name);
enum_itype.proxy_name = itype.proxy_name + "." + enum_proxy_name;
TypeInterface::postsetup_enum_type(enum_itype);
enum_itype.cs_variant_to_managed = "(%1)VariantUtils.ConvertToInt32(%0)";
enum_itype.cs_managed_to_variant = "VariantUtils.CreateFromInt((int)%0)";
enum_types.insert(enum_itype.cname, enum_itype);
}
@ -3448,16 +3443,14 @@ void BindingsGenerator::_populate_builtin_type_interfaces() {
TypeInterface itype;
#define INSERT_STRUCT_TYPE(m_type) \
{ \
itype = TypeInterface::create_value_type(String(#m_type)); \
itype.c_type_in = #m_type "*"; \
itype.c_type_out = itype.cs_type; \
itype.cs_in_expr = "&%0"; \
itype.cs_in_expr_is_unsafe = true; \
itype.cs_variant_to_managed = "VariantUtils.ConvertTo%2(%0)"; \
itype.cs_managed_to_variant = "VariantUtils.CreateFrom%2(%0)"; \
builtin_types.insert(itype.cname, itype); \
#define INSERT_STRUCT_TYPE(m_type) \
{ \
itype = TypeInterface::create_value_type(String(#m_type)); \
itype.c_type_in = #m_type "*"; \
itype.c_type_out = itype.cs_type; \
itype.cs_in_expr = "&%0"; \
itype.cs_in_expr_is_unsafe = true; \
builtin_types.insert(itype.cname, itype); \
}
INSERT_STRUCT_TYPE(Vector2)
@ -3488,8 +3481,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() {
itype.c_type_out = itype.c_type;
itype.c_arg_in = "&%s";
itype.c_in_vararg = "%5using godot_variant %1_in = VariantUtils.CreateFromBool(%1);\n";
itype.cs_variant_to_managed = "VariantUtils.ConvertToBool(%0)";
itype.cs_managed_to_variant = "VariantUtils.CreateFromBool(%0)";
builtin_types.insert(itype.cname, itype);
// Integer types
@ -3510,8 +3501,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() {
itype.c_type_in = itype.name; \
itype.c_type_out = itype.name; \
itype.c_in_vararg = "%5using godot_variant %1_in = VariantUtils.CreateFromInt(%1);\n"; \
itype.cs_variant_to_managed = "VariantUtils.ConvertTo" m_int_struct_name "(%0)"; \
itype.cs_managed_to_variant = "VariantUtils.CreateFromInt(%0)"; \
builtin_types.insert(itype.cname, itype); \
}
@ -3547,8 +3536,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() {
itype.c_type_in = itype.proxy_name;
itype.c_type_out = itype.proxy_name;
itype.c_in_vararg = "%5using godot_variant %1_in = VariantUtils.CreateFromFloat(%1);\n";
itype.cs_variant_to_managed = "VariantUtils.ConvertToFloat32(%0)";
itype.cs_managed_to_variant = "VariantUtils.CreateFromFloat(%0)";
builtin_types.insert(itype.cname, itype);
// double
@ -3562,8 +3549,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() {
itype.c_type_in = itype.proxy_name;
itype.c_type_out = itype.proxy_name;
itype.c_in_vararg = "%5using godot_variant %1_in = VariantUtils.CreateFromFloat(%1);\n";
itype.cs_variant_to_managed = "VariantUtils.ConvertToFloat64(%0)";
itype.cs_managed_to_variant = "VariantUtils.CreateFromFloat(%0)";
builtin_types.insert(itype.cname, itype);
}
@ -3581,8 +3566,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() {
itype.c_type_out = itype.cs_type;
itype.c_type_is_disposable_struct = true;
itype.c_in_vararg = "%5using godot_variant %1_in = VariantUtils.CreateFromString(%1);\n";
itype.cs_variant_to_managed = "VariantUtils.ConvertToStringObject(%0)";
itype.cs_managed_to_variant = "VariantUtils.CreateFromString(%0)";
builtin_types.insert(itype.cname, itype);
// StringName
@ -3601,8 +3584,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() {
itype.c_in_vararg = "%5using godot_variant %1_in = VariantUtils.CreateFromStringName(%1);\n";
itype.c_type_is_disposable_struct = false; // [c_out] takes ownership
itype.c_ret_needs_default_initialization = true;
itype.cs_variant_to_managed = "VariantUtils.ConvertToStringNameObject(%0)";
itype.cs_managed_to_variant = "VariantUtils.CreateFromStringName(%0)";
builtin_types.insert(itype.cname, itype);
// NodePath
@ -3620,8 +3601,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() {
itype.c_type_out = itype.cs_type;
itype.c_type_is_disposable_struct = false; // [c_out] takes ownership
itype.c_ret_needs_default_initialization = true;
itype.cs_variant_to_managed = "VariantUtils.ConvertToNodePathObject(%0)";
itype.cs_managed_to_variant = "VariantUtils.CreateFromNodePath(%0)";
builtin_types.insert(itype.cname, itype);
// RID
@ -3634,8 +3613,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() {
itype.c_type = itype.cs_type;
itype.c_type_in = itype.c_type;
itype.c_type_out = itype.c_type;
itype.cs_variant_to_managed = "VariantUtils.ConvertToRID(%0)";
itype.cs_managed_to_variant = "VariantUtils.CreateFromRID(%0)";
builtin_types.insert(itype.cname, itype);
// Variant
@ -3652,8 +3629,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() {
itype.c_type_out = itype.cs_type;
itype.c_type_is_disposable_struct = false; // [c_out] takes ownership
itype.c_ret_needs_default_initialization = true;
itype.cs_variant_to_managed = "Variant.CreateCopyingBorrowed(%0)";
itype.cs_managed_to_variant = "%0.CopyNativeVariant()";
builtin_types.insert(itype.cname, itype);
// Callable
@ -3666,8 +3641,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() {
itype.c_type_in = "in " + itype.cs_type;
itype.c_type_out = itype.cs_type;
itype.c_type_is_disposable_struct = true;
itype.cs_variant_to_managed = "VariantUtils.ConvertToCallableManaged(%0)";
itype.cs_managed_to_variant = "VariantUtils.CreateFromCallable(%0)";
builtin_types.insert(itype.cname, itype);
// Signal
@ -3684,8 +3657,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() {
itype.c_type_in = "in " + itype.cs_type;
itype.c_type_out = itype.cs_type;
itype.c_type_is_disposable_struct = true;
itype.cs_variant_to_managed = "VariantUtils.ConvertToSignalInfo(%0)";
itype.cs_managed_to_variant = "VariantUtils.CreateFromSignalInfo(%0)";
builtin_types.insert(itype.cname, itype);
// VarArg (fictitious type to represent variable arguments)
@ -3715,8 +3686,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() {
itype.c_type_in = itype.proxy_name; \
itype.c_type_out = itype.proxy_name; \
itype.c_type_is_disposable_struct = true; \
itype.cs_variant_to_managed = "VariantUtils.ConvertAs%2ToSystemArray(%0)"; \
itype.cs_managed_to_variant = "VariantUtils.CreateFrom%2(%0)"; \
builtin_types.insert(itype.name, itype); \
}
@ -3752,8 +3721,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() {
itype.c_type_out = itype.cs_type;
itype.c_type_is_disposable_struct = false; // [c_out] takes ownership
itype.c_ret_needs_default_initialization = true;
itype.cs_variant_to_managed = "VariantUtils.ConvertToArrayObject(%0)";
itype.cs_managed_to_variant = "VariantUtils.CreateFromArray(%0)";
builtin_types.insert(itype.cname, itype);
// Array_@generic
@ -3761,6 +3728,9 @@ void BindingsGenerator::_populate_builtin_type_interfaces() {
itype.name = "Array_@generic";
itype.cname = itype.name;
itype.cs_out = "%5return new %2(%0(%1));";
// For generic Godot collections, Variant.From<T>/As<T> is slower, so we need this special case
itype.cs_variant_to_managed = "VariantUtils.ConvertToArrayObject(%0)";
itype.cs_managed_to_variant = "VariantUtils.CreateFromArray(%0)";
builtin_types.insert(itype.cname, itype);
// Dictionary
@ -3778,8 +3748,6 @@ void BindingsGenerator::_populate_builtin_type_interfaces() {
itype.c_type_out = itype.cs_type;
itype.c_type_is_disposable_struct = false; // [c_out] takes ownership
itype.c_ret_needs_default_initialization = true;
itype.cs_variant_to_managed = "VariantUtils.ConvertToDictionaryObject(%0)";
itype.cs_managed_to_variant = "VariantUtils.CreateFromDictionary(%0)";
builtin_types.insert(itype.cname, itype);
// Dictionary_@generic
@ -3787,6 +3755,9 @@ void BindingsGenerator::_populate_builtin_type_interfaces() {
itype.name = "Dictionary_@generic";
itype.cname = itype.name;
itype.cs_out = "%5return new %2(%0(%1));";
// For generic Godot collections, Variant.From<T>/As<T> is slower, so we need this special case
itype.cs_variant_to_managed = "VariantUtils.ConvertToDictionaryObject(%0)";
itype.cs_managed_to_variant = "VariantUtils.CreateFromDictionary(%0)";
builtin_types.insert(itype.cname, itype);
// void (fictitious type to represent the return type of methods that do not return anything)
@ -3852,8 +3823,6 @@ void BindingsGenerator::_populate_global_constants() {
enum_itype.cname = ienum.cname;
enum_itype.proxy_name = enum_itype.name;
TypeInterface::postsetup_enum_type(enum_itype);
enum_itype.cs_variant_to_managed = "(%1)VariantUtils.ConvertToInt32(%0)";
enum_itype.cs_managed_to_variant = "VariantUtils.CreateFromInt((int)%0)";
enum_types.insert(enum_itype.cname, enum_itype);
int prefix_length = _determine_enum_prefix(ienum);
@ -3886,8 +3855,6 @@ void BindingsGenerator::_populate_global_constants() {
enum_itype.cname = enum_cname;
enum_itype.proxy_name = enum_itype.name;
TypeInterface::postsetup_enum_type(enum_itype);
enum_itype.cs_variant_to_managed = "(%1)VariantUtils.ConvertToInt32(%0)";
enum_itype.cs_managed_to_variant = "VariantUtils.CreateFromInt((int)%0)";
enum_types.insert(enum_itype.cname, enum_itype);
}
}

View file

@ -209,7 +209,7 @@ class BindingsGenerator {
String name;
StringName cname;
int type_parameter_count;
int type_parameter_count = 0;
/**
* Identifier name of the base class.
@ -514,7 +514,12 @@ class BindingsGenerator {
static void postsetup_enum_type(TypeInterface &r_enum_itype);
TypeInterface() {}
TypeInterface() {
static String default_cs_variant_to_managed = "VariantUtils.ConvertTo<%1>(%0)";
static String default_cs_managed_to_variant = "VariantUtils.CreateFrom<%1>(%0)";
cs_variant_to_managed = default_cs_variant_to_managed;
cs_managed_to_variant = default_cs_managed_to_variant;
}
};
struct InternalCall {