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

Reuse inline array structs across all users #707

Merged
merged 5 commits into from
Oct 4, 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
124 changes: 114 additions & 10 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5172,14 +5172,67 @@ private ParameterSyntax CreateParameter(TypeHandleInfo parameterInfo, Parameter
return (ranklessArray, default(SyntaxList<MemberDeclarationSyntax>), marshalAs);
}

SyntaxList<MemberDeclarationSyntax> additionalMembers = default;
int length = int.Parse(((LiteralExpressionSyntax)arrayType.RankSpecifiers[0].Sizes[0]).Token.ValueText, CultureInfo.InvariantCulture);
TypeSyntax elementType = arrayType.ElementType;

// C# does not allow Span<T> where T is a pointer type.
if (elementType is PointerTypeSyntax ptr)
static string SanitizeTypeName(string typeName) => typeName.Replace(' ', '_').Replace('.', '_').Replace(':', '_').Replace('*', '_').Replace('<', '_').Replace('>', '_').Replace('[', '_').Replace(']', '_').Replace(',', '_');
void DetermineNames(TypeSyntax elementType, out string? structNamespace, out string fixedLengthStructNameString, out string? fileNamePrefix)
{
if (elementType is QualifiedNameSyntax qualifiedElementType)
{
structNamespace = qualifiedElementType.Left.ToString();
if (!structNamespace.StartsWith(GlobalWinmdRootNamespaceAlias))
{
// Force structs to be under the root namespace.
structNamespace = GlobalWinmdRootNamespaceAlias;
}

fileNamePrefix = SanitizeTypeName(qualifiedElementType.Right.Identifier.ValueText);
fixedLengthStructNameString = $"__{fileNamePrefix}_{length}";
}
else if (elementType is PredefinedTypeSyntax predefined)
{
structNamespace = GlobalWinmdRootNamespaceAlias;
fileNamePrefix = predefined.Keyword.ValueText;
fixedLengthStructNameString = $"__{fileNamePrefix}_{length}";
}
else if (elementType is IdentifierNameSyntax identifier)
{
structNamespace = GlobalWinmdRootNamespaceAlias;
fileNamePrefix = identifier.Identifier.ValueText;
fixedLengthStructNameString = $"__{fileNamePrefix}_{length}";
}
else if (elementType is FunctionPointerTypeSyntax functionPtr)
{
structNamespace = GlobalWinmdRootNamespaceAlias;
fileNamePrefix = "FunctionPointer";
fixedLengthStructNameString = $"__{SanitizeTypeName(functionPtr.ToString())}_{length}";
}
else if (elementType is PointerTypeSyntax elementPointerType)
{
DetermineNames(elementPointerType.ElementType, out structNamespace, out fixedLengthStructNameString, out fileNamePrefix);
fixedLengthStructNameString = $"P{fixedLengthStructNameString}";
}
else
{
throw new NotSupportedException($"Type {elementType} had unexpected kind: {elementType.GetType().Name}");
}

// Generate inline array as a nested struct if the element type is itself a nested type.
if (fieldTypeHandleInfo is ArrayTypeHandleInfo { ElementType: HandleTypeHandleInfo fieldHandleTypeInfo } && this.IsNestedType(fieldHandleTypeInfo.Handle))
{
structNamespace = null;
fileNamePrefix = null;
}
}

DetermineNames(elementType, out string? structNamespace, out string fixedLengthStructNameString, out string? fileNamePrefix);
IdentifierNameSyntax fixedLengthStructName = IdentifierName(fixedLengthStructNameString);
TypeSyntax qualifiedFixedLengthStructName = ParseTypeName($"{structNamespace}.{fixedLengthStructNameString}");

if (structNamespace is not null && this.volatileCode.IsInlineArrayStructGenerated(structNamespace, fixedLengthStructNameString))
{
elementType = IntPtrTypeSyntax;
return (qualifiedFixedLengthStructName, default, default);
}

