Java Code Examples for org.eclipse.xtext.xbase.typesystem.references.LightweightBoundTypeArgument#getDeclaredVariance()

The following examples show how to use org.eclipse.xtext.xbase.typesystem.references.LightweightBoundTypeArgument#getDeclaredVariance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResolvedTypes.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void addNonRecursiveHints(LightweightBoundTypeArgument original, List<LightweightBoundTypeArgument> hints, Set<Object> seenHandles,
		List<LightweightBoundTypeArgument> result) {
	for(LightweightBoundTypeArgument hint: hints) {
		LightweightTypeReference reference = hint.getTypeReference();
		if (reference instanceof UnboundTypeReference) {
			addNonRecursiveHints(original, (UnboundTypeReference)reference, seenHandles, result);
		} else {
			if (original.getDeclaredVariance() == VarianceInfo.IN && hint.getTypeReference() instanceof WildcardTypeReference) {
				LightweightTypeReference upperBound = hint.getTypeReference().getUpperBoundSubstitute();
				if (upperBound instanceof UnboundTypeReference) {
					addNonRecursiveHints(original, (UnboundTypeReference)upperBound, seenHandles, result);
				} else {
					LightweightBoundTypeArgument delegateHint = new LightweightBoundTypeArgument(
							upperBound, original.getSource(), hint.getOrigin(), hint.getDeclaredVariance(), original.getActualVariance());
					result.add(delegateHint);
				}
			} else {
				if (!result.isEmpty()) {
					if (original.getDeclaredVariance() == VarianceInfo.OUT && original.getActualVariance() == VarianceInfo.INVARIANT) {
						if (hint.getDeclaredVariance() == VarianceInfo.OUT && hint.getActualVariance() == VarianceInfo.INVARIANT) {
							continue;
						}
					}
					if (!result.contains(hint))
						result.add(hint);
				} else {
					result.add(hint);
				}
			}
		}
	}
}
 
Example 2
Source File: BoundTypeArgumentMerger.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected LightweightMergedBoundTypeArgument getSingleArgumentAsMergedArgument(LightweightBoundTypeArgument argument) {
	LightweightTypeReference typeReference = argument.getTypeReference();
	VarianceInfo varianceInfo = argument.getDeclaredVariance().mergeDeclaredWithActual(argument.getActualVariance());
	if (argument.getDeclaredVariance() == VarianceInfo.IN && varianceInfo == VarianceInfo.INVARIANT) {
		if (typeReference.getKind() == LightweightTypeReference.KIND_WILDCARD_TYPE_REFERENCE) {
			typeReference = typeReference.getInvariantBoundSubstitute();
		}
	}
	return new LightweightMergedBoundTypeArgument(typeReference, varianceInfo);
}
 
Example 3
Source File: StackedResolvedTypes.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void mergeTypeParametersIntoParent(ResolvedTypes parent) {
	for(UnboundTypeReference unbound: basicGetTypeParameters().values()) {
		LightweightTypeReference resolvedTo = unbound.getResolvedTo();
		if (resolvedTo == null) {
			List<JvmTypeParameter> typeParameters = basicGetDeclardTypeParameters();
			if (typeParameters != null && typeParameters.contains(unbound.getTypeParameter())) {
				unbound.tryResolve();
				if (!unbound.internalIsResolved()) {
					if (unbound.getExpression() instanceof XConstructorCall) {
						unbound.resolve(); // resolve against constraints 
					} else {
						unbound.acceptHint(unbound.getOwner().newParameterizedTypeReference(unbound.getTypeParameter()), 
								BoundTypeArgumentSource.RESOLVED, unbound, VarianceInfo.INVARIANT, VarianceInfo.INVARIANT);
					}
				}
			} else {
				LightweightTypeReference reference = unbound.copyInto(parent.getReferenceOwner());
				if (reference instanceof UnboundTypeReference) {
					parent.acceptUnboundTypeReference(unbound.getHandle(), (UnboundTypeReference) reference);
				}
			}
		}
	}
	Map<Object, List<LightweightBoundTypeArgument>> typeParameterHints = basicGetTypeParameterHints();
	for(Map.Entry<Object, List<LightweightBoundTypeArgument>> hint: typeParameterHints.entrySet()) {
		if (!parent.isResolved(hint.getKey())) {
			List<LightweightBoundTypeArgument> boundTypeArguments = hint.getValue();
			for(LightweightBoundTypeArgument boundTypeArgument: boundTypeArguments) {
				if (boundTypeArgument.getOrigin() instanceof VarianceInfo) {
					parent.acceptHint(hint.getKey(), boundTypeArgument);
				} else {
					LightweightBoundTypeArgument copy = new LightweightBoundTypeArgument(
							boundTypeArgument.getTypeReference().copyInto(parent.getReferenceOwner()), 
							boundTypeArgument.getSource(), boundTypeArgument.getOrigin(), 
							boundTypeArgument.getDeclaredVariance(), 
							boundTypeArgument.getActualVariance());
					parent.acceptHint(hint.getKey(), copy);
				}
			}
		}
	}
}
 
