Search in sources :

Example 21 with JCBlock

use of com.sun.tools.javac.tree.JCTree.JCBlock in project ceylon-compiler by ceylon.

the class CeylonTransformer method transformAttribute.

public List<JCTree> transformAttribute(TypedDeclaration declarationModel, String attrName, String attrClassName, final Tree.Declaration annotated, final Tree.Block block, final Tree.SpecifierOrInitializerExpression expression, final Tree.TypedDeclaration decl, final Tree.AttributeSetterDefinition setterDecl) {
    // For everything else generate a getter/setter method
    AttributeDefinitionBuilder builder = AttributeDefinitionBuilder.wrapped(this, attrClassName, null, attrName, declarationModel, declarationModel.isToplevel()).is(Flags.PUBLIC, declarationModel.isShared());
    final JCExpression initialValue;
    final HasErrorException expressionError;
    if (expression != null) {
        expressionError = errors().getFirstExpressionErrorAndMarkBrokenness(expression.getExpression());
        if (expressionError != null) {
            initialValue = make().Erroneous();
        } else {
            initialValue = transformValueInit(declarationModel, attrName, expression);
        }
    } else {
        expressionError = null;
        initialValue = transformValueInit(declarationModel, attrName, expression);
    }
    // For captured local variable Values, use a VariableBox
    if (Decl.isBoxedVariable(declarationModel)) {
        if (expressionError != null) {
            return List.<JCTree>of(this.makeThrowUnresolvedCompilationError(expressionError));
        } else {
            return List.<JCTree>of(makeVariableBoxDecl(initialValue, declarationModel));
        }
    }
    // For late-bound getters we only generate a declaration
    if (block == null && expression == null && !Decl.isToplevel(declarationModel)) {
        JCExpression typeExpr = makeJavaType(getGetterInterfaceType(declarationModel));
        JCTree.JCVariableDecl var = makeVar(attrClassName, typeExpr, null);
        return List.<JCTree>of(var);
    }
    // Set the local declarations annotation
    if (decl != null) {
        List<JCAnnotation> scopeAnnotations;
        if (Decl.isToplevel(declarationModel) && setterDecl != null) {
            scopeAnnotations = makeAtLocalDeclarations(decl, setterDecl);
        } else {
            scopeAnnotations = makeAtLocalDeclarations(decl);
        }
        builder.classAnnotations(scopeAnnotations);
    } else if (block != null) {
        List<JCAnnotation> scopeAnnotations = makeAtLocalDeclarations(block);
        builder.classAnnotations(scopeAnnotations);
    }
    // Remember the setter class if we generate a getter
    if (Decl.isGetter(declarationModel) && declarationModel.isVariable() && Decl.isLocal(declarationModel)) {
        // we must have a setter class
        Setter setter = ((Value) declarationModel).getSetter();
        if (setter != null) {
            String setterClassName = Naming.getAttrClassName(setter, 0);
            JCExpression setterClassNameExpr = naming.makeUnquotedIdent(setterClassName);
            builder.setterClass(makeSelect(setterClassNameExpr, "class"));
        }
    }
    if (declarationModel instanceof Setter || (declarationModel instanceof FunctionOrValue && ((FunctionOrValue) declarationModel).isParameter())) {
        // For local setters
        JCBlock setterBlock = makeSetterBlock(declarationModel, block, expression);
        builder.setterBlock(setterBlock);
        builder.skipGetter();
        if (Decl.isLocal(decl)) {
            // we need to find back the Setter model for local setters, because 
            // in transformAttribute(Tree.TypedDeclaration decl, Tree.AttributeSetterDefinition setterDecl)
            // we turn the declaration model from the Setter to its single parameter
            Setter setter = (Setter) declarationModel.getContainer();
            String getterClassName = Naming.getAttrClassName(setter.getGetter(), 0);
            JCExpression getterClassNameExpr = naming.makeUnquotedIdent(getterClassName);
            builder.isSetter(makeSelect(getterClassNameExpr, "class"));
        }
    } else {
        if (Decl.isValue(declarationModel)) {
            // For local and toplevel value attributes
            if (!declarationModel.isVariable() && !declarationModel.isLate()) {
                builder.immutable();
            }
        } else {
            // For local and toplevel getters
            boolean prevSyntheticClassBody;
            if (Decl.isLocal(declarationModel)) {
                prevSyntheticClassBody = expressionGen().withinSyntheticClassBody(true);
            } else {
                prevSyntheticClassBody = expressionGen().isWithinSyntheticClassBody();
            }
            JCBlock getterBlock = makeGetterBlock(declarationModel, block, expression);
            prevSyntheticClassBody = expressionGen().withinSyntheticClassBody(prevSyntheticClassBody);
            builder.getterBlock(getterBlock);
            if (Decl.isLocal(declarationModel)) {
                // For local getters
                builder.immutable();
            } else {
                // For toplevel getters
                if (setterDecl != null) {
                    JCBlock setterBlock = makeSetterBlock(setterDecl.getDeclarationModel(), setterDecl.getBlock(), setterDecl.getSpecifierExpression());
                    builder.setterBlock(setterBlock);
                    //builder.userAnnotationsSetter(expressionGen().transformAnnotations(true, OutputElement.METHOD, setterDecl));
                    builder.userAnnotationsSetter(expressionGen().transformAnnotations(OutputElement.SETTER, setterDecl));
                } else {
                    builder.immutable();
                }
            }
        }
    }
    if (annotated != null) {
        builder.userAnnotations(expressionGen().transformAnnotations(OutputElement.GETTER, annotated));
    }
    if (Decl.isLocal(declarationModel)) {
        if (expressionError != null) {
            return List.<JCTree>of(this.makeThrowUnresolvedCompilationError(expressionError));
        }
        builder.classAnnotations(makeAtLocalDeclaration(declarationModel.getQualifier(), false));
        if (initialValue != null)
            builder.valueConstructor();
        JCExpression typeExpr;
        if (declarationModel instanceof Setter || (declarationModel instanceof FunctionOrValue && ((FunctionOrValue) declarationModel).isParameter())) {
            typeExpr = makeQuotedIdent(attrClassName);
        } else {
            typeExpr = makeJavaType(getGetterInterfaceType(declarationModel));
        }
        return builder.build().append(makeLocalIdentityInstance(typeExpr, attrClassName, attrClassName, declarationModel.isShared(), initialValue));
    } else {
        if (expressionError != null) {
            builder.initialValueError(expressionError);
        } else if (initialValue != null) {
            builder.initialValue(initialValue);
        }
        builder.is(Flags.STATIC, true);
        return builder.build();
    }
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCTree(com.sun.tools.javac.tree.JCTree) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) HasErrorException(com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Setter(com.redhat.ceylon.model.typechecker.model.Setter) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) Value(com.redhat.ceylon.model.typechecker.model.Value) List(com.sun.tools.javac.util.List) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue)

