Java Code Examples for org.eclipse.xtext.xbase.typesystem.references.UnboundTypeReference#acceptHint()

The following examples show how to use org.eclipse.xtext.xbase.typesystem.references.UnboundTypeReference#acceptHint() . 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: AbstractClosureTypeHelper.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void deferredBindTypeArgument(/* @Nullable */ LightweightTypeReference declared, LightweightTypeReference actual, final BoundTypeArgumentSource source) {
	if (declared != null) { 
		ExpectationTypeParameterHintCollector collector = new ResolvingTypeParameterHintCollector(expectation.getReferenceOwner(), source) {
			@Override
			protected void addHint(UnboundTypeReference typeParameter, LightweightTypeReference reference) {
				LightweightTypeReference wrapped = reference.getWrapperTypeIfPrimitive();
				if (source == BoundTypeArgumentSource.INFERRED_CONSTRAINT) {
					wrapped = getStricterConstraint(typeParameter, wrapped);
				}
				if (wrapped != null) {
					typeParameter.acceptHint(wrapped, source, getOrigin(), getExpectedVariance(), getActualVariance());	
				}
			}
		};
		collector.processPairedReferences(declared, actual);
	}
}
 
Example 2
Source File: RawTypeConformanceComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected int doIsConformant(LightweightTypeReference left, UnboundTypeReference right, int flags) {
	if (left.getType() == right.getType() || left.isType(Object.class)) {
		return flags | SUCCESS;
	}
	if ((flags & ALLOW_UNBOXING) == 0 && left.isPrimitive()) {
		return flags;
	}
	boolean doesNotHaveSignificantHints = false;
	if (((flags & RAW_TYPE) == 0) && (right.canResolveTo(left) || (flags & AS_TYPE_ARGUMENT) != 0 && (doesNotHaveSignificantHints = !right.hasSignificantHints()))) {
		if ((flags & UNBOUND_COMPUTATION_ADDS_HINTS) != 0 && doesNotHaveSignificantHints) {
			right.acceptHint(left, BoundTypeArgumentSource.INFERRED_LATER, left, VarianceInfo.INVARIANT, VarianceInfo.INVARIANT);
		}
		return flags | SUCCESS;
	}
	right.tryResolve(false);
	LightweightTypeReference resolvedTo = right.getResolvedTo();
	if (resolvedTo != null) {
		return doIsConformant(left, resolvedTo, flags);
	}
	return flags;
}
 
Example 3
Source File: RawTypeConformanceComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected int doIsConformant(UnboundTypeReference left, UnboundTypeReference right, int flags) {
	if (left.getHandle().equals(right.getHandle())) {
		return flags | SUCCESS;
	}
	List<LightweightBoundTypeArgument> leftHints = left.getAllHints();
	List<LightweightBoundTypeArgument> rightHints = right.getAllHints();
	if ((flags & UNBOUND_COMPUTATION_ADDS_HINTS) != 0) {
		if (leftHints.isEmpty() || rightHints.isEmpty() || !left.hasSignificantHints(leftHints) || !right.hasSignificantHints()) {
			left.acceptHint(right, BoundTypeArgumentSource.INFERRED, this, VarianceInfo.OUT, VarianceInfo.OUT);
			return flags | SUCCESS;
		}
	}
	if (leftHints.equals(rightHints)) {
		return flags | SUCCESS;
	}
	return tryResolveAndCheckConformance(left, right, flags);
}
 
Example 4
Source File: RawTypeConformanceComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected int addHintAndAnnounceSuccess(UnboundTypeReference left, LightweightTypeReference hint,
		int flags) {
	if (hint instanceof WildcardTypeReference) {
		List<LightweightTypeReference> bounds = ((WildcardTypeReference) hint).getUpperBounds();
		for(LightweightTypeReference upperBound: bounds)
			left.acceptHint(upperBound, BoundTypeArgumentSource.INFERRED, this, VarianceInfo.OUT, VarianceInfo.OUT);
	} else {
		left.acceptHint(hint, BoundTypeArgumentSource.INFERRED, this, VarianceInfo.OUT, VarianceInfo.OUT);
	}
	return flags | SUCCESS;
}
 
Example 5
Source File: AbstractLinkingCandidate.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void initializeConstraintMapping(JvmTypeParameter typeParameter, UnboundTypeParameterPreservingSubstitutor substitutor, UnboundTypeReference typeReference) {
	if (!typeReference.internalIsResolved()) {
		List<JvmTypeConstraint> constraints = typeParameter.getConstraints();
		for(JvmTypeConstraint constraint: constraints) {
			JvmTypeReference constraintReference = constraint.getTypeReference();
			if (constraintReference != null) {
				LightweightTypeReference substitute = substitutor.substitute(constraintReference);
				if (!substitute.isType(Object.class) && !substitute.isPrimitiveVoid()) {
					typeReference.acceptHint(substitute, BoundTypeArgumentSource.CONSTRAINT, constraint, VarianceInfo.OUT, VarianceInfo.OUT);
				}
			}
		}
	}
}
 
