Search in sources :

Example 1 with Condition

use of org.eclipse.ceylon.compiler.typechecker.tree.Tree.Condition in project ceylon by eclipse.

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, newValue.hasUncheckedNullType() ? ExpressionTransformer.EXPR_TARGET_ACCEPTS_NULL : 0);
    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(org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName) Type(org.eclipse.ceylon.model.typechecker.model.Type) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) Substitution(org.eclipse.ceylon.compiler.java.codegen.Naming.Substitution) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) ConditionScope(org.eclipse.ceylon.model.typechecker.model.ConditionScope) ConditionScope(org.eclipse.ceylon.model.typechecker.model.ConditionScope) Condition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Condition) Expression(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Expression) Value(org.eclipse.ceylon.model.typechecker.model.Value) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree)

Example 2 with Condition

use of org.eclipse.ceylon.compiler.typechecker.tree.Tree.Condition in project ceylon by eclipse.

the class ConditionGenerator method specialConditions.

/**
 * Handles the "is", "exists" and "nonempty" conditions, with a pre-generated
 * list of the variables from the conditions.
 */
void specialConditions(final List<VarHolder> vars, Tree.ConditionList conditions, String keyword, final boolean forAssert) {
    // Second pass: generate the conditions
    if (!keyword.isEmpty()) {
        gen.out(keyword, "(");
    }
    boolean first = true;
    final Iterator<VarHolder> ivars = vars.iterator();
    for (Condition cond : conditions.getConditions()) {
        if (first) {
            first = false;
        } else {
            gen.out("&&");
        }
        if (cond instanceof Tree.BooleanCondition) {
            cond.visit(gen);
        } else {
            specialCondition(ivars.next(), cond, forAssert);
        }
    }
    if (!keyword.isEmpty()) {
        gen.out(")");
    }
}
Also used : ExistsCondition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.ExistsCondition) Condition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Condition) ExistsOrNonemptyCondition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.ExistsOrNonemptyCondition) IsCondition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.IsCondition) NonemptyCondition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.NonemptyCondition)

Example 3 with Condition

use of org.eclipse.ceylon.compiler.typechecker.tree.Tree.Condition in project ceylon by eclipse.

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(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Term) Condition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Condition) Value(org.eclipse.ceylon.model.typechecker.model.Value) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 4 with Condition

use of org.eclipse.ceylon.compiler.typechecker.tree.Tree.Condition in project ceylon by eclipse.

the class ConditionGenerator method gatherVariables.

/**
 * Generate a list of all the variables from conditions in the list.
 * @param conditions The ConditionList that may contain variable declarations.
 * @param output Whether to generate the variable declarations or not.
 */
List<VarHolder> gatherVariables(Tree.ConditionList conditions, boolean output, final boolean forAssert) {
    ArrayList<VarHolder> vars = new ArrayList<>();
    boolean first = true;
    for (Condition cond : conditions.getConditions()) {
        Tree.Variable variable = null;
        Tree.Destructure destruct = null;
        if (cond instanceof ExistsOrNonemptyCondition) {
            ExistsOrNonemptyCondition enc = (ExistsOrNonemptyCondition) cond;
            if (enc.getVariable() instanceof Tree.Variable) {
                variable = (Tree.Variable) enc.getVariable();
            } else if (enc.getVariable() instanceof Tree.Destructure) {
                destruct = (Tree.Destructure) enc.getVariable();
            }
        } else if (cond instanceof IsCondition) {
            variable = ((IsCondition) cond).getVariable();
        } else if (!(cond instanceof Tree.BooleanCondition)) {
            cond.addUnexpectedError("No support for conditions of type " + cond.getClass().getSimpleName(), Backend.JavaScript);
            return null;
        }
        if (variable != null) {
            final Tree.Term variableRHS = variable.getSpecifierExpression().getExpression().getTerm();
            final Value vdecl = variable.getDeclarationModel();
            String varName = names.name(vdecl);
            final boolean member = ModelUtil.getRealScope(vdecl.getContainer()) instanceof ClassOrInterface;
            if (member) {
                // that should become attributes. For some reason the typechecker doesn't do this.
                if (vdecl.getScope() instanceof ConditionScope) {
                    vdecl.setContainer(ModelUtil.getRealScope(vdecl.getContainer()));
                    varName = names.name(vdecl);
                }
            } else if (output) {
                if (first) {
                    first = false;
                    gen.out("var ");
                } else {
                    gen.out(",");
                }
                gen.out(varName);
                directAccess.add(vdecl);
            }
            vars.add(new VarHolder(variable, variableRHS, varName, member));
        } else if (destruct != null) {
            final Destructurer d = new Destructurer(destruct.getPattern(), null, directAccess, "", first, forAssert);
            for (Tree.Variable v : d.getVariables()) {
                if (output) {
                    final String vname = names.name(v.getDeclarationModel());
                    if (first) {
                        first = false;
                        gen.out("var ", vname);
                    } else {
                        gen.out(",", vname);
                    }
                }
            }
            VarHolder vh = new VarHolder(destruct, null, null, false);
            vh.vars = d.getVariables();
            vars.add(vh);
        }
    }
    if (output && !first) {
        gen.endLine(true);
    }
    return vars;
}
Also used : ExistsCondition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.ExistsCondition) Condition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Condition) ExistsOrNonemptyCondition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.ExistsOrNonemptyCondition) IsCondition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.IsCondition) NonemptyCondition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.NonemptyCondition) IsCondition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.IsCondition) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) ArrayList(java.util.ArrayList) ExistsOrNonemptyCondition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.ExistsOrNonemptyCondition) ConditionScope(org.eclipse.ceylon.model.typechecker.model.ConditionScope) Value(org.eclipse.ceylon.model.typechecker.model.Value) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree)

Aggregations

Condition (org.eclipse.ceylon.compiler.typechecker.tree.Tree.Condition)4 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)3 Value (org.eclipse.ceylon.model.typechecker.model.Value)3 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)2 ExistsCondition (org.eclipse.ceylon.compiler.typechecker.tree.Tree.ExistsCondition)2 ExistsOrNonemptyCondition (org.eclipse.ceylon.compiler.typechecker.tree.Tree.ExistsOrNonemptyCondition)2 IsCondition (org.eclipse.ceylon.compiler.typechecker.tree.Tree.IsCondition)2 NonemptyCondition (org.eclipse.ceylon.compiler.typechecker.tree.Tree.NonemptyCondition)2 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)2 ConditionScope (org.eclipse.ceylon.model.typechecker.model.ConditionScope)2 ArrayList (java.util.ArrayList)1 Substitution (org.eclipse.ceylon.compiler.java.codegen.Naming.Substitution)1 SyntheticName (org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName)1 Expression (org.eclipse.ceylon.compiler.typechecker.tree.Tree.Expression)1 Term (org.eclipse.ceylon.compiler.typechecker.tree.Tree.Term)1 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)1 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)1 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)1 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)1 Type (org.eclipse.ceylon.model.typechecker.model.Type)1