Search in sources :

Example 6 with HasErrorException

use of com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException in project ceylon-compiler by ceylon.

the class ClassTransformer method transformMethodBlock.

private List<JCStatement> transformMethodBlock(final Tree.MethodDefinition def) {
    final Function model = def.getDeclarationModel();
    final Tree.Block block = def.getBlock();
    List<JCStatement> body;
    boolean prevNoExpressionlessReturn = statementGen().noExpressionlessReturn;
    try {
        statementGen().noExpressionlessReturn = Decl.isMpl(model) || Strategy.useBoxedVoid(model);
        body = statementGen().transformBlock(block);
    } finally {
        statementGen().noExpressionlessReturn = prevNoExpressionlessReturn;
    }
    // We void methods need to have their Callables return null
    // so adjust here.
    HasErrorException error = errors().getFirstErrorBlock(block);
    if ((Decl.isMpl(model) || Strategy.useBoxedVoid(model)) && !block.getDefinitelyReturns() && error == null) {
        if (Decl.isUnboxedVoid(model)) {
            body = body.append(make().Return(makeNull()));
        } else {
            body = body.append(make().Return(makeErroneous(block, "compiler bug: non-void method doesn't definitely return")));
        }
    }
    return body;
}
Also used : Function(com.redhat.ceylon.model.typechecker.model.Function) HasErrorException(com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException) JCPrimitiveTypeTree(com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 7 with HasErrorException

use of com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException in project ceylon-compiler by ceylon.

the class StatementTransformer method transformBlock.

public List<JCStatement> transformBlock(Tree.Block block, boolean revertRet) {
    if (block == null) {
        return List.<JCStatement>nil();
    }
    at(block);
    CeylonVisitor v = gen().visitor;
    final ListBuffer<JCTree> prevDefs = v.defs;
    final boolean prevInInitializer = v.inInitializer;
    final ClassDefinitionBuilder prevClassBuilder = v.classBuilder;
    List<JCStatement> result;
    try {
        v.defs = new ListBuffer<JCTree>();
        v.inInitializer = false;
        v.classBuilder = current();
        java.util.Iterator<Statement> statements = block.getStatements().iterator();
        while (statements.hasNext()) {
            Tree.Statement stmt = statements.next();
            Transformer<JCStatement, Return> returnTransformer;
            if (revertRet && stmt instanceof Tree.Declaration) {
                returnTransformer = returnTransformer(defaultReturnTransformer);
            } else {
                returnTransformer = this.returnTransformer;
            }
            try {
                HasErrorException error = errors().getFirstErrorBlock(stmt);
                if (error == null) {
                    stmt.visit(v);
                } else {
                    v.append(this.makeThrowUnresolvedCompilationError(error));
                    break;
                }
            } finally {
                returnTransformer(returnTransformer);
            }
        }
        result = (List<JCStatement>) v.getResult().toList();
    } finally {
        v.classBuilder = prevClassBuilder;
        v.inInitializer = prevInInitializer;
        v.defs = prevDefs;
        // Close Substitutions which were scoped to this block
        Scope scope = block.getScope();
        while (scope instanceof ConditionScope) {
            scope = scope.getScope();
        }
        naming.closeScopedSubstitutions(scope);
    }
    return result;
}
Also used : Return(com.redhat.ceylon.compiler.typechecker.tree.Tree.Return) ForStatement(com.redhat.ceylon.compiler.typechecker.tree.Tree.ForStatement) Statement(com.redhat.ceylon.compiler.typechecker.tree.Tree.Statement) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCExpressionStatement(com.sun.tools.javac.tree.JCTree.JCExpressionStatement) JCTree(com.sun.tools.javac.tree.JCTree) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) Statement(com.redhat.ceylon.compiler.typechecker.tree.Tree.Statement) ConditionScope(com.redhat.ceylon.model.typechecker.model.ConditionScope) Scope(com.redhat.ceylon.model.typechecker.model.Scope) ConditionScope(com.redhat.ceylon.model.typechecker.model.ConditionScope) HasErrorException(com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException) CustomTree(com.redhat.ceylon.compiler.typechecker.tree.CustomTree) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree)

Example 8 with HasErrorException

use of com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException in project ceylon-compiler by ceylon.

the class AbstractTransformer method makeSetterBlock.

