Skip to content

Commit f4cf69e

Browse files
authored
[LibraryImportGenerator] Basic stateless value marshaller support (#71116)
- Separate out previous custom marshalling support into _V1 classes - Add basic support for new stateless marshaller shape for values This only adds really basic support for stateless value marshalling. It doesn't deal with support for caller-allocated buffer, pinning, guaranteed unmarshal, or linear collection marshalling.
1 parent 315e931 commit f4cf69e

41 files changed

Lines changed: 1729 additions & 565 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/CustomTypeMarshallerAnalyzer.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ private static void AnalyzeManagedTypeMarshallingInfo(SymbolAnalysisContext cont
490490
type.ToDisplayString()));
491491
}
492492

493-
(bool hasCustomTypeMarshallerAttribute, ITypeSymbol? marshallerManagedType, _) = ManualTypeMarshallingHelper.GetMarshallerShapeInfo(marshallerType);
493+
(bool hasCustomTypeMarshallerAttribute, ITypeSymbol? marshallerManagedType, _) = ManualTypeMarshallingHelper_V1.GetMarshallerShapeInfo(marshallerType);
494494

495495
marshallerManagedType = ManualTypeMarshallingHelper.ResolveManagedType(marshallerManagedType, marshallerType, context.Compilation);
496496

@@ -534,7 +534,7 @@ private static bool TypeSymbolsConstructedFromEqualTypes(ITypeSymbol left, IType
534534
public void AnalyzeMarshallerType(SymbolAnalysisContext context)
535535
{
536536
INamedTypeSymbol marshallerType = (INamedTypeSymbol)context.Symbol;
537-
(bool hasCustomTypeMarshallerAttribute, ITypeSymbol? type, CustomTypeMarshallerData? marshallerDataMaybe) = ManualTypeMarshallingHelper.GetMarshallerShapeInfo(marshallerType);
537+
(bool hasCustomTypeMarshallerAttribute, ITypeSymbol? type, CustomTypeMarshallerData_V1? marshallerDataMaybe) = ManualTypeMarshallingHelper_V1.GetMarshallerShapeInfo(marshallerType);
538538
type = ManualTypeMarshallingHelper.ResolveManagedType(type, marshallerType, context.Compilation);
539539

540540
if (!hasCustomTypeMarshallerAttribute)
@@ -583,12 +583,12 @@ public void AnalyzeMarshallerType(SymbolAnalysisContext context)
583583
continue;
584584
}
585585

586-
if (inConstructor is null && ManualTypeMarshallingHelper.IsManagedToNativeConstructor(ctor, type, marshallerData.Kind))
586+
if (inConstructor is null && ManualTypeMarshallingHelper_V1.IsManagedToNativeConstructor(ctor, type, marshallerData.Kind))
587587
{
588588
inConstructor = ctor;
589589
}
590590

591-
if (callerAllocatedSpanConstructor is null && ManualTypeMarshallingHelper.IsCallerAllocatedSpanConstructor(ctor, type, _spanOfT, marshallerData.Kind, out _))
591+
if (callerAllocatedSpanConstructor is null && ManualTypeMarshallingHelper_V1.IsCallerAllocatedSpanConstructor(ctor, type, _spanOfT, marshallerData.Kind, out _))
592592
{
593593
callerAllocatedSpanConstructor = ctor;
594594
}
@@ -653,30 +653,30 @@ public void AnalyzeMarshallerType(SymbolAnalysisContext context)
653653
}
654654
}
655655

656-
if (marshallerData.Direction.HasFlag(CustomTypeMarshallerDirection.Out) && !ManualTypeMarshallingHelper.HasToManagedMethod(marshallerType, type))
656+
if (marshallerData.Direction.HasFlag(CustomTypeMarshallerDirection.Out) && !ManualTypeMarshallingHelper_V1.HasToManagedMethod(marshallerType, type))
657657
{
658658
context.ReportDiagnostic(
659659
marshallerType.CreateDiagnostic(
660660
OutRequiresToManagedRule,
661661
ImmutableDictionary<string, string>.Empty.Add(
662662
MissingMemberNames.Key,
663-
ShapeMemberNames.Value.ToManaged),
663+
ShapeMemberNames_V1.Value.ToManaged),
664664
marshallerType.ToDisplayString()));
665665
}
666666

