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

Prefix type names with global and use static TypeSyntax and NameSyntax instead of Parse(Type)Name #90339

Merged
merged 5 commits into from
Aug 11, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public override void Initialize(AnalysisContext context)
context.Compilation,
targetFramework.TargetFramework,
targetFramework.Version,
context.Compilation.SourceModule.GetAttributes().Any(attr => attr.AttributeClass.ToDisplayString() == TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute_Metadata));
context.Compilation.SourceModule.GetAttributes().Any(attr => attr.AttributeClass.ToDisplayString() == TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute));

context.RegisterSymbolAction(context =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,11 @@ private static ClassDeclarationSyntax GenerateClassInfoType(ImmutableArray<strin
Token(SyntaxKind.FileKeyword),
Token(SyntaxKind.SealedKeyword),
Token(SyntaxKind.UnsafeKeyword))
.AddBaseListTypes(SimpleBaseType(ParseTypeName(TypeNames.IComExposedClass)))
.AddBaseListTypes(SimpleBaseType(TypeSyntaxes.IComExposedClass))
.AddMembers(
FieldDeclaration(
VariableDeclaration(
PointerType(
ParseTypeName(TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry)),
PointerType(TypeSyntaxes.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry),
SingletonSeparatedList(VariableDeclarator(vtablesField))))
.AddModifiers(
Token(SyntaxKind.PrivateKeyword),
Expand All @@ -140,31 +139,29 @@ private static ClassDeclarationSyntax GenerateClassInfoType(ImmutableArray<strin
// ComInterfaceEntry* vtables = (ComInterfaceEntry*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(<ClassInfoTypeName>), sizeof(ComInterfaceEntry) * <numInterfaces>);
LocalDeclarationStatement(
VariableDeclaration(
PointerType(
ParseTypeName(TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry)),
PointerType(TypeSyntaxes.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry),
SingletonSeparatedList(
VariableDeclarator(vtablesLocal)
.WithInitializer(EqualsValueClause(
CastExpression(
PointerType(
ParseTypeName(TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry)),
PointerType(TypeSyntaxes.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry),
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
ParseTypeName(TypeNames.System_Runtime_CompilerServices_RuntimeHelpers),
TypeSyntaxes.System_Runtime_CompilerServices_RuntimeHelpers,
IdentifierName("AllocateTypeAssociatedMemory")))
.AddArgumentListArguments(
Argument(TypeOfExpression(IdentifierName(ClassInfoTypeName))),
Argument(
BinaryExpression(
SyntaxKind.MultiplyExpression,
SizeOfExpression(ParseTypeName(TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry)),
SizeOfExpression(TypeSyntaxes.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry),
LiteralExpression(
SyntaxKind.NumericLiteralExpression,
Literal(implementedInterfaces.Length))))))))))),
// IIUnknownDerivedDetails details;
LocalDeclarationStatement(
VariableDeclaration(
ParseTypeName(TypeNames.IIUnknownDerivedDetails),
TypeSyntaxes.IIUnknownDerivedDetails,
SingletonSeparatedList(
VariableDeclarator(detailsTempLocal))))
};
Expand All @@ -180,7 +177,7 @@ private static ClassDeclarationSyntax GenerateClassInfoType(ImmutableArray<strin
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
ParseTypeName(TypeNames.StrategyBasedComWrappers),
TypeSyntaxes.StrategyBasedComWrappers,
IdentifierName("DefaultIUnknownInterfaceDetailsStrategy")),
IdentifierName("GetIUnknownDerivedDetails")),
ArgumentList(
Expand Down Expand Up @@ -253,7 +250,7 @@ private static ClassDeclarationSyntax GenerateClassInfoType(ImmutableArray<strin
// { body }
MethodDeclaration(
PointerType(
ParseTypeName(TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry)),
TypeSyntaxes.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry),
"GetComInterfaceEntries")
.AddParameterListParameters(
Parameter(Identifier(countIdentifier))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ private static InterfaceDeclarationSyntax GenerateImplementationInterface(ComInt
.Select(ctx => ctx.Stub.Node)
.Concat(shadowImplementations)
.Concat(inheritedStubs)))
.AddAttributeLists(AttributeList(SingletonSeparatedList(Attribute(ParseName(TypeNames.System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute)))));
.AddAttributeLists(AttributeList(SingletonSeparatedList(Attribute(NameSyntaxes.System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute))));
}

