2006-11-04 07:16:25 +00:00
|
|
|
//===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Chris Lattner and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Stmt::dump/Stmt::print methods.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2006-11-20 05:01:40 +00:00
|
|
|
#include "clang/AST/Decl.h"
|
2006-12-04 18:06:35 +00:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
2006-11-05 00:19:50 +00:00
|
|
|
#include "clang/Lex/IdentifierTable.h"
|
2006-11-04 07:16:25 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include <iostream>
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
2006-11-04 18:52:07 +00:00
|
|
|
namespace {
|
|
|
|
struct VISIBILITY_HIDDEN IsExprStmtVisitor : public StmtVisitor {
|
|
|
|
bool &Result;
|
|
|
|
IsExprStmtVisitor(bool &R) : Result(R) { Result = false; }
|
|
|
|
|
|
|
|
virtual void VisitExpr(Expr *Node) {
|
|
|
|
Result = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isExpr(Stmt *S) {
|
|
|
|
bool Val = false;
|
|
|
|
IsExprStmtVisitor V(Val);
|
|
|
|
S->visit(V);
|
|
|
|
return Val;
|
|
|
|
}
|
|
|
|
|
2006-11-04 07:16:25 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// StmtPrinter Visitor
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor {
|
|
|
|
std::ostream &OS;
|
|
|
|
unsigned IndentLevel;
|
|
|
|
public:
|
|
|
|
StmtPrinter(std::ostream &os) : OS(os), IndentLevel(0) {}
|
|
|
|
|
2007-05-20 22:52:15 +00:00
|
|
|
void PrintStmt(Stmt *S, int SubIndent = 1) {
|
|
|
|
IndentLevel += SubIndent;
|
2006-11-04 18:52:07 +00:00
|
|
|
if (S && isExpr(S)) {
|
|
|
|
// If this is an expr used in a stmt context, indent and newline it.
|
|
|
|
Indent();
|
|
|
|
S->visit(*this);
|
|
|
|
OS << "\n";
|
|
|
|
} else if (S) {
|
2006-11-04 07:16:25 +00:00
|
|
|
S->visit(*this);
|
2006-11-04 18:52:07 +00:00
|
|
|
} else {
|
2006-11-04 20:40:44 +00:00
|
|
|
Indent() << ";\n";
|
2006-11-04 18:52:07 +00:00
|
|
|
}
|
2007-05-20 22:52:15 +00:00
|
|
|
IndentLevel -= SubIndent;
|
2006-11-04 18:52:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintExpr(Expr *E) {
|
|
|
|
if (E)
|
|
|
|
E->visit(*this);
|
2006-11-04 07:16:25 +00:00
|
|
|
else
|
2006-11-04 18:52:07 +00:00
|
|
|
OS << "<null expr>";
|
2006-11-04 07:16:25 +00:00
|
|
|
}
|
|
|
|
|
2007-05-20 22:52:15 +00:00
|
|
|
std::ostream &Indent(int Delta = 0) const {
|
|
|
|
for (unsigned i = 0, e = IndentLevel+Delta; i != e; ++i)
|
2006-11-04 07:16:25 +00:00
|
|
|
OS << " ";
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2006-11-04 18:52:07 +00:00
|
|
|
virtual void VisitStmt(Stmt *Node);
|
2007-02-27 02:53:10 +00:00
|
|
|
#define STMT(N, CLASS, PARENT) \
|
2006-11-04 20:59:27 +00:00
|
|
|
virtual void Visit##CLASS(CLASS *Node);
|
|
|
|
#include "clang/AST/StmtNodes.def"
|
2006-11-04 20:18:38 +00:00
|
|
|
};
|
2006-11-04 18:52:07 +00:00
|
|
|
}
|
|
|
|
|
2006-11-04 20:18:38 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Stmt printing methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-11-04 18:52:07 +00:00
|
|
|
void StmtPrinter::VisitStmt(Stmt *Node) {
|
|
|
|
Indent() << "<<unknown stmt type>>\n";
|
2006-11-04 07:16:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
|
|
|
|
Indent() << "{\n";
|
|
|
|
|
|
|
|
for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
|
2006-11-04 18:52:07 +00:00
|
|
|
I != E; ++I)
|
|
|
|
PrintStmt(*I);
|
2006-11-04 07:16:25 +00:00
|
|
|
|
|
|
|
Indent() << "}\n";
|
|
|
|
}
|
|
|
|
|
2006-11-05 00:19:50 +00:00
|
|
|
void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
|
2007-05-20 22:52:15 +00:00
|
|
|
Indent(-1) << "case ";
|
2006-11-05 00:19:50 +00:00
|
|
|
PrintExpr(Node->getLHS());
|
|
|
|
if (Node->getRHS()) {
|
|
|
|
OS << " ... ";
|
|
|
|
PrintExpr(Node->getRHS());
|
|
|
|
}
|
|
|
|
OS << ":\n";
|
|
|
|
|
2007-05-20 22:52:15 +00:00
|
|
|
PrintStmt(Node->getSubStmt(), 0);
|
2006-11-05 00:19:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
|
2007-05-20 22:52:15 +00:00
|
|
|
Indent(-1) << "default:\n";
|
|
|
|
PrintStmt(Node->getSubStmt(), 0);
|
2006-11-05 00:19:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
|
2007-05-20 22:52:15 +00:00
|
|
|
Indent(-1) << Node->getLabel()->getName() << ":\n";
|
|
|
|
PrintStmt(Node->getSubStmt(), 0);
|
2006-11-05 00:19:50 +00:00
|
|
|
}
|
|
|
|
|
2006-11-04 07:16:25 +00:00
|
|
|
void StmtPrinter::VisitIfStmt(IfStmt *If) {
|
2006-11-04 20:40:44 +00:00
|
|
|
Indent() << "if (";
|
2006-11-04 18:52:07 +00:00
|
|
|
PrintExpr(If->getCond());
|
2006-11-04 07:16:25 +00:00
|
|
|
|
2006-11-04 20:40:44 +00:00
|
|
|
OS << ")\n";
|
2006-11-04 18:52:07 +00:00
|
|
|
PrintStmt(If->getThen());
|
|
|
|
if (If->getElse()) {
|
|
|
|
Indent() << "else\n";
|
|
|
|
PrintStmt(If->getElse());
|
|
|
|
}
|
2006-11-04 20:40:44 +00:00
|
|
|
}
|
|
|
|
|
2006-11-04 20:59:27 +00:00
|
|
|
void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
|
|
|
|
Indent() << "switch (";
|
|
|
|
PrintExpr(Node->getCond());
|
|
|
|
OS << ")\n";
|
|
|
|
PrintStmt(Node->getBody());
|
|
|
|
}
|
|
|
|
|
2006-11-04 20:40:44 +00:00
|
|
|
void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
|
|
|
|
Indent() << "while (";
|
|
|
|
PrintExpr(Node->getCond());
|
|
|
|
OS << ")\n";
|
|
|
|
PrintStmt(Node->getBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
void StmtPrinter::VisitDoStmt(DoStmt *Node) {
|
|
|
|
Indent() << "do\n";
|
|
|
|
PrintStmt(Node->getBody());
|
|
|
|
Indent() << "while (";
|
|
|
|
PrintExpr(Node->getCond());
|
|
|
|
OS << ")\n";
|
2006-11-04 07:16:25 +00:00
|
|
|
}
|
|
|
|
|
2006-11-04 20:18:38 +00:00
|
|
|
void StmtPrinter::VisitForStmt(ForStmt *Node) {
|
|
|
|
Indent() << "for (";
|
|
|
|
if (Node->getFirst())
|
|
|
|
PrintExpr((Expr*)Node->getFirst());
|
|
|
|
OS << "; ";
|
|
|
|
if (Node->getSecond())
|
|
|
|
PrintExpr(Node->getSecond());
|
|
|
|
OS << "; ";
|
|
|
|
if (Node->getThird())
|
|
|
|
PrintExpr(Node->getThird());
|
|
|
|
OS << ")\n";
|
2006-11-04 20:40:44 +00:00
|
|
|
PrintStmt(Node->getBody());
|
2006-11-04 20:18:38 +00:00
|
|
|
}
|
|
|
|
|
2006-11-05 01:46:01 +00:00
|
|
|
void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
|
|
|
|
Indent() << "goto " << Node->getLabel()->getName() << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
|
2006-11-05 01:51:06 +00:00
|
|
|
Indent() << "goto *";
|
2006-11-05 01:46:01 +00:00
|
|
|
PrintExpr(Node->getTarget());
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
|
|
|
|
Indent() << "continue\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
|
|
|
|
Indent() << "break\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-04 18:52:07 +00:00
|
|
|
void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
|
|
|
|
Indent() << "return";
|
|
|
|
if (Node->getRetValue()) {
|
|
|
|
OS << " ";
|
|
|
|
PrintExpr(Node->getRetValue());
|
|
|
|
}
|
|
|
|
OS << "\n";
|
|
|
|
}
|
2006-11-04 07:16:25 +00:00
|
|
|
|
2006-11-04 20:18:38 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Expr printing methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2006-11-04 07:16:25 +00:00
|
|
|
|
2006-11-04 18:52:07 +00:00
|
|
|
void StmtPrinter::VisitExpr(Expr *Node) {
|
|
|
|
OS << "<<unknown expr type>>";
|
2006-11-04 07:16:25 +00:00
|
|
|
}
|
|
|
|
|
2006-11-04 18:52:07 +00:00
|
|
|
void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
|
2006-11-20 05:01:40 +00:00
|
|
|
OS << Node->getDecl()->getName();
|
2006-11-04 07:16:25 +00:00
|
|
|
}
|
|
|
|
|
2007-04-26 20:39:23 +00:00
|
|
|
void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
|
|
|
|
// FIXME: print value.
|
|
|
|
OS << "x";
|
|
|
|
}
|
|
|
|
|
2007-02-21 23:46:25 +00:00
|
|
|
void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
|
2006-11-04 18:52:07 +00:00
|
|
|
// FIXME: print value.
|
|
|
|
OS << "1";
|
2006-11-04 07:16:25 +00:00
|
|
|
}
|
2007-02-21 22:05:47 +00:00
|
|
|
void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
|
2006-11-04 18:52:07 +00:00
|
|
|
// FIXME: print value.
|
|
|
|
OS << "1.0";
|
2006-11-04 07:16:25 +00:00
|
|
|
}
|
2007-02-21 23:46:25 +00:00
|
|
|
void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
|
2006-11-04 18:52:07 +00:00
|
|
|
if (Str->isWide()) OS << 'L';
|
2006-11-04 20:29:31 +00:00
|
|
|
OS << '"';
|
|
|
|
|
|
|
|
// FIXME: this doesn't print wstrings right.
|
|
|
|
for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
|
|
|
|
switch (Str->getStrData()[i]) {
|
|
|
|
default: OS << Str->getStrData()[i]; break;
|
|
|
|
// Handle some common ones to make dumps prettier.
|
|
|
|
case '\\': OS << "\\\\"; break;
|
|
|
|
case '"': OS << "\\\""; break;
|
|
|
|
case '\n': OS << "\\n"; break;
|
|
|
|
case '\t': OS << "\\t"; break;
|
|
|
|
case '\a': OS << "\\a"; break;
|
|
|
|
case '\b': OS << "\\b"; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OS << '"';
|
2006-11-04 07:16:25 +00:00
|
|
|
}
|
2006-11-04 18:52:07 +00:00
|
|
|
void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
|
|
|
|
OS << "(";
|
|
|
|
PrintExpr(Node->getSubExpr());
|
|
|
|
OS << ")'";
|
2006-11-04 07:16:25 +00:00
|
|
|
}
|
2006-11-04 18:52:07 +00:00
|
|
|
void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
|
2006-11-05 23:54:51 +00:00
|
|
|
if (!Node->isPostfix())
|
|
|
|
OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
|
2006-11-04 18:52:07 +00:00
|
|
|
PrintExpr(Node->getSubExpr());
|
2006-11-05 23:54:51 +00:00
|
|
|
|
|
|
|
if (Node->isPostfix())
|
|
|
|
OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
|
|
|
|
|
2006-11-04 07:16:25 +00:00
|
|
|
}
|
2006-11-04 18:52:07 +00:00
|
|
|
void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
|
2006-11-19 01:48:02 +00:00
|
|
|
OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
|
2007-05-16 18:07:12 +00:00
|
|
|
OS << Node->getArgumentType().getAsString() << ")";
|
2006-11-04 07:16:25 +00:00
|
|
|
}
|
2006-11-04 18:52:07 +00:00
|
|
|
void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
|
|
|
|
PrintExpr(Node->getBase());
|
|
|
|
OS << "[";
|
|
|
|
PrintExpr(Node->getIdx());
|
|
|
|
OS << "]";
|
2006-11-04 07:16:25 +00:00
|
|
|
}
|
|
|
|
|
2006-11-04 18:52:07 +00:00
|
|
|
void StmtPrinter::VisitCallExpr(CallExpr *Call) {
|
|
|
|
PrintExpr(Call->getCallee());
|
|
|
|
OS << "(";
|
|
|
|
for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
|
|
|
|
if (i) OS << ", ";
|
|
|
|
PrintExpr(Call->getArg(i));
|
2006-11-04 07:16:25 +00:00
|
|
|
}
|
2006-11-04 18:52:07 +00:00
|
|
|
OS << ")";
|
2006-11-04 07:16:25 +00:00
|
|
|
}
|
2006-11-04 18:52:07 +00:00
|
|
|
void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
|
|
|
|
PrintExpr(Node->getBase());
|
|
|
|
OS << (Node->isArrow() ? "->" : ".");
|
2006-11-04 07:16:25 +00:00
|
|
|
|
2006-11-04 18:52:07 +00:00
|
|
|
if (Node->getMemberDecl())
|
|
|
|
assert(0 && "TODO: should print member decl!");
|
|
|
|
OS << "member";
|
2006-11-04 07:16:25 +00:00
|
|
|
}
|
2006-11-04 18:52:07 +00:00
|
|
|
void StmtPrinter::VisitCastExpr(CastExpr *Node) {
|
2007-05-16 18:07:12 +00:00
|
|
|
OS << "(" << Node->getDestType().getAsString() << ")";
|
2006-11-04 18:52:07 +00:00
|
|
|
PrintExpr(Node->getSubExpr());
|
|
|
|
}
|
2006-12-04 18:06:35 +00:00
|
|
|
void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) {
|
|
|
|
switch (Node->getOpcode()) {
|
|
|
|
default:
|
|
|
|
assert(0 && "Not a C++ cast expression");
|
|
|
|
abort();
|
|
|
|
case CXXCastExpr::ConstCast: OS << "const_cast<"; break;
|
|
|
|
case CXXCastExpr::DynamicCast: OS << "dynamic_cast<"; break;
|
|
|
|
case CXXCastExpr::ReinterpretCast: OS << "reinterpret_cast<"; break;
|
|
|
|
case CXXCastExpr::StaticCast: OS << "static_cast<"; break;
|
|
|
|
}
|
|
|
|
|
2007-05-16 18:07:12 +00:00
|
|
|
OS << Node->getDestType().getAsString() << ">(";
|
2006-12-04 18:06:35 +00:00
|
|
|
PrintExpr(Node->getSubExpr());
|
|
|
|
OS << ")";
|
|
|
|
}
|
2007-02-27 02:53:10 +00:00
|
|
|
|
|
|
|
void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
|
|
|
|
assert(0 && "TODO: should print CXXBoolLiteralExpr!");
|
|
|
|
}
|
|
|
|
|
2006-11-04 18:52:07 +00:00
|
|
|
void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
|
|
|
|
PrintExpr(Node->getLHS());
|
|
|
|
OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
|
|
|
|
PrintExpr(Node->getRHS());
|
|
|
|
}
|
|
|
|
void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
|
|
|
|
PrintExpr(Node->getCond());
|
|
|
|
OS << " ? ";
|
|
|
|
PrintExpr(Node->getLHS());
|
2006-11-04 07:16:25 +00:00
|
|
|
std::cerr << " : ";
|
2006-11-04 18:52:07 +00:00
|
|
|
PrintExpr(Node->getRHS());
|
2006-11-04 07:16:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Stmt method implementations
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void Stmt::dump() const {
|
|
|
|
print(std::cerr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stmt::print(std::ostream &OS) const {
|
2006-11-04 18:52:07 +00:00
|
|
|
if (this == 0) {
|
|
|
|
OS << "<NULL>";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-11-04 07:16:25 +00:00
|
|
|
StmtPrinter P(OS);
|
|
|
|
const_cast<Stmt*>(this)->visit(P);
|
|
|
|
}
|