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