Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 447df4672086719c5dde90d3084f0f018b1f3116 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2013, 2019 GK Software AG.
 *
 * 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:
 *     Stephan Herrmann - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.internal.compiler.lookup;

import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.ast.Wildcard;

/**
 * Capture-like type variable introduced during 1.8 type inference.
 */
public class CaptureBinding18 extends CaptureBinding {
	
	TypeBinding[] upperBounds;
	private char[] originalName;
	private CaptureBinding18 prototype;

	public CaptureBinding18(ReferenceBinding contextType, char[] sourceName, char[] originalName, int start, int end, int captureID, LookupEnvironment environment) {
		super(contextType, sourceName, start, end, captureID, environment);
		this.originalName = originalName;
		this.prototype = this;
	}
	
	private CaptureBinding18(CaptureBinding18 prototype) {
		super(prototype);
		this.sourceName = CharOperation.append(prototype.sourceName, '\'');
		this.originalName = prototype.originalName;
		this.upperBounds = prototype.upperBounds;
		this.prototype = prototype.prototype;		
	}

	public boolean setUpperBounds(TypeBinding[] upperBounds, ReferenceBinding javaLangObject) {
		this.upperBounds = upperBounds;
		if (upperBounds.length > 0)
			this.firstBound = upperBounds[0];
		int numReferenceInterfaces = 0;
		if (!isConsistentIntersection(upperBounds))
			return false;
		for (int i = 0; i < upperBounds.length; i++) {
			TypeBinding aBound = upperBounds[i];
			if (aBound instanceof ReferenceBinding) {
				if (this.superclass == null && aBound.isClass())
					this.superclass = (ReferenceBinding) aBound;
				else if (aBound.isInterface())
					numReferenceInterfaces++;
			} else if (TypeBinding.equalsEquals(aBound.leafComponentType(), this)) {
				return false; // cycle detected
			}
		}
		this.superInterfaces = new ReferenceBinding[numReferenceInterfaces];
		int idx = 0;
		for (int i = 0; i < upperBounds.length; i++) {
			TypeBinding aBound = upperBounds[i];
			if (aBound.isInterface())
				this.superInterfaces[idx++] = (ReferenceBinding) aBound;
		}
		if (this.superclass == null)
			this.superclass = javaLangObject;
		return true;
	}

	@Override
	public void initializeBounds(Scope scope, ParameterizedTypeBinding capturedParameterizedType) {
		// nothing to initialize here (and cannot use super methods which requires wildcard to be set).
	}

	@Override
	public TypeBinding clone(TypeBinding enclosingType) {
		return new CaptureBinding18(this);
	}

	@Override
	public MethodBinding[] getMethods(char[] selector) {
		if (this.upperBounds.length == 1 && this.upperBounds[0] instanceof ReferenceBinding)
			return ((ReferenceBinding)this.upperBounds[0]).getMethods(selector);
		return super.getMethods(selector);
	}

	@Override
	public TypeBinding erasure() {
		if (this.upperBounds != null && this.upperBounds.length > 1) {
			ReferenceBinding[] erasures = new ReferenceBinding[this.upperBounds.length];
			boolean multipleErasures = false;
			for (int i = 0; i < this.upperBounds.length; i++) {
				erasures[i] = (ReferenceBinding) this.upperBounds[i].erasure(); // FIXME cast?
				if (i > 0) {
					if (TypeBinding.notEquals(erasures[0], erasures[i]))
						multipleErasures = true;
				}
			}
			if (!multipleErasures)
				return erasures[0];
			return this.environment.createIntersectionType18(erasures);
		}
		return super.erasure();
	}

	/**
	 * @see TypeBinding#isEquivalentTo(TypeBinding)
	 */
	@Override
	public boolean isEquivalentTo(TypeBinding otherType) {
		// from CaptureBinding:
		if (equalsEquals(this, otherType)) return true;
		if (otherType == null) return false;
		if (this.upperBounds != null) {
			// from CaptureBinding:
			for (int i = 0; i < this.upperBounds.length; i++) {
				TypeBinding aBound = this.upperBounds[i];
				// capture of ? extends X[]
				if (aBound != null && aBound.isArrayType()) {
					if (!aBound.isCompatibleWith(otherType))
						return false;
				} else switch (otherType.kind()) {
					case Binding.WILDCARD_TYPE :
					case Binding.INTERSECTION_TYPE :
						if (!((WildcardBinding) otherType).boundCheck(aBound))
							return false;
				}
			}
			return true;
		}
		return false;
	}