JCBlock makeSetterBlock(TypedDeclaration declarationModel, final Tree.Block block, final Tree.SpecifierOrInitializerExpression expression) {
    List<JCStatement> stats;
    if (block != null) {
        stats = statementGen().transformBlock(block);
    } else {
        Type type = declarationModel.getType();
        JCStatement transStmt;
        HasErrorException error = errors().getFirstExpressionErrorAndMarkBrokenness(expression.getExpression());
        if (error != null) {
            transStmt = this.makeThrowUnresolvedCompilationError(error);
        } else {
            transStmt = make().Exec(expressionGen().transformExpression(expression.getExpression(), BoxingStrategy.INDIFFERENT, type));
        }
        stats = List.<JCStatement>of(transStmt);
    }
    JCBlock setterBlock = make().Block(0, stats);
    return setterBlock;
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) HasErrorException(com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 9 with HasErrorException

use of com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException in project ceylon-compiler by ceylon.

the class StatementTransformer method transform.

// FIXME There is a similar implementation in ClassGen!
public List<JCStatement> transform(Tree.AttributeDeclaration decl) {
    ListBuffer<JCStatement> result = ListBuffer.<JCStatement>lb();
    // If the attribute is really from a parameter then don't generate a local variable
    Parameter parameter = CodegenUtil.findParamForDecl(decl);
    if (parameter == null) {
        final Name attrName = names().fromString(naming.substitute(decl.getDeclarationModel()));
        Type t = decl.getDeclarationModel().getType();
        JCExpression initialValue = null;
        SpecifierOrInitializerExpression initOrSpec = decl.getSpecifierOrInitializerExpression();
        if (initOrSpec != null) {
            HasErrorException error = errors().getFirstExpressionErrorAndMarkBrokenness(initOrSpec.getExpression().getTerm());
            if (error != null) {
                return List.<JCStatement>of(this.makeThrowUnresolvedCompilationError(error));
            }
            initialValue = expressionGen().transformExpression(initOrSpec.getExpression(), CodegenUtil.getBoxingStrategy(decl.getDeclarationModel()), decl.getDeclarationModel().getType());
        } else if (decl.getDeclarationModel().isVariable()) {
            // Java's definite initialization doesn't always work 
            // so give variable attribute declarations without 
            // initializers a default value. See #1153.
            initialValue = makeDefaultExprForType(t);
            if (CodegenUtil.getBoxingStrategy(decl.getDeclarationModel()) == BoxingStrategy.BOXED && canUnbox(t)) {
                initialValue = boxType(initialValue, t);
            }
        }
        List<JCAnnotation> annots = List.<JCAnnotation>nil();
        int modifiers = transformLocalFieldDeclFlags(decl);
        JCExpression typeExpr = makeJavaType(decl.getDeclarationModel(), t, modifiers);
        result.append(at(decl.getIdentifier()).VarDef(at(decl.getIdentifier()).Modifiers(modifiers, annots), attrName, typeExpr, initialValue));
        JCStatement outerSubs = openOuterSubstitutionIfNeeded(decl.getDeclarationModel(), t, annots, modifiers);
        if (outerSubs != null) {
            result.append(outerSubs);
        }
    }
    return result.toList();
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) HasErrorException(com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) SpecifierOrInitializerExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.SpecifierOrInitializerExpression) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) SyntheticName(com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName) CName(com.redhat.ceylon.compiler.java.codegen.Naming.CName) Name(com.sun.tools.javac.util.Name) OptionName(com.sun.tools.javac.main.OptionName)

Example 10 with HasErrorException

use of com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException in project ceylon-compiler by ceylon.

the class AbstractTransformer method makeGetterBlock.

JCBlock makeGetterBlock(TypedDeclaration declarationModel, final Tree.Block block, final Tree.SpecifierOrInitializerExpression expression) {
    List<JCStatement> stats;
    if (block != null) {
        stats = statementGen().transformBlock(block);
    } else {
        BoxingStrategy boxing = CodegenUtil.getBoxingStrategy(declarationModel);
        Type type = declarationModel.getType();
        JCStatement transStat;
        HasErrorException error = errors().getFirstExpressionErrorAndMarkBrokenness(expression.getExpression());
        if (error != null) {
            transStat = this.makeThrowUnresolvedCompilationError(error);
        } else {
            transStat = make().Return(expressionGen().transformExpression(expression.getExpression(), boxing, type));
        }
        stats = List.<JCStatement>of(transStat);
    }
    JCBlock getterBlock = make().Block(0, stats);
    return getterBlock;
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) HasErrorException(com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Aggregations

HasErrorException (com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException)13 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)11 Type (com.redhat.ceylon.model.typechecker.model.Type)6 JCTree (com.sun.tools.javac.tree.JCTree)6 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)5 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)5 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)4 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)3 Parameter (com.redhat.ceylon.model.typechecker.model.Parameter)3 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)3 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)3 CustomTree (com.redhat.ceylon.compiler.typechecker.tree.CustomTree)2 SpecifierOrInitializerExpression (com.redhat.ceylon.compiler.typechecker.tree.Tree.SpecifierOrInitializerExpression)2 Statement (com.redhat.ceylon.compiler.typechecker.tree.Tree.Statement)2 Constructor (com.redhat.ceylon.model.typechecker.model.Constructor)2 Function (com.redhat.ceylon.model.typechecker.model.Function)2 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)2 Functional (com.redhat.ceylon.model.typechecker.model.Functional)2 ModelUtil.appliedType (com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType)2 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)2