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

The following examples show how to use org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference#isAny() . 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: SarlCompiler.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Override
protected void doConversion(LightweightTypeReference left, LightweightTypeReference right,
		ITreeAppendable appendable, XExpression context, Later expression) {
	// This function overrides the super one in order to enables a NULL-SAFE conversion
	// from a wrapper to a primitive type.
	if (left.isPrimitive() && !right.isPrimitive()) {
		if (right.isAny()) {
			convertNullSafeWrapperToPrimitive(left, left, context, appendable, expression);
		} else {
			convertNullSafeWrapperToPrimitive(right, right.getPrimitiveIfWrapperType(), context, appendable, expression);
		}
		return;
	}
	// Standard behavior
	super.doConversion(left, right, appendable, context, expression);
}
 
Example 3
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected LightweightTypeReference appendVariableTypeAndName(XVariableDeclaration varDeclaration, ITreeAppendable appendable) {
	if (!varDeclaration.isWriteable()) {
		appendable.append("final ");
	}
	LightweightTypeReference type = null;
	if (varDeclaration.getType() != null) {
		serialize(varDeclaration.getType(), varDeclaration, appendable);
		type = getLightweightType((JvmIdentifiableElement) varDeclaration);
	} else {
		type = getLightweightType(varDeclaration.getRight());
		if (type.isAny()) {
			type = getTypeForVariableDeclaration(varDeclaration.getRight());
		}
		appendable.append(type);
	}
	appendable.append(" ");
	appendable.append(appendable.declareVariable(varDeclaration, makeJavaIdentifier(varDeclaration.getName())));
	return type;
}
 
Example 4
Source File: AbstractLinkingCandidate.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void resolveAgainstActualType(LightweightTypeReference declaredType, LightweightTypeReference actualType, final AbstractTypeComputationState state) {
	if (!actualType.isAny()) {
		// TODO this(..) and super(..) for generic types
		List<JvmTypeParameter> typeParameters = getDeclaredTypeParameters();
		if (!typeParameters.isEmpty()) {
			// TODO actualType -(hint for)-> declared type == inferred
			// declared type -(hint for)-> actual type == expected
			TypeArgumentFromComputedTypeCollector.resolveAgainstActualType(
					declaredType,
					actualType,
					typeParameters,
					getTypeParameterMapping(),
					BoundTypeArgumentSource.EXPECTATION,
					state.getReferenceOwner());
		}
	}
}
 
Example 5
Source File: RootResolvedTypes.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected AbstractDiagnostic createTypeDiagnostic(XExpression expression, LightweightTypeReference actualType, LightweightTypeReference expectedType) {
	if (!expectedType.isAny()) {
		String actualName = actualType.getSimpleName();
		String expectedName = expectedType.getSimpleName();
		if (actualName.equals(expectedName)) {
			if (expectedType.isAssignableFrom(actualType)) {
				return null;
			}
		}
		if (expression.eContainingFeature() == XbasePackage.Literals.XABSTRACT_FEATURE_CALL__IMPLICIT_FIRST_ARGUMENT) {
			return new EObjectDiagnosticImpl(Severity.ERROR, IssueCodes.INCOMPATIBLE_TYPES, String.format(
					"Type mismatch: cannot convert implicit first argument from %s to %s", actualType.getHumanReadableName(), expectedType.getHumanReadableName()),
					expression, null, -1, null);
		} else {
			return new EObjectDiagnosticImpl(Severity.ERROR, IssueCodes.INCOMPATIBLE_TYPES, String.format(
					"Type mismatch: cannot convert from %s to %s", actualType.getHumanReadableName(), expectedType.getHumanReadableName()),
					expression, null, -1, null);
		}
	} else {
		return new EObjectDiagnosticImpl(Severity.ERROR, IssueCodes.INCOMPATIBLE_TYPES, String.format(
				"Type mismatch: type %s is not applicable at this location", actualType.getHumanReadableName()), expression, null, -1,
				null);
	}
}
 
