org.eclipse.xtext.xbase.XWhileExpression Java Examples

The following examples show how to use org.eclipse.xtext.xbase.XWhileExpression. 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: EarlyExitValidator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Check
public void checkDeadCode(XWhileExpression loop) {
	XExpression predicate = loop.getPredicate();
	if (!earlyExitComputer.isEarlyExit(predicate)) {
		Optional<BooleanResult> result = getBooleanResult(predicate);
		if (result.isPresent()) {
			BooleanResult booleanResult = result.get();
			markConstantBooleanCondition(predicate, booleanResult, true);
			if (booleanResult.isCompileTimeConstant() && !booleanResult.getValue().or(Boolean.TRUE)) {
				markAsDeadCode(loop.getBody());
			}
		}
	} else {
		markAsDeadCode(loop.getBody());
	}
}
 
Example #2
Source File: DefaultEarlyExitComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected Collection<IEarlyExitComputer.ExitPoint> _exitPoints(final XWhileExpression expression) {
  Collection<IEarlyExitComputer.ExitPoint> exitPoints = this.getExitPoints(expression.getPredicate());
  boolean _isNotEmpty = this.isNotEmpty(exitPoints);
  if (_isNotEmpty) {
    return exitPoints;
  }
  boolean _isBooleanConstant = this.isBooleanConstant(expression.getPredicate(), true);
  if (_isBooleanConstant) {
    exitPoints = this.getExitPoints(expression.getBody());
    boolean _isNotEmpty_1 = this.isNotEmpty(exitPoints);
    if (_isNotEmpty_1) {
      return exitPoints;
    }
    IEarlyExitComputer.ExitPoint _exitPoint = new IEarlyExitComputer.ExitPoint(expression, false);
    return Collections.<IEarlyExitComputer.ExitPoint>singletonList(_exitPoint);
  }
  return Collections.<IEarlyExitComputer.ExitPoint>emptyList();
}
 
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(XWhileExpression expr, ITreeAppendable b, boolean isReferenced) {
	boolean needsStatement = !canCompileToJavaExpression(expr.getPredicate(), b);
	String varName = null;
	if (needsStatement) {
		internalToJavaStatement(expr.getPredicate(), b, true);
		varName = b.declareSyntheticVariable(expr, "_while");
		b.newLine().append("boolean ").append(varName).append(" = ");
		internalToJavaExpression(expr.getPredicate(), b);
		b.append(";");
	}
	b.newLine().append("while (");
	if (needsStatement) {
		b.append(varName);
	} else {
		internalToJavaExpression(expr.getPredicate(), b);
	}
	b.append(") {").increaseIndentation();
	b.openPseudoScope();
	internalToJavaStatement(expr.getBody(), b, false);
	if (needsStatement && !isEarlyExit(expr.getBody())) {
		internalToJavaStatement(expr.getPredicate(), b, true);
		b.newLine();
		b.append(varName).append(" = ");
		internalToJavaExpression(expr.getPredicate(), b);
		b.append(";");
	}
	b.closeScope();
	b.decreaseIndentation().newLine().append("}");
}
 
Example #4
Source File: XbaseInterpreter.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected Object _doEvaluate(XWhileExpression whileLoop, IEvaluationContext context, CancelIndicator indicator) {
	Object condition = internalEvaluate(whileLoop.getPredicate(), context, indicator);
	while (Boolean.TRUE.equals(condition)) {
		internalEvaluate(whileLoop.getBody(), context, indicator);
		condition = internalEvaluate(whileLoop.getPredicate(), context, indicator);
	}
	return null;
}
 
