This repository was archived by the owner on Apr 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathExpressionEval.cs
More file actions
321 lines (286 loc) · 13.5 KB
/
ExpressionEval.cs
File metadata and controls
321 lines (286 loc) · 13.5 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
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
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABILITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Python.Analysis.Analyzer.Symbols;
using Microsoft.Python.Analysis.Diagnostics;
using Microsoft.Python.Analysis.Modules;
using Microsoft.Python.Analysis.Types;
using Microsoft.Python.Analysis.Values;
using Microsoft.Python.Core;
using Microsoft.Python.Core.Disposables;
using Microsoft.Python.Core.Logging;
using Microsoft.Python.Core.Text;
using Microsoft.Python.Parsing.Ast;
namespace Microsoft.Python.Analysis.Analyzer.Evaluation {
/// <summary>
/// Helper class that provides methods for looking up variables
/// and types in a chain of scopes during analysis.
/// </summary>
internal sealed partial class ExpressionEval : IExpressionEvaluator {
private readonly Stack<Scope> _openScopes = new Stack<Scope>();
private readonly object _lock = new object();
private readonly List<DiagnosticsEntry> _diagnostics = new List<DiagnosticsEntry>();
private readonly AnalysisOptions _analysisOptions;
public ExpressionEval(IServiceContainer services, IPythonModule module, PythonAst ast) {
Services = services ?? throw new ArgumentNullException(nameof(services));
Module = module ?? throw new ArgumentNullException(nameof(module));
Ast = ast ?? throw new ArgumentNullException(nameof(ast));
GlobalScope = new GlobalScope(module, ast);
CurrentScope = GlobalScope;
DefaultLocation = new Location(module);
//Log = services.GetService<ILogger>();
var optionsOptovider = services.GetService<IAnalysisOptionsProvider>();
if (optionsOptovider != null) {
_analysisOptions = optionsOptovider.Options;
} else {
_analysisOptions = new AnalysisOptions {
AnalysisCachingLevel = AnalysisCachingLevel.None,
StubOnlyAnalysis = true,
KeepLibraryAst = false,
LintingEnabled = true
};
}
}
public GlobalScope GlobalScope { get; }
public Scope CurrentScope { get; private set; }
public bool SuppressBuiltinLookup => Module.ModuleType == ModuleType.Builtins;
public ILogger Log { get; }
public ModuleSymbolTable SymbolTable { get; } = new ModuleSymbolTable();
public IPythonType UnknownType => Interpreter.UnknownType;
public Location DefaultLocation { get; }
public IPythonModule BuiltinsModule => Interpreter.ModuleResolution.BuiltinsModule;
public bool StubOnlyAnalysis
=> _analysisOptions.StubOnlyAnalysis
&& Module.Stub != null && Module.ModuleType != ModuleType.User && Module.Stub.IsTypeshed;
public LocationInfo GetLocationInfo(Node node) => node?.GetLocation(this) ?? LocationInfo.Empty;
public Location GetLocationOfName(Node node) {
if (node == null ||
Module.ModuleType == ModuleType.Specialized || Module.ModuleType == ModuleType.Compiled ||
Module.ModuleType == ModuleType.CompiledBuiltin || Module.ModuleType == ModuleType.Builtins) {
return DefaultLocation;
}
IndexSpan indexSpan;
switch (node) {
case MemberExpression mex:
indexSpan = mex.GetNameSpan(Ast).ToIndexSpan(Ast);
break;
case ClassDefinition cd:
indexSpan = cd.NameExpression.IndexSpan;
break;
case FunctionDefinition fd:
indexSpan = fd.NameExpression.IndexSpan;
break;
case NameExpression nex:
indexSpan = nex.IndexSpan;
break;
default:
indexSpan = node.IndexSpan;
break;
}
// Sanity check that AST matches. If it is not, indexSpan typically
// turns into span at line 1 and very large column. This DOES can
// produce false positives occasionally.
#if DEBUG
var sourceSpan = indexSpan.ToSourceSpan(Ast);
Debug.Assert(sourceSpan.Start.Line > 1 || sourceSpan.Start.Column < 1000);
#endif
return new Location(Module, indexSpan);
}
#region IExpressionEvaluator
public PythonAst Ast { get; }
public IPythonModule Module { get; }
public IPythonInterpreter Interpreter => Module.Interpreter;
public IServiceContainer Services { get; }
IScope IExpressionEvaluator.CurrentScope => CurrentScope;
IGlobalScope IExpressionEvaluator.GlobalScope => GlobalScope;
public LocationInfo GetLocation(Node node) => node?.GetLocation(this) ?? LocationInfo.Empty;
public IEnumerable<DiagnosticsEntry> Diagnostics => _diagnostics;
public void ReportDiagnostics(Uri documentUri, DiagnosticsEntry entry) {
// Do not add if module is library, etc. Only handle user code.
if (entry.ShouldReport(Module)) {
lock (_lock) {
_diagnostics.Add(entry);
}
}
}
public IDisposable OpenScope(IScope scope) {
if (!(scope is Scope s)) {
return Disposable.Empty;
}
_openScopes.Push(s);
CurrentScope = s;
return new ScopeTracker(this);
}
public IDisposable OpenScope(IPythonModule module, ScopeStatement scope) => OpenScope(module, scope, out _);
#endregion
public IMember GetValueFromExpression(Expression expr, LookupOptions lookupOptions = LookupOptions.Normal) {
if (expr == null) {
return null;
}
expr = expr.RemoveParenthesis();
IMember m;
switch (expr) {
case NameExpression nex:
m = GetValueFromName(nex, lookupOptions);
break;
case MemberExpression mex:
m = GetValueFromMember(mex, lookupOptions);
break;
case CallExpression cex:
m = GetValueFromCallable(cex, lookupOptions);
break;
case UnaryExpression uex:
m = GetValueFromUnaryOp(uex, lookupOptions);
break;
case IndexExpression iex:
m = GetValueFromIndex(iex, lookupOptions);
break;
case ConditionalExpression coex:
m = GetValueFromConditional(coex, lookupOptions);
break;
case ListExpression listex:
m = GetValueFromList(listex, lookupOptions);
break;
case DictionaryExpression dictex:
m = GetValueFromDictionary(dictex, lookupOptions);
break;
case SetExpression setex:
m = GetValueFromSet(setex, lookupOptions);
break;
case TupleExpression tex:
m = GetValueFromTuple(tex, lookupOptions);
break;
case YieldExpression yex:
m = GetValueFromExpression(yex.Expression, lookupOptions);
break;
case GeneratorExpression genex:
m = GetValueFromGenerator(genex, lookupOptions);
break;
case Comprehension comp:
m = GetValueFromComprehension(comp, lookupOptions);
break;
case LambdaExpression lambda:
m = GetValueFromLambda(lambda);
break;
case FString fString:
m = GetValueFromFString(fString);
break;
case FormatSpecifier formatSpecifier:
m = GetValueFromFormatSpecifier(formatSpecifier);
break;
case NamedExpression namedExpr:
m = GetValueFromExpression(namedExpr.Value, lookupOptions);
break;
// indexing with nothing, e.g Generic[]
case ErrorExpression error:
m = null;
break;
default:
m = GetValueFromBinaryOp(expr, lookupOptions) ?? GetConstantFromLiteral(expr);
break;
}
if (m == null) {
Log?.Log(TraceEventType.Verbose, $"Unknown expression: {expr.ToCodeString(Ast).Trim()}");
}
return m;
}
internal void ClearCache() => _scopeLookupCache.Clear();
private IMember GetValueFromFormatSpecifier(FormatSpecifier formatSpecifier)
=> new PythonFString(formatSpecifier.Unparsed, Interpreter);
private IMember GetValueFromFString(FString fString)
=> new PythonFString(fString.Unparsed, Interpreter);
private IMember GetValueFromName(NameExpression expr, LookupOptions options = LookupOptions.Normal) {
if (expr == null || string.IsNullOrEmpty(expr.Name)) {
return null;
}
var member = LookupNameInScopes(expr.Name, out _, out var v, options);
if (member != null) {
v?.AddReference(GetLocationOfName(expr));
switch (member.GetPythonType()) {
case IPythonClassType cls:
SymbolTable.Evaluate(cls.ClassDefinition);
break;
case IPythonFunctionType fn:
SymbolTable.Evaluate(fn.FunctionDefinition);
break;
case IPythonPropertyType prop:
SymbolTable.Evaluate(prop.FunctionDefinition);
break;
}
return member;
}
Log?.Log(TraceEventType.Verbose, $"Unknown name: {expr.Name}");
return UnknownType;
}
private IMember GetValueFromMember(MemberExpression expr, LookupOptions lookupOptions = LookupOptions.Normal) {
var memberName = expr?.Name;
if (expr?.Target == null || string.IsNullOrEmpty(memberName)) {
return null;
}
var m = GetValueFromExpression(expr.Target, lookupOptions);
if (m == null) {
return UnknownType;
}
var type = m.GetPythonType();
var value = type?.GetMember(memberName);
var location = GetLocationOfName(expr);
type?.AddMemberReference(memberName, this, location);
if (type is IPythonModule) {
return value;
}
IPythonInstance instance = null;
if (m == type) {
// If container is class/type info rather than the instance, then the method is an unbound function.
// Example: C.f where f is a method of C. Compare to C().f where f is bound to the instance of C.
if (value is PythonFunctionType f && f.DeclaringType != null && !f.IsStatic && !f.IsClassMethod) {
f.AddReference(GetLocationOfName(expr));
return f.ToUnbound();
}
instance = type.CreateInstance(ArgumentSet.Empty(expr, this)) as IPythonInstance;
}
instance = instance ?? m as IPythonInstance;
// Class type GetMember returns a type. However, class members are
// mostly instances (consider self.x = 1, x is an instance of int).
// However, it is indeed possible to have them as types, like in
// class X ...
// class C: ...
// self.x = X
// which is somewhat rare as compared to self.x = X() but does happen.
switch (value) {
case IPythonClassType _:
return value;
case IPythonPropertyType prop:
return prop.Call(instance, prop.Name, ArgumentSet.Empty(expr, this));
case IPythonType p:
return new PythonBoundType(p, instance);
case null:
Log?.Log(TraceEventType.Verbose, $"Unknown member {expr.ToCodeString(Ast).Trim()}");
return UnknownType;
default:
return value;
}
}
private IMember GetValueFromConditional(ConditionalExpression expr, LookupOptions lookupOptions = LookupOptions.Normal) {
if (expr == null) {
return null;
}
var trueValue = GetValueFromExpression(expr.TrueExpression, lookupOptions);
var falseValue = GetValueFromExpression(expr.FalseExpression, lookupOptions);
return trueValue ?? falseValue ?? UnknownType;
}
}
}