Search in sources :

Example 11 with Term

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

the class ExpressionTransformer method checkForByteLiterals.

private JCExpression checkForByteLiterals(Tree.InvocationExpression ce) {
    // same test as in BoxingVisitor.isByteLiteral()
    if (ce.getPrimary() instanceof Tree.BaseTypeExpression && ce.getPositionalArgumentList() != null) {
        java.util.List<Tree.PositionalArgument> positionalArguments = ce.getPositionalArgumentList().getPositionalArguments();
        if (positionalArguments.size() == 1) {
            PositionalArgument argument = positionalArguments.get(0);
            if (argument instanceof Tree.ListedArgument && ((Tree.ListedArgument) argument).getExpression() != null) {
                Term term = ((Tree.ListedArgument) argument).getExpression().getTerm();
                boolean negative = false;
                if (term instanceof Tree.NegativeOp) {
                    negative = true;
                    term = ((Tree.NegativeOp) term).getTerm();
                }
                if (term instanceof Tree.NaturalLiteral) {
                    Declaration decl = ((Tree.BaseTypeExpression) ce.getPrimary()).getDeclaration();
                    if (decl instanceof Class) {
                        String name = decl.getQualifiedNameString();
                        if (name.equals("ceylon.language::Byte")) {
                            at(ce);
                            try {
                                long value = literalValue((Tree.NaturalLiteral) term);
                                if (negative)
                                    value = -value;
                                // assignment, not for method calls, so it's simpler to always cast
                                return make().TypeCast(syms().byteType, make().Literal(value));
                            } catch (ErroneousException e) {
                                // replaced with a throw.
                                return e.makeErroneous(this);
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : PositionalArgument(com.redhat.ceylon.compiler.typechecker.tree.Tree.PositionalArgument) TreeUtil.unwrapExpressionUntilTerm(com.redhat.ceylon.compiler.typechecker.tree.TreeUtil.unwrapExpressionUntilTerm) Term(com.redhat.ceylon.compiler.typechecker.tree.Tree.Term) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 12 with Term

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

the class ExpressionTransformer method transform.

public JCExpression transform(Tree.DefaultOp op, Type expectedType) {
    Term elseTerm = unwrapExpressionUntilTerm(op.getRightTerm());
    Type typeModel = typeFact().denotableType(op.getTypeModel());
    // make sure we do not insert null checks if we're going to allow testing for null
    Type rightExpectedType = getOptionalTypeForInteropIfAllowed(expectedType, typeModel, elseTerm);
    if (unwrapExpressionUntilTerm(op.getLeftTerm()) instanceof Tree.ThenOp) {
        // Optimize cond then foo else bar (avoids unnecessary boxing in particular)
        Tree.ThenOp then = (Tree.ThenOp) unwrapExpressionUntilTerm(op.getLeftTerm());
        Term condTerm = then.getLeftTerm();
        Term thenTerm = then.getRightTerm();
        JCExpression cond = transformExpression(condTerm, BoxingStrategy.UNBOXED, condTerm.getTypeModel());
        JCExpression thenpart = transformExpression(thenTerm, CodegenUtil.getBoxingStrategy(op), rightExpectedType);
        JCExpression elsepart = transformExpression(elseTerm, CodegenUtil.getBoxingStrategy(op), rightExpectedType);
        return make().Conditional(cond, thenpart, elsepart);
    }
    JCExpression left = transformExpression(op.getLeftTerm(), BoxingStrategy.BOXED, typeFact().getOptionalType(typeModel));
    JCExpression right = transformExpression(elseTerm, BoxingStrategy.BOXED, rightExpectedType);
    Naming.SyntheticName varName = naming.temp();
    JCExpression varIdent = varName.makeIdent();
    JCExpression test = at(op).Binary(JCTree.NE, varIdent, makeNull());
    JCExpression cond = make().Conditional(test, varIdent, right);
    JCExpression typeExpr = makeJavaType(typeModel, JT_NO_PRIMITIVES);
    return makeLetExpr(varName, null, typeExpr, left, cond);
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) SyntheticName(com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName) TreeUtil.unwrapExpressionUntilTerm(com.redhat.ceylon.compiler.typechecker.tree.TreeUtil.unwrapExpressionUntilTerm) Term(com.redhat.ceylon.compiler.typechecker.tree.Tree.Term)

Example 13 with Term

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

the class ExpressionTransformer method transformLet.

// this one trusts the expected type
private JCExpression transformLet(LetExpression op, Type expectedType) {
    ListBuffer<JCStatement> defs = new ListBuffer<JCStatement>();
    for (Tree.Statement stmt : op.getLetClause().getVariables()) {
        defs.addAll(statementGen().transformVariableOrDestructure(stmt));
    }
    Tree.Term term = op.getLetClause().getExpression().getTerm();
    BoxingStrategy boxingStrategy = CodegenUtil.getBoxingStrategy(term);
    JCExpression expr = transformExpression(term, boxingStrategy, expectedType);
    at(op);
    if (isAnything(op.getTypeModel()) && CodegenUtil.isUnBoxed(term)) {
        defs.add(make().Exec(expr));
        expr = makeNull();
    }
    return make().LetExpr(defs.toList(), expr);
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Term(com.redhat.ceylon.compiler.typechecker.tree.Tree.Term) ListBuffer(com.sun.tools.javac.util.ListBuffer) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 14 with Term

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

the class BoxingVisitor method visit.

@Override
public void visit(Expression that) {
    Stack<Boolean> npebs = setPEB();
    super.visit(that);
    resetPEB(npebs);
    Term term = that.getTerm();
    propagateFromTerm(that, term);
    // which will need to be marked boxed
    if (term instanceof MemberOrTypeExpression) {
        Tree.MemberOrTypeExpression expr = (Tree.MemberOrTypeExpression) term;
        if (expr.getDeclaration() instanceof Function) {
            that.setUnboxed(false);
        }
    }
}
Also used : Function(com.redhat.ceylon.model.typechecker.model.Function) MemberOrTypeExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.MemberOrTypeExpression) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) TreeUtil.unwrapExpressionUntilTerm(com.redhat.ceylon.compiler.typechecker.tree.TreeUtil.unwrapExpressionUntilTerm) Term(com.redhat.ceylon.compiler.typechecker.tree.Tree.Term) MemberOrTypeExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.MemberOrTypeExpression) StaticMemberOrTypeExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.StaticMemberOrTypeExpression)

Example 15 with Term

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

the class MethodOrValueReferenceVisitor method visit.

@Override
public void visit(Tree.ForComprehensionClause that) {
    super.visit(that);
    final SpecifierExpression specifier = that.getForIterator().getSpecifierExpression();
    if (specifier != null) {
        final Expression expr = specifier.getExpression();
        final Term term = expr.getTerm();
        if (term instanceof Tree.Primary) {
            capture((Tree.Primary) term, true);
        }
    }
    that.getComprehensionClause().visit(this);
}
Also used : LazySpecifierExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.LazySpecifierExpression) SpecifierExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.SpecifierExpression) LazySpecifierExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.LazySpecifierExpression) Expression(com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression) SpecifierExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.SpecifierExpression) SpecifierOrInitializerExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.SpecifierOrInitializerExpression) Primary(com.redhat.ceylon.compiler.typechecker.tree.Tree.Primary) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) Term(com.redhat.ceylon.compiler.typechecker.tree.Tree.Term)

