Browse Source

Add typed child accessors over CSharpSlotInfo<T>

GetChild/GetChildren/SetChild take a typed CSharpSlotInfo<T> and infer the child
type from the slot, delegating to the existing kind-based lookups. Call sites can
pass a node's static slot (e.g. node.GetChild(PropertyDeclaration.GetterSlot))
instead of an explicit type argument plus a SlotKind.

Assisted-by: Claude:claude-opus-4-8:Claude Code
pull/3807/head
Siegfried Pammer 2 weeks ago committed by Siegfried Pammer
parent
commit
dc186be0a9
  1. 12
      ICSharpCode.Decompiler/CSharp/Syntax/AstNode.cs

12
ICSharpCode.Decompiler/CSharp/Syntax/AstNode.cs

@ -308,6 +308,12 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -308,6 +308,12 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
return null;
}
/// <summary>
/// Gets the first child occupying <paramref name="slot"/>, or null if the slot is empty (or the
/// node declares no such slot). The result type is inferred from the typed slot.
/// </summary>
public T? GetChild<T>(CSharpSlotInfo<T> slot) where T : AstNode => GetChildByRole<T>(slot.Kind);
public T? GetParent<T>() where T : AstNode
{
return Ancestors.OfType<T>().FirstOrDefault();
@ -327,11 +333,17 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -327,11 +333,17 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
return collection != null ? (AstNodeCollection<T>)collection : new AstNodeCollection<T>(this, kind);
}
/// <summary>Gets the collection occupying <paramref name="slot"/>; the element type is inferred from the slot.</summary>
public AstNodeCollection<T> GetChildren<T>(CSharpSlotInfo<T> slot) where T : AstNode => GetChildrenByRole<T>(slot.Kind);
protected void SetChildByRole<T>(SlotKind kind, T? newChild) where T : AstNode
{
SetChildByRoleUntyped(kind, newChild);
}
/// <summary>Sets the single child occupying <paramref name="slot"/>; the child type is inferred from the slot.</summary>
protected void SetChild<T>(CSharpSlotInfo<T> slot, T? newChild) where T : AstNode => SetChildByRole(slot.Kind, newChild);
#region Slot storage contract
// Each concrete node's slots form a flattened child-index space, in source-declaration order.
// A single slot occupies exactly one index (even when empty, where GetChild returns null); a

Loading…
Cancel
Save