Search in sources :

Example 6 with Statement

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

the class AstBuilder method visitMethods.

private void visitMethods(ClassNode clazz) {
    thisClazz = clazz;
    for (MethodNode m : thisClazz.getDeclaredMethodNodes()) {
        BlockStmt mbody = m.getBody();
        StatContext[] stats = classNodeMetaBuilder.getStatContexts(m);
        if (stats != null) {
            method = m;
            returned = false;
            visitBlockStmt(stats, mbody);
            boolean needReturn = (m.getType() != null && !m.getType().equals(Types.VOID_TYPE));
            if (m.getBody() != null && needReturn && !returned) {
                this.diagnosisReporter.report(Diagnosis.Kind.ERROR, "Missing return statement in method:" + MethodUtil.toString(method), classNodeMetaBuilder.getMethodDeclContext(m));
            }
            new InitializationAnalyzer(compilationUnit, astLoader).check(clazz, m);
        }
        if (AstUtil.isConstructor(m)) {
            @SuppressWarnings("null") List<Statement> bodyStmts = mbody.statements;
            if (!AstUtil.hasConstructorCallStatement(bodyStmts)) {
                try {
                    bodyStmts.add(0, AstUtil.createDefaultSuperConstructorCall(thisClazz));
                } catch (MethodNotFoundException | AmbiguousMethodException ex) {
                    diagnosisReporter.report(Diagnosis.Kind.ERROR, "default constructor not found", classNodeMetaBuilder.getMethodDeclContext(m));
                }
            }
            // check super()
            int stmtsSize = mbody.statements.size();
            assert stmtsSize > 0;
            Statement firstStmt = mbody.statements.get(0);
            if (!AstUtil.isConstructorCallStatement(firstStmt)) {
                // TODO handle error
                throw new RuntimeException("missing constructor call");
            }
            mbody.statements.addAll(1, this.thisClazz.initStmts);
        }
    }
    for (ClassNode c : clazz.classes) {
        this.visitMethods(c);
    }
}
Also used : ClassNode(kalang.ast.ClassNode) Statement(kalang.ast.Statement) BlockStmt(kalang.ast.BlockStmt) IfStatContext(kalang.antlr.KalangParser.IfStatContext) ContinueStatContext(kalang.antlr.KalangParser.ContinueStatContext) ExprStatContext(kalang.antlr.KalangParser.ExprStatContext) WhileStatContext(kalang.antlr.KalangParser.WhileStatContext) BreakStatContext(kalang.antlr.KalangParser.BreakStatContext) StatContext(kalang.antlr.KalangParser.StatContext) TryStatContext(kalang.antlr.KalangParser.TryStatContext) ForStatContext(kalang.antlr.KalangParser.ForStatContext) ReturnStatContext(kalang.antlr.KalangParser.ReturnStatContext) VarDeclStatContext(kalang.antlr.KalangParser.VarDeclStatContext) DoWhileStatContext(kalang.antlr.KalangParser.DoWhileStatContext) MethodNode(kalang.ast.MethodNode) MethodNotFoundException(kalang.MethodNotFoundException) AmbiguousMethodException(kalang.AmbiguousMethodException)

Example 7 with Statement

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

the class InitializationAnalyzer method visitTryStmt.

@Override
public Type visitTryStmt(TryStmt node) {
    List<VarTable<LocalVarNode, Void>> assignedList = new ArrayList(node.getCatchStmts().size() + 1);
    enterNewFrame();
    assignedList.add(assignedVars);
    visit(node.getExecStmt());
    exitFrame();
    for (CatchBlock cs : node.getCatchStmts()) {
        enterNewFrame();
        assignedList.add(assignedVars);
        visit(cs);
        exitFrame();
    }
    addIntersectedAssignedVar(assignedList.toArray(new VarTable[assignedList.size()]));
    Statement finallyStmt = node.getFinallyStmt();
    if (finallyStmt != null) {
        visit(finallyStmt);
    }
    return null;
}
Also used : VarTable(kalang.core.VarTable) CatchBlock(kalang.ast.CatchBlock) Statement(kalang.ast.Statement) ArrayList(java.util.ArrayList)

Example 8 with Statement

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

the class Ast2Class method visitIfStmt.

