Search in sources :

Example 26 with Type

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

the class Ast2Class method visitPrimitiveCastExpr.

@Override
public Object visitPrimitiveCastExpr(PrimitiveCastExpr node) {
    ExprNode expr = node.getExpr();
    visit(expr);
    int opc;
    Type ft = expr.getType();
    Type tt = node.getToType();
    opc = getPrimitiveCastOpc(ft, tt);
    if (opc > 0) {
        md.visitInsn(opc);
    }
    return null;
}
Also used : ExprNode(kalang.ast.ExprNode) 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 27 with Type

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

the class Ast2Class method visitUnaryExpr.

@Override
public Object visitUnaryExpr(UnaryExpr node) {
    Type exprType = node.getExpr().getType();
    org.objectweb.asm.Type t = asmType(exprType);
    visit(node.getExpr());
    switch(node.getOperation()) {
        case UnaryExpr.OPERATION_POS:
            break;
        case UnaryExpr.OPERATION_NEG:
            md.visitInsn(t.getOpcode(INEG));
            break;
        case UnaryExpr.OPERATION_NOT:
            // TODO here I am not sure
            constX(exprType, -1);
            md.visitInsn(t.getOpcode(IXOR));
            break;
        // md.visitInsn(ICONST_M1);
        case UnaryExpr.OPERATION_LOGIC_NOT:
            Label falseLabel = new Label();
            Label stopLabel = new Label();
            md.visitJumpInsn(IFEQ, falseLabel);
            constFalse();
            md.visitJumpInsn(GOTO, stopLabel);
            md.visitLabel(falseLabel);
            constTrue();
            md.visitLabel(stopLabel);
            break;
        default:
            throw new UnsupportedOperationException("unsupported unary operation:" + node.getOperation());
    }
    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) Label(org.objectweb.asm.Label)

Example 28 with Type

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

the class Ast2Class method visitReturnStmt.

@Override
public Object visitReturnStmt(ReturnStmt node) {
    int lnsn = RETURN;
    if (node.expr != null) {
        visit(node.expr);
        Type type = node.expr.getType();
        lnsn = asmType(type).getOpcode(IRETURN);
    }
    md.visitInsn(lnsn);
    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 29 with Type

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

the class Ast2Class method visitExprStmt.

@Override
public Object visitExprStmt(ExprStmt node) {
    visitChildren(node);
    Type type = node.getExpr().getType();
    if (type != null && !Types.VOID_TYPE.equals(type)) {
        pop(type);
    }
    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 30 with Type

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

the class Ast2Class method typeSignature.

@Nullable
private String typeSignature(Type type) {
    if (type instanceof GenericType) {
        return "T" + type.getName() + ";";
    } else if (type instanceof ClassType) {
        ClassType pt = (ClassType) type;
        String ptypes = "";
        for (Type p : pt.getTypeArguments()) {
            ptypes += typeSignature(p);
        }
        if (!ptypes.isEmpty())
            ptypes = "<" + ptypes + ">";
        return "L" + pt.getClassNode().name.replace('.', '/') + ptypes + ";";
    } else if (type instanceof PrimitiveType) {
        return getTypeDescriptor(type);
    } else if (type instanceof ArrayType) {
        return "[" + typeSignature(((ArrayType) type).getComponentType());
    } else if (type instanceof WildcardType) {
        WildcardType wt = (WildcardType) type;
        Type[] lbs = wt.getLowerBounds();
        Type[] ubs = wt.getUpperBounds();
        if (lbs.length > 0) {
            // FIXME handle other lowerBounds
            return "-" + typeSignature(lbs[0]);
        } else if (ubs.length > 0) {
            // FIXME handle other lowerBounds
            return "+" + typeSignature(ubs[0]);
        } else {
            return "*";
        }
    } else {
        throw Exceptions.unsupportedTypeException(type);
    }
}
Also used : ArrayType(kalang.core.ArrayType) GenericType(kalang.core.GenericType) 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) WildcardType(kalang.core.WildcardType) PrimitiveType(kalang.core.PrimitiveType) ClassType(kalang.core.ClassType) Nullable(javax.annotation.Nullable)

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