667667
if (marshallerData.Kind == CustomTypeMarshallerKind.LinearCollection)
668668
{
669-
IMethodSymbol? getManagedValuesSourceMethod = ManualTypeMarshallingHelper.FindGetManagedValuesSourceMethod(marshallerType, _readOnlySpanOfT);
670-
IMethodSymbol? getManagedValuesDestinationMethod = ManualTypeMarshallingHelper.FindGetManagedValuesDestinationMethod(marshallerType, _spanOfT);
671-
IMethodSymbol? getNativeValuesSourceMethod = ManualTypeMarshallingHelper.FindGetNativeValuesSourceMethod(marshallerType, _readOnlySpanOfByte);
672-
IMethodSymbol? getNativeValuesDestinationMethod = ManualTypeMarshallingHelper.FindGetNativeValuesDestinationMethod(marshallerType, _spanOfByte);
669+
IMethodSymbol? getManagedValuesSourceMethod = ManualTypeMarshallingHelper_V1.FindGetManagedValuesSourceMethod(marshallerType, _readOnlySpanOfT);
670+
IMethodSymbol? getManagedValuesDestinationMethod = ManualTypeMarshallingHelper_V1.FindGetManagedValuesDestinationMethod(marshallerType, _spanOfT);
671+
IMethodSymbol? getNativeValuesSourceMethod = ManualTypeMarshallingHelper_V1.FindGetNativeValuesSourceMethod(marshallerType, _readOnlySpanOfByte);
672+
IMethodSymbol? getNativeValuesDestinationMethod = ManualTypeMarshallingHelper_V1.FindGetNativeValuesDestinationMethod(marshallerType, _spanOfByte);
673673
if (marshallerData.Direction.HasFlag(CustomTypeMarshallerDirection.In) && (getManagedValuesSourceMethod is null || getNativeValuesDestinationMethod is null))
674674
{
675675
var missingMembers = (getManagedValuesSourceMethod, getNativeValuesDestinationMethod) switch
676676
{
677-
(null, not null) => ShapeMemberNames.LinearCollection.GetManagedValuesSource,
678-
(not null, null) => ShapeMemberNames.LinearCollection.GetNativeValuesDestination,
679-
(null, null) => $"{ShapeMemberNames.LinearCollection.GetManagedValuesSource}{MissingMemberNames.Delimiter}{ShapeMemberNames.LinearCollection.GetNativeValuesDestination}",
677+
(null, not null) => ShapeMemberNames_V1.LinearCollection.GetManagedValuesSource,
678+
(not null, null) => ShapeMemberNames_V1.LinearCollection.GetNativeValuesDestination,
679+
(null, null) => $"{ShapeMemberNames_V1.LinearCollection.GetManagedValuesSource}{MissingMemberNames.Delimiter}{ShapeMemberNames_V1.LinearCollection.GetNativeValuesDestination}",
680680
(not null, not null) => string.Empty
681681
};
682682
context.ReportDiagnostic(
@@ -692,9 +692,9 @@ public void AnalyzeMarshallerType(SymbolAnalysisContext context)
692692
{
693693
var missingMembers = (getNativeValuesSourceMethod, getManagedValuesDestinationMethod) switch
694694
{
695-
(not null, null) => ShapeMemberNames.LinearCollection.GetNativeValuesSource,
696-
(null, not null) => ShapeMemberNames.LinearCollection.GetManagedValuesDestination,
697-
(null, null) => $"{ShapeMemberNames.LinearCollection.GetNativeValuesSource}{MissingMemberNames.Delimiter}{ShapeMemberNames.LinearCollection.GetManagedValuesDestination}",
695+
(not null, null) => ShapeMemberNames_V1.LinearCollection.GetNativeValuesSource,
696+
(null, not null) => ShapeMemberNames_V1.LinearCollection.GetManagedValuesDestination,
697+
(null, null) => $"{ShapeMemberNames_V1.LinearCollection.GetNativeValuesSource}{MissingMemberNames.Delimiter}{ShapeMemberNames_V1.LinearCollection.GetManagedValuesDestination}",
698698
(not null, not null) => string.Empty
699699
};
700700
context.ReportDiagnostic(
@@ -736,18 +736,18 @@ public void AnalyzeMarshallerType(SymbolAnalysisContext context)
736736
marshallerType.ToDisplayString()));
737737
}
738738

739-
if (marshallerData.Features.HasFlag(CustomTypeMarshallerFeatures.UnmanagedResources) && !ManualTypeMarshallingHelper.HasFreeNativeMethod(marshallerType))
739+
if (marshallerData.Features.HasFlag(CustomTypeMarshallerFeatures.UnmanagedResources) && !ManualTypeMarshallingHelper_V1.HasFreeNativeMethod(marshallerType))
740740
{
741741
context.ReportDiagnostic(
742742
marshallerType.CreateDiagnostic(
743743
UnmanagedResourcesRequiresFreeNativeRule,
744744
ImmutableDictionary<string, string>.Empty.Add(
745745
MissingMemberNames.Key,
746-
ShapeMemberNames.Value.FreeNative),
746+
ShapeMemberNames_V1.Value.FreeNative),
747747
marshallerType.ToDisplayString(),
748748
type.ToDisplayString()));
749749
}
750-
else if (!marshallerData.Features.HasFlag(CustomTypeMarshallerFeatures.UnmanagedResources) && ManualTypeMarshallingHelper.HasFreeNativeMethod(marshallerType))
750+
else if (!marshallerData.Features.HasFlag(CustomTypeMarshallerFeatures.UnmanagedResources) && ManualTypeMarshallingHelper_V1.HasFreeNativeMethod(marshallerType))
751751
{
752752
context.ReportDiagnostic(
753753
marshallerType.CreateDiagnostic(
@@ -758,8 +758,8 @@ public void AnalyzeMarshallerType(SymbolAnalysisContext context)
758758
marshallerType.ToDisplayString()));
759759
}
760760

761-
IMethodSymbol? toNativeValueMethod = ManualTypeMarshallingHelper.FindToNativeValueMethod(marshallerType);
762-
IMethodSymbol? fromNativeValueMethod = ManualTypeMarshallingHelper.FindFromNativeValueMethod(marshallerType);
761+
IMethodSymbol? toNativeValueMethod = ManualTypeMarshallingHelper_V1.FindToNativeValueMethod(marshallerType);
762+
IMethodSymbol? fromNativeValueMethod = ManualTypeMarshallingHelper_V1.FindFromNativeValueMethod(marshallerType);
763763
bool toNativeValueMethodIsRefReturn = toNativeValueMethod is { ReturnsByRef: true } or { ReturnsByRefReadonly: true };
764764
ITypeSymbol nativeType = marshallerType;
765765

@@ -771,7 +771,7 @@ public void AnalyzeMarshallerType(SymbolAnalysisContext context)
771771
InTwoStageMarshallingRequiresToNativeValueRule,
772772
ImmutableDictionary<string, string>.Empty.Add(
773773
MissingMemberNames.Key,
774-
ShapeMemberNames.Value.ToNativeValue),
774+
ShapeMemberNames_V1.Value.ToNativeValue),
775775
marshallerType.ToDisplayString()));
776776
}
777777
if (marshallerData.Direction.HasFlag(CustomTypeMarshallerDirection.Out) && fromNativeValueMethod is null)
@@ -780,7 +780,7 @@ public void AnalyzeMarshallerType(SymbolAnalysisContext context)
780780
OutTwoStageMarshallingRequiresFromNativeValueRule,
781781
ImmutableDictionary<string, string>.Empty.Add(
782782
MissingMemberNames.Key,
783-
ShapeMemberNames.Value.FromNativeValue),
783+
ShapeMemberNames_V1.Value.FromNativeValue),
784784
marshallerType.ToDisplayString()));
785785
}
786786

