|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +#nullable enable |
| 6 | + |
| 7 | +using System.Threading; |
| 8 | + |
| 9 | +using Microsoft.Scripting; |
| 10 | +using MSAst = System.Linq.Expressions; |
| 11 | + |
| 12 | +namespace IronPython.Compiler.Ast { |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Represents an async for statement. |
| 16 | + /// Desugared to Python AST that uses __aiter__ and await __anext__(). |
| 17 | + /// </summary> |
| 18 | + public class AsyncForStatement : Statement, ILoopStatement { |
| 19 | + private static int _counter; |
| 20 | + private Statement? _desugared; |
| 21 | + |
| 22 | + public AsyncForStatement(Expression left, Expression list, Statement body, Statement? @else) { |
| 23 | + Left = left; |
| 24 | + List = list; |
| 25 | + Body = body; |
| 26 | + Else = @else; |
| 27 | + } |
| 28 | + |
| 29 | + public int HeaderIndex { private get; set; } |
| 30 | + |
| 31 | + public Expression Left { get; } |
| 32 | + |
| 33 | + public Expression List { get; set; } |
| 34 | + |
| 35 | + public Statement Body { get; set; } |
| 36 | + |
| 37 | + public Statement? Else { get; } |
| 38 | + |
| 39 | + MSAst.LabelTarget ILoopStatement.BreakLabel { get; set; } = null!; |
| 40 | + |
| 41 | + MSAst.LabelTarget ILoopStatement.ContinueLabel { get; set; } = null!; |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Build the desugared tree. Called during Walk when Parent and IndexSpan are available. |
| 45 | + /// </summary> |
| 46 | + private Statement BuildDesugared() { |
| 47 | + var parent = Parent; |
| 48 | + var span = IndexSpan; |
| 49 | + var id = Interlocked.Increment(ref _counter); |
| 50 | + |
| 51 | + // async for TARGET in ITER: |
| 52 | + // BLOCK |
| 53 | + // else: |
| 54 | + // ELSE_BLOCK |
| 55 | + // |
| 56 | + // desugars to: |
| 57 | + // |
| 58 | + // __aiter = ITER.__aiter__() |
| 59 | + // __running = True |
| 60 | + // while __running: |
| 61 | + // try: |
| 62 | + // TARGET = await __aiter.__anext__() |
| 63 | + // except StopAsyncIteration: |
| 64 | + // __running = False |
| 65 | + // else: |
| 66 | + // BLOCK |
| 67 | + // else: |
| 68 | + // ELSE_BLOCK |
| 69 | + |
| 70 | + var iterName = $"__asyncfor_iter{id}"; |
| 71 | + var runningName = $"__asyncfor_running{id}"; |
| 72 | + |
| 73 | + // Helper to create nodes with proper parent and span |
| 74 | + NameExpression MakeName(string name) { |
| 75 | + var n = new NameExpression(name) { Parent = parent }; |
| 76 | + n.IndexSpan = span; |
| 77 | + return n; |
| 78 | + } |
| 79 | + |
| 80 | + T WithSpan<T>(T node) where T : Node { |
| 81 | + node.IndexSpan = span; |
| 82 | + return node; |
| 83 | + } |
| 84 | + |
| 85 | + // _iter = ITER.__aiter__() |
| 86 | + var aiterAttr = WithSpan(new MemberExpression(List, "__aiter__") { Parent = parent }); |
| 87 | + var aiterCall = WithSpan(new CallExpression(aiterAttr, null, null) { Parent = parent }); |
| 88 | + var assignIter = WithSpan(new AssignmentStatement(new Expression[] { MakeName(iterName) }, aiterCall) { Parent = parent }); |
| 89 | + |
| 90 | + // running = True |
| 91 | + var trueConst = new ConstantExpression(true) { Parent = parent }; trueConst.IndexSpan = span; |
| 92 | + var assignRunning = WithSpan(new AssignmentStatement(new Expression[] { MakeName(runningName) }, trueConst) { Parent = parent }); |
| 93 | + |
| 94 | + // TARGET = await __aiter.__anext__() |
| 95 | + var anextAttr = WithSpan(new MemberExpression(MakeName(iterName), "__anext__") { Parent = parent }); |
| 96 | + var anextCall = WithSpan(new CallExpression(anextAttr, null, null) { Parent = parent }); |
| 97 | + var awaitNext = new AwaitExpression(anextCall); |
| 98 | + var assignTarget = WithSpan(new AssignmentStatement(new Expression[] { Left }, awaitNext) { Parent = parent }); |
| 99 | + |
| 100 | + // except StopAsyncIteration: __running = False |
| 101 | + var falseConst = new ConstantExpression(false) { Parent = parent }; falseConst.IndexSpan = span; |
| 102 | + var stopRunning = WithSpan(new AssignmentStatement( |
| 103 | + new Expression[] { MakeName(runningName) }, falseConst) { Parent = parent }); |
| 104 | + var handler = WithSpan(new TryStatementHandler( |
| 105 | + MakeName("StopAsyncIteration"), |
| 106 | + null!, |
| 107 | + WithSpan(new SuiteStatement(new Statement[] { stopRunning }) { Parent = parent }) |
| 108 | + ) { Parent = parent }); |
| 109 | + handler.HeaderIndex = span.End; |
| 110 | + |
| 111 | + // try/except/else block |
| 112 | + var tryExcept = WithSpan(new TryStatement( |
| 113 | + assignTarget, |
| 114 | + new[] { handler }, |
| 115 | + WithSpan(new SuiteStatement(new Statement[] { Body }) { Parent = parent }), |
| 116 | + null! |
| 117 | + ) { Parent = parent }); |
| 118 | + tryExcept.HeaderIndex = span.End; |
| 119 | + |
| 120 | + // while __running: try/except/else |
| 121 | + var whileStmt = new WhileStatement(MakeName(runningName), tryExcept, Else); |
| 122 | + whileStmt.SetLoc(GlobalParent, span.Start, span.End, span.End); |
| 123 | + whileStmt.Parent = parent; |
| 124 | + |
| 125 | + var suite = WithSpan(new SuiteStatement(new Statement[] { assignIter, assignRunning, whileStmt }) { Parent = parent }); |
| 126 | + return suite; |
| 127 | + } |
| 128 | + |
| 129 | + public override MSAst.Expression Reduce() { |
| 130 | + return _desugared!.Reduce(); |
| 131 | + } |
| 132 | + |
| 133 | + public override void Walk(PythonWalker walker) { |
| 134 | + if (walker.Walk(this)) { |
| 135 | + // Build the desugared tree on first walk (when Parent and IndexSpan are set) |
| 136 | + if (_desugared == null) { |
| 137 | + _desugared = BuildDesugared(); |
| 138 | + } |
| 139 | + _desugared.Walk(walker); |
| 140 | + } |
| 141 | + walker.PostWalk(this); |
| 142 | + } |
| 143 | + |
| 144 | + internal override bool CanThrow => true; |
| 145 | + } |
| 146 | +} |
0 commit comments