org.eclipse.xtext.xbase.typesystem.computation.ITypeComputationState Java Examples

The following examples show how to use org.eclipse.xtext.xbase.typesystem.computation.ITypeComputationState. 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: XtendTypeComputer.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void _computeTypes(RichStringForLoop object, ITypeComputationState state) {
	LightweightTypeReference charSequence = getRawTypeForName(CharSequence.class, state);
	ITypeComputationState eachState = state.withExpectation(charSequence);
	JvmFormalParameter parameter = object.getDeclaredParam();
	if (parameter != null) {
		LightweightTypeReference parameterType = computeForLoopParameterType(object, state);
		eachState = eachState.assignType(parameter, parameterType);
	}
	eachState.computeTypes(object.getEachExpression());
	
	state.withNonVoidExpectation().computeTypes(object.getBefore());
	state.withNonVoidExpectation().computeTypes(object.getSeparator());
	state.withNonVoidExpectation().computeTypes(object.getAfter());
	
	LightweightTypeReference primitiveVoid = getPrimitiveVoid(state);
	state.acceptActualType(primitiveVoid);
	
	state.acceptActualType(charSequence);
}
 
Example #2
Source File: TypeComputationStateTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void computeTypes(XExpression expression, ITypeComputationState state) {
	try {
		assertTrue("state is instanceof ExpressionTypeComputationState", (state instanceof ExpressionTypeComputationState));
		LightweightTypeReference expectedType = getFirst(state.getExpectations(), null).getExpectedType();
		if (expression instanceof XNullLiteral) {
			ExpressionTypeComputationState casted = ((ExpressionTypeComputationState) state);
			ResolvedTypes resolution = reflectExtensions.get(casted, "resolvedTypes");
			ResolvedTypes parentResolution = reflectExtensions
					.get(reflectExtensions.<ExpressionTypeComputationState> get(casted, "parent"), "resolvedTypes");
			assertNull(parentResolution.getActualType(((XExpression) ((XNullLiteral) expression).eContainer())));
			state.acceptActualType(expectedType);
			assertNull(parentResolution.getActualType(expression));
			assertEquals(expectedType.getIdentifier(), resolution.getActualType(expression).getIdentifier());
			assertNull(parentResolution.getActualType(((XExpression) ((XNullLiteral) expression).eContainer())));
		} else {
			assertTrue((expression instanceof XBlockExpression));
			XNullLiteral nullLiteral = ((XNullLiteral) getFirst(expression.eContents(), null));
			state.computeTypes(nullLiteral);
			ResolvedTypes res = reflectExtensions.get(state, "resolvedTypes");
			assertEquals(expectedType.getIdentifier(), res.getActualType(nullLiteral).getIdentifier());
		}
	} catch (IllegalAccessException | SecurityException | NoSuchFieldException | IllegalArgumentException e) {
		throw Exceptions.sneakyThrow(e);
	}
}
 
Example #3
Source File: ResolvedFeature.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void resolveArgumentType(XExpression argument, /* @Nullable */ LightweightTypeReference declaredType, ITypeComputationState argumentState) {
	if (argument == getSyntacticReceiver()) {
		LightweightTypeReference receiverType = getSyntacticReceiverType();
		if (receiverType == null) {
			throw new IllegalStateException("Cannot determine the receiver's type");
		}
		resolveKnownArgumentType(argument, receiverType, declaredType, argumentState);
	} else if (argument == getImplicitFirstArgument()) {
		LightweightTypeReference argumentType = getImplicitFirstArgumentType();
		if (argumentType == null) {
			throw new IllegalStateException("Cannot determine the implicit argument's type");
		}
		resolveKnownArgumentType(argument, argumentType, declaredType, argumentState);
	} else {
		super.resolveArgumentType(argument, declaredType, argumentState);
	}
}
 
Example #4
Source File: XtendTypeComputer.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void computeTypes(XExpression expression, ITypeComputationState state) {
	if (expression instanceof AnonymousClass) {
		_computeTypes((AnonymousClass) expression, state);
	} else if (expression instanceof RichString) {
		_computeTypes((RichString)expression, state);
	} else if (expression instanceof RichStringForLoop) {
		_computeTypes((RichStringForLoop)expression, state);
	} else if (expression instanceof RichStringIf) {
		_computeTypes((RichStringIf)expression, state);
	} else if (expression instanceof RichStringLiteral) {
		_computeTypes((RichStringLiteral)expression, state);
	} else {
		super.computeTypes(expression, state);
	}
}
 