Example #5
Source File: DefaultEarlyExitComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected Collection<IEarlyExitComputer.ExitPoint> exitPoints(final XExpression expression) {
  if (expression instanceof XDoWhileExpression) {
    return _exitPoints((XDoWhileExpression)expression);
  } else if (expression instanceof XWhileExpression) {
    return _exitPoints((XWhileExpression)expression);
  } else if (expression instanceof XAbstractFeatureCall) {
    return _exitPoints((XAbstractFeatureCall)expression);
  } else if (expression instanceof XBasicForLoopExpression) {
    return _exitPoints((XBasicForLoopExpression)expression);
  } else if (expression instanceof XBlockExpression) {
    return _exitPoints((XBlockExpression)expression);
  } else if (expression instanceof XConstructorCall) {
    return _exitPoints((XConstructorCall)expression);
  } else if (expression instanceof XForLoopExpression) {
    return _exitPoints((XForLoopExpression)expression);
  } else if (expression instanceof XIfExpression) {
    return _exitPoints((XIfExpression)expression);
  } else if (expression instanceof XReturnExpression) {
    return _exitPoints((XReturnExpression)expression);
  } else if (expression instanceof XSwitchExpression) {
    return _exitPoints((XSwitchExpression)expression);
  } else if (expression instanceof XSynchronizedExpression) {
    return _exitPoints((XSynchronizedExpression)expression);
  } else if (expression instanceof XThrowExpression) {
    return _exitPoints((XThrowExpression)expression);
  } else if (expression instanceof XTryCatchFinallyExpression) {
    return _exitPoints((XTryCatchFinallyExpression)expression);
  } else if (expression instanceof XVariableDeclaration) {
    return _exitPoints((XVariableDeclaration)expression);
  } else if (expression != null) {
    return _exitPoints(expression);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(expression).toString());
  }
}
 
Example #6
Source File: ValidationTests.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testUnreachableCode_01() throws Exception {
	XBlockExpression expression = (XBlockExpression) expression(
			"{" +
			"	while (false) {" +
			"      throw new Exception() " +
			"   }" +
			"   null" +
			"}");
	XWhileExpression whileLoop = (XWhileExpression) expression.getExpressions().get(0);
	XThrowExpression throwExpression = (XThrowExpression) ((XBlockExpression) whileLoop.getBody()).getExpressions().get(0);
	helper.assertError(throwExpression, XbasePackage.Literals.XTHROW_EXPRESSION, UNREACHABLE_CODE);
}
 
Example #7
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Generate the given object.
 *
 * @param whileLoop the while-loop.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the last statement in the loop or {@code null}.
 */
protected XExpression _generate(XWhileExpression whileLoop, IAppendable it, IExtraLanguageGeneratorContext context) {
	it.append("while "); //$NON-NLS-1$
	generate(whileLoop.getPredicate(), it, context);
	it.append(":"); //$NON-NLS-1$
	it.increaseIndentation().newLine();
	final XExpression last = generate(whileLoop.getBody(), it, context);
	it.decreaseIndentation();
	return last;
}
 
Example #8
Source File: AbstractXbaseSemanticSequencer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Deprecated
protected void sequence_XWhileExpression(EObject context, XWhileExpression semanticObject) {
	sequence_XWhileExpression(createContext(context, semanticObject), semanticObject);
}
 
