Java Code Examples for org.eclipse.xtext.xbase.XReturnExpression#getExpression()

The following examples show how to use org.eclipse.xtext.xbase.XReturnExpression#getExpression() . 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: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Check
public void checkReturn(XReturnExpression expr) {
	XExpression returnedExpression = expr.getExpression();
	IResolvedTypes resolvedTypes = typeResolver.resolveTypes(expr);
	LightweightTypeReference expectedReturnType = resolvedTypes.getExpectedReturnType(expr);
	if (expectedReturnType == null) {
		return;
	}
	if (expectedReturnType.isPrimitiveVoid()) {
		if (returnedExpression != null)
			error("Void functions cannot return a value.", expr, null,
					ValidationMessageAcceptor.INSIGNIFICANT_INDEX, INVALID_RETURN);
	} else {
		if (returnedExpression == null)
			error("The function must return a result of type " + expectedReturnType.getHumanReadableName() + ".", expr, null,
					ValidationMessageAcceptor.INSIGNIFICANT_INDEX, INVALID_RETURN);
		else {
			LightweightTypeReference expressionType = getActualType(returnedExpression);
			if (expressionType.isPrimitiveVoid()) {
				error("Incompatible types. Expected " + getNameOfTypes(expectedReturnType) + " but was "
						+ canonicalName(expressionType), returnedExpression, null,
						ValidationMessageAcceptor.INSIGNIFICANT_INDEX, INCOMPATIBLE_TYPES);
			}
		}

	}
}
 
Example 2
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _computeTypes(XReturnExpression object, ITypeComputationState state) {
	XExpression returnValue = object.getExpression();
	ITypeComputationState expressionState = state.withReturnExpectation();

	checkValidReturn(object, state);
	
	LightweightTypeReference primitiveVoid = getPrimitiveVoid(state);
	if (returnValue != null) {
		checkValidReturnExpression(returnValue, expressionState);
		state.acceptActualType(primitiveVoid, ConformanceFlags.NO_IMPLICIT_RETURN);
	} else {
		state.acceptActualType(primitiveVoid, ConformanceFlags.EXPLICIT_VOID_RETURN);
		state.acceptActualType(primitiveVoid, ConformanceFlags.NO_IMPLICIT_RETURN);
	}
}
 
Example 3
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @param isReferenced unused in this context but necessary for dispatch signature 
 */
protected void _toJavaStatement(XReturnExpression expr, ITreeAppendable b, boolean isReferenced) {
	if (expr.getExpression()!=null) {
		internalToJavaStatement(expr.getExpression(), b, true);
		b.newLine().append("return ");
		LightweightTypeReference returnTypeToCompile = findRealReturnType(expr);
		internalToConvertedExpression(expr.getExpression(), b, returnTypeToCompile);
		b.append(";");
	} else {
		b.newLine().append("return;");
	}
}
 
Example 4
Source File: TypeProviderTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testReturnTypeInConstructor_02() throws Exception {
	XtendConstructor constructor = constructor(
			"new() {\n" + 
			"	return ''.toString\n" + 
			"}\n", false);
	XBlockExpression body = (XBlockExpression) constructor.getExpression();
	assertEquals("void", getExpectedType(body).getIdentifier());
	assertEquals("void", getExpectedReturnType(body).getIdentifier());
	XReturnExpression returnExpression = (XReturnExpression) body.getExpressions().get(0);
	XMemberFeatureCall toString = (XMemberFeatureCall) returnExpression.getExpression();
	assertEquals("void", getExpectedType(toString).getIdentifier());
	assertEquals("void", getExpectedReturnType(toString).getIdentifier());
}
 
Example 5
Source File: CheckJavaValidator.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Checks that return expressions do not occur in check implementations.
 *
 * @param expression
 *          the expression
 */
@Check
public void checkReturnExpressions(final XReturnExpression expression) {
  if (expression.getExpression() != null) {
    error(Messages.CheckJavaValidator_NO_RETURN_IN_CHECK_IMPL, XbasePackage.Literals.XRETURN_EXPRESSION__EXPRESSION, IssueCodes.RETURN_IN_IMPL);
  }
}