-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathNUnionType.java
More file actions
237 lines (208 loc) · 7.24 KB
/
NUnionType.java
File metadata and controls
237 lines (208 loc) · 7.24 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
/**
* Copyright 2009, Google Inc. All rights reserved.
* Licensed to PSF under a Contributor Agreement.
*/
package org.python.indexer.types;
import org.python.indexer.Indexer;
import org.python.indexer.NBinding;
import java.util.HashSet;
import java.util.Set;
/**
* A union type is a set of several other types. During a union operation,
* destructuring happens and unknown types are unified.
*/
public class NUnionType extends NType {
/**
* Union types can lead to infinite recursion in the occurs check. Until we've got a handle on
* these cases, I'm limiting the recursion depth.
*
* @see <a href=
* "http://www.cs.kuleuven.ac.be/~dtai/projects/ALP/newsletter/archive_93_96/net/impl/occur.html">
* The Occurs Check</a> for an interesting and highly relevant discussion.
*/
private static final int MAX_RECURSION_DEPTH = 15;
private Set<NType> types;
public NUnionType() {
this.types = new HashSet<NType>();
}
public NUnionType(NType... initialTypes) {
this();
for (NType nt : initialTypes) {
addType(nt);
}
}
public void setTypes(Set<NType> types) {
this.types = types;
}
public Set<NType> getTypes() {
return types;
}
public void addType(NType t) {
if (t == null) {
throw new IllegalArgumentException("null type");
}
if (t.isUnionType()) {
types.addAll(t.asUnionType().types);
} else {
types.add(t);
}
}
public boolean contains(NType t) {
return types.contains(t);
}
public static NType union(NType u, NType v) {
NType wu = NUnknownType.follow(u);
NType wv = NUnknownType.follow(v);
if (wu == wv) {
return u;
}
// This is a bit unconventional, as most type inferencers try to
// determine whether a given name can ever take a null value. However,
// doing so complicates the logic and proliferates union types, arguably
// with little benefit for Python. So for now, X|None => X.
if (wu == Indexer.idx.builtins.None) {
return v;
}
if (wv == Indexer.idx.builtins.None) {
return u;
}
if (wu.isUnknownType() && !occurs(wu, wv, 0)) {
NUnknownType.point(wu, wv);
return u;
}
if (wv.isUnknownType() && !occurs(wv, wu, 0)) {
NUnknownType.point(wv, wu);
return v;
}
if (wu.isTupleType() && wv.isTupleType()) {
NTupleType tu = (NTupleType)wu;
NTupleType tv = (NTupleType)wv;
if (tu.getElementTypes().size() == tv.getElementTypes().size()) {
NTupleType ret = new NTupleType();
for (int i = 0; i < tu.getElementTypes().size(); i++) {
ret.add(union(tu.getElementTypes().get(i), tv.getElementTypes().get(i)));
}
return ret;
}
return newUnion(wu, wv);
}
if (wu.isListType() && wv.isListType()) {
return new NListType(union(wu.asListType().getElementType(),
wv.asListType().getElementType()));
}
if (wu.isDictType() && wv.isDictType()) {
NDictType du = (NDictType)wu;
NDictType dv = (NDictType)wv;
return new NDictType(union(du.getKeyType(), dv.getKeyType()),
union(du.getValueType(), dv.getValueType()));
}
if (wu.isFuncType() && wv.isFuncType()) {
return new NFuncType(NUnionType.union(wu.asFuncType().getReturnType(),
wv.asFuncType().getReturnType()));
}
// XXX: see comments in NInstanceType
if (wu.isFuncType() && wv.isClassType()) {
// NUnknownType.point(wu.asFuncType().getReturnType(), new NInstanceType(wv));
NUnknownType.point(wu.asFuncType().getReturnType(), wv);
NUnknownType.point(u, wv);
return u;
}
if (wu.isClassType() && wv.isFuncType()) {
// NUnknownType.point(wv.asFuncType().getReturnType(), new NInstanceType(wu));
NUnknownType.point(wv.asFuncType().getReturnType(), wu);
NUnknownType.point(v, wu);
return v;
}
return newUnion(wu, wv);
}
/**
* @see <a href="http://en.wikipedia.org/wiki/Occurs_check">Occurs check</a>
*/
private static boolean occurs(NType u, NType v, int depth) {
if (depth++ > MAX_RECURSION_DEPTH) {
return true;
}
u = NUnknownType.follow(u);
v = NUnknownType.follow(v);
if (u == v) {
return true;
}
if (v.isTupleType()) {
for (NType vv : v.asTupleType().getElementTypes()) {
if (occurs(u, vv, depth)) {
return true;
}
}
return false;
}
if (v.isListType()) {
return occurs(u, v.asListType().getElementType(), depth);
}
if (v.isDictType()) {
return occurs(u, v.asDictType().getKeyType(), depth)
|| occurs(u, v.asDictType().getValueType(), depth);
}
if (v.isFuncType()) {
// A function type appearing in its own return type can happen
// (e.g. def foo(): return [foo]), and causes infinite recursion if
// we don't check for it
NType ret = v.asFuncType().getReturnType();
if (occurs(v, ret, depth)) {
return true;
}
return occurs(u, ret, depth);
}
if (v.isUnionType()) {
for (NType vv : v.asUnionType().types) {
if (occurs(u, vv, depth)) {
return true;
}
}
return false;
}
return false;
}
public static NUnionType newUnion(NType... types) {
NUnionType ret = new NUnionType();
for (NType type : types) {
ret.addType(type);
}
return ret;
}
/**
* Returns the first alternate whose type is not unknown.
* @return the first non-unknown alternate, or {@code null} if none found
*/
public NType firstKnownAlternate() {
for (NType type : types) {
if (!type.follow().isUnknownType()) {
return type;
}
}
return null;
}
/**
* Returns the first alternate whose type is not unknown and
* is not {@code Indexer#idx.builtins.None}.
* @return the first non-unknown, non-{@code None} alternate, or {@code null} if none found
*/
public NType firstKnownNonNullAlternate() {
for (NType type : types) {
NType tt = type.follow();
if (!tt.isUnknownType() && tt != Indexer.idx.builtins.None) {
return type;
}
}
return null;
}
@Override
public void printKids(CyclicTypeRecorder ctr, StringBuilder sb) {
sb.append("[");
for (NType u : types) {
u.print(ctr, sb);
sb.append(",");
}
sb.setLength(sb.length() - 1); // pop last comma
sb.append("]");
}
}