@Override public boolean enterTernaryNode(final TernaryNode ternaryNode) { enterDefault(ternaryNode); type("ConditionalExpression"); comma(); property("test"); ternaryNode.getTest().accept(this); comma(); property("consequent"); ternaryNode.getTrueExpression().accept(this); comma(); property("alternate"); ternaryNode.getFalseExpression().accept(this); return leave(); }
@Override public boolean enterTernaryNode(final TernaryNode ternaryNode) { final Expression test = ternaryNode.getTest(); final Expression trueExpr = ternaryNode.getTrueExpression(); final Expression falseExpr = ternaryNode.getFalseExpression(); test.accept(this); final Map<Symbol, LvarType> testExitLvarTypes = localVariableTypes; if(!isAlwaysFalse(test)) { trueExpr.accept(this); } final Map<Symbol, LvarType> trueExitLvarTypes = localVariableTypes; localVariableTypes = testExitLvarTypes; if(!isAlwaysTrue(test)) { falseExpr.accept(this); } final Map<Symbol, LvarType> falseExitLvarTypes = localVariableTypes; localVariableTypes = getUnionTypes(trueExitLvarTypes, falseExitLvarTypes); setConversion((JoinPredecessor)trueExpr, trueExitLvarTypes, localVariableTypes); setConversion((JoinPredecessor)falseExpr, falseExitLvarTypes, localVariableTypes); return false; }
private void loadTernaryNode(final TernaryNode ternaryNode, final TypeBounds resultBounds) { final Expression test = ternaryNode.getTest(); final JoinPredecessorExpression trueExpr = ternaryNode.getTrueExpression(); final JoinPredecessorExpression falseExpr = ternaryNode.getFalseExpression(); final Label falseLabel = new Label("ternary_false"); final Label exitLabel = new Label("ternary_exit"); final Type outNarrowest = Type.narrowest(resultBounds.widest, Type.generic(Type.widestReturnType(trueExpr.getType(), falseExpr.getType()))); final TypeBounds outBounds = resultBounds.notNarrowerThan(outNarrowest); emitBranch(test, falseLabel, false); loadExpression(trueExpr.getExpression(), outBounds); assert Type.generic(method.peekType()) == outBounds.narrowest; method.beforeJoinPoint(trueExpr); method._goto(exitLabel); method.label(falseLabel); loadExpression(falseExpr.getExpression(), outBounds); assert Type.generic(method.peekType()) == outBounds.narrowest; method.beforeJoinPoint(falseExpr); method.label(exitLabel); }
private void loadTernaryNode(final TernaryNode ternaryNode, final TypeBounds resultBounds) { final Expression test = ternaryNode.getTest(); final JoinPredecessorExpression trueExpr = ternaryNode.getTrueExpression(); final JoinPredecessorExpression falseExpr = ternaryNode.getFalseExpression(); final Label falseLabel = new Label("ternary_false"); final Label exitLabel = new Label("ternary_exit"); final Type outNarrowest = Type.narrowest(resultBounds.widest, Type.generic(Type.widestReturnType(trueExpr.getType(), falseExpr.getType()))); final TypeBounds outBounds = resultBounds.notNarrowerThan(outNarrowest); emitBranch(test, falseLabel, false); final boolean isCurrentDiscard = lc.popDiscardIfCurrent(ternaryNode); loadMaybeDiscard(isCurrentDiscard, trueExpr.getExpression(), outBounds); assert isCurrentDiscard || Type.generic(method.peekType()) == outBounds.narrowest; method.beforeJoinPoint(trueExpr); method._goto(exitLabel); method.label(falseLabel); loadMaybeDiscard(isCurrentDiscard, falseExpr.getExpression(), outBounds); assert isCurrentDiscard || Type.generic(method.peekType()) == outBounds.narrowest; method.beforeJoinPoint(falseExpr); method.label(exitLabel); }
private void branchOptimizer(final Expression node, final Label label, final boolean state) { if (!(node instanceof TernaryNode)) { if (node instanceof BinaryNode) { branchOptimizer((BinaryNode)node, label, state); return; } if (node instanceof UnaryNode) { branchOptimizer((UnaryNode)node, label, state); return; } } codegen.load(node, Type.BOOLEAN); if (state) { method.ifne(label); } else { method.ifeq(label); } }
private void branchOptimizer(final Expression node, final Label label, final boolean state) { if (!(node instanceof TernaryNode)) { if (node instanceof BinaryNode) { branchOptimizer((BinaryNode)node, label, state); return; } if (node instanceof UnaryNode) { branchOptimizer((UnaryNode)node, label, state); return; } } load(node); method.convert(Type.BOOLEAN); if (state) { method.ifne(label); } else { method.ifeq(label); } }
@Override public Node leaveTernaryNode(final TernaryNode ternaryNode) { final Node test = ternaryNode.getTest(); if (test instanceof LiteralNode.PrimitiveLiteralNode) { return (((LiteralNode.PrimitiveLiteralNode<?>)test).isTrue() ? ternaryNode.getTrueExpression() : ternaryNode.getFalseExpression()).getExpression(); } return ternaryNode; }
@Override public boolean enterTernaryNode(final TernaryNode ternaryNode) { curExpr = new ConditionalExpressionTreeImpl(ternaryNode, translateExpr(ternaryNode.getTest()), translateExpr(ternaryNode.getTrueExpression()), translateExpr(ternaryNode.getFalseExpression())); return false; }
ConditionalExpressionTreeImpl(final TernaryNode node, final ExpressionTree condExpr, final ExpressionTree trueExpr, final ExpressionTree falseExpr) { super(node); this.condExpr = condExpr; this.trueExpr = trueExpr; this.falseExpr = falseExpr; }
@Override public boolean enterTernaryNode(final TernaryNode ternaryNode) { final Expression test = ternaryNode.getTest(); final Expression trueExpr = ternaryNode.getTrueExpression(); final Expression falseExpr = ternaryNode.getFalseExpression(); visitExpression(test); final Map<Symbol, LvarType> testExitLvarTypes = localVariableTypes; final LvarType trueType; if(!isAlwaysFalse(test)) { trueType = visitExpression(trueExpr); } else { trueType = null; } final Map<Symbol, LvarType> trueExitLvarTypes = localVariableTypes; localVariableTypes = testExitLvarTypes; final LvarType falseType; if(!isAlwaysTrue(test)) { falseType = visitExpression(falseExpr); } else { falseType = null; } final Map<Symbol, LvarType> falseExitLvarTypes = localVariableTypes; localVariableTypes = getUnionTypes(trueExitLvarTypes, falseExitLvarTypes); setConversion((JoinPredecessor)trueExpr, trueExitLvarTypes, localVariableTypes); setConversion((JoinPredecessor)falseExpr, falseExitLvarTypes, localVariableTypes); typeStack.push(trueType != null ? falseType != null ? widestLvarType(trueType, falseType) : trueType : assertNotNull(falseType)); return false; }
@Override public Node leaveTernaryNode(final TernaryNode ternaryNode) { final Expression trueExpr = ternaryNode.getTrueExpression(); final Expression falseExpr = ternaryNode.getFalseExpression(); ensureTypeNotUnknown(trueExpr); ensureTypeNotUnknown(falseExpr); final Type type = widestReturnType(trueExpr.getType(), falseExpr.getType()); return end(ensureSymbol(type, ternaryNode)); }
@Override public Node leaveTernaryNode(final TernaryNode ternaryNode) { final Node test = ternaryNode.getTest(); if (test instanceof LiteralNode.PrimitiveLiteralNode) { return ((LiteralNode.PrimitiveLiteralNode<?>)test).isTrue() ? ternaryNode.getTrueExpression() : ternaryNode.getFalseExpression(); } return ternaryNode; }