Search in sources :

Example 1 with MethodDeclaration

use of org.eclipse.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration in project ceylon by eclipse.

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.Tag.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 : MethodDeclaration(org.eclipse.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration) JCBinary(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBinary) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement) Function(org.eclipse.ceylon.model.typechecker.model.Function) Type(org.eclipse.ceylon.model.typechecker.model.Type) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCPrimitiveTypeTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree)

Aggregations

Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)1 MethodDeclaration (org.eclipse.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration)1 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)1 JCBinary (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBinary)1 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)1 JCPrimitiveTypeTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCPrimitiveTypeTree)1 JCStatement (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)1 Function (org.eclipse.ceylon.model.typechecker.model.Function)1 Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)1 Type (org.eclipse.ceylon.model.typechecker.model.Type)1 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)1