Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Refactor out MarshallingInfoParser creation from SignatureContext.
  • Loading branch information
jkoritzinsky committed Sep 6, 2022
commit dcf495c09dc3ac8af69bb51d5f97ab3d60fbe224
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo
AnyDiagnosticsSink diagnostics = new();
StubEnvironment env = context.Compilation.CreateStubEnvironment();
AttributeData dllImportAttribute = method.GetAttributes().First(attr => attr.AttributeClass.ToDisplayString() == TypeNames.DllImportAttribute);
SignatureContext targetSignatureContext = SignatureContext.Create(method, CreateInteropAttributeDataFromDllImport(dllImportData), env, diagnostics, dllImportAttribute, typeof(ConvertToLibraryImportAnalyzer).Assembly);
SignatureContext targetSignatureContext = SignatureContext.Create(method, DefaultMarshallingInfoParser.Create(env, diagnostics, method, CreateInteropAttributeDataFromDllImport(dllImportData), dllImportAttribute), env, typeof(ConvertToLibraryImportAnalyzer).Assembly);

var generatorFactoryKey = LibraryImportGeneratorHelpers.CreateGeneratorFactory(env, new LibraryImportGeneratorOptions(context.Options.AnalyzerConfigOptionsProvider.GlobalOptions));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text;
using Microsoft.CodeAnalysis;

