forked from rubberduck-vba/Rubberduck
-
Notifications
You must be signed in to change notification settings - Fork 12
/
IndexDefaultBinding.cs
308 lines (289 loc) · 14.2 KB
/
IndexDefaultBinding.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using Antlr4.Runtime;
using Rubberduck.Parsing.Symbols;
using System.Linq;
namespace Rubberduck.Parsing.Binding
{
public sealed class IndexDefaultBinding : IExpressionBinding
{
private readonly DeclarationFinder _declarationFinder;
private readonly Declaration _project;
private readonly Declaration _module;
private readonly Declaration _parent;
private readonly ParserRuleContext _expression;
private readonly IExpressionBinding _lExpressionBinding;
private IBoundExpression _lExpression;
private readonly ArgumentList _argumentList;
private const int DEFAULT_MEMBER_RECURSION_LIMIT = 32;
private int _defaultMemberRecursionLimitCounter = 0;
public IndexDefaultBinding(
DeclarationFinder declarationFinder,
Declaration project,
Declaration module,
Declaration parent,
ParserRuleContext expression,
IExpressionBinding lExpressionBinding,
ArgumentList argumentList)
: this(
declarationFinder,
project,
module,
parent,
expression,
(IBoundExpression)null,
argumentList)
{
_lExpressionBinding = lExpressionBinding;
}
public IndexDefaultBinding(
DeclarationFinder declarationFinder,
Declaration project,
Declaration module,
Declaration parent,
ParserRuleContext expression,
IBoundExpression lExpression,
ArgumentList argumentList)
{
_declarationFinder = declarationFinder;
_project = project;
_module = module;
_parent = parent;
_expression = expression;
_lExpression = lExpression;
_argumentList = argumentList;
}
private void ResolveArgumentList(Declaration calledProcedure)
{
foreach (var argument in _argumentList.Arguments)
{
argument.Resolve(calledProcedure);
}
}
public IBoundExpression Resolve()
{
if (_lExpressionBinding != null)
{
_lExpression = _lExpressionBinding.Resolve();
}
if (_lExpression.Classification != ExpressionClassification.ResolutionFailed)
{
ResolveArgumentList(_lExpression.ReferencedDeclaration);
}
else
{
ResolveArgumentList(null);
}
return Resolve(_lExpression);
}
private IBoundExpression Resolve(IBoundExpression lExpression)
{
IBoundExpression boundExpression = null;
if (lExpression.Classification == ExpressionClassification.ResolutionFailed)
{
return CreateFailedExpression(lExpression);
}
boundExpression = ResolveLExpressionIsVariablePropertyFunctionNoParameters(lExpression);
if (boundExpression != null)
{
return boundExpression;
}
boundExpression = ResolveLExpressionIsPropertyFunctionSubroutine(lExpression);
if (boundExpression != null)
{
return boundExpression;
}
boundExpression = ResolveLExpressionIsUnbound(lExpression);
if (boundExpression != null)
{
return boundExpression;
}
return CreateFailedExpression(lExpression);
}
private IBoundExpression CreateFailedExpression(IBoundExpression lExpression)
{
var failedExpr = new ResolutionFailedExpression();
failedExpr.AddSuccessfullyResolvedExpression(lExpression);
foreach (var arg in _argumentList.Arguments)
{
failedExpr.AddSuccessfullyResolvedExpression(arg.Expression);
}
return failedExpr;
}
private IBoundExpression ResolveLExpressionIsVariablePropertyFunctionNoParameters(IBoundExpression lExpression)
{
/*
<l-expression> is classified as a variable, or <l-expression> is classified as a property or function
with a parameter list that cannot accept any parameters and an <argument-list> that is not
empty, and one of the following is true (see below):
*/
bool isVariable = lExpression.Classification == ExpressionClassification.Variable;
bool propertyWithParameters = lExpression.Classification == ExpressionClassification.Property && !((IParameterizedDeclaration)lExpression.ReferencedDeclaration).Parameters.Any();
bool functionWithParameters = lExpression.Classification == ExpressionClassification.Function && !((IParameterizedDeclaration)lExpression.ReferencedDeclaration).Parameters.Any();
if (lExpression.ReferencedDeclaration != null && (isVariable || ((propertyWithParameters || functionWithParameters) && _argumentList.HasArguments)))
{
IBoundExpression boundExpression = null;
var asTypeName = lExpression.ReferencedDeclaration.AsTypeName;
var asTypeDeclaration = lExpression.ReferencedDeclaration.AsTypeDeclaration;
boundExpression = ResolveDefaultMember(lExpression, asTypeName, asTypeDeclaration);
if (boundExpression != null)
{
return boundExpression;
}
boundExpression = ResolveLExpressionDeclaredTypeIsArray(lExpression, asTypeDeclaration);
if (boundExpression != null)
{
return boundExpression;
}
return boundExpression;
}
return null;
}
private IBoundExpression ResolveDefaultMember(IBoundExpression lExpression, string asTypeName, Declaration asTypeDeclaration)
{
if (lExpression.ReferencedDeclaration.IsArray)
{
return null;
}
/*
The declared type of <l-expression> is Object or Variant, and <argument-list> contains no
named arguments. In this case, the index expression is classified as an unbound member with
a declared type of Variant, referencing <l-expression> with no member name.
*/
if (
asTypeName != null
&& (asTypeName.ToUpperInvariant() == "VARIANT" || asTypeName.ToUpperInvariant() == "OBJECT")
&& !_argumentList.HasNamedArguments)
{
return new IndexExpression(null, ExpressionClassification.Unbound, _expression, lExpression, _argumentList);
}
/*
The declared type of <l-expression> is a specific class, which has a public default Property
Get, Property Let, function or subroutine, and one of the following is true:
*/
bool hasDefaultMember = asTypeDeclaration != null
&& asTypeDeclaration.DeclarationType == DeclarationType.ClassModule
&& ((ClassModuleDeclaration)asTypeDeclaration).DefaultMember != null;
if (hasDefaultMember)
{
ClassModuleDeclaration classModule = (ClassModuleDeclaration)asTypeDeclaration;
Declaration defaultMember = classModule.DefaultMember;
bool isPropertyGetLetFunctionProcedure =
defaultMember.DeclarationType == DeclarationType.PropertyGet
|| defaultMember.DeclarationType == DeclarationType.PropertyLet
|| defaultMember.DeclarationType == DeclarationType.Function
|| defaultMember.DeclarationType == DeclarationType.Procedure;
bool isPublic =
defaultMember.Accessibility == Accessibility.Global
|| defaultMember.Accessibility == Accessibility.Implicit
|| defaultMember.Accessibility == Accessibility.Public;
if (isPropertyGetLetFunctionProcedure && isPublic)
{
/*
This default member’s parameter list is compatible with <argument-list>. In this case, the
index expression references this default member and takes on its classification and
declared type.
TODO: Primitive argument compatibility checking for now.
*/
if (((IParameterizedDeclaration)defaultMember).Parameters.Count() == _argumentList.Arguments.Count)
{
return new IndexExpression(defaultMember, lExpression.Classification, _expression, lExpression, _argumentList);
}
/**
This default member cannot accept any parameters. In this case, the static analysis restarts
recursively, as if this default member was specified instead for <l-expression> with the
same <argument-list>.
*/
if (((IParameterizedDeclaration)defaultMember).Parameters.Count() == 0)
{
// Recursion limit reached, abort.
if (DEFAULT_MEMBER_RECURSION_LIMIT == _defaultMemberRecursionLimitCounter)
{
return null;
}
_defaultMemberRecursionLimitCounter++;
ExpressionClassification classification;
if (defaultMember.DeclarationType.HasFlag(DeclarationType.Property))
{
classification = ExpressionClassification.Property;
}
else if (defaultMember.DeclarationType == DeclarationType.Procedure)
{
classification = ExpressionClassification.Subroutine;
}
else
{
classification = ExpressionClassification.Function;
}
var defaultMemberAsLExpression = new SimpleNameExpression(defaultMember, classification, _expression);
return Resolve(defaultMemberAsLExpression);
}
}
}
return null;
}
private IBoundExpression ResolveLExpressionDeclaredTypeIsArray(IBoundExpression lExpression, Declaration asTypeDeclaration)
{
/*
The declared type of <l-expression> is an array type, an empty argument list has not already
been specified for it, and one of the following is true:
*/
if (lExpression.ReferencedDeclaration.IsArray)
{
/*
<argument-list> represents an empty argument list. In this case, the index expression
takes on the classification and declared type of <l-expression> and references the same
array.
*/
if (!_argumentList.HasArguments)
{
return new IndexExpression(asTypeDeclaration, lExpression.Classification, _expression, lExpression, _argumentList);
}
else
{
/*
<argument-list> represents an argument list with a number of positional arguments equal
to the rank of the array, and with no named arguments. In this case, the index expression
references an individual element of the array, is classified as a variable and has the
declared type of the array’s element type.
TODO: Implement compatibility checking / amend the grammar
*/
if (!_argumentList.HasNamedArguments)
{
return new IndexExpression(asTypeDeclaration, ExpressionClassification.Variable, _expression, lExpression, _argumentList);
}
}
}
return null;
}
private IBoundExpression ResolveLExpressionIsPropertyFunctionSubroutine(IBoundExpression lExpression)
{
/*
<l-expression> is classified as a property or function and its parameter list is compatible with
<argument-list>. In this case, the index expression references <l-expression> and takes on its
classification and declared type.
<l-expression> is classified as a subroutine and its parameter list is compatible with <argument-
list>. In this case, the index expression references <l-expression> and takes on its classification
and declared type.
Note: We assume compatibility through enforcement by the VBE.
*/
if (lExpression.Classification == ExpressionClassification.Property
|| lExpression.Classification == ExpressionClassification.Function
|| lExpression.Classification == ExpressionClassification.Subroutine)
{
return new IndexExpression(lExpression.ReferencedDeclaration, lExpression.Classification, _expression, lExpression, _argumentList);
}
return null;
}
private IBoundExpression ResolveLExpressionIsUnbound(IBoundExpression lExpression)
{
/*
<l-expression> is classified as an unbound member. In this case, the index expression references
<l-expression>, is classified as an unbound member and its declared type is Variant.
*/
if (lExpression.Classification == ExpressionClassification.Unbound)
{
return new IndexExpression(lExpression.ReferencedDeclaration, ExpressionClassification.Unbound, _expression, lExpression, _argumentList);
}
return null;
}
}
}