-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompilationExtensions.cs
More file actions
30 lines (25 loc) · 1.15 KB
/
CompilationExtensions.cs
File metadata and controls
30 lines (25 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
namespace DynamoDBGenerator.SourceGenerator.Extensions;
// Great source: https://github.com/dotnet/runtime/blob/main/src/tools/illink/src/ILLink.RoslynAnalyzer/CompilationExtensions.cs
public static class CompilationExtensions
{
public static ReadOnlySpan<INamedTypeSymbol> GetTypeSymbols(this Compilation compilation,
ImmutableArray<SyntaxNode> classDeclarations)
{
var span = classDeclarations.AsSpan();
var symbols = new INamedTypeSymbol[classDeclarations.Length];
for (var i = 0; i < span.Length; i++)
{
var classDeclarationSyntax = span[i];
if (compilation
.GetSemanticModel(classDeclarationSyntax.SyntaxTree)
.GetDeclaredSymbol(classDeclarationSyntax)
is not INamedTypeSymbol typeSymbol)
throw new ArgumentException(
$"Could not convert the '{classDeclarationSyntax.ToFullString()}' into a '{nameof(ITypeSymbol)}'.");
symbols[i] = typeSymbol;
}
return new ReadOnlySpan<INamedTypeSymbol>(symbols);
}
}