org.eclipse.xtext.xbase.typesystem.InferredTypeIndicator Java Examples

The following examples show how to use org.eclipse.xtext.xbase.typesystem.InferredTypeIndicator. 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: LogicalContainerAwareReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected JvmTypeReference createComputedTypeReference(
			Map<JvmIdentifiableElement, ResolvedTypes> resolvedTypesByContext,
			ResolvedTypes resolvedTypes,
			IFeatureScopeSession featureScopeSession,
			JvmMember member,
			/* @Nullable */ InferredTypeIndicator indicator,
			boolean returnType) {
		XComputedTypeReference result = getServices().getXtypeFactory().createXComputedTypeReference();
		if (indicator == null || indicator.getExpression() == null)
			result.setTypeProvider(createTypeProvider(resolvedTypesByContext, resolvedTypes, featureScopeSession, member, returnType));
		else
			result.setTypeProvider(createTypeProvider(resolvedTypesByContext, resolvedTypes, featureScopeSession, member, indicator.getExpression(), returnType));
		// TODO do we need a lightweight computed type reference?
//		resolvedTypes.setType(member, result);
		return result;
	}
 
Example #2
Source File: SARLJvmModelInferrer.java    From sarl with Apache License 2.0 6 votes vote down vote up
private JvmTypeReference ensureValidType(Resource targetResource, JvmTypeReference returnType) {
	// No return type could be inferred => assume "void"
	if (returnType == null) {
		return this._typeReferenceBuilder.typeRef(Void.TYPE);
	}
	// The given type is not associated to the target resource => force relocation.
	final Resource returnTypeResource = returnType.eResource();
	if (returnTypeResource != null && !Objects.equal(returnType.eResource(), targetResource)) {
		return this.typeBuilder.cloneWithProxies(returnType);
	}
	// A return type was inferred => use it as-is because it is not yet resolved to the concrete type.
	if (InferredTypeIndicator.isInferred(returnType)) {
		return returnType;
	}
	// A return was inferred and resolved => use it.
	return this.typeBuilder.cloneWithProxies(returnType);
}
 
