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);
}
}
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;
}
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;
}
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;
}
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;
}
Aggregations