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 @@ -24,7 +24,6 @@ internal sealed record IncrementalStubGenerationContext(
ContainingSyntax StubMethodSyntaxTemplate,
MethodSignatureDiagnosticLocations DiagnosticLocation,
JSExportData JSExportData,
MarshallingGeneratorFactoryKey<JSGeneratorOptions> GeneratorFactoryKey,
SequenceEqualImmutableArray<DiagnosticInfo> Diagnostics);

public static class StepNames
Expand Down Expand Up @@ -57,10 +56,6 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.ReportDiagnostic(invalidMethod.Diagnostic);
});

// Compute generator options
IncrementalValueProvider<JSGeneratorOptions> stubOptions = context.AnalyzerConfigOptionsProvider
.Select(static (options, ct) => new JSGeneratorOptions(options.GlobalOptions));

IncrementalValueProvider<StubEnvironment> stubEnvironment = context.CreateStubEnvironmentProvider();

// Validate environment that is being used to generate stubs.
Expand All @@ -77,16 +72,14 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

IncrementalValuesProvider<(MemberDeclarationSyntax, StatementSyntax, AttributeListSyntax, ImmutableArray<DiagnosticInfo>)> generateSingleStub = methodsToGenerate
.Combine(stubEnvironment)
.Combine(stubOptions)
.Select(static (data, ct) => new
{
data.Left.Left.Syntax,
data.Left.Left.Symbol,
Environment = data.Left.Right,
Options = data.Right
data.Left.Syntax,
data.Left.Symbol,
Environment = data.Right,
})
.Select(
static (data, ct) => CalculateStubInformation(data.Syntax, data.Symbol, data.Environment, data.Options, ct)
static (data, ct) => CalculateStubInformation(data.Syntax, data.Symbol, data.Environment, ct)
)
.WithTrackingName(StepNames.CalculateStubInformation)
.Select(
Expand Down Expand Up @@ -172,7 +165,6 @@ private static IncrementalStubGenerationContext CalculateStubInformation(
MethodDeclarationSyntax originalSyntax,
IMethodSymbol symbol,
StubEnvironment environment,
JSGeneratorOptions options,
CancellationToken ct)
{
ct.ThrowIfCancellationRequested();
Expand Down Expand Up @@ -214,16 +206,9 @@ private static IncrementalStubGenerationContext CalculateStubInformation(
methodSyntaxTemplate,
locations,
jsExportData,
CreateGeneratorFactory(options),
new SequenceEqualImmutableArray<DiagnosticInfo>(generatorDiagnostics.Diagnostics.ToImmutableArray()));
}

private static MarshallingGeneratorFactoryKey<JSGeneratorOptions> CreateGeneratorFactory(JSGeneratorOptions options)
{
JSGeneratorFactory jsGeneratorFactory = new JSGeneratorFactory();
return MarshallingGeneratorFactoryKey.Create(options, jsGeneratorFactory);
}

private static NamespaceDeclarationSyntax GenerateRegSource(
ImmutableArray<(StatementSyntax Registration, AttributeListSyntax Attribute)> methods)
{
Expand Down Expand Up @@ -298,7 +283,7 @@ private static (MemberDeclarationSyntax, StatementSyntax, AttributeListSyntax, I
incrementalContext.JSExportData,
incrementalContext.SignatureContext,
diagnostics,
incrementalContext.GeneratorFactoryKey.GeneratorFactory);
new JSGeneratorFactory());

var wrapperName = "__Wrapper_" + incrementalContext.StubMethodSyntaxTemplate.Identifier + "_" + incrementalContext.SignatureContext.TypesHash;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ internal sealed record IncrementalStubGenerationContext(
ContainingSyntax StubMethodSyntaxTemplate,
MethodSignatureDiagnosticLocations DiagnosticLocation,
JSImportData JSImportData,
MarshallingGeneratorFactoryKey<JSGeneratorOptions> GeneratorFactoryKey,
SequenceEqualImmutableArray<DiagnosticInfo> Diagnostics);

public static class StepNames
Expand Down Expand Up @@ -57,10 +56,6 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.ReportDiagnostic(invalidMethod.Diagnostic);
});

// Compute generator options
IncrementalValueProvider<JSGeneratorOptions> stubOptions = context.AnalyzerConfigOptionsProvider
.Select(static (options, ct) => new JSGeneratorOptions(options.GlobalOptions));

IncrementalValueProvider<StubEnvironment> stubEnvironment = context.CreateStubEnvironmentProvider();

