-
Notifications
You must be signed in to change notification settings - Fork 82
/
InferTypeArgumentsConstraintsSolver.java
416 lines (358 loc) · 16.6 KB
/
InferTypeArgumentsConstraintsSolver.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*******************************************************************************
* Copyright (c) 2000, 2011 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.corext.refactoring.generics;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.ArrayType;
import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.HierarchyType;
import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType;
import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.EnumeratedTypeSet;
import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.SingletonTypeSet;
import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSet;
import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.typesets.TypeSetEnvironment;
import org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ArrayElementVariable2;
import org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ArrayTypeVariable2;
import org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CastVariable2;
import org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2;
import org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2;
import org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ITypeConstraint2;
import org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.IndependentTypeVariable2;
import org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.TTypes;
import org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.TypeEquivalenceSet;
import org.eclipse.jdt.internal.ui.util.Progress;
public class InferTypeArgumentsConstraintsSolver {
private static class TTypeComparator implements Comparator<TType> {
@Override
public int compare(TType o1, TType o2) {
return o1.getPrettySignature().compareTo(o2.getPrettySignature());
}
public static TTypeComparator INSTANCE= new TTypeComparator();
}
private final static String CHOSEN_TYPE= "chosenType"; //$NON-NLS-1$
private final InferTypeArgumentsTCModel fTCModel;
private TypeSetEnvironment fTypeSetEnvironment;
/**
* The work-list used by the type constraint solver to hold the set of
* nodes in the constraint graph that remain to be (re-)processed. Entries
* are <code>ConstraintVariable2</code>s.
*/
private LinkedList<ConstraintVariable2> fWorkList;
private InferTypeArgumentsUpdate fUpdate;
public InferTypeArgumentsConstraintsSolver(InferTypeArgumentsTCModel typeConstraintFactory) {
fTCModel= typeConstraintFactory;
fWorkList= new LinkedList<>();
}
public InferTypeArgumentsUpdate solveConstraints(IProgressMonitor pm) {
pm.beginTask("", 2); //$NON-NLS-1$
fUpdate= new InferTypeArgumentsUpdate();
ConstraintVariable2[] allConstraintVariables= fTCModel.getAllConstraintVariables();
if (allConstraintVariables.length == 0)
return fUpdate;
fTypeSetEnvironment= new TypeSetEnvironment(fTCModel.getTypeEnvironment());
ParametricStructureComputer parametricStructureComputer= new ParametricStructureComputer(allConstraintVariables, fTCModel);
Collection<CollectionElementVariable2> newVars= parametricStructureComputer.createElemConstraintVariables();
ArrayList<ConstraintVariable2> newAllConstraintVariables= new ArrayList<>(Arrays.asList(allConstraintVariables));
newAllConstraintVariables.addAll(newVars);
allConstraintVariables= newAllConstraintVariables.toArray(new ConstraintVariable2[newAllConstraintVariables.size()]);
//loop over all TypeEquivalenceSets and unify the elements from the fElemStructureEnv with the existing TypeEquivalenceSets
HashSet<TypeEquivalenceSet> allTypeEquivalenceSets= new HashSet<>();
for (ConstraintVariable2 allConstraintVariable : allConstraintVariables) {
TypeEquivalenceSet typeEquivalenceSet= allConstraintVariable.getTypeEquivalenceSet();
if (typeEquivalenceSet != null)
allTypeEquivalenceSets.add(typeEquivalenceSet);
}
for (TypeEquivalenceSet typeEquivalenceSet : allTypeEquivalenceSets) {
ConstraintVariable2[] contributingVariables= typeEquivalenceSet.getContributingVariables();
for (int i= 0; i < contributingVariables.length; i++) {
for (int j= i + 1; j < contributingVariables.length; j++) {
ConstraintVariable2 first= contributingVariables[i];
ConstraintVariable2 second= contributingVariables[j];
fTCModel.createElementEqualsConstraints(first, second); // recursively
}
}
}
for (ITypeConstraint2 typeConstraint : fTCModel.getAllTypeConstraints()) {
fTCModel.createElementEqualsConstraints(typeConstraint.getLeft(), typeConstraint.getRight());
}
initializeTypeEstimates(allConstraintVariables);
if (pm.isCanceled())
throw new OperationCanceledException();
fWorkList.addAll(Arrays.asList(allConstraintVariables));
runSolver(Progress.subMonitor(pm, 1));
chooseTypes(allConstraintVariables, Progress.subMonitor(pm, 1));
findCastsToRemove(fTCModel.getCastVariables());
return fUpdate;
}
private void initializeTypeEstimates(ConstraintVariable2[] allConstraintVariables) {
for (ConstraintVariable2 cv : allConstraintVariables) {
//TODO: not necessary for types that are not used in a TypeConstraint but only as type in CollectionElementVariable
//TODO: handle nested element variables; see ParametricStructureComputer.createAndInitVars()
TypeEquivalenceSet set= cv.getTypeEquivalenceSet();
if (set == null) {
set= new TypeEquivalenceSet(cv);
set.setTypeEstimate(createInitialEstimate(cv));
cv.setTypeEquivalenceSet(set);
} else {
TypeSet typeEstimate= (TypeSet) cv.getTypeEstimate();
if (typeEstimate == null) {
typeEstimate= fTypeSetEnvironment.getUniverseTypeSet();
for (ConstraintVariable2 c : set.getContributingVariables()) { //TODO: optimize: just try to find an immutable CV; if not found, use Universe
typeEstimate= typeEstimate.intersectedWith(createInitialEstimate(c));
}
set.setTypeEstimate(typeEstimate);
}
}
}
}
private TypeSet createInitialEstimate(ConstraintVariable2 cv) {
// TODO: check assumption: only immutable CVs have a type
// ParametricStructure parametricStructure= fElemStructureEnv.elemStructure(cv);
// if (parametricStructure != null && parametricStructure != ParametricStructureComputer.ParametricStructure.NONE) {
// return SubTypesOfSingleton.create(parametricStructure.getBase());
// }
TType type= cv.getType();
if (type == null
|| cv instanceof IndependentTypeVariable2
|| cv instanceof ArrayTypeVariable2) {
return fTypeSetEnvironment.getUniverseTypeSet();
//TODO: solve problem with recursive bounds
// TypeVariable tv= (TypeVariable) type;
// TType[] bounds= tv.getBounds();
// TypeSet result= SubTypesOfSingleton.create(bounds[0].getErasure());
// for (int i= 1; i < bounds.length; i++) {
// result= result.intersectedWith(SubTypesOfSingleton.create(bounds[i].getErasure()));
// }
// return result;
} else if (cv instanceof ArrayElementVariable2) {
if (cv.getType() != null && cv.getType().isTypeVariable()) {
return fTypeSetEnvironment.getUniverseTypeSet();
} else {
return new SingletonTypeSet(type, fTypeSetEnvironment);
}
} else if (type.isVoidType()) {
return fTypeSetEnvironment.getEmptyTypeSet();
} else {
return new SingletonTypeSet(type, fTypeSetEnvironment);
}
}
private void runSolver(IProgressMonitor pm) {
pm.beginTask("", fWorkList.size() * 3); //$NON-NLS-1$
while (! fWorkList.isEmpty()) {
// Get a variable whose type estimate has changed
ConstraintVariable2 cv= fWorkList.removeFirst();
List<ITypeConstraint2> usedIn= fTCModel.getUsedIn(cv);
processConstraints(usedIn);
pm.worked(1);
if (pm.isCanceled())
throw new OperationCanceledException();
}
pm.done();
}
/**
* Given a list of <code>ITypeConstraint2</code>s that all refer to a
* given <code>ConstraintVariable2</code> (whose type bound has presumably
* just changed), process each <code>ITypeConstraint</code>, propagating
* the type bound across the constraint as needed.
*
* @param usedIn the <code>List</code> of <code>ITypeConstraint2</code>s
* to process
*/
private void processConstraints(List<ITypeConstraint2> usedIn) {
Iterator<ITypeConstraint2> iter= usedIn.iterator();
while (iter.hasNext()) {
ITypeConstraint2 tc= iter.next();
maintainSimpleConstraint(tc);
//TODO: prune tcs which cannot cause further changes
// Maybe these should be pruned after a special first loop over all ConstraintVariables,
// Since this can only happen once for every CV in the work list.
// if (isConstantConstraint(stc))
// fTypeConstraintFactory.removeUsedIn(stc, changedCv);
}
}
private void maintainSimpleConstraint(ITypeConstraint2 stc) {
ConstraintVariable2 left= stc.getLeft();
ConstraintVariable2 right= stc.getRight();
TypeEquivalenceSet leftSet= left.getTypeEquivalenceSet();
TypeEquivalenceSet rightSet= right.getTypeEquivalenceSet();
TypeSet leftEstimate= (TypeSet) leftSet.getTypeEstimate();
TypeSet rightEstimate= (TypeSet) rightSet.getTypeEstimate();
if (leftEstimate.isUniverse() && rightEstimate.isUniverse())
return; // nothing to do
if (leftEstimate.equals(rightEstimate))
return; // nothing to do
TypeSet lhsSuperTypes= leftEstimate.superTypes();
TypeSet rhsSubTypes= rightEstimate.subTypes();
if (! rhsSubTypes.containsAll(leftEstimate)) {
TypeSet xsection= leftEstimate.intersectedWith(rhsSubTypes);
// if (xsection.isEmpty()) // too bad, but this can happen
// throw new IllegalStateException("Type estimate set is now empty for LHS in " + left + " <= " + right + "; estimates were " + leftEstimate + " <= " + rightEstimate); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
leftSet.setTypeEstimate(xsection);
fWorkList.addAll(Arrays.asList(leftSet.getContributingVariables()));
}
if (! lhsSuperTypes.containsAll(rightEstimate)) {
TypeSet xsection= rightEstimate.intersectedWith(lhsSuperTypes);
// if (xsection.isEmpty())
// throw new IllegalStateException("Type estimate set is now empty for RHS in " + left + " <= " + right + "; estimates were " + leftEstimate + " <= " + rightEstimate); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
rightSet.setTypeEstimate(xsection);
fWorkList.addAll(Arrays.asList(rightSet.getContributingVariables()));
}
}
private void chooseTypes(ConstraintVariable2[] allConstraintVariables, IProgressMonitor pm) {
pm.beginTask("", allConstraintVariables.length); //$NON-NLS-1$
for (ConstraintVariable2 cv : allConstraintVariables) {
TypeEquivalenceSet set= cv.getTypeEquivalenceSet();
if (set == null)
continue; //TODO: should not happen iff all unused constraint variables got pruned
//TODO: should calculate only once per EquivalenceRepresentative; can throw away estimate TypeSet afterwards
TType type= chooseSingleType((TypeSet) cv.getTypeEstimate()); //TODO: is null for Universe TypeSet
setChosenType(cv, type);
if (cv instanceof CollectionElementVariable2) {
CollectionElementVariable2 elementCv= (CollectionElementVariable2) cv;
fUpdate.addDeclaration(elementCv);
}
pm.worked(1);
if (pm.isCanceled())
throw new OperationCanceledException();
}
pm.done();
}
private TType chooseSingleType(TypeSet typeEstimate) {
if (typeEstimate.isUniverse() || typeEstimate.isEmpty()) {
return null;
} else if (typeEstimate.hasUniqueLowerBound()) {
return typeEstimate.uniqueLowerBound();
} else {
EnumeratedTypeSet lowerBound= typeEstimate.lowerBound().enumerate();
ArrayList<TType> interfaceCandidates= null;
for (Iterator<TType> iter= lowerBound.iterator(); iter.hasNext();) {
TType type= iter.next();
if (! type.isInterface()) {
return type;
} else {
if (interfaceCandidates == null)
interfaceCandidates= new ArrayList<>(2);
interfaceCandidates.add(type);
}
}
if (interfaceCandidates == null || interfaceCandidates.isEmpty()) {
return null;
} else if (interfaceCandidates.size() == 1) {
return interfaceCandidates.get(0);
} else {
ArrayList<TType> nontaggingCandidates= getNonTaggingInterfaces(interfaceCandidates);
if (!nontaggingCandidates.isEmpty()) {
return Collections.min(nontaggingCandidates, TTypeComparator.INSTANCE);
} else {
return Collections.min(interfaceCandidates, TTypeComparator.INSTANCE);
}
}
}
}
private static final int MAX_CACHE= 1024;
private Map<TType, Boolean> fInterfaceTaggingCache= new LinkedHashMap<>(MAX_CACHE, 0.75f, true) {
private static final long serialVersionUID= 1L;
@Override
protected boolean removeEldestEntry(Map.Entry<TType, Boolean> eldest) {
return size() > MAX_CACHE;
}
};
private ArrayList<TType> getNonTaggingInterfaces(ArrayList<TType> interfaceCandidates) {
ArrayList<TType> unresolvedTypes= new ArrayList<>();
ArrayList<TType> nonTagging= new ArrayList<>();
for (int i= 0; i < interfaceCandidates.size(); i++) {
TType interf= interfaceCandidates.get(i);
Boolean isTagging= fInterfaceTaggingCache.get(interf);
if (isTagging == null)
unresolvedTypes.add(interf);
else if (Boolean.FALSE.equals(isTagging))
nonTagging.add(interf);
}
if (!unresolvedTypes.isEmpty()) {
for (TType interf : unresolvedTypes) {
if (isTaggingInterface(interf)) {
fInterfaceTaggingCache.put(interf, Boolean.TRUE);
} else {
fInterfaceTaggingCache.put(interf, Boolean.FALSE);
nonTagging.add(interf);
}
}
}
return nonTagging;
}
private static boolean isTaggingInterface(TType interf) {
if (interf instanceof HierarchyType) {
try {
return ((HierarchyType) interf).getJavaElementType().getMethods().length == 0;
} catch (JavaModelException e) {
// assume it's not
}
}
return false;
}
private void findCastsToRemove(CastVariable2[] castVariables) {
for (CastVariable2 castCv : castVariables) {
ConstraintVariable2 expressionVariable= castCv.getExpressionVariable();
TType chosenType= InferTypeArgumentsConstraintsSolver.getChosenType(expressionVariable);
TType castType= castCv.getType();
TType expressionType= expressionVariable.getType();
if (chosenType != null && TTypes.canAssignTo(chosenType, castType)) {
if (chosenType.equals(expressionType))
continue; // The type has not changed. Don't remove the cast, since it could be
// there to get access to default-visible members or to
// unify types of conditional expressions.
fUpdate.addCastToRemove(castCv);
} else if (expressionVariable instanceof ArrayTypeVariable2 && castType.isArrayType()) { // bug 97258
ArrayElementVariable2 arrayElementCv= fTCModel.getArrayElementVariable(expressionVariable);
if (arrayElementCv == null)
continue;
TType chosenArrayElementType= InferTypeArgumentsConstraintsSolver.getChosenType(arrayElementCv);
if (chosenArrayElementType != null && TTypes.canAssignTo(chosenArrayElementType, ((ArrayType) castType).getComponentType())) {
if (expressionType instanceof ArrayType && chosenArrayElementType.equals(((ArrayType) expressionType).getComponentType()))
continue; // The type has not changed. Don't remove the cast, since it could be
// there to unify types of conditional expressions.
fUpdate.addCastToRemove(castCv);
}
}
}
}
public static TType getChosenType(ConstraintVariable2 cv) {
TType type= (TType) cv.getData(CHOSEN_TYPE);
if (type != null)
return type;
TypeEquivalenceSet set= cv.getTypeEquivalenceSet();
if (set == null) { //TODO: should not have to set this here. Clean up when caching chosen type
return null;
// // no representative == no restriction
// set= new TypeEquivalenceSet(cv);
// set.setTypeEstimate(TypeUniverseSet.create());
// cv.setTypeEquivalenceSet(set);
}
return cv.getTypeEstimate().chooseSingleType();
}
private static void setChosenType(ConstraintVariable2 cv, TType type) {
cv.setData(CHOSEN_TYPE, type);
}
}