Search in sources :

Example 1 with JCReturn

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCReturn in project ceylon by eclipse.

the class ClassTransformer method generateIndirectGetterBlock.

private JCTree.JCBlock generateIndirectGetterBlock(Value v) {
    JCTree.JCExpression returnExpr;
    returnExpr = naming.makeQualIdent(naming.makeName(v, Naming.NA_WRAPPER), "get_");
    returnExpr = make().Apply(null, returnExpr, List.<JCExpression>nil());
    JCReturn returnValue = make().Return(returnExpr);
    List<JCStatement> stmts = List.<JCTree.JCStatement>of(returnValue);
    JCTree.JCBlock block = make().Block(0L, stmts);
    return block;
}
Also used : JCReturn(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCReturn) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) JCBlock(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)

Example 2 with JCReturn

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCReturn in project ceylon by eclipse.

the class NamedArgumentInvocation method bindMethodArgument.

private void bindMethodArgument(Tree.MethodArgument methodArg, Parameter declaredParam, Naming.SyntheticName argName) {
    ListBuffer<JCStatement> statements;
    Function model = methodArg.getDeclarationModel();
    List<JCStatement> body;
    boolean prevNoExpressionlessReturn = gen.statementGen().noExpressionlessReturn;
    boolean prevSyntheticClassBody = gen.expressionGen().withinSyntheticClassBody(Decl.isMpl(model) || gen.expressionGen().isWithinSyntheticClassBody());
    try {
        gen.expressionGen().withinSyntheticClassBody(true);
        gen.statementGen().noExpressionlessReturn = AbstractTransformer.isAnything(model.getType());
        if (methodArg.getBlock() != null) {
            body = gen.statementGen().transformBlock(methodArg.getBlock());
            if (!methodArg.getBlock().getDefinitelyReturns()) {
                if (AbstractTransformer.isAnything(model.getType())) {
                    body = body.append(gen.make().Return(gen.makeNull()));
                } else {
                    body = body.append(gen.make().Return(gen.makeErroneous(methodArg.getBlock(), "compiler bug: non-void method does not definitely return")));
                }
            }
        } else {
            Expression expr = methodArg.getSpecifierExpression().getExpression();
            BoxingStrategy boxing = CodegenUtil.getBoxingStrategy(model);
            Type type = model.getType();
            JCExpression transExpr = gen.expressionGen().transformExpression(expr, boxing, type);
            JCReturn returnStat = gen.make().Return(transExpr);
            body = List.<JCStatement>of(returnStat);
        }
    } finally {
        gen.expressionGen().withinSyntheticClassBody(prevSyntheticClassBody);
        gen.statementGen().noExpressionlessReturn = prevNoExpressionlessReturn;
    }
    Type callableType = model.appliedReference(null, Collections.<Type>emptyList()).getFullType();
    CallableBuilder callableBuilder = CallableBuilder.methodArgument(gen.gen(), methodArg, model, callableType, Collections.singletonList(methodArg.getParameterLists().get(0)), gen.classGen().transformMplBody(methodArg.getParameterLists(), model, body));
    JCExpression callable = callableBuilder.build();
    gen.at(methodArg);
    JCExpression typeExpr = gen.makeJavaType(callableType, JT_RAW);
    JCVariableDecl varDecl = gen.makeVar(argName, typeExpr, callable);
    statements = ListBuffer.<JCStatement>of(varDecl);
    bind(declaredParam, argName, gen.makeJavaType(callableType), statements.toList());
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) Type(org.eclipse.ceylon.model.typechecker.model.Type) JCReturn(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCReturn) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) QualifiedTypeExpression(org.eclipse.ceylon.compiler.typechecker.tree.Tree.QualifiedTypeExpression) Expression(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Expression) StaticMemberOrTypeExpression(org.eclipse.ceylon.compiler.typechecker.tree.Tree.StaticMemberOrTypeExpression) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement) BoxingStrategy(org.eclipse.ceylon.compiler.java.codegen.AbstractTransformer.BoxingStrategy) JCVariableDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl)

Example 3 with JCReturn

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCReturn in project ceylon by eclipse.

the class ExpressionTransformer method transform.

