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);
}
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(")");
}
}
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);
}
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;
}
Aggregations