Example 6
Source File: AbstractTypeComputationState.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void addExtensionsToCurrentScope(List<? extends JvmIdentifiableElement> extensionProviders) {
	if (extensionProviders.isEmpty())
		return;
	if (extensionProviders.size() == 1) {
		addExtensionToCurrentScope(extensionProviders.get(0));
		return;
	}
	Map<XExpression, LightweightTypeReference> prototypeToType = Maps2.newLinkedHashMapWithExpectedSize(extensionProviders.size());
	for(JvmIdentifiableElement extensionProvider: extensionProviders) {
		LightweightTypeReference knownType = getResolvedTypes().getActualType(extensionProvider);
		if (knownType != null && !knownType.isAny() && !knownType.isUnknown()) {
			XFeatureCall prototype = getResolver().getXbaseFactory().createXFeatureCall();
			prototype.setFeature(extensionProvider);
			prototypeToType.put(prototype, knownType);
		}
	}
	if (!prototypeToType.isEmpty())
		featureScopeSession = featureScopeSession.addToExtensionScope(prototypeToType);
}
 
Example 7
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void _computeTypes(XSynchronizedExpression expr, ITypeComputationState state) {
	ITypeComputationState paramState = state.withExpectation(state.getReferenceOwner().newReferenceToObject());
	ITypeComputationResult paramType = paramState.computeTypes(expr.getParam());
	LightweightTypeReference actualParamType = paramType.getActualExpressionType();
	if (actualParamType != null && (actualParamType.isPrimitive() || actualParamType.isAny())) {
		state.addDiagnostic(new EObjectDiagnosticImpl(
				Severity.ERROR,
				IssueCodes.INCOMPATIBLE_TYPES,
				actualParamType.getHumanReadableName() +  " is not a valid type's argument for the synchronized expression.",
				expr.getParam(),
				null,
				-1,
				new String[] { 
				}));
	}
	state.computeTypes(expr.getExpression());
}
 
Example 8
Source File: CollectionLiteralsTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates a list of collection type references from the element types of a collection literal.
 */
protected List<LightweightTypeReference> computeCollectionTypeCandidates(XCollectionLiteral literal, JvmGenericType collectionType, LightweightTypeReference elementTypeExpectation, ITypeComputationState state) {
	List<XExpression> elements = literal.getElements();
	if(!elements.isEmpty()) {
		List<LightweightTypeReference> elementTypes = Lists.newArrayListWithCapacity(elements.size());
		for(XExpression element: elements) {
			ITypeComputationResult elementType = computeTypes(element, elementTypeExpectation, state);
			LightweightTypeReference actualType = elementType.getActualExpressionType();
			if(actualType != null && !actualType.isAny()) {
				ParameterizedTypeReference collectionTypeCandidate = state.getReferenceOwner().newParameterizedTypeReference(collectionType);
				collectionTypeCandidate.addTypeArgument(actualType.getWrapperTypeIfPrimitive());
				elementTypes.add(collectionTypeCandidate);
			}
		}
		return elementTypes;
	}
	return Collections.emptyList();
}
 
Example 9
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Check
public void checkOperandTypesForTripleEquals(XBinaryOperation binaryOperation) {
	if(isTripleEqualsOperation(binaryOperation)){
		LightweightTypeReference left = getActualType(binaryOperation.getLeftOperand());
		LightweightTypeReference right = getActualType(binaryOperation.getRightOperand());
		if(left.isArray() != right.isArray()) {
			if (left.isArray()) {
				if (right.isAny() || right.isType(Object.class) || right.isType(Serializable.class) || right.isType(Cloneable.class)) {
					return;
				}
			} else {
				if (left.isAny() || left.isType(Object.class) || left.isType(Serializable.class) || left.isType(Cloneable.class)) {
					return;
				}
			}
			error("Incompatible operand types " + left.getHumanReadableName() + " and " + right.getHumanReadableName(), null, INVALID_OPERAND_TYPES);
		}
	}
}
 
Example 10
Source File: TypeConformanceComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected List<LightweightTypeReference> replacePrimitivesAndRemoveAnyReferences(List<LightweightTypeReference> types) {
	List<LightweightTypeReference> result = Lists.newArrayList();
	for(LightweightTypeReference type: types) {
		if (!(type.isAny()))
			result.add(type.getWrapperTypeIfPrimitive());
	}
	return result;
}
 
Example 11
Source File: TypeConformanceComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean containsPrimitiveOrAnyReferences(List<LightweightTypeReference> types) {
	for(LightweightTypeReference type: types) {
		if (type.isPrimitive() || type.isAny())
			return true;
	}
	return false;
}
 