src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/CustomTypeMarshallerFixer.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ private static SyntaxNode AddMissingFeatures(string attributeName, AttributeData
214214

215215
SyntaxNode featureAttributeArgument = gen.AttributeArgument("Features",
216216
gen.GetEnumValueAsFlagsExpression(
217-
customTypeMarshallerAttribute.AttributeClass.GetMembers(ManualTypeMarshallingHelper.CustomMarshallerAttributeFields.Features).OfType<IPropertySymbol>().First().Type,
217+
customTypeMarshallerAttribute.AttributeClass.GetMembers(ManualTypeMarshallingHelper_V1.CustomMarshallerAttributeFields.Features).OfType<IPropertySymbol>().First().Type,
218218
(int)newFeaturesValue,
219219
includeZeroValueFlags: false));
220220

@@ -251,12 +251,12 @@ private static SyntaxNode AddMissingMembers(SyntaxNode node, ITypeSymbol
251251
SyntaxNode updatedDeclaration = node;
252252

253253

254-
(_, ITypeSymbol managedType, _) = ManualTypeMarshallingHelper.GetMarshallerShapeInfo(marshallerType);
254+
(_, ITypeSymbol managedType, _) = ManualTypeMarshallingHelper_V1.GetMarshallerShapeInfo(marshallerType);
255255

256-
IMethodSymbol? fromNativeValueMethod = ManualTypeMarshallingHelper.FindFromNativeValueMethod(marshallerType);
257-
IMethodSymbol? toNativeValueMethod = ManualTypeMarshallingHelper.FindToNativeValueMethod(marshallerType);
258-
IMethodSymbol? getManagedValuesSourceMethod = ManualTypeMarshallingHelper.FindGetManagedValuesSourceMethod(marshallerType, readOnlySpanOfT);
259-
IMethodSymbol? getManagedValuesDestinationMethod = ManualTypeMarshallingHelper.FindGetManagedValuesDestinationMethod(marshallerType, spanOfT);
256+
IMethodSymbol? fromNativeValueMethod = ManualTypeMarshallingHelper_V1.FindFromNativeValueMethod(marshallerType);
257+
IMethodSymbol? toNativeValueMethod = ManualTypeMarshallingHelper_V1.FindToNativeValueMethod(marshallerType);
258+
IMethodSymbol? getManagedValuesSourceMethod = ManualTypeMarshallingHelper_V1.FindGetManagedValuesSourceMethod(marshallerType, readOnlySpanOfT);
259+
IMethodSymbol? getManagedValuesDestinationMethod = ManualTypeMarshallingHelper_V1.FindGetManagedValuesDestinationMethod(marshallerType, spanOfT);
260260

261261
SyntaxNode[] throwNotImplementedStatements = new[]
262262
{
@@ -321,21 +321,21 @@ private static SyntaxNode AddMissingMembers(SyntaxNode node, ITypeSymbol
321321
accessibility: Accessibility.Public,
322322
statements: throwNotImplementedStatements));
323323
break;
324-
case ShapeMemberNames.Value.ToManaged:
324+
case ShapeMemberNames_V1.Value.ToManaged:
325325
updatedDeclaration = gen.AddMembers(updatedDeclaration, gen.MethodDeclaration(
326-
ShapeMemberNames.Value.ToManaged,
326+
ShapeMemberNames_V1.Value.ToManaged,
327327
returnType: gen.TypeExpression(managedType),
328328
accessibility: Accessibility.Public,
329329
statements: throwNotImplementedStatements));
330330
break;
331-
case ShapeMemberNames.Value.FreeNative:
332-
updatedDeclaration = gen.AddMembers(updatedDeclaration, gen.MethodDeclaration(ShapeMemberNames.Value.FreeNative,
331+
case ShapeMemberNames_V1.Value.FreeNative:
332+
updatedDeclaration = gen.AddMembers(updatedDeclaration, gen.MethodDeclaration(ShapeMemberNames_V1.Value.FreeNative,
333333
accessibility: Accessibility.Public,
334334
statements: throwNotImplementedStatements));
335335
break;
336-
case ShapeMemberNames.Value.FromNativeValue:
336+
case ShapeMemberNames_V1.Value.FromNativeValue:
337337
updatedDeclaration = gen.AddMembers(updatedDeclaration, gen.MethodDeclaration(
338-
ShapeMemberNames.Value.FromNativeValue,
338+
ShapeMemberNames_V1.Value.FromNativeValue,
339339
parameters: new[]
340340
{
341341
gen.ParameterDeclaration("value",
@@ -344,33 +344,33 @@ private static SyntaxNode AddMissingMembers(SyntaxNode node, ITypeSymbol
344344
accessibility: Accessibility.Public,
345345
statements: throwNotImplementedStatements));
346346
break;
347-
case ShapeMemberNames.Value.ToNativeValue:
347+
case ShapeMemberNames_V1.Value.ToNativeValue:
348348
updatedDeclaration = gen.AddMembers(updatedDeclaration, gen.MethodDeclaration(
349-
ShapeMemberNames.Value.ToNativeValue,
349+
ShapeMemberNames_V1.Value.ToNativeValue,
350350
returnType: gen.TypeExpression(fromNativeValueMethod?.Parameters[0].Type ?? @byte),
351351
accessibility: Accessibility.Public,
352352
statements: throwNotImplementedStatements));
353353
break;
354-
case ShapeMemberNames.LinearCollection.GetManagedValuesSource:
354+
case ShapeMemberNames_V1.LinearCollection.GetManagedValuesSource:
355355
INamedTypeSymbol? getManagedValuesDestinationReturnType = (INamedTypeSymbol?)getManagedValuesDestinationMethod?.ReturnType;
356356
updatedDeclaration = gen.AddMembers(updatedDeclaration, gen.MethodDeclaration(
357-
ShapeMemberNames.LinearCollection.GetManagedValuesSource,
357+
ShapeMemberNames_V1.LinearCollection.GetManagedValuesSource,
358358
returnType: gen.TypeExpression(
359359
readOnlySpanOfT.Construct(
360360
getManagedValuesDestinationReturnType?.TypeArguments[0] ?? @object)),
361361
accessibility: Accessibility.Public,
362362
statements: throwNotImplementedStatements));
363363
break;
364-
case ShapeMemberNames.LinearCollection.GetNativeValuesDestination:
364+
case ShapeMemberNames_V1.LinearCollection.GetNativeValuesDestination:
365365
updatedDeclaration = gen.AddMembers(updatedDeclaration, gen.MethodDeclaration(
366-
ShapeMemberNames.LinearCollection.GetNativeValuesDestination,
366+
ShapeMemberNames_V1.LinearCollection.GetNativeValuesDestination,
367367
returnType: gen.TypeExpression(spanOfByte),
368368
accessibility: Accessibility.Public,
369369
statements: throwNotImplementedStatements));
370370
break;
371-
case ShapeMemberNames.LinearCollection.GetNativeValuesSource:
371+
case ShapeMemberNames_V1.LinearCollection.GetNativeValuesSource:
372372
updatedDeclaration = gen.AddMembers(updatedDeclaration, gen.MethodDeclaration(
373-
ShapeMemberNames.LinearCollection.GetNativeValuesSource,
373+
ShapeMemberNames_V1.LinearCollection.GetNativeValuesSource,
374374
parameters: new[]
375375
{
376376
gen.ParameterDeclaration("numElements", type: gen.TypeExpression(int32))
@@ -379,10 +379,10 @@ private static SyntaxNode AddMissingMembers(SyntaxNode node, ITypeSymbol
379379
accessibility: Accessibility.Public,
380380
statements: throwNotImplementedStatements));
381381
break;
382-
case ShapeMemberNames.LinearCollection.GetManagedValuesDestination:
382+
case ShapeMemberNames_V1.LinearCollection.GetManagedValuesDestination:
383383
INamedTypeSymbol? getManagedValuesSourceReturnType = (INamedTypeSymbol?)getManagedValuesSourceMethod?.ReturnType;
384384
updatedDeclaration = gen.AddMembers(updatedDeclaration, gen.MethodDeclaration(
385-
ShapeMemberNames.LinearCollection.GetNativeValuesDestination,
385+
ShapeMemberNames_V1.LinearCollection.GetNativeValuesDestination,
386386
parameters: new[]
387387
{
388388
gen.ParameterDeclaration("numElements", type: gen.TypeExpression(int32))

0 commit comments

Comments
 (0)