Example 22 with JCBlock

use of com.sun.tools.javac.tree.JCTree.JCBlock in project ceylon-compiler by ceylon.

the class AttributeDefinitionBuilder method generateFieldInit.

private JCTree generateFieldInit() {
    long flags = (modifiers & Flags.STATIC);
    JCTree.JCExpression varInit = variableInit;
    if (hasInitFlag()) {
        varInit = variableInit;
    }
    JCTree.JCAssign init = owner.make().Assign(owner.makeUnquotedIdent(Naming.quoteFieldName(fieldName)), varInit);
    List<JCStatement> stmts;
    if (isDeferredInitError()) {
        // surround the init expression with a try/catch that saves the exception
        // doesn't matter
        String exceptionName = "x";
        // $initException$ = x
        JCStatement saveException = owner.make().Exec(owner.make().Assign(owner.makeUnquotedIdent(Naming.getToplevelAttributeSavedExceptionName()), owner.makeUnquotedIdent(exceptionName)));
        // value = null
        JCStatement nullValue = owner.make().Exec(owner.make().Assign(owner.makeUnquotedIdent(fieldName), owner.makeDefaultExprForType(this.attrType)));
        // the catch statements
        JCStatement initFlagFalse = owner.make().Exec(owner.make().Assign(owner.naming.makeUnquotedIdent(Naming.getInitializationFieldName(fieldName)), owner.make().Literal(false)));
        JCBlock handlerBlock = owner.make().Block(0, List.<JCTree.JCStatement>of(saveException, nullValue, initFlagFalse));
        // the catch block
        JCExpression throwableType = owner.makeJavaType(owner.syms().throwableType.tsym);
        JCVariableDecl exceptionParam = owner.make().VarDef(owner.make().Modifiers(0), owner.naming.makeUnquotedName(exceptionName), throwableType, null);
        JCCatch catchers = owner.make().Catch(exceptionParam, handlerBlock);
        // $initException$ = null
        JCTree.JCAssign nullException = owner.make().Assign(owner.makeUnquotedIdent(Naming.getToplevelAttributeSavedExceptionName()), owner.makeNull());
        // $init$value = true;
        JCTree.JCAssign initFlagTrue = owner.make().Assign(owner.naming.makeUnquotedIdent(Naming.getInitializationFieldName(fieldName)), owner.make().Literal(true));
        // save the value, mark the exception as null
        List<JCStatement> body = List.<JCTree.JCStatement>of(owner.make().Exec(init), owner.make().Exec(nullException), owner.make().Exec(initFlagTrue));
        // the try/catch
        JCTree.JCTry try_ = owner.make().Try(owner.make().Block(0, body), List.<JCTree.JCCatch>of(catchers), null);
        stmts = List.<JCTree.JCStatement>of(try_);
    } else {
        stmts = List.<JCTree.JCStatement>of(owner.make().Exec(init));
    }
    return owner.make().Block(flags, stmts);
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCTree(com.sun.tools.javac.tree.JCTree) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCCatch(com.sun.tools.javac.tree.JCTree.JCCatch) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 23 with JCBlock

use of com.sun.tools.javac.tree.JCTree.JCBlock in project ceylon-compiler by ceylon.

the class AbstractTransformer method makeGetterBlock.

JCBlock makeGetterBlock(final JCExpression expression) {
    List<JCStatement> stats = List.<JCStatement>of(make().Return(expression));
    JCBlock getterBlock = make().Block(0, stats);
    return getterBlock;
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 24 with JCBlock

use of com.sun.tools.javac.tree.JCTree.JCBlock in project ceylon-compiler by ceylon.

the class CeylonTransformer method transformAttributeGetter.

public JCExpression transformAttributeGetter(TypedDeclaration declarationModel, final JCExpression expression) {
    final String attrName = declarationModel.getName();
    final String attrClassName = Naming.getAttrClassName(declarationModel, 0);
    JCBlock getterBlock = makeGetterBlock(expression);
    // For everything else generate a getter/setter method
    AttributeDefinitionBuilder builder = AttributeDefinitionBuilder.indirect(this, attrClassName, attrName, declarationModel, declarationModel.isToplevel()).getterBlock(getterBlock).immutable();
    List<JCTree> attr = builder.build();
    JCNewClass newExpr = makeNewClass(attrClassName, false, null);
    JCExpression result = makeLetExpr(naming.temp(), List.<JCStatement>of((JCStatement) attr.get(0)), newExpr);
    return result;
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCTree(com.sun.tools.javac.tree.JCTree) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 25 with JCBlock

use of com.sun.tools.javac.tree.JCTree.JCBlock in project ceylon-compiler by ceylon.

the class ClassTransformer method transform.

public AttributeDefinitionBuilder transform(AttributeGetterDefinition decl, boolean forCompanion) {
    if (Strategy.onlyOnCompanion(decl.getDeclarationModel()) && !forCompanion) {
        return null;
    }
    String name = decl.getIdentifier().getText();
    //expressionGen().transform(decl.getAnnotationList());
    final AttributeDefinitionBuilder builder = AttributeDefinitionBuilder.getter(this, name, decl.getDeclarationModel()).modifiers(transformAttributeGetSetDeclFlags(decl.getDeclarationModel(), forCompanion));
    // companion class members are never actual no matter what the Declaration says
    if (forCompanion)
        builder.notActual();
    if (Decl.withinClass(decl) || forCompanion) {
        JCBlock body = statementGen().transform(decl.getBlock());
        builder.getterBlock(body);
    } else {
        builder.isFormal(true);
    }
    builder.userAnnotations(expressionGen().transformAnnotations(OutputElement.GETTER, decl));
    return builder;
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock)

Aggregations

JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)44 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)32 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)31 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)28 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)23 Name (com.sun.tools.javac.util.Name)22 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)17 ListBuffer (com.sun.tools.javac.util.ListBuffer)17 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)16 JavacTreeMaker (lombok.javac.JavacTreeMaker)13 JCTree (com.sun.tools.javac.tree.JCTree)11 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)10 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)6 JavacNode (lombok.javac.JavacNode)6 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)5 HasErrorException (com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException)4 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)4 Type (com.redhat.ceylon.model.typechecker.model.Type)4 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)4 CustomTree (com.redhat.ceylon.compiler.typechecker.tree.CustomTree)3