protected void _computeTypes(XIfExpression object, ITypeComputationState state) { ITypeComputationState conditionExpectation = state.withExpectation(getRawTypeForName(Boolean.TYPE, state)); XExpression condition = object.getIf(); conditionExpectation.computeTypes(condition); // TODO then expression may influence the expected type of else and vice versa XExpression thenExpression = getThen(object); ITypeComputationState thenState = reassignCheckedType(condition, thenExpression, state); ITypeComputationResult thenResult = thenState.computeTypes(thenExpression); XExpression elseExpression = getElse(object); if (elseExpression != null) { state.computeTypes(elseExpression); } else { BranchExpressionProcessor processor = new BranchExpressionProcessor(state, object) { @Override protected String getMessage() { return "Missing else branch for conditional expression with primitive type"; } }; processor.process(thenResult); processor.commit(); } }
/** * Syntax: '('* */ @Override protected void emit_XParenthesizedExpression_LeftParenthesisKeyword_0_a(EObject semanticObject, ISynNavigable transition, List<INode> nodes) { Keyword kw = grammarAccess.getXParenthesizedExpressionAccess().getLeftParenthesisKeyword_0(); if (nodes == null) { if (semanticObject instanceof XIfExpression || semanticObject instanceof XTryCatchFinallyExpression) { EObject cnt = semanticObject.eContainer(); if (cnt instanceof XExpression && !(cnt instanceof XBlockExpression) && !(cnt instanceof XForLoopExpression)) acceptUnassignedKeyword(kw, kw.getValue(), null); } if (semanticObject instanceof XConstructorCall) { XConstructorCall call = (XConstructorCall) semanticObject; if (!call.isExplicitConstructorCall() && call.getArguments().isEmpty()) { acceptUnassignedKeyword(kw, kw.getValue(), null); } } } acceptNodes(transition, nodes); }
protected boolean bracesAreAddedByOuterStructure(XExpression expression) { EObject container = expression.eContainer(); if (container instanceof XTryCatchFinallyExpression || container instanceof XIfExpression || container instanceof XClosure || container instanceof XSynchronizedExpression) { return true; } if (container instanceof XBlockExpression) { XBlockExpression blockExpression = (XBlockExpression) container; EList<XExpression> expressions = blockExpression.getExpressions(); if (expressions.size() == 1 && expressions.get(0) == expression) { return bracesAreAddedByOuterStructure(blockExpression); } } if (!(container instanceof XExpression)) { return true; } return false; }
@Test public void testSerialize_02() throws Exception { Resource resource = newResource("'foo' as String"); XCastedExpression casted = (XCastedExpression) resource.getContents().get(0); XbaseFactory factory = XbaseFactory.eINSTANCE; XIfExpression ifExpression = factory.createXIfExpression(); ifExpression.setIf(factory.createXBooleanLiteral()); XStringLiteral stringLiteral = factory.createXStringLiteral(); stringLiteral.setValue("value"); ifExpression.setThen(stringLiteral); XInstanceOfExpression instanceOfExpression = factory.createXInstanceOfExpression(); instanceOfExpression.setExpression(ifExpression); instanceOfExpression.setType(EcoreUtil.copy(casted.getType())); resource.getContents().clear(); resource.getContents().add(instanceOfExpression); ISerializer serializer = get(ISerializer.class); String string = serializer.serialize(instanceOfExpression); // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=464846 assertEquals("( if(false) \"value\" ) instanceof String", string); XInstanceOfExpression parsedExpression = parseHelper.parse(string); assertTrue(EcoreUtil.equals(instanceOfExpression, parsedExpression)); }
@Test public void testReassignedType_01() { try { StringConcatenation _builder = new StringConcatenation(); _builder.append("{ var it = new Object() if (it instanceof String) {} }"); XExpression _expression = this.expression(_builder, false); XExpression _last = IterableExtensions.<XExpression>last(((XBlockExpression) _expression).getExpressions()); final XIfExpression ifExpr = ((XIfExpression) _last); XExpression _then = ifExpr.getThen(); final XBlockExpression block = ((XBlockExpression) _then); final IExpressionScope expressionScope = this._iBatchTypeResolver.resolveTypes(block).getExpressionScope(block, IExpressionScope.Anchor.BEFORE); this.contains(expressionScope, "charAt"); this.contains(expressionScope, "it"); this.contains(expressionScope, "operator_lessThan"); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
@Test public void testReassignedType_02() { try { StringConcatenation _builder = new StringConcatenation(); _builder.append("{ var it = new Object() if (it instanceof String) { it = new Object() } }"); XExpression _expression = this.expression(_builder, false); XExpression _last = IterableExtensions.<XExpression>last(((XBlockExpression) _expression).getExpressions()); final XIfExpression ifExpr = ((XIfExpression) _last); XExpression _then = ifExpr.getThen(); final XBlockExpression block = ((XBlockExpression) _then); final IExpressionScope expressionScope = this._iBatchTypeResolver.resolveTypes(block).getExpressionScope(block, IExpressionScope.Anchor.BEFORE); this.contains(expressionScope, "charAt"); this.contains(expressionScope, "it"); this.contains(expressionScope, "operator_lessThan"); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
@Test public void testReassignedType_03() { try { StringConcatenation _builder = new StringConcatenation(); _builder.append("{ var it = new Object() if (it instanceof String) { it = new Object() } }"); XExpression _expression = this.expression(_builder, false); XExpression _last = IterableExtensions.<XExpression>last(((XBlockExpression) _expression).getExpressions()); final XIfExpression ifExpr = ((XIfExpression) _last); XExpression _then = ifExpr.getThen(); final XBlockExpression block = ((XBlockExpression) _then); final XExpression assignment = IterableExtensions.<XExpression>head(block.getExpressions()); final IExpressionScope expressionScope = this._iBatchTypeResolver.resolveTypes(assignment).getExpressionScope(assignment, IExpressionScope.Anchor.AFTER); this.containsNot(expressionScope, "charAt"); this.contains(expressionScope, "it"); this.containsNot(expressionScope, "operator_lessThan"); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
private List<XExpression> collectIfParts(XIfExpression expression, List<XExpression> result) { result.add(expression.getIf()); if (expression.getElse() instanceof XIfExpression) { collectIfParts((XIfExpression) expression.getElse(), result); } return result; }
@Check public void checkDeadCode(XIfExpression condition) { if (!earlyExitComputer.isEarlyExit(condition.getIf())) { validateCondition(condition.getIf(), false); } else { if (!markAsDeadCode(condition.getThen())) { markAsDeadCode(condition.getElse()); } } }
@Override protected void internalToConvertedExpression(XExpression obj, ITreeAppendable appendable) { if (obj instanceof XBlockExpression) { _toJavaExpression((XBlockExpression) obj, appendable); } else if (obj instanceof XCastedExpression) { _toJavaExpression((XCastedExpression) obj, appendable); } else if (obj instanceof XClosure) { _toJavaExpression((XClosure) obj, appendable); } else if (obj instanceof XAnnotation) { _toJavaExpression((XAnnotation) obj, appendable); } else if (obj instanceof XConstructorCall) { _toJavaExpression((XConstructorCall) obj, appendable); } else if (obj instanceof XIfExpression) { _toJavaExpression((XIfExpression) obj, appendable); } else if (obj instanceof XInstanceOfExpression) { _toJavaExpression((XInstanceOfExpression) obj, appendable); } else if (obj instanceof XSwitchExpression) { _toJavaExpression((XSwitchExpression) obj, appendable); } else if (obj instanceof XTryCatchFinallyExpression) { _toJavaExpression((XTryCatchFinallyExpression) obj, appendable); } else if (obj instanceof XListLiteral) { _toJavaExpression((XListLiteral) obj, appendable); } else if (obj instanceof XSetLiteral) { _toJavaExpression((XSetLiteral) obj, appendable); } else if (obj instanceof XSynchronizedExpression) { _toJavaExpression((XSynchronizedExpression) obj, appendable); } else if (obj instanceof XReturnExpression) { _toJavaExpression((XReturnExpression) obj, appendable); } else if (obj instanceof XThrowExpression) { _toJavaExpression((XThrowExpression) obj, appendable); } else { super.internalToConvertedExpression(obj, appendable); } }
protected void _toJavaStatement(XIfExpression expr, ITreeAppendable b, boolean isReferenced) { if (isReferenced) declareSyntheticVariable(expr, b); internalToJavaStatement(expr.getIf(), b, true); b.newLine().append("if ("); internalToJavaExpression(expr.getIf(), b); b.append(") {").increaseIndentation(); final boolean canBeReferenced = isReferenced && !isPrimitiveVoid(expr.getThen()); internalToJavaStatement(expr.getThen(), b, canBeReferenced); if (canBeReferenced) { b.newLine(); b.append(getVarName(expr, b)); b.append(" = "); internalToConvertedExpression(expr.getThen(), b, getLightweightType(expr)); b.append(";"); } b.decreaseIndentation().newLine().append("}"); if (expr.getElse() != null) { b.append(" else {").increaseIndentation(); final boolean canElseBeReferenced = isReferenced && !isPrimitiveVoid(expr.getElse()); internalToJavaStatement(expr.getElse(), b, canElseBeReferenced); if (canElseBeReferenced) { b.newLine(); b.append(getVarName(expr, b)); b.append(" = "); internalToConvertedExpression(expr.getElse(), b, getLightweightType(expr)); b.append(";"); } b.decreaseIndentation().newLine().append("}"); } }
protected Object _doEvaluate(XIfExpression ifExpression, IEvaluationContext context, CancelIndicator indicator) { Object conditionResult = internalEvaluate(ifExpression.getIf(), context, indicator); if (Boolean.TRUE.equals(conditionResult)) { return internalEvaluate(ifExpression.getThen(), context, indicator); } else { if (ifExpression.getElse() == null) return getDefaultObjectValue(typeResolver.resolveTypes(ifExpression).getActualType(ifExpression)); return internalEvaluate(ifExpression.getElse(), context, indicator); } }
protected Collection<IEarlyExitComputer.ExitPoint> _exitPoints(final XIfExpression expression) { Collection<IEarlyExitComputer.ExitPoint> ifExitPoints = this.getExitPoints(expression.getIf()); boolean _isNotEmpty = this.isNotEmpty(ifExitPoints); if (_isNotEmpty) { return ifExitPoints; } Collection<IEarlyExitComputer.ExitPoint> thenExitPoints = this.getExitPoints(expression.getThen()); Collection<IEarlyExitComputer.ExitPoint> elseExitPoints = this.getExitPoints(expression.getElse()); if ((this.isNotEmpty(thenExitPoints) && this.isNotEmpty(elseExitPoints))) { Collection<IEarlyExitComputer.ExitPoint> result = Lists.<IEarlyExitComputer.ExitPoint>newArrayList(thenExitPoints); result.addAll(elseExitPoints); return result; } return Collections.<IEarlyExitComputer.ExitPoint>emptyList(); }
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()); } }
@Test public void testIf_3() throws Exception { XIfExpression ie = (XIfExpression) expression("if (foo) bar else if (baz) if (apa) bpa else cpa"); XIfExpression nestedIf = (XIfExpression) ie.getElse(); XIfExpression secondNested = (XIfExpression) nestedIf.getThen(); XFeatureCall cpa = (XFeatureCall) secondNested.getElse(); assertEquals("cpa",cpa.getConcreteSyntaxFeatureName()); }
@Test public void testIfExpression() throws Exception { XIfExpression target = (XIfExpression) expressionWithExpectedType("if (null) null else null", "String"); assertExpected("boolean", target.getIf()); // if (null) is invalid thus we expect the primitive boolean assertExpected("java.lang.String", target.getThen()); assertExpected("java.lang.String", target.getElse()); }
@Test public void testIf01() { try { StringConcatenation _builder = new StringConcatenation(); _builder.append("if (true) 1 else 2"); XExpression _expression = this.expression(_builder); final XIfExpression expr = ((XIfExpression) _expression); this.hasImplicitReturns(expr, expr.getThen(), expr.getElse()); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
@Deprecated protected void sequence_XIfExpression(EObject context, XIfExpression semanticObject) { sequence_XIfExpression(createContext(context, semanticObject), semanticObject); }
@Check public void checkInstanceOfOrder(XIfExpression expression) { if (isIgnored(IssueCodes.UNREACHABLE_IF_BLOCK)) { return; } if (expression.eContainer() instanceof XIfExpression) { XIfExpression container = (XIfExpression) expression.eContainer(); if (container.getElse() == expression) { return; } } List<XExpression> ifParts = collectIfParts(expression, new ArrayList<XExpression>()); ITypeReferenceOwner owner = new StandardTypeReferenceOwner(getServices(), expression); Multimap<JvmIdentifiableElement, LightweightTypeReference> previousTypeReferences = HashMultimap.create(); for (XExpression ifPart : ifParts) { if (!(ifPart instanceof XInstanceOfExpression)) { continue; } XInstanceOfExpression instanceOfExpression = (XInstanceOfExpression) ifPart; if (!(instanceOfExpression.getExpression() instanceof XAbstractFeatureCall)) { continue; } XAbstractFeatureCall featureCall = (XAbstractFeatureCall) instanceOfExpression.getExpression(); JvmIdentifiableElement feature = featureCall.getFeature(); if (!(feature instanceof XVariableDeclaration) && !(feature instanceof JvmField) && !(feature instanceof JvmFormalParameter)) { continue; } JvmTypeReference type = instanceOfExpression.getType(); LightweightTypeReference actualType = owner.toLightweightTypeReference(type); if (actualType == null) { continue; } if (isHandled(actualType, previousTypeReferences.get(feature))) { addIssue("Unreachable code: The if condition can never match. It is already handled by a previous condition.", type, IssueCodes.UNREACHABLE_IF_BLOCK); continue; } previousTypeReferences.put(feature, actualType); } }
@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); } }
/** * Returns <code>true</code> for expressions that seem to be early exit expressions, e.g. * <pre> * while(condition) { * if (anotherCondition) * return value * changeResultOfFirstCondition * } * </pre> */ public boolean isIntentionalEarlyExit(/* @Nullable */ XExpression expression) { if (expression == null) { return true; } if (expression instanceof XBlockExpression) { XBlockExpression block = (XBlockExpression) expression; List<XExpression> children = block.getExpressions(); for(XExpression child: children) { if (isIntentionalEarlyExit(child)) { return true; } } } else if (expression instanceof XIfExpression) { return isIntentionalEarlyExit(((XIfExpression) expression).getThen()) || isIntentionalEarlyExit(((XIfExpression) expression).getElse()); } else if (expression instanceof XSwitchExpression) { XSwitchExpression switchExpression = (XSwitchExpression) expression; for(XCasePart caseExpression: switchExpression.getCases()) { if (isIntentionalEarlyExit(caseExpression.getThen())) { return true; } } if (isIntentionalEarlyExit(switchExpression.getDefault())) { return true; } } else if (expression instanceof XTryCatchFinallyExpression) { XTryCatchFinallyExpression tryCatchFinally = (XTryCatchFinallyExpression) expression; if (isIntentionalEarlyExit(tryCatchFinally.getExpression())) { for(XCatchClause catchClause: tryCatchFinally.getCatchClauses()) { if (!isIntentionalEarlyExit(catchClause.getExpression())) return false; } return true; } return false; } else if (expression instanceof XAbstractWhileExpression) { return isIntentionalEarlyExit(((XAbstractWhileExpression) expression).getBody()); } else if (expression instanceof XForLoopExpression) { return isIntentionalEarlyExit(((XForLoopExpression) expression).getEachExpression()); } else if (expression instanceof XBasicForLoopExpression) { return isIntentionalEarlyExit(((XBasicForLoopExpression) expression).getEachExpression()); } else if (expression instanceof XSynchronizedExpression) { return isIntentionalEarlyExit(((XSynchronizedExpression) expression).getExpression()); } return expression instanceof XReturnExpression || expression instanceof XThrowExpression; }
public boolean isDefiniteEarlyExit(XExpression expression) { // TODO further improvements if (expression instanceof XIfExpression) { XIfExpression ifExpression = (XIfExpression) expression; return isDefiniteEarlyExit(ifExpression.getThen()) && isDefiniteEarlyExit(ifExpression.getElse()); } else if (expression instanceof XSwitchExpression) { XSwitchExpression switchExpression = (XSwitchExpression) expression; if (isDefiniteEarlyExit(switchExpression.getDefault())) { for(XCasePart caseExpression: switchExpression.getCases()) { if (!isDefiniteEarlyExit(caseExpression.getThen())) { return false; } } return true; } return false; } else if (expression instanceof XTryCatchFinallyExpression) { XTryCatchFinallyExpression tryExpression = (XTryCatchFinallyExpression) expression; if (isDefiniteEarlyExit(tryExpression.getFinallyExpression())) { return true; } if (isDefiniteEarlyExit(tryExpression.getExpression())) { for(XCatchClause catchClause: tryExpression.getCatchClauses()) { if (!isDefiniteEarlyExit(catchClause.getExpression())) { return false; } } return true; } return false; } else if (expression instanceof XBlockExpression) { List<XExpression> expressions = ((XBlockExpression) expression).getExpressions(); for(int i = expressions.size() - 1; i >= 0; i--) { if (isDefiniteEarlyExit(expressions.get(i))) { return true; } } } else if (expression instanceof XSynchronizedExpression) { return isDefiniteEarlyExit(((XSynchronizedExpression) expression).getExpression()); } return expression instanceof XReturnExpression || expression instanceof XThrowExpression; }
@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); } }
protected void _toJavaExpression(XIfExpression expr, ITreeAppendable b) { b.trace(expr, false).append(getVarName(expr, b)); }
@Override protected boolean isVariableDeclarationRequired(XExpression expr, ITreeAppendable b, boolean recursive) { if (expr instanceof XAnnotation) { return false; } if (expr instanceof XListLiteral) { return false; } if (expr instanceof XSetLiteral) { return false; } if (expr instanceof XCastedExpression) { return false; } if (expr instanceof XInstanceOfExpression) { return false; } if (expr instanceof XMemberFeatureCall && isVariableDeclarationRequired((XMemberFeatureCall) expr, b)) return true; EObject container = expr.eContainer(); if ((container instanceof XVariableDeclaration) || (container instanceof XReturnExpression) || (container instanceof XThrowExpression)) { return false; } if (container instanceof XIfExpression) { XIfExpression ifExpression = (XIfExpression) container; if (ifExpression.getThen() == expr || ifExpression.getElse() == expr) { return false; } } if (container instanceof XCasePart) { XCasePart casePart = (XCasePart) container; if (casePart.getThen() == expr) { return false; } } if (container instanceof XSwitchExpression) { XSwitchExpression switchExpression = (XSwitchExpression) container; if (switchExpression.getDefault() == expr) { return false; } } if (container instanceof XBlockExpression) { List<XExpression> siblings = ((XBlockExpression) container).getExpressions(); if (siblings.get(siblings.size() - 1) == expr) { return isVariableDeclarationRequired(getFeatureCall(expr), expr, b); } } if (container instanceof XClosure) { if (((XClosure) container).getExpression() == expr) { return false; } } if (expr instanceof XAssignment) { XAssignment a = (XAssignment) expr; for (XExpression arg : getActualArguments(a)) { if (isVariableDeclarationRequired(arg, b, recursive)) { return true; } } } return super.isVariableDeclarationRequired(expr, b, recursive); }
/** * 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()); } }
protected void _findImplicitReturns(final XIfExpression expression, final ImplicitReturnFinder.Acceptor acceptor) { this.findImplicitReturns(expression.getThen(), acceptor); this.findImplicitReturns(expression.getElse(), acceptor); }
public void findImplicitReturns(final XExpression expression, final ImplicitReturnFinder.Acceptor acceptor) { if (expression instanceof XAbstractFeatureCall) { _findImplicitReturns((XAbstractFeatureCall)expression, acceptor); return; } else if (expression instanceof XBlockExpression) { _findImplicitReturns((XBlockExpression)expression, acceptor); return; } else if (expression instanceof XBooleanLiteral) { _findImplicitReturns((XBooleanLiteral)expression, acceptor); return; } else if (expression instanceof XCastedExpression) { _findImplicitReturns((XCastedExpression)expression, acceptor); return; } else if (expression instanceof XClosure) { _findImplicitReturns((XClosure)expression, acceptor); return; } else if (expression instanceof XCollectionLiteral) { _findImplicitReturns((XCollectionLiteral)expression, acceptor); return; } else if (expression instanceof XConstructorCall) { _findImplicitReturns((XConstructorCall)expression, acceptor); return; } else if (expression instanceof XIfExpression) { _findImplicitReturns((XIfExpression)expression, acceptor); return; } else if (expression instanceof XInstanceOfExpression) { _findImplicitReturns((XInstanceOfExpression)expression, acceptor); return; } else if (expression instanceof XNullLiteral) { _findImplicitReturns((XNullLiteral)expression, acceptor); return; } else if (expression instanceof XNumberLiteral) { _findImplicitReturns((XNumberLiteral)expression, acceptor); return; } else if (expression instanceof XStringLiteral) { _findImplicitReturns((XStringLiteral)expression, acceptor); return; } else if (expression instanceof XSwitchExpression) { _findImplicitReturns((XSwitchExpression)expression, acceptor); return; } else if (expression instanceof XSynchronizedExpression) { _findImplicitReturns((XSynchronizedExpression)expression, acceptor); return; } else if (expression instanceof XTryCatchFinallyExpression) { _findImplicitReturns((XTryCatchFinallyExpression)expression, acceptor); return; } else if (expression instanceof XTypeLiteral) { _findImplicitReturns((XTypeLiteral)expression, acceptor); return; } else if (expression != null) { _findImplicitReturns(expression, acceptor); return; } else if (expression == null) { _findImplicitReturns((Void)null, acceptor); return; } else { throw new IllegalArgumentException("Unhandled parameter types: " + Arrays.<Object>asList(expression, acceptor).toString()); } }
@Override protected XExpression getThen(XIfExpression ifExpression) { if (ifExpression.getElse() != null) return ifExpression.getElse(); return super.getThen(ifExpression); }
@Override protected XExpression getElse(XIfExpression ifExpression) { if (ifExpression.getElse() != null) return ifExpression.getThen(); return super.getElse(ifExpression); }
@Test public void testIf_0() throws Exception { XIfExpression ie = (XIfExpression) expression("if (true) false"); assertTrue(((XBooleanLiteral) ie.getIf()).isIsTrue()); assertFalse(((XBooleanLiteral) ie.getThen()).isIsTrue()); assertNull(ie.getElse()); }
@Test public void testIf_1() throws Exception { XIfExpression ie = (XIfExpression) expression("if (true) false else bar"); assertTrue(((XBooleanLiteral) ie.getIf()).isIsTrue()); assertFalse(((XBooleanLiteral) ie.getThen()).isIsTrue()); assertTrue(ie.getElse() instanceof XFeatureCall); }
@Test public void testIf_2() throws Exception { XIfExpression ie = (XIfExpression) expression("if (foo) bar else if (baz) apa else bpa"); XIfExpression nestedIf = (XIfExpression) ie.getElse(); XFeatureCall bpa = (XFeatureCall) nestedIf.getElse(); assertEquals("bpa",bpa.getConcreteSyntaxFeatureName()); }
@Test public void testIfWithClosure() throws Exception { XIfExpression ie = (XIfExpression) expression("if (foo) [| if (bar) zonk else bar]"); assertNull(ie.getElse()); }
@Test public void testIfWithAdd() throws Exception { XBinaryOperation bo = (XBinaryOperation) expression("1 + if (foo) bar + 2"); assertTrue(bo.getExplicitArguments().get(1) instanceof XIfExpression); }
/** * Contexts: * XExpression returns XIfExpression * XAssignment returns XIfExpression * XAssignment.XBinaryOperation_1_1_0_0_0 returns XIfExpression * XOrExpression returns XIfExpression * XOrExpression.XBinaryOperation_1_0_0_0 returns XIfExpression * XAndExpression returns XIfExpression * XAndExpression.XBinaryOperation_1_0_0_0 returns XIfExpression * XEqualityExpression returns XIfExpression * XEqualityExpression.XBinaryOperation_1_0_0_0 returns XIfExpression * XRelationalExpression returns XIfExpression * XRelationalExpression.XInstanceOfExpression_1_0_0_0_0 returns XIfExpression * XRelationalExpression.XBinaryOperation_1_1_0_0_0 returns XIfExpression * XOtherOperatorExpression returns XIfExpression * XOtherOperatorExpression.XBinaryOperation_1_0_0_0 returns XIfExpression * XAdditiveExpression returns XIfExpression * XAdditiveExpression.XBinaryOperation_1_0_0_0 returns XIfExpression * XMultiplicativeExpression returns XIfExpression * XMultiplicativeExpression.XBinaryOperation_1_0_0_0 returns XIfExpression * XUnaryOperation returns XIfExpression * XCastedExpression returns XIfExpression * XCastedExpression.XCastedExpression_1_0_0_0 returns XIfExpression * XPostfixOperation returns XIfExpression * XPostfixOperation.XPostfixOperation_1_0_0 returns XIfExpression * XMemberFeatureCall returns XIfExpression * XMemberFeatureCall.XAssignment_1_0_0_0_0 returns XIfExpression * XMemberFeatureCall.XMemberFeatureCall_1_1_0_0_0 returns XIfExpression * XPrimaryExpression returns XIfExpression * XParenthesizedExpression returns XIfExpression * XIfExpression returns XIfExpression * XExpressionOrVarDeclaration returns XIfExpression * * Constraint: * (if=XExpression then=XExpression else=XExpression?) */ protected void sequence_XIfExpression(ISerializationContext context, XIfExpression semanticObject) { genericSequencer.createSequence(context, semanticObject); }
/** * Only for testing purpose. * @nooverride This method is not intended to be re-implemented or extended by clients. * @noreference This method is not intended to be referenced by clients. */ /* @Nullable */ protected XExpression getElse(XIfExpression ifExpression) { return ifExpression.getElse(); }
/** * Only for testing purpose. * @nooverride This method is not intended to be re-implemented or extended by clients. * @noreference This method is not intended to be referenced by clients. */ /* @Nullable */ protected XExpression getThen(XIfExpression ifExpression) { return ifExpression.getThen(); }
/** * Constraint: * (if=XExpression then=XExpression else=XExpression?) */ protected void sequence_XIfExpression(EObject context, XIfExpression semanticObject) { genericSequencer.createSequence(context, semanticObject); }