Search in sources :

Example 1 with ExistsOrNonemptyCondition

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

the class ConditionGenerator method specialConditionCheck.

private void specialConditionCheck(Condition condition, Tree.Term variableRHS, String varName, final boolean forAssert) {
    if (condition instanceof ExistsOrNonemptyCondition) {
        ExistsOrNonemptyCondition enc = (ExistsOrNonemptyCondition) condition;
        if (enc.getNot()) {
            gen.out("!");
        }
        if (condition instanceof Tree.NonemptyCondition) {
            gen.out(gen.getClAlias(), "ne$(");
        } else {
            // exists
            gen.out(gen.getClAlias(), "nn$(");
        }
        specialConditionRHS(variableRHS, varName);
        gen.out(")");
    } else {
        IsCondition ic = (IsCondition) condition;
        Tree.Type type = ic.getType();
        gen.generateIsOfType(variableRHS, null, type.getTypeModel(), varName, ic.getNot(), forAssert);
    }
}
Also used : IsCondition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.IsCondition) ExistsOrNonemptyCondition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.ExistsOrNonemptyCondition) NonemptyCondition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.NonemptyCondition) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) ExistsOrNonemptyCondition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.ExistsOrNonemptyCondition)

Example 2 with ExistsOrNonemptyCondition

use of org.eclipse.ceylon.compiler.typechecker.tree.Tree.ExistsOrNonemptyCondition 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

Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)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 ArrayList (java.util.ArrayList)1 Condition (org.eclipse.ceylon.compiler.typechecker.tree.Tree.Condition)1 ExistsCondition (org.eclipse.ceylon.compiler.typechecker.tree.Tree.ExistsCondition)1 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)1 ConditionScope (org.eclipse.ceylon.model.typechecker.model.ConditionScope)1 Value (org.eclipse.ceylon.model.typechecker.model.Value)1