org.eclipse.xtext.xbase.typesystem.conformance.TypeConformanceComputationArgument Java Examples

The following examples show how to use org.eclipse.xtext.xbase.typesystem.conformance.TypeConformanceComputationArgument. 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
/**
 * Same as {@link LightweightTypeReference#isSubtypeOf(Class)} but does not accept synonym types as subtypes.
 */
protected boolean isSubtypeButNotSynonym(LightweightTypeReference expectation, Class<?> clazz) {
	if (expectation.isType(clazz)) {
		return true;
	}
	ITypeReferenceOwner owner = expectation.getOwner();
	JvmType declaredType = owner.getServices().getTypeReferences().findDeclaredType(clazz, owner.getContextResourceSet());
	if (declaredType == null) {
		return false;
	}
	LightweightTypeReference superType = owner.newParameterizedTypeReference(declaredType);
	// don't allow synonyms, e.g. Iterable is not considered to be a supertype of Functions.Function0
	boolean result = superType.isAssignableFrom(expectation.getRawTypeReference(), 
			new TypeConformanceComputationArgument(false, false, true, true, false, false));
	return result;
}
 
Example #2
Source File: AbstractPendingLinkingCandidate.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected int compareDeclaredTypes(LightweightTypeReference left, LightweightTypeReference right, boolean leftResolved, boolean rightResolved) {
	int rightToLeftConformance = left.internalIsAssignableFrom(right, TypeConformanceComputationArgument.DEFAULT);
	if ((rightToLeftConformance & ConformanceFlags.SUCCESS) != 0) {
		if (!right.isAssignableFrom(left) && 
				(!leftResolved || !rightResolved || ((rightToLeftConformance & ConformanceFlags.RAW_TYPE_CONVERSION) == 0))) {
			return 1;
		}
	} else {
		int leftToRightConformance = right.internalIsAssignableFrom(left, TypeConformanceComputationArgument.DEFAULT);
		if ((leftToRightConformance & ConformanceFlags.SUCCESS) != 0 && 
				(!leftResolved || !rightResolved || ((leftToRightConformance & ConformanceFlags.RAW_TYPE_CONVERSION) == 0))) {
			return -1;
		}
	}
	return 0;
}
 
Example #3
Source File: DispatchMethodCompileStrategy.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void generateActualDispatchCall(JvmOperation dispatchOperation, JvmOperation actualOperationToCall,
		ITreeAppendable a, ITypeReferenceOwner owner) {
	a.append(actualOperationToCall.getSimpleName()).append("(");
	Iterator<JvmFormalParameter> iter1 = dispatchOperation.getParameters().iterator();
	for (Iterator<JvmFormalParameter> iter2 = actualOperationToCall.getParameters().iterator(); iter2.hasNext();) {
		JvmFormalParameter p1 = iter1.next();
		JvmFormalParameter p2 = iter2.next();
		LightweightTypeReference type1 = owner.toLightweightTypeReference(p1.getParameterType());
		LightweightTypeReference type2 = owner.toLightweightTypeReference(p2.getParameterType());
		if (!type2.isAssignableFrom(type1, new TypeConformanceComputationArgument(true, false, true, true, false, false))) {
			a.append("(").append(type2.getWrapperTypeIfPrimitive()).append(")");
		}
		if (typeReferences.is(p2.getParameterType(), Void.class)) {
			a.append("null");
		} else {
			a.append(getVarName(p1, a));
		}
		if (iter2.hasNext()) {
			a.append(", ");
		}
	}
	a.append(")");
}
 
Example #4
Source File: CheckJavaValidator.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Checks that type of a default expression of a formal parameter actually matches the declared type.
 *
 * @param parameter
 *          to check
 */
@Check
public void checkFormalParameterTypeConformance(final FormalParameter parameter) {
  JvmTypeReference jvmType = parameter.getType();
  XExpression value = parameter.getRight();
  if (jvmType == null || value == null) {
    return;
  }
  LightweightTypeReference declaredType = toLightweightTypeReference(jvmType);
  LightweightTypeReference valueType = getActualType(value);
  if (!declaredType.isAssignableFrom(valueType, new TypeConformanceComputationArgument())) {
    error(Messages.CheckJavaValidator_FormalParameterType_Incompatibility, value, null, IssueCodes.FORMAL_PARAMETER_TYPE);
  }
}
 
Example #5
Source File: ConstructorLinkingCandidate.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected LightweightTypeReference deferredBindTypeArgument(ITypeExpectation expectation, LightweightTypeReference type) {
	LightweightTypeReference result = super.deferredBindTypeArgument(expectation, type);
	LightweightTypeReference expectedType = expectation.getExpectedType();
	if (expectedType != null && getConstructorCall().getTypeArguments().isEmpty() && !result.isRawType() && !getDeclaredTypeParameters().isEmpty()) {
		if (!expectedType.isAssignableFrom(result, TypeConformanceComputationArgument.DEFAULT)) {
			LightweightTypeReference rawFeatureType = result.getRawTypeReference();
			if (expectedType.isAssignableFrom(rawFeatureType)) {
				result = rawFeatureType;
				getTypeParameterMapping().clear();
			}
		}
	}
	return result;
}
 
Example #6
Source File: ResolvedTypes.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected int getConformanceFlags(TypeData typeData, boolean recompute) {
	int flags = typeData.getConformanceFlags();
	if (recompute) {
		if ((flags & ConformanceFlags.SEALED) != 0) {
			ConformanceFlags.sanityCheck(flags);
			return flags;
		}
		flags &= ~(ConformanceFlags.INCOMPATIBLE | ConformanceFlags.SUCCESS);
		flags |= ConformanceFlags.UNCHECKED;
	}
	if ((flags & ConformanceFlags.UNCHECKED) != 0) {
		LightweightTypeReference actualType = typeData.getActualType();
		ITypeExpectation expectation = typeData.getExpectation();
		LightweightTypeReference expectedType = expectation.getExpectedType();
		if (expectedType != null) {
			int conformanceResult = expectedType.getUpperBoundSubstitute().internalIsAssignableFrom(
					actualType, TypeConformanceComputationArgument.DEFAULT);
			flags |= conformanceResult | ConformanceFlags.CHECKED;
			flags &= ~ConformanceFlags.UNCHECKED;
		} else {
			flags &= ~ConformanceFlags.UNCHECKED;
			flags |= ConformanceFlags.CHECKED_SUCCESS; 
		}
	}
	ConformanceFlags.sanityCheck(flags);
	typeData.setConformanceFlags(flags);
	return flags;
}
 
Example #7
Source File: ResolvedFeature.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected Map<JvmTypeParameter, LightweightMergedBoundTypeArgument> getDeclaratorParameterMapping() {
	final Wrapper<Map<JvmTypeParameter, LightweightMergedBoundTypeArgument>> receiverTypeParameterMapping = Wrapper.wrap(Collections.<JvmTypeParameter, LightweightMergedBoundTypeArgument>emptyMap());
	XExpression receiver = getReceiver();
	if (receiver != null) {
		LightweightTypeReference receiverType = getReceiverType();
		if (receiverType == null) {
			throw new IllegalStateException("Cannot determine type of receiver "+ getReceiver());
		}
		JvmIdentifiableElement feature = getFeature();
		if (feature instanceof JvmFeature) {
			JvmDeclaredType declaringType = ((JvmFeature) feature).getDeclaringType();
			final LightweightTypeReference declaringTypeReference = receiverType.getOwner().newParameterizedTypeReference(declaringType);
			final TypeConformanceComputationArgument rawConformanceCheck = new TypeConformanceComputationArgument(true, false, false, false, false, false);
			if (declaringTypeReference.isAssignableFrom(receiverType, rawConformanceCheck)) {
				receiverTypeParameterMapping.set(new DeclaratorTypeArgumentCollector().getTypeParameterMapping(receiverType));
			} else {
				CommonTypeComputationServices services = receiverType.getOwner().getServices();
				SynonymTypesProvider synonymProvider = services.getSynonymTypesProvider();
				synonymProvider.collectSynonymTypes(receiverType, new SynonymTypesProvider.Acceptor() {
					
					@Override
					protected boolean accept(LightweightTypeReference synonym, int hints) {
						if (declaringTypeReference.isAssignableFrom(synonym, rawConformanceCheck)) {
							receiverTypeParameterMapping.set(new DeclaratorTypeArgumentCollector().getTypeParameterMapping(synonym));
							return false;
						}
						return true;
					}
				});
			}
			
		} else {
			receiverTypeParameterMapping.set(new DeclaratorTypeArgumentCollector().getTypeParameterMapping(receiverType));
		}
	}
	return receiverTypeParameterMapping.get();
}
 
Example #8
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Check
public void checkInstanceOf(XInstanceOfExpression instanceOfExpression) {
	LightweightTypeReference leftType = getActualType(instanceOfExpression.getExpression());
	final LightweightTypeReference rightType = toLightweightTypeReference(instanceOfExpression.getType(), true);
	if (leftType == null || rightType == null || rightType.getType() == null || rightType.getType().eIsProxy()) {
		return;
	}
	if (containsTypeArgs(rightType)) {
		error("Cannot perform instanceof check against parameterized type " + getNameOfTypes(rightType), null, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, INVALID_INSTANCEOF);
		return;
	}
	if (leftType.isAny() || leftType.isUnknown()) {
		return; // null / unknown is ok
	}
	if (rightType.isPrimitive()) {
		error("Cannot perform instanceof check against primitive type " + this.getNameOfTypes(rightType), null, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, INVALID_INSTANCEOF);
		return;
	}
	if (leftType.isPrimitive() 
		|| rightType.isArray() && !(leftType.isArray() || leftType.isType(Object.class) || leftType.isType(Cloneable.class) || leftType.isType(Serializable.class))
		|| isFinal(rightType) && !memberOfTypeHierarchy(rightType, leftType)
		|| isFinal(leftType) && !memberOfTypeHierarchy(leftType, rightType)) {
		error("Incompatible conditional operand types " + this.getNameOfTypes(leftType)+" and "+this.getNameOfTypes(rightType), null, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, INVALID_INSTANCEOF);
		return;
	}
	if (!isIgnored(OBSOLETE_INSTANCEOF) && rightType.isAssignableFrom(leftType, new TypeConformanceComputationArgument(false, false, true, true, false, false))) {
		// check that we do not have a type parameter usage on the rhs of the instanceof 
		if (rightType.getConstraintSubstitute() == rightType) {
			addIssueToState(OBSOLETE_INSTANCEOF, "The expression of type " + getNameOfTypes(leftType) + " is already of type " + canonicalName(rightType), null);	
		}
	}
}
 
Example #9
Source File: UnboundTypeReference.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns true if the existing hints would allow to resolve to the given reference.
 */
public boolean canResolveTo(LightweightTypeReference reference) {
	if (internalIsResolved())
		return reference.isAssignableFrom(resolvedTo, new TypeConformanceComputationArgument(false, true, true, true, false, false /* TODO do we need to support synonmys here? */));
	List<LightweightBoundTypeArgument> hints = getAllHints();
	if (!hints.isEmpty() && hasSignificantHints(hints)) {
		return canResolveTo(reference, hints);
	}
	return false;
}
 
Example #10
Source File: OverrideTester.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void addReturnTypeDetails(AbstractResolvedOperation overriding, AbstractResolvedOperation overridden,
		EnumSet<OverrideCheckDetails> result) {
	LightweightTypeReference overridingReturnType = overriding.getResolvedReturnType();
	LightweightTypeReference overriddenReturnType = overridden.getResolvedReturnType();
	TypeConformanceComputationArgument conformanceArgument = new TypeConformanceComputationArgument(false, false, false, false, false, false);
	if (!overriddenReturnType.isAssignableFrom(overridingReturnType, conformanceArgument)) {
		if (overriding.getTypeParameters().isEmpty() && !overridden.getTypeParameters().isEmpty()) {
			TypeConformanceComputationArgument rawConformanceArgument = new TypeConformanceComputationArgument(true, false, false, false, false, false);
			if (!overriddenReturnType.isAssignableFrom(overridingReturnType, rawConformanceArgument)) {
				result.add(OverrideCheckDetails.RETURN_MISMATCH);		
			} else {
				result.add(OverrideCheckDetails.UNCHECKED_CONVERSION_REQUIRED);
				if (overridingReturnType.getRawTypeReference().getType() != overriddenReturnType.getRawTypeReference().getType()) {
					result.add(OverrideCheckDetails.COVARIANT_RETURN);
				}
			}
		} else {
			result.add(OverrideCheckDetails.RETURN_MISMATCH);
		}
	} else if (!overriddenReturnType.getJavaIdentifier().equals(overridingReturnType.getJavaIdentifier())) {
		if (!overridden.isRawTypeInheritance() && overriding.getTypeParameters().isEmpty() && !overridden.getTypeParameters().isEmpty()) {
			if (overridden.getTypeParameters().contains(overridden.getDeclaration().getReturnType().getType())) {
				result.add(OverrideCheckDetails.UNCHECKED_CONVERSION_REQUIRED);
			}
		}
		result.add(OverrideCheckDetails.COVARIANT_RETURN);
	}
}
 
Example #11
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 #12
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 #13
Source File: XbaseTypeConformanceTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isConformantReturnTypes(final String leftExpression, final String rightExpression, boolean ignoreGenerics)
		throws Exception {
	XExpression leftParse = parseHelper.parse(leftExpression);
	IResolvedTypes leftTypes = typeResolver.resolveTypes(leftParse);
	XExpression rightParse = parseHelper.parse(rightExpression, leftParse.eResource().getResourceSet());
	LightweightTypeReference leftType = leftTypes.getActualType(leftParse);
	IResolvedTypes rightTypes = typeResolver.resolveTypes(rightParse);
	LightweightTypeReference rightType = rightTypes.getActualType(rightParse);
	if (rightType == null) {
		throw new IllegalStateException("rightType may not be null");
	}
	boolean conformant = leftType.isAssignableFrom(rightType, new TypeConformanceComputationArgument(ignoreGenerics, false, true, true, false, true));
	return conformant;
}
 
Example #14
Source File: LightweightTypeReference.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public int internalIsAssignableFrom(LightweightTypeReference reference, TypeConformanceComputationArgument argument) {
	TypeConformanceComputer conformanceCompouter = getOwner().getServices().getTypeConformanceComputer();
	int result = conformanceCompouter.isConformant(this, reference, argument);
	return result;
}
 
Example #15
Source File: AssignabilityTest.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean doIsAssignable(final LightweightTypeReference lhs, final LightweightTypeReference rhs) {
  final int result = lhs.internalIsAssignableFrom(rhs, TypeConformanceComputationArgument.DEFAULT);
  Assert.assertTrue(((result & ConformanceFlags.RAW_TYPE) == 0));
  return ((result & ConformanceFlags.SUCCESS) != 0);
}
 
Example #16
Source File: RawAssignabilityTest.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean doIsAssignable(final LightweightTypeReference lhs, final LightweightTypeReference rhs) {
  final int result = lhs.internalIsAssignableFrom(rhs, TypeConformanceComputationArgument.RAW);
  Assert.assertTrue(((result & ConformanceFlags.RAW_TYPE) != 0));
  return ((result & ConformanceFlags.SUCCESS) != 0);
}
 
Example #17
Source File: LightweightTypeReference.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public boolean isAssignableFrom(LightweightTypeReference reference, TypeConformanceComputationArgument argument) {
	int result = internalIsAssignableFrom(reference, argument);
	return (result & ConformanceFlags.INCOMPATIBLE) == 0;
}
 
Example #18
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected boolean memberOfTypeHierarchy(LightweightTypeReference type, LightweightTypeReference potentialMember) {
	return potentialMember.getConstraintSubstitute().isAssignableFrom(type, new TypeConformanceComputationArgument(false, false, false, true, false, false));
}
 
Example #19
Source File: LightweightTypeReference.java    From xtext-extras with Eclipse Public License 2.0 2 votes vote down vote up
/**
    * Determines if this type reference denotes the same or a supertype of 
    * the given {@code reference}.
    */
public boolean isAssignableFrom(LightweightTypeReference reference) {
	return isAssignableFrom(reference, TypeConformanceComputationArgument.DEFAULT);
}