Search in sources :

Example 1 with MethodDeclaration

use of com.redhat.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration in project ceylon-compiler by ceylon.

the class ClassTransformer method transformMethodBody.

private List<JCStatement> transformMethodBody(Tree.AnyMethod def) {
    List<JCStatement> body = null;
    final Function model = def.getDeclarationModel();
    if (model.isDeferred()) {
        // Uninitialized or deferred initialized method => Make a Callable field
        String fieldName = naming.selector(model);
        final Parameter initializingParameter = CodegenUtil.findParamForDecl(def);
        int mods = PRIVATE;
        JCExpression initialValue;
        if (initializingParameter != null) {
            mods |= FINAL;
            initialValue = makeUnquotedIdent(Naming.getAliasedParameterName(initializingParameter));
        } else {
            // The field isn't initialized by a parameter, but later in the block
            initialValue = makeNull();
        }
        Type callableType = model.getReference().getFullType();
        current().field(mods, fieldName, makeJavaType(callableType), initialValue, false);
        Invocation invocation = new CallableSpecifierInvocation(this, model, makeUnquotedIdent(fieldName), // but with deferred methods we can't define them so that they are erased so we're good
        null, def);
        invocation.handleBoxing(true);
        JCExpression call = expressionGen().transformInvocation(invocation);
        JCStatement stmt;
        if (!isVoid(def) || !Decl.isUnboxedVoid(model) || Strategy.useBoxedVoid((Function) model)) {
            stmt = make().Return(call);
        } else {
            stmt = make().Exec(call);
        }
        JCStatement result;
        if (initializingParameter == null) {
            // If the field isn't initialized by a parameter we have to 
            // cope with the possibility that it's never initialized
            final JCBinary cond = make().Binary(JCTree.EQ, makeUnquotedIdent(fieldName), makeNull());
            final JCStatement throw_ = make().Throw(make().NewClass(null, null, makeIdent(syms().ceylonUninitializedMethodErrorType), List.<JCExpression>nil(), null));
            result = make().If(cond, throw_, stmt);
        } else {
            result = stmt;
        }
        return List.<JCStatement>of(result);
    } else if (def instanceof Tree.MethodDefinition) {
        body = transformMethodBlock((Tree.MethodDefinition) def);
    } else if (def instanceof MethodDeclaration && ((MethodDeclaration) def).getSpecifierExpression() != null) {
        body = transformSpecifiedMethodBody((MethodDeclaration) def, ((MethodDeclaration) def).getSpecifierExpression());
    }
    return body;
}
Also used : JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) MethodDeclaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration) JCBinary(com.sun.tools.javac.tree.JCTree.JCBinary) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) Function(com.redhat.ceylon.model.typechecker.model.Function) Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) JCPrimitiveTypeTree(com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree)

Aggregations

Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)1 MethodDeclaration (com.redhat.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration)1 Function (com.redhat.ceylon.model.typechecker.model.Function)1 Parameter (com.redhat.ceylon.model.typechecker.model.Parameter)1 Type (com.redhat.ceylon.model.typechecker.model.Type)1 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)1 JCTree (com.sun.tools.javac.tree.JCTree)1 JCBinary (com.sun.tools.javac.tree.JCTree.JCBinary)1 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)1 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)1 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)1 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)1