Example 4
Source File: ResolvedTypes.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public void acceptHint(Object handle, LightweightBoundTypeArgument boundTypeArgument) {
	if (boundTypeArgument.getSource() == BoundTypeArgumentSource.RESOLVED) {
		if (resolvedTypeParameters == null) {
			resolvedTypeParameters = new SharedKeysAwareSet<Object>(shared.allResolvedTypeParameters);
		}
		if (resolvedTypeParameters.add(handle)) {
			if (boundTypeArgument.getDeclaredVariance().mergeDeclaredWithActual(boundTypeArgument.getActualVariance()) == VarianceInfo.INVARIANT) {
				resolveDependentTypeArguments(handle, boundTypeArgument);
			}
			LightweightBoundTypeArgument boundWithoutRecursion = removeRecursiveTypeArguments(handle, boundTypeArgument);
			ensureTypeParameterHintsMapExists().put(handle, Collections.singletonList(boundWithoutRecursion));
		}
	} else {
		if (!isResolved(handle)) {
			if (boundTypeArgument.getTypeReference() instanceof UnboundTypeReference && boundTypeArgument.getSource() != BoundTypeArgumentSource.CONSTRAINT) {
				UnboundTypeReference other = (UnboundTypeReference) boundTypeArgument.getTypeReference();
				Object otherHandle = other.getHandle();
				if (ensureTypeParameterHintsMapExists().containsKey(handle)) {
					// don't add fully redundant hints
					List<LightweightBoundTypeArgument> existingValues = ensureTypeParameterHintsMapExists().get(handle);
					for(LightweightBoundTypeArgument existingValue: existingValues) {
						if (existingValue.getTypeReference() instanceof UnboundTypeReference) {
							if (((UnboundTypeReference) existingValue.getTypeReference()).getHandle() == otherHandle) {
								if (existingValue.getActualVariance() == boundTypeArgument.getActualVariance() 
										&& existingValue.getDeclaredVariance() == boundTypeArgument.getDeclaredVariance()
										&& existingValue.getSource() == boundTypeArgument.getSource()) {
									return;
								}
							}
						}
					}
				}
				UnboundTypeReference currentUnbound = getUnboundTypeReference(handle);
				Maps2.putIntoListMap(otherHandle, copyBoundTypeArgument(currentUnbound, boundTypeArgument), ensureTypeParameterHintsMapExists());
			}
			Maps2.putIntoListMap(handle, boundTypeArgument, ensureTypeParameterHintsMapExists());
		} else {
			throw new IllegalStateException("Cannot add hints if the reference was already resolved");
		}
	}
}
 
Example 5
Source File: ResolvedTypes.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected LightweightBoundTypeArgument copyBoundTypeArgument(LightweightTypeReference typeReference,
		LightweightBoundTypeArgument boundTypeArgument) {
	return new LightweightBoundTypeArgument(typeReference, boundTypeArgument.getSource(),
			boundTypeArgument.getOrigin(), boundTypeArgument.getDeclaredVariance(),
			boundTypeArgument.getActualVariance());
}
 