Example #5
Source File: FeatureLinkingCandidate.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void resolveArgumentType(XExpression argument, /* @Nullable */ LightweightTypeReference declaredType, ITypeComputationState argumentState) {
	if (argument == getSyntacticReceiverIfPossibleArgument()) {
		LightweightTypeReference receiverType = getSyntacticReceiverType();
		if (receiverType != null) {
			resolveKnownArgumentType(argument, receiverType, declaredType, argumentState);
		}
	} else if (argument == getImplicitFirstArgument()) {
		LightweightTypeReference argumentType = getImplicitFirstArgumentType();
		if (argumentType != null) {
			resolveKnownArgumentType(argument, argumentType, declaredType, argumentState);
		}
	} else {
		super.resolveArgumentType(argument, declaredType, argumentState);
	}
}
 
Example #6
Source File: XtendTypeComputer.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void _computeTypes(RichStringIf object, ITypeComputationState state) {
	LightweightTypeReference charSequence = getRawTypeForName(CharSequence.class, state);
	LightweightTypeReference booleanType = getRawTypeForName(Boolean.TYPE, state);
	
	ITypeComputationState conditionExpectation = state.withExpectation(booleanType);
	XExpression condition = object.getIf();
	conditionExpectation.computeTypes(condition);
	XExpression thenExpression = object.getThen();
	ITypeComputationState thenState = reassignCheckedType(condition, thenExpression, state);
	thenState.withExpectation(charSequence).computeTypes(thenExpression);
	for(RichStringElseIf elseIf: object.getElseIfs()) {
		state.withExpectation(booleanType).computeTypes(elseIf.getIf());
		ITypeComputationState elseState = reassignCheckedType(elseIf.getIf(), elseIf.getThen(), state);
		elseState.withExpectation(charSequence).computeTypes(elseIf.getThen());
	}
	state.withExpectation(charSequence).computeTypes(object.getElse());
	state.acceptActualType(charSequence);
}
 
Example #7
Source File: ScriptTypeComputer.java    From openhab-core with Eclipse Public License 2.0 6 votes vote down vote up
protected void _computeTypes(final QuantityLiteral assignment, ITypeComputationState state) {
    LightweightTypeReference qt = null;
    for (ITypeExpectation exp : state.getExpectations()) {
        if (exp.getExpectedType() == null) {
            continue;
        }

        if (exp.getExpectedType().isType(Number.class)) {
            qt = getRawTypeForName(Number.class, state);
        }
        if (exp.getExpectedType().isType(State.class)) {
            qt = getRawTypeForName(Number.class, state);
        }
        if (exp.getExpectedType().isType(Command.class)) {
            qt = getRawTypeForName(Number.class, state);
        }
    }
    if (qt == null) {
        qt = getRawTypeForName(QuantityType.class, state);
    }
    state.acceptActualType(qt);
}
 
Example #8
Source File: SARLTypeComputer.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Override
public void computeTypes(XExpression expression, ITypeComputationState state) {
	if (expression instanceof SarlBreakExpression) {
		_computeTypes((SarlBreakExpression) expression, state);
	} else if (expression instanceof SarlContinueExpression) {
		_computeTypes((SarlContinueExpression) expression, state);
	} else if (expression instanceof SarlAssertExpression) {
		_computeTypes((SarlAssertExpression) expression, state);
	} else if (expression instanceof SarlCastedExpression) {
		_computeTypes((SarlCastedExpression) expression, state);
	} else {
		try {
			super.computeTypes(expression, state);
		} catch (Throwable exception) {
			final Throwable cause = Throwables.getRootCause(exception);
			state.addDiagnostic(new EObjectDiagnosticImpl(
					Severity.ERROR,
					IssueCodes.INTERNAL_ERROR,
					cause.getLocalizedMessage(),
					expression,
					null,
					-1,
					null));
		}
	}
}
 