// IntPtr/UIntPtr began implementing IEquatable<T> in .NET 5. We may want to actually resolve the type in the compilation to see if it implements this.
Expand All @@ -5192,7 +5245,6 @@ private ParameterSyntax CreateParameter(TypeHandleInfo parameterInfo, Parameter
// /// <summary>The length of the inline array.</summary>
// internal const int Length = LENGTH;
// ...
IdentifierNameSyntax fixedLengthStructName = IdentifierName($"__{elementType.ToString().Replace(' ', '_').Replace('.', '_').Replace(':', '_').Replace('*', '_').Replace('<', '_').Replace('>', '_').Replace('[', '_').Replace(']', '_').Replace(',', '_')}_{length}");
IdentifierNameSyntax lengthConstant = IdentifierName("SpanLength");
IdentifierNameSyntax lengthInstanceProperty = IdentifierName("Length");

Expand Down Expand Up @@ -5840,7 +5892,7 @@ StatementSyntax ClearSlice(ExpressionSyntax span) =>

// internal static unsafe ref readonly TheStruct ReadOnlyItemRef(this in MainAVIHeader.__dwReserved_4 @this, int index) => ref @this.Value[index]
ParameterSyntax thisParameter = Parameter(atThis.Identifier)
.WithType(QualifiedName((NameSyntax)new HandleTypeHandleInfo(this.Reader, fieldDef.GetDeclaringType()).ToTypeSyntax(extensionMethodSignatureTypeSettings, customAttributes).Type, fixedLengthStructName).WithTrailingTrivia(TriviaList(Space)))
.WithType(qualifiedFixedLengthStructName.WithTrailingTrivia(Space))
.AddModifiers(TokenWithSpace(SyntaxKind.ThisKeyword), TokenWithSpace(SyntaxKind.InKeyword));
ParameterSyntax indexParameter = Parameter(indexParamName.Identifier).WithType(PredefinedType(TokenWithSpace(SyntaxKind.IntKeyword)));
MethodDeclarationSyntax getAtMethod = MethodDeclaration(RefType(qualifiedElementType.WithTrailingTrivia(TriviaList(Space))).WithReadOnlyKeyword(TokenWithSpace(SyntaxKind.ReadOnlyKeyword)), Identifier("ReadOnlyItemRef"))
Expand All @@ -5852,8 +5904,33 @@ StatementSyntax ClearSlice(ExpressionSyntax span) =>
this.volatileCode.AddInlineArrayIndexerExtension(getAtMethod);
}

additionalMembers = additionalMembers.Add(fixedLengthStruct);
return (fixedLengthStructName, additionalMembers, null);
if (structNamespace is not null)
{
// Wrap with any additional namespaces.
MemberDeclarationSyntax fixedLengthStructInNamespace = fixedLengthStruct;
if (structNamespace != GlobalWinmdRootNamespaceAlias)
{
if (!structNamespace.StartsWith(GlobalWinmdRootNamespaceAlias + ".", StringComparison.Ordinal))
{
throw new NotSupportedException($"The {structNamespace}.{fixedLengthStructNameString} struct must be under the metadata's common namespace.");
}

fixedLengthStructInNamespace = NamespaceDeclaration(ParseName(structNamespace.Substring(GlobalWinmdRootNamespaceAlias.Length + 1)))
.AddMembers(fixedLengthStruct);
}

fixedLengthStructInNamespace = fixedLengthStructInNamespace
.WithAdditionalAnnotations(new SyntaxAnnotation(SimpleFileNameAnnotation, $"{fileNamePrefix}.InlineArrays"));

this.volatileCode.AddInlineArrayStruct(structNamespace, fixedLengthStructNameString, fixedLengthStructInNamespace);

return (qualifiedFixedLengthStructName, default, null);
}
else
{
// This struct will be injected as a nested type, to match the element type.
return (fixedLengthStructName, List<MemberDeclarationSyntax>().Add(fixedLengthStruct), null);
}
}

private void DeclareSliceAtNullExtensionMethodIfNecessary()
Expand Down Expand Up @@ -5998,6 +6075,20 @@ private bool IsDelegateReference(HandleTypeHandleInfo typeHandleInfo, out TypeDe
return false;
}