JCExpression transform(Tree.FunctionArgument functionArg) {
    Function model = functionArg.getDeclarationModel();
    TypedReference functionalInterface = gen().checkForFunctionalInterface(expectedType);
    int flags = 0;
    // even if we are a Callable<X>, if the FI returns void don't check for non-null return types
    if (functionalInterface != null && Decl.isUnboxedVoid(functionalInterface.getDeclaration()))
        flags |= EXPR_TARGET_ACCEPTS_NULL;
    List<JCStatement> body;
    boolean prevNoExpressionlessReturn = statementGen().noExpressionlessReturn;
    boolean prevSyntheticClassBody = expressionGen().withinSyntheticClassBody(true);
    Function prevCoercedFunctionalInterfaceNeedsNoNullChecks = expressionGen().coercedFunctionalInterfaceNeedsNoNullChecks;
    try {
        statementGen().noExpressionlessReturn = isAnything(model.getType());
        expressionGen().coercedFunctionalInterfaceNeedsNoNullChecks = (flags & EXPR_TARGET_ACCEPTS_NULL) != 0 ? model : null;
        if (functionArg.getBlock() != null) {
            body = statementGen().transformBlock(functionArg.getBlock());
            if (!functionArg.getBlock().getDefinitelyReturns()) {
                if (isAnything(model.getType())) {
                    body = body.append(make().Return(makeNull()));
                } else {
                    body = body.append(make().Return(makeErroneous(functionArg.getBlock(), "compiler bug: non-void method does not definitely return")));
                }
            }
        } else {
            Tree.Expression expr = functionArg.getExpression();
            JCExpression transExpr = expressionGen().transformExpression(expr, flags);
            JCReturn returnStat = make().Return(transExpr);
            body = List.<JCStatement>of(returnStat);
        }
    } finally {
        expressionGen().withinSyntheticClassBody(prevSyntheticClassBody);
        expressionGen().coercedFunctionalInterfaceNeedsNoNullChecks = prevCoercedFunctionalInterfaceNeedsNoNullChecks;
        statementGen().noExpressionlessReturn = prevNoExpressionlessReturn;
    }
    Type callableType = functionArg.getTypeModel();
    CallableBuilder callableBuilder = CallableBuilder.methodArgument(gen(), functionArg, model, callableType, Collections.singletonList(functionArg.getParameterLists().get(0)), classGen().transformMplBody(functionArg.getParameterLists(), model, body));
    callableBuilder.checkForFunctionalInterface(expectedType);
    JCExpression result = callableBuilder.build();
    result = applyErasureAndBoxing(result, callableType, true, BoxingStrategy.BOXED, expectedType);
    return result;
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) JCReturn(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCReturn) UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) Type(org.eclipse.ceylon.model.typechecker.model.Type) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference) Expression(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Expression) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)

Example 4 with JCReturn

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCReturn in project ceylon by eclipse.

the class JavaPositionsRetriever method getJavaSourceCodeWithCeylonPositions.

