forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClosureExpression.cs
More file actions
194 lines (158 loc) · 5.96 KB
/
ClosureExpression.cs
File metadata and controls
194 lines (158 loc) · 5.96 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
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
// 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.Linq.Expressions;
using System;
using System.Reflection;
using Microsoft.Scripting.Interpreter;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;
using IronPython.Runtime;
using IronPython.Runtime.Operations;
namespace IronPython.Compiler {
/// <summary>
/// Small reducable node which just fetches the value from a ClosureCell
/// object. Like w/ global variables the compiler recognizes these on
/// sets and turns them into assignments on the python global object.
/// </summary>
internal class ClosureExpression : Expression, IPythonVariableExpression {
private readonly Expression/*!*/ _closureCell;
private readonly ParameterExpression _parameter;
private readonly Ast.PythonVariable/*!*/ _variable;
internal static readonly FieldInfo _cellField = typeof(ClosureCell).GetField("Value");
public ClosureExpression(Ast.PythonVariable/*!*/ variable, Expression/*!*/ closureCell, ParameterExpression parameter) {
Assert.NotNull(closureCell);
_variable = variable;
_closureCell = closureCell;
_parameter = parameter;
}
#region ClosureExpression Public API
/// <summary>
/// Gets the expression which points at the closure cell.
/// </summary>
public Expression/*!*/ ClosureCell {
get {
return _closureCell;
}
}
/// <summary>
/// The original expression for the incoming parameter if this is a parameter closure. Otherwise
/// the value is null.
/// </summary>
public ParameterExpression OriginalParameter {
get {
return _parameter;
}
}
/// <summary>
/// Gets the PythonVariable for which this closure expression was created.
/// </summary>
public Ast.PythonVariable/*!*/ PythonVariable {
get {
return _variable;
}
}
/// <summary>
/// Creates the storage for the closure cell. If this is a closure over a parameter it
/// captures the initial incoming parameter value.
/// </summary>
public Expression/*!*/ Create() {
if (OriginalParameter != null) {
return Expression.Assign(_closureCell, Expression.Call(Ast.AstMethods.MakeClosureCellWithValue, OriginalParameter));
}
return Expression.Assign(_closureCell, MakeClosureCellExpression.Instance);
}
private class MakeClosureCellExpression : Expression, IInstructionProvider {
private static readonly Expression _call = Expression.Call(Ast.AstMethods.MakeClosureCell);
public static readonly MakeClosureCellExpression Instance = new MakeClosureCellExpression();
public override bool CanReduce {
get {
return true;
}
}
public override ExpressionType NodeType {
get {
return ExpressionType.Extension;
}
}
public override Type Type {
get {
return typeof(ClosureCell);
}
}
public override Expression Reduce() {
return _call;
}
#region IInstructionProvider Members
public void AddInstructions(LightCompiler compiler) {
compiler.Instructions.Emit(MakeClosureCellInstruction.Instance);
}
#endregion
private class MakeClosureCellInstruction : Instruction {
public static readonly MakeClosureCellInstruction Instance = new MakeClosureCellInstruction();
public override int ProducedStack {
get {
return 1;
}
}
public override int ConsumedStack {
get {
return 0;
}
}
public override int Run(InterpretedFrame frame) {
frame.Push(PythonOps.MakeClosureCell());
return +1;
}
}
}
#endregion
#region Expression overrides
public sealed override ExpressionType NodeType {
get { return ExpressionType.Extension; }
}
public sealed override Type/*!*/ Type {
get { return typeof(object); }
}
public override bool CanReduce {
get {
return true;
}
}
public string Name {
get {
return _variable.Name;
}
}
/// <summary>
/// Reduces the closure cell to a read of the value stored in the cell.
/// </summary>
public override Expression/*!*/ Reduce() {
return Expression.Field(
_closureCell,
_cellField
);
}
#endregion
#region IPythonVariableExpression implementation
/// <summary>
/// Assigns a value to the closure cell.
/// </summary>
public Expression/*!*/ Assign(Expression/*!*/ value) {
return Expression.Assign(
Expression.Field(_closureCell, _cellField),
value
);
}
/// <summary>
/// Removes the current value from the closure cell.
/// </summary>
public Expression/*!*/ Delete() {
return Expression.Assign(
Expression.Field(_closureCell, _cellField),
Expression.Field(null, typeof(Uninitialized).GetDeclaredField("Instance"))
);
}
#endregion
}
}