-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
VariableBinder.cs
244 lines (216 loc) · 9.12 KB
/
VariableBinder.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
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Dynamic.Utils;
#if SILVERLIGHT
using System.Core;
#endif
#if CLR2
namespace Microsoft.Scripting.Ast.Compiler {
#else
namespace System.Linq.Expressions.Compiler {
#endif
/// <summary>
/// Determines if variables are closed over in nested lambdas and need to
/// be hoisted.
/// </summary>
internal sealed class VariableBinder : ExpressionVisitor {
private readonly AnalyzedTree _tree = new AnalyzedTree();
private readonly Stack<CompilerScope> _scopes = new Stack<CompilerScope>();
private readonly Stack<BoundConstants> _constants = new Stack<BoundConstants>();
private bool _inQuote;
internal static AnalyzedTree Bind(LambdaExpression lambda) {
var binder = new VariableBinder();
binder.Visit(lambda);
return binder._tree;
}
private VariableBinder() {
}
protected internal override Expression VisitConstant(ConstantExpression node) {
// If we're in Quote, we can ignore constants completely
if (_inQuote) {
return node;
}
// Constants that can be emitted into IL don't need to be stored on
// the delegate
if (ILGen.CanEmitConstant(node.Value, node.Type)) {
return node;
}
_constants.Peek().AddReference(node.Value, node.Type);
return node;
}
protected internal override Expression VisitUnary(UnaryExpression node) {
if (node.NodeType == ExpressionType.Quote) {
bool savedInQuote = _inQuote;
_inQuote = true;
Visit(node.Operand);
_inQuote = savedInQuote;
} else {
Visit(node.Operand);
}
return node;
}
protected internal override Expression VisitLambda<T>(Expression<T> node) {
_scopes.Push(_tree.Scopes[node] = new CompilerScope(node, true));
_constants.Push(_tree.Constants[node] = new BoundConstants());
Visit(MergeScopes(node));
_constants.Pop();
_scopes.Pop();
return node;
}
protected internal override Expression VisitInvocation(InvocationExpression node) {
LambdaExpression lambda = node.LambdaOperand;
// optimization: inline code for literal lambda's directly
if (lambda != null) {
// visit the lambda, but treat it more like a scope
_scopes.Push(_tree.Scopes[lambda] = new CompilerScope(lambda, false));
Visit(MergeScopes(lambda));
_scopes.Pop();
// visit the invoke's arguments
Visit(node.Arguments);
return node;
}
return base.VisitInvocation(node);
}
protected internal override Expression VisitBlock(BlockExpression node) {
if (node.Variables.Count == 0) {
Visit(node.Expressions);
return node;
}
_scopes.Push(_tree.Scopes[node] = new CompilerScope(node, false));
Visit(MergeScopes(node));
_scopes.Pop();
return node;
}
protected override CatchBlock VisitCatchBlock(CatchBlock node) {
if (node.Variable == null) {
Visit(node.Body);
return node;
}
_scopes.Push(_tree.Scopes[node] = new CompilerScope(node, false));
Visit(node.Body);
_scopes.Pop();
return node;
}
// If the immediate child is another scope, merge it into this one
// This is an optimization to save environment allocations and
// array accesses.
private ReadOnlyCollection<Expression> MergeScopes(Expression node) {
ReadOnlyCollection<Expression> body;
var lambda = node as LambdaExpression;
if (lambda != null) {
body = new ReadOnlyCollection<Expression>(new[] { lambda.Body });
} else {
body = ((BlockExpression)node).Expressions;
}
var currentScope = _scopes.Peek();
// A block body is mergeable if the body only contains one single block node containing variables,
// and the child block has the same type as the parent block.
while (body.Count == 1 && body[0].NodeType == ExpressionType.Block) {
var block = (BlockExpression)body[0];
if (block.Variables.Count > 0) {
// Make sure none of the variables are shadowed. If any
// are, we can't merge it.
foreach (var v in block.Variables) {
if (currentScope.Definitions.ContainsKey(v)) {
return body;
}
}
// Otherwise, merge it
if (currentScope.MergedScopes == null) {
currentScope.MergedScopes = new Set<object>(ReferenceEqualityComparer<object>.Instance);
}
currentScope.MergedScopes.Add(block);
foreach (var v in block.Variables) {
currentScope.Definitions.Add(v, VariableStorageKind.Local);
}
}
node = block;
body = block.Expressions;
}
return body;
}
protected internal override Expression VisitParameter(ParameterExpression node) {
Reference(node, VariableStorageKind.Local);
//
// Track reference count so we can emit it in a more optimal way if
// it is used a lot.
//
CompilerScope referenceScope = null;
foreach (CompilerScope scope in _scopes) {
//
// There are two times we care about references:
// 1. When we enter a lambda, we want to cache frequently
// used variables
// 2. When we enter a scope with closed-over variables, we
// want to cache it immediately when we allocate the
// closure slot for it
//
if (scope.IsMethod || scope.Definitions.ContainsKey(node)) {
referenceScope = scope;
break;
}
}
Debug.Assert(referenceScope != null);
if (referenceScope.ReferenceCount == null) {
referenceScope.ReferenceCount = new Dictionary<ParameterExpression, int>();
}
Helpers.IncrementCount(node, referenceScope.ReferenceCount);
return node;
}
protected internal override Expression VisitRuntimeVariables(RuntimeVariablesExpression node) {
foreach (var v in node.Variables) {
// Force hoisting of these variables
Reference(v, VariableStorageKind.Hoisted);
}
return node;
}
private void Reference(ParameterExpression node, VariableStorageKind storage) {
CompilerScope definition = null;
foreach (CompilerScope scope in _scopes) {
if (scope.Definitions.ContainsKey(node)) {
definition = scope;
break;
}
scope.NeedsClosure = true;
if (scope.IsMethod) {
storage = VariableStorageKind.Hoisted;
}
}
if (definition == null) {
throw Error.UndefinedVariable(node.Name, node.Type, CurrentLambdaName);
}
if (storage == VariableStorageKind.Hoisted) {
if (node.IsByRef) {
throw Error.CannotCloseOverByRef(node.Name, CurrentLambdaName);
}
definition.Definitions[node] = VariableStorageKind.Hoisted;
}
}
private string CurrentLambdaName {
get {
foreach (var scope in _scopes) {
var lambda = scope.Node as LambdaExpression;
if (lambda != null) {
return lambda.Name;
}
}
throw ContractUtils.Unreachable;
}
}
}
}