Search in sources :

Example 1 with VarObject

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);
        }
    }
}
Also used : ObjectType(kalang.core.ObjectType) WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType) Type(kalang.core.Type) GenericType(kalang.core.GenericType) ObjectType(kalang.core.ObjectType) ParameterExpr(kalang.ast.ParameterExpr) VarExpr(kalang.ast.VarExpr) VarObject(kalang.ast.VarObject)

Example 2 with VarObject

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;
}
Also used : ExprNode(kalang.ast.ExprNode) WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType) Type(kalang.core.Type) GenericType(kalang.core.GenericType) ObjectType(kalang.core.ObjectType) IfStmt(kalang.ast.IfStmt) BlockStmt(kalang.ast.BlockStmt) VarObject(kalang.ast.VarObject)

Example 3 with VarObject

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);
    }
}
Also used : VarObject(kalang.ast.VarObject)

Aggregations

VarObject (kalang.ast.VarObject)3 ArrayType (kalang.core.ArrayType)2 ClassType (kalang.core.ClassType)2 GenericType (kalang.core.GenericType)2 ObjectType (kalang.core.ObjectType)2 PrimitiveType (kalang.core.PrimitiveType)2 Type (kalang.core.Type)2 WildcardType (kalang.core.WildcardType)2 BlockStmt (kalang.ast.BlockStmt)1 ExprNode (kalang.ast.ExprNode)1 IfStmt (kalang.ast.IfStmt)1 ParameterExpr (kalang.ast.ParameterExpr)1 VarExpr (kalang.ast.VarExpr)1