00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 #include "StmtDumper.h"
00010 #include "comma/ast/Decl.h"
00011 #include "comma/ast/DSTDefinition.h"
00012 #include "comma/ast/Expr.h"
00013 #include "comma/ast/Stmt.h"
00014 
00015 #include "llvm/Support/Format.h"
00016 
00017 using namespace comma;
00018 
00019 using llvm::dyn_cast;
00020 using llvm::cast;
00021 using llvm::isa;
00022 
00023 
00024 llvm::raw_ostream &StmtDumper::dump(Stmt *stmt, unsigned level)
00025 {
00026     unsigned savedLevel = indentLevel;
00027     indentLevel = level;
00028     visitStmt(stmt);
00029     indentLevel = savedLevel;
00030     return S;
00031 }
00032 
00033 llvm::raw_ostream &StmtDumper::dumpAST(Ast *node)
00034 {
00035     return dumper->dump(node, indentLevel);
00036 }
00037 
00038 void StmtDumper::visitStmtSequence(StmtSequence *node)
00039 {
00040     printHeader(node);
00041     indent();
00042     for (StmtSequence::stmt_iter I = node->stmt_begin();
00043          I != node->stmt_end(); ++I) {
00044         S << '\n';
00045         printIndentation();
00046         visitStmt(*I);
00047     }
00048     dedent();
00049     S << '>';
00050 }
00051 
00052 void StmtDumper::visitBlockStmt(BlockStmt *node)
00053 {
00054     printHeader(node);
00055 
00056     if (node->hasLabel())
00057         S << llvm::format(" '%s'", node->getLabel()->getString());
00058 
00059     if (node->countDecls()) {
00060         indent();
00061         S << '\n';
00062         printIndentation();
00063         S << ":Declare";
00064         indent();
00065         for (BlockStmt::DeclIter I = node->beginDecls();
00066              I != node->endDecls(); ++I) {
00067             S << '\n';
00068             printIndentation();
00069             dumpAST(*I);
00070         }
00071         dedent();
00072         dedent();
00073     }
00074 
00075     if (node->numStatements()) {
00076         indent();
00077         S << '\n';
00078         printIndentation();
00079         S << ":Body";
00080         indent();
00081         for (StmtSequence::stmt_iter I = node->stmt_begin();
00082              I != node->stmt_end(); ++I) {
00083             S << '\n';
00084             printIndentation();
00085             visitStmt(*I);
00086         }
00087         dedent();
00088         dedent();
00089     }
00090     S << '>';
00091 }
00092 
00093 void StmtDumper::visitProcedureCallStmt(ProcedureCallStmt *node)
00094 {
00095     printHeader(node)
00096         << llvm::format(" '%s'>", node->getConnective()->getString());
00097 }
00098 
00099 void StmtDumper::visitReturnStmt(ReturnStmt *node)
00100 {
00101     printHeader(node);
00102     if (node->hasReturnExpr()) {
00103         S << '\n';
00104         indent();
00105         printIndentation();
00106         dumpAST(node->getReturnExpr());
00107         dedent();
00108     }
00109     S << '>';
00110 }
00111 
00112 void StmtDumper::visitAssignmentStmt(AssignmentStmt *node)
00113 {
00114     printHeader(node) << '\n';
00115     indent();
00116     printIndentation();
00117     dumpAST(node->getTarget()) << '\n';
00118     printIndentation();
00119     dumpAST(node->getAssignedExpr());
00120     dedent();
00121     S << '>';
00122 }
00123 
00124 void StmtDumper::visitIfStmt(IfStmt *node)
00125 {
00126     printHeader(node) << '\n';
00127     indent();
00128     printIndentation();
00129     dumpAST(node->getCondition()) << '\n';
00130     printIndentation();
00131     visitStmtSequence(node->getConsequent());
00132 
00133     for (IfStmt::iterator I = node->beginElsif();
00134          I != node->endElsif(); ++I) {
00135         // For each elsif, print a condition and consequent group.
00136         S << '\n';
00137         printIndentation() << "<elsif\n";
00138         indent();
00139         printIndentation();
00140         dumpAST(I->getCondition()) << '\n';
00141         printIndentation();
00142         visitStmtSequence(I->getConsequent());
00143         dedent();
00144         S << '>';
00145     }
00146 
00147     if (node->hasAlternate()) {
00148         S << '\n';
00149         printIndentation() << "<else\n";
00150         indent();
00151         printIndentation();
00152         visitStmtSequence(node->getAlternate());
00153         dedent();
00154         S << '>';
00155     }
00156 
00157     dedent();
00158     S << '>';
00159 }
00160 
00161 void StmtDumper::visitWhileStmt(WhileStmt *node)
00162 {
00163     printHeader(node) << '\n';
00164     indent();
00165     printIndentation();
00166     dumpAST(node->getCondition()) << '\n';
00167     printIndentation();
00168     visitStmtSequence(node->getBody());
00169     dedent();
00170     S << '>';
00171 }
00172 
00173 void StmtDumper::visitLoopStmt(LoopStmt *node)
00174 {
00175     printHeader(node) << '\n';
00176     indent();
00177     printIndentation();
00178     visitStmtSequence(node->getBody());
00179     dedent();
00180     S << '>';
00181 }
00182 
00183 void StmtDumper::visitForStmt(ForStmt *node)
00184 {
00185     printHeader(node) << ' ';
00186     dumpAST(node->getLoopDecl()) << '\n';
00187     indent();
00188     printIndentation();
00189     dumpAST(node->getControl()) << '\n';
00190     printIndentation();
00191     visitStmtSequence(node->getBody());
00192     dedent();
00193     S << '>';
00194 }
00195 
00196 void StmtDumper::visitPragmaStmt(PragmaStmt *node)
00197 {
00198     printHeader(node) << '>';
00199 }
00200 
00201 void StmtDumper::visitNullStmt(NullStmt *node)
00202 {
00203     printHeader(node) << '>';
00204 }
00205