package com.hpctoday.fada.c2fada;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
import org.eclipse.cdt.core.dom.ast.IASTForStatement;
import org.eclipse.cdt.core.dom.ast.IASTIfStatement;
import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTWhileStatement;
import com.hpctoday.fada.Assignment;
import com.hpctoday.fada.Expression;
import com.hpctoday.fada.Statement;
/*
* Entry point to C2FADA Translator
*/
public class FADAVisitor extends ASTVisitor {
private Statement AST;
public FADAVisitor() {
AST = new Statement();
this.shouldVisitStatements=true;
}
public Statement getFadaStatement(){
return AST;
}
@Override
public int visit(IASTStatement stmt) {
if (stmt instanceof IASTForStatement || stmt instanceof IASTWhileStatement || stmt instanceof IASTIfStatement) {
ControlVisitor controlVisitor = new ControlVisitor(AST);
stmt.accept(controlVisitor);
return PROCESS_SKIP;
} else if(stmt instanceof IASTExpressionStatement){
StmtVisitor stmtVisitor = new StmtVisitor(AST, stmt, false);
stmt.accept(stmtVisitor);
return PROCESS_SKIP;
//TODO: process declaration statements??
} else if(stmt instanceof IASTDeclarationStatement){
//Ignore compound statement, visitor will visit individual statements
} else if(stmt instanceof IASTCompoundStatement){
} else if(stmt instanceof IASTReturnStatement){
//TODO: Construct fake statement with read dependency
IASTReturnStatement ret = (IASTReturnStatement)stmt;
IASTExpression exp = ret.getReturnValue();
if(exp != null){
//System.err.println("FadaVisitor return expression not handled: " + stmt.getRawSignature());
ExprVisitor visitor = new ExprVisitor();
exp.accept(visitor);
List<Expression> arguments = new ArrayList<Expression>();
arguments.add(visitor.getExpr());
Expression retExp = new Expression(Expression.Leaf.FADA_function, "return", arguments);
Statement retStmt = new Statement(new Assignment ("", retExp));
AST.Enclose(retStmt, false);
}
} else {
System.err.println("FadaVisitor Unknown Statement: [" + stmt.getClass() + "] " + stmt.getRawSignature());
}
return PROCESS_CONTINUE;
}
}