Search in sources :

Example 11 with Expression

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

the class StatementTransformer method transformVariableOrDestructure.

public List<JCVariableDecl> transformVariableOrDestructure(Tree.Statement varOrDes) {
    List<JCVariableDecl> vars = List.<JCVariableDecl>nil();
    if (varOrDes instanceof Tree.Variable) {
        Tree.Variable var = (Tree.Variable) varOrDes;
        Expression expr = var.getSpecifierExpression().getExpression();
        BoxingStrategy boxingStrategy = CodegenUtil.getBoxingStrategy(var.getDeclarationModel());
        JCExpression init = expressionGen().transformExpression(expr, boxingStrategy, var.getType().getTypeModel());
        vars = vars.append(transformVariable(var, init, expr.getTypeModel(), boxingStrategy == BoxingStrategy.BOXED).build());
    } else if (varOrDes instanceof Tree.Destructure) {
        Tree.Destructure des = (Tree.Destructure) varOrDes;
        vars = vars.appendList(transform(des));
    } else {
        throw BugException.unhandledCase(varOrDes);
    }
    return vars;
}
Also used : Variable(com.redhat.ceylon.compiler.typechecker.tree.Tree.Variable) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Variable(com.redhat.ceylon.compiler.typechecker.tree.Tree.Variable) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) SpecifierOrInitializerExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.SpecifierOrInitializerExpression) Expression(com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression) CustomTree(com.redhat.ceylon.compiler.typechecker.tree.CustomTree) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) BoxingStrategy(com.redhat.ceylon.compiler.java.codegen.AbstractTransformer.BoxingStrategy) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 12 with Expression

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

the class NamedArgumentInvocation method bindMethodArgument.

private void bindMethodArgument(Tree.MethodArgument methodArg, Parameter declaredParam, Naming.SyntheticName argName) {
    ListBuffer<JCStatement> statements;
    Function model = methodArg.getDeclarationModel();
    List<JCStatement> body;
    boolean prevNoExpressionlessReturn = gen.statementGen().noExpressionlessReturn;
    boolean prevSyntheticClassBody = gen.expressionGen().withinSyntheticClassBody(Decl.isMpl(model) || gen.expressionGen().isWithinSyntheticClassBody());
    try {
        gen.statementGen().noExpressionlessReturn = gen.isAnything(model.getType());
        if (methodArg.getBlock() != null) {
            body = gen.statementGen().transformBlock(methodArg.getBlock());
            if (!methodArg.getBlock().getDefinitelyReturns()) {
                if (gen.isAnything(model.getType())) {
                    body = body.append(gen.make().Return(gen.makeNull()));
                } else {
                    body = body.append(gen.make().Return(gen.makeErroneous(methodArg.getBlock(), "compiler bug: non-void method does not definitely return")));
                }
            }
        } else {
            Expression expr = methodArg.getSpecifierExpression().getExpression();
            BoxingStrategy boxing = CodegenUtil.getBoxingStrategy(model);
            Type type = model.getType();
            JCExpression transExpr = gen.expressionGen().transformExpression(expr, boxing, type);
            JCReturn returnStat = gen.make().Return(transExpr);
            body = List.<JCStatement>of(returnStat);
        }
    } finally {
        gen.expressionGen().withinSyntheticClassBody(prevSyntheticClassBody);
        gen.statementGen().noExpressionlessReturn = prevNoExpressionlessReturn;
    }
    Type callableType = model.appliedReference(null, Collections.<Type>emptyList()).getFullType();
    CallableBuilder callableBuilder = CallableBuilder.methodArgument(gen.gen(), methodArg, model, callableType, Collections.singletonList(methodArg.getParameterLists().get(0)), gen.classGen().transformMplBody(methodArg.getParameterLists(), model, body));
    JCExpression callable = callableBuilder.build();
    JCExpression typeExpr = gen.makeJavaType(callableType, JT_RAW);
    JCVariableDecl varDecl = gen.makeVar(argName, typeExpr, callable);
    statements = ListBuffer.<JCStatement>of(varDecl);
    bind(declaredParam, argName, gen.makeJavaType(callableType), statements.toList());
}
Also used : Function(com.redhat.ceylon.model.typechecker.model.Function) Type(com.redhat.ceylon.model.typechecker.model.Type) JCReturn(com.sun.tools.javac.tree.JCTree.JCReturn) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) QualifiedTypeExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.QualifiedTypeExpression) Expression(com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) BoxingStrategy(com.redhat.ceylon.compiler.java.codegen.AbstractTransformer.BoxingStrategy) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 13 with Expression

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

the class BoxingVisitor method visit.

