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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected BaseJSGenerator(MarshalerType marshalerType, IMarshallingGenerator inn
Type = marshalerType;
}

public TypeSyntax AsNativeType(TypePositionInfo info) => _inner.AsNativeType(info);
public ManagedTypeInfo AsNativeType(TypePositionInfo info) => _inner.AsNativeType(info);
public ParameterSyntax AsParameter(TypePositionInfo info) => _inner.AsParameter(info);
public bool IsSupported(TargetFramework target, Version version) => _inner.IsSupported(target, version);
public virtual bool UsesNativeIdentifier(TypePositionInfo info, StubCodeContext context) => _inner.UsesNativeIdentifier(info, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.Interop.JavaScript
{
internal sealed class EmptyJSGenerator : IJSMarshallingGenerator
{
public TypeSyntax AsNativeType(TypePositionInfo info) => info.ManagedType.Syntax;
public ManagedTypeInfo AsNativeType(TypePositionInfo info) => info.ManagedType;
public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeContext context) => Array.Empty<StatementSyntax>();
public IEnumerable<ExpressionSyntax> GenerateBind(TypePositionInfo info, StubCodeContext context) => Array.Empty<ExpressionSyntax>();
public SignatureBehavior GetNativeSignatureBehavior(TypePositionInfo info) => SignatureBehavior.ManagedTypeAndAttributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,13 @@ private IMarshallingGenerator CreateCustomNativeTypeMarshaller(TypePositionInfo
ICustomTypeMarshallingStrategy marshallingStrategy;
if (marshallerData.HasState)
{
marshallingStrategy = new StatefulValueMarshalling(marshallerData.MarshallerType, marshallerData.NativeType.Syntax, marshallerData.Shape);
marshallingStrategy = new StatefulValueMarshalling(marshallerData.MarshallerType, marshallerData.NativeType, marshallerData.Shape);
if (marshallerData.Shape.HasFlag(MarshallerShape.CallerAllocatedBuffer))
marshallingStrategy = new StatefulCallerAllocatedBufferMarshalling(marshallingStrategy, marshallerData.MarshallerType.Syntax, marshallerData.BufferElementType.Syntax);
}
else
{
marshallingStrategy = new StatelessValueMarshalling(marshallerData.MarshallerType.Syntax, marshallerData.NativeType.Syntax, marshallerData.Shape);
marshallingStrategy = new StatelessValueMarshalling(marshallerData.MarshallerType.Syntax, marshallerData.NativeType, marshallerData.Shape);
if (marshallerData.Shape.HasFlag(MarshallerShape.CallerAllocatedBuffer))
marshallingStrategy = new StatelessCallerAllocatedBufferMarshalling(marshallingStrategy, marshallerData.MarshallerType.Syntax, marshallerData.BufferElementType.Syntax, isLinearCollectionMarshalling: false);

Expand Down Expand Up @@ -282,22 +282,27 @@ private IMarshallingGenerator CreateNativeCollectionMarshaller(
}

// Insert the unmanaged element type into the marshaller type
TypeSyntax unmanagedElementType = elementMarshaller.AsNativeType(elementInfo).GetCompatibleGenericTypeParameterSyntax();
TypeSyntax unmanagedElementType = elementMarshaller.AsNativeType(elementInfo).Syntax.GetCompatibleGenericTypeParameterSyntax();
ManagedTypeInfo marshallerType = marshallerData.MarshallerType;
TypeSyntax marshallerTypeSyntax = ReplacePlaceholderSyntaxWithUnmanagedTypeSyntax(marshallerType.Syntax, marshalInfo, unmanagedElementType);
marshallerType = marshallerType with
{
FullTypeName = marshallerTypeSyntax.ToString(),
DiagnosticFormattedName = marshallerTypeSyntax.ToString(),
};
TypeSyntax nativeTypeSyntax = ReplacePlaceholderSyntaxWithUnmanagedTypeSyntax(marshallerData.NativeType.Syntax, marshalInfo, unmanagedElementType);
string newNativeTypeName = ReplacePlaceholderSyntaxWithUnmanagedTypeSyntax(marshallerData.NativeType.Syntax, marshalInfo, unmanagedElementType).ToFullString();
ManagedTypeInfo nativeType = marshallerData.NativeType with
{
FullTypeName = newNativeTypeName,
DiagnosticFormattedName = newNativeTypeName
};

ICustomTypeMarshallingStrategy marshallingStrategy;
bool elementIsBlittable = elementMarshaller is BlittableMarshaller;

if (marshallerData.HasState)
{
marshallingStrategy = new StatefulValueMarshalling(marshallerType, nativeTypeSyntax, marshallerData.Shape);
marshallingStrategy = new StatefulValueMarshalling(marshallerType, nativeType, marshallerData.Shape);
if (marshallerData.Shape.HasFlag(MarshallerShape.CallerAllocatedBuffer))
{
// Check if the buffer element type is actually the unmanaged element type
Expand All @@ -320,11 +325,11 @@ private IMarshallingGenerator CreateNativeCollectionMarshaller(
{
if (elementIsBlittable)
{
marshallingStrategy = new StatelessLinearCollectionBlittableElementsMarshalling(marshallerTypeSyntax, nativeTypeSyntax, marshallerData.Shape, marshallerData.CollectionElementType.Syntax, unmanagedElementType, numElementsExpression);
marshallingStrategy = new StatelessLinearCollectionBlittableElementsMarshalling(marshallerTypeSyntax, nativeType, marshallerData.Shape, marshallerData.CollectionElementType.Syntax, unmanagedElementType, numElementsExpression);
}
else
{
marshallingStrategy = new StatelessLinearCollectionNonBlittableElementsMarshalling(marshallerTypeSyntax, nativeTypeSyntax, marshallerData.Shape, unmanagedElementType, elementMarshaller, elementInfo, numElementsExpression);
marshallingStrategy = new StatelessLinearCollectionNonBlittableElementsMarshalling(marshallerTypeSyntax, nativeType, marshallerData.Shape, unmanagedElementType, elementMarshaller, elementInfo, numElementsExpression);
}

if (marshallerData.Shape.HasFlag(MarshallerShape.CallerAllocatedBuffer))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public sealed class BlittableMarshaller : IMarshallingGenerator
{
public bool IsSupported(TargetFramework target, Version version) => true;

public TypeSyntax AsNativeType(TypePositionInfo info)
public ManagedTypeInfo AsNativeType(TypePositionInfo info)
{
return info.ManagedType.Syntax;
return info.ManagedType;
}

public SignatureBehavior GetNativeSignatureBehavior(TypePositionInfo info)
Expand Down Expand Up @@ -51,7 +51,7 @@ public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeCont
{
yield return FixedStatement(
VariableDeclaration(
PointerType(AsNativeType(info)),
PointerType(AsNativeType(info).Syntax),
SingletonSeparatedList(
VariableDeclarator(Identifier(nativeIdentifier))
.WithInitializer(EqualsValueClause(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ namespace Microsoft.Interop
{
public abstract class BoolMarshallerBase : IMarshallingGenerator
{
private readonly PredefinedTypeSyntax _nativeType;
private readonly ManagedTypeInfo _nativeType;
private readonly int _trueValue;
private readonly int _falseValue;
private readonly bool _compareToTrue;

protected BoolMarshallerBase(PredefinedTypeSyntax nativeType, int trueValue, int falseValue, bool compareToTrue)
protected BoolMarshallerBase(ManagedTypeInfo nativeType, int trueValue, int falseValue, bool compareToTrue)
{
_nativeType = nativeType;
_trueValue = trueValue;
Expand All @@ -29,7 +29,7 @@ protected BoolMarshallerBase(PredefinedTypeSyntax nativeType, int trueValue, int

public bool IsSupported(TargetFramework target, Version version) => true;

public TypeSyntax AsNativeType(TypePositionInfo info)
public ManagedTypeInfo AsNativeType(TypePositionInfo info)
{
Debug.Assert(info.ManagedType is SpecialTypeInfo(_, _, SpecialType.System_Boolean));
return _nativeType;
Expand Down Expand Up @@ -66,7 +66,7 @@ public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeCont
SyntaxKind.SimpleAssignmentExpression,
IdentifierName(nativeIdentifier),
CastExpression(
AsNativeType(info),
AsNativeType(info).Syntax,
ParenthesizedExpression(
ConditionalExpression(IdentifierName(managedIdentifier),
LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(_trueValue)),
Expand Down Expand Up @@ -119,7 +119,7 @@ public sealed class ByteBoolMarshaller : BoolMarshallerBase
/// </summary>
/// <param name="signed">True if the byte should be signed, otherwise false</param>
public ByteBoolMarshaller(bool signed)
: base(PredefinedType(Token(signed ? SyntaxKind.SByteKeyword : SyntaxKind.ByteKeyword)), trueValue: 1, falseValue: 0, compareToTrue: false)
: base(new SpecialTypeInfo(signed ? "sbyte" : "byte", signed ? "sbyte" : "byte", signed ? SpecialType.System_SByte : SpecialType.System_Byte), trueValue: 1, falseValue: 0, compareToTrue: false)
{
}
}
Expand All @@ -137,7 +137,7 @@ public sealed class WinBoolMarshaller : BoolMarshallerBase
/// </summary>
/// <param name="signed">True if the int should be signed, otherwise false</param>
public WinBoolMarshaller(bool signed)
: base(PredefinedType(Token(signed ? SyntaxKind.IntKeyword : SyntaxKind.UIntKeyword)), trueValue: 1, falseValue: 0, compareToTrue: false)
: base(new SpecialTypeInfo(signed ? "int" : "uint", signed ? "int" : "uint", signed ? SpecialType.System_Int32 : SpecialType.System_UInt32), trueValue: 1, falseValue: 0, compareToTrue: false)
{
}
}
Expand All @@ -150,7 +150,7 @@ public sealed class VariantBoolMarshaller : BoolMarshallerBase
private const short VARIANT_TRUE = -1;
private const short VARIANT_FALSE = 0;
public VariantBoolMarshaller()
: base(PredefinedType(Token(SyntaxKind.ShortKeyword)), trueValue: VARIANT_TRUE, falseValue: VARIANT_FALSE, compareToTrue: true)
: base(new SpecialTypeInfo("short", "short", SpecialType.System_Int16), trueValue: VARIANT_TRUE, falseValue: VARIANT_FALSE, compareToTrue: true)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Microsoft.Interop
{
public sealed class Utf16CharMarshaller : IMarshallingGenerator
{
private static readonly PredefinedTypeSyntax s_nativeType = PredefinedType(Token(SyntaxKind.UShortKeyword));
private static readonly ManagedTypeInfo s_nativeType = new SpecialTypeInfo("ushort", "ushort", SpecialType.System_UInt16);

public Utf16CharMarshaller()
{
Expand All @@ -36,7 +36,7 @@ public ValueBoundaryBehavior GetValueBoundaryBehavior(TypePositionInfo info, Stu
return ValueBoundaryBehavior.AddressOfNativeIdentifier;
}

public TypeSyntax AsNativeType(TypePositionInfo info)
public ManagedTypeInfo AsNativeType(TypePositionInfo info)
{
Debug.Assert(info.ManagedType is SpecialTypeInfo(_, _, SpecialType.System_Char));
return s_nativeType;
Expand Down Expand Up @@ -70,12 +70,12 @@ public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeCont
),
// ushort* <native> = (ushort*)<pinned>;
LocalDeclarationStatement(
VariableDeclaration(PointerType(AsNativeType(info)),
VariableDeclaration(PointerType(AsNativeType(info).Syntax),
SingletonSeparatedList(
VariableDeclarator(nativeIdentifier)
.WithInitializer(EqualsValueClause(
CastExpression(
PointerType(AsNativeType(info)),
PointerType(AsNativeType(info).Syntax),
IdentifierName(PinnedIdentifier(info.InstanceIdentifier))))))))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ValueBoundaryBehavior GetValueBoundaryBehavior(TypePositionInfo info, Stu
return info.IsByRef ? ValueBoundaryBehavior.AddressOfNativeIdentifier : ValueBoundaryBehavior.NativeIdentifier;
}

public TypeSyntax AsNativeType(TypePositionInfo info)
public ManagedTypeInfo AsNativeType(TypePositionInfo info)
{
return _nativeTypeMarshaller.AsNativeType(info);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public sealed class DelegateMarshaller : IMarshallingGenerator
{
public bool IsSupported(TargetFramework target, Version version) => true;

public TypeSyntax AsNativeType(TypePositionInfo info)
public ManagedTypeInfo AsNativeType(TypePositionInfo info)
{
return MarshallerHelpers.SystemIntPtrType;
return SpecialTypeInfo.IntPtr;
}

public SignatureBehavior GetNativeSignatureBehavior(TypePositionInfo info)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ protected StatementSyntax GenerateContentsMarshallingStatement(
List(_elementMarshaller.Generate(localElementInfo, elementSetupSubContext)
.Concat(elementStatements)));

if (_elementMarshaller.AsNativeType(_elementInfo) is PointerTypeSyntax elementNativeType)
if (_elementMarshaller.AsNativeType(_elementInfo).Syntax is PointerTypeSyntax elementNativeType)
{
PointerNativeTypeAssignmentRewriter rewriter = new(elementSetupSubContext.GetIdentifiers(localElementInfo).native, elementNativeType);
marshallingStatement = (StatementSyntax)rewriter.Visit(marshallingStatement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public sealed class Forwarder : IMarshallingGenerator
{
public bool IsSupported(TargetFramework target, Version version) => true;

public TypeSyntax AsNativeType(TypePositionInfo info)
public ManagedTypeInfo AsNativeType(TypePositionInfo info)
{
return info.ManagedType.Syntax;
return info.ManagedType;
}

public SignatureBehavior GetNativeSignatureBehavior(TypePositionInfo info)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.Interop
/// </summary>
internal interface ICustomTypeMarshallingStrategy
{
TypeSyntax AsNativeType(TypePositionInfo info);
ManagedTypeInfo AsNativeType(TypePositionInfo info);

IEnumerable<StatementSyntax> GenerateCleanupStatements(TypePositionInfo info, StubCodeContext context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public interface IMarshallingGenerator
/// Get the native type syntax for <paramref name="info"/>
/// </summary>
/// <param name="info">Object to marshal</param>
/// <returns>Type syntax for the native type representing <paramref name="info"/></returns>
TypeSyntax AsNativeType(TypePositionInfo info);
/// <returns>Managed type info for the native type representing <paramref name="info"/></returns>
ManagedTypeInfo AsNativeType(TypePositionInfo info);

/// <summary>
/// Get shape that represents the provided <paramref name="info"/> in the native signature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static TypeSyntax AsReturnType(this IMarshallingGenerator generator, Type
return generator.GetNativeSignatureBehavior(info) switch
{
SignatureBehavior.ManagedTypeAndAttributes => info.ManagedType.Syntax,
SignatureBehavior.NativeType => generator.AsNativeType(info),
SignatureBehavior.PointerToNativeType => PointerType(generator.AsNativeType(info)),
SignatureBehavior.NativeType => generator.AsNativeType(info).Syntax,
SignatureBehavior.PointerToNativeType => PointerType(generator.AsNativeType(info).Syntax),
_ => throw new InvalidOperationException()
};
}
Expand Down Expand Up @@ -62,8 +62,8 @@ public static ParameterSyntax AsParameter(this IMarshallingGenerator generator,
return Parameter(Identifier(info.InstanceIdentifier))
.WithType(behavior switch
{
SignatureBehavior.NativeType => generator.AsNativeType(info),
SignatureBehavior.PointerToNativeType => PointerType(generator.AsNativeType(info)),
SignatureBehavior.NativeType => generator.AsNativeType(info).Syntax,
SignatureBehavior.PointerToNativeType => PointerType(generator.AsNativeType(info).Syntax),
_ => throw new InvalidOperationException()
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public bool IsSupported(TargetFramework target, Version version)
return target is TargetFramework.Net && version.Major >= 6;
}

public TypeSyntax AsNativeType(TypePositionInfo info)
public ManagedTypeInfo AsNativeType(TypePositionInfo info)
{
return MarshallerHelpers.SystemIntPtrType;
return SpecialTypeInfo.IntPtr;
}

public SignatureBehavior GetNativeSignatureBehavior(TypePositionInfo info)
Expand Down Expand Up @@ -117,7 +117,7 @@ public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeCont
{
yield return LocalDeclarationStatement(
VariableDeclaration(
AsNativeType(info),
AsNativeType(info).Syntax,
SingletonSeparatedList(
VariableDeclarator(handleValueBackupIdentifier)
.WithInitializer(EqualsValueClause(
Expand Down
Loading