private static InterfaceDeclarationSyntax GenerateImplementationVTableMethods(ComInterfaceAndMethodsContext comInterfaceAndMethods, CancellationToken _)
Expand Down Expand Up @@ -496,7 +496,7 @@ private static InterfaceDeclarationSyntax GenerateImplementationVTable(ComInterf
CastExpression(VoidStarStarSyntax,
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
ParseTypeName(TypeNames.System_Runtime_CompilerServices_RuntimeHelpers),
TypeSyntaxes.System_Runtime_CompilerServices_RuntimeHelpers,
IdentifierName("AllocateTypeAssociatedMemory")))
.AddArgumentListArguments(
Argument(TypeOfExpression(interfaceType.Syntax)),
Expand Down Expand Up @@ -525,7 +525,7 @@ private static InterfaceDeclarationSyntax GenerateImplementationVTable(ComInterf
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
ParseTypeName(TypeNames.System_Runtime_InteropServices_ComWrappers),
TypeSyntaxes.System_Runtime_InteropServices_ComWrappers,
IdentifierName("GetIUnknownImpl")))
.AddArgumentListArguments(
Argument(IdentifierName("v0"))
Expand Down Expand Up @@ -586,7 +586,7 @@ private static InterfaceDeclarationSyntax GenerateImplementationVTable(ComInterf
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
ParseTypeName(TypeNames.System_Runtime_InteropServices_NativeMemory),
TypeSyntaxes.System_Runtime_InteropServices_NativeMemory,
IdentifierName("Copy")))
.WithArgumentList(
ArgumentList(
Expand All @@ -601,7 +601,7 @@ private static InterfaceDeclarationSyntax GenerateImplementationVTable(ComInterf
SyntaxKind.SimpleMemberAccessExpression,
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
ParseTypeName(TypeNames.StrategyBasedComWrappers),
TypeSyntaxes.StrategyBasedComWrappers,
IdentifierName("DefaultIUnknownInterfaceDetailsStrategy")),
IdentifierName("GetIUnknownDerivedDetails")))
.WithArgumentList(
Expand Down Expand Up @@ -643,14 +643,14 @@ private static InterfaceDeclarationSyntax GenerateImplementationVTable(ComInterf
private static readonly ClassDeclarationSyntax InterfaceInformationTypeTemplate =
ClassDeclaration("InterfaceInformation")
.AddModifiers(Token(SyntaxKind.FileKeyword), Token(SyntaxKind.UnsafeKeyword))
.AddBaseListTypes(SimpleBaseType(ParseTypeName(TypeNames.IIUnknownInterfaceType)));
.AddBaseListTypes(SimpleBaseType(TypeSyntaxes.IIUnknownInterfaceType));

private static ClassDeclarationSyntax GenerateInterfaceInformation(ComInterfaceInfo context, CancellationToken _)
{
ClassDeclarationSyntax interfaceInformationType = InterfaceInformationTypeTemplate
.AddMembers(
// public static System.Guid Iid { get; } = new(<embeddedDataBlob>);
PropertyDeclaration(ParseTypeName(TypeNames.System_Guid), "Iid")
PropertyDeclaration(TypeSyntaxes.System_Guid, "Iid")
.AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword))
.AddAccessorListAccessors(
AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(Token(SyntaxKind.SemicolonToken)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private MethodDeclarationSyntax CreateUnreachableExceptionStub()
.WithExpressionBody(ArrowExpressionClause(
ThrowExpression(
ObjectCreationExpression(
ParseTypeName(TypeNames.UnreachableException))
TypeSyntaxes.UnreachableException)
.WithArgumentList(ArgumentList()))));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public BlockSyntax GenerateStubBody(int index, ImmutableArray<FunctionPointerUnm
SyntaxKind.SimpleMemberAccessExpression,
ParenthesizedExpression(
CastExpression(
ParseTypeName(TypeNames.IUnmanagedVirtualMethodTableProvider),
TypeSyntaxes.IUnmanagedVirtualMethodTableProvider,
ThisExpression())),
IdentifierName("GetVirtualMethodTableInfoForKey") ))
.WithArgumentList(
Expand Down Expand Up @@ -189,7 +189,7 @@ public BlockSyntax GenerateStubBody(int index, ImmutableArray<FunctionPointerUnm
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
ParseTypeName(TypeNames.System_GC),
TypeSyntaxes.System_GC,
IdentifierName("KeepAlive")),
ArgumentList(SingletonSeparatedList(Argument(ThisExpression()))))));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private sealed class Marshaller : IMarshallingGenerator
{
public ManagedTypeInfo AsNativeType(TypePositionInfo info) =>
new PointerTypeInfo(
$"{TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch}*",
$"global::{TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch}*",
$"{TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch}*",
IsFunctionPointer: false);
public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeContext context)
Expand All @@ -47,7 +47,7 @@ public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeCont
IdentifierName(managed),
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
ParseName(TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch),
TypeSyntaxes.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch,
GenericName(
Identifier("GetInstance"),
TypeArgumentList(SingletonSeparatedList(info.ManagedType.Syntax)))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeCont
yield return ExpressionStatement(
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
ParseName(TypeNames.System_Runtime_InteropServices_Marshal),
TypeSyntaxes.System_Runtime_InteropServices_Marshal,
IdentifierName("ThrowExceptionForHR")),
ArgumentList(
SingletonSeparatedList(Argument(IdentifierName(managedIdentifier))))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeCont
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
ParseTypeName(TypeNames.UnmanagedObjectUnwrapper),
TypeSyntaxes.UnmanagedObjectUnwrapper,
GenericName(Identifier("GetObjectForUnmanagedWrapper"))
.WithTypeArgumentList(
TypeArgumentList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static (MethodDeclarationSyntax, ImmutableArray<DiagnosticInfo>) Generate
(ParameterListSyntax unmanagedParameterList, TypeSyntax returnType, _) = stubGenerator.GenerateAbiMethodSignatureData();

AttributeSyntax unmanagedCallersOnlyAttribute = Attribute(
ParseName(TypeNames.UnmanagedCallersOnlyAttribute));
NameSyntaxes.UnmanagedCallersOnlyAttribute);

