Browse Source

Extract syntax tree generator emit helpers

Reduce nested generation routines by moving constructor, child-dispatch, slot, and slot-kind emission into focused helpers. This keeps generator behavior intact while making the source-generator control flow easier to review and maintain.

Assisted-by: OpenCode:openai/gpt-5.5:OpenCode
pull/3829/head
Siegfried Pammer 5 days ago committed by Siegfried Pammer
parent
commit
0d2a137f02
  1. 382
      ICSharpCode.Decompiler.Generators/DecompilerSyntaxTreeGenerator.cs

382
ICSharpCode.Decompiler.Generators/DecompilerSyntaxTreeGenerator.cs

@ -220,6 +220,32 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator
{ {
var builder = new StringBuilder(); var builder = new StringBuilder();
WriteGeneratedMembersHeader(builder, source);
WritePatternPlaceholder(builder, source);
WriteVisitorOverrides(builder, source);
WriteDoMatch(builder, source);
if (source.Slots is { } slotsArray)
{
var slots = slotsArray.ToList();
WriteSlotProperties(builder, slots);
WriteNameAccessors(builder, source.NameAccessors);
WriteConstructors(builder, source, slots);
WriteChildAccessors(builder, slots);
WriteSlotInfoFields(builder, slots);
WriteChildSlotInfo(builder, slots);
WriteCollectionLookup(builder, slots);
WriteCloneChildrenInto(builder, source, slots);
}
// Close the class, trimming the blank line the per-member spacer leaves before the brace.
string body = builder.ToString().TrimEnd() + "\n}\n";
context.AddSource(source.NodeName + ".g.cs", SourceText.From(body.Replace("\r\n", "\n"), Encoding.UTF8));
}
static void WriteGeneratedMembersHeader(StringBuilder builder, AstNodeAdditions source)
{
builder.AppendLine("// <auto-generated/>"); builder.AppendLine("// <auto-generated/>");
builder.AppendLine("#nullable enable"); builder.AppendLine("#nullable enable");
builder.AppendLine(); builder.AppendLine();
@ -237,9 +263,13 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator
builder.AppendLine($"partial class {source.NodeName}"); builder.AppendLine($"partial class {source.NodeName}");
builder.AppendLine("{"); builder.AppendLine("{");
}
if (source.NeedsPatternPlaceholder) static void WritePatternPlaceholder(StringBuilder builder, AstNodeAdditions source)
{ {
if (!source.NeedsPatternPlaceholder)
return;
// The placeholder conversion is part of the pattern-construction DSL, where a non-null // The placeholder conversion is part of the pattern-construction DSL, where a non-null
// pattern is the invariant; the result is therefore non-nullable for the specific node // pattern is the invariant; the result is therefore non-nullable for the specific node
// types. AstNode (the base) and ParameterDeclaration keep the nullable contract, and only // types. AstNode (the base) and ParameterDeclaration keep the nullable contract, and only
@ -292,8 +322,11 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator
); );
} }
if (source.NeedsVisitor) static void WriteVisitorOverrides(StringBuilder builder, AstNodeAdditions source)
{ {
if (!source.NeedsVisitor)
return;
builder.Append($@" public override void AcceptVisitor(IAstVisitor visitor) builder.Append($@" public override void AcceptVisitor(IAstVisitor visitor)
{{ {{
visitor.Visit{source.VisitMethodName}(this); visitor.Visit{source.VisitMethodName}(this);
@ -312,40 +345,17 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator
"); ");
} }
if (source.MembersToMatch != null) static void WriteDoMatch(StringBuilder builder, AstNodeAdditions source)
{ {
if (source.MembersToMatch == null)
return;
builder.Append($@" protected internal override bool DoMatch(AstNode? other, PatternMatching.Match match) builder.Append($@" protected internal override bool DoMatch(AstNode? other, PatternMatching.Match match)
{{ {{
return other is {source.NodeName} o"); return other is {source.NodeName} o");
foreach (var (member, typeName, recursive, hasAny, nullable) in source.MembersToMatch) foreach (var (member, typeName, recursive, hasAny, nullable) in source.MembersToMatch)
{ builder.Append(DoMatchTerm(member, typeName, recursive, hasAny, nullable));
if (member == "MatchAttributesAndModifiers")
{
builder.Append($"\r\n\t\t\t&& this.MatchAttributesAndModifiers(o, match)");
}
else if (recursive && nullable && typeName != "AstNodeCollection")
{
// An optional single-value child is null when absent; match null-safely.
builder.Append($"\r\n\t\t\t&& MatchOptional(this.{member}, o.{member}, 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.Append(@";
} }
@ -353,10 +363,24 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator
"); ");
} }
if (source.Slots is { } slotsArray) static string DoMatchTerm(string member, string typeName, bool recursive, bool hasAny, bool nullable)
{ {
var slots = slotsArray.ToList(); if (member == "MatchAttributesAndModifiers")
return $"\r\n\t\t\t&& this.MatchAttributesAndModifiers(o, match)";
// An optional single-value child is null when absent; match null-safely.
if (recursive && nullable && typeName != "AstNodeCollection")
return $"\r\n\t\t\t&& MatchOptional(this.{member}, o.{member}, match)";
if (recursive)
return $"\r\n\t\t\t&& this.{member}.DoMatch(o.{member}, match)";
if (hasAny)
return $"\r\n\t\t\t&& (this.{member} == {typeName}.Any || this.{member} == o.{member})";
if (typeName == "String")
return $"\r\n\t\t\t&& MatchString(this.{member}, o.{member})";
return $"\r\n\t\t\t&& this.{member} == o.{member}";
}
static void WriteSlotProperties(StringBuilder builder, List<SlotInfo> slots)
{
// Backing fields and the partial-property bodies. A single slot stores a nullable backing // Backing fields and the partial-property bodies. A single slot stores a nullable backing
// field (returned null-forgiving for a required child, nullable for an optional one); a // field (returned null-forgiving for a required child, nullable for an optional one); a
// collection slot owns a lazily created AstNodeCollection bound to this node. A slot re-declared from an inherited contract // collection slot owns a lazily created AstNodeCollection bound to this node. A slot re-declared from an inherited contract
@ -367,6 +391,10 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator
// preceding slots are single children, one index each). // preceding slots are single children, one index each).
int collectionCount = slots.Count(s => s.IsCollection); int collectionCount = slots.Count(s => s.IsCollection);
for (int slotIndex = 0; slotIndex < slots.Count; slotIndex++) for (int slotIndex = 0; slotIndex < slots.Count; slotIndex++)
WriteSlotProperty(builder, slots, slotIndex, collectionCount);
}
static void WriteSlotProperty(StringBuilder builder, List<SlotInfo> slots, int slotIndex, int collectionCount)
{ {
var (isCollection, name, type, elementType, isOverride, isNullable, kindName, isPartial) = slots[slotIndex]; var (isCollection, name, type, elementType, isOverride, isNullable, kindName, isPartial) = slots[slotIndex];
string field = FieldName(name); string field = FieldName(name);
@ -397,10 +425,17 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator
builder.AppendLine(); builder.AppendLine();
} }
// A string [Slot] is a convenience accessor over its generated Identifier token slot. static void WriteNameAccessors(StringBuilder builder, EquatableArray<NameAccessor>? nameAccessors)
if (source.NameAccessors is { } nameAccessorsArray)
{ {
// A string [Slot] is a convenience accessor over its generated Identifier token slot.
if (nameAccessors is not { } nameAccessorsArray)
return;
foreach (var (stringName, tokenName, isOptional) in nameAccessorsArray) foreach (var (stringName, tokenName, isOptional) in nameAccessorsArray)
WriteNameAccessor(builder, stringName, tokenName, isOptional);
}
static void WriteNameAccessor(StringBuilder builder, string stringName, string tokenName, bool isOptional)
{ {
// The token factory is the type 'Identifier'. A [Slot] string property literally named // The token factory is the type 'Identifier'. A [Slot] string property literally named
// "Identifier" (e.g. SimpleType.Identifier) shadows that type inside its own setter, so the // "Identifier" (e.g. SimpleType.Identifier) shadows that type inside its own setter, so the
@ -425,8 +460,9 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator
builder.AppendLine("\t}"); builder.AppendLine("\t}");
builder.AppendLine(); builder.AppendLine();
} }
}
static void WriteConstructors(StringBuilder builder, AstNodeAdditions source, List<SlotInfo> slots)
{
// Constructors. Parameters follow member source order and cover single/collection [Slot] children, // Constructors. Parameters follow member source order and cover single/collection [Slot] children,
// the string [Slot], and settable enum scalars (Operator, FieldDirection, ...); a collection is // the string [Slot], and settable enum scalars (Operator, FieldDirection, ...); a collection is
// an IEnumerable<T> param in its declared position. We emit the empty ctor (for object-initializer // an IEnumerable<T> param in its declared position. We emit the empty ctor (for object-initializer
@ -434,56 +470,94 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator
// each collection, and one with all params. A params T[] overload is added when a ctor's last param // each collection, and one with all params. A params T[] overload is added when a ctor's last param
// is the collection. Pure-scalar nodes (no [Slot], e.g. PrimitiveExpression) are excluded // is the collection. Pure-scalar nodes (no [Slot], e.g. PrimitiveExpression) are excluded
// because their non-enum state (a literal value) is invisible here; those keep hand-written ctors. // because their non-enum state (a literal value) is invisible here; those keep hand-written ctors.
if (!source.IsAbstract && source.BaseHasDefaultConstructor && slots.Count > 0 && source.CtorParams is { } ctorParamsArray) if (source.IsAbstract || !source.BaseHasDefaultConstructor || slots.Count == 0 || source.CtorParams is not { } ctorParamsArray)
{ return;
var cp = ctorParamsArray.ToList();
string ParamName(string n)
{
string p = char.ToLowerInvariant(n[0]) + n.Substring(1);
return SyntaxFacts.GetKeywordKind(p) != SyntaxKind.None ? "@" + p : p;
}
string ParamType(int i) => cp[i].IsCollection
? $"IEnumerable<{cp[i].ElementType}>"
: cp[i].ParamType;
var cp = ctorParamsArray.ToList();
builder.AppendLine($"\tpublic {source.NodeName}()"); builder.AppendLine($"\tpublic {source.NodeName}()");
builder.AppendLine("\t{"); builder.AppendLine("\t{");
builder.AppendLine("\t}"); builder.AppendLine("\t}");
builder.AppendLine(); builder.AppendLine();
int reqLen = RequiredConstructorPrefixLength(cp);
int prev = 0;
foreach (int len in ConstructorPrefixLengths(cp, reqLen))
{
if (len <= 0)
continue;
WriteConstructorPrefix(builder, source.NodeName, cp, len, prev, paramsForm: false);
if (cp[len - 1].IsCollection)
WriteConstructorPrefix(builder, source.NodeName, cp, len, len, paramsForm: true);
prev = len;
}
}
static int RequiredConstructorPrefixLength(List<CtorParam> cp)
{
// Required prefix: through the last non-optional param (an optional param before it is still // Required prefix: through the last non-optional param (an optional param before it is still
// positionally included so the required param after it can be passed). // positionally included so the required param after it can be passed).
int reqLen = 0; int reqLen = 0;
for (int i = 0; i < cp.Count; i++) for (int i = 0; i < cp.Count; i++)
if (!cp[i].IsOptional) if (!cp[i].IsOptional)
reqLen = i + 1; reqLen = i + 1;
return reqLen;
}
static SortedSet<int> ConstructorPrefixLengths(List<CtorParam> cp, int reqLen)
{
var lengths = new SortedSet<int>();
if (reqLen > 0)
lengths.Add(reqLen);
for (int i = 0; i < cp.Count; i++)
if (cp[i].IsCollection && i + 1 >= reqLen)
lengths.Add(i + 1);
lengths.Add(cp.Count);
return lengths;
}
static void WriteConstructorPrefix(StringBuilder builder, string nodeName, List<CtorParam> cp, int len, int chainLen, bool paramsForm)
{
// A normal prefix ctor forwards to the previous (shorter) emitted prefix via : this(...) and only // A normal prefix ctor forwards to the previous (shorter) emitted prefix via : this(...) and only
// sets the params between them; the shortest sets its params directly. A params T[] overload // sets the params between them; the shortest sets its params directly. A params T[] overload
// forwards to the IEnumerable overload of the same length. // forwards to the IEnumerable overload of the same length.
void EmitPrefix(int len, int chainLen, bool paramsForm) builder.AppendLine($"\tpublic {nodeName}({string.Join(", ", ConstructorParameterDeclarations(cp, len, paramsForm))})");
if (paramsForm)
WriteParamsConstructorBody(builder, cp, len);
else
WriteNormalConstructorBody(builder, cp, len, chainLen);
builder.AppendLine();
}
static IEnumerable<string> ConstructorParameterDeclarations(List<CtorParam> cp, int len, bool paramsForm)
{ {
var decls = new List<string>();
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
{ {
if (paramsForm && i == len - 1) if (paramsForm && i == len - 1)
decls.Add($"params {cp[i].ElementType}[] {ParamName(cp[i].PropertyName)}"); yield return $"params {cp[i].ElementType}[] {ParamName(cp[i].PropertyName)}";
else else
decls.Add($"{ParamType(i)} {ParamName(cp[i].PropertyName)}"); yield return $"{ParamType(cp[i])} {ParamName(cp[i].PropertyName)}";
} }
builder.AppendLine($"\tpublic {source.NodeName}({string.Join(", ", decls)})"); }
if (paramsForm)
static void WriteParamsConstructorBody(StringBuilder builder, List<CtorParam> cp, int len)
{ {
var args = new List<string>(); builder.AppendLine($"\t\t: this({string.Join(", ", ParamsConstructorArguments(cp, len))})");
for (int i = 0; i < len; i++)
args.Add(i == len - 1
? $"(IEnumerable<{cp[i].ElementType}>){ParamName(cp[i].PropertyName)}"
: ParamName(cp[i].PropertyName));
builder.AppendLine($"\t\t: this({string.Join(", ", args)})");
builder.AppendLine("\t{"); builder.AppendLine("\t{");
builder.AppendLine("\t}"); builder.AppendLine("\t}");
} }
static IEnumerable<string> ParamsConstructorArguments(List<CtorParam> cp, int len)
{
for (int i = 0; i < len; i++)
{
if (i == len - 1)
yield return $"(IEnumerable<{cp[i].ElementType}>){ParamName(cp[i].PropertyName)}";
else else
yield return ParamName(cp[i].PropertyName);
}
}
static void WriteNormalConstructorBody(StringBuilder builder, List<CtorParam> cp, int len, int chainLen)
{ {
if (chainLen > 0) if (chainLen > 0)
builder.AppendLine($"\t\t: this({string.Join(", ", Enumerable.Range(0, chainLen).Select(i => ParamName(cp[i].PropertyName)))})"); builder.AppendLine($"\t\t: this({string.Join(", ", Enumerable.Range(0, chainLen).Select(i => ParamName(cp[i].PropertyName)))})");
@ -497,28 +571,17 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator
} }
builder.AppendLine("\t}"); builder.AppendLine("\t}");
} }
builder.AppendLine();
}
var lengths = new SortedSet<int>(); static string ParamName(string n)
if (reqLen > 0)
lengths.Add(reqLen);
for (int i = 0; i < cp.Count; i++)
if (cp[i].IsCollection && i + 1 >= reqLen)
lengths.Add(i + 1);
lengths.Add(cp.Count);
int prev = 0;
foreach (int len in lengths)
{ {
if (len <= 0) string p = char.ToLowerInvariant(n[0]) + n.Substring(1);
continue; return SyntaxFacts.GetKeywordKind(p) != SyntaxKind.None ? "@" + p : p;
EmitPrefix(len, prev, paramsForm: false);
if (cp[len - 1].IsCollection)
EmitPrefix(len, len, paramsForm: true);
prev = len;
}
} }
static string ParamType(CtorParam cp) => cp.IsCollection ? $"IEnumerable<{cp.ElementType}>" : cp.ParamType;
static void WriteChildAccessors(StringBuilder builder, List<SlotInfo> slots)
{
// Flattened child-index space: slots in declaration order, a single slot occupying one index // Flattened child-index space: slots in declaration order, a single slot occupying one index
// (even when empty), a collection slot a contiguous run of its current length. // (even when empty), a collection slot a contiguous run of its current length.
var countTerms = new List<string>(); var countTerms = new List<string>();
@ -530,15 +593,35 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator
builder.AppendLine($"\tinternal override int GetChildCount() => {string.Join(" + ", countTerms)};"); builder.AppendLine($"\tinternal override int GetChildCount() => {string.Join(" + ", countTerms)};");
builder.AppendLine(); builder.AppendLine();
bool anyCollection = slots.Any(s => s.IsCollection); builder.AppendLine("\tinternal override AstNode? GetChild(int index)");
builder.AppendLine("\t{");
WriteReturnDispatch(builder, slots, k => FieldName(slots[k].PropertyName), k => $"{FieldName(slots[k].PropertyName)}![i]");
builder.AppendLine("\t}");
builder.AppendLine();
builder.AppendLine("\tinternal override void SetChild(int index, AstNode? value)");
builder.AppendLine("\t{");
if (slots.Any(s => s.IsCollection))
WriteSetChildWithCollections(builder, slots);
else
WriteSetChildSwitch(builder, slots);
builder.AppendLine("\t}");
builder.AppendLine();
}
static void WriteReturnDispatch(StringBuilder builder, List<SlotInfo> slots, Func<int, string> singleExpr, Func<int, string> collectionExpr)
{
// Emits a method body that maps a flat child index to a slot and returns an expression for it. // Emits a method body that maps a flat child index to a slot and returns an expression for it.
// With only single slots the index is a constant offset, so a switch reads best; once a // With only single slots the index is a constant offset, so a switch reads best; once a
// collection slot is present the widths are dynamic, so walk the slots subtracting each one's // collection slot is present the widths are dynamic, so walk the slots subtracting each one's
// length from a running index. // length from a running index.
void EmitReturnDispatch(Func<int, string> singleExpr, Func<int, string> collectionExpr) if (slots.Any(s => s.IsCollection))
{ WriteReturnDispatchWithCollections(builder, slots, singleExpr, collectionExpr);
if (!anyCollection) else
WriteReturnDispatchSwitch(builder, slots, singleExpr);
}
static void WriteReturnDispatchSwitch(StringBuilder builder, List<SlotInfo> slots, Func<int, string> singleExpr)
{ {
builder.AppendLine("\t\tswitch (index)"); builder.AppendLine("\t\tswitch (index)");
builder.AppendLine("\t\t{"); builder.AppendLine("\t\t{");
@ -550,43 +633,43 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator
builder.AppendLine("\t\t\tdefault:"); builder.AppendLine("\t\t\tdefault:");
builder.AppendLine("\t\t\t\tthrow new System.ArgumentOutOfRangeException(nameof(index));"); builder.AppendLine("\t\t\t\tthrow new System.ArgumentOutOfRangeException(nameof(index));");
builder.AppendLine("\t\t}"); builder.AppendLine("\t\t}");
return;
} }
static void WriteReturnDispatchWithCollections(StringBuilder builder, List<SlotInfo> slots, Func<int, string> singleExpr, Func<int, string> collectionExpr)
{
builder.AppendLine("\t\tint i = index;"); builder.AppendLine("\t\tint i = index;");
for (int k = 0; k < slots.Count; k++) for (int k = 0; k < slots.Count; k++)
{ {
bool last = k == slots.Count - 1; bool last = k == slots.Count - 1;
if (slots[k].IsCollection) if (slots[k].IsCollection)
WriteCollectionReturnDispatchStep(builder, slots[k], k, collectionExpr, last);
else
WriteSingleReturnDispatchStep(builder, k, singleExpr, last);
}
builder.AppendLine("\t\tthrow new System.ArgumentOutOfRangeException(nameof(index));");
}
static void WriteCollectionReturnDispatchStep(StringBuilder builder, SlotInfo slot, int index, Func<int, string> collectionExpr, bool last)
{ {
string field = FieldName(slots[k].PropertyName); string field = FieldName(slot.PropertyName);
builder.AppendLine("\t\t{"); builder.AppendLine("\t\t{");
builder.AppendLine($"\t\t\tint n = {field}?.Count ?? 0;"); builder.AppendLine($"\t\t\tint n = {field}?.Count ?? 0;");
builder.AppendLine("\t\t\tif (i < n)"); builder.AppendLine("\t\t\tif (i < n)");
builder.AppendLine($"\t\t\t\treturn {collectionExpr(k)};"); builder.AppendLine($"\t\t\t\treturn {collectionExpr(index)};");
if (!last) if (!last)
builder.AppendLine("\t\t\ti -= n;"); builder.AppendLine("\t\t\ti -= n;");
builder.AppendLine("\t\t}"); builder.AppendLine("\t\t}");
} }
else
static void WriteSingleReturnDispatchStep(StringBuilder builder, int index, Func<int, string> singleExpr, bool last)
{ {
builder.AppendLine("\t\tif (i == 0)"); builder.AppendLine("\t\tif (i == 0)");
builder.AppendLine($"\t\t\treturn {singleExpr(k)};"); builder.AppendLine($"\t\t\treturn {singleExpr(index)};");
if (!last) if (!last)
builder.AppendLine("\t\ti--;"); builder.AppendLine("\t\ti--;");
} }
}
builder.AppendLine("\t\tthrow new System.ArgumentOutOfRangeException(nameof(index));");
}
builder.AppendLine("\tinternal override AstNode? GetChild(int index)"); static void WriteSetChildSwitch(StringBuilder builder, List<SlotInfo> slots)
builder.AppendLine("\t{");
EmitReturnDispatch(k => FieldName(slots[k].PropertyName), k => $"{FieldName(slots[k].PropertyName)}![i]");
builder.AppendLine("\t}");
builder.AppendLine();
builder.AppendLine("\tinternal override void SetChild(int index, AstNode? value)");
builder.AppendLine("\t{");
if (!anyCollection)
{ {
builder.AppendLine("\t\tswitch (index)"); builder.AppendLine("\t\tswitch (index)");
builder.AppendLine("\t\t{"); builder.AppendLine("\t\t{");
@ -600,57 +683,72 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator
builder.AppendLine("\t\t\t\tthrow new System.ArgumentOutOfRangeException(nameof(index));"); builder.AppendLine("\t\t\t\tthrow new System.ArgumentOutOfRangeException(nameof(index));");
builder.AppendLine("\t\t}"); builder.AppendLine("\t\t}");
} }
else
static void WriteSetChildWithCollections(StringBuilder builder, List<SlotInfo> slots)
{ {
builder.AppendLine("\t\tint i = index;"); builder.AppendLine("\t\tint i = index;");
for (int k = 0; k < slots.Count; k++) for (int k = 0; k < slots.Count; k++)
{ {
bool last = k == slots.Count - 1; bool last = k == slots.Count - 1;
var s = slots[k]; var s = slots[k];
string field = FieldName(s.PropertyName);
if (s.IsCollection) if (s.IsCollection)
WriteSetCollectionChildStep(builder, s, last);
else
WriteSetSingleChildStep(builder, s, last);
}
builder.AppendLine("\t\tthrow new System.ArgumentOutOfRangeException(nameof(index));");
}
static void WriteSetCollectionChildStep(StringBuilder builder, SlotInfo slot, bool last)
{ {
string field = FieldName(slot.PropertyName);
builder.AppendLine("\t\t{"); builder.AppendLine("\t\t{");
builder.AppendLine($"\t\t\tint n = {field}?.Count ?? 0;"); builder.AppendLine($"\t\t\tint n = {field}?.Count ?? 0;");
builder.AppendLine("\t\t\tif (i < n)"); builder.AppendLine("\t\t\tif (i < n)");
builder.AppendLine("\t\t\t{"); builder.AppendLine("\t\t\t{");
builder.AppendLine($"\t\t\t\t{field}![i] = ({s.ElementType})value!;"); builder.AppendLine($"\t\t\t\t{field}![i] = ({slot.ElementType})value!;");
builder.AppendLine("\t\t\t\treturn;"); builder.AppendLine("\t\t\t\treturn;");
builder.AppendLine("\t\t\t}"); builder.AppendLine("\t\t\t}");
if (!last) if (!last)
builder.AppendLine("\t\t\ti -= n;"); builder.AppendLine("\t\t\ti -= n;");
builder.AppendLine("\t\t}"); builder.AppendLine("\t\t}");
} }
else
static void WriteSetSingleChildStep(StringBuilder builder, SlotInfo slot, bool last)
{ {
string field = FieldName(slot.PropertyName);
builder.AppendLine("\t\tif (i == 0)"); builder.AppendLine("\t\tif (i == 0)");
builder.AppendLine("\t\t{"); builder.AppendLine("\t\t{");
builder.AppendLine($"\t\t\tSetChildNode(ref {field}, ({s.PropertyType}?)value, index);"); builder.AppendLine($"\t\t\tSetChildNode(ref {field}, ({slot.PropertyType}?)value, index);");
builder.AppendLine("\t\t\treturn;"); builder.AppendLine("\t\t\treturn;");
builder.AppendLine("\t\t}"); builder.AppendLine("\t\t}");
if (!last) if (!last)
builder.AppendLine("\t\ti--;"); builder.AppendLine("\t\ti--;");
} }
}
builder.AppendLine("\t\tthrow new System.ArgumentOutOfRangeException(nameof(index));");
}
builder.AppendLine("\t}");
builder.AppendLine();
static void WriteSlotInfoFields(StringBuilder builder, List<SlotInfo> slots)
{
// One typed CSharpSlotInfo<T> static per slot; node.Slot compares against these by object // One typed CSharpSlotInfo<T> static per slot; node.Slot compares against these by object
// identity, and the typed child accessors infer the child type from the slot. // identity, and the typed child accessors infer the child type from the slot.
foreach (var s in slots) foreach (var s in slots)
builder.AppendLine($"\tpublic static readonly CSharpSlotInfo<{s.ElementType}> {s.PropertyName}Slot = new CSharpSlotInfo<{s.ElementType}>(\"{s.PropertyName}\", {(s.IsCollection ? "true" : "false")}, Slots.{s.KindName}, {(s.IsCollection || s.IsNullable ? "true" : "false")});"); builder.AppendLine($"\tpublic static readonly CSharpSlotInfo<{s.ElementType}> {s.PropertyName}Slot = new CSharpSlotInfo<{s.ElementType}>(\"{s.PropertyName}\", {(s.IsCollection ? "true" : "false")}, Slots.{s.KindName}, {(s.IsCollection || s.IsNullable ? "true" : "false")});");
builder.AppendLine(); builder.AppendLine();
}
static void WriteChildSlotInfo(StringBuilder builder, List<SlotInfo> slots)
{
builder.AppendLine("\tinternal override CSharpSlotInfo GetChildSlotInfo(int index)"); builder.AppendLine("\tinternal override CSharpSlotInfo GetChildSlotInfo(int index)");
builder.AppendLine("\t{"); builder.AppendLine("\t{");
EmitReturnDispatch(k => $"{slots[k].PropertyName}Slot", k => $"{slots[k].PropertyName}Slot"); WriteReturnDispatch(builder, slots, k => $"{slots[k].PropertyName}Slot", k => $"{slots[k].PropertyName}Slot");
builder.AppendLine("\t}"); builder.AppendLine("\t}");
builder.AppendLine(); builder.AppendLine();
}
if (slots.Any(s => s.IsCollection)) static void WriteCollectionLookup(StringBuilder builder, List<SlotInfo> slots)
{ {
if (!slots.Any(s => s.IsCollection))
return;
builder.AppendLine("\tinternal override AstNodeCollection? GetCollectionByKind(CSharpSlotInfo kind)"); builder.AppendLine("\tinternal override AstNodeCollection? GetCollectionByKind(CSharpSlotInfo kind)");
builder.AppendLine("\t{"); builder.AppendLine("\t{");
foreach (var s in slots.Where(s => s.IsCollection)) foreach (var s in slots.Where(s => s.IsCollection))
@ -660,6 +758,8 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator
builder.AppendLine(); builder.AppendLine();
} }
static void WriteCloneChildrenInto(StringBuilder builder, AstNodeAdditions source, List<SlotInfo> slots)
{
builder.AppendLine("\tinternal override void CloneChildrenInto(AstNode copyNode)"); builder.AppendLine("\tinternal override void CloneChildrenInto(AstNode copyNode)");
builder.AppendLine("\t{"); builder.AppendLine("\t{");
builder.AppendLine($"\t\tvar copy = ({source.NodeName})copyNode;"); builder.AppendLine($"\t\tvar copy = ({source.NodeName})copyNode;");
@ -675,18 +775,14 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator
builder.AppendLine($"\t\t\t\tcopy.{s.PropertyName}.Add(({s.ElementType})c.Clone());"); builder.AppendLine($"\t\t\t\tcopy.{s.PropertyName}.Add(({s.ElementType})c.Clone());");
} }
else else
{
builder.AppendLine($"\t\tif ({field} != null) copy.{s.PropertyName} = ({s.PropertyType}){field}.Clone();"); builder.AppendLine($"\t\tif ({field} != null) copy.{s.PropertyName} = ({s.PropertyType}){field}.Clone();");
} }
}
builder.AppendLine("\t}"); builder.AppendLine("\t}");
builder.AppendLine(); builder.AppendLine();
} }
// Close the class, trimming the blank line the per-member spacer leaves before the brace.
string body = builder.ToString().TrimEnd() + "\n}\n";
context.AddSource(source.NodeName + ".g.cs", SourceText.From(body.Replace("\r\n", "\n"), Encoding.UTF8));
}
void WriteVisitors(SourceProductionContext context, ImmutableArray<AstNodeAdditions> source) void WriteVisitors(SourceProductionContext context, ImmutableArray<AstNodeAdditions> source)
{ {
var builder = new StringBuilder(); var builder = new StringBuilder();
@ -791,6 +887,20 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
// back at it, and consumers compare node.Slot.Kind == Slots.X by object identity -- shared across node // back at it, and consumers compare node.Slot.Kind == Slots.X by object identity -- shared across node
// types, replacing the old polymorphic node.Role == Roles.X comparisons. // types, replacing the old polymorphic node.Role == Roles.X comparisons.
void WriteSlotKinds(SourceProductionContext context, ImmutableArray<AstNodeAdditions> source) void WriteSlotKinds(SourceProductionContext context, ImmutableArray<AstNodeAdditions> source)
{
var (kindTypes, kindIsCollection) = CollectSlotKinds(source);
// A slot kind names one child position, so it maps to a single child type (DSTG001 enforces this);
// the typed Slots constant therefore always carries that precise type. A kind that is a collection
// on one node and a single (or optional) child on another keeps a single type but no single arity,
// so its hard-coded IsCollection/isOptional flags are not authoritative -- the precise per-position
// flags live on the per-node slots, which is where consumers read them; the shared constant carries
// identity (and the now-precise child type), not those flags.
ReportSlotKindTypeConflicts(context, kindTypes);
WriteSlotKindsSource(context, kindTypes, kindIsCollection);
}
static (SortedDictionary<string, SortedSet<string>> kindTypes, Dictionary<string, bool?> kindIsCollection) CollectSlotKinds(ImmutableArray<AstNodeAdditions> source)
{ {
// Per kind: the element types seen (to choose a typed slot's T) and whether it is a collection. // Per kind: the element types seen (to choose a typed slot's T) and whether it is a collection.
// kindIsCollection is null once a kind is seen as a collection on one node and a single child on // kindIsCollection is null once a kind is seen as a collection on one node and a single child on
@ -799,34 +909,42 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
var kindTypes = new SortedDictionary<string, SortedSet<string>>(StringComparer.Ordinal); var kindTypes = new SortedDictionary<string, SortedSet<string>>(StringComparer.Ordinal);
var kindIsCollection = new Dictionary<string, bool?>(); var kindIsCollection = new Dictionary<string, bool?>();
foreach (var node in source) foreach (var node in source)
CollectNodeSlotKinds(node, kindTypes, kindIsCollection);
return (kindTypes, kindIsCollection);
}
static void CollectNodeSlotKinds(AstNodeAdditions node, SortedDictionary<string, SortedSet<string>> kindTypes, Dictionary<string, bool?> kindIsCollection)
{ {
if (node.Slots is { } slots) if (node.Slots is not { } slots)
{ return;
foreach (var s in slots) foreach (var s in slots)
{ CollectSlotKind(s, kindTypes, kindIsCollection);
if (!kindTypes.TryGetValue(s.KindName, out var set))
kindTypes[s.KindName] = set = new SortedSet<string>(StringComparer.Ordinal);
set.Add(s.ElementType);
if (!kindIsCollection.TryGetValue(s.KindName, out var arity))
kindIsCollection[s.KindName] = s.IsCollection;
else if (arity is bool b && b != s.IsCollection)
kindIsCollection[s.KindName] = null;
}
} }
static void CollectSlotKind(SlotInfo slot, SortedDictionary<string, SortedSet<string>> kindTypes, Dictionary<string, bool?> kindIsCollection)
{
if (!kindTypes.TryGetValue(slot.KindName, out var set))
kindTypes[slot.KindName] = set = new SortedSet<string>(StringComparer.Ordinal);
set.Add(slot.ElementType);
if (!kindIsCollection.TryGetValue(slot.KindName, out var arity))
kindIsCollection[slot.KindName] = slot.IsCollection;
else if (arity is bool b && b != slot.IsCollection)
kindIsCollection[slot.KindName] = null;
} }
// A slot kind names one child position, so it maps to a single child type (DSTG001 enforces this); void ReportSlotKindTypeConflicts(SourceProductionContext context, SortedDictionary<string, SortedSet<string>> kindTypes)
// the typed Slots constant therefore always carries that precise type. A kind that is a collection {
// on one node and a single (or optional) child on another keeps a single type but no single arity,
// so its hard-coded IsCollection/isOptional flags are not authoritative -- the precise per-position
// flags live on the per-node slots, which is where consumers read them; the shared constant carries
// identity (and the now-precise child type), not those flags.
foreach (var kv in kindTypes) foreach (var kv in kindTypes)
{ {
if (kv.Value.Count > 1) if (kv.Value.Count > 1)
context.ReportDiagnostic(Diagnostic.Create(MultipleChildTypesForKind, Location.None, kv.Key, string.Join(", ", kv.Value))); context.ReportDiagnostic(Diagnostic.Create(MultipleChildTypesForKind, Location.None, kv.Key, string.Join(", ", kv.Value)));
} }
}
static void WriteSlotKindsSource(SourceProductionContext context, SortedDictionary<string, SortedSet<string>> kindTypes, Dictionary<string, bool?> kindIsCollection)
{
var builder = new StringBuilder(); var builder = new StringBuilder();
builder.AppendLine("// <auto-generated/>"); builder.AppendLine("// <auto-generated/>");
builder.AppendLine(); builder.AppendLine();

Loading…
Cancel
Save