Search in sources :

Example 6 with Type

use of kalang.core.Type in project kalang by kasonyang.

the class Ast2Class method visitNewArrayExpr.

@Override
public Object visitNewArrayExpr(NewArrayExpr node) {
    visit(node.getSize());
    Type t = node.getComponentType();
    int opr = -1;
    int op = NEWARRAY;
    if (t.equals(BOOLEAN_TYPE)) {
        opr = T_BOOLEAN;
    } else if (t.equals(CHAR_TYPE)) {
        opr = T_CHAR;
    } else if (t.equals(SHORT_TYPE)) {
        opr = T_SHORT;
    } else if (t.equals(INT_TYPE)) {
        opr = T_INT;
    } else if (t.equals(LONG_TYPE)) {
        opr = T_LONG;
    } else if (t.equals(FLOAT_TYPE)) {
        opr = T_FLOAT;
    } else if (t.equals(DOUBLE_TYPE)) {
        opr = T_DOUBLE;
    } else if (t.equals(BYTE_TYPE)) {
        opr = T_BYTE;
    } else {
        op = ANEWARRAY;
    }
    if (op == NEWARRAY) {
        md.visitIntInsn(op, opr);
    } else {
        md.visitTypeInsn(ANEWARRAY, internalName(t));
    }
    return null;
}
Also used : Type(kalang.core.Type) GenericType(kalang.core.GenericType) WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ObjectType(kalang.core.ObjectType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType)

Example 7 with Type

use of kalang.core.Type in project kalang by kasonyang.

the class Ast2Class method visitIncrementExpr.

@Override
public Object visitIncrementExpr(IncrementExpr node) {
    if (!node.isIsPrefix()) {
        visit(node.getExpr());
    }
    Type exprType = node.getExpr().getType();
    ConstExpr ce = getConstX(exprType, node.isIsDesc() ? -1 : 1);
    BinaryExpr be = new MathExpr(node.getExpr(), ce, "+");
    AssignExpr addOne = new AssignExpr(node.getExpr(), be);
    visit(addOne);
    pop(exprType);
    if (node.isIsPrefix()) {
        visit(node.getExpr());
    }
    return null;
}
Also used : Type(kalang.core.Type) GenericType(kalang.core.GenericType) WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ObjectType(kalang.core.ObjectType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType) ConstExpr(kalang.ast.ConstExpr) BinaryExpr(kalang.ast.BinaryExpr) MathExpr(kalang.ast.MathExpr) AssignExpr(kalang.ast.AssignExpr)

Example 8 with Type

use of kalang.core.Type in project kalang by kasonyang.

the class Ast2Class method ifCompare.

private void ifCompare(boolean jumpOnTrue, ExprNode expr1, ExprNode expr2, String op, Label label) {
    Type type = expr1.getType();
    visit(expr1);
    visit(expr2);
    int t = getT(type);
    if (T_I == t) {
        int opc = -1;
        switch(op) {
            case "==":
                opc = jumpOnTrue ? IF_ICMPEQ : IF_ICMPNE;
                break;
            case ">":
                opc = jumpOnTrue ? IF_ICMPGT : IF_ICMPLE;
                break;
            case ">=":
                opc = jumpOnTrue ? IF_ICMPGE : IF_ICMPLT;
                break;
            case "<":
                opc = jumpOnTrue ? IF_ICMPLT : IF_ICMPGE;
                break;
            case "<=":
                opc = jumpOnTrue ? IF_ICMPLE : IF_ICMPGT;
                break;
            case "!=":
                opc = jumpOnTrue ? IF_ICMPNE : IF_ICMPEQ;
                break;
            default:
                throw new UnsupportedOperationException("Unsupported operation:" + op);
        }
        md.visitJumpInsn(opc, label);
    } else if (T_A == t) {
        // object type
        if (op.equals("==")) {
            md.visitJumpInsn(jumpOnTrue ? IF_ACMPEQ : IF_ACMPNE, label);
        } else if (op.equals("!=")) {
            md.visitJumpInsn(jumpOnTrue ? IF_ACMPNE : IF_ACMPEQ, label);
        } else {
            throw new UnsupportedOperationException("It is unsupported to compare object type:" + type);
        }
    } else {
        // type is not int,not object
        if (T_L == t) {
            md.visitInsn(LCMP);
        } else if (T_F == t) {
            md.visitInsn(FCMPL);
        } else if (T_D == t) {
            md.visitInsn(DCMPL);
        } else {
            throw new UnsupportedOperationException("It is unsupported to compare object type:" + type);
        }
        int opc = -1;
        switch(op) {
            case "==":
                opc = jumpOnTrue ? IFEQ : IFNE;
                break;
            case ">":
                opc = jumpOnTrue ? IFGT : IFLE;
                break;
            case ">=":
                opc = jumpOnTrue ? IFGE : IFLT;
                break;
            case "<":
                opc = jumpOnTrue ? IFLT : IFGE;
                break;
            case "<=":
                opc = jumpOnTrue ? IFLE : IFGT;
                break;
            case "!=":
                opc = jumpOnTrue ? IFNE : IFEQ;
                break;
            default:
                throw new UnsupportedOperationException("Unsupported operation:" + op);
        }
        md.visitJumpInsn(opc, label);
    }
}
Also used : Type(kalang.core.Type) GenericType(kalang.core.GenericType) WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ObjectType(kalang.core.ObjectType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType)

