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 @@ -5,10 +5,10 @@

namespace Microsoft.Interop
{
internal record DllImportGeneratorOptions(bool GenerateForwarders, bool UseMarshalType, bool UseInternalUnsafeType)
internal record DllImportGeneratorOptions(bool GenerateForwarders, bool UseMarshalType)
{
public DllImportGeneratorOptions(AnalyzerConfigOptions options)
: this(options.GenerateForwarders(), options.UseMarshalType(), options.UseInternalUnsafeType())
: this(options.GenerateForwarders(), options.UseMarshalType())
{
}
}
Expand All @@ -17,8 +17,6 @@ public static class OptionsHelper
{
public const string UseMarshalTypeOption = "build_property.DllImportGenerator_UseMarshalType";
public const string GenerateForwardersOption = "build_property.DllImportGenerator_GenerateForwarders";
public const string UseInternalUnsafeTypeOption = "build_property.DllImportGenerator_UseInternalUnsafeType";

private static bool GetBoolOption(this AnalyzerConfigOptions options, string key)
{
return options.TryGetValue(key, out string? value)
Expand All @@ -29,7 +27,5 @@ private static bool GetBoolOption(this AnalyzerConfigOptions options, string key
internal static bool UseMarshalType(this AnalyzerConfigOptions options) => options.GetBoolOption(UseMarshalTypeOption);

internal static bool GenerateForwarders(this AnalyzerConfigOptions options) => options.GetBoolOption(GenerateForwardersOption);

internal static bool UseInternalUnsafeType(this AnalyzerConfigOptions options) => options.GetBoolOption(UseInternalUnsafeTypeOption);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private static (ImmutableArray<TypePositionInfo>, IMarshallingGeneratorFactory)
NativeIndex = TypePositionInfo.ReturnIndex
};

InteropGenerationOptions options = new(env.Options.UseMarshalType, env.Options.UseInternalUnsafeType);
InteropGenerationOptions options = new(env.Options.UseMarshalType);
IMarshallingGeneratorFactory generatorFactory;

if (env.Options.GenerateForwarders)
Expand All @@ -213,13 +213,14 @@ private static (ImmutableArray<TypePositionInfo>, IMarshallingGeneratorFactory)
generatorFactory = new MarshalAsMarshallingGeneratorFactory(options, generatorFactory);

IAssemblySymbol coreLibraryAssembly = env.Compilation.GetSpecialType(SpecialType.System_Object).ContainingAssembly;
ITypeSymbol disabledRuntimeMarshallingAttributeType = coreLibraryAssembly.GetTypeByMetadataName(TypeNames.System_Runtime_CompilerServices_DisableRuntimeMarshallingAttribute);
bool runtimeMarshallingDisabled = env.Compilation.Assembly.GetAttributes().Any(attr => SymbolEqualityComparer.Default.Equals(attr.AttributeClass, disabledRuntimeMarshallingAttributeType));
ITypeSymbol? disabledRuntimeMarshallingAttributeType = coreLibraryAssembly.GetTypeByMetadataName(TypeNames.System_Runtime_CompilerServices_DisableRuntimeMarshallingAttribute);
bool runtimeMarshallingDisabled = disabledRuntimeMarshallingAttributeType is not null
&& env.Compilation.Assembly.GetAttributes().Any(attr => SymbolEqualityComparer.Default.Equals(attr.AttributeClass, disabledRuntimeMarshallingAttributeType));

IMarshallingGeneratorFactory elementFactory = new AttributedMarshallingModelGeneratorFactory(generatorFactory, new AttributedMarshallingModelOptions(options, runtimeMarshallingDisabled));
IMarshallingGeneratorFactory elementFactory = new AttributedMarshallingModelGeneratorFactory(generatorFactory, new AttributedMarshallingModelOptions(runtimeMarshallingDisabled));
// We don't need to include the later generator factories for collection elements
// as the later generator factories only apply to parameters.
generatorFactory = new AttributedMarshallingModelGeneratorFactory(generatorFactory, elementFactory, new AttributedMarshallingModelOptions(options, runtimeMarshallingDisabled));
generatorFactory = new AttributedMarshallingModelGeneratorFactory(generatorFactory, elementFactory, new AttributedMarshallingModelOptions(runtimeMarshallingDisabled));

generatorFactory = new ByValueContentsMarshalKindValidator(generatorFactory);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,5 @@
of generating a stub that handles all of the marshalling.
-->
<CompilerVisibleProperty Include="DllImportGenerator_GenerateForwarders" />
<!--
Use the Internal.Runtime.ComplierServices.Unsafe type instead of
the System.Runtime.CompilerServices.Unsafe type when emitting code.
-->
<CompilerVisibleProperty Include="DllImportGenerator_UseInternalUnsafeType" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

namespace Microsoft.Interop
{
public readonly record struct InteropGenerationOptions(bool UseMarshalType, bool UseInternalUnsafeType);
public readonly record struct InteropGenerationOptions(bool UseMarshalType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ public sealed class ArrayMarshaller : IMarshallingGenerator
private readonly IMarshallingGenerator _manualMarshallingGenerator;
private readonly TypeSyntax _elementType;
private readonly bool _enablePinning;
private readonly InteropGenerationOptions _options;

public ArrayMarshaller(IMarshallingGenerator manualMarshallingGenerator, TypeSyntax elementType, bool enablePinning, InteropGenerationOptions options)
public ArrayMarshaller(IMarshallingGenerator manualMarshallingGenerator, TypeSyntax elementType, bool enablePinning)
{
_manualMarshallingGenerator = manualMarshallingGenerator;
_elementType = elementType;
_enablePinning = enablePinning;
_options = options;
}

public bool IsSupported(TargetFramework target, Version version)
Expand Down Expand Up @@ -138,7 +136,7 @@ private IEnumerable<StatementSyntax> GeneratePinningPath(TypePositionInfo info,
PrefixUnaryExpression(SyntaxKind.AddressOfExpression,
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
ParseTypeName(TypeNames.Unsafe(_options)),
ParseTypeName(TypeNames.System_Runtime_CompilerServices_Unsafe),
GenericName("As").AddTypeArgumentListArguments(
arrayElementType,
PredefinedType(Token(SyntaxKind.ByteKeyword)))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Microsoft.Interop
{
public readonly record struct AttributedMarshallingModelOptions(InteropGenerationOptions InteropOptions, bool RuntimeMarshallingDisabled);
public readonly record struct AttributedMarshallingModelOptions(bool RuntimeMarshallingDisabled);

public class AttributedMarshallingModelGeneratorFactory : IMarshallingGeneratorFactory
{
Expand Down Expand Up @@ -294,8 +294,7 @@ private IMarshallingGenerator CreateNativeCollectionMarshaller(
return new ArrayMarshaller(
new CustomNativeTypeMarshallingGenerator(marshallingStrategy, enableByValueContentsMarshalling: true),
elementType,
isBlittable,
Options.InteropOptions);
isBlittable);
}

IMarshallingGenerator marshallingGenerator = new CustomNativeTypeMarshallingGenerator(marshallingStrategy, enableByValueContentsMarshalling: false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,12 @@ public static string MarshalEx(InteropGenerationOptions options)

public const string System_Runtime_CompilerServices_SkipLocalsInitAttribute = "System.Runtime.CompilerServices.SkipLocalsInitAttribute";

private const string System_Runtime_CompilerServices_Unsafe = "System.Runtime.CompilerServices.Unsafe";


public static string Unsafe(InteropGenerationOptions options)
{
return System_Runtime_CompilerServices_Unsafe;
}
public const string System_Runtime_CompilerServices_Unsafe = "System.Runtime.CompilerServices.Unsafe";

public const string System_Runtime_CompilerServices_DisableRuntimeMarshallingAttribute = "System.Runtime.CompilerServices.DisableRuntimeMarshallingAttribute";

public const string DefaultDllImportSearchPathsAttribute = "System.Runtime.InteropServices.DefaultDllImportSearchPathsAttribute";

public const string DllImportSearchPath = "System.Runtime.InteropServices.DllImportSearchPath";
}
}