ltd-graph-builder/src/main/java/grafos/Visitador.java

181 lines
5 KiB
Java
Raw Normal View History

2019-03-26 20:11:14 +01:00
package grafos;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
2019-03-26 20:11:14 +01:00
import com.github.javaparser.ast.body.MethodDeclaration;
2019-03-27 01:57:03 +01:00
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.stmt.*;
2019-03-26 20:11:14 +01:00
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
2019-03-26 23:26:01 +01:00
import java.util.LinkedList;
import java.util.List;
2019-03-26 22:25:52 +01:00
public class Visitador extends VoidVisitorAdapter<CFG> {
@Override
2019-03-26 22:25:52 +01:00
public void visit(MethodDeclaration methodDeclaration, CFG graph) {
2019-04-04 00:03:12 +02:00
graph.beginMethod();
2019-03-26 22:25:52 +01:00
super.visit(methodDeclaration, graph);
2019-04-04 00:03:12 +02:00
graph.finishMethod();
2019-03-26 20:11:14 +01:00
}
2019-03-27 11:55:19 +01:00
@Override
public void visit(ReturnStmt n, CFG arg) {
2019-04-04 00:03:12 +02:00
arg.connectTo(n);
arg.connectTo(arg.getEndNode());
arg.clearNode();
2019-03-27 11:55:19 +01:00
}
2019-03-26 23:26:01 +01:00
@Override
public void visit(IfStmt n, CFG graph) {
2019-04-04 00:03:12 +02:00
// Connect to the condition
2019-03-26 23:26:01 +01:00
Node ifStart = n.getCondition();
graph.beginBlock(n, ifStart);
2019-04-04 00:03:12 +02:00
graph.connectTo(ifStart);
// Connect condition to then block
graph.setNextLabel(true);
2019-03-26 23:26:01 +01:00
n.getThenStmt().accept(this, graph);
2019-04-04 00:03:12 +02:00
// Save end of then block
List<Node> newPrev = new LinkedList<>(graph.getNodeList());
// Connect condition to else block
graph.setNode(ifStart);
graph.setNextLabel(false);
2019-03-26 23:26:01 +01:00
if (n.getElseStmt().isPresent()) {
n.getElseStmt().get().accept(this, graph);
2019-04-04 00:03:12 +02:00
newPrev.addAll(graph.getNodeList());
2019-03-26 23:26:01 +01:00
} else {
newPrev.add(ifStart);
}
2019-04-04 00:03:12 +02:00
// Set ends of then/else as the new list
graph.setNodeList(newPrev);
graph.endBlock();
2019-03-26 23:26:01 +01:00
}
@Override
public void visit(LabeledStmt n, CFG graph) {
graph.beginBreakSection(n.getLabel());
super.visit(n, graph);
graph.endBreakSection(n.getLabel());
}
@Override
public void visit(WhileStmt n, CFG graph) {
Node whileStart = n.getCondition();
graph.beginBlock(n, whileStart);
graph.beginBreakSection();
2019-04-04 00:03:12 +02:00
graph.connectTo(whileStart);
graph.setNextLabel(true);
n.getBody().accept(this, graph);
2019-04-04 00:03:12 +02:00
graph.connectTo(whileStart);
graph.setNextLabel(false);
graph.endBreakSection();
graph.endBlock();
}
2019-03-27 00:41:26 +01:00
@Override
public void visit(DoStmt n, CFG graph) {
Node condition = n.getCondition();
2019-04-04 00:03:12 +02:00
graph.beginBlock(n, condition);
graph.beginBreakSection();
2019-04-04 00:03:12 +02:00
graph.appendNode(condition);
2019-03-27 00:41:26 +01:00
n.getBody().accept(this, graph);
2019-04-04 00:03:12 +02:00
graph.connectTo(condition);
graph.setNextLabel(false);
graph.endBreakSection();
graph.endBlock();
2019-03-27 00:41:26 +01:00
}
2019-03-27 01:57:03 +01:00
@Override
public void visit(ForStmt n, CFG graph) {
graph.beginBlock(n, n.getCompare().orElse(null));
graph.beginBreakSection();
2019-03-27 01:57:03 +01:00
// Initialization expressions
if (n.getInitialization() != null) {
for (Expression e : n.getInitialization()) {
2019-04-04 00:03:12 +02:00
graph.connectTo(e);
2019-03-27 01:57:03 +01:00
}
}
// Comparison expression
// TODO: shortcut conditions (||, &&)
if (n.getCompare().isPresent()) {
2019-04-04 00:03:12 +02:00
graph.connectTo(n.getCompare().get());
graph.setNextLabel(true);
2019-03-27 01:57:03 +01:00
}
// Loop body
n.getBody().accept(this, graph);
// Update expressions
2019-04-04 00:03:12 +02:00
if (n.getUpdate() != null)
for (Expression e : n.getUpdate())
graph.connectTo(e);
2019-03-27 01:57:03 +01:00
// Connect to comparison expression
// Set comparison as last possible statement
if (n.getCompare().isPresent()) {
2019-04-04 00:03:12 +02:00
graph.connectTo(n.getCompare().get());
graph.setNextLabel(false);
2019-04-04 00:03:12 +02:00
} else {
// TODO: connect last update / end of loop with first element of loop body
// There is no comparison, can't exit the loop TODO implement break and continue as way to exit
graph.clearNode();
}
graph.endBreakSection();
graph.endBlock();
2019-03-27 01:57:03 +01:00
}
@Override
public void visit(ForeachStmt n, CFG graph) {
ForeachStmt copy = new ForeachStmt(n.getTokenRange().orElse(null), n.getVariable(), n.getIterable(), new EmptyStmt());
graph.beginBlock(n, copy);
graph.beginBreakSection();
2019-04-04 00:03:12 +02:00
graph.connectTo(copy);
graph.setNextLabel(true);
n.getBody().accept(this, graph);
2019-04-04 00:03:12 +02:00
graph.connectTo(copy);
graph.setNextLabel(false);
graph.endBreakSection();
graph.endBlock();
}
@Override
public void visit(SwitchEntryStmt n, CFG graph) {
// Case header
Node caseNode = new SwitchEntryStmt(n.getTokenRange().orElse(null), n.getLabel().orElse(null), new NodeList<>());
2019-04-04 00:03:12 +02:00
graph.connectTo(caseNode);
// Case body
n.getStatements().accept(this, graph);
}
@Override
public void visit(SwitchStmt n, CFG graph) {
// Link previous statement to the switch's selector
Node selectorNode = n.getSelector();
graph.beginBlock(n, selectorNode);
graph.beginBreakSection();
2019-04-04 00:03:12 +02:00
graph.connectTo(selectorNode);
// Analyze switch's cases
for (SwitchEntryStmt entry : n.getEntries()) {
entry.accept(this, graph);
2019-04-04 00:03:12 +02:00
graph.appendNode(selectorNode);
}
// The next statement will be linked to:
// 1. All break statements that broke from the switch
// 2. If the switch doesn't have a default statement, the switch's selector
// 3. If the last entry doesn't break, to the last statement
2019-04-04 00:03:12 +02:00
graph.removeNode(selectorNode);
if (n.getEntries().get(n.getEntries().size() - 1).getLabel().isPresent())
graph.appendNode(selectorNode);
graph.endBreakSection();
graph.endBlock();
}
@Override
public void visit(BreakStmt n, CFG graph) {
graph.connectTo(n, false);
graph.clearNode();
graph.registerBreak(n);
}
2019-03-26 20:11:14 +01:00
@Override
2019-03-26 22:25:52 +01:00
public void visit(ExpressionStmt es, CFG graph) {
2019-04-04 00:03:12 +02:00
graph.connectTo(es);
2019-03-26 20:11:14 +01:00
}
}