Java Code Examples for org.eclipse.xtext.xbase.typesystem.references.LightweightMergedBoundTypeArgument#getTypeReference()

The following examples show how to use org.eclipse.xtext.xbase.typesystem.references.LightweightMergedBoundTypeArgument#getTypeReference() . 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: RawTypeConformanceComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected int isConformantMergeResult(LightweightMergedBoundTypeArgument mergeResult, LightweightTypeReference right,
		int flags) {
	LightweightTypeReference mergeResultReference = mergeResult.getTypeReference();
	if (right.isWildcard() && mergeResultReference.isWildcard()) {
		if (right.getLowerBoundSubstitute().isAny()) {
			LightweightTypeReference lowerBoundMergeResult = mergeResultReference.getLowerBoundSubstitute();
			if (!lowerBoundMergeResult.isAny()) {
				mergeResultReference = lowerBoundMergeResult;
			}
		} else {
			flags = flags | AS_TYPE_ARGUMENT;
		}
	} else if (mergeResultReference.isWildcard()) {
		flags = flags | AS_TYPE_ARGUMENT;
	}
	return isConformant(mergeResultReference, right, flags);
}
 
Example 2
Source File: TypeParameterByConstraintSubstitutor.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
/* @Nullable */
protected LightweightTypeReference getUnmappedSubstitute(ParameterizedTypeReference reference, JvmTypeParameter type, ConstraintVisitingInfo visiting) {
	if (!ignoreDeclaredTypeParameters) {
		if (isDeclaredTypeParameter(type)) {
			return reference;
		}
	}
	ConstraintAwareTypeArgumentCollector collector = new ConstraintAwareTypeArgumentCollector(getOwner());
	LightweightTraversalData data = new LightweightTraversalData();
	data.getTypeParameterMapping().putAll(getTypeParameterMapping());
	reference.accept(collector, data);
	LightweightMergedBoundTypeArgument boundTypeArgument = data.getTypeParameterMapping().get(type);
	if (boundTypeArgument != null && boundTypeArgument.getTypeReference() != reference) {
		return boundTypeArgument.getTypeReference().accept(this, visiting);
	}
	if (boundTypeArgument != null)
		return boundTypeArgument.getTypeReference();
	return null;
}
 
Example 3
Source File: UnboundTypeParameterPreservingSubstitutor.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
/* @Nullable */
protected LightweightTypeReference getBoundTypeArgument(ParameterizedTypeReference reference, JvmTypeParameter type, Set<JvmTypeParameter> visiting) {
	LightweightMergedBoundTypeArgument boundTypeArgument = getTypeParameterMapping().get(type);
	if (boundTypeArgument != null) {
		LightweightTypeReference boundReference = boundTypeArgument.getTypeReference();
		if (boundReference != null && reference != boundReference) {
			if (boundReference instanceof UnboundTypeReference)
				return boundReference.copyInto(getOwner());
			JvmType boundType = boundReference.getType();
			if (boundType != type) {
				if (visiting.add(type)) {
					try {
						LightweightTypeReference result = boundReference.accept(this, visiting);
						return result;
					} finally {
						visiting.remove(type);
					}
				} else {
					return reference;
				}
			} 
		}
	}
	return null;
}
 
Example 4
Source File: BoundTypeArgumentMerger.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public boolean isPossibleMergeResult(List<LightweightBoundTypeArgument> allArguments, LightweightTypeReference candidate) {
	if (allArguments.isEmpty())
		return false;
	
	if (allArguments.size() == 1 && !candidate.isWildcard()) {
		LightweightBoundTypeArgument singleArgument = allArguments.get(0);
		if (VarianceInfo.OUT.equals(singleArgument.getActualVariance()) && singleArgument.getActualVariance().equals(singleArgument.getDeclaredVariance())) {
			LightweightTypeReference singleReference = singleArgument.getTypeReference();
			if (singleReference.isResolved())
				return candidate.isAssignableFrom(singleReference, TypeConformanceComputationArgument.DEFAULT);
		}
	}
	LightweightMergedBoundTypeArgument merged = merge(allArguments, candidate.getOwner());
	if (merged == null)
		return false;
	VarianceInfo variance = merged.getVariance();
	LightweightTypeReference type = merged.getTypeReference();
	if (variance == null || type == null) {
		return false;
	}
	switch(variance) {
		case INVARIANT: {
			int result = candidate.internalIsAssignableFrom(type, new TypeConformanceComputationArgument(false, true, true, true, false, false));
			if ((result & ConformanceFlags.SUCCESS) != 0 && (result & ConformanceFlags.RAW_TYPE_CONVERSION) == 0) {
				return true;
			}
			return false;
		}
		case OUT: return type.isAssignableFrom(candidate, TypeConformanceComputationArgument.DEFAULT);
		case IN: return candidate.isAssignableFrom(type, TypeConformanceComputationArgument.DEFAULT);
		default: throw new IllegalStateException("Unknown variance info: " + variance);
	}
}
 
Example 5
Source File: TypeParameterSubstitutor.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected LightweightTypeReference getBoundTypeArgument(ParameterizedTypeReference reference, JvmTypeParameter type,
		Visiting visiting) {
	LightweightMergedBoundTypeArgument boundTypeArgument = typeParameterMapping.get(type);
	if (boundTypeArgument != null) {
		LightweightTypeReference boundReference = boundTypeArgument.getTypeReference();
		if (boundReference != null && reference != boundReference && boundReference.getType() != type) {
			return boundReference.accept(this, visiting);
		}
	}
	return null;
}
 