// Validate environment that is being used to generate stubs.
Expand All @@ -77,21 +72,18 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

IncrementalValuesProvider<(MemberDeclarationSyntax, ImmutableArray<DiagnosticInfo>)> generateSingleStub = methodsToGenerate
.Combine(stubEnvironment)
.Combine(stubOptions)
.Select(static (data, ct) => new
{
data.Left.Left.Syntax,
data.Left.Left.Symbol,
Environment = data.Left.Right,
Options = data.Right
data.Left.Syntax,
data.Left.Symbol,
Environment = data.Right,
})
.Select(
static (data, ct) => CalculateStubInformation(data.Syntax, data.Symbol, data.Environment, data.Options, ct)
static (data, ct) => CalculateStubInformation(data.Syntax, data.Symbol, data.Environment, ct)
)
.WithTrackingName(StepNames.CalculateStubInformation)
.Combine(stubOptions)
.Select(
static (data, ct) => GenerateSource(data.Left)
static (data, ct) => GenerateSource(data)
)
.WithComparer(Comparers.GeneratedSyntax)
.WithTrackingName(StepNames.GenerateSingleStub);
Expand Down Expand Up @@ -160,7 +152,6 @@ private static IncrementalStubGenerationContext CalculateStubInformation(
MethodDeclarationSyntax originalSyntax,
IMethodSymbol symbol,
StubEnvironment environment,
JSGeneratorOptions options,
CancellationToken ct)
{
ct.ThrowIfCancellationRequested();
Expand Down Expand Up @@ -201,17 +192,9 @@ private static IncrementalStubGenerationContext CalculateStubInformation(
methodSyntaxTemplate,
locations,
jsImportData,
CreateGeneratorFactory(options),
new SequenceEqualImmutableArray<DiagnosticInfo>(generatorDiagnostics.Diagnostics.ToImmutableArray()));
}

private static MarshallingGeneratorFactoryKey<JSGeneratorOptions> CreateGeneratorFactory(JSGeneratorOptions options)
{
JSGeneratorFactory jsGeneratorFactory = new JSGeneratorFactory();

return MarshallingGeneratorFactoryKey.Create(options, jsGeneratorFactory);
}

private static (MemberDeclarationSyntax, ImmutableArray<DiagnosticInfo>) GenerateSource(
IncrementalStubGenerationContext incrementalContext)
{
Expand All @@ -223,7 +206,7 @@ private static (MemberDeclarationSyntax, ImmutableArray<DiagnosticInfo>) Generat
incrementalContext.JSImportData,
incrementalContext.SignatureContext,
diagnostics,
incrementalContext.GeneratorFactoryKey.GeneratorFactory);
new JSGeneratorFactory());

BlockSyntax code = stubGenerator.GenerateJSImportBody();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public override void Initialize(AnalysisContext context)
new CodeEmitOptions(SkipInit: true),
typeof(ConvertComImportToGeneratedComInterfaceAnalyzer).Assembly);

var managedToUnmanagedFactory = ComInterfaceGeneratorHelpers.CreateGeneratorFactory(env, MarshalDirection.ManagedToUnmanaged);
var unmanagedToManagedFactory = ComInterfaceGeneratorHelpers.CreateGeneratorFactory(env, MarshalDirection.UnmanagedToManaged);
var managedToUnmanagedFactory = ComInterfaceGeneratorHelpers.GetGeneratorFactory(env.EnvironmentFlags, MarshalDirection.ManagedToUnmanaged);
var unmanagedToManagedFactory = ComInterfaceGeneratorHelpers.GetGeneratorFactory(env.EnvironmentFlags, MarshalDirection.UnmanagedToManaged);