Example 9 with Type

use of kalang.core.Type in project kalang by kasonyang.

the class AstBuilder method visitLocalVarDecl.

@Override
public Statement visitLocalVarDecl(LocalVarDeclContext ctx) {
    MultiStmt ms = new MultiStmt();
    for (VarDeclContext v : ctx.varDecl()) {
        TypeContext varType = v.varType;
        Type exceptedType = varType == null ? null : parseType(varType);
        ExprNode initExpr = null;
        ExpressionContext initExprContext = v.expression();
        if (initExprContext != null) {
            if (initExprContext instanceof LiteralExprContext) {
                initExpr = this.parseLiteral(((LiteralExprContext) initExprContext).literal(), exceptedType);
            } else {
                initExpr = visitExpression(initExprContext);
            }
        }
        VarInfo varInfo = varDecl(v, initExpr == null ? Types.getRootType() : initExpr.getType());
        LocalVarNode localVar = this.declareLocalVar(varInfo.name, varInfo.type, varInfo.modifier, ctx);
        if (localVar == null)
            return null;
        VarDeclStmt vds = new VarDeclStmt(localVar);
        ms.statements.add(vds);
        if (initExpr != null) {
            AssignExpr assignExpr = new AssignExpr(new VarExpr(localVar), initExpr);
            mapAst(assignExpr, v);
            ms.statements.add(new ExprStmt(assignExpr));
        }
        mapAst(localVar, ctx);
    }
    return ms;
}
Also used : MultiStmt(kalang.ast.MultiStmt) TypeContext(kalang.antlr.KalangParser.TypeContext) AssignExpr(kalang.ast.AssignExpr) 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) ExprStmt(kalang.ast.ExprStmt) ExpressionContext(kalang.antlr.KalangParser.ExpressionContext) VarDeclStmt(kalang.ast.VarDeclStmt) VarExpr(kalang.ast.VarExpr) LiteralExprContext(kalang.antlr.KalangParser.LiteralExprContext) LocalVarNode(kalang.ast.LocalVarNode) VarDeclContext(kalang.antlr.KalangParser.VarDeclContext) LocalVarDeclContext(kalang.antlr.KalangParser.LocalVarDeclContext)

Example 10 with Type

use of kalang.core.Type 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)

Aggregations

ObjectType (kalang.core.ObjectType)44 Type (kalang.core.Type)44 ArrayType (kalang.core.ArrayType)37 ClassType (kalang.core.ClassType)36 GenericType (kalang.core.GenericType)36 PrimitiveType (kalang.core.PrimitiveType)33 WildcardType (kalang.core.WildcardType)30 ExprNode (kalang.ast.ExprNode)17 LinkedList (java.util.LinkedList)7 ExprStmt (kalang.ast.ExprStmt)7 VarExpr (kalang.ast.VarExpr)7 Nullable (javax.annotation.Nullable)6 AssignExpr (kalang.ast.AssignExpr)6 LocalVarNode (kalang.ast.LocalVarNode)6 BlockStmt (kalang.ast.BlockStmt)5 VarDeclStmt (kalang.ast.VarDeclStmt)5 AmbiguousMethodException (kalang.AmbiguousMethodException)4 MethodNotFoundException (kalang.MethodNotFoundException)4 ConstExpr (kalang.ast.ConstExpr)4 MethodNode (kalang.ast.MethodNode)4