-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
DeclaredFunctionType.java
395 lines (359 loc) · 13.6 KB
/
DeclaredFunctionType.java
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
/*
* Copyright 2013 The Closure Compiler Authors.
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp.newtypes;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* This class represents the function types for functions that are defined
* statically in the code.
*
* Since these types may have incomplete ATparam and ATreturns JSDoc, this
* class needs to allow null JSTypes for undeclared formals/return.
* Used in the Scope class.
*
* @author blickly@google.com (Ben Lickly)
* @author dimvar@google.com (Dimitris Vardoulakis)
*/
public final class DeclaredFunctionType implements Serializable {
private final List<JSType> requiredFormals;
private final List<JSType> optionalFormals;
private final JSType restFormals;
private final JSType returnType;
// If this DeclaredFunctionType is a constructor/interface, this field stores
// the type of the instance.
private final JSType nominalType;
// If this DeclaredFunctionType is a prototype method, this field stores the
// type of the instance.
private final JSType receiverType;
private final TypeParameters typeParameters;
private final JSTypes commonTypes;
private final boolean isAbstract;
private DeclaredFunctionType(
JSTypes commonTypes,
List<JSType> requiredFormals,
List<JSType> optionalFormals,
JSType restFormals,
JSType retType,
JSType nominalType,
JSType receiverType,
TypeParameters typeParameters,
boolean isAbstract) {
checkArgument(retType == null || !retType.isBottom());
checkNotNull(commonTypes);
this.commonTypes = commonTypes;
this.requiredFormals = requiredFormals;
this.optionalFormals = optionalFormals;
this.restFormals = restFormals;
this.returnType = retType;
this.nominalType = nominalType;
this.receiverType = receiverType;
this.typeParameters = typeParameters;
this.isAbstract = isAbstract;
}
public FunctionType toFunctionType() {
FunctionTypeBuilder builder = new FunctionTypeBuilder(this.commonTypes);
for (JSType formal : this.requiredFormals) {
builder.addReqFormal(formal == null ? this.commonTypes.UNKNOWN : formal);
}
for (JSType formal : this.optionalFormals) {
builder.addOptFormal(formal == null ? this.commonTypes.UNKNOWN : formal);
}
builder.addRestFormals(this.restFormals);
builder.addRetType(this.returnType == null ? this.commonTypes.UNKNOWN : this.returnType);
builder.addNominalType(this.nominalType);
builder.addReceiverType(this.receiverType);
builder.addTypeParameters(this.typeParameters);
builder.addAbstract(this.isAbstract);
return builder.buildFunction();
}
static DeclaredFunctionType make(
JSTypes commonTypes,
List<JSType> requiredFormals,
List<JSType> optionalFormals,
JSType restFormals,
JSType retType,
JSType nominalType,
JSType receiverType,
TypeParameters typeParameters,
boolean isAbstract) {
if (requiredFormals == null) {
requiredFormals = new ArrayList<>();
}
if (optionalFormals == null) {
optionalFormals = new ArrayList<>();
}
return new DeclaredFunctionType(
commonTypes,
requiredFormals, optionalFormals, restFormals, retType,
nominalType, receiverType, typeParameters, isAbstract);
}
static DeclaredFunctionType qmarkFunctionDeclaration(JSTypes commonTypes) {
FunctionTypeBuilder builder = new FunctionTypeBuilder(commonTypes);
builder.addRestFormals(commonTypes.UNKNOWN);
builder.addRetType(commonTypes.UNKNOWN);
return builder.buildDeclaration();
}
// 0-indexed
public JSType getFormalType(int argpos) {
int numReqFormals = requiredFormals.size();
if (argpos < numReqFormals) {
return requiredFormals.get(argpos);
} else if (argpos < numReqFormals + optionalFormals.size()) {
return optionalFormals.get(argpos - numReqFormals);
} else {
// TODO(blickly): Distinguish between undeclared varargs and no varargs.
return restFormals;
}
}
public int getRequiredArity() {
return requiredFormals.size();
}
public int getOptionalArity() {
return requiredFormals.size() + optionalFormals.size();
}
public int getMaxArity() {
if (this.restFormals != null) {
return Integer.MAX_VALUE; // "Infinite" arity
} else {
return this.getOptionalArity();
}
}
private int getSyntacticArity() {
return this.getOptionalArity() + (this.restFormals == null ? 0 : 1);
}
public boolean hasRestFormals() {
return restFormals != null;
}
public JSType getRestFormalsType() {
checkState(restFormals != null);
return restFormals;
}
public JSType getReturnType() {
return returnType;
}
public JSType getThisType() {
if (this.nominalType != null) {
return this.nominalType;
} else {
return this.receiverType;
}
}
public JSType getNominalType() {
return this.nominalType;
}
public JSType getReceiverType() {
return this.receiverType;
}
public boolean isGeneric() {
return !typeParameters.isEmpty();
}
public TypeParameters getTypeParameters() {
return this.typeParameters;
}
public boolean isAbstract() {
return this.isAbstract;
}
public boolean isTypeVariableDefinedLocally(String tvar) {
return getTypeVariableDefinedLocally(tvar) != null;
}
public String getTypeVariableDefinedLocally(String tvar) {
String tmp = UniqueNameGenerator.findGeneratedName(tvar, this.typeParameters.asList());
if (tmp != null) {
return tmp;
}
// We don't look at this.nominalType, b/c if this function is a generic
// constructor, then typeParameters contains the relevant type variables.
if (this.receiverType != null) {
NominalType recvType = this.receiverType.getNominalTypeIfSingletonObj();
if (recvType != null && recvType.isUninstantiatedGenericType()) {
RawNominalType rawType = recvType.getRawNominalType();
tmp = UniqueNameGenerator.findGeneratedName(tvar, rawType.getTypeParameters());
if (tmp != null) {
return tmp;
}
}
}
return null;
}
public DeclaredFunctionType withReceiverType(JSType newReceiverType) {
return new DeclaredFunctionType(
this.commonTypes,
this.requiredFormals, this.optionalFormals,
this.restFormals, this.returnType, this.nominalType,
newReceiverType, this.typeParameters, this.isAbstract);
}
public DeclaredFunctionType withTypeInfoFromSuper(
DeclaredFunctionType superType, boolean getsTypeInfoFromParentMethod) {
// getsTypeInfoFromParentMethod is true when a method w/out jsdoc overrides
// a parent method. In this case, the parent may be declaring some formals
// as optional and we want to preserve that type information here.
if (getsTypeInfoFromParentMethod
&& getSyntacticArity() == superType.getSyntacticArity()) {
NominalType nt = superType.nominalType == null
? null : superType.nominalType.getNominalTypeIfSingletonObj();
// Only keep this.receiverType from the current type
NominalType rt = this.receiverType == null
? null : this.receiverType.getNominalTypeIfSingletonObj();
return new DeclaredFunctionType(
this.commonTypes,
superType.requiredFormals, superType.optionalFormals,
superType.restFormals, superType.returnType,
nt == null ? null : nt.getInstanceAsJSType(),
rt == null ? null : rt.getInstanceAsJSType(),
superType.typeParameters, this.isAbstract);
}
FunctionTypeBuilder builder = new FunctionTypeBuilder(this.commonTypes);
int i = 0;
for (JSType formal : this.requiredFormals) {
builder.addReqFormal(formal != null ? formal : superType.getFormalType(i));
i++;
}
for (JSType formal : this.optionalFormals) {
builder.addOptFormal(formal != null ? formal : superType.getFormalType(i));
i++;
}
if (this.restFormals != null) {
builder.addRestFormals(this.restFormals);
} else if (superType.hasRestFormals()) {
builder.addRestFormals(superType.restFormals);
}
builder.addRetType(
this.returnType != null ? this.returnType : superType.returnType);
builder.addNominalType(this.nominalType);
builder.addReceiverType(this.receiverType);
if (!this.typeParameters.isEmpty()) {
builder.addTypeParameters(this.typeParameters);
} else if (!superType.typeParameters.isEmpty()) {
builder.addTypeParameters(superType.typeParameters);
}
builder.addAbstract(this.isAbstract);
return builder.buildDeclaration();
}
// Analogous to FunctionType#substituteNominalGenerics
public DeclaredFunctionType substituteNominalGenerics(NominalType nt) {
if (!nt.isGeneric()) {
return this;
}
FunctionTypeBuilder builder = substituteGenerics(nt.getTypeMap());
// Explicitly forget nominalType and receiverType. This method is used when
// calculating the declared type of a method using the inherited types.
// In withTypeInfoFromSuper, we ignore super's nominalType and receiverType.
builder.addReceiverType(null);
builder.addNominalType(null);
return builder.buildDeclaration();
}
public DeclaredFunctionType instantiateGenericsWithUnknown() {
return substituteGenerics(this.commonTypes.MAP_TO_UNKNOWN).buildDeclaration();
}
private FunctionTypeBuilder substituteGenerics(Map<String, JSType> typeMap) {
checkState(!typeMap.isEmpty());
// Before we switched to unique generated names for type variables, a method's type variables
// could shadow type variables defined on the class. Check that this no longer happens.
if (!this.commonTypes.MAP_TO_UNKNOWN.equals(typeMap)) {
for (String typeParam : this.typeParameters.asList()) {
checkState(!typeMap.containsKey(typeParam));
}
}
FunctionTypeBuilder builder = new FunctionTypeBuilder(this.commonTypes);
for (JSType reqFormal : requiredFormals) {
builder.addReqFormal(reqFormal == null ? null : reqFormal.substituteGenerics(typeMap));
}
for (JSType optFormal : optionalFormals) {
builder.addOptFormal(optFormal == null ? null : optFormal.substituteGenerics(typeMap));
}
if (restFormals != null) {
builder.addRestFormals(restFormals.substituteGenerics(typeMap));
}
if (returnType != null) {
builder.addRetType(returnType.substituteGenerics(typeMap));
}
if (this.receiverType != null) {
builder.addReceiverType(this.receiverType.substituteGenerics(typeMap));
}
if (this.nominalType != null) {
builder.addNominalType(this.nominalType.substituteGenerics(typeMap));
}
// Technically, we should only be adding the type parameters that haven't been substituted
// away, but AFAICT, it makes no difference to just add them all.
builder.addTypeParameters(this.typeParameters);
return builder;
}
public static DeclaredFunctionType meet(Collection<DeclaredFunctionType> toMeet) {
DeclaredFunctionType result = null;
for (DeclaredFunctionType declType : toMeet) {
if (result == null) {
result = declType;
} else {
result = DeclaredFunctionType.meet(result, declType);
}
}
return result;
}
private static DeclaredFunctionType meet(DeclaredFunctionType f1, DeclaredFunctionType f2) {
if (f1.equals(f2)) {
return f1;
}
JSTypes commonTypes = f1.commonTypes;
FunctionTypeBuilder builder = new FunctionTypeBuilder(f1.commonTypes);
int minRequiredArity = Math.min(f1.requiredFormals.size(), f2.requiredFormals.size());
for (int i = 0; i < minRequiredArity; i++) {
builder.addReqFormal(nullAcceptingJoin(f1.getFormalType(i), f2.getFormalType(i)));
}
int maxTotalArity = Math.max(
f1.requiredFormals.size() + f1.optionalFormals.size(),
f2.requiredFormals.size() + f2.optionalFormals.size());
for (int i = minRequiredArity; i < maxTotalArity; i++) {
builder.addOptFormal(nullAcceptingJoin(f1.getFormalType(i), f2.getFormalType(i)));
}
if (f1.restFormals != null || f2.restFormals != null) {
builder.addRestFormals(nullAcceptingJoin(f1.restFormals, f2.restFormals));
}
JSType retType = nullAcceptingMeet(f1.returnType, f2.returnType);
if (commonTypes.BOTTOM.equals(retType)) {
return null;
}
builder.addRetType(retType);
return builder.buildDeclaration();
}
// Returns possibly-null JSType
private static JSType nullAcceptingJoin(JSType t1, JSType t2) {
if (t1 == null) {
return t2;
} else if (t2 == null) {
return t1;
}
return JSType.join(t1, t2);
}
// Returns possibly-null JSType
private static JSType nullAcceptingMeet(JSType t1, JSType t2) {
if (t1 == null) {
return t2;
} else if (t2 == null) {
return t1;
}
return JSType.meet(t1, t2);
}
@Override
public String toString() {
return toFunctionType().toString();
}
}