if (methodStub.CallingConvention.Array.Length != 0)
{
Expand All @@ -94,7 +94,7 @@ public static (MethodDeclarationSyntax, ImmutableArray<DiagnosticInfo>) Generate
ImplicitArrayCreationExpression(
InitializerExpression(SyntaxKind.CollectionInitializerExpression,
SeparatedList<ExpressionSyntax>(
methodStub.CallingConvention.Array.Select(callConv => TypeOfExpression(ParseName($"System.Runtime.CompilerServices.CallConv{callConv.Name.ValueText}")))))))
methodStub.CallingConvention.Array.Select(callConv => TypeOfExpression(ParseName($"global::System.Runtime.CompilerServices.CallConv{callConv.Name.ValueText}")))))))
jtschuster marked this conversation as resolved.
Show resolved Hide resolved
.WithNameEquals(NameEquals(IdentifierName("CallConvs"))));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,6 @@ private static (MemberDeclarationSyntax, ImmutableArray<DiagnosticInfo>) Generat

// Verify there is an [UnmanagedObjectUnwrapperAttribute<TMapper>]
if (!method.ContainingType.GetAttributes().Any(att => att.AttributeClass.IsOfType(TypeNames.UnmanagedObjectUnwrapperAttribute)))
//!method.ContainingType.GetAttributes().Any(att =>
//att.AttributeClass.MetadataName == TypeNames.UnmanagedObjectUnwrapperAttribute.Substring(TypeNames.UnmanagedObjectUnwrapperAttribute.LastIndexOf('.') + 1)
//&& att.AttributeClass.OriginalDefinition.ToDisplayString().Substring(0, att.AttributeClass.OriginalDefinition.ToDisplayString().LastIndexOf(att.AttributeClass.ToDisplayString())) == TypeNames.UnmanagedObjectUnwrapperAttribute.Substring(0, TypeNames.UnmanagedObjectUnwrapperAttribute.LastIndexOf('.'))))
{
return Diagnostic.Create(GeneratorDiagnostics.InvalidAttributedMethodContainingTypeMissingUnmanagedObjectUnwrapperAttribute, methodSyntax.Identifier.GetLocation(), method.Name);
}
Expand All @@ -429,7 +426,7 @@ private static MemberDeclarationSyntax GenerateNativeInterfaceMetadata(Containin
InterfaceDeclaration("Native")
.WithModifiers(TokenList(Token(SyntaxKind.InternalKeyword), Token(SyntaxKind.PartialKeyword)))
.WithBaseList(BaseList(SingletonSeparatedList((BaseTypeSyntax)SimpleBaseType(IdentifierName(context.ContainingSyntax[0].Identifier)))))
.AddAttributeLists(AttributeList(SingletonSeparatedList(Attribute(ParseName(TypeNames.System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute))))));
.AddAttributeLists(AttributeList(SingletonSeparatedList(Attribute(NameSyntaxes.System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute)))));
}

private static MemberDeclarationSyntax GeneratePopulateVTableMethod(IGrouping<ContainingSyntaxContext, IncrementalMethodStubGenerationContext> vtableMethods)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public override void Initialize(AnalysisContext context)
context.Compilation,
targetFramework.TargetFramework,
targetFramework.Version,
context.Compilation.SourceModule.GetAttributes().Any(attr => attr.AttributeClass.ToDisplayString() == TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute_Metadata));
context.Compilation.SourceModule.GetAttributes().Any(attr => attr.AttributeClass.ToDisplayString() == TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute));

context.RegisterSymbolAction(symbolContext => AnalyzeSymbol(symbolContext, libraryImportAttrType, env), SymbolKind.Method);
});
Expand Down
Loading