	@Override
	public boolean isCompatibleWith(TypeBinding otherType, Scope captureScope) {
		if (TypeBinding.equalsEquals(this, otherType))
			return true;
		if (this.inRecursiveFunction)
			return true;
		this.inRecursiveFunction = true; 
		try {
			if (this.upperBounds != null) {
				int length = this.upperBounds.length;

				// need to compare two intersection types? (borrowed from IntersectionType18)
				int rightKind = otherType.kind();
				TypeBinding[] rightIntersectingTypes = null;
				if (rightKind == INTERSECTION_TYPE && otherType.boundKind() == Wildcard.EXTENDS) {
					TypeBinding allRightBounds = ((WildcardBinding) otherType).allBounds();
					if (allRightBounds instanceof IntersectionTypeBinding18)
						rightIntersectingTypes = ((IntersectionTypeBinding18) allRightBounds).intersectingTypes;
				} else if (rightKind == INTERSECTION_TYPE18) {
					rightIntersectingTypes = ((IntersectionTypeBinding18) otherType).intersectingTypes;
				}
				if (rightIntersectingTypes != null) {
					nextRequired:
					for (TypeBinding required : rightIntersectingTypes) {
						for (TypeBinding provided : this.upperBounds) {
							if (provided.isCompatibleWith(required, captureScope))
								continue nextRequired;
						}
						return false;
					}
					return true;
				}

				for (int i = 0; i < length; i++) {
					if (this.upperBounds[i].isCompatibleWith(otherType, captureScope))
						return true;
				}
			}
			return false;
		} finally {
			this.inRecursiveFunction = false;
		}
	}

	@Override
	public TypeBinding findSuperTypeOriginatingFrom(TypeBinding otherType) {
		if (this.upperBounds != null && this.upperBounds.length > 1) {
			for (int i = 0; i < this.upperBounds.length; i++) {
				TypeBinding candidate = this.upperBounds[i].findSuperTypeOriginatingFrom(otherType);
				if (candidate != null)
					return candidate;
				// TODO: maybe we should double check about multiple candidates here,
				// but upper bounds should be consistent so hopefully the first non-null candidate is good enough. 
			}
		}
		return super.findSuperTypeOriginatingFrom(otherType);
	}

	@Override
	TypeBinding substituteInferenceVariable(InferenceVariable var, TypeBinding substituteType) {
		if (this.inRecursiveFunction) return this;
		this.inRecursiveFunction = true;
		try {
			boolean haveSubstitution = false;
			ReferenceBinding currentSuperclass = this.superclass;
			if (currentSuperclass != null) {
				currentSuperclass = (ReferenceBinding) currentSuperclass.substituteInferenceVariable(var, substituteType);
				haveSubstitution |= TypeBinding.notEquals(currentSuperclass, this.superclass);
			}
			ReferenceBinding[] currentSuperInterfaces = null;
			if (this.superInterfaces != null) {
				int length = this.superInterfaces.length;
				if (haveSubstitution)
					System.arraycopy(this.superInterfaces, 0, currentSuperInterfaces=new ReferenceBinding[length], 0, length);
				for (int i = 0; i < length; i++) {
					ReferenceBinding currentSuperInterface = this.superInterfaces[i];
					if (currentSuperInterface != null) {
						currentSuperInterface = (ReferenceBinding) currentSuperInterface.substituteInferenceVariable(var, substituteType);
						if (TypeBinding.notEquals(currentSuperInterface, this.superInterfaces[i])) {
							if (currentSuperInterfaces == null)
								System.arraycopy(this.superInterfaces, 0, currentSuperInterfaces=new ReferenceBinding[length], 0, length);
							currentSuperInterfaces[i] = currentSuperInterface;
							haveSubstitution = true;
						}
					}
				}
			}
			TypeBinding[] currentUpperBounds = null;
			if (this.upperBounds != null) {
				int length = this.upperBounds.length;
				if (haveSubstitution)
					System.arraycopy(this.upperBounds, 0, currentUpperBounds=new TypeBinding[length], 0, length);
				for (int i = 0; i < length; i++) {
					TypeBinding currentBound = this.upperBounds[i];
					if (currentBound != null) {
						currentBound = currentBound.substituteInferenceVariable(var, substituteType);
						if (TypeBinding.notEquals(currentBound, this.upperBounds[i])) {
							if (currentUpperBounds == null)
								System.arraycopy(this.upperBounds, 0, currentUpperBounds=new TypeBinding[length], 0, length);
							currentUpperBounds[i] = currentBound;
							haveSubstitution = true;
						}
					}
				}
			}
			TypeBinding currentFirstBound = null;
			if (this.firstBound != null) {
				currentFirstBound = this.firstBound.substituteInferenceVariable(var, substituteType);
				haveSubstitution |= TypeBinding.notEquals(this.firstBound, currentFirstBound);
			}
			if (haveSubstitution) {
				final CaptureBinding18 newCapture = (CaptureBinding18) clone(enclosingType());
				newCapture.tagBits = this.tagBits;
				Substitution substitution = new Substitution() {
					@Override
					public TypeBinding substitute(TypeVariableBinding typeVariable) {
						return  (typeVariable == CaptureBinding18.this) ? newCapture : typeVariable; //$IDENTITY-COMPARISON$
					}
					@Override
					public boolean isRawSubstitution() {
						return false;
					}
					@Override
					public LookupEnvironment environment() {
						return CaptureBinding18.this.environment;
					}
				};
				if (currentFirstBound != null)
					newCapture.firstBound = Scope.substitute(substitution, currentFirstBound);
				newCapture.superclass = (ReferenceBinding) Scope.substitute(substitution, currentSuperclass);
				newCapture.superInterfaces = Scope.substitute(substitution, currentSuperInterfaces);
				newCapture.upperBounds = Scope.substitute(substitution, currentUpperBounds);
				return newCapture;
			}
			return this;
		} finally {
			this.inRecursiveFunction = false;
		}
	}

