-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneratesMethodGenerationPipeline.cs
More file actions
130 lines (112 loc) · 5.33 KB
/
GeneratesMethodGenerationPipeline.cs
File metadata and controls
130 lines (112 loc) · 5.33 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using System.Collections.Immutable;
using static EasySourceGenerators.Generators.Consts;
namespace EasySourceGenerators.Generators;
internal static class GeneratesMethodGenerationPipeline
{
internal static void Execute(
SourceProductionContext context,
ImmutableArray<MethodDeclarationSyntax?> generatorMethods,
Compilation compilation)
{
List<GeneratesMethodGenerationTarget> validMethods = GeneratesMethodGenerationTargetCollector.Collect(context, generatorMethods, compilation);
IReadOnlyList<IMethodSymbol> allPartials = GeneratesMethodExecutionRuntime.GetAllUnimplementedPartialMethods(compilation);
IEnumerable<IGrouping<(string TypeKey, string TargetMethodName), GeneratesMethodGenerationTarget>> groups = validMethods
.GroupBy(method => (TypeKey: method.ContainingType.ToDisplayString(), method.TargetMethodName));
foreach (IGrouping<(string TypeKey, string TargetMethodName), GeneratesMethodGenerationTarget> group in groups)
{
List<GeneratesMethodGenerationTarget> methods = group.ToList();
GeneratesMethodGenerationTarget firstMethod = methods[0];
context.ReportDiagnostic(Diagnostic.Create(
GeneratesMethodGeneratorDiagnostics.GeneratingMethodInfo,
firstMethod.Syntax.GetLocation(),
firstMethod.TargetMethodName,
firstMethod.ContainingType.Name,
string.Join(", ", methods.Select(method => method.Symbol.Name))));
string source = GenerateSourceForGroup(context, methods, firstMethod, allPartials, compilation);
if (!string.IsNullOrEmpty(source))
{
context.AddSource($"{firstMethod.ContainingType.Name}_{firstMethod.TargetMethodName}.g.cs", source);
}
}
}
private static string GenerateSourceForGroup(
SourceProductionContext context,
List<GeneratesMethodGenerationTarget> methods,
GeneratesMethodGenerationTarget firstMethod,
IReadOnlyList<IMethodSymbol> allPartials,
Compilation compilation)
{
bool hasSwitchCase = methods.Any(method => HasAttribute(method.Symbol, SwitchCaseAttributeFullName));
bool hasSwitchDefault = methods.Any(method => HasAttribute(method.Symbol, SwitchDefaultAttributeFullName));
bool isFluentPattern = methods.Count == 1 && methods[0].Symbol.ReturnType.ToDisplayString() == IMethodImplementationGeneratorFullName;
if (hasSwitchCase || hasSwitchDefault)
{
return GeneratesMethodPatternSourceBuilder.GenerateFromSwitchAttributes(
context,
methods,
firstMethod.PartialMethod,
firstMethod.ContainingType,
allPartials,
compilation);
}
if (isFluentPattern)
{
return GeneratesMethodPatternSourceBuilder.GenerateFromFluent(
context,
methods[0],
firstMethod.PartialMethod,
firstMethod.ContainingType,
compilation);
}
List<GeneratesMethodGenerationTarget> methodsWithParameters = methods
.Where(method => method.Symbol.Parameters.Length > 0)
.ToList();
if (methodsWithParameters.Count > 0)
{
foreach (GeneratesMethodGenerationTarget methodWithParameters in methodsWithParameters)
{
context.ReportDiagnostic(Diagnostic.Create(
GeneratesMethodGeneratorDiagnostics.CannotUseRuntimeParameterForCompileTimeGeneratorError,
GetMethodSignatureLocation(methodWithParameters.Syntax)));
}
return string.Empty;
}
return GenerateFromSimplePattern(context, firstMethod, compilation);
}
private static string GenerateFromSimplePattern(
SourceProductionContext context,
GeneratesMethodGenerationTarget firstMethod,
Compilation compilation)
{
(string? returnValue, string? error) = GeneratesMethodExecutionRuntime.ExecuteSimpleGeneratorMethod(
firstMethod.Symbol,
firstMethod.PartialMethod,
compilation);
if (error != null)
{
context.ReportDiagnostic(Diagnostic.Create(
GeneratesMethodGeneratorDiagnostics.GeneratorMethodExecutionError,
firstMethod.Syntax.GetLocation(),
firstMethod.Symbol.Name,
error));
return string.Empty;
}
return GeneratesMethodPatternSourceBuilder.GenerateSimplePartialMethod(
firstMethod.ContainingType,
firstMethod.PartialMethod,
returnValue);
}
private static bool HasAttribute(IMethodSymbol methodSymbol, string fullAttributeTypeName)
{
return methodSymbol.GetAttributes()
.Any(attribute => attribute.AttributeClass?.ToDisplayString() == fullAttributeTypeName);
}
private static Location GetMethodSignatureLocation(MethodDeclarationSyntax methodSyntax)
{
TextSpan signatureSpan = TextSpan.FromBounds(methodSyntax.SpanStart, methodSyntax.ParameterList.Span.End);
return Location.Create(methodSyntax.SyntaxTree, signatureSpan);
}
}