Search in sources :

Example 1 with JCExpressionStatement

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

the class ClassTransformer method makeCompanionInstanceAssignment.

/**
 * Returns the companion instances assignment expression used in the constructor,
 * e.g.
 * <pre>
 * this.$ceylon$language$Enumerable$this$ = new .ceylon.language.Enumerable$impl<.org.eclipse.ceylon.compiler.java.test.structure.klass.SerializableEnumerable>(.org.eclipse.ceylon.compiler.java.test.structure.klass.SerializableEnumerable.$TypeDescriptor$, this);
 * </pre>
 * @param classBuilder
 * @return
 */
private JCExpressionStatement makeCompanionInstanceAssignment(final Class model, final Interface iface, final Type satisfiedType) {
    final Type bestSatisfiedType = getBestSatisfiedType(model.getType(), iface);
    JCExpression containerInstance = null;
    if (!iface.isToplevel() && !Decl.isLocal(iface)) {
        // if it's a member type we need to qualify the new instance with its $impl container
        ClassOrInterface interfaceContainer = ModelUtil.getClassOrInterfaceContainer(iface, false);
        if (interfaceContainer instanceof Interface) {
            ClassOrInterface modelContainer = model;
            // first try to find exactly the interface we are looking for
            while ((modelContainer = ModelUtil.getClassOrInterfaceContainer(modelContainer, false)) != null && !modelContainer.equals(interfaceContainer)) {
            // keep searching
            }
            // then find one that inherits it
            if (modelContainer == null) {
                modelContainer = model;
                while ((modelContainer = ModelUtil.getClassOrInterfaceContainer(modelContainer, false)) != null && modelContainer.getType().getSupertype(interfaceContainer) == null) {
                // keep searching
                }
            }
            if (modelContainer == null) {
                throw new BugException("Could not find container that satisfies interface " + iface.getQualifiedNameString() + " to find qualifying instance for companion instance for " + model.getQualifiedNameString());
            }
            // if it's an interface we just qualify it properly
            if (modelContainer instanceof Interface) {
                JCExpression containerType = makeJavaType(modelContainer.getType(), JT_COMPANION | JT_SATISFIES | JT_RAW);
                containerInstance = makeSelect(containerType, "this");
            } else {
                // it's a class: find the right field used for the interface container impl
                String containerFieldName = getCompanionFieldName((Interface) interfaceContainer);
                JCExpression containerType = makeJavaType(modelContainer.getType(), JT_SATISFIES);
                containerInstance = makeSelect(makeSelect(containerType, "this"), containerFieldName);
            }
        }
    }
    List<JCExpression> state = List.nil();
    // pass all reified type info to the constructor
    for (JCExpression t : makeReifiedTypeArguments(satisfiedType)) {
        state = state.append(t);
    }
    // pass the instance of this
    state = state.append(expressionGen().applyErasureAndBoxing(naming.makeThis(), model.getType(), false, true, BoxingStrategy.BOXED, bestSatisfiedType, ExpressionTransformer.EXPR_FOR_COMPANION));
    final JCExpression ifaceImplType;
    if (!iface.isToplevel() && !Decl.isLocal(iface) && ModelUtil.getClassOrInterfaceContainer(iface, false) instanceof Interface) {
        ifaceImplType = makeJavaType(bestSatisfiedType, JT_COMPANION | JT_CLASS_NEW | JT_NON_QUALIFIED);
    } else {
        ifaceImplType = makeJavaType(bestSatisfiedType, JT_COMPANION | JT_CLASS_NEW);
    }
    JCExpression newInstance = make().NewClass(containerInstance, null, ifaceImplType, state, null);
    JCExpressionStatement companionInstanceAssign = make().Exec(make().Assign(// TODO Use qualified name for quoting?
    makeSelect("this", getCompanionFieldName(iface)), newInstance));
    return companionInstanceAssign;
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) Type(org.eclipse.ceylon.model.typechecker.model.Type) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) JCExpressionStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpressionStatement) Interface(org.eclipse.ceylon.model.typechecker.model.Interface) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface)