	@Override
	public boolean isProperType(boolean admitCapture18) {
		if (!admitCapture18) 
			return false;
		if (this.inRecursiveFunction)
			return true;
		this.inRecursiveFunction = true;
		try {
			if (this.lowerBound != null && !this.lowerBound.isProperType(admitCapture18))
				return false;
			if (this.upperBounds != null) {
				for (int i = 0; i < this.upperBounds.length; i++) {
					if (!this.upperBounds[i].isProperType(admitCapture18))
						return false;
				}
			}
		} finally {
			this.inRecursiveFunction = false;
		}
		return true;
	}

	int recursionLevel = 0; // used to give a hint at recursive types without going into infinity

	@Override
	public char[] readableName() {
		if (this.lowerBound == null && this.firstBound != null) {
			if (this.prototype.recursionLevel < 2) {
				try {
					this.prototype.recursionLevel ++;
					if (this.upperBounds != null && this.upperBounds.length > 1) {
						StringBuffer sb = new StringBuffer();
						sb.append(this.upperBounds[0].readableName());
						for (int i = 1; i < this.upperBounds.length; i++)
							sb.append('&').append(this.upperBounds[i].readableName());
						int len = sb.length();
						char[] name = new char[len];
						sb.getChars(0, len, name, 0);
						return name;
					}
					return this.firstBound.readableName();
				} finally {
					this.prototype.recursionLevel--;
				}
			} else {
				return this.originalName;
			}
		}
		return super.readableName();
	}

	@Override
	public char[] shortReadableName() {
		if (this.lowerBound == null && this.firstBound != null) {
			if (this.prototype.recursionLevel < 2) {
				try {
					this.prototype.recursionLevel++;
					if (this.upperBounds != null && this.upperBounds.length > 1) {
						StringBuffer sb = new StringBuffer();
						sb.append(this.upperBounds[0].shortReadableName());
						for (int i = 1; i < this.upperBounds.length; i++)
							sb.append('&').append(this.upperBounds[i].shortReadableName());
						int len = sb.length();
						char[] name = new char[len];
						sb.getChars(0, len, name, 0);
						return name;
					}
					return this.firstBound.shortReadableName();
				} finally {
					this.prototype.recursionLevel--;
				}
			} else {
				return this.originalName;
			}
		}
		return super.shortReadableName();
	}
	
	@Override
	public TypeBinding uncapture(Scope scope) {
		return this;
	}
	@Override
	public char[] computeUniqueKey(boolean isLeaf) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(TypeConstants.CAPTURE18);
		buffer.append('{').append(this.end).append('#').append(this.captureID).append('}');
		buffer.append(';');
		int length = buffer.length();
		char[] uniqueKey = new char[length];
		buffer.getChars(0, length, uniqueKey, 0);
		return uniqueKey;
	}
}

Back to the top