Skip to content

Commit

Permalink
Add Length property to inline arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
jnm2 committed Mar 3, 2021
1 parent fb25c3e commit f926686
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public class Generator : IDisposable
/// </summary>
");

private static readonly XmlTextSyntax DocCommentStart = XmlText(" ").WithLeadingTrivia(DocumentationCommentExterior("///"));
private static readonly XmlTextSyntax DocCommentEnd = XmlText(XmlTextNewLine("\r\n", continueXmlDocumentationComment: false));

private static readonly IdentifierNameSyntax ConstantsClassName = IdentifierName("Constants");
private static readonly IdentifierNameSyntax InlineArrayIndexerExtensionsClassName = IdentifierName("InlineArrayIndexerExtensions");
private static readonly TypeSyntax SafeHandleTypeSyntax = IdentifierName("SafeHandle");
Expand Down Expand Up @@ -3158,17 +3161,34 @@ ExpressionSyntax GetHiddenFieldAccess() => MemberAccessExpression(
elementType = IntPtrTypeSyntax;
}

// private struct __TheStruct_Count
var lengthLiteralSyntax = LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(length));

// internal struct __TheStruct_Count
// {
// private TheStruct _0, _1, _2, _3, _4, _5, _6, _7, _8;
// internal TheStruct _0, _1, _2, _3, _4, _5, _6, _7, _8;
// /// <summary>Always <c>8</c>.</summary>
// internal int Length => 8;
// ...
IdentifierNameSyntax fixedLengthStructName = IdentifierName($"__{fieldName}_{length}");
var fixedLengthStruct = StructDeclaration(fixedLengthStructName.Identifier)
.AddModifiers(Token(this.Visibility))
.AddMembers(
FieldDeclaration(VariableDeclaration(elementType)
.AddVariables(Enumerable.Range(0, length).Select(n => VariableDeclarator($"_{n}")).ToArray()))
.AddModifiers(Token(this.Visibility)));
.AddModifiers(Token(this.Visibility)),
PropertyDeclaration(PredefinedType(Token(SyntaxKind.IntKeyword)), "Length")
.AddModifiers(Token(this.Visibility))
.WithExpressionBody(ArrowExpressionClause(lengthLiteralSyntax))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken))
.WithLeadingTrivia(Trivia(DocumentationCommentTrivia(SyntaxKind.SingleLineDocumentationCommentTrivia).AddContent(
DocCommentStart,
XmlElement("summary", List(new XmlNodeSyntax[]
{
XmlText("Always "),
XmlElement("c", List(new XmlNodeSyntax[] { XmlText(length.ToString(CultureInfo.InvariantCulture)) })),
XmlText("."),
})),
DocCommentEnd))));

var firstElementFieldName = IdentifierName("_0");
if (this.canCallCreateSpan)
Expand All @@ -3192,7 +3212,7 @@ ExpressionSyntax GetHiddenFieldAccess() => MemberAccessExpression(
InvocationExpression(MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName("MemoryMarshal"), IdentifierName("CreateSpan")))
.AddArgumentListArguments(
Argument(nameColon: null, Token(SyntaxKind.RefKeyword), firstElementFieldName),
Argument(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(length))))))
Argument(lengthLiteralSyntax))))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken))
.WithLeadingTrivia(InlineArrayUnsafeAsSpanComment));
}
Expand Down
4 changes: 4 additions & 0 deletions test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,8 @@ internal partial struct MainAVIHeader
internal struct __dwReserved_4
{
internal uint _0, _1, _2, _3;
/// <summary>Always <c>4</c>.</summary>
internal int Length => 4;
}
}
";
Expand Down Expand Up @@ -446,6 +448,8 @@ internal partial struct MainAVIHeader
internal struct __dwReserved_4
{
internal uint _0, _1, _2, _3;
/// <summary>Always <c>4</c>.</summary>
internal int Length => 4;
/// <summary>
/// Gets a ref to an individual element of the inline array.
/// ⚠ Important ⚠: When this struct is on the stack, do not let the returned reference outlive the stack frame that defines it.
Expand Down

0 comments on commit f926686

Please sign in to comment.