Skip to content

Commit

Permalink
Prefix type names with global and use static TypeSyntax and NameSynta…
Browse files Browse the repository at this point in the history
…x instead of Parse(Type)Name (#90339)

* Prefix type names with global and use static TypeSyntax and NameSyntax instead of Parse(Type)Name
  • Loading branch information
jtschuster authored Aug 11, 2023
1 parent 13337d8 commit 05dfb7e
Show file tree
Hide file tree
Showing 33 changed files with 263 additions and 122 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
// 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.Composition;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
Expand Down
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 @@ -104,7 +104,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

private static readonly AttributeSyntax s_comExposedClassAttributeTemplate =
Attribute(
GenericName(TypeNames.ComExposedClassAttribute)
GenericName(TypeNames.GlobalAlias + TypeNames.ComExposedClassAttribute)
.AddTypeArgumentListArguments(
IdentifierName(ClassInfoTypeName)));
private static MemberDeclarationSyntax GenerateClassInfoAttributeOnUserType(ContainingSyntaxContext containingSyntaxContext, ContainingSyntax classSyntax) =>
Expand All @@ -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 @@ -193,13 +193,13 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

context.RegisterSourceOutput(filesToGenerate, (context, data) =>
{
context.AddSource(data.TypeName.Replace("global::", ""), data.Source);
context.AddSource(data.TypeName.Replace(TypeNames.GlobalAlias, ""), data.Source);
});
}

private static readonly AttributeSyntax s_iUnknownDerivedAttributeTemplate =
Attribute(
GenericName(TypeNames.IUnknownDerivedAttribute)
GenericName(TypeNames.GlobalAlias + TypeNames.IUnknownDerivedAttribute)
.AddTypeArgumentListArguments(
IdentifierName("InterfaceInformation"),
IdentifierName("InterfaceImplementation")));
Expand Down 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 @@ -123,7 +123,7 @@ private static bool StringMarshallingIsValid(
stringMarshallingDiagnostic = DiagnosticInfo.Create(
GeneratorDiagnostics.StringMarshallingCustomTypeNotAccessibleByGeneratedCode,
syntax.Identifier.GetLocation(),
attrInfo.StringMarshallingCustomType.FullTypeName.Replace("global::", ""),
attrInfo.StringMarshallingCustomType.FullTypeName.Replace(TypeNames.GlobalAlias, ""),
details);
return false;
}
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}*",
$"{TypeNames.GlobalAlias + 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(TypeSyntaxes.CallConv(callConv.Name.ValueText)))))))
.WithNameEquals(NameEquals(IdentifierName("CallConvs"))));
}

Expand Down Expand Up @@ -132,7 +132,7 @@ private static ImmutableArray<TypePositionInfo> AddImplicitElementInfos(Incremen
{
elements.Add(
new TypePositionInfo(
new ReferenceTypeInfo($"global::{TypeNames.System_Exception}", TypeNames.System_Exception),
new ReferenceTypeInfo(TypeNames.GlobalAlias + TypeNames.System_Exception, TypeNames.System_Exception),
methodStub.ExceptionMarshallingInfo)
{
InstanceIdentifier = "__exception",
Expand Down
Loading

0 comments on commit 05dfb7e

Please sign in to comment.