Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the scoped keyword in LibraryImportGenerator #73479

Merged
merged 2 commits into from
Aug 24, 2022
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
12 changes: 6 additions & 6 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@
<MicrosoftCodeAnalysisVersion_3_11>3.11.0</MicrosoftCodeAnalysisVersion_3_11>
<!-- Compatibility with VS 17.0/.NET SDK 6.0.1xx -->
<MicrosoftCodeAnalysisVersion_4_0>4.0.1</MicrosoftCodeAnalysisVersion_4_0>
<!-- Compatibility with VS 17.X/.NET SDK 7.0.1xx -->
<!-- Compatibility with VS 17.4/.NET SDK 7.0.1xx -->
<!--
This version is a moving target until we ship.
The exact version is a moving target until we ship.
It should never go ahead of the Roslyn version included in the SDK version in dotnet/arcade's global.json to avoid causing breaks in product construction.
-->
<MicrosoftCodeAnalysisVersion_4_X>4.3.0-2.final</MicrosoftCodeAnalysisVersion_4_X>
<MicrosoftCodeAnalysisVersion_4_4>4.4.0-1.22369.1</MicrosoftCodeAnalysisVersion_4_4>
</PropertyGroup>
<PropertyGroup>
<!-- Code analysis dependencies -->
<MicrosoftCodeAnalysisAnalyzersVersion>3.3.3</MicrosoftCodeAnalysisAnalyzersVersion>
<MicrosoftCodeAnalysisCSharpCodeStyleVersion>4.3.0-2.final</MicrosoftCodeAnalysisCSharpCodeStyleVersion>
<MicrosoftCodeAnalysisCSharpWorkspacesVersion>4.3.0-2.final</MicrosoftCodeAnalysisCSharpWorkspacesVersion>
<MicrosoftCodeAnalysisCSharpVersion>4.3.0-2.final</MicrosoftCodeAnalysisCSharpVersion>
<MicrosoftCodeAnalysisCSharpWorkspacesVersion>4.4.0-1.final</MicrosoftCodeAnalysisCSharpWorkspacesVersion>
<MicrosoftCodeAnalysisCSharpVersion>4.4.0-1.final</MicrosoftCodeAnalysisCSharpVersion>
<MicrosoftCodeAnalysisNetAnalyzersVersion>7.0.0-preview1.22403.2</MicrosoftCodeAnalysisNetAnalyzersVersion>
<MicrosoftCodeAnalysisVersion>4.3.0-2.final</MicrosoftCodeAnalysisVersion>
<MicrosoftCodeAnalysisVersion>4.4.0-1.final</MicrosoftCodeAnalysisVersion>
<!--
TODO: Remove pinned version once arcade supplies a compiler that enables the repo to compile.
-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.DotnetRuntime.Extensions;
using GeneratorAttributeSyntaxContext = Microsoft.CodeAnalysis.DotnetRuntime.Extensions.GeneratorAttributeSyntaxContext;

namespace Generators
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisVersion_4_X)" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisVersion_4_4)" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="$(MicrosoftCodeAnalysisAnalyzersVersion)" PrivateAssets="all" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisVersion_4_X)" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisVersion_4_4)" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="$(MicrosoftCodeAnalysisAnalyzersVersion)" PrivateAssets="all" />
</ItemGroup>

Expand All @@ -45,9 +45,9 @@
<Compile Include="$(CommonPath)Roslyn\SyntaxValueProvider.ImmutableArrayValueComparer.cs" Link="Common\Roslyn\SyntaxValueProvider.ImmutableArrayValueComparer.cs" />
<Compile Include="$(CommonPath)Roslyn\SyntaxValueProvider_ForAttributeWithMetadataName.cs" Link="Common\Roslyn\SyntaxValueProvider_ForAttributeWithMetadataName.cs" />
<Compile Include="$(CommonPath)Roslyn\SyntaxValueProvider_ForAttributeWithSimpleName.cs" Link="Common\Roslyn\SyntaxValueProvider_ForAttributeWithSimpleName.cs" />