Example #9
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void computeTypes(XExpression expression, ITypeComputationState state) {
	if (expression instanceof XAssignment) {
		_computeTypes((XAssignment)expression, state);
	} else if (expression instanceof XAbstractFeatureCall) {
		_computeTypes((XAbstractFeatureCall)expression, state);
	} else if (expression instanceof XDoWhileExpression) {
		_computeTypes((XDoWhileExpression)expression, state);
	} else if (expression instanceof XWhileExpression) {
		_computeTypes((XWhileExpression)expression, state);
	} else if (expression instanceof XBlockExpression) {
		_computeTypes((XBlockExpression)expression, state);
	} else if (expression instanceof XBooleanLiteral) {
		_computeTypes((XBooleanLiteral)expression, state);
	} else if (expression instanceof XCastedExpression) {
		_computeTypes((XCastedExpression)expression, state);
	} else if (expression instanceof XClosure) {
		_computeTypes((XClosure)expression, state);
	} else if (expression instanceof XConstructorCall) {
		_computeTypes((XConstructorCall)expression, state);
	} else if (expression instanceof XForLoopExpression) {
		_computeTypes((XForLoopExpression)expression, state);
	} else if (expression instanceof XBasicForLoopExpression) {
		_computeTypes((XBasicForLoopExpression)expression, state);
	} else if (expression instanceof XIfExpression) {
		_computeTypes((XIfExpression)expression, state);
	} else if (expression instanceof XInstanceOfExpression) {
		_computeTypes((XInstanceOfExpression)expression, state);
	} else if (expression instanceof XNumberLiteral) {
		_computeTypes((XNumberLiteral)expression, state);
	} else if (expression instanceof XNullLiteral) {
		_computeTypes((XNullLiteral)expression, state);
	} else if (expression instanceof XReturnExpression) {
		_computeTypes((XReturnExpression)expression, state);
	} else if (expression instanceof XStringLiteral) {
		_computeTypes((XStringLiteral)expression, state);
	} else if (expression instanceof XSwitchExpression) {
		_computeTypes((XSwitchExpression)expression, state);
	} else if (expression instanceof XThrowExpression) {
		_computeTypes((XThrowExpression)expression, state);
	} else if (expression instanceof XTryCatchFinallyExpression) {
		_computeTypes((XTryCatchFinallyExpression)expression, state);
	} else if (expression instanceof XTypeLiteral) {
		_computeTypes((XTypeLiteral)expression, state);
	} else if (expression instanceof XVariableDeclaration) {
		_computeTypes((XVariableDeclaration)expression, state);
	} else if (expression instanceof XListLiteral) {
		_computeTypes((XListLiteral)expression, state);
	} else if (expression instanceof XSetLiteral) {
		_computeTypes((XSetLiteral)expression, state);
	} else if (expression instanceof XSynchronizedExpression) {
		_computeTypes((XSynchronizedExpression)expression, state);
	} else {
		throw new UnsupportedOperationException("Missing type computation for expression type: " + expression.eClass().getName() + " / " + state);
	}
}
 
Example #10
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void _computeTypes(XWhileExpression object, ITypeComputationState state) {
	computeWhileLoopBody(object, state, true);
	
	LightweightTypeReference primitiveVoid = getPrimitiveVoid(state);
	state.acceptActualType(primitiveVoid);
}
 