Example 6
Source File: ExpectationTypeParameterHintCollector.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void doVisitUnboundTypeReference(UnboundTypeReference reference,
		ParameterizedTypeReference declaration) {
	boolean constraintSeen = false;
	boolean constraintsMatch = true;
	boolean othersSeen = false;
	boolean declarationMatches = getExpectedVariance() != VarianceInfo.OUT;
	if (reference.getTypeParameter() != declaration.getType()) {
		List<LightweightBoundTypeArgument> hints = reference.getAllHints();
		for(int i = 0; i < hints.size(); i++) {
			LightweightBoundTypeArgument hint = hints.get(i);
			if (hint.getSource() == BoundTypeArgumentSource.CONSTRAINT) {
				constraintSeen = true;
				outerVisit(hint.getTypeReference(), declaration, hint.getSource(), hint.getDeclaredVariance(), hint.getActualVariance());
				if (constraintsMatch && !hint.getTypeReference().isAssignableFrom(declaration)) {
					constraintsMatch = false;
				}
			} else {
				othersSeen = true;
				// we don't break the list traversal here since we want to do the paired outerVisit for all constraints
				if (declarationMatches) {
					if (hint.getActualVariance() == VarianceInfo.OUT && hint.getDeclaredVariance() == VarianceInfo.OUT && 
							(hint.getSource() == BoundTypeArgumentSource.INFERRED || hint.getSource() == BoundTypeArgumentSource.INFERRED_EXPECTATION || hint.getSource() == BoundTypeArgumentSource.INFERRED_LATER)) {
						if (!declaration.isAssignableFrom(hint.getTypeReference())) {
							declarationMatches = false;
						}
					} else {
						declarationMatches = false;
					}
				}
			}
		}
	} else {
		if (getOwner().getDeclaredTypeParameters().contains(reference.getTypeParameter())) {
			reference.acceptHint(declaration, BoundTypeArgumentSource.RESOLVED, this, VarianceInfo.INVARIANT, VarianceInfo.INVARIANT);
			return;
		}
	}
	if (constraintSeen && constraintsMatch && !othersSeen) {
		reference.acceptHint(declaration, BoundTypeArgumentSource.RESOLVED, this, VarianceInfo.INVARIANT, VarianceInfo.INVARIANT);
	} else if (!constraintSeen && !reference.internalIsResolved() && declaration.isResolved() && !getOwner().isResolved(reference.getHandle()) && reference.canResolveTo(declaration)) {
		reference.acceptHint(declaration, BoundTypeArgumentSource.RESOLVED, this, VarianceInfo.INVARIANT, VarianceInfo.INVARIANT);
	} else if (othersSeen && declarationMatches) {
		reference.acceptHint(declaration, BoundTypeArgumentSource.INFERRED, this, VarianceInfo.INVARIANT, VarianceInfo.INVARIANT);
	} else {
		reference.tryResolve();
		if (reference.internalIsResolved()) {
			outerVisit(reference, declaration);
		} else {
			addHint(reference, declaration);
		}
	}
}
 
Example 7
Source File: ExpectationTypeParameterHintCollector.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void doVisitUnboundTypeReference(UnboundTypeReference reference, ArrayTypeReference declaration) {
	boolean constraintSeen = false;
	boolean constraintsMatch = true;
	boolean othersSeen = false;
	boolean declarationMatches = getExpectedVariance() != VarianceInfo.OUT;
	List<LightweightBoundTypeArgument> hints = reference.getAllHints();
	for(int i = 0; i < hints.size(); i++) {
		LightweightBoundTypeArgument hint = hints.get(i);
		if (hint.getSource() == BoundTypeArgumentSource.CONSTRAINT) {
			constraintSeen = true;
			outerVisit(hint.getTypeReference(), declaration, hint.getSource(), hint.getDeclaredVariance(), hint.getActualVariance());
			if (constraintsMatch && !hint.getTypeReference().isAssignableFrom(declaration)) {
				constraintsMatch = false;
			}
		} else {
			othersSeen = true;
			// we don't break the list traversal here since we want to do the paired outerVisit for all constraints
			if (declarationMatches) {
				if (hint.getActualVariance() == VarianceInfo.OUT && hint.getDeclaredVariance() == VarianceInfo.OUT && 
						(hint.getSource() == BoundTypeArgumentSource.INFERRED || hint.getSource() == BoundTypeArgumentSource.INFERRED_LATER || hint.getSource() == BoundTypeArgumentSource.INFERRED_EXPECTATION)) {
					if (!declaration.isAssignableFrom(hint.getTypeReference())) {
						declarationMatches = false;
					}
				} else {
					declarationMatches = false;
				}
			}
		}
	}
	if (constraintSeen && constraintsMatch && !othersSeen) {
		reference.acceptHint(declaration, BoundTypeArgumentSource.RESOLVED, this, VarianceInfo.INVARIANT, VarianceInfo.INVARIANT);
	} else if (!constraintSeen && !reference.internalIsResolved() && declaration.isResolved() && !getOwner().isResolved(reference.getHandle()) && reference.canResolveTo(declaration)) {
		reference.acceptHint(declaration, BoundTypeArgumentSource.RESOLVED, this, VarianceInfo.INVARIANT, VarianceInfo.INVARIANT);
	} else if (othersSeen && declarationMatches) {
		reference.acceptHint(declaration, BoundTypeArgumentSource.INFERRED, this, VarianceInfo.INVARIANT, VarianceInfo.INVARIANT);
	} else {
		reference.tryResolve();
		if (reference.internalIsResolved()) {
			outerVisit(reference, declaration);
		} else {
			addHint(reference, declaration);
		}
	}
}