Search in sources :

Example 1 with FieldExpr

use of kalang.ast.FieldExpr 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 FieldExpr

use of kalang.ast.FieldExpr in project kalang by kasonyang.

the class AstUtil method createSetter.

public static void createSetter(ClassNode clazz, FieldDescriptor field, int accessModifier) {
    String fn = field.getName();
    String setterName = "set" + NameUtil.firstCharToUpperCase(fn);
    boolean isStatic = isStatic(field.getModifier());
    if (isStatic) {
        accessModifier |= Modifier.STATIC;
    }
    MethodNode setter = clazz.createMethodNode(Types.VOID_TYPE, setterName, accessModifier);
    // setter.offset = field.offset;
    ParameterNode param = setter.createParameter(field.getType(), field.getName());
    BlockStmt body = setter.getBody();
    FieldExpr fe;
    ExprNode paramVal = new ParameterExpr(param);
    ClassReference cr = new ClassReference(clazz);
    if (isStatic) {
        fe = new StaticFieldExpr(cr, field);
    } else {
        fe = new ObjectFieldExpr(new ThisExpr(Types.getClassType(clazz)), field);
    }
    body.statements.add(new ExprStmt(new AssignExpr(fe, paramVal)));
}
Also used : ParameterExpr(kalang.ast.ParameterExpr) BlockStmt(kalang.ast.BlockStmt) AssignExpr(kalang.ast.AssignExpr) ExprNode(kalang.ast.ExprNode) StaticFieldExpr(kalang.ast.StaticFieldExpr) ObjectFieldExpr(kalang.ast.ObjectFieldExpr) MethodNode(kalang.ast.MethodNode) ParameterNode(kalang.ast.ParameterNode) ExprStmt(kalang.ast.ExprStmt) ClassReference(kalang.ast.ClassReference) StaticFieldExpr(kalang.ast.StaticFieldExpr) FieldExpr(kalang.ast.FieldExpr) ObjectFieldExpr(kalang.ast.ObjectFieldExpr) ThisExpr(kalang.ast.ThisExpr)

Example 3 with FieldExpr

use of kalang.ast.FieldExpr in project kalang by kasonyang.

the class Ast2Class method assign.

private void assign(ExprNode to, ExprNode from) {
    if (to instanceof FieldExpr) {
        FieldExpr toField = (FieldExpr) to;
        assignField(toField, from);
    } else if (to instanceof VarExpr) {
        assignVarObject(((VarExpr) to).getVar(), from);
    } else if (to instanceof ElementExpr) {
        ElementExpr elementExpr = (ElementExpr) to;
        assignArrayElement(elementExpr.getArrayExpr(), elementExpr.getIndex(), from);
    } else {
        throw new UnknownError("unknown expression:" + to);
    }
}
Also used : VarExpr(kalang.ast.VarExpr) ObjectFieldExpr(kalang.ast.ObjectFieldExpr) StaticFieldExpr(kalang.ast.StaticFieldExpr) FieldExpr(kalang.ast.FieldExpr) UnknownFieldExpr(kalang.ast.UnknownFieldExpr) StoreArrayElementExpr(kalang.ast.StoreArrayElementExpr) ElementExpr(kalang.ast.ElementExpr)

Example 4 with FieldExpr

use of kalang.ast.FieldExpr in project kalang by kasonyang.

the class AstUtil method createGetter.

public static void createGetter(ClassNode clazz, FieldDescriptor field, int accessModifier) {
    String fn = field.getName();
    String getterName = "get" + NameUtil.firstCharToUpperCase(fn);
    boolean isStatic = isStatic(field.getModifier());
    if (isStatic) {
        accessModifier |= Modifier.STATIC;
    }
    MethodNode getter = clazz.createMethodNode(field.getType(), getterName, accessModifier);
    // getter.offset = field.offset;
    BlockStmt body = getter.getBody();
    FieldExpr fe;
    ClassReference cr = new ClassReference(clazz);
    if (isStatic) {
        fe = new StaticFieldExpr(cr, field);
    } else {
        fe = new ObjectFieldExpr(new ThisExpr(Types.getClassType(clazz)), field);
    }
    body.statements.add(new ReturnStmt(fe));
}
Also used : StaticFieldExpr(kalang.ast.StaticFieldExpr) ObjectFieldExpr(kalang.ast.ObjectFieldExpr) MethodNode(kalang.ast.MethodNode) BlockStmt(kalang.ast.BlockStmt) ClassReference(kalang.ast.ClassReference) StaticFieldExpr(kalang.ast.StaticFieldExpr) FieldExpr(kalang.ast.FieldExpr) ObjectFieldExpr(kalang.ast.ObjectFieldExpr) ReturnStmt(kalang.ast.ReturnStmt) ThisExpr(kalang.ast.ThisExpr)

Aggregations

FieldExpr (kalang.ast.FieldExpr)4 ObjectFieldExpr (kalang.ast.ObjectFieldExpr)3 StaticFieldExpr (kalang.ast.StaticFieldExpr)3 BlockStmt (kalang.ast.BlockStmt)2 ClassReference (kalang.ast.ClassReference)2 MethodNode (kalang.ast.MethodNode)2 ThisExpr (kalang.ast.ThisExpr)2 VarExpr (kalang.ast.VarExpr)2 AssignExpr (kalang.ast.AssignExpr)1 ElementExpr (kalang.ast.ElementExpr)1 ExprNode (kalang.ast.ExprNode)1 ExprStmt (kalang.ast.ExprStmt)1 LocalVarNode (kalang.ast.LocalVarNode)1 ParameterExpr (kalang.ast.ParameterExpr)1 ParameterNode (kalang.ast.ParameterNode)1 ReturnStmt (kalang.ast.ReturnStmt)1 StoreArrayElementExpr (kalang.ast.StoreArrayElementExpr)1 UnknownFieldExpr (kalang.ast.UnknownFieldExpr)1 ArrayType (kalang.core.ArrayType)1 FieldDescriptor (kalang.core.FieldDescriptor)1