2006-11-04 15:16:25 +08:00
|
|
|
//===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-11-04 15:16:25 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-08-09 06:51:59 +08:00
|
|
|
// This file implements the Stmt::dumpPretty/Stmt::printPretty methods, which
|
|
|
|
// pretty print the AST back out to C code.
|
2006-11-04 15:16:25 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2007-10-18 02:36:42 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2007-09-01 05:30:12 +08:00
|
|
|
#include "clang/AST/PrettyPrinter.h"
|
2006-11-04 15:16:25 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2007-11-27 06:50:46 +08:00
|
|
|
#include "llvm/Support/Streams.h"
|
2008-09-13 13:16:45 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2006-11-04 15:16:25 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// StmtPrinter Visitor
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2007-08-21 12:04:25 +08:00
|
|
|
class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor<StmtPrinter> {
|
2008-09-13 13:16:45 +08:00
|
|
|
llvm::raw_ostream &OS;
|
2006-11-04 15:16:25 +08:00
|
|
|
unsigned IndentLevel;
|
2007-09-01 05:30:12 +08:00
|
|
|
clang::PrinterHelper* Helper;
|
2006-11-04 15:16:25 +08:00
|
|
|
public:
|
2008-09-13 13:16:45 +08:00
|
|
|
StmtPrinter(llvm::raw_ostream &os, PrinterHelper* helper) :
|
2007-09-01 05:30:12 +08:00
|
|
|
OS(os), IndentLevel(0), Helper(helper) {}
|
2006-11-04 15:16:25 +08:00
|
|
|
|
2007-05-21 06:52:15 +08:00
|
|
|
void PrintStmt(Stmt *S, int SubIndent = 1) {
|
|
|
|
IndentLevel += SubIndent;
|
2007-06-01 02:21:33 +08:00
|
|
|
if (S && isa<Expr>(S)) {
|
2006-11-05 02:52:07 +08:00
|
|
|
// If this is an expr used in a stmt context, indent and newline it.
|
|
|
|
Indent();
|
2007-08-21 12:04:25 +08:00
|
|
|
Visit(S);
|
2007-05-31 01:57:36 +08:00
|
|
|
OS << ";\n";
|
2006-11-05 02:52:07 +08:00
|
|
|
} else if (S) {
|
2007-08-21 12:04:25 +08:00
|
|
|
Visit(S);
|
2006-11-05 02:52:07 +08:00
|
|
|
} else {
|
2007-05-28 09:47:47 +08:00
|
|
|
Indent() << "<<<NULL STATEMENT>>>\n";
|
2006-11-05 02:52:07 +08:00
|
|
|
}
|
2007-05-21 06:52:15 +08:00
|
|
|
IndentLevel -= SubIndent;
|
2006-11-05 02:52:07 +08:00
|
|
|
}
|
2007-05-21 07:04:55 +08:00
|
|
|
|
|
|
|
void PrintRawCompoundStmt(CompoundStmt *S);
|
2007-06-06 04:52:47 +08:00
|
|
|
void PrintRawDecl(Decl *D);
|
2008-10-07 02:39:36 +08:00
|
|
|
void PrintRawDeclStmt(DeclStmt *S);
|
Pretty print if/else/elseif chains nicer, like this:
void printutf8(unsigned int X) {
if (X <= 127)
printf("%c", (char)X);
else if (X <= 2047)
printf("%d %d ", 128 + 64 + (X >> 6), 128 + (X & ((1 << 6) - 1)));
else if (X <= 65535)
printf("%c%c%c", 128 + 64 + 32 + (X >> 12), 128 + ((X >> 6) & 63), 128 + (X & 63));
else
printf("UNKNOWN %d\n", X);
instead of:
if (X <= 127)
printf("%c", (char)X);
else
if (X <= 2047)
printf("%d %d ", 128 + 64 + (X >> 6), 128 + (X & ((1 << 6) - 1)));
else
if (X <= 65535)
printf("%c%c%c", 128 + 64 + 32 + (X >> 12), 128 + ((X >> 6) & 63), 128 + (X & 63));
else
printf("UNKNOWN %d\n", X);
llvm-svn: 39648
2007-06-12 06:26:23 +08:00
|
|
|
void PrintRawIfStmt(IfStmt *If);
|
|
|
|
|
2006-11-05 02:52:07 +08:00
|
|
|
void PrintExpr(Expr *E) {
|
|
|
|
if (E)
|
2007-08-21 12:04:25 +08:00
|
|
|
Visit(E);
|
2006-11-04 15:16:25 +08:00
|
|
|
else
|
2006-11-05 02:52:07 +08:00
|
|
|
OS << "<null expr>";
|
2006-11-04 15:16:25 +08:00
|
|
|
}
|
|
|
|
|
2008-09-13 13:16:45 +08:00
|
|
|
llvm::raw_ostream &Indent(int Delta = 0) const {
|
2007-05-30 07:49:07 +08:00
|
|
|
for (int i = 0, e = IndentLevel+Delta; i < e; ++i)
|
2006-11-04 15:16:25 +08:00
|
|
|
OS << " ";
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2007-08-31 01:59:59 +08:00
|
|
|
bool PrintOffsetOfDesignator(Expr *E);
|
|
|
|
void VisitUnaryOffsetOf(UnaryOperator *Node);
|
|
|
|
|
2007-09-01 05:30:12 +08:00
|
|
|
void Visit(Stmt* S) {
|
|
|
|
if (Helper && Helper->handledStmt(S,OS))
|
|
|
|
return;
|
|
|
|
else StmtVisitor<StmtPrinter>::Visit(S);
|
|
|
|
}
|
|
|
|
|
2007-08-21 12:04:25 +08:00
|
|
|
void VisitStmt(Stmt *Node);
|
2008-11-14 20:46:07 +08:00
|
|
|
#define STMT(CLASS, PARENT) \
|
2007-08-21 12:04:25 +08:00
|
|
|
void Visit##CLASS(CLASS *Node);
|
2006-11-05 04:59:27 +08:00
|
|
|
#include "clang/AST/StmtNodes.def"
|
2006-11-05 04:18:38 +08:00
|
|
|
};
|
2006-11-05 02:52:07 +08:00
|
|
|
}
|
|
|
|
|
2006-11-05 04:18:38 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Stmt printing methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-11-05 02:52:07 +08:00
|
|
|
void StmtPrinter::VisitStmt(Stmt *Node) {
|
|
|
|
Indent() << "<<unknown stmt type>>\n";
|
2006-11-04 15:16:25 +08:00
|
|
|
}
|
|
|
|
|
2007-05-21 07:04:55 +08:00
|
|
|
/// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and
|
|
|
|
/// with no newline after the }.
|
|
|
|
void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) {
|
|
|
|
OS << "{\n";
|
2006-11-04 15:16:25 +08:00
|
|
|
for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end();
|
2006-11-05 02:52:07 +08:00
|
|
|
I != E; ++I)
|
|
|
|
PrintStmt(*I);
|
2006-11-04 15:16:25 +08:00
|
|
|
|
2007-05-21 07:04:55 +08:00
|
|
|
Indent() << "}";
|
|
|
|
}
|
|
|
|
|
2007-06-06 04:52:47 +08:00
|
|
|
void StmtPrinter::PrintRawDecl(Decl *D) {
|
|
|
|
// FIXME: Need to complete/beautify this... this code simply shows the
|
2007-05-30 06:59:26 +08:00
|
|
|
// nodes are where they need to be.
|
2007-06-06 04:52:47 +08:00
|
|
|
if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) {
|
|
|
|
OS << "typedef " << localType->getUnderlyingType().getAsString();
|
|
|
|
OS << " " << localType->getName();
|
|
|
|
} else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
|
2007-06-02 13:27:01 +08:00
|
|
|
// Emit storage class for vardecls.
|
|
|
|
if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
|
|
|
|
switch (V->getStorageClass()) {
|
2007-06-06 04:52:47 +08:00
|
|
|
default: assert(0 && "Unknown storage class!");
|
|
|
|
case VarDecl::None: break;
|
|
|
|
case VarDecl::Extern: OS << "extern "; break;
|
|
|
|
case VarDecl::Static: OS << "static "; break;
|
|
|
|
case VarDecl::Auto: OS << "auto "; break;
|
|
|
|
case VarDecl::Register: OS << "register "; break;
|
2007-06-02 13:27:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-02 11:38:08 +08:00
|
|
|
std::string Name = VD->getName();
|
|
|
|
VD->getType().getAsStringInternal(Name);
|
2007-06-06 04:52:47 +08:00
|
|
|
OS << Name;
|
|
|
|
|
2007-07-12 08:36:32 +08:00
|
|
|
// If this is a vardecl with an initializer, emit it.
|
|
|
|
if (VarDecl *V = dyn_cast<VarDecl>(VD)) {
|
|
|
|
if (V->getInit()) {
|
|
|
|
OS << " = ";
|
|
|
|
PrintExpr(V->getInit());
|
|
|
|
}
|
|
|
|
}
|
2007-11-18 05:21:01 +08:00
|
|
|
} else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
|
|
|
|
// print a free standing tag decl (e.g. "struct x;").
|
|
|
|
OS << TD->getKindName();
|
|
|
|
OS << " ";
|
|
|
|
if (const IdentifierInfo *II = TD->getIdentifier())
|
|
|
|
OS << II->getName();
|
|
|
|
else
|
|
|
|
OS << "<anonymous>";
|
|
|
|
// FIXME: print tag bodies.
|
2007-06-06 04:52:47 +08:00
|
|
|
} else {
|
2007-06-02 11:38:08 +08:00
|
|
|
assert(0 && "Unexpected decl");
|
2007-06-06 04:52:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-07 02:39:36 +08:00
|
|
|
void StmtPrinter::PrintRawDeclStmt(DeclStmt *S) {
|
|
|
|
bool isFirst = false;
|
|
|
|
|
|
|
|
for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
|
|
|
|
if (!isFirst) OS << ", ";
|
|
|
|
else isFirst = false;
|
|
|
|
|
|
|
|
PrintRawDecl(*I);
|
|
|
|
}
|
|
|
|
}
|
2007-06-06 04:52:47 +08:00
|
|
|
|
|
|
|
void StmtPrinter::VisitNullStmt(NullStmt *Node) {
|
|
|
|
Indent() << ";\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void StmtPrinter::VisitDeclStmt(DeclStmt *Node) {
|
2008-10-07 02:39:36 +08:00
|
|
|
for (DeclStmt::decl_iterator I = Node->decl_begin(), E = Node->decl_end();
|
|
|
|
I!=E; ++I) {
|
2007-06-09 08:53:06 +08:00
|
|
|
Indent();
|
2008-10-07 02:39:36 +08:00
|
|
|
PrintRawDecl(*I);
|
2007-06-09 08:53:06 +08:00
|
|
|
OS << ";\n";
|
|
|
|
}
|
2007-05-30 06:59:26 +08:00
|
|
|
}
|
|
|
|
|
2007-05-21 07:04:55 +08:00
|
|
|
void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) {
|
|
|
|
Indent();
|
|
|
|
PrintRawCompoundStmt(Node);
|
2007-05-31 13:08:56 +08:00
|
|
|
OS << "\n";
|
2006-11-04 15:16:25 +08:00
|
|
|
}
|
|
|
|
|
2006-11-05 08:19:50 +08:00
|
|
|
void StmtPrinter::VisitCaseStmt(CaseStmt *Node) {
|
2007-05-21 06:52:15 +08:00
|
|
|
Indent(-1) << "case ";
|
2006-11-05 08:19:50 +08:00
|
|
|
PrintExpr(Node->getLHS());
|
|
|
|
if (Node->getRHS()) {
|
|
|
|
OS << " ... ";
|
|
|
|
PrintExpr(Node->getRHS());
|
|
|
|
}
|
|
|
|
OS << ":\n";
|
|
|
|
|
2007-05-21 06:52:15 +08:00
|
|
|
PrintStmt(Node->getSubStmt(), 0);
|
2006-11-05 08:19:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) {
|
2007-05-21 06:52:15 +08:00
|
|
|
Indent(-1) << "default:\n";
|
|
|
|
PrintStmt(Node->getSubStmt(), 0);
|
2006-11-05 08:19:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StmtPrinter::VisitLabelStmt(LabelStmt *Node) {
|
2007-05-28 14:56:27 +08:00
|
|
|
Indent(-1) << Node->getName() << ":\n";
|
2007-05-21 06:52:15 +08:00
|
|
|
PrintStmt(Node->getSubStmt(), 0);
|
2006-11-05 08:19:50 +08:00
|
|
|
}
|
|
|
|
|
Pretty print if/else/elseif chains nicer, like this:
void printutf8(unsigned int X) {
if (X <= 127)
printf("%c", (char)X);
else if (X <= 2047)
printf("%d %d ", 128 + 64 + (X >> 6), 128 + (X & ((1 << 6) - 1)));
else if (X <= 65535)
printf("%c%c%c", 128 + 64 + 32 + (X >> 12), 128 + ((X >> 6) & 63), 128 + (X & 63));
else
printf("UNKNOWN %d\n", X);
instead of:
if (X <= 127)
printf("%c", (char)X);
else
if (X <= 2047)
printf("%d %d ", 128 + 64 + (X >> 6), 128 + (X & ((1 << 6) - 1)));
else
if (X <= 65535)
printf("%c%c%c", 128 + 64 + 32 + (X >> 12), 128 + ((X >> 6) & 63), 128 + (X & 63));
else
printf("UNKNOWN %d\n", X);
llvm-svn: 39648
2007-06-12 06:26:23 +08:00
|
|
|
void StmtPrinter::PrintRawIfStmt(IfStmt *If) {
|
|
|
|
OS << "if ";
|
2006-11-05 02:52:07 +08:00
|
|
|
PrintExpr(If->getCond());
|
2007-05-21 07:04:55 +08:00
|
|
|
|
|
|
|
if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) {
|
|
|
|
OS << ' ';
|
|
|
|
PrintRawCompoundStmt(CS);
|
|
|
|
OS << (If->getElse() ? ' ' : '\n');
|
|
|
|
} else {
|
|
|
|
OS << '\n';
|
|
|
|
PrintStmt(If->getThen());
|
|
|
|
if (If->getElse()) Indent();
|
|
|
|
}
|
Pretty print if/else/elseif chains nicer, like this:
void printutf8(unsigned int X) {
if (X <= 127)
printf("%c", (char)X);
else if (X <= 2047)
printf("%d %d ", 128 + 64 + (X >> 6), 128 + (X & ((1 << 6) - 1)));
else if (X <= 65535)
printf("%c%c%c", 128 + 64 + 32 + (X >> 12), 128 + ((X >> 6) & 63), 128 + (X & 63));
else
printf("UNKNOWN %d\n", X);
instead of:
if (X <= 127)
printf("%c", (char)X);
else
if (X <= 2047)
printf("%d %d ", 128 + 64 + (X >> 6), 128 + (X & ((1 << 6) - 1)));
else
if (X <= 65535)
printf("%c%c%c", 128 + 64 + 32 + (X >> 12), 128 + ((X >> 6) & 63), 128 + (X & 63));
else
printf("UNKNOWN %d\n", X);
llvm-svn: 39648
2007-06-12 06:26:23 +08:00
|
|
|
|
2007-05-21 07:04:55 +08:00
|
|
|
if (Stmt *Else = If->getElse()) {
|
|
|
|
OS << "else";
|
|
|
|
|
|
|
|
if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) {
|
|
|
|
OS << ' ';
|
|
|
|
PrintRawCompoundStmt(CS);
|
|
|
|
OS << '\n';
|
Pretty print if/else/elseif chains nicer, like this:
void printutf8(unsigned int X) {
if (X <= 127)
printf("%c", (char)X);
else if (X <= 2047)
printf("%d %d ", 128 + 64 + (X >> 6), 128 + (X & ((1 << 6) - 1)));
else if (X <= 65535)
printf("%c%c%c", 128 + 64 + 32 + (X >> 12), 128 + ((X >> 6) & 63), 128 + (X & 63));
else
printf("UNKNOWN %d\n", X);
instead of:
if (X <= 127)
printf("%c", (char)X);
else
if (X <= 2047)
printf("%d %d ", 128 + 64 + (X >> 6), 128 + (X & ((1 << 6) - 1)));
else
if (X <= 65535)
printf("%c%c%c", 128 + 64 + 32 + (X >> 12), 128 + ((X >> 6) & 63), 128 + (X & 63));
else
printf("UNKNOWN %d\n", X);
llvm-svn: 39648
2007-06-12 06:26:23 +08:00
|
|
|
} else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) {
|
|
|
|
OS << ' ';
|
|
|
|
PrintRawIfStmt(ElseIf);
|
2007-05-21 07:04:55 +08:00
|
|
|
} else {
|
|
|
|
OS << '\n';
|
|
|
|
PrintStmt(If->getElse());
|
|
|
|
}
|
2006-11-05 02:52:07 +08:00
|
|
|
}
|
2006-11-05 04:40:44 +08:00
|
|
|
}
|
|
|
|
|
Pretty print if/else/elseif chains nicer, like this:
void printutf8(unsigned int X) {
if (X <= 127)
printf("%c", (char)X);
else if (X <= 2047)
printf("%d %d ", 128 + 64 + (X >> 6), 128 + (X & ((1 << 6) - 1)));
else if (X <= 65535)
printf("%c%c%c", 128 + 64 + 32 + (X >> 12), 128 + ((X >> 6) & 63), 128 + (X & 63));
else
printf("UNKNOWN %d\n", X);
instead of:
if (X <= 127)
printf("%c", (char)X);
else
if (X <= 2047)
printf("%d %d ", 128 + 64 + (X >> 6), 128 + (X & ((1 << 6) - 1)));
else
if (X <= 65535)
printf("%c%c%c", 128 + 64 + 32 + (X >> 12), 128 + ((X >> 6) & 63), 128 + (X & 63));
else
printf("UNKNOWN %d\n", X);
llvm-svn: 39648
2007-06-12 06:26:23 +08:00
|
|
|
void StmtPrinter::VisitIfStmt(IfStmt *If) {
|
|
|
|
Indent();
|
|
|
|
PrintRawIfStmt(If);
|
|
|
|
}
|
|
|
|
|
2006-11-05 04:59:27 +08:00
|
|
|
void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) {
|
|
|
|
Indent() << "switch (";
|
|
|
|
PrintExpr(Node->getCond());
|
2007-05-21 07:04:55 +08:00
|
|
|
OS << ")";
|
|
|
|
|
|
|
|
// Pretty print compoundstmt bodies (very common).
|
|
|
|
if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
|
|
|
|
OS << " ";
|
|
|
|
PrintRawCompoundStmt(CS);
|
|
|
|
OS << "\n";
|
|
|
|
} else {
|
|
|
|
OS << "\n";
|
|
|
|
PrintStmt(Node->getBody());
|
|
|
|
}
|
2006-11-05 04:59:27 +08:00
|
|
|
}
|
|
|
|
|
2007-07-22 15:07:56 +08:00
|
|
|
void StmtPrinter::VisitSwitchCase(SwitchCase*) {
|
|
|
|
assert(0 && "SwitchCase is an abstract class");
|
|
|
|
}
|
|
|
|
|
2006-11-05 04:40:44 +08:00
|
|
|
void StmtPrinter::VisitWhileStmt(WhileStmt *Node) {
|
|
|
|
Indent() << "while (";
|
|
|
|
PrintExpr(Node->getCond());
|
|
|
|
OS << ")\n";
|
|
|
|
PrintStmt(Node->getBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
void StmtPrinter::VisitDoStmt(DoStmt *Node) {
|
2007-09-16 05:49:37 +08:00
|
|
|
Indent() << "do ";
|
|
|
|
if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
|
|
|
|
PrintRawCompoundStmt(CS);
|
|
|
|
OS << " ";
|
|
|
|
} else {
|
|
|
|
OS << "\n";
|
|
|
|
PrintStmt(Node->getBody());
|
|
|
|
Indent();
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << "while ";
|
2006-11-05 04:40:44 +08:00
|
|
|
PrintExpr(Node->getCond());
|
2007-06-01 02:21:33 +08:00
|
|
|
OS << ";\n";
|
2006-11-04 15:16:25 +08:00
|
|
|
}
|
|
|
|
|
2006-11-05 04:18:38 +08:00
|
|
|
void StmtPrinter::VisitForStmt(ForStmt *Node) {
|
|
|
|
Indent() << "for (";
|
2007-06-06 04:52:47 +08:00
|
|
|
if (Node->getInit()) {
|
|
|
|
if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit()))
|
2008-10-07 02:39:36 +08:00
|
|
|
PrintRawDeclStmt(DS);
|
2007-06-06 04:52:47 +08:00
|
|
|
else
|
|
|
|
PrintExpr(cast<Expr>(Node->getInit()));
|
|
|
|
}
|
2007-09-16 05:49:37 +08:00
|
|
|
OS << ";";
|
|
|
|
if (Node->getCond()) {
|
|
|
|
OS << " ";
|
2007-06-06 04:52:47 +08:00
|
|
|
PrintExpr(Node->getCond());
|
2007-09-16 05:49:37 +08:00
|
|
|
}
|
|
|
|
OS << ";";
|
|
|
|
if (Node->getInc()) {
|
|
|
|
OS << " ";
|
2007-06-06 04:52:47 +08:00
|
|
|
PrintExpr(Node->getInc());
|
2007-09-16 05:49:37 +08:00
|
|
|
}
|
|
|
|
OS << ") ";
|
|
|
|
|
|
|
|
if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
|
|
|
|
PrintRawCompoundStmt(CS);
|
|
|
|
OS << "\n";
|
|
|
|
} else {
|
|
|
|
OS << "\n";
|
|
|
|
PrintStmt(Node->getBody());
|
|
|
|
}
|
2006-11-05 04:18:38 +08:00
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void StmtPrinter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *Node) {
|
2008-01-03 06:54:34 +08:00
|
|
|
Indent() << "for (";
|
|
|
|
if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getElement()))
|
2008-10-07 02:39:36 +08:00
|
|
|
PrintRawDeclStmt(DS);
|
2008-01-03 06:54:34 +08:00
|
|
|
else
|
|
|
|
PrintExpr(cast<Expr>(Node->getElement()));
|
|
|
|
OS << " in ";
|
|
|
|
PrintExpr(Node->getCollection());
|
|
|
|
OS << ") ";
|
|
|
|
|
|
|
|
if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) {
|
|
|
|
PrintRawCompoundStmt(CS);
|
|
|
|
OS << "\n";
|
|
|
|
} else {
|
|
|
|
OS << "\n";
|
|
|
|
PrintStmt(Node->getBody());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-05 09:46:01 +08:00
|
|
|
void StmtPrinter::VisitGotoStmt(GotoStmt *Node) {
|
2007-05-28 14:56:27 +08:00
|
|
|
Indent() << "goto " << Node->getLabel()->getName() << ";\n";
|
2006-11-05 09:46:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) {
|
2006-11-05 09:51:06 +08:00
|
|
|
Indent() << "goto *";
|
2006-11-05 09:46:01 +08:00
|
|
|
PrintExpr(Node->getTarget());
|
2007-05-31 14:00:14 +08:00
|
|
|
OS << ";\n";
|
2006-11-05 09:46:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) {
|
2007-05-31 14:00:14 +08:00
|
|
|
Indent() << "continue;\n";
|
2006-11-05 09:46:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StmtPrinter::VisitBreakStmt(BreakStmt *Node) {
|
2007-05-31 14:00:14 +08:00
|
|
|
Indent() << "break;\n";
|
2006-11-05 09:46:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-05 02:52:07 +08:00
|
|
|
void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) {
|
|
|
|
Indent() << "return";
|
|
|
|
if (Node->getRetValue()) {
|
|
|
|
OS << " ";
|
|
|
|
PrintExpr(Node->getRetValue());
|
|
|
|
}
|
2007-05-31 14:00:14 +08:00
|
|
|
OS << ";\n";
|
2006-11-05 02:52:07 +08:00
|
|
|
}
|
2006-11-04 15:16:25 +08:00
|
|
|
|
2007-10-29 12:04:16 +08:00
|
|
|
|
|
|
|
void StmtPrinter::VisitAsmStmt(AsmStmt *Node) {
|
2007-11-24 07:12:25 +08:00
|
|
|
Indent() << "asm ";
|
|
|
|
|
|
|
|
if (Node->isVolatile())
|
|
|
|
OS << "volatile ";
|
|
|
|
|
|
|
|
OS << "(";
|
2007-11-21 03:21:03 +08:00
|
|
|
VisitStringLiteral(Node->getAsmString());
|
2007-11-22 09:36:19 +08:00
|
|
|
|
|
|
|
// Outputs
|
|
|
|
if (Node->getNumOutputs() != 0 || Node->getNumInputs() != 0 ||
|
|
|
|
Node->getNumClobbers() != 0)
|
|
|
|
OS << " : ";
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Node->getNumOutputs(); i != e; ++i) {
|
|
|
|
if (i != 0)
|
|
|
|
OS << ", ";
|
|
|
|
|
|
|
|
if (!Node->getOutputName(i).empty()) {
|
|
|
|
OS << '[';
|
|
|
|
OS << Node->getOutputName(i);
|
|
|
|
OS << "] ";
|
|
|
|
}
|
|
|
|
|
|
|
|
VisitStringLiteral(Node->getOutputConstraint(i));
|
|
|
|
OS << " ";
|
|
|
|
Visit(Node->getOutputExpr(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inputs
|
|
|
|
if (Node->getNumInputs() != 0 || Node->getNumClobbers() != 0)
|
|
|
|
OS << " : ";
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Node->getNumInputs(); i != e; ++i) {
|
|
|
|
if (i != 0)
|
|
|
|
OS << ", ";
|
|
|
|
|
|
|
|
if (!Node->getInputName(i).empty()) {
|
|
|
|
OS << '[';
|
|
|
|
OS << Node->getInputName(i);
|
|
|
|
OS << "] ";
|
|
|
|
}
|
|
|
|
|
|
|
|
VisitStringLiteral(Node->getInputConstraint(i));
|
|
|
|
OS << " ";
|
|
|
|
Visit(Node->getInputExpr(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clobbers
|
|
|
|
if (Node->getNumClobbers() != 0)
|
|
|
|
OS << " : ";
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Node->getNumClobbers(); i != e; ++i) {
|
|
|
|
if (i != 0)
|
|
|
|
OS << ", ";
|
|
|
|
|
|
|
|
VisitStringLiteral(Node->getClobber(i));
|
|
|
|
}
|
|
|
|
|
2007-11-21 03:21:03 +08:00
|
|
|
OS << ");\n";
|
2007-10-29 12:04:16 +08:00
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
|
2007-11-03 02:16:07 +08:00
|
|
|
Indent() << "@try";
|
|
|
|
if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
|
|
|
|
PrintRawCompoundStmt(TS);
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
for (ObjCAtCatchStmt *catchStmt =
|
|
|
|
static_cast<ObjCAtCatchStmt *>(Node->getCatchStmts());
|
2007-11-03 02:16:07 +08:00
|
|
|
catchStmt;
|
|
|
|
catchStmt =
|
2008-01-08 03:49:32 +08:00
|
|
|
static_cast<ObjCAtCatchStmt *>(catchStmt->getNextCatchStmt())) {
|
2007-11-03 02:16:07 +08:00
|
|
|
Indent() << "@catch(";
|
|
|
|
if (catchStmt->getCatchParamStmt()) {
|
|
|
|
if (DeclStmt *DS = dyn_cast<DeclStmt>(catchStmt->getCatchParamStmt()))
|
2008-10-07 02:39:36 +08:00
|
|
|
PrintRawDeclStmt(DS);
|
2007-11-03 02:16:07 +08:00
|
|
|
}
|
|
|
|
OS << ")";
|
|
|
|
if (CompoundStmt *CS = dyn_cast<CompoundStmt>(catchStmt->getCatchBody()))
|
|
|
|
{
|
|
|
|
PrintRawCompoundStmt(CS);
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
if (ObjCAtFinallyStmt *FS =static_cast<ObjCAtFinallyStmt *>(
|
2007-11-07 08:46:42 +08:00
|
|
|
Node->getFinallyStmt())) {
|
|
|
|
Indent() << "@finally";
|
|
|
|
PrintRawCompoundStmt(dyn_cast<CompoundStmt>(FS->getFinallyBody()));
|
2007-11-03 02:16:07 +08:00
|
|
|
OS << "\n";
|
|
|
|
}
|
2007-11-02 05:12:44 +08:00
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void StmtPrinter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *Node) {
|
2007-11-02 05:12:44 +08:00
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void StmtPrinter::VisitObjCAtCatchStmt (ObjCAtCatchStmt *Node) {
|
2007-11-02 05:12:44 +08:00
|
|
|
Indent() << "@catch (...) { /* todo */ } \n";
|
|
|
|
}
|
|
|
|
|
2008-01-31 01:38:29 +08:00
|
|
|
void StmtPrinter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *Node) {
|
2007-11-07 10:00:49 +08:00
|
|
|
Indent() << "@throw";
|
|
|
|
if (Node->getThrowExpr()) {
|
|
|
|
OS << " ";
|
|
|
|
PrintExpr(Node->getThrowExpr());
|
|
|
|
}
|
|
|
|
OS << ";\n";
|
|
|
|
}
|
|
|
|
|
2008-01-31 01:38:29 +08:00
|
|
|
void StmtPrinter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *Node) {
|
2008-01-30 02:21:32 +08:00
|
|
|
Indent() << "@synchronized (";
|
|
|
|
PrintExpr(Node->getSynchExpr());
|
|
|
|
OS << ")";
|
2008-01-31 01:38:29 +08:00
|
|
|
PrintRawCompoundStmt(Node->getSynchBody());
|
|
|
|
OS << "\n";
|
2008-01-30 02:21:32 +08:00
|
|
|
}
|
|
|
|
|
2006-11-05 04:18:38 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Expr printing methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2006-11-04 15:16:25 +08:00
|
|
|
|
2006-11-05 02:52:07 +08:00
|
|
|
void StmtPrinter::VisitExpr(Expr *Node) {
|
|
|
|
OS << "<<unknown expr type>>";
|
2006-11-04 15:16:25 +08:00
|
|
|
}
|
|
|
|
|
2006-11-05 02:52:07 +08:00
|
|
|
void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
|
2006-11-20 13:01:40 +08:00
|
|
|
OS << Node->getDecl()->getName();
|
2006-11-04 15:16:25 +08:00
|
|
|
}
|
|
|
|
|
2007-11-12 22:29:37 +08:00
|
|
|
void StmtPrinter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
|
2007-11-13 06:29:28 +08:00
|
|
|
if (Node->getBase()) {
|
|
|
|
PrintExpr(Node->getBase());
|
|
|
|
OS << (Node->isArrow() ? "->" : ".");
|
|
|
|
}
|
2007-11-12 22:29:37 +08:00
|
|
|
OS << Node->getDecl()->getName();
|
|
|
|
}
|
|
|
|
|
2008-05-30 08:40:33 +08:00
|
|
|
void StmtPrinter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *Node) {
|
|
|
|
if (Node->getBase()) {
|
|
|
|
PrintExpr(Node->getBase());
|
|
|
|
OS << ".";
|
|
|
|
}
|
|
|
|
// FIXME: OS << Node->getDecl()->getName();
|
|
|
|
}
|
|
|
|
|
2008-08-10 09:53:14 +08:00
|
|
|
void StmtPrinter::VisitPredefinedExpr(PredefinedExpr *Node) {
|
2007-07-21 13:21:51 +08:00
|
|
|
switch (Node->getIdentType()) {
|
|
|
|
default:
|
|
|
|
assert(0 && "unknown case");
|
2008-08-10 09:53:14 +08:00
|
|
|
case PredefinedExpr::Func:
|
2007-07-21 13:21:51 +08:00
|
|
|
OS << "__func__";
|
|
|
|
break;
|
2008-08-10 09:53:14 +08:00
|
|
|
case PredefinedExpr::Function:
|
2007-07-21 13:21:51 +08:00
|
|
|
OS << "__FUNCTION__";
|
|
|
|
break;
|
2008-08-10 09:53:14 +08:00
|
|
|
case PredefinedExpr::PrettyFunction:
|
2007-07-21 13:21:51 +08:00
|
|
|
OS << "__PRETTY_FUNCTION__";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-27 04:39:23 +08:00
|
|
|
void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
|
2007-07-13 13:18:11 +08:00
|
|
|
unsigned value = Node->getValue();
|
2008-06-08 06:35:38 +08:00
|
|
|
if (Node->isWide())
|
|
|
|
OS << "L";
|
2007-07-14 07:58:20 +08:00
|
|
|
switch (value) {
|
|
|
|
case '\\':
|
|
|
|
OS << "'\\\\'";
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
OS << "'\\''";
|
|
|
|
break;
|
|
|
|
case '\a':
|
|
|
|
// TODO: K&R: the meaning of '\\a' is different in traditional C
|
|
|
|
OS << "'\\a'";
|
|
|
|
break;
|
|
|
|
case '\b':
|
|
|
|
OS << "'\\b'";
|
|
|
|
break;
|
|
|
|
// Nonstandard escape sequence.
|
|
|
|
/*case '\e':
|
|
|
|
OS << "'\\e'";
|
|
|
|
break;*/
|
|
|
|
case '\f':
|
|
|
|
OS << "'\\f'";
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
OS << "'\\n'";
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
OS << "'\\r'";
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
OS << "'\\t'";
|
|
|
|
break;
|
|
|
|
case '\v':
|
|
|
|
OS << "'\\v'";
|
|
|
|
break;
|
|
|
|
default:
|
2008-02-23 08:52:04 +08:00
|
|
|
if (value < 256 && isprint(value)) {
|
2007-07-14 07:58:20 +08:00
|
|
|
OS << "'" << (char)value << "'";
|
|
|
|
} else if (value < 256) {
|
2008-09-13 13:16:45 +08:00
|
|
|
OS << "'\\x" << llvm::format("%x", value) << "'";
|
2007-07-14 07:58:20 +08:00
|
|
|
} else {
|
|
|
|
// FIXME what to really do here?
|
|
|
|
OS << value;
|
|
|
|
}
|
2007-07-13 13:18:11 +08:00
|
|
|
}
|
2007-04-27 04:39:23 +08:00
|
|
|
}
|
|
|
|
|
2007-02-22 07:46:25 +08:00
|
|
|
void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
|
2007-05-21 13:45:03 +08:00
|
|
|
bool isSigned = Node->getType()->isSignedIntegerType();
|
|
|
|
OS << Node->getValue().toString(10, isSigned);
|
|
|
|
|
|
|
|
// Emit suffixes. Integer literals are always a builtin integer type.
|
2008-07-27 06:17:49 +08:00
|
|
|
switch (Node->getType()->getAsBuiltinType()->getKind()) {
|
2007-05-21 13:45:03 +08:00
|
|
|
default: assert(0 && "Unexpected type for integer literal!");
|
|
|
|
case BuiltinType::Int: break; // no suffix.
|
|
|
|
case BuiltinType::UInt: OS << 'U'; break;
|
|
|
|
case BuiltinType::Long: OS << 'L'; break;
|
|
|
|
case BuiltinType::ULong: OS << "UL"; break;
|
|
|
|
case BuiltinType::LongLong: OS << "LL"; break;
|
|
|
|
case BuiltinType::ULongLong: OS << "ULL"; break;
|
|
|
|
}
|
2006-11-04 15:16:25 +08:00
|
|
|
}
|
2007-02-22 06:05:47 +08:00
|
|
|
void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) {
|
2007-08-01 08:23:58 +08:00
|
|
|
// FIXME: print value more precisely.
|
2008-06-08 06:13:43 +08:00
|
|
|
OS << Node->getValueAsApproximateDouble();
|
2006-11-04 15:16:25 +08:00
|
|
|
}
|
2007-08-26 11:42:43 +08:00
|
|
|
|
|
|
|
void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {
|
|
|
|
PrintExpr(Node->getSubExpr());
|
|
|
|
OS << "i";
|
|
|
|
}
|
|
|
|
|
2007-02-22 07:46:25 +08:00
|
|
|
void StmtPrinter::VisitStringLiteral(StringLiteral *Str) {
|
2006-11-05 02:52:07 +08:00
|
|
|
if (Str->isWide()) OS << 'L';
|
2006-11-05 04:29:31 +08:00
|
|
|
OS << '"';
|
2007-10-15 10:50:23 +08:00
|
|
|
|
2006-11-05 04:29:31 +08:00
|
|
|
// 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 15:16:25 +08:00
|
|
|
}
|
2006-11-05 02:52:07 +08:00
|
|
|
void StmtPrinter::VisitParenExpr(ParenExpr *Node) {
|
|
|
|
OS << "(";
|
|
|
|
PrintExpr(Node->getSubExpr());
|
2007-06-01 02:21:33 +08:00
|
|
|
OS << ")";
|
2006-11-04 15:16:25 +08:00
|
|
|
}
|
2006-11-05 02:52:07 +08:00
|
|
|
void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
|
2007-08-24 05:46:40 +08:00
|
|
|
if (!Node->isPostfix()) {
|
2006-11-06 07:54:51 +08:00
|
|
|
OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
|
2007-08-24 05:46:40 +08:00
|
|
|
|
2008-11-12 01:56:53 +08:00
|
|
|
// Print a space if this is an "identifier operator" like __real.
|
2007-08-24 05:46:40 +08:00
|
|
|
switch (Node->getOpcode()) {
|
|
|
|
default: break;
|
|
|
|
case UnaryOperator::Real:
|
|
|
|
case UnaryOperator::Imag:
|
|
|
|
case UnaryOperator::Extension:
|
|
|
|
OS << ' ';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-11-05 02:52:07 +08:00
|
|
|
PrintExpr(Node->getSubExpr());
|
2006-11-06 07:54:51 +08:00
|
|
|
|
|
|
|
if (Node->isPostfix())
|
|
|
|
OS << UnaryOperator::getOpcodeStr(Node->getOpcode());
|
2007-08-31 01:59:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StmtPrinter::PrintOffsetOfDesignator(Expr *E) {
|
|
|
|
if (isa<CompoundLiteralExpr>(E)) {
|
|
|
|
// Base case, print the type and comma.
|
|
|
|
OS << E->getType().getAsString() << ", ";
|
|
|
|
return true;
|
|
|
|
} else if (ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
|
|
|
|
PrintOffsetOfDesignator(ASE->getLHS());
|
|
|
|
OS << "[";
|
|
|
|
PrintExpr(ASE->getRHS());
|
|
|
|
OS << "]";
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
MemberExpr *ME = cast<MemberExpr>(E);
|
|
|
|
bool IsFirst = PrintOffsetOfDesignator(ME->getBase());
|
|
|
|
OS << (IsFirst ? "" : ".") << ME->getMemberDecl()->getName();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2006-11-06 07:54:51 +08:00
|
|
|
|
2007-08-31 01:59:59 +08:00
|
|
|
void StmtPrinter::VisitUnaryOffsetOf(UnaryOperator *Node) {
|
|
|
|
OS << "__builtin_offsetof(";
|
|
|
|
PrintOffsetOfDesignator(Node->getSubExpr());
|
|
|
|
OS << ")";
|
2006-11-04 15:16:25 +08:00
|
|
|
}
|
2007-08-31 01:59:59 +08:00
|
|
|
|
2008-11-12 01:56:53 +08:00
|
|
|
void StmtPrinter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *Node) {
|
|
|
|
OS << (Node->isSizeOf() ? "sizeof" : "__alignof");
|
|
|
|
if (Node->isArgumentType())
|
|
|
|
OS << "(" << Node->getArgumentType().getAsString() << ")";
|
|
|
|
else {
|
|
|
|
OS << " ";
|
|
|
|
PrintExpr(Node->getArgumentExpr());
|
|
|
|
}
|
2006-11-04 15:16:25 +08:00
|
|
|
}
|
2006-11-05 02:52:07 +08:00
|
|
|
void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
|
2007-08-21 00:18:38 +08:00
|
|
|
PrintExpr(Node->getLHS());
|
2006-11-05 02:52:07 +08:00
|
|
|
OS << "[";
|
2007-08-21 00:18:38 +08:00
|
|
|
PrintExpr(Node->getRHS());
|
2006-11-05 02:52:07 +08:00
|
|
|
OS << "]";
|
2006-11-04 15:16:25 +08:00
|
|
|
}
|
|
|
|
|
2006-11-05 02:52:07 +08:00
|
|
|
void StmtPrinter::VisitCallExpr(CallExpr *Call) {
|
|
|
|
PrintExpr(Call->getCallee());
|
|
|
|
OS << "(";
|
|
|
|
for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) {
|
2008-04-08 12:40:51 +08:00
|
|
|
if (isa<CXXDefaultArgExpr>(Call->getArg(i))) {
|
|
|
|
// Don't print any defaulted arguments
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-11-05 02:52:07 +08:00
|
|
|
if (i) OS << ", ";
|
|
|
|
PrintExpr(Call->getArg(i));
|
2006-11-04 15:16:25 +08:00
|
|
|
}
|
2006-11-05 02:52:07 +08:00
|
|
|
OS << ")";
|
2006-11-04 15:16:25 +08:00
|
|
|
}
|
2006-11-05 02:52:07 +08:00
|
|
|
void StmtPrinter::VisitMemberExpr(MemberExpr *Node) {
|
|
|
|
PrintExpr(Node->getBase());
|
|
|
|
OS << (Node->isArrow() ? "->" : ".");
|
2006-11-04 15:16:25 +08:00
|
|
|
|
2007-05-24 08:47:01 +08:00
|
|
|
FieldDecl *Field = Node->getMemberDecl();
|
|
|
|
assert(Field && "MemberExpr should alway reference a field!");
|
|
|
|
OS << Field->getName();
|
2006-11-04 15:16:25 +08:00
|
|
|
}
|
2008-04-19 07:10:10 +08:00
|
|
|
void StmtPrinter::VisitExtVectorElementExpr(ExtVectorElementExpr *Node) {
|
2007-07-29 07:10:27 +08:00
|
|
|
PrintExpr(Node->getBase());
|
|
|
|
OS << ".";
|
|
|
|
OS << Node->getAccessor().getName();
|
|
|
|
}
|
2008-08-19 07:01:59 +08:00
|
|
|
void StmtPrinter::VisitCastExpr(CastExpr *) {
|
|
|
|
assert(0 && "CastExpr is an abstract class");
|
|
|
|
}
|
2008-10-28 03:41:14 +08:00
|
|
|
void StmtPrinter::VisitExplicitCastExpr(ExplicitCastExpr *) {
|
|
|
|
assert(0 && "ExplicitCastExpr is an abstract class");
|
|
|
|
}
|
2008-10-28 23:36:24 +08:00
|
|
|
void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
|
2007-07-16 07:54:50 +08:00
|
|
|
OS << "(" << Node->getType().getAsString() << ")";
|
2006-11-05 02:52:07 +08:00
|
|
|
PrintExpr(Node->getSubExpr());
|
|
|
|
}
|
2007-07-20 05:32:11 +08:00
|
|
|
void StmtPrinter::VisitCompoundLiteralExpr(CompoundLiteralExpr *Node) {
|
|
|
|
OS << "(" << Node->getType().getAsString() << ")";
|
|
|
|
PrintExpr(Node->getInitializer());
|
|
|
|
}
|
2007-07-14 00:58:59 +08:00
|
|
|
void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) {
|
2007-07-14 07:32:42 +08:00
|
|
|
// No need to print anything, simply forward to the sub expression.
|
|
|
|
PrintExpr(Node->getSubExpr());
|
2007-07-14 00:58:59 +08:00
|
|
|
}
|
2006-11-05 02:52:07 +08:00
|
|
|
void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) {
|
|
|
|
PrintExpr(Node->getLHS());
|
|
|
|
OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
|
2007-08-25 10:00:02 +08:00
|
|
|
PrintExpr(Node->getRHS());
|
|
|
|
}
|
|
|
|
void StmtPrinter::VisitCompoundAssignOperator(CompoundAssignOperator *Node) {
|
|
|
|
PrintExpr(Node->getLHS());
|
|
|
|
OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " ";
|
2006-11-05 02:52:07 +08:00
|
|
|
PrintExpr(Node->getRHS());
|
|
|
|
}
|
|
|
|
void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) {
|
|
|
|
PrintExpr(Node->getCond());
|
2007-11-27 02:27:54 +08:00
|
|
|
|
|
|
|
if (Node->getLHS()) {
|
|
|
|
OS << " ? ";
|
|
|
|
PrintExpr(Node->getLHS());
|
|
|
|
OS << " : ";
|
|
|
|
}
|
|
|
|
else { // Handle GCC extention where LHS can be NULL.
|
|
|
|
OS << " ?: ";
|
|
|
|
}
|
|
|
|
|
2006-11-05 02:52:07 +08:00
|
|
|
PrintExpr(Node->getRHS());
|
2006-11-04 15:16:25 +08:00
|
|
|
}
|
|
|
|
|
2007-05-28 14:56:27 +08:00
|
|
|
// GNU extensions.
|
|
|
|
|
2007-08-04 01:31:20 +08:00
|
|
|
void StmtPrinter::VisitAddrLabelExpr(AddrLabelExpr *Node) {
|
2007-05-28 14:56:27 +08:00
|
|
|
OS << "&&" << Node->getLabel()->getName();
|
|
|
|
}
|
|
|
|
|
2007-07-25 00:58:17 +08:00
|
|
|
void StmtPrinter::VisitStmtExpr(StmtExpr *E) {
|
|
|
|
OS << "(";
|
|
|
|
PrintRawCompoundStmt(E->getSubStmt());
|
|
|
|
OS << ")";
|
|
|
|
}
|
|
|
|
|
2007-08-02 06:05:33 +08:00
|
|
|
void StmtPrinter::VisitTypesCompatibleExpr(TypesCompatibleExpr *Node) {
|
|
|
|
OS << "__builtin_types_compatible_p(";
|
|
|
|
OS << Node->getArgType1().getAsString() << ",";
|
|
|
|
OS << Node->getArgType2().getAsString() << ")";
|
|
|
|
}
|
|
|
|
|
2007-08-04 05:21:27 +08:00
|
|
|
void StmtPrinter::VisitChooseExpr(ChooseExpr *Node) {
|
|
|
|
OS << "__builtin_choose_expr(";
|
|
|
|
PrintExpr(Node->getCond());
|
2007-08-04 08:20:15 +08:00
|
|
|
OS << ", ";
|
2007-08-04 05:21:27 +08:00
|
|
|
PrintExpr(Node->getLHS());
|
2007-08-04 08:20:15 +08:00
|
|
|
OS << ", ";
|
2007-08-04 05:21:27 +08:00
|
|
|
PrintExpr(Node->getRHS());
|
|
|
|
OS << ")";
|
|
|
|
}
|
2007-07-25 00:58:17 +08:00
|
|
|
|
2008-01-18 01:46:27 +08:00
|
|
|
void StmtPrinter::VisitOverloadExpr(OverloadExpr *Node) {
|
|
|
|
OS << "__builtin_overload(";
|
2008-01-31 04:50:20 +08:00
|
|
|
for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
|
2008-01-18 01:46:27 +08:00
|
|
|
if (i) OS << ", ";
|
2008-01-31 04:50:20 +08:00
|
|
|
PrintExpr(Node->getExpr(i));
|
2008-01-18 01:46:27 +08:00
|
|
|
}
|
|
|
|
OS << ")";
|
2008-05-15 03:38:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StmtPrinter::VisitShuffleVectorExpr(ShuffleVectorExpr *Node) {
|
|
|
|
OS << "__builtin_shufflevector(";
|
|
|
|
for (unsigned i = 0, e = Node->getNumSubExprs(); i != e; ++i) {
|
|
|
|
if (i) OS << ", ";
|
|
|
|
PrintExpr(Node->getExpr(i));
|
|
|
|
}
|
|
|
|
OS << ")";
|
2008-01-18 01:46:27 +08:00
|
|
|
}
|
|
|
|
|
2007-08-31 12:56:16 +08:00
|
|
|
void StmtPrinter::VisitInitListExpr(InitListExpr* Node) {
|
|
|
|
OS << "{ ";
|
|
|
|
for (unsigned i = 0, e = Node->getNumInits(); i != e; ++i) {
|
|
|
|
if (i) OS << ", ";
|
|
|
|
PrintExpr(Node->getInit(i));
|
|
|
|
}
|
|
|
|
OS << " }";
|
|
|
|
}
|
|
|
|
|
2007-10-16 04:28:48 +08:00
|
|
|
void StmtPrinter::VisitVAArgExpr(VAArgExpr *Node) {
|
|
|
|
OS << "va_arg(";
|
|
|
|
PrintExpr(Node->getSubExpr());
|
|
|
|
OS << ", ";
|
|
|
|
OS << Node->getType().getAsString();
|
|
|
|
OS << ")";
|
|
|
|
}
|
|
|
|
|
2007-05-28 14:56:27 +08:00
|
|
|
// C++
|
2008-11-15 00:09:21 +08:00
|
|
|
void StmtPrinter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *Node) {
|
|
|
|
const char *OpStrings[NUM_OVERLOADED_OPERATORS] = {
|
|
|
|
"",
|
|
|
|
#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
|
|
|
|
Spelling,
|
|
|
|
#include "clang/Basic/OperatorKinds.def"
|
|
|
|
};
|
|
|
|
|
|
|
|
OverloadedOperatorKind Kind = Node->getOperator();
|
|
|
|
if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
|
|
|
|
if (Node->getNumArgs() == 1) {
|
|
|
|
OS << OpStrings[Kind] << ' ';
|
|
|
|
PrintExpr(Node->getArg(0));
|
|
|
|
} else {
|
|
|
|
PrintExpr(Node->getArg(0));
|
|
|
|
OS << ' ' << OpStrings[Kind];
|
|
|
|
}
|
|
|
|
} else if (Kind == OO_Call) {
|
|
|
|
PrintExpr(Node->getArg(0));
|
|
|
|
OS << '(';
|
|
|
|
for (unsigned ArgIdx = 1; ArgIdx < Node->getNumArgs(); ++ArgIdx) {
|
|
|
|
if (ArgIdx > 1)
|
|
|
|
OS << ", ";
|
|
|
|
if (!isa<CXXDefaultArgExpr>(Node->getArg(ArgIdx)))
|
|
|
|
PrintExpr(Node->getArg(ArgIdx));
|
|
|
|
}
|
|
|
|
OS << ')';
|
|
|
|
} else if (Kind == OO_Subscript) {
|
|
|
|
PrintExpr(Node->getArg(0));
|
|
|
|
OS << '[';
|
|
|
|
PrintExpr(Node->getArg(1));
|
|
|
|
OS << ']';
|
|
|
|
} else if (Node->getNumArgs() == 1) {
|
|
|
|
OS << OpStrings[Kind] << ' ';
|
|
|
|
PrintExpr(Node->getArg(0));
|
|
|
|
} else if (Node->getNumArgs() == 2) {
|
|
|
|
PrintExpr(Node->getArg(0));
|
|
|
|
OS << ' ' << OpStrings[Kind] << ' ';
|
|
|
|
PrintExpr(Node->getArg(1));
|
|
|
|
} else {
|
|
|
|
assert(false && "unknown overloaded operator");
|
|
|
|
}
|
|
|
|
}
|
2007-05-28 14:56:27 +08:00
|
|
|
|
2008-10-28 03:41:14 +08:00
|
|
|
void StmtPrinter::VisitCXXNamedCastExpr(CXXNamedCastExpr *Node) {
|
|
|
|
OS << Node->getCastName() << '<';
|
|
|
|
OS << Node->getTypeAsWritten().getAsString() << ">(";
|
2007-05-28 14:56:27 +08:00
|
|
|
PrintExpr(Node->getSubExpr());
|
|
|
|
OS << ")";
|
|
|
|
}
|
|
|
|
|
2008-10-28 03:41:14 +08:00
|
|
|
void StmtPrinter::VisitCXXStaticCastExpr(CXXStaticCastExpr *Node) {
|
|
|
|
VisitCXXNamedCastExpr(Node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StmtPrinter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *Node) {
|
|
|
|
VisitCXXNamedCastExpr(Node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StmtPrinter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *Node) {
|
|
|
|
VisitCXXNamedCastExpr(Node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StmtPrinter::VisitCXXConstCastExpr(CXXConstCastExpr *Node) {
|
|
|
|
VisitCXXNamedCastExpr(Node);
|
|
|
|
}
|
|
|
|
|
2008-11-11 19:37:55 +08:00
|
|
|
void StmtPrinter::VisitCXXTypeidExpr(CXXTypeidExpr *Node) {
|
|
|
|
OS << "typeid(";
|
|
|
|
if (Node->isTypeOperand()) {
|
|
|
|
OS << Node->getTypeOperand().getAsString();
|
|
|
|
} else {
|
|
|
|
PrintExpr(Node->getExprOperand());
|
|
|
|
}
|
|
|
|
OS << ")";
|
|
|
|
}
|
|
|
|
|
2007-05-28 14:56:27 +08:00
|
|
|
void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) {
|
|
|
|
OS << (Node->getValue() ? "true" : "false");
|
|
|
|
}
|
|
|
|
|
2008-11-04 22:32:21 +08:00
|
|
|
void StmtPrinter::VisitCXXThisExpr(CXXThisExpr *Node) {
|
|
|
|
OS << "this";
|
|
|
|
}
|
|
|
|
|
2008-02-26 08:51:44 +08:00
|
|
|
void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
|
|
|
|
if (Node->getSubExpr() == 0)
|
|
|
|
OS << "throw";
|
|
|
|
else {
|
|
|
|
OS << "throw ";
|
|
|
|
PrintExpr(Node->getSubExpr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-08 12:40:51 +08:00
|
|
|
void StmtPrinter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Node) {
|
|
|
|
// Nothing to print: we picked up the default argument
|
|
|
|
}
|
|
|
|
|
2008-08-22 23:38:55 +08:00
|
|
|
void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
|
|
|
|
OS << Node->getType().getAsString();
|
|
|
|
OS << "(";
|
|
|
|
PrintExpr(Node->getSubExpr());
|
|
|
|
OS << ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
|
|
|
|
OS << Node->getType().getAsString() << "()";
|
|
|
|
}
|
|
|
|
|
2008-09-10 07:47:53 +08:00
|
|
|
void
|
|
|
|
StmtPrinter::VisitCXXConditionDeclExpr(CXXConditionDeclExpr *E) {
|
|
|
|
PrintRawDecl(E->getVarDecl());
|
|
|
|
}
|
|
|
|
|
2008-11-22 03:14:01 +08:00
|
|
|
void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
|
|
|
|
if (E->isGlobalNew())
|
|
|
|
OS << "::";
|
|
|
|
OS << "new ";
|
|
|
|
unsigned NumPlace = E->getNumPlacementArgs();
|
|
|
|
if (NumPlace > 0) {
|
|
|
|
OS << "(";
|
|
|
|
PrintExpr(E->getPlacementArg(0));
|
|
|
|
for (unsigned i = 1; i < NumPlace; ++i) {
|
|
|
|
OS << ", ";
|
|
|
|
PrintExpr(E->getPlacementArg(i));
|
|
|
|
}
|
|
|
|
OS << ") ";
|
|
|
|
}
|
|
|
|
if (E->isParenTypeId())
|
|
|
|
OS << "(";
|
|
|
|
OS << E->getAllocatedType().getAsString();
|
|
|
|
if (E->isParenTypeId())
|
|
|
|
OS << ")";
|
|
|
|
|
|
|
|
if (E->hasInitializer()) {
|
|
|
|
OS << "(";
|
|
|
|
unsigned NumCons = E->getNumConstructorArgs();
|
|
|
|
if (NumCons > 0) {
|
|
|
|
PrintExpr(E->getConstructorArg(0));
|
|
|
|
for (unsigned i = 1; i < NumCons; ++i) {
|
|
|
|
OS << ", ";
|
|
|
|
PrintExpr(E->getConstructorArg(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OS << ")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StmtPrinter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
|
|
|
|
if (E->isGlobalDelete())
|
|
|
|
OS << "::";
|
|
|
|
OS << "delete ";
|
|
|
|
if (E->isArrayForm())
|
|
|
|
OS << "[] ";
|
|
|
|
PrintExpr(E->getArgument());
|
|
|
|
}
|
|
|
|
|
2007-08-22 01:43:55 +08:00
|
|
|
// Obj-C
|
|
|
|
|
|
|
|
void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
|
|
|
|
OS << "@";
|
|
|
|
VisitStringLiteral(Node->getString());
|
|
|
|
}
|
2007-05-28 14:56:27 +08:00
|
|
|
|
2007-08-22 23:14:15 +08:00
|
|
|
void StmtPrinter::VisitObjCEncodeExpr(ObjCEncodeExpr *Node) {
|
2007-10-18 08:39:29 +08:00
|
|
|
OS << "@encode(" << Node->getEncodedType().getAsString() << ")";
|
2007-08-22 23:14:15 +08:00
|
|
|
}
|
|
|
|
|
2007-10-17 04:40:23 +08:00
|
|
|
void StmtPrinter::VisitObjCSelectorExpr(ObjCSelectorExpr *Node) {
|
2007-10-18 08:39:29 +08:00
|
|
|
OS << "@selector(" << Node->getSelector().getName() << ")";
|
2007-10-17 04:40:23 +08:00
|
|
|
}
|
|
|
|
|
2007-10-18 00:58:11 +08:00
|
|
|
void StmtPrinter::VisitObjCProtocolExpr(ObjCProtocolExpr *Node) {
|
2007-10-18 08:39:29 +08:00
|
|
|
OS << "@protocol(" << Node->getProtocol()->getName() << ")";
|
2007-10-18 00:58:11 +08:00
|
|
|
}
|
|
|
|
|
2007-09-19 07:55:05 +08:00
|
|
|
void StmtPrinter::VisitObjCMessageExpr(ObjCMessageExpr *Mess) {
|
|
|
|
OS << "[";
|
2007-10-03 04:01:56 +08:00
|
|
|
Expr *receiver = Mess->getReceiver();
|
|
|
|
if (receiver) PrintExpr(receiver);
|
|
|
|
else OS << Mess->getClassName()->getName();
|
2008-05-03 01:32:38 +08:00
|
|
|
OS << ' ';
|
2008-04-16 12:30:16 +08:00
|
|
|
Selector selector = Mess->getSelector();
|
2007-10-03 04:01:56 +08:00
|
|
|
if (selector.isUnarySelector()) {
|
2008-05-03 01:32:38 +08:00
|
|
|
OS << selector.getIdentifierInfoForSlot(0)->getName();
|
2007-10-03 04:01:56 +08:00
|
|
|
} else {
|
|
|
|
for (unsigned i = 0, e = Mess->getNumArgs(); i != e; ++i) {
|
2008-05-03 01:32:38 +08:00
|
|
|
if (i < selector.getNumArgs()) {
|
|
|
|
if (i > 0) OS << ' ';
|
|
|
|
if (selector.getIdentifierInfoForSlot(i))
|
|
|
|
OS << selector.getIdentifierInfoForSlot(i)->getName() << ":";
|
|
|
|
else
|
|
|
|
OS << ":";
|
|
|
|
}
|
|
|
|
else OS << ", "; // Handle variadic methods.
|
|
|
|
|
2007-10-03 04:01:56 +08:00
|
|
|
PrintExpr(Mess->getArg(i));
|
|
|
|
}
|
2007-09-19 07:55:05 +08:00
|
|
|
}
|
|
|
|
OS << "]";
|
|
|
|
}
|
|
|
|
|
2008-11-04 22:56:14 +08:00
|
|
|
void StmtPrinter::VisitObjCSuperExpr(ObjCSuperExpr *) {
|
|
|
|
OS << "super";
|
|
|
|
}
|
|
|
|
|
2008-09-04 02:15:37 +08:00
|
|
|
void StmtPrinter::VisitBlockExpr(BlockExpr *Node) {
|
2008-10-09 01:01:13 +08:00
|
|
|
BlockDecl *BD = Node->getBlockDecl();
|
2008-09-04 02:15:37 +08:00
|
|
|
OS << "^";
|
|
|
|
|
|
|
|
const FunctionType *AFT = Node->getFunctionType();
|
|
|
|
|
|
|
|
if (isa<FunctionTypeNoProto>(AFT)) {
|
|
|
|
OS << "()";
|
2008-10-09 01:01:13 +08:00
|
|
|
} else if (!BD->param_empty() || cast<FunctionTypeProto>(AFT)->isVariadic()) {
|
2008-09-04 02:15:37 +08:00
|
|
|
OS << '(';
|
|
|
|
std::string ParamStr;
|
2008-10-09 01:01:13 +08:00
|
|
|
for (BlockDecl::param_iterator AI = BD->param_begin(),
|
|
|
|
E = BD->param_end(); AI != E; ++AI) {
|
|
|
|
if (AI != BD->param_begin()) OS << ", ";
|
2008-09-04 02:15:37 +08:00
|
|
|
ParamStr = (*AI)->getName();
|
|
|
|
(*AI)->getType().getAsStringInternal(ParamStr);
|
|
|
|
OS << ParamStr;
|
|
|
|
}
|
|
|
|
|
2008-10-09 01:01:13 +08:00
|
|
|
const FunctionTypeProto *FT = cast<FunctionTypeProto>(AFT);
|
2008-09-04 02:15:37 +08:00
|
|
|
if (FT->isVariadic()) {
|
2008-10-09 01:01:13 +08:00
|
|
|
if (!BD->param_empty()) OS << ", ";
|
2008-09-04 02:15:37 +08:00
|
|
|
OS << "...";
|
|
|
|
}
|
|
|
|
OS << ')';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
|
|
|
|
OS << Node->getDecl()->getName();
|
|
|
|
}
|
2006-11-04 15:16:25 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Stmt method implementations
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-08-09 06:51:59 +08:00
|
|
|
void Stmt::dumpPretty() const {
|
2008-09-13 13:16:45 +08:00
|
|
|
printPretty(llvm::errs());
|
2006-11-04 15:16:25 +08:00
|
|
|
}
|
|
|
|
|
2008-09-13 13:16:45 +08:00
|
|
|
void Stmt::printPretty(llvm::raw_ostream &OS, PrinterHelper* Helper) const {
|
2006-11-05 02:52:07 +08:00
|
|
|
if (this == 0) {
|
|
|
|
OS << "<NULL>";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-09-01 05:30:12 +08:00
|
|
|
StmtPrinter P(OS, Helper);
|
2007-08-21 12:04:25 +08:00
|
|
|
P.Visit(const_cast<Stmt*>(this));
|
2006-11-04 15:16:25 +08:00
|
|
|
}
|
2007-09-01 05:30:12 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PrinterHelper
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Implement virtual destructor.
|
2007-09-11 23:32:40 +08:00
|
|
|
PrinterHelper::~PrinterHelper() {}
|