Terraria v1.4.4.9
Terraria source code documentation
All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros
VariableBinder.cs
Go to the documentation of this file.
5
7
8internal sealed class VariableBinder : ExpressionVisitor
9{
10 private readonly AnalyzedTree _tree = new AnalyzedTree();
11
13
15
16 private readonly StackGuard _guard = new StackGuard();
17
18 private bool _inQuote;
19
20 private string CurrentLambdaName
21 {
22 get
23 {
24 foreach (CompilerScope scope in _scopes)
25 {
27 {
28 return lambdaExpression.Name;
29 }
30 }
32 }
33 }
34
36 {
39 return variableBinder._tree;
40 }
41
43 {
44 }
45
46 [return: NotNullIfNotNull("node")]
48 {
50 {
51 return _guard.RunOnEmptyStack((VariableBinder @this, Expression e) => @this.Visit(e), this, node);
52 }
53 return base.Visit(node);
54 }
55
56 protected internal override Expression VisitConstant(ConstantExpression node)
57 {
58 if (_inQuote)
59 {
60 return node;
61 }
62 if (ILGen.CanEmitConstant(node.Value, node.Type))
63 {
64 return node;
65 }
66 _constants.Peek().AddReference(node.Value, node.Type);
67 return node;
68 }
69
70 protected internal override Expression VisitUnary(UnaryExpression node)
71 {
72 if (node.NodeType == ExpressionType.Quote)
73 {
74 bool inQuote = _inQuote;
75 _inQuote = true;
76 Visit(node.Operand);
78 }
79 else
80 {
81 Visit(node.Operand);
82 }
83 return node;
84 }
85
86 protected internal override Expression VisitLambda<T>(Expression<T> node)
87 {
90 scopes.Push(item);
93 constants.Push(item2);
95 _constants.Pop();
96 _scopes.Pop();
97 return node;
98 }
99
101 {
102 LambdaExpression lambdaOperand = node.LambdaOperand;
103 if (lambdaOperand != null)
104 {
107 scopes.Push(item);
109 _scopes.Pop();
110 int i = 0;
111 for (int argumentCount = node.ArgumentCount; i < argumentCount; i++)
112 {
113 Visit(node.GetArgument(i));
114 }
115 return node;
116 }
117 return base.VisitInvocation(node);
118 }
119
120 protected internal override Expression VisitBlock(BlockExpression node)
121 {
122 if (node.Variables.Count == 0)
123 {
124 Visit(node.Expressions);
125 return node;
126 }
129 scopes.Push(item);
131 _scopes.Pop();
132 return node;
133 }
134
136 {
137 if (node.Variable == null)
138 {
139 Visit(node.Filter);
140 Visit(node.Body);
141 return node;
142 }
145 scopes.Push(item);
146 Visit(node.Filter);
147 Visit(node.Body);
148 _scopes.Pop();
149 return node;
150 }
151
153 {
156 while (readOnlyCollection.Count == 1 && readOnlyCollection[0].NodeType == ExpressionType.Block)
157 {
159 if (blockExpression.Variables.Count > 0)
160 {
161 foreach (ParameterExpression variable in blockExpression.Variables)
162 {
163 if (compilerScope.Definitions.ContainsKey(variable))
164 {
165 return readOnlyCollection;
166 }
167 }
168 if (compilerScope.MergedScopes == null)
169 {
170 compilerScope.MergedScopes = new HashSet<BlockExpression>(ReferenceEqualityComparer.Instance);
171 }
172 compilerScope.MergedScopes.Add(blockExpression);
173 foreach (ParameterExpression variable2 in blockExpression.Variables)
174 {
176 }
177 }
179 }
180 return readOnlyCollection;
181 }
182
184 {
187 foreach (CompilerScope scope in _scopes)
188 {
189 if (scope.IsMethod || scope.Definitions.ContainsKey(node))
190 {
191 compilerScope = scope;
192 break;
193 }
194 }
195 if (compilerScope.ReferenceCount == null)
196 {
197 compilerScope.ReferenceCount = new Dictionary<ParameterExpression, int>();
198 }
199 Helpers.IncrementCount(node, compilerScope.ReferenceCount);
200 return node;
201 }
202
204 {
205 foreach (ParameterExpression variable in node.Variables)
206 {
208 }
209 return node;
210 }
211
213 {
215 foreach (CompilerScope scope in _scopes)
216 {
217 if (scope.Definitions.ContainsKey(node))
218 {
219 compilerScope = scope;
220 break;
221 }
222 scope.NeedsClosure = true;
223 if (scope.IsMethod)
224 {
226 }
227 }
228 if (compilerScope == null)
229 {
231 }
232 if (storage == VariableStorageKind.Hoisted)
233 {
234 if (node.IsByRef)
235 {
237 }
238 compilerScope.Definitions[node] = VariableStorageKind.Hoisted;
239 }
240 }
241}
void Add(TKey key, TValue value)
readonly Dictionary< object, CompilerScope > Scopes
readonly Dictionary< LambdaExpression, BoundConstants > Constants
readonly Dictionary< ParameterExpression, VariableStorageKind > Definitions
static bool CanEmitConstant(object value, Type type)
Definition ILGen.cs:290
override Expression VisitRuntimeVariables(RuntimeVariablesExpression node)
override CatchBlock VisitCatchBlock(CatchBlock node)
readonly Stack< CompilerScope > _scopes
static AnalyzedTree Bind(LambdaExpression lambda)
void Reference(ParameterExpression node, VariableStorageKind storage)
override Expression VisitUnary(UnaryExpression node)
readonly Stack< BoundConstants > _constants
override Expression VisitParameter(ParameterExpression node)
override Expression Visit(Expression node)
override Expression VisitConstant(ConstantExpression node)
ReadOnlyCollection< Expression > MergeScopes(Expression node)
override Expression VisitInvocation(InvocationExpression node)
override Expression VisitBlock(BlockExpression node)
override Expression VisitLambda< T >(Expression< T > node)
static Exception CannotCloseOverByRef(object p0, object p1)
Definition Error.cs:763
static Exception UndefinedVariable(object p0, object p1, object p2)
Definition Error.cs:758