use of kalang.ast.VarObject in project kalang by kasonyang.
the class AstBuilder method onAssign.
private void onAssign(ExprNode to, ExprNode expr) {
removeOverrideType(to);
if (to instanceof VarExpr) {
((VarExpr) to).removeOverrideType();
} else if (to instanceof ParameterExpr) {
((ParameterExpr) to).removeOverrideType();
}
VarObject key = getOverrideTypeKey(to);
if (key != null) {
Type toType = to.getType();
if (toType instanceof ObjectType) {
Type type = expr.getType();
int ns;
if (Types.NULL_TYPE.equals(type)) {
ns = NULLSTATE_MUST_NULL;
} else if (type instanceof ObjectType) {
ns = getNullState(((ObjectType) type).getNullable());
} else {
throw Exceptions.unexceptedValue(type);
}
nullState.put(key, ns);
}
}
}
use of kalang.ast.VarObject in project kalang by kasonyang.
the class AstBuilder method visitIfStat.
@Override
public AstNode visitIfStat(IfStatContext ctx) {
ExprNode expr = visitExpression(ctx.expression());
if (expr == null) {
return null;
}
Type exprType = expr.getType();
expr = BoxUtil.assign(expr, expr.getType(), Types.BOOLEAN_TYPE);
if (expr == null) {
this.diagnosisReporter.report(Diagnosis.Kind.ERROR, exprType + " cannot be converted to boolean", ctx.expression());
return null;
}
BlockStmt trueBody = null;
BlockStmt falseBody = null;
VarTable<VarObject, Integer> trueAssigned, falseAssigned;
this.nullState = trueAssigned = this.nullState.newStack();
newOverrideTypeStack();
onIf(expr, true);
if (ctx.trueStmt != null) {
trueBody = requireBlock(ctx.trueStmt);
}
popOverrideTypeStack();
this.nullState = this.nullState.popStack();
boolean trueReturned = this.returned;
this.returned = false;
this.nullState = falseAssigned = this.nullState.newStack();
newOverrideTypeStack();
onIf(expr, false);
if (ctx.falseStmt != null) {
falseBody = requireBlock(ctx.falseStmt);
}
popOverrideTypeStack();
this.nullState = this.nullState.popStack();
handleMultiBranchedAssign(trueAssigned.vars(), falseAssigned.vars());
boolean falseReturned = this.returned;
if (trueReturned)
onIf(expr, false);
if (falseReturned)
onIf(expr, true);
this.returned = falseReturned && trueReturned;
IfStmt ifStmt = new IfStmt(expr, trueBody, falseBody);
mapAst(ifStmt, ctx);
return ifStmt;
}
use of kalang.ast.VarObject in project kalang by kasonyang.
the class AstBuilder method onNull.
private void onNull(ExprNode expr, boolean onTrue, boolean isEQ) {
boolean mustNull = (onTrue && isEQ) || (!onTrue && !isEQ);
VarObject key = this.getOverrideTypeKey(expr);
if (key != null) {
nullState.put(key, mustNull ? NULLSTATE_MUST_NULL : NULLSTATE_MUST_NONNULL);
}
}
Aggregations