Refactoring for immutable nodes

This commit is contained in:
Michael Schimmel
2025-11-05 13:21:17 +01:00
parent 9bd2d6f7ab
commit c0a689d2bc
13 changed files with 217 additions and 190 deletions
+7 -20
View File
@@ -571,28 +571,15 @@ end;
function TAstTransformer.VisitMacroExpansionNode(const Node: IMacroExpansionNode): IAstNode;
var
newBody: IAstNode;
N: TMacroExpansionNode;
newType: IStaticType;
newBody: IAstTypedNode;
begin
N := (Node as TMacroExpansionNode);
// Visit the body and check if it changed
newBody := Accept(Node.ExpandedBody).AsTypedNode;
if newBody = Node.ExpandedBody then
exit(Node);
// 1. Visit the body
newBody := Accept(N.ExpandedBody);
// 2. Check if the body changed (Copy-on-Write)
if newBody = N.ExpandedBody then
begin
// No change, return the original immutable node
Result := Node;
end
else
begin
// The body changed. Create a NEW wrapper node.
// The type of the wrapper is the type of its body.
newType := newBody.AsTypedNode.StaticType;
Result := TMacroExpansionNode.Create(N.CallNode, newBody, newType);
end;
// The body changed. Create a NEW wrapper node.
Result := TMacroExpansionNode.Create(Node.CallNode, newBody);
end;
function TAstTransformer.VisitBlockExpression(const Node: IBlockExpressionNode): IAstNode;