Search in sources :

Example 1 with ConditionList

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

Aggregations

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 CustomTree (com.redhat.ceylon.compiler.typechecker.tree.CustomTree)1 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)1 Condition (com.redhat.ceylon.compiler.typechecker.tree.Tree.Condition)1 ConditionList (com.redhat.ceylon.compiler.typechecker.tree.Tree.ConditionList)1 Expression (com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression)1 ConditionScope (com.redhat.ceylon.model.typechecker.model.ConditionScope)1 Scope (com.redhat.ceylon.model.typechecker.model.Scope)1 Type (com.redhat.ceylon.model.typechecker.model.Type)1 Value (com.redhat.ceylon.model.typechecker.model.Value)1 JCTree (com.sun.tools.javac.tree.JCTree)1 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)1