Skip to content

Commit

Permalink
Check whether Span<> is available before using it to unblock net35 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jnm2 committed Sep 23, 2021
1 parent 32ea310 commit ff64648
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ public class Generator : IDisposable
private readonly GeneratorOptions options;
private readonly CSharpCompilation? compilation;
private readonly CSharpParseOptions? parseOptions;
private readonly bool canUseSpan;
private readonly bool canCallCreateSpan;
private readonly bool getDelegateForFunctionPointerGenericExists;
private readonly bool generateSupportedOSPlatformAttributes;
Expand Down Expand Up @@ -320,6 +321,7 @@ public Generator(string metadataLibraryPath, Docs? docs, GeneratorOptions option
this.parseOptions = parseOptions;
this.volatileCode = new(this.committedCode);

this.canUseSpan = this.compilation?.GetTypeByMetadataName(typeof(Span<>).FullName) is not null;
this.canCallCreateSpan = this.compilation?.GetTypeByMetadataName(typeof(MemoryMarshal).FullName)?.GetMembers("CreateSpan").Any() is true;
this.getDelegateForFunctionPointerGenericExists = this.compilation?.GetTypeByMetadataName(typeof(Marshal).FullName)?.GetMembers(nameof(Marshal.GetDelegateForFunctionPointer)).Any(m => m is IMethodSymbol { IsGenericMethod: true }) is true;
this.generateDefaultDllImportSearchPathsAttribute = this.compilation?.GetTypeByMetadataName(typeof(DefaultDllImportSearchPathsAttribute).FullName) is object;
Expand Down Expand Up @@ -3694,15 +3696,18 @@ private IEnumerable<MemberDeclarationSyntax> CreateAdditionalTypeDefPWSTRMembers
.AddArgumentListArguments(Argument(thisValue)))))
.WithSemicolonToken(SemicolonWithLineFeed);

// internal Span<char> AsSpan() => this.Value is null ? default : new Span<char>(this.Value, this.Length);
TypeSyntax spanChar = MakeSpanOfT(PredefinedType(Token(SyntaxKind.CharKeyword)));
ExpressionSyntax thisLength = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, ThisExpression(), IdentifierName("Length"));
ExpressionSyntax spanCreation = ObjectCreationExpression(spanChar).AddArgumentListArguments(Argument(thisValue), Argument(thisLength));
ExpressionSyntax conditional = ConditionalExpression(thisValueIsNull, DefaultExpression(spanChar), spanCreation);
yield return MethodDeclaration(spanChar, Identifier("AsSpan"))
.AddModifiers(TokenWithSpace(this.Visibility))
.WithExpressionBody(ArrowExpressionClause(conditional))
.WithSemicolonToken(SemicolonWithLineFeed);
if (this.canUseSpan)
{
// internal Span<char> AsSpan() => this.Value is null ? default : new Span<char>(this.Value, this.Length);
TypeSyntax spanChar = MakeSpanOfT(PredefinedType(Token(SyntaxKind.CharKeyword)));
ExpressionSyntax thisLength = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, ThisExpression(), IdentifierName("Length"));
ExpressionSyntax spanCreation = ObjectCreationExpression(spanChar).AddArgumentListArguments(Argument(thisValue), Argument(thisLength));
ExpressionSyntax conditional = ConditionalExpression(thisValueIsNull, DefaultExpression(spanChar), spanCreation);
yield return MethodDeclaration(spanChar, Identifier("AsSpan"))
.AddModifiers(TokenWithSpace(this.Visibility))
.WithExpressionBody(ArrowExpressionClause(conditional))
.WithSemicolonToken(SemicolonWithLineFeed);
}
#pragma warning restore SA1114 // Parameter list should follow declaration
}

Expand Down

0 comments on commit ff64648

Please sign in to comment.