Search in sources :

Example 6 with Term

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

the class AnnotationModelVisitor method defaultedParameter.

public void defaultedParameter(Tree.SpecifierOrInitializerExpression d) {
    if (annotationConstructor != null) {
        AnnotationConstructorParameter annotationConstructorParameter = instantiation.getConstructorParameters().get(instantiation.getConstructorParameters().size() - 1);
        Declaration t = d.getUnit().getTrueValueDeclaration();
        Declaration f = d.getUnit().getFalseValueDeclaration();
        Term term = d.getExpression().getTerm();
        if (term instanceof Tree.InvocationExpression) {
            Tree.Primary primary = ((Tree.InvocationExpression) term).getPrimary();
            if (primary instanceof Tree.BaseMemberOrTypeExpression && (isAnnotationConstructor(((Tree.BaseMemberOrTypeExpression) primary).getDeclaration()) || isAnnotationClass(((Tree.BaseMemberOrTypeExpression) primary).getDeclaration()))) {
                final AnnotationInvocation prevInstantiation = this.instantiation;
                this.instantiation = new AnnotationInvocation();
                if (isAnnotationConstructor(((Tree.BaseMemberOrTypeExpression) primary).getDeclaration())) {
                    Function constructor = (Function) ((Tree.BaseMemberOrTypeExpression) primary).getDeclaration();
                    instantiation.setConstructorDeclaration(constructor);
                    instantiation.getConstructorParameters().addAll(((AnnotationInvocation) constructor.getAnnotationConstructor()).getConstructorParameters());
                }
                checkingDefaults = true;
                super.visit(d);
                annotationConstructorParameter.setDefaultArgument(this.term);
                this.term = null;
                checkingDefaults = false;
                this.instantiation = prevInstantiation;
            } else {
                errorDefaultedParameter(d);
            }
        } else if (term instanceof Tree.Literal || (term instanceof Tree.NegativeOp && ((Tree.NegativeOp) term).getTerm() instanceof Tree.Literal) || (term instanceof Tree.BaseMemberExpression && (((Tree.BaseMemberExpression) term).getDeclaration().equals(t) || ((Tree.BaseMemberExpression) term).getDeclaration().equals(f) || ((Tree.BaseMemberExpression) term).getDeclaration().isParameter() || Decl.isAnonCaseOfEnumeratedType((Tree.BaseMemberExpression) term)))) {
            checkingDefaults = true;
            super.visit(d);
            annotationConstructorParameter.setDefaultArgument(this.term);
            this.term = null;
            checkingDefaults = false;
        } else if (term instanceof Tree.Tuple || term instanceof Tree.SequenceEnumeration) {
            // TODO Tuples and SequenceEnumerations of the above cases should also be allowed
            checkingDefaults = true;
            super.visit(d);
            annotationConstructorParameter.setDefaultArgument(this.term);
            this.term = null;
            checkingDefaults = false;
        } else {
            errorDefaultedParameter(d);
        }
    }
}
Also used : Term(com.redhat.ceylon.compiler.typechecker.tree.Tree.Term) Function(com.redhat.ceylon.model.typechecker.model.Function) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 7 with Term

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

the class StatementTransformer method transformCaseMatch.

private JCStatement transformCaseMatch(Naming.SyntheticName selectorAlias, Tree.SwitchClause switchClause, Tree.CaseClause caseClause, String tmpVar, Tree.Term outerExpression, Type expectedType, Tree.MatchCase matchCase, JCStatement last, Type switchType, boolean primitiveSelector) {
    at(matchCase);
    JCExpression tests = null;
    java.util.List<Tree.Expression> expressions = matchCase.getExpressionList().getExpressions();
    for (Tree.Expression expr : expressions) {
        Tree.Term term = ExpressionTransformer.eliminateParens(expr.getTerm());
        boolean unboxedEquality = primitiveSelector || isCeylonBasicType(typeFact().getDefiniteType(switchType));
        JCExpression transformedExpression = expressionGen().transformExpression(term, unboxedEquality ? BoxingStrategy.UNBOXED : BoxingStrategy.BOXED, term.getTypeModel());
        JCExpression test;
        if (term instanceof Tree.Literal || term instanceof Tree.NegativeOp) {
            if (unboxedEquality) {
                if (term instanceof Tree.StringLiteral) {
                    test = make().Apply(null, makeSelect(unboxType(selectorAlias.makeIdent(), term.getTypeModel()), "equals"), List.<JCExpression>of(transformedExpression));
                } else {
                    test = make().Binary(JCTree.EQ, primitiveSelector ? selectorAlias.makeIdent() : unboxType(selectorAlias.makeIdent(), term.getTypeModel()), transformedExpression);
                }
            } else {
                test = make().Apply(null, makeSelect(selectorAlias.makeIdent(), "equals"), List.<JCExpression>of(transformedExpression));
            }
            if (isOptional(switchType)) {
                test = make().Binary(JCTree.AND, make().Binary(JCTree.NE, selectorAlias.makeIdent(), makeNull()), test);
            }
        } else {
            JCExpression selectorExpr;
            if (!primitiveSelector && isCeylonBasicType(typeFact().getDefiniteType(switchType))) {
                selectorExpr = unboxType(selectorAlias.makeIdent(), term.getTypeModel());
            } else {
                selectorExpr = selectorAlias.makeIdent();
            }
            test = make().Binary(JCTree.EQ, selectorExpr, transformedExpression);
        }
        if (tests == null)
            tests = test;
        else if (isNull(term.getTypeModel())) {
            // ensure we do any null check as the first operation in the ||-ed expression
            tests = make().Binary(JCTree.OR, test, tests);
        } else {
            tests = make().Binary(JCTree.OR, tests, test);
        }
    }
    Substitution prevSubst = null;
    if (switchClause.getSwitched().getVariable() != null) {
        // Prepare for variable substitution in the following code block
        prevSubst = naming.addVariableSubst(switchClause.getSwitched().getVariable().getDeclarationModel(), selectorAlias.toString());
    }
    JCBlock block = transformCaseClauseBlock(caseClause, tmpVar, outerExpression, expectedType);
    if (prevSubst != null) {
        // Deactivate the above variable substitution
        prevSubst.close();
    }
    return at(caseClause).If(tests, block, last);
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) Term(com.redhat.ceylon.compiler.typechecker.tree.Tree.Term) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Substitution(com.redhat.ceylon.compiler.java.codegen.Naming.Substitution) 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) 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)