Example #11
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected void doInternalToJavaStatement(XExpression obj, ITreeAppendable appendable, boolean isReferenced) {
	if (obj instanceof XBlockExpression) {
		_toJavaStatement((XBlockExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XCastedExpression) {
		_toJavaStatement((XCastedExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XClosure) {
		_toJavaStatement((XClosure) obj, appendable, isReferenced);
	} else if (obj instanceof XConstructorCall) {
		_toJavaStatement((XConstructorCall) obj, appendable, isReferenced);
	} else if (obj instanceof XDoWhileExpression) {
		_toJavaStatement((XDoWhileExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XForLoopExpression) {
		_toJavaStatement((XForLoopExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XBasicForLoopExpression) {
		_toJavaStatement((XBasicForLoopExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XIfExpression) {
		_toJavaStatement((XIfExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XInstanceOfExpression) {
		_toJavaStatement((XInstanceOfExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XReturnExpression) {
		_toJavaStatement((XReturnExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XSwitchExpression) {
		_toJavaStatement((XSwitchExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XThrowExpression) {
		_toJavaStatement((XThrowExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XTryCatchFinallyExpression) {
		_toJavaStatement((XTryCatchFinallyExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XVariableDeclaration) {
		_toJavaStatement((XVariableDeclaration) obj, appendable, isReferenced);
	} else if (obj instanceof XWhileExpression) {
		_toJavaStatement((XWhileExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XListLiteral) {
		_toJavaStatement((XListLiteral) obj, appendable, isReferenced);
	} else if (obj instanceof XSetLiteral) {
		_toJavaStatement((XSetLiteral) obj, appendable, isReferenced);
	} else if (obj instanceof XSynchronizedExpression) {
		_toJavaStatement((XSynchronizedExpression) obj, appendable, isReferenced);
	} else {
		super.doInternalToJavaStatement(obj, appendable, isReferenced);
	}
}
 
Example #12
Source File: XbaseInterpreter.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * don't call this directly. Always call evaluate() internalEvaluate()
 */
protected Object doEvaluate(XExpression expression, IEvaluationContext context, CancelIndicator indicator) {
	if (expression instanceof XAssignment) {
      return _doEvaluate((XAssignment)expression, context, indicator);
    } else if (expression instanceof XDoWhileExpression) {
      return _doEvaluate((XDoWhileExpression)expression, context, indicator);
    } else if (expression instanceof XMemberFeatureCall) {
      return _doEvaluate((XMemberFeatureCall)expression, context, indicator);
    } else if (expression instanceof XWhileExpression) {
      return _doEvaluate((XWhileExpression)expression, context, indicator);
    } else if (expression instanceof XFeatureCall) {
    	return _doEvaluate((XFeatureCall)expression, context, indicator);
    } else if (expression instanceof XAbstractFeatureCall) {
    	return _doEvaluate((XAbstractFeatureCall)expression, context, indicator);
    } else if (expression instanceof XBlockExpression) {
      return _doEvaluate((XBlockExpression)expression, context, indicator);
    } else if (expression instanceof XSynchronizedExpression) {
	  return _doEvaluate((XSynchronizedExpression)expression, context, indicator);
	} else if (expression instanceof XBooleanLiteral) {
      return _doEvaluate((XBooleanLiteral)expression, context, indicator);
    } else if (expression instanceof XCastedExpression) {
      return _doEvaluate((XCastedExpression)expression, context, indicator);
    } else if (expression instanceof XClosure) {
      return _doEvaluate((XClosure)expression, context, indicator);
    } else if (expression instanceof XConstructorCall) {
      return _doEvaluate((XConstructorCall)expression, context, indicator);
    } else if (expression instanceof XForLoopExpression) {
      return _doEvaluate((XForLoopExpression)expression, context, indicator);
    } else if (expression instanceof XBasicForLoopExpression) {
	  return _doEvaluate((XBasicForLoopExpression)expression, context, indicator);
	} else if (expression instanceof XIfExpression) {
      return _doEvaluate((XIfExpression)expression, context, indicator);
    } else if (expression instanceof XInstanceOfExpression) {
      return _doEvaluate((XInstanceOfExpression)expression, context, indicator);
    } else if (expression instanceof XNullLiteral) {
      return _doEvaluate((XNullLiteral)expression, context, indicator);
    } else if (expression instanceof XNumberLiteral) {
      return _doEvaluate((XNumberLiteral)expression, context, indicator);
    } else if (expression instanceof XReturnExpression) {
      return _doEvaluate((XReturnExpression)expression, context, indicator);
    } else if (expression instanceof XStringLiteral) {
      return _doEvaluate((XStringLiteral)expression, context, indicator);
    } else if (expression instanceof XSwitchExpression) {
      return _doEvaluate((XSwitchExpression)expression, context, indicator);
    } else if (expression instanceof XThrowExpression) {
      return _doEvaluate((XThrowExpression)expression, context, indicator);
    } else if (expression instanceof XTryCatchFinallyExpression) {
      return _doEvaluate((XTryCatchFinallyExpression)expression, context, indicator);
    } else if (expression instanceof XTypeLiteral) {
      return _doEvaluate((XTypeLiteral)expression, context, indicator);
    } else if (expression instanceof XVariableDeclaration) {
	      return _doEvaluate((XVariableDeclaration)expression, context, indicator);
    } else if (expression instanceof XListLiteral) {
	      return _doEvaluate((XListLiteral)expression, context, indicator);
    } else if (expression instanceof XSetLiteral) {
	      return _doEvaluate((XSetLiteral)expression, context, indicator);
    } else {
      throw new IllegalArgumentException("Unhandled parameter types: " +
        Arrays.<Object>asList(expression, context, indicator).toString());
    }
}
 
Example #13
Source File: XbaseParserTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testWhileExpression() throws Exception {
	XWhileExpression expression = (XWhileExpression) expression("while (true) 'foo'");
	assertTrue(expression.getPredicate() instanceof XBooleanLiteral);
	assertTrue(expression.getBody() instanceof XStringLiteral);
}
 
Example #14
Source File: XbaseExpectedTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testWhileExpression_0() throws Exception {
	XWhileExpression exp = (XWhileExpression) expression("while (null) null");

	assertExpected("boolean", exp.getPredicate()); // while (null) is invalid thus we expect the primitive boolean
	assertExpected(null, exp.getBody());
}
 
Example #15
Source File: AbstractXbaseSemanticSequencer.java    From xtext-extras with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Contexts:
 *     XExpression returns XWhileExpression
 *     XAssignment returns XWhileExpression
 *     XAssignment.XBinaryOperation_1_1_0_0_0 returns XWhileExpression
 *     XOrExpression returns XWhileExpression
 *     XOrExpression.XBinaryOperation_1_0_0_0 returns XWhileExpression
 *     XAndExpression returns XWhileExpression
 *     XAndExpression.XBinaryOperation_1_0_0_0 returns XWhileExpression
 *     XEqualityExpression returns XWhileExpression
 *     XEqualityExpression.XBinaryOperation_1_0_0_0 returns XWhileExpression
 *     XRelationalExpression returns XWhileExpression
 *     XRelationalExpression.XInstanceOfExpression_1_0_0_0_0 returns XWhileExpression
 *     XRelationalExpression.XBinaryOperation_1_1_0_0_0 returns XWhileExpression
 *     XOtherOperatorExpression returns XWhileExpression
 *     XOtherOperatorExpression.XBinaryOperation_1_0_0_0 returns XWhileExpression
 *     XAdditiveExpression returns XWhileExpression
 *     XAdditiveExpression.XBinaryOperation_1_0_0_0 returns XWhileExpression
 *     XMultiplicativeExpression returns XWhileExpression
 *     XMultiplicativeExpression.XBinaryOperation_1_0_0_0 returns XWhileExpression
 *     XUnaryOperation returns XWhileExpression
 *     XCastedExpression returns XWhileExpression
 *     XCastedExpression.XCastedExpression_1_0_0_0 returns XWhileExpression
 *     XPostfixOperation returns XWhileExpression
 *     XPostfixOperation.XPostfixOperation_1_0_0 returns XWhileExpression
 *     XMemberFeatureCall returns XWhileExpression
 *     XMemberFeatureCall.XAssignment_1_0_0_0_0 returns XWhileExpression
 *     XMemberFeatureCall.XMemberFeatureCall_1_1_0_0_0 returns XWhileExpression
 *     XPrimaryExpression returns XWhileExpression
 *     XParenthesizedExpression returns XWhileExpression
 *     XWhileExpression returns XWhileExpression
 *     XExpressionOrVarDeclaration returns XWhileExpression
 *
 * Constraint:
 *     (predicate=XExpression body=XExpression)
 */
protected void sequence_XWhileExpression(ISerializationContext context, XWhileExpression semanticObject) {
	if (errorAcceptor != null) {
		if (transientValues.isValueTransient(semanticObject, XbasePackage.Literals.XABSTRACT_WHILE_EXPRESSION__PREDICATE) == ValueTransient.YES)
			errorAcceptor.accept(diagnosticProvider.createFeatureValueMissing(semanticObject, XbasePackage.Literals.XABSTRACT_WHILE_EXPRESSION__PREDICATE));
		if (transientValues.isValueTransient(semanticObject, XbasePackage.Literals.XABSTRACT_WHILE_EXPRESSION__BODY) == ValueTransient.YES)
			errorAcceptor.accept(diagnosticProvider.createFeatureValueMissing(semanticObject, XbasePackage.Literals.XABSTRACT_WHILE_EXPRESSION__BODY));
	}
	SequenceFeeder feeder = createSequencerFeeder(context, semanticObject);
	feeder.accept(grammarAccess.getXWhileExpressionAccess().getPredicateXExpressionParserRuleCall_3_0(), semanticObject.getPredicate());
	feeder.accept(grammarAccess.getXWhileExpressionAccess().getBodyXExpressionParserRuleCall_5_0(), semanticObject.getBody());
	feeder.finish();
}