-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Emit interop source directly as text #133450
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ef4a642
Emit interop source directly as text
jkoritzinsky 53b238d
Decouple containment models from Roslyn syntax
jkoritzinsky b84a48b
Share containing-type declarations across source generators
jkoritzinsky a179adc
Use sequence-equality arrays for JavaScript marshalling info
jkoritzinsky 8b70f5f
Merge main into interop text generation
jkoritzinsky 12a5939
Address interop source generator review feedback
jkoritzinsky da5fc8b
Fix interop declaration metadata and incremental caching
jkoritzinsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/libraries/Common/src/SourceGenerators/ContainingTypeUtilities.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // 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.Diagnostics; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
|
||
| namespace SourceGenerators; | ||
|
|
||
| /// <summary>Extracts containing declarations during source-generator input analysis.</summary> | ||
| public static class ContainingTypeUtilities | ||
| { | ||
| public static IEnumerable<TypeDeclarationSyntax> EnumerateContainingTypes(TypeDeclarationSyntax? innermostType) | ||
| { | ||
| for (TypeDeclarationSyntax? current = innermostType; current is not null; current = current.Parent as TypeDeclarationSyntax) | ||
| { | ||
| yield return current; | ||
| } | ||
| } | ||
|
|
||
| public static ImmutableArray<string> GetModifiers(SyntaxTokenList modifiers) | ||
| => modifiers.Select(static modifier => modifier.Text).ToImmutableArray(); | ||
|
|
||
| public static DeclarationHeader GetDeclarationHeader(TypeDeclarationSyntax declaration) | ||
| { | ||
| return new DeclarationHeader( | ||
| GetModifiers(declaration.Modifiers), | ||
| GetTypeKindKeyword(declaration), | ||
| declaration.Identifier.Text, | ||
| declaration.Identifier.Text + GetTypeParameters(declaration.TypeParameterList)); | ||
| } | ||
|
|
||
| public static DeclarationHeader GetDeclarationHeader(MethodDeclarationSyntax declaration) | ||
| { | ||
| return new DeclarationHeader( | ||
| GetModifiers(declaration.Modifiers), | ||
| "", | ||
| declaration.Identifier.Text, | ||
| declaration.Identifier.Text + GetTypeParameters(declaration.TypeParameterList)); | ||
| } | ||
|
|
||
| public static bool TryGetContainingTypeDeclarations( | ||
| TypeDeclarationSyntax innermostType, | ||
| SemanticModel semanticModel, | ||
| CancellationToken cancellationToken, | ||
| [NotNullWhen(true)] out List<string>? declarations) | ||
| { | ||
| declarations = null; | ||
| foreach (TypeDeclarationSyntax current in EnumerateContainingTypes(innermostType)) | ||
| { | ||
| if (!current.Modifiers.Any(SyntaxKind.PartialKeyword)) | ||
| { | ||
| declarations = null; | ||
| return false; | ||
| } | ||
|
|
||
| INamedTypeSymbol? typeSymbol = semanticModel.GetDeclaredSymbol(current, cancellationToken); | ||
| Debug.Assert(typeSymbol is not null); | ||
| var header = new DeclarationHeader( | ||
| GetModifiers(current.Modifiers), | ||
| GetTypeKindKeyword(current), | ||
| current.Identifier.Text, | ||
| typeSymbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)); | ||
| (declarations ??= new()).Add(header.ToString()); | ||
| } | ||
| Debug.Assert(declarations?.Count > 0); | ||
| return true; | ||
| } | ||
|
|
||
| private static string GetTypeKindKeyword(TypeDeclarationSyntax declaration) => declaration.Kind() switch | ||
| { | ||
| SyntaxKind.ClassDeclaration => "class", | ||
| SyntaxKind.InterfaceDeclaration => "interface", | ||
| SyntaxKind.StructDeclaration => "struct", | ||
| SyntaxKind.RecordDeclaration => "record", | ||
| SyntaxKind.RecordStructDeclaration => "record struct", | ||
| _ => throw new InvalidOperationException("Unexpected containing declaration kind."), | ||
| }; | ||
|
|
||
| private static string? GetTypeParameters(TypeParameterListSyntax? typeParameters) | ||
| { | ||
| if (typeParameters is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| // Attributes on generic parameters are merged across partial declarations, so do not repeat them. | ||
| return "<" + string.Join(", ", typeParameters.Parameters.Select(static parameter => | ||
| (parameter.VarianceKeyword.RawKind == 0 ? "" : parameter.VarianceKeyword.Text + " ") | ||
| + parameter.Identifier.Text)) + ">"; | ||
| } | ||
| } | ||
59 changes: 59 additions & 0 deletions
59
src/libraries/Common/src/SourceGenerators/DeclarationHeader.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // 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.Immutable; | ||
| using System.Linq; | ||
|
|
||
| namespace SourceGenerators; | ||
|
|
||
| /// <summary>Cacheable declaration text independent of Roslyn syntax nodes and symbols.</summary> | ||
| public readonly struct DeclarationHeader : IEquatable<DeclarationHeader> | ||
| { | ||
| public DeclarationHeader(ImmutableArray<string> modifiers, string keyword, string identifier, string? name = null) | ||
| { | ||
| Modifiers = modifiers; | ||
| Keyword = keyword; | ||
| Identifier = identifier; | ||
| Name = name ?? identifier; | ||
| } | ||
|
|
||
| public ImmutableArray<string> Modifiers { get; init; } | ||
| public string Keyword { get; init; } | ||
| public string Identifier { get; init; } | ||
|
|
||
| /// <summary>The declaration name, including any generic parameters but not their attributes.</summary> | ||
| public string Name { get; init; } | ||
|
|
||
| public bool Equals(DeclarationHeader other) | ||
| { | ||
| return Keyword == other.Keyword | ||
| && Identifier == other.Identifier | ||
| && Name == other.Name | ||
| && Modifiers.SequenceEqual(other.Modifiers); | ||
| } | ||
|
|
||
| public override bool Equals(object? obj) => obj is DeclarationHeader other && Equals(other); | ||
|
|
||
| public override int GetHashCode() | ||
| { | ||
| unchecked | ||
| { | ||
| int hash = Keyword.GetHashCode(); | ||
| hash = hash * 31 + Identifier.GetHashCode(); | ||
| hash = hash * 31 + Name.GetHashCode(); | ||
| foreach (string modifier in Modifiers) | ||
| { | ||
| hash = hash * 31 + modifier.GetHashCode(); | ||
| } | ||
| return hash; | ||
| } | ||
| } | ||
|
|
||
| public override string ToString() | ||
| { | ||
| string modifiers = Modifiers.IsEmpty ? "" : string.Join(" ", Modifiers) + " "; | ||
| string keyword = Keyword.Length == 0 ? "" : Keyword + " "; | ||
| return modifiers + keyword + Name; | ||
| } | ||
| } |
46 changes: 0 additions & 46 deletions
46
src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/Comparers.cs
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.