forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncWithStatement.cs
More file actions
108 lines (87 loc) · 4.05 KB
/
AsyncWithStatement.cs
File metadata and controls
108 lines (87 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
#nullable enable
using Microsoft.Scripting;
using MSAst = System.Linq.Expressions;
using AstUtils = Microsoft.Scripting.Ast.Utils;
namespace IronPython.Compiler.Ast {
using Ast = MSAst.Expression;
/// <summary>
/// Represents an async with statement.
/// Desugared to Python AST that uses await on __aenter__ and __aexit__.
/// </summary>
public class AsyncWithStatement : Statement {
private Statement? _desugared;
public AsyncWithStatement(Expression contextManager, Expression? var, Statement body) {
ContextManager = contextManager;
Variable = var;
Body = body;
}
public int HeaderIndex { private get; set; }
public Expression ContextManager { get; }
public new Expression? Variable { get; }
public Statement Body { get; }
/// <summary>
/// Build the desugared tree. Called during Walk when Parent and IndexSpan are available.
/// </summary>
private Statement BuildDesugared() {
var parent = Parent;
var span = IndexSpan;
// async with EXPR as VAR:
// BLOCK
//
// desugars to:
//
// mgr = EXPR
// try:
// VAR = await mgr.__aenter__() (or just await mgr.__aenter__())
// BLOCK
// finally:
// await mgr.__aexit__(None, None, None)
// Helper to assign proper parent and span to nodes
T SetScope<T>(T node) where T : Node {
node.Parent = parent;
node.IndexSpan = span;
return node;
}
// mgr = EXPR
var assignMgr = SetScope(new AssignmentStatement([SetScope(new NameExpression("__asyncwith_mgr"))], ContextManager));
// await mgr.__aenter__()
var aenterAttr = SetScope(new MemberExpression(SetScope(new NameExpression("__asyncwith_mgr")), "__aenter__"));
var aenterCall = SetScope(new CallExpression(aenterAttr, null, null));
var awaitEnter = new AwaitExpression(aenterCall);
Statement bodyStmt;
if (Variable is not null) {
// VAR = await value; BLOCK
var assignVar = SetScope(new AssignmentStatement([Variable], awaitEnter));
bodyStmt = new SuiteStatement([assignVar, Body]) { Parent = parent };
} else {
var exprStmt = SetScope(new ExpressionStatement(awaitEnter));
bodyStmt = new SuiteStatement([exprStmt, Body]) { Parent = parent };
}
// await mgr.__aexit__(None, None, None)
var aexitAttr = SetScope(new MemberExpression(SetScope(new NameExpression("__asyncwith_mgr")), "__aexit__"));
var none = SetScope(new ConstantExpression(null));
var aexitCallNormal = SetScope(new CallExpression(aexitAttr, [none, none, none], null));
var awaitExitNormal = new AwaitExpression(aexitCallNormal);
// try/finally: await __aexit__ on normal exit
var finallyExprStmt = SetScope(new ExpressionStatement(awaitExitNormal));
var tryFinally = SetScope(new TryStatement(bodyStmt, null, null, finallyExprStmt) { HeaderIndex = span.End });
return SetScope(new SuiteStatement([assignMgr, tryFinally]));
}
public override MSAst.Expression Reduce() {
return _desugared!.Reduce();
}
public override void Walk(PythonWalker walker) {
if (walker.Walk(this)) {
// Build the desugared tree on first walk (when Parent and IndexSpan are set)
if (_desugared == null) {
_desugared = BuildDesugared();
}
_desugared.Walk(walker);
}
walker.PostWalk(this);
}
}
}