-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathProjectableInterpreter.cs
More file actions
256 lines (224 loc) · 11.9 KB
/
ProjectableInterpreter.cs
File metadata and controls
256 lines (224 loc) · 11.9 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace EntityFrameworkCore.Projectables.Generator
{
public static class ProjectableInterpreter
{
static IEnumerable<string> GetNestedInClassPath(ITypeSymbol namedTypeSymbol)
{
if (namedTypeSymbol.ContainingType is not null)
{
foreach (var nestedInClassName in GetNestedInClassPath(namedTypeSymbol.ContainingType))
{
yield return nestedInClassName;
}
}
yield return namedTypeSymbol.Name;
}
public static ProjectableDescriptor? GetDescriptor(Compilation compilation, MemberDeclarationSyntax member, SourceProductionContext context)
{
var semanticModel = compilation.GetSemanticModel(member.SyntaxTree);
var memberSymbol = semanticModel.GetDeclaredSymbol(member);
if (memberSymbol is null)
{
return null;
}
var projectableAttributeTypeSymbol = compilation.GetTypeByMetadataName("EntityFrameworkCore.Projectables.ProjectableAttribute");
var projectableAttributeClass = memberSymbol.GetAttributes()
.Where(x => x.AttributeClass?.Name == "ProjectableAttribute")
.FirstOrDefault();
if (projectableAttributeClass is null || !SymbolEqualityComparer.Default.Equals(projectableAttributeClass.AttributeClass, projectableAttributeTypeSymbol))
{
return null;
}
var nullConditionalRewriteSupport = projectableAttributeClass.NamedArguments
.Where(x => x.Key == "NullConditionalRewriteSupport")
.Where(x => x.Value.Kind == TypedConstantKind.Enum)
.Select(x => x.Value.Value)
.Where(x => Enum.IsDefined(typeof(NullConditionalRewriteSupport), x))
.Cast<NullConditionalRewriteSupport>()
.FirstOrDefault();
var useMemberBody = projectableAttributeClass.NamedArguments
.Where(x => x.Key == "UseMemberBody")
.Select(x => x.Value.Value)
.OfType<string?>()
.FirstOrDefault();
var memberBody = member;
if (useMemberBody is not null)
{
var comparer = SymbolEqualityComparer.Default;
memberBody = memberSymbol.ContainingType.GetMembers(useMemberBody)
.Where(x =>
{
if (memberSymbol is IMethodSymbol symbolMethod &&
x is IMethodSymbol xMethod &&
comparer.Equals(symbolMethod.ReturnType, xMethod.ReturnType) &&
symbolMethod.TypeArguments.Length == xMethod.TypeArguments.Length &&
!symbolMethod.TypeArguments.Zip(xMethod.TypeArguments, (a, b) => !comparer.Equals(a, b)).Any())
{
return true;
}
else if (memberSymbol is IPropertySymbol symbolProperty &&
x is IPropertySymbol xProperty &&
comparer.Equals(symbolProperty.Type, xProperty.Type))
{
return true;
}
else
{
return false;
}
})
.SelectMany(x => x.DeclaringSyntaxReferences)
.Select(x => x.GetSyntax())
.OfType<MemberDeclarationSyntax>()
.FirstOrDefault(x =>
{
if (x == null ||
x.SyntaxTree != member.SyntaxTree ||
x.Modifiers.Any(SyntaxKind.StaticKeyword) != member.Modifiers.Any(SyntaxKind.StaticKeyword))
{
return false;
}
else if (x is MethodDeclarationSyntax xMethod &&
xMethod.ExpressionBody is not null)
{
return true;
}
else if (x is PropertyDeclarationSyntax xProperty &&
xProperty.ExpressionBody is not null)
{
return true;
}
else
{
return false;
}
});
if (memberBody is null) return null;
}
var expressionSyntaxRewriter = new ExpressionSyntaxRewriter(memberSymbol.ContainingType, nullConditionalRewriteSupport, semanticModel, context);
var declarationSyntaxRewriter = new DeclarationSyntaxRewriter(semanticModel);
var descriptor = new ProjectableDescriptor {
UsingDirectives = member.SyntaxTree.GetRoot().DescendantNodes().OfType<UsingDirectiveSyntax>(),
ClassName = memberSymbol.ContainingType.Name,
ClassNamespace = memberSymbol.ContainingType.ContainingNamespace.IsGlobalNamespace ? null : memberSymbol.ContainingType.ContainingNamespace.ToDisplayString(),
MemberName = memberSymbol.Name,
NestedInClassNames = GetNestedInClassPath(memberSymbol.ContainingType),
ParametersList = SyntaxFactory.ParameterList()
};
if (memberSymbol.ContainingType is INamedTypeSymbol { IsGenericType: true } containingNamedType)
{
descriptor.ClassTypeParameterList = SyntaxFactory.TypeParameterList();
foreach (var additionalClassTypeParameter in containingNamedType.TypeParameters)
{
descriptor.ClassTypeParameterList = descriptor.ClassTypeParameterList.AddParameters(
SyntaxFactory.TypeParameter(additionalClassTypeParameter.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat))
);
if (!additionalClassTypeParameter.ConstraintTypes.IsDefaultOrEmpty)
{
descriptor.ClassConstraintClauses ??= SyntaxFactory.List<TypeParameterConstraintClauseSyntax>();
descriptor.ClassConstraintClauses = descriptor.ClassConstraintClauses.Value.Add(
SyntaxFactory.TypeParameterConstraintClause(
SyntaxFactory.IdentifierName(additionalClassTypeParameter.Name),
SyntaxFactory.SeparatedList<TypeParameterConstraintSyntax>(
additionalClassTypeParameter
.ConstraintTypes
.Select(c => SyntaxFactory.TypeConstraint(
SyntaxFactory.IdentifierName(c.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat))
))
)
)
);
}
// todo: add additional type constraints
}
}
if (!member.Modifiers.Any(SyntaxKind.StaticKeyword))
{
descriptor.ParametersList = descriptor.ParametersList.AddParameters(
SyntaxFactory.Parameter(
SyntaxFactory.Identifier("@this")
)
.WithType(
SyntaxFactory.ParseTypeName(
memberSymbol.ContainingType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)
)
)
);
}
var methodSymbol = memberSymbol as IMethodSymbol;
if (methodSymbol is { IsExtensionMethod: true })
{
var targetTypeSymbol = methodSymbol.Parameters.First().Type;
descriptor.TargetClassNamespace = targetTypeSymbol.ContainingNamespace.IsGlobalNamespace ? null : targetTypeSymbol.ContainingNamespace.ToDisplayString();
descriptor.TargetNestedInClassNames = GetNestedInClassPath(targetTypeSymbol);
}
else
{
descriptor.TargetClassNamespace = descriptor.ClassNamespace;
descriptor.TargetNestedInClassNames = descriptor.NestedInClassNames;
}
if (memberBody is MethodDeclarationSyntax methodDeclarationSyntax)
{
if (methodDeclarationSyntax.ExpressionBody is null)
{
var diagnostic = Diagnostic.Create(Diagnostics.RequiresExpressionBodyDefinition, methodDeclarationSyntax.GetLocation(), memberSymbol.Name);
context.ReportDiagnostic(diagnostic);
return null;
}
var returnType = declarationSyntaxRewriter.Visit(methodDeclarationSyntax.ReturnType);
descriptor.ReturnTypeName = returnType.ToString();
descriptor.ExpressionBody = (ExpressionSyntax)expressionSyntaxRewriter.Visit(methodDeclarationSyntax.ExpressionBody.Expression);
foreach (var additionalParameter in ((ParameterListSyntax)declarationSyntaxRewriter.Visit(methodDeclarationSyntax.ParameterList)).Parameters)
{
descriptor.ParametersList = descriptor.ParametersList.AddParameters(additionalParameter);
}
if (methodDeclarationSyntax.TypeParameterList is not null)
{
descriptor.TypeParameterList = SyntaxFactory.TypeParameterList();
foreach (var additionalTypeParameter in ((TypeParameterListSyntax)declarationSyntaxRewriter.Visit(methodDeclarationSyntax.TypeParameterList)).Parameters)
{
descriptor.TypeParameterList = descriptor.TypeParameterList.AddParameters(additionalTypeParameter);
}
}
if (methodDeclarationSyntax.ConstraintClauses.Any())
{
descriptor.ConstraintClauses = SyntaxFactory.List(
methodDeclarationSyntax
.ConstraintClauses
.Select(x => (TypeParameterConstraintClauseSyntax)declarationSyntaxRewriter.Visit(x))
);
}
}
else if (memberBody is PropertyDeclarationSyntax propertyDeclarationSyntax)
{
var expressionBody = propertyDeclarationSyntax.ExpressionBody;
if (expressionBody is null)
{
//try to get from getter
var getter = propertyDeclarationSyntax.AccessorList?.Accessors.FirstOrDefault(x => x.Kind() == SyntaxKind.GetAccessorDeclaration);
expressionBody = getter?.ExpressionBody;
}
if (expressionBody is null)
{
var diagnostic = Diagnostic.Create(Diagnostics.RequiresExpressionBodyDefinition, propertyDeclarationSyntax.GetLocation(), memberSymbol.Name);
context.ReportDiagnostic(diagnostic);
return null;
}
var returnType = declarationSyntaxRewriter.Visit(propertyDeclarationSyntax.Type);
descriptor.ReturnTypeName = returnType.ToString();
descriptor.ExpressionBody = (ExpressionSyntax)expressionSyntaxRewriter.Visit(expressionBody.Expression);
}
else
{
return null;
}
return descriptor;
}
}
}