Example 6
Source File: ResolvedTypes.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public void reassignType(JvmIdentifiableElement identifiable, /* @Nullable */ LightweightTypeReference reference) {
	if (reference != null) {
		LightweightTypeReference actualType = getActualType(identifiable);
		if (actualType != null) {
			if (actualType.getKind() == LightweightTypeReference.KIND_UNBOUND_TYPE_REFERENCE && !((UnboundTypeReference) actualType).internalIsResolved()) {
				UnboundTypeReference casted = (UnboundTypeReference) actualType;
				List<LightweightBoundTypeArgument> hints = getHints(casted.getHandle());
				boolean canAddHint = true;
				for(int i = 0; i < hints.size() && canAddHint; i++) {
					LightweightBoundTypeArgument hint = hints.get(i);
					if (hint.getTypeReference() == null || (hint.getSource() != BoundTypeArgumentSource.EXPECTATION && hint.getSource() != BoundTypeArgumentSource.INFERRED_CONSTRAINT)) {
						canAddHint = false;
					}
				}
				if (canAddHint) {
					casted.acceptHint(reference, BoundTypeArgumentSource.EXPECTATION, identifiable, VarianceInfo.OUT, VarianceInfo.OUT);
				}
				ensureReassignedTypesMapExists().put(identifiable, reference);
			} else if (!reference.isAssignableFrom(actualType)) {
				if (actualType.isAssignableFrom(reference)) {
					ensureReassignedTypesMapExists().put(identifiable, reference);
				} else {
					CompoundTypeReference multiType = toMultiType(actualType, reference);
					ensureReassignedTypesMapExists().put(identifiable, multiType);					
				}
			}
		} else {
			ensureReassignedTypesMapExists().put(identifiable, reference);
		}
	} else {
		if (reassignedTypes != null)
			reassignedTypes.remove(identifiable);
	}
}
 
Example 7
Source File: TypeArgumentFromComputedTypeCollector.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public static void resolveAgainstActualType(final LightweightTypeReference declaredType, LightweightTypeReference actualType,
		Collection<JvmTypeParameter> typeParameters, Map<JvmTypeParameter, LightweightMergedBoundTypeArgument> typeParameterMapping,
		BoundTypeArgumentSource source,
		ITypeReferenceOwner owner) {
	if (declaredType.isRawType() || actualType.isRawType())
		return;
	TypeArgumentFromComputedTypeCollector implementation = new TypeArgumentFromComputedTypeCollector(typeParameters, source, owner);
	implementation.populateTypeParameterMapping(declaredType, actualType);
	Map<JvmTypeParameter, List<LightweightBoundTypeArgument>> parameterMapping = implementation.rawGetTypeParameterMapping();
	for(Map.Entry<JvmTypeParameter, List<LightweightBoundTypeArgument>> entry: parameterMapping.entrySet()) {
		LightweightMergedBoundTypeArgument boundTypeArgument = typeParameterMapping.get(entry.getKey());
		if (boundTypeArgument != null ) {
			List<LightweightBoundTypeArgument> computedBoundTypeArguments = entry.getValue();
			for(LightweightBoundTypeArgument computedBoundTypeArgument: computedBoundTypeArguments) { 
				if (computedBoundTypeArgument.getSource() == BoundTypeArgumentSource.RESOLVED) {
					VarianceInfo varianceInfo = computedBoundTypeArgument.getDeclaredVariance().mergeDeclaredWithActual(computedBoundTypeArgument.getActualVariance());
					typeParameterMapping.put(entry.getKey(), new LightweightMergedBoundTypeArgument(computedBoundTypeArgument.getTypeReference(), varianceInfo));
				} else if (boundTypeArgument.getTypeReference() instanceof UnboundTypeReference) {
					UnboundTypeReference typeReference = (UnboundTypeReference) boundTypeArgument.getTypeReference();
					if (!typeReference.internalIsResolved()) {
						if (!(computedBoundTypeArgument.getTypeReference() instanceof UnboundTypeReference) || 
								((UnboundTypeReference) computedBoundTypeArgument.getTypeReference()).getHandle() != typeReference.getHandle())
							typeReference.acceptHint(computedBoundTypeArgument);
					}
				}
			}
		}
	}
}
 
Example 8
Source File: TypeArgumentFromComputedTypeCollector.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void acceptHint(UnboundTypeReference reference, LightweightTypeReference param) {
	if (!shouldProcess(reference.getTypeParameter())) {
		reference.tryResolve();
		if (reference.internalIsResolved()) {
			outerVisit(reference, param);
		} else {
			super.acceptHint(reference, param);
		}
	} else {
		reference.acceptHint(boundByInference(param));
	}
}
 
Example 9
Source File: TypeArgumentFromComputedTypeCollector.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected UnboundTypeReferenceTraverser createUnboundTypeReferenceTraverser() {
	return new UnboundTypeParameterAwareUnboundTypeReferenceTraverser() {
		@Override
		protected void doVisitWildcardTypeReference(WildcardTypeReference reference,
				UnboundTypeReference declaration) {
			if (shouldProcess(declaration.getTypeParameter())) {
				declaration.acceptHint(boundByInference(reference));
			} else {
				super.doVisitWildcardTypeReference(reference, declaration);
			}
		}
	};
}
 
Example 10
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 11
Source File: UnboundTypeParameterAwareTypeArgumentCollector.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void acceptHint(UnboundTypeReference reference, LightweightTypeReference param) {
	reference.acceptHint(boundByDefaultSource(param));
}
 
Example 12
Source File: DeferredTypeParameterHintCollector.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void addHint(UnboundTypeReference typeParameter, LightweightTypeReference reference) {
	LightweightTypeReference wrapped = getStricterConstraint(typeParameter, reference.getWrapperTypeIfPrimitive());
	if (wrapped != null) {
		typeParameter.acceptHint(wrapped, getTypeArgumentSource(), getOrigin(), getExpectedVariance(), getActualVariance());	
	}
}
 
Example 13
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 14
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);
		}
	}
}