Search in sources :

Example 1 with JCMethodInvocation

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

the class StatementTransformer method transform.

public JCStatement transform(Tree.TryCatchStatement t) {
    Tree.TryClause tryClause = t.getTryClause();
    at(tryClause);
    JCBlock tryBlock = transform(tryClause.getBlock());
    Tree.ResourceList resList = tryClause.getResourceList();
    if (resList != null) {
        ArrayList<Tree.Resource> resources = new ArrayList<Tree.Resource>(resList.getResources());
        Collections.reverse(resources);
        for (Tree.Resource res : resources) {
            List<JCStatement> stats = List.nil();
            Tree.Expression resExpr;
            String resVarName;
            if (res.getExpression() != null) {
                resExpr = res.getExpression();
                resVarName = naming.newTemp("try");
            } else if (res.getVariable() != null) {
                Tree.Variable var = res.getVariable();
                resExpr = var.getSpecifierExpression().getExpression();
                resVarName = var.getIdentifier().getText();
            } else {
                throw new BugException(res, "missing resource expression");
            }
            final TryResourceTransformation resourceTx;
            if (typeFact().getDestroyableType().isSupertypeOf(resExpr.getTypeModel())) {
                resourceTx = destroyableResource;
            } else if (typeFact().getObtainableType().isSupertypeOf(resExpr.getTypeModel())) {
                resourceTx = obtainableResource;
            } else if (javacJavaTypeToProducedType(syms().autoCloseableType).isSupertypeOf(resExpr.getTypeModel())) {
                resourceTx = javaAutoCloseableResource;
            } else {
                throw BugException.unhandledTypeCase(resExpr.getTypeModel());
            }
            Type resVarType = resExpr.getTypeModel();
            Type resVarExpectedType = resourceTx.getType();
            // CloseableType $var = resource-expression
            JCExpression expr = expressionGen().transformExpression(resExpr);
            JCExpression javaType = makeJavaType(resVarType);
            JCVariableDecl var = makeVar(FINAL, resVarName, javaType, expr);
            stats = stats.append(var);
            if (resourceTx.getInitMethodName() != null) {
                JCExpression resVar0 = expressionGen().applyErasureAndBoxing(makeUnquotedIdent(resVarName), resVarType, true, BoxingStrategy.BOXED, resVarExpectedType);
                JCMethodInvocation openCall = make().Apply(null, makeQualIdent(resVar0, resourceTx.getInitMethodName()), List.<JCExpression>nil());
                stats = stats.append(make().Exec(openCall));
            }
            // Exception $tpmex = null;
            String innerExTmpVarName = naming.newTemp("ex");
            JCExpression innerExType = makeJavaType(typeFact().getThrowableType(), JT_CATCH);
            JCVariableDecl innerExTmpVar = makeVar(innerExTmpVarName, innerExType, makeNull());
            stats = stats.append(innerExTmpVar);
            // $tmpex = ex;
            List<JCStatement> innerCatchStats = List.nil();
            Name innerCatchVarName = naming.tempName("ex");
            JCAssign exTmpAssign = make().Assign(makeUnquotedIdent(innerExTmpVarName), make().Ident(innerCatchVarName));
            innerCatchStats = innerCatchStats.append(make().Exec(exTmpAssign));
            // throw ex;
            JCThrow innerCatchThrow = make().Throw(make().Ident(innerCatchVarName));
            innerCatchStats = innerCatchStats.append(innerCatchThrow);
            JCBlock innerCatchBlock = make().Block(0, innerCatchStats);
            // $var.close() /// ((Closeable)$var).close()
            JCExpression exarg = makeUnquotedIdent(innerExTmpVarName);
            JCExpression resVar1 = expressionGen().applyErasureAndBoxing(makeUnquotedIdent(resVarName), resVarType, true, BoxingStrategy.BOXED, resVarExpectedType);
            JCExpression closeCall = resourceTx.makeRecover(resVar1, exarg);
            JCBlock closeTryBlock = make().Block(0, List.<JCStatement>of(make().Exec(closeCall)));
            // try { $var.close() } catch (Exception closex) { $tmpex.addSuppressed(closex); }
            Name closeCatchVarName = naming.tempName("closex");
            JCExpression closeCatchExType = makeJavaType(typeFact().getThrowableType(), JT_CATCH);
            JCVariableDecl closeCatchVar = make().VarDef(make().Modifiers(Flags.FINAL), closeCatchVarName, closeCatchExType, null);
            JCExpression addarg = make().Ident(closeCatchVarName);
            JCMethodInvocation addSuppressedCall = make().Apply(null, makeQualIdent(makeUnquotedIdent(innerExTmpVarName), "addSuppressed"), List.<JCExpression>of(addarg));
            JCStatement catchForClose;
            if (resourceTx != javaAutoCloseableResource) {
                // Obtainable.release() and Destroyable.close() could
                // rethrow the originating exception, so guard against
                // self-supression (which causes addSuppressed() to throw
                catchForClose = make().If(make().Binary(JCTree.Tag.NE, makeUnquotedIdent(innerExTmpVarName), make().Ident(closeCatchVarName)), make().Block(0, List.<JCStatement>of(make().Exec(addSuppressedCall))), null);
            } else {
                // AutoClosable can't rethrow the originating exception,
                // so no need to worry about self suppression
                catchForClose = make().Exec(addSuppressedCall);
            }
            JCCatch closeCatch = make().Catch(closeCatchVar, make().Block(0, List.<JCStatement>of(catchForClose)));
            JCTry closeTry = at(res).Try(closeTryBlock, List.<JCCatch>of(closeCatch), null);
            // $var.close() /// ((Closeable)$var).close()
            JCExpression exarg2 = makeUnquotedIdent(innerExTmpVarName);
            JCExpression resVar2 = expressionGen().applyErasureAndBoxing(makeUnquotedIdent(resVarName), resVarType, true, BoxingStrategy.BOXED, resVarExpectedType);
            JCExpression closeCall2 = resourceTx.makeRecover(resVar1, exarg);
            // if ($tmpex != null) { ... } else { ... }
            JCBinary closeCatchCond = make().Binary(JCTree.Tag.NE, makeUnquotedIdent(innerExTmpVarName), makeNull());
            JCIf closeCatchIf = make().If(closeCatchCond, make().Block(0, List.<JCStatement>of(closeTry)), make().Block(0, List.<JCStatement>of(make().Exec(closeCall2))));
            // try { .... } catch (Exception ex) { $tmpex=ex; throw ex; }
            // finally { try { $var.close() } catch (Exception closex) { } }
            JCExpression innerCatchExType = makeJavaType(typeFact().getThrowableType(), JT_CATCH);
            JCVariableDecl innerCatchVar = make().VarDef(make().Modifiers(Flags.FINAL), innerCatchVarName, innerCatchExType, null);
            JCCatch innerCatch = make().Catch(innerCatchVar, innerCatchBlock);
            JCBlock innerFinallyBlock = make().Block(0, List.<JCStatement>of(closeCatchIf));
            JCTry innerTry = at(res).Try(tryBlock, List.<JCCatch>of(innerCatch), innerFinallyBlock);
            stats = stats.append(innerTry);
            tryBlock = at(res).Block(0, stats);
        }
    }
    final List<JCCatch> catches;
    if (usePolymorphicCatches(t.getCatchClauses())) {
        catches = transformCatchesPolymorphic(t.getCatchClauses());
    } else {
        catches = transformCatchesIfElseIf(t.getCatchClauses());
    }
    final JCBlock finallyBlock;
    Tree.FinallyClause finallyClause = t.getFinallyClause();
    if (finallyClause != null) {
        at(finallyClause);
        finallyBlock = transform(finallyClause.getBlock());
    } else {
        finallyBlock = null;
    }
    if (!catches.isEmpty() || finallyBlock != null) {
        return at(t).Try(tryBlock, catches, finallyBlock);
    } else {
        return tryBlock;
    }
}
Also used : Variable(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Variable) JCAssign(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAssign) ArrayList(java.util.ArrayList) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement) Name(org.eclipse.ceylon.langtools.tools.javac.util.Name) CName(org.eclipse.ceylon.compiler.java.codegen.Naming.CName) SyntheticName(org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName) JCIf(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCIf) Expression(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Expression) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) JCThrow(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCThrow) JCBlock(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock) JCBinary(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBinary) JCVariableDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl) JCTry(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTry) JCMethodInvocation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCMethodInvocation) Type(org.eclipse.ceylon.model.typechecker.model.Type) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) JCCatch(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCatch)