Example 12
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 13
Source File: AbstractTypeComputationState.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void addExtensionToCurrentScope(JvmIdentifiableElement extensionProvider) {
	LightweightTypeReference knownType = getResolvedTypes().getActualType(extensionProvider);
	if (knownType != null && !knownType.isAny() && !knownType.isUnknown()) {
		XFeatureCall prototype = getResolver().getXbaseFactory().createXFeatureCall();
		prototype.setFeature(extensionProvider);
		featureScopeSession = featureScopeSession.addToExtensionScope(Collections.<XExpression, LightweightTypeReference>singletonMap(prototype, knownType));
	}
}
 
Example 14
Source File: ClosureWithoutExpectationHelper.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected FunctionTypeReference processExpressionType(FunctionTypeReference incompleteClosureType, ITypeComputationResult expressionResult) {
	LightweightTypeReference expressionResultType = expressionResult.getReturnType();
	if (expressionResultType == null || !expressionResultType.isPrimitiveVoid()) {
		FunctionTypeReference result = getFunctionTypeReference(false);
		LightweightTypeReference expectedReturnType = result.getTypeArguments().get(result.getTypeArguments().size() - 1);
		if (expressionResultType != null && !expressionResultType.isAny()) {
			result.setReturnType(expressionResultType);
			deferredBindTypeArgument(expectedReturnType, expressionResultType, BoundTypeArgumentSource.INFERRED);
		} else {
			LightweightTypeReference objectTypeReference = incompleteClosureType.getOwner().newReferenceToObject();
			result.setReturnType(objectTypeReference);
			deferredBindTypeArgument(expectedReturnType, objectTypeReference, BoundTypeArgumentSource.INFERRED);
		}
		List<LightweightTypeReference> incompleteParameterTypes = incompleteClosureType.getParameterTypes();
		for(int i = 0; i < incompleteParameterTypes.size(); i++) {
			result.addParameterType(incompleteParameterTypes.get(i));
		}
		List<LightweightTypeReference> incompleteTypeArguments = incompleteClosureType.getTypeArguments();
		List<LightweightTypeReference> resultTypeArguments = result.getTypeArguments();
		for(int i = 0; i < incompleteTypeArguments.size(); i++) {
			deferredBindTypeArgument(resultTypeArguments.get(i), incompleteTypeArguments.get(i), BoundTypeArgumentSource.INFERRED);
		}
		return result;
	} else {
		incompleteClosureType.setReturnType(expressionResultType);
		return incompleteClosureType;
	}
}
 
Example 15
Source File: TypeParameterSubstitutor.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected LightweightTypeReference doVisitWildcardTypeReference(WildcardTypeReference reference, Visiting visiting) {
	if (reference.isResolved() && reference.isOwnedBy(getOwner()))
		return reference;
	WildcardTypeReference result = getOwner().newWildcardTypeReference();
	LightweightTypeReference lowerBound = reference.getLowerBound();
	if (lowerBound != null) {
		LightweightTypeReference visited = visitTypeArgument(lowerBound, visiting, true);
		if (visited.isWildcard()) {
			LightweightTypeReference lowerBoundSubstitute = visited.getLowerBoundSubstitute();
			if (lowerBoundSubstitute.isAny()) {
				result.addUpperBound(getOwner().newReferenceToObject());
				return result;
			} else {
				result.setLowerBound(lowerBoundSubstitute);
			}
		} else {
			result.setLowerBound(visited.copyInto(result.getOwner()));
		}
	} 
	for(LightweightTypeReference upperBound: reference.getUpperBounds()) {
		LightweightTypeReference visitedArgument = visitTypeArgument(upperBound, visiting);
		LightweightTypeReference upperBoundSubstitute = visitedArgument.getUpperBoundSubstitute();
		result.addUpperBound(upperBoundSubstitute);
	}
	if (result.getUpperBounds().isEmpty()) {
		throw new IllegalStateException("UpperBounds may not be empty");
	}
	return result;
}
 
Example 16
Source File: TypeConvertingCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void doConversion(final LightweightTypeReference left, final LightweightTypeReference right,
		final ITreeAppendable appendable, XExpression context, final Later expression) {
	if(left.isPrimitive() && !right.isPrimitive()) {
		if (right.isAny()) {
			convertWrapperToPrimitive(left, left, context, appendable, expression);
		} else {
			convertWrapperToPrimitive(right, right.getPrimitiveIfWrapperType(), context, appendable, expression);
		}
	} else if (right.isPrimitive() && !left.isPrimitive()) {
		convertPrimitiveToWrapper(right, right.getWrapperTypeIfPrimitive(), appendable, expression);
	} else if (right.isMultiType()) {
		convertMultiType(left, (CompoundTypeReference) right, context, appendable, expression);
	} else if (right.isArray() && !left.isArray() && left.isSubtypeOf(Iterable.class)) {
		convertArrayToList(left, appendable, context, expression);
	} else if (isJavaConformant(left, right)) {
		if (mustInsertTypeCast(context, left)) {
			doCastConversion(left, appendable, expression);
		} else {
			expression.exec(appendable);
		}
	} else if (left.isArray() && !right.isArray() && right.isSubtypeOf(Iterable.class)) {
		convertListToArray(left, appendable, expression);
	} else if ((isFunction(left) && isFunction(right) || isProcedure(left) && isProcedure(right)) && left.getType() == right.getType()) {
		doCastConversion(left, appendable, expression);
	} else if (isFunction(right) || (isFunction(left) && findImplementingOperation(right) != null)) {
		convertFunctionType(left, right, appendable, expression);
	} else if (isProcedure(right) || (isProcedure(left) && findImplementingOperation(right) != null)) {
		convertFunctionType(left, right, appendable, expression);
	} else if (mustInsertTypeCast(context, left)) {
		doCastConversion(left, appendable, expression);
	} else {
		expression.exec(appendable);
	}
}
 
Example 17
Source File: UIStrings.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected String referenceToString(LightweightTypeReference reference) {
	if (reference == null) {
		return "[null]";
	}
	if (reference.isAny()) {
		return "Object";
	}
	return reference.getHumanReadableName();
}
 
Example 18
Source File: XbaseCopyQualifiedNameService.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected String toQualifiedName(XExpression expression, IResolvedExecutable resolvedExecutable, JvmExecutable executable,
		IResolvedTypes resolvedTypes, List<XExpression> arguments) {
	LightweightTypeReference actualType = resolvedTypes.getActualType(expression);
	if (actualType != null && !actualType.isAny() && !actualType.isUnknown()) {
		return actualType.getHumanReadableName();
	}

	int index = arguments.indexOf(expression);
	if (resolvedExecutable == null) {
		return executable.getParameters().get(index).getParameterType().getSimpleName();
	}
	return resolvedExecutable.getResolvedParameterTypes().get(index).getSimpleName();
}
 
Example 19
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected LightweightTypeReference getAndEnhanceIterableOrArrayFromComponent(LightweightTypeReference parameterType, JvmGenericType iterableType,
		final CompoundTypeReference compoundResult) {
	if (parameterType.isUnknown()) {
		compoundResult.addComponent(parameterType);
		return parameterType;
	}
	ITypeReferenceOwner owner = compoundResult.getOwner();
	LightweightTypeReference iterableOrArray = null;
	LightweightTypeReference addAsArrayComponentAndIterable = null;
	if (parameterType.isPrimitive()) {
		iterableOrArray = owner.newArrayTypeReference(parameterType);
		compoundResult.addComponent(iterableOrArray);
		addAsArrayComponentAndIterable = parameterType.getWrapperTypeIfPrimitive();
	} else if (parameterType.isAny()) {
		addAsArrayComponentAndIterable = parameterType.getOwner().newReferenceToObject();
	} else {
		addAsArrayComponentAndIterable = parameterType;
	}
	if (iterableType != null) {
		ParameterizedTypeReference reference = owner.newParameterizedTypeReference(iterableType);
		WildcardTypeReference wildcard = owner.newWildcardTypeReference();
		wildcard.addUpperBound(addAsArrayComponentAndIterable);
		reference.addTypeArgument(wildcard);
		compoundResult.addComponent(reference);
		if (iterableOrArray == null) {
			iterableOrArray = reference;
			LightweightTypeReference potentialPrimitive = addAsArrayComponentAndIterable.getPrimitiveIfWrapperType();
			if (potentialPrimitive != addAsArrayComponentAndIterable) {
				compoundResult.addComponent(owner.newArrayTypeReference(potentialPrimitive));
			}
		}
		compoundResult.addComponent(owner.newArrayTypeReference(addAsArrayComponentAndIterable));
	} else if (iterableOrArray == null) { // no JRE on the CP
		if (addAsArrayComponentAndIterable != null) {
			iterableOrArray = owner.newArrayTypeReference(addAsArrayComponentAndIterable);
			compoundResult.addComponent(iterableOrArray);
		} else {
			compoundResult.addComponent(parameterType);
			return parameterType;
		}
	}
	return iterableOrArray;
}