Example 2 with JCExpressionStatement

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

the class ClassTransformer method serializationReferences.

/**
 * <p>Generates the {@code $deserialize$()} method to deserialize
 * the classes state, which:</p>
 * <ul>
 * <li>invokes {@code super.$deserialize$()}, if the super class is also
 *     serializable,</li>
 * <li>assigns each reified type argument in the
 *     class by invoking {@code dted.getTypeArgument()},</li>
 * <li>assigns each field in the
 *     class by invoking {@code dted.getValue()}.</li>
 * </ul>
 */
private void serializationReferences(Class model, ClassDefinitionBuilder classBuilder) {
    MethodDefinitionBuilder mdb = MethodDefinitionBuilder.systemMethod(this, Unfix.$references$.toString());
    mdb.isOverride(true);
    mdb.ignoreModelAnnotations();
    mdb.modifiers(PUBLIC);
    mdb.resultType(new TransformedType(make().TypeApply(naming.makeQuotedFQIdent("java.util.Collection"), List.<JCExpression>of(make().Type(syms().ceylonReachableReferenceType))), null, makeAtNonNull()));
    ListBuffer<JCStatement> stmts = new ListBuffer<JCStatement>();
    // TODO this is all static information, but the method itself needs to be
    // callable virtually, so we should cache it somehow.
    SyntheticName r = naming.synthetic(Unfix.reference);
    if (extendsSerializable(model)) {
        // prepend the invocation of super.$serialize$()
        stmts.add(makeVar(r, make().TypeApply(naming.makeQuotedFQIdent("java.util.Collection"), List.<JCExpression>of(make().Type(syms().ceylonReachableReferenceType))), make().Apply(null, naming.makeQualIdent(naming.makeSuper(), Unfix.$references$.toString()), List.<JCExpression>nil())));
    } else {
        stmts.add(makeVar(r, make().TypeApply(naming.makeQuotedFQIdent("java.util.Collection"), List.<JCExpression>of(make().Type(syms().ceylonReachableReferenceType))), make().NewClass(null, null, make().TypeApply(naming.makeQuotedFQIdent("java.util.ArrayList"), List.<JCExpression>of(make().Type(syms().ceylonReachableReferenceType))), List.<JCExpression>nil(), null)));
    }
    if (model.isMember()) {
        JCExpressionStatement outer = make().Exec(make().Apply(null, naming.makeQualIdent(r.makeIdent(), "add"), List.<JCExpression>of(make().Apply(null, naming.makeQualIdent(make().Type(syms().ceylonOuterImplType), "get_"), List.<JCExpression>nil()))));
        stmts.add(outer);
    }
    for (Declaration member : model.getMembers()) {
        if (hasField(member)) {
            // Obtain a ValueDeclaration
            JCExpression valueDeclaration = expressionGen().makeMemberValueOrFunctionDeclarationLiteral(null, member, false);
            // Create a MemberImpl
            JCExpression mi = make().NewClass(null, null, make().QualIdent(syms().ceylonMemberImplType.tsym), List.of(valueDeclaration), null);
            JCExpressionStatement attribute = make().Exec(make().Apply(null, naming.makeQualIdent(r.makeIdent(), "add"), List.of(mi)));
            stmts.add(attribute);
        }
    }
    stmts.add(make().Return(r.makeIdent()));
    mdb.body(stmts.toList());
    classBuilder.method(mdb);
}
Also used : JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) SyntheticName(org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) MethodDeclaration(org.eclipse.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration) JCExpressionStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpressionStatement)

Example 3 with JCExpressionStatement

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

JCExpressionStatement (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpressionStatement)3 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)2 CharArrayWriter (java.io.CharArrayWriter)1 IOException (java.io.IOException)1 SyntheticName (org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName)1 MethodDeclaration (org.eclipse.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration)1 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)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 JCBlock (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock)1 JCBreak (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBreak)1 JCCase (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCase)1 JCCatch (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCatch)1 JCClassDecl (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCClassDecl)1 JCCompilationUnit (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit)1