-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathNameBinder.java
More file actions
216 lines (192 loc) · 7.59 KB
/
NameBinder.java
File metadata and controls
216 lines (192 loc) · 7.59 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
/**
* Copyright 2009, Google Inc. All rights reserved.
* Licensed to PSF under a Contributor Agreement.
*/
package org.python.indexer.ast;
import org.python.indexer.Indexer;
import org.python.indexer.NBinding;
import org.python.indexer.types.NListType;
import org.python.indexer.types.NTupleType;
import org.python.indexer.types.NType;
import org.python.indexer.types.NUnknownType;
import org.python.indexer.Scope;
import java.util.List;
import static org.python.indexer.NBinding.Kind.ATTRIBUTE;
import static org.python.indexer.NBinding.Kind.CLASS;
import static org.python.indexer.NBinding.Kind.CONSTRUCTOR;
import static org.python.indexer.NBinding.Kind.FUNCTION;
import static org.python.indexer.NBinding.Kind.METHOD;
import static org.python.indexer.NBinding.Kind.MODULE;
import static org.python.indexer.NBinding.Kind.PARAMETER;
import static org.python.indexer.NBinding.Kind.SCOPE;
import static org.python.indexer.NBinding.Kind.VARIABLE;
/**
* Handles binding names to scopes, including destructuring assignment.
*/
public class NameBinder {
static private final NameBinder DEFAULT_BINDER = new NameBinder();
static private final NameBinder ATTRIBUTE_BINDER = new NameBinder(ATTRIBUTE);
static private final NameBinder CLASS_BINDER = new NameBinder(CLASS);
static private final NameBinder CONSTRUCTOR_BINDER = new NameBinder(CONSTRUCTOR);
static private final NameBinder FUNCTION_BINDER = new NameBinder(FUNCTION);
static private final NameBinder METHOD_BINDER = new NameBinder(METHOD);
static private final NameBinder MODULE_BINDER = new NameBinder(MODULE);
static private final NameBinder PARAMETER_BINDER = new NameBinder(PARAMETER);
static private final NameBinder VARIABLE_BINDER = new NameBinder(VARIABLE);
private NBinding.Kind kind;
/**
* Factory method for creating instances.
*/
public static NameBinder make() {
return DEFAULT_BINDER;
}
/**
* Factory method for creating instances.
* @return a {@code NameBinder} that will create bindings of {@code kind},
* overriding the default choices.
*/
public static NameBinder make(NBinding.Kind kind) {
switch (kind) {
case ATTRIBUTE:
return ATTRIBUTE_BINDER;
case CLASS:
return CLASS_BINDER;
case CONSTRUCTOR:
return CONSTRUCTOR_BINDER;
case FUNCTION:
return FUNCTION_BINDER;
case METHOD:
return METHOD_BINDER;
case MODULE:
return MODULE_BINDER;
case PARAMETER:
return PARAMETER_BINDER;
case VARIABLE:
return VARIABLE_BINDER;
default:
return DEFAULT_BINDER;
}
}
private NameBinder() {
}
private NameBinder(NBinding.Kind kind) {
this.kind = kind;
}
public void bind(Scope s, NNode target, NType rvalue) throws Exception {
if (target instanceof NName) {
bindName(s, (NName)target, rvalue);
return;
}
if (target instanceof NTuple) {
bind(s, ((NTuple)target).elts, rvalue);
return;
}
if (target instanceof NList) {
bind(s, ((NList)target).elts, rvalue);
return;
}
if (target instanceof NAttribute) {
// This causes various problems if we let it happen during the
// name-binding pass. I believe the only name-binding context
// in which an NAttribute can be an lvalue is in an assignment.
// Assignments are statements, so they can only appear in blocks.
// Hence the scope for the top-level name is unambiguous; we can
// safely leave binding it until the resolve pass.
if (!s.isNameBindingPhase()) {
((NAttribute)target).setAttr(s, rvalue);
}
return;
}
if (target instanceof NSubscript) {
// Ditto. No resolving is allowed during the name-binding phase.
if (!s.isNameBindingPhase()) {
target.resolveExpr(target, s);
}
return;
}
Indexer.idx.putProblem(target, "invalid location for assignment");
}
public void bind(Scope s, List<NNode> xs, NType rvalue) throws Exception {
if (rvalue.isTupleType()) {
List<NType> vs = rvalue.asTupleType().getElementTypes();
if (xs.size() != vs.size()) {
reportUnpackMismatch(xs, vs.size());
} else {
for (int i = 0; i < xs.size(); i++) {
bind(s, xs.get(i), vs.get(i));
}
}
return;
}
if (rvalue.isListType()) {
bind(s, xs, rvalue.asListType().toTupleType(xs.size()));
return;
}
if (rvalue.isDictType()) {
bind(s, xs, rvalue.asDictType().toTupleType(xs.size()));
return;
}
if (!rvalue.isUnknownType()) {
Indexer.idx.putProblem(xs.get(0).getFile(),
xs.get(0).start(),
xs.get(xs.size()-1).end(),
"unpacking non-iterable: " + rvalue);
}
for (int i = 0; i < xs.size(); i++) {
bind(s, xs.get(i), new NUnknownType());
}
}
public NBinding bindName(Scope s, NName name, NType rvalue) throws Exception {
NBinding b;
if (s.isGlobalName(name.id)) {
b = s.getGlobalTable().put(name.id, name, rvalue, kindOr(SCOPE));
Indexer.idx.putLocation(name, b);
} else {
Scope bindingScope = s.getScopeSymtab();
b = bindingScope.put(name.id, name, rvalue,
kindOr(bindingScope.isFunctionScope() ? VARIABLE : SCOPE));
}
name.setType(b.followType());
// XXX: this seems like a bit of a hack; should at least figure out
// and document what use cases require it.
NType nameType = name.getType();
if (!(nameType.isModuleType() || nameType.isClassType())) {
nameType.getTable().setPath(b.getQname());
}
return b;
}
public void bindIter(Scope s, NNode target, NNode iter) throws Exception {
NType iterType = NNode.resolveExpr(iter, s);
if (iterType.isListType()) {
bind(s, target, iterType.asListType().getElementType());
} else if (iterType.isTupleType()) {
bind(s, target, iterType.asTupleType().toListType().getElementType());
} else {
NBinding ent = iterType.getTable().lookupAttr("__iter__");
if (ent == null || !ent.getType().isFuncType()) {
if (!iterType.isUnknownType()) {
iter.addWarning("not an iterable type: " + iterType);
}
bind(s, target, new NUnknownType());
} else {
bind(s, target, ent.getType().asFuncType().getReturnType());
}
}
}
private void reportUnpackMismatch(List<NNode> xs, int vsize) {
int xsize = xs.size();
int beg = xs.get(0).start();
int end = xs.get(xs.size() - 1).end();
int diff = xsize - vsize;
String msg;
if (diff > 0) {
msg = "ValueError: need more than " + vsize + " values to unpack";
} else {
msg = "ValueError: too many values to unpack";
}
Indexer.idx.putProblem(xs.get(0).getFile(), beg, end, msg);
}
private NBinding.Kind kindOr(NBinding.Kind k) {
return kind != null ? kind : k;
}
}