Search in sources :

Example 1 with FieldDescriptor

use of kalang.core.FieldDescriptor 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;
}
Also used : ArrayType(kalang.core.ArrayType) Type(kalang.core.Type) ObjectType(kalang.core.ObjectType) MathType(kalang.util.MathType) PrimitiveType(kalang.core.PrimitiveType) VarExpr(kalang.ast.VarExpr) FieldExpr(kalang.ast.FieldExpr) LocalVarNode(kalang.ast.LocalVarNode) FieldDescriptor(kalang.core.FieldDescriptor)

Example 2 with FieldDescriptor

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

the class ObjectFieldExpr method create.

@Nonnull
public static FieldExpr create(@Nonnull ExprNode target, String fieldName, @Nullable ClassNode caller) throws FieldNotFoundException {
    Type type = target.getType();
    if (!(type instanceof ObjectType)) {
        throw new UnsupportedOperationException("unsupported type:" + type);
    }
    ObjectType classType = (ObjectType) type;
    FieldDescriptor field = getField(classType, fieldName, caller);
    if (AstUtil.isStatic(field.getModifier())) {
        throw new FieldNotFoundException(fieldName + " is static");
    }
    return new ObjectFieldExpr(target, field);
}
Also used : ObjectType(kalang.core.ObjectType) Type(kalang.core.Type) ObjectType(kalang.core.ObjectType) FieldNotFoundException(kalang.FieldNotFoundException) FieldDescriptor(kalang.core.FieldDescriptor) Nonnull(javax.annotation.Nonnull)

Aggregations

FieldDescriptor (kalang.core.FieldDescriptor)2 ObjectType (kalang.core.ObjectType)2 Type (kalang.core.Type)2 Nonnull (javax.annotation.Nonnull)1 FieldNotFoundException (kalang.FieldNotFoundException)1 FieldExpr (kalang.ast.FieldExpr)1 LocalVarNode (kalang.ast.LocalVarNode)1 VarExpr (kalang.ast.VarExpr)1 ArrayType (kalang.core.ArrayType)1 PrimitiveType (kalang.core.PrimitiveType)1 MathType (kalang.util.MathType)1