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

The following examples show how to use org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference#getWrapperTypeIfPrimitive() . 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: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void _computeTypes(XTypeLiteral object, ITypeComputationState state) {
	JvmType type = object.getType();
	if (type == null) {
		return;
	}
	checkTypeParameterNotAllowedAsLiteral(object, type, state);
	ITypeReferenceOwner owner = state.getReferenceOwner();
	LightweightTypeReference clazz = owner.newParameterizedTypeReference(type);
	for (int i = 0; i < object.getArrayDimensions().size(); i++) {
		clazz = owner.newArrayTypeReference(clazz);
	}
	if (object.getArrayDimensions().isEmpty()) {
		if (clazz.isPrimitiveVoid()) {
			clazz = state.getReferenceOwner().newReferenceTo(Void.class);
		} else {
			clazz = clazz.getWrapperTypeIfPrimitive();
		}
	}
	LightweightTypeReference result = owner.newReferenceTo(Class.class);
	if (result instanceof ParameterizedTypeReference) {
		ParameterizedTypeReference parameterizedTypeReference = (ParameterizedTypeReference) result;
		parameterizedTypeReference.addTypeArgument(clazz);
	}
	state.acceptActualType(result);
}
 
Example 2
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 3
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 4
Source File: CastOperatorLinkingCandidate.java    From sarl with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("checkstyle:magicnumber")
private static int computeCompliance(LightweightTypeReference target, LightweightTypeReference current) {
	if (isSame(target, current)) {
		return 0;
	}
	final LightweightTypeReference target2 = target.getWrapperTypeIfPrimitive();
	if (target2.isSubtypeOf(Number.class)) {
		final LightweightTypeReference current2 = current.getWrapperTypeIfPrimitive();
		if (current2.isSubtypeOf(Number.class)) {
			return 1 + Math.max(getNumberPrecision(target) - getNumberPrecision(current), 0);
		}
		if (target.isAssignableFrom(current)) {
			return 8;
		}
		return 9;
	}
	if (target.isAssignableFrom(current)) {
		return 1;
	}
	return 2;
}
 
Example 5
Source File: XtendValidator.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected List<JvmType> getParamTypes(JvmOperation jvmOperation, boolean wrapPrimitives) {
	List<JvmType> types = newArrayList();
	for (JvmFormalParameter p : jvmOperation.getParameters()) {
		LightweightTypeReference typeReference = toLightweightTypeReference(p.getParameterType());
		if (wrapPrimitives) {
			typeReference = typeReference.getWrapperTypeIfPrimitive();
		}
		types.add(typeReference.getType());
	}
	return types;
}
 
Example 6
Source File: ObjectAndPrimitiveBasedCastOperationCandidateSelector.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Constructor.
 *
 * @param state the current state of the type computation.
 * @param castType the target type.
 * @param expressionType the type of the expression to cast.
 */
protected ObjectAndPrimitiveBasedSelector(AbstractTypeComputationState state,
		LightweightTypeReference castType, LightweightTypeReference expressionType) {
	this.state = state;
	this.castType = castType.getWrapperTypeIfPrimitive();
	this.primitiveCastType = castType.getPrimitiveIfWrapperType();
	this.expressionType = expressionType;
	this.primitiveCast = castType.isPrimitive() || castType.isWrapper();
}
 
Example 7
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;
}
 
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;
}
 
Example 9
Source File: ActualTypeArgumentCollector.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected LightweightBoundTypeArgument boundByConstraint(LightweightTypeReference reference, Object origin) {
	return new LightweightBoundTypeArgument(reference.getWrapperTypeIfPrimitive(), BoundTypeArgumentSource.CONSTRAINT, origin, VarianceInfo.OUT, VarianceInfo.OUT);
}
 
Example 10
Source File: ActualTypeArgumentCollector.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected LightweightBoundTypeArgument boundByDefaultSource(LightweightTypeReference reference) {
	return new LightweightBoundTypeArgument(reference.getWrapperTypeIfPrimitive(), defaultSource, getOrigin(), getExpectedVariance(), getActualVariance());
}
 
Example 11
Source File: TypeArgumentFromComputedTypeCollector.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
private LightweightBoundTypeArgument boundByInference(LightweightTypeReference reference) {
	return new LightweightBoundTypeArgument(reference.getWrapperTypeIfPrimitive(), BoundTypeArgumentSource.INFERRED, getOrigin(), getExpectedVariance(), getActualVariance());
}