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 pathClassEvaluator.cs
More file actions
213 lines (184 loc) · 8.6 KB
/
ClassEvaluator.cs
File metadata and controls
213 lines (184 loc) · 8.6 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
// 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.Collections.Generic;
using System.Linq;
using Microsoft.Python.Analysis.Analyzer.Evaluation;
using Microsoft.Python.Analysis.Diagnostics;
using Microsoft.Python.Analysis.Types;
using Microsoft.Python.Analysis.Values;
using Microsoft.Python.Core;
using Microsoft.Python.Parsing;
using Microsoft.Python.Parsing.Ast;
namespace Microsoft.Python.Analysis.Analyzer.Symbols {
internal sealed class ClassEvaluator : MemberEvaluator {
private readonly ClassDefinition _classDef;
private PythonClassType _class;
public ClassEvaluator(ExpressionEval eval, ClassDefinition classDef) : base(eval, classDef) {
_classDef = classDef;
}
public override void Evaluate() {
EvaluateClass();
Result = _class;
}
public void EvaluateClass() {
// Open class scope chain
using (Eval.OpenScope(Module, _classDef, out var outerScope)) {
var instance = Eval.GetInScope(_classDef.Name, outerScope);
if (!(instance?.GetPythonType() is PythonClassType classInfo)) {
if (instance != null) {
// TODO: warning that variable is already declared of a different type.
}
return;
}
// Evaluate inner classes, if any
EvaluateInnerClasses(_classDef);
_class = classInfo;
var bases = ProcessBases();
_class.SetBases(bases, Eval.CurrentScope);
// Declare __class__ variable in the scope.
Eval.DeclareVariable("__class__", _class, VariableSource.Declaration);
ProcessClassBody();
}
}
private void ProcessClassBody() {
// Class is handled in a specific order rather than in the order of
// the statement appearance. This is because we need all members
// properly declared and added to the class type so when we process
// methods, the class variables are all declared and constructors
// are evaluated.
// Process bases.
foreach (var b in _class.Bases.Select(b => b.GetPythonType<IPythonClassType>()).ExcludeDefault()) {
SymbolTable.Evaluate(b.ClassDefinition);
}
if (!Eval.StubOnlyAnalysis) {
// Process imports
foreach (var s in GetStatements<FromImportStatement>(_classDef)) {
ImportHandler.HandleFromImport(s);
}
foreach (var s in GetStatements<ImportStatement>(_classDef)) {
ImportHandler.HandleImport(s);
}
UpdateClassMembers();
// Process assignments so we get class variables declared.
// Note that annotated definitions and assignments can be intermixed
// and must be processed in order. Consider
// class A:
// x: int
// x = 1
foreach (var s in GetStatements<Statement>(_classDef)) {
switch (s) {
case AssignmentStatement assignment:
AssignmentHandler.HandleAssignment(assignment, LookupOptions.All);
break;
case ExpressionStatement e:
AssignmentHandler.HandleAnnotatedExpression(e.Expression as ExpressionWithAnnotation, null, LookupOptions.All);
break;
}
}
UpdateClassMembers();
// Ensure constructors are processed so class members are initialized.
EvaluateConstructors(_classDef);
UpdateClassMembers();
}
// Process remaining methods.
SymbolTable.EvaluateScope(_classDef);
UpdateClassMembers();
}
private IEnumerable<IPythonType> ProcessBases() {
// Base types must be evaluated in outer scope
using (Eval.OpenScope(Eval.CurrentScope.OuterScope)) {
var bases = new List<IPythonType>();
foreach (var a in _classDef.Bases.Where(a => string.IsNullOrEmpty(a.Name))) {
if (IsValidBase(a, LookupOptions.Normal)) {
TryAddBase(bases, a);
} else {
ReportInvalidBase(a);
}
}
return bases;
}
}
private bool IsValidBase(Arg a, LookupOptions lookupOptions) {
var expr = a.Expression;
var m = Eval.GetValueFromExpression(expr, lookupOptions);
// Allow any unknown members
if (m.IsUnknown()) {
return true;
}
switch (m.MemberType) {
// Inheriting from these members is invalid
case PythonMemberType.Method:
case PythonMemberType.Function:
case PythonMemberType.Property:
case PythonMemberType.Instance:
case PythonMemberType.Variable when m is IPythonConstant:
return false;
}
// Optimistically say anything that passes these checks is a valid base
return true;
}
private void TryAddBase(List<IPythonType> bases, Arg arg) {
// We cheat slightly and treat base classes as annotations.
var b = Eval.GetTypeFromAnnotation(arg.Expression);
if (b != null) {
var t = b.GetPythonType();
bases.Add(t);
t.AddReference(Eval.GetLocationOfName(arg.Expression));
}
}
private void EvaluateConstructors(ClassDefinition cd) {
// Do not use foreach since walker list is dynamically modified and walkers are removed
// after processing. Handle __init__ and __new__ first so class variables are initialized.
var constructors = SymbolTable.Evaluators
.Where(kvp => kvp.Key.Parent == cd && (kvp.Key.Name == "__init__" || kvp.Key.Name == "__new__"))
.Select(c => c.Value)
.ExcludeDefault()
.ToArray();
foreach (var ctor in constructors) {
SymbolTable.Evaluate(ctor);
}
}
private void EvaluateInnerClasses(ClassDefinition cd) {
// Do not use foreach since walker list is dynamically modified and walkers are removed
// after processing. Handle __init__ and __new__ first so class variables are initialized.
var innerClasses = SymbolTable.Evaluators
.Where(kvp => kvp.Key.Parent == cd && (kvp.Key is ClassDefinition))
.Select(c => c.Value)
.ExcludeDefault()
.ToArray();
foreach (var c in innerClasses) {
SymbolTable.Evaluate(c);
}
}
private void UpdateClassMembers() {
// Add members from this file
var members = Eval.CurrentScope.Variables.Where(v => v.Source == VariableSource.Declaration || v.Source == VariableSource.Import);
_class.AddMembers(members, false);
}
private void ReportInvalidBase(Arg arg) {
Eval.ReportDiagnostics(Eval.Module.Uri,
new DiagnosticsEntry(
Resources.InheritNonClass.FormatInvariant(arg.ToCodeString(Eval.Ast, CodeFormattingOptions.Traditional)),
Eval.GetLocation(arg)?.Span ?? default,
Diagnostics.ErrorCodes.InheritNonClass,
Severity.Warning,
DiagnosticSource.Analysis
));
}
// Classes and functions are walked by their respective evaluators
public override bool Walk(ClassDefinition node) => false;
public override bool Walk(FunctionDefinition node) => false;
}
}