public String getJavaSourceCodeWithCeylonPositions() {
    final CharArrayWriter writer = new CharArrayWriter();
    Pretty printer = new Pretty(writer, true) {

        int previousCeylonPosition = -1;

        int previousPositionInString = 0;

        private void outputCeylonPosition(JCTree tree) {
            try {
                int currentCeylonPosition = tree.getPreferredPosition();
                int currentPositionInString = writer.size();
                if (previousCeylonPosition != currentCeylonPosition || previousPositionInString != currentPositionInString) {
                    if (currentCeylonPosition != -1 && currentCeylonPosition != 0) {
                        writer.write("/* " + formatCeylonPosition(currentCeylonPosition) + " */");
                    }
                    previousCeylonPosition = currentCeylonPosition;
                    previousPositionInString = writer.size();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void visitTopLevel(JCCompilationUnit tree) {
            outputCeylonPosition(tree);
            super.visitTopLevel(tree);
        }

        @Override
        public void visitImport(JCImport tree) {
            outputCeylonPosition(tree);
            super.visitImport(tree);
        }

        @Override
        public void visitClassDef(JCClassDecl tree) {
            outputCeylonPosition(tree);
            super.visitClassDef(tree);
        }

        @Override
        public void visitMethodDef(JCMethodDecl tree) {
            outputCeylonPosition(tree);
            super.visitMethodDef(tree);
        }

        @Override
        public void visitVarDef(JCVariableDecl tree) {
            outputCeylonPosition(tree);
            super.visitVarDef(tree);
        }

        @Override
        public void visitSkip(JCSkip tree) {
            outputCeylonPosition(tree);
            super.visitSkip(tree);
        }

        @Override
        public void visitBlock(JCBlock tree) {
            outputCeylonPosition(tree);
            super.visitBlock(tree);
            tree.endpos = currentPosition - 1;
        }

        @Override
        public void visitDoLoop(JCDoWhileLoop tree) {
            outputCeylonPosition(tree);
            super.visitDoLoop(tree);
        }

        @Override
        public void visitWhileLoop(JCWhileLoop tree) {
            outputCeylonPosition(tree);
            super.visitWhileLoop(tree);
        }

        @Override
        public void visitForLoop(JCForLoop tree) {
            outputCeylonPosition(tree);
            super.visitForLoop(tree);
        }

        @Override
        public void visitForeachLoop(JCEnhancedForLoop tree) {
            outputCeylonPosition(tree);
            super.visitForeachLoop(tree);
        }

        @Override
        public void visitLabelled(JCLabeledStatement tree) {
            outputCeylonPosition(tree);
            super.visitLabelled(tree);
        }

        @Override
        public void visitSwitch(JCSwitch tree) {
            outputCeylonPosition(tree);
            super.visitSwitch(tree);
        }

        @Override
        public void visitCase(JCCase tree) {
            outputCeylonPosition(tree);
            super.visitCase(tree);
        }

        @Override
        public void visitSynchronized(JCSynchronized tree) {
            outputCeylonPosition(tree);
            super.visitSynchronized(tree);
        }

        @Override
        public void visitTry(JCTry tree) {
            outputCeylonPosition(tree);
            super.visitTry(tree);
        }

        @Override
        public void visitCatch(JCCatch tree) {
            outputCeylonPosition(tree);
            super.visitCatch(tree);
        }

        @Override
        public void visitConditional(JCConditional tree) {
            outputCeylonPosition(tree);
            super.visitConditional(tree);
        }

        @Override
        public void visitIf(JCIf tree) {
            outputCeylonPosition(tree);
            super.visitIf(tree);
        }

        @Override
        public void visitExec(JCExpressionStatement tree) {
            outputCeylonPosition(tree);
            super.visitExec(tree);
        }

        @Override
        public void visitBreak(JCBreak tree) {
            outputCeylonPosition(tree);
            super.visitBreak(tree);
        }

        @Override
        public void visitContinue(JCContinue tree) {
            outputCeylonPosition(tree);
            super.visitContinue(tree);
        }

        @Override
        public void visitReturn(JCReturn tree) {
            outputCeylonPosition(tree);
            super.visitReturn(tree);
        }

        @Override
        public void visitThrow(JCThrow tree) {
            outputCeylonPosition(tree);
            super.visitThrow(tree);
        }

        @Override
        public void visitAssert(JCAssert tree) {
            outputCeylonPosition(tree);
            super.visitAssert(tree);
        }

        @Override
        public void visitApply(JCMethodInvocation tree) {
            outputCeylonPosition(tree);
            super.visitApply(tree);
        }

        @Override
        public void visitNewClass(JCNewClass tree) {
            outputCeylonPosition(tree);
            super.visitNewClass(tree);
        }

        @Override
        public void visitNewArray(JCNewArray tree) {
            outputCeylonPosition(tree);
            super.visitNewArray(tree);
        }

        @Override
        public void visitParens(JCParens tree) {
            outputCeylonPosition(tree);
            super.visitParens(tree);
        }

        @Override
        public void visitAssign(JCAssign tree) {
            outputCeylonPosition(tree);
            super.visitAssign(tree);
        }

        @Override
        public void visitAssignop(JCAssignOp tree) {
            outputCeylonPosition(tree);
            super.visitAssignop(tree);
        }

        @Override
        public void visitUnary(JCUnary tree) {
            outputCeylonPosition(tree);
            super.visitUnary(tree);
        }

        @Override
        public void visitBinary(JCBinary tree) {
            outputCeylonPosition(tree);
            super.visitBinary(tree);
        }

        @Override
        public void visitTypeCast(JCTypeCast tree) {
            outputCeylonPosition(tree);
            super.visitTypeCast(tree);
        }

        @Override
        public void visitTypeTest(JCInstanceOf tree) {
            outputCeylonPosition(tree);
            super.visitTypeTest(tree);
        }

        @Override
        public void visitIndexed(JCArrayAccess tree) {
            outputCeylonPosition(tree);
            super.visitIndexed(tree);
        }

        @Override
        public void visitSelect(JCFieldAccess tree) {
            outputCeylonPosition(tree);
            super.visitSelect(tree);
        }

        @Override
        public void visitIdent(JCIdent tree) {
            outputCeylonPosition(tree);
            super.visitIdent(tree);
        }

        @Override
        public void visitLiteral(JCLiteral tree) {
            outputCeylonPosition(tree);
            super.visitLiteral(tree);
        }

        @Override
        public void visitTypeIdent(JCPrimitiveTypeTree tree) {
            outputCeylonPosition(tree);
            super.visitTypeIdent(tree);
        }

        @Override
        public void visitTypeArray(JCArrayTypeTree tree) {
            outputCeylonPosition(tree);
            super.visitTypeArray(tree);
        }

        @Override
        public void visitTypeApply(JCTypeApply tree) {
            outputCeylonPosition(tree);
            super.visitTypeApply(tree);
        }

        @Override
        public void visitTypeParameter(JCTypeParameter tree) {
            outputCeylonPosition(tree);
            super.visitTypeParameter(tree);
        }

        @Override
        public void visitWildcard(JCWildcard tree) {
            outputCeylonPosition(tree);
            super.visitWildcard(tree);
        }

        @Override
        public void visitTypeBoundKind(TypeBoundKind tree) {
            outputCeylonPosition(tree);
            super.visitTypeBoundKind(tree);
        }

        @Override
        public void visitErroneous(JCErroneous tree) {
            outputCeylonPosition(tree);
            super.visitErroneous(tree);
        }

        @Override
        public void visitLetExpr(LetExpr tree) {
            outputCeylonPosition(tree);
            super.visitLetExpr(tree);
        }

        @Override
        public void visitModifiers(JCModifiers mods) {
            outputCeylonPosition(mods);
            super.visitModifiers(mods);
        }

        @Override
        public void visitAnnotation(JCAnnotation tree) {
            outputCeylonPosition(tree);
            super.visitAnnotation(tree);
        }

        @Override
        public void visitTree(JCTree tree) {
            outputCeylonPosition(tree);
            super.visitTree(tree);
        }
    };
    printer.visitTopLevel(unit);
    return writer.toString();
}
Also used : JCFieldAccess(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCFieldAccess) JCTypeApply(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeApply) JCAssert(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAssert) JCPrimitiveTypeTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCIf(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCIf) JCEnhancedForLoop(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCEnhancedForLoop) JCNewClass(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass) JCNewArray(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewArray) JCAnnotation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation) JCCase(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCase) JCThrow(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCThrow) JCImport(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCImport) JCWildcard(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCWildcard) JCClassDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCClassDecl) JCIdent(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCIdent) JCMethodDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCMethodDecl) LetExpr(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.LetExpr) JCErroneous(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCErroneous) JCSynchronized(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCSynchronized) JCParens(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCParens) JCDoWhileLoop(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCDoWhileLoop) JCContinue(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCContinue) JCInstanceOf(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCInstanceOf) TypeBoundKind(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.TypeBoundKind) JCTypeParameter(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter) JCMethodInvocation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCMethodInvocation) JCUnary(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCUnary) JCModifiers(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCModifiers) JCCatch(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCatch) JCCompilationUnit(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit) JCWhileLoop(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCWhileLoop) JCReturn(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCReturn) JCLabeledStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCLabeledStatement) JCAssign(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAssign) JCSkip(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCSkip) JCConditional(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCConditional) JCExpressionStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpressionStatement) CharArrayWriter(java.io.CharArrayWriter) JCTypeCast(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeCast) JCArrayTypeTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCArrayTypeTree) JCBlock(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) JCBinary(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBinary) JCArrayAccess(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCArrayAccess) IOException(java.io.IOException) JCForLoop(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCForLoop) JCVariableDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl) Pretty(org.eclipse.ceylon.langtools.tools.javac.tree.Pretty) JCTry(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTry) JCSwitch(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCSwitch) JCLiteral(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCLiteral) JCAssignOp(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAssignOp) JCBreak(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBreak)

Aggregations

JCReturn (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCReturn)4 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)3 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)3 Expression (org.eclipse.ceylon.compiler.typechecker.tree.Tree.Expression)2 JCBlock (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock)2 JCStatement (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)2 CharArrayWriter (java.io.CharArrayWriter)1 IOException (java.io.IOException)1 BoxingStrategy (org.eclipse.ceylon.compiler.java.codegen.AbstractTransformer.BoxingStrategy)1 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)1 QualifiedTypeExpression (org.eclipse.ceylon.compiler.typechecker.tree.Tree.QualifiedTypeExpression)1 StaticMemberOrTypeExpression (org.eclipse.ceylon.compiler.typechecker.tree.Tree.StaticMemberOrTypeExpression)1 JCAnnotation (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation)1 JCArrayAccess (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCArrayAccess)1 JCArrayTypeTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCArrayTypeTree)1 JCAssert (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAssert)1 JCAssign (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAssign)1 JCAssignOp (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAssignOp)1 JCBinary (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBinary)1 JCBreak (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBreak)1