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

The following examples show how to use org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference#isUnknown() . 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: TypeReferenceImpl.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Type getType() {
  Type _switchResult = null;
  LightweightTypeReference _delegate = this.getDelegate();
  final LightweightTypeReference it = _delegate;
  boolean _matched = false;
  boolean _isUnknown = it.isUnknown();
  if (_isUnknown) {
    _matched=true;
    CompilationUnitImpl _compilationUnit = this.getCompilationUnit();
    String _packageName = this.getCompilationUnit().getPackageName();
    String _plus = (_packageName + ".");
    String _simpleName = it.getSimpleName();
    String _plus_1 = (_plus + _simpleName);
    _switchResult = new UnknownType(_compilationUnit, _plus_1);
  }
  if (!_matched) {
    _switchResult = this.getCompilationUnit().toType(it.getType());
  }
  return _switchResult;
}
 
Example 2
Source File: XbaseHyperLinkHelper.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void addOpenInferredTypeHyperlink(final XtextResource resource, JvmIdentifiableElement typedElement,
		ILeafNode node, final IHyperlinkAcceptor acceptor) {
	IResolvedTypes resolveTypes = typeResolver.resolveTypes(resource);
	final LightweightTypeReference type = resolveTypes.getActualType(typedElement);
	if (type != null && !type.isUnknown() && type.getType() != null) {
		createHyperlinksTo(resource, new Region(node.getOffset(), node.getLength()), type.getType(), new IHyperlinkAcceptor() {
			@Override
			public void accept(IHyperlink hyperlink) {
				if (hyperlink instanceof AbstractHyperlink) {
					AbstractHyperlink abstractHyperlink = (AbstractHyperlink) hyperlink;
					abstractHyperlink.setHyperlinkText("Open Inferred Type - " + type.getSimpleName());
					abstractHyperlink.setTypeLabel(SingleHoverShowingHyperlinkPresenter.SHOW_ALWAYS);
				}
				acceptor.accept(hyperlink);
			}
		});
	}
}
 
Example 3
Source File: FeatureScopes.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected IScope createImplicitFeatureCallScope(QualifiedName implicitName, EObject featureCall,
		IFeatureScopeSession session, IResolvedTypes resolvedTypes, IScope parent) {
	IEObjectDescription thisDescription = session.getLocalElement(implicitName);
	if (thisDescription != null) {
		JvmIdentifiableElement thisElement = (JvmIdentifiableElement) thisDescription.getEObjectOrProxy();
		boolean validStaticScope = true;
		if (thisElement instanceof JvmType && THIS.equals(implicitName) && !session.isInstanceContext()) {
			validStaticScope = false;
		}
		LightweightTypeReference type = resolvedTypes.getActualType(thisElement);
		if (type !=null && !type.isUnknown()) {
			XFeatureCall implicitReceiver = xbaseFactory.createXFeatureCall();
			implicitReceiver.setFeature(thisElement);
			return createFeatureScopeForTypeRef(implicitReceiver, type, true, featureCall, session, thisElement, parent, validStaticScope);
		}
	}
	return parent;
}
 
Example 4
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 5
Source File: FeatureScopes.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected IScope createConstructorDelegates(EObject context, IScope parent, IFeatureScopeSession session, IResolvedTypes resolvedTypes) {
	if (session.isConstructorContext()) {
		IEObjectDescription thisDescription = session.getLocalElement(THIS);
		if (thisDescription != null) {
			JvmIdentifiableElement thisElement = (JvmIdentifiableElement) thisDescription.getEObjectOrProxy();
			LightweightTypeReference type = resolvedTypes.getActualType(thisElement);
			if (type != null && !type.isUnknown()) {
				return createConstructorDelegateScope(context, type, parent, session);
			}
		}
	}
	return parent;
}
 
Example 6
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 7
Source File: ReferencedInvalidTypeFinder.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected LightweightTypeReference doVisitTypeReference(final LightweightTypeReference reference) {
  boolean _isUnknown = reference.isUnknown();
  if (_isUnknown) {
    return reference;
  }
  return null;
}
 
Example 8
Source File: FeatureScopes.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected IScope createImplicitStaticScope(QualifiedName implicitName, XAbstractFeatureCall featureCall, IFeatureScopeSession session,
		IResolvedTypes resolvedTypes, IScope parent) {
	IEObjectDescription thisDescription = session.getLocalElement(implicitName);
	if (thisDescription != null) {
		JvmIdentifiableElement thisElement = (JvmIdentifiableElement) thisDescription.getEObjectOrProxy();
		LightweightTypeReference type = resolvedTypes.getActualType(thisElement);
		if (type != null && !type.isUnknown()) {
			TypeBucket receiverBucket = new TypeBucket(-1, Collections.singletonList(type.getType()), resolvedFeaturesProvider);
			return createStaticFeatureScope(featureCall, null, type, receiverBucket, parent, session);
		}
	}
	return parent;
}
 
Example 9
Source File: FeatureScopes.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected IScope createImplicitExtensionScope(QualifiedName implicitName, EObject featureCall,
		IFeatureScopeSession session, IResolvedTypes resolvedTypes, IScope parent) {
	IEObjectDescription thisDescription = session.getLocalElement(implicitName);
	if (thisDescription != null) {
		JvmIdentifiableElement thisElement = (JvmIdentifiableElement) thisDescription.getEObjectOrProxy();
		LightweightTypeReference type = resolvedTypes.getActualType(thisElement);
		if (type != null && !type.isUnknown()) {
			XFeatureCall implicitReceiver = xbaseFactory.createXFeatureCall();
			implicitReceiver.setFeature(thisElement);
			return createStaticExtensionsScope(featureCall, implicitReceiver, type, true, parent, session);
		}
	}
	return parent;
}
 
Example 10
Source File: FeatureScopes.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected IScope createDynamicExtensionsScope(QualifiedName implicitFirstArgumentName, IEObjectDescription firstArgumentDescription, EObject featureCall,
		IFeatureScopeSession captureLayer, IFeatureScopeSession session, IResolvedTypes resolvedTypes, IScope parent) {
	JvmIdentifiableElement feature = (JvmIdentifiableElement) firstArgumentDescription.getEObjectOrProxy();
	if (feature instanceof JvmType && THIS.equals(implicitFirstArgumentName) && !session.isInstanceContext()) {
		return parent;
	}
	LightweightTypeReference type = resolvedTypes.getActualType(feature);
	if (type != null && !type.isUnknown()) {
		XFeatureCall implicitArgument = xbaseFactory.createXFeatureCall();
		implicitArgument.setFeature(feature);
		return createDynamicExtensionsScope(featureCall, implicitArgument, type, true, parent, captureLayer);
	}
	return parent;
}
 
Example 11
Source File: FeatureScopes.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isUnknownReceiverType(LightweightTypeReference receiverType) {
	if (receiverType.isUnknown()) {
		return true;
	}
	if (receiverType instanceof UnboundTypeReference) {
		List<LightweightBoundTypeArgument> hints = ((UnboundTypeReference) receiverType).getAllHints();
		for(LightweightBoundTypeArgument hint: hints) {
			LightweightTypeReference typeReference = hint.getTypeReference();
			if (typeReference != null && typeReference.isUnknown())
				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 checkClosureParameterTypes(XClosure closure) {
	if (closure.getFormalParameters().isEmpty())
		return;
	LightweightTypeReference closureType = getActualType(closure);
	if (closureType != null && closureType.isUnknown())
		return;
	boolean checkedClosure = false;
	for (JvmFormalParameter p : closure.getFormalParameters()) {
		if (p.getParameterType() == null) {
			if (!checkedClosure) {
				LightweightTypeReference type = getExpectedType(closure);
				if (type == null) {
					error("There is no context to infer the closure's argument types from. Consider typing the arguments or put the closures into a typed context.",
							closure, null, INSIGNIFICANT_INDEX, TOO_LITTLE_TYPE_INFORMATION);
					return;
				} else {
					JvmOperation operation = getServices().getFunctionTypes().findImplementingOperation(type);
					if (operation == null) {
						error("There is no context to infer the closure's argument types from. Consider typing the arguments or use the closures in a more specific context.",
								closure, null, INSIGNIFICANT_INDEX, TOO_LITTLE_TYPE_INFORMATION);
						return;
					}
				}
				checkedClosure = true;
			}
			LightweightTypeReference parameterType = getActualType(closure, p);
			if (parameterType == null) {
				error("There is no context to infer the closure's argument types from. Consider typing the arguments or use the closures in a more specific context.",
						closure, null, INSIGNIFICANT_INDEX, TOO_LITTLE_TYPE_INFORMATION);
				return;
			}
		}
	}
}
 
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: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _computeTypes(XThrowExpression object, ITypeComputationState state) {
	LightweightTypeReference throwable = getRawTypeForName(Throwable.class, state);
	ITypeComputationState expressionState = state.withExpectation(throwable);
	ITypeComputationResult types = expressionState.computeTypes(object.getExpression());
	
	state.acceptActualType(getPrimitiveVoid(state), ConformanceFlags.NO_IMPLICIT_RETURN | ConformanceFlags.THROWN_EXCEPTION);
	
	LightweightTypeReference thrownException = types.getActualExpressionType();
	if (thrownException != null && !thrownException.isUnknown()) {
		validateUnhandledException(thrownException, object, XbasePackage.Literals.XTHROW_EXPRESSION__EXPRESSION, state, 
				(type)->"Unhandled exception type " + type.getSimpleName());
	}
}
 
Example 15
Source File: SynonymTypesProvider.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Announce a synonym type with the given conformance flags.
 * @see ConformanceFlags
 */
protected final boolean announceSynonym(LightweightTypeReference synonym, int flags, Acceptor acceptor) {
	if (synonym.isUnknown()) {
		return true;
	}
	return acceptor.accept(synonym, flags | ConformanceFlags.CHECKED_SUCCESS);
}
 
Example 16
Source File: SynonymTypesProvider.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Announce a synonym type with the given conformance hints.
 */
protected final boolean announceSynonym(LightweightTypeReference synonym, EnumSet<ConformanceHint> hints, Acceptor acceptor) {
	if (synonym.isUnknown()) {
		return true;
	}
	return acceptor.accept(synonym, hints);
}
 
Example 17
Source File: SynonymTypesProvider.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Announce a synonym type with the given conformance hint.
 */
protected final boolean announceSynonym(LightweightTypeReference synonym, ConformanceHint hint, Acceptor acceptor) {
	if (synonym.isUnknown()) {
		return true;
	}
	return acceptor.accept(synonym, hint);
}
 
Example 18
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 19
Source File: ResolvedTypes.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
private void enhanceMergeData(List<TypeData> values, MergeData mergeData) {
	for (int i = 0, size = values.size(); i < size; i++) {
		TypeData value = values.get(i);
		LightweightTypeReference reference = value.getActualType().getUpperBoundSubstitute();
		int flags = value.getConformanceFlags();
		if (reference.isPrimitiveVoid()) {
			if ((flags & ConformanceFlags.EXPLICIT_VOID_RETURN) != 0) {
				mergeData.references.clear();
				mergeData.references.add(reference);
				mergeData.mergedFlags = flags;
				mergeData.expectation = value.getExpectation();
				mergeData.voidSeen = false;
				return;
			}
			mergeData.voidSeen = true;
		} else {
			mergeData.references.add(reference);
		}
		mergeData.allNoImplicitReturn = mergeData.allNoImplicitReturn && (flags & ConformanceFlags.NO_IMPLICIT_RETURN) != 0; 
		mergeData.allThrownException = mergeData.allThrownException && (flags & ConformanceFlags.THROWN_EXCEPTION) != 0;
		if (!reference.isUnknown()) {
			mergeData.mergedFlags |= flags;
		}
		if (mergeData.expectation == null) {
			mergeData.expectation = value.getExpectation();
		} else if (mergeData.expectation.getExpectedType() == null) {
			ITypeExpectation knownExpectation = value.getExpectation();
			if (knownExpectation.getExpectedType() != null) {
				mergeData.expectation = knownExpectation;
			}
		}
	}
	if (mergeData.mergedFlags == ConformanceFlags.MERGED) {
		mergeData.mergedFlags |= ConformanceFlags.CHECKED_SUCCESS;
	}
	if (!mergeData.allNoImplicitReturn) {
		mergeData.mergedFlags &= ~ConformanceFlags.NO_IMPLICIT_RETURN;
	}
	if (!mergeData.allThrownException) {
		mergeData.mergedFlags &= ~ConformanceFlags.THROWN_EXCEPTION;
	}
	if ((mergeData.mergedFlags & (ConformanceFlags.CHECKED | ConformanceFlags.UNCHECKED)) == (ConformanceFlags.CHECKED | ConformanceFlags.UNCHECKED)) {
		mergeData.mergedFlags &= ~(ConformanceFlags.CHECKED | ConformanceFlags.CHECK_RESULTS);
	}
	if ((mergeData.mergedFlags & ConformanceFlags.UNCHECKED) != 0) {
		mergeData.mergedFlags &= ~ConformanceFlags.SEALED;
	}
}
 
Example 20
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;
}