-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathDecompilerSyntaxTreeGenerator.cs
More file actions
384 lines (309 loc) · 11.5 KB
/
DecompilerSyntaxTreeGenerator.cs
File metadata and controls
384 lines (309 loc) · 11.5 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
using System.Collections;
using System.Collections.Immutable;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
namespace ICSharpCode.Decompiler.Generators;
[Generator]
internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator
{
record AstNodeAdditions(string NodeName, bool NeedsAcceptImpls, bool NeedsVisitor, bool NeedsNullNode, bool NeedsPatternPlaceholder, int NullNodeBaseCtorParamCount, bool IsTypeNode, string VisitMethodName, string VisitMethodParamType, EquatableArray<(string Member, string TypeName, bool RecursiveMatch, bool MatchAny)>? MembersToMatch);
AstNodeAdditions GetAstNodeAdditions(GeneratorAttributeSyntaxContext context, CancellationToken ct)
{
var targetSymbol = (INamedTypeSymbol)context.TargetSymbol;
var attribute = context.Attributes.SingleOrDefault(ad => ad.AttributeClass?.Name == "DecompilerAstNodeAttribute")!;
var (visitMethodName, paramTypeName) = targetSymbol.Name switch {
"ErrorExpression" => ("ErrorNode", "AstNode"),
string s when s.Contains("AstType") => (s.Replace("AstType", "Type"), s),
_ => (targetSymbol.Name, targetSymbol.Name),
};
List<(string Member, string TypeName, bool RecursiveMatch, bool MatchAny)>? membersToMatch = null;
if (!targetSymbol.MemberNames.Contains("DoMatch"))
{
membersToMatch = new();
var astNodeType = (INamedTypeSymbol)context.SemanticModel.GetSpeculativeSymbolInfo(context.TargetNode.Span.Start, SyntaxFactory.ParseTypeName("AstNode"), SpeculativeBindingOption.BindAsTypeOrNamespace).Symbol!;
if (targetSymbol.BaseType!.MemberNames.Contains("MatchAttributesAndModifiers"))
membersToMatch.Add(("MatchAttributesAndModifiers", null!, false, false));
foreach (var m in targetSymbol.GetMembers())
{
if (m is not IPropertySymbol property || property.IsIndexer || property.IsOverride)
continue;
if (property.GetAttributes().Any(a => a.AttributeClass?.Name == "ExcludeFromMatchAttribute"))
continue;
if (property.Type.MetadataName is "CSharpTokenNode" or "TextLocation")
continue;
switch (property.Type)
{
case INamedTypeSymbol named when named.IsDerivedFrom(astNodeType) || named.MetadataName == "AstNodeCollection`1":
membersToMatch.Add((property.Name, named.Name, true, false));
break;
case INamedTypeSymbol { TypeKind: TypeKind.Enum } named when named.GetMembers().Any(_ => _.Name == "Any"):
membersToMatch.Add((property.Name, named.Name, false, true));
break;
default:
membersToMatch.Add((property.Name, property.Type.Name, false, false));
break;
}
}
}
return new(targetSymbol.Name, !targetSymbol.MemberNames.Contains("AcceptVisitor"),
NeedsVisitor: !targetSymbol.IsAbstract && targetSymbol.BaseType!.IsAbstract,
NeedsNullNode: (bool)attribute.ConstructorArguments[0].Value!,
NeedsPatternPlaceholder: (bool)attribute.ConstructorArguments[1].Value!,
NullNodeBaseCtorParamCount: targetSymbol.InstanceConstructors.Min(m => m.Parameters.Length),
IsTypeNode: targetSymbol.Name == "AstType" || targetSymbol.BaseType?.Name == "AstType",
visitMethodName, paramTypeName, membersToMatch?.ToEquatableArray());
}
void WriteGeneratedMembers(SourceProductionContext context, AstNodeAdditions source)
{
var builder = new StringBuilder();
builder.AppendLine("namespace ICSharpCode.Decompiler.CSharp.Syntax;");
builder.AppendLine();
if (source.NeedsPatternPlaceholder)
{
builder.AppendLine("using ICSharpCode.Decompiler.CSharp.Syntax.PatternMatching;");
}
builder.AppendLine();
builder.AppendLine("#nullable enable");
builder.AppendLine();
builder.AppendLine($"partial class {source.NodeName}");
builder.AppendLine("{");
if (source.NeedsNullNode)
{
bool needsNew = source.NodeName != "AstNode";
builder.AppendLine($" {(needsNew ? "new " : "")}public static readonly {source.NodeName} Null = new Null{source.NodeName}();");
builder.AppendLine($@"
sealed class Null{source.NodeName} : {source.NodeName}
{{
public override NodeType NodeType => NodeType.Unknown;
public override bool IsNull => true;
public override void AcceptVisitor(IAstVisitor visitor)
{{
visitor.VisitNullNode(this);
}}
public override T AcceptVisitor<T>(IAstVisitor<T> visitor)
{{
return visitor.VisitNullNode(this);
}}
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
{{
return visitor.VisitNullNode(this, data);
}}
protected internal override bool DoMatch(AstNode? other, PatternMatching.Match match)
{{
return other == null || other.IsNull;
}}");
if (source.IsTypeNode)
{
builder.AppendLine(
$@"
public override Decompiler.TypeSystem.ITypeReference ToTypeReference(Resolver.NameLookupMode lookupMode, Decompiler.TypeSystem.InterningProvider? interningProvider = null)
{{
return Decompiler.TypeSystem.SpecialType.UnknownType;
}}"
);
}
if (source.NullNodeBaseCtorParamCount > 0)
{
builder.AppendLine($@"
public Null{source.NodeName}() : base({string.Join(", ", Enumerable.Repeat("default", source.NullNodeBaseCtorParamCount))}) {{ }}");
}
builder.AppendLine($@"
}}
");
}
if (source.NeedsPatternPlaceholder)
{
const string toTypeReferenceCode = @"
public override Decompiler.TypeSystem.ITypeReference ToTypeReference(Resolver.NameLookupMode lookupMode, Decompiler.TypeSystem.InterningProvider? interningProvider = null)
{
throw new System.NotSupportedException();
}";
builder.Append(
$@" public static implicit operator {source.NodeName}?(PatternMatching.Pattern? pattern)
{{
return pattern != null ? new PatternPlaceholder(pattern) : null;
}}
sealed class PatternPlaceholder : {source.NodeName}, INode
{{
readonly PatternMatching.Pattern child;
public PatternPlaceholder(PatternMatching.Pattern child)
{{
this.child = child;
}}
public override NodeType NodeType {{
get {{ return NodeType.Pattern; }}
}}
public override void AcceptVisitor(IAstVisitor visitor)
{{
visitor.VisitPatternPlaceholder(this, child);
}}
public override T AcceptVisitor<T>(IAstVisitor<T> visitor)
{{
return visitor.VisitPatternPlaceholder(this, child);
}}
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
{{
return visitor.VisitPatternPlaceholder(this, child, data);
}}
protected internal override bool DoMatch(AstNode? other, PatternMatching.Match match)
{{
return child.DoMatch(other, match);
}}
bool PatternMatching.INode.DoMatchCollection(Role? role, PatternMatching.INode? pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo)
{{
return child.DoMatchCollection(role, pos, match, backtrackingInfo);
}}{(source.NodeName == "AstType" ? toTypeReferenceCode : "")}
}}
"
);
}
if (source.NeedsAcceptImpls && source.NeedsVisitor)
{
builder.Append($@" public override void AcceptVisitor(IAstVisitor visitor)
{{
visitor.Visit{source.VisitMethodName}(this);
}}
public override T AcceptVisitor<T>(IAstVisitor<T> visitor)
{{
return visitor.Visit{source.VisitMethodName}(this);
}}
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
{{
return visitor.Visit{source.VisitMethodName}(this, data);
}}
");
}
if (source.MembersToMatch != null)
{
builder.Append($@" protected internal override bool DoMatch(AstNode? other, PatternMatching.Match match)
{{
return other is {source.NodeName} o && !o.IsNull");
foreach (var (member, typeName, recursive, hasAny) in source.MembersToMatch)
{
if (member == "MatchAttributesAndModifiers")
{
builder.Append($"\r\n\t\t\t&& this.MatchAttributesAndModifiers(o, match)");
}
else if (recursive)
{
builder.Append($"\r\n\t\t\t&& this.{member}.DoMatch(o.{member}, match)");
}
else if (hasAny)
{
builder.Append($"\r\n\t\t\t&& (this.{member} == {typeName}.Any || this.{member} == o.{member})");
}
else if (typeName == "String")
{
builder.Append($"\r\n\t\t\t&& MatchString(this.{member}, o.{member})");
}
else
{
builder.Append($"\r\n\t\t\t&& this.{member} == o.{member}");
}
}
builder.Append(@";
}
");
}
builder.AppendLine("}");
context.AddSource(source.NodeName + ".g.cs", SourceText.From(builder.ToString(), Encoding.UTF8));
}
void WriteVisitors(SourceProductionContext context, ImmutableArray<AstNodeAdditions> source)
{
var builder = new StringBuilder();
builder.AppendLine("namespace ICSharpCode.Decompiler.CSharp.Syntax;");
source = source
.Concat([new("NullNode", false, true, false, false, 0, false, "NullNode", "AstNode", null), new("PatternPlaceholder", false, true, false, false, 0, false, "PatternPlaceholder", "AstNode", null)])
.ToImmutableArray();
WriteInterface("IAstVisitor", "void", "");
WriteInterface("IAstVisitor<out S>", "S", "");
WriteInterface("IAstVisitor<in T, out S>", "S", ", T data");
context.AddSource("IAstVisitor.g.cs", SourceText.From(builder.ToString(), Encoding.UTF8));
void WriteInterface(string name, string ret, string param)
{
builder.AppendLine($"public interface {name}");
builder.AppendLine("{");
foreach (var type in source.OrderBy(t => t.VisitMethodName))
{
if (!type.NeedsVisitor)
continue;
string extParams, paramName;
if (type.VisitMethodName == "PatternPlaceholder")
{
paramName = "placeholder";
extParams = ", PatternMatching.Pattern pattern" + param;
}
else
{
paramName = char.ToLowerInvariant(type.VisitMethodName[0]) + type.VisitMethodName.Substring(1);
extParams = param;
}
builder.AppendLine($"\t{ret} Visit{type.VisitMethodName}({type.VisitMethodParamType} {paramName}{extParams});");
}
builder.AppendLine("}");
}
}
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var astNodeAdditions = context.SyntaxProvider.ForAttributeWithMetadataName(
"ICSharpCode.Decompiler.CSharp.Syntax.DecompilerAstNodeAttribute",
(n, ct) => n is ClassDeclarationSyntax,
GetAstNodeAdditions);
var visitorMembers = astNodeAdditions.Collect();
context
.RegisterPostInitializationOutput(i => i.AddSource("DecompilerSyntaxTreeGeneratorAttributes.g.cs", @"
using System;
namespace Microsoft.CodeAnalysis
{
internal sealed partial class EmbeddedAttribute : global::System.Attribute
{
}
}
namespace ICSharpCode.Decompiler.CSharp.Syntax
{
[global::Microsoft.CodeAnalysis.EmbeddedAttribute]
sealed class DecompilerAstNodeAttribute : global::System.Attribute
{
public DecompilerAstNodeAttribute(bool hasNullNode = false, bool hasPatternPlaceholder = false) { }
}
[global::Microsoft.CodeAnalysis.EmbeddedAttribute]
sealed class ExcludeFromMatchAttribute : global::System.Attribute
{
}
}
"));
context.RegisterSourceOutput(astNodeAdditions, WriteGeneratedMembers);
context.RegisterSourceOutput(visitorMembers, WriteVisitors);
}
}
readonly struct EquatableArray<T> : IEquatable<EquatableArray<T>>, IEnumerable<T>
where T : IEquatable<T>
{
readonly T[] array;
public EquatableArray(T[] array)
{
this.array = array ?? throw new ArgumentNullException(nameof(array));
}
public bool Equals(EquatableArray<T> other)
{
return other.array.AsSpan().SequenceEqual(this.array);
}
public IEnumerator<T> GetEnumerator()
{
return ((IEnumerable<T>)array).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return array.GetEnumerator();
}
}
static class EquatableArrayExtensions
{
public static EquatableArray<T> ToEquatableArray<T>(this List<T> array) where T : IEquatable<T>
{
return new EquatableArray<T>(array.ToArray());
}
}