Aggregations

Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)15 Term (com.redhat.ceylon.compiler.typechecker.tree.Tree.Term)15 JCTree (com.sun.tools.javac.tree.JCTree)12 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)9 Expression (com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression)5 Type (com.redhat.ceylon.model.typechecker.model.Type)5 TreeUtil.unwrapExpressionUntilTerm (com.redhat.ceylon.compiler.typechecker.tree.TreeUtil.unwrapExpressionUntilTerm)4 AssignmentOperatorTranslation (com.redhat.ceylon.compiler.java.codegen.Operators.AssignmentOperatorTranslation)3 OperatorTranslation (com.redhat.ceylon.compiler.java.codegen.Operators.OperatorTranslation)3 CustomTree (com.redhat.ceylon.compiler.typechecker.tree.CustomTree)3 SpecifierOrInitializerExpression (com.redhat.ceylon.compiler.typechecker.tree.Tree.SpecifierOrInitializerExpression)3 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)3 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)3 SyntheticName (com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName)2 OptimisationStrategy (com.redhat.ceylon.compiler.java.codegen.Operators.OptimisationStrategy)2 LetExpression (com.redhat.ceylon.compiler.typechecker.tree.Tree.LetExpression)2 ClassOrInterface (com.redhat.ceylon.model.typechecker.model.ClassOrInterface)2 Function (com.redhat.ceylon.model.typechecker.model.Function)2 Interface (com.redhat.ceylon.model.typechecker.model.Interface)2 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)2