Example 6
Source File: AbstractTypeReferencePairWalker.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void doVisitMatchingTypeParameters(ParameterizedTypeReference reference,
		ParameterizedTypeReference declaration) {
	Map<JvmTypeParameter, LightweightMergedBoundTypeArgument> actualMapping = getTypeParameterMapping(reference);
	TypeParameterSubstitutor<?> actualSubstitutor = createTypeParameterSubstitutor(actualMapping);
	Map<JvmTypeParameter, LightweightMergedBoundTypeArgument> declaredMapping = getTypeParameterMapping(declaration);
	TypeParameterSubstitutor<?> declaredSubstitutor = createTypeParameterSubstitutor(declaredMapping);
	Set<JvmTypeParameter> actualBoundParameters = actualMapping.keySet();
	Set<JvmTypeParameter> visited = Sets.newHashSet();
	outer: for (JvmTypeParameter actualBoundParameter : actualBoundParameters) {
		if (visited.add(actualBoundParameter)) {
			LightweightMergedBoundTypeArgument declaredBoundArgument = declaredMapping.get(actualBoundParameter);
			while(declaredBoundArgument == null && actualBoundParameter != null) {
				actualBoundParameter = findMappedParameter(actualBoundParameter, actualMapping, visited);
				if (actualBoundParameter == null)
					continue outer;
				declaredBoundArgument = declaredMapping.get(actualBoundParameter);
			}
			if (declaredBoundArgument != null) {
				LightweightTypeReference declaredTypeReference = declaredBoundArgument.getTypeReference();
				JvmType declaredType = declaredTypeReference.getType();
				if (declaredType instanceof JvmTypeParameter) {
					JvmTypeParameter declaredTypeParameter = (JvmTypeParameter) declaredType;
					if (!shouldProcessInContextOf(declaredTypeParameter, actualBoundParameters, visited))
						continue;
					declaredTypeReference = declaredSubstitutor.substitute(declaredTypeReference);
				}
				LightweightTypeReference actual = actualSubstitutor.substitute(actualMapping.get(actualBoundParameter).getTypeReference());
				if (!actual.isResolved() || !declaredTypeReference.isResolved() || !Strings.equal(actual.getIdentifier(), declaredTypeReference.getIdentifier())) {
					if (reference.getType() != actual.getType() 
							|| declaredTypeReference.getType() != declaration.getType() 
							|| !reference.getIdentifier().equals(actual.getIdentifier())
							|| !declaredTypeReference.getIdentifier().equals(declaration.getIdentifier())) {
						outerVisit(declaredTypeReference, actual, declaration, VarianceInfo.INVARIANT, VarianceInfo.INVARIANT);
					}
				}
			}
		}
	}
}
 
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: RawTypeConformanceComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected int isConformantToConstraints(
		final UnboundTypeReference left, 
		final LightweightTypeReference right, 
		List<LightweightBoundTypeArgument> leftHints, 
		int flags) {
	int result = flags;
	for(LightweightBoundTypeArgument leftHint: leftHints) {
		if (leftHint.getSource() == BoundTypeArgumentSource.CONSTRAINT) {
			final LightweightTypeReference leftHintReference = leftHint.getTypeReference();
			if (!leftHintReference.getUniqueIdentifier().equals(right.getUniqueIdentifier())) {
				final LightweightMergedBoundTypeArgument rightTypeArgument = new LightweightMergedBoundTypeArgument(right.getWrapperTypeIfPrimitive(), VarianceInfo.INVARIANT);
				final UnboundTypeParameterPreservingSubstitutor unboundSubstitutor = new UnboundTypeParameterPreservingSubstitutor(
						Collections.singletonMap(left.getTypeParameter(), rightTypeArgument), right.getOwner()) {
					@Override
					public LightweightTypeReference doVisitUnboundTypeReference(UnboundTypeReference reference, Set<JvmTypeParameter> visiting) {
						if (reference.getHandle() == left.getHandle()) {
							if (right.getKind() == KIND_UNBOUND_TYPE_REFERENCE) {
								UnboundTypeReference rightUnbound = (UnboundTypeReference) right;
								List<LightweightBoundTypeArgument> rightHints = rightUnbound.getAllHints();
								for(LightweightBoundTypeArgument rightHint: rightHints) {
									LightweightTypeReference rightHintReference = rightHint.getTypeReference();
									if (rightHintReference != null && leftHintReference.getUniqueIdentifier().equals(rightHintReference.getUniqueIdentifier())) {
										return super.doVisitUnboundTypeReference(reference, visiting);
									}
								}
							}
							return rightTypeArgument.getTypeReference();
						}
						return super.doVisitUnboundTypeReference(reference, visiting);
					}
				};
				LightweightTypeReference constraintReference = unboundSubstitutor.substitute(leftHintReference);
				int constraintResult = doIsConformant(constraintReference, right, flags);
				if ((constraintResult & SUCCESS) == 0) {
					return flags;
				}
				result |= constraintResult;
			}
		}
	}
	return result | SUCCESS;
}