namespace Microsoft.Interop
{
internal static class DefaultMarshallingInfoParser
{
public static MarshallingInfoParser Create(StubEnvironment env, IGeneratorDiagnostics diagnostics, IMethodSymbol method, InteropAttributeData interopAttributeData, AttributeData unparsedAttributeData)
{

// Compute the current default string encoding value.
CharEncoding defaultEncoding = CharEncoding.Undefined;
if (interopAttributeData.IsUserDefined.HasFlag(InteropAttributeMember.StringMarshalling))
{
defaultEncoding = interopAttributeData.StringMarshalling switch
{
StringMarshalling.Utf16 => CharEncoding.Utf16,
StringMarshalling.Utf8 => CharEncoding.Utf8,
StringMarshalling.Custom => CharEncoding.Custom,
_ => CharEncoding.Undefined, // [Compat] Do not assume a specific value
};
}
else if (interopAttributeData.IsUserDefined.HasFlag(InteropAttributeMember.StringMarshallingCustomType))
{
defaultEncoding = CharEncoding.Custom;
}

var defaultInfo = new DefaultMarshallingInfo(defaultEncoding, interopAttributeData.StringMarshallingCustomType);

var useSiteAttributeParsers = ImmutableArray.Create<IUseSiteAttributeParser>(
new MarshalAsAttributeParser(env.Compilation, diagnostics, defaultInfo),
new MarshalUsingAttributeParser(env.Compilation, diagnostics));

return new MarshallingInfoParser(
diagnostics,
new MethodSignatureElementInfoProvider(env.Compilation, diagnostics, method, useSiteAttributeParsers),
useSiteAttributeParsers,
ImmutableArray.Create<IMarshallingInfoAttributeParser>(
new MarshalAsAttributeParser(env.Compilation, diagnostics, defaultInfo),
new MarshalUsingAttributeParser(env.Compilation, diagnostics),
new NativeMarshallingAttributeParser(env.Compilation, diagnostics)),
ImmutableArray.Create<ITypeBasedMarshallingInfoProvider>(
new SafeHandleMarshallingInfoProvider(env.Compilation, method.ContainingType),
new ArrayMarshallingInfoProvider(env.Compilation),
new CharMarshallingInfoProvider(defaultInfo),
new StringMarshallingInfoProvider(env.Compilation, diagnostics, unparsedAttributeData, defaultInfo),
new BooleanMarshallingInfoProvider(),
new BlittableTypeMarshallingInfoProvider(env.Compilation)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ private static IncrementalStubGenerationContext CalculateStubInformation(
}

// Create the stub.
var signatureContext = SignatureContext.Create(symbol, libraryImportData, environment, generatorDiagnostics, generatedDllImportAttr, typeof(LibraryImportGenerator).Assembly);
var signatureContext = SignatureContext.Create(symbol, DefaultMarshallingInfoParser.Create(environment, generatorDiagnostics, symbol, libraryImportData, generatedDllImportAttr), environment, typeof(LibraryImportGenerator).Assembly);

var containingTypeContext = new ContainingSyntaxContext(originalSyntax);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,11 @@ public IEnumerable<ParameterSyntax> StubParameters

public static SignatureContext Create(
IMethodSymbol method,
InteropAttributeData interopAttributeData,
MarshallingInfoParser marshallingInfoParser,
StubEnvironment env,
IGeneratorDiagnostics diagnostics,
AttributeData signatureWideMarshallingAttributeData,
Assembly generatorInfoAssembly)
{
ImmutableArray<TypePositionInfo> typeInfos = GenerateTypeInformation(method, interopAttributeData, diagnostics, env, signatureWideMarshallingAttributeData);
ImmutableArray<TypePositionInfo> typeInfos = GenerateTypeInformation(method, marshallingInfoParser, env);

ImmutableArray<AttributeListSyntax>.Builder additionalAttrs = ImmutableArray.CreateBuilder<AttributeListSyntax>();

Expand Down Expand Up @@ -102,49 +100,9 @@ public static SignatureContext Create(

private static ImmutableArray<TypePositionInfo> GenerateTypeInformation(
IMethodSymbol method,
InteropAttributeData interopAttributeData,
IGeneratorDiagnostics diagnostics,
StubEnvironment env,
AttributeData signatureWideMarshallingAttributeData)
MarshallingInfoParser marshallingInfoParser,
StubEnvironment env)
{
// Compute the current default string encoding value.
CharEncoding defaultEncoding = CharEncoding.Undefined;
if (interopAttributeData.IsUserDefined.HasFlag(InteropAttributeMember.StringMarshalling))
{
defaultEncoding = interopAttributeData.StringMarshalling switch
{
StringMarshalling.Utf16 => CharEncoding.Utf16,
StringMarshalling.Utf8 => CharEncoding.Utf8,
StringMarshalling.Custom => CharEncoding.Custom,
_ => CharEncoding.Undefined, // [Compat] Do not assume a specific value
};
}
else if (interopAttributeData.IsUserDefined.HasFlag(InteropAttributeMember.StringMarshallingCustomType))
{
defaultEncoding = CharEncoding.Custom;
}

var defaultInfo = new DefaultMarshallingInfo(defaultEncoding, interopAttributeData.StringMarshallingCustomType);

var useSiteAttributeParsers = ImmutableArray.Create<IUseSiteAttributeParser>(
new MarshalAsAttributeParser(env.Compilation, diagnostics, defaultInfo),
new MarshalUsingAttributeParser(env.Compilation, diagnostics));

var marshallingInfoParser = new MarshallingInfoParser(
diagnostics,
new MethodSignatureElementInfoProvider(env.Compilation, diagnostics, method, useSiteAttributeParsers),
useSiteAttributeParsers,
ImmutableArray.Create<IMarshallingInfoAttributeParser>(
new MarshalAsAttributeParser(env.Compilation, diagnostics, defaultInfo),
new MarshalUsingAttributeParser(env.Compilation, diagnostics),
new NativeMarshallingAttributeParser(env.Compilation, diagnostics)),
ImmutableArray.Create<ITypeBasedMarshallingInfoProvider>(
new SafeHandleMarshallingInfoProvider(env.Compilation, method.ContainingType),
new ArrayMarshallingInfoProvider(env.Compilation),
new CharMarshallingInfoProvider(defaultInfo),
new StringMarshallingInfoProvider(env.Compilation, diagnostics, signatureWideMarshallingAttributeData, defaultInfo),
new BooleanMarshallingInfoProvider(),
new BlittableTypeMarshallingInfoProvider(env.Compilation)));

// Determine parameter and return types
ImmutableArray<TypePositionInfo>.Builder typeInfos = ImmutableArray.CreateBuilder<TypePositionInfo>();
Expand Down