Search in sources :

Example 1 with Condition

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

the class StatementTransformer method transform.

public JCTree transform(CustomTree.GuardedVariable that) {
    BoxingStrategy boxingStrategy = CodegenUtil.getBoxingStrategy(that.getDeclarationModel());
    Tree.Expression expr = that.getSpecifierExpression().getExpression();
    Type fromType = expr.getTypeModel();
    Value newValue = that.getDeclarationModel();
    Type toType = newValue.getType();
    Tree.ConditionList conditionList = that.getConditionList();
    Tree.Condition condition = conditionList.getConditions().get(0);
    JCExpression val = expressionGen().transformExpression(expr);
    at(that);
    if (condition instanceof Tree.IsCondition) {
        if (!willEraseToObject(toType)) {
            // Want raw type for instanceof since it can't be used with generic types
            JCExpression rawToTypeExpr = makeJavaType(toType, JT_NO_PRIMITIVES | JT_RAW);
            // Substitute variable with the correct type to use in the rest of the code block
            val = make().TypeCast(rawToTypeExpr, val);
            if (CodegenUtil.isUnBoxed(newValue) && canUnbox(toType)) {
                val = unboxType(val, toType);
            }
        }
    } else if (condition instanceof Tree.ExistsCondition) {
        Type exprType = fromType;
        if (isOptional(exprType)) {
            exprType = typeFact().getDefiniteType(exprType);
        }
        val = expressionGen().applyErasureAndBoxing(val, exprType, CodegenUtil.hasTypeErased(expr), true, CodegenUtil.hasUntrustedType(expr), boxingStrategy, toType, 0);
    } else if (condition instanceof Tree.NonemptyCondition) {
        Type exprType = fromType;
        if (isOptional(exprType)) {
            exprType = typeFact().getDefiniteType(exprType);
        }
        val = expressionGen().applyErasureAndBoxing(val, exprType, false, true, BoxingStrategy.BOXED, toType, ExpressionTransformer.EXPR_DOWN_CAST);
    }
    SyntheticName alias = naming.alias(that.getIdentifier().getText());
    Substitution subst = naming.addVariableSubst(newValue, alias.getName());
    // FIXME: this is rubbish, but the same rubbish from assert. it's most likely wrong there too
    Scope scope = that.getScope().getScope();
    while (scope instanceof ConditionScope) {
        scope = scope.getScope();
    }
    subst.scopeClose(scope);
    JCExpression varType = makeJavaType(toType);
    return make().VarDef(make().Modifiers(FINAL), alias.asName(), varType, val);
}
Also used : SyntheticName(com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName) ConditionList(com.redhat.ceylon.compiler.typechecker.tree.Tree.ConditionList) Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Substitution(com.redhat.ceylon.compiler.java.codegen.Naming.Substitution) ConditionScope(com.redhat.ceylon.model.typechecker.model.ConditionScope) Scope(com.redhat.ceylon.model.typechecker.model.Scope) ConditionScope(com.redhat.ceylon.model.typechecker.model.ConditionScope) Condition(com.redhat.ceylon.compiler.typechecker.tree.Tree.Condition) Expression(com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression) 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) BoxingStrategy(com.redhat.ceylon.compiler.java.codegen.AbstractTransformer.BoxingStrategy)

Example 2 with Condition

use of com.redhat.ceylon.compiler.typechecker.tree.Tree.Condition 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)

Aggregations

CustomTree (com.redhat.ceylon.compiler.typechecker.tree.CustomTree)2 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)2 Condition (com.redhat.ceylon.compiler.typechecker.tree.Tree.Condition)2 Value (com.redhat.ceylon.model.typechecker.model.Value)2 JCTree (com.sun.tools.javac.tree.JCTree)2 BoxingStrategy (com.redhat.ceylon.compiler.java.codegen.AbstractTransformer.BoxingStrategy)1 Substitution (com.redhat.ceylon.compiler.java.codegen.Naming.Substitution)1 SyntheticName (com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName)1 ConditionList (com.redhat.ceylon.compiler.typechecker.tree.Tree.ConditionList)1 Expression (com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression)1 Term (com.redhat.ceylon.compiler.typechecker.tree.Tree.Term)1 ConditionScope (com.redhat.ceylon.model.typechecker.model.ConditionScope)1 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)1 Scope (com.redhat.ceylon.model.typechecker.model.Scope)1 Type (com.redhat.ceylon.model.typechecker.model.Type)1 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)1 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)1 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)1