未验证 提交 05dfb7ed 编写于 作者: J Jackson Schuster 提交者: GitHub

Prefix type names with global and use static TypeSyntax and NameSyntax instead...

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

* Prefix type names with global and use static TypeSyntax and NameSyntax instead of Parse(Type)Name
上级 13337d8f
// 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;
......
......@@ -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 =>
{
......
......@@ -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) =>
......@@ -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),
......@@ -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))))
};
......@@ -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(
......@@ -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))
......
......@@ -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")));
......@@ -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 _)
......@@ -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)),
......@@ -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"))
......@@ -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(
......@@ -601,7 +601,7 @@ private static InterfaceDeclarationSyntax GenerateImplementationVTable(ComInterf
SyntaxKind.SimpleMemberAccessExpression,
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
ParseTypeName(TypeNames.StrategyBasedComWrappers),
TypeSyntaxes.StrategyBasedComWrappers,
IdentifierName("DefaultIUnknownInterfaceDetailsStrategy")),
IdentifierName("GetIUnknownDerivedDetails")))
.WithArgumentList(
......@@ -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)))
......
......@@ -123,7 +123,7 @@ private static bool IsInPartialContext(INamedTypeSymbol symbol, InterfaceDeclara
stringMarshallingDiagnostic = DiagnosticInfo.Create(
GeneratorDiagnostics.StringMarshallingCustomTypeNotAccessibleByGeneratedCode,
syntax.Identifier.GetLocation(),
attrInfo.StringMarshallingCustomType.FullTypeName.Replace("global::", ""),
attrInfo.StringMarshallingCustomType.FullTypeName.Replace(TypeNames.GlobalAlias, ""),
details);
return false;
}
......
......@@ -114,7 +114,7 @@ private MethodDeclarationSyntax CreateUnreachableExceptionStub()
.WithExpressionBody(ArrowExpressionClause(
ThrowExpression(
ObjectCreationExpression(
ParseTypeName(TypeNames.UnreachableException))
TypeSyntaxes.UnreachableException)
.WithArgumentList(ArgumentList()))));
}
......
......@@ -115,7 +115,7 @@ public BlockSyntax GenerateStubBody(int index, ImmutableArray<FunctionPointerUnm
SyntaxKind.SimpleMemberAccessExpression,
ParenthesizedExpression(
CastExpression(
ParseTypeName(TypeNames.IUnmanagedVirtualMethodTableProvider),
TypeSyntaxes.IUnmanagedVirtualMethodTableProvider,
ThisExpression())),
IdentifierName("GetVirtualMethodTableInfoForKey") ))
.WithArgumentList(
......@@ -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()))))));
......
......@@ -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)
......@@ -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)))),
......
......@@ -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))))));
......
......@@ -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(
......
......@@ -85,7 +85,7 @@ internal static class VirtualMethodPointerStubGenerator
(ParameterListSyntax unmanagedParameterList, TypeSyntax returnType, _) = stubGenerator.GenerateAbiMethodSignatureData();
AttributeSyntax unmanagedCallersOnlyAttribute = Attribute(
ParseName(TypeNames.UnmanagedCallersOnlyAttribute));
NameSyntaxes.UnmanagedCallersOnlyAttribute);
if (methodStub.CallingConvention.Array.Length != 0)
{
......@@ -94,7 +94,7 @@ internal static class VirtualMethodPointerStubGenerator
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"))));
}
......@@ -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",
......
......@@ -413,9 +413,6 @@ private static MarshallingInfo CreateExceptionMarshallingInfo(AttributeData virt
// 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);
}
......@@ -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)
......
// 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.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
......
......@@ -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);
});
......
......@@ -196,7 +196,7 @@ private static string AppendSuffix(string entryPoint, char? entryPointSuffix)
char? entryPointSuffix,
CancellationToken cancellationToken)
{
INamedTypeSymbol? dllImportAttrType = editor.SemanticModel.Compilation.GetBestTypeByMetadataName(typeof(DllImportAttribute).FullName);
INamedTypeSymbol? dllImportAttrType = editor.SemanticModel.Compilation.GetBestTypeByMetadataName(TypeNames.DllImportAttribute);
if (dllImportAttrType == null)
return methodSyntax;
......
......@@ -113,11 +113,11 @@ private static List<AttributeSyntax> GenerateSyntaxForForwardedAttributes(Attrib
if (suppressGCTransitionAttribute is not null)
{
attributes.Add(Attribute(ParseName(TypeNames.SuppressGCTransitionAttribute)));
attributes.Add(Attribute(NameSyntaxes.SuppressGCTransitionAttribute));
}
if (unmanagedCallConvAttribute is not null)
{
AttributeSyntax unmanagedCallConvSyntax = Attribute(ParseName(TypeNames.UnmanagedCallConvAttribute));
AttributeSyntax unmanagedCallConvSyntax = Attribute(NameSyntaxes.UnmanagedCallConvAttribute);
foreach (KeyValuePair<string, TypedConstant> arg in unmanagedCallConvAttribute.NamedArguments)
{
if (arg.Key == CallConvsField)
......@@ -129,7 +129,7 @@ private static List<AttributeSyntax> GenerateSyntaxForForwardedAttributes(Attrib
TypeOfExpression(((ITypeSymbol)callConv.Value!).AsTypeSyntax()));
}
ArrayTypeSyntax arrayOfSystemType = ArrayType(ParseTypeName(TypeNames.System_Type), SingletonList(ArrayRankSpecifier()));
ArrayTypeSyntax arrayOfSystemType = ArrayType(TypeSyntaxes.System_Type, SingletonList(ArrayRankSpecifier()));
unmanagedCallConvSyntax = unmanagedCallConvSyntax.AddArgumentListArguments(
AttributeArgument(
......@@ -143,9 +143,9 @@ private static List<AttributeSyntax> GenerateSyntaxForForwardedAttributes(Attrib
if (defaultDllImportSearchPathsAttribute is not null)
{
attributes.Add(
Attribute(ParseName(TypeNames.DefaultDllImportSearchPathsAttribute)).AddArgumentListArguments(
Attribute(NameSyntaxes.DefaultDllImportSearchPathsAttribute).AddArgumentListArguments(
AttributeArgument(
CastExpression(ParseTypeName(TypeNames.DllImportSearchPath),
CastExpression(TypeSyntaxes.DllImportSearchPath,
LiteralExpression(SyntaxKind.NumericLiteralExpression,
Literal((int)defaultDllImportSearchPathsAttribute.ConstructorArguments[0].Value!))))));
}
......@@ -424,26 +424,26 @@ private static MemberDeclarationSyntax PrintForwarderStub(ContainingSyntax userD
SingletonList(AttributeList(
SingletonSeparatedList(
Attribute(
ParseName(typeof(DllImportAttribute).FullName),
AttributeArgumentList(
SeparatedList(
new[]
{
AttributeArgument(LiteralExpression(
SyntaxKind.StringLiteralExpression,
Literal(libraryImportData.ModuleName))),
AttributeArgument(
NameEquals(nameof(DllImportAttribute.EntryPoint)),
null,
LiteralExpression(
SyntaxKind.StringLiteralExpression,
Literal(libraryImportData.EntryPoint ?? stubMethodName))),
AttributeArgument(
NameEquals(nameof(DllImportAttribute.ExactSpelling)),
null,
LiteralExpression(SyntaxKind.TrueLiteralExpression))
}
)))))))
NameSyntaxes.DllImportAttribute,
AttributeArgumentList(
SeparatedList(
new[]
{
AttributeArgument(LiteralExpression(
SyntaxKind.StringLiteralExpression,
Literal(libraryImportData.ModuleName))),
AttributeArgument(
NameEquals(nameof(DllImportAttribute.EntryPoint)),
null,
LiteralExpression(
SyntaxKind.StringLiteralExpression,
Literal(libraryImportData.EntryPoint ?? stubMethodName))),
AttributeArgument(
NameEquals(nameof(DllImportAttribute.ExactSpelling)),
null,
LiteralExpression(SyntaxKind.TrueLiteralExpression))
}
)))))))
.WithParameterList(parameterList);
if (returnTypeAttributes is not null)
{
......@@ -486,7 +486,7 @@ private static AttributeSyntax CreateForwarderDllImport(LibraryImportData target
// Create new attribute
return Attribute(
ParseName(typeof(DllImportAttribute).FullName),
NameSyntaxes.DllImportAttribute,
AttributeArgumentList(SeparatedList(newAttributeArgs)));
static ExpressionSyntax CreateBoolExpressionSyntax(bool trueOrFalse)
......
......@@ -168,7 +168,7 @@ private static ImmutableArray<CatchClauseSyntax> GenerateCatchClauseForManagedEx
managedExceptionMarshaller.TypeInfo, context with { CurrentStage = StubCodeContext.Stage.PinnedMarshal }));
return ImmutableArray.Create(
CatchClause(
CatchDeclaration(ParseTypeName(TypeNames.System_Exception), Identifier(managed)),
CatchDeclaration(TypeSyntaxes.System_Exception, Identifier(managed)),
filter: null,
Block(List(catchClauseBuilder))));
}
......
......@@ -21,7 +21,7 @@ public static IncrementalValueProvider<StubEnvironment> CreateStubEnvironmentPro
var isModuleSkipLocalsInit = context.SyntaxProvider
.ForAttributeWithMetadataName(
TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute_Metadata,
TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute,
(node, ct) => node is ICompilationUnitSyntax,
// If SkipLocalsInit is applied at the top level, it is either applied to the module
// or is invalid syntax. As a result, we just need to know if there's any top-level
......
......@@ -53,7 +53,7 @@ public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeCont
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
ParseName(TypeNames.System_Runtime_InteropServices_Marshal),
TypeSyntaxes.System_Runtime_InteropServices_Marshal,
IdentifierName("GetFunctionPointerForDelegate")),
ArgumentList(SingletonSeparatedList(Argument(IdentifierName(managedIdentifier))))),
LiteralExpression(SyntaxKind.DefaultLiteralExpression))));
......@@ -75,7 +75,7 @@ public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeCont
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
ParseName(TypeNames.System_Runtime_InteropServices_Marshal),
TypeSyntaxes.System_Runtime_InteropServices_Marshal,
GenericName(Identifier("GetDelegateForFunctionPointer"))
.WithTypeArgumentList(
TypeArgumentList(
......
......@@ -113,7 +113,7 @@ public override StatementSyntax GenerateUnmanagedToManagedByValueOutMarshalState
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
ParseName(TypeNames.System_Runtime_InteropServices_MemoryMarshal),
TypeSyntaxes.System_Runtime_InteropServices_MemoryMarshal,
IdentifierName("CreateSpan")),
ArgumentList(
SeparatedList(new[]
......@@ -121,7 +121,7 @@ public override StatementSyntax GenerateUnmanagedToManagedByValueOutMarshalState
Argument(
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
ParseName(TypeNames.System_Runtime_InteropServices_MemoryMarshal),
TypeSyntaxes.System_Runtime_InteropServices_MemoryMarshal,
IdentifierName("GetReference")),
ArgumentList(SingletonSeparatedList(
Argument(CollectionSource.GetUnmanagedValuesSource(info, context))))))
......@@ -167,7 +167,7 @@ public override StatementSyntax GenerateManagedToUnmanagedByValueOutUnmarshalSta
ExpressionSyntax destination = InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
ParseName(TypeNames.System_Runtime_InteropServices_MemoryMarshal),
TypeSyntaxes.System_Runtime_InteropServices_MemoryMarshal,
IdentifierName("CreateSpan")),
ArgumentList(
SeparatedList(new[]
......@@ -175,7 +175,7 @@ public override StatementSyntax GenerateManagedToUnmanagedByValueOutUnmarshalSta
Argument(
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
ParseName(TypeNames.System_Runtime_InteropServices_MemoryMarshal),
TypeSyntaxes.System_Runtime_InteropServices_MemoryMarshal,
IdentifierName("GetReference")),
ArgumentList(SingletonSeparatedList(
Argument(CollectionSource.GetManagedValuesSource(info, context))))))
......@@ -223,7 +223,7 @@ private ExpressionSyntax CastToManagedIfNecessary(ExpressionSyntax expression)
return InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
ParseTypeName(TypeNames.System_Runtime_InteropServices_MemoryMarshal),
TypeSyntaxes.System_Runtime_InteropServices_MemoryMarshal,
GenericName(
Identifier("Cast"),
TypeArgumentList(SeparatedList(
......@@ -369,7 +369,7 @@ public override StatementSyntax GenerateManagedToUnmanagedByValueOutUnmarshalSta
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
ParseName(TypeNames.System_Runtime_InteropServices_MemoryMarshal),
TypeSyntaxes.System_Runtime_InteropServices_MemoryMarshal,
IdentifierName("CreateSpan")))
.WithArgumentList(
ArgumentList(
......@@ -379,7 +379,7 @@ public override StatementSyntax GenerateManagedToUnmanagedByValueOutUnmarshalSta
Argument(
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
ParseName(TypeNames.System_Runtime_CompilerServices_Unsafe),
TypeSyntaxes.System_Runtime_CompilerServices_Unsafe,
IdentifierName("AsRef")),
ArgumentList(SingletonSeparatedList(
Argument(
......@@ -493,7 +493,7 @@ public override StatementSyntax GenerateUnmanagedToManagedByValueOutMarshalState
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
ParseName(TypeNames.System_Runtime_InteropServices_MemoryMarshal),
TypeSyntaxes.System_Runtime_InteropServices_MemoryMarshal,
IdentifierName("CreateSpan")))
.WithArgumentList(
ArgumentList(
......@@ -503,7 +503,7 @@ public override StatementSyntax GenerateUnmanagedToManagedByValueOutMarshalState
Argument(
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
ParseName(TypeNames.System_Runtime_CompilerServices_Unsafe),
TypeSyntaxes.System_Runtime_CompilerServices_Unsafe,
IdentifierName("AsRef")),
ArgumentList(SingletonSeparatedList(
Argument(
......
......@@ -12,7 +12,7 @@ namespace Microsoft.Interop
{
public static class MarshallerHelpers
{
public static readonly TypeSyntax SystemIntPtrType = ParseTypeName(TypeNames.System_IntPtr);
public static readonly TypeSyntax SystemIntPtrType = TypeSyntaxes.System_IntPtr;
public static ForStatementSyntax GetForLoop(ExpressionSyntax lengthExpression, string indexerIdentifier)
{
......@@ -96,7 +96,7 @@ public static TypeSyntax GetCompatibleGenericTypeParameterSyntax(this TypeSyntax
if (spanElementTypeSyntax is PointerTypeSyntax)
{
// Pointers cannot be passed to generics, so use IntPtr for this case.
spanElementTypeSyntax = SystemIntPtrType;
spanElementTypeSyntax = TypeSyntaxes.System_IntPtr;
}
return spanElementTypeSyntax;
}
......@@ -108,7 +108,7 @@ public static TypeSyntax GetCompatibleGenericTypeParameterSyntax(this TypeSyntax
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
ParseName(TypeNames.System_Runtime_InteropServices_Marshal),
TypeSyntaxes.System_Runtime_InteropServices_Marshal,
IdentifierName("SetLastSystemError")),
ArgumentList(SingletonSeparatedList(
Argument(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(errorCode)))))));
......@@ -122,7 +122,7 @@ public static TypeSyntax GetCompatibleGenericTypeParameterSyntax(this TypeSyntax
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
ParseName(TypeNames.System_Runtime_InteropServices_Marshal),
TypeSyntaxes.System_Runtime_InteropServices_Marshal,
IdentifierName("GetLastSystemError")))));
// Marshal.SetLastPInvokeError(<lastError>);
......@@ -131,7 +131,7 @@ public static TypeSyntax GetCompatibleGenericTypeParameterSyntax(this TypeSyntax
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
ParseName(TypeNames.System_Runtime_InteropServices_Marshal),
TypeSyntaxes.System_Runtime_InteropServices_Marshal,
IdentifierName("SetLastPInvokeError")),
ArgumentList(SingletonSeparatedList(
Argument(IdentifierName(lastErrorIdentifier))))));
......@@ -327,7 +327,7 @@ public static StatementSyntax SkipInitOrDefaultInit(TypePositionInfo info, StubC
return ExpressionStatement(
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
ParseName(TypeNames.System_Runtime_CompilerServices_Unsafe),
TypeSyntaxes.System_Runtime_CompilerServices_Unsafe,
IdentifierName("SkipInit")))
.WithArgumentList(
ArgumentList(SingletonSeparatedList(
......
......@@ -137,9 +137,9 @@ private static bool TryRehydrateMarshalAsAttribute(TypePositionInfo info, out At
// so explicitly do not resurface a [MarshalAs(UnmanagdType.CustomMarshaler)] attribute.
if (info.MarshallingAttributeInfo is MarshalAsInfo { UnmanagedType: not UnmanagedType.CustomMarshaler } marshalAs)
{
marshalAsAttribute = Attribute(ParseName(TypeNames.System_Runtime_InteropServices_MarshalAsAttribute))
marshalAsAttribute = Attribute(NameSyntaxes.System_Runtime_InteropServices_MarshalAsAttribute)
.WithArgumentList(AttributeArgumentList(SingletonSeparatedList(AttributeArgument(
CastExpression(ParseTypeName(TypeNames.System_Runtime_InteropServices_UnmanagedType),
CastExpression(TypeSyntaxes.System_Runtime_InteropServices_UnmanagedType,
LiteralExpression(SyntaxKind.NumericLiteralExpression,
Literal((int)marshalAs.UnmanagedType)))))));
return true;
......@@ -184,7 +184,7 @@ private static bool TryRehydrateMarshalAsAttribute(TypePositionInfo info, out At
List<AttributeArgumentSyntax> marshalAsArguments = new List<AttributeArgumentSyntax>
{
AttributeArgument(
CastExpression(ParseTypeName(TypeNames.System_Runtime_InteropServices_UnmanagedType),
CastExpression(TypeSyntaxes.System_Runtime_InteropServices_UnmanagedType,
LiteralExpression(SyntaxKind.NumericLiteralExpression,
Literal((int)UnmanagedType.LPArray))))
};
......@@ -213,12 +213,12 @@ private static bool TryRehydrateMarshalAsAttribute(TypePositionInfo info, out At
{
marshalAsArguments.Add(
AttributeArgument(NameEquals("ArraySubType"), null,
CastExpression(ParseTypeName(TypeNames.System_Runtime_InteropServices_UnmanagedType),
CastExpression(TypeSyntaxes.System_Runtime_InteropServices_UnmanagedType,
LiteralExpression(SyntaxKind.NumericLiteralExpression,
Literal((int)elementMarshalAs.UnmanagedType))))
);
}
marshalAsAttribute = Attribute(ParseName(TypeNames.System_Runtime_InteropServices_MarshalAsAttribute))
marshalAsAttribute = Attribute(NameSyntaxes.System_Runtime_InteropServices_MarshalAsAttribute)
.WithArgumentList(AttributeArgumentList(SeparatedList(marshalAsArguments)));
return true;
}
......
......@@ -35,7 +35,7 @@ public PointerNativeTypeAssignmentRewriter(string nativeIdentifier, PointerTypeS
{
return node.WithInitializer(
EqualsValueClause(
CastExpression(MarshallerHelpers.SystemIntPtrType, node.Initializer.Value)));
CastExpression(TypeSyntaxes.System_IntPtr, node.Initializer.Value)));
}
if (node.Initializer.Value.ToString() == _nativeIdentifier)
{
......@@ -52,7 +52,7 @@ public override SyntaxNode VisitAssignmentExpression(AssignmentExpressionSyntax
if (node.Left.ToString() == _nativeIdentifier)
{
return node.WithRight(
CastExpression(MarshallerHelpers.SystemIntPtrType, node.Right));
CastExpression(TypeSyntaxes.System_IntPtr, node.Right));
}
if (node.Right.ToString() == _nativeIdentifier)
{
......
......@@ -77,7 +77,7 @@ public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeCont
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
ParseTypeName(TypeNames.System_Activator),
TypeSyntaxes.System_Activator,
IdentifierName("CreateInstance")))
.WithArgumentList(
ArgumentList(
......@@ -168,7 +168,7 @@ public IEnumerable<StatementSyntax> Generate(TypePositionInfo info, StubCodeCont
StatementSyntax unmarshalStatement = ExpressionStatement(
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
ParseTypeName(TypeNames.System_Runtime_InteropServices_Marshal),
TypeSyntaxes.System_Runtime_InteropServices_Marshal,
IdentifierName("InitHandle")),
ArgumentList(SeparatedList(
new[]
......
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
namespace Microsoft.Interop
{
......@@ -155,7 +150,7 @@ internal static MarshallingInfo CreateSpecificMarshallingInfo(ManagedTypeInfo un
{
return unmanagedReturnType switch
{
SpecialTypeInfo(_, _, SpecialType.System_Void) => CreateWellKnownComExceptionMarshallingData($"{TypeNames.ExceptionAsVoidMarshaller}", unmanagedReturnType),
SpecialTypeInfo(_, _, SpecialType.System_Void) => CreateWellKnownComExceptionMarshallingData(TypeNames.ExceptionAsVoidMarshaller, unmanagedReturnType),
SpecialTypeInfo(_, _, SpecialType.System_Int32) => CreateWellKnownComExceptionMarshallingData($"{TypeNames.ExceptionAsHResultMarshaller}<int>", unmanagedReturnType),
SpecialTypeInfo(_, _, SpecialType.System_UInt32) => CreateWellKnownComExceptionMarshallingData($"{TypeNames.ExceptionAsHResultMarshaller}<uint>", unmanagedReturnType),
SpecialTypeInfo(_, _, SpecialType.System_Single) => CreateWellKnownComExceptionMarshallingData($"{TypeNames.ExceptionAsNaNMarshaller}<float>", unmanagedReturnType),
......@@ -165,7 +160,7 @@ internal static MarshallingInfo CreateSpecificMarshallingInfo(ManagedTypeInfo un
static NativeMarshallingAttributeInfo CreateWellKnownComExceptionMarshallingData(string marshallerName, ManagedTypeInfo unmanagedType)
{
ManagedTypeInfo marshallerTypeInfo = new ReferenceTypeInfo(marshallerName, marshallerName);
ManagedTypeInfo marshallerTypeInfo = new ReferenceTypeInfo(TypeNames.GlobalAlias + marshallerName, marshallerName);
return new NativeMarshallingAttributeInfo(marshallerTypeInfo,
new CustomTypeMarshallers(ImmutableDictionary<MarshalMode, CustomTypeMarshallerData>.Empty.Add(
MarshalMode.UnmanagedToManagedOut,
......
......@@ -86,7 +86,7 @@ public IEnumerable<ParameterSyntax> StubParameters
AttributeList(
SingletonSeparatedList(
Attribute(
ParseName(TypeNames.System_CodeDom_Compiler_GeneratedCodeAttribute_WithGlobal),
NameSyntaxes.System_CodeDom_Compiler_GeneratedCodeAttribute,
AttributeArgumentList(
SeparatedList(
new[]
......@@ -104,7 +104,7 @@ public IEnumerable<ParameterSyntax> StubParameters
// Adding the skip locals init indiscriminately since the source generator is
// targeted at non-blittable method signatures which typically will contain locals
// in the generated code.
Attribute(ParseName(TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute_WithGlobal)))));
Attribute(NameSyntaxes.System_Runtime_CompilerServices_SkipLocalsInitAttribute))));
}
return new SignatureContext()
......@@ -187,7 +187,7 @@ private static bool MethodIsSkipLocalsInit(StubEnvironment env, IMethodSymbol me
return false;
static bool IsSkipLocalsInitAttribute(AttributeData a)
=> a.AttributeClass?.ToDisplayString() == TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute_Metadata;
=> a.AttributeClass?.ToDisplayString() == TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute;
}
}
}
......@@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.CodeAnalysis;
namespace Microsoft.Interop
......
// 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 Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace Microsoft.Interop
{
public static class NameSyntaxes
{
private static NameSyntax? _DllImportAttribute;
public static NameSyntax DllImportAttribute => _DllImportAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.DllImportAttribute);
private static NameSyntax? _LibraryImportAttribute;
public static NameSyntax LibraryImportAttribute => _LibraryImportAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.LibraryImportAttribute);
private static NameSyntax? _System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute;
public static NameSyntax System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute => _System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute);
private static NameSyntax? _System_Runtime_InteropServices_MarshalAsAttribute;
public static NameSyntax System_Runtime_InteropServices_MarshalAsAttribute => _System_Runtime_InteropServices_MarshalAsAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_MarshalAsAttribute);
private static NameSyntax? _DefaultDllImportSearchPathsAttribute;
public static NameSyntax DefaultDllImportSearchPathsAttribute => _DefaultDllImportSearchPathsAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.DefaultDllImportSearchPathsAttribute);
private static NameSyntax? _SuppressGCTransitionAttribute;
public static NameSyntax SuppressGCTransitionAttribute => _SuppressGCTransitionAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.SuppressGCTransitionAttribute);
private static NameSyntax? _UnmanagedCallConvAttribute;
public static NameSyntax UnmanagedCallConvAttribute => _UnmanagedCallConvAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.UnmanagedCallConvAttribute);
private static NameSyntax? _System_Runtime_CompilerServices_SkipLocalsInitAttribute;
public static NameSyntax System_Runtime_CompilerServices_SkipLocalsInitAttribute => _System_Runtime_CompilerServices_SkipLocalsInitAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.System_Runtime_CompilerServices_SkipLocalsInitAttribute);
private static NameSyntax? _System_CodeDom_Compiler_GeneratedCodeAttribute;
public static NameSyntax System_CodeDom_Compiler_GeneratedCodeAttribute => _System_CodeDom_Compiler_GeneratedCodeAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.System_CodeDom_Compiler_GeneratedCodeAttribute);
private static NameSyntax? _UnmanagedCallersOnlyAttribute;
public static NameSyntax UnmanagedCallersOnlyAttribute => _UnmanagedCallersOnlyAttribute ??= ParseName(TypeNames.GlobalAlias + TypeNames.UnmanagedCallersOnlyAttribute);
}
public static class TypeSyntaxes
{
private static TypeSyntax? _StringMarshalling;
public static TypeSyntax StringMarshalling => _StringMarshalling ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.StringMarshalling);
private static TypeSyntax? _System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry;
public static TypeSyntax System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry => _System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceEntry);
private static TypeSyntax? _System_Runtime_InteropServices_NativeMemory;
public static TypeSyntax System_Runtime_InteropServices_NativeMemory => _System_Runtime_InteropServices_NativeMemory ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_NativeMemory);
private static TypeSyntax? _StrategyBasedComWrappers;
public static TypeSyntax StrategyBasedComWrappers => _StrategyBasedComWrappers ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.StrategyBasedComWrappers);
private static TypeSyntax? _IUnmanagedVirtualMethodTableProvider;
public static TypeSyntax IUnmanagedVirtualMethodTableProvider => _IUnmanagedVirtualMethodTableProvider ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.IUnmanagedVirtualMethodTableProvider);
private static TypeSyntax? _IIUnknownInterfaceType;
public static TypeSyntax IIUnknownInterfaceType => _IIUnknownInterfaceType ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.IIUnknownInterfaceType);
private static TypeSyntax? _IIUnknownDerivedDetails;
public static TypeSyntax IIUnknownDerivedDetails => _IIUnknownDerivedDetails ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.IIUnknownDerivedDetails);
private static TypeSyntax? _UnmanagedObjectUnwrapper;
public static TypeSyntax UnmanagedObjectUnwrapper => _UnmanagedObjectUnwrapper ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.UnmanagedObjectUnwrapper);
private static TypeSyntax? _IComExposedClass;
public static TypeSyntax IComExposedClass => _IComExposedClass ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.IComExposedClass);
private static TypeSyntax? _UnreachableException;
public static TypeSyntax UnreachableException => _UnreachableException ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.UnreachableException);
private static TypeSyntax? _System_Runtime_CompilerServices_RuntimeHelpers;
public static TypeSyntax System_Runtime_CompilerServices_RuntimeHelpers => _System_Runtime_CompilerServices_RuntimeHelpers ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Runtime_CompilerServices_RuntimeHelpers);
private static TypeSyntax? _System_Runtime_InteropServices_ComWrappers;
public static TypeSyntax System_Runtime_InteropServices_ComWrappers => _System_Runtime_InteropServices_ComWrappers ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_ComWrappers);
private static TypeSyntax? _System_IntPtr;
public static TypeSyntax System_IntPtr => _System_IntPtr ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_IntPtr);
private static TypeSyntax? _System_Guid;
public static TypeSyntax System_Guid => _System_Guid ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Guid);
private static TypeSyntax? _DllImportSearchPath;
public static TypeSyntax DllImportSearchPath => _DllImportSearchPath ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.DllImportSearchPath);
private static TypeSyntax? _System_Type;
public static TypeSyntax System_Type => _System_Type ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Type);
private static TypeSyntax? _System_Activator;
public static TypeSyntax System_Activator => _System_Activator ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Activator);
private static TypeSyntax? _System_Runtime_InteropServices_Marshal;
public static TypeSyntax System_Runtime_InteropServices_Marshal => _System_Runtime_InteropServices_Marshal ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_Marshal);
private static TypeSyntax? _System_Runtime_InteropServices_UnmanagedType;
public static TypeSyntax System_Runtime_InteropServices_UnmanagedType => _System_Runtime_InteropServices_UnmanagedType ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_UnmanagedType);
private static TypeSyntax? _System_Runtime_InteropServices_MemoryMarshal;
public static TypeSyntax System_Runtime_InteropServices_MemoryMarshal => _System_Runtime_InteropServices_MemoryMarshal ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_MemoryMarshal);
private static TypeSyntax? _System_Exception;
public static TypeSyntax System_Exception => _System_Exception ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Exception);
private static TypeSyntax? _System_GC;
public static TypeSyntax System_GC => _System_GC ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_GC);
private static TypeSyntax? _System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch;
public static TypeSyntax System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch => _System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Runtime_InteropServices_ComWrappers_ComInterfaceDispatch);
private static TypeSyntax? _System_Runtime_CompilerServices_Unsafe;
public static TypeSyntax System_Runtime_CompilerServices_Unsafe => _System_Runtime_CompilerServices_Unsafe ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.System_Runtime_CompilerServices_Unsafe);
private static TypeSyntax? _CallConvCdecl;
private static TypeSyntax? _CallConvFastcall;
private static TypeSyntax? _CallConvMemberFunction;
private static TypeSyntax? _CallConvStdcall;
private static TypeSyntax? _CallConvSuppressGCTransition;
private static TypeSyntax? _CallConvThiscall;
public static TypeSyntax CallConv(string callConv)
{
return callConv switch
{
"Cdecl" => _CallConvCdecl ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.CallConvCdeclName),
"Fastcall" => _CallConvFastcall ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.CallConvFastcallName),
"MemberFunction" => _CallConvMemberFunction ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.CallConvMemberFunctionName),
"Stdcall" => _CallConvStdcall ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.CallConvStdcallName),
"SuppressGCTransition" => _CallConvSuppressGCTransition ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.CallConvSuppressGCTransitionName),
"Thiscall" => _CallConvThiscall ??= ParseTypeName(TypeNames.GlobalAlias + TypeNames.CallConvThiscallName),
_ => throw new ArgumentException($"Unexpected CallConv: {callConv}")
};
}
}
public static class TypeNames
{
public const string GlobalAlias = "global::";
public const string DllImportAttribute = "System.Runtime.InteropServices.DllImportAttribute";
public const string LibraryImportAttribute = "System.Runtime.InteropServices.LibraryImportAttribute";
public const string LibraryImportAttribute_ShortName = "LibraryImportAttribute";
......@@ -46,9 +179,9 @@ public static class TypeNames
public const string IUnmanagedInterfaceType_Metadata = "System.Runtime.InteropServices.Marshalling.IUnmanagedInterfaceType";
public const string System_Span_Metadata = "System.Span`1";
public const string System_Span = "System.Span";
public const string System_Span = GlobalAlias + "System.Span";
public const string System_ReadOnlySpan_Metadata = "System.ReadOnlySpan`1";
public const string System_ReadOnlySpan = "System.ReadOnlySpan";
public const string System_ReadOnlySpan = GlobalAlias + "System.ReadOnlySpan";
public const string System_IntPtr = "System.IntPtr";
......@@ -94,9 +227,7 @@ public static string MarshalEx(InteropGenerationOptions options)
public const string System_Runtime_InteropServices_InAttribute = "System.Runtime.InteropServices.InAttribute";
public const string System_Runtime_CompilerServices_SkipLocalsInitAttribute_WithGlobal = "global::System.Runtime.CompilerServices.SkipLocalsInitAttribute";
public const string System_Runtime_CompilerServices_SkipLocalsInitAttribute_Metadata = "System.Runtime.CompilerServices.SkipLocalsInitAttribute";
public const string System_Runtime_CompilerServices_SkipLocalsInitAttribute = "System.Runtime.CompilerServices.SkipLocalsInitAttribute";
public const string System_Runtime_CompilerServices_Unsafe = "System.Runtime.CompilerServices.Unsafe";
......@@ -106,7 +237,7 @@ public static string MarshalEx(InteropGenerationOptions options)
public const string DllImportSearchPath = "System.Runtime.InteropServices.DllImportSearchPath";
public const string System_CodeDom_Compiler_GeneratedCodeAttribute_WithGlobal = "global::System.CodeDom.Compiler.GeneratedCodeAttribute";
public const string System_CodeDom_Compiler_GeneratedCodeAttribute = "System.CodeDom.Compiler.GeneratedCodeAttribute";
public const string System_Runtime_InteropServices_DynamicInterfaceCastableImplementationAttribute = "System.Runtime.InteropServices.DynamicInterfaceCastableImplementationAttribute";
......@@ -162,5 +293,12 @@ public static string MarshalEx(InteropGenerationOptions options)
public const string System_Runtime_InteropServices_CULong = "System.Runtime.InteropServices.CULong";
public const string System_Runtime_InteropServices_NFloat = "System.Runtime.InteropServices.NFloat";
public const string CallConvCdeclName = "System.Runtime.CompilerServices.CallConvCdecl";
public const string CallConvFastcallName = "System.Runtime.CompilerServices.CallConvFastcall";
public const string CallConvStdcallName = "System.Runtime.CompilerServices.CallConvStdcall";
public const string CallConvThiscallName = "System.Runtime.CompilerServices.CallConvThiscall";
public const string CallConvSuppressGCTransitionName = "System.Runtime.CompilerServices.CallConvSuppressGCTransition";
public const string CallConvMemberFunctionName = "System.Runtime.CompilerServices.CallConvMemberFunction";
}
}
......@@ -13,6 +13,7 @@
</PropertyGroup>
<ItemGroup>
<EnabledGenerators Include="ComInterfaceGenerator" />
<Compile Include="$(CommonPath)DisableRuntimeMarshalling.cs" Link="Common\DisableRuntimeMarshalling.cs" />
<Compile Include="..\TestAssets\SharedTypes\ComInterfaces\**\*.cs" Link="ComInterfaces\%(RecursiveDir)\%(FileName).cs" />
</ItemGroup>
......
......@@ -16,6 +16,8 @@ partial class NativeExportsNE
{
internal partial class Stateless
{
public static string System = "Make sure generated code prefixes type references with global::";
[LibraryImport(NativeExportsNE_Binary, EntryPoint = "stringcontainer_deepduplicate")]
public static partial void DeepDuplicateStrings(StringContainer strings, out StringContainer pStringsOut);
......
......@@ -4,12 +4,11 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.Interop;
using Xunit;
using System.IO;
using VerifyCS = Microsoft.Interop.UnitTests.Verifiers.CSharpCodeFixVerifier<
Microsoft.CodeAnalysis.Testing.EmptyDiagnosticAnalyzer,
Microsoft.Interop.Analyzers.AddDisableRuntimeMarshallingAttributeFixer>;
......
......@@ -4,6 +4,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.Interop;
using Microsoft.Interop.UnitTests;
using System.Collections.Generic;
using System.Linq;
......@@ -286,7 +287,7 @@ bool ValidateAttribute(AttributeData attr)
}
AttributeSyntax syntax = (AttributeSyntax)attr.ApplicationSyntaxReference!.GetSyntax();
return syntax.Name.ToString().StartsWith("global::");
return syntax.Name.ToString().StartsWith(TypeNames.GlobalAlias);
}
}
else
......
// 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.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
namespace SharedTypes.ComInterfaces
{
[GeneratedComInterface(StringMarshalling = StringMarshalling.Utf8)]
[Guid(IID)]
internal partial interface ISystem
{
// Make sure method names System and Microsoft don't interfere with framework type / method references
void Microsoft(int p);
void System(int p);
public const string IID = "3BFFE3FD-D11E-4195-8250-0C73321977A0";
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册