@Override
public Object visitIfStmt(IfStmt node) {
    Label stopLabel = new Label();
    Label falseLabel = new Label();
    ExprNode condition = node.getConditionExpr();
    Statement trueBody = node.getTrueBody();
    Statement falseBody = node.getFalseBody();
    ifExpr(false, condition, falseLabel);
    if (trueBody != null) {
        visit(trueBody);
    }
    if (falseBody == null) {
        md.visitLabel(falseLabel);
    } else {
        md.visitJumpInsn(GOTO, stopLabel);
        md.visitLabel(falseLabel);
        visit(falseBody);
    }
    md.visitLabel(stopLabel);
    return null;
}
Also used : ExprNode(kalang.ast.ExprNode) Statement(kalang.ast.Statement) Label(org.objectweb.asm.Label)

Example 9 with Statement

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

the class AstBuilder method visitVarDeclStat.

@Override
public Statement visitVarDeclStat(VarDeclStatContext ctx) {
    Statement vars = visitLocalVarDecl(ctx.localVarDecl());
    mapAst(vars, ctx);
    return vars;
}
Also used : Statement(kalang.ast.Statement)

Example 10 with Statement

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

the class AstBuilder method visitQuestionExpr.

@Override
public AstNode visitQuestionExpr(KalangParser.QuestionExprContext ctx) {
    List<Statement> stmts = new LinkedList<>();
    ExprNode conditionExpr = (ExprNode) visit(ctx.expression(0));
    ExprNode trueExpr = (ExprNode) visit(ctx.expression(1));
    ExprNode falseExpr = (ExprNode) visit(ctx.expression(2));
    Type trueType = trueExpr.getType();
    Type falseType = falseExpr.getType();
    Type type;
    if (trueType.equals(falseType)) {
        type = trueType;
    } else {
        type = TypeUtil.getCommonType(trueType, falseType);
    }
    LocalVarNode vo = this.declareTempLocalVar(type);
    VarDeclStmt vds = new VarDeclStmt(vo);
    stmts.add(vds);
    VarExpr ve = new VarExpr(vo);
    IfStmt is = new IfStmt(conditionExpr);
    is.getTrueBody().statements.add(new ExprStmt(new AssignExpr(ve, trueExpr)));
    is.getFalseBody().statements.add(new ExprStmt(new AssignExpr(ve, falseExpr)));
    stmts.add(is);
    MultiStmtExpr mse = new MultiStmtExpr(stmts, ve);
    mapAst(ve, ctx);
    return mse;
}
Also used : ExprNode(kalang.ast.ExprNode) MultiStmtExpr(kalang.ast.MultiStmtExpr) WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType) Type(kalang.core.Type) GenericType(kalang.core.GenericType) ObjectType(kalang.core.ObjectType) IfStmt(kalang.ast.IfStmt) ExprStmt(kalang.ast.ExprStmt) Statement(kalang.ast.Statement) VarDeclStmt(kalang.ast.VarDeclStmt) VarExpr(kalang.ast.VarExpr) LocalVarNode(kalang.ast.LocalVarNode) LinkedList(java.util.LinkedList) AssignExpr(kalang.ast.AssignExpr)

Aggregations

Statement (kalang.ast.Statement)11 ExprNode (kalang.ast.ExprNode)7 ExprStmt (kalang.ast.ExprStmt)5 LinkedList (java.util.LinkedList)4 AssignExpr (kalang.ast.AssignExpr)4 LocalVarNode (kalang.ast.LocalVarNode)4 MultiStmtExpr (kalang.ast.MultiStmtExpr)4 VarDeclStmt (kalang.ast.VarDeclStmt)4 VarExpr (kalang.ast.VarExpr)4 BlockStmt (kalang.ast.BlockStmt)3 ConstExpr (kalang.ast.ConstExpr)3 ArrayType (kalang.core.ArrayType)3 ClassType (kalang.core.ClassType)3 GenericType (kalang.core.GenericType)3 ObjectType (kalang.core.ObjectType)3 PrimitiveType (kalang.core.PrimitiveType)3 Type (kalang.core.Type)3 WildcardType (kalang.core.WildcardType)3 AmbiguousMethodException (kalang.AmbiguousMethodException)2 MethodNotFoundException (kalang.MethodNotFoundException)2