use of kalang.antlr.KalangParser.StatContext in project kalang by kasonyang.
the class AstBuilder method visit.
@Override
public Object visit(ParseTree tree) {
if (tree == null) {
System.err.print("visit null");
Exception ex = new Exception();
ex.printStackTrace(System.err);
return null;
}
if (tree instanceof StatContext && returned) {
diagnosisReporter.report(Diagnosis.Kind.ERROR, "unreachable statement", (StatContext) tree);
}
return super.visit(tree);
}
use of kalang.antlr.KalangParser.StatContext in project kalang by kasonyang.
the class AstBuilder method visitBlockStmt.
public void visitBlockStmt(StatContext[] stats, BlockStmt blockStmt) {
this.newFrame();
if (stats == null) {
return;
}
for (StatContext s : stats) {
blockStmt.statements.add(visitStat(s));
}
this.popFrame();
}
use of kalang.antlr.KalangParser.StatContext in project kalang by kasonyang.
the class AstBuilder method visitBlockStmt.
@Override
public AstNode visitBlockStmt(BlockStmtContext ctx) {
BlockStmt bs = newBlock();
List<StatContext> stats = ctx.stat();
if (stats != null) {
this.visitBlockStmt(stats.toArray(new StatContext[stats.size()]), bs);
}
mapAst(bs, ctx);
popBlock();
return bs;
}
use of kalang.antlr.KalangParser.StatContext 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);
}
}
Aggregations