Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- StmtPrinter.cpp - Printing implementation for Stmt ASTs ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Stmt::dump/Stmt::print methods. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/StmtVisitor.h" |
| 15 | #include "clang/AST/Decl.h" |
| 16 | #include "clang/AST/ExprCXX.h" |
| 17 | #include "clang/Lex/IdentifierTable.h" |
| 18 | #include "llvm/Support/Compiler.h" |
| 19 | #include <iostream> |
Chris Lattner | b0a721a | 2007-07-13 05:18:11 +0000 | [diff] [blame] | 20 | #include <iomanip> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // StmtPrinter Visitor |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
| 27 | namespace { |
| 28 | class VISIBILITY_HIDDEN StmtPrinter : public StmtVisitor { |
| 29 | std::ostream &OS; |
| 30 | unsigned IndentLevel; |
| 31 | public: |
| 32 | StmtPrinter(std::ostream &os) : OS(os), IndentLevel(0) {} |
| 33 | |
| 34 | void PrintStmt(Stmt *S, int SubIndent = 1) { |
| 35 | IndentLevel += SubIndent; |
| 36 | if (S && isa<Expr>(S)) { |
| 37 | // If this is an expr used in a stmt context, indent and newline it. |
| 38 | Indent(); |
| 39 | S->visit(*this); |
| 40 | OS << ";\n"; |
| 41 | } else if (S) { |
| 42 | S->visit(*this); |
| 43 | } else { |
| 44 | Indent() << "<<<NULL STATEMENT>>>\n"; |
| 45 | } |
| 46 | IndentLevel -= SubIndent; |
| 47 | } |
| 48 | |
| 49 | void PrintRawCompoundStmt(CompoundStmt *S); |
| 50 | void PrintRawDecl(Decl *D); |
| 51 | void PrintRawIfStmt(IfStmt *If); |
| 52 | |
| 53 | void PrintExpr(Expr *E) { |
| 54 | if (E) |
| 55 | E->visit(*this); |
| 56 | else |
| 57 | OS << "<null expr>"; |
| 58 | } |
| 59 | |
| 60 | std::ostream &Indent(int Delta = 0) const { |
| 61 | for (int i = 0, e = IndentLevel+Delta; i < e; ++i) |
| 62 | OS << " "; |
| 63 | return OS; |
| 64 | } |
| 65 | |
| 66 | virtual void VisitStmt(Stmt *Node); |
| 67 | #define STMT(N, CLASS, PARENT) \ |
| 68 | virtual void Visit##CLASS(CLASS *Node); |
| 69 | #include "clang/AST/StmtNodes.def" |
| 70 | }; |
| 71 | } |
| 72 | |
| 73 | //===----------------------------------------------------------------------===// |
| 74 | // Stmt printing methods. |
| 75 | //===----------------------------------------------------------------------===// |
| 76 | |
| 77 | void StmtPrinter::VisitStmt(Stmt *Node) { |
| 78 | Indent() << "<<unknown stmt type>>\n"; |
| 79 | } |
| 80 | |
| 81 | /// PrintRawCompoundStmt - Print a compound stmt without indenting the {, and |
| 82 | /// with no newline after the }. |
| 83 | void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) { |
| 84 | OS << "{\n"; |
| 85 | for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end(); |
| 86 | I != E; ++I) |
| 87 | PrintStmt(*I); |
| 88 | |
| 89 | Indent() << "}"; |
| 90 | } |
| 91 | |
| 92 | void StmtPrinter::PrintRawDecl(Decl *D) { |
| 93 | // FIXME: Need to complete/beautify this... this code simply shows the |
| 94 | // nodes are where they need to be. |
| 95 | if (TypedefDecl *localType = dyn_cast<TypedefDecl>(D)) { |
| 96 | OS << "typedef " << localType->getUnderlyingType().getAsString(); |
| 97 | OS << " " << localType->getName(); |
| 98 | } else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 99 | // Emit storage class for vardecls. |
| 100 | if (VarDecl *V = dyn_cast<VarDecl>(VD)) { |
| 101 | switch (V->getStorageClass()) { |
| 102 | default: assert(0 && "Unknown storage class!"); |
| 103 | case VarDecl::None: break; |
| 104 | case VarDecl::Extern: OS << "extern "; break; |
| 105 | case VarDecl::Static: OS << "static "; break; |
| 106 | case VarDecl::Auto: OS << "auto "; break; |
| 107 | case VarDecl::Register: OS << "register "; break; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | std::string Name = VD->getName(); |
| 112 | VD->getType().getAsStringInternal(Name); |
| 113 | OS << Name; |
| 114 | |
Chris Lattner | 24c3990 | 2007-07-12 00:36:32 +0000 | [diff] [blame] | 115 | // If this is a vardecl with an initializer, emit it. |
| 116 | if (VarDecl *V = dyn_cast<VarDecl>(VD)) { |
| 117 | if (V->getInit()) { |
| 118 | OS << " = "; |
| 119 | PrintExpr(V->getInit()); |
| 120 | } |
| 121 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 122 | } else { |
| 123 | // FIXME: "struct x;" |
| 124 | assert(0 && "Unexpected decl"); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | void StmtPrinter::VisitNullStmt(NullStmt *Node) { |
| 130 | Indent() << ";\n"; |
| 131 | } |
| 132 | |
| 133 | void StmtPrinter::VisitDeclStmt(DeclStmt *Node) { |
| 134 | for (Decl *D = Node->getDecl(); D; D = D->getNextDeclarator()) { |
| 135 | Indent(); |
| 136 | PrintRawDecl(D); |
| 137 | OS << ";\n"; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void StmtPrinter::VisitCompoundStmt(CompoundStmt *Node) { |
| 142 | Indent(); |
| 143 | PrintRawCompoundStmt(Node); |
| 144 | OS << "\n"; |
| 145 | } |
| 146 | |
| 147 | void StmtPrinter::VisitCaseStmt(CaseStmt *Node) { |
| 148 | Indent(-1) << "case "; |
| 149 | PrintExpr(Node->getLHS()); |
| 150 | if (Node->getRHS()) { |
| 151 | OS << " ... "; |
| 152 | PrintExpr(Node->getRHS()); |
| 153 | } |
| 154 | OS << ":\n"; |
| 155 | |
| 156 | PrintStmt(Node->getSubStmt(), 0); |
| 157 | } |
| 158 | |
| 159 | void StmtPrinter::VisitDefaultStmt(DefaultStmt *Node) { |
| 160 | Indent(-1) << "default:\n"; |
| 161 | PrintStmt(Node->getSubStmt(), 0); |
| 162 | } |
| 163 | |
| 164 | void StmtPrinter::VisitLabelStmt(LabelStmt *Node) { |
| 165 | Indent(-1) << Node->getName() << ":\n"; |
| 166 | PrintStmt(Node->getSubStmt(), 0); |
| 167 | } |
| 168 | |
| 169 | void StmtPrinter::PrintRawIfStmt(IfStmt *If) { |
| 170 | OS << "if "; |
| 171 | PrintExpr(If->getCond()); |
| 172 | |
| 173 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(If->getThen())) { |
| 174 | OS << ' '; |
| 175 | PrintRawCompoundStmt(CS); |
| 176 | OS << (If->getElse() ? ' ' : '\n'); |
| 177 | } else { |
| 178 | OS << '\n'; |
| 179 | PrintStmt(If->getThen()); |
| 180 | if (If->getElse()) Indent(); |
| 181 | } |
| 182 | |
| 183 | if (Stmt *Else = If->getElse()) { |
| 184 | OS << "else"; |
| 185 | |
| 186 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Else)) { |
| 187 | OS << ' '; |
| 188 | PrintRawCompoundStmt(CS); |
| 189 | OS << '\n'; |
| 190 | } else if (IfStmt *ElseIf = dyn_cast<IfStmt>(Else)) { |
| 191 | OS << ' '; |
| 192 | PrintRawIfStmt(ElseIf); |
| 193 | } else { |
| 194 | OS << '\n'; |
| 195 | PrintStmt(If->getElse()); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | void StmtPrinter::VisitIfStmt(IfStmt *If) { |
| 201 | Indent(); |
| 202 | PrintRawIfStmt(If); |
| 203 | } |
| 204 | |
| 205 | void StmtPrinter::VisitSwitchStmt(SwitchStmt *Node) { |
| 206 | Indent() << "switch ("; |
| 207 | PrintExpr(Node->getCond()); |
| 208 | OS << ")"; |
| 209 | |
| 210 | // Pretty print compoundstmt bodies (very common). |
| 211 | if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Node->getBody())) { |
| 212 | OS << " "; |
| 213 | PrintRawCompoundStmt(CS); |
| 214 | OS << "\n"; |
| 215 | } else { |
| 216 | OS << "\n"; |
| 217 | PrintStmt(Node->getBody()); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void StmtPrinter::VisitWhileStmt(WhileStmt *Node) { |
| 222 | Indent() << "while ("; |
| 223 | PrintExpr(Node->getCond()); |
| 224 | OS << ")\n"; |
| 225 | PrintStmt(Node->getBody()); |
| 226 | } |
| 227 | |
| 228 | void StmtPrinter::VisitDoStmt(DoStmt *Node) { |
| 229 | Indent() << "do\n"; |
| 230 | PrintStmt(Node->getBody()); |
| 231 | Indent() << "while "; |
| 232 | PrintExpr(Node->getCond()); |
| 233 | OS << ";\n"; |
| 234 | } |
| 235 | |
| 236 | void StmtPrinter::VisitForStmt(ForStmt *Node) { |
| 237 | Indent() << "for ("; |
| 238 | if (Node->getInit()) { |
| 239 | if (DeclStmt *DS = dyn_cast<DeclStmt>(Node->getInit())) |
| 240 | PrintRawDecl(DS->getDecl()); |
| 241 | else |
| 242 | PrintExpr(cast<Expr>(Node->getInit())); |
| 243 | } |
| 244 | OS << "; "; |
| 245 | if (Node->getCond()) |
| 246 | PrintExpr(Node->getCond()); |
| 247 | OS << "; "; |
| 248 | if (Node->getInc()) |
| 249 | PrintExpr(Node->getInc()); |
| 250 | OS << ")\n"; |
| 251 | PrintStmt(Node->getBody()); |
| 252 | } |
| 253 | |
| 254 | void StmtPrinter::VisitGotoStmt(GotoStmt *Node) { |
| 255 | Indent() << "goto " << Node->getLabel()->getName() << ";\n"; |
| 256 | } |
| 257 | |
| 258 | void StmtPrinter::VisitIndirectGotoStmt(IndirectGotoStmt *Node) { |
| 259 | Indent() << "goto *"; |
| 260 | PrintExpr(Node->getTarget()); |
| 261 | OS << ";\n"; |
| 262 | } |
| 263 | |
| 264 | void StmtPrinter::VisitContinueStmt(ContinueStmt *Node) { |
| 265 | Indent() << "continue;\n"; |
| 266 | } |
| 267 | |
| 268 | void StmtPrinter::VisitBreakStmt(BreakStmt *Node) { |
| 269 | Indent() << "break;\n"; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | void StmtPrinter::VisitReturnStmt(ReturnStmt *Node) { |
| 274 | Indent() << "return"; |
| 275 | if (Node->getRetValue()) { |
| 276 | OS << " "; |
| 277 | PrintExpr(Node->getRetValue()); |
| 278 | } |
| 279 | OS << ";\n"; |
| 280 | } |
| 281 | |
| 282 | //===----------------------------------------------------------------------===// |
| 283 | // Expr printing methods. |
| 284 | //===----------------------------------------------------------------------===// |
| 285 | |
| 286 | void StmtPrinter::VisitExpr(Expr *Node) { |
| 287 | OS << "<<unknown expr type>>"; |
| 288 | } |
| 289 | |
| 290 | void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) { |
| 291 | OS << Node->getDecl()->getName(); |
| 292 | } |
| 293 | |
| 294 | void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) { |
Chris Lattner | b0a721a | 2007-07-13 05:18:11 +0000 | [diff] [blame] | 295 | unsigned value = Node->getValue(); |
| 296 | if (isprint(value)) { |
| 297 | OS << "'" << (char)value << "'"; |
| 298 | } else { |
| 299 | // FIXME something to indicate this is a character literal? |
| 300 | OS << std::hex << std::setiosflags(std::ios_base::showbase) << value |
| 301 | << std::dec << std::resetiosflags(std::ios_base::showbase); |
| 302 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { |
| 306 | bool isSigned = Node->getType()->isSignedIntegerType(); |
| 307 | OS << Node->getValue().toString(10, isSigned); |
| 308 | |
| 309 | // Emit suffixes. Integer literals are always a builtin integer type. |
| 310 | switch (cast<BuiltinType>(Node->getType().getCanonicalType())->getKind()) { |
| 311 | default: assert(0 && "Unexpected type for integer literal!"); |
| 312 | case BuiltinType::Int: break; // no suffix. |
| 313 | case BuiltinType::UInt: OS << 'U'; break; |
| 314 | case BuiltinType::Long: OS << 'L'; break; |
| 315 | case BuiltinType::ULong: OS << "UL"; break; |
| 316 | case BuiltinType::LongLong: OS << "LL"; break; |
| 317 | case BuiltinType::ULongLong: OS << "ULL"; break; |
| 318 | } |
| 319 | } |
| 320 | void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { |
| 321 | // FIXME: print value. |
| 322 | OS << "~1.0~"; |
| 323 | } |
| 324 | void StmtPrinter::VisitStringLiteral(StringLiteral *Str) { |
| 325 | if (Str->isWide()) OS << 'L'; |
| 326 | OS << '"'; |
| 327 | |
| 328 | // FIXME: this doesn't print wstrings right. |
| 329 | for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) { |
| 330 | switch (Str->getStrData()[i]) { |
| 331 | default: OS << Str->getStrData()[i]; break; |
| 332 | // Handle some common ones to make dumps prettier. |
| 333 | case '\\': OS << "\\\\"; break; |
| 334 | case '"': OS << "\\\""; break; |
| 335 | case '\n': OS << "\\n"; break; |
| 336 | case '\t': OS << "\\t"; break; |
| 337 | case '\a': OS << "\\a"; break; |
| 338 | case '\b': OS << "\\b"; break; |
| 339 | } |
| 340 | } |
| 341 | OS << '"'; |
| 342 | } |
| 343 | void StmtPrinter::VisitParenExpr(ParenExpr *Node) { |
| 344 | OS << "("; |
| 345 | PrintExpr(Node->getSubExpr()); |
| 346 | OS << ")"; |
| 347 | } |
| 348 | void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) { |
| 349 | if (!Node->isPostfix()) |
| 350 | OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); |
| 351 | PrintExpr(Node->getSubExpr()); |
| 352 | |
| 353 | if (Node->isPostfix()) |
| 354 | OS << UnaryOperator::getOpcodeStr(Node->getOpcode()); |
| 355 | |
| 356 | } |
| 357 | void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) { |
| 358 | OS << (Node->isSizeOf() ? "sizeof(" : "__alignof("); |
| 359 | OS << Node->getArgumentType().getAsString() << ")"; |
| 360 | } |
| 361 | void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) { |
| 362 | PrintExpr(Node->getBase()); |
| 363 | OS << "["; |
| 364 | PrintExpr(Node->getIdx()); |
| 365 | OS << "]"; |
| 366 | } |
| 367 | |
| 368 | void StmtPrinter::VisitCallExpr(CallExpr *Call) { |
| 369 | PrintExpr(Call->getCallee()); |
| 370 | OS << "("; |
| 371 | for (unsigned i = 0, e = Call->getNumArgs(); i != e; ++i) { |
| 372 | if (i) OS << ", "; |
| 373 | PrintExpr(Call->getArg(i)); |
| 374 | } |
| 375 | OS << ")"; |
| 376 | } |
| 377 | void StmtPrinter::VisitMemberExpr(MemberExpr *Node) { |
| 378 | PrintExpr(Node->getBase()); |
| 379 | OS << (Node->isArrow() ? "->" : "."); |
| 380 | |
| 381 | FieldDecl *Field = Node->getMemberDecl(); |
| 382 | assert(Field && "MemberExpr should alway reference a field!"); |
| 383 | OS << Field->getName(); |
| 384 | } |
| 385 | void StmtPrinter::VisitCastExpr(CastExpr *Node) { |
| 386 | OS << "(" << Node->getDestType().getAsString() << ")"; |
| 387 | PrintExpr(Node->getSubExpr()); |
| 388 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 389 | void StmtPrinter::VisitImplicitCastExpr(ImplicitCastExpr *Node) { |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame^] | 390 | // No need to print anything, simply forward to the sub expression. |
| 391 | PrintExpr(Node->getSubExpr()); |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 392 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 393 | void StmtPrinter::VisitBinaryOperator(BinaryOperator *Node) { |
| 394 | PrintExpr(Node->getLHS()); |
| 395 | OS << " " << BinaryOperator::getOpcodeStr(Node->getOpcode()) << " "; |
| 396 | PrintExpr(Node->getRHS()); |
| 397 | } |
| 398 | void StmtPrinter::VisitConditionalOperator(ConditionalOperator *Node) { |
| 399 | PrintExpr(Node->getCond()); |
| 400 | OS << " ? "; |
| 401 | PrintExpr(Node->getLHS()); |
| 402 | OS << " : "; |
| 403 | PrintExpr(Node->getRHS()); |
| 404 | } |
| 405 | |
| 406 | // GNU extensions. |
| 407 | |
| 408 | void StmtPrinter::VisitAddrLabel(AddrLabel *Node) { |
| 409 | OS << "&&" << Node->getLabel()->getName(); |
| 410 | |
| 411 | } |
| 412 | |
| 413 | // C++ |
| 414 | |
| 415 | void StmtPrinter::VisitCXXCastExpr(CXXCastExpr *Node) { |
| 416 | switch (Node->getOpcode()) { |
| 417 | default: |
| 418 | assert(0 && "Not a C++ cast expression"); |
| 419 | abort(); |
| 420 | case CXXCastExpr::ConstCast: OS << "const_cast<"; break; |
| 421 | case CXXCastExpr::DynamicCast: OS << "dynamic_cast<"; break; |
| 422 | case CXXCastExpr::ReinterpretCast: OS << "reinterpret_cast<"; break; |
| 423 | case CXXCastExpr::StaticCast: OS << "static_cast<"; break; |
| 424 | } |
| 425 | |
| 426 | OS << Node->getDestType().getAsString() << ">("; |
| 427 | PrintExpr(Node->getSubExpr()); |
| 428 | OS << ")"; |
| 429 | } |
| 430 | |
| 431 | void StmtPrinter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *Node) { |
| 432 | OS << (Node->getValue() ? "true" : "false"); |
| 433 | } |
| 434 | |
| 435 | |
| 436 | //===----------------------------------------------------------------------===// |
| 437 | // Stmt method implementations |
| 438 | //===----------------------------------------------------------------------===// |
| 439 | |
| 440 | void Stmt::dump() const { |
| 441 | // FIXME: eliminate use of <iostream> |
| 442 | print(std::cerr); |
| 443 | } |
| 444 | |
| 445 | void Stmt::print(std::ostream &OS) const { |
| 446 | if (this == 0) { |
| 447 | OS << "<NULL>"; |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | StmtPrinter P(OS); |
| 452 | const_cast<Stmt*>(this)->visit(P); |
| 453 | } |