Java Code Examples for org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference#isResolved()

The following examples show how to use org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference#isResolved() . 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: CollectionLiteralsTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean isExpectedType(LightweightTypeReference expectation, Class<?> clazz) {
	if (expectation != null) {
		if (expectation.isResolved() && isSubtypeButNotSynonym(expectation, clazz)) {
			return true;
		}
		if (expectation instanceof UnboundTypeReference) {
			if (expectation.getOwner().newParameterizedTypeReference(((UnboundTypeReference) expectation).getTypeParameter()).isSubtypeOf(clazz)) {
				return true;
			}
			List<LightweightBoundTypeArgument> hints = ((UnboundTypeReference) expectation).getAllHints();
			for(LightweightBoundTypeArgument hint: hints) {
				LightweightTypeReference hintReference = hint.getTypeReference();
				if (hintReference != null && isSubtypeButNotSynonym(hintReference, clazz)) {
					return true;
				}
			}
		} else if (expectation instanceof ParameterizedTypeReference) {
			return isSubtypeButNotSynonym(expectation, clazz);
		}
	}
	return false;
}
 
Example 2
Source File: ResolvedTypes.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
private LightweightTypeReference refineMergedType(MergeData mergeData, LightweightTypeReference mergedType, boolean returnType, boolean useExpectation) {
	if (useExpectation && mergeData.expectation != null && (returnType || !mergeData.allNoImplicitReturn)) {
		LightweightTypeReference expectedType = mergeData.expectation.getExpectedType();
		if (expectedType != null && expectedType.isResolved()) {
			if (!expectedType.isAssignableFrom(mergedType)) {
				boolean valid = true;
				for (LightweightTypeReference mergedReference: mergeData.references) {
					if (!expectedType.isAssignableFrom(mergedReference)) {
						valid = false;
						break;
					}
				}
				if (valid) {
					mergedType = expectedType; // branches have already been validated
					mergeData.mergedFlags &= ~(ConformanceFlags.UNCHECKED | ConformanceFlags.INCOMPATIBLE | ConformanceFlags.LAMBDA_RAW_COMPATIBLE | ConformanceFlags.LAMBDA_PARAMETER_COMPATIBLE);
					mergeData.mergedFlags |= ConformanceFlags.CHECKED_SUCCESS;
				}
			}
		}
	}
	if (mergeData.voidSeen && returnType && !mergeData.references.isEmpty()) {
		mergedType = mergedType.getWrapperTypeIfPrimitive();
	}
	return mergedType;
}
 
Example 3
Source File: ExpectationTypeParameterHintCollector.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void doVisitUnboundTypeReference(UnboundTypeReference reference,
		WildcardTypeReference declaration) {
	if (declaration.getLowerBound() == null) {
		if (!reference.internalIsResolved()) {
			List<LightweightTypeReference> upperBounds = declaration.getUpperBounds();
			for(LightweightTypeReference upperBound: upperBounds) {
				if (!upperBound.isResolved() || !reference.canResolveTo(upperBound)) {
					super.doVisitUnboundTypeReference(reference, declaration);
					return;
				}
			}
			reference.tryResolve();
			if (reference.internalIsResolved()) {
				outerVisit(reference, declaration);
			} else {
				addHint(reference, declaration);
			}
			return;
		}
	}
	super.doVisitUnboundTypeReference(reference, declaration);
}
 
Example 4
Source File: CollectionLiteralsTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * If the expected type is a subtype of {@link Map}, the resolved super type is returned.
 * This allows to query for the type arguments that are available on the expectation.
 */
protected LightweightTypeReference getMapExpectation(LightweightTypeReference expectation) {
	if (expectation != null && expectation.isResolved()) {
		LightweightTypeReference result = expectation.getSuperType(Map.class);
		if (result != null && result.getTypeArguments().size() == 2) {
			return result;
		}
	}
	return null;
}
 
Example 5
Source File: TypeConformanceComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void addIdentifier(LightweightTypeReference type, Set<String> allNames, Set<String> allBoundNames) {
	if (type instanceof UnboundTypeReference && !type.isResolved()) {
		allNames.add(((UnboundTypeReference) type).getHandle().toString());
	} else {
		String identifier = type.getJavaIdentifier();
		allNames.add(identifier);
		allBoundNames.add(identifier);
	}
}
 