mayRequireAdditionalWork = diagnostics.Diagnostics.Any();
bool anyExplicitlyUnsupportedInfo = false;
Expand Down Expand Up @@ -118,8 +118,8 @@ public override void Initialize(AnalysisContext context)
info = info with { MarshallingAttributeInfo = inner };
}
// Run both factories and collect any binding failures.
ResolvedGenerator unmanagedToManagedGenerator = unmanagedToManagedFactory.GeneratorFactory.Create(info, nativeToManagedStubCodeContext);
ResolvedGenerator managedToUnmanagedGenerator = managedToUnmanagedFactory.GeneratorFactory.Create(info, managedToNativeStubCodeContext);
ResolvedGenerator unmanagedToManagedGenerator = unmanagedToManagedFactory.Create(info, nativeToManagedStubCodeContext);
ResolvedGenerator managedToUnmanagedGenerator = managedToUnmanagedFactory.Create(info, managedToNativeStubCodeContext);
return managedToUnmanagedGenerator with
{
Diagnostics = managedToUnmanagedGenerator.Diagnostics.AddRange(unmanagedToManagedGenerator.Diagnostics)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,7 @@ private static IncrementalMethodStubGenerationContext CalculateStubInformation(M
callConv.ToSequenceEqualImmutableArray(SyntaxEquivalentComparer.Instance),
virtualMethodIndexData,
new ComExceptionMarshalling(),
ComInterfaceGeneratorHelpers.CreateGeneratorFactory(environment, MarshalDirection.ManagedToUnmanaged),
ComInterfaceGeneratorHelpers.CreateGeneratorFactory(environment, MarshalDirection.UnmanagedToManaged),
environment.EnvironmentFlags,
owningInterface,
declaringType,
generatorDiagnostics.Diagnostics.ToSequenceEqualImmutableArray(),
Expand Down Expand Up @@ -569,7 +568,8 @@ private static InterfaceDeclarationSyntax GenerateImplementationVTable(ComInterf
interfaceMethods.DeclaredMethods
.Where(context => context.UnmanagedToManagedStub.Diagnostics.All(diag => diag.Descriptor.DefaultSeverity != DiagnosticSeverity.Error))
.Select(context => context.GenerationContext),
vtableLocalName);
vtableLocalName,
ComInterfaceGeneratorHelpers.GetGeneratorFactory);

return ImplementationInterfaceTemplate
.AddMembers(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ namespace Microsoft.Interop
{
internal static class ComInterfaceGeneratorHelpers
{
public static MarshallingGeneratorFactoryKey<FactoryKey> CreateGeneratorFactory(StubEnvironment env, MarshalDirection direction)
private static readonly IMarshallingGeneratorFactory s_managedToUnmanagedDisabledMarshallingGeneratorFactory = CreateGeneratorFactory(EnvironmentFlags.DisableRuntimeMarshalling, MarshalDirection.ManagedToUnmanaged);
private static readonly IMarshallingGeneratorFactory s_unmanagedToManagedDisabledMarshallingGeneratorFactory = CreateGeneratorFactory(EnvironmentFlags.DisableRuntimeMarshalling, MarshalDirection.UnmanagedToManaged);
private static readonly IMarshallingGeneratorFactory s_managedToUnmanagedEnabledMarshallingGeneratorFactory = CreateGeneratorFactory(EnvironmentFlags.None, MarshalDirection.ManagedToUnmanaged);
private static readonly IMarshallingGeneratorFactory s_unmanagedToManagedEnabledMarshallingGeneratorFactory = CreateGeneratorFactory(EnvironmentFlags.None, MarshalDirection.UnmanagedToManaged);

private static IMarshallingGeneratorFactory CreateGeneratorFactory(EnvironmentFlags env, MarshalDirection direction)
{
IMarshallingGeneratorFactory generatorFactory;

Expand All @@ -20,7 +25,7 @@ public static MarshallingGeneratorFactoryKey<FactoryKey> CreateGeneratorFactory(

// Since the char type can go into the P/Invoke signature here, we can only use it when
// runtime marshalling is disabled.
generatorFactory = new CharMarshallingGeneratorFactory(generatorFactory, useBlittableMarshallerForUtf16: env.EnvironmentFlags.HasFlag(EnvironmentFlags.DisableRuntimeMarshalling), TypeNames.GeneratedComInterfaceAttribute_ShortName);
generatorFactory = new CharMarshallingGeneratorFactory(generatorFactory, useBlittableMarshallerForUtf16: env.HasFlag(EnvironmentFlags.DisableRuntimeMarshalling), TypeNames.GeneratedComInterfaceAttribute_ShortName);

InteropGenerationOptions interopGenerationOptions = new(UseMarshalType: true);
generatorFactory = new MarshalAsMarshallingGeneratorFactory(interopGenerationOptions, generatorFactory);
Expand All @@ -31,14 +36,14 @@ public static MarshallingGeneratorFactoryKey<FactoryKey> CreateGeneratorFactory(
// Since the char type in an array will not be part of the P/Invoke signature, we can
// use the regular blittable marshaller in all cases.
new CharMarshallingGeneratorFactory(generatorFactory, useBlittableMarshallerForUtf16: true, TypeNames.GeneratedComInterfaceAttribute_ShortName),
new AttributedMarshallingModelOptions(env.EnvironmentFlags.HasFlag(EnvironmentFlags.DisableRuntimeMarshalling), MarshalMode.ElementIn, MarshalMode.ElementRef, MarshalMode.ElementOut));
new AttributedMarshallingModelOptions(env.HasFlag(EnvironmentFlags.DisableRuntimeMarshalling), MarshalMode.ElementIn, MarshalMode.ElementRef, MarshalMode.ElementOut));
// 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(
env.EnvironmentFlags.HasFlag(EnvironmentFlags.DisableRuntimeMarshalling),
env.HasFlag(EnvironmentFlags.DisableRuntimeMarshalling),
direction == MarshalDirection.ManagedToUnmanaged
? MarshalMode.ManagedToUnmanagedIn
: MarshalMode.UnmanagedToManagedOut,
Expand All @@ -55,7 +60,17 @@ public static MarshallingGeneratorFactoryKey<FactoryKey> CreateGeneratorFactory(

generatorFactory = new ByValueContentsMarshalKindValidator(generatorFactory);

return MarshallingGeneratorFactoryKey.Create(new FactoryKey(env.EnvironmentFlags.HasFlag(EnvironmentFlags.DisableRuntimeMarshalling)), generatorFactory);
return generatorFactory;
}

public static IMarshallingGeneratorFactory GetGeneratorFactory(EnvironmentFlags env, MarshalDirection direction)
=> (env.HasFlag(EnvironmentFlags.DisableRuntimeMarshalling), direction) switch
{
(true, MarshalDirection.ManagedToUnmanaged) => s_managedToUnmanagedDisabledMarshallingGeneratorFactory,
(true, MarshalDirection.UnmanagedToManaged) => s_unmanagedToManagedDisabledMarshallingGeneratorFactory,
(false, MarshalDirection.ManagedToUnmanaged) => s_managedToUnmanagedEnabledMarshallingGeneratorFactory,
(false, MarshalDirection.UnmanagedToManaged) => s_unmanagedToManagedEnabledMarshallingGeneratorFactory,
_ => throw new UnreachableException(),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private GeneratedMethodContextBase CreateManagedToUnmanagedStub()
{
return new SkippedStubContext(OriginalDeclaringInterface.Info.Type);
}
var (methodStub, diagnostics) = VirtualMethodPointerStubGenerator.GenerateManagedToNativeStub(GenerationContext);
var (methodStub, diagnostics) = VirtualMethodPointerStubGenerator.GenerateManagedToNativeStub(GenerationContext, ComInterfaceGeneratorHelpers.GetGeneratorFactory);
return new GeneratedStubCodeContext(GenerationContext.TypeKeyOwner, GenerationContext.ContainingSyntaxContext, new(methodStub), new(diagnostics));
}

Expand All @@ -93,7 +93,7 @@ private GeneratedMethodContextBase CreateUnmanagedToManagedStub()
{
return new SkippedStubContext(GenerationContext.OriginalDefiningType);
}
var (methodStub, diagnostics) = VirtualMethodPointerStubGenerator.GenerateNativeToManagedStub(GenerationContext);
var (methodStub, diagnostics) = VirtualMethodPointerStubGenerator.GenerateNativeToManagedStub(GenerationContext, ComInterfaceGeneratorHelpers.GetGeneratorFactory);
return new GeneratedStubCodeContext(GenerationContext.OriginalDefiningType, GenerationContext.ContainingSyntaxContext, new(methodStub), new(diagnostics));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

namespace Microsoft.Interop
{
internal record struct FactoryKey(bool RuntimeMarshallingDisabled);

internal abstract record GeneratedMethodContextBase(ManagedTypeInfo OriginalDefiningType, SequenceEqualImmutableArray<DiagnosticInfo> Diagnostics);

internal sealed record IncrementalMethodStubGenerationContext(
Expand All @@ -19,8 +17,7 @@ internal sealed record IncrementalMethodStubGenerationContext(
SequenceEqualImmutableArray<FunctionPointerUnmanagedCallingConventionSyntax> CallingConvention,
VirtualMethodIndexData VtableIndexData,
MarshallingInfo ExceptionMarshallingInfo,
MarshallingGeneratorFactoryKey<FactoryKey> ManagedToUnmanagedGeneratorFactory,
MarshallingGeneratorFactoryKey<FactoryKey> UnmanagedToManagedGeneratorFactory,
EnvironmentFlags EnvironmentFlags,
ManagedTypeInfo TypeKeyOwner,
ManagedTypeInfo DeclaringType,
SequenceEqualImmutableArray<DiagnosticInfo> Diagnostics,
Expand Down
Loading