-
Notifications
You must be signed in to change notification settings - Fork 673
/
DictionaryInfo.cs
452 lines (383 loc) · 18.1 KB
/
DictionaryInfo.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* ****************************************************************************
*
* 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
* vspython@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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.PythonTools.Analysis.Interpreter;
using Microsoft.PythonTools.Interpreter;
using Microsoft.PythonTools.Parsing;
using Microsoft.PythonTools.Parsing.Ast;
namespace Microsoft.PythonTools.Analysis.Values {
internal class DictionaryInfo : BuiltinInstanceInfo {
private readonly VariableDef _keyTypes;
private readonly VariableDef _valueTypes;
private SequenceInfo _keyValueTuple;
private VariableDef _keyValueTupleVariable;
private readonly ProjectEntry _declaringModule;
private readonly int _declVersion;
private SpecializedDictionaryMethod _getMethod, _itemsMethod, _keysMethod, _valuesMethod, _iterKeysMethod, _iterValuesMethod, _popMethod, _popItemMethod, _iterItemsMethod, _updateMethod;
public DictionaryInfo(ProjectEntry declaringModule)
: base(declaringModule.ProjectState._dictType) {
_keyTypes = new VariableDef();
_valueTypes = new VariableDef();
_declaringModule = declaringModule;
_declVersion = declaringModule.AnalysisVersion;
}
public override ISet<Namespace> GetIndex(Node node, AnalysisUnit unit, ISet<Namespace> index) {
return _valueTypes.Types;
}
public override void SetIndex(Node node, AnalysisUnit unit, ISet<Namespace> index, ISet<Namespace> value) {
foreach (var indexVal in index) {
AddKeyType(node, unit, indexVal);
}
foreach (var valueVal in value) {
AddValueType(node, unit, valueVal);
}
}
internal bool AddValueType(Node node, AnalysisUnit unit, Namespace valueVal) {
if (_valueTypes.AddTypes(node, unit, valueVal)) {
if (_iterValuesMethod != null) {
_iterValuesMethod.Update();
}
if (_valuesMethod != null) {
_valuesMethod.Update();
}
return true;
}
return false;
}
internal bool AddKeyType(Node node, AnalysisUnit unit, Namespace indexVal) {
if (_keyTypes.AddTypes(node, unit, indexVal)) {
if (_iterKeysMethod != null) {
_iterKeysMethod.Update();
}
if (_keysMethod != null) {
_keysMethod.Update();
}
return true;
}
return false;
}
public override ISet<Namespace> GetMember(Node node, AnalysisUnit unit, string name) {
ISet<Namespace> res = null;
switch (name) {
case "get":
res = GetOrMakeSpecializedMethod(ref _getMethod, "get", method => new DictionaryGetBoundMethod(method, this));
break;
case "items":
res = GetOrMakeSpecializedMethod(ref _itemsMethod, "items", method => new DictionaryItemsBoundMethod(method, this));
break;
case "keys":
if (unit.ProjectState.LanguageVersion.Is3x()) {
res = GetOrMakeSpecializedMethod(ref _keysMethod, "keys", method => new DictionaryKeysIterableBoundMethod(method, this));
} else {
res = GetOrMakeSpecializedMethod(ref _keysMethod, "keys", method => new DictionaryKeysListBoundMethod(method, this));
}
break;
case "values":
if (unit.ProjectState.LanguageVersion.Is3x()) {
res = GetOrMakeSpecializedMethod(ref _valuesMethod, "values", method => new DictionaryValuesIterableBoundMethod(method, this));
} else {
res = GetOrMakeSpecializedMethod(ref _valuesMethod, "values", method => new DictionaryValuesListBoundMethod(method, this));
}
break;
case "pop":
res = GetOrMakeSpecializedMethod(ref _popMethod, "pop", method => new DictionaryValuesBoundMethod(method, this));
break;
case "popitem":
res = GetOrMakeSpecializedMethod(ref _popItemMethod, "popitem", method => new DictionaryKeyValueTupleBoundMethod(method, this));
break;
case "iterkeys":
if (!unit.ProjectState.LanguageVersion.Is3x()) {
res = GetOrMakeSpecializedMethod(ref _iterKeysMethod, "iterkeys", method => new DictionaryKeysIterableBoundMethod(method, this));
}
break;
case "itervalues":
if (!unit.ProjectState.LanguageVersion.Is3x()) {
res = GetOrMakeSpecializedMethod(ref _iterValuesMethod, "itervalues", method => new DictionaryValuesIterableBoundMethod(method, this));
}
break;
case "iteritems":
if (!unit.ProjectState.LanguageVersion.Is3x()) {
res = GetOrMakeSpecializedMethod(ref _iterItemsMethod, "iteritems", method => new DictionaryItemsIterableBoundMethod(method, this));
}
break;
case "update":
res = GetOrMakeSpecializedMethod(ref _updateMethod, "update", method => new DictionaryUpdateBoundMethod(method, this));
break;
}
return res ?? base.GetMember(node, unit, name);
}
private ISet<Namespace> GetOrMakeSpecializedMethod(ref SpecializedDictionaryMethod method, string name, Func<BuiltinMethodInfo, SpecializedDictionaryMethod> maker) {
if (method == null) {
ISet<Namespace> itemsMeth;
if (TryGetMember(name, out itemsMeth)) {
method = maker((BuiltinMethodInfo)itemsMeth.First());
}
}
return method;
}
public override string ShortDescription {
get {
return "dict";
}
}
public override string Description {
get {
// dict({k : v})
Namespace keyType = _keyTypes.Types.GetUnionType();
string keyName = keyType == null ? null : keyType.ShortDescription;
Namespace valueType = _valueTypes.Types.GetUnionType();
string valueName = valueType == null ? null : valueType.ShortDescription;
if (keyName != null || valueName != null) {
return "dict({" +
(keyName ?? "unknown") +
" : " +
(valueName ?? "unknown") +
"})";
}
return "dict";
}
}
public override bool UnionEquals(Namespace ns) {
return ns is DictionaryInfo;
}
public override int UnionHashCode() {
return 2;
}
public override PythonMemberType ResultType {
get {
return PythonMemberType.Field;
}
}
public override ProjectEntry DeclaringModule {
get {
return _declaringModule;
}
}
public override int DeclaringVersion {
get {
return _declVersion;
}
}
/// <summary>
/// Gets a type which represents the tuple of (keyTypes, valueTypes)
/// </summary>
private SequenceInfo KeyValueTuple {
get {
if (_keyValueTuple == null) {
_keyValueTuple = new SequenceInfo(
new[] { _keyTypes, _valueTypes },
ProjectState._tupleType
);
}
return _keyValueTuple;
}
}
private VariableDef KeyValueTupleVariable {
get {
if (_keyValueTupleVariable == null) {
_keyValueTupleVariable = new VariableDef();
_keyValueTupleVariable.AddTypes(DeclaringModule, KeyValueTuple.SelfSet);
}
return _keyValueTupleVariable;
}
}
class SpecializedDictionaryMethod : BoundBuiltinMethodInfo {
internal readonly DictionaryInfo _myDict;
internal SpecializedDictionaryMethod(BuiltinMethodInfo method, DictionaryInfo myDict)
: base(method) {
_myDict = myDict;
}
public virtual void Update() {
}
}
#region Specialized functions
class DictionaryItemsBoundMethod : SpecializedDictionaryMethod {
private ListInfo _list;
internal DictionaryItemsBoundMethod(BuiltinMethodInfo method, DictionaryInfo myDict)
: base(method, myDict) {
}
public override ISet<Namespace> Call(Node node, AnalysisUnit unit, ISet<Namespace>[] args, NameExpression[] keywordArgNames) {
_myDict._valueTypes.AddDependency(unit);
_myDict._keyTypes.AddDependency(unit);
if (_list == null) {
_list = new ListInfo(
new[] { _myDict.KeyValueTupleVariable },
unit.ProjectState._listType
);
}
return _list.SelfSet;
}
}
class DictionaryGetBoundMethod : SpecializedDictionaryMethod {
internal DictionaryGetBoundMethod(BuiltinMethodInfo method, DictionaryInfo myDict)
: base(method, myDict) {
}
public override ISet<Namespace> Call(Node node, AnalysisUnit unit, ISet<Namespace>[] args, NameExpression[] keywordArgNames) {
_myDict._valueTypes.AddDependency(unit);
if (args.Length <= 1) {
return _myDict._valueTypes.Types;
}
return _myDict._valueTypes.Types.Union(args[1]);
}
}
class DictionaryKeysListBoundMethod : SpecializedDictionaryMethod {
private ListInfo _list;
internal DictionaryKeysListBoundMethod(BuiltinMethodInfo method, DictionaryInfo myDict)
: base(method, myDict) {
}
public override ISet<Namespace> Call(Node node, AnalysisUnit unit, ISet<Namespace>[] args, NameExpression[] keywordArgNames) {
_myDict._keyTypes.AddDependency(unit);
if (_list == null) {
_list = new ListInfo(new[] { _myDict._keyTypes }, unit.ProjectState._listType);
}
return _list;
}
public override void Update() {
if (_list != null) {
_list.UnionType = null;
}
}
}
class DictionaryValuesListBoundMethod : SpecializedDictionaryMethod {
private ListInfo _list;
internal DictionaryValuesListBoundMethod(BuiltinMethodInfo method, DictionaryInfo myDict)
: base(method, myDict) {
}
public override ISet<Namespace> Call(Node node, AnalysisUnit unit, ISet<Namespace>[] args, NameExpression[] keywordArgNames) {
_myDict._valueTypes.AddDependency(unit);
if (_list == null) {
_list = new ListInfo(new[] { _myDict._valueTypes }, unit.ProjectState._listType);
}
return _list;
}
public override void Update() {
if (_list != null) {
_list.UnionType = null;
}
}
}
class DictionaryKeysIterableBoundMethod : SpecializedDictionaryMethod {
private IterableInfo _list;
internal DictionaryKeysIterableBoundMethod(BuiltinMethodInfo method, DictionaryInfo myDict)
: base(method, myDict) {
}
public override ISet<Namespace> Call(Node node, AnalysisUnit unit, ISet<Namespace>[] args, NameExpression[] keywordArgNames) {
_myDict._keyTypes.AddDependency(unit);
if (_list == null) {
_list = new IteratorInfo(new[] { _myDict._keyTypes }, unit.ProjectState._dictKeysType);
}
return _list;
}
public override void Update() {
if (_list != null) {
_list.UnionType = null;
}
}
}
class DictionaryValuesIterableBoundMethod : SpecializedDictionaryMethod {
private IterableInfo _list;
internal DictionaryValuesIterableBoundMethod(BuiltinMethodInfo method, DictionaryInfo myDict)
: base(method, myDict) {
}
public override ISet<Namespace> Call(Node node, AnalysisUnit unit, ISet<Namespace>[] args, NameExpression[] keywordArgNames) {
_myDict._valueTypes.AddDependency(unit);
if (_list == null) {
_list = new IteratorInfo(new[] { _myDict._valueTypes }, unit.ProjectState._dictValuesType);
}
return _list;
}
public override void Update() {
if (_list != null) {
_list.UnionType = null;
}
}
}
class DictionaryItemsIterableBoundMethod : SpecializedDictionaryMethod {
private IterableInfo _list;
internal DictionaryItemsIterableBoundMethod(BuiltinMethodInfo method, DictionaryInfo myDict)
: base(method, myDict) {
}
public override ISet<Namespace> Call(Node node, AnalysisUnit unit, ISet<Namespace>[] args, NameExpression[] keywordArgNames) {
_myDict._valueTypes.AddDependency(unit);
_myDict._keyTypes.AddDependency(unit);
if (_list == null) {
_list = new IteratorInfo(new[] { _myDict.KeyValueTupleVariable }, unit.ProjectState._dictValuesType);
}
return _list;
}
}
class DictionaryValuesBoundMethod : SpecializedDictionaryMethod {
internal DictionaryValuesBoundMethod(BuiltinMethodInfo method, DictionaryInfo myDict)
: base(method, myDict) {
}
public override ISet<Namespace> Call(Node node, AnalysisUnit unit, ISet<Namespace>[] args, NameExpression[] keywordArgNames) {
_myDict._valueTypes.AddDependency(unit);
return _myDict._valueTypes.Types;
}
}
class DictionaryKeyValueTupleBoundMethod : SpecializedDictionaryMethod {
internal DictionaryKeyValueTupleBoundMethod(BuiltinMethodInfo method, DictionaryInfo myDict)
: base(method, myDict) {
}
public override ISet<Namespace> Call(Node node, AnalysisUnit unit, ISet<Namespace>[] args, NameExpression[] keywordArgNames) {
_myDict._valueTypes.AddDependency(unit);
_myDict._keyTypes.AddDependency(unit);
return _myDict.KeyValueTuple;
}
}
class DictionaryUpdateBoundMethod : SpecializedDictionaryMethod {
internal DictionaryUpdateBoundMethod(BuiltinMethodInfo method, DictionaryInfo myDict)
: base(method, myDict) {
}
public override ISet<Namespace> Call(Node node, AnalysisUnit unit, ISet<Namespace>[] args, NameExpression[] keywordArgNames) {
if (args.Length >= 1) {
foreach (var type in args[0]) {
DictionaryInfo otherDict = type as DictionaryInfo;
if (otherDict != null) {
_myDict._valueTypes.AddTypes(node, unit, otherDict._valueTypes.Types);
_myDict._keyTypes.AddTypes(node, unit, otherDict._keyTypes.Types);
}
}
}
// TODO: Process keyword args and add those values to our dictionary, plus a string key
return EmptySet<Namespace>.Instance;
}
}
#endregion
public override string ToString() {
StringBuilder sb = new StringBuilder();
sb.Append("dict(keys=(");
foreach (var type in _keyTypes.Types) {
if (type.Push()) {
sb.Append(type.ToString());
sb.Append(", ");
type.Pop();
}
}
sb.Append("), values = (");
foreach (var type in _valueTypes.Types) {
if (type.Push()) {
sb.Append(type.ToString());
sb.Append(", ");
type.Pop();
}
}
sb.Append(")");
return sb.ToString();
}
}
}