Example 2 with JCMethodInvocation

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

the class ExpressionTransformer method transformSpreadOperator.

private JCExpression transformSpreadOperator(final Tree.QualifiedMemberOrTypeExpression expr, TermTransformer transformer) {
    at(expr);
    boolean spreadMethodReferenceOuter = !expr.equals(spreading) && !isWithinInvocation() && isCeylonCallableSubtype(expr.getTypeModel());
    boolean spreadMethodReferenceInner = expr.equals(spreading) && isWithinInvocation();
    Tree.QualifiedMemberOrTypeExpression oldSpreading = spreading;
    if (spreadMethodReferenceOuter) {
        spreading = expr;
    }
    try {
        Naming.SyntheticName varBaseName = naming.alias("spread");
        ListBuffer<JCStatement> letStmts = new ListBuffer<JCStatement>();
        final Naming.SyntheticName srcIterableName;
        if (spreadMethodReferenceInner) {
            // use the var we initialized in the outer
            srcIterableName = this.memberPrimary;
        } else {
            srcIterableName = varBaseName.suffixedBy(Suffix.$iterable$);
        }
        if (spreadMethodReferenceOuter) {
            // if we're in the outer, note then name of the var for use in the inner.
            this.memberPrimary = srcIterableName;
        }
        Naming.SyntheticName srcIteratorName = varBaseName.suffixedBy(Suffix.$iterator$);
        Type srcElementType = expr.getTarget().getQualifyingType();
        JCExpression srcIterableExpr;
        boolean isSuperOrSuperOf = false;
        Type srcIterableType;
        if (typeFact().isIterableType(expr.getPrimary().getTypeModel())) {
            srcIterableType = typeFact().getIterableType(srcElementType);
        } else if (typeFact().isJavaIterableType(expr.getPrimary().getTypeModel())) {
            srcIterableType = typeFact().getJavaIterableDeclaration().appliedType(null, Collections.singletonList(srcElementType));
        } else if (typeFact().isJavaArrayType(expr.getPrimary().getTypeModel())) {
            srcIterableType = expr.getPrimary().getTypeModel();
            srcElementType = typeFact().getJavaArrayElementType(srcIterableType);
        } else {
            return makeErroneous(expr, "unhandled iterable type");
        }
        if (spreadMethodReferenceInner) {
            srcIterableExpr = srcIterableName.makeIdent();
        } else {
            boolean isSuper = isSuper(expr.getPrimary());
            isSuperOrSuperOf = isSuper || isSuperOf(expr.getPrimary());
            if (isSuperOrSuperOf) {
                // so we just refer to it later
                if (isSuper) {
                    Declaration member = expr.getPrimary().getTypeModel().getDeclaration().getMember("iterator", null, false);
                    srcIterableExpr = transformSuper(expr, (TypeDeclaration) member.getContainer());
                } else
                    srcIterableExpr = transformSuperOf(expr, expr.getPrimary(), "iterator");
            } else {
                srcIterableExpr = transformExpression(expr.getPrimary(), BoxingStrategy.BOXED, srcIterableType);
            }
        }
        // do not capture the iterable for super invocations: see above
        if (!spreadMethodReferenceInner && !isSuperOrSuperOf) {
            JCVariableDecl srcIterable = null;
            JCExpression srcIterableTypeExpr = makeJavaType(srcIterableType, JT_NO_PRIMITIVES);
            srcIterable = makeVar(Flags.FINAL, srcIterableName, srcIterableTypeExpr, srcIterableExpr);
            letStmts.prepend(srcIterable);
        }
        /* public Object next() {
             *     Object result;
             *     if (!((result = iterator.next()) instanceof Finished)) {
             *         result = transformedMember(result);
             *     }
             *     return result;
             */
        /* Any arguments in the member of the spread would get re-evaluated on each iteration
             * so we need to shift them to the scope of the Let to ensure they're evaluated once. 
             */
        boolean aliasArguments = transformer instanceof InvocationTermTransformer && ((InvocationTermTransformer) transformer).invocation.getNode() instanceof Tree.InvocationExpression && ((Tree.InvocationExpression) ((InvocationTermTransformer) transformer).invocation.getNode()).getPositionalArgumentList() != null;
        if (aliasArguments) {
            ((InvocationTermTransformer) transformer).callBuilder.argumentHandling(CallBuilder.CB_ALIAS_ARGS, varBaseName);
        }
        JCNewClass iterableClass;
        boolean prevSyntheticClassBody = expressionGen().withinSyntheticClassBody(true);
        try {
            Naming.SyntheticName iteratorResultName = varBaseName.suffixedBy(Suffix.$element$);
            JCExpression transformedElement = applyErasureAndBoxing(iteratorResultName.makeIdent(), typeFact().getAnythingType(), CodegenUtil.hasTypeErased(expr.getPrimary()), true, BoxingStrategy.BOXED, srcElementType, 0);
            transformedElement = transformMemberExpression(expr, transformedElement, transformer);
            // be handled by the previous recursion
            if (spreadMethodReferenceOuter) {
                return make().LetExpr(letStmts.toList(), transformedElement);
            }
            Type resultElementType = expr.getTarget().getType();
            final Type resultAbsentType;
            transformedElement = applyErasureAndBoxing(transformedElement, resultElementType, // not necessarily of the applied member
            expr.getTarget().getDeclaration() instanceof TypedDeclaration ? CodegenUtil.hasTypeErased((TypedDeclaration) expr.getTarget().getDeclaration()) : false, !CodegenUtil.isUnBoxed(expr), BoxingStrategy.BOXED, resultElementType, 0);
            MethodDefinitionBuilder nextMdb = MethodDefinitionBuilder.systemMethod(this, "next");
            nextMdb.isOverride(true);
            nextMdb.annotationFlags(Annotations.IGNORE);
            nextMdb.modifiers(Flags.PUBLIC | Flags.FINAL);
            nextMdb.resultType(new TransformedType(make().Type(syms().objectType)));
            final List<JCTree> l;
            if (typeFact().isIterableType(expr.getPrimary().getTypeModel())) {
                // private Iterator<srcElementType> iterator = srcIterableName.iterator();
                JCVariableDecl srcIterator = makeVar(Flags.FINAL, srcIteratorName, makeJavaType(typeFact().getIteratorType(srcElementType)), make().Apply(null, // for super we do not capture it because we can't and it's constant anyways
                naming.makeQualIdent(isSuperOrSuperOf ? srcIterableExpr : srcIterableName.makeIdent(), "iterator"), List.<JCExpression>nil()));
                resultAbsentType = typeFact().getIteratedAbsentType(expr.getPrimary().getTypeModel());
                nextMdb.body(List.of(makeVar(iteratorResultName, make().Type(syms().objectType), null), make().If(make().Unary(JCTree.Tag.NOT, make().TypeTest(make().Assign(iteratorResultName.makeIdent(), make().Apply(null, naming.makeQualIdent(srcIteratorName.makeIdent(), "next"), List.<JCExpression>nil())), make().Type(syms().ceylonFinishedType))), make().Block(0, List.<JCStatement>of(make().Exec(make().Assign(iteratorResultName.makeIdent(), transformedElement)))), null), make().Return(iteratorResultName.makeIdent())));
                l = List.of(srcIterator, nextMdb.build());
            } else if (typeFact().isJavaIterableType(expr.getPrimary().getTypeModel())) {
                // private Iterator<srcElementType> iterator = srcIterableName.iterator();
                JCVariableDecl srcIterator = makeVar(Flags.PRIVATE | Flags.FINAL, srcIteratorName, makeJavaType(typeFact().getJavaIteratorType(srcElementType)), make().Apply(null, // for super we do not capture it because we can't and it's constant anyways
                naming.makeQualIdent(isSuperOrSuperOf ? srcIterableExpr : srcIterableName.makeIdent(), "iterator"), List.<JCExpression>nil()));
                resultAbsentType = typeFact().getNullType();
                nextMdb.body(List.<JCStatement>of(make().If(make().Apply(null, naming.makeQualIdent(srcIteratorName.makeIdent(), "hasNext"), List.<JCExpression>nil()), make().Block(0, List.<JCStatement>of(makeVar(iteratorResultName, make().Type(syms().objectType), make().Apply(null, naming.makeQualIdent(srcIteratorName.makeIdent(), "next"), List.<JCExpression>nil())), make().Return(transformedElement))), make().Return(makeFinished()))));
                l = List.of(srcIterator, nextMdb.build());
            } else if (typeFact().isJavaArrayType(expr.getPrimary().getTypeModel())) {
                resultAbsentType = typeFact().getNullType();
                JCVariableDecl srcIndex = makeVar(Flags.PRIVATE, srcIteratorName, make().Type(syms().intType), make().Literal(0));
                JCExpression indexed = make().Indexed(srcIterableName.makeIdent(), make().Unary(Tag.POSTINC, srcIteratorName.makeIdent()));
                if (typeFact().isJavaPrimitiveArrayType(expr.getPrimary().getTypeModel())) {
                    indexed = applyErasureAndBoxing(indexed, srcElementType, false, BoxingStrategy.BOXED, srcElementType);
                }
                nextMdb.body(List.<JCStatement>of(make().If(make().Binary(Tag.LT, srcIteratorName.makeIdent(), // for super we do not capture it because we can't and it's constant anyways
                naming.makeQualIdent(isSuperOrSuperOf ? srcIterableExpr : srcIterableName.makeIdent(), "length")), make().Block(0, List.<JCStatement>of(makeVar(iteratorResultName, make().Type(syms().objectType), indexed), make().Return(transformedElement))), make().Return(makeFinished()))));
                l = List.of(srcIndex, nextMdb.build());
            } else {
                return makeErroneous(expr, "unhandled iterable type");
            }
            // new AbstractIterator()
            JCNewClass iteratorClass = make().NewClass(null, null, make().TypeApply(make().QualIdent(syms().ceylonAbstractIteratorType.tsym), List.of(makeJavaType(resultElementType, JT_TYPE_ARGUMENT))), List.of(makeReifiedTypeArgument(resultElementType)), make().AnonymousClassDef(make().Modifiers(0), l));
            MethodDefinitionBuilder iteratorMdb = MethodDefinitionBuilder.systemMethod(this, "iterator");
            iteratorMdb.isOverride(true);
            iteratorMdb.annotationFlags(Annotations.IGNORE);
            iteratorMdb.modifiers(Flags.PUBLIC | Flags.FINAL);
            iteratorMdb.resultType(new TransformedType(makeJavaType(typeFact().getIteratorType(resultElementType)), makeAtNonNull()));
            iteratorMdb.body(make().Return(iteratorClass));
            // new AbstractIterable()
            iterableClass = make().NewClass(null, null, make().TypeApply(make().QualIdent(syms().ceylonAbstractIterableType.tsym), List.of(makeJavaType(resultElementType, JT_TYPE_ARGUMENT), makeJavaType(resultAbsentType, JT_TYPE_ARGUMENT))), List.of(makeReifiedTypeArgument(resultElementType), makeReifiedTypeArgument(resultAbsentType)), make().AnonymousClassDef(make().Modifiers(0), List.<JCTree>of(iteratorMdb.build())));
        } finally {
            expressionGen().withinSyntheticClassBody(prevSyntheticClassBody);
        }
        if (aliasArguments) {
            letStmts = letStmts.appendList(((InvocationTermTransformer) transformer).callBuilder.getStatements());
        }
        JCMethodInvocation result = make().Apply(null, naming.makeQualIdent(iterableClass, "sequence"), List.<JCExpression>nil());
        JCExpression spread = letStmts.isEmpty() ? result : make().LetExpr(letStmts.toList(), result);
        // Do we *statically* know the result must be a Sequence
        final boolean primaryIsSequence = typeFact().isNonemptyIterableType(expr.getPrimary().getTypeModel());
        Type returnElementType = expr.getTarget().getType();
        if (primaryIsSequence) {
            int flags = EXPR_DOWN_CAST;
            spread = applyErasureAndBoxing(spread, typeFact().getSequentialType(returnElementType), false, true, BoxingStrategy.BOXED, primaryIsSequence ? typeFact().getSequenceType(returnElementType) : typeFact().getSequentialType(returnElementType), flags);
        }
        return spread;
    } finally {
        spreading = oldSpreading;
    }
}
Also used : TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl) JCMethodInvocation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCMethodInvocation) 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) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) JCNewClass(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass) SyntheticName(org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 3 with JCMethodInvocation

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

the class ExpressionTransformer method transformStringExpression.

public JCExpression transformStringExpression(Tree.StringTemplate expr) {
    at(expr);
    JCExpression builder;
    builder = make().NewClass(null, null, naming.makeFQIdent("java", "lang", "StringBuilder"), List.<JCExpression>nil(), null);
    java.util.List<Tree.StringLiteral> literals = expr.getStringLiterals();
    java.util.List<Tree.Expression> expressions = expr.getExpressions();
    for (int ii = 0; ii < literals.size(); ii += 1) {
        Tree.StringLiteral literal = literals.get(ii);
        if (!literal.getText().isEmpty()) {
            // ignore empty string literals
            at(literal);
            builder = make().Apply(null, makeSelect(builder, "append"), List.<JCExpression>of(transform(literal)));
        }
        if (ii == expressions.size()) {
            // after that because we've already exhausted all the expressions
            break;
        }
        Tree.Expression expression = expressions.get(ii);
        at(expression);
        // Here in both cases we don't need a type cast for erasure
        if (isCeylonBasicType(expression.getTypeModel()) && expression.getUnboxed()) {
            // TODO: Test should be erases to String, long, int, boolean, char, byte, float, double
            // If erases to a Java primitive just call append, don't box it just to call format.
            String method = isCeylonCharacter(expression.getTypeModel()) ? "appendCodePoint" : "append";
            builder = make().Apply(null, makeSelect(builder, method), List.<JCExpression>of(transformExpression(expression, BoxingStrategy.UNBOXED, null)));
        } else {
            JCMethodInvocation formatted = make().Apply(null, makeSelect(transformExpression(expression), "toString"), List.<JCExpression>nil());
            builder = make().Apply(null, makeSelect(builder, "append"), List.<JCExpression>of(formatted));
        }
    }
    return make().Apply(null, makeSelect(builder, "toString"), List.<JCExpression>nil());
}
Also used : JCMethodInvocation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCMethodInvocation) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) Expression(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Expression) LetExpression(org.eclipse.ceylon.compiler.typechecker.tree.Tree.LetExpression) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) 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)

