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;
}
Aggregations