Example 6
Source File: AbstractPendingLinkingCandidate.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected CandidateCompareResult compareExpectedArgumentTypes(AbstractPendingLinkingCandidate<?> right) {
	int result = 0;
	int upTo = Math.max(arguments.getArgumentCount(), right.arguments.getArgumentCount());
	for(int leftIdx = hasReceiver() ? 0 : -1, rightIdx = right.hasReceiver() ? 0 : -1; leftIdx < upTo && rightIdx < upTo; leftIdx++, rightIdx++) {
		LightweightTypeReference expectedArgumentType = getSubstitutedExpectedType(leftIdx);
		LightweightTypeReference rightExpectedArgumentType = right.getSubstitutedExpectedType(rightIdx);
		if (expectedArgumentType == null) {
			if (rightExpectedArgumentType != null)
				return CandidateCompareResult.OTHER;
		} else {
			if (rightExpectedArgumentType == null) {
				return CandidateCompareResult.THIS;
			}
			boolean leftResolved = expectedArgumentType.isResolved();
			if (!leftResolved) {
				expectedArgumentType = expectedArgumentType.getRawTypeReference();
			}
			boolean rightResolved = rightExpectedArgumentType.isResolved();
			if (!rightResolved) {
				rightExpectedArgumentType = rightExpectedArgumentType.getRawTypeReference();
			}
			result += compareDeclaredTypes(expectedArgumentType, rightExpectedArgumentType, leftResolved, rightResolved);
		}
	}
	if (result == 0) {
		if (!getDeclaredTypeParameters().isEmpty() || !right.getDeclaredTypeParameters().isEmpty()) {
			return compareDeclaredParameterTypes(right);
		}
		return CandidateCompareResult.AMBIGUOUS;
	} else if (result < 0) {
		return CandidateCompareResult.THIS;
	} else {
		return getExpectedTypeCompareResultOther(right);
	}
}
 
Example 7
Source File: AbstractPendingLinkingCandidate.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private CandidateCompareResult compareDeclaredParameterTypes(AbstractPendingLinkingCandidate<?> right) {
	if(getFeature() instanceof JvmExecutable && right.getFeature() instanceof JvmExecutable) {
		List<JvmFormalParameter> parameters = ((JvmExecutable) getFeature()).getParameters();
		List<JvmFormalParameter> rightParameters = ((JvmExecutable) right.getFeature()).getParameters();
		if (parameters.size() == rightParameters.size()) {
			int result = 0;
			ITypeReferenceOwner leftReferenceOwner = getState().getReferenceOwner();
			ITypeReferenceOwner rightReferenceOwner = right.getState().getReferenceOwner();
			TypeParameterByConstraintSubstitutor substitutor = new TypeParameterByConstraintSubstitutor(getDeclaratorParameterMapping(), leftReferenceOwner);
			TypeParameterByConstraintSubstitutor rightSubstitutor = new TypeParameterByConstraintSubstitutor(right.getDeclaratorParameterMapping(), right.getState().getReferenceOwner());
			for(int i = 0; i < parameters.size(); i++) {
				LightweightTypeReference parameterType = leftReferenceOwner.toLightweightTypeReference(parameters.get(i).getParameterType());
				LightweightTypeReference rightParameterType = rightReferenceOwner.toLightweightTypeReference(rightParameters.get(i).getParameterType());
				if (!parameterType.isResolved() || !rightParameterType.isResolved()) {
					result += compareDeclaredTypes(
							substitutor.substitute(parameterType),
							rightSubstitutor.substitute(rightParameterType),
							false,
							false);
				}
			}
			if (result == 0) {
				return CandidateCompareResult.AMBIGUOUS;
			} else if (result < 0) {
				return CandidateCompareResult.THIS;
			} else {
				return getExpectedTypeCompareResultOther(right);
			}
		}
	}
	return CandidateCompareResult.AMBIGUOUS;
}
 
Example 8
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 9
Source File: DeferredTypeParameterHintCollector.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected LightweightTypeReference getStricterConstraint(final UnboundTypeReference typeParameter, LightweightTypeReference hint) {
	final JvmTypeParameter parameter = typeParameter.getTypeParameter();
	List<JvmTypeConstraint> constraints = parameter.getConstraints();
	for(JvmTypeConstraint constraint: constraints) {
		JvmTypeReference constraintReference = constraint.getTypeReference();
		if (constraintReference != null) {
			final boolean[] recursive = new boolean[] { false };
			LightweightTypeReferenceFactory factory = new LightweightTypeReferenceFactory(hint.getOwner()) {
				@Override
				public LightweightTypeReference doVisitParameterizedTypeReference(JvmParameterizedTypeReference reference) {
					JvmType type = reference.getType();
					if (type == parameter) {// recursively bound
						recursive[0] = true;
					}
					return super.doVisitParameterizedTypeReference(reference);
				}
			};
			LightweightTypeReference lightweightReference = factory.toLightweightReference(constraintReference);
			if (!recursive[0]) {
				if (hint.isAssignableFrom(lightweightReference)) {
					hint = lightweightReference;	
				} else if (hint.isResolved() && !lightweightReference.getRawTypeReference().isAssignableFrom(hint, TypeConformanceComputationArgument.RAW)) {
					return null;
				}
			}
		}
	}
	return hint;
}
 
Example 10
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 11
Source File: ExtensionScopeHelper.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isMatchingFirstParameter(JvmOperation feature) {
	List<JvmFormalParameter> parameters = feature.getParameters();
	JvmFormalParameter firstParameter = parameters.get(0);
	JvmTypeReference type = firstParameter.getParameterType();
	if (type == null)
		return false;
	JvmType rawParameterType = type.getType();
	if (rawParameterType == null || rawParameterType.eIsProxy())
		return false;
	if (!(rawParameterType instanceof JvmTypeParameter)) {
		if (rawArgumentType.isResolved()) {
			// short circuit - limit extension scope entries to real candidates
			LightweightTypeReference parameterTypeReference = rawArgumentType.getOwner().toPlainTypeReference(rawParameterType);
			if (parameterTypeReference.isResolved() && !parameterTypeReference.isAssignableFrom(rawArgumentType)) {
				if (parameterTypeReference.isArray() && !rawArgumentType.isArray() && rawArgumentType.isSubtypeOf(Iterable.class)) {
					return true;
				}
				return false;
			}
			if (parameterTypeReference.isArray() && !rawArgumentType.isArray() && !rawArgumentType.isSubtypeOf(Iterable.class)) {
				return false;
			}
		} else if (isArrayTypeMismatch(rawArgumentType, rawParameterType)) {
			return false;
		}
	}
	return true;
}
 
Example 12
Source File: InheritanceHelper.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies if the type candidate is a proxy (unresolved type) or a subtype of the given super type.
 *
 * @param candidate the type to test.
 * @param jvmSuperType the expected JVM super-type.
 * @param sarlSuperType the expected SARL super-type.
 * @return <code>true</code> if the candidate is a sub-type of the super-type.
 */
public boolean isProxyOrSubTypeOf(LightweightTypeReference candidate, Class<?> jvmSuperType,
		Class<? extends XtendTypeDeclaration> sarlSuperType) {
	if (!candidate.isResolved()) {
		return true;
	}
	return isSubTypeOf(candidate, jvmSuperType, sarlSuperType);
}
 
Example 13
Source File: TypeConformanceComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected String getIdentifier(LightweightTypeReference type) {
	if (type instanceof UnboundTypeReference && !type.isResolved())
		return ((UnboundTypeReference) type).getHandle().toString();
	return type.getJavaIdentifier();
}
 
Example 14
Source File: ExtensionScopeHelper.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected Boolean doVisitTypeReference(LightweightTypeReference reference) {
	return reference.isResolved();
}
 
Example 15
Source File: CollectionLiteralsTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Implements fall-back strategy. If the expected type of a collection literal does not match the actual type, but the expected element
 * types would match the actual element type, the collection literal will be successfully typed according to the expectation.
 */
protected boolean matchesExpectation(LightweightTypeReference elementType, LightweightTypeReference expectation) {
	return expectation != null && expectation.isResolved() && !expectation.isWildcard() && expectation.isAssignableFrom(elementType);
}