Search in sources :

Example 1 with StaticFieldExpr

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

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

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

the class AstBuilder method getImplicitInvokeExpr.

@Nullable
private ExprNode getImplicitInvokeExpr(String methodName, ExprNode[] args, ParserRuleContext ctx) {
    ExprNode expr = null;
    try {
        ObjectType clazzType = getThisType();
        InvocationExpr.MethodSelection ms = InvocationExpr.applyMethod(clazzType, methodName, args, clazzType.getMethodDescriptors(thisClazz, true, true));
        if (Modifier.isStatic(ms.selectedMethod.getModifier())) {
            expr = new StaticInvokeExpr(new ClassReference(thisClazz), ms.selectedMethod, ms.appliedArguments);
        } else {
            expr = new ObjectInvokeExpr(new ThisExpr(getThisType()), ms.selectedMethod, ms.appliedArguments);
        }
    } catch (MethodNotFoundException ex) {
        if (args.length == 1 && (methodName.equals("print") || methodName.equals("println"))) {
            try {
                StaticFieldExpr fieldExpr = StaticFieldExpr.create(new ClassReference(Types.requireClassType("java.lang.System").getClassNode()), "out", null);
                expr = getObjectInvokeExpr(fieldExpr, methodName, args, ctx);
            } catch (FieldNotFoundException fnfEx) {
                throw Exceptions.unexceptedException(fnfEx);
            }
        }
        if (expr == null) {
            this.methodNotFound(ctx.getStart(), className, methodName, args);
            expr = new UnknownInvocationExpr(null, methodName, args);
        }
    } catch (AmbiguousMethodException ex) {
        methodIsAmbiguous(ctx.start, ex);
        return null;
    }
    mapAst(expr, ctx);
    return expr;
}
Also used : FieldNotFoundException(kalang.FieldNotFoundException) ExprNode(kalang.ast.ExprNode) StaticInvokeExpr(kalang.ast.StaticInvokeExpr) ObjectType(kalang.core.ObjectType) StaticFieldExpr(kalang.ast.StaticFieldExpr) ObjectInvokeExpr(kalang.ast.ObjectInvokeExpr) ClassReference(kalang.ast.ClassReference) MethodNotFoundException(kalang.MethodNotFoundException) UnknownInvocationExpr(kalang.ast.UnknownInvocationExpr) InvocationExpr(kalang.ast.InvocationExpr) UnknownInvocationExpr(kalang.ast.UnknownInvocationExpr) ThisExpr(kalang.ast.ThisExpr) AmbiguousMethodException(kalang.AmbiguousMethodException) Nullable(javax.annotation.Nullable)

Example 4 with StaticFieldExpr

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

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

Aggregations

StaticFieldExpr (kalang.ast.StaticFieldExpr)6 ClassReference (kalang.ast.ClassReference)5 ExprNode (kalang.ast.ExprNode)5 ObjectFieldExpr (kalang.ast.ObjectFieldExpr)5 ThisExpr (kalang.ast.ThisExpr)5 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 Nullable (javax.annotation.Nullable)1 AmbiguousMethodException (kalang.AmbiguousMethodException)1 FieldNotFoundException (kalang.FieldNotFoundException)1 MethodNotFoundException (kalang.MethodNotFoundException)1 KalangParser (kalang.antlr.KalangParser)1 LocalVarDeclContext (kalang.antlr.KalangParser.LocalVarDeclContext)1 VarDeclContext (kalang.antlr.KalangParser.VarDeclContext)1 InvocationExpr (kalang.ast.InvocationExpr)1 ObjectInvokeExpr (kalang.ast.ObjectInvokeExpr)1