Example 4 with JCMethodInvocation

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCMethodInvocation 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)

Example 5 with JCMethodInvocation

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

the class AbstractTransformer method makeTypedDeclarationTypeDescriptorResolved.

private JCExpression makeTypedDeclarationTypeDescriptorResolved(TypedDeclaration declaration, TypeArgumentAccessor typeArgumentAccessor) {
    // figure out the method name
    String methodName = declaration.getPrefixedName();
    List<JCExpression> arguments;
    if (declaration instanceof Function)
        arguments = makeReifiedTypeArgumentsResolved(getTypeArguments((Function) declaration), true, typeArgumentAccessor);
    else
        arguments = List.nil();
    if (declaration.isToplevel()) {
        JCExpression getterClassNameExpr;
        if (declaration instanceof Function) {
            getterClassNameExpr = naming.makeName(declaration, Naming.NA_FQ | Naming.NA_WRAPPER);
        } else {
            String getterClassName = Naming.getAttrClassName(declaration, 0);
            getterClassNameExpr = naming.makeUnquotedIdent(getterClassName);
        }
        arguments = arguments.prepend(makeSelect(getterClassNameExpr, "class"));
    } else
        arguments = arguments.prepend(make().Literal(methodName));
    JCMethodInvocation typedDeclarationDescriptor = make().Apply(null, makeSelect(makeTypeDescriptorType(), "functionOrValue"), arguments);
    // see if the declaration has a container too
    Declaration enclosingDeclaration = getDeclarationContainer(declaration);
    JCExpression containerType = null;
    if (enclosingDeclaration instanceof TypedDeclaration)
        containerType = makeTypedDeclarationTypeDescriptorResolved((TypedDeclaration) enclosingDeclaration, typeArgumentAccessor);
    else if (enclosingDeclaration instanceof TypeDeclaration) {
        Type qualifyingType = ((TypeDeclaration) enclosingDeclaration).getType();
        containerType = makeReifiedTypeArgumentResolved(qualifyingType, true);
    }
    if (containerType == null) {
        return typedDeclarationDescriptor;
    } else {
        return make().Apply(null, makeSelect(makeTypeDescriptorType(), "member"), List.of(containerType, typedDeclarationDescriptor));
    }
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) JCMethodInvocation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCMethodInvocation) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Aggregations

JCMethodInvocation (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCMethodInvocation)5 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)4 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)4 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)3 JCVariableDecl (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl)3 Type (org.eclipse.ceylon.model.typechecker.model.Type)3 SyntheticName (org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName)2 Expression (org.eclipse.ceylon.compiler.typechecker.tree.Tree.Expression)2 JCAssign (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAssign)2 JCBinary (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBinary)2 JCBlock (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock)2 JCCatch (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCatch)2 JCIf (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCIf)2 JCNewClass (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass)2 JCStatement (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)2 JCThrow (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCThrow)2 JCTry (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTry)2 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)2 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)2 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)2