-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathWithStatement.cs
286 lines (263 loc) · 11.7 KB
/
WithStatement.cs
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// 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.
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Microsoft.Scripting;
using Microsoft.Scripting.Actions;
using IronPython.Runtime.Binding;
using MSAst = System.Linq.Expressions;
using AstUtils = Microsoft.Scripting.Ast.Utils;
namespace IronPython.Compiler.Ast {
using Ast = MSAst.Expression;
public class WithStatement : Statement {
private int _headerIndex;
private readonly Expression _contextManager;
private readonly Expression _var;
private Statement _body;
public WithStatement(Expression contextManager, Expression var, Statement body) {
_contextManager = contextManager;
_var = var;
_body = body;
}
public int HeaderIndex {
set { _headerIndex = value; }
}
public new Expression Variable {
get { return _var; }
}
public Expression ContextManager {
get { return _contextManager; }
}
public Statement Body {
get { return _body; }
}
/// <summary>
/// WithStatement is translated to the DLR AST equivalent to
/// the following Python code snippet (from with statement spec):
///
/// mgr = (EXPR)
/// exit = mgr.__exit__ # Not calling it yet
/// value = mgr.__enter__()
/// exc = True
/// try:
/// VAR = value # Only if "as VAR" is present
/// BLOCK
/// except:
/// # The exceptional case is handled here
/// exc = False
/// if not exit(*sys.exc_info()):
/// raise
/// # The exception is swallowed if exit() returns true
/// finally:
/// # The normal and non-local-goto cases are handled here
/// if exc:
/// exit(None, None, None)
///
/// </summary>
public override MSAst.Expression Reduce() {
// Five statements in the result...
ReadOnlyCollectionBuilder<MSAst.Expression> statements = new ReadOnlyCollectionBuilder<MSAst.Expression>(6);
ReadOnlyCollectionBuilder<MSAst.ParameterExpression> variables = new ReadOnlyCollectionBuilder<MSAst.ParameterExpression>(6);
MSAst.ParameterExpression lineUpdated = Ast.Variable(typeof(bool), "$lineUpdated_with");
variables.Add(lineUpdated);
//******************************************************************
// 1. mgr = (EXPR)
//******************************************************************
MSAst.ParameterExpression manager = Ast.Variable(typeof(object), "with_manager");
variables.Add(manager);
statements.Add(
GlobalParent.AddDebugInfo(
Ast.Assign(
manager,
_contextManager
),
new SourceSpan(GlobalParent.IndexToLocation(StartIndex), GlobalParent.IndexToLocation(_headerIndex))
)
);
//******************************************************************
// 2. exit = mgr.__exit__ # Not calling it yet
//******************************************************************
MSAst.ParameterExpression exit = Ast.Variable(typeof(object), "with_exit");
variables.Add(exit);
statements.Add(
MakeAssignment(
exit,
GlobalParent.Get(
"__exit__",
manager
)
)
);
//******************************************************************
// 3. value = mgr.__enter__()
//******************************************************************
MSAst.ParameterExpression value = Ast.Variable(typeof(object), "with_value");
variables.Add(value);
statements.Add(
GlobalParent.AddDebugInfoAndVoid(
MakeAssignment(
value,
Parent.Invoke(
new CallSignature(0),
Parent.LocalContext,
GlobalParent.Get(
"__enter__",
manager
)
)
),
new SourceSpan(GlobalParent.IndexToLocation(StartIndex), GlobalParent.IndexToLocation(_headerIndex))
)
);
//******************************************************************
// 4. exc = True
//******************************************************************
MSAst.ParameterExpression exc = Ast.Variable(typeof(bool), "with_exc");
variables.Add(exc);
statements.Add(
MakeAssignment(
exc,
AstUtils.Constant(true)
)
);
//******************************************************************
// 5. The final try statement:
//
// try:
// VAR = value # Only if "as VAR" is present
// BLOCK
// except:
// # The exceptional case is handled here
// exc = False
// if not exit(*sys.exc_info()):
// raise
// # The exception is swallowed if exit() returns true
// finally:
// # The normal and non-local-goto cases are handled here
// if exc:
// exit(None, None, None)
//******************************************************************
var previousException = Ast.Variable(typeof(Exception), "$previousException");
variables.Add(previousException);
MSAst.ParameterExpression exception;
statements.Add(
// try:
AstUtils.Try(
AstUtils.Try(// try statement body
PushLineUpdated(false, lineUpdated),
Ast.Assign(previousException, Ast.Call(AstMethods.SaveCurrentException)),
_var != null ?
(MSAst.Expression)Ast.Block(
// VAR = value
_var.TransformSet(SourceSpan.None, value, PythonOperationKind.None),
// BLOCK
_body,
AstUtils.Empty()
) :
// BLOCK
(MSAst.Expression)_body // except:, // try statement location
).Catch(exception = Ast.Variable(typeof(Exception), "exception"),
// Python specific exception handling code
TryStatement.GetTracebackHeader(
this,
exception,
GlobalParent.AddDebugInfoAndVoid(
Ast.Block(
Ast.Call(AstMethods.SetCurrentException, Parent.LocalContext, exception),
// exc = False
MakeAssignment(
exc,
AstUtils.Constant(false)
),
// if not exit(*sys.exc_info()):
// raise
AstUtils.IfThen(
GlobalParent.Convert(
typeof(bool),
ConversionResultKind.ExplicitCast,
GlobalParent.Operation(
typeof(bool),
PythonOperationKind.IsFalse,
MakeExitCall(exit, exception)
)
),
UpdateLineUpdated(true),
Ast.Throw(
Ast.Call(
AstMethods.MakeRethrownException,
Parent.LocalContext
)
)
)
),
_body.Span
)
),
PopLineUpdated(lineUpdated),
Ast.Empty()
)
// finally:
).Finally(
Ast.Call(AstMethods.RestoreCurrentException, previousException),
// if exc:
// exit(None, None, None)
AstUtils.IfThen(
exc,
GlobalParent.AddDebugInfoAndVoid(
Ast.Block(
MSAst.DynamicExpression.Dynamic(
GlobalParent.PyContext.Invoke(
new CallSignature(3) // signature doesn't include function
),
typeof(object),
new MSAst.Expression[] {
Parent.LocalContext,
exit,
AstUtils.Constant(null),
AstUtils.Constant(null),
AstUtils.Constant(null)
}
),
Ast.Empty()
),
_contextManager.Span
)
)
)
);
statements.Add(AstUtils.Empty());
return Ast.Block(variables.ToReadOnlyCollection(), statements.ToReadOnlyCollection());
}
private MSAst.Expression MakeExitCall(MSAst.ParameterExpression exit, MSAst.Expression exception) {
// The 'with' statement's exceptional clause explicitly does not set the thread's current exception information.
// So while the pseudo code says:
// exit(*sys.exc_info())
// we'll actually do:
// exit(*PythonOps.GetExceptionInfoLocal($exception))
return GlobalParent.Convert(
typeof(bool),
ConversionResultKind.ExplicitCast,
Parent.Invoke(
new CallSignature(ArgumentType.List),
Parent.LocalContext,
exit,
Ast.Call(
AstMethods.GetExceptionInfoLocal,
Parent.LocalContext,
exception
)
)
);
}
public override void Walk(PythonWalker walker) {
if (walker.Walk(this)) {
_contextManager?.Walk(walker);
_var?.Walk(walker);
_body?.Walk(walker);
}
walker.PostWalk(this);
}
}
}