private bool IsNestedType(EntityHandle typeHandle)
{
switch (typeHandle.Kind)
{
case HandleKind.TypeDefinition:
TypeDefinition typeDef = this.Reader.GetTypeDefinition((TypeDefinitionHandle)typeHandle);
return typeDef.IsNested;
case HandleKind.TypeReference:
return this.TryGetTypeDefHandle((TypeReferenceHandle)typeHandle, out TypeDefinitionHandle typeDefHandle) && this.IsNestedType(typeDefHandle);
}

return false;
}

private bool IsManagedType(TypeDefinitionHandle typeDefinitionHandle)
{
if (this.managedTypesCheck.TryGetValue(typeDefinitionHandle, out bool result))
Expand Down Expand Up @@ -6304,6 +6395,8 @@ private class GeneratedCode
/// </summary>
private readonly Dictionary<string, Exception?> specialTypesGenerating = new(StringComparer.Ordinal);

private readonly Dictionary<(string Namespace, string Name), MemberDeclarationSyntax> inlineArrays = new();

private readonly Dictionary<string, TypeSyntax?> releaseMethodsWithSafeHandleTypesGenerating = new();

private readonly List<MethodDeclarationSyntax> inlineArrayIndexerExtensionsMembers = new();
Expand All @@ -6322,11 +6415,12 @@ internal GeneratedCode(GeneratedCode parent)
}

internal bool IsEmpty => this.modulesAndMembers.Count == 0 && this.types.Count == 0 && this.fieldsToSyntax.Count == 0 && this.safeHandleTypes.Count == 0 && this.specialTypes.Count == 0
&& this.inlineArrayIndexerExtensionsMembers.Count == 0 && this.comInterfaceFriendlyExtensionsMembers.Count == 0 && this.macros.Count == 0;
&& this.inlineArrayIndexerExtensionsMembers.Count == 0 && this.comInterfaceFriendlyExtensionsMembers.Count == 0 && this.macros.Count == 0 && this.inlineArrays.Count == 0;

internal IEnumerable<MemberDeclarationSyntax> GeneratedTypes => this.GetTypesWithInjectedFields()
.Concat(this.specialTypes.Values.Where(st => !st.TopLevel).Select(st => st.Type))
.Concat(this.safeHandleTypes);
.Concat(this.safeHandleTypes)
.Concat(this.inlineArrays.Values);

internal IEnumerable<MemberDeclarationSyntax> GeneratedTopLevelTypes => this.specialTypes.Values.Where(st => st.TopLevel).Select(st => st.Type);

Expand Down Expand Up @@ -6525,6 +6619,15 @@ internal void GenerateSpecialType(string name, Action generator)
}
}

internal bool IsInlineArrayStructGenerated(string @namespace, string name) => this.parent?.inlineArrays.ContainsKey((@namespace, name)) is true || this.inlineArrays.ContainsKey((@namespace, name));

internal void AddInlineArrayStruct(string @namespace, string name, MemberDeclarationSyntax inlineArrayStructDeclaration)
{
this.ThrowIfNotGenerating();

this.inlineArrays.Add((@namespace, name), inlineArrayStructDeclaration);
}

internal void GenerateType(TypeDefinitionHandle typeDefinitionHandle, bool hasUnmanagedName, Action generator)
{
this.ThrowIfNotGenerating();
Expand Down Expand Up @@ -6637,6 +6740,7 @@ private void Commit(GeneratedCode? parent)
Commit(this.macros, parent?.macros);
Commit(this.methodsGenerating, parent?.methodsGenerating);
Commit(this.specialTypesGenerating, parent?.specialTypesGenerating);
Commit(this.inlineArrays, parent?.inlineArrays);
Commit(this.releaseMethodsWithSafeHandleTypesGenerating, parent?.releaseMethodsWithSafeHandleTypesGenerating);
Commit(this.inlineArrayIndexerExtensionsMembers, parent?.inlineArrayIndexerExtensionsMembers);
Commit(this.comInterfaceFriendlyExtensionsMembers, parent?.comInterfaceFriendlyExtensionsMembers);
Expand Down
13 changes: 6 additions & 7 deletions test/GenerationSandbox.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Windows.Win32.Storage.FileSystem;
using Windows.Win32.System.Com;
using Windows.Win32.System.Console;
using Windows.Win32.System.ErrorReporting;
using Windows.Win32.UI.Shell;
using VARDESC = Windows.Win32.System.Com.VARDESC;

Expand Down Expand Up @@ -252,7 +251,7 @@ public void FixedCharArrayToString_Length()
[Fact]
public void FixedCharArray_ToString()
{
Windows.Win32.System.RestartManager.RM_PROCESS_INFO.__char_64 fixedCharArray = default;
__char_64 fixedCharArray = default;
Assert.Equal(string.Empty, fixedCharArray.ToString());
fixedCharArray[0] = 'H';
Assert.Equal("H", fixedCharArray.ToString());
Expand All @@ -270,7 +269,7 @@ public void FixedCharArray_ToString()
[Fact]
public void FixedLengthArray_ToArray()
{
Windows.Win32.System.RestartManager.RM_PROCESS_INFO.__char_64 fixedCharArray = default;
__char_64 fixedCharArray = default;
fixedCharArray = "hi";
char[] expected = new char[fixedCharArray.Length];
expected[0] = fixedCharArray[0];
Expand All @@ -285,7 +284,7 @@ public void FixedLengthArray_ToArray()
[Fact]
public void FixedLengthArray_CopyTo()
{
Windows.Win32.System.RestartManager.RM_PROCESS_INFO.__char_64 fixedCharArray = default;
__char_64 fixedCharArray = default;
fixedCharArray = "hi";
Span<char> span = new char[fixedCharArray.Length];
fixedCharArray.CopyTo(span);
Expand All @@ -302,7 +301,7 @@ public void FixedLengthArray_CopyTo()
[Fact]
public void FixedLengthArray_Equals()
{
Windows.Win32.System.RestartManager.RM_PROCESS_INFO.__char_64 fixedCharArray = default;
__char_64 fixedCharArray = default;
fixedCharArray = "hi";

Assert.True(fixedCharArray.Equals("hi"));
Expand Down Expand Up @@ -330,7 +329,7 @@ public void FixedLengthArray_Equals()
[Fact]
public void FixedCharArraySetWithString()
{
Windows.Win32.System.RestartManager.RM_PROCESS_INFO.__char_64 fixedCharArray = default;
__char_64 fixedCharArray = default;

fixedCharArray = null;
Assert.Equal(string.Empty, fixedCharArray.ToString());
Expand Down Expand Up @@ -365,7 +364,7 @@ public unsafe void GetProcAddress_String()
[Fact]
public void StructCharFieldsMarshaledAsUtf16()
{
Assert.Equal(128 * sizeof(char), Marshal.SizeOf<WER_REPORT_INFORMATION.__char_128>());
Assert.Equal(128 * sizeof(char), Marshal.SizeOf<__char_128>());
Assert.Equal(sizeof(char), Marshal.SizeOf<KEY_EVENT_RECORD._uChar_e__Union>());
}

Expand Down
7 changes: 5 additions & 2 deletions test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,9 @@ public void FixedLengthInlineArray_TwoRequested()

this.CollectGeneratedCode(this.generator);
this.AssertNoDiagnostics();

// Verify that inline arrays that share the same length and type are only declared once and shared with all users.
Assert.Single(this.FindGeneratedType("__char_64"));
}

[Theory, PairwiseData]
Expand All @@ -1256,8 +1259,8 @@ public void FixedLengthInlineArrayIn_MODULEENTRY32(bool allowMarshaling)
var decl = (StructDeclarationSyntax)Assert.Single(this.FindGeneratedType("MODULEENTRY32"));
var field = this.FindFieldDeclaration(decl, "szModule");
Assert.True(field.HasValue);
var fieldType = Assert.IsType<IdentifierNameSyntax>(field!.Value.Field.Declaration.Type);
Assert.IsType<StructDeclarationSyntax>(Assert.Single(this.FindGeneratedType(fieldType.Identifier.ValueText)));
var fieldType = Assert.IsType<QualifiedNameSyntax>(field!.Value.Field.Declaration.Type);
Assert.IsType<StructDeclarationSyntax>(Assert.Single(this.FindGeneratedType(fieldType.Right.Identifier.ValueText)));
}

[Fact]
Expand Down