<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ValueListBuilder.cs" Link="Production\ValueListBuilder.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ValueListBuilder.Pop.cs" Link="Production\ValueListBuilder.Pop.cs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ namespace Microsoft.Interop
/// </summary>
public abstract record ManagedTypeInfo(string FullTypeName, string DiagnosticFormattedName)
{
public TypeSyntax Syntax { get; } = SyntaxFactory.ParseTypeName(FullTypeName);
private TypeSyntax? _syntax;
public TypeSyntax Syntax => _syntax ??= SyntaxFactory.ParseTypeName(FullTypeName);

protected ManagedTypeInfo(ManagedTypeInfo original)
{
FullTypeName = original.FullTypeName;
DiagnosticFormattedName = original.DiagnosticFormattedName;
// Explicitly don't initialize _syntax here. We want Syntax to be recalculated
// from the results of a with-expression, which assigns the new property values
// to the result of this constructor.
}

public static ManagedTypeInfo CreateTypeInfoForTypeSymbol(ITypeSymbol type)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ private IMarshallingGenerator CreateCustomNativeTypeMarshaller(TypePositionInfo
ICustomTypeMarshallingStrategy marshallingStrategy;
if (marshallerData.HasState)
{
marshallingStrategy = new StatefulValueMarshalling(marshallerData.MarshallerType.Syntax, marshallerData.NativeType.Syntax, marshallerData.Shape);
marshallingStrategy = new StatefulValueMarshalling(marshallerData.MarshallerType, marshallerData.NativeType.Syntax, marshallerData.Shape);
if (marshallerData.Shape.HasFlag(MarshallerShape.CallerAllocatedBuffer))
marshallingStrategy = new StatefulCallerAllocatedBufferMarshalling(marshallingStrategy, marshallerData.MarshallerType.Syntax, marshallerData.BufferElementType.Syntax);
}
Expand Down Expand Up @@ -283,16 +283,21 @@ private IMarshallingGenerator CreateNativeCollectionMarshaller(

// Insert the unmanaged element type into the marshaller type
TypeSyntax unmanagedElementType = elementMarshaller.AsNativeType(elementInfo).GetCompatibleGenericTypeParameterSyntax();
TypeSyntax marshallerTypeSyntax = marshallerData.MarshallerType.Syntax;
marshallerTypeSyntax = ReplacePlaceholderSyntaxWithUnmanagedTypeSyntax(marshallerTypeSyntax, marshalInfo, unmanagedElementType);
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);

ICustomTypeMarshallingStrategy marshallingStrategy;
bool elementIsBlittable = elementMarshaller is BlittableMarshaller;

if (marshallerData.HasState)
{
marshallingStrategy = new StatefulValueMarshalling(marshallerTypeSyntax, nativeTypeSyntax, marshallerData.Shape);
marshallingStrategy = new StatefulValueMarshalling(marshallerType, nativeTypeSyntax, marshallerData.Shape);
if (marshallerData.Shape.HasFlag(MarshallerShape.CallerAllocatedBuffer))
{
// Check if the buffer element type is actually the unmanaged element type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ namespace Microsoft.Interop
internal sealed class StatefulValueMarshalling : ICustomTypeMarshallingStrategy
{
internal const string MarshallerIdentifier = "marshaller";
private readonly TypeSyntax _marshallerTypeSyntax;
private readonly ManagedTypeInfo _marshallerType;
private readonly TypeSyntax _nativeTypeSyntax;
private readonly MarshallerShape _shape;

public StatefulValueMarshalling(TypeSyntax marshallerTypeSyntax, TypeSyntax nativeTypeSyntax, MarshallerShape shape)
public StatefulValueMarshalling(ManagedTypeInfo marshallerType, TypeSyntax nativeTypeSyntax, MarshallerShape shape)
{
_marshallerTypeSyntax = marshallerTypeSyntax;
_marshallerType = marshallerType;
_nativeTypeSyntax = nativeTypeSyntax;
_shape = shape;
}
Expand Down Expand Up @@ -140,10 +140,23 @@ public IEnumerable<StatementSyntax> GenerateUnmarshalCaptureStatements(TypePosit
public IEnumerable<StatementSyntax> GenerateSetupStatements(TypePositionInfo info, StubCodeContext context)
{
// <marshaller> = new();
yield return MarshallerHelpers.Declare(
_marshallerTypeSyntax,
LocalDeclarationStatementSyntax declaration = MarshallerHelpers.Declare(
_marshallerType.Syntax,
context.GetAdditionalIdentifier(info, MarshallerIdentifier),
ImplicitObjectCreationExpression(ArgumentList(), initializer: null));

// For byref-like marshaller types, we'll mark them as scoped.
// Byref-like types can capture references, so by default the compiler has to worry that
// they could enable those references to escape the current stack frame.
// In particular, this can interact poorly with the caller-allocated-buffer marshalling
// support and make the simple `marshaller.FromManaged(managed, stackalloc X[i])` expression
// illegal. Mark the marshaller type as scoped so the compiler knows that it won't escape.
if (_marshallerType is ValueTypeInfo { IsByRefLike: true })
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved
{
declaration = declaration.AddModifiers(Token(SyntaxKind.ScopedKeyword));
}

yield return declaration;
}

public IEnumerable<StatementSyntax> GeneratePinStatements(TypePositionInfo info, StubCodeContext context)
Expand Down Expand Up @@ -218,28 +231,9 @@ public IEnumerable<StatementSyntax> GenerateMarshalStatements(TypePositionInfo i

IEnumerable<StatementSyntax> GenerateCallerAllocatedBufferMarshalStatements()
{
// TODO: Update once we can consume the scoped keword. We should be able to simplify this once we get that API.
string stackPtrIdentifier = context.GetAdditionalIdentifier(info, "stackptr");
// <bufferElementType>* <managedIdentifier>__stackptr = stackalloc <bufferElementType>[<_bufferSize>];
yield return LocalDeclarationStatement(
VariableDeclaration(
PointerType(_bufferElementType),
SingletonSeparatedList(
VariableDeclarator(stackPtrIdentifier)
.WithInitializer(EqualsValueClause(
StackAllocArrayCreationExpression(
ArrayType(
_bufferElementType,
SingletonList(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
_marshallerType,
IdentifierName(ShapeMemberNames.BufferSize))
))))))))));


(string managedIdentifier, _) = context.GetIdentifiers(info);

// <marshaller>.FromManaged(<managedIdentifier>, new Span<bufferElementType>(<stackPtrIdentifier>, <marshallerType>.BufferSize));
// <marshaller>.FromManaged(<managedIdentifier>, stackalloc <bufferElementType>[<marshallerType>.BufferSize]);
yield return ExpressionStatement(
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
Expand All @@ -249,19 +243,13 @@ IEnumerable<StatementSyntax> GenerateCallerAllocatedBufferMarshalStatements()
new[]
{
Argument(IdentifierName(managedIdentifier)),
Argument(
ObjectCreationExpression(
GenericName(Identifier(TypeNames.System_Span),
TypeArgumentList(SingletonSeparatedList(
_bufferElementType))))
.WithArgumentList(
ArgumentList(SeparatedList(new ArgumentSyntax[]
{
Argument(IdentifierName(stackPtrIdentifier)),
Argument(MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
Argument(StackAllocArrayCreationExpression(
ArrayType(
_bufferElementType,
SingletonList(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
_marshallerType,
IdentifierName(ShapeMemberNames.BufferSize)))
}))))
IdentifierName(ShapeMemberNames.BufferSize))))))))
}))));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis" Version="$(MicrosoftCodeAnalysisVersion_4_X)" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="$(MicrosoftCodeAnalysisVersion_4_4)" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="$(MicrosoftCodeAnalysisAnalyzersVersion)" PrivateAssets="all" />
</ItemGroup>

Expand Down