@Override
public void visit(Tree.SwitchExpression that) {
    super.visit(that);
    SwitchCaseList caseList = that.getSwitchCaseList();
    if (caseList == null || caseList.getCaseClauses() == null)
        return;
    boolean unboxed = true;
    for (Tree.CaseClause caseClause : caseList.getCaseClauses()) {
        Expression expr = caseClause.getExpression();
        if (expr == null)
            return;
        // a single boxed one makes the whole switch boxed
        if (!CodegenUtil.isUnBoxed(expr))
            unboxed = false;
    // A Switch expression can never be raw, type erased or untrusted because
    // it uses a Let with a new variable declaration, so the rawness, 
    // erasedness and untrustedness of its branches cannot propagate further 
    // up the tree.
    }
    if (caseList.getElseClause() != null) {
        Expression expr = caseList.getElseClause().getExpression();
        if (expr == null)
            return;
        // a single boxed one makes the whole switch boxed
        if (!CodegenUtil.isUnBoxed(expr))
            unboxed = false;
    // see comment about about why we don't propagate rawness etc here.
    }
    if (unboxed && !willEraseToObject(that.getUnit().denotableType(that.getTypeModel())))
        CodegenUtil.markUnBoxed(that);
    if (that.getTypeModel().isExactly(that.getUnit().getNullValueDeclaration().getType())) {
        CodegenUtil.markTypeErased(that);
    }
}
Also used : InvocationExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.InvocationExpression) MemberOrTypeExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.MemberOrTypeExpression) PrefixOperatorExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.PrefixOperatorExpression) PostfixOperatorExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.PostfixOperatorExpression) StaticMemberOrTypeExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.StaticMemberOrTypeExpression) ParameterizedExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.ParameterizedExpression) IndexExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.IndexExpression) QualifiedMemberExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.QualifiedMemberExpression) Expression(com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression) BaseMemberExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.BaseMemberExpression) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) SwitchCaseList(com.redhat.ceylon.compiler.typechecker.tree.Tree.SwitchCaseList)

Example 14 with Expression

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

the class BoxingVisitor method visit.

@Override
public void visit(Tree.IfExpression that) {
    super.visit(that);
    if (that.getIfClause() == null || that.getElseClause() == null)
        return;
    Tree.Expression ifExpr = that.getIfClause().getExpression();
    Tree.Expression elseExpr = that.getElseClause().getExpression();
    if (ifExpr == null || elseExpr == null)
        return;
    if (CodegenUtil.isUnBoxed(ifExpr) && CodegenUtil.isUnBoxed(elseExpr) && !willEraseToObject(that.getUnit().denotableType(that.getTypeModel())))
        CodegenUtil.markUnBoxed(that);
    if (that.getTypeModel().isExactly(that.getUnit().getNullValueDeclaration().getType())) {
        CodegenUtil.markTypeErased(that);
    }
// An If expression can never be raw, type erased or untrusted because
// it uses a Let with a new variable declaration, so the rawness, 
// erasedness and untrustedness of its branches cannot propagate further 
// up the tree.
}
Also used : Expression(com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree)

Example 15 with Expression

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

the class ExpressionTransformer method transformStringExpression.

public JCExpression transformStringExpression(Tree.StringTemplate expr) {
    at(expr);
    JCExpression builder;
    builder = make().NewClass(null, null, naming.makeFQIdent("java", "lang", "StringBuilder"), List.<JCExpression>nil(), null);
    java.util.List<Tree.StringLiteral> literals = expr.getStringLiterals();
    java.util.List<Tree.Expression> expressions = expr.getExpressions();
    for (int ii = 0; ii < literals.size(); ii += 1) {
        Tree.StringLiteral literal = literals.get(ii);
        if (!literal.getText().isEmpty()) {
            // ignore empty string literals
            at(literal);
            builder = make().Apply(null, makeSelect(builder, "append"), List.<JCExpression>of(transform(literal)));
        }
        if (ii == expressions.size()) {
            // after that because we've already exhausted all the expressions
            break;
        }
        Tree.Expression expression = expressions.get(ii);
        at(expression);
        // Here in both cases we don't need a type cast for erasure
        if (isCeylonBasicType(expression.getTypeModel()) && expression.getUnboxed()) {
            // TODO: Test should be erases to String, long, int, boolean, char, byte, float, double
            // If erases to a Java primitive just call append, don't box it just to call format. 
            String method = isCeylonCharacter(expression.getTypeModel()) ? "appendCodePoint" : "append";
            builder = make().Apply(null, makeSelect(builder, method), List.<JCExpression>of(transformExpression(expression, BoxingStrategy.UNBOXED, null)));
        } else {
            JCMethodInvocation formatted = make().Apply(null, makeSelect(transformExpression(expression), "toString"), List.<JCExpression>nil());
            builder = make().Apply(null, makeSelect(builder, "append"), List.<JCExpression>of(formatted));
        }
    }
    return make().Apply(null, makeSelect(builder, "toString"), List.<JCExpression>nil());
}
Also used : JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) LetExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.LetExpression) Expression(com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression) Expression(com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree)

Aggregations

Expression (com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression)15 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)14 JCTree (com.sun.tools.javac.tree.JCTree)10 CustomTree (com.redhat.ceylon.compiler.typechecker.tree.CustomTree)8 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)8 Type (com.redhat.ceylon.model.typechecker.model.Type)6 SpecifierOrInitializerExpression (com.redhat.ceylon.compiler.typechecker.tree.Tree.SpecifierOrInitializerExpression)5 Term (com.redhat.ceylon.compiler.typechecker.tree.Tree.Term)5 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)4 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)4 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)3 ArrayList (java.util.ArrayList)3 BoxingStrategy (com.redhat.ceylon.compiler.java.codegen.AbstractTransformer.BoxingStrategy)2 SyntheticName (com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName)2 LetExpression (com.redhat.ceylon.compiler.typechecker.tree.Tree.LetExpression)2 Variable (com.redhat.ceylon.compiler.typechecker.tree.Tree.Variable)2 CName (com.redhat.ceylon.compiler.java.codegen.Naming.CName)1 Substitution (com.redhat.ceylon.compiler.java.codegen.Naming.Substitution)1 SourceDeclarationVisitor (com.redhat.ceylon.compiler.java.loader.SourceDeclarationVisitor)1 PhasedUnit (com.redhat.ceylon.compiler.typechecker.context.PhasedUnit)1