Example 8 with Term

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

the class ExpressionTransformer method transform.

// Prefix operator
public JCExpression transform(final Tree.PrefixOperatorExpression expr) {
    final OperatorTranslation operator = Operators.getOperator(expr.getClass());
    if (operator == null) {
        return makeErroneous(expr, "compiler bug: " + expr.getNodeType() + " is not supported yet");
    }
    OptimisationStrategy optimisationStrategy = operator.getUnOpOptimisationStrategy(expr, expr.getTerm(), this);
    final boolean canOptimise = optimisationStrategy.useJavaOperator();
    Tree.Term term = expr.getTerm();
    // only fully optimise if we don't have to access the getter/setter
    if (canOptimise && CodegenUtil.isDirectAccessVariable(term)) {
        JCExpression jcTerm = transformExpression(term, BoxingStrategy.UNBOXED, expr.getTypeModel(), EXPR_WIDEN_PRIM);
        return at(expr).Unary(operator.javacOperator, jcTerm);
    }
    Interface compoundType = expr.getUnit().getOrdinalDeclaration();
    Type valueType = getSupertype(term, compoundType);
    final Type returnType = getMostPreciseType(term, getTypeArgument(valueType, 0));
    // we work on boxed types unless we could have optimised
    return transformAssignAndReturnOperation(expr, term, !canOptimise, valueType, returnType, new AssignAndReturnOperationFactory() {

        @Override
        public JCExpression getNewValue(JCExpression previousValue) {
            // use +1/-1 if we can optimise a bit
            if (canOptimise) {
                JCExpression ret = make().Binary(operator == OperatorTranslation.UNARY_PREFIX_INCREMENT ? JCTree.PLUS : JCTree.MINUS, previousValue, makeInteger(1));
                ret = unAutoPromote(ret, returnType);
                return ret;
            }
            // make this call: previousValue.getSuccessor() or previousValue.getPredecessor()
            return make().Apply(null, makeSelect(previousValue, operator.ceylonMethod), List.<JCExpression>nil());
        }
    });
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Term(com.redhat.ceylon.compiler.typechecker.tree.Tree.Term) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) OptimisationStrategy(com.redhat.ceylon.compiler.java.codegen.Operators.OptimisationStrategy) Interface(com.redhat.ceylon.model.typechecker.model.Interface) ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) AssignmentOperatorTranslation(com.redhat.ceylon.compiler.java.codegen.Operators.AssignmentOperatorTranslation) OperatorTranslation(com.redhat.ceylon.compiler.java.codegen.Operators.OperatorTranslation)

Example 9 with Term

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

the class StatementTransformer method definitelySatisfiedOrNot.

private boolean definitelySatisfiedOrNot(java.util.List<Tree.Condition> conditions, boolean satisfied) {
    if (conditions.size() != 1) {
        return false;
    }
    Tree.Condition condition = conditions.get(0);
    if (!(condition instanceof Tree.BooleanCondition)) {
        return false;
    }
    Tree.Term term = ((Tree.BooleanCondition) condition).getExpression().getTerm();
    if (!(term instanceof Tree.BaseMemberExpression)) {
        return false;
    }
    Declaration declaration = ((Tree.BaseMemberExpression) term).getDeclaration();
    return declaration instanceof Value && satisfied ? isBooleanTrue(declaration) : isBooleanFalse(declaration);
}
Also used : Term(com.redhat.ceylon.compiler.typechecker.tree.Tree.Term) Condition(com.redhat.ceylon.compiler.typechecker.tree.Tree.Condition) Value(com.redhat.ceylon.model.typechecker.model.Value) CustomTree(com.redhat.ceylon.compiler.typechecker.tree.CustomTree) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) 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 10 with Term

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

the class ExpressionTransformer method getIntegerLiteralPower.

/**
 * Returns the literal value of the power in the given power expression,
 * or null if the power is not an integer literal (or negation of an
 * integer literal)
 * @throws ErroneousException
 */
static java.lang.Long getIntegerLiteralPower(Tree.PowerOp op) throws ErroneousException {
    java.lang.Long power;
    Tree.Term term = unwrapExpressionUntilTerm(op.getRightTerm());
    if (term instanceof Tree.NaturalLiteral) {
        power = literalValue((Tree.NaturalLiteral) term);
    } else if (term instanceof Tree.NegativeOp && ((Tree.NegativeOp) term).getTerm() instanceof Tree.NaturalLiteral) {
        power = literalValue((Tree.NegativeOp) term);
    } else {
        power = null;
    }
    return power;
}
Also used : Term(com.redhat.ceylon.compiler.typechecker.tree.Tree.Term) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree)

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