Example #3
Source File: SARLJvmModelInferrer.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Clone the given type reference that is associated to another Xtext resource.
 *
 * <p>This function ensures that the resource of the reference clone is not pointing
 * to the resource of the original reference.
 *
 * <p>This function calls {@link JvmTypesBuilder#cloneWithProxies(JvmTypeReference)} or
 * {@link #cloneWithTypeParametersAndProxies(JvmTypeReference, JvmExecutable)} if the
 * {@code target} is {@code null} for the first, and not {@code null} for the second.
 *
 * @param type the source type.
 * @param target the operation for which the type is clone, or {@code null} if not relevant.
 * @return the result type, i.e. a copy of the source type.
 */
protected JvmTypeReference cloneWithProxiesFromOtherResource(JvmTypeReference type, JvmOperation target) {
	if (type == null) {
		return this._typeReferenceBuilder.typeRef(Void.TYPE);
	}
	// Do not clone inferred types because they are not yet resolved and it is located within the current resource.
	if (InferredTypeIndicator.isInferred(type)) {
		return type;
	}
	// Do not clone primitive types because the associated resource to the type reference will not be correct.
	final String id = type.getIdentifier();
	if (Objects.equal(id, Void.TYPE.getName())) {
		return this._typeReferenceBuilder.typeRef(Void.TYPE);
	}
	if (this.services.getPrimitives().isPrimitive(type)) {
		return this._typeReferenceBuilder.typeRef(id);
	}
	// Clone the type
	if (target != null) {
		return cloneWithTypeParametersAndProxies(type, target);
	}
	return this.typeBuilder.cloneWithProxies(type);
}
 
Example #4
Source File: NestedTypesScope.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected IEObjectDescription doGetSingleElement(JvmDeclaredType declarator, QualifiedName name, String firstSegment, int dollarIndex) {
	if (declarator.isLocal()) {
		JvmTypeReference superTypeReference = Iterables.getLast(declarator.getSuperTypes());
		if (InferredTypeIndicator.isInferred(superTypeReference))
			return findNestedTypeInLocalTypeNonResolving(declarator, name, firstSegment, dollarIndex);
	}
	
	Iterable<JvmDeclaredType> nestedTypes = declarator.findAllNestedTypesByName(firstSegment);
	for(JvmDeclaredType nested: nestedTypes) {
		JvmType nestedType = findNestedType(nested, 0, name);
		if (nestedType != null) {
			return toDescription(name, nestedType, dollarIndex, 0);
		}
	}
	return null;
}
 
Example #5
Source File: DispatchOperationBodyComputationState.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
/* @Nullable */
protected LightweightTypeReference getExpectedType() {
	LightweightTypeReference expectedType = super.getExpectedType();
	if (expectedType != null) {
		return expectedType;
	}
	if (dispatcher != null) {
		JvmOperation operation = (JvmOperation) getMember();
		if (!InferredTypeIndicator.isInferred(dispatcher.getReturnType())) {
			LightweightTypeReference result = getResolvedTypes().getActualType(dispatcher);
			if (result != null)
				InferredTypeIndicator.resolveTo(operation.getReturnType(), result.toJavaCompliantTypeReference());
			return result;
		}
	}
	return inheritedExpectedType;
}
 
Example #6
Source File: XtendReentrantTypeResolver.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Initializes the type inference strategy for the cache field for create extensions.
 */
@Override
protected void _doPrepare(ResolvedTypes resolvedTypes, IFeatureScopeSession featureScopeSession, JvmField field,
		Map<JvmIdentifiableElement, ResolvedTypes> resolvedTypesByContext) {
	JvmTypeReference knownType = field.getType();
	if (InferredTypeIndicator.isInferred(knownType)) {
		XComputedTypeReference castedKnownType = (XComputedTypeReference) knownType;
		EObject sourceElement = associations.getPrimarySourceElement(field);
		if (sourceElement instanceof XtendFunction) {
			XtendFunction function = (XtendFunction) sourceElement;
			if (function.getCreateExtensionInfo() != null) {
				JvmOperation operation = associations.getDirectlyInferredOperation(function);
				if (operation != null) {
					declareTypeParameters(resolvedTypes, field, resolvedTypesByContext);
					XComputedTypeReference fieldType = getServices().getXtypeFactory().createXComputedTypeReference();
					fieldType.setTypeProvider(new CreateCacheFieldTypeReferenceProvider(operation, resolvedTypes, featureScopeSession));
					castedKnownType.setEquivalent(fieldType);
					return;
				}
			}
		}
	}
	super._doPrepare(resolvedTypes, featureScopeSession, field, resolvedTypesByContext);
	doPrepareLocalTypes(resolvedTypesByContext.get(field), featureScopeSession, field, resolvedTypesByContext);
}
 
Example #7
Source File: LogicalContainerAwareReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns <code>null</code> if the given operation declares it's own return type or if it does not override
 * another operation.
 */
/* @Nullable */
@SuppressWarnings("unused")
protected LightweightTypeReference getReturnTypeOfOverriddenOperation(JvmOperation operation, ResolvedTypes resolvedTypes, IFeatureScopeSession session) {
	if (operation.getVisibility() == JvmVisibility.PRIVATE)
		return null;
	if (InferredTypeIndicator.isInferred(operation.getReturnType())) {
		LightweightTypeReference declaringType = resolvedTypes.getActualType(operation.getDeclaringType());
		if (declaringType == null) {
			throw new IllegalStateException("Cannot determine declaring type of operation: " + operation);
		}
		LightweightTypeReference result = overrideHelper.getReturnTypeOfOverriddenOperation(operation, declaringType);
		return result;
	}
	return null;
}
 
Example #8
Source File: JvmTypesBuilder.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Detects whether the type reference refers to primitive boolean.
 * 
 * @since 2.9
 */
protected boolean isPrimitiveBoolean(JvmTypeReference typeRef) {
	if (InferredTypeIndicator.isInferred(typeRef)) {
		return false;
	}
	
	return typeRef != null && typeRef.getType() != null &&
			!typeRef.getType().eIsProxy() &&
			"boolean".equals(typeRef.getType().getIdentifier());
}
 
Example #9
Source File: XtendReentrantTypeResolver.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void resolveDispatchCaseTypes(JvmOperation dispatcher, List<JvmOperation> dispatchCases, LightweightTypeReference type,
		IFeatureScopeSession featureScopeSession) {
	if (InferredTypeIndicator.isInferred(dispatcher.getReturnType())) {
		InferredTypeIndicator.resolveTo(dispatcher.getReturnType(), toJavaCompliantTypeReference(type, featureScopeSession));
	}
	for (JvmOperation dispatchCase : dispatchCases) {
		if (InferredTypeIndicator.isInferred(dispatchCase.getReturnType())) {
			InferredTypeIndicator.resolveTo(dispatchCase.getReturnType(), toJavaCompliantTypeReference(type, featureScopeSession));
		}
	}
}
 
Example #10
Source File: OperationBodyComputationState.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected ITypeComputationResult createNoTypeResult() {
	JvmOperation operation = (JvmOperation) getMember();
	LightweightTypeReference expectedType = ((LogicalContainerAwareReentrantTypeResolver)getResolver()).getReturnTypeOfOverriddenOperation(operation, resolvedTypes, getFeatureScopeSession());
	if (expectedType != null) {
		InferredTypeIndicator.resolveTo(operation.getReturnType(), expectedType.toJavaCompliantTypeReference());
	}
	return new NoTypeResult(getMember(), resolvedTypes.getReferenceOwner());
}
 
Example #11
Source File: XtendReentrantTypeResolver.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected LightweightTypeReference getReturnTypeOfOverriddenOperation(JvmOperation operation,
		ResolvedTypes resolvedTypes, IFeatureScopeSession session) {
	if (operation.getVisibility() == JvmVisibility.PRIVATE)
		return null;
	if (InferredTypeIndicator.isInferred(operation.getReturnType())) {
		LightweightTypeReference declaringType = resolvedTypes.getActualType(operation.getDeclaringType());
		if (declaringType == null) {
			throw new IllegalStateException("Cannot determine declaring type of operation: " + operation);
		}
		BottomResolvedOperation resolvedOperation = new BottomResolvedOperation(operation, declaringType, overrideTester);
		List<IResolvedOperation> overriddenMethods = resolvedOperation.getOverriddenAndImplementedMethods();
		if (overriddenMethods.isEmpty())
			return null;
		IResolvedOperation overriddenMethod = overriddenMethods.get(0);
		JvmOperation declaration = overriddenMethod.getDeclaration();
		XExpression inferredFrom = getInferredFrom(declaration.getReturnType());
		// guard against active annotations that put an expression into a second method
		// namely in a synthesized super type - in that case, the expression should not be
		// inferred in the context of the super type but the subtype thus the return type
		// of a super method has to be ignored
		
		// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=439535
		if (inferredFrom != null && (inferredFrom == getInferredFrom(operation.getReturnType()) || isHandled(inferredFrom))) {
			return null;
		}
		LightweightTypeReference result = overriddenMethod.getResolvedReturnType();
		return result;
	}
	return null;
}
 
Example #12
Source File: OperationBodyComputationState.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
/* @Nullable */
protected LightweightTypeReference getExpectedType() {
	JvmOperation operation = (JvmOperation) getMember();
	LightweightTypeReference expectedType = ((LogicalContainerAwareReentrantTypeResolver)getResolver()).getReturnTypeOfOverriddenOperation(operation, resolvedTypes, getFeatureScopeSession());
	if (expectedType != null) {
		InferredTypeIndicator.resolveTo(operation.getReturnType(), expectedType.toJavaCompliantTypeReference());
		return expectedType;
	}
	return getResolvedTypes().getExpectedTypeForAssociatedExpression(getMember(), getNonNullRootExpression());
}
 
Example #13
Source File: LogicalContainerAwareReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the expression that will be used to infer the given type from. If the type is 
 * already resolved, the result will be null. If no expression can be determined, null is
 * also returned.
 */
protected XExpression getInferredFrom(JvmTypeReference typeReference) {
	if (InferredTypeIndicator.isInferred(typeReference)) {
		XComputedTypeReference computed = (XComputedTypeReference) typeReference;
		if (computed.getEquivalent() instanceof XComputedTypeReference) {
			XComputedTypeReference inferred = (XComputedTypeReference) computed.getEquivalent();
			IJvmTypeReferenceProvider typeProvider = inferred.getTypeProvider();
			if (typeProvider instanceof DemandTypeReferenceProvider) {
				return ((DemandTypeReferenceProvider) typeProvider).expression;
			}
		}
	}
	return null;
}
 
Example #14
Source File: LogicalContainerAwareReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected AbstractDemandTypeReferenceProvider getComputedTypeReference(JvmTypeReference knownType) {
	if (InferredTypeIndicator.isInferred(knownType)) {
		XComputedTypeReference casted = (XComputedTypeReference) knownType;
		JvmTypeReference equivalent = casted.getEquivalent();
		if (equivalent instanceof XComputedTypeReference) {
			IJvmTypeReferenceProvider typeProvider = ((XComputedTypeReference) equivalent).getTypeProvider();
			if (typeProvider instanceof AbstractDemandTypeReferenceProvider) {
				return (AbstractDemandTypeReferenceProvider) typeProvider;
			}
		}
	}
	return null;
}
 
Example #15
Source File: OverrideHelper.java    From xtext-extras with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Returns <code>null</code> if the given operation declares it's own return type or if it does not override
 * another operation.
 * 
 * TODO support this case:
 * 
 * <pre>
 * interface I {
 *   String m()
 *   String m2()
 * }
 * class A {
 *   CharSequence m()
 *   int m2()
 * }
 * class B extends A implements I {
 *   m() will expect String since this is the best choice
 *   m2() will expect int since this is actually overridden and not compatible to String from I#m2
 * }
 * </pre>
 */
/* @Nullable */
public LightweightTypeReference getReturnTypeOfOverriddenOperation(JvmOperation operation, ITypeReferenceOwner owner, IVisibilityHelper visibilityHelper) {
	if (operation.getVisibility() == JvmVisibility.PRIVATE || !InferredTypeIndicator.isInferred(operation.getReturnType())) {
		return null;
	}
	LightweightTypeReference declaringType = owner.newParameterizedTypeReference(operation.getDeclaringType());
	TypeParameterSubstitutor<?> substitutor = createSubstitutor(owner, declaringType);
	JvmOperation overriddenOperation = findOverriddenOperation(operation, declaringType, substitutor, owner, visibilityHelper);
	if (overriddenOperation != null) {
		return substitutor.substitute(owner.toLightweightTypeReference(overriddenOperation.getReturnType()));
	}
	return null;
}
 
Example #16
Source File: OverrideHelper.java    From xtext-extras with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Returns <code>null</code> if the given operation declares it's own return type or if it does not override
 * another operation.
 * 
 * TODO support this case:
 * 
 * <pre>
 * interface I {
 *   String m()
 *   String m2()
 * }
 * class A {
 *   CharSequence m()
 *   int m2()
 * }
 * class B extends A implements I {
 *   m() will expect String since this is the best choice
 *   m2() will expect int since this is actually overridden and not compatible to String from I#m2
 * }
 * </pre>
 */
/* @Nullable */
public LightweightTypeReference getReturnTypeOfOverriddenOperation(JvmOperation operation, LightweightTypeReference context) {
	if (operation.getVisibility() == JvmVisibility.PRIVATE || !InferredTypeIndicator.isInferred(operation.getReturnType())) {
		return null;
	}
	BottomResolvedOperation resolvedOperation = new BottomResolvedOperation(operation, context, overrideTester);
	List<IResolvedOperation> overriddenMethods = resolvedOperation.getOverriddenAndImplementedMethods();
	if (overriddenMethods.isEmpty())
		return null;
	LightweightTypeReference result = overriddenMethods.get(0).getResolvedReturnType();
	return result;
}
 
Example #17
Source File: JvmTypesBuilder.java    From xtext-extras with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Produces an inferred type which will be resolved on demand. It should not be attempted to resolve
 * this type during the model inference.
 * 
 * @param expression the expression that will be used resolve the type. May not be <code>null</code>.
 * @return an inferred type.
 */
public JvmTypeReference inferredType(XExpression expression) {
	Preconditions.checkNotNull(expression);
	XComputedTypeReference result = xtypesFactory.createXComputedTypeReference();
	result.setTypeProvider(new InferredTypeIndicator(expression));
	return result;
}
 
Example #18
Source File: JvmTypesBuilder.java    From xtext-extras with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Produces an inferred type which will be resolved on demand. It should not be attempted to resolve
 * this type during the model inference.
 * 
 * @return an inferred type.
 */
public JvmTypeReference inferredType() {
	XComputedTypeReference result = xtypesFactory.createXComputedTypeReference();
	result.setTypeProvider(new InferredTypeIndicator(null));
	return result;
}