use of kalang.core.Type in project kalang by kasonyang.
the class SemanticAnalyzer method validateAssign.
public boolean validateAssign(AssignableExpr to, ExprNode from, OffsetRange offset) {
if (to instanceof VarExpr) {
LocalVarNode varObject = ((VarExpr) to).getVar();
if (Modifier.isFinal(varObject.modifier)) {
this.diagnosisReporter.report(Diagnosis.Kind.ERROR, String.format("%s is readonly", varObject.getName()), offset);
return false;
}
} else if (to instanceof FieldExpr) {
FieldDescriptor field = ((FieldExpr) to).getField();
if (Modifier.isFinal(field.getModifier())) {
this.diagnosisReporter.report(Diagnosis.Kind.ERROR, String.format("%s is readonly", field.getName()), offset);
return false;
}
}
Type toType = to.getType();
Type fromType = from.getType();
if (!toType.isAssignableFrom(fromType)) {
diagnosisReporter.report(Diagnosis.Kind.ERROR, String.format("incompatible types: %s cannot be converted to %s", fromType, toType), offset);
return false;
}
return true;
}
use of kalang.core.Type in project kalang by kasonyang.
the class SemanticAnalyzer method validateUnaryExpr.
public boolean validateUnaryExpr(UnaryExpr node) {
String op = node.getOperation();
Type et = node.getExpr().getType();
switch(op) {
case UnaryExpr.OPERATION_LOGIC_NOT:
return requireBoolean(node, et);
case UnaryExpr.OPERATION_NEG:
case UnaryExpr.OPERATION_POS:
case UnaryExpr.OPERATION_NOT:
return requireNumber(node, et);
default:
throw Exceptions.unexceptedValue(op);
}
}
use of kalang.core.Type in project kalang by kasonyang.
the class SemanticAnalyzer method validateBinaryExpr.
public boolean validateBinaryExpr(BinaryExpr node) {
ExprNode expr1 = node.getExpr1();
ExprNode expr2 = node.getExpr2();
Type t1 = expr1.getType();
Type t2 = expr2.getType();
String op = node.getOperation();
switch(op) {
case "==":
case "!=":
if (Types.isNumber(t1)) {
return this.requireNumber(expr2, t2);
} else {
return true;
// TODO pass anything.may be Object needed?
}
case "+":
case "-":
case "*":
case "/":
case "%":
case ">=":
case "<=":
case ">":
case "<":
case "&":
case "|":
case "^":
case BinaryExpr.OP_SHIFT_LEFT:
case BinaryExpr.OP_SHIFT_RIGHT:
return this.requireNumber(expr1, t1) && this.requireNumber(expr2, t2);
case "&&":
case "||":
return requireBoolean(expr1, t1) && requireBoolean(expr2, t2);
default:
diagnosisReporter.report(Diagnosis.Kind.ERROR, "unsupport operation:" + op, node.offset);
return false;
}
}
use of kalang.core.Type in project kalang by kasonyang.
the class SemanticAnalyzer method validateReturnStmt.
public boolean validateReturnStmt(MethodNode method, ReturnStmt node) {
Type retType = method.getType();
if (node.expr == null) {
if (!retType.equals(Types.VOID_TYPE)) {
diagnosisReporter.report(Diagnosis.Kind.ERROR, "missing return value", node.offset);
return false;
}
return true;
} else {
Type exType = node.expr.getType();
node.expr = this.checkAssign(node.expr, exType, retType, node);
if (node.expr == null)
return false;
return true;
}
}
use of kalang.core.Type in project kalang by kasonyang.
the class Ast2Class method getPrimitiveCastOpc.
private int getPrimitiveCastOpc(Type fromType, Type toType) {
Type f = fromType;
Type tt = toType;
if (f.equals(INT_TYPE)) {
if (tt.equals(LONG_TYPE))
return I2L;
else if (tt.equals(FLOAT_TYPE))
return I2F;
else if (tt.equals(DOUBLE_TYPE))
return I2D;
else if (tt.equals(SHORT_TYPE))
return I2S;
else if (tt.equals(BYTE_TYPE))
return I2B;
else if (tt.equals(CHAR_TYPE))
return I2C;
} else if (f.equals(FLOAT_TYPE)) {
if (tt.equals(INT_TYPE))
return F2I;
else if (tt.equals(LONG_TYPE))
return F2L;
else if (tt.equals(DOUBLE_TYPE))
return F2D;
} else if (f.equals(LONG_TYPE)) {
if (tt.equals(INT_TYPE))
return L2I;
else if (tt.equals(FLOAT_TYPE))
return L2F;
else if (tt.equals(DOUBLE_TYPE))
return L2D;
} else if (f.equals(DOUBLE_TYPE)) {
if (tt.equals(INT_TYPE))
return D2I;
else if (tt.equals(LONG_TYPE))
return D2L;
else if (tt.equals(FLOAT_TYPE))
return D2F;
} else if (f.equals(BYTE_TYPE)) {
if (tt.equals(SHORT_TYPE))
return 0;
else if (tt.equals(INT_TYPE))
return 0;
else if (tt.equals(LONG_TYPE))
return I2L;
else if (tt.equals(FLOAT_TYPE))
return I2F;
else if (tt.equals(DOUBLE_TYPE))
return I2D;
} else if (f.equals(CHAR_TYPE) || f.equals(SHORT_TYPE)) {
if (tt.equals(INT_TYPE))
return 0;
else if (tt.equals(LONG_TYPE))
return I2L;
else if (tt.equals(FLOAT_TYPE))
return I2F;
else if (tt.equals(DOUBLE_TYPE))
return I2D;
}
throw Exceptions.unexceptedException("It is unable to cast " + fromType + " to " + toType);
}
Aggregations