Search in sources :

Example 6 with ConditionScope

use of org.eclipse.ceylon.model.typechecker.model.ConditionScope 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

ConditionScope (org.eclipse.ceylon.model.typechecker.model.ConditionScope)6 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)4 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)3 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)3 Value (org.eclipse.ceylon.model.typechecker.model.Value)3 Condition (org.eclipse.ceylon.compiler.typechecker.tree.Tree.Condition)2 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)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 HasErrorException (org.eclipse.ceylon.compiler.java.codegen.recovery.HasErrorException)1 ExistsCondition (org.eclipse.ceylon.compiler.typechecker.tree.Tree.ExistsCondition)1 ExistsOrNonemptyCondition (org.eclipse.ceylon.compiler.typechecker.tree.Tree.ExistsOrNonemptyCondition)1 Expression (org.eclipse.ceylon.compiler.typechecker.tree.Tree.Expression)1 ForStatement (org.eclipse.ceylon.compiler.typechecker.tree.Tree.ForStatement)1 IsCondition (org.eclipse.ceylon.compiler.typechecker.tree.Tree.IsCondition)1 NonemptyCondition (org.eclipse.ceylon.compiler.typechecker.tree.Tree.NonemptyCondition)1 Return (org.eclipse.ceylon.compiler.typechecker.tree.Tree.Return)1 Statement (org.eclipse.ceylon.compiler.typechecker.tree.Tree.Statement)1 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)1