Search in sources :

Example 1 with ObjectFieldExpr

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

the class Ast2Class method visitFieldExpr.

@Override
public Object visitFieldExpr(FieldExpr node) {
    int opc;
    String owner = internalName(node.getField().getFieldNode().getClassNode());
    if (node instanceof ObjectFieldExpr) {
        ExprNode target = ((ObjectFieldExpr) node).getTarget();
        visit(target);
        opc = GETFIELD;
    } else if (node instanceof StaticFieldExpr) {
        opc = GETSTATIC;
    } else {
        throw new UnsupportedOperationException("unsupported field type:" + node);
    }
    md.visitFieldInsn(opc, owner, node.getField().getName(), getTypeDescriptor(node.getType()));
    return null;
}
Also used : ExprNode(kalang.ast.ExprNode) ObjectFieldExpr(kalang.ast.ObjectFieldExpr) StaticFieldExpr(kalang.ast.StaticFieldExpr)

Example 2 with ObjectFieldExpr

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

the class AstBuilder method visitFieldDecl.

@Override
public Void visitFieldDecl(FieldDeclContext ctx) {
    int fieldModifier = this.parseModifier(ctx.varModifier());
    for (VarDeclContext vd : ctx.varDecl()) {
        ExprNode initExpr;
        if (vd.expression() != null) {
            initExpr = visitExpression(vd.expression());
        } else {
            initExpr = null;
        }
        VarInfo varInfo = varDecl(vd, initExpr == null ? Types.getRootType() : initExpr.getType());
        varInfo.modifier |= fieldModifier;
        FieldNode fieldNode = thisClazz.createField(varInfo.type, varInfo.name, varInfo.modifier);
        // TODO simplify it
        if (initExpr != null) {
            if (AstUtil.isStatic(fieldNode.modifier)) {
                thisClazz.staticInitStmts.add(new ExprStmt(new AssignExpr(new StaticFieldExpr(new ClassReference(thisClazz), fieldNode), initExpr)));
            } else {
                thisClazz.initStmts.add(new ExprStmt(new AssignExpr(new ObjectFieldExpr(new ThisExpr(getThisType()), fieldNode), initExpr)));
            }
        }
    }
    return null;
}
Also used : ExprNode(kalang.ast.ExprNode) StaticFieldExpr(kalang.ast.StaticFieldExpr) ObjectFieldExpr(kalang.ast.ObjectFieldExpr) FieldNode(kalang.ast.FieldNode) ExprStmt(kalang.ast.ExprStmt) ClassReference(kalang.ast.ClassReference) ThisExpr(kalang.ast.ThisExpr) VarDeclContext(kalang.antlr.KalangParser.VarDeclContext) LocalVarDeclContext(kalang.antlr.KalangParser.LocalVarDeclContext) AssignExpr(kalang.ast.AssignExpr)

Example 3 with ObjectFieldExpr

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

the class ClassNodeMetaBuilder method visitFieldDecl.

@Override
public Void visitFieldDecl(KalangParser.FieldDeclContext ctx) {
    int fieldModifier = astBuilder.parseModifier(ctx.varModifier());
    for (KalangParser.VarDeclContext vd : ctx.varDecl()) {
        ExprNode initExpr;
        if (vd.expression() != null) {
            initExpr = astBuilder.visitExpression(vd.expression());
        } else {
            initExpr = null;
        }
        AstBuilder.VarInfo varInfo = astBuilder.varDecl(vd, initExpr == null ? Types.getRootType() : initExpr.getType());
        varInfo.modifier |= fieldModifier;
        FieldNode fieldNode = thisClazz.createField(varInfo.type, varInfo.name, varInfo.modifier);
        // TODO simplify it
        if (initExpr != null) {
            if (AstUtil.isStatic(fieldNode.modifier)) {
                thisClazz.staticInitStmts.add(new ExprStmt(new AssignExpr(new StaticFieldExpr(new ClassReference(thisClazz), fieldNode), initExpr)));
            } else {
                thisClazz.initStmts.add(new ExprStmt(new AssignExpr(new ObjectFieldExpr(new ThisExpr(Types.getClassType(thisClazz)), fieldNode), initExpr)));
            }
        }
    }
    return null;
}
Also used : FieldNode(kalang.ast.FieldNode) AssignExpr(kalang.ast.AssignExpr) ExprNode(kalang.ast.ExprNode) StaticFieldExpr(kalang.ast.StaticFieldExpr) ObjectFieldExpr(kalang.ast.ObjectFieldExpr) KalangParser(kalang.antlr.KalangParser) ExprStmt(kalang.ast.ExprStmt) ClassReference(kalang.ast.ClassReference) ThisExpr(kalang.ast.ThisExpr)

Example 4 with ObjectFieldExpr

use of kalang.ast.ObjectFieldExpr 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 5 with ObjectFieldExpr

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

ObjectFieldExpr (kalang.ast.ObjectFieldExpr)5 StaticFieldExpr (kalang.ast.StaticFieldExpr)5 ClassReference (kalang.ast.ClassReference)4 ExprNode (kalang.ast.ExprNode)4 ThisExpr (kalang.ast.ThisExpr)4 AssignExpr (kalang.ast.AssignExpr)3 ExprStmt (kalang.ast.ExprStmt)3 BlockStmt (kalang.ast.BlockStmt)2 FieldExpr (kalang.ast.FieldExpr)2 FieldNode (kalang.ast.FieldNode)2 MethodNode (kalang.ast.MethodNode)2 KalangParser (kalang.antlr.KalangParser)1 LocalVarDeclContext (kalang.antlr.KalangParser.LocalVarDeclContext)1 VarDeclContext (kalang.antlr.KalangParser.VarDeclContext)1 ParameterExpr (kalang.ast.ParameterExpr)1 ParameterNode (kalang.ast.ParameterNode)1 ReturnStmt (kalang.ast.ReturnStmt)1