Example #9
Source File: ScriptTypeComputer.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
protected void _computeTypes(final QuantityLiteral assignment, ITypeComputationState state) {
    LightweightTypeReference qt = null;
    for (ITypeExpectation exp : state.getExpectations()) {
        if (exp.getExpectedType() == null) {
            continue;
        }

        if (exp.getExpectedType().isType(Number.class)) {
            qt = getRawTypeForName(Number.class, state);
        }
        if (exp.getExpectedType().isType(State.class)) {
            qt = getRawTypeForName(Number.class, state);
        }
        if (exp.getExpectedType().isType(Command.class)) {
            qt = getRawTypeForName(Number.class, state);
        }
    }
    if (qt == null) {
        qt = getRawTypeForName(QuantityType.class, state);
    }
    state.acceptActualType(qt);
}
 
Example #10
Source File: LogicalContainerAwareReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void addExtensionProviders(ITypeComputationState state, List<JvmFormalParameter> parameters) {
	List<JvmFormalParameter> extensionProviders = null;
	for(JvmFormalParameter parameter: parameters) {
		if (isExtensionProvider(parameter)) {
			if (extensionProviders == null)
				extensionProviders = Lists.newLinkedList();
			extensionProviders.add(parameter);
		}
	}
	if (extensionProviders != null)
		state.addExtensionsToCurrentScope(extensionProviders);
}
 
Example #11
Source File: ForwardingTypeComputationState.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ITypeAssigner assignTypes() {
	final ITypeAssigner result = delegate.assignTypes();
	return new ITypeAssigner() {

		@Override
		public void assignType(JvmIdentifiableElement element, /* @Nullable */ LightweightTypeReference actualType) {
			result.assignType(element, actualType);
		}

		@Override
		public ITypeComputationState getForkedState() {
			return newForwardingTypeComputationState(result.getForkedState());
		}

		@Override
		public ITypeReferenceOwner getReferenceOwner() {
			return result.getReferenceOwner();
		}

		@Override
		public LightweightTypeReference toLightweightTypeReference(JvmTypeReference reference) {
			return result.toLightweightTypeReference(reference);
		}
		
	};
}
 
Example #12
Source File: LogicalContainerAwareReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void capture(ITypeComputationState state) {
	this.capturedState = ((AbstractTypeComputationState) state).getFeatureScopeSession();
	IFeatureScopeSession nestedSession = typeResolver.addThisAndSuper(capturedState, state.getReferenceOwner(), localClass, getEquivalent(), true);
	
	ResolvedTypes capturedResolvedTypes = captureResolvedTypes(state);
	typeResolver.doPrepare(capturedResolvedTypes, nestedSession, localClass, resolvedTypesByContext);
}
 
Example #13
Source File: CompoundTypeComputationState.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ITypeComputationState withoutExpectation() {
	AbstractTypeComputationState[] result = new AbstractTypeComputationState[components.length];
	for (int i = 0; i < components.length; i++) {
		result[i] = components[i].withoutExpectation();
	}
	return new CompoundTypeComputationState(owner, result);
}
 
Example #14
Source File: FeatureLinkingCandidate.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void resolveKnownArgumentType(XExpression argument, LightweightTypeReference knownType,
		/* @Nullable */ LightweightTypeReference declaredType, ITypeComputationState argumentState) {
	if (!(argumentState instanceof AbstractLinkingCandidate.ArgumentTypeComputationState))
		throw new IllegalArgumentException("argumentState was " + argumentState);
	AbstractLinkingCandidate<?>.ArgumentTypeComputationState castedArgumentState = (AbstractLinkingCandidate<?>.ArgumentTypeComputationState) argumentState;
	ResolvedTypes resolvedTypes = getState().getResolvedTypes();
	LightweightTypeReference copiedDeclaredType = declaredType != null ? declaredType.copyInto(resolvedTypes.getReferenceOwner()) : null;
	TypeExpectation expectation = new TypeExpectation(copiedDeclaredType, castedArgumentState, false);
	LightweightTypeReference copiedReceiverType = knownType.copyInto(resolvedTypes.getReferenceOwner());
	// TODO should we use the result of #acceptType?
	int defaultFlags = castedArgumentState.getDefaultFlags();
	resolvedTypes.acceptType(argument, expectation, copiedReceiverType, false, ConformanceFlags.UNCHECKED | defaultFlags);
	if (copiedDeclaredType != null)
		resolveAgainstActualType(copiedDeclaredType, copiedReceiverType, castedArgumentState);
}
 
Example #15
Source File: LocalVariableCapturer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public static void captureLocalVariables(JvmTypeReference typeReference, ITypeComputationState state) {
	LocalVariableCapturer localClassSupertype = findLocalClassSupertype(typeReference);
	if (localClassSupertype != null) {
		if (!localClassSupertype.captured) {
			localClassSupertype.captured = true;
			localClassSupertype.capture(state);
		}
		return;
	}
	throw new IllegalStateException("Unexpected typeReference: " + typeReference);
}
 
Example #16
Source File: ResolvedFeature.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void resolveKnownArgumentType(XExpression argument, LightweightTypeReference knownType,
		/* @Nullable */ LightweightTypeReference declaredType, ITypeComputationState argumentState) {
	if (!(argumentState instanceof AbstractTypeComputationState))
		throw new IllegalArgumentException("argumentState was " + argumentState);
	AbstractTypeComputationState castedArgumentState = (AbstractTypeComputationState) argumentState;
	ResolvedTypes resolvedTypes = getState().getResolvedTypes();
	LightweightTypeReference copiedDeclaredType = declaredType != null ? declaredType.copyInto(resolvedTypes.getReferenceOwner()) : null;
	TypeExpectation expectation = new TypeExpectation(copiedDeclaredType, castedArgumentState, false);
	LightweightTypeReference copiedReceiverType = knownType.copyInto(resolvedTypes.getReferenceOwner());
	// TODO should we use the result of #acceptType?
	resolvedTypes.acceptType(argument, expectation, copiedReceiverType, false, ConformanceFlags.UNCHECKED);
	if (copiedDeclaredType != null)
		resolveAgainstActualType(copiedDeclaredType, copiedReceiverType, castedArgumentState);
}
 
Example #17
Source File: XbaseWithAnnotationsTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void computeTypes(XExpression expression, ITypeComputationState state) {
	if (expression instanceof XAnnotation) {
		_computeTypes((XAnnotation)expression, state);
	} else {
		super.computeTypes(expression, state);
	}
}
 
Example #18
Source File: XbaseWithAnnotationsTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void computeChildTypesForUnknownAnnotation(XAnnotation object, ITypeComputationState state) {
	XExpression expression = object.getValue();
	if (expression != null)
		state.withNonVoidExpectation().computeTypes(expression);
	else {
		List<XAnnotationElementValuePair> valuePairs = object.getElementValuePairs();
		for(XAnnotationElementValuePair pair: valuePairs) {
			computeTypes(object, pair.getElement(), pair.getValue(), state);
		}
	}
}
 
Example #19
Source File: XbaseWithAnnotationsTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private ITypeComputationState addEnumImportsIfNecessary(ITypeComputationState state, LightweightTypeReference expectedType, JvmType enumTypeCandidate) {
	ITypeComputationState expectationState = state.withExpectation(expectedType);
	if (enumTypeCandidate != null && enumTypeCandidate.eClass() == TypesPackage.Literals.JVM_ENUMERATION_TYPE) {
		expectationState.addImports(new EnumLiteralImporter((JvmDeclaredType) enumTypeCandidate));
	}
	return expectationState;
}
 
Example #20
Source File: ExpectationTestingTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void computeTypes(XExpression expression, ITypeComputationState state) {
	if (predicate.apply(expression).booleanValue()) {
		test.recordExpectation(expression, state);
	}
	super.computeTypes(expression, state);
}
 
Example #21
Source File: AbstractExpectationTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public void recordExpectation(final XExpression expression, final ITypeComputationState state) {
  List<? extends ITypeExpectation> _expectations = state.getExpectations();
  Iterables.<ITypeExpectation>addAll(this.finalExpectations, _expectations);
  final Function1<ITypeExpectation, String> _function = (ITypeExpectation it) -> {
    LightweightTypeReference _expectedType = it.getExpectedType();
    String _simpleName = null;
    if (_expectedType!=null) {
      _simpleName=_expectedType.getSimpleName();
    }
    return _simpleName;
  };
  List<String> _map = ListExtensions.map(state.getExpectations(), _function);
  Iterables.<String>addAll(this.expectations, _map);
  this.expressions.add(expression);
}
 
Example #22
Source File: ExtensionAwareClosureTypeComputer.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected ClosureWithExpectationHelper createClosureWithExpectationHelper(JvmOperation operation) {
	return new ClosureWithExpectationHelper(getClosure(), operation, getExpectation(), getState()) {
		@Override
		protected ITypeComputationState assignParameters(ITypeAssigner typeAssigner) {
			ITypeComputationState result = super.assignParameters(typeAssigner);
			addExtensions(result);
			return result;
		}
	};
}
 
Example #23
Source File: ExtensionAwareClosureTypeComputer.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected UnknownClosureTypeHelper createUnknownClosureTypeHelper() {
	return new UnknownClosureTypeHelper(getClosure(), getExpectation(), getState()) {
		@Override
		protected ITypeComputationState assignParameters(ITypeAssigner typeAssigner) {
			ITypeComputationState result = super.assignParameters(typeAssigner);
			addExtensions(result);
			return result;
		}
	};
}
 
Example #24
Source File: ExtensionAwareClosureTypeComputer.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected ClosureWithoutExpectationHelper createClosureWithoutExpectationHelper() {
	return new ClosureWithoutExpectationHelper(getClosure(), getExpectation(), getState()) {
		@Override
		protected ITypeComputationState assignParameters(ITypeAssigner typeAssigner,
				FunctionTypeReference incompleteClosureType) {
			ITypeComputationState result = super.assignParameters(typeAssigner, incompleteClosureType);
			addExtensions(result);
			return result;
		}
	};
}
 
Example #25
Source File: ExtensionAwareClosureTypeComputer.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void addExtensions(ITypeComputationState state) {
	List<JvmFormalParameter> extensionProviders = null;
	for(JvmFormalParameter parameter: getClosure().getDeclaredFormalParameters()) {
		if (parameter instanceof XtendFormalParameter && ((XtendFormalParameter) parameter).isExtension()) {
			if (extensionProviders == null)
				extensionProviders = Lists.newLinkedList();
			extensionProviders.add(parameter);
		}
	}
	if (extensionProviders != null)
		state.addExtensionsToCurrentScope(extensionProviders);
}
 
Example #26
Source File: XtendTypeComputer.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void _computeTypes(AnonymousClass anonymousClass, ITypeComputationState state) {
	JvmGenericType type = associations.getInferredType(anonymousClass);
	if (type != null) {
		// should never be null but the associations don't promise anything
		LocalVariableCapturer.captureLocalVariables(type, state);
	}
	state.computeTypes(anonymousClass.getConstructorCall());
}
 
Example #27
Source File: XtendTypeComputer.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void addLocalToCurrentScope(XVariableDeclaration localVariable, ITypeComputationState state) {
	super.addLocalToCurrentScope(localVariable, state);
	if (localVariable instanceof XtendVariableDeclaration && ((XtendVariableDeclaration) localVariable).isExtension()) {
		state.addExtensionToCurrentScope(localVariable);
		state.rewriteScope(localVariable);
	}
}
 
Example #28
Source File: XtendTypeComputer.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected ITypeComputationState assignType(JvmFormalParameter param, /* @Nullable */ LightweightTypeReference type,
		ITypeComputationState state) {
	ITypeComputationState result = super.assignType(param, type, state);
	if (param instanceof XtendFormalParameter && ((XtendFormalParameter) param).isExtension()) {
		result.addExtensionToCurrentScope(param);
	}
	return result;
}
 
Example #29
Source File: ScriptTypeComputer.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void computeTypes(XExpression expression, ITypeComputationState state) {
    if (expression instanceof QuantityLiteral) {
        _computeTypes((QuantityLiteral) expression, state);
    } else {
        super.computeTypes(expression, state);
    }
}
 
Example #30
Source File: ScriptTypeComputer.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void computeTypes(XExpression expression, ITypeComputationState state) {
    if (expression instanceof QuantityLiteral) {
        _computeTypes((QuantityLiteral) expression, state);
    } else {
        super.computeTypes(expression, state);
    }
}