Skip to content

Commit 331ecce

Browse files
Copilotdadhi
andauthored
Document flat parameter declaration and usage shapes
Agent-Logs-Url: https://github.com/dadhi/FastExpressionCompiler/sessions/cec4f4cc-6325-4330-b621-6a290f3fef7e Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>
1 parent 1484adf commit 331ecce

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

src/FastExpressionCompiler.LightExpression/FlatExpression.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,12 @@ public int Block(params int[] expressions) =>
338338
/// <summary>Adds a block node with optional explicit result type and variables.</summary>
339339
public int Block(Type type, IEnumerable<int> variables, params int[] expressions)
340340
{
341+
// Block shape:
342+
// - without variables: children = [body-expressions-child-list]
343+
// - with variables: children = [declarations-child-list, body-expressions-child-list]
344+
// The declarations child list contains parameter declaration nodes.
345+
// Each declaration node is later bound to this block via ChildIdx = blockIndex and ChildCount = declaration position.
346+
// Parameter references inside the body are separate usage nodes with ChildIdx = declaration index.
341347
if (expressions == null || expressions.Length == 0)
342348
throw new ArgumentException("Block should contain at least one expression.", nameof(expressions));
343349

@@ -367,6 +373,10 @@ public int Lambda<TDelegate>(int body, params int[] parameters) where TDelegate
367373
/// <summary>Adds a lambda node.</summary>
368374
public int Lambda(Type delegateType, int body, params int[] parameters)
369375
{
376+
// Lambda shape: children = [body, declaration0, declaration1, ...]
377+
// The lambda stores declaration nodes directly after the body.
378+
// Each declaration node is later bound to this lambda via ChildIdx = lambdaIndex and ChildCount = parameter position.
379+
// Parameter references inside the body are separate usage nodes with ChildIdx = declaration index.
370380
if (parameters == null || parameters.Length == 0)
371381
return AddFactoryExpressionNode(delegateType, null, ExpressionType.Lambda, 0, body);
372382

@@ -1259,6 +1269,9 @@ private void BindParameterDeclarations(int ownerIndex, in ChildList declarations
12591269

12601270
private void BindParameterDeclaration(int ownerIndex, int declarationIndex, int position)
12611271
{
1272+
// Parameter node contract:
1273+
// - declaration node: ParameterDeclarationFlag set, ChildIdx = owning lambda/block index, ChildCount = position in that owner
1274+
// - usage node: ParameterDeclarationFlag clear, ChildIdx = declaration node index, ChildCount = 0
12621275
ref var declaration = ref Nodes.GetSurePresentRef(declarationIndex);
12631276
if (!declaration.IsParameterDeclaration())
12641277
Throw.ParameterDeclarationExpected(